From 184cbc5564d757080df410690733ebb010557657 Mon Sep 17 00:00:00 2001 From: David Turner Date: Wed, 20 Oct 2021 16:13:18 +0100 Subject: [PATCH 1/2] `CoordinatorTests` sometimes needs three term bumps Today we require the cluster to stabilise in a time period that allows time for the first election to encounter conflicts. However on very rare occasions there might be an election conflict in the second election too. This commit extends the stabilisation timeout to allow for this. Similar to #64462 Closes #78370 --- ...rnalClusterInfoServiceSchedulingTests.java | 2 +- .../snapshots/SnapshotResiliencyTests.java | 2 +- .../AbstractCoordinatorTestCase.java | 25 +++++++++++++++---- .../MockSinglePrioritizingExecutor.java | 19 +++++++++++--- .../MockSinglePrioritizingExecutorTests.java | 7 +++++- 5 files changed, 44 insertions(+), 11 deletions(-) diff --git a/server/src/test/java/org/elasticsearch/cluster/InternalClusterInfoServiceSchedulingTests.java b/server/src/test/java/org/elasticsearch/cluster/InternalClusterInfoServiceSchedulingTests.java index 03edc291843eb..aa56af7a627fe 100644 --- a/server/src/test/java/org/elasticsearch/cluster/InternalClusterInfoServiceSchedulingTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/InternalClusterInfoServiceSchedulingTests.java @@ -59,7 +59,7 @@ public void testScheduling() { final ClusterApplierService clusterApplierService = new ClusterApplierService("test", settings, clusterSettings, threadPool) { @Override protected PrioritizedEsThreadPoolExecutor createThreadPoolExecutor() { - return new MockSinglePrioritizingExecutor("mock-executor", deterministicTaskQueue, threadPool); + return new MockSinglePrioritizingExecutor("mock-executor", "", deterministicTaskQueue, threadPool); } }; diff --git a/server/src/test/java/org/elasticsearch/snapshots/SnapshotResiliencyTests.java b/server/src/test/java/org/elasticsearch/snapshots/SnapshotResiliencyTests.java index d7ca3fbc93b4c..b65e145e4610f 100644 --- a/server/src/test/java/org/elasticsearch/snapshots/SnapshotResiliencyTests.java +++ b/server/src/test/java/org/elasticsearch/snapshots/SnapshotResiliencyTests.java @@ -1628,7 +1628,7 @@ private final class TestClusterNode { new ClusterApplierService(node.getName(), settings, clusterSettings, threadPool) { @Override protected PrioritizedEsThreadPoolExecutor createThreadPoolExecutor() { - return new MockSinglePrioritizingExecutor(node.getName(), deterministicTaskQueue, threadPool); + return new MockSinglePrioritizingExecutor(node.getName(), node.getId(), deterministicTaskQueue, threadPool); } @Override diff --git a/test/framework/src/main/java/org/elasticsearch/cluster/coordination/AbstractCoordinatorTestCase.java b/test/framework/src/main/java/org/elasticsearch/cluster/coordination/AbstractCoordinatorTestCase.java index dc9424488a761..b98f0926c43f1 100644 --- a/test/framework/src/main/java/org/elasticsearch/cluster/coordination/AbstractCoordinatorTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/cluster/coordination/AbstractCoordinatorTestCase.java @@ -235,6 +235,8 @@ protected static int defaultInt(Setting setting) { + DEFAULT_ELECTION_DELAY // perhaps there is an election collision requiring another publication (which times out) and a term bump + defaultMillis(PUBLISH_TIMEOUT_SETTING) + DEFAULT_ELECTION_DELAY + // very rarely there is another election collision requiring another publication (which times out) and a term bump + + defaultMillis(PUBLISH_TIMEOUT_SETTING) + DEFAULT_ELECTION_DELAY // then wait for the new leader to notice that the old leader is unresponsive + (defaultMillis(FOLLOWER_CHECK_INTERVAL_SETTING) + defaultMillis(FOLLOWER_CHECK_TIMEOUT_SETTING)) * defaultInt(FOLLOWER_CHECK_RETRY_COUNT_SETTING) @@ -1029,8 +1031,13 @@ protected void onSendRequest( masterService = new AckedFakeThreadPoolMasterService(localNode.getId(), "test", threadPool, runnable -> deterministicTaskQueue.scheduleNow(onNode(runnable))); final ClusterSettings clusterSettings = new ClusterSettings(settings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); - clusterApplierService = new DisruptableClusterApplierService(localNode.getId(), settings, clusterSettings, - deterministicTaskQueue, threadPool); + clusterApplierService = new DisruptableClusterApplierService( + localNode.getId(), + localNode.getEphemeralId(), + settings, + clusterSettings, + deterministicTaskQueue, + threadPool); clusterService = new ClusterService(settings, clusterSettings, masterService, clusterApplierService); clusterService.setNodeConnectionsService( new NodeConnectionsService(clusterService.getSettings(), threadPool, transportService)); @@ -1442,15 +1449,23 @@ public void onNodeAck(DiscoveryNode node, Exception e) { static class DisruptableClusterApplierService extends ClusterApplierService { private final String nodeName; + private final String nodeId; private final DeterministicTaskQueue deterministicTaskQueue; private final ThreadPool threadPool; ClusterStateApplyResponse clusterStateApplyResponse = ClusterStateApplyResponse.SUCCEED; private boolean applicationMayFail; - DisruptableClusterApplierService(String nodeName, Settings settings, ClusterSettings clusterSettings, - DeterministicTaskQueue deterministicTaskQueue, ThreadPool threadPool) { + DisruptableClusterApplierService( + String nodeName, + String nodeId, + Settings settings, + ClusterSettings clusterSettings, + DeterministicTaskQueue deterministicTaskQueue, + ThreadPool threadPool + ) { super(nodeName, settings, clusterSettings, threadPool); this.nodeName = nodeName; + this.nodeId = nodeId; this.deterministicTaskQueue = deterministicTaskQueue; this.threadPool = threadPool; addStateApplier(event -> { @@ -1470,7 +1485,7 @@ static class DisruptableClusterApplierService extends ClusterApplierService { @Override protected PrioritizedEsThreadPoolExecutor createThreadPoolExecutor() { - return new MockSinglePrioritizingExecutor(nodeName, deterministicTaskQueue, threadPool); + return new MockSinglePrioritizingExecutor(nodeName, nodeId, deterministicTaskQueue, threadPool); } @Override diff --git a/test/framework/src/main/java/org/elasticsearch/cluster/coordination/MockSinglePrioritizingExecutor.java b/test/framework/src/main/java/org/elasticsearch/cluster/coordination/MockSinglePrioritizingExecutor.java index 38225891b52ac..4addf2b338e79 100644 --- a/test/framework/src/main/java/org/elasticsearch/cluster/coordination/MockSinglePrioritizingExecutor.java +++ b/test/framework/src/main/java/org/elasticsearch/cluster/coordination/MockSinglePrioritizingExecutor.java @@ -7,6 +7,7 @@ */ package org.elasticsearch.cluster.coordination; +import org.apache.logging.log4j.CloseableThreadContext; import org.elasticsearch.common.util.concurrent.DeterministicTaskQueue; import org.elasticsearch.common.util.concurrent.EsExecutors; import org.elasticsearch.common.util.concurrent.PrioritizedEsThreadPoolExecutor; @@ -14,15 +15,22 @@ import java.util.concurrent.TimeUnit; +import static org.elasticsearch.common.util.concurrent.DeterministicTaskQueue.NODE_ID_LOG_CONTEXT_KEY; + /** * Mock single threaded {@link PrioritizedEsThreadPoolExecutor} based on {@link DeterministicTaskQueue}, * simulating the behaviour of an executor returned by {@link EsExecutors#newSinglePrioritizing}. */ public class MockSinglePrioritizingExecutor extends PrioritizedEsThreadPoolExecutor { - public MockSinglePrioritizingExecutor(String name, DeterministicTaskQueue deterministicTaskQueue, ThreadPool threadPool) { + public MockSinglePrioritizingExecutor( + String nodeName, + String nodeId, + DeterministicTaskQueue deterministicTaskQueue, + ThreadPool threadPool + ) { super( - name, + nodeName, 0, 1, 0L, @@ -34,7 +42,12 @@ public void start() { @Override public void run() { try { - r.run(); + try (CloseableThreadContext.Instance ignored = CloseableThreadContext.put( + NODE_ID_LOG_CONTEXT_KEY, + '{' + nodeName + "}{" + nodeId + '}' + )) { + r.run(); + } } catch (KillWorkerError kwe) { // hacks everywhere } diff --git a/test/framework/src/test/java/org/elasticsearch/cluster/coordination/MockSinglePrioritizingExecutorTests.java b/test/framework/src/test/java/org/elasticsearch/cluster/coordination/MockSinglePrioritizingExecutorTests.java index 2594643604e93..7d351d820ff6e 100644 --- a/test/framework/src/test/java/org/elasticsearch/cluster/coordination/MockSinglePrioritizingExecutorTests.java +++ b/test/framework/src/test/java/org/elasticsearch/cluster/coordination/MockSinglePrioritizingExecutorTests.java @@ -20,7 +20,12 @@ public class MockSinglePrioritizingExecutorTests extends ESTestCase { public void testPrioritizedEsThreadPoolExecutor() { final DeterministicTaskQueue taskQueue = new DeterministicTaskQueue(); - final PrioritizedEsThreadPoolExecutor executor = new MockSinglePrioritizingExecutor("test", taskQueue, taskQueue.getThreadPool()); + final PrioritizedEsThreadPoolExecutor executor = new MockSinglePrioritizingExecutor( + "test", + "", + taskQueue, + taskQueue.getThreadPool() + ); final AtomicBoolean called1 = new AtomicBoolean(); final AtomicBoolean called2 = new AtomicBoolean(); executor.execute(new PrioritizedRunnable(Priority.NORMAL) { From 333aef2ae3ed2329b75f24a72940ef5dc685894c Mon Sep 17 00:00:00 2001 From: David Turner Date: Thu, 28 Oct 2021 10:34:44 +0100 Subject: [PATCH 2/2] Reformat the world --- .../client/benchmark/AbstractBenchmark.java | 29 +- .../client/benchmark/BenchmarkRunner.java | 32 +- .../client/benchmark/metrics/Metrics.java | 22 +- .../benchmark/metrics/MetricsCalculator.java | 40 +- .../benchmark/ops/bulk/BulkBenchmarkTask.java | 31 +- .../ops/search/SearchBenchmarkTask.java | 9 +- .../benchmark/rest/RestClientBenchmark.java | 8 +- .../elasticsearch/plugin/noop/NoopPlugin.java | 22 +- .../noop/action/bulk/RestNoopBulkAction.java | 27 +- .../action/bulk/TransportNoopBulkAction.java | 7 +- .../action/search/RestNoopSearchAction.java | 3 +- .../search/TransportNoopSearchAction.java | 27 +- .../elasticsearch/client/EnrollmentIT.java | 22 +- .../client/AsyncSearchClient.java | 70 +- .../client/AsyncSearchRequestConverters.java | 20 +- .../org/elasticsearch/client/CcrClient.java | 135 +- .../client/CcrRequestConverters.java | 46 +- .../elasticsearch/client/ClusterClient.java | 221 +- .../client/ClusterRequestConverters.java | 17 +- .../elasticsearch/client/EnrichClient.java | 28 +- .../client/EnrichRequestConverters.java | 16 +- .../org/elasticsearch/client/EqlClient.java | 9 +- .../client/EqlRequestConverters.java | 7 +- .../elasticsearch/client/FeaturesClient.java | 18 +- .../client/GeoIpStatsResponse.java | 50 +- .../client/GetAliasesResponse.java | 2 +- .../org/elasticsearch/client/GraphClient.java | 30 +- .../client/IndexLifecycleClient.java | 478 +- .../IndexLifecycleRequestConverters.java | 137 +- .../elasticsearch/client/IndicesClient.java | 1151 +- .../client/IndicesRequestConverters.java | 76 +- .../elasticsearch/client/IngestClient.java | 111 +- .../client/IngestRequestConverters.java | 9 +- .../elasticsearch/client/LicenseClient.java | 154 +- .../client/LicenseRequestConverters.java | 8 +- .../client/MLRequestConverters.java | 362 +- .../client/MachineLearningClient.java | 1375 +- .../elasticsearch/client/MigrationClient.java | 46 +- .../client/MigrationRequestConverters.java | 14 +- .../elasticsearch/client/NodesResponse.java | 2 +- .../client/NodesResponseHeader.java | 38 +- .../client/RequestConverters.java | 128 +- .../client/RestHighLevelClient.java | 1133 +- .../client/RethrottleRequest.java | 4 +- .../elasticsearch/client/RollupClient.java | 176 +- .../client/RollupRequestConverters.java | 24 +- .../client/SearchableSnapshotsClient.java | 8 +- .../SearchableSnapshotsRequestConverters.java | 6 +- .../elasticsearch/client/SecurityClient.java | 971 +- .../client/SecurityRequestConverters.java | 68 +- .../elasticsearch/client/SnapshotClient.java | 309 +- .../client/SnapshotRequestConverters.java | 16 +- .../org/elasticsearch/client/TasksClient.java | 48 +- .../client/TasksRequestConverters.java | 13 +- .../client/TextStructureClient.java | 20 +- .../TextStructureRequestConverters.java | 30 +- .../elasticsearch/client/TransformClient.java | 272 +- .../org/elasticsearch/client/Validatable.java | 3 +- .../elasticsearch/client/WatcherClient.java | 246 +- .../client/WatcherRequestConverters.java | 38 +- .../org/elasticsearch/client/XPackClient.java | 41 +- .../client/XPackRequestConverters.java | 7 +- .../InferencePipelineAggregationBuilder.java | 24 +- .../client/analytics/ParsedInference.java | 32 +- .../client/analytics/ParsedStringStats.java | 65 +- .../client/analytics/ParsedTopMetrics.java | 46 +- .../StringStatsAggregationBuilder.java | 14 +- .../TopMetricsAggregationBuilder.java | 6 +- .../asyncsearch/AsyncSearchResponse.java | 48 +- .../asyncsearch/DeleteAsyncSearchRequest.java | 3 +- .../asyncsearch/GetAsyncSearchRequest.java | 5 +- .../asyncsearch/SubmitAsyncSearchRequest.java | 8 +- .../client/ccr/AutoFollowStats.java | 47 +- .../client/ccr/CcrStatsRequest.java | 3 +- .../client/ccr/CcrStatsResponse.java | 15 +- .../client/ccr/FollowConfig.java | 53 +- .../client/ccr/FollowInfoResponse.java | 37 +- .../client/ccr/ForgetFollowerRequest.java | 11 +- .../ccr/GetAutoFollowPatternResponse.java | 76 +- .../client/ccr/IndicesFollowStats.java | 178 +- .../ccr/PutAutoFollowPatternRequest.java | 24 +- .../client/ccr/PutFollowRequest.java | 15 +- .../client/ccr/PutFollowResponse.java | 13 +- .../client/cluster/ProxyModeInfo.java | 8 +- .../client/cluster/RemoteConnectionInfo.java | 42 +- .../client/cluster/SniffModeInfo.java | 6 +- .../elasticsearch/client/common/TimeUtil.java | 6 +- .../client/core/AcknowledgedResponse.java | 9 +- .../client/core/BroadcastResponse.java | 40 +- .../client/core/CountRequest.java | 23 +- .../client/core/CountResponse.java | 36 +- .../client/core/GetSourceRequest.java | 3 +- .../client/core/IndexerJobStats.java | 94 +- .../client/core/IndexerState.java | 1 - .../client/core/MainRequest.java | 3 +- .../client/core/MainResponse.java | 91 +- .../client/core/MultiTermVectorsResponse.java | 14 +- .../elasticsearch/client/core/PageParams.java | 11 +- .../core/ShardsAcknowledgedResponse.java | 10 +- .../client/core/TermVectorsRequest.java | 17 +- .../client/core/TermVectorsResponse.java | 77 +- .../client/enrich/ExecutePolicyResponse.java | 2 +- .../client/enrich/GetPolicyResponse.java | 12 +- .../client/enrich/NamedPolicy.java | 2 +- .../client/enrich/PutPolicyRequest.java | 20 +- .../client/enrich/StatsRequest.java | 3 +- .../client/enrich/StatsResponse.java | 36 +- .../client/eql/EqlSearchRequest.java | 37 +- .../client/eql/EqlSearchResponse.java | 113 +- .../client/eql/EqlStatsRequest.java | 3 +- .../client/eql/EqlStatsResponse.java | 24 +- .../client/feature/GetFeaturesRequest.java | 3 +- .../client/feature/GetFeaturesResponse.java | 18 +- .../client/feature/ResetFeaturesRequest.java | 3 +- .../client/feature/ResetFeaturesResponse.java | 25 +- .../client/graph/Connection.java | 59 +- .../client/graph/GraphExploreRequest.java | 10 +- .../client/graph/GraphExploreResponse.java | 85 +- .../org/elasticsearch/client/graph/Hop.java | 6 +- .../elasticsearch/client/graph/Vertex.java | 64 +- .../client/graph/VertexRequest.java | 4 +- .../client/ilm/AllocateAction.java | 28 +- .../client/ilm/DeleteAction.java | 3 +- .../ilm/DeleteLifecyclePolicyRequest.java | 1 - .../client/ilm/ExplainLifecycleRequest.java | 3 +- .../client/ilm/ExplainLifecycleResponse.java | 17 +- .../client/ilm/ForceMergeAction.java | 8 +- .../client/ilm/FreezeAction.java | 3 +- .../client/ilm/GetLifecyclePolicyRequest.java | 1 - .../ilm/GetLifecyclePolicyResponse.java | 2 +- .../ilm/IndexLifecycleExplainResponse.java | 148 +- .../IndexLifecycleNamedXContentProvider.java | 57 +- .../ilm/LifecycleManagementStatusRequest.java | 3 +- .../LifecycleManagementStatusResponse.java | 9 +- .../client/ilm/LifecyclePolicy.java | 63 +- .../client/ilm/LifecyclePolicyMetadata.java | 26 +- .../client/ilm/MigrateAction.java | 8 +- .../client/ilm/OperationMode.java | 6 +- .../org/elasticsearch/client/ilm/Phase.java | 36 +- .../client/ilm/PhaseExecutionInfo.java | 17 +- .../client/ilm/ReadOnlyAction.java | 3 +- .../RemoveIndexLifecyclePolicyRequest.java | 7 +- .../RemoveIndexLifecyclePolicyResponse.java | 7 +- .../ilm/RetryLifecyclePolicyRequest.java | 3 +- .../client/ilm/RolloverAction.java | 38 +- .../client/ilm/SearchableSnapshotAction.java | 13 +- .../client/ilm/SetPriorityAction.java | 22 +- .../client/ilm/ShrinkAction.java | 21 +- .../client/ilm/StartILMRequest.java | 3 +- .../client/ilm/StopILMRequest.java | 3 +- .../client/ilm/WaitForSnapshotAction.java | 9 +- .../client/indices/AnalyzeRequest.java | 9 +- .../client/indices/AnalyzeResponse.java | 33 +- .../client/indices/CloseIndexResponse.java | 49 +- .../client/indices/CreateIndexRequest.java | 20 +- .../client/indices/CreateIndexResponse.java | 9 +- .../client/indices/DataStream.java | 100 +- .../indices/DataStreamsStatsResponse.java | 116 +- .../client/indices/DetailAnalyzeResponse.java | 72 +- .../GetComponentTemplatesResponse.java | 25 +- .../GetComposableIndexTemplatesResponse.java | 25 +- .../client/indices/GetDataStreamResponse.java | 1 - .../indices/GetFieldMappingsResponse.java | 53 +- .../client/indices/GetIndexRequest.java | 1 - .../client/indices/GetIndexResponse.java | 26 +- .../indices/GetIndexTemplatesRequest.java | 2 +- .../indices/GetIndexTemplatesResponse.java | 14 +- .../client/indices/GetMappingsResponse.java | 13 +- .../client/indices/IndexTemplateMetadata.java | 78 +- .../indices/PutIndexTemplateRequest.java | 39 +- .../indices/ReloadAnalyzersResponse.java | 40 +- .../client/indices/ResizeResponse.java | 9 +- .../indices/SimulateIndexTemplateRequest.java | 2 +- .../SimulateIndexTemplateResponse.java | 38 +- .../indices/rollover/RolloverRequest.java | 4 +- .../indices/rollover/RolloverResponse.java | 39 +- .../license/GetBasicStatusResponse.java | 15 +- .../client/license/GetLicenseRequest.java | 4 +- .../client/license/GetLicenseResponse.java | 3 +- .../license/GetTrialStatusResponse.java | 15 +- .../client/license/LicensesStatus.java | 1 - .../client/license/PutLicenseRequest.java | 3 +- .../client/license/PutLicenseResponse.java | 98 +- .../client/license/StartBasicRequest.java | 1 - .../client/license/StartBasicResponse.java | 77 +- .../client/license/StartTrialResponse.java | 19 +- .../migration/DeprecationInfoResponse.java | 52 +- .../GetFeatureUpgradeStatusResponse.java | 46 +- .../migration/PostFeatureUpgradeResponse.java | 46 +- .../client/ml/AbstractResultResponse.java | 3 +- .../client/ml/CloseJobRequest.java | 23 +- .../client/ml/CloseJobResponse.java | 9 +- .../client/ml/DeleteCalendarEventRequest.java | 3 +- .../client/ml/DeleteCalendarJobRequest.java | 3 +- .../ml/DeleteDataFrameAnalyticsRequest.java | 4 +- .../client/ml/DeleteExpiredDataRequest.java | 8 +- .../client/ml/DeleteExpiredDataResponse.java | 11 +- .../client/ml/DeleteForecastRequest.java | 19 +- .../client/ml/DeleteJobResponse.java | 11 +- .../ml/DeleteTrainedModelAliasRequest.java | 3 +- .../client/ml/EstimateModelMemoryRequest.java | 6 +- .../ml/EstimateModelMemoryResponse.java | 12 +- .../client/ml/EvaluateDataFrameRequest.java | 23 +- .../client/ml/EvaluateDataFrameResponse.java | 16 +- .../ml/ExplainDataFrameAnalyticsResponse.java | 14 +- .../client/ml/FlushJobRequest.java | 20 +- .../client/ml/FlushJobResponse.java | 26 +- .../client/ml/ForecastJobRequest.java | 15 +- .../client/ml/ForecastJobResponse.java | 14 +- .../client/ml/GetBucketsRequest.java | 22 +- .../client/ml/GetBucketsResponse.java | 9 +- .../client/ml/GetCalendarEventsRequest.java | 8 +- .../client/ml/GetCalendarEventsResponse.java | 12 +- .../client/ml/GetCalendarsRequest.java | 9 +- .../client/ml/GetCalendarsResponse.java | 10 +- .../client/ml/GetCategoriesRequest.java | 7 +- .../client/ml/GetCategoriesResponse.java | 10 +- .../ml/GetDataFrameAnalyticsResponse.java | 12 +- .../GetDataFrameAnalyticsStatsResponse.java | 36 +- .../client/ml/GetDatafeedRequest.java | 12 +- .../client/ml/GetDatafeedResponse.java | 10 +- .../client/ml/GetDatafeedStatsRequest.java | 18 +- .../client/ml/GetDatafeedStatsResponse.java | 11 +- .../client/ml/GetFiltersRequest.java | 7 +- .../client/ml/GetFiltersResponse.java | 10 +- .../client/ml/GetInfluencersRequest.java | 22 +- .../client/ml/GetInfluencersResponse.java | 7 +- .../client/ml/GetJobRequest.java | 12 +- .../client/ml/GetJobResponse.java | 10 +- .../client/ml/GetJobStatsRequest.java | 19 +- .../client/ml/GetJobStatsResponse.java | 14 +- .../client/ml/GetModelSnapshotsRequest.java | 12 +- .../client/ml/GetModelSnapshotsResponse.java | 10 +- .../client/ml/GetOverallBucketsRequest.java | 22 +- .../client/ml/GetOverallBucketsResponse.java | 7 +- .../client/ml/GetRecordsRequest.java | 18 +- .../client/ml/GetRecordsResponse.java | 9 +- .../client/ml/GetTrainedModelsResponse.java | 13 +- .../ml/GetTrainedModelsStatsResponse.java | 13 +- .../client/ml/MlInfoRequest.java | 3 +- .../client/ml/NodeAttributes.java | 37 +- .../client/ml/OpenJobRequest.java | 7 +- .../client/ml/OpenJobResponse.java | 13 +- .../client/ml/PostCalendarEventRequest.java | 17 +- .../client/ml/PostCalendarEventResponse.java | 16 +- .../client/ml/PostDataRequest.java | 23 +- .../client/ml/PreviewDatafeedRequest.java | 15 +- .../client/ml/PreviewDatafeedResponse.java | 8 +- .../client/ml/PutCalendarJobRequest.java | 3 +- .../client/ml/PutTrainedModelRequest.java | 1 - .../client/ml/PutTrainedModelResponse.java | 1 - .../client/ml/RevertModelSnapshotRequest.java | 8 +- .../ml/RevertModelSnapshotResponse.java | 12 +- .../client/ml/SetUpgradeModeRequest.java | 3 +- .../ml/StartDataFrameAnalyticsRequest.java | 3 +- .../ml/StartDataFrameAnalyticsResponse.java | 15 +- .../client/ml/StartDatafeedRequest.java | 19 +- .../client/ml/StartDatafeedResponse.java | 12 +- .../ml/StopDataFrameAnalyticsRequest.java | 2 +- .../ml/StopDataFrameAnalyticsResponse.java | 17 +- .../client/ml/StopDatafeedRequest.java | 22 +- .../client/ml/StopDatafeedResponse.java | 12 +- .../client/ml/UpdateFilterRequest.java | 14 +- .../client/ml/UpdateModelSnapshotRequest.java | 6 +- .../ml/UpdateModelSnapshotResponse.java | 16 +- .../ml/UpgradeJobModelSnapshotRequest.java | 18 +- .../ml/UpgradeJobModelSnapshotResponse.java | 13 +- .../client/ml/calendars/Calendar.java | 10 +- .../client/ml/calendars/ScheduledEvent.java | 33 +- .../client/ml/datafeed/ChunkingConfig.java | 18 +- .../client/ml/datafeed/DatafeedConfig.java | 92 +- .../client/ml/datafeed/DatafeedState.java | 5 +- .../client/ml/datafeed/DatafeedStats.java | 37 +- .../ml/datafeed/DatafeedTimingStats.java | 55 +- .../client/ml/datafeed/DatafeedUpdate.java | 98 +- .../ml/datafeed/DelayedDataCheckConfig.java | 12 +- .../client/ml/dataframe/Classification.java | 152 +- .../dataframe/DataFrameAnalyticsConfig.java | 82 +- .../DataFrameAnalyticsConfigUpdate.java | 17 +- .../ml/dataframe/DataFrameAnalyticsDest.java | 7 +- .../dataframe/DataFrameAnalyticsSource.java | 22 +- .../ml/dataframe/DataFrameAnalyticsState.java | 8 +- .../ml/dataframe/DataFrameAnalyticsStats.java | 51 +- ...ataFrameAnalysisNamedXContentProvider.java | 18 +- .../client/ml/dataframe/OutlierDetection.java | 37 +- .../client/ml/dataframe/PhaseProgress.java | 12 +- .../client/ml/dataframe/QueryConfig.java | 4 +- .../client/ml/dataframe/Regression.java | 148 +- .../MlEvaluationNamedXContentProvider.java | 120 +- .../classification/AccuracyMetric.java | 12 +- .../classification/AucRocMetric.java | 12 +- .../classification/Classification.java | 41 +- .../MulticlassConfusionMatrixMetric.java | 50 +- .../classification/PerClassSingleValue.java | 12 +- .../classification/PrecisionMetric.java | 12 +- .../classification/RecallMetric.java | 12 +- .../evaluation/common/AucRocPoint.java | 15 +- .../evaluation/common/AucRocResult.java | 15 +- .../AbstractConfusionMatrixMetric.java | 5 +- .../outlierdetection/AucRocMetric.java | 9 +- .../ConfusionMatrixMetric.java | 19 +- .../outlierdetection/OutlierDetection.java | 22 +- .../outlierdetection/PrecisionMetric.java | 6 +- .../outlierdetection/RecallMetric.java | 6 +- .../evaluation/regression/HuberMetric.java | 16 +- .../regression/MeanSquaredErrorMetric.java | 9 +- .../MeanSquaredLogarithmicErrorMetric.java | 16 +- .../evaluation/regression/RSquaredMetric.java | 9 +- .../evaluation/regression/Regression.java | 24 +- .../ml/dataframe/explain/FieldSelection.java | 30 +- .../dataframe/explain/MemoryEstimation.java | 17 +- .../AnalysisStatsNamedXContentProvider.java | 8 +- .../classification/ClassificationStats.java | 28 +- .../stats/classification/Hyperparameters.java | 43 +- .../stats/classification/TimingStats.java | 9 +- .../stats/classification/ValidationLoss.java | 8 +- .../ml/dataframe/stats/common/DataCounts.java | 24 +- .../ml/dataframe/stats/common/FoldValues.java | 9 +- .../dataframe/stats/common/MemoryUsage.java | 20 +- .../OutlierDetectionStats.java | 14 +- .../stats/outlierdetection/Parameters.java | 35 +- .../stats/outlierdetection/TimingStats.java | 8 +- .../stats/regression/Hyperparameters.java | 41 +- .../stats/regression/RegressionStats.java | 28 +- .../stats/regression/TimingStats.java | 9 +- .../stats/regression/ValidationLoss.java | 8 +- .../InferenceToXContentCompressor.java | 24 +- .../MlInferenceNamedXContentProvider.java | 90 +- .../inference/NamedXContentObjectHelper.java | 22 +- .../inference/SimpleBoundedInputStream.java | 2 - .../ml/inference/TrainedModelConfig.java | 110 +- .../ml/inference/TrainedModelDefinition.java | 33 +- .../ml/inference/TrainedModelInput.java | 8 +- .../ml/inference/TrainedModelStats.java | 14 +- .../client/ml/inference/TrainedModelType.java | 4 +- .../preprocessing/CustomWordEmbedding.java | 80 +- .../preprocessing/FrequencyEncoding.java | 12 +- .../ml/inference/preprocessing/Multi.java | 20 +- .../ml/inference/preprocessing/NGram.java | 32 +- .../preprocessing/OneHotEncoding.java | 10 +- .../inference/preprocessing/PreProcessor.java | 1 - .../preprocessing/TargetMeanEncoding.java | 12 +- .../inference/results/FeatureImportance.java | 38 +- .../ml/inference/results/TopClassEntry.java | 24 +- .../trainedmodel/ClassificationConfig.java | 11 +- .../inference/trainedmodel/IndexLocation.java | 9 +- .../trainedmodel/InferenceConfig.java | 1 - .../trainedmodel/InferenceStats.java | 53 +- .../trainedmodel/RegressionConfig.java | 13 +- .../ml/inference/trainedmodel/TargetType.java | 3 +- .../trainedmodel/TrainedModelLocation.java | 3 +- .../trainedmodel/ensemble/Ensemble.java | 51 +- .../trainedmodel/ensemble/Exponent.java | 7 +- .../ensemble/LogisticRegression.java | 7 +- .../trainedmodel/ensemble/WeightedMode.java | 7 +- .../trainedmodel/ensemble/WeightedSum.java | 6 +- .../langident/LangIdentNeuralNetwork.java | 11 +- .../trainedmodel/langident/LangNetLayer.java | 15 +- .../ml/inference/trainedmodel/tree/Tree.java | 12 +- .../inference/trainedmodel/tree/TreeNode.java | 59 +- .../client/ml/job/config/AnalysisConfig.java | 119 +- .../client/ml/job/config/AnalysisLimits.java | 15 +- .../config/CategorizationAnalyzerConfig.java | 101 +- .../client/ml/job/config/DataDescription.java | 9 +- .../config/DefaultDetectorDescription.java | 6 +- .../client/ml/job/config/DetectionRule.java | 16 +- .../client/ml/job/config/Detector.java | 68 +- .../client/ml/job/config/FilterRef.java | 12 +- .../client/ml/job/config/Job.java | 119 +- .../client/ml/job/config/JobState.java | 6 +- .../client/ml/job/config/JobUpdate.java | 118 +- .../client/ml/job/config/MlFilter.java | 5 +- .../client/ml/job/config/ModelPlotConfig.java | 10 +- .../PerPartitionCategorizationConfig.java | 10 +- .../client/ml/job/config/RuleCondition.java | 10 +- .../client/ml/job/config/RuleScope.java | 9 +- .../client/ml/job/process/DataCounts.java | 184 +- .../client/ml/job/process/ModelSizeStats.java | 132 +- .../client/ml/job/process/ModelSnapshot.java | 89 +- .../client/ml/job/process/Quantiles.java | 13 +- .../client/ml/job/process/TimingStats.java | 83 +- .../client/ml/job/results/AnomalyCause.java | 64 +- .../client/ml/job/results/AnomalyRecord.java | 52 +- .../client/ml/job/results/Bucket.java | 50 +- .../ml/job/results/BucketInfluencer.java | 45 +- .../ml/job/results/CategoryDefinition.java | 24 +- .../client/ml/job/results/Influence.java | 9 +- .../client/ml/job/results/Influencer.java | 45 +- .../client/ml/job/results/OverallBucket.java | 34 +- .../client/ml/job/results/Result.java | 3 +- .../client/ml/job/stats/ForecastStats.java | 62 +- .../client/ml/job/stats/JobStats.java | 88 +- .../client/ml/job/stats/SimpleStats.java | 26 +- .../client/rollup/DeleteRollupJobRequest.java | 1 - .../rollup/GetRollupIndexCapsRequest.java | 3 +- .../client/rollup/GetRollupJobResponse.java | 111 +- .../client/rollup/RollableIndexCaps.java | 25 +- .../client/rollup/RollupJobCaps.java | 31 +- .../client/rollup/StartRollupJobResponse.java | 7 +- .../client/rollup/StopRollupJobResponse.java | 7 +- .../job/config/DateHistogramGroupConfig.java | 46 +- .../client/rollup/job/config/GroupConfig.java | 40 +- .../job/config/HistogramGroupConfig.java | 5 +- .../rollup/job/config/MetricConfig.java | 5 +- .../rollup/job/config/RollupJobConfig.java | 30 +- .../rollup/job/config/TermsGroupConfig.java | 5 +- .../CachesStatsResponse.java | 40 +- .../MountSnapshotRequest.java | 3 +- .../client/security/AuthenticateRequest.java | 3 +- .../client/security/AuthenticateResponse.java | 61 +- .../security/ChangePasswordRequest.java | 4 +- .../security/ClearApiKeyCacheRequest.java | 10 +- .../ClearPrivilegesCacheResponse.java | 8 +- .../security/ClearRealmCacheResponse.java | 7 +- .../security/ClearRolesCacheResponse.java | 8 +- .../security/ClearSecurityCacheResponse.java | 8 +- .../ClearServiceAccountTokenCacheRequest.java | 6 +- .../client/security/CreateApiKeyRequest.java | 20 +- .../client/security/CreateApiKeyResponse.java | 26 +- .../CreateServiceAccountTokenRequest.java | 21 +- .../CreateServiceAccountTokenResponse.java | 11 +- .../client/security/CreateTokenRequest.java | 27 +- .../client/security/CreateTokenResponse.java | 47 +- .../DelegatePkiAuthenticationRequest.java | 12 +- .../DelegatePkiAuthenticationResponse.java | 29 +- .../security/DeletePrivilegesRequest.java | 2 +- .../security/DeleteRoleMappingRequest.java | 2 +- .../security/DeleteRoleMappingResponse.java | 7 +- .../client/security/DeleteRoleRequest.java | 2 +- .../client/security/DeleteRoleResponse.java | 9 +- .../DeleteServiceAccountTokenRequest.java | 15 +- .../DeleteServiceAccountTokenResponse.java | 7 +- .../client/security/DeleteUserRequest.java | 2 +- .../client/security/DeleteUserResponse.java | 7 +- .../security/ExpressionRoleMapping.java | 47 +- .../client/security/GetApiKeyRequest.java | 14 +- .../client/security/GetApiKeyResponse.java | 2 +- .../security/GetBuiltinPrivilegesRequest.java | 3 +- .../GetBuiltinPrivilegesResponse.java | 12 +- .../client/security/GetPrivilegesRequest.java | 5 +- .../security/GetPrivilegesResponse.java | 2 +- .../security/GetRoleMappingsRequest.java | 4 +- .../security/GetRoleMappingsResponse.java | 2 +- .../client/security/GetRolesResponse.java | 5 +- .../GetServiceAccountCredentialsRequest.java | 6 +- .../GetServiceAccountCredentialsResponse.java | 47 +- .../security/GetServiceAccountsRequest.java | 6 +- .../security/GetServiceAccountsResponse.java | 8 +- .../security/GetSslCertificatesRequest.java | 3 +- .../security/GetSslCertificatesResponse.java | 2 +- .../security/GetUserPrivilegesRequest.java | 3 +- .../security/GetUserPrivilegesResponse.java | 63 +- .../client/security/GetUsersResponse.java | 29 +- .../client/security/GrantApiKeyRequest.java | 13 +- .../client/security/HasPrivilegesRequest.java | 14 +- .../security/HasPrivilegesResponse.java | 67 +- .../security/InvalidateApiKeyRequest.java | 43 +- .../security/InvalidateApiKeyResponse.java | 35 +- .../security/InvalidateTokenRequest.java | 23 +- .../security/InvalidateTokenResponse.java | 16 +- .../security/KibanaEnrollmentRequest.java | 4 +- .../security/KibanaEnrollmentResponse.java | 32 +- .../security/NodeEnrollmentRequest.java | 2 +- .../security/NodeEnrollmentResponse.java | 28 +- .../client/security/PutPrivilegesRequest.java | 5 +- .../security/PutPrivilegesResponse.java | 8 +- .../security/PutRoleMappingRequest.java | 38 +- .../security/PutRoleMappingResponse.java | 12 +- .../client/security/PutRoleRequest.java | 3 +- .../client/security/PutRoleResponse.java | 11 +- .../client/security/PutUserRequest.java | 17 +- .../client/security/PutUserResponse.java | 9 +- .../client/security/QueryApiKeyRequest.java | 23 +- .../client/security/QueryApiKeyResponse.java | 3 +- .../security/SecurityNodesResponse.java | 17 +- ...erviceAccountCredentialsNodesResponse.java | 10 +- .../client/security/TemplateRoleName.java | 18 +- .../client/security/support/ApiKey.java | 88 +- .../security/support/CertificateInfo.java | 29 +- .../security/support/ServiceAccountInfo.java | 6 +- .../security/support/ServiceTokenInfo.java | 6 +- .../CompositeRoleMapperExpression.java | 1 - .../expressions/CompositeType.java | 4 +- .../fields/FieldRoleMapperExpression.java | 6 +- .../parser/RoleMapperExpressionParser.java | 55 +- .../client/security/user/User.java | 22 +- .../privileges/AbstractIndicesPrivileges.java | 17 +- .../user/privileges/ApplicationPrivilege.java | 26 +- .../ApplicationResourcePrivileges.java | 31 +- .../user/privileges/GlobalPrivileges.java | 36 +- .../user/privileges/IndicesPrivileges.java | 38 +- .../ManageApplicationPrivilege.java | 2 +- .../client/security/user/privileges/Role.java | 229 +- .../privileges/UserIndicesPrivileges.java | 43 +- ...xecuteSnapshotLifecyclePolicyResponse.java | 10 +- ...cuteSnapshotLifecycleRetentionRequest.java | 3 +- .../client/slm/SnapshotInvocationRecord.java | 16 +- ...pshotLifecycleManagementStatusRequest.java | 3 +- .../client/slm/SnapshotLifecyclePolicy.java | 49 +- .../slm/SnapshotLifecyclePolicyMetadata.java | 87 +- .../client/slm/SnapshotLifecycleStats.java | 78 +- .../slm/SnapshotRetentionConfiguration.java | 35 +- .../client/slm/StartSLMRequest.java | 3 +- .../client/slm/StopSLMRequest.java | 3 +- .../client/tasks/CancelTasksRequest.java | 46 +- .../client/tasks/CancelTasksResponse.java | 46 +- .../client/tasks/ElasticsearchException.java | 31 +- .../client/tasks/GetTaskRequest.java | 8 +- .../client/tasks/GetTaskResponse.java | 11 +- .../client/tasks/ListTasksResponse.java | 45 +- .../elasticsearch/client/tasks/NodeData.java | 66 +- .../elasticsearch/client/tasks/TaskGroup.java | 14 +- .../elasticsearch/client/tasks/TaskId.java | 4 +- .../elasticsearch/client/tasks/TaskInfo.java | 87 +- .../client/tasks/TaskOperationFailure.java | 41 +- .../client/tasks/TaskSubmissionResponse.java | 6 +- .../textstructure/FindStructureRequest.java | 48 +- .../structurefinder/FieldStats.java | 51 +- .../structurefinder/TextStructure.java | 133 +- .../transform/AcknowledgedTasksResponse.java | 31 +- .../transform/DeleteTransformRequest.java | 1 - .../client/transform/GetTransformRequest.java | 1 + .../transform/GetTransformResponse.java | 2 +- .../transform/GetTransformStatsResponse.java | 2 +- .../transform/PreviewTransformRequest.java | 8 +- .../transform/PreviewTransformResponse.java | 2 +- .../transform/StartTransformRequest.java | 3 +- .../transform/StartTransformResponse.java | 15 +- .../transform/StopTransformRequest.java | 8 +- .../transform/StopTransformResponse.java | 14 +- .../TransformNamedXContentProvider.java | 4 +- .../transform/transforms/DestConfig.java | 2 +- .../transform/transforms/NodeAttributes.java | 37 +- .../transform/transforms/QueryConfig.java | 4 +- .../transform/transforms/SettingsConfig.java | 2 +- .../transform/transforms/SourceConfig.java | 33 +- .../transforms/TimeRetentionPolicyConfig.java | 2 +- .../transform/transforms/TimeSyncConfig.java | 20 +- .../transforms/TransformCheckpointStats.java | 33 +- .../TransformCheckpointingInfo.java | 2 +- .../transform/transforms/TransformConfig.java | 4 +- .../transforms/TransformConfigUpdate.java | 2 +- .../transforms/TransformIndexerPosition.java | 12 +- .../transforms/TransformIndexerStats.java | 2 +- .../transforms/TransformProgress.java | 13 +- .../transform/transforms/TransformStats.java | 40 +- .../transforms/latest/LatestConfig.java | 9 +- .../transforms/pivot/AggregationConfig.java | 4 +- .../pivot/DateHistogramGroupSource.java | 6 +- .../transforms/pivot/GeoTileGroupSource.java | 4 +- .../transforms/pivot/GroupConfig.java | 2 +- .../pivot/HistogramGroupSource.java | 4 +- .../transforms/pivot/PivotConfig.java | 4 +- .../transforms/pivot/SingleGroupSource.java | 2 +- .../transforms/pivot/TermsGroupSource.java | 2 +- .../client/watcher/AckWatchRequest.java | 3 +- .../client/watcher/AckWatchResponse.java | 14 +- .../client/watcher/ActionStatus.java | 100 +- .../client/watcher/ActivateWatchRequest.java | 2 +- .../client/watcher/ActivateWatchResponse.java | 14 +- .../watcher/DeactivateWatchRequest.java | 2 - .../watcher/DeactivateWatchResponse.java | 14 +- .../client/watcher/DeleteWatchResponse.java | 12 +- .../client/watcher/ExecuteWatchRequest.java | 9 +- .../client/watcher/ExecuteWatchResponse.java | 19 +- .../client/watcher/GetWatchResponse.java | 80 +- .../client/watcher/PutWatchRequest.java | 5 +- .../client/watcher/PutWatchResponse.java | 20 +- .../client/watcher/QueuedWatch.java | 18 +- .../watcher/WatchExecutionSnapshot.java | 38 +- .../client/watcher/WatchStatus.java | 71 +- .../client/watcher/WatchStatusDateParser.java | 9 +- .../client/watcher/WatcherMetadata.java | 3 +- .../client/watcher/WatcherState.java | 1 - .../client/watcher/WatcherStatsRequest.java | 2 +- .../client/watcher/WatcherStatsResponse.java | 100 +- .../client/xpack/XPackInfoRequest.java | 4 +- .../client/xpack/XPackInfoResponse.java | 199 +- .../client/xpack/XPackUsageResponse.java | 7 +- .../GeoIpStatsResponseTests.java | 17 +- .../client/AbstractRequestTestCase.java | 7 +- .../client/AbstractResponseTestCase.java | 5 +- .../elasticsearch/client/BulkProcessorIT.java | 156 +- .../client/BulkProcessorRetryIT.java | 23 +- .../BulkRequestWithGlobalParametersIT.java | 62 +- .../java/org/elasticsearch/client/CCRIT.java | 104 +- .../client/CcrRequestConvertersTests.java | 19 +- .../elasticsearch/client/ClusterClientIT.java | 164 +- .../java/org/elasticsearch/client/CrudIT.java | 385 +- .../CustomRestHighLevelClientTests.java | 47 +- .../client/ESRestHighLevelClientTestCase.java | 154 +- .../org/elasticsearch/client/EnrichIT.java | 28 +- .../java/org/elasticsearch/client/EqlIT.java | 14 +- .../org/elasticsearch/client/FeaturesIT.java | 23 +- .../client/GetAliasesResponseTests.java | 82 +- .../org/elasticsearch/client/GraphIT.java | 12 +- .../client/GraphRequestConvertersTests.java | 2 +- .../HighLevelRestClientCompressionIT.java | 4 +- .../HighLevelRestClientFilterPathIT.java | 4 +- .../client/IndexLifecycleIT.java | 239 +- .../IndexLifecycleRequestConvertersTests.java | 2 +- .../elasticsearch/client/IndicesClientIT.java | 1201 +- .../client/IndicesRequestConvertersTests.java | 190 +- .../elasticsearch/client/IngestClientIT.java | 104 +- .../client/IngestRequestConvertersTests.java | 2 +- .../org/elasticsearch/client/LicenseIT.java | 121 +- .../client/LicenseRequestConvertersTests.java | 17 +- .../client/MLRequestConvertersTests.java | 127 +- .../client/MachineLearningGetResultsIT.java | 513 +- .../client/MachineLearningIT.java | 1507 +- .../org/elasticsearch/client/MigrationIT.java | 3 +- .../client/MlTestStateCleaner.java | 11 +- .../client/MockRestHighLevelTests.java | 9 +- .../elasticsearch/client/PingAndInfoIT.java | 2 +- .../org/elasticsearch/client/RankEvalIT.java | 13 +- .../org/elasticsearch/client/ReindexIT.java | 133 +- .../client/RequestConvertersTests.java | 278 +- .../client/RestHighLevelClientExtTests.java | 26 +- .../client/RestHighLevelClientTests.java | 850 +- .../org/elasticsearch/client/RollupIT.java | 80 +- .../org/elasticsearch/client/SearchIT.java | 490 +- .../client/SearchableSnapshotsIT.java | 31 +- ...chableSnapshotsRequestConvertersTests.java | 11 +- .../org/elasticsearch/client/SecurityIT.java | 87 +- .../SecurityRequestConvertersTests.java | 159 +- .../org/elasticsearch/client/SnapshotIT.java | 122 +- .../SnapshotRequestConvertersTests.java | 9 +- .../elasticsearch/client/StoredScriptsIT.java | 48 +- .../org/elasticsearch/client/TasksIT.java | 27 +- .../client/TasksRequestConvertersTests.java | 18 +- .../elasticsearch/client/TextStructureIT.java | 23 +- .../TextStructureRequestConvertersTests.java | 17 +- .../client/TimedRequestTests.java | 8 +- .../elasticsearch/client/UpdateByQueryIT.java | 86 +- .../org/elasticsearch/client/WatcherIT.java | 83 +- .../client/WatcherRequestConvertersTests.java | 39 +- .../client/XPackInfoResponseTests.java | 44 +- .../client/XPackRequestConvertersTests.java | 26 +- .../client/analytics/AnalyticsAggsIT.java | 17 +- .../client/analytics/InferenceAggIT.java | 7 +- .../client/asyncsearch/AsyncSearchIT.java | 19 +- .../asyncsearch/AsyncSearchResponseTests.java | 33 +- .../client/ccr/CcrStatsResponseTests.java | 165 +- .../client/ccr/FollowConfigTests.java | 17 +- .../client/ccr/FollowInfoResponseTests.java | 17 +- .../client/ccr/FollowStatsResponseTests.java | 94 +- .../GetAutoFollowPatternResponseTests.java | 7 +- .../ccr/PutAutoFollowPatternRequestTests.java | 3 +- .../client/ccr/PutFollowRequestTests.java | 3 +- .../cluster/RemoteInfoResponseTests.java | 40 +- .../client/common/ProtocolUtilsTests.java | 2 +- .../core/AcknowledgedResponseTests.java | 9 +- .../client/core/BroadcastResponseTests.java | 14 +- .../client/core/CountRequestTests.java | 25 +- .../client/core/CountResponseTests.java | 54 +- .../client/core/GetSourceResponseTests.java | 5 +- .../client/core/MainResponseTests.java | 22 +- .../core/MultiTermVectorsResponseTests.java | 11 +- .../core/ShardsAcknowledgedResponseTests.java | 10 +- .../client/core/TermVectorsResponseTests.java | 66 +- .../core/tasks/GetTaskResponseTests.java | 44 +- .../AsyncSearchDocumentationIT.java | 10 +- .../documentation/CCRDocumentationIT.java | 29 +- .../documentation/CRUDDocumentationIT.java | 214 +- .../ClusterClientDocumentationIT.java | 42 +- .../documentation/EnrichDocumentationIT.java | 46 +- .../documentation/GraphDocumentationIT.java | 9 +- .../documentation/ILMDocumentationIT.java | 189 +- .../IndicesClientDocumentationIT.java | 213 +- .../IngestClientDocumentationIT.java | 30 +- .../LicensingDocumentationIT.java | 41 +- .../MigrationDocumentationIT.java | 2 +- .../MlClientDocumentationIT.java | 541 +- .../documentation/RollupDocumentationIT.java | 41 +- .../documentation/SearchDocumentationIT.java | 266 +- .../SearchableSnapshotsDocumentationIT.java | 8 +- .../SecurityDocumentationIT.java | 404 +- .../SnapshotClientDocumentationIT.java | 14 +- .../StoredScriptsDocumentationIT.java | 26 +- .../TasksClientDocumentationIT.java | 1 - .../TextStructureClientDocumentationIT.java | 26 +- .../documentation/WatcherDocumentationIT.java | 75 +- .../enrich/ExecutePolicyResponseTests.java | 2 +- .../client/enrich/GetPolicyResponseTests.java | 37 +- .../client/enrich/PutPolicyRequestTests.java | 25 +- .../client/enrich/StatsResponseTests.java | 50 +- .../client/eql/EqlSearchRequestTests.java | 15 +- .../client/eql/EqlSearchResponseTests.java | 82 +- .../client/eql/EqlStatsResponseTests.java | 3 +- .../graph/GraphExploreResponseTests.java | 86 +- .../client/ilm/AllocateActionTests.java | 28 +- .../client/ilm/DeleteActionTests.java | 2 +- .../ilm/ExplainLifecycleRequestTests.java | 45 +- .../ilm/ExplainLifecycleResponseTests.java | 12 +- .../client/ilm/ForceMergeActionTests.java | 12 +- .../client/ilm/FreezeActionTests.java | 2 +- .../ilm/GetLifecyclePolicyRequestTests.java | 6 +- .../ilm/GetLifecyclePolicyResponseTests.java | 46 +- .../IndexLifecycleExplainResponseTests.java | 32 +- ...ifecycleManagementStatusResponseTests.java | 30 +- .../ilm/LifecyclePolicyMetadataTests.java | 48 +- .../client/ilm/LifecyclePolicyTests.java | 151 +- .../client/ilm/MigrateActionTests.java | 8 +- .../client/ilm/PhaseExecutionInfoTests.java | 20 +- .../elasticsearch/client/ilm/PhaseTests.java | 14 +- .../client/ilm/ReadOnlyActionTests.java | 2 +- ...emoveIndexLifecyclePolicyRequestTests.java | 59 +- ...moveIndexLifecyclePolicyResponseTests.java | 23 +- .../client/ilm/RolloverActionTests.java | 17 +- .../ilm/SearchableSnapshotActionTests.java | 2 +- .../client/ilm/SetPriorityActionTests.java | 4 +- .../client/ilm/ShrinkActionTests.java | 2 +- .../client/ilm/UnfollowActionTests.java | 2 +- .../ilm/WaitForSnapshotActionTests.java | 2 +- .../client/indices/AnalyzeRequestTests.java | 3 +- .../client/indices/AnalyzeResponseTests.java | 30 +- .../indices/CloseIndexRequestTests.java | 2 +- .../indices/CloseIndexResponseTests.java | 51 +- .../indices/CreateIndexRequestTests.java | 8 +- .../DataStreamsStatsResponseTests.java | 32 +- .../GetComponentTemplatesResponseTests.java | 18 +- ...ComposableIndexTemplatesResponseTests.java | 10 +- .../indices/GetDataStreamResponseTests.java | 12 +- .../GetFieldMappingsResponseTests.java | 11 +- .../client/indices/GetIndexResponseTests.java | 27 +- .../GetIndexTemplatesResponseTests.java | 58 +- .../indices/GetMappingsResponseTests.java | 13 +- .../indices/PutIndexTemplateRequestTests.java | 40 +- .../indices/PutMappingRequestTests.java | 8 +- .../indices/RandomCreateIndexGenerator.java | 3 +- .../indices/ReloadAnalyzersResponseTests.java | 19 +- .../client/indices/ResizeRequestTests.java | 18 +- .../client/indices/ResizeResponseTests.java | 11 +- .../rollover/RolloverRequestTests.java | 12 +- .../rollover/RolloverResponseTests.java | 8 +- .../license/GetBasicStatusResponseTests.java | 16 +- .../license/GetTrialStatusResponseTests.java | 16 +- .../license/PutLicenseResponseTests.java | 20 +- .../license/StartBasicResponseTests.java | 7 +- .../DeprecationInfoResponseTests.java | 79 +- .../GetFeatureUpgradeStatusResponseTests.java | 23 +- .../PostFeatureUpgradeResponseTests.java | 29 +- .../client/ml/CloseJobRequestTests.java | 3 +- .../client/ml/CloseJobResponseTests.java | 2 +- .../ml/DeleteCalendarEventRequestTests.java | 6 +- .../ml/DeleteCalendarJobRequestTests.java | 12 +- .../client/ml/DeleteCalendarRequestTests.java | 1 - .../DeleteDataFrameAnalyticsRequestTests.java | 6 +- .../ml/DeleteExpiredDataRequestTests.java | 23 +- .../ml/DeleteExpiredDataResponseTests.java | 3 +- .../client/ml/DeleteForecastRequestTests.java | 4 +- .../client/ml/DeleteJobResponseTests.java | 2 +- .../ml/DeleteModelSnapshotRequestTests.java | 12 +- .../ml/DeleteTrainedModelRequestTests.java | 3 +- .../ml/EvaluateDataFrameRequestTests.java | 11 +- .../ml/EvaluateDataFrameResponseTests.java | 23 +- ...xplainDataFrameAnalyticsResponseTests.java | 2 +- .../client/ml/FlushJobRequestTests.java | 2 +- .../client/ml/FlushJobResponseTests.java | 5 +- .../client/ml/ForecastJobRequestTests.java | 10 +- .../client/ml/ForecastJobResponseTests.java | 4 +- .../client/ml/GetBucketsRequestTests.java | 2 +- .../client/ml/GetBucketsResponseTests.java | 2 +- .../ml/GetCalendarEventsRequestTests.java | 2 +- .../ml/GetCalendarEventsResponseTests.java | 4 +- .../client/ml/GetCalendarsRequestTests.java | 2 +- .../client/ml/GetCalendarsResponseTests.java | 4 +- .../client/ml/GetCategoriesRequestTests.java | 2 +- .../client/ml/GetCategoriesResponseTests.java | 2 +- .../ml/GetDataFrameAnalyticsRequestTests.java | 6 +- ...etDataFrameAnalyticsStatsRequestTests.java | 6 +- .../client/ml/GetDatafeedRequestTests.java | 4 +- .../client/ml/GetDatafeedResponseTests.java | 4 +- .../ml/GetDatafeedStatsRequestTests.java | 2 +- .../ml/GetDatafeedStatsResponseTests.java | 4 +- .../client/ml/GetFiltersRequestTests.java | 2 +- .../client/ml/GetFiltersResponseTests.java | 4 +- .../client/ml/GetInfluencersRequestTests.java | 2 +- .../ml/GetInfluencersResponseTests.java | 2 +- .../client/ml/GetJobRequestTests.java | 4 +- .../client/ml/GetJobResponseTests.java | 4 +- .../client/ml/GetJobStatsRequestTests.java | 2 +- .../client/ml/GetJobStatsResponseTests.java | 4 +- .../ml/GetModelSnapshotsRequestTests.java | 6 +- .../ml/GetModelSnapshotsResponseTests.java | 2 +- .../ml/GetOverallBucketsRequestTests.java | 2 +- .../ml/GetOverallBucketsResponseTests.java | 2 +- .../client/ml/GetRecordsRequestTests.java | 2 +- .../client/ml/GetRecordsResponseTests.java | 2 +- .../ml/GetTrainedModelsRequestTests.java | 6 +- .../ml/GetTrainedModelsStatsRequestTests.java | 6 +- .../client/ml/NodeAttributesTests.java | 12 +- .../client/ml/OpenJobRequestTests.java | 2 +- .../client/ml/OpenJobResponseTests.java | 2 +- .../ml/PostCalendarEventRequestTests.java | 2 +- .../ml/PostCalendarEventResponseTests.java | 2 +- .../client/ml/PostDataRequestTests.java | 17 +- .../client/ml/PostDataResponseTests.java | 2 +- .../ml/PreviewDatafeedRequestTests.java | 8 +- .../ml/PreviewDatafeedResponseTests.java | 65 +- .../ml/PutCalendarActionResponseTests.java | 3 +- .../client/ml/PutCalendarJobRequestTests.java | 12 +- .../client/ml/PutCalendarRequestTests.java | 2 +- .../client/ml/PutCalendarResponseTests.java | 2 +- .../ml/PutDataFrameAnalyticsRequestTests.java | 4 +- .../client/ml/PutDatafeedRequestTests.java | 3 +- .../client/ml/PutDatafeedResponseTests.java | 2 +- .../client/ml/PutFilterRequestTests.java | 3 +- .../client/ml/PutFilterResponseTests.java | 2 +- .../client/ml/PutJobRequestTests.java | 3 +- .../client/ml/PutJobResponseTests.java | 2 +- .../ml/PutTrainedModelActionRequestTests.java | 2 +- .../PutTrainedModelActionResponseTests.java | 2 +- .../ml/RevertModelSnapshotRequestTests.java | 3 +- .../ml/RevertModelSnapshotResponseTests.java | 3 +- .../StartDataFrameAnalyticsRequestTests.java | 12 +- .../StartDataFrameAnalyticsResponseTests.java | 2 +- .../client/ml/StartDatafeedRequestTests.java | 2 +- .../client/ml/StartDatafeedResponseTests.java | 2 +- .../StopDataFrameAnalyticsRequestTests.java | 12 +- .../StopDataFrameAnalyticsResponseTests.java | 2 +- .../client/ml/StopDatafeedRequestTests.java | 3 +- .../client/ml/StopDatafeedResponseTests.java | 2 +- .../UpdateDataFrameAnalyticsRequestTests.java | 4 +- .../client/ml/UpdateDatafeedRequestTests.java | 3 +- .../client/ml/UpdateFilterRequestTests.java | 3 +- .../client/ml/UpdateJobRequestTests.java | 3 +- .../ml/UpdateModelSnapshotRequestTests.java | 3 +- .../ml/UpdateModelSnapshotResponseTests.java | 5 +- .../UpgradeJobModelSnapshotRequestTests.java | 8 +- .../UpgradeJobModelSnapshotResponseTests.java | 9 +- .../client/ml/calendars/CalendarTests.java | 5 +- .../ml/calendars/ScheduledEventTests.java | 9 +- .../ml/datafeed/ChunkingConfigTests.java | 2 +- .../ml/datafeed/DatafeedConfigTests.java | 46 +- .../ml/datafeed/DatafeedStatsTests.java | 20 +- .../ml/datafeed/DatafeedTimingStatsTests.java | 12 +- .../ml/datafeed/DatafeedUpdateTests.java | 15 +- .../datafeed/DelayedDataCheckConfigTests.java | 3 +- .../ml/dataframe/ClassificationTests.java | 19 +- .../DataFrameAnalyticsConfigTests.java | 25 +- .../DataFrameAnalyticsConfigUpdateTests.java | 8 +- .../DataFrameAnalyticsDestTests.java | 2 +- .../DataFrameAnalyticsSourceTests.java | 11 +- .../DataFrameAnalyticsStatsTests.java | 17 +- .../ml/dataframe/OutlierDetectionTests.java | 19 +- .../ml/dataframe/PhaseProgressTests.java | 2 +- .../client/ml/dataframe/QueryConfigTests.java | 4 +- .../client/ml/dataframe/RegressionTests.java | 19 +- .../AccuracyMetricResultTests.java | 2 +- .../classification/AccuracyMetricTests.java | 2 +- .../classification/AucRocMetricTests.java | 6 +- .../classification/ClassificationTests.java | 22 +- ...classConfusionMatrixMetricResultTests.java | 6 +- .../MulticlassConfusionMatrixMetricTests.java | 2 +- .../PerClassSingleValueTests.java | 2 +- .../PrecisionMetricResultTests.java | 2 +- .../classification/PrecisionMetricTests.java | 2 +- .../RecallMetricResultTests.java | 2 +- .../classification/RecallMetricTests.java | 2 +- .../evaluation/common/AucRocPointTests.java | 2 +- .../evaluation/common/AucRocResultTests.java | 8 +- .../outlierdetection/AucRocMetricTests.java | 2 +- ...usionMatrixMetricConfusionMatrixTests.java | 2 +- .../ConfusionMatrixMetricResultTests.java | 8 +- .../OutlierDetectionTests.java | 23 +- .../PrecisionMetricResultTests.java | 8 +- .../RecallMetricResultTests.java | 8 +- .../regression/HuberMetricResultTests.java | 2 +- .../regression/HuberMetricTests.java | 2 +- .../MeanSquaredErrorMetricResultTests.java | 2 +- .../MeanSquaredErrorMetricTests.java | 2 +- ...aredLogarithmicErrorMetricResultTests.java | 2 +- ...eanSquaredLogarithmicErrorMetricTests.java | 2 +- .../regression/RSquaredMetricResultTests.java | 2 +- .../regression/RSquaredMetricTests.java | 2 +- .../regression/RegressionTests.java | 8 +- .../explain/FieldSelectionTests.java | 13 +- .../explain/MemoryEstimationTests.java | 5 +- .../ClassificationStatsTests.java | 2 +- .../classification/HyperparametersTests.java | 2 +- .../classification/TimingStatsTests.java | 2 +- .../classification/ValidationLossTests.java | 2 +- .../stats/common/DataCountsTests.java | 8 +- .../stats/common/FoldValuesTests.java | 2 +- .../stats/common/MemoryUsageTests.java | 8 +- .../OutlierDetectionStatsTests.java | 8 +- .../outlierdetection/ParametersTests.java | 2 +- .../outlierdetection/TimingStatsTests.java | 3 +- .../regression/HyperparametersTests.java | 3 +- .../regression/RegressionStatsTests.java | 3 +- .../stats/regression/TimingStatsTests.java | 2 +- .../stats/regression/ValidationLossTests.java | 2 +- .../ml/inference/IndexLocationTests.java | 2 +- .../InferenceToXContentCompressorTests.java | 24 +- .../NamedXContentObjectHelperTests.java | 21 +- .../ml/inference/TrainedModelConfigTests.java | 24 +- .../TrainedModelDefinitionTests.java | 26 +- .../ml/inference/TrainedModelInputTests.java | 9 +- .../ml/inference/TrainedModelStatsTests.java | 17 +- .../CustomWordEmbeddingTests.java | 3 +- .../preprocessing/FrequencyEncodingTests.java | 8 +- .../inference/preprocessing/MultiTests.java | 15 +- .../inference/preprocessing/NGramTests.java | 9 +- .../preprocessing/OneHotEncodingTests.java | 7 +- .../TargetMeanEncodingTests.java | 9 +- .../results/FeatureImportanceTests.java | 10 +- .../inference/results/TopClassEntryTests.java | 2 +- .../ClassificationConfigTests.java | 7 +- .../trainedmodel/InferenceStatsTests.java | 7 +- .../trainedmodel/RegressionConfigTests.java | 7 +- .../trainedmodel/ensemble/EnsembleTests.java | 36 +- .../trainedmodel/ensemble/ExponentTests.java | 3 +- .../ensemble/LogisticRegressionTests.java | 3 +- .../ensemble/WeightedModeTests.java | 6 +- .../ensemble/WeightedSumTests.java | 3 +- .../LangIdentNeuralNetworkTests.java | 9 +- .../langident/LangNetLayerTests.java | 6 +- .../trainedmodel/tree/TreeNodeTests.java | 16 +- .../trainedmodel/tree/TreeTests.java | 18 +- .../ml/job/config/AnalysisConfigTests.java | 9 +- .../ml/job/config/AnalysisLimitsTests.java | 12 +- .../CategorizationAnalyzerConfigTests.java | 2 +- .../ml/job/config/DataDescriptionTests.java | 2 +- .../ml/job/config/DetectionRuleTests.java | 2 +- .../client/ml/job/config/DetectorTests.java | 8 +- .../client/ml/job/config/FilterRefTests.java | 2 +- .../client/ml/job/config/JobTests.java | 33 +- .../client/ml/job/config/JobUpdateTests.java | 9 +- .../client/ml/job/config/MlFilterTests.java | 8 +- .../ml/job/config/ModelPlotConfigTests.java | 2 +- ...PerPartitionCategorizationConfigTests.java | 2 +- .../ml/job/config/RuleConditionTests.java | 2 +- .../client/ml/job/config/RuleScopeTests.java | 8 +- .../ml/job/process/DataCountsTests.java | 27 +- .../ml/job/process/ModelSizeStatsTests.java | 2 +- .../ml/job/process/ModelSnapshotTests.java | 34 +- .../client/ml/job/process/QuantilesTests.java | 16 +- .../ml/job/process/TimingStatsTests.java | 12 +- .../ml/job/results/AnomalyCauseTests.java | 2 +- .../ml/job/results/AnomalyRecordTests.java | 10 +- .../ml/job/results/BucketInfluencerTests.java | 9 +- .../client/ml/job/results/BucketTests.java | 5 +- .../job/results/CategoryDefinitionTests.java | 8 +- .../client/ml/job/results/InfluenceTests.java | 2 +- .../ml/job/results/InfluencerTests.java | 11 +- .../ml/job/results/OverallBucketTests.java | 12 +- .../ml/job/stats/ForecastStatsTests.java | 7 +- .../client/ml/job/stats/JobStatsTests.java | 18 +- .../client/ml/job/stats/SimpleStatsTests.java | 3 +- .../client/ml/util/PageParamsTests.java | 2 +- .../rollup/DeleteRollupJobRequestTests.java | 2 +- .../GetRollupIndexCapsRequestTests.java | 4 +- .../GetRollupIndexCapsResponseTests.java | 3 +- .../rollup/GetRollupJobResponseTests.java | 38 +- .../rollup/PutRollupJobRequestTests.java | 5 +- .../rollup/RollupCapsResponseTestCase.java | 35 +- .../rollup/StartRollupJobRequestTests.java | 10 +- .../rollup/StartRollupJobResponseTests.java | 9 +- .../rollup/StopRollupJobRequestTests.java | 10 +- .../rollup/StopRollupJobResponseTests.java | 9 +- .../config/DateHistogramGroupConfigTests.java | 6 +- .../rollup/job/config/GroupConfigTests.java | 13 +- .../job/config/HistogramGroupConfigTests.java | 3 +- .../rollup/job/config/MetricConfigTests.java | 2 +- .../job/config/RollupJobConfigTests.java | 213 +- .../job/config/TermsGroupConfigTests.java | 2 +- .../security/AuthenticateResponseTests.java | 237 +- .../ClearRealmCacheResponseTests.java | 14 +- .../ClearRolesCacheResponseTests.java | 14 +- ...rServiceAccountTokenCacheRequestTests.java | 26 +- .../security/CreateApiKeyRequestTests.java | 191 +- .../security/CreateApiKeyResponseTests.java | 57 +- ...CreateServiceAccountTokenRequestTests.java | 32 +- ...reateServiceAccountTokenResponseTests.java | 14 +- .../security/CreateTokenRequestTests.java | 97 +- .../security/CreateTokenResponseTests.java | 18 +- ...DelegatePkiAuthenticationRequestTests.java | 24 +- ...elegatePkiAuthenticationResponseTests.java | 67 +- .../DeleteRoleMappingRequestTests.java | 20 +- .../DeleteRoleMappingResponseTests.java | 25 +- .../security/DeleteRoleResponseTests.java | 8 +- ...DeleteServiceAccountTokenRequestTests.java | 40 +- ...eleteServiceAccountTokenResponseTests.java | 10 +- .../security/DeleteUserRequestTests.java | 32 +- .../security/DeleteUserResponseTests.java | 5 +- .../security/ExpressionRoleMappingTests.java | 161 +- .../security/GetApiKeyRequestTests.java | 37 +- .../security/GetApiKeyResponseTests.java | 105 +- .../security/GetPrivilegesRequestTests.java | 30 +- .../security/GetPrivilegesResponseTests.java | 188 +- .../security/GetRoleMappingsRequestTests.java | 23 +- .../GetRoleMappingsResponseTests.java | 160 +- .../client/security/GetRolesRequestTests.java | 15 +- .../security/GetRolesResponseTests.java | 85 +- ...ServiceAccountCredentialsRequestTests.java | 18 +- ...erviceAccountCredentialsResponseTests.java | 44 +- .../GetServiceAccountsRequestTests.java | 18 +- .../GetServiceAccountsResponseTests.java | 62 +- .../GetSslCertificatesResponseTests.java | 80 +- .../GetUserPrivilegesResponseTests.java | 54 +- .../client/security/GetUsersRequestTests.java | 15 +- .../security/GetUsersResponseTests.java | 69 +- .../security/GrantApiKeyRequestTests.java | 78 +- .../security/HasPrivilegesRequestTests.java | 103 +- .../security/HasPrivilegesResponseTests.java | 167 +- .../InvalidateApiKeyRequestTests.java | 44 +- .../InvalidateApiKeyResponseTests.java | 104 +- .../security/InvalidateTokenRequestTests.java | 10 +- .../InvalidateTokenResponseTests.java | 20 +- .../KibanaEnrollmentResponseTests.java | 26 +- .../security/PutPrivilegesRequestTests.java | 172 +- .../security/PutPrivilegesResponseTests.java | 61 +- .../security/PutRoleMappingRequestTests.java | 184 +- .../client/security/PutRoleRequestTests.java | 34 +- .../client/security/PutUserRequestTests.java | 29 +- .../security/QueryApiKeyRequestTests.java | 66 +- .../security/QueryApiKeyResponseTests.java | 39 +- .../hlrc/HasPrivilegesResponseTests.java | 93 +- .../RoleMapperExpressionDslTests.java | 94 +- .../RoleMapperExpressionParserTests.java | 47 +- .../privileges/ApplicationPrivilegeTests.java | 57 +- .../ApplicationResourcePrivilegesTests.java | 46 +- .../GlobalOperationPrivilegeTests.java | 45 +- .../privileges/GlobalPrivilegesTests.java | 62 +- .../privileges/IndicesPrivilegesTests.java | 12 +- .../snapshots/GetFeaturesResponseTests.java | 3 +- .../snapshots/ResetFeaturesResponseTests.java | 7 +- .../tasks/CancelTasksResponseTests.java | 82 +- .../tasks/ElasticsearchExceptionTests.java | 28 +- .../tasks/TaskSubmissionResponseTests.java | 8 +- .../FindStructureRequestTests.java | 14 +- .../FindStructureResponseTests.java | 2 +- .../structurefinder/FieldStatsTests.java | 3 +- .../structurefinder/TextStructureTests.java | 2 +- .../AcknowledgedTasksResponseTests.java | 48 +- .../DeleteTransformRequestTests.java | 3 +- .../transform/GetTransformRequestTests.java | 3 +- .../transform/GetTransformResponseTests.java | 19 +- .../GetTransformStatsRequestTests.java | 3 +- .../GetTransformStatsResponseTests.java | 2 +- .../PreviewTransformRequestTests.java | 4 +- .../PreviewTransformResponseTests.java | 2 +- .../transform/PutTransformRequestTests.java | 4 +- .../UpdateTransformRequestTests.java | 5 +- .../transform/transforms/DestConfigTests.java | 5 +- .../transforms/NodeAttributesTests.java | 12 +- .../transforms/QueryConfigTests.java | 4 +- .../transforms/SettingsConfigTests.java | 2 +- .../transforms/SourceConfigTests.java | 8 +- .../TimeRetentionPolicyConfigTests.java | 2 +- .../transforms/TimeSyncConfigTests.java | 2 +- .../TransformCheckpointStatsTests.java | 18 +- .../TransformCheckpointingInfoTests.java | 2 +- .../transforms/TransformConfigTests.java | 4 +- .../TransformConfigUpdateTests.java | 4 +- .../TransformIndexerPositionTests.java | 18 +- .../TransformIndexerStatsTests.java | 2 +- .../transforms/TransformProgressTests.java | 13 +- .../transforms/TransformStatsTests.java | 22 +- .../transforms/hlrc/TimeSyncConfigTests.java | 23 +- .../hlrc/TransformCheckpointStatsTests.java | 50 +- .../hlrc/TransformIndexerPositionTests.java | 13 +- .../hlrc/TransformProgressTests.java | 22 +- .../transforms/latest/LatestConfigTests.java | 2 +- .../latest/hlrc/LatestConfigTests.java | 5 +- .../pivot/AggregationConfigTests.java | 4 +- .../pivot/DateHistogramGroupSourceTests.java | 2 +- .../pivot/GeoTileGroupSourceTests.java | 2 +- .../transforms/pivot/GroupConfigTests.java | 2 +- .../pivot/HistogramGroupSourceTests.java | 2 +- .../transforms/pivot/PivotConfigTests.java | 10 +- .../pivot/TermsGroupSourceTests.java | 2 +- .../hlrc/DateHistogramGroupSourceTests.java | 8 +- .../pivot/hlrc/GeoTileGroupSourceTests.java | 4 +- .../client/watcher/AckWatchResponseTests.java | 32 +- .../watcher/ActivateWatchResponseTests.java | 44 +- .../watcher/DeactivateWatchRequestTests.java | 6 +- .../watcher/DeactivateWatchResponseTests.java | 9 +- .../watcher/DeleteWatchResponseTests.java | 11 +- .../client/watcher/GetWatchResponseTests.java | 35 +- .../client/watcher/PutWatchResponseTests.java | 11 +- .../VerifyRepositoryResponseTests.java | 11 +- .../watcher/WatchRequestValidationTests.java | 51 +- .../client/watcher/WatchStatusTests.java | 100 +- .../watcher/WatcherStatsResponseTests.java | 62 +- .../hlrc/DeleteWatchResponseTests.java | 9 +- .../hlrc/ExecuteWatchResponseTests.java | 12 +- .../watcher/hlrc/PutWatchResponseTests.java | 9 +- .../elasticsearch/test/RequestMatcher.java | 6 +- .../org/elasticsearch/client/Cancellable.java | 3 +- .../elasticsearch/client/DeadHostState.java | 24 +- .../client/HasAttributeNodeSelector.java | 3 +- .../HeapBufferedAsyncResponseConsumer.java | 5 +- .../HttpAsyncResponseConsumerFactory.java | 2 +- .../client/LanguageRuntimeVersions.java | 4 +- .../java/org/elasticsearch/client/Node.java | 5 +- .../elasticsearch/client/NodeSelector.java | 4 +- ...tentCredentialsAuthenticationStrategy.java | 9 +- .../PreferHasAttributeNodeSelector.java | 3 +- .../org/elasticsearch/client/Request.java | 10 +- .../elasticsearch/client/RequestLogger.java | 32 +- .../elasticsearch/client/RequestOptions.java | 30 +- .../org/elasticsearch/client/Response.java | 27 +- .../client/ResponseException.java | 3 +- .../org/elasticsearch/client/RestClient.java | 100 +- .../client/RestClientBuilder.java | 59 +- .../client/DeadHostStateTests.java | 19 +- .../FailureTrackingResponseListenerTests.java | 2 +- .../client/HasAttributeNodeSelectorTests.java | 12 +- ...eapBufferedAsyncResponseConsumerTests.java | 27 +- .../client/HostsTrackingFailureListener.java | 2 +- .../client/NodeSelectorTests.java | 27 +- .../org/elasticsearch/client/NodeTests.java | 130 +- .../PreferHasAttributeNodeSelectorTests.java | 10 +- .../client/RequestLoggerTests.java | 54 +- .../client/RequestOptionsTests.java | 38 +- .../elasticsearch/client/RequestTests.java | 59 +- .../client/ResponseExceptionTests.java | 11 +- .../client/RestClientBuilderIntegTests.java | 29 +- .../client/RestClientBuilderTests.java | 30 +- .../RestClientGzipCompressionTests.java | 1 + .../RestClientMultipleHostsIntegTests.java | 19 +- .../client/RestClientMultipleHostsTests.java | 81 +- .../RestClientSingleHostIntegTests.java | 58 +- .../client/RestClientSingleHostTests.java | 121 +- .../elasticsearch/client/RestClientTests.java | 51 +- .../RestClientDocumentation.java | 3 +- .../sniff/ElasticsearchNodesSniffer.java | 42 +- .../elasticsearch/client/sniff/Sniffer.java | 13 +- .../ElasticsearchNodesSnifferParseTests.java | 67 +- .../sniff/ElasticsearchNodesSnifferTests.java | 40 +- .../sniff/SniffOnFailureListenerTests.java | 6 +- .../client/sniff/SnifferBuilderTests.java | 11 +- .../client/sniff/SnifferTests.java | 79 +- .../client/RestClientTestCase.java | 17 +- .../client/RestClientTestUtil.java | 5 +- .../test/rest/CreatedLocationHeaderIT.java | 4 +- .../test/rest/JsonLogsFormatAndParseIT.java | 6 +- .../test/rest/NodeRestUsageIT.java | 61 +- .../test/rest/RequestsWithoutContentIT.java | 56 +- .../docker/test/DockerYmlTestSuiteIT.java | 14 +- .../smoketest/DocsClientYamlTestSuiteIT.java | 123 +- .../java/org/elasticsearch/cli/Command.java | 8 +- .../elasticsearch/cli/SuppressForbidden.java | 1 - .../java/org/elasticsearch/cli/Terminal.java | 2 +- .../core/AbstractRefCounted.java | 3 +- .../java/org/elasticsearch/core/Booleans.java | 19 +- .../core/CompletableContext.java | 2 +- .../java/org/elasticsearch/core/FastMath.java | 76 +- .../java/org/elasticsearch/core/Glob.java | 6 +- .../java/org/elasticsearch/core/Nullable.java | 7 +- .../org/elasticsearch/core/PathUtils.java | 2 +- .../elasticsearch/core/RestApiVersion.java | 2 +- .../elasticsearch/core/SuppressForbidden.java | 2 +- .../org/elasticsearch/core/TimeValue.java | 17 +- .../core/internal/io/IOUtils.java | 24 +- .../core/internal/net/NetUtils.java | 3 +- .../java/org/elasticsearch/jdk/JarHell.java | 81 +- .../org/elasticsearch/jdk/JavaVersion.java | 12 +- .../common/unit/TimeValueTests.java | 108 +- .../common/util/ESSloppyMathTests.java | 4 +- .../util/concurrent/RefCountedTests.java | 9 +- .../core/internal/io/IOUtilsTests.java | 19 +- .../org/elasticsearch/jdk/JarHellTests.java | 36 +- .../org/elasticsearch/dissect/DissectKey.java | 37 +- .../elasticsearch/dissect/DissectMatch.java | 6 +- .../elasticsearch/dissect/DissectParser.java | 81 +- .../dissect/DissectKeyTests.java | 13 +- .../dissect/DissectMatchTests.java | 20 +- .../dissect/DissectParserTests.java | 209 +- .../org/elasticsearch/geometry/Circle.java | 2 +- .../java/org/elasticsearch/geometry/Line.java | 3 +- .../elasticsearch/geometry/LinearRing.java | 32 +- .../org/elasticsearch/geometry/MultiLine.java | 3 +- .../elasticsearch/geometry/MultiPoint.java | 3 +- .../elasticsearch/geometry/MultiPolygon.java | 3 +- .../org/elasticsearch/geometry/Polygon.java | 3 +- .../org/elasticsearch/geometry/Rectangle.java | 3 +- .../elasticsearch/geometry/package-info.java | 1 - .../elasticsearch/geometry/utils/BitUtil.java | 18 +- .../geometry/utils/GeographyValidator.java | 11 +- .../elasticsearch/geometry/utils/Geohash.java | 96 +- .../geometry/utils/StandardValidator.java | 8 +- .../geometry/utils/WellKnownText.java | 19 +- .../geometry/BaseGeometryTestCase.java | 3 +- .../elasticsearch/geometry/CircleTests.java | 12 +- .../geometry/GeometryCollectionTests.java | 25 +- .../geometry/GeometryValidatorTests.java | 21 +- .../org/elasticsearch/geometry/LineTests.java | 51 +- .../geometry/LinearRingTests.java | 65 +- .../geometry/MultiLineTests.java | 27 +- .../geometry/MultiPointTests.java | 36 +- .../geometry/MultiPolygonTests.java | 48 +- .../elasticsearch/geometry/PointTests.java | 6 +- .../elasticsearch/geometry/PolygonTests.java | 123 +- .../geometry/RectangleTests.java | 18 +- .../geometry/utils/GeoHashTests.java | 24 +- .../java/org/elasticsearch/grok/Grok.java | 129 +- .../elasticsearch/grok/MatcherWatchdog.java | 24 +- .../org/elasticsearch/grok/GrokTests.java | 190 +- .../grok/MatcherWatchdogTests.java | 26 +- .../elasticsearch/lz4/ESLZ4Compressor.java | 19 +- .../elasticsearch/lz4/ESLZ4Decompressor.java | 9 +- .../org/elasticsearch/lz4/LZ4Constants.java | 4 +- .../java/org/elasticsearch/lz4/SafeUtils.java | 4 +- .../lz4/AbstractLZ4TestCase.java | 98 +- .../lz4/ESLZ4CompressorTests.java | 2 - .../org/elasticsearch/lz4/ESLZ4Tests.java | 1677 +- .../nio/BytesChannelContext.java | 10 +- .../elasticsearch/nio/BytesWriteHandler.java | 4 +- .../org/elasticsearch/nio/ChannelFactory.java | 53 +- .../java/org/elasticsearch/nio/Config.java | 15 +- .../org/elasticsearch/nio/FlushOperation.java | 14 +- .../nio/InboundChannelBuffer.java | 28 +- .../org/elasticsearch/nio/NioSelector.java | 18 +- .../elasticsearch/nio/NioSelectorGroup.java | 17 +- .../nio/NioServerSocketChannel.java | 4 +- .../elasticsearch/nio/NioSocketChannel.java | 5 +- .../main/java/org/elasticsearch/nio/Page.java | 2 +- .../elasticsearch/nio/SelectionKeyUtils.java | 1 - .../nio/ServerChannelContext.java | 11 +- .../nio/SocketChannelContext.java | 11 +- .../nio/BytesChannelContextTests.java | 15 +- .../nio/ChannelFactoryTests.java | 7 +- .../elasticsearch/nio/EventHandlerTests.java | 25 +- .../nio/FlushOperationTests.java | 6 +- .../nio/NioSelectorGroupTests.java | 31 +- .../elasticsearch/nio/NioSelectorTests.java | 18 +- .../nio/SocketChannelContextTests.java | 114 +- .../loader/ExtendedPluginsClassLoader.java | 5 +- .../org/elasticsearch/secure_sm/SecureSM.java | 12 +- .../secure_sm/SecureSMTests.java | 4 +- .../secure_sm/ThreadPermissionTests.java | 3 +- .../common/ssl/CompositeTrustConfig.java | 18 +- .../common/ssl/DefaultJdkTrustConfig.java | 5 +- .../elasticsearch/common/ssl/DerParser.java | 29 +- .../common/ssl/DiagnosticTrustManager.java | 30 +- .../common/ssl/KeyStoreUtil.java | 41 +- .../common/ssl/PemKeyConfig.java | 10 +- .../common/ssl/PemTrustConfig.java | 8 +- .../elasticsearch/common/ssl/PemUtils.java | 56 +- .../ssl/SslClientAuthenticationMode.java | 8 +- .../common/ssl/SslConfiguration.java | 59 +- .../common/ssl/SslConfigurationKeys.java | 21 +- .../common/ssl/SslConfigurationLoader.java | 42 +- .../common/ssl/SslDiagnostics.java | 89 +- .../elasticsearch/common/ssl/SslFileUtil.java | 7 +- .../common/ssl/SslKeyConfig.java | 4 +- .../common/ssl/SslTrustConfig.java | 4 +- .../common/ssl/SslVerificationMode.java | 9 +- .../common/ssl/StoreKeyConfig.java | 38 +- .../common/ssl/StoreTrustConfig.java | 34 +- .../common/ssl/TrustEverythingConfig.java | 23 +- .../ssl/DefaultJdkTrustConfigTests.java | 3 +- .../common/ssl/PemKeyConfigTests.java | 60 +- .../common/ssl/PemTrustConfigTests.java | 5 +- .../ssl/SslConfigurationLoaderTests.java | 32 +- .../common/ssl/SslConfigurationTests.java | 110 +- .../common/ssl/SslDiagnosticsTests.java | 946 +- .../common/ssl/StoreKeyConfigTests.java | 8 +- .../common/ssl/StoreTrustConfigTests.java | 5 +- .../xcontent/AbstractObjectParser.java | 115 +- .../xcontent/ConstructingObjectParser.java | 98 +- .../xcontent/DeprecationHandler.java | 79 +- .../xcontent/FilterXContentParser.java | 4 +- .../xcontent/InstantiatingObjectParser.java | 63 +- .../xcontent/MediaTypeRegistry.java | 4 +- .../xcontent/NamedXContentRegistry.java | 68 +- .../elasticsearch/xcontent/ObjectParser.java | 163 +- .../elasticsearch/xcontent/ObjectPath.java | 3 +- .../elasticsearch/xcontent/ParseField.java | 34 +- .../xcontent/ParsedMediaType.java | 31 +- .../org/elasticsearch/xcontent/XContent.java | 42 +- .../xcontent/XContentBuilder.java | 72 +- .../xcontent/XContentFactory.java | 13 +- .../xcontent/XContentParser.java | 11 +- .../xcontent/XContentSubParser.java | 4 +- .../elasticsearch/xcontent/XContentType.java | 48 +- .../elasticsearch/xcontent/XContentUtils.java | 3 +- .../xcontent/cbor/CborXContent.java | 51 +- .../xcontent/cbor/CborXContentGenerator.java | 1 + .../xcontent/cbor/CborXContentParser.java | 13 +- .../xcontent/json/JsonXContent.java | 51 +- .../xcontent/json/JsonXContentGenerator.java | 27 +- .../xcontent/json/JsonXContentParser.java | 16 +- .../xcontent/smile/SmileXContent.java | 50 +- .../smile/SmileXContentGenerator.java | 1 + .../xcontent/smile/SmileXContentParser.java | 13 +- .../support/AbstractXContentParser.java | 61 +- .../xcontent/support/MapXContentParser.java | 13 +- .../support/filtering/FilterPath.java | 3 +- .../filtering/FilterPathBasedFilter.java | 1 - .../xcontent/yaml/YamlXContent.java | 50 +- .../xcontent/yaml/YamlXContentGenerator.java | 1 + .../xcontent/yaml/YamlXContentParser.java | 14 +- .../ConstructingObjectParserTests.java | 374 +- .../InstantiatingObjectParserTests.java | 35 +- .../xcontent/MapXContentParserTests.java | 23 +- .../xcontent/ObjectParserTests.java | 351 +- .../xcontent/ObjectPathTests.java | 1 - .../xcontent/ParseFieldTests.java | 67 +- .../xcontent/ParsedMediaTypeTests.java | 231 +- .../elasticsearch/xcontent/SimpleStruct.java | 12 +- .../xcontent/XContentParserTests.java | 161 +- .../AbstractXContentFilteringTestCase.java | 16 +- .../FilterPathGeneratorFilteringTests.java | 293 +- .../support/filtering/FilterPathTests.java | 1 - .../common/QueryStringWithAnalyzersIT.java | 30 +- .../ASCIIFoldingTokenFilterFactory.java | 8 +- .../common/AnalysisPainlessExtension.java | 6 +- .../common/AnalysisPredicateScript.java | 2 +- .../BrazilianStemTokenFilterFactory.java | 2 +- .../common/CJKBigramFilterFactory.java | 2 +- .../common/CharGroupTokenizerFactory.java | 8 +- .../analysis/common/CharMatcher.java | 75 +- .../analysis/common/CommonAnalysisPlugin.java | 223 +- .../common/CommonGramsTokenFilterFactory.java | 4 +- ...tionaryCompoundWordTokenFilterFactory.java | 4 +- .../analysis/common/ESSolrSynonymParser.java | 2 +- .../common/ESWordnetSynonymParser.java | 2 +- .../common/EdgeNGramTokenFilterFactory.java | 12 +- .../common/EstonianAnalyzerProvider.java | 4 +- .../common/FingerprintAnalyzerProvider.java | 12 +- .../common/GreekAnalyzerProvider.java | 3 +- ...enationCompoundWordTokenFilterFactory.java | 11 +- .../common/KeepTypesFilterFactory.java | 14 +- .../common/KeepWordFilterFactory.java | 5 +- .../KeywordMarkerTokenFilterFactory.java | 10 +- .../common/LowerCaseTokenFilterFactory.java | 2 - .../common/MappingCharFilterFactory.java | 9 +- .../common/MultiplexerTokenFilterFactory.java | 9 +- .../common/NGramTokenFilterFactory.java | 10 +- .../common/NGramTokenizerFactory.java | 25 +- ...PatternCaptureGroupTokenFilterFactory.java | 1 - .../common/PorterStemTokenFilterFactory.java | 2 - .../ScriptedConditionTokenFilterFactory.java | 20 +- .../analysis/common/SnowballAnalyzer.java | 54 +- .../common/SnowballAnalyzerProvider.java | 16 +- .../common/StemmerTokenFilterFactory.java | 265 +- .../SynonymGraphTokenFilterFactory.java | 12 +- .../common/SynonymTokenFilterFactory.java | 38 +- .../analysis/common/ThaiAnalyzerProvider.java | 3 +- .../common/TrimTokenFilterFactory.java | 2 +- .../analysis/common/UniqueTokenFilter.java | 2 - .../common/UpperCaseTokenFilterFactory.java | 2 - .../WordDelimiterGraphTokenFilterFactory.java | 3 +- .../WordDelimiterTokenFilterFactory.java | 45 +- .../analysis/common/XLowerCaseTokenizer.java | 4 +- .../ASCIIFoldingTokenFilterFactoryTests.java | 30 +- ...rdDelimiterTokenFilterFactoryTestCase.java | 138 +- .../common/CJKFilterFactoryTests.java | 10 +- .../CharGroupTokenizerFactoryTests.java | 70 +- .../common/CommonAnalysisFactoryTests.java | 24 +- .../common/CommonAnalysisPluginTests.java | 253 +- .../CommonGramsTokenFilterFactoryTests.java | 209 +- .../common/CompoundAnalysisTests.java | 26 +- .../common/DisableGraphQueryTests.java | 111 +- .../common/ESSolrSynonymParserTests.java | 15 +- .../common/ESWordnetSynonymParserTests.java | 34 +- .../EdgeNGramTokenFilterFactoryTests.java | 10 +- .../common/EdgeNGramTokenizerTests.java | 55 +- .../common/ElisionFilterFactoryTests.java | 6 +- .../common/FingerprintAnalyzerTests.java | 21 +- .../FlattenGraphTokenFilterFactoryTests.java | 39 +- .../common/HighlighterWithAnalyzersTests.java | 392 +- .../common/KeepFilterFactoryTests.java | 54 +- .../common/KeepTypesFilterFactoryTests.java | 39 +- .../KeywordMarkerFilterFactoryTests.java | 12 +- .../LimitTokenCountFilterFactoryTests.java | 36 +- .../analysis/common/MassiveWordListTests.java | 21 +- .../common/MinHashFilterFactoryTests.java | 7 +- .../common/MultiplexerTokenFilterTests.java | 43 +- .../common/NGramTokenFilterFactoryTests.java | 10 +- .../common/NGramTokenizerFactoryTests.java | 120 +- .../PathHierarchyTokenizerFactoryTests.java | 82 +- .../analysis/common/PatternAnalyzerTests.java | 178 +- .../PatternCaptureTokenFilterTests.java | 24 +- .../PredicateTokenScriptFilterTests.java | 13 +- .../RemoveDuplicatesFilterFactoryTests.java | 6 +- .../ScriptedConditionTokenFilterTests.java | 13 +- .../common/ShingleTokenFilterTests.java | 5 +- .../common/SnowballAnalyzerTests.java | 56 +- ...temmerOverrideTokenFilterFactoryTests.java | 18 +- .../StemmerTokenFilterFactoryTests.java | 47 +- .../common/SynonymsAnalysisTests.java | 107 +- .../analysis/common/TrimTokenFilterTests.java | 2 +- .../WhitespaceTokenizerFactoryTests.java | 30 +- ...DelimiterGraphTokenFilterFactoryTests.java | 191 +- .../WordDelimiterTokenFilterFactoryTests.java | 21 +- .../CommonAnalysisClientYamlTestSuiteIT.java | 2 +- .../ingest/common/IngestRestartIT.java | 191 +- .../common/AbstractStringProcessor.java | 37 +- .../ingest/common/AppendProcessor.java | 8 +- .../ingest/common/BytesProcessor.java | 10 +- .../ingest/common/CommunityIdProcessor.java | 31 +- .../ingest/common/ConvertProcessor.java | 49 +- .../ingest/common/CsvParser.java | 7 +- .../ingest/common/CsvProcessor.java | 38 +- .../ingest/common/DateFormat.java | 23 +- .../ingest/common/DateIndexNameProcessor.java | 85 +- .../ingest/common/DateProcessor.java | 62 +- .../ingest/common/DissectProcessor.java | 10 +- .../ingest/common/DotExpanderProcessor.java | 32 +- .../ingest/common/FailProcessor.java | 18 +- .../ingest/common/FailProcessorException.java | 1 - .../ingest/common/ForEachProcessor.java | 31 +- .../ingest/common/GrokProcessor.java | 59 +- .../ingest/common/GrokProcessorGetAction.java | 17 +- .../ingest/common/GsubProcessor.java | 21 +- .../ingest/common/HtmlStripProcessor.java | 10 +- .../ingest/common/IngestCommonPlugin.java | 109 +- .../ingest/common/JoinProcessor.java | 13 +- .../ingest/common/JsonProcessor.java | 82 +- .../ingest/common/KeyValueProcessor.java | 81 +- .../ingest/common/LowercaseProcessor.java | 10 +- .../common/NetworkDirectionProcessor.java | 3 +- .../ingest/common/Processors.java | 12 +- .../common/ProcessorsWhitelistExtension.java | 6 +- .../common/RegisteredDomainProcessor.java | 16 +- .../ingest/common/RemoveProcessor.java | 9 +- .../ingest/common/RenameProcessor.java | 31 +- .../ingest/common/ScriptProcessor.java | 39 +- .../ingest/common/SetProcessor.java | 36 +- .../ingest/common/SortProcessor.java | 23 +- .../ingest/common/SplitProcessor.java | 23 +- .../ingest/common/TrimProcessor.java | 11 +- .../ingest/common/URLDecodeProcessor.java | 10 +- .../ingest/common/UppercaseProcessor.java | 10 +- ...bstractStringProcessorFactoryTestCase.java | 2 +- .../AbstractStringProcessorTestCase.java | 30 +- .../common/AppendProcessorFactoryTests.java | 18 +- .../ingest/common/AppendProcessorTests.java | 28 +- .../ingest/common/BytesProcessorTests.java | 25 +- .../common/CommunityIdProcessorTests.java | 5 +- .../ingest/common/ConvertProcessorTests.java | 26 +- .../common/CsvProcessorFactoryTests.java | 2 +- .../ingest/common/CsvProcessorTests.java | 127 +- .../ingest/common/DateFormatTests.java | 89 +- .../common/DateIndexNameProcessorTests.java | 124 +- .../common/DateProcessorFactoryTests.java | 6 +- .../ingest/common/DateProcessorTests.java | 173 +- .../ingest/common/DissectProcessorTests.java | 65 +- .../DotExpanderProcessorFactoryTests.java | 8 +- .../common/DotExpanderProcessorTests.java | 28 +- .../common/FailProcessorFactoryTests.java | 8 +- .../ingest/common/FailProcessorTests.java | 3 +- .../common/ForEachProcessorFactoryTests.java | 14 +- .../ingest/common/ForEachProcessorTests.java | 214 +- .../common/GrokProcessorFactoryTests.java | 6 +- .../common/GrokProcessorGetActionTests.java | 44 +- .../ingest/common/GrokProcessorTests.java | 213 +- .../common/GsubProcessorFactoryTests.java | 6 +- .../ingest/common/JoinProcessorTests.java | 8 +- .../common/JsonProcessorFactoryTests.java | 36 +- .../ingest/common/JsonProcessorTests.java | 24 +- .../common/KeyValueProcessorFactoryTests.java | 18 +- .../ingest/common/KeyValueProcessorTests.java | 90 +- ...NetworkDirectionProcessorFactoryTests.java | 4 +- .../NetworkDirectionProcessorTests.java | 21 +- ...RegisteredDomainProcessorFactoryTests.java | 1 - .../RegisteredDomainProcessorTests.java | 37 +- .../common/RemoveProcessorFactoryTests.java | 18 +- .../ingest/common/RemoveProcessorTests.java | 10 +- .../common/RenameProcessorFactoryTests.java | 4 +- .../ingest/common/RenameProcessorTests.java | 45 +- .../common/ScriptProcessorFactoryTests.java | 53 +- .../ingest/common/ScriptProcessorTests.java | 24 +- .../common/SetProcessorFactoryTests.java | 24 +- .../ingest/common/SetProcessorTests.java | 33 +- .../common/SortProcessorFactoryTests.java | 2 +- .../ingest/common/SortProcessorTests.java | 14 +- .../common/SplitProcessorFactoryTests.java | 4 +- .../ingest/common/SplitProcessorTests.java | 23 +- .../ingest/common/UriPartsProcessorTests.java | 2 +- .../IngestCommonClientYamlTestSuiteIT.java | 1 - .../ingest/geoip/UpdateDatabasesIT.java | 16 +- .../ingest/geoip/AbstractGeoIpIT.java | 21 +- .../ingest/geoip/GeoIpDownloaderIT.java | 162 +- .../ingest/geoip/GeoIpDownloaderStatsIT.java | 21 +- .../geoip/GeoIpProcessorNonIngestNodeIT.java | 12 +- ...gDatabasesWhilePerformingGeoLookupsIT.java | 55 +- .../ingest/geoip/DatabaseNodeService.java | 111 +- .../geoip/DatabaseReaderLazyLoader.java | 33 +- .../ingest/geoip/GeoIpCache.java | 24 +- .../ingest/geoip/GeoIpDownloader.java | 93 +- .../geoip/GeoIpDownloaderTaskExecutor.java | 90 +- .../ingest/geoip/GeoIpProcessor.java | 108 +- .../ingest/geoip/GeoIpTaskParams.java | 11 +- .../ingest/geoip/GeoIpTaskState.java | 48 +- .../ingest/geoip/IngestGeoIpPlugin.java | 94 +- .../ingest/geoip/TarInputStream.java | 5 +- .../geoip/stats/GeoIpDownloaderStats.java | 81 +- .../stats/GeoIpDownloaderStatsAction.java | 19 +- .../GeoIpDownloaderStatsTransportAction.java | 36 +- .../stats/RestGeoIpDownloaderStatsAction.java | 7 +- .../ingest/geoip/ConfigDatabasesTests.java | 8 +- .../geoip/DatabaseNodeServiceTests.java | 86 +- .../ingest/geoip/GeoIpCacheTests.java | 17 +- .../ingest/geoip/GeoIpDownloaderTests.java | 162 +- .../geoip/GeoIpProcessorFactoryTests.java | 45 +- .../ingest/geoip/GeoIpProcessorTests.java | 268 +- .../GeoIpTaskStateSerializationTests.java | 11 +- .../ingest/geoip/TarInputStreamTests.java | 28 +- ...atsActionNodeResponseSerializingTests.java | 13 +- ...erStatsActionResponseSerializingTests.java | 10 +- .../GeoIpDownloaderStatsSerializingTests.java | 2 +- .../ingest/useragent/DeviceTypeParser.java | 10 +- .../useragent/IngestUserAgentPlugin.java | 33 +- .../ingest/useragent/UserAgentCache.java | 4 +- .../ingest/useragent/UserAgentParser.java | 153 +- .../ingest/useragent/UserAgentProcessor.java | 55 +- .../useragent/DeviceTypeParserTests.java | 19 +- .../UserAgentProcessorFactoryTests.java | 15 +- .../useragent/UserAgentProcessorTests.java | 123 +- .../script/expression/MoreExpressionIT.java | 335 +- .../script/expression/StoredExpressionIT.java | 36 +- .../expression/CountMethodValueSource.java | 2 +- .../script/expression/DateField.java | 34 +- .../expression/DateMethodValueSource.java | 4 +- .../script/expression/DateObject.java | 114 +- .../expression/DateObjectValueSource.java | 10 +- .../expression/EmptyMemberValueSource.java | 2 +- .../ExpressionAggregationScript.java | 5 +- .../ExpressionNumberSortScript.java | 5 +- .../script/expression/ExpressionPlugin.java | 4 +- .../expression/ExpressionScoreScript.java | 2 +- .../expression/ExpressionScriptEngine.java | 177 +- .../ExpressionTermSetQueryScript.java | 3 +- .../expression/FieldDataValueSource.java | 2 +- .../script/expression/GeoField.java | 12 +- .../expression/GeoLatitudeValueSource.java | 2 +- .../expression/GeoLongitudeValueSource.java | 2 +- .../script/expression/NumericField.java | 29 +- .../ReplaceableConstDoubleValueSource.java | 6 +- .../ExpressionFieldScriptTests.java | 8 +- .../ExpressionNumberSortScriptTests.java | 11 +- .../ExpressionTermsSetQueryTests.java | 14 +- .../LangExpressionClientYamlTestSuiteIT.java | 1 - .../mustache/MultiSearchTemplateIT.java | 38 +- .../script/mustache/SearchTemplateIT.java | 301 +- .../SearchTemplateWithoutContentIT.java | 12 +- .../mustache/CustomMustacheFactory.java | 17 +- .../CustomReflectionObjectHandler.java | 5 +- .../mustache/MultiSearchTemplateRequest.java | 9 +- .../mustache/MultiSearchTemplateResponse.java | 6 +- .../script/mustache/MustachePlugin.java | 27 +- .../script/mustache/MustacheScriptEngine.java | 8 +- .../RestMultiSearchTemplateAction.java | 38 +- .../RestRenderSearchTemplateAction.java | 5 +- .../mustache/RestSearchTemplateAction.java | 12 +- .../mustache/SearchTemplateRequest.java | 24 +- .../SearchTemplateRequestBuilder.java | 3 +- .../mustache/SearchTemplateResponse.java | 21 +- .../TransportMultiSearchTemplateAction.java | 11 +- .../TransportSearchTemplateAction.java | 51 +- .../mustache/CustomMustacheFactoryTests.java | 36 +- .../MultiSearchTemplateRequestTests.java | 27 +- .../MultiSearchTemplateResponseTests.java | 51 +- .../mustache/MustacheScriptEngineTests.java | 133 +- .../script/mustache/MustacheTests.java | 241 +- .../RestMultiSearchTemplateActionTests.java | 29 +- .../RestSearchTemplateActionTests.java | 17 +- .../mustache/SearchTemplateRequestTests.java | 22 +- .../SearchTemplateRequestXContentTests.java | 100 +- .../mustache/SearchTemplateResponseTests.java | 127 +- .../LangMustacheClientYamlTestSuiteIT.java | 1 - .../elasticsearch/painless/spi/Whitelist.java | 9 +- .../painless/spi/WhitelistClass.java | 18 +- .../painless/spi/WhitelistClassBinding.java | 18 +- .../painless/spi/WhitelistConstructor.java | 7 +- .../painless/spi/WhitelistField.java | 7 +- .../spi/WhitelistInstanceBinding.java | 18 +- .../painless/spi/WhitelistLoader.java | 146 +- .../painless/spi/WhitelistMethod.java | 18 +- .../DynamicTypeAnnotationParser.java | 2 +- .../annotation/InjectConstantAnnotation.java | 1 + .../annotation/WhitelistAnnotationParser.java | 18 +- .../painless/WhitelistLoaderTests.java | 46 +- .../painless/ContextApiSpecGenerator.java | 32 +- .../painless/ContextDocGenerator.java | 294 +- .../painless/ContextGeneratorCommon.java | 66 +- .../painless/JavadocExtractor.java | 64 +- .../painless/PainlessInfoJson.java | 106 +- .../painless/AnalyzerCaster.java | 75 +- .../elasticsearch/painless/ClassWriter.java | 44 +- .../org/elasticsearch/painless/Compiler.java | 19 +- .../painless/CompilerSettings.java | 22 +- .../java/org/elasticsearch/painless/Def.java | 1505 +- .../elasticsearch/painless/DefBootstrap.java | 134 +- .../org/elasticsearch/painless/DefMath.java | 493 +- .../elasticsearch/painless/FunctionRef.java | 153 +- .../org/elasticsearch/painless/Globals.java | 4 +- .../painless/LambdaBootstrap.java | 227 +- .../elasticsearch/painless/MethodWriter.java | 132 +- .../org/elasticsearch/painless/Operation.java | 50 +- .../elasticsearch/painless/PainlessError.java | 2 +- .../painless/PainlessPlugin.java | 53 +- .../painless/PainlessScript.java | 8 +- .../painless/PainlessScriptEngine.java | 154 +- .../painless/ScriptClassInfo.java | 117 +- .../org/elasticsearch/painless/Utility.java | 10 +- .../painless/WriterConstants.java | 146 +- .../action/PainlessContextAction.java | 19 +- .../PainlessContextClassBindingInfo.java | 59 +- .../action/PainlessContextClassInfo.java | 119 +- .../PainlessContextConstructorInfo.java | 30 +- .../action/PainlessContextFieldInfo.java | 31 +- .../painless/action/PainlessContextInfo.java | 165 +- .../PainlessContextInstanceBindingInfo.java | 51 +- .../action/PainlessContextMethodInfo.java | 51 +- .../action/PainlessExecuteAction.java | 208 +- .../painless/antlr/EnhancedPainlessLexer.java | 31 +- .../painless/antlr/PainlessLexer.java | 934 +- .../painless/antlr/PainlessParser.java | 9793 +++++---- .../antlr/PainlessParserBaseVisitor.java | 1404 +- .../painless/antlr/PainlessParserVisitor.java | 1114 +- .../painless/antlr/ParserErrorStrategy.java | 21 +- .../painless/antlr/SuggestLexer.java | 938 +- .../elasticsearch/painless/antlr/Walker.java | 197 +- .../painless/api/Augmentation.java | 115 +- .../org/elasticsearch/painless/api/CIDR.java | 12 +- .../org/elasticsearch/painless/api/Json.java | 7 +- .../painless/api/LimitedCharSequence.java | 41 +- .../org/elasticsearch/painless/ir/IRNode.java | 3 +- .../painless/lookup/PainlessCast.java | 42 +- .../painless/lookup/PainlessClass.java | 35 +- .../painless/lookup/PainlessClassBinding.java | 19 +- .../painless/lookup/PainlessClassBuilder.java | 30 +- .../painless/lookup/PainlessConstructor.java | 19 +- .../painless/lookup/PainlessField.java | 17 +- .../lookup/PainlessInstanceBinding.java | 12 +- .../painless/lookup/PainlessLookup.java | 32 +- .../lookup/PainlessLookupBuilder.java | 1518 +- .../lookup/PainlessLookupUtility.java | 36 +- .../painless/lookup/PainlessMethod.java | 25 +- .../painless/node/EAssignment.java | 10 +- .../elasticsearch/painless/node/ECall.java | 10 +- .../elasticsearch/painless/node/ELambda.java | 9 +- .../elasticsearch/painless/node/SBlock.java | 2 +- .../org/elasticsearch/painless/node/SFor.java | 10 +- .../painless/node/SFunction.java | 17 +- ...faultConstantFoldingOptimizationPhase.java | 644 +- .../phase/DefaultIRTreeToASMBytesPhase.java | 398 +- .../phase/DefaultSemanticAnalysisPhase.java | 1081 +- .../phase/DefaultSemanticHeaderPhase.java | 54 +- ...tStringConcatenationOptimizationPhase.java | 4 +- .../phase/DefaultUserTreeToIRTreePhase.java | 384 +- .../painless/phase/IRTreeVisitor.java | 65 + .../phase/PainlessSemanticAnalysisPhase.java | 48 +- .../phase/PainlessSemanticHeaderPhase.java | 5 +- .../phase/PainlessUserTreeToIRTreePhase.java | 113 +- .../painless/phase/UserTreeVisitor.java | 42 + .../painless/symbol/Decorations.java | 3 +- .../painless/symbol/Decorator.java | 2 +- .../painless/symbol/FunctionTable.java | 49 +- .../painless/symbol/ScriptScope.java | 10 +- .../painless/symbol/SemanticScope.java | 4 +- .../painless/symbol/WriteScope.java | 2 +- .../toxcontent/DecorationToXContent.java | 2 +- .../toxcontent/UserTreeToXContent.java | 9 +- .../toxcontent/XContentBuilderWrapper.java | 16 +- .../elasticsearch/painless/AdditionTests.java | 268 +- .../painless/AnalyzerCasterTests.java | 6 +- .../org/elasticsearch/painless/AndTests.java | 70 +- .../painless/ArrayLikeObjectTestCase.java | 96 +- .../elasticsearch/painless/ArrayTests.java | 14 +- .../painless/AugmentationTests.java | 196 +- .../painless/BaseClassTests.java | 796 +- .../elasticsearch/painless/BasicAPITests.java | 106 +- .../painless/BasicExpressionTests.java | 154 +- .../painless/BasicStatementTests.java | 763 +- .../elasticsearch/painless/BindingsTests.java | 51 +- .../painless/BoxedCastTests.java | 752 +- .../elasticsearch/painless/BufferTests.java | 40 +- .../org/elasticsearch/painless/CidrTests.java | 4 +- .../painless/ComparisonTests.java | 24 +- .../painless/ConditionalTests.java | 52 +- .../painless/ConstantFoldingTests.java | 12 +- .../painless/ContextExampleTests.java | 83 +- .../elasticsearch/painless/DateTimeTests.java | 303 +- .../elasticsearch/painless/DebugTests.java | 20 +- .../org/elasticsearch/painless/Debugger.java | 50 +- .../painless/DefBootstrapTests.java | 283 +- .../elasticsearch/painless/DefCastTests.java | 508 +- .../painless/DefEncodingTests.java | 63 +- .../painless/DefOptimizationTests.java | 210 +- .../elasticsearch/painless/DivisionTests.java | 170 +- .../painless/DynamicTypeTests.java | 156 +- .../elasticsearch/painless/ElvisTests.java | 14 +- .../org/elasticsearch/painless/EmitTests.java | 4 +- .../elasticsearch/painless/EqualsTests.java | 8 +- .../elasticsearch/painless/FactoryTests.java | 300 +- .../FeatureTestAugmentationObject.java | 15 +- .../painless/FeatureTestObject.java | 11 +- .../painless/FeatureTestObject2.java | 6 +- .../painless/FloatOverflowTests.java | 6 +- .../painless/FunctionRefTests.java | 353 +- .../elasticsearch/painless/FunctionTests.java | 54 +- .../painless/GeneralCastTests.java | 195 +- .../painless/GetByPathAugmentationTests.java | 29 +- .../painless/IncrementTests.java | 44 +- .../painless/InitializerTests.java | 40 +- .../painless/InjectionTests.java | 286 +- .../painless/IntegerOverflowTests.java | 36 +- .../elasticsearch/painless/LambdaTests.java | 223 +- .../org/elasticsearch/painless/ListTests.java | 21 +- .../elasticsearch/painless/LookupTests.java | 87 +- .../org/elasticsearch/painless/MapTests.java | 4 +- .../painless/MultiplicationTests.java | 146 +- .../painless/NoSemiColonTests.java | 10 +- .../org/elasticsearch/painless/OrTests.java | 82 +- .../elasticsearch/painless/OverloadTests.java | 36 +- .../elasticsearch/painless/PostfixTests.java | 26 +- .../painless/PromotionTests.java | 132 +- .../painless/RegexLimitTests.java | 163 +- .../elasticsearch/painless/RegexTests.java | 89 +- .../painless/RemainderTests.java | 146 +- .../painless/ScriptEngineTests.java | 8 +- .../painless/ScriptTestCase.java | 21 +- .../ScriptedMetricAggContextsTests.java | 67 +- .../elasticsearch/painless/ShiftTests.java | 236 +- .../painless/SimilarityScriptTests.java | 34 +- .../elasticsearch/painless/StringTests.java | 84 +- .../painless/SubtractionTests.java | 226 +- .../painless/TestFieldScript.java | 9 +- .../org/elasticsearch/painless/ThisTests.java | 22 +- .../painless/ToXContentTests.java | 22 +- .../elasticsearch/painless/TryCatchTests.java | 117 +- .../elasticsearch/painless/UnaryTests.java | 18 +- .../painless/UserFunctionTests.java | 130 +- .../painless/WhenThingsGoWrongTests.java | 242 +- .../org/elasticsearch/painless/XorTests.java | 76 +- .../painless/action/ContextInfoTests.java | 72 +- .../action/PainlessExecuteApiTests.java | 284 +- .../action/PainlessExecuteRequestTests.java | 15 +- .../action/PainlessExecuteResponseTests.java | 2 +- .../painless/action/SuggestTests.java | 226 +- .../api/LimitedCharSequenceTests.java | 44 +- .../LangPainlessClientYamlTestSuiteIT.java | 2 +- .../mapper/MatchOnlyTextFieldMapperTests.java | 18 +- .../TokenCountFieldMapperIntegrationIT.java | 119 +- .../mapper/extras/MapperExtrasPlugin.java | 4 +- .../extras/MatchOnlyTextFieldMapper.java | 47 +- .../mapper/extras/RankFeatureFieldMapper.java | 34 +- .../extras/RankFeatureQueryBuilder.java | 116 +- .../extras/RankFeaturesFieldMapper.java | 49 +- .../mapper/extras/ScaledFloatFieldMapper.java | 95 +- .../extras/SearchAsYouTypeFieldMapper.java | 242 +- .../mapper/extras/SourceIntervalsSource.java | 10 +- .../mapper/extras/TokenCountFieldMapper.java | 35 +- .../index/mapper/extras/BWCTemplateTests.java | 2 +- .../extras/MatchOnlyTextFieldTypeTests.java | 2 - .../extras/RankFeatureFieldMapperTests.java | 17 +- .../extras/RankFeatureFieldTypeTests.java | 1 - .../RankFeatureMetaFieldMapperTests.java | 27 +- .../extras/RankFeatureQueryBuilderTests.java | 111 +- .../extras/RankFeaturesFieldMapperTests.java | 23 +- .../extras/RankFeaturesFieldTypeTests.java | 1 - .../extras/ScaledFloatFieldMapperTests.java | 164 +- .../extras/ScaledFloatFieldTypeTests.java | 67 +- .../extras/SearchAsYouTypeAnalyzerTests.java | 236 +- .../SearchAsYouTypeFieldMapperTests.java | 344 +- .../extras/SearchAsYouTypeFieldTypeTests.java | 87 +- .../extras/SourceConfirmedTextQueryTests.java | 1 - .../extras/SourceIntervalsSourceTests.java | 18 +- .../extras/TokenCountFieldMapperTests.java | 32 +- .../MapperExtrasClientYamlTestSuiteIT.java | 1 - .../AbstractParentChildTestCase.java | 36 +- .../join/aggregations/ChildrenIT.java | 158 +- .../join/aggregations/ParentIT.java | 155 +- .../join/query/ChildQuerySearchIT.java | 914 +- .../elasticsearch/join/query/InnerHitsIT.java | 388 +- .../join/query/ParentChildTestCase.java | 25 +- .../elasticsearch/join/ParentJoinPlugin.java | 3 +- .../join/aggregations/Children.java | 3 +- .../ChildrenAggregationBuilder.java | 32 +- .../ChildrenAggregatorFactory.java | 37 +- .../ChildrenToParentAggregator.java | 29 +- .../join/aggregations/Parent.java | 3 +- .../ParentAggregationBuilder.java | 32 +- .../aggregations/ParentAggregatorFactory.java | 37 +- .../aggregations/ParentJoinAggregator.java | 37 +- .../ParentToChildrenAggregator.java | 29 +- .../join/aggregations/ParsedChildren.java | 2 +- .../join/aggregations/ParsedParent.java | 2 +- .../org/elasticsearch/join/mapper/Joiner.java | 1 + .../join/mapper/ParentJoinFieldMapper.java | 63 +- .../elasticsearch/join/mapper/Relations.java | 3 +- .../join/query/HasChildQueryBuilder.java | 108 +- .../join/query/HasParentQueryBuilder.java | 64 +- .../ParentChildInnerHitContextBuilder.java | 28 +- .../join/query/ParentIdQueryBuilder.java | 23 +- .../spi/ParentJoinNamedXContentProvider.java | 6 +- .../ChildrenToParentAggregatorTests.java | 79 +- .../aggregations/InternalChildrenTests.java | 22 +- .../aggregations/InternalParentTests.java | 18 +- .../join/aggregations/ParentTests.java | 6 +- .../ParentToChildrenAggregatorTests.java | 43 +- .../join/mapper/JoinFieldTypeTests.java | 7 +- .../mapper/ParentJoinFieldMapperTests.java | 106 +- .../join/query/HasChildQueryBuilderTests.java | 193 +- .../query/HasParentQueryBuilderTests.java | 125 +- .../join/query/ParentIdQueryBuilderTests.java | 70 +- .../percolator/PercolatorQuerySearchIT.java | 1403 +- .../percolator/PercolateQuery.java | 41 +- .../percolator/PercolateQueryBuilder.java | 140 +- .../percolator/PercolatorFieldMapper.java | 84 +- .../PercolatorHighlightSubFetchPhase.java | 20 +- .../PercolatorMatchedSlotSubFetchPhase.java | 6 +- .../percolator/PercolatorPlugin.java | 5 +- .../percolator/QueryAnalyzer.java | 88 +- .../percolator/CandidateQueryTests.java | 265 +- .../PercolateQueryBuilderTests.java | 157 +- .../percolator/PercolateQueryTests.java | 54 +- .../PercolateWithNestedQueryBuilderTests.java | 19 +- .../PercolatorFieldMapperTests.java | 563 +- ...PercolatorHighlightSubFetchPhaseTests.java | 39 +- ...rcolatorMatchedSlotSubFetchPhaseTests.java | 33 +- .../PercolatorQuerySearchTests.java | 221 +- .../percolator/QueryAnalyzerTests.java | 294 +- .../percolator/QueryBuilderStoreTests.java | 12 +- .../index/rankeval/RankEvalRequestIT.java | 76 +- .../rankeval/DiscountedCumulativeGain.java | 55 +- .../index/rankeval/EvalQueryQuality.java | 24 +- .../index/rankeval/EvaluationMetric.java | 10 +- .../rankeval/ExpectedReciprocalRank.java | 42 +- .../index/rankeval/MeanReciprocalRank.java | 41 +- .../index/rankeval/PrecisionAtK.java | 28 +- .../RankEvalNamedXContentProvider.java | 76 +- .../index/rankeval/RankEvalPlugin.java | 26 +- .../index/rankeval/RankEvalRequest.java | 13 +- .../rankeval/RankEvalRequestBuilder.java | 5 +- .../index/rankeval/RankEvalResponse.java | 32 +- .../index/rankeval/RankEvalSpec.java | 44 +- .../index/rankeval/RatedDocument.java | 8 +- .../index/rankeval/RatedRequest.java | 71 +- .../index/rankeval/RatedSearchHit.java | 26 +- .../index/rankeval/RecallAtK.java | 30 +- .../index/rankeval/RestRankEvalAction.java | 12 +- .../rankeval/TransportRankEvalAction.java | 44 +- .../DiscountedCumulativeGainTests.java | 71 +- .../index/rankeval/EvalQueryQualityTests.java | 74 +- .../rankeval/ExpectedReciprocalRankTests.java | 71 +- .../rankeval/MeanReciprocalRankTests.java | 28 +- .../index/rankeval/PrecisionAtKTests.java | 59 +- .../index/rankeval/RankEvalRequestTests.java | 25 +- .../index/rankeval/RankEvalResponseTests.java | 104 +- .../index/rankeval/RankEvalSpecTests.java | 71 +- .../index/rankeval/RatedDocumentTests.java | 39 +- .../index/rankeval/RatedRequestsTests.java | 144 +- .../index/rankeval/RatedSearchHitTests.java | 32 +- .../index/rankeval/RecallAtKTests.java | 20 +- .../TransportRankEvalActionTests.java | 43 +- .../documentation/ReindexDocumentationIT.java | 29 +- ...rollDocumentsAfterConflictsIntegTests.java | 59 +- .../migration/FeatureMigrationIT.java | 10 +- .../migration/MultiFeatureMigrationIT.java | 32 +- .../index/reindex/ManyDocumentsIT.java | 79 +- .../reindex/ReindexWithoutContentIT.java | 6 +- .../remote/ReindexFromOldRemoteIT.java | 14 +- .../AbstractAsyncBulkByScrollAction.java | 151 +- .../AbstractBaseReindexRestHandler.java | 29 +- .../AbstractBulkByQueryRestHandler.java | 45 +- .../reindex/AsyncDeleteByQueryAction.java | 12 +- .../BulkByScrollParallelizationHelper.java | 47 +- ...kIndexByScrollResponseContentListener.java | 6 +- .../elasticsearch/reindex/ReindexPlugin.java | 55 +- .../reindex/ReindexSslConfig.java | 9 +- .../reindex/ReindexValidator.java | 53 +- .../org/elasticsearch/reindex/Reindexer.java | 178 +- .../reindex/RestDeleteByQueryAction.java | 5 +- .../reindex/RestReindexAction.java | 7 +- .../reindex/RestRethrottleAction.java | 10 +- .../reindex/RestUpdateByQueryAction.java | 2 +- .../reindex/RethrottleRequest.java | 22 +- .../reindex/RethrottleRequestBuilder.java | 3 +- .../reindex/TransportDeleteByQueryAction.java | 37 +- .../reindex/TransportReindexAction.java | 72 +- .../reindex/TransportRethrottleAction.java | 75 +- .../reindex/TransportUpdateByQueryAction.java | 72 +- .../reindex/remote/RemoteRequestBuilders.java | 29 +- .../reindex/remote/RemoteResponseParsers.java | 179 +- .../remote/RemoteScrollableHitSource.java | 76 +- ...yncBulkByScrollActionMetadataTestCase.java | 6 +- ...AsyncBulkByScrollActionScriptTestCase.java | 12 +- .../reindex/AsyncBulkByScrollActionTests.java | 314 +- ...ulkByScrollParallelizationHelperTests.java | 10 +- .../BulkIndexByScrollResponseMatcher.java | 16 +- .../BulkIndexByScrollResponseTests.java | 33 +- .../elasticsearch/reindex/CancelTests.java | 118 +- .../ClientScrollableHitSourceTests.java | 91 +- .../reindex/DeleteByQueryBasicTests.java | 119 +- .../reindex/DeleteByQueryConcurrentTests.java | 10 +- .../reindex/ReindexBasicTests.java | 20 +- .../reindex/ReindexFailureTests.java | 20 +- ...ReindexFromRemoteBuildRestClientTests.java | 32 +- .../ReindexFromRemoteWhitelistTests.java | 53 +- .../ReindexFromRemoteWithAuthTests.java | 79 +- .../reindex/ReindexMetadataTests.java | 21 +- .../reindex/ReindexRestClientSslTests.java | 52 +- .../reindex/ReindexScriptTests.java | 6 +- .../reindex/ReindexSingleNodeTests.java | 7 +- .../ReindexSourceTargetValidationTests.java | 87 +- .../reindex/ReindexTestCase.java | 6 +- .../reindex/ReindexVersioningTests.java | 19 +- .../reindex/RestDeleteByQueryActionTests.java | 12 +- .../reindex/RestReindexActionTests.java | 21 +- .../reindex/RestUpdateByQueryActionTests.java | 11 +- .../reindex/RethrottleTests.java | 82 +- .../org/elasticsearch/reindex/RetryTests.java | 86 +- .../elasticsearch/reindex/RoundTripTests.java | 18 +- .../TransportRethrottleActionTests.java | 75 +- .../reindex/UpdateByQueryBasicTests.java | 52 +- .../reindex/UpdateByQueryMetadataTests.java | 23 +- .../UpdateByQueryWhileModifyingTests.java | 24 +- .../reindex/UpdateByQueryWithScriptTests.java | 31 +- .../reindex/remote/RemoteInfoTests.java | 41 +- .../remote/RemoteRequestBuildersTests.java | 65 +- .../remote/RemoteResponseParsersTests.java | 4 +- .../RemoteScrollableHitSourceTests.java | 89 +- .../url/URLSnapshotRestoreIT.java | 41 +- .../blobstore/url/URLBlobContainer.java | 10 +- .../common/blobstore/url/URLBlobStore.java | 12 +- .../url/http/HttpResponseInputStream.java | 12 +- .../url/http/HttpURLBlobContainer.java | 18 +- .../url/http/RetryingHttpInputStream.java | 41 +- .../blobstore/url/http/URLHttpClient.java | 7 +- .../url/http/URLHttpClientSettings.java | 6 +- .../repository/url/URLRepositoryPlugin.java | 45 +- .../repositories/url/URLRepository.java | 59 +- .../blobstore/url/FileURLBlobStoreTests.java | 8 +- .../blobstore/url/HttpURLBlobStoreTests.java | 3 +- .../url/URLBlobContainerRetriesTests.java | 23 +- .../http/RetryingHttpInputStreamTests.java | 38 +- .../url/http/URLHttpClientTests.java | 71 +- .../repositories/url/URLRepositoryTests.java | 27 +- .../RepositoryURLClientYamlTestSuiteIT.java | 20 +- .../elasticsearch/systemd/SystemdPlugin.java | 20 +- .../systemd/SystemdPluginTests.java | 65 +- .../netty4/Netty4HttpRequestSizeLimitIT.java | 13 +- .../http/netty4/Netty4PipeliningIT.java | 3 +- .../rest/discovery/Zen2RestApiIT.java | 19 +- .../transport/netty4/ESLoggingHandlerIT.java | 79 +- ...Netty4TransportMultiPortIntegrationIT.java | 6 +- .../rest/Netty4BadRequestIT.java | 19 +- .../rest/Netty4HeadBodyIsEmptyIT.java | 15 +- .../http/netty4/Netty4HttpChannel.java | 6 +- .../netty4/Netty4HttpPipeliningHandler.java | 1 + .../http/netty4/Netty4HttpRequest.java | 84 +- .../http/netty4/Netty4HttpRequestCreator.java | 1 + .../http/netty4/Netty4HttpRequestHandler.java | 1 + .../http/netty4/Netty4HttpResponse.java | 2 +- .../netty4/Netty4HttpResponseCreator.java | 1 + .../http/netty4/Netty4HttpServerChannel.java | 1 + .../netty4/Netty4HttpServerTransport.java | 61 +- .../netty4/CopyBytesServerSocketChannel.java | 1 + .../netty4/CopyBytesSocketChannel.java | 9 +- .../netty4/Netty4MessageChannelHandler.java | 11 +- .../transport/netty4/Netty4Plugin.java | 72 +- .../transport/netty4/Netty4TcpChannel.java | 16 +- .../netty4/Netty4TcpServerChannel.java | 1 + .../transport/netty4/Netty4Transport.java | 65 +- .../transport/netty4/Netty4Utils.java | 12 +- .../transport/netty4/NettyAllocator.java | 48 +- .../transport/netty4/SharedGroupFactory.java | 13 +- .../http/netty4/Netty4BadRequestTests.java | 26 +- .../http/netty4/Netty4HttpClient.java | 18 +- .../Netty4HttpPipeliningHandlerTests.java | 23 +- .../Netty4HttpServerPipeliningTests.java | 23 +- .../Netty4HttpServerTransportTests.java | 198 +- .../netty4/CopyBytesSocketChannelTests.java | 3 +- .../Netty4SizeHeaderFrameDecoderTests.java | 12 +- .../transport/netty4/Netty4UtilsTests.java | 10 +- .../netty4/NettyTransportMultiPortTests.java | 13 +- .../netty4/SharedGroupFactoryTests.java | 1 - .../netty4/SimpleNetty4TransportTests.java | 48 +- .../ICUCollationKeywordFieldMapperIT.java | 280 +- .../analysis/icu/ICUCollationKeyFilter.java | 69 +- .../icu/ICUCollationKeywordFieldMapper.java | 175 +- .../analysis/icu/IcuAnalyzerProvider.java | 11 +- .../icu/IcuCollationTokenFilterFactory.java | 16 +- .../icu/IcuFoldingTokenFilterFactory.java | 6 +- .../icu/IcuNormalizerCharFilterFactory.java | 7 +- .../icu/IcuNormalizerTokenFilterFactory.java | 6 +- .../analysis/icu/IcuTokenizerFactory.java | 10 +- .../icu/IcuTransformTokenFilterFactory.java | 1 + .../icu/IndexableBinaryStringTools.java | 354 +- .../analysis/icu/AnalysisICUFactoryTests.java | 6 - .../analysis/icu/CollationFieldTypeTests.java | 83 +- .../ICUCollationKeywordFieldMapperTests.java | 22 +- .../ICUCollationKeywordFieldTypeTests.java | 10 +- .../plugin/analysis/icu/IcuAnalyzerTests.java | 39 +- .../icu/IcuTokenizerFactoryTests.java | 39 +- .../icu/IndexableBinaryStringToolsTests.java | 416 +- .../analysis/icu/SimpleIcuAnalysisTests.java | 7 - .../SimpleIcuCollationTokenFilterTests.java | 107 +- .../SimpleIcuNormalizerCharFilterTests.java | 7 +- .../analysis/IcuClientYamlTestSuiteIT.java | 1 - .../JapaneseStopTokenFilterFactory.java | 11 +- .../kuromoji/KuromojiAnalyzerProvider.java | 1 - .../kuromoji/KuromojiTokenizerFactory.java | 12 +- .../AnalysisKuromojiFactoryTests.java | 7 - .../kuromoji/KuromojiAnalysisTests.java | 83 +- .../KuromojiClientYamlTestSuiteIT.java | 1 - .../analysis/nori/NoriAnalyzerProvider.java | 9 +- .../NoriPartOfSpeechStopFilterFactory.java | 1 - .../analysis/nori/NoriTokenizerFactory.java | 14 +- .../nori/AnalysisNoriFactoryTests.java | 4 - .../analysis/nori/NoriAnalysisTests.java | 41 +- .../analysis/NoriClientYamlTestSuiteIT.java | 1 - .../phonetic/AnalysisPhoneticPlugin.java | 1 - .../analysis/phonetic/HaasePhonetik.java | 18 +- .../analysis/phonetic/KoelnerPhonetik.java | 17 +- .../phonetic/PhoneticTokenFilterFactory.java | 60 +- .../AnalysisPhoneticFactoryTests.java | 5 +- .../phonetic/SimplePhoneticAnalysisTests.java | 58 +- .../PhoneticClientYamlTestSuiteIT.java | 1 - .../SmartChineseStopTokenFilterFactory.java | 10 +- .../AnalysisSmartChineseFactoryTests.java | 3 +- .../SimpleSmartChineseAnalysisTests.java | 5 +- .../SmartCNClientYamlTestSuiteIT.java | 1 - .../pl/PolishStemTokenFilterFactory.java | 6 +- .../pl/PolishStopTokenFilterFactory.java | 4 +- .../stempel/AnalysisStempelPlugin.java | 3 +- .../analysis/AnalysisPolishFactoryTests.java | 2 +- .../index/analysis/PolishAnalysisTests.java | 3 +- .../SimplePolishTokenFilterTests.java | 4 +- .../StempelClientYamlTestSuiteIT.java | 1 - .../XUkrainianMorfologikAnalyzer.java | 11 +- .../SimpleUkrainianAnalyzerTests.java | 1 - .../ukrainian/UkrainianAnalysisTests.java | 3 +- .../UkrainianClientYamlTestSuiteIT.java | 1 - .../AbstractAzureComputeServiceTestCase.java | 11 +- .../AzureDiscoveryClusterFormationTests.java | 35 +- .../azure/classic/AzureSimpleTests.java | 16 +- .../classic/AzureTwoStartedNodesTests.java | 4 +- .../management/AzureComputeService.java | 87 +- .../management/AzureComputeServiceImpl.java | 40 +- .../azure/classic/AzureSeedHostsProvider.java | 39 +- .../azure/classic/AzureDiscoveryPlugin.java | 41 +- ...veryAzureClassicClientYamlTestSuiteIT.java | 1 - ...azonEC2DiscoveryClientYamlTestSuiteIT.java | 1 + .../discovery/ec2/AmazonEC2Fixture.java | 49 +- .../discovery/ec2/AbstractAwsTestCase.java | 11 +- .../ec2/Ec2DiscoveryUpdateSettingsTests.java | 13 +- .../discovery/ec2/AmazonEc2Reference.java | 2 +- .../ec2/AwsEc2SeedHostsProvider.java | 57 +- .../discovery/ec2/AwsEc2Service.java | 37 +- .../discovery/ec2/AwsEc2ServiceImpl.java | 12 +- .../discovery/ec2/Ec2ClientSettings.java | 77 +- .../discovery/ec2/Ec2DiscoveryPlugin.java | 51 +- .../discovery/ec2/Ec2NameResolver.java | 4 +- .../ec2/AbstractEC2MockAPITestCase.java | 11 +- .../discovery/ec2/AwsEc2ServiceImplTests.java | 87 +- .../discovery/ec2/EC2RetriesTests.java | 32 +- .../ec2/Ec2DiscoveryPluginTests.java | 49 +- .../discovery/ec2/Ec2DiscoveryTests.java | 119 +- .../discovery/ec2/Ec2NetworkTests.java | 41 +- .../ec2/CloudAwsClientYamlTestSuiteIT.java | 1 - .../GCEDiscoveryClientYamlTestSuiteIT.java | 1 + .../elasticsearch/cloud/gce/GCEFixture.java | 138 +- .../discovery/gce/GceDiscoverTests.java | 38 +- .../cloud/gce/GceInstancesService.java | 19 +- .../cloud/gce/GceInstancesServiceImpl.java | 41 +- .../cloud/gce/GceMetadataService.java | 33 +- .../cloud/gce/network/GceNameResolver.java | 8 +- .../elasticsearch/cloud/gce/util/Access.java | 3 +- .../discovery/gce/GceSeedHostsProvider.java | 59 +- .../gce/RetryHttpInitializerWrapper.java | 68 +- .../discovery/gce/GceDiscoveryPlugin.java | 18 +- .../gce/GceInstancesServiceImplTests.java | 1 + .../discovery/gce/GceDiscoveryTests.java | 70 +- .../gce/GceInstancesServiceMock.java | 1 + .../discovery/gce/GceMetadataServiceMock.java | 1 + .../discovery/gce/GceMockUtils.java | 1 + .../discovery/gce/GceNetworkTests.java | 15 +- .../gce/RetryHttpInitializerWrapperTests.java | 63 +- .../DiscoveryGceClientYamlTestSuiteIT.java | 1 - .../attachment/AttachmentProcessor.java | 45 +- .../attachment/IngestAttachmentPlugin.java | 6 +- .../ingest/attachment/TikaImpl.java | 48 +- .../attachment/AttachmentProcessorTests.java | 221 +- .../ingest/attachment/TikaDocTests.java | 4 +- .../ingest/attachment/TikaImplTests.java | 6 +- ...IngestAttachmentClientYamlTestSuiteIT.java | 1 - .../AnnotatedTextFieldMapperTests.java | 228 +- .../AnnotatedPassageFormatter.java | 66 +- .../AnnotatedTextFieldMapper.java | 184 +- .../AnnotatedTextHighlighter.java | 2 +- .../AnnotatedTextFieldTypeTests.java | 6 +- .../AnnotatedTextHighlighterTests.java | 186 +- .../AnnotatedTextParsingTests.java | 24 +- .../AnnotatedTextClientYamlTestSuiteIT.java | 1 - .../mapper/murmur3/Murmur3FieldMapper.java | 11 +- .../murmur3/Murmur3FieldMapperTests.java | 2 +- .../MapperMurmur3ClientYamlTestSuiteIT.java | 1 - .../index/mapper/size/SizeMappingIT.java | 55 +- .../index/mapper/size/SizeFieldMapper.java | 3 +- .../index/mapper/size/SizeMappingTests.java | 2 +- .../size/MapperSizeClientYamlTestSuiteIT.java | 1 - .../azure/AzureBlobStoreRepositoryTests.java | 29 +- .../AzureStorageCleanupThirdPartyTests.java | 37 +- .../azure/AzureBlobContainer.java | 13 +- .../azure/AzureBlobServiceClient.java | 13 +- .../repositories/azure/AzureBlobStore.java | 264 +- .../azure/AzureClientProvider.java | 105 +- .../repositories/azure/AzureRepository.java | 74 +- .../azure/AzureRepositoryPlugin.java | 37 +- .../azure/AzureStorageService.java | 22 +- .../azure/AzureStorageSettings.java | 108 +- .../CancellableRateLimitedFluxIterator.java | 5 +- .../ReactorScheduledExecutorService.java | 19 +- .../azure/AzureBlobContainerRetriesTests.java | 73 +- .../azure/AzureClientProviderTests.java | 17 +- .../azure/AzureRepositorySettingsTests.java | 108 +- .../azure/AzureStorageServiceTests.java | 94 +- ...ncellableRateLimitedFluxIteratorTests.java | 37 +- .../RepositoryAzureClientYamlTestSuiteIT.java | 2 +- ...eCloudStorageBlobStoreRepositoryTests.java | 143 +- .../GoogleCloudStorageThirdPartyTests.java | 20 +- .../gcs/GoogleCloudStorageBlobContainer.java | 6 +- .../gcs/GoogleCloudStorageBlobStore.java | 122 +- .../gcs/GoogleCloudStorageClientSettings.java | 67 +- .../GoogleCloudStorageHttpStatsCollector.java | 50 +- .../gcs/GoogleCloudStoragePlugin.java | 29 +- .../gcs/GoogleCloudStorageRepository.java | 38 +- ...GoogleCloudStorageRetryingInputStream.java | 54 +- .../gcs/GoogleCloudStorageService.java | 62 +- ...CloudStorageBlobContainerRetriesTests.java | 52 +- ...leCloudStorageBlobStoreContainerTests.java | 13 +- ...GoogleCloudStorageClientSettingsTests.java | 84 +- .../gcs/GoogleCloudStorageServiceTests.java | 93 +- .../RepositoryGcsClientYamlTestSuiteIT.java | 1 - .../repositories/hdfs/HdfsBlobContainer.java | 45 +- .../repositories/hdfs/HdfsPlugin.java | 41 +- .../repositories/hdfs/HdfsRepository.java | 78 +- .../hdfs/HdfsSecurityContext.java | 14 +- .../hdfs/HaHdfsFailoverTestSuiteIT.java | 54 +- .../hdfs/HdfsBlobStoreContainerTests.java | 17 +- .../hdfs/HdfsBlobStoreRepositoryTests.java | 4 +- .../hdfs/HdfsClientThreadLeakFilter.java | 3 +- .../hdfs/HdfsRepositoryTests.java | 21 +- .../repositories/hdfs/HdfsTests.java | 85 +- .../repositories/hdfs/TestingFs.java | 8 +- .../s3/S3BlobStoreRepositoryTests.java | 94 +- .../s3/S3RepositoryThirdPartyTests.java | 16 +- .../repositories/s3/AmazonS3Reference.java | 3 +- .../s3/S3BasicSessionCredentials.java | 6 +- .../repositories/s3/S3BlobContainer.java | 243 +- .../repositories/s3/S3BlobStore.java | 20 +- .../repositories/s3/S3ClientSettings.java | 218 +- .../repositories/s3/S3Repository.java | 105 +- .../repositories/s3/S3RepositoryPlugin.java | 23 +- .../s3/S3RetryingInputStream.java | 35 +- .../repositories/s3/S3Service.java | 16 +- .../repositories/s3/AmazonS3Wrapper.java | 165 +- .../s3/AwsS3ServiceImplTests.java | 57 +- .../s3/RepositoryCredentialsTests.java | 30 +- .../s3/S3BlobContainerRetriesTests.java | 269 +- .../s3/S3BlobStoreContainerTests.java | 63 +- .../s3/S3ClientSettingsTests.java | 45 +- .../repositories/s3/S3RepositoryTests.java | 52 +- .../s3/S3RetryingInputStreamTests.java | 10 +- .../repositories/s3/S3ServiceTests.java | 1 - .../s3/RepositoryS3ClientYamlTestSuiteIT.java | 1 - .../index/store/smb/SmbMMapFsTests.java | 6 +- .../index/store/smb/SmbNIOFSTests.java | 6 +- .../index/store/smb/SmbDirectoryWrapper.java | 41 +- .../store/smb/SmbMmapFsDirectoryFactory.java | 9 +- .../plugin/store/smb/SMBStorePlugin.java | 11 +- .../store/smb/SmbMMapDirectoryTests.java | 5 +- .../smb/StoreSmbClientYamlTestSuiteIT.java | 1 - .../http/nio/NioPipeliningIT.java | 3 +- .../transport/nio/NioTransportLoggingIT.java | 66 +- .../elasticsearch/http/nio/ByteBufUtils.java | 1 + .../http/nio/HttpReadWriteHandler.java | 34 +- .../elasticsearch/http/nio/NettyAdaptor.java | 1 + .../elasticsearch/http/nio/NettyListener.java | 1 + .../http/nio/NioHttpChannel.java | 5 +- .../http/nio/NioHttpPipeliningHandler.java | 1 + .../http/nio/NioHttpRequest.java | 81 +- .../http/nio/NioHttpRequestCreator.java | 1 + .../http/nio/NioHttpResponse.java | 1 + .../http/nio/NioHttpResponseCreator.java | 1 + .../http/nio/NioHttpServerTransport.java | 77 +- .../elasticsearch/http/nio/PagedByteBuf.java | 2 +- .../transport/nio/NioGroupFactory.java | 20 +- .../transport/nio/NioTcpChannel.java | 13 +- .../transport/nio/NioTcpServerChannel.java | 4 +- .../transport/nio/NioTransport.java | 43 +- .../transport/nio/NioTransportPlugin.java | 82 +- .../transport/nio/PageAllocator.java | 2 +- .../transport/nio/TcpReadWriteHandler.java | 11 +- .../http/nio/HttpReadWriteHandlerTests.java | 3 +- .../http/nio/NettyAdaptorTests.java | 17 +- .../elasticsearch/http/nio/NioHttpClient.java | 46 +- .../nio/NioHttpPipeliningHandlerTests.java | 22 +- .../http/nio/NioHttpServerTransportTests.java | 183 +- .../http/nio/PagedByteBufTests.java | 1 + .../transport/nio/NioGroupFactoryTests.java | 7 +- .../nio/SimpleNioTransportTests.java | 49 +- .../upgrades/SearchStatesIT.java | 88 +- ...rossClusterSearchUnavailableClusterIT.java | 155 +- .../bootstrap/ESPolicyUnitTests.java | 19 +- .../bootstrap/EvilBootstrapChecksTests.java | 16 +- .../bootstrap/EvilElasticsearchCliTests.java | 45 +- .../bootstrap/EvilJNANativesTests.java | 21 +- .../bootstrap/EvilSecurityTests.java | 21 +- .../bootstrap/PolicyUtilTests.java | 22 +- .../elasticsearch/cli/EvilCommandTests.java | 1 + .../cli/EvilEnvironmentAwareCommandTests.java | 9 +- .../metadata/EvilSystemPropertyTests.java | 16 +- .../logging/EvilLoggerConfigurationTests.java | 12 +- .../common/logging/EvilLoggerTests.java | 104 +- .../settings/EvilKeyStoreWrapperTests.java | 3 +- .../env/NodeEnvironmentEvilTests.java | 65 +- .../index/engine/EvilInternalEngineTests.java | 86 +- .../monitor/os/EvilOsProbeTests.java | 5 +- .../plugins/cli/PluginSecurityTests.java | 23 +- .../threadpool/EvilThreadPoolTests.java | 95 +- .../upgrades/FullClusterRestartIT.java | 249 +- .../upgrades/QueryBuilderBWCIT.java | 77 +- .../common/logging/ESJsonLayoutTests.java | 69 +- .../common/logging/JsonLoggerTests.java | 403 +- .../elasticsearch/backwards/HotThreadsIT.java | 6 +- .../elasticsearch/backwards/IndexingIT.java | 181 +- .../MixedClusterClientYamlTestSuiteIT.java | 2 +- .../elasticsearch/backwards/RareTermsIT.java | 2 +- .../SearchWithMinCompatibleSearchNodeIT.java | 92 +- .../org/elasticsearch/search/CCSDuelIT.java | 128 +- .../MultiClusterSearchYamlTestSuiteIT.java | 1 + .../bootstrap/SpawnerNoBootstrapTests.java | 113 +- .../AbstractMultiClusterRemoteTestCase.java | 18 +- .../cluster/remote/test/RemoteClustersIT.java | 123 +- .../MultiVersionRepositoryAccessIT.java | 114 +- .../upgrades/AbstractRollingTestCase.java | 3 +- .../upgrades/FeatureUpgradeIT.java | 27 +- .../elasticsearch/upgrades/IndexingIT.java | 119 +- .../elasticsearch/upgrades/RecoveryIT.java | 178 +- .../upgrades/SnapshotBasedRecoveryIT.java | 19 +- .../upgrades/SystemIndicesUpgradeIT.java | 43 +- .../UpgradeClusterClientYamlTestSuiteIT.java | 4 +- .../org/elasticsearch/upgrades/XPackIT.java | 28 +- .../elasticsearch/http/AutoCreateIndexIT.java | 3 +- ...ockedSearcherRestCancellationTestCase.java | 11 +- .../http/ClusterStateRestCancellationIT.java | 6 +- .../http/ClusterStatsRestCancellationIT.java | 12 +- .../org/elasticsearch/http/CorsRegexIT.java | 16 +- .../http/DetailedErrorsDisabledIT.java | 19 +- .../http/DetailedErrorsEnabledIT.java | 10 +- .../elasticsearch/http/HttpCompressionIT.java | 16 +- .../elasticsearch/http/HttpSmokeTestCase.java | 7 +- .../http/IndexingPressureRestIT.java | 14 +- .../http/IndicesStatsRestCancellationIT.java | 8 +- .../org/elasticsearch/http/NoHandlerIT.java | 21 +- .../http/ResponseHeaderPluginIT.java | 2 +- .../http/RestGetMappingsCancellationIT.java | 11 +- .../http/RestHttpResponseHeadersIT.java | 21 +- .../http/SearchRestCancellationIT.java | 37 +- .../elasticsearch/http/SystemIndexRestIT.java | 71 +- .../http/TestResponseHeaderPlugin.java | 12 +- .../AbstractSnapshotRestTestCase.java | 6 +- .../http/snapshots/RestGetSnapshotsIT.java | 205 +- .../RestSnapshotsStatusCancellationIT.java | 4 +- .../ingest/IngestDocumentMustacheIT.java | 21 +- .../ingest/ValueSourceMustacheIT.java | 14 +- ...okeTestMultiNodeClientYamlTestSuiteIT.java | 3 +- ...SmokeTestPluginsClientYamlTestSuiteIT.java | 1 - .../VerifyVersionConstantsIT.java | 4 +- .../test/rest/ClientYamlTestSuiteIT.java | 3 +- .../java/fixture/geoip/GeoIpHttpFixture.java | 16 +- .../src/main/java/oldes/OldElasticsearch.java | 32 +- .../loggerusage/ESLoggerUsageChecker.java | 364 +- .../test/loggerusage/ESLoggerUsageTests.java | 31 +- .../smoketest/XDocsClientYamlTestSuiteIT.java | 27 +- .../license/licensor/LicenseSigner.java | 35 +- .../licensor/tools/KeyPairGeneratorTool.java | 17 +- .../licensor/tools/LicenseGeneratorTool.java | 29 +- .../tools/LicenseVerificationTool.java | 26 +- .../licensor/LicenseVerificationTests.java | 31 +- .../license/licensor/TestUtils.java | 137 +- .../tools/KeyPairGenerationToolTests.java | 23 +- .../tools/LicenseGenerationToolTests.java | 64 +- .../tools/LicenseVerificationToolTests.java | 40 +- .../xpack/search/AsyncSearchSecurityIT.java | 138 +- .../xpack/search/AsyncSearchActionIT.java | 92 +- .../search/AsyncSearchIntegTestCase.java | 86 +- .../xpack/search/BlockingQueryBuilder.java | 2 +- .../xpack/search/AsyncSearch.java | 13 +- .../xpack/search/AsyncSearchTask.java | 111 +- .../xpack/search/MutableSearchResponse.java | 88 +- .../search/RestSubmitAsyncSearchAction.java | 14 +- .../search/TransportGetAsyncSearchAction.java | 60 +- .../search/TransportGetAsyncStatusAction.java | 36 +- .../TransportSubmitAsyncSearchAction.java | 175 +- .../search/AsyncSearchResponseTests.java | 112 +- .../xpack/search/AsyncSearchTaskTests.java | 199 +- .../search/AsyncStatusResponseTests.java | 59 +- .../search/CancellingAggregationBuilder.java | 24 +- .../RestSubmitAsyncSearchActionTests.java | 85 +- .../search/SubmitAsyncSearchRequestTests.java | 16 +- .../xpack/search/ThrowingQueryBuilder.java | 2 +- .../xpack/autoscaling/AutoscalingRestIT.java | 1 + .../xpack/ccr/FollowIndexIT.java | 31 +- .../elasticsearch/xpack/ccr/AutoFollowIT.java | 173 +- .../org/elasticsearch/xpack/ccr/ChainIT.java | 10 +- .../xpack/ccr/FollowIndexIT.java | 71 +- .../elasticsearch/xpack/ccr/XPackUsageIT.java | 8 +- .../xpack/ccr/CcrMultiClusterLicenseIT.java | 15 +- .../elasticsearch/xpack/ccr/CcrRestIT.java | 1 + .../elasticsearch/xpack/ccr/RestartIT.java | 25 +- .../xpack/ccr/FollowIndexSecurityIT.java | 65 +- .../xpack/ccr/ESCCRRestTestCase.java | 55 +- .../elasticsearch/xpack/ccr/AutoFollowIT.java | 133 +- .../elasticsearch/xpack/ccr/CcrAliasesIT.java | 125 +- .../xpack/ccr/CcrDisabledIT.java | 7 +- .../elasticsearch/xpack/ccr/CcrLicenseIT.java | 135 +- .../xpack/ccr/CcrRepositoryIT.java | 141 +- .../xpack/ccr/CcrRetentionLeaseIT.java | 804 +- .../xpack/ccr/CloseFollowerIndexIT.java | 2 +- .../elasticsearch/xpack/ccr/FollowInfoIT.java | 2 +- .../xpack/ccr/FollowStatsIT.java | 76 +- .../xpack/ccr/FollowerFailOverIT.java | 57 +- .../xpack/ccr/IndexFollowingIT.java | 691 +- .../xpack/ccr/LocalIndexFollowingIT.java | 64 +- .../ccr/PrimaryFollowerAllocationIT.java | 97 +- .../xpack/ccr/RestartIndexFollowingIT.java | 29 +- .../xpack/ccr/CCRInfoTransportAction.java | 26 +- .../xpack/ccr/CCRUsageTransportAction.java | 32 +- .../java/org/elasticsearch/xpack/ccr/Ccr.java | 249 +- .../xpack/ccr/CcrLicenseChecker.java | 248 +- .../xpack/ccr/CcrRepositoryManager.java | 6 +- .../xpack/ccr/CcrRetentionLeases.java | 107 +- .../elasticsearch/xpack/ccr/CcrSettings.java | 90 +- .../ccr/action/AutoFollowCoordinator.java | 291 +- .../xpack/ccr/action/CcrRequests.java | 148 +- .../xpack/ccr/action/ShardChangesAction.java | 322 +- .../xpack/ccr/action/ShardFollowNodeTask.java | 405 +- .../ccr/action/ShardFollowTaskCleaner.java | 8 +- .../ccr/action/ShardFollowTasksExecutor.java | 358 +- ...nsportActivateAutoFollowPatternAction.java | 59 +- .../ccr/action/TransportCcrStatsAction.java | 21 +- ...ransportDeleteAutoFollowPatternAction.java | 52 +- .../ccr/action/TransportFollowInfoAction.java | 39 +- .../action/TransportFollowStatsAction.java | 62 +- .../action/TransportForgetFollowerAction.java | 75 +- .../TransportGetAutoFollowPatternAction.java | 40 +- .../action/TransportPauseFollowAction.java | 36 +- .../TransportPutAutoFollowPatternAction.java | 87 +- .../ccr/action/TransportPutFollowAction.java | 176 +- .../action/TransportResumeFollowAction.java | 318 +- .../ccr/action/TransportUnfollowAction.java | 169 +- .../bulk/BulkShardOperationsRequest.java | 39 +- .../bulk/BulkShardOperationsResponse.java | 6 +- .../TransportBulkShardOperationsAction.java | 130 +- .../ClearCcrRestoreSessionAction.java | 15 +- .../ClearCcrRestoreSessionRequest.java | 2 +- .../DeleteInternalCcrRepositoryAction.java | 15 +- .../DeleteInternalCcrRepositoryRequest.java | 4 +- .../GetCcrRestoreFileChunkAction.java | 20 +- .../PutCcrRestoreSessionAction.java | 32 +- .../PutInternalCcrRepositoryAction.java | 15 +- .../PutInternalCcrRepositoryRequest.java | 8 +- .../CcrPrimaryFollowerAllocationDecider.java | 23 +- .../ccr/index/engine/FollowingEngine.java | 34 +- .../engine/FollowingEngineAssertions.java | 4 +- .../xpack/ccr/repository/CcrRepository.java | 429 +- .../repository/CcrRestoreSourceService.java | 16 +- .../ccr/rest/RestForgetFollowerAction.java | 2 +- .../rest/RestGetAutoFollowPatternAction.java | 4 +- .../rest/RestPutAutoFollowPatternAction.java | 2 +- .../xpack/ccr/rest/RestPutFollowAction.java | 2 +- .../ccr/rest/RestResumeFollowAction.java | 2 +- .../elasticsearch/xpack/CcrIntegTestCase.java | 302 +- .../xpack/CcrSingleNodeTestCase.java | 6 +- .../xpack/ccr/AutoFollowMetadataTests.java | 5 +- .../xpack/ccr/CCRFeatureSetUsageTests.java | 9 +- .../ccr/CCRInfoTransportActionTests.java | 29 +- .../xpack/ccr/CcrLicenseCheckerTests.java | 13 +- .../xpack/ccr/CcrSettingsTests.java | 32 +- .../org/elasticsearch/xpack/ccr/CcrTests.java | 14 +- .../xpack/ccr/LocalStateCcr.java | 1 - .../action/AutoFollowCoordinatorTests.java | 1264 +- .../action/AutoFollowStatsResponseTests.java | 2 +- .../ccr/action/AutoFollowStatsTests.java | 19 +- .../ccr/action/FollowInfoResponseTests.java | 50 +- .../ccr/action/FollowParametersTests.java | 2 +- .../GetAutoFollowPatternResponseTests.java | 3 +- .../PutAutoFollowPatternRequestTests.java | 2 +- .../action/PutFollowActionRequestTests.java | 7 +- .../ResumeFollowActionRequestTests.java | 2 +- .../ccr/action/ShardChangesActionTests.java | 204 +- .../ccr/action/ShardChangesRequestTests.java | 3 +- .../xpack/ccr/action/ShardChangesTests.java | 79 +- .../ShardFollowNodeTaskRandomTests.java | 108 +- .../ShardFollowNodeTaskStatusTests.java | 78 +- .../ccr/action/ShardFollowNodeTaskTests.java | 103 +- .../ShardFollowTaskReplicationTests.java | 322 +- ...ardFollowTasksExecutorAssignmentTests.java | 33 +- .../xpack/ccr/action/StatsResponsesTests.java | 3 +- ...tActivateAutoFollowPatternActionTests.java | 50 +- ...ortDeleteAutoFollowPatternActionTests.java | 39 +- .../TransportFollowInfoActionTests.java | 18 +- .../TransportFollowStatsActionTests.java | 26 +- ...nsportGetAutoFollowPatternActionTests.java | 23 +- ...nsportPutAutoFollowPatternActionTests.java | 75 +- .../action/TransportPutFollowActionTests.java | 4 +- .../TransportResumeFollowActionTests.java | 272 +- .../action/TransportUnfollowActionTests.java | 51 +- .../action/bulk/BulkShardOperationsTests.java | 52 +- ...PrimaryFollowerAllocationDeciderTests.java | 64 +- .../engine/FollowEngineIndexShardTests.java | 96 +- .../index/engine/FollowingEngineTests.java | 405 +- .../CcrRepositoryRetentionLeaseTests.java | 167 +- .../CcrRestoreSourceServiceTests.java | 36 +- .../AutoFollowStatsMonitoringDocTests.java | 193 +- .../ccr/FollowStatsMonitoringDocTests.java | 307 +- .../collector/ccr/StatsCollectorTests.java | 16 +- .../sourceonly/SourceOnlySnapshotIT.java | 158 +- .../DataTierAllocationDeciderIT.java | 139 +- .../allocation/DataTierTelemetryPlugin.java | 20 +- .../rest/action/ReloadSynonymAnalyzerIT.java | 43 +- .../action/XPackUsageRestCancellationIT.java | 44 +- .../xpack/core/termsenum/CCSTermsEnumIT.java | 14 +- .../index/engine/frozen/FrozenEngine.java | 64 +- .../frozen/RewriteCachingDirectoryReader.java | 10 +- .../org/elasticsearch/license/CryptUtils.java | 57 +- .../license/DeleteLicenseRequestBuilder.java | 6 +- .../license/ExpirationCallback.java | 16 +- .../license/GetBasicStatusRequest.java | 3 +- .../license/GetFeatureUsageResponse.java | 11 +- .../license/GetLicenseRequestBuilder.java | 6 +- .../license/GetTrialStatusRequest.java | 3 +- .../org/elasticsearch/license/License.java | 80 +- .../elasticsearch/license/LicenseService.java | 200 +- .../elasticsearch/license/LicenseUtils.java | 17 +- .../license/LicenseVerifier.java | 4 +- .../LicensedAllocatedPersistentTask.java | 22 +- .../license/LicensesMetadata.java | 35 +- .../org/elasticsearch/license/Licensing.java | 40 +- .../license/OperationModeFileWatcher.java | 24 +- .../license/PostStartBasicResponse.java | 10 +- .../license/PostStartTrialResponse.java | 3 +- .../license/PutLicenseRequest.java | 4 +- .../license/PutLicenseRequestBuilder.java | 2 +- .../license/RemoteClusterLicenseChecker.java | 38 +- .../license/RestDeleteLicenseAction.java | 10 +- .../license/RestGetBasicStatus.java | 3 +- .../license/RestGetFeatureUsageAction.java | 7 +- .../license/RestGetLicenseAction.java | 56 +- .../license/RestGetTrialStatus.java | 3 +- .../license/RestPostStartBasicLicense.java | 3 +- .../license/RestPostStartTrialLicense.java | 62 +- .../license/RestPutLicenseAction.java | 12 +- .../license/SelfGeneratedLicense.java | 32 +- .../license/StartBasicClusterTask.java | 20 +- .../license/StartTrialClusterTask.java | 38 +- .../StartupSelfGeneratedLicenseTask.java | 58 +- .../license/TransportDeleteLicenseAction.java | 39 +- .../TransportGetBasicStatusAction.java | 31 +- .../TransportGetFeatureUsageAction.java | 10 +- .../license/TransportGetLicenseAction.java | 32 +- .../TransportGetTrialStatusAction.java | 30 +- .../TransportPostStartBasicAction.java | 32 +- .../TransportPostStartTrialAction.java | 32 +- .../license/TransportPutLicenseAction.java | 32 +- .../license/XPackLicenseState.java | 129 +- .../protocol/xpack/XPackInfoRequest.java | 7 +- .../protocol/xpack/XPackInfoResponse.java | 53 +- .../protocol/xpack/XPackUsageResponse.java | 7 +- .../protocol/xpack/frozen/FreezeRequest.java | 3 +- .../protocol/xpack/graph/Connection.java | 47 +- .../xpack/graph/GraphExploreRequest.java | 10 +- .../xpack/graph/GraphExploreResponse.java | 82 +- .../protocol/xpack/graph/Hop.java | 8 +- .../protocol/xpack/graph/Vertex.java | 52 +- .../protocol/xpack/graph/VertexRequest.java | 8 +- .../xpack/license/DeleteLicenseRequest.java | 1 - .../xpack/license/GetLicenseRequest.java | 4 +- .../protocol/xpack/license/LicenseStatus.java | 4 +- .../xpack/license/LicensesStatus.java | 1 - .../xpack/license/PutLicenseResponse.java | 16 +- .../xpack/watcher/DeleteWatchRequest.java | 2 +- .../xpack/watcher/DeleteWatchResponse.java | 14 +- .../xpack/watcher/PutWatchRequest.java | 13 +- .../xpack/watcher/PutWatchResponse.java | 26 +- .../SeqIdGeneratingFilterReader.java | 2 +- .../sourceonly/SourceOnlySnapshot.java | 83 +- .../SourceOnlySnapshotRepository.java | 72 +- .../action/MigrateToDataTiersRequest.java | 14 +- .../action/MigrateToDataTiersResponse.java | 16 +- .../allocation/DataTierAllocationDecider.java | 43 +- .../mapper/DataTierFieldMapper.java | 4 +- .../xpack/core/ClientHelper.java | 65 +- .../xpack/core/DataTiersFeatureSetUsage.java | 67 +- .../core/DataTiersUsageTransportAction.java | 83 +- .../xpack/core/MigrateToDataStreamAction.java | 2 +- .../elasticsearch/xpack/core/XPackBuild.java | 2 +- .../xpack/core/XPackClientPlugin.java | 648 +- .../elasticsearch/xpack/core/XPackPlugin.java | 147 +- .../xpack/core/XPackSettings.java | 183 +- .../action/AbstractGetResourcesRequest.java | 9 +- .../action/AbstractGetResourcesResponse.java | 5 +- .../AbstractTransportGetResourcesAction.java | 120 +- .../AbstractTransportSetResetModeAction.java | 56 +- .../core/action/CreateDataStreamAction.java | 5 +- .../core/action/DataStreamsStatsAction.java | 63 +- .../core/action/DeleteDataStreamAction.java | 5 +- .../core/action/GetDataStreamAction.java | 33 +- .../core/action/PromoteDataStreamAction.java | 2 +- .../core/action/ReloadAnalyzersRequest.java | 3 +- .../core/action/ReloadAnalyzersResponse.java | 54 +- .../action/SetResetModeActionRequest.java | 12 +- .../TransportReloadAnalyzersAction.java | 50 +- .../core/action/TransportXPackInfoAction.java | 25 +- .../action/TransportXPackUsageAction.java | 44 +- .../core/action/XPackInfoFeatureAction.java | 31 +- .../core/action/XPackInfoFeatureResponse.java | 2 +- .../core/action/XPackInfoRequestBuilder.java | 1 - .../XPackUsageFeatureTransportAction.java | 24 +- .../core/action/XPackUsageRequestBuilder.java | 6 +- .../xpack/core/action/XPackUsageResponse.java | 3 +- .../core/action/util/ExpandedIdsMatcher.java | 3 +- .../xpack/core/action/util/PageParams.java | 11 +- .../xpack/core/action/util/QueryPage.java | 5 +- .../AggregateMetricFeatureSetUsage.java | 6 +- .../action/AnalyticsStatsAction.java | 8 +- .../xpack/core/async/AsyncExecutionId.java | 8 +- .../xpack/core/async/AsyncResultsService.java | 83 +- .../core/async/AsyncTaskIndexService.java | 303 +- .../async/AsyncTaskMaintenanceService.java | 30 +- .../core/async/DeleteAsyncResultsService.java | 81 +- .../core/async/GetAsyncResultRequest.java | 6 +- .../xpack/core/async/StoredAsyncResponse.java | 19 +- .../xpack/core/async/StoredAsyncTask.java | 15 +- .../TransportDeleteAsyncResultAction.java | 38 +- .../xpack/core/ccr/AutoFollowMetadata.java | 151 +- .../xpack/core/ccr/AutoFollowStats.java | 104 +- .../xpack/core/ccr/CcrConstants.java | 3 +- .../core/ccr/ShardFollowNodeTaskStatus.java | 349 +- .../ActivateAutoFollowPatternAction.java | 3 +- .../xpack/core/ccr/action/CcrStatsAction.java | 6 +- .../core/ccr/action/FollowInfoAction.java | 26 +- .../core/ccr/action/FollowParameters.java | 60 +- .../core/ccr/action/FollowStatsAction.java | 16 +- .../core/ccr/action/ForgetFollowerAction.java | 19 +- .../action/GetAutoFollowPatternAction.java | 3 +- .../ccr/action/ImmutableFollowParameters.java | 60 +- .../action/PutAutoFollowPatternAction.java | 39 +- .../core/ccr/action/PutFollowAction.java | 36 +- .../core/ccr/action/ResumeFollowAction.java | 6 +- .../core/ccr/action/ShardFollowTask.java | 89 +- .../xpack/core/ccr/action/UnfollowAction.java | 2 +- .../core/common/IteratingActionListener.java | 28 +- .../notifications/AbstractAuditMessage.java | 28 +- .../common/notifications/AbstractAuditor.java | 118 +- .../core/common/notifications/Level.java | 4 +- .../search/aggregations/MissingHelper.java | 2 +- .../core/common/socket/SocketAccess.java | 5 +- .../xpack/core/common/stats/EnumCounters.java | 3 +- .../xpack/core/common/time/TimeUtils.java | 23 +- .../validation/SourceDestValidator.java | 32 +- .../DataStreamFeatureSetUsage.java | 3 +- .../core/deprecation/DeprecationIssue.java | 33 +- ...LoggingDeprecationAccumulationHandler.java | 20 +- .../xpack/core/enrich/EnrichPolicy.java | 71 +- .../core/enrich/action/EnrichStatsAction.java | 43 +- .../action/ExecuteEnrichPolicyAction.java | 5 +- .../action/ExecuteEnrichPolicyStatus.java | 6 +- .../enrich/action/GetEnrichPolicyAction.java | 6 +- .../enrich/action/PutEnrichPolicyAction.java | 8 +- .../frozen/FrozenIndicesFeatureSetUsage.java | 6 +- .../action/GraphExploreRequestBuilder.java | 2 - .../core/ilm/AbstractUnfollowIndexStep.java | 8 +- .../xpack/core/ilm/AllocateAction.java | 55 +- .../xpack/core/ilm/AllocationRoutedStep.java | 45 +- .../xpack/core/ilm/AsyncActionStep.java | 8 +- .../AsyncRetryDuringSnapshotActionStep.java | 130 +- .../xpack/core/ilm/AsyncWaitStep.java | 2 +- .../xpack/core/ilm/BranchingStep.java | 6 +- .../ilm/CheckNotDataStreamWriteIndexStep.java | 22 +- .../xpack/core/ilm/CheckShrinkReadyStep.java | 67 +- .../core/ilm/CheckTargetShardsCountStep.java | 15 +- .../core/ilm/CleanupShrinkIndexStep.java | 19 +- .../xpack/core/ilm/CleanupSnapshotStep.java | 69 +- .../core/ilm/CloseFollowerIndexStep.java | 18 +- .../xpack/core/ilm/CloseIndexStep.java | 24 +- .../xpack/core/ilm/ClusterStateWaitStep.java | 2 +- .../ClusterStateWaitUntilThresholdStep.java | 28 +- .../core/ilm/CopyExecutionStateStep.java | 30 +- .../xpack/core/ilm/CopySettingsStep.java | 18 +- .../xpack/core/ilm/CreateSnapshotStep.java | 58 +- .../core/ilm/DataTierMigrationRoutedStep.java | 79 +- .../xpack/core/ilm/DeleteAction.java | 8 +- .../xpack/core/ilm/DeleteStep.java | 31 +- .../core/ilm/ExplainLifecycleRequest.java | 19 +- .../core/ilm/ExplainLifecycleResponse.java | 17 +- .../xpack/core/ilm/ForceMergeAction.java | 65 +- .../xpack/core/ilm/ForceMergeStep.java | 56 +- .../xpack/core/ilm/FreezeAction.java | 37 +- .../core/ilm/GenerateSnapshotNameStep.java | 40 +- .../core/ilm/GenerateUniqueIndexNameStep.java | 18 +- .../ilm/IndexLifecycleExplainResponse.java | 243 +- .../ilm/IndexLifecycleFeatureSetUsage.java | 89 +- .../core/ilm/IndexLifecycleMetadata.java | 46 +- .../IndexLifecycleOriginationDateParser.java | 15 +- .../core/ilm/InitializePolicyContextStep.java | 13 +- .../xpack/core/ilm/LifecycleAction.java | 4 +- .../core/ilm/LifecycleExecutionState.java | 141 +- .../xpack/core/ilm/LifecyclePolicy.java | 55 +- .../core/ilm/LifecyclePolicyMetadata.java | 26 +- .../xpack/core/ilm/LifecyclePolicyUtils.java | 58 +- .../xpack/core/ilm/LifecycleSettings.java | 120 +- .../xpack/core/ilm/LifecycleType.java | 1 - .../xpack/core/ilm/MigrateAction.java | 35 +- .../xpack/core/ilm/MountSnapshotStep.java | 85 +- .../xpack/core/ilm/OpenIndexStep.java | 22 +- .../core/ilm/PauseFollowerIndexStep.java | 18 +- .../elasticsearch/xpack/core/ilm/Phase.java | 65 +- .../xpack/core/ilm/PhaseCacheManagement.java | 156 +- .../xpack/core/ilm/PhaseExecutionInfo.java | 18 +- .../xpack/core/ilm/ReadOnlyAction.java | 12 +- .../xpack/core/ilm/ReadOnlyStep.java | 28 +- .../ReplaceDataStreamBackingIndexStep.java | 40 +- .../xpack/core/ilm/RolloverAction.java | 80 +- .../xpack/core/ilm/RolloverStep.java | 69 +- .../xpack/core/ilm/RollupILMAction.java | 40 +- .../xpack/core/ilm/RollupStep.java | 25 +- .../core/ilm/SearchableSnapshotAction.java | 181 +- .../xpack/core/ilm/SegmentCountStep.java | 78 +- .../xpack/core/ilm/SetPriorityAction.java | 30 +- .../core/ilm/SetSingleNodeAllocateStep.java | 62 +- .../xpack/core/ilm/ShrinkAction.java | 121 +- .../core/ilm/ShrinkIndexNameSupplier.java | 3 +- .../xpack/core/ilm/ShrinkStep.java | 36 +- .../core/ilm/ShrunkShardsAllocatedStep.java | 16 +- .../core/ilm/ShrunkenIndexCheckStep.java | 16 +- .../xpack/core/ilm/StartILMRequest.java | 3 +- .../elasticsearch/xpack/core/ilm/Step.java | 15 +- .../xpack/core/ilm/StopILMRequest.java | 3 +- .../SwapAliasesAndDeleteSourceIndexStep.java | 59 +- .../core/ilm/TimeseriesLifecycleType.java | 195 +- .../xpack/core/ilm/UnfollowAction.java | 8 +- .../core/ilm/UnfollowFollowerIndexStep.java | 36 +- .../ilm/UpdateRolloverLifecycleDateStep.java | 28 +- .../core/ilm/UpdateRollupIndexPolicyStep.java | 22 +- .../xpack/core/ilm/UpdateSettingsStep.java | 22 +- .../core/ilm/WaitForActiveShardsStep.java | 56 +- .../xpack/core/ilm/WaitForDataTierStep.java | 4 +- .../core/ilm/WaitForFollowShardTasksStep.java | 35 +- .../xpack/core/ilm/WaitForIndexColorStep.java | 13 +- .../core/ilm/WaitForIndexingCompleteStep.java | 7 +- .../core/ilm/WaitForNoFollowersStep.java | 10 +- .../core/ilm/WaitForRolloverReadyStep.java | 108 +- .../xpack/core/ilm/WaitForSnapshotAction.java | 8 +- .../xpack/core/ilm/WaitForSnapshotStep.java | 23 +- .../ilm/action/DeleteLifecycleAction.java | 5 +- .../core/ilm/action/GetLifecycleAction.java | 8 +- .../core/ilm/action/GetStatusAction.java | 3 +- .../core/ilm/action/MoveToStepAction.java | 47 +- .../core/ilm/action/PutLifecycleAction.java | 11 +- .../RemoveIndexLifecyclePolicyAction.java | 14 +- .../xpack/core/ilm/action/RetryAction.java | 6 +- .../core/ilm/step/info/AllocationInfo.java | 27 +- .../core/index/query/PinnedQueryBuilder.java | 35 +- .../core/indexing/AsyncTwoPhaseIndexer.java | 208 +- .../xpack/core/indexing/IndexerJobStats.java | 48 +- .../xpack/core/indexing/IndexerState.java | 2 +- .../ml/MachineLearningFeatureSetUsage.java | 16 +- .../xpack/core/ml/MachineLearningField.java | 16 +- .../xpack/core/ml/MlConfigIndex.java | 3 +- .../xpack/core/ml/MlMetaIndex.java | 3 +- .../xpack/core/ml/MlMetadata.java | 62 +- .../xpack/core/ml/MlStatsIndex.java | 16 +- .../elasticsearch/xpack/core/ml/MlTasks.java | 92 +- .../xpack/core/ml/action/CloseJobAction.java | 29 +- .../CreateTrainedModelAllocationAction.java | 1 + .../core/ml/action/DeleteCalendarAction.java | 1 - .../DeleteDataFrameAnalyticsAction.java | 6 +- .../core/ml/action/DeleteDatafeedAction.java | 4 +- .../ml/action/DeleteExpiredDataAction.java | 16 +- .../core/ml/action/DeleteFilterAction.java | 4 +- .../action/DeleteTrainedModelAliasAction.java | 6 +- .../ml/action/EstimateModelMemoryAction.java | 32 +- .../ml/action/EvaluateDataFrameAction.java | 21 +- .../ExplainDataFrameAnalyticsAction.java | 15 +- .../xpack/core/ml/action/FlushJobAction.java | 35 +- .../core/ml/action/ForecastJobAction.java | 36 +- .../core/ml/action/GetBucketsAction.java | 72 +- .../ml/action/GetCalendarEventsAction.java | 26 +- .../core/ml/action/GetCalendarsAction.java | 21 +- .../core/ml/action/GetCategoriesAction.java | 65 +- .../action/GetDataFrameAnalyticsAction.java | 2 +- .../GetDataFrameAnalyticsStatsAction.java | 67 +- .../action/GetDatafeedRunningStateAction.java | 13 +- .../ml/action/GetDatafeedsStatsAction.java | 15 +- .../ml/action/GetDeploymentStatsAction.java | 138 +- .../core/ml/action/GetFiltersAction.java | 2 - .../core/ml/action/GetInfluencersAction.java | 23 +- .../xpack/core/ml/action/GetJobsAction.java | 1 - .../core/ml/action/GetJobsStatsAction.java | 51 +- .../ml/action/GetModelSnapshotsAction.java | 17 +- .../ml/action/GetOverallBucketsAction.java | 32 +- .../core/ml/action/GetRecordsAction.java | 22 +- .../ml/action/GetTrainedModelsAction.java | 9 +- .../action/GetTrainedModelsStatsAction.java | 25 +- .../InferTrainedModelDeploymentAction.java | 9 +- .../ml/action/InternalInferModelAction.java | 23 +- .../core/ml/action/IsolateDatafeedAction.java | 7 +- .../xpack/core/ml/action/JobTaskRequest.java | 3 +- .../ml/action/NodeAcknowledgedResponse.java | 3 +- .../xpack/core/ml/action/OpenJobAction.java | 20 +- .../ml/action/PostCalendarEventsAction.java | 7 +- .../xpack/core/ml/action/PostDataAction.java | 21 +- .../PreviewDataFrameAnalyticsAction.java | 17 +- .../core/ml/action/PreviewDatafeedAction.java | 15 +- .../core/ml/action/PutCalendarAction.java | 23 +- .../action/PutDataFrameAnalyticsAction.java | 43 +- .../xpack/core/ml/action/PutFilterAction.java | 9 +- .../xpack/core/ml/action/PutJobAction.java | 10 +- .../core/ml/action/PutTrainedModelAction.java | 22 +- .../ml/action/PutTrainedModelAliasAction.java | 9 +- .../PutTrainedModelVocabularyAction.java | 8 +- .../ml/action/RevertModelSnapshotAction.java | 16 +- .../core/ml/action/SetResetModeAction.java | 1 - .../core/ml/action/SetUpgradeModeAction.java | 8 +- .../action/StartDataFrameAnalyticsAction.java | 17 +- .../core/ml/action/StartDatafeedAction.java | 53 +- .../StartTrainedModelDeploymentAction.java | 9 +- .../action/StopDataFrameAnalyticsAction.java | 20 +- .../core/ml/action/StopDatafeedAction.java | 25 +- .../StopTrainedModelDeploymentAction.java | 8 +- .../ml/action/UpdateCalendarJobAction.java | 6 +- .../UpdateDataFrameAnalyticsAction.java | 3 +- .../core/ml/action/UpdateDatafeedAction.java | 2 +- .../core/ml/action/UpdateFilterAction.java | 19 +- .../xpack/core/ml/action/UpdateJobAction.java | 4 +- .../ml/action/UpdateModelSnapshotAction.java | 15 +- .../core/ml/action/UpdateProcessAction.java | 34 +- .../action/UpgradeJobModelSnapshotAction.java | 25 +- .../ml/action/ValidateJobConfigAction.java | 5 +- .../xpack/core/ml/annotations/Annotation.java | 139 +- .../core/ml/annotations/AnnotationIndex.java | 112 +- .../xpack/core/ml/calendars/Calendar.java | 4 +- .../core/ml/calendars/ScheduledEvent.java | 41 +- .../xpack/core/ml/datafeed/AggProvider.java | 54 +- .../core/ml/datafeed/ChunkingConfig.java | 21 +- .../core/ml/datafeed/DatafeedConfig.java | 204 +- .../ml/datafeed/DatafeedJobValidator.java | 54 +- .../xpack/core/ml/datafeed/DatafeedState.java | 15 +- .../core/ml/datafeed/DatafeedTimingStats.java | 56 +- .../core/ml/datafeed/DatafeedUpdate.java | 184 +- .../ml/datafeed/DelayedDataCheckConfig.java | 12 +- .../ml/datafeed/extractor/ExtractorUtils.java | 31 +- .../dataframe/DataFrameAnalyticsConfig.java | 100 +- .../DataFrameAnalyticsConfigUpdate.java | 29 +- .../ml/dataframe/DataFrameAnalyticsDest.java | 11 +- .../dataframe/DataFrameAnalyticsSource.java | 62 +- .../ml/dataframe/DataFrameAnalyticsState.java | 8 +- .../DataFrameAnalyticsTaskState.java | 18 +- .../dataframe/analyses/BoostedTreeParams.java | 87 +- .../ml/dataframe/analyses/Classification.java | 129 +- .../dataframe/analyses/DataFrameAnalysis.java | 3 +- .../analyses/FieldCardinalityConstraint.java | 12 +- ...ataFrameAnalysisNamedXContentProvider.java | 26 +- .../dataframe/analyses/OutlierDetection.java | 51 +- .../ml/dataframe/analyses/Regression.java | 98 +- .../core/ml/dataframe/analyses/Types.java | 28 +- .../ml/dataframe/evaluation/Evaluation.java | 49 +- .../evaluation/EvaluationFields.java | 27 +- .../evaluation/EvaluationMetric.java | 4 +- .../MlEvaluationNamedXContentProvider.java | 257 +- .../evaluation/classification/Accuracy.java | 40 +- .../evaluation/classification/AucRoc.java | 87 +- .../classification/Classification.java | 64 +- .../MulticlassConfusionMatrix.java | 108 +- .../classification/PainlessScripts.java | 8 +- .../classification/PerClassSingleValue.java | 12 +- .../evaluation/classification/Precision.java | 66 +- .../evaluation/classification/Recall.java | 50 +- .../evaluation/common/AbstractAucRoc.java | 20 +- .../AbstractConfusionMatrixMetric.java | 21 +- .../evaluation/outlierdetection/AucRoc.java | 64 +- .../outlierdetection/ConfusionMatrix.java | 14 +- .../outlierdetection/OutlierDetection.java | 45 +- .../outlierdetection/Precision.java | 12 +- .../evaluation/outlierdetection/Recall.java | 12 +- .../ScoreByThresholdResult.java | 2 +- .../evaluation/regression/Huber.java | 39 +- .../regression/MeanSquaredError.java | 37 +- .../MeanSquaredLogarithmicError.java | 39 +- .../evaluation/regression/RSquared.java | 42 +- .../evaluation/regression/Regression.java | 34 +- .../ml/dataframe/explain/FieldSelection.java | 31 +- .../dataframe/explain/MemoryEstimation.java | 16 +- .../ml/dataframe/stats/AnalysisStats.java | 3 +- .../classification/ClassificationStats.java | 43 +- .../stats/classification/Hyperparameters.java | 43 +- .../stats/classification/TimingStats.java | 8 +- .../stats/classification/ValidationLoss.java | 15 +- .../ml/dataframe/stats/common/DataCounts.java | 9 +- .../ml/dataframe/stats/common/FoldValues.java | 9 +- .../dataframe/stats/common/MemoryUsage.java | 43 +- .../OutlierDetectionStats.java | 29 +- .../stats/outlierdetection/Parameters.java | 35 +- .../stats/outlierdetection/TimingStats.java | 8 +- .../stats/regression/Hyperparameters.java | 41 +- .../stats/regression/RegressionStats.java | 43 +- .../stats/regression/TimingStats.java | 9 +- .../stats/regression/ValidationLoss.java | 17 +- .../InferenceToXContentCompressor.java | 79 +- .../MlInferenceNamedXContentProvider.java | 630 +- .../core/ml/inference/TrainedModelConfig.java | 280 +- .../ml/inference/TrainedModelDefinition.java | 49 +- .../core/ml/inference/TrainedModelInput.java | 9 +- .../core/ml/inference/TrainedModelType.java | 8 +- .../allocation/AllocationStatus.java | 2 +- .../allocation/TrainedModelAllocation.java | 18 +- .../persistence/InferenceIndexConstants.java | 5 +- .../preprocessing/CustomWordEmbedding.java | 77 +- .../preprocessing/FrequencyEncoding.java | 26 +- .../LenientlyParsedPreProcessor.java | 3 +- .../ml/inference/preprocessing/Multi.java | 41 +- .../ml/inference/preprocessing/NGram.java | 67 +- .../preprocessing/OneHotEncoding.java | 17 +- .../inference/preprocessing/PreProcessor.java | 1 + .../StrictlyParsedPreProcessor.java | 3 +- .../preprocessing/TargetMeanEncoding.java | 28 +- .../DiscreteFeatureValue.java | 1 + .../customwordembedding/Hash32.java | 6 +- .../NGramFeatureExtractor.java | 2 +- .../RelevantScriptFeatureExtractor.java | 2 +- .../customwordembedding/ScriptCode.java | 198 +- .../customwordembedding/ScriptDetector.java | 8 +- .../ClassificationFeatureImportance.java | 38 +- .../ClassificationInferenceResults.java | 68 +- .../inference/results/InferenceResults.java | 2 +- .../core/ml/inference/results/NerResults.java | 9 +- .../results/RawInferenceResults.java | 11 +- .../results/RegressionFeatureImportance.java | 21 +- .../results/RegressionInferenceResults.java | 31 +- .../results/SingleValueInferenceResults.java | 2 - .../ml/inference/results/TopClassEntry.java | 23 +- .../results/WarningInferenceResults.java | 12 +- .../trainedmodel/BertTokenization.java | 4 +- .../trainedmodel/ClassificationConfig.java | 40 +- .../ClassificationConfigUpdate.java | 66 +- .../trainedmodel/EmptyConfigUpdate.java | 6 +- .../trainedmodel/FillMaskConfig.java | 44 +- .../trainedmodel/FillMaskConfigUpdate.java | 28 +- .../inference/trainedmodel/IndexLocation.java | 5 +- .../trainedmodel/InferenceConfig.java | 1 - .../trainedmodel/InferenceConfigUpdate.java | 17 +- .../trainedmodel/InferenceHelpers.java | 102 +- .../trainedmodel/InferenceStats.java | 89 +- .../LenientlyParsedInferenceConfig.java | 3 +- .../LenientlyParsedTrainedModel.java | 3 +- .../LenientlyParsedTrainedModelLocation.java | 3 +- .../ml/inference/trainedmodel/NerConfig.java | 46 +- .../trainedmodel/NerConfigUpdate.java | 19 +- .../trainedmodel/NullInferenceConfig.java | 1 - .../trainedmodel/PassThroughConfig.java | 39 +- .../trainedmodel/PassThroughConfigUpdate.java | 21 +- .../trainedmodel/PredictionFieldType.java | 5 +- .../trainedmodel/RegressionConfig.java | 14 +- .../trainedmodel/RegressionConfigUpdate.java | 25 +- .../trainedmodel/ResultsFieldUpdate.java | 4 +- .../ml/inference/trainedmodel/ShapPath.java | 17 +- .../StrictlyParsedInferenceConfig.java | 3 +- .../StrictlyParsedTrainedModel.java | 3 +- .../StrictlyParsedTrainedModelLocation.java | 3 +- .../ml/inference/trainedmodel/TargetType.java | 5 +- .../TextClassificationConfig.java | 60 +- .../TextClassificationConfigUpdate.java | 36 +- .../trainedmodel/TextEmbeddingConfig.java | 40 +- .../TextEmbeddingConfigUpdate.java | 21 +- .../inference/trainedmodel/Tokenization.java | 4 +- .../inference/trainedmodel/TrainedModel.java | 1 - .../trainedmodel/VocabularyConfig.java | 4 +- .../ZeroShotClassificationConfig.java | 33 +- .../ZeroShotClassificationConfigUpdate.java | 32 +- .../trainedmodel/ensemble/Ensemble.java | 81 +- .../trainedmodel/ensemble/Exponent.java | 5 +- .../LenientlyParsedOutputAggregator.java | 4 +- .../ensemble/LogisticRegression.java | 10 +- .../StrictlyParsedOutputAggregator.java | 4 +- .../trainedmodel/ensemble/WeightedMode.java | 8 +- .../trainedmodel/ensemble/WeightedSum.java | 6 +- .../inference/EnsembleInferenceModel.java | 124 +- .../inference/InferenceDefinition.java | 38 +- .../inference/TreeInferenceModel.java | 228 +- .../langident/LangIdentNeuralNetwork.java | 181 +- .../trainedmodel/langident/LangNetLayer.java | 22 +- .../metadata/FeatureImportanceBaseline.java | 23 +- .../metadata/Hyperparameters.java | 12 +- .../metadata/TotalFeatureImportance.java | 46 +- .../metadata/TrainedModelMetadata.java | 52 +- .../ml/inference/trainedmodel/tree/Tree.java | 48 +- .../inference/trainedmodel/tree/TreeNode.java | 58 +- .../core/ml/inference/utils/Statistics.java | 4 +- .../core/ml/job/config/AnalysisConfig.java | 250 +- .../core/ml/job/config/AnalysisLimits.java | 43 +- .../xpack/core/ml/job/config/Blocked.java | 16 +- .../config/CategorizationAnalyzerConfig.java | 200 +- .../core/ml/job/config/DataDescription.java | 13 +- .../config/DefaultDetectorDescription.java | 4 +- .../core/ml/job/config/DetectionRule.java | 16 +- .../xpack/core/ml/job/config/Detector.java | 263 +- .../xpack/core/ml/job/config/FilterRef.java | 12 +- .../xpack/core/ml/job/config/Job.java | 358 +- .../xpack/core/ml/job/config/JobState.java | 7 +- .../core/ml/job/config/JobTaskState.java | 18 +- .../xpack/core/ml/job/config/JobUpdate.java | 243 +- .../xpack/core/ml/job/config/MlFilter.java | 4 +- .../core/ml/job/config/ModelPlotConfig.java | 9 +- .../xpack/core/ml/job/config/Operator.java | 2 +- .../PerPartitionCategorizationConfig.java | 20 +- .../core/ml/job/config/RuleCondition.java | 9 +- .../xpack/core/ml/job/config/RuleScope.java | 23 +- .../xpack/core/ml/job/groups/GroupOrJob.java | 1 + .../core/ml/job/groups/GroupOrJobLookup.java | 9 +- .../xpack/core/ml/job/messages/Messages.java | 158 +- .../persistence/AnomalyDetectorsIndex.java | 33 +- .../persistence/ElasticsearchMappings.java | 45 +- .../output/FlushAcknowledgement.java | 21 +- .../state/CategorizationStatus.java | 3 +- .../autodetect/state/CategorizerState.java | 5 +- .../autodetect/state/CategorizerStats.java | 93 +- .../process/autodetect/state/DataCounts.java | 205 +- .../autodetect/state/ModelSizeStats.java | 168 +- .../autodetect/state/ModelSnapshot.java | 141 +- .../process/autodetect/state/ModelState.java | 5 +- .../process/autodetect/state/Quantiles.java | 16 +- .../process/autodetect/state/TimingStats.java | 82 +- .../core/ml/job/results/AnomalyCause.java | 92 +- .../core/ml/job/results/AnomalyRecord.java | 138 +- .../xpack/core/ml/job/results/Bucket.java | 68 +- .../core/ml/job/results/BucketInfluencer.java | 53 +- .../ml/job/results/CategoryDefinition.java | 35 +- .../xpack/core/ml/job/results/Forecast.java | 80 +- .../ml/job/results/ForecastRequestStats.java | 75 +- .../xpack/core/ml/job/results/GeoResults.java | 5 +- .../xpack/core/ml/job/results/Influence.java | 12 +- .../xpack/core/ml/job/results/Influencer.java | 57 +- .../xpack/core/ml/job/results/ModelPlot.java | 83 +- .../core/ml/job/results/OverallBucket.java | 10 +- .../ml/job/results/ReservedFieldNames.java | 321 +- .../upgrade/SnapshotUpgradeState.java | 5 +- .../upgrade/SnapshotUpgradeTaskState.java | 18 +- .../AnomalyDetectionAuditMessage.java | 9 +- .../DataFrameAnalyticsAuditMessage.java | 9 +- .../notifications/InferenceAuditMessage.java | 12 +- .../ml/notifications/NotificationsIndex.java | 7 +- .../xpack/core/ml/stats/CountAccumulator.java | 14 +- .../xpack/core/ml/stats/ForecastStats.java | 18 +- .../xpack/core/ml/stats/StatsAccumulator.java | 20 +- .../xpack/core/ml/utils/ExceptionsHelper.java | 20 +- .../ExponentialAverageCalculationContext.java | 47 +- .../xpack/core/ml/utils/MapHelper.java | 8 +- .../xpack/core/ml/utils/MlIndexAndAlias.java | 224 +- .../xpack/core/ml/utils/MlParserUtils.java | 29 +- .../xpack/core/ml/utils/MlStrings.java | 5 +- .../xpack/core/ml/utils/NameResolver.java | 8 +- .../ml/utils/NamedXContentObjectHelper.java | 22 +- .../xpack/core/ml/utils/PhaseProgress.java | 9 +- .../xpack/core/ml/utils/QueryProvider.java | 23 +- .../xpack/core/ml/utils/ToXContentParams.java | 4 +- .../ml/utils/XContentObjectTransformer.java | 25 +- .../DateTimeFormatterTimestampConverter.java | 15 +- .../MonitoringDeprecatedSettings.java | 24 +- .../monitoring/MonitoringFeatureSetUsage.java | 2 +- .../core/monitoring/MonitoringField.java | 12 +- .../action/MonitoringBulkAction.java | 1 - .../monitoring/action/MonitoringBulkDoc.java | 32 +- .../action/MonitoringBulkRequest.java | 58 +- .../action/MonitoringBulkRequestBuilder.java | 15 +- .../action/MonitoringBulkResponse.java | 2 +- .../monitoring/action/MonitoringIndex.java | 2 +- .../MonitoringMigrateAlertsResponse.java | 32 +- .../monitoring/exporter/MonitoringDoc.java | 54 +- .../exporter/MonitoringTemplateUtils.java | 2 +- .../action/RestReloadAnalyzersAction.java | 7 +- .../core/rest/action/RestXPackInfoAction.java | 26 +- .../rest/action/RestXPackUsageAction.java | 28 +- .../xpack/core/rollup/RollupActionConfig.java | 18 +- .../RollupActionDateHistogramGroupConfig.java | 71 +- .../core/rollup/RollupActionGroupConfig.java | 46 +- .../xpack/core/rollup/RollupField.java | 23 +- .../rollup/action/DeleteRollupJobAction.java | 5 +- .../rollup/action/GetRollupCapsAction.java | 2 +- .../action/GetRollupIndexCapsAction.java | 6 +- .../rollup/action/GetRollupJobsAction.java | 18 +- .../rollup/action/PutRollupJobAction.java | 2 +- .../core/rollup/action/RollableIndexCaps.java | 12 +- .../core/rollup/action/RollupAction.java | 8 +- ...ollupActionRequestValidationException.java | 3 +- .../rollup/action/RollupIndexerAction.java | 8 +- .../core/rollup/action/RollupJobCaps.java | 39 +- .../xpack/core/rollup/action/RollupTask.java | 11 +- .../rollup/action/StartRollupJobAction.java | 3 +- .../rollup/action/StopRollupJobAction.java | 8 +- .../rollup/job/DateHistogramGroupConfig.java | 94 +- .../xpack/core/rollup/job/GroupConfig.java | 41 +- .../core/rollup/job/HistogramGroupConfig.java | 31 +- .../xpack/core/rollup/job/MetricConfig.java | 60 +- .../rollup/job/RollupIndexerJobStats.java | 57 +- .../xpack/core/rollup/job/RollupJob.java | 13 +- .../core/rollup/job/RollupJobConfig.java | 50 +- .../core/rollup/job/RollupJobStatus.java | 21 +- .../core/rollup/job/TermsGroupConfig.java | 47 +- .../xpack/core/scheduler/Cron.java | 107 +- .../xpack/core/scheduler/SchedulerEngine.java | 8 +- .../search/action/AsyncSearchResponse.java | 36 +- .../search/action/AsyncStatusResponse.java | 45 +- .../action/SubmitAsyncSearchRequest.java | 39 +- .../MountSearchableSnapshotRequest.java | 92 +- .../SearchableSnapshotFeatureSetUsage.java | 27 +- .../SearchableSnapshotShardStats.java | 106 +- .../SearchableSnapshotsConstants.java | 7 +- .../core/security/CommandLineHttpClient.java | 81 +- .../xpack/core/security/EnrollmentToken.java | 35 +- .../xpack/core/security/HttpResponse.java | 7 +- .../xpack/core/security/ScrollHelper.java | 123 +- .../xpack/core/security/SecurityContext.java | 52 +- .../core/security/SecurityExtension.java | 10 +- .../security/SecurityFeatureSetUsage.java | 20 +- .../xpack/core/security/SecurityField.java | 25 +- .../xpack/core/security/SecuritySettings.java | 20 +- .../xpack/core/security/action/ApiKey.java | 73 +- .../action/ClearSecurityCacheResponse.java | 3 +- .../security/action/CreateApiKeyRequest.java | 16 +- .../action/CreateApiKeyRequestBuilder.java | 26 +- .../security/action/CreateApiKeyResponse.java | 24 +- .../DelegatePkiAuthenticationRequest.java | 31 +- .../DelegatePkiAuthenticationResponse.java | 10 +- .../security/action/GetApiKeyRequest.java | 29 +- .../security/action/GetApiKeyResponse.java | 12 +- .../security/action/GrantApiKeyRequest.java | 17 +- .../action/InvalidateApiKeyRequest.java | 54 +- .../action/InvalidateApiKeyResponse.java | 33 +- .../action/apikey/QueryApiKeyResponse.java | 19 +- .../enrollment/KibanaEnrollmentRequest.java | 3 +- .../enrollment/KibanaEnrollmentResponse.java | 23 +- .../enrollment/NodeEnrollmentRequest.java | 6 +- .../enrollment/NodeEnrollmentResponse.java | 28 +- .../OpenIdConnectAuthenticateRequest.java | 3 +- ...enIdConnectAuthenticateRequestBuilder.java | 5 +- .../OpenIdConnectAuthenticateResponse.java | 15 +- .../oidc/OpenIdConnectLogoutRequest.java | 2 +- ...IdConnectPrepareAuthenticationRequest.java | 16 +- ...ctPrepareAuthenticationRequestBuilder.java | 5 +- ...dConnectPrepareAuthenticationResponse.java | 13 +- .../ClearPrivilegesCacheResponse.java | 3 +- .../privilege/DeletePrivilegesRequest.java | 4 +- .../DeletePrivilegesRequestBuilder.java | 3 +- .../GetBuiltinPrivilegesRequest.java | 3 +- .../GetBuiltinPrivilegesResponse.java | 5 +- .../privilege/GetPrivilegesRequest.java | 2 +- .../privilege/PutPrivilegesRequest.java | 32 +- .../PutPrivilegesRequestBuilder.java | 42 +- .../privilege/PutPrivilegesResponse.java | 2 +- .../action/realm/ClearRealmCacheRequest.java | 11 +- .../realm/ClearRealmCacheRequestBuilder.java | 6 +- .../action/role/ClearRolesCacheRequest.java | 5 +- .../role/ClearRolesCacheRequestBuilder.java | 6 +- .../action/role/ClearRolesCacheResponse.java | 2 +- .../action/role/DeleteRoleAction.java | 1 - .../action/role/DeleteRoleRequest.java | 3 +- .../action/role/DeleteRoleRequestBuilder.java | 3 +- .../security/action/role/GetRolesAction.java | 1 - .../security/action/role/GetRolesRequest.java | 3 +- .../security/action/role/PutRoleAction.java | 1 - .../security/action/role/PutRoleRequest.java | 45 +- .../action/role/PutRoleRequestBuilder.java | 15 +- .../security/action/role/PutRoleResponse.java | 2 +- .../rolemapping/DeleteRoleMappingRequest.java | 3 +- .../DeleteRoleMappingRequestBuilder.java | 3 +- .../rolemapping/GetRoleMappingsRequest.java | 6 +- .../rolemapping/PutRoleMappingRequest.java | 21 +- .../PutRoleMappingRequestBuilder.java | 7 +- .../rolemapping/PutRoleMappingResponse.java | 2 +- .../action/saml/SamlAuthenticateRequest.java | 5 +- .../saml/SamlAuthenticateRequestBuilder.java | 7 +- .../action/saml/SamlAuthenticateResponse.java | 6 +- .../saml/SamlCompleteLogoutRequest.java | 5 +- .../saml/SamlInvalidateSessionRequest.java | 22 +- .../SamlInvalidateSessionRequestBuilder.java | 5 +- .../action/saml/SamlLogoutRequest.java | 5 +- .../SamlPrepareAuthenticationRequest.java | 19 +- ...mlPrepareAuthenticationRequestBuilder.java | 3 +- .../action/saml/SamlSpMetadataRequest.java | 4 +- .../CreateServiceAccountTokenRequest.java | 12 +- .../CreateServiceAccountTokenResponse.java | 6 +- .../DeleteServiceAccountTokenRequest.java | 12 +- .../DeleteServiceAccountTokenResponse.java | 6 +- ...erviceAccountCredentialsNodesResponse.java | 13 +- .../GetServiceAccountCredentialsRequest.java | 6 +- .../GetServiceAccountCredentialsResponse.java | 24 +- .../service/GetServiceAccountRequest.java | 8 +- .../service/GetServiceAccountResponse.java | 6 +- .../action/service/ServiceAccountInfo.java | 6 +- .../security/action/service/TokenInfo.java | 11 +- .../action/token/CreateTokenRequest.java | 60 +- .../token/CreateTokenRequestBuilder.java | 7 +- .../action/token/CreateTokenResponse.java | 34 +- .../action/token/InvalidateTokenRequest.java | 28 +- .../token/InvalidateTokenRequestBuilder.java | 3 +- .../user/AuthenticateRequestBuilder.java | 3 +- .../action/user/AuthenticateResponse.java | 2 +- .../action/user/ChangePasswordRequest.java | 3 +- .../user/ChangePasswordRequestBuilder.java | 54 +- .../action/user/DeleteUserRequest.java | 3 +- .../action/user/DeleteUserRequestBuilder.java | 3 +- .../action/user/GetUserPrivilegesRequest.java | 3 +- .../user/GetUserPrivilegesRequestBuilder.java | 3 +- .../user/GetUserPrivilegesResponse.java | 48 +- .../action/user/HasPrivilegesRequest.java | 9 +- .../user/HasPrivilegesRequestBuilder.java | 3 +- .../action/user/HasPrivilegesResponse.java | 38 +- .../security/action/user/PutUserRequest.java | 38 +- .../action/user/PutUserRequestBuilder.java | 62 +- .../security/action/user/PutUserResponse.java | 4 +- .../action/user/SetEnabledRequestBuilder.java | 3 +- .../core/security/authc/Authentication.java | 43 +- .../authc/AuthenticationFailureHandler.java | 8 +- .../security/authc/AuthenticationResult.java | 25 +- .../authc/AuthenticationServiceField.java | 7 +- .../DefaultAuthenticationFailureHandler.java | 51 +- .../authc/InternalRealmsSettings.java | 3 +- .../core/security/authc/KeyAndTimestamp.java | 9 +- .../xpack/core/security/authc/Realm.java | 13 +- .../core/security/authc/RealmConfig.java | 12 +- .../core/security/authc/RealmSettings.java | 48 +- .../core/security/authc/TokenMetadata.java | 3 +- .../authc/kerberos/KerberosRealmSettings.java | 41 +- ...ActiveDirectorySessionFactorySettings.java | 92 +- .../authc/ldap/LdapRealmSettings.java | 11 +- .../ldap/LdapSessionFactorySettings.java | 6 +- .../LdapUserSearchSessionFactorySettings.java | 48 +- .../ldap/PoolingSessionFactorySettings.java | 61 +- .../ldap/SearchGroupsResolverSettings.java | 37 +- .../UserAttributeGroupsResolverSettings.java | 9 +- .../support/LdapLoadBalancingSettings.java | 11 +- .../support/LdapMetadataResolverSettings.java | 4 +- .../authc/ldap/support/LdapSearchScope.java | 10 +- .../ldap/support/SessionFactorySettings.java | 36 +- .../oidc/OpenIdConnectRealmSettings.java | 277 +- .../security/authc/pki/PkiRealmSettings.java | 30 +- .../authc/saml/SamlRealmSettings.java | 121 +- .../AuthenticationContextSerializer.java | 5 +- .../core/security/authc/support/BCrypt.java | 1620 +- .../CachingUsernamePasswordRealmSettings.java | 27 +- .../DelegatedAuthorizationSettings.java | 4 +- .../authc/support/DnRoleMapperSettings.java | 17 +- .../core/security/authc/support/Hasher.java | 30 +- .../support/SecondaryAuthentication.java | 2 +- .../support/TokensInvalidationResult.java | 13 +- .../authc/support/UserRoleMapper.java | 34 +- .../authc/support/UsernamePasswordToken.java | 15 +- .../support/mapper/ExpressionRoleMapping.java | 45 +- .../support/mapper/TemplateRoleName.java | 73 +- .../mapper/expressiondsl/AllExpression.java | 8 +- .../mapper/expressiondsl/AnyExpression.java | 8 +- .../mapper/expressiondsl/ExpressionModel.java | 25 +- .../expressiondsl/ExpressionParser.java | 48 +- .../mapper/expressiondsl/FieldExpression.java | 2 +- .../security/authz/AuthorizationEngine.java | 48 +- .../authz/IndicesAndAliasesResolverField.java | 2 +- .../core/security/authz/RoleDescriptor.java | 459 +- .../DocumentSubsetBitsetCache.java | 52 +- .../accesscontrol/DocumentSubsetReader.java | 34 +- .../accesscontrol/FieldSubsetReader.java | 17 +- .../accesscontrol/IndicesAccessControl.java | 61 +- .../SecurityIndexReaderWrapper.java | 12 +- .../permission/ApplicationPermission.java | 26 +- .../authz/permission/ClusterPermission.java | 31 +- .../authz/permission/DocumentPermissions.java | 53 +- .../authz/permission/FieldPermissions.java | 33 +- .../permission/FieldPermissionsCache.java | 34 +- .../FieldPermissionsDefinition.java | 6 +- .../authz/permission/IndicesPermission.java | 98 +- .../authz/permission/LimitedRole.java | 73 +- .../permission/ResourcePrivilegesMap.java | 15 +- .../core/security/authz/permission/Role.java | 89 +- .../authz/privilege/ApplicationPrivilege.java | 33 +- .../ApplicationPrivilegeDescriptor.java | 25 +- .../privilege/ClusterPrivilegeResolver.java | 258 +- .../ConfigurableClusterPrivilege.java | 2 +- .../ConfigurableClusterPrivileges.java | 58 +- .../privilege/HealthAndStatsPrivilege.java | 5 +- .../authz/privilege/IndexPrivilege.java | 127 +- .../ManageOwnApiKeyClusterPrivilege.java | 44 +- .../security/authz/privilege/Privilege.java | 10 +- .../authz/store/ReservedRolesStore.java | 825 +- .../authz/support/DLSRoleQueryValidator.java | 82 +- .../SecurityQueryTemplateEvaluator.java | 11 +- .../index/RestrictedIndicesNames.java | 14 +- .../core/security/support/Automatons.java | 59 +- .../core/security/support/Exceptions.java | 3 +- .../core/security/support/MetadataUtils.java | 9 +- .../support/MustacheTemplateEvaluator.java | 11 +- .../core/security/support/NoOpLogger.java | 696 +- .../support/RestorableContextClassLoader.java | 4 +- .../core/security/support/Validation.java | 132 +- .../core/security/user/APMSystemUser.java | 2 +- .../core/security/user/AnonymousUser.java | 26 +- .../core/security/user/AsyncSearchUser.java | 28 +- .../core/security/user/BeatsSystemUser.java | 2 +- .../xpack/core/security/user/ElasticUser.java | 2 - .../user/InternalUserSerializationHelper.java | 1 + .../core/security/user/KibanaSystemUser.java | 2 +- .../xpack/core/security/user/KibanaUser.java | 10 +- .../security/user/LogstashSystemUser.java | 2 +- .../security/user/RemoteMonitoringUser.java | 9 +- .../xpack/core/security/user/User.java | 28 +- .../core/security/user/UsernamesField.java | 2 +- .../xpack/core/security/user/XPackUser.java | 11 +- .../core/security/xcontent/XContentUtils.java | 23 +- .../xpack/core/slm/SLMFeatureSetUsage.java | 8 +- .../core/slm/SnapshotInvocationRecord.java | 23 +- .../core/slm/SnapshotLifecycleMetadata.java | 57 +- .../core/slm/SnapshotLifecyclePolicy.java | 101 +- .../core/slm/SnapshotLifecyclePolicyItem.java | 68 +- .../slm/SnapshotLifecyclePolicyMetadata.java | 71 +- .../core/slm/SnapshotLifecycleStats.java | 134 +- .../slm/SnapshotRetentionConfiguration.java | 117 +- .../action/DeleteSnapshotLifecycleAction.java | 2 +- .../ExecuteSnapshotLifecycleAction.java | 2 +- .../ExecuteSnapshotRetentionAction.java | 2 +- .../core/slm/action/GetSLMStatusAction.java | 3 +- .../action/GetSnapshotLifecycleAction.java | 2 +- .../GetSnapshotLifecycleStatsAction.java | 4 +- .../action/PutSnapshotLifecycleAction.java | 5 +- .../xpack/core/slm/action/StartSLMAction.java | 3 +- .../xpack/core/slm/action/StopSLMAction.java | 3 +- .../core/slm/history/SnapshotHistoryItem.java | 126 +- .../slm/history/SnapshotHistoryStore.java | 48 +- .../SnapshotLifecycleTemplateRegistry.java | 28 +- .../spatial/action/SpatialStatsAction.java | 8 +- .../xpack/core/ssl/CertParsingUtils.java | 40 +- .../core/ssl/RestrictedTrustManager.java | 54 +- .../core/ssl/SSLConfigurationReloader.java | 22 +- .../core/ssl/SSLConfigurationSettings.java | 1 + .../xpack/core/ssl/SSLService.java | 156 +- .../xpack/core/ssl/SslSettingsLoader.java | 18 +- .../core/ssl/TransportTLSBootstrapCheck.java | 2 +- .../xpack/core/ssl/X509KeyPairSettings.java | 139 +- .../ssl/action/GetCertificateInfoAction.java | 2 +- .../TransportGetCertificateInfoAction.java | 12 +- .../xpack/core/ssl/cert/CertificateInfo.java | 40 +- .../rest/RestGetCertificateInfoAction.java | 22 +- .../core/template/IndexTemplateConfig.java | 3 +- .../core/template/IndexTemplateRegistry.java | 216 +- .../xpack/core/template/TemplateUtils.java | 56 +- .../termsenum/action/MultiShardTermsEnum.java | 2 +- .../action/NodeTermsEnumRequest.java | 20 +- .../termsenum/action/SimpleTermCountEnum.java | 17 +- .../core/termsenum/action/TermCount.java | 2 +- .../termsenum/action/TermsEnumAction.java | 10 +- .../termsenum/action/TermsEnumRequest.java | 31 +- .../termsenum/action/TermsEnumResponse.java | 5 +- .../action/TransportTermsEnumAction.java | 66 +- .../core/termsenum/action/package-info.java | 2 +- .../termsenum/rest/RestTermsEnumAction.java | 15 +- .../action/FindStructureAction.java | 84 +- .../structurefinder/FieldStats.java | 62 +- .../structurefinder/TextStructure.java | 139 +- .../core/transform/TransformDeprecations.java | 3 +- .../transform/TransformFeatureSetUsage.java | 20 +- .../core/transform/TransformMetadata.java | 21 +- .../TransformNamedXContentProvider.java | 2 +- .../transform/action/GetTransformAction.java | 10 +- .../action/GetTransformStatsAction.java | 2 +- .../transform/action/SetResetModeAction.java | 1 - .../action/StartTransformAction.java | 4 +- .../transform/action/StopTransformAction.java | 57 +- .../PreviewTransformActionDeprecated.java | 1 - .../StartTransformActionDeprecated.java | 2 +- .../notifications/TransformAuditMessage.java | 9 +- .../core/transform/transforms/DestConfig.java | 14 +- .../transform/transforms/NodeAttributes.java | 43 +- .../transform/transforms/QueryConfig.java | 11 +- .../transform/transforms/SettingsConfig.java | 7 +- .../transform/transforms/SourceConfig.java | 34 +- .../core/transform/transforms/SyncConfig.java | 2 +- .../transforms/TimeRetentionPolicyConfig.java | 5 +- .../transform/transforms/TimeSyncConfig.java | 4 +- .../TransformCheckpointingInfo.java | 2 +- .../TransformDestIndexSettings.java | 2 +- .../transforms/TransformIndexerPosition.java | 13 +- .../transforms/TransformIndexerStats.java | 2 +- .../transforms/TransformProgress.java | 19 +- .../transform/transforms/TransformState.java | 131 +- .../transform/transforms/TransformStats.java | 6 +- .../transforms/TransformStoredDoc.java | 17 +- .../transforms/TransformTaskParams.java | 13 +- .../transforms/TransformTaskState.java | 4 +- .../transforms/latest/LatestConfig.java | 27 +- .../transforms/pivot/AggregationConfig.java | 8 +- .../pivot/DateHistogramGroupSource.java | 6 +- .../transforms/pivot/GeoTileGroupSource.java | 6 +- .../transforms/pivot/GroupConfig.java | 5 +- .../pivot/HistogramGroupSource.java | 2 +- .../transforms/pivot/ScriptConfig.java | 2 +- .../transforms/pivot/SingleGroupSource.java | 10 +- .../transform/utils/TransformStrings.java | 3 +- .../xpack/core/watcher/WatcherField.java | 10 +- .../xpack/core/watcher/WatcherMetadata.java | 4 +- .../xpack/core/watcher/WatcherState.java | 2 +- .../xpack/core/watcher/actions/Action.java | 2 +- .../core/watcher/actions/ActionFactory.java | 2 +- .../core/watcher/actions/ActionRegistry.java | 27 +- .../core/watcher/actions/ActionStatus.java | 166 +- .../core/watcher/actions/ActionWrapper.java | 147 +- .../watcher/actions/ActionWrapperResult.java | 16 +- .../actions/throttler/PeriodThrottler.java | 11 +- .../watcher/client/WatchSourceBuilder.java | 102 +- .../core/watcher/common/stats/Counters.java | 3 +- .../watcher/condition/AlwaysCondition.java | 2 +- .../core/watcher/condition/Condition.java | 5 +- .../watcher/condition/ConditionRegistry.java | 21 +- .../core/watcher/crypto/CryptoService.java | 42 +- .../execution/ActionExecutionMode.java | 30 +- .../execution/WatchExecutionContext.java | 6 +- .../execution/WatchExecutionResult.java | 30 +- .../xpack/core/watcher/execution/Wid.java | 3 +- .../core/watcher/history/WatchRecord.java | 117 +- .../xpack/core/watcher/input/Input.java | 6 +- .../core/watcher/input/none/NoneInput.java | 6 +- .../core/watcher/support/Exceptions.java | 3 +- .../watcher/support/WatcherDateTimeUtils.java | 54 +- .../WatcherIndexTemplateRegistryField.java | 8 +- .../core/watcher/support/WatcherUtils.java | 12 +- .../support/xcontent/WatcherParams.java | 4 +- .../xcontent/WatcherXContentParser.java | 15 +- .../support/xcontent/XContentSource.java | 8 +- .../core/watcher/transform/Transform.java | 12 +- .../watcher/transform/TransformRegistry.java | 24 +- .../transform/chain/ChainTransform.java | 22 +- .../chain/ChainTransformFactory.java | 2 +- .../chain/ExecutableChainTransform.java | 11 +- .../transport/actions/QueryWatchesAction.java | 44 +- .../actions/ack/AckWatchRequest.java | 10 +- .../actions/ack/AckWatchResponse.java | 2 +- .../activate/ActivateWatchRequest.java | 6 +- .../activate/ActivateWatchResponse.java | 2 +- .../actions/execute/ExecuteWatchRequest.java | 41 +- .../actions/execute/ExecuteWatchResponse.java | 13 +- .../actions/get/GetWatchRequest.java | 6 +- .../actions/get/GetWatchRequestBuilder.java | 1 - .../actions/get/GetWatchResponse.java | 14 +- .../actions/put/PutWatchRequestBuilder.java | 4 +- .../actions/service/WatcherServiceAction.java | 1 - .../service/WatcherServiceRequest.java | 8 +- .../service/WatcherServiceRequestBuilder.java | 6 +- .../stats/WatcherStatsRequestBuilder.java | 6 +- .../actions/stats/WatcherStatsResponse.java | 17 +- .../core/watcher/trigger/TriggerEvent.java | 17 +- .../xpack/core/watcher/watch/Watch.java | 39 +- .../xpack/core/watcher/watch/WatchStatus.java | 90 +- .../IndexMetadataConversionTests.java | 81 +- .../AbstractLicenseServiceTestCase.java | 9 +- .../AbstractLicensesIntegrationTestCase.java | 2 +- .../license/ExpirationCallbackTests.java | 70 +- .../license/LicenseClusterChangeTests.java | 4 +- .../license/LicenseFIPSTests.java | 6 +- .../license/LicenseOperationModeTests.java | 3 +- .../LicenseOperationModeUpdateTests.java | 16 +- .../license/LicenseRegistrationTests.java | 36 +- .../license/LicenseScheduleTests.java | 24 +- .../license/LicenseSerializationTests.java | 22 +- .../license/LicenseServiceClusterTests.java | 11 +- .../license/LicenseServiceTests.java | 63 +- .../license/LicenseTLSTests.java | 14 +- .../elasticsearch/license/LicenseTests.java | 228 +- .../LicensedAllocatedPersistentTaskTests.java | 29 +- .../license/LicensesAcknowledgementTests.java | 3 +- .../license/LicensesManagerServiceTests.java | 8 +- .../LicensesMetadataSerializationTests.java | 22 +- .../license/LicensesTransportTests.java | 105 +- .../OperationModeFileWatcherTests.java | 12 +- .../RemoteClusterLicenseCheckerTests.java | 258 +- .../license/SelfGeneratedLicenseTests.java | 87 +- .../license/StartBasicLicenseTests.java | 16 +- .../org/elasticsearch/license/TestUtils.java | 124 +- .../protocol/xpack/XPackInfoRequestTests.java | 20 +- .../xpack/common/ProtocolUtilsTests.java | 2 +- .../xpack/license/LicenseStatusTests.java | 4 +- .../script/MockMustacheScriptEngine.java | 13 +- .../SourceOnlySnapshotShardTests.java | 272 +- .../sourceonly/SourceOnlySnapshotTests.java | 74 +- .../test/http/MockWebServer.java | 48 +- .../MigrateToDataTiersResponseTests.java | 41 +- .../DataTierAllocationDeciderTests.java | 248 +- .../mapper/DataTierFieldTypeTests.java | 28 +- .../xpack/core/ClientHelperTests.java | 54 +- .../core/DataTiersFeatureSetUsageTests.java | 26 +- .../DataTiersUsageTransportActionTests.java | 136 +- .../core/LocalStateCompositeXPackPlugin.java | 318 +- .../xpack/core/XPackPluginTests.java | 19 +- .../xpack/core/XPackSettingsTests.java | 33 +- ...DelegatePkiAuthenticationRequestTests.java | 10 +- ...elegatePkiAuthenticationResponseTests.java | 72 +- .../core/action/ReloadAnalyzerTests.java | 131 +- .../action/ReloadAnalyzersResponseTests.java | 29 +- .../xpack/core/action/ReloadDetailsTests.java | 29 +- .../SetResetModeActionRequestTests.java | 2 +- .../action/TransportXPackInfoActionTests.java | 10 +- .../core/action/XPackUsageResponseTests.java | 26 +- .../action/util/ExpandedIdsMatcherTests.java | 32 +- .../core/action/util/PageParamsTests.java | 18 +- .../core/action/util/QueryPageTests.java | 22 +- .../core/async/AsyncExecutionIdTests.java | 20 +- .../core/async/AsyncResultsServiceTests.java | 87 +- .../async/AsyncSearchIndexServiceTests.java | 77 +- .../core/async/AsyncTaskServiceTests.java | 64 +- .../core/ccr/action/ShardFollowTaskTests.java | 2 +- .../common/IteratingActionListenerTests.java | 30 +- .../AbstractAuditMessageTests.java | 13 +- .../notifications/AbstractAuditorTests.java | 81 +- .../core/common/stats/EnumCountersTests.java | 18 +- .../core/common/time/TimeUtilsTests.java | 40 +- ...eClusterMinimumVersionValidationTests.java | 43 +- .../validation/SourceDestValidatorTests.java | 65 +- .../deprecation/DeprecationIssueTests.java | 25 +- .../xpack/core/ilm/AbstractStepTestCase.java | 2 + .../core/ilm/ActionConfigStatsTests.java | 18 +- .../xpack/core/ilm/AllocateActionTests.java | 99 +- .../core/ilm/AllocationRoutedStepTests.java | 410 +- .../xpack/core/ilm/BranchingStepTests.java | 24 +- .../CheckNoDataStreamWriteIndexStepTests.java | 73 +- .../core/ilm/CheckShrinkReadyStepTests.java | 381 +- .../ilm/CheckTargetShardsCountStepTests.java | 55 +- .../core/ilm/CleanupShrinkIndexStepTests.java | 77 +- .../core/ilm/CleanupSnapshotStepTests.java | 52 +- .../core/ilm/CloseFollowerIndexStepTests.java | 17 +- .../xpack/core/ilm/CloseIndexStepTests.java | 33 +- ...usterStateWaitUntilThresholdStepTests.java | 78 +- .../core/ilm/CopyExecutionStateStepTests.java | 92 +- .../xpack/core/ilm/CopySettingsStepTests.java | 37 +- .../core/ilm/CreateSnapshotStepTests.java | 120 +- .../ilm/DataTierMigrationRoutedStepTests.java | 177 +- .../xpack/core/ilm/DeleteActionTests.java | 7 +- .../xpack/core/ilm/DeleteStepTests.java | 80 +- .../ilm/ExplainLifecycleRequestTests.java | 33 +- .../ilm/ExplainLifecycleResponseTests.java | 17 +- .../xpack/core/ilm/ForceMergeActionTests.java | 59 +- .../xpack/core/ilm/ForceMergeStepTests.java | 61 +- .../xpack/core/ilm/FreezeActionTests.java | 7 +- .../xpack/core/ilm/FreezeStepTests.java | 24 +- .../ilm/GenerateSnapshotNameStepTests.java | 74 +- .../ilm/GenerateUniqueIndexNameStepTests.java | 89 +- .../IndexLifecycleExplainResponseTests.java | 196 +- ...exLifecycleOriginationDateParserTests.java | 26 +- .../ilm/InitializePolicyContextStepTests.java | 9 +- .../ilm/LifecycleExecutionStateTests.java | 37 +- .../ilm/LifecyclePolicyMetadataTests.java | 107 +- .../xpack/core/ilm/LifecyclePolicyTests.java | 224 +- .../core/ilm/LifecyclePolicyUtilsTests.java | 249 +- .../xpack/core/ilm/MigrateActionTests.java | 43 +- .../xpack/core/ilm/MockAction.java | 3 +- .../xpack/core/ilm/MockActionTests.java | 4 +- .../core/ilm/MountSnapshotStepTests.java | 196 +- .../xpack/core/ilm/OpenIndexStepTests.java | 20 +- .../core/ilm/PauseFollowerIndexStepTests.java | 74 +- .../core/ilm/PhaseCacheManagementTests.java | 458 +- .../core/ilm/PhaseExecutionInfoTests.java | 25 +- .../xpack/core/ilm/PhaseStatsTests.java | 20 +- .../xpack/core/ilm/PhaseTests.java | 43 +- .../xpack/core/ilm/PolicyStatsTests.java | 18 +- .../xpack/core/ilm/ReadOnlyActionTests.java | 7 +- ...eplaceDataStreamBackingIndexStepTests.java | 151 +- .../xpack/core/ilm/RolloverActionTests.java | 21 +- .../xpack/core/ilm/RolloverStepTests.java | 167 +- .../xpack/core/ilm/RollupILMActionTests.java | 12 +- .../xpack/core/ilm/RollupStepTests.java | 30 +- .../ilm/SearchableSnapshotActionTests.java | 13 +- .../core/ilm/SegmentCountStepInfoTests.java | 2 +- .../xpack/core/ilm/SegmentCountStepTests.java | 18 +- .../core/ilm/SetPriorityActionTests.java | 26 +- .../ilm/SetSingleNodeAllocateStepTests.java | 416 +- .../xpack/core/ilm/ShrinkActionTests.java | 143 +- .../ilm/ShrinkIndexNameSupplierTests.java | 9 +- .../core/ilm/ShrinkSetAliasStepTests.java | 53 +- .../xpack/core/ilm/ShrinkStepTests.java | 103 +- .../ShrunkShardsAllocatedStepInfoTests.java | 24 +- .../ilm/ShrunkShardsAllocatedStepTests.java | 47 +- .../ilm/ShrunkenIndexCheckStepInfoTests.java | 2 +- .../core/ilm/ShrunkenIndexCheckStepTests.java | 43 +- .../xpack/core/ilm/StepKeyTests.java | 25 +- ...pAliasesAndDeleteSourceIndexStepTests.java | 62 +- .../core/ilm/TerminalPolicyStepTests.java | 1 + .../xpack/core/ilm/TestLifecycleType.java | 16 +- .../ilm/TimeseriesLifecycleTypeTests.java | 722 +- .../xpack/core/ilm/UnfollowActionTests.java | 7 +- .../ilm/UnfollowFollowerIndexStepTests.java | 15 +- .../UpdateRolloverLifecycleDateStepTests.java | 117 +- .../ilm/UpdateRollupIndexPolicyStepTests.java | 65 +- .../core/ilm/UpdateSettingsStepTests.java | 43 +- .../core/ilm/WaitForActiveShardsTests.java | 149 +- .../core/ilm/WaitForDataTierStepTests.java | 36 +- .../WaitForFollowShardTasksStepInfoTests.java | 44 +- .../ilm/WaitForFollowShardTasksStepTests.java | 28 +- .../core/ilm/WaitForIndexColorStepTests.java | 83 +- .../ilm/WaitForIndexingCompleteStepTests.java | 17 +- .../core/ilm/WaitForNoFollowersStepTests.java | 49 +- .../ilm/WaitForRolloverReadyStepTests.java | 169 +- .../core/ilm/WaitForSnapshotStepTests.java | 81 +- .../ilm/action/GetLifecycleResponseTests.java | 37 +- .../ilm/action/MoveToStepRequestTests.java | 2 +- .../ilm/action/PutLifecycleRequestTests.java | 69 +- ...emoveIndexLifecyclePolicyRequestTests.java | 48 +- ...moveIndexLifecyclePolicyResponseTests.java | 8 +- .../core/ilm/action/RetryRequestTests.java | 33 +- .../info/AllocationRoutedStepInfoTests.java | 38 +- .../indexing/AsyncTwoPhaseIndexerTests.java | 73 +- .../core/indexing/IndexerStateEnumTests.java | 2 +- .../ml/AbstractBWCSerializationTestCase.java | 2 +- .../xpack/core/ml/MlTasksTests.java | 332 +- .../ml/action/CloseJobActionRequestTests.java | 2 +- ...nedModelAllocationActionResponseTests.java | 2 +- .../core/ml/action/DatafeedParamsTests.java | 8 +- ...teTrainedModelAliasActionRequestTests.java | 1 - .../EvaluateDataFrameActionRequestTests.java | 18 +- .../EvaluateDataFrameActionResponseTests.java | 16 +- ...DataFrameAnalyticsActionResponseTests.java | 2 +- .../action/ForecastJobActionRequestTests.java | 8 +- .../action/GetBucketActionRequestTests.java | 2 +- .../action/GetBucketActionResponseTests.java | 2 +- .../GetCalendarEventsActionRequestTests.java | 2 +- .../GetCalendarsActionRequestTests.java | 2 +- .../ml/action/GetCategoriesRequestTests.java | 2 +- .../ml/action/GetCategoriesResponseTests.java | 7 +- ...DataFrameAnalyticsActionResponseTests.java | 2 +- ...rameAnalyticsStatsActionResponseTests.java | 45 +- ...tafeedRunningStateActionResponseTests.java | 8 +- .../GetDatafeedStatsActionResponseTests.java | 24 +- .../GetDatafeedsActionResponseTests.java | 3 +- ...GetDeploymentStatsActionResponseTests.java | 117 +- .../action/GetFiltersActionRequestTests.java | 1 - .../action/GetFiltersActionResponseTests.java | 2 +- .../GetInfluencersActionRequestTests.java | 2 +- .../GetInfluencersActionResponseTests.java | 9 +- .../GetJobStatsActionResponseTests.java | 22 +- .../ml/action/GetJobsActionResponseTests.java | 2 +- .../GetModelSnapshotsActionRequestTests.java | 2 +- .../GetModelSnapshotsActionResponseTests.java | 2 +- .../GetOverallBucketsActionRequestTests.java | 2 +- .../GetOverallBucketsActionResponseTests.java | 2 +- .../action/GetRecordsActionRequestTests.java | 2 +- .../action/GetRecordsActionResponseTests.java | 2 +- .../action/GetTrainedModelsRequestTests.java | 31 +- ...TrainedModelsStatsActionResponseTests.java | 16 +- ...erTrainedModelDeploymentRequestsTests.java | 7 +- .../InternalInferModelActionRequestTests.java | 22 +- ...InternalInferModelActionResponseTests.java | 7 +- .../xpack/core/ml/action/JobParamsTests.java | 2 +- .../ml/action/OpenJobActionRequestTests.java | 2 +- .../action/PersistJobActionRequestTests.java | 2 +- .../PostCalendarEventActionRequestTests.java | 28 +- .../ml/action/PostDataActionRequestTests.java | 2 +- ...wDataFrameAnalyticsActionRequestTests.java | 1 - ...DataFrameAnalyticsActionResponseTests.java | 4 +- .../PreviewDatafeedActionRequestTests.java | 51 +- .../action/PutCalendarActionRequestTests.java | 2 +- ...tDataFrameAnalyticsActionRequestTests.java | 30 +- ...DataFrameAnalyticsActionResponseTests.java | 2 +- .../action/PutDatafeedActionRequestTests.java | 4 +- .../PutDatafeedActionResponseTests.java | 4 +- .../action/PutFilterActionRequestTests.java | 2 +- .../action/PutFilterActionResponseTests.java | 2 +- .../PutTrainedModelActionRequestTests.java | 2 +- .../PutTrainedModelActionResponseTests.java | 10 +- ...utTrainedModelAliasActionRequestTests.java | 8 +- ...ModelDefinitionPartActionRequestTests.java | 5 +- ...RevertModelSnapshotActionRequestTests.java | 8 +- .../SetUpgradeModeActionRequestTests.java | 2 +- ...taFrameAnalyticsActionTaskParamsTests.java | 7 +- .../StartDataFrameAnalyticsRequestTests.java | 2 +- .../StartDatafeedActionRequestTests.java | 43 +- ...artTrainedModelDeploymentRequestTests.java | 2 +- ...TrainedModelDeploymentTaskParamsTests.java | 2 +- .../StopDataFrameAnalyticsRequestTests.java | 2 +- .../StopDatafeedActionRequestTests.java | 2 +- .../UpdateCalendarJobActionResquestTests.java | 8 +- ...eDataFrameAnalyticsActionRequestTests.java | 2 +- .../UpdateDatafeedActionRequestTests.java | 4 +- .../UpdateFilterActionRequestTests.java | 2 +- ...UpdateModelSnapshotActionRequestTests.java | 5 +- ...pdateModelSnapshotActionResponseTests.java | 3 +- .../UpdateProcessActionRequestTests.java | 10 +- .../UpgradeJobModelSnapshotRequestTests.java | 8 +- .../UpgradeJobModelSnapshotResponseTests.java | 2 +- .../ValidateDetectorActionRequestTests.java | 2 +- .../ValidateJobConfigActionRequestTests.java | 9 +- .../core/ml/annotations/AnnotationTests.java | 5 +- .../core/ml/calendars/CalendarTests.java | 5 +- .../ml/calendars/ScheduledEventTests.java | 18 +- .../core/ml/datafeed/AggProviderTests.java | 138 +- .../AggProviderWireSerializationTests.java | 14 +- .../core/ml/datafeed/ChunkingConfigTests.java | 46 +- .../datafeed/DatafeedConfigBuilderTests.java | 33 +- .../core/ml/datafeed/DatafeedConfigTests.java | 726 +- .../ml/datafeed/DatafeedTimingStatsTests.java | 49 +- .../core/ml/datafeed/DatafeedUpdateTests.java | 402 +- .../datafeed/DelayedDataCheckConfigTests.java | 42 +- .../extractor/ExtractorUtilsTests.java | 62 +- .../DataFrameAnalyticsConfigTests.java | 305 +- .../DataFrameAnalyticsConfigUpdateTests.java | 121 +- .../DataFrameAnalyticsSourceTests.java | 46 +- .../DataFrameAnalyticsTaskStateTests.java | 2 +- .../analyses/BoostedTreeParamsTests.java | 150 +- .../analyses/ClassificationTests.java | 366 +- .../analyses/OutlierDetectionTests.java | 23 +- .../dataframe/analyses/RegressionTests.java | 262 +- .../classification/AccuracyTests.java | 209 +- .../classification/AucRocResultTests.java | 8 +- .../classification/AucRocTests.java | 2 +- .../classification/ClassificationTests.java | 158 +- .../MulticlassConfusionMatrixResultTests.java | 66 +- .../MulticlassConfusionMatrixTests.java | 414 +- .../PerClassSingleValueTests.java | 2 +- .../classification/PrecisionTests.java | 55 +- .../classification/RecallTests.java | 51 +- .../common/AbstractAucRocTests.java | 4 +- .../outlierdetection/AucRocTests.java | 2 +- .../ConfusionMatrixTests.java | 24 +- .../OutlierDetectionTests.java | 43 +- .../outlierdetection/PrecisionTests.java | 25 +- .../outlierdetection/RecallTests.java | 25 +- .../evaluation/regression/HuberTests.java | 13 +- .../regression/MeanSquaredErrorTests.java | 13 +- .../MeanSquaredLogarithmicErrorTests.java | 13 +- .../evaluation/regression/RSquaredTests.java | 62 +- .../regression/RegressionTests.java | 30 +- .../explain/FieldSelectionTests.java | 13 +- .../explain/MemoryEstimationTests.java | 13 +- .../classification/ValidationLossTests.java | 17 +- .../stats/common/DataCountsTests.java | 7 +- .../stats/common/MemoryUsageTests.java | 2 +- .../OutlierDetectionStatsTests.java | 3 +- .../stats/regression/ValidationLossTests.java | 17 +- .../InferenceConfigItemTestCase.java | 2 +- .../InferenceToXContentCompressorTests.java | 74 +- .../inference/NamedXContentObjectsTests.java | 78 +- .../ml/inference/TrainedModelConfigTests.java | 181 +- .../TrainedModelDefinitionTests.java | 395 +- .../ml/inference/TrainedModelInputTests.java | 7 +- .../ml/inference/TrainedModelTypeTests.java | 6 +- .../allocation/AllocationStatusTests.java | 7 +- .../RoutingStateAndReasonTests.java | 2 +- .../TrainedModelAllocationTests.java | 10 +- .../CustomWordEmbeddingTests.java | 1 - .../preprocessing/FrequencyEncodingTests.java | 25 +- .../inference/preprocessing/MultiTests.java | 116 +- .../inference/preprocessing/NGramTests.java | 38 +- .../preprocessing/OneHotEncodingTests.java | 20 +- .../preprocessing/PreProcessingTests.java | 11 +- .../TargetMeanEncodingTests.java | 26 +- .../FeatureUtilsTests.java | 57 +- .../NGramFeatureExtractorTests.java | 203 +- .../RelevantScriptFeatureExtractorTests.java | 3 +- .../ScriptDetectorTests.java | 4 +- .../ScriptFeatureExtractorTests.java | 95 +- .../ClassificationFeatureImportanceTests.java | 5 +- .../ClassificationInferenceResultsTests.java | 80 +- .../results/FillMaskResultsTests.java | 6 +- .../results/InferenceResultsTestCase.java | 8 +- .../ml/inference/results/NerResultsTests.java | 13 +- .../PyTorchPassThroughResultsTests.java | 6 +- .../results/RawInferenceResultsTests.java | 6 +- .../RegressionFeatureImportanceTests.java | 2 +- .../RegressionInferenceResultsTests.java | 23 +- .../results/TextEmbeddingResultsTests.java | 5 +- .../inference/results/TopClassEntryTests.java | 2 +- .../results/WarningInferenceResultsTests.java | 5 +- .../ClassificationConfigTests.java | 6 +- .../ClassificationConfigUpdateTests.java | 49 +- .../FillMaskConfigUpdateTests.java | 58 +- .../trainedmodel/IndexLocationTests.java | 2 +- .../trainedmodel/InferenceHelpersTests.java | 1 - .../trainedmodel/InferenceStatsTests.java | 9 +- .../trainedmodel/NerConfigUpdateTests.java | 29 +- .../PassThroughConfigUpdateTests.java | 28 +- .../PredictionFieldTypeTests.java | 33 +- .../RegressionConfigUpdateTests.java | 56 +- .../trainedmodel/ResultsFieldUpdateTests.java | 4 +- .../TextClassificationConfigTests.java | 18 +- .../TextClassificationConfigUpdateTests.java | 119 +- .../TextEmbeddingConfigUpdateTests.java | 28 +- ...roShotClassificationConfigUpdateTests.java | 38 +- .../trainedmodel/ensemble/EnsembleTests.java | 131 +- .../trainedmodel/ensemble/ExponentTests.java | 19 +- .../ensemble/LogisticRegressionTests.java | 36 +- .../ensemble/WeightedAggregatorTests.java | 2 +- .../ensemble/WeightedModeTests.java | 49 +- .../ensemble/WeightedSumTests.java | 19 +- .../EnsembleInferenceModelTests.java | 427 +- .../inference/InferenceDefinitionTests.java | 381 +- .../inference/InferenceModelTestUtils.java | 17 +- .../inference/TreeInferenceModelTests.java | 147 +- .../LangIdentNeuralNetworkTests.java | 9 +- .../langident/LangNetLayerTests.java | 6 +- .../langident/LanguageExamples.java | 21 +- .../FeatureImportanceBaselineTests.java | 11 +- .../metadata/HyperparametersTests.java | 4 +- .../metadata/TotalFeatureImportanceTests.java | 11 +- .../metadata/TrainedModelMetadataTests.java | 7 +- .../trainedmodel/tree/TreeNodeTests.java | 24 +- .../trainedmodel/tree/TreeTests.java | 114 +- .../ml/inference/utils/StatisticsTests.java | 8 +- .../ml/job/config/AnalysisConfigTests.java | 240 +- .../ml/job/config/AnalysisLimitsTests.java | 75 +- .../core/ml/job/config/BlockedTests.java | 7 +- .../ml/job/config/DataDescriptionTests.java | 34 +- .../ml/job/config/DetectionRuleTests.java | 38 +- .../core/ml/job/config/DetectorTests.java | 172 +- .../core/ml/job/config/FilterRefTests.java | 2 +- .../xpack/core/ml/job/config/JobTests.java | 165 +- .../core/ml/job/config/JobUpdateTests.java | 91 +- .../core/ml/job/config/MlFilterTests.java | 20 +- .../ml/job/config/ModelPlotConfigTests.java | 2 +- ...PerPartitionCategorizationConfigTests.java | 8 +- .../ml/job/config/RuleConditionTests.java | 2 +- .../core/ml/job/config/RuleScopeTests.java | 34 +- .../ml/job/groups/GroupOrJobLookupTests.java | 9 +- .../core/ml/job/groups/GroupOrJobTests.java | 1 - .../ElasticsearchMappingsTests.java | 66 +- .../state/CategorizerStatsTests.java | 2 +- .../autodetect/state/DataCountsTests.java | 139 +- .../autodetect/state/ModelSizeStatsTests.java | 8 +- .../autodetect/state/ModelSnapshotTests.java | 44 +- .../autodetect/state/QuantilesTests.java | 17 +- .../autodetect/state/TimingStatsTests.java | 29 +- .../ml/job/results/AnomalyCauseTests.java | 5 +- .../ml/job/results/AnomalyRecordTests.java | 23 +- .../ml/job/results/BucketInfluencerTests.java | 15 +- .../core/ml/job/results/GeoResultsTests.java | 11 +- .../core/ml/job/results/InfluencerTests.java | 16 +- .../ml/notifications/AuditMessageTests.java | 1 - .../core/ml/stats/CountAccumulatorTests.java | 2 +- .../core/ml/stats/ForecastStatsTests.java | 11 +- .../core/ml/stats/StatsAccumulatorTests.java | 1 + .../core/ml/utils/ExceptionsHelperTests.java | 13 +- ...nentialAverageCalculationContextTests.java | 26 +- .../xpack/core/ml/utils/MapHelperTests.java | 187 +- .../core/ml/utils/MlIndexAndAliasTests.java | 256 +- .../utils/NamedXContentObjectHelperTests.java | 21 +- .../core/ml/utils/PhaseProgressTests.java | 2 +- .../core/ml/utils/QueryProviderTests.java | 40 +- .../utils/XContentObjectTransformerTests.java | 138 +- .../xpack/core/rollup/ConfigTestHelpers.java | 43 +- .../core/rollup/RollupActionConfigTests.java | 29 +- ...eHistogramGroupConfigSerializingTests.java | 66 +- ...llupActionGroupConfigSerializingTests.java | 22 +- ...eHistogramGroupConfigSerializingTests.java | 98 +- .../job/GroupConfigSerializingTests.java | 2 +- .../HistogramGroupConfigSerializingTests.java | 20 +- .../job/JobWrapperSerializingTests.java | 28 +- .../job/MetricConfigSerializingTests.java | 56 +- .../job/RollupIndexerJobStatsTests.java | 20 +- .../core/rollup/job/RollupJobConfigTests.java | 259 +- .../core/rollup/job/RollupJobStatusTests.java | 3 +- .../xpack/core/rollup/job/RollupJobTests.java | 2 +- .../job/TermsGroupConfigSerializingTests.java | 31 +- .../core/scheduler/SchedulerEngineTests.java | 56 +- .../DataStreamFeatureSetUsageTests.java | 5 +- .../SearchableSnapshotShardStatsTests.java | 41 +- .../SearchableSnapshotsConstantsTests.java | 44 +- ...archableSnapshotsFeatureSetUsageTests.java | 19 +- .../core/security/EnrollmentTokenTests.java | 5 +- .../core/security/action/ApiKeyTests.java | 23 +- .../CreateApiKeyRequestBuilderTests.java | 25 +- .../action/CreateApiKeyRequestTests.java | 5 +- .../action/CreateApiKeyResponseTests.java | 49 +- .../action/GetApiKeyRequestTests.java | 42 +- .../action/GetApiKeyResponseTests.java | 88 +- .../action/InvalidateApiKeyRequestTests.java | 78 +- .../action/InvalidateApiKeyResponseTests.java | 79 +- .../apikey/QueryApiKeyRequestTests.java | 36 +- .../apikey/QueryApiKeyResponseTests.java | 3 +- .../KibanaEnrollmentResponseTests.java | 30 +- .../NodeEnrollementResponseTests.java | 26 +- .../DeletePrivilegesRequestTests.java | 14 +- .../DeletePrivilegesResponseTests.java | 3 +- .../privilege/GetPrivilegesRequestTests.java | 3 +- .../privilege/GetPrivilegesResponseTests.java | 6 +- .../privilege/PutPrivilegesRequestTests.java | 35 +- .../privilege/PutPrivilegesResponseTests.java | 4 +- .../action/role/PutRoleRequestTests.java | 63 +- .../saml/SamlCompleteLogoutRequestTests.java | 6 +- .../SamlInvalidateSessionRequestTests.java | 6 +- .../saml/SamlSpMetadataRequestTests.java | 2 +- ...CreateServiceAccountTokenRequestTests.java | 26 +- ...reateServiceAccountTokenResponseTests.java | 33 +- ...DeleteServiceAccountTokenRequestTests.java | 41 +- ...eleteServiceAccountTokenResponseTests.java | 9 +- ...ServiceAccountCredentialsRequestTests.java | 23 +- ...erviceAccountCredentialsResponseTests.java | 14 +- .../GetServiceAccountRequestTests.java | 12 +- .../GetServiceAccountResponseTests.java | 49 +- .../token/CreateTokenResponseTests.java | 32 +- .../token/InvalidateTokenRequestTests.java | 65 +- .../token/InvalidateTokenResponseTests.java | 96 +- .../user/GetUserPrivilegesResponseTests.java | 124 +- .../user/HasPrivilegesRequestTests.java | 23 +- .../audit/logfile/CapturingLogger.java | 2 +- .../security/authc/AuthenticationTests.java | 81 +- ...aultAuthenticationFailureHandlerTests.java | 41 +- .../core/security/authc/RealmConfigTests.java | 14 +- .../security/authc/TokenMetadataTests.java | 3 +- .../support/SecondaryAuthenticationTests.java | 7 +- .../support/mapper/TemplateRoleNameTests.java | 194 +- .../ExpressionModelPredicateTests.java | 7 +- .../expressiondsl/ExpressionModelTests.java | 25 +- .../expressiondsl/ExpressionParserTests.java | 64 +- .../DocumentSubsetBitsetCacheTests.java | 84 +- .../DocumentSubsetReaderTests.java | 53 +- .../accesscontrol/FieldSubsetReaderTests.java | 227 +- ...ityIndexReaderWrapperIntegrationTests.java | 144 +- .../SecurityIndexReaderWrapperUnitTests.java | 80 +- .../ApplicationPermissionTests.java | 20 +- .../permission/ClusterPermissionTests.java | 101 +- .../permission/DocumentPermissionsTests.java | 64 +- .../FieldPermissionsCacheTests.java | 109 +- .../permission/FieldPermissionsTests.java | 79 +- .../authz/permission/LimitedRoleTests.java | 531 +- .../ResourcePrivilegesMapTests.java | 62 +- .../permission/ResourcePrivilegesTests.java | 33 +- .../ApplicationPrivilegeDescriptorTests.java | 43 +- .../privilege/ApplicationPrivilegeTests.java | 15 +- .../ClusterPrivilegeResolverTests.java | 18 +- .../ConfigurableClusterPrivilegesTests.java | 6 +- .../ManageApplicationPrivilegesTests.java | 21 +- .../ManageOwnApiKeyClusterPrivilegeTests.java | 109 +- .../authz/privilege/PrivilegeTests.java | 101 +- .../authz/store/ReservedRolesStoreTests.java | 1005 +- .../support/DLSRoleQueryValidatorTests.java | 17 +- .../SecurityQueryTemplateEvaluatorTests.java | 23 +- .../security/support/AutomatonsTests.java | 15 +- .../security/support/StringMatcherTests.java | 19 +- .../security/support/ValidationTests.java | 116 +- .../security/test/TestRestrictedIndices.java | 186 +- .../xpack/core/security/user/UserTests.java | 11 +- .../slm/SnapshotInvocationRecordTests.java | 25 +- .../slm/SnapshotLifecycleMetadataTests.java | 9 +- .../slm/SnapshotLifecyclePolicyItemTests.java | 77 +- .../SnapshotLifecyclePolicyMetadataTests.java | 37 +- .../core/slm/SnapshotLifecycleStatsTests.java | 11 +- .../SnapshotRetentionConfigurationTests.java | 63 +- .../slm/history/SnapshotHistoryItemTests.java | 96 +- .../history/SnapshotHistoryStoreTests.java | 41 +- ...napshotLifecycleTemplateRegistryTests.java | 132 +- .../spatial/SpatialFeatureSetUsageTests.java | 3 +- .../xpack/core/ssl/CertParsingUtilsTests.java | 13 +- .../core/ssl/RestrictedTrustConfigTests.java | 3 +- .../core/ssl/RestrictedTrustManagerTests.java | 84 +- .../ssl/SSLConfigurationReloaderTests.java | 124 +- .../ssl/SSLConfigurationSettingsTests.java | 36 +- .../xpack/core/ssl/SSLServiceTests.java | 106 +- .../core/ssl/SslSettingsLoaderTests.java | 167 +- .../ssl/TransportTLSBootstrapCheckTests.java | 24 +- .../core/ssl/cert/CertificateInfoTests.java | 7 +- .../core/template/TemplateUtilsTests.java | 120 +- .../termsenum/MultiShardTermsEnumTests.java | 8 +- .../xpack/core/termsenum/TermCountTests.java | 2 +- .../core/termsenum/TermsEnumRequestTests.java | 18 +- .../termsenum/TermsEnumResponseTests.java | 23 +- .../TransportTermsEnumActionTests.java | 2 +- .../action/RestTermsEnumActionTests.java | 23 +- .../structurefinder/FieldStatsTests.java | 2 +- .../structurefinder/TextStructureTests.java | 2 +- .../AbstractSerializingTransformTestCase.java | 8 +- .../MockDeprecatedAggregationBuilder.java | 4 +- .../transform/MockDeprecatedQueryBuilder.java | 4 +- .../TransformFeatureSetUsageTests.java | 10 +- .../transform/TransformMessagesTests.java | 12 +- ...tractWireSerializingTransformTestCase.java | 2 +- .../GetTransformActionResponseTests.java | 2 +- .../GetTransformStatsActionRequestTests.java | 6 +- .../PreviewTransformActionRequestTests.java | 29 +- .../PreviewTransformsActionResponseTests.java | 2 +- .../UpgradeTransformsActionResponseTests.java | 6 +- .../TransformAuditMessageTests.java | 2 +- .../transforms/NodeAttributeTests.java | 9 +- .../transforms/QueryConfigTests.java | 10 +- .../transforms/SettingsConfigTests.java | 18 +- .../TimeRetentionPolicyConfigTests.java | 7 +- .../transforms/TimeSyncConfigTests.java | 2 +- .../TransformConfigUpdateTests.java | 26 +- .../TransformIndexerPositionTests.java | 2 +- .../TransformIndexerStatsTests.java | 2 +- .../transforms/TransformStateTests.java | 4 +- .../transforms/TransformStatsTests.java | 2 +- .../transforms/TransformTaskStateTests.java | 2 +- .../transforms/latest/LatestConfigTests.java | 4 +- .../pivot/AggregationConfigTests.java | 8 +- .../pivot/DateHistogramGroupSourceTests.java | 2 +- .../pivot/GeoTileGroupSourceTests.java | 2 +- .../transforms/pivot/GroupConfigTests.java | 4 +- .../pivot/HistogramGroupSourceTests.java | 2 +- .../transforms/pivot/ScriptConfigTests.java | 6 +- .../pivot/TermsGroupSourceTests.java | 2 +- .../schema/TransformConfigTests.java | 2 +- .../utils/TransformStringsTests.java | 3 +- .../watcher/crypto/CryptoServiceTests.java | 8 +- .../xpack/core/watcher/watch/ClockMock.java | 1 - .../xcontent/WatcherXContentParserTests.java | 14 +- .../XPackCoreClientYamlTestSuiteIT.java | 8 +- .../xpack/datastreams/DataStreamRestIT.java | 2 +- .../datastreams/DataStreamUpgradeRestIT.java | 2 +- .../xpack/datastreams/DataStreamsRestIT.java | 52 +- .../datastreams/AutoCreateDataStreamIT.java | 2 +- .../DataStreamsClientYamlTestSuiteIT.java | 1 + .../deprecation/ClusterDeprecationChecks.java | 6 +- .../xpack/deprecation/Deprecation.java | 50 +- .../xpack/deprecation/DeprecationChecks.java | 18 +- .../deprecation/DeprecationInfoAction.java | 77 +- .../deprecation/IndexDeprecationChecks.java | 56 +- .../deprecation/MlDeprecationChecker.java | 121 +- .../deprecation/NodeDeprecationChecks.java | 333 +- .../NodesDeprecationCheckAction.java | 18 +- .../NodesDeprecationCheckResponse.java | 8 +- .../RestDeprecationInfoAction.java | 6 +- .../TransportDeprecationInfoAction.java | 119 +- .../TransportNodeDeprecationCheckAction.java | 49 +- .../logging/DeprecationCacheResetAction.java | 8 +- .../logging/DeprecationIndexingComponent.java | 19 +- .../DeprecationIndexingTemplateRegistry.java | 2 +- .../TransportDeprecationCacheResetAction.java | 36 +- .../ClusterDeprecationChecksTests.java | 34 +- .../deprecation/DeprecationChecksTests.java | 11 +- .../DeprecationInfoActionResponseTests.java | 107 +- .../IndexDeprecationChecksTests.java | 95 +- .../MlDeprecationCheckerTests.java | 11 +- .../NodeDeprecationChecksTests.java | 377 +- .../NodesDeprecationCheckRequestTests.java | 8 +- .../NodesDeprecationCheckResponseTests.java | 33 +- .../TransportDeprecationInfoActionTests.java | 30 +- ...nsportNodeDeprecationCheckActionTests.java | 23 +- .../test/enrich/CommonEnrichRestTestCase.java | 79 +- .../enrich/EnrichAdvancedSecurityIT.java | 12 +- .../xpack/enrich/EnrichRestIT.java | 1 + .../test/eql/BaseEqlSpecTestCase.java | 45 +- .../elasticsearch/test/eql/DataLoader.java | 69 +- .../test/eql/EqlRestTestCase.java | 30 +- .../test/eql/EqlRestValidationTestCase.java | 30 +- .../elasticsearch/test/eql/EqlSpecLoader.java | 4 +- .../RemoteClusterAwareEqlRestTestCase.java | 10 +- .../test/eql/stats/EqlUsageRestTestCase.java | 88 +- .../xpack/eql/qa/mixed_node/EqlSearchIT.java | 191 +- .../xpack/eql/EqlRestValidationIT.java | 23 +- .../xpack/eql/RemoteClusterTestUtils.java | 2 +- .../elasticsearch/xpack/eql/EqlRestIT.java | 4 +- .../xpack/eql/EqlRestValidationIT.java | 37 +- .../xpack/eql/AsyncEqlSecurityIT.java | 32 +- .../xpack/eql/EqlRestValidationIT.java | 37 +- .../elasticsearch/xpack/eql/EqlStatsIT.java | 2 +- .../xpack/eql/SecurityUtils.java | 4 +- .../AbstractEqlBlockingIntegTestCase.java | 11 +- .../eql/action/AsyncEqlSearchActionIT.java | 105 +- .../xpack/eql/action/EqlCancellationIT.java | 30 +- .../eql/action/RestEqlCancellationIT.java | 41 +- .../xpack/eql/EqlUsageTransportAction.java | 22 +- .../xpack/eql/action/EqlSearchRequest.java | 112 +- .../xpack/eql/action/EqlSearchResponse.java | 120 +- .../xpack/eql/action/EqlSearchTask.java | 23 +- .../xpack/eql/analysis/AnalysisUtils.java | 58 +- .../xpack/eql/analysis/Analyzer.java | 11 +- .../xpack/eql/analysis/PostAnalyzer.java | 6 +- .../xpack/eql/analysis/Verifier.java | 104 +- .../xpack/eql/execution/PlanExecutor.java | 1 - .../eql/execution/assembler/Criterion.java | 16 +- .../execution/assembler/ExecutionManager.java | 39 +- .../execution/search/BasicQueryClient.java | 9 +- .../eql/execution/search/HitReference.java | 3 +- .../xpack/eql/execution/search/Ordinal.java | 8 +- .../execution/search/PITAwareQueryClient.java | 40 +- .../eql/execution/search/RuntimeUtils.java | 36 +- .../eql/execution/search/SourceGenerator.java | 24 +- .../xpack/eql/execution/search/Timestamp.java | 7 +- .../search/extractor/FieldHitExtractor.java | 4 +- .../ImplicitTiebreakerHitExtractor.java | 2 +- .../extractor/TimestampFieldHitExtractor.java | 3 +- .../eql/execution/sequence/KeyAndOrdinal.java | 3 +- .../execution/sequence/KeyToSequences.java | 4 +- .../xpack/eql/execution/sequence/Match.java | 3 +- .../eql/execution/sequence/Sequence.java | 8 +- .../execution/sequence/SequenceMatcher.java | 24 +- .../execution/sequence/TumblingWindow.java | 32 +- .../expression/OptionalMissingAttribute.java | 15 +- .../function/EqlFunctionDefinition.java | 12 +- .../function/EqlFunctionRegistry.java | 47 +- .../function/scalar/math/ToNumber.java | 21 +- .../scalar/math/ToNumberFunctionPipe.java | 1 - .../math/ToNumberFunctionProcessor.java | 1 - .../function/scalar/string/Between.java | 53 +- .../scalar/string/BetweenFunctionPipe.java | 29 +- .../function/scalar/string/CIDRMatch.java | 18 +- .../scalar/string/CIDRMatchFunctionPipe.java | 2 +- .../string/CIDRMatchFunctionProcessor.java | 12 +- .../scalar/string/ConcatFunctionPipe.java | 2 +- .../string/ConcatFunctionProcessor.java | 7 +- .../string/EndsWithFunctionProcessor.java | 1 - .../function/scalar/string/IndexOf.java | 33 +- .../string/IndexOfFunctionProcessor.java | 1 - .../function/scalar/string/Length.java | 19 +- .../string/LengthFunctionProcessor.java | 1 - .../string/StringContainsFunctionPipe.java | 3 +- .../StringContainsFunctionProcessor.java | 4 +- .../function/scalar/string/Substring.java | 24 +- .../scalar/string/SubstringFunctionPipe.java | 4 +- .../string/SubstringFunctionProcessor.java | 5 +- .../function/scalar/string/ToString.java | 19 +- .../string/ToStringFunctionProcessor.java | 1 - .../whitelist/InternalEqlScriptUtils.java | 3 +- .../InsensitiveBinaryComparison.java | 19 +- .../InsensitiveBinaryComparisonPipe.java | 9 +- .../InsensitiveBinaryComparisonProcessor.java | 7 +- .../comparison/InsensitiveNotEquals.java | 4 - .../InsensitiveWildcardNotEquals.java | 4 +- .../comparison/StringComparisons.java | 2 +- .../xpack/eql/optimizer/Optimizer.java | 90 +- .../xpack/eql/parser/AbstractBuilder.java | 18 +- .../xpack/eql/parser/EqlBaseBaseListener.java | 1288 +- .../xpack/eql/parser/EqlBaseBaseVisitor.java | 792 +- .../xpack/eql/parser/EqlBaseLexer.java | 711 +- .../xpack/eql/parser/EqlBaseListener.java | 1044 +- .../xpack/eql/parser/EqlBaseParser.java | 6297 +++--- .../xpack/eql/parser/EqlBaseVisitor.java | 610 +- .../xpack/eql/parser/EqlParser.java | 58 +- .../xpack/eql/parser/ExpressionBuilder.java | 15 +- .../xpack/eql/parser/LogicalPlanBuilder.java | 67 +- .../xpack/eql/plan/logical/Join.java | 37 +- .../xpack/eql/plan/logical/KeyedFilter.java | 6 +- .../xpack/eql/plan/logical/Sequence.java | 43 +- .../xpack/eql/plan/physical/EsQueryExec.java | 12 +- .../xpack/eql/plan/physical/FilterExec.java | 4 +- .../plan/physical/LimitWithOffsetExec.java | 3 +- .../eql/plan/physical/LocalRelation.java | 1 - .../xpack/eql/plan/physical/OrderExec.java | 3 +- .../xpack/eql/plan/physical/ProjectExec.java | 3 +- .../xpack/eql/plan/physical/SequenceExec.java | 51 +- .../xpack/eql/plan/physical/Unexecutable.java | 1 - .../xpack/eql/planner/Mapper.java | 20 +- .../planner/MultiValueAwareScriptQuery.java | 2 +- .../xpack/eql/planner/QueryFolder.java | 12 +- .../xpack/eql/planner/QueryTranslator.java | 16 +- .../xpack/eql/plugin/EqlPlugin.java | 66 +- .../xpack/eql/plugin/RestEqlSearchAction.java | 17 +- .../TransportEqlAsyncGetResultsAction.java | 29 +- .../TransportEqlAsyncGetStatusAction.java | 30 +- .../eql/plugin/TransportEqlSearchAction.java | 164 +- .../eql/plugin/TransportEqlStatsAction.java | 37 +- .../eql/querydsl/container/ComputedRef.java | 1 - .../querydsl/container/QueryContainer.java | 26 +- .../xpack/eql/session/EmptyExecutable.java | 3 +- .../xpack/eql/session/EqlConfiguration.java | 20 +- .../xpack/eql/session/EqlSession.java | 22 +- .../xpack/eql/stats/Metrics.java | 7 +- .../xpack/eql/util/RemoteClusterRegistry.java | 2 +- .../xpack/eql/util/StringUtils.java | 5 +- .../eql/AbstractBWCSerializationTestCase.java | 7 +- .../AbstractBWCWireSerializingTestCase.java | 5 +- .../eql/EqlInfoTransportActionTests.java | 21 +- .../elasticsearch/xpack/eql/EqlTestUtils.java | 54 +- .../xpack/eql/StringUtilsTests.java | 6 +- .../eql/action/EqlRequestParserTests.java | 68 +- .../eql/action/EqlSearchRequestTests.java | 18 +- .../eql/action/EqlSearchResponseTests.java | 64 +- .../xpack/eql/analysis/CancellationTests.java | 99 +- .../xpack/eql/analysis/VerifierTests.java | 269 +- .../assembler/ImplicitTiebreakerTests.java | 46 +- .../assembler/SequenceSpecTests.java | 69 +- .../eql/execution/assembler/SeriesUtils.java | 56 +- .../CriterionOrdinalExtractionTests.java | 47 +- .../eql/execution/search/LimitTests.java | 1 - .../eql/execution/search/OrdinalTests.java | 2 +- .../sequence/CircuitBreakerTests.java | 45 +- .../function/EqlFunctionRegistryTests.java | 35 +- .../math/ToNumberFunctionPipeTests.java | 52 +- .../math/ToNumberFunctionProcessorTests.java | 75 +- .../string/BetweenFunctionPipeTests.java | 83 +- .../string/BetweenFunctionProcessorTests.java | 6 +- .../string/CIDRMatchFunctionPipeTests.java | 33 +- .../CIDRMatchFunctionProcessorTests.java | 31 +- .../string/ConcatFunctionPipeTests.java | 14 +- .../string/ConcatFunctionProcessorTests.java | 3 +- .../string/EndsWithFunctionPipeTests.java | 73 +- .../EndsWithFunctionProcessorTests.java | 12 +- .../string/IndexOfFunctionPipeTests.java | 85 +- .../string/IndexOfFunctionProcessorTests.java | 18 +- .../string/LengthFunctionPipeTests.java | 18 +- .../string/LengthFunctionProcessorTests.java | 12 +- .../StartsWithFunctionProcessorTests.java | 4 +- .../StringContainsFunctionPipeTests.java | 73 +- .../StringContainsFunctionProcessorTests.java | 6 +- .../scalar/string/StringUtilsTests.java | 82 +- .../string/SubstringFunctionPipeTests.java | 64 +- .../SubstringFunctionProcessorTests.java | 18 +- .../string/ToStringFunctionPipeTests.java | 18 +- .../InsensitiveBinaryComparisonPipeTests.java | 79 +- ...nsitiveBinaryComparisonProcessorTests.java | 9 +- .../eql/optimizer/EqlFoldSpecLoader.java | 1 - .../xpack/eql/optimizer/OptimizerTests.java | 25 +- .../xpack/eql/optimizer/TomlFoldTests.java | 13 +- .../xpack/eql/parser/ExpressionTests.java | 198 +- .../xpack/eql/parser/GrammarTests.java | 10 +- .../xpack/eql/parser/LogicalPlanTests.java | 27 +- .../AbstractQueryTranslatorTestCase.java | 7 +- .../eql/planner/QueryTranslatorFailTests.java | 229 +- .../eql/planner/QueryTranslatorSpecTests.java | 1 + .../xpack/eql/stats/VerifierMetricsTests.java | 120 +- .../xpack/fleet/FleetRestIT.java | 1 + .../smoketest/GraphWithSecurityIT.java | 13 +- .../GraphWithSecurityInsufficientRoleIT.java | 5 +- .../xpack/graph/test/GraphTests.java | 102 +- .../org/elasticsearch/xpack/graph/Graph.java | 18 +- .../xpack/graph/GraphInfoTransportAction.java | 8 +- .../graph/GraphUsageTransportAction.java | 36 +- .../action/TransportGraphExploreAction.java | 177 +- .../graph/rest/action/RestGraphAction.java | 102 +- .../graph/GraphInfoTransportActionTests.java | 34 +- .../rest/action/RestGraphActionTests.java | 32 +- .../idp/IdentityProviderAuthenticationIT.java | 94 +- .../xpack/idp/IdpRestTestCase.java | 27 +- .../idp/ManageServiceProviderRestIT.java | 34 +- .../idp/WildcardServiceProviderRestIT.java | 22 +- .../idp/action/SamlIdentityProviderTests.java | 203 +- .../sp/SamlServiceProviderIndexTests.java | 45 +- .../test/IdentityProviderIntegTestCase.java | 99 +- .../xpack/idp/IdentityProviderPlugin.java | 56 +- .../DeleteSamlServiceProviderResponse.java | 8 +- .../action/PutSamlServiceProviderRequest.java | 30 +- .../SamlInitiateSingleSignOnRequest.java | 3 +- .../SamlInitiateSingleSignOnResponse.java | 11 +- .../xpack/idp/action/SamlMetadataRequest.java | 2 +- .../SamlValidateAuthnRequestRequest.java | 3 +- .../SamlValidateAuthnRequestResponse.java | 17 +- ...nsportDeleteSamlServiceProviderAction.java | 39 +- ...TransportPutSamlServiceProviderAction.java | 78 +- ...ansportSamlInitiateSingleSignOnAction.java | 221 +- .../action/TransportSamlMetadataAction.java | 8 +- ...ansportSamlValidateAuthnRequestAction.java | 20 +- .../ApplicationActionsResolver.java | 57 +- .../idp/privileges/UserPrivilegeResolver.java | 30 +- .../saml/authn/SamlAuthnRequestValidator.java | 198 +- ...lAuthenticationResponseMessageBuilder.java | 11 +- .../saml/authn/UserServiceAuthentication.java | 41 +- .../idp/saml/idp/SamlIdPMetadataBuilder.java | 5 +- .../idp/saml/idp/SamlIdentityProvider.java | 93 +- .../saml/idp/SamlIdentityProviderBuilder.java | 147 +- .../idp/saml/idp/SamlMetadataGenerator.java | 53 +- .../saml/rest/action/IdpBaseRestHandler.java | 7 +- .../RestDeleteSamlServiceProviderAction.java | 20 +- .../RestPutSamlServiceProviderAction.java | 27 +- .../RestSamlInitiateSingleSignOnAction.java | 33 +- .../rest/action/RestSamlMetadataAction.java | 9 +- ...mlValidateAuthenticationRequestAction.java | 21 +- .../idp/saml/sp/CloudServiceProvider.java | 32 +- .../saml/sp/SamlServiceProviderDocument.java | 128 +- .../saml/sp/SamlServiceProviderFactory.java | 27 +- .../idp/saml/sp/SamlServiceProviderIndex.java | 99 +- .../saml/sp/SamlServiceProviderResolver.java | 54 +- .../saml/sp/ServiceProviderCacheSettings.java | 14 +- .../idp/saml/sp/ServiceProviderDefaults.java | 24 +- .../idp/saml/sp/WildcardServiceProvider.java | 42 +- .../sp/WildcardServiceProviderResolver.java | 71 +- .../saml/support/SamlAuthenticationState.java | 14 +- .../xpack/idp/saml/support/SamlFactory.java | 99 +- .../xpack/idp/saml/support/SamlInit.java | 4 +- .../xpack/idp/saml/support/XmlValidator.java | 16 +- ...DeleteSamlServiceProviderRequestTests.java | 21 +- .../PutSamlServiceProviderRequestTests.java | 48 +- ...portPutSamlServiceProviderActionTests.java | 28 +- ...rtSamlInitiateSingleSignOnActionTests.java | 86 +- .../UserPrivilegeResolverTests.java | 24 +- ...ledAuthenticationResponseBuilderTests.java | 13 +- .../authn/SamlAuthnRequestValidatorTests.java | 147 +- ...enticationResponseMessageBuilderTests.java | 13 +- .../idp/SamlIdentityProviderBuilderTests.java | 103 +- .../saml/idp/SamlIdpMetadataBuilderTests.java | 14 +- .../saml/idp/SamlMetadataGeneratorTests.java | 11 +- .../rest/action/IdpBaseRestHandlerTests.java | 11 +- .../sp/SamlServiceProviderDocumentTests.java | 43 +- .../sp/SamlServiceProviderResolverTests.java | 7 +- .../saml/sp/SamlServiceProviderTestUtils.java | 12 +- .../WildcardServiceProviderResolverTests.java | 48 +- .../support/SamlAuthenticationStateTests.java | 22 +- .../saml/support/SamlObjectSignerTests.java | 9 +- .../xpack/idp/saml/test/IdpSamlTestCase.java | 15 +- .../xpack/ilm/CCRIndexLifecycleIT.java | 181 +- .../xpack/MigrateToDataTiersIT.java | 147 +- .../xpack/TimeSeriesRestDriver.java | 158 +- .../xpack/ilm/ChangePolicyforIndexIT.java | 91 +- .../xpack/ilm/ExplainLifecycleIT.java | 98 +- .../xpack/ilm/LifecycleLicenseIT.java | 53 +- .../xpack/ilm/TimeSeriesDataStreamsIT.java | 158 +- .../ilm/TimeSeriesLifecycleActionsIT.java | 657 +- .../xpack/ilm/TimeseriesMoveToStepIT.java | 408 +- .../xpack/ilm/actions/ReadonlyActionIT.java | 34 +- .../xpack/ilm/actions/RolloverActionIT.java | 192 +- .../xpack/ilm/actions/RollupActionIT.java | 22 +- .../actions/SearchableSnapshotActionIT.java | 334 +- .../xpack/ilm/actions/ShrinkActionIT.java | 175 +- .../xpack/slm/SnapshotLifecycleRestIT.java | 278 +- .../xpack/ilm/IndexLifecycleRestIT.java | 7 +- .../xpack/security/PermissionsIT.java | 139 +- .../ClusterStateWaitThresholdBreachTests.java | 63 +- .../xpack/ilm/DataTiersMigrationsTests.java | 43 +- .../xpack/ilm/ILMMultiNodeIT.java | 26 +- .../IndexLifecycleInitialisationTests.java | 206 +- .../xpack/ilm/UpdateSettingsStepTests.java | 42 +- .../slm/SLMSnapshotBlockingIntegTests.java | 242 +- .../SnapshotLifecycleInitialisationTests.java | 22 +- ...adataMigrateToDataTiersRoutingService.java | 213 +- .../xpack/ilm/ExecuteStepsUpdateTask.java | 86 +- .../xpack/ilm/IndexLifecycle.java | 285 +- .../IndexLifecycleClusterStateUpdateTask.java | 3 +- .../xpack/ilm/IndexLifecycleRunner.java | 274 +- .../xpack/ilm/IndexLifecycleService.java | 175 +- .../xpack/ilm/IndexLifecycleTransition.java | 220 +- .../IndexLifecycleUsageTransportAction.java | 35 +- .../ilm/LifecyclePolicySecurityClient.java | 9 +- .../xpack/ilm/MoveToErrorStepUpdateTask.java | 19 +- .../xpack/ilm/MoveToNextStepUpdateTask.java | 17 +- .../xpack/ilm/OperationModeUpdateTask.java | 20 +- .../xpack/ilm/PolicyStepsRegistry.java | 128 +- .../xpack/ilm/SetStepInfoUpdateTask.java | 14 +- .../ilm/action/RestGetLifecycleAction.java | 4 +- .../action/RestMigrateToDataTiersAction.java | 5 +- .../ilm/action/RestMoveToStepAction.java | 2 +- .../ilm/action/RestPutLifecycleAction.java | 2 +- .../RestRemoveIndexLifecyclePolicyAction.java | 7 +- .../TransportDeleteLifecycleAction.java | 83 +- .../TransportExplainLifecycleAction.java | 79 +- .../action/TransportGetLifecycleAction.java | 51 +- .../ilm/action/TransportGetStatusAction.java | 22 +- .../TransportMigrateToDataTiersAction.java | 75 +- .../ilm/action/TransportMoveToStepAction.java | 83 +- .../action/TransportPutLifecycleAction.java | 124 +- ...sportRemoveIndexLifecyclePolicyAction.java | 54 +- .../ilm/action/TransportRetryAction.java | 68 +- .../ilm/action/TransportStartILMAction.java | 21 +- .../ilm/action/TransportStopILMAction.java | 27 +- .../xpack/ilm/history/ILMHistoryItem.java | 40 +- .../xpack/ilm/history/ILMHistoryStore.java | 125 +- .../history/ILMHistoryTemplateRegistry.java | 12 +- .../elasticsearch/xpack/ilm/package-info.java | 1 - .../xpack/slm/SLMUsageTransportAction.java | 27 +- .../xpack/slm/SnapshotLifecycleService.java | 39 +- .../xpack/slm/SnapshotLifecycleTask.java | 149 +- .../xpack/slm/SnapshotRetentionService.java | 11 +- .../xpack/slm/SnapshotRetentionTask.java | 312 +- .../slm/UpdateSnapshotLifecycleStatsTask.java | 21 +- .../RestExecuteSnapshotLifecycleAction.java | 4 +- .../RestGetSnapshotLifecycleAction.java | 4 +- .../RestPutSnapshotLifecycleAction.java | 2 +- ...ransportDeleteSnapshotLifecycleAction.java | 71 +- ...ansportExecuteSnapshotLifecycleAction.java | 48 +- ...ansportExecuteSnapshotRetentionAction.java | 35 +- .../action/TransportGetSLMStatusAction.java | 31 +- .../TransportGetSnapshotLifecycleAction.java | 87 +- ...nsportGetSnapshotLifecycleStatsAction.java | 39 +- .../TransportPutSnapshotLifecycleAction.java | 56 +- .../slm/action/TransportStartSLMAction.java | 29 +- .../slm/action/TransportStopSLMAction.java | 29 +- ...MigrateToDataTiersRoutingServiceTests.java | 636 +- .../ilm/ExecuteStepsUpdateTaskTests.java | 113 +- ...ndexLifecycleInfoTransportActionTests.java | 25 +- .../ilm/IndexLifecycleMetadataTests.java | 93 +- .../xpack/ilm/IndexLifecycleRunnerTests.java | 431 +- .../xpack/ilm/IndexLifecycleServiceTests.java | 344 +- .../ilm/IndexLifecycleTransitionTests.java | 757 +- .../xpack/ilm/LifecyclePolicyClientTests.java | 27 +- .../xpack/ilm/LockableLifecycleType.java | 6 +- .../ilm/MoveToErrorStepUpdateTaskTests.java | 82 +- .../ilm/MoveToNextStepUpdateTaskTests.java | 96 +- .../ilm/OperationModeUpdateTaskTests.java | 33 +- .../xpack/ilm/PhaseStatsTests.java | 22 +- .../xpack/ilm/PolicyStatsTests.java | 18 +- .../xpack/ilm/PolicyStepsRegistryTests.java | 197 +- .../xpack/ilm/SetStepInfoUpdateTaskTests.java | 53 +- .../xpack/ilm/TimeValueScheduleTests.java | 14 +- .../TransportExplainLifecycleActionTests.java | 73 +- .../action/TransportStopILMActionTests.java | 19 +- .../ilm/history/ILMHistoryItemTests.java | 89 +- .../ilm/history/ILMHistoryStoreTests.java | 101 +- .../slm/SnapshotLifecyclePolicyTests.java | 194 +- .../slm/SnapshotLifecycleServiceTests.java | 172 +- .../xpack/slm/SnapshotLifecycleTaskTests.java | 325 +- .../slm/SnapshotRetentionServiceTests.java | 37 +- .../xpack/slm/SnapshotRetentionTaskTests.java | 373 +- .../ConstantKeywordFieldMapperTests.java | 50 +- .../mapper/ConstantKeywordFieldMapper.java | 82 +- .../ml/integration/MlBasicMultiNodeIT.java | 185 +- .../ml/integration/MlPluginDisabledIT.java | 2 +- .../smoketest/MlWithSecurityIT.java | 18 +- .../MlWithSecurityInsufficientRoleIT.java | 29 +- .../smoketest/MlWithSecurityUserRoleIT.java | 12 +- .../integration/AutodetectMemoryLimitIT.java | 23 +- .../xpack/ml/integration/AutoscalingIT.java | 111 +- .../integration/BasicRenormalizationIT.java | 23 +- .../ml/integration/BulkFailureRetryIT.java | 84 +- .../ml/integration/CategorizationIT.java | 271 +- .../ClassificationEvaluationIT.java | 595 +- ...lassificationEvaluationWithSecurityIT.java | 55 +- .../ml/integration/ClassificationIT.java | 553 +- .../DataFrameAnalysisCustomFeatureIT.java | 213 +- .../xpack/ml/integration/DatafeedJobsIT.java | 178 +- .../ml/integration/DatafeedJobsRestIT.java | 814 +- .../ml/integration/DatafeedWithAggsIT.java | 40 +- .../ml/integration/DelayedDataDetectorIT.java | 60 +- .../ml/integration/DeleteExpiredDataIT.java | 133 +- .../xpack/ml/integration/DeleteJobIT.java | 18 +- .../ml/integration/DetectionRulesIT.java | 36 +- .../ExplainDataFrameAnalyticsIT.java | 210 +- .../ExplainDataFrameAnalyticsRestIT.java | 71 +- .../xpack/ml/integration/ForecastIT.java | 109 +- .../ml/integration/InferenceIngestIT.java | 792 +- ...erimResultsDeletedAfterReopeningJobIT.java | 6 +- .../ml/integration/InterimResultsIT.java | 12 +- .../JobAndDatafeedResilienceIT.java | 99 +- .../MlDailyMaintenanceServiceIT.java | 56 +- .../MlInitializationServiceIT.java | 37 +- .../xpack/ml/integration/MlJobIT.java | 562 +- .../MlNativeAutodetectIntegTestCase.java | 121 +- ...NativeDataFrameAnalyticsIntegTestCase.java | 104 +- .../ml/integration/MlNativeIntegTestCase.java | 148 +- .../xpack/ml/integration/ModelPlotsIT.java | 18 +- .../integration/ModelSnapshotRetentionIT.java | 36 +- .../OutlierDetectionEvaluationIT.java | 42 +- .../OutlierDetectionWithMissingFieldsIT.java | 15 +- .../ml/integration/OverallBucketsIT.java | 17 +- .../xpack/ml/integration/PyTorchModelIT.java | 218 +- .../integration/RegressionEvaluationIT.java | 76 +- .../xpack/ml/integration/RegressionIT.java | 343 +- .../ml/integration/ReopenJobWithGapIT.java | 3 +- .../xpack/ml/integration/ResetJobIT.java | 7 +- .../ml/integration/RevertModelSnapshotIT.java | 79 +- .../integration/RunDataFrameAnalyticsIT.java | 237 +- .../ml/integration/ScheduledEventsIT.java | 153 +- .../ml/integration/SetUpgradeModeIT.java | 51 +- .../ml/integration/TestFeatureResetIT.java | 79 +- .../xpack/ml/integration/TrainedModelIT.java | 158 +- .../NamedPipeHelperNoBootstrapTests.java | 71 +- .../integration/InferencePipelineAggIT.java | 333 +- .../ml/integration/InferenceProcessorIT.java | 152 +- .../ml/integration/InferenceTestCase.java | 47 +- .../ml/transforms/PainlessDomainSplitIT.java | 211 +- .../license/MachineLearningLicensingIT.java | 264 +- .../ml/integration/AnnotationIndexIT.java | 22 +- .../ml/integration/AnomalyJobCRUDIT.java | 151 +- .../AutodetectResultProcessorIT.java | 182 +- .../integration/BasicDistributedJobsIT.java | 212 +- .../BucketCorrelationAggregationIT.java | 40 +- .../CategorizationAggregationIT.java | 81 +- .../ChunkedTrainedModelPersisterIT.java | 29 +- .../ChunkedTrainedModelRestorerIT.java | 119 +- .../integration/DataFrameAnalyticsCRUDIT.java | 60 +- .../DataFrameAnalyticsConfigProviderIT.java | 165 +- .../integration/DatafeedConfigProviderIT.java | 250 +- .../ml/integration/EstablishedMemUsageIT.java | 43 +- .../xpack/ml/integration/IndexLayoutIT.java | 123 +- .../ml/integration/JobConfigProviderIT.java | 250 +- .../integration/JobModelSnapshotCRUDIT.java | 74 +- .../ml/integration/JobResultsProviderIT.java | 336 +- .../integration/JobStorageDeletionTaskIT.java | 125 +- .../ml/integration/MlAutoUpdateServiceIT.java | 85 +- .../ml/integration/MlConfigMigratorIT.java | 169 +- .../integration/MlDistributedFailureIT.java | 205 +- .../xpack/ml/integration/MlFiltersIT.java | 3 +- .../ml/integration/MlNodeShutdownIT.java | 169 +- .../integration/ModelInferenceActionIT.java | 382 +- .../ml/integration/NetworkDisruptionIT.java | 22 +- .../integration/PyTorchStateStreamerIT.java | 43 +- .../TestFeatureLicenseTrackingIT.java | 72 +- .../xpack/ml/integration/TooManyJobsIT.java | 122 +- .../ml/integration/TrainedModelCRUDIT.java | 65 +- .../integration/TrainedModelProviderIT.java | 112 +- .../ml/integration/UnusedStatsRemoverIT.java | 126 +- .../xpack/ml/InvalidLicenseEnforcer.java | 8 +- .../xpack/ml/MachineLearning.java | 1127 +- .../MachineLearningInfoTransportAction.java | 8 +- .../ml/MachineLearningPainlessExtension.java | 6 +- .../MachineLearningUsageTransportAction.java | 264 +- .../xpack/ml/MlAssignmentNotifier.java | 65 +- .../xpack/ml/MlAutoUpdateService.java | 7 +- .../ml/MlConfigMigrationEligibilityCheck.java | 13 +- .../xpack/ml/MlConfigMigrator.java | 215 +- .../xpack/ml/MlDailyMaintenanceService.java | 153 +- .../xpack/ml/MlIndexTemplateRegistry.java | 56 +- .../xpack/ml/MlInitializationService.java | 195 +- .../xpack/ml/MlLifeCycleService.java | 18 +- .../xpack/ml/MlUpgradeModeActionFilter.java | 21 +- .../ml/action/TransportCloseJobAction.java | 419 +- .../action/TransportDeleteCalendarAction.java | 43 +- .../TransportDeleteCalendarEventAction.java | 120 +- ...ansportDeleteDataFrameAnalyticsAction.java | 90 +- .../action/TransportDeleteDatafeedAction.java | 85 +- .../TransportDeleteExpiredDataAction.java | 201 +- .../action/TransportDeleteFilterAction.java | 62 +- .../action/TransportDeleteForecastAction.java | 132 +- .../ml/action/TransportDeleteJobAction.java | 248 +- .../TransportDeleteModelSnapshotAction.java | 102 +- .../TransportDeleteTrainedModelAction.java | 99 +- ...ransportDeleteTrainedModelAliasAction.java | 13 +- .../TransportEstimateModelMemoryAction.java | 83 +- .../TransportEvaluateDataFrameAction.java | 80 +- ...nsportExplainDataFrameAnalyticsAction.java | 110 +- .../TransportFinalizeJobExecutionAction.java | 61 +- .../ml/action/TransportFlushJobAction.java | 31 +- .../ml/action/TransportForecastJobAction.java | 138 +- .../ml/action/TransportGetBucketsAction.java | 59 +- .../TransportGetCalendarEventsAction.java | 105 +- .../action/TransportGetCalendarsAction.java | 18 +- .../action/TransportGetCategoriesAction.java | 33 +- .../TransportGetDataFrameAnalyticsAction.java | 47 +- ...sportGetDataFrameAnalyticsStatsAction.java | 258 +- ...ransportGetDatafeedRunningStateAction.java | 65 +- .../action/TransportGetDatafeedsAction.java | 43 +- .../TransportGetDatafeedsStatsAction.java | 91 +- .../TransportGetDeploymentStatsAction.java | 173 +- .../ml/action/TransportGetFiltersAction.java | 35 +- .../action/TransportGetInfluencersAction.java | 45 +- .../ml/action/TransportGetJobsAction.java | 68 +- .../action/TransportGetJobsStatsAction.java | 137 +- .../TransportGetModelSnapshotsAction.java | 35 +- .../TransportGetOverallBucketsAction.java | 235 +- .../ml/action/TransportGetRecordsAction.java | 42 +- .../TransportGetTrainedModelsAction.java | 88 +- .../TransportGetTrainedModelsStatsAction.java | 175 +- ...portInferTrainedModelDeploymentAction.java | 58 +- .../TransportInternalInferModelAction.java | 154 +- .../TransportIsolateDatafeedAction.java | 41 +- .../ml/action/TransportJobTaskAction.java | 41 +- .../ml/action/TransportKillProcessAction.java | 59 +- .../ml/action/TransportMlInfoAction.java | 48 +- .../ml/action/TransportOpenJobAction.java | 291 +- .../ml/action/TransportPersistJobAction.java | 20 +- .../TransportPostCalendarEventsAction.java | 96 +- .../ml/action/TransportPostDataAction.java | 24 +- ...nsportPreviewDataFrameAnalyticsAction.java | 11 +- .../TransportPreviewDatafeedAction.java | 84 +- .../ml/action/TransportPutCalendarAction.java | 49 +- .../TransportPutDataFrameAnalyticsAction.java | 143 +- .../ml/action/TransportPutDatafeedAction.java | 42 +- .../ml/action/TransportPutFilterAction.java | 40 +- .../ml/action/TransportPutJobAction.java | 119 +- .../TransportPutTrainedModelAction.java | 281 +- .../TransportPutTrainedModelAliasAction.java | 143 +- ...rtPutTrainedModelDefinitionPartAction.java | 2 +- ...nsportPutTrainedModelVocabularyAction.java | 5 +- .../ml/action/TransportResetJobAction.java | 262 +- .../TransportRevertModelSnapshotAction.java | 253 +- .../action/TransportSetResetModeAction.java | 24 +- .../action/TransportSetUpgradeModeAction.java | 314 +- ...ransportStartDataFrameAnalyticsAction.java | 607 +- .../action/TransportStartDatafeedAction.java | 535 +- ...portStartTrainedModelDeploymentAction.java | 238 +- ...TransportStopDataFrameAnalyticsAction.java | 320 +- .../action/TransportStopDatafeedAction.java | 468 +- ...sportStopTrainedModelDeploymentAction.java | 263 +- .../TransportUpdateCalendarJobAction.java | 21 +- ...ansportUpdateDataFrameAnalyticsAction.java | 81 +- .../action/TransportUpdateDatafeedAction.java | 43 +- .../action/TransportUpdateFilterAction.java | 73 +- .../ml/action/TransportUpdateJobAction.java | 32 +- .../TransportUpdateModelSnapshotAction.java | 57 +- .../action/TransportUpdateProcessAction.java | 47 +- ...ransportUpgradeJobModelSnapshotAction.java | 256 +- .../TransportValidateJobConfigAction.java | 3 +- .../xpack/ml/aggs/DoubleArray.java | 2 +- .../xpack/ml/aggs/MlAggsHelper.java | 16 +- .../CategorizationTokenTree.java | 1 - .../CategorizeTextAggregationBuilder.java | 8 +- .../CategorizeTextAggregator.java | 8 +- .../InternalCategorizationAggregation.java | 31 +- .../categorization/TextCategorization.java | 3 +- .../ml/aggs/categorization/TreeNode.java | 7 +- .../UnmappedCategorizationAggregation.java | 1 - .../BucketCorrelationAggregationBuilder.java | 18 +- .../BucketCorrelationAggregator.java | 16 +- .../aggs/correlation/CorrelationFunction.java | 1 - .../CorrelationNamedContentProvider.java | 2 +- .../correlation/CountCorrelationFunction.java | 20 +- .../CountCorrelationIndicator.java | 15 +- .../heuristic/LongBinomialDistribution.java | 26 +- .../heuristic/MlChiSquaredDistribution.java | 4 +- .../xpack/ml/aggs/heuristic/PValueScore.java | 43 +- .../InferencePipelineAggregationBuilder.java | 157 +- .../InferencePipelineAggregator.java | 44 +- .../InternalInferenceAggregation.java | 8 +- .../BucketCountKSTestAggregationBuilder.java | 13 +- .../kstest/BucketCountKSTestAggregator.java | 2 +- .../kstest/InternalKSTestAggregation.java | 2 +- .../ml/annotations/AnnotationPersister.java | 10 +- .../MlAutoscalingDeciderService.java | 472 +- .../MlAutoscalingNamedWritableProvider.java | 6 +- .../xpack/ml/autoscaling/MlScalingReason.java | 47 +- .../ml/autoscaling/NativeMemoryCapacity.java | 25 +- .../datafeed/DatafeedConfigAutoUpdater.java | 37 +- .../ml/datafeed/DatafeedContextProvider.java | 42 +- .../xpack/ml/datafeed/DatafeedJob.java | 117 +- .../xpack/ml/datafeed/DatafeedJobBuilder.java | 106 +- .../xpack/ml/datafeed/DatafeedManager.java | 296 +- .../ml/datafeed/DatafeedNodeSelector.java | 86 +- .../xpack/ml/datafeed/DatafeedRunner.java | 322 +- .../datafeed/DatafeedTimingStatsReporter.java | 7 +- .../DatafeedDelayedDataDetector.java | 36 +- .../DelayedDataDetectorFactory.java | 38 +- .../extractor/DataExtractorFactory.java | 151 +- .../AbstractAggregationDataExtractor.java | 26 +- .../AggregatedSearchRequestBuilder.java | 1 - .../aggregation/AggregationDataExtractor.java | 8 +- .../AggregationDataExtractorContext.java | 17 +- .../AggregationDataExtractorFactory.java | 52 +- .../AggregationToJsonProcessor.java | 82 +- .../CompositeAggregationDataExtractor.java | 64 +- ...positeAggregationDataExtractorContext.java | 28 +- ...positeAggregationDataExtractorFactory.java | 41 +- .../aggregation/RollupDataExtractor.java | 5 +- .../RollupDataExtractorFactory.java | 88 +- .../chunked/ChunkedDataExtractor.java | 33 +- .../chunked/ChunkedDataExtractorContext.java | 21 +- .../chunked/ChunkedDataExtractorFactory.java | 48 +- .../extractor/scroll/ScrollDataExtractor.java | 46 +- .../scroll/ScrollDataExtractorContext.java | 16 +- .../scroll/ScrollDataExtractorFactory.java | 89 +- .../scroll/SearchHitToJsonProcessor.java | 2 +- .../scroll/TimeBasedExtractedFields.java | 19 +- .../persistence/DatafeedConfigProvider.java | 328 +- .../dataframe/DataFrameAnalyticsManager.java | 207 +- .../ml/dataframe/DataFrameAnalyticsTask.java | 218 +- .../xpack/ml/dataframe/DestinationIndex.java | 172 +- .../xpack/ml/dataframe/MappingsMerger.java | 35 +- .../xpack/ml/dataframe/StoredProgress.java | 7 +- .../extractor/DataFrameDataExtractor.java | 89 +- .../DataFrameDataExtractorContext.java | 15 +- .../DataFrameDataExtractorFactory.java | 107 +- .../extractor/ExtractedFieldsDetector.java | 270 +- .../ExtractedFieldsDetectorFactory.java | 124 +- .../dataframe/inference/InferenceRunner.java | 75 +- .../dataframe/inference/TestDocsIterator.java | 14 +- .../DataFrameAnalyticsConfigProvider.java | 250 +- .../DataFrameAnalyticsDeleter.java | 128 +- .../AbstractNativeAnalyticsProcess.java | 15 +- .../dataframe/process/AnalysisFieldInfo.java | 4 +- .../dataframe/process/AnalyticsBuilder.java | 15 +- .../process/AnalyticsProcessConfig.java | 13 +- .../process/AnalyticsProcessFactory.java | 10 +- .../process/AnalyticsProcessManager.java | 178 +- .../process/AnalyticsResultProcessor.java | 22 +- .../process/ChunkedTrainedModelPersister.java | 241 +- .../process/DataFrameRowsJoiner.java | 14 +- .../MemoryUsageEstimationProcessManager.java | 101 +- .../process/NativeAnalyticsProcess.java | 35 +- .../NativeAnalyticsProcessFactory.java | 82 +- .../NativeMemoryUsageEstimationProcess.java | 23 +- ...veMemoryUsageEstimationProcessFactory.java | 54 +- .../process/results/AnalyticsResult.java | 61 +- .../results/MemoryUsageEstimationResult.java | 20 +- .../process/results/ModelMetadata.java | 13 +- .../dataframe/process/results/RowResults.java | 8 +- .../results/TrainedModelDefinitionChunk.java | 12 +- .../ml/dataframe/stats/DataCountsTracker.java | 7 +- .../xpack/ml/dataframe/stats/StatsHolder.java | 8 +- .../ml/dataframe/stats/StatsPersister.java | 11 +- .../steps/AbstractDataFrameAnalyticsStep.java | 31 +- .../ml/dataframe/steps/AnalysisStep.java | 24 +- .../steps/DataFrameAnalyticsStep.java | 5 +- .../xpack/ml/dataframe/steps/FinalStep.java | 14 +- .../ml/dataframe/steps/InferenceStep.java | 99 +- .../ml/dataframe/steps/ReindexingStep.java | 293 +- .../AbstractReservoirTrainTestSplitter.java | 3 +- ...SingleClassReservoirTrainTestSplitter.java | 9 +- .../StratifiedTrainTestSplitter.java | 9 +- .../TrainTestSplitterFactory.java | 42 +- .../xpack/ml/extractor/ExtractedField.java | 4 +- .../xpack/ml/extractor/ExtractedFields.java | 73 +- .../xpack/ml/extractor/GeoShapeField.java | 2 +- .../xpack/ml/extractor/SourceField.java | 2 +- .../xpack/ml/extractor/TimeField.java | 4 +- .../ml/inference/ModelAliasMetadata.java | 14 +- .../inference/TrainedModelStatsService.java | 105 +- .../TrainedModelAllocationClusterService.java | 27 +- .../TrainedModelAllocationMetadata.java | 8 +- .../TrainedModelAllocationNodeService.java | 116 +- .../TrainedModelAllocationService.java | 2 +- .../deployment/DeploymentManager.java | 188 +- .../inference/deployment/PyTorchResult.java | 26 +- .../TrainedModelDeploymentTask.java | 1 - .../inference/ingest/InferenceProcessor.java | 140 +- .../inference/loadingservice/LocalModel.java | 81 +- .../loadingservice/ModelLoadingService.java | 326 +- .../inference/modelsize/EnsembleSizeInfo.java | 43 +- .../modelsize/FrequencyEncodingSize.java | 11 +- .../MlModelSizeNamedXContentProvider.java | 18 +- .../ml/inference/modelsize/ModelSizeInfo.java | 18 +- .../modelsize/OneHotEncodingSize.java | 10 +- .../inference/modelsize/PreprocessorSize.java | 1 - .../modelsize/SizeEstimatorHelper.java | 2 +- .../modelsize/TargetMeanEncodingSize.java | 8 +- .../modelsize/TrainedModelSizeInfo.java | 3 +- .../ml/inference/modelsize/TreeSizeInfo.java | 9 +- .../ml/inference/nlp/BertRequestBuilder.java | 11 +- .../ml/inference/nlp/FillMaskProcessor.java | 11 +- .../xpack/ml/inference/nlp/NerProcessor.java | 15 +- .../xpack/ml/inference/nlp/NlpHelpers.java | 5 +- .../xpack/ml/inference/nlp/NlpTask.java | 26 +- .../xpack/ml/inference/nlp/TaskType.java | 2 +- .../nlp/TextClassificationProcessor.java | 6 +- .../xpack/ml/inference/nlp/Vocabulary.java | 9 +- .../nlp/ZeroShotClassificationProcessor.java | 8 +- .../nlp/tokenizers/BasicTokenizer.java | 50 +- .../nlp/tokenizers/BertTokenizer.java | 38 +- .../nlp/tokenizers/WordPieceTokenizer.java | 2 +- .../nlp/tokenizers/WordPieceVocabulary.java | 8 +- .../ChunkedTrainedModelRestorer.java | 180 +- .../TrainedModelDefinitionDoc.java | 52 +- .../persistence/TrainedModelProvider.java | 1160 +- .../pytorch/process/NativePyTorchProcess.java | 10 +- .../process/NativePyTorchProcessFactory.java | 27 +- .../pytorch/process/PyTorchBuilder.java | 5 +- .../process/PyTorchResultProcessor.java | 41 +- .../pytorch/process/PyTorchStateStreamer.java | 33 +- .../xpack/ml/job/JobManager.java | 492 +- .../xpack/ml/job/JobNodeSelector.java | 132 +- .../elasticsearch/xpack/ml/job/NodeLoad.java | 57 +- .../xpack/ml/job/NodeLoadDetector.java | 52 +- .../ml/job/UpdateJobProcessNotifier.java | 76 +- .../categorization/AbstractMlTokenizer.java | 3 +- .../CategorizationAnalyzer.java | 13 +- .../categorization/GrokPatternCreator.java | 169 +- .../categorization/MlClassicTokenizer.java | 8 +- .../MlClassicTokenizerFactory.java | 1 - .../categorization/MlStandardTokenizer.java | 13 +- .../persistence/BatchedBucketsIterator.java | 10 +- .../BatchedInfluencersIterator.java | 10 +- .../persistence/BatchedRecordsIterator.java | 10 +- .../job/persistence/BucketsQueryBuilder.java | 12 +- .../job/persistence/CalendarQueryBuilder.java | 4 +- .../persistence/InfluencersQueryBuilder.java | 18 +- .../ml/job/persistence/JobConfigProvider.java | 514 +- .../persistence/JobDataCountsPersister.java | 18 +- .../ml/job/persistence/JobDataDeleter.java | 414 +- .../JobRenormalizedResultsPersister.java | 2 - .../job/persistence/JobResultsPersister.java | 171 +- .../job/persistence/JobResultsProvider.java | 1656 +- .../job/persistence/RecordsQueryBuilder.java | 43 +- .../job/persistence/ResultsFilterBuilder.java | 2 +- .../ScheduledEventsQueryBuilder.java | 1 - .../persistence/SearchAfterJobsIterator.java | 18 +- .../ml/job/persistence/StateStreamer.java | 19 +- .../OverallBucketsAggregator.java | 3 +- .../OverallBucketsProcessor.java | 2 + .../OverallBucketsProvider.java | 11 +- .../ml/job/process/DataCountsReporter.java | 14 +- .../process/ProcessWorkerExecutorService.java | 2 +- .../process/autodetect/AutodetectBuilder.java | 69 +- .../autodetect/AutodetectCommunicator.java | 66 +- .../autodetect/AutodetectProcessFactory.java | 22 +- .../autodetect/AutodetectProcessManager.java | 499 +- .../BlackHoleAutodetectProcess.java | 76 +- .../autodetect/JobModelSnapshotUpgrader.java | 172 +- .../autodetect/NativeAutodetectProcess.java | 14 +- .../NativeAutodetectProcessFactory.java | 88 +- .../process/autodetect/ProcessContext.java | 6 +- .../job/process/autodetect/UpdateParams.java | 37 +- .../autodetect/UpdateProcessMessage.java | 43 +- .../output/AutodetectResultProcessor.java | 124 +- .../autodetect/output/FlushListener.java | 2 +- .../JobSnapshotUpgraderResultProcessor.java | 44 +- .../autodetect/params/AutodetectParams.java | 29 +- .../autodetect/params/DataLoadParams.java | 1 - .../autodetect/params/FlushJobParams.java | 27 +- .../autodetect/params/ForecastParams.java | 35 +- .../process/autodetect/params/TimeRange.java | 8 +- .../writer/AbstractDataToProcessWriter.java | 45 +- .../writer/AutodetectControlMsgWriter.java | 3 +- .../writer/DataToProcessWriter.java | 10 +- .../writer/JsonDataToProcessWriter.java | 51 +- .../writer/XContentRecordReader.java | 6 +- .../diagnostics/DataStreamDiagnostics.java | 9 +- .../BucketInfluencerNormalizable.java | 4 +- .../normalizer/BucketNormalizable.java | 7 +- .../ml/job/process/normalizer/Level.java | 1 - .../MultiplyingNormalizerProcess.java | 3 +- .../normalizer/NativeNormalizerProcess.java | 3 +- .../NativeNormalizerProcessFactory.java | 32 +- .../job/process/normalizer/Normalizable.java | 5 +- .../ml/job/process/normalizer/Normalizer.java | 46 +- .../process/normalizer/NormalizerResult.java | 40 +- .../normalizer/RecordNormalizable.java | 1 - .../job/process/normalizer/ScoresUpdater.java | 61 +- .../ShortCircuitingRenormalizer.java | 20 +- .../output/NormalizerResultHandler.java | 8 +- .../ml/job/results/AutodetectResult.java | 114 +- .../AbstractExpiredJobDataRemover.java | 42 +- .../job/retention/EmptyStateIndexRemover.java | 87 +- .../retention/ExpiredAnnotationsRemover.java | 42 +- .../retention/ExpiredForecastsRemover.java | 58 +- .../ExpiredModelSnapshotsRemover.java | 127 +- .../job/retention/ExpiredResultsRemover.java | 91 +- .../xpack/ml/job/retention/MlDataRemover.java | 2 +- .../ml/job/retention/UnusedStateRemover.java | 53 +- .../ml/job/retention/UnusedStatsRemover.java | 53 +- .../upgrader/SnapshotUpgradePredicate.java | 15 +- .../upgrader/SnapshotUpgradeTaskExecutor.java | 276 +- .../upgrader/SnapshotUpgradeTaskParams.java | 12 +- .../xpack/ml/job/task/JobTask.java | 6 +- .../task/OpenJobPersistentTasksExecutor.java | 428 +- .../ml/process/AbstractNativeProcess.java | 14 +- .../xpack/ml/process/ControllerResponse.java | 10 +- .../ml/process/IndexingStateProcessor.java | 66 +- .../xpack/ml/process/MlMemoryTracker.java | 188 +- .../xpack/ml/process/NativeController.java | 61 +- .../xpack/ml/process/ProcessPipes.java | 22 +- .../ml/process/ProcessResultsParser.java | 6 +- .../process/StateToProcessWriterHelper.java | 2 +- .../ml/process/logging/CppLogMessage.java | 25 +- .../process/logging/CppLogMessageHandler.java | 94 +- .../process/writer/LengthEncodedWriter.java | 3 - .../ml/rest/RestDeleteExpiredDataAction.java | 17 +- .../xpack/ml/rest/RestMlInfoAction.java | 5 +- .../ml/rest/RestSetUpgradeModeAction.java | 6 +- .../calendar/RestDeleteCalendarAction.java | 3 +- .../RestDeleteCalendarEventAction.java | 9 +- .../calendar/RestDeleteCalendarJobAction.java | 6 +- .../calendar/RestGetCalendarEventsAction.java | 13 +- .../rest/calendar/RestGetCalendarsAction.java | 24 +- .../calendar/RestPostCalendarEventAction.java | 8 +- .../rest/calendar/RestPutCalendarAction.java | 6 +- .../calendar/RestPutCalendarJobAction.java | 6 +- .../cat/RestCatDataFrameAnalyticsAction.java | 110 +- .../ml/rest/cat/RestCatDatafeedsAction.java | 96 +- .../xpack/ml/rest/cat/RestCatJobsAction.java | 481 +- .../rest/cat/RestCatTrainedModelsAction.java | 270 +- .../datafeeds/RestDeleteDatafeedAction.java | 3 +- .../datafeeds/RestGetDatafeedStatsAction.java | 12 +- .../datafeeds/RestGetDatafeedsAction.java | 12 +- .../datafeeds/RestPreviewDatafeedAction.java | 20 +- .../rest/datafeeds/RestPutDatafeedAction.java | 5 +- .../datafeeds/RestStartDatafeedAction.java | 34 +- .../datafeeds/RestStopDatafeedAction.java | 19 +- .../datafeeds/RestUpdateDatafeedAction.java | 5 +- .../RestDeleteDataFrameAnalyticsAction.java | 4 +- .../RestExplainDataFrameAnalyticsAction.java | 44 +- .../RestGetDataFrameAnalyticsAction.java | 16 +- .../RestGetDataFrameAnalyticsStatsAction.java | 12 +- ...estPostDataFrameAnalyticsUpdateAction.java | 6 +- .../RestPreviewDataFrameAnalyticsAction.java | 12 +- .../RestPutDataFrameAnalyticsAction.java | 6 +- .../RestStartDataFrameAnalyticsAction.java | 10 +- .../RestStopDataFrameAnalyticsAction.java | 14 +- .../rest/filter/RestDeleteFilterAction.java | 3 +- .../ml/rest/filter/RestGetFiltersAction.java | 13 +- .../ml/rest/filter/RestPutFilterAction.java | 5 +- .../rest/filter/RestUpdateFilterAction.java | 7 +- .../RestDeleteTrainedModelAction.java | 3 +- ...tGetTrainedModelDeploymentStatsAction.java | 15 +- .../inference/RestGetTrainedModelsAction.java | 53 +- .../RestGetTrainedModelsStatsAction.java | 14 +- ...RestInferTrainedModelDeploymentAction.java | 16 +- .../inference/RestPutTrainedModelAction.java | 5 +- ...stPutTrainedModelDefinitionPartAction.java | 2 +- .../RestPutTrainedModelVocabularyAction.java | 10 +- ...RestStartTrainedModelDeploymentAction.java | 23 +- .../RestStopTrainedModelDeploymentAction.java | 4 +- .../xpack/ml/rest/job/RestCloseJobAction.java | 20 +- .../ml/rest/job/RestDeleteForecastAction.java | 11 +- .../ml/rest/job/RestDeleteJobAction.java | 5 +- .../job/RestEstimateModelMemoryAction.java | 5 +- .../xpack/ml/rest/job/RestFlushJobAction.java | 10 +- .../ml/rest/job/RestForecastJobAction.java | 5 +- .../ml/rest/job/RestGetJobStatsAction.java | 11 +- .../xpack/ml/rest/job/RestGetJobsAction.java | 11 +- .../xpack/ml/rest/job/RestOpenJobAction.java | 11 +- .../xpack/ml/rest/job/RestPostDataAction.java | 4 +- .../ml/rest/job/RestPostJobUpdateAction.java | 5 +- .../xpack/ml/rest/job/RestPutJobAction.java | 5 +- .../xpack/ml/rest/job/RestResetJobAction.java | 6 +- .../RestDeleteModelSnapshotAction.java | 15 +- .../RestGetModelSnapshotsAction.java | 31 +- .../RestRevertModelSnapshotAction.java | 18 +- .../RestUpdateModelSnapshotAction.java | 26 +- .../RestUpgradeJobModelSnapshotAction.java | 19 +- .../ml/rest/results/RestGetBucketsAction.java | 41 +- .../rest/results/RestGetCategoriesAction.java | 41 +- .../results/RestGetInfluencersAction.java | 35 +- .../results/RestGetOverallBucketsAction.java | 18 +- .../ml/rest/results/RestGetRecordsAction.java | 38 +- .../validate/RestValidateDetectorAction.java | 8 +- .../validate/RestValidateJobConfigAction.java | 8 +- .../AbstractJobPersistentTasksExecutor.java | 91 +- .../xpack/ml/utils/DomainSplitFunction.java | 55 +- .../xpack/ml/utils/MlIndicesUtils.java | 12 +- .../xpack/ml/utils/NamedPipeHelper.java | 13 +- .../ml/utils/NativeMemoryCalculator.java | 48 +- .../xpack/ml/utils/QueryBuilderHelper.java | 2 +- .../ml/utils/TypedChainTaskExecutor.java | 12 +- .../xpack/ml/utils/VoidChainTaskExecutor.java | 8 +- .../persistence/BatchedDocumentsIterator.java | 9 +- .../persistence/LimitAwareBulkIndexer.java | 8 +- .../ml/utils/persistence/MlParserUtils.java | 10 +- .../persistence/ResultsPersisterService.java | 294 +- .../SearchAfterDocumentsIterator.java | 5 +- .../xpack/ml/LocalStateMachineLearning.java | 28 +- ...chineLearningInfoTransportActionTests.java | 330 +- .../xpack/ml/MachineLearningTests.java | 57 +- .../xpack/ml/MlAssignmentNotifierTests.java | 143 +- ...lConfigMigrationEligibilityCheckTests.java | 159 +- .../xpack/ml/MlConfigMigratorTests.java | 271 +- .../ml/MlDailyMaintenanceServiceTests.java | 112 +- .../ml/MlIndexTemplateRegistryTests.java | 57 +- .../ml/MlInitializationServiceTests.java | 28 +- .../xpack/ml/MlLifeCycleServiceTests.java | 163 +- .../xpack/ml/MlMetadataTests.java | 69 +- .../xpack/ml/MlSingleNodeTestCase.java | 39 +- .../ml/MlUpgradeModeActionFilterTests.java | 14 +- .../action/TransportCloseJobActionTests.java | 92 +- ...TransportDeleteExpiredDataActionTests.java | 34 +- .../TransportDeleteForecastActionTests.java | 35 +- ...ansportEstimateModelMemoryActionTests.java | 192 +- ...nsportFinalizeJobExecutionActionTests.java | 21 +- ...ransportForecastJobActionRequestTests.java | 83 +- .../TransportGetJobsStatsActionTests.java | 99 +- ...sportGetTrainedModelsStatsActionTests.java | 241 +- .../ml/action/TransportMlInfoActionTests.java | 98 +- .../TransportPreviewDatafeedActionTests.java | 6 +- ...ortStartDataFrameAnalyticsActionTests.java | 107 +- .../TransportStartDatafeedActionTests.java | 63 +- ...portStopDataFrameAnalyticsActionTests.java | 41 +- .../TransportStopDatafeedActionTests.java | 48 +- .../xpack/ml/aggs/DoubleArrayTests.java | 15 +- .../CategorizeTextAggregatorTests.java | 107 +- ...nternalCategorizationAggregationTests.java | 4 +- .../categorization/ParsedCategorization.java | 6 +- ...ketCorrelationAggregationBuilderTests.java | 8 +- .../CountCorrelationFunctionTests.java | 17 +- .../CountCorrelationIndicatorTests.java | 8 +- .../MlChiSquaredDistributionTests.java | 17 +- .../ml/aggs/heuristic/PValueScoreTests.java | 138 +- ...erencePipelineAggregationBuilderTests.java | 30 +- .../InternalInferenceAggregationTests.java | 58 +- .../ml/aggs/inference/ParsedInference.java | 63 +- .../BucketCountKSTestAggregatorTests.java | 35 +- .../InternalKSTestAggregationTests.java | 7 +- .../xpack/ml/aggs/kstest/ParsedKSTest.java | 5 +- .../annotations/AnnotationPersisterTests.java | 35 +- .../MlAutoscalingDeciderServiceTests.java | 375 +- .../ml/autoscaling/MlScalingReasonTests.java | 3 +- .../NativeMemoryCapacityTests.java | 40 +- .../DatafeedConfigAutoUpdaterTests.java | 77 +- .../ml/datafeed/DatafeedJobBuilderTests.java | 91 +- .../xpack/ml/datafeed/DatafeedJobTests.java | 154 +- .../datafeed/DatafeedJobValidatorTests.java | 83 +- .../datafeed/DatafeedNodeSelectorTests.java | 523 +- .../ml/datafeed/DatafeedRunnerTests.java | 104 +- .../DatafeedTimingStatsReporterTests.java | 93 +- .../ml/datafeed/ProblemTrackerTests.java | 7 +- .../DelayedDataDetectorFactoryTests.java | 41 +- .../extractor/DataExtractorFactoryTests.java | 396 +- .../AggregationDataExtractorFactoryTests.java | 16 +- .../AggregationDataExtractorTests.java | 97 +- .../aggregation/AggregationTestUtils.java | 22 +- .../AggregationToJsonProcessorTests.java | 570 +- ...ompositeAggregationDataExtractorTests.java | 86 +- .../ChunkedDataExtractorFactoryTests.java | 16 +- .../chunked/ChunkedDataExtractorTests.java | 67 +- .../scroll/ScrollDataExtractorTests.java | 158 +- .../scroll/SearchHitToJsonProcessorTests.java | 48 +- .../scroll/TimeBasedExtractedFieldsTests.java | 52 +- .../DataFrameAnalyticsTaskTests.java | 144 +- .../ml/dataframe/DestinationIndexTests.java | 158 +- .../ml/dataframe/MappingsMergerTests.java | 39 +- .../ml/dataframe/StoredProgressTests.java | 8 +- .../DataFrameDataExtractorTests.java | 294 +- .../ExtractedFieldsDetectorTests.java | 983 +- .../inference/InferenceRunnerTests.java | 63 +- .../process/AnalyticsBuilderTests.java | 4 +- .../process/AnalyticsProcessConfigTests.java | 67 +- .../process/AnalyticsProcessManagerTests.java | 152 +- .../AnalyticsResultProcessorTests.java | 61 +- .../ChunkedTrainedModelPersisterTests.java | 44 +- .../DataFrameAnalyticsManagerTests.java | 24 +- .../process/DataFrameRowsJoinerTests.java | 35 +- ...oryUsageEstimationProcessManagerTests.java | 13 +- .../process/results/AnalyticsResultTests.java | 22 +- .../MemoryUsageEstimationResultTests.java | 11 +- .../process/results/RowResultsTests.java | 2 +- .../dataframe/stats/ProgressTrackerTests.java | 74 +- .../ml/dataframe/stats/StatsHolderTests.java | 49 +- .../StratifiedTrainTestSplitterTests.java | 14 +- .../ml/extractor/ExtractedFieldsTests.java | 53 +- .../ml/extractor/GeoPointFieldTests.java | 2 +- .../ml/extractor/GeoShapeFieldTests.java | 9 +- .../ml/extractor/ProcessedFieldTests.java | 61 +- .../xpack/ml/extractor/TimeFieldTests.java | 6 +- .../TrainedModelStatsServiceTests.java | 129 +- ...nedModelAllocationClusterServiceTests.java | 81 +- .../TrainedModelAllocationMetadataTests.java | 5 +- ...rainedModelAllocationNodeServiceTests.java | 9 +- .../deployment/DeploymentManagerTests.java | 4 +- .../deployment/PyTorchResultTests.java | 10 +- .../InferenceProcessorFactoryTests.java | 435 +- .../ingest/InferenceProcessorTests.java | 313 +- .../loadingservice/LocalModelTests.java | 223 +- .../ModelLoadingServiceTests.java | 272 +- .../modelsize/EnsembleSizeInfoTests.java | 12 +- .../modelsize/FrequencyEncodingSizeTests.java | 14 +- .../modelsize/ModelSizeInfoTests.java | 143 +- .../modelsize/OneHotEncodingSizeTests.java | 15 +- .../modelsize/SizeEstimatorTestCase.java | 16 +- .../TargetMeanEncodingSizeTests.java | 14 +- .../modelsize/TreeSizeInfoTests.java | 14 +- .../nlp/BertRequestBuilderTests.java | 49 +- .../inference/nlp/FillMaskProcessorTests.java | 35 +- .../ml/inference/nlp/NerProcessorTests.java | 33 +- .../ml/inference/nlp/NlpHelpersTests.java | 23 +- .../xpack/ml/inference/nlp/NlpTaskTests.java | 12 +- .../nlp/TextClassificationProcessorTests.java | 26 +- .../ZeroShotClassificationProcessorTests.java | 33 +- .../nlp/tokenizers/BasicTokenizerTests.java | 6 +- .../nlp/tokenizers/BertTokenizerTests.java | 42 +- .../tokenizers/WordPieceTokenizerTests.java | 18 +- .../TrainedModelDefinitionDocTests.java | 25 +- .../TrainedModelProviderTests.java | 127 +- .../pytorch/process/PyTorchBuilderTests.java | 15 +- .../LangIdentNeuralNetworkInferenceTests.java | 17 +- .../xpack/ml/job/JobManagerTests.java | 267 +- .../xpack/ml/job/JobNodeSelectorTests.java | 747 +- .../xpack/ml/job/NodeLoadDetectorTests.java | 109 +- .../CategorizationAnalyzerTests.java | 662 +- .../FirstLineWithLettersCharFilterTests.java | 10 +- .../GrokPatternCreatorTests.java | 420 +- .../MlClassicTokenizerTests.java | 1 - .../CategorizationAnalyzerConfigTests.java | 7 +- .../DefaultDetectorDescriptionTests.java | 9 +- .../xpack/ml/job/config/JobBuilderTests.java | 3 +- .../ml/job/config/JobTaskStateTests.java | 2 +- .../xpack/ml/job/messages/MessagesTests.java | 10 +- .../InfluencersQueryBuilderTests.java | 59 +- .../job/persistence/JobDataDeleterTests.java | 35 +- .../JobRenormalizedResultsPersisterTests.java | 4 +- .../persistence/JobResultsPersisterTests.java | 117 +- .../persistence/JobResultsProviderTests.java | 344 +- .../MockBatchedDocumentsIterator.java | 5 +- .../ml/job/persistence/MockClientBuilder.java | 13 +- .../ResultsFilterBuilderTests.java | 93 +- .../job/persistence/StateStreamerTests.java | 36 +- .../persistence/TimingStatsReporterTests.java | 52 +- .../OverallBucketsAggregatorTests.java | 85 +- .../job/process/CountingInputStreamTests.java | 9 +- .../job/process/DataCountsReporterTests.java | 55 +- .../job/process/DummyDataCountsReporter.java | 5 +- .../ProcessWorkerExecutorServiceTests.java | 13 +- .../autodetect/AutodetectBuilderTests.java | 5 +- .../AutodetectCommunicatorTests.java | 63 +- .../AutodetectProcessManagerTests.java | 248 +- .../BlackHoleAutodetectProcessTests.java | 2 +- .../NativeAutodetectProcessTests.java | 78 +- .../process/autodetect/UpdateParamsTests.java | 23 +- .../AutodetectResultProcessorTests.java | 71 +- .../output/FlushAcknowledgementTests.java | 2 +- .../params/AutodetectParamsTests.java | 6 +- .../params/FlushJobParamsTests.java | 91 +- .../params/ForecastParamsTests.java | 14 +- .../autodetect/params/TimeRangeTests.java | 19 +- .../AbstractDataToProcessWriterTests.java | 82 +- .../AutodetectControlMsgWriterTests.java | 65 +- .../DateFormatDateTransformerTests.java | 6 +- .../writer/DoubleDateTransformerTests.java | 6 +- .../writer/JsonDataToProcessWriterTests.java | 163 +- .../writer/XContentRecordReaderTests.java | 57 +- .../BucketInfluencerNormalizableTests.java | 17 +- .../normalizer/BucketNormalizableTests.java | 1 - .../InfluencerNormalizableTests.java | 12 +- .../normalizer/NormalizerBuilderTests.java | 3 +- .../normalizer/NormalizerResultTests.java | 2 +- .../process/normalizer/NormalizerTests.java | 6 +- .../normalizer/ScoresUpdaterTests.java | 18 +- .../ShortCircuitingRenormalizerTests.java | 7 +- .../output/NormalizerResultHandlerTests.java | 17 +- .../ml/job/results/AutodetectResultTests.java | 32 +- .../xpack/ml/job/results/BucketTests.java | 10 +- .../job/results/CategoryDefinitionTests.java | 8 +- .../results/ForecastRequestStatsTests.java | 8 +- .../xpack/ml/job/results/ForecastTests.java | 13 +- .../xpack/ml/job/results/InfluenceTests.java | 5 +- .../xpack/ml/job/results/ModelPlotTests.java | 135 +- .../ml/job/results/OverallBucketTests.java | 12 +- .../AbstractExpiredJobDataRemoverTests.java | 13 +- .../EmptyStateIndexRemoverTests.java | 31 +- .../ExpiredAnnotationsRemoverTests.java | 55 +- .../ExpiredModelSnapshotsRemoverTests.java | 145 +- .../retention/ExpiredResultsRemoverTests.java | 59 +- .../ml/job/retention/MlDataRemoverTests.java | 2 +- .../SnapshotUpgradePredicateTests.java | 40 +- .../OpenJobPersistentTasksExecutorTests.java | 126 +- .../GetModelSnapshotsTests.java | 12 +- .../UpdateModelSnapshotActionTests.java | 7 +- .../process/AbstractNativeProcessTests.java | 35 +- .../ml/process/ControllerResponseTests.java | 2 +- .../process/IndexingStateProcessorTests.java | 26 +- .../ml/process/MlMemoryTrackerTests.java | 90 +- .../ml/process/NativeControllerTests.java | 94 +- .../process/NativeStorageProviderTests.java | 15 +- .../xpack/ml/process/ProcessPipesTests.java | 84 +- .../ml/process/ProcessResultsParserTests.java | 26 +- .../logging/CppLogMessageHandlerTests.java | 346 +- .../process/logging/CppLogMessageTests.java | 6 +- .../writer/LengthEncodedWriterTests.java | 24 +- .../RestStartDatafeedActionTests.java | 30 +- .../xpack/ml/support/BaseMlIntegTestCase.java | 119 +- ...stractJobPersistentTasksExecutorTests.java | 73 +- .../ml/test/MockOriginSettingClient.java | 1 - .../ml/utils/DomainSplitFunctionTests.java | 7 +- .../xpack/ml/utils/MlStringsTests.java | 16 +- .../xpack/ml/utils/NameResolverTests.java | 12 +- .../xpack/ml/utils/NamedPipeHelperTests.java | 69 +- .../ml/utils/NativeMemoryCalculatorTests.java | 93 +- .../ml/utils/VoidChainTaskExecutorTests.java | 28 +- .../BatchedDocumentsIteratorTests.java | 34 +- .../LimitAwareBulkIndexerTests.java | 4 +- .../ResultsPersisterServiceTests.java | 201 +- .../SearchAfterDocumentsIteratorTests.java | 27 +- ...eTimeFormatterTimestampConverterTests.java | 22 +- .../exporter/http/HttpExporterIT.java | 377 +- .../exporter/http/HttpExporterSslIT.java | 17 +- .../monitoring/integration/MonitoringIT.java | 215 +- .../xpack/monitoring/Monitoring.java | 94 +- .../xpack/monitoring/MonitoringService.java | 52 +- .../MonitoringTemplateRegistry.java | 17 +- .../MonitoringUsageTransportAction.java | 29 +- .../action/TransportMonitoringBulkAction.java | 72 +- ...ransportMonitoringMigrateAlertsAction.java | 109 +- .../monitoring/cleaner/CleanerService.java | 16 +- .../xpack/monitoring/collector/Collector.java | 40 +- .../monitoring/collector/TimeoutUtils.java | 12 +- .../ccr/AutoFollowStatsMonitoringDoc.java | 12 +- .../ccr/FollowStatsMonitoringDoc.java | 12 +- .../collector/ccr/StatsCollector.java | 47 +- .../cluster/ClusterStatsCollector.java | 64 +- .../cluster/ClusterStatsMonitoringDoc.java | 48 +- .../enrich/EnrichCoordinatorDoc.java | 12 +- .../enrich/EnrichStatsCollector.java | 21 +- .../collector/enrich/ExecutingPolicyDoc.java | 6 +- .../indices/IndexRecoveryCollector.java | 32 +- .../indices/IndexRecoveryMonitoringDoc.java | 14 +- .../indices/IndexStatsCollector.java | 57 +- .../indices/IndexStatsMonitoringDoc.java | 195 +- .../indices/IndicesStatsMonitoringDoc.java | 67 +- .../collector/ml/JobStatsCollector.java | 36 +- .../collector/ml/JobStatsMonitoringDoc.java | 12 +- .../collector/node/NodeStatsCollector.java | 46 +- .../node/NodeStatsMonitoringDoc.java | 211 +- .../collector/shards/ShardMonitoringDoc.java | 31 +- .../collector/shards/ShardsCollector.java | 8 +- .../exporter/BytesReferenceMonitoringDoc.java | 22 +- .../exporter/ClusterAlertsUtil.java | 27 +- .../xpack/monitoring/exporter/ExportBulk.java | 17 +- .../xpack/monitoring/exporter/Exporter.java | 148 +- .../xpack/monitoring/exporter/Exporters.java | 60 +- .../exporter/FilteredMonitoringDoc.java | 26 +- .../http/ClusterAlertHttpResource.java | 72 +- .../exporter/http/HttpExportBulk.java | 25 +- .../http/HttpExportBulkResponseListener.java | 9 +- .../exporter/http/HttpExporter.java | 576 +- .../exporter/http/HttpHostBuilder.java | 8 +- .../exporter/http/HttpResource.java | 18 +- .../exporter/http/MultiHttpResource.java | 2 +- .../http/PublishableHttpResource.java | 232 +- .../SecurityHttpClientConfigCallback.java | 6 +- .../exporter/http/TemplateHttpResource.java | 26 +- .../http/TimeoutRequestConfigCallback.java | 4 +- .../exporter/http/VersionHttpResource.java | 23 +- .../http/WatcherExistsHttpResource.java | 43 +- .../monitoring/exporter/local/LocalBulk.java | 39 +- .../exporter/local/LocalExporter.java | 222 +- .../rest/action/RestMonitoringBulkAction.java | 23 +- .../RestMonitoringMigrateAlertsAction.java | 14 +- .../monitoring/BaseCollectorTestCase.java | 24 +- .../monitoring/LocalStateMonitoring.java | 33 +- ...onitoringHistoryDurationSettingsTests.java | 30 +- .../MonitoringInfoTransportActionTests.java | 24 +- .../monitoring/MonitoringServiceTests.java | 50 +- .../xpack/monitoring/MonitoringTestUtils.java | 43 +- .../monitoring/MultiNodesStatsTests.java | 21 +- .../action/MonitoringBulkDocTests.java | 91 +- .../action/MonitoringBulkRequestTests.java | 18 +- .../action/MonitoringBulkResponseTests.java | 7 +- .../TransportMonitoringBulkActionTests.java | 209 +- ...ortMonitoringMigrateAlertsActionTests.java | 146 +- .../AbstractIndicesCleanerTestCase.java | 11 +- .../cleaner/CleanerServiceTests.java | 26 +- .../local/LocalIndicesCleanerTests.java | 25 +- .../cluster/ClusterStatsCollectorTests.java | 158 +- .../ClusterStatsMonitoringDocTests.java | 322 +- .../indices/IndexRecoveryCollectorTests.java | 53 +- .../IndexRecoveryMonitoringDocTests.java | 69 +- .../indices/IndexStatsCollectorTests.java | 38 +- .../indices/IndexStatsMonitoringDocTests.java | 354 +- .../IndicesStatsMonitoringDocTests.java | 43 +- .../collector/ml/JobStatsCollectorTests.java | 25 +- .../ml/JobStatsMonitoringDocTests.java | 115 +- .../node/NodeStatsCollectorTests.java | 29 +- .../node/NodeStatsMonitoringDocTests.java | 134 +- .../shards/ShardsCollectorTests.java | 10 +- .../shards/ShardsMonitoringDocTests.java | 33 +- .../BaseFilteredMonitoringDocTestCase.java | 47 +- .../exporter/BaseMonitoringDocTestCase.java | 59 +- .../BytesReferenceMonitoringDocTests.java | 145 +- .../exporter/ClusterAlertsUtilTests.java | 54 +- .../monitoring/exporter/ExportersTests.java | 236 +- .../MonitoringTemplateUtilsTests.java | 48 +- ...stractPublishableHttpResourceTestCase.java | 167 +- .../http/AsyncHttpResourceHelper.java | 39 +- .../HttpExportBulkResponseListenerTests.java | 64 +- .../http/HttpExporterResourceTests.java | 136 +- .../exporter/http/HttpExporterTests.java | 388 +- .../exporter/http/HttpResourceTests.java | 28 +- .../exporter/http/MockHttpResource.java | 20 +- .../exporter/http/MultiHttpResourceTests.java | 3 +- .../http/PublishableHttpResourceTests.java | 184 +- ...SecurityHttpClientConfigCallbackTests.java | 3 +- .../http/TemplateHttpResourceTests.java | 19 +- .../TimeoutRequestConfigCallbackTests.java | 5 +- .../http/WatcherExistsHttpResourceTests.java | 14 +- .../local/LocalExporterIntegTestCase.java | 34 +- .../local/LocalExporterIntegTests.java | 126 +- .../LocalExporterResourceIntegTests.java | 91 +- .../action/RestMonitoringBulkActionTests.java | 52 +- ...estMonitoringMigrateAlertsActionTests.java | 24 +- .../test/MockClusterAlertScriptEngine.java | 5 +- .../monitoring/test/MockIngestPlugin.java | 25 +- .../test/MonitoringIntegTestCase.java | 52 +- .../xpack/ql/analyzer/AnalyzerRules.java | 6 +- .../ql/async/AsyncTaskManagementService.java | 164 +- .../xpack/ql/async/QlStatusResponse.java | 23 +- .../xpack/ql/capabilities/Unresolvable.java | 1 - .../xpack/ql/common/Failure.java | 11 +- .../ql/execution/search/QlSourceBuilder.java | 3 +- .../extractor/AbstractFieldHitExtractor.java | 8 +- .../search/extractor/ComputingExtractor.java | 3 +- .../search/extractor/HitExtractor.java | 2 +- .../xpack/ql/expression/Alias.java | 6 +- .../xpack/ql/expression/Attribute.java | 24 +- .../xpack/ql/expression/AttributeMap.java | 1 - .../xpack/ql/expression/AttributeSet.java | 4 +- .../xpack/ql/expression/EmptyAttribute.java | 11 +- .../xpack/ql/expression/Expressions.java | 1 - .../xpack/ql/expression/FieldAttribute.java | 36 +- .../xpack/ql/expression/NamedExpression.java | 17 +- .../xpack/ql/expression/Order.java | 10 +- .../ql/expression/ReferenceAttribute.java | 22 +- .../xpack/ql/expression/TypeResolutions.java | 59 +- .../xpack/ql/expression/TypedAttribute.java | 11 +- .../xpack/ql/expression/UnaryExpression.java | 1 + .../ql/expression/UnresolvedAttribute.java | 29 +- .../function/DefaultFunctionTypeRegistry.java | 1 - .../expression/function/FunctionRegistry.java | 120 +- .../function/FunctionResolutionStrategy.java | 1 - .../function/UnresolvedFunction.java | 13 +- .../function/aggregate/AggregateFunction.java | 3 +- .../function/aggregate/InnerAggregate.java | 4 +- .../function/grouping/GroupingFunction.java | 3 +- .../function/scalar/ScalarFunction.java | 40 +- ...naryComparisonCaseInsensitiveFunction.java | 25 +- .../string/CaseInsensitiveScalarFunction.java | 6 +- .../function/scalar/string/StartsWith.java | 12 +- .../scalar/string/StartsWithFunctionPipe.java | 4 +- .../string/StartsWithFunctionProcessor.java | 1 - .../whitelist/InternalQlScriptUtils.java | 1 - .../expression/gen/pipeline/AggPathInput.java | 3 +- .../expression/gen/pipeline/BinaryPipe.java | 3 +- .../ql/expression/gen/pipeline/LeafInput.java | 3 +- .../ql/expression/gen/pipeline/UnaryPipe.java | 4 +- .../gen/processor/BinaryProcessor.java | 2 +- .../processor/FunctionalBinaryProcessor.java | 4 +- .../FunctionalEnumBinaryProcessor.java | 7 +- .../xpack/ql/expression/gen/script/Param.java | 7 +- .../ql/expression/gen/script/Params.java | 16 +- .../expression/gen/script/ScriptTemplate.java | 4 +- .../ql/expression/gen/script/Scripts.java | 80 +- .../expression/predicate/BinaryPredicate.java | 4 +- .../xpack/ql/expression/predicate/Range.java | 62 +- .../predicate/fulltext/FullTextPredicate.java | 3 +- .../predicate/fulltext/FullTextUtils.java | 6 +- .../arithmetic/UnaryArithmeticProcessor.java | 1 - .../predicate/operator/comparison/In.java | 44 +- .../predicate/regex/LikePattern.java | 3 +- .../ql/expression/processor/Processors.java | 3 +- .../xpack/ql/index/IndexResolution.java | 5 +- .../xpack/ql/index/IndexResolver.java | 386 +- .../xpack/ql/optimizer/OptimizerRules.java | 366 +- .../xpack/ql/plan/logical/Aggregate.java | 4 +- .../xpack/ql/plan/logical/BinaryPlan.java | 3 +- .../xpack/ql/plan/logical/EsRelation.java | 3 +- .../xpack/ql/plan/logical/Filter.java | 3 +- .../xpack/ql/plan/logical/Limit.java | 3 +- .../xpack/ql/plan/logical/OrderBy.java | 3 +- .../xpack/ql/plan/logical/Project.java | 3 +- .../ql/planner/ExpressionTranslators.java | 35 +- ...tractTransportQlAsyncGetResultsAction.java | 82 +- ...stractTransportQlAsyncGetStatusAction.java | 44 +- .../xpack/ql/plugin/TransportActionUtils.java | 20 +- .../ql/querydsl/container/AttributeSort.java | 4 +- .../ql/querydsl/container/ScriptSort.java | 4 +- .../xpack/ql/querydsl/container/Sort.java | 4 +- .../xpack/ql/querydsl/query/BoolQuery.java | 4 +- .../ql/querydsl/query/GeoDistanceQuery.java | 10 +- .../xpack/ql/querydsl/query/MatchQuery.java | 28 +- .../ql/querydsl/query/MultiMatchQuery.java | 37 +- .../xpack/ql/querydsl/query/NestedQuery.java | 9 +- .../xpack/ql/querydsl/query/PrefixQuery.java | 4 +- .../ql/querydsl/query/QueryStringQuery.java | 53 +- .../xpack/ql/querydsl/query/RangeQuery.java | 30 +- .../xpack/ql/querydsl/query/TermQuery.java | 4 +- .../xpack/ql/querydsl/query/TermsQuery.java | 3 +- .../ql/querydsl/query/WildcardQuery.java | 4 +- .../xpack/ql/rule/RuleExecutor.java | 14 +- .../elasticsearch/xpack/ql/tree/Location.java | 3 +- .../org/elasticsearch/xpack/ql/tree/Node.java | 10 +- .../elasticsearch/xpack/ql/tree/NodeInfo.java | 113 +- .../xpack/ql/tree/NodeUtils.java | 9 +- .../elasticsearch/xpack/ql/type/DataType.java | 10 +- .../xpack/ql/type/DataTypeConverter.java | 10 +- .../xpack/ql/type/DataTypes.java | 54 +- .../xpack/ql/type/DateUtils.java | 16 +- .../elasticsearch/xpack/ql/type/EsField.java | 8 +- .../xpack/ql/type/KeywordEsField.java | 24 +- .../elasticsearch/xpack/ql/type/Schema.java | 1 + .../xpack/ql/type/StringUtils.java | 44 +- .../xpack/ql/type/TextEsField.java | 12 +- .../elasticsearch/xpack/ql/type/Types.java | 2 +- .../xpack/ql/type/UnsupportedEsField.java | 3 +- .../xpack/ql/util/CollectionUtils.java | 3 +- .../xpack/ql/util/DateUtils.java | 45 +- .../elasticsearch/xpack/ql/util/Graphviz.java | 77 +- .../elasticsearch/xpack/ql/util/Holder.java | 3 +- .../xpack/ql/util/ReflectionUtils.java | 10 +- .../xpack/ql/util/RemoteClusterUtils.java | 7 +- .../xpack/ql/util/StringUtils.java | 41 +- .../ql/action/QlStatusResponseTests.java | 36 +- .../AsyncTaskManagementServiceTests.java | 128 +- .../ql/async/StoredAsyncResponseTests.java | 2 +- .../extractor/ConstantExtractorTests.java | 5 +- .../ql/expression/AttributeMapTests.java | 9 +- .../xpack/ql/expression/LiteralTests.java | 33 +- .../expression/UnresolvedAttributeTests.java | 71 +- .../function/FunctionRegistryTests.java | 64 +- .../function/UnresolvedFunctionTests.java | 127 +- .../string/StartsWithFunctionPipeTests.java | 46 +- .../string/StartsWithProcessorTests.java | 22 +- .../gen/pipeline/BinaryPipesTests.java | 3 +- .../gen/processor/ChainingProcessorTests.java | 7 +- .../gen/processor/ConstantProcessorTests.java | 2 +- .../expression/gen/script/ScriptsTests.java | 32 +- .../fulltext/FullTextUtilsTests.java | 6 +- .../logical/BinaryLogicProcessorTests.java | 7 +- .../nulls/CheckNullProcessorTests.java | 1 - .../BinaryArithmeticProcessorTests.java | 7 +- .../BinaryComparisonProcessorTests.java | 7 +- .../operator/comparison/InProcessorTests.java | 2 - .../operator/comparison/InTests.java | 1 - .../ql/optimizer/OptimizerRulesTests.java | 57 +- .../xpack/ql/plan/QueryPlanTests.java | 6 +- .../ql/querydsl/query/BoolQueryTests.java | 46 +- .../ql/querydsl/query/LeafQueryTests.java | 2 - .../ql/querydsl/query/MatchQueryTests.java | 13 +- .../querydsl/query/MultiMatchQueryTests.java | 1 - .../ql/querydsl/query/NestedQueryTests.java | 15 +- .../querydsl/query/QueryStringQueryTests.java | 2 - .../xpack/ql/tree/AbstractNodeTestCase.java | 3 + .../xpack/ql/tree/NodeSubclassTests.java | 58 +- .../xpack/ql/tree/NodeTests.java | 25 +- .../xpack/ql/tree/SourceTests.java | 15 +- .../ql/type/DataTypeConversionTests.java | 12 +- .../xpack/ql/type/TypesTests.java | 4 +- .../ql/util/RemoteClusterUtilsTests.java | 4 +- .../xpack/rollup/v2/RollupRestIT.java | 1 + .../PinnedQueryBuilderIT.java | 188 +- .../searchbusinessrules/CappedScoreQuery.java | 11 +- .../CappedScoreWeight.java | 11 +- .../PinnedQueryBuilder.java | 35 +- .../PinnedQueryBuilderTests.java | 163 +- .../rest/FsSearchableSnapshotsIT.java | 4 +- ...rchableSnapshotsClientYamlTestSuiteIT.java | 4 +- .../xpack/security/cli/AutoConfigureNode.java | 11 +- .../xpack/security/cli/CertGenUtils.java | 104 +- .../security/cli/CertificateGenerateTool.java | 152 +- .../xpack/security/cli/CertificateTool.java | 278 +- .../security/cli/HttpCertificateCommand.java | 154 +- .../security/cli/AutoConfigureNodeTests.java | 6 +- .../xpack/security/cli/CertGenUtilsTests.java | 66 +- .../cli/CertificateGenerateToolTests.java | 94 +- .../security/cli/CertificateToolTests.java | 322 +- .../cli/HttpCertificateCommandTests.java | 79 +- .../EnableSecurityOnBasicLicenseIT.java | 13 +- .../xpack/security/QueryApiKeyIT.java | 327 +- .../security/SecurityInBasicRestTestCase.java | 14 +- .../security/SecurityWithBasicLicenseIT.java | 156 +- ...tyUserProcessorWithSecurityDisabledIT.java | 22 +- .../SecurityOnTrialLicenseRestTestCase.java | 23 +- .../xpack/security/apikey/ApiKeyRestIT.java | 25 +- .../security/dlsfls/DlsRequestCacheIT.java | 22 +- .../test/rest/CatIndicesWithSecurityIT.java | 22 +- .../authc/service/ServiceAccountIT.java | 119 +- .../xpack/security/authc/FileRealmAuthIT.java | 3 +- .../security/authc/NativeRealmAuthIT.java | 3 +- .../xpack/security/authc/RealmInfoIT.java | 3 +- .../authc/SecurityRealmSmokeTestCase.java | 33 +- .../xpack/security/TlsWithBasicLicenseIT.java | 4 +- .../AbstractPrivilegeTestCase.java | 32 +- .../integration/BulkUpdateTests.java | 40 +- .../integration/ClearRealmsCacheTests.java | 27 +- .../integration/ClearRolesCacheTests.java | 49 +- .../ClusterPrivilegeIntegrationTests.java | 88 +- .../CreateDocsIndexPrivilegeTests.java | 83 +- .../DateMathExpressionIntegTests.java | 43 +- .../integration/DlsFlsRequestCacheTests.java | 262 +- .../DocumentAndFieldLevelSecurityTests.java | 369 +- .../DocumentLevelSecurityRandomTests.java | 21 +- .../DocumentLevelSecurityTests.java | 1422 +- .../FieldLevelSecurityRandomTests.java | 222 +- .../integration/FieldLevelSecurityTests.java | 1899 +- .../integration/IndexPrivilegeIntegTests.java | 529 +- ...onsWithAliasesWildcardsAndRegexsTests.java | 155 +- .../KibanaSystemRoleIntegTests.java | 34 +- .../integration/KibanaUserRoleIntegTests.java | 88 +- .../MultipleIndicesPermissionsTests.java | 230 +- .../PermissionPrecedenceTests.java | 80 +- .../SecurityCachePermissionTests.java | 37 +- .../integration/SecurityClearScrollTests.java | 61 +- .../SecurityFeatureStateIntegTests.java | 63 +- .../ShrinkIndexWithSecurityTests.java | 35 +- .../elasticsearch/license/LicensingTests.java | 114 +- .../test/NativeRealmIntegTestCase.java | 10 +- .../test/SecuritySingleNodeTestCase.java | 48 +- .../SecurityServerTransportServiceTests.java | 17 +- .../security/ScrollHelperIntegTests.java | 46 +- .../xpack/security/SecurityPluginTests.java | 13 +- .../xpack/security/TemplateUpgraderTests.java | 26 +- .../filter/DestructiveOperationsTests.java | 58 +- .../AuditTrailSettingsUpdateTests.java | 82 +- .../security/authc/ApiKeyIntegTests.java | 860 +- .../xpack/security/authc/RunAsIntegTests.java | 30 +- .../authc/SecurityRealmSettingsTests.java | 10 +- .../security/authc/TokenAuthIntegTests.java | 470 +- .../authc/apikey/ApiKeySingleNodeTests.java | 99 +- .../authc/esnative/NativeRealmIntegTests.java | 495 +- ...ervedRealmElasticAutoconfigIntegTests.java | 107 +- .../esnative/ReservedRealmIntegTests.java | 126 +- .../pki/PkiAuthDelegationIntegTests.java | 271 +- .../authc/pki/PkiAuthenticationTests.java | 50 +- .../authc/pki/PkiOptionalClientAuthTests.java | 44 +- .../ServiceAccountSingleNodeTests.java | 84 +- .../xpack/security/authz/AnalyzeTests.java | 95 +- .../security/authz/IndexAliasesTests.java | 850 +- .../security/authz/ReadActionsTests.java | 148 +- .../security/authz/SecurityScrollTests.java | 37 +- .../SecuritySearchOperationListenerTests.java | 277 +- .../authz/SnapshotUserRoleIntegTests.java | 130 +- .../security/authz/WriteActionsTests.java | 128 +- .../store/NativePrivilegeStoreCacheTests.java | 202 +- ...ivilegesPartiallyEnabledIntegTestCase.java | 10 +- .../OperatorPrivilegesSingleNodeTests.java | 30 +- .../action/RestAuthenticateActionTests.java | 15 +- .../SecurityIndexManagerIntegTests.java | 9 +- .../filter/IpFilteringIntegrationTests.java | 33 +- .../filter/IpFilteringUpdateTests.java | 76 +- .../transport/ssl/EllipticCurveSSLTests.java | 33 +- .../transport/ssl/SslIntegrationTests.java | 49 +- .../user/AnonymousUserIntegTests.java | 16 +- .../xpack/ssl/SSLReloadIntegTests.java | 25 +- .../xpack/ssl/SSLTrustRestrictionsTests.java | 75 +- .../ssl/SslClientAuthenticationTests.java | 41 +- .../transport/ProfileConfigurations.java | 6 +- .../transport/SSLExceptionHelper.java | 23 +- .../SecurityTransportExceptionHandler.java | 1 - .../netty4/SecurityNetty4Transport.java | 62 +- .../InitialNodeSecurityAutoConfiguration.java | 51 +- .../security/PkiRealmBootstrapCheck.java | 12 +- .../xpack/security/Security.java | 992 +- .../SecurityUsageTransportAction.java | 91 +- .../security/TokenSSLBootstrapCheck.java | 11 +- .../security/action/SecurityActionMapper.java | 2 +- .../TransportClearSecurityCacheAction.java | 18 +- .../action/TransportCreateApiKeyAction.java | 19 +- ...nsportDelegatePkiAuthenticationAction.java | 74 +- .../action/TransportGetApiKeyAction.java | 13 +- .../action/TransportGrantApiKeyAction.java | 54 +- .../TransportInvalidateApiKeyAction.java | 16 +- .../apikey/TransportQueryApiKeyAction.java | 26 +- .../TransportKibanaEnrollmentAction.java | 57 +- .../TransportNodeEnrollmentAction.java | 118 +- .../action/filter/SecurityActionFilter.java | 94 +- ...nsportOpenIdConnectAuthenticateAction.java | 93 +- .../TransportOpenIdConnectLogoutAction.java | 61 +- ...nIdConnectPrepareAuthenticationAction.java | 59 +- .../TransportClearPrivilegesCacheAction.java | 18 +- .../TransportDeletePrivilegesAction.java | 21 +- .../TransportGetPrivilegesAction.java | 16 +- .../TransportPutPrivilegesAction.java | 16 +- .../realm/TransportClearRealmCacheAction.java | 40 +- .../role/TransportClearRolesCacheAction.java | 36 +- .../action/role/TransportGetRolesAction.java | 8 +- .../action/role/TransportPutRoleAction.java | 10 +- .../TransportDeleteRoleMappingAction.java | 16 +- .../TransportGetRoleMappingsAction.java | 23 +- .../TransportPutRoleMappingAction.java | 18 +- .../saml/TransportSamlAuthenticateAction.java | 37 +- .../TransportSamlCompleteLogoutAction.java | 7 +- .../TransportSamlInvalidateSessionAction.java | 69 +- .../saml/TransportSamlLogoutAction.java | 57 +- ...nsportSamlPrepareAuthenticationAction.java | 21 +- .../saml/TransportSamlSpMetadataAction.java | 19 +- ...nsportCreateServiceAccountTokenAction.java | 21 +- ...nsportDeleteServiceAccountTokenAction.java | 25 +- ...ortGetServiceAccountCredentialsAction.java | 19 +- ...tServiceAccountNodesCredentialsAction.java | 42 +- .../token/TransportCreateTokenAction.java | 95 +- .../token/TransportInvalidateTokenAction.java | 7 +- .../token/TransportRefreshTokenAction.java | 11 +- .../user/TransportAuthenticateAction.java | 45 +- .../user/TransportChangePasswordAction.java | 20 +- .../user/TransportDeleteUserAction.java | 8 +- .../TransportGetUserPrivilegesAction.java | 10 +- .../action/user/TransportGetUsersAction.java | 18 +- .../user/TransportHasPrivilegesAction.java | 39 +- .../action/user/TransportPutUserAction.java | 20 +- .../user/TransportSetEnabledAction.java | 17 +- .../xpack/security/audit/AuditTrail.java | 62 +- .../security/audit/AuditTrailService.java | 198 +- .../xpack/security/audit/AuditUtil.java | 5 +- .../audit/index/IndexNameResolver.java | 8 +- .../audit/logfile/LoggingAuditTrail.java | 1281 +- .../security/authc/ApiKeyAuthenticator.java | 22 +- .../xpack/security/authc/ApiKeyService.java | 744 +- .../security/authc/AuthenticationService.java | 95 +- .../security/authc/AuthenticatorChain.java | 89 +- .../security/authc/ExpiredApiKeysRemover.java | 42 +- .../security/authc/ExpiredTokenRemover.java | 72 +- .../xpack/security/authc/InternalRealms.java | 75 +- .../xpack/security/authc/Realms.java | 140 +- .../security/authc/RealmsAuthenticator.java | 144 +- .../xpack/security/authc/TokenService.java | 1111 +- .../security/authc/esnative/NativeRealm.java | 10 +- .../authc/esnative/NativeUsersStore.java | 599 +- .../authc/esnative/ReservedRealm.java | 104 +- .../esnative/tool/ResetPasswordTool.java | 9 +- .../esnative/tool/SetupPasswordTool.java | 158 +- .../authc/file/FileUserPasswdStore.java | 23 +- .../authc/file/FileUserRolesStore.java | 44 +- .../security/authc/file/tool/UsersTool.java | 78 +- .../kerberos/KerberosAuthenticationToken.java | 18 +- .../authc/kerberos/KerberosRealm.java | 93 +- .../kerberos/KerberosTicketValidator.java | 75 +- .../ldap/ActiveDirectoryGroupsResolver.java | 100 +- .../authc/ldap/ActiveDirectorySIDUtil.java | 44 +- .../ldap/ActiveDirectorySessionFactory.java | 429 +- .../xpack/security/authc/ldap/LdapRealm.java | 147 +- .../authc/ldap/LdapSessionFactory.java | 22 +- .../ldap/LdapUserSearchSessionFactory.java | 123 +- .../authc/ldap/PoolingSessionFactory.java | 69 +- .../authc/ldap/SearchGroupsResolver.java | 90 +- .../ldap/UserAttributeGroupsResolver.java | 42 +- .../authc/ldap/support/LdapLoadBalancing.java | 93 +- .../ldap/support/LdapMetadataResolver.java | 71 +- .../authc/ldap/support/LdapSession.java | 41 +- .../authc/ldap/support/LdapUtils.java | 82 +- .../authc/ldap/support/SessionFactory.java | 95 +- .../oidc/OpenIdConnectAuthenticator.java | 340 +- .../OpenIdConnectProviderConfiguration.java | 12 +- .../authc/oidc/OpenIdConnectRealm.java | 221 +- .../authc/oidc/OpenIdConnectToken.java | 18 +- .../authc/oidc/RelyingPartyConfiguration.java | 26 +- .../xpack/security/authc/pki/PkiRealm.java | 83 +- .../security/authc/saml/IdpConfiguration.java | 4 +- .../security/authc/saml/SamlAttributes.java | 30 +- .../authc/saml/SamlAuthenticator.java | 155 +- .../authc/saml/SamlAuthnRequestBuilder.java | 20 +- .../authc/saml/SamlLogoutRequestHandler.java | 64 +- .../saml/SamlLogoutRequestMessageBuilder.java | 13 +- .../authc/saml/SamlLogoutResponseBuilder.java | 21 +- .../authc/saml/SamlLogoutResponseHandler.java | 13 +- .../authc/saml/SamlMessageBuilder.java | 27 +- .../authc/saml/SamlMetadataCommand.java | 180 +- .../xpack/security/authc/saml/SamlNameId.java | 9 +- .../authc/saml/SamlObjectHandler.java | 151 +- .../xpack/security/authc/saml/SamlRealm.java | 333 +- .../security/authc/saml/SamlRedirect.java | 9 +- .../authc/saml/SamlResponseHandler.java | 24 +- .../authc/saml/SamlSpMetadataBuilder.java | 40 +- .../xpack/security/authc/saml/SamlToken.java | 7 +- .../xpack/security/authc/saml/SamlUtils.java | 97 +- .../authc/saml/SigningConfiguration.java | 6 +- .../security/authc/saml/SpConfiguration.java | 11 +- .../CachingServiceAccountTokenStore.java | 23 +- .../CompositeServiceAccountTokenStore.java | 3 +- .../authc/service/ElasticServiceAccounts.java | 54 +- .../service/FileServiceAccountTokenStore.java | 36 +- .../authc/service/FileTokensTool.java | 29 +- .../IndexServiceAccountTokenStore.java | 140 +- .../authc/service/ServiceAccount.java | 3 +- .../authc/service/ServiceAccountService.java | 84 +- .../authc/service/ServiceAccountToken.java | 37 +- .../authc/support/ApiKeyGenerator.java | 10 +- .../support/CachingUsernamePasswordRealm.java | 47 +- .../DelegatedAuthorizationSupport.java | 53 +- .../security/authc/support/DnRoleMapper.java | 81 +- .../authc/support/RealmUserLookup.java | 26 +- .../RoleMappingFileBootstrapCheck.java | 4 +- .../authc/support/SecondaryAuthenticator.java | 64 +- .../support/mapper/CompositeRoleMapper.java | 32 +- .../mapper/NativeRoleMappingStore.java | 212 +- .../security/authz/AuthorizationService.java | 474 +- .../security/authz/AuthorizationUtils.java | 7 +- .../DlsFlsRequestCacheDifferentiator.java | 26 +- .../authz/IndicesAndAliasesResolver.java | 86 +- .../xpack/security/authz/RBACEngine.java | 316 +- .../SecuritySearchOperationListener.java | 54 +- .../authz/accesscontrol/FieldExtractor.java | 12 +- .../authz/accesscontrol/OptOutQueryCache.java | 9 +- .../BulkShardRequestInterceptor.java | 25 +- .../DlsFlsLicenseRequestInterceptor.java | 13 +- ...cumentLevelSecurityRequestInterceptor.java | 31 +- .../IndicesAliasesRequestInterceptor.java | 93 +- .../authz/interceptor/RequestInterceptor.java | 8 +- .../interceptor/ResizeRequestInterceptor.java | 46 +- .../interceptor/SearchRequestInterceptor.java | 24 +- .../ShardSearchRequestInterceptor.java | 14 +- .../interceptor/UpdateRequestInterceptor.java | 18 +- .../authz/store/CompositeRolesStore.java | 298 +- .../DeprecationRoleDescriptorConsumer.java | 27 +- .../security/authz/store/FileRolesStore.java | 177 +- .../authz/store/NativePrivilegeStore.java | 250 +- .../authz/store/NativeRolesStore.java | 289 +- .../security/authz/store/RoleProviders.java | 1 + .../security/crypto/tool/SystemKeyTool.java | 23 +- .../BaseEnrollmentTokenGenerator.java | 31 +- .../ExternalEnrollmentTokenGenerator.java | 37 +- .../InternalEnrollmentTokenGenerator.java | 47 +- .../tool/CreateEnrollmentTokenTool.java | 2 +- .../ingest/SetSecurityUserProcessor.java | 58 +- .../operator/FileOperatorUsersStore.java | 78 +- .../operator/OperatorOnlyRegistry.java | 12 +- .../security/operator/OperatorPrivileges.java | 32 +- .../security/rest/SecurityRestFilter.java | 73 +- .../rest/action/RestAuthenticateAction.java | 26 +- .../RestDelegatePkiAuthenticationAction.java | 25 +- .../rest/action/SecurityBaseRestHandler.java | 1 - .../action/apikey/RestCreateApiKeyAction.java | 15 +- .../action/apikey/RestGetApiKeyAction.java | 25 +- .../action/apikey/RestGrantApiKeyAction.java | 46 +- .../apikey/RestInvalidateApiKeyAction.java | 77 +- .../action/apikey/RestQueryApiKeyAction.java | 37 +- .../enrollment/EnrollmentBaseRestHandler.java | 9 +- .../enrollment/RestKibanaEnrollAction.java | 22 +- .../enrollment/RestNodeEnrollmentAction.java | 25 +- .../action/oauth2/RestGetTokenAction.java | 100 +- .../oauth2/RestInvalidateTokenAction.java | 62 +- .../RestOpenIdConnectAuthenticateAction.java | 25 +- .../oidc/RestOpenIdConnectLogoutAction.java | 18 +- ...nIdConnectPrepareAuthenticationAction.java | 21 +- .../RestClearPrivilegesCacheAction.java | 3 +- .../privilege/RestDeletePrivilegesAction.java | 10 +- .../RestGetBuiltinPrivilegesAction.java | 9 +- .../privilege/RestGetPrivilegesAction.java | 20 +- .../privilege/RestPutPrivilegesAction.java | 17 +- .../realm/RestClearRealmCacheAction.java | 5 +- .../role/RestClearRolesCacheAction.java | 5 +- .../action/role/RestDeleteRoleAction.java | 13 +- .../rest/action/role/RestGetRolesAction.java | 14 +- .../rest/action/role/RestPutRoleAction.java | 18 +- .../RestDeleteRoleMappingAction.java | 16 +- .../RestGetRoleMappingsAction.java | 42 +- .../rolemapping/RestPutRoleMappingAction.java | 31 +- .../saml/RestSamlAuthenticateAction.java | 24 +- .../saml/RestSamlCompleteLogoutAction.java | 29 +- .../saml/RestSamlInvalidateSessionAction.java | 26 +- .../action/saml/RestSamlLogoutAction.java | 20 +- .../RestSamlPrepareAuthenticationAction.java | 48 +- .../action/saml/RestSamlSpMetadataAction.java | 9 +- ...arServiceAccountTokenStoreCacheAction.java | 2 +- .../RestCreateServiceAccountTokenAction.java | 14 +- .../RestDeleteServiceAccountTokenAction.java | 15 +- .../service/RestGetServiceAccountAction.java | 3 +- ...estGetServiceAccountCredentialsAction.java | 16 +- .../action/user/RestChangePasswordAction.java | 19 +- .../action/user/RestDeleteUserAction.java | 18 +- .../user/RestGetUserPrivilegesAction.java | 9 +- .../rest/action/user/RestGetUsersAction.java | 10 +- .../action/user/RestHasPrivilegesAction.java | 26 +- .../rest/action/user/RestPutUserAction.java | 19 +- .../action/user/RestSetEnabledAction.java | 19 +- .../support/ApiKeyBoolQueryBuilder.java | 19 +- .../support/ApiKeyFieldNameTranslators.java | 3 +- .../support/CacheInvalidatorRegistry.java | 16 +- .../security/support/ExtensionComponents.java | 9 +- .../support/FileAttributesChecker.java | 55 +- .../xpack/security/support/SecurityFiles.java | 12 +- .../support/SecurityIndexManager.java | 141 +- .../tool/BaseRunAsSuperuserCommand.java | 55 +- .../xpack/security/tool/CommandUtils.java | 1 + .../security/transport/SSLEngineUtils.java | 11 +- .../SecurityServerTransportInterceptor.java | 157 +- .../transport/ServerTransportFilter.java | 18 +- .../security/transport/filter/IPFilter.java | 180 +- .../transport/filter/PatternRule.java | 4 +- .../filter/SecurityIpFilterRule.java | 6 +- .../netty4/IpFilterRemoteAddressFilter.java | 1 + .../SecurityNetty4HttpServerTransport.java | 25 +- .../netty4/SecurityNetty4ServerTransport.java | 40 +- .../transport/nio/SSLChannelContext.java | 50 +- .../security/transport/nio/SSLDriver.java | 9 +- .../nio/SecurityNioHttpServerTransport.java | 92 +- .../transport/nio/SecurityNioTransport.java | 63 +- .../test/SecurityIntegTestCase.java | 86 +- .../test/SecuritySettingsSource.java | 217 +- .../test/SecurityTestsUtils.java | 37 +- .../test/SettingsFilterTests.java | 72 +- .../PutPrivilegesRequestBuilderTests.java | 79 +- .../transport/ProfileConfigurationsTests.java | 6 +- .../xpack/security/LocalStateSecurity.java | 28 +- .../security/PkiRealmBootstrapCheckTests.java | 98 +- .../xpack/security/SecurityContextTests.java | 55 +- .../SecurityInfoTransportActionTests.java | 40 +- .../xpack/security/SecurityTests.java | 329 +- .../security/TokenSSLBootsrapCheckTests.java | 5 +- .../action/SecurityActionMapperTests.java | 23 +- .../TransportGrantApiKeyActionTests.java | 21 +- .../TransportQueryApiKeyActionTests.java | 11 +- .../TransportKibanaEnrollmentActionTests.java | 33 +- .../TransportNodeEnrollmentActionTests.java | 62 +- .../filter/SecurityActionFilterTests.java | 51 +- ...OpenIdConnectAuthenticateRequestTests.java | 1 - ...nectPrepareAuthenticationRequestTests.java | 21 +- ...ansportOpenIdConnectLogoutActionTests.java | 60 +- .../action/role/PutRoleBuilderTests.java | 17 +- .../role/TransportDeleteRoleActionTests.java | 39 +- .../role/TransportGetRolesActionTests.java | 105 +- .../role/TransportPutRoleActionTests.java | 163 +- .../PutRoleMappingRequestTests.java | 26 +- .../TransportGetRoleMappingsActionTests.java | 13 +- .../TransportPutRoleMappingActionTests.java | 26 +- ...sportSamlInvalidateSessionActionTests.java | 131 +- .../saml/TransportSamlLogoutActionTests.java | 60 +- ...tCreateServiceAccountTokenActionTests.java | 7 +- ...tDeleteServiceAccountTokenActionTests.java | 15 +- ...TransportGetServiceAccountActionTests.java | 9 +- ...tServiceAccountCredentialsActionTests.java | 18 +- .../TransportCreateTokenActionTests.java | 163 +- .../TransportInvalidateTokenActionTests.java | 65 +- .../ChangePasswordRequestBuilderTests.java | 54 +- .../HasPrivilegesRequestBuilderTests.java | 60 +- .../user/PutUserRequestBuilderTests.java | 168 +- .../TransportAuthenticateActionTests.java | 77 +- .../TransportChangePasswordActionTests.java | 123 +- .../user/TransportDeleteUserActionTests.java | 97 +- .../user/TransportGetUsersActionTests.java | 177 +- .../TransportHasPrivilegesActionTests.java | 19 +- .../user/TransportPutUserActionTests.java | 97 +- .../user/TransportSetEnabledActionTests.java | 132 +- .../xpack/security/audit/AuditLevelTests.java | 1 - .../audit/AuditTrailServiceTests.java | 68 +- .../xpack/security/audit/AuditUtilTests.java | 7 +- .../logfile/LoggingAuditTrailFilterTests.java | 1425 +- .../audit/logfile/LoggingAuditTrailTests.java | 1403 +- .../security/authc/ApiKeyServiceTests.java | 721 +- .../authc/AuthenticationServiceTests.java | 656 +- .../authc/AuthenticatorChainTests.java | 57 +- .../security/authc/InternalRealmsTests.java | 2 +- .../security/authc/RealmSettingsTests.java | 111 +- .../authc/RealmsAuthenticatorTests.java | 66 +- .../xpack/security/authc/RealmsTests.java | 16 +- .../security/authc/TokenServiceMock.java | 10 +- .../security/authc/TokenServiceTests.java | 277 +- .../authc/esnative/NativeRealmTests.java | 27 +- .../authc/esnative/NativeUsersStoreTests.java | 100 +- .../authc/esnative/ReservedRealmTests.java | 459 +- .../tool/CommandLineHttpClientTests.java | 40 +- .../esnative/tool/SetupPasswordToolTests.java | 309 +- .../security/authc/file/FileRealmTests.java | 33 +- .../authc/file/FileUserPasswdStoreTests.java | 40 +- .../authc/file/FileUserRolesStoreTests.java | 34 +- .../KerberosAuthenticationTokenTests.java | 51 +- .../KerberosRealmAuthenticateFailedTests.java | 54 +- .../kerberos/KerberosRealmCacheTests.java | 47 +- .../kerberos/KerberosRealmSettingsTests.java | 26 +- .../authc/kerberos/KerberosRealmTestCase.java | 83 +- .../authc/kerberos/KerberosRealmTests.java | 102 +- .../ldap/ActiveDirectorySIDUtilTests.java | 2 +- .../ldap/CancellableLdapRunnableTests.java | 68 +- .../authc/ldap/GroupsResolverTestCase.java | 19 +- .../security/authc/ldap/LdapRealmTests.java | 327 +- .../authc/ldap/LdapSessionFactoryTests.java | 117 +- .../security/authc/ldap/LdapTestUtils.java | 12 +- .../LdapUserSearchSessionFactoryTests.java | 238 +- .../SearchGroupsResolverInMemoryTests.java | 59 +- .../ldap/support/LdapLoadBalancingTests.java | 49 +- .../support/LdapMetadataResolverTests.java | 35 +- .../authc/ldap/support/LdapTestCase.java | 115 +- .../SessionFactoryLoadBalancingTests.java | 80 +- .../ldap/support/SessionFactoryTests.java | 136 +- .../oidc/OpenIdConnectAuthenticatorTests.java | 303 +- .../oidc/OpenIdConnectRealmSettingsTests.java | 265 +- .../authc/oidc/OpenIdConnectRealmTests.java | 224 +- .../authc/oidc/OpenIdConnectTestCase.java | 59 +- .../security/authc/pki/PkiRealmTests.java | 332 +- .../authc/saml/SamlAuthenticatorTests.java | 234 +- .../saml/SamlAuthnRequestBuilderTests.java | 64 +- .../saml/SamlLogoutRequestHandlerTests.java | 26 +- .../SamlLogoutRequestMessageBuilderTests.java | 25 +- ...amlLogoutResponseHandlerHttpPostTests.java | 24 +- ...ogoutResponseHandlerHttpRedirectTests.java | 13 +- .../authc/saml/SamlMetadataCommandTests.java | 315 +- .../authc/saml/SamlRealmTestHelper.java | 53 +- .../security/authc/saml/SamlRealmTests.java | 382 +- .../authc/saml/SamlRedirectTests.java | 94 +- .../authc/saml/SamlResponseHandlerTests.java | 65 +- .../saml/SamlSpMetadataBuilderTests.java | 92 +- .../security/authc/saml/SamlTestCase.java | 38 +- .../authc/saml/SigningConfigurationTests.java | 4 +- .../CachingServiceAccountTokenStoreTests.java | 12 +- ...ompositeServiceAccountTokenStoreTests.java | 13 +- .../service/ElasticServiceAccountsTests.java | 152 +- .../FileServiceAccountTokenStoreTests.java | 86 +- .../IndexServiceAccountTokenStoreTests.java | 176 +- .../authc/service/ServiceAccountIdTests.java | 44 +- .../service/ServiceAccountServiceTests.java | 468 +- .../service/ServiceAccountTokenTests.java | 47 +- .../authc/support/ApiKeyGeneratorTests.java | 11 +- .../CachingUsernamePasswordRealmTests.java | 176 +- .../DelegatedAuthorizationSupportTests.java | 53 +- .../DistinguishedNamePredicateTests.java | 10 +- .../authc/support/DnRoleMapperTests.java | 95 +- .../support/DummyUsernamePasswordRealm.java | 2 +- .../security/authc/support/HasherTests.java | 196 +- .../authc/support/RealmUserLookupTests.java | 33 +- .../RoleMappingFileBootstrapCheckTests.java | 36 +- .../support/SecondaryAuthenticatorTests.java | 85 +- .../TokensInvalidationResultTests.java | 83 +- .../support/UsernamePasswordTokenTests.java | 4 +- .../mapper/ExpressionRoleMappingTests.java | 182 +- .../mapper/NativeRoleMappingStoreTests.java | 147 +- .../authz/AuthorizationServiceTests.java | 1813 +- .../authz/AuthorizationUtilsTests.java | 22 +- .../authz/AuthorizedIndicesTests.java | 352 +- ...DlsFlsRequestCacheDifferentiatorTests.java | 39 +- .../authz/IndicesAndAliasesResolverTests.java | 989 +- .../xpack/security/authz/RBACEngineTests.java | 978 +- .../security/authz/RoleDescriptorTests.java | 312 +- ...ldDataCacheWithFieldSubsetReaderTests.java | 34 +- .../accesscontrol/FieldExtractorTests.java | 20 +- .../IndicesAccessControlTests.java | 53 +- .../accesscontrol/IndicesPermissionTests.java | 403 +- .../accesscontrol/OptOutQueryCacheTests.java | 103 +- ...IndicesAliasesRequestInterceptorTests.java | 86 +- .../ResizeRequestInterceptorTests.java | 77 +- .../SearchRequestInterceptorTests.java | 24 +- .../ShardSearchRequestInterceptorTests.java | 18 +- .../permission/FieldPermissionsTests.java | 289 +- .../authz/permission/PermissionTests.java | 9 +- .../authz/store/CompositeRolesStoreTests.java | 791 +- ...eprecationRoleDescriptorConsumerTests.java | 282 +- .../authz/store/FileRolesStoreTests.java | 193 +- .../store/NativePrivilegeStoreTests.java | 440 +- .../authz/store/NativeRolesStoreTests.java | 166 +- ...ExternalEnrollmentTokenGeneratorTests.java | 292 +- .../ingest/SetSecurityUserProcessorTests.java | 256 +- .../operator/FileOperatorUsersStoreTests.java | 66 +- .../operator/OperatorOnlyRegistryTests.java | 22 +- .../operator/OperatorPrivilegesTests.java | 55 +- .../security/rest/RestRequestFilterTests.java | 38 +- .../rest/SecurityRestFilterTests.java | 101 +- ...SecurityRestFilterWarningHeadersTests.java | 30 +- .../action/SecurityBaseRestHandlerTests.java | 3 +- .../apikey/RestCreateApiKeyActionTests.java | 43 +- .../apikey/RestGetApiKeyActionTests.java | 100 +- .../RestInvalidateApiKeyActionTests.java | 83 +- .../apikey/RestQueryApiKeyActionTests.java | 56 +- .../EnrollmentBaseRestHandlerTests.java | 18 +- .../oauth2/RestGetTokenActionTests.java | 80 +- .../RestInvalidateTokenActionTests.java | 40 +- .../action/saml/SamlBaseRestHandlerTests.java | 14 +- ...viceAccountTokenStoreCacheActionTests.java | 65 +- .../RestGetUserPrivilegesActionTests.java | 120 +- .../user/RestHasPrivilegesActionTests.java | 43 +- .../support/ApiKeyBoolQueryBuilderTests.java | 83 +- .../CacheInvalidatorRegistryTests.java | 54 +- .../security/support/FileLineParserTests.java | 30 +- .../support/FileReloadListenerTests.java | 7 +- .../support/LockingAtomicCounterTests.java | 2 +- .../support/SecurityIndexManagerTests.java | 152 +- .../xpack/security/test/SecurityMocks.java | 15 +- .../security/test/SecurityTestUtils.java | 18 +- ...stractSimpleSecurityTransportTestCase.java | 94 +- .../transport/SecurityHttpSettingsTests.java | 6 +- ...curityServerTransportInterceptorTests.java | 229 +- .../transport/ServerTransportFilterTests.java | 30 +- .../transport/filter/IPFilterTests.java | 131 +- .../transport/filter/PatternRuleTests.java | 1 + .../filter/SecurityIpFilterRuleTests.java | 5 +- .../IpFilterRemoteAddressFilterTests.java | 31 +- ...ecurityNetty4HttpServerTransportTests.java | 150 +- ...pleSecurityNetty4ServerTransportTests.java | 31 +- .../transport/nio/NioIPFilterTests.java | 24 +- .../transport/nio/SSLChannelContextTests.java | 29 +- .../transport/nio/SSLDriverTests.java | 62 +- .../SecurityNioHttpServerTransportTests.java | 143 +- .../nio/SimpleSecurityNioTransportTests.java | 27 +- .../security/user/AnonymousUserTests.java | 16 +- .../security/user/UserSerializationTests.java | 9 +- ...orMessageCertificateVerificationTests.java | 85 +- .../xpack/ssl/SSLErrorMessageFileTests.java | 116 +- .../xpack/shutdown/NodeShutdownIT.java | 15 +- ...pshotRepoTestKitClientYamlTestSuiteIT.java | 1 + ...undingBoxQueryGeoShapeWithDocValuesIT.java | 15 +- ...BoxQueryLegacyGeoShapeWithDocValuesIT.java | 19 +- .../search/GeoShapeScriptDocValuesIT.java | 43 +- .../search/GeoShapeWithDocValuesIT.java | 58 +- .../GeoShapeWithDocValuesQueryTests.java | 52 +- .../search/LegacyGeoShapeWithDocValuesIT.java | 59 +- .../search/ShapeQueryOverPointTests.java | 87 +- .../search/ShapeQueryOverShapeTests.java | 198 +- .../spatial/search/ShapeQueryTestCase.java | 260 +- .../xpack/spatial/SpatialPlugin.java | 120 +- .../xpack/spatial/SpatialUsage.java | 3 +- .../xpack/spatial/SpatialUtils.java | 24 +- .../action/SpatialStatsTransportAction.java | 38 +- .../action/SpatialUsageTransportAction.java | 37 +- .../xpack/spatial/common/CartesianPoint.java | 110 +- .../xpack/spatial/common/ShapeUtils.java | 24 +- .../index/fielddata/CentroidCalculator.java | 43 +- .../index/fielddata/Component2DVisitor.java | 11 +- .../xpack/spatial/index/fielddata/Extent.java | 27 +- .../index/fielddata/GeoShapeValues.java | 19 +- .../fielddata/GeometryDocValueReader.java | 2 +- .../fielddata/GeometryDocValueWriter.java | 8 +- .../fielddata/IndexGeoShapeFieldData.java | 4 +- .../index/fielddata/Tile2DVisitor.java | 54 +- .../index/fielddata/TriangleTreeReader.java | 36 +- .../index/fielddata/TriangleTreeWriter.java | 17 +- .../AbstractAtomicGeoShapeShapeFieldData.java | 3 +- .../AbstractLatLonShapeIndexFieldData.java | 38 +- .../GeoShapeWithDocValuesFieldMapper.java | 99 +- .../mapper/LatLonShapeDocValuesQuery.java | 8 +- .../index/mapper/PointFieldMapper.java | 83 +- .../index/mapper/ShapeFieldMapper.java | 63 +- .../spatial/index/mapper/ShapeIndexer.java | 6 +- .../index/query/ShapeQueryBuilder.java | 22 +- .../index/query/ShapeQueryPointProcessor.java | 43 +- .../index/query/ShapeQueryProcessor.java | 17 +- .../xpack/spatial/ingest/CircleProcessor.java | 58 +- .../GeoLineAggregationBuilder.java | 55 +- .../aggregations/GeoLineAggregator.java | 20 +- .../GeoLineAggregatorFactory.java | 32 +- .../aggregations/GeoLineBucketedSort.java | 40 +- .../search/aggregations/InternalGeoLine.java | 39 +- .../geogrid/AbstractGeoHashGridTiler.java | 35 +- .../geogrid/AbstractGeoTileGridTiler.java | 32 +- .../geogrid/BoundedGeoTileGridTiler.java | 2 +- .../bucket/geogrid/GeoGridTiler.java | 1 - .../bucket/geogrid/GeoShapeCellIdSource.java | 2 +- .../bucket/geogrid/GeoShapeCellValues.java | 5 +- .../geogrid/GeoShapeHashGridAggregator.java | 14 +- .../geogrid/GeoShapeTileGridAggregator.java | 14 +- .../geogrid/UnboundedGeoHashGridTiler.java | 4 +- .../geogrid/UnboundedGeoTileGridTiler.java | 5 +- .../metrics/GeoShapeBoundsAggregator.java | 18 +- .../metrics/GeoShapeCentroidAggregator.java | 19 +- .../support/GeoLineMultiValuesSource.java | 8 +- .../support/GeoShapeValuesSource.java | 2 +- .../support/GeoShapeValuesSourceType.java | 13 +- .../org/apache/lucene/geo/XShapeTestUtil.java | 33 +- .../xpack/spatial/SpatialPluginTests.java | 30 +- .../xpack/spatial/SpatialUtilsTests.java | 16 +- .../SpatialInfoTransportActionTests.java | 35 +- .../SpatialStatsTransportActionTests.java | 22 +- .../fielddata/CentroidCalculatorTests.java | 89 +- .../GeoShapeCoordinateEncoderTests.java | 6 +- .../fielddata/GeometryDocValueTests.java | 61 +- .../index/fielddata/Tile2DVisitorTests.java | 63 +- .../index/fielddata/TriangleTreeTests.java | 2 +- .../mapper/CartesianFieldMapperTests.java | 67 +- ...GeoShapeWithDocValuesFieldMapperTests.java | 125 +- .../GeoShapeWithDocValuesFieldTypeTests.java | 22 +- .../LatLonShapeDocValuesQueryTests.java | 25 +- .../index/mapper/PointFieldMapperTests.java | 39 +- .../index/mapper/PointFieldTypeTests.java | 4 +- .../index/mapper/ShapeFieldMapperTests.java | 56 +- .../index/mapper/ShapeFieldTypeTests.java | 7 +- ...eoShapeWithDocValuesQueryBuilderTests.java | 17 +- .../GeoShapeWithDocValuesQueryTests.java | 52 +- ...LegacyGeoShapeWithDocValuesQueryTests.java | 197 +- .../ShapeQueryBuilderOverPointTests.java | 9 +- .../ShapeQueryBuilderOverShapeTests.java | 10 +- .../index/query/ShapeQueryBuilderTests.java | 57 +- .../ingest/CircleProcessorFactoryTests.java | 13 +- .../spatial/ingest/CircleProcessorTests.java | 56 +- .../GeoLineAggregationBuilderTests.java | 30 +- .../aggregations/GeoLineAggregatorTests.java | 188 +- .../aggregations/InternalGeoLineTests.java | 18 +- .../aggregations/MergedGeoLinesTests.java | 12 +- .../bucket/geogrid/GeoGridTilerTestCase.java | 73 +- .../bucket/geogrid/GeoHashTilerTests.java | 24 +- .../geogrid/GeoShapeGeoGridTestCase.java | 122 +- .../GeoShapeGeoTileGridAggregatorTests.java | 11 +- .../bucket/geogrid/GeoTileTilerTests.java | 48 +- .../GeoShapeBoundsAggregatorTests.java | 90 +- .../GeoShapeCentroidAggregatorTests.java | 79 +- .../xpack/spatial/util/GeoTestUtils.java | 26 +- .../xpack/spatial/util/ShapeTestUtils.java | 14 +- .../spatial/SpatialClientYamlTestSuiteIT.java | 1 + .../elasticsearch/xpack/sql/jdbc/Debug.java | 9 +- .../xpack/sql/jdbc/DebugLog.java | 87 +- .../xpack/sql/jdbc/DefaultCursor.java | 3 +- .../xpack/sql/jdbc/EsDataSource.java | 3 +- .../elasticsearch/xpack/sql/jdbc/EsType.java | 71 +- .../xpack/sql/jdbc/InfoResponse.java | 1 - .../xpack/sql/jdbc/JdbcColumnInfo.java | 12 +- .../xpack/sql/jdbc/JdbcConfiguration.java | 40 +- .../xpack/sql/jdbc/JdbcConnection.java | 4 +- .../xpack/sql/jdbc/JdbcDatabaseMetaData.java | 644 +- .../xpack/sql/jdbc/JdbcDateUtils.java | 1 + .../xpack/sql/jdbc/JdbcHttpClient.java | 39 +- .../xpack/sql/jdbc/JdbcPreparedStatement.java | 77 +- .../xpack/sql/jdbc/JdbcResultSet.java | 30 +- .../xpack/sql/jdbc/JdbcStatement.java | 7 +- .../xpack/sql/jdbc/Nullable.java | 2 +- .../xpack/sql/jdbc/PreparedQuery.java | 6 +- .../sql/jdbc/SqlQueryParameterAnalyzer.java | 3 +- .../xpack/sql/jdbc/TypeConverter.java | 24 +- .../xpack/sql/jdbc/TypeUtils.java | 16 +- .../xpack/sql/jdbc/XContentSqlExtension.java | 8 +- .../xpack/sql/jdbc/ColumnInfoTests.java | 9 +- .../JdbcConfigurationDataSourceTests.java | 20 +- .../sql/jdbc/JdbcConfigurationTests.java | 42 +- .../sql/jdbc/JdbcDatabaseMetaDataTests.java | 3 +- .../sql/jdbc/JdbcHttpClientRequestTests.java | 31 +- .../sql/jdbc/JdbcPreparedStatementTests.java | 49 +- .../sql/jdbc/JdbcResultSetMetaDataTests.java | 10 +- .../jdbc/SqlQueryParameterAnalyzerTests.java | 22 +- .../xpack/sql/jdbc/TypeConverterTests.java | 5 +- .../xpack/sql/jdbc/VersionParityTests.java | 26 +- .../xpack/sql/jdbc/WebServerTestCase.java | 8 +- .../qa/jdbc/security/JdbcConnectionIT.java | 4 +- .../xpack/sql/qa/jdbc/JdbcTestUtils.java | 1 - .../qa/jdbc/PreparedStatementTestCase.java | 24 +- .../xpack/sql/qa/jdbc/ResultSetTestCase.java | 135 +- .../xpack/sql/qa/mixed_node/SqlCompatIT.java | 4 +- .../xpack/sql/qa/mixed_node/SqlSearchIT.java | 162 +- .../sql/qa/multi_node/RestSqlMultinodeIT.java | 4 +- .../ConsistentFunctionArgHandlingIT.java | 4 +- .../sql/qa/single_node/GeoJdbcCsvSpecIT.java | 1 + .../sql/qa/single_node/JdbcDocCsvSpecIT.java | 1 + .../sql/action/AbstractSqlQueryRequest.java | 115 +- .../xpack/sql/action/BasicFormatter.java | 1 - .../sql/action/SqlClearCursorRequest.java | 10 +- .../action/SqlClearCursorRequestBuilder.java | 3 +- .../sql/action/SqlClearCursorResponse.java | 2 +- .../xpack/sql/action/SqlQueryRequest.java | 119 +- .../sql/action/SqlQueryRequestBuilder.java | 81 +- .../xpack/sql/action/SqlQueryResponse.java | 9 +- .../xpack/sql/action/SqlQueryTask.java | 20 +- .../xpack/sql/action/SqlTranslateRequest.java | 27 +- .../action/SqlTranslateRequestBuilder.java | 38 +- .../sql/action/SqlTranslateResponse.java | 2 +- .../action/SqlClearCursorRequestTests.java | 10 +- .../action/SqlClearCursorResponseTests.java | 6 +- .../sql/action/SqlQueryRequestTests.java | 103 +- .../sql/action/SqlQueryResponseTests.java | 62 +- .../sql/action/SqlRequestParsersTests.java | 226 +- .../xpack/sql/action/SqlTestUtils.java | 4 +- .../sql/action/SqlTranslateRequestTests.java | 51 +- .../org/elasticsearch/xpack/sql/cli/Cli.java | 76 +- .../xpack/sql/cli/CliTerminal.java | 3 +- .../xpack/sql/cli/Completers.java | 7 +- .../xpack/sql/cli/ConnectionBuilder.java | 12 +- .../xpack/sql/cli/FatalCliException.java | 2 +- .../xpack/sql/cli/JLineTerminal.java | 8 +- .../cli/command/AbstractServerCliCommand.java | 11 +- .../xpack/sql/cli/command/CliSession.java | 9 +- .../sql/cli/command/ServerInfoCliCommand.java | 14 +- .../xpack/sql/cli/CliReplTests.java | 19 +- .../xpack/sql/cli/CliSessionTests.java | 30 +- .../xpack/sql/cli/ConnectionBuilderTests.java | 19 +- .../xpack/sql/cli/SqlCliTestCase.java | 28 +- .../xpack/sql/cli/TestTerminal.java | 2 +- .../sql/cli/command/BuiltinCommandTests.java | 1 - .../sql/cli/command/CliCommandsTests.java | 6 +- .../command/ServerInfoCliCommandTests.java | 5 +- .../command/ServerQueryCliCommandTests.java | 15 +- .../xpack/sql/client/ClientVersion.java | 5 +- .../sql/client/ConnectionConfiguration.java | 61 +- .../xpack/sql/client/HttpClient.java | 135 +- .../sql/client/JreHttpUrlConnection.java | 79 +- .../xpack/sql/client/RemoteFailure.java | 147 +- .../xpack/sql/client/SslConfig.java | 35 +- .../xpack/sql/client/StringUtils.java | 35 +- .../xpack/sql/client/UriUtils.java | 37 +- .../sql/client/HttpClientRequestTests.java | 52 +- .../xpack/sql/client/RemoteFailureTests.java | 109 +- .../xpack/sql/client/UriUtilsTests.java | 155 +- .../xpack/sql/client/VersionTests.java | 14 +- .../xpack/sql/proto/ColumnInfo.java | 28 +- .../xpack/sql/proto/MainResponse.java | 31 +- .../elasticsearch/xpack/sql/proto/Mode.java | 1 - .../xpack/sql/proto/ProtoUtils.java | 5 +- .../sql/proto/SqlClearCursorResponse.java | 11 +- .../xpack/sql/proto/SqlQueryRequest.java | 153 +- .../xpack/sql/proto/SqlQueryResponse.java | 44 +- .../xpack/sql/proto/SqlTypedParamValue.java | 15 +- .../xpack/sql/proto/SqlVersion.java | 21 +- .../xpack/sql/proto/StringUtils.java | 90 +- .../xpack/sql/proto/ProtoUtilsTests.java | 9 +- .../xpack/sql/proto/SqlVersionTests.java | 7 +- .../AbstractSqlBlockingIntegTestCase.java | 11 +- .../sql/action/AsyncSqlSearchActionIT.java | 104 +- .../sql/action/RestSqlCancellationIT.java | 40 +- .../xpack/sql/action/SqlActionIT.java | 25 +- .../xpack/sql/action/SqlCancellationIT.java | 35 +- .../sql/action/SqlClearCursorActionIT.java | 32 +- .../xpack/sql/action/SqlLicenseIT.java | 46 +- .../sql/action/SqlTranslateActionIT.java | 19 +- .../xpack/sql/SqlInfoTransportAction.java | 3 +- .../xpack/sql/SqlUsageTransportAction.java | 23 +- .../xpack/sql/analysis/analyzer/Analyzer.java | 278 +- .../xpack/sql/analysis/analyzer/Verifier.java | 331 +- .../xpack/sql/execution/PlanExecutor.java | 15 +- .../execution/search/CompositeAggCursor.java | 46 +- .../sql/execution/search/PivotCursor.java | 12 +- .../sql/execution/search/PivotRowSet.java | 10 +- .../xpack/sql/execution/search/Querier.java | 179 +- .../sql/execution/search/ResultRowSet.java | 1 - .../sql/execution/search/ScrollCursor.java | 34 +- .../sql/execution/search/SearchHitRowSet.java | 15 +- .../sql/execution/search/SourceGenerator.java | 16 +- .../extractor/CompositeKeyExtractor.java | 6 +- .../search/extractor/FieldHitExtractor.java | 4 +- .../search/extractor/MetricAggExtractor.java | 17 +- .../search/extractor/PivotExtractor.java | 4 +- .../search/extractor/TopHitsAggExtractor.java | 1 + .../sql/expression/SqlTypeResolutions.java | 15 +- .../function/SqlFunctionRegistry.java | 70 +- .../aggregate/PercentileAggregate.java | 41 +- .../PercentileCompoundAggregate.java | 3 +- .../function/grouping/Histogram.java | 11 +- .../sql/expression/function/scalar/Cast.java | 19 +- .../scalar/datetime/BaseDateTimeFunction.java | 3 +- .../datetime/BinaryDateTimeFunction.java | 12 +- .../datetime/BinaryDateTimeProcessor.java | 7 +- .../function/scalar/datetime/CurrentTime.java | 3 +- .../function/scalar/datetime/DateAdd.java | 30 +- .../scalar/datetime/DateAddProcessor.java | 10 +- .../function/scalar/datetime/DateDiff.java | 68 +- .../scalar/datetime/DateDiffProcessor.java | 16 +- .../scalar/datetime/DatePartProcessor.java | 10 +- .../datetime/DateTimeFormatProcessor.java | 19 +- .../scalar/datetime/DateTimeFunction.java | 4 +- .../scalar/datetime/DateTimeParse.java | 2 +- .../scalar/datetime/DateTimeParsePipe.java | 2 +- .../datetime/DateTimeParseProcessor.java | 5 +- .../scalar/datetime/DateTimeProcessor.java | 5 +- .../function/scalar/datetime/DateTrunc.java | 212 +- .../scalar/datetime/DateTruncProcessor.java | 19 +- .../datetime/NamedDateTimeFunction.java | 5 +- .../datetime/NamedDateTimeProcessor.java | 4 +- .../datetime/NonIsoDateTimeFunction.java | 5 +- .../datetime/NonIsoDateTimeProcessor.java | 7 +- .../scalar/datetime/QuarterProcessor.java | 1 - .../datetime/ThreeArgsDateTimeFunction.java | 19 +- .../datetime/ThreeArgsDateTimePipe.java | 5 +- .../datetime/ThreeArgsDateTimeProcessor.java | 8 +- .../function/scalar/datetime/TimeParse.java | 2 +- .../scalar/datetime/TimeProcessor.java | 5 +- .../scalar/datetime/ToCharFormatter.java | 24 +- .../function/scalar/geo/StAswkt.java | 1 - .../function/scalar/geo/StDistance.java | 6 +- .../scalar/geo/StDistanceFunction.java | 2 +- .../function/scalar/geo/StDistancePipe.java | 3 +- .../scalar/geo/StDistanceProcessor.java | 3 +- .../function/scalar/geo/StGeometryType.java | 1 - .../scalar/geo/StWkttosqlProcessor.java | 6 +- .../expression/function/scalar/geo/StX.java | 1 - .../expression/function/scalar/geo/StY.java | 1 - .../expression/function/scalar/geo/StZ.java | 1 - .../function/scalar/geo/UnaryGeoFunction.java | 13 +- .../expression/function/scalar/math/ACos.java | 1 - .../scalar/math/BinaryNumericFunction.java | 4 +- .../scalar/math/BinaryOptionalMathPipe.java | 4 +- .../math/BinaryOptionalMathProcessor.java | 4 +- .../math/BinaryOptionalNumericFunction.java | 24 +- .../expression/function/scalar/math/E.java | 1 - .../function/scalar/math/MathFunction.java | 3 +- .../function/scalar/math/MathProcessor.java | 4 +- .../expression/function/scalar/math/Pi.java | 1 - .../scalar/string/BinaryStringFunction.java | 15 +- .../string/BinaryStringNumericPipe.java | 3 +- .../string/BinaryStringNumericProcessor.java | 6 +- .../string/BinaryStringStringProcessor.java | 4 +- .../function/scalar/string/BitLength.java | 2 +- .../function/scalar/string/Concat.java | 8 +- .../scalar/string/ConcatFunctionPipe.java | 3 +- .../string/ConcatFunctionProcessor.java | 6 +- .../function/scalar/string/Insert.java | 53 +- .../scalar/string/InsertFunctionPipe.java | 26 +- .../string/InsertFunctionProcessor.java | 10 +- .../function/scalar/string/Locate.java | 46 +- .../scalar/string/LocateFunctionPipe.java | 5 +- .../string/LocateFunctionProcessor.java | 7 +- .../function/scalar/string/Position.java | 11 +- .../function/scalar/string/Replace.java | 32 +- .../scalar/string/ReplaceFunctionPipe.java | 4 +- .../string/ReplaceFunctionProcessor.java | 12 +- .../function/scalar/string/Substring.java | 26 +- .../string/SubstringFunctionProcessor.java | 13 +- .../scalar/string/UnaryStringFunction.java | 15 +- .../scalar/string/UnaryStringIntFunction.java | 9 +- .../whitelist/InternalSqlScriptUtils.java | 8 +- .../sql/expression/literal/geo/GeoShape.java | 23 +- .../expression/literal/interval/Interval.java | 3 +- .../literal/interval/IntervalYearMonth.java | 12 +- .../literal/interval/Intervals.java | 149 +- .../ArbitraryConditionalFunction.java | 2 +- .../predicate/conditional/Case.java | 43 +- .../conditional/ConditionalFunction.java | 17 +- .../conditional/ConditionalProcessor.java | 7 +- .../predicate/conditional/IfConditional.java | 3 +- .../expression/predicate/conditional/Iif.java | 21 +- .../predicate/conditional/NullIf.java | 3 +- .../conditional/NullIfProcessor.java | 4 +- .../DateTimeArithmeticOperation.java | 2 +- .../SqlBinaryArithmeticOperation.java | 24 +- .../predicate/operator/arithmetic/Sub.java | 11 +- .../xpack/sql/optimizer/Optimizer.java | 200 +- .../xpack/sql/parser/CommandBuilder.java | 25 +- .../xpack/sql/parser/ExpressionBuilder.java | 153 +- .../xpack/sql/parser/LogicalPlanBuilder.java | 20 +- .../xpack/sql/parser/SqlBaseBaseListener.java | 2968 +-- .../xpack/sql/parser/SqlBaseBaseVisitor.java | 1872 +- .../xpack/sql/parser/SqlBaseLexer.java | 1480 +- .../xpack/sql/parser/SqlBaseListener.java | 2516 +-- .../xpack/sql/parser/SqlBaseParser.java | 16469 +++++++++------- .../xpack/sql/parser/SqlBaseVisitor.java | 1466 +- .../xpack/sql/parser/SqlParser.java | 102 +- .../xpack/sql/plan/logical/Join.java | 18 +- .../xpack/sql/plan/logical/Pivot.java | 28 +- .../xpack/sql/plan/logical/SubQueryAlias.java | 5 +- .../xpack/sql/plan/logical/command/Debug.java | 13 +- .../sql/plan/logical/command/Explain.java | 51 +- .../sql/plan/logical/command/ShowColumns.java | 30 +- .../plan/logical/command/ShowFunctions.java | 12 +- .../sql/plan/logical/command/ShowTables.java | 16 +- .../plan/logical/command/sys/SysColumns.java | 131 +- .../plan/logical/command/sys/SysTables.java | 78 +- .../plan/logical/command/sys/SysTypes.java | 108 +- .../sql/plan/physical/AggregateExec.java | 12 +- .../xpack/sql/plan/physical/BinaryExec.java | 4 +- .../xpack/sql/plan/physical/EsQueryExec.java | 4 +- .../xpack/sql/plan/physical/FilterExec.java | 3 +- .../xpack/sql/plan/physical/LimitExec.java | 3 +- .../xpack/sql/plan/physical/OrderExec.java | 3 +- .../xpack/sql/plan/physical/PivotExec.java | 3 +- .../xpack/sql/plan/physical/ProjectExec.java | 3 +- .../xpack/sql/plan/physical/Unexecutable.java | 1 - .../xpack/sql/planner/Mapper.java | 8 +- .../xpack/sql/planner/QueryFolder.java | 212 +- .../xpack/sql/planner/QueryTranslator.java | 100 +- .../sql/plugin/RestSqlClearCursorAction.java | 5 +- .../xpack/sql/plugin/RestSqlQueryAction.java | 13 +- .../sql/plugin/RestSqlTranslateAction.java | 10 +- .../xpack/sql/plugin/SqlMediaTypeParser.java | 22 +- .../xpack/sql/plugin/SqlPlugin.java | 131 +- .../xpack/sql/plugin/SqlResponseListener.java | 17 +- .../xpack/sql/plugin/TextFormat.java | 62 +- .../xpack/sql/plugin/TextFormatterCursor.java | 13 +- .../TransportSqlAsyncGetResultsAction.java | 29 +- .../TransportSqlAsyncGetStatusAction.java | 30 +- .../plugin/TransportSqlClearCursorAction.java | 16 +- .../sql/plugin/TransportSqlQueryAction.java | 165 +- .../sql/plugin/TransportSqlStatsAction.java | 37 +- .../plugin/TransportSqlTranslateAction.java | 51 +- .../xpack/sql/querydsl/agg/Agg.java | 3 +- .../xpack/sql/querydsl/agg/AggFilter.java | 3 +- .../xpack/sql/querydsl/agg/AggSource.java | 3 +- .../xpack/sql/querydsl/agg/Aggs.java | 4 +- .../sql/querydsl/agg/FilterExistsAgg.java | 9 +- .../querydsl/agg/GroupByDateHistogram.java | 22 +- .../xpack/sql/querydsl/agg/GroupByKey.java | 3 +- .../querydsl/agg/GroupByNumericHistogram.java | 3 +- .../sql/querydsl/agg/PercentileRanksAgg.java | 3 +- .../sql/querydsl/agg/PercentilesAgg.java | 3 +- .../xpack/sql/querydsl/agg/TopHitsAgg.java | 10 +- .../sql/querydsl/container/AggregateSort.java | 4 +- .../sql/querydsl/container/GroupByRef.java | 3 +- .../container/GroupingFunctionSort.java | 3 +- .../querydsl/container/QueryContainer.java | 219 +- .../sql/querydsl/container/ScoreSort.java | 3 +- .../xpack/sql/session/Cursors.java | 2 +- .../xpack/sql/session/ListCursor.java | 14 +- .../xpack/sql/session/ListRowSet.java | 1 - .../xpack/sql/session/SqlSession.java | 31 +- .../xpack/sql/stats/Metrics.java | 8 +- .../xpack/sql/stats/QueryMetric.java | 8 +- .../xpack/sql/type/SqlDataTypeConverter.java | 1 - .../xpack/sql/type/SqlDataTypes.java | 56 +- .../elasticsearch/xpack/sql/util/Check.java | 10 +- .../xpack/sql/util/DateUtils.java | 47 +- .../sql/SqlInfoTransportActionTests.java | 21 +- .../elasticsearch/xpack/sql/SqlTestUtils.java | 46 +- .../sql/action/AbstractSqlIntegTestCase.java | 1 - .../xpack/sql/action/BasicFormatterTests.java | 63 +- .../xpack/sql/analysis/CancellationTests.java | 56 +- .../analyzer/FieldAttributeTests.java | 106 +- .../analyzer/VerifierErrorMessagesTests.java | 1312 +- .../analysis/index/IndexResolverTests.java | 169 +- .../CompositeAggregationCursorTests.java | 23 +- .../sql/execution/search/QuerierTests.java | 30 +- .../execution/search/ScrollCursorTests.java | 17 +- .../search/SourceGeneratorTests.java | 42 +- .../search/SqlSourceBuilderTests.java | 4 +- .../extractor/CompositeKeyExtractorTests.java | 6 +- .../extractor/ComputingExtractorTests.java | 11 +- .../extractor/FieldHitExtractorTests.java | 19 +- .../extractor/MetricAggExtractorTests.java | 42 +- .../search/extractor/TestBucket.java | 2 +- .../extractor/TestMultiValueAggregation.java | 2 +- .../extractor/TestSingleValueAggregation.java | 2 +- .../extractor/TopHitsAggExtractorTests.java | 30 +- .../xpack/sql/expression/ProcessorTests.java | 9 +- .../function/SqlFunctionRegistryTests.java | 40 +- .../scalar/DatabaseFunctionTests.java | 27 +- .../function/scalar/UserFunctionTests.java | 28 +- .../scalar/datetime/CurrentDateTests.java | 12 +- .../scalar/datetime/CurrentDateTimeTests.java | 26 +- .../scalar/datetime/CurrentTimeTests.java | 26 +- .../scalar/datetime/DateAddPipeTests.java | 69 +- .../datetime/DateAddProcessorTests.java | 330 +- .../scalar/datetime/DateDiffPipeTests.java | 70 +- .../datetime/DateDiffProcessorTests.java | 604 +- .../scalar/datetime/DatePartPipeTests.java | 72 +- .../datetime/DatePartProcessorTests.java | 43 +- .../datetime/DateTimeFormatPipeTests.java | 12 +- .../DateTimeFormatProcessorTests.java | 69 +- .../datetime/DateTimeParsePipeTests.java | 51 +- .../datetime/DateTimeParseProcessorTests.java | 57 +- .../datetime/DateTimeProcessorTests.java | 4 +- .../scalar/datetime/DateTimeTestUtils.java | 5 +- .../DateTimeToCharProcessorTests.java | 64 +- .../scalar/datetime/DateTruncPipeTests.java | 72 +- .../datetime/DateTruncProcessorTests.java | 508 +- .../datetime/NamedDateTimeProcessorTests.java | 6 +- .../NonIsoDateTimeProcessorTests.java | 34 +- .../scalar/datetime/TimeProcessorTests.java | 3 +- .../scalar/datetime/ToCharTestScript.java | 83 +- .../scalar/geo/GeoProcessorTests.java | 59 +- .../scalar/geo/StDistanceProcessorTests.java | 12 +- .../scalar/geo/StWkttosqlProcessorTests.java | 1 - .../scalar/math/BinaryMathProcessorTests.java | 52 +- .../string/BinaryStringNumericPipeTests.java | 72 +- .../BinaryStringNumericProcessorTests.java | 97 +- .../string/BinaryStringStringPipeTests.java | 65 +- .../BinaryStringStringProcessorTests.java | 19 +- .../string/ConcatFunctionPipeTests.java | 53 +- .../scalar/string/ConcatProcessorTests.java | 17 +- .../string/InsertFunctionPipeTests.java | 75 +- .../scalar/string/InsertProcessorTests.java | 72 +- .../string/LocateFunctionPipeTests.java | 71 +- .../scalar/string/LocateProcessorTests.java | 37 +- .../string/ReplaceFunctionPipeTests.java | 59 +- .../scalar/string/ReplaceProcessorTests.java | 28 +- .../string/StringFunctionProcessorTests.java | 2 +- .../string/SubstringFunctionPipeTests.java | 55 +- .../string/SubstringProcessorTests.java | 49 +- .../literal/interval/IntervalsTests.java | 58 +- .../sql/expression/parser/ParameterTests.java | 37 +- .../sql/expression/parser/QuotingTests.java | 10 +- .../predicate/conditional/CaseTests.java | 50 +- .../predicate/conditional/IifTests.java | 41 +- .../arithmetic/SqlBinaryArithmeticTests.java | 5 +- .../sql/optimizer/OptimizerRunTests.java | 37 +- .../xpack/sql/optimizer/OptimizerTests.java | 190 +- .../sql/parser/EscapedFunctionsTests.java | 78 +- .../xpack/sql/parser/ExpressionTests.java | 72 +- .../sql/parser/LikeEscapingParsingTests.java | 37 +- .../xpack/sql/parser/ParamLiteralTests.java | 20 +- .../xpack/sql/parser/SqlParserTests.java | 150 +- .../plan/logical/UnresolvedRelationTests.java | 53 +- .../logical/command/sys/SysColumnsTests.java | 83 +- .../logical/command/sys/SysTablesTests.java | 50 +- .../logical/command/sys/SysTypesTests.java | 66 +- .../xpack/sql/planner/QueryFolderTests.java | 154 +- .../sql/planner/QueryTranslatorSpecTests.java | 5 +- .../sql/planner/QueryTranslatorTests.java | 537 +- .../xpack/sql/planner/VerifierTests.java | 102 +- .../xpack/sql/plugin/CursorTests.java | 54 +- .../sql/plugin/SqlMediaTypeParserTests.java | 111 +- .../xpack/sql/plugin/SqlPluginTests.java | 31 +- .../xpack/sql/plugin/TextFormatTests.java | 52 +- .../container/QueryContainerTests.java | 52 +- .../xpack/sql/session/ListCursorTests.java | 4 +- .../xpack/sql/stats/VerifierMetricsTests.java | 34 +- .../xpack/sql/tree/SqlNodeSubclassTests.java | 14 +- .../sql/type/SqlDataTypeConverterTests.java | 15 +- .../xpack/sql/type/SqlDataTypesTests.java | 100 +- .../xpack/sql/types/SqlTypesTests.java | 2 - .../test/rest/AbstractXPackRestTest.java | 45 +- .../xpack/test/rest/XPackRestIT.java | 2 +- .../xpack/stack/StackYamlIT.java | 1 + .../TextStructureWithSecurityIT.java | 1 + .../MultiClusterYamlTestSuiteIT.java | 5 +- .../xpack/transform/integration/LatestIT.java | 130 +- .../integration/TestFeatureResetIT.java | 28 +- .../transform/integration/TransformIT.java | 73 +- .../integration/TransformIntegTestCase.java | 46 +- .../TransformUsingSearchRuntimeFieldsIT.java | 289 +- .../continuous/ContinuousTestCase.java | 2 +- .../DateHistogramGroupByOtherTimeFieldIT.java | 1 - .../continuous/LatestContinuousIT.java | 91 +- .../continuous/TransformContinuousIT.java | 22 +- .../integration/TransformAuditorIT.java | 31 +- .../TransformConfigurationIndexIT.java | 21 +- .../TransformGetAndGetStatsIT.java | 4 +- .../integration/TransformMetadataIT.java | 26 +- .../integration/TransformPivotRestIT.java | 32 +- .../TransformPivotRestSpecialCasesIT.java | 2 +- .../integration/TransformProgressIT.java | 4 +- .../integration/TransformRestTestCase.java | 2 +- .../integration/TransformRobustnessIT.java | 11 +- .../TransformTaskFailedStateIT.java | 2 +- .../integration/TransformUsageIT.java | 11 +- .../TransformSingleNodeTestCase.java | 12 +- .../integration/TransformInternalIndexIT.java | 26 +- .../TransformClusterStateListener.java | 56 +- .../TransformUsageTransportAction.java | 12 +- .../action/TransformPrivilegeChecker.java | 16 +- .../TransportDeleteTransformAction.java | 12 +- .../action/TransportGetTransformAction.java | 18 +- .../TransportGetTransformStatsAction.java | 37 +- .../TransportPreviewTransformAction.java | 157 +- .../action/TransportPutTransformAction.java | 31 +- .../TransportSetTransformResetModeAction.java | 21 +- .../action/TransportStartTransformAction.java | 17 +- .../action/TransportStopTransformAction.java | 37 +- .../TransportValidateTransformAction.java | 62 +- .../checkpoint/DefaultCheckpointProvider.java | 33 +- .../TimeBasedCheckpointProvider.java | 31 +- .../TransformCheckpointService.java | 8 +- .../notifications/TransformAuditor.java | 4 +- .../transform/persistence/TransformIndex.java | 3 +- .../persistence/TransformInternalIndex.java | 278 +- .../rest/action/RestCatTransformAction.java | 5 +- .../rest/action/RestGetTransformAction.java | 10 +- .../action/RestGetTransformStatsAction.java | 13 +- .../rest/action/RestStopTransformAction.java | 13 +- .../transform/transforms/IDGenerator.java | 5 +- .../transforms/TransformContext.java | 14 +- .../transform/transforms/TransformNodes.java | 23 +- .../TransformPersistentTasksExecutor.java | 6 +- .../common/DocumentConversionUtils.java | 30 +- .../pivot/AggregationResultUtils.java | 6 +- .../transform/transforms/pivot/Pivot.java | 6 +- .../transforms/pivot/SchemaUtil.java | 57 +- .../pivot/TransformAggregations.java | 12 +- .../utils/SourceDestValidations.java | 13 +- .../TransformInfoTransportActionTests.java | 10 +- .../transform/TransformMetadataTests.java | 2 +- .../TransformPrivilegeCheckerTests.java | 119 +- .../DefaultCheckpointProviderTests.java | 30 +- .../TimeBasedCheckpointProviderTests.java | 124 +- .../notifications/MockTransformAuditor.java | 1 - .../SeqNoPrimaryTermAndIndexTests.java | 12 +- .../persistence/TransformIndexTests.java | 54 +- .../RestDeleteTransformActionTests.java | 16 +- .../ClientTransformIndexerTests.java | 13 +- .../transforms/TransformIndexerTests.java | 6 +- .../transforms/TransformNodesTests.java | 61 +- ...TransformPersistentTasksExecutorTests.java | 68 +- .../common/DocumentConversionUtilsTests.java | 45 +- .../latest/LatestChangeCollectorTests.java | 2 +- .../transforms/latest/LatestTests.java | 5 +- .../pivot/AggregationResultUtilsTests.java | 16 +- .../AggregationSchemaAndResultTests.java | 5 +- .../transforms/pivot/SchemaUtilTests.java | 33 +- .../pivot/TransformAggregationsTests.java | 2 +- .../vectortile/VectorTileCCSIT.java | 21 +- .../mapper/SparseVectorFieldMapperTests.java | 91 +- .../xpack/vectors/DenseVectorPlugin.java | 15 +- .../action/KnnSearchRequestBuilder.java | 50 +- .../vectors/action/RestKnnSearchAction.java | 5 +- .../mapper/DenseVectorFieldMapper.java | 166 +- .../mapper/SparseVectorFieldMapper.java | 16 +- .../vectors/mapper/VectorEncoderDecoder.java | 8 +- .../BinaryDenseVectorScriptDocValues.java | 2 - .../query/DenseVectorScriptDocValues.java | 8 +- .../query/DocValuesWhitelistExtension.java | 5 +- .../query/KnnDenseVectorScriptDocValues.java | 1 - .../vectors/query/KnnVectorQueryBuilder.java | 14 +- .../xpack/vectors/query/ScoreScriptUtils.java | 21 +- .../vectors/query/VectorDVLeafFieldData.java | 1 - .../vectors/query/VectorIndexFieldData.java | 21 +- .../action/KnnSearchRequestBuilderTests.java | 85 +- .../mapper/DenseVectorFieldMapperTests.java | 316 +- .../mapper/DenseVectorFieldTypeTests.java | 33 +- .../mapper/SparseVectorFieldTypeTests.java | 1 - ...BinaryDenseVectorScriptDocValuesTests.java | 27 +- .../query/DenseVectorFunctionTests.java | 7 +- .../KnnDenseVectorScriptDocValuesTests.java | 21 +- .../vectors/query/KnnSearchActionTests.java | 33 +- .../query/KnnVectorQueryBuilderTests.java | 80 +- .../SmokeTestWatcherTestSuiteIT.java | 41 +- .../MonitoringWithWatcherRestIT.java | 31 +- .../SmokeTestWatcherWithSecurityIT.java | 207 +- ...cherWithSecurityClientYamlTestSuiteIT.java | 9 +- .../watcher/WatcherConcreteIndexTests.java | 12 +- .../actions/ActionErrorIntegrationTests.java | 26 +- .../actions/TimeThrottleIntegrationTests.java | 47 +- .../actions/email/EmailAttachmentTests.java | 68 +- .../throttler/ActionThrottleTests.java | 198 +- .../webhook/WebhookHttpsIntegrationTests.java | 62 +- .../webhook/WebhookIntegrationTests.java | 89 +- .../ArrayCompareConditionSearchTests.java | 46 +- .../CompareConditionSearchTests.java | 55 +- .../ExecuteWatchQueuedStatsTests.java | 25 +- .../history/HistoryActionConditionTests.java | 61 +- .../HistoryTemplateEmailMappingsTests.java | 61 +- .../HistoryTemplateHttpMappingsTests.java | 68 +- ...storyTemplateIndexActionMappingsTests.java | 13 +- ...storyTemplateSearchInputMappingsTests.java | 26 +- .../HistoryTemplateTimeMappingsTests.java | 15 +- ...HistoryTemplateTransformMappingsTests.java | 87 +- .../input/chain/ChainIntegrationTests.java | 32 +- .../email/EmailSecretsIntegrationTests.java | 41 +- .../AbstractWatcherIntegrationTestCase.java | 220 +- .../test/integration/BasicWatcherTests.java | 303 +- .../test/integration/BootStrapTests.java | 272 +- .../ExecutionVarsIntegrationTests.java | 83 +- .../integration/HistoryIntegrationTests.java | 94 +- .../HttpSecretsIntegrationTests.java | 90 +- .../integration/RejectedExecutionTests.java | 15 +- .../test/integration/SingleNodeTests.java | 11 +- .../test/integration/WatchAckTests.java | 153 +- .../test/integration/WatchMetadataTests.java | 42 +- .../transform/TransformIntegrationTests.java | 98 +- .../action/activate/ActivateWatchTests.java | 67 +- .../action/delete/DeleteWatchTests.java | 15 +- .../action/execute/ExecuteWatchTests.java | 48 +- .../transport/action/get/GetWatchTests.java | 8 +- .../watch/WatchStatusIntegrationTests.java | 17 +- .../EncryptSensitiveDataBootstrapCheck.java | 28 +- .../elasticsearch/xpack/watcher/Watcher.java | 317 +- .../watcher/WatcherIndexingListener.java | 60 +- .../watcher/WatcherInfoTransportAction.java | 8 +- .../watcher/WatcherLifeCycleService.java | 9 +- .../watcher/WatcherPainlessExtension.java | 6 +- .../xpack/watcher/WatcherService.java | 124 +- .../watcher/WatcherUsageTransportAction.java | 44 +- .../xpack/watcher/actions/ActionBuilders.java | 3 +- .../watcher/actions/email/EmailAction.java | 96 +- .../actions/email/EmailActionFactory.java | 18 +- .../actions/email/ExecutableEmailAction.java | 9 +- .../actions/index/ExecutableIndexAction.java | 105 +- .../watcher/actions/index/IndexAction.java | 138 +- .../actions/index/IndexActionFactory.java | 9 +- .../watcher/actions/jira/JiraAction.java | 45 +- .../logging/ExecutableLoggingAction.java | 4 +- .../actions/logging/LoggingAction.java | 63 +- .../pagerduty/ExecutablePagerDutyAction.java | 8 +- .../actions/pagerduty/PagerDutyAction.java | 5 +- .../watcher/actions/slack/SlackAction.java | 53 +- .../actions/webhook/WebhookAction.java | 25 +- .../actions/webhook/WebhookActionFactory.java | 3 +- .../watcher/client/WatchSourceBuilders.java | 3 +- .../xpack/watcher/common/http/HttpClient.java | 58 +- .../watcher/common/http/HttpContentType.java | 9 +- .../xpack/watcher/common/http/HttpProxy.java | 3 +- .../watcher/common/http/HttpRequest.java | 159 +- .../common/http/HttpRequestTemplate.java | 138 +- .../watcher/common/http/HttpResponse.java | 36 +- .../watcher/common/http/HttpSettings.java | 48 +- .../watcher/common/text/TextTemplate.java | 15 +- .../common/text/TextTemplateEngine.java | 17 +- .../condition/ArrayCompareCondition.java | 139 +- .../watcher/condition/CompareCondition.java | 72 +- .../condition/InternalAlwaysCondition.java | 18 +- .../watcher/condition/LenientCompare.java | 5 +- .../watcher/condition/NeverCondition.java | 18 +- .../watcher/condition/ScriptCondition.java | 12 +- .../condition/WatcherConditionScript.java | 10 +- .../execution/AsyncTriggerEventConsumer.java | 11 +- .../watcher/execution/CurrentExecutions.java | 9 +- .../watcher/execution/ExecutionService.java | 142 +- .../execution/ManualExecutionContext.java | 37 +- .../execution/SyncTriggerEventConsumer.java | 13 +- .../execution/TriggeredExecutionContext.java | 17 +- .../watcher/execution/TriggeredWatch.java | 11 +- .../execution/TriggeredWatchStore.java | 21 +- .../xpack/watcher/history/HistoryStore.java | 15 +- .../xpack/watcher/input/InputBuilders.java | 3 +- .../xpack/watcher/input/InputRegistry.java | 29 +- .../xpack/watcher/input/chain/ChainInput.java | 14 +- .../input/chain/ChainInputFactory.java | 4 +- .../input/chain/ExecutableChainInput.java | 4 +- .../input/http/ExecutableHttpInput.java | 33 +- .../xpack/watcher/input/http/HttpInput.java | 86 +- .../input/search/ExecutableSearchInput.java | 36 +- .../watcher/input/search/SearchInput.java | 78 +- .../input/search/SearchInputFactory.java | 5 +- .../watcher/input/simple/SimpleInput.java | 8 +- .../transform/TransformInputFactory.java | 2 +- .../notification/NotificationService.java | 51 +- .../watcher/notification/email/Account.java | 47 +- .../notification/email/Attachment.java | 37 +- .../notification/email/Authentication.java | 3 +- .../notification/email/DataAttachment.java | 26 +- .../watcher/notification/email/Email.java | 178 +- .../notification/email/EmailService.java | 212 +- .../notification/email/EmailTemplate.java | 68 +- .../notification/email/HtmlSanitizer.java | 113 +- .../watcher/notification/email/Profile.java | 26 +- .../email/attachment/DataAttachment.java | 1 - .../attachment/DataAttachmentParser.java | 11 +- .../email/attachment/EmailAttachments.java | 3 +- .../HttpEmailAttachementParser.java | 41 +- .../attachment/HttpRequestAttachment.java | 12 +- .../email/attachment/ReportingAttachment.java | 29 +- .../attachment/ReportingAttachmentParser.java | 196 +- .../email/support/BodyPartSource.java | 8 +- .../notification/jira/JiraAccount.java | 30 +- .../watcher/notification/jira/JiraIssue.java | 45 +- .../notification/jira/JiraService.java | 47 +- .../notification/pagerduty/IncidentEvent.java | 211 +- .../pagerduty/IncidentEventContext.java | 139 +- .../pagerduty/IncidentEventDefaults.java | 5 +- .../pagerduty/PagerDutyAccount.java | 3 +- .../pagerduty/PagerDutyService.java | 23 +- .../notification/pagerduty/SentEvent.java | 56 +- .../notification/slack/SentMessages.java | 12 +- .../notification/slack/SlackAccount.java | 73 +- .../notification/slack/SlackService.java | 22 +- .../notification/slack/message/Action.java | 43 +- .../slack/message/Attachment.java | 317 +- .../slack/message/DynamicAttachments.java | 54 +- .../notification/slack/message/Field.java | 62 +- .../slack/message/SlackMessage.java | 102 +- .../slack/message/SlackMessageDefaults.java | 37 +- .../rest/action/RestAckWatchAction.java | 21 +- .../rest/action/RestActivateWatchAction.java | 64 +- .../rest/action/RestDeleteWatchAction.java | 13 +- .../rest/action/RestExecuteWatchAction.java | 74 +- .../rest/action/RestGetWatchAction.java | 7 +- .../rest/action/RestPutWatchAction.java | 24 +- .../rest/action/RestQueryWatchesAction.java | 5 +- .../rest/action/RestWatchServiceAction.java | 17 +- .../rest/action/RestWatcherStatsAction.java | 16 +- .../xpack/watcher/support/Strings.java | 3 +- .../support/WatcherIndexTemplateRegistry.java | 41 +- .../support/XContentFilterKeysUtils.java | 3 +- .../search/WatcherSearchTemplateRequest.java | 79 +- .../search/WatcherSearchTemplateService.java | 23 +- .../watcher/transform/TransformBuilders.java | 3 +- .../transform/script/ScriptTransform.java | 10 +- .../script/ScriptTransformFactory.java | 2 +- .../script/WatcherTransformScript.java | 10 +- .../search/ExecutableSearchTransform.java | 17 +- .../transform/search/SearchTransform.java | 47 +- .../search/SearchTransformFactory.java | 4 +- .../actions/TransportAckWatchAction.java | 69 +- .../actions/TransportActivateWatchAction.java | 60 +- .../actions/TransportDeleteWatchAction.java | 17 +- .../actions/TransportExecuteWatchAction.java | 87 +- .../actions/TransportGetWatchAction.java | 93 +- .../actions/TransportPutWatchAction.java | 78 +- .../actions/TransportQueryWatchesAction.java | 49 +- .../TransportWatcherServiceAction.java | 41 +- .../actions/TransportWatcherStatsAction.java | 39 +- .../actions/WatcherTransportAction.java | 14 +- .../watcher/trigger/TriggerBuilders.java | 3 +- .../xpack/watcher/trigger/TriggerService.java | 48 +- .../watcher/trigger/TriggerWatchStats.java | 14 +- .../watcher/trigger/manual/ManualTrigger.java | 22 +- .../trigger/manual/ManualTriggerEngine.java | 19 +- .../trigger/manual/ManualTriggerEvent.java | 4 +- .../trigger/schedule/CronSchedule.java | 13 +- .../trigger/schedule/DailySchedule.java | 21 +- .../trigger/schedule/HourlySchedule.java | 31 +- .../trigger/schedule/IntervalSchedule.java | 16 +- .../trigger/schedule/MonthlySchedule.java | 11 +- .../trigger/schedule/ScheduleRegistry.java | 6 +- .../schedule/ScheduleTriggerEngine.java | 5 +- .../schedule/ScheduleTriggerEvent.java | 34 +- .../watcher/trigger/schedule/Schedules.java | 3 +- .../trigger/schedule/WeeklySchedule.java | 9 +- .../trigger/schedule/YearlySchedule.java | 11 +- .../engine/TickerScheduleTriggerEngine.java | 10 +- .../trigger/schedule/support/DayOfWeek.java | 43 +- .../trigger/schedule/support/DayTimes.java | 51 +- .../trigger/schedule/support/Month.java | 73 +- .../trigger/schedule/support/MonthTimes.java | 22 +- .../trigger/schedule/support/WeekTimes.java | 11 +- .../trigger/schedule/support/YearTimes.java | 36 +- .../trigger/schedule/tool/CronEvalTool.java | 1 + .../xpack/watcher/watch/WatchParser.java | 129 +- ...cryptSensitiveDataBootstrapCheckTests.java | 6 +- .../watcher/WatcherIndexingListenerTests.java | 269 +- .../WatcherInfoTransportActionTests.java | 38 +- .../watcher/WatcherLifeCycleServiceTests.java | 239 +- .../WatcherMetadataSerializationTests.java | 15 +- .../xpack/watcher/WatcherPluginTests.java | 40 +- .../xpack/watcher/WatcherServiceTests.java | 123 +- .../watcher/actions/ActionWrapperTests.java | 113 +- .../actions/email/EmailActionTests.java | 236 +- .../actions/email/EmailMessageIdTests.java | 44 +- .../watcher/actions/email/EmailSslTests.java | 28 +- .../actions/index/IndexActionTests.java | 176 +- .../jira/ExecutableJiraActionTests.java | 43 +- .../actions/jira/JiraActionFactoryTests.java | 4 +- .../watcher/actions/jira/JiraActionTests.java | 26 +- .../actions/logging/LoggingActionTests.java | 11 +- .../PagerDutyActionFactoryTests.java | 11 +- .../pagerduty/PagerDutyActionTests.java | 54 +- .../slack/ExecutableSlackActionTests.java | 9 +- .../slack/SlackActionFactoryTests.java | 11 +- .../actions/slack/SlackActionTests.java | 26 +- .../actions/throttler/AckThrottlerTests.java | 5 +- .../throttler/PeriodThrottlerTests.java | 10 +- .../actions/webhook/WebhookActionTests.java | 94 +- .../watcher/common/http/HttpClientTests.java | 250 +- .../http/HttpConnectionTimeoutTests.java | 39 +- .../watcher/common/http/HttpProxyTests.java | 75 +- .../common/http/HttpReadTimeoutTests.java | 51 +- .../common/http/HttpRequestTemplateTests.java | 37 +- .../watcher/common/http/HttpRequestTests.java | 12 +- .../common/http/HttpResponseTests.java | 6 +- .../http/SizeLimitInputStreamTests.java | 8 +- .../common/text/TextTemplateTests.java | 142 +- .../condition/AlwaysConditionTests.java | 36 +- .../condition/ArrayCompareConditionTests.java | 107 +- .../condition/CompareConditionTests.java | 15 +- .../condition/NeverConditionTests.java | 9 +- .../condition/ScriptConditionTests.java | 113 +- .../execution/ExecutionServiceTests.java | 131 +- .../execution/TriggeredWatchStoreTests.java | 123 +- .../watcher/history/HistoryStoreTests.java | 11 +- .../watcher/input/InputRegistryTests.java | 2 +- .../watcher/input/chain/ChainInputTests.java | 110 +- .../chain/ExecutableChainInputTests.java | 8 +- .../watcher/input/http/HttpInputTests.java | 92 +- .../input/simple/SimpleInputTests.java | 5 +- .../input/transform/TransformInputTests.java | 41 +- .../NotificationServiceTests.java | 93 +- .../notification/email/AccountTests.java | 174 +- .../notification/email/AccountsTests.java | 61 +- .../notification/email/EmailServiceTests.java | 46 +- .../email/EmailTemplateTests.java | 87 +- .../notification/email/EmailTests.java | 4 +- .../email/HtmlSanitizerTests.java | 79 +- .../notification/email/ProfileTests.java | 38 +- .../attachment/DataAttachmentParserTests.java | 12 +- .../EmailAttachmentParsersTests.java | 40 +- .../HttpEmailAttachementParserTests.java | 100 +- .../ReportingAttachmentParserTests.java | 462 +- .../email/support/EmailServer.java | 13 +- .../notification/jira/JiraAccountTests.java | 38 +- .../notification/jira/JiraIssueTests.java | 12 +- .../pagerduty/IncidentEventTests.java | 32 +- .../pagerduty/PagerDutyAccountsTests.java | 31 +- .../pagerduty/SentEventTests.java | 18 +- .../slack/message/SlackMessageTests.java | 189 +- .../action/RestExecuteWatchActionTests.java | 8 +- .../watcher/support/FilterXContentTests.java | 98 +- .../xpack/watcher/support/VariablesTests.java | 14 +- .../support/WatcherDateTimeUtilsTests.java | 17 +- .../WatcherIndexTemplateRegistryTests.java | 122 +- .../watcher/support/WatcherTemplateTests.java | 65 +- .../watcher/support/WatcherUtilsTests.java | 51 +- .../WatcherSearchTemplateRequestTests.java | 4 +- .../support/xcontent/XContentSourceTests.java | 22 +- .../watcher/test/MockTextTemplateEngine.java | 2 +- .../xpack/watcher/test/TimeWarpedWatcher.java | 6 +- .../watcher/test/WatcherMockScriptPlugin.java | 60 +- .../xpack/watcher/test/WatcherTestUtils.java | 150 +- .../bench/ScheduleEngineTriggerBenchmark.java | 72 +- .../WatcherExecutorServiceBenchmark.java | 78 +- .../bench/WatcherScheduleEngineBenchmark.java | 151 +- .../test/integration/SearchInputTests.java | 99 +- .../integration/SearchTransformTests.java | 19 +- .../transform/chain/ChainTransformTests.java | 70 +- .../script/ScriptTransformTests.java | 42 +- .../action/QueryWatchesRequestTests.java | 6 +- .../action/QueryWatchesResponseTests.java | 29 +- .../action/WatchRequestValidationTests.java | 22 +- .../execute/ExecuteWatchRequestTests.java | 2 +- .../put/PutWatchSerializationTests.java | 16 +- .../actions/TransportAckWatchActionTests.java | 49 +- .../actions/TransportPutWatchActionTests.java | 13 +- .../TransportWatcherStatsActionTests.java | 20 +- .../trigger/ScheduleTriggerEngineMock.java | 4 +- .../watcher/trigger/TriggerServiceTests.java | 4 +- .../trigger/schedule/CronScheduleTests.java | 24 +- .../trigger/schedule/DailyScheduleTests.java | 56 +- .../trigger/schedule/HourlyScheduleTests.java | 44 +- .../schedule/IntervalScheduleTests.java | 8 +- .../schedule/MonthlyScheduleTests.java | 36 +- .../schedule/ScheduleRegistryTests.java | 35 +- .../trigger/schedule/ScheduleTestCase.java | 79 +- .../schedule/ScheduleTriggerEventTests.java | 3 +- .../trigger/schedule/WeeklyScheduleTests.java | 63 +- .../trigger/schedule/YearlyScheduleTests.java | 46 +- .../engine/TickerScheduleEngineTests.java | 65 +- .../schedule/tool/CronEvalToolTests.java | 2 +- .../xpack/watcher/watch/WatchStatusTests.java | 2 +- .../xpack/watcher/watch/WatchTests.java | 255 +- .../BinaryDvConfirmedAutomatonQuery.java | 13 +- .../wildcard/mapper/WildcardFieldMapper.java | 157 +- .../mapper/WildcardFieldAggregationTests.java | 88 +- .../mapper/WildcardFieldMapperTests.java | 580 +- .../mapper/WildcardFieldTypeTests.java | 10 +- ...CoreWithSecurityClientYamlTestSuiteIT.java | 7 +- .../scheduler/EvilSchedulerEngineTests.java | 18 +- .../authc/kerberos/KerberosTestCase.java | 19 +- .../KerberosTicketValidatorTests.java | 79 +- .../authc/kerberos/SimpleKdcLdapServer.java | 18 +- .../kerberos/SimpleKdcLdapServerTests.java | 22 +- .../security/authc/kerberos/SpnegoClient.java | 86 +- .../restart/CoreFullClusterRestartIT.java | 4 +- .../xpack/restart/FullClusterRestartIT.java | 200 +- ...nfigIndexMappingsFullClusterRestartIT.java | 37 +- .../MlHiddenIndicesFullClusterRestartIT.java | 94 +- .../MlMigrationFullClusterRestartIT.java | 72 +- .../xpack/restart/QueryBuilderBWCIT.java | 4 +- .../kerberos/KerberosAuthenticationIT.java | 90 +- ...SpnegoHttpClientConfigCallbackHandler.java | 80 +- .../elasticsearch/mixed/DataTierMixedIT.java | 8 +- ...sterSearchWithSecurityYamlTestSuiteIT.java | 8 +- .../GlobalCheckpointSyncActionIT.java | 2 +- .../elasticsearch/multi_node/RollupIT.java | 114 +- .../authc/oidc/OpenIdConnectAuthIT.java | 271 +- .../org/elasticsearch/test/OpenLdapTests.java | 127 +- ...OpenLdapUserSearchSessionFactoryTests.java | 84 +- .../authc/ldap/SearchGroupsResolverTests.java | 136 +- ...gsWithPasswordProtectedKeystoreRestIT.java | 37 +- ...ndexWithSecurityClientYamlTestSuiteIT.java | 12 +- .../xpack/security/ReindexWithSecurityIT.java | 68 +- .../AbstractMultiClusterUpgradeTestCase.java | 14 +- .../upgrades/CcrRollingUpgradeIT.java | 50 +- .../upgrades/AbstractUpgradeTestCase.java | 21 +- .../upgrades/DataStreamsUpgradeIT.java | 58 +- .../elasticsearch/upgrades/IndexingIT.java | 72 +- .../upgrades/MlJobSnapshotUpgradeIT.java | 121 +- .../upgrades/MlMappingsUpgradeIT.java | 32 +- .../upgrades/RollupDateHistoUpgradeIT.java | 87 +- .../SearchableSnapshotsRollingUpgradeIT.java | 182 +- .../TokenBackwardsCompatibilityIT.java | 43 +- .../upgrades/TransformSurvivesUpgradeIT.java | 142 +- .../UpgradeClusterClientYamlTestSuiteIT.java | 20 +- .../mapped/CoreWithMappedRuntimeFieldsIT.java | 1 + .../CoreTestsWithSearchRuntimeFieldsIT.java | 1 + .../authc/saml/SamlAuthenticationIT.java | 79 +- .../example/realm/CustomRealmIT.java | 2 +- .../realm/CustomRoleMappingRealmIT.java | 18 +- .../example/role/CustomRolesProviderIT.java | 14 +- .../example/ExampleSecurityExtension.java | 6 +- .../example/SpiExtensionPlugin.java | 3 +- .../CustomAuthenticationFailureHandler.java | 11 +- .../example/realm/CustomRealm.java | 16 +- .../example/realm/CustomRoleMappingRealm.java | 16 +- .../role/CustomInMemoryRolesProvider.java | 19 +- .../example/realm/CustomRealmTests.java | 33 +- .../realm/CustomRoleMappingRealmTests.java | 3 +- .../esnative/tool/ResetPasswordToolIT.java | 19 +- .../esnative/tool/SetupPasswordToolIT.java | 4 +- .../esnative/tool/ResetPasswordToolTests.java | 7 +- .../authc/file/tool/UsersToolTests.java | 247 +- .../authc/service/FileTokensToolTests.java | 111 +- .../crypto/tool/SystemKeyToolTests.java | 7 +- ...onfigGenerateElasticPasswordHashTests.java | 6 +- .../tool/BaseRunAsSuperuserCommandTests.java | 85 +- .../tool/CreateEnrollmentTokenToolTests.java | 117 +- .../support/FileAttributesCheckerTests.java | 11 +- .../security/support/SecurityFilesTests.java | 22 +- .../SmokeTestMonitoringWithSecurityIT.java | 14 +- ...keTestPluginsSslClientYamlTestSuiteIT.java | 26 +- ...SmokeTestPluginsClientYamlTestSuiteIT.java | 5 +- ...rityWithMustacheClientYamlTestSuiteIT.java | 10 +- .../test/SecuritySettingsSourceField.java | 4 +- .../rest/IndexMappingTemplateAsserter.java | 134 +- .../test/rest/XPackRestTestConstants.java | 24 +- .../xpack/test/rest/XPackRestTestHelper.java | 26 +- .../ADLdapUserSearchSessionFactoryTests.java | 55 +- .../ldap/AbstractActiveDirectoryTestCase.java | 31 +- .../ldap/AbstractAdLdapRealmTestCase.java | 281 +- .../ActiveDirectoryGroupsResolverTests.java | 98 +- .../authc/ldap/ActiveDirectoryRunAsIT.java | 14 +- .../ActiveDirectorySessionFactoryTests.java | 299 +- .../authc/ldap/MultiGroupMappingIT.java | 32 +- .../authc/ldap/MultipleAdRealmIT.java | 21 +- .../UserAttributeGroupsResolverTests.java | 61 +- .../smoketest/WatcherJiraYamlTestSuiteIT.java | 15 +- .../WatcherPagerDutyYamlTestSuiteIT.java | 15 +- .../WatcherSlackYamlTestSuiteIT.java | 15 +- 6805 files changed, 262048 insertions(+), 173170 deletions(-) diff --git a/client/benchmark/src/main/java/org/elasticsearch/client/benchmark/AbstractBenchmark.java b/client/benchmark/src/main/java/org/elasticsearch/client/benchmark/AbstractBenchmark.java index 959bbcaef8e71..ba675c6323a8b 100644 --- a/client/benchmark/src/main/java/org/elasticsearch/client/benchmark/AbstractBenchmark.java +++ b/client/benchmark/src/main/java/org/elasticsearch/client/benchmark/AbstractBenchmark.java @@ -53,8 +53,7 @@ public final void run(String[] args) throws Exception { @SuppressForbidden(reason = "system out is ok for a command line tool") private void runBulkIndexBenchmark(String[] args) throws Exception { if (args.length != 7) { - System.err.println( - "usage: 'bulk' benchmarkTargetHostIp indexFilePath indexName typeName numberOfDocuments bulkSize"); + System.err.println("usage: 'bulk' benchmarkTargetHostIp indexFilePath indexName typeName numberOfDocuments bulkSize"); System.exit(1); } String benchmarkTargetHost = args[1]; @@ -71,9 +70,11 @@ private void runBulkIndexBenchmark(String[] args) throws Exception { T client = client(benchmarkTargetHost); - BenchmarkRunner benchmark = new BenchmarkRunner(warmupIterations, iterations, - new BulkBenchmarkTask( - bulkRequestExecutor(client, indexName, typeName), indexFilePath, warmupIterations, iterations, bulkSize)); + BenchmarkRunner benchmark = new BenchmarkRunner( + warmupIterations, + iterations, + new BulkBenchmarkTask(bulkRequestExecutor(client, indexName, typeName), indexFilePath, warmupIterations, iterations, bulkSize) + ); try { runTrials(() -> { @@ -89,8 +90,7 @@ private void runBulkIndexBenchmark(String[] args) throws Exception { @SuppressForbidden(reason = "system out is ok for a command line tool") private void runSearchBenchmark(String[] args) throws Exception { if (args.length != 5) { - System.err.println( - "usage: 'search' benchmarkTargetHostIp indexName searchRequestBody throughputRates"); + System.err.println("usage: 'search' benchmarkTargetHostIp indexName searchRequestBody throughputRates"); System.exit(1); } String benchmarkTargetHost = args[1]; @@ -103,12 +103,19 @@ private void runSearchBenchmark(String[] args) throws Exception { try { runTrials(() -> { for (int throughput : throughputRates) { - //GC between trials to reduce the likelihood of a GC occurring in the middle of a trial. + // GC between trials to reduce the likelihood of a GC occurring in the middle of a trial. runGc(); - BenchmarkRunner benchmark = new BenchmarkRunner(SEARCH_BENCHMARK_ITERATIONS, SEARCH_BENCHMARK_ITERATIONS, + BenchmarkRunner benchmark = new BenchmarkRunner( + SEARCH_BENCHMARK_ITERATIONS, + SEARCH_BENCHMARK_ITERATIONS, new SearchBenchmarkTask( - searchRequestExecutor(client, indexName), searchBody, SEARCH_BENCHMARK_ITERATIONS, - SEARCH_BENCHMARK_ITERATIONS, throughput)); + searchRequestExecutor(client, indexName), + searchBody, + SEARCH_BENCHMARK_ITERATIONS, + SEARCH_BENCHMARK_ITERATIONS, + throughput + ) + ); System.out.printf("Target throughput = %d ops / s%n", throughput); benchmark.run(); } diff --git a/client/benchmark/src/main/java/org/elasticsearch/client/benchmark/BenchmarkRunner.java b/client/benchmark/src/main/java/org/elasticsearch/client/benchmark/BenchmarkRunner.java index 8c3eca43882c6..19aa5ed98a3d9 100644 --- a/client/benchmark/src/main/java/org/elasticsearch/client/benchmark/BenchmarkRunner.java +++ b/client/benchmark/src/main/java/org/elasticsearch/client/benchmark/BenchmarkRunner.java @@ -31,8 +31,12 @@ public BenchmarkRunner(int warmupIterations, int iterations, BenchmarkTask task) @SuppressForbidden(reason = "system out is ok for a command line tool") public void run() { SampleRecorder recorder = new SampleRecorder(iterations); - System.out.printf("Running %s with %d warmup iterations and %d iterations.%n", - task.getClass().getSimpleName(), warmupIterations, iterations); + System.out.printf( + "Running %s with %d warmup iterations and %d iterations.%n", + task.getClass().getSimpleName(), + warmupIterations, + iterations + ); try { task.setUp(recorder); @@ -54,14 +58,26 @@ public void run() { for (Metrics metrics : summaryMetrics) { String throughput = String.format(Locale.ROOT, "Throughput [ops/s]: %f", metrics.throughput); - String serviceTimes = String.format(Locale.ROOT, + String serviceTimes = String.format( + Locale.ROOT, "Service time [ms]: p50 = %f, p90 = %f, p95 = %f, p99 = %f, p99.9 = %f, p99.99 = %f", - metrics.serviceTimeP50, metrics.serviceTimeP90, metrics.serviceTimeP95, - metrics.serviceTimeP99, metrics.serviceTimeP999, metrics.serviceTimeP9999); - String latencies = String.format(Locale.ROOT, + metrics.serviceTimeP50, + metrics.serviceTimeP90, + metrics.serviceTimeP95, + metrics.serviceTimeP99, + metrics.serviceTimeP999, + metrics.serviceTimeP9999 + ); + String latencies = String.format( + Locale.ROOT, "Latency [ms]: p50 = %f, p90 = %f, p95 = %f, p99 = %f, p99.9 = %f, p99.99 = %f", - metrics.latencyP50, metrics.latencyP90, metrics.latencyP95, - metrics.latencyP99, metrics.latencyP999, metrics.latencyP9999); + metrics.latencyP50, + metrics.latencyP90, + metrics.latencyP95, + metrics.latencyP99, + metrics.latencyP999, + metrics.latencyP9999 + ); int lineLength = Math.max(serviceTimes.length(), latencies.length()); diff --git a/client/benchmark/src/main/java/org/elasticsearch/client/benchmark/metrics/Metrics.java b/client/benchmark/src/main/java/org/elasticsearch/client/benchmark/metrics/Metrics.java index 951e316a68722..cff540d665efa 100644 --- a/client/benchmark/src/main/java/org/elasticsearch/client/benchmark/metrics/Metrics.java +++ b/client/benchmark/src/main/java/org/elasticsearch/client/benchmark/metrics/Metrics.java @@ -25,10 +25,24 @@ public final class Metrics { public final double latencyP999; public final double latencyP9999; - public Metrics(String operation, long successCount, long errorCount, double throughput, - double serviceTimeP50, double serviceTimeP90, double serviceTimeP95, double serviceTimeP99, - double serviceTimeP999, double serviceTimeP9999, double latencyP50, double latencyP90, - double latencyP95, double latencyP99, double latencyP999, double latencyP9999) { + public Metrics( + String operation, + long successCount, + long errorCount, + double throughput, + double serviceTimeP50, + double serviceTimeP90, + double serviceTimeP95, + double serviceTimeP99, + double serviceTimeP999, + double serviceTimeP9999, + double latencyP50, + double latencyP90, + double latencyP95, + double latencyP99, + double latencyP999, + double latencyP9999 + ) { this.operation = operation; this.successCount = successCount; this.errorCount = errorCount; diff --git a/client/benchmark/src/main/java/org/elasticsearch/client/benchmark/metrics/MetricsCalculator.java b/client/benchmark/src/main/java/org/elasticsearch/client/benchmark/metrics/MetricsCalculator.java index e7f9a0a266517..2f9a6e65655de 100644 --- a/client/benchmark/src/main/java/org/elasticsearch/client/benchmark/metrics/MetricsCalculator.java +++ b/client/benchmark/src/main/java/org/elasticsearch/client/benchmark/metrics/MetricsCalculator.java @@ -51,24 +51,28 @@ private static List calculateMetricsPerOperation(Map r.isSuccess()).count(), - samples.stream().filter((r) -> r.isSuccess() == false).count(), - // throughput calculation is based on the total (Wall clock) time it took to generate all samples - calculateThroughput(samples.size(), latestEnd - firstStart), - // convert ns -> ms without losing precision - StatUtils.percentile(serviceTimes, 50.0d) / TimeUnit.MILLISECONDS.toNanos(1L), - StatUtils.percentile(serviceTimes, 90.0d) / TimeUnit.MILLISECONDS.toNanos(1L), - StatUtils.percentile(serviceTimes, 95.0d) / TimeUnit.MILLISECONDS.toNanos(1L), - StatUtils.percentile(serviceTimes, 99.0d) / TimeUnit.MILLISECONDS.toNanos(1L), - StatUtils.percentile(serviceTimes, 99.9d) / TimeUnit.MILLISECONDS.toNanos(1L), - StatUtils.percentile(serviceTimes, 99.99d) / TimeUnit.MILLISECONDS.toNanos(1L), - StatUtils.percentile(latencies, 50.0d) / TimeUnit.MILLISECONDS.toNanos(1L), - StatUtils.percentile(latencies, 90.0d) / TimeUnit.MILLISECONDS.toNanos(1L), - StatUtils.percentile(latencies, 95.0d) / TimeUnit.MILLISECONDS.toNanos(1L), - StatUtils.percentile(latencies, 99.0d) / TimeUnit.MILLISECONDS.toNanos(1L), - StatUtils.percentile(latencies, 99.9d) / TimeUnit.MILLISECONDS.toNanos(1L), - StatUtils.percentile(latencies, 99.99d) / TimeUnit.MILLISECONDS.toNanos(1L))); + metrics.add( + new Metrics( + operationAndMetrics.getKey(), + samples.stream().filter((r) -> r.isSuccess()).count(), + samples.stream().filter((r) -> r.isSuccess() == false).count(), + // throughput calculation is based on the total (Wall clock) time it took to generate all samples + calculateThroughput(samples.size(), latestEnd - firstStart), + // convert ns -> ms without losing precision + StatUtils.percentile(serviceTimes, 50.0d) / TimeUnit.MILLISECONDS.toNanos(1L), + StatUtils.percentile(serviceTimes, 90.0d) / TimeUnit.MILLISECONDS.toNanos(1L), + StatUtils.percentile(serviceTimes, 95.0d) / TimeUnit.MILLISECONDS.toNanos(1L), + StatUtils.percentile(serviceTimes, 99.0d) / TimeUnit.MILLISECONDS.toNanos(1L), + StatUtils.percentile(serviceTimes, 99.9d) / TimeUnit.MILLISECONDS.toNanos(1L), + StatUtils.percentile(serviceTimes, 99.99d) / TimeUnit.MILLISECONDS.toNanos(1L), + StatUtils.percentile(latencies, 50.0d) / TimeUnit.MILLISECONDS.toNanos(1L), + StatUtils.percentile(latencies, 90.0d) / TimeUnit.MILLISECONDS.toNanos(1L), + StatUtils.percentile(latencies, 95.0d) / TimeUnit.MILLISECONDS.toNanos(1L), + StatUtils.percentile(latencies, 99.0d) / TimeUnit.MILLISECONDS.toNanos(1L), + StatUtils.percentile(latencies, 99.9d) / TimeUnit.MILLISECONDS.toNanos(1L), + StatUtils.percentile(latencies, 99.99d) / TimeUnit.MILLISECONDS.toNanos(1L) + ) + ); } return metrics; } diff --git a/client/benchmark/src/main/java/org/elasticsearch/client/benchmark/ops/bulk/BulkBenchmarkTask.java b/client/benchmark/src/main/java/org/elasticsearch/client/benchmark/ops/bulk/BulkBenchmarkTask.java index 3351a4b99c474..69edd9c8f86ff 100644 --- a/client/benchmark/src/main/java/org/elasticsearch/client/benchmark/ops/bulk/BulkBenchmarkTask.java +++ b/client/benchmark/src/main/java/org/elasticsearch/client/benchmark/ops/bulk/BulkBenchmarkTask.java @@ -7,14 +7,14 @@ */ package org.elasticsearch.client.benchmark.ops.bulk; -import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.client.benchmark.BenchmarkTask; import org.elasticsearch.client.benchmark.metrics.Sample; import org.elasticsearch.client.benchmark.metrics.SampleRecorder; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.core.PathUtils; +import org.elasticsearch.core.SuppressForbidden; import java.io.BufferedReader; import java.io.IOException; @@ -38,8 +38,13 @@ public class BulkBenchmarkTask implements BenchmarkTask { private LoadGenerator generator; private ExecutorService executorService; - public BulkBenchmarkTask(BulkRequestExecutor requestExecutor, String indexFilePath, int warmupIterations, int measurementIterations, - int bulkSize) { + public BulkBenchmarkTask( + BulkRequestExecutor requestExecutor, + String indexFilePath, + int warmupIterations, + int measurementIterations, + int bulkSize + ) { this.requestExecutor = requestExecutor; this.indexFilePath = indexFilePath; this.warmupIterations = warmupIterations; @@ -62,11 +67,11 @@ public void setUp(SampleRecorder sampleRecorder) { @Override @SuppressForbidden(reason = "system out is ok for a command line tool") - public void run() throws Exception { + public void run() throws Exception { generator.execute(); // when the generator is done, there are no more data -> shutdown client executorService.shutdown(); - //We need to wait until the queue is drained + // We need to wait until the queue is drained final boolean finishedNormally = executorService.awaitTermination(20, TimeUnit.MINUTES); if (finishedNormally == false) { System.err.println("Background tasks are still running after timeout on enclosing pool. Forcing pool shutdown."); @@ -76,7 +81,7 @@ public void run() throws Exception { @Override public void tearDown() { - //no op + // no op } private static final class LoadGenerator { @@ -122,7 +127,6 @@ private void sendBulk(List bulkData) throws InterruptedException { } } - private static final class BulkIndexer implements Runnable { private static final Logger logger = LogManager.getLogger(BulkIndexer.class); @@ -132,8 +136,13 @@ private static final class BulkIndexer implements Runnable { private final BulkRequestExecutor bulkRequestExecutor; private final SampleRecorder sampleRecorder; - BulkIndexer(BlockingQueue> bulkData, int warmupIterations, int measurementIterations, - SampleRecorder sampleRecorder, BulkRequestExecutor bulkRequestExecutor) { + BulkIndexer( + BlockingQueue> bulkData, + int warmupIterations, + int measurementIterations, + SampleRecorder sampleRecorder, + BulkRequestExecutor bulkRequestExecutor + ) { this.bulkData = bulkData; this.warmupIterations = warmupIterations; this.measurementIterations = measurementIterations; @@ -152,7 +161,7 @@ public void run() { Thread.currentThread().interrupt(); return; } - //measure only service time, latency is not that interesting for a throughput benchmark + // measure only service time, latency is not that interesting for a throughput benchmark long start = System.nanoTime(); try { success = bulkRequestExecutor.bulkIndex(currentBulk); diff --git a/client/benchmark/src/main/java/org/elasticsearch/client/benchmark/ops/search/SearchBenchmarkTask.java b/client/benchmark/src/main/java/org/elasticsearch/client/benchmark/ops/search/SearchBenchmarkTask.java index 7562a4e9e4c26..a00d78bcf77f4 100644 --- a/client/benchmark/src/main/java/org/elasticsearch/client/benchmark/ops/search/SearchBenchmarkTask.java +++ b/client/benchmark/src/main/java/org/elasticsearch/client/benchmark/ops/search/SearchBenchmarkTask.java @@ -22,8 +22,13 @@ public class SearchBenchmarkTask implements BenchmarkTask { private SampleRecorder sampleRecorder; - public SearchBenchmarkTask(SearchRequestExecutor searchRequestExecutor, String body, int warmupIterations, - int measurementIterations, int targetThroughput) { + public SearchBenchmarkTask( + SearchRequestExecutor searchRequestExecutor, + String body, + int warmupIterations, + int measurementIterations, + int targetThroughput + ) { this.searchRequestExecutor = searchRequestExecutor; this.searchRequestBody = body; this.warmupIterations = warmupIterations; diff --git a/client/benchmark/src/main/java/org/elasticsearch/client/benchmark/rest/RestClientBenchmark.java b/client/benchmark/src/main/java/org/elasticsearch/client/benchmark/rest/RestClientBenchmark.java index 18c7d4cd2dfb2..0793b8c721c7b 100644 --- a/client/benchmark/src/main/java/org/elasticsearch/client/benchmark/rest/RestClientBenchmark.java +++ b/client/benchmark/src/main/java/org/elasticsearch/client/benchmark/rest/RestClientBenchmark.java @@ -32,10 +32,10 @@ public static void main(String[] args) throws Exception { @Override protected RestClient client(String benchmarkTargetHost) { - return RestClient - .builder(new HttpHost(benchmarkTargetHost, 9200)) - .setHttpClientConfigCallback(b -> b.setDefaultHeaders( - Collections.singleton(new BasicHeader(HttpHeaders.ACCEPT_ENCODING, "gzip")))) + return RestClient.builder(new HttpHost(benchmarkTargetHost, 9200)) + .setHttpClientConfigCallback( + b -> b.setDefaultHeaders(Collections.singleton(new BasicHeader(HttpHeaders.ACCEPT_ENCODING, "gzip"))) + ) .setRequestConfigCallback(b -> b.setContentCompressionEnabled(true)) .build(); } diff --git a/client/client-benchmark-noop-api-plugin/src/main/java/org/elasticsearch/plugin/noop/NoopPlugin.java b/client/client-benchmark-noop-api-plugin/src/main/java/org/elasticsearch/plugin/noop/NoopPlugin.java index 4e1738760b5eb..c5699514c12aa 100644 --- a/client/client-benchmark-noop-api-plugin/src/main/java/org/elasticsearch/plugin/noop/NoopPlugin.java +++ b/client/client-benchmark-noop-api-plugin/src/main/java/org/elasticsearch/plugin/noop/NoopPlugin.java @@ -7,9 +7,6 @@ */ package org.elasticsearch.plugin.noop; -import org.elasticsearch.plugin.noop.action.bulk.NoopBulkAction; -import org.elasticsearch.plugin.noop.action.bulk.RestNoopBulkAction; -import org.elasticsearch.plugin.noop.action.bulk.TransportNoopBulkAction; import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionResponse; import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; @@ -18,6 +15,9 @@ import org.elasticsearch.common.settings.IndexScopedSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsFilter; +import org.elasticsearch.plugin.noop.action.bulk.NoopBulkAction; +import org.elasticsearch.plugin.noop.action.bulk.RestNoopBulkAction; +import org.elasticsearch.plugin.noop.action.bulk.TransportNoopBulkAction; import org.elasticsearch.plugin.noop.action.search.NoopSearchAction; import org.elasticsearch.plugin.noop.action.search.RestNoopSearchAction; import org.elasticsearch.plugin.noop.action.search.TransportNoopSearchAction; @@ -40,11 +40,15 @@ public class NoopPlugin extends Plugin implements ActionPlugin { } @Override - public List getRestHandlers(Settings settings, RestController restController, ClusterSettings clusterSettings, - IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter, IndexNameExpressionResolver indexNameExpressionResolver, - Supplier nodesInCluster) { - return Arrays.asList( - new RestNoopBulkAction(), - new RestNoopSearchAction()); + public List getRestHandlers( + Settings settings, + RestController restController, + ClusterSettings clusterSettings, + IndexScopedSettings indexScopedSettings, + SettingsFilter settingsFilter, + IndexNameExpressionResolver indexNameExpressionResolver, + Supplier nodesInCluster + ) { + return Arrays.asList(new RestNoopBulkAction(), new RestNoopSearchAction()); } } diff --git a/client/client-benchmark-noop-api-plugin/src/main/java/org/elasticsearch/plugin/noop/action/bulk/RestNoopBulkAction.java b/client/client-benchmark-noop-api-plugin/src/main/java/org/elasticsearch/plugin/noop/action/bulk/RestNoopBulkAction.java index 6ab689a267bdf..a9ffd60f0a5df 100644 --- a/client/client-benchmark-noop-api-plugin/src/main/java/org/elasticsearch/plugin/noop/action/bulk/RestNoopBulkAction.java +++ b/client/client-benchmark-noop-api-plugin/src/main/java/org/elasticsearch/plugin/noop/action/bulk/RestNoopBulkAction.java @@ -16,7 +16,6 @@ import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.client.Requests; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.BytesRestResponse; @@ -24,6 +23,7 @@ import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.List; @@ -40,7 +40,8 @@ public List routes() { new Route(POST, "/_noop_bulk"), new Route(PUT, "/_noop_bulk"), new Route(POST, "/{index}/_noop_bulk"), - new Route(PUT, "/{index}/_noop_bulk")); + new Route(PUT, "/{index}/_noop_bulk") + ); } @Override @@ -62,9 +63,17 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC } bulkRequest.timeout(request.paramAsTime("timeout", BulkShardRequest.DEFAULT_TIMEOUT)); bulkRequest.setRefreshPolicy(request.param("refresh")); - bulkRequest.add(request.requiredContent(), defaultIndex, defaultRouting, - null, defaultPipeline, defaultRequireAlias, true, request.getXContentType(), - request.getRestApiVersion()); + bulkRequest.add( + request.requiredContent(), + defaultIndex, + defaultRouting, + null, + defaultPipeline, + defaultRequireAlias, + true, + request.getXContentType(), + request.getRestApiVersion() + ); // short circuit the call to the transport layer return channel -> { @@ -74,12 +83,14 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC } private static class BulkRestBuilderListener extends RestBuilderListener { - private final BulkItemResponse ITEM_RESPONSE = BulkItemResponse.success(1, DocWriteRequest.OpType.UPDATE, - new UpdateResponse(new ShardId("mock", "", 1), "1", 0L, 1L, 1L, DocWriteResponse.Result.CREATED)); + private final BulkItemResponse ITEM_RESPONSE = BulkItemResponse.success( + 1, + DocWriteRequest.OpType.UPDATE, + new UpdateResponse(new ShardId("mock", "", 1), "1", 0L, 1L, 1L, DocWriteResponse.Result.CREATED) + ); private final RestRequest request; - BulkRestBuilderListener(RestChannel channel, RestRequest request) { super(channel); this.request = request; diff --git a/client/client-benchmark-noop-api-plugin/src/main/java/org/elasticsearch/plugin/noop/action/bulk/TransportNoopBulkAction.java b/client/client-benchmark-noop-api-plugin/src/main/java/org/elasticsearch/plugin/noop/action/bulk/TransportNoopBulkAction.java index 0535e234e5ec2..cf91d07ed5687 100644 --- a/client/client-benchmark-noop-api-plugin/src/main/java/org/elasticsearch/plugin/noop/action/bulk/TransportNoopBulkAction.java +++ b/client/client-benchmark-noop-api-plugin/src/main/java/org/elasticsearch/plugin/noop/action/bulk/TransportNoopBulkAction.java @@ -22,8 +22,11 @@ import org.elasticsearch.transport.TransportService; public class TransportNoopBulkAction extends HandledTransportAction { - private static final BulkItemResponse ITEM_RESPONSE = BulkItemResponse.success(1, DocWriteRequest.OpType.UPDATE, - new UpdateResponse(new ShardId("mock", "", 1), "1", 0L, 1L, 1L, DocWriteResponse.Result.CREATED)); + private static final BulkItemResponse ITEM_RESPONSE = BulkItemResponse.success( + 1, + DocWriteRequest.OpType.UPDATE, + new UpdateResponse(new ShardId("mock", "", 1), "1", 0L, 1L, 1L, DocWriteResponse.Result.CREATED) + ); @Inject public TransportNoopBulkAction(TransportService transportService, ActionFilters actionFilters) { diff --git a/client/client-benchmark-noop-api-plugin/src/main/java/org/elasticsearch/plugin/noop/action/search/RestNoopSearchAction.java b/client/client-benchmark-noop-api-plugin/src/main/java/org/elasticsearch/plugin/noop/action/search/RestNoopSearchAction.java index c93034fb4b46f..7da435806a3bc 100644 --- a/client/client-benchmark-noop-api-plugin/src/main/java/org/elasticsearch/plugin/noop/action/search/RestNoopSearchAction.java +++ b/client/client-benchmark-noop-api-plugin/src/main/java/org/elasticsearch/plugin/noop/action/search/RestNoopSearchAction.java @@ -26,7 +26,8 @@ public List routes() { new Route(GET, "/_noop_search"), new Route(POST, "/_noop_search"), new Route(GET, "/{index}/_noop_search"), - new Route(POST, "/{index}/_noop_search")); + new Route(POST, "/{index}/_noop_search") + ); } @Override diff --git a/client/client-benchmark-noop-api-plugin/src/main/java/org/elasticsearch/plugin/noop/action/search/TransportNoopSearchAction.java b/client/client-benchmark-noop-api-plugin/src/main/java/org/elasticsearch/plugin/noop/action/search/TransportNoopSearchAction.java index c6579e70f645e..0451be0e34afb 100644 --- a/client/client-benchmark-noop-api-plugin/src/main/java/org/elasticsearch/plugin/noop/action/search/TransportNoopSearchAction.java +++ b/client/client-benchmark-noop-api-plugin/src/main/java/org/elasticsearch/plugin/noop/action/search/TransportNoopSearchAction.java @@ -35,12 +35,25 @@ public TransportNoopSearchAction(TransportService transportService, ActionFilter @Override protected void doExecute(Task task, SearchRequest request, ActionListener listener) { - listener.onResponse(new SearchResponse(new InternalSearchResponse( - new SearchHits( - new SearchHit[0], new TotalHits(0L, TotalHits.Relation.EQUAL_TO), 0.0f), - InternalAggregations.EMPTY, - new Suggest(Collections.emptyList()), - new SearchProfileResults(Collections.emptyMap()), false, false, 1), - "", 1, 1, 0, 0, ShardSearchFailure.EMPTY_ARRAY, SearchResponse.Clusters.EMPTY)); + listener.onResponse( + new SearchResponse( + new InternalSearchResponse( + new SearchHits(new SearchHit[0], new TotalHits(0L, TotalHits.Relation.EQUAL_TO), 0.0f), + InternalAggregations.EMPTY, + new Suggest(Collections.emptyList()), + new SearchProfileResults(Collections.emptyMap()), + false, + false, + 1 + ), + "", + 1, + 1, + 0, + 0, + ShardSearchFailure.EMPTY_ARRAY, + SearchResponse.Clusters.EMPTY + ) + ); } } diff --git a/client/rest-high-level/qa/ssl-enabled/src/javaRestTest/java/org/elasticsearch/client/EnrollmentIT.java b/client/rest-high-level/qa/ssl-enabled/src/javaRestTest/java/org/elasticsearch/client/EnrollmentIT.java index c8951b7104357..2ef3bac0c89c3 100644 --- a/client/rest-high-level/qa/ssl-enabled/src/javaRestTest/java/org/elasticsearch/client/EnrollmentIT.java +++ b/client/rest-high-level/qa/ssl-enabled/src/javaRestTest/java/org/elasticsearch/client/EnrollmentIT.java @@ -27,7 +27,7 @@ import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.startsWith; -public class EnrollmentIT extends ESRestHighLevelClientTestCase { +public class EnrollmentIT extends ESRestHighLevelClientTestCase { private static Path httpTrustStore; @BeforeClass @@ -71,8 +71,11 @@ protected Settings restAdminSettings() { } public void testEnrollNode() throws Exception { - final NodeEnrollmentResponse nodeEnrollmentResponse = - execute(highLevelClient().security()::enrollNode, highLevelClient().security()::enrollNodeAsync, RequestOptions.DEFAULT); + final NodeEnrollmentResponse nodeEnrollmentResponse = execute( + highLevelClient().security()::enrollNode, + highLevelClient().security()::enrollNodeAsync, + RequestOptions.DEFAULT + ); assertThat(nodeEnrollmentResponse, notNullValue()); assertThat(nodeEnrollmentResponse.getHttpCaKey(), endsWith("K2S3vidA=")); assertThat(nodeEnrollmentResponse.getHttpCaCert(), endsWith("LfkRjirc=")); @@ -84,11 +87,16 @@ public void testEnrollNode() throws Exception { } public void testEnrollKibana() throws Exception { - KibanaEnrollmentResponse kibanaResponse = - execute(highLevelClient().security()::enrollKibana, highLevelClient().security()::enrollKibanaAsync, RequestOptions.DEFAULT); + KibanaEnrollmentResponse kibanaResponse = execute( + highLevelClient().security()::enrollKibana, + highLevelClient().security()::enrollKibanaAsync, + RequestOptions.DEFAULT + ); assertThat(kibanaResponse, notNullValue()); - assertThat(kibanaResponse.getHttpCa() - , endsWith("brcNC5xq6YE7C4/06nH7F6le4kE4Uo6c9fpkl4ehOxQxndNLn462tFF+8VBA8IftJ1PPWzqGxLsCTzM6p6w8sa+XhgNYglLfkRjirc=")); + assertThat( + kibanaResponse.getHttpCa(), + endsWith("brcNC5xq6YE7C4/06nH7F6le4kE4Uo6c9fpkl4ehOxQxndNLn462tFF+8VBA8IftJ1PPWzqGxLsCTzM6p6w8sa+XhgNYglLfkRjirc=") + ); assertNotNull(kibanaResponse.getTokenValue()); assertNotNull(kibanaResponse.getTokenName(), startsWith("enroll-process-token-")); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/AsyncSearchClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/AsyncSearchClient.java index 30dbfd022e205..a1c63785be5d9 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/AsyncSearchClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/AsyncSearchClient.java @@ -42,8 +42,13 @@ public class AsyncSearchClient { * @throws IOException in case there is a problem sending the request or parsing back the response */ public AsyncSearchResponse submit(SubmitAsyncSearchRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, AsyncSearchRequestConverters::submitAsyncSearch, options, - AsyncSearchResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + AsyncSearchRequestConverters::submitAsyncSearch, + options, + AsyncSearchResponse::fromXContent, + emptySet() + ); } /** @@ -56,10 +61,15 @@ public AsyncSearchResponse submit(SubmitAsyncSearchRequest request, RequestOptio * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable submitAsync(SubmitAsyncSearchRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, AsyncSearchRequestConverters::submitAsyncSearch, options, - AsyncSearchResponse::fromXContent, listener, emptySet()); + public Cancellable submitAsync(SubmitAsyncSearchRequest request, RequestOptions options, ActionListener listener) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + AsyncSearchRequestConverters::submitAsyncSearch, + options, + AsyncSearchResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -68,8 +78,13 @@ public Cancellable submitAsync(SubmitAsyncSearchRequest request, RequestOptions * */ public AsyncSearchResponse get(GetAsyncSearchRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, AsyncSearchRequestConverters::getAsyncSearch, options, - AsyncSearchResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + AsyncSearchRequestConverters::getAsyncSearch, + options, + AsyncSearchResponse::fromXContent, + emptySet() + ); } /** @@ -80,10 +95,15 @@ public AsyncSearchResponse get(GetAsyncSearchRequest request, RequestOptions opt * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getAsync(GetAsyncSearchRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, AsyncSearchRequestConverters::getAsyncSearch, options, - AsyncSearchResponse::fromXContent, listener, emptySet()); + public Cancellable getAsync(GetAsyncSearchRequest request, RequestOptions options, ActionListener listener) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + AsyncSearchRequestConverters::getAsyncSearch, + options, + AsyncSearchResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -95,8 +115,13 @@ public Cancellable getAsync(GetAsyncSearchRequest request, RequestOptions option * @throws IOException in case there is a problem sending the request or parsing back the response */ public AcknowledgedResponse delete(DeleteAsyncSearchRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, AsyncSearchRequestConverters::deleteAsyncSearch, options, - AcknowledgedResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + AsyncSearchRequestConverters::deleteAsyncSearch, + options, + AcknowledgedResponse::fromXContent, + emptySet() + ); } /** @@ -107,10 +132,19 @@ public AcknowledgedResponse delete(DeleteAsyncSearchRequest request, RequestOpti * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable deleteAsync(DeleteAsyncSearchRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, AsyncSearchRequestConverters::deleteAsyncSearch, options, - AcknowledgedResponse::fromXContent, listener, emptySet()); + public Cancellable deleteAsync( + DeleteAsyncSearchRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + AsyncSearchRequestConverters::deleteAsyncSearch, + options, + AcknowledgedResponse::fromXContent, + listener, + emptySet() + ); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/AsyncSearchRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/AsyncSearchRequestConverters.java index 7ba787dfdd72a..5430e6a36c84b 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/AsyncSearchRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/AsyncSearchRequestConverters.java @@ -26,9 +26,9 @@ final class AsyncSearchRequestConverters { static Request submitAsyncSearch(SubmitAsyncSearchRequest asyncSearchRequest) throws IOException { - String endpoint = new RequestConverters.EndpointBuilder().addCommaSeparatedPathParts( - asyncSearchRequest.getIndices()) - .addPathPartAsIs("_async_search").build(); + String endpoint = new RequestConverters.EndpointBuilder().addCommaSeparatedPathParts(asyncSearchRequest.getIndices()) + .addPathPartAsIs("_async_search") + .build(); Request request = new Request(HttpPost.METHOD_NAME, endpoint); Params params = new RequestConverters.Params(); // add all typical search params and search request source as body @@ -71,10 +71,9 @@ static void addSearchRequestParams(Params params, SubmitAsyncSearchRequest reque } static Request getAsyncSearch(GetAsyncSearchRequest asyncSearchRequest) { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_async_search") - .addPathPart(asyncSearchRequest.getId()) - .build(); + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_async_search") + .addPathPart(asyncSearchRequest.getId()) + .build(); Request request = new Request(HttpGet.METHOD_NAME, endpoint); Params params = new RequestConverters.Params(); params.putParam(RestSearchAction.TYPED_KEYS_PARAM, "true"); @@ -89,10 +88,9 @@ static Request getAsyncSearch(GetAsyncSearchRequest asyncSearchRequest) { } static Request deleteAsyncSearch(DeleteAsyncSearchRequest deleteAsyncSearchRequest) { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_async_search") - .addPathPart(deleteAsyncSearchRequest.getId()) - .build(); + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_async_search") + .addPathPart(deleteAsyncSearchRequest.getId()) + .build(); return new Request(HttpDelete.METHOD_NAME, endpoint); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/CcrClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/CcrClient.java index 70975ccc42e74..e7ef990cc2762 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/CcrClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/CcrClient.java @@ -87,9 +87,7 @@ public PutFollowResponse putFollow(PutFollowRequest request, RequestOptions opti * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable putFollowAsync(PutFollowRequest request, - RequestOptions options, - ActionListener listener) { + public Cancellable putFollowAsync(PutFollowRequest request, RequestOptions options, ActionListener listener) { return restHighLevelClient.performRequestAsyncAndParseEntity( request, CcrRequestConverters::putFollow, @@ -131,16 +129,15 @@ public AcknowledgedResponse pauseFollow(PauseFollowRequest request, RequestOptio * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable pauseFollowAsync(PauseFollowRequest request, - RequestOptions options, - ActionListener listener) { + public Cancellable pauseFollowAsync(PauseFollowRequest request, RequestOptions options, ActionListener listener) { return restHighLevelClient.performRequestAsyncAndParseEntity( request, CcrRequestConverters::pauseFollow, options, AcknowledgedResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -174,16 +171,19 @@ public AcknowledgedResponse resumeFollow(ResumeFollowRequest request, RequestOpt * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable resumeFollowAsync(ResumeFollowRequest request, - RequestOptions options, - ActionListener listener) { + public Cancellable resumeFollowAsync( + ResumeFollowRequest request, + RequestOptions options, + ActionListener listener + ) { return restHighLevelClient.performRequestAsyncAndParseEntity( request, CcrRequestConverters::resumeFollow, options, AcknowledgedResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -219,9 +219,7 @@ public AcknowledgedResponse unfollow(UnfollowRequest request, RequestOptions opt * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable unfollowAsync(UnfollowRequest request, - RequestOptions options, - ActionListener listener) { + public Cancellable unfollowAsync(UnfollowRequest request, RequestOptions options, ActionListener listener) { return restHighLevelClient.performRequestAsyncAndParseEntity( request, CcrRequestConverters::unfollow, @@ -245,11 +243,12 @@ public Cancellable unfollowAsync(UnfollowRequest request, */ public BroadcastResponse forgetFollower(final ForgetFollowerRequest request, final RequestOptions options) throws IOException { return restHighLevelClient.performRequestAndParseEntity( - request, - CcrRequestConverters::forgetFollower, - options, - BroadcastResponse::fromXContent, - Collections.emptySet()); + request, + CcrRequestConverters::forgetFollower, + options, + BroadcastResponse::fromXContent, + Collections.emptySet() + ); } /** @@ -262,16 +261,18 @@ public BroadcastResponse forgetFollower(final ForgetFollowerRequest request, fin * @return cancellable that may be used to cancel the request */ public Cancellable forgetFollowerAsync( - final ForgetFollowerRequest request, - final RequestOptions options, - final ActionListener listener) { + final ForgetFollowerRequest request, + final RequestOptions options, + final ActionListener listener + ) { return restHighLevelClient.performRequestAsyncAndParseEntity( - request, - CcrRequestConverters::forgetFollower, - options, - BroadcastResponse::fromXContent, - listener, - Collections.emptySet()); + request, + CcrRequestConverters::forgetFollower, + options, + BroadcastResponse::fromXContent, + listener, + Collections.emptySet() + ); } /** @@ -305,16 +306,19 @@ public AcknowledgedResponse putAutoFollowPattern(PutAutoFollowPatternRequest req * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable putAutoFollowPatternAsync(PutAutoFollowPatternRequest request, - RequestOptions options, - ActionListener listener) { + public Cancellable putAutoFollowPatternAsync( + PutAutoFollowPatternRequest request, + RequestOptions options, + ActionListener listener + ) { return restHighLevelClient.performRequestAsyncAndParseEntity( request, CcrRequestConverters::putAutoFollowPattern, options, AcknowledgedResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -328,8 +332,7 @@ public Cancellable putAutoFollowPatternAsync(PutAutoFollowPatternRequest request * @return the response * @throws IOException in case there is a problem sending the request or parsing back the response */ - public AcknowledgedResponse deleteAutoFollowPattern(DeleteAutoFollowPatternRequest request, - RequestOptions options) throws IOException { + public AcknowledgedResponse deleteAutoFollowPattern(DeleteAutoFollowPatternRequest request, RequestOptions options) throws IOException { return restHighLevelClient.performRequestAndParseEntity( request, CcrRequestConverters::deleteAutoFollowPattern, @@ -349,9 +352,11 @@ public AcknowledgedResponse deleteAutoFollowPattern(DeleteAutoFollowPatternReque * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable deleteAutoFollowPatternAsync(DeleteAutoFollowPatternRequest request, - RequestOptions options, - ActionListener listener) { + public Cancellable deleteAutoFollowPatternAsync( + DeleteAutoFollowPatternRequest request, + RequestOptions options, + ActionListener listener + ) { return restHighLevelClient.performRequestAsyncAndParseEntity( request, CcrRequestConverters::deleteAutoFollowPattern, @@ -373,8 +378,8 @@ public Cancellable deleteAutoFollowPatternAsync(DeleteAutoFollowPatternRequest r * @return the response * @throws IOException in case there is a problem sending the request or parsing back the response */ - public GetAutoFollowPatternResponse getAutoFollowPattern(GetAutoFollowPatternRequest request, - RequestOptions options) throws IOException { + public GetAutoFollowPatternResponse getAutoFollowPattern(GetAutoFollowPatternRequest request, RequestOptions options) + throws IOException { return restHighLevelClient.performRequestAndParseEntity( request, CcrRequestConverters::getAutoFollowPattern, @@ -394,9 +399,11 @@ public GetAutoFollowPatternResponse getAutoFollowPattern(GetAutoFollowPatternReq * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getAutoFollowPatternAsync(GetAutoFollowPatternRequest request, - RequestOptions options, - ActionListener listener) { + public Cancellable getAutoFollowPatternAsync( + GetAutoFollowPatternRequest request, + RequestOptions options, + ActionListener listener + ) { return restHighLevelClient.performRequestAsyncAndParseEntity( request, CcrRequestConverters::getAutoFollowPattern, @@ -438,16 +445,19 @@ public AcknowledgedResponse pauseAutoFollowPattern(PauseAutoFollowPatternRequest * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable pauseAutoFollowPatternAsync(PauseAutoFollowPatternRequest request, - RequestOptions options, - ActionListener listener) { + public Cancellable pauseAutoFollowPatternAsync( + PauseAutoFollowPatternRequest request, + RequestOptions options, + ActionListener listener + ) { return restHighLevelClient.performRequestAsyncAndParseEntity( request, CcrRequestConverters::pauseAutoFollowPattern, options, AcknowledgedResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -481,16 +491,19 @@ public AcknowledgedResponse resumeAutoFollowPattern(ResumeAutoFollowPatternReque * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable resumeAutoFollowPatternAsync(ResumeAutoFollowPatternRequest request, - RequestOptions options, - ActionListener listener) { + public Cancellable resumeAutoFollowPatternAsync( + ResumeAutoFollowPatternRequest request, + RequestOptions options, + ActionListener listener + ) { return restHighLevelClient.performRequestAsyncAndParseEntity( request, CcrRequestConverters::resumeAutoFollowPattern, options, AcknowledgedResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -504,8 +517,7 @@ public Cancellable resumeAutoFollowPatternAsync(ResumeAutoFollowPatternRequest r * @return the response * @throws IOException in case there is a problem sending the request or parsing back the response */ - public CcrStatsResponse getCcrStats(CcrStatsRequest request, - RequestOptions options) throws IOException { + public CcrStatsResponse getCcrStats(CcrStatsRequest request, RequestOptions options) throws IOException { return restHighLevelClient.performRequestAndParseEntity( request, CcrRequestConverters::getCcrStats, @@ -524,9 +536,7 @@ public CcrStatsResponse getCcrStats(CcrStatsRequest request, * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized * @return cancellable that may be used to cancel the request */ - public Cancellable getCcrStatsAsync(CcrStatsRequest request, - RequestOptions options, - ActionListener listener) { + public Cancellable getCcrStatsAsync(CcrStatsRequest request, RequestOptions options, ActionListener listener) { return restHighLevelClient.performRequestAsyncAndParseEntity( request, CcrRequestConverters::getCcrStats, @@ -548,8 +558,7 @@ public Cancellable getCcrStatsAsync(CcrStatsRequest request, * @return the response * @throws IOException in case there is a problem sending the request or parsing back the response */ - public FollowStatsResponse getFollowStats(FollowStatsRequest request, - RequestOptions options) throws IOException { + public FollowStatsResponse getFollowStats(FollowStatsRequest request, RequestOptions options) throws IOException { return restHighLevelClient.performRequestAndParseEntity( request, CcrRequestConverters::getFollowStats, @@ -568,9 +577,11 @@ public FollowStatsResponse getFollowStats(FollowStatsRequest request, * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized * @return cancellable that may be used to cancel the request */ - public Cancellable getFollowStatsAsync(FollowStatsRequest request, - RequestOptions options, - ActionListener listener) { + public Cancellable getFollowStatsAsync( + FollowStatsRequest request, + RequestOptions options, + ActionListener listener + ) { return restHighLevelClient.performRequestAsyncAndParseEntity( request, CcrRequestConverters::getFollowStats, @@ -611,9 +622,7 @@ public FollowInfoResponse getFollowInfo(FollowInfoRequest request, RequestOption * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized * @return cancellable that may be used to cancel the request */ - public Cancellable getFollowInfoAsync(FollowInfoRequest request, - RequestOptions options, - ActionListener listener) { + public Cancellable getFollowInfoAsync(FollowInfoRequest request, RequestOptions options, ActionListener listener) { return restHighLevelClient.performRequestAsyncAndParseEntity( request, CcrRequestConverters::getFollowInfo, diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/CcrRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/CcrRequestConverters.java index ba8b5e37e0c56..a558c2ff604fa 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/CcrRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/CcrRequestConverters.java @@ -34,8 +34,7 @@ final class CcrRequestConverters { static Request putFollow(PutFollowRequest putFollowRequest) throws IOException { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPart(putFollowRequest.getFollowerIndex()) + String endpoint = new RequestConverters.EndpointBuilder().addPathPart(putFollowRequest.getFollowerIndex()) .addPathPartAsIs("_ccr", "follow") .build(); Request request = new Request(HttpPut.METHOD_NAME, endpoint); @@ -47,16 +46,14 @@ static Request putFollow(PutFollowRequest putFollowRequest) throws IOException { } static Request pauseFollow(PauseFollowRequest pauseFollowRequest) { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPart(pauseFollowRequest.getFollowerIndex()) + String endpoint = new RequestConverters.EndpointBuilder().addPathPart(pauseFollowRequest.getFollowerIndex()) .addPathPartAsIs("_ccr", "pause_follow") .build(); return new Request(HttpPost.METHOD_NAME, endpoint); } static Request resumeFollow(ResumeFollowRequest resumeFollowRequest) throws IOException { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPart(resumeFollowRequest.getFollowerIndex()) + String endpoint = new RequestConverters.EndpointBuilder().addPathPart(resumeFollowRequest.getFollowerIndex()) .addPathPartAsIs("_ccr", "resume_follow") .build(); Request request = new Request(HttpPost.METHOD_NAME, endpoint); @@ -65,27 +62,24 @@ static Request resumeFollow(ResumeFollowRequest resumeFollowRequest) throws IOEx } static Request unfollow(UnfollowRequest unfollowRequest) { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPart(unfollowRequest.getFollowerIndex()) + String endpoint = new RequestConverters.EndpointBuilder().addPathPart(unfollowRequest.getFollowerIndex()) .addPathPartAsIs("_ccr", "unfollow") .build(); return new Request(HttpPost.METHOD_NAME, endpoint); } static Request forgetFollower(final ForgetFollowerRequest forgetFollowerRequest) throws IOException { - final String endpoint = new RequestConverters.EndpointBuilder() - .addPathPart(forgetFollowerRequest.leaderIndex()) - .addPathPartAsIs("_ccr") - .addPathPartAsIs("forget_follower") - .build(); + final String endpoint = new RequestConverters.EndpointBuilder().addPathPart(forgetFollowerRequest.leaderIndex()) + .addPathPartAsIs("_ccr") + .addPathPartAsIs("forget_follower") + .build(); final Request request = new Request(HttpPost.METHOD_NAME, endpoint); request.setEntity(createEntity(forgetFollowerRequest, REQUEST_BODY_CONTENT_TYPE)); return request; } static Request putAutoFollowPattern(PutAutoFollowPatternRequest putAutoFollowPatternRequest) throws IOException { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_ccr", "auto_follow") + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_ccr", "auto_follow") .addPathPart(putAutoFollowPatternRequest.getName()) .build(); Request request = new Request(HttpPut.METHOD_NAME, endpoint); @@ -94,24 +88,21 @@ static Request putAutoFollowPattern(PutAutoFollowPatternRequest putAutoFollowPat } static Request deleteAutoFollowPattern(DeleteAutoFollowPatternRequest deleteAutoFollowPatternRequest) { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_ccr", "auto_follow") + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_ccr", "auto_follow") .addPathPart(deleteAutoFollowPatternRequest.getName()) .build(); return new Request(HttpDelete.METHOD_NAME, endpoint); } static Request getAutoFollowPattern(GetAutoFollowPatternRequest getAutoFollowPatternRequest) { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_ccr", "auto_follow") + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_ccr", "auto_follow") .addPathPart(getAutoFollowPatternRequest.getName()) .build(); return new Request(HttpGet.METHOD_NAME, endpoint); } static Request pauseAutoFollowPattern(PauseAutoFollowPatternRequest pauseAutoFollowPatternRequest) throws IOException { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_ccr", "auto_follow") + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_ccr", "auto_follow") .addPathPart(pauseAutoFollowPatternRequest.getName()) .addPathPartAsIs("pause") .build(); @@ -119,8 +110,7 @@ static Request pauseAutoFollowPattern(PauseAutoFollowPatternRequest pauseAutoFol } static Request resumeAutoFollowPattern(ResumeAutoFollowPatternRequest resumeAutoFollowPatternRequest) throws IOException { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_ccr", "auto_follow") + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_ccr", "auto_follow") .addPathPart(resumeAutoFollowPatternRequest.getName()) .addPathPartAsIs("resume") .build(); @@ -128,23 +118,19 @@ static Request resumeAutoFollowPattern(ResumeAutoFollowPatternRequest resumeAuto } static Request getCcrStats(CcrStatsRequest ccrStatsRequest) { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_ccr", "stats") - .build(); + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_ccr", "stats").build(); return new Request(HttpGet.METHOD_NAME, endpoint); } static Request getFollowStats(FollowStatsRequest followStatsRequest) { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPart(followStatsRequest.getFollowerIndex()) + String endpoint = new RequestConverters.EndpointBuilder().addPathPart(followStatsRequest.getFollowerIndex()) .addPathPartAsIs("_ccr", "stats") .build(); return new Request(HttpGet.METHOD_NAME, endpoint); } static Request getFollowInfo(FollowInfoRequest followInfoRequest) { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPart(followInfoRequest.getFollowerIndex()) + String endpoint = new RequestConverters.EndpointBuilder().addPathPart(followInfoRequest.getFollowerIndex()) .addPathPartAsIs("_ccr", "info") .build(); return new Request(HttpGet.METHOD_NAME, endpoint); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ClusterClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ClusterClient.java index 114547dfe30c2..a9a119da79bab 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ClusterClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ClusterClient.java @@ -58,9 +58,14 @@ public final class ClusterClient { * @throws IOException in case there is a problem sending the request or parsing back the response */ public ClusterUpdateSettingsResponse putSettings(ClusterUpdateSettingsRequest clusterUpdateSettingsRequest, RequestOptions options) - throws IOException { - return restHighLevelClient.performRequestAndParseEntity(clusterUpdateSettingsRequest, ClusterRequestConverters::clusterPutSettings, - options, ClusterUpdateSettingsResponse::fromXContent, emptySet()); + throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + clusterUpdateSettingsRequest, + ClusterRequestConverters::clusterPutSettings, + options, + ClusterUpdateSettingsResponse::fromXContent, + emptySet() + ); } /** @@ -72,11 +77,19 @@ public ClusterUpdateSettingsResponse putSettings(ClusterUpdateSettingsRequest cl * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable putSettingsAsync(ClusterUpdateSettingsRequest clusterUpdateSettingsRequest, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(clusterUpdateSettingsRequest, - ClusterRequestConverters::clusterPutSettings, - options, ClusterUpdateSettingsResponse::fromXContent, listener, emptySet()); + public Cancellable putSettingsAsync( + ClusterUpdateSettingsRequest clusterUpdateSettingsRequest, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + clusterUpdateSettingsRequest, + ClusterRequestConverters::clusterPutSettings, + options, + ClusterUpdateSettingsResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -90,8 +103,13 @@ public Cancellable putSettingsAsync(ClusterUpdateSettingsRequest clusterUpdateSe */ public ClusterGetSettingsResponse getSettings(ClusterGetSettingsRequest clusterGetSettingsRequest, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(clusterGetSettingsRequest, ClusterRequestConverters::clusterGetSettings, - options, ClusterGetSettingsResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + clusterGetSettingsRequest, + ClusterRequestConverters::clusterGetSettings, + options, + ClusterGetSettingsResponse::fromXContent, + emptySet() + ); } /** @@ -103,11 +121,19 @@ public ClusterGetSettingsResponse getSettings(ClusterGetSettingsRequest clusterG * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getSettingsAsync(ClusterGetSettingsRequest clusterGetSettingsRequest, RequestOptions options, - ActionListener listener) { + public Cancellable getSettingsAsync( + ClusterGetSettingsRequest clusterGetSettingsRequest, + RequestOptions options, + ActionListener listener + ) { return restHighLevelClient.performRequestAsyncAndParseEntity( - clusterGetSettingsRequest, ClusterRequestConverters::clusterGetSettings, - options, ClusterGetSettingsResponse::fromXContent, listener, emptySet()); + clusterGetSettingsRequest, + ClusterRequestConverters::clusterGetSettings, + options, + ClusterGetSettingsResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -122,8 +148,13 @@ public Cancellable getSettingsAsync(ClusterGetSettingsRequest clusterGetSettings * @throws IOException in case there is a problem sending the request or parsing back the response */ public ClusterHealthResponse health(ClusterHealthRequest healthRequest, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(healthRequest, ClusterRequestConverters::clusterHealth, options, - ClusterHealthResponse::fromXContent, singleton(RestStatus.REQUEST_TIMEOUT.getStatus())); + return restHighLevelClient.performRequestAndParseEntity( + healthRequest, + ClusterRequestConverters::clusterHealth, + options, + ClusterHealthResponse::fromXContent, + singleton(RestStatus.REQUEST_TIMEOUT.getStatus()) + ); } /** @@ -136,10 +167,19 @@ public ClusterHealthResponse health(ClusterHealthRequest healthRequest, RequestO * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable healthAsync(ClusterHealthRequest healthRequest, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(healthRequest, ClusterRequestConverters::clusterHealth, options, - ClusterHealthResponse::fromXContent, listener, singleton(RestStatus.REQUEST_TIMEOUT.getStatus())); + public Cancellable healthAsync( + ClusterHealthRequest healthRequest, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + healthRequest, + ClusterRequestConverters::clusterHealth, + options, + ClusterHealthResponse::fromXContent, + listener, + singleton(RestStatus.REQUEST_TIMEOUT.getStatus()) + ); } /** @@ -152,8 +192,13 @@ public Cancellable healthAsync(ClusterHealthRequest healthRequest, RequestOption * @throws IOException in case there is a problem sending the request or parsing back the response */ public RemoteInfoResponse remoteInfo(RemoteInfoRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, ClusterRequestConverters::remoteInfo, options, - RemoteInfoResponse::fromXContent, singleton(RestStatus.REQUEST_TIMEOUT.getStatus())); + return restHighLevelClient.performRequestAndParseEntity( + request, + ClusterRequestConverters::remoteInfo, + options, + RemoteInfoResponse::fromXContent, + singleton(RestStatus.REQUEST_TIMEOUT.getStatus()) + ); } /** @@ -165,10 +210,15 @@ public RemoteInfoResponse remoteInfo(RemoteInfoRequest request, RequestOptions o * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable remoteInfoAsync(RemoteInfoRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, ClusterRequestConverters::remoteInfo, options, - RemoteInfoResponse::fromXContent, listener, singleton(RestStatus.REQUEST_TIMEOUT.getStatus())); + public Cancellable remoteInfoAsync(RemoteInfoRequest request, RequestOptions options, ActionListener listener) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + ClusterRequestConverters::remoteInfo, + options, + RemoteInfoResponse::fromXContent, + listener, + singleton(RestStatus.REQUEST_TIMEOUT.getStatus()) + ); } /** @@ -179,8 +229,13 @@ public Cancellable remoteInfoAsync(RemoteInfoRequest request, RequestOptions opt * @throws IOException in case there is a problem sending the request or parsing back the response */ public AcknowledgedResponse deleteComponentTemplate(DeleteComponentTemplateRequest req, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(req, ClusterRequestConverters::deleteComponentTemplate, - options, AcknowledgedResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + req, + ClusterRequestConverters::deleteComponentTemplate, + options, + AcknowledgedResponse::fromXContent, + emptySet() + ); } /** @@ -191,10 +246,19 @@ public AcknowledgedResponse deleteComponentTemplate(DeleteComponentTemplateReque * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable deleteComponentTemplateAsync(DeleteComponentTemplateRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, ClusterRequestConverters::deleteComponentTemplate, - options, AcknowledgedResponse::fromXContent, listener, emptySet()); + public Cancellable deleteComponentTemplateAsync( + DeleteComponentTemplateRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + ClusterRequestConverters::deleteComponentTemplate, + options, + AcknowledgedResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -205,10 +269,15 @@ public Cancellable deleteComponentTemplateAsync(DeleteComponentTemplateRequest r * @return the response * @throws IOException in case there is a problem sending the request or parsing back the response */ - public AcknowledgedResponse putComponentTemplate(PutComponentTemplateRequest putComponentTemplateRequest, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(putComponentTemplateRequest, ClusterRequestConverters::putComponentTemplate, - options, AcknowledgedResponse::fromXContent, emptySet()); + public AcknowledgedResponse putComponentTemplate(PutComponentTemplateRequest putComponentTemplateRequest, RequestOptions options) + throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + putComponentTemplateRequest, + ClusterRequestConverters::putComponentTemplate, + options, + AcknowledgedResponse::fromXContent, + emptySet() + ); } /** @@ -219,10 +288,19 @@ public AcknowledgedResponse putComponentTemplate(PutComponentTemplateRequest put * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable putComponentTemplateAsync(PutComponentTemplateRequest putComponentTemplateRequest, - RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(putComponentTemplateRequest, - ClusterRequestConverters::putComponentTemplate, options, AcknowledgedResponse::fromXContent, listener, emptySet()); + public Cancellable putComponentTemplateAsync( + PutComponentTemplateRequest putComponentTemplateRequest, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + putComponentTemplateRequest, + ClusterRequestConverters::putComponentTemplate, + options, + AcknowledgedResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -232,10 +310,17 @@ public Cancellable putComponentTemplateAsync(PutComponentTemplateRequest putComp * @return the response * @throws IOException in case there is a problem sending the request or parsing back the response */ - public GetComponentTemplatesResponse getComponentTemplate(GetComponentTemplatesRequest getComponentTemplatesRequest, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(getComponentTemplatesRequest, - ClusterRequestConverters::getComponentTemplates, options, GetComponentTemplatesResponse::fromXContent, emptySet()); + public GetComponentTemplatesResponse getComponentTemplate( + GetComponentTemplatesRequest getComponentTemplatesRequest, + RequestOptions options + ) throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + getComponentTemplatesRequest, + ClusterRequestConverters::getComponentTemplates, + options, + GetComponentTemplatesResponse::fromXContent, + emptySet() + ); } /** @@ -245,10 +330,19 @@ public GetComponentTemplatesResponse getComponentTemplate(GetComponentTemplatesR * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getComponentTemplateAsync(GetComponentTemplatesRequest getComponentTemplatesRequest, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(getComponentTemplatesRequest, - ClusterRequestConverters::getComponentTemplates, options, GetComponentTemplatesResponse::fromXContent, listener, emptySet()); + public Cancellable getComponentTemplateAsync( + GetComponentTemplatesRequest getComponentTemplatesRequest, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + getComponentTemplatesRequest, + ClusterRequestConverters::getComponentTemplates, + options, + GetComponentTemplatesResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -259,10 +353,15 @@ public Cancellable getComponentTemplateAsync(GetComponentTemplatesRequest getCom * @return true if any index templates in the request exist, false otherwise * @throws IOException in case there is a problem sending the request or parsing back the response */ - public boolean existsComponentTemplate(ComponentTemplatesExistRequest componentTemplatesRequest, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequest(componentTemplatesRequest, - ClusterRequestConverters::componentTemplatesExist, options, RestHighLevelClient::convertExistsResponse, emptySet()); + public boolean existsComponentTemplate(ComponentTemplatesExistRequest componentTemplatesRequest, RequestOptions options) + throws IOException { + return restHighLevelClient.performRequest( + componentTemplatesRequest, + ClusterRequestConverters::componentTemplatesExist, + options, + RestHighLevelClient::convertExistsResponse, + emptySet() + ); } /** @@ -272,11 +371,19 @@ public boolean existsComponentTemplate(ComponentTemplatesExistRequest componentT * @param listener the listener to be notified upon request completion. The listener will be called with the value {@code true} * @return cancellable that may be used to cancel the request */ - public Cancellable existsComponentTemplateAsync(ComponentTemplatesExistRequest componentTemplatesRequest, - RequestOptions options, - ActionListener listener) { + public Cancellable existsComponentTemplateAsync( + ComponentTemplatesExistRequest componentTemplatesRequest, + RequestOptions options, + ActionListener listener + ) { - return restHighLevelClient.performRequestAsync(componentTemplatesRequest, - ClusterRequestConverters::componentTemplatesExist, options, RestHighLevelClient::convertExistsResponse, listener, emptySet()); + return restHighLevelClient.performRequestAsync( + componentTemplatesRequest, + ClusterRequestConverters::componentTemplatesExist, + options, + RestHighLevelClient::convertExistsResponse, + listener, + emptySet() + ); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ClusterRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ClusterRequestConverters.java index affe7cce2f82c..36d09cfab6f25 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ClusterRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ClusterRequestConverters.java @@ -52,14 +52,12 @@ static Request clusterGetSettings(ClusterGetSettingsRequest clusterGetSettingsRe static Request clusterHealth(ClusterHealthRequest healthRequest) { String[] indices = healthRequest.indices() == null ? Strings.EMPTY_ARRAY : healthRequest.indices(); - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_cluster/health") + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_cluster/health") .addCommaSeparatedPathParts(indices) .build(); Request request = new Request(HttpGet.METHOD_NAME, endpoint); - RequestConverters.Params params = new RequestConverters.Params() - .withWaitForStatus(healthRequest.waitForStatus()) + RequestConverters.Params params = new RequestConverters.Params().withWaitForStatus(healthRequest.waitForStatus()) .withWaitForNoRelocatingShards(healthRequest.waitForNoRelocatingShards()) .withWaitForNoInitializingShards(healthRequest.waitForNoInitializingShards()) .withWaitForActiveShards(healthRequest.waitForActiveShards(), ActiveShardCount.NONE) @@ -79,7 +77,8 @@ static Request remoteInfo(RemoteInfoRequest remoteInfoRequest) { static Request putComponentTemplate(PutComponentTemplateRequest putComponentTemplateRequest) throws IOException { String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_component_template") - .addPathPart(putComponentTemplateRequest.name()).build(); + .addPathPart(putComponentTemplateRequest.name()) + .build(); Request request = new Request(HttpPut.METHOD_NAME, endpoint); RequestConverters.Params params = new RequestConverters.Params(); params.withMasterTimeout(putComponentTemplateRequest.masterNodeTimeout()); @@ -94,9 +93,8 @@ static Request putComponentTemplate(PutComponentTemplateRequest putComponentTemp return request; } - static Request getComponentTemplates(GetComponentTemplatesRequest getComponentTemplatesRequest){ - final String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_component_template") + static Request getComponentTemplates(GetComponentTemplatesRequest getComponentTemplatesRequest) { + final String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_component_template") .addPathPart(getComponentTemplatesRequest.name()) .build(); final Request request = new Request(HttpGet.METHOD_NAME, endpoint); @@ -108,8 +106,7 @@ static Request getComponentTemplates(GetComponentTemplatesRequest getComponentTe } static Request componentTemplatesExist(ComponentTemplatesExistRequest componentTemplatesRequest) { - final String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_component_template") + final String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_component_template") .addPathPart(componentTemplatesRequest.name()) .build(); final Request request = new Request(HttpHead.METHOD_NAME, endpoint); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/EnrichClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/EnrichClient.java index 3744eaeb93ad8..772cb65fc0a11 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/EnrichClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/EnrichClient.java @@ -74,9 +74,7 @@ public AcknowledgedResponse putPolicy(PutPolicyRequest request, RequestOptions o * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable putPolicyAsync(PutPolicyRequest request, - RequestOptions options, - ActionListener listener) { + public Cancellable putPolicyAsync(PutPolicyRequest request, RequestOptions options, ActionListener listener) { return restHighLevelClient.performRequestAsyncAndParseEntity( request, EnrichRequestConverters::putPolicy, @@ -119,9 +117,11 @@ public AcknowledgedResponse deletePolicy(DeletePolicyRequest request, RequestOpt * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable deletePolicyAsync(DeletePolicyRequest request, - RequestOptions options, - ActionListener listener) { + public Cancellable deletePolicyAsync( + DeletePolicyRequest request, + RequestOptions options, + ActionListener listener + ) { return restHighLevelClient.performRequestAsyncAndParseEntity( request, EnrichRequestConverters::deletePolicy, @@ -164,9 +164,7 @@ public GetPolicyResponse getPolicy(GetPolicyRequest request, RequestOptions opti * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getPolicyAsync(GetPolicyRequest request, - RequestOptions options, - ActionListener listener) { + public Cancellable getPolicyAsync(GetPolicyRequest request, RequestOptions options, ActionListener listener) { return restHighLevelClient.performRequestAsyncAndParseEntity( request, EnrichRequestConverters::getPolicy, @@ -209,9 +207,7 @@ public StatsResponse stats(StatsRequest request, RequestOptions options) throws * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable statsAsync(StatsRequest request, - RequestOptions options, - ActionListener listener) { + public Cancellable statsAsync(StatsRequest request, RequestOptions options, ActionListener listener) { return restHighLevelClient.performRequestAsyncAndParseEntity( request, EnrichRequestConverters::stats, @@ -254,9 +250,11 @@ public ExecutePolicyResponse executePolicy(ExecutePolicyRequest request, Request * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable executePolicyAsync(ExecutePolicyRequest request, - RequestOptions options, - ActionListener listener) { + public Cancellable executePolicyAsync( + ExecutePolicyRequest request, + RequestOptions options, + ActionListener listener + ) { return restHighLevelClient.performRequestAsyncAndParseEntity( request, EnrichRequestConverters::executePolicy, diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/EnrichRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/EnrichRequestConverters.java index 47d6863dc6077..a00ae7624696d 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/EnrichRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/EnrichRequestConverters.java @@ -25,8 +25,7 @@ final class EnrichRequestConverters { static Request putPolicy(PutPolicyRequest putPolicyRequest) throws IOException { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_enrich", "policy") + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_enrich", "policy") .addPathPart(putPolicyRequest.getName()) .build(); Request request = new Request(HttpPut.METHOD_NAME, endpoint); @@ -35,31 +34,26 @@ static Request putPolicy(PutPolicyRequest putPolicyRequest) throws IOException { } static Request deletePolicy(DeletePolicyRequest deletePolicyRequest) { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_enrich", "policy") + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_enrich", "policy") .addPathPart(deletePolicyRequest.getName()) .build(); return new Request(HttpDelete.METHOD_NAME, endpoint); } static Request getPolicy(GetPolicyRequest getPolicyRequest) { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_enrich", "policy") + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_enrich", "policy") .addCommaSeparatedPathParts(getPolicyRequest.getNames()) .build(); return new Request(HttpGet.METHOD_NAME, endpoint); } static Request stats(StatsRequest statsRequest) { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_enrich", "_stats") - .build(); + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_enrich", "_stats").build(); return new Request(HttpGet.METHOD_NAME, endpoint); } static Request executePolicy(ExecutePolicyRequest executePolicyRequest) { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_enrich", "policy") + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_enrich", "policy") .addPathPart(executePolicyRequest.getName()) .addPathPartAsIs("_execute") .build(); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/EqlClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/EqlClient.java index 6b1a44ca6ea1e..ab23d8b0bcf8a 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/EqlClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/EqlClient.java @@ -70,9 +70,7 @@ public EqlSearchResponse search(EqlSearchRequest request, RequestOptions options * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable searchAsync(EqlSearchRequest request, - RequestOptions options, - ActionListener listener) { + public Cancellable searchAsync(EqlSearchRequest request, RequestOptions options, ActionListener listener) { return restHighLevelClient.performRequestAsyncAndParseEntity( request, EqlRequestConverters::search, @@ -92,7 +90,7 @@ public Cancellable searchAsync(EqlSearchRequest request, * @return the response * @throws IOException in case there is a problem sending the request or parsing back the response */ - public EqlStatsResponse stats(EqlStatsRequest request, RequestOptions options) throws IOException { + public EqlStatsResponse stats(EqlStatsRequest request, RequestOptions options) throws IOException { return restHighLevelClient.performRequestAndParseEntity( request, EqlRequestConverters::stats, @@ -112,7 +110,8 @@ public EqlStatsResponse stats(EqlStatsRequest request, RequestOptions options) * @return cancellable that may be used to cancel the request */ public Cancellable statsAsync(EqlStatsRequest request, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, EqlRequestConverters::stats, options, EqlStatsResponse::fromXContent, diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/EqlRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/EqlRequestConverters.java index 907c5c0458145..f788d6fcf15fb 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/EqlRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/EqlRequestConverters.java @@ -20,8 +20,7 @@ final class EqlRequestConverters { static Request search(EqlSearchRequest eqlSearchRequest) throws IOException { - String endpoint = new RequestConverters.EndpointBuilder() - .addCommaSeparatedPathParts(eqlSearchRequest.indices()) + String endpoint = new RequestConverters.EndpointBuilder().addCommaSeparatedPathParts(eqlSearchRequest.indices()) .addPathPartAsIs("_eql", "search") .build(); Request request = new Request(HttpGet.METHOD_NAME, endpoint); @@ -33,9 +32,7 @@ static Request search(EqlSearchRequest eqlSearchRequest) throws IOException { } static Request stats(EqlStatsRequest eqlStatsRequest) throws IOException { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_eql", "stats") - .build(); + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_eql", "stats").build(); return new Request(HttpGet.METHOD_NAME, endpoint); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/FeaturesClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/FeaturesClient.java index 11c92276ce6e5..3a63162960a7c 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/FeaturesClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/FeaturesClient.java @@ -46,8 +46,7 @@ public class FeaturesClient { * @return the response * @throws IOException in case there is a problem sending the request or parsing back the response */ - public GetFeaturesResponse getFeatures(GetFeaturesRequest getFeaturesRequest, RequestOptions options) - throws IOException { + public GetFeaturesResponse getFeatures(GetFeaturesRequest getFeaturesRequest, RequestOptions options) throws IOException { return restHighLevelClient.performRequestAndParseEntity( getFeaturesRequest, FeaturesRequestConverters::getFeatures, @@ -68,8 +67,10 @@ public GetFeaturesResponse getFeatures(GetFeaturesRequest getFeaturesRequest, Re * @return cancellable that may be used to cancel the request */ public Cancellable getFeaturesAsync( - GetFeaturesRequest getFeaturesRequest, RequestOptions options, - ActionListener listener) { + GetFeaturesRequest getFeaturesRequest, + RequestOptions options, + ActionListener listener + ) { return restHighLevelClient.performRequestAsyncAndParseEntity( getFeaturesRequest, FeaturesRequestConverters::getFeatures, @@ -91,8 +92,7 @@ public Cancellable getFeaturesAsync( * @return the response * @throws IOException in case there is a problem sending the request or parsing back the response */ - public ResetFeaturesResponse resetFeatures(ResetFeaturesRequest resetFeaturesRequest, RequestOptions options) - throws IOException { + public ResetFeaturesResponse resetFeatures(ResetFeaturesRequest resetFeaturesRequest, RequestOptions options) throws IOException { return restHighLevelClient.performRequestAndParseEntity( resetFeaturesRequest, FeaturesRequestConverters::resetFeatures, @@ -114,8 +114,10 @@ public ResetFeaturesResponse resetFeatures(ResetFeaturesRequest resetFeaturesReq * @return cancellable that may be used to cancel the request */ public Cancellable resetFeaturesAsync( - ResetFeaturesRequest resetFeaturesRequest, RequestOptions options, - ActionListener listener) { + ResetFeaturesRequest resetFeaturesRequest, + RequestOptions options, + ActionListener listener + ) { return restHighLevelClient.performRequestAsyncAndParseEntity( resetFeaturesRequest, FeaturesRequestConverters::resetFeatures, diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/GeoIpStatsResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/GeoIpStatsResponse.java index 74d50b897d00b..64cc9ccf3c53d 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/GeoIpStatsResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/GeoIpStatsResponse.java @@ -8,9 +8,9 @@ package org.elasticsearch.client; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.core.Tuple; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -33,15 +33,23 @@ public class GeoIpStatsResponse implements ToXContentObject { Map stats = (Map) a[0]; List> nodes = (List>) a[1]; - return new GeoIpStatsResponse((int) stats.get("successful_downloads"), (int) stats.get("failed_downloads"), - ((Number) stats.get("total_download_time")).longValue(), (int) stats.get("databases_count"), (int) stats.get("skipped_updates"), - nodes.stream().collect(Collectors.toMap(Tuple::v1, Tuple::v2))); + return new GeoIpStatsResponse( + (int) stats.get("successful_downloads"), + (int) stats.get("failed_downloads"), + ((Number) stats.get("total_download_time")).longValue(), + (int) stats.get("databases_count"), + (int) stats.get("skipped_updates"), + nodes.stream().collect(Collectors.toMap(Tuple::v1, Tuple::v2)) + ); }); static { PARSER.declareObject(constructorArg(), (p, c) -> p.map(), new ParseField("stats")); - PARSER.declareNamedObjects(constructorArg(), (p, c, name) -> Tuple.tuple(name, NodeInfo.PARSER.apply(p, c)), - new ParseField("nodes")); + PARSER.declareNamedObjects( + constructorArg(), + (p, c, name) -> Tuple.tuple(name, NodeInfo.PARSER.apply(p, c)), + new ParseField("nodes") + ); } private final int successfulDownloads; @@ -51,8 +59,14 @@ public class GeoIpStatsResponse implements ToXContentObject { private final int skippedDownloads; private final Map nodes; - public GeoIpStatsResponse(int successfulDownloads, int failedDownloads, long totalDownloadTime, int databasesCount, - int skippedDownloads, Map nodes) { + public GeoIpStatsResponse( + int successfulDownloads, + int failedDownloads, + long totalDownloadTime, + int databasesCount, + int skippedDownloads, + Map nodes + ) { this.successfulDownloads = successfulDownloads; this.failedDownloads = failedDownloads; this.totalDownloadTime = totalDownloadTime; @@ -128,8 +142,10 @@ public static final class NodeInfo implements ToXContentObject { @SuppressWarnings("unchecked") private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("node_info", a -> { List databases = (List) a[1]; - return new NodeInfo((Collection) a[0], databases.stream().collect(Collectors.toMap(DatabaseInfo::getName, - Function.identity()))); + return new NodeInfo( + (Collection) a[0], + databases.stream().collect(Collectors.toMap(DatabaseInfo::getName, Function.identity())) + ); }); static { @@ -157,10 +173,10 @@ public Map getDatabases() { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); builder.stringListField("files_in_temp", filesInTemp); - builder.field("databases", databases.entrySet().stream() - .sorted(Map.Entry.comparingByKey()) - .map(Map.Entry::getValue) - .collect(Collectors.toList())); + builder.field( + "databases", + databases.entrySet().stream().sorted(Map.Entry.comparingByKey()).map(Map.Entry::getValue).collect(Collectors.toList()) + ); builder.endObject(); return builder; } @@ -181,8 +197,10 @@ public int hashCode() { public static final class DatabaseInfo implements ToXContentObject { - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("database_info", - a -> new DatabaseInfo((String) a[0])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "database_info", + a -> new DatabaseInfo((String) a[0]) + ); static { PARSER.declareString(constructorArg(), new ParseField("name")); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/GetAliasesResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/GetAliasesResponse.java index cc700bcba940d..c5a1d97b7edf4 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/GetAliasesResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/GetAliasesResponse.java @@ -11,11 +11,11 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.cluster.metadata.AliasMetadata; import org.elasticsearch.common.xcontent.StatusToXContentObject; +import org.elasticsearch.rest.RestStatus; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentParser.Token; -import org.elasticsearch.rest.RestStatus; import java.io.IOException; import java.util.Collections; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/GraphClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/GraphClient.java index f0652eeecc852..fe043da3aea1a 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/GraphClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/GraphClient.java @@ -36,10 +36,14 @@ public class GraphClient { * See Graph API * on elastic.co. */ - public final GraphExploreResponse explore(GraphExploreRequest graphExploreRequest, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(graphExploreRequest, GraphRequestConverters::explore, - options, GraphExploreResponse::fromXContent, emptySet()); + public final GraphExploreResponse explore(GraphExploreRequest graphExploreRequest, RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + graphExploreRequest, + GraphRequestConverters::explore, + options, + GraphExploreResponse::fromXContent, + emptySet() + ); } /** @@ -49,11 +53,19 @@ public final GraphExploreResponse explore(GraphExploreRequest graphExploreReques * on elastic.co. * @return cancellable that may be used to cancel the request */ - public final Cancellable exploreAsync(GraphExploreRequest graphExploreRequest, - RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(graphExploreRequest, GraphRequestConverters::explore, - options, GraphExploreResponse::fromXContent, listener, emptySet()); + public final Cancellable exploreAsync( + GraphExploreRequest graphExploreRequest, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + graphExploreRequest, + GraphRequestConverters::explore, + options, + GraphExploreResponse::fromXContent, + listener, + emptySet() + ); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndexLifecycleClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndexLifecycleClient.java index 3fcc3740a63ba..84394c029e09e 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndexLifecycleClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndexLifecycleClient.java @@ -63,10 +63,14 @@ public class IndexLifecycleClient { * @return the response * @throws IOException in case there is a problem sending the request or parsing back the response */ - public GetLifecyclePolicyResponse getLifecyclePolicy(GetLifecyclePolicyRequest request, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, IndexLifecycleRequestConverters::getLifecyclePolicy, options, - GetLifecyclePolicyResponse::fromXContent, emptySet()); + public GetLifecyclePolicyResponse getLifecyclePolicy(GetLifecyclePolicyRequest request, RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, + IndexLifecycleRequestConverters::getLifecyclePolicy, + options, + GetLifecyclePolicyResponse::fromXContent, + emptySet() + ); } /** @@ -78,10 +82,19 @@ public GetLifecyclePolicyResponse getLifecyclePolicy(GetLifecyclePolicyRequest r * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getLifecyclePolicyAsync(GetLifecyclePolicyRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, IndexLifecycleRequestConverters::getLifecyclePolicy, options, - GetLifecyclePolicyResponse::fromXContent, listener, emptySet()); + public Cancellable getLifecyclePolicyAsync( + GetLifecyclePolicyRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + IndexLifecycleRequestConverters::getLifecyclePolicy, + options, + GetLifecyclePolicyResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -93,10 +106,14 @@ public Cancellable getLifecyclePolicyAsync(GetLifecyclePolicyRequest request, Re * @return the response * @throws IOException in case there is a problem sending the request or parsing back the response */ - public AcknowledgedResponse putLifecyclePolicy(PutLifecyclePolicyRequest request, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, IndexLifecycleRequestConverters::putLifecyclePolicy, options, - AcknowledgedResponse::fromXContent, emptySet()); + public AcknowledgedResponse putLifecyclePolicy(PutLifecyclePolicyRequest request, RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, + IndexLifecycleRequestConverters::putLifecyclePolicy, + options, + AcknowledgedResponse::fromXContent, + emptySet() + ); } /** @@ -108,10 +125,19 @@ public AcknowledgedResponse putLifecyclePolicy(PutLifecyclePolicyRequest request * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable putLifecyclePolicyAsync(PutLifecyclePolicyRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, IndexLifecycleRequestConverters::putLifecyclePolicy, options, - AcknowledgedResponse::fromXContent, listener, emptySet()); + public Cancellable putLifecyclePolicyAsync( + PutLifecyclePolicyRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + IndexLifecycleRequestConverters::putLifecyclePolicy, + options, + AcknowledgedResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -126,10 +152,14 @@ public Cancellable putLifecyclePolicyAsync(PutLifecyclePolicyRequest request, Re * @return the response * @throws IOException in case there is a problem sending the request or parsing back the response */ - public AcknowledgedResponse deleteLifecyclePolicy(DeleteLifecyclePolicyRequest request, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, IndexLifecycleRequestConverters::deleteLifecyclePolicy, options, - AcknowledgedResponse::fromXContent, emptySet()); + public AcknowledgedResponse deleteLifecyclePolicy(DeleteLifecyclePolicyRequest request, RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, + IndexLifecycleRequestConverters::deleteLifecyclePolicy, + options, + AcknowledgedResponse::fromXContent, + emptySet() + ); } /** @@ -144,11 +174,19 @@ public AcknowledgedResponse deleteLifecyclePolicy(DeleteLifecyclePolicyRequest r * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable deleteLifecyclePolicyAsync(DeleteLifecyclePolicyRequest request, RequestOptions options, - ActionListener listener) { + public Cancellable deleteLifecyclePolicyAsync( + DeleteLifecyclePolicyRequest request, + RequestOptions options, + ActionListener listener + ) { return restHighLevelClient.performRequestAsyncAndParseEntity( - request, IndexLifecycleRequestConverters::deleteLifecyclePolicy, options, - AcknowledgedResponse::fromXContent, listener, emptySet()); + request, + IndexLifecycleRequestConverters::deleteLifecyclePolicy, + options, + AcknowledgedResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -163,10 +201,15 @@ public Cancellable deleteLifecyclePolicyAsync(DeleteLifecyclePolicyRequest reque * @return the response * @throws IOException in case there is a problem sending the request or parsing back the response */ - public RemoveIndexLifecyclePolicyResponse removeIndexLifecyclePolicy(RemoveIndexLifecyclePolicyRequest request, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, IndexLifecycleRequestConverters::removeIndexLifecyclePolicy, - options, RemoveIndexLifecyclePolicyResponse::fromXContent, emptySet()); + public RemoveIndexLifecyclePolicyResponse removeIndexLifecyclePolicy(RemoveIndexLifecyclePolicyRequest request, RequestOptions options) + throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, + IndexLifecycleRequestConverters::removeIndexLifecyclePolicy, + options, + RemoveIndexLifecyclePolicyResponse::fromXContent, + emptySet() + ); } /** @@ -181,11 +224,19 @@ public RemoveIndexLifecyclePolicyResponse removeIndexLifecyclePolicy(RemoveIndex * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable removeIndexLifecyclePolicyAsync(RemoveIndexLifecyclePolicyRequest request, RequestOptions options, - ActionListener listener) { + public Cancellable removeIndexLifecyclePolicyAsync( + RemoveIndexLifecyclePolicyRequest request, + RequestOptions options, + ActionListener listener + ) { return restHighLevelClient.performRequestAsyncAndParseEntity( - request, IndexLifecycleRequestConverters::removeIndexLifecyclePolicy, options, - RemoveIndexLifecyclePolicyResponse::fromXContent, listener, emptySet()); + request, + IndexLifecycleRequestConverters::removeIndexLifecyclePolicy, + options, + RemoveIndexLifecyclePolicyResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -201,8 +252,13 @@ public Cancellable removeIndexLifecyclePolicyAsync(RemoveIndexLifecyclePolicyReq * @throws IOException in case there is a problem sending the request or parsing back the response */ public AcknowledgedResponse startILM(StartILMRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, IndexLifecycleRequestConverters::startILM, options, - AcknowledgedResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + IndexLifecycleRequestConverters::startILM, + options, + AcknowledgedResponse::fromXContent, + emptySet() + ); } /** @@ -218,8 +274,14 @@ public AcknowledgedResponse startILM(StartILMRequest request, RequestOptions opt * @return cancellable that may be used to cancel the request */ public Cancellable startILMAsync(StartILMRequest request, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, IndexLifecycleRequestConverters::startILM, options, - AcknowledgedResponse::fromXContent, listener, emptySet()); + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + IndexLifecycleRequestConverters::startILM, + options, + AcknowledgedResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -235,8 +297,13 @@ public Cancellable startILMAsync(StartILMRequest request, RequestOptions options * @throws IOException in case there is a problem sending the request or parsing back the response */ public AcknowledgedResponse stopILM(StopILMRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, IndexLifecycleRequestConverters::stopILM, options, - AcknowledgedResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + IndexLifecycleRequestConverters::stopILM, + options, + AcknowledgedResponse::fromXContent, + emptySet() + ); } /** @@ -252,8 +319,14 @@ public AcknowledgedResponse stopILM(StopILMRequest request, RequestOptions optio * @return cancellable that may be used to cancel the request */ public Cancellable stopILMAsync(StopILMRequest request, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, IndexLifecycleRequestConverters::stopILM, options, - AcknowledgedResponse::fromXContent, listener, emptySet()); + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + IndexLifecycleRequestConverters::stopILM, + options, + AcknowledgedResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -269,8 +342,13 @@ public Cancellable stopILMAsync(StopILMRequest request, RequestOptions options, */ public LifecycleManagementStatusResponse lifecycleManagementStatus(LifecycleManagementStatusRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, IndexLifecycleRequestConverters::lifecycleManagementStatus, - options, LifecycleManagementStatusResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + IndexLifecycleRequestConverters::lifecycleManagementStatus, + options, + LifecycleManagementStatusResponse::fromXContent, + emptySet() + ); } /** @@ -285,11 +363,19 @@ public LifecycleManagementStatusResponse lifecycleManagementStatus(LifecycleMana * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable lifecycleManagementStatusAsync(LifecycleManagementStatusRequest request, RequestOptions options, - ActionListener listener) { + public Cancellable lifecycleManagementStatusAsync( + LifecycleManagementStatusRequest request, + RequestOptions options, + ActionListener listener + ) { return restHighLevelClient.performRequestAsyncAndParseEntity( - request, IndexLifecycleRequestConverters::lifecycleManagementStatus, options, - LifecycleManagementStatusResponse::fromXContent, listener, emptySet()); + request, + IndexLifecycleRequestConverters::lifecycleManagementStatus, + options, + LifecycleManagementStatusResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -304,9 +390,14 @@ public Cancellable lifecycleManagementStatusAsync(LifecycleManagementStatusReque * @return the response * @throws IOException in case there is a problem sending the request or parsing back the response */ - public ExplainLifecycleResponse explainLifecycle(ExplainLifecycleRequest request,RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, IndexLifecycleRequestConverters::explainLifecycle, options, - ExplainLifecycleResponse::fromXContent, emptySet()); + public ExplainLifecycleResponse explainLifecycle(ExplainLifecycleRequest request, RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, + IndexLifecycleRequestConverters::explainLifecycle, + options, + ExplainLifecycleResponse::fromXContent, + emptySet() + ); } /** @@ -321,10 +412,19 @@ public ExplainLifecycleResponse explainLifecycle(ExplainLifecycleRequest request * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable explainLifecycleAsync(ExplainLifecycleRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, IndexLifecycleRequestConverters::explainLifecycle, options, - ExplainLifecycleResponse::fromXContent, listener, emptySet()); + public Cancellable explainLifecycleAsync( + ExplainLifecycleRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + IndexLifecycleRequestConverters::explainLifecycle, + options, + ExplainLifecycleResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -340,8 +440,13 @@ public Cancellable explainLifecycleAsync(ExplainLifecycleRequest request, Reques * @throws IOException in case there is a problem sending the request or parsing back the response */ public AcknowledgedResponse retryLifecyclePolicy(RetryLifecyclePolicyRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, IndexLifecycleRequestConverters::retryLifecycle, options, - AcknowledgedResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + IndexLifecycleRequestConverters::retryLifecycle, + options, + AcknowledgedResponse::fromXContent, + emptySet() + ); } /** @@ -356,10 +461,19 @@ public AcknowledgedResponse retryLifecyclePolicy(RetryLifecyclePolicyRequest req * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable retryLifecyclePolicyAsync(RetryLifecyclePolicyRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, IndexLifecycleRequestConverters::retryLifecycle, options, - AcknowledgedResponse::fromXContent, listener, emptySet()); + public Cancellable retryLifecyclePolicyAsync( + RetryLifecyclePolicyRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + IndexLifecycleRequestConverters::retryLifecycle, + options, + AcknowledgedResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -374,10 +488,15 @@ public Cancellable retryLifecyclePolicyAsync(RetryLifecyclePolicyRequest request * @return the response * @throws IOException in case there is a problem sending the request or parsing back the response */ - public GetSnapshotLifecyclePolicyResponse getSnapshotLifecyclePolicy(GetSnapshotLifecyclePolicyRequest request, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, IndexLifecycleRequestConverters::getSnapshotLifecyclePolicy, - options, GetSnapshotLifecyclePolicyResponse::fromXContent, emptySet()); + public GetSnapshotLifecyclePolicyResponse getSnapshotLifecyclePolicy(GetSnapshotLifecyclePolicyRequest request, RequestOptions options) + throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, + IndexLifecycleRequestConverters::getSnapshotLifecyclePolicy, + options, + GetSnapshotLifecyclePolicyResponse::fromXContent, + emptySet() + ); } /** @@ -392,10 +511,19 @@ public GetSnapshotLifecyclePolicyResponse getSnapshotLifecyclePolicy(GetSnapshot * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getSnapshotLifecyclePolicyAsync(GetSnapshotLifecyclePolicyRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, IndexLifecycleRequestConverters::getSnapshotLifecyclePolicy, - options, GetSnapshotLifecyclePolicyResponse::fromXContent, listener, emptySet()); + public Cancellable getSnapshotLifecyclePolicyAsync( + GetSnapshotLifecyclePolicyRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + IndexLifecycleRequestConverters::getSnapshotLifecyclePolicy, + options, + GetSnapshotLifecyclePolicyResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -410,10 +538,15 @@ public Cancellable getSnapshotLifecyclePolicyAsync(GetSnapshotLifecyclePolicyReq * @return the response * @throws IOException in case there is a problem sending the request or parsing back the response */ - public AcknowledgedResponse putSnapshotLifecyclePolicy(PutSnapshotLifecyclePolicyRequest request, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, IndexLifecycleRequestConverters::putSnapshotLifecyclePolicy, - options, AcknowledgedResponse::fromXContent, emptySet()); + public AcknowledgedResponse putSnapshotLifecyclePolicy(PutSnapshotLifecyclePolicyRequest request, RequestOptions options) + throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, + IndexLifecycleRequestConverters::putSnapshotLifecyclePolicy, + options, + AcknowledgedResponse::fromXContent, + emptySet() + ); } /** @@ -428,10 +561,19 @@ public AcknowledgedResponse putSnapshotLifecyclePolicy(PutSnapshotLifecyclePolic * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable putSnapshotLifecyclePolicyAsync(PutSnapshotLifecyclePolicyRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, IndexLifecycleRequestConverters::putSnapshotLifecyclePolicy, - options, AcknowledgedResponse::fromXContent, listener, emptySet()); + public Cancellable putSnapshotLifecyclePolicyAsync( + PutSnapshotLifecyclePolicyRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + IndexLifecycleRequestConverters::putSnapshotLifecyclePolicy, + options, + AcknowledgedResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -446,10 +588,15 @@ public Cancellable putSnapshotLifecyclePolicyAsync(PutSnapshotLifecyclePolicyReq * @return the response * @throws IOException in case there is a problem sending the request or parsing back the response */ - public AcknowledgedResponse deleteSnapshotLifecyclePolicy(DeleteSnapshotLifecyclePolicyRequest request, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, IndexLifecycleRequestConverters::deleteSnapshotLifecyclePolicy, - options, AcknowledgedResponse::fromXContent, emptySet()); + public AcknowledgedResponse deleteSnapshotLifecyclePolicy(DeleteSnapshotLifecyclePolicyRequest request, RequestOptions options) + throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, + IndexLifecycleRequestConverters::deleteSnapshotLifecyclePolicy, + options, + AcknowledgedResponse::fromXContent, + emptySet() + ); } /** @@ -464,11 +611,19 @@ public AcknowledgedResponse deleteSnapshotLifecyclePolicy(DeleteSnapshotLifecycl * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable deleteSnapshotLifecyclePolicyAsync(DeleteSnapshotLifecyclePolicyRequest request, - RequestOptions options,ActionListener listener) { + public Cancellable deleteSnapshotLifecyclePolicyAsync( + DeleteSnapshotLifecyclePolicyRequest request, + RequestOptions options, + ActionListener listener + ) { return restHighLevelClient.performRequestAsyncAndParseEntity( - request, IndexLifecycleRequestConverters::deleteSnapshotLifecyclePolicy, - options, AcknowledgedResponse::fromXContent, listener, emptySet()); + request, + IndexLifecycleRequestConverters::deleteSnapshotLifecyclePolicy, + options, + AcknowledgedResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -483,10 +638,17 @@ public Cancellable deleteSnapshotLifecyclePolicyAsync(DeleteSnapshotLifecyclePol * @return the response * @throws IOException in case there is a problem sending the request or parsing back the response */ - public ExecuteSnapshotLifecyclePolicyResponse executeSnapshotLifecyclePolicy(ExecuteSnapshotLifecyclePolicyRequest request, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, IndexLifecycleRequestConverters::executeSnapshotLifecyclePolicy, - options, ExecuteSnapshotLifecyclePolicyResponse::fromXContent, emptySet()); + public ExecuteSnapshotLifecyclePolicyResponse executeSnapshotLifecyclePolicy( + ExecuteSnapshotLifecyclePolicyRequest request, + RequestOptions options + ) throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, + IndexLifecycleRequestConverters::executeSnapshotLifecyclePolicy, + options, + ExecuteSnapshotLifecyclePolicyResponse::fromXContent, + emptySet() + ); } /** @@ -502,11 +664,18 @@ public ExecuteSnapshotLifecyclePolicyResponse executeSnapshotLifecyclePolicy(Exe * @return cancellable that may be used to cancel the request */ public Cancellable executeSnapshotLifecyclePolicyAsync( - ExecuteSnapshotLifecyclePolicyRequest request, RequestOptions options, - ActionListener listener) { + ExecuteSnapshotLifecyclePolicyRequest request, + RequestOptions options, + ActionListener listener + ) { return restHighLevelClient.performRequestAsyncAndParseEntity( - request, IndexLifecycleRequestConverters::executeSnapshotLifecyclePolicy, - options, ExecuteSnapshotLifecyclePolicyResponse::fromXContent, listener, emptySet()); + request, + IndexLifecycleRequestConverters::executeSnapshotLifecyclePolicy, + options, + ExecuteSnapshotLifecyclePolicyResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -521,10 +690,15 @@ public Cancellable executeSnapshotLifecyclePolicyAsync( * @return the response * @throws IOException in case there is a problem sending the request or parsing back the response */ - public AcknowledgedResponse executeSnapshotLifecycleRetention(ExecuteSnapshotLifecycleRetentionRequest request, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, IndexLifecycleRequestConverters::executeSnapshotLifecycleRetention, - options, AcknowledgedResponse::fromXContent, emptySet()); + public AcknowledgedResponse executeSnapshotLifecycleRetention(ExecuteSnapshotLifecycleRetentionRequest request, RequestOptions options) + throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, + IndexLifecycleRequestConverters::executeSnapshotLifecycleRetention, + options, + AcknowledgedResponse::fromXContent, + emptySet() + ); } /** @@ -540,11 +714,18 @@ public AcknowledgedResponse executeSnapshotLifecycleRetention(ExecuteSnapshotLif * @return cancellable that may be used to cancel the request */ public Cancellable executeSnapshotLifecycleRetentionAsync( - ExecuteSnapshotLifecycleRetentionRequest request, RequestOptions options, - ActionListener listener) { + ExecuteSnapshotLifecycleRetentionRequest request, + RequestOptions options, + ActionListener listener + ) { return restHighLevelClient.performRequestAsyncAndParseEntity( - request, IndexLifecycleRequestConverters::executeSnapshotLifecycleRetention, - options, AcknowledgedResponse::fromXContent, listener, emptySet()); + request, + IndexLifecycleRequestConverters::executeSnapshotLifecycleRetention, + options, + AcknowledgedResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -559,10 +740,15 @@ public Cancellable executeSnapshotLifecycleRetentionAsync( * @return the response * @throws IOException in case there is a problem sending the request or parsing back the response */ - public GetSnapshotLifecycleStatsResponse getSnapshotLifecycleStats(GetSnapshotLifecycleStatsRequest request, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, IndexLifecycleRequestConverters::getSnapshotLifecycleStats, - options, GetSnapshotLifecycleStatsResponse::fromXContent, emptySet()); + public GetSnapshotLifecycleStatsResponse getSnapshotLifecycleStats(GetSnapshotLifecycleStatsRequest request, RequestOptions options) + throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, + IndexLifecycleRequestConverters::getSnapshotLifecycleStats, + options, + GetSnapshotLifecycleStatsResponse::fromXContent, + emptySet() + ); } /** @@ -577,10 +763,19 @@ public GetSnapshotLifecycleStatsResponse getSnapshotLifecycleStats(GetSnapshotLi * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getSnapshotLifecycleStatsAsync(GetSnapshotLifecycleStatsRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, IndexLifecycleRequestConverters::getSnapshotLifecycleStats, - options, GetSnapshotLifecycleStatsResponse::fromXContent, listener, emptySet()); + public Cancellable getSnapshotLifecycleStatsAsync( + GetSnapshotLifecycleStatsRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + IndexLifecycleRequestConverters::getSnapshotLifecycleStats, + options, + GetSnapshotLifecycleStatsResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -595,8 +790,13 @@ public Cancellable getSnapshotLifecycleStatsAsync(GetSnapshotLifecycleStatsReque * @throws IOException in case there is a problem sending the request or parsing back the response */ public AcknowledgedResponse startSLM(StartSLMRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, IndexLifecycleRequestConverters::startSLM, options, - AcknowledgedResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + IndexLifecycleRequestConverters::startSLM, + options, + AcknowledgedResponse::fromXContent, + emptySet() + ); } /** @@ -610,8 +810,14 @@ public AcknowledgedResponse startSLM(StartSLMRequest request, RequestOptions opt * @return cancellable that may be used to cancel the request */ public Cancellable startSLMAsync(StartSLMRequest request, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, IndexLifecycleRequestConverters::startSLM, options, - AcknowledgedResponse::fromXContent, listener, emptySet()); + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + IndexLifecycleRequestConverters::startSLM, + options, + AcknowledgedResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -626,8 +832,13 @@ public Cancellable startSLMAsync(StartSLMRequest request, RequestOptions options * @throws IOException in case there is a problem sending the request or parsing back the response */ public AcknowledgedResponse stopSLM(StopSLMRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, IndexLifecycleRequestConverters::stopSLM, options, - AcknowledgedResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + IndexLifecycleRequestConverters::stopSLM, + options, + AcknowledgedResponse::fromXContent, + emptySet() + ); } /** @@ -642,8 +853,14 @@ public AcknowledgedResponse stopSLM(StopSLMRequest request, RequestOptions optio * @return cancellable that may be used to cancel the request */ public Cancellable stopSLMAsync(StopSLMRequest request, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, IndexLifecycleRequestConverters::stopSLM, options, - AcknowledgedResponse::fromXContent, listener, emptySet()); + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + IndexLifecycleRequestConverters::stopSLM, + options, + AcknowledgedResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -657,10 +874,15 @@ public Cancellable stopSLMAsync(StopSLMRequest request, RequestOptions options, * @return the response * @throws IOException in case there is a problem sending the request or parsing back the response */ - public LifecycleManagementStatusResponse getSLMStatus(SnapshotLifecycleManagementStatusRequest request, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, IndexLifecycleRequestConverters::snapshotLifecycleManagementStatus, - options, LifecycleManagementStatusResponse::fromXContent, emptySet()); + public LifecycleManagementStatusResponse getSLMStatus(SnapshotLifecycleManagementStatusRequest request, RequestOptions options) + throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, + IndexLifecycleRequestConverters::snapshotLifecycleManagementStatus, + options, + LifecycleManagementStatusResponse::fromXContent, + emptySet() + ); } /** @@ -674,10 +896,18 @@ public LifecycleManagementStatusResponse getSLMStatus(SnapshotLifecycleManagemen * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getSLMStatusAsync(SnapshotLifecycleManagementStatusRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, - IndexLifecycleRequestConverters::snapshotLifecycleManagementStatus, options, LifecycleManagementStatusResponse::fromXContent, - listener, emptySet()); + public Cancellable getSLMStatusAsync( + SnapshotLifecycleManagementStatusRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + IndexLifecycleRequestConverters::snapshotLifecycleManagementStatus, + options, + LifecycleManagementStatusResponse::fromXContent, + listener, + emptySet() + ); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndexLifecycleRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndexLifecycleRequestConverters.java index f37bdb6b9ddba..bdf73a0bf8c35 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndexLifecycleRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndexLifecycleRequestConverters.java @@ -40,7 +40,8 @@ private IndexLifecycleRequestConverters() {} static Request getLifecyclePolicy(GetLifecyclePolicyRequest getLifecyclePolicyRequest) { String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_ilm/policy") - .addCommaSeparatedPathParts(getLifecyclePolicyRequest.getPolicyNames()).build(); + .addCommaSeparatedPathParts(getLifecyclePolicyRequest.getPolicyNames()) + .build(); Request request = new Request(HttpGet.METHOD_NAME, endpoint); RequestConverters.Params params = new RequestConverters.Params(); params.withMasterTimeout(getLifecyclePolicyRequest.masterNodeTimeout()); @@ -50,8 +51,7 @@ static Request getLifecyclePolicy(GetLifecyclePolicyRequest getLifecyclePolicyRe } static Request putLifecyclePolicy(PutLifecyclePolicyRequest putLifecycleRequest) throws IOException { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_ilm/policy") + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_ilm/policy") .addPathPartAsIs(putLifecycleRequest.getName()) .build(); Request request = new Request(HttpPut.METHOD_NAME, endpoint); @@ -64,11 +64,12 @@ static Request putLifecyclePolicy(PutLifecyclePolicyRequest putLifecycleRequest) } static Request deleteLifecyclePolicy(DeleteLifecyclePolicyRequest deleteLifecyclePolicyRequest) { - Request request = new Request(HttpDelete.METHOD_NAME, - new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_ilm/policy") + Request request = new Request( + HttpDelete.METHOD_NAME, + new RequestConverters.EndpointBuilder().addPathPartAsIs("_ilm/policy") .addPathPartAsIs(deleteLifecyclePolicyRequest.getLifecyclePolicy()) - .build()); + .build() + ); RequestConverters.Params params = new RequestConverters.Params(); params.withMasterTimeout(deleteLifecyclePolicyRequest.masterNodeTimeout()); params.withTimeout(deleteLifecyclePolicyRequest.timeout()); @@ -77,13 +78,13 @@ static Request deleteLifecyclePolicy(DeleteLifecyclePolicyRequest deleteLifecycl } static Request removeIndexLifecyclePolicy(RemoveIndexLifecyclePolicyRequest removePolicyRequest) { - String[] indices = removePolicyRequest.indices() == null ? - Strings.EMPTY_ARRAY : removePolicyRequest.indices().toArray(new String[] {}); - Request request = new Request(HttpPost.METHOD_NAME, - new RequestConverters.EndpointBuilder() - .addCommaSeparatedPathParts(indices) - .addPathPartAsIs("_ilm", "remove") - .build()); + String[] indices = removePolicyRequest.indices() == null + ? Strings.EMPTY_ARRAY + : removePolicyRequest.indices().toArray(new String[] {}); + Request request = new Request( + HttpPost.METHOD_NAME, + new RequestConverters.EndpointBuilder().addCommaSeparatedPathParts(indices).addPathPartAsIs("_ilm", "remove").build() + ); RequestConverters.Params params = new RequestConverters.Params(); params.withIndicesOptions(removePolicyRequest.indicesOptions()); params.withMasterTimeout(removePolicyRequest.masterNodeTimeout()); @@ -92,11 +93,10 @@ static Request removeIndexLifecyclePolicy(RemoveIndexLifecyclePolicyRequest remo } static Request startILM(StartILMRequest startILMRequest) { - Request request = new Request(HttpPost.METHOD_NAME, - new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_ilm") - .addPathPartAsIs("start") - .build()); + Request request = new Request( + HttpPost.METHOD_NAME, + new RequestConverters.EndpointBuilder().addPathPartAsIs("_ilm").addPathPartAsIs("start").build() + ); RequestConverters.Params params = new RequestConverters.Params(); params.withMasterTimeout(startILMRequest.masterNodeTimeout()); params.withTimeout(startILMRequest.timeout()); @@ -105,11 +105,10 @@ static Request startILM(StartILMRequest startILMRequest) { } static Request stopILM(StopILMRequest stopILMRequest) { - Request request = new Request(HttpPost.METHOD_NAME, - new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_ilm") - .addPathPartAsIs("stop") - .build()); + Request request = new Request( + HttpPost.METHOD_NAME, + new RequestConverters.EndpointBuilder().addPathPartAsIs("_ilm").addPathPartAsIs("stop").build() + ); RequestConverters.Params params = new RequestConverters.Params(); params.withMasterTimeout(stopILMRequest.masterNodeTimeout()); params.withTimeout(stopILMRequest.timeout()); @@ -117,12 +116,11 @@ static Request stopILM(StopILMRequest stopILMRequest) { return request; } - static Request lifecycleManagementStatus(LifecycleManagementStatusRequest lifecycleManagementStatusRequest){ - Request request = new Request(HttpGet.METHOD_NAME, - new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_ilm") - .addPathPartAsIs("status") - .build()); + static Request lifecycleManagementStatus(LifecycleManagementStatusRequest lifecycleManagementStatusRequest) { + Request request = new Request( + HttpGet.METHOD_NAME, + new RequestConverters.EndpointBuilder().addPathPartAsIs("_ilm").addPathPartAsIs("status").build() + ); RequestConverters.Params params = new RequestConverters.Params(); params.withMasterTimeout(lifecycleManagementStatusRequest.masterNodeTimeout()); params.withTimeout(lifecycleManagementStatusRequest.timeout()); @@ -131,12 +129,13 @@ static Request lifecycleManagementStatus(LifecycleManagementStatusRequest lifecy } static Request explainLifecycle(ExplainLifecycleRequest explainLifecycleRequest) { - Request request = new Request(HttpGet.METHOD_NAME, - new RequestConverters.EndpointBuilder() - .addCommaSeparatedPathParts(explainLifecycleRequest.getIndices()) + Request request = new Request( + HttpGet.METHOD_NAME, + new RequestConverters.EndpointBuilder().addCommaSeparatedPathParts(explainLifecycleRequest.getIndices()) .addPathPartAsIs("_ilm") .addPathPartAsIs("explain") - .build()); + .build() + ); RequestConverters.Params params = new RequestConverters.Params(); params.withIndicesOptions(explainLifecycleRequest.indicesOptions()); params.withMasterTimeout(explainLifecycleRequest.masterNodeTimeout()); @@ -145,12 +144,13 @@ static Request explainLifecycle(ExplainLifecycleRequest explainLifecycleRequest) } static Request retryLifecycle(RetryLifecyclePolicyRequest retryLifecyclePolicyRequest) { - Request request = new Request(HttpPost.METHOD_NAME, - new RequestConverters.EndpointBuilder() - .addCommaSeparatedPathParts(retryLifecyclePolicyRequest.getIndices()) + Request request = new Request( + HttpPost.METHOD_NAME, + new RequestConverters.EndpointBuilder().addCommaSeparatedPathParts(retryLifecyclePolicyRequest.getIndices()) .addPathPartAsIs("_ilm") .addPathPartAsIs("retry") - .build()); + .build() + ); RequestConverters.Params params = new RequestConverters.Params(); params.withMasterTimeout(retryLifecyclePolicyRequest.masterNodeTimeout()); params.withTimeout(retryLifecyclePolicyRequest.timeout()); @@ -160,7 +160,8 @@ static Request retryLifecycle(RetryLifecyclePolicyRequest retryLifecyclePolicyRe static Request getSnapshotLifecyclePolicy(GetSnapshotLifecyclePolicyRequest getSnapshotLifecyclePolicyRequest) { String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_slm/policy") - .addCommaSeparatedPathParts(getSnapshotLifecyclePolicyRequest.getPolicyIds()).build(); + .addCommaSeparatedPathParts(getSnapshotLifecyclePolicyRequest.getPolicyIds()) + .build(); Request request = new Request(HttpGet.METHOD_NAME, endpoint); RequestConverters.Params params = new RequestConverters.Params(); params.withMasterTimeout(getSnapshotLifecyclePolicyRequest.masterNodeTimeout()); @@ -170,8 +171,7 @@ static Request getSnapshotLifecyclePolicy(GetSnapshotLifecyclePolicyRequest getS } static Request putSnapshotLifecyclePolicy(PutSnapshotLifecyclePolicyRequest putSnapshotLifecyclePolicyRequest) throws IOException { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_slm/policy") + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_slm/policy") .addPathPartAsIs(putSnapshotLifecyclePolicyRequest.getPolicy().getId()) .build(); Request request = new Request(HttpPut.METHOD_NAME, endpoint); @@ -184,11 +184,12 @@ static Request putSnapshotLifecyclePolicy(PutSnapshotLifecyclePolicyRequest putS } static Request deleteSnapshotLifecyclePolicy(DeleteSnapshotLifecyclePolicyRequest deleteSnapshotLifecyclePolicyRequest) { - Request request = new Request(HttpDelete.METHOD_NAME, - new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_slm/policy") + Request request = new Request( + HttpDelete.METHOD_NAME, + new RequestConverters.EndpointBuilder().addPathPartAsIs("_slm/policy") .addPathPartAsIs(deleteSnapshotLifecyclePolicyRequest.getPolicyId()) - .build()); + .build() + ); RequestConverters.Params params = new RequestConverters.Params(); params.withMasterTimeout(deleteSnapshotLifecyclePolicyRequest.masterNodeTimeout()); params.withTimeout(deleteSnapshotLifecyclePolicyRequest.timeout()); @@ -197,12 +198,13 @@ static Request deleteSnapshotLifecyclePolicy(DeleteSnapshotLifecyclePolicyReques } static Request executeSnapshotLifecyclePolicy(ExecuteSnapshotLifecyclePolicyRequest executeSnapshotLifecyclePolicyRequest) { - Request request = new Request(HttpPost.METHOD_NAME, - new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_slm/policy") + Request request = new Request( + HttpPost.METHOD_NAME, + new RequestConverters.EndpointBuilder().addPathPartAsIs("_slm/policy") .addPathPartAsIs(executeSnapshotLifecyclePolicyRequest.getPolicyId()) .addPathPartAsIs("_execute") - .build()); + .build() + ); RequestConverters.Params params = new RequestConverters.Params(); params.withMasterTimeout(executeSnapshotLifecyclePolicyRequest.masterNodeTimeout()); params.withTimeout(executeSnapshotLifecyclePolicyRequest.timeout()); @@ -211,10 +213,10 @@ static Request executeSnapshotLifecyclePolicy(ExecuteSnapshotLifecyclePolicyRequ } static Request executeSnapshotLifecycleRetention(ExecuteSnapshotLifecycleRetentionRequest executeSnapshotLifecycleRetentionRequest) { - Request request = new Request(HttpPost.METHOD_NAME, - new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_slm/_execute_retention") - .build()); + Request request = new Request( + HttpPost.METHOD_NAME, + new RequestConverters.EndpointBuilder().addPathPartAsIs("_slm/_execute_retention").build() + ); RequestConverters.Params params = new RequestConverters.Params(); params.withMasterTimeout(executeSnapshotLifecycleRetentionRequest.masterNodeTimeout()); params.withTimeout(executeSnapshotLifecycleRetentionRequest.timeout()); @@ -232,12 +234,11 @@ static Request getSnapshotLifecycleStats(GetSnapshotLifecycleStatsRequest getSna return request; } - static Request snapshotLifecycleManagementStatus(SnapshotLifecycleManagementStatusRequest snapshotLifecycleManagementStatusRequest){ - Request request = new Request(HttpGet.METHOD_NAME, - new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_slm") - .addPathPartAsIs("status") - .build()); + static Request snapshotLifecycleManagementStatus(SnapshotLifecycleManagementStatusRequest snapshotLifecycleManagementStatusRequest) { + Request request = new Request( + HttpGet.METHOD_NAME, + new RequestConverters.EndpointBuilder().addPathPartAsIs("_slm").addPathPartAsIs("status").build() + ); RequestConverters.Params params = new RequestConverters.Params(); params.withMasterTimeout(snapshotLifecycleManagementStatusRequest.masterNodeTimeout()); params.withTimeout(snapshotLifecycleManagementStatusRequest.timeout()); @@ -246,11 +247,10 @@ static Request snapshotLifecycleManagementStatus(SnapshotLifecycleManagementStat } static Request startSLM(StartSLMRequest startSLMRequest) { - Request request = new Request(HttpPost.METHOD_NAME, - new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_slm") - .addPathPartAsIs("start") - .build()); + Request request = new Request( + HttpPost.METHOD_NAME, + new RequestConverters.EndpointBuilder().addPathPartAsIs("_slm").addPathPartAsIs("start").build() + ); RequestConverters.Params params = new RequestConverters.Params(); params.withMasterTimeout(startSLMRequest.masterNodeTimeout()); params.withTimeout(startSLMRequest.timeout()); @@ -259,11 +259,10 @@ static Request startSLM(StartSLMRequest startSLMRequest) { } static Request stopSLM(StopSLMRequest stopSLMRequest) { - Request request = new Request(HttpPost.METHOD_NAME, - new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_slm") - .addPathPartAsIs("stop") - .build()); + Request request = new Request( + HttpPost.METHOD_NAME, + new RequestConverters.EndpointBuilder().addPathPartAsIs("_slm").addPathPartAsIs("stop").build() + ); RequestConverters.Params params = new RequestConverters.Params(); params.withMasterTimeout(stopSLMRequest.masterNodeTimeout()); params.withTimeout(stopSLMRequest.timeout()); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java index f56231f5a263a..afb876eda1394 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java @@ -105,8 +105,13 @@ public final class IndicesClient { * @throws IOException in case there is a problem sending the request or parsing back the response */ public AcknowledgedResponse delete(DeleteIndexRequest deleteIndexRequest, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(deleteIndexRequest, IndicesRequestConverters::deleteIndex, options, - AcknowledgedResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + deleteIndexRequest, + IndicesRequestConverters::deleteIndex, + options, + AcknowledgedResponse::fromXContent, + emptySet() + ); } /** @@ -118,11 +123,19 @@ public AcknowledgedResponse delete(DeleteIndexRequest deleteIndexRequest, Reques * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable deleteAsync(DeleteIndexRequest deleteIndexRequest, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(deleteIndexRequest, - IndicesRequestConverters::deleteIndex, options, - AcknowledgedResponse::fromXContent, listener, emptySet()); + public Cancellable deleteAsync( + DeleteIndexRequest deleteIndexRequest, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + deleteIndexRequest, + IndicesRequestConverters::deleteIndex, + options, + AcknowledgedResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -134,10 +147,14 @@ public Cancellable deleteAsync(DeleteIndexRequest deleteIndexRequest, RequestOpt * @return the response * @throws IOException in case there is a problem sending the request or parsing back the response */ - public CreateIndexResponse create(CreateIndexRequest createIndexRequest, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(createIndexRequest, IndicesRequestConverters::createIndex, options, - CreateIndexResponse::fromXContent, emptySet()); + public CreateIndexResponse create(CreateIndexRequest createIndexRequest, RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + createIndexRequest, + IndicesRequestConverters::createIndex, + options, + CreateIndexResponse::fromXContent, + emptySet() + ); } /** @@ -149,11 +166,19 @@ public CreateIndexResponse create(CreateIndexRequest createIndexRequest, * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable createAsync(CreateIndexRequest createIndexRequest, - RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(createIndexRequest, IndicesRequestConverters::createIndex, options, - CreateIndexResponse::fromXContent, listener, emptySet()); + public Cancellable createAsync( + CreateIndexRequest createIndexRequest, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + createIndexRequest, + IndicesRequestConverters::createIndex, + options, + CreateIndexResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -167,10 +192,15 @@ public Cancellable createAsync(CreateIndexRequest createIndexRequest, * @return the response * @throws IOException in case there is a problem sending the request or parsing back the response */ - public AcknowledgedResponse createDataStream(CreateDataStreamRequest createDataStreamRequest, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(createDataStreamRequest, IndicesRequestConverters::putDataStream, options, - AcknowledgedResponse::fromXContent, emptySet()); + public AcknowledgedResponse createDataStream(CreateDataStreamRequest createDataStreamRequest, RequestOptions options) + throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + createDataStreamRequest, + IndicesRequestConverters::putDataStream, + options, + AcknowledgedResponse::fromXContent, + emptySet() + ); } /** @@ -184,11 +214,19 @@ public AcknowledgedResponse createDataStream(CreateDataStreamRequest createDataS * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable createDataStreamAsync(CreateDataStreamRequest createDataStreamRequest, - RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(createDataStreamRequest, IndicesRequestConverters::putDataStream, - options, AcknowledgedResponse::fromXContent, listener, emptySet()); + public Cancellable createDataStreamAsync( + CreateDataStreamRequest createDataStreamRequest, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + createDataStreamRequest, + IndicesRequestConverters::putDataStream, + options, + AcknowledgedResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -202,10 +240,15 @@ public Cancellable createDataStreamAsync(CreateDataStreamRequest createDataStrea * @return the response * @throws IOException in case there is a problem sending the request or parsing back the response */ - public AcknowledgedResponse deleteDataStream(DeleteDataStreamRequest deleteDataStreamRequest, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(deleteDataStreamRequest, IndicesRequestConverters::deleteDataStream, - options, AcknowledgedResponse::fromXContent, emptySet()); + public AcknowledgedResponse deleteDataStream(DeleteDataStreamRequest deleteDataStreamRequest, RequestOptions options) + throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + deleteDataStreamRequest, + IndicesRequestConverters::deleteDataStream, + options, + AcknowledgedResponse::fromXContent, + emptySet() + ); } /** @@ -219,10 +262,19 @@ public AcknowledgedResponse deleteDataStream(DeleteDataStreamRequest deleteDataS * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable deleteDataStreamAsync(DeleteDataStreamRequest deleteDataStreamRequest, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(deleteDataStreamRequest, IndicesRequestConverters::deleteDataStream, - options, AcknowledgedResponse::fromXContent, listener, emptySet()); + public Cancellable deleteDataStreamAsync( + DeleteDataStreamRequest deleteDataStreamRequest, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + deleteDataStreamRequest, + IndicesRequestConverters::deleteDataStream, + options, + AcknowledgedResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -236,8 +288,13 @@ public Cancellable deleteDataStreamAsync(DeleteDataStreamRequest deleteDataStrea * @throws IOException in case there is a problem sending the request or parsing back the response */ public GetDataStreamResponse getDataStream(GetDataStreamRequest dataStreamRequest, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(dataStreamRequest, IndicesRequestConverters::getDataStreams, options, - GetDataStreamResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + dataStreamRequest, + IndicesRequestConverters::getDataStreams, + options, + GetDataStreamResponse::fromXContent, + emptySet() + ); } /** @@ -250,10 +307,19 @@ public GetDataStreamResponse getDataStream(GetDataStreamRequest dataStreamReques * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getDataStreamAsync(GetDataStreamRequest dataStreamRequest, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(dataStreamRequest, IndicesRequestConverters::getDataStreams, options, - GetDataStreamResponse::fromXContent, listener, emptySet()); + public Cancellable getDataStreamAsync( + GetDataStreamRequest dataStreamRequest, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + dataStreamRequest, + IndicesRequestConverters::getDataStreams, + options, + GetDataStreamResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -269,8 +335,13 @@ public Cancellable getDataStreamAsync(GetDataStreamRequest dataStreamRequest, Re */ public DataStreamsStatsResponse dataStreamsStats(DataStreamsStatsRequest dataStreamsStatsRequest, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(dataStreamsStatsRequest, IndicesRequestConverters::dataStreamsStats, - options, DataStreamsStatsResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + dataStreamsStatsRequest, + IndicesRequestConverters::dataStreamsStats, + options, + DataStreamsStatsResponse::fromXContent, + emptySet() + ); } /** @@ -284,10 +355,19 @@ public DataStreamsStatsResponse dataStreamsStats(DataStreamsStatsRequest dataStr * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable dataStreamsStatsAsync(DataStreamsStatsRequest dataStreamsStatsRequest, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(dataStreamsStatsRequest, IndicesRequestConverters::dataStreamsStats, - options, DataStreamsStatsResponse::fromXContent, listener, emptySet()); + public Cancellable dataStreamsStatsAsync( + DataStreamsStatsRequest dataStreamsStatsRequest, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + dataStreamsStatsRequest, + IndicesRequestConverters::dataStreamsStats, + options, + DataStreamsStatsResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -300,8 +380,13 @@ public Cancellable dataStreamsStatsAsync(DataStreamsStatsRequest dataStreamsStat * @throws IOException in case there is a problem sending the request or parsing back the response */ public AcknowledgedResponse putMapping(PutMappingRequest putMappingRequest, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(putMappingRequest, IndicesRequestConverters::putMapping, options, - AcknowledgedResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + putMappingRequest, + IndicesRequestConverters::putMapping, + options, + AcknowledgedResponse::fromXContent, + emptySet() + ); } /** @@ -313,10 +398,19 @@ public AcknowledgedResponse putMapping(PutMappingRequest putMappingRequest, Requ * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable putMappingAsync(PutMappingRequest putMappingRequest, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(putMappingRequest, IndicesRequestConverters::putMapping, options, - AcknowledgedResponse::fromXContent, listener, emptySet()); + public Cancellable putMappingAsync( + PutMappingRequest putMappingRequest, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + putMappingRequest, + IndicesRequestConverters::putMapping, + options, + AcknowledgedResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -329,11 +423,13 @@ public Cancellable putMappingAsync(PutMappingRequest putMappingRequest, RequestO * @throws IOException in case there is a problem sending the request or parsing back the response */ public GetMappingsResponse getMapping(GetMappingsRequest getMappingsRequest, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(getMappingsRequest, + return restHighLevelClient.performRequestAndParseEntity( + getMappingsRequest, IndicesRequestConverters::getMappings, options, GetMappingsResponse::fromXContent, - emptySet()); + emptySet() + ); } /** @@ -345,14 +441,19 @@ public GetMappingsResponse getMapping(GetMappingsRequest getMappingsRequest, Req * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getMappingAsync(GetMappingsRequest getMappingsRequest, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(getMappingsRequest, + public Cancellable getMappingAsync( + GetMappingsRequest getMappingsRequest, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + getMappingsRequest, IndicesRequestConverters::getMappings, options, GetMappingsResponse::fromXContent, listener, - emptySet()); + emptySet() + ); } /** @@ -364,10 +465,14 @@ public Cancellable getMappingAsync(GetMappingsRequest getMappingsRequest, Reques * @return the response * @throws IOException in case there is a problem sending the request or parsing back the response */ - public GetFieldMappingsResponse getFieldMapping(GetFieldMappingsRequest getFieldMappingsRequest, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(getFieldMappingsRequest, IndicesRequestConverters::getFieldMapping, - options, GetFieldMappingsResponse::fromXContent, emptySet() + public GetFieldMappingsResponse getFieldMapping(GetFieldMappingsRequest getFieldMappingsRequest, RequestOptions options) + throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + getFieldMappingsRequest, + IndicesRequestConverters::getFieldMapping, + options, + GetFieldMappingsResponse::fromXContent, + emptySet() ); } @@ -380,11 +485,19 @@ public GetFieldMappingsResponse getFieldMapping(GetFieldMappingsRequest getField * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getFieldMappingAsync(GetFieldMappingsRequest getFieldMappingsRequest, - RequestOptions options, ActionListener listener) { + public Cancellable getFieldMappingAsync( + GetFieldMappingsRequest getFieldMappingsRequest, + RequestOptions options, + ActionListener listener + ) { return restHighLevelClient.performRequestAsyncAndParseEntity( - getFieldMappingsRequest, IndicesRequestConverters::getFieldMapping, options, - GetFieldMappingsResponse::fromXContent, listener, emptySet()); + getFieldMappingsRequest, + IndicesRequestConverters::getFieldMapping, + options, + GetFieldMappingsResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -397,8 +510,13 @@ public Cancellable getFieldMappingAsync(GetFieldMappingsRequest getFieldMappings * @throws IOException in case there is a problem sending the request or parsing back the response */ public AcknowledgedResponse updateAliases(IndicesAliasesRequest indicesAliasesRequest, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(indicesAliasesRequest, IndicesRequestConverters::updateAliases, options, - AcknowledgedResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + indicesAliasesRequest, + IndicesRequestConverters::updateAliases, + options, + AcknowledgedResponse::fromXContent, + emptySet() + ); } /** @@ -410,11 +528,19 @@ public AcknowledgedResponse updateAliases(IndicesAliasesRequest indicesAliasesRe * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable updateAliasesAsync(IndicesAliasesRequest indicesAliasesRequest, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(indicesAliasesRequest, - IndicesRequestConverters::updateAliases, options, - AcknowledgedResponse::fromXContent, listener, emptySet()); + public Cancellable updateAliasesAsync( + IndicesAliasesRequest indicesAliasesRequest, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + indicesAliasesRequest, + IndicesRequestConverters::updateAliases, + options, + AcknowledgedResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -427,8 +553,13 @@ public Cancellable updateAliasesAsync(IndicesAliasesRequest indicesAliasesReques * @throws IOException in case there is a problem sending the request or parsing back the response */ public OpenIndexResponse open(OpenIndexRequest openIndexRequest, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(openIndexRequest, IndicesRequestConverters::openIndex, options, - OpenIndexResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + openIndexRequest, + IndicesRequestConverters::openIndex, + options, + OpenIndexResponse::fromXContent, + emptySet() + ); } /** @@ -441,8 +572,14 @@ public OpenIndexResponse open(OpenIndexRequest openIndexRequest, RequestOptions * @return cancellable that may be used to cancel the request */ public Cancellable openAsync(OpenIndexRequest openIndexRequest, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(openIndexRequest, IndicesRequestConverters::openIndex, options, - OpenIndexResponse::fromXContent, listener, emptySet()); + return restHighLevelClient.performRequestAsyncAndParseEntity( + openIndexRequest, + IndicesRequestConverters::openIndex, + options, + OpenIndexResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -455,8 +592,13 @@ public Cancellable openAsync(OpenIndexRequest openIndexRequest, RequestOptions o * @throws IOException in case there is a problem sending the request or parsing back the response */ public CloseIndexResponse close(CloseIndexRequest closeIndexRequest, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(closeIndexRequest, IndicesRequestConverters::closeIndex, options, - CloseIndexResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + closeIndexRequest, + IndicesRequestConverters::closeIndex, + options, + CloseIndexResponse::fromXContent, + emptySet() + ); } /** @@ -468,14 +610,21 @@ public CloseIndexResponse close(CloseIndexRequest closeIndexRequest, RequestOpti * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable closeAsync(CloseIndexRequest closeIndexRequest, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(closeIndexRequest, - IndicesRequestConverters::closeIndex, options, - CloseIndexResponse::fromXContent, listener, emptySet()); + public Cancellable closeAsync( + CloseIndexRequest closeIndexRequest, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + closeIndexRequest, + IndicesRequestConverters::closeIndex, + options, + CloseIndexResponse::fromXContent, + listener, + emptySet() + ); } - /** * Checks if one or more aliases exist using the Aliases Exist API. * See @@ -486,8 +635,13 @@ public Cancellable closeAsync(CloseIndexRequest closeIndexRequest, RequestOption * @throws IOException in case there is a problem sending the request */ public boolean existsAlias(GetAliasesRequest getAliasesRequest, RequestOptions options) throws IOException { - return restHighLevelClient.performRequest(getAliasesRequest, IndicesRequestConverters::existsAlias, options, - RestHighLevelClient::convertExistsResponse, emptySet()); + return restHighLevelClient.performRequest( + getAliasesRequest, + IndicesRequestConverters::existsAlias, + options, + RestHighLevelClient::convertExistsResponse, + emptySet() + ); } /** @@ -500,8 +654,14 @@ public boolean existsAlias(GetAliasesRequest getAliasesRequest, RequestOptions o * @return cancellable that may be used to cancel the request */ public Cancellable existsAliasAsync(GetAliasesRequest getAliasesRequest, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsync(getAliasesRequest, IndicesRequestConverters::existsAlias, options, - RestHighLevelClient::convertExistsResponse, listener, emptySet()); + return restHighLevelClient.performRequestAsync( + getAliasesRequest, + IndicesRequestConverters::existsAlias, + options, + RestHighLevelClient::convertExistsResponse, + listener, + emptySet() + ); } /** @@ -513,8 +673,13 @@ public Cancellable existsAliasAsync(GetAliasesRequest getAliasesRequest, Request * @throws IOException in case there is a problem sending the request or parsing back the response */ public RefreshResponse refresh(RefreshRequest refreshRequest, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(refreshRequest, IndicesRequestConverters::refresh, options, - RefreshResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + refreshRequest, + IndicesRequestConverters::refresh, + options, + RefreshResponse::fromXContent, + emptySet() + ); } /** @@ -526,8 +691,14 @@ public RefreshResponse refresh(RefreshRequest refreshRequest, RequestOptions opt * @return cancellable that may be used to cancel the request */ public Cancellable refreshAsync(RefreshRequest refreshRequest, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(refreshRequest, IndicesRequestConverters::refresh, options, - RefreshResponse::fromXContent, listener, emptySet()); + return restHighLevelClient.performRequestAsyncAndParseEntity( + refreshRequest, + IndicesRequestConverters::refresh, + options, + RefreshResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -539,8 +710,13 @@ public Cancellable refreshAsync(RefreshRequest refreshRequest, RequestOptions op * @throws IOException in case there is a problem sending the request or parsing back the response */ public FlushResponse flush(FlushRequest flushRequest, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(flushRequest, IndicesRequestConverters::flush, options, - FlushResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + flushRequest, + IndicesRequestConverters::flush, + options, + FlushResponse::fromXContent, + emptySet() + ); } /** @@ -552,8 +728,14 @@ public FlushResponse flush(FlushRequest flushRequest, RequestOptions options) th * @return cancellable that may be used to cancel the request */ public Cancellable flushAsync(FlushRequest flushRequest, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(flushRequest, IndicesRequestConverters::flush, options, - FlushResponse::fromXContent, listener, emptySet()); + return restHighLevelClient.performRequestAsyncAndParseEntity( + flushRequest, + IndicesRequestConverters::flush, + options, + FlushResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -566,8 +748,13 @@ public Cancellable flushAsync(FlushRequest flushRequest, RequestOptions options, * @throws IOException in case there is a problem sending the request or parsing back the response */ public GetSettingsResponse getSettings(GetSettingsRequest getSettingsRequest, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(getSettingsRequest, IndicesRequestConverters::getSettings, options, - GetSettingsResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + getSettingsRequest, + IndicesRequestConverters::getSettings, + options, + GetSettingsResponse::fromXContent, + emptySet() + ); } /** @@ -579,10 +766,19 @@ public GetSettingsResponse getSettings(GetSettingsRequest getSettingsRequest, Re * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getSettingsAsync(GetSettingsRequest getSettingsRequest, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(getSettingsRequest, IndicesRequestConverters::getSettings, options, - GetSettingsResponse::fromXContent, listener, emptySet()); + public Cancellable getSettingsAsync( + GetSettingsRequest getSettingsRequest, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + getSettingsRequest, + IndicesRequestConverters::getSettings, + options, + GetSettingsResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -595,8 +791,13 @@ public Cancellable getSettingsAsync(GetSettingsRequest getSettingsRequest, Reque * @throws IOException in case there is a problem sending the request or parsing back the response */ public GetIndexResponse get(GetIndexRequest getIndexRequest, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(getIndexRequest, IndicesRequestConverters::getIndex, options, - GetIndexResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + getIndexRequest, + IndicesRequestConverters::getIndex, + options, + GetIndexResponse::fromXContent, + emptySet() + ); } /** @@ -608,10 +809,15 @@ public GetIndexResponse get(GetIndexRequest getIndexRequest, RequestOptions opti * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getAsync(GetIndexRequest getIndexRequest, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(getIndexRequest, IndicesRequestConverters::getIndex, options, - GetIndexResponse::fromXContent, listener, emptySet()); + public Cancellable getAsync(GetIndexRequest getIndexRequest, RequestOptions options, ActionListener listener) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + getIndexRequest, + IndicesRequestConverters::getIndex, + options, + GetIndexResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -639,8 +845,13 @@ public ForceMergeResponse forceMerge(ForceMergeRequest forceMergeRequest, Reques * @throws IOException in case there is a problem sending the request or parsing back the response */ public ForceMergeResponse forcemerge(ForceMergeRequest forceMergeRequest, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(forceMergeRequest, IndicesRequestConverters::forceMerge, options, - ForceMergeResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + forceMergeRequest, + IndicesRequestConverters::forceMerge, + options, + ForceMergeResponse::fromXContent, + emptySet() + ); } /** @@ -654,8 +865,11 @@ public ForceMergeResponse forcemerge(ForceMergeRequest forceMergeRequest, Reques * @return cancellable that may be used to cancel the request */ @Deprecated - public Cancellable forceMergeAsync(ForceMergeRequest forceMergeRequest, RequestOptions options, - ActionListener listener) { + public Cancellable forceMergeAsync( + ForceMergeRequest forceMergeRequest, + RequestOptions options, + ActionListener listener + ) { return forcemergeAsync(forceMergeRequest, options, listener); } @@ -668,11 +882,19 @@ public Cancellable forceMergeAsync(ForceMergeRequest forceMergeRequest, RequestO * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable forcemergeAsync(ForceMergeRequest forceMergeRequest, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(forceMergeRequest, - IndicesRequestConverters::forceMerge, options, - ForceMergeResponse::fromXContent, listener, emptySet()); + public Cancellable forcemergeAsync( + ForceMergeRequest forceMergeRequest, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + forceMergeRequest, + IndicesRequestConverters::forceMerge, + options, + ForceMergeResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -684,10 +906,15 @@ public Cancellable forcemergeAsync(ForceMergeRequest forceMergeRequest, RequestO * @return the response * @throws IOException in case there is a problem sending the request or parsing back the response */ - public ClearIndicesCacheResponse clearCache(ClearIndicesCacheRequest clearIndicesCacheRequest, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(clearIndicesCacheRequest, IndicesRequestConverters::clearCache, options, - ClearIndicesCacheResponse::fromXContent, emptySet()); + public ClearIndicesCacheResponse clearCache(ClearIndicesCacheRequest clearIndicesCacheRequest, RequestOptions options) + throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + clearIndicesCacheRequest, + IndicesRequestConverters::clearCache, + options, + ClearIndicesCacheResponse::fromXContent, + emptySet() + ); } /** @@ -699,11 +926,19 @@ public ClearIndicesCacheResponse clearCache(ClearIndicesCacheRequest clearIndice * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable clearCacheAsync(ClearIndicesCacheRequest clearIndicesCacheRequest, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(clearIndicesCacheRequest, - IndicesRequestConverters::clearCache, options, - ClearIndicesCacheResponse::fromXContent, listener, emptySet()); + public Cancellable clearCacheAsync( + ClearIndicesCacheRequest clearIndicesCacheRequest, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + clearIndicesCacheRequest, + IndicesRequestConverters::clearCache, + options, + ClearIndicesCacheResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -736,12 +971,12 @@ public boolean exists(GetIndexRequest request, RequestOptions options) throws IO */ public Cancellable existsAsync(GetIndexRequest request, RequestOptions options, ActionListener listener) { return restHighLevelClient.performRequestAsync( - request, - IndicesRequestConverters::indicesExist, - options, - RestHighLevelClient::convertExistsResponse, - listener, - Collections.emptySet() + request, + IndicesRequestConverters::indicesExist, + options, + RestHighLevelClient::convertExistsResponse, + listener, + Collections.emptySet() ); } @@ -755,8 +990,13 @@ public Cancellable existsAsync(GetIndexRequest request, RequestOptions options, * @throws IOException in case there is a problem sending the request or parsing back the response */ public ResizeResponse shrink(ResizeRequest resizeRequest, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(resizeRequest, IndicesRequestConverters::shrink, options, - ResizeResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + resizeRequest, + IndicesRequestConverters::shrink, + options, + ResizeResponse::fromXContent, + emptySet() + ); } /** @@ -771,9 +1011,16 @@ public ResizeResponse shrink(ResizeRequest resizeRequest, RequestOptions options */ @Deprecated public org.elasticsearch.action.admin.indices.shrink.ResizeResponse shrink( - org.elasticsearch.action.admin.indices.shrink.ResizeRequest resizeRequest, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(resizeRequest, IndicesRequestConverters::shrink, options, - org.elasticsearch.action.admin.indices.shrink.ResizeResponse::fromXContent, emptySet()); + org.elasticsearch.action.admin.indices.shrink.ResizeRequest resizeRequest, + RequestOptions options + ) throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + resizeRequest, + IndicesRequestConverters::shrink, + options, + org.elasticsearch.action.admin.indices.shrink.ResizeResponse::fromXContent, + emptySet() + ); } /** @@ -786,8 +1033,14 @@ public org.elasticsearch.action.admin.indices.shrink.ResizeResponse shrink( * @return cancellable that may be used to cancel the request */ public Cancellable shrinkAsync(ResizeRequest resizeRequest, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(resizeRequest, IndicesRequestConverters::shrink, options, - ResizeResponse::fromXContent, listener, emptySet()); + return restHighLevelClient.performRequestAsyncAndParseEntity( + resizeRequest, + IndicesRequestConverters::shrink, + options, + ResizeResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -801,10 +1054,19 @@ public Cancellable shrinkAsync(ResizeRequest resizeRequest, RequestOptions optio * @deprecated use {@link #shrinkAsync(ResizeRequest, RequestOptions, ActionListener)} */ @Deprecated - public Cancellable shrinkAsync(org.elasticsearch.action.admin.indices.shrink.ResizeRequest resizeRequest, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(resizeRequest, IndicesRequestConverters::shrink, options, - org.elasticsearch.action.admin.indices.shrink.ResizeResponse::fromXContent, listener, emptySet()); + public Cancellable shrinkAsync( + org.elasticsearch.action.admin.indices.shrink.ResizeRequest resizeRequest, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + resizeRequest, + IndicesRequestConverters::shrink, + options, + org.elasticsearch.action.admin.indices.shrink.ResizeResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -817,8 +1079,13 @@ public Cancellable shrinkAsync(org.elasticsearch.action.admin.indices.shrink.Res * @throws IOException in case there is a problem sending the request or parsing back the response */ public ResizeResponse split(ResizeRequest resizeRequest, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(resizeRequest, IndicesRequestConverters::split, options, - ResizeResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + resizeRequest, + IndicesRequestConverters::split, + options, + ResizeResponse::fromXContent, + emptySet() + ); } /** @@ -833,9 +1100,16 @@ public ResizeResponse split(ResizeRequest resizeRequest, RequestOptions options) */ @Deprecated public org.elasticsearch.action.admin.indices.shrink.ResizeResponse split( - org.elasticsearch.action.admin.indices.shrink.ResizeRequest resizeRequest, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(resizeRequest, IndicesRequestConverters::split, options, - org.elasticsearch.action.admin.indices.shrink.ResizeResponse::fromXContent, emptySet()); + org.elasticsearch.action.admin.indices.shrink.ResizeRequest resizeRequest, + RequestOptions options + ) throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + resizeRequest, + IndicesRequestConverters::split, + options, + org.elasticsearch.action.admin.indices.shrink.ResizeResponse::fromXContent, + emptySet() + ); } /** @@ -848,8 +1122,14 @@ public org.elasticsearch.action.admin.indices.shrink.ResizeResponse split( * @return cancellable that may be used to cancel the request */ public Cancellable splitAsync(ResizeRequest resizeRequest, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(resizeRequest, IndicesRequestConverters::split, options, - ResizeResponse::fromXContent, listener, emptySet()); + return restHighLevelClient.performRequestAsyncAndParseEntity( + resizeRequest, + IndicesRequestConverters::split, + options, + ResizeResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -863,10 +1143,19 @@ public Cancellable splitAsync(ResizeRequest resizeRequest, RequestOptions option * @deprecated use {@link #splitAsync(ResizeRequest, RequestOptions, ActionListener)} */ @Deprecated - public Cancellable splitAsync(org.elasticsearch.action.admin.indices.shrink.ResizeRequest resizeRequest, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(resizeRequest, IndicesRequestConverters::split, options, - org.elasticsearch.action.admin.indices.shrink.ResizeResponse::fromXContent, listener, emptySet()); + public Cancellable splitAsync( + org.elasticsearch.action.admin.indices.shrink.ResizeRequest resizeRequest, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + resizeRequest, + IndicesRequestConverters::split, + options, + org.elasticsearch.action.admin.indices.shrink.ResizeResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -879,8 +1168,13 @@ public Cancellable splitAsync(org.elasticsearch.action.admin.indices.shrink.Resi * @throws IOException in case there is a problem sending the request or parsing back the response */ public ResizeResponse clone(ResizeRequest resizeRequest, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(resizeRequest, IndicesRequestConverters::clone, options, - ResizeResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + resizeRequest, + IndicesRequestConverters::clone, + options, + ResizeResponse::fromXContent, + emptySet() + ); } /** @@ -895,9 +1189,16 @@ public ResizeResponse clone(ResizeRequest resizeRequest, RequestOptions options) */ @Deprecated public org.elasticsearch.action.admin.indices.shrink.ResizeResponse clone( - org.elasticsearch.action.admin.indices.shrink.ResizeRequest resizeRequest, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(resizeRequest, IndicesRequestConverters::clone, options, - org.elasticsearch.action.admin.indices.shrink.ResizeResponse::fromXContent, emptySet()); + org.elasticsearch.action.admin.indices.shrink.ResizeRequest resizeRequest, + RequestOptions options + ) throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + resizeRequest, + IndicesRequestConverters::clone, + options, + org.elasticsearch.action.admin.indices.shrink.ResizeResponse::fromXContent, + emptySet() + ); } /** @@ -910,8 +1211,14 @@ public org.elasticsearch.action.admin.indices.shrink.ResizeResponse clone( * @return cancellable that may be used to cancel the request */ public Cancellable cloneAsync(ResizeRequest resizeRequest, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(resizeRequest, IndicesRequestConverters::clone, options, - ResizeResponse::fromXContent, listener, emptySet()); + return restHighLevelClient.performRequestAsyncAndParseEntity( + resizeRequest, + IndicesRequestConverters::clone, + options, + ResizeResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -925,10 +1232,19 @@ public Cancellable cloneAsync(ResizeRequest resizeRequest, RequestOptions option * @deprecated use {@link #cloneAsync(ResizeRequest, RequestOptions, ActionListener)} */ @Deprecated - public Cancellable cloneAsync(org.elasticsearch.action.admin.indices.shrink.ResizeRequest resizeRequest, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(resizeRequest, IndicesRequestConverters::clone, options, - org.elasticsearch.action.admin.indices.shrink.ResizeResponse::fromXContent, listener, emptySet()); + public Cancellable cloneAsync( + org.elasticsearch.action.admin.indices.shrink.ResizeRequest resizeRequest, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + resizeRequest, + IndicesRequestConverters::clone, + options, + org.elasticsearch.action.admin.indices.shrink.ResizeResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -941,8 +1257,13 @@ public Cancellable cloneAsync(org.elasticsearch.action.admin.indices.shrink.Resi * @throws IOException in case there is a problem sending the request or parsing back the response */ public RolloverResponse rollover(RolloverRequest rolloverRequest, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(rolloverRequest, IndicesRequestConverters::rollover, options, - RolloverResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + rolloverRequest, + IndicesRequestConverters::rollover, + options, + RolloverResponse::fromXContent, + emptySet() + ); } /** @@ -955,8 +1276,14 @@ public RolloverResponse rollover(RolloverRequest rolloverRequest, RequestOptions * @return cancellable that may be used to cancel the request */ public Cancellable rolloverAsync(RolloverRequest rolloverRequest, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(rolloverRequest, IndicesRequestConverters::rollover, options, - RolloverResponse::fromXContent, listener, emptySet()); + return restHighLevelClient.performRequestAsyncAndParseEntity( + rolloverRequest, + IndicesRequestConverters::rollover, + options, + RolloverResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -969,8 +1296,13 @@ public Cancellable rolloverAsync(RolloverRequest rolloverRequest, RequestOptions * @throws IOException in case there is a problem sending the request or parsing back the response */ public GetAliasesResponse getAlias(GetAliasesRequest getAliasesRequest, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(getAliasesRequest, IndicesRequestConverters::getAlias, options, - GetAliasesResponse::fromXContent, singleton(RestStatus.NOT_FOUND.getStatus())); + return restHighLevelClient.performRequestAndParseEntity( + getAliasesRequest, + IndicesRequestConverters::getAlias, + options, + GetAliasesResponse::fromXContent, + singleton(RestStatus.NOT_FOUND.getStatus()) + ); } /** @@ -982,11 +1314,19 @@ public GetAliasesResponse getAlias(GetAliasesRequest getAliasesRequest, RequestO * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getAliasAsync(GetAliasesRequest getAliasesRequest, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(getAliasesRequest, - IndicesRequestConverters::getAlias, options, - GetAliasesResponse::fromXContent, listener, singleton(RestStatus.NOT_FOUND.getStatus())); + public Cancellable getAliasAsync( + GetAliasesRequest getAliasesRequest, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + getAliasesRequest, + IndicesRequestConverters::getAlias, + options, + GetAliasesResponse::fromXContent, + listener, + singleton(RestStatus.NOT_FOUND.getStatus()) + ); } /** @@ -999,8 +1339,13 @@ public Cancellable getAliasAsync(GetAliasesRequest getAliasesRequest, RequestOpt * @throws IOException in case there is a problem sending the request or parsing back the response */ public AcknowledgedResponse putSettings(UpdateSettingsRequest updateSettingsRequest, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(updateSettingsRequest, IndicesRequestConverters::indexPutSettings, options, - AcknowledgedResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + updateSettingsRequest, + IndicesRequestConverters::indexPutSettings, + options, + AcknowledgedResponse::fromXContent, + emptySet() + ); } /** @@ -1012,11 +1357,19 @@ public AcknowledgedResponse putSettings(UpdateSettingsRequest updateSettingsRequ * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable putSettingsAsync(UpdateSettingsRequest updateSettingsRequest, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(updateSettingsRequest, - IndicesRequestConverters::indexPutSettings, options, - AcknowledgedResponse::fromXContent, listener, emptySet()); + public Cancellable putSettingsAsync( + UpdateSettingsRequest updateSettingsRequest, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + updateSettingsRequest, + IndicesRequestConverters::indexPutSettings, + options, + AcknowledgedResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -1028,11 +1381,14 @@ public Cancellable putSettingsAsync(UpdateSettingsRequest updateSettingsRequest, * @return the response * @throws IOException in case there is a problem sending the request or parsing back the response */ - public AcknowledgedResponse putTemplate( - PutIndexTemplateRequest putIndexTemplateRequest, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(putIndexTemplateRequest, IndicesRequestConverters::putTemplate, options, - AcknowledgedResponse::fromXContent, emptySet()); + public AcknowledgedResponse putTemplate(PutIndexTemplateRequest putIndexTemplateRequest, RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + putIndexTemplateRequest, + IndicesRequestConverters::putTemplate, + options, + AcknowledgedResponse::fromXContent, + emptySet() + ); } /** @@ -1044,11 +1400,19 @@ public AcknowledgedResponse putTemplate( * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable putTemplateAsync(PutIndexTemplateRequest putIndexTemplateRequest, - RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(putIndexTemplateRequest, - IndicesRequestConverters::putTemplate, options, - AcknowledgedResponse::fromXContent, listener, emptySet()); + public Cancellable putTemplateAsync( + PutIndexTemplateRequest putIndexTemplateRequest, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + putIndexTemplateRequest, + IndicesRequestConverters::putTemplate, + options, + AcknowledgedResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -1062,8 +1426,13 @@ public Cancellable putTemplateAsync(PutIndexTemplateRequest putIndexTemplateRequ */ public AcknowledgedResponse putIndexTemplate(PutComposableIndexTemplateRequest putIndexTemplateRequest, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(putIndexTemplateRequest, IndicesRequestConverters::putIndexTemplate, - options, AcknowledgedResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + putIndexTemplateRequest, + IndicesRequestConverters::putIndexTemplate, + options, + AcknowledgedResponse::fromXContent, + emptySet() + ); } /** @@ -1075,10 +1444,19 @@ public AcknowledgedResponse putIndexTemplate(PutComposableIndexTemplateRequest p * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable putIndexTemplateAsync(PutComposableIndexTemplateRequest putIndexTemplateRequest, - RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(putIndexTemplateRequest, IndicesRequestConverters::putIndexTemplate, - options, AcknowledgedResponse::fromXContent, listener, emptySet()); + public Cancellable putIndexTemplateAsync( + PutComposableIndexTemplateRequest putIndexTemplateRequest, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + putIndexTemplateRequest, + IndicesRequestConverters::putIndexTemplate, + options, + AcknowledgedResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -1092,10 +1470,17 @@ public Cancellable putIndexTemplateAsync(PutComposableIndexTemplateRequest putIn * @return the response * @throws IOException in case there is a problem sending the request or parsing back the response */ - public SimulateIndexTemplateResponse simulateIndexTemplate(SimulateIndexTemplateRequest simulateIndexTemplateRequest, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(simulateIndexTemplateRequest, - IndicesRequestConverters::simulateIndexTemplate, options, SimulateIndexTemplateResponse::fromXContent, emptySet()); + public SimulateIndexTemplateResponse simulateIndexTemplate( + SimulateIndexTemplateRequest simulateIndexTemplateRequest, + RequestOptions options + ) throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + simulateIndexTemplateRequest, + IndicesRequestConverters::simulateIndexTemplate, + options, + SimulateIndexTemplateResponse::fromXContent, + emptySet() + ); } /** @@ -1109,10 +1494,19 @@ public SimulateIndexTemplateResponse simulateIndexTemplate(SimulateIndexTemplate * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable simulateIndexTemplateAsync(SimulateIndexTemplateRequest simulateIndexTemplateRequest, - RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(simulateIndexTemplateRequest, - IndicesRequestConverters::simulateIndexTemplate, options, SimulateIndexTemplateResponse::fromXContent, listener, emptySet()); + public Cancellable simulateIndexTemplateAsync( + SimulateIndexTemplateRequest simulateIndexTemplateRequest, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + simulateIndexTemplateRequest, + IndicesRequestConverters::simulateIndexTemplate, + options, + SimulateIndexTemplateResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -1125,11 +1519,14 @@ public Cancellable simulateIndexTemplateAsync(SimulateIndexTemplateRequest simul * @return the response * @throws IOException in case there is a problem sending the request or parsing back the response */ - public ValidateQueryResponse validateQuery(ValidateQueryRequest validateQueryRequest, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(validateQueryRequest, - IndicesRequestConverters::validateQuery, options, - ValidateQueryResponse::fromXContent, emptySet()); + public ValidateQueryResponse validateQuery(ValidateQueryRequest validateQueryRequest, RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + validateQueryRequest, + IndicesRequestConverters::validateQuery, + options, + ValidateQueryResponse::fromXContent, + emptySet() + ); } /** @@ -1142,11 +1539,19 @@ public ValidateQueryResponse validateQuery(ValidateQueryRequest validateQueryReq * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable validateQueryAsync(ValidateQueryRequest validateQueryRequest, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(validateQueryRequest, - IndicesRequestConverters::validateQuery, options, - ValidateQueryResponse::fromXContent, listener, emptySet()); + public Cancellable validateQueryAsync( + ValidateQueryRequest validateQueryRequest, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + validateQueryRequest, + IndicesRequestConverters::validateQuery, + options, + ValidateQueryResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -1158,10 +1563,17 @@ public Cancellable validateQueryAsync(ValidateQueryRequest validateQueryRequest, * @return the response * @throws IOException in case there is a problem sending the request or parsing back the response */ - public GetComposableIndexTemplatesResponse getIndexTemplate(GetComposableIndexTemplateRequest getIndexTemplatesRequest, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(getIndexTemplatesRequest, IndicesRequestConverters::getIndexTemplates, - options, GetComposableIndexTemplatesResponse::fromXContent, emptySet()); + public GetComposableIndexTemplatesResponse getIndexTemplate( + GetComposableIndexTemplateRequest getIndexTemplatesRequest, + RequestOptions options + ) throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + getIndexTemplatesRequest, + IndicesRequestConverters::getIndexTemplates, + options, + GetComposableIndexTemplatesResponse::fromXContent, + emptySet() + ); } /** @@ -1173,10 +1585,19 @@ public GetComposableIndexTemplatesResponse getIndexTemplate(GetComposableIndexTe * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getIndexTemplateAsync(GetComposableIndexTemplateRequest getIndexTemplatesRequest, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(getIndexTemplatesRequest, - IndicesRequestConverters::getIndexTemplates, options, GetComposableIndexTemplatesResponse::fromXContent, listener, emptySet()); + public Cancellable getIndexTemplateAsync( + GetComposableIndexTemplateRequest getIndexTemplatesRequest, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + getIndexTemplatesRequest, + IndicesRequestConverters::getIndexTemplates, + options, + GetComposableIndexTemplatesResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -1188,11 +1609,15 @@ public Cancellable getIndexTemplateAsync(GetComposableIndexTemplateRequest getIn * @return the response * @throws IOException in case there is a problem sending the request or parsing back the response */ - public GetIndexTemplatesResponse getIndexTemplate(GetIndexTemplatesRequest getIndexTemplatesRequest, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(getIndexTemplatesRequest, + public GetIndexTemplatesResponse getIndexTemplate(GetIndexTemplatesRequest getIndexTemplatesRequest, RequestOptions options) + throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + getIndexTemplatesRequest, IndicesRequestConverters::getTemplates, - options, GetIndexTemplatesResponse::fromXContent, emptySet()); + options, + GetIndexTemplatesResponse::fromXContent, + emptySet() + ); } /** @@ -1204,11 +1629,19 @@ public GetIndexTemplatesResponse getIndexTemplate(GetIndexTemplatesRequest getIn * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getIndexTemplateAsync(GetIndexTemplatesRequest getIndexTemplatesRequest, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(getIndexTemplatesRequest, + public Cancellable getIndexTemplateAsync( + GetIndexTemplatesRequest getIndexTemplatesRequest, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + getIndexTemplatesRequest, IndicesRequestConverters::getTemplates, - options, GetIndexTemplatesResponse::fromXContent, listener, emptySet()); + options, + GetIndexTemplatesResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -1219,11 +1652,14 @@ public Cancellable getIndexTemplateAsync(GetIndexTemplatesRequest getIndexTempla * @return true if any index templates in the request exist, false otherwise * @throws IOException in case there is a problem sending the request or parsing back the response */ - public boolean existsTemplate(IndexTemplatesExistRequest indexTemplatesRequest, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequest(indexTemplatesRequest, - IndicesRequestConverters::templatesExist, options, - RestHighLevelClient::convertExistsResponse, emptySet()); + public boolean existsTemplate(IndexTemplatesExistRequest indexTemplatesRequest, RequestOptions options) throws IOException { + return restHighLevelClient.performRequest( + indexTemplatesRequest, + IndicesRequestConverters::templatesExist, + options, + RestHighLevelClient::convertExistsResponse, + emptySet() + ); } /** @@ -1233,13 +1669,20 @@ public boolean existsTemplate(IndexTemplatesExistRequest indexTemplatesRequest, * @param listener the listener to be notified upon request completion. The listener will be called with the value {@code true} * @return cancellable that may be used to cancel the request */ - public Cancellable existsTemplateAsync(IndexTemplatesExistRequest indexTemplatesExistRequest, - RequestOptions options, - ActionListener listener) { + public Cancellable existsTemplateAsync( + IndexTemplatesExistRequest indexTemplatesExistRequest, + RequestOptions options, + ActionListener listener + ) { - return restHighLevelClient.performRequestAsync(indexTemplatesExistRequest, - IndicesRequestConverters::templatesExist, options, - RestHighLevelClient::convertExistsResponse, listener, emptySet()); + return restHighLevelClient.performRequestAsync( + indexTemplatesExistRequest, + IndicesRequestConverters::templatesExist, + options, + RestHighLevelClient::convertExistsResponse, + listener, + emptySet() + ); } /** @@ -1250,11 +1693,15 @@ public Cancellable existsTemplateAsync(IndexTemplatesExistRequest indexTemplates * @return true if any index templates in the request exist, false otherwise * @throws IOException in case there is a problem sending the request or parsing back the response */ - public boolean existsIndexTemplate(ComposableIndexTemplateExistRequest indexTemplatesRequest, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequest(indexTemplatesRequest, - IndicesRequestConverters::templatesExist, options, - RestHighLevelClient::convertExistsResponse, emptySet()); + public boolean existsIndexTemplate(ComposableIndexTemplateExistRequest indexTemplatesRequest, RequestOptions options) + throws IOException { + return restHighLevelClient.performRequest( + indexTemplatesRequest, + IndicesRequestConverters::templatesExist, + options, + RestHighLevelClient::convertExistsResponse, + emptySet() + ); } /** @@ -1264,13 +1711,20 @@ public boolean existsIndexTemplate(ComposableIndexTemplateExistRequest indexTemp * @param listener the listener to be notified upon request completion. The listener will be called with the value {@code true} * @return cancellable that may be used to cancel the request */ - public Cancellable existsIndexTemplateAsync(ComposableIndexTemplateExistRequest indexTemplatesExistRequest, - RequestOptions options, - ActionListener listener) { + public Cancellable existsIndexTemplateAsync( + ComposableIndexTemplateExistRequest indexTemplatesExistRequest, + RequestOptions options, + ActionListener listener + ) { - return restHighLevelClient.performRequestAsync(indexTemplatesExistRequest, - IndicesRequestConverters::templatesExist, options, - RestHighLevelClient::convertExistsResponse, listener, emptySet()); + return restHighLevelClient.performRequestAsync( + indexTemplatesExistRequest, + IndicesRequestConverters::templatesExist, + options, + RestHighLevelClient::convertExistsResponse, + listener, + emptySet() + ); } /** @@ -1282,8 +1736,13 @@ public Cancellable existsIndexTemplateAsync(ComposableIndexTemplateExistRequest * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized */ public AnalyzeResponse analyze(AnalyzeRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, IndicesRequestConverters::analyze, options, - AnalyzeResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + IndicesRequestConverters::analyze, + options, + AnalyzeResponse::fromXContent, + emptySet() + ); } /** @@ -1295,10 +1754,15 @@ public AnalyzeResponse analyze(AnalyzeRequest request, RequestOptions options) t * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable analyzeAsync(AnalyzeRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, IndicesRequestConverters::analyze, options, - AnalyzeResponse::fromXContent, listener, emptySet()); + public Cancellable analyzeAsync(AnalyzeRequest request, RequestOptions options, ActionListener listener) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + IndicesRequestConverters::analyze, + options, + AnalyzeResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -1308,8 +1772,13 @@ public Cancellable analyzeAsync(AnalyzeRequest request, RequestOptions options, * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized */ public ShardsAcknowledgedResponse freeze(FreezeIndexRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, IndicesRequestConverters::freezeIndex, options, - ShardsAcknowledgedResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + IndicesRequestConverters::freezeIndex, + options, + ShardsAcknowledgedResponse::fromXContent, + emptySet() + ); } /** @@ -1319,10 +1788,19 @@ public ShardsAcknowledgedResponse freeze(FreezeIndexRequest request, RequestOpti * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable freezeAsync(FreezeIndexRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, IndicesRequestConverters::freezeIndex, options, - ShardsAcknowledgedResponse::fromXContent, listener, emptySet()); + public Cancellable freezeAsync( + FreezeIndexRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + IndicesRequestConverters::freezeIndex, + options, + ShardsAcknowledgedResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -1332,8 +1810,13 @@ public Cancellable freezeAsync(FreezeIndexRequest request, RequestOptions option * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized */ public ShardsAcknowledgedResponse unfreeze(UnfreezeIndexRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, IndicesRequestConverters::unfreezeIndex, options, - ShardsAcknowledgedResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + IndicesRequestConverters::unfreezeIndex, + options, + ShardsAcknowledgedResponse::fromXContent, + emptySet() + ); } /** @@ -1343,11 +1826,19 @@ public ShardsAcknowledgedResponse unfreeze(UnfreezeIndexRequest request, Request * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable unfreezeAsync(UnfreezeIndexRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, - IndicesRequestConverters::unfreezeIndex, options, - ShardsAcknowledgedResponse::fromXContent, listener, emptySet()); + public Cancellable unfreezeAsync( + UnfreezeIndexRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + IndicesRequestConverters::unfreezeIndex, + options, + ShardsAcknowledgedResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -1360,8 +1851,13 @@ public Cancellable unfreezeAsync(UnfreezeIndexRequest request, RequestOptions op * @throws IOException in case there is a problem sending the request or parsing back the response */ public AcknowledgedResponse deleteTemplate(DeleteIndexTemplateRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, IndicesRequestConverters::deleteTemplate, - options, AcknowledgedResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + IndicesRequestConverters::deleteTemplate, + options, + AcknowledgedResponse::fromXContent, + emptySet() + ); } /** @@ -1373,10 +1869,19 @@ public AcknowledgedResponse deleteTemplate(DeleteIndexTemplateRequest request, R * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable deleteTemplateAsync(DeleteIndexTemplateRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, IndicesRequestConverters::deleteTemplate, - options, AcknowledgedResponse::fromXContent, listener, emptySet()); + public Cancellable deleteTemplateAsync( + DeleteIndexTemplateRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + IndicesRequestConverters::deleteTemplate, + options, + AcknowledgedResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -1388,10 +1893,15 @@ public Cancellable deleteTemplateAsync(DeleteIndexTemplateRequest request, Reque * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized * @throws IOException in case there is a problem sending the request or parsing back the response */ - public AcknowledgedResponse deleteIndexTemplate(DeleteComposableIndexTemplateRequest request, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, IndicesRequestConverters::deleteIndexTemplate, - options, AcknowledgedResponse::fromXContent, emptySet()); + public AcknowledgedResponse deleteIndexTemplate(DeleteComposableIndexTemplateRequest request, RequestOptions options) + throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, + IndicesRequestConverters::deleteIndexTemplate, + options, + AcknowledgedResponse::fromXContent, + emptySet() + ); } /** @@ -1403,10 +1913,19 @@ public AcknowledgedResponse deleteIndexTemplate(DeleteComposableIndexTemplateReq * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable deleteIndexTemplateAsync(DeleteComposableIndexTemplateRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, IndicesRequestConverters::deleteIndexTemplate, - options, AcknowledgedResponse::fromXContent, listener, emptySet()); + public Cancellable deleteIndexTemplateAsync( + DeleteComposableIndexTemplateRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + IndicesRequestConverters::deleteIndexTemplate, + options, + AcknowledgedResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -1416,8 +1935,13 @@ public Cancellable deleteIndexTemplateAsync(DeleteComposableIndexTemplateRequest * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized */ public ReloadAnalyzersResponse reloadAnalyzers(ReloadAnalyzersRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, IndicesRequestConverters::reloadAnalyzers, options, - ReloadAnalyzersResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + IndicesRequestConverters::reloadAnalyzers, + options, + ReloadAnalyzersResponse::fromXContent, + emptySet() + ); } /** @@ -1427,10 +1951,19 @@ public ReloadAnalyzersResponse reloadAnalyzers(ReloadAnalyzersRequest request, R * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable reloadAnalyzersAsync(ReloadAnalyzersRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, IndicesRequestConverters::reloadAnalyzers, options, - ReloadAnalyzersResponse::fromXContent, listener, emptySet()); + public Cancellable reloadAnalyzersAsync( + ReloadAnalyzersRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + IndicesRequestConverters::reloadAnalyzers, + options, + ReloadAnalyzersResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -1438,10 +1971,15 @@ public Cancellable reloadAnalyzersAsync(ReloadAnalyzersRequest request, RequestO * @param request the request * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized */ - public org.elasticsearch.client.core.AcknowledgedResponse deleteAlias(DeleteAliasRequest request, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, IndicesRequestConverters::deleteAlias, options, - org.elasticsearch.client.core.AcknowledgedResponse::fromXContent, emptySet()); + public org.elasticsearch.client.core.AcknowledgedResponse deleteAlias(DeleteAliasRequest request, RequestOptions options) + throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, + IndicesRequestConverters::deleteAlias, + options, + org.elasticsearch.client.core.AcknowledgedResponse::fromXContent, + emptySet() + ); } /** @@ -1451,9 +1989,18 @@ public org.elasticsearch.client.core.AcknowledgedResponse deleteAlias(DeleteAlia * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable deleteAliasAsync(DeleteAliasRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, IndicesRequestConverters::deleteAlias, options, - org.elasticsearch.client.core.AcknowledgedResponse::fromXContent, listener, emptySet()); + public Cancellable deleteAliasAsync( + DeleteAliasRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + IndicesRequestConverters::deleteAlias, + options, + org.elasticsearch.client.core.AcknowledgedResponse::fromXContent, + listener, + emptySet() + ); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java index 5dda32eb64cfd..f08a458083dc5 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java @@ -29,23 +29,23 @@ import org.elasticsearch.action.support.broadcast.BroadcastRequest; import org.elasticsearch.client.indices.AnalyzeRequest; import org.elasticsearch.client.indices.CloseIndexRequest; +import org.elasticsearch.client.indices.ComposableIndexTemplateExistRequest; import org.elasticsearch.client.indices.CreateDataStreamRequest; import org.elasticsearch.client.indices.CreateIndexRequest; import org.elasticsearch.client.indices.DataStreamsStatsRequest; -import org.elasticsearch.client.indices.GetDataStreamRequest; import org.elasticsearch.client.indices.DeleteAliasRequest; import org.elasticsearch.client.indices.DeleteComposableIndexTemplateRequest; import org.elasticsearch.client.indices.DeleteDataStreamRequest; import org.elasticsearch.client.indices.FreezeIndexRequest; +import org.elasticsearch.client.indices.GetComposableIndexTemplateRequest; +import org.elasticsearch.client.indices.GetDataStreamRequest; import org.elasticsearch.client.indices.GetFieldMappingsRequest; import org.elasticsearch.client.indices.GetIndexRequest; -import org.elasticsearch.client.indices.GetComposableIndexTemplateRequest; import org.elasticsearch.client.indices.GetIndexTemplatesRequest; import org.elasticsearch.client.indices.GetMappingsRequest; -import org.elasticsearch.client.indices.ComposableIndexTemplateExistRequest; import org.elasticsearch.client.indices.IndexTemplatesExistRequest; -import org.elasticsearch.client.indices.PutIndexTemplateRequest; import org.elasticsearch.client.indices.PutComposableIndexTemplateRequest; +import org.elasticsearch.client.indices.PutIndexTemplateRequest; import org.elasticsearch.client.indices.PutMappingRequest; import org.elasticsearch.client.indices.ReloadAnalyzersRequest; import org.elasticsearch.client.indices.ResizeRequest; @@ -64,7 +64,8 @@ private IndicesRequestConverters() {} static Request putDataStream(CreateDataStreamRequest createDataStreamRequest) { String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_data_stream") - .addPathPart(createDataStreamRequest.getName()).build(); + .addPathPart(createDataStreamRequest.getName()) + .build(); Request request = new Request(HttpPut.METHOD_NAME, endpoint); return request; } @@ -77,8 +78,7 @@ static Request deleteDataStream(DeleteDataStreamRequest deleteDataStreamRequest) } static Request getDataStreams(GetDataStreamRequest dataStreamRequest) { - final String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_data_stream") + final String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_data_stream") .addPathPart(dataStreamRequest.getName()) .build(); return new Request(HttpGet.METHOD_NAME, endpoint); @@ -86,8 +86,7 @@ static Request getDataStreams(GetDataStreamRequest dataStreamRequest) { static Request dataStreamsStats(DataStreamsStatsRequest dataStreamsStatsRequest) { String[] expressions = dataStreamsStatsRequest.indices() == null ? Strings.EMPTY_ARRAY : dataStreamsStatsRequest.indices(); - final String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_data_stream") + final String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_data_stream") .addCommaSeparatedPathParts(expressions) .addPathPartAsIs("_stats") .build(); @@ -139,8 +138,7 @@ static Request closeIndex(CloseIndexRequest closeIndexRequest) { } static Request createIndex(CreateIndexRequest createIndexRequest) throws IOException { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPart(createIndexRequest.index()).build(); + String endpoint = new RequestConverters.EndpointBuilder().addPathPart(createIndexRequest.index()).build(); Request request = new Request(HttpPut.METHOD_NAME, endpoint); RequestConverters.Params parameters = new RequestConverters.Params(); @@ -163,7 +161,6 @@ static Request updateAliases(IndicesAliasesRequest indicesAliasesRequest) throws return request; } - static Request putMapping(PutMappingRequest putMappingRequest) throws IOException { Request request = new Request(HttpPut.METHOD_NAME, RequestConverters.endpoint(putMappingRequest.indices(), "_mapping")); @@ -195,8 +192,7 @@ static Request getFieldMapping(GetFieldMappingsRequest getFieldMappingsRequest) String[] indices = getFieldMappingsRequest.indices() == null ? Strings.EMPTY_ARRAY : getFieldMappingsRequest.indices(); String[] fields = getFieldMappingsRequest.fields() == null ? Strings.EMPTY_ARRAY : getFieldMappingsRequest.fields(); - String endpoint = new RequestConverters.EndpointBuilder() - .addCommaSeparatedPathParts(indices) + String endpoint = new RequestConverters.EndpointBuilder().addCommaSeparatedPathParts(indices) .addPathPartAsIs("_mapping") .addPathPartAsIs("field") .addCommaSeparatedPathParts(fields) @@ -253,7 +249,7 @@ static Request forceMerge(ForceMergeRequest forceMergeRequest) { } static Request clearCache(ClearIndicesCacheRequest clearIndicesCacheRequest) { - String[] indices = clearIndicesCacheRequest.indices() == null ? Strings.EMPTY_ARRAY :clearIndicesCacheRequest.indices(); + String[] indices = clearIndicesCacheRequest.indices() == null ? Strings.EMPTY_ARRAY : clearIndicesCacheRequest.indices(); Request request = new Request(HttpPost.METHOD_NAME, RequestConverters.endpoint(indices, "_cache/clear")); RequestConverters.Params parameters = new RequestConverters.Params(); @@ -269,8 +265,8 @@ static Request clearCache(ClearIndicesCacheRequest clearIndicesCacheRequest) { } static Request existsAlias(GetAliasesRequest getAliasesRequest) { - if ((getAliasesRequest.indices() == null || getAliasesRequest.indices().length == 0) && - (getAliasesRequest.aliases() == null || getAliasesRequest.aliases().length == 0)) { + if ((getAliasesRequest.indices() == null || getAliasesRequest.indices().length == 0) + && (getAliasesRequest.aliases() == null || getAliasesRequest.aliases().length == 0)) { throw new IllegalArgumentException("existsAlias requires at least an alias or an index"); } String[] indices = getAliasesRequest.indices() == null ? Strings.EMPTY_ARRAY : getAliasesRequest.indices(); @@ -328,8 +324,9 @@ static Request clone(org.elasticsearch.action.admin.indices.shrink.ResizeRequest private static Request resize(ResizeRequest resizeRequest, ResizeType type) throws IOException { String endpoint = new RequestConverters.EndpointBuilder().addPathPart(resizeRequest.getSourceIndex()) - .addPathPartAsIs("_" + type.name().toLowerCase(Locale.ROOT)) - .addPathPart(resizeRequest.getTargetIndex()).build(); + .addPathPartAsIs("_" + type.name().toLowerCase(Locale.ROOT)) + .addPathPart(resizeRequest.getTargetIndex()) + .build(); Request request = new Request(HttpPut.METHOD_NAME, endpoint); RequestConverters.Params params = new RequestConverters.Params(); @@ -345,7 +342,8 @@ private static Request resize(ResizeRequest resizeRequest, ResizeType type) thro private static Request resize(org.elasticsearch.action.admin.indices.shrink.ResizeRequest resizeRequest) throws IOException { String endpoint = new RequestConverters.EndpointBuilder().addPathPart(resizeRequest.getSourceIndex()) .addPathPartAsIs("_" + resizeRequest.getResizeType().name().toLowerCase(Locale.ROOT)) - .addPathPart(resizeRequest.getTargetIndexRequest().index()).build(); + .addPathPart(resizeRequest.getTargetIndexRequest().index()) + .build(); Request request = new Request(HttpPut.METHOD_NAME, endpoint); RequestConverters.Params params = new RequestConverters.Params(); @@ -358,8 +356,10 @@ private static Request resize(org.elasticsearch.action.admin.indices.shrink.Resi } static Request rollover(RolloverRequest rolloverRequest) throws IOException { - String endpoint = new RequestConverters.EndpointBuilder().addPathPart(rolloverRequest.getAlias()).addPathPartAsIs("_rollover") - .addPathPart(rolloverRequest.getNewIndexName()).build(); + String endpoint = new RequestConverters.EndpointBuilder().addPathPart(rolloverRequest.getAlias()) + .addPathPartAsIs("_rollover") + .addPathPart(rolloverRequest.getNewIndexName()) + .build(); Request request = new Request(HttpPost.METHOD_NAME, endpoint); RequestConverters.Params params = new RequestConverters.Params(); @@ -446,7 +446,8 @@ static Request indexPutSettings(UpdateSettingsRequest updateSettingsRequest) thr static Request putTemplate(PutIndexTemplateRequest putIndexTemplateRequest) throws IOException { String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_template") - .addPathPart(putIndexTemplateRequest.name()).build(); + .addPathPart(putIndexTemplateRequest.name()) + .build(); Request request = new Request(HttpPut.METHOD_NAME, endpoint); RequestConverters.Params params = new RequestConverters.Params(); params.withMasterTimeout(putIndexTemplateRequest.masterNodeTimeout()); @@ -463,7 +464,8 @@ static Request putTemplate(PutIndexTemplateRequest putIndexTemplateRequest) thro static Request putIndexTemplate(PutComposableIndexTemplateRequest putIndexTemplateRequest) throws IOException { String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_index_template") - .addPathPart(putIndexTemplateRequest.name()).build(); + .addPathPart(putIndexTemplateRequest.name()) + .build(); Request request = new Request(HttpPut.METHOD_NAME, endpoint); RequestConverters.Params params = new RequestConverters.Params(); params.withMasterTimeout(putIndexTemplateRequest.masterNodeTimeout()); @@ -480,7 +482,8 @@ static Request putIndexTemplate(PutComposableIndexTemplateRequest putIndexTempla static Request simulateIndexTemplate(SimulateIndexTemplateRequest simulateIndexTemplateRequest) throws IOException { String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_index_template", "_simulate_index") - .addPathPart(simulateIndexTemplateRequest.indexName()).build(); + .addPathPart(simulateIndexTemplateRequest.indexName()) + .build(); Request request = new Request(HttpPost.METHOD_NAME, endpoint); RequestConverters.Params params = new RequestConverters.Params(); params.withMasterTimeout(simulateIndexTemplateRequest.masterNodeTimeout()); @@ -492,8 +495,9 @@ static Request simulateIndexTemplate(SimulateIndexTemplateRequest simulateIndexT if (Strings.hasText(putComposableIndexTemplateRequest.cause())) { params.putParam("cause", putComposableIndexTemplateRequest.cause()); } - request.setEntity(RequestConverters.createEntity(putComposableIndexTemplateRequest, - RequestConverters.REQUEST_BODY_CONTENT_TYPE)); + request.setEntity( + RequestConverters.createEntity(putComposableIndexTemplateRequest, RequestConverters.REQUEST_BODY_CONTENT_TYPE) + ); } request.addParameters(params.asMap()); return request; @@ -530,8 +534,7 @@ static Request getAlias(GetAliasesRequest getAliasesRequest) { } static Request getTemplates(GetIndexTemplatesRequest getIndexTemplatesRequest) { - final String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_template") + final String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_template") .addCommaSeparatedPathParts(getIndexTemplatesRequest.names()) .build(); final Request request = new Request(HttpGet.METHOD_NAME, endpoint); @@ -543,8 +546,7 @@ static Request getTemplates(GetIndexTemplatesRequest getIndexTemplatesRequest) { } static Request getIndexTemplates(GetComposableIndexTemplateRequest getIndexTemplatesRequest) { - final String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_index_template") + final String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_index_template") .addPathPart(getIndexTemplatesRequest.name()) .build(); final Request request = new Request(HttpGet.METHOD_NAME, endpoint); @@ -556,8 +558,7 @@ static Request getIndexTemplates(GetComposableIndexTemplateRequest getIndexTempl } static Request templatesExist(IndexTemplatesExistRequest indexTemplatesExistRequest) { - final String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_template") + final String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_template") .addCommaSeparatedPathParts(indexTemplatesExistRequest.names()) .build(); final Request request = new Request(HttpHead.METHOD_NAME, endpoint); @@ -569,8 +570,7 @@ static Request templatesExist(IndexTemplatesExistRequest indexTemplatesExistRequ } static Request templatesExist(ComposableIndexTemplateExistRequest indexTemplatesExistRequest) { - final String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_index_template") + final String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_index_template") .addPathPart(indexTemplatesExistRequest.name()) .build(); final Request request = new Request(HttpHead.METHOD_NAME, endpoint); @@ -649,10 +649,10 @@ static Request reloadAnalyzers(ReloadAnalyzersRequest reloadAnalyzersRequest) { } static Request deleteAlias(DeleteAliasRequest deleteAliasRequest) { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPart(deleteAliasRequest.getIndex()) + String endpoint = new RequestConverters.EndpointBuilder().addPathPart(deleteAliasRequest.getIndex()) .addPathPartAsIs("_alias") - .addPathPart(deleteAliasRequest.getAlias()).build(); + .addPathPart(deleteAliasRequest.getAlias()) + .build(); Request request = new Request(HttpDelete.METHOD_NAME, endpoint); RequestConverters.Params parameters = new RequestConverters.Params(); parameters.withTimeout(deleteAliasRequest.timeout()); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/IngestClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/IngestClient.java index 8f4ccfcf306f5..e55e06707bb21 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/IngestClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/IngestClient.java @@ -53,8 +53,13 @@ public final class IngestClient { * @throws IOException in case there is a problem sending the request or parsing back the response */ public AcknowledgedResponse putPipeline(PutPipelineRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, IngestRequestConverters::putPipeline, options, - AcknowledgedResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + IngestRequestConverters::putPipeline, + options, + AcknowledgedResponse::fromXContent, + emptySet() + ); } /** @@ -68,8 +73,14 @@ public AcknowledgedResponse putPipeline(PutPipelineRequest request, RequestOptio * @return cancellable that may be used to cancel the request */ public Cancellable putPipelineAsync(PutPipelineRequest request, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, IngestRequestConverters::putPipeline, options, - AcknowledgedResponse::fromXContent, listener, emptySet()); + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + IngestRequestConverters::putPipeline, + options, + AcknowledgedResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -83,8 +94,13 @@ public Cancellable putPipelineAsync(PutPipelineRequest request, RequestOptions o * @throws IOException in case there is a problem sending the request or parsing back the response */ public GetPipelineResponse getPipeline(GetPipelineRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, IngestRequestConverters::getPipeline, options, - GetPipelineResponse::fromXContent, Collections.singleton(404)); + return restHighLevelClient.performRequestAndParseEntity( + request, + IngestRequestConverters::getPipeline, + options, + GetPipelineResponse::fromXContent, + Collections.singleton(404) + ); } /** @@ -98,8 +114,14 @@ public GetPipelineResponse getPipeline(GetPipelineRequest request, RequestOption * @return cancellable that may be used to cancel the request */ public Cancellable getPipelineAsync(GetPipelineRequest request, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, IngestRequestConverters::getPipeline, options, - GetPipelineResponse::fromXContent, listener, Collections.singleton(404)); + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + IngestRequestConverters::getPipeline, + options, + GetPipelineResponse::fromXContent, + listener, + Collections.singleton(404) + ); } /** @@ -114,8 +136,13 @@ public Cancellable getPipelineAsync(GetPipelineRequest request, RequestOptions o * @throws IOException in case there is a problem sending the request or parsing back the response */ public AcknowledgedResponse deletePipeline(DeletePipelineRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, IngestRequestConverters::deletePipeline, options, - AcknowledgedResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + IngestRequestConverters::deletePipeline, + options, + AcknowledgedResponse::fromXContent, + emptySet() + ); } /** @@ -129,11 +156,19 @@ public AcknowledgedResponse deletePipeline(DeletePipelineRequest request, Reques * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable deletePipelineAsync(DeletePipelineRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, - IngestRequestConverters::deletePipeline, options, - AcknowledgedResponse::fromXContent, listener, emptySet()); + public Cancellable deletePipelineAsync( + DeletePipelineRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + IngestRequestConverters::deletePipeline, + options, + AcknowledgedResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -149,8 +184,13 @@ public Cancellable deletePipelineAsync(DeletePipelineRequest request, RequestOpt * @throws IOException in case there is a problem sending the request or parsing back the response */ public SimulatePipelineResponse simulate(SimulatePipelineRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, IngestRequestConverters::simulatePipeline, options, - SimulatePipelineResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + IngestRequestConverters::simulatePipeline, + options, + SimulatePipelineResponse::fromXContent, + emptySet() + ); } /** @@ -165,20 +205,39 @@ public SimulatePipelineResponse simulate(SimulatePipelineRequest request, Reques * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable simulateAsync(SimulatePipelineRequest request, - RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, IngestRequestConverters::simulatePipeline, options, - SimulatePipelineResponse::fromXContent, listener, emptySet()); + public Cancellable simulateAsync( + SimulatePipelineRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + IngestRequestConverters::simulatePipeline, + options, + SimulatePipelineResponse::fromXContent, + listener, + emptySet() + ); } public GeoIpStatsResponse geoIpStats(MainRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(new MainRequest(), IngestRequestConverters::geoIpStats, options, - GeoIpStatsResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + new MainRequest(), + IngestRequestConverters::geoIpStats, + options, + GeoIpStatsResponse::fromXContent, + emptySet() + ); } public Cancellable geoIpStatsAsync(MainRequest request, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, IngestRequestConverters::geoIpStats, options, - GeoIpStatsResponse::fromXContent, listener, emptySet()); + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + IngestRequestConverters::geoIpStats, + options, + GeoIpStatsResponse::fromXContent, + listener, + emptySet() + ); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/IngestRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/IngestRequestConverters.java index fc4ca502a0d3a..1b378ab5de42a 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/IngestRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/IngestRequestConverters.java @@ -25,8 +25,7 @@ final class IngestRequestConverters { private IngestRequestConverters() {} static Request getPipeline(GetPipelineRequest getPipelineRequest) { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_ingest/pipeline") + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_ingest/pipeline") .addCommaSeparatedPathParts(getPipelineRequest.getIds()) .build(); Request request = new Request(HttpGet.METHOD_NAME, endpoint); @@ -38,8 +37,7 @@ static Request getPipeline(GetPipelineRequest getPipelineRequest) { } static Request putPipeline(PutPipelineRequest putPipelineRequest) throws IOException { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_ingest/pipeline") + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_ingest/pipeline") .addPathPart(putPipelineRequest.getId()) .build(); Request request = new Request(HttpPut.METHOD_NAME, endpoint); @@ -53,8 +51,7 @@ static Request putPipeline(PutPipelineRequest putPipelineRequest) throws IOExcep } static Request deletePipeline(DeletePipelineRequest deletePipelineRequest) { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_ingest/pipeline") + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_ingest/pipeline") .addPathPart(deletePipelineRequest.getId()) .build(); Request request = new Request(HttpDelete.METHOD_NAME, endpoint); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/LicenseClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/LicenseClient.java index 5add90115f19b..3b311894caee7 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/LicenseClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/LicenseClient.java @@ -11,12 +11,17 @@ import org.apache.http.HttpEntity; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.support.master.AcknowledgedResponse; -import org.elasticsearch.client.license.StartTrialRequest; -import org.elasticsearch.client.license.StartTrialResponse; -import org.elasticsearch.client.license.StartBasicRequest; -import org.elasticsearch.client.license.StartBasicResponse; +import org.elasticsearch.client.license.DeleteLicenseRequest; import org.elasticsearch.client.license.GetBasicStatusResponse; +import org.elasticsearch.client.license.GetLicenseRequest; +import org.elasticsearch.client.license.GetLicenseResponse; import org.elasticsearch.client.license.GetTrialStatusResponse; +import org.elasticsearch.client.license.PutLicenseRequest; +import org.elasticsearch.client.license.PutLicenseResponse; +import org.elasticsearch.client.license.StartBasicRequest; +import org.elasticsearch.client.license.StartBasicResponse; +import org.elasticsearch.client.license.StartTrialRequest; +import org.elasticsearch.client.license.StartTrialResponse; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.Streams; import org.elasticsearch.xcontent.DeprecationHandler; @@ -25,11 +30,6 @@ import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.client.license.DeleteLicenseRequest; -import org.elasticsearch.client.license.GetLicenseRequest; -import org.elasticsearch.client.license.GetLicenseResponse; -import org.elasticsearch.client.license.PutLicenseRequest; -import org.elasticsearch.client.license.PutLicenseResponse; import java.io.IOException; import java.io.InputStream; @@ -67,8 +67,13 @@ public final class LicenseClient { * @throws IOException in case there is a problem sending the request or parsing back the response */ public PutLicenseResponse putLicense(PutLicenseRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, LicenseRequestConverters::putLicense, options, - PutLicenseResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + LicenseRequestConverters::putLicense, + options, + PutLicenseResponse::fromXContent, + emptySet() + ); } /** @@ -78,8 +83,14 @@ public PutLicenseResponse putLicense(PutLicenseRequest request, RequestOptions o * @return cancellable that may be used to cancel the request */ public Cancellable putLicenseAsync(PutLicenseRequest request, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, LicenseRequestConverters::putLicense, options, - PutLicenseResponse::fromXContent, listener, emptySet()); + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + LicenseRequestConverters::putLicense, + options, + PutLicenseResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -89,8 +100,13 @@ public Cancellable putLicenseAsync(PutLicenseRequest request, RequestOptions opt * @throws IOException in case there is a problem sending the request or parsing back the response */ public GetLicenseResponse getLicense(GetLicenseRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequest(request, LicenseRequestConverters::getLicense, options, - response -> new GetLicenseResponse(convertResponseToJson(response)), emptySet()); + return restHighLevelClient.performRequest( + request, + LicenseRequestConverters::getLicense, + options, + response -> new GetLicenseResponse(convertResponseToJson(response)), + emptySet() + ); } /** @@ -100,8 +116,14 @@ public GetLicenseResponse getLicense(GetLicenseRequest request, RequestOptions o * @return cancellable that may be used to cancel the request */ public Cancellable getLicenseAsync(GetLicenseRequest request, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsync(request, LicenseRequestConverters::getLicense, options, - response -> new GetLicenseResponse(convertResponseToJson(response)), listener, emptySet()); + return restHighLevelClient.performRequestAsync( + request, + LicenseRequestConverters::getLicense, + options, + response -> new GetLicenseResponse(convertResponseToJson(response)), + listener, + emptySet() + ); } /** @@ -111,8 +133,13 @@ public Cancellable getLicenseAsync(GetLicenseRequest request, RequestOptions opt * @throws IOException in case there is a problem sending the request or parsing back the response */ public AcknowledgedResponse deleteLicense(DeleteLicenseRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, LicenseRequestConverters::deleteLicense, options, - AcknowledgedResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + LicenseRequestConverters::deleteLicense, + options, + AcknowledgedResponse::fromXContent, + emptySet() + ); } /** @@ -121,11 +148,19 @@ public AcknowledgedResponse deleteLicense(DeleteLicenseRequest request, RequestO * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable deleteLicenseAsync(DeleteLicenseRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, - LicenseRequestConverters::deleteLicense, options, - AcknowledgedResponse::fromXContent, listener, emptySet()); + public Cancellable deleteLicenseAsync( + DeleteLicenseRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + LicenseRequestConverters::deleteLicense, + options, + AcknowledgedResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -135,8 +170,13 @@ public Cancellable deleteLicenseAsync(DeleteLicenseRequest request, RequestOptio * @throws IOException in case there is a problem sending the request or parsing back the response */ public StartTrialResponse startTrial(StartTrialRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, LicenseRequestConverters::startTrial, options, - StartTrialResponse::fromXContent, singleton(403)); + return restHighLevelClient.performRequestAndParseEntity( + request, + LicenseRequestConverters::startTrial, + options, + StartTrialResponse::fromXContent, + singleton(403) + ); } /** @@ -145,12 +185,16 @@ public StartTrialResponse startTrial(StartTrialRequest request, RequestOptions o * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable startTrialAsync(StartTrialRequest request, - RequestOptions options, - ActionListener listener) { + public Cancellable startTrialAsync(StartTrialRequest request, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, LicenseRequestConverters::startTrial, options, - StartTrialResponse::fromXContent, listener, singleton(403)); + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + LicenseRequestConverters::startTrial, + options, + StartTrialResponse::fromXContent, + listener, + singleton(403) + ); } /** @@ -160,8 +204,13 @@ public Cancellable startTrialAsync(StartTrialRequest request, * @throws IOException in case there is a problem sending the request or parsing back the response */ public StartBasicResponse startBasic(StartBasicRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, LicenseRequestConverters::startBasic, options, - StartBasicResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + LicenseRequestConverters::startBasic, + options, + StartBasicResponse::fromXContent, + emptySet() + ); } /** @@ -170,10 +219,15 @@ public StartBasicResponse startBasic(StartBasicRequest request, RequestOptions o * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable startBasicAsync(StartBasicRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, LicenseRequestConverters::startBasic, options, - StartBasicResponse::fromXContent, listener, emptySet()); + public Cancellable startBasicAsync(StartBasicRequest request, RequestOptions options, ActionListener listener) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + LicenseRequestConverters::startBasic, + options, + StartBasicResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -183,8 +237,13 @@ public Cancellable startBasicAsync(StartBasicRequest request, RequestOptions opt * @throws IOException in case there is a problem sending the request or parsing back the response */ public GetTrialStatusResponse getTrialStatus(RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(Validatable.EMPTY, - request -> LicenseRequestConverters.getLicenseTrialStatus(), options, GetTrialStatusResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + Validatable.EMPTY, + request -> LicenseRequestConverters.getLicenseTrialStatus(), + options, + GetTrialStatusResponse::fromXContent, + emptySet() + ); } /** @@ -194,8 +253,13 @@ public GetTrialStatusResponse getTrialStatus(RequestOptions options) throws IOEx * @throws IOException in case there is a problem sending the request or parsing back the response */ public GetBasicStatusResponse getBasicStatus(RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(Validatable.EMPTY, - request -> LicenseRequestConverters.getLicenseBasicStatus(), options, GetBasicStatusResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + Validatable.EMPTY, + request -> LicenseRequestConverters.getLicenseBasicStatus(), + options, + GetBasicStatusResponse::fromXContent, + emptySet() + ); } /** @@ -221,9 +285,11 @@ static String convertResponseToJson(Response response) throws IOException { return Streams.copyToString(new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8)); } else { // Need to convert into JSON - try (InputStream stream = response.getEntity().getContent(); - XContentParser parser = XContentFactory.xContent(xContentType).createParser(NamedXContentRegistry.EMPTY, - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, stream)) { + try ( + InputStream stream = response.getEntity().getContent(); + XContentParser parser = XContentFactory.xContent(xContentType) + .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, stream) + ) { parser.nextToken(); XContentBuilder builder = XContentFactory.jsonBuilder(); builder.copyCurrentStructure(parser); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/LicenseRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/LicenseRequestConverters.java index 3d4ad366d3f5a..c5caa6fccc3a6 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/LicenseRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/LicenseRequestConverters.java @@ -12,11 +12,11 @@ import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; -import org.elasticsearch.client.license.StartTrialRequest; -import org.elasticsearch.client.license.StartBasicRequest; import org.elasticsearch.client.license.DeleteLicenseRequest; import org.elasticsearch.client.license.GetLicenseRequest; import org.elasticsearch.client.license.PutLicenseRequest; +import org.elasticsearch.client.license.StartBasicRequest; +import org.elasticsearch.client.license.StartTrialRequest; final class LicenseRequestConverters { @@ -69,9 +69,7 @@ static Request startTrial(StartTrialRequest startTrialRequest) { } static Request startBasic(StartBasicRequest startBasicRequest) { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_license", "start_basic") - .build(); + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_license", "start_basic").build(); Request request = new Request(HttpPost.METHOD_NAME, endpoint); RequestConverters.Params parameters = new RequestConverters.Params(); parameters.withTimeout(startBasicRequest.timeout()); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/MLRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/MLRequestConverters.java index 2106a97889f04..7c036510d0790 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/MLRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/MLRequestConverters.java @@ -91,19 +91,17 @@ final class MLRequestConverters { private MLRequestConverters() {} static Request putJob(PutJobRequest putJobRequest) throws IOException { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml") - .addPathPartAsIs("anomaly_detectors") - .addPathPart(putJobRequest.getJob().getId()) - .build(); + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml") + .addPathPartAsIs("anomaly_detectors") + .addPathPart(putJobRequest.getJob().getId()) + .build(); Request request = new Request(HttpPut.METHOD_NAME, endpoint); request.setEntity(createEntity(putJobRequest, REQUEST_BODY_CONTENT_TYPE)); return request; } static Request getJob(GetJobRequest getJobRequest) { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml") + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml") .addPathPartAsIs("anomaly_detectors") .addPathPart(Strings.collectionToCommaDelimitedString(getJobRequest.getJobIds())) .build(); @@ -121,12 +119,11 @@ static Request getJob(GetJobRequest getJobRequest) { } static Request getJobStats(GetJobStatsRequest getJobStatsRequest) { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml") - .addPathPartAsIs("anomaly_detectors") - .addPathPart(Strings.collectionToCommaDelimitedString(getJobStatsRequest.getJobIds())) - .addPathPartAsIs("_stats") - .build(); + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml") + .addPathPartAsIs("anomaly_detectors") + .addPathPart(Strings.collectionToCommaDelimitedString(getJobStatsRequest.getJobIds())) + .addPathPartAsIs("_stats") + .build(); Request request = new Request(HttpGet.METHOD_NAME, endpoint); RequestConverters.Params params = new RequestConverters.Params(); @@ -138,20 +135,18 @@ static Request getJobStats(GetJobStatsRequest getJobStatsRequest) { } static Request openJob(OpenJobRequest openJobRequest) throws IOException { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml") - .addPathPartAsIs("anomaly_detectors") - .addPathPart(openJobRequest.getJobId()) - .addPathPartAsIs("_open") - .build(); + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml") + .addPathPartAsIs("anomaly_detectors") + .addPathPart(openJobRequest.getJobId()) + .addPathPartAsIs("_open") + .build(); Request request = new Request(HttpPost.METHOD_NAME, endpoint); request.setEntity(createEntity(openJobRequest, REQUEST_BODY_CONTENT_TYPE)); return request; } static Request closeJob(CloseJobRequest closeJobRequest) throws IOException { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml") + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml") .addPathPartAsIs("anomaly_detectors") .addPathPart(Strings.collectionToCommaDelimitedString(closeJobRequest.getJobIds())) .addPathPartAsIs("_close") @@ -162,8 +157,7 @@ static Request closeJob(CloseJobRequest closeJobRequest) throws IOException { } static Request deleteExpiredData(DeleteExpiredDataRequest deleteExpiredDataRequest) throws IOException { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml") + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml") .addPathPartAsIs("_delete_expired_data") .addPathPart(deleteExpiredDataRequest.getJobId()) .build(); @@ -173,11 +167,10 @@ static Request deleteExpiredData(DeleteExpiredDataRequest deleteExpiredDataReque } static Request deleteJob(DeleteJobRequest deleteJobRequest) { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml") - .addPathPartAsIs("anomaly_detectors") - .addPathPart(deleteJobRequest.getJobId()) - .build(); + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml") + .addPathPartAsIs("anomaly_detectors") + .addPathPart(deleteJobRequest.getJobId()) + .build(); Request request = new Request(HttpDelete.METHOD_NAME, endpoint); RequestConverters.Params params = new RequestConverters.Params(); @@ -192,20 +185,18 @@ static Request deleteJob(DeleteJobRequest deleteJobRequest) { } static Request flushJob(FlushJobRequest flushJobRequest) throws IOException { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml") - .addPathPartAsIs("anomaly_detectors") - .addPathPart(flushJobRequest.getJobId()) - .addPathPartAsIs("_flush") - .build(); + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml") + .addPathPartAsIs("anomaly_detectors") + .addPathPart(flushJobRequest.getJobId()) + .addPathPartAsIs("_flush") + .build(); Request request = new Request(HttpPost.METHOD_NAME, endpoint); request.setEntity(createEntity(flushJobRequest, REQUEST_BODY_CONTENT_TYPE)); return request; } static Request forecastJob(ForecastJobRequest forecastJobRequest) throws IOException { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml") + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml") .addPathPartAsIs("anomaly_detectors") .addPathPart(forecastJobRequest.getJobId()) .addPathPartAsIs("_forecast") @@ -216,31 +207,28 @@ static Request forecastJob(ForecastJobRequest forecastJobRequest) throws IOExcep } static Request updateJob(UpdateJobRequest updateJobRequest) throws IOException { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml") - .addPathPartAsIs("anomaly_detectors") - .addPathPart(updateJobRequest.getJobUpdate().getJobId()) - .addPathPartAsIs("_update") - .build(); + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml") + .addPathPartAsIs("anomaly_detectors") + .addPathPart(updateJobRequest.getJobUpdate().getJobId()) + .addPathPartAsIs("_update") + .build(); Request request = new Request(HttpPost.METHOD_NAME, endpoint); request.setEntity(createEntity(updateJobRequest.getJobUpdate(), REQUEST_BODY_CONTENT_TYPE)); return request; } static Request putDatafeed(PutDatafeedRequest putDatafeedRequest) throws IOException { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml") - .addPathPartAsIs("datafeeds") - .addPathPart(putDatafeedRequest.getDatafeed().getId()) - .build(); + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml") + .addPathPartAsIs("datafeeds") + .addPathPart(putDatafeedRequest.getDatafeed().getId()) + .build(); Request request = new Request(HttpPut.METHOD_NAME, endpoint); request.setEntity(createEntity(putDatafeedRequest, REQUEST_BODY_CONTENT_TYPE)); return request; } static Request updateDatafeed(UpdateDatafeedRequest updateDatafeedRequest) throws IOException { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml") + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml") .addPathPartAsIs("datafeeds") .addPathPart(updateDatafeedRequest.getDatafeedUpdate().getId()) .addPathPartAsIs("_update") @@ -251,17 +239,15 @@ static Request updateDatafeed(UpdateDatafeedRequest updateDatafeedRequest) throw } static Request getDatafeed(GetDatafeedRequest getDatafeedRequest) { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml") - .addPathPartAsIs("datafeeds") - .addPathPart(Strings.collectionToCommaDelimitedString(getDatafeedRequest.getDatafeedIds())) - .build(); + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml") + .addPathPartAsIs("datafeeds") + .addPathPart(Strings.collectionToCommaDelimitedString(getDatafeedRequest.getDatafeedIds())) + .build(); Request request = new Request(HttpGet.METHOD_NAME, endpoint); RequestConverters.Params params = new RequestConverters.Params(); if (getDatafeedRequest.getAllowNoMatch() != null) { - params.putParam(GetDatafeedRequest.ALLOW_NO_MATCH.getPreferredName(), - Boolean.toString(getDatafeedRequest.getAllowNoMatch())); + params.putParam(GetDatafeedRequest.ALLOW_NO_MATCH.getPreferredName(), Boolean.toString(getDatafeedRequest.getAllowNoMatch())); } if (getDatafeedRequest.getExcludeGenerated() != null) { params.putParam(GetDatafeedRequest.EXCLUDE_GENERATED, Boolean.toString(getDatafeedRequest.getExcludeGenerated())); @@ -271,11 +257,10 @@ static Request getDatafeed(GetDatafeedRequest getDatafeedRequest) { } static Request deleteDatafeed(DeleteDatafeedRequest deleteDatafeedRequest) { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml") - .addPathPartAsIs("datafeeds") - .addPathPart(deleteDatafeedRequest.getDatafeedId()) - .build(); + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml") + .addPathPartAsIs("datafeeds") + .addPathPart(deleteDatafeedRequest.getDatafeedId()) + .build(); Request request = new Request(HttpDelete.METHOD_NAME, endpoint); RequestConverters.Params params = new RequestConverters.Params(); if (deleteDatafeedRequest.getForce() != null) { @@ -286,8 +271,7 @@ static Request deleteDatafeed(DeleteDatafeedRequest deleteDatafeedRequest) { } static Request startDatafeed(StartDatafeedRequest startDatafeedRequest) throws IOException { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml") + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml") .addPathPartAsIs("datafeeds") .addPathPart(startDatafeedRequest.getDatafeedId()) .addPathPartAsIs("_start") @@ -298,8 +282,7 @@ static Request startDatafeed(StartDatafeedRequest startDatafeedRequest) throws I } static Request stopDatafeed(StopDatafeedRequest stopDatafeedRequest) throws IOException { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml") + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml") .addPathPartAsIs("datafeeds") .addPathPart(Strings.collectionToCommaDelimitedString(stopDatafeedRequest.getDatafeedIds())) .addPathPartAsIs("_stop") @@ -310,8 +293,7 @@ static Request stopDatafeed(StopDatafeedRequest stopDatafeedRequest) throws IOEx } static Request getDatafeedStats(GetDatafeedStatsRequest getDatafeedStatsRequest) { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml") + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml") .addPathPartAsIs("datafeeds") .addPathPart(Strings.collectionToCommaDelimitedString(getDatafeedStatsRequest.getDatafeedIds())) .addPathPartAsIs("_stats") @@ -327,12 +309,10 @@ static Request getDatafeedStats(GetDatafeedStatsRequest getDatafeedStatsRequest) } static Request previewDatafeed(PreviewDatafeedRequest previewDatafeedRequest) throws IOException { - EndpointBuilder builder = new EndpointBuilder() - .addPathPartAsIs("_ml") - .addPathPartAsIs("datafeeds"); - String endpoint = previewDatafeedRequest.getDatafeedId() != null ? - builder.addPathPart(previewDatafeedRequest.getDatafeedId()).addPathPartAsIs("_preview").build() : - builder.addPathPartAsIs("_preview").build(); + EndpointBuilder builder = new EndpointBuilder().addPathPartAsIs("_ml").addPathPartAsIs("datafeeds"); + String endpoint = previewDatafeedRequest.getDatafeedId() != null + ? builder.addPathPart(previewDatafeedRequest.getDatafeedId()).addPathPartAsIs("_preview").build() + : builder.addPathPartAsIs("_preview").build(); Request request = new Request(HttpPost.METHOD_NAME, endpoint); if (previewDatafeedRequest.getDatafeedId() == null) { request.setEntity(createEntity(previewDatafeedRequest, REQUEST_BODY_CONTENT_TYPE)); @@ -341,8 +321,7 @@ static Request previewDatafeed(PreviewDatafeedRequest previewDatafeedRequest) th } static Request deleteForecast(DeleteForecastRequest deleteForecastRequest) { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml") + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml") .addPathPartAsIs("anomaly_detectors") .addPathPart(deleteForecastRequest.getJobId()) .addPathPartAsIs("_forecast") @@ -361,8 +340,7 @@ static Request deleteForecast(DeleteForecastRequest deleteForecastRequest) { } static Request deleteModelSnapshot(DeleteModelSnapshotRequest deleteModelSnapshotRequest) { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml") + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml") .addPathPartAsIs("anomaly_detectors") .addPathPart(deleteModelSnapshotRequest.getJobId()) .addPathPartAsIs("model_snapshots") @@ -372,21 +350,19 @@ static Request deleteModelSnapshot(DeleteModelSnapshotRequest deleteModelSnapsho } static Request getBuckets(GetBucketsRequest getBucketsRequest) throws IOException { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml") - .addPathPartAsIs("anomaly_detectors") - .addPathPart(getBucketsRequest.getJobId()) - .addPathPartAsIs("results") - .addPathPartAsIs("buckets") - .build(); + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml") + .addPathPartAsIs("anomaly_detectors") + .addPathPart(getBucketsRequest.getJobId()) + .addPathPartAsIs("results") + .addPathPartAsIs("buckets") + .build(); Request request = new Request(HttpGet.METHOD_NAME, endpoint); request.setEntity(createEntity(getBucketsRequest, REQUEST_BODY_CONTENT_TYPE)); return request; } static Request getCategories(GetCategoriesRequest getCategoriesRequest) throws IOException { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml") + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml") .addPathPartAsIs("anomaly_detectors") .addPathPart(getCategoriesRequest.getJobId()) .addPathPartAsIs("results") @@ -398,8 +374,7 @@ static Request getCategories(GetCategoriesRequest getCategoriesRequest) throws I } static Request getModelSnapshots(GetModelSnapshotsRequest getModelSnapshotsRequest) throws IOException { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml") + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml") .addPathPartAsIs("anomaly_detectors") .addPathPart(getModelSnapshotsRequest.getJobId()) .addPathPartAsIs("model_snapshots") @@ -410,8 +385,7 @@ static Request getModelSnapshots(GetModelSnapshotsRequest getModelSnapshotsReque } static Request updateModelSnapshot(UpdateModelSnapshotRequest updateModelSnapshotRequest) throws IOException { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml") + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml") .addPathPartAsIs("anomaly_detectors") .addPathPart(updateModelSnapshotRequest.getJobId()) .addPathPartAsIs("model_snapshots") @@ -424,8 +398,7 @@ static Request updateModelSnapshot(UpdateModelSnapshotRequest updateModelSnapsho } static Request upgradeJobSnapshot(UpgradeJobModelSnapshotRequest upgradeJobModelSnapshotRequest) { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml") + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml") .addPathPartAsIs("anomaly_detectors") .addPathPart(upgradeJobModelSnapshotRequest.getJobId()) .addPathPartAsIs("model_snapshots") @@ -435,20 +408,23 @@ static Request upgradeJobSnapshot(UpgradeJobModelSnapshotRequest upgradeJobModel Request request = new Request(HttpPost.METHOD_NAME, endpoint); RequestConverters.Params params = new RequestConverters.Params(); if (upgradeJobModelSnapshotRequest.getTimeout() != null) { - params.putParam(UpgradeJobModelSnapshotRequest.TIMEOUT.getPreferredName(), - upgradeJobModelSnapshotRequest.getTimeout().getStringRep()); + params.putParam( + UpgradeJobModelSnapshotRequest.TIMEOUT.getPreferredName(), + upgradeJobModelSnapshotRequest.getTimeout().getStringRep() + ); } if (upgradeJobModelSnapshotRequest.getWaitForCompletion() != null) { - params.putParam(UpgradeJobModelSnapshotRequest.WAIT_FOR_COMPLETION.getPreferredName(), - upgradeJobModelSnapshotRequest.getWaitForCompletion().toString()); + params.putParam( + UpgradeJobModelSnapshotRequest.WAIT_FOR_COMPLETION.getPreferredName(), + upgradeJobModelSnapshotRequest.getWaitForCompletion().toString() + ); } request.addParameters(params.asMap()); return request; } static Request revertModelSnapshot(RevertModelSnapshotRequest revertModelSnapshotsRequest) throws IOException { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml") + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml") .addPathPartAsIs("anomaly_detectors") .addPathPart(revertModelSnapshotsRequest.getJobId()) .addPathPartAsIs("model_snapshots") @@ -461,34 +437,31 @@ static Request revertModelSnapshot(RevertModelSnapshotRequest revertModelSnapsho } static Request getOverallBuckets(GetOverallBucketsRequest getOverallBucketsRequest) throws IOException { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml") - .addPathPartAsIs("anomaly_detectors") - .addPathPart(Strings.collectionToCommaDelimitedString(getOverallBucketsRequest.getJobIds())) - .addPathPartAsIs("results") - .addPathPartAsIs("overall_buckets") - .build(); + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml") + .addPathPartAsIs("anomaly_detectors") + .addPathPart(Strings.collectionToCommaDelimitedString(getOverallBucketsRequest.getJobIds())) + .addPathPartAsIs("results") + .addPathPartAsIs("overall_buckets") + .build(); Request request = new Request(HttpGet.METHOD_NAME, endpoint); request.setEntity(createEntity(getOverallBucketsRequest, REQUEST_BODY_CONTENT_TYPE)); return request; } static Request getRecords(GetRecordsRequest getRecordsRequest) throws IOException { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml") - .addPathPartAsIs("anomaly_detectors") - .addPathPart(getRecordsRequest.getJobId()) - .addPathPartAsIs("results") - .addPathPartAsIs("records") - .build(); + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml") + .addPathPartAsIs("anomaly_detectors") + .addPathPart(getRecordsRequest.getJobId()) + .addPathPartAsIs("results") + .addPathPartAsIs("records") + .build(); Request request = new Request(HttpGet.METHOD_NAME, endpoint); request.setEntity(createEntity(getRecordsRequest, REQUEST_BODY_CONTENT_TYPE)); return request; } static Request postData(PostDataRequest postDataRequest) { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml") + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml") .addPathPartAsIs("anomaly_detectors") .addPathPart(postDataRequest.getJobId()) .addPathPartAsIs("_data") @@ -506,53 +479,51 @@ static Request postData(PostDataRequest postDataRequest) { request.addParameters(params.asMap()); if (content != null) { BytesRef source = postDataRequest.getContent().toBytesRef(); - HttpEntity byteEntity = new NByteArrayEntity(source.bytes, + HttpEntity byteEntity = new NByteArrayEntity( + source.bytes, source.offset, source.length, - createContentType(postDataRequest.getXContentType())); + createContentType(postDataRequest.getXContentType()) + ); request.setEntity(byteEntity); } return request; } static Request getInfluencers(GetInfluencersRequest getInfluencersRequest) throws IOException { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml") - .addPathPartAsIs("anomaly_detectors") - .addPathPart(getInfluencersRequest.getJobId()) - .addPathPartAsIs("results") - .addPathPartAsIs("influencers") - .build(); + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml") + .addPathPartAsIs("anomaly_detectors") + .addPathPart(getInfluencersRequest.getJobId()) + .addPathPartAsIs("results") + .addPathPartAsIs("influencers") + .build(); Request request = new Request(HttpGet.METHOD_NAME, endpoint); request.setEntity(createEntity(getInfluencersRequest, REQUEST_BODY_CONTENT_TYPE)); return request; } static Request putCalendar(PutCalendarRequest putCalendarRequest) throws IOException { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml") - .addPathPartAsIs("calendars") - .addPathPart(putCalendarRequest.getCalendar().getId()) - .build(); + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml") + .addPathPartAsIs("calendars") + .addPathPart(putCalendarRequest.getCalendar().getId()) + .build(); Request request = new Request(HttpPut.METHOD_NAME, endpoint); request.setEntity(createEntity(putCalendarRequest, REQUEST_BODY_CONTENT_TYPE)); return request; } static Request getCalendars(GetCalendarsRequest getCalendarsRequest) throws IOException { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml") - .addPathPartAsIs("calendars") - .addPathPart(getCalendarsRequest.getCalendarId()) - .build(); + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml") + .addPathPartAsIs("calendars") + .addPathPart(getCalendarsRequest.getCalendarId()) + .build(); Request request = new Request(HttpGet.METHOD_NAME, endpoint); request.setEntity(createEntity(getCalendarsRequest, REQUEST_BODY_CONTENT_TYPE)); return request; } static Request putCalendarJob(PutCalendarJobRequest putCalendarJobRequest) { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml") + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml") .addPathPartAsIs("calendars") .addPathPart(putCalendarJobRequest.getCalendarId()) .addPathPartAsIs("jobs") @@ -562,8 +533,7 @@ static Request putCalendarJob(PutCalendarJobRequest putCalendarJobRequest) { } static Request deleteCalendarJob(DeleteCalendarJobRequest deleteCalendarJobRequest) { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml") + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml") .addPathPartAsIs("calendars") .addPathPart(deleteCalendarJobRequest.getCalendarId()) .addPathPartAsIs("jobs") @@ -573,18 +543,16 @@ static Request deleteCalendarJob(DeleteCalendarJobRequest deleteCalendarJobReque } static Request deleteCalendar(DeleteCalendarRequest deleteCalendarRequest) { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml") - .addPathPartAsIs("calendars") - .addPathPart(deleteCalendarRequest.getCalendarId()) - .build(); + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml") + .addPathPartAsIs("calendars") + .addPathPart(deleteCalendarRequest.getCalendarId()) + .build(); Request request = new Request(HttpDelete.METHOD_NAME, endpoint); return request; } static Request getCalendarEvents(GetCalendarEventsRequest getCalendarEventsRequest) throws IOException { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml") + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml") .addPathPartAsIs("calendars") .addPathPart(getCalendarEventsRequest.getCalendarId()) .addPathPartAsIs("events") @@ -595,22 +563,20 @@ static Request getCalendarEvents(GetCalendarEventsRequest getCalendarEventsReque } static Request postCalendarEvents(PostCalendarEventRequest postCalendarEventRequest) throws IOException { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml") + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml") .addPathPartAsIs("calendars") .addPathPart(postCalendarEventRequest.getCalendarId()) .addPathPartAsIs("events") .build(); Request request = new Request(HttpPost.METHOD_NAME, endpoint); - request.setEntity(createEntity(postCalendarEventRequest, - REQUEST_BODY_CONTENT_TYPE, - PostCalendarEventRequest.EXCLUDE_CALENDAR_ID_PARAMS)); + request.setEntity( + createEntity(postCalendarEventRequest, REQUEST_BODY_CONTENT_TYPE, PostCalendarEventRequest.EXCLUDE_CALENDAR_ID_PARAMS) + ); return request; } static Request deleteCalendarEvent(DeleteCalendarEventRequest deleteCalendarEventRequest) { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml") + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml") .addPathPartAsIs("calendars") .addPathPart(deleteCalendarEventRequest.getCalendarId()) .addPathPartAsIs("events") @@ -620,8 +586,7 @@ static Request deleteCalendarEvent(DeleteCalendarEventRequest deleteCalendarEven } static Request estimateModelMemory(EstimateModelMemoryRequest estimateModelMemoryRequest) throws IOException { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml") + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml") .addPathPartAsIs("anomaly_detectors") .addPathPartAsIs("_estimate_model_memory") .build(); @@ -631,8 +596,7 @@ static Request estimateModelMemory(EstimateModelMemoryRequest estimateModelMemor } static Request putDataFrameAnalytics(PutDataFrameAnalyticsRequest putRequest) throws IOException { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml", "data_frame", "analytics") + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml", "data_frame", "analytics") .addPathPart(putRequest.getConfig().getId()) .build(); Request request = new Request(HttpPut.METHOD_NAME, endpoint); @@ -641,8 +605,7 @@ static Request putDataFrameAnalytics(PutDataFrameAnalyticsRequest putRequest) th } static Request updateDataFrameAnalytics(UpdateDataFrameAnalyticsRequest updateRequest) throws IOException { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml", "data_frame", "analytics") + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml", "data_frame", "analytics") .addPathPart(updateRequest.getUpdate().getId()) .addPathPartAsIs("_update") .build(); @@ -652,8 +615,7 @@ static Request updateDataFrameAnalytics(UpdateDataFrameAnalyticsRequest updateRe } static Request getDataFrameAnalytics(GetDataFrameAnalyticsRequest getRequest) { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml", "data_frame", "analytics") + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml", "data_frame", "analytics") .addPathPart(Strings.collectionToCommaDelimitedString(getRequest.getIds())) .build(); Request request = new Request(HttpGet.METHOD_NAME, endpoint); @@ -678,8 +640,7 @@ static Request getDataFrameAnalytics(GetDataFrameAnalyticsRequest getRequest) { } static Request getDataFrameAnalyticsStats(GetDataFrameAnalyticsStatsRequest getStatsRequest) { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml", "data_frame", "analytics") + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml", "data_frame", "analytics") .addPathPart(Strings.collectionToCommaDelimitedString(getStatsRequest.getIds())) .addPathPartAsIs("_stats") .build(); @@ -695,16 +656,17 @@ static Request getDataFrameAnalyticsStats(GetDataFrameAnalyticsStatsRequest getS } } if (getStatsRequest.getAllowNoMatch() != null) { - params.putParam(GetDataFrameAnalyticsStatsRequest.ALLOW_NO_MATCH.getPreferredName(), - Boolean.toString(getStatsRequest.getAllowNoMatch())); + params.putParam( + GetDataFrameAnalyticsStatsRequest.ALLOW_NO_MATCH.getPreferredName(), + Boolean.toString(getStatsRequest.getAllowNoMatch()) + ); } request.addParameters(params.asMap()); return request; } static Request startDataFrameAnalytics(StartDataFrameAnalyticsRequest startRequest) { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml", "data_frame", "analytics") + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml", "data_frame", "analytics") .addPathPart(startRequest.getId()) .addPathPartAsIs("_start") .build(); @@ -718,8 +680,7 @@ static Request startDataFrameAnalytics(StartDataFrameAnalyticsRequest startReque } static Request stopDataFrameAnalytics(StopDataFrameAnalyticsRequest stopRequest) { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml", "data_frame", "analytics") + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml", "data_frame", "analytics") .addPathPart(stopRequest.getId()) .addPathPartAsIs("_stop") .build(); @@ -730,7 +691,9 @@ static Request stopDataFrameAnalytics(StopDataFrameAnalyticsRequest stopRequest) } if (stopRequest.getAllowNoMatch() != null) { params.putParam( - StopDataFrameAnalyticsRequest.ALLOW_NO_MATCH.getPreferredName(), Boolean.toString(stopRequest.getAllowNoMatch())); + StopDataFrameAnalyticsRequest.ALLOW_NO_MATCH.getPreferredName(), + Boolean.toString(stopRequest.getAllowNoMatch()) + ); } if (stopRequest.getForce() != null) { params.putParam(StopDataFrameAnalyticsRequest.FORCE.getPreferredName(), Boolean.toString(stopRequest.getForce())); @@ -740,8 +703,7 @@ static Request stopDataFrameAnalytics(StopDataFrameAnalyticsRequest stopRequest) } static Request deleteDataFrameAnalytics(DeleteDataFrameAnalyticsRequest deleteRequest) { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml", "data_frame", "analytics") + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml", "data_frame", "analytics") .addPathPart(deleteRequest.getId()) .build(); @@ -760,9 +722,7 @@ static Request deleteDataFrameAnalytics(DeleteDataFrameAnalyticsRequest deleteRe } static Request evaluateDataFrame(EvaluateDataFrameRequest evaluateRequest) throws IOException { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml", "data_frame", "_evaluate") - .build(); + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml", "data_frame", "_evaluate").build(); Request request = new Request(HttpPost.METHOD_NAME, endpoint); request.setEntity(createEntity(evaluateRequest, REQUEST_BODY_CONTENT_TYPE)); return request; @@ -783,8 +743,7 @@ static Request explainDataFrameAnalytics(ExplainDataFrameAnalyticsRequest explai } static Request getTrainedModels(GetTrainedModelsRequest getTrainedModelsRequest) { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml", "trained_models") + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml", "trained_models") .addPathPart(Strings.collectionToCommaDelimitedString(getTrainedModelsRequest.getIds())) .build(); RequestConverters.Params params = new RequestConverters.Params(); @@ -798,16 +757,19 @@ static Request getTrainedModels(GetTrainedModelsRequest getTrainedModelsRequest) } } if (getTrainedModelsRequest.getAllowNoMatch() != null) { - params.putParam(GetTrainedModelsRequest.ALLOW_NO_MATCH, - Boolean.toString(getTrainedModelsRequest.getAllowNoMatch())); + params.putParam(GetTrainedModelsRequest.ALLOW_NO_MATCH, Boolean.toString(getTrainedModelsRequest.getAllowNoMatch())); } if (getTrainedModelsRequest.getDecompressDefinition() != null) { - params.putParam(GetTrainedModelsRequest.DECOMPRESS_DEFINITION, - Boolean.toString(getTrainedModelsRequest.getDecompressDefinition())); + params.putParam( + GetTrainedModelsRequest.DECOMPRESS_DEFINITION, + Boolean.toString(getTrainedModelsRequest.getDecompressDefinition()) + ); } if (getTrainedModelsRequest.getIncludes().isEmpty() == false) { - params.putParam(GetTrainedModelsRequest.INCLUDE, - Strings.collectionToCommaDelimitedString(getTrainedModelsRequest.getIncludes())); + params.putParam( + GetTrainedModelsRequest.INCLUDE, + Strings.collectionToCommaDelimitedString(getTrainedModelsRequest.getIncludes()) + ); } if (getTrainedModelsRequest.getTags() != null) { params.putParam(GetTrainedModelsRequest.TAGS, Strings.collectionToCommaDelimitedString(getTrainedModelsRequest.getTags())); @@ -821,8 +783,7 @@ static Request getTrainedModels(GetTrainedModelsRequest getTrainedModelsRequest) } static Request getTrainedModelsStats(GetTrainedModelsStatsRequest getTrainedModelsStatsRequest) { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml", "trained_models") + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml", "trained_models") .addPathPart(Strings.collectionToCommaDelimitedString(getTrainedModelsStatsRequest.getIds())) .addPathPart("_stats") .build(); @@ -837,8 +798,7 @@ static Request getTrainedModelsStats(GetTrainedModelsStatsRequest getTrainedMode } } if (getTrainedModelsStatsRequest.getAllowNoMatch() != null) { - params.putParam(GetTrainedModelsStatsRequest.ALLOW_NO_MATCH, - Boolean.toString(getTrainedModelsStatsRequest.getAllowNoMatch())); + params.putParam(GetTrainedModelsStatsRequest.ALLOW_NO_MATCH, Boolean.toString(getTrainedModelsStatsRequest.getAllowNoMatch())); } Request request = new Request(HttpGet.METHOD_NAME, endpoint); request.addParameters(params.asMap()); @@ -846,16 +806,12 @@ static Request getTrainedModelsStats(GetTrainedModelsStatsRequest getTrainedMode } static Request deleteTrainedModel(DeleteTrainedModelRequest deleteRequest) { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml", "trained_models") - .addPathPart(deleteRequest.getId()) - .build(); + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml", "trained_models").addPathPart(deleteRequest.getId()).build(); return new Request(HttpDelete.METHOD_NAME, endpoint); } static Request putTrainedModel(PutTrainedModelRequest putTrainedModelRequest) throws IOException { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml", "trained_models") + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml", "trained_models") .addPathPart(putTrainedModelRequest.getTrainedModelConfig().getModelId()) .build(); Request request = new Request(HttpPut.METHOD_NAME, endpoint); @@ -864,8 +820,7 @@ static Request putTrainedModel(PutTrainedModelRequest putTrainedModelRequest) th } static Request putTrainedModelAlias(PutTrainedModelAliasRequest putTrainedModelAliasRequest) throws IOException { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml", "trained_models") + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml", "trained_models") .addPathPart(putTrainedModelAliasRequest.getModelId()) .addPathPartAsIs("model_aliases") .addPathPart(putTrainedModelAliasRequest.getModelAlias()) @@ -880,8 +835,7 @@ static Request putTrainedModelAlias(PutTrainedModelAliasRequest putTrainedModelA } static Request deleteTrainedModelAlias(DeleteTrainedModelAliasRequest deleteTrainedModelAliasRequest) throws IOException { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml", "trained_models") + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml", "trained_models") .addPathPart(deleteTrainedModelAliasRequest.getModelId()) .addPathPartAsIs("model_aliases") .addPathPart(deleteTrainedModelAliasRequest.getModelAlias()) @@ -890,8 +844,7 @@ static Request deleteTrainedModelAlias(DeleteTrainedModelAliasRequest deleteTrai } static Request putFilter(PutFilterRequest putFilterRequest) throws IOException { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml") + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml") .addPathPartAsIs("filters") .addPathPart(putFilterRequest.getMlFilter().getId()) .build(); @@ -901,8 +854,7 @@ static Request putFilter(PutFilterRequest putFilterRequest) throws IOException { } static Request getFilter(GetFiltersRequest getFiltersRequest) { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml") + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml") .addPathPartAsIs("filters") .addPathPart(getFiltersRequest.getFilterId()) .build(); @@ -919,8 +871,7 @@ static Request getFilter(GetFiltersRequest getFiltersRequest) { } static Request updateFilter(UpdateFilterRequest updateFilterRequest) throws IOException { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml") + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml") .addPathPartAsIs("filters") .addPathPart(updateFilterRequest.getFilterId()) .addPathPartAsIs("_update") @@ -931,10 +882,7 @@ static Request updateFilter(UpdateFilterRequest updateFilterRequest) throws IOEx } static Request deleteFilter(DeleteFilterRequest deleteFilterRequest) { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml", "filters") - .addPathPart(deleteFilterRequest.getId()) - .build(); + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml", "filters").addPathPart(deleteFilterRequest.getId()).build(); Request request = new Request(HttpDelete.METHOD_NAME, endpoint); return request; } @@ -952,9 +900,7 @@ static Request setUpgradeMode(SetUpgradeModeRequest setUpgradeModeRequest) { } static Request mlInfo(MlInfoRequest infoRequest) { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_ml", "info") - .build(); + String endpoint = new EndpointBuilder().addPathPartAsIs("_ml", "info").build(); return new Request(HttpGet.METHOD_NAME, endpoint); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/MachineLearningClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/MachineLearningClient.java index 4daa42349a0f2..1272ca1658578 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/MachineLearningClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/MachineLearningClient.java @@ -11,12 +11,6 @@ import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.client.ml.CloseJobRequest; import org.elasticsearch.client.ml.CloseJobResponse; -import org.elasticsearch.client.ml.DeleteTrainedModelAliasRequest; -import org.elasticsearch.client.ml.DeleteTrainedModelRequest; -import org.elasticsearch.client.ml.EstimateModelMemoryRequest; -import org.elasticsearch.client.ml.EstimateModelMemoryResponse; -import org.elasticsearch.client.ml.ExplainDataFrameAnalyticsRequest; -import org.elasticsearch.client.ml.ExplainDataFrameAnalyticsResponse; import org.elasticsearch.client.ml.DeleteCalendarEventRequest; import org.elasticsearch.client.ml.DeleteCalendarJobRequest; import org.elasticsearch.client.ml.DeleteCalendarRequest; @@ -29,8 +23,14 @@ import org.elasticsearch.client.ml.DeleteJobRequest; import org.elasticsearch.client.ml.DeleteJobResponse; import org.elasticsearch.client.ml.DeleteModelSnapshotRequest; +import org.elasticsearch.client.ml.DeleteTrainedModelAliasRequest; +import org.elasticsearch.client.ml.DeleteTrainedModelRequest; +import org.elasticsearch.client.ml.EstimateModelMemoryRequest; +import org.elasticsearch.client.ml.EstimateModelMemoryResponse; import org.elasticsearch.client.ml.EvaluateDataFrameRequest; import org.elasticsearch.client.ml.EvaluateDataFrameResponse; +import org.elasticsearch.client.ml.ExplainDataFrameAnalyticsRequest; +import org.elasticsearch.client.ml.ExplainDataFrameAnalyticsResponse; import org.elasticsearch.client.ml.FlushJobRequest; import org.elasticsearch.client.ml.FlushJobResponse; import org.elasticsearch.client.ml.ForecastJobRequest; @@ -117,7 +117,6 @@ import java.io.IOException; import java.util.Collections; - /** * Machine Learning API client wrapper for the {@link RestHighLevelClient} *

@@ -150,11 +149,13 @@ public final class MachineLearningClient { * @throws IOException when there is a serialization issue sending the request or receiving the response */ public PutJobResponse putJob(PutJobRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, - MLRequestConverters::putJob, - options, - PutJobResponse::fromXContent, - Collections.emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + MLRequestConverters::putJob, + options, + PutJobResponse::fromXContent, + Collections.emptySet() + ); } /** @@ -168,12 +169,14 @@ public PutJobResponse putJob(PutJobRequest request, RequestOptions options) thro * @return cancellable that may be used to cancel the request */ public Cancellable putJobAsync(PutJobRequest request, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, - MLRequestConverters::putJob, - options, - PutJobResponse::fromXContent, - listener, - Collections.emptySet()); + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + MLRequestConverters::putJob, + options, + PutJobResponse::fromXContent, + listener, + Collections.emptySet() + ); } /** @@ -189,11 +192,13 @@ public Cancellable putJobAsync(PutJobRequest request, RequestOptions options, Ac * @throws IOException when there is a serialization issue sending the request or receiving the response */ public GetJobResponse getJob(GetJobRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, - MLRequestConverters::getJob, - options, - GetJobResponse::fromXContent, - Collections.emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + MLRequestConverters::getJob, + options, + GetJobResponse::fromXContent, + Collections.emptySet() + ); } /** @@ -207,12 +212,14 @@ public GetJobResponse getJob(GetJobRequest request, RequestOptions options) thro * @return cancellable that may be used to cancel the request */ public Cancellable getJobAsync(GetJobRequest request, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, - MLRequestConverters::getJob, - options, - GetJobResponse::fromXContent, - listener, - Collections.emptySet()); + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + MLRequestConverters::getJob, + options, + GetJobResponse::fromXContent, + listener, + Collections.emptySet() + ); } /** @@ -228,11 +235,13 @@ public Cancellable getJobAsync(GetJobRequest request, RequestOptions options, Ac * @throws IOException when there is a serialization issue sending the request or receiving the response */ public GetJobStatsResponse getJobStats(GetJobStatsRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, - MLRequestConverters::getJobStats, - options, - GetJobStatsResponse::fromXContent, - Collections.emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + MLRequestConverters::getJobStats, + options, + GetJobStatsResponse::fromXContent, + Collections.emptySet() + ); } /** @@ -246,12 +255,14 @@ public GetJobStatsResponse getJobStats(GetJobStatsRequest request, RequestOption * @return cancellable that may be used to cancel the request */ public Cancellable getJobStatsAsync(GetJobStatsRequest request, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, - MLRequestConverters::getJobStats, - options, - GetJobStatsResponse::fromXContent, - listener, - Collections.emptySet()); + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + MLRequestConverters::getJobStats, + options, + GetJobStatsResponse::fromXContent, + listener, + Collections.emptySet() + ); } /** @@ -268,11 +279,13 @@ public Cancellable getJobStatsAsync(GetJobStatsRequest request, RequestOptions o * @throws IOException when there is a serialization issue sending the request or receiving the response */ public DeleteExpiredDataResponse deleteExpiredData(DeleteExpiredDataRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + return restHighLevelClient.performRequestAndParseEntity( + request, MLRequestConverters::deleteExpiredData, options, DeleteExpiredDataResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -286,14 +299,19 @@ public DeleteExpiredDataResponse deleteExpiredData(DeleteExpiredDataRequest requ * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable deleteExpiredDataAsync(DeleteExpiredDataRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + public Cancellable deleteExpiredDataAsync( + DeleteExpiredDataRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, MLRequestConverters::deleteExpiredData, options, DeleteExpiredDataResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -309,11 +327,13 @@ public Cancellable deleteExpiredDataAsync(DeleteExpiredDataRequest request, Requ * @throws IOException when there is a serialization issue sending the request or receiving the response */ public DeleteJobResponse deleteJob(DeleteJobRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + return restHighLevelClient.performRequestAndParseEntity( + request, MLRequestConverters::deleteJob, options, DeleteJobResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -328,12 +348,14 @@ public DeleteJobResponse deleteJob(DeleteJobRequest request, RequestOptions opti * @return cancellable that may be used to cancel the request */ public Cancellable deleteJobAsync(DeleteJobRequest request, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, MLRequestConverters::deleteJob, options, DeleteJobResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -351,11 +373,13 @@ public Cancellable deleteJobAsync(DeleteJobRequest request, RequestOptions optio * @throws IOException when there is a serialization issue sending the request or receiving the response */ public OpenJobResponse openJob(OpenJobRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, - MLRequestConverters::openJob, - options, - OpenJobResponse::fromXContent, - Collections.emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + MLRequestConverters::openJob, + options, + OpenJobResponse::fromXContent, + Collections.emptySet() + ); } /** @@ -373,12 +397,14 @@ public OpenJobResponse openJob(OpenJobRequest request, RequestOptions options) t * @return cancellable that may be used to cancel the request */ public Cancellable openJobAsync(OpenJobRequest request, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, - MLRequestConverters::openJob, - options, - OpenJobResponse::fromXContent, - listener, - Collections.emptySet()); + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + MLRequestConverters::openJob, + options, + OpenJobResponse::fromXContent, + listener, + Collections.emptySet() + ); } /** @@ -394,11 +420,13 @@ public Cancellable openJobAsync(OpenJobRequest request, RequestOptions options, * @throws IOException when there is a serialization issue sending the request or receiving the response */ public CloseJobResponse closeJob(CloseJobRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, - MLRequestConverters::closeJob, - options, - CloseJobResponse::fromXContent, - Collections.emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + MLRequestConverters::closeJob, + options, + CloseJobResponse::fromXContent, + Collections.emptySet() + ); } /** @@ -414,12 +442,14 @@ public CloseJobResponse closeJob(CloseJobRequest request, RequestOptions options * @return cancellable that may be used to cancel the request */ public Cancellable closeJobAsync(CloseJobRequest request, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, - MLRequestConverters::closeJob, - options, - CloseJobResponse::fromXContent, - listener, - Collections.emptySet()); + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + MLRequestConverters::closeJob, + options, + CloseJobResponse::fromXContent, + listener, + Collections.emptySet() + ); } /** @@ -439,11 +469,13 @@ public Cancellable closeJobAsync(CloseJobRequest request, RequestOptions options * @throws IOException when there is a serialization issue sending the request or receiving the response */ public FlushJobResponse flushJob(FlushJobRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, - MLRequestConverters::flushJob, - options, - FlushJobResponse::fromXContent, - Collections.emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + MLRequestConverters::flushJob, + options, + FlushJobResponse::fromXContent, + Collections.emptySet() + ); } /** @@ -464,12 +496,14 @@ public FlushJobResponse flushJob(FlushJobRequest request, RequestOptions options * @return cancellable that may be used to cancel the request */ public Cancellable flushJobAsync(FlushJobRequest request, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, - MLRequestConverters::flushJob, - options, - FlushJobResponse::fromXContent, - listener, - Collections.emptySet()); + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + MLRequestConverters::flushJob, + options, + FlushJobResponse::fromXContent, + listener, + Collections.emptySet() + ); } /** @@ -485,11 +519,13 @@ public Cancellable flushJobAsync(FlushJobRequest request, RequestOptions options * @throws IOException when there is a serialization issue sending the request or receiving the response */ public ForecastJobResponse forecastJob(ForecastJobRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, - MLRequestConverters::forecastJob, - options, - ForecastJobResponse::fromXContent, - Collections.emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + MLRequestConverters::forecastJob, + options, + ForecastJobResponse::fromXContent, + Collections.emptySet() + ); } /** @@ -505,12 +541,14 @@ public ForecastJobResponse forecastJob(ForecastJobRequest request, RequestOption * @return cancellable that may be used to cancel the request */ public Cancellable forecastJobAsync(ForecastJobRequest request, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, - MLRequestConverters::forecastJob, - options, - ForecastJobResponse::fromXContent, - listener, - Collections.emptySet()); + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + MLRequestConverters::forecastJob, + options, + ForecastJobResponse::fromXContent, + listener, + Collections.emptySet() + ); } /** @@ -526,11 +564,13 @@ public Cancellable forecastJobAsync(ForecastJobRequest request, RequestOptions o * @throws IOException when there is a serialization issue sending the request or receiving the response */ public AcknowledgedResponse deleteForecast(DeleteForecastRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, - MLRequestConverters::deleteForecast, - options, - AcknowledgedResponse::fromXContent, - Collections.emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + MLRequestConverters::deleteForecast, + options, + AcknowledgedResponse::fromXContent, + Collections.emptySet() + ); } /** @@ -545,14 +585,19 @@ public AcknowledgedResponse deleteForecast(DeleteForecastRequest request, Reques * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable deleteForecastAsync(DeleteForecastRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, - MLRequestConverters::deleteForecast, - options, - AcknowledgedResponse::fromXContent, - listener, - Collections.emptySet()); + public Cancellable deleteForecastAsync( + DeleteForecastRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + MLRequestConverters::deleteForecast, + options, + AcknowledgedResponse::fromXContent, + listener, + Collections.emptySet() + ); } /** @@ -568,11 +613,13 @@ public Cancellable deleteForecastAsync(DeleteForecastRequest request, RequestOpt * @throws IOException when there is a serialization issue sending the request or receiving the response */ public AcknowledgedResponse deleteModelSnapshot(DeleteModelSnapshotRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + return restHighLevelClient.performRequestAndParseEntity( + request, MLRequestConverters::deleteModelSnapshot, options, AcknowledgedResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -587,14 +634,19 @@ public AcknowledgedResponse deleteModelSnapshot(DeleteModelSnapshotRequest reque * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable deleteModelSnapshotAsync(DeleteModelSnapshotRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + public Cancellable deleteModelSnapshotAsync( + DeleteModelSnapshotRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, MLRequestConverters::deleteModelSnapshot, options, AcknowledgedResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -610,11 +662,13 @@ public Cancellable deleteModelSnapshotAsync(DeleteModelSnapshotRequest request, * @throws IOException when there is a serialization issue sending the request or receiving the response */ public RevertModelSnapshotResponse revertModelSnapshot(RevertModelSnapshotRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + return restHighLevelClient.performRequestAndParseEntity( + request, MLRequestConverters::revertModelSnapshot, options, RevertModelSnapshotResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -629,14 +683,19 @@ public RevertModelSnapshotResponse revertModelSnapshot(RevertModelSnapshotReques * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable revertModelSnapshotAsync(RevertModelSnapshotRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + public Cancellable revertModelSnapshotAsync( + RevertModelSnapshotRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, MLRequestConverters::revertModelSnapshot, options, RevertModelSnapshotResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -651,11 +710,13 @@ public Cancellable revertModelSnapshotAsync(RevertModelSnapshotRequest request, * @throws IOException when there is a serialization issue sending the request or receiving the response */ public PutDatafeedResponse putDatafeed(PutDatafeedRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, - MLRequestConverters::putDatafeed, - options, - PutDatafeedResponse::fromXContent, - Collections.emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + MLRequestConverters::putDatafeed, + options, + PutDatafeedResponse::fromXContent, + Collections.emptySet() + ); } /** @@ -670,12 +731,14 @@ public PutDatafeedResponse putDatafeed(PutDatafeedRequest request, RequestOption * @return cancellable that may be used to cancel the request */ public Cancellable putDatafeedAsync(PutDatafeedRequest request, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, - MLRequestConverters::putDatafeed, - options, - PutDatafeedResponse::fromXContent, - listener, - Collections.emptySet()); + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + MLRequestConverters::putDatafeed, + options, + PutDatafeedResponse::fromXContent, + listener, + Collections.emptySet() + ); } /** @@ -691,11 +754,13 @@ public Cancellable putDatafeedAsync(PutDatafeedRequest request, RequestOptions o * @throws IOException when there is a serialization issue sending the request or receiving the response */ public PutDatafeedResponse updateDatafeed(UpdateDatafeedRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + return restHighLevelClient.performRequestAndParseEntity( + request, MLRequestConverters::updateDatafeed, options, PutDatafeedResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -710,14 +775,19 @@ public PutDatafeedResponse updateDatafeed(UpdateDatafeedRequest request, Request * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable updateDatafeedAsync(UpdateDatafeedRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + public Cancellable updateDatafeedAsync( + UpdateDatafeedRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, MLRequestConverters::updateDatafeed, options, PutDatafeedResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -734,11 +804,13 @@ public Cancellable updateDatafeedAsync(UpdateDatafeedRequest request, RequestOpt * @throws IOException when there is a serialization issue sending the request or receiving the response */ public GetDatafeedResponse getDatafeed(GetDatafeedRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, - MLRequestConverters::getDatafeed, - options, - GetDatafeedResponse::fromXContent, - Collections.emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + MLRequestConverters::getDatafeed, + options, + GetDatafeedResponse::fromXContent, + Collections.emptySet() + ); } /** @@ -753,14 +825,15 @@ public GetDatafeedResponse getDatafeed(GetDatafeedRequest request, RequestOption * @param listener Listener to be notified with {@link GetDatafeedResponse} upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getDatafeedAsync(GetDatafeedRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, - MLRequestConverters::getDatafeed, - options, - GetDatafeedResponse::fromXContent, - listener, - Collections.emptySet()); + public Cancellable getDatafeedAsync(GetDatafeedRequest request, RequestOptions options, ActionListener listener) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + MLRequestConverters::getDatafeed, + options, + GetDatafeedResponse::fromXContent, + listener, + Collections.emptySet() + ); } /** @@ -776,11 +849,13 @@ public Cancellable getDatafeedAsync(GetDatafeedRequest request, RequestOptions o * @throws IOException when there is a serialization issue sending the request or receiving the response */ public AcknowledgedResponse deleteDatafeed(DeleteDatafeedRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, - MLRequestConverters::deleteDatafeed, - options, - AcknowledgedResponse::fromXContent, - Collections.emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + MLRequestConverters::deleteDatafeed, + options, + AcknowledgedResponse::fromXContent, + Collections.emptySet() + ); } /** @@ -795,14 +870,19 @@ public AcknowledgedResponse deleteDatafeed(DeleteDatafeedRequest request, Reques * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable deleteDatafeedAsync(DeleteDatafeedRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, - MLRequestConverters::deleteDatafeed, - options, - AcknowledgedResponse::fromXContent, - listener, - Collections.emptySet()); + public Cancellable deleteDatafeedAsync( + DeleteDatafeedRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + MLRequestConverters::deleteDatafeed, + options, + AcknowledgedResponse::fromXContent, + listener, + Collections.emptySet() + ); } /** @@ -818,11 +898,13 @@ public Cancellable deleteDatafeedAsync(DeleteDatafeedRequest request, RequestOpt * @throws IOException when there is a serialization issue sending the request or receiving the response */ public StartDatafeedResponse startDatafeed(StartDatafeedRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + return restHighLevelClient.performRequestAndParseEntity( + request, MLRequestConverters::startDatafeed, options, StartDatafeedResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -837,14 +919,19 @@ public StartDatafeedResponse startDatafeed(StartDatafeedRequest request, Request * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable startDatafeedAsync(StartDatafeedRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + public Cancellable startDatafeedAsync( + StartDatafeedRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, MLRequestConverters::startDatafeed, options, StartDatafeedResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -860,11 +947,13 @@ public Cancellable startDatafeedAsync(StartDatafeedRequest request, RequestOptio * @throws IOException when there is a serialization issue sending the request or receiving the response */ public StopDatafeedResponse stopDatafeed(StopDatafeedRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + return restHighLevelClient.performRequestAndParseEntity( + request, MLRequestConverters::stopDatafeed, options, StopDatafeedResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -879,14 +968,19 @@ public StopDatafeedResponse stopDatafeed(StopDatafeedRequest request, RequestOpt * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable stopDatafeedAsync(StopDatafeedRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + public Cancellable stopDatafeedAsync( + StopDatafeedRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, MLRequestConverters::stopDatafeed, options, StopDatafeedResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -902,11 +996,13 @@ public Cancellable stopDatafeedAsync(StopDatafeedRequest request, RequestOptions * @throws IOException when there is a serialization issue sending the request or receiving the response */ public GetDatafeedStatsResponse getDatafeedStats(GetDatafeedStatsRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + return restHighLevelClient.performRequestAndParseEntity( + request, MLRequestConverters::getDatafeedStats, options, GetDatafeedStatsResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -923,11 +1019,13 @@ public GetDatafeedStatsResponse getDatafeedStats(GetDatafeedStatsRequest request * @throws IOException when there is a serialization issue sending the request or receiving the response */ public PreviewDatafeedResponse previewDatafeed(PreviewDatafeedRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + return restHighLevelClient.performRequestAndParseEntity( + request, MLRequestConverters::previewDatafeed, options, PreviewDatafeedResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -941,15 +1039,19 @@ public PreviewDatafeedResponse previewDatafeed(PreviewDatafeedRequest request, R * @param listener Listener to be notified with {@link GetDatafeedStatsResponse} upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getDatafeedStatsAsync(GetDatafeedStatsRequest request, - RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + public Cancellable getDatafeedStatsAsync( + GetDatafeedStatsRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, MLRequestConverters::getDatafeedStats, options, GetDatafeedStatsResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -964,15 +1066,19 @@ public Cancellable getDatafeedStatsAsync(GetDatafeedStatsRequest request, * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable previewDatafeedAsync(PreviewDatafeedRequest request, - RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + public Cancellable previewDatafeedAsync( + PreviewDatafeedRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, MLRequestConverters::previewDatafeed, options, PreviewDatafeedResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -987,11 +1093,13 @@ public Cancellable previewDatafeedAsync(PreviewDatafeedRequest request, * @throws IOException when there is a serialization issue sending the request or receiving the response */ public PutJobResponse updateJob(UpdateJobRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, - MLRequestConverters::updateJob, - options, - PutJobResponse::fromXContent, - Collections.emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + MLRequestConverters::updateJob, + options, + PutJobResponse::fromXContent, + Collections.emptySet() + ); } /** @@ -1006,12 +1114,14 @@ public PutJobResponse updateJob(UpdateJobRequest request, RequestOptions options * @return cancellable that may be used to cancel the request */ public Cancellable updateJobAsync(UpdateJobRequest request, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, - MLRequestConverters::updateJob, - options, - PutJobResponse::fromXContent, - listener, - Collections.emptySet()); + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + MLRequestConverters::updateJob, + options, + PutJobResponse::fromXContent, + listener, + Collections.emptySet() + ); } /** @@ -1024,11 +1134,13 @@ public Cancellable updateJobAsync(UpdateJobRequest request, RequestOptions optio * @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized */ public GetBucketsResponse getBuckets(GetBucketsRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, - MLRequestConverters::getBuckets, - options, - GetBucketsResponse::fromXContent, - Collections.emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + MLRequestConverters::getBuckets, + options, + GetBucketsResponse::fromXContent, + Collections.emptySet() + ); } /** @@ -1043,12 +1155,14 @@ public GetBucketsResponse getBuckets(GetBucketsRequest request, RequestOptions o * @return cancellable that may be used to cancel the request */ public Cancellable getBucketsAsync(GetBucketsRequest request, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, - MLRequestConverters::getBuckets, - options, - GetBucketsResponse::fromXContent, - listener, - Collections.emptySet()); + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + MLRequestConverters::getBuckets, + options, + GetBucketsResponse::fromXContent, + listener, + Collections.emptySet() + ); } /** @@ -1063,11 +1177,13 @@ public Cancellable getBucketsAsync(GetBucketsRequest request, RequestOptions opt * @throws IOException when there is a serialization issue sending the request or receiving the response */ public GetCategoriesResponse getCategories(GetCategoriesRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, - MLRequestConverters::getCategories, - options, - GetCategoriesResponse::fromXContent, - Collections.emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + MLRequestConverters::getCategories, + options, + GetCategoriesResponse::fromXContent, + Collections.emptySet() + ); } /** @@ -1082,14 +1198,19 @@ public GetCategoriesResponse getCategories(GetCategoriesRequest request, Request * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getCategoriesAsync(GetCategoriesRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, - MLRequestConverters::getCategories, - options, - GetCategoriesResponse::fromXContent, - listener, - Collections.emptySet()); + public Cancellable getCategoriesAsync( + GetCategoriesRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + MLRequestConverters::getCategories, + options, + GetCategoriesResponse::fromXContent, + listener, + Collections.emptySet() + ); } /** @@ -1104,11 +1225,13 @@ public Cancellable getCategoriesAsync(GetCategoriesRequest request, RequestOptio * @throws IOException when there is a serialization issue sending the request or receiving the response */ public GetModelSnapshotsResponse getModelSnapshots(GetModelSnapshotsRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + return restHighLevelClient.performRequestAndParseEntity( + request, MLRequestConverters::getModelSnapshots, options, GetModelSnapshotsResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -1123,14 +1246,19 @@ public GetModelSnapshotsResponse getModelSnapshots(GetModelSnapshotsRequest requ * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getModelSnapshotsAsync(GetModelSnapshotsRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + public Cancellable getModelSnapshotsAsync( + GetModelSnapshotsRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, MLRequestConverters::getModelSnapshots, options, GetModelSnapshotsResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -1144,13 +1272,14 @@ public Cancellable getModelSnapshotsAsync(GetModelSnapshotsRequest request, Requ * @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized * @throws IOException when there is a serialization issue sending the request or receiving the response */ - public UpdateModelSnapshotResponse updateModelSnapshot(UpdateModelSnapshotRequest request, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + public UpdateModelSnapshotResponse updateModelSnapshot(UpdateModelSnapshotRequest request, RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, MLRequestConverters::updateModelSnapshot, options, UpdateModelSnapshotResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -1165,14 +1294,19 @@ public UpdateModelSnapshotResponse updateModelSnapshot(UpdateModelSnapshotReques * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable updateModelSnapshotAsync(UpdateModelSnapshotRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + public Cancellable updateModelSnapshotAsync( + UpdateModelSnapshotRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, MLRequestConverters::updateModelSnapshot, options, UpdateModelSnapshotResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -1186,13 +1320,15 @@ public Cancellable updateModelSnapshotAsync(UpdateModelSnapshotRequest request, * @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized * @throws IOException when there is a serialization issue sending the request or receiving the response */ - public UpgradeJobModelSnapshotResponse upgradeJobSnapshot(UpgradeJobModelSnapshotRequest request, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + public UpgradeJobModelSnapshotResponse upgradeJobSnapshot(UpgradeJobModelSnapshotRequest request, RequestOptions options) + throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, MLRequestConverters::upgradeJobSnapshot, options, UpgradeJobModelSnapshotResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -1208,15 +1344,19 @@ public UpgradeJobModelSnapshotResponse upgradeJobSnapshot(UpgradeJobModelSnapsho * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable upgradeJobSnapshotAsync(UpgradeJobModelSnapshotRequest request, - RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + public Cancellable upgradeJobSnapshotAsync( + UpgradeJobModelSnapshotRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, MLRequestConverters::upgradeJobSnapshot, options, UpgradeJobModelSnapshotResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -1230,11 +1370,13 @@ public Cancellable upgradeJobSnapshotAsync(UpgradeJobModelSnapshotRequest reques * @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized */ public GetOverallBucketsResponse getOverallBuckets(GetOverallBucketsRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, - MLRequestConverters::getOverallBuckets, - options, - GetOverallBucketsResponse::fromXContent, - Collections.emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + MLRequestConverters::getOverallBuckets, + options, + GetOverallBucketsResponse::fromXContent, + Collections.emptySet() + ); } /** @@ -1249,14 +1391,19 @@ public GetOverallBucketsResponse getOverallBuckets(GetOverallBucketsRequest requ * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getOverallBucketsAsync(GetOverallBucketsRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, - MLRequestConverters::getOverallBuckets, - options, - GetOverallBucketsResponse::fromXContent, - listener, - Collections.emptySet()); + public Cancellable getOverallBucketsAsync( + GetOverallBucketsRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + MLRequestConverters::getOverallBuckets, + options, + GetOverallBucketsResponse::fromXContent, + listener, + Collections.emptySet() + ); } /** @@ -1269,11 +1416,13 @@ public Cancellable getOverallBucketsAsync(GetOverallBucketsRequest request, Requ * @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized */ public GetRecordsResponse getRecords(GetRecordsRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, - MLRequestConverters::getRecords, - options, - GetRecordsResponse::fromXContent, - Collections.emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + MLRequestConverters::getRecords, + options, + GetRecordsResponse::fromXContent, + Collections.emptySet() + ); } /** @@ -1288,12 +1437,14 @@ public GetRecordsResponse getRecords(GetRecordsRequest request, RequestOptions o * @return cancellable that may be used to cancel the request */ public Cancellable getRecordsAsync(GetRecordsRequest request, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, - MLRequestConverters::getRecords, - options, - GetRecordsResponse::fromXContent, - listener, - Collections.emptySet()); + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + MLRequestConverters::getRecords, + options, + GetRecordsResponse::fromXContent, + listener, + Collections.emptySet() + ); } /** @@ -1310,11 +1461,13 @@ public Cancellable getRecordsAsync(GetRecordsRequest request, RequestOptions opt * @throws IOException when there is a serialization issue sending the request or receiving the response */ public PostDataResponse postData(PostDataRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, - MLRequestConverters::postData, - options, - PostDataResponse::fromXContent, - Collections.emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + MLRequestConverters::postData, + options, + PostDataResponse::fromXContent, + Collections.emptySet() + ); } /** @@ -1331,12 +1484,14 @@ public PostDataResponse postData(PostDataRequest request, RequestOptions options * @return cancellable that may be used to cancel the request */ public Cancellable postDataAsync(PostDataRequest request, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, - MLRequestConverters::postData, - options, - PostDataResponse::fromXContent, - listener, - Collections.emptySet()); + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + MLRequestConverters::postData, + options, + PostDataResponse::fromXContent, + listener, + Collections.emptySet() + ); } /** @@ -1351,11 +1506,13 @@ public Cancellable postDataAsync(PostDataRequest request, RequestOptions options * objects and the number of calendars found */ public GetCalendarsResponse getCalendars(GetCalendarsRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, - MLRequestConverters::getCalendars, - options, - GetCalendarsResponse::fromXContent, - Collections.emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + MLRequestConverters::getCalendars, + options, + GetCalendarsResponse::fromXContent, + Collections.emptySet() + ); } /** @@ -1369,14 +1526,19 @@ public GetCalendarsResponse getCalendars(GetCalendarsRequest request, RequestOpt * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getCalendarsAsync(GetCalendarsRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, - MLRequestConverters::getCalendars, - options, - GetCalendarsResponse::fromXContent, - listener, - Collections.emptySet()); + public Cancellable getCalendarsAsync( + GetCalendarsRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + MLRequestConverters::getCalendars, + options, + GetCalendarsResponse::fromXContent, + listener, + Collections.emptySet() + ); } /** @@ -1390,11 +1552,13 @@ public Cancellable getCalendarsAsync(GetCalendarsRequest request, RequestOptions * @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized */ public GetInfluencersResponse getInfluencers(GetInfluencersRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, - MLRequestConverters::getInfluencers, - options, - GetInfluencersResponse::fromXContent, - Collections.emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + MLRequestConverters::getInfluencers, + options, + GetInfluencersResponse::fromXContent, + Collections.emptySet() + ); } /** @@ -1409,14 +1573,19 @@ public GetInfluencersResponse getInfluencers(GetInfluencersRequest request, Requ * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getInfluencersAsync(GetInfluencersRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, - MLRequestConverters::getInfluencers, - options, - GetInfluencersResponse::fromXContent, - listener, - Collections.emptySet()); + public Cancellable getInfluencersAsync( + GetInfluencersRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + MLRequestConverters::getInfluencers, + options, + GetInfluencersResponse::fromXContent, + listener, + Collections.emptySet() + ); } /** @@ -1432,11 +1601,13 @@ public Cancellable getInfluencersAsync(GetInfluencersRequest request, RequestOpt * @throws IOException when there is a serialization issue sending the request or receiving the response */ public PutCalendarResponse putCalendar(PutCalendarRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, - MLRequestConverters::putCalendar, - options, - PutCalendarResponse::fromXContent, - Collections.emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + MLRequestConverters::putCalendar, + options, + PutCalendarResponse::fromXContent, + Collections.emptySet() + ); } /** @@ -1452,12 +1623,14 @@ public PutCalendarResponse putCalendar(PutCalendarRequest request, RequestOption * @return cancellable that may be used to cancel the request */ public Cancellable putCalendarAsync(PutCalendarRequest request, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, - MLRequestConverters::putCalendar, - options, - PutCalendarResponse::fromXContent, - listener, - Collections.emptySet()); + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + MLRequestConverters::putCalendar, + options, + PutCalendarResponse::fromXContent, + listener, + Collections.emptySet() + ); } /** @@ -1473,11 +1646,13 @@ public Cancellable putCalendarAsync(PutCalendarRequest request, RequestOptions o * @throws IOException when there is a serialization issue sending the request or receiving the response */ public PutCalendarResponse putCalendarJob(PutCalendarJobRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + return restHighLevelClient.performRequestAndParseEntity( + request, MLRequestConverters::putCalendarJob, options, PutCalendarResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -1492,14 +1667,19 @@ public PutCalendarResponse putCalendarJob(PutCalendarJobRequest request, Request * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable putCalendarJobAsync(PutCalendarJobRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + public Cancellable putCalendarJobAsync( + PutCalendarJobRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, MLRequestConverters::putCalendarJob, options, PutCalendarResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -1515,11 +1695,13 @@ public Cancellable putCalendarJobAsync(PutCalendarJobRequest request, RequestOpt * @throws IOException when there is a serialization issue sending the request or receiving the response */ public PutCalendarResponse deleteCalendarJob(DeleteCalendarJobRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + return restHighLevelClient.performRequestAndParseEntity( + request, MLRequestConverters::deleteCalendarJob, options, PutCalendarResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -1534,15 +1716,19 @@ public PutCalendarResponse deleteCalendarJob(DeleteCalendarJobRequest request, R * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable deleteCalendarJobAsync(DeleteCalendarJobRequest request, - RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + public Cancellable deleteCalendarJobAsync( + DeleteCalendarJobRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, MLRequestConverters::deleteCalendarJob, options, PutCalendarResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -1558,11 +1744,13 @@ public Cancellable deleteCalendarJobAsync(DeleteCalendarJobRequest request, * @throws IOException when there is a serialization issue sending the request or receiving the response */ public AcknowledgedResponse deleteCalendar(DeleteCalendarRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, - MLRequestConverters::deleteCalendar, - options, - AcknowledgedResponse::fromXContent, - Collections.emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + MLRequestConverters::deleteCalendar, + options, + AcknowledgedResponse::fromXContent, + Collections.emptySet() + ); } /** @@ -1577,14 +1765,19 @@ public AcknowledgedResponse deleteCalendar(DeleteCalendarRequest request, Reques * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable deleteCalendarAsync(DeleteCalendarRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, - MLRequestConverters::deleteCalendar, - options, - AcknowledgedResponse::fromXContent, - listener, - Collections.emptySet()); + public Cancellable deleteCalendarAsync( + DeleteCalendarRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + MLRequestConverters::deleteCalendar, + options, + AcknowledgedResponse::fromXContent, + listener, + Collections.emptySet() + ); } /** @@ -1600,11 +1793,13 @@ public Cancellable deleteCalendarAsync(DeleteCalendarRequest request, RequestOpt * @throws IOException when there is a serialization issue sending the request or receiving the response */ public GetCalendarEventsResponse getCalendarEvents(GetCalendarEventsRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + return restHighLevelClient.performRequestAndParseEntity( + request, MLRequestConverters::getCalendarEvents, options, GetCalendarEventsResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -1619,14 +1814,19 @@ public GetCalendarEventsResponse getCalendarEvents(GetCalendarEventsRequest requ * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getCalendarEventsAsync(GetCalendarEventsRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + public Cancellable getCalendarEventsAsync( + GetCalendarEventsRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, MLRequestConverters::getCalendarEvents, options, GetCalendarEventsResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -1642,11 +1842,13 @@ public Cancellable getCalendarEventsAsync(GetCalendarEventsRequest request, Requ * @throws IOException when there is a serialization issue sending the request or receiving the response */ public PostCalendarEventResponse postCalendarEvent(PostCalendarEventRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + return restHighLevelClient.performRequestAndParseEntity( + request, MLRequestConverters::postCalendarEvents, options, PostCalendarEventResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -1661,14 +1863,19 @@ public PostCalendarEventResponse postCalendarEvent(PostCalendarEventRequest requ * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable postCalendarEventAsync(PostCalendarEventRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + public Cancellable postCalendarEventAsync( + PostCalendarEventRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, MLRequestConverters::postCalendarEvents, options, PostCalendarEventResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -1684,11 +1891,13 @@ public Cancellable postCalendarEventAsync(PostCalendarEventRequest request, Requ * @throws IOException when there is a serialization issue sending the request or receiving the response */ public AcknowledgedResponse deleteCalendarEvent(DeleteCalendarEventRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + return restHighLevelClient.performRequestAndParseEntity( + request, MLRequestConverters::deleteCalendarEvent, options, AcknowledgedResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -1703,15 +1912,19 @@ public AcknowledgedResponse deleteCalendarEvent(DeleteCalendarEventRequest reque * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable deleteCalendarEventAsync(DeleteCalendarEventRequest request, - RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + public Cancellable deleteCalendarEventAsync( + DeleteCalendarEventRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, MLRequestConverters::deleteCalendarEvent, options, AcknowledgedResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -1726,11 +1939,13 @@ public Cancellable deleteCalendarEventAsync(DeleteCalendarEventRequest request, * @throws IOException when there is a serialization issue sending the request or receiving the response */ public PutFilterResponse putFilter(PutFilterRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + return restHighLevelClient.performRequestAndParseEntity( + request, MLRequestConverters::putFilter, options, PutFilterResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -1745,12 +1960,14 @@ public PutFilterResponse putFilter(PutFilterRequest request, RequestOptions opti * @return cancellable that may be used to cancel the request */ public Cancellable putFilterAsync(PutFilterRequest request, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, MLRequestConverters::putFilter, options, PutFilterResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -1765,11 +1982,13 @@ public Cancellable putFilterAsync(PutFilterRequest request, RequestOptions optio * @throws IOException when there is a serialization issue sending the request or receiving the response */ public GetFiltersResponse getFilter(GetFiltersRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + return restHighLevelClient.performRequestAndParseEntity( + request, MLRequestConverters::getFilter, options, GetFiltersResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -1784,12 +2003,14 @@ public GetFiltersResponse getFilter(GetFiltersRequest request, RequestOptions op * @return cancellable that may be used to cancel the request */ public Cancellable getFilterAsync(GetFiltersRequest request, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, MLRequestConverters::getFilter, options, GetFiltersResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -1805,11 +2026,13 @@ public Cancellable getFilterAsync(GetFiltersRequest request, RequestOptions opti * @throws IOException when there is a serialization issue sending the request or receiving the response */ public PutFilterResponse updateFilter(UpdateFilterRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + return restHighLevelClient.performRequestAndParseEntity( + request, MLRequestConverters::updateFilter, options, PutFilterResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -1825,12 +2048,14 @@ public PutFilterResponse updateFilter(UpdateFilterRequest request, RequestOption * @return cancellable that may be used to cancel the request */ public Cancellable updateFilterAsync(UpdateFilterRequest request, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, MLRequestConverters::updateFilter, options, PutFilterResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -1846,11 +2071,13 @@ public Cancellable updateFilterAsync(UpdateFilterRequest request, RequestOptions * @throws IOException when there is a serialization issue sending the request or receiving the response */ public AcknowledgedResponse deleteFilter(DeleteFilterRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + return restHighLevelClient.performRequestAndParseEntity( + request, MLRequestConverters::deleteFilter, options, AcknowledgedResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -1865,14 +2092,19 @@ public AcknowledgedResponse deleteFilter(DeleteFilterRequest request, RequestOpt * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable deleteFilterAsync(DeleteFilterRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + public Cancellable deleteFilterAsync( + DeleteFilterRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, MLRequestConverters::deleteFilter, options, AcknowledgedResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -1887,11 +2119,13 @@ public Cancellable deleteFilterAsync(DeleteFilterRequest request, RequestOptions * @throws IOException when there is a serialization issue sending the request or receiving the response */ public MlInfoResponse getMlInfo(MlInfoRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + return restHighLevelClient.performRequestAndParseEntity( + request, MLRequestConverters::mlInfo, options, MlInfoResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -1906,12 +2140,14 @@ public MlInfoResponse getMlInfo(MlInfoRequest request, RequestOptions options) t * @return cancellable that may be used to cancel the request */ public Cancellable getMlInfoAsync(MlInfoRequest request, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, MLRequestConverters::mlInfo, options, MlInfoResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -1926,11 +2162,13 @@ public Cancellable getMlInfoAsync(MlInfoRequest request, RequestOptions options, * @throws IOException when there is a serialization issue sending the request or receiving the response */ public AcknowledgedResponse setUpgradeMode(SetUpgradeModeRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + return restHighLevelClient.performRequestAndParseEntity( + request, MLRequestConverters::setUpgradeMode, options, AcknowledgedResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -1944,14 +2182,19 @@ public AcknowledgedResponse setUpgradeMode(SetUpgradeModeRequest request, Reques * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable setUpgradeModeAsync(SetUpgradeModeRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + public Cancellable setUpgradeModeAsync( + SetUpgradeModeRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, MLRequestConverters::setUpgradeMode, options, AcknowledgedResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -1964,13 +2207,14 @@ public Cancellable setUpgradeModeAsync(SetUpgradeModeRequest request, RequestOpt * @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized * @return {@link EstimateModelMemoryResponse} response object */ - public EstimateModelMemoryResponse estimateModelMemory(EstimateModelMemoryRequest request, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + public EstimateModelMemoryResponse estimateModelMemory(EstimateModelMemoryRequest request, RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, MLRequestConverters::estimateModelMemory, options, EstimateModelMemoryResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -1985,15 +2229,19 @@ public EstimateModelMemoryResponse estimateModelMemory(EstimateModelMemoryReques * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable estimateModelMemoryAsync(EstimateModelMemoryRequest request, - RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + public Cancellable estimateModelMemoryAsync( + EstimateModelMemoryRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, MLRequestConverters::estimateModelMemory, options, EstimateModelMemoryResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -2010,13 +2258,15 @@ public Cancellable estimateModelMemoryAsync(EstimateModelMemoryRequest request, * {@link org.elasticsearch.client.ml.dataframe.DataFrameAnalyticsConfig} * @throws IOException when there is a serialization issue sending the request or receiving the response */ - public PutDataFrameAnalyticsResponse putDataFrameAnalytics(PutDataFrameAnalyticsRequest request, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + public PutDataFrameAnalyticsResponse putDataFrameAnalytics(PutDataFrameAnalyticsRequest request, RequestOptions options) + throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, MLRequestConverters::putDataFrameAnalytics, options, PutDataFrameAnalyticsResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -2032,14 +2282,19 @@ public PutDataFrameAnalyticsResponse putDataFrameAnalytics(PutDataFrameAnalytics * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable putDataFrameAnalyticsAsync(PutDataFrameAnalyticsRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + public Cancellable putDataFrameAnalyticsAsync( + PutDataFrameAnalyticsRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, MLRequestConverters::putDataFrameAnalytics, options, PutDataFrameAnalyticsResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -2056,13 +2311,15 @@ public Cancellable putDataFrameAnalyticsAsync(PutDataFrameAnalyticsRequest reque * {@link org.elasticsearch.client.ml.dataframe.DataFrameAnalyticsConfig} * @throws IOException when there is a serialization issue sending the request or receiving the response */ - public PutDataFrameAnalyticsResponse updateDataFrameAnalytics(UpdateDataFrameAnalyticsRequest request, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + public PutDataFrameAnalyticsResponse updateDataFrameAnalytics(UpdateDataFrameAnalyticsRequest request, RequestOptions options) + throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, MLRequestConverters::updateDataFrameAnalytics, options, PutDataFrameAnalyticsResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -2078,14 +2335,19 @@ public PutDataFrameAnalyticsResponse updateDataFrameAnalytics(UpdateDataFrameAna * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable updateDataFrameAnalyticsAsync(UpdateDataFrameAnalyticsRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + public Cancellable updateDataFrameAnalyticsAsync( + UpdateDataFrameAnalyticsRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, MLRequestConverters::updateDataFrameAnalytics, options, PutDataFrameAnalyticsResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -2100,13 +2362,15 @@ public Cancellable updateDataFrameAnalyticsAsync(UpdateDataFrameAnalyticsRequest * @return {@link GetDataFrameAnalyticsResponse} response object containing the * {@link org.elasticsearch.client.ml.dataframe.DataFrameAnalyticsConfig} objects */ - public GetDataFrameAnalyticsResponse getDataFrameAnalytics(GetDataFrameAnalyticsRequest request, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + public GetDataFrameAnalyticsResponse getDataFrameAnalytics(GetDataFrameAnalyticsRequest request, RequestOptions options) + throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, MLRequestConverters::getDataFrameAnalytics, options, GetDataFrameAnalyticsResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -2121,14 +2385,19 @@ public GetDataFrameAnalyticsResponse getDataFrameAnalytics(GetDataFrameAnalytics * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getDataFrameAnalyticsAsync(GetDataFrameAnalyticsRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + public Cancellable getDataFrameAnalyticsAsync( + GetDataFrameAnalyticsRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, MLRequestConverters::getDataFrameAnalytics, options, GetDataFrameAnalyticsResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -2142,13 +2411,15 @@ public Cancellable getDataFrameAnalyticsAsync(GetDataFrameAnalyticsRequest reque * @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized * @return {@link GetDataFrameAnalyticsStatsResponse} response object */ - public GetDataFrameAnalyticsStatsResponse getDataFrameAnalyticsStats(GetDataFrameAnalyticsStatsRequest request, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + public GetDataFrameAnalyticsStatsResponse getDataFrameAnalyticsStats(GetDataFrameAnalyticsStatsRequest request, RequestOptions options) + throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, MLRequestConverters::getDataFrameAnalyticsStats, options, GetDataFrameAnalyticsStatsResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -2163,14 +2434,19 @@ public GetDataFrameAnalyticsStatsResponse getDataFrameAnalyticsStats(GetDataFram * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getDataFrameAnalyticsStatsAsync(GetDataFrameAnalyticsStatsRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + public Cancellable getDataFrameAnalyticsStatsAsync( + GetDataFrameAnalyticsStatsRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, MLRequestConverters::getDataFrameAnalyticsStats, options, GetDataFrameAnalyticsStatsResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -2185,13 +2461,15 @@ public Cancellable getDataFrameAnalyticsStatsAsync(GetDataFrameAnalyticsStatsReq * @return action acknowledgement * @throws IOException when there is a serialization issue sending the request or receiving the response */ - public StartDataFrameAnalyticsResponse startDataFrameAnalytics(StartDataFrameAnalyticsRequest request, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + public StartDataFrameAnalyticsResponse startDataFrameAnalytics(StartDataFrameAnalyticsRequest request, RequestOptions options) + throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, MLRequestConverters::startDataFrameAnalytics, options, StartDataFrameAnalyticsResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -2206,14 +2484,19 @@ public StartDataFrameAnalyticsResponse startDataFrameAnalytics(StartDataFrameAna * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable startDataFrameAnalyticsAsync(StartDataFrameAnalyticsRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + public Cancellable startDataFrameAnalyticsAsync( + StartDataFrameAnalyticsRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, MLRequestConverters::startDataFrameAnalytics, options, StartDataFrameAnalyticsResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -2228,13 +2511,15 @@ public Cancellable startDataFrameAnalyticsAsync(StartDataFrameAnalyticsRequest r * @return {@link StopDataFrameAnalyticsResponse} * @throws IOException when there is a serialization issue sending the request or receiving the response */ - public StopDataFrameAnalyticsResponse stopDataFrameAnalytics(StopDataFrameAnalyticsRequest request, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + public StopDataFrameAnalyticsResponse stopDataFrameAnalytics(StopDataFrameAnalyticsRequest request, RequestOptions options) + throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, MLRequestConverters::stopDataFrameAnalytics, options, StopDataFrameAnalyticsResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -2249,14 +2534,19 @@ public StopDataFrameAnalyticsResponse stopDataFrameAnalytics(StopDataFrameAnalyt * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable stopDataFrameAnalyticsAsync(StopDataFrameAnalyticsRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + public Cancellable stopDataFrameAnalyticsAsync( + StopDataFrameAnalyticsRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, MLRequestConverters::stopDataFrameAnalytics, options, StopDataFrameAnalyticsResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -2271,13 +2561,15 @@ public Cancellable stopDataFrameAnalyticsAsync(StopDataFrameAnalyticsRequest req * @return action acknowledgement * @throws IOException when there is a serialization issue sending the request or receiving the response */ - public AcknowledgedResponse deleteDataFrameAnalytics(DeleteDataFrameAnalyticsRequest request, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + public AcknowledgedResponse deleteDataFrameAnalytics(DeleteDataFrameAnalyticsRequest request, RequestOptions options) + throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, MLRequestConverters::deleteDataFrameAnalytics, options, AcknowledgedResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -2292,14 +2584,19 @@ public AcknowledgedResponse deleteDataFrameAnalytics(DeleteDataFrameAnalyticsReq * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable deleteDataFrameAnalyticsAsync(DeleteDataFrameAnalyticsRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + public Cancellable deleteDataFrameAnalyticsAsync( + DeleteDataFrameAnalyticsRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, MLRequestConverters::deleteDataFrameAnalytics, options, AcknowledgedResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -2314,13 +2611,14 @@ public Cancellable deleteDataFrameAnalyticsAsync(DeleteDataFrameAnalyticsRequest * @return {@link EvaluateDataFrameResponse} response object * @throws IOException when there is a serialization issue sending the request or receiving the response */ - public EvaluateDataFrameResponse evaluateDataFrame(EvaluateDataFrameRequest request, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + public EvaluateDataFrameResponse evaluateDataFrame(EvaluateDataFrameRequest request, RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, MLRequestConverters::evaluateDataFrame, options, EvaluateDataFrameResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -2335,14 +2633,19 @@ public EvaluateDataFrameResponse evaluateDataFrame(EvaluateDataFrameRequest requ * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable evaluateDataFrameAsync(EvaluateDataFrameRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + public Cancellable evaluateDataFrameAsync( + EvaluateDataFrameRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, MLRequestConverters::evaluateDataFrame, options, EvaluateDataFrameResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -2357,14 +2660,15 @@ public Cancellable evaluateDataFrameAsync(EvaluateDataFrameRequest request, Requ * @return {@link ExplainDataFrameAnalyticsResponse} response object * @throws IOException when there is a serialization issue sending the request or receiving the response */ - public ExplainDataFrameAnalyticsResponse explainDataFrameAnalytics(ExplainDataFrameAnalyticsRequest request, - RequestOptions options) throws IOException { + public ExplainDataFrameAnalyticsResponse explainDataFrameAnalytics(ExplainDataFrameAnalyticsRequest request, RequestOptions options) + throws IOException { return restHighLevelClient.performRequestAndParseEntity( request, MLRequestConverters::explainDataFrameAnalytics, options, ExplainDataFrameAnalyticsResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -2379,15 +2683,19 @@ public ExplainDataFrameAnalyticsResponse explainDataFrameAnalytics(ExplainDataFr * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable explainDataFrameAnalyticsAsync(ExplainDataFrameAnalyticsRequest request, RequestOptions options, - ActionListener listener) { + public Cancellable explainDataFrameAnalyticsAsync( + ExplainDataFrameAnalyticsRequest request, + RequestOptions options, + ActionListener listener + ) { return restHighLevelClient.performRequestAsyncAndParseEntity( request, MLRequestConverters::explainDataFrameAnalytics, options, ExplainDataFrameAnalyticsResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -2401,13 +2709,14 @@ public Cancellable explainDataFrameAnalyticsAsync(ExplainDataFrameAnalyticsReque * @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized * @return {@link GetTrainedModelsResponse} response object */ - public GetTrainedModelsResponse getTrainedModels(GetTrainedModelsRequest request, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + public GetTrainedModelsResponse getTrainedModels(GetTrainedModelsRequest request, RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, MLRequestConverters::getTrainedModels, options, GetTrainedModelsResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -2422,15 +2731,19 @@ public GetTrainedModelsResponse getTrainedModels(GetTrainedModelsRequest request * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getTrainedModelsAsync(GetTrainedModelsRequest request, - RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + public Cancellable getTrainedModelsAsync( + GetTrainedModelsRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, MLRequestConverters::getTrainedModels, options, GetTrainedModelsResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -2445,11 +2758,13 @@ public Cancellable getTrainedModelsAsync(GetTrainedModelsRequest request, * @return {@link PutTrainedModelResponse} response object */ public PutTrainedModelResponse putTrainedModel(PutTrainedModelRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + return restHighLevelClient.performRequestAndParseEntity( + request, MLRequestConverters::putTrainedModel, options, PutTrainedModelResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -2464,15 +2779,19 @@ public PutTrainedModelResponse putTrainedModel(PutTrainedModelRequest request, R * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable putTrainedModelAsync(PutTrainedModelRequest request, - RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + public Cancellable putTrainedModelAsync( + PutTrainedModelRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, MLRequestConverters::putTrainedModel, options, PutTrainedModelResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -2486,13 +2805,15 @@ public Cancellable putTrainedModelAsync(PutTrainedModelRequest request, * @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized * @return {@link GetTrainedModelsStatsResponse} response object */ - public GetTrainedModelsStatsResponse getTrainedModelsStats(GetTrainedModelsStatsRequest request, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + public GetTrainedModelsStatsResponse getTrainedModelsStats(GetTrainedModelsStatsRequest request, RequestOptions options) + throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, MLRequestConverters::getTrainedModelsStats, options, GetTrainedModelsStatsResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -2507,15 +2828,19 @@ public GetTrainedModelsStatsResponse getTrainedModelsStats(GetTrainedModelsStats * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getTrainedModelsStatsAsync(GetTrainedModelsStatsRequest request, - RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + public Cancellable getTrainedModelsStatsAsync( + GetTrainedModelsStatsRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, MLRequestConverters::getTrainedModelsStats, options, GetTrainedModelsStatsResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -2531,11 +2856,13 @@ public Cancellable getTrainedModelsStatsAsync(GetTrainedModelsStatsRequest reque * @throws IOException when there is a serialization issue sending the request or receiving the response */ public AcknowledgedResponse deleteTrainedModel(DeleteTrainedModelRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + return restHighLevelClient.performRequestAndParseEntity( + request, MLRequestConverters::deleteTrainedModel, options, AcknowledgedResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -2550,15 +2877,19 @@ public AcknowledgedResponse deleteTrainedModel(DeleteTrainedModelRequest request * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable deleteTrainedModelAsync(DeleteTrainedModelRequest request, - RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + public Cancellable deleteTrainedModelAsync( + DeleteTrainedModelRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, MLRequestConverters::deleteTrainedModel, options, AcknowledgedResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -2574,11 +2905,13 @@ public Cancellable deleteTrainedModelAsync(DeleteTrainedModelRequest request, * @throws IOException when there is a serialization issue sending the request or receiving the response */ public AcknowledgedResponse putTrainedModelAlias(PutTrainedModelAliasRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + return restHighLevelClient.performRequestAndParseEntity( + request, MLRequestConverters::putTrainedModelAlias, options, AcknowledgedResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -2593,15 +2926,19 @@ public AcknowledgedResponse putTrainedModelAlias(PutTrainedModelAliasRequest req * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable putTrainedModelAliasAsync(PutTrainedModelAliasRequest request, - RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + public Cancellable putTrainedModelAliasAsync( + PutTrainedModelAliasRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, MLRequestConverters::putTrainedModelAlias, options, AcknowledgedResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -2617,11 +2954,13 @@ public Cancellable putTrainedModelAliasAsync(PutTrainedModelAliasRequest request * @throws IOException when there is a serialization issue sending the request or receiving the response */ public AcknowledgedResponse deleteTrainedModelAlias(DeleteTrainedModelAliasRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + return restHighLevelClient.performRequestAndParseEntity( + request, MLRequestConverters::deleteTrainedModelAlias, options, AcknowledgedResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -2636,14 +2975,18 @@ public AcknowledgedResponse deleteTrainedModelAlias(DeleteTrainedModelAliasReque * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable deleteTrainedModelAliasAsync(DeleteTrainedModelAliasRequest request, - RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + public Cancellable deleteTrainedModelAliasAsync( + DeleteTrainedModelAliasRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, MLRequestConverters::deleteTrainedModelAlias, options, AcknowledgedResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/MigrationClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/MigrationClient.java index a48ec45ad8854..a23758cfe3c0f 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/MigrationClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/MigrationClient.java @@ -48,8 +48,13 @@ public final class MigrationClient { * @throws IOException in case there is a problem sending the request or parsing back the response */ public DeprecationInfoResponse getDeprecationInfo(DeprecationInfoRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, MigrationRequestConverters::getDeprecationInfo, options, - DeprecationInfoResponse::fromXContent, Collections.emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + MigrationRequestConverters::getDeprecationInfo, + options, + DeprecationInfoResponse::fromXContent, + Collections.emptySet() + ); } /** @@ -59,10 +64,19 @@ public DeprecationInfoResponse getDeprecationInfo(DeprecationInfoRequest request * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getDeprecationInfoAsync(DeprecationInfoRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, MigrationRequestConverters::getDeprecationInfo, options, - DeprecationInfoResponse::fromXContent, listener, Collections.emptySet()); + public Cancellable getDeprecationInfoAsync( + DeprecationInfoRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + MigrationRequestConverters::getDeprecationInfo, + options, + DeprecationInfoResponse::fromXContent, + listener, + Collections.emptySet() + ); } /** @@ -72,8 +86,8 @@ public Cancellable getDeprecationInfoAsync(DeprecationInfoRequest request, Reque * @return the response * @throws IOException in case there is a problem sending the request or parsing back the response */ - public GetFeatureUpgradeStatusResponse getFeatureUpgradeStatus( - GetFeatureUpgradeStatusRequest request, RequestOptions options) throws IOException { + public GetFeatureUpgradeStatusResponse getFeatureUpgradeStatus(GetFeatureUpgradeStatusRequest request, RequestOptions options) + throws IOException { return restHighLevelClient.performRequestAndParseEntity( request, MigrationRequestConverters::getFeatureUpgradeStatus, @@ -90,8 +104,11 @@ public GetFeatureUpgradeStatusResponse getFeatureUpgradeStatus( * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getFeatureUpgradeStatusAsync(GetFeatureUpgradeStatusRequest request, - RequestOptions options, ActionListener listener) { + public Cancellable getFeatureUpgradeStatusAsync( + GetFeatureUpgradeStatusRequest request, + RequestOptions options, + ActionListener listener + ) { return restHighLevelClient.performRequestAsyncAndParseEntity( request, MigrationRequestConverters::getFeatureUpgradeStatus, @@ -109,8 +126,7 @@ public Cancellable getFeatureUpgradeStatusAsync(GetFeatureUpgradeStatusRequest r * @return the response * @throws IOException in case there is a problem sending the request or parsing back the response */ - public PostFeatureUpgradeResponse postFeatureUpgrade( - PostFeatureUpgradeRequest request, RequestOptions options) throws IOException { + public PostFeatureUpgradeResponse postFeatureUpgrade(PostFeatureUpgradeRequest request, RequestOptions options) throws IOException { return restHighLevelClient.performRequestAndParseEntity( request, MigrationRequestConverters::postFeatureUpgrade, @@ -128,8 +144,10 @@ public PostFeatureUpgradeResponse postFeatureUpgrade( * @return cancellable that may be used to cancel the request */ public Cancellable postFeatureUpgradeAsync( - PostFeatureUpgradeRequest request, RequestOptions options, - ActionListener listener) throws IOException { + PostFeatureUpgradeRequest request, + RequestOptions options, + ActionListener listener + ) throws IOException { return restHighLevelClient.performRequestAsyncAndParseEntity( request, MigrationRequestConverters::postFeatureUpgrade, diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/MigrationRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/MigrationRequestConverters.java index 41a0c437a121e..6c33ea6e27526 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/MigrationRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/MigrationRequestConverters.java @@ -16,12 +16,10 @@ final class MigrationRequestConverters { - private MigrationRequestConverters() { - } + private MigrationRequestConverters() {} static Request getDeprecationInfo(DeprecationInfoRequest deprecationInfoRequest) { - String endpoint = new RequestConverters.EndpointBuilder() - .addCommaSeparatedPathParts(deprecationInfoRequest.getIndices()) + String endpoint = new RequestConverters.EndpointBuilder().addCommaSeparatedPathParts(deprecationInfoRequest.getIndices()) .addPathPartAsIs("_migration", "deprecations") .build(); @@ -34,9 +32,7 @@ static Request getDeprecationInfo(DeprecationInfoRequest deprecationInfoRequest) * @return a {@link Request} with the correct path and HTTP request type */ static Request getFeatureUpgradeStatus(GetFeatureUpgradeStatusRequest getFeatureUpgradeStatusRequest) { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_migration", "system_features") - .build(); + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_migration", "system_features").build(); return new Request(HttpGet.METHOD_NAME, endpoint); } @@ -47,9 +43,7 @@ static Request getFeatureUpgradeStatus(GetFeatureUpgradeStatusRequest getFeature * @return a {@link Request} with the correct path and HTTP request type */ static Request postFeatureUpgrade(PostFeatureUpgradeRequest postFeatureUpgradeRequest) { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_migration", "system_features") - .build(); + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_migration", "system_features").build(); return new Request(HttpPost.METHOD_NAME, endpoint); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/NodesResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/NodesResponse.java index 0ea0f111126aa..a0c38498591f8 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/NodesResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/NodesResponse.java @@ -8,8 +8,8 @@ package org.elasticsearch.client; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; /** * Base class for responses that are node responses. These responses always contain the cluster diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/NodesResponseHeader.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/NodesResponseHeader.java index 0cf0e9b6bc857..e22326dc88fb3 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/NodesResponseHeader.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/NodesResponseHeader.java @@ -10,12 +10,12 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.action.support.nodes.BaseNodesResponse; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.rest.action.RestActions; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.rest.action.RestActions; import java.io.IOException; import java.util.Collections; @@ -34,23 +34,28 @@ public final class NodesResponseHeader { public static final ParseField FAILURES = new ParseField("failures"); @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("nodes_response_header", true, - (a) -> { - int i = 0; - int total = (Integer) a[i++]; - int successful = (Integer) a[i++]; - int failed = (Integer) a[i++]; - List failures = (List) a[i++]; - return new NodesResponseHeader(total, successful, failed, failures); - }); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "nodes_response_header", + true, + (a) -> { + int i = 0; + int total = (Integer) a[i++]; + int successful = (Integer) a[i++]; + int failed = (Integer) a[i++]; + List failures = (List) a[i++]; + return new NodesResponseHeader(total, successful, failed, failures); + } + ); static { PARSER.declareInt(ConstructingObjectParser.constructorArg(), TOTAL); PARSER.declareInt(ConstructingObjectParser.constructorArg(), SUCCESSFUL); PARSER.declareInt(ConstructingObjectParser.constructorArg(), FAILED); - PARSER.declareObjectArray(ConstructingObjectParser.optionalConstructorArg(), - (p, c) -> ElasticsearchException.fromXContent(p), FAILURES); + PARSER.declareObjectArray( + ConstructingObjectParser.optionalConstructorArg(), + (p, c) -> ElasticsearchException.fromXContent(p), + FAILURES + ); } private final int total; @@ -111,10 +116,7 @@ public boolean equals(Object o) { return false; } NodesResponseHeader that = (NodesResponseHeader) o; - return total == that.total && - successful == that.successful && - failed == that.failed && - Objects.equals(failures, that.failures); + return total == that.total && successful == that.successful && failed == that.failed && Objects.equals(failures, that.failures); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java index f9009cd6e184a..51eaa866fee71 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java @@ -47,22 +47,15 @@ import org.elasticsearch.client.security.RefreshPolicy; import org.elasticsearch.client.tasks.TaskId; import org.elasticsearch.cluster.health.ClusterHealthStatus; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Priority; import org.elasticsearch.common.Strings; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.lucene.uid.Versions; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.CollectionUtils; -import org.elasticsearch.xcontent.DeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContent; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.core.SuppressForbidden; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.VersionType; import org.elasticsearch.index.rankeval.RankEvalRequest; import org.elasticsearch.index.reindex.AbstractBulkByScrollRequest; @@ -74,6 +67,13 @@ import org.elasticsearch.script.mustache.MultiSearchTemplateRequest; import org.elasticsearch.script.mustache.SearchTemplateRequest; import org.elasticsearch.search.fetch.subphase.FetchSourceContext; +import org.elasticsearch.xcontent.DeprecationHandler; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -216,14 +216,19 @@ static Request bulk(BulkRequest bulkRequest) throws IOException { BytesReference indexSource = indexRequest.source(); XContentType indexXContentType = indexRequest.getContentType(); - try (XContentParser parser = XContentHelper.createParser( + try ( + XContentParser parser = XContentHelper.createParser( /* * EMPTY and THROW are fine here because we just call * copyCurrentStructure which doesn't touch the * registry or deprecation. */ - NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, - indexSource, indexXContentType)) { + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + indexSource, + indexXContentType + ) + ) { try (XContentBuilder builder = XContentBuilder.builder(bulkContentType.xContent())) { builder.copyCurrentStructure(parser); source = BytesReference.bytes(builder).toBytesRef(); @@ -363,8 +368,14 @@ static Request update(UpdateRequest updateRequest) throws IOException { if (updateRequest.upsertRequest() != null) { XContentType upsertContentType = updateRequest.upsertRequest().getContentType(); if ((xContentType != null) && (xContentType != upsertContentType)) { - throw new IllegalStateException("Update request cannot have different content types for doc [" + xContentType + "]" + - " and upsert [" + upsertContentType + "] documents"); + throw new IllegalStateException( + "Update request cannot have different content types for doc [" + + xContentType + + "]" + + " and upsert [" + + upsertContentType + + "] documents" + ); } else { xContentType = upsertContentType; } @@ -512,10 +523,10 @@ static Request count(CountRequest countRequest) throws IOException { params.withRouting(countRequest.routing()); params.withPreference(countRequest.preference()); params.withIndicesOptions(countRequest.indicesOptions()); - if (countRequest.terminateAfter() != 0){ + if (countRequest.terminateAfter() != 0) { params.withTerminateAfter(countRequest.terminateAfter()); } - if (countRequest.minScore() != null){ + if (countRequest.minScore() != null) { params.putParam("min_score", String.valueOf(countRequest.minScore())); } request.addParameters(params.asMap()); @@ -538,7 +549,7 @@ static Request explain(ExplainRequest explainRequest) throws IOException { } static Request fieldCaps(FieldCapabilitiesRequest fieldCapabilitiesRequest) throws IOException { - String methodName = fieldCapabilitiesRequest.indexFilter() != null ? HttpPost.METHOD_NAME : HttpGet.METHOD_NAME; + String methodName = fieldCapabilitiesRequest.indexFilter() != null ? HttpPost.METHOD_NAME : HttpGet.METHOD_NAME; Request request = new Request(methodName, endpoint(fieldCapabilitiesRequest.indices(), "_field_caps")); Params params = new Params(); @@ -593,8 +604,7 @@ static Request submitUpdateByQuery(UpdateByQueryRequest updateByQueryRequest) th private static Request prepareReindexRequest(ReindexRequest reindexRequest, boolean waitForCompletion) throws IOException { String endpoint = new EndpointBuilder().addPathPart("_reindex").build(); Request request = new Request(HttpPost.METHOD_NAME, endpoint); - Params params = new Params() - .withWaitForCompletion(waitForCompletion) + Params params = new Params().withWaitForCompletion(waitForCompletion) .withRefresh(reindexRequest.isRefresh()) .withTimeout(reindexRequest.getTimeout()) .withWaitForActiveShards(reindexRequest.getWaitForActiveShards()) @@ -611,12 +621,11 @@ private static Request prepareReindexRequest(ReindexRequest reindexRequest, bool return request; } - private static Request prepareDeleteByQueryRequest(DeleteByQueryRequest deleteByQueryRequest, - boolean waitForCompletion) throws IOException { + private static Request prepareDeleteByQueryRequest(DeleteByQueryRequest deleteByQueryRequest, boolean waitForCompletion) + throws IOException { String endpoint = endpoint(deleteByQueryRequest.indices(), "_delete_by_query"); Request request = new Request(HttpPost.METHOD_NAME, endpoint); - Params params = new Params() - .withRouting(deleteByQueryRequest.getRouting()) + Params params = new Params().withRouting(deleteByQueryRequest.getRouting()) .withRefresh(deleteByQueryRequest.isRefresh()) .withTimeout(deleteByQueryRequest.getTimeout()) .withWaitForActiveShards(deleteByQueryRequest.getWaitForActiveShards()) @@ -645,12 +654,10 @@ private static Request prepareDeleteByQueryRequest(DeleteByQueryRequest deleteBy return request; } - static Request prepareUpdateByQueryRequest(UpdateByQueryRequest updateByQueryRequest, - boolean waitForCompletion) throws IOException { + static Request prepareUpdateByQueryRequest(UpdateByQueryRequest updateByQueryRequest, boolean waitForCompletion) throws IOException { String endpoint = endpoint(updateByQueryRequest.indices(), "_update_by_query"); Request request = new Request(HttpPost.METHOD_NAME, endpoint); - Params params = new Params() - .withRouting(updateByQueryRequest.getRouting()) + Params params = new Params().withRouting(updateByQueryRequest.getRouting()) .withPipeline(updateByQueryRequest.getPipeline()) .withRefresh(updateByQueryRequest.isRefresh()) .withTimeout(updateByQueryRequest.getTimeout()) @@ -691,11 +698,12 @@ static Request rethrottleDeleteByQuery(RethrottleRequest rethrottleRequest) { } private static Request rethrottle(RethrottleRequest rethrottleRequest, String firstPathPart) { - String endpoint = new EndpointBuilder().addPathPart(firstPathPart).addPathPart(rethrottleRequest.getTaskId().toString()) - .addPathPart("_rethrottle").build(); + String endpoint = new EndpointBuilder().addPathPart(firstPathPart) + .addPathPart(rethrottleRequest.getTaskId().toString()) + .addPathPart("_rethrottle") + .build(); Request request = new Request(HttpPost.METHOD_NAME, endpoint); - Params params = new Params() - .withRequestsPerSecond(rethrottleRequest.getRequestsPerSecond()); + Params params = new Params().withRequestsPerSecond(rethrottleRequest.getRequestsPerSecond()); // we set "group_by" to "none" because this is the response format we can parse back params.putParam("group_by", "none"); request.addParameters(params.asMap()); @@ -811,13 +819,17 @@ static String endpoint(String[] indices, String endpoint) { @Deprecated static String endpoint(String[] indices, String[] types, String endpoint) { - return new EndpointBuilder().addCommaSeparatedPathParts(indices).addCommaSeparatedPathParts(types) - .addPathPartAsIs(endpoint).build(); + return new EndpointBuilder().addCommaSeparatedPathParts(indices) + .addCommaSeparatedPathParts(types) + .addPathPartAsIs(endpoint) + .build(); } static String endpoint(String[] indices, String endpoint, String[] suffixes) { - return new EndpointBuilder().addCommaSeparatedPathParts(indices).addPathPartAsIs(endpoint) - .addCommaSeparatedPathParts(suffixes).build(); + return new EndpointBuilder().addCommaSeparatedPathParts(indices) + .addPathPartAsIs(endpoint) + .addCommaSeparatedPathParts(suffixes) + .build(); } @Deprecated @@ -841,14 +853,13 @@ public static ContentType createContentType(final XContentType xContentType) { * a {@link Request} and adds the parameters to it directly. */ static class Params { - private final Map parameters = new HashMap<>(); + private final Map parameters = new HashMap<>(); - Params() { - } + Params() {} Params putParam(String name, String value) { if (Strings.hasLength(value)) { - parameters.put(name,value); + parameters.put(name, value); } return this; } @@ -860,7 +871,7 @@ Params putParam(String key, TimeValue value) { return this; } - Map asMap(){ + Map asMap() { return parameters; } @@ -994,7 +1005,7 @@ Params withStoredFields(String[] storedFields) { return this; } - Params withTerminateAfter(int terminateAfter){ + Params withTerminateAfter(int terminateAfter) { return putParam("terminate_after", String.valueOf(terminateAfter)); } @@ -1117,7 +1128,7 @@ Params withWaitForCompletion(Boolean waitForCompletion) { } Params withNodes(String[] nodes) { - return withNodes(Arrays.asList(nodes)); + return withNodes(Arrays.asList(nodes)); } Params withNodes(List nodes) { @@ -1212,15 +1223,23 @@ Params withWaitForEvents(Priority waitForEvents) { static XContentType enforceSameContentType(IndexRequest indexRequest, @Nullable XContentType xContentType) { XContentType requestContentType = indexRequest.getContentType(); if (requestContentType.canonical() != XContentType.JSON && requestContentType.canonical() != XContentType.SMILE) { - throw new IllegalArgumentException("Unsupported content-type found for request with content-type [" + requestContentType - + "], only JSON and SMILE are supported"); + throw new IllegalArgumentException( + "Unsupported content-type found for request with content-type [" + + requestContentType + + "], only JSON and SMILE are supported" + ); } if (xContentType == null) { return requestContentType; } if (requestContentType.canonical() != xContentType.canonical()) { - throw new IllegalArgumentException("Mismatching content-type found for request with content-type [" + requestContentType - + "], previous requests have content-type [" + xContentType + "]"); + throw new IllegalArgumentException( + "Mismatching content-type found for request with content-type [" + + requestContentType + + "], previous requests have content-type [" + + xContentType + + "]" + ); } return xContentType; } @@ -1251,7 +1270,7 @@ EndpointBuilder addCommaSeparatedPathParts(List parts) { return this; } - EndpointBuilder addPathPartAsIs(String ... parts) { + EndpointBuilder addPathPartAsIs(String... parts) { for (String part : parts) { if (Strings.hasLength(part)) { joiner.add(part); @@ -1266,13 +1285,13 @@ String build() { private static String encodePart(String pathPart) { try { - //encode each part (e.g. index, type and id) separately before merging them into the path - //we prepend "/" to the path part to make this path absolute, otherwise there can be issues with - //paths that start with `-` or contain `:` - //the authority must be an empty string and not null, else paths that being with slashes could have them - //misinterpreted as part of the authority. + // encode each part (e.g. index, type and id) separately before merging them into the path + // we prepend "/" to the path part to make this path absolute, otherwise there can be issues with + // paths that start with `-` or contain `:` + // the authority must be an empty string and not null, else paths that being with slashes could have them + // misinterpreted as part of the authority. URI uri = new URI(null, "", "/" + pathPart, null, null); - //manually encode any slash that each part may contain + // manually encode any slash that each part may contain return uri.getRawPath().substring(1).replaceAll("/", "%2F"); } catch (URISyntaxException e) { throw new IllegalArgumentException("Path part [" + pathPart + "] couldn't be encoded", e); @@ -1280,4 +1299,3 @@ private static String encodePart(String pathPart) { } } } - diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java index 6f9cbc62011c7..60327e987dd69 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java @@ -319,8 +319,11 @@ protected RestHighLevelClient(RestClientBuilder restClientBuilder, List doClose, - List namedXContentEntries) { + protected RestHighLevelClient( + RestClient restClient, + CheckedConsumer doClose, + List namedXContentEntries + ) { this(restClient, doClose, namedXContentEntries, null); } @@ -331,13 +334,19 @@ protected RestHighLevelClient(RestClient restClient, CheckedConsumer doClose, - List namedXContentEntries, Boolean useAPICompatibility) { + protected RestHighLevelClient( + RestClient restClient, + CheckedConsumer doClose, + List namedXContentEntries, + Boolean useAPICompatibility + ) { this.client = Objects.requireNonNull(restClient, "restClient must not be null"); this.doClose = Objects.requireNonNull(doClose, "doClose consumer must not be null"); this.registry = new NamedXContentRegistry( Stream.of(getDefaultNamedXContents().stream(), getProvidedNamedXContents().stream(), namedXContentEntries.stream()) - .flatMap(Function.identity()).collect(toList())); + .flatMap(Function.identity()) + .collect(toList()) + ); if (useAPICompatibility == null && "true".equals(System.getenv(API_VERSIONING_ENV_VARIABLE))) { this.useAPICompatibility = true; } else { @@ -449,7 +458,9 @@ public final XPackClient xpack() { * See the * Watcher APIs on elastic.co for more information. */ - public WatcherClient watcher() { return watcherClient; } + public WatcherClient watcher() { + return watcherClient; + } /** * Provides methods for accessing the Elastic Licensed Graph explore API that @@ -459,7 +470,9 @@ public final XPackClient xpack() { * See the * Graph API on elastic.co for more information. */ - public GraphClient graph() { return graphClient; } + public GraphClient graph() { + return graphClient; + } /** * Provides methods for accessing the Elastic Licensed Licensing APIs that @@ -469,7 +482,9 @@ public final XPackClient xpack() { * See the * Licensing APIs on elastic.co for more information. */ - public LicenseClient license() { return licenseClient; } + public LicenseClient license() { + return licenseClient; + } /** * A wrapper for the {@link RestHighLevelClient} that provides methods for @@ -614,8 +629,14 @@ public final BulkResponse bulk(BulkRequest bulkRequest, RequestOptions options) * @return cancellable that may be used to cancel the request */ public final Cancellable bulkAsync(BulkRequest bulkRequest, RequestOptions options, ActionListener listener) { - return performRequestAsyncAndParseEntity(bulkRequest, RequestConverters::bulk, options, - BulkResponse::fromXContent, listener, emptySet()); + return performRequestAsyncAndParseEntity( + bulkRequest, + RequestConverters::bulk, + options, + BulkResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -627,7 +648,11 @@ public final Cancellable bulkAsync(BulkRequest bulkRequest, RequestOptions optio */ public final BulkByScrollResponse reindex(ReindexRequest reindexRequest, RequestOptions options) throws IOException { return performRequestAndParseEntity( - reindexRequest, RequestConverters::reindex, options, BulkByScrollResponse::fromXContent, singleton(409) + reindexRequest, + RequestConverters::reindex, + options, + BulkByScrollResponse::fromXContent, + singleton(409) ); } @@ -640,7 +665,11 @@ public final BulkByScrollResponse reindex(ReindexRequest reindexRequest, Request */ public final TaskSubmissionResponse submitReindexTask(ReindexRequest reindexRequest, RequestOptions options) throws IOException { return performRequestAndParseEntity( - reindexRequest, RequestConverters::submitReindex, options, TaskSubmissionResponse::fromXContent, emptySet() + reindexRequest, + RequestConverters::submitReindex, + options, + TaskSubmissionResponse::fromXContent, + emptySet() ); } @@ -652,10 +681,18 @@ public final TaskSubmissionResponse submitReindexTask(ReindexRequest reindexRequ * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public final Cancellable reindexAsync(ReindexRequest reindexRequest, RequestOptions options, - ActionListener listener) { + public final Cancellable reindexAsync( + ReindexRequest reindexRequest, + RequestOptions options, + ActionListener listener + ) { return performRequestAsyncAndParseEntity( - reindexRequest, RequestConverters::reindex, options, BulkByScrollResponse::fromXContent, listener, singleton(409) + reindexRequest, + RequestConverters::reindex, + options, + BulkByScrollResponse::fromXContent, + listener, + singleton(409) ); } @@ -669,7 +706,11 @@ public final Cancellable reindexAsync(ReindexRequest reindexRequest, RequestOpti */ public final BulkByScrollResponse updateByQuery(UpdateByQueryRequest updateByQueryRequest, RequestOptions options) throws IOException { return performRequestAndParseEntity( - updateByQueryRequest, RequestConverters::updateByQuery, options, BulkByScrollResponse::fromXContent, singleton(409) + updateByQueryRequest, + RequestConverters::updateByQuery, + options, + BulkByScrollResponse::fromXContent, + singleton(409) ); } @@ -681,10 +722,14 @@ public final BulkByScrollResponse updateByQuery(UpdateByQueryRequest updateByQue * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized * @return the submission response */ - public final TaskSubmissionResponse submitUpdateByQueryTask(UpdateByQueryRequest updateByQueryRequest, - RequestOptions options) throws IOException { + public final TaskSubmissionResponse submitUpdateByQueryTask(UpdateByQueryRequest updateByQueryRequest, RequestOptions options) + throws IOException { return performRequestAndParseEntity( - updateByQueryRequest, RequestConverters::submitUpdateByQuery, options, TaskSubmissionResponse::fromXContent, emptySet() + updateByQueryRequest, + RequestConverters::submitUpdateByQuery, + options, + TaskSubmissionResponse::fromXContent, + emptySet() ); } @@ -697,10 +742,18 @@ public final TaskSubmissionResponse submitUpdateByQueryTask(UpdateByQueryRequest * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public final Cancellable updateByQueryAsync(UpdateByQueryRequest updateByQueryRequest, RequestOptions options, - ActionListener listener) { + public final Cancellable updateByQueryAsync( + UpdateByQueryRequest updateByQueryRequest, + RequestOptions options, + ActionListener listener + ) { return performRequestAsyncAndParseEntity( - updateByQueryRequest, RequestConverters::updateByQuery, options, BulkByScrollResponse::fromXContent, listener, singleton(409) + updateByQueryRequest, + RequestConverters::updateByQuery, + options, + BulkByScrollResponse::fromXContent, + listener, + singleton(409) ); } @@ -714,7 +767,11 @@ public final Cancellable updateByQueryAsync(UpdateByQueryRequest updateByQueryRe */ public final BulkByScrollResponse deleteByQuery(DeleteByQueryRequest deleteByQueryRequest, RequestOptions options) throws IOException { return performRequestAndParseEntity( - deleteByQueryRequest, RequestConverters::deleteByQuery, options, BulkByScrollResponse::fromXContent, singleton(409) + deleteByQueryRequest, + RequestConverters::deleteByQuery, + options, + BulkByScrollResponse::fromXContent, + singleton(409) ); } @@ -726,10 +783,14 @@ public final BulkByScrollResponse deleteByQuery(DeleteByQueryRequest deleteByQue * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized * @return the submission response */ - public final TaskSubmissionResponse submitDeleteByQueryTask(DeleteByQueryRequest deleteByQueryRequest, - RequestOptions options) throws IOException { + public final TaskSubmissionResponse submitDeleteByQueryTask(DeleteByQueryRequest deleteByQueryRequest, RequestOptions options) + throws IOException { return performRequestAndParseEntity( - deleteByQueryRequest, RequestConverters::submitDeleteByQuery, options, TaskSubmissionResponse::fromXContent, emptySet() + deleteByQueryRequest, + RequestConverters::submitDeleteByQuery, + options, + TaskSubmissionResponse::fromXContent, + emptySet() ); } @@ -742,10 +803,18 @@ public final TaskSubmissionResponse submitDeleteByQueryTask(DeleteByQueryRequest * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public final Cancellable deleteByQueryAsync(DeleteByQueryRequest deleteByQueryRequest, RequestOptions options, - ActionListener listener) { + public final Cancellable deleteByQueryAsync( + DeleteByQueryRequest deleteByQueryRequest, + RequestOptions options, + ActionListener listener + ) { return performRequestAsyncAndParseEntity( - deleteByQueryRequest, RequestConverters::deleteByQuery, options, BulkByScrollResponse::fromXContent, listener, singleton(409) + deleteByQueryRequest, + RequestConverters::deleteByQuery, + options, + BulkByScrollResponse::fromXContent, + listener, + singleton(409) ); } @@ -758,8 +827,13 @@ public final Cancellable deleteByQueryAsync(DeleteByQueryRequest deleteByQueryRe * @return the response */ public final ListTasksResponse deleteByQueryRethrottle(RethrottleRequest rethrottleRequest, RequestOptions options) throws IOException { - return performRequestAndParseEntity(rethrottleRequest, RequestConverters::rethrottleDeleteByQuery, options, - ListTasksResponse::fromXContent, emptySet()); + return performRequestAndParseEntity( + rethrottleRequest, + RequestConverters::rethrottleDeleteByQuery, + options, + ListTasksResponse::fromXContent, + emptySet() + ); } /** @@ -771,10 +845,19 @@ public final ListTasksResponse deleteByQueryRethrottle(RethrottleRequest rethrot * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public final Cancellable deleteByQueryRethrottleAsync(RethrottleRequest rethrottleRequest, RequestOptions options, - ActionListener listener) { - return performRequestAsyncAndParseEntity(rethrottleRequest, RequestConverters::rethrottleDeleteByQuery, options, - ListTasksResponse::fromXContent, listener, emptySet()); + public final Cancellable deleteByQueryRethrottleAsync( + RethrottleRequest rethrottleRequest, + RequestOptions options, + ActionListener listener + ) { + return performRequestAsyncAndParseEntity( + rethrottleRequest, + RequestConverters::rethrottleDeleteByQuery, + options, + ListTasksResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -786,8 +869,13 @@ public final Cancellable deleteByQueryRethrottleAsync(RethrottleRequest rethrott * @return the response */ public final ListTasksResponse updateByQueryRethrottle(RethrottleRequest rethrottleRequest, RequestOptions options) throws IOException { - return performRequestAndParseEntity(rethrottleRequest, RequestConverters::rethrottleUpdateByQuery, options, - ListTasksResponse::fromXContent, emptySet()); + return performRequestAndParseEntity( + rethrottleRequest, + RequestConverters::rethrottleUpdateByQuery, + options, + ListTasksResponse::fromXContent, + emptySet() + ); } /** @@ -799,10 +887,19 @@ public final ListTasksResponse updateByQueryRethrottle(RethrottleRequest rethrot * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public final Cancellable updateByQueryRethrottleAsync(RethrottleRequest rethrottleRequest, RequestOptions options, - ActionListener listener) { - return performRequestAsyncAndParseEntity(rethrottleRequest, RequestConverters::rethrottleUpdateByQuery, options, - ListTasksResponse::fromXContent, listener, emptySet()); + public final Cancellable updateByQueryRethrottleAsync( + RethrottleRequest rethrottleRequest, + RequestOptions options, + ActionListener listener + ) { + return performRequestAsyncAndParseEntity( + rethrottleRequest, + RequestConverters::rethrottleUpdateByQuery, + options, + ListTasksResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -815,8 +912,13 @@ public final Cancellable updateByQueryRethrottleAsync(RethrottleRequest rethrott * @return the response */ public final ListTasksResponse reindexRethrottle(RethrottleRequest rethrottleRequest, RequestOptions options) throws IOException { - return performRequestAndParseEntity(rethrottleRequest, RequestConverters::rethrottleReindex, options, - ListTasksResponse::fromXContent, emptySet()); + return performRequestAndParseEntity( + rethrottleRequest, + RequestConverters::rethrottleReindex, + options, + ListTasksResponse::fromXContent, + emptySet() + ); } /** @@ -828,10 +930,19 @@ public final ListTasksResponse reindexRethrottle(RethrottleRequest rethrottleReq * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public final Cancellable reindexRethrottleAsync(RethrottleRequest rethrottleRequest, RequestOptions options, - ActionListener listener) { - return performRequestAsyncAndParseEntity(rethrottleRequest, - RequestConverters::rethrottleReindex, options, ListTasksResponse::fromXContent, listener, emptySet()); + public final Cancellable reindexRethrottleAsync( + RethrottleRequest rethrottleRequest, + RequestOptions options, + ActionListener listener + ) { + return performRequestAsyncAndParseEntity( + rethrottleRequest, + RequestConverters::rethrottleReindex, + options, + ListTasksResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -840,8 +951,13 @@ public final Cancellable reindexRethrottleAsync(RethrottleRequest rethrottleRequ * @return true if the ping succeeded, false otherwise */ public final boolean ping(RequestOptions options) throws IOException { - return performRequest(new MainRequest(), (request) -> RequestConverters.ping(), options, RestHighLevelClient::convertExistsResponse, - emptySet()); + return performRequest( + new MainRequest(), + (request) -> RequestConverters.ping(), + options, + RestHighLevelClient::convertExistsResponse, + emptySet() + ); } /** @@ -850,8 +966,13 @@ public final boolean ping(RequestOptions options) throws IOException { * @return the response */ public final MainResponse info(RequestOptions options) throws IOException { - return performRequestAndParseEntity(new MainRequest(), (request) -> RequestConverters.info(), options, - MainResponse::fromXContent, emptySet()); + return performRequestAndParseEntity( + new MainRequest(), + (request) -> RequestConverters.info(), + options, + MainResponse::fromXContent, + emptySet() + ); } /** @@ -874,8 +995,14 @@ public final GetResponse get(GetRequest getRequest, RequestOptions options) thro * @return cancellable that may be used to cancel the request */ public final Cancellable getAsync(GetRequest getRequest, RequestOptions options, ActionListener listener) { - return performRequestAsyncAndParseEntity(getRequest, RequestConverters::get, options, GetResponse::fromXContent, listener, - singleton(404)); + return performRequestAsyncAndParseEntity( + getRequest, + RequestConverters::get, + options, + GetResponse::fromXContent, + listener, + singleton(404) + ); } /** @@ -891,7 +1018,6 @@ public final MultiGetResponse multiGet(MultiGetRequest multiGetRequest, RequestO return mget(multiGetRequest, options); } - /** * Retrieves multiple documents by id using the Multi Get API. * See Multi Get API on elastic.co @@ -900,8 +1026,13 @@ public final MultiGetResponse multiGet(MultiGetRequest multiGetRequest, RequestO * @return the response */ public final MultiGetResponse mget(MultiGetRequest multiGetRequest, RequestOptions options) throws IOException { - return performRequestAndParseEntity(multiGetRequest, RequestConverters::multiGet, options, MultiGetResponse::fromXContent, - singleton(404)); + return performRequestAndParseEntity( + multiGetRequest, + RequestConverters::multiGet, + options, + MultiGetResponse::fromXContent, + singleton(404) + ); } /** @@ -914,8 +1045,11 @@ public final MultiGetResponse mget(MultiGetRequest multiGetRequest, RequestOptio * @return cancellable that may be used to cancel the request */ @Deprecated - public final Cancellable multiGetAsync(MultiGetRequest multiGetRequest, RequestOptions options, - ActionListener listener) { + public final Cancellable multiGetAsync( + MultiGetRequest multiGetRequest, + RequestOptions options, + ActionListener listener + ) { return mgetAsync(multiGetRequest, options, listener); } @@ -927,10 +1061,15 @@ public final Cancellable multiGetAsync(MultiGetRequest multiGetRequest, RequestO * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public final Cancellable mgetAsync(MultiGetRequest multiGetRequest, RequestOptions options, - ActionListener listener) { - return performRequestAsyncAndParseEntity(multiGetRequest, RequestConverters::multiGet, options, - MultiGetResponse::fromXContent, listener, singleton(404)); + public final Cancellable mgetAsync(MultiGetRequest multiGetRequest, RequestOptions options, ActionListener listener) { + return performRequestAsyncAndParseEntity( + multiGetRequest, + RequestConverters::multiGet, + options, + MultiGetResponse::fromXContent, + listener, + singleton(404) + ); } /** @@ -953,8 +1092,14 @@ public final boolean exists(GetRequest getRequest, RequestOptions options) throw * @return cancellable that may be used to cancel the request */ public final Cancellable existsAsync(GetRequest getRequest, RequestOptions options, ActionListener listener) { - return performRequestAsync(getRequest, RequestConverters::exists, options, RestHighLevelClient::convertExistsResponse, listener, - emptySet()); + return performRequestAsync( + getRequest, + RequestConverters::exists, + options, + RestHighLevelClient::convertExistsResponse, + listener, + emptySet() + ); } /** @@ -969,8 +1114,13 @@ public final Cancellable existsAsync(GetRequest getRequest, RequestOptions optio @Deprecated public boolean existsSource(GetRequest getRequest, RequestOptions options) throws IOException { GetSourceRequest getSourceRequest = GetSourceRequest.from(getRequest); - return performRequest(getSourceRequest, RequestConverters::sourceExists, options, - RestHighLevelClient::convertExistsResponse, emptySet()); + return performRequest( + getSourceRequest, + RequestConverters::sourceExists, + options, + RestHighLevelClient::convertExistsResponse, + emptySet() + ); } /** @@ -986,8 +1136,14 @@ public boolean existsSource(GetRequest getRequest, RequestOptions options) throw @Deprecated public final Cancellable existsSourceAsync(GetRequest getRequest, RequestOptions options, ActionListener listener) { GetSourceRequest getSourceRequest = GetSourceRequest.from(getRequest); - return performRequestAsync(getSourceRequest, RequestConverters::sourceExists, options, - RestHighLevelClient::convertExistsResponse, listener, emptySet()); + return performRequestAsync( + getSourceRequest, + RequestConverters::sourceExists, + options, + RestHighLevelClient::convertExistsResponse, + listener, + emptySet() + ); } /** @@ -999,8 +1155,13 @@ public final Cancellable existsSourceAsync(GetRequest getRequest, RequestOptions * @return true if the document and _source field exists, false otherwise */ public boolean existsSource(GetSourceRequest getSourceRequest, RequestOptions options) throws IOException { - return performRequest(getSourceRequest, RequestConverters::sourceExists, options, - RestHighLevelClient::convertExistsResponse, emptySet()); + return performRequest( + getSourceRequest, + RequestConverters::sourceExists, + options, + RestHighLevelClient::convertExistsResponse, + emptySet() + ); } /** @@ -1012,10 +1173,19 @@ public boolean existsSource(GetSourceRequest getSourceRequest, RequestOptions op * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public final Cancellable existsSourceAsync(GetSourceRequest getSourceRequest, RequestOptions options, - ActionListener listener) { - return performRequestAsync(getSourceRequest, RequestConverters::sourceExists, options, - RestHighLevelClient::convertExistsResponse, listener, emptySet()); + public final Cancellable existsSourceAsync( + GetSourceRequest getSourceRequest, + RequestOptions options, + ActionListener listener + ) { + return performRequestAsync( + getSourceRequest, + RequestConverters::sourceExists, + options, + RestHighLevelClient::convertExistsResponse, + listener, + emptySet() + ); } /** @@ -1027,8 +1197,13 @@ public final Cancellable existsSourceAsync(GetSourceRequest getSourceRequest, Re * @return the response */ public GetSourceResponse getSource(GetSourceRequest getSourceRequest, RequestOptions options) throws IOException { - return performRequestAndParseEntity(getSourceRequest, RequestConverters::getSource, options, - GetSourceResponse::fromXContent, emptySet()); + return performRequestAndParseEntity( + getSourceRequest, + RequestConverters::getSource, + options, + GetSourceResponse::fromXContent, + emptySet() + ); } /** @@ -1040,10 +1215,19 @@ public GetSourceResponse getSource(GetSourceRequest getSourceRequest, RequestOpt * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public final Cancellable getSourceAsync(GetSourceRequest getSourceRequest, RequestOptions options, - ActionListener listener) { - return performRequestAsyncAndParseEntity(getSourceRequest, RequestConverters::getSource, options, - GetSourceResponse::fromXContent, listener, emptySet()); + public final Cancellable getSourceAsync( + GetSourceRequest getSourceRequest, + RequestOptions options, + ActionListener listener + ) { + return performRequestAsyncAndParseEntity( + getSourceRequest, + RequestConverters::getSource, + options, + GetSourceResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -1054,8 +1238,7 @@ public final Cancellable getSourceAsync(GetSourceRequest getSourceRequest, Reque * @return the response */ public final IndexResponse index(IndexRequest indexRequest, RequestOptions options) throws IOException { - return performRequestAndParseEntity(indexRequest, RequestConverters::index, options, - IndexResponse::fromXContent, emptySet()); + return performRequestAndParseEntity(indexRequest, RequestConverters::index, options, IndexResponse::fromXContent, emptySet()); } /** @@ -1067,8 +1250,14 @@ public final IndexResponse index(IndexRequest indexRequest, RequestOptions optio * @return cancellable that may be used to cancel the request */ public final Cancellable indexAsync(IndexRequest indexRequest, RequestOptions options, ActionListener listener) { - return performRequestAsyncAndParseEntity(indexRequest, RequestConverters::index, options, IndexResponse::fromXContent, listener, - emptySet()); + return performRequestAsyncAndParseEntity( + indexRequest, + RequestConverters::index, + options, + IndexResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -1079,8 +1268,7 @@ public final Cancellable indexAsync(IndexRequest indexRequest, RequestOptions op * @return the response */ public final CountResponse count(CountRequest countRequest, RequestOptions options) throws IOException { - return performRequestAndParseEntity(countRequest, RequestConverters::count, options, CountResponse::fromXContent, - emptySet()); + return performRequestAndParseEntity(countRequest, RequestConverters::count, options, CountResponse::fromXContent, emptySet()); } /** @@ -1092,8 +1280,14 @@ public final CountResponse count(CountRequest countRequest, RequestOptions optio * @return cancellable that may be used to cancel the request */ public final Cancellable countAsync(CountRequest countRequest, RequestOptions options, ActionListener listener) { - return performRequestAsyncAndParseEntity(countRequest, RequestConverters::count, options,CountResponse::fromXContent, - listener, emptySet()); + return performRequestAsyncAndParseEntity( + countRequest, + RequestConverters::count, + options, + CountResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -1116,8 +1310,14 @@ public final UpdateResponse update(UpdateRequest updateRequest, RequestOptions o * @return cancellable that may be used to cancel the request */ public final Cancellable updateAsync(UpdateRequest updateRequest, RequestOptions options, ActionListener listener) { - return performRequestAsyncAndParseEntity(updateRequest, RequestConverters::update, options, UpdateResponse::fromXContent, listener, - emptySet()); + return performRequestAsyncAndParseEntity( + updateRequest, + RequestConverters::update, + options, + UpdateResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -1128,8 +1328,13 @@ public final Cancellable updateAsync(UpdateRequest updateRequest, RequestOptions * @return the response */ public final DeleteResponse delete(DeleteRequest deleteRequest, RequestOptions options) throws IOException { - return performRequestAndParseEntity(deleteRequest, RequestConverters::delete, options, DeleteResponse::fromXContent, - singleton(404)); + return performRequestAndParseEntity( + deleteRequest, + RequestConverters::delete, + options, + DeleteResponse::fromXContent, + singleton(404) + ); } /** @@ -1141,8 +1346,14 @@ public final DeleteResponse delete(DeleteRequest deleteRequest, RequestOptions o * @return cancellable that may be used to cancel the request */ public final Cancellable deleteAsync(DeleteRequest deleteRequest, RequestOptions options, ActionListener listener) { - return performRequestAsyncAndParseEntity(deleteRequest, RequestConverters::delete, options, DeleteResponse::fromXContent, listener, - Collections.singleton(404)); + return performRequestAsyncAndParseEntity( + deleteRequest, + RequestConverters::delete, + options, + DeleteResponse::fromXContent, + listener, + Collections.singleton(404) + ); } /** @@ -1154,11 +1365,12 @@ public final Cancellable deleteAsync(DeleteRequest deleteRequest, RequestOptions */ public final SearchResponse search(SearchRequest searchRequest, RequestOptions options) throws IOException { return performRequestAndParseEntity( - searchRequest, - r -> RequestConverters.search(r, "_search"), - options, - SearchResponse::fromXContent, - emptySet()); + searchRequest, + r -> RequestConverters.search(r, "_search"), + options, + SearchResponse::fromXContent, + emptySet() + ); } /** @@ -1171,12 +1383,13 @@ public final SearchResponse search(SearchRequest searchRequest, RequestOptions o */ public final Cancellable searchAsync(SearchRequest searchRequest, RequestOptions options, ActionListener listener) { return performRequestAsyncAndParseEntity( - searchRequest, - r -> RequestConverters.search(r, "_search"), - options, - SearchResponse::fromXContent, - listener, - emptySet()); + searchRequest, + r -> RequestConverters.search(r, "_search"), + options, + SearchResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -1202,8 +1415,13 @@ public final MultiSearchResponse multiSearch(MultiSearchRequest multiSearchReque * @return the response */ public final MultiSearchResponse msearch(MultiSearchRequest multiSearchRequest, RequestOptions options) throws IOException { - return performRequestAndParseEntity(multiSearchRequest, RequestConverters::multiSearch, options, MultiSearchResponse::fromXContext, - emptySet()); + return performRequestAndParseEntity( + multiSearchRequest, + RequestConverters::multiSearch, + options, + MultiSearchResponse::fromXContext, + emptySet() + ); } /** @@ -1217,8 +1435,11 @@ public final MultiSearchResponse msearch(MultiSearchRequest multiSearchRequest, * @return cancellable that may be used to cancel the request */ @Deprecated - public final Cancellable multiSearchAsync(MultiSearchRequest searchRequest, RequestOptions options, - ActionListener listener) { + public final Cancellable multiSearchAsync( + MultiSearchRequest searchRequest, + RequestOptions options, + ActionListener listener + ) { return msearchAsync(searchRequest, options, listener); } @@ -1231,10 +1452,19 @@ public final Cancellable multiSearchAsync(MultiSearchRequest searchRequest, Requ * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public final Cancellable msearchAsync(MultiSearchRequest searchRequest, RequestOptions options, - ActionListener listener) { - return performRequestAsyncAndParseEntity(searchRequest, RequestConverters::multiSearch, options, MultiSearchResponse::fromXContext, - listener, emptySet()); + public final Cancellable msearchAsync( + MultiSearchRequest searchRequest, + RequestOptions options, + ActionListener listener + ) { + return performRequestAsyncAndParseEntity( + searchRequest, + RequestConverters::multiSearch, + options, + MultiSearchResponse::fromXContext, + listener, + emptySet() + ); } /** @@ -1262,8 +1492,13 @@ public final SearchResponse searchScroll(SearchScrollRequest searchScrollRequest * @return the response */ public final SearchResponse scroll(SearchScrollRequest searchScrollRequest, RequestOptions options) throws IOException { - return performRequestAndParseEntity(searchScrollRequest, RequestConverters::searchScroll, options, SearchResponse::fromXContent, - emptySet()); + return performRequestAndParseEntity( + searchScrollRequest, + RequestConverters::searchScroll, + options, + SearchResponse::fromXContent, + emptySet() + ); } /** @@ -1278,8 +1513,11 @@ public final SearchResponse scroll(SearchScrollRequest searchScrollRequest, Requ * @return cancellable that may be used to cancel the request */ @Deprecated - public final Cancellable searchScrollAsync(SearchScrollRequest searchScrollRequest, RequestOptions options, - ActionListener listener) { + public final Cancellable searchScrollAsync( + SearchScrollRequest searchScrollRequest, + RequestOptions options, + ActionListener listener + ) { return scrollAsync(searchScrollRequest, options, listener); } @@ -1293,10 +1531,19 @@ public final Cancellable searchScrollAsync(SearchScrollRequest searchScrollReque * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public final Cancellable scrollAsync(SearchScrollRequest searchScrollRequest, RequestOptions options, - ActionListener listener) { - return performRequestAsyncAndParseEntity(searchScrollRequest, RequestConverters::searchScroll, - options, SearchResponse::fromXContent, listener, emptySet()); + public final Cancellable scrollAsync( + SearchScrollRequest searchScrollRequest, + RequestOptions options, + ActionListener listener + ) { + return performRequestAsyncAndParseEntity( + searchScrollRequest, + RequestConverters::searchScroll, + options, + SearchResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -1309,8 +1556,13 @@ public final Cancellable scrollAsync(SearchScrollRequest searchScrollRequest, Re * @return the response */ public final ClearScrollResponse clearScroll(ClearScrollRequest clearScrollRequest, RequestOptions options) throws IOException { - return performRequestAndParseEntity(clearScrollRequest, RequestConverters::clearScroll, options, ClearScrollResponse::fromXContent, - emptySet()); + return performRequestAndParseEntity( + clearScrollRequest, + RequestConverters::clearScroll, + options, + ClearScrollResponse::fromXContent, + emptySet() + ); } /** @@ -1323,10 +1575,19 @@ public final ClearScrollResponse clearScroll(ClearScrollRequest clearScrollReque * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public final Cancellable clearScrollAsync(ClearScrollRequest clearScrollRequest, RequestOptions options, - ActionListener listener) { - return performRequestAsyncAndParseEntity(clearScrollRequest, RequestConverters::clearScroll, - options, ClearScrollResponse::fromXContent, listener, emptySet()); + public final Cancellable clearScrollAsync( + ClearScrollRequest clearScrollRequest, + RequestOptions options, + ActionListener listener + ) { + return performRequestAsyncAndParseEntity( + clearScrollRequest, + RequestConverters::clearScroll, + options, + ClearScrollResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -1336,10 +1597,14 @@ public final Cancellable clearScrollAsync(ClearScrollRequest clearScrollRequest, * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized * @return the response containing the point in time id */ - public final OpenPointInTimeResponse openPointInTime(OpenPointInTimeRequest openRequest, - RequestOptions options) throws IOException { - return performRequestAndParseEntity(openRequest, RequestConverters::openPointInTime, - options, OpenPointInTimeResponse::fromXContent, emptySet()); + public final OpenPointInTimeResponse openPointInTime(OpenPointInTimeRequest openRequest, RequestOptions options) throws IOException { + return performRequestAndParseEntity( + openRequest, + RequestConverters::openPointInTime, + options, + OpenPointInTimeResponse::fromXContent, + emptySet() + ); } /** @@ -1350,11 +1615,19 @@ public final OpenPointInTimeResponse openPointInTime(OpenPointInTimeRequest open * @param listener the listener to be notified upon request completion * @return a cancellable that may be used to cancel the request */ - public final Cancellable openPointInTimeAsync(OpenPointInTimeRequest openRequest, - RequestOptions options, - ActionListener listener) { - return performRequestAsyncAndParseEntity(openRequest, RequestConverters::openPointInTime, - options, OpenPointInTimeResponse::fromXContent, listener, emptySet()); + public final Cancellable openPointInTimeAsync( + OpenPointInTimeRequest openRequest, + RequestOptions options, + ActionListener listener + ) { + return performRequestAsyncAndParseEntity( + openRequest, + RequestConverters::openPointInTime, + options, + OpenPointInTimeResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -1366,10 +1639,15 @@ public final Cancellable openPointInTimeAsync(OpenPointInTimeRequest openRequest * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized * @return the response */ - public final ClosePointInTimeResponse closePointInTime(ClosePointInTimeRequest closeRequest, - RequestOptions options) throws IOException { - return performRequestAndParseEntity(closeRequest, RequestConverters::closePointInTime, options, - ClosePointInTimeResponse::fromXContent, emptySet()); + public final ClosePointInTimeResponse closePointInTime(ClosePointInTimeRequest closeRequest, RequestOptions options) + throws IOException { + return performRequestAndParseEntity( + closeRequest, + RequestConverters::closePointInTime, + options, + ClosePointInTimeResponse::fromXContent, + emptySet() + ); } /** @@ -1382,11 +1660,19 @@ public final ClosePointInTimeResponse closePointInTime(ClosePointInTimeRequest c * @param listener the listener to be notified upon request completion * @return a cancellable that may be used to cancel the request */ - public final Cancellable closePointInTimeAsync(ClosePointInTimeRequest closeRequest, - RequestOptions options, - ActionListener listener) { - return performRequestAsyncAndParseEntity(closeRequest, RequestConverters::closePointInTime, - options, ClosePointInTimeResponse::fromXContent, listener, emptySet()); + public final Cancellable closePointInTimeAsync( + ClosePointInTimeRequest closeRequest, + RequestOptions options, + ActionListener listener + ) { + return performRequestAsyncAndParseEntity( + closeRequest, + RequestConverters::closePointInTime, + options, + ClosePointInTimeResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -1397,10 +1683,15 @@ public final Cancellable closePointInTimeAsync(ClosePointInTimeRequest closeRequ * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized * @return the response */ - public final SearchTemplateResponse searchTemplate(SearchTemplateRequest searchTemplateRequest, - RequestOptions options) throws IOException { - return performRequestAndParseEntity(searchTemplateRequest, RequestConverters::searchTemplate, options, - SearchTemplateResponse::fromXContent, emptySet()); + public final SearchTemplateResponse searchTemplate(SearchTemplateRequest searchTemplateRequest, RequestOptions options) + throws IOException { + return performRequestAndParseEntity( + searchTemplateRequest, + RequestConverters::searchTemplate, + options, + SearchTemplateResponse::fromXContent, + emptySet() + ); } /** @@ -1410,10 +1701,19 @@ public final SearchTemplateResponse searchTemplate(SearchTemplateRequest searchT * on elastic.co. * @return cancellable that may be used to cancel the request */ - public final Cancellable searchTemplateAsync(SearchTemplateRequest searchTemplateRequest, RequestOptions options, - ActionListener listener) { - return performRequestAsyncAndParseEntity(searchTemplateRequest, RequestConverters::searchTemplate, options, - SearchTemplateResponse::fromXContent, listener, emptySet()); + public final Cancellable searchTemplateAsync( + SearchTemplateRequest searchTemplateRequest, + RequestOptions options, + ActionListener listener + ) { + return performRequestAsyncAndParseEntity( + searchTemplateRequest, + RequestConverters::searchTemplate, + options, + SearchTemplateResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -1424,13 +1724,13 @@ public final Cancellable searchTemplateAsync(SearchTemplateRequest searchTemplat * @return the response */ public final ExplainResponse explain(ExplainRequest explainRequest, RequestOptions options) throws IOException { - return performRequest(explainRequest, RequestConverters::explain, options, - response -> { - CheckedFunction entityParser = - parser -> ExplainResponse.fromXContent(parser, convertExistsResponse(response)); - return parseEntity(response.getEntity(), entityParser); - }, - singleton(404)); + return performRequest(explainRequest, RequestConverters::explain, options, response -> { + CheckedFunction entityParser = parser -> ExplainResponse.fromXContent( + parser, + convertExistsResponse(response) + ); + return parseEntity(response.getEntity(), entityParser); + }, singleton(404)); } /** @@ -1443,16 +1743,15 @@ public final ExplainResponse explain(ExplainRequest explainRequest, RequestOptio * @return cancellable that may be used to cancel the request */ public final Cancellable explainAsync(ExplainRequest explainRequest, RequestOptions options, ActionListener listener) { - return performRequestAsync(explainRequest, RequestConverters::explain, options, - response -> { - CheckedFunction entityParser = - parser -> ExplainResponse.fromXContent(parser, convertExistsResponse(response)); - return parseEntity(response.getEntity(), entityParser); - }, - listener, singleton(404)); + return performRequestAsync(explainRequest, RequestConverters::explain, options, response -> { + CheckedFunction entityParser = parser -> ExplainResponse.fromXContent( + parser, + convertExistsResponse(response) + ); + return parseEntity(response.getEntity(), entityParser); + }, listener, singleton(404)); } - /** * Calls the Term Vectors API * @@ -1463,8 +1762,13 @@ public final Cancellable explainAsync(ExplainRequest explainRequest, RequestOpti * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized */ public final TermVectorsResponse termvectors(TermVectorsRequest request, RequestOptions options) throws IOException { - return performRequestAndParseEntity(request, RequestConverters::termVectors, options, TermVectorsResponse::fromXContent, - emptySet()); + return performRequestAndParseEntity( + request, + RequestConverters::termVectors, + options, + TermVectorsResponse::fromXContent, + emptySet() + ); } /** @@ -1477,14 +1781,21 @@ public final TermVectorsResponse termvectors(TermVectorsRequest request, Request * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public final Cancellable termvectorsAsync(TermVectorsRequest request, RequestOptions options, - ActionListener listener) { - return performRequestAsyncAndParseEntity(request, RequestConverters::termVectors, options, - TermVectorsResponse::fromXContent, listener, - emptySet()); + public final Cancellable termvectorsAsync( + TermVectorsRequest request, + RequestOptions options, + ActionListener listener + ) { + return performRequestAsyncAndParseEntity( + request, + RequestConverters::termVectors, + options, + TermVectorsResponse::fromXContent, + listener, + emptySet() + ); } - /** * Calls the Multi Term Vectors API * @@ -1496,10 +1807,14 @@ public final Cancellable termvectorsAsync(TermVectorsRequest request, RequestOpt */ public final MultiTermVectorsResponse mtermvectors(MultiTermVectorsRequest request, RequestOptions options) throws IOException { return performRequestAndParseEntity( - request, RequestConverters::mtermVectors, options, MultiTermVectorsResponse::fromXContent, emptySet()); + request, + RequestConverters::mtermVectors, + options, + MultiTermVectorsResponse::fromXContent, + emptySet() + ); } - /** * Asynchronously calls the Multi Term Vectors API * @@ -1510,13 +1825,21 @@ public final MultiTermVectorsResponse mtermvectors(MultiTermVectorsRequest reque * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public final Cancellable mtermvectorsAsync(MultiTermVectorsRequest request, RequestOptions options, - ActionListener listener) { + public final Cancellable mtermvectorsAsync( + MultiTermVectorsRequest request, + RequestOptions options, + ActionListener listener + ) { return performRequestAsyncAndParseEntity( - request, RequestConverters::mtermVectors, options, MultiTermVectorsResponse::fromXContent, listener, emptySet()); + request, + RequestConverters::mtermVectors, + options, + MultiTermVectorsResponse::fromXContent, + listener, + emptySet() + ); } - /** * Executes a request using the Ranking Evaluation API. * See Ranking Evaluation API @@ -1526,21 +1849,30 @@ public final Cancellable mtermvectorsAsync(MultiTermVectorsRequest request, Requ * @return the response */ public final RankEvalResponse rankEval(RankEvalRequest rankEvalRequest, RequestOptions options) throws IOException { - return performRequestAndParseEntity(rankEvalRequest, RequestConverters::rankEval, options, RankEvalResponse::fromXContent, - emptySet()); + return performRequestAndParseEntity( + rankEvalRequest, + RequestConverters::rankEval, + options, + RankEvalResponse::fromXContent, + emptySet() + ); } - /** * Executes a request using the Multi Search Template API. * * See Multi Search Template API * on elastic.co. */ - public final MultiSearchTemplateResponse msearchTemplate(MultiSearchTemplateRequest multiSearchTemplateRequest, - RequestOptions options) throws IOException { - return performRequestAndParseEntity(multiSearchTemplateRequest, RequestConverters::multiSearchTemplate, - options, MultiSearchTemplateResponse::fromXContext, emptySet()); + public final MultiSearchTemplateResponse msearchTemplate(MultiSearchTemplateRequest multiSearchTemplateRequest, RequestOptions options) + throws IOException { + return performRequestAndParseEntity( + multiSearchTemplateRequest, + RequestConverters::multiSearchTemplate, + options, + MultiSearchTemplateResponse::fromXContext, + emptySet() + ); } /** @@ -1550,11 +1882,19 @@ public final MultiSearchTemplateResponse msearchTemplate(MultiSearchTemplateRequ * on elastic.co. * @return cancellable that may be used to cancel the request */ - public final Cancellable msearchTemplateAsync(MultiSearchTemplateRequest multiSearchTemplateRequest, - RequestOptions options, - ActionListener listener) { - return performRequestAsyncAndParseEntity(multiSearchTemplateRequest, RequestConverters::multiSearchTemplate, - options, MultiSearchTemplateResponse::fromXContext, listener, emptySet()); + public final Cancellable msearchTemplateAsync( + MultiSearchTemplateRequest multiSearchTemplateRequest, + RequestOptions options, + ActionListener listener + ) { + return performRequestAsyncAndParseEntity( + multiSearchTemplateRequest, + RequestConverters::multiSearchTemplate, + options, + MultiSearchTemplateResponse::fromXContext, + listener, + emptySet() + ); } /** @@ -1566,11 +1906,19 @@ public final Cancellable msearchTemplateAsync(MultiSearchTemplateRequest multiSe * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public final Cancellable rankEvalAsync(RankEvalRequest rankEvalRequest, RequestOptions options, - ActionListener listener) { - return performRequestAsyncAndParseEntity(rankEvalRequest, RequestConverters::rankEval, options, - RankEvalResponse::fromXContent, listener, - emptySet()); + public final Cancellable rankEvalAsync( + RankEvalRequest rankEvalRequest, + RequestOptions options, + ActionListener listener + ) { + return performRequestAsyncAndParseEntity( + rankEvalRequest, + RequestConverters::rankEval, + options, + RankEvalResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -1581,10 +1929,15 @@ public final Cancellable rankEvalAsync(RankEvalRequest rankEvalRequest, RequestO * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized * @return the response */ - public final FieldCapabilitiesResponse fieldCaps(FieldCapabilitiesRequest fieldCapabilitiesRequest, - RequestOptions options) throws IOException { - return performRequestAndParseEntity(fieldCapabilitiesRequest, RequestConverters::fieldCaps, options, - FieldCapabilitiesResponse::fromXContent, emptySet()); + public final FieldCapabilitiesResponse fieldCaps(FieldCapabilitiesRequest fieldCapabilitiesRequest, RequestOptions options) + throws IOException { + return performRequestAndParseEntity( + fieldCapabilitiesRequest, + RequestConverters::fieldCaps, + options, + FieldCapabilitiesResponse::fromXContent, + emptySet() + ); } /** @@ -1596,8 +1949,13 @@ public final FieldCapabilitiesResponse fieldCaps(FieldCapabilitiesRequest fieldC * @return the response */ public GetStoredScriptResponse getScript(GetStoredScriptRequest request, RequestOptions options) throws IOException { - return performRequestAndParseEntity(request, RequestConverters::getScript, options, - GetStoredScriptResponse::fromXContent, emptySet()); + return performRequestAndParseEntity( + request, + RequestConverters::getScript, + options, + GetStoredScriptResponse::fromXContent, + emptySet() + ); } /** @@ -1609,10 +1967,19 @@ public GetStoredScriptResponse getScript(GetStoredScriptRequest request, Request * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getScriptAsync(GetStoredScriptRequest request, RequestOptions options, - ActionListener listener) { - return performRequestAsyncAndParseEntity(request, RequestConverters::getScript, options, - GetStoredScriptResponse::fromXContent, listener, emptySet()); + public Cancellable getScriptAsync( + GetStoredScriptRequest request, + RequestOptions options, + ActionListener listener + ) { + return performRequestAsyncAndParseEntity( + request, + RequestConverters::getScript, + options, + GetStoredScriptResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -1624,8 +1991,13 @@ public Cancellable getScriptAsync(GetStoredScriptRequest request, RequestOptions * @return the response */ public AcknowledgedResponse deleteScript(DeleteStoredScriptRequest request, RequestOptions options) throws IOException { - return performRequestAndParseEntity(request, RequestConverters::deleteScript, options, - AcknowledgedResponse::fromXContent, emptySet()); + return performRequestAndParseEntity( + request, + RequestConverters::deleteScript, + options, + AcknowledgedResponse::fromXContent, + emptySet() + ); } /** @@ -1637,10 +2009,19 @@ public AcknowledgedResponse deleteScript(DeleteStoredScriptRequest request, Requ * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable deleteScriptAsync(DeleteStoredScriptRequest request, RequestOptions options, - ActionListener listener) { - return performRequestAsyncAndParseEntity(request, RequestConverters::deleteScript, options, - AcknowledgedResponse::fromXContent, listener, emptySet()); + public Cancellable deleteScriptAsync( + DeleteStoredScriptRequest request, + RequestOptions options, + ActionListener listener + ) { + return performRequestAsyncAndParseEntity( + request, + RequestConverters::deleteScript, + options, + AcknowledgedResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -1651,10 +2032,14 @@ public Cancellable deleteScriptAsync(DeleteStoredScriptRequest request, RequestO * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized * @return the response */ - public AcknowledgedResponse putScript(PutStoredScriptRequest putStoredScriptRequest, - RequestOptions options) throws IOException { - return performRequestAndParseEntity(putStoredScriptRequest, RequestConverters::putScript, options, - AcknowledgedResponse::fromXContent, emptySet()); + public AcknowledgedResponse putScript(PutStoredScriptRequest putStoredScriptRequest, RequestOptions options) throws IOException { + return performRequestAndParseEntity( + putStoredScriptRequest, + RequestConverters::putScript, + options, + AcknowledgedResponse::fromXContent, + emptySet() + ); } /** @@ -1666,10 +2051,19 @@ public AcknowledgedResponse putScript(PutStoredScriptRequest putStoredScriptRequ * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable putScriptAsync(PutStoredScriptRequest putStoredScriptRequest, RequestOptions options, - ActionListener listener) { - return performRequestAsyncAndParseEntity(putStoredScriptRequest, RequestConverters::putScript, options, - AcknowledgedResponse::fromXContent, listener, emptySet()); + public Cancellable putScriptAsync( + PutStoredScriptRequest putStoredScriptRequest, + RequestOptions options, + ActionListener listener + ) { + return performRequestAsyncAndParseEntity( + putStoredScriptRequest, + RequestConverters::putScript, + options, + AcknowledgedResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -1681,10 +2075,19 @@ public Cancellable putScriptAsync(PutStoredScriptRequest putStoredScriptRequest, * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public final Cancellable fieldCapsAsync(FieldCapabilitiesRequest fieldCapabilitiesRequest, RequestOptions options, - ActionListener listener) { - return performRequestAsyncAndParseEntity(fieldCapabilitiesRequest, RequestConverters::fieldCaps, options, - FieldCapabilitiesResponse::fromXContent, listener, emptySet()); + public final Cancellable fieldCapsAsync( + FieldCapabilitiesRequest fieldCapabilitiesRequest, + RequestOptions options, + ActionListener listener + ) { + return performRequestAsyncAndParseEntity( + fieldCapabilitiesRequest, + RequestConverters::fieldCaps, + options, + FieldCapabilitiesResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -1692,25 +2095,27 @@ public final Cancellable fieldCapsAsync(FieldCapabilitiesRequest fieldCapabiliti * layer has been added to the ReST client, and requests should extend {@link Validatable} instead of {@link ActionRequest}. */ @Deprecated - protected final Resp performRequestAndParseEntity(Req request, - CheckedFunction requestConverter, - RequestOptions options, - CheckedFunction entityParser, - Set ignores) throws IOException { - return performRequest(request, requestConverter, options, - response -> parseEntity(response.getEntity(), entityParser), ignores); + protected final Resp performRequestAndParseEntity( + Req request, + CheckedFunction requestConverter, + RequestOptions options, + CheckedFunction entityParser, + Set ignores + ) throws IOException { + return performRequest(request, requestConverter, options, response -> parseEntity(response.getEntity(), entityParser), ignores); } /** * Defines a helper method for performing a request and then parsing the returned entity using the provided entityParser. */ - protected final Resp performRequestAndParseEntity(Req request, - CheckedFunction requestConverter, - RequestOptions options, - CheckedFunction entityParser, - Set ignores) throws IOException { - return performRequest(request, requestConverter, options, - response -> parseEntity(response.getEntity(), entityParser), ignores); + protected final Resp performRequestAndParseEntity( + Req request, + CheckedFunction requestConverter, + RequestOptions options, + CheckedFunction entityParser, + Set ignores + ) throws IOException { + return performRequest(request, requestConverter, options, response -> parseEntity(response.getEntity(), entityParser), ignores); } /** @@ -1718,11 +2123,13 @@ protected final Resp performRequestAndParseEntit * layer has been added to the ReST client, and requests should extend {@link Validatable} instead of {@link ActionRequest}. */ @Deprecated - protected final Resp performRequest(Req request, - CheckedFunction requestConverter, - RequestOptions options, - CheckedFunction responseConverter, - Set ignores) throws IOException { + protected final Resp performRequest( + Req request, + CheckedFunction requestConverter, + RequestOptions options, + CheckedFunction responseConverter, + Set ignores + ) throws IOException { ActionRequestValidationException validationException = request.validate(); if (validationException != null && validationException.validationErrors().isEmpty() == false) { throw validationException; @@ -1733,11 +2140,13 @@ protected final Resp performRequest(Req reques /** * Defines a helper method for performing a request. */ - protected final Resp performRequest(Req request, - CheckedFunction requestConverter, - RequestOptions options, - CheckedFunction responseConverter, - Set ignores) throws IOException { + protected final Resp performRequest( + Req request, + CheckedFunction requestConverter, + RequestOptions options, + CheckedFunction responseConverter, + Set ignores + ) throws IOException { Optional validationException = request.validate(); if (validationException != null && validationException.isPresent()) { throw validationException.get(); @@ -1748,11 +2157,13 @@ protected final Resp performRequest(Req request, /** * Provides common functionality for performing a request. */ - private Resp internalPerformRequest(Req request, - CheckedFunction requestConverter, - RequestOptions options, - CheckedFunction responseConverter, - Set ignores) throws IOException { + private Resp internalPerformRequest( + Req request, + CheckedFunction requestConverter, + RequestOptions options, + CheckedFunction responseConverter, + Set ignores + ) throws IOException { Request req = requestConverter.apply(request); req.setOptions(options); Response response; @@ -1775,7 +2186,7 @@ private Resp internalPerformRequest(Req request, try { return responseConverter.apply(response); - } catch(Exception e) { + } catch (Exception e) { throw new IOException("Unable to parse response body for " + response, e); } } @@ -1784,11 +2195,12 @@ private Resp internalPerformRequest(Req request, * Defines a helper method for requests that can 404 and in which case will return an empty Optional * otherwise tries to parse the response body */ - protected final Optional performRequestAndParseOptionalEntity(Req request, - CheckedFunction requestConverter, - RequestOptions options, - CheckedFunction entityParser - ) throws IOException { + protected final Optional performRequestAndParseOptionalEntity( + Req request, + CheckedFunction requestConverter, + RequestOptions options, + CheckedFunction entityParser + ) throws IOException { Optional validationException = request.validate(); if (validationException != null && validationException.isPresent()) { throw validationException.get(); @@ -1818,40 +2230,60 @@ protected final Optional performRequestAnd * @return Cancellable instance that may be used to cancel the request */ @Deprecated - protected final Cancellable performRequestAsyncAndParseEntity(Req request, - CheckedFunction requestConverter, - RequestOptions options, - CheckedFunction entityParser, - ActionListener listener, Set ignores) { - return performRequestAsync(request, requestConverter, options, - response -> parseEntity(response.getEntity(), entityParser), listener, ignores); + protected final Cancellable performRequestAsyncAndParseEntity( + Req request, + CheckedFunction requestConverter, + RequestOptions options, + CheckedFunction entityParser, + ActionListener listener, + Set ignores + ) { + return performRequestAsync( + request, + requestConverter, + options, + response -> parseEntity(response.getEntity(), entityParser), + listener, + ignores + ); } /** * Defines a helper method for asynchronously performing a request. * @return Cancellable instance that may be used to cancel the request */ - protected final Cancellable performRequestAsyncAndParseEntity(Req request, - CheckedFunction requestConverter, - RequestOptions options, - CheckedFunction entityParser, - ActionListener listener, Set ignores) { - return performRequestAsync(request, requestConverter, options, - response -> parseEntity(response.getEntity(), entityParser), listener, ignores); + protected final Cancellable performRequestAsyncAndParseEntity( + Req request, + CheckedFunction requestConverter, + RequestOptions options, + CheckedFunction entityParser, + ActionListener listener, + Set ignores + ) { + return performRequestAsync( + request, + requestConverter, + options, + response -> parseEntity(response.getEntity(), entityParser), + listener, + ignores + ); } - /** * @deprecated If creating a new HLRC ReST API call, consider creating new actions instead of reusing server actions. The Validation * layer has been added to the ReST client, and requests should extend {@link Validatable} instead of {@link ActionRequest}. * @return Cancellable instance that may be used to cancel the request */ @Deprecated - protected final Cancellable performRequestAsync(Req request, - CheckedFunction requestConverter, - RequestOptions options, - CheckedFunction responseConverter, - ActionListener listener, Set ignores) { + protected final Cancellable performRequestAsync( + Req request, + CheckedFunction requestConverter, + RequestOptions options, + CheckedFunction responseConverter, + ActionListener listener, + Set ignores + ) { ActionRequestValidationException validationException = request.validate(); if (validationException != null && validationException.validationErrors().isEmpty() == false) { listener.onFailure(validationException); @@ -1864,11 +2296,14 @@ protected final Cancellable performRequestAsyn * Defines a helper method for asynchronously performing a request. * @return Cancellable instance that may be used to cancel the request */ - protected final Cancellable performRequestAsync(Req request, - CheckedFunction requestConverter, - RequestOptions options, - CheckedFunction responseConverter, - ActionListener listener, Set ignores) { + protected final Cancellable performRequestAsync( + Req request, + CheckedFunction requestConverter, + RequestOptions options, + CheckedFunction responseConverter, + ActionListener listener, + Set ignores + ) { Optional validationException = request.validate(); if (validationException != null && validationException.isPresent()) { listener.onFailure(validationException.get()); @@ -1881,11 +2316,14 @@ protected final Cancellable performRequestAsync( * Provides common functionality for asynchronously performing a request. * @return Cancellable instance that may be used to cancel the request */ - private Cancellable internalPerformRequestAsync(Req request, - CheckedFunction requestConverter, - RequestOptions options, - CheckedFunction responseConverter, - ActionListener listener, Set ignores) { + private Cancellable internalPerformRequestAsync( + Req request, + CheckedFunction requestConverter, + RequestOptions options, + CheckedFunction responseConverter, + ActionListener listener, + Set ignores + ) { Request req; try { req = requestConverter.apply(request); @@ -1899,15 +2337,17 @@ private Cancellable internalPerformRequestAsync(Req request, return performClientRequestAsync(req, responseListener); } - - final ResponseListener wrapResponseListener(CheckedFunction responseConverter, - ActionListener actionListener, Set ignores) { + final ResponseListener wrapResponseListener( + CheckedFunction responseConverter, + ActionListener actionListener, + Set ignores + ) { return new ResponseListener() { @Override public void onSuccess(Response response) { try { actionListener.onResponse(responseConverter.apply(response)); - } catch(Exception e) { + } catch (Exception e) { IOException ioe = new IOException("Unable to parse response body for " + response, e); onFailure(ioe); } @@ -1942,11 +2382,13 @@ public void onFailure(Exception exception) { * Asynchronous request which returns empty {@link Optional}s in the case of 404s or parses entity into an Optional * @return Cancellable instance that may be used to cancel the request */ - protected final Cancellable performRequestAsyncAndParseOptionalEntity(Req request, - CheckedFunction requestConverter, - RequestOptions options, - CheckedFunction entityParser, - ActionListener> listener) { + protected final Cancellable performRequestAsyncAndParseOptionalEntity( + Req request, + CheckedFunction requestConverter, + RequestOptions options, + CheckedFunction entityParser, + ActionListener> listener + ) { Optional validationException = request.validate(); if (validationException != null && validationException.isPresent()) { listener.onFailure(validationException.get()); @@ -1960,13 +2402,17 @@ protected final Cancellable performRequestAsyncA return Cancellable.NO_OP; } req.setOptions(options); - ResponseListener responseListener = wrapResponseListener404sOptional(response -> parseEntity(response.getEntity(), - entityParser), listener); + ResponseListener responseListener = wrapResponseListener404sOptional( + response -> parseEntity(response.getEntity(), entityParser), + listener + ); return performClientRequestAsync(req, responseListener); } - final ResponseListener wrapResponseListener404sOptional(CheckedFunction responseConverter, - ActionListener> actionListener) { + final ResponseListener wrapResponseListener404sOptional( + CheckedFunction responseConverter, + ActionListener> actionListener + ) { return new ResponseListener() { @Override public void onSuccess(Response response) { @@ -1984,7 +2430,7 @@ public void onFailure(Exception exception) { ResponseException responseException = (ResponseException) exception; Response response = responseException.getResponse(); if (RestStatus.NOT_FOUND.getStatus() == response.getStatusLine().getStatusCode()) { - actionListener.onResponse(Optional.empty()); + actionListener.onResponse(Optional.empty()); } else { actionListener.onFailure(parseResponseException(responseException)); } @@ -2009,8 +2455,7 @@ protected final ElasticsearchStatusException parseResponseException(ResponseExce RestStatus restStatus = RestStatus.fromCode(response.getStatusLine().getStatusCode()); if (entity == null) { - elasticsearchException = new ElasticsearchStatusException( - responseException.getMessage(), restStatus, responseException); + elasticsearchException = new ElasticsearchStatusException(responseException.getMessage(), restStatus, responseException); } else { try { elasticsearchException = parseEntity(entity, BytesRestResponse::errorFromXContent); @@ -2023,8 +2468,8 @@ protected final ElasticsearchStatusException parseResponseException(ResponseExce return elasticsearchException; } - protected final Resp parseEntity(final HttpEntity entity, - final CheckedFunction entityParser) throws IOException { + protected final Resp parseEntity(final HttpEntity entity, final CheckedFunction entityParser) + throws IOException { if (entity == null) { throw new IllegalStateException("Response body expected but not returned"); } @@ -2050,6 +2495,7 @@ private enum EntityType { public String header() { return "application/json"; } + @Override public String compatibleHeader() { return "application/vnd.elasticsearch+json; compatible-with=7"; @@ -2060,6 +2506,7 @@ public String compatibleHeader() { public String header() { return "application/x-ndjson"; } + @Override public String compatibleHeader() { return "application/vnd.elasticsearch+x-ndjson; compatible-with=7"; @@ -2070,6 +2517,7 @@ public String compatibleHeader() { public String header() { return "application/*"; } + @Override public String compatibleHeader() { return "application/vnd.elasticsearch+json; compatible-with=7"; @@ -2080,6 +2528,7 @@ public String compatibleHeader() { public String header() { return "application/yaml"; } + @Override public String compatibleHeader() { return "application/vnd.elasticsearch+yaml; compatible-with=7"; @@ -2090,6 +2539,7 @@ public String compatibleHeader() { public String header() { return "application/smile"; } + @Override public String compatibleHeader() { return "application/vnd.elasticsearch+smile; compatible-with=7"; @@ -2100,6 +2550,7 @@ public String compatibleHeader() { public String header() { return "application/cbor"; } + @Override public String compatibleHeader() { return "application/vnd.elasticsearch+cbor; compatible-with=7"; @@ -2107,6 +2558,7 @@ public String compatibleHeader() { }; public abstract String header(); + public abstract String compatibleHeader(); @Override @@ -2150,10 +2602,9 @@ public void onResponse(Optional validation) { // Send the request and propagate cancellation Cancellable call = client.performRequestAsync(request, listener); cancellationForwarder.whenComplete((r, t) -> - // Forward cancellation to the actual request (no need to check parameters as the - // only way for cancellationForwarder to be completed is by being cancelled). - call.cancel() - ); + // Forward cancellation to the actual request (no need to check parameters as the + // only way for cancellationForwarder to be completed is by being cancelled). + call.cancel()); } else { // Version validation wasn't successful, fail the request with the validation result. listener.onFailure(new ElasticsearchException(validation.get())); @@ -2171,12 +2622,11 @@ public void onFailure(Exception e) { return result; }; - /** * Go through all the request's existing headers, looking for {@code headerName} headers and if they exist, * changing them to use version compatibility. If no request headers are changed, modify the entity type header if appropriate */ - boolean addCompatibilityFor(RequestOptions.Builder newOptions, Header entityHeader, String headerName) { + boolean addCompatibilityFor(RequestOptions.Builder newOptions, Header entityHeader, String headerName) { // Modify any existing "Content-Type" headers on the request to use the version compatibility, if available boolean contentTypeModified = false; for (Header header : new ArrayList<>(newOptions.getHeaders())) { @@ -2290,8 +2740,9 @@ public void onSuccess(Response response) { validation = getVersionValidation(response); } catch (Exception e) { logger.error("Failed to parse info response", e); - validation = Optional.of("Failed to parse info response. Check logs for detailed information - " + - e.getMessage()); + validation = Optional.of( + "Failed to parse info response. Check logs for detailed information - " + e.getMessage() + ); } future.onResponse(validation); } @@ -2369,8 +2820,8 @@ private Optional getVersionValidation(Response response) throws IOExcept String header = response.getHeader("X-Elastic-Product"); if (header == null) { return Optional.of( - "Missing [X-Elastic-Product] header. Please check that you are connecting to an Elasticsearch " + - "instance, and that any networking filters are preserving that header." + "Missing [X-Elastic-Product] header. Please check that you are connecting to an Elasticsearch " + + "instance, and that any networking filters are preserving that header." ); } @@ -2410,15 +2861,13 @@ static List getDefaultNamedXContents() { map.put(StatsAggregationBuilder.NAME, (p, c) -> ParsedStats.fromXContent(p, (String) c)); map.put(StatsBucketPipelineAggregationBuilder.NAME, (p, c) -> ParsedStatsBucket.fromXContent(p, (String) c)); map.put(ExtendedStatsAggregationBuilder.NAME, (p, c) -> ParsedExtendedStats.fromXContent(p, (String) c)); - map.put(ExtendedStatsBucketPipelineAggregationBuilder.NAME, - (p, c) -> ParsedExtendedStatsBucket.fromXContent(p, (String) c)); + map.put(ExtendedStatsBucketPipelineAggregationBuilder.NAME, (p, c) -> ParsedExtendedStatsBucket.fromXContent(p, (String) c)); map.put(GeoBoundsAggregationBuilder.NAME, (p, c) -> ParsedGeoBounds.fromXContent(p, (String) c)); map.put(GeoCentroidAggregationBuilder.NAME, (p, c) -> ParsedGeoCentroid.fromXContent(p, (String) c)); map.put(HistogramAggregationBuilder.NAME, (p, c) -> ParsedHistogram.fromXContent(p, (String) c)); map.put(DateHistogramAggregationBuilder.NAME, (p, c) -> ParsedDateHistogram.fromXContent(p, (String) c)); map.put(AutoDateHistogramAggregationBuilder.NAME, (p, c) -> ParsedAutoDateHistogram.fromXContent(p, (String) c)); - map.put(VariableWidthHistogramAggregationBuilder.NAME, - (p, c) -> ParsedVariableWidthHistogram.fromXContent(p, (String) c)); + map.put(VariableWidthHistogramAggregationBuilder.NAME, (p, c) -> ParsedVariableWidthHistogram.fromXContent(p, (String) c)); map.put(StringTerms.NAME, (p, c) -> ParsedStringTerms.fromXContent(p, (String) c)); map.put(LongTerms.NAME, (p, c) -> ParsedLongTerms.fromXContent(p, (String) c)); map.put(DoubleTerms.NAME, (p, c) -> ParsedDoubleTerms.fromXContent(p, (String) c)); @@ -2445,16 +2894,32 @@ static List getDefaultNamedXContents() { map.put(CompositeAggregationBuilder.NAME, (p, c) -> ParsedComposite.fromXContent(p, (String) c)); map.put(StringStatsAggregationBuilder.NAME, (p, c) -> ParsedStringStats.PARSER.parse(p, (String) c)); map.put(TopMetricsAggregationBuilder.NAME, (p, c) -> ParsedTopMetrics.PARSER.parse(p, (String) c)); - map.put(InferencePipelineAggregationBuilder.NAME, (p, c) -> ParsedInference.fromXContent(p, (String ) (c))); - List entries = map.entrySet().stream() - .map(entry -> new NamedXContentRegistry.Entry(Aggregation.class, new ParseField(entry.getKey()), entry.getValue())) - .collect(Collectors.toList()); - entries.add(new NamedXContentRegistry.Entry(Suggest.Suggestion.class, new ParseField(TermSuggestionBuilder.SUGGESTION_NAME), - (parser, context) -> TermSuggestion.fromXContent(parser, (String)context))); - entries.add(new NamedXContentRegistry.Entry(Suggest.Suggestion.class, new ParseField(PhraseSuggestionBuilder.SUGGESTION_NAME), - (parser, context) -> PhraseSuggestion.fromXContent(parser, (String)context))); - entries.add(new NamedXContentRegistry.Entry(Suggest.Suggestion.class, new ParseField(CompletionSuggestionBuilder.SUGGESTION_NAME), - (parser, context) -> CompletionSuggestion.fromXContent(parser, (String)context))); + map.put(InferencePipelineAggregationBuilder.NAME, (p, c) -> ParsedInference.fromXContent(p, (String) (c))); + List entries = map.entrySet() + .stream() + .map(entry -> new NamedXContentRegistry.Entry(Aggregation.class, new ParseField(entry.getKey()), entry.getValue())) + .collect(Collectors.toList()); + entries.add( + new NamedXContentRegistry.Entry( + Suggest.Suggestion.class, + new ParseField(TermSuggestionBuilder.SUGGESTION_NAME), + (parser, context) -> TermSuggestion.fromXContent(parser, (String) context) + ) + ); + entries.add( + new NamedXContentRegistry.Entry( + Suggest.Suggestion.class, + new ParseField(PhraseSuggestionBuilder.SUGGESTION_NAME), + (parser, context) -> PhraseSuggestion.fromXContent(parser, (String) context) + ) + ); + entries.add( + new NamedXContentRegistry.Entry( + Suggest.Suggestion.class, + new ParseField(CompletionSuggestionBuilder.SUGGESTION_NAME), + (parser, context) -> CompletionSuggestion.fromXContent(parser, (String) context) + ) + ); return entries; } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/RethrottleRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/RethrottleRequest.java index 709ea64770804..a518d74b14c20 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/RethrottleRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/RethrottleRequest.java @@ -39,7 +39,7 @@ public RethrottleRequest(TaskId taskId) { public RethrottleRequest(TaskId taskId, float requestsPerSecond) { Objects.requireNonNull(taskId, "taskId cannot be null"); if (requestsPerSecond <= 0) { - throw new IllegalArgumentException("requestsPerSecond needs to be positive value but was [" + requestsPerSecond+"]"); + throw new IllegalArgumentException("requestsPerSecond needs to be positive value but was [" + requestsPerSecond + "]"); } this.taskId = taskId; this.requestsPerSecond = requestsPerSecond; @@ -61,6 +61,6 @@ public float getRequestsPerSecond() { @Override public String toString() { - return "RethrottleRequest: taskID = " + taskId +"; reqestsPerSecond = " + requestsPerSecond; + return "RethrottleRequest: taskID = " + taskId + "; reqestsPerSecond = " + requestsPerSecond; } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/RollupClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/RollupClient.java index 397ffcf8116c0..743d4ffc7529e 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/RollupClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/RollupClient.java @@ -13,12 +13,12 @@ import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.core.AcknowledgedResponse; import org.elasticsearch.client.rollup.DeleteRollupJobRequest; +import org.elasticsearch.client.rollup.GetRollupCapsRequest; +import org.elasticsearch.client.rollup.GetRollupCapsResponse; import org.elasticsearch.client.rollup.GetRollupIndexCapsRequest; import org.elasticsearch.client.rollup.GetRollupIndexCapsResponse; import org.elasticsearch.client.rollup.GetRollupJobRequest; import org.elasticsearch.client.rollup.GetRollupJobResponse; -import org.elasticsearch.client.rollup.GetRollupCapsRequest; -import org.elasticsearch.client.rollup.GetRollupCapsResponse; import org.elasticsearch.client.rollup.PutRollupJobRequest; import org.elasticsearch.client.rollup.StartRollupJobRequest; import org.elasticsearch.client.rollup.StartRollupJobResponse; @@ -59,11 +59,13 @@ public class RollupClient { * @throws IOException in case there is a problem sending the request or parsing back the response */ public AcknowledgedResponse putRollupJob(PutRollupJobRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + return restHighLevelClient.performRequestAndParseEntity( + request, RollupRequestConverters::putJob, options, AcknowledgedResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -75,13 +77,19 @@ public AcknowledgedResponse putRollupJob(PutRollupJobRequest request, RequestOpt * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable putRollupJobAsync(PutRollupJobRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + public Cancellable putRollupJobAsync( + PutRollupJobRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, RollupRequestConverters::putJob, options, AcknowledgedResponse::fromXContent, - listener, Collections.emptySet()); + listener, + Collections.emptySet() + ); } /** @@ -93,12 +101,14 @@ public Cancellable putRollupJobAsync(PutRollupJobRequest request, RequestOptions * @return the response * @throws IOException in case there is a problem sending the request or parsing back the response */ - public StartRollupJobResponse startRollupJob(StartRollupJobRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + public StartRollupJobResponse startRollupJob(StartRollupJobRequest request, RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, RollupRequestConverters::startJob, options, StartRollupJobResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -110,13 +120,19 @@ public StartRollupJobResponse startRollupJob(StartRollupJobRequest request, Requ * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable startRollupJobAsync(StartRollupJobRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + public Cancellable startRollupJobAsync( + StartRollupJobRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, RollupRequestConverters::startJob, options, StartRollupJobResponse::fromXContent, - listener, Collections.emptySet()); + listener, + Collections.emptySet() + ); } /** @@ -128,12 +144,14 @@ public Cancellable startRollupJobAsync(StartRollupJobRequest request, RequestOpt * @return the response * @throws IOException in case there is a problem sending the request or parsing back the response */ - public StopRollupJobResponse stopRollupJob(StopRollupJobRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + public StopRollupJobResponse stopRollupJob(StopRollupJobRequest request, RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, RollupRequestConverters::stopJob, options, StopRollupJobResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -145,13 +163,19 @@ public StopRollupJobResponse stopRollupJob(StopRollupJobRequest request, Request * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable stopRollupJobAsync(StopRollupJobRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + public Cancellable stopRollupJobAsync( + StopRollupJobRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, RollupRequestConverters::stopJob, options, StopRollupJobResponse::fromXContent, - listener, Collections.emptySet()); + listener, + Collections.emptySet() + ); } /** @@ -164,12 +188,15 @@ public Cancellable stopRollupJobAsync(StopRollupJobRequest request, RequestOptio * @throws IOException in case there is a problem sending the request or parsing back the response */ public AcknowledgedResponse deleteRollupJob(DeleteRollupJobRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + return restHighLevelClient.performRequestAndParseEntity( + request, RollupRequestConverters::deleteJob, options, AcknowledgedResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } + /** * Asynchronously delete a rollup job from the cluster * See @@ -179,14 +206,19 @@ public AcknowledgedResponse deleteRollupJob(DeleteRollupJobRequest request, Requ * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable deleteRollupJobAsync(DeleteRollupJobRequest request, - RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + public Cancellable deleteRollupJobAsync( + DeleteRollupJobRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, RollupRequestConverters::deleteJob, options, AcknowledgedResponse::fromXContent, - listener, Collections.emptySet()); + listener, + Collections.emptySet() + ); } /** @@ -199,11 +231,13 @@ public Cancellable deleteRollupJobAsync(DeleteRollupJobRequest request, * @throws IOException in case there is a problem sending the request or parsing back the response */ public GetRollupJobResponse getRollupJob(GetRollupJobRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + return restHighLevelClient.performRequestAndParseEntity( + request, RollupRequestConverters::getJob, options, GetRollupJobResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -215,13 +249,19 @@ public GetRollupJobResponse getRollupJob(GetRollupJobRequest request, RequestOpt * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getRollupJobAsync(GetRollupJobRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + public Cancellable getRollupJobAsync( + GetRollupJobRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, RollupRequestConverters::getJob, options, GetRollupJobResponse::fromXContent, - listener, Collections.emptySet()); + listener, + Collections.emptySet() + ); } /** @@ -235,11 +275,12 @@ public Cancellable getRollupJobAsync(GetRollupJobRequest request, RequestOptions */ public SearchResponse search(SearchRequest request, RequestOptions options) throws IOException { return restHighLevelClient.performRequestAndParseEntity( - request, - RollupRequestConverters::search, - options, - SearchResponse::fromXContent, - Collections.emptySet()); + request, + RollupRequestConverters::search, + options, + SearchResponse::fromXContent, + Collections.emptySet() + ); } /** @@ -253,12 +294,13 @@ public SearchResponse search(SearchRequest request, RequestOptions options) thro */ public Cancellable searchAsync(SearchRequest request, RequestOptions options, ActionListener listener) { return restHighLevelClient.performRequestAsyncAndParseEntity( - request, - RollupRequestConverters::search, - options, - SearchResponse::fromXContent, - listener, - Collections.emptySet()); + request, + RollupRequestConverters::search, + options, + SearchResponse::fromXContent, + listener, + Collections.emptySet() + ); } /** @@ -271,11 +313,13 @@ public Cancellable searchAsync(SearchRequest request, RequestOptions options, Ac * @throws IOException in case there is a problem sending the request or parsing back the response */ public GetRollupCapsResponse getRollupCapabilities(GetRollupCapsRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + return restHighLevelClient.performRequestAndParseEntity( + request, RollupRequestConverters::getRollupCaps, options, GetRollupCapsResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -287,14 +331,19 @@ public GetRollupCapsResponse getRollupCapabilities(GetRollupCapsRequest request, * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getRollupCapabilitiesAsync(GetRollupCapsRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + public Cancellable getRollupCapabilitiesAsync( + GetRollupCapsRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, RollupRequestConverters::getRollupCaps, options, GetRollupCapsResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -306,13 +355,15 @@ public Cancellable getRollupCapabilitiesAsync(GetRollupCapsRequest request, Requ * @return the response * @throws IOException in case there is a problem sending the request or parsing back the response */ - public GetRollupIndexCapsResponse getRollupIndexCapabilities(GetRollupIndexCapsRequest request, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + public GetRollupIndexCapsResponse getRollupIndexCapabilities(GetRollupIndexCapsRequest request, RequestOptions options) + throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, RollupRequestConverters::getRollupIndexCaps, options, GetRollupIndexCapsResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -324,13 +375,18 @@ public GetRollupIndexCapsResponse getRollupIndexCapabilities(GetRollupIndexCapsR * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getRollupIndexCapabilitiesAsync(GetRollupIndexCapsRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + public Cancellable getRollupIndexCapabilitiesAsync( + GetRollupIndexCapsRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, RollupRequestConverters::getRollupIndexCaps, options, GetRollupIndexCapsResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/RollupRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/RollupRequestConverters.java index 809c9a161509a..b0177c1c57f73 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/RollupRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/RollupRequestConverters.java @@ -27,12 +27,10 @@ final class RollupRequestConverters { - private RollupRequestConverters() { - } + private RollupRequestConverters() {} static Request putJob(final PutRollupJobRequest putRollupJobRequest) throws IOException { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_rollup", "job") + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_rollup", "job") .addPathPart(putRollupJobRequest.getConfig().getId()) .build(); Request request = new Request(HttpPut.METHOD_NAME, endpoint); @@ -41,8 +39,7 @@ static Request putJob(final PutRollupJobRequest putRollupJobRequest) throws IOEx } static Request startJob(final StartRollupJobRequest startRollupJobRequest) throws IOException { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_rollup", "job") + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_rollup", "job") .addPathPart(startRollupJobRequest.getJobId()) .addPathPartAsIs("_start") .build(); @@ -50,8 +47,7 @@ static Request startJob(final StartRollupJobRequest startRollupJobRequest) throw } static Request stopJob(final StopRollupJobRequest stopRollupJobRequest) throws IOException { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_rollup", "job") + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_rollup", "job") .addPathPart(stopRollupJobRequest.getJobId()) .addPathPartAsIs("_stop") .build(); @@ -67,16 +63,14 @@ static Request stopJob(final StopRollupJobRequest stopRollupJobRequest) throws I } static Request getJob(final GetRollupJobRequest getRollupJobRequest) { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_rollup", "job") + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_rollup", "job") .addPathPart(getRollupJobRequest.getJobId()) .build(); return new Request(HttpGet.METHOD_NAME, endpoint); } static Request deleteJob(final DeleteRollupJobRequest deleteRollupJobRequest) throws IOException { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_rollup", "job") + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_rollup", "job") .addPathPart(deleteRollupJobRequest.getId()) .build(); return new Request(HttpDelete.METHOD_NAME, endpoint); @@ -87,16 +81,14 @@ static Request search(final SearchRequest request) throws IOException { } static Request getRollupCaps(final GetRollupCapsRequest getRollupCapsRequest) throws IOException { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_rollup", "data") + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_rollup", "data") .addPathPart(getRollupCapsRequest.getIndexPattern()) .build(); return new Request(HttpGet.METHOD_NAME, endpoint); } static Request getRollupIndexCaps(final GetRollupIndexCapsRequest getRollupIndexCapsRequest) throws IOException { - String endpoint = new RequestConverters.EndpointBuilder() - .addCommaSeparatedPathParts(getRollupIndexCapsRequest.indices()) + String endpoint = new RequestConverters.EndpointBuilder().addCommaSeparatedPathParts(getRollupIndexCapsRequest.indices()) .addPathPartAsIs("_rollup", "data") .build(); return new Request(HttpGet.METHOD_NAME, endpoint); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/SearchableSnapshotsClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/SearchableSnapshotsClient.java index 4bb3f2367fdba..b43fdf6a66b61 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/SearchableSnapshotsClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/SearchableSnapshotsClient.java @@ -70,8 +70,8 @@ public RestoreSnapshotResponse mountSnapshot(final MountSnapshotRequest request, public Cancellable mountSnapshotAsync( final MountSnapshotRequest request, final RequestOptions options, - final ActionListener listener) - { + final ActionListener listener + ) { return restHighLevelClient.performRequestAsyncAndParseEntity( request, SearchableSnapshotsRequestConverters::mountSnapshot, @@ -114,8 +114,8 @@ public CachesStatsResponse cacheStats(final CachesStatsRequest request, final Re public Cancellable cacheStatsAsync( final CachesStatsRequest request, final RequestOptions options, - final ActionListener listener) - { + final ActionListener listener + ) { return restHighLevelClient.performRequestAsyncAndParseEntity( request, SearchableSnapshotsRequestConverters::cacheStats, diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/SearchableSnapshotsRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/SearchableSnapshotsRequestConverters.java index c2785e9253f17..852f7bcbd6a30 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/SearchableSnapshotsRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/SearchableSnapshotsRequestConverters.java @@ -21,8 +21,7 @@ final class SearchableSnapshotsRequestConverters { static Request mountSnapshot(final MountSnapshotRequest mountSnapshotRequest) throws IOException { - final String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_snapshot") + final String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_snapshot") .addPathPart(mountSnapshotRequest.getRepository()) .addPathPart(mountSnapshotRequest.getSnapshot()) .addPathPartAsIs("_mount") @@ -44,8 +43,7 @@ static Request mountSnapshot(final MountSnapshotRequest mountSnapshotRequest) th } static Request cacheStats(final CachesStatsRequest cacheStatsRequest) { - final RequestConverters.EndpointBuilder endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_searchable_snapshots"); + final RequestConverters.EndpointBuilder endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_searchable_snapshots"); if (cacheStatsRequest.getNodesIds() != null) { endpoint.addCommaSeparatedPathParts(cacheStatsRequest.getNodesIds()); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/SecurityClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/SecurityClient.java index 86f2a04d7fc4f..6f65f5d4145cf 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/SecurityClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/SecurityClient.java @@ -68,6 +68,8 @@ import org.elasticsearch.client.security.InvalidateApiKeyResponse; import org.elasticsearch.client.security.InvalidateTokenRequest; import org.elasticsearch.client.security.InvalidateTokenResponse; +import org.elasticsearch.client.security.KibanaEnrollmentRequest; +import org.elasticsearch.client.security.KibanaEnrollmentResponse; import org.elasticsearch.client.security.NodeEnrollmentRequest; import org.elasticsearch.client.security.NodeEnrollmentResponse; import org.elasticsearch.client.security.PutPrivilegesRequest; @@ -78,8 +80,6 @@ import org.elasticsearch.client.security.PutRoleResponse; import org.elasticsearch.client.security.PutUserRequest; import org.elasticsearch.client.security.PutUserResponse; -import org.elasticsearch.client.security.KibanaEnrollmentRequest; -import org.elasticsearch.client.security.KibanaEnrollmentResponse; import org.elasticsearch.client.security.QueryApiKeyRequest; import org.elasticsearch.client.security.QueryApiKeyResponse; @@ -117,8 +117,13 @@ public final class SecurityClient { * @throws IOException in case there is a problem sending the request or parsing back the response */ public GetUsersResponse getUsers(GetUsersRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, SecurityRequestConverters::getUsers, options, - GetUsersResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + SecurityRequestConverters::getUsers, + options, + GetUsersResponse::fromXContent, + emptySet() + ); } /** @@ -131,8 +136,14 @@ public GetUsersResponse getUsers(GetUsersRequest request, RequestOptions options * @return cancellable that may be used to cancel the request */ public Cancellable getUsersAsync(GetUsersRequest request, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, SecurityRequestConverters::getUsers, options, - GetUsersResponse::fromXContent, listener, emptySet()); + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + SecurityRequestConverters::getUsers, + options, + GetUsersResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -146,8 +157,13 @@ public Cancellable getUsersAsync(GetUsersRequest request, RequestOptions options * @throws IOException in case there is a problem sending the request or parsing back the response */ public PutUserResponse putUser(PutUserRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, SecurityRequestConverters::putUser, options, - PutUserResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + SecurityRequestConverters::putUser, + options, + PutUserResponse::fromXContent, + emptySet() + ); } /** @@ -161,8 +177,14 @@ public PutUserResponse putUser(PutUserRequest request, RequestOptions options) t * @return cancellable that may be used to cancel the request */ public Cancellable putUserAsync(PutUserRequest request, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, SecurityRequestConverters::putUser, options, - PutUserResponse::fromXContent, listener, emptySet()); + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + SecurityRequestConverters::putUser, + options, + PutUserResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -175,8 +197,13 @@ public Cancellable putUserAsync(PutUserRequest request, RequestOptions options, * @throws IOException in case there is a problem sending the request or parsing back the response */ public DeleteUserResponse deleteUser(DeleteUserRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, SecurityRequestConverters::deleteUser, options, - DeleteUserResponse::fromXContent, singleton(404)); + return restHighLevelClient.performRequestAndParseEntity( + request, + SecurityRequestConverters::deleteUser, + options, + DeleteUserResponse::fromXContent, + singleton(404) + ); } /** @@ -189,8 +216,14 @@ public DeleteUserResponse deleteUser(DeleteUserRequest request, RequestOptions o * @return cancellable that may be used to cancel the request */ public Cancellable deleteUserAsync(DeleteUserRequest request, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, SecurityRequestConverters::deleteUser, options, - DeleteUserResponse::fromXContent, listener, singleton(404)); + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + SecurityRequestConverters::deleteUser, + options, + DeleteUserResponse::fromXContent, + listener, + singleton(404) + ); } /** @@ -203,8 +236,13 @@ public Cancellable deleteUserAsync(DeleteUserRequest request, RequestOptions opt * @throws IOException in case there is a problem sending the request or parsing back the response */ public PutRoleMappingResponse putRoleMapping(final PutRoleMappingRequest request, final RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, SecurityRequestConverters::putRoleMapping, options, - PutRoleMappingResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + SecurityRequestConverters::putRoleMapping, + options, + PutRoleMappingResponse::fromXContent, + emptySet() + ); } /** @@ -216,10 +254,19 @@ public PutRoleMappingResponse putRoleMapping(final PutRoleMappingRequest request * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable putRoleMappingAsync(final PutRoleMappingRequest request, final RequestOptions options, - final ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, SecurityRequestConverters::putRoleMapping, options, - PutRoleMappingResponse::fromXContent, listener, emptySet()); + public Cancellable putRoleMappingAsync( + final PutRoleMappingRequest request, + final RequestOptions options, + final ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + SecurityRequestConverters::putRoleMapping, + options, + PutRoleMappingResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -235,10 +282,14 @@ public Cancellable putRoleMappingAsync(final PutRoleMappingRequest request, fina * @throws IOException in case there is a problem sending the request or * parsing back the response */ - public GetRoleMappingsResponse getRoleMappings(final GetRoleMappingsRequest request, - final RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, SecurityRequestConverters::getRoleMappings, - options, GetRoleMappingsResponse::fromXContent, emptySet()); + public GetRoleMappingsResponse getRoleMappings(final GetRoleMappingsRequest request, final RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, + SecurityRequestConverters::getRoleMappings, + options, + GetRoleMappingsResponse::fromXContent, + emptySet() + ); } /** @@ -252,10 +303,19 @@ public GetRoleMappingsResponse getRoleMappings(final GetRoleMappingsRequest requ * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getRoleMappingsAsync(final GetRoleMappingsRequest request, final RequestOptions options, - final ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, SecurityRequestConverters::getRoleMappings, - options, GetRoleMappingsResponse::fromXContent, listener, emptySet()); + public Cancellable getRoleMappingsAsync( + final GetRoleMappingsRequest request, + final RequestOptions options, + final ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + SecurityRequestConverters::getRoleMappings, + options, + GetRoleMappingsResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -269,8 +329,13 @@ public Cancellable getRoleMappingsAsync(final GetRoleMappingsRequest request, fi * @throws IOException in case there is a problem sending the request or parsing back the response */ public boolean enableUser(EnableUserRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequest(request, SecurityRequestConverters::enableUser, options, - RestHighLevelClient::convertExistsResponse, emptySet()); + return restHighLevelClient.performRequest( + request, + SecurityRequestConverters::enableUser, + options, + RestHighLevelClient::convertExistsResponse, + emptySet() + ); } /** @@ -299,10 +364,15 @@ public boolean enableUser(RequestOptions options, EnableUserRequest request) thr * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable enableUserAsync(EnableUserRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsync(request, SecurityRequestConverters::enableUser, options, - RestHighLevelClient::convertExistsResponse, listener, emptySet()); + public Cancellable enableUserAsync(EnableUserRequest request, RequestOptions options, ActionListener listener) { + return restHighLevelClient.performRequestAsync( + request, + SecurityRequestConverters::enableUser, + options, + RestHighLevelClient::convertExistsResponse, + listener, + emptySet() + ); } /** @@ -317,8 +387,7 @@ public Cancellable enableUserAsync(EnableUserRequest request, RequestOptions opt * @return cancellable that may be used to cancel the request */ @Deprecated - public Cancellable enableUserAsync(RequestOptions options, EnableUserRequest request, - ActionListener listener) { + public Cancellable enableUserAsync(RequestOptions options, EnableUserRequest request, ActionListener listener) { return enableUserAsync(request, options, listener); } @@ -333,8 +402,13 @@ public Cancellable enableUserAsync(RequestOptions options, EnableUserRequest req * @throws IOException in case there is a problem sending the request or parsing back the response */ public boolean disableUser(DisableUserRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequest(request, SecurityRequestConverters::disableUser, options, - RestHighLevelClient::convertExistsResponse, emptySet()); + return restHighLevelClient.performRequest( + request, + SecurityRequestConverters::disableUser, + options, + RestHighLevelClient::convertExistsResponse, + emptySet() + ); } /** @@ -363,10 +437,15 @@ public boolean disableUser(RequestOptions options, DisableUserRequest request) t * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable disableUserAsync(DisableUserRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsync(request, SecurityRequestConverters::disableUser, options, - RestHighLevelClient::convertExistsResponse, listener, emptySet()); + public Cancellable disableUserAsync(DisableUserRequest request, RequestOptions options, ActionListener listener) { + return restHighLevelClient.performRequestAsync( + request, + SecurityRequestConverters::disableUser, + options, + RestHighLevelClient::convertExistsResponse, + listener, + emptySet() + ); } /** @@ -381,8 +460,7 @@ public Cancellable disableUserAsync(DisableUserRequest request, RequestOptions o * @return cancellable that may be used to cancel the request */ @Deprecated - public Cancellable disableUserAsync(RequestOptions options, DisableUserRequest request, - ActionListener listener) { + public Cancellable disableUserAsync(RequestOptions options, DisableUserRequest request, ActionListener listener) { return disableUserAsync(request, options, listener); } @@ -395,8 +473,13 @@ public Cancellable disableUserAsync(RequestOptions options, DisableUserRequest r * @return the responsee from the authenticate user call */ public AuthenticateResponse authenticate(RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(AuthenticateRequest.INSTANCE, AuthenticateRequest::getRequest, options, - AuthenticateResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + AuthenticateRequest.INSTANCE, + AuthenticateRequest::getRequest, + options, + AuthenticateResponse::fromXContent, + emptySet() + ); } /** @@ -409,8 +492,14 @@ public AuthenticateResponse authenticate(RequestOptions options) throws IOExcept * @return cancellable that may be used to cancel the request */ public Cancellable authenticateAsync(RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(AuthenticateRequest.INSTANCE, AuthenticateRequest::getRequest, options, - AuthenticateResponse::fromXContent, listener, emptySet()); + return restHighLevelClient.performRequestAsyncAndParseEntity( + AuthenticateRequest.INSTANCE, + AuthenticateRequest::getRequest, + options, + AuthenticateResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -423,8 +512,13 @@ public Cancellable authenticateAsync(RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, SecurityRequestConverters::hasPrivileges, options, - HasPrivilegesResponse::fromXContent, listener, emptySet()); + public Cancellable hasPrivilegesAsync( + HasPrivilegesRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + SecurityRequestConverters::hasPrivileges, + options, + HasPrivilegesResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -447,8 +550,13 @@ public Cancellable hasPrivilegesAsync(HasPrivilegesRequest request, RequestOptio * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized */ public GetUserPrivilegesResponse getUserPrivileges(RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(GetUserPrivilegesRequest.INSTANCE, GetUserPrivilegesRequest::getRequest, - options, GetUserPrivilegesResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + GetUserPrivilegesRequest.INSTANCE, + GetUserPrivilegesRequest::getRequest, + options, + GetUserPrivilegesResponse::fromXContent, + emptySet() + ); } /** @@ -459,8 +567,13 @@ public GetUserPrivilegesResponse getUserPrivileges(RequestOptions options) throw */ public Cancellable getUserPrivilegesAsync(RequestOptions options, ActionListener listener) { return restHighLevelClient.performRequestAsyncAndParseEntity( - GetUserPrivilegesRequest.INSTANCE, GetUserPrivilegesRequest::getRequest, - options, GetUserPrivilegesResponse::fromXContent, listener, emptySet()); + GetUserPrivilegesRequest.INSTANCE, + GetUserPrivilegesRequest::getRequest, + options, + GetUserPrivilegesResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -474,8 +587,13 @@ public Cancellable getUserPrivilegesAsync(RequestOptions options, ActionListener * @throws IOException in case there is a problem sending the request or parsing back the response */ public ClearRealmCacheResponse clearRealmCache(ClearRealmCacheRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, SecurityRequestConverters::clearRealmCache, options, - ClearRealmCacheResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + SecurityRequestConverters::clearRealmCache, + options, + ClearRealmCacheResponse::fromXContent, + emptySet() + ); } /** @@ -488,10 +606,19 @@ public ClearRealmCacheResponse clearRealmCache(ClearRealmCacheRequest request, R * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable clearRealmCacheAsync(ClearRealmCacheRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, SecurityRequestConverters::clearRealmCache, options, - ClearRealmCacheResponse::fromXContent, listener, emptySet()); + public Cancellable clearRealmCacheAsync( + ClearRealmCacheRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + SecurityRequestConverters::clearRealmCache, + options, + ClearRealmCacheResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -505,8 +632,13 @@ public Cancellable clearRealmCacheAsync(ClearRealmCacheRequest request, RequestO * @throws IOException in case there is a problem sending the request or parsing back the response */ public ClearRolesCacheResponse clearRolesCache(ClearRolesCacheRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, SecurityRequestConverters::clearRolesCache, options, - ClearRolesCacheResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + SecurityRequestConverters::clearRolesCache, + options, + ClearRolesCacheResponse::fromXContent, + emptySet() + ); } /** @@ -519,10 +651,19 @@ public ClearRolesCacheResponse clearRolesCache(ClearRolesCacheRequest request, R * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable clearRolesCacheAsync(ClearRolesCacheRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, SecurityRequestConverters::clearRolesCache, options, - ClearRolesCacheResponse::fromXContent, listener, emptySet()); + public Cancellable clearRolesCacheAsync( + ClearRolesCacheRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + SecurityRequestConverters::clearRolesCache, + options, + ClearRolesCacheResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -535,10 +676,15 @@ public Cancellable clearRolesCacheAsync(ClearRolesCacheRequest request, RequestO * @return the response from the clear privileges cache call * @throws IOException in case there is a problem sending the request or parsing back the response */ - public ClearPrivilegesCacheResponse clearPrivilegesCache(ClearPrivilegesCacheRequest request, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, SecurityRequestConverters::clearPrivilegesCache, options, - ClearPrivilegesCacheResponse::fromXContent, emptySet()); + public ClearPrivilegesCacheResponse clearPrivilegesCache(ClearPrivilegesCacheRequest request, RequestOptions options) + throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, + SecurityRequestConverters::clearPrivilegesCache, + options, + ClearPrivilegesCacheResponse::fromXContent, + emptySet() + ); } /** @@ -551,10 +697,19 @@ public ClearPrivilegesCacheResponse clearPrivilegesCache(ClearPrivilegesCacheReq * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable clearPrivilegesCacheAsync(ClearPrivilegesCacheRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, SecurityRequestConverters::clearPrivilegesCache, options, - ClearPrivilegesCacheResponse::fromXContent, listener, emptySet()); + public Cancellable clearPrivilegesCacheAsync( + ClearPrivilegesCacheRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + SecurityRequestConverters::clearPrivilegesCache, + options, + ClearPrivilegesCacheResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -566,10 +721,15 @@ public Cancellable clearPrivilegesCacheAsync(ClearPrivilegesCacheRequest request * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized * @return the response from the clear security cache call * @throws IOException in case there is a problem sending the request or parsing back the response - */public ClearSecurityCacheResponse clearApiKeyCache(ClearApiKeyCacheRequest request, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, SecurityRequestConverters::clearApiKeyCache, options, - ClearSecurityCacheResponse::fromXContent, emptySet()); + */ + public ClearSecurityCacheResponse clearApiKeyCache(ClearApiKeyCacheRequest request, RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, + SecurityRequestConverters::clearApiKeyCache, + options, + ClearSecurityCacheResponse::fromXContent, + emptySet() + ); } /** @@ -582,10 +742,19 @@ public Cancellable clearPrivilegesCacheAsync(ClearPrivilegesCacheRequest request * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable clearApiKeyCacheAsync(ClearApiKeyCacheRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, SecurityRequestConverters::clearApiKeyCache, options, - ClearSecurityCacheResponse::fromXContent, listener, emptySet()); + public Cancellable clearApiKeyCacheAsync( + ClearApiKeyCacheRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + SecurityRequestConverters::clearApiKeyCache, + options, + ClearSecurityCacheResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -598,10 +767,16 @@ public Cancellable clearApiKeyCacheAsync(ClearApiKeyCacheRequest request, Reques * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized * @return the response from the clear security cache call * @throws IOException in case there is a problem sending the request or parsing back the response - */public ClearSecurityCacheResponse clearServiceAccountTokenCache(ClearServiceAccountTokenCacheRequest request, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, SecurityRequestConverters::clearServiceAccountTokenCache, - options, ClearSecurityCacheResponse::fromXContent, emptySet()); + */ + public ClearSecurityCacheResponse clearServiceAccountTokenCache(ClearServiceAccountTokenCacheRequest request, RequestOptions options) + throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, + SecurityRequestConverters::clearServiceAccountTokenCache, + options, + ClearSecurityCacheResponse::fromXContent, + emptySet() + ); } /** @@ -614,10 +789,20 @@ public Cancellable clearApiKeyCacheAsync(ClearApiKeyCacheRequest request, Reques * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request - */public Cancellable clearServiceAccountTokenCacheAsync(ClearServiceAccountTokenCacheRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, SecurityRequestConverters::clearServiceAccountTokenCache, - options, ClearSecurityCacheResponse::fromXContent, listener, emptySet()); + */ + public Cancellable clearServiceAccountTokenCacheAsync( + ClearServiceAccountTokenCacheRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + SecurityRequestConverters::clearServiceAccountTokenCache, + options, + ClearSecurityCacheResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -630,8 +815,13 @@ public Cancellable clearApiKeyCacheAsync(ClearApiKeyCacheRequest request, Reques * @throws IOException in case there is a problem sending the request or parsing back the response */ public GetSslCertificatesResponse getSslCertificates(RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(GetSslCertificatesRequest.INSTANCE, GetSslCertificatesRequest::getRequest, - options, GetSslCertificatesResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + GetSslCertificatesRequest.INSTANCE, + GetSslCertificatesRequest::getRequest, + options, + GetSslCertificatesResponse::fromXContent, + emptySet() + ); } /** @@ -645,8 +835,13 @@ public GetSslCertificatesResponse getSslCertificates(RequestOptions options) thr */ public Cancellable getSslCertificatesAsync(RequestOptions options, ActionListener listener) { return restHighLevelClient.performRequestAsyncAndParseEntity( - GetSslCertificatesRequest.INSTANCE, GetSslCertificatesRequest::getRequest, - options, GetSslCertificatesResponse::fromXContent, listener, emptySet()); + GetSslCertificatesRequest.INSTANCE, + GetSslCertificatesRequest::getRequest, + options, + GetSslCertificatesResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -660,8 +855,13 @@ public Cancellable getSslCertificatesAsync(RequestOptions options, ActionListene * @throws IOException in case there is a problem sending the request or parsing back the response */ public boolean changePassword(ChangePasswordRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequest(request, SecurityRequestConverters::changePassword, options, - RestHighLevelClient::convertExistsResponse, emptySet()); + return restHighLevelClient.performRequest( + request, + SecurityRequestConverters::changePassword, + options, + RestHighLevelClient::convertExistsResponse, + emptySet() + ); } /** @@ -690,10 +890,15 @@ public boolean changePassword(RequestOptions options, ChangePasswordRequest requ * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable changePasswordAsync(ChangePasswordRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsync(request, SecurityRequestConverters::changePassword, options, - RestHighLevelClient::convertExistsResponse, listener, emptySet()); + public Cancellable changePasswordAsync(ChangePasswordRequest request, RequestOptions options, ActionListener listener) { + return restHighLevelClient.performRequestAsync( + request, + SecurityRequestConverters::changePassword, + options, + RestHighLevelClient::convertExistsResponse, + listener, + emptySet() + ); } /** @@ -708,8 +913,7 @@ public Cancellable changePasswordAsync(ChangePasswordRequest request, RequestOpt * @return cancellable that may be used to cancel the request */ @Deprecated - public Cancellable changePasswordAsync(RequestOptions options, ChangePasswordRequest request, - ActionListener listener) { + public Cancellable changePasswordAsync(RequestOptions options, ChangePasswordRequest request, ActionListener listener) { return changePasswordAsync(request, options, listener); } @@ -723,8 +927,13 @@ public Cancellable changePasswordAsync(RequestOptions options, ChangePasswordReq * @throws IOException in case there is a problem sending the request or parsing back the response */ public DeleteRoleMappingResponse deleteRoleMapping(DeleteRoleMappingRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, SecurityRequestConverters::deleteRoleMapping, options, - DeleteRoleMappingResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + SecurityRequestConverters::deleteRoleMapping, + options, + DeleteRoleMappingResponse::fromXContent, + emptySet() + ); } /** @@ -738,8 +947,14 @@ public DeleteRoleMappingResponse deleteRoleMapping(DeleteRoleMappingRequest requ * @return cancellable that may be used to cancel the request */ public Cancellable getRolesAsync(GetRolesRequest request, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, SecurityRequestConverters::getRoles, options, - GetRolesResponse::fromXContent, listener, emptySet()); + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + SecurityRequestConverters::getRoles, + options, + GetRolesResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -753,8 +968,13 @@ public Cancellable getRolesAsync(GetRolesRequest request, RequestOptions options * @throws IOException in case there is a problem sending the request or parsing back the response */ public GetRolesResponse getRoles(final GetRolesRequest request, final RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, SecurityRequestConverters::getRoles, options, - GetRolesResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + SecurityRequestConverters::getRoles, + options, + GetRolesResponse::fromXContent, + emptySet() + ); } /** @@ -768,8 +988,14 @@ public GetRolesResponse getRoles(final GetRolesRequest request, final RequestOpt * @return cancellable that may be used to cancel the request */ public Cancellable putRoleAsync(PutRoleRequest request, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, SecurityRequestConverters::putRole, options, - PutRoleResponse::fromXContent, listener, emptySet()); + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + SecurityRequestConverters::putRole, + options, + PutRoleResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -783,8 +1009,13 @@ public Cancellable putRoleAsync(PutRoleRequest request, RequestOptions options, * @throws IOException in case there is a problem sending the request or parsing back the response */ public PutRoleResponse putRole(final PutRoleRequest request, final RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, SecurityRequestConverters::putRole, options, - PutRoleResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + SecurityRequestConverters::putRole, + options, + PutRoleResponse::fromXContent, + emptySet() + ); } /** @@ -796,11 +1027,19 @@ public PutRoleResponse putRole(final PutRoleRequest request, final RequestOption * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable deleteRoleMappingAsync(DeleteRoleMappingRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, - SecurityRequestConverters::deleteRoleMapping, options, - DeleteRoleMappingResponse::fromXContent, listener, emptySet()); + public Cancellable deleteRoleMappingAsync( + DeleteRoleMappingRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + SecurityRequestConverters::deleteRoleMapping, + options, + DeleteRoleMappingResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -813,8 +1052,13 @@ public Cancellable deleteRoleMappingAsync(DeleteRoleMappingRequest request, Requ * @throws IOException in case there is a problem sending the request or parsing back the response */ public DeleteRoleResponse deleteRole(DeleteRoleRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, SecurityRequestConverters::deleteRole, options, - DeleteRoleResponse::fromXContent, singleton(404)); + return restHighLevelClient.performRequestAndParseEntity( + request, + SecurityRequestConverters::deleteRole, + options, + DeleteRoleResponse::fromXContent, + singleton(404) + ); } /** @@ -826,10 +1070,15 @@ public DeleteRoleResponse deleteRole(DeleteRoleRequest request, RequestOptions o * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable deleteRoleAsync(DeleteRoleRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, SecurityRequestConverters::deleteRole, options, - DeleteRoleResponse::fromXContent, listener, singleton(404)); + public Cancellable deleteRoleAsync(DeleteRoleRequest request, RequestOptions options, ActionListener listener) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + SecurityRequestConverters::deleteRole, + options, + DeleteRoleResponse::fromXContent, + listener, + singleton(404) + ); } /** @@ -843,8 +1092,13 @@ public Cancellable deleteRoleAsync(DeleteRoleRequest request, RequestOptions opt * @throws IOException in case there is a problem sending the request or parsing back the response */ public CreateTokenResponse createToken(CreateTokenRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, SecurityRequestConverters::createToken, options, - CreateTokenResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + SecurityRequestConverters::createToken, + options, + CreateTokenResponse::fromXContent, + emptySet() + ); } /** @@ -857,10 +1111,15 @@ public CreateTokenResponse createToken(CreateTokenRequest request, RequestOption * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable createTokenAsync(CreateTokenRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, SecurityRequestConverters::createToken, options, - CreateTokenResponse::fromXContent, listener, emptySet()); + public Cancellable createTokenAsync(CreateTokenRequest request, RequestOptions options, ActionListener listener) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + SecurityRequestConverters::createToken, + options, + CreateTokenResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -874,8 +1133,13 @@ public Cancellable createTokenAsync(CreateTokenRequest request, RequestOptions o * @throws IOException in case there is a problem sending the request or parsing back the response */ public InvalidateTokenResponse invalidateToken(InvalidateTokenRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, SecurityRequestConverters::invalidateToken, options, - InvalidateTokenResponse::fromXContent, singleton(404)); + return restHighLevelClient.performRequestAndParseEntity( + request, + SecurityRequestConverters::invalidateToken, + options, + InvalidateTokenResponse::fromXContent, + singleton(404) + ); } /** @@ -887,10 +1151,19 @@ public InvalidateTokenResponse invalidateToken(InvalidateTokenRequest request, R * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable invalidateTokenAsync(InvalidateTokenRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, SecurityRequestConverters::invalidateToken, options, - InvalidateTokenResponse::fromXContent, listener, singleton(404)); + public Cancellable invalidateTokenAsync( + InvalidateTokenRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + SecurityRequestConverters::invalidateToken, + options, + InvalidateTokenResponse::fromXContent, + listener, + singleton(404) + ); } /** @@ -904,8 +1177,13 @@ public Cancellable invalidateTokenAsync(InvalidateTokenRequest request, RequestO * @throws IOException in case there is a problem sending the request or parsing back the response */ public GetBuiltinPrivilegesResponse getBuiltinPrivileges(final RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(GetBuiltinPrivilegesRequest.INSTANCE, - GetBuiltinPrivilegesRequest::getRequest, options, GetBuiltinPrivilegesResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + GetBuiltinPrivilegesRequest.INSTANCE, + GetBuiltinPrivilegesRequest::getRequest, + options, + GetBuiltinPrivilegesResponse::fromXContent, + emptySet() + ); } /** @@ -917,11 +1195,18 @@ public GetBuiltinPrivilegesResponse getBuiltinPrivileges(final RequestOptions op * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getBuiltinPrivilegesAsync(final RequestOptions options, - final ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(GetBuiltinPrivilegesRequest.INSTANCE, - GetBuiltinPrivilegesRequest::getRequest, options, GetBuiltinPrivilegesResponse::fromXContent, - listener, emptySet()); + public Cancellable getBuiltinPrivilegesAsync( + final RequestOptions options, + final ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + GetBuiltinPrivilegesRequest.INSTANCE, + GetBuiltinPrivilegesRequest::getRequest, + options, + GetBuiltinPrivilegesResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -939,8 +1224,13 @@ public Cancellable getBuiltinPrivilegesAsync(final RequestOptions options, * parsing back the response */ public GetPrivilegesResponse getPrivileges(final GetPrivilegesRequest request, final RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, SecurityRequestConverters::getPrivileges, - options, GetPrivilegesResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + SecurityRequestConverters::getPrivileges, + options, + GetPrivilegesResponse::fromXContent, + emptySet() + ); } /** @@ -954,10 +1244,19 @@ public GetPrivilegesResponse getPrivileges(final GetPrivilegesRequest request, f * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getPrivilegesAsync(final GetPrivilegesRequest request, final RequestOptions options, - final ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, SecurityRequestConverters::getPrivileges, - options, GetPrivilegesResponse::fromXContent, listener, emptySet()); + public Cancellable getPrivilegesAsync( + final GetPrivilegesRequest request, + final RequestOptions options, + final ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + SecurityRequestConverters::getPrivileges, + options, + GetPrivilegesResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -971,8 +1270,13 @@ public Cancellable getPrivilegesAsync(final GetPrivilegesRequest request, final * @throws IOException in case there is a problem sending the request or parsing back the response */ public PutPrivilegesResponse putPrivileges(final PutPrivilegesRequest request, final RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, SecurityRequestConverters::putPrivileges, options, - PutPrivilegesResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + SecurityRequestConverters::putPrivileges, + options, + PutPrivilegesResponse::fromXContent, + emptySet() + ); } /** @@ -987,10 +1291,19 @@ public PutPrivilegesResponse putPrivileges(final PutPrivilegesRequest request, f * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable putPrivilegesAsync(final PutPrivilegesRequest request, final RequestOptions options, - final ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, SecurityRequestConverters::putPrivileges, options, - PutPrivilegesResponse::fromXContent, listener, emptySet()); + public Cancellable putPrivilegesAsync( + final PutPrivilegesRequest request, + final RequestOptions options, + final ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + SecurityRequestConverters::putPrivileges, + options, + PutPrivilegesResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -1004,8 +1317,13 @@ public Cancellable putPrivilegesAsync(final PutPrivilegesRequest request, final * @throws IOException in case there is a problem sending the request or parsing back the response */ public DeletePrivilegesResponse deletePrivileges(DeletePrivilegesRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, SecurityRequestConverters::deletePrivileges, options, - DeletePrivilegesResponse::fromXContent, singleton(404)); + return restHighLevelClient.performRequestAndParseEntity( + request, + SecurityRequestConverters::deletePrivileges, + options, + DeletePrivilegesResponse::fromXContent, + singleton(404) + ); } /** @@ -1018,10 +1336,19 @@ public DeletePrivilegesResponse deletePrivileges(DeletePrivilegesRequest request * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable deletePrivilegesAsync(DeletePrivilegesRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, SecurityRequestConverters::deletePrivileges, options, - DeletePrivilegesResponse::fromXContent, listener, singleton(404)); + public Cancellable deletePrivilegesAsync( + DeletePrivilegesRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + SecurityRequestConverters::deletePrivileges, + options, + DeletePrivilegesResponse::fromXContent, + listener, + singleton(404) + ); } /** @@ -1035,8 +1362,13 @@ public Cancellable deletePrivilegesAsync(DeletePrivilegesRequest request, Reques * @throws IOException in case there is a problem sending the request or parsing back the response */ public CreateApiKeyResponse createApiKey(final CreateApiKeyRequest request, final RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, SecurityRequestConverters::createApiKey, options, - CreateApiKeyResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + SecurityRequestConverters::createApiKey, + options, + CreateApiKeyResponse::fromXContent, + emptySet() + ); } /** @@ -1049,10 +1381,19 @@ public CreateApiKeyResponse createApiKey(final CreateApiKeyRequest request, fina * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable createApiKeyAsync(final CreateApiKeyRequest request, final RequestOptions options, - final ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, SecurityRequestConverters::createApiKey, options, - CreateApiKeyResponse::fromXContent, listener, emptySet()); + public Cancellable createApiKeyAsync( + final CreateApiKeyRequest request, + final RequestOptions options, + final ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + SecurityRequestConverters::createApiKey, + options, + CreateApiKeyResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -1066,8 +1407,13 @@ public Cancellable createApiKeyAsync(final CreateApiKeyRequest request, final Re * @throws IOException in case there is a problem sending the request or parsing back the response */ public GetApiKeyResponse getApiKey(final GetApiKeyRequest request, final RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, SecurityRequestConverters::getApiKey, options, - GetApiKeyResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + SecurityRequestConverters::getApiKey, + options, + GetApiKeyResponse::fromXContent, + emptySet() + ); } /** @@ -1080,10 +1426,19 @@ public GetApiKeyResponse getApiKey(final GetApiKeyRequest request, final Request * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getApiKeyAsync(final GetApiKeyRequest request, final RequestOptions options, - final ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, SecurityRequestConverters::getApiKey, options, - GetApiKeyResponse::fromXContent, listener, emptySet()); + public Cancellable getApiKeyAsync( + final GetApiKeyRequest request, + final RequestOptions options, + final ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + SecurityRequestConverters::getApiKey, + options, + GetApiKeyResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -1097,9 +1452,14 @@ public Cancellable getApiKeyAsync(final GetApiKeyRequest request, final RequestO * @throws IOException in case there is a problem sending the request or parsing back the response */ public InvalidateApiKeyResponse invalidateApiKey(final InvalidateApiKeyRequest request, final RequestOptions options) - throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, SecurityRequestConverters::invalidateApiKey, options, - InvalidateApiKeyResponse::fromXContent, emptySet()); + throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, + SecurityRequestConverters::invalidateApiKey, + options, + InvalidateApiKeyResponse::fromXContent, + emptySet() + ); } /** @@ -1112,10 +1472,19 @@ public InvalidateApiKeyResponse invalidateApiKey(final InvalidateApiKeyRequest r * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable invalidateApiKeyAsync(final InvalidateApiKeyRequest request, final RequestOptions options, - final ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, SecurityRequestConverters::invalidateApiKey, options, - InvalidateApiKeyResponse::fromXContent, listener, emptySet()); + public Cancellable invalidateApiKeyAsync( + final InvalidateApiKeyRequest request, + final RequestOptions options, + final ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + SecurityRequestConverters::invalidateApiKey, + options, + InvalidateApiKeyResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -1129,8 +1498,13 @@ public Cancellable invalidateApiKeyAsync(final InvalidateApiKeyRequest request, * @throws IOException in case there is a problem sending the request or parsing back the response */ public CreateApiKeyResponse grantApiKey(final GrantApiKeyRequest request, final RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, SecurityRequestConverters::grantApiKey, options, - CreateApiKeyResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + SecurityRequestConverters::grantApiKey, + options, + CreateApiKeyResponse::fromXContent, + emptySet() + ); } /** @@ -1143,10 +1517,19 @@ public CreateApiKeyResponse grantApiKey(final GrantApiKeyRequest request, final * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable grantApiKeyAsync(final GrantApiKeyRequest request, final RequestOptions options, - final ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, SecurityRequestConverters::grantApiKey, options, - CreateApiKeyResponse::fromXContent, listener, emptySet()); + public Cancellable grantApiKeyAsync( + final GrantApiKeyRequest request, + final RequestOptions options, + final ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + SecurityRequestConverters::grantApiKey, + options, + CreateApiKeyResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -1160,8 +1543,13 @@ public Cancellable grantApiKeyAsync(final GrantApiKeyRequest request, final Requ * @throws IOException in case there is a problem sending the request or parsing back the response */ public QueryApiKeyResponse queryApiKey(final QueryApiKeyRequest request, final RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, SecurityRequestConverters::queryApiKey, options, - QueryApiKeyResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + SecurityRequestConverters::queryApiKey, + options, + QueryApiKeyResponse::fromXContent, + emptySet() + ); } /** @@ -1174,10 +1562,19 @@ public QueryApiKeyResponse queryApiKey(final QueryApiKeyRequest request, final R * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable queryApiKeyAsync(final QueryApiKeyRequest request, final RequestOptions options, - final ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, SecurityRequestConverters::queryApiKey, options, - QueryApiKeyResponse::fromXContent, listener, emptySet()); + public Cancellable queryApiKeyAsync( + final QueryApiKeyRequest request, + final RequestOptions options, + final ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + SecurityRequestConverters::queryApiKey, + options, + QueryApiKeyResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -1190,8 +1587,13 @@ public Cancellable queryApiKeyAsync(final QueryApiKeyRequest request, final Requ * @throws IOException in case there is a problem sending the request or parsing back the response */ public GetServiceAccountsResponse getServiceAccounts(GetServiceAccountsRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, SecurityRequestConverters::getServiceAccounts, options, - GetServiceAccountsResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + SecurityRequestConverters::getServiceAccounts, + options, + GetServiceAccountsResponse::fromXContent, + emptySet() + ); } /** @@ -1203,10 +1605,19 @@ public GetServiceAccountsResponse getServiceAccounts(GetServiceAccountsRequest r * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getServiceAccountsAsync(GetServiceAccountsRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, SecurityRequestConverters::getServiceAccounts, options, - GetServiceAccountsResponse::fromXContent, listener, emptySet()); + public Cancellable getServiceAccountsAsync( + GetServiceAccountsRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + SecurityRequestConverters::getServiceAccounts, + options, + GetServiceAccountsResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -1219,10 +1630,17 @@ public Cancellable getServiceAccountsAsync(GetServiceAccountsRequest request, Re * @return the response from the create service account token call * @throws IOException in case there is a problem sending the request or parsing back the response */ - public CreateServiceAccountTokenResponse createServiceAccountToken(final CreateServiceAccountTokenRequest request, - final RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, SecurityRequestConverters::createServiceAccountToken, options, - CreateServiceAccountTokenResponse::fromXContent, emptySet()); + public CreateServiceAccountTokenResponse createServiceAccountToken( + final CreateServiceAccountTokenRequest request, + final RequestOptions options + ) throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, + SecurityRequestConverters::createServiceAccountToken, + options, + CreateServiceAccountTokenResponse::fromXContent, + emptySet() + ); } /** @@ -1235,11 +1653,19 @@ public CreateServiceAccountTokenResponse createServiceAccountToken(final CreateS * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable createServiceAccountTokenAsync(final CreateServiceAccountTokenRequest request, - final RequestOptions options, - final ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, SecurityRequestConverters::createServiceAccountToken, options, - CreateServiceAccountTokenResponse::fromXContent, listener, emptySet()); + public Cancellable createServiceAccountTokenAsync( + final CreateServiceAccountTokenRequest request, + final RequestOptions options, + final ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + SecurityRequestConverters::createServiceAccountToken, + options, + CreateServiceAccountTokenResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -1252,10 +1678,17 @@ public Cancellable createServiceAccountTokenAsync(final CreateServiceAccountToke * @return the response from the create service account token call * @throws IOException in case there is a problem sending the request or parsing back the response */ - public DeleteServiceAccountTokenResponse deleteServiceAccountToken(final DeleteServiceAccountTokenRequest request, - final RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, SecurityRequestConverters::deleteServiceAccountToken, options, - DeleteServiceAccountTokenResponse::fromXContent, emptySet()); + public DeleteServiceAccountTokenResponse deleteServiceAccountToken( + final DeleteServiceAccountTokenRequest request, + final RequestOptions options + ) throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, + SecurityRequestConverters::deleteServiceAccountToken, + options, + DeleteServiceAccountTokenResponse::fromXContent, + emptySet() + ); } /** @@ -1268,11 +1701,19 @@ public DeleteServiceAccountTokenResponse deleteServiceAccountToken(final DeleteS * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable deleteServiceAccountTokenAsync(final DeleteServiceAccountTokenRequest request, - final RequestOptions options, - final ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, SecurityRequestConverters::deleteServiceAccountToken, options, - DeleteServiceAccountTokenResponse::fromXContent, listener, emptySet()); + public Cancellable deleteServiceAccountTokenAsync( + final DeleteServiceAccountTokenRequest request, + final RequestOptions options, + final ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + SecurityRequestConverters::deleteServiceAccountToken, + options, + DeleteServiceAccountTokenResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -1284,10 +1725,17 @@ public Cancellable deleteServiceAccountTokenAsync(final DeleteServiceAccountToke * @return the response from the get service accounts call * @throws IOException in case there is a problem sending the request or parsing back the response */ - public GetServiceAccountCredentialsResponse getServiceAccountCredentials(GetServiceAccountCredentialsRequest request, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, SecurityRequestConverters::getServiceAccountCredentials, - options, GetServiceAccountCredentialsResponse::fromXContent, emptySet()); + public GetServiceAccountCredentialsResponse getServiceAccountCredentials( + GetServiceAccountCredentialsRequest request, + RequestOptions options + ) throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, + SecurityRequestConverters::getServiceAccountCredentials, + options, + GetServiceAccountCredentialsResponse::fromXContent, + emptySet() + ); } /** @@ -1299,10 +1747,19 @@ public GetServiceAccountCredentialsResponse getServiceAccountCredentials(GetServ * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getServiceAccountCredentialsAsync(GetServiceAccountCredentialsRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, SecurityRequestConverters::getServiceAccountCredentials, - options, GetServiceAccountCredentialsResponse::fromXContent, listener, emptySet()); + public Cancellable getServiceAccountCredentialsAsync( + GetServiceAccountCredentialsRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + SecurityRequestConverters::getServiceAccountCredentials, + options, + GetServiceAccountCredentialsResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -1317,9 +1774,14 @@ public Cancellable getServiceAccountCredentialsAsync(GetServiceAccountCredential * @throws IOException in case there is a problem sending the request or parsing back the response */ public DelegatePkiAuthenticationResponse delegatePkiAuthentication(DelegatePkiAuthenticationRequest request, RequestOptions options) - throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, SecurityRequestConverters::delegatePkiAuthentication, options, - DelegatePkiAuthenticationResponse::fromXContent, emptySet()); + throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, + SecurityRequestConverters::delegatePkiAuthentication, + options, + DelegatePkiAuthenticationResponse::fromXContent, + emptySet() + ); } /** @@ -1334,10 +1796,19 @@ public DelegatePkiAuthenticationResponse delegatePkiAuthentication(DelegatePkiAu * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable delegatePkiAuthenticationAsync(DelegatePkiAuthenticationRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, SecurityRequestConverters::delegatePkiAuthentication, options, - DelegatePkiAuthenticationResponse::fromXContent, listener, emptySet()); + public Cancellable delegatePkiAuthenticationAsync( + DelegatePkiAuthenticationRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + SecurityRequestConverters::delegatePkiAuthentication, + options, + DelegatePkiAuthenticationResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -1348,8 +1819,12 @@ public Cancellable delegatePkiAuthenticationAsync(DelegatePkiAuthenticationReque */ public NodeEnrollmentResponse enrollNode(RequestOptions options) throws IOException { return restHighLevelClient.performRequestAndParseEntity( - NodeEnrollmentRequest.INSTANCE, NodeEnrollmentRequest::getRequest, - options, NodeEnrollmentResponse::fromXContent, emptySet()); + NodeEnrollmentRequest.INSTANCE, + NodeEnrollmentRequest::getRequest, + options, + NodeEnrollmentResponse::fromXContent, + emptySet() + ); } /** @@ -1358,8 +1833,14 @@ public NodeEnrollmentResponse enrollNode(RequestOptions options) throws IOExcept * @param listener the listener to be notified upon request completion. The listener will be called with the value {@code true} */ public Cancellable enrollNodeAsync(RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(NodeEnrollmentRequest.INSTANCE, NodeEnrollmentRequest::getRequest, - options, NodeEnrollmentResponse::fromXContent, listener, emptySet()); + return restHighLevelClient.performRequestAsyncAndParseEntity( + NodeEnrollmentRequest.INSTANCE, + NodeEnrollmentRequest::getRequest, + options, + NodeEnrollmentResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -1374,7 +1855,8 @@ public KibanaEnrollmentResponse enrollKibana(RequestOptions options) throws IOEx KibanaEnrollmentRequest::getRequest, options, KibanaEnrollmentResponse::fromXContent, - emptySet()); + emptySet() + ); } /** @@ -1382,12 +1864,15 @@ public KibanaEnrollmentResponse enrollKibana(RequestOptions options) throws IOEx * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized * @param listener the listener to be notified upon request completion */ - public Cancellable enrollKibanaAsync( - RequestOptions options, - ActionListener listener) { + public Cancellable enrollKibanaAsync(RequestOptions options, ActionListener listener) { return restHighLevelClient.performRequestAsyncAndParseEntity( KibanaEnrollmentRequest.INSTANCE, - KibanaEnrollmentRequest::getRequest, options, KibanaEnrollmentResponse::fromXContent, listener, emptySet()); + KibanaEnrollmentRequest::getRequest, + options, + KibanaEnrollmentResponse::fromXContent, + listener, + emptySet() + ); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/SecurityRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/SecurityRequestConverters.java index 624ff74d802ab..cc8497f8d323f 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/SecurityRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/SecurityRequestConverters.java @@ -58,8 +58,7 @@ final class SecurityRequestConverters { private SecurityRequestConverters() {} static Request changePassword(ChangePasswordRequest changePasswordRequest) throws IOException { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_security/user") + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_security/user") .addPathPart(changePasswordRequest.getUsername()) .addPathPartAsIs("_password") .build(); @@ -72,8 +71,7 @@ static Request changePassword(ChangePasswordRequest changePasswordRequest) throw } static Request getUsers(GetUsersRequest getUsersRequest) { - RequestConverters.EndpointBuilder builder = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_security/user"); + RequestConverters.EndpointBuilder builder = new RequestConverters.EndpointBuilder().addPathPartAsIs("_security/user"); if (getUsersRequest.getUsernames().size() > 0) { builder.addPathPart(Strings.collectionToCommaDelimitedString(getUsersRequest.getUsernames())); } @@ -81,8 +79,7 @@ static Request getUsers(GetUsersRequest getUsersRequest) { } static Request putUser(PutUserRequest putUserRequest) throws IOException { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_security/user") + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_security/user") .addPathPart(putUserRequest.getUser().getUsername()) .build(); Request request = new Request(HttpPut.METHOD_NAME, endpoint); @@ -94,8 +91,7 @@ static Request putUser(PutUserRequest putUserRequest) throws IOException { } static Request deleteUser(DeleteUserRequest deleteUserRequest) { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_security", "user") + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_security", "user") .addPathPart(deleteUserRequest.getName()) .build(); Request request = new Request(HttpDelete.METHOD_NAME, endpoint); @@ -106,8 +102,7 @@ static Request deleteUser(DeleteUserRequest deleteUserRequest) { } static Request putRoleMapping(final PutRoleMappingRequest putRoleMappingRequest) throws IOException { - final String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_security/role_mapping") + final String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_security/role_mapping") .addPathPart(putRoleMappingRequest.getName()) .build(); final Request request = new Request(HttpPut.METHOD_NAME, endpoint); @@ -136,8 +131,7 @@ static Request disableUser(DisableUserRequest disableUserRequest) { } private static Request setUserEnabled(SetUserEnabledRequest setUserEnabledRequest) { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_security/user") + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_security/user") .addPathPart(setUserEnabledRequest.getUsername()) .addPathPart(setUserEnabledRequest.isEnabled() ? "_enable" : "_disable") .build(); @@ -155,8 +149,7 @@ static Request hasPrivileges(HasPrivilegesRequest hasPrivilegesRequest) throws I } static Request clearRealmCache(ClearRealmCacheRequest clearRealmCacheRequest) { - RequestConverters.EndpointBuilder builder = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_security/realm"); + RequestConverters.EndpointBuilder builder = new RequestConverters.EndpointBuilder().addPathPartAsIs("_security/realm"); if (clearRealmCacheRequest.getRealms().isEmpty() == false) { builder.addCommaSeparatedPathParts(clearRealmCacheRequest.getRealms().toArray(Strings.EMPTY_ARRAY)); } else { @@ -173,8 +166,7 @@ static Request clearRealmCache(ClearRealmCacheRequest clearRealmCacheRequest) { } static Request clearRolesCache(ClearRolesCacheRequest disableCacheRequest) { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_security/role") + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_security/role") .addCommaSeparatedPathParts(disableCacheRequest.names()) .addPathPart("_clear_cache") .build(); @@ -182,8 +174,7 @@ static Request clearRolesCache(ClearRolesCacheRequest disableCacheRequest) { } static Request clearPrivilegesCache(ClearPrivilegesCacheRequest clearPrivilegesCacheRequest) { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_security/privilege") + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_security/privilege") .addCommaSeparatedPathParts(clearPrivilegesCacheRequest.applications()) .addPathPart("_clear_cache") .build(); @@ -191,8 +182,7 @@ static Request clearPrivilegesCache(ClearPrivilegesCacheRequest clearPrivilegesC } static Request clearApiKeyCache(ClearApiKeyCacheRequest clearApiKeyCacheRequest) { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_security/api_key") + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_security/api_key") .addCommaSeparatedPathParts(clearApiKeyCacheRequest.ids()) .addPathPart("_clear_cache") .build(); @@ -200,8 +190,7 @@ static Request clearApiKeyCache(ClearApiKeyCacheRequest clearApiKeyCacheRequest) } static Request clearServiceAccountTokenCache(ClearServiceAccountTokenCacheRequest clearServiceAccountTokenCacheRequest) { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_security/service") + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_security/service") .addPathPart(clearServiceAccountTokenCacheRequest.getNamespace(), clearServiceAccountTokenCacheRequest.getServiceName()) .addPathPartAsIs("credential/token") .addCommaSeparatedPathParts(clearServiceAccountTokenCacheRequest.getTokenNames()) @@ -211,8 +200,7 @@ static Request clearServiceAccountTokenCache(ClearServiceAccountTokenCacheReques } static Request deleteRoleMapping(DeleteRoleMappingRequest deleteRoleMappingRequest) { - final String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_security/role_mapping") + final String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_security/role_mapping") .addPathPart(deleteRoleMappingRequest.getName()) .build(); final Request request = new Request(HttpDelete.METHOD_NAME, endpoint); @@ -223,8 +211,7 @@ static Request deleteRoleMapping(DeleteRoleMappingRequest deleteRoleMappingReque } static Request deleteRole(DeleteRoleRequest deleteRoleRequest) { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_security/role") + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_security/role") .addPathPart(deleteRoleRequest.getName()) .build(); Request request = new Request(HttpDelete.METHOD_NAME, endpoint); @@ -262,8 +249,7 @@ static Request invalidateToken(InvalidateTokenRequest invalidateTokenRequest) th } static Request getPrivileges(GetPrivilegesRequest getPrivilegesRequest) { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_security/privilege") + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_security/privilege") .addPathPart(getPrivilegesRequest.getApplicationName()) .addCommaSeparatedPathParts(getPrivilegesRequest.getPrivilegeNames()) .build(); @@ -280,8 +266,7 @@ static Request putPrivileges(final PutPrivilegesRequest putPrivilegesRequest) th } static Request deletePrivileges(DeletePrivilegesRequest deletePrivilegeRequest) { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_security/privilege") + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_security/privilege") .addPathPart(deletePrivilegeRequest.getApplication()) .addCommaSeparatedPathParts(deletePrivilegeRequest.getPrivileges()) .build(); @@ -293,8 +278,7 @@ static Request deletePrivileges(DeletePrivilegesRequest deletePrivilegeRequest) } static Request putRole(final PutRoleRequest putRoleRequest) throws IOException { - final String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_security/role") + final String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_security/role") .addPathPart(putRoleRequest.getRole().getName()) .build(); final Request request = new Request(HttpPut.METHOD_NAME, endpoint); @@ -354,8 +338,9 @@ static Request queryApiKey(final QueryApiKeyRequest queryApiKeyRequest) throws I } static Request getServiceAccounts(final GetServiceAccountsRequest getServiceAccountsRequest) { - final RequestConverters.EndpointBuilder endpointBuilder = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_security/service"); + final RequestConverters.EndpointBuilder endpointBuilder = new RequestConverters.EndpointBuilder().addPathPartAsIs( + "_security/service" + ); if (getServiceAccountsRequest.getNamespace() != null) { endpointBuilder.addPathPart(getServiceAccountsRequest.getNamespace()); if (getServiceAccountsRequest.getServiceName() != null) { @@ -366,8 +351,9 @@ static Request getServiceAccounts(final GetServiceAccountsRequest getServiceAcco } static Request createServiceAccountToken(final CreateServiceAccountTokenRequest createServiceAccountTokenRequest) throws IOException { - final RequestConverters.EndpointBuilder endpointBuilder = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_security/service") + final RequestConverters.EndpointBuilder endpointBuilder = new RequestConverters.EndpointBuilder().addPathPartAsIs( + "_security/service" + ) .addPathPart(createServiceAccountTokenRequest.getNamespace(), createServiceAccountTokenRequest.getServiceName()) .addPathPartAsIs("credential/token"); if (createServiceAccountTokenRequest.getTokenName() != null) { @@ -383,8 +369,9 @@ static Request createServiceAccountToken(final CreateServiceAccountTokenRequest } static Request deleteServiceAccountToken(final DeleteServiceAccountTokenRequest deleteServiceAccountTokenRequest) { - final RequestConverters.EndpointBuilder endpointBuilder = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_security/service") + final RequestConverters.EndpointBuilder endpointBuilder = new RequestConverters.EndpointBuilder().addPathPartAsIs( + "_security/service" + ) .addPathPart(deleteServiceAccountTokenRequest.getNamespace(), deleteServiceAccountTokenRequest.getServiceName()) .addPathPartAsIs("credential/token") .addPathPart(deleteServiceAccountTokenRequest.getTokenName()); @@ -399,8 +386,9 @@ static Request deleteServiceAccountToken(final DeleteServiceAccountTokenRequest } static Request getServiceAccountCredentials(final GetServiceAccountCredentialsRequest getServiceAccountCredentialsRequest) { - final RequestConverters.EndpointBuilder endpointBuilder = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_security/service") + final RequestConverters.EndpointBuilder endpointBuilder = new RequestConverters.EndpointBuilder().addPathPartAsIs( + "_security/service" + ) .addPathPart(getServiceAccountCredentialsRequest.getNamespace(), getServiceAccountCredentialsRequest.getServiceName()) .addPathPartAsIs("credential"); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/SnapshotClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/SnapshotClient.java index 28406b9bb119a..c31fe0415bb07 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/SnapshotClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/SnapshotClient.java @@ -61,10 +61,14 @@ public final class SnapshotClient { * @return the response * @throws IOException in case there is a problem sending the request or parsing back the response */ - public GetRepositoriesResponse getRepository(GetRepositoriesRequest getRepositoriesRequest, RequestOptions options) - throws IOException { - return restHighLevelClient.performRequestAndParseEntity(getRepositoriesRequest, SnapshotRequestConverters::getRepositories, options, - GetRepositoriesResponse::fromXContent, emptySet()); + public GetRepositoriesResponse getRepository(GetRepositoriesRequest getRepositoriesRequest, RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + getRepositoriesRequest, + SnapshotRequestConverters::getRepositories, + options, + GetRepositoriesResponse::fromXContent, + emptySet() + ); } /** @@ -77,11 +81,19 @@ public GetRepositoriesResponse getRepository(GetRepositoriesRequest getRepositor * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getRepositoryAsync(GetRepositoriesRequest getRepositoriesRequest, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(getRepositoriesRequest, - SnapshotRequestConverters::getRepositories, options, - GetRepositoriesResponse::fromXContent, listener, emptySet()); + public Cancellable getRepositoryAsync( + GetRepositoriesRequest getRepositoriesRequest, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + getRepositoriesRequest, + SnapshotRequestConverters::getRepositories, + options, + GetRepositoriesResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -94,8 +106,13 @@ public Cancellable getRepositoryAsync(GetRepositoriesRequest getRepositoriesRequ * @throws IOException in case there is a problem sending the request or parsing back the response */ public AcknowledgedResponse createRepository(PutRepositoryRequest putRepositoryRequest, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(putRepositoryRequest, SnapshotRequestConverters::createRepository, options, - AcknowledgedResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + putRepositoryRequest, + SnapshotRequestConverters::createRepository, + options, + AcknowledgedResponse::fromXContent, + emptySet() + ); } /** @@ -107,11 +124,19 @@ public AcknowledgedResponse createRepository(PutRepositoryRequest putRepositoryR * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable createRepositoryAsync(PutRepositoryRequest putRepositoryRequest, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(putRepositoryRequest, - SnapshotRequestConverters::createRepository, options, - AcknowledgedResponse::fromXContent, listener, emptySet()); + public Cancellable createRepositoryAsync( + PutRepositoryRequest putRepositoryRequest, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + putRepositoryRequest, + SnapshotRequestConverters::createRepository, + options, + AcknowledgedResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -125,8 +150,13 @@ public Cancellable createRepositoryAsync(PutRepositoryRequest putRepositoryReque */ public AcknowledgedResponse deleteRepository(DeleteRepositoryRequest deleteRepositoryRequest, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(deleteRepositoryRequest, SnapshotRequestConverters::deleteRepository, - options, AcknowledgedResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + deleteRepositoryRequest, + SnapshotRequestConverters::deleteRepository, + options, + AcknowledgedResponse::fromXContent, + emptySet() + ); } /** @@ -138,11 +168,19 @@ public AcknowledgedResponse deleteRepository(DeleteRepositoryRequest deleteRepos * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable deleteRepositoryAsync(DeleteRepositoryRequest deleteRepositoryRequest, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(deleteRepositoryRequest, - SnapshotRequestConverters::deleteRepository, options, - AcknowledgedResponse::fromXContent, listener, emptySet()); + public Cancellable deleteRepositoryAsync( + DeleteRepositoryRequest deleteRepositoryRequest, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + deleteRepositoryRequest, + SnapshotRequestConverters::deleteRepository, + options, + AcknowledgedResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -156,8 +194,13 @@ public Cancellable deleteRepositoryAsync(DeleteRepositoryRequest deleteRepositor */ public VerifyRepositoryResponse verifyRepository(VerifyRepositoryRequest verifyRepositoryRequest, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(verifyRepositoryRequest, SnapshotRequestConverters::verifyRepository, - options, VerifyRepositoryResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + verifyRepositoryRequest, + SnapshotRequestConverters::verifyRepository, + options, + VerifyRepositoryResponse::fromXContent, + emptySet() + ); } /** @@ -169,11 +212,19 @@ public VerifyRepositoryResponse verifyRepository(VerifyRepositoryRequest verifyR * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable verifyRepositoryAsync(VerifyRepositoryRequest verifyRepositoryRequest, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(verifyRepositoryRequest, - SnapshotRequestConverters::verifyRepository, options, - VerifyRepositoryResponse::fromXContent, listener, emptySet()); + public Cancellable verifyRepositoryAsync( + VerifyRepositoryRequest verifyRepositoryRequest, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + verifyRepositoryRequest, + SnapshotRequestConverters::verifyRepository, + options, + VerifyRepositoryResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -187,8 +238,13 @@ public Cancellable verifyRepositoryAsync(VerifyRepositoryRequest verifyRepositor */ public CleanupRepositoryResponse cleanupRepository(CleanupRepositoryRequest cleanupRepositoryRequest, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(cleanupRepositoryRequest, SnapshotRequestConverters::cleanupRepository, - options, CleanupRepositoryResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + cleanupRepositoryRequest, + SnapshotRequestConverters::cleanupRepository, + options, + CleanupRepositoryResponse::fromXContent, + emptySet() + ); } /** @@ -200,10 +256,19 @@ public CleanupRepositoryResponse cleanupRepository(CleanupRepositoryRequest clea * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable cleanupRepositoryAsync(CleanupRepositoryRequest cleanupRepositoryRequest, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(cleanupRepositoryRequest, SnapshotRequestConverters::cleanupRepository, - options, CleanupRepositoryResponse::fromXContent, listener, emptySet()); + public Cancellable cleanupRepositoryAsync( + CleanupRepositoryRequest cleanupRepositoryRequest, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + cleanupRepositoryRequest, + SnapshotRequestConverters::cleanupRepository, + options, + CleanupRepositoryResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -212,10 +277,14 @@ public Cancellable cleanupRepositoryAsync(CleanupRepositoryRequest cleanupReposi * See Snapshot and Restore * API on elastic.co */ - public CreateSnapshotResponse create(CreateSnapshotRequest createSnapshotRequest, RequestOptions options) - throws IOException { - return restHighLevelClient.performRequestAndParseEntity(createSnapshotRequest, SnapshotRequestConverters::createSnapshot, options, - CreateSnapshotResponse::fromXContent, emptySet()); + public CreateSnapshotResponse create(CreateSnapshotRequest createSnapshotRequest, RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + createSnapshotRequest, + SnapshotRequestConverters::createSnapshot, + options, + CreateSnapshotResponse::fromXContent, + emptySet() + ); } /** @@ -225,11 +294,19 @@ public CreateSnapshotResponse create(CreateSnapshotRequest createSnapshotRequest * API on elastic.co * @return cancellable that may be used to cancel the request */ - public Cancellable createAsync(CreateSnapshotRequest createSnapshotRequest, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(createSnapshotRequest, - SnapshotRequestConverters::createSnapshot, options, - CreateSnapshotResponse::fromXContent, listener, emptySet()); + public Cancellable createAsync( + CreateSnapshotRequest createSnapshotRequest, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + createSnapshotRequest, + SnapshotRequestConverters::createSnapshot, + options, + CreateSnapshotResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -238,10 +315,14 @@ public Cancellable createAsync(CreateSnapshotRequest createSnapshotRequest, Requ * See Snapshot and Restore * API on elastic.co */ - public AcknowledgedResponse clone(CloneSnapshotRequest cloneSnapshotRequest, RequestOptions options) - throws IOException { - return restHighLevelClient.performRequestAndParseEntity(cloneSnapshotRequest, SnapshotRequestConverters::cloneSnapshot, options, - AcknowledgedResponse::fromXContent, emptySet()); + public AcknowledgedResponse clone(CloneSnapshotRequest cloneSnapshotRequest, RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + cloneSnapshotRequest, + SnapshotRequestConverters::cloneSnapshot, + options, + AcknowledgedResponse::fromXContent, + emptySet() + ); } /** @@ -251,11 +332,19 @@ public AcknowledgedResponse clone(CloneSnapshotRequest cloneSnapshotRequest, Req * API on elastic.co * @return cancellable that may be used to cancel the request */ - public Cancellable cloneAsync(CloneSnapshotRequest cloneSnapshotRequest, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(cloneSnapshotRequest, - SnapshotRequestConverters::cloneSnapshot, options, - AcknowledgedResponse::fromXContent, listener, emptySet()); + public Cancellable cloneAsync( + CloneSnapshotRequest cloneSnapshotRequest, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + cloneSnapshotRequest, + SnapshotRequestConverters::cloneSnapshot, + options, + AcknowledgedResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -269,8 +358,13 @@ public Cancellable cloneAsync(CloneSnapshotRequest cloneSnapshotRequest, Request * @throws IOException in case there is a problem sending the request or parsing back the response */ public GetSnapshotsResponse get(GetSnapshotsRequest getSnapshotsRequest, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(getSnapshotsRequest, SnapshotRequestConverters::getSnapshots, options, - GetSnapshotsResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + getSnapshotsRequest, + SnapshotRequestConverters::getSnapshots, + options, + GetSnapshotsResponse::fromXContent, + emptySet() + ); } /** @@ -282,11 +376,19 @@ public GetSnapshotsResponse get(GetSnapshotsRequest getSnapshotsRequest, Request * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getAsync(GetSnapshotsRequest getSnapshotsRequest, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(getSnapshotsRequest, - SnapshotRequestConverters::getSnapshots, options, - GetSnapshotsResponse::fromXContent, listener, emptySet()); + public Cancellable getAsync( + GetSnapshotsRequest getSnapshotsRequest, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + getSnapshotsRequest, + SnapshotRequestConverters::getSnapshots, + options, + GetSnapshotsResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -298,10 +400,14 @@ public Cancellable getAsync(GetSnapshotsRequest getSnapshotsRequest, RequestOpti * @return the response * @throws IOException in case there is a problem sending the request or parsing back the response */ - public SnapshotsStatusResponse status(SnapshotsStatusRequest snapshotsStatusRequest, RequestOptions options) - throws IOException { - return restHighLevelClient.performRequestAndParseEntity(snapshotsStatusRequest, SnapshotRequestConverters::snapshotsStatus, options, - SnapshotsStatusResponse::fromXContent, emptySet()); + public SnapshotsStatusResponse status(SnapshotsStatusRequest snapshotsStatusRequest, RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + snapshotsStatusRequest, + SnapshotRequestConverters::snapshotsStatus, + options, + SnapshotsStatusResponse::fromXContent, + emptySet() + ); } /** @@ -313,11 +419,19 @@ public SnapshotsStatusResponse status(SnapshotsStatusRequest snapshotsStatusRequ * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable statusAsync(SnapshotsStatusRequest snapshotsStatusRequest, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(snapshotsStatusRequest, - SnapshotRequestConverters::snapshotsStatus, options, - SnapshotsStatusResponse::fromXContent, listener, emptySet()); + public Cancellable statusAsync( + SnapshotsStatusRequest snapshotsStatusRequest, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + snapshotsStatusRequest, + SnapshotRequestConverters::snapshotsStatus, + options, + SnapshotsStatusResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -331,8 +445,13 @@ public Cancellable statusAsync(SnapshotsStatusRequest snapshotsStatusRequest, Re * @throws IOException in case there is a problem sending the request or parsing back the response */ public RestoreSnapshotResponse restore(RestoreSnapshotRequest restoreSnapshotRequest, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(restoreSnapshotRequest, SnapshotRequestConverters::restoreSnapshot, options, - RestoreSnapshotResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + restoreSnapshotRequest, + SnapshotRequestConverters::restoreSnapshot, + options, + RestoreSnapshotResponse::fromXContent, + emptySet() + ); } /** @@ -345,11 +464,19 @@ public RestoreSnapshotResponse restore(RestoreSnapshotRequest restoreSnapshotReq * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable restoreAsync(RestoreSnapshotRequest restoreSnapshotRequest, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(restoreSnapshotRequest, - SnapshotRequestConverters::restoreSnapshot, options, - RestoreSnapshotResponse::fromXContent, listener, emptySet()); + public Cancellable restoreAsync( + RestoreSnapshotRequest restoreSnapshotRequest, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + restoreSnapshotRequest, + SnapshotRequestConverters::restoreSnapshot, + options, + RestoreSnapshotResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -363,9 +490,13 @@ public Cancellable restoreAsync(RestoreSnapshotRequest restoreSnapshotRequest, R * @throws IOException in case there is a problem sending the request or parsing back the response */ public AcknowledgedResponse delete(DeleteSnapshotRequest deleteSnapshotRequest, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(deleteSnapshotRequest, - SnapshotRequestConverters::deleteSnapshot, options, - AcknowledgedResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + deleteSnapshotRequest, + SnapshotRequestConverters::deleteSnapshot, + options, + AcknowledgedResponse::fromXContent, + emptySet() + ); } /** @@ -378,10 +509,18 @@ public AcknowledgedResponse delete(DeleteSnapshotRequest deleteSnapshotRequest, * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable deleteAsync(DeleteSnapshotRequest deleteSnapshotRequest, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(deleteSnapshotRequest, - SnapshotRequestConverters::deleteSnapshot, options, - AcknowledgedResponse::fromXContent, listener, emptySet()); + public Cancellable deleteAsync( + DeleteSnapshotRequest deleteSnapshotRequest, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + deleteSnapshotRequest, + SnapshotRequestConverters::deleteSnapshot, + options, + AcknowledgedResponse::fromXContent, + listener, + emptySet() + ); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/SnapshotRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/SnapshotRequestConverters.java index 31383d0c351bc..37e40fcd10a8b 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/SnapshotRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/SnapshotRequestConverters.java @@ -33,7 +33,8 @@ private SnapshotRequestConverters() {} static Request getRepositories(GetRepositoriesRequest getRepositoriesRequest) { String[] repositories = getRepositoriesRequest.repositories() == null ? Strings.EMPTY_ARRAY : getRepositoriesRequest.repositories(); - String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_snapshot").addCommaSeparatedPathParts(repositories) + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_snapshot") + .addCommaSeparatedPathParts(repositories) .build(); Request request = new Request(HttpGet.METHOD_NAME, endpoint); @@ -60,7 +61,8 @@ static Request createRepository(PutRepositoryRequest putRepositoryRequest) throw } static Request deleteRepository(DeleteRepositoryRequest deleteRepositoryRequest) { - String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_snapshot").addPathPart(deleteRepositoryRequest.name()) + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_snapshot") + .addPathPart(deleteRepositoryRequest.name()) .build(); Request request = new Request(HttpDelete.METHOD_NAME, endpoint); @@ -115,11 +117,11 @@ static Request createSnapshot(CreateSnapshotRequest createSnapshotRequest) throw static Request cloneSnapshot(CloneSnapshotRequest cloneSnapshotRequest) throws IOException { String endpoint = new RequestConverters.EndpointBuilder().addPathPart("_snapshot") - .addPathPart(cloneSnapshotRequest.repository()) - .addPathPart(cloneSnapshotRequest.source()) - .addPathPart("_clone") - .addPathPart(cloneSnapshotRequest.target()) - .build(); + .addPathPart(cloneSnapshotRequest.repository()) + .addPathPart(cloneSnapshotRequest.source()) + .addPathPart("_clone") + .addPathPart(cloneSnapshotRequest.target()) + .build(); Request request = new Request(HttpPut.METHOD_NAME, endpoint); RequestConverters.Params params = new RequestConverters.Params(); params.withMasterTimeout(cloneSnapshotRequest.masterNodeTimeout()); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/TasksClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/TasksClient.java index 0d3cc51998f9e..83f2185f5104b 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/TasksClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/TasksClient.java @@ -49,8 +49,13 @@ public final class TasksClient { * @throws IOException in case there is a problem sending the request or parsing back the response */ public ListTasksResponse list(ListTasksRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, TasksRequestConverters::listTasks, options, - ListTasksResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + TasksRequestConverters::listTasks, + options, + ListTasksResponse::fromXContent, + emptySet() + ); } /** @@ -63,8 +68,14 @@ public ListTasksResponse list(ListTasksRequest request, RequestOptions options) * @return cancellable that may be used to cancel the request */ public Cancellable listAsync(ListTasksRequest request, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, TasksRequestConverters::listTasks, options, - ListTasksResponse::fromXContent, listener, emptySet()); + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + TasksRequestConverters::listTasks, + options, + ListTasksResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -77,8 +88,12 @@ public Cancellable listAsync(ListTasksRequest request, RequestOptions options, A * @throws IOException in case there is a problem sending the request or parsing back the response */ public Optional get(GetTaskRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseOptionalEntity(request, TasksRequestConverters::getTask, options, - GetTaskResponse::fromXContent); + return restHighLevelClient.performRequestAndParseOptionalEntity( + request, + TasksRequestConverters::getTask, + options, + GetTaskResponse::fromXContent + ); } /** @@ -90,11 +105,15 @@ public Optional get(GetTaskRequest request, RequestOptions opti * @param listener an actionlistener that takes an optional response (404s are returned as an empty Optional) * @return cancellable that may be used to cancel the request */ - public Cancellable getAsync(GetTaskRequest request, RequestOptions options, - ActionListener> listener) { + public Cancellable getAsync(GetTaskRequest request, RequestOptions options, ActionListener> listener) { - return restHighLevelClient.performRequestAsyncAndParseOptionalEntity(request, TasksRequestConverters::getTask, options, - GetTaskResponse::fromXContent, listener); + return restHighLevelClient.performRequestAsyncAndParseOptionalEntity( + request, + TasksRequestConverters::getTask, + options, + GetTaskResponse::fromXContent, + listener + ); } /** @@ -108,7 +127,7 @@ public Cancellable getAsync(GetTaskRequest request, RequestOptions options, * @throws IOException in case there is a problem sending the request or parsing back the response * */ - public CancelTasksResponse cancel(CancelTasksRequest cancelTasksRequest, RequestOptions options ) throws IOException { + public CancelTasksResponse cancel(CancelTasksRequest cancelTasksRequest, RequestOptions options) throws IOException { return restHighLevelClient.performRequestAndParseEntity( cancelTasksRequest, TasksRequestConverters::cancelTasks, @@ -128,8 +147,11 @@ public CancelTasksResponse cancel(CancelTasksRequest cancelTasksRequest, Request * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable cancelAsync(CancelTasksRequest cancelTasksRequest, RequestOptions options, - ActionListener listener) { + public Cancellable cancelAsync( + CancelTasksRequest cancelTasksRequest, + RequestOptions options, + ActionListener listener + ) { return restHighLevelClient.performRequestAsyncAndParseEntity( cancelTasksRequest, TasksRequestConverters::cancelTasks, diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/TasksRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/TasksRequestConverters.java index dd9dd09d0113a..a9c9df2c451e9 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/TasksRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/TasksRequestConverters.java @@ -25,9 +25,7 @@ static Request cancelTasks(CancelTasksRequest req) { req.getTimeout().ifPresent(params::withTimeout); req.getTaskId().ifPresent(params::withTaskId); req.getParentTaskId().ifPresent(params::withParentTaskId); - params - .withNodes(req.getNodes()) - .withActions(req.getActions()); + params.withNodes(req.getNodes()).withActions(req.getActions()); if (req.getWaitForCompletion() != null) { params.withWaitForCompletion(req.getWaitForCompletion()); } @@ -39,7 +37,7 @@ static Request listTasks(ListTasksRequest listTaskRequest) { if (listTaskRequest.getTaskId() != null && listTaskRequest.getTaskId().isSet()) { throw new IllegalArgumentException("TaskId cannot be used for list tasks request"); } - Request request = new Request(HttpGet.METHOD_NAME, "/_tasks"); + Request request = new Request(HttpGet.METHOD_NAME, "/_tasks"); RequestConverters.Params params = new RequestConverters.Params(); params.withTimeout(listTaskRequest.getTimeout()) .withDetailed(listTaskRequest.getDetailed()) @@ -54,12 +52,11 @@ static Request listTasks(ListTasksRequest listTaskRequest) { static Request getTask(GetTaskRequest getTaskRequest) { String endpoint = new EndpointBuilder().addPathPartAsIs("_tasks") - .addPathPartAsIs(getTaskRequest.getNodeId() + ":" + Long.toString(getTaskRequest.getTaskId())) - .build(); + .addPathPartAsIs(getTaskRequest.getNodeId() + ":" + Long.toString(getTaskRequest.getTaskId())) + .build(); Request request = new Request(HttpGet.METHOD_NAME, endpoint); RequestConverters.Params params = new RequestConverters.Params(); - params.withTimeout(getTaskRequest.getTimeout()) - .withWaitForCompletion(getTaskRequest.getWaitForCompletion()); + params.withTimeout(getTaskRequest.getTimeout()).withWaitForCompletion(getTaskRequest.getWaitForCompletion()); request.addParameters(params.asMap()); return request; } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/TextStructureClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/TextStructureClient.java index 5549ac16666c7..7d5e822e7a5c2 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/TextStructureClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/TextStructureClient.java @@ -14,7 +14,6 @@ import java.io.IOException; import java.util.Collections; - /** * Text Structure API client wrapper for the {@link RestHighLevelClient} *

@@ -48,11 +47,13 @@ public final class TextStructureClient { * @throws IOException when there is a serialization issue sending the request or receiving the response */ public FindStructureResponse findStructure(FindStructureRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + return restHighLevelClient.performRequestAndParseEntity( + request, TextStructureRequestConverters::findFileStructure, options, FindStructureResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -67,14 +68,19 @@ public FindStructureResponse findStructure(FindStructureRequest request, Request * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable findStructureAsync(FindStructureRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + public Cancellable findStructureAsync( + FindStructureRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, TextStructureRequestConverters::findFileStructure, options, FindStructureResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/TextStructureRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/TextStructureRequestConverters.java index 721955dfb5017..2d2438d705162 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/TextStructureRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/TextStructureRequestConverters.java @@ -8,8 +8,6 @@ package org.elasticsearch.client; -import static org.elasticsearch.client.RequestConverters.createContentType; - import org.apache.http.HttpEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.nio.entity.NByteArrayEntity; @@ -20,21 +18,19 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.xcontent.XContentType; +import static org.elasticsearch.client.RequestConverters.createContentType; + final class TextStructureRequestConverters { private TextStructureRequestConverters() {} static Request findFileStructure(FindStructureRequest findStructureRequest) { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_text_structure") - .addPathPartAsIs("find_structure") - .build(); + String endpoint = new EndpointBuilder().addPathPartAsIs("_text_structure").addPathPartAsIs("find_structure").build(); Request request = new Request(HttpPost.METHOD_NAME, endpoint); RequestConverters.Params params = new RequestConverters.Params(); if (findStructureRequest.getLinesToSample() != null) { - params.putParam(FindStructureRequest.LINES_TO_SAMPLE.getPreferredName(), - findStructureRequest.getLinesToSample().toString()); + params.putParam(FindStructureRequest.LINES_TO_SAMPLE.getPreferredName(), findStructureRequest.getLinesToSample().toString()); } if (findStructureRequest.getTimeout() != null) { params.putParam(FindStructureRequest.TIMEOUT.getPreferredName(), findStructureRequest.getTimeout().toString()); @@ -46,23 +42,25 @@ static Request findFileStructure(FindStructureRequest findStructureRequest) { params.putParam(FindStructureRequest.FORMAT.getPreferredName(), findStructureRequest.getFormat().toString()); } if (findStructureRequest.getColumnNames() != null) { - params.putParam(FindStructureRequest.COLUMN_NAMES.getPreferredName(), - Strings.collectionToCommaDelimitedString(findStructureRequest.getColumnNames())); + params.putParam( + FindStructureRequest.COLUMN_NAMES.getPreferredName(), + Strings.collectionToCommaDelimitedString(findStructureRequest.getColumnNames()) + ); } if (findStructureRequest.getHasHeaderRow() != null) { - params.putParam(FindStructureRequest.HAS_HEADER_ROW.getPreferredName(), - findStructureRequest.getHasHeaderRow().toString()); + params.putParam(FindStructureRequest.HAS_HEADER_ROW.getPreferredName(), findStructureRequest.getHasHeaderRow().toString()); } if (findStructureRequest.getDelimiter() != null) { - params.putParam(FindStructureRequest.DELIMITER.getPreferredName(), - findStructureRequest.getDelimiter().toString()); + params.putParam(FindStructureRequest.DELIMITER.getPreferredName(), findStructureRequest.getDelimiter().toString()); } if (findStructureRequest.getQuote() != null) { params.putParam(FindStructureRequest.QUOTE.getPreferredName(), findStructureRequest.getQuote().toString()); } if (findStructureRequest.getShouldTrimFields() != null) { - params.putParam(FindStructureRequest.SHOULD_TRIM_FIELDS.getPreferredName(), - findStructureRequest.getShouldTrimFields().toString()); + params.putParam( + FindStructureRequest.SHOULD_TRIM_FIELDS.getPreferredName(), + findStructureRequest.getShouldTrimFields().toString() + ); } if (findStructureRequest.getGrokPattern() != null) { params.putParam(FindStructureRequest.GROK_PATTERN.getPreferredName(), findStructureRequest.getGrokPattern()); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/TransformClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/TransformClient.java index 74be3b94054ac..d9c5d99e50e99 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/TransformClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/TransformClient.java @@ -57,11 +57,13 @@ public final class TransformClient { * @throws IOException when there is a serialization issue sending the request or receiving the response */ public AcknowledgedResponse putTransform(PutTransformRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, - TransformRequestConverters::putTransform, - options, - AcknowledgedResponse::fromXContent, - Collections.emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + TransformRequestConverters::putTransform, + options, + AcknowledgedResponse::fromXContent, + Collections.emptySet() + ); } /** @@ -76,14 +78,19 @@ public AcknowledgedResponse putTransform(PutTransformRequest request, RequestOpt * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable putTransformAsync(PutTransformRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, - TransformRequestConverters::putTransform, - options, - AcknowledgedResponse::fromXContent, - listener, - Collections.emptySet()); + public Cancellable putTransformAsync( + PutTransformRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + TransformRequestConverters::putTransform, + options, + AcknowledgedResponse::fromXContent, + listener, + Collections.emptySet() + ); } /** @@ -99,13 +106,14 @@ public Cancellable putTransformAsync(PutTransformRequest request, RequestOptions * @return An UpdateTransformResponse object containing the updated configuration * @throws IOException when there is a serialization issue sending the request or receiving the response */ - public UpdateTransformResponse updateTransform(UpdateTransformRequest request, - RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, + public UpdateTransformResponse updateTransform(UpdateTransformRequest request, RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, TransformRequestConverters::updateTransform, options, UpdateTransformResponse::fromXContent, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -120,15 +128,19 @@ public UpdateTransformResponse updateTransform(UpdateTransformRequest request, * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable updateTransformAsync(UpdateTransformRequest request, - RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, + public Cancellable updateTransformAsync( + UpdateTransformRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, TransformRequestConverters::updateTransform, options, UpdateTransformResponse::fromXContent, listener, - Collections.emptySet()); + Collections.emptySet() + ); } /** @@ -143,13 +155,14 @@ public Cancellable updateTransformAsync(UpdateTransformRequest request, * @return The transform stats * @throws IOException when there is a serialization issue sending the request or receiving the response */ - public GetTransformStatsResponse getTransformStats(GetTransformStatsRequest request, RequestOptions options) - throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, - TransformRequestConverters::getTransformStats, - options, - GetTransformStatsResponse::fromXContent, - Collections.emptySet()); + public GetTransformStatsResponse getTransformStats(GetTransformStatsRequest request, RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, + TransformRequestConverters::getTransformStats, + options, + GetTransformStatsResponse::fromXContent, + Collections.emptySet() + ); } /** @@ -163,14 +176,19 @@ public GetTransformStatsResponse getTransformStats(GetTransformStatsRequest requ * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getTransformStatsAsync(GetTransformStatsRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, - TransformRequestConverters::getTransformStats, - options, - GetTransformStatsResponse::fromXContent, - listener, - Collections.emptySet()); + public Cancellable getTransformStatsAsync( + GetTransformStatsRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + TransformRequestConverters::getTransformStats, + options, + GetTransformStatsResponse::fromXContent, + listener, + Collections.emptySet() + ); } /** @@ -185,13 +203,14 @@ public Cancellable getTransformStatsAsync(GetTransformStatsRequest request, Requ * @return An AcknowledgedResponse object indicating request success * @throws IOException when there is a serialization issue sending the request or receiving the response */ - public AcknowledgedResponse deleteTransform(DeleteTransformRequest request, RequestOptions options) - throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, - TransformRequestConverters::deleteTransform, - options, - AcknowledgedResponse::fromXContent, - Collections.emptySet()); + public AcknowledgedResponse deleteTransform(DeleteTransformRequest request, RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, + TransformRequestConverters::deleteTransform, + options, + AcknowledgedResponse::fromXContent, + Collections.emptySet() + ); } /** @@ -205,14 +224,19 @@ public AcknowledgedResponse deleteTransform(DeleteTransformRequest request, Requ * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable deleteTransformAsync(DeleteTransformRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, - TransformRequestConverters::deleteTransform, - options, - AcknowledgedResponse::fromXContent, - listener, - Collections.emptySet()); + public Cancellable deleteTransformAsync( + DeleteTransformRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + TransformRequestConverters::deleteTransform, + options, + AcknowledgedResponse::fromXContent, + listener, + Collections.emptySet() + ); } /** @@ -227,13 +251,14 @@ public Cancellable deleteTransformAsync(DeleteTransformRequest request, RequestO * @return A response containing the results of the applied transform * @throws IOException when there is a serialization issue sending the request or receiving the response */ - public PreviewTransformResponse previewTransform(PreviewTransformRequest request, RequestOptions options) - throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, - TransformRequestConverters::previewTransform, - options, - PreviewTransformResponse::fromXContent, - Collections.emptySet()); + public PreviewTransformResponse previewTransform(PreviewTransformRequest request, RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, + TransformRequestConverters::previewTransform, + options, + PreviewTransformResponse::fromXContent, + Collections.emptySet() + ); } /** @@ -246,14 +271,19 @@ public PreviewTransformResponse previewTransform(PreviewTransformRequest request * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable previewTransformAsync(PreviewTransformRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, - TransformRequestConverters::previewTransform, - options, - PreviewTransformResponse::fromXContent, - listener, - Collections.emptySet()); + public Cancellable previewTransformAsync( + PreviewTransformRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + TransformRequestConverters::previewTransform, + options, + PreviewTransformResponse::fromXContent, + listener, + Collections.emptySet() + ); } /** @@ -268,13 +298,14 @@ public Cancellable previewTransformAsync(PreviewTransformRequest request, Reques * @return A response object indicating request success * @throws IOException when there is a serialization issue sending the request or receiving the response */ - public StartTransformResponse startTransform(StartTransformRequest request, RequestOptions options) - throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, - TransformRequestConverters::startTransform, - options, - StartTransformResponse::fromXContent, - Collections.emptySet()); + public StartTransformResponse startTransform(StartTransformRequest request, RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, + TransformRequestConverters::startTransform, + options, + StartTransformResponse::fromXContent, + Collections.emptySet() + ); } /** @@ -288,14 +319,19 @@ public StartTransformResponse startTransform(StartTransformRequest request, Requ * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable startTransformAsync(StartTransformRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, - TransformRequestConverters::startTransform, - options, - StartTransformResponse::fromXContent, - listener, - Collections.emptySet()); + public Cancellable startTransformAsync( + StartTransformRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + TransformRequestConverters::startTransform, + options, + StartTransformResponse::fromXContent, + listener, + Collections.emptySet() + ); } /** @@ -310,13 +346,14 @@ public Cancellable startTransformAsync(StartTransformRequest request, RequestOpt * @return A response object indicating request success * @throws IOException when there is a serialization issue sending the request or receiving the response */ - public StopTransformResponse stopTransform(StopTransformRequest request, RequestOptions options) - throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, - TransformRequestConverters::stopTransform, - options, - StopTransformResponse::fromXContent, - Collections.emptySet()); + public StopTransformResponse stopTransform(StopTransformRequest request, RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, + TransformRequestConverters::stopTransform, + options, + StopTransformResponse::fromXContent, + Collections.emptySet() + ); } /** @@ -330,14 +367,19 @@ public StopTransformResponse stopTransform(StopTransformRequest request, Request * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable stopTransformAsync(StopTransformRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, - TransformRequestConverters::stopTransform, - options, - StopTransformResponse::fromXContent, - listener, - Collections.emptySet()); + public Cancellable stopTransformAsync( + StopTransformRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + TransformRequestConverters::stopTransform, + options, + StopTransformResponse::fromXContent, + listener, + Collections.emptySet() + ); } /** @@ -352,13 +394,14 @@ public Cancellable stopTransformAsync(StopTransformRequest request, RequestOptio * @return An GetTransformResponse containing the requested transforms * @throws IOException when there is a serialization issue sending the request or receiving the response */ - public GetTransformResponse getTransform(GetTransformRequest request, RequestOptions options) - throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, - TransformRequestConverters::getTransform, - options, - GetTransformResponse::fromXContent, - Collections.emptySet()); + public GetTransformResponse getTransform(GetTransformRequest request, RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity( + request, + TransformRequestConverters::getTransform, + options, + GetTransformResponse::fromXContent, + Collections.emptySet() + ); } /** @@ -372,13 +415,18 @@ public GetTransformResponse getTransform(GetTransformRequest request, RequestOpt * @param listener Listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getTransformAsync(GetTransformRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, - TransformRequestConverters::getTransform, - options, - GetTransformResponse::fromXContent, - listener, - Collections.emptySet()); + public Cancellable getTransformAsync( + GetTransformRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + TransformRequestConverters::getTransform, + options, + GetTransformResponse::fromXContent, + listener, + Collections.emptySet() + ); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/Validatable.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/Validatable.java index e083f1c1332fe..b7635f7054299 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/Validatable.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/Validatable.java @@ -14,7 +14,8 @@ */ public interface Validatable { - Validatable EMPTY = new Validatable() {}; + Validatable EMPTY = new Validatable() { + }; /** * Perform validation. This method does not have to be overridden in the event that no validation needs to be done, diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/WatcherClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/WatcherClient.java index 9f535e76ec9f5..b14cff12ab6e2 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/WatcherClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/WatcherClient.java @@ -8,23 +8,23 @@ package org.elasticsearch.client; import org.elasticsearch.action.ActionListener; -import org.elasticsearch.client.watcher.DeactivateWatchRequest; -import org.elasticsearch.client.watcher.DeactivateWatchResponse; import org.elasticsearch.action.support.master.AcknowledgedResponse; -import org.elasticsearch.client.watcher.ActivateWatchRequest; -import org.elasticsearch.client.watcher.ActivateWatchResponse; import org.elasticsearch.client.watcher.AckWatchRequest; import org.elasticsearch.client.watcher.AckWatchResponse; +import org.elasticsearch.client.watcher.ActivateWatchRequest; +import org.elasticsearch.client.watcher.ActivateWatchResponse; +import org.elasticsearch.client.watcher.DeactivateWatchRequest; +import org.elasticsearch.client.watcher.DeactivateWatchResponse; +import org.elasticsearch.client.watcher.DeleteWatchRequest; +import org.elasticsearch.client.watcher.DeleteWatchResponse; import org.elasticsearch.client.watcher.ExecuteWatchRequest; import org.elasticsearch.client.watcher.ExecuteWatchResponse; import org.elasticsearch.client.watcher.GetWatchRequest; import org.elasticsearch.client.watcher.GetWatchResponse; -import org.elasticsearch.client.watcher.StartWatchServiceRequest; -import org.elasticsearch.client.watcher.StopWatchServiceRequest; -import org.elasticsearch.client.watcher.DeleteWatchRequest; -import org.elasticsearch.client.watcher.DeleteWatchResponse; import org.elasticsearch.client.watcher.PutWatchRequest; import org.elasticsearch.client.watcher.PutWatchResponse; +import org.elasticsearch.client.watcher.StartWatchServiceRequest; +import org.elasticsearch.client.watcher.StopWatchServiceRequest; import org.elasticsearch.client.watcher.WatcherStatsRequest; import org.elasticsearch.client.watcher.WatcherStatsResponse; @@ -59,7 +59,12 @@ public final class WatcherClient { */ public AcknowledgedResponse startWatchService(StartWatchServiceRequest request, RequestOptions options) throws IOException { return restHighLevelClient.performRequestAndParseEntity( - request, WatcherRequestConverters::startWatchService, options, AcknowledgedResponse::fromXContent, emptySet()); + request, + WatcherRequestConverters::startWatchService, + options, + AcknowledgedResponse::fromXContent, + emptySet() + ); } /** @@ -69,10 +74,19 @@ public AcknowledgedResponse startWatchService(StartWatchServiceRequest request, * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized * @return cancellable that may be used to cancel the request */ - public Cancellable startWatchServiceAsync(StartWatchServiceRequest request, RequestOptions options, - ActionListener listener) { + public Cancellable startWatchServiceAsync( + StartWatchServiceRequest request, + RequestOptions options, + ActionListener listener + ) { return restHighLevelClient.performRequestAsyncAndParseEntity( - request, WatcherRequestConverters::startWatchService, options, AcknowledgedResponse::fromXContent, listener, emptySet()); + request, + WatcherRequestConverters::startWatchService, + options, + AcknowledgedResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -86,7 +100,12 @@ public Cancellable startWatchServiceAsync(StartWatchServiceRequest request, Requ */ public AcknowledgedResponse stopWatchService(StopWatchServiceRequest request, RequestOptions options) throws IOException { return restHighLevelClient.performRequestAndParseEntity( - request, WatcherRequestConverters::stopWatchService, options, AcknowledgedResponse::fromXContent, emptySet()); + request, + WatcherRequestConverters::stopWatchService, + options, + AcknowledgedResponse::fromXContent, + emptySet() + ); } /** @@ -97,10 +116,19 @@ public AcknowledgedResponse stopWatchService(StopWatchServiceRequest request, Re * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized * @return cancellable that may be used to cancel the request */ - public Cancellable stopWatchServiceAsync(StopWatchServiceRequest request, RequestOptions options, - ActionListener listener) { + public Cancellable stopWatchServiceAsync( + StopWatchServiceRequest request, + RequestOptions options, + ActionListener listener + ) { return restHighLevelClient.performRequestAsyncAndParseEntity( - request, WatcherRequestConverters::stopWatchService, options, AcknowledgedResponse::fromXContent, listener, emptySet()); + request, + WatcherRequestConverters::stopWatchService, + options, + AcknowledgedResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -113,8 +141,13 @@ public Cancellable stopWatchServiceAsync(StopWatchServiceRequest request, Reques * @throws IOException in case there is a problem sending the request or parsing back the response */ public PutWatchResponse putWatch(PutWatchRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, WatcherRequestConverters::putWatch, options, - PutWatchResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + WatcherRequestConverters::putWatch, + options, + PutWatchResponse::fromXContent, + emptySet() + ); } /** @@ -126,10 +159,15 @@ public PutWatchResponse putWatch(PutWatchRequest request, RequestOptions options * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable putWatchAsync(PutWatchRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, WatcherRequestConverters::putWatch, options, - PutWatchResponse::fromXContent, listener, emptySet()); + public Cancellable putWatchAsync(PutWatchRequest request, RequestOptions options, ActionListener listener) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + WatcherRequestConverters::putWatch, + options, + PutWatchResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -142,8 +180,13 @@ public Cancellable putWatchAsync(PutWatchRequest request, RequestOptions options * @throws IOException in case there is a problem sending the request or parsing back the response */ public GetWatchResponse getWatch(GetWatchRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, WatcherRequestConverters::getWatch, options, - GetWatchResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + WatcherRequestConverters::getWatch, + options, + GetWatchResponse::fromXContent, + emptySet() + ); } /** @@ -155,10 +198,15 @@ public GetWatchResponse getWatch(GetWatchRequest request, RequestOptions options * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable getWatchAsync(GetWatchRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, WatcherRequestConverters::getWatch, options, - GetWatchResponse::fromXContent, listener, emptySet()); + public Cancellable getWatchAsync(GetWatchRequest request, RequestOptions options, ActionListener listener) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + WatcherRequestConverters::getWatch, + options, + GetWatchResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -171,8 +219,13 @@ public Cancellable getWatchAsync(GetWatchRequest request, RequestOptions options * @throws IOException in case there is a problem sending the request or parsing back the response */ public DeactivateWatchResponse deactivateWatch(DeactivateWatchRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, WatcherRequestConverters::deactivateWatch, options, - DeactivateWatchResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + WatcherRequestConverters::deactivateWatch, + options, + DeactivateWatchResponse::fromXContent, + emptySet() + ); } /** @@ -185,10 +238,19 @@ public DeactivateWatchResponse deactivateWatch(DeactivateWatchRequest request, R * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable deactivateWatchAsync(DeactivateWatchRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, WatcherRequestConverters::deactivateWatch, options, - DeactivateWatchResponse::fromXContent, listener, emptySet()); + public Cancellable deactivateWatchAsync( + DeactivateWatchRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + WatcherRequestConverters::deactivateWatch, + options, + DeactivateWatchResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -201,8 +263,13 @@ public Cancellable deactivateWatchAsync(DeactivateWatchRequest request, RequestO * @throws IOException in case there is a problem sending the request or parsing back the response */ public DeleteWatchResponse deleteWatch(DeleteWatchRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, WatcherRequestConverters::deleteWatch, options, - DeleteWatchResponse::fromXContent, singleton(404)); + return restHighLevelClient.performRequestAndParseEntity( + request, + WatcherRequestConverters::deleteWatch, + options, + DeleteWatchResponse::fromXContent, + singleton(404) + ); } /** @@ -215,8 +282,14 @@ public DeleteWatchResponse deleteWatch(DeleteWatchRequest request, RequestOption * @return cancellable that may be used to cancel the request */ public Cancellable deleteWatchAsync(DeleteWatchRequest request, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, WatcherRequestConverters::deleteWatch, options, - DeleteWatchResponse::fromXContent, listener, singleton(404)); + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + WatcherRequestConverters::deleteWatch, + options, + DeleteWatchResponse::fromXContent, + listener, + singleton(404) + ); } /** @@ -229,8 +302,13 @@ public Cancellable deleteWatchAsync(DeleteWatchRequest request, RequestOptions o * @throws IOException if there is a problem sending the request or parsing back the response */ public AckWatchResponse ackWatch(AckWatchRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, WatcherRequestConverters::ackWatch, options, - AckWatchResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + WatcherRequestConverters::ackWatch, + options, + AckWatchResponse::fromXContent, + emptySet() + ); } /** @@ -243,8 +321,14 @@ public AckWatchResponse ackWatch(AckWatchRequest request, RequestOptions options * @return cancellable that may be used to cancel the request */ public Cancellable ackWatchAsync(AckWatchRequest request, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, WatcherRequestConverters::ackWatch, options, - AckWatchResponse::fromXContent, listener, emptySet()); + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + WatcherRequestConverters::ackWatch, + options, + AckWatchResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -257,8 +341,13 @@ public Cancellable ackWatchAsync(AckWatchRequest request, RequestOptions options * @throws IOException in case there is a problem sending the request or parsing back the response */ public ActivateWatchResponse activateWatch(ActivateWatchRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, WatcherRequestConverters::activateWatch, options, - ActivateWatchResponse::fromXContent, singleton(404)); + return restHighLevelClient.performRequestAndParseEntity( + request, + WatcherRequestConverters::activateWatch, + options, + ActivateWatchResponse::fromXContent, + singleton(404) + ); } /** @@ -270,10 +359,19 @@ public ActivateWatchResponse activateWatch(ActivateWatchRequest request, Request * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable activateWatchAsync(ActivateWatchRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, WatcherRequestConverters::activateWatch, options, - ActivateWatchResponse::fromXContent, listener, singleton(404)); + public Cancellable activateWatchAsync( + ActivateWatchRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + WatcherRequestConverters::activateWatch, + options, + ActivateWatchResponse::fromXContent, + listener, + singleton(404) + ); } /** @@ -286,8 +384,13 @@ public Cancellable activateWatchAsync(ActivateWatchRequest request, RequestOptio * @throws IOException if there is a problem sending the request or parsing the response */ public ExecuteWatchResponse executeWatch(ExecuteWatchRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, WatcherRequestConverters::executeWatch, options, - ExecuteWatchResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + WatcherRequestConverters::executeWatch, + options, + ExecuteWatchResponse::fromXContent, + emptySet() + ); } /** @@ -299,10 +402,19 @@ public ExecuteWatchResponse executeWatch(ExecuteWatchRequest request, RequestOpt * @param listener the listener to be notifed upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable executeWatchAsync(ExecuteWatchRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, WatcherRequestConverters::executeWatch, options, - ExecuteWatchResponse::fromXContent, listener, emptySet()); + public Cancellable executeWatchAsync( + ExecuteWatchRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + WatcherRequestConverters::executeWatch, + options, + ExecuteWatchResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -315,8 +427,13 @@ public Cancellable executeWatchAsync(ExecuteWatchRequest request, RequestOptions * @throws IOException in case there is a problem sending the request or parsing back the response */ public WatcherStatsResponse watcherStats(WatcherStatsRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, WatcherRequestConverters::watcherStats, options, - WatcherStatsResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + WatcherRequestConverters::watcherStats, + options, + WatcherStatsResponse::fromXContent, + emptySet() + ); } /** @@ -328,10 +445,19 @@ public WatcherStatsResponse watcherStats(WatcherStatsRequest request, RequestOpt * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable watcherStatsAsync(WatcherStatsRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, WatcherRequestConverters::watcherStats, options, - WatcherStatsResponse::fromXContent, listener, emptySet()); + public Cancellable watcherStatsAsync( + WatcherStatsRequest request, + RequestOptions options, + ActionListener listener + ) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + WatcherRequestConverters::watcherStats, + options, + WatcherStatsResponse::fromXContent, + listener, + emptySet() + ); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/WatcherRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/WatcherRequestConverters.java index 1d4bfcac23a0b..456bf8b0d3fda 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/WatcherRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/WatcherRequestConverters.java @@ -34,32 +34,24 @@ final class WatcherRequestConverters { private WatcherRequestConverters() {} static Request startWatchService(StartWatchServiceRequest startWatchServiceRequest) { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_watcher") - .addPathPartAsIs("_start") - .build(); + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_watcher").addPathPartAsIs("_start").build(); return new Request(HttpPost.METHOD_NAME, endpoint); } static Request stopWatchService(StopWatchServiceRequest stopWatchServiceRequest) { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_watcher") - .addPathPartAsIs("_stop") - .build(); + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_watcher").addPathPartAsIs("_stop").build(); return new Request(HttpPost.METHOD_NAME, endpoint); } static Request putWatch(PutWatchRequest putWatchRequest) { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_watcher", "watch") + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_watcher", "watch") .addPathPart(putWatchRequest.getId()) .build(); Request request = new Request(HttpPut.METHOD_NAME, endpoint); - RequestConverters.Params params = new RequestConverters.Params() - .withIfSeqNo(putWatchRequest.ifSeqNo()) + RequestConverters.Params params = new RequestConverters.Params().withIfSeqNo(putWatchRequest.ifSeqNo()) .withIfPrimaryTerm(putWatchRequest.ifPrimaryTerm()); if (putWatchRequest.isActive() == false) { params.putParam("active", "false"); @@ -71,10 +63,8 @@ static Request putWatch(PutWatchRequest putWatchRequest) { return request; } - static Request getWatch(GetWatchRequest getWatchRequest) { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_watcher", "watch") + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_watcher", "watch") .addPathPart(getWatchRequest.getId()) .build(); @@ -82,8 +72,7 @@ static Request getWatch(GetWatchRequest getWatchRequest) { } static Request deactivateWatch(DeactivateWatchRequest deactivateWatchRequest) { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_watcher") + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_watcher") .addPathPartAsIs("watch") .addPathPart(deactivateWatchRequest.getWatchId()) .addPathPartAsIs("_deactivate") @@ -92,8 +81,7 @@ static Request deactivateWatch(DeactivateWatchRequest deactivateWatchRequest) { } static Request deleteWatch(DeleteWatchRequest deleteWatchRequest) { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_watcher", "watch") + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_watcher", "watch") .addPathPart(deleteWatchRequest.getId()) .build(); @@ -102,10 +90,10 @@ static Request deleteWatch(DeleteWatchRequest deleteWatchRequest) { } static Request executeWatch(ExecuteWatchRequest executeWatchRequest) throws IOException { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_watcher", "watch") + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_watcher", "watch") .addPathPart(executeWatchRequest.getId()) // will ignore if ID is null - .addPathPartAsIs("_execute").build(); + .addPathPartAsIs("_execute") + .build(); Request request = new Request(HttpPost.METHOD_NAME, endpoint); RequestConverters.Params params = new RequestConverters.Params(); @@ -124,8 +112,7 @@ static Request executeWatch(ExecuteWatchRequest executeWatchRequest) throws IOEx } public static Request ackWatch(AckWatchRequest ackWatchRequest) { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_watcher", "watch") + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_watcher", "watch") .addPathPart(ackWatchRequest.getWatchId()) .addPathPartAsIs("_ack") .addCommaSeparatedPathParts(ackWatchRequest.getActionIds()) @@ -135,8 +122,7 @@ public static Request ackWatch(AckWatchRequest ackWatchRequest) { } static Request activateWatch(ActivateWatchRequest activateWatchRequest) { - String endpoint = new RequestConverters.EndpointBuilder() - .addPathPartAsIs("_watcher", "watch") + String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_watcher", "watch") .addPathPart(activateWatchRequest.getWatchId()) .addPathPartAsIs("_activate") .build(); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/XPackClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/XPackClient.java index 9767e1cba944f..f019a262b607a 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/XPackClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/XPackClient.java @@ -51,8 +51,13 @@ public final class XPackClient { * @throws IOException in case there is a problem sending the request or parsing back the response */ public XPackInfoResponse info(XPackInfoRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, XPackRequestConverters::info, options, - XPackInfoResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + XPackRequestConverters::info, + options, + XPackInfoResponse::fromXContent, + emptySet() + ); } /** @@ -64,10 +69,15 @@ public XPackInfoResponse info(XPackInfoRequest request, RequestOptions options) * @param listener the listener to be notified upon request completion * @return cancellable that may be used to cancel the request */ - public Cancellable infoAsync(XPackInfoRequest request, RequestOptions options, - ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, XPackRequestConverters::info, options, - XPackInfoResponse::fromXContent, listener, emptySet()); + public Cancellable infoAsync(XPackInfoRequest request, RequestOptions options, ActionListener listener) { + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + XPackRequestConverters::info, + options, + XPackInfoResponse::fromXContent, + listener, + emptySet() + ); } /** @@ -77,8 +87,13 @@ public Cancellable infoAsync(XPackInfoRequest request, RequestOptions options, * @throws IOException in case there is a problem sending the request or parsing back the response */ public XPackUsageResponse usage(XPackUsageRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, XPackRequestConverters::usage, options, - XPackUsageResponse::fromXContent, emptySet()); + return restHighLevelClient.performRequestAndParseEntity( + request, + XPackRequestConverters::usage, + options, + XPackUsageResponse::fromXContent, + emptySet() + ); } /** @@ -88,7 +103,13 @@ public XPackUsageResponse usage(XPackUsageRequest request, RequestOptions option * @return cancellable that may be used to cancel the request */ public Cancellable usageAsync(XPackUsageRequest request, RequestOptions options, ActionListener listener) { - return restHighLevelClient.performRequestAsyncAndParseEntity(request, XPackRequestConverters::usage, options, - XPackUsageResponse::fromXContent, listener, emptySet()); + return restHighLevelClient.performRequestAsyncAndParseEntity( + request, + XPackRequestConverters::usage, + options, + XPackUsageResponse::fromXContent, + listener, + emptySet() + ); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/XPackRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/XPackRequestConverters.java index 7ce2fe2512f33..d5605d2211fcf 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/XPackRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/XPackRequestConverters.java @@ -26,9 +26,10 @@ static Request info(XPackInfoRequest infoRequest) { request.addParameter("human", "false"); } if (false == infoRequest.getCategories().equals(EnumSet.allOf(XPackInfoRequest.Category.class))) { - request.addParameter("categories", infoRequest.getCategories().stream() - .map(c -> c.toString().toLowerCase(Locale.ROOT)) - .collect(Collectors.joining(","))); + request.addParameter( + "categories", + infoRequest.getCategories().stream().map(c -> c.toString().toLowerCase(Locale.ROOT)).collect(Collectors.joining(",")) + ); } return request; } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/analytics/InferencePipelineAggregationBuilder.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/analytics/InferencePipelineAggregationBuilder.java index efec2ffa8cd0a..47e1eb00bbd7f 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/analytics/InferencePipelineAggregationBuilder.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/analytics/InferencePipelineAggregationBuilder.java @@ -9,15 +9,15 @@ package org.elasticsearch.client.analytics; import org.elasticsearch.client.ml.inference.trainedmodel.InferenceConfig; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.xcontent.ConstructingObjectParser; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.aggregations.PipelineAggregationBuilder; import org.elasticsearch.search.aggregations.pipeline.AbstractPipelineAggregationBuilder; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; import org.elasticsearch.search.builder.SearchSourceBuilder; +import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.Map; @@ -41,26 +41,28 @@ public class InferencePipelineAggregationBuilder extends AbstractPipelineAggrega public static final ParseField MODEL_ID = new ParseField("model_id"); private static final ParseField INFERENCE_CONFIG = new ParseField("inference_config"); - @SuppressWarnings("unchecked") private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - NAME, false, - (args, name) -> new InferencePipelineAggregationBuilder(name, (String)args[0], (Map) args[1]) + NAME, + false, + (args, name) -> new InferencePipelineAggregationBuilder(name, (String) args[0], (Map) args[1]) ); static { PARSER.declareString(constructorArg(), MODEL_ID); PARSER.declareObject(constructorArg(), (p, c) -> p.mapStrings(), BUCKETS_PATH_FIELD); - PARSER.declareNamedObject(InferencePipelineAggregationBuilder::setInferenceConfig, - (p, c, n) -> p.namedObject(InferenceConfig.class, n, c), INFERENCE_CONFIG); + PARSER.declareNamedObject( + InferencePipelineAggregationBuilder::setInferenceConfig, + (p, c, n) -> p.namedObject(InferenceConfig.class, n, c), + INFERENCE_CONFIG + ); } private final Map bucketPathMap; private final String modelId; private InferenceConfig inferenceConfig; - public static InferencePipelineAggregationBuilder parse(String pipelineAggregatorName, - XContentParser parser) { + public static InferencePipelineAggregationBuilder parse(String pipelineAggregatorName, XContentParser parser) { return PARSER.apply(parser, pipelineAggregatorName); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/analytics/ParsedInference.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/analytics/ParsedInference.java index 136b5db14dcad..d3e1fcd5c85f0 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/analytics/ParsedInference.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/analytics/ParsedInference.java @@ -10,13 +10,13 @@ import org.elasticsearch.client.ml.inference.results.FeatureImportance; import org.elasticsearch.client.ml.inference.results.TopClassEntry; -import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.search.aggregations.ParsedAggregation; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParseException; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.search.aggregations.ParsedAggregation; import java.io.IOException; import java.util.List; @@ -34,10 +34,11 @@ public class ParsedInference extends ParsedAggregation { @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(ParsedInference.class.getSimpleName(), true, - args -> new ParsedInference(args[0], (List) args[1], - (List) args[2], (String) args[3])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + ParsedInference.class.getSimpleName(), + true, + args -> new ParsedInference(args[0], (List) args[1], (List) args[2], (String) args[3]) + ); public static final ParseField FEATURE_IMPORTANCE = new ParseField("feature_importance"); public static final ParseField WARNING = new ParseField("warning"); @@ -54,9 +55,17 @@ public class ParsedInference extends ParsedAggregation { } else if (token == XContentParser.Token.VALUE_NUMBER) { o = p.doubleValue(); } else { - throw new XContentParseException(p.getTokenLocation(), - "[" + ParsedInference.class.getSimpleName() + "] failed to parse field [" + CommonFields.VALUE + "] " - + "value [" + token + "] is not a string, boolean or number"); + throw new XContentParseException( + p.getTokenLocation(), + "[" + + ParsedInference.class.getSimpleName() + + "] failed to parse field [" + + CommonFields.VALUE + + "] " + + "value [" + + token + + "] is not a string, boolean or number" + ); } return o; }, CommonFields.VALUE, ObjectParser.ValueType.VALUE); @@ -77,10 +86,7 @@ public static ParsedInference fromXContent(XContentParser parser, final String n private final List topClasses; private final String warning; - ParsedInference(Object value, - List featureImportance, - List topClasses, - String warning) { + ParsedInference(Object value, List featureImportance, List topClasses, String warning) { this.value = value; this.warning = warning; this.featureImportance = featureImportance; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/analytics/ParsedStringStats.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/analytics/ParsedStringStats.java index 56600fc3ace18..ab059682460f2 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/analytics/ParsedStringStats.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/analytics/ParsedStringStats.java @@ -8,11 +8,11 @@ package org.elasticsearch.client.analytics; -import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.search.aggregations.ParsedAggregation; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.search.aggregations.ParsedAggregation; import java.io.IOException; import java.util.HashMap; @@ -41,8 +41,16 @@ public class ParsedStringStats extends ParsedAggregation { private final boolean showDistribution; private final Map distribution; - private ParsedStringStats(String name, long count, int minLength, int maxLength, double avgLength, double entropy, - boolean showDistribution, Map distribution) { + private ParsedStringStats( + String name, + long count, + int minLength, + int maxLength, + double avgLength, + double entropy, + boolean showDistribution, + Map distribution + ) { setName(name); this.count = count; this.minLength = minLength; @@ -108,34 +116,39 @@ public String getType() { private static final Object NULL_DISTRIBUTION_MARKER = new Object(); public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - StringStatsAggregationBuilder.NAME, true, (args, name) -> { - long count = (long) args[0]; - boolean disributionWasExplicitNull = args[5] == NULL_DISTRIBUTION_MARKER; - if (count == 0) { - return new ParsedStringStats(name, count, 0, 0, 0, 0, disributionWasExplicitNull, null); - } - int minLength = (int) args[1]; - int maxLength = (int) args[2]; - double averageLength = (double) args[3]; - double entropy = (double) args[4]; - if (disributionWasExplicitNull) { - return new ParsedStringStats(name, count, minLength, maxLength, averageLength, entropy, - disributionWasExplicitNull, null); - } else { - @SuppressWarnings("unchecked") - Map distribution = (Map) args[5]; - return new ParsedStringStats(name, count, minLength, maxLength, averageLength, entropy, - distribution != null, distribution); - } - }); + StringStatsAggregationBuilder.NAME, + true, + (args, name) -> { + long count = (long) args[0]; + boolean disributionWasExplicitNull = args[5] == NULL_DISTRIBUTION_MARKER; + if (count == 0) { + return new ParsedStringStats(name, count, 0, 0, 0, 0, disributionWasExplicitNull, null); + } + int minLength = (int) args[1]; + int maxLength = (int) args[2]; + double averageLength = (double) args[3]; + double entropy = (double) args[4]; + if (disributionWasExplicitNull) { + return new ParsedStringStats(name, count, minLength, maxLength, averageLength, entropy, disributionWasExplicitNull, null); + } else { + @SuppressWarnings("unchecked") + Map distribution = (Map) args[5]; + return new ParsedStringStats(name, count, minLength, maxLength, averageLength, entropy, distribution != null, distribution); + } + } + ); static { PARSER.declareLong(constructorArg(), COUNT_FIELD); PARSER.declareIntOrNull(constructorArg(), 0, MIN_LENGTH_FIELD); PARSER.declareIntOrNull(constructorArg(), 0, MAX_LENGTH_FIELD); PARSER.declareDoubleOrNull(constructorArg(), 0, AVG_LENGTH_FIELD); PARSER.declareDoubleOrNull(constructorArg(), 0, ENTROPY_FIELD); - PARSER.declareObjectOrNull(optionalConstructorArg(), (p, c) -> unmodifiableMap(p.map(HashMap::new, XContentParser::doubleValue)), - NULL_DISTRIBUTION_MARKER, DISTRIBUTION_FIELD); + PARSER.declareObjectOrNull( + optionalConstructorArg(), + (p, c) -> unmodifiableMap(p.map(HashMap::new, XContentParser::doubleValue)), + NULL_DISTRIBUTION_MARKER, + DISTRIBUTION_FIELD + ); ParsedAggregation.declareAggregationFields(PARSER); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/analytics/ParsedTopMetrics.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/analytics/ParsedTopMetrics.java index 232a8c1eab5d9..994c3411ce081 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/analytics/ParsedTopMetrics.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/analytics/ParsedTopMetrics.java @@ -8,13 +8,13 @@ package org.elasticsearch.client.analytics; -import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.common.xcontent.XContentParserUtils; +import org.elasticsearch.search.aggregations.ParsedAggregation; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentParserUtils; -import org.elasticsearch.search.aggregations.ParsedAggregation; import java.io.IOException; import java.util.List; @@ -57,11 +57,14 @@ protected XContentBuilder doXContentBody(XContentBuilder builder, Params params) } public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - TopMetricsAggregationBuilder.NAME, true, (args, name) -> { - @SuppressWarnings("unchecked") - List topMetrics = (List) args[0]; - return new ParsedTopMetrics(name, topMetrics); - }); + TopMetricsAggregationBuilder.NAME, + true, + (args, name) -> { + @SuppressWarnings("unchecked") + List topMetrics = (List) args[0]; + return new ParsedTopMetrics(name, topMetrics); + } + ); static { PARSER.declareObjectArray(constructorArg(), (p, c) -> TopMetrics.PARSER.parse(p, null), TOP_FIELD); ParsedAggregation.declareAggregationFields(PARSER); @@ -96,17 +99,24 @@ public Map getMetrics() { return metrics; } - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("top", true, - (args, name) -> { - @SuppressWarnings("unchecked") - List sort = (List) args[0]; - @SuppressWarnings("unchecked") - Map metrics = (Map) args[1]; - return new TopMetrics(sort, metrics); - }); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "top", + true, + (args, name) -> { + @SuppressWarnings("unchecked") + List sort = (List) args[0]; + @SuppressWarnings("unchecked") + Map metrics = (Map) args[1]; + return new TopMetrics(sort, metrics); + } + ); static { - PARSER.declareFieldArray(constructorArg(), (p, c) -> XContentParserUtils.parseFieldsValue(p), - SORT_FIELD, ObjectParser.ValueType.VALUE_ARRAY); + PARSER.declareFieldArray( + constructorArg(), + (p, c) -> XContentParserUtils.parseFieldsValue(p), + SORT_FIELD, + ObjectParser.ValueType.VALUE_ARRAY + ); PARSER.declareObject(constructorArg(), (p, c) -> p.map(), METRICS_FIELD); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/analytics/StringStatsAggregationBuilder.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/analytics/StringStatsAggregationBuilder.java index f36bf2f5a3f7c..a60992c449918 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/analytics/StringStatsAggregationBuilder.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/analytics/StringStatsAggregationBuilder.java @@ -8,10 +8,8 @@ package org.elasticsearch.client.analytics; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.index.query.QueryRewriteContext; import org.elasticsearch.search.aggregations.AbstractAggregationBuilder; import org.elasticsearch.search.aggregations.AggregationBuilder; @@ -25,6 +23,8 @@ import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry; import org.elasticsearch.search.aggregations.support.ValuesSourceType; import org.elasticsearch.search.builder.SearchSourceBuilder; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.Map; @@ -71,7 +71,7 @@ public String getType() { @Override protected ValuesSourceRegistry.RegistryKey getRegistryKey() { - // This would be called from the same thing that calls innerBuild, which also throws. So it's "safe" to throw here. + // This would be called from the same thing that calls innerBuild, which also throws. So it's "safe" to throw here. throw new UnsupportedOperationException(); } @@ -91,8 +91,12 @@ public BucketCardinality bucketCardinality() { } @Override - protected ValuesSourceAggregatorFactory innerBuild(AggregationContext context, ValuesSourceConfig config, - AggregatorFactory parent, Builder subFactoriesBuilder) throws IOException { + protected ValuesSourceAggregatorFactory innerBuild( + AggregationContext context, + ValuesSourceConfig config, + AggregatorFactory parent, + Builder subFactoriesBuilder + ) throws IOException { throw new UnsupportedOperationException(); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/analytics/TopMetricsAggregationBuilder.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/analytics/TopMetricsAggregationBuilder.java index fd987adc4def2..891803b2f32dd 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/analytics/TopMetricsAggregationBuilder.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/analytics/TopMetricsAggregationBuilder.java @@ -10,7 +10,6 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.index.query.QueryRewriteContext; import org.elasticsearch.search.aggregations.AbstractAggregationBuilder; import org.elasticsearch.search.aggregations.AggregationBuilder; @@ -19,6 +18,7 @@ import org.elasticsearch.search.aggregations.support.AggregationContext; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.sort.SortBuilder; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.Arrays; @@ -70,7 +70,7 @@ protected XContentBuilder internalXContent(XContentBuilder builder, Params param builder.endArray(); builder.field("size", size); builder.startArray("metrics"); - for (String metric: metrics) { + for (String metric : metrics) { builder.startObject().field("field", metric).endObject(); } builder.endArray(); @@ -90,7 +90,7 @@ public BucketCardinality bucketCardinality() { @Override protected AggregatorFactory doBuild(AggregationContext context, AggregatorFactory parent, Builder subfactoriesBuilder) - throws IOException { + throws IOException { throw new UnsupportedOperationException(); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/asyncsearch/AsyncSearchResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/asyncsearch/AsyncSearchResponse.java index 3674a2112f750..b3788eb6c9fe2 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/asyncsearch/AsyncSearchResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/asyncsearch/AsyncSearchResponse.java @@ -9,10 +9,10 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.action.search.SearchResponse; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -20,14 +20,14 @@ import java.io.IOException; +import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken; import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg; import static org.elasticsearch.xcontent.ConstructingObjectParser.optionalConstructorArg; -import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken; /** * A response of an async search request. */ -public class AsyncSearchResponse implements ToXContentObject { +public class AsyncSearchResponse implements ToXContentObject { @Nullable private final String id; @Nullable @@ -43,13 +43,15 @@ public class AsyncSearchResponse implements ToXContentObject { /** * Creates an {@link AsyncSearchResponse} with the arguments that are always present in the server response */ - AsyncSearchResponse(boolean isPartial, - boolean isRunning, - long startTimeMillis, - long expirationTimeMillis, - @Nullable String id, - @Nullable SearchResponse searchResponse, - @Nullable ElasticsearchException error) { + AsyncSearchResponse( + boolean isPartial, + boolean isRunning, + long startTimeMillis, + long expirationTimeMillis, + @Nullable String id, + @Nullable SearchResponse searchResponse, + @Nullable ElasticsearchException error + ) { this.isPartial = isPartial; this.isRunning = isRunning; this.startTimeMillis = startTimeMillis; @@ -151,23 +153,25 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws public static final ParseField ERROR_FIELD = new ParseField("error"); public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "submit_async_search_response", true, - args -> new AsyncSearchResponse( - (boolean) args[0], - (boolean) args[1], - (long) args[2], - (long) args[3], - (String) args[4], - (SearchResponse) args[5], - (ElasticsearchException) args[6])); + "submit_async_search_response", + true, + args -> new AsyncSearchResponse( + (boolean) args[0], + (boolean) args[1], + (long) args[2], + (long) args[3], + (String) args[4], + (SearchResponse) args[5], + (ElasticsearchException) args[6] + ) + ); static { PARSER.declareBoolean(constructorArg(), IS_PARTIAL_FIELD); PARSER.declareBoolean(constructorArg(), IS_RUNNING_FIELD); PARSER.declareLong(constructorArg(), START_TIME_FIELD); PARSER.declareLong(constructorArg(), EXPIRATION_FIELD); PARSER.declareString(optionalConstructorArg(), ID_FIELD); - PARSER.declareObject(optionalConstructorArg(), (p, c) -> AsyncSearchResponse.parseSearchResponse(p), - RESPONSE_FIELD); + PARSER.declareObject(optionalConstructorArg(), (p, c) -> AsyncSearchResponse.parseSearchResponse(p), RESPONSE_FIELD); PARSER.declareObject(optionalConstructorArg(), (p, c) -> ElasticsearchException.fromXContent(p), ERROR_FIELD); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/asyncsearch/DeleteAsyncSearchRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/asyncsearch/DeleteAsyncSearchRequest.java index 70de7156a0b7d..0f7e7579e0b5b 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/asyncsearch/DeleteAsyncSearchRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/asyncsearch/DeleteAsyncSearchRequest.java @@ -6,7 +6,6 @@ * Side Public License, v 1. */ - package org.elasticsearch.client.asyncsearch; import org.elasticsearch.client.Validatable; @@ -19,7 +18,7 @@ public class DeleteAsyncSearchRequest implements Validatable { public DeleteAsyncSearchRequest(String id) { this.id = id; -} + } public String getId() { return this.id; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/asyncsearch/GetAsyncSearchRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/asyncsearch/GetAsyncSearchRequest.java index c128f5b5bbd91..d6c40dbe1a567 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/asyncsearch/GetAsyncSearchRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/asyncsearch/GetAsyncSearchRequest.java @@ -6,7 +6,6 @@ * Side Public License, v 1. */ - package org.elasticsearch.client.asyncsearch; import org.elasticsearch.client.Validatable; @@ -62,8 +61,8 @@ public boolean equals(Object o) { } GetAsyncSearchRequest request = (GetAsyncSearchRequest) o; return Objects.equals(getId(), request.getId()) - && Objects.equals(getKeepAlive(), request.getKeepAlive()) - && Objects.equals(getWaitForCompletion(), request.getWaitForCompletion()); + && Objects.equals(getKeepAlive(), request.getKeepAlive()) + && Objects.equals(getWaitForCompletion(), request.getWaitForCompletion()); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/asyncsearch/SubmitAsyncSearchRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/asyncsearch/SubmitAsyncSearchRequest.java index a54aa53ec79ee..9c381645d6ff4 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/asyncsearch/SubmitAsyncSearchRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/asyncsearch/SubmitAsyncSearchRequest.java @@ -6,7 +6,6 @@ * Side Public License, v 1. */ - package org.elasticsearch.client.asyncsearch; import org.elasticsearch.action.search.SearchRequest; @@ -48,7 +47,6 @@ public String[] getIndices() { return this.searchRequest.indices(); } - /** * Get the minimum time that the request should wait before returning a partial result (defaults to 1 second). */ @@ -255,9 +253,9 @@ public boolean equals(Object o) { } SubmitAsyncSearchRequest request = (SubmitAsyncSearchRequest) o; return Objects.equals(searchRequest, request.searchRequest) - && Objects.equals(getKeepAlive(), request.getKeepAlive()) - && Objects.equals(getWaitForCompletionTimeout(), request.getWaitForCompletionTimeout()) - && Objects.equals(isKeepOnCompletion(), request.isKeepOnCompletion()); + && Objects.equals(getKeepAlive(), request.getKeepAlive()) + && Objects.equals(getWaitForCompletionTimeout(), request.getWaitForCompletionTimeout()) + && Objects.equals(isKeepOnCompletion(), request.isKeepOnCompletion()); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/AutoFollowStats.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/AutoFollowStats.java index fcf6feadca0ba..34ab9f3b972a1 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/AutoFollowStats.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/AutoFollowStats.java @@ -10,9 +10,9 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.util.Maps; +import org.elasticsearch.core.Tuple; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.core.Tuple; import java.util.AbstractMap; import java.util.List; @@ -23,8 +23,9 @@ public final class AutoFollowStats { static final ParseField NUMBER_OF_SUCCESSFUL_INDICES_AUTO_FOLLOWED = new ParseField("number_of_successful_follow_indices"); static final ParseField NUMBER_OF_FAILED_INDICES_AUTO_FOLLOWED = new ParseField("number_of_failed_follow_indices"); - static final ParseField NUMBER_OF_FAILED_REMOTE_CLUSTER_STATE_REQUESTS = - new ParseField("number_of_failed_remote_cluster_state_requests"); + static final ParseField NUMBER_OF_FAILED_REMOTE_CLUSTER_STATE_REQUESTS = new ParseField( + "number_of_failed_remote_cluster_state_requests" + ); static final ParseField RECENT_AUTO_FOLLOW_ERRORS = new ParseField("recent_auto_follow_errors"); static final ParseField LEADER_INDEX = new ParseField("leader_index"); static final ParseField TIMESTAMP = new ParseField("timestamp"); @@ -42,25 +43,26 @@ public final class AutoFollowStats { (Long) args[0], (Long) args[1], (Long) args[2], - ((List>>) args[3]) - .stream() + ((List>>) args[3]).stream() .collect(Maps.toUnmodifiableSortedMap(Map.Entry::getKey, Map.Entry::getValue)), - ((List>) args[4]) - .stream() + ((List>) args[4]).stream() .collect(Maps.toUnmodifiableSortedMap(Map.Entry::getKey, Map.Entry::getValue)) - )); + ) + ); static final ConstructingObjectParser>, Void> AUTO_FOLLOW_EXCEPTIONS_PARSER = new ConstructingObjectParser<>( "auto_follow_stats_errors", true, - args -> new AbstractMap.SimpleEntry<>((String) args[0], Tuple.tuple((Long) args[1], (ElasticsearchException) args[2]))); + args -> new AbstractMap.SimpleEntry<>((String) args[0], Tuple.tuple((Long) args[1], (ElasticsearchException) args[2])) + ); private static final ConstructingObjectParser, Void> AUTO_FOLLOWED_CLUSTERS_PARSER = new ConstructingObjectParser<>( "auto_followed_clusters", true, - args -> new AbstractMap.SimpleEntry<>((String) args[0], new AutoFollowedCluster((Long) args[1], (Long) args[2]))); + args -> new AbstractMap.SimpleEntry<>((String) args[0], new AutoFollowedCluster((Long) args[1], (Long) args[2])) + ); static { AUTO_FOLLOW_EXCEPTIONS_PARSER.declareString(ConstructingObjectParser.constructorArg(), LEADER_INDEX); @@ -68,7 +70,8 @@ public final class AutoFollowStats { AUTO_FOLLOW_EXCEPTIONS_PARSER.declareObject( ConstructingObjectParser.constructorArg(), (p, c) -> ElasticsearchException.fromXContent(p), - AUTO_FOLLOW_EXCEPTION); + AUTO_FOLLOW_EXCEPTION + ); AUTO_FOLLOWED_CLUSTERS_PARSER.declareString(ConstructingObjectParser.constructorArg(), CLUSTER_NAME); AUTO_FOLLOWED_CLUSTERS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), TIME_SINCE_LAST_CHECK_MILLIS); @@ -77,10 +80,12 @@ public final class AutoFollowStats { STATS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), NUMBER_OF_FAILED_INDICES_AUTO_FOLLOWED); STATS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), NUMBER_OF_FAILED_REMOTE_CLUSTER_STATE_REQUESTS); STATS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), NUMBER_OF_SUCCESSFUL_INDICES_AUTO_FOLLOWED); - STATS_PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), AUTO_FOLLOW_EXCEPTIONS_PARSER, - RECENT_AUTO_FOLLOW_ERRORS); - STATS_PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), AUTO_FOLLOWED_CLUSTERS_PARSER, - AUTO_FOLLOWED_CLUSTERS); + STATS_PARSER.declareObjectArray( + ConstructingObjectParser.constructorArg(), + AUTO_FOLLOW_EXCEPTIONS_PARSER, + RECENT_AUTO_FOLLOW_ERRORS + ); + STATS_PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), AUTO_FOLLOWED_CLUSTERS_PARSER, AUTO_FOLLOWED_CLUSTERS); } private final long numberOfFailedFollowIndices; @@ -89,11 +94,13 @@ public final class AutoFollowStats { private final NavigableMap> recentAutoFollowErrors; private final NavigableMap autoFollowedClusters; - AutoFollowStats(long numberOfFailedFollowIndices, - long numberOfFailedRemoteClusterStateRequests, - long numberOfSuccessfulFollowIndices, - NavigableMap> recentAutoFollowErrors, - NavigableMap autoFollowedClusters) { + AutoFollowStats( + long numberOfFailedFollowIndices, + long numberOfFailedRemoteClusterStateRequests, + long numberOfSuccessfulFollowIndices, + NavigableMap> recentAutoFollowErrors, + NavigableMap autoFollowedClusters + ) { this.numberOfFailedFollowIndices = numberOfFailedFollowIndices; this.numberOfFailedRemoteClusterStateRequests = numberOfFailedRemoteClusterStateRequests; this.numberOfSuccessfulFollowIndices = numberOfSuccessfulFollowIndices; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/CcrStatsRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/CcrStatsRequest.java index 1b96ce186278d..1c82012211bfe 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/CcrStatsRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/CcrStatsRequest.java @@ -10,5 +10,4 @@ import org.elasticsearch.client.Validatable; -public final class CcrStatsRequest implements Validatable { -} +public final class CcrStatsRequest implements Validatable {} diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/CcrStatsResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/CcrStatsResponse.java index cf9121b808584..21105916043e3 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/CcrStatsResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/CcrStatsResponse.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ccr; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; public final class CcrStatsResponse { @@ -17,14 +17,11 @@ public final class CcrStatsResponse { static final ParseField AUTO_FOLLOW_STATS_FIELD = new ParseField("auto_follow_stats"); static final ParseField FOLLOW_STATS_FIELD = new ParseField("follow_stats"); - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "indices", - true, - args -> { - AutoFollowStats autoFollowStats = (AutoFollowStats) args[0]; - IndicesFollowStats indicesFollowStats = (IndicesFollowStats) args[1]; - return new CcrStatsResponse(autoFollowStats, indicesFollowStats); - }); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("indices", true, args -> { + AutoFollowStats autoFollowStats = (AutoFollowStats) args[0]; + IndicesFollowStats indicesFollowStats = (IndicesFollowStats) args[1]; + return new CcrStatsResponse(autoFollowStats, indicesFollowStats); + }); static { PARSER.declareObject(ConstructingObjectParser.constructorArg(), AutoFollowStats.STATS_PARSER, AUTO_FOLLOW_STATS_FIELD); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/FollowConfig.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/FollowConfig.java index d9a542f6f6da6..6c0c228ac40d8 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/FollowConfig.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/FollowConfig.java @@ -8,11 +8,11 @@ package org.elasticsearch.client.ccr; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -34,10 +34,7 @@ public class FollowConfig { static final ParseField MAX_RETRY_DELAY_FIELD = new ParseField("max_retry_delay"); static final ParseField READ_POLL_TIMEOUT = new ParseField("read_poll_timeout"); - private static final ObjectParser PARSER = new ObjectParser<>( - "follow_config", - true, - FollowConfig::new); + private static final ObjectParser PARSER = new ObjectParser<>("follow_config", true, FollowConfig::new); static { PARSER.declareObject(FollowConfig::setSettings, (p, c) -> Settings.fromXContent(p), SETTINGS); @@ -47,26 +44,35 @@ public class FollowConfig { FollowConfig::setMaxReadRequestSize, (p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), MAX_READ_REQUEST_SIZE.getPreferredName()), MAX_READ_REQUEST_SIZE, - ObjectParser.ValueType.STRING); + ObjectParser.ValueType.STRING + ); PARSER.declareInt(FollowConfig::setMaxWriteRequestOperationCount, MAX_WRITE_REQUEST_OPERATION_COUNT); PARSER.declareField( FollowConfig::setMaxWriteRequestSize, (p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), MAX_WRITE_REQUEST_SIZE.getPreferredName()), MAX_WRITE_REQUEST_SIZE, - ObjectParser.ValueType.STRING); + ObjectParser.ValueType.STRING + ); PARSER.declareInt(FollowConfig::setMaxOutstandingWriteRequests, MAX_OUTSTANDING_WRITE_REQUESTS); PARSER.declareInt(FollowConfig::setMaxWriteBufferCount, MAX_WRITE_BUFFER_COUNT); PARSER.declareField( FollowConfig::setMaxWriteBufferSize, (p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), MAX_WRITE_BUFFER_SIZE.getPreferredName()), MAX_WRITE_BUFFER_SIZE, - ObjectParser.ValueType.STRING); - PARSER.declareField(FollowConfig::setMaxRetryDelay, + ObjectParser.ValueType.STRING + ); + PARSER.declareField( + FollowConfig::setMaxRetryDelay, (p, c) -> TimeValue.parseTimeValue(p.text(), MAX_RETRY_DELAY_FIELD.getPreferredName()), - MAX_RETRY_DELAY_FIELD, ObjectParser.ValueType.STRING); - PARSER.declareField(FollowConfig::setReadPollTimeout, + MAX_RETRY_DELAY_FIELD, + ObjectParser.ValueType.STRING + ); + PARSER.declareField( + FollowConfig::setReadPollTimeout, (p, c) -> TimeValue.parseTimeValue(p.text(), READ_POLL_TIMEOUT.getPreferredName()), - READ_POLL_TIMEOUT, ObjectParser.ValueType.STRING); + READ_POLL_TIMEOUT, + ObjectParser.ValueType.STRING + ); } static FollowConfig fromXContent(XContentParser parser) { @@ -85,8 +91,7 @@ static FollowConfig fromXContent(XContentParser parser) { private TimeValue maxRetryDelay; private TimeValue readPollTimeout; - FollowConfig() { - } + FollowConfig() {} public Settings getSettings() { return settings; @@ -221,16 +226,16 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; FollowConfig that = (FollowConfig) o; - return Objects.equals(maxReadRequestOperationCount, that.maxReadRequestOperationCount) && - Objects.equals(maxOutstandingReadRequests, that.maxOutstandingReadRequests) && - Objects.equals(maxReadRequestSize, that.maxReadRequestSize) && - Objects.equals(maxWriteRequestOperationCount, that.maxWriteRequestOperationCount) && - Objects.equals(maxWriteRequestSize, that.maxWriteRequestSize) && - Objects.equals(maxOutstandingWriteRequests, that.maxOutstandingWriteRequests) && - Objects.equals(maxWriteBufferCount, that.maxWriteBufferCount) && - Objects.equals(maxWriteBufferSize, that.maxWriteBufferSize) && - Objects.equals(maxRetryDelay, that.maxRetryDelay) && - Objects.equals(readPollTimeout, that.readPollTimeout); + return Objects.equals(maxReadRequestOperationCount, that.maxReadRequestOperationCount) + && Objects.equals(maxOutstandingReadRequests, that.maxOutstandingReadRequests) + && Objects.equals(maxReadRequestSize, that.maxReadRequestSize) + && Objects.equals(maxWriteRequestOperationCount, that.maxWriteRequestOperationCount) + && Objects.equals(maxWriteRequestSize, that.maxWriteRequestSize) + && Objects.equals(maxOutstandingWriteRequests, that.maxOutstandingWriteRequests) + && Objects.equals(maxWriteBufferCount, that.maxWriteBufferCount) + && Objects.equals(maxWriteBufferSize, that.maxWriteBufferSize) + && Objects.equals(maxRetryDelay, that.maxRetryDelay) + && Objects.equals(readPollTimeout, that.readPollTimeout); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/FollowInfoResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/FollowInfoResponse.java index fc4c542c1504a..0d8fbfef0d73e 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/FollowInfoResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/FollowInfoResponse.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ccr; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.util.List; @@ -26,7 +26,8 @@ public final class FollowInfoResponse { @SuppressWarnings("unchecked") List infos = (List) args[0]; return new FollowInfoResponse(infos); - }); + } + ); static { PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), FollowerInfo.PARSER, FOLLOWER_INDICES_FIELD); @@ -71,17 +72,26 @@ public static final class FollowerInfo { "follower_info", true, args -> { - return new FollowerInfo((String) args[0], (String) args[1], (String) args[2], - Status.fromString((String) args[3]), (FollowConfig) args[4]); - }); + return new FollowerInfo( + (String) args[0], + (String) args[1], + (String) args[2], + Status.fromString((String) args[3]), + (FollowConfig) args[4] + ); + } + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), FOLLOWER_INDEX_FIELD); PARSER.declareString(ConstructingObjectParser.constructorArg(), REMOTE_CLUSTER_FIELD); PARSER.declareString(ConstructingObjectParser.constructorArg(), LEADER_INDEX_FIELD); PARSER.declareString(ConstructingObjectParser.constructorArg(), STATUS_FIELD); - PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), - (p, c) -> FollowConfig.fromXContent(p), PARAMETERS_FIELD); + PARSER.declareObject( + ConstructingObjectParser.optionalConstructorArg(), + (p, c) -> FollowConfig.fromXContent(p), + PARAMETERS_FIELD + ); } private final String followerIndex; @@ -90,8 +100,7 @@ public static final class FollowerInfo { private final Status status; private final FollowConfig parameters; - FollowerInfo(String followerIndex, String remoteCluster, String leaderIndex, Status status, - FollowConfig parameters) { + FollowerInfo(String followerIndex, String remoteCluster, String leaderIndex, Status status, FollowConfig parameters) { this.followerIndex = followerIndex; this.remoteCluster = remoteCluster; this.leaderIndex = leaderIndex; @@ -124,11 +133,11 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; FollowerInfo that = (FollowerInfo) o; - return Objects.equals(followerIndex, that.followerIndex) && - Objects.equals(remoteCluster, that.remoteCluster) && - Objects.equals(leaderIndex, that.leaderIndex) && - status == that.status && - Objects.equals(parameters, that.parameters); + return Objects.equals(followerIndex, that.followerIndex) + && Objects.equals(remoteCluster, that.remoteCluster) + && Objects.equals(leaderIndex, that.leaderIndex) + && status == that.status + && Objects.equals(parameters, that.parameters); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/ForgetFollowerRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/ForgetFollowerRequest.java index cc85d8ee0c3cc..2059648f150af 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/ForgetFollowerRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/ForgetFollowerRequest.java @@ -50,11 +50,12 @@ public String leaderIndex() { * @param leaderIndex the name of the leader index */ public ForgetFollowerRequest( - final String followerCluster, - final String followerIndex, - final String followerIndexUUID, - final String leaderRemoteCluster, - final String leaderIndex) { + final String followerCluster, + final String followerIndex, + final String followerIndexUUID, + final String leaderRemoteCluster, + final String leaderIndex + ) { this.followerCluster = Objects.requireNonNull(followerCluster); this.followerIndex = Objects.requireNonNull(followerIndex); this.followerIndexUUID = Objects.requireNonNull(followerIndexUUID); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/GetAutoFollowPatternResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/GetAutoFollowPatternResponse.java index 5078e45f1db5d..d705e39a5b789 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/GetAutoFollowPatternResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/GetAutoFollowPatternResponse.java @@ -11,11 +11,11 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.common.util.Maps; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.core.TimeValue; import java.util.AbstractMap; import java.util.Collections; @@ -31,7 +31,10 @@ public final class GetAutoFollowPatternResponse { static final ParseField PATTERN_FIELD = new ParseField("pattern"); private static final ConstructingObjectParser, Void> ENTRY_PARSER = new ConstructingObjectParser<>( - "get_auto_follow_pattern_response", true, args -> new AbstractMap.SimpleEntry<>((String) args[0], (Pattern) args[1])); + "get_auto_follow_pattern_response", + true, + args -> new AbstractMap.SimpleEntry<>((String) args[0], (Pattern) args[1]) + ); static { ENTRY_PARSER.declareString(ConstructingObjectParser.constructorArg(), NAME_FIELD); @@ -39,12 +42,16 @@ public final class GetAutoFollowPatternResponse { } private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "get_auto_follow_pattern_response", true, args -> { + "get_auto_follow_pattern_response", + true, + args -> { @SuppressWarnings("unchecked") List> entries = (List>) args[0]; - return new GetAutoFollowPatternResponse(entries.stream() - .collect(Maps.toUnmodifiableSortedMap(Map.Entry::getKey, Map.Entry::getValue))); - }); + return new GetAutoFollowPatternResponse( + entries.stream().collect(Maps.toUnmodifiableSortedMap(Map.Entry::getKey, Map.Entry::getValue)) + ); + } + ); static { PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), ENTRY_PARSER, PATTERNS_FIELD); @@ -83,17 +90,21 @@ public static class Pattern extends FollowConfig { private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( "pattern", true, - args -> new Pattern((String) args[0], - (List) args[1], - args[2] == null ? Collections.emptyList() : (List) args[2], - (String) args[3]) + args -> new Pattern( + (String) args[0], + (List) args[1], + args[2] == null ? Collections.emptyList() : (List) args[2], + (String) args[3] + ) ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), PutFollowRequest.REMOTE_CLUSTER_FIELD); PARSER.declareStringArray(ConstructingObjectParser.constructorArg(), PutAutoFollowPatternRequest.LEADER_PATTERNS_FIELD); - PARSER.declareStringArray(ConstructingObjectParser.optionalConstructorArg(), - PutAutoFollowPatternRequest.LEADER_EXCLUSION_PATTERNS_FIELD); + PARSER.declareStringArray( + ConstructingObjectParser.optionalConstructorArg(), + PutAutoFollowPatternRequest.LEADER_EXCLUSION_PATTERNS_FIELD + ); PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), PutAutoFollowPatternRequest.FOLLOW_PATTERN_FIELD); PARSER.declareObject(Pattern::setSettings, (p, c) -> Settings.fromXContent(p), PutAutoFollowPatternRequest.SETTINGS); PARSER.declareInt(Pattern::setMaxReadRequestOperationCount, FollowConfig.MAX_READ_REQUEST_OPERATION_COUNT); @@ -101,31 +112,36 @@ public static class Pattern extends FollowConfig { Pattern::setMaxReadRequestSize, (p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), FollowConfig.MAX_READ_REQUEST_SIZE.getPreferredName()), PutFollowRequest.MAX_READ_REQUEST_SIZE, - ObjectParser.ValueType.STRING); + ObjectParser.ValueType.STRING + ); PARSER.declareInt(Pattern::setMaxOutstandingReadRequests, FollowConfig.MAX_OUTSTANDING_READ_REQUESTS); PARSER.declareInt(Pattern::setMaxWriteRequestOperationCount, FollowConfig.MAX_WRITE_REQUEST_OPERATION_COUNT); PARSER.declareField( Pattern::setMaxWriteRequestSize, (p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), FollowConfig.MAX_WRITE_REQUEST_SIZE.getPreferredName()), PutFollowRequest.MAX_WRITE_REQUEST_SIZE, - ObjectParser.ValueType.STRING); + ObjectParser.ValueType.STRING + ); PARSER.declareInt(Pattern::setMaxOutstandingWriteRequests, FollowConfig.MAX_OUTSTANDING_WRITE_REQUESTS); PARSER.declareInt(Pattern::setMaxWriteBufferCount, FollowConfig.MAX_WRITE_BUFFER_COUNT); PARSER.declareField( Pattern::setMaxWriteBufferSize, (p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), FollowConfig.MAX_WRITE_BUFFER_SIZE.getPreferredName()), PutFollowRequest.MAX_WRITE_BUFFER_SIZE, - ObjectParser.ValueType.STRING); + ObjectParser.ValueType.STRING + ); PARSER.declareField( Pattern::setMaxRetryDelay, (p, c) -> TimeValue.parseTimeValue(p.text(), FollowConfig.MAX_RETRY_DELAY_FIELD.getPreferredName()), PutFollowRequest.MAX_RETRY_DELAY_FIELD, - ObjectParser.ValueType.STRING); + ObjectParser.ValueType.STRING + ); PARSER.declareField( Pattern::setReadPollTimeout, (p, c) -> TimeValue.parseTimeValue(p.text(), FollowConfig.READ_POLL_TIMEOUT.getPreferredName()), PutFollowRequest.READ_POLL_TIMEOUT, - ObjectParser.ValueType.STRING); + ObjectParser.ValueType.STRING + ); } private final String remoteCluster; @@ -133,10 +149,12 @@ public static class Pattern extends FollowConfig { private final List leaderIndexExclusionPatterns; private final String followIndexNamePattern; - Pattern(String remoteCluster, - List leaderIndexPatterns, - List leaderIndexExclusionPatterns, - String followIndexNamePattern) { + Pattern( + String remoteCluster, + List leaderIndexPatterns, + List leaderIndexExclusionPatterns, + String followIndexNamePattern + ) { this.remoteCluster = remoteCluster; this.leaderIndexPatterns = leaderIndexPatterns; this.leaderIndexExclusionPatterns = leaderIndexExclusionPatterns; @@ -165,21 +183,15 @@ public boolean equals(Object o) { if (o == null || getClass() != o.getClass()) return false; if (super.equals(o) == false) return false; Pattern pattern = (Pattern) o; - return Objects.equals(remoteCluster, pattern.remoteCluster) && - Objects.equals(leaderIndexPatterns, pattern.leaderIndexPatterns) && - Objects.equals(leaderIndexExclusionPatterns, pattern.leaderIndexExclusionPatterns) && - Objects.equals(followIndexNamePattern, pattern.followIndexNamePattern); + return Objects.equals(remoteCluster, pattern.remoteCluster) + && Objects.equals(leaderIndexPatterns, pattern.leaderIndexPatterns) + && Objects.equals(leaderIndexExclusionPatterns, pattern.leaderIndexExclusionPatterns) + && Objects.equals(followIndexNamePattern, pattern.followIndexNamePattern); } @Override public int hashCode() { - return Objects.hash( - super.hashCode(), - remoteCluster, - leaderIndexPatterns, - leaderIndexExclusionPatterns, - followIndexNamePattern - ); + return Objects.hash(super.hashCode(), remoteCluster, leaderIndexPatterns, leaderIndexExclusionPatterns, followIndexNamePattern); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/IndicesFollowStats.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/IndicesFollowStats.java index b68c5824563f0..a5ecb9edb01c1 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/IndicesFollowStats.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/IndicesFollowStats.java @@ -10,9 +10,9 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.util.Maps; +import org.elasticsearch.core.Tuple; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.core.Tuple; import java.util.AbstractMap; import java.util.Collections; @@ -29,31 +29,24 @@ public final class IndicesFollowStats { static final ParseField SHARDS_FIELD = new ParseField("shards"); private static final ConstructingObjectParser>, Void> ENTRY_PARSER = - new ConstructingObjectParser<>( - "entry", - true, - args -> { - String index = (String) args[0]; - @SuppressWarnings("unchecked") - List shardFollowStats = (List) args[1]; - return new Tuple<>(index, shardFollowStats); - } - ); + new ConstructingObjectParser<>("entry", true, args -> { + String index = (String) args[0]; + @SuppressWarnings("unchecked") + List shardFollowStats = (List) args[1]; + return new Tuple<>(index, shardFollowStats); + }); static { ENTRY_PARSER.declareString(ConstructingObjectParser.constructorArg(), INDEX_FIELD); ENTRY_PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), ShardFollowStats.PARSER, SHARDS_FIELD); } - static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "indices", - true, - args -> { - @SuppressWarnings("unchecked") - List>> entries = (List>>) args[0]; - Map> shardFollowStats = entries.stream().collect(Collectors.toMap(Tuple::v1, Tuple::v2)); - return new IndicesFollowStats(new TreeMap<>(shardFollowStats)); - }); + static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("indices", true, args -> { + @SuppressWarnings("unchecked") + List>> entries = (List>>) args[0]; + Map> shardFollowStats = entries.stream().collect(Collectors.toMap(Tuple::v1, Tuple::v2)); + return new IndicesFollowStats(new TreeMap<>(shardFollowStats)); + }); static { PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), ENTRY_PARSER, INDICES_FIELD); @@ -75,7 +68,6 @@ public Map> getShardFollowStats() { public static final class ShardFollowStats { - static final ParseField LEADER_CLUSTER = new ParseField("remote_cluster"); static final ParseField LEADER_INDEX = new ParseField("leader_index"); static final ParseField FOLLOWER_INDEX = new ParseField("follower_index"); @@ -107,48 +99,49 @@ public static final class ShardFollowStats { static final ParseField FATAL_EXCEPTION = new ParseField("fatal_exception"); @SuppressWarnings("unchecked") - static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>( - "shard-follow-stats", - true, - args -> new ShardFollowStats( - (String) args[0], - (String) args[1], - (String) args[2], - (int) args[3], - (long) args[4], - (long) args[5], - (long) args[6], - (long) args[7], - (long) args[8], - (int) args[9], - (int) args[10], - (int) args[11], - (long) args[12], - (long) args[13], - (long) args[14], - (long) args[15], - (long) args[16], - (long) args[17], - (long) args[18], - (long) args[19], - (long) args[20], - (long) args[21], - (long) args[22], - (long) args[23], - (long) args[24], - (long) args[25], - (long) args[26], - ((List>>) args[27]) - .stream() - .collect(Maps.toUnmodifiableSortedMap(Map.Entry::getKey, Map.Entry::getValue)), - (ElasticsearchException) args[28])); + static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "shard-follow-stats", + true, + args -> new ShardFollowStats( + (String) args[0], + (String) args[1], + (String) args[2], + (int) args[3], + (long) args[4], + (long) args[5], + (long) args[6], + (long) args[7], + (long) args[8], + (int) args[9], + (int) args[10], + (int) args[11], + (long) args[12], + (long) args[13], + (long) args[14], + (long) args[15], + (long) args[16], + (long) args[17], + (long) args[18], + (long) args[19], + (long) args[20], + (long) args[21], + (long) args[22], + (long) args[23], + (long) args[24], + (long) args[25], + (long) args[26], + ((List>>) args[27]).stream() + .collect(Maps.toUnmodifiableSortedMap(Map.Entry::getKey, Map.Entry::getValue)), + (ElasticsearchException) args[28] + ) + ); static final ConstructingObjectParser>, Void> READ_EXCEPTIONS_ENTRY_PARSER = new ConstructingObjectParser<>( "shard-follow-stats-read-exceptions-entry", true, - args -> new AbstractMap.SimpleEntry<>((long) args[0], Tuple.tuple((Integer) args[1], (ElasticsearchException)args[2]))); + args -> new AbstractMap.SimpleEntry<>((long) args[0], Tuple.tuple((Integer) args[1], (ElasticsearchException) args[2])) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), LEADER_CLUSTER); @@ -179,9 +172,11 @@ public static final class ShardFollowStats { PARSER.declareLong(ConstructingObjectParser.constructorArg(), OPERATIONS_WRITTEN); PARSER.declareLong(ConstructingObjectParser.constructorArg(), TIME_SINCE_LAST_READ_MILLIS_FIELD); PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), READ_EXCEPTIONS_ENTRY_PARSER, READ_EXCEPTIONS); - PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), + PARSER.declareObject( + ConstructingObjectParser.optionalConstructorArg(), (p, c) -> ElasticsearchException.fromXContent(p), - FATAL_EXCEPTION); + FATAL_EXCEPTION + ); } static final ParseField READ_EXCEPTIONS_ENTRY_FROM_SEQ_NO = new ParseField("from_seq_no"); @@ -194,7 +189,8 @@ public static final class ShardFollowStats { READ_EXCEPTIONS_ENTRY_PARSER.declareObject( ConstructingObjectParser.constructorArg(), (p, c) -> ElasticsearchException.fromXContent(p), - READ_EXCEPTIONS_ENTRY_EXCEPTION); + READ_EXCEPTIONS_ENTRY_EXCEPTION + ); } private final String remoteCluster; @@ -227,35 +223,37 @@ public static final class ShardFollowStats { private final NavigableMap> readExceptions; private final ElasticsearchException fatalException; - ShardFollowStats(String remoteCluster, - String leaderIndex, - String followerIndex, - int shardId, - long leaderGlobalCheckpoint, - long leaderMaxSeqNo, - long followerGlobalCheckpoint, - long followerMaxSeqNo, - long lastRequestedSeqNo, - int outstandingReadRequests, - int outstandingWriteRequests, - int writeBufferOperationCount, - long writeBufferSizeInBytes, - long followerMappingVersion, - long followerSettingsVersion, - long followerAliasesVersion, - long totalReadTimeMillis, - long totalReadRemoteExecTimeMillis, - long successfulReadRequests, - long failedReadRequests, - long operationsReads, - long bytesRead, - long totalWriteTimeMillis, - long successfulWriteRequests, - long failedWriteRequests, - long operationWritten, - long timeSinceLastReadMillis, - NavigableMap> readExceptions, - ElasticsearchException fatalException) { + ShardFollowStats( + String remoteCluster, + String leaderIndex, + String followerIndex, + int shardId, + long leaderGlobalCheckpoint, + long leaderMaxSeqNo, + long followerGlobalCheckpoint, + long followerMaxSeqNo, + long lastRequestedSeqNo, + int outstandingReadRequests, + int outstandingWriteRequests, + int writeBufferOperationCount, + long writeBufferSizeInBytes, + long followerMappingVersion, + long followerSettingsVersion, + long followerAliasesVersion, + long totalReadTimeMillis, + long totalReadRemoteExecTimeMillis, + long successfulReadRequests, + long failedReadRequests, + long operationsReads, + long bytesRead, + long totalWriteTimeMillis, + long successfulWriteRequests, + long failedWriteRequests, + long operationWritten, + long timeSinceLastReadMillis, + NavigableMap> readExceptions, + ElasticsearchException fatalException + ) { this.remoteCluster = remoteCluster; this.leaderIndex = leaderIndex; this.followerIndex = followerIndex; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/PutAutoFollowPatternRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/PutAutoFollowPatternRequest.java index c581896f42a7c..9918d7bb84231 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/PutAutoFollowPatternRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/PutAutoFollowPatternRequest.java @@ -30,16 +30,16 @@ public final class PutAutoFollowPatternRequest extends FollowConfig implements V private final List leaderIndexExclusionPatterns; private String followIndexNamePattern; - public PutAutoFollowPatternRequest(String name, - String remoteCluster, - List leaderIndexPatterns) { + public PutAutoFollowPatternRequest(String name, String remoteCluster, List leaderIndexPatterns) { this(name, remoteCluster, leaderIndexPatterns, Collections.emptyList()); } - public PutAutoFollowPatternRequest(String name, - String remoteCluster, - List leaderIndexPatterns, - List leaderIndexExclusionPatterns) { + public PutAutoFollowPatternRequest( + String name, + String remoteCluster, + List leaderIndexPatterns, + List leaderIndexExclusionPatterns + ) { this.name = Objects.requireNonNull(name); this.remoteCluster = Objects.requireNonNull(remoteCluster); this.leaderIndexPatterns = Objects.requireNonNull(leaderIndexPatterns); @@ -92,11 +92,11 @@ public boolean equals(Object o) { if (o == null || getClass() != o.getClass()) return false; if (super.equals(o) == false) return false; PutAutoFollowPatternRequest that = (PutAutoFollowPatternRequest) o; - return Objects.equals(name, that.name) && - Objects.equals(remoteCluster, that.remoteCluster) && - Objects.equals(leaderIndexPatterns, that.leaderIndexPatterns) && - Objects.equals(leaderIndexExclusionPatterns, that.leaderIndexExclusionPatterns) && - Objects.equals(followIndexNamePattern, that.followIndexNamePattern); + return Objects.equals(name, that.name) + && Objects.equals(remoteCluster, that.remoteCluster) + && Objects.equals(leaderIndexPatterns, that.leaderIndexPatterns) + && Objects.equals(leaderIndexExclusionPatterns, that.leaderIndexExclusionPatterns) + && Objects.equals(followIndexNamePattern, that.followIndexNamePattern); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/PutFollowRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/PutFollowRequest.java index e144b7b28d6de..26b5225fab630 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/PutFollowRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/PutFollowRequest.java @@ -70,19 +70,14 @@ public boolean equals(Object o) { if (o == null || getClass() != o.getClass()) return false; if (super.equals(o) == false) return false; PutFollowRequest that = (PutFollowRequest) o; - return Objects.equals(waitForActiveShards, that.waitForActiveShards) && - Objects.equals(remoteCluster, that.remoteCluster) && - Objects.equals(leaderIndex, that.leaderIndex) && - Objects.equals(followerIndex, that.followerIndex); + return Objects.equals(waitForActiveShards, that.waitForActiveShards) + && Objects.equals(remoteCluster, that.remoteCluster) + && Objects.equals(leaderIndex, that.leaderIndex) + && Objects.equals(followerIndex, that.followerIndex); } @Override public int hashCode() { - return Objects.hash( - super.hashCode(), - remoteCluster, - leaderIndex, - followerIndex, - waitForActiveShards); + return Objects.hash(super.hashCode(), remoteCluster, leaderIndex, followerIndex, waitForActiveShards); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/PutFollowResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/PutFollowResponse.java index 26ec16ebbc2d9..4f9a016f7a507 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/PutFollowResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/PutFollowResponse.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ccr; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -22,7 +22,10 @@ public final class PutFollowResponse { static final ParseField INDEX_FOLLOWING_STARTED = new ParseField("index_following_started"); private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "put_follow_response", true, args -> new PutFollowResponse((boolean) args[0], (boolean) args[1], (boolean) args[2])); + "put_follow_response", + true, + args -> new PutFollowResponse((boolean) args[0], (boolean) args[1], (boolean) args[2]) + ); static { PARSER.declareBoolean(ConstructingObjectParser.constructorArg(), FOLLOW_INDEX_CREATED); @@ -61,9 +64,9 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; PutFollowResponse that = (PutFollowResponse) o; - return followIndexCreated == that.followIndexCreated && - followIndexShardsAcked == that.followIndexShardsAcked && - indexFollowingStarted == that.indexFollowingStarted; + return followIndexCreated == that.followIndexCreated + && followIndexShardsAcked == that.followIndexShardsAcked + && indexFollowingStarted == that.indexFollowingStarted; } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/cluster/ProxyModeInfo.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/cluster/ProxyModeInfo.java index dcb96e7344742..0e55c232cf3f5 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/cluster/ProxyModeInfo.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/cluster/ProxyModeInfo.java @@ -59,10 +59,10 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; ProxyModeInfo otherProxy = (ProxyModeInfo) o; - return maxSocketConnections == otherProxy.maxSocketConnections && - numSocketsConnected == otherProxy.numSocketsConnected && - Objects.equals(address, otherProxy.address) && - Objects.equals(serverName, otherProxy.serverName); + return maxSocketConnections == otherProxy.maxSocketConnections + && numSocketsConnected == otherProxy.numSocketsConnected + && Objects.equals(address, otherProxy.address) + && Objects.equals(serverName, otherProxy.serverName); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/cluster/RemoteConnectionInfo.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/cluster/RemoteConnectionInfo.java index 078a0b5a7e305..f5069d7771d75 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/cluster/RemoteConnectionInfo.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/cluster/RemoteConnectionInfo.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.cluster; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -31,23 +31,21 @@ public final class RemoteConnectionInfo { @SuppressWarnings("unchecked") private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "RemoteConnectionInfoObjectParser", - false, - (args, clusterAlias) -> { - String mode = (String) args[1]; - ModeInfo modeInfo; - if (mode.equals(ProxyModeInfo.NAME)) { - modeInfo = new ProxyModeInfo((String) args[4], (String) args[5], (int) args[6], (int) args[7]); - } else if (mode.equals(SniffModeInfo.NAME)) { - modeInfo = new SniffModeInfo((List) args[8], (int) args[9], (int) args[10]); - } else { - throw new IllegalArgumentException("mode cannot be " + mode); - } - return new RemoteConnectionInfo(clusterAlias, - modeInfo, - (String) args[2], - (boolean) args[3]); - }); + "RemoteConnectionInfoObjectParser", + false, + (args, clusterAlias) -> { + String mode = (String) args[1]; + ModeInfo modeInfo; + if (mode.equals(ProxyModeInfo.NAME)) { + modeInfo = new ProxyModeInfo((String) args[4], (String) args[5], (int) args[6], (int) args[7]); + } else if (mode.equals(SniffModeInfo.NAME)) { + modeInfo = new SniffModeInfo((List) args[8], (int) args[9], (int) args[10]); + } else { + throw new IllegalArgumentException("mode cannot be " + mode); + } + return new RemoteConnectionInfo(clusterAlias, modeInfo, (String) args[2], (boolean) args[3]); + } + ); static { PARSER.declareBoolean(constructorArg(), new ParseField(CONNECTED)); @@ -109,10 +107,10 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; RemoteConnectionInfo that = (RemoteConnectionInfo) o; - return skipUnavailable == that.skipUnavailable && - Objects.equals(modeInfo, that.modeInfo) && - Objects.equals(initialConnectionTimeoutString, that.initialConnectionTimeoutString) && - Objects.equals(clusterAlias, that.clusterAlias); + return skipUnavailable == that.skipUnavailable + && Objects.equals(modeInfo, that.modeInfo) + && Objects.equals(initialConnectionTimeoutString, that.initialConnectionTimeoutString) + && Objects.equals(clusterAlias, that.clusterAlias); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/cluster/SniffModeInfo.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/cluster/SniffModeInfo.java index cc1f334dd0e1a..e08509dd14b62 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/cluster/SniffModeInfo.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/cluster/SniffModeInfo.java @@ -53,9 +53,9 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; SniffModeInfo sniff = (SniffModeInfo) o; - return maxConnectionsPerCluster == sniff.maxConnectionsPerCluster && - numNodesConnected == sniff.numNodesConnected && - Objects.equals(seedNodes, sniff.seedNodes); + return maxConnectionsPerCluster == sniff.maxConnectionsPerCluster + && numNodesConnected == sniff.numNodesConnected + && Objects.equals(seedNodes, sniff.seedNodes); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/common/TimeUtil.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/common/TimeUtil.java index 2dca391ceb00b..5971dea044daa 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/common/TimeUtil.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/common/TimeUtil.java @@ -31,8 +31,7 @@ public static Date parseTimeField(XContentParser parser, String fieldName) throw } else if (parser.currentToken() == XContentParser.Token.VALUE_STRING) { return new Date(DateFormatters.from(DateTimeFormatter.ISO_INSTANT.parse(parser.text())).toInstant().toEpochMilli()); } - throw new IllegalArgumentException( - "unexpected token [" + parser.currentToken() + "] for [" + fieldName + "]"); + throw new IllegalArgumentException("unexpected token [" + parser.currentToken() + "] for [" + fieldName + "]"); } /** @@ -49,8 +48,7 @@ public static Instant parseTimeFieldToInstant(XContentParser parser, String fiel } else if (parser.currentToken() == XContentParser.Token.VALUE_STRING) { return DateFormatters.from(DateTimeFormatter.ISO_INSTANT.parse(parser.text())).toInstant(); } - throw new IllegalArgumentException( - "unexpected token [" + parser.currentToken() + "] for [" + fieldName + "]"); + throw new IllegalArgumentException("unexpected token [" + parser.currentToken() + "] for [" + fieldName + "]"); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/core/AcknowledgedResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/core/AcknowledgedResponse.java index cd8fb63c54613..7adcee74cb206 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/core/AcknowledgedResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/core/AcknowledgedResponse.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.core; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -21,8 +21,11 @@ public class AcknowledgedResponse { protected static final String PARSE_FIELD_NAME = "acknowledged"; - private static final ConstructingObjectParser PARSER = AcknowledgedResponse - .generateParser("acknowledged_response", AcknowledgedResponse::new, AcknowledgedResponse.PARSE_FIELD_NAME); + private static final ConstructingObjectParser PARSER = AcknowledgedResponse.generateParser( + "acknowledged_response", + AcknowledgedResponse::new, + AcknowledgedResponse.PARSE_FIELD_NAME + ); private final boolean acknowledged; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/core/BroadcastResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/core/BroadcastResponse.java index 702fc1c8c1c8a..cad105a3951d0 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/core/BroadcastResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/core/BroadcastResponse.java @@ -9,8 +9,8 @@ package org.elasticsearch.client.core; import org.elasticsearch.action.support.DefaultShardOperationFailedException; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -41,8 +41,9 @@ protected BroadcastResponse(final Shards shards) { private static final ParseField SHARDS_FIELD = new ParseField("_shards"); static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "broadcast_response", - a -> new BroadcastResponse((Shards) a[0])); + "broadcast_response", + a -> new BroadcastResponse((Shards) a[0]) + ); static { declareShardsField(PARSER); @@ -125,11 +126,12 @@ public Collection failures() { } Shards( - final int total, - final int successful, - final int skipped, - final int failed, - final Collection failures) { + final int total, + final int successful, + final int skipped, + final int failed, + final Collection failures + ) { this.total = total; this.successful = successful; this.skipped = skipped; @@ -145,13 +147,15 @@ public Collection failures() { @SuppressWarnings("unchecked") static final ConstructingObjectParser SHARDS_PARSER = new ConstructingObjectParser<>( - "shards", - a -> new Shards( - (int) a[0], // total - (int) a[1], // successful - a[2] == null ? 0 : (int) a[2], // skipped - (int) a[3], // failed - a[4] == null ? Collections.emptyList() : (Collection) a[4])); // failures + "shards", + a -> new Shards( + (int) a[0], // total + (int) a[1], // successful + a[2] == null ? 0 : (int) a[2], // skipped + (int) a[3], // failed + a[4] == null ? Collections.emptyList() : (Collection) a[4] + ) + ); // failures static { SHARDS_PARSER.declareInt(ConstructingObjectParser.constructorArg(), TOTAL_FIELD); @@ -159,8 +163,10 @@ public Collection failures() { SHARDS_PARSER.declareInt(ConstructingObjectParser.optionalConstructorArg(), SKIPPED_FIELD); SHARDS_PARSER.declareInt(ConstructingObjectParser.constructorArg(), FAILED_FIELD); SHARDS_PARSER.declareObjectArray( - ConstructingObjectParser.optionalConstructorArg(), - DefaultShardOperationFailedException.PARSER, FAILURES_FIELD); + ConstructingObjectParser.optionalConstructorArg(), + DefaultShardOperationFailedException.PARSER, + FAILURES_FIELD + ); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/core/CountRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/core/CountRequest.java index f9a94d5c6d34f..0899eb03311da 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/core/CountRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/core/CountRequest.java @@ -11,11 +11,11 @@ import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.client.Validatable; import org.elasticsearch.common.Strings; -import org.elasticsearch.xcontent.ToXContentObject; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.internal.SearchContext; +import org.elasticsearch.xcontent.ToXContentObject; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.Arrays; @@ -60,7 +60,8 @@ public CountRequest(String[] indices, SearchSourceBuilder searchSourceBuilder) { */ public CountRequest(String[] indices, QueryBuilder query) { indices(indices); - this.query = Objects.requireNonNull(query, "query must not be null");; + this.query = Objects.requireNonNull(query, "query must not be null"); + ; } /** @@ -228,14 +229,14 @@ public boolean equals(Object o) { return false; } CountRequest that = (CountRequest) o; - return Objects.equals(indicesOptions, that.indicesOptions) && - Arrays.equals(indices, that.indices) && - Arrays.equals(types, that.types) && - Objects.equals(routing, that.routing) && - Objects.equals(preference, that.preference) && - Objects.equals(terminateAfter, that.terminateAfter) && - Objects.equals(minScore, that.minScore) && - Objects.equals(query, that.query); + return Objects.equals(indicesOptions, that.indicesOptions) + && Arrays.equals(indices, that.indices) + && Arrays.equals(types, that.types) + && Objects.equals(routing, that.routing) + && Objects.equals(preference, that.preference) + && Objects.equals(terminateAfter, that.terminateAfter) + && Objects.equals(minScore, that.minScore) + && Objects.equals(query, that.query); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/core/CountResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/core/CountResponse.java index 233e677ff9ce2..c19245c4d09f8 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/core/CountResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/core/CountResponse.java @@ -9,9 +9,9 @@ package org.elasticsearch.client.core; import org.elasticsearch.action.search.ShardSearchFailure; +import org.elasticsearch.rest.RestStatus; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.rest.RestStatus; import java.io.IOException; import java.util.ArrayList; @@ -96,7 +96,7 @@ public static CountResponse fromXContent(XContentParser parser) throws IOExcepti String currentName = parser.currentName(); Boolean terminatedEarly = null; long count = 0; - ShardStats shardStats = new ShardStats(-1, -1,0, ShardSearchFailure.EMPTY_ARRAY); + ShardStats shardStats = new ShardStats(-1, -1, 0, ShardSearchFailure.EMPTY_ARRAY); for (XContentParser.Token token = parser.nextToken(); token != XContentParser.Token.END_OBJECT; token = parser.nextToken()) { if (token == XContentParser.Token.FIELD_NAME) { @@ -122,11 +122,13 @@ public static CountResponse fromXContent(XContentParser parser) throws IOExcepti @Override public String toString() { - String s = "{" + - "count=" + count + - (isTerminatedEarly() != null ? ", terminatedEarly=" + terminatedEarly : "") + - ", " + shardStats + - '}'; + String s = "{" + + "count=" + + count + + (isTerminatedEarly() != null ? ", terminatedEarly=" + terminatedEarly : "") + + ", " + + shardStats + + '}'; return s; } @@ -176,7 +178,7 @@ public ShardSearchFailure[] getShardFailures() { static ShardStats fromXContent(XContentParser parser) throws IOException { int successfulShards = -1; int totalShards = -1; - int skippedShards = 0; //BWC @see org.elasticsearch.action.search.SearchResponse + int skippedShards = 0; // BWC @see org.elasticsearch.action.search.SearchResponse List failures = new ArrayList<>(); XContentParser.Token token; String currentName = parser.currentName(); @@ -212,13 +214,17 @@ static ShardStats fromXContent(XContentParser parser) throws IOException { @Override public String toString() { - return "_shards : {" + - "total=" + totalShards + - ", successful=" + successfulShards + - ", skipped=" + skippedShards + - ", failed=" + (shardFailures != null && shardFailures.length > 0 ? shardFailures.length : 0 ) + - (shardFailures != null && shardFailures.length > 0 ? ", failures: " + Arrays.asList(shardFailures): "") + - '}'; + return "_shards : {" + + "total=" + + totalShards + + ", successful=" + + successfulShards + + ", skipped=" + + skippedShards + + ", failed=" + + (shardFailures != null && shardFailures.length > 0 ? shardFailures.length : 0) + + (shardFailures != null && shardFailures.length > 0 ? ", failures: " + Arrays.asList(shardFailures) : "") + + '}'; } } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/core/GetSourceRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/core/GetSourceRequest.java index 2abb6ec343982..6e26457a27a51 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/core/GetSourceRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/core/GetSourceRequest.java @@ -30,8 +30,7 @@ public GetSourceRequest(String index, String id) { } public static GetSourceRequest from(GetRequest getRequest) { - return new GetSourceRequest(getRequest.index(), getRequest.id()) - .routing(getRequest.routing()) + return new GetSourceRequest(getRequest.index(), getRequest.id()).routing(getRequest.routing()) .preference(getRequest.preference()) .refresh(getRequest.refresh()) .realtime(getRequest.realtime()) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/core/IndexerJobStats.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/core/IndexerJobStats.java index faf42ef76ba98..e404f254e17ad 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/core/IndexerJobStats.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/core/IndexerJobStats.java @@ -39,9 +39,20 @@ public abstract class IndexerJobStats { protected final long indexFailures; protected final long searchFailures; - public IndexerJobStats(long numPages, long numInputDocuments, long numOutputDocuments, long numInvocations, - long indexTime, long searchTime, long processingTime, long indexTotal, long searchTotal, long processingTotal, - long indexFailures, long searchFailures) { + public IndexerJobStats( + long numPages, + long numInputDocuments, + long numOutputDocuments, + long numInvocations, + long indexTime, + long searchTime, + long processingTime, + long indexTotal, + long searchTotal, + long processingTotal, + long indexFailures, + long searchFailures + ) { this.numPages = numPages; this.numInputDocuments = numInputDocuments; this.numOuputDocuments = numOutputDocuments; @@ -141,7 +152,6 @@ public long getProcessingTotal() { return processingTotal; } - @Override public boolean equals(Object other) { if (this == other) { @@ -154,39 +164,63 @@ public boolean equals(Object other) { IndexerJobStats that = (IndexerJobStats) other; return Objects.equals(this.numPages, that.numPages) - && Objects.equals(this.numInputDocuments, that.numInputDocuments) - && Objects.equals(this.numOuputDocuments, that.numOuputDocuments) - && Objects.equals(this.numInvocations, that.numInvocations) - && Objects.equals(this.indexTime, that.indexTime) - && Objects.equals(this.searchTime, that.searchTime) - && Objects.equals(this.processingTime, that.processingTime) - && Objects.equals(this.indexFailures, that.indexFailures) - && Objects.equals(this.searchFailures, that.searchFailures) - && Objects.equals(this.searchTotal, that.searchTotal) - && Objects.equals(this.processingTotal, that.processingTotal) - && Objects.equals(this.indexTotal, that.indexTotal); + && Objects.equals(this.numInputDocuments, that.numInputDocuments) + && Objects.equals(this.numOuputDocuments, that.numOuputDocuments) + && Objects.equals(this.numInvocations, that.numInvocations) + && Objects.equals(this.indexTime, that.indexTime) + && Objects.equals(this.searchTime, that.searchTime) + && Objects.equals(this.processingTime, that.processingTime) + && Objects.equals(this.indexFailures, that.indexFailures) + && Objects.equals(this.searchFailures, that.searchFailures) + && Objects.equals(this.searchTotal, that.searchTotal) + && Objects.equals(this.processingTotal, that.processingTotal) + && Objects.equals(this.indexTotal, that.indexTotal); } @Override public int hashCode() { - return Objects.hash(numPages, numInputDocuments, numOuputDocuments, numInvocations, - indexTime, searchTime, processingTime, indexFailures, searchFailures, searchTotal, - indexTotal, processingTotal); + return Objects.hash( + numPages, + numInputDocuments, + numOuputDocuments, + numInvocations, + indexTime, + searchTime, + processingTime, + indexFailures, + searchFailures, + searchTotal, + indexTotal, + processingTotal + ); } @Override public final String toString() { - return "{pages=" + numPages - + ", input_docs=" + numInputDocuments - + ", output_docs=" + numOuputDocuments - + ", invocations=" + numInvocations - + ", index_failures=" + indexFailures - + ", search_failures=" + searchFailures - + ", index_time_in_ms=" + indexTime - + ", index_total=" + indexTotal - + ", search_time_in_ms=" + searchTime - + ", search_total=" + searchTotal - + ", processing_time_in_ms=" + processingTime - + ", processing_total=" + processingTotal + "}"; + return "{pages=" + + numPages + + ", input_docs=" + + numInputDocuments + + ", output_docs=" + + numOuputDocuments + + ", invocations=" + + numInvocations + + ", index_failures=" + + indexFailures + + ", search_failures=" + + searchFailures + + ", index_time_in_ms=" + + indexTime + + ", index_total=" + + indexTotal + + ", search_time_in_ms=" + + searchTime + + ", search_total=" + + searchTotal + + ", processing_time_in_ms=" + + processingTime + + ", processing_total=" + + processingTotal + + "}"; } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/core/IndexerState.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/core/IndexerState.java index f930226779590..95ca555052613 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/core/IndexerState.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/core/IndexerState.java @@ -8,7 +8,6 @@ package org.elasticsearch.client.core; - import java.util.Locale; /** diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/core/MainRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/core/MainRequest.java index 37e69e1e0218c..592d986743724 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/core/MainRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/core/MainRequest.java @@ -10,5 +10,4 @@ import org.elasticsearch.client.Validatable; -public class MainRequest implements Validatable { -} +public class MainRequest implements Validatable {} diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/core/MainResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/core/MainResponse.java index fc55308d2c4d8..0e16c6d71a82b 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/core/MainResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/core/MainResponse.java @@ -8,20 +8,19 @@ package org.elasticsearch.client.core; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.util.Objects; public class MainResponse { - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(MainResponse.class.getName(), true, - args -> { - return new MainResponse((String) args[0], (Version) args[1], (String) args[2], (String) args[3], (String) args[4]); - } - ); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + MainResponse.class.getName(), + true, + args -> { return new MainResponse((String) args[0], (Version) args[1], (String) args[2], (String) args[3], (String) args[4]); } + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), new ParseField("name")); @@ -75,11 +74,11 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; MainResponse that = (MainResponse) o; - return nodeName.equals(that.nodeName) && - version.equals(that.version) && - clusterName.equals(that.clusterName) && - clusterUuid.equals(that.clusterUuid) && - tagline.equals(that.tagline); + return nodeName.equals(that.nodeName) + && version.equals(that.version) + && clusterName.equals(that.clusterName) + && clusterUuid.equals(that.clusterUuid) + && tagline.equals(that.tagline); } @Override @@ -88,13 +87,23 @@ public int hashCode() { } public static class Version { - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(Version.class.getName(), true, - args -> { - return new Version((String) args[0], (String) args[1], (String) args[2], (String) args[3], (String) args[4], - (Boolean) args[5], (String) args[6], (String) args[7], (String) args[8]); - } - ); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + Version.class.getName(), + true, + args -> { + return new Version( + (String) args[0], + (String) args[1], + (String) args[2], + (String) args[3], + (String) args[4], + (Boolean) args[5], + (String) args[6], + (String) args[7], + (String) args[8] + ); + } + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), new ParseField("number")); @@ -117,8 +126,17 @@ public static class Version { private final String minimumWireCompatibilityVersion; private final String minimumIndexCompatibilityVersion; - public Version(String number, String buildFlavor, String buildType, String buildHash, String buildDate, boolean isSnapshot, - String luceneVersion, String minimumWireCompatibilityVersion, String minimumIndexCompatibilityVersion) { + public Version( + String number, + String buildFlavor, + String buildType, + String buildHash, + String buildDate, + boolean isSnapshot, + String luceneVersion, + String minimumWireCompatibilityVersion, + String minimumIndexCompatibilityVersion + ) { this.number = number; this.buildFlavor = buildFlavor; this.buildType = buildType; @@ -171,21 +189,30 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Version version = (Version) o; - return isSnapshot == version.isSnapshot && - number.equals(version.number) && - Objects.equals(buildFlavor, version.buildFlavor) && - Objects.equals(buildType, version.buildType) && - buildHash.equals(version.buildHash) && - buildDate.equals(version.buildDate) && - luceneVersion.equals(version.luceneVersion) && - minimumWireCompatibilityVersion.equals(version.minimumWireCompatibilityVersion) && - minimumIndexCompatibilityVersion.equals(version.minimumIndexCompatibilityVersion); + return isSnapshot == version.isSnapshot + && number.equals(version.number) + && Objects.equals(buildFlavor, version.buildFlavor) + && Objects.equals(buildType, version.buildType) + && buildHash.equals(version.buildHash) + && buildDate.equals(version.buildDate) + && luceneVersion.equals(version.luceneVersion) + && minimumWireCompatibilityVersion.equals(version.minimumWireCompatibilityVersion) + && minimumIndexCompatibilityVersion.equals(version.minimumIndexCompatibilityVersion); } @Override public int hashCode() { - return Objects.hash(number, buildFlavor, buildType, buildHash, buildDate, isSnapshot, luceneVersion, - minimumWireCompatibilityVersion, minimumIndexCompatibilityVersion); + return Objects.hash( + number, + buildFlavor, + buildType, + buildHash, + buildDate, + isSnapshot, + luceneVersion, + minimumWireCompatibilityVersion, + minimumIndexCompatibilityVersion + ); } } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/core/MultiTermVectorsResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/core/MultiTermVectorsResponse.java index ed833d35072bb..3f836d714433b 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/core/MultiTermVectorsResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/core/MultiTermVectorsResponse.java @@ -8,9 +8,8 @@ package org.elasticsearch.client.core; - -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.util.List; @@ -25,17 +24,19 @@ public MultiTermVectorsResponse(List responses) { this.responses = responses; } - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("multi_term_vectors", true, + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "multi_term_vectors", + true, args -> { // as the response comes from server, we are sure that args[0] will be a list of TermVectorsResponse - @SuppressWarnings("unchecked") List termVectorsResponsesList = (List) args[0]; + @SuppressWarnings("unchecked") + List termVectorsResponsesList = (List) args[0]; return new MultiTermVectorsResponse(termVectorsResponsesList); } ); static { - PARSER.declareObjectArray(constructorArg(), (p,c) -> TermVectorsResponse.fromXContent(p), new ParseField("docs")); + PARSER.declareObjectArray(constructorArg(), (p, c) -> TermVectorsResponse.fromXContent(p), new ParseField("docs")); } public static MultiTermVectorsResponse fromXContent(XContentParser parser) { @@ -49,7 +50,6 @@ public List getTermVectorsResponses() { return responses; } - @Override public boolean equals(Object obj) { if (this == obj) return true; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/core/PageParams.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/core/PageParams.java index d86f884a8f71a..c41e17e5d1ed3 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/core/PageParams.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/core/PageParams.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.core; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -25,8 +25,10 @@ public class PageParams implements ToXContentObject { public static final ParseField FROM = new ParseField("from"); public static final ParseField SIZE = new ParseField("size"); - public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(PAGE.getPreferredName(), - a -> new PageParams((Integer) a[0], (Integer) a[1])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + PAGE.getPreferredName(), + a -> new PageParams((Integer) a[0], (Integer) a[1]) + ); static { PARSER.declareInt(ConstructingObjectParser.optionalConstructorArg(), FROM); @@ -81,8 +83,7 @@ public boolean equals(Object obj) { return false; } PageParams other = (PageParams) obj; - return Objects.equals(from, other.from) && - Objects.equals(size, other.size); + return Objects.equals(from, other.from) && Objects.equals(size, other.size); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/core/ShardsAcknowledgedResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/core/ShardsAcknowledgedResponse.java index 5958b5741719a..a80a6bb2a15b7 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/core/ShardsAcknowledgedResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/core/ShardsAcknowledgedResponse.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.core; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -18,10 +18,14 @@ public class ShardsAcknowledgedResponse extends AcknowledgedResponse { protected static final String SHARDS_PARSE_FIELD_NAME = "shards_acknowledged"; + private static ConstructingObjectParser buildParser() { - ConstructingObjectParser p = new ConstructingObjectParser<>("freeze", true, - args -> new ShardsAcknowledgedResponse((boolean) args[0], (boolean) args[1])); + ConstructingObjectParser p = new ConstructingObjectParser<>( + "freeze", + true, + args -> new ShardsAcknowledgedResponse((boolean) args[0], (boolean) args[1]) + ); p.declareBoolean(constructorArg(), new ParseField(AcknowledgedResponse.PARSE_FIELD_NAME)); p.declareBoolean(constructorArg(), new ParseField(SHARDS_PARSE_FIELD_NAME)); return p; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/core/TermVectorsRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/core/TermVectorsRequest.java index 38dc03fce7e14..e2b54f94524ac 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/core/TermVectorsRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/core/TermVectorsRequest.java @@ -9,8 +9,8 @@ package org.elasticsearch.client.core; import org.elasticsearch.client.Validatable; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -21,7 +21,8 @@ public class TermVectorsRequest implements ToXContentObject, Validatable { private final String index; - @Nullable private final String type; + @Nullable + private final String type; private String id = null; private XContentBuilder docBuilder = null; @@ -94,7 +95,6 @@ public TermVectorsRequest(String index, String type, XContentBuilder docBuilder) this.docBuilder = docBuilder; } - /** * Constructs a new TermVectorRequest from a template * using the provided document id @@ -238,7 +238,6 @@ public boolean getRealtime() { return realtime; } - @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); @@ -265,8 +264,14 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws if (filterSettings != null) { builder.startObject("filter"); - String[] filterSettingNames = - {"max_num_terms", "min_term_freq", "max_term_freq", "min_doc_freq", "max_doc_freq", "min_word_length", "max_word_length"}; + String[] filterSettingNames = { + "max_num_terms", + "min_term_freq", + "max_term_freq", + "min_doc_freq", + "max_doc_freq", + "min_word_length", + "max_word_length" }; for (String settingName : filterSettingNames) { if (filterSettings.containsKey(settingName)) builder.field(settingName, filterSettings.get(settingName)); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/core/TermVectorsResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/core/TermVectorsResponse.java index 738002f895608..89764f639e1a0 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/core/TermVectorsResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/core/TermVectorsResponse.java @@ -9,8 +9,8 @@ package org.elasticsearch.client.core; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.util.Collections; @@ -38,10 +38,13 @@ public TermVectorsResponse(String index, String id, long version, boolean found, this.termVectorList = termVectorList; } - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("term_vectors", true, + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "term_vectors", + true, args -> { // as the response comes from server, we are sure that args[5] will be a list of TermVector - @SuppressWarnings("unchecked") List termVectorList = (List) args[5]; + @SuppressWarnings("unchecked") + List termVectorList = (List) args[5]; if (termVectorList != null) { Collections.sort(termVectorList, Comparator.comparing(TermVector::getFieldName)); } @@ -62,8 +65,11 @@ public TermVectorsResponse(String index, String id, long version, boolean found, PARSER.declareLong(constructorArg(), new ParseField("_version")); PARSER.declareBoolean(constructorArg(), new ParseField("found")); PARSER.declareLong(constructorArg(), new ParseField("took")); - PARSER.declareNamedObjects(optionalConstructorArg(), - (p, c, fieldName) -> TermVector.fromXContent(p, fieldName), new ParseField("term_vectors")); + PARSER.declareNamedObjects( + optionalConstructorArg(), + (p, c, fieldName) -> TermVector.fromXContent(p, fieldName), + new ParseField("term_vectors") + ); } public static TermVectorsResponse fromXContent(XContentParser parser) { @@ -97,7 +103,7 @@ public boolean getFound() { * Returns the document version */ public long getDocVersion() { - return docVersion; + return docVersion; } /** @@ -110,7 +116,7 @@ public long getTookInMillis() { /** * Returns the list of term vectors */ - public List getTermVectorsList(){ + public List getTermVectorsList() { return termVectorList; } @@ -132,13 +138,15 @@ public int hashCode() { return Objects.hash(index, id, docVersion, found, tookInMillis, termVectorList); } - public static final class TermVector { - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("term_vector", true, - (args, ctxFieldName) -> { + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "term_vector", + true, + (args, ctxFieldName) -> { // as the response comes from server, we are sure that args[1] will be a list of Term - @SuppressWarnings("unchecked") List terms = (List) args[1]; + @SuppressWarnings("unchecked") + List terms = (List) args[1]; if (terms != null) { Collections.sort(terms, Comparator.comparing(Term::getTerm)); } @@ -147,8 +155,7 @@ public static final class TermVector { ); static { - PARSER.declareObject(optionalConstructorArg(), - (p,c) -> FieldStatistics.fromXContent(p), new ParseField("field_statistics")); + PARSER.declareObject(optionalConstructorArg(), (p, c) -> FieldStatistics.fromXContent(p), new ParseField("field_statistics")); PARSER.declareNamedObjects(optionalConstructorArg(), (p, c, term) -> Term.fromXContent(p, term), new ParseField("terms")); } @@ -189,7 +196,6 @@ public FieldStatistics getFieldStatistics() { return fieldStatistics; } - @Override public boolean equals(Object obj) { if (this == obj) return true; @@ -209,10 +215,9 @@ public int hashCode() { public static final class FieldStatistics { private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "field_statistics", true, - args -> { - return new FieldStatistics((long) args[0], (int) args[1], (long) args[2]); - } + "field_statistics", + true, + args -> { return new FieldStatistics((long) args[0], (int) args[1], (long) args[2]); } ); static { @@ -254,14 +259,13 @@ public long getSumDocFreq() { public long getSumTotalTermFreq() { return sumTotalTermFreq; } + @Override public boolean equals(Object obj) { if (this == obj) return true; if ((obj instanceof FieldStatistics) == false) return false; FieldStatistics other = (FieldStatistics) obj; - return docCount == other.docCount - && sumDocFreq == other.sumDocFreq - && sumTotalTermFreq == other.sumTotalTermFreq; + return docCount == other.docCount && sumDocFreq == other.sumDocFreq && sumTotalTermFreq == other.sumTotalTermFreq; } @Override @@ -270,12 +274,14 @@ public int hashCode() { } } - public static final class Term { - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("token", true, - (args, ctxTerm) -> { + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "token", + true, + (args, ctxTerm) -> { // as the response comes from server, we are sure that args[4] will be a list of Token - @SuppressWarnings("unchecked") List tokens = (List) args[4]; + @SuppressWarnings("unchecked") + List tokens = (List) args[4]; if (tokens != null) { Collections.sort( tokens, @@ -292,7 +298,7 @@ public static final class Term { PARSER.declareInt(optionalConstructorArg(), new ParseField("doc_freq")); PARSER.declareLong(optionalConstructorArg(), new ParseField("ttf")); PARSER.declareFloat(optionalConstructorArg(), new ParseField("score")); - PARSER.declareObjectArray(optionalConstructorArg(), (p,c) -> Token.fromXContent(p), new ParseField("tokens")); + PARSER.declareObjectArray(optionalConstructorArg(), (p, c) -> Token.fromXContent(p), new ParseField("tokens")); } private final String term; @@ -343,14 +349,14 @@ public Integer getDocFreq() { /** * Returns total term frequency - the number of times this term occurs across all documents */ - public Long getTotalTermFreq( ){ + public Long getTotalTermFreq() { return totalTermFreq; } /** * Returns tf-idf score, if the request used some form of terms filtering */ - public Float getScore(){ + public Float getScore() { return score; } @@ -380,13 +386,13 @@ public int hashCode() { } } - public static final class Token { - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("token", true, - args -> { - return new Token((Integer) args[0], (Integer) args[1], (Integer) args[2], (String) args[3]); - }); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "token", + true, + args -> { return new Token((Integer) args[0], (Integer) args[1], (Integer) args[2], (String) args[3]); } + ); static { PARSER.declareInt(optionalConstructorArg(), new ParseField("start_offset")); PARSER.declareInt(optionalConstructorArg(), new ParseField("end_offset")); @@ -403,8 +409,7 @@ public static final class Token { @Nullable private final String payload; - - public Token(Integer startOffset, Integer endOffset, Integer position, String payload) { + public Token(Integer startOffset, Integer endOffset, Integer position, String payload) { this.startOffset = startOffset; this.endOffset = endOffset; this.position = position; @@ -449,7 +454,7 @@ public boolean equals(Object obj) { if ((obj instanceof Token) == false) return false; Token other = (Token) obj; return Objects.equals(startOffset, other.startOffset) - && Objects.equals(endOffset,other.endOffset) + && Objects.equals(endOffset, other.endOffset) && Objects.equals(position, other.position) && Objects.equals(payload, other.payload); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/enrich/ExecutePolicyResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/enrich/ExecutePolicyResponse.java index 6da3c69a924b4..32e1d037c9bad 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/enrich/ExecutePolicyResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/enrich/ExecutePolicyResponse.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.enrich; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; public final class ExecutePolicyResponse { diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/enrich/GetPolicyResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/enrich/GetPolicyResponse.java index 0e3b14375e9bc..b8d823d33a259 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/enrich/GetPolicyResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/enrich/GetPolicyResponse.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.enrich; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -31,10 +31,12 @@ public final class GetPolicyResponse { ); static { - PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), - CONFIG_PARSER::apply, new ParseField("policies")); - CONFIG_PARSER.declareObject(ConstructingObjectParser.constructorArg(), - (p, c) -> NamedPolicy.fromXContent(p), new ParseField("config")); + PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), CONFIG_PARSER::apply, new ParseField("policies")); + CONFIG_PARSER.declareObject( + ConstructingObjectParser.constructorArg(), + (p, c) -> NamedPolicy.fromXContent(p), + new ParseField("config") + ); } private final List policies; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/enrich/NamedPolicy.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/enrich/NamedPolicy.java index 4971e57d0436c..d83975fbd5a8d 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/enrich/NamedPolicy.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/enrich/NamedPolicy.java @@ -7,10 +7,10 @@ */ package org.elasticsearch.client.enrich; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/enrich/PutPolicyRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/enrich/PutPolicyRequest.java index f1b1e4ec7fdc1..b9875608ef931 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/enrich/PutPolicyRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/enrich/PutPolicyRequest.java @@ -10,12 +10,12 @@ import org.elasticsearch.client.Validatable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.index.query.QueryBuilder; import java.io.IOException; import java.util.List; @@ -69,8 +69,8 @@ public BytesReference getQuery() { // package private for testing only void setQuery(BytesReference query) { - assert query == null || XContentHelper.xContentType(query).canonical() == XContentType.JSON : - "Only accepts JSON encoded query but received [" + Strings.toString(query) + "]"; + assert query == null || XContentHelper.xContentType(query).canonical() == XContentType.JSON + : "Only accepts JSON encoded query but received [" + Strings.toString(query) + "]"; this.query = query; } @@ -114,12 +114,12 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; PutPolicyRequest that = (PutPolicyRequest) o; - return Objects.equals(name, that.name) && - Objects.equals(type, that.type) && - Objects.equals(query, that.query) && - Objects.equals(indices, that.indices) && - Objects.equals(matchField, that.matchField) && - Objects.equals(enrichFields, that.enrichFields); + return Objects.equals(name, that.name) + && Objects.equals(type, that.type) + && Objects.equals(query, that.query) + && Objects.equals(indices, that.indices) + && Objects.equals(matchField, that.matchField) + && Objects.equals(enrichFields, that.enrichFields); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/enrich/StatsRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/enrich/StatsRequest.java index 39df14f746ce4..fbc7a6a782fc7 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/enrich/StatsRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/enrich/StatsRequest.java @@ -9,5 +9,4 @@ import org.elasticsearch.client.Validatable; -public final class StatsRequest implements Validatable { -} +public final class StatsRequest implements Validatable {} diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/enrich/StatsResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/enrich/StatsResponse.java index 9471dee878eee..134b4b0cb16d2 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/enrich/StatsResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/enrich/StatsResponse.java @@ -7,10 +7,10 @@ */ package org.elasticsearch.client.enrich; -import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.tasks.TaskInfo; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.tasks.TaskInfo; import java.util.List; import java.util.Objects; @@ -88,11 +88,13 @@ public static final class CoordinatorStats { private final long remoteRequestsTotal; private final long executedSearchesTotal; - public CoordinatorStats(String nodeId, - int queueSize, - int remoteRequestsCurrent, - long remoteRequestsTotal, - long executedSearchesTotal) { + public CoordinatorStats( + String nodeId, + int queueSize, + int remoteRequestsCurrent, + long remoteRequestsTotal, + long executedSearchesTotal + ) { this.nodeId = nodeId; this.queueSize = queueSize; this.remoteRequestsCurrent = remoteRequestsCurrent; @@ -125,11 +127,11 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; CoordinatorStats stats = (CoordinatorStats) o; - return Objects.equals(nodeId, stats.nodeId) && - queueSize == stats.queueSize && - remoteRequestsCurrent == stats.remoteRequestsCurrent && - remoteRequestsTotal == stats.remoteRequestsTotal && - executedSearchesTotal == stats.executedSearchesTotal; + return Objects.equals(nodeId, stats.nodeId) + && queueSize == stats.queueSize + && remoteRequestsCurrent == stats.remoteRequestsCurrent + && remoteRequestsTotal == stats.remoteRequestsTotal + && executedSearchesTotal == stats.executedSearchesTotal; } @Override @@ -175,8 +177,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; ExecutingPolicy that = (ExecutingPolicy) o; - return name.equals(that.name) && - taskInfo.equals(that.taskInfo); + return name.equals(that.name) && taskInfo.equals(that.taskInfo); } @Override @@ -246,8 +247,11 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; CacheStats that = (CacheStats) o; - return count == that.count && hits == that.hits && misses == that.misses && - evictions == that.evictions && nodeId.equals(that.nodeId); + return count == that.count + && hits == that.hits + && misses == that.misses + && evictions == that.evictions + && nodeId.equals(that.nodeId); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/eql/EqlSearchRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/eql/EqlSearchRequest.java index d41ac63e2d485..b80593dc31ca8 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/eql/EqlSearchRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/eql/EqlSearchRequest.java @@ -11,11 +11,11 @@ import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.client.Validatable; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.index.query.QueryBuilder; +import org.elasticsearch.search.fetch.subphase.FieldAndFormat; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.index.query.QueryBuilder; -import org.elasticsearch.search.fetch.subphase.FieldAndFormat; import java.io.IOException; import java.util.Arrays; @@ -247,21 +247,21 @@ public boolean equals(Object o) { return false; } EqlSearchRequest that = (EqlSearchRequest) o; - return size == that.size && - fetchSize == that.fetchSize && - Arrays.equals(indices, that.indices) && - Objects.equals(indicesOptions, that.indicesOptions) && - Objects.equals(filter, that.filter) && - Objects.equals(timestampField, that.timestampField) && - Objects.equals(tiebreakerField, that.tiebreakerField) && - Objects.equals(eventCategoryField, that.eventCategoryField) && - Objects.equals(query, that.query) && - Objects.equals(waitForCompletionTimeout, that.waitForCompletionTimeout) && - Objects.equals(keepAlive, that.keepAlive) && - Objects.equals(keepOnCompletion, that.keepOnCompletion) && - Objects.equals(resultPosition, that.resultPosition) && - Objects.equals(fetchFields, that.fetchFields) && - Objects.equals(runtimeMappings, that.runtimeMappings); + return size == that.size + && fetchSize == that.fetchSize + && Arrays.equals(indices, that.indices) + && Objects.equals(indicesOptions, that.indicesOptions) + && Objects.equals(filter, that.filter) + && Objects.equals(timestampField, that.timestampField) + && Objects.equals(tiebreakerField, that.tiebreakerField) + && Objects.equals(eventCategoryField, that.eventCategoryField) + && Objects.equals(query, that.query) + && Objects.equals(waitForCompletionTimeout, that.waitForCompletionTimeout) + && Objects.equals(keepAlive, that.keepAlive) + && Objects.equals(keepOnCompletion, that.keepOnCompletion) + && Objects.equals(resultPosition, that.resultPosition) + && Objects.equals(fetchFields, that.fetchFields) + && Objects.equals(runtimeMappings, that.runtimeMappings); } @Override @@ -281,7 +281,8 @@ public int hashCode() { keepOnCompletion, resultPosition, fetchFields, - runtimeMappings); + runtimeMappings + ); } public String[] indices() { diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/eql/EqlSearchResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/eql/EqlSearchResponse.java index d0d9623381d76..209d088231920 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/eql/EqlSearchResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/eql/EqlSearchResponse.java @@ -9,19 +9,19 @@ package org.elasticsearch.client.eql; import org.apache.lucene.search.TotalHits; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.xcontent.ConstructingObjectParser; -import org.elasticsearch.xcontent.InstantiatingObjectParser; -import org.elasticsearch.xcontent.ObjectParser; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParserUtils; +import org.elasticsearch.core.Nullable; import org.elasticsearch.index.get.GetResult; import org.elasticsearch.index.mapper.SourceFieldMapper; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.lookup.SourceLookup; +import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.InstantiatingObjectParser; +import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.Collections; @@ -59,8 +59,11 @@ private static final class Fields { private static final InstantiatingObjectParser PARSER; static { - InstantiatingObjectParser.Builder parser = - InstantiatingObjectParser.builder("eql/search_response", true, EqlSearchResponse.class); + InstantiatingObjectParser.Builder parser = InstantiatingObjectParser.builder( + "eql/search_response", + true, + EqlSearchResponse.class + ); parser.declareObject(constructorArg(), (p, c) -> Hits.fromXContent(p), HITS); parser.declareLong(constructorArg(), TOOK); parser.declareBoolean(constructorArg(), TIMED_OUT); @@ -70,8 +73,14 @@ private static final class Fields { PARSER = parser.build(); } - public EqlSearchResponse(Hits hits, long tookInMillis, boolean isTimeout, String asyncExecutionId, - boolean isRunning, boolean isPartial) { + public EqlSearchResponse( + Hits hits, + long tookInMillis, + boolean isTimeout, + String asyncExecutionId, + boolean isRunning, + boolean isPartial + ) { super(); this.hits = hits == null ? Hits.EMPTY : hits; this.tookInMillis = tookInMillis; @@ -141,9 +150,11 @@ private static final class Fields { private static final ParseField ID = new ParseField(Fields.ID); private static final ParseField SOURCE = new ParseField(Fields.SOURCE); - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("eql/search_response_event", true, - args -> new Event((String) args[0], (String) args[1], (BytesReference) args[2])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "eql/search_response_event", + true, + args -> new Event((String) args[0], (String) args[1], (BytesReference) args[2]) + ); static { PARSER.declareString(constructorArg(), INDEX); @@ -211,9 +222,7 @@ public boolean equals(Object obj) { } EqlSearchResponse.Event other = (EqlSearchResponse.Event) obj; - return Objects.equals(index, other.index) - && Objects.equals(id, other.id) - && Objects.equals(source, other.source); + return Objects.equals(index, other.index) && Objects.equals(id, other.id) && Objects.equals(source, other.source); } } @@ -227,18 +236,26 @@ private static final class Fields { private static final ParseField JOIN_KEYS = new ParseField(Fields.JOIN_KEYS); private static final ParseField EVENTS = new ParseField(Fields.EVENTS); - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("eql/search_response_sequence", true, - args -> { - int i = 0; - @SuppressWarnings("unchecked") List joinKeys = (List) args[i++]; - @SuppressWarnings("unchecked") List events = (List) args[i]; - return new EqlSearchResponse.Sequence(joinKeys, events); - }); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "eql/search_response_sequence", + true, + args -> { + int i = 0; + @SuppressWarnings("unchecked") + List joinKeys = (List) args[i++]; + @SuppressWarnings("unchecked") + List events = (List) args[i]; + return new EqlSearchResponse.Sequence(joinKeys, events); + } + ); static { - PARSER.declareFieldArray(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> XContentParserUtils.parseFieldsValue(p), - JOIN_KEYS, ObjectParser.ValueType.VALUE_ARRAY); + PARSER.declareFieldArray( + ConstructingObjectParser.optionalConstructorArg(), + (p, c) -> XContentParserUtils.parseFieldsValue(p), + JOIN_KEYS, + ObjectParser.ValueType.VALUE_ARRAY + ); PARSER.declareObjectArray(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> Event.fromXContent(p), EVENTS); } @@ -276,8 +293,7 @@ public boolean equals(Object o) { return false; } Sequence that = (Sequence) o; - return Objects.equals(joinKeys, that.joinKeys) - && Objects.equals(events, that.events); + return Objects.equals(joinKeys, that.joinKeys) && Objects.equals(events, that.events); } } @@ -301,23 +317,32 @@ public Hits(@Nullable List events, @Nullable List sequences, @N this.totalHits = totalHits; } - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("eql/search_response_hits", true, - args -> { - int i = 0; - @SuppressWarnings("unchecked") List events = (List) args[i++]; - @SuppressWarnings("unchecked") List sequences = (List) args[i++]; - TotalHits totalHits = (TotalHits) args[i]; - return new EqlSearchResponse.Hits(events, sequences, totalHits); - }); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "eql/search_response_hits", + true, + args -> { + int i = 0; + @SuppressWarnings("unchecked") + List events = (List) args[i++]; + @SuppressWarnings("unchecked") + List sequences = (List) args[i++]; + TotalHits totalHits = (TotalHits) args[i]; + return new EqlSearchResponse.Hits(events, sequences, totalHits); + } + ); static { - PARSER.declareObjectArray(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> Event.fromXContent(p), - new ParseField(Fields.EVENTS)); - PARSER.declareObjectArray(ConstructingObjectParser.optionalConstructorArg(), Sequence.PARSER, - new ParseField(Fields.SEQUENCES)); - PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> SearchHits.parseTotalHitsFragment(p), - new ParseField(Fields.TOTAL)); + PARSER.declareObjectArray( + ConstructingObjectParser.optionalConstructorArg(), + (p, c) -> Event.fromXContent(p), + new ParseField(Fields.EVENTS) + ); + PARSER.declareObjectArray(ConstructingObjectParser.optionalConstructorArg(), Sequence.PARSER, new ParseField(Fields.SEQUENCES)); + PARSER.declareObject( + ConstructingObjectParser.optionalConstructorArg(), + (p, c) -> SearchHits.parseTotalHitsFragment(p), + new ParseField(Fields.TOTAL) + ); } public static Hits fromXContent(XContentParser parser) throws IOException { diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/eql/EqlStatsRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/eql/EqlStatsRequest.java index deda31c0d4f0d..4d52d5b97faff 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/eql/EqlStatsRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/eql/EqlStatsRequest.java @@ -9,5 +9,4 @@ import org.elasticsearch.client.Validatable; -public final class EqlStatsRequest implements Validatable { -} +public final class EqlStatsRequest implements Validatable {} diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/eql/EqlStatsResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/eql/EqlStatsResponse.java index 6278add6592e9..ae0ae0cf76a3c 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/eql/EqlStatsResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/eql/EqlStatsResponse.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.eql; import org.elasticsearch.client.NodesResponseHeader; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -29,21 +29,26 @@ public EqlStatsResponse(NodesResponseHeader header, String clusterName, List - PARSER = new ConstructingObjectParser<>("eql/stats_response", true, args -> { + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "eql/stats_response", + true, + args -> { int i = 0; NodesResponseHeader header = (NodesResponseHeader) args[i++]; String clusterName = (String) args[i++]; List nodes = (List) args[i]; return new EqlStatsResponse(header, clusterName, nodes); - }); + } + ); static { PARSER.declareObject(ConstructingObjectParser.constructorArg(), NodesResponseHeader::fromXContent, new ParseField("_nodes")); PARSER.declareString(ConstructingObjectParser.constructorArg(), new ParseField("cluster_name")); - PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), + PARSER.declareObjectArray( + ConstructingObjectParser.constructorArg(), (p, c) -> EqlStatsResponse.Node.PARSER.apply(p, null), - new ParseField("stats")); + new ParseField("stats") + ); } public static EqlStatsResponse fromXContent(XContentParser parser) throws IOException { @@ -77,8 +82,11 @@ public int hashCode() { public static class Node { @SuppressWarnings("unchecked") - public static final ConstructingObjectParser - PARSER = new ConstructingObjectParser<>("eql/stats_response_node", true, (args, c) -> new Node((Map) args[0])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "eql/stats_response_node", + true, + (args, c) -> new Node((Map) args[0]) + ); static { PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> p.map(), new ParseField("stats")); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/feature/GetFeaturesRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/feature/GetFeaturesRequest.java index 71ff178585cf1..313a118ba4f00 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/feature/GetFeaturesRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/feature/GetFeaturesRequest.java @@ -13,5 +13,4 @@ /** * A {@link TimedRequest} to get the list of features available to be included in snapshots in the cluster. */ -public class GetFeaturesRequest extends TimedRequest { -} +public class GetFeaturesRequest extends TimedRequest {} diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/feature/GetFeaturesResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/feature/GetFeaturesResponse.java index 81ce37f50b7f3..440444abbbbee 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/feature/GetFeaturesResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/feature/GetFeaturesResponse.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.feature; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.util.List; @@ -24,7 +24,9 @@ public class GetFeaturesResponse { @SuppressWarnings("unchecked") private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "snapshottable_features_response", true, (a, ctx) -> new GetFeaturesResponse((List) a[0]) + "snapshottable_features_response", + true, + (a, ctx) -> new GetFeaturesResponse((List) a[0]) ); static { @@ -64,15 +66,15 @@ public static class SnapshottableFeature { private static final ParseField FEATURE_NAME = new ParseField("name"); private static final ParseField DESCRIPTION = new ParseField("description"); - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "feature", true, (a, ctx) -> new SnapshottableFeature((String) a[0], (String) a[1]) + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "feature", + true, + (a, ctx) -> new SnapshottableFeature((String) a[0], (String) a[1]) ); static { - PARSER.declareField(ConstructingObjectParser.constructorArg(), - (p, c) -> p.text(), FEATURE_NAME, ObjectParser.ValueType.STRING); - PARSER.declareField(ConstructingObjectParser.constructorArg(), - (p, c) -> p.text(), DESCRIPTION, ObjectParser.ValueType.STRING); + PARSER.declareField(ConstructingObjectParser.constructorArg(), (p, c) -> p.text(), FEATURE_NAME, ObjectParser.ValueType.STRING); + PARSER.declareField(ConstructingObjectParser.constructorArg(), (p, c) -> p.text(), DESCRIPTION, ObjectParser.ValueType.STRING); } public SnapshottableFeature(String featureName, String description) { diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/feature/ResetFeaturesRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/feature/ResetFeaturesRequest.java index 7e49a562c9a4e..5bc2565c24b17 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/feature/ResetFeaturesRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/feature/ResetFeaturesRequest.java @@ -10,5 +10,4 @@ import org.elasticsearch.client.TimedRequest; -public class ResetFeaturesRequest extends TimedRequest { -} +public class ResetFeaturesRequest extends TimedRequest {} diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/feature/ResetFeaturesResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/feature/ResetFeaturesResponse.java index 24a8077c4bdae..c3fca66724138 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/feature/ResetFeaturesResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/feature/ResetFeaturesResponse.java @@ -10,9 +10,9 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.util.List; @@ -31,14 +31,17 @@ public class ResetFeaturesResponse { @SuppressWarnings("unchecked") private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "features_reset_status_response", true, + "features_reset_status_response", + true, (a, ctx) -> new ResetFeaturesResponse((List) a[0]) ); static { PARSER.declareObjectArray( ConstructingObjectParser.constructorArg(), - ResetFeaturesResponse.ResetFeatureStateStatus::parse, FEATURES); + ResetFeaturesResponse.ResetFeatureStateStatus::parse, + FEATURES + ); } /** @@ -76,17 +79,19 @@ public static class ResetFeatureStateStatus { private static final ParseField EXCEPTION = new ParseField("exception"); private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "feature_state_reset_stats", true, + "feature_state_reset_stats", + true, (a, ctx) -> new ResetFeatureStateStatus((String) a[0], (String) a[1], (ElasticsearchException) a[2]) ); static { - PARSER.declareField(ConstructingObjectParser.constructorArg(), - (p, c) -> p.text(), FEATURE_NAME, ObjectParser.ValueType.STRING); - PARSER.declareField(ConstructingObjectParser.constructorArg(), - (p, c) -> p.text(), STATUS, ObjectParser.ValueType.STRING); - PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), - (p, c) -> ElasticsearchException.fromXContent(p), EXCEPTION); + PARSER.declareField(ConstructingObjectParser.constructorArg(), (p, c) -> p.text(), FEATURE_NAME, ObjectParser.ValueType.STRING); + PARSER.declareField(ConstructingObjectParser.constructorArg(), (p, c) -> p.text(), STATUS, ObjectParser.ValueType.STRING); + PARSER.declareObject( + ConstructingObjectParser.optionalConstructorArg(), + (p, c) -> ElasticsearchException.fromXContent(p), + EXCEPTION + ); } /** diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/Connection.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/Connection.java index e143b31c64a60..56993fe6a2bd2 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/Connection.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/Connection.java @@ -9,12 +9,12 @@ import com.carrotsearch.hppc.ObjectIntHashMap; -import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.client.graph.Vertex.VertexId; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent.Params; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.client.graph.Vertex.VertexId; import java.io.IOException; import java.util.List; @@ -41,8 +41,7 @@ public Connection(Vertex from, Vertex to, double weight, long docCount) { this.docCount = docCount; } - Connection() { - } + Connection() {} public ConnectionId getId() { return new ConnectionId(from.getId(), to.getId()); @@ -73,17 +72,11 @@ public long getDocCount() { @Override public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; + if (this == obj) return true; + if (obj == null) return false; + if (getClass() != obj.getClass()) return false; Connection other = (Connection) obj; - return docCount == other.docCount && - weight == other.weight && - Objects.equals(to, other.to) && - Objects.equals(from, other.from); + return docCount == other.docCount && weight == other.weight && Objects.equals(to, other.to) && Objects.equals(from, other.from); } @Override @@ -91,13 +84,11 @@ public int hashCode() { return Objects.hash(docCount, weight, from, to); } - private static final ParseField SOURCE = new ParseField("source"); private static final ParseField TARGET = new ParseField("target"); private static final ParseField WEIGHT = new ParseField("weight"); private static final ParseField DOC_COUNT = new ParseField("doc_count"); - void toXContent(XContentBuilder builder, Params params, ObjectIntHashMap vertexNumbers) throws IOException { builder.field(SOURCE.getPreferredName(), vertexNumbers.get(from)); builder.field(TARGET.getPreferredName(), vertexNumbers.get(to)); @@ -105,7 +96,7 @@ void toXContent(XContentBuilder builder, Params params, ObjectIntHashMap builder.field(DOC_COUNT.getPreferredName(), docCount); } - //When deserializing from XContent we need to wait for all vertices to be loaded before + // When deserializing from XContent we need to wait for all vertices to be loaded before // Connection objects can be created that reference them. This class provides the interim // state for connections. static class UnresolvedConnection { @@ -113,6 +104,7 @@ static class UnresolvedConnection { int toIndex; double weight; long docCount; + UnresolvedConnection(int fromIndex, int toIndex, double weight, long docCount) { super(); this.fromIndex = fromIndex; @@ -120,19 +112,22 @@ static class UnresolvedConnection { this.weight = weight; this.docCount = docCount; } + public Connection resolve(List vertices) { return new Connection(vertices.get(fromIndex), vertices.get(toIndex), weight, docCount); } private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "ConnectionParser", true, - args -> { - int source = (Integer) args[0]; - int target = (Integer) args[1]; - double weight = (Double) args[2]; - long docCount = (Long) args[3]; - return new UnresolvedConnection(source, target, weight, docCount); - }); + "ConnectionParser", + true, + args -> { + int source = (Integer) args[0]; + int target = (Integer) args[1]; + double weight = (Double) args[2]; + long docCount = (Long) args[3]; + return new UnresolvedConnection(source, target, weight, docCount); + } + ); static { PARSER.declareInt(constructorArg(), SOURCE); @@ -140,12 +135,12 @@ public Connection resolve(List vertices) { PARSER.declareDouble(constructorArg(), WEIGHT); PARSER.declareLong(constructorArg(), DOC_COUNT); } + static UnresolvedConnection fromXContent(XContentParser parser) throws IOException { return PARSER.apply(parser, null); } } - /** * An identifier (implements hashcode and equals) that represents a * unique key for a {@link Connection} @@ -161,17 +156,13 @@ public ConnectionId(VertexId source, VertexId target) { @Override public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; ConnectionId vertexId = (ConnectionId) o; - if (source != null ? source.equals(vertexId.source) == false : vertexId.source != null) - return false; - if (target != null ? target.equals(vertexId.target) == false : vertexId.target != null) - return false; + if (source != null ? source.equals(vertexId.source) == false : vertexId.source != null) return false; + if (target != null ? target.equals(vertexId.target) == false : vertexId.target != null) return false; return true; } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/GraphExploreRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/GraphExploreRequest.java index 2e09cc6b3b8c2..f8f76629869d9 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/GraphExploreRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/GraphExploreRequest.java @@ -13,12 +13,12 @@ import org.elasticsearch.client.ValidationException; import org.elasticsearch.common.Strings; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.ToXContentObject; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.search.aggregations.bucket.sampler.SamplerAggregationBuilder; import org.elasticsearch.search.aggregations.bucket.terms.SignificantTerms; import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregator; +import org.elasticsearch.xcontent.ToXContentObject; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.ArrayList; @@ -47,8 +47,7 @@ public class GraphExploreRequest implements IndicesRequest.Replaceable, ToXConte private List hops = new ArrayList<>(); - public GraphExploreRequest() { - } + public GraphExploreRequest() {} /** * Constructs a new graph request to run against the provided indices. No @@ -264,8 +263,7 @@ public TermBoost(String term, float boost) { this.boost = boost; } - TermBoost() { - } + TermBoost() {} public String getTerm() { return term; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/GraphExploreResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/GraphExploreResponse.java index 9918717dcf0a9..26f7008bde898 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/GraphExploreResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/GraphExploreResponse.java @@ -11,15 +11,15 @@ import org.elasticsearch.action.ShardOperationFailedException; import org.elasticsearch.action.search.ShardSearchFailure; -import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.client.graph.Connection.ConnectionId; +import org.elasticsearch.client.graph.Connection.UnresolvedConnection; +import org.elasticsearch.client.graph.Vertex.VertexId; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.client.graph.Connection.ConnectionId; -import org.elasticsearch.client.graph.Connection.UnresolvedConnection; -import org.elasticsearch.client.graph.Vertex.VertexId; import java.io.IOException; import java.util.Collection; @@ -46,11 +46,16 @@ public class GraphExploreResponse implements ToXContentObject { private boolean returnDetailedInfo; static final String RETURN_DETAILED_INFO_PARAM = "returnDetailedInfo"; - public GraphExploreResponse() { - } + public GraphExploreResponse() {} - public GraphExploreResponse(long tookInMillis, boolean timedOut, ShardOperationFailedException[] shardFailures, - Map vertices, Map connections, boolean returnDetailedInfo) { + public GraphExploreResponse( + long tookInMillis, + boolean timedOut, + ShardOperationFailedException[] shardFailures, + Map vertices, + Map connections, + boolean returnDetailedInfo + ) { this.tookInMillis = tookInMillis; this.timedOut = timedOut; this.shardFailures = shardFailures; @@ -59,7 +64,6 @@ public GraphExploreResponse(long tookInMillis, boolean timedOut, ShardOperationF this.returnDetailedInfo = returnDetailedInfo; } - public TimeValue getTook() { return new TimeValue(tookInMillis); } @@ -75,6 +79,7 @@ public long getTookInMillis() { public boolean isTimedOut() { return this.timedOut; } + public ShardOperationFailedException[] getShardFailures() { return shardFailures; } @@ -154,36 +159,38 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws } private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "GraphExploreResponsenParser", true, - args -> { - GraphExploreResponse result = new GraphExploreResponse(); - result.vertices = new HashMap<>(); - result.connections = new HashMap<>(); - - result.tookInMillis = (Long) args[0]; - result.timedOut = (Boolean) args[1]; - - @SuppressWarnings("unchecked") - List vertices = (List) args[2]; - @SuppressWarnings("unchecked") - List unresolvedConnections = (List) args[3]; - @SuppressWarnings("unchecked") - List failures = (List) args[4]; - for (Vertex vertex : vertices) { - // reverse-engineer if detailed stats were requested - - // mainly here for testing framework's equality tests - result.returnDetailedInfo = result.returnDetailedInfo || vertex.getFg() > 0; - result.vertices.put(vertex.getId(), vertex); - } - for (UnresolvedConnection unresolvedConnection : unresolvedConnections) { - Connection resolvedConnection = unresolvedConnection.resolve(vertices); - result.connections.put(resolvedConnection.getId(), resolvedConnection); - } - if (failures.size() > 0) { - result.shardFailures = failures.toArray(new ShardSearchFailure[failures.size()]); - } - return result; - }); + "GraphExploreResponsenParser", + true, + args -> { + GraphExploreResponse result = new GraphExploreResponse(); + result.vertices = new HashMap<>(); + result.connections = new HashMap<>(); + + result.tookInMillis = (Long) args[0]; + result.timedOut = (Boolean) args[1]; + + @SuppressWarnings("unchecked") + List vertices = (List) args[2]; + @SuppressWarnings("unchecked") + List unresolvedConnections = (List) args[3]; + @SuppressWarnings("unchecked") + List failures = (List) args[4]; + for (Vertex vertex : vertices) { + // reverse-engineer if detailed stats were requested - + // mainly here for testing framework's equality tests + result.returnDetailedInfo = result.returnDetailedInfo || vertex.getFg() > 0; + result.vertices.put(vertex.getId(), vertex); + } + for (UnresolvedConnection unresolvedConnection : unresolvedConnections) { + Connection resolvedConnection = unresolvedConnection.resolve(vertices); + result.connections.put(resolvedConnection.getId(), resolvedConnection); + } + if (failures.size() > 0) { + result.shardFailures = failures.toArray(new ShardSearchFailure[failures.size()]); + } + return result; + } + ); static { PARSER.declareLong(constructorArg(), TOOK); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/Hop.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/Hop.java index ec8bb81f6a057..e997b7cc3c9cd 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/Hop.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/Hop.java @@ -8,10 +8,10 @@ package org.elasticsearch.client.graph; import org.elasticsearch.client.ValidationException; -import org.elasticsearch.xcontent.ToXContentFragment; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; +import org.elasticsearch.xcontent.ToXContentFragment; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.ArrayList; @@ -119,7 +119,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.field("query"); guidingQuery.toXContent(builder, params); } - if(vertices != null && vertices.size()>0) { + if (vertices != null && vertices.size() > 0) { builder.startArray("vertices"); for (VertexRequest vertexRequest : vertices) { vertexRequest.toXContent(builder, params); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/Vertex.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/Vertex.java index b643631b726f0..d7e1af6deb99f 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/Vertex.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/Vertex.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.graph; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentFragment; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -41,7 +41,6 @@ public class Vertex implements ToXContentFragment { private static final ParseField FG = new ParseField("fg"); private static final ParseField BG = new ParseField("bg"); - public Vertex(String field, String term, double weight, int depth, long bg, long fg) { super(); this.field = field; @@ -59,19 +58,16 @@ public int hashCode() { @Override public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; + if (this == obj) return true; + if (obj == null) return false; + if (getClass() != obj.getClass()) return false; Vertex other = (Vertex) obj; - return depth == other.depth && - weight == other.weight && - bg == other.bg && - fg == other.fg && - Objects.equals(field, other.field) && - Objects.equals(term, other.term); + return depth == other.depth + && weight == other.weight + && bg == other.bg + && fg == other.fg + && Objects.equals(field, other.field) + && Objects.equals(term, other.term); } @@ -89,20 +85,17 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws return builder; } - - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "VertexParser", true, - args -> { - String field = (String) args[0]; - String term = (String) args[1]; - double weight = (Double) args[2]; - int depth = (Integer) args[3]; - Long optionalBg = (Long) args[4]; - Long optionalFg = (Long) args[5]; - long bg = optionalBg == null ? 0 : optionalBg; - long fg = optionalFg == null ? 0 : optionalFg; - return new Vertex(field, term, weight, depth, bg, fg); - }); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("VertexParser", true, args -> { + String field = (String) args[0]; + String term = (String) args[1]; + double weight = (Double) args[2]; + int depth = (Integer) args[3]; + Long optionalBg = (Long) args[4]; + Long optionalFg = (Long) args[5]; + long bg = optionalBg == null ? 0 : optionalBg; + long fg = optionalFg == null ? 0 : optionalFg; + return new Vertex(field, term, weight, depth, bg, fg); + }); static { PARSER.declareString(constructorArg(), FIELD); @@ -117,7 +110,6 @@ static Vertex fromXContent(XContentParser parser) throws IOException { return PARSER.apply(parser, null); } - /** * @return a {@link VertexId} object that uniquely identifies this Vertex */ @@ -132,7 +124,7 @@ public VertexId getId() { * @return a {@link VertexId} that can be used for looking up vertices */ public static VertexId createId(String field, String term) { - return new VertexId(field,term); + return new VertexId(field, term); } @Override @@ -225,17 +217,13 @@ public String getTerm() { @Override public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; VertexId vertexId = (VertexId) o; - if (field != null ? field.equals(vertexId.field) == false : vertexId.field != null) - return false; - if (term != null ? term.equals(vertexId.term) == false : vertexId.term != null) - return false; + if (field != null ? field.equals(vertexId.field) == false : vertexId.field != null) return false; + if (term != null ? term.equals(vertexId.term) == false : vertexId.term != null) return false; return true; } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/VertexRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/VertexRequest.java index 8deba4d81a1e5..92c26b00e9201 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/VertexRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/graph/VertexRequest.java @@ -7,9 +7,9 @@ */ package org.elasticsearch.client.graph; +import org.elasticsearch.client.graph.GraphExploreRequest.TermBoost; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.client.graph.GraphExploreRequest.TermBoost; import java.io.IOException; import java.util.HashMap; @@ -34,7 +34,6 @@ public class VertexRequest implements ToXContentObject { public static final int DEFAULT_SHARD_MIN_DOC_COUNT = 2; private int shardMinDocCount = DEFAULT_SHARD_MIN_DOC_COUNT; - public VertexRequest() { } @@ -131,7 +130,6 @@ public VertexRequest minDocCount(int value) { return this; } - public int shardMinDocCount() { return Math.min(shardMinDocCount, minDocCount); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/AllocateAction.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/AllocateAction.java index cc2ce1eaf64a5..381e83e6c7474 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/AllocateAction.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/AllocateAction.java @@ -7,9 +7,9 @@ */ package org.elasticsearch.client.ilm; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -29,8 +29,11 @@ public class AllocateAction implements LifecycleAction, ToXContentObject { static final ParseField REQUIRE_FIELD = new ParseField("require"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(NAME, true, - a -> new AllocateAction((Integer) a[0], (Map) a[1], (Map) a[2], (Map) a[3])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + true, + a -> new AllocateAction((Integer) a[0], (Map) a[1], (Map) a[2], (Map) a[3]) + ); static { PARSER.declareInt(ConstructingObjectParser.optionalConstructorArg(), NUMBER_OF_REPLICAS_FIELD); @@ -66,8 +69,15 @@ public AllocateAction(Integer numberOfReplicas, Map include, Map } if (this.include.isEmpty() && this.exclude.isEmpty() && this.require.isEmpty() && numberOfReplicas == null) { throw new IllegalArgumentException( - "At least one of " + INCLUDE_FIELD.getPreferredName() + ", " + EXCLUDE_FIELD.getPreferredName() + " or " - + REQUIRE_FIELD.getPreferredName() + "must contain attributes for action " + NAME); + "At least one of " + + INCLUDE_FIELD.getPreferredName() + + ", " + + EXCLUDE_FIELD.getPreferredName() + + " or " + + REQUIRE_FIELD.getPreferredName() + + "must contain attributes for action " + + NAME + ); } if (numberOfReplicas != null && numberOfReplicas < 0) { throw new IllegalArgumentException("[" + NUMBER_OF_REPLICAS_FIELD.getPreferredName() + "] must be >= 0"); @@ -123,10 +133,10 @@ public boolean equals(Object obj) { return false; } AllocateAction other = (AllocateAction) obj; - return Objects.equals(numberOfReplicas, other.numberOfReplicas) && - Objects.equals(include, other.include) && - Objects.equals(exclude, other.exclude) && - Objects.equals(require, other.require); + return Objects.equals(numberOfReplicas, other.numberOfReplicas) + && Objects.equals(include, other.include) + && Objects.equals(exclude, other.exclude) + && Objects.equals(require, other.require); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/DeleteAction.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/DeleteAction.java index f4ab927c1d380..e274d3193fbef 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/DeleteAction.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/DeleteAction.java @@ -25,8 +25,7 @@ public static DeleteAction parse(XContentParser parser) { return PARSER.apply(parser, null); } - public DeleteAction() { - } + public DeleteAction() {} @Override public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/DeleteLifecyclePolicyRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/DeleteLifecyclePolicyRequest.java index e97c1a779ffbb..535f0c99e39bf 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/DeleteLifecyclePolicyRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/DeleteLifecyclePolicyRequest.java @@ -28,7 +28,6 @@ public String getLifecyclePolicy() { return lifecyclePolicy; } - @Override public boolean equals(Object o) { if (this == o) { diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/ExplainLifecycleRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/ExplainLifecycleRequest.java index ad47316d023e5..ec0ba2a4bbc17 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/ExplainLifecycleRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/ExplainLifecycleRequest.java @@ -63,8 +63,7 @@ public boolean equals(Object obj) { return false; } ExplainLifecycleRequest other = (ExplainLifecycleRequest) obj; - return Objects.deepEquals(getIndices(), other.getIndices()) && - Objects.equals(indicesOptions(), other.indicesOptions()); + return Objects.deepEquals(getIndices(), other.getIndices()) && Objects.equals(indicesOptions(), other.indicesOptions()); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/ExplainLifecycleResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/ExplainLifecycleResponse.java index 29ae430aae612..e06e3d0b1b676 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/ExplainLifecycleResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/ExplainLifecycleResponse.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.ilm; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -36,11 +36,18 @@ public class ExplainLifecycleResponse implements ToXContentObject { @SuppressWarnings("unchecked") private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "explain_lifecycle_response", a -> new ExplainLifecycleResponse(((List) a[0]).stream() - .collect(Collectors.toMap(IndexLifecycleExplainResponse::getIndex, Function.identity())))); + "explain_lifecycle_response", + a -> new ExplainLifecycleResponse( + ((List) a[0]).stream() + .collect(Collectors.toMap(IndexLifecycleExplainResponse::getIndex, Function.identity())) + ) + ); static { - PARSER.declareNamedObjects(ConstructingObjectParser.constructorArg(), (p, c, n) -> IndexLifecycleExplainResponse.PARSER.apply(p, c), - INDICES_FIELD); + PARSER.declareNamedObjects( + ConstructingObjectParser.constructorArg(), + (p, c, n) -> IndexLifecycleExplainResponse.PARSER.apply(p, c), + INDICES_FIELD + ); } public static ExplainLifecycleResponse fromXContent(XContentParser parser) { diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/ForceMergeAction.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/ForceMergeAction.java index 7e36c39a37ed3..2db502839b9db 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/ForceMergeAction.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/ForceMergeAction.java @@ -7,9 +7,9 @@ */ package org.elasticsearch.client.ilm; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -21,8 +21,7 @@ public class ForceMergeAction implements LifecycleAction, ToXContentObject { public static final String NAME = "forcemerge"; private static final ParseField MAX_NUM_SEGMENTS_FIELD = new ParseField("max_num_segments"); - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(NAME, - true, a -> { + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(NAME, true, a -> { int maxNumSegments = (int) a[0]; return new ForceMergeAction(maxNumSegments); }); @@ -39,8 +38,7 @@ public static ForceMergeAction parse(XContentParser parser) { public ForceMergeAction(int maxNumSegments) { if (maxNumSegments <= 0) { - throw new IllegalArgumentException("[" + MAX_NUM_SEGMENTS_FIELD.getPreferredName() - + "] must be a positive integer"); + throw new IllegalArgumentException("[" + MAX_NUM_SEGMENTS_FIELD.getPreferredName() + "] must be a positive integer"); } this.maxNumSegments = maxNumSegments; } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/FreezeAction.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/FreezeAction.java index c68e8e56570c4..3064068a69c19 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/FreezeAction.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/FreezeAction.java @@ -24,8 +24,7 @@ public static FreezeAction parse(XContentParser parser) { return PARSER.apply(parser, null); } - public FreezeAction() { - } + public FreezeAction() {} @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/GetLifecyclePolicyRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/GetLifecyclePolicyRequest.java index 265132b81087b..2969bb8c9a132 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/GetLifecyclePolicyRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/GetLifecyclePolicyRequest.java @@ -6,7 +6,6 @@ * Side Public License, v 1. */ - package org.elasticsearch.client.ilm; import org.elasticsearch.client.TimedRequest; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/GetLifecyclePolicyResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/GetLifecyclePolicyResponse.java index 1c3c8ce34cd22..c7320ed108059 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/GetLifecyclePolicyResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/GetLifecyclePolicyResponse.java @@ -6,10 +6,10 @@ * Side Public License, v 1. */ - package org.elasticsearch.client.ilm; import com.carrotsearch.hppc.cursors.ObjectObjectCursor; + import org.elasticsearch.common.collect.ImmutableOpenMap; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/IndexLifecycleExplainResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/IndexLifecycleExplainResponse.java index 6ad9e104864bd..a30468a375db4 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/IndexLifecycleExplainResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/IndexLifecycleExplainResponse.java @@ -8,11 +8,11 @@ package org.elasticsearch.client.ilm; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentType; @@ -44,7 +44,8 @@ public class IndexLifecycleExplainResponse implements ToXContentObject { private static final ParseField AGE_FIELD = new ParseField("age"); public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "index_lifecycle_explain_response", true, + "index_lifecycle_explain_response", + true, a -> new IndexLifecycleExplainResponse( (String) a[0], (boolean) a[1], @@ -58,7 +59,9 @@ public class IndexLifecycleExplainResponse implements ToXContentObject { (Long) a[9], (Long) a[10], (BytesReference) a[11], - (PhaseExecutionInfo) a[12])); + (PhaseExecutionInfo) a[12] + ) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), INDEX_FIELD); PARSER.declareBoolean(ConstructingObjectParser.constructorArg(), MANAGED_BY_ILM_FIELD); @@ -76,8 +79,11 @@ public class IndexLifecycleExplainResponse implements ToXContentObject { builder.copyCurrentStructure(p); return BytesReference.bytes(builder); }, STEP_INFO_FIELD); - PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> PhaseExecutionInfo.parse(p, ""), - PHASE_EXECUTION_INFO); + PARSER.declareObject( + ConstructingObjectParser.optionalConstructorArg(), + (p, c) -> PhaseExecutionInfo.parse(p, ""), + PHASE_EXECUTION_INFO + ); } private final String index; @@ -94,21 +100,56 @@ public class IndexLifecycleExplainResponse implements ToXContentObject { private final BytesReference stepInfo; private final PhaseExecutionInfo phaseExecutionInfo; - public static IndexLifecycleExplainResponse newManagedIndexResponse(String index, String policyName, Long lifecycleDate, - String phase, String action, String step, String failedStep, - Long phaseTime, Long actionTime, Long stepTime, - BytesReference stepInfo, PhaseExecutionInfo phaseExecutionInfo) { - return new IndexLifecycleExplainResponse(index, true, policyName, lifecycleDate, phase, action, step, failedStep, phaseTime, - actionTime, stepTime, stepInfo, phaseExecutionInfo); + public static IndexLifecycleExplainResponse newManagedIndexResponse( + String index, + String policyName, + Long lifecycleDate, + String phase, + String action, + String step, + String failedStep, + Long phaseTime, + Long actionTime, + Long stepTime, + BytesReference stepInfo, + PhaseExecutionInfo phaseExecutionInfo + ) { + return new IndexLifecycleExplainResponse( + index, + true, + policyName, + lifecycleDate, + phase, + action, + step, + failedStep, + phaseTime, + actionTime, + stepTime, + stepInfo, + phaseExecutionInfo + ); } public static IndexLifecycleExplainResponse newUnmanagedIndexResponse(String index) { return new IndexLifecycleExplainResponse(index, false, null, null, null, null, null, null, null, null, null, null, null); } - private IndexLifecycleExplainResponse(String index, boolean managedByILM, String policyName, Long lifecycleDate, - String phase, String action, String step, String failedStep, Long phaseTime, Long actionTime, - Long stepTime, BytesReference stepInfo, PhaseExecutionInfo phaseExecutionInfo) { + private IndexLifecycleExplainResponse( + String index, + boolean managedByILM, + String policyName, + Long lifecycleDate, + String phase, + String action, + String step, + String failedStep, + Long phaseTime, + Long actionTime, + Long stepTime, + BytesReference stepInfo, + PhaseExecutionInfo phaseExecutionInfo + ) { if (managedByILM) { if (policyName == null) { throw new IllegalArgumentException("[" + POLICY_NAME_FIELD.getPreferredName() + "] cannot be null for managed index"); @@ -116,16 +157,37 @@ private IndexLifecycleExplainResponse(String index, boolean managedByILM, String // check to make sure that step details are either all null or all set. long numNull = Stream.of(phase, action, step).filter(Objects::isNull).count(); if (numNull > 0 && numNull < 3) { - throw new IllegalArgumentException("managed index response must have complete step details [" + - PHASE_FIELD.getPreferredName() + "=" + phase + ", " + - ACTION_FIELD.getPreferredName() + "=" + action + ", " + - STEP_FIELD.getPreferredName() + "=" + step + "]"); + throw new IllegalArgumentException( + "managed index response must have complete step details [" + + PHASE_FIELD.getPreferredName() + + "=" + + phase + + ", " + + ACTION_FIELD.getPreferredName() + + "=" + + action + + ", " + + STEP_FIELD.getPreferredName() + + "=" + + step + + "]" + ); } } else { - if (policyName != null || lifecycleDate != null || phase != null || action != null || step != null || failedStep != null - || phaseTime != null || actionTime != null || stepTime != null || stepInfo != null || phaseExecutionInfo != null) { + if (policyName != null + || lifecycleDate != null + || phase != null + || action != null + || step != null + || failedStep != null + || phaseTime != null + || actionTime != null + || stepTime != null + || stepInfo != null + || phaseExecutionInfo != null) { throw new IllegalArgumentException( - "Unmanaged index response must only contain fields: [" + MANAGED_BY_ILM_FIELD + ", " + INDEX_FIELD + "]"); + "Unmanaged index response must only contain fields: [" + MANAGED_BY_ILM_FIELD + ", " + INDEX_FIELD + "]" + ); } } this.index = index; @@ -248,8 +310,21 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws @Override public int hashCode() { - return Objects.hash(index, managedByILM, policyName, lifecycleDate, phase, action, step, failedStep, phaseTime, actionTime, - stepTime, stepInfo, phaseExecutionInfo); + return Objects.hash( + index, + managedByILM, + policyName, + lifecycleDate, + phase, + action, + step, + failedStep, + phaseTime, + actionTime, + stepTime, + stepInfo, + phaseExecutionInfo + ); } @Override @@ -261,19 +336,19 @@ public boolean equals(Object obj) { return false; } IndexLifecycleExplainResponse other = (IndexLifecycleExplainResponse) obj; - return Objects.equals(index, other.index) && - Objects.equals(managedByILM, other.managedByILM) && - Objects.equals(policyName, other.policyName) && - Objects.equals(lifecycleDate, other.lifecycleDate) && - Objects.equals(phase, other.phase) && - Objects.equals(action, other.action) && - Objects.equals(step, other.step) && - Objects.equals(failedStep, other.failedStep) && - Objects.equals(phaseTime, other.phaseTime) && - Objects.equals(actionTime, other.actionTime) && - Objects.equals(stepTime, other.stepTime) && - Objects.equals(stepInfo, other.stepInfo) && - Objects.equals(phaseExecutionInfo, other.phaseExecutionInfo); + return Objects.equals(index, other.index) + && Objects.equals(managedByILM, other.managedByILM) + && Objects.equals(policyName, other.policyName) + && Objects.equals(lifecycleDate, other.lifecycleDate) + && Objects.equals(phase, other.phase) + && Objects.equals(action, other.action) + && Objects.equals(step, other.step) + && Objects.equals(failedStep, other.failedStep) + && Objects.equals(phaseTime, other.phaseTime) + && Objects.equals(actionTime, other.actionTime) + && Objects.equals(stepTime, other.stepTime) + && Objects.equals(stepInfo, other.stepInfo) + && Objects.equals(phaseExecutionInfo, other.phaseExecutionInfo); } @Override @@ -282,4 +357,3 @@ public String toString() { } } - diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/IndexLifecycleNamedXContentProvider.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/IndexLifecycleNamedXContentProvider.java index 17a89ac08b0a4..12bbf762c45c2 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/IndexLifecycleNamedXContentProvider.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/IndexLifecycleNamedXContentProvider.java @@ -8,56 +8,39 @@ package org.elasticsearch.client.ilm; -import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.plugins.spi.NamedXContentProvider; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import java.util.Arrays; import java.util.List; public class IndexLifecycleNamedXContentProvider implements NamedXContentProvider { - @Override public List getNamedXContentParsers() { return Arrays.asList( // ILM - new NamedXContentRegistry.Entry(LifecycleAction.class, - new ParseField(AllocateAction.NAME), - AllocateAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, - new ParseField(DeleteAction.NAME), - DeleteAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, - new ParseField(ForceMergeAction.NAME), - ForceMergeAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, - new ParseField(ReadOnlyAction.NAME), - ReadOnlyAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, - new ParseField(RolloverAction.NAME), - RolloverAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, - new ParseField(ShrinkAction.NAME), - ShrinkAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(AllocateAction.NAME), AllocateAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ForceMergeAction.NAME), ForceMergeAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ReadOnlyAction.NAME), ReadOnlyAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(RolloverAction.NAME), RolloverAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ShrinkAction.NAME), ShrinkAction::parse), + new NamedXContentRegistry.Entry( + LifecycleAction.class, new ParseField(WaitForSnapshotAction.NAME), - WaitForSnapshotAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, - new ParseField(FreezeAction.NAME), - FreezeAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, - new ParseField(SetPriorityAction.NAME), - SetPriorityAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, - new ParseField(MigrateAction.NAME), - MigrateAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, + WaitForSnapshotAction::parse + ), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(FreezeAction.NAME), FreezeAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(SetPriorityAction.NAME), SetPriorityAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(MigrateAction.NAME), MigrateAction::parse), + new NamedXContentRegistry.Entry( + LifecycleAction.class, new ParseField(SearchableSnapshotAction.NAME), - SearchableSnapshotAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, - new ParseField(UnfollowAction.NAME), - UnfollowAction::parse) + SearchableSnapshotAction::parse + ), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(UnfollowAction.NAME), UnfollowAction::parse) ); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/LifecycleManagementStatusRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/LifecycleManagementStatusRequest.java index 2f7a63aa59e50..aa5073af6c4ff 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/LifecycleManagementStatusRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/LifecycleManagementStatusRequest.java @@ -13,5 +13,4 @@ /** * A {@link TimedRequest} to get the current status of index lifecycle management. */ -public class LifecycleManagementStatusRequest extends TimedRequest { -} +public class LifecycleManagementStatusRequest extends TimedRequest {} diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/LifecycleManagementStatusResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/LifecycleManagementStatusResponse.java index a2701ff0ec263..ce8484b0bb39e 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/LifecycleManagementStatusResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/LifecycleManagementStatusResponse.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ilm; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.util.Objects; @@ -23,13 +23,16 @@ public class LifecycleManagementStatusResponse { private static final String OPERATION_MODE = "operation_mode"; @SuppressWarnings("unchecked") private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - OPERATION_MODE, true, a -> new LifecycleManagementStatusResponse((String) a[0])); + OPERATION_MODE, + true, + a -> new LifecycleManagementStatusResponse((String) a[0]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), new ParseField(OPERATION_MODE)); } - //package private for testing + // package private for testing LifecycleManagementStatusResponse(String operationMode) { this.operationMode = OperationMode.fromString(operationMode); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/LifecyclePolicy.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/LifecyclePolicy.java index 3bb9f8c7c5880..d5da9a717ce68 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/LifecyclePolicy.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/LifecyclePolicy.java @@ -7,10 +7,10 @@ */ package org.elasticsearch.client.ilm; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.util.set.Sets; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -33,24 +33,49 @@ public class LifecyclePolicy implements ToXContentObject { static final ParseField PHASES_FIELD = new ParseField("phases"); @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("lifecycle_policy", true, + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "lifecycle_policy", + true, (a, name) -> { List phases = (List) a[0]; Map phaseMap = phases.stream().collect(Collectors.toMap(Phase::getName, Function.identity())); return new LifecyclePolicy(name, phaseMap); - }); + } + ); private static Map> ALLOWED_ACTIONS = new HashMap<>(); static { - PARSER.declareNamedObjects(ConstructingObjectParser.constructorArg(), (p, c, n) -> Phase.parse(p, n), v -> { - throw new IllegalArgumentException("ordered " + PHASES_FIELD.getPreferredName() + " are not supported"); - }, PHASES_FIELD); + PARSER.declareNamedObjects( + ConstructingObjectParser.constructorArg(), + (p, c, n) -> Phase.parse(p, n), + v -> { throw new IllegalArgumentException("ordered " + PHASES_FIELD.getPreferredName() + " are not supported"); }, + PHASES_FIELD + ); ALLOWED_ACTIONS.put("hot", Sets.newHashSet(UnfollowAction.NAME, SetPriorityAction.NAME, RolloverAction.NAME)); - ALLOWED_ACTIONS.put("warm", Sets.newHashSet(UnfollowAction.NAME, SetPriorityAction.NAME, MigrateAction.NAME, AllocateAction.NAME, - ForceMergeAction.NAME, ReadOnlyAction.NAME, ShrinkAction.NAME)); - ALLOWED_ACTIONS.put("cold", Sets.newHashSet(UnfollowAction.NAME, SetPriorityAction.NAME, MigrateAction.NAME, AllocateAction.NAME, - FreezeAction.NAME, SearchableSnapshotAction.NAME)); + ALLOWED_ACTIONS.put( + "warm", + Sets.newHashSet( + UnfollowAction.NAME, + SetPriorityAction.NAME, + MigrateAction.NAME, + AllocateAction.NAME, + ForceMergeAction.NAME, + ReadOnlyAction.NAME, + ShrinkAction.NAME + ) + ); + ALLOWED_ACTIONS.put( + "cold", + Sets.newHashSet( + UnfollowAction.NAME, + SetPriorityAction.NAME, + MigrateAction.NAME, + AllocateAction.NAME, + FreezeAction.NAME, + SearchableSnapshotAction.NAME + ) + ); ALLOWED_ACTIONS.put("delete", Sets.newHashSet(DeleteAction.NAME, WaitForSnapshotAction.NAME)); } @@ -65,17 +90,18 @@ public class LifecyclePolicy implements ToXContentObject { * {@link LifecyclePolicy}. */ public LifecyclePolicy(String name, Map phases) { - phases.values().forEach(phase -> { + phases.values().forEach(phase -> { if (ALLOWED_ACTIONS.containsKey(phase.getName()) == false) { throw new IllegalArgumentException("Lifecycle does not support phase [" + phase.getName() + "]"); } - if (phase.getName().equals("delete") && phase.getActions().size() == 0) { - throw new IllegalArgumentException("phase [" + phase.getName() + "] must define actions"); - } - phase.getActions().forEach((actionName, action) -> { + if (phase.getName().equals("delete") && phase.getActions().size() == 0) { + throw new IllegalArgumentException("phase [" + phase.getName() + "] must define actions"); + } + phase.getActions().forEach((actionName, action) -> { if (ALLOWED_ACTIONS.get(phase.getName()).contains(actionName) == false) { - throw new IllegalArgumentException("invalid action [" + actionName + "] " + - "defined in phase [" + phase.getName() +"]"); + throw new IllegalArgumentException( + "invalid action [" + actionName + "] " + "defined in phase [" + phase.getName() + "]" + ); } }); }); @@ -128,8 +154,7 @@ public boolean equals(Object obj) { return false; } LifecyclePolicy other = (LifecyclePolicy) obj; - return Objects.equals(name, other.name) && - Objects.equals(phases, other.phases); + return Objects.equals(name, other.name) && Objects.equals(phases, other.phases); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/LifecyclePolicyMetadata.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/LifecyclePolicyMetadata.java index a9e1bc8c897b4..3ece349779c04 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/LifecyclePolicyMetadata.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/LifecyclePolicyMetadata.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ilm; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -28,11 +28,13 @@ public class LifecyclePolicyMetadata implements ToXContentObject { @SuppressWarnings("unchecked") public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "policy_metadata", true, - a -> { - LifecyclePolicy policy = (LifecyclePolicy) a[0]; - return new LifecyclePolicyMetadata(policy, (long) a[1], ZonedDateTime.parse((String) a[2]).toInstant().toEpochMilli()); - }); + "policy_metadata", + true, + a -> { + LifecyclePolicy policy = (LifecyclePolicy) a[0]; + return new LifecyclePolicyMetadata(policy, (long) a[1], ZonedDateTime.parse((String) a[2]).toInstant().toEpochMilli()); + } + ); static { PARSER.declareObject(ConstructingObjectParser.constructorArg(), LifecyclePolicy::parse, POLICY); PARSER.declareLong(ConstructingObjectParser.constructorArg(), VERSION); @@ -79,8 +81,10 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.startObject(); builder.field(POLICY.getPreferredName(), policy); builder.field(VERSION.getPreferredName(), version); - builder.field(MODIFIED_DATE.getPreferredName(), - ZonedDateTime.ofInstant(Instant.ofEpochMilli(modifiedDate), ZoneOffset.UTC).toString()); + builder.field( + MODIFIED_DATE.getPreferredName(), + ZonedDateTime.ofInstant(Instant.ofEpochMilli(modifiedDate), ZoneOffset.UTC).toString() + ); builder.endObject(); return builder; } @@ -99,9 +103,9 @@ public boolean equals(Object obj) { return false; } LifecyclePolicyMetadata other = (LifecyclePolicyMetadata) obj; - return Objects.equals(policy, other.policy) && - Objects.equals(version, other.version) && - Objects.equals(modifiedDate, other.modifiedDate); + return Objects.equals(policy, other.policy) + && Objects.equals(version, other.version) + && Objects.equals(modifiedDate, other.modifiedDate); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/MigrateAction.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/MigrateAction.java index 2d621d1996807..4df87a1443481 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/MigrateAction.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/MigrateAction.java @@ -7,9 +7,9 @@ */ package org.elasticsearch.client.ilm; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -22,8 +22,10 @@ public class MigrateAction implements LifecycleAction, ToXContentObject { public static final ParseField ENABLED_FIELD = new ParseField("enabled"); - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(NAME, - a -> new MigrateAction(a[0] == null ? true : (boolean) a[0])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + a -> new MigrateAction(a[0] == null ? true : (boolean) a[0]) + ); static { PARSER.declareBoolean(ConstructingObjectParser.optionalConstructorArg(), ENABLED_FIELD); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/OperationMode.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/OperationMode.java index 4eec03a3671b9..e8338dbd80ec6 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/OperationMode.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/OperationMode.java @@ -50,8 +50,10 @@ public boolean isValidChange(OperationMode nextMode) { public abstract boolean isValidChange(OperationMode nextMode); static OperationMode fromString(String string) { - return EnumSet.allOf(OperationMode.class).stream() - .filter(e -> string.equalsIgnoreCase(e.name())).findFirst() + return EnumSet.allOf(OperationMode.class) + .stream() + .filter(e -> string.equalsIgnoreCase(e.name())) + .findFirst() .orElseThrow(() -> new IllegalArgumentException(String.format(Locale.ROOT, "%s is not a valid operation_mode", string))); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/Phase.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/Phase.java index 351a4f1ae2065..727fdbbdb9de4 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/Phase.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/Phase.java @@ -7,11 +7,11 @@ */ package org.elasticsearch.client.ilm; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -33,16 +33,28 @@ public class Phase implements ToXContentObject { static final ParseField ACTIONS_FIELD = new ParseField("actions"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("phase", true, - (a, name) -> new Phase(name, (TimeValue) a[0], ((List) a[1]).stream() - .collect(Collectors.toMap(LifecycleAction::getName, Function.identity())))); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "phase", + true, + (a, name) -> new Phase( + name, + (TimeValue) a[0], + ((List) a[1]).stream().collect(Collectors.toMap(LifecycleAction::getName, Function.identity())) + ) + ); static { - PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(), - (p, c) -> TimeValue.parseTimeValue(p.text(), MIN_AGE.getPreferredName()), MIN_AGE, ValueType.VALUE); - PARSER.declareNamedObjects(ConstructingObjectParser.constructorArg(), - (p, c, n) -> p.namedObject(LifecycleAction.class, n, null), v -> { - throw new IllegalArgumentException("ordered " + ACTIONS_FIELD.getPreferredName() + " are not supported"); - }, ACTIONS_FIELD); + PARSER.declareField( + ConstructingObjectParser.optionalConstructorArg(), + (p, c) -> TimeValue.parseTimeValue(p.text(), MIN_AGE.getPreferredName()), + MIN_AGE, + ValueType.VALUE + ); + PARSER.declareNamedObjects( + ConstructingObjectParser.constructorArg(), + (p, c, n) -> p.namedObject(LifecycleAction.class, n, null), + v -> { throw new IllegalArgumentException("ordered " + ACTIONS_FIELD.getPreferredName() + " are not supported"); }, + ACTIONS_FIELD + ); } public static Phase parse(XContentParser parser, String name) { @@ -120,9 +132,7 @@ public boolean equals(Object obj) { return false; } Phase other = (Phase) obj; - return Objects.equals(name, other.name) && - Objects.equals(minimumAge, other.minimumAge) && - Objects.equals(actions, other.actions); + return Objects.equals(name, other.name) && Objects.equals(minimumAge, other.minimumAge) && Objects.equals(actions, other.actions); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/PhaseExecutionInfo.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/PhaseExecutionInfo.java index 52dcbb228a4e6..3288c0ef6b6d5 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/PhaseExecutionInfo.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/PhaseExecutionInfo.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.ilm; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -29,8 +29,10 @@ public class PhaseExecutionInfo implements ToXContentObject { private static final ParseField MODIFIED_DATE_IN_MILLIS_FIELD = new ParseField("modified_date_in_millis"); private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "phase_execution_info", true, - (a, name) -> new PhaseExecutionInfo((String) a[0], (Phase) a[1], (long) a[2], (long) a[3])); + "phase_execution_info", + true, + (a, name) -> new PhaseExecutionInfo((String) a[0], (Phase) a[1], (long) a[2], (long) a[3]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), POLICY_NAME_FIELD); PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), Phase::parse, PHASE_DEFINITION_FIELD); @@ -92,10 +94,10 @@ public boolean equals(Object obj) { return false; } PhaseExecutionInfo other = (PhaseExecutionInfo) obj; - return Objects.equals(policyName, other.policyName) && - Objects.equals(phase, other.phase) && - Objects.equals(version, other.version) && - Objects.equals(modifiedDate, other.modifiedDate); + return Objects.equals(policyName, other.policyName) + && Objects.equals(phase, other.phase) + && Objects.equals(version, other.version) + && Objects.equals(modifiedDate, other.modifiedDate); } @Override @@ -116,4 +118,3 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws return builder; } } - diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/ReadOnlyAction.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/ReadOnlyAction.java index 32080899c1277..bd4f0b01901c1 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/ReadOnlyAction.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/ReadOnlyAction.java @@ -24,8 +24,7 @@ public static ReadOnlyAction parse(XContentParser parser) { return PARSER.apply(parser, null); } - public ReadOnlyAction() { - } + public ReadOnlyAction() {} @Override public String getName() { diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/RemoveIndexLifecyclePolicyRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/RemoveIndexLifecyclePolicyRequest.java index 4b2c01da05435..81dcdef26b3f6 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/RemoveIndexLifecyclePolicyRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/RemoveIndexLifecyclePolicyRequest.java @@ -21,8 +21,8 @@ public class RemoveIndexLifecyclePolicyRequest extends TimedRequest { private final IndicesOptions indicesOptions; public RemoveIndexLifecyclePolicyRequest(List indices) { - this.indices = Objects.requireNonNull(indices); - this.indicesOptions = null; + this.indices = Objects.requireNonNull(indices); + this.indicesOptions = null; } public RemoveIndexLifecyclePolicyRequest(List indices, IndicesOptions indicesOptions) { @@ -52,7 +52,6 @@ public boolean equals(Object obj) { return false; } RemoveIndexLifecyclePolicyRequest other = (RemoveIndexLifecyclePolicyRequest) obj; - return Objects.deepEquals(indices, other.indices) && - Objects.equals(indicesOptions, other.indicesOptions); + return Objects.deepEquals(indices, other.indices) && Objects.equals(indicesOptions, other.indicesOptions); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/RemoveIndexLifecyclePolicyResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/RemoveIndexLifecyclePolicyResponse.java index 6ac257970c76a..dc45a11d914e8 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/RemoveIndexLifecyclePolicyResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/RemoveIndexLifecyclePolicyResponse.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ilm; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.util.Collections; @@ -22,7 +22,10 @@ public class RemoveIndexLifecyclePolicyResponse { public static final ParseField FAILED_INDEXES_FIELD = new ParseField("failed_indexes"); @SuppressWarnings("unchecked") public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "change_policy_for_index_response", true, args -> new RemoveIndexLifecyclePolicyResponse((List)args[0])); + "change_policy_for_index_response", + true, + args -> new RemoveIndexLifecyclePolicyResponse((List) args[0]) + ); static { PARSER.declareStringArray(ConstructingObjectParser.constructorArg(), FAILED_INDEXES_FIELD); // Needs to be declared but not used in constructing the response object diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/RetryLifecyclePolicyRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/RetryLifecyclePolicyRequest.java index 27e032457645c..accd76a56e1a9 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/RetryLifecyclePolicyRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/RetryLifecyclePolicyRequest.java @@ -8,10 +8,11 @@ package org.elasticsearch.client.ilm; +import org.elasticsearch.client.TimedRequest; + import java.util.Arrays; import java.util.List; import java.util.Objects; -import org.elasticsearch.client.TimedRequest; public class RetryLifecyclePolicyRequest extends TimedRequest { diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/RolloverAction.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/RolloverAction.java index ed81975246eaa..b83c4d0792704 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/RolloverAction.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/RolloverAction.java @@ -7,12 +7,12 @@ */ package org.elasticsearch.client.ilm; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -27,19 +27,31 @@ public class RolloverAction implements LifecycleAction, ToXContentObject { private static final ParseField MAX_AGE_FIELD = new ParseField("max_age"); private static final ParseField MAX_DOCS_FIELD = new ParseField("max_docs"); - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(NAME, true, - a -> new RolloverAction((ByteSizeValue) a[0], (ByteSizeValue) a[1], (TimeValue) a[2], (Long) a[3])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + true, + a -> new RolloverAction((ByteSizeValue) a[0], (ByteSizeValue) a[1], (TimeValue) a[2], (Long) a[3]) + ); static { - PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(), + PARSER.declareField( + ConstructingObjectParser.optionalConstructorArg(), (p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), MAX_SIZE_FIELD.getPreferredName()), - MAX_SIZE_FIELD, ValueType.VALUE); - PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(), + MAX_SIZE_FIELD, + ValueType.VALUE + ); + PARSER.declareField( + ConstructingObjectParser.optionalConstructorArg(), (p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), MAX_PRIMARY_SHARD_SIZE_FIELD.getPreferredName()), - MAX_PRIMARY_SHARD_SIZE_FIELD, ValueType.VALUE); - PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(), + MAX_PRIMARY_SHARD_SIZE_FIELD, + ValueType.VALUE + ); + PARSER.declareField( + ConstructingObjectParser.optionalConstructorArg(), (p, c) -> TimeValue.parseTimeValue(p.text(), MAX_AGE_FIELD.getPreferredName()), - MAX_AGE_FIELD, ValueType.VALUE); + MAX_AGE_FIELD, + ValueType.VALUE + ); PARSER.declareLong(ConstructingObjectParser.optionalConstructorArg(), MAX_DOCS_FIELD); } @@ -116,10 +128,10 @@ public boolean equals(Object obj) { return false; } RolloverAction other = (RolloverAction) obj; - return Objects.equals(maxSize, other.maxSize) && - Objects.equals(maxPrimaryShardSize, other.maxPrimaryShardSize) && - Objects.equals(maxAge, other.maxAge) && - Objects.equals(maxDocs, other.maxDocs); + return Objects.equals(maxSize, other.maxSize) + && Objects.equals(maxPrimaryShardSize, other.maxPrimaryShardSize) + && Objects.equals(maxAge, other.maxAge) + && Objects.equals(maxDocs, other.maxDocs); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/SearchableSnapshotAction.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/SearchableSnapshotAction.java index 26551f7173b9f..8af8760193772 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/SearchableSnapshotAction.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/SearchableSnapshotAction.java @@ -7,9 +7,9 @@ */ package org.elasticsearch.client.ilm; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -27,9 +27,11 @@ public class SearchableSnapshotAction implements LifecycleAction, ToXContentObje public static final ParseField SNAPSHOT_REPOSITORY = new ParseField("snapshot_repository"); public static final ParseField FORCE_MERGE_INDEX = new ParseField("force_merge_index"); - - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(NAME, - true, a -> new SearchableSnapshotAction((String) a[0], a[1] == null || (boolean) a[1])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + true, + a -> new SearchableSnapshotAction((String) a[0], a[1] == null || (boolean) a[1]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), SNAPSHOT_REPOSITORY); @@ -77,8 +79,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; SearchableSnapshotAction that = (SearchableSnapshotAction) o; - return forceMergeIndex == that.forceMergeIndex && - snapshotRepository.equals(that.snapshotRepository); + return forceMergeIndex == that.forceMergeIndex && snapshotRepository.equals(that.snapshotRepository); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/SetPriorityAction.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/SetPriorityAction.java index 57574bbd0e508..ed4c2cac27e4b 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/SetPriorityAction.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/SetPriorityAction.java @@ -7,11 +7,11 @@ */ package org.elasticsearch.client.ilm; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -27,16 +27,22 @@ public class SetPriorityAction implements LifecycleAction, ToXContentObject { private static final ParseField RECOVERY_PRIORITY_FIELD = new ParseField("priority"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(NAME, true, - a -> new SetPriorityAction((Integer) a[0])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + true, + a -> new SetPriorityAction((Integer) a[0]) + ); - //package private for testing + // package private for testing final Integer recoveryPriority; static { - PARSER.declareField(ConstructingObjectParser.constructorArg(), - (p) -> p.currentToken() == XContentParser.Token.VALUE_NULL ? null : p.intValue() - , RECOVERY_PRIORITY_FIELD, ObjectParser.ValueType.INT_OR_NULL); + PARSER.declareField( + ConstructingObjectParser.constructorArg(), + (p) -> p.currentToken() == XContentParser.Token.VALUE_NULL ? null : p.intValue(), + RECOVERY_PRIORITY_FIELD, + ObjectParser.ValueType.INT_OR_NULL + ); } public static SetPriorityAction parse(XContentParser parser) { diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/ShrinkAction.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/ShrinkAction.java index fdfabd617394e..11ea156556d51 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/ShrinkAction.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/ShrinkAction.java @@ -7,12 +7,12 @@ */ package org.elasticsearch.client.ilm; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -25,14 +25,20 @@ public class ShrinkAction implements LifecycleAction, ToXContentObject { private static final ParseField NUMBER_OF_SHARDS_FIELD = new ParseField("number_of_shards"); private static final ParseField MAX_PRIMARY_SHARD_SIZE = new ParseField("max_primary_shard_size"); - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(NAME, true, a -> new ShrinkAction((Integer) a[0], (ByteSizeValue) a[1])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + true, + a -> new ShrinkAction((Integer) a[0], (ByteSizeValue) a[1]) + ); static { PARSER.declareInt(ConstructingObjectParser.optionalConstructorArg(), NUMBER_OF_SHARDS_FIELD); - PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(), + PARSER.declareField( + ConstructingObjectParser.optionalConstructorArg(), (p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), MAX_PRIMARY_SHARD_SIZE.getPreferredName()), - MAX_PRIMARY_SHARD_SIZE, ObjectParser.ValueType.STRING); + MAX_PRIMARY_SHARD_SIZE, + ObjectParser.ValueType.STRING + ); } private Integer numberOfShards; @@ -94,8 +100,7 @@ public boolean equals(Object o) { if (o == null || getClass() != o.getClass()) return false; ShrinkAction that = (ShrinkAction) o; - return Objects.equals(numberOfShards, that.numberOfShards) && - Objects.equals(maxPrimaryShardSize, that.maxPrimaryShardSize); + return Objects.equals(numberOfShards, that.numberOfShards) && Objects.equals(maxPrimaryShardSize, that.maxPrimaryShardSize); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/StartILMRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/StartILMRequest.java index 7bc42a61a4225..50d8959134da8 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/StartILMRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/StartILMRequest.java @@ -12,8 +12,7 @@ public class StartILMRequest extends TimedRequest { - public StartILMRequest() { - } + public StartILMRequest() {} @Override public int hashCode() { diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/StopILMRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/StopILMRequest.java index 94a5b741de413..a371dd1fb4364 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/StopILMRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/StopILMRequest.java @@ -12,8 +12,7 @@ public class StopILMRequest extends TimedRequest { - public StopILMRequest() { - } + public StopILMRequest() {} @Override public int hashCode() { diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/WaitForSnapshotAction.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/WaitForSnapshotAction.java index 10f855c76b47e..8de923d62f7a0 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/WaitForSnapshotAction.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ilm/WaitForSnapshotAction.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.ilm; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -27,8 +27,11 @@ public class WaitForSnapshotAction implements LifecycleAction, ToXContentObject public static final String NAME = "wait_for_snapshot"; public static final ParseField POLICY_FIELD = new ParseField("policy"); - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(NAME, - true, a -> new WaitForSnapshotAction((String) a[0])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + true, + a -> new WaitForSnapshotAction((String) a[0]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), POLICY_FIELD); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/AnalyzeRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/AnalyzeRequest.java index 5044286c89ec6..a6b8c16016f8c 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/AnalyzeRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/AnalyzeRequest.java @@ -164,8 +164,13 @@ private AnalyzeRequest(String index, String analyzer, String normalizer, String this.text = text; } - private AnalyzeRequest(String index, NameOrDefinition tokenizer, List charFilters, - List tokenFilters, String... text) { + private AnalyzeRequest( + String index, + NameOrDefinition tokenizer, + List charFilters, + List tokenFilters, + String... text + ) { this.index = index; this.analyzer = null; this.normalizer = null; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/AnalyzeResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/AnalyzeResponse.java index bcd8df71f0ae1..101550f9ff442 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/AnalyzeResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/AnalyzeResponse.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.indices; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -40,13 +40,13 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; AnalyzeResponse.AnalyzeToken that = (AnalyzeResponse.AnalyzeToken) o; - return startOffset == that.startOffset && - endOffset == that.endOffset && - position == that.position && - positionLength == that.positionLength && - Objects.equals(term, that.term) && - Objects.equals(attributes, that.attributes) && - Objects.equals(type, that.type); + return startOffset == that.startOffset + && endOffset == that.endOffset + && position == that.position + && positionLength == that.positionLength + && Objects.equals(term, that.term) + && Objects.equals(attributes, that.attributes) + && Objects.equals(type, that.type); } @Override @@ -110,8 +110,11 @@ private void setAttribute(String key, Object value) { this.attributes.put(key, value); } - private static final ObjectParser PARSER - = new ObjectParser<>("analyze_token", AnalyzeToken::setAttribute, AnalyzeToken::new); + private static final ObjectParser PARSER = new ObjectParser<>( + "analyze_token", + AnalyzeToken::setAttribute, + AnalyzeToken::new + ); static { PARSER.declareString(AnalyzeToken::setTerm, new ParseField("token")); PARSER.declareString(AnalyzeToken::setType, new ParseField("type")); @@ -143,8 +146,11 @@ public DetailAnalyzeResponse detail() { } @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("analyze_response", - true, args -> new AnalyzeResponse((List) args[0], (DetailAnalyzeResponse) args[1])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "analyze_response", + true, + args -> new AnalyzeResponse((List) args[0], (DetailAnalyzeResponse) args[1]) + ); static { PARSER.declareObjectArray(optionalConstructorArg(), AnalyzeToken.PARSER, new ParseField(TOKENS)); @@ -160,8 +166,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; AnalyzeResponse that = (AnalyzeResponse) o; - return Objects.equals(detail, that.detail) && - Objects.equals(tokens, that.tokens); + return Objects.equals(detail, that.detail) && Objects.equals(tokens, that.tokens); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/CloseIndexResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/CloseIndexResponse.java index 413ab82c332b2..8c9719fbe3df8 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/CloseIndexResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/CloseIndexResponse.java @@ -10,11 +10,11 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.action.support.DefaultShardOperationFailedException; import org.elasticsearch.action.support.master.ShardsAcknowledgedResponse; +import org.elasticsearch.common.xcontent.XContentParserUtils; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.XContentParserUtils; import java.util.List; import java.util.Objects; @@ -27,13 +27,16 @@ public class CloseIndexResponse extends ShardsAcknowledgedResponse { @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("close_index_response", - true, args -> { - boolean acknowledged = (boolean) args[0]; - boolean shardsAcknowledged = args[1] != null ? (boolean) args[1] : acknowledged; - List indices = args[2] != null ? (List) args[2] : emptyList(); - return new CloseIndexResponse(acknowledged, shardsAcknowledged, indices); - }); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "close_index_response", + true, + args -> { + boolean acknowledged = (boolean) args[0]; + boolean shardsAcknowledged = args[1] != null ? (boolean) args[1] : acknowledged; + List indices = args[2] != null ? (List) args[2] : emptyList(); + return new CloseIndexResponse(acknowledged, shardsAcknowledged, indices); + } + ); static { declareAcknowledgedField(PARSER); @@ -59,7 +62,9 @@ public static CloseIndexResponse fromXContent(final XContentParser parser) { public static class IndexResult { @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("index_result", true, + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "index_result", + true, (args, index) -> { Exception exception = (Exception) args[1]; if (exception != null) { @@ -73,7 +78,8 @@ public static class IndexResult { } assert (boolean) args[0]; return new IndexResult(index); - }); + } + ); static { PARSER.declareBoolean(optionalConstructorArg(), new ParseField("closed")); PARSER.declareObject(optionalConstructorArg(), (p, c) -> { @@ -83,8 +89,11 @@ public static class IndexResult { XContentParserUtils.ensureExpectedToken(XContentParser.Token.END_OBJECT, p.nextToken(), p); return e; }, new ParseField("exception")); - PARSER.declareNamedObjects(optionalConstructorArg(), - (p, c, id) -> ShardResult.fromXContent(p, id), new ParseField("failedShards")); + PARSER.declareNamedObjects( + optionalConstructorArg(), + (p, c, id) -> ShardResult.fromXContent(p, id), + new ParseField("failedShards") + ); } private final String index; @@ -143,11 +152,14 @@ static IndexResult fromXContent(final XContentParser parser, final String name) public static class ShardResult { @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("shard_result", true, + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "shard_result", + true, (arg, id) -> { Failure[] failures = arg[0] != null ? ((List) arg[0]).toArray(new Failure[0]) : new Failure[0]; return new ShardResult(Integer.parseInt(id), failures); - }); + } + ); static { PARSER.declareObjectArray(optionalConstructorArg(), (p, c) -> Failure.PARSER.apply(p, null), new ParseField("failures")); @@ -179,8 +191,11 @@ static ShardResult fromXContent(final XContentParser parser, final String id) { public static class Failure extends DefaultShardOperationFailedException { - static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("failure", true, - arg -> new Failure((String) arg[0], (int) arg[1], (Throwable) arg[2], (String) arg[3])); + static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "failure", + true, + arg -> new Failure((String) arg[0], (int) arg[1], (Throwable) arg[2], (String) arg[3]) + ); static { declareFields(PARSER); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/CreateIndexRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/CreateIndexRequest.java index 758eb480935cb..594f23234c7df 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/CreateIndexRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/CreateIndexRequest.java @@ -14,17 +14,17 @@ import org.elasticsearch.action.support.ActiveShardCount; import org.elasticsearch.client.TimedRequest; import org.elasticsearch.client.Validatable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; @@ -157,7 +157,7 @@ public CreateIndexRequest mapping(XContentBuilder source) { * @param source The mapping source */ public CreateIndexRequest mapping(Map source) { - try { + try { XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); builder.map(source); return mapping(BytesReference.bytes(builder), builder.contentType()); @@ -217,15 +217,21 @@ public CreateIndexRequest aliases(String source, XContentType contentType) { */ public CreateIndexRequest aliases(BytesReference source, XContentType contentType) { // EMPTY is safe here because we never call namedObject - try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, source, contentType)) { - //move to the first alias + try ( + XContentParser parser = XContentHelper.createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + source, + contentType + ) + ) { + // move to the first alias parser.nextToken(); while ((parser.nextToken()) != XContentParser.Token.END_OBJECT) { alias(Alias.fromXContent(parser)); } return this; - } catch(IOException e) { + } catch (IOException e) { throw new ElasticsearchParseException("Failed to parse aliases", e); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/CreateIndexResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/CreateIndexResponse.java index 334813404c338..95d8e1143c064 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/CreateIndexResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/CreateIndexResponse.java @@ -9,9 +9,9 @@ package org.elasticsearch.client.indices; import org.elasticsearch.action.support.master.ShardsAcknowledgedResponse; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.util.Objects; @@ -24,8 +24,11 @@ public class CreateIndexResponse extends ShardsAcknowledgedResponse { private static final ParseField INDEX = new ParseField("index"); - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("create_index", - true, args -> new CreateIndexResponse((boolean) args[0], (boolean) args[1], (String) args[2])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "create_index", + true, + args -> new CreateIndexResponse((boolean) args[0], (boolean) args[1], (String) args[2]) + ); static { declareAcknowledgedAndShardsAcknowledgedFields(PARSER); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/DataStream.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/DataStream.java index e0ca3382c2f22..047b13b0d2ef4 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/DataStream.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/DataStream.java @@ -9,8 +9,8 @@ import org.elasticsearch.cluster.health.ClusterHealthStatus; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -36,9 +36,19 @@ public final class DataStream { private final Map metadata; private final boolean allowCustomRouting; - public DataStream(String name, String timeStampField, List indices, long generation, ClusterHealthStatus dataStreamStatus, - @Nullable String indexTemplate, @Nullable String ilmPolicyName, @Nullable Map metadata, - boolean hidden, boolean system, boolean allowCustomRouting) { + public DataStream( + String name, + String timeStampField, + List indices, + long generation, + ClusterHealthStatus dataStreamStatus, + @Nullable String indexTemplate, + @Nullable String ilmPolicyName, + @Nullable Map metadata, + boolean hidden, + boolean system, + boolean allowCustomRouting + ) { this.name = name; this.timeStampField = timeStampField; this.indices = indices; @@ -109,24 +119,33 @@ public boolean allowsCustomRouting() { public static final ParseField ALLOW_CUSTOM_ROUTING = new ParseField("allow_custom_routing"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("data_stream", - args -> { - String dataStreamName = (String) args[0]; - String timeStampField = (String) ((Map) args[1]).get("name"); - List indices = - ((List>) args[2]).stream().map(m -> m.get("index_name")).collect(Collectors.toList()); - Long generation = (Long) args[3]; - String statusStr = (String) args[4]; - ClusterHealthStatus status = ClusterHealthStatus.fromString(statusStr); - String indexTemplate = (String) args[5]; - String ilmPolicy = (String) args[6]; - Map metadata = (Map) args[7]; - boolean hidden = args[8] != null && (boolean) args[8]; - boolean system = args[9] != null && (boolean) args[9]; - boolean allowCustomRouting = args[10] != null && (boolean) args[10]; - return new DataStream(dataStreamName, timeStampField, indices, generation, status, indexTemplate, ilmPolicy, metadata, hidden, - system, allowCustomRouting); - }); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("data_stream", args -> { + String dataStreamName = (String) args[0]; + String timeStampField = (String) ((Map) args[1]).get("name"); + List indices = ((List>) args[2]).stream().map(m -> m.get("index_name")).collect(Collectors.toList()); + Long generation = (Long) args[3]; + String statusStr = (String) args[4]; + ClusterHealthStatus status = ClusterHealthStatus.fromString(statusStr); + String indexTemplate = (String) args[5]; + String ilmPolicy = (String) args[6]; + Map metadata = (Map) args[7]; + boolean hidden = args[8] != null && (boolean) args[8]; + boolean system = args[9] != null && (boolean) args[9]; + boolean allowCustomRouting = args[10] != null && (boolean) args[10]; + return new DataStream( + dataStreamName, + timeStampField, + indices, + generation, + status, + indexTemplate, + ilmPolicy, + metadata, + hidden, + system, + allowCustomRouting + ); + }); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), NAME_FIELD); @@ -151,22 +170,33 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; DataStream that = (DataStream) o; - return generation == that.generation && - name.equals(that.name) && - timeStampField.equals(that.timeStampField) && - indices.equals(that.indices) && - dataStreamStatus == that.dataStreamStatus && - hidden == that.hidden && - system == that.system && - Objects.equals(indexTemplate, that.indexTemplate) && - Objects.equals(ilmPolicyName, that.ilmPolicyName) && - Objects.equals(metadata, that.metadata) && - allowCustomRouting == that.allowCustomRouting; + return generation == that.generation + && name.equals(that.name) + && timeStampField.equals(that.timeStampField) + && indices.equals(that.indices) + && dataStreamStatus == that.dataStreamStatus + && hidden == that.hidden + && system == that.system + && Objects.equals(indexTemplate, that.indexTemplate) + && Objects.equals(ilmPolicyName, that.ilmPolicyName) + && Objects.equals(metadata, that.metadata) + && allowCustomRouting == that.allowCustomRouting; } @Override public int hashCode() { - return Objects.hash(name, timeStampField, indices, generation, dataStreamStatus, indexTemplate, ilmPolicyName, metadata, hidden, - system, allowCustomRouting); + return Objects.hash( + name, + timeStampField, + indices, + generation, + dataStreamStatus, + indexTemplate, + ilmPolicyName, + metadata, + hidden, + system, + allowCustomRouting + ); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/DataStreamsStatsResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/DataStreamsStatsResponse.java index 394473ee9d33e..1fa24a0b256bd 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/DataStreamsStatsResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/DataStreamsStatsResponse.java @@ -9,10 +9,10 @@ package org.elasticsearch.client.indices; import org.elasticsearch.client.core.BroadcastResponse; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -30,8 +30,13 @@ public class DataStreamsStatsResponse extends BroadcastResponse { private final ByteSizeValue totalStoreSize; private final Map dataStreams; - protected DataStreamsStatsResponse(Shards shards, int dataStreamCount, int backingIndices, ByteSizeValue totalStoreSize, - Map dataStreams) { + protected DataStreamsStatsResponse( + Shards shards, + int dataStreamCount, + int backingIndices, + ByteSizeValue totalStoreSize, + Map dataStreams + ) { super(shards); this.dataStreamCount = dataStreamCount; this.backingIndices = backingIndices; @@ -49,38 +54,52 @@ protected DataStreamsStatsResponse(Shards shards, int dataStreamCount, int backi @SuppressWarnings("unchecked") private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "data_streams_stats", true, arg -> { - Shards shards = (Shards) arg[0]; - Integer dataStreamCount = ((Integer) arg[1]); - Integer backingIndices = ((Integer) arg[2]); - ByteSizeValue totalStoreSize = ((ByteSizeValue) arg[3]); - Map dataStreams = new HashMap<>(); - for (DataStreamStats dataStreamStats : ((List) arg[4])) { - dataStreams.put(dataStreamStats.dataStream, dataStreamStats); + "data_streams_stats", + true, + arg -> { + Shards shards = (Shards) arg[0]; + Integer dataStreamCount = ((Integer) arg[1]); + Integer backingIndices = ((Integer) arg[2]); + ByteSizeValue totalStoreSize = ((ByteSizeValue) arg[3]); + Map dataStreams = new HashMap<>(); + for (DataStreamStats dataStreamStats : ((List) arg[4])) { + dataStreams.put(dataStreamStats.dataStream, dataStreamStats); + } + return new DataStreamsStatsResponse(shards, dataStreamCount, backingIndices, totalStoreSize, dataStreams); } - return new DataStreamsStatsResponse(shards, dataStreamCount, backingIndices, totalStoreSize, dataStreams); - }); + ); private static final ConstructingObjectParser ENTRY_PARSER = new ConstructingObjectParser<>( - "data_streams_stats.entry", true, arg -> { - String dataStream = ((String) arg[0]); - Integer backingIndices = ((Integer) arg[1]); - ByteSizeValue storeSize = ((ByteSizeValue) arg[2]); - Long maximumTimestamp = ((Long) arg[3]); - return new DataStreamStats(dataStream, backingIndices, storeSize, maximumTimestamp); - }); + "data_streams_stats.entry", + true, + arg -> { + String dataStream = ((String) arg[0]); + Integer backingIndices = ((Integer) arg[1]); + ByteSizeValue storeSize = ((ByteSizeValue) arg[2]); + Long maximumTimestamp = ((Long) arg[3]); + return new DataStreamStats(dataStream, backingIndices, storeSize, maximumTimestamp); + } + ); static { declareShardsField(PARSER); PARSER.declareInt(constructorArg(), DATA_STREAM_COUNT); PARSER.declareInt(constructorArg(), BACKING_INDICES); - PARSER.declareField(constructorArg(), (p, c) -> new ByteSizeValue(p.longValue()), TOTAL_STORE_SIZE_BYTES, - ObjectParser.ValueType.VALUE); + PARSER.declareField( + constructorArg(), + (p, c) -> new ByteSizeValue(p.longValue()), + TOTAL_STORE_SIZE_BYTES, + ObjectParser.ValueType.VALUE + ); PARSER.declareObjectArray(constructorArg(), ENTRY_PARSER, DATA_STREAMS); ENTRY_PARSER.declareString(constructorArg(), DATA_STREAM); ENTRY_PARSER.declareInt(constructorArg(), BACKING_INDICES); - ENTRY_PARSER.declareField(constructorArg(), (p, c) -> new ByteSizeValue(p.longValue()), STORE_SIZE_BYTES, - ObjectParser.ValueType.VALUE); + ENTRY_PARSER.declareField( + constructorArg(), + (p, c) -> new ByteSizeValue(p.longValue()), + STORE_SIZE_BYTES, + ObjectParser.ValueType.VALUE + ); ENTRY_PARSER.declareLong(constructorArg(), MAXIMUM_TIMESTAMP); } @@ -113,10 +132,10 @@ public boolean equals(Object obj) { return false; } DataStreamsStatsResponse that = (DataStreamsStatsResponse) obj; - return dataStreamCount == that.dataStreamCount && - backingIndices == that.backingIndices && - Objects.equals(totalStoreSize, that.totalStoreSize) && - Objects.equals(dataStreams, that.dataStreams); + return dataStreamCount == that.dataStreamCount + && backingIndices == that.backingIndices + && Objects.equals(totalStoreSize, that.totalStoreSize) + && Objects.equals(dataStreams, that.dataStreams); } @Override @@ -126,12 +145,16 @@ public int hashCode() { @Override public String toString() { - return "DataStreamsStatsResponse{" + - "dataStreamCount=" + dataStreamCount + - ", backingIndices=" + backingIndices + - ", totalStoreSize=" + totalStoreSize + - ", dataStreams=" + dataStreams + - '}'; + return "DataStreamsStatsResponse{" + + "dataStreamCount=" + + dataStreamCount + + ", backingIndices=" + + backingIndices + + ", totalStoreSize=" + + totalStoreSize + + ", dataStreams=" + + dataStreams + + '}'; } public static class DataStreamStats { @@ -173,10 +196,10 @@ public boolean equals(Object obj) { return false; } DataStreamStats that = (DataStreamStats) obj; - return backingIndices == that.backingIndices && - maximumTimestamp == that.maximumTimestamp && - Objects.equals(dataStream, that.dataStream) && - Objects.equals(storeSize, that.storeSize); + return backingIndices == that.backingIndices + && maximumTimestamp == that.maximumTimestamp + && Objects.equals(dataStream, that.dataStream) + && Objects.equals(storeSize, that.storeSize); } @Override @@ -186,12 +209,17 @@ public int hashCode() { @Override public String toString() { - return "DataStreamStats{" + - "dataStream='" + dataStream + '\'' + - ", backingIndices=" + backingIndices + - ", storeSize=" + storeSize + - ", maximumTimestamp=" + maximumTimestamp + - '}'; + return "DataStreamStats{" + + "dataStream='" + + dataStream + + '\'' + + ", backingIndices=" + + backingIndices + + ", storeSize=" + + storeSize + + ", maximumTimestamp=" + + maximumTimestamp + + '}'; } } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/DetailAnalyzeResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/DetailAnalyzeResponse.java index 914ab44f9207e..0a76137b3a766 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/DetailAnalyzeResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/DetailAnalyzeResponse.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.indices; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -29,16 +29,18 @@ public class DetailAnalyzeResponse { private final AnalyzeTokenList tokenizer; private final AnalyzeTokenList[] tokenfilters; - private DetailAnalyzeResponse(boolean customAnalyzer, - AnalyzeTokenList analyzer, - List charfilters, - AnalyzeTokenList tokenizer, - List tokenfilters) { + private DetailAnalyzeResponse( + boolean customAnalyzer, + AnalyzeTokenList analyzer, + List charfilters, + AnalyzeTokenList tokenizer, + List tokenfilters + ) { this.customAnalyzer = customAnalyzer; this.analyzer = analyzer; - this.charfilters = charfilters == null ? null : charfilters.toArray(new CharFilteredText[]{}); + this.charfilters = charfilters == null ? null : charfilters.toArray(new CharFilteredText[] {}); this.tokenizer = tokenizer; - this.tokenfilters = tokenfilters == null ? null : tokenfilters.toArray(new AnalyzeTokenList[]{}); + this.tokenfilters = tokenfilters == null ? null : tokenfilters.toArray(new AnalyzeTokenList[] {}); } public AnalyzeTokenList analyzer() { @@ -62,11 +64,11 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; DetailAnalyzeResponse that = (DetailAnalyzeResponse) o; - return customAnalyzer == that.customAnalyzer && - Objects.equals(analyzer, that.analyzer) && - Arrays.equals(charfilters, that.charfilters) && - Objects.equals(tokenizer, that.tokenizer) && - Arrays.equals(tokenfilters, that.tokenfilters); + return customAnalyzer == that.customAnalyzer + && Objects.equals(analyzer, that.analyzer) + && Arrays.equals(charfilters, that.charfilters) + && Objects.equals(tokenizer, that.tokenizer) + && Arrays.equals(tokenfilters, that.tokenfilters); } @Override @@ -78,13 +80,17 @@ public int hashCode() { } @SuppressWarnings("unchecked") - static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("detail", - true, args -> new DetailAnalyzeResponse( - (boolean) args[0], - (AnalyzeTokenList) args[1], - (List)args[2], - (AnalyzeTokenList) args[3], - (List)args[4])); + static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "detail", + true, + args -> new DetailAnalyzeResponse( + (boolean) args[0], + (AnalyzeTokenList) args[1], + (List) args[2], + (AnalyzeTokenList) args[3], + (List) args[4] + ) + ); static { PARSER.declareBoolean(constructorArg(), new ParseField("custom_analyzer")); @@ -107,8 +113,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; AnalyzeTokenList that = (AnalyzeTokenList) o; - return Objects.equals(name, that.name) && - Arrays.equals(tokens, that.tokens); + return Objects.equals(name, that.name) && Arrays.equals(tokens, that.tokens); } @Override @@ -120,7 +125,7 @@ public int hashCode() { public AnalyzeTokenList(String name, List tokens) { this.name = name; - this.tokens = tokens.toArray(new AnalyzeResponse.AnalyzeToken[]{}); + this.tokens = tokens.toArray(new AnalyzeResponse.AnalyzeToken[] {}); } public String getName() { @@ -132,14 +137,15 @@ public AnalyzeResponse.AnalyzeToken[] getTokens() { } @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("token_list", - true, args -> new AnalyzeTokenList((String) args[0], - (List)args[1])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "token_list", + true, + args -> new AnalyzeTokenList((String) args[0], (List) args[1]) + ); static { PARSER.declareString(constructorArg(), new ParseField("name")); - PARSER.declareObjectArray(constructorArg(), (p, c) -> AnalyzeResponse.AnalyzeToken.fromXContent(p), - new ParseField("tokens")); + PARSER.declareObjectArray(constructorArg(), (p, c) -> AnalyzeResponse.AnalyzeToken.fromXContent(p), new ParseField("tokens")); } public static AnalyzeTokenList fromXContent(XContentParser parser) throws IOException { @@ -170,8 +176,11 @@ public String[] getTexts() { } @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("char_filtered_text", - true, args -> new CharFilteredText((String) args[0], ((List) args[1]).toArray(new String[0]))); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "char_filtered_text", + true, + args -> new CharFilteredText((String) args[0], ((List) args[1]).toArray(new String[0])) + ); static { PARSER.declareString(constructorArg(), new ParseField("name")); @@ -187,8 +196,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; CharFilteredText that = (CharFilteredText) o; - return Objects.equals(name, that.name) && - Arrays.equals(texts, that.texts); + return Objects.equals(name, that.name) && Arrays.equals(texts, that.texts); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetComponentTemplatesResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetComponentTemplatesResponse.java index d4b04654e1143..ffb34a71643ea 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetComponentTemplatesResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetComponentTemplatesResponse.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.indices; import org.elasticsearch.cluster.metadata.ComponentTemplate; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -20,7 +20,6 @@ import java.util.Objects; import java.util.stream.Collectors; - public class GetComponentTemplatesResponse { public static final ParseField NAME = new ParseField("name"); @@ -28,14 +27,18 @@ public class GetComponentTemplatesResponse { public static final ParseField COMPONENT_TEMPLATE = new ParseField("component_template"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser, Void> PARSER = - new ConstructingObjectParser<>("component_templates", false, - a -> ((List) a[0]).stream().collect(Collectors.toMap(n -> n.name, n -> n.componentTemplate, - (n1, n2) -> n1, LinkedHashMap::new))); - - private static final ConstructingObjectParser INNER_PARSER = - new ConstructingObjectParser<>("named_component_template", false, - a -> new NamedComponentTemplate((String) a[0], (ComponentTemplate) a[1])); + private static final ConstructingObjectParser, Void> PARSER = new ConstructingObjectParser<>( + "component_templates", + false, + a -> ((List) a[0]).stream() + .collect(Collectors.toMap(n -> n.name, n -> n.componentTemplate, (n1, n2) -> n1, LinkedHashMap::new)) + ); + + private static final ConstructingObjectParser INNER_PARSER = new ConstructingObjectParser<>( + "named_component_template", + false, + a -> new NamedComponentTemplate((String) a[0], (ComponentTemplate) a[1]) + ); static { INNER_PARSER.declareString(ConstructingObjectParser.constructorArg(), NAME); @@ -68,7 +71,6 @@ public Map getComponentTemplates() { return componentTemplates; } - public static GetComponentTemplatesResponse fromXContent(XContentParser parser) throws IOException { return new GetComponentTemplatesResponse(PARSER.apply(parser, null)); } @@ -93,5 +95,4 @@ public boolean equals(Object obj) { return Objects.equals(componentTemplates, other.componentTemplates); } - } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetComposableIndexTemplatesResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetComposableIndexTemplatesResponse.java index 52a1899ce342c..2d9d715bf29d6 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetComposableIndexTemplatesResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetComposableIndexTemplatesResponse.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.indices; import org.elasticsearch.cluster.metadata.ComposableIndexTemplate; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -20,7 +20,6 @@ import java.util.Objects; import java.util.stream.Collectors; - public class GetComposableIndexTemplatesResponse { public static final ParseField NAME = new ParseField("name"); @@ -28,14 +27,18 @@ public class GetComposableIndexTemplatesResponse { public static final ParseField INDEX_TEMPLATE = new ParseField("index_template"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser, Void> PARSER = - new ConstructingObjectParser<>("index_templates", false, - a -> ((List) a[0]).stream().collect(Collectors.toMap(n -> n.name, n -> n.indexTemplate, - (n1, n2) -> n1, LinkedHashMap::new))); - - private static final ConstructingObjectParser INNER_PARSER = - new ConstructingObjectParser<>("named_index_template", false, - a -> new NamedIndexTemplate((String) a[0], (ComposableIndexTemplate) a[1])); + private static final ConstructingObjectParser, Void> PARSER = new ConstructingObjectParser<>( + "index_templates", + false, + a -> ((List) a[0]).stream() + .collect(Collectors.toMap(n -> n.name, n -> n.indexTemplate, (n1, n2) -> n1, LinkedHashMap::new)) + ); + + private static final ConstructingObjectParser INNER_PARSER = new ConstructingObjectParser<>( + "named_index_template", + false, + a -> new NamedIndexTemplate((String) a[0], (ComposableIndexTemplate) a[1]) + ); static { INNER_PARSER.declareString(ConstructingObjectParser.constructorArg(), NAME); @@ -68,7 +71,6 @@ public Map getIndexTemplates() { return indexTemplates; } - public static GetComposableIndexTemplatesResponse fromXContent(XContentParser parser) throws IOException { return new GetComposableIndexTemplatesResponse(PARSER.apply(parser, null)); } @@ -93,5 +95,4 @@ public boolean equals(Object obj) { return Objects.equals(indexTemplates, other.indexTemplates); } - } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetDataStreamResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetDataStreamResponse.java index 7477614e39738..344a0ce115937 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetDataStreamResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetDataStreamResponse.java @@ -16,7 +16,6 @@ import java.util.List; import java.util.Objects; - public class GetDataStreamResponse { private final List dataStreams; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetFieldMappingsResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetFieldMappingsResponse.java index db56467968ab4..3683d46b359db 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetFieldMappingsResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetFieldMappingsResponse.java @@ -8,32 +8,35 @@ package org.elasticsearch.client.indices; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.index.mapper.Mapper; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.index.mapper.Mapper; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Objects; +import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken; import static org.elasticsearch.xcontent.ConstructingObjectParser.optionalConstructorArg; import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; -import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken; /** Response object for {@link GetFieldMappingsRequest} API */ public class GetFieldMappingsResponse { private static final ParseField MAPPINGS = new ParseField("mappings"); - private static final ObjectParser, String> PARSER = - new ObjectParser<>(MAPPINGS.getPreferredName(), true, HashMap::new); + private static final ObjectParser, String> PARSER = new ObjectParser<>( + MAPPINGS.getPreferredName(), + true, + HashMap::new + ); static { PARSER.declareField((p, fieldMappings, index) -> { @@ -53,10 +56,9 @@ public class GetFieldMappingsResponse { this.mappings = mappings; } - - /** - * Returns the fields mapping. The return map keys are indexes and fields (as specified in the request). - */ + /** + * Returns the fields mapping. The return map keys are indexes and fields (as specified in the request). + */ public Map> mappings() { return mappings; } @@ -75,7 +77,6 @@ public FieldMappingMetadata fieldMappings(String index, String field) { return indexMapping.get(field); } - public static GetFieldMappingsResponse fromXContent(XContentParser parser) throws IOException { ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser); final Map> mappings = new HashMap<>(); @@ -94,20 +95,19 @@ public static class FieldMappingMetadata { private static final ParseField FULL_NAME = new ParseField("full_name"); private static final ParseField MAPPING = new ParseField("mapping"); - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("field_mapping_meta_data", true, - a -> new FieldMappingMetadata((String)a[0], (BytesReference)a[1]) - ); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "field_mapping_meta_data", + true, + a -> new FieldMappingMetadata((String) a[0], (BytesReference) a[1]) + ); static { - PARSER.declareField(optionalConstructorArg(), - (p, c) -> p.text(), FULL_NAME, ObjectParser.ValueType.STRING); - PARSER.declareField(optionalConstructorArg(), - (p, c) -> { - final XContentBuilder jsonBuilder = jsonBuilder().copyCurrentStructure(p); - final BytesReference bytes = BytesReference.bytes(jsonBuilder); - return bytes; - }, MAPPING, ObjectParser.ValueType.OBJECT); + PARSER.declareField(optionalConstructorArg(), (p, c) -> p.text(), FULL_NAME, ObjectParser.ValueType.STRING); + PARSER.declareField(optionalConstructorArg(), (p, c) -> { + final XContentBuilder jsonBuilder = jsonBuilder().copyCurrentStructure(p); + final BytesReference bytes = BytesReference.bytes(jsonBuilder); + return bytes; + }, MAPPING, ObjectParser.ValueType.OBJECT); } private String fullName; @@ -129,7 +129,7 @@ public Map sourceAsMap() { return XContentHelper.convertToMap(source, true, XContentType.JSON).v2(); } - //pkg-private for testing + // pkg-private for testing BytesReference getSource() { return source; } @@ -138,7 +138,7 @@ public static FieldMappingMetadata fromXContent(XContentParser parser) throws IO return PARSER.parse(parser, null); } - @Override + @Override public String toString() { return "FieldMappingMetadata{fullName='" + fullName + '\'' + ", source=" + source + '}'; } @@ -157,10 +157,9 @@ public int hashCode() { } } - @Override public String toString() { - return "GetFieldMappingsResponse{" + "mappings=" + mappings + '}'; + return "GetFieldMappingsResponse{" + "mappings=" + mappings + '}'; } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetIndexRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetIndexRequest.java index 2224c35c7d33d..425f17780622c 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetIndexRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetIndexRequest.java @@ -119,5 +119,4 @@ public boolean includeDefaults() { return includeDefaults; } - } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetIndexResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetIndexResponse.java index a56777ad9e711..a77621328fdd2 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetIndexResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetIndexResponse.java @@ -12,9 +12,9 @@ import org.elasticsearch.cluster.metadata.AliasMetadata; import org.elasticsearch.cluster.metadata.MappingMetadata; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentParser.Token; -import org.elasticsearch.index.mapper.MapperService; import java.io.IOException; import java.util.ArrayList; @@ -39,12 +39,14 @@ public class GetIndexResponse { private Map dataStreams; private String[] indices; - GetIndexResponse(String[] indices, - Map mappings, - Map> aliases, - Map settings, - Map defaultSettings, - Map dataStreams) { + GetIndexResponse( + String[] indices, + Map mappings, + Map> aliases, + Map settings, + Map defaultSettings, + Map dataStreams + ) { this.indices = indices; // to have deterministic order Arrays.sort(indices); @@ -181,8 +183,14 @@ private static class IndexEntry { Settings indexSettings = Settings.EMPTY; Settings indexDefaultSettings = Settings.EMPTY; String dataStream; - IndexEntry(List indexAliases, MappingMetadata indexMappings, Settings indexSettings, Settings indexDefaultSettings, - String dataStream) { + + IndexEntry( + List indexAliases, + MappingMetadata indexMappings, + Settings indexSettings, + Settings indexDefaultSettings, + String dataStream + ) { if (indexAliases != null) this.indexAliases = indexAliases; if (indexMappings != null) this.indexMappings = indexMappings; if (indexSettings != null) this.indexSettings = indexSettings; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetIndexTemplatesRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetIndexTemplatesRequest.java index 5e2923df77ba5..a3ad6c4a9100b 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetIndexTemplatesRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetIndexTemplatesRequest.java @@ -10,8 +10,8 @@ import org.elasticsearch.client.TimedRequest; import org.elasticsearch.client.Validatable; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; import java.util.Arrays; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetIndexTemplatesResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetIndexTemplatesResponse.java index 68495bf9fd872..fbdf82ad21b19 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetIndexTemplatesResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetIndexTemplatesResponse.java @@ -15,8 +15,7 @@ import java.util.List; import java.util.Objects; - -public class GetIndexTemplatesResponse { +public class GetIndexTemplatesResponse { @Override public String toString() { @@ -39,7 +38,6 @@ public List getIndexTemplates() { return indexTemplates; } - public static GetIndexTemplatesResponse fromXContent(XContentParser parser) throws IOException { final List templates = new ArrayList<>(); for (XContentParser.Token token = parser.nextToken(); token != XContentParser.Token.END_OBJECT; token = parser.nextToken()) { @@ -60,12 +58,9 @@ public int hashCode() { @Override public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; + if (this == obj) return true; + if (obj == null) return false; + if (getClass() != obj.getClass()) return false; // To compare results we need to make sure the templates are listed in the same order GetIndexTemplatesResponse other = (GetIndexTemplatesResponse) obj; List thisList = new ArrayList<>(this.indexTemplates); @@ -75,5 +70,4 @@ public boolean equals(Object obj) { return Objects.equals(thisList, otherList); } - } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetMappingsResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetMappingsResponse.java index cec598844e049..1ccf18e75e671 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetMappingsResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/GetMappingsResponse.java @@ -9,10 +9,10 @@ package org.elasticsearch.client.indices; import org.elasticsearch.cluster.metadata.MappingMetadata; -import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParserUtils; import org.elasticsearch.index.mapper.MapperService; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.HashMap; @@ -37,9 +37,7 @@ public static GetMappingsResponse fromXContent(XContentParser parser) throws IOE parser.nextToken(); } - XContentParserUtils.ensureExpectedToken(parser.currentToken(), - XContentParser.Token.START_OBJECT, - parser); + XContentParserUtils.ensureExpectedToken(parser.currentToken(), XContentParser.Token.START_OBJECT, parser); Map parts = parser.map(); @@ -49,8 +47,9 @@ public static GetMappingsResponse fromXContent(XContentParser parser) throws IOE assert entry.getValue() instanceof Map : "expected a map as type mapping, but got: " + entry.getValue().getClass(); @SuppressWarnings("unchecked") - final Map fieldMappings = (Map) ((Map) entry.getValue()) - .get(MAPPINGS.getPreferredName()); + final Map fieldMappings = (Map) ((Map) entry.getValue()).get( + MAPPINGS.getPreferredName() + ); mappings.put(indexName, new MappingMetadata(MapperService.SINGLE_MAPPING_NAME, fieldMappings)); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/IndexTemplateMetadata.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/IndexTemplateMetadata.java index 1017986beb544..f40eb85b0ba28 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/IndexTemplateMetadata.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/IndexTemplateMetadata.java @@ -10,13 +10,13 @@ import org.elasticsearch.cluster.metadata.AliasMetadata; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.MappingMetadata; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.collect.ImmutableOpenMap; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.index.mapper.MapperService; import java.io.IOException; import java.util.AbstractMap; @@ -27,25 +27,28 @@ import static org.elasticsearch.xcontent.ConstructingObjectParser.optionalConstructorArg; -public class IndexTemplateMetadata { +public class IndexTemplateMetadata { @SuppressWarnings("unchecked") private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "IndexTemplateMetadata", true, (a, name) -> { - List> alias = (List>) a[5]; - ImmutableOpenMap aliasMap = - new ImmutableOpenMap.Builder() - .putAll(alias.stream().collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))) - .build(); - return new IndexTemplateMetadata( - name, - (Integer) a[0], - (Integer) a[1], - (List) a[2], - (Settings) a[3], - (MappingMetadata) a[4], - aliasMap); - }); + "IndexTemplateMetadata", + true, + (a, name) -> { + List> alias = (List>) a[5]; + ImmutableOpenMap aliasMap = new ImmutableOpenMap.Builder().putAll( + alias.stream().collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)) + ).build(); + return new IndexTemplateMetadata( + name, + (Integer) a[0], + (Integer) a[1], + (List) a[2], + (Settings) a[3], + (MappingMetadata) a[4], + aliasMap + ); + } + ); static { PARSER.declareInt(optionalConstructorArg(), new ParseField("order")); @@ -64,8 +67,11 @@ public class IndexTemplateMetadata { } return new MappingMetadata(MapperService.SINGLE_MAPPING_NAME, mapping); }, new ParseField("mappings")); - PARSER.declareNamedObjects(optionalConstructorArg(), - (p, c, name) -> new AbstractMap.SimpleEntry<>(name, AliasMetadata.Builder.fromXContent(p)), new ParseField("aliases")); + PARSER.declareNamedObjects( + optionalConstructorArg(), + (p, c, name) -> new AbstractMap.SimpleEntry<>(name, AliasMetadata.Builder.fromXContent(p)), + new ParseField("aliases") + ); } private final String name; @@ -100,17 +106,22 @@ public class IndexTemplateMetadata { private final ImmutableOpenMap aliases; - public IndexTemplateMetadata(String name, int order, Integer version, - List patterns, Settings settings, - MappingMetadata mappings, - ImmutableOpenMap aliases) { + public IndexTemplateMetadata( + String name, + int order, + Integer version, + List patterns, + Settings settings, + MappingMetadata mappings, + ImmutableOpenMap aliases + ) { if (patterns == null || patterns.isEmpty()) { throw new IllegalArgumentException("Index patterns must not be null or empty; got " + patterns); } this.name = name; this.order = order; this.version = version; - this.patterns= patterns; + this.patterns = patterns; this.settings = settings; this.mappings = mappings; this.aliases = aliases; @@ -154,13 +165,13 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; IndexTemplateMetadata that = (IndexTemplateMetadata) o; - return order == that.order && - Objects.equals(name, that.name) && - Objects.equals(version, that.version) && - Objects.equals(patterns, that.patterns) && - Objects.equals(settings, that.settings) && - Objects.equals(mappings, that.mappings) && - Objects.equals(aliases, that.aliases); + return order == that.order + && Objects.equals(name, that.name) + && Objects.equals(version, that.version) + && Objects.equals(patterns, that.patterns) + && Objects.equals(settings, that.settings) + && Objects.equals(mappings, that.mappings) + && Objects.equals(aliases, that.aliases); } @Override @@ -245,7 +256,6 @@ public IndexTemplateMetadata build() { return new IndexTemplateMetadata(name, order, version, indexPatterns, settings, mappings, aliases.build()); } - public static IndexTemplateMetadata fromXContent(XContentParser parser, String templateName) throws IOException { return PARSER.parse(parser, templateName); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/PutIndexTemplateRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/PutIndexTemplateRequest.java index 0bf518501e476..9125e21a9e2a5 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/PutIndexTemplateRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/PutIndexTemplateRequest.java @@ -14,17 +14,17 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.common.xcontent.support.XContentMapValues; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ToXContentFragment; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.common.xcontent.support.XContentMapValues; import java.io.IOException; import java.io.UncheckedIOException; @@ -71,7 +71,7 @@ public PutIndexTemplateRequest(String name, List indexPatterns) { * Sets the name of the index template. */ public PutIndexTemplateRequest name(String name) { - if(name == null) { + if (name == null) { throw new IllegalArgumentException("Name cannot be null"); } this.name = name; @@ -193,8 +193,7 @@ public String cause() { * @param source The mapping source */ public PutIndexTemplateRequest mapping(XContentBuilder source) { - internalMapping(XContentHelper.convertToMap(BytesReference.bytes(source), - true, source.contentType()).v2()); + internalMapping(XContentHelper.convertToMap(BytesReference.bytes(source), true, source.contentType()).v2()); return this; } @@ -224,8 +223,7 @@ private PutIndexTemplateRequest internalMapping(Map source) { builder.map(source); Objects.requireNonNull(builder.contentType()); try { - mappings = new BytesArray( - XContentHelper.convertToJson(BytesReference.bytes(builder), false, false, builder.contentType())); + mappings = new BytesArray(XContentHelper.convertToJson(BytesReference.bytes(builder), false, false, builder.contentType())); return this; } catch (IOException e) { throw new UncheckedIOException("failed to convert source to json", e); @@ -259,7 +257,7 @@ public PutIndexTemplateRequest source(Map templateSource) { for (Map.Entry entry : source.entrySet()) { String name = entry.getKey(); if (name.equals("index_patterns")) { - if(entry.getValue() instanceof String) { + if (entry.getValue() instanceof String) { patterns(Collections.singletonList((String) entry.getValue())); } else if (entry.getValue() instanceof List) { List elements = ((List) entry.getValue()).stream().map(Object::toString).collect(Collectors.toList()); @@ -273,7 +271,7 @@ public PutIndexTemplateRequest source(Map templateSource) { if ((entry.getValue() instanceof Integer) == false) { throw new IllegalArgumentException("Malformed [version] value, should be an integer"); } - version((Integer)entry.getValue()); + version((Integer) entry.getValue()); } else if (name.equals("settings")) { if ((entry.getValue() instanceof Map) == false) { throw new IllegalArgumentException("Malformed [settings] section, should include an inner object"); @@ -319,7 +317,6 @@ public PutIndexTemplateRequest source(BytesReference source, XContentType xConte return source(XContentHelper.convertToMap(source, true, xContentType).v2()); } - public Set aliases() { return this.aliases; } @@ -356,15 +353,20 @@ public PutIndexTemplateRequest aliases(String source) { */ public PutIndexTemplateRequest aliases(BytesReference source) { // EMPTY is safe here because we never call namedObject - try (XContentParser parser = XContentHelper - .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, source)) { - //move to the first alias + try ( + XContentParser parser = XContentHelper.createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + source + ) + ) { + // move to the first alias parser.nextToken(); while ((parser.nextToken()) != XContentParser.Token.END_OBJECT) { alias(Alias.fromXContent(parser)); } return this; - } catch(IOException e) { + } catch (IOException e) { throw new ElasticsearchParseException("Failed to parse aliases", e); } } @@ -411,8 +413,13 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws if (mappings != null) { builder.field("mappings"); - try (XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, mappings.utf8ToString())) { + try ( + XContentParser parser = JsonXContent.jsonXContent.createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + mappings.utf8ToString() + ) + ) { builder.copyCurrentStructure(parser); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/ReloadAnalyzersResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/ReloadAnalyzersResponse.java index 197165fc0aede..db7fa2299de23 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/ReloadAnalyzersResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/ReloadAnalyzersResponse.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.indices; import org.elasticsearch.client.core.BroadcastResponse; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.core.Tuple; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.util.HashMap; @@ -34,25 +34,31 @@ public class ReloadAnalyzersResponse extends BroadcastResponse { } @SuppressWarnings({ "unchecked" }) - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("reload_analyzer", - true, arg -> { - Shards shards = (Shards) arg[0]; - List> results = (List>) arg[1]; - Map reloadDetails = new HashMap<>(); - for (Tuple result : results) { - reloadDetails.put(result.v1(), result.v2()); - } - return new ReloadAnalyzersResponse(shards, reloadDetails); - }); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "reload_analyzer", + true, + arg -> { + Shards shards = (Shards) arg[0]; + List> results = (List>) arg[1]; + Map reloadDetails = new HashMap<>(); + for (Tuple result : results) { + reloadDetails.put(result.v1(), result.v2()); + } + return new ReloadAnalyzersResponse(shards, reloadDetails); + } + ); @SuppressWarnings({ "unchecked" }) private static final ConstructingObjectParser, Void> ENTRY_PARSER = new ConstructingObjectParser<>( - "reload_analyzer.entry", true, arg -> { - String index = (String) arg[0]; - Set nodeIds = new HashSet<>((List) arg[1]); - Set analyzers = new HashSet<>((List) arg[2]); - return new Tuple<>(index, new ReloadDetails(index, nodeIds, analyzers)); - }); + "reload_analyzer.entry", + true, + arg -> { + String index = (String) arg[0]; + Set nodeIds = new HashSet<>((List) arg[1]); + Set analyzers = new HashSet<>((List) arg[2]); + return new Tuple<>(index, new ReloadDetails(index, nodeIds, analyzers)); + } + ); static { declareShardsField(PARSER); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/ResizeResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/ResizeResponse.java index 0cbda6e89585d..8d61f5f4a2684 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/ResizeResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/ResizeResponse.java @@ -10,9 +10,9 @@ import org.elasticsearch.client.core.AcknowledgedResponse; import org.elasticsearch.client.core.ShardsAcknowledgedResponse; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.util.Objects; @@ -25,8 +25,11 @@ public class ResizeResponse extends ShardsAcknowledgedResponse { private static final ParseField INDEX = new ParseField("index"); - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("resize_index", - true, args -> new ResizeResponse((boolean) args[0], (boolean) args[1], (String) args[2])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "resize_index", + true, + args -> new ResizeResponse((boolean) args[0], (boolean) args[1], (String) args[2]) + ); static { PARSER.declareBoolean(constructorArg(), new ParseField(AcknowledgedResponse.PARSE_FIELD_NAME)); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/SimulateIndexTemplateRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/SimulateIndexTemplateRequest.java index a52e3257a27c4..a59cb5e39d1ec 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/SimulateIndexTemplateRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/SimulateIndexTemplateRequest.java @@ -9,8 +9,8 @@ package org.elasticsearch.client.indices; import org.elasticsearch.client.TimedRequest; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; +import org.elasticsearch.core.Nullable; /** * A request to simulate matching a provided index name and an optional new index template against the existing index templates. diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/SimulateIndexTemplateResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/SimulateIndexTemplateResponse.java index 13aed5d5fff1b..92822ccb63b55 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/SimulateIndexTemplateResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/SimulateIndexTemplateResponse.java @@ -9,8 +9,8 @@ import org.elasticsearch.cluster.metadata.Template; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -27,20 +27,24 @@ public class SimulateIndexTemplateResponse { private static final ParseField INDEX_PATTERNS = new ParseField("index_patterns"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("simulate_index_templates_response", false, - a -> new SimulateIndexTemplateResponse( - a[0] != null ? (Template) a[0] : null, - a[1] != null ? - ((List) a[1]).stream() - .collect(Collectors.toMap(IndexTemplateAndPatterns::name, IndexTemplateAndPatterns::indexPatterns)) : null - ) - ); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "simulate_index_templates_response", + false, + a -> new SimulateIndexTemplateResponse( + a[0] != null ? (Template) a[0] : null, + a[1] != null + ? ((List) a[1]).stream() + .collect(Collectors.toMap(IndexTemplateAndPatterns::name, IndexTemplateAndPatterns::indexPatterns)) + : null + ) + ); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser INNER_PARSER = - new ConstructingObjectParser<>("index_template_and_patterns", false, - a -> new IndexTemplateAndPatterns((String) a[0], (List) a[1])); + private static final ConstructingObjectParser INNER_PARSER = new ConstructingObjectParser<>( + "index_template_and_patterns", + false, + a -> new IndexTemplateAndPatterns((String) a[0], (List) a[1]) + ); private static class IndexTemplateAndPatterns { String name; @@ -112,7 +116,11 @@ public int hashCode() { @Override public String toString() { - return "SimulateIndexTemplateResponse{" + "resolved template=" + resolvedTemplate + ", overlapping templates=" - + String.join("|", overlappingTemplates.keySet()) + "}"; + return "SimulateIndexTemplateResponse{" + + "resolved template=" + + resolvedTemplate + + ", overlapping templates=" + + String.join("|", overlappingTemplates.keySet()) + + "}"; } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/rollover/RolloverRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/rollover/RolloverRequest.java index 355337775e5ff..0e114fd91ded1 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/rollover/RolloverRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/rollover/RolloverRequest.java @@ -32,7 +32,7 @@ public class RolloverRequest extends TimedRequest implements ToXContentObject { private final String newIndexName; private boolean dryRun; private final Map> conditions = new HashMap<>(2); - //the index name "_na_" is never read back, what matters are settings, mappings and aliases + // the index name "_na_" is never read back, what matters are settings, mappings and aliases private final CreateIndexRequest createIndexRequest = new CreateIndexRequest("_na_"); public RolloverRequest(String alias, String newIndexName) { @@ -57,7 +57,6 @@ public String getNewIndexName() { return newIndexName; } - /** * Sets if the rollover should not be executed when conditions are met */ @@ -65,6 +64,7 @@ public RolloverRequest dryRun(boolean dryRun) { this.dryRun = dryRun; return this; } + /** * Returns if the rollover should not be executed when conditions are met */ diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/rollover/RolloverResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/rollover/RolloverResponse.java index 11a51382c186d..fcbd1508d76d9 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/rollover/RolloverResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/rollover/RolloverResponse.java @@ -9,8 +9,8 @@ package org.elasticsearch.client.indices.rollover; import org.elasticsearch.action.support.master.ShardsAcknowledgedResponse; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.util.Map; @@ -30,9 +30,19 @@ public final class RolloverResponse extends ShardsAcknowledgedResponse { private static final ParseField CONDITIONS = new ParseField("conditions"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("rollover", - true, args -> new RolloverResponse((String) args[0], (String) args[1], (Map) args[2], - (Boolean)args[3], (Boolean)args[4], (Boolean) args[5], (Boolean) args[6])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "rollover", + true, + args -> new RolloverResponse( + (String) args[0], + (String) args[1], + (Map) args[2], + (Boolean) args[3], + (Boolean) args[4], + (Boolean) args[5], + (Boolean) args[6] + ) + ); static { PARSER.declareString(constructorArg(), OLD_INDEX); @@ -49,8 +59,15 @@ public final class RolloverResponse extends ShardsAcknowledgedResponse { private final boolean dryRun; private final boolean rolledOver; - public RolloverResponse(String oldIndex, String newIndex, Map conditionResults, - boolean dryRun, boolean rolledOver, boolean acknowledged, boolean shardsAcknowledged) { + public RolloverResponse( + String oldIndex, + String newIndex, + Map conditionResults, + boolean dryRun, + boolean rolledOver, + boolean acknowledged, + boolean shardsAcknowledged + ) { super(acknowledged, shardsAcknowledged); this.oldIndex = oldIndex; this.newIndex = newIndex; @@ -102,11 +119,11 @@ public static RolloverResponse fromXContent(XContentParser parser) { public boolean equals(Object o) { if (super.equals(o)) { RolloverResponse that = (RolloverResponse) o; - return dryRun == that.dryRun && - rolledOver == that.rolledOver && - Objects.equals(oldIndex, that.oldIndex) && - Objects.equals(newIndex, that.newIndex) && - Objects.equals(conditionStatus, that.conditionStatus); + return dryRun == that.dryRun + && rolledOver == that.rolledOver + && Objects.equals(oldIndex, that.oldIndex) + && Objects.equals(newIndex, that.newIndex) + && Objects.equals(conditionStatus, that.conditionStatus); } return false; } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/license/GetBasicStatusResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/GetBasicStatusResponse.java index ebef8f1f176d5..4d98ab60074c1 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/license/GetBasicStatusResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/GetBasicStatusResponse.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.license; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.util.Objects; @@ -25,11 +25,18 @@ public class GetBasicStatusResponse { private static final ParseField ELIGIBLE_TO_START_BASIC = new ParseField("eligible_to_start_basic"); private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "get_basic_status_response", true, a -> new GetBasicStatusResponse((boolean) a[0])); + "get_basic_status_response", + true, + a -> new GetBasicStatusResponse((boolean) a[0]) + ); static { - PARSER.declareField(constructorArg(), (parser, context) -> parser.booleanValue(), ELIGIBLE_TO_START_BASIC, - ObjectParser.ValueType.BOOLEAN); + PARSER.declareField( + constructorArg(), + (parser, context) -> parser.booleanValue(), + ELIGIBLE_TO_START_BASIC, + ObjectParser.ValueType.BOOLEAN + ); } private final boolean eligibleToStartBasic; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/license/GetLicenseRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/GetLicenseRequest.java index 68741e15803fd..1e236379c6da2 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/license/GetLicenseRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/GetLicenseRequest.java @@ -9,13 +9,11 @@ import org.elasticsearch.client.Validatable; - public class GetLicenseRequest implements Validatable { protected boolean local = false; - public GetLicenseRequest() { - } + public GetLicenseRequest() {} public boolean isLocal() { return local; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/license/GetLicenseResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/GetLicenseResponse.java index 2dca3b243f58d..6ebef1dd6a54b 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/license/GetLicenseResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/GetLicenseResponse.java @@ -11,8 +11,7 @@ public class GetLicenseResponse { private String license; - GetLicenseResponse() { - } + GetLicenseResponse() {} public GetLicenseResponse(String license) { this.license = license; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/license/GetTrialStatusResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/GetTrialStatusResponse.java index 2ca498319ca30..65f7ba8f4a2df 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/license/GetTrialStatusResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/GetTrialStatusResponse.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.license; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.util.Objects; @@ -25,11 +25,18 @@ public class GetTrialStatusResponse { private static final ParseField ELIGIBLE_TO_START_TRIAL = new ParseField("eligible_to_start_trial"); private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "get_trial_status_response", true, a -> new GetTrialStatusResponse((boolean) a[0])); + "get_trial_status_response", + true, + a -> new GetTrialStatusResponse((boolean) a[0]) + ); static { - PARSER.declareField(constructorArg(), (parser, context) -> parser.booleanValue(), ELIGIBLE_TO_START_TRIAL, - ObjectParser.ValueType.BOOLEAN); + PARSER.declareField( + constructorArg(), + (parser, context) -> parser.booleanValue(), + ELIGIBLE_TO_START_TRIAL, + ObjectParser.ValueType.BOOLEAN + ); } private final boolean eligibleToStartTrial; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/license/LicensesStatus.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/LicensesStatus.java index 4277686d5126c..8f6e03cd8533c 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/license/LicensesStatus.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/LicensesStatus.java @@ -36,7 +36,6 @@ public static LicensesStatus fromId(int id) { } } - @Override public String toString() { return this.name().toLowerCase(Locale.ROOT); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/license/PutLicenseRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/PutLicenseRequest.java index 5969fa2049a5c..0a9e2e6ab18cc 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/license/PutLicenseRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/PutLicenseRequest.java @@ -15,8 +15,7 @@ public class PutLicenseRequest extends TimedRequest { private String licenseDefinition; private boolean acknowledge = false; - public PutLicenseRequest() { - } + public PutLicenseRequest() {} public void setLicenseDefinition(String licenseDefinition) { this.licenseDefinition = licenseDefinition; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/license/PutLicenseResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/PutLicenseResponse.java index 6d7905d712bdc..2970f8b83a707 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/license/PutLicenseResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/PutLicenseResponse.java @@ -8,12 +8,12 @@ package org.elasticsearch.client.license; -import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.client.common.ProtocolUtils; import org.elasticsearch.core.Tuple; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParseException; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.client.common.ProtocolUtils; import java.io.IOException; import java.util.ArrayList; @@ -29,56 +29,59 @@ public final class PutLicenseResponse { private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "put_license_response", true, (a, v) -> { - boolean acknowledged = (Boolean) a[0]; - LicensesStatus licensesStatus = LicensesStatus.fromString((String) a[1]); - @SuppressWarnings("unchecked") Tuple> acknowledgements = (Tuple>) a[2]; - if (acknowledgements == null) { - return new PutLicenseResponse(acknowledged, licensesStatus); - } else { - return new PutLicenseResponse(acknowledged, licensesStatus, acknowledgements.v1(), acknowledgements.v2()); - } + "put_license_response", + true, + (a, v) -> { + boolean acknowledged = (Boolean) a[0]; + LicensesStatus licensesStatus = LicensesStatus.fromString((String) a[1]); + @SuppressWarnings("unchecked") + Tuple> acknowledgements = (Tuple>) a[2]; + if (acknowledgements == null) { + return new PutLicenseResponse(acknowledged, licensesStatus); + } else { + return new PutLicenseResponse(acknowledged, licensesStatus, acknowledgements.v1(), acknowledgements.v2()); + } - }); + } + ); static { PARSER.declareBoolean(constructorArg(), new ParseField("acknowledged")); PARSER.declareString(constructorArg(), new ParseField("license_status")); PARSER.declareObject(optionalConstructorArg(), (parser, v) -> { - Map acknowledgeMessages = new HashMap<>(); - String message = null; - XContentParser.Token token; - String currentFieldName = null; - while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { - if (token == XContentParser.Token.FIELD_NAME) { - currentFieldName = parser.currentName(); + Map acknowledgeMessages = new HashMap<>(); + String message = null; + XContentParser.Token token; + String currentFieldName = null; + while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { + if (token == XContentParser.Token.FIELD_NAME) { + currentFieldName = parser.currentName(); + } else { + if (currentFieldName == null) { + throw new XContentParseException(parser.getTokenLocation(), "expected message header or acknowledgement"); + } + if ("message".equals(currentFieldName)) { + if (token != XContentParser.Token.VALUE_STRING) { + throw new XContentParseException(parser.getTokenLocation(), "unexpected message header type"); + } + message = parser.text(); } else { - if (currentFieldName == null) { - throw new XContentParseException(parser.getTokenLocation(), "expected message header or acknowledgement"); + if (token != XContentParser.Token.START_ARRAY) { + throw new XContentParseException(parser.getTokenLocation(), "unexpected acknowledgement type"); } - if ("message".equals(currentFieldName)) { + List acknowledgeMessagesList = new ArrayList<>(); + while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { if (token != XContentParser.Token.VALUE_STRING) { - throw new XContentParseException(parser.getTokenLocation(), "unexpected message header type"); - } - message = parser.text(); - } else { - if (token != XContentParser.Token.START_ARRAY) { - throw new XContentParseException(parser.getTokenLocation(), "unexpected acknowledgement type"); + throw new XContentParseException(parser.getTokenLocation(), "unexpected acknowledgement text"); } - List acknowledgeMessagesList = new ArrayList<>(); - while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { - if (token != XContentParser.Token.VALUE_STRING) { - throw new XContentParseException(parser.getTokenLocation(), "unexpected acknowledgement text"); - } - acknowledgeMessagesList.add(parser.text()); - } - acknowledgeMessages.put(currentFieldName, acknowledgeMessagesList.toArray(new String[0])); + acknowledgeMessagesList.add(parser.text()); } + acknowledgeMessages.put(currentFieldName, acknowledgeMessagesList.toArray(new String[0])); } } - return new Tuple<>(message, acknowledgeMessages); - }, - new ParseField("acknowledge")); + } + return new Tuple<>(message, acknowledgeMessages); + }, new ParseField("acknowledge")); } private boolean acknowledged; @@ -86,15 +89,18 @@ public final class PutLicenseResponse { private Map acknowledgeMessages; private String acknowledgeHeader; - public PutLicenseResponse() { - } + public PutLicenseResponse() {} public PutLicenseResponse(boolean acknowledged, LicensesStatus status) { this(acknowledged, status, null, Collections.emptyMap()); } - public PutLicenseResponse(boolean acknowledged, LicensesStatus status, String acknowledgeHeader, - Map acknowledgeMessages) { + public PutLicenseResponse( + boolean acknowledged, + LicensesStatus status, + String acknowledgeHeader, + Map acknowledgeMessages + ) { this.acknowledged = acknowledged; this.status = status; this.acknowledgeHeader = acknowledgeHeader; @@ -128,9 +134,9 @@ public boolean equals(Object o) { if (super.equals(o) == false) return false; PutLicenseResponse that = (PutLicenseResponse) o; - return status == that.status && - ProtocolUtils.equals(acknowledgeMessages, that.acknowledgeMessages) && - Objects.equals(acknowledgeHeader, that.acknowledgeHeader); + return status == that.status + && ProtocolUtils.equals(acknowledgeMessages, that.acknowledgeMessages) + && Objects.equals(acknowledgeHeader, that.acknowledgeHeader); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/license/StartBasicRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/StartBasicRequest.java index aea164cadbc7f..ebfaca66552ad 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/license/StartBasicRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/StartBasicRequest.java @@ -24,4 +24,3 @@ public boolean isAcknowledge() { return acknowledge; } } - diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/license/StartBasicResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/StartBasicResponse.java index ff00763bd46ed..df037e22169c0 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/license/StartBasicResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/StartBasicResponse.java @@ -7,9 +7,9 @@ */ package org.elasticsearch.client.license; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.core.Tuple; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParseException; import org.elasticsearch.xcontent.XContentParser; @@ -21,58 +21,62 @@ import java.util.Map; import java.util.Objects; +import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken; import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg; import static org.elasticsearch.xcontent.ConstructingObjectParser.optionalConstructorArg; -import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken; public class StartBasicResponse { private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "start_basic_response", true, (a, v) -> { - boolean basicWasStarted = (Boolean) a[0]; - String errorMessage = (String) a[1]; - - if (basicWasStarted) { - return new StartBasicResponse(StartBasicResponse.Status.GENERATED_BASIC); + "start_basic_response", + true, + (a, v) -> { + boolean basicWasStarted = (Boolean) a[0]; + String errorMessage = (String) a[1]; + + if (basicWasStarted) { + return new StartBasicResponse(StartBasicResponse.Status.GENERATED_BASIC); + } + StartBasicResponse.Status status = StartBasicResponse.Status.fromErrorMessage(errorMessage); + @SuppressWarnings("unchecked") + Tuple> acknowledgements = (Tuple>) a[2]; + return new StartBasicResponse(status, acknowledgements.v2(), acknowledgements.v1()); } - StartBasicResponse.Status status = StartBasicResponse.Status.fromErrorMessage(errorMessage); - @SuppressWarnings("unchecked") Tuple> acknowledgements = (Tuple>) a[2]; - return new StartBasicResponse(status, acknowledgements.v2(), acknowledgements.v1()); - }); + ); static { PARSER.declareBoolean(constructorArg(), new ParseField("basic_was_started")); PARSER.declareString(optionalConstructorArg(), new ParseField("error_message")); PARSER.declareObject(optionalConstructorArg(), (parser, v) -> { - Map acknowledgeMessages = new HashMap<>(); - String message = null; - XContentParser.Token token; - String currentFieldName = null; - while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { - if (token == XContentParser.Token.FIELD_NAME) { - currentFieldName = parser.currentName(); + Map acknowledgeMessages = new HashMap<>(); + String message = null; + XContentParser.Token token; + String currentFieldName = null; + while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { + if (token == XContentParser.Token.FIELD_NAME) { + currentFieldName = parser.currentName(); + } else { + if (currentFieldName == null) { + throw new XContentParseException(parser.getTokenLocation(), "expected message header or acknowledgement"); + } + if (new ParseField("message").getPreferredName().equals(currentFieldName)) { + ensureExpectedToken(XContentParser.Token.VALUE_STRING, token, parser); + message = parser.text(); } else { - if (currentFieldName == null) { - throw new XContentParseException(parser.getTokenLocation(), "expected message header or acknowledgement"); + if (token != XContentParser.Token.START_ARRAY) { + throw new XContentParseException(parser.getTokenLocation(), "unexpected acknowledgement type"); } - if (new ParseField("message").getPreferredName().equals(currentFieldName)) { + List acknowledgeMessagesList = new ArrayList<>(); + while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { ensureExpectedToken(XContentParser.Token.VALUE_STRING, token, parser); - message = parser.text(); - } else { - if (token != XContentParser.Token.START_ARRAY) { - throw new XContentParseException(parser.getTokenLocation(), "unexpected acknowledgement type"); - } - List acknowledgeMessagesList = new ArrayList<>(); - while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { - ensureExpectedToken(XContentParser.Token.VALUE_STRING, token, parser); - acknowledgeMessagesList.add(parser.text()); - } - acknowledgeMessages.put(currentFieldName, acknowledgeMessagesList.toArray(new String[0])); + acknowledgeMessagesList.add(parser.text()); } + acknowledgeMessages.put(currentFieldName, acknowledgeMessagesList.toArray(new String[0])); } } - return new Tuple<>(message, acknowledgeMessages); - }, new ParseField("acknowledge")); + } + return new Tuple<>(message, acknowledgeMessages); + }, new ParseField("acknowledge")); } private Map acknowledgeMessages; @@ -108,8 +112,7 @@ private StartBasicResponse(StartBasicResponse.Status status) { this(status, Collections.emptyMap(), null); } - private StartBasicResponse(StartBasicResponse.Status status, - Map acknowledgeMessages, String acknowledgeMessage) { + private StartBasicResponse(StartBasicResponse.Status status, Map acknowledgeMessages, String acknowledgeMessage) { this.status = status; this.acknowledgeMessages = acknowledgeMessages; this.acknowledgeMessage = acknowledgeMessage; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/license/StartTrialResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/StartTrialResponse.java index 223a86cc641a6..13e831be3149e 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/license/StartTrialResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/license/StartTrialResponse.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.license; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.core.Tuple; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParseException; import org.elasticsearch.xcontent.XContentParser; @@ -47,8 +47,7 @@ public class StartTrialResponse { acknowledgeMessages = null; } - return new StartTrialResponse(acknowledged, trialWasStarted, licenseType, errorMessage, acknowledgeHeader, - acknowledgeMessages); + return new StartTrialResponse(acknowledged, trialWasStarted, licenseType, errorMessage, acknowledgeHeader, acknowledgeMessages); } ); @@ -110,12 +109,14 @@ public static StartTrialResponse fromXContent(XContentParser parser) throws IOEx private final String acknowledgeHeader; private final Map acknowledgeMessages; - public StartTrialResponse(boolean acknowledged, - boolean trialWasStarted, - String licenseType, - String errorMessage, - String acknowledgeHeader, - Map acknowledgeMessages) { + public StartTrialResponse( + boolean acknowledged, + boolean trialWasStarted, + String licenseType, + String errorMessage, + String acknowledgeHeader, + Map acknowledgeMessages + ) { this.acknowledged = acknowledged; this.trialWasStarted = trialWasStarted; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/migration/DeprecationInfoResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/migration/DeprecationInfoResponse.java index 5fc58d80e834a..1bf24ae754590 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/migration/DeprecationInfoResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/migration/DeprecationInfoResponse.java @@ -9,8 +9,8 @@ package org.elasticsearch.client.migration; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -33,8 +33,12 @@ public class DeprecationInfoResponse { private final Map> indexSettingsIssues; private final List mlSettingsIssues; - public DeprecationInfoResponse(List clusterSettingsIssues, List nodeSettingsIssues, - Map> indexSettingsIssues, List mlSettingsIssues) { + public DeprecationInfoResponse( + List clusterSettingsIssues, + List nodeSettingsIssues, + Map> indexSettingsIssues, + List mlSettingsIssues + ) { this.clusterSettingsIssues = Objects.requireNonNull(clusterSettingsIssues, "cluster settings issues cannot be null"); this.nodeSettingsIssues = Objects.requireNonNull(nodeSettingsIssues, "node settings issues cannot be null"); this.indexSettingsIssues = Objects.requireNonNull(indexSettingsIssues, "index settings issues cannot be null"); @@ -103,10 +107,10 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; DeprecationInfoResponse that = (DeprecationInfoResponse) o; - return Objects.equals(clusterSettingsIssues, that.clusterSettingsIssues) && - Objects.equals(nodeSettingsIssues, that.nodeSettingsIssues) && - Objects.equals(mlSettingsIssues, that.mlSettingsIssues) && - Objects.equals(indexSettingsIssues, that.indexSettingsIssues); + return Objects.equals(clusterSettingsIssues, that.clusterSettingsIssues) + && Objects.equals(nodeSettingsIssues, that.nodeSettingsIssues) + && Objects.equals(mlSettingsIssues, that.mlSettingsIssues) + && Objects.equals(indexSettingsIssues, that.indexSettingsIssues); } @Override @@ -131,8 +135,10 @@ public static class DeprecationIssue { private static final ParseField RESOLVE_DURING_ROLLING_UPGRADE = new ParseField("resolve_during_rolling_upgrade"); private static final ParseField META = new ParseField("_meta"); - static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("deprecation_issue", true, args -> { + static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "deprecation_issue", + true, + args -> { String logLevel = (String) args[0]; String message = (String) args[1]; String url = (String) args[2]; @@ -141,7 +147,8 @@ public static class DeprecationIssue { @SuppressWarnings("unchecked") Map meta = (Map) args[5]; return new DeprecationIssue(Level.fromString(logLevel), message, url, details, resolveDuringRollingUpgrade, meta); - }); + } + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), LEVEL); @@ -154,8 +161,7 @@ public static class DeprecationIssue { public enum Level { WARNING, - CRITICAL - ; + CRITICAL; public static Level fromString(String value) { return Level.valueOf(value.toUpperCase(Locale.ROOT)); @@ -174,8 +180,14 @@ public String toString() { private final boolean resolveDuringRollingUpgrade; private final Map meta; - public DeprecationIssue(Level level, String message, String url, @Nullable String details, boolean resolveDuringRollingUpgrade, - @Nullable Map meta) { + public DeprecationIssue( + Level level, + String message, + String url, + @Nullable String details, + boolean resolveDuringRollingUpgrade, + @Nullable Map meta + ) { this.level = level; this.message = message; this.url = url; @@ -217,12 +229,12 @@ public boolean equals(Object o) { return false; } DeprecationIssue that = (DeprecationIssue) o; - return Objects.equals(level, that.level) && - Objects.equals(message, that.message) && - Objects.equals(url, that.url) && - Objects.equals(details, that.details) && - Objects.equals(resolveDuringRollingUpgrade, that.resolveDuringRollingUpgrade) && - Objects.equals(meta, that.meta); + return Objects.equals(level, that.level) + && Objects.equals(message, that.message) + && Objects.equals(url, that.url) + && Objects.equals(details, that.details) + && Objects.equals(resolveDuringRollingUpgrade, that.resolveDuringRollingUpgrade) + && Objects.equals(meta, that.meta); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/migration/GetFeatureUpgradeStatusResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/migration/GetFeatureUpgradeStatusResponse.java index 545837d7e5b87..eb79ed025add1 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/migration/GetFeatureUpgradeStatusResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/migration/GetFeatureUpgradeStatusResponse.java @@ -32,15 +32,14 @@ public class GetFeatureUpgradeStatusResponse { @SuppressWarnings("unchecked") private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "get_feature_upgrade_response", true, (a, ctx) -> new GetFeatureUpgradeStatusResponse( - (List) a[0], (String) a[1]) + "get_feature_upgrade_response", + true, + (a, ctx) -> new GetFeatureUpgradeStatusResponse((List) a[0], (String) a[1]) ); static { - PARSER.declareObjectArray(ConstructingObjectParser.optionalConstructorArg(), - FeatureUpgradeStatus::parse, FEATURE_UPGRADE_STATUSES); - PARSER.declareField(ConstructingObjectParser.constructorArg(), - (p, c) -> p.text(), UPGRADE_STATUS, ObjectParser.ValueType.STRING); + PARSER.declareObjectArray(ConstructingObjectParser.optionalConstructorArg(), FeatureUpgradeStatus::parse, FEATURE_UPGRADE_STATUSES); + PARSER.declareField(ConstructingObjectParser.constructorArg(), (p, c) -> p.text(), UPGRADE_STATUS, ObjectParser.ValueType.STRING); } /** @@ -81,16 +80,25 @@ public static class FeatureUpgradeStatus { @SuppressWarnings("unchecked") private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "feature_upgrade_status", true, (a, ctx) -> new FeatureUpgradeStatus( - (String) a[0], (String) a[1], (String) a[2], (List) a[3])); + "feature_upgrade_status", + true, + (a, ctx) -> new FeatureUpgradeStatus((String) a[0], (String) a[1], (String) a[2], (List) a[3]) + ); static { - PARSER.declareField(ConstructingObjectParser.constructorArg(), - (p, c) -> p.text(), FEATURE_NAME, ObjectParser.ValueType.STRING); - PARSER.declareField(ConstructingObjectParser.constructorArg(), - (p, c) -> p.text(), MINIMUM_INDEX_VERSION, ObjectParser.ValueType.STRING); - PARSER.declareField(ConstructingObjectParser.constructorArg(), - (p, c) -> p.text(), UPGRADE_STATUS, ObjectParser.ValueType.STRING); + PARSER.declareField(ConstructingObjectParser.constructorArg(), (p, c) -> p.text(), FEATURE_NAME, ObjectParser.ValueType.STRING); + PARSER.declareField( + ConstructingObjectParser.constructorArg(), + (p, c) -> p.text(), + MINIMUM_INDEX_VERSION, + ObjectParser.ValueType.STRING + ); + PARSER.declareField( + ConstructingObjectParser.constructorArg(), + (p, c) -> p.text(), + UPGRADE_STATUS, + ObjectParser.ValueType.STRING + ); PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), IndexVersion::parse, INDEX_VERSIONS); } @@ -171,14 +179,14 @@ public IndexVersion(String indexName, String version) { private static final ParseField VERSION = new ParseField("version"); private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "index_version", true, (a, ctx) -> new IndexVersion((String) a[0], (String) a[1]) + "index_version", + true, + (a, ctx) -> new IndexVersion((String) a[0], (String) a[1]) ); static { - PARSER.declareField(ConstructingObjectParser.constructorArg(), - (p, c) -> p.text(), INDEX_NAME, ObjectParser.ValueType.STRING); - PARSER.declareField(ConstructingObjectParser.constructorArg(), - (p, c) -> p.text(), VERSION, ObjectParser.ValueType.STRING); + PARSER.declareField(ConstructingObjectParser.constructorArg(), (p, c) -> p.text(), INDEX_NAME, ObjectParser.ValueType.STRING); + PARSER.declareField(ConstructingObjectParser.constructorArg(), (p, c) -> p.text(), VERSION, ObjectParser.ValueType.STRING); } public static IndexVersion parse(XContentParser parser, Void ctx) { diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/migration/PostFeatureUpgradeResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/migration/PostFeatureUpgradeResponse.java index e8d18b62c1afb..b278fc3bb27bd 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/migration/PostFeatureUpgradeResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/migration/PostFeatureUpgradeResponse.java @@ -9,11 +9,11 @@ package org.elasticsearch.client.migration; import org.elasticsearch.ElasticsearchException; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.core.Nullable; import java.util.Collections; import java.util.List; @@ -27,8 +27,10 @@ public class PostFeatureUpgradeResponse { private final boolean accepted; private final List features; - @Nullable private final String reason; - @Nullable private final ElasticsearchException elasticsearchException; + @Nullable + private final String reason; + @Nullable + private final ElasticsearchException elasticsearchException; private static final ParseField ACCEPTED = new ParseField("accepted"); private static final ParseField FEATURES = new ParseField("features"); @@ -37,22 +39,25 @@ public class PostFeatureUpgradeResponse { @SuppressWarnings("unchecked") private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "post_feature_upgrade_response", true, (a, ctx) -> new PostFeatureUpgradeResponse( - (Boolean) a[0], - (List) a[1], - (String) a[2], - (ElasticsearchException) a[3] - )); + "post_feature_upgrade_response", + true, + (a, ctx) -> new PostFeatureUpgradeResponse((Boolean) a[0], (List) a[1], (String) a[2], (ElasticsearchException) a[3]) + ); static { - PARSER.declareField(ConstructingObjectParser.constructorArg(), - (p, c) -> p.booleanValue(), ACCEPTED, ObjectParser.ValueType.BOOLEAN); - PARSER.declareObjectArray(ConstructingObjectParser.optionalConstructorArg(), - Feature::parse, FEATURES); - PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(), - (p, c) -> p.text(), REASON, ObjectParser.ValueType.STRING); - PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), - (p, c) -> ElasticsearchException.fromXContent(p), ELASTICSEARCH_EXCEPTION); + PARSER.declareField( + ConstructingObjectParser.constructorArg(), + (p, c) -> p.booleanValue(), + ACCEPTED, + ObjectParser.ValueType.BOOLEAN + ); + PARSER.declareObjectArray(ConstructingObjectParser.optionalConstructorArg(), Feature::parse, FEATURES); + PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> p.text(), REASON, ObjectParser.ValueType.STRING); + PARSER.declareObject( + ConstructingObjectParser.optionalConstructorArg(), + (p, c) -> ElasticsearchException.fromXContent(p), + ELASTICSEARCH_EXCEPTION + ); } public static PostFeatureUpgradeResponse parse(XContentParser parser) { @@ -123,12 +128,13 @@ public static class Feature { private static final ParseField FEATURE_NAME = new ParseField("feature_name"); private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "feature", true, (a, ctx) -> new Feature((String) a[0]) + "feature", + true, + (a, ctx) -> new Feature((String) a[0]) ); static { - PARSER.declareField(ConstructingObjectParser.constructorArg(), - (p, c) -> p.text(), FEATURE_NAME, ObjectParser.ValueType.STRING); + PARSER.declareField(ConstructingObjectParser.constructorArg(), (p, c) -> p.text(), FEATURE_NAME, ObjectParser.ValueType.STRING); } public static Feature parse(XContentParser parser, Void ctx) { diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/AbstractResultResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/AbstractResultResponse.java index d49c9b3b65e5e..94e42fc0c8b0a 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/AbstractResultResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/AbstractResultResponse.java @@ -29,8 +29,7 @@ public abstract class AbstractResultResponse implements To protected final long count; AbstractResultResponse(ParseField resultsField, List results, long count) { - this.resultsField = Objects.requireNonNull(resultsField, - "[results_field] must not be null"); + this.resultsField = Objects.requireNonNull(resultsField, "[results_field] must not be null"); this.results = Collections.unmodifiableList(results); this.count = count; } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/CloseJobRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/CloseJobRequest.java index 6a79ad10997cb..bd55976d7debb 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/CloseJobRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/CloseJobRequest.java @@ -8,11 +8,11 @@ package org.elasticsearch.client.ml; import org.elasticsearch.client.Validatable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -36,12 +36,17 @@ public class CloseJobRequest implements ToXContentObject, Validatable { @SuppressWarnings("unchecked") public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( "close_job_request", - true, a -> new CloseJobRequest((List) a[0])); + true, + a -> new CloseJobRequest((List) a[0]) + ); static { - PARSER.declareField(ConstructingObjectParser.constructorArg(), + PARSER.declareField( + ConstructingObjectParser.constructorArg(), p -> Arrays.asList(Strings.commaDelimitedListToStringArray(p.text())), - JOB_ID, ObjectParser.ValueType.STRING_ARRAY); + JOB_ID, + ObjectParser.ValueType.STRING_ARRAY + ); PARSER.declareString((obj, val) -> obj.setTimeout(TimeValue.parseTimeValue(val, TIMEOUT.getPreferredName())), TIMEOUT); PARSER.declareBoolean(CloseJobRequest::setForce, FORCE); PARSER.declareBoolean(CloseJobRequest::setAllowNoMatch, ALLOW_NO_MATCH); @@ -59,7 +64,7 @@ public class CloseJobRequest implements ToXContentObject, Validatable { * * @return a {@link CloseJobRequest} for all existing jobs */ - public static CloseJobRequest closeAllJobsRequest(){ + public static CloseJobRequest closeAllJobsRequest() { return new CloseJobRequest(ALL_JOBS); } @@ -148,10 +153,10 @@ public boolean equals(Object other) { } CloseJobRequest that = (CloseJobRequest) other; - return Objects.equals(jobIds, that.jobIds) && - Objects.equals(timeout, that.timeout) && - Objects.equals(force, that.force) && - Objects.equals(allowNoMatch, that.allowNoMatch); + return Objects.equals(jobIds, that.jobIds) + && Objects.equals(timeout, that.timeout) + && Objects.equals(force, that.force) + && Objects.equals(allowNoMatch, that.allowNoMatch); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/CloseJobResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/CloseJobResponse.java index b6ca8c5fef286..827cd87595ad4 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/CloseJobResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/CloseJobResponse.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -23,8 +23,11 @@ public class CloseJobResponse implements ToXContentObject { private static final ParseField CLOSED = new ParseField("closed"); - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("close_job_response", true, (a) -> new CloseJobResponse((Boolean)a[0])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "close_job_response", + true, + (a) -> new CloseJobResponse((Boolean) a[0]) + ); static { PARSER.declareBoolean(ConstructingObjectParser.constructorArg(), CLOSED); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/DeleteCalendarEventRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/DeleteCalendarEventRequest.java index 44d4fa15c61d3..5ade66ff71820 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/DeleteCalendarEventRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/DeleteCalendarEventRequest.java @@ -55,7 +55,6 @@ public boolean equals(Object other) { } DeleteCalendarEventRequest that = (DeleteCalendarEventRequest) other; - return Objects.equals(eventId, that.eventId) && - Objects.equals(calendarId, that.calendarId); + return Objects.equals(eventId, that.eventId) && Objects.equals(calendarId, that.calendarId); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/DeleteCalendarJobRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/DeleteCalendarJobRequest.java index f3ac3c985d9c4..fff975334d95c 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/DeleteCalendarJobRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/DeleteCalendarJobRequest.java @@ -65,7 +65,6 @@ public boolean equals(Object other) { } DeleteCalendarJobRequest that = (DeleteCalendarJobRequest) other; - return Objects.equals(jobIds, that.jobIds) && - Objects.equals(calendarId, that.calendarId); + return Objects.equals(jobIds, that.jobIds) && Objects.equals(calendarId, that.calendarId); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/DeleteDataFrameAnalyticsRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/DeleteDataFrameAnalyticsRequest.java index e78df151e55b3..ec19611ec58ae 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/DeleteDataFrameAnalyticsRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/DeleteDataFrameAnalyticsRequest.java @@ -73,9 +73,7 @@ public boolean equals(Object o) { if (o == null || getClass() != o.getClass()) return false; DeleteDataFrameAnalyticsRequest other = (DeleteDataFrameAnalyticsRequest) o; - return Objects.equals(id, other.id) - && Objects.equals(force, other.force) - && Objects.equals(timeout, other.timeout); + return Objects.equals(id, other.id) && Objects.equals(force, other.force) && Objects.equals(timeout, other.timeout); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/DeleteExpiredDataRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/DeleteExpiredDataRequest.java index 434f64ea01e07..66edbcfe8e961 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/DeleteExpiredDataRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/DeleteExpiredDataRequest.java @@ -28,7 +28,7 @@ public class DeleteExpiredDataRequest implements Validatable, ToXContentObject { private final Float requestsPerSecond; private final TimeValue timeout; - /** + /** * Create a new request to delete expired data */ public DeleteExpiredDataRequest() { @@ -76,9 +76,9 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; DeleteExpiredDataRequest that = (DeleteExpiredDataRequest) o; - return Objects.equals(requestsPerSecond, that.requestsPerSecond) && - Objects.equals(timeout, that.timeout) && - Objects.equals(jobId, that.jobId); + return Objects.equals(requestsPerSecond, that.requestsPerSecond) + && Objects.equals(timeout, that.timeout) + && Objects.equals(jobId, that.jobId); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/DeleteExpiredDataResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/DeleteExpiredDataResponse.java index 1d2715f77b855..18cd260698198 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/DeleteExpiredDataResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/DeleteExpiredDataResponse.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -17,7 +17,6 @@ import java.io.IOException; import java.util.Objects; - /** * A response acknowledging the deletion of expired data */ @@ -29,9 +28,11 @@ public DeleteExpiredDataResponse(boolean deleted) { this.deleted = deleted; } - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("delete_expired_data_response", true, - a -> new DeleteExpiredDataResponse((Boolean) a[0])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "delete_expired_data_response", + true, + a -> new DeleteExpiredDataResponse((Boolean) a[0]) + ); static { PARSER.declareBoolean(ConstructingObjectParser.constructorArg(), DELETED); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/DeleteForecastRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/DeleteForecastRequest.java index fb9b8f5605cce..11a49bf3aa270 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/DeleteForecastRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/DeleteForecastRequest.java @@ -9,10 +9,10 @@ import org.elasticsearch.client.Validatable; import org.elasticsearch.client.ml.job.config.Job; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -32,13 +32,14 @@ public class DeleteForecastRequest implements Validatable, ToXContentObject { public static final ParseField TIMEOUT = new ParseField("timeout"); public static final String ALL = "_all"; - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("delete_forecast_request", (a) -> new DeleteForecastRequest((String) a[0])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "delete_forecast_request", + (a) -> new DeleteForecastRequest((String) a[0]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), Job.ID); - PARSER.declareStringOrNull( - (c, p) -> c.setForecastIds(Strings.commaDelimitedListToStringArray(p)), FORECAST_ID); + PARSER.declareStringOrNull((c, p) -> c.setForecastIds(Strings.commaDelimitedListToStringArray(p)), FORECAST_ID); PARSER.declareBoolean(DeleteForecastRequest::setAllowNoForecasts, ALLOW_NO_FORECASTS); PARSER.declareString(DeleteForecastRequest::timeout, TIMEOUT); } @@ -136,10 +137,10 @@ public boolean equals(Object other) { } DeleteForecastRequest that = (DeleteForecastRequest) other; - return Objects.equals(jobId, that.jobId) && - Objects.equals(forecastIds, that.forecastIds) && - Objects.equals(allowNoForecasts, that.allowNoForecasts) && - Objects.equals(timeout, that.timeout); + return Objects.equals(jobId, that.jobId) + && Objects.equals(forecastIds, that.forecastIds) + && Objects.equals(allowNoForecasts, that.allowNoForecasts) + && Objects.equals(timeout, that.timeout); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/DeleteJobResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/DeleteJobResponse.java index 4c1a62e1de76a..ad843da43a357 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/DeleteJobResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/DeleteJobResponse.java @@ -8,13 +8,13 @@ package org.elasticsearch.client.ml; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.tasks.TaskId; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.tasks.TaskId; import java.io.IOException; import java.util.Objects; @@ -28,8 +28,11 @@ public class DeleteJobResponse implements ToXContentObject { private static final ParseField ACKNOWLEDGED = new ParseField("acknowledged"); private static final ParseField TASK = new ParseField("task"); - public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("delete_job_response", - true, a-> new DeleteJobResponse((Boolean) a[0], (TaskId) a[1])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "delete_job_response", + true, + a -> new DeleteJobResponse((Boolean) a[0], (TaskId) a[1]) + ); static { PARSER.declareBoolean(ConstructingObjectParser.optionalConstructorArg(), ACKNOWLEDGED); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/DeleteTrainedModelAliasRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/DeleteTrainedModelAliasRequest.java index aa0a2c93ea586..aa91a01a0d775 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/DeleteTrainedModelAliasRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/DeleteTrainedModelAliasRequest.java @@ -35,8 +35,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; DeleteTrainedModelAliasRequest request = (DeleteTrainedModelAliasRequest) o; - return Objects.equals(modelAlias, request.modelAlias) - && Objects.equals(modelId, request.modelId); + return Objects.equals(modelAlias, request.modelAlias) && Objects.equals(modelId, request.modelId); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/EstimateModelMemoryRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/EstimateModelMemoryRequest.java index 67dc9f8dd78b0..2a2a43fb2441e 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/EstimateModelMemoryRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/EstimateModelMemoryRequest.java @@ -92,8 +92,8 @@ public boolean equals(Object other) { } EstimateModelMemoryRequest that = (EstimateModelMemoryRequest) other; - return Objects.equals(analysisConfig, that.analysisConfig) && - Objects.equals(overallCardinality, that.overallCardinality) && - Objects.equals(maxBucketCardinality, that.maxBucketCardinality); + return Objects.equals(analysisConfig, that.analysisConfig) + && Objects.equals(overallCardinality, that.overallCardinality) + && Objects.equals(maxBucketCardinality, that.maxBucketCardinality); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/EstimateModelMemoryResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/EstimateModelMemoryResponse.java index f16df0f6d59be..806a76fe7eb63 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/EstimateModelMemoryResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/EstimateModelMemoryResponse.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.ml; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.util.Objects; @@ -21,11 +21,11 @@ public class EstimateModelMemoryResponse { public static final ParseField MODEL_MEMORY_ESTIMATE = new ParseField("model_memory_estimate"); - static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>( - "estimate_model_memory", - true, - args -> new EstimateModelMemoryResponse((String) args[0])); + static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "estimate_model_memory", + true, + args -> new EstimateModelMemoryResponse((String) args[0]) + ); static { PARSER.declareString(constructorArg(), MODEL_MEMORY_ESTIMATE); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/EvaluateDataFrameRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/EvaluateDataFrameRequest.java index 06db559dbc24e..5bad5d73a8892 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/EvaluateDataFrameRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/EvaluateDataFrameRequest.java @@ -13,8 +13,8 @@ import org.elasticsearch.client.ml.dataframe.QueryConfig; import org.elasticsearch.client.ml.dataframe.evaluation.Evaluation; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -27,9 +27,9 @@ import java.util.Objects; import java.util.Optional; +import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken; import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg; import static org.elasticsearch.xcontent.ConstructingObjectParser.optionalConstructorArg; -import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken; public class EvaluateDataFrameRequest implements ToXContentObject, Validatable { @@ -38,11 +38,11 @@ public class EvaluateDataFrameRequest implements ToXContentObject, Validatable { private static final ParseField EVALUATION = new ParseField("evaluation"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>( - "evaluate_data_frame_request", - true, - args -> new EvaluateDataFrameRequest((List) args[0], (QueryConfig) args[1], (Evaluation) args[2])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "evaluate_data_frame_request", + true, + args -> new EvaluateDataFrameRequest((List) args[0], (QueryConfig) args[1], (Evaluation) args[2]) + ); static { PARSER.declareStringArray(constructorArg(), INDEX); @@ -110,9 +110,7 @@ public Optional validate() { if (evaluation == null) { errors.add("evaluation must not be null"); } - return errors.isEmpty() - ? Optional.empty() - : Optional.of(ValidationException.withErrors(errors)); + return errors.isEmpty() ? Optional.empty() : Optional.of(ValidationException.withErrors(errors)); } @Override @@ -122,10 +120,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws if (queryConfig != null) { builder.field(QUERY.getPreferredName(), queryConfig.getQuery()); } - builder - .startObject(EVALUATION.getPreferredName()) - .field(evaluation.getName(), evaluation) - .endObject(); + builder.startObject(EVALUATION.getPreferredName()).field(evaluation.getName(), evaluation).endObject(); builder.endObject(); return builder; } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/EvaluateDataFrameResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/EvaluateDataFrameResponse.java index a3561403c857f..82213974297ab 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/EvaluateDataFrameResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/EvaluateDataFrameResponse.java @@ -37,10 +37,10 @@ public static EvaluateDataFrameResponse fromXContent(XContentParser parser) thro String evaluationName = parser.currentName(); parser.nextToken(); Map metrics = parser.map(LinkedHashMap::new, p -> parseMetric(evaluationName, p)); - List knownMetrics = - metrics.values().stream() - .filter(Objects::nonNull) // Filter out null values returned by {@link EvaluateDataFrameResponse::parseMetric}. - .collect(Collectors.toList()); + List knownMetrics = metrics.values() + .stream() + .filter(Objects::nonNull) // Filter out null values returned by {@link EvaluateDataFrameResponse::parseMetric}. + .collect(Collectors.toList()); ensureExpectedToken(XContentParser.Token.END_OBJECT, parser.nextToken(), parser); return new EvaluateDataFrameResponse(evaluationName, knownMetrics); } @@ -80,10 +80,7 @@ public T getMetricByName(String metricName) @Override public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { - return builder - .startObject() - .field(evaluationName, metrics) - .endObject(); + return builder.startObject().field(evaluationName, metrics).endObject(); } @Override @@ -91,8 +88,7 @@ public boolean equals(Object o) { if (o == this) return true; if (o == null || getClass() != o.getClass()) return false; EvaluateDataFrameResponse that = (EvaluateDataFrameResponse) o; - return Objects.equals(evaluationName, that.evaluationName) - && Objects.equals(metrics, that.metrics); + return Objects.equals(evaluationName, that.evaluationName) && Objects.equals(metrics, that.metrics); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/ExplainDataFrameAnalyticsResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/ExplainDataFrameAnalyticsResponse.java index d460a2122bba0..d9e15f8ff7031 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/ExplainDataFrameAnalyticsResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/ExplainDataFrameAnalyticsResponse.java @@ -9,8 +9,8 @@ import org.elasticsearch.client.ml.dataframe.explain.FieldSelection; import org.elasticsearch.client.ml.dataframe.explain.MemoryEstimation; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -31,10 +31,11 @@ public static ExplainDataFrameAnalyticsResponse fromXContent(XContentParser pars } @SuppressWarnings("unchecked") - static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>( - TYPE.getPreferredName(), true, - args -> new ExplainDataFrameAnalyticsResponse((List) args[0], (MemoryEstimation) args[1])); + static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + TYPE.getPreferredName(), + true, + args -> new ExplainDataFrameAnalyticsResponse((List) args[0], (MemoryEstimation) args[1]) + ); static { PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), FieldSelection.PARSER, FIELD_SELECTION); @@ -64,8 +65,7 @@ public boolean equals(Object other) { if (other == null || getClass() != other.getClass()) return false; ExplainDataFrameAnalyticsResponse that = (ExplainDataFrameAnalyticsResponse) other; - return Objects.equals(fieldSelection, that.fieldSelection) - && Objects.equals(memoryEstimation, that.memoryEstimation); + return Objects.equals(fieldSelection, that.fieldSelection) && Objects.equals(memoryEstimation, that.memoryEstimation); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/FlushJobRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/FlushJobRequest.java index 9c8623c18521a..a19f787a6458f 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/FlushJobRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/FlushJobRequest.java @@ -9,8 +9,8 @@ import org.elasticsearch.client.Validatable; import org.elasticsearch.client.ml.job.config.Job; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -28,8 +28,10 @@ public class FlushJobRequest implements Validatable, ToXContentObject { public static final ParseField ADVANCE_TIME = new ParseField("advance_time"); public static final ParseField SKIP_TIME = new ParseField("skip_time"); - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("flush_job_request", (a) -> new FlushJobRequest((String) a[0])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "flush_job_request", + (a) -> new FlushJobRequest((String) a[0]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), Job.ID); @@ -145,12 +147,12 @@ public boolean equals(Object obj) { } FlushJobRequest other = (FlushJobRequest) obj; - return Objects.equals(jobId, other.jobId) && - calcInterim == other.calcInterim && - Objects.equals(start, other.start) && - Objects.equals(end, other.end) && - Objects.equals(advanceTime, other.advanceTime) && - Objects.equals(skipTime, other.skipTime); + return Objects.equals(jobId, other.jobId) + && calcInterim == other.calcInterim + && Objects.equals(start, other.start) + && Objects.equals(end, other.end) + && Objects.equals(advanceTime, other.advanceTime) + && Objects.equals(skipTime, other.skipTime); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/FlushJobResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/FlushJobResponse.java index 9a5cb28ecce1f..d85ec888b61a4 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/FlushJobResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/FlushJobResponse.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -26,14 +26,15 @@ public class FlushJobResponse implements ToXContentObject { public static final ParseField FLUSHED = new ParseField("flushed"); public static final ParseField LAST_FINALIZED_BUCKET_END = new ParseField("last_finalized_bucket_end"); - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("flush_job_response", - true, - (a) -> { - boolean flushed = (boolean) a[0]; - Date date = a[1] == null ? null : new Date((long) a[1]); - return new FlushJobResponse(flushed, date); - }); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "flush_job_response", + true, + (a) -> { + boolean flushed = (boolean) a[0]; + Date date = a[1] == null ? null : new Date((long) a[1]); + return new FlushJobResponse(flushed, date); + } + ); static { PARSER.declareBoolean(ConstructingObjectParser.constructorArg(), FLUSHED); @@ -91,8 +92,11 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.startObject(); builder.field(FLUSHED.getPreferredName(), flushed); if (lastFinalizedBucketEnd != null) { - builder.timeField(LAST_FINALIZED_BUCKET_END.getPreferredName(), - LAST_FINALIZED_BUCKET_END.getPreferredName() + "_string", lastFinalizedBucketEnd.getTime()); + builder.timeField( + LAST_FINALIZED_BUCKET_END.getPreferredName(), + LAST_FINALIZED_BUCKET_END.getPreferredName() + "_string", + lastFinalizedBucketEnd.getTime() + ); } builder.endObject(); return builder; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/ForecastJobRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/ForecastJobRequest.java index 176808fce0a17..2a7f09c802dc3 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/ForecastJobRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/ForecastJobRequest.java @@ -9,11 +9,11 @@ import org.elasticsearch.client.Validatable; import org.elasticsearch.client.ml.job.config.Job; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -32,15 +32,18 @@ public class ForecastJobRequest implements Validatable, ToXContentObject { public static final ParseField EXPIRES_IN = new ParseField("expires_in"); public static final ParseField MAX_MODEL_MEMORY = new ParseField("max_model_memory"); - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("forecast_job_request", (a) -> new ForecastJobRequest((String)a[0])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "forecast_job_request", + (a) -> new ForecastJobRequest((String) a[0]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), Job.ID); + PARSER.declareString((request, val) -> request.setDuration(TimeValue.parseTimeValue(val, DURATION.getPreferredName())), DURATION); PARSER.declareString( - (request, val) -> request.setDuration(TimeValue.parseTimeValue(val, DURATION.getPreferredName())), DURATION); - PARSER.declareString( - (request, val) -> request.setExpiresIn(TimeValue.parseTimeValue(val, EXPIRES_IN.getPreferredName())), EXPIRES_IN); + (request, val) -> request.setExpiresIn(TimeValue.parseTimeValue(val, EXPIRES_IN.getPreferredName())), + EXPIRES_IN + ); PARSER.declareField(ForecastJobRequest::setMaxModelMemory, (p, c) -> { if (p.currentToken() == XContentParser.Token.VALUE_STRING) { return ByteSizeValue.parseBytesSizeValue(p.text(), MAX_MODEL_MEMORY.getPreferredName()); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/ForecastJobResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/ForecastJobResponse.java index 75a529ea0ef24..b1cbd5d863c99 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/ForecastJobResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/ForecastJobResponse.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -24,10 +24,11 @@ public class ForecastJobResponse implements ToXContentObject { public static final ParseField ACKNOWLEDGED = new ParseField("acknowledged"); public static final ParseField FORECAST_ID = new ParseField("forecast_id"); - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("forecast_job_response", - true, - (a) -> new ForecastJobResponse((Boolean)a[0], (String)a[1])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "forecast_job_response", + true, + (a) -> new ForecastJobResponse((Boolean) a[0], (String) a[1]) + ); static { PARSER.declareBoolean(ConstructingObjectParser.constructorArg(), ACKNOWLEDGED); @@ -75,8 +76,7 @@ public boolean equals(Object obj) { return false; } ForecastJobResponse other = (ForecastJobResponse) obj; - return Objects.equals(acknowledged, other.acknowledged) - && Objects.equals(forecastId, other.forecastId); + return Objects.equals(acknowledged, other.acknowledged) && Objects.equals(forecastId, other.forecastId); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetBucketsRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetBucketsRequest.java index a8d8996b383ef..052f177bb8743 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetBucketsRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetBucketsRequest.java @@ -11,8 +11,8 @@ import org.elasticsearch.client.core.PageParams; import org.elasticsearch.client.ml.job.config.Job; import org.elasticsearch.client.ml.job.results.Result; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -236,15 +236,15 @@ public boolean equals(Object obj) { return false; } GetBucketsRequest other = (GetBucketsRequest) obj; - return Objects.equals(jobId, other.jobId) && - Objects.equals(timestamp, other.timestamp) && - Objects.equals(expand, other.expand) && - Objects.equals(excludeInterim, other.excludeInterim) && - Objects.equals(anomalyScore, other.anomalyScore) && - Objects.equals(pageParams, other.pageParams) && - Objects.equals(start, other.start) && - Objects.equals(end, other.end) && - Objects.equals(sort, other.sort) && - Objects.equals(descending, other.descending); + return Objects.equals(jobId, other.jobId) + && Objects.equals(timestamp, other.timestamp) + && Objects.equals(expand, other.expand) + && Objects.equals(excludeInterim, other.excludeInterim) + && Objects.equals(anomalyScore, other.anomalyScore) + && Objects.equals(pageParams, other.pageParams) + && Objects.equals(start, other.start) + && Objects.equals(end, other.end) + && Objects.equals(sort, other.sort) + && Objects.equals(descending, other.descending); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetBucketsResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetBucketsResponse.java index 157abe9669a29..ced8ad201adbc 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetBucketsResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetBucketsResponse.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml; import org.elasticsearch.client.ml.job.results.Bucket; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -24,8 +24,11 @@ public class GetBucketsResponse extends AbstractResultResponse { public static final ParseField BUCKETS = new ParseField("buckets"); @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("get_buckets_response", - true, a -> new GetBucketsResponse((List) a[0], (long) a[1])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "get_buckets_response", + true, + a -> new GetBucketsResponse((List) a[0], (long) a[1]) + ); static { PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), Bucket.PARSER, BUCKETS); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetCalendarEventsRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetCalendarEventsRequest.java index b866650ad6a4b..91bc1f0b537ba 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetCalendarEventsRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetCalendarEventsRequest.java @@ -12,8 +12,8 @@ import org.elasticsearch.client.core.PageParams; import org.elasticsearch.client.ml.calendars.Calendar; import org.elasticsearch.client.ml.job.config.Job; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -28,8 +28,10 @@ public class GetCalendarEventsRequest implements Validatable, ToXContentObject { public static final ParseField START = new ParseField("start"); public static final ParseField END = new ParseField("end"); - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("get_calendar_events_request", a -> new GetCalendarEventsRequest((String)a[0])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "get_calendar_events_request", + a -> new GetCalendarEventsRequest((String) a[0]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), Calendar.ID); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetCalendarEventsResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetCalendarEventsResponse.java index de03a8605cb14..2c36c1c329e84 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetCalendarEventsResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetCalendarEventsResponse.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.ml; import org.elasticsearch.client.ml.calendars.ScheduledEvent; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -22,14 +22,16 @@ /** * Contains a {@link List} of the found {@link ScheduledEvent} objects and the total count found */ -public class GetCalendarEventsResponse extends AbstractResultResponse { +public class GetCalendarEventsResponse extends AbstractResultResponse { public static final ParseField RESULTS_FIELD = new ParseField("events"); @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("calendar_events_response", true, - a -> new GetCalendarEventsResponse((List) a[0], (long) a[1])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "calendar_events_response", + true, + a -> new GetCalendarEventsResponse((List) a[0], (long) a[1]) + ); static { PARSER.declareObjectArray(constructorArg(), ScheduledEvent.PARSER, RESULTS_FIELD); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetCalendarsRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetCalendarsRequest.java index 4d51cda347db8..55bcbd88964be 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetCalendarsRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetCalendarsRequest.java @@ -20,8 +20,10 @@ public class GetCalendarsRequest implements Validatable, ToXContentObject { - public static final ObjectParser PARSER = - new ObjectParser<>("get_calendars_request", GetCalendarsRequest::new); + public static final ObjectParser PARSER = new ObjectParser<>( + "get_calendars_request", + GetCalendarsRequest::new + ); static { PARSER.declareString(GetCalendarsRequest::setCalendarId, Calendar.ID); @@ -31,8 +33,7 @@ public class GetCalendarsRequest implements Validatable, ToXContentObject { private String calendarId; private PageParams pageParams; - public GetCalendarsRequest() { - } + public GetCalendarsRequest() {} public GetCalendarsRequest(String calendarId) { this.calendarId = calendarId; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetCalendarsResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetCalendarsResponse.java index 031a3a4e6ab7c..bf2119692b485 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetCalendarsResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetCalendarsResponse.java @@ -9,9 +9,9 @@ package org.elasticsearch.client.ml; import org.elasticsearch.client.ml.calendars.Calendar; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -25,9 +25,11 @@ public class GetCalendarsResponse extends AbstractResultResponse { public static final ParseField RESULTS_FIELD = new ParseField("calendars"); @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("calendars_response", true, - a -> new GetCalendarsResponse((List) a[0], (long) a[1])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "calendars_response", + true, + a -> new GetCalendarsResponse((List) a[0], (long) a[1]) + ); static { PARSER.declareObjectArray(constructorArg(), Calendar.PARSER, RESULTS_FIELD); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetCategoriesRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetCategoriesRequest.java index 94f2c28c3f6c2..9a00c09ffd847 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetCategoriesRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetCategoriesRequest.java @@ -11,8 +11,8 @@ import org.elasticsearch.client.core.PageParams; import org.elasticsearch.client.ml.job.config.Job; import org.elasticsearch.client.ml.job.results.CategoryDefinition; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -28,8 +28,9 @@ public class GetCategoriesRequest implements Validatable, ToXContentObject { public static final ParseField PARTITION_FIELD_VALUE = CategoryDefinition.PARTITION_FIELD_VALUE; public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "get_categories_request", a -> new GetCategoriesRequest((String) a[0])); - + "get_categories_request", + a -> new GetCategoriesRequest((String) a[0]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), Job.ID); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetCategoriesResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetCategoriesResponse.java index a8c874787a9b0..d1e542d18e43d 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetCategoriesResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetCategoriesResponse.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml; import org.elasticsearch.client.ml.job.results.CategoryDefinition; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -24,9 +24,11 @@ public class GetCategoriesResponse extends AbstractResultResponse PARSER = - new ConstructingObjectParser<>("get_categories_response", true, - a -> new GetCategoriesResponse((List) a[0], (long) a[1])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "get_categories_response", + true, + a -> new GetCategoriesResponse((List) a[0], (long) a[1]) + ); static { PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), CategoryDefinition.PARSER, CATEGORIES); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetDataFrameAnalyticsResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetDataFrameAnalyticsResponse.java index acf3592fae57f..8e991efa10d44 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetDataFrameAnalyticsResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetDataFrameAnalyticsResponse.java @@ -9,8 +9,8 @@ package org.elasticsearch.client.ml; import org.elasticsearch.client.ml.dataframe.DataFrameAnalyticsConfig; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.util.List; @@ -23,11 +23,11 @@ public class GetDataFrameAnalyticsResponse { public static final ParseField DATA_FRAME_ANALYTICS = new ParseField("data_frame_analytics"); @SuppressWarnings("unchecked") - static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>( - "get_data_frame_analytics", - true, - args -> new GetDataFrameAnalyticsResponse((List) args[0])); + static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "get_data_frame_analytics", + true, + args -> new GetDataFrameAnalyticsResponse((List) args[0]) + ); static { PARSER.declareObjectArray(constructorArg(), (p, c) -> DataFrameAnalyticsConfig.fromXContent(p), DATA_FRAME_ANALYTICS); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetDataFrameAnalyticsStatsResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetDataFrameAnalyticsStatsResponse.java index d4377be4c19ed..00284b0802a16 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetDataFrameAnalyticsStatsResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetDataFrameAnalyticsStatsResponse.java @@ -13,8 +13,8 @@ import org.elasticsearch.client.ml.dataframe.DataFrameAnalyticsStats; import org.elasticsearch.client.transform.AcknowledgedTasksResponse; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.util.Collections; @@ -33,29 +33,39 @@ public static GetDataFrameAnalyticsStatsResponse fromXContent(XContentParser par private static final ParseField DATA_FRAME_ANALYTICS = new ParseField("data_frame_analytics"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>( - "get_data_frame_analytics_stats_response", true, - args -> new GetDataFrameAnalyticsStatsResponse( - (List) args[0], - (List) args[1], - (List) args[2])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "get_data_frame_analytics_stats_response", + true, + args -> new GetDataFrameAnalyticsStatsResponse( + (List) args[0], + (List) args[1], + (List) args[2] + ) + ); static { PARSER.declareObjectArray(constructorArg(), (p, c) -> DataFrameAnalyticsStats.fromXContent(p), DATA_FRAME_ANALYTICS); PARSER.declareObjectArray( - optionalConstructorArg(), (p, c) -> TaskOperationFailure.fromXContent(p), AcknowledgedTasksResponse.TASK_FAILURES); + optionalConstructorArg(), + (p, c) -> TaskOperationFailure.fromXContent(p), + AcknowledgedTasksResponse.TASK_FAILURES + ); PARSER.declareObjectArray( - optionalConstructorArg(), (p, c) -> ElasticsearchException.fromXContent(p), AcknowledgedTasksResponse.NODE_FAILURES); + optionalConstructorArg(), + (p, c) -> ElasticsearchException.fromXContent(p), + AcknowledgedTasksResponse.NODE_FAILURES + ); } private final List analyticsStats; private final List taskFailures; private final List nodeFailures; - public GetDataFrameAnalyticsStatsResponse(List analyticsStats, - @Nullable List taskFailures, - @Nullable List nodeFailures) { + public GetDataFrameAnalyticsStatsResponse( + List analyticsStats, + @Nullable List taskFailures, + @Nullable List nodeFailures + ) { this.analyticsStats = analyticsStats; this.taskFailures = taskFailures == null ? Collections.emptyList() : Collections.unmodifiableList(taskFailures); this.nodeFailures = nodeFailures == null ? Collections.emptyList() : Collections.unmodifiableList(nodeFailures); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetDatafeedRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetDatafeedRequest.java index 33ebc5fdfea63..67fa0503a9b77 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetDatafeedRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetDatafeedRequest.java @@ -9,8 +9,8 @@ import org.elasticsearch.client.Validatable; import org.elasticsearch.client.ml.datafeed.DatafeedConfig; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -40,7 +40,9 @@ public class GetDatafeedRequest implements Validatable, ToXContentObject { @SuppressWarnings("unchecked") public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( "get_datafeed_request", - true, a -> new GetDatafeedRequest(a[0] == null ? new ArrayList<>() : (List) a[0])); + true, + a -> new GetDatafeedRequest(a[0] == null ? new ArrayList<>() : (List) a[0]) + ); static { PARSER.declareStringArray(ConstructingObjectParser.optionalConstructorArg(), DATAFEED_IDS); @@ -123,9 +125,9 @@ public boolean equals(Object other) { } GetDatafeedRequest that = (GetDatafeedRequest) other; - return Objects.equals(datafeedIds, that.datafeedIds) && - Objects.equals(allowNoMatch, that.allowNoMatch) && - Objects.equals(excludeGenerated, that.excludeGenerated); + return Objects.equals(datafeedIds, that.datafeedIds) + && Objects.equals(allowNoMatch, that.allowNoMatch) + && Objects.equals(excludeGenerated, that.excludeGenerated); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetDatafeedResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetDatafeedResponse.java index e80160c1144ab..b8a6a0d79972a 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetDatafeedResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetDatafeedResponse.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.ml; import org.elasticsearch.client.ml.datafeed.DatafeedConfig; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -28,9 +28,11 @@ public class GetDatafeedResponse extends AbstractResultResponse public static final ParseField RESULTS_FIELD = new ParseField("datafeeds"); @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("get_datafeed_response", true, - a -> new GetDatafeedResponse((List) a[0], (long) a[1])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "get_datafeed_response", + true, + a -> new GetDatafeedResponse((List) a[0], (long) a[1]) + ); static { PARSER.declareObjectArray(constructorArg(), DatafeedConfig.PARSER, RESULTS_FIELD); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetDatafeedStatsRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetDatafeedStatsRequest.java index a451296410776..081504354eb20 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetDatafeedStatsRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetDatafeedStatsRequest.java @@ -9,10 +9,10 @@ import org.elasticsearch.client.Validatable; import org.elasticsearch.client.ml.datafeed.DatafeedConfig; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -35,12 +35,17 @@ public class GetDatafeedStatsRequest implements Validatable, ToXContentObject { @SuppressWarnings("unchecked") public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "get_datafeed_stats_request", a -> new GetDatafeedStatsRequest((List) a[0])); + "get_datafeed_stats_request", + a -> new GetDatafeedStatsRequest((List) a[0]) + ); static { - PARSER.declareField(ConstructingObjectParser.constructorArg(), + PARSER.declareField( + ConstructingObjectParser.constructorArg(), p -> Arrays.asList(Strings.commaDelimitedListToStringArray(p.text())), - DatafeedConfig.ID, ObjectParser.ValueType.STRING_ARRAY); + DatafeedConfig.ID, + ObjectParser.ValueType.STRING_ARRAY + ); PARSER.declareBoolean(GetDatafeedStatsRequest::setAllowNoMatch, ALLOW_NO_MATCH); } @@ -54,7 +59,7 @@ public class GetDatafeedStatsRequest implements Validatable, ToXContentObject { * * @return a {@link GetDatafeedStatsRequest} for all existing datafeeds */ - public static GetDatafeedStatsRequest getAllDatafeedStatsRequest(){ + public static GetDatafeedStatsRequest getAllDatafeedStatsRequest() { return new GetDatafeedStatsRequest(ALL_DATAFEEDS); } @@ -112,8 +117,7 @@ public boolean equals(Object other) { } GetDatafeedStatsRequest that = (GetDatafeedStatsRequest) other; - return Objects.equals(datafeedIds, that.datafeedIds) && - Objects.equals(allowNoMatch, that.allowNoMatch); + return Objects.equals(datafeedIds, that.datafeedIds) && Objects.equals(allowNoMatch, that.allowNoMatch); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetDatafeedStatsResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetDatafeedStatsResponse.java index 06590e2ab9c85..94a49fc074c04 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetDatafeedStatsResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetDatafeedStatsResponse.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.ml; import org.elasticsearch.client.ml.datafeed.DatafeedStats; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -27,10 +27,11 @@ public class GetDatafeedStatsResponse extends AbstractResultResponse PARSER = - new ConstructingObjectParser<>("get_datafeed_stats_response", - true, - a -> new GetDatafeedStatsResponse((List) a[0], (long) a[1])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "get_datafeed_stats_response", + true, + a -> new GetDatafeedStatsResponse((List) a[0], (long) a[1]) + ); static { PARSER.declareObjectArray(constructorArg(), DatafeedStats.PARSER, RESULTS_FIELD); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetFiltersRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetFiltersRequest.java index 2c79eb55624c5..cafa4d8b331f5 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetFiltersRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetFiltersRequest.java @@ -22,8 +22,7 @@ */ public class GetFiltersRequest implements Validatable, ToXContentObject { - public static final ObjectParser PARSER = - new ObjectParser<>("get_filters_request", GetFiltersRequest::new); + public static final ObjectParser PARSER = new ObjectParser<>("get_filters_request", GetFiltersRequest::new); static { PARSER.declareString(GetFiltersRequest::setFilterId, MlFilter.ID); @@ -96,9 +95,7 @@ public boolean equals(Object obj) { return false; } GetFiltersRequest request = (GetFiltersRequest) obj; - return Objects.equals(filterId, request.filterId) - && Objects.equals(from, request.from) - && Objects.equals(size, request.size); + return Objects.equals(filterId, request.filterId) && Objects.equals(from, request.from) && Objects.equals(size, request.size); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetFiltersResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetFiltersResponse.java index 3b24454c84477..a0a190d89cfc2 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetFiltersResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetFiltersResponse.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.ml; import org.elasticsearch.client.ml.job.config.MlFilter; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -28,9 +28,11 @@ public class GetFiltersResponse extends AbstractResultResponse { public static final ParseField RESULTS_FIELD = new ParseField("filters"); @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("get_filters_response", true, - a -> new GetFiltersResponse((List) a[0], (long) a[1])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "get_filters_response", + true, + a -> new GetFiltersResponse((List) a[0], (long) a[1]) + ); static { PARSER.declareObjectArray(constructorArg(), MlFilter.PARSER, RESULTS_FIELD); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetInfluencersRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetInfluencersRequest.java index c82738a9050f4..b174f4c91f1af 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetInfluencersRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetInfluencersRequest.java @@ -10,8 +10,8 @@ import org.elasticsearch.client.Validatable; import org.elasticsearch.client.core.PageParams; import org.elasticsearch.client.ml.job.config.Job; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -31,7 +31,9 @@ public class GetInfluencersRequest implements Validatable, ToXContentObject { public static final ParseField DESCENDING = new ParseField("desc"); public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "get_influencers_request", a -> new GetInfluencersRequest((String) a[0])); + "get_influencers_request", + a -> new GetInfluencersRequest((String) a[0]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), Job.ID); @@ -198,13 +200,13 @@ public boolean equals(Object obj) { return false; } GetInfluencersRequest other = (GetInfluencersRequest) obj; - return Objects.equals(jobId, other.jobId) && - Objects.equals(excludeInterim, other.excludeInterim) && - Objects.equals(influencerScore, other.influencerScore) && - Objects.equals(pageParams, other.pageParams) && - Objects.equals(start, other.start) && - Objects.equals(end, other.end) && - Objects.equals(sort, other.sort) && - Objects.equals(descending, other.descending); + return Objects.equals(jobId, other.jobId) + && Objects.equals(excludeInterim, other.excludeInterim) + && Objects.equals(influencerScore, other.influencerScore) + && Objects.equals(pageParams, other.pageParams) + && Objects.equals(start, other.start) + && Objects.equals(end, other.end) + && Objects.equals(sort, other.sort) + && Objects.equals(descending, other.descending); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetInfluencersResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetInfluencersResponse.java index 536cd76d8488b..6d075c7fb535d 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetInfluencersResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetInfluencersResponse.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml; import org.elasticsearch.client.ml.job.results.Influencer; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -25,7 +25,10 @@ public class GetInfluencersResponse extends AbstractResultResponse { @SuppressWarnings("unchecked") public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "get_influencers_response", true, a -> new GetInfluencersResponse((List) a[0], (long) a[1])); + "get_influencers_response", + true, + a -> new GetInfluencersResponse((List) a[0], (long) a[1]) + ); static { PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), Influencer.PARSER, INFLUENCERS); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetJobRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetJobRequest.java index 2efa0ec71d9d9..037af8a412132 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetJobRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetJobRequest.java @@ -9,8 +9,8 @@ import org.elasticsearch.client.Validatable; import org.elasticsearch.client.ml.job.config.Job; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -41,7 +41,9 @@ public class GetJobRequest implements Validatable, ToXContentObject { @SuppressWarnings("unchecked") public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( "get_job_request", - true, a -> new GetJobRequest(a[0] == null ? new ArrayList<>() : (List) a[0])); + true, + a -> new GetJobRequest(a[0] == null ? new ArrayList<>() : (List) a[0]) + ); static { PARSER.declareStringArray(ConstructingObjectParser.optionalConstructorArg(), JOB_IDS); @@ -123,9 +125,9 @@ public boolean equals(Object other) { } GetJobRequest that = (GetJobRequest) other; - return Objects.equals(jobIds, that.jobIds) && - Objects.equals(excludeGenerated, that.excludeGenerated) && - Objects.equals(allowNoMatch, that.allowNoMatch); + return Objects.equals(jobIds, that.jobIds) + && Objects.equals(excludeGenerated, that.excludeGenerated) + && Objects.equals(allowNoMatch, that.allowNoMatch); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetJobResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetJobResponse.java index 1239867a321dc..ccc40edf3687d 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetJobResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetJobResponse.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.ml; import org.elasticsearch.client.ml.job.config.Job; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -28,9 +28,11 @@ public class GetJobResponse extends AbstractResultResponse { public static final ParseField RESULTS_FIELD = new ParseField("jobs"); @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("jobs_response", true, - a -> new GetJobResponse((List) a[0], (long) a[1])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "jobs_response", + true, + a -> new GetJobResponse((List) a[0], (long) a[1]) + ); static { PARSER.declareObjectArray(constructorArg(), Job.PARSER, RESULTS_FIELD); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetJobStatsRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetJobStatsRequest.java index d74840f0da1c0..009b0239e276d 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetJobStatsRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetJobStatsRequest.java @@ -9,10 +9,10 @@ import org.elasticsearch.client.Validatable; import org.elasticsearch.client.ml.job.config.Job; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -22,7 +22,6 @@ import java.util.List; import java.util.Objects; - /** * Request object to get {@link org.elasticsearch.client.ml.job.stats.JobStats} by their respective jobIds * @@ -35,12 +34,17 @@ public class GetJobStatsRequest implements Validatable, ToXContentObject { @SuppressWarnings("unchecked") public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "get_jobs_stats_request", a -> new GetJobStatsRequest((List) a[0])); + "get_jobs_stats_request", + a -> new GetJobStatsRequest((List) a[0]) + ); static { - PARSER.declareField(ConstructingObjectParser.constructorArg(), + PARSER.declareField( + ConstructingObjectParser.constructorArg(), p -> Arrays.asList(Strings.commaDelimitedListToStringArray(p.text())), - Job.ID, ObjectParser.ValueType.STRING_ARRAY); + Job.ID, + ObjectParser.ValueType.STRING_ARRAY + ); PARSER.declareBoolean(GetJobStatsRequest::setAllowNoMatch, ALLOW_NO_MATCH); } @@ -54,7 +58,7 @@ public class GetJobStatsRequest implements Validatable, ToXContentObject { * * @return a {@link GetJobStatsRequest} for all existing jobs */ - public static GetJobStatsRequest getAllJobStatsRequest(){ + public static GetJobStatsRequest getAllJobStatsRequest() { return new GetJobStatsRequest(ALL_JOBS); } @@ -112,8 +116,7 @@ public boolean equals(Object other) { } GetJobStatsRequest that = (GetJobStatsRequest) other; - return Objects.equals(jobIds, that.jobIds) && - Objects.equals(allowNoMatch, that.allowNoMatch); + return Objects.equals(jobIds, that.jobIds) && Objects.equals(allowNoMatch, that.allowNoMatch); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetJobStatsResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetJobStatsResponse.java index 655a6c24e0d5d..3443010fe66a4 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetJobStatsResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetJobStatsResponse.java @@ -7,11 +7,11 @@ */ package org.elasticsearch.client.ml; -import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.client.ml.job.stats.JobStats; import org.elasticsearch.common.Strings; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.client.ml.job.stats.JobStats; import java.io.IOException; import java.util.List; @@ -22,14 +22,16 @@ /** * Contains a {@link List} of the found {@link JobStats} objects and the total count found */ -public class GetJobStatsResponse extends AbstractResultResponse { +public class GetJobStatsResponse extends AbstractResultResponse { public static final ParseField RESULTS_FIELD = new ParseField("jobs"); @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("jobs_stats_response", true, - a -> new GetJobStatsResponse((List) a[0], (long) a[1])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "jobs_stats_response", + true, + a -> new GetJobStatsResponse((List) a[0], (long) a[1]) + ); static { PARSER.declareObjectArray(constructorArg(), JobStats.PARSER, RESULTS_FIELD); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetModelSnapshotsRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetModelSnapshotsRequest.java index cbbc527bb0a42..d6ecbf18a2444 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetModelSnapshotsRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetModelSnapshotsRequest.java @@ -10,8 +10,8 @@ import org.elasticsearch.client.Validatable; import org.elasticsearch.client.core.PageParams; import org.elasticsearch.client.ml.job.config.Job; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -23,7 +23,6 @@ */ public class GetModelSnapshotsRequest implements Validatable, ToXContentObject { - public static final ParseField SNAPSHOT_ID = new ParseField("snapshot_id"); public static final ParseField SORT = new ParseField("sort"); public static final ParseField START = new ParseField("start"); @@ -31,8 +30,9 @@ public class GetModelSnapshotsRequest implements Validatable, ToXContentObject { public static final ParseField DESC = new ParseField("desc"); public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "get_model_snapshots_request", a -> new GetModelSnapshotsRequest((String) a[0])); - + "get_model_snapshots_request", + a -> new GetModelSnapshotsRequest((String) a[0]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), Job.ID); @@ -114,7 +114,6 @@ public void setStart(String start) { this.start = start; } - public String getEnd() { return end; } @@ -162,7 +161,8 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws } if (pageParams != null) { builder.field(PageParams.PAGE.getPreferredName(), pageParams); - } builder.endObject(); + } + builder.endObject(); return builder; } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetModelSnapshotsResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetModelSnapshotsResponse.java index f8b4deceef69b..b52055ced3046 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetModelSnapshotsResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetModelSnapshotsResponse.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml; import org.elasticsearch.client.ml.job.process.ModelSnapshot; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -25,9 +25,11 @@ public class GetModelSnapshotsResponse extends AbstractResultResponse PARSER = - new ConstructingObjectParser<>("get_model_snapshots_response", true, - a -> new GetModelSnapshotsResponse((List) a[0], (long) a[1])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "get_model_snapshots_response", + true, + a -> new GetModelSnapshotsResponse((List) a[0], (long) a[1]) + ); static { PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), ModelSnapshot.PARSER, SNAPSHOTS); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetOverallBucketsRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetOverallBucketsRequest.java index 2735c8f203997..628fcc804d423 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetOverallBucketsRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetOverallBucketsRequest.java @@ -9,10 +9,10 @@ import org.elasticsearch.client.Validatable; import org.elasticsearch.client.ml.job.config.Job; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -39,7 +39,9 @@ public class GetOverallBucketsRequest implements Validatable, ToXContentObject { @SuppressWarnings("unchecked") public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "get_overall_buckets_request", a -> new GetOverallBucketsRequest((String) a[0])); + "get_overall_buckets_request", + a -> new GetOverallBucketsRequest((String) a[0]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), Job.ID); @@ -237,13 +239,13 @@ public boolean equals(Object obj) { return false; } GetOverallBucketsRequest other = (GetOverallBucketsRequest) obj; - return Objects.equals(jobIds, other.jobIds) && - Objects.equals(topN, other.topN) && - Objects.equals(bucketSpan, other.bucketSpan) && - Objects.equals(excludeInterim, other.excludeInterim) && - Objects.equals(overallScore, other.overallScore) && - Objects.equals(start, other.start) && - Objects.equals(end, other.end) && - Objects.equals(allowNoMatch, other.allowNoMatch); + return Objects.equals(jobIds, other.jobIds) + && Objects.equals(topN, other.topN) + && Objects.equals(bucketSpan, other.bucketSpan) + && Objects.equals(excludeInterim, other.excludeInterim) + && Objects.equals(overallScore, other.overallScore) + && Objects.equals(start, other.start) + && Objects.equals(end, other.end) + && Objects.equals(allowNoMatch, other.allowNoMatch); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetOverallBucketsResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetOverallBucketsResponse.java index f41f1ef2d45bf..a75b740c99a14 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetOverallBucketsResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetOverallBucketsResponse.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml; import org.elasticsearch.client.ml.job.results.OverallBucket; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -25,7 +25,10 @@ public class GetOverallBucketsResponse extends AbstractResultResponse PARSER = new ConstructingObjectParser<>( - "get_overall_buckets_response", true, a -> new GetOverallBucketsResponse((List) a[0], (long) a[1])); + "get_overall_buckets_response", + true, + a -> new GetOverallBucketsResponse((List) a[0], (long) a[1]) + ); static { PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), OverallBucket.PARSER, OVERALL_BUCKETS); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetRecordsRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetRecordsRequest.java index 872725d7d3b8d..c3ebcd1f86e99 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetRecordsRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetRecordsRequest.java @@ -10,8 +10,8 @@ import org.elasticsearch.client.Validatable; import org.elasticsearch.client.core.PageParams; import org.elasticsearch.client.ml.job.config.Job; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -199,13 +199,13 @@ public boolean equals(Object obj) { return false; } GetRecordsRequest other = (GetRecordsRequest) obj; - return Objects.equals(jobId, other.jobId) && - Objects.equals(excludeInterim, other.excludeInterim) && - Objects.equals(recordScore, other.recordScore) && - Objects.equals(pageParams, other.pageParams) && - Objects.equals(start, other.start) && - Objects.equals(end, other.end) && - Objects.equals(sort, other.sort) && - Objects.equals(descending, other.descending); + return Objects.equals(jobId, other.jobId) + && Objects.equals(excludeInterim, other.excludeInterim) + && Objects.equals(recordScore, other.recordScore) + && Objects.equals(pageParams, other.pageParams) + && Objects.equals(start, other.start) + && Objects.equals(end, other.end) + && Objects.equals(sort, other.sort) + && Objects.equals(descending, other.descending); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetRecordsResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetRecordsResponse.java index 226fcee6cdffd..3f94a06211ade 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetRecordsResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetRecordsResponse.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml; import org.elasticsearch.client.ml.job.results.AnomalyRecord; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -24,8 +24,11 @@ public class GetRecordsResponse extends AbstractResultResponse { public static final ParseField RECORDS = new ParseField("records"); @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("get_records_response", - true, a -> new GetRecordsResponse((List) a[0], (long) a[1])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "get_records_response", + true, + a -> new GetRecordsResponse((List) a[0], (long) a[1]) + ); static { PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), AnomalyRecord.PARSER, RECORDS); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetTrainedModelsResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetTrainedModelsResponse.java index 0548ff6c72dc9..9fb7cf8f7fd13 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetTrainedModelsResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetTrainedModelsResponse.java @@ -9,8 +9,8 @@ package org.elasticsearch.client.ml; import org.elasticsearch.client.ml.inference.TrainedModelConfig; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.util.List; @@ -24,11 +24,11 @@ public class GetTrainedModelsResponse { public static final ParseField COUNT = new ParseField("count"); @SuppressWarnings("unchecked") - static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>( - "get_trained_model_configs", - true, - args -> new GetTrainedModelsResponse((List) args[0], (Long) args[1])); + static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "get_trained_model_configs", + true, + args -> new GetTrainedModelsResponse((List) args[0], (Long) args[1]) + ); static { PARSER.declareObjectArray(constructorArg(), (p, c) -> TrainedModelConfig.fromXContent(p), TRAINED_MODEL_CONFIGS); @@ -42,7 +42,6 @@ public static GetTrainedModelsResponse fromXContent(final XContentParser parser) private final List trainedModels; private final Long count; - public GetTrainedModelsResponse(List trainedModels, Long count) { this.trainedModels = trainedModels; this.count = count; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetTrainedModelsStatsResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetTrainedModelsStatsResponse.java index a020c33493c22..ca218657cce83 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetTrainedModelsStatsResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetTrainedModelsStatsResponse.java @@ -9,8 +9,8 @@ package org.elasticsearch.client.ml; import org.elasticsearch.client.ml.inference.TrainedModelStats; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.util.List; @@ -24,11 +24,11 @@ public class GetTrainedModelsStatsResponse { public static final ParseField COUNT = new ParseField("count"); @SuppressWarnings("unchecked") - static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>( - "get_trained_model_stats", - true, - args -> new GetTrainedModelsStatsResponse((List) args[0], (Long) args[1])); + static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "get_trained_model_stats", + true, + args -> new GetTrainedModelsStatsResponse((List) args[0], (Long) args[1]) + ); static { PARSER.declareObjectArray(constructorArg(), (p, c) -> TrainedModelStats.fromXContent(p), TRAINED_MODEL_STATS); @@ -42,7 +42,6 @@ public static GetTrainedModelsStatsResponse fromXContent(final XContentParser pa private final List trainedModelStats; private final Long count; - public GetTrainedModelsStatsResponse(List trainedModelStats, Long count) { this.trainedModelStats = trainedModelStats; this.count = count; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/MlInfoRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/MlInfoRequest.java index 9b79f9a374d2e..6c5f1787fd183 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/MlInfoRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/MlInfoRequest.java @@ -10,5 +10,4 @@ import org.elasticsearch.client.Validatable; -public class MlInfoRequest implements Validatable { -} +public class MlInfoRequest implements Validatable {} diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/NodeAttributes.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/NodeAttributes.java index 955510d87cade..4a732c9523415 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/NodeAttributes.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/NodeAttributes.java @@ -7,10 +7,10 @@ */ package org.elasticsearch.client.ml; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -31,27 +31,22 @@ public class NodeAttributes implements ToXContentObject { public static final ParseField ATTRIBUTES = new ParseField("attributes"); @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("node", true, - (a) -> { - int i = 0; - String id = (String) a[i++]; - String name = (String) a[i++]; - String ephemeralId = (String) a[i++]; - String transportAddress = (String) a[i++]; - Map attributes = (Map) a[i]; - return new NodeAttributes(id, name, ephemeralId, transportAddress, attributes); - }); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("node", true, (a) -> { + int i = 0; + String id = (String) a[i++]; + String name = (String) a[i++]; + String ephemeralId = (String) a[i++]; + String transportAddress = (String) a[i++]; + Map attributes = (Map) a[i]; + return new NodeAttributes(id, name, ephemeralId, transportAddress, attributes); + }); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), ID); PARSER.declareString(ConstructingObjectParser.constructorArg(), NAME); PARSER.declareString(ConstructingObjectParser.constructorArg(), EPHEMERAL_ID); PARSER.declareString(ConstructingObjectParser.constructorArg(), TRANSPORT_ADDRESS); - PARSER.declareField(ConstructingObjectParser.constructorArg(), - (p, c) -> p.mapStrings(), - ATTRIBUTES, - ObjectParser.ValueType.OBJECT); + PARSER.declareField(ConstructingObjectParser.constructorArg(), (p, c) -> p.mapStrings(), ATTRIBUTES, ObjectParser.ValueType.OBJECT); } private final String id; @@ -131,11 +126,11 @@ public boolean equals(Object other) { } NodeAttributes that = (NodeAttributes) other; - return Objects.equals(id, that.id) && - Objects.equals(name, that.name) && - Objects.equals(ephemeralId, that.ephemeralId) && - Objects.equals(transportAddress, that.transportAddress) && - Objects.equals(attributes, that.attributes); + return Objects.equals(id, that.id) + && Objects.equals(name, that.name) + && Objects.equals(ephemeralId, that.ephemeralId) + && Objects.equals(transportAddress, that.transportAddress) + && Objects.equals(attributes, that.attributes); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/OpenJobRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/OpenJobRequest.java index 74a8113936bfd..c19ff484242ad 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/OpenJobRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/OpenJobRequest.java @@ -9,10 +9,10 @@ import org.elasticsearch.client.Validatable; import org.elasticsearch.client.ml.job.config.Job; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -27,7 +27,10 @@ public class OpenJobRequest implements Validatable, ToXContentObject { public static final ParseField TIMEOUT = new ParseField("timeout"); public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "open_job_request", true, a -> new OpenJobRequest((String) a[0])); + "open_job_request", + true, + a -> new OpenJobRequest((String) a[0]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), Job.ID); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/OpenJobResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/OpenJobResponse.java index a918719d37629..a9c6118db26d6 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/OpenJobResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/OpenJobResponse.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -24,9 +24,11 @@ public class OpenJobResponse implements ToXContentObject { private static final ParseField OPENED = new ParseField("opened"); private static final ParseField NODE = new ParseField("node"); - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("open_job_response", true, - (a) -> new OpenJobResponse((Boolean) a[0], (String) a[1])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "open_job_response", + true, + (a) -> new OpenJobResponse((Boolean) a[0], (String) a[1]) + ); static { PARSER.declareBoolean(ConstructingObjectParser.constructorArg(), OPENED); @@ -77,8 +79,7 @@ public boolean equals(Object other) { } OpenJobResponse that = (OpenJobResponse) other; - return opened == that.opened - && Objects.equals(node, that.node); + return opened == that.opened && Objects.equals(node, that.node); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/PostCalendarEventRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/PostCalendarEventRequest.java index 0918e98221bb2..0752221e9eee0 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/PostCalendarEventRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/PostCalendarEventRequest.java @@ -10,8 +10,8 @@ import org.elasticsearch.client.Validatable; import org.elasticsearch.client.ml.calendars.Calendar; import org.elasticsearch.client.ml.calendars.ScheduledEvent; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -32,17 +32,18 @@ public class PostCalendarEventRequest implements Validatable, ToXContentObject { public static final ParseField EVENTS = new ParseField("events"); @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("post_calendar_event_request", - a -> new PostCalendarEventRequest((String)a[0], (List)a[1])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "post_calendar_event_request", + a -> new PostCalendarEventRequest((String) a[0], (List) a[1]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), Calendar.ID); - PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), - (p, c) -> ScheduledEvent.PARSER.apply(p, null), EVENTS); + PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), (p, c) -> ScheduledEvent.PARSER.apply(p, null), EVENTS); } - public static final MapParams EXCLUDE_CALENDAR_ID_PARAMS = - new MapParams(Collections.singletonMap(INCLUDE_CALENDAR_ID_KEY, Boolean.toString(false))); + public static final MapParams EXCLUDE_CALENDAR_ID_PARAMS = new MapParams( + Collections.singletonMap(INCLUDE_CALENDAR_ID_KEY, Boolean.toString(false)) + ); /** * Create a new PostCalendarEventRequest with an existing non-null calendarId and a list of Scheduled events diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/PostCalendarEventResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/PostCalendarEventResponse.java index 1ef8bd3c73852..4aeb8da98f260 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/PostCalendarEventResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/PostCalendarEventResponse.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml; import org.elasticsearch.client.ml.calendars.ScheduledEvent; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -27,14 +27,14 @@ public class PostCalendarEventResponse implements ToXContentObject { public static final ParseField EVENTS = new ParseField("events"); @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("post_calendar_event_response", - true, - a -> new PostCalendarEventResponse((List)a[0])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "post_calendar_event_response", + true, + a -> new PostCalendarEventResponse((List) a[0]) + ); static { - PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), - (p, c) -> ScheduledEvent.PARSER.apply(p, null), EVENTS); + PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), (p, c) -> ScheduledEvent.PARSER.apply(p, null), EVENTS); } public static PostCalendarEventResponse fromXContent(XContentParser parser) throws IOException { @@ -63,7 +63,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws } @Override - public int hashCode(){ + public int hashCode() { return Objects.hash(scheduledEvents); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/PostDataRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/PostDataRequest.java index 2eb4c445aca26..5918f15c412c4 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/PostDataRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/PostDataRequest.java @@ -9,10 +9,10 @@ import org.elasticsearch.client.Validatable; import org.elasticsearch.client.ml.job.config.Job; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentType; @@ -34,9 +34,10 @@ public class PostDataRequest implements Validatable, ToXContentObject { public static final ParseField RESET_END = new ParseField("reset_end"); public static final ParseField CONTENT_TYPE = new ParseField("content_type"); - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("post_data_request", - (a) -> new PostDataRequest((String)a[0], XContentType.fromMediaType((String)a[1]), new byte[0])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "post_data_request", + (a) -> new PostDataRequest((String) a[0], XContentType.fromMediaType((String) a[1]), new byte[0]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), Job.ID); @@ -125,13 +126,13 @@ public XContentType getXContentType() { @Override public int hashCode() { - //We leave out the content for server side parity + // We leave out the content for server side parity return Objects.hash(jobId, resetStart, resetEnd, xContentType); } @Override public boolean equals(Object obj) { - if(obj == this) { + if (obj == this) { return true; } @@ -139,12 +140,12 @@ public boolean equals(Object obj) { return false; } - //We leave out the content for server side parity + // We leave out the content for server side parity PostDataRequest other = (PostDataRequest) obj; - return Objects.equals(jobId, other.jobId) && - Objects.equals(resetStart, other.resetStart) && - Objects.equals(resetEnd, other.resetEnd) && - Objects.equals(xContentType, other.xContentType); + return Objects.equals(jobId, other.jobId) + && Objects.equals(resetStart, other.resetStart) + && Objects.equals(resetEnd, other.resetEnd) + && Objects.equals(xContentType, other.xContentType); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/PreviewDatafeedRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/PreviewDatafeedRequest.java index 7c869b26f7dfe..8bd277fa31efc 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/PreviewDatafeedRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/PreviewDatafeedRequest.java @@ -10,10 +10,10 @@ import org.elasticsearch.client.Validatable; import org.elasticsearch.client.ml.datafeed.DatafeedConfig; import org.elasticsearch.client.ml.job.config.Job; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -31,7 +31,8 @@ public class PreviewDatafeedRequest implements Validatable, ToXContentObject { public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( "preview_datafeed_request", - a -> new PreviewDatafeedRequest((String) a[0], (DatafeedConfig.Builder) a[1], (Job.Builder) a[2])); + a -> new PreviewDatafeedRequest((String) a[0], (DatafeedConfig.Builder) a[1], (Job.Builder) a[2]) + ); static { PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), DatafeedConfig.ID); @@ -47,9 +48,11 @@ public static PreviewDatafeedRequest fromXContent(XContentParser parser) throws private final DatafeedConfig datafeedConfig; private final Job jobConfig; - private PreviewDatafeedRequest(@Nullable String datafeedId, - @Nullable DatafeedConfig.Builder datafeedConfig, - @Nullable Job.Builder jobConfig) { + private PreviewDatafeedRequest( + @Nullable String datafeedId, + @Nullable DatafeedConfig.Builder datafeedConfig, + @Nullable Job.Builder jobConfig + ) { this.datafeedId = datafeedId; this.datafeedConfig = datafeedConfig == null ? null : datafeedConfig.build(); this.jobConfig = jobConfig == null ? null : jobConfig.build(); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/PreviewDatafeedResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/PreviewDatafeedResponse.java index 32047300c9927..44ed4e40cd165 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/PreviewDatafeedResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/PreviewDatafeedResponse.java @@ -57,12 +57,14 @@ public BytesReference getPreview() { */ @SuppressWarnings("unchecked") public List> getDataList() throws IOException { - try(StreamInput streamInput = preview.streamInput(); + try ( + StreamInput streamInput = preview.streamInput(); XContentParser parser = XContentType.JSON.xContent() - .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, streamInput)) { + .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, streamInput) + ) { XContentParser.Token token = parser.nextToken(); if (token == XContentParser.Token.START_ARRAY) { - return parser.listOrderedMap().stream().map(obj -> (Map)obj).collect(Collectors.toList()); + return parser.listOrderedMap().stream().map(obj -> (Map) obj).collect(Collectors.toList()); } else { return Collections.singletonList(parser.mapOrdered()); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/PutCalendarJobRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/PutCalendarJobRequest.java index 5256ffe926094..cba01a764f6ca 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/PutCalendarJobRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/PutCalendarJobRequest.java @@ -65,7 +65,6 @@ public boolean equals(Object other) { } PutCalendarJobRequest that = (PutCalendarJobRequest) other; - return Objects.equals(jobIds, that.jobIds) && - Objects.equals(calendarId, that.calendarId); + return Objects.equals(jobIds, that.jobIds) && Objects.equals(calendarId, that.calendarId); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/PutTrainedModelRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/PutTrainedModelRequest.java index 0ecf345deb722..5276713c921be 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/PutTrainedModelRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/PutTrainedModelRequest.java @@ -17,7 +17,6 @@ import java.io.IOException; import java.util.Objects; - public class PutTrainedModelRequest implements Validatable, ToXContentObject { private final TrainedModelConfig config; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/PutTrainedModelResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/PutTrainedModelResponse.java index 25cb73e582056..dabcc7d24cc0f 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/PutTrainedModelResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/PutTrainedModelResponse.java @@ -15,7 +15,6 @@ import java.io.IOException; import java.util.Objects; - public class PutTrainedModelResponse implements ToXContentObject { private final TrainedModelConfig trainedModelConfig; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/RevertModelSnapshotRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/RevertModelSnapshotRequest.java index 30df3a6754a41..0295d72b7d9c5 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/RevertModelSnapshotRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/RevertModelSnapshotRequest.java @@ -10,8 +10,8 @@ import org.elasticsearch.client.Validatable; import org.elasticsearch.client.ml.job.config.Job; import org.elasticsearch.client.ml.job.process.ModelSnapshot; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -23,12 +23,12 @@ */ public class RevertModelSnapshotRequest implements Validatable, ToXContentObject { - public static final ParseField DELETE_INTERVENING = new ParseField("delete_intervening_results"); public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "revert_model_snapshots_request", a -> new RevertModelSnapshotRequest((String) a[0], (String) a[1])); - + "revert_model_snapshots_request", + a -> new RevertModelSnapshotRequest((String) a[0], (String) a[1]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), Job.ID); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/RevertModelSnapshotResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/RevertModelSnapshotResponse.java index 6f196187c082f..6110569ac9197 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/RevertModelSnapshotResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/RevertModelSnapshotResponse.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml; import org.elasticsearch.client.ml.job.process.ModelSnapshot; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -24,9 +24,11 @@ public class RevertModelSnapshotResponse implements ToXContentObject { private static final ParseField MODEL = new ParseField("model"); - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("revert_model_snapshot_response", true, - a -> new RevertModelSnapshotResponse((ModelSnapshot.Builder) a[0])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "revert_model_snapshot_response", + true, + a -> new RevertModelSnapshotResponse((ModelSnapshot.Builder) a[0]) + ); static { PARSER.declareObject(ConstructingObjectParser.constructorArg(), ModelSnapshot.PARSER, MODEL); @@ -46,7 +48,7 @@ public RevertModelSnapshotResponse(ModelSnapshot.Builder modelSnapshot) { * Get full information about the reverted model snapshot * @return the reverted model snapshot. */ - public ModelSnapshot getModel() { + public ModelSnapshot getModel() { return model; } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/SetUpgradeModeRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/SetUpgradeModeRequest.java index 84a802ac6f381..7e03117fd13d4 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/SetUpgradeModeRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/SetUpgradeModeRequest.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml; import org.elasticsearch.client.Validatable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.xcontent.ParseField; import java.util.Objects; @@ -18,7 +18,6 @@ */ public class SetUpgradeModeRequest implements Validatable { - public static final ParseField ENABLED = new ParseField("enabled"); public static final ParseField TIMEOUT = new ParseField("timeout"); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/StartDataFrameAnalyticsRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/StartDataFrameAnalyticsRequest.java index cc4c1b62e5ff9..0bb09846e7047 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/StartDataFrameAnalyticsRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/StartDataFrameAnalyticsRequest.java @@ -52,8 +52,7 @@ public boolean equals(Object o) { if (o == null || getClass() != o.getClass()) return false; StartDataFrameAnalyticsRequest other = (StartDataFrameAnalyticsRequest) o; - return Objects.equals(id, other.id) - && Objects.equals(timeout, other.timeout); + return Objects.equals(id, other.id) && Objects.equals(timeout, other.timeout); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/StartDataFrameAnalyticsResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/StartDataFrameAnalyticsResponse.java index e29cee1184c64..a158ad9eae705 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/StartDataFrameAnalyticsResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/StartDataFrameAnalyticsResponse.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml; import org.elasticsearch.action.support.master.AcknowledgedResponse; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -23,11 +23,11 @@ public class StartDataFrameAnalyticsResponse extends AcknowledgedResponse { private static final ParseField NODE = new ParseField("node"); - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>( - "start_data_frame_analytics_response", - true, - (a) -> new StartDataFrameAnalyticsResponse((Boolean) a[0], (String) a[1])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "start_data_frame_analytics_response", + true, + (a) -> new StartDataFrameAnalyticsResponse((Boolean) a[0], (String) a[1]) + ); static { declareAcknowledgedField(PARSER); @@ -68,8 +68,7 @@ public boolean equals(Object other) { } StartDataFrameAnalyticsResponse that = (StartDataFrameAnalyticsResponse) other; - return isAcknowledged() == that.isAcknowledged() - && Objects.equals(node, that.node); + return isAcknowledged() == that.isAcknowledged() && Objects.equals(node, that.node); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/StartDatafeedRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/StartDatafeedRequest.java index eddc257f5271b..48a9d41192215 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/StartDatafeedRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/StartDatafeedRequest.java @@ -9,9 +9,9 @@ import org.elasticsearch.client.Validatable; import org.elasticsearch.client.ml.datafeed.DatafeedConfig; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -27,15 +27,16 @@ public class StartDatafeedRequest implements Validatable, ToXContentObject { public static final ParseField END = new ParseField("end"); public static final ParseField TIMEOUT = new ParseField("timeout"); - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("start_datafeed_request", a -> new StartDatafeedRequest((String)a[0])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "start_datafeed_request", + a -> new StartDatafeedRequest((String) a[0]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), DatafeedConfig.ID); PARSER.declareString(StartDatafeedRequest::setStart, START); PARSER.declareString(StartDatafeedRequest::setEnd, END); - PARSER.declareString((params, val) -> - params.setTimeout(TimeValue.parseTimeValue(val, TIMEOUT.getPreferredName())), TIMEOUT); + PARSER.declareString((params, val) -> params.setTimeout(TimeValue.parseTimeValue(val, TIMEOUT.getPreferredName())), TIMEOUT); } private final String datafeedId; @@ -118,10 +119,10 @@ public boolean equals(Object obj) { } StartDatafeedRequest other = (StartDatafeedRequest) obj; - return Objects.equals(datafeedId, other.datafeedId) && - Objects.equals(start, other.start) && - Objects.equals(end, other.end) && - Objects.equals(timeout, other.timeout); + return Objects.equals(datafeedId, other.datafeedId) + && Objects.equals(start, other.start) + && Objects.equals(end, other.end) + && Objects.equals(timeout, other.timeout); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/StartDatafeedResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/StartDatafeedResponse.java index a59efd100ed24..25417797bb6ba 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/StartDatafeedResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/StartDatafeedResponse.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -24,11 +24,11 @@ public class StartDatafeedResponse implements ToXContentObject { private static final ParseField STARTED = new ParseField("started"); private static final ParseField NODE = new ParseField("node"); - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>( - "start_datafeed_response", - true, - (a) -> new StartDatafeedResponse((Boolean) a[0], (String) a[1])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "start_datafeed_response", + true, + (a) -> new StartDatafeedResponse((Boolean) a[0], (String) a[1]) + ); static { PARSER.declareBoolean(ConstructingObjectParser.constructorArg(), STARTED); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/StopDataFrameAnalyticsRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/StopDataFrameAnalyticsRequest.java index 4d3b1939a2d52..c8263bed50fac 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/StopDataFrameAnalyticsRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/StopDataFrameAnalyticsRequest.java @@ -11,8 +11,8 @@ import org.elasticsearch.client.Validatable; import org.elasticsearch.client.ValidationException; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.xcontent.ParseField; import java.util.Objects; import java.util.Optional; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/StopDataFrameAnalyticsResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/StopDataFrameAnalyticsResponse.java index 66b7cd9d6f3cb..9c4dc1d67be5c 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/StopDataFrameAnalyticsResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/StopDataFrameAnalyticsResponse.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -23,11 +23,11 @@ public class StopDataFrameAnalyticsResponse implements ToXContentObject { private static final ParseField STOPPED = new ParseField("stopped"); - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>( - "stop_data_frame_analytics_response", - true, - args -> new StopDataFrameAnalyticsResponse((Boolean) args[0])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "stop_data_frame_analytics_response", + true, + args -> new StopDataFrameAnalyticsResponse((Boolean) args[0]) + ); static { PARSER.declareBoolean(ConstructingObjectParser.constructorArg(), STOPPED); @@ -68,9 +68,6 @@ public int hashCode() { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder - .startObject() - .field(STOPPED.getPreferredName(), stopped) - .endObject(); + return builder.startObject().field(STOPPED.getPreferredName(), stopped).endObject(); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/StopDatafeedRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/StopDatafeedRequest.java index d9c9dd4dc42e3..9c62b0a4d2bf4 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/StopDatafeedRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/StopDatafeedRequest.java @@ -9,11 +9,11 @@ import org.elasticsearch.client.Validatable; import org.elasticsearch.client.ml.datafeed.DatafeedConfig; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -36,12 +36,16 @@ public class StopDatafeedRequest implements Validatable, ToXContentObject { @SuppressWarnings("unchecked") public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( "stop_datafeed_request", - a -> new StopDatafeedRequest((List) a[0])); + a -> new StopDatafeedRequest((List) a[0]) + ); static { - PARSER.declareField(ConstructingObjectParser.constructorArg(), + PARSER.declareField( + ConstructingObjectParser.constructorArg(), p -> Arrays.asList(Strings.commaDelimitedListToStringArray(p.text())), - DatafeedConfig.ID, ObjectParser.ValueType.STRING_ARRAY); + DatafeedConfig.ID, + ObjectParser.ValueType.STRING_ARRAY + ); PARSER.declareString((obj, val) -> obj.setTimeout(TimeValue.parseTimeValue(val, TIMEOUT.getPreferredName())), TIMEOUT); PARSER.declareBoolean(StopDatafeedRequest::setForce, FORCE); PARSER.declareBoolean(StopDatafeedRequest::setAllowNoMatch, ALLOW_NO_MATCH); @@ -59,7 +63,7 @@ public class StopDatafeedRequest implements Validatable, ToXContentObject { * * @return a {@link StopDatafeedRequest} for all existing datafeeds */ - public static StopDatafeedRequest stopAllDatafeedsRequest(){ + public static StopDatafeedRequest stopAllDatafeedsRequest() { return new StopDatafeedRequest(ALL_DATAFEEDS); } @@ -148,10 +152,10 @@ public boolean equals(Object other) { } StopDatafeedRequest that = (StopDatafeedRequest) other; - return Objects.equals(datafeedIds, that.datafeedIds) && - Objects.equals(timeout, that.timeout) && - Objects.equals(force, that.force) && - Objects.equals(allowNoMatch, that.allowNoMatch); + return Objects.equals(datafeedIds, that.datafeedIds) + && Objects.equals(timeout, that.timeout) + && Objects.equals(force, that.force) + && Objects.equals(allowNoMatch, that.allowNoMatch); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/StopDatafeedResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/StopDatafeedResponse.java index f67c54bb45a65..864b9ea6d4127 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/StopDatafeedResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/StopDatafeedResponse.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -23,11 +23,11 @@ public class StopDatafeedResponse implements ToXContentObject { private static final ParseField STOPPED = new ParseField("stopped"); - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>( - "stop_datafeed_response", - true, - (a) -> new StopDatafeedResponse((Boolean)a[0])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "stop_datafeed_response", + true, + (a) -> new StopDatafeedResponse((Boolean) a[0]) + ); static { PARSER.declareBoolean(ConstructingObjectParser.constructorArg(), STOPPED); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/UpdateFilterRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/UpdateFilterRequest.java index 6bdc73871f557..3a76432715067 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/UpdateFilterRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/UpdateFilterRequest.java @@ -9,9 +9,9 @@ import org.elasticsearch.client.Validatable; import org.elasticsearch.client.ml.job.config.MlFilter; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -29,8 +29,10 @@ public class UpdateFilterRequest implements Validatable, ToXContentObject { public static final ParseField ADD_ITEMS = new ParseField("add_items"); public static final ParseField REMOVE_ITEMS = new ParseField("remove_items"); - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("update_filter_request", (a) -> new UpdateFilterRequest((String)a[0])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "update_filter_request", + (a) -> new UpdateFilterRequest((String) a[0]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), MlFilter.ID); @@ -77,8 +79,7 @@ public SortedSet getAddItems() { * @param addItems non-null items to add to the filter, defaults to empty array */ public void setAddItems(Collection addItems) { - this.addItems = new TreeSet<>(Objects.requireNonNull(addItems, - "[" + ADD_ITEMS.getPreferredName()+"] must not be null")); + this.addItems = new TreeSet<>(Objects.requireNonNull(addItems, "[" + ADD_ITEMS.getPreferredName() + "] must not be null")); } public SortedSet getRemoveItems() { @@ -90,8 +91,7 @@ public SortedSet getRemoveItems() { * @param removeItems non-null items to remove from the filter, defaults to empty array */ public void setRemoveItems(Collection removeItems) { - this.removeItems = new TreeSet<>(Objects.requireNonNull(removeItems, - "[" + REMOVE_ITEMS.getPreferredName()+"] must not be null")); + this.removeItems = new TreeSet<>(Objects.requireNonNull(removeItems, "[" + REMOVE_ITEMS.getPreferredName() + "] must not be null")); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/UpdateModelSnapshotRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/UpdateModelSnapshotRequest.java index aaa85c3e2bf82..a06880369f6fa 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/UpdateModelSnapshotRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/UpdateModelSnapshotRequest.java @@ -22,10 +22,10 @@ */ public class UpdateModelSnapshotRequest implements Validatable, ToXContentObject { - public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "update_model_snapshot_request", a -> new UpdateModelSnapshotRequest((String) a[0], (String) a[1])); - + "update_model_snapshot_request", + a -> new UpdateModelSnapshotRequest((String) a[0], (String) a[1]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), Job.ID); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/UpdateModelSnapshotResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/UpdateModelSnapshotResponse.java index e7115008021a0..82b73fe260aa9 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/UpdateModelSnapshotResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/UpdateModelSnapshotResponse.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml; import org.elasticsearch.client.ml.job.process.ModelSnapshot; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -31,9 +31,11 @@ public UpdateModelSnapshotResponse(boolean acknowledged, ModelSnapshot.Builder m this.model = modelSnapshot.build(); } - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("update_model_snapshot_response", true, - a -> new UpdateModelSnapshotResponse((Boolean) a[0], ((ModelSnapshot.Builder) a[1]))); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "update_model_snapshot_response", + true, + a -> new UpdateModelSnapshotResponse((Boolean) a[0], ((ModelSnapshot.Builder) a[1])) + ); static { PARSER.declareBoolean(ConstructingObjectParser.constructorArg(), ACKNOWLEDGED); @@ -59,7 +61,7 @@ public Boolean getAcknowledged() { * Get the updated snapshot of the model * @return the updated model snapshot. */ - public ModelSnapshot getModel() { + public ModelSnapshot getModel() { return model; } @@ -81,7 +83,6 @@ public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params par return builder; } - @Override public boolean equals(Object obj) { if (obj == null) { @@ -91,7 +92,6 @@ public boolean equals(Object obj) { return false; } UpdateModelSnapshotResponse request = (UpdateModelSnapshotResponse) obj; - return Objects.equals(acknowledged, request.acknowledged) - && Objects.equals(model, request.model); + return Objects.equals(acknowledged, request.acknowledged) && Objects.equals(model, request.model); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/UpgradeJobModelSnapshotRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/UpgradeJobModelSnapshotRequest.java index 240916857f0c3..cc1660ed4dc6b 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/UpgradeJobModelSnapshotRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/UpgradeJobModelSnapshotRequest.java @@ -9,9 +9,9 @@ import org.elasticsearch.client.Validatable; import org.elasticsearch.client.ml.job.config.Job; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -28,7 +28,8 @@ public class UpgradeJobModelSnapshotRequest implements Validatable, ToXContentOb private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( "upgrade_job_snapshot_request", true, - a -> new UpgradeJobModelSnapshotRequest((String) a[0], (String) a[1], (String) a[2], (Boolean) a[3])); + a -> new UpgradeJobModelSnapshotRequest((String) a[0], (String) a[1], (String) a[2], (Boolean) a[3]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), Job.ID); @@ -43,10 +44,7 @@ public class UpgradeJobModelSnapshotRequest implements Validatable, ToXContentOb private final Boolean waitForCompletion; UpgradeJobModelSnapshotRequest(String jobId, String snapshotId, String timeout, Boolean waitForCompletion) { - this(jobId, - snapshotId, - timeout == null ? null : TimeValue.parseTimeValue(timeout, TIMEOUT.getPreferredName()), - waitForCompletion); + this(jobId, snapshotId, timeout == null ? null : TimeValue.parseTimeValue(timeout, TIMEOUT.getPreferredName()), waitForCompletion); } public UpgradeJobModelSnapshotRequest(String jobId, String snapshotId, TimeValue timeValue, Boolean waitForCompletion) { @@ -81,10 +79,10 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; UpgradeJobModelSnapshotRequest request = (UpgradeJobModelSnapshotRequest) o; - return Objects.equals(jobId, request.jobId) && - Objects.equals(timeout, request.timeout) && - Objects.equals(waitForCompletion, request.waitForCompletion) && - Objects.equals(snapshotId, request.snapshotId); + return Objects.equals(jobId, request.jobId) + && Objects.equals(timeout, request.timeout) + && Objects.equals(waitForCompletion, request.waitForCompletion) + && Objects.equals(snapshotId, request.snapshotId); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/UpgradeJobModelSnapshotResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/UpgradeJobModelSnapshotResponse.java index c23da3b93bdf2..b260bbaa5d22b 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/UpgradeJobModelSnapshotResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/UpgradeJobModelSnapshotResponse.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -21,9 +21,11 @@ public class UpgradeJobModelSnapshotResponse implements ToXContentObject { private static final ParseField COMPLETED = new ParseField("completed"); private static final ParseField NODE = new ParseField("node"); - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("upgrade_job_snapshot_response", true, - (a) -> new UpgradeJobModelSnapshotResponse((Boolean) a[0], (String) a[1])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "upgrade_job_snapshot_response", + true, + (a) -> new UpgradeJobModelSnapshotResponse((Boolean) a[0], (String) a[1]) + ); static { PARSER.declareBoolean(ConstructingObjectParser.optionalConstructorArg(), COMPLETED); @@ -66,8 +68,7 @@ public boolean equals(Object other) { } UpgradeJobModelSnapshotResponse that = (UpgradeJobModelSnapshotResponse) other; - return completed == that.completed - && Objects.equals(node, that.node); + return completed == that.completed && Objects.equals(node, that.node); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/calendars/Calendar.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/calendars/Calendar.java index 006d708d22607..da7c0ac2cac52 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/calendars/Calendar.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/calendars/Calendar.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml.calendars; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -32,9 +32,11 @@ public class Calendar implements ToXContentObject { public static final ParseField DESCRIPTION = new ParseField("description"); @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(CALENDAR_TYPE, true, a -> - new Calendar((String) a[0], (List) a[1], (String) a[2])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + CALENDAR_TYPE, + true, + a -> new Calendar((String) a[0], (List) a[1], (String) a[2]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), ID); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/calendars/ScheduledEvent.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/calendars/ScheduledEvent.java index af855eb442642..8aecc33d32b1a 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/calendars/ScheduledEvent.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/calendars/ScheduledEvent.java @@ -9,13 +9,12 @@ import org.elasticsearch.client.common.TimeUtil; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; - import java.io.IOException; import java.util.Date; import java.util.Objects; @@ -28,16 +27,26 @@ public class ScheduledEvent implements ToXContentObject { public static final ParseField EVENT_ID = new ParseField("event_id"); public static final String SCHEDULED_EVENT_TYPE = "scheduled_event"; - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(SCHEDULED_EVENT_TYPE, true, a -> - new ScheduledEvent((String) a[0], (Date) a[1], (Date) a[2], (String) a[3], (String) a[4])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + SCHEDULED_EVENT_TYPE, + true, + a -> new ScheduledEvent((String) a[0], (Date) a[1], (Date) a[2], (String) a[3], (String) a[4]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), DESCRIPTION); - PARSER.declareField(ConstructingObjectParser.constructorArg(),(p) -> TimeUtil.parseTimeField(p, START_TIME.getPreferredName()), - START_TIME, ObjectParser.ValueType.VALUE); - PARSER.declareField(ConstructingObjectParser.constructorArg(),(p) -> TimeUtil.parseTimeField(p, END_TIME.getPreferredName()), - END_TIME, ObjectParser.ValueType.VALUE); + PARSER.declareField( + ConstructingObjectParser.constructorArg(), + (p) -> TimeUtil.parseTimeField(p, START_TIME.getPreferredName()), + START_TIME, + ObjectParser.ValueType.VALUE + ); + PARSER.declareField( + ConstructingObjectParser.constructorArg(), + (p) -> TimeUtil.parseTimeField(p, END_TIME.getPreferredName()), + END_TIME, + ObjectParser.ValueType.VALUE + ); PARSER.declareString(ConstructingObjectParser.constructorArg(), Calendar.ID); PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), EVENT_ID); } @@ -102,9 +111,9 @@ public boolean equals(Object obj) { ScheduledEvent other = (ScheduledEvent) obj; return Objects.equals(this.description, other.description) - && Objects.equals(this.startTime, other.startTime) - && Objects.equals(this.endTime, other.endTime) - && Objects.equals(this.calendarId, other.calendarId); + && Objects.equals(this.startTime, other.startTime) + && Objects.equals(this.endTime, other.endTime) + && Objects.equals(this.calendarId, other.calendarId); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/datafeed/ChunkingConfig.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/datafeed/ChunkingConfig.java index 869719451e42a..5f23f2478c070 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/datafeed/ChunkingConfig.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/datafeed/ChunkingConfig.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.ml.datafeed; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -27,21 +27,24 @@ public class ChunkingConfig implements ToXContentObject { public static final ParseField TIME_SPAN_FIELD = new ParseField("time_span"); public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "chunking_config", true, a -> new ChunkingConfig((Mode) a[0], (TimeValue) a[1])); + "chunking_config", + true, + a -> new ChunkingConfig((Mode) a[0], (TimeValue) a[1]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), Mode::fromString, MODE_FIELD); PARSER.declareString( ConstructingObjectParser.optionalConstructorArg(), text -> TimeValue.parseTimeValue(text, TIME_SPAN_FIELD.getPreferredName()), - TIME_SPAN_FIELD); + TIME_SPAN_FIELD + ); } private final Mode mode; private final TimeValue timeSpan; - ChunkingConfig(Mode mode, @Nullable TimeValue timeSpan) { this.mode = Objects.requireNonNull(mode, MODE_FIELD.getPreferredName()); this.timeSpan = timeSpan; @@ -83,8 +86,7 @@ public boolean equals(Object obj) { } ChunkingConfig other = (ChunkingConfig) obj; - return Objects.equals(this.mode, other.mode) && - Objects.equals(this.timeSpan, other.timeSpan); + return Objects.equals(this.mode, other.mode) && Objects.equals(this.timeSpan, other.timeSpan); } public static ChunkingConfig newAuto() { @@ -100,7 +102,9 @@ public static ChunkingConfig newManual(TimeValue timeSpan) { } public enum Mode { - AUTO, MANUAL, OFF; + AUTO, + MANUAL, + OFF; public static Mode fromString(String value) { return Mode.valueOf(value.toUpperCase(Locale.ROOT)); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/datafeed/DatafeedConfig.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/datafeed/DatafeedConfig.java index 8199020ec4592..e1363239f4e44 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/datafeed/DatafeedConfig.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/datafeed/DatafeedConfig.java @@ -9,21 +9,21 @@ import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.client.ml.job.config.Job; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.index.query.QueryBuilder; +import org.elasticsearch.search.aggregations.AggregatorFactories; +import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.index.query.QueryBuilder; -import org.elasticsearch.search.aggregations.AggregatorFactories; -import org.elasticsearch.search.builder.SearchSourceBuilder; import java.io.IOException; import java.util.ArrayList; @@ -57,7 +57,10 @@ public class DatafeedConfig implements ToXContentObject { public static final ParseField INDICES_OPTIONS = new ParseField("indices_options"); public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "datafeed_config", true, a -> new Builder((String)a[0], (String)a[1])); + "datafeed_config", + true, + a -> new Builder((String) a[0], (String) a[1]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), ID); @@ -65,10 +68,14 @@ public class DatafeedConfig implements ToXContentObject { PARSER.declareStringArray(Builder::setIndices, INDEXES); PARSER.declareStringArray(Builder::setIndices, INDICES); - PARSER.declareString((builder, val) -> - builder.setQueryDelay(TimeValue.parseTimeValue(val, QUERY_DELAY.getPreferredName())), QUERY_DELAY); - PARSER.declareString((builder, val) -> - builder.setFrequency(TimeValue.parseTimeValue(val, FREQUENCY.getPreferredName())), FREQUENCY); + PARSER.declareString( + (builder, val) -> builder.setQueryDelay(TimeValue.parseTimeValue(val, QUERY_DELAY.getPreferredName())), + QUERY_DELAY + ); + PARSER.declareString( + (builder, val) -> builder.setFrequency(TimeValue.parseTimeValue(val, FREQUENCY.getPreferredName())), + FREQUENCY + ); PARSER.declareField(Builder::setQuery, DatafeedConfig::parseBytes, QUERY, ObjectParser.ValueType.OBJECT); PARSER.declareField(Builder::setAggregations, DatafeedConfig::parseBytes, AGGREGATIONS, ObjectParser.ValueType.OBJECT); PARSER.declareObject(Builder::setScriptFields, (p, c) -> { @@ -82,9 +89,11 @@ public class DatafeedConfig implements ToXContentObject { PARSER.declareObject(Builder::setChunkingConfig, ChunkingConfig.PARSER, CHUNKING_CONFIG); PARSER.declareObject(Builder::setDelayedDataCheckConfig, DelayedDataCheckConfig.PARSER, DELAYED_DATA_CHECK_CONFIG); PARSER.declareInt(Builder::setMaxEmptySearches, MAX_EMPTY_SEARCHES); - PARSER.declareObject(Builder::setIndicesOptions, + PARSER.declareObject( + Builder::setIndicesOptions, (p, c) -> IndicesOptions.fromMap(p.map(), new IndicesOptions(IndicesOptions.Option.NONE, IndicesOptions.WildcardStates.NONE)), - INDICES_OPTIONS); + INDICES_OPTIONS + ); PARSER.declareObject(Builder::setRuntimeMappings, (p, c) -> p.map(), SearchSourceBuilder.RUNTIME_MAPPINGS_FIELD); } @@ -109,10 +118,22 @@ private static BytesReference parseBytes(XContentParser parser) throws IOExcepti private final IndicesOptions indicesOptions; private final Map runtimeMappings; - private DatafeedConfig(String id, String jobId, TimeValue queryDelay, TimeValue frequency, List indices, BytesReference query, - BytesReference aggregations, List scriptFields, Integer scrollSize, - ChunkingConfig chunkingConfig, DelayedDataCheckConfig delayedDataCheckConfig, - Integer maxEmptySearches, IndicesOptions indicesOptions, Map runtimeMappings) { + private DatafeedConfig( + String id, + String jobId, + TimeValue queryDelay, + TimeValue frequency, + List indices, + BytesReference query, + BytesReference aggregations, + List scriptFields, + Integer scrollSize, + ChunkingConfig chunkingConfig, + DelayedDataCheckConfig delayedDataCheckConfig, + Integer maxEmptySearches, + IndicesOptions indicesOptions, + Map runtimeMappings + ) { this.id = id; this.jobId = jobId; this.queryDelay = queryDelay; @@ -285,8 +306,22 @@ public boolean equals(Object other) { */ @Override public int hashCode() { - return Objects.hash(id, jobId, frequency, queryDelay, indices, asMap(query), scrollSize, asMap(aggregations), scriptFields, - chunkingConfig, delayedDataCheckConfig, maxEmptySearches, indicesOptions, runtimeMappings); + return Objects.hash( + id, + jobId, + frequency, + queryDelay, + indices, + asMap(query), + scrollSize, + asMap(aggregations), + scriptFields, + chunkingConfig, + delayedDataCheckConfig, + maxEmptySearches, + indicesOptions, + runtimeMappings + ); } public static Builder builder(String id, String jobId) { @@ -423,14 +458,27 @@ public Builder setIndicesOptions(IndicesOptions indicesOptions) { } public Builder setRuntimeMappings(Map runtimeMappings) { - this.runtimeMappings = Objects.requireNonNull(runtimeMappings, - SearchSourceBuilder.RUNTIME_MAPPINGS_FIELD.getPreferredName()); + this.runtimeMappings = Objects.requireNonNull(runtimeMappings, SearchSourceBuilder.RUNTIME_MAPPINGS_FIELD.getPreferredName()); return this; } public DatafeedConfig build() { - return new DatafeedConfig(id, jobId, queryDelay, frequency, indices, query, aggregations, scriptFields, scrollSize, - chunkingConfig, delayedDataCheckConfig, maxEmptySearches, indicesOptions, runtimeMappings); + return new DatafeedConfig( + id, + jobId, + queryDelay, + frequency, + indices, + query, + aggregations, + scriptFields, + scrollSize, + chunkingConfig, + delayedDataCheckConfig, + maxEmptySearches, + indicesOptions, + runtimeMappings + ); } private static BytesReference xContentToBytes(ToXContentObject object) throws IOException { diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/datafeed/DatafeedState.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/datafeed/DatafeedState.java index d72b8e98a9430..4d309c31ab375 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/datafeed/DatafeedState.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/datafeed/DatafeedState.java @@ -16,7 +16,10 @@ */ public enum DatafeedState { - STARTED, STOPPED, STARTING, STOPPING; + STARTED, + STOPPED, + STARTING, + STOPPING; public static final ParseField STATE = new ParseField("state"); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/datafeed/DatafeedStats.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/datafeed/DatafeedStats.java index 6fdd70f66d9b6..b218f749a10f3 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/datafeed/DatafeedStats.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/datafeed/DatafeedStats.java @@ -9,8 +9,8 @@ import org.elasticsearch.client.ml.NodeAttributes; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -37,16 +37,14 @@ public class DatafeedStats implements ToXContentObject { public static final ParseField NODE = new ParseField("node"); public static final ParseField TIMING_STATS = new ParseField("timing_stats"); - public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("datafeed_stats", - true, - a -> { - String datafeedId = (String)a[0]; - DatafeedState datafeedState = DatafeedState.fromString((String)a[1]); - NodeAttributes nodeAttributes = (NodeAttributes)a[2]; - String assignmentExplanation = (String)a[3]; - DatafeedTimingStats timingStats = (DatafeedTimingStats)a[4]; + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("datafeed_stats", true, a -> { + String datafeedId = (String) a[0]; + DatafeedState datafeedState = DatafeedState.fromString((String) a[1]); + NodeAttributes nodeAttributes = (NodeAttributes) a[2]; + String assignmentExplanation = (String) a[3]; + DatafeedTimingStats timingStats = (DatafeedTimingStats) a[4]; return new DatafeedStats(datafeedId, datafeedState, nodeAttributes, assignmentExplanation, timingStats); - } ); + }); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), DatafeedConfig.ID); @@ -56,8 +54,13 @@ public class DatafeedStats implements ToXContentObject { PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), DatafeedTimingStats.PARSER, TIMING_STATS); } - public DatafeedStats(String datafeedId, DatafeedState datafeedState, @Nullable NodeAttributes node, - @Nullable String assignmentExplanation, @Nullable DatafeedTimingStats timingStats) { + public DatafeedStats( + String datafeedId, + DatafeedState datafeedState, + @Nullable NodeAttributes node, + @Nullable String assignmentExplanation, + @Nullable DatafeedTimingStats timingStats + ) { this.datafeedId = Objects.requireNonNull(datafeedId); this.datafeedState = Objects.requireNonNull(datafeedState); this.node = node; @@ -130,10 +133,10 @@ public boolean equals(Object obj) { return false; } DatafeedStats other = (DatafeedStats) obj; - return Objects.equals(datafeedId, other.datafeedId) && - Objects.equals(this.datafeedState, other.datafeedState) && - Objects.equals(this.node, other.node) && - Objects.equals(this.assignmentExplanation, other.assignmentExplanation) && - Objects.equals(this.timingStats, other.timingStats); + return Objects.equals(datafeedId, other.datafeedId) + && Objects.equals(this.datafeedState, other.datafeedState) + && Objects.equals(this.node, other.node) + && Objects.equals(this.assignmentExplanation, other.assignmentExplanation) + && Objects.equals(this.timingStats, other.timingStats); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/datafeed/DatafeedTimingStats.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/datafeed/DatafeedTimingStats.java index 229d69dfa7875..2da81faaa8d13 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/datafeed/DatafeedTimingStats.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/datafeed/DatafeedTimingStats.java @@ -7,10 +7,10 @@ */ package org.elasticsearch.client.ml.datafeed; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -35,25 +35,22 @@ public class DatafeedTimingStats implements ToXContentObject { public static final ConstructingObjectParser PARSER = createParser(); private static ConstructingObjectParser createParser() { - ConstructingObjectParser parser = - new ConstructingObjectParser<>( - "datafeed_timing_stats", - true, - args -> { - String jobId = (String) args[0]; - Long searchCount = (Long) args[1]; - Long bucketCount = (Long) args[2]; - Double totalSearchTimeMs = (Double) args[3]; - Double avgSearchTimePerBucketMs = (Double) args[4]; - Double exponentialAvgSearchTimePerHourMs = (Double) args[5]; - return new DatafeedTimingStats( - jobId, - getOrDefault(searchCount, 0L), - getOrDefault(bucketCount, 0L), - getOrDefault(totalSearchTimeMs, 0.0), - avgSearchTimePerBucketMs, - exponentialAvgSearchTimePerHourMs); - }); + ConstructingObjectParser parser = new ConstructingObjectParser<>("datafeed_timing_stats", true, args -> { + String jobId = (String) args[0]; + Long searchCount = (Long) args[1]; + Long bucketCount = (Long) args[2]; + Double totalSearchTimeMs = (Double) args[3]; + Double avgSearchTimePerBucketMs = (Double) args[4]; + Double exponentialAvgSearchTimePerHourMs = (Double) args[5]; + return new DatafeedTimingStats( + jobId, + getOrDefault(searchCount, 0L), + getOrDefault(bucketCount, 0L), + getOrDefault(totalSearchTimeMs, 0.0), + avgSearchTimePerBucketMs, + exponentialAvgSearchTimePerHourMs + ); + }); parser.declareString(constructorArg(), JOB_ID); parser.declareLong(optionalConstructorArg(), SEARCH_COUNT); parser.declareLong(optionalConstructorArg(), BUCKET_COUNT); @@ -71,12 +68,13 @@ private static ConstructingObjectParser createParser( private Double exponentialAvgSearchTimePerHourMs; public DatafeedTimingStats( - String jobId, - long searchCount, - long bucketCount, - double totalSearchTimeMs, - @Nullable Double avgSearchTimePerBucketMs, - @Nullable Double exponentialAvgSearchTimePerHourMs) { + String jobId, + long searchCount, + long bucketCount, + double totalSearchTimeMs, + @Nullable Double avgSearchTimePerBucketMs, + @Nullable Double exponentialAvgSearchTimePerHourMs + ) { this.jobId = Objects.requireNonNull(jobId); this.searchCount = searchCount; this.bucketCount = bucketCount; @@ -152,7 +150,8 @@ public int hashCode() { bucketCount, totalSearchTimeMs, avgSearchTimePerBucketMs, - exponentialAvgSearchTimePerHourMs); + exponentialAvgSearchTimePerHourMs + ); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/datafeed/DatafeedUpdate.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/datafeed/DatafeedUpdate.java index b0dc021c375f3..3b4be882a868b 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/datafeed/DatafeedUpdate.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/datafeed/DatafeedUpdate.java @@ -8,21 +8,21 @@ package org.elasticsearch.client.ml.datafeed; import org.elasticsearch.action.support.IndicesOptions; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.index.query.QueryBuilder; +import org.elasticsearch.search.aggregations.AggregatorFactories; +import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.index.query.QueryBuilder; -import org.elasticsearch.search.aggregations.AggregatorFactories; -import org.elasticsearch.search.builder.SearchSourceBuilder; import java.io.IOException; import java.util.ArrayList; @@ -42,20 +42,31 @@ public class DatafeedUpdate implements ToXContentObject { public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "datafeed_update", true, a -> new Builder((String)a[0])); + "datafeed_update", + true, + a -> new Builder((String) a[0]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), DatafeedConfig.ID); PARSER.declareStringArray(Builder::setIndices, DatafeedConfig.INDEXES); PARSER.declareStringArray(Builder::setIndices, DatafeedConfig.INDICES); - PARSER.declareString((builder, val) -> builder.setQueryDelay( - TimeValue.parseTimeValue(val, DatafeedConfig.QUERY_DELAY.getPreferredName())), DatafeedConfig.QUERY_DELAY); - PARSER.declareString((builder, val) -> builder.setFrequency( - TimeValue.parseTimeValue(val, DatafeedConfig.FREQUENCY.getPreferredName())), DatafeedConfig.FREQUENCY); + PARSER.declareString( + (builder, val) -> builder.setQueryDelay(TimeValue.parseTimeValue(val, DatafeedConfig.QUERY_DELAY.getPreferredName())), + DatafeedConfig.QUERY_DELAY + ); + PARSER.declareString( + (builder, val) -> builder.setFrequency(TimeValue.parseTimeValue(val, DatafeedConfig.FREQUENCY.getPreferredName())), + DatafeedConfig.FREQUENCY + ); PARSER.declareField(Builder::setQuery, DatafeedUpdate::parseBytes, DatafeedConfig.QUERY, ObjectParser.ValueType.OBJECT); - PARSER.declareField(Builder::setAggregations, DatafeedUpdate::parseBytes, DatafeedConfig.AGGREGATIONS, - ObjectParser.ValueType.OBJECT); + PARSER.declareField( + Builder::setAggregations, + DatafeedUpdate::parseBytes, + DatafeedConfig.AGGREGATIONS, + ObjectParser.ValueType.OBJECT + ); PARSER.declareObject(Builder::setScriptFields, (p, c) -> { List parsedScriptFields = new ArrayList<>(); while (p.nextToken() != XContentParser.Token.END_OBJECT) { @@ -65,13 +76,13 @@ public class DatafeedUpdate implements ToXContentObject { }, DatafeedConfig.SCRIPT_FIELDS); PARSER.declareInt(Builder::setScrollSize, DatafeedConfig.SCROLL_SIZE); PARSER.declareObject(Builder::setChunkingConfig, ChunkingConfig.PARSER, DatafeedConfig.CHUNKING_CONFIG); - PARSER.declareObject(Builder::setDelayedDataCheckConfig, - DelayedDataCheckConfig.PARSER, - DatafeedConfig.DELAYED_DATA_CHECK_CONFIG); + PARSER.declareObject(Builder::setDelayedDataCheckConfig, DelayedDataCheckConfig.PARSER, DatafeedConfig.DELAYED_DATA_CHECK_CONFIG); PARSER.declareInt(Builder::setMaxEmptySearches, DatafeedConfig.MAX_EMPTY_SEARCHES); - PARSER.declareObject(Builder::setIndicesOptions, + PARSER.declareObject( + Builder::setIndicesOptions, (p, c) -> IndicesOptions.fromMap(p.map(), new IndicesOptions(IndicesOptions.Option.NONE, IndicesOptions.WildcardStates.NONE)), - DatafeedConfig.INDICES_OPTIONS); + DatafeedConfig.INDICES_OPTIONS + ); PARSER.declareObject(Builder::setRuntimeMappings, (p, c) -> p.map(), SearchSourceBuilder.RUNTIME_MAPPINGS_FIELD); } @@ -95,10 +106,21 @@ private static BytesReference parseBytes(XContentParser parser) throws IOExcepti private final IndicesOptions indicesOptions; private final Map runtimeMappings; - private DatafeedUpdate(String id, TimeValue queryDelay, TimeValue frequency, List indices, BytesReference query, - BytesReference aggregations, List scriptFields, Integer scrollSize, - ChunkingConfig chunkingConfig, DelayedDataCheckConfig delayedDataCheckConfig, - Integer maxEmptySearches, IndicesOptions indicesOptions, Map runtimeMappings) { + private DatafeedUpdate( + String id, + TimeValue queryDelay, + TimeValue frequency, + List indices, + BytesReference query, + BytesReference aggregations, + List scriptFields, + Integer scrollSize, + ChunkingConfig chunkingConfig, + DelayedDataCheckConfig delayedDataCheckConfig, + Integer maxEmptySearches, + IndicesOptions indicesOptions, + Map runtimeMappings + ) { this.id = id; this.queryDelay = queryDelay; this.frequency = frequency; @@ -262,8 +284,21 @@ public boolean equals(Object other) { */ @Override public int hashCode() { - return Objects.hash(id, frequency, queryDelay, indices, asMap(query), scrollSize, asMap(aggregations), scriptFields, - chunkingConfig, delayedDataCheckConfig, maxEmptySearches, indicesOptions, runtimeMappings); + return Objects.hash( + id, + frequency, + queryDelay, + indices, + asMap(query), + scrollSize, + asMap(aggregations), + scriptFields, + chunkingConfig, + delayedDataCheckConfig, + maxEmptySearches, + indicesOptions, + runtimeMappings + ); } public static Builder builder(String id) { @@ -393,8 +428,21 @@ public Builder setRuntimeMappings(Map runtimeMappings) { } public DatafeedUpdate build() { - return new DatafeedUpdate(id, queryDelay, frequency, indices, query, aggregations, scriptFields, scrollSize, - chunkingConfig, delayedDataCheckConfig, maxEmptySearches, indicesOptions, runtimeMappings); + return new DatafeedUpdate( + id, + queryDelay, + frequency, + indices, + query, + aggregations, + scriptFields, + scrollSize, + chunkingConfig, + delayedDataCheckConfig, + maxEmptySearches, + indicesOptions, + runtimeMappings + ); } private static BytesReference xContentToBytes(ToXContentObject object) throws IOException { diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/datafeed/DelayedDataCheckConfig.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/datafeed/DelayedDataCheckConfig.java index 7193baefd3911..4c55662f8b833 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/datafeed/DelayedDataCheckConfig.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/datafeed/DelayedDataCheckConfig.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.ml.datafeed; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -33,16 +33,20 @@ public class DelayedDataCheckConfig implements ToXContentObject { // These parsers follow the pattern that metadata is parsed leniently (to allow for enhancements), whilst config is parsed strictly public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "delayed_data_check_config", true, a -> new DelayedDataCheckConfig((Boolean) a[0], (TimeValue) a[1])); + "delayed_data_check_config", + true, + a -> new DelayedDataCheckConfig((Boolean) a[0], (TimeValue) a[1]) + ); static { PARSER.declareBoolean(ConstructingObjectParser.constructorArg(), ENABLED); PARSER.declareString( ConstructingObjectParser.optionalConstructorArg(), text -> TimeValue.parseTimeValue(text, CHECK_WINDOW.getPreferredName()), - CHECK_WINDOW); + CHECK_WINDOW + ); } - /** + /** * This creates a new DelayedDataCheckConfig that has a check_window of the passed `timeValue` * * We query the index to the latest finalized bucket from this TimeValue in the past looking to see if any data has been indexed diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/Classification.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/Classification.java index 1e7be753bccfd..b1fe4a5d1b87c 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/Classification.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/Classification.java @@ -9,10 +9,10 @@ import org.elasticsearch.client.ml.inference.NamedXContentObjectHelper; import org.elasticsearch.client.ml.inference.preprocessing.PreProcessor; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -55,32 +55,32 @@ public static Builder builder(String dependentVariable) { static final ParseField EARLY_STOPPING_ENABLED = new ParseField("early_stopping_enabled"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>( - NAME.getPreferredName(), - true, - a -> new Classification( - (String) a[0], - (Double) a[1], - (Double) a[2], - (Double) a[3], - (Integer) a[4], - (Double) a[5], - (Integer) a[6], - (String) a[7], - (Double) a[8], - (Integer) a[9], - (Long) a[10], - (ClassAssignmentObjective) a[11], - (List) a[12], - (Double) a[13], - (Double) a[14], - (Double) a[15], - (Double) a[16], - (Double) a[17], - (Integer) a[18], - (Boolean) a[19] - )); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME.getPreferredName(), + true, + a -> new Classification( + (String) a[0], + (Double) a[1], + (Double) a[2], + (Double) a[3], + (Integer) a[4], + (Double) a[5], + (Integer) a[6], + (String) a[7], + (Double) a[8], + (Integer) a[9], + (Long) a[10], + (ClassAssignmentObjective) a[11], + (List) a[12], + (Double) a[13], + (Double) a[14], + (Double) a[15], + (Double) a[16], + (Double) a[17], + (Integer) a[18], + (Boolean) a[19] + ) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), DEPENDENT_VARIABLE); @@ -95,11 +95,16 @@ public static Builder builder(String dependentVariable) { PARSER.declareInt(ConstructingObjectParser.optionalConstructorArg(), NUM_TOP_CLASSES); PARSER.declareLong(ConstructingObjectParser.optionalConstructorArg(), RANDOMIZE_SEED); PARSER.declareString( - ConstructingObjectParser.optionalConstructorArg(), ClassAssignmentObjective::fromString, CLASS_ASSIGNMENT_OBJECTIVE); - PARSER.declareNamedObjects(ConstructingObjectParser.optionalConstructorArg(), + ConstructingObjectParser.optionalConstructorArg(), + ClassAssignmentObjective::fromString, + CLASS_ASSIGNMENT_OBJECTIVE + ); + PARSER.declareNamedObjects( + ConstructingObjectParser.optionalConstructorArg(), (p, c, n) -> p.namedObject(PreProcessor.class, n, c), (classification) -> {}, - FEATURE_PROCESSORS); + FEATURE_PROCESSORS + ); PARSER.declareDouble(ConstructingObjectParser.optionalConstructorArg(), ALPHA); PARSER.declareDouble(ConstructingObjectParser.optionalConstructorArg(), ETA_GROWTH_RATE_PER_TREE); PARSER.declareDouble(ConstructingObjectParser.optionalConstructorArg(), SOFT_TREE_DEPTH_LIMIT); @@ -130,14 +135,28 @@ public static Builder builder(String dependentVariable) { private final Integer maxOptimizationRoundsPerHyperparameter; private final Boolean earlyStoppingEnabled; - private Classification(String dependentVariable, @Nullable Double lambda, @Nullable Double gamma, @Nullable Double eta, - @Nullable Integer maxTrees, @Nullable Double featureBagFraction, - @Nullable Integer numTopFeatureImportanceValues, @Nullable String predictionFieldName, - @Nullable Double trainingPercent, @Nullable Integer numTopClasses, @Nullable Long randomizeSeed, - @Nullable ClassAssignmentObjective classAssignmentObjective, @Nullable List featureProcessors, - @Nullable Double alpha, @Nullable Double etaGrowthRatePerTree, @Nullable Double softTreeDepthLimit, - @Nullable Double softTreeDepthTolerance, @Nullable Double downsampleFactor, - @Nullable Integer maxOptimizationRoundsPerHyperparameter, @Nullable Boolean earlyStoppingEnabled) { + private Classification( + String dependentVariable, + @Nullable Double lambda, + @Nullable Double gamma, + @Nullable Double eta, + @Nullable Integer maxTrees, + @Nullable Double featureBagFraction, + @Nullable Integer numTopFeatureImportanceValues, + @Nullable String predictionFieldName, + @Nullable Double trainingPercent, + @Nullable Integer numTopClasses, + @Nullable Long randomizeSeed, + @Nullable ClassAssignmentObjective classAssignmentObjective, + @Nullable List featureProcessors, + @Nullable Double alpha, + @Nullable Double etaGrowthRatePerTree, + @Nullable Double softTreeDepthLimit, + @Nullable Double softTreeDepthTolerance, + @Nullable Double downsampleFactor, + @Nullable Integer maxOptimizationRoundsPerHyperparameter, + @Nullable Boolean earlyStoppingEnabled + ) { this.dependentVariable = Objects.requireNonNull(dependentVariable); this.lambda = lambda; this.gamma = gamma; @@ -312,10 +331,28 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws @Override public int hashCode() { - return Objects.hash(dependentVariable, lambda, gamma, eta, maxTrees, featureBagFraction, numTopFeatureImportanceValues, - predictionFieldName, trainingPercent, randomizeSeed, numTopClasses, classAssignmentObjective, featureProcessors, alpha, - etaGrowthRatePerTree, softTreeDepthLimit, softTreeDepthTolerance, downsampleFactor, maxOptimizationRoundsPerHyperparameter, - earlyStoppingEnabled); + return Objects.hash( + dependentVariable, + lambda, + gamma, + eta, + maxTrees, + featureBagFraction, + numTopFeatureImportanceValues, + predictionFieldName, + trainingPercent, + randomizeSeed, + numTopClasses, + classAssignmentObjective, + featureProcessors, + alpha, + etaGrowthRatePerTree, + softTreeDepthLimit, + softTreeDepthTolerance, + downsampleFactor, + maxOptimizationRoundsPerHyperparameter, + earlyStoppingEnabled + ); } @Override @@ -351,7 +388,8 @@ public String toString() { } public enum ClassAssignmentObjective { - MAXIMIZE_ACCURACY, MAXIMIZE_MINIMUM_RECALL; + MAXIMIZE_ACCURACY, + MAXIMIZE_MINIMUM_RECALL; public static ClassAssignmentObjective fromString(String value) { return ClassAssignmentObjective.valueOf(value.toUpperCase(Locale.ROOT)); @@ -485,10 +523,28 @@ public Builder setEarlyStoppingEnabled(Boolean earlyStoppingEnabled) { } public Classification build() { - return new Classification(dependentVariable, lambda, gamma, eta, maxTrees, featureBagFraction, - numTopFeatureImportanceValues, predictionFieldName, trainingPercent, numTopClasses, randomizeSeed, - classAssignmentObjective, featureProcessors, alpha, etaGrowthRatePerTree, softTreeDepthLimit, softTreeDepthTolerance, - downsampleFactor, maxOptimizationRoundsPerHyperparameter, earlyStoppingEnabled); + return new Classification( + dependentVariable, + lambda, + gamma, + eta, + maxTrees, + featureBagFraction, + numTopFeatureImportanceValues, + predictionFieldName, + trainingPercent, + numTopClasses, + randomizeSeed, + classAssignmentObjective, + featureProcessors, + alpha, + etaGrowthRatePerTree, + softTreeDepthLimit, + softTreeDepthTolerance, + downsampleFactor, + maxOptimizationRoundsPerHyperparameter, + earlyStoppingEnabled + ); } } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsConfig.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsConfig.java index 5988905d4be6d..53c92d792fe20 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsConfig.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsConfig.java @@ -10,17 +10,17 @@ import org.elasticsearch.Version; import org.elasticsearch.client.common.TimeUtil; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.common.xcontent.XContentParserUtils; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.search.fetch.subphase.FetchSourceContext; import org.elasticsearch.xcontent.ObjectParser; import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.XContentParserUtils; -import org.elasticsearch.search.fetch.subphase.FetchSourceContext; import java.io.IOException; import java.time.Instant; @@ -56,18 +56,24 @@ public static Builder builder() { PARSER.declareObject(Builder::setSource, (p, c) -> DataFrameAnalyticsSource.fromXContent(p), SOURCE); PARSER.declareObject(Builder::setDest, (p, c) -> DataFrameAnalyticsDest.fromXContent(p), DEST); PARSER.declareObject(Builder::setAnalysis, (p, c) -> parseAnalysis(p), ANALYSIS); - PARSER.declareField(Builder::setAnalyzedFields, + PARSER.declareField( + Builder::setAnalyzedFields, (p, c) -> FetchSourceContext.fromXContent(p), ANALYZED_FIELDS, - ValueType.OBJECT_ARRAY_BOOLEAN_OR_STRING); - PARSER.declareField(Builder::setModelMemoryLimit, + ValueType.OBJECT_ARRAY_BOOLEAN_OR_STRING + ); + PARSER.declareField( + Builder::setModelMemoryLimit, (p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), MODEL_MEMORY_LIMIT.getPreferredName()), MODEL_MEMORY_LIMIT, - ValueType.VALUE); - PARSER.declareField(Builder::setCreateTime, + ValueType.VALUE + ); + PARSER.declareField( + Builder::setCreateTime, p -> TimeUtil.parseTimeFieldToInstant(p, CREATE_TIME.getPreferredName()), CREATE_TIME, - ValueType.VALUE); + ValueType.VALUE + ); PARSER.declareString(Builder::setVersion, Version::fromString, VERSION); PARSER.declareBoolean(Builder::setAllowLazyStart, ALLOW_LAZY_START); PARSER.declareInt(Builder::setMaxNumThreads, MAX_NUM_THREADS); @@ -93,11 +99,19 @@ private static DataFrameAnalysis parseAnalysis(XContentParser parser) throws IOE private final Boolean allowLazyStart; private final Integer maxNumThreads; - private DataFrameAnalyticsConfig(@Nullable String id, @Nullable String description, @Nullable DataFrameAnalyticsSource source, - @Nullable DataFrameAnalyticsDest dest, @Nullable DataFrameAnalysis analysis, - @Nullable FetchSourceContext analyzedFields, @Nullable ByteSizeValue modelMemoryLimit, - @Nullable Instant createTime, @Nullable Version version, @Nullable Boolean allowLazyStart, - @Nullable Integer maxNumThreads) { + private DataFrameAnalyticsConfig( + @Nullable String id, + @Nullable String description, + @Nullable DataFrameAnalyticsSource source, + @Nullable DataFrameAnalyticsDest dest, + @Nullable DataFrameAnalysis analysis, + @Nullable FetchSourceContext analyzedFields, + @Nullable ByteSizeValue modelMemoryLimit, + @Nullable Instant createTime, + @Nullable Version version, + @Nullable Boolean allowLazyStart, + @Nullable Integer maxNumThreads + ) { this.id = id; this.description = description; this.source = source; @@ -105,7 +119,8 @@ private DataFrameAnalyticsConfig(@Nullable String id, @Nullable String descripti this.analysis = analysis; this.analyzedFields = analyzedFields; this.modelMemoryLimit = modelMemoryLimit; - this.createTime = createTime == null ? null : Instant.ofEpochMilli(createTime.toEpochMilli());; + this.createTime = createTime == null ? null : Instant.ofEpochMilli(createTime.toEpochMilli()); + ; this.version = version; this.allowLazyStart = allowLazyStart; this.maxNumThreads = maxNumThreads; @@ -171,10 +186,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.field(DEST.getPreferredName(), dest); } if (analysis != null) { - builder - .startObject(ANALYSIS.getPreferredName()) - .field(analysis.getName(), analysis) - .endObject(); + builder.startObject(ANALYSIS.getPreferredName()).field(analysis.getName(), analysis).endObject(); } if (analyzedFields != null) { builder.field(ANALYZED_FIELDS.getPreferredName(), analyzedFields); @@ -219,8 +231,19 @@ public boolean equals(Object o) { @Override public int hashCode() { - return Objects.hash(id, description, source, dest, analysis, analyzedFields, modelMemoryLimit, createTime, version, allowLazyStart, - maxNumThreads); + return Objects.hash( + id, + description, + source, + dest, + analysis, + analyzedFields, + modelMemoryLimit, + createTime, + version, + allowLazyStart, + maxNumThreads + ); } @Override @@ -300,8 +323,19 @@ public Builder setMaxNumThreads(Integer maxNumThreads) { } public DataFrameAnalyticsConfig build() { - return new DataFrameAnalyticsConfig(id, description, source, dest, analysis, analyzedFields, modelMemoryLimit, createTime, - version, allowLazyStart, maxNumThreads); + return new DataFrameAnalyticsConfig( + id, + description, + source, + dest, + analysis, + analyzedFields, + modelMemoryLimit, + createTime, + version, + allowLazyStart, + maxNumThreads + ); } } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsConfigUpdate.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsConfigUpdate.java index 36696cca92387..4dccee1019ce1 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsConfigUpdate.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsConfigUpdate.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml.dataframe; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ObjectParser; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -38,7 +38,8 @@ public static Builder builder() { Builder::setModelMemoryLimit, (p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), DataFrameAnalyticsConfig.MODEL_MEMORY_LIMIT.getPreferredName()), DataFrameAnalyticsConfig.MODEL_MEMORY_LIMIT, - VALUE); + VALUE + ); PARSER.declareBoolean(Builder::setAllowLazyStart, DataFrameAnalyticsConfig.ALLOW_LAZY_START); PARSER.declareInt(Builder::setMaxNumThreads, DataFrameAnalyticsConfig.MAX_NUM_THREADS); } @@ -49,11 +50,13 @@ public static Builder builder() { private final Boolean allowLazyStart; private final Integer maxNumThreads; - private DataFrameAnalyticsConfigUpdate(String id, - @Nullable String description, - @Nullable ByteSizeValue modelMemoryLimit, - @Nullable Boolean allowLazyStart, - @Nullable Integer maxNumThreads) { + private DataFrameAnalyticsConfigUpdate( + String id, + @Nullable String description, + @Nullable ByteSizeValue modelMemoryLimit, + @Nullable Boolean allowLazyStart, + @Nullable Integer maxNumThreads + ) { this.id = id; this.description = description; this.modelMemoryLimit = modelMemoryLimit; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsDest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsDest.java index d447317caa2d2..fe576411f131b 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsDest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsDest.java @@ -8,10 +8,10 @@ package org.elasticsearch.client.ml.dataframe; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -74,8 +74,7 @@ public boolean equals(Object o) { if (o == null || getClass() != o.getClass()) return false; DataFrameAnalyticsDest other = (DataFrameAnalyticsDest) o; - return Objects.equals(index, other.index) - && Objects.equals(resultsField, other.resultsField); + return Objects.equals(index, other.index) && Objects.equals(resultsField, other.resultsField); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsSource.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsSource.java index e52399bdc1dcc..da9cf7aa15b44 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsSource.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsSource.java @@ -8,15 +8,15 @@ package org.elasticsearch.client.ml.dataframe; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.search.builder.SearchSourceBuilder; +import org.elasticsearch.search.fetch.subphase.FetchSourceContext; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.search.builder.SearchSourceBuilder; -import org.elasticsearch.search.fetch.subphase.FetchSourceContext; import java.io.IOException; import java.util.Arrays; @@ -43,10 +43,12 @@ public static Builder builder() { static { PARSER.declareStringArray(Builder::setIndex, INDEX); PARSER.declareObject(Builder::setQueryConfig, (p, c) -> QueryConfig.fromXContent(p), QUERY); - PARSER.declareField(Builder::setSourceFiltering, + PARSER.declareField( + Builder::setSourceFiltering, (p, c) -> FetchSourceContext.fromXContent(p), _SOURCE, - ObjectParser.ValueType.OBJECT_ARRAY_BOOLEAN_OR_STRING); + ObjectParser.ValueType.OBJECT_ARRAY_BOOLEAN_OR_STRING + ); PARSER.declareObject(Builder::setRuntimeMappings, (p, c) -> p.map(), SearchSourceBuilder.RUNTIME_MAPPINGS_FIELD); } @@ -55,8 +57,12 @@ public static Builder builder() { private final FetchSourceContext sourceFiltering; private final Map runtimeMappings; - private DataFrameAnalyticsSource(String[] index, @Nullable QueryConfig queryConfig, @Nullable FetchSourceContext sourceFiltering, - @Nullable Map runtimeMappings) { + private DataFrameAnalyticsSource( + String[] index, + @Nullable QueryConfig queryConfig, + @Nullable FetchSourceContext sourceFiltering, + @Nullable Map runtimeMappings + ) { this.index = Objects.requireNonNull(index); this.queryConfig = queryConfig; this.sourceFiltering = sourceFiltering; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsState.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsState.java index 8913e66a89c92..157ebe614f761 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsState.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsState.java @@ -11,7 +11,13 @@ import java.util.Locale; public enum DataFrameAnalyticsState { - STARTED, REINDEXING, ANALYZING, STOPPING, STOPPED, STARTING, FAILED; + STARTED, + REINDEXING, + ANALYZING, + STOPPING, + STOPPED, + STARTING, + FAILED; public static DataFrameAnalyticsState fromString(String name) { return valueOf(name.trim().toUpperCase(Locale.ROOT)); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsStats.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsStats.java index 58905ae9c8252..75eb216aed402 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsStats.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsStats.java @@ -12,12 +12,12 @@ import org.elasticsearch.client.ml.dataframe.stats.AnalysisStats; import org.elasticsearch.client.ml.dataframe.stats.common.DataCounts; import org.elasticsearch.client.ml.dataframe.stats.common.MemoryUsage; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.inject.internal.ToStringBuilder; +import org.elasticsearch.common.xcontent.XContentParserUtils; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.XContentParserUtils; import java.io.IOException; import java.util.List; @@ -43,18 +43,21 @@ public static DataFrameAnalyticsStats fromXContent(XContentParser parser) throws static final ParseField ASSIGNMENT_EXPLANATION = new ParseField("assignment_explanation"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("data_frame_analytics_stats", true, - args -> new DataFrameAnalyticsStats( - (String) args[0], - (DataFrameAnalyticsState) args[1], - (String) args[2], - (List) args[3], - (DataCounts) args[4], - (MemoryUsage) args[5], - (AnalysisStats) args[6], - (NodeAttributes) args[7], - (String) args[8])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "data_frame_analytics_stats", + true, + args -> new DataFrameAnalyticsStats( + (String) args[0], + (DataFrameAnalyticsState) args[1], + (String) args[2], + (List) args[3], + (DataCounts) args[4], + (MemoryUsage) args[5], + (AnalysisStats) args[6], + (NodeAttributes) args[7], + (String) args[8] + ) + ); static { PARSER.declareString(constructorArg(), ID); @@ -86,10 +89,17 @@ private static AnalysisStats parseAnalysisStats(XContentParser parser) throws IO private final NodeAttributes node; private final String assignmentExplanation; - public DataFrameAnalyticsStats(String id, DataFrameAnalyticsState state, @Nullable String failureReason, - @Nullable List progress, @Nullable DataCounts dataCounts, - @Nullable MemoryUsage memoryUsage, @Nullable AnalysisStats analysisStats, @Nullable NodeAttributes node, - @Nullable String assignmentExplanation) { + public DataFrameAnalyticsStats( + String id, + DataFrameAnalyticsState state, + @Nullable String failureReason, + @Nullable List progress, + @Nullable DataCounts dataCounts, + @Nullable MemoryUsage memoryUsage, + @Nullable AnalysisStats analysisStats, + @Nullable NodeAttributes node, + @Nullable String assignmentExplanation + ) { this.id = id; this.state = state; this.failureReason = failureReason; @@ -164,8 +174,7 @@ public int hashCode() { @Override public String toString() { - return new ToStringBuilder(getClass()) - .add("id", id) + return new ToStringBuilder(getClass()).add("id", id) .add("state", state) .add("failureReason", failureReason) .add("progress", progress) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/MlDataFrameAnalysisNamedXContentProvider.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/MlDataFrameAnalysisNamedXContentProvider.java index a67579da42863..562409b53df8d 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/MlDataFrameAnalysisNamedXContentProvider.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/MlDataFrameAnalysisNamedXContentProvider.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml.dataframe; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.plugins.spi.NamedXContentProvider; +import org.elasticsearch.xcontent.NamedXContentRegistry; import java.util.Arrays; import java.util.List; @@ -18,17 +18,9 @@ public class MlDataFrameAnalysisNamedXContentProvider implements NamedXContentPr @Override public List getNamedXContentParsers() { return Arrays.asList( - new NamedXContentRegistry.Entry( - DataFrameAnalysis.class, - OutlierDetection.NAME, - (p, c) -> OutlierDetection.fromXContent(p)), - new NamedXContentRegistry.Entry( - DataFrameAnalysis.class, - Regression.NAME, - (p, c) -> Regression.fromXContent(p)), - new NamedXContentRegistry.Entry( - DataFrameAnalysis.class, - Classification.NAME, - (p, c) -> Classification.fromXContent(p))); + new NamedXContentRegistry.Entry(DataFrameAnalysis.class, OutlierDetection.NAME, (p, c) -> OutlierDetection.fromXContent(p)), + new NamedXContentRegistry.Entry(DataFrameAnalysis.class, Regression.NAME, (p, c) -> Regression.fromXContent(p)), + new NamedXContentRegistry.Entry(DataFrameAnalysis.class, Classification.NAME, (p, c) -> Classification.fromXContent(p)) + ); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/OutlierDetection.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/OutlierDetection.java index a63662b338b89..559407ffcb2a0 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/OutlierDetection.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/OutlierDetection.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.ml.dataframe; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -81,8 +81,14 @@ public static Builder builder() { */ private final Boolean standardizationEnabled; - private OutlierDetection(Integer nNeighbors, Method method, Double featureInfluenceThreshold, Boolean computeFeatureInfluence, - Double outlierFraction, Boolean standardizationEnabled) { + private OutlierDetection( + Integer nNeighbors, + Method method, + Double featureInfluenceThreshold, + Boolean computeFeatureInfluence, + Double outlierFraction, + Boolean standardizationEnabled + ) { this.nNeighbors = nNeighbors; this.method = method; this.featureInfluenceThreshold = featureInfluenceThreshold; @@ -161,8 +167,14 @@ public boolean equals(Object o) { @Override public int hashCode() { - return Objects.hash(nNeighbors, method, featureInfluenceThreshold, computeFeatureInfluence, outlierFraction, - standardizationEnabled); + return Objects.hash( + nNeighbors, + method, + featureInfluenceThreshold, + computeFeatureInfluence, + outlierFraction, + standardizationEnabled + ); } @Override @@ -171,7 +183,10 @@ public String toString() { } public enum Method { - LOF, LDOF, DISTANCE_KTH_NN, DISTANCE_KNN; + LOF, + LDOF, + DISTANCE_KTH_NN, + DISTANCE_KNN; public static Method fromString(String value) { return Method.valueOf(value.toUpperCase(Locale.ROOT)); @@ -225,8 +240,14 @@ public Builder setStandardizationEnabled(Boolean standardizationEnabled) { } public OutlierDetection build() { - return new OutlierDetection(nNeighbors, method, featureInfluenceThreshold, computeFeatureInfluence, outlierFraction, - standardizationEnabled); + return new OutlierDetection( + nNeighbors, + method, + featureInfluenceThreshold, + computeFeatureInfluence, + outlierFraction, + standardizationEnabled + ); } } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/PhaseProgress.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/PhaseProgress.java index 9910cebbff305..f8d629586d2e7 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/PhaseProgress.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/PhaseProgress.java @@ -7,9 +7,9 @@ */ package org.elasticsearch.client.ml.dataframe; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.inject.internal.ToStringBuilder; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -24,8 +24,11 @@ public class PhaseProgress implements ToXContentObject { static final ParseField PHASE = new ParseField("phase"); static final ParseField PROGRESS_PERCENT = new ParseField("progress_percent"); - public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("phase_progress", - true, a -> new PhaseProgress((String) a[0], (int) a[1])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "phase_progress", + true, + a -> new PhaseProgress((String) a[0], (int) a[1]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), PHASE); @@ -63,8 +66,7 @@ public boolean equals(Object o) { @Override public String toString() { - return new ToStringBuilder(getClass()) - .add(PHASE.getPreferredName(), phase) + return new ToStringBuilder(getClass()).add(PHASE.getPreferredName(), phase) .add(PROGRESS_PERCENT.getPreferredName(), progressPercent) .toString(); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/QueryConfig.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/QueryConfig.java index f07bbe9c5df40..73dd0d82b2221 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/QueryConfig.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/QueryConfig.java @@ -9,11 +9,11 @@ package org.elasticsearch.client.ml.dataframe; import org.elasticsearch.common.Strings; +import org.elasticsearch.index.query.AbstractQueryBuilder; +import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.index.query.AbstractQueryBuilder; -import org.elasticsearch.index.query.QueryBuilder; import java.io.IOException; import java.util.Objects; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/Regression.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/Regression.java index 652583e8e801b..04f61d09305c2 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/Regression.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/Regression.java @@ -9,10 +9,10 @@ import org.elasticsearch.client.ml.inference.NamedXContentObjectHelper; import org.elasticsearch.client.ml.inference.preprocessing.PreProcessor; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -57,32 +57,32 @@ public static Builder builder(String dependentVariable) { static final ParseField EARLY_STOPPING_ENABLED = new ParseField("early_stopping_enabled"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>( - NAME.getPreferredName(), - true, - a -> new Regression( - (String) a[0], - (Double) a[1], - (Double) a[2], - (Double) a[3], - (Integer) a[4], - (Double) a[5], - (Integer) a[6], - (String) a[7], - (Double) a[8], - (Long) a[9], - (LossFunction) a[10], - (Double) a[11], - (List) a[12], - (Double) a[13], - (Double) a[14], - (Double) a[15], - (Double) a[16], - (Double) a[17], - (Integer) a[18], - (Boolean) a[19] - )); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME.getPreferredName(), + true, + a -> new Regression( + (String) a[0], + (Double) a[1], + (Double) a[2], + (Double) a[3], + (Integer) a[4], + (Double) a[5], + (Integer) a[6], + (String) a[7], + (Double) a[8], + (Long) a[9], + (LossFunction) a[10], + (Double) a[11], + (List) a[12], + (Double) a[13], + (Double) a[14], + (Double) a[15], + (Double) a[16], + (Double) a[17], + (Integer) a[18], + (Boolean) a[19] + ) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), DEPENDENT_VARIABLE); @@ -97,10 +97,12 @@ public static Builder builder(String dependentVariable) { PARSER.declareLong(ConstructingObjectParser.optionalConstructorArg(), RANDOMIZE_SEED); PARSER.declareString(optionalConstructorArg(), LossFunction::fromString, LOSS_FUNCTION); PARSER.declareDouble(ConstructingObjectParser.optionalConstructorArg(), LOSS_FUNCTION_PARAMETER); - PARSER.declareNamedObjects(ConstructingObjectParser.optionalConstructorArg(), + PARSER.declareNamedObjects( + ConstructingObjectParser.optionalConstructorArg(), (p, c, n) -> p.namedObject(PreProcessor.class, n, c), (regression) -> {}, - FEATURE_PROCESSORS); + FEATURE_PROCESSORS + ); PARSER.declareDouble(ConstructingObjectParser.optionalConstructorArg(), ALPHA); PARSER.declareDouble(ConstructingObjectParser.optionalConstructorArg(), ETA_GROWTH_RATE_PER_TREE); PARSER.declareDouble(ConstructingObjectParser.optionalConstructorArg(), SOFT_TREE_DEPTH_LIMIT); @@ -131,14 +133,28 @@ public static Builder builder(String dependentVariable) { private final Integer maxOptimizationRoundsPerHyperparameter; private final Boolean earlyStoppingEnabled; - private Regression(String dependentVariable, @Nullable Double lambda, @Nullable Double gamma, @Nullable Double eta, - @Nullable Integer maxTrees, @Nullable Double featureBagFraction, - @Nullable Integer numTopFeatureImportanceValues, @Nullable String predictionFieldName, - @Nullable Double trainingPercent, @Nullable Long randomizeSeed, @Nullable LossFunction lossFunction, - @Nullable Double lossFunctionParameter, @Nullable List featureProcessors, @Nullable Double alpha, - @Nullable Double etaGrowthRatePerTree, @Nullable Double softTreeDepthLimit, @Nullable Double softTreeDepthTolerance, - @Nullable Double downsampleFactor, @Nullable Integer maxOptimizationRoundsPerHyperparameter, - @Nullable Boolean earlyStoppingEnabled) { + private Regression( + String dependentVariable, + @Nullable Double lambda, + @Nullable Double gamma, + @Nullable Double eta, + @Nullable Integer maxTrees, + @Nullable Double featureBagFraction, + @Nullable Integer numTopFeatureImportanceValues, + @Nullable String predictionFieldName, + @Nullable Double trainingPercent, + @Nullable Long randomizeSeed, + @Nullable LossFunction lossFunction, + @Nullable Double lossFunctionParameter, + @Nullable List featureProcessors, + @Nullable Double alpha, + @Nullable Double etaGrowthRatePerTree, + @Nullable Double softTreeDepthLimit, + @Nullable Double softTreeDepthTolerance, + @Nullable Double downsampleFactor, + @Nullable Integer maxOptimizationRoundsPerHyperparameter, + @Nullable Boolean earlyStoppingEnabled + ) { this.dependentVariable = Objects.requireNonNull(dependentVariable); this.lambda = lambda; this.gamma = gamma; @@ -313,10 +329,28 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws @Override public int hashCode() { - return Objects.hash(dependentVariable, lambda, gamma, eta, maxTrees, featureBagFraction, numTopFeatureImportanceValues, - predictionFieldName, trainingPercent, randomizeSeed, lossFunction, lossFunctionParameter, featureProcessors, alpha, - etaGrowthRatePerTree, softTreeDepthLimit, softTreeDepthTolerance, downsampleFactor, maxOptimizationRoundsPerHyperparameter, - earlyStoppingEnabled); + return Objects.hash( + dependentVariable, + lambda, + gamma, + eta, + maxTrees, + featureBagFraction, + numTopFeatureImportanceValues, + predictionFieldName, + trainingPercent, + randomizeSeed, + lossFunction, + lossFunctionParameter, + featureProcessors, + alpha, + etaGrowthRatePerTree, + softTreeDepthLimit, + softTreeDepthTolerance, + downsampleFactor, + maxOptimizationRoundsPerHyperparameter, + earlyStoppingEnabled + ); } @Override @@ -473,15 +507,35 @@ public Builder setEarlyStoppingEnabled(Boolean earlyStoppingEnabled) { } public Regression build() { - return new Regression(dependentVariable, lambda, gamma, eta, maxTrees, featureBagFraction, - numTopFeatureImportanceValues, predictionFieldName, trainingPercent, randomizeSeed, lossFunction, lossFunctionParameter, - featureProcessors, alpha, etaGrowthRatePerTree, softTreeDepthLimit, softTreeDepthTolerance, downsampleFactor, - maxOptimizationRoundsPerHyperparameter, earlyStoppingEnabled); + return new Regression( + dependentVariable, + lambda, + gamma, + eta, + maxTrees, + featureBagFraction, + numTopFeatureImportanceValues, + predictionFieldName, + trainingPercent, + randomizeSeed, + lossFunction, + lossFunctionParameter, + featureProcessors, + alpha, + etaGrowthRatePerTree, + softTreeDepthLimit, + softTreeDepthTolerance, + downsampleFactor, + maxOptimizationRoundsPerHyperparameter, + earlyStoppingEnabled + ); } } public enum LossFunction { - MSE, MSLE, HUBER; + MSE, + MSLE, + HUBER; private static LossFunction fromString(String value) { return LossFunction.valueOf(value.toUpperCase(Locale.ROOT)); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/MlEvaluationNamedXContentProvider.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/MlEvaluationNamedXContentProvider.java index 1fb2e7920a7e5..da1d66785f386 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/MlEvaluationNamedXContentProvider.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/MlEvaluationNamedXContentProvider.java @@ -21,9 +21,9 @@ import org.elasticsearch.client.ml.dataframe.evaluation.regression.MeanSquaredLogarithmicErrorMetric; import org.elasticsearch.client.ml.dataframe.evaluation.regression.RSquaredMetric; import org.elasticsearch.client.ml.dataframe.evaluation.regression.Regression; -import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.plugins.spi.NamedXContentProvider; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import java.util.Arrays; import java.util.List; @@ -39,15 +39,14 @@ public class MlEvaluationNamedXContentProvider implements NamedXContentProvider * @return name appropriate for registering a metric (or metric result) in {@link NamedXContentRegistry} */ public static String registeredMetricName(String evaluationName, String metricName) { - return evaluationName + "." + metricName; + return evaluationName + "." + metricName; } @Override public List getNamedXContentParsers() { return Arrays.asList( // Evaluations - new NamedXContentRegistry.Entry( - Evaluation.class, new ParseField(OutlierDetection.NAME), OutlierDetection::fromXContent), + new NamedXContentRegistry.Entry(Evaluation.class, new ParseField(OutlierDetection.NAME), OutlierDetection::fromXContent), new NamedXContentRegistry.Entry(Evaluation.class, new ParseField(Classification.NAME), Classification::fromXContent), new NamedXContentRegistry.Entry(Evaluation.class, new ParseField(Regression.NAME), Regression::fromXContent), // Evaluation metrics @@ -55,118 +54,163 @@ Evaluation.class, new ParseField(OutlierDetection.NAME), OutlierDetection::fromX EvaluationMetric.class, new ParseField( registeredMetricName( - OutlierDetection.NAME, org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.AucRocMetric.NAME)), - org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.AucRocMetric::fromXContent), + OutlierDetection.NAME, + org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.AucRocMetric.NAME + ) + ), + org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.AucRocMetric::fromXContent + ), new NamedXContentRegistry.Entry( EvaluationMetric.class, new ParseField( registeredMetricName( - OutlierDetection.NAME, org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.PrecisionMetric.NAME)), - org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.PrecisionMetric::fromXContent), + OutlierDetection.NAME, + org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.PrecisionMetric.NAME + ) + ), + org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.PrecisionMetric::fromXContent + ), new NamedXContentRegistry.Entry( EvaluationMetric.class, new ParseField( registeredMetricName( - OutlierDetection.NAME, org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.RecallMetric.NAME)), - org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.RecallMetric::fromXContent), + OutlierDetection.NAME, + org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.RecallMetric.NAME + ) + ), + org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.RecallMetric::fromXContent + ), new NamedXContentRegistry.Entry( EvaluationMetric.class, new ParseField(registeredMetricName(OutlierDetection.NAME, ConfusionMatrixMetric.NAME)), - ConfusionMatrixMetric::fromXContent), + ConfusionMatrixMetric::fromXContent + ), new NamedXContentRegistry.Entry( EvaluationMetric.class, new ParseField(registeredMetricName(Classification.NAME, AucRocMetric.NAME)), - AucRocMetric::fromXContent), + AucRocMetric::fromXContent + ), new NamedXContentRegistry.Entry( EvaluationMetric.class, new ParseField(registeredMetricName(Classification.NAME, AccuracyMetric.NAME)), - AccuracyMetric::fromXContent), + AccuracyMetric::fromXContent + ), new NamedXContentRegistry.Entry( EvaluationMetric.class, new ParseField(registeredMetricName(Classification.NAME, PrecisionMetric.NAME)), - PrecisionMetric::fromXContent), + PrecisionMetric::fromXContent + ), new NamedXContentRegistry.Entry( EvaluationMetric.class, new ParseField(registeredMetricName(Classification.NAME, RecallMetric.NAME)), - RecallMetric::fromXContent), + RecallMetric::fromXContent + ), new NamedXContentRegistry.Entry( EvaluationMetric.class, new ParseField(registeredMetricName(Classification.NAME, MulticlassConfusionMatrixMetric.NAME)), - MulticlassConfusionMatrixMetric::fromXContent), + MulticlassConfusionMatrixMetric::fromXContent + ), new NamedXContentRegistry.Entry( EvaluationMetric.class, new ParseField(registeredMetricName(Regression.NAME, MeanSquaredErrorMetric.NAME)), - MeanSquaredErrorMetric::fromXContent), + MeanSquaredErrorMetric::fromXContent + ), new NamedXContentRegistry.Entry( EvaluationMetric.class, new ParseField(registeredMetricName(Regression.NAME, MeanSquaredLogarithmicErrorMetric.NAME)), - MeanSquaredLogarithmicErrorMetric::fromXContent), + MeanSquaredLogarithmicErrorMetric::fromXContent + ), new NamedXContentRegistry.Entry( EvaluationMetric.class, new ParseField(registeredMetricName(Regression.NAME, HuberMetric.NAME)), - HuberMetric::fromXContent), + HuberMetric::fromXContent + ), new NamedXContentRegistry.Entry( EvaluationMetric.class, new ParseField(registeredMetricName(Regression.NAME, RSquaredMetric.NAME)), - RSquaredMetric::fromXContent), + RSquaredMetric::fromXContent + ), // Evaluation metrics results new NamedXContentRegistry.Entry( EvaluationMetric.Result.class, - new ParseField(registeredMetricName( - OutlierDetection.NAME, org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.AucRocMetric.NAME)), - AucRocResult::fromXContent), + new ParseField( + registeredMetricName( + OutlierDetection.NAME, + org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.AucRocMetric.NAME + ) + ), + AucRocResult::fromXContent + ), new NamedXContentRegistry.Entry( EvaluationMetric.Result.class, new ParseField( registeredMetricName( - OutlierDetection.NAME, org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.PrecisionMetric.NAME)), - org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.PrecisionMetric.Result::fromXContent), + OutlierDetection.NAME, + org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.PrecisionMetric.NAME + ) + ), + org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.PrecisionMetric.Result::fromXContent + ), new NamedXContentRegistry.Entry( EvaluationMetric.Result.class, new ParseField( registeredMetricName( - OutlierDetection.NAME, org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.RecallMetric.NAME)), - org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.RecallMetric.Result::fromXContent), + OutlierDetection.NAME, + org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.RecallMetric.NAME + ) + ), + org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.RecallMetric.Result::fromXContent + ), new NamedXContentRegistry.Entry( EvaluationMetric.Result.class, new ParseField(registeredMetricName(OutlierDetection.NAME, ConfusionMatrixMetric.NAME)), - ConfusionMatrixMetric.Result::fromXContent), + ConfusionMatrixMetric.Result::fromXContent + ), new NamedXContentRegistry.Entry( EvaluationMetric.Result.class, new ParseField(registeredMetricName(Classification.NAME, AucRocMetric.NAME)), - AucRocResult::fromXContent), + AucRocResult::fromXContent + ), new NamedXContentRegistry.Entry( EvaluationMetric.Result.class, new ParseField(registeredMetricName(Classification.NAME, AccuracyMetric.NAME)), - AccuracyMetric.Result::fromXContent), + AccuracyMetric.Result::fromXContent + ), new NamedXContentRegistry.Entry( EvaluationMetric.Result.class, new ParseField(registeredMetricName(Classification.NAME, PrecisionMetric.NAME)), - PrecisionMetric.Result::fromXContent), + PrecisionMetric.Result::fromXContent + ), new NamedXContentRegistry.Entry( EvaluationMetric.Result.class, new ParseField(registeredMetricName(Classification.NAME, RecallMetric.NAME)), - RecallMetric.Result::fromXContent), + RecallMetric.Result::fromXContent + ), new NamedXContentRegistry.Entry( EvaluationMetric.Result.class, new ParseField(registeredMetricName(Classification.NAME, MulticlassConfusionMatrixMetric.NAME)), - MulticlassConfusionMatrixMetric.Result::fromXContent), + MulticlassConfusionMatrixMetric.Result::fromXContent + ), new NamedXContentRegistry.Entry( EvaluationMetric.Result.class, new ParseField(registeredMetricName(Regression.NAME, MeanSquaredErrorMetric.NAME)), - MeanSquaredErrorMetric.Result::fromXContent), + MeanSquaredErrorMetric.Result::fromXContent + ), new NamedXContentRegistry.Entry( EvaluationMetric.Result.class, new ParseField(registeredMetricName(Regression.NAME, MeanSquaredLogarithmicErrorMetric.NAME)), - MeanSquaredLogarithmicErrorMetric.Result::fromXContent), + MeanSquaredLogarithmicErrorMetric.Result::fromXContent + ), new NamedXContentRegistry.Entry( EvaluationMetric.Result.class, new ParseField(registeredMetricName(Regression.NAME, HuberMetric.NAME)), - HuberMetric.Result::fromXContent), + HuberMetric.Result::fromXContent + ), new NamedXContentRegistry.Entry( EvaluationMetric.Result.class, new ParseField(registeredMetricName(Regression.NAME, RSquaredMetric.NAME)), - RSquaredMetric.Result::fromXContent) + RSquaredMetric.Result::fromXContent + ) ); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/AccuracyMetric.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/AccuracyMetric.java index d2c1babec4032..f95b8a0b77344 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/AccuracyMetric.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/AccuracyMetric.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.ml.dataframe.evaluation.classification; import org.elasticsearch.client.ml.dataframe.evaluation.EvaluationMetric; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -85,8 +85,11 @@ public static class Result implements EvaluationMetric.Result { private static final ParseField OVERALL_ACCURACY = new ParseField("overall_accuracy"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("accuracy_result", true, a -> new Result((List) a[0], (double) a[1])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "accuracy_result", + true, + a -> new Result((List) a[0], (double) a[1]) + ); static { PARSER.declareObjectArray(constructorArg(), PerClassSingleValue.PARSER, CLASSES); @@ -134,8 +137,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Result that = (Result) o; - return Objects.equals(this.classes, that.classes) - && this.overallAccuracy == that.overallAccuracy; + return Objects.equals(this.classes, that.classes) && this.overallAccuracy == that.overallAccuracy; } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/AucRocMetric.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/AucRocMetric.java index b850b866415e2..f8a85d7d665b7 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/AucRocMetric.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/AucRocMetric.java @@ -9,8 +9,8 @@ import org.elasticsearch.client.ml.dataframe.evaluation.EvaluationMetric; import org.elasticsearch.client.ml.dataframe.evaluation.common.AucRocResult; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -33,8 +33,11 @@ public class AucRocMetric implements EvaluationMetric { public static final ParseField CLASS_NAME = new ParseField("class_name"); public static final ParseField INCLUDE_CURVE = new ParseField("include_curve"); - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(NAME, true, args -> new AucRocMetric((String) args[0], (Boolean) args[1])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + true, + args -> new AucRocMetric((String) args[0], (Boolean) args[1]) + ); static { PARSER.declareString(constructorArg(), CLASS_NAME); @@ -82,8 +85,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; AucRocMetric that = (AucRocMetric) o; - return Objects.equals(className, that.className) - && Objects.equals(includeCurve, that.includeCurve); + return Objects.equals(className, that.className) && Objects.equals(includeCurve, that.includeCurve); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/Classification.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/Classification.java index 89ab657493fdd..7f394ff30a046 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/Classification.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/Classification.java @@ -10,8 +10,8 @@ import org.elasticsearch.client.ml.dataframe.evaluation.Evaluation; import org.elasticsearch.client.ml.dataframe.evaluation.EvaluationMetric; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -42,14 +42,18 @@ public class Classification implements Evaluation { public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( NAME, true, - a -> new Classification((String) a[0], (String) a[1], (String) a[2], (List) a[3])); + a -> new Classification((String) a[0], (String) a[1], (String) a[2], (List) a[3]) + ); static { PARSER.declareString(constructorArg(), ACTUAL_FIELD); PARSER.declareString(optionalConstructorArg(), PREDICTED_FIELD); PARSER.declareString(optionalConstructorArg(), TOP_CLASSES_FIELD); PARSER.declareNamedObjects( - optionalConstructorArg(), (p, c, n) -> p.namedObject(EvaluationMetric.class, registeredMetricName(NAME, n), c), METRICS); + optionalConstructorArg(), + (p, c, n) -> p.namedObject(EvaluationMetric.class, registeredMetricName(NAME, n), c), + METRICS + ); } public static Classification fromXContent(XContentParser parser) { @@ -76,23 +80,20 @@ public static Classification fromXContent(XContentParser parser) { */ private final List metrics; - public Classification(String actualField, - String predictedField, - String topClassesField) { - this(actualField, predictedField, topClassesField, (List)null); + public Classification(String actualField, String predictedField, String topClassesField) { + this(actualField, predictedField, topClassesField, (List) null); } - public Classification(String actualField, - String predictedField, - String topClassesField, - EvaluationMetric... metrics) { + public Classification(String actualField, String predictedField, String topClassesField, EvaluationMetric... metrics) { this(actualField, predictedField, topClassesField, Arrays.asList(metrics)); } - public Classification(String actualField, - @Nullable String predictedField, - @Nullable String topClassesField, - @Nullable List metrics) { + public Classification( + String actualField, + @Nullable String predictedField, + @Nullable String topClassesField, + @Nullable List metrics + ) { this.actualField = Objects.requireNonNull(actualField); this.predictedField = predictedField; this.topClassesField = topClassesField; @@ -118,11 +119,11 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.field(TOP_CLASSES_FIELD.getPreferredName(), topClassesField); } if (metrics != null) { - builder.startObject(METRICS.getPreferredName()); - for (EvaluationMetric metric : metrics) { - builder.field(metric.getName(), metric); - } - builder.endObject(); + builder.startObject(METRICS.getPreferredName()); + for (EvaluationMetric metric : metrics) { + builder.field(metric.getName(), metric); + } + builder.endObject(); } builder.endObject(); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/MulticlassConfusionMatrixMetric.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/MulticlassConfusionMatrixMetric.java index 3059ea463142c..ae55246c11dc5 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/MulticlassConfusionMatrixMetric.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/MulticlassConfusionMatrixMetric.java @@ -8,10 +8,10 @@ package org.elasticsearch.client.ml.dataframe.evaluation.classification; import org.elasticsearch.client.ml.dataframe.evaluation.EvaluationMetric; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -35,8 +35,11 @@ public class MulticlassConfusionMatrixMetric implements EvaluationMetric { private static final ConstructingObjectParser PARSER = createParser(); private static ConstructingObjectParser createParser() { - ConstructingObjectParser parser = - new ConstructingObjectParser<>(NAME, true, args -> new MulticlassConfusionMatrixMetric((Integer) args[0])); + ConstructingObjectParser parser = new ConstructingObjectParser<>( + NAME, + true, + args -> new MulticlassConfusionMatrixMetric((Integer) args[0]) + ); parser.declareInt(optionalConstructorArg(), SIZE); return parser; } @@ -89,9 +92,11 @@ public static class Result implements EvaluationMetric.Result { private static final ParseField OTHER_ACTUAL_CLASS_COUNT = new ParseField("other_actual_class_count"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>( - "multiclass_confusion_matrix_result", true, a -> new Result((List) a[0], (Long) a[1])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "multiclass_confusion_matrix_result", + true, + a -> new Result((List) a[0], (Long) a[1]) + ); static { PARSER.declareObjectArray(optionalConstructorArg(), ActualClass.PARSER, CONFUSION_MATRIX); @@ -159,11 +164,11 @@ public static class ActualClass implements ToXContentObject { private static final ParseField OTHER_PREDICTED_CLASS_DOC_COUNT = new ParseField("other_predicted_class_doc_count"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>( - "multiclass_confusion_matrix_actual_class", - true, - a -> new ActualClass((String) a[0], (Long) a[1], (List) a[2], (Long) a[3])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "multiclass_confusion_matrix_actual_class", + true, + a -> new ActualClass((String) a[0], (Long) a[1], (List) a[2], (Long) a[3]) + ); static { PARSER.declareString(optionalConstructorArg(), ACTUAL_CLASS); @@ -177,10 +182,12 @@ public static class ActualClass implements ToXContentObject { private final List predictedClasses; private final Long otherPredictedClassDocCount; - public ActualClass(@Nullable String actualClass, - @Nullable Long actualClassDocCount, - @Nullable List predictedClasses, - @Nullable Long otherPredictedClassDocCount) { + public ActualClass( + @Nullable String actualClass, + @Nullable Long actualClassDocCount, + @Nullable List predictedClasses, + @Nullable Long otherPredictedClassDocCount + ) { this.actualClass = actualClass; this.actualClassDocCount = actualClassDocCount; this.predictedClasses = predictedClasses != null ? Collections.unmodifiableList(predictedClasses) : null; @@ -234,9 +241,11 @@ public static class PredictedClass implements ToXContentObject { private static final ParseField COUNT = new ParseField("count"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>( - "multiclass_confusion_matrix_predicted_class", true, a -> new PredictedClass((String) a[0], (Long) a[1])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "multiclass_confusion_matrix_predicted_class", + true, + a -> new PredictedClass((String) a[0], (Long) a[1]) + ); static { PARSER.declareString(optionalConstructorArg(), PREDICTED_CLASS); @@ -269,8 +278,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; PredictedClass that = (PredictedClass) o; - return Objects.equals(this.predictedClass, that.predictedClass) - && Objects.equals(this.count, that.count); + return Objects.equals(this.predictedClass, that.predictedClass) && Objects.equals(this.count, that.count); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/PerClassSingleValue.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/PerClassSingleValue.java index 824ef88f92c04..703468b5ec282 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/PerClassSingleValue.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/PerClassSingleValue.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml.dataframe.evaluation.classification; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -21,8 +21,11 @@ public class PerClassSingleValue implements ToXContentObject { private static final ParseField CLASS_NAME = new ParseField("class_name"); private static final ParseField VALUE = new ParseField("value"); - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("per_class_result", true, a -> new PerClassSingleValue((String) a[0], (double) a[1])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "per_class_result", + true, + a -> new PerClassSingleValue((String) a[0], (double) a[1]) + ); static { PARSER.declareString(constructorArg(), CLASS_NAME); @@ -59,8 +62,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; PerClassSingleValue that = (PerClassSingleValue) o; - return Objects.equals(this.className, that.className) - && this.value == that.value; + return Objects.equals(this.className, that.className) && this.value == that.value; } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/PrecisionMetric.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/PrecisionMetric.java index 9720a15c3863b..168eeed66d67d 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/PrecisionMetric.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/PrecisionMetric.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.ml.dataframe.evaluation.classification; import org.elasticsearch.client.ml.dataframe.evaluation.EvaluationMetric; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -72,8 +72,11 @@ public static class Result implements EvaluationMetric.Result { private static final ParseField AVG_PRECISION = new ParseField("avg_precision"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("precision_result", true, a -> new Result((List) a[0], (double) a[1])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "precision_result", + true, + a -> new Result((List) a[0], (double) a[1]) + ); static { PARSER.declareObjectArray(constructorArg(), PerClassSingleValue.PARSER, CLASSES); @@ -121,8 +124,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Result that = (Result) o; - return Objects.equals(this.classes, that.classes) - && this.avgPrecision == that.avgPrecision; + return Objects.equals(this.classes, that.classes) && this.avgPrecision == that.avgPrecision; } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/RecallMetric.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/RecallMetric.java index 81ca09ca245e9..689d441944e7a 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/RecallMetric.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/RecallMetric.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.ml.dataframe.evaluation.classification; import org.elasticsearch.client.ml.dataframe.evaluation.EvaluationMetric; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -72,8 +72,11 @@ public static class Result implements EvaluationMetric.Result { private static final ParseField AVG_RECALL = new ParseField("avg_recall"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("recall_result", true, a -> new Result((List) a[0], (double) a[1])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "recall_result", + true, + a -> new Result((List) a[0], (double) a[1]) + ); static { PARSER.declareObjectArray(constructorArg(), PerClassSingleValue.PARSER, CLASSES); @@ -121,8 +124,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Result that = (Result) o; - return Objects.equals(this.classes, that.classes) - && this.avgRecall == that.avgRecall; + return Objects.equals(this.classes, that.classes) && this.avgRecall == that.avgRecall; } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/common/AucRocPoint.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/common/AucRocPoint.java index b63eb40be7329..08e5122181269 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/common/AucRocPoint.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/common/AucRocPoint.java @@ -7,9 +7,9 @@ */ package org.elasticsearch.client.ml.dataframe.evaluation.common; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -29,11 +29,11 @@ public static AucRocPoint fromXContent(XContentParser parser) { private static final ParseField FPR = new ParseField("fpr"); private static final ParseField THRESHOLD = new ParseField("threshold"); - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>( - "auc_roc_point", - true, - args -> new AucRocPoint((double) args[0], (double) args[1], (double) args[2])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "auc_roc_point", + true, + args -> new AucRocPoint((double) args[0], (double) args[1], (double) args[2]) + ); static { PARSER.declareDouble(constructorArg(), TPR); @@ -65,8 +65,7 @@ public double getThreshold() { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder - .startObject() + return builder.startObject() .field(TPR.getPreferredName(), tpr) .field(FPR.getPreferredName(), fpr) .field(THRESHOLD.getPreferredName(), threshold) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/common/AucRocResult.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/common/AucRocResult.java index d3f06fd98c38f..d661115b67291 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/common/AucRocResult.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/common/AucRocResult.java @@ -8,10 +8,10 @@ package org.elasticsearch.client.ml.dataframe.evaluation.common; import org.elasticsearch.client.ml.dataframe.evaluation.EvaluationMetric; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -36,9 +36,11 @@ public static AucRocResult fromXContent(XContentParser parser) { private static final ParseField CURVE = new ParseField("curve"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>( - NAME, true, args -> new AucRocResult((double) args[0], (List) args[1])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + true, + args -> new AucRocResult((double) args[0], (List) args[1]) + ); static { PARSER.declareDouble(constructorArg(), VALUE); @@ -82,8 +84,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; AucRocResult that = (AucRocResult) o; - return value == that.value - && Objects.equals(curve, that.curve); + return value == that.value && Objects.equals(curve, that.curve); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/outlierdetection/AbstractConfusionMatrixMetric.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/outlierdetection/AbstractConfusionMatrixMetric.java index e151f7bc6badb..e39af0d143c4b 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/outlierdetection/AbstractConfusionMatrixMetric.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/outlierdetection/AbstractConfusionMatrixMetric.java @@ -28,9 +28,6 @@ protected AbstractConfusionMatrixMetric(List at) { @Override public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { - return builder - .startObject() - .field(AT.getPreferredName(), thresholds) - .endObject(); + return builder.startObject().field(AT.getPreferredName(), thresholds).endObject(); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/outlierdetection/AucRocMetric.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/outlierdetection/AucRocMetric.java index 71f4f8808774c..7c8ea07ab6c77 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/outlierdetection/AucRocMetric.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/outlierdetection/AucRocMetric.java @@ -9,8 +9,8 @@ import org.elasticsearch.client.ml.dataframe.evaluation.EvaluationMetric; import org.elasticsearch.client.ml.dataframe.evaluation.common.AucRocResult; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -31,8 +31,11 @@ public class AucRocMetric implements EvaluationMetric { public static final ParseField INCLUDE_CURVE = new ParseField("include_curve"); @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(NAME, true, args -> new AucRocMetric((Boolean) args[0])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + true, + args -> new AucRocMetric((Boolean) args[0]) + ); static { PARSER.declareBoolean(optionalConstructorArg(), INCLUDE_CURVE); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/outlierdetection/ConfusionMatrixMetric.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/outlierdetection/ConfusionMatrixMetric.java index 26d749602e5f6..0d4617baeb56f 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/outlierdetection/ConfusionMatrixMetric.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/outlierdetection/ConfusionMatrixMetric.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection; import org.elasticsearch.client.ml.dataframe.evaluation.EvaluationMetric; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -30,8 +30,10 @@ public class ConfusionMatrixMetric extends AbstractConfusionMatrixMetric { public static final String NAME = "confusion_matrix"; @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(NAME, args -> new ConfusionMatrixMetric((List) args[0])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + args -> new ConfusionMatrixMetric((List) args[0]) + ); static { PARSER.declareDoubleArray(constructorArg(), AT); @@ -124,9 +126,11 @@ public static ConfusionMatrix fromXContent(XContentParser parser) { private static final ParseField FN = new ParseField("fn"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>( - "confusion_matrix", true, args -> new ConfusionMatrix((long) args[0], (long) args[1], (long) args[2], (long) args[3])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "confusion_matrix", + true, + args -> new ConfusionMatrix((long) args[0], (long) args[1], (long) args[2], (long) args[3]) + ); static { PARSER.declareLong(constructorArg(), TP); @@ -165,8 +169,7 @@ public long getFalseNegatives() { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder - .startObject() + return builder.startObject() .field(TP.getPreferredName(), tp) .field(FP.getPreferredName(), fp) .field(TN.getPreferredName(), tn) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/outlierdetection/OutlierDetection.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/outlierdetection/OutlierDetection.java index bb62dee96a791..7372e85d0bf05 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/outlierdetection/OutlierDetection.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/outlierdetection/OutlierDetection.java @@ -10,8 +10,8 @@ import org.elasticsearch.client.ml.dataframe.evaluation.Evaluation; import org.elasticsearch.client.ml.dataframe.evaluation.EvaluationMetric; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -38,17 +38,20 @@ public class OutlierDetection implements Evaluation { private static final ParseField METRICS = new ParseField("metrics"); @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>( - NAME, - true, - args -> new OutlierDetection((String) args[0], (String) args[1], (List) args[2])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + true, + args -> new OutlierDetection((String) args[0], (String) args[1], (List) args[2]) + ); static { PARSER.declareString(constructorArg(), ACTUAL_FIELD); PARSER.declareString(constructorArg(), PREDICTED_PROBABILITY_FIELD); PARSER.declareNamedObjects( - optionalConstructorArg(), (p, c, n) -> p.namedObject(EvaluationMetric.class, registeredMetricName(NAME, n), null), METRICS); + optionalConstructorArg(), + (p, c, n) -> p.namedObject(EvaluationMetric.class, registeredMetricName(NAME, n), null), + METRICS + ); } public static OutlierDetection fromXContent(XContentParser parser) { @@ -72,15 +75,14 @@ public static OutlierDetection fromXContent(XContentParser parser) { private final List metrics; public OutlierDetection(String actualField, String predictedField) { - this(actualField, predictedField, (List)null); + this(actualField, predictedField, (List) null); } public OutlierDetection(String actualField, String predictedProbabilityField, EvaluationMetric... metric) { this(actualField, predictedProbabilityField, Arrays.asList(metric)); } - public OutlierDetection(String actualField, String predictedProbabilityField, - @Nullable List metrics) { + public OutlierDetection(String actualField, String predictedProbabilityField, @Nullable List metrics) { this.actualField = Objects.requireNonNull(actualField); this.predictedProbabilityField = Objects.requireNonNull(predictedProbabilityField); if (metrics != null) { diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/outlierdetection/PrecisionMetric.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/outlierdetection/PrecisionMetric.java index 601ea0f7f44a0..4f992615d79af 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/outlierdetection/PrecisionMetric.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/outlierdetection/PrecisionMetric.java @@ -28,8 +28,10 @@ public class PrecisionMetric extends AbstractConfusionMatrixMetric { public static final String NAME = "precision"; @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(NAME, args -> new PrecisionMetric((List) args[0])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + args -> new PrecisionMetric((List) args[0]) + ); static { PARSER.declareDoubleArray(constructorArg(), AT); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/outlierdetection/RecallMetric.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/outlierdetection/RecallMetric.java index 95080bd54cb6e..531c62f825722 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/outlierdetection/RecallMetric.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/outlierdetection/RecallMetric.java @@ -28,8 +28,10 @@ public class RecallMetric extends AbstractConfusionMatrixMetric { public static final String NAME = "recall"; @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(NAME, args -> new RecallMetric((List) args[0])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + args -> new RecallMetric((List) args[0]) + ); static { PARSER.declareDoubleArray(constructorArg(), AT); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/HuberMetric.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/HuberMetric.java index 1e23b4d39b540..038c659324da4 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/HuberMetric.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/HuberMetric.java @@ -10,8 +10,8 @@ import org.elasticsearch.client.ml.dataframe.Regression.LossFunction; import org.elasticsearch.client.ml.dataframe.evaluation.EvaluationMetric; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -34,8 +34,11 @@ public class HuberMetric implements EvaluationMetric { public static final ParseField DELTA = new ParseField("delta"); - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(NAME, true, args -> new HuberMetric((Double) args[0])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + true, + args -> new HuberMetric((Double) args[0]) + ); static { PARSER.declareDouble(optionalConstructorArg(), DELTA); @@ -88,8 +91,11 @@ public static Result fromXContent(XContentParser parser) { return PARSER.apply(parser, null); } - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(NAME + "_result", true, args -> new Result((double) args[0])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME + "_result", + true, + args -> new Result((double) args[0]) + ); static { PARSER.declareDouble(constructorArg(), VALUE); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/MeanSquaredErrorMetric.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/MeanSquaredErrorMetric.java index b590473126789..4c593dc75db4e 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/MeanSquaredErrorMetric.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/MeanSquaredErrorMetric.java @@ -9,9 +9,9 @@ import org.elasticsearch.client.ml.dataframe.Regression.LossFunction; import org.elasticsearch.client.ml.dataframe.evaluation.EvaluationMetric; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -71,8 +71,11 @@ public static Result fromXContent(XContentParser parser) { return PARSER.apply(parser, null); } - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(NAME + "_result", true, args -> new Result((double) args[0])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME + "_result", + true, + args -> new Result((double) args[0]) + ); static { PARSER.declareDouble(constructorArg(), VALUE); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/MeanSquaredLogarithmicErrorMetric.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/MeanSquaredLogarithmicErrorMetric.java index 51a3424d1eff3..676ee74cb3f83 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/MeanSquaredLogarithmicErrorMetric.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/MeanSquaredLogarithmicErrorMetric.java @@ -10,8 +10,8 @@ import org.elasticsearch.client.ml.dataframe.Regression.LossFunction; import org.elasticsearch.client.ml.dataframe.evaluation.EvaluationMetric; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -33,8 +33,11 @@ public class MeanSquaredLogarithmicErrorMetric implements EvaluationMetric { public static final ParseField OFFSET = new ParseField("offset"); - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(NAME, true, args -> new MeanSquaredLogarithmicErrorMetric((Double) args[0])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + true, + args -> new MeanSquaredLogarithmicErrorMetric((Double) args[0]) + ); static { PARSER.declareDouble(optionalConstructorArg(), OFFSET); @@ -87,8 +90,11 @@ public static Result fromXContent(XContentParser parser) { return PARSER.apply(parser, null); } - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(NAME + "_result", true, args -> new Result((double) args[0])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME + "_result", + true, + args -> new Result((double) args[0]) + ); static { PARSER.declareDouble(constructorArg(), VALUE); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/RSquaredMetric.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/RSquaredMetric.java index ccf877405c1fc..496a3d55c0e51 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/RSquaredMetric.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/RSquaredMetric.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.ml.dataframe.evaluation.regression; import org.elasticsearch.client.ml.dataframe.evaluation.EvaluationMetric; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -73,8 +73,11 @@ public static Result fromXContent(XContentParser parser) { return PARSER.apply(parser, null); } - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(NAME + "_result", true, args -> new Result((double) args[0])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME + "_result", + true, + args -> new Result((double) args[0]) + ); static { PARSER.declareDouble(constructorArg(), VALUE); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/Regression.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/Regression.java index 79bfbca24d5cc..622013957281e 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/Regression.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/Regression.java @@ -10,8 +10,8 @@ import org.elasticsearch.client.ml.dataframe.evaluation.Evaluation; import org.elasticsearch.client.ml.dataframe.evaluation.EvaluationMetric; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -39,13 +39,19 @@ public class Regression implements Evaluation { @SuppressWarnings("unchecked") public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - NAME, true, a -> new Regression((String) a[0], (String) a[1], (List) a[2])); + NAME, + true, + a -> new Regression((String) a[0], (String) a[1], (List) a[2]) + ); static { PARSER.declareString(constructorArg(), ACTUAL_FIELD); PARSER.declareString(constructorArg(), PREDICTED_FIELD); PARSER.declareNamedObjects( - optionalConstructorArg(), (p, c, n) -> p.namedObject(EvaluationMetric.class, registeredMetricName(NAME, n), c), METRICS); + optionalConstructorArg(), + (p, c, n) -> p.namedObject(EvaluationMetric.class, registeredMetricName(NAME, n), c), + METRICS + ); } public static Regression fromXContent(XContentParser parser) { @@ -70,7 +76,7 @@ public static Regression fromXContent(XContentParser parser) { private final List metrics; public Regression(String actualField, String predictedField) { - this(actualField, predictedField, (List)null); + this(actualField, predictedField, (List) null); } public Regression(String actualField, String predictedField, EvaluationMetric... metrics) { @@ -98,11 +104,11 @@ public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params par builder.field(PREDICTED_FIELD.getPreferredName(), predictedField); if (metrics != null) { - builder.startObject(METRICS.getPreferredName()); - for (EvaluationMetric metric : metrics) { - builder.field(metric.getName(), metric); - } - builder.endObject(); + builder.startObject(METRICS.getPreferredName()); + for (EvaluationMetric metric : metrics) { + builder.field(metric.getName(), metric); + } + builder.endObject(); } builder.endObject(); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/explain/FieldSelection.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/explain/FieldSelection.java index ebc68e5283767..e6a0362e3c0ca 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/explain/FieldSelection.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/explain/FieldSelection.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml.dataframe.explain; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -31,7 +31,8 @@ public class FieldSelection implements ToXContentObject { private static final ParseField REASON = new ParseField("reason"); public enum FeatureType { - CATEGORICAL, NUMERICAL; + CATEGORICAL, + NUMERICAL; public static FeatureType fromString(String value) { return FeatureType.valueOf(value.toUpperCase(Locale.ROOT)); @@ -44,9 +45,18 @@ public String toString() { } @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("field_selection", true, - a -> new FieldSelection((String) a[0], new HashSet<>((List) a[1]), (boolean) a[2], (boolean) a[3], (FeatureType) a[4], - (String) a[5])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "field_selection", + true, + a -> new FieldSelection( + (String) a[0], + new HashSet<>((List) a[1]), + (boolean) a[2], + (boolean) a[3], + (FeatureType) a[4], + (String) a[5] + ) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), NAME); @@ -72,8 +82,14 @@ public static FieldSelection excluded(String name, Set mappingTypes, Str return new FieldSelection(name, mappingTypes, false, false, null, reason); } - FieldSelection(String name, Set mappingTypes, boolean isIncluded, boolean isRequired, @Nullable FeatureType featureType, - @Nullable String reason) { + FieldSelection( + String name, + Set mappingTypes, + boolean isIncluded, + boolean isRequired, + @Nullable FeatureType featureType, + @Nullable String reason + ) { this.name = Objects.requireNonNull(name); this.mappingTypes = Collections.unmodifiableSet(mappingTypes); this.isIncluded = isIncluded; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/explain/MemoryEstimation.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/explain/MemoryEstimation.java index 71bdf4df321ef..54525134853aa 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/explain/MemoryEstimation.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/explain/MemoryEstimation.java @@ -7,11 +7,11 @@ */ package org.elasticsearch.client.ml.dataframe.explain; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -25,20 +25,25 @@ public class MemoryEstimation implements ToXContentObject { public static final ParseField EXPECTED_MEMORY_WITHOUT_DISK = new ParseField("expected_memory_without_disk"); public static final ParseField EXPECTED_MEMORY_WITH_DISK = new ParseField("expected_memory_with_disk"); - public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("memory_estimation", true, - a -> new MemoryEstimation((ByteSizeValue) a[0], (ByteSizeValue) a[1])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "memory_estimation", + true, + a -> new MemoryEstimation((ByteSizeValue) a[0], (ByteSizeValue) a[1]) + ); static { PARSER.declareField( optionalConstructorArg(), (p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), EXPECTED_MEMORY_WITHOUT_DISK.getPreferredName()), EXPECTED_MEMORY_WITHOUT_DISK, - ObjectParser.ValueType.VALUE); + ObjectParser.ValueType.VALUE + ); PARSER.declareField( optionalConstructorArg(), (p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), EXPECTED_MEMORY_WITH_DISK.getPreferredName()), EXPECTED_MEMORY_WITH_DISK, - ObjectParser.ValueType.VALUE); + ObjectParser.ValueType.VALUE + ); } private final ByteSizeValue expectedMemoryWithoutDisk; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/AnalysisStatsNamedXContentProvider.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/AnalysisStatsNamedXContentProvider.java index e8cd65b65a7a7..4da0981fa87d3 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/AnalysisStatsNamedXContentProvider.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/AnalysisStatsNamedXContentProvider.java @@ -10,8 +10,8 @@ import org.elasticsearch.client.ml.dataframe.stats.classification.ClassificationStats; import org.elasticsearch.client.ml.dataframe.stats.outlierdetection.OutlierDetectionStats; import org.elasticsearch.client.ml.dataframe.stats.regression.RegressionStats; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.plugins.spi.NamedXContentProvider; +import org.elasticsearch.xcontent.NamedXContentRegistry; import java.util.Arrays; import java.util.List; @@ -31,11 +31,7 @@ public List getNamedXContentParsers() { OutlierDetectionStats.NAME, (p, c) -> OutlierDetectionStats.PARSER.apply(p, null) ), - new NamedXContentRegistry.Entry( - AnalysisStats.class, - RegressionStats.NAME, - (p, c) -> RegressionStats.PARSER.apply(p, null) - ) + new NamedXContentRegistry.Entry(AnalysisStats.class, RegressionStats.NAME, (p, c) -> RegressionStats.PARSER.apply(p, null)) ); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/classification/ClassificationStats.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/classification/ClassificationStats.java index 27b397a81858c..e8367ae13c95e 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/classification/ClassificationStats.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/classification/ClassificationStats.java @@ -9,9 +9,9 @@ import org.elasticsearch.client.common.TimeUtil; import org.elasticsearch.client.ml.dataframe.stats.AnalysisStats; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; @@ -29,22 +29,19 @@ public class ClassificationStats implements AnalysisStats { public static final ParseField TIMING_STATS = new ParseField("timing_stats"); public static final ParseField VALIDATION_LOSS = new ParseField("validation_loss"); - public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(NAME.getPreferredName(), + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME.getPreferredName(), true, - a -> new ClassificationStats( - (Instant) a[0], - (Integer) a[1], - (Hyperparameters) a[2], - (TimingStats) a[3], - (ValidationLoss) a[4] - ) + a -> new ClassificationStats((Instant) a[0], (Integer) a[1], (Hyperparameters) a[2], (TimingStats) a[3], (ValidationLoss) a[4]) ); static { - PARSER.declareField(ConstructingObjectParser.constructorArg(), + PARSER.declareField( + ConstructingObjectParser.constructorArg(), p -> TimeUtil.parseTimeFieldToInstant(p, TIMESTAMP.getPreferredName()), TIMESTAMP, - ObjectParser.ValueType.VALUE); + ObjectParser.ValueType.VALUE + ); PARSER.declareInt(ConstructingObjectParser.optionalConstructorArg(), ITERATION); PARSER.declareObject(ConstructingObjectParser.constructorArg(), Hyperparameters.PARSER, HYPERPARAMETERS); PARSER.declareObject(ConstructingObjectParser.constructorArg(), TimingStats.PARSER, TIMING_STATS); @@ -57,8 +54,13 @@ public class ClassificationStats implements AnalysisStats { private final TimingStats timingStats; private final ValidationLoss validationLoss; - public ClassificationStats(Instant timestamp, Integer iteration, Hyperparameters hyperparameters, TimingStats timingStats, - ValidationLoss validationLoss) { + public ClassificationStats( + Instant timestamp, + Integer iteration, + Hyperparameters hyperparameters, + TimingStats timingStats, + ValidationLoss validationLoss + ) { this.timestamp = Instant.ofEpochMilli(Objects.requireNonNull(timestamp).toEpochMilli()); this.iteration = iteration; this.hyperparameters = Objects.requireNonNull(hyperparameters); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/classification/Hyperparameters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/classification/Hyperparameters.java index ac30cf4e20017..c136928aeb76f 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/classification/Hyperparameters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/classification/Hyperparameters.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml.dataframe.stats.classification; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -29,14 +29,16 @@ public class Hyperparameters implements ToXContentObject { public static final ParseField LAMBDA = new ParseField("lambda"); public static final ParseField MAX_ATTEMPTS_TO_ADD_TREE = new ParseField("max_attempts_to_add_tree"); public static final ParseField MAX_OPTIMIZATION_ROUNDS_PER_HYPERPARAMETER = new ParseField( - "max_optimization_rounds_per_hyperparameter"); + "max_optimization_rounds_per_hyperparameter" + ); public static final ParseField MAX_TREES = new ParseField("max_trees"); public static final ParseField NUM_FOLDS = new ParseField("num_folds"); public static final ParseField NUM_SPLITS_PER_FEATURE = new ParseField("num_splits_per_feature"); public static final ParseField SOFT_TREE_DEPTH_LIMIT = new ParseField("soft_tree_depth_limit"); public static final ParseField SOFT_TREE_DEPTH_TOLERANCE = new ParseField("soft_tree_depth_tolerance"); - public static ConstructingObjectParser PARSER = new ConstructingObjectParser<>("classification_hyperparameters", + public static ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "classification_hyperparameters", true, a -> new Hyperparameters( (String) a[0], @@ -54,7 +56,8 @@ public class Hyperparameters implements ToXContentObject { (Integer) a[12], (Double) a[13], (Double) a[14] - )); + ) + ); static { PARSER.declareString(optionalConstructorArg(), CLASS_ASSIGNMENT_OBJECTIVE); @@ -90,21 +93,23 @@ public class Hyperparameters implements ToXContentObject { private final Double softTreeDepthLimit; private final Double softTreeDepthTolerance; - public Hyperparameters(String classAssignmentObjective, - Double alpha, - Double downsampleFactor, - Double eta, - Double etaGrowthRatePerTree, - Double featureBagFraction, - Double gamma, - Double lambda, - Integer maxAttemptsToAddTree, - Integer maxOptimizationRoundsPerHyperparameter, - Integer maxTrees, - Integer numFolds, - Integer numSplitsPerFeature, - Double softTreeDepthLimit, - Double softTreeDepthTolerance) { + public Hyperparameters( + String classAssignmentObjective, + Double alpha, + Double downsampleFactor, + Double eta, + Double etaGrowthRatePerTree, + Double featureBagFraction, + Double gamma, + Double lambda, + Integer maxAttemptsToAddTree, + Integer maxOptimizationRoundsPerHyperparameter, + Integer maxTrees, + Integer numFolds, + Integer numSplitsPerFeature, + Double softTreeDepthLimit, + Double softTreeDepthTolerance + ) { this.classAssignmentObjective = classAssignmentObjective; this.alpha = alpha; this.downsampleFactor = downsampleFactor; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/classification/TimingStats.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/classification/TimingStats.java index 7dc2833b2b87f..9afeeeeb3a4f8 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/classification/TimingStats.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/classification/TimingStats.java @@ -7,9 +7,9 @@ */ package org.elasticsearch.client.ml.dataframe.stats.classification; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -21,11 +21,14 @@ public class TimingStats implements ToXContentObject { public static final ParseField ELAPSED_TIME = new ParseField("elapsed_time"); public static final ParseField ITERATION_TIME = new ParseField("iteration_time"); - public static ConstructingObjectParser PARSER = new ConstructingObjectParser<>("classification_timing_stats", true, + public static ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "classification_timing_stats", + true, a -> new TimingStats( a[0] == null ? null : TimeValue.timeValueMillis((long) a[0]), a[1] == null ? null : TimeValue.timeValueMillis((long) a[1]) - )); + ) + ); static { PARSER.declareLong(ConstructingObjectParser.optionalConstructorArg(), ELAPSED_TIME); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/classification/ValidationLoss.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/classification/ValidationLoss.java index 21d42e37a8ca8..ca781c8205300 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/classification/ValidationLoss.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/classification/ValidationLoss.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml.dataframe.stats.classification; import org.elasticsearch.client.ml.dataframe.stats.common.FoldValues; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -23,9 +23,11 @@ public class ValidationLoss implements ToXContentObject { public static final ParseField FOLD_VALUES = new ParseField("fold_values"); @SuppressWarnings("unchecked") - public static ConstructingObjectParser PARSER = new ConstructingObjectParser<>("classification_validation_loss", + public static ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "classification_validation_loss", true, - a -> new ValidationLoss((String) a[0], (List) a[1])); + a -> new ValidationLoss((String) a[0], (List) a[1]) + ); static { PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), LOSS_TYPE); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/common/DataCounts.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/common/DataCounts.java index 638427a9500f3..82c4fccb09c8f 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/common/DataCounts.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/common/DataCounts.java @@ -8,10 +8,10 @@ package org.elasticsearch.client.ml.dataframe.stats.common; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.inject.internal.ToStringBuilder; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -28,17 +28,12 @@ public class DataCounts implements ToXContentObject { public static final ParseField TEST_DOCS_COUNT = new ParseField("test_docs_count"); public static final ParseField SKIPPED_DOCS_COUNT = new ParseField("skipped_docs_count"); - public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(TYPE_VALUE, true, - a -> { - Long trainingDocsCount = (Long) a[0]; - Long testDocsCount = (Long) a[1]; - Long skippedDocsCount = (Long) a[2]; - return new DataCounts( - getOrDefault(trainingDocsCount, 0L), - getOrDefault(testDocsCount, 0L), - getOrDefault(skippedDocsCount, 0L) - ); - }); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(TYPE_VALUE, true, a -> { + Long trainingDocsCount = (Long) a[0]; + Long testDocsCount = (Long) a[1]; + Long skippedDocsCount = (Long) a[2]; + return new DataCounts(getOrDefault(trainingDocsCount, 0L), getOrDefault(testDocsCount, 0L), getOrDefault(skippedDocsCount, 0L)); + }); static { PARSER.declareLong(optionalConstructorArg(), TRAINING_DOCS_COUNT); @@ -83,8 +78,7 @@ public int hashCode() { @Override public String toString() { - return new ToStringBuilder(getClass()) - .add(TRAINING_DOCS_COUNT.getPreferredName(), trainingDocsCount) + return new ToStringBuilder(getClass()).add(TRAINING_DOCS_COUNT.getPreferredName(), trainingDocsCount) .add(TEST_DOCS_COUNT.getPreferredName(), testDocsCount) .add(SKIPPED_DOCS_COUNT.getPreferredName(), skippedDocsCount) .toString(); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/common/FoldValues.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/common/FoldValues.java index 959d94c0306a8..d9f9fbc74fe70 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/common/FoldValues.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/common/FoldValues.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml.dataframe.stats.common; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -23,8 +23,11 @@ public class FoldValues implements ToXContentObject { public static final ParseField VALUES = new ParseField("values"); @SuppressWarnings("unchecked") - public static ConstructingObjectParser PARSER = new ConstructingObjectParser<>("fold_values", true, - a -> new FoldValues((int) a[0], (List) a[1])); + public static ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "fold_values", + true, + a -> new FoldValues((int) a[0], (List) a[1]) + ); static { PARSER.declareInt(ConstructingObjectParser.constructorArg(), FOLD); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/common/MemoryUsage.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/common/MemoryUsage.java index cdc435283af62..a856df9c3130b 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/common/MemoryUsage.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/common/MemoryUsage.java @@ -8,11 +8,11 @@ package org.elasticsearch.client.ml.dataframe.stats.common; import org.elasticsearch.client.common.TimeUtil; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.inject.internal.ToStringBuilder; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -28,14 +28,19 @@ public class MemoryUsage implements ToXContentObject { static final ParseField STATUS = new ParseField("status"); static final ParseField MEMORY_REESTIMATE_BYTES = new ParseField("memory_reestimate_bytes"); - public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("analytics_memory_usage", - true, a -> new MemoryUsage((Instant) a[0], (long) a[1], (Status) a[2], (Long) a[3])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "analytics_memory_usage", + true, + a -> new MemoryUsage((Instant) a[0], (long) a[1], (Status) a[2], (Long) a[3]) + ); static { - PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(), + PARSER.declareField( + ConstructingObjectParser.optionalConstructorArg(), p -> TimeUtil.parseTimeFieldToInstant(p, TIMESTAMP.getPreferredName()), TIMESTAMP, - ObjectParser.ValueType.VALUE); + ObjectParser.ValueType.VALUE + ); PARSER.declareLong(ConstructingObjectParser.constructorArg(), PEAK_USAGE_BYTES); PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), Status::fromString, STATUS); PARSER.declareLong(ConstructingObjectParser.optionalConstructorArg(), MEMORY_REESTIMATE_BYTES); @@ -105,8 +110,7 @@ public int hashCode() { @Override public String toString() { - return new ToStringBuilder(getClass()) - .add(TIMESTAMP.getPreferredName(), timestamp == null ? null : timestamp.getEpochSecond()) + return new ToStringBuilder(getClass()).add(TIMESTAMP.getPreferredName(), timestamp == null ? null : timestamp.getEpochSecond()) .add(PEAK_USAGE_BYTES.getPreferredName(), peakUsageBytes) .add(STATUS.getPreferredName(), status) .add(MEMORY_REESTIMATE_BYTES.getPreferredName(), memoryReestimateBytes) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/outlierdetection/OutlierDetectionStats.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/outlierdetection/OutlierDetectionStats.java index 221d038c28069..8481aecf808a0 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/outlierdetection/OutlierDetectionStats.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/outlierdetection/OutlierDetectionStats.java @@ -9,9 +9,9 @@ import org.elasticsearch.client.common.TimeUtil; import org.elasticsearch.client.ml.dataframe.stats.AnalysisStats; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; @@ -28,14 +28,18 @@ public class OutlierDetectionStats implements AnalysisStats { public static final ParseField TIMING_STATS = new ParseField("timing_stats"); public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - NAME.getPreferredName(), true, - a -> new OutlierDetectionStats((Instant) a[0], (Parameters) a[1], (TimingStats) a[2])); + NAME.getPreferredName(), + true, + a -> new OutlierDetectionStats((Instant) a[0], (Parameters) a[1], (TimingStats) a[2]) + ); static { - PARSER.declareField(ConstructingObjectParser.constructorArg(), + PARSER.declareField( + ConstructingObjectParser.constructorArg(), p -> TimeUtil.parseTimeFieldToInstant(p, TIMESTAMP.getPreferredName()), TIMESTAMP, - ObjectParser.ValueType.VALUE); + ObjectParser.ValueType.VALUE + ); PARSER.declareObject(ConstructingObjectParser.constructorArg(), Parameters.PARSER, PARAMETERS); PARSER.declareObject(ConstructingObjectParser.constructorArg(), TimingStats.PARSER, TIMING_STATS); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/outlierdetection/Parameters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/outlierdetection/Parameters.java index ea2768621030a..aef6ad0833d42 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/outlierdetection/Parameters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/outlierdetection/Parameters.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml.dataframe.stats.outlierdetection; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -27,16 +27,11 @@ public class Parameters implements ToXContentObject { public static final ParseField STANDARDIZATION_ENABLED = new ParseField("standardization_enabled"); @SuppressWarnings("unchecked") - public static ConstructingObjectParser PARSER = new ConstructingObjectParser<>("outlier_detection_parameters", + public static ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "outlier_detection_parameters", true, - a -> new Parameters( - (Integer) a[0], - (String) a[1], - (Boolean) a[2], - (Double) a[3], - (Double) a[4], - (Boolean) a[5] - )); + a -> new Parameters((Integer) a[0], (String) a[1], (Boolean) a[2], (Double) a[3], (Double) a[4], (Boolean) a[5]) + ); static { PARSER.declareInt(optionalConstructorArg(), N_NEIGHBORS); @@ -54,8 +49,14 @@ public class Parameters implements ToXContentObject { private final Double outlierFraction; private final Boolean standardizationEnabled; - public Parameters(Integer nNeighbors, String method, Boolean computeFeatureInfluence, Double featureInfluenceThreshold, - Double outlierFraction, Boolean standardizationEnabled) { + public Parameters( + Integer nNeighbors, + String method, + Boolean computeFeatureInfluence, + Double featureInfluenceThreshold, + Double outlierFraction, + Boolean standardizationEnabled + ) { this.nNeighbors = nNeighbors; this.method = method; this.computeFeatureInfluence = computeFeatureInfluence; @@ -129,7 +130,13 @@ public boolean equals(Object o) { @Override public int hashCode() { - return Objects.hash(nNeighbors, method, computeFeatureInfluence, featureInfluenceThreshold, outlierFraction, - standardizationEnabled); + return Objects.hash( + nNeighbors, + method, + computeFeatureInfluence, + featureInfluenceThreshold, + outlierFraction, + standardizationEnabled + ); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/outlierdetection/TimingStats.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/outlierdetection/TimingStats.java index 11c07b23fbc00..72d96fa4d71cf 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/outlierdetection/TimingStats.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/outlierdetection/TimingStats.java @@ -7,9 +7,9 @@ */ package org.elasticsearch.client.ml.dataframe.stats.outlierdetection; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -20,9 +20,11 @@ public class TimingStats implements ToXContentObject { public static final ParseField ELAPSED_TIME = new ParseField("elapsed_time"); - public static ConstructingObjectParser PARSER = new ConstructingObjectParser<>("outlier_detection_timing_stats", + public static ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "outlier_detection_timing_stats", true, - a -> new TimingStats(a[0] == null ? null : TimeValue.timeValueMillis((long) a[0]))); + a -> new TimingStats(a[0] == null ? null : TimeValue.timeValueMillis((long) a[0])) + ); static { PARSER.declareLong(ConstructingObjectParser.optionalConstructorArg(), ELAPSED_TIME); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/regression/Hyperparameters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/regression/Hyperparameters.java index d28922d6a4442..bd89928f035c8 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/regression/Hyperparameters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/regression/Hyperparameters.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml.dataframe.stats.regression; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -28,14 +28,16 @@ public class Hyperparameters implements ToXContentObject { public static final ParseField LAMBDA = new ParseField("lambda"); public static final ParseField MAX_ATTEMPTS_TO_ADD_TREE = new ParseField("max_attempts_to_add_tree"); public static final ParseField MAX_OPTIMIZATION_ROUNDS_PER_HYPERPARAMETER = new ParseField( - "max_optimization_rounds_per_hyperparameter"); + "max_optimization_rounds_per_hyperparameter" + ); public static final ParseField MAX_TREES = new ParseField("max_trees"); public static final ParseField NUM_FOLDS = new ParseField("num_folds"); public static final ParseField NUM_SPLITS_PER_FEATURE = new ParseField("num_splits_per_feature"); public static final ParseField SOFT_TREE_DEPTH_LIMIT = new ParseField("soft_tree_depth_limit"); public static final ParseField SOFT_TREE_DEPTH_TOLERANCE = new ParseField("soft_tree_depth_tolerance"); - public static ConstructingObjectParser PARSER = new ConstructingObjectParser<>("regression_hyperparameters", + public static ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "regression_hyperparameters", true, a -> new Hyperparameters( (Double) a[0], @@ -52,7 +54,8 @@ public class Hyperparameters implements ToXContentObject { (Integer) a[11], (Double) a[12], (Double) a[13] - )); + ) + ); static { PARSER.declareDouble(optionalConstructorArg(), ALPHA); @@ -86,20 +89,22 @@ public class Hyperparameters implements ToXContentObject { private final Double softTreeDepthLimit; private final Double softTreeDepthTolerance; - public Hyperparameters(Double alpha, - Double downsampleFactor, - Double eta, - Double etaGrowthRatePerTree, - Double featureBagFraction, - Double gamma, - Double lambda, - Integer maxAttemptsToAddTree, - Integer maxOptimizationRoundsPerHyperparameter, - Integer maxTrees, - Integer numFolds, - Integer numSplitsPerFeature, - Double softTreeDepthLimit, - Double softTreeDepthTolerance) { + public Hyperparameters( + Double alpha, + Double downsampleFactor, + Double eta, + Double etaGrowthRatePerTree, + Double featureBagFraction, + Double gamma, + Double lambda, + Integer maxAttemptsToAddTree, + Integer maxOptimizationRoundsPerHyperparameter, + Integer maxTrees, + Integer numFolds, + Integer numSplitsPerFeature, + Double softTreeDepthLimit, + Double softTreeDepthTolerance + ) { this.alpha = alpha; this.downsampleFactor = downsampleFactor; this.eta = eta; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/regression/RegressionStats.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/regression/RegressionStats.java index c8049fa966212..8507a2c88f3a9 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/regression/RegressionStats.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/regression/RegressionStats.java @@ -9,9 +9,9 @@ import org.elasticsearch.client.common.TimeUtil; import org.elasticsearch.client.ml.dataframe.stats.AnalysisStats; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; @@ -29,22 +29,19 @@ public class RegressionStats implements AnalysisStats { public static final ParseField TIMING_STATS = new ParseField("timing_stats"); public static final ParseField VALIDATION_LOSS = new ParseField("validation_loss"); - public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(NAME.getPreferredName(), + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME.getPreferredName(), true, - a -> new RegressionStats( - (Instant) a[0], - (Integer) a[1], - (Hyperparameters) a[2], - (TimingStats) a[3], - (ValidationLoss) a[4] - ) + a -> new RegressionStats((Instant) a[0], (Integer) a[1], (Hyperparameters) a[2], (TimingStats) a[3], (ValidationLoss) a[4]) ); static { - PARSER.declareField(ConstructingObjectParser.constructorArg(), + PARSER.declareField( + ConstructingObjectParser.constructorArg(), p -> TimeUtil.parseTimeFieldToInstant(p, TIMESTAMP.getPreferredName()), TIMESTAMP, - ObjectParser.ValueType.VALUE); + ObjectParser.ValueType.VALUE + ); PARSER.declareInt(ConstructingObjectParser.optionalConstructorArg(), ITERATION); PARSER.declareObject(ConstructingObjectParser.constructorArg(), Hyperparameters.PARSER, HYPERPARAMETERS); PARSER.declareObject(ConstructingObjectParser.constructorArg(), TimingStats.PARSER, TIMING_STATS); @@ -57,8 +54,13 @@ public class RegressionStats implements AnalysisStats { private final TimingStats timingStats; private final ValidationLoss validationLoss; - public RegressionStats(Instant timestamp, Integer iteration, Hyperparameters hyperparameters, TimingStats timingStats, - ValidationLoss validationLoss) { + public RegressionStats( + Instant timestamp, + Integer iteration, + Hyperparameters hyperparameters, + TimingStats timingStats, + ValidationLoss validationLoss + ) { this.timestamp = Instant.ofEpochMilli(Objects.requireNonNull(timestamp).toEpochMilli()); this.iteration = iteration; this.hyperparameters = Objects.requireNonNull(hyperparameters); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/regression/TimingStats.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/regression/TimingStats.java index 61d016afd3b93..7a06a2aa3b4d5 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/regression/TimingStats.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/regression/TimingStats.java @@ -7,9 +7,9 @@ */ package org.elasticsearch.client.ml.dataframe.stats.regression; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -21,11 +21,14 @@ public class TimingStats implements ToXContentObject { public static final ParseField ELAPSED_TIME = new ParseField("elapsed_time"); public static final ParseField ITERATION_TIME = new ParseField("iteration_time"); - public static ConstructingObjectParser PARSER = new ConstructingObjectParser<>("regression_timing_stats", true, + public static ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "regression_timing_stats", + true, a -> new TimingStats( a[0] == null ? null : TimeValue.timeValueMillis((long) a[0]), a[1] == null ? null : TimeValue.timeValueMillis((long) a[1]) - )); + ) + ); static { PARSER.declareLong(ConstructingObjectParser.optionalConstructorArg(), ELAPSED_TIME); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/regression/ValidationLoss.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/regression/ValidationLoss.java index b6d52a23f102f..2fabaad16ffc5 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/regression/ValidationLoss.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/stats/regression/ValidationLoss.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml.dataframe.stats.regression; import org.elasticsearch.client.ml.dataframe.stats.common.FoldValues; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -23,9 +23,11 @@ public class ValidationLoss implements ToXContentObject { public static final ParseField FOLD_VALUES = new ParseField("fold_values"); @SuppressWarnings("unchecked") - public static ConstructingObjectParser PARSER = new ConstructingObjectParser<>("regression_validation_loss", + public static ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "regression_validation_loss", true, - a -> new ValidationLoss((String) a[0], (List) a[1])); + a -> new ValidationLoss((String) a[0], (List) a[1]) + ); static { PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), LOSS_TYPE); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/InferenceToXContentCompressor.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/InferenceToXContentCompressor.java index 0cfcdbe2579ad..af06d177d9bf9 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/InferenceToXContentCompressor.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/InferenceToXContentCompressor.java @@ -8,15 +8,15 @@ package org.elasticsearch.client.ml.inference; -import org.elasticsearch.core.CheckedFunction; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.Streams; import org.elasticsearch.common.io.stream.BytesStreamOutput; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.core.CheckedFunction; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ToXContentObject; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; @@ -42,13 +42,19 @@ public static String deflate(T objectToCompress) th return deflate(reference); } - public static T inflate(String compressedString, - CheckedFunction parserFunction, - NamedXContentRegistry xContentRegistry) throws IOException { - try(XContentParser parser = XContentHelper.createParser(xContentRegistry, - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, - inflate(compressedString, MAX_INFLATED_BYTES), - XContentType.JSON)) { + public static T inflate( + String compressedString, + CheckedFunction parserFunction, + NamedXContentRegistry xContentRegistry + ) throws IOException { + try ( + XContentParser parser = XContentHelper.createParser( + xContentRegistry, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + inflate(compressedString, MAX_INFLATED_BYTES), + XContentType.JSON + ) + ) { return parserFunction.apply(parser); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/MlInferenceNamedXContentProvider.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/MlInferenceNamedXContentProvider.java index 1ad49046d5e70..271b882f697e3 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/MlInferenceNamedXContentProvider.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/MlInferenceNamedXContentProvider.java @@ -8,8 +8,12 @@ package org.elasticsearch.client.ml.inference; import org.elasticsearch.client.ml.inference.preprocessing.CustomWordEmbedding; +import org.elasticsearch.client.ml.inference.preprocessing.FrequencyEncoding; import org.elasticsearch.client.ml.inference.preprocessing.Multi; import org.elasticsearch.client.ml.inference.preprocessing.NGram; +import org.elasticsearch.client.ml.inference.preprocessing.OneHotEncoding; +import org.elasticsearch.client.ml.inference.preprocessing.PreProcessor; +import org.elasticsearch.client.ml.inference.preprocessing.TargetMeanEncoding; import org.elasticsearch.client.ml.inference.trainedmodel.ClassificationConfig; import org.elasticsearch.client.ml.inference.trainedmodel.IndexLocation; import org.elasticsearch.client.ml.inference.trainedmodel.InferenceConfig; @@ -24,13 +28,9 @@ import org.elasticsearch.client.ml.inference.trainedmodel.ensemble.WeightedSum; import org.elasticsearch.client.ml.inference.trainedmodel.langident.LangIdentNeuralNetwork; import org.elasticsearch.client.ml.inference.trainedmodel.tree.Tree; -import org.elasticsearch.client.ml.inference.preprocessing.FrequencyEncoding; -import org.elasticsearch.client.ml.inference.preprocessing.OneHotEncoding; -import org.elasticsearch.client.ml.inference.preprocessing.PreProcessor; -import org.elasticsearch.client.ml.inference.preprocessing.TargetMeanEncoding; -import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.plugins.spi.NamedXContentProvider; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import java.util.ArrayList; import java.util.List; @@ -42,52 +42,58 @@ public List getNamedXContentParsers() { List namedXContent = new ArrayList<>(); // PreProcessing - namedXContent.add(new NamedXContentRegistry.Entry(PreProcessor.class, new ParseField(OneHotEncoding.NAME), - OneHotEncoding::fromXContent)); - namedXContent.add(new NamedXContentRegistry.Entry(PreProcessor.class, new ParseField(TargetMeanEncoding.NAME), - TargetMeanEncoding::fromXContent)); - namedXContent.add(new NamedXContentRegistry.Entry(PreProcessor.class, new ParseField(FrequencyEncoding.NAME), - FrequencyEncoding::fromXContent)); - namedXContent.add(new NamedXContentRegistry.Entry(PreProcessor.class, new ParseField(CustomWordEmbedding.NAME), - CustomWordEmbedding::fromXContent)); - namedXContent.add(new NamedXContentRegistry.Entry(PreProcessor.class, new ParseField(NGram.NAME), - NGram::fromXContent)); - namedXContent.add(new NamedXContentRegistry.Entry(PreProcessor.class, new ParseField(Multi.NAME), - Multi::fromXContent)); + namedXContent.add( + new NamedXContentRegistry.Entry(PreProcessor.class, new ParseField(OneHotEncoding.NAME), OneHotEncoding::fromXContent) + ); + namedXContent.add( + new NamedXContentRegistry.Entry(PreProcessor.class, new ParseField(TargetMeanEncoding.NAME), TargetMeanEncoding::fromXContent) + ); + namedXContent.add( + new NamedXContentRegistry.Entry(PreProcessor.class, new ParseField(FrequencyEncoding.NAME), FrequencyEncoding::fromXContent) + ); + namedXContent.add( + new NamedXContentRegistry.Entry(PreProcessor.class, new ParseField(CustomWordEmbedding.NAME), CustomWordEmbedding::fromXContent) + ); + namedXContent.add(new NamedXContentRegistry.Entry(PreProcessor.class, new ParseField(NGram.NAME), NGram::fromXContent)); + namedXContent.add(new NamedXContentRegistry.Entry(PreProcessor.class, new ParseField(Multi.NAME), Multi::fromXContent)); // Model namedXContent.add(new NamedXContentRegistry.Entry(TrainedModel.class, new ParseField(Tree.NAME), Tree::fromXContent)); namedXContent.add(new NamedXContentRegistry.Entry(TrainedModel.class, new ParseField(Ensemble.NAME), Ensemble::fromXContent)); - namedXContent.add(new NamedXContentRegistry.Entry(TrainedModel.class, - new ParseField(LangIdentNeuralNetwork.NAME), - LangIdentNeuralNetwork::fromXContent)); + namedXContent.add( + new NamedXContentRegistry.Entry( + TrainedModel.class, + new ParseField(LangIdentNeuralNetwork.NAME), + LangIdentNeuralNetwork::fromXContent + ) + ); // Inference Config - namedXContent.add(new NamedXContentRegistry.Entry(InferenceConfig.class, - ClassificationConfig.NAME, - ClassificationConfig::fromXContent)); - namedXContent.add(new NamedXContentRegistry.Entry(InferenceConfig.class, - RegressionConfig.NAME, - RegressionConfig::fromXContent)); + namedXContent.add( + new NamedXContentRegistry.Entry(InferenceConfig.class, ClassificationConfig.NAME, ClassificationConfig::fromXContent) + ); + namedXContent.add(new NamedXContentRegistry.Entry(InferenceConfig.class, RegressionConfig.NAME, RegressionConfig::fromXContent)); // Aggregating output - namedXContent.add(new NamedXContentRegistry.Entry(OutputAggregator.class, - new ParseField(WeightedMode.NAME), - WeightedMode::fromXContent)); - namedXContent.add(new NamedXContentRegistry.Entry(OutputAggregator.class, - new ParseField(WeightedSum.NAME), - WeightedSum::fromXContent)); - namedXContent.add(new NamedXContentRegistry.Entry(OutputAggregator.class, - new ParseField(LogisticRegression.NAME), - LogisticRegression::fromXContent)); - namedXContent.add(new NamedXContentRegistry.Entry(OutputAggregator.class, - new ParseField(Exponent.NAME), - Exponent::fromXContent)); + namedXContent.add( + new NamedXContentRegistry.Entry(OutputAggregator.class, new ParseField(WeightedMode.NAME), WeightedMode::fromXContent) + ); + namedXContent.add( + new NamedXContentRegistry.Entry(OutputAggregator.class, new ParseField(WeightedSum.NAME), WeightedSum::fromXContent) + ); + namedXContent.add( + new NamedXContentRegistry.Entry( + OutputAggregator.class, + new ParseField(LogisticRegression.NAME), + LogisticRegression::fromXContent + ) + ); + namedXContent.add(new NamedXContentRegistry.Entry(OutputAggregator.class, new ParseField(Exponent.NAME), Exponent::fromXContent)); // location - namedXContent.add(new NamedXContentRegistry.Entry(TrainedModelLocation.class, - new ParseField(IndexLocation.INDEX), - IndexLocation::fromXContent)); + namedXContent.add( + new NamedXContentRegistry.Entry(TrainedModelLocation.class, new ParseField(IndexLocation.INDEX), IndexLocation::fromXContent) + ); return namedXContent; } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/NamedXContentObjectHelper.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/NamedXContentObjectHelper.java index 6c8a69a4aacd8..b0c4015e186a0 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/NamedXContentObjectHelper.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/NamedXContentObjectHelper.java @@ -17,11 +17,13 @@ public final class NamedXContentObjectHelper { private NamedXContentObjectHelper() {} - public static XContentBuilder writeNamedObjects(XContentBuilder builder, - ToXContent.Params params, - boolean useExplicitOrder, - String namedObjectsName, - List namedObjects) throws IOException { + public static XContentBuilder writeNamedObjects( + XContentBuilder builder, + ToXContent.Params params, + boolean useExplicitOrder, + String namedObjectsName, + List namedObjects + ) throws IOException { if (useExplicitOrder) { builder.startArray(namedObjectsName); } else { @@ -44,10 +46,12 @@ public static XContentBuilder writeNamedObjects(XContentBuilder builder, return builder; } - public static XContentBuilder writeNamedObject(XContentBuilder builder, - ToXContent.Params params, - String namedObjectName, - NamedXContentObject namedObject) throws IOException { + public static XContentBuilder writeNamedObject( + XContentBuilder builder, + ToXContent.Params params, + String namedObjectName, + NamedXContentObject namedObject + ) throws IOException { builder.startObject(namedObjectName); builder.field(namedObject.getName(), namedObject, params); builder.endObject(); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/SimpleBoundedInputStream.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/SimpleBoundedInputStream.java index 114616dcef7eb..f0c274d49592a 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/SimpleBoundedInputStream.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/SimpleBoundedInputStream.java @@ -7,7 +7,6 @@ */ package org.elasticsearch.client.ml.inference; - import java.io.IOException; import java.io.InputStream; import java.util.Objects; @@ -30,7 +29,6 @@ final class SimpleBoundedInputStream extends InputStream { this.maxBytes = maxBytes; } - /** * A simple wrapper around the injected input stream that restricts the total number of bytes able to be read. * @return The byte read. -1 on internal stream completion or when maxBytes is exceeded. diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/TrainedModelConfig.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/TrainedModelConfig.java index c245c91acf704..1d1e7cdf59013 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/TrainedModelConfig.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/TrainedModelConfig.java @@ -11,11 +11,11 @@ import org.elasticsearch.client.common.TimeUtil; import org.elasticsearch.client.ml.inference.trainedmodel.InferenceConfig; import org.elasticsearch.client.ml.inference.trainedmodel.TrainedModelLocation; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -52,22 +52,20 @@ public class TrainedModelConfig implements ToXContentObject { public static final ParseField INFERENCE_CONFIG = new ParseField("inference_config"); public static final ParseField LOCATION = new ParseField("location"); - public static final ObjectParser PARSER = new ObjectParser<>(NAME, - true, - TrainedModelConfig.Builder::new); + public static final ObjectParser PARSER = new ObjectParser<>(NAME, true, TrainedModelConfig.Builder::new); static { PARSER.declareString(TrainedModelConfig.Builder::setModelId, MODEL_ID); PARSER.declareString(TrainedModelConfig.Builder::setModelType, MODEL_TYPE); PARSER.declareString(TrainedModelConfig.Builder::setCreatedBy, CREATED_BY); PARSER.declareString(TrainedModelConfig.Builder::setVersion, VERSION); PARSER.declareString(TrainedModelConfig.Builder::setDescription, DESCRIPTION); - PARSER.declareField(TrainedModelConfig.Builder::setCreateTime, + PARSER.declareField( + TrainedModelConfig.Builder::setCreateTime, (p, c) -> TimeUtil.parseTimeFieldToInstant(p, CREATE_TIME.getPreferredName()), CREATE_TIME, - ObjectParser.ValueType.VALUE); - PARSER.declareObject(TrainedModelConfig.Builder::setDefinition, - (p, c) -> TrainedModelDefinition.fromXContent(p), - DEFINITION); + ObjectParser.ValueType.VALUE + ); + PARSER.declareObject(TrainedModelConfig.Builder::setDefinition, (p, c) -> TrainedModelDefinition.fromXContent(p), DEFINITION); PARSER.declareString(TrainedModelConfig.Builder::setCompressedDefinition, COMPRESSED_DEFINITION); PARSER.declareStringArray(TrainedModelConfig.Builder::setTags, TAGS); PARSER.declareObject(TrainedModelConfig.Builder::setMetadata, (p, c) -> p.map(), METADATA); @@ -76,12 +74,16 @@ public class TrainedModelConfig implements ToXContentObject { PARSER.declareLong(TrainedModelConfig.Builder::setEstimatedOperations, ESTIMATED_OPERATIONS); PARSER.declareString(TrainedModelConfig.Builder::setLicenseLevel, LICENSE_LEVEL); PARSER.declareObject(TrainedModelConfig.Builder::setDefaultFieldMap, (p, c) -> p.mapStrings(), DEFAULT_FIELD_MAP); - PARSER.declareNamedObject(TrainedModelConfig.Builder::setInferenceConfig, + PARSER.declareNamedObject( + TrainedModelConfig.Builder::setInferenceConfig, (p, c, n) -> p.namedObject(InferenceConfig.class, n, null), - INFERENCE_CONFIG); - PARSER.declareNamedObject(TrainedModelConfig.Builder::setLocation, + INFERENCE_CONFIG + ); + PARSER.declareNamedObject( + TrainedModelConfig.Builder::setLocation, (p, c, n) -> p.namedObject(TrainedModelLocation.class, n, null), - LOCATION); + LOCATION + ); } public static TrainedModelConfig fromXContent(XContentParser parser) throws IOException { @@ -106,23 +108,25 @@ public static TrainedModelConfig fromXContent(XContentParser parser) throws IOEx private final InferenceConfig inferenceConfig; private final TrainedModelLocation location; - TrainedModelConfig(String modelId, - TrainedModelType modelType, - String createdBy, - Version version, - String description, - Instant createTime, - TrainedModelDefinition definition, - String compressedDefinition, - List tags, - Map metadata, - TrainedModelInput input, - Long estimatedHeapMemory, - Long estimatedOperations, - String licenseLevel, - Map defaultFieldMap, - InferenceConfig inferenceConfig, - TrainedModelLocation location) { + TrainedModelConfig( + String modelId, + TrainedModelType modelType, + String createdBy, + Version version, + String description, + Instant createTime, + TrainedModelDefinition definition, + String compressedDefinition, + List tags, + Map metadata, + TrainedModelInput input, + Long estimatedHeapMemory, + Long estimatedOperations, + String licenseLevel, + Map defaultFieldMap, + InferenceConfig inferenceConfig, + TrainedModelLocation location + ) { this.modelId = modelId; this.modelType = modelType; this.createdBy = createdBy; @@ -287,28 +291,29 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; TrainedModelConfig that = (TrainedModelConfig) o; - return Objects.equals(modelId, that.modelId) && - Objects.equals(modelType, that.modelType) && - Objects.equals(createdBy, that.createdBy) && - Objects.equals(version, that.version) && - Objects.equals(description, that.description) && - Objects.equals(createTime, that.createTime) && - Objects.equals(definition, that.definition) && - Objects.equals(compressedDefinition, that.compressedDefinition) && - Objects.equals(tags, that.tags) && - Objects.equals(input, that.input) && - Objects.equals(estimatedHeapMemory, that.estimatedHeapMemory) && - Objects.equals(estimatedOperations, that.estimatedOperations) && - Objects.equals(licenseLevel, that.licenseLevel) && - Objects.equals(defaultFieldMap, that.defaultFieldMap) && - Objects.equals(inferenceConfig, that.inferenceConfig) && - Objects.equals(metadata, that.metadata) && - Objects.equals(location, that.location); + return Objects.equals(modelId, that.modelId) + && Objects.equals(modelType, that.modelType) + && Objects.equals(createdBy, that.createdBy) + && Objects.equals(version, that.version) + && Objects.equals(description, that.description) + && Objects.equals(createTime, that.createTime) + && Objects.equals(definition, that.definition) + && Objects.equals(compressedDefinition, that.compressedDefinition) + && Objects.equals(tags, that.tags) + && Objects.equals(input, that.input) + && Objects.equals(estimatedHeapMemory, that.estimatedHeapMemory) + && Objects.equals(estimatedOperations, that.estimatedOperations) + && Objects.equals(licenseLevel, that.licenseLevel) + && Objects.equals(defaultFieldMap, that.defaultFieldMap) + && Objects.equals(inferenceConfig, that.inferenceConfig) + && Objects.equals(metadata, that.metadata) + && Objects.equals(location, that.location); } @Override public int hashCode() { - return Objects.hash(modelId, + return Objects.hash( + modelId, modelType, createdBy, version, @@ -324,10 +329,10 @@ public int hashCode() { input, inferenceConfig, defaultFieldMap, - location); + location + ); } - public static class Builder { private String modelId; @@ -469,7 +474,8 @@ public TrainedModelConfig build() { licenseLevel, defaultFieldMap, inferenceConfig, - location); + location + ); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/TrainedModelDefinition.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/TrainedModelDefinition.java index d6f30fa7c22ad..3ca84bc62cbd5 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/TrainedModelDefinition.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/TrainedModelDefinition.java @@ -9,9 +9,9 @@ import org.elasticsearch.client.ml.inference.preprocessing.PreProcessor; import org.elasticsearch.client.ml.inference.trainedmodel.TrainedModel; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -28,17 +28,19 @@ public class TrainedModelDefinition implements ToXContentObject { public static final ParseField TRAINED_MODEL = new ParseField("trained_model"); public static final ParseField PREPROCESSORS = new ParseField("preprocessors"); - public static final ObjectParser PARSER = new ObjectParser<>(NAME, - true, - TrainedModelDefinition.Builder::new); + public static final ObjectParser PARSER = new ObjectParser<>(NAME, true, TrainedModelDefinition.Builder::new); static { - PARSER.declareNamedObject(TrainedModelDefinition.Builder::setTrainedModel, + PARSER.declareNamedObject( + TrainedModelDefinition.Builder::setTrainedModel, (p, c, n) -> p.namedObject(TrainedModel.class, n, null), - TRAINED_MODEL); - PARSER.declareNamedObjects(TrainedModelDefinition.Builder::setPreProcessors, + TRAINED_MODEL + ); + PARSER.declareNamedObjects( + TrainedModelDefinition.Builder::setPreProcessors, (p, c, n) -> p.namedObject(PreProcessor.class, n, null), (trainedModelDefBuilder) -> {/* Does not matter client side*/ }, - PREPROCESSORS); + PREPROCESSORS + ); } public static TrainedModelDefinition.Builder fromXContent(XContentParser parser) throws IOException { @@ -56,16 +58,14 @@ public static TrainedModelDefinition.Builder fromXContent(XContentParser parser) @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); - NamedXContentObjectHelper.writeNamedObjects(builder, + NamedXContentObjectHelper.writeNamedObjects( + builder, params, false, TRAINED_MODEL.getPreferredName(), - Collections.singletonList(trainedModel)); - NamedXContentObjectHelper.writeNamedObjects(builder, - params, - true, - PREPROCESSORS.getPreferredName(), - preProcessors); + Collections.singletonList(trainedModel) + ); + NamedXContentObjectHelper.writeNamedObjects(builder, params, true, PREPROCESSORS.getPreferredName(), preProcessors); builder.endObject(); return builder; } @@ -88,8 +88,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; TrainedModelDefinition that = (TrainedModelDefinition) o; - return Objects.equals(trainedModel, that.trainedModel) && - Objects.equals(preProcessors, that.preProcessors); + return Objects.equals(trainedModel, that.trainedModel) && Objects.equals(preProcessors, that.preProcessors); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/TrainedModelInput.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/TrainedModelInput.java index 95601e0ce0b59..d6e2d0559396c 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/TrainedModelInput.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/TrainedModelInput.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml.inference; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -24,9 +24,11 @@ public class TrainedModelInput implements ToXContentObject { public static final ParseField FIELD_NAMES = new ParseField("field_names"); @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(NAME, + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, true, - a -> new TrainedModelInput((List) a[0])); + a -> new TrainedModelInput((List) a[0]) + ); static { PARSER.declareStringArray(ConstructingObjectParser.constructorArg(), FIELD_NAMES); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/TrainedModelStats.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/TrainedModelStats.java index 0b5f5945ca451..bd45805e70603 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/TrainedModelStats.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/TrainedModelStats.java @@ -9,12 +9,12 @@ import org.elasticsearch.client.ml.inference.trainedmodel.InferenceStats; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.ingest.IngestStats; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.ingest.IngestStats; import java.io.IOException; import java.util.Map; @@ -36,11 +36,11 @@ public class TrainedModelStats implements ToXContentObject { private final InferenceStats inferenceStats; @SuppressWarnings("unchecked") - static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>( - "trained_model_stats", - true, - args -> new TrainedModelStats((String) args[0], (Map) args[1], (Integer) args[2], (InferenceStats) args[3])); + static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "trained_model_stats", + true, + args -> new TrainedModelStats((String) args[0], (Map) args[1], (Integer) args[2], (InferenceStats) args[3]) + ); static { PARSER.declareString(constructorArg(), MODEL_ID); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/TrainedModelType.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/TrainedModelType.java index 7829a4a56f0ab..e34c01d880b87 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/TrainedModelType.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/TrainedModelType.java @@ -11,7 +11,9 @@ import java.util.Locale; public enum TrainedModelType { - TREE_ENSEMBLE, LANG_IDENT, PYTORCH; + TREE_ENSEMBLE, + LANG_IDENT, + PYTORCH; public static TrainedModelType fromString(String name) { return valueOf(name.trim().toUpperCase(Locale.ROOT)); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/preprocessing/CustomWordEmbedding.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/preprocessing/CustomWordEmbedding.java index 6b9d8a6c3414b..3ad8170b3ce9f 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/preprocessing/CustomWordEmbedding.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/preprocessing/CustomWordEmbedding.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.ml.inference.preprocessing; import org.elasticsearch.core.CheckedFunction; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -41,61 +41,57 @@ public class CustomWordEmbedding implements PreProcessor { public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( NAME, true, - a -> new CustomWordEmbedding((short[][])a[0], (byte[][])a[1], (String)a[2], (String)a[3])); + a -> new CustomWordEmbedding((short[][]) a[0], (byte[][]) a[1], (String) a[2], (String) a[3]) + ); static { - PARSER.declareField(ConstructingObjectParser.constructorArg(), - (p, c) -> { - List> listOfListOfShorts = parseArrays(EMBEDDING_QUANT_SCALES.getPreferredName(), - XContentParser::shortValue, - p); - short[][] primitiveShorts = new short[listOfListOfShorts.size()][]; - int i = 0; - for (List shorts : listOfListOfShorts) { - short[] innerShorts = new short[shorts.size()]; - for (int j = 0; j < shorts.size(); j++) { - innerShorts[j] = shorts.get(j); - } - primitiveShorts[i++] = innerShorts; - } - return primitiveShorts; - }, - EMBEDDING_QUANT_SCALES, - ObjectParser.ValueType.VALUE_ARRAY); - PARSER.declareField(ConstructingObjectParser.constructorArg(), - (p, c) -> { - List values = new ArrayList<>(); - while(p.nextToken() != XContentParser.Token.END_ARRAY) { - values.add(p.binaryValue()); - } - byte[][] primitiveBytes = new byte[values.size()][]; - int i = 0; - for (byte[] bytes : values) { - primitiveBytes[i++] = bytes; + PARSER.declareField(ConstructingObjectParser.constructorArg(), (p, c) -> { + List> listOfListOfShorts = parseArrays(EMBEDDING_QUANT_SCALES.getPreferredName(), XContentParser::shortValue, p); + short[][] primitiveShorts = new short[listOfListOfShorts.size()][]; + int i = 0; + for (List shorts : listOfListOfShorts) { + short[] innerShorts = new short[shorts.size()]; + for (int j = 0; j < shorts.size(); j++) { + innerShorts[j] = shorts.get(j); } - return primitiveBytes; - }, - EMBEDDING_WEIGHTS, - ObjectParser.ValueType.VALUE_ARRAY); + primitiveShorts[i++] = innerShorts; + } + return primitiveShorts; + }, EMBEDDING_QUANT_SCALES, ObjectParser.ValueType.VALUE_ARRAY); + PARSER.declareField(ConstructingObjectParser.constructorArg(), (p, c) -> { + List values = new ArrayList<>(); + while (p.nextToken() != XContentParser.Token.END_ARRAY) { + values.add(p.binaryValue()); + } + byte[][] primitiveBytes = new byte[values.size()][]; + int i = 0; + for (byte[] bytes : values) { + primitiveBytes[i++] = bytes; + } + return primitiveBytes; + }, EMBEDDING_WEIGHTS, ObjectParser.ValueType.VALUE_ARRAY); PARSER.declareString(ConstructingObjectParser.constructorArg(), FIELD); PARSER.declareString(ConstructingObjectParser.constructorArg(), DEST_FIELD); } - private static List> parseArrays(String fieldName, - CheckedFunction fromParser, - XContentParser p) throws IOException { + private static List> parseArrays( + String fieldName, + CheckedFunction fromParser, + XContentParser p + ) throws IOException { if (p.currentToken() != XContentParser.Token.START_ARRAY) { throw new IllegalArgumentException("unexpected token [" + p.currentToken() + "] for [" + fieldName + "]"); } List> values = new ArrayList<>(); - while(p.nextToken() != XContentParser.Token.END_ARRAY) { + while (p.nextToken() != XContentParser.Token.END_ARRAY) { if (p.currentToken() != XContentParser.Token.START_ARRAY) { throw new IllegalArgumentException("unexpected token [" + p.currentToken() + "] for [" + fieldName + "]"); } List innerList = new ArrayList<>(); - while(p.nextToken() != XContentParser.Token.END_ARRAY) { - if(p.currentToken().isValue() == false) { - throw new IllegalStateException("expected non-null value but got [" + p.currentToken() + "] " + - "for [" + fieldName + "]"); + while (p.nextToken() != XContentParser.Token.END_ARRAY) { + if (p.currentToken().isValue() == false) { + throw new IllegalStateException( + "expected non-null value but got [" + p.currentToken() + "] " + "for [" + fieldName + "]" + ); } innerList.add(fromParser.apply(p)); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/preprocessing/FrequencyEncoding.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/preprocessing/FrequencyEncoding.java index 3092c5d70d060..e30fdb44cce4d 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/preprocessing/FrequencyEncoding.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/preprocessing/FrequencyEncoding.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml.inference.preprocessing; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -19,7 +19,6 @@ import java.util.Map; import java.util.Objects; - /** * PreProcessor for frequency encoding a set of categorical values for a given field. */ @@ -35,13 +34,16 @@ public class FrequencyEncoding implements PreProcessor { public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( NAME, true, - a -> new FrequencyEncoding((String)a[0], (String)a[1], (Map)a[2], (Boolean)a[3])); + a -> new FrequencyEncoding((String) a[0], (String) a[1], (Map) a[2], (Boolean) a[3]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), FIELD); PARSER.declareString(ConstructingObjectParser.constructorArg(), FEATURE_NAME); - PARSER.declareObject(ConstructingObjectParser.constructorArg(), + PARSER.declareObject( + ConstructingObjectParser.constructorArg(), (p, c) -> p.map(HashMap::new, XContentParser::doubleValue), - FREQUENCY_MAP); + FREQUENCY_MAP + ); PARSER.declareBoolean(ConstructingObjectParser.optionalConstructorArg(), CUSTOM); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/preprocessing/Multi.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/preprocessing/Multi.java index 965f2b94d5ac6..bf9f1aba2c057 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/preprocessing/Multi.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/preprocessing/Multi.java @@ -8,18 +8,17 @@ package org.elasticsearch.client.ml.inference.preprocessing; - -import java.io.IOException; -import java.util.List; -import java.util.Objects; - import org.elasticsearch.client.ml.inference.NamedXContentObjectHelper; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; +import java.io.IOException; +import java.util.List; +import java.util.Objects; + /** * Multi-PreProcessor for chaining together multiple processors */ @@ -33,12 +32,15 @@ public class Multi implements PreProcessor { public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( NAME, true, - a -> new Multi((List)a[0], (Boolean)a[1])); + a -> new Multi((List) a[0], (Boolean) a[1]) + ); static { - PARSER.declareNamedObjects(ConstructingObjectParser.constructorArg(), + PARSER.declareNamedObjects( + ConstructingObjectParser.constructorArg(), (p, c, n) -> p.namedObject(PreProcessor.class, n, null), (_unused) -> {/* Does not matter client side*/ }, - PROCESSORS); + PROCESSORS + ); PARSER.declareBoolean(ConstructingObjectParser.optionalConstructorArg(), CUSTOM); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/preprocessing/NGram.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/preprocessing/NGram.java index 574787f5720dc..bd831a6bf8d54 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/preprocessing/NGram.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/preprocessing/NGram.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml.inference.preprocessing; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -20,7 +20,6 @@ import java.util.function.IntFunction; import java.util.stream.IntStream; - /** * PreProcessor for n-gram encoding a string */ @@ -36,14 +35,10 @@ public class NGram implements PreProcessor { @SuppressWarnings("unchecked") private static final ConstructingObjectParser PARSER = new ConstructingObjectParser( - NAME, - true, - a -> new NGram((String)a[0], - (List)a[1], - (Integer)a[2], - (Integer)a[3], - (Boolean)a[4], - (String)a[5])); + NAME, + true, + a -> new NGram((String) a[0], (List) a[1], (Integer) a[2], (Integer) a[3], (Boolean) a[4], (String) a[5]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), FIELD); PARSER.declareIntArray(ConstructingObjectParser.constructorArg(), NGRAMS); @@ -136,12 +131,12 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; NGram nGram = (NGram) o; - return Objects.equals(field, nGram.field) && - Objects.equals(featurePrefix, nGram.featurePrefix) && - Objects.equals(nGrams, nGram.nGrams) && - Objects.equals(start, nGram.start) && - Objects.equals(length, nGram.length) && - Objects.equals(custom, nGram.custom); + return Objects.equals(field, nGram.field) + && Objects.equals(featurePrefix, nGram.featurePrefix) + && Objects.equals(nGrams, nGram.nGrams) + && Objects.equals(start, nGram.start) + && Objects.equals(length, nGram.length) + && Objects.equals(custom, nGram.custom); } @Override @@ -150,10 +145,7 @@ public int hashCode() { } private String nGramFeature(int nGram, int pos) { - return featurePrefix - + "." - + nGram - + pos; + return featurePrefix + "." + nGram + pos; } private List allPossibleNGramOutputFeatureNames() { diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/preprocessing/OneHotEncoding.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/preprocessing/OneHotEncoding.java index 84c83c50e9323..461c62fd54c0d 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/preprocessing/OneHotEncoding.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/preprocessing/OneHotEncoding.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml.inference.preprocessing; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -33,7 +33,8 @@ public class OneHotEncoding implements PreProcessor { public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( NAME, true, - a -> new OneHotEncoding((String)a[0], (Map)a[1], (Boolean)a[2])); + a -> new OneHotEncoding((String) a[0], (Map) a[1], (Boolean) a[2]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), FIELD); PARSER.declareObject(ConstructingObjectParser.constructorArg(), (p, c) -> p.mapStrings(), HOT_MAP); @@ -53,6 +54,7 @@ public static OneHotEncoding fromXContent(XContentParser parser) { this.hotMap = Collections.unmodifiableMap(Objects.requireNonNull(hotMap)); this.custom = custom; } + /** * @return Field name on which to one hot encode */ @@ -93,9 +95,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; OneHotEncoding that = (OneHotEncoding) o; - return Objects.equals(field, that.field) - && Objects.equals(hotMap, that.hotMap) - && Objects.equals(custom, that.custom); + return Objects.equals(field, that.field) && Objects.equals(hotMap, that.hotMap) && Objects.equals(custom, that.custom); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/preprocessing/PreProcessor.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/preprocessing/PreProcessor.java index 58fe903d03f89..a9e21874313ad 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/preprocessing/PreProcessor.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/preprocessing/PreProcessor.java @@ -9,7 +9,6 @@ import org.elasticsearch.client.ml.inference.NamedXContentObject; - /** * Describes a pre-processor for a defined machine learning model */ diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/preprocessing/TargetMeanEncoding.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/preprocessing/TargetMeanEncoding.java index b51bfe2bdb521..756f5d2da8093 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/preprocessing/TargetMeanEncoding.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/preprocessing/TargetMeanEncoding.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml.inference.preprocessing; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -19,7 +19,6 @@ import java.util.Map; import java.util.Objects; - /** * PreProcessor for target mean encoding a set of categorical values for a given field. */ @@ -36,13 +35,16 @@ public class TargetMeanEncoding implements PreProcessor { public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( NAME, true, - a -> new TargetMeanEncoding((String)a[0], (String)a[1], (Map)a[2], (Double)a[3], (Boolean)a[4])); + a -> new TargetMeanEncoding((String) a[0], (String) a[1], (Map) a[2], (Double) a[3], (Boolean) a[4]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), FIELD); PARSER.declareString(ConstructingObjectParser.constructorArg(), FEATURE_NAME); - PARSER.declareObject(ConstructingObjectParser.constructorArg(), + PARSER.declareObject( + ConstructingObjectParser.constructorArg(), (p, c) -> p.map(HashMap::new, XContentParser::doubleValue), - TARGET_MAP); + TARGET_MAP + ); PARSER.declareDouble(ConstructingObjectParser.constructorArg(), DEFAULT_VALUE); PARSER.declareBoolean(ConstructingObjectParser.optionalConstructorArg(), CUSTOM); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/results/FeatureImportance.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/results/FeatureImportance.java index cfeccf6399675..d68dfc88488a7 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/results/FeatureImportance.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/results/FeatureImportance.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.ml.inference.results; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParseException; @@ -31,17 +31,20 @@ public class FeatureImportance implements ToXContentObject { public static final String CLASSES = "classes"; @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("feature_importance", true, - a -> new FeatureImportance((String) a[0], (Double) a[1], (List) a[2]) - ); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "feature_importance", + true, + a -> new FeatureImportance((String) a[0], (Double) a[1], (List) a[2]) + ); static { PARSER.declareString(constructorArg(), new ParseField(FeatureImportance.FEATURE_NAME)); PARSER.declareDouble(optionalConstructorArg(), new ParseField(FeatureImportance.IMPORTANCE)); - PARSER.declareObjectArray(optionalConstructorArg(), + PARSER.declareObjectArray( + optionalConstructorArg(), (p, c) -> ClassImportance.fromXContent(p), - new ParseField(FeatureImportance.CLASSES)); + new ParseField(FeatureImportance.CLASSES) + ); } public static FeatureImportance fromXContent(XContentParser parser) { @@ -86,8 +89,12 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws @Override public boolean equals(Object object) { - if (object == this) { return true; } - if (object == null || getClass() != object.getClass()) { return false; } + if (object == this) { + return true; + } + if (object == null || getClass() != object.getClass()) { + return false; + } FeatureImportance that = (FeatureImportance) object; return Objects.equals(featureName, that.featureName) && Objects.equals(importance, that.importance) @@ -103,11 +110,11 @@ public static class ClassImportance implements ToXContentObject { static final String CLASS_NAME = "class_name"; - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("feature_importance_class_importance", - true, - a -> new ClassImportance(a[0], (Double) a[1]) - ); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "feature_importance_class_importance", + true, + a -> new ClassImportance(a[0], (Double) a[1]) + ); static { PARSER.declareField(ConstructingObjectParser.constructorArg(), (p, c) -> { @@ -157,8 +164,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; ClassImportance that = (ClassImportance) o; - return Double.compare(that.importance, importance) == 0 && - Objects.equals(className, that.className); + return Double.compare(that.importance, importance) == 0 && Objects.equals(className, that.className); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/results/TopClassEntry.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/results/TopClassEntry.java index 2cff338a91c36..889677f6dd99f 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/results/TopClassEntry.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/results/TopClassEntry.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.ml.inference.results; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -30,8 +30,11 @@ public class TopClassEntry implements ToXContentObject { public static final String NAME = "top_class"; - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(NAME, true, a -> new TopClassEntry(a[0], (Double) a[1], (Double) a[2])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + true, + a -> new TopClassEntry(a[0], (Double) a[1], (Double) a[2]) + ); static { PARSER.declareField(constructorArg(), (p, n) -> { @@ -44,9 +47,10 @@ public class TopClassEntry implements ToXContentObject { } else if (token == XContentParser.Token.VALUE_NUMBER) { o = p.doubleValue(); } else { - throw new XContentParseException(p.getTokenLocation(), - "[" + NAME + "] failed to parse field [" + CLASS_NAME + "] value [" + token - + "] is not a string, boolean or number"); + throw new XContentParseException( + p.getTokenLocation(), + "[" + NAME + "] failed to parse field [" + CLASS_NAME + "] value [" + token + "] is not a string, boolean or number" + ); } return o; }, CLASS_NAME, ObjectParser.ValueType.VALUE); @@ -92,8 +96,12 @@ public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params par @Override public boolean equals(Object object) { - if (object == this) { return true; } - if (object == null || getClass() != object.getClass()) { return false; } + if (object == this) { + return true; + } + if (object == null || getClass() != object.getClass()) { + return false; + } TopClassEntry that = (TopClassEntry) object; return Objects.equals(classification, that.classification) && probability == that.probability && score == that.score; } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/ClassificationConfig.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/ClassificationConfig.java index b8d31d3c3cdd8..1e63677bfd70b 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/ClassificationConfig.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/ClassificationConfig.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml.inference.trainedmodel; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -27,15 +27,16 @@ public class ClassificationConfig implements InferenceConfig { public static final ParseField TOP_CLASSES_RESULTS_FIELD = new ParseField("top_classes_results_field"); public static final ParseField NUM_TOP_FEATURE_IMPORTANCE_VALUES = new ParseField("num_top_feature_importance_values"); - private final Integer numTopClasses; private final String topClassesResultsField; private final String resultsField; private final Integer numTopFeatureImportanceValues; - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(NAME.getPreferredName(), true, args -> new ClassificationConfig( - (Integer) args[0], (String) args[1], (String) args[2], (Integer) args[3])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME.getPreferredName(), + true, + args -> new ClassificationConfig((Integer) args[0], (String) args[1], (String) args[2], (Integer) args[3]) + ); static { PARSER.declareInt(optionalConstructorArg(), NUM_TOP_CLASSES); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/IndexLocation.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/IndexLocation.java index 6704f4200bb0b..44bcac9e67554 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/IndexLocation.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/IndexLocation.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml.inference.trainedmodel; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -21,8 +21,11 @@ public class IndexLocation implements TrainedModelLocation { public static final String INDEX = "index"; private static final ParseField NAME = new ParseField("name"); - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(INDEX, true, a -> new IndexLocation((String) a[0])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + INDEX, + true, + a -> new IndexLocation((String) a[0]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), NAME); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/InferenceConfig.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/InferenceConfig.java index 2c82dcf830299..1b444cc14cbe2 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/InferenceConfig.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/InferenceConfig.java @@ -9,7 +9,6 @@ import org.elasticsearch.client.ml.inference.NamedXContentObject; - public interface InferenceConfig extends NamedXContentObject { } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/InferenceStats.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/InferenceStats.java index 094790718365f..d668f7a2aa6fc 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/InferenceStats.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/InferenceStats.java @@ -10,9 +10,9 @@ import org.elasticsearch.client.common.TimeUtil; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -32,17 +32,19 @@ public class InferenceStats implements ToXContentObject { public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( NAME, true, - a -> new InferenceStats((Long)a[0], (Long)a[1], (Long)a[2], (Long)a[3], (Instant)a[4]) + a -> new InferenceStats((Long) a[0], (Long) a[1], (Long) a[2], (Long) a[3], (Instant) a[4]) ); static { PARSER.declareLong(ConstructingObjectParser.constructorArg(), MISSING_ALL_FIELDS_COUNT); PARSER.declareLong(ConstructingObjectParser.constructorArg(), INFERENCE_COUNT); PARSER.declareLong(ConstructingObjectParser.constructorArg(), FAILURE_COUNT); PARSER.declareLong(ConstructingObjectParser.optionalConstructorArg(), CACHE_MISS_COUNT); - PARSER.declareField(ConstructingObjectParser.constructorArg(), + PARSER.declareField( + ConstructingObjectParser.constructorArg(), p -> TimeUtil.parseTimeFieldToInstant(p, TIMESTAMP.getPreferredName()), TIMESTAMP, - ObjectParser.ValueType.VALUE); + ObjectParser.ValueType.VALUE + ); } private final long missingAllFieldsCount; @@ -51,30 +53,24 @@ public class InferenceStats implements ToXContentObject { private final long cacheMissCount; private final Instant timeStamp; - private InferenceStats(Long missingAllFieldsCount, - Long inferenceCount, - Long failureCount, - Long cacheMissCount, - Instant instant) { - this(unboxOrZero(missingAllFieldsCount), + private InferenceStats(Long missingAllFieldsCount, Long inferenceCount, Long failureCount, Long cacheMissCount, Instant instant) { + this( + unboxOrZero(missingAllFieldsCount), unboxOrZero(inferenceCount), unboxOrZero(failureCount), unboxOrZero(cacheMissCount), - instant); + instant + ); } - public InferenceStats(long missingAllFieldsCount, - long inferenceCount, - long failureCount, - long cacheMissCount, - Instant timeStamp) { + public InferenceStats(long missingAllFieldsCount, long inferenceCount, long failureCount, long cacheMissCount, Instant timeStamp) { this.missingAllFieldsCount = missingAllFieldsCount; this.inferenceCount = inferenceCount; this.failureCount = failureCount; this.cacheMissCount = cacheMissCount; - this.timeStamp = timeStamp == null ? - Instant.ofEpochMilli(Instant.now().toEpochMilli()) : - Instant.ofEpochMilli(timeStamp.toEpochMilli()); + this.timeStamp = timeStamp == null + ? Instant.ofEpochMilli(Instant.now().toEpochMilli()) + : Instant.ofEpochMilli(timeStamp.toEpochMilli()); } /** @@ -143,13 +139,18 @@ public int hashCode() { @Override public String toString() { - return "InferenceStats{" + - "missingAllFieldsCount=" + missingAllFieldsCount + - ", inferenceCount=" + inferenceCount + - ", failureCount=" + failureCount + - ", cacheMissCount=" + cacheMissCount + - ", timeStamp=" + timeStamp + - '}'; + return "InferenceStats{" + + "missingAllFieldsCount=" + + missingAllFieldsCount + + ", inferenceCount=" + + inferenceCount + + ", failureCount=" + + failureCount + + ", cacheMissCount=" + + cacheMissCount + + ", timeStamp=" + + timeStamp + + '}'; } private static long unboxOrZero(@Nullable Long value) { diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/RegressionConfig.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/RegressionConfig.java index 1f3134bfc9785..496cceb4e5a17 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/RegressionConfig.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/RegressionConfig.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml.inference.trainedmodel; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -24,10 +24,11 @@ public class RegressionConfig implements InferenceConfig { public static final ParseField RESULTS_FIELD = new ParseField("results_field"); public static final ParseField NUM_TOP_FEATURE_IMPORTANCE_VALUES = new ParseField("num_top_feature_importance_values"); - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(NAME.getPreferredName(), - true, - args -> new RegressionConfig((String) args[0], (Integer)args[1])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME.getPreferredName(), + true, + args -> new RegressionConfig((String) args[0], (Integer) args[1]) + ); static { PARSER.declareString(optionalConstructorArg(), RESULTS_FIELD); @@ -80,7 +81,7 @@ public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params par public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - RegressionConfig that = (RegressionConfig)o; + RegressionConfig that = (RegressionConfig) o; return Objects.equals(this.resultsField, that.resultsField) && Objects.equals(this.numTopFeatureImportanceValues, that.numTopFeatureImportanceValues); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/TargetType.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/TargetType.java index a3af7039e29d2..4f5ce1aecadcc 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/TargetType.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/TargetType.java @@ -13,7 +13,8 @@ public enum TargetType { - REGRESSION, CLASSIFICATION; + REGRESSION, + CLASSIFICATION; public static final ParseField TARGET_TYPE = new ParseField("target_type"); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/TrainedModelLocation.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/TrainedModelLocation.java index c5914b5bb625d..cb86b0d121c1f 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/TrainedModelLocation.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/TrainedModelLocation.java @@ -10,5 +10,4 @@ import org.elasticsearch.client.ml.inference.NamedXContentObject; -public interface TrainedModelLocation extends NamedXContentObject { -} +public interface TrainedModelLocation extends NamedXContentObject {} diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/ensemble/Ensemble.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/ensemble/Ensemble.java index 79d8e2b66c7b5..c19e50b46c824 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/ensemble/Ensemble.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/ensemble/Ensemble.java @@ -11,8 +11,8 @@ import org.elasticsearch.client.ml.inference.trainedmodel.TargetType; import org.elasticsearch.client.ml.inference.trainedmodel.TrainedModel; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -28,25 +28,25 @@ public class Ensemble implements TrainedModel { public static final String NAME = "ensemble"; public static final ParseField FEATURE_NAMES = new ParseField("feature_names"); public static final ParseField TRAINED_MODELS = new ParseField("trained_models"); - public static final ParseField AGGREGATE_OUTPUT = new ParseField("aggregate_output"); + public static final ParseField AGGREGATE_OUTPUT = new ParseField("aggregate_output"); public static final ParseField CLASSIFICATION_LABELS = new ParseField("classification_labels"); public static final ParseField CLASSIFICATION_WEIGHTS = new ParseField("classification_weights"); - private static final ObjectParser PARSER = new ObjectParser<>( - NAME, - true, - Ensemble.Builder::new); + private static final ObjectParser PARSER = new ObjectParser<>(NAME, true, Ensemble.Builder::new); static { PARSER.declareStringArray(Ensemble.Builder::setFeatureNames, FEATURE_NAMES); - PARSER.declareNamedObjects(Ensemble.Builder::setTrainedModels, - (p, c, n) -> - p.namedObject(TrainedModel.class, n, null), + PARSER.declareNamedObjects( + Ensemble.Builder::setTrainedModels, + (p, c, n) -> p.namedObject(TrainedModel.class, n, null), (ensembleBuilder) -> { /* Noop does not matter client side */ }, - TRAINED_MODELS); - PARSER.declareNamedObject(Ensemble.Builder::setOutputAggregator, + TRAINED_MODELS + ); + PARSER.declareNamedObject( + Ensemble.Builder::setOutputAggregator, (p, c, n) -> p.namedObject(OutputAggregator.class, n, null), - AGGREGATE_OUTPUT); + AGGREGATE_OUTPUT + ); PARSER.declareString(Ensemble.Builder::setTargetType, TargetType.TARGET_TYPE); PARSER.declareStringArray(Ensemble.Builder::setClassificationLabels, CLASSIFICATION_LABELS); PARSER.declareDoubleArray(Ensemble.Builder::setClassificationWeights, CLASSIFICATION_WEIGHTS); @@ -63,12 +63,14 @@ public static Ensemble fromXContent(XContentParser parser) { private final List classificationLabels; private final double[] classificationWeights; - Ensemble(List featureNames, - List models, - @Nullable OutputAggregator outputAggregator, - TargetType targetType, - @Nullable List classificationLabels, - @Nullable double[] classificationWeights) { + Ensemble( + List featureNames, + List models, + @Nullable OutputAggregator outputAggregator, + TargetType targetType, + @Nullable List classificationLabels, + @Nullable double[] classificationWeights + ) { this.featureNames = featureNames; this.models = models; this.outputAggregator = outputAggregator; @@ -97,11 +99,13 @@ public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params par NamedXContentObjectHelper.writeNamedObjects(builder, params, true, TRAINED_MODELS.getPreferredName(), models); } if (outputAggregator != null) { - NamedXContentObjectHelper.writeNamedObjects(builder, + NamedXContentObjectHelper.writeNamedObjects( + builder, params, false, AGGREGATE_OUTPUT.getPreferredName(), - Collections.singletonList(outputAggregator)); + Collections.singletonList(outputAggregator) + ); } if (targetType != null) { builder.field(TargetType.TARGET_TYPE.getPreferredName(), targetType); @@ -131,12 +135,14 @@ public boolean equals(Object o) { @Override public int hashCode() { - return Objects.hash(featureNames, + return Objects.hash( + featureNames, models, outputAggregator, classificationLabels, targetType, - Arrays.hashCode(classificationWeights)); + Arrays.hashCode(classificationWeights) + ); } public static Builder builder() { @@ -181,7 +187,6 @@ public Builder setClassificationWeights(List classificationWeights) { return this; } - private void setTargetType(String targetType) { this.targetType = TargetType.fromString(targetType); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/ensemble/Exponent.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/ensemble/Exponent.java index 8b2bda648d2c4..22fc6f7ef3f55 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/ensemble/Exponent.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/ensemble/Exponent.java @@ -7,9 +7,8 @@ */ package org.elasticsearch.client.ml.inference.trainedmodel.ensemble; - -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -17,7 +16,6 @@ import java.util.List; import java.util.Objects; - public class Exponent implements OutputAggregator { public static final String NAME = "exponent"; @@ -27,7 +25,8 @@ public class Exponent implements OutputAggregator { private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( NAME, true, - a -> new Exponent((List)a[0])); + a -> new Exponent((List) a[0]) + ); static { PARSER.declareDoubleArray(ConstructingObjectParser.optionalConstructorArg(), WEIGHTS); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/ensemble/LogisticRegression.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/ensemble/LogisticRegression.java index f4114c1de1df9..19693a728d2ee 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/ensemble/LogisticRegression.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/ensemble/LogisticRegression.java @@ -7,9 +7,8 @@ */ package org.elasticsearch.client.ml.inference.trainedmodel.ensemble; - -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -17,7 +16,6 @@ import java.util.List; import java.util.Objects; - public class LogisticRegression implements OutputAggregator { public static final String NAME = "logistic_regression"; @@ -27,7 +25,8 @@ public class LogisticRegression implements OutputAggregator { private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( NAME, true, - a -> new LogisticRegression((List)a[0])); + a -> new LogisticRegression((List) a[0]) + ); static { PARSER.declareDoubleArray(ConstructingObjectParser.optionalConstructorArg(), WEIGHTS); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/ensemble/WeightedMode.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/ensemble/WeightedMode.java index c35a3de87cde3..422dfb0a21fc1 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/ensemble/WeightedMode.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/ensemble/WeightedMode.java @@ -7,9 +7,8 @@ */ package org.elasticsearch.client.ml.inference.trainedmodel.ensemble; - -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -18,7 +17,6 @@ import java.util.List; import java.util.Objects; - public class WeightedMode implements OutputAggregator { public static final String NAME = "weighted_mode"; @@ -29,7 +27,8 @@ public class WeightedMode implements OutputAggregator { private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( NAME, true, - a -> new WeightedMode((Integer)a[0], (List)a[1])); + a -> new WeightedMode((Integer) a[0], (List) a[1]) + ); static { PARSER.declareInt(ConstructingObjectParser.constructorArg(), NUM_CLASSES); PARSER.declareDoubleArray(ConstructingObjectParser.optionalConstructorArg(), WEIGHTS); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/ensemble/WeightedSum.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/ensemble/WeightedSum.java index ef106a3cd2195..a36c13b138f78 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/ensemble/WeightedSum.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/ensemble/WeightedSum.java @@ -7,9 +7,8 @@ */ package org.elasticsearch.client.ml.inference.trainedmodel.ensemble; - -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -27,7 +26,8 @@ public class WeightedSum implements OutputAggregator { private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( NAME, true, - a -> new WeightedSum((List)a[0])); + a -> new WeightedSum((List) a[0]) + ); static { PARSER.declareDoubleArray(ConstructingObjectParser.optionalConstructorArg(), WEIGHTS); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/langident/LangIdentNeuralNetwork.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/langident/LangIdentNeuralNetwork.java index 9f251ccf4db0c..89f5625331cd7 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/langident/LangIdentNeuralNetwork.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/langident/LangIdentNeuralNetwork.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml.inference.trainedmodel.langident; import org.elasticsearch.client.ml.inference.trainedmodel.TrainedModel; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -33,9 +33,8 @@ public class LangIdentNeuralNetwork implements TrainedModel { public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( NAME, true, - a -> new LangIdentNeuralNetwork((String) a[0], - (LangNetLayer) a[1], - (LangNetLayer) a[2])); + a -> new LangIdentNeuralNetwork((String) a[0], (LangNetLayer) a[1], (LangNetLayer) a[2]) + ); static { PARSER.declareString(constructorArg(), EMBEDDED_VECTOR_FEATURE_NAME); @@ -51,9 +50,7 @@ public static LangIdentNeuralNetwork fromXContent(XContentParser parser) { private final LangNetLayer softmaxLayer; private final String embeddedVectorFeatureName; - LangIdentNeuralNetwork(String embeddedVectorFeatureName, - LangNetLayer hiddenLayer, - LangNetLayer softmaxLayer) { + LangIdentNeuralNetwork(String embeddedVectorFeatureName, LangNetLayer hiddenLayer, LangNetLayer softmaxLayer) { this.embeddedVectorFeatureName = embeddedVectorFeatureName; this.hiddenLayer = hiddenLayer; this.softmaxLayer = softmaxLayer; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/langident/LangNetLayer.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/langident/LangNetLayer.java index c982c701e7b31..9737a577725f7 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/langident/LangNetLayer.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/langident/LangNetLayer.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml.inference.trainedmodel.langident; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -35,11 +35,8 @@ public class LangNetLayer implements ToXContentObject { public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( NAME.getPreferredName(), true, - a -> new LangNetLayer( - (List) a[0], - (int) a[1], - (int) a[2], - (List) a[3])); + a -> new LangNetLayer((List) a[0], (int) a[1], (int) a[2], (List) a[3]) + ); static { PARSER.declareDoubleArray(constructorArg(), WEIGHTS); @@ -54,10 +51,12 @@ public class LangNetLayer implements ToXContentObject { private final double[] bias; private LangNetLayer(List weights, int numCols, int numRows, List bias) { - this(weights.stream().mapToDouble(Double::doubleValue).toArray(), + this( + weights.stream().mapToDouble(Double::doubleValue).toArray(), numCols, numRows, - bias.stream().mapToDouble(Double::doubleValue).toArray()); + bias.stream().mapToDouble(Double::doubleValue).toArray() + ); } LangNetLayer(double[] weights, int numCols, int numRows, double[] bias) { diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/tree/Tree.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/tree/Tree.java index dabc8e3eba287..7d0b633693e7d 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/tree/Tree.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/tree/Tree.java @@ -9,10 +9,10 @@ import org.elasticsearch.client.ml.inference.trainedmodel.TargetType; import org.elasticsearch.client.ml.inference.trainedmodel.TrainedModel; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -96,7 +96,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.field(TargetType.TARGET_TYPE.getPreferredName(), targetType.toString()); } builder.endObject(); - return builder; + return builder; } @Override @@ -219,10 +219,12 @@ public Builder addLeaf(int nodeIndex, double value) { } public Tree build() { - return new Tree(featureNames, + return new Tree( + featureNames, nodes.stream().map(TreeNode.Builder::build).collect(Collectors.toList()), targetType, - classificationLabels); + classificationLabels + ); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/tree/TreeNode.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/tree/TreeNode.java index 5ad43e9a4283c..cb7d9a0f8f211 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/tree/TreeNode.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/inference/trainedmodel/tree/TreeNode.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.ml.inference.trainedmodel.tree; import org.elasticsearch.client.ml.job.config.Operator; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -34,17 +34,10 @@ public class TreeNode implements ToXContentObject { public static final ParseField LEAF_VALUE = new ParseField("leaf_value"); public static final ParseField NUMBER_SAMPLES = new ParseField("number_samples"); - - private static final ObjectParser PARSER = new ObjectParser<>( - NAME, - true, - Builder::new); + private static final ObjectParser PARSER = new ObjectParser<>(NAME, true, Builder::new); static { PARSER.declareDouble(Builder::setThreshold, THRESHOLD); - PARSER.declareField(Builder::setOperator, - p -> Operator.fromString(p.text()), - DECISION_TYPE, - ObjectParser.ValueType.STRING); + PARSER.declareField(Builder::setOperator, p -> Operator.fromString(p.text()), DECISION_TYPE, ObjectParser.ValueType.STRING); PARSER.declareInt(Builder::setLeftChild, LEFT_CHILD); PARSER.declareInt(Builder::setRightChild, RIGHT_CHILD); PARSER.declareBoolean(Builder::setDefaultLeft, DEFAULT_LEFT); @@ -70,25 +63,26 @@ public static Builder fromXContent(XContentParser parser) { private final Integer rightChild; private final Long numberSamples; - - TreeNode(Operator operator, - Double threshold, - Integer splitFeature, - int nodeIndex, - Double splitGain, - List leafValue, - Boolean defaultLeft, - Integer leftChild, - Integer rightChild, - Long numberSamples) { + TreeNode( + Operator operator, + Double threshold, + Integer splitFeature, + int nodeIndex, + Double splitGain, + List leafValue, + Boolean defaultLeft, + Integer leftChild, + Integer rightChild, + Long numberSamples + ) { this.operator = operator; - this.threshold = threshold; + this.threshold = threshold; this.splitFeature = splitFeature; this.nodeIndex = nodeIndex; - this.splitGain = splitGain; + this.splitGain = splitGain; this.leafValue = leafValue; this.defaultLeft = defaultLeft; - this.leftChild = leftChild; + this.leftChild = leftChild; this.rightChild = rightChild; this.numberSamples = numberSamples; } @@ -142,7 +136,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws addOptionalField(builder, SPLIT_GAIN, splitGain); addOptionalField(builder, NODE_INDEX, nodeIndex); addOptionalField(builder, LEAF_VALUE, leafValue); - addOptionalField(builder, DEFAULT_LEFT, defaultLeft ); + addOptionalField(builder, DEFAULT_LEFT, defaultLeft); addOptionalField(builder, LEFT_CHILD, leftChild); addOptionalField(builder, RIGHT_CHILD, rightChild); addOptionalField(builder, NUMBER_SAMPLES, numberSamples); @@ -175,7 +169,8 @@ public boolean equals(Object o) { @Override public int hashCode() { - return Objects.hash(operator, + return Objects.hash( + operator, threshold, splitFeature, splitGain, @@ -184,7 +179,8 @@ public int hashCode() { defaultLeft, leftChild, rightChild, - numberSamples); + numberSamples + ); } @Override @@ -212,8 +208,7 @@ public Builder(int nodeIndex) { this.nodeIndex = nodeIndex; } - private Builder() { - } + private Builder() {} public Builder setOperator(Operator operator) { this.operator = operator; @@ -274,7 +269,8 @@ public Builder setNumberSamples(Long numberSamples) { } public TreeNode build() { - return new TreeNode(operator, + return new TreeNode( + operator, threshold, splitFeature, nodeIndex, @@ -283,7 +279,8 @@ public TreeNode build() { defaultLeft, leftChild, rightChild, - numberSamples); + numberSamples + ); } } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/AnalysisConfig.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/AnalysisConfig.java index 52e2e35f14910..09b8ef16eeda4 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/AnalysisConfig.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/AnalysisConfig.java @@ -7,10 +7,10 @@ */ package org.elasticsearch.client.ml.job.config; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -54,30 +54,41 @@ public class AnalysisConfig implements ToXContentObject { public static final ParseField MODEL_PRUNE_WINDOW = new ParseField("model_prune_window"); @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(ANALYSIS_CONFIG.getPreferredName(), - true, a -> new AnalysisConfig.Builder((List) a[0])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + ANALYSIS_CONFIG.getPreferredName(), + true, + a -> new AnalysisConfig.Builder((List) a[0]) + ); static { - PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), - (p, c) -> (Detector.PARSER).apply(p, c).build(), DETECTORS); - PARSER.declareString((builder, val) -> - builder.setBucketSpan(TimeValue.parseTimeValue(val, BUCKET_SPAN.getPreferredName())), BUCKET_SPAN); + PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), (p, c) -> (Detector.PARSER).apply(p, c).build(), DETECTORS); + PARSER.declareString( + (builder, val) -> builder.setBucketSpan(TimeValue.parseTimeValue(val, BUCKET_SPAN.getPreferredName())), + BUCKET_SPAN + ); PARSER.declareString(Builder::setCategorizationFieldName, CATEGORIZATION_FIELD_NAME); PARSER.declareStringArray(Builder::setCategorizationFilters, CATEGORIZATION_FILTERS); // This one is nasty - the syntax for analyzers takes either names or objects at many levels, hence it's not // possible to simply declare whether the field is a string or object and a completely custom parser is required - PARSER.declareField(Builder::setCategorizationAnalyzerConfig, + PARSER.declareField( + Builder::setCategorizationAnalyzerConfig, (p, c) -> CategorizationAnalyzerConfig.buildFromXContentFragment(p), - CATEGORIZATION_ANALYZER, ObjectParser.ValueType.OBJECT_OR_STRING); - PARSER.declareObject(Builder::setPerPartitionCategorizationConfig, PerPartitionCategorizationConfig.PARSER, - PER_PARTITION_CATEGORIZATION); - PARSER.declareString((builder, val) -> - builder.setLatency(TimeValue.parseTimeValue(val, LATENCY.getPreferredName())), LATENCY); + CATEGORIZATION_ANALYZER, + ObjectParser.ValueType.OBJECT_OR_STRING + ); + PARSER.declareObject( + Builder::setPerPartitionCategorizationConfig, + PerPartitionCategorizationConfig.PARSER, + PER_PARTITION_CATEGORIZATION + ); + PARSER.declareString((builder, val) -> builder.setLatency(TimeValue.parseTimeValue(val, LATENCY.getPreferredName())), LATENCY); PARSER.declareString(Builder::setSummaryCountFieldName, SUMMARY_COUNT_FIELD_NAME); PARSER.declareStringArray(Builder::setInfluencers, INFLUENCERS); PARSER.declareBoolean(Builder::setMultivariateByFields, MULTIVARIATE_BY_FIELDS); - PARSER.declareString((builder, val) -> - builder.setModelPruneWindow(TimeValue.parseTimeValue(val, MODEL_PRUNE_WINDOW.getPreferredName())), MODEL_PRUNE_WINDOW); + PARSER.declareString( + (builder, val) -> builder.setModelPruneWindow(TimeValue.parseTimeValue(val, MODEL_PRUNE_WINDOW.getPreferredName())), + MODEL_PRUNE_WINDOW + ); } /** @@ -95,11 +106,19 @@ public class AnalysisConfig implements ToXContentObject { private final Boolean multivariateByFields; private final TimeValue modelPruneWindow; - private AnalysisConfig(TimeValue bucketSpan, String categorizationFieldName, List categorizationFilters, - CategorizationAnalyzerConfig categorizationAnalyzerConfig, - PerPartitionCategorizationConfig perPartitionCategorizationConfig, TimeValue latency, - String summaryCountFieldName, List detectors, List influencers, Boolean multivariateByFields, - TimeValue modelPruneWindow) { + private AnalysisConfig( + TimeValue bucketSpan, + String categorizationFieldName, + List categorizationFilters, + CategorizationAnalyzerConfig categorizationAnalyzerConfig, + PerPartitionCategorizationConfig perPartitionCategorizationConfig, + TimeValue latency, + String summaryCountFieldName, + List detectors, + List influencers, + Boolean multivariateByFields, + TimeValue modelPruneWindow + ) { this.detectors = Collections.unmodifiableList(detectors); this.bucketSpan = bucketSpan; this.latency = latency; @@ -191,8 +210,7 @@ public List fields() { return collectNonNullAndNonEmptyDetectorFields(Detector::getFieldName); } - private List collectNonNullAndNonEmptyDetectorFields( - Function fieldGetter) { + private List collectNonNullAndNonEmptyDetectorFields(Function fieldGetter) { Set fields = new HashSet<>(); for (Detector d : getDetectors()) { @@ -271,24 +289,34 @@ public boolean equals(Object object) { } AnalysisConfig that = (AnalysisConfig) object; - return Objects.equals(latency, that.latency) && - Objects.equals(bucketSpan, that.bucketSpan) && - Objects.equals(categorizationFieldName, that.categorizationFieldName) && - Objects.equals(categorizationFilters, that.categorizationFilters) && - Objects.equals(categorizationAnalyzerConfig, that.categorizationAnalyzerConfig) && - Objects.equals(perPartitionCategorizationConfig, that.perPartitionCategorizationConfig) && - Objects.equals(summaryCountFieldName, that.summaryCountFieldName) && - Objects.equals(detectors, that.detectors) && - Objects.equals(influencers, that.influencers) && - Objects.equals(multivariateByFields, that.multivariateByFields) && - Objects.equals(modelPruneWindow, that.modelPruneWindow); + return Objects.equals(latency, that.latency) + && Objects.equals(bucketSpan, that.bucketSpan) + && Objects.equals(categorizationFieldName, that.categorizationFieldName) + && Objects.equals(categorizationFilters, that.categorizationFilters) + && Objects.equals(categorizationAnalyzerConfig, that.categorizationAnalyzerConfig) + && Objects.equals(perPartitionCategorizationConfig, that.perPartitionCategorizationConfig) + && Objects.equals(summaryCountFieldName, that.summaryCountFieldName) + && Objects.equals(detectors, that.detectors) + && Objects.equals(influencers, that.influencers) + && Objects.equals(multivariateByFields, that.multivariateByFields) + && Objects.equals(modelPruneWindow, that.modelPruneWindow); } @Override public int hashCode() { return Objects.hash( - bucketSpan, categorizationFieldName, categorizationFilters, categorizationAnalyzerConfig, perPartitionCategorizationConfig, - latency, summaryCountFieldName, detectors, influencers, multivariateByFields, modelPruneWindow); + bucketSpan, + categorizationFieldName, + categorizationFilters, + categorizationAnalyzerConfig, + perPartitionCategorizationConfig, + latency, + summaryCountFieldName, + detectors, + influencers, + multivariateByFields, + modelPruneWindow + ); } public static Builder builder(List detectors) { @@ -318,7 +346,8 @@ public Builder(AnalysisConfig analysisConfig) { this.bucketSpan = analysisConfig.bucketSpan; this.latency = analysisConfig.latency; this.categorizationFieldName = analysisConfig.categorizationFieldName; - this.categorizationFilters = analysisConfig.categorizationFilters == null ? null + this.categorizationFilters = analysisConfig.categorizationFilters == null + ? null : new ArrayList<>(analysisConfig.categorizationFilters); this.categorizationAnalyzerConfig = analysisConfig.categorizationAnalyzerConfig; this.perPartitionCategorizationConfig = analysisConfig.perPartitionCategorizationConfig; @@ -329,7 +358,7 @@ public Builder(AnalysisConfig analysisConfig) { } public Builder setDetectors(List detectors) { - Objects.requireNonNull(detectors, "[" + DETECTORS.getPreferredName() + "] must not be null"); + Objects.requireNonNull(detectors, "[" + DETECTORS.getPreferredName() + "] must not be null"); // We always assign sequential IDs to the detectors that are correct for this analysis config int detectorIndex = 0; List sequentialIndexDetectors = new ArrayList<>(detectors.size()); @@ -399,9 +428,19 @@ public Builder setModelPruneWindow(TimeValue modelPruneWindow) { public AnalysisConfig build() { - return new AnalysisConfig(bucketSpan, categorizationFieldName, categorizationFilters, categorizationAnalyzerConfig, - perPartitionCategorizationConfig, latency, summaryCountFieldName, detectors, influencers, multivariateByFields, - modelPruneWindow); + return new AnalysisConfig( + bucketSpan, + categorizationFieldName, + categorizationFilters, + categorizationAnalyzerConfig, + perPartitionCategorizationConfig, + latency, + summaryCountFieldName, + detectors, + influencers, + multivariateByFields, + modelPruneWindow + ); } } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/AnalysisLimits.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/AnalysisLimits.java index 27268f537a60b..f4172c843dd39 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/AnalysisLimits.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/AnalysisLimits.java @@ -7,11 +7,11 @@ */ package org.elasticsearch.client.ml.job.config; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -32,8 +32,11 @@ public class AnalysisLimits implements ToXContentObject { public static final ParseField MODEL_MEMORY_LIMIT = new ParseField("model_memory_limit"); public static final ParseField CATEGORIZATION_EXAMPLES_LIMIT = new ParseField("categorization_examples_limit"); - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("analysis_limits", true, a -> new AnalysisLimits((Long) a[0], (Long) a[1])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "analysis_limits", + true, + a -> new AnalysisLimits((Long) a[0], (Long) a[1]) + ); static { PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(), p -> { @@ -117,8 +120,8 @@ public boolean equals(Object other) { } AnalysisLimits that = (AnalysisLimits) other; - return Objects.equals(this.modelMemoryLimit, that.modelMemoryLimit) && - Objects.equals(this.categorizationExamplesLimit, that.categorizationExamplesLimit); + return Objects.equals(this.modelMemoryLimit, that.modelMemoryLimit) + && Objects.equals(this.categorizationExamplesLimit, that.categorizationExamplesLimit); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/CategorizationAnalyzerConfig.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/CategorizationAnalyzerConfig.java index a0cea26b7e338..8782bacc83f32 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/CategorizationAnalyzerConfig.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/CategorizationAnalyzerConfig.java @@ -8,12 +8,12 @@ package org.elasticsearch.client.ml.job.config; import org.elasticsearch.action.admin.indices.analyze.AnalyzeAction; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.rest.action.admin.indices.RestAnalyzeAction; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentFragment; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.rest.action.admin.indices.RestAnalyzeAction; import java.io.IOException; import java.util.ArrayList; @@ -90,39 +90,60 @@ static CategorizationAnalyzerConfig buildFromXContentFragment(XContentParser par if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (CHAR_FILTERS.match(currentFieldName, parser.getDeprecationHandler()) - && token == XContentParser.Token.START_ARRAY) { - while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { + && token == XContentParser.Token.START_ARRAY) { + while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { + if (token == XContentParser.Token.VALUE_STRING) { + builder.addCharFilter(parser.text()); + } else if (token == XContentParser.Token.START_OBJECT) { + builder.addCharFilter(parser.map()); + } else { + throw new IllegalArgumentException( + "[" + + currentFieldName + + "] in [" + + CATEGORIZATION_ANALYZER + + "] array element should contain char_filter's name or settings [" + + token + + "]" + ); + } + } + } else if (TOKENIZER.match(currentFieldName, parser.getDeprecationHandler())) { if (token == XContentParser.Token.VALUE_STRING) { - builder.addCharFilter(parser.text()); + builder.setTokenizer(parser.text()); } else if (token == XContentParser.Token.START_OBJECT) { - builder.addCharFilter(parser.map()); + builder.setTokenizer(parser.map()); } else { - throw new IllegalArgumentException("[" + currentFieldName + "] in [" + CATEGORIZATION_ANALYZER + - "] array element should contain char_filter's name or settings [" + token + "]"); + throw new IllegalArgumentException( + "[" + + currentFieldName + + "] in [" + + CATEGORIZATION_ANALYZER + + "] should be tokenizer's name or settings [" + + token + + "]" + ); } - } - } else if (TOKENIZER.match(currentFieldName, parser.getDeprecationHandler())) { - if (token == XContentParser.Token.VALUE_STRING) { - builder.setTokenizer(parser.text()); - } else if (token == XContentParser.Token.START_OBJECT) { - builder.setTokenizer(parser.map()); - } else { - throw new IllegalArgumentException("[" + currentFieldName + "] in [" + CATEGORIZATION_ANALYZER + - "] should be tokenizer's name or settings [" + token + "]"); - } - } else if (TOKEN_FILTERS.match(currentFieldName, parser.getDeprecationHandler()) + } else if (TOKEN_FILTERS.match(currentFieldName, parser.getDeprecationHandler()) && token == XContentParser.Token.START_ARRAY) { - while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { - if (token == XContentParser.Token.VALUE_STRING) { - builder.addTokenFilter(parser.text()); - } else if (token == XContentParser.Token.START_OBJECT) { - builder.addTokenFilter(parser.map()); - } else { - throw new IllegalArgumentException("[" + currentFieldName + "] in [" + CATEGORIZATION_ANALYZER + - "] array element should contain token_filter's name or settings [" + token + "]"); + while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { + if (token == XContentParser.Token.VALUE_STRING) { + builder.addTokenFilter(parser.text()); + } else if (token == XContentParser.Token.START_OBJECT) { + builder.addTokenFilter(parser.map()); + } else { + throw new IllegalArgumentException( + "[" + + currentFieldName + + "] in [" + + CATEGORIZATION_ANALYZER + + "] array element should contain token_filter's name or settings [" + + token + + "]" + ); + } + } } - } - } } } @@ -170,8 +191,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; NameOrDefinition that = (NameOrDefinition) o; - return Objects.equals(name, that.name) && - Objects.equals(definition, that.definition); + return Objects.equals(name, that.name) && Objects.equals(definition, that.definition); } @Override @@ -194,8 +214,12 @@ public String toString() { private final NameOrDefinition tokenizer; private final List tokenFilters; - private CategorizationAnalyzerConfig(String analyzer, List charFilters, NameOrDefinition tokenizer, - List tokenFilters) { + private CategorizationAnalyzerConfig( + String analyzer, + List charFilters, + NameOrDefinition tokenizer, + List tokenFilters + ) { this.analyzer = analyzer; this.charFilters = Collections.unmodifiableList(charFilters); this.tokenizer = tokenizer; @@ -251,10 +275,10 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; CategorizationAnalyzerConfig that = (CategorizationAnalyzerConfig) o; - return Objects.equals(analyzer, that.analyzer) && - Objects.equals(charFilters, that.charFilters) && - Objects.equals(tokenizer, that.tokenizer) && - Objects.equals(tokenFilters, that.tokenFilters); + return Objects.equals(analyzer, that.analyzer) + && Objects.equals(charFilters, that.charFilters) + && Objects.equals(tokenizer, that.tokenizer) + && Objects.equals(tokenFilters, that.tokenFilters); } @Override @@ -269,8 +293,7 @@ public static class Builder { private NameOrDefinition tokenizer; private List tokenFilters = new ArrayList<>(); - public Builder() { - } + public Builder() {} public Builder(CategorizationAnalyzerConfig categorizationAnalyzerConfig) { this.analyzer = categorizationAnalyzerConfig.analyzer; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/DataDescription.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/DataDescription.java index c209241b2d071..d460cf9bd81a4 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/DataDescription.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/DataDescription.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml.job.config; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -70,8 +70,11 @@ public String toString() { private final String timeFieldName; private final String timeFormat; - public static final ObjectParser PARSER = - new ObjectParser<>(DATA_DESCRIPTION_FIELD.getPreferredName(), true, Builder::new); + public static final ObjectParser PARSER = new ObjectParser<>( + DATA_DESCRIPTION_FIELD.getPreferredName(), + true, + Builder::new + ); static { PARSER.declareString(Builder::setTimeField, TIME_FIELD_NAME_FIELD); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/DefaultDetectorDescription.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/DefaultDetectorDescription.java index 4a7c1f4e320f4..66ea72f928d54 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/DefaultDetectorDescription.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/DefaultDetectorDescription.java @@ -17,8 +17,7 @@ public final class DefaultDetectorDescription { private static final String PARTITION_FIELD_OPTION = " partitionfield="; private static final String EXCLUDE_FREQUENT_OPTION = " excludefrequent="; - private DefaultDetectorDescription() { - } + private DefaultDetectorDescription() {} /** * Returns the default description for the given {@code detector} @@ -43,8 +42,7 @@ public static void appendOn(Detector detector, StringBuilder sb) { if (isNotNullOrEmpty(detector.getFunction().getFullName())) { sb.append(detector.getFunction()); if (isNotNullOrEmpty(detector.getFieldName())) { - sb.append('(').append(quoteField(detector.getFieldName())) - .append(')'); + sb.append('(').append(quoteField(detector.getFieldName())).append(')'); } } else if (isNotNullOrEmpty(detector.getFieldName())) { sb.append(quoteField(detector.getFieldName())); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/DetectionRule.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/DetectionRule.java index 8cf32dbcd97e2..e23cad0c024aa 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/DetectionRule.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/DetectionRule.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml.job.config; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -26,8 +26,11 @@ public class DetectionRule implements ToXContentObject { public static final ParseField SCOPE_FIELD = new ParseField("scope"); public static final ParseField CONDITIONS_FIELD = new ParseField("conditions"); - public static final ObjectParser PARSER = - new ObjectParser<>(DETECTION_RULE_FIELD.getPreferredName(), true, Builder::new); + public static final ObjectParser PARSER = new ObjectParser<>( + DETECTION_RULE_FIELD.getPreferredName(), + true, + Builder::new + ); static { PARSER.declareStringArray(Builder::setActions, ACTIONS_FIELD); @@ -82,9 +85,7 @@ public boolean equals(Object obj) { } DetectionRule other = (DetectionRule) obj; - return Objects.equals(actions, other.actions) - && Objects.equals(scope, other.scope) - && Objects.equals(conditions, other.conditions); + return Objects.equals(actions, other.actions) && Objects.equals(scope, other.scope) && Objects.equals(conditions, other.conditions); } @Override @@ -105,8 +106,7 @@ public Builder(List conditions) { this.conditions = Objects.requireNonNull(conditions); } - Builder() { - } + Builder() {} public Builder setActions(List actions) { this.actions.clear(); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/Detector.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/Detector.java index a187ae32e431a..f20d67a238008 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/Detector.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/Detector.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml.job.config; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -122,9 +122,18 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws return builder; } - private Detector(String detectorDescription, DetectorFunction function, String fieldName, String byFieldName, String overFieldName, - String partitionFieldName, boolean useNull, ExcludeFrequent excludeFrequent, List rules, - int detectorIndex) { + private Detector( + String detectorDescription, + DetectorFunction function, + String fieldName, + String byFieldName, + String overFieldName, + String partitionFieldName, + boolean useNull, + ExcludeFrequent excludeFrequent, + List rules, + int detectorIndex + ) { this.function = function; this.fieldName = fieldName; this.byFieldName = byFieldName; @@ -230,22 +239,32 @@ public boolean equals(Object other) { Detector that = (Detector) other; - return Objects.equals(this.detectorDescription, that.detectorDescription) && - Objects.equals(this.function, that.function) && - Objects.equals(this.fieldName, that.fieldName) && - Objects.equals(this.byFieldName, that.byFieldName) && - Objects.equals(this.overFieldName, that.overFieldName) && - Objects.equals(this.partitionFieldName, that.partitionFieldName) && - Objects.equals(this.useNull, that.useNull) && - Objects.equals(this.excludeFrequent, that.excludeFrequent) && - Objects.equals(this.rules, that.rules) && - this.detectorIndex == that.detectorIndex; + return Objects.equals(this.detectorDescription, that.detectorDescription) + && Objects.equals(this.function, that.function) + && Objects.equals(this.fieldName, that.fieldName) + && Objects.equals(this.byFieldName, that.byFieldName) + && Objects.equals(this.overFieldName, that.overFieldName) + && Objects.equals(this.partitionFieldName, that.partitionFieldName) + && Objects.equals(this.useNull, that.useNull) + && Objects.equals(this.excludeFrequent, that.excludeFrequent) + && Objects.equals(this.rules, that.rules) + && this.detectorIndex == that.detectorIndex; } @Override public int hashCode() { - return Objects.hash(detectorDescription, function, fieldName, byFieldName, overFieldName, partitionFieldName, useNull, - excludeFrequent, rules, detectorIndex); + return Objects.hash( + detectorDescription, + function, + fieldName, + byFieldName, + overFieldName, + partitionFieldName, + useNull, + excludeFrequent, + rules, + detectorIndex + ); } public static Builder builder() { @@ -266,8 +285,7 @@ public static class Builder { // negative means unknown private int detectorIndex = -1; - public Builder() { - } + public Builder() {} public Builder(Detector detector) { detectorDescription = detector.detectorDescription; @@ -342,8 +360,18 @@ public Builder setDetectorIndex(int detectorIndex) { } public Detector build() { - return new Detector(detectorDescription, function, fieldName, byFieldName, overFieldName, partitionFieldName, - useNull, excludeFrequent, rules, detectorIndex); + return new Detector( + detectorDescription, + function, + fieldName, + byFieldName, + overFieldName, + partitionFieldName, + useNull, + excludeFrequent, + rules, + detectorIndex + ); } } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/FilterRef.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/FilterRef.java index c8880fcdf6659..e9c0fbece98c3 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/FilterRef.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/FilterRef.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml.job.config; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -23,7 +23,8 @@ public class FilterRef implements ToXContentObject { public static final ParseField FILTER_TYPE = new ParseField("filter_type"); public enum FilterType { - INCLUDE, EXCLUDE; + INCLUDE, + EXCLUDE; public static FilterType fromString(String value) { return valueOf(value.toUpperCase(Locale.ROOT)); @@ -35,8 +36,11 @@ public String toString() { } } - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(FILTER_REF_FIELD.getPreferredName(), true, a -> new FilterRef((String) a[0], (FilterType) a[1])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + FILTER_REF_FIELD.getPreferredName(), + true, + a -> new FilterRef((String) a[0], (FilterType) a[1]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), FILTER_ID); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/Job.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/Job.java index 9515c6272dca2..ace8a70c1c742 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/Job.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/Job.java @@ -8,11 +8,11 @@ package org.elasticsearch.client.ml.job.config; import org.elasticsearch.client.common.TimeUtil; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ObjectParser; import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -52,8 +52,7 @@ public class Job implements ToXContentObject { public static final ParseField RENORMALIZATION_WINDOW_DAYS = new ParseField("renormalization_window_days"); public static final ParseField BACKGROUND_PERSIST_INTERVAL = new ParseField("background_persist_interval"); public static final ParseField MODEL_SNAPSHOT_RETENTION_DAYS = new ParseField("model_snapshot_retention_days"); - public static final ParseField DAILY_MODEL_SNAPSHOT_RETENTION_AFTER_DAYS = - new ParseField("daily_model_snapshot_retention_after_days"); + public static final ParseField DAILY_MODEL_SNAPSHOT_RETENTION_AFTER_DAYS = new ParseField("daily_model_snapshot_retention_after_days"); public static final ParseField RESULTS_RETENTION_DAYS = new ParseField("results_retention_days"); public static final ParseField MODEL_SNAPSHOT_ID = new ParseField("model_snapshot_id"); public static final ParseField RESULTS_INDEX_NAME = new ParseField("results_index_name"); @@ -67,21 +66,29 @@ public class Job implements ToXContentObject { PARSER.declareString(Builder::setJobType, JOB_TYPE); PARSER.declareStringArray(Builder::setGroups, GROUPS); PARSER.declareStringOrNull(Builder::setDescription, DESCRIPTION); - PARSER.declareField(Builder::setCreateTime, + PARSER.declareField( + Builder::setCreateTime, (p) -> TimeUtil.parseTimeField(p, CREATE_TIME.getPreferredName()), CREATE_TIME, - ValueType.VALUE); - PARSER.declareField(Builder::setFinishedTime, + ValueType.VALUE + ); + PARSER.declareField( + Builder::setFinishedTime, (p) -> TimeUtil.parseTimeField(p, FINISHED_TIME.getPreferredName()), FINISHED_TIME, - ValueType.VALUE); + ValueType.VALUE + ); PARSER.declareObject(Builder::setAnalysisConfig, AnalysisConfig.PARSER, ANALYSIS_CONFIG); PARSER.declareObject(Builder::setAnalysisLimits, AnalysisLimits.PARSER, ANALYSIS_LIMITS); PARSER.declareObject(Builder::setDataDescription, DataDescription.PARSER, DATA_DESCRIPTION); PARSER.declareObject(Builder::setModelPlotConfig, ModelPlotConfig.PARSER, MODEL_PLOT_CONFIG); PARSER.declareLong(Builder::setRenormalizationWindowDays, RENORMALIZATION_WINDOW_DAYS); - PARSER.declareString((builder, val) -> builder.setBackgroundPersistInterval( - TimeValue.parseTimeValue(val, BACKGROUND_PERSIST_INTERVAL.getPreferredName())), BACKGROUND_PERSIST_INTERVAL); + PARSER.declareString( + (builder, val) -> builder.setBackgroundPersistInterval( + TimeValue.parseTimeValue(val, BACKGROUND_PERSIST_INTERVAL.getPreferredName()) + ), + BACKGROUND_PERSIST_INTERVAL + ); PARSER.declareLong(Builder::setResultsRetentionDays, RESULTS_RETENTION_DAYS); PARSER.declareLong(Builder::setModelSnapshotRetentionDays, MODEL_SNAPSHOT_RETENTION_DAYS); PARSER.declareLong(Builder::setDailyModelSnapshotRetentionAfterDays, DAILY_MODEL_SNAPSHOT_RETENTION_AFTER_DAYS); @@ -114,13 +121,28 @@ public class Job implements ToXContentObject { private final Boolean deleting; private final Boolean allowLazyOpen; - private Job(String jobId, String jobType, List groups, String description, - Date createTime, Date finishedTime, - AnalysisConfig analysisConfig, AnalysisLimits analysisLimits, DataDescription dataDescription, - ModelPlotConfig modelPlotConfig, Long renormalizationWindowDays, TimeValue backgroundPersistInterval, - Long modelSnapshotRetentionDays, Long dailyModelSnapshotRetentionAfterDays, Long resultsRetentionDays, - Map customSettings, String modelSnapshotId, String resultsIndexName, - Boolean deleting, Boolean allowLazyOpen) { + private Job( + String jobId, + String jobType, + List groups, + String description, + Date createTime, + Date finishedTime, + AnalysisConfig analysisConfig, + AnalysisLimits analysisLimits, + DataDescription dataDescription, + ModelPlotConfig modelPlotConfig, + Long renormalizationWindowDays, + TimeValue backgroundPersistInterval, + Long modelSnapshotRetentionDays, + Long dailyModelSnapshotRetentionAfterDays, + Long resultsRetentionDays, + Map customSettings, + String modelSnapshotId, + String resultsIndexName, + Boolean deleting, + Boolean allowLazyOpen + ) { this.jobId = jobId; this.jobType = jobType; @@ -296,8 +318,11 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.timeField(CREATE_TIME.getPreferredName(), CREATE_TIME.getPreferredName() + humanReadableSuffix, createTime.getTime()); } if (finishedTime != null) { - builder.timeField(FINISHED_TIME.getPreferredName(), FINISHED_TIME.getPreferredName() + humanReadableSuffix, - finishedTime.getTime()); + builder.timeField( + FINISHED_TIME.getPreferredName(), + FINISHED_TIME.getPreferredName() + humanReadableSuffix, + finishedTime.getTime() + ); } builder.field(ANALYSIS_CONFIG.getPreferredName(), analysisConfig, params); if (analysisLimits != null) { @@ -378,10 +403,28 @@ public boolean equals(Object other) { @Override public int hashCode() { - return Objects.hash(jobId, jobType, groups, description, createTime, finishedTime, - analysisConfig, analysisLimits, dataDescription, modelPlotConfig, renormalizationWindowDays, - backgroundPersistInterval, modelSnapshotRetentionDays, dailyModelSnapshotRetentionAfterDays, resultsRetentionDays, - customSettings, modelSnapshotId, resultsIndexName, deleting, allowLazyOpen); + return Objects.hash( + jobId, + jobType, + groups, + description, + createTime, + finishedTime, + analysisConfig, + analysisLimits, + dataDescription, + modelPlotConfig, + renormalizationWindowDays, + backgroundPersistInterval, + modelSnapshotRetentionDays, + dailyModelSnapshotRetentionAfterDays, + resultsRetentionDays, + customSettings, + modelSnapshotId, + resultsIndexName, + deleting, + allowLazyOpen + ); } @Override @@ -416,8 +459,7 @@ public static class Builder { private Boolean deleting; private Boolean allowLazyOpen; - private Builder() { - } + private Builder() {} public Builder(String id) { this.id = id; @@ -556,13 +598,30 @@ Builder setAllowLazyOpen(Boolean allowLazyOpen) { * @return The job */ public Job build() { - Objects.requireNonNull(id, "[" + ID.getPreferredName() + "] must not be null"); - Objects.requireNonNull(jobType, "[" + JOB_TYPE.getPreferredName() + "] must not be null"); + Objects.requireNonNull(id, "[" + ID.getPreferredName() + "] must not be null"); + Objects.requireNonNull(jobType, "[" + JOB_TYPE.getPreferredName() + "] must not be null"); return new Job( - id, jobType, groups, description, createTime, finishedTime, - analysisConfig, analysisLimits, dataDescription, modelPlotConfig, renormalizationWindowDays, - backgroundPersistInterval, modelSnapshotRetentionDays, dailyModelSnapshotRetentionAfterDays, resultsRetentionDays, - customSettings, modelSnapshotId, resultsIndexName, deleting, allowLazyOpen); + id, + jobType, + groups, + description, + createTime, + finishedTime, + analysisConfig, + analysisLimits, + dataDescription, + modelPlotConfig, + renormalizationWindowDays, + backgroundPersistInterval, + modelSnapshotRetentionDays, + dailyModelSnapshotRetentionAfterDays, + resultsRetentionDays, + customSettings, + modelSnapshotId, + resultsIndexName, + deleting, + allowLazyOpen + ); } } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/JobState.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/JobState.java index dd5a22006a7bb..1a248ef137d53 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/JobState.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/JobState.java @@ -16,7 +16,11 @@ */ public enum JobState { - CLOSING, CLOSED, OPENED, FAILED, OPENING; + CLOSING, + CLOSED, + OPENED, + FAILED, + OPENING; public static JobState fromString(String name) { return valueOf(name.trim().toUpperCase(Locale.ROOT)); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/JobUpdate.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/JobUpdate.java index 150a5a763c424..f0d70a2509a39 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/JobUpdate.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/JobUpdate.java @@ -8,10 +8,10 @@ package org.elasticsearch.client.ml.job.config; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -27,7 +27,10 @@ public class JobUpdate implements ToXContentObject { public static final ParseField DETECTORS = new ParseField("detectors"); public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "job_update", true, args -> new Builder((String) args[0])); + "job_update", + true, + args -> new Builder((String) args[0]) + ); static { PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), Job.ID); @@ -36,19 +39,30 @@ public class JobUpdate implements ToXContentObject { PARSER.declareObjectArray(Builder::setDetectorUpdates, DetectorUpdate.PARSER, DETECTORS); PARSER.declareObject(Builder::setModelPlotConfig, ModelPlotConfig.PARSER, Job.MODEL_PLOT_CONFIG); PARSER.declareObject(Builder::setAnalysisLimits, AnalysisLimits.PARSER, Job.ANALYSIS_LIMITS); - PARSER.declareString((builder, val) -> builder.setBackgroundPersistInterval( - TimeValue.parseTimeValue(val, Job.BACKGROUND_PERSIST_INTERVAL.getPreferredName())), Job.BACKGROUND_PERSIST_INTERVAL); + PARSER.declareString( + (builder, val) -> builder.setBackgroundPersistInterval( + TimeValue.parseTimeValue(val, Job.BACKGROUND_PERSIST_INTERVAL.getPreferredName()) + ), + Job.BACKGROUND_PERSIST_INTERVAL + ); PARSER.declareLong(Builder::setRenormalizationWindowDays, Job.RENORMALIZATION_WINDOW_DAYS); PARSER.declareLong(Builder::setResultsRetentionDays, Job.RESULTS_RETENTION_DAYS); PARSER.declareLong(Builder::setModelSnapshotRetentionDays, Job.MODEL_SNAPSHOT_RETENTION_DAYS); PARSER.declareLong(Builder::setDailyModelSnapshotRetentionAfterDays, Job.DAILY_MODEL_SNAPSHOT_RETENTION_AFTER_DAYS); PARSER.declareStringArray(Builder::setCategorizationFilters, AnalysisConfig.CATEGORIZATION_FILTERS); - PARSER.declareObject(Builder::setPerPartitionCategorizationConfig, PerPartitionCategorizationConfig.PARSER, - AnalysisConfig.PER_PARTITION_CATEGORIZATION); + PARSER.declareObject( + Builder::setPerPartitionCategorizationConfig, + PerPartitionCategorizationConfig.PARSER, + AnalysisConfig.PER_PARTITION_CATEGORIZATION + ); PARSER.declareField(Builder::setCustomSettings, (p, c) -> p.map(), Job.CUSTOM_SETTINGS, ObjectParser.ValueType.OBJECT); PARSER.declareBoolean(Builder::setAllowLazyOpen, Job.ALLOW_LAZY_OPEN); - PARSER.declareString((builder, val) -> builder.setModelPruneWindow( - TimeValue.parseTimeValue(val, AnalysisConfig.MODEL_PRUNE_WINDOW.getPreferredName())), AnalysisConfig.MODEL_PRUNE_WINDOW); + PARSER.declareString( + (builder, val) -> builder.setModelPruneWindow( + TimeValue.parseTimeValue(val, AnalysisConfig.MODEL_PRUNE_WINDOW.getPreferredName()) + ), + AnalysisConfig.MODEL_PRUNE_WINDOW + ); } private final String jobId; @@ -68,14 +82,24 @@ public class JobUpdate implements ToXContentObject { private final Boolean allowLazyOpen; private final TimeValue modelPruneWindow; - private JobUpdate(String jobId, @Nullable List groups, @Nullable String description, - @Nullable List detectorUpdates, @Nullable ModelPlotConfig modelPlotConfig, - @Nullable AnalysisLimits analysisLimits, @Nullable TimeValue backgroundPersistInterval, - @Nullable Long renormalizationWindowDays, @Nullable Long resultsRetentionDays, - @Nullable Long modelSnapshotRetentionDays, - @Nullable Long dailyModelSnapshotRetentionAfterDays, @Nullable List categorizationFilters, - @Nullable PerPartitionCategorizationConfig perPartitionCategorizationConfig, - @Nullable Map customSettings, @Nullable Boolean allowLazyOpen, @Nullable TimeValue modelPruneWindow) { + private JobUpdate( + String jobId, + @Nullable List groups, + @Nullable String description, + @Nullable List detectorUpdates, + @Nullable ModelPlotConfig modelPlotConfig, + @Nullable AnalysisLimits analysisLimits, + @Nullable TimeValue backgroundPersistInterval, + @Nullable Long renormalizationWindowDays, + @Nullable Long resultsRetentionDays, + @Nullable Long modelSnapshotRetentionDays, + @Nullable Long dailyModelSnapshotRetentionAfterDays, + @Nullable List categorizationFilters, + @Nullable PerPartitionCategorizationConfig perPartitionCategorizationConfig, + @Nullable Map customSettings, + @Nullable Boolean allowLazyOpen, + @Nullable TimeValue modelPruneWindow + ) { this.jobId = jobId; this.groups = groups; this.description = description; @@ -239,23 +263,42 @@ public boolean equals(Object other) { @Override public int hashCode() { - return Objects.hash(jobId, groups, description, detectorUpdates, modelPlotConfig, analysisLimits, renormalizationWindowDays, - backgroundPersistInterval, modelSnapshotRetentionDays, dailyModelSnapshotRetentionAfterDays, resultsRetentionDays, - categorizationFilters, perPartitionCategorizationConfig, customSettings, allowLazyOpen, - modelPruneWindow); + return Objects.hash( + jobId, + groups, + description, + detectorUpdates, + modelPlotConfig, + analysisLimits, + renormalizationWindowDays, + backgroundPersistInterval, + modelSnapshotRetentionDays, + dailyModelSnapshotRetentionAfterDays, + resultsRetentionDays, + categorizationFilters, + perPartitionCategorizationConfig, + customSettings, + allowLazyOpen, + modelPruneWindow + ); } public static class DetectorUpdate implements ToXContentObject { @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("detector_update", true, a -> new DetectorUpdate((int) a[0], (String) a[1], - (List) a[2])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "detector_update", + true, + a -> new DetectorUpdate((int) a[0], (String) a[1], (List) a[2]) + ); static { PARSER.declareInt(ConstructingObjectParser.optionalConstructorArg(), Detector.DETECTOR_INDEX); PARSER.declareStringOrNull(ConstructingObjectParser.optionalConstructorArg(), Job.DESCRIPTION); - PARSER.declareObjectArray(ConstructingObjectParser.optionalConstructorArg(), (parser, parseFieldMatcher) -> - DetectionRule.PARSER.apply(parser, parseFieldMatcher).build(), Detector.CUSTOM_RULES_FIELD); + PARSER.declareObjectArray( + ConstructingObjectParser.optionalConstructorArg(), + (parser, parseFieldMatcher) -> DetectionRule.PARSER.apply(parser, parseFieldMatcher).build(), + Detector.CUSTOM_RULES_FIELD + ); } private final int detectorIndex; @@ -319,7 +362,8 @@ public boolean equals(Object other) { } DetectorUpdate that = (DetectorUpdate) other; - return this.detectorIndex == that.detectorIndex && Objects.equals(this.description, that.description) + return this.detectorIndex == that.detectorIndex + && Objects.equals(this.description, that.description) && Objects.equals(this.rules, that.rules); } } @@ -521,10 +565,24 @@ public Builder setModelPruneWindow(TimeValue modelPruneWindow) { } public JobUpdate build() { - return new JobUpdate(jobId, groups, description, detectorUpdates, modelPlotConfig, analysisLimits, backgroundPersistInterval, - renormalizationWindowDays, resultsRetentionDays, modelSnapshotRetentionDays, - dailyModelSnapshotRetentionAfterDays, categorizationFilters, perPartitionCategorizationConfig, customSettings, - allowLazyOpen, modelPruneWindow); + return new JobUpdate( + jobId, + groups, + description, + detectorUpdates, + modelPlotConfig, + analysisLimits, + backgroundPersistInterval, + renormalizationWindowDays, + resultsRetentionDays, + modelSnapshotRetentionDays, + dailyModelSnapshotRetentionAfterDays, + categorizationFilters, + perPartitionCategorizationConfig, + customSettings, + allowLazyOpen, + modelPruneWindow + ); } } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/MlFilter.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/MlFilter.java index 0e85464fbbe1b..7079ff69ea106 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/MlFilter.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/MlFilter.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml.job.config; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -116,8 +116,7 @@ public static class Builder { private String description; private SortedSet items = new TreeSet<>(); - private Builder() { - } + private Builder() {} /** * Set the ID of the filter diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/ModelPlotConfig.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/ModelPlotConfig.java index 5d6de1b389606..4581409d64989 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/ModelPlotConfig.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/ModelPlotConfig.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml.job.config; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -22,9 +22,11 @@ public class ModelPlotConfig implements ToXContentObject { private static final ParseField TERMS_FIELD = new ParseField("terms"); private static final ParseField ANNOTATIONS_ENABLED_FIELD = new ParseField("annotations_enabled"); - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>( - TYPE_FIELD.getPreferredName(), true, a -> new ModelPlotConfig((boolean) a[0], (String) a[1], (Boolean) a[2])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + TYPE_FIELD.getPreferredName(), + true, + a -> new ModelPlotConfig((boolean) a[0], (String) a[1], (Boolean) a[2]) + ); static { PARSER.declareBoolean(ConstructingObjectParser.constructorArg(), ENABLED_FIELD); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/PerPartitionCategorizationConfig.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/PerPartitionCategorizationConfig.java index 27f7dfb2d8134..86cd40f45c601 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/PerPartitionCategorizationConfig.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/PerPartitionCategorizationConfig.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml.job.config; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -23,9 +23,11 @@ public class PerPartitionCategorizationConfig implements ToXContentObject { public static final ParseField ENABLED_FIELD = new ParseField("enabled"); public static final ParseField STOP_ON_WARN = new ParseField("stop_on_warn"); - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(TYPE_FIELD.getPreferredName(), true, - a -> new PerPartitionCategorizationConfig((boolean) a[0], (Boolean) a[1])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + TYPE_FIELD.getPreferredName(), + true, + a -> new PerPartitionCategorizationConfig((boolean) a[0], (Boolean) a[1]) + ); static { PARSER.declareBoolean(ConstructingObjectParser.constructorArg(), ENABLED_FIELD); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/RuleCondition.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/RuleCondition.java index ac68bc388d98b..d7f9054c23485 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/RuleCondition.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/RuleCondition.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml.job.config; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -23,9 +23,11 @@ public class RuleCondition implements ToXContentObject { public static final ParseField APPLIES_TO_FIELD = new ParseField("applies_to"); public static final ParseField VALUE_FIELD = new ParseField("value"); - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(RULE_CONDITION_FIELD.getPreferredName(), true, - a -> new RuleCondition((AppliesTo) a[0], (Operator) a[1], (double) a[2])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + RULE_CONDITION_FIELD.getPreferredName(), + true, + a -> new RuleCondition((AppliesTo) a[0], (Operator) a[1], (double) a[2]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), AppliesTo::fromString, APPLIES_TO_FIELD); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/RuleScope.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/RuleScope.java index d8f812276ed3c..352d240943a9c 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/RuleScope.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/RuleScope.java @@ -38,8 +38,10 @@ public static ContextParser parser() { @SuppressWarnings("unchecked") Map value = (Map) entry.getValue(); builder.map(value); - try (XContentParser scopeParser = XContentFactory.xContent(builder.contentType()).createParser( - NamedXContentRegistry.EMPTY, DeprecationHandler.IGNORE_DEPRECATIONS, Strings.toString(builder))) { + try ( + XContentParser scopeParser = XContentFactory.xContent(builder.contentType()) + .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.IGNORE_DEPRECATIONS, Strings.toString(builder)) + ) { scope.put(entry.getKey(), FilterRef.PARSER.parse(scopeParser, null)); } } @@ -98,8 +100,7 @@ public static class Builder { private Map scope = new HashMap<>(); - public Builder() { - } + public Builder() {} public Builder(RuleScope otherScope) { scope = new HashMap<>(otherScope.scope); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/process/DataCounts.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/process/DataCounts.java index 61c185f6a6351..20cc5db284302 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/process/DataCounts.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/process/DataCounts.java @@ -9,9 +9,9 @@ import org.elasticsearch.client.common.TimeUtil; import org.elasticsearch.client.ml.job.config.Job; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -52,10 +52,29 @@ public class DataCounts implements ToXContentObject { public static final ParseField LATEST_SPARSE_BUCKET_TIME = new ParseField("latest_sparse_bucket_timestamp"); public static final ParseField LOG_TIME = new ParseField("log_time"); - public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("data_counts", true, - a -> new DataCounts((String) a[0], (long) a[1], (long) a[2], (long) a[3], (long) a[4], (long) a[5], (long) a[6], - (long) a[7], (long) a[8], (long) a[9], (long) a[10], (Date) a[11], (Date) a[12], (Date) a[13], (Date) a[14], - (Date) a[15], (Instant) a[16])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "data_counts", + true, + a -> new DataCounts( + (String) a[0], + (long) a[1], + (long) a[2], + (long) a[3], + (long) a[4], + (long) a[5], + (long) a[6], + (long) a[7], + (long) a[8], + (long) a[9], + (long) a[10], + (Date) a[11], + (Date) a[12], + (Date) a[13], + (Date) a[14], + (Date) a[15], + (Instant) a[16] + ) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), Job.ID); @@ -69,30 +88,42 @@ public class DataCounts implements ToXContentObject { PARSER.declareLong(ConstructingObjectParser.constructorArg(), EMPTY_BUCKET_COUNT); PARSER.declareLong(ConstructingObjectParser.constructorArg(), SPARSE_BUCKET_COUNT); PARSER.declareLong(ConstructingObjectParser.constructorArg(), BUCKET_COUNT); - PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(), + PARSER.declareField( + ConstructingObjectParser.optionalConstructorArg(), (p) -> TimeUtil.parseTimeField(p, EARLIEST_RECORD_TIME.getPreferredName()), EARLIEST_RECORD_TIME, - ValueType.VALUE); - PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(), + ValueType.VALUE + ); + PARSER.declareField( + ConstructingObjectParser.optionalConstructorArg(), (p) -> TimeUtil.parseTimeField(p, LATEST_RECORD_TIME.getPreferredName()), LATEST_RECORD_TIME, - ValueType.VALUE); - PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(), + ValueType.VALUE + ); + PARSER.declareField( + ConstructingObjectParser.optionalConstructorArg(), (p) -> TimeUtil.parseTimeField(p, LAST_DATA_TIME.getPreferredName()), LAST_DATA_TIME, - ValueType.VALUE); - PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(), + ValueType.VALUE + ); + PARSER.declareField( + ConstructingObjectParser.optionalConstructorArg(), (p) -> TimeUtil.parseTimeField(p, LATEST_EMPTY_BUCKET_TIME.getPreferredName()), LATEST_EMPTY_BUCKET_TIME, - ValueType.VALUE); - PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(), + ValueType.VALUE + ); + PARSER.declareField( + ConstructingObjectParser.optionalConstructorArg(), (p) -> TimeUtil.parseTimeField(p, LATEST_SPARSE_BUCKET_TIME.getPreferredName()), LATEST_SPARSE_BUCKET_TIME, - ValueType.VALUE); - PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(), + ValueType.VALUE + ); + PARSER.declareField( + ConstructingObjectParser.optionalConstructorArg(), p -> TimeUtil.parseTimeFieldToInstant(p, LOG_TIME.getPreferredName()), LOG_TIME, - ValueType.VALUE); + ValueType.VALUE + ); } private final String jobId; @@ -113,11 +144,25 @@ public class DataCounts implements ToXContentObject { private Date latestSparseBucketTimeStamp; private Instant logTime; - public DataCounts(String jobId, long processedRecordCount, long processedFieldCount, long inputBytes, - long inputFieldCount, long invalidDateCount, long missingFieldCount, long outOfOrderTimeStampCount, - long emptyBucketCount, long sparseBucketCount, long bucketCount, - Date earliestRecordTimeStamp, Date latestRecordTimeStamp, Date lastDataTimeStamp, - Date latestEmptyBucketTimeStamp, Date latestSparseBucketTimeStamp, Instant logTime) { + public DataCounts( + String jobId, + long processedRecordCount, + long processedFieldCount, + long inputBytes, + long inputFieldCount, + long invalidDateCount, + long missingFieldCount, + long outOfOrderTimeStampCount, + long emptyBucketCount, + long sparseBucketCount, + long bucketCount, + Date earliestRecordTimeStamp, + Date latestRecordTimeStamp, + Date lastDataTimeStamp, + Date latestEmptyBucketTimeStamp, + Date latestSparseBucketTimeStamp, + Instant logTime + ) { this.jobId = jobId; this.processedRecordCount = processedRecordCount; this.processedFieldCount = processedFieldCount; @@ -178,8 +223,7 @@ public long getProcessedFieldCount() { * @return Total number of input records read {@code long} */ public long getInputRecordCount() { - return processedRecordCount + outOfOrderTimeStampCount - + invalidDateCount; + return processedRecordCount + outOfOrderTimeStampCount + invalidDateCount; } /** @@ -334,24 +378,39 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.field(SPARSE_BUCKET_COUNT.getPreferredName(), sparseBucketCount); builder.field(BUCKET_COUNT.getPreferredName(), bucketCount); if (earliestRecordTimeStamp != null) { - builder.timeField(EARLIEST_RECORD_TIME.getPreferredName(), EARLIEST_RECORD_TIME.getPreferredName() + "_string", - earliestRecordTimeStamp.getTime()); + builder.timeField( + EARLIEST_RECORD_TIME.getPreferredName(), + EARLIEST_RECORD_TIME.getPreferredName() + "_string", + earliestRecordTimeStamp.getTime() + ); } if (latestRecordTimeStamp != null) { - builder.timeField(LATEST_RECORD_TIME.getPreferredName(), LATEST_RECORD_TIME.getPreferredName() + "_string", - latestRecordTimeStamp.getTime()); + builder.timeField( + LATEST_RECORD_TIME.getPreferredName(), + LATEST_RECORD_TIME.getPreferredName() + "_string", + latestRecordTimeStamp.getTime() + ); } if (lastDataTimeStamp != null) { - builder.timeField(LAST_DATA_TIME.getPreferredName(), LAST_DATA_TIME.getPreferredName() + "_string", - lastDataTimeStamp.getTime()); + builder.timeField( + LAST_DATA_TIME.getPreferredName(), + LAST_DATA_TIME.getPreferredName() + "_string", + lastDataTimeStamp.getTime() + ); } if (latestEmptyBucketTimeStamp != null) { - builder.timeField(LATEST_EMPTY_BUCKET_TIME.getPreferredName(), LATEST_EMPTY_BUCKET_TIME.getPreferredName() + "_string", - latestEmptyBucketTimeStamp.getTime()); + builder.timeField( + LATEST_EMPTY_BUCKET_TIME.getPreferredName(), + LATEST_EMPTY_BUCKET_TIME.getPreferredName() + "_string", + latestEmptyBucketTimeStamp.getTime() + ); } if (latestSparseBucketTimeStamp != null) { - builder.timeField(LATEST_SPARSE_BUCKET_TIME.getPreferredName(), LATEST_SPARSE_BUCKET_TIME.getPreferredName() + "_string", - latestSparseBucketTimeStamp.getTime()); + builder.timeField( + LATEST_SPARSE_BUCKET_TIME.getPreferredName(), + LATEST_SPARSE_BUCKET_TIME.getPreferredName() + "_string", + latestSparseBucketTimeStamp.getTime() + ); } builder.field(INPUT_RECORD_COUNT.getPreferredName(), getInputRecordCount()); if (logTime != null) { @@ -377,30 +436,45 @@ public boolean equals(Object other) { DataCounts that = (DataCounts) other; - return Objects.equals(this.jobId, that.jobId) && - this.processedRecordCount == that.processedRecordCount && - this.processedFieldCount == that.processedFieldCount && - this.inputBytes == that.inputBytes && - this.inputFieldCount == that.inputFieldCount && - this.invalidDateCount == that.invalidDateCount && - this.missingFieldCount == that.missingFieldCount && - this.outOfOrderTimeStampCount == that.outOfOrderTimeStampCount && - this.emptyBucketCount == that.emptyBucketCount && - this.sparseBucketCount == that.sparseBucketCount && - this.bucketCount == that.bucketCount && - Objects.equals(this.latestRecordTimeStamp, that.latestRecordTimeStamp) && - Objects.equals(this.earliestRecordTimeStamp, that.earliestRecordTimeStamp) && - Objects.equals(this.lastDataTimeStamp, that.lastDataTimeStamp) && - Objects.equals(this.latestEmptyBucketTimeStamp, that.latestEmptyBucketTimeStamp) && - Objects.equals(this.latestSparseBucketTimeStamp, that.latestSparseBucketTimeStamp) && - Objects.equals(this.logTime, that.logTime); + return Objects.equals(this.jobId, that.jobId) + && this.processedRecordCount == that.processedRecordCount + && this.processedFieldCount == that.processedFieldCount + && this.inputBytes == that.inputBytes + && this.inputFieldCount == that.inputFieldCount + && this.invalidDateCount == that.invalidDateCount + && this.missingFieldCount == that.missingFieldCount + && this.outOfOrderTimeStampCount == that.outOfOrderTimeStampCount + && this.emptyBucketCount == that.emptyBucketCount + && this.sparseBucketCount == that.sparseBucketCount + && this.bucketCount == that.bucketCount + && Objects.equals(this.latestRecordTimeStamp, that.latestRecordTimeStamp) + && Objects.equals(this.earliestRecordTimeStamp, that.earliestRecordTimeStamp) + && Objects.equals(this.lastDataTimeStamp, that.lastDataTimeStamp) + && Objects.equals(this.latestEmptyBucketTimeStamp, that.latestEmptyBucketTimeStamp) + && Objects.equals(this.latestSparseBucketTimeStamp, that.latestSparseBucketTimeStamp) + && Objects.equals(this.logTime, that.logTime); } @Override public int hashCode() { - return Objects.hash(jobId, processedRecordCount, processedFieldCount, - inputBytes, inputFieldCount, invalidDateCount, missingFieldCount, - outOfOrderTimeStampCount, lastDataTimeStamp, emptyBucketCount, sparseBucketCount, bucketCount, - latestRecordTimeStamp, earliestRecordTimeStamp, latestEmptyBucketTimeStamp, latestSparseBucketTimeStamp, logTime); + return Objects.hash( + jobId, + processedRecordCount, + processedFieldCount, + inputBytes, + inputFieldCount, + invalidDateCount, + missingFieldCount, + outOfOrderTimeStampCount, + lastDataTimeStamp, + emptyBucketCount, + sparseBucketCount, + bucketCount, + latestRecordTimeStamp, + earliestRecordTimeStamp, + latestEmptyBucketTimeStamp, + latestSparseBucketTimeStamp, + logTime + ); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/process/ModelSizeStats.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/process/ModelSizeStats.java index b8cbac253d0ac..db3a3fa011738 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/process/ModelSizeStats.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/process/ModelSizeStats.java @@ -11,9 +11,9 @@ import org.elasticsearch.client.ml.job.config.Job; import org.elasticsearch.client.ml.job.results.Result; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -56,8 +56,11 @@ public class ModelSizeStats implements ToXContentObject { public static final ParseField LOG_TIME_FIELD = new ParseField("log_time"); public static final ParseField TIMESTAMP_FIELD = new ParseField("timestamp"); - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(RESULT_TYPE_VALUE, true, a -> new Builder((String) a[0])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + RESULT_TYPE_VALUE, + true, + a -> new Builder((String) a[0]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), Job.ID); @@ -70,24 +73,36 @@ public class ModelSizeStats implements ToXContentObject { PARSER.declareLong(Builder::setTotalOverFieldCount, TOTAL_OVER_FIELD_COUNT_FIELD); PARSER.declareLong(Builder::setTotalPartitionFieldCount, TOTAL_PARTITION_FIELD_COUNT_FIELD); PARSER.declareField(Builder::setMemoryStatus, p -> MemoryStatus.fromString(p.text()), MEMORY_STATUS_FIELD, ValueType.STRING); - PARSER.declareField(Builder::setAssignmentMemoryBasis, - p -> AssignmentMemoryBasis.fromString(p.text()), ASSIGNMENT_MEMORY_BASIS_FIELD, ValueType.STRING); + PARSER.declareField( + Builder::setAssignmentMemoryBasis, + p -> AssignmentMemoryBasis.fromString(p.text()), + ASSIGNMENT_MEMORY_BASIS_FIELD, + ValueType.STRING + ); PARSER.declareLong(Builder::setCategorizedDocCount, CATEGORIZED_DOC_COUNT_FIELD); PARSER.declareLong(Builder::setTotalCategoryCount, TOTAL_CATEGORY_COUNT_FIELD); PARSER.declareLong(Builder::setFrequentCategoryCount, FREQUENT_CATEGORY_COUNT_FIELD); PARSER.declareLong(Builder::setRareCategoryCount, RARE_CATEGORY_COUNT_FIELD); PARSER.declareLong(Builder::setDeadCategoryCount, DEAD_CATEGORY_COUNT_FIELD); PARSER.declareLong(Builder::setFailedCategoryCount, FAILED_CATEGORY_COUNT_FIELD); - PARSER.declareField(Builder::setCategorizationStatus, - p -> CategorizationStatus.fromString(p.text()), CATEGORIZATION_STATUS_FIELD, ValueType.STRING); - PARSER.declareField(Builder::setLogTime, + PARSER.declareField( + Builder::setCategorizationStatus, + p -> CategorizationStatus.fromString(p.text()), + CATEGORIZATION_STATUS_FIELD, + ValueType.STRING + ); + PARSER.declareField( + Builder::setLogTime, (p) -> TimeUtil.parseTimeField(p, LOG_TIME_FIELD.getPreferredName()), LOG_TIME_FIELD, - ValueType.VALUE); - PARSER.declareField(Builder::setTimestamp, + ValueType.VALUE + ); + PARSER.declareField( + Builder::setTimestamp, (p) -> TimeUtil.parseTimeField(p, TIMESTAMP_FIELD.getPreferredName()), TIMESTAMP_FIELD, - ValueType.VALUE); + ValueType.VALUE + ); } /** @@ -97,7 +112,9 @@ public class ModelSizeStats implements ToXContentObject { * been dropped */ public enum MemoryStatus { - OK, SOFT_LIMIT, HARD_LIMIT; + OK, + SOFT_LIMIT, + HARD_LIMIT; public static MemoryStatus fromString(String statusName) { return valueOf(statusName.trim().toUpperCase(Locale.ROOT)); @@ -120,7 +137,9 @@ public String toString() { * to 7.11. */ public enum AssignmentMemoryBasis { - MODEL_MEMORY_LIMIT, CURRENT_MODEL_BYTES, PEAK_MODEL_BYTES; + MODEL_MEMORY_LIMIT, + CURRENT_MODEL_BYTES, + PEAK_MODEL_BYTES; public static AssignmentMemoryBasis fromString(String statusName) { return valueOf(statusName.trim().toUpperCase(Locale.ROOT)); @@ -137,7 +156,8 @@ public String toString() { * means that inappropriate numbers of categories are being found */ public enum CategorizationStatus { - OK, WARN; + OK, + WARN; public static CategorizationStatus fromString(String statusName) { return valueOf(statusName.trim().toUpperCase(Locale.ROOT)); @@ -170,12 +190,28 @@ public String toString() { private final Date timestamp; private final Date logTime; - private ModelSizeStats(String jobId, long modelBytes, Long peakModelBytes, Long modelBytesExceeded, Long modelBytesMemoryLimit, - long totalByFieldCount, long totalOverFieldCount, long totalPartitionFieldCount, - long bucketAllocationFailuresCount, MemoryStatus memoryStatus, - AssignmentMemoryBasis assignmentMemoryBasis, long categorizedDocCount, long totalCategoryCount, - long frequentCategoryCount, long rareCategoryCount, long deadCategoryCount, long failedCategoryCount, - CategorizationStatus categorizationStatus, Date timestamp, Date logTime) { + private ModelSizeStats( + String jobId, + long modelBytes, + Long peakModelBytes, + Long modelBytesExceeded, + Long modelBytesMemoryLimit, + long totalByFieldCount, + long totalOverFieldCount, + long totalPartitionFieldCount, + long bucketAllocationFailuresCount, + MemoryStatus memoryStatus, + AssignmentMemoryBasis assignmentMemoryBasis, + long categorizedDocCount, + long totalCategoryCount, + long frequentCategoryCount, + long rareCategoryCount, + long deadCategoryCount, + long failedCategoryCount, + CategorizationStatus categorizationStatus, + Date timestamp, + Date logTime + ) { this.jobId = jobId; this.modelBytes = modelBytes; this.peakModelBytes = peakModelBytes; @@ -332,10 +368,27 @@ public Date getLogTime() { @Override public int hashCode() { return Objects.hash( - jobId, modelBytes, peakModelBytes, modelBytesExceeded, modelBytesMemoryLimit, totalByFieldCount, totalOverFieldCount, - totalPartitionFieldCount, this.bucketAllocationFailuresCount, memoryStatus, assignmentMemoryBasis, categorizedDocCount, - totalCategoryCount, frequentCategoryCount, rareCategoryCount, deadCategoryCount, failedCategoryCount, categorizationStatus, - timestamp, logTime); + jobId, + modelBytes, + peakModelBytes, + modelBytesExceeded, + modelBytesMemoryLimit, + totalByFieldCount, + totalOverFieldCount, + totalPartitionFieldCount, + this.bucketAllocationFailuresCount, + memoryStatus, + assignmentMemoryBasis, + categorizedDocCount, + totalCategoryCount, + frequentCategoryCount, + rareCategoryCount, + deadCategoryCount, + failedCategoryCount, + categorizationStatus, + timestamp, + logTime + ); } /** @@ -356,8 +409,10 @@ public boolean equals(Object other) { return this.modelBytes == that.modelBytes && Objects.equals(this.peakModelBytes, that.peakModelBytes) && Objects.equals(this.modelBytesExceeded, that.modelBytesExceeded) - && Objects.equals(this.modelBytesMemoryLimit, that.modelBytesMemoryLimit) && this.totalByFieldCount == that.totalByFieldCount - && this.totalOverFieldCount == that.totalOverFieldCount && this.totalPartitionFieldCount == that.totalPartitionFieldCount + && Objects.equals(this.modelBytesMemoryLimit, that.modelBytesMemoryLimit) + && this.totalByFieldCount == that.totalByFieldCount + && this.totalOverFieldCount == that.totalOverFieldCount + && this.totalPartitionFieldCount == that.totalPartitionFieldCount && this.bucketAllocationFailuresCount == that.bucketAllocationFailuresCount && Objects.equals(this.memoryStatus, that.memoryStatus) && Objects.equals(this.assignmentMemoryBasis, that.assignmentMemoryBasis) @@ -525,10 +580,27 @@ public Builder setLogTime(Date logTime) { public ModelSizeStats build() { return new ModelSizeStats( - jobId, modelBytes, peakModelBytes, modelBytesExceeded, modelBytesMemoryLimit, totalByFieldCount, totalOverFieldCount, - totalPartitionFieldCount, bucketAllocationFailuresCount, memoryStatus, assignmentMemoryBasis, categorizedDocCount, - totalCategoryCount, frequentCategoryCount, rareCategoryCount, deadCategoryCount, failedCategoryCount, categorizationStatus, - timestamp, logTime); + jobId, + modelBytes, + peakModelBytes, + modelBytesExceeded, + modelBytesMemoryLimit, + totalByFieldCount, + totalOverFieldCount, + totalPartitionFieldCount, + bucketAllocationFailuresCount, + memoryStatus, + assignmentMemoryBasis, + categorizedDocCount, + totalCategoryCount, + frequentCategoryCount, + rareCategoryCount, + deadCategoryCount, + failedCategoryCount, + categorizationStatus, + timestamp, + logTime + ); } } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/process/ModelSnapshot.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/process/ModelSnapshot.java index 23ec2c753d09d..e21b8f512a143 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/process/ModelSnapshot.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/process/ModelSnapshot.java @@ -10,9 +10,9 @@ import org.elasticsearch.Version; import org.elasticsearch.client.common.TimeUtil; import org.elasticsearch.client.ml.job.config.Job; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ObjectParser; import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -42,28 +42,32 @@ public class ModelSnapshot implements ToXContentObject { static { PARSER.declareString(Builder::setJobId, Job.ID); PARSER.declareString(Builder::setMinVersion, MIN_VERSION); - PARSER.declareField(Builder::setTimestamp, + PARSER.declareField( + Builder::setTimestamp, (p) -> TimeUtil.parseTimeField(p, TIMESTAMP.getPreferredName()), TIMESTAMP, - ValueType.VALUE); + ValueType.VALUE + ); PARSER.declareString(Builder::setDescription, DESCRIPTION); PARSER.declareString(Builder::setSnapshotId, SNAPSHOT_ID); PARSER.declareInt(Builder::setSnapshotDocCount, SNAPSHOT_DOC_COUNT); - PARSER.declareObject(Builder::setModelSizeStats, ModelSizeStats.PARSER, - ModelSizeStats.RESULT_TYPE_FIELD); - PARSER.declareField(Builder::setLatestRecordTimeStamp, + PARSER.declareObject(Builder::setModelSizeStats, ModelSizeStats.PARSER, ModelSizeStats.RESULT_TYPE_FIELD); + PARSER.declareField( + Builder::setLatestRecordTimeStamp, (p) -> TimeUtil.parseTimeField(p, LATEST_RECORD_TIME.getPreferredName()), LATEST_RECORD_TIME, - ValueType.VALUE); - PARSER.declareField(Builder::setLatestResultTimeStamp, + ValueType.VALUE + ); + PARSER.declareField( + Builder::setLatestResultTimeStamp, (p) -> TimeUtil.parseTimeField(p, LATEST_RESULT_TIME.getPreferredName()), LATEST_RESULT_TIME, - ValueType.VALUE); + ValueType.VALUE + ); PARSER.declareObject(Builder::setQuantiles, Quantiles.PARSER, QUANTILES); PARSER.declareBoolean(Builder::setRetain, RETAIN); } - private final String jobId; /** @@ -82,10 +86,19 @@ public class ModelSnapshot implements ToXContentObject { private final Quantiles quantiles; private final boolean retain; - - private ModelSnapshot(String jobId, Version minVersion, Date timestamp, String description, String snapshotId, int snapshotDocCount, - ModelSizeStats modelSizeStats, Date latestRecordTimeStamp, Date latestResultTimeStamp, Quantiles quantiles, - boolean retain) { + private ModelSnapshot( + String jobId, + Version minVersion, + Date timestamp, + String description, + String snapshotId, + int snapshotDocCount, + ModelSizeStats modelSizeStats, + Date latestRecordTimeStamp, + Date latestResultTimeStamp, + Quantiles quantiles, + boolean retain + ) { this.jobId = jobId; this.minVersion = minVersion; this.timestamp = timestamp; @@ -118,12 +131,18 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.field(ModelSizeStats.RESULT_TYPE_FIELD.getPreferredName(), modelSizeStats); } if (latestRecordTimeStamp != null) { - builder.timeField(LATEST_RECORD_TIME.getPreferredName(), LATEST_RECORD_TIME.getPreferredName() + "_string", - latestRecordTimeStamp.getTime()); + builder.timeField( + LATEST_RECORD_TIME.getPreferredName(), + LATEST_RECORD_TIME.getPreferredName() + "_string", + latestRecordTimeStamp.getTime() + ); } if (latestResultTimeStamp != null) { - builder.timeField(LATEST_RESULT_TIME.getPreferredName(), LATEST_RESULT_TIME.getPreferredName() + "_string", - latestResultTimeStamp.getTime()); + builder.timeField( + LATEST_RESULT_TIME.getPreferredName(), + LATEST_RESULT_TIME.getPreferredName() + "_string", + latestResultTimeStamp.getTime() + ); } if (quantiles != null) { builder.field(QUANTILES.getPreferredName(), quantiles); @@ -179,8 +198,19 @@ public Date getLatestResultTimeStamp() { @Override public int hashCode() { - return Objects.hash(jobId, minVersion, timestamp, description, snapshotId, quantiles, snapshotDocCount, modelSizeStats, - latestRecordTimeStamp, latestResultTimeStamp, retain); + return Objects.hash( + jobId, + minVersion, + timestamp, + description, + snapshotId, + quantiles, + snapshotDocCount, + modelSizeStats, + latestRecordTimeStamp, + latestResultTimeStamp, + retain + ); } /** @@ -227,9 +257,7 @@ public static class Builder { private Quantiles quantiles; private boolean retain; - - public Builder() { - } + public Builder() {} public Builder(String jobId) { this.jobId = jobId; @@ -315,8 +343,19 @@ public Builder setRetain(boolean value) { } public ModelSnapshot build() { - return new ModelSnapshot(jobId, minVersion, timestamp, description, snapshotId, snapshotDocCount, modelSizeStats, - latestRecordTimeStamp, latestResultTimeStamp, quantiles, retain); + return new ModelSnapshot( + jobId, + minVersion, + timestamp, + description, + snapshotId, + snapshotDocCount, + modelSizeStats, + latestRecordTimeStamp, + latestResultTimeStamp, + quantiles, + retain + ); } } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/process/Quantiles.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/process/Quantiles.java index fa818653076f2..968447bcfa4dd 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/process/Quantiles.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/process/Quantiles.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.ml.job.process; import org.elasticsearch.client.ml.job.config.Job; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -29,8 +29,11 @@ public class Quantiles implements ToXContentObject { public static final ParseField TIMESTAMP = new ParseField("timestamp"); public static final ParseField QUANTILE_STATE = new ParseField("quantile_state"); - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("quantiles", true, a -> new Quantiles((String) a[0], (Date) a[1], (String) a[2])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "quantiles", + true, + a -> new Quantiles((String) a[0], (Date) a[1], (String) a[2]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), Job.ID); @@ -94,8 +97,8 @@ public boolean equals(Object other) { Quantiles that = (Quantiles) other; - return Objects.equals(this.jobId, that.jobId) && Objects.equals(this.timestamp, that.timestamp) + return Objects.equals(this.jobId, that.jobId) + && Objects.equals(this.timestamp, that.timestamp) && Objects.equals(this.quantileState, that.quantileState); } } - diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/process/TimingStats.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/process/TimingStats.java index 2f45aeadb4db0..60ed9252affde 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/process/TimingStats.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/process/TimingStats.java @@ -8,10 +8,10 @@ package org.elasticsearch.client.ml.job.process; import org.elasticsearch.client.ml.job.config.Job; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -32,34 +32,33 @@ public class TimingStats implements ToXContentObject { public static final ParseField MIN_BUCKET_PROCESSING_TIME_MS = new ParseField("minimum_bucket_processing_time_ms"); public static final ParseField MAX_BUCKET_PROCESSING_TIME_MS = new ParseField("maximum_bucket_processing_time_ms"); public static final ParseField AVG_BUCKET_PROCESSING_TIME_MS = new ParseField("average_bucket_processing_time_ms"); - public static final ParseField EXPONENTIAL_AVG_BUCKET_PROCESSING_TIME_MS = - new ParseField("exponential_average_bucket_processing_time_ms"); - public static final ParseField EXPONENTIAL_AVG_BUCKET_PROCESSING_TIME_PER_HOUR_MS = - new ParseField("exponential_average_bucket_processing_time_per_hour_ms"); - - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>( - "timing_stats", - true, - args -> { - String jobId = (String) args[0]; - Long bucketCount = (Long) args[1]; - Double totalBucketProcessingTimeMs = (Double) args[2]; - Double minBucketProcessingTimeMs = (Double) args[3]; - Double maxBucketProcessingTimeMs = (Double) args[4]; - Double avgBucketProcessingTimeMs = (Double) args[5]; - Double exponentialAvgBucketProcessingTimeMs = (Double) args[6]; - Double exponentialAvgBucketProcessingTimePerHourMs = (Double) args[7]; - return new TimingStats( - jobId, - getOrDefault(bucketCount, 0L), - getOrDefault(totalBucketProcessingTimeMs, 0.0), - minBucketProcessingTimeMs, - maxBucketProcessingTimeMs, - avgBucketProcessingTimeMs, - exponentialAvgBucketProcessingTimeMs, - exponentialAvgBucketProcessingTimePerHourMs); - }); + public static final ParseField EXPONENTIAL_AVG_BUCKET_PROCESSING_TIME_MS = new ParseField( + "exponential_average_bucket_processing_time_ms" + ); + public static final ParseField EXPONENTIAL_AVG_BUCKET_PROCESSING_TIME_PER_HOUR_MS = new ParseField( + "exponential_average_bucket_processing_time_per_hour_ms" + ); + + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("timing_stats", true, args -> { + String jobId = (String) args[0]; + Long bucketCount = (Long) args[1]; + Double totalBucketProcessingTimeMs = (Double) args[2]; + Double minBucketProcessingTimeMs = (Double) args[3]; + Double maxBucketProcessingTimeMs = (Double) args[4]; + Double avgBucketProcessingTimeMs = (Double) args[5]; + Double exponentialAvgBucketProcessingTimeMs = (Double) args[6]; + Double exponentialAvgBucketProcessingTimePerHourMs = (Double) args[7]; + return new TimingStats( + jobId, + getOrDefault(bucketCount, 0L), + getOrDefault(totalBucketProcessingTimeMs, 0.0), + minBucketProcessingTimeMs, + maxBucketProcessingTimeMs, + avgBucketProcessingTimeMs, + exponentialAvgBucketProcessingTimeMs, + exponentialAvgBucketProcessingTimePerHourMs + ); + }); static { PARSER.declareString(constructorArg(), Job.ID); @@ -82,14 +81,15 @@ public class TimingStats implements ToXContentObject { private Double exponentialAvgBucketProcessingTimePerHourMs; public TimingStats( - String jobId, - long bucketCount, - double totalBucketProcessingTimeMs, - @Nullable Double minBucketProcessingTimeMs, - @Nullable Double maxBucketProcessingTimeMs, - @Nullable Double avgBucketProcessingTimeMs, - @Nullable Double exponentialAvgBucketProcessingTimeMs, - @Nullable Double exponentialAvgBucketProcessingTimePerHourMs) { + String jobId, + long bucketCount, + double totalBucketProcessingTimeMs, + @Nullable Double minBucketProcessingTimeMs, + @Nullable Double maxBucketProcessingTimeMs, + @Nullable Double avgBucketProcessingTimeMs, + @Nullable Double exponentialAvgBucketProcessingTimeMs, + @Nullable Double exponentialAvgBucketProcessingTimePerHourMs + ) { this.jobId = jobId; this.bucketCount = bucketCount; this.totalBucketProcessingTimeMs = totalBucketProcessingTimeMs; @@ -152,7 +152,9 @@ public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params par } if (exponentialAvgBucketProcessingTimePerHourMs != null) { builder.field( - EXPONENTIAL_AVG_BUCKET_PROCESSING_TIME_PER_HOUR_MS.getPreferredName(), exponentialAvgBucketProcessingTimePerHourMs); + EXPONENTIAL_AVG_BUCKET_PROCESSING_TIME_PER_HOUR_MS.getPreferredName(), + exponentialAvgBucketProcessingTimePerHourMs + ); } builder.endObject(); return builder; @@ -183,7 +185,8 @@ public int hashCode() { maxBucketProcessingTimeMs, avgBucketProcessingTimeMs, exponentialAvgBucketProcessingTimeMs, - exponentialAvgBucketProcessingTimePerHourMs); + exponentialAvgBucketProcessingTimePerHourMs + ); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/results/AnomalyCause.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/results/AnomalyCause.java index e10bff962bd99..5be75c52b19a6 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/results/AnomalyCause.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/results/AnomalyCause.java @@ -8,10 +8,10 @@ package org.elasticsearch.client.ml.job.results; import org.elasticsearch.client.ml.job.config.DetectorFunction; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.geo.GeoPoint; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -50,8 +50,11 @@ public class AnomalyCause implements ToXContentObject { */ public static final ParseField FIELD_NAME = new ParseField("field_name"); - public static final ObjectParser PARSER = - new ObjectParser<>(ANOMALY_CAUSE.getPreferredName(), true, AnomalyCause::new); + public static final ObjectParser PARSER = new ObjectParser<>( + ANOMALY_CAUSE.getPreferredName(), + true, + AnomalyCause::new + ); static { PARSER.declareDouble(AnomalyCause::setProbability, PROBABILITY); @@ -86,8 +89,7 @@ public class AnomalyCause implements ToXContentObject { private List influencers; - AnomalyCause() { - } + AnomalyCause() {} @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { @@ -272,8 +274,22 @@ public GeoPoint getActualGeoPoint() { @Override public int hashCode() { - return Objects.hash(probability, actual, typical, byFieldName, byFieldValue, correlatedByFieldValue, fieldName, function, - functionDescription, overFieldName, overFieldValue, partitionFieldName, partitionFieldValue, influencers); + return Objects.hash( + probability, + actual, + typical, + byFieldName, + byFieldValue, + correlatedByFieldValue, + fieldName, + function, + functionDescription, + overFieldName, + overFieldValue, + partitionFieldName, + partitionFieldValue, + influencers + ); } @Override @@ -286,21 +302,21 @@ public boolean equals(Object other) { return false; } - AnomalyCause that = (AnomalyCause)other; - - return this.probability == that.probability && - Objects.equals(this.typical, that.typical) && - Objects.equals(this.actual, that.actual) && - Objects.equals(this.function, that.function) && - Objects.equals(this.functionDescription, that.functionDescription) && - Objects.equals(this.fieldName, that.fieldName) && - Objects.equals(this.byFieldName, that.byFieldName) && - Objects.equals(this.byFieldValue, that.byFieldValue) && - Objects.equals(this.correlatedByFieldValue, that.correlatedByFieldValue) && - Objects.equals(this.partitionFieldName, that.partitionFieldName) && - Objects.equals(this.partitionFieldValue, that.partitionFieldValue) && - Objects.equals(this.overFieldName, that.overFieldName) && - Objects.equals(this.overFieldValue, that.overFieldValue) && - Objects.equals(this.influencers, that.influencers); + AnomalyCause that = (AnomalyCause) other; + + return this.probability == that.probability + && Objects.equals(this.typical, that.typical) + && Objects.equals(this.actual, that.actual) + && Objects.equals(this.function, that.function) + && Objects.equals(this.functionDescription, that.functionDescription) + && Objects.equals(this.fieldName, that.fieldName) + && Objects.equals(this.byFieldName, that.byFieldName) + && Objects.equals(this.byFieldValue, that.byFieldValue) + && Objects.equals(this.correlatedByFieldValue, that.correlatedByFieldValue) + && Objects.equals(this.partitionFieldName, that.partitionFieldName) + && Objects.equals(this.partitionFieldValue, that.partitionFieldValue) + && Objects.equals(this.overFieldName, that.overFieldName) + && Objects.equals(this.overFieldValue, that.overFieldValue) + && Objects.equals(this.influencers, that.influencers); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/results/AnomalyRecord.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/results/AnomalyRecord.java index 145aa00a4e2e0..6381779d83f23 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/results/AnomalyRecord.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/results/AnomalyRecord.java @@ -10,11 +10,11 @@ import org.elasticsearch.client.common.TimeUtil; import org.elasticsearch.client.ml.job.config.DetectorFunction; import org.elasticsearch.client.ml.job.config.Job; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.geo.GeoPoint; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -75,15 +75,20 @@ public class AnomalyRecord implements ToXContentObject { public static final ParseField RECORD_SCORE = new ParseField("record_score"); public static final ParseField INITIAL_RECORD_SCORE = new ParseField("initial_record_score"); - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(RESULT_TYPE_VALUE, true, a -> new AnomalyRecord((String) a[0], (Date) a[1], (long) a[2])); - + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + RESULT_TYPE_VALUE, + true, + a -> new AnomalyRecord((String) a[0], (Date) a[1], (long) a[2]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), Job.ID); - PARSER.declareField(ConstructingObjectParser.constructorArg(), - (p) -> TimeUtil.parseTimeField(p, Result.TIMESTAMP.getPreferredName()), - Result.TIMESTAMP, ValueType.VALUE); + PARSER.declareField( + ConstructingObjectParser.constructorArg(), + (p) -> TimeUtil.parseTimeField(p, Result.TIMESTAMP.getPreferredName()), + Result.TIMESTAMP, + ValueType.VALUE + ); PARSER.declareLong(ConstructingObjectParser.constructorArg(), BUCKET_SPAN); PARSER.declareString((anomalyRecord, s) -> {}, Result.RESULT_TYPE); PARSER.declareDouble(AnomalyRecord::setProbability, PROBABILITY); @@ -404,11 +409,32 @@ public GeoPoint getActualGeoPoint() { @Override public int hashCode() { - return Objects.hash(jobId, detectorIndex, bucketSpan, probability, multiBucketImpact, recordScore, - initialRecordScore, typical, actual,function, functionDescription, fieldName, - byFieldName, byFieldValue, correlatedByFieldValue, partitionFieldName, - partitionFieldValue, overFieldName, overFieldValue, timestamp, isInterim, - causes, influences, jobId); + return Objects.hash( + jobId, + detectorIndex, + bucketSpan, + probability, + multiBucketImpact, + recordScore, + initialRecordScore, + typical, + actual, + function, + functionDescription, + fieldName, + byFieldName, + byFieldValue, + correlatedByFieldValue, + partitionFieldName, + partitionFieldValue, + overFieldName, + overFieldValue, + timestamp, + isInterim, + causes, + influences, + jobId + ); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/results/Bucket.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/results/Bucket.java index 950a97f62a280..c33db2edaaacd 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/results/Bucket.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/results/Bucket.java @@ -9,9 +9,9 @@ import org.elasticsearch.client.common.TimeUtil; import org.elasticsearch.client.ml.job.config.Job; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -45,13 +45,20 @@ public class Bucket implements ToXContentObject { public static final String RESULT_TYPE_VALUE = "bucket"; public static final ParseField RESULT_TYPE_FIELD = new ParseField(RESULT_TYPE_VALUE); - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(RESULT_TYPE_VALUE, true, a -> new Bucket((String) a[0], (Date) a[1], (long) a[2])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + RESULT_TYPE_VALUE, + true, + a -> new Bucket((String) a[0], (Date) a[1], (long) a[2]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), Job.ID); - PARSER.declareField(ConstructingObjectParser.constructorArg(), - (p) -> TimeUtil.parseTimeField(p, Result.TIMESTAMP.getPreferredName()), Result.TIMESTAMP, ValueType.VALUE); + PARSER.declareField( + ConstructingObjectParser.constructorArg(), + (p) -> TimeUtil.parseTimeField(p, Result.TIMESTAMP.getPreferredName()), + Result.TIMESTAMP, + ValueType.VALUE + ); PARSER.declareLong(ConstructingObjectParser.constructorArg(), BUCKET_SPAN); PARSER.declareDouble(Bucket::setAnomalyScore, ANOMALY_SCORE); PARSER.declareDouble(Bucket::setInitialAnomalyScore, INITIAL_ANOMALY_SCORE); @@ -197,8 +204,19 @@ void setScheduledEvents(List scheduledEvents) { @Override public int hashCode() { - return Objects.hash(jobId, timestamp, eventCount, initialAnomalyScore, anomalyScore, records, - isInterim, bucketSpan, bucketInfluencers, processingTimeMs, scheduledEvents); + return Objects.hash( + jobId, + timestamp, + eventCount, + initialAnomalyScore, + anomalyScore, + records, + isInterim, + bucketSpan, + bucketInfluencers, + processingTimeMs, + scheduledEvents + ); } /** @@ -216,12 +234,16 @@ public boolean equals(Object other) { Bucket that = (Bucket) other; - return Objects.equals(this.jobId, that.jobId) && Objects.equals(this.timestamp, that.timestamp) - && (this.eventCount == that.eventCount) && (this.bucketSpan == that.bucketSpan) - && (this.anomalyScore == that.anomalyScore) && (this.initialAnomalyScore == that.initialAnomalyScore) - && Objects.equals(this.records, that.records) && Objects.equals(this.isInterim, that.isInterim) - && Objects.equals(this.bucketInfluencers, that.bucketInfluencers) - && (this.processingTimeMs == that.processingTimeMs) - && Objects.equals(this.scheduledEvents, that.scheduledEvents); + return Objects.equals(this.jobId, that.jobId) + && Objects.equals(this.timestamp, that.timestamp) + && (this.eventCount == that.eventCount) + && (this.bucketSpan == that.bucketSpan) + && (this.anomalyScore == that.anomalyScore) + && (this.initialAnomalyScore == that.initialAnomalyScore) + && Objects.equals(this.records, that.records) + && Objects.equals(this.isInterim, that.isInterim) + && Objects.equals(this.bucketInfluencers, that.bucketInfluencers) + && (this.processingTimeMs == that.processingTimeMs) + && Objects.equals(this.scheduledEvents, that.scheduledEvents); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/results/BucketInfluencer.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/results/BucketInfluencer.java index a0862e2019385..62df14ce4e817 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/results/BucketInfluencer.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/results/BucketInfluencer.java @@ -9,9 +9,9 @@ import org.elasticsearch.client.common.TimeUtil; import org.elasticsearch.client.ml.job.config.Job; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -37,15 +37,20 @@ public class BucketInfluencer implements ToXContentObject { public static final ParseField PROBABILITY = new ParseField("probability"); public static final ParseField BUCKET_SPAN = new ParseField("bucket_span"); - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(RESULT_TYPE_FIELD.getPreferredName(), true, - a -> new BucketInfluencer((String) a[0], (Date) a[1], (long) a[2])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + RESULT_TYPE_FIELD.getPreferredName(), + true, + a -> new BucketInfluencer((String) a[0], (Date) a[1], (long) a[2]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), Job.ID); - PARSER.declareField(ConstructingObjectParser.constructorArg(), - (p) -> TimeUtil.parseTimeField(p, Result.TIMESTAMP.getPreferredName()), - Result.TIMESTAMP, ValueType.VALUE); + PARSER.declareField( + ConstructingObjectParser.constructorArg(), + (p) -> TimeUtil.parseTimeField(p, Result.TIMESTAMP.getPreferredName()), + Result.TIMESTAMP, + ValueType.VALUE + ); PARSER.declareLong(ConstructingObjectParser.constructorArg(), BUCKET_SPAN); PARSER.declareString((bucketInfluencer, s) -> {}, Result.RESULT_TYPE); PARSER.declareString(BucketInfluencer::setInfluencerFieldName, INFLUENCER_FIELD_NAME); @@ -149,8 +154,17 @@ public Date getTimestamp() { @Override public int hashCode() { - return Objects.hash(influenceField, initialAnomalyScore, anomalyScore, rawAnomalyScore, probability, isInterim, timestamp, jobId, - bucketSpan); + return Objects.hash( + influenceField, + initialAnomalyScore, + anomalyScore, + rawAnomalyScore, + probability, + isInterim, + timestamp, + jobId, + bucketSpan + ); } @Override @@ -169,9 +183,14 @@ public boolean equals(Object obj) { BucketInfluencer other = (BucketInfluencer) obj; - return Objects.equals(influenceField, other.influenceField) && Double.compare(initialAnomalyScore, other.initialAnomalyScore) == 0 - && Double.compare(anomalyScore, other.anomalyScore) == 0 && Double.compare(rawAnomalyScore, other.rawAnomalyScore) == 0 - && Double.compare(probability, other.probability) == 0 && Objects.equals(isInterim, other.isInterim) - && Objects.equals(timestamp, other.timestamp) && Objects.equals(jobId, other.jobId) && bucketSpan == other.bucketSpan; + return Objects.equals(influenceField, other.influenceField) + && Double.compare(initialAnomalyScore, other.initialAnomalyScore) == 0 + && Double.compare(anomalyScore, other.anomalyScore) == 0 + && Double.compare(rawAnomalyScore, other.rawAnomalyScore) == 0 + && Double.compare(probability, other.probability) == 0 + && Objects.equals(isInterim, other.isInterim) + && Objects.equals(timestamp, other.timestamp) + && Objects.equals(jobId, other.jobId) + && bucketSpan == other.bucketSpan; } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/results/CategoryDefinition.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/results/CategoryDefinition.java index 6de5206c331aa..4b204d7279c38 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/results/CategoryDefinition.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/results/CategoryDefinition.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml.job.results; import org.elasticsearch.client.ml.job.config.Job; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -40,8 +40,11 @@ public class CategoryDefinition implements ToXContentObject { // Used for QueryPage public static final ParseField RESULTS_FIELD = new ParseField("categories"); - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(TYPE.getPreferredName(), true, a -> new CategoryDefinition((String) a[0])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + TYPE.getPreferredName(), + true, + a -> new CategoryDefinition((String) a[0]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), Job.ID); @@ -212,7 +215,18 @@ public boolean equals(Object other) { @Override public int hashCode() { - return Objects.hash(jobId, categoryId, partitionFieldName, partitionFieldValue, terms, regex, maxMatchingLength, examples, - preferredToCategories, numMatches, grokPattern); + return Objects.hash( + jobId, + categoryId, + partitionFieldName, + partitionFieldValue, + terms, + regex, + maxMatchingLength, + examples, + preferredToCategories, + numMatches, + grokPattern + ); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/results/Influence.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/results/Influence.java index 2021ed8aefeb7..0969b5983c75e 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/results/Influence.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/results/Influence.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml.job.results; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -30,8 +30,11 @@ public class Influence implements ToXContentObject { public static final ParseField INFLUENCER_FIELD_VALUES = new ParseField("influencer_field_values"); @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(INFLUENCER.getPreferredName(), true, a -> new Influence((String) a[0], (List) a[1])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + INFLUENCER.getPreferredName(), + true, + a -> new Influence((String) a[0], (List) a[1]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), INFLUENCER_FIELD_NAME); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/results/Influencer.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/results/Influencer.java index bab4e83e66091..46c7516b9853a 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/results/Influencer.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/results/Influencer.java @@ -9,9 +9,9 @@ import org.elasticsearch.client.common.TimeUtil; import org.elasticsearch.client.ml.job.config.Job; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -41,16 +41,21 @@ public class Influencer implements ToXContentObject { public static final ParseField RESULTS_FIELD = new ParseField("influencers"); public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - RESULT_TYPE_FIELD.getPreferredName(), true, - a -> new Influencer((String) a[0], (String) a[1], (String) a[2], (Date) a[3], (long) a[4])); + RESULT_TYPE_FIELD.getPreferredName(), + true, + a -> new Influencer((String) a[0], (String) a[1], (String) a[2], (Date) a[3], (long) a[4]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), Job.ID); PARSER.declareString(ConstructingObjectParser.constructorArg(), INFLUENCER_FIELD_NAME); PARSER.declareString(ConstructingObjectParser.constructorArg(), INFLUENCER_FIELD_VALUE); - PARSER.declareField(ConstructingObjectParser.constructorArg(), - (p) -> TimeUtil.parseTimeField(p, Result.TIMESTAMP.getPreferredName()), - Result.TIMESTAMP, ValueType.VALUE); + PARSER.declareField( + ConstructingObjectParser.constructorArg(), + (p) -> TimeUtil.parseTimeField(p, Result.TIMESTAMP.getPreferredName()), + Result.TIMESTAMP, + ValueType.VALUE + ); PARSER.declareLong(ConstructingObjectParser.constructorArg(), BUCKET_SPAN); PARSER.declareString((influencer, s) -> {}, Result.RESULT_TYPE); PARSER.declareDouble(Influencer::setProbability, PROBABILITY); @@ -144,8 +149,17 @@ void setInterim(boolean value) { @Override public int hashCode() { - return Objects.hash(jobId, timestamp, influenceField, influenceValue, initialInfluencerScore, - influencerScore, probability, isInterim, bucketSpan); + return Objects.hash( + jobId, + timestamp, + influenceField, + influenceValue, + initialInfluencerScore, + influencerScore, + probability, + isInterim, + bucketSpan + ); } @Override @@ -163,11 +177,14 @@ public boolean equals(Object obj) { } Influencer other = (Influencer) obj; - return Objects.equals(jobId, other.jobId) && Objects.equals(timestamp, other.timestamp) - && Objects.equals(influenceField, other.influenceField) - && Objects.equals(influenceValue, other.influenceValue) - && Double.compare(initialInfluencerScore, other.initialInfluencerScore) == 0 - && Double.compare(influencerScore, other.influencerScore) == 0 && Double.compare(probability, other.probability) == 0 - && (isInterim == other.isInterim) && (bucketSpan == other.bucketSpan); + return Objects.equals(jobId, other.jobId) + && Objects.equals(timestamp, other.timestamp) + && Objects.equals(influenceField, other.influenceField) + && Objects.equals(influenceValue, other.influenceValue) + && Double.compare(initialInfluencerScore, other.initialInfluencerScore) == 0 + && Double.compare(influencerScore, other.influencerScore) == 0 + && Double.compare(probability, other.probability) == 0 + && (isInterim == other.isInterim) + && (bucketSpan == other.bucketSpan); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/results/OverallBucket.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/results/OverallBucket.java index 253f8d7256030..9a6bb40682e6f 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/results/OverallBucket.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/results/OverallBucket.java @@ -9,9 +9,9 @@ import org.elasticsearch.client.common.TimeUtil; import org.elasticsearch.client.ml.job.config.Job; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -38,14 +38,19 @@ public class OverallBucket implements ToXContentObject { */ public static final String RESULT_TYPE_VALUE = "overall_bucket"; - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(RESULT_TYPE_VALUE, true, - a -> new OverallBucket((Date) a[0], (long) a[1], (double) a[2], (boolean) a[3])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + RESULT_TYPE_VALUE, + true, + a -> new OverallBucket((Date) a[0], (long) a[1], (double) a[2], (boolean) a[3]) + ); static { - PARSER.declareField(ConstructingObjectParser.constructorArg(), - (p) -> TimeUtil.parseTimeField(p, Result.TIMESTAMP.getPreferredName()), - Result.TIMESTAMP, ObjectParser.ValueType.VALUE); + PARSER.declareField( + ConstructingObjectParser.constructorArg(), + (p) -> TimeUtil.parseTimeField(p, Result.TIMESTAMP.getPreferredName()), + Result.TIMESTAMP, + ObjectParser.ValueType.VALUE + ); PARSER.declareLong(ConstructingObjectParser.constructorArg(), BUCKET_SPAN); PARSER.declareDouble(ConstructingObjectParser.constructorArg(), OVERALL_SCORE); PARSER.declareBoolean(ConstructingObjectParser.constructorArg(), Result.IS_INTERIM); @@ -126,18 +131,21 @@ public boolean equals(Object other) { OverallBucket that = (OverallBucket) other; return Objects.equals(this.timestamp, that.timestamp) - && this.bucketSpan == that.bucketSpan - && this.overallScore == that.overallScore - && Objects.equals(this.jobs, that.jobs) - && this.isInterim == that.isInterim; + && this.bucketSpan == that.bucketSpan + && this.overallScore == that.overallScore + && Objects.equals(this.jobs, that.jobs) + && this.isInterim == that.isInterim; } public static class JobInfo implements ToXContentObject, Comparable { private static final ParseField MAX_ANOMALY_SCORE = new ParseField("max_anomaly_score"); - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("job_info", true, a -> new JobInfo((String) a[0], (double) a[1])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "job_info", + true, + a -> new JobInfo((String) a[0], (double) a[1]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), Job.ID); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/results/Result.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/results/Result.java index 91b675fd4e190..6f5408bb2ae0a 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/results/Result.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/results/Result.java @@ -21,6 +21,5 @@ public final class Result { public static final ParseField TIMESTAMP = new ParseField("timestamp"); public static final ParseField IS_INTERIM = new ParseField("is_interim"); - private Result() { - } + private Result() {} } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/stats/ForecastStats.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/stats/ForecastStats.java index ed98162fd5ab2..796cb18f3eb2d 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/stats/ForecastStats.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/stats/ForecastStats.java @@ -7,9 +7,9 @@ */ package org.elasticsearch.client.ml.job.stats; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -32,30 +32,30 @@ public class ForecastStats implements ToXContentObject { public static final ParseField STATUS = new ParseField("status"); @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("forecast_stats", - true, - (a) -> { - int i = 0; - long total = (long)a[i++]; - SimpleStats memoryStats = (SimpleStats)a[i++]; - SimpleStats recordStats = (SimpleStats)a[i++]; - SimpleStats runtimeStats = (SimpleStats)a[i++]; - Map statusCounts = (Map)a[i]; - return new ForecastStats(total, memoryStats, recordStats, runtimeStats, statusCounts); - }); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "forecast_stats", + true, + (a) -> { + int i = 0; + long total = (long) a[i++]; + SimpleStats memoryStats = (SimpleStats) a[i++]; + SimpleStats recordStats = (SimpleStats) a[i++]; + SimpleStats runtimeStats = (SimpleStats) a[i++]; + Map statusCounts = (Map) a[i]; + return new ForecastStats(total, memoryStats, recordStats, runtimeStats, statusCounts); + } + ); static { PARSER.declareLong(ConstructingObjectParser.constructorArg(), TOTAL); PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), SimpleStats.PARSER, MEMORY_BYTES); PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), SimpleStats.PARSER, RECORDS); PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), SimpleStats.PARSER, PROCESSING_TIME_MS); - PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(), - p -> { - Map counts = new HashMap<>(); - p.map().forEach((key, value) -> counts.put(key, ((Number)value).longValue())); - return counts; - }, STATUS, ObjectParser.ValueType.OBJECT); + PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(), p -> { + Map counts = new HashMap<>(); + p.map().forEach((key, value) -> counts.put(key, ((Number) value).longValue())); + return counts; + }, STATUS, ObjectParser.ValueType.OBJECT); } private final long total; @@ -65,11 +65,13 @@ public class ForecastStats implements ToXContentObject { private SimpleStats runtimeStats; private Map statusCounts; - public ForecastStats(long total, - SimpleStats memoryStats, - SimpleStats recordStats, - SimpleStats runtimeStats, - Map statusCounts) { + public ForecastStats( + long total, + SimpleStats memoryStats, + SimpleStats recordStats, + SimpleStats runtimeStats, + Map statusCounts + ) { this.total = total; this.forecastedJobs = total > 0 ? 1 : 0; if (total > 0) { @@ -153,11 +155,11 @@ public boolean equals(Object obj) { } ForecastStats other = (ForecastStats) obj; - return Objects.equals(total, other.total) && - Objects.equals(forecastedJobs, other.forecastedJobs) && - Objects.equals(memoryStats, other.memoryStats) && - Objects.equals(recordStats, other.recordStats) && - Objects.equals(runtimeStats, other.runtimeStats) && - Objects.equals(statusCounts, other.statusCounts); + return Objects.equals(total, other.total) + && Objects.equals(forecastedJobs, other.forecastedJobs) + && Objects.equals(memoryStats, other.memoryStats) + && Objects.equals(recordStats, other.recordStats) + && Objects.equals(runtimeStats, other.runtimeStats) + && Objects.equals(statusCounts, other.statusCounts); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/stats/JobStats.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/stats/JobStats.java index 60cb0cce15fe9..abf2a278ba763 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/stats/JobStats.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/stats/JobStats.java @@ -7,19 +7,19 @@ */ package org.elasticsearch.client.ml.job.stats; +import org.elasticsearch.client.ml.NodeAttributes; import org.elasticsearch.client.ml.job.config.Job; import org.elasticsearch.client.ml.job.config.JobState; import org.elasticsearch.client.ml.job.process.DataCounts; import org.elasticsearch.client.ml.job.process.ModelSizeStats; import org.elasticsearch.client.ml.job.process.TimingStats; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.client.ml.NodeAttributes; import java.io.IOException; import java.util.Objects; @@ -39,51 +39,43 @@ public class JobStats implements ToXContentObject { private static final ParseField OPEN_TIME = new ParseField("open_time"); private static final ParseField ASSIGNMENT_EXPLANATION = new ParseField("assignment_explanation"); - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("job_stats", - true, - (a) -> { - int i = 0; - String jobId = (String) a[i++]; - DataCounts dataCounts = (DataCounts) a[i++]; - JobState jobState = (JobState) a[i++]; - ModelSizeStats.Builder modelSizeStatsBuilder = (ModelSizeStats.Builder) a[i++]; - ModelSizeStats modelSizeStats = modelSizeStatsBuilder == null ? null : modelSizeStatsBuilder.build(); - TimingStats timingStats = (TimingStats) a[i++]; - ForecastStats forecastStats = (ForecastStats) a[i++]; - NodeAttributes node = (NodeAttributes) a[i++]; - String assignmentExplanation = (String) a[i++]; - TimeValue openTime = (TimeValue) a[i]; - return new JobStats(jobId, - dataCounts, - jobState, - modelSizeStats, - timingStats, - forecastStats, - node, - assignmentExplanation, - openTime); - }); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("job_stats", true, (a) -> { + int i = 0; + String jobId = (String) a[i++]; + DataCounts dataCounts = (DataCounts) a[i++]; + JobState jobState = (JobState) a[i++]; + ModelSizeStats.Builder modelSizeStatsBuilder = (ModelSizeStats.Builder) a[i++]; + ModelSizeStats modelSizeStats = modelSizeStatsBuilder == null ? null : modelSizeStatsBuilder.build(); + TimingStats timingStats = (TimingStats) a[i++]; + ForecastStats forecastStats = (ForecastStats) a[i++]; + NodeAttributes node = (NodeAttributes) a[i++]; + String assignmentExplanation = (String) a[i++]; + TimeValue openTime = (TimeValue) a[i]; + return new JobStats(jobId, dataCounts, jobState, modelSizeStats, timingStats, forecastStats, node, assignmentExplanation, openTime); + }); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), Job.ID); PARSER.declareObject(ConstructingObjectParser.constructorArg(), DataCounts.PARSER, DATA_COUNTS); - PARSER.declareField(ConstructingObjectParser.constructorArg(), + PARSER.declareField( + ConstructingObjectParser.constructorArg(), (p) -> JobState.fromString(p.text()), STATE, - ObjectParser.ValueType.VALUE); + ObjectParser.ValueType.VALUE + ); PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), ModelSizeStats.PARSER, MODEL_SIZE_STATS); PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), TimingStats.PARSER, TIMING_STATS); PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), ForecastStats.PARSER, FORECASTS_STATS); PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), NodeAttributes.PARSER, NODE); PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), ASSIGNMENT_EXPLANATION); - PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(), + PARSER.declareField( + ConstructingObjectParser.optionalConstructorArg(), (p, c) -> TimeValue.parseTimeValue(p.textOrNull(), OPEN_TIME.getPreferredName()), OPEN_TIME, - ObjectParser.ValueType.STRING_OR_NULL); + ObjectParser.ValueType.STRING_OR_NULL + ); } - private final String jobId; private final DataCounts dataCounts; private final JobState state; @@ -94,9 +86,17 @@ public class JobStats implements ToXContentObject { private final String assignmentExplanation; private final TimeValue openTime; - JobStats(String jobId, DataCounts dataCounts, JobState state, @Nullable ModelSizeStats modelSizeStats, - @Nullable TimingStats timingStats, @Nullable ForecastStats forecastStats, @Nullable NodeAttributes node, - @Nullable String assignmentExplanation, @Nullable TimeValue openTime) { + JobStats( + String jobId, + DataCounts dataCounts, + JobState state, + @Nullable ModelSizeStats modelSizeStats, + @Nullable TimingStats timingStats, + @Nullable ForecastStats forecastStats, + @Nullable NodeAttributes node, + @Nullable String assignmentExplanation, + @Nullable TimeValue openTime + ) { this.jobId = Objects.requireNonNull(jobId); this.dataCounts = Objects.requireNonNull(dataCounts); this.state = Objects.requireNonNull(state); @@ -216,14 +216,14 @@ public boolean equals(Object obj) { } JobStats other = (JobStats) obj; - return Objects.equals(jobId, other.jobId) && - Objects.equals(this.dataCounts, other.dataCounts) && - Objects.equals(this.modelSizeStats, other.modelSizeStats) && - Objects.equals(this.timingStats, other.timingStats) && - Objects.equals(this.forecastStats, other.forecastStats) && - Objects.equals(this.state, other.state) && - Objects.equals(this.node, other.node) && - Objects.equals(this.assignmentExplanation, other.assignmentExplanation) && - Objects.equals(this.openTime, other.openTime); + return Objects.equals(jobId, other.jobId) + && Objects.equals(this.dataCounts, other.dataCounts) + && Objects.equals(this.modelSizeStats, other.modelSizeStats) + && Objects.equals(this.timingStats, other.timingStats) + && Objects.equals(this.forecastStats, other.forecastStats) + && Objects.equals(this.state, other.state) + && Objects.equals(this.node, other.node) + && Objects.equals(this.assignmentExplanation, other.assignmentExplanation) + && Objects.equals(this.openTime, other.openTime); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/stats/SimpleStats.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/stats/SimpleStats.java index c984a9d77c394..01050d93b1a91 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/stats/SimpleStats.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/stats/SimpleStats.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml.job.stats; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -25,15 +25,14 @@ public class SimpleStats implements ToXContentObject { public static final ParseField AVG = new ParseField("avg"); public static final ParseField TOTAL = new ParseField("total"); - public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("simple_stats", true, - (a) -> { + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("simple_stats", true, (a) -> { int i = 0; - double total = (double)a[i++]; - double min = (double)a[i++]; - double max = (double)a[i++]; - double avg = (double)a[i++]; - return new SimpleStats(total, min, max, avg); - }); + double total = (double) a[i++]; + double min = (double) a[i++]; + double max = (double) a[i++]; + double avg = (double) a[i++]; + return new SimpleStats(total, min, max, avg); + }); static { PARSER.declareDouble(ConstructingObjectParser.constructorArg(), TOTAL); @@ -86,10 +85,10 @@ public boolean equals(Object obj) { } SimpleStats other = (SimpleStats) obj; - return Objects.equals(total, other.total) && - Objects.equals(min, other.min) && - Objects.equals(avg, other.avg) && - Objects.equals(max, other.max); + return Objects.equals(total, other.total) + && Objects.equals(min, other.min) + && Objects.equals(avg, other.avg) + && Objects.equals(max, other.max); } @Override @@ -103,4 +102,3 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws return builder; } } - diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/DeleteRollupJobRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/DeleteRollupJobRequest.java index 5a63840b53c78..4d03176edd391 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/DeleteRollupJobRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/DeleteRollupJobRequest.java @@ -11,7 +11,6 @@ import java.util.Objects; - public class DeleteRollupJobRequest implements Validatable { private final String id; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/GetRollupIndexCapsRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/GetRollupIndexCapsRequest.java index abc17fad1bef2..351e648fe5dd0 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/GetRollupIndexCapsRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/GetRollupIndexCapsRequest.java @@ -58,7 +58,6 @@ public boolean equals(Object obj) { return false; } GetRollupIndexCapsRequest other = (GetRollupIndexCapsRequest) obj; - return Arrays.equals(indices, other.indices) - && Objects.equals(options, other.options); + return Arrays.equals(indices, other.indices) && Objects.equals(options, other.options); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/GetRollupJobResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/GetRollupJobResponse.java index f0af42854120a..6aebeab9ca4d4 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/GetRollupJobResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/GetRollupJobResponse.java @@ -11,9 +11,9 @@ import org.elasticsearch.client.core.IndexerJobStats; import org.elasticsearch.client.core.IndexerState; import org.elasticsearch.client.rollup.job.config.RollupJobConfig; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -70,13 +70,14 @@ public int hashCode() { } private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "get_rollup_job_response", - true, - args -> { - @SuppressWarnings("unchecked") // We're careful about the type in the list - List jobs = (List) args[0]; - return new GetRollupJobResponse(unmodifiableList(jobs)); - }); + "get_rollup_job_response", + true, + args -> { + @SuppressWarnings("unchecked") // We're careful about the type in the list + List jobs = (List) args[0]; + return new GetRollupJobResponse(unmodifiableList(jobs)); + } + ); static { PARSER.declareObjectArray(constructorArg(), JobWrapper.PARSER::apply, JOBS); } @@ -123,9 +124,10 @@ public RollupJobStatus getStatus() { } private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "job", - true, - a -> new JobWrapper((RollupJobConfig) a[0], (RollupIndexerJobStats) a[1], (RollupJobStatus) a[2])); + "job", + true, + a -> new JobWrapper((RollupJobConfig) a[0], (RollupIndexerJobStats) a[1], (RollupJobStatus) a[2]) + ); static { PARSER.declareObject(ConstructingObjectParser.constructorArg(), (p, c) -> RollupJobConfig.fromXContent(p, null), CONFIG); PARSER.declareObject(ConstructingObjectParser.constructorArg(), RollupIndexerJobStats.PARSER::apply, STATS); @@ -141,9 +143,7 @@ public boolean equals(Object obj) { return false; } JobWrapper other = (JobWrapper) obj; - return Objects.equals(job, other.job) - && Objects.equals(stats, other.stats) - && Objects.equals(status, other.status); + return Objects.equals(job, other.job) && Objects.equals(stats, other.stats) && Objects.equals(status, other.status); } @Override @@ -153,9 +153,7 @@ public int hashCode() { @Override public final String toString() { - return "{job=" + job - + ", stats=" + stats - + ", status=" + status + "}"; + return "{job=" + job + ", stats=" + stats + ", status=" + status + "}"; } } @@ -165,19 +163,54 @@ public final String toString() { */ public static class RollupIndexerJobStats extends IndexerJobStats { - RollupIndexerJobStats(long numPages, long numInputDocuments, long numOuputDocuments, long numInvocations, - long indexTime, long indexTotal, long searchTime, long searchTotal, long processingTime, - long processingTotal, long indexFailures, long searchFailures) { - super(numPages, numInputDocuments, numOuputDocuments, numInvocations, - indexTime, searchTime, processingTime, indexTotal, searchTotal, processingTotal, indexFailures, searchFailures); + RollupIndexerJobStats( + long numPages, + long numInputDocuments, + long numOuputDocuments, + long numInvocations, + long indexTime, + long indexTotal, + long searchTime, + long searchTotal, + long processingTime, + long processingTotal, + long indexFailures, + long searchFailures + ) { + super( + numPages, + numInputDocuments, + numOuputDocuments, + numInvocations, + indexTime, + searchTime, + processingTime, + indexTotal, + searchTotal, + processingTotal, + indexFailures, + searchFailures + ); } private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - STATS.getPreferredName(), - true, - args -> new RollupIndexerJobStats((long) args[0], (long) args[1], (long) args[2], (long) args[3], - (long) args[4], (long) args[5], (long) args[6], (long) args[7], (long) args[8], (long) args[9], - (long) args[10], (long) args[11])); + STATS.getPreferredName(), + true, + args -> new RollupIndexerJobStats( + (long) args[0], + (long) args[1], + (long) args[2], + (long) args[3], + (long) args[4], + (long) args[5], + (long) args[6], + (long) args[7], + (long) args[8], + (long) args[9], + (long) args[10], + (long) args[11] + ) + ); static { PARSER.declareLong(constructorArg(), NUM_PAGES); PARSER.declareLong(constructorArg(), NUM_INPUT_DOCUMENTS); @@ -212,6 +245,7 @@ public static class RollupJobStatus { public IndexerState getState() { return state; } + /** * The current position of the writer. */ @@ -220,14 +254,15 @@ public Map getCurrentPosition() { } private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - STATUS.getPreferredName(), - true, - args -> { - IndexerState state = (IndexerState) args[0]; - @SuppressWarnings("unchecked") // We're careful of the contents - Map currentPosition = (Map) args[1]; - return new RollupJobStatus(state, currentPosition); - }); + STATUS.getPreferredName(), + true, + args -> { + IndexerState state = (IndexerState) args[0]; + @SuppressWarnings("unchecked") // We're careful of the contents + Map currentPosition = (Map) args[1]; + return new RollupJobStatus(state, currentPosition); + } + ); static { PARSER.declareField(constructorArg(), p -> IndexerState.fromString(p.text()), STATE, ObjectParser.ValueType.STRING); PARSER.declareField(optionalConstructorArg(), p -> { @@ -249,8 +284,7 @@ public boolean equals(Object other) { if (this == other) return true; if (other == null || getClass() != other.getClass()) return false; RollupJobStatus that = (RollupJobStatus) other; - return Objects.equals(state, that.state) - && Objects.equals(currentPosition, that.currentPosition); + return Objects.equals(state, that.state) && Objects.equals(currentPosition, that.currentPosition); } @Override @@ -260,8 +294,7 @@ public int hashCode() { @Override public final String toString() { - return "{stats=" + state - + ", currentPosition=" + currentPosition + "}"; + return "{stats=" + state + ", currentPosition=" + currentPosition + "}"; } } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/RollableIndexCaps.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/RollableIndexCaps.java index 13c44b023f20b..9a3fef50cb56f 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/RollableIndexCaps.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/RollableIndexCaps.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.rollup; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentFragment; import org.elasticsearch.xcontent.XContentBuilder; @@ -32,11 +32,14 @@ public class RollableIndexCaps implements ToXContentFragment { private static final ParseField ROLLUP_JOBS = new ParseField("rollup_jobs"); public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - ROLLUP_JOBS.getPreferredName(), true, (Object[] args, String indexName) -> { - @SuppressWarnings("unchecked") - var caps = (List) args[0]; - return new RollableIndexCaps(indexName, caps); - }); + ROLLUP_JOBS.getPreferredName(), + true, + (Object[] args, String indexName) -> { + @SuppressWarnings("unchecked") + var caps = (List) args[0]; + return new RollableIndexCaps(indexName, caps); + } + ); static { PARSER.declareObjectArray(constructorArg(), (p, name) -> RollupJobCaps.PARSER.parse(p, null), ROLLUP_JOBS); } @@ -46,10 +49,9 @@ public class RollableIndexCaps implements ToXContentFragment { RollableIndexCaps(final String indexName, final List caps) { this.indexName = indexName; - this.jobCaps = Collections.unmodifiableList(Objects.requireNonNull(caps) - .stream() - .sorted(Comparator.comparing(RollupJobCaps::getJobID)) - .collect(Collectors.toList())); + this.jobCaps = Collections.unmodifiableList( + Objects.requireNonNull(caps).stream().sorted(Comparator.comparing(RollupJobCaps::getJobID)).collect(Collectors.toList()) + ); } public String getIndexName() { @@ -81,8 +83,7 @@ public boolean equals(Object other) { } RollableIndexCaps that = (RollableIndexCaps) other; - return Objects.equals(this.jobCaps, that.jobCaps) - && Objects.equals(this.indexName, that.indexName); + return Objects.equals(this.jobCaps, that.jobCaps) && Objects.equals(this.indexName, that.indexName); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/RollupJobCaps.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/RollupJobCaps.java index 5d8957558556b..6a9abb76de092 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/RollupJobCaps.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/RollupJobCaps.java @@ -7,9 +7,9 @@ */ package org.elasticsearch.client.rollup; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.core.Tuple; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentFragment; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -34,21 +34,22 @@ public class RollupJobCaps implements ToXContentObject { private static final ParseField FIELDS = new ParseField("fields"); private static final String NAME = "rollup_job_caps"; - public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(NAME, true, - a -> { - @SuppressWarnings("unchecked") - List> caps = (List>) a[3]; - Map mapCaps = - new HashMap<>(caps.stream().collect(Collectors.toMap(Tuple::v1, Tuple::v2))); - return new RollupJobCaps((String) a[0], (String) a[1], (String) a[2], mapCaps); - }); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(NAME, true, a -> { + @SuppressWarnings("unchecked") + List> caps = (List>) a[3]; + Map mapCaps = new HashMap<>(caps.stream().collect(Collectors.toMap(Tuple::v1, Tuple::v2))); + return new RollupJobCaps((String) a[0], (String) a[1], (String) a[2], mapCaps); + }); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), JOB_ID); PARSER.declareString(ConstructingObjectParser.constructorArg(), ROLLUP_INDEX); PARSER.declareString(ConstructingObjectParser.constructorArg(), INDEX_PATTERN); - PARSER.declareNamedObjects(ConstructingObjectParser.constructorArg(), - (p, c, name) -> new Tuple<>(name, RollupFieldCaps.fromXContent(p)), FIELDS); + PARSER.declareNamedObjects( + ConstructingObjectParser.constructorArg(), + (p, c, name) -> new Tuple<>(name, RollupFieldCaps.fromXContent(p)), + FIELDS + ); } private final String jobID; @@ -56,8 +57,12 @@ public class RollupJobCaps implements ToXContentObject { private final String indexPattern; private final Map fieldCapLookup; - RollupJobCaps(final String jobID, final String rollupIndex, - final String indexPattern, final Map fieldCapLookup) { + RollupJobCaps( + final String jobID, + final String rollupIndex, + final String indexPattern, + final Map fieldCapLookup + ) { this.jobID = jobID; this.rollupIndex = rollupIndex; this.indexPattern = indexPattern; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/StartRollupJobResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/StartRollupJobResponse.java index a9f2d618f16fd..3c56eff4415c9 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/StartRollupJobResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/StartRollupJobResponse.java @@ -18,8 +18,11 @@ public class StartRollupJobResponse extends AcknowledgedResponse { private static final String PARSE_FIELD_NAME = "started"; - private static final ConstructingObjectParser PARSER = AcknowledgedResponse - .generateParser("start_rollup_job_response", StartRollupJobResponse::new, PARSE_FIELD_NAME); + private static final ConstructingObjectParser PARSER = AcknowledgedResponse.generateParser( + "start_rollup_job_response", + StartRollupJobResponse::new, + PARSE_FIELD_NAME + ); public StartRollupJobResponse(boolean acknowledged) { super(acknowledged); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/StopRollupJobResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/StopRollupJobResponse.java index 2134b7b989bd0..4f2daecee494c 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/StopRollupJobResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/StopRollupJobResponse.java @@ -18,8 +18,11 @@ public class StopRollupJobResponse extends AcknowledgedResponse { private static final String PARSE_FIELD_NAME = "stopped"; - private static final ConstructingObjectParser PARSER = AcknowledgedResponse - .generateParser("stop_rollup_job_response", StopRollupJobResponse::new, PARSE_FIELD_NAME); + private static final ConstructingObjectParser PARSER = AcknowledgedResponse.generateParser( + "stop_rollup_job_response", + StopRollupJobResponse::new, + PARSE_FIELD_NAME + ); public StopRollupJobResponse(boolean acknowledged) { super(acknowledged); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/job/config/DateHistogramGroupConfig.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/job/config/DateHistogramGroupConfig.java index 074f694a8e6a9..266f0d5bc0fc0 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/job/config/DateHistogramGroupConfig.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/job/config/DateHistogramGroupConfig.java @@ -9,14 +9,14 @@ import org.elasticsearch.client.Validatable; import org.elasticsearch.client.ValidationException; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.core.TimeValue; +import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.core.TimeValue; -import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; import java.io.IOException; import java.time.ZoneId; @@ -87,9 +87,10 @@ public class DateHistogramGroupConfig implements Validatable, ToXContentObject { DateHistogramInterval fixedInterval = (DateHistogramInterval) a[3]; if (oldInterval != null) { - if (calendarInterval != null || fixedInterval != null) { - throw new IllegalArgumentException("Cannot use [interval] with [fixed_interval] or [calendar_interval] " + - "configuration options."); + if (calendarInterval != null || fixedInterval != null) { + throw new IllegalArgumentException( + "Cannot use [interval] with [fixed_interval] or [calendar_interval] " + "configuration options." + ); } return new DateHistogramGroupConfig((String) a[0], oldInterval, (DateHistogramInterval) a[4], (String) a[5]); } else if (calendarInterval != null && fixedInterval == null) { @@ -104,11 +105,19 @@ public class DateHistogramGroupConfig implements Validatable, ToXContentObject { }); PARSER.declareString(constructorArg(), new ParseField(FIELD)); PARSER.declareField(optionalConstructorArg(), p -> new DateHistogramInterval(p.text()), new ParseField(INTERVAL), ValueType.STRING); - PARSER.declareField(optionalConstructorArg(), p -> new DateHistogramInterval(p.text()), - new ParseField(CALENDAR_INTERVAL), ValueType.STRING); - PARSER.declareField(optionalConstructorArg(), p -> new DateHistogramInterval(p.text()), - new ParseField(FIXED_INTERVAL), ValueType.STRING); - PARSER.declareField(optionalConstructorArg(), p -> new DateHistogramInterval(p.text()), new ParseField(DELAY), ValueType.STRING); + PARSER.declareField( + optionalConstructorArg(), + p -> new DateHistogramInterval(p.text()), + new ParseField(CALENDAR_INTERVAL), + ValueType.STRING + ); + PARSER.declareField( + optionalConstructorArg(), + p -> new DateHistogramInterval(p.text()), + new ParseField(FIXED_INTERVAL), + ValueType.STRING + ); + PARSER.declareField(optionalConstructorArg(), p -> new DateHistogramInterval(p.text()), new ParseField(DELAY), ValueType.STRING); PARSER.declareString(optionalConstructorArg(), new ParseField(TIME_ZONE)); } @@ -153,8 +162,9 @@ public CalendarInterval(String field, DateHistogramInterval interval) { public CalendarInterval(String field, DateHistogramInterval interval, DateHistogramInterval delay, String timeZone) { super(field, interval, delay, timeZone); if (DATE_FIELD_UNITS.contains(interval.toString()) == false) { - throw new IllegalArgumentException("The supplied interval [" + interval +"] could not be parsed " + - "as a calendar interval."); + throw new IllegalArgumentException( + "The supplied interval [" + interval + "] could not be parsed " + "as a calendar interval." + ); } } @@ -192,10 +202,12 @@ public DateHistogramGroupConfig(final String field, final DateHistogramInterval * @since 7.2.0 */ @Deprecated - public DateHistogramGroupConfig(final String field, - final DateHistogramInterval interval, - final @Nullable DateHistogramInterval delay, - final @Nullable String timeZone) { + public DateHistogramGroupConfig( + final String field, + final DateHistogramInterval interval, + final @Nullable DateHistogramInterval delay, + final @Nullable String timeZone + ) { this.field = field; this.interval = interval; this.delay = delay; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/job/config/GroupConfig.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/job/config/GroupConfig.java index ffcf1a8f69ab0..fa700bb95d474 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/job/config/GroupConfig.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/job/config/GroupConfig.java @@ -10,8 +10,8 @@ import org.elasticsearch.client.Validatable; import org.elasticsearch.client.ValidationException; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -40,29 +40,37 @@ public class GroupConfig implements Validatable, ToXContentObject { static final String NAME = "groups"; private static final ConstructingObjectParser PARSER; static { - PARSER = new ConstructingObjectParser<>(NAME, true, args -> - new GroupConfig((DateHistogramGroupConfig) args[0], (HistogramGroupConfig) args[1], (TermsGroupConfig) args[2])); - PARSER.declareObject(constructorArg(), - (p, c) -> DateHistogramGroupConfig.fromXContent(p), new ParseField(DateHistogramGroupConfig.NAME)); - PARSER.declareObject(optionalConstructorArg(), - (p, c) -> HistogramGroupConfig.fromXContent(p), new ParseField(HistogramGroupConfig.NAME)); - PARSER.declareObject(optionalConstructorArg(), - (p, c) -> TermsGroupConfig.fromXContent(p), new ParseField(TermsGroupConfig.NAME)); + PARSER = new ConstructingObjectParser<>( + NAME, + true, + args -> new GroupConfig((DateHistogramGroupConfig) args[0], (HistogramGroupConfig) args[1], (TermsGroupConfig) args[2]) + ); + PARSER.declareObject( + constructorArg(), + (p, c) -> DateHistogramGroupConfig.fromXContent(p), + new ParseField(DateHistogramGroupConfig.NAME) + ); + PARSER.declareObject( + optionalConstructorArg(), + (p, c) -> HistogramGroupConfig.fromXContent(p), + new ParseField(HistogramGroupConfig.NAME) + ); + PARSER.declareObject(optionalConstructorArg(), (p, c) -> TermsGroupConfig.fromXContent(p), new ParseField(TermsGroupConfig.NAME)); } private final DateHistogramGroupConfig dateHistogram; - private final @Nullable - HistogramGroupConfig histogram; - private final @Nullable - TermsGroupConfig terms; + private final @Nullable HistogramGroupConfig histogram; + private final @Nullable TermsGroupConfig terms; public GroupConfig(final DateHistogramGroupConfig dateHistogram) { this(dateHistogram, null, null); } - public GroupConfig(final DateHistogramGroupConfig dateHistogram, - final @Nullable HistogramGroupConfig histogram, - final @Nullable TermsGroupConfig terms) { + public GroupConfig( + final DateHistogramGroupConfig dateHistogram, + final @Nullable HistogramGroupConfig histogram, + final @Nullable TermsGroupConfig terms + ) { this.dateHistogram = dateHistogram; this.histogram = histogram; this.terms = terms; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/job/config/HistogramGroupConfig.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/job/config/HistogramGroupConfig.java index cdfd48fbe058d..68dfc2717b3c7 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/job/config/HistogramGroupConfig.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/job/config/HistogramGroupConfig.java @@ -9,8 +9,8 @@ import org.elasticsearch.client.Validatable; import org.elasticsearch.client.ValidationException; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -44,7 +44,8 @@ public class HistogramGroupConfig implements Validatable, ToXContentObject { private static final ConstructingObjectParser PARSER; static { PARSER = new ConstructingObjectParser<>(NAME, true, args -> { - @SuppressWarnings("unchecked") List fields = (List) args[1]; + @SuppressWarnings("unchecked") + List fields = (List) args[1]; return new HistogramGroupConfig((long) args[0], fields != null ? fields.toArray(new String[fields.size()]) : null); }); PARSER.declareLong(constructorArg(), new ParseField(INTERVAL)); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/job/config/MetricConfig.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/job/config/MetricConfig.java index 371047f5d6026..4e9590fd2cdc0 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/job/config/MetricConfig.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/job/config/MetricConfig.java @@ -9,8 +9,8 @@ import org.elasticsearch.client.Validatable; import org.elasticsearch.client.ValidationException; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -47,7 +47,8 @@ public class MetricConfig implements Validatable, ToXContentObject { private static final ConstructingObjectParser PARSER; static { PARSER = new ConstructingObjectParser<>(NAME, true, args -> { - @SuppressWarnings("unchecked") List metrics = (List) args[1]; + @SuppressWarnings("unchecked") + List metrics = (List) args[1]; return new MetricConfig((String) args[0], metrics); }); PARSER.declareString(constructorArg(), new ParseField(FIELD)); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/job/config/RollupJobConfig.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/job/config/RollupJobConfig.java index 1ec9756be3224..92620c9f06458 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/job/config/RollupJobConfig.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/job/config/RollupJobConfig.java @@ -9,12 +9,12 @@ import org.elasticsearch.client.Validatable; import org.elasticsearch.client.ValidationException; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.regex.Regex; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -70,20 +70,26 @@ public class RollupJobConfig implements Validatable, ToXContentObject { PARSER.declareString(constructorArg(), new ParseField(ROLLUP_INDEX)); PARSER.declareObject(optionalConstructorArg(), (p, c) -> GroupConfig.fromXContent(p), new ParseField(GroupConfig.NAME)); PARSER.declareObjectArray(optionalConstructorArg(), (p, c) -> MetricConfig.fromXContent(p), new ParseField(MetricConfig.NAME)); - PARSER.declareField(optionalConstructorArg(), (p, c) -> TimeValue.parseTimeValue(p.textOrNull(), TIMEOUT), - new ParseField(TIMEOUT), ObjectParser.ValueType.STRING_OR_NULL); + PARSER.declareField( + optionalConstructorArg(), + (p, c) -> TimeValue.parseTimeValue(p.textOrNull(), TIMEOUT), + new ParseField(TIMEOUT), + ObjectParser.ValueType.STRING_OR_NULL + ); PARSER.declareString(constructorArg(), new ParseField(CRON)); PARSER.declareInt(constructorArg(), new ParseField(PAGE_SIZE)); } - public RollupJobConfig(final String id, - final String indexPattern, - final String rollupIndex, - final String cron, - final int pageSize, - final GroupConfig groupConfig, - final List metricsConfig, - final @Nullable TimeValue timeout) { + public RollupJobConfig( + final String id, + final String indexPattern, + final String rollupIndex, + final String cron, + final int pageSize, + final GroupConfig groupConfig, + final List metricsConfig, + final @Nullable TimeValue timeout + ) { this.id = id; this.indexPattern = indexPattern; this.rollupIndex = rollupIndex; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/job/config/TermsGroupConfig.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/job/config/TermsGroupConfig.java index 6166bf03913fa..d75e2801f94d8 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/job/config/TermsGroupConfig.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/job/config/TermsGroupConfig.java @@ -9,8 +9,8 @@ import org.elasticsearch.client.Validatable; import org.elasticsearch.client.ValidationException; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -41,7 +41,8 @@ public class TermsGroupConfig implements Validatable, ToXContentObject { private static final ConstructingObjectParser PARSER; static { PARSER = new ConstructingObjectParser<>(NAME, true, args -> { - @SuppressWarnings("unchecked") List fields = (List) args[0]; + @SuppressWarnings("unchecked") + List fields = (List) args[0]; return new TermsGroupConfig(fields != null ? fields.toArray(new String[fields.size()]) : null); }); PARSER.declareStringArray(constructorArg(), new ParseField(FIELDS)); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/searchable_snapshots/CachesStatsResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/searchable_snapshots/CachesStatsResponse.java index 6ef15eb64f40e..65601cd49b488 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/searchable_snapshots/CachesStatsResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/searchable_snapshots/CachesStatsResponse.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.searchable_snapshots; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.util.List; @@ -30,7 +30,10 @@ public List getNodeCachesStats() { @SuppressWarnings("unchecked") private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "caches_stats_response", true, args -> new CachesStatsResponse((List) args[0])); + "caches_stats_response", + true, + args -> new CachesStatsResponse((List) args[0]) + ); static { PARSER.declareNamedObjects(constructorArg(), (p, c, nodeId) -> NodeCachesStats.PARSER.apply(p, nodeId), new ParseField("nodes")); } @@ -58,7 +61,10 @@ public SharedCacheStats getSharedCacheStats() { } private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "node_caches_stats", true, (args, nodeId) -> new NodeCachesStats(nodeId, (SharedCacheStats) args[0])); + "node_caches_stats", + true, + (args, nodeId) -> new NodeCachesStats(nodeId, (SharedCacheStats) args[0]) + ); static { PARSER.declareObject(constructorArg(), (p, c) -> SharedCacheStats.fromXContent(p), new ParseField("shared_cache")); } @@ -162,16 +168,24 @@ public static SharedCacheStats fromXContent(XContentParser parser) { @Override public String toString() { - return "SharedCacheStats{" + - "numRegions=" + numRegions + - ", size=" + size + - ", regionSize=" + regionSize + - ", writes=" + writes + - ", bytesWritten=" + bytesWritten + - ", reads=" + reads + - ", bytesRead=" + bytesRead + - ", evictions=" + evictions + - '}'; + return "SharedCacheStats{" + + "numRegions=" + + numRegions + + ", size=" + + size + + ", regionSize=" + + regionSize + + ", writes=" + + writes + + ", bytesWritten=" + + bytesWritten + + ", reads=" + + reads + + ", bytesRead=" + + bytesRead + + ", evictions=" + + evictions + + '}'; } } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/searchable_snapshots/MountSnapshotRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/searchable_snapshots/MountSnapshotRequest.java index 78d9d6c4b0661..5c603bc32d96f 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/searchable_snapshots/MountSnapshotRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/searchable_snapshots/MountSnapshotRequest.java @@ -140,7 +140,8 @@ public XContentBuilder toXContent(final XContentBuilder builder, final Params pa builder.field("renamed_index", renamedIndex); } if (indexSettings != null) { - builder.startObject("index_settings"); { + builder.startObject("index_settings"); + { indexSettings.toXContent(builder, params); } builder.endObject(); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/AuthenticateRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/AuthenticateRequest.java index 762b9e060ac06..2a5b092fc0263 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/AuthenticateRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/AuthenticateRequest.java @@ -20,8 +20,7 @@ public final class AuthenticateRequest implements Validatable { public static final AuthenticateRequest INSTANCE = new AuthenticateRequest(); - private AuthenticateRequest() { - } + private AuthenticateRequest() {} public Request getRequest() { return new Request(HttpGet.METHOD_NAME, "/_security/_authenticate"); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/AuthenticateResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/AuthenticateResponse.java index c56cf8f7cd522..e9eeb4454d05f 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/AuthenticateResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/AuthenticateResponse.java @@ -9,12 +9,12 @@ package org.elasticsearch.client.security; import org.elasticsearch.client.security.user.User; -import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.core.Nullable; import java.io.IOException; import java.util.List; @@ -47,14 +47,23 @@ public final class AuthenticateResponse implements ToXContentObject { @SuppressWarnings("unchecked") private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "client_security_authenticate_response", true, - a -> new AuthenticateResponse( - new User((String) a[0], ((List) a[1]), (Map) a[2], - (String) a[3], (String) a[4]), (Boolean) a[5], (RealmInfo) a[6], (RealmInfo) a[7], (String) a[8], - (Map) a[9])); + "client_security_authenticate_response", + true, + a -> new AuthenticateResponse( + new User((String) a[0], ((List) a[1]), (Map) a[2], (String) a[3], (String) a[4]), + (Boolean) a[5], + (RealmInfo) a[6], + (RealmInfo) a[7], + (String) a[8], + (Map) a[9] + ) + ); static { - final ConstructingObjectParser realmInfoParser = new ConstructingObjectParser<>("realm_info", true, - a -> new RealmInfo((String) a[0], (String) a[1])); + final ConstructingObjectParser realmInfoParser = new ConstructingObjectParser<>( + "realm_info", + true, + a -> new RealmInfo((String) a[0], (String) a[1]) + ); realmInfoParser.declareString(constructorArg(), REALM_NAME); realmInfoParser.declareString(constructorArg(), REALM_TYPE); PARSER.declareString(constructorArg(), USERNAME); @@ -77,13 +86,24 @@ public final class AuthenticateResponse implements ToXContentObject { @Nullable private final Map token; - public AuthenticateResponse(User user, boolean enabled, RealmInfo authenticationRealm, - RealmInfo lookupRealm, String authenticationType) { + public AuthenticateResponse( + User user, + boolean enabled, + RealmInfo authenticationRealm, + RealmInfo lookupRealm, + String authenticationType + ) { this(user, enabled, authenticationRealm, lookupRealm, authenticationType, null); } - public AuthenticateResponse(User user, boolean enabled, RealmInfo authenticationRealm, - RealmInfo lookupRealm, String authenticationType, @Nullable Map token) { + public AuthenticateResponse( + User user, + boolean enabled, + RealmInfo authenticationRealm, + RealmInfo lookupRealm, + String authenticationType, + @Nullable Map token + ) { this.user = user; this.enabled = enabled; this.authenticationRealm = authenticationRealm; @@ -163,12 +183,12 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; AuthenticateResponse that = (AuthenticateResponse) o; - return enabled == that.enabled && - Objects.equals(user, that.user) && - Objects.equals(authenticationRealm, that.authenticationRealm) && - Objects.equals(lookupRealm, that.lookupRealm) && - Objects.equals(authenticationType, that.authenticationType) && - Objects.equals(token, that.token); + return enabled == that.enabled + && Objects.equals(user, that.user) + && Objects.equals(authenticationRealm, that.authenticationRealm) + && Objects.equals(lookupRealm, that.lookupRealm) + && Objects.equals(authenticationType, that.authenticationType) + && Objects.equals(token, that.token); } @Override @@ -202,8 +222,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; RealmInfo realmInfo = (RealmInfo) o; - return Objects.equals(name, realmInfo.name) && - Objects.equals(type, realmInfo.type); + return Objects.equals(name, realmInfo.name) && Objects.equals(type, realmInfo.type); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/ChangePasswordRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/ChangePasswordRequest.java index cc15459c0f255..ae13a77abc456 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/ChangePasswordRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/ChangePasswordRequest.java @@ -55,9 +55,7 @@ public RefreshPolicy getRefreshPolicy() { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { byte[] charBytes = CharArrays.toUtf8Bytes(password); try { - return builder.startObject() - .field("password").utf8Value(charBytes, 0, charBytes.length) - .endObject(); + return builder.startObject().field("password").utf8Value(charBytes, 0, charBytes.length).endObject(); } finally { Arrays.fill(charBytes, (byte) 0); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/ClearApiKeyCacheRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/ClearApiKeyCacheRequest.java index 2dedaa9e119f0..df12f843c6361 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/ClearApiKeyCacheRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/ClearApiKeyCacheRequest.java @@ -31,12 +31,12 @@ public static ClearApiKeyCacheRequest clearAll() { return new ClearApiKeyCacheRequest(); } - public static ClearApiKeyCacheRequest clearById(String ... ids) { + public static ClearApiKeyCacheRequest clearById(String... ids) { if (ids.length == 0) { throw new IllegalArgumentException("Ids cannot be empty"); } return new ClearApiKeyCacheRequest(ids); - } + } /** * @return an array of key names that will be evicted @@ -47,10 +47,8 @@ public String[] ids() { @Override public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; ClearApiKeyCacheRequest that = (ClearApiKeyCacheRequest) o; return Arrays.equals(ids, that.ids); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/ClearPrivilegesCacheResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/ClearPrivilegesCacheResponse.java index d0632e6fe30d2..123403a4a5a82 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/ClearPrivilegesCacheResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/ClearPrivilegesCacheResponse.java @@ -21,9 +21,11 @@ public final class ClearPrivilegesCacheResponse extends SecurityNodesResponse { @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("clear_privileges_cache_response", false, - args -> new ClearPrivilegesCacheResponse((List)args[0], (NodesResponseHeader) args[1], (String) args[2])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "clear_privileges_cache_response", + false, + args -> new ClearPrivilegesCacheResponse((List) args[0], (NodesResponseHeader) args[1], (String) args[2]) + ); static { SecurityNodesResponse.declareCommonNodesResponseParsing(PARSER); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/ClearRealmCacheResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/ClearRealmCacheResponse.java index 26a3cd524c53a..787d74325feb3 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/ClearRealmCacheResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/ClearRealmCacheResponse.java @@ -22,9 +22,10 @@ public final class ClearRealmCacheResponse extends SecurityNodesResponse { @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("clear_realm_cache_response_parser", - args -> new ClearRealmCacheResponse((List) args[0], (NodesResponseHeader) args[1], (String) args[2])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "clear_realm_cache_response_parser", + args -> new ClearRealmCacheResponse((List) args[0], (NodesResponseHeader) args[1], (String) args[2]) + ); static { SecurityNodesResponse.declareCommonNodesResponseParsing(PARSER); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/ClearRolesCacheResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/ClearRolesCacheResponse.java index 65c56ebbf8816..7f6949dbfe9d4 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/ClearRolesCacheResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/ClearRolesCacheResponse.java @@ -21,9 +21,11 @@ public final class ClearRolesCacheResponse extends SecurityNodesResponse { @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("clear_roles_cache_response", false, - args -> new ClearRolesCacheResponse((List)args[0], (NodesResponseHeader) args[1], (String) args[2])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "clear_roles_cache_response", + false, + args -> new ClearRolesCacheResponse((List) args[0], (NodesResponseHeader) args[1], (String) args[2]) + ); static { SecurityNodesResponse.declareCommonNodesResponseParsing(PARSER); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/ClearSecurityCacheResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/ClearSecurityCacheResponse.java index 5471cf4bb1498..4861e75e80fec 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/ClearSecurityCacheResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/ClearSecurityCacheResponse.java @@ -21,9 +21,11 @@ public final class ClearSecurityCacheResponse extends SecurityNodesResponse { @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("clear_security_cache_response", false, - args -> new ClearSecurityCacheResponse((List)args[0], (NodesResponseHeader) args[1], (String) args[2])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "clear_security_cache_response", + false, + args -> new ClearSecurityCacheResponse((List) args[0], (NodesResponseHeader) args[1], (String) args[2]) + ); static { SecurityNodesResponse.declareCommonNodesResponseParsing(PARSER); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/ClearServiceAccountTokenCacheRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/ClearServiceAccountTokenCacheRequest.java index e390db0702202..4754b290daa10 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/ClearServiceAccountTokenCacheRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/ClearServiceAccountTokenCacheRequest.java @@ -46,10 +46,8 @@ public String[] getTokenNames() { @Override public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; ClearServiceAccountTokenCacheRequest that = (ClearServiceAccountTokenCacheRequest) o; return namespace.equals(that.namespace) && serviceName.equals(that.serviceName) && Arrays.equals(tokenNames, that.tokenNames); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/CreateApiKeyRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/CreateApiKeyRequest.java index 5e7f4eae28e8e..1baa9c93c95f8 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/CreateApiKeyRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/CreateApiKeyRequest.java @@ -38,9 +38,13 @@ public final class CreateApiKeyRequest implements Validatable, ToXContentObject * @param expiration to specify expiration for the API key * @param metadata Arbitrary metadata for the API key */ - public CreateApiKeyRequest(String name, List roles, @Nullable TimeValue expiration, - @Nullable final RefreshPolicy refreshPolicy, - @Nullable Map metadata) { + public CreateApiKeyRequest( + String name, + List roles, + @Nullable TimeValue expiration, + @Nullable final RefreshPolicy refreshPolicy, + @Nullable Map metadata + ) { this.name = name; this.roles = Objects.requireNonNull(roles, "roles may not be null"); this.expiration = expiration; @@ -48,8 +52,7 @@ public CreateApiKeyRequest(String name, List roles, @Nullable TimeValue ex this.metadata = metadata; } - public CreateApiKeyRequest(String name, List roles, @Nullable TimeValue expiration, - @Nullable final RefreshPolicy refreshPolicy) { + public CreateApiKeyRequest(String name, List roles, @Nullable TimeValue expiration, @Nullable final RefreshPolicy refreshPolicy) { this(name, roles, expiration, refreshPolicy, null); } @@ -87,8 +90,11 @@ public boolean equals(Object o) { return false; } final CreateApiKeyRequest that = (CreateApiKeyRequest) o; - return Objects.equals(name, that.name) && Objects.equals(refreshPolicy, that.refreshPolicy) && Objects.equals(roles, that.roles) - && Objects.equals(expiration, that.expiration) && Objects.equals(metadata, that.metadata); + return Objects.equals(name, that.name) + && Objects.equals(refreshPolicy, that.refreshPolicy) + && Objects.equals(roles, that.roles) + && Objects.equals(expiration, that.expiration) + && Objects.equals(metadata, that.metadata); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/CreateApiKeyResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/CreateApiKeyResponse.java index 92cf42e88b015..6bd9b80bc7bba 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/CreateApiKeyResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/CreateApiKeyResponse.java @@ -8,10 +8,10 @@ package org.elasticsearch.client.security; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.settings.SecureString; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -40,7 +40,7 @@ public CreateApiKeyResponse(String name, String id, SecureString key, Instant ex // As we do not yet support the nanosecond precision when we serialize to JSON, // here creating the 'Instant' of milliseconds precision. // This Instant can then be used for date comparison. - this.expiration = (expiration != null) ? Instant.ofEpochMilli(expiration.toEpochMilli()): null; + this.expiration = (expiration != null) ? Instant.ofEpochMilli(expiration.toEpochMilli()) : null; } public String getName() { @@ -79,25 +79,31 @@ public boolean equals(Object obj) { } final CreateApiKeyResponse other = (CreateApiKeyResponse) obj; return Objects.equals(id, other.id) - && Objects.equals(key, other.key) - && Objects.equals(name, other.name) - && Objects.equals(expiration, other.expiration); + && Objects.equals(key, other.key) + && Objects.equals(name, other.name) + && Objects.equals(expiration, other.expiration); } private static String encode(CharSequence id, CharSequence key) { return Base64.getEncoder().encodeToString((id + ":" + key).getBytes(StandardCharsets.UTF_8)); } - static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("create_api_key_response", + static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "create_api_key_response", args -> { final String id = (String) args[1]; final String key = (String) args[2]; if (args[4] != null && false == args[4].equals(encode(id, key))) { throw new IllegalArgumentException("the encoded value does not match id and api_key"); } - return new CreateApiKeyResponse((String) args[0], id, new SecureString(key.toCharArray()), - (args[3] == null) ? null : Instant.ofEpochMilli((Long) args[3])); - }); + return new CreateApiKeyResponse( + (String) args[0], + id, + new SecureString(key.toCharArray()), + (args[3] == null) ? null : Instant.ofEpochMilli((Long) args[3]) + ); + } + ); static { PARSER.declareString(constructorArg(), new ParseField("name")); PARSER.declareString(constructorArg(), new ParseField("id")); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/CreateServiceAccountTokenRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/CreateServiceAccountTokenRequest.java index f7a94f490ab0a..3a0a4a82b44fb 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/CreateServiceAccountTokenRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/CreateServiceAccountTokenRequest.java @@ -25,9 +25,12 @@ public final class CreateServiceAccountTokenRequest implements Validatable { @Nullable private final RefreshPolicy refreshPolicy; - public CreateServiceAccountTokenRequest(String namespace, String serviceName, - @Nullable String tokenName, - @Nullable RefreshPolicy refreshPolicy) { + public CreateServiceAccountTokenRequest( + String namespace, + String serviceName, + @Nullable String tokenName, + @Nullable RefreshPolicy refreshPolicy + ) { this.namespace = Objects.requireNonNull(namespace, "namespace is required"); this.serviceName = Objects.requireNonNull(serviceName, "service-name is required"); this.tokenName = tokenName; @@ -60,13 +63,13 @@ public RefreshPolicy getRefreshPolicy() { @Override public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; CreateServiceAccountTokenRequest that = (CreateServiceAccountTokenRequest) o; - return namespace.equals(that.namespace) && serviceName.equals(that.serviceName) && Objects.equals(tokenName, - that.tokenName) && refreshPolicy == that.refreshPolicy; + return namespace.equals(that.namespace) + && serviceName.equals(that.serviceName) + && Objects.equals(tokenName, that.tokenName) + && refreshPolicy == that.refreshPolicy; } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/CreateServiceAccountTokenResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/CreateServiceAccountTokenResponse.java index 76a0c72192b7e..3d955e09245ce 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/CreateServiceAccountTokenResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/CreateServiceAccountTokenResponse.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.security; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -41,10 +41,8 @@ public SecureString getValue() { @Override public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; CreateServiceAccountTokenResponse that = (CreateServiceAccountTokenResponse) o; return Objects.equals(name, that.name) && Objects.equals(value, that.value); } @@ -67,7 +65,8 @@ public int hashCode() { } final Token token = (Token) args[1]; return new CreateServiceAccountTokenResponse(token.name, new SecureString(token.value.toCharArray())); - }); + } + ); static { TOKEN_PARSER.declareString(constructorArg(), new ParseField("name")); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/CreateTokenRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/CreateTokenRequest.java index ca8cce781fa06..c055c543c06c1 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/CreateTokenRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/CreateTokenRequest.java @@ -9,9 +9,9 @@ package org.elasticsearch.client.security; import org.elasticsearch.client.Validatable; +import org.elasticsearch.common.Strings; import org.elasticsearch.core.CharArrays; import org.elasticsearch.core.Nullable; -import org.elasticsearch.common.Strings; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -41,8 +41,14 @@ public final class CreateTokenRequest implements Validatable, ToXContentObject { *
  • {@link #kerberosGrant(char[])}
  • * */ - public CreateTokenRequest(String grantType, @Nullable String scope, @Nullable String username, @Nullable char[] password, - @Nullable String refreshToken, @Nullable char[] kerberosTicket) { + public CreateTokenRequest( + String grantType, + @Nullable String scope, + @Nullable String username, + @Nullable char[] password, + @Nullable String refreshToken, + @Nullable char[] kerberosTicket + ) { if (Strings.isNullOrEmpty(grantType)) { throw new IllegalArgumentException("grant_type is required"); } @@ -108,8 +114,7 @@ public char[] getKerberosTicket() { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.startObject() - .field("grant_type", grantType); + builder.startObject().field("grant_type", grantType); if (scope != null) { builder.field("scope", scope); } @@ -147,12 +152,12 @@ public boolean equals(Object o) { return false; } final CreateTokenRequest that = (CreateTokenRequest) o; - return Objects.equals(grantType, that.grantType) && - Objects.equals(scope, that.scope) && - Objects.equals(username, that.username) && - Arrays.equals(password, that.password) && - Objects.equals(refreshToken, that.refreshToken) && - Arrays.equals(kerberosTicket, that.kerberosTicket); + return Objects.equals(grantType, that.grantType) + && Objects.equals(scope, that.scope) + && Objects.equals(username, that.username) + && Arrays.equals(password, that.password) + && Objects.equals(refreshToken, that.refreshToken) + && Arrays.equals(kerberosTicket, that.kerberosTicket); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/CreateTokenResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/CreateTokenResponse.java index 617b5eeda4f86..a7160e4ab6e26 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/CreateTokenResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/CreateTokenResponse.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.security; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -33,8 +33,15 @@ public final class CreateTokenResponse { private final String kerberosAuthenticationResponseToken; private final AuthenticateResponse authentication; - public CreateTokenResponse(String accessToken, String type, TimeValue expiresIn, String scope, String refreshToken, - String kerberosAuthenticationResponseToken, AuthenticateResponse authentication) { + public CreateTokenResponse( + String accessToken, + String type, + TimeValue expiresIn, + String scope, + String refreshToken, + String kerberosAuthenticationResponseToken, + AuthenticateResponse authentication + ) { this.accessToken = accessToken; this.type = type; this.expiresIn = expiresIn; @@ -68,7 +75,9 @@ public String getKerberosAuthenticationResponseToken() { return kerberosAuthenticationResponseToken; } - public AuthenticateResponse getAuthentication() { return authentication; } + public AuthenticateResponse getAuthentication() { + return authentication; + } @Override public boolean equals(Object o) { @@ -79,13 +88,13 @@ public boolean equals(Object o) { return false; } final CreateTokenResponse that = (CreateTokenResponse) o; - return Objects.equals(accessToken, that.accessToken) && - Objects.equals(type, that.type) && - Objects.equals(expiresIn, that.expiresIn) && - Objects.equals(scope, that.scope) && - Objects.equals(refreshToken, that.refreshToken) && - Objects.equals(kerberosAuthenticationResponseToken, that.kerberosAuthenticationResponseToken)&& - Objects.equals(authentication, that.authentication); + return Objects.equals(accessToken, that.accessToken) + && Objects.equals(type, that.type) + && Objects.equals(expiresIn, that.expiresIn) + && Objects.equals(scope, that.scope) + && Objects.equals(refreshToken, that.refreshToken) + && Objects.equals(kerberosAuthenticationResponseToken, that.kerberosAuthenticationResponseToken) + && Objects.equals(authentication, that.authentication); } @Override @@ -94,9 +103,18 @@ public int hashCode() { } private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "create_token_response", true, args -> new CreateTokenResponse((String) args[0], (String) args[1], - TimeValue.timeValueSeconds((Long) args[2]), (String) args[3], (String) args[4], (String) args[5], - (AuthenticateResponse) args[6])); + "create_token_response", + true, + args -> new CreateTokenResponse( + (String) args[0], + (String) args[1], + TimeValue.timeValueSeconds((Long) args[2]), + (String) args[3], + (String) args[4], + (String) args[5], + (AuthenticateResponse) args[6] + ) + ); static { PARSER.declareString(constructorArg(), new ParseField("access_token")); @@ -112,4 +130,3 @@ public static CreateTokenResponse fromXContent(XContentParser parser) throws IOE return PARSER.parse(parser, null); } } - diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DelegatePkiAuthenticationRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DelegatePkiAuthenticationRequest.java index 5af713218d997..800c21362e860 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DelegatePkiAuthenticationRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DelegatePkiAuthenticationRequest.java @@ -39,12 +39,12 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.startObject().startArray("x509_certificate_chain"); try { for (X509Certificate cert : x509CertificateChain) { - builder.value(Base64.getEncoder().encodeToString(cert.getEncoded())); - } - } catch (CertificateEncodingException e) { - throw new IOException(e); - } - return builder.endArray().endObject(); + builder.value(Base64.getEncoder().encodeToString(cert.getEncoded())); + } + } catch (CertificateEncodingException e) { + throw new IOException(e); + } + return builder.endArray().endObject(); } public List getCertificateChain() { diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DelegatePkiAuthenticationResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DelegatePkiAuthenticationResponse.java index 58f35bd072dd7..043649d6c0bce 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DelegatePkiAuthenticationResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DelegatePkiAuthenticationResponse.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.security; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -25,8 +25,7 @@ public final class DelegatePkiAuthenticationResponse { private final TimeValue expiresIn; private final AuthenticateResponse authentication; - public DelegatePkiAuthenticationResponse(String accessToken, String type, TimeValue expiresIn, - AuthenticateResponse authentication) { + public DelegatePkiAuthenticationResponse(String accessToken, String type, TimeValue expiresIn, AuthenticateResponse authentication) { this.accessToken = accessToken; this.type = type; this.expiresIn = expiresIn; @@ -45,7 +44,9 @@ public TimeValue getExpiresIn() { return expiresIn; } - public AuthenticateResponse getAuthentication() { return authentication; } + public AuthenticateResponse getAuthentication() { + return authentication; + } @Override public boolean equals(Object o) { @@ -56,10 +57,10 @@ public boolean equals(Object o) { return false; } final DelegatePkiAuthenticationResponse that = (DelegatePkiAuthenticationResponse) o; - return Objects.equals(accessToken, that.accessToken) && - Objects.equals(type, that.type) && - Objects.equals(expiresIn, that.expiresIn) && - Objects.equals(authentication, that.authentication); + return Objects.equals(accessToken, that.accessToken) + && Objects.equals(type, that.type) + && Objects.equals(expiresIn, that.expiresIn) + && Objects.equals(authentication, that.authentication); } @Override @@ -69,9 +70,15 @@ public int hashCode() { @SuppressWarnings("unchecked") private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "delegate_pki_response", true, - args -> new DelegatePkiAuthenticationResponse((String) args[0], (String) args[1], TimeValue.timeValueSeconds((Long) args[2]), - (AuthenticateResponse) args[3])); + "delegate_pki_response", + true, + args -> new DelegatePkiAuthenticationResponse( + (String) args[0], + (String) args[1], + TimeValue.timeValueSeconds((Long) args[2]), + (AuthenticateResponse) args[3] + ) + ); static { PARSER.declareString(constructorArg(), new ParseField("access_token")); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeletePrivilegesRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeletePrivilegesRequest.java index a84554ddbdf82..f73f194ad4ac3 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeletePrivilegesRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeletePrivilegesRequest.java @@ -9,9 +9,9 @@ package org.elasticsearch.client.security; import org.elasticsearch.client.Validatable; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.util.CollectionUtils; +import org.elasticsearch.core.Nullable; /** * A request to delete application privileges diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteRoleMappingRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteRoleMappingRequest.java index edc950dc6ef12..023bfabc47b54 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteRoleMappingRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteRoleMappingRequest.java @@ -9,8 +9,8 @@ package org.elasticsearch.client.security; import org.elasticsearch.client.Validatable; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; +import org.elasticsearch.core.Nullable; import java.util.Objects; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteRoleMappingResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteRoleMappingResponse.java index f80b32fa054c6..8861ef8cf6c88 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteRoleMappingResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteRoleMappingResponse.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.security; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -52,7 +52,10 @@ public int hashCode() { } private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "delete_role_mapping_response", true, args -> new DeleteRoleMappingResponse((boolean) args[0])); + "delete_role_mapping_response", + true, + args -> new DeleteRoleMappingResponse((boolean) args[0]) + ); static { PARSER.declareBoolean(constructorArg(), new ParseField("found")); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteRoleRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteRoleRequest.java index 0d25990df333f..c81de22d5fcb6 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteRoleRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteRoleRequest.java @@ -21,7 +21,7 @@ public final class DeleteRoleRequest implements Validatable { private final RefreshPolicy refreshPolicy; public DeleteRoleRequest(String name) { - this(name, RefreshPolicy.IMMEDIATE); + this(name, RefreshPolicy.IMMEDIATE); } public DeleteRoleRequest(String name, RefreshPolicy refreshPolicy) { diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteRoleResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteRoleResponse.java index a356c8483697d..7960bc5335a20 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteRoleResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteRoleResponse.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.security; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -31,8 +31,11 @@ public boolean isFound() { return this.found; } - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("delete_role_response", - true, args -> new DeleteRoleResponse((boolean) args[0])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "delete_role_response", + true, + args -> new DeleteRoleResponse((boolean) args[0]) + ); static { PARSER.declareBoolean(constructorArg(), new ParseField("found")); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteServiceAccountTokenRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteServiceAccountTokenRequest.java index 9ab8339a156a8..4e633f52dab7d 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteServiceAccountTokenRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteServiceAccountTokenRequest.java @@ -24,8 +24,7 @@ public class DeleteServiceAccountTokenRequest implements Validatable { @Nullable private final RefreshPolicy refreshPolicy; - public DeleteServiceAccountTokenRequest(String namespace, String serviceName, String tokenName, - @Nullable RefreshPolicy refreshPolicy) { + public DeleteServiceAccountTokenRequest(String namespace, String serviceName, String tokenName, @Nullable RefreshPolicy refreshPolicy) { this.namespace = Objects.requireNonNull(namespace, "namespace is required"); this.serviceName = Objects.requireNonNull(serviceName, "service-name is required"); this.tokenName = Objects.requireNonNull(tokenName, "token name is required"); @@ -54,13 +53,13 @@ public RefreshPolicy getRefreshPolicy() { @Override public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; DeleteServiceAccountTokenRequest that = (DeleteServiceAccountTokenRequest) o; - return namespace.equals(that.namespace) && serviceName.equals(that.serviceName) - && tokenName.equals(that.tokenName) && refreshPolicy == that.refreshPolicy; + return namespace.equals(that.namespace) + && serviceName.equals(that.serviceName) + && tokenName.equals(that.tokenName) + && refreshPolicy == that.refreshPolicy; } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteServiceAccountTokenResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteServiceAccountTokenResponse.java index c8c1dc161ca7c..876f876c13a8e 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteServiceAccountTokenResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteServiceAccountTokenResponse.java @@ -21,8 +21,11 @@ public final class DeleteServiceAccountTokenResponse extends AcknowledgedRespons private static final String PARSE_FIELD_NAME = "found"; - private static final ConstructingObjectParser PARSER = AcknowledgedResponse - .generateParser("delete_service_account_token_response", DeleteServiceAccountTokenResponse::new, PARSE_FIELD_NAME); + private static final ConstructingObjectParser PARSER = AcknowledgedResponse.generateParser( + "delete_service_account_token_response", + DeleteServiceAccountTokenResponse::new, + PARSE_FIELD_NAME + ); public DeleteServiceAccountTokenResponse(boolean acknowledged) { super(acknowledged); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserRequest.java index 2e9992d9be31d..cb2e3ea74582e 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserRequest.java @@ -21,7 +21,7 @@ public final class DeleteUserRequest implements Validatable { private final RefreshPolicy refreshPolicy; public DeleteUserRequest(String name) { - this(name, RefreshPolicy.IMMEDIATE); + this(name, RefreshPolicy.IMMEDIATE); } public DeleteUserRequest(String name, RefreshPolicy refreshPolicy) { diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserResponse.java index b749885eef242..8de14c31e3d40 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteUserResponse.java @@ -21,8 +21,11 @@ public final class DeleteUserResponse extends AcknowledgedResponse { private static final String PARSE_FIELD_NAME = "found"; - private static final ConstructingObjectParser PARSER = AcknowledgedResponse - .generateParser("delete_user_response", DeleteUserResponse::new, PARSE_FIELD_NAME); + private static final ConstructingObjectParser PARSER = AcknowledgedResponse.generateParser( + "delete_user_response", + DeleteUserResponse::new, + PARSE_FIELD_NAME + ); public DeleteUserResponse(boolean acknowledged) { super(acknowledged); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/ExpressionRoleMapping.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/ExpressionRoleMapping.java index 0fc062c4a6182..4818ebdd167c7 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/ExpressionRoleMapping.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/ExpressionRoleMapping.java @@ -10,9 +10,9 @@ import org.elasticsearch.client.security.support.expressiondsl.RoleMapperExpression; import org.elasticsearch.client.security.support.expressiondsl.parser.RoleMapperExpressionParser; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.util.Collections; @@ -32,13 +32,26 @@ public final class ExpressionRoleMapping { @SuppressWarnings("unchecked") - static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("role-mapping", true, - (args, name) -> new ExpressionRoleMapping(name, (RoleMapperExpression) args[0], (List) args[1], - (List) args[2], (Map) args[3], (boolean) args[4])); + static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "role-mapping", + true, + (args, name) -> new ExpressionRoleMapping( + name, + (RoleMapperExpression) args[0], + (List) args[1], + (List) args[2], + (Map) args[3], + (boolean) args[4] + ) + ); static { - PARSER.declareField(constructorArg(), (parser, context) -> RoleMapperExpressionParser.fromXContent(parser), Fields.RULES, - ObjectParser.ValueType.OBJECT); + PARSER.declareField( + constructorArg(), + (parser, context) -> RoleMapperExpressionParser.fromXContent(parser), + Fields.RULES, + ObjectParser.ValueType.OBJECT + ); PARSER.declareStringArray(optionalConstructorArg(), Fields.ROLES); PARSER.declareObjectArray(optionalConstructorArg(), (parser, ctx) -> TemplateRoleName.fromXContent(parser), Fields.ROLE_TEMPLATES); PARSER.declareField(constructorArg(), XContentParser::map, Fields.METADATA, ObjectParser.ValueType.OBJECT); @@ -62,8 +75,14 @@ public final class ExpressionRoleMapping { * to the user * @param enabled a flag when {@code true} signifies the role mapping is active */ - public ExpressionRoleMapping(final String name, final RoleMapperExpression expr, final List roles, - final List templates, final Map metadata, boolean enabled) { + public ExpressionRoleMapping( + final String name, + final RoleMapperExpression expr, + final List roles, + final List templates, + final Map metadata, + boolean enabled + ) { this.name = name; this.expression = expr; this.roles = roles == null ? Collections.emptyList() : Collections.unmodifiableList(roles); @@ -101,12 +120,12 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; final ExpressionRoleMapping that = (ExpressionRoleMapping) o; - return this.enabled == that.enabled && - Objects.equals(this.name, that.name) && - Objects.equals(this.expression, that.expression) && - Objects.equals(this.roles, that.roles) && - Objects.equals(this.roleTemplates, that.roleTemplates) && - Objects.equals(this.metadata, that.metadata); + return this.enabled == that.enabled + && Objects.equals(this.name, that.name) + && Objects.equals(this.expression, that.expression) + && Objects.equals(this.roles, that.roles) + && Objects.equals(this.roleTemplates, that.roleTemplates) + && Objects.equals(this.metadata, that.metadata); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetApiKeyRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetApiKeyRequest.java index 7c2e393223cf1..954219260c264 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetApiKeyRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetApiKeyRequest.java @@ -9,8 +9,8 @@ package org.elasticsearch.client.security; import org.elasticsearch.client.Validatable; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -32,12 +32,16 @@ private GetApiKeyRequest() { } // pkg scope for testing - GetApiKeyRequest(@Nullable String realmName, @Nullable String userName, @Nullable String apiKeyId, - @Nullable String apiKeyName, boolean ownedByAuthenticatedUser) { + GetApiKeyRequest( + @Nullable String realmName, + @Nullable String userName, + @Nullable String apiKeyId, + @Nullable String apiKeyName, + boolean ownedByAuthenticatedUser + ) { if (Strings.hasText(apiKeyId) || Strings.hasText(apiKeyName)) { if (Strings.hasText(realmName) || Strings.hasText(userName)) { - throwValidationError( - "username or realm name must not be specified when the api key id or api key name is specified"); + throwValidationError("username or realm name must not be specified when the api key id or api key name is specified"); } } if (ownedByAuthenticatedUser) { diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetApiKeyResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetApiKeyResponse.java index 80305b7a12260..09465373521cb 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetApiKeyResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetApiKeyResponse.java @@ -9,8 +9,8 @@ package org.elasticsearch.client.security; import org.elasticsearch.client.security.support.ApiKey; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetBuiltinPrivilegesRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetBuiltinPrivilegesRequest.java index 529b34bd65386..e472355ca84ce 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetBuiltinPrivilegesRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetBuiltinPrivilegesRequest.java @@ -19,8 +19,7 @@ public final class GetBuiltinPrivilegesRequest implements Validatable { public static final GetBuiltinPrivilegesRequest INSTANCE = new GetBuiltinPrivilegesRequest(); - private GetBuiltinPrivilegesRequest() { - } + private GetBuiltinPrivilegesRequest() {} public Request getRequest() { return new Request(HttpGet.METHOD_NAME, "/_security/privilege/_builtin"); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetBuiltinPrivilegesResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetBuiltinPrivilegesResponse.java index 56e6f31d554f7..8d6dd093cba58 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetBuiltinPrivilegesResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetBuiltinPrivilegesResponse.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.security; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -49,8 +49,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; GetBuiltinPrivilegesResponse that = (GetBuiltinPrivilegesResponse) o; - return Objects.equals(this.clusterPrivileges, that.clusterPrivileges) - && Objects.equals(this.indexPrivileges, that.indexPrivileges); + return Objects.equals(this.clusterPrivileges, that.clusterPrivileges) && Objects.equals(this.indexPrivileges, that.indexPrivileges); } @Override @@ -58,11 +57,12 @@ public int hashCode() { return Objects.hash(clusterPrivileges, indexPrivileges); } - @SuppressWarnings("unchecked") private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "get_builtin_privileges", true, - args -> new GetBuiltinPrivilegesResponse((Collection) args[0], (Collection) args[1])); + "get_builtin_privileges", + true, + args -> new GetBuiltinPrivilegesResponse((Collection) args[0], (Collection) args[1]) + ); static { PARSER.declareStringArray(constructorArg(), new ParseField("cluster")); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetPrivilegesRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetPrivilegesRequest.java index e263a36357620..4df6e1b7a51f4 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetPrivilegesRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetPrivilegesRequest.java @@ -9,9 +9,9 @@ package org.elasticsearch.client.security; import org.elasticsearch.client.Validatable; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.util.CollectionUtils; +import org.elasticsearch.core.Nullable; import java.util.Arrays; import java.util.Objects; @@ -69,8 +69,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; GetPrivilegesRequest that = (GetPrivilegesRequest) o; - return Objects.equals(applicationName, that.applicationName) && - Arrays.equals(privilegeNames, that.privilegeNames); + return Objects.equals(applicationName, that.applicationName) && Arrays.equals(privilegeNames, that.privilegeNames); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetPrivilegesResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetPrivilegesResponse.java index 919a2b0507508..7b6a9c4100944 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetPrivilegesResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetPrivilegesResponse.java @@ -9,8 +9,8 @@ package org.elasticsearch.client.security; import org.elasticsearch.client.security.user.privileges.ApplicationPrivilege; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParserUtils; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.ArrayList; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetRoleMappingsRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetRoleMappingsRequest.java index c2fcb7e26cba0..2af13544e7488 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetRoleMappingsRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetRoleMappingsRequest.java @@ -23,9 +23,9 @@ public final class GetRoleMappingsRequest implements Validatable { public GetRoleMappingsRequest(final String... roleMappingNames) { if (roleMappingNames != null) { - this.roleMappingNames = Collections.unmodifiableSet(Sets.newHashSet(roleMappingNames)); + this.roleMappingNames = Collections.unmodifiableSet(Sets.newHashSet(roleMappingNames)); } else { - this.roleMappingNames = Collections.emptySet(); + this.roleMappingNames = Collections.emptySet(); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetRoleMappingsResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetRoleMappingsResponse.java index 940fd18e8a849..1ef1106a0a726 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetRoleMappingsResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetRoleMappingsResponse.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.security; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParserUtils; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.ArrayList; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetRolesResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetRolesResponse.java index 9fc368ef1bba4..65b188dbcc31f 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetRolesResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetRolesResponse.java @@ -9,9 +9,9 @@ package org.elasticsearch.client.security; import org.elasticsearch.client.security.user.privileges.Role; +import org.elasticsearch.common.xcontent.XContentParserUtils; import org.elasticsearch.core.Tuple; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.XContentParserUtils; import java.io.IOException; import java.util.ArrayList; @@ -66,8 +66,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; GetRolesResponse response = (GetRolesResponse) o; - return Objects.equals(roles, response.roles) - && Objects.equals(transientMetadataMap, response.transientMetadataMap); + return Objects.equals(roles, response.roles) && Objects.equals(transientMetadataMap, response.transientMetadataMap); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetServiceAccountCredentialsRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetServiceAccountCredentialsRequest.java index a3319abd4011d..c65f1642b2ecf 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetServiceAccountCredentialsRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetServiceAccountCredentialsRequest.java @@ -35,10 +35,8 @@ public String getServiceName() { @Override public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; GetServiceAccountCredentialsRequest that = (GetServiceAccountCredentialsRequest) o; return namespace.equals(that.namespace) && serviceName.equals(that.serviceName); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetServiceAccountCredentialsResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetServiceAccountCredentialsResponse.java index e220113131b42..52fd19850899d 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetServiceAccountCredentialsResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetServiceAccountCredentialsResponse.java @@ -18,8 +18,8 @@ import java.util.List; import java.util.Objects; -import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg; import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken; +import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg; /** * Response when requesting credentials of a service account. @@ -30,9 +30,11 @@ public final class GetServiceAccountCredentialsResponse { private final List indexTokenInfos; private final ServiceAccountCredentialsNodesResponse nodesResponse; - public GetServiceAccountCredentialsResponse(String principal, - List indexTokenInfos, - ServiceAccountCredentialsNodesResponse nodesResponse) { + public GetServiceAccountCredentialsResponse( + String principal, + List indexTokenInfos, + ServiceAccountCredentialsNodesResponse nodesResponse + ) { this.principal = Objects.requireNonNull(principal, "principal is required"); this.indexTokenInfos = List.copyOf(Objects.requireNonNull(indexTokenInfos, "service token infos are required")); this.nodesResponse = Objects.requireNonNull(nodesResponse, "nodes response is required"); @@ -51,25 +53,32 @@ public ServiceAccountCredentialsNodesResponse getNodesResponse() { } @SuppressWarnings("unchecked") - static ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("get_service_account_credentials_response", - args -> { - final int count = (int) args[1]; - final List indexTokenInfos = (List) args[2]; - final ServiceAccountCredentialsNodesResponse fileTokensResponse = (ServiceAccountCredentialsNodesResponse) args[3]; - if (count != indexTokenInfos.size() + fileTokensResponse.getFileTokenInfos().size()) { - throw new IllegalArgumentException("number of tokens do not match"); - } - return new GetServiceAccountCredentialsResponse((String) args[0], indexTokenInfos, fileTokensResponse); - }); + static ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "get_service_account_credentials_response", + args -> { + final int count = (int) args[1]; + final List indexTokenInfos = (List) args[2]; + final ServiceAccountCredentialsNodesResponse fileTokensResponse = (ServiceAccountCredentialsNodesResponse) args[3]; + if (count != indexTokenInfos.size() + fileTokensResponse.getFileTokenInfos().size()) { + throw new IllegalArgumentException("number of tokens do not match"); + } + return new GetServiceAccountCredentialsResponse((String) args[0], indexTokenInfos, fileTokensResponse); + } + ); static { PARSER.declareString(constructorArg(), new ParseField("service_account")); PARSER.declareInt(constructorArg(), new ParseField("count")); - PARSER.declareObject(constructorArg(), - (p, c) -> GetServiceAccountCredentialsResponse.parseIndexTokenInfos(p), new ParseField("tokens")); - PARSER.declareObject(constructorArg(), - (p, c) -> ServiceAccountCredentialsNodesResponse.fromXContent(p), new ParseField("nodes_credentials")); + PARSER.declareObject( + constructorArg(), + (p, c) -> GetServiceAccountCredentialsResponse.parseIndexTokenInfos(p), + new ParseField("tokens") + ); + PARSER.declareObject( + constructorArg(), + (p, c) -> ServiceAccountCredentialsNodesResponse.fromXContent(p), + new ParseField("nodes_credentials") + ); } public static GetServiceAccountCredentialsResponse fromXContent(XContentParser parser) throws IOException { diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetServiceAccountsRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetServiceAccountsRequest.java index 01b2932cad7db..ae5c4910ac300 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetServiceAccountsRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetServiceAccountsRequest.java @@ -59,10 +59,8 @@ public Optional validate() { @Override public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; GetServiceAccountsRequest that = (GetServiceAccountsRequest) o; return Objects.equals(namespace, that.namespace) && Objects.equals(serviceName, that.serviceName); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetServiceAccountsResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetServiceAccountsResponse.java index 12e9ad04efcc4..5b03295d9f766 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetServiceAccountsResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetServiceAccountsResponse.java @@ -10,8 +10,8 @@ import org.elasticsearch.client.security.support.ServiceAccountInfo; import org.elasticsearch.client.security.user.privileges.Role; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParserUtils; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.ArrayList; @@ -36,10 +36,8 @@ public List getServiceAccountInfos() { @Override public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; GetServiceAccountsResponse that = (GetServiceAccountsResponse) o; return serviceAccountInfos.equals(that.serviceAccountInfos); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetSslCertificatesRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetSslCertificatesRequest.java index 59514fd5e4b70..d4423cd33e93e 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetSslCertificatesRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetSslCertificatesRequest.java @@ -19,8 +19,7 @@ public final class GetSslCertificatesRequest implements Validatable { public static final GetSslCertificatesRequest INSTANCE = new GetSslCertificatesRequest(); - private GetSslCertificatesRequest() { - } + private GetSslCertificatesRequest() {} public Request getRequest() { return new Request(HttpGet.METHOD_NAME, "/_ssl/certificates"); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetSslCertificatesResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetSslCertificatesResponse.java index eaf51958571a6..34a7cf64c8217 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetSslCertificatesResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetSslCertificatesResponse.java @@ -9,8 +9,8 @@ package org.elasticsearch.client.security; import org.elasticsearch.client.security.support.CertificateInfo; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParserUtils; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.ArrayList; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetUserPrivilegesRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetUserPrivilegesRequest.java index 2a51b10cfd2fb..3a433d2bf87a7 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetUserPrivilegesRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetUserPrivilegesRequest.java @@ -21,8 +21,7 @@ public class GetUserPrivilegesRequest implements Validatable { public static final GetUserPrivilegesRequest INSTANCE = new GetUserPrivilegesRequest(); - private GetUserPrivilegesRequest() { - } + private GetUserPrivilegesRequest() {} public Request getRequest() { return new Request(HttpGet.METHOD_NAME, "/_security/user/_privileges"); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetUserPrivilegesResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetUserPrivilegesResponse.java index 9b2667b7cdc15..f193828ff929e 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetUserPrivilegesResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetUserPrivilegesResponse.java @@ -12,8 +12,8 @@ import org.elasticsearch.client.security.user.privileges.ApplicationResourcePrivileges; import org.elasticsearch.client.security.user.privileges.GlobalPrivileges; import org.elasticsearch.client.security.user.privileges.UserIndicesPrivileges; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -32,7 +32,10 @@ public class GetUserPrivilegesResponse { private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "get_user_privileges_response", true, GetUserPrivilegesResponse::buildResponseFromParserArgs); + "get_user_privileges_response", + true, + GetUserPrivilegesResponse::buildResponseFromParserArgs + ); @SuppressWarnings("unchecked") private static GetUserPrivilegesResponse buildResponseFromParserArgs(Object[] args) { @@ -47,12 +50,17 @@ private static GetUserPrivilegesResponse buildResponseFromParserArgs(Object[] ar static { PARSER.declareStringArray(constructorArg(), new ParseField("cluster")); - PARSER.declareObjectArray(constructorArg(), (parser, ignore) -> GlobalPrivileges.fromXContent(parser), - new ParseField("global")); - PARSER.declareObjectArray(constructorArg(), (parser, ignore) -> UserIndicesPrivileges.fromXContent(parser), - new ParseField("indices")); - PARSER.declareObjectArray(constructorArg(), (parser, ignore) -> ApplicationResourcePrivileges.fromXContent(parser), - new ParseField("applications")); + PARSER.declareObjectArray(constructorArg(), (parser, ignore) -> GlobalPrivileges.fromXContent(parser), new ParseField("global")); + PARSER.declareObjectArray( + constructorArg(), + (parser, ignore) -> UserIndicesPrivileges.fromXContent(parser), + new ParseField("indices") + ); + PARSER.declareObjectArray( + constructorArg(), + (parser, ignore) -> ApplicationResourcePrivileges.fromXContent(parser), + new ParseField("applications") + ); PARSER.declareStringArray(constructorArg(), new ParseField("run_as")); } @@ -66,9 +74,13 @@ public static GetUserPrivilegesResponse fromXContent(XContentParser parser) thro private Set applicationPrivileges; private Set runAsPrivilege; - public GetUserPrivilegesResponse(Collection clusterPrivileges, Collection globalPrivileges, - Collection indicesPrivileges, - Collection applicationPrivileges, Collection runAsPrivilege) { + public GetUserPrivilegesResponse( + Collection clusterPrivileges, + Collection globalPrivileges, + Collection indicesPrivileges, + Collection applicationPrivileges, + Collection runAsPrivilege + ) { this.clusterPrivileges = Collections.unmodifiableSet(new LinkedHashSet<>(clusterPrivileges)); this.globalPrivileges = Collections.unmodifiableSet(new LinkedHashSet<>(globalPrivileges)); this.indicesPrivileges = Collections.unmodifiableSet(new LinkedHashSet<>(indicesPrivileges)); @@ -98,13 +110,18 @@ public Set getRunAsPrivilege() { @Override public String toString() { - return "GetUserPrivilegesResponse{" + - "clusterPrivileges=" + clusterPrivileges + - ", globalPrivileges=" + globalPrivileges + - ", indicesPrivileges=" + indicesPrivileges + - ", applicationPrivileges=" + applicationPrivileges + - ", runAsPrivilege=" + runAsPrivilege + - '}'; + return "GetUserPrivilegesResponse{" + + "clusterPrivileges=" + + clusterPrivileges + + ", globalPrivileges=" + + globalPrivileges + + ", indicesPrivileges=" + + indicesPrivileges + + ", applicationPrivileges=" + + applicationPrivileges + + ", runAsPrivilege=" + + runAsPrivilege + + '}'; } @Override @@ -116,11 +133,11 @@ public boolean equals(Object o) { return false; } final GetUserPrivilegesResponse that = (GetUserPrivilegesResponse) o; - return Objects.equals(this.clusterPrivileges, that.clusterPrivileges) && - Objects.equals(this.globalPrivileges, that.globalPrivileges) && - Objects.equals(this.indicesPrivileges, that.indicesPrivileges) && - Objects.equals(this.applicationPrivileges, that.applicationPrivileges) && - Objects.equals(this.runAsPrivilege, that.runAsPrivilege); + return Objects.equals(this.clusterPrivileges, that.clusterPrivileges) + && Objects.equals(this.globalPrivileges, that.globalPrivileges) + && Objects.equals(this.indicesPrivileges, that.indicesPrivileges) + && Objects.equals(this.applicationPrivileges, that.applicationPrivileges) + && Objects.equals(this.runAsPrivilege, that.runAsPrivilege); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetUsersResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetUsersResponse.java index 99c1066d0934d..5f20e3d655c86 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetUsersResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetUsersResponse.java @@ -8,12 +8,12 @@ package org.elasticsearch.client.security; import org.elasticsearch.client.security.user.User; +import org.elasticsearch.common.xcontent.XContentParserUtils; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentParser.Token; -import org.elasticsearch.common.xcontent.XContentParserUtils; import java.io.IOException; import java.util.ArrayList; @@ -91,7 +91,9 @@ public int hashCode() { public static final ParseField ENABLED = new ParseField("enabled"); @SuppressWarnings("unchecked") - public static final ConstructingObjectParser USER_PARSER = new ConstructingObjectParser<>("user_info", true, + public static final ConstructingObjectParser USER_PARSER = new ConstructingObjectParser<>( + "user_info", + true, (constructorObjects) -> { int i = 0; final String username = (String) constructorObjects[i++]; @@ -101,7 +103,8 @@ public int hashCode() { final String fullName = (String) constructorObjects[i++]; final String email = (String) constructorObjects[i++]; return new ParsedUser(username, roles, metadata, enabled, fullName, email); - }); + } + ); static { USER_PARSER.declareString(constructorArg(), USERNAME); @@ -116,13 +119,21 @@ protected static final class ParsedUser { protected User user; protected boolean enabled; - public ParsedUser(String username, List roles, Map metadata, Boolean enabled, - @Nullable String fullName, @Nullable String email) { + public ParsedUser( + String username, + List roles, + Map metadata, + Boolean enabled, + @Nullable String fullName, + @Nullable String email + ) { String checkedUsername = Objects.requireNonNull(username, "`username` is required, cannot be null"); - List checkedRoles = - List.copyOf(Objects.requireNonNull(roles, "`roles` is required, cannot be null. Pass an empty list instead.")); + List checkedRoles = List.copyOf( + Objects.requireNonNull(roles, "`roles` is required, cannot be null. Pass an empty list instead.") + ); Map checkedMetadata = Collections.unmodifiableMap( - Objects.requireNonNull(metadata, "`metadata` is required, cannot be null. Pass an empty map instead.")); + Objects.requireNonNull(metadata, "`metadata` is required, cannot be null. Pass an empty map instead.") + ); this.user = new User(checkedUsername, checkedRoles, checkedMetadata, fullName, email); this.enabled = enabled; } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GrantApiKeyRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GrantApiKeyRequest.java index 530424a02b525..be976d7d00413 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GrantApiKeyRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/GrantApiKeyRequest.java @@ -44,16 +44,12 @@ public static Grant passwordGrant(String username, char[] password) { "password", Objects.requireNonNull(username, "Username may not be null"), Objects.requireNonNull(password, "Password may not be null"), - null); + null + ); } public static Grant accessTokenGrant(String accessToken) { - return new Grant( - "access_token", - null, - null, - Objects.requireNonNull(accessToken, "Access token may not be null") - ); + return new Grant("access_token", null, null, Objects.requireNonNull(accessToken, "Access token may not be null")); } public String getGrantType() { @@ -145,8 +141,7 @@ public boolean equals(Object o) { return false; } final GrantApiKeyRequest that = (GrantApiKeyRequest) o; - return Objects.equals(this.grant, that.grant) - && Objects.equals(this.apiKeyRequest, that.apiKeyRequest); + return Objects.equals(this.grant, that.grant) && Objects.equals(this.apiKeyRequest, that.apiKeyRequest); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/HasPrivilegesRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/HasPrivilegesRequest.java index bc8ecf46d1e68..fd1ef17dd6fc1 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/HasPrivilegesRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/HasPrivilegesRequest.java @@ -31,9 +31,11 @@ public final class HasPrivilegesRequest implements Validatable, ToXContentObject private final Set indexPrivileges; private final Set applicationPrivileges; - public HasPrivilegesRequest(@Nullable Set clusterPrivileges, - @Nullable Set indexPrivileges, - @Nullable Set applicationPrivileges) { + public HasPrivilegesRequest( + @Nullable Set clusterPrivileges, + @Nullable Set indexPrivileges, + @Nullable Set applicationPrivileges + ) { this.clusterPrivileges = clusterPrivileges == null ? emptySet() : unmodifiableSet(clusterPrivileges); this.indexPrivileges = indexPrivileges == null ? emptySet() : unmodifiableSet(indexPrivileges); this.applicationPrivileges = applicationPrivileges == null ? emptySet() : unmodifiableSet(applicationPrivileges); @@ -73,9 +75,9 @@ public boolean equals(Object o) { return false; } final HasPrivilegesRequest that = (HasPrivilegesRequest) o; - return Objects.equals(clusterPrivileges, that.clusterPrivileges) && - Objects.equals(indexPrivileges, that.indexPrivileges) && - Objects.equals(applicationPrivileges, that.applicationPrivileges); + return Objects.equals(clusterPrivileges, that.clusterPrivileges) + && Objects.equals(indexPrivileges, that.indexPrivileges) + && Objects.equals(applicationPrivileges, that.applicationPrivileges); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/HasPrivilegesResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/HasPrivilegesResponse.java index 10abb5d247f65..ef83aef3cc9e2 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/HasPrivilegesResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/HasPrivilegesResponse.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.security; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -28,8 +28,16 @@ public final class HasPrivilegesResponse { private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "has_privileges_response", true, args -> new HasPrivilegesResponse( - (String) args[0], (Boolean) args[1], checkMap(args[2], 0), checkMap(args[3], 1), checkMap(args[4], 2))); + "has_privileges_response", + true, + args -> new HasPrivilegesResponse( + (String) args[0], + (Boolean) args[1], + checkMap(args[2], 0), + checkMap(args[3], 1), + checkMap(args[4], 2) + ) + ); static { PARSER.declareString(constructorArg(), new ParseField("username")); @@ -44,11 +52,10 @@ private static Map checkMap(Object argument, int depth) { if (argument instanceof Map) { Map map = (Map) argument; if (depth == 0) { - map.values().stream() + map.values() + .stream() .filter(val -> (val instanceof Boolean) == false) - .forEach(val -> { - throw new IllegalArgumentException("Map value [" + val + "] in [" + map + "] is not a Boolean"); - }); + .forEach(val -> { throw new IllegalArgumentException("Map value [" + val + "] in [" + map + "] is not a Boolean"); }); } else { map.values().stream().forEach(val -> checkMap(val, depth - 1)); } @@ -67,10 +74,13 @@ private static void declareMap(BiConsumer> indexPrivileges; private final Map>> applicationPrivileges; - public HasPrivilegesResponse(String username, boolean hasAllRequested, - Map clusterPrivileges, - Map> indexPrivileges, - Map>> applicationPrivileges) { + public HasPrivilegesResponse( + String username, + boolean hasAllRequested, + Map clusterPrivileges, + Map> indexPrivileges, + Map>> applicationPrivileges + ) { this.username = username; this.hasAllRequested = hasAllRequested; this.clusterPrivileges = Collections.unmodifiableMap(clusterPrivileges); @@ -85,7 +95,8 @@ private static Map> unmodifiableMap2(final Map>> unmodifiableMap3( - final Map>> map) { + final Map>> map + ) { final Map>> copy = new HashMap<>(map); copy.replaceAll((k, v) -> unmodifiableMap2(v)); return Collections.unmodifiableMap(copy); @@ -144,8 +155,9 @@ public boolean hasIndexPrivilege(String indexName, String privilegeName) { } Boolean has = indexPrivileges.get(privilegeName); if (has == null) { - throw new IllegalArgumentException("Privilege [" + privilegeName + "] was not included in the response for index [" - + indexName + "]"); + throw new IllegalArgumentException( + "Privilege [" + privilegeName + "] was not included in the response for index [" + indexName + "]" + ); } return has; } @@ -171,13 +183,21 @@ public boolean hasApplicationPrivilege(String applicationName, String resourceNa } final Map resourcePrivileges = appPrivileges.get(resourceName); if (resourcePrivileges == null) { - throw new IllegalArgumentException("No privileges for resource [" + resourceName + - "] were included in the response for application [" + applicationName + "]"); + throw new IllegalArgumentException( + "No privileges for resource [" + resourceName + "] were included in the response for application [" + applicationName + "]" + ); } Boolean has = resourcePrivileges.get(privilegeName); if (has == null) { - throw new IllegalArgumentException("Privilege [" + privilegeName + "] was not included in the response for application [" + - applicationName + "] and resource [" + resourceName + "]"); + throw new IllegalArgumentException( + "Privilege [" + + privilegeName + + "] was not included in the response for application [" + + applicationName + + "] and resource [" + + resourceName + + "]" + ); } return has; } @@ -226,11 +246,11 @@ public boolean equals(Object o) { return false; } final HasPrivilegesResponse that = (HasPrivilegesResponse) o; - return this.hasAllRequested == that.hasAllRequested && - Objects.equals(this.username, that.username) && - Objects.equals(this.clusterPrivileges, that.clusterPrivileges) && - Objects.equals(this.indexPrivileges, that.indexPrivileges) && - Objects.equals(this.applicationPrivileges, that.applicationPrivileges); + return this.hasAllRequested == that.hasAllRequested + && Objects.equals(this.username, that.username) + && Objects.equals(this.clusterPrivileges, that.clusterPrivileges) + && Objects.equals(this.indexPrivileges, that.indexPrivileges) + && Objects.equals(this.applicationPrivileges, that.applicationPrivileges); } @Override @@ -238,4 +258,3 @@ public int hashCode() { return Objects.hash(username, hasAllRequested, clusterPrivileges, indexPrivileges, applicationPrivileges); } } - diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/InvalidateApiKeyRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/InvalidateApiKeyRequest.java index e675068007193..037c47260d291 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/InvalidateApiKeyRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/InvalidateApiKeyRequest.java @@ -9,8 +9,8 @@ package org.elasticsearch.client.security; import org.elasticsearch.client.Validatable; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -30,17 +30,24 @@ public final class InvalidateApiKeyRequest implements Validatable, ToXContentObj private final String name; private final boolean ownedByAuthenticatedUser; - InvalidateApiKeyRequest(@Nullable String realmName, @Nullable String userName, - @Nullable String apiKeyName, boolean ownedByAuthenticatedUser, @Nullable List apiKeyIds) { + InvalidateApiKeyRequest( + @Nullable String realmName, + @Nullable String userName, + @Nullable String apiKeyName, + boolean ownedByAuthenticatedUser, + @Nullable List apiKeyIds + ) { validateApiKeyIds(apiKeyIds); - if (Strings.hasText(realmName) == false && Strings.hasText(userName) == false && apiKeyIds == null - && Strings.hasText(apiKeyName) == false && ownedByAuthenticatedUser == false) { + if (Strings.hasText(realmName) == false + && Strings.hasText(userName) == false + && apiKeyIds == null + && Strings.hasText(apiKeyName) == false + && ownedByAuthenticatedUser == false) { throwValidationError("One of [api key id(s), api key name, username, realm name] must be specified if [owner] flag is false"); } if (apiKeyIds != null || Strings.hasText(apiKeyName)) { if (Strings.hasText(realmName) || Strings.hasText(userName)) { - throwValidationError( - "username or realm name must not be specified when the api key id(s) or api key name is specified"); + throwValidationError("username or realm name must not be specified when the api key id(s) or api key name is specified"); } } if (ownedByAuthenticatedUser) { @@ -64,12 +71,17 @@ private void validateApiKeyIds(@Nullable List apiKeyIds) { throwValidationError("Argument [apiKeyIds] cannot be an empty array"); } else { final int[] idxOfBlankIds = IntStream.range(0, apiKeyIds.size()) - .filter(i -> Strings.hasText(apiKeyIds.get(i)) == false).toArray(); + .filter(i -> Strings.hasText(apiKeyIds.get(i)) == false) + .toArray(); if (idxOfBlankIds.length > 0) { - throwValidationError("Argument [apiKeyIds] must not contain blank id, but got blank " - + (idxOfBlankIds.length == 1 ? "id" : "ids") + " at index " - + (idxOfBlankIds.length == 1 ? "position" : "positions") + ": " - + Arrays.toString(idxOfBlankIds)); + throwValidationError( + "Argument [apiKeyIds] must not contain blank id, but got blank " + + (idxOfBlankIds.length == 1 ? "id" : "ids") + + " at index " + + (idxOfBlankIds.length == 1 ? "position" : "positions") + + ": " + + Arrays.toString(idxOfBlankIds) + ); } } } @@ -94,8 +106,9 @@ public String getId() { } else if (ids.size() == 1) { return ids.get(0); } else { - throw new IllegalArgumentException("Cannot get a single api key id when multiple ids have been set [" - + Strings.collectionToCommaDelimitedString(ids) + "]"); + throw new IllegalArgumentException( + "Cannot get a single api key id when multiple ids have been set [" + Strings.collectionToCommaDelimitedString(ids) + "]" + ); } } @@ -126,7 +139,7 @@ public static InvalidateApiKeyRequest usingRealmName(String realmName) { * @return {@link InvalidateApiKeyRequest} */ public static InvalidateApiKeyRequest usingUserName(String userName) { - return new InvalidateApiKeyRequest(null, userName, null, false, null); + return new InvalidateApiKeyRequest(null, userName, null, false, null); } /** diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/InvalidateApiKeyResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/InvalidateApiKeyResponse.java index 08f1741b2f6e1..941b0b2af431e 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/InvalidateApiKeyResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/InvalidateApiKeyResponse.java @@ -10,8 +10,8 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -34,11 +34,16 @@ public final class InvalidateApiKeyResponse { * @param previouslyInvalidatedApiKeys list of previously invalidated API key ids * @param errors list of encountered errors while invalidating API keys */ - public InvalidateApiKeyResponse(List invalidatedApiKeys, List previouslyInvalidatedApiKeys, - @Nullable List errors) { + public InvalidateApiKeyResponse( + List invalidatedApiKeys, + List previouslyInvalidatedApiKeys, + @Nullable List errors + ) { this.invalidatedApiKeys = Objects.requireNonNull(invalidatedApiKeys, "invalidated_api_keys must be provided"); - this.previouslyInvalidatedApiKeys = Objects.requireNonNull(previouslyInvalidatedApiKeys, - "previously_invalidated_api_keys must be provided"); + this.previouslyInvalidatedApiKeys = Objects.requireNonNull( + previouslyInvalidatedApiKeys, + "previously_invalidated_api_keys must be provided" + ); if (null != errors) { this.errors = errors; } else { @@ -74,8 +79,11 @@ public List getErrors() { PARSER.declareStringArray(constructorArg(), new ParseField("previously_invalidated_api_keys")); // error count is parsed but ignored as we have list of errors PARSER.declareInt(constructorArg(), new ParseField("error_count")); - PARSER.declareObjectArray(optionalConstructorArg(), (p, c) -> ElasticsearchException.fromXContent(p), - new ParseField("error_details")); + PARSER.declareObjectArray( + optionalConstructorArg(), + (p, c) -> ElasticsearchException.fromXContent(p), + new ParseField("error_details") + ); } public static InvalidateApiKeyResponse fromXContent(XContentParser parser) throws IOException { @@ -100,13 +108,18 @@ public boolean equals(Object obj) { } InvalidateApiKeyResponse other = (InvalidateApiKeyResponse) obj; return Objects.equals(invalidatedApiKeys, other.invalidatedApiKeys) - && Objects.equals(previouslyInvalidatedApiKeys, other.previouslyInvalidatedApiKeys) - && Objects.equals(errors, other.errors); + && Objects.equals(previouslyInvalidatedApiKeys, other.previouslyInvalidatedApiKeys) + && Objects.equals(errors, other.errors); } @Override public String toString() { - return "ApiKeysInvalidationResult [invalidatedApiKeys=" + invalidatedApiKeys + ", previouslyInvalidatedApiKeys=" - + previouslyInvalidatedApiKeys + ", errors=" + errors + "]"; + return "ApiKeysInvalidationResult [invalidatedApiKeys=" + + invalidatedApiKeys + + ", previouslyInvalidatedApiKeys=" + + previouslyInvalidatedApiKeys + + ", errors=" + + errors + + "]"; } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/InvalidateTokenRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/InvalidateTokenRequest.java index edf8549c3a2ba..f6c22eb7319c5 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/InvalidateTokenRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/InvalidateTokenRequest.java @@ -9,8 +9,8 @@ package org.elasticsearch.client.security; import org.elasticsearch.client.Validatable; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -31,8 +31,12 @@ public final class InvalidateTokenRequest implements Validatable, ToXContentObje this(accessToken, refreshToken, null, null); } - public InvalidateTokenRequest(@Nullable String accessToken, @Nullable String refreshToken, - @Nullable String realmName, @Nullable String username) { + public InvalidateTokenRequest( + @Nullable String accessToken, + @Nullable String refreshToken, + @Nullable String realmName, + @Nullable String username + ) { if (Strings.hasText(realmName) || Strings.hasText(username)) { if (Strings.hasText(accessToken)) { throw new IllegalArgumentException("access token is not allowed when realm name or username are specified"); @@ -43,8 +47,9 @@ public InvalidateTokenRequest(@Nullable String accessToken, @Nullable String ref } else { if (Strings.isNullOrEmpty(accessToken)) { if (Strings.isNullOrEmpty(refreshToken)) { - throw new IllegalArgumentException("Either access token or refresh token is required when neither realm name or " + - "username are specified"); + throw new IllegalArgumentException( + "Either access token or refresh token is required when neither realm name or " + "username are specified" + ); } } else if (Strings.isNullOrEmpty(refreshToken) == false) { throw new IllegalArgumentException("Cannot supply both access token and refresh token"); @@ -123,10 +128,10 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; InvalidateTokenRequest that = (InvalidateTokenRequest) o; - return Objects.equals(accessToken, that.accessToken) && - Objects.equals(refreshToken, that.refreshToken) && - Objects.equals(realmName, that.realmName) && - Objects.equals(username, that.username); + return Objects.equals(accessToken, that.accessToken) + && Objects.equals(refreshToken, that.refreshToken) + && Objects.equals(realmName, that.realmName) + && Objects.equals(username, that.username); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/InvalidateTokenResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/InvalidateTokenResponse.java index 5b1c5aec81092..1f25402a3dd8e 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/InvalidateTokenResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/InvalidateTokenResponse.java @@ -9,11 +9,11 @@ package org.elasticsearch.client.security; import org.elasticsearch.ElasticsearchException; +import org.elasticsearch.common.xcontent.XContentParserUtils; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.XContentParserUtils; import java.io.IOException; import java.util.Collections; @@ -42,9 +42,11 @@ public final class InvalidateTokenResponse { @SuppressWarnings("unchecked") private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "tokens_invalidation_result", true, + "tokens_invalidation_result", + true, // we parse but do not use the count of errors as we implicitly have this in the size of the Exceptions list - args -> new InvalidateTokenResponse((int) args[0], (int) args[1], (List) args[3])); + args -> new InvalidateTokenResponse((int) args[0], (int) args[1], (List) args[3]) + ); static { PARSER.declareInt(constructorArg(), INVALIDATED_TOKENS); @@ -85,9 +87,9 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; InvalidateTokenResponse that = (InvalidateTokenResponse) o; - return invalidatedTokens == that.invalidatedTokens && - previouslyInvalidatedTokens == that.previouslyInvalidatedTokens && - Objects.equals(errors, that.errors); + return invalidatedTokens == that.invalidatedTokens + && previouslyInvalidatedTokens == that.previouslyInvalidatedTokens + && Objects.equals(errors, that.errors); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/KibanaEnrollmentRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/KibanaEnrollmentRequest.java index a667e2b759db7..427be9e81b374 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/KibanaEnrollmentRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/KibanaEnrollmentRequest.java @@ -15,8 +15,8 @@ public final class KibanaEnrollmentRequest implements Validatable { public static final KibanaEnrollmentRequest INSTANCE = new KibanaEnrollmentRequest(); - private KibanaEnrollmentRequest() { - } + + private KibanaEnrollmentRequest() {} public Request getRequest() { return new Request(HttpGet.METHOD_NAME, "/_security/enroll/kibana"); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/KibanaEnrollmentResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/KibanaEnrollmentResponse.java index 105a55fa0ec6e..5ead42d340b04 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/KibanaEnrollmentResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/KibanaEnrollmentResponse.java @@ -30,9 +30,13 @@ public KibanaEnrollmentResponse(String tokenName, SecureString tokenValue, Strin this.httpCa = httpCa; } - public String getTokenName() { return tokenName; } + public String getTokenName() { + return tokenName; + } - public SecureString getTokenValue() { return tokenValue; } + public SecureString getTokenValue() { + return tokenValue; + } public String getHttpCa() { return httpCa; @@ -44,17 +48,19 @@ public String getHttpCa() { private static final ParseField HTTP_CA = new ParseField("http_ca"); static final ConstructingObjectParser TOKEN_PARSER = new ConstructingObjectParser<>( - KibanaEnrollmentResponse.class.getName(), true, + KibanaEnrollmentResponse.class.getName(), + true, a -> new Token((String) a[0], (String) a[1]) ); - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>( - KibanaEnrollmentResponse.class.getName(), true, - a -> { - final Token token = (Token) a[0]; - return new KibanaEnrollmentResponse(token.name, new SecureString(token.value.toCharArray()), (String) a[1]); - }); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + KibanaEnrollmentResponse.class.getName(), + true, + a -> { + final Token token = (Token) a[0]; + return new KibanaEnrollmentResponse(token.name, new SecureString(token.value.toCharArray()), (String) a[1]); + } + ); static { TOKEN_PARSER.declareString(constructorArg(), TOKEN_NAME); @@ -67,14 +73,16 @@ public static KibanaEnrollmentResponse fromXContent(XContentParser parser) throw return PARSER.apply(parser, null); } - @Override public boolean equals(Object o) { + @Override + public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; KibanaEnrollmentResponse that = (KibanaEnrollmentResponse) o; return tokenName.equals(that.tokenName) && tokenValue.equals(that.tokenValue) && httpCa.equals(that.httpCa); } - @Override public int hashCode() { + @Override + public int hashCode() { return Objects.hash(tokenName, tokenValue, httpCa); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/NodeEnrollmentRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/NodeEnrollmentRequest.java index f618f702af25b..5a9c18d15e03d 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/NodeEnrollmentRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/NodeEnrollmentRequest.java @@ -19,7 +19,7 @@ public final class NodeEnrollmentRequest implements Validatable { public static final NodeEnrollmentRequest INSTANCE = new NodeEnrollmentRequest(); - private NodeEnrollmentRequest(){ + private NodeEnrollmentRequest() { } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/NodeEnrollmentResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/NodeEnrollmentResponse.java index 602252f22753b..e49e71d045f54 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/NodeEnrollmentResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/NodeEnrollmentResponse.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.security; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -26,8 +26,14 @@ public class NodeEnrollmentResponse { private final String transportCert; private final List nodesAddresses; - public NodeEnrollmentResponse(String httpCaKey, String httpCaCert, String transportCaCert, String transportKey, String transportCert, - List nodesAddresses){ + public NodeEnrollmentResponse( + String httpCaKey, + String httpCaCert, + String transportCaCert, + String transportKey, + String transportCert, + List nodesAddresses + ) { this.httpCaKey = httpCaKey; this.httpCaCert = httpCaCert; this.transportCaCert = transportCaCert; @@ -68,9 +74,10 @@ public List getNodesAddresses() { private static final ParseField NODES_ADDRESSES = new ParseField("nodes_addresses"); @SuppressWarnings("unchecked") - public static final ConstructingObjectParser - PARSER = - new ConstructingObjectParser<>(NodeEnrollmentResponse.class.getName(), true, a -> { + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NodeEnrollmentResponse.class.getName(), + true, + a -> { final String httpCaKey = (String) a[0]; final String httpCaCert = (String) a[1]; final String transportCaCert = (String) a[2]; @@ -78,7 +85,8 @@ public List getNodesAddresses() { final String transportCert = (String) a[4]; final List nodesAddresses = (List) a[5]; return new NodeEnrollmentResponse(httpCaKey, httpCaCert, transportCaCert, transportKey, transportCert, nodesAddresses); - }); + } + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), HTTP_CA_KEY); @@ -93,7 +101,8 @@ public static NodeEnrollmentResponse fromXContent(XContentParser parser) throws return PARSER.apply(parser, null); } - @Override public boolean equals(Object o) { + @Override + public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; NodeEnrollmentResponse that = (NodeEnrollmentResponse) o; @@ -105,7 +114,8 @@ public static NodeEnrollmentResponse fromXContent(XContentParser parser) throws && nodesAddresses.equals(that.nodesAddresses); } - @Override public int hashCode() { + @Override + public int hashCode() { return Objects.hash(httpCaKey, httpCaCert, transportCaCert, transportKey, transportCert, nodesAddresses); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/PutPrivilegesRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/PutPrivilegesRequest.java index d494f4bb98d61..22a3272a3aef9 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/PutPrivilegesRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/PutPrivilegesRequest.java @@ -35,8 +35,9 @@ public PutPrivilegesRequest(final List privileges, @Nullab if (privileges == null || privileges.isEmpty()) { throw new IllegalArgumentException("privileges are required"); } - this.privileges = Collections.unmodifiableMap(privileges.stream() - .collect(Collectors.groupingBy(ApplicationPrivilege::getApplication, TreeMap::new, Collectors.toList()))); + this.privileges = Collections.unmodifiableMap( + privileges.stream().collect(Collectors.groupingBy(ApplicationPrivilege::getApplication, TreeMap::new, Collectors.toList())) + ); this.refreshPolicy = refreshPolicy == null ? RefreshPolicy.IMMEDIATE : refreshPolicy; } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/PutPrivilegesResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/PutPrivilegesResponse.java index e1ed303975c7c..1f923af835b15 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/PutPrivilegesResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/PutPrivilegesResponse.java @@ -53,7 +53,7 @@ public boolean wasCreated(final String applicationName, final String privilegeNa throw new IllegalArgumentException("privilege name is required"); } if (applicationPrivilegesCreatedOrUpdated.get(applicationName) == null - || applicationPrivilegesCreatedOrUpdated.get(applicationName).get(privilegeName) == null) { + || applicationPrivilegesCreatedOrUpdated.get(applicationName).get(privilegeName) == null) { throw new IllegalArgumentException("application name or privilege name not found in the response"); } return applicationPrivilegesCreatedOrUpdated.get(applicationName).get(privilegeName); @@ -69,8 +69,10 @@ public static PutPrivilegesResponse fromXContent(final XContentParser parser) th final Map appNameToPrivStatus = parser.map(); for (Entry entry : appNameToPrivStatus.entrySet()) { if (entry.getValue() instanceof Map) { - final Map privilegeToStatus = applicationPrivilegesCreatedOrUpdated.computeIfAbsent(entry.getKey(), - (a) -> new HashMap<>()); + final Map privilegeToStatus = applicationPrivilegesCreatedOrUpdated.computeIfAbsent( + entry.getKey(), + (a) -> new HashMap<>() + ); final Map createdOrUpdated = (Map) entry.getValue(); for (String privilegeName : createdOrUpdated.keySet()) { if (createdOrUpdated.get(privilegeName) instanceof Map) { diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/PutRoleMappingRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/PutRoleMappingRequest.java index bdeba45f55b6e..cb690ba1cb12d 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/PutRoleMappingRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/PutRoleMappingRequest.java @@ -10,8 +10,8 @@ import org.elasticsearch.client.Validatable; import org.elasticsearch.client.security.support.expressiondsl.RoleMapperExpression; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -36,14 +36,26 @@ public final class PutRoleMappingRequest implements Validatable, ToXContentObjec private final RefreshPolicy refreshPolicy; @Deprecated - public PutRoleMappingRequest(final String name, final boolean enabled, final List roles, final RoleMapperExpression rules, - @Nullable final Map metadata, @Nullable final RefreshPolicy refreshPolicy) { + public PutRoleMappingRequest( + final String name, + final boolean enabled, + final List roles, + final RoleMapperExpression rules, + @Nullable final Map metadata, + @Nullable final RefreshPolicy refreshPolicy + ) { this(name, enabled, roles, Collections.emptyList(), rules, metadata, refreshPolicy); } - public PutRoleMappingRequest(final String name, final boolean enabled, final List roles, final List templates, - final RoleMapperExpression rules, @Nullable final Map metadata, - @Nullable final RefreshPolicy refreshPolicy) { + public PutRoleMappingRequest( + final String name, + final boolean enabled, + final List roles, + final List templates, + final RoleMapperExpression rules, + @Nullable final Map metadata, + @Nullable final RefreshPolicy refreshPolicy + ) { if (Strings.hasText(name) == false) { throw new IllegalArgumentException("role-mapping name is missing"); } @@ -108,13 +120,13 @@ public boolean equals(Object obj) { } final PutRoleMappingRequest other = (PutRoleMappingRequest) obj; - return (enabled == other.enabled) && - (refreshPolicy == other.refreshPolicy) && - Objects.equals(name, other.name) && - Objects.equals(roles, other.roles) && - Objects.equals(roleTemplates, other.roleTemplates) && - Objects.equals(rules, other.rules) && - Objects.equals(metadata, other.metadata); + return (enabled == other.enabled) + && (refreshPolicy == other.refreshPolicy) + && Objects.equals(name, other.name) + && Objects.equals(roles, other.roles) + && Objects.equals(roleTemplates, other.roleTemplates) + && Objects.equals(rules, other.rules) + && Objects.equals(metadata, other.metadata); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/PutRoleMappingResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/PutRoleMappingResponse.java index 32d8b0851c1b8..08562b1556f37 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/PutRoleMappingResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/PutRoleMappingResponse.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.security; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -51,10 +51,16 @@ public int hashCode() { } private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "put_role_mapping_response", true, args -> new PutRoleMappingResponse((boolean) args[0])); + "put_role_mapping_response", + true, + args -> new PutRoleMappingResponse((boolean) args[0]) + ); static { ConstructingObjectParser roleMappingParser = new ConstructingObjectParser<>( - "put_role_mapping_response.role_mapping", true, args -> (Boolean) args[0]); + "put_role_mapping_response.role_mapping", + true, + args -> (Boolean) args[0] + ); roleMappingParser.declareBoolean(constructorArg(), new ParseField("created")); PARSER.declareObject(constructorArg(), roleMappingParser::parse, new ParseField("role_mapping")); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/PutRoleRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/PutRoleRequest.java index 4c0220a5790bd..cea3f16a27af7 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/PutRoleRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/PutRoleRequest.java @@ -56,8 +56,7 @@ public boolean equals(Object obj) { } final PutRoleRequest other = (PutRoleRequest) obj; - return (refreshPolicy == other.getRefreshPolicy()) && - Objects.equals(role, other.role); + return (refreshPolicy == other.getRefreshPolicy()) && Objects.equals(role, other.role); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/PutRoleResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/PutRoleResponse.java index 958f20e3e41b0..e84e06dd94221 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/PutRoleResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/PutRoleResponse.java @@ -8,17 +8,17 @@ package org.elasticsearch.client.security; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentParser.Token; import java.io.IOException; import java.util.Objects; -import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg; import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken; import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureFieldName; +import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg; /** * Response when adding a role to the native roles store. Returns a @@ -49,8 +49,11 @@ public int hashCode() { return Objects.hash(created); } - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("put_role_response", - true, args -> new PutRoleResponse((boolean) args[0])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "put_role_response", + true, + args -> new PutRoleResponse((boolean) args[0]) + ); static { PARSER.declareBoolean(constructorArg(), new ParseField("created")); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/PutUserRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/PutUserRequest.java index 67ec1b5a44139..28ffa7bb10da6 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/PutUserRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/PutUserRequest.java @@ -96,8 +96,13 @@ public PutUserRequest(User user, @Nullable char[] password, boolean enabled, @Nu * @param enabled true if the user is enabled and allowed to access elasticsearch * @param refreshPolicy the refresh policy for the request. */ - private PutUserRequest(User user, @Nullable char[] password, @Nullable char[] passwordHash, boolean enabled, - RefreshPolicy refreshPolicy) { + private PutUserRequest( + User user, + @Nullable char[] password, + @Nullable char[] passwordHash, + boolean enabled, + RefreshPolicy refreshPolicy + ) { this.user = Objects.requireNonNull(user, "user is required, cannot be null"); if (password != null && passwordHash != null) { throw new IllegalArgumentException("cannot specify both password and passwordHash"); @@ -130,10 +135,10 @@ public boolean equals(Object o) { if (o == null || getClass() != o.getClass()) return false; final PutUserRequest that = (PutUserRequest) o; return Objects.equals(user, that.user) - && Arrays.equals(password, that.password) - && Arrays.equals(passwordHash, that.passwordHash) - && enabled == that.enabled - && refreshPolicy == that.refreshPolicy; + && Arrays.equals(password, that.password) + && Arrays.equals(passwordHash, that.passwordHash) + && enabled == that.enabled + && refreshPolicy == that.refreshPolicy; } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/PutUserResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/PutUserResponse.java index 24ed7d3764dfb..b03de98390158 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/PutUserResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/PutUserResponse.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.security; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -46,8 +46,11 @@ public int hashCode() { return Objects.hash(created); } - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("put_user_response", - true, args -> new PutUserResponse((boolean) args[0])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "put_user_response", + true, + args -> new PutUserResponse((boolean) args[0]) + ); static { PARSER.declareBoolean(constructorArg(), new ParseField("created")); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/QueryApiKeyRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/QueryApiKeyRequest.java index 50bf38beb33de..f6441f5ae1202 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/QueryApiKeyRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/QueryApiKeyRequest.java @@ -10,12 +10,12 @@ import org.elasticsearch.client.Validatable; import org.elasticsearch.client.ValidationException; -import org.elasticsearch.xcontent.ToXContentObject; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.core.Nullable; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.search.searchafter.SearchAfterBuilder; import org.elasticsearch.search.sort.FieldSortBuilder; +import org.elasticsearch.xcontent.ToXContentObject; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.List; @@ -42,7 +42,8 @@ public QueryApiKeyRequest( @Nullable Integer from, @Nullable Integer size, @Nullable List fieldSortBuilders, - @Nullable SearchAfterBuilder searchAfterBuilder) { + @Nullable SearchAfterBuilder searchAfterBuilder + ) { this.queryBuilder = queryBuilder; this.from = from; this.size = size; @@ -131,16 +132,14 @@ public Optional validate() { @Override public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; QueryApiKeyRequest that = (QueryApiKeyRequest) o; - return Objects.equals(queryBuilder, that.queryBuilder) && Objects.equals(from, that.from) && Objects.equals( - size, - that.size) && Objects.equals(fieldSortBuilders, that.fieldSortBuilders) && Objects.equals( - searchAfterBuilder, - that.searchAfterBuilder); + return Objects.equals(queryBuilder, that.queryBuilder) + && Objects.equals(from, that.from) + && Objects.equals(size, that.size) + && Objects.equals(fieldSortBuilders, that.fieldSortBuilders) + && Objects.equals(searchAfterBuilder, that.searchAfterBuilder); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/QueryApiKeyResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/QueryApiKeyResponse.java index 2ebdf7b515ea8..dfcee83bbcb65 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/QueryApiKeyResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/QueryApiKeyResponse.java @@ -53,8 +53,7 @@ public static QueryApiKeyResponse fromXContent(XContentParser parser) throws IOE @SuppressWarnings("unchecked") final List items = (List) args[2]; if (count != items.size()) { - throw new IllegalArgumentException("count [" + count + "] is not equal to number of items [" - + items.size() + "]"); + throw new IllegalArgumentException("count [" + count + "] is not equal to number of items [" + items.size() + "]"); } return new QueryApiKeyResponse(total, items); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/SecurityNodesResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/SecurityNodesResponse.java index f969a0ec844de..18d711280f054 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/SecurityNodesResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/SecurityNodesResponse.java @@ -10,8 +10,8 @@ import org.elasticsearch.client.NodesResponse; import org.elasticsearch.client.NodesResponseHeader; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import java.util.List; @@ -35,9 +35,11 @@ public List getNodes() { public static class Node { - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("clear_roles_cache_response_node", false, - (args, id) -> new ClearRolesCacheResponse.Node(id, (String) args[0])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "clear_roles_cache_response_node", + false, + (args, id) -> new ClearRolesCacheResponse.Node(id, (String) args[0]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), new ParseField("name")); @@ -61,8 +63,11 @@ public String getName() { } public static void declareCommonNodesResponseParsing(ConstructingObjectParser parser) { - parser.declareNamedObjects(ConstructingObjectParser.constructorArg(), (p, c, n) -> Node.PARSER.apply(p, n), - new ParseField("nodes")); + parser.declareNamedObjects( + ConstructingObjectParser.constructorArg(), + (p, c, n) -> Node.PARSER.apply(p, n), + new ParseField("nodes") + ); NodesResponse.declareCommonNodesResponseParsing(parser); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/ServiceAccountCredentialsNodesResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/ServiceAccountCredentialsNodesResponse.java index db74e62c44b55..ce716d964f977 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/ServiceAccountCredentialsNodesResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/ServiceAccountCredentialsNodesResponse.java @@ -10,8 +10,8 @@ import org.elasticsearch.client.NodesResponseHeader; import org.elasticsearch.client.security.support.ServiceTokenInfo; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParserUtils; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.ArrayList; @@ -25,8 +25,7 @@ public class ServiceAccountCredentialsNodesResponse { private final NodesResponseHeader header; private final List fileTokenInfos; - public ServiceAccountCredentialsNodesResponse( - NodesResponseHeader header, List fileTokenInfos) { + public ServiceAccountCredentialsNodesResponse(NodesResponseHeader header, List fileTokenInfos) { this.header = header; this.fileTokenInfos = fileTokenInfos; } @@ -55,8 +54,9 @@ public static ServiceAccountCredentialsNodesResponse fromXContent(XContentParser } else if ("file_tokens".equals(parser.currentName())) { fileTokenInfos = parseFileToken(parser); } else { - throw new IllegalArgumentException("expecting field of either [_nodes] or [file_tokens], found [" - + parser.currentName() + "]"); + throw new IllegalArgumentException( + "expecting field of either [_nodes] or [file_tokens], found [" + parser.currentName() + "]" + ); } } return new ServiceAccountCredentialsNodesResponse(header, fileTokenInfos); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/TemplateRoleName.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/TemplateRoleName.java index a3e9afea8b842..a255b92787eac 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/TemplateRoleName.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/TemplateRoleName.java @@ -8,14 +8,14 @@ package org.elasticsearch.client.security; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; +import org.elasticsearch.common.xcontent.XContentParserUtils; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.XContentParserUtils; import org.elasticsearch.xcontent.XContentType; import java.io.IOException; @@ -30,8 +30,11 @@ */ public class TemplateRoleName implements ToXContentObject { - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("template-role-name", - true, args -> new TemplateRoleName((String) args[0], (Format) args[1])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "template-role-name", + true, + args -> new TemplateRoleName((String) args[0], (Format) args[1]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), Fields.TEMPLATE); @@ -66,8 +69,7 @@ public boolean equals(Object o) { return false; } final TemplateRoleName that = (TemplateRoleName) o; - return Objects.equals(this.template, that.template) && - this.format == that.format; + return Objects.equals(this.template, that.template) && this.format == that.format; } @Override @@ -88,9 +90,9 @@ static TemplateRoleName fromXContent(XContentParser parser) throws IOException { return PARSER.parse(parser, null); } - public enum Format { - STRING, JSON; + STRING, + JSON; private static Format fromXContent(XContentParser parser) throws IOException { XContentParserUtils.ensureExpectedToken(XContentParser.Token.VALUE_STRING, parser.currentToken(), parser); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/support/ApiKey.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/support/ApiKey.java index 4de32924c7684..928639d39e1d6 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/support/ApiKey.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/support/ApiKey.java @@ -8,11 +8,11 @@ package org.elasticsearch.client.security.support; -import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.core.Nullable; import java.io.IOException; import java.time.Instant; @@ -40,20 +40,37 @@ public final class ApiKey { @Nullable private final Object[] sortValues; - public ApiKey(String name, String id, Instant creation, Instant expiration, boolean invalidated, String username, String realm, - Map metadata) { + public ApiKey( + String name, + String id, + Instant creation, + Instant expiration, + boolean invalidated, + String username, + String realm, + Map metadata + ) { this(name, id, creation, expiration, invalidated, username, realm, metadata, null); } - public ApiKey(String name, String id, Instant creation, Instant expiration, boolean invalidated, String username, String realm, - Map metadata, @Nullable Object[] sortValues) { + public ApiKey( + String name, + String id, + Instant creation, + Instant expiration, + boolean invalidated, + String username, + String realm, + Map metadata, + @Nullable Object[] sortValues + ) { this.name = name; this.id = id; // As we do not yet support the nanosecond precision when we serialize to JSON, // here creating the 'Instant' of milliseconds precision. // This Instant can then be used for date comparison. this.creation = Instant.ofEpochMilli(creation.toEpochMilli()); - this.expiration = (expiration != null) ? Instant.ofEpochMilli(expiration.toEpochMilli()): null; + this.expiration = (expiration != null) ? Instant.ofEpochMilli(expiration.toEpochMilli()) : null; this.invalidated = invalidated; this.username = username; this.realm = realm; @@ -139,14 +156,14 @@ public boolean equals(Object obj) { } ApiKey other = (ApiKey) obj; return Objects.equals(name, other.name) - && Objects.equals(id, other.id) - && Objects.equals(creation, other.creation) - && Objects.equals(expiration, other.expiration) - && Objects.equals(invalidated, other.invalidated) - && Objects.equals(username, other.username) - && Objects.equals(realm, other.realm) - && Objects.equals(metadata, other.metadata) - && Arrays.equals(sortValues, other.sortValues); + && Objects.equals(id, other.id) + && Objects.equals(creation, other.creation) + && Objects.equals(expiration, other.expiration) + && Objects.equals(invalidated, other.invalidated) + && Objects.equals(username, other.username) + && Objects.equals(realm, other.realm) + && Objects.equals(metadata, other.metadata) + && Arrays.equals(sortValues, other.sortValues); } @SuppressWarnings("unchecked") @@ -158,13 +175,25 @@ public boolean equals(Object obj) { final List arg8 = (List) args[8]; sortValues = arg8.isEmpty() ? null : arg8.toArray(); } - return new ApiKey((String) args[0], (String) args[1], Instant.ofEpochMilli((Long) args[2]), - (args[3] == null) ? null : Instant.ofEpochMilli((Long) args[3]), (Boolean) args[4], (String) args[5], (String) args[6], - (Map) args[7], sortValues); + return new ApiKey( + (String) args[0], + (String) args[1], + Instant.ofEpochMilli((Long) args[2]), + (args[3] == null) ? null : Instant.ofEpochMilli((Long) args[3]), + (Boolean) args[4], + (String) args[5], + (String) args[6], + (Map) args[7], + sortValues + ); }); static { - PARSER.declareField(optionalConstructorArg(), (p, c) -> p.textOrNull(), new ParseField("name"), - ObjectParser.ValueType.STRING_OR_NULL); + PARSER.declareField( + optionalConstructorArg(), + (p, c) -> p.textOrNull(), + new ParseField("name"), + ObjectParser.ValueType.STRING_OR_NULL + ); PARSER.declareString(constructorArg(), new ParseField("id")); PARSER.declareLong(constructorArg(), new ParseField("creation")); PARSER.declareLong(optionalConstructorArg(), new ParseField("expiration")); @@ -181,7 +210,22 @@ public static ApiKey fromXContent(XContentParser parser) throws IOException { @Override public String toString() { - return "ApiKey [name=" + name + ", id=" + id + ", creation=" + creation + ", expiration=" + expiration + ", invalidated=" - + invalidated + ", username=" + username + ", realm=" + realm + ", _sort=" + Arrays.toString(sortValues) + "]"; + return "ApiKey [name=" + + name + + ", id=" + + id + + ", creation=" + + creation + + ", expiration=" + + expiration + + ", invalidated=" + + invalidated + + ", username=" + + username + + ", realm=" + + realm + + ", _sort=" + + Arrays.toString(sortValues) + + "]"; } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/support/CertificateInfo.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/support/CertificateInfo.java index 7d59a3d8e1c87..288b38b2e738d 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/support/CertificateInfo.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/support/CertificateInfo.java @@ -9,8 +9,8 @@ package org.elasticsearch.client.security.support; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -38,8 +38,15 @@ public final class CertificateInfo { private final boolean hasPrivateKey; private final String expiry; - public CertificateInfo(String path, String format, @Nullable String alias, String subjectDn, String serialNumber, boolean hasPrivateKey, - String expiry) { + public CertificateInfo( + String path, + String format, + @Nullable String alias, + String subjectDn, + String serialNumber, + boolean hasPrivateKey, + String expiry + ) { this.path = path; this.format = format; this.alias = alias; @@ -78,9 +85,19 @@ public String getExpiry() { } @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("certificate_info", - true, args -> new CertificateInfo((String) args[0], (String) args[1], (String) args[2], (String) args[3], (String) args[4], - (boolean) args[5], (String) args[6])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "certificate_info", + true, + args -> new CertificateInfo( + (String) args[0], + (String) args[1], + (String) args[2], + (String) args[3], + (String) args[4], + (boolean) args[5], + (String) args[6] + ) + ); static { PARSER.declareString(constructorArg(), PATH); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/support/ServiceAccountInfo.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/support/ServiceAccountInfo.java index 93ba0a09f39f5..d469495efbff5 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/support/ServiceAccountInfo.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/support/ServiceAccountInfo.java @@ -35,10 +35,8 @@ public Role getRole() { @Override public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; ServiceAccountInfo that = (ServiceAccountInfo) o; return principal.equals(that.principal) && role.equals(that.role); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/support/ServiceTokenInfo.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/support/ServiceTokenInfo.java index c00aea1de6475..cb019a39540a3 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/support/ServiceTokenInfo.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/support/ServiceTokenInfo.java @@ -43,10 +43,8 @@ public Collection getNodeNames() { @Override public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; ServiceTokenInfo that = (ServiceTokenInfo) o; return Objects.equals(name, that.name) && Objects.equals(source, that.source) && Objects.equals(nodeNames, that.nodeNames); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/support/expressiondsl/expressions/CompositeRoleMapperExpression.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/support/expressiondsl/expressions/CompositeRoleMapperExpression.java index e8d32dcf3a7e5..81d5389da0d79 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/support/expressiondsl/expressions/CompositeRoleMapperExpression.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/support/expressiondsl/expressions/CompositeRoleMapperExpression.java @@ -84,4 +84,3 @@ public XContentBuilder toXContent(final XContentBuilder builder, final Params pa } } - diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/support/expressiondsl/expressions/CompositeType.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/support/expressiondsl/expressions/CompositeType.java index 74aa03e53c061..575ca13192d41 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/support/expressiondsl/expressions/CompositeType.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/support/expressiondsl/expressions/CompositeType.java @@ -16,7 +16,9 @@ public enum CompositeType { - ANY("any"), ALL("all"), EXCEPT("except"); + ANY("any"), + ALL("all"), + EXCEPT("except"); private static Map nameToType = Collections.unmodifiableMap(initialize()); private ParseField field; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/support/expressiondsl/fields/FieldRoleMapperExpression.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/support/expressiondsl/fields/FieldRoleMapperExpression.java index 3693cfd325be5..2427f51cc9adc 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/support/expressiondsl/fields/FieldRoleMapperExpression.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/support/expressiondsl/fields/FieldRoleMapperExpression.java @@ -53,10 +53,8 @@ public List getValues() { @Override public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; final FieldRoleMapperExpression that = (FieldRoleMapperExpression) o; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/support/expressiondsl/parser/RoleMapperExpressionParser.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/support/expressiondsl/parser/RoleMapperExpressionParser.java index 2d801faf20120..eb0d748ca444d 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/support/expressiondsl/parser/RoleMapperExpressionParser.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/support/expressiondsl/parser/RoleMapperExpressionParser.java @@ -56,8 +56,7 @@ public RoleMapperExpression parse(final String name, final XContentParser parser return parseRulesObject(name, parser); } - private RoleMapperExpression parseRulesObject(final String objectName, final XContentParser parser) - throws IOException { + private RoleMapperExpression parseRulesObject(final String objectName, final XContentParser parser) throws IOException { // find the start of the DSL object final XContentParser.Token token; if (parser.currentToken() == null) { @@ -66,8 +65,11 @@ private RoleMapperExpression parseRulesObject(final String objectName, final XCo token = parser.currentToken(); } if (token != XContentParser.Token.START_OBJECT) { - throw new ElasticsearchParseException("failed to parse rules expression. expected [{}] to be an object but found [{}] instead", - objectName, token); + throw new ElasticsearchParseException( + "failed to parse rules expression. expected [{}] to be an object but found [{}] instead", + objectName, + token + ); } final String fieldName = fieldName(objectName, parser); @@ -78,8 +80,7 @@ private RoleMapperExpression parseRulesObject(final String objectName, final XCo return expr; } - private RoleMapperExpression parseExpression(XContentParser parser, String field, String objectName) - throws IOException { + private RoleMapperExpression parseExpression(XContentParser parser, String field, String objectName) throws IOException { if (CompositeType.ANY.getParseField().match(field, parser.getDeprecationHandler())) { final AnyRoleMapperExpression.Builder builder = AnyRoleMapperExpression.builder(); @@ -94,8 +95,11 @@ private RoleMapperExpression parseExpression(XContentParser parser, String field } else if (CompositeType.EXCEPT.getParseField().match(field, parser.getDeprecationHandler())) { return parseExceptExpression(parser); } else { - throw new ElasticsearchParseException("failed to parse rules expression. field [{}] is not recognised in object [{}]", field, - objectName); + throw new ElasticsearchParseException( + "failed to parse rules expression. field [{}] is not recognised in object [{}]", + field, + objectName + ); } } @@ -110,8 +114,10 @@ private RoleMapperExpression parseFieldExpression(XContentParser parser) throws values = Collections.singletonList(parseFieldValue(parser)); } if (parser.nextToken() != XContentParser.Token.END_OBJECT) { - throw new ElasticsearchParseException("failed to parse rules expression. object [{}] contains multiple fields", - FIELD.getPreferredName()); + throw new ElasticsearchParseException( + "failed to parse rules expression. object [{}] contains multiple fields", + FIELD.getPreferredName() + ); } return FieldRoleMapperExpression.ofKeyValues(fieldName, values.toArray()); @@ -137,15 +143,14 @@ private String fieldName(String objectName, XContentParser parser) throws IOExce return parsedFieldName; } - private List parseExpressionArray(ParseField field, XContentParser parser) - throws IOException { + private List parseExpressionArray(ParseField field, XContentParser parser) throws IOException { parser.nextToken(); // parseArray requires that the parser is positioned // at the START_ARRAY token return parseArray(field, parser, p -> parseRulesObject(field.getPreferredName(), p)); } private List parseArray(ParseField field, XContentParser parser, CheckedFunction elementParser) - throws IOException { + throws IOException { final XContentParser.Token token = parser.currentToken(); if (token == XContentParser.Token.START_ARRAY) { List list = new ArrayList<>(); @@ -160,21 +165,23 @@ private List parseArray(ParseField field, XContentParser parser, CheckedF private Object parseFieldValue(XContentParser parser) throws IOException { switch (parser.currentToken()) { - case VALUE_STRING: - return parser.text(); + case VALUE_STRING: + return parser.text(); - case VALUE_BOOLEAN: - return parser.booleanValue(); + case VALUE_BOOLEAN: + return parser.booleanValue(); - case VALUE_NUMBER: - return parser.longValue(); + case VALUE_NUMBER: + return parser.longValue(); - case VALUE_NULL: - return null; + case VALUE_NULL: + return null; - default: - throw new ElasticsearchParseException("failed to parse rules expression. expected a field value but found [{}] instead", parser - .currentToken()); + default: + throw new ElasticsearchParseException( + "failed to parse rules expression. expected a field value but found [{}] instead", + parser.currentToken() + ); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/User.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/User.java index f99832e301c8a..7c9cabb623401 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/User.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/User.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.security.user; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; +import org.elasticsearch.core.Nullable; import java.util.Collections; import java.util.List; @@ -25,8 +25,10 @@ public final class User { private final String username; private final List roles; private final Map metadata; - @Nullable private final String fullName; - @Nullable private final String email; + @Nullable + private final String fullName; + @Nullable + private final String email; /** * Builds the user to be utilized with security APIs. @@ -37,12 +39,12 @@ public final class User { * @param fullName the full name of the user that may be used for display purposes * @param email the email address of the user */ - public User(String username, List roles, Map metadata, @Nullable String fullName, - @Nullable String email) { + public User(String username, List roles, Map metadata, @Nullable String fullName, @Nullable String email) { this.username = Objects.requireNonNull(username, "`username` is required, cannot be null"); this.roles = List.copyOf(Objects.requireNonNull(roles, "`roles` is required, cannot be null. Pass an empty list instead.")); this.metadata = Collections.unmodifiableMap( - Objects.requireNonNull(metadata, "`metadata` is required, cannot be null. Pass an empty map instead.")); + Objects.requireNonNull(metadata, "`metadata` is required, cannot be null. Pass an empty map instead.") + ); this.fullName = fullName; this.email = email; } @@ -113,10 +115,10 @@ public boolean equals(Object o) { if (o == null || this.getClass() != o.getClass()) return false; final User that = (User) o; return Objects.equals(username, that.username) - && Objects.equals(roles, that.roles) - && Objects.equals(metadata, that.metadata) - && Objects.equals(fullName, that.fullName) - && Objects.equals(email, that.email); + && Objects.equals(roles, that.roles) + && Objects.equals(metadata, that.metadata) + && Objects.equals(fullName, that.fullName) + && Objects.equals(email, that.email); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/privileges/AbstractIndicesPrivileges.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/privileges/AbstractIndicesPrivileges.java index a437575c1a99e..2008af93b38ff 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/privileges/AbstractIndicesPrivileges.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/privileges/AbstractIndicesPrivileges.java @@ -8,11 +8,11 @@ package org.elasticsearch.client.security.user.privileges; -import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; @@ -89,14 +89,14 @@ public static class FieldSecurity implements ToXContentObject { static final ParseField EXCEPT_FIELDS = new ParseField("except"); private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - FIELD_PERMISSIONS.getPreferredName(), true, FieldSecurity::buildObjectFromParserArgs); + FIELD_PERMISSIONS.getPreferredName(), + true, + FieldSecurity::buildObjectFromParserArgs + ); @SuppressWarnings("unchecked") private static FieldSecurity buildObjectFromParserArgs(Object[] args) { - return new FieldSecurity( - (List) args[0], - (List) args[1] - ); + return new FieldSecurity((List) args[0], (List) args[1]); } static { @@ -186,8 +186,7 @@ public boolean equals(Object o) { return false; } final FieldSecurity that = (FieldSecurity) o; - return Objects.equals(this.grantedFields, that.grantedFields) && - Objects.equals(this.deniedFields, that.deniedFields); + return Objects.equals(this.grantedFields, that.grantedFields) && Objects.equals(this.deniedFields, that.deniedFields); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/privileges/ApplicationPrivilege.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/privileges/ApplicationPrivilege.java index 81ae5c714ca8c..48c1e5cf7f9e0 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/privileges/ApplicationPrivilege.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/privileges/ApplicationPrivilege.java @@ -8,11 +8,11 @@ package org.elasticsearch.client.security.user.privileges; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -86,8 +86,9 @@ public Map getMetadata() { @SuppressWarnings("unchecked") public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( "application_privilege", - true, args -> new ApplicationPrivilege((String) args[0], (String) args[1], (List) args[2], - (Map) args[3])); + true, + args -> new ApplicationPrivilege((String) args[0], (String) args[1], (List) args[2], (Map) args[3]) + ); static { PARSER.declareString(constructorArg(), APPLICATION); @@ -101,10 +102,10 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; ApplicationPrivilege that = (ApplicationPrivilege) o; - return Objects.equals(application, that.application) && - Objects.equals(name, that.name) && - Objects.equals(actions, that.actions) && - Objects.equals(metadata, that.metadata); + return Objects.equals(application, that.application) + && Objects.equals(name, that.name) + && Objects.equals(actions, that.actions) + && Objects.equals(metadata, that.metadata); } @Override @@ -126,8 +127,7 @@ public static final class Builder { private List actions = null; private Map metadata = null; - private Builder() { - } + private Builder() {} public Builder application(String applicationName) { this.applicationName = Objects.requireNonNull(applicationName, "application name must be provided"); @@ -162,9 +162,9 @@ public ApplicationPrivilege build() { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject() - .field(APPLICATION.getPreferredName(), application) - .field(NAME.getPreferredName(), name) - .field(ACTIONS.getPreferredName(), actions); + .field(APPLICATION.getPreferredName(), application) + .field(NAME.getPreferredName(), name) + .field(ACTIONS.getPreferredName(), actions); if (metadata != null && metadata.isEmpty() == false) { builder.field(METADATA.getPreferredName(), metadata); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/privileges/ApplicationResourcePrivileges.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/privileges/ApplicationResourcePrivileges.java index cb39d6bf152eb..18da5acd8fdb7 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/privileges/ApplicationResourcePrivileges.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/privileges/ApplicationResourcePrivileges.java @@ -8,12 +8,12 @@ package org.elasticsearch.client.security.user.privileges; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; @@ -37,16 +37,19 @@ public final class ApplicationResourcePrivileges implements ToXContentObject { @SuppressWarnings("unchecked") static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "application_privileges", false, constructorObjects -> { - // Don't ignore unknown fields. It is dangerous if the object we parse is also - // part of a request that we build later on, and the fields that we now ignore will - // end up being implicitly set to null in that request. - int i = 0; - final String application = (String) constructorObjects[i++]; - final List privileges = (List) constructorObjects[i++]; - final List resources = (List) constructorObjects[i]; - return new ApplicationResourcePrivileges(application, privileges, resources); - }); + "application_privileges", + false, + constructorObjects -> { + // Don't ignore unknown fields. It is dangerous if the object we parse is also + // part of a request that we build later on, and the fields that we now ignore will + // end up being implicitly set to null in that request. + int i = 0; + final String application = (String) constructorObjects[i++]; + final List privileges = (List) constructorObjects[i++]; + final List resources = (List) constructorObjects[i]; + return new ApplicationResourcePrivileges(application, privileges, resources); + } + ); static { PARSER.declareString(constructorArg(), APPLICATION); @@ -107,9 +110,7 @@ public boolean equals(Object o) { return false; } ApplicationResourcePrivileges that = (ApplicationResourcePrivileges) o; - return application.equals(that.application) - && privileges.equals(that.privileges) - && resources.equals(that.resources); + return application.equals(that.application) && privileges.equals(that.privileges) && resources.equals(that.resources); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/privileges/GlobalPrivileges.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/privileges/GlobalPrivileges.java index c52cb9de1c4cc..19210c1e34157 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/privileges/GlobalPrivileges.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/privileges/GlobalPrivileges.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.security.user.privileges; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -39,18 +39,23 @@ public final class GlobalPrivileges implements ToXContentObject { public static final List CATEGORIES = Collections.singletonList("application"); @SuppressWarnings("unchecked") - static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("global_category_privileges", - false, constructorObjects -> { - // ignore_unknown_fields is irrelevant here anyway, but let's keep it to false - // because this conveys strictness (woop woop) - return new GlobalPrivileges((Collection) constructorObjects[0]); - }); + static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "global_category_privileges", + false, + constructorObjects -> { + // ignore_unknown_fields is irrelevant here anyway, but let's keep it to false + // because this conveys strictness (woop woop) + return new GlobalPrivileges((Collection) constructorObjects[0]); + } + ); static { for (final String category : CATEGORIES) { - PARSER.declareNamedObjects(optionalConstructorArg(), - (parser, context, operation) -> GlobalOperationPrivilege.fromXContent(category, operation, parser), - new ParseField(category)); + PARSER.declareNamedObjects( + optionalConstructorArg(), + (parser, context, operation) -> GlobalOperationPrivilege.fromXContent(category, operation, parser), + new ParseField(category) + ); } } @@ -71,12 +76,15 @@ public GlobalPrivileges(Collection privilege } // duplicates are just ignored this.privileges = Set.copyOf(Objects.requireNonNull(privileges)); - this.privilegesByCategoryMap = Collections - .unmodifiableMap(this.privileges.stream().collect(Collectors.groupingBy(GlobalOperationPrivilege::getCategory))); + this.privilegesByCategoryMap = Collections.unmodifiableMap( + this.privileges.stream().collect(Collectors.groupingBy(GlobalOperationPrivilege::getCategory)) + ); for (final Map.Entry> privilegesByCategory : privilegesByCategoryMap.entrySet()) { // all operations for a specific category - final Set allOperations = privilegesByCategory.getValue().stream().map(p -> p.getOperation()) - .collect(Collectors.toSet()); + final Set allOperations = privilegesByCategory.getValue() + .stream() + .map(p -> p.getOperation()) + .collect(Collectors.toSet()); if (allOperations.size() != privilegesByCategory.getValue().size()) { throw new IllegalArgumentException("Different privileges for the same category and operation are not permitted"); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/privileges/IndicesPrivileges.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/privileges/IndicesPrivileges.java index 11c27a93dca27..559129818c1cd 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/privileges/IndicesPrivileges.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/privileges/IndicesPrivileges.java @@ -8,11 +8,11 @@ package org.elasticsearch.client.security.user.privileges; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; @@ -34,8 +34,10 @@ public final class IndicesPrivileges extends AbstractIndicesPrivileges implements ToXContentObject { @SuppressWarnings("unchecked") - static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("indices_privileges", false, constructorObjects -> { + static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "indices_privileges", + false, + constructorObjects -> { int i = 0; final List indices = (List) constructorObjects[i++]; final List privileges = (List) constructorObjects[i++]; @@ -43,7 +45,8 @@ public final class IndicesPrivileges extends AbstractIndicesPrivileges implement final FieldSecurity fields = (FieldSecurity) constructorObjects[i++]; final String query = (String) constructorObjects[i]; return new IndicesPrivileges(indices, privileges, allowRestrictedIndices, fields, query); - }); + } + ); static { PARSER.declareStringArray(constructorArg(), NAMES); @@ -57,8 +60,13 @@ public final class IndicesPrivileges extends AbstractIndicesPrivileges implement // missing query means all documents, i.e. no restrictions private final @Nullable String query; - private IndicesPrivileges(List indices, List privileges, boolean allowRestrictedIndices, - @Nullable FieldSecurity fieldSecurity, @Nullable String query) { + private IndicesPrivileges( + List indices, + List privileges, + boolean allowRestrictedIndices, + @Nullable FieldSecurity fieldSecurity, + @Nullable String query + ) { super(indices, privileges, allowRestrictedIndices); this.fieldSecurity = fieldSecurity; this.query = query; @@ -153,20 +161,14 @@ public static Builder builder() { public static final class Builder { - private @Nullable - List indices = null; - private @Nullable - List privileges = null; - private @Nullable - List grantedFields = null; - private @Nullable - List deniedFields = null; - private @Nullable - String query = null; + private @Nullable List indices = null; + private @Nullable List privileges = null; + private @Nullable List grantedFields = null; + private @Nullable List deniedFields = null; + private @Nullable String query = null; boolean allowRestrictedIndices = false; - public Builder() { - } + public Builder() {} public Builder indices(String... indices) { return indices(Arrays.asList(Objects.requireNonNull(indices, "indices required"))); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/privileges/ManageApplicationPrivilege.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/privileges/ManageApplicationPrivilege.java index ae580396ff343..8dc083f1f3a35 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/privileges/ManageApplicationPrivilege.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/privileges/ManageApplicationPrivilege.java @@ -30,7 +30,7 @@ public ManageApplicationPrivilege(Collection applications) { @SuppressWarnings("unchecked") public Set getManagedApplications() { - return (Set)getRaw().get(KEY); + return (Set) getRaw().get(KEY); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/privileges/Role.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/privileges/Role.java index c068e7c2df21d..9db2f42e2e04c 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/privileges/Role.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/privileges/Role.java @@ -8,12 +8,12 @@ package org.elasticsearch.client.security.user.privileges; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.Tuple; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.util.Arrays; @@ -40,33 +40,52 @@ public final class Role { public static final ParseField TRANSIENT_METADATA = new ParseField("transient_metadata"); @SuppressWarnings("unchecked") - public static final ConstructingObjectParser>, String> PARSER = - new ConstructingObjectParser<>("role_descriptor", false, (constructorObjects, roleName) -> { - // Don't ignore unknown fields. It is dangerous if the object we parse is also - // part of a request that we build later on, and the fields that we now ignore - // will end up being implicitly set to null in that request. - int i = 0; - final Collection clusterPrivileges = (Collection) constructorObjects[i++]; - final GlobalPrivileges globalApplicationPrivileges = (GlobalPrivileges) constructorObjects[i++]; - final Collection indicesPrivileges = (Collection) constructorObjects[i++]; - final Collection applicationResourcePrivileges = - (Collection) constructorObjects[i++]; - final Collection runAsPrivilege = (Collection) constructorObjects[i++]; - final Map metadata = (Map) constructorObjects[i++]; - final Map transientMetadata = (Map) constructorObjects[i]; - return new Tuple<>( - new Role(roleName, clusterPrivileges, globalApplicationPrivileges, indicesPrivileges, applicationResourcePrivileges, - runAsPrivilege, metadata), - transientMetadata != null ? Collections.unmodifiableMap(transientMetadata) : Collections.emptyMap()); - }); + public static final ConstructingObjectParser>, String> PARSER = new ConstructingObjectParser<>( + "role_descriptor", + false, + (constructorObjects, roleName) -> { + // Don't ignore unknown fields. It is dangerous if the object we parse is also + // part of a request that we build later on, and the fields that we now ignore + // will end up being implicitly set to null in that request. + int i = 0; + final Collection clusterPrivileges = (Collection) constructorObjects[i++]; + final GlobalPrivileges globalApplicationPrivileges = (GlobalPrivileges) constructorObjects[i++]; + final Collection indicesPrivileges = (Collection) constructorObjects[i++]; + final Collection applicationResourcePrivileges = (Collection< + ApplicationResourcePrivileges>) constructorObjects[i++]; + final Collection runAsPrivilege = (Collection) constructorObjects[i++]; + final Map metadata = (Map) constructorObjects[i++]; + final Map transientMetadata = (Map) constructorObjects[i]; + return new Tuple<>( + new Role( + roleName, + clusterPrivileges, + globalApplicationPrivileges, + indicesPrivileges, + applicationResourcePrivileges, + runAsPrivilege, + metadata + ), + transientMetadata != null ? Collections.unmodifiableMap(transientMetadata) : Collections.emptyMap() + ); + } + ); static { PARSER.declareStringArray(optionalConstructorArg(), CLUSTER); - PARSER.declareObject(optionalConstructorArg(), (parser,c)-> GlobalPrivileges.PARSER.parse(parser,null), GLOBAL); - PARSER.declareFieldArray(optionalConstructorArg(), (parser,c)->IndicesPrivileges.PARSER.parse(parser,null), INDICES, - ValueType.OBJECT_ARRAY); - PARSER.declareFieldArray(optionalConstructorArg(), (parser,c)->ApplicationResourcePrivileges.PARSER.parse(parser,null), - APPLICATIONS, ValueType.OBJECT_ARRAY); + PARSER.declareObject(optionalConstructorArg(), (parser, c) -> GlobalPrivileges.PARSER.parse(parser, null), GLOBAL); + PARSER.declareFieldArray( + optionalConstructorArg(), + (parser, c) -> IndicesPrivileges.PARSER.parse(parser, null), + INDICES, + ValueType.OBJECT_ARRAY + ); + PARSER.declareFieldArray( + optionalConstructorArg(), + (parser, c) -> ApplicationResourcePrivileges.PARSER.parse(parser, null), + APPLICATIONS, + ValueType.OBJECT_ARRAY + ); PARSER.declareStringArray(optionalConstructorArg(), RUN_AS); PARSER.declareObject(optionalConstructorArg(), (parser, c) -> parser.map(), METADATA); PARSER.declareObject(optionalConstructorArg(), (parser, c) -> parser.map(), TRANSIENT_METADATA); @@ -80,26 +99,33 @@ public final class Role { private final Set runAsPrivilege; private final Map metadata; - private Role(String name, @Nullable Collection clusterPrivileges, - @Nullable GlobalPrivileges globalPrivileges, - @Nullable Collection indicesPrivileges, - @Nullable Collection applicationPrivileges, - @Nullable Collection runAsPrivilege, @Nullable Map metadata) { - if (Strings.hasText(name) == false){ + private Role( + String name, + @Nullable Collection clusterPrivileges, + @Nullable GlobalPrivileges globalPrivileges, + @Nullable Collection indicesPrivileges, + @Nullable Collection applicationPrivileges, + @Nullable Collection runAsPrivilege, + @Nullable Map metadata + ) { + if (Strings.hasText(name) == false) { throw new IllegalArgumentException("role name must be provided"); } else { this.name = name; } // no cluster privileges are granted unless otherwise specified - this.clusterPrivileges = Collections - .unmodifiableSet(clusterPrivileges != null ? new HashSet<>(clusterPrivileges) : Collections.emptySet()); + this.clusterPrivileges = Collections.unmodifiableSet( + clusterPrivileges != null ? new HashSet<>(clusterPrivileges) : Collections.emptySet() + ); this.globalPrivileges = globalPrivileges; // no indices privileges are granted unless otherwise specified - this.indicesPrivileges = Collections - .unmodifiableSet(indicesPrivileges != null ? new HashSet<>(indicesPrivileges) : Collections.emptySet()); + this.indicesPrivileges = Collections.unmodifiableSet( + indicesPrivileges != null ? new HashSet<>(indicesPrivileges) : Collections.emptySet() + ); // no application resource privileges are granted unless otherwise specified this.applicationPrivileges = Collections.unmodifiableSet( - applicationPrivileges != null ? new HashSet<>(applicationPrivileges) : Collections.emptySet()); + applicationPrivileges != null ? new HashSet<>(applicationPrivileges) : Collections.emptySet() + ); // no run as privileges are granted unless otherwise specified this.runAsPrivilege = Collections.unmodifiableSet(runAsPrivilege != null ? new HashSet<>(runAsPrivilege) : Collections.emptySet()); this.metadata = metadata != null ? Collections.unmodifiableMap(metadata) : Collections.emptyMap(); @@ -149,8 +175,7 @@ public boolean equals(Object o) { @Override public int hashCode() { - return Objects.hash(name, clusterPrivileges, globalPrivileges, indicesPrivileges, applicationPrivileges, - runAsPrivilege, metadata); + return Objects.hash(name, clusterPrivileges, globalPrivileges, indicesPrivileges, applicationPrivileges, runAsPrivilege, metadata); } @Override @@ -209,12 +234,10 @@ public static final class Builder { private @Nullable Collection runAsPrivilege = null; private @Nullable Map metadata = null; - private Builder() { - } + private Builder() {} public Builder clone(Role role) { - return this - .name(role.name) + return this.name(role.name) .clusterPrivileges(role.clusterPrivileges) .globalApplicationPrivileges(role.globalPrivileges) .indicesPrivileges(role.indicesPrivileges) @@ -224,7 +247,7 @@ public Builder clone(Role role) { } public Builder name(String name) { - if (Strings.hasText(name) == false){ + if (Strings.hasText(name) == false) { throw new IllegalArgumentException("role name must be provided"); } else { this.name = name; @@ -233,13 +256,16 @@ public Builder name(String name) { } public Builder clusterPrivileges(String... clusterPrivileges) { - return clusterPrivileges(Arrays - .asList(Objects.requireNonNull(clusterPrivileges, "Cluster privileges cannot be null. Pass an empty array instead."))); + return clusterPrivileges( + Arrays.asList(Objects.requireNonNull(clusterPrivileges, "Cluster privileges cannot be null. Pass an empty array instead.")) + ); } public Builder clusterPrivileges(Collection clusterPrivileges) { - this.clusterPrivileges = Objects.requireNonNull(clusterPrivileges, - "Cluster privileges cannot be null. Pass an empty collection instead."); + this.clusterPrivileges = Objects.requireNonNull( + clusterPrivileges, + "Cluster privileges cannot be null. Pass an empty collection instead." + ); return this; } @@ -249,35 +275,49 @@ public Builder globalApplicationPrivileges(GlobalPrivileges globalApplicationPri } public Builder indicesPrivileges(IndicesPrivileges... indicesPrivileges) { - return indicesPrivileges(Arrays - .asList(Objects.requireNonNull(indicesPrivileges, "Indices privileges cannot be null. Pass an empty array instead."))); + return indicesPrivileges( + Arrays.asList(Objects.requireNonNull(indicesPrivileges, "Indices privileges cannot be null. Pass an empty array instead.")) + ); } public Builder indicesPrivileges(Collection indicesPrivileges) { - this.indicesPrivileges = Objects.requireNonNull(indicesPrivileges, - "Indices privileges cannot be null. Pass an empty collection instead."); + this.indicesPrivileges = Objects.requireNonNull( + indicesPrivileges, + "Indices privileges cannot be null. Pass an empty collection instead." + ); return this; } public Builder applicationResourcePrivileges(ApplicationResourcePrivileges... applicationResourcePrivileges) { - return applicationResourcePrivileges(Arrays.asList(Objects.requireNonNull(applicationResourcePrivileges, - "Application resource privileges cannot be null. Pass an empty array instead."))); + return applicationResourcePrivileges( + Arrays.asList( + Objects.requireNonNull( + applicationResourcePrivileges, + "Application resource privileges cannot be null. Pass an empty array instead." + ) + ) + ); } public Builder applicationResourcePrivileges(Collection applicationResourcePrivileges) { - this.applicationResourcePrivileges = Objects.requireNonNull(applicationResourcePrivileges, - "Application resource privileges cannot be null. Pass an empty collection instead."); + this.applicationResourcePrivileges = Objects.requireNonNull( + applicationResourcePrivileges, + "Application resource privileges cannot be null. Pass an empty collection instead." + ); return this; } public Builder runAsPrivilege(String... runAsPrivilege) { - return runAsPrivilege(Arrays - .asList(Objects.requireNonNull(runAsPrivilege, "Run as privilege cannot be null. Pass an empty array instead."))); + return runAsPrivilege( + Arrays.asList(Objects.requireNonNull(runAsPrivilege, "Run as privilege cannot be null. Pass an empty array instead.")) + ); } public Builder runAsPrivilege(Collection runAsPrivilege) { - this.runAsPrivilege = Objects.requireNonNull(runAsPrivilege, - "Run as privilege cannot be null. Pass an empty collection instead."); + this.runAsPrivilege = Objects.requireNonNull( + runAsPrivilege, + "Run as privilege cannot be null. Pass an empty collection instead." + ); return this; } @@ -287,8 +327,15 @@ public Builder metadata(Map metadata) { } public Role build() { - return new Role(name, clusterPrivileges, globalApplicationPrivileges, indicesPrivileges, applicationResourcePrivileges, - runAsPrivilege, metadata); + return new Role( + name, + clusterPrivileges, + globalApplicationPrivileges, + indicesPrivileges, + applicationResourcePrivileges, + runAsPrivilege, + metadata + ); } } @@ -326,11 +373,37 @@ public static class ClusterPrivilegeName { public static final String MANAGE_ILM = "manage_ilm"; public static final String READ_ILM = "read_ilm"; public static final String MANAGE_ENRICH = "manage_enrich"; - public static final String[] ALL_ARRAY = new String[] { NONE, ALL, MONITOR, MONITOR_TRANSFORM_DEPRECATED, MONITOR_TRANSFORM, - MONITOR_ML, MONITOR_TEXT_STRUCTURE, MONITOR_WATCHER, MONITOR_ROLLUP, MANAGE, MANAGE_TRANSFORM_DEPRECATED, MANAGE_TRANSFORM, - MANAGE_ML, MANAGE_WATCHER, MANAGE_ROLLUP, MANAGE_INDEX_TEMPLATES, MANAGE_INGEST_PIPELINES, READ_PIPELINE, - TRANSPORT_CLIENT, MANAGE_SECURITY, MANAGE_SAML, MANAGE_OIDC, MANAGE_TOKEN, MANAGE_PIPELINE, MANAGE_AUTOSCALING, MANAGE_CCR, - READ_CCR, MANAGE_ILM, READ_ILM, MANAGE_ENRICH }; + public static final String[] ALL_ARRAY = new String[] { + NONE, + ALL, + MONITOR, + MONITOR_TRANSFORM_DEPRECATED, + MONITOR_TRANSFORM, + MONITOR_ML, + MONITOR_TEXT_STRUCTURE, + MONITOR_WATCHER, + MONITOR_ROLLUP, + MANAGE, + MANAGE_TRANSFORM_DEPRECATED, + MANAGE_TRANSFORM, + MANAGE_ML, + MANAGE_WATCHER, + MANAGE_ROLLUP, + MANAGE_INDEX_TEMPLATES, + MANAGE_INGEST_PIPELINES, + READ_PIPELINE, + TRANSPORT_CLIENT, + MANAGE_SECURITY, + MANAGE_SAML, + MANAGE_OIDC, + MANAGE_TOKEN, + MANAGE_PIPELINE, + MANAGE_AUTOSCALING, + MANAGE_CCR, + READ_CCR, + MANAGE_ILM, + READ_ILM, + MANAGE_ENRICH }; } /** @@ -355,9 +428,25 @@ public static class IndexPrivilegeName { public static final String CREATE_DOC = "create_doc"; public static final String MAINTENANCE = "maintenance"; public static final String AUTO_CONFIGURE = "auto_configure"; - public static final String[] ALL_ARRAY = new String[] { NONE, ALL, READ, READ_CROSS, CREATE, INDEX, DELETE, WRITE, MONITOR, MANAGE, - DELETE_INDEX, CREATE_INDEX, VIEW_INDEX_METADATA, MANAGE_FOLLOW_INDEX, MANAGE_ILM, CREATE_DOC, MAINTENANCE, - AUTO_CONFIGURE}; + public static final String[] ALL_ARRAY = new String[] { + NONE, + ALL, + READ, + READ_CROSS, + CREATE, + INDEX, + DELETE, + WRITE, + MONITOR, + MANAGE, + DELETE_INDEX, + CREATE_INDEX, + VIEW_INDEX_METADATA, + MANAGE_FOLLOW_INDEX, + MANAGE_ILM, + CREATE_DOC, + MAINTENANCE, + AUTO_CONFIGURE }; } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/privileges/UserIndicesPrivileges.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/privileges/UserIndicesPrivileges.java index 49cc303e0e4de..c8d9d0a4e4be9 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/privileges/UserIndicesPrivileges.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/privileges/UserIndicesPrivileges.java @@ -34,7 +34,10 @@ public class UserIndicesPrivileges extends AbstractIndicesPrivileges { private final List query; private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "user_indices_privilege", true, UserIndicesPrivileges::buildObjectFromParserArgs); + "user_indices_privilege", + true, + UserIndicesPrivileges::buildObjectFromParserArgs + ); static { PARSER.declareStringArray(constructorArg(), IndicesPrivileges.NAMES); @@ -59,8 +62,13 @@ public static UserIndicesPrivileges fromXContent(XContentParser parser) throws I return PARSER.parse(parser, null); } - public UserIndicesPrivileges(Collection indices, Collection privileges, boolean allowRestrictedIndices, - List fieldSecurity, List query) { + public UserIndicesPrivileges( + Collection indices, + Collection privileges, + boolean allowRestrictedIndices, + List fieldSecurity, + List query + ) { super(indices, privileges, allowRestrictedIndices); this.fieldSecurity = fieldSecurity == null ? Collections.emptyList() : List.copyOf(fieldSecurity); this.query = query == null ? Collections.emptyList() : List.copyOf(query); @@ -93,11 +101,11 @@ public boolean equals(Object o) { return false; } final UserIndicesPrivileges that = (UserIndicesPrivileges) o; - return Objects.equals(indices, that.indices) && - Objects.equals(privileges, that.privileges) && - allowRestrictedIndices == that.allowRestrictedIndices && - Objects.equals(fieldSecurity, that.fieldSecurity) && - Objects.equals(query, that.query); + return Objects.equals(indices, that.indices) + && Objects.equals(privileges, that.privileges) + && allowRestrictedIndices == that.allowRestrictedIndices + && Objects.equals(fieldSecurity, that.fieldSecurity) + && Objects.equals(query, that.query); } @Override @@ -107,12 +115,17 @@ public int hashCode() { @Override public String toString() { - return "UserIndexPrivilege{" + - "indices=" + indices + - ", privileges=" + privileges + - ", allow_restricted_indices=" + allowRestrictedIndices + - ", fieldSecurity=" + fieldSecurity + - ", query=" + query + - '}'; + return "UserIndexPrivilege{" + + "indices=" + + indices + + ", privileges=" + + privileges + + ", allow_restricted_indices=" + + allowRestrictedIndices + + ", fieldSecurity=" + + fieldSecurity + + ", query=" + + query + + '}'; } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/slm/ExecuteSnapshotLifecyclePolicyResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/slm/ExecuteSnapshotLifecyclePolicyResponse.java index b3609fe4e2f06..ffd7a9509685e 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/slm/ExecuteSnapshotLifecyclePolicyResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/slm/ExecuteSnapshotLifecyclePolicyResponse.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.slm; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -19,9 +19,11 @@ public class ExecuteSnapshotLifecyclePolicyResponse implements ToXContentObject { private static final ParseField SNAPSHOT_NAME = new ParseField("snapshot_name"); - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("excecute_snapshot_policy", true, - a -> new ExecuteSnapshotLifecyclePolicyResponse((String) a[0])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "excecute_snapshot_policy", + true, + a -> new ExecuteSnapshotLifecyclePolicyResponse((String) a[0]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), SNAPSHOT_NAME); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/slm/ExecuteSnapshotLifecycleRetentionRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/slm/ExecuteSnapshotLifecycleRetentionRequest.java index 0f86f765116c5..8c92f06edc53b 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/slm/ExecuteSnapshotLifecycleRetentionRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/slm/ExecuteSnapshotLifecycleRetentionRequest.java @@ -10,5 +10,4 @@ import org.elasticsearch.client.TimedRequest; -public class ExecuteSnapshotLifecycleRetentionRequest extends TimedRequest { -} +public class ExecuteSnapshotLifecycleRetentionRequest extends TimedRequest {} diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/slm/SnapshotInvocationRecord.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/slm/SnapshotInvocationRecord.java index e04c841b4dc3e..4c5350b71b26a 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/slm/SnapshotInvocationRecord.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/slm/SnapshotInvocationRecord.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.slm; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -26,9 +26,11 @@ public class SnapshotInvocationRecord implements ToXContentObject { private long timestamp; private String details; - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("snapshot_policy_invocation_record", true, - a -> new SnapshotInvocationRecord((String) a[0], (long) a[1], (String) a[2])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "snapshot_policy_invocation_record", + true, + a -> new SnapshotInvocationRecord((String) a[0], (long) a[1], (String) a[2]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), SNAPSHOT_NAME); @@ -77,9 +79,9 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; SnapshotInvocationRecord that = (SnapshotInvocationRecord) o; - return getTimestamp() == that.getTimestamp() && - Objects.equals(getSnapshotName(), that.getSnapshotName()) && - Objects.equals(getDetails(), that.getDetails()); + return getTimestamp() == that.getTimestamp() + && Objects.equals(getSnapshotName(), that.getSnapshotName()) + && Objects.equals(getDetails(), that.getDetails()); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/slm/SnapshotLifecycleManagementStatusRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/slm/SnapshotLifecycleManagementStatusRequest.java index 387d902a74737..71a73d8147617 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/slm/SnapshotLifecycleManagementStatusRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/slm/SnapshotLifecycleManagementStatusRequest.java @@ -10,5 +10,4 @@ import org.elasticsearch.client.TimedRequest; -public class SnapshotLifecycleManagementStatusRequest extends TimedRequest { -} +public class SnapshotLifecycleManagementStatusRequest extends TimedRequest {} diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/slm/SnapshotLifecyclePolicy.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/slm/SnapshotLifecyclePolicy.java index 6066aa2c25c64..ad60bd045f717 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/slm/SnapshotLifecyclePolicy.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/slm/SnapshotLifecyclePolicy.java @@ -8,10 +8,10 @@ package org.elasticsearch.client.slm; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -36,16 +36,18 @@ public class SnapshotLifecyclePolicy implements ToXContentObject { private static final ParseField RETENTION = new ParseField("retention"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("snapshot_lifecycle", true, - (a, id) -> { - String name = (String) a[0]; - String schedule = (String) a[1]; - String repo = (String) a[2]; - Map config = (Map) a[3]; - SnapshotRetentionConfiguration retention = (SnapshotRetentionConfiguration) a[4]; - return new SnapshotLifecyclePolicy(id, name, schedule, repo, config, retention); - }); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "snapshot_lifecycle", + true, + (a, id) -> { + String name = (String) a[0]; + String schedule = (String) a[1]; + String repo = (String) a[2]; + Map config = (Map) a[3]; + SnapshotRetentionConfiguration retention = (SnapshotRetentionConfiguration) a[4]; + return new SnapshotLifecyclePolicy(id, name, schedule, repo, config, retention); + } + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), NAME); @@ -55,9 +57,14 @@ public class SnapshotLifecyclePolicy implements ToXContentObject { PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), SnapshotRetentionConfiguration::parse, RETENTION); } - public SnapshotLifecyclePolicy(final String id, final String name, final String schedule, - final String repository, @Nullable final Map configuration, - @Nullable final SnapshotRetentionConfiguration retentionPolicy) { + public SnapshotLifecyclePolicy( + final String id, + final String name, + final String schedule, + final String repository, + @Nullable final Map configuration, + @Nullable final SnapshotRetentionConfiguration retentionPolicy + ) { this.id = Objects.requireNonNull(id, "policy id is required"); this.name = Objects.requireNonNull(name, "policy snapshot name is required"); this.schedule = Objects.requireNonNull(schedule, "policy schedule is required"); @@ -127,12 +134,12 @@ public boolean equals(Object obj) { return false; } SnapshotLifecyclePolicy other = (SnapshotLifecyclePolicy) obj; - return Objects.equals(id, other.id) && - Objects.equals(name, other.name) && - Objects.equals(schedule, other.schedule) && - Objects.equals(repository, other.repository) && - Objects.equals(configuration, other.configuration) && - Objects.equals(retentionPolicy, other.retentionPolicy); + return Objects.equals(id, other.id) + && Objects.equals(name, other.name) + && Objects.equals(schedule, other.schedule) + && Objects.equals(repository, other.repository) + && Objects.equals(configuration, other.configuration) + && Objects.equals(retentionPolicy, other.retentionPolicy); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/slm/SnapshotLifecyclePolicyMetadata.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/slm/SnapshotLifecyclePolicyMetadata.java index f7b9577404180..434d44d2952b7 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/slm/SnapshotLifecyclePolicyMetadata.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/slm/SnapshotLifecyclePolicyMetadata.java @@ -8,14 +8,14 @@ package org.elasticsearch.client.slm; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.snapshots.SnapshotId; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.snapshots.SnapshotId; import java.io.IOException; import java.util.Objects; @@ -46,20 +46,20 @@ public class SnapshotLifecyclePolicyMetadata implements ToXContentObject { private final SnapshotLifecycleStats.SnapshotPolicyStats policyStats; @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("snapshot_policy_metadata", - a -> { - SnapshotLifecyclePolicy policy = (SnapshotLifecyclePolicy) a[0]; - long version = (long) a[1]; - long modifiedDate = (long) a[2]; - SnapshotInvocationRecord lastSuccess = (SnapshotInvocationRecord) a[3]; - SnapshotInvocationRecord lastFailure = (SnapshotInvocationRecord) a[4]; - long nextExecution = (long) a[5]; - SnapshotInProgress sip = (SnapshotInProgress) a[6]; - SnapshotLifecycleStats.SnapshotPolicyStats stats = (SnapshotLifecycleStats.SnapshotPolicyStats) a[7]; - return new SnapshotLifecyclePolicyMetadata(policy, version, modifiedDate, lastSuccess, - lastFailure, nextExecution, sip, stats); - }); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "snapshot_policy_metadata", + a -> { + SnapshotLifecyclePolicy policy = (SnapshotLifecyclePolicy) a[0]; + long version = (long) a[1]; + long modifiedDate = (long) a[2]; + SnapshotInvocationRecord lastSuccess = (SnapshotInvocationRecord) a[3]; + SnapshotInvocationRecord lastFailure = (SnapshotInvocationRecord) a[4]; + long nextExecution = (long) a[5]; + SnapshotInProgress sip = (SnapshotInProgress) a[6]; + SnapshotLifecycleStats.SnapshotPolicyStats stats = (SnapshotLifecycleStats.SnapshotPolicyStats) a[7]; + return new SnapshotLifecyclePolicyMetadata(policy, version, modifiedDate, lastSuccess, lastFailure, nextExecution, sip, stats); + } + ); static { PARSER.declareObject(ConstructingObjectParser.constructorArg(), SnapshotLifecyclePolicy::parse, POLICY); @@ -69,8 +69,11 @@ public class SnapshotLifecyclePolicyMetadata implements ToXContentObject { PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), SnapshotInvocationRecord::parse, LAST_FAILURE); PARSER.declareLong(ConstructingObjectParser.constructorArg(), NEXT_EXECUTION_MILLIS); PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), SnapshotInProgress::parse, SNAPSHOT_IN_PROGRESS); - PARSER.declareObject(ConstructingObjectParser.constructorArg(), - (p, c) -> SnapshotLifecycleStats.SnapshotPolicyStats.parse(p, "policy"), POLICY_STATS); + PARSER.declareObject( + ConstructingObjectParser.constructorArg(), + (p, c) -> SnapshotLifecycleStats.SnapshotPolicyStats.parse(p, "policy"), + POLICY_STATS + ); } @@ -78,11 +81,16 @@ public static SnapshotLifecyclePolicyMetadata parse(XContentParser parser, Strin return PARSER.apply(parser, id); } - public SnapshotLifecyclePolicyMetadata(SnapshotLifecyclePolicy policy, long version, long modifiedDate, - SnapshotInvocationRecord lastSuccess, SnapshotInvocationRecord lastFailure, - long nextExecution, - @Nullable SnapshotInProgress snapshotInProgress, - SnapshotLifecycleStats.SnapshotPolicyStats policyStats) { + public SnapshotLifecyclePolicyMetadata( + SnapshotLifecyclePolicy policy, + long version, + long modifiedDate, + SnapshotInvocationRecord lastSuccess, + SnapshotInvocationRecord lastFailure, + long nextExecution, + @Nullable SnapshotInProgress snapshotInProgress, + SnapshotLifecycleStats.SnapshotPolicyStats policyStats + ) { this.policy = policy; this.version = version; this.modifiedDate = modifiedDate; @@ -167,13 +175,13 @@ public boolean equals(Object obj) { return false; } SnapshotLifecyclePolicyMetadata other = (SnapshotLifecyclePolicyMetadata) obj; - return Objects.equals(policy, other.policy) && - Objects.equals(version, other.version) && - Objects.equals(modifiedDate, other.modifiedDate) && - Objects.equals(lastSuccess, other.lastSuccess) && - Objects.equals(lastFailure, other.lastFailure) && - Objects.equals(nextExecution, other.nextExecution) && - Objects.equals(policyStats, other.policyStats); + return Objects.equals(policy, other.policy) + && Objects.equals(version, other.version) + && Objects.equals(modifiedDate, other.modifiedDate) + && Objects.equals(lastSuccess, other.lastSuccess) + && Objects.equals(lastFailure, other.lastFailure) + && Objects.equals(nextExecution, other.nextExecution) + && Objects.equals(policyStats, other.policyStats); } public static class SnapshotInProgress implements ToXContentObject { @@ -183,14 +191,17 @@ public static class SnapshotInProgress implements ToXContentObject { private static final ParseField START_TIME = new ParseField("start_time_millis"); private static final ParseField FAILURE = new ParseField("failure"); - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("snapshot_in_progress", true, a -> { + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "snapshot_in_progress", + true, + a -> { SnapshotId id = new SnapshotId((String) a[0], (String) a[1]); String state = (String) a[2]; long start = (long) a[3]; String failure = (String) a[4]; return new SnapshotInProgress(id, state, start, failure); - }); + } + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), NAME); @@ -245,10 +256,10 @@ public boolean equals(Object obj) { return false; } SnapshotInProgress other = (SnapshotInProgress) obj; - return Objects.equals(snapshotId, other.snapshotId) && - Objects.equals(state, other.state) && - startTime == other.startTime && - Objects.equals(failure, other.failure); + return Objects.equals(snapshotId, other.snapshotId) + && Objects.equals(state, other.state) + && startTime == other.startTime + && Objects.equals(failure, other.failure); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/slm/SnapshotLifecycleStats.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/slm/SnapshotLifecycleStats.java index 6ea23dcc11df7..e99dc852786ca 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/slm/SnapshotLifecycleStats.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/slm/SnapshotLifecycleStats.java @@ -8,10 +8,10 @@ package org.elasticsearch.client.slm; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentFragment; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -44,19 +44,20 @@ public class SnapshotLifecycleStats implements ToXContentObject { public static final ParseField TOTAL_DELETIONS = new ParseField("total_snapshots_deleted"); public static final ParseField TOTAL_DELETION_FAILURES = new ParseField("total_snapshot_deletion_failures"); - @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("snapshot_policy_stats", true, - a -> { - long runs = (long) a[0]; - long failed = (long) a[1]; - long timedOut = (long) a[2]; - long timeMs = (long) a[3]; - Map policyStatsMap = ((List) a[4]).stream() - .collect(Collectors.toMap(m -> m.policyId, Function.identity())); - return new SnapshotLifecycleStats(runs, failed, timedOut, timeMs, policyStatsMap); - }); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "snapshot_policy_stats", + true, + a -> { + long runs = (long) a[0]; + long failed = (long) a[1]; + long timedOut = (long) a[2]; + long timeMs = (long) a[3]; + Map policyStatsMap = ((List) a[4]).stream() + .collect(Collectors.toMap(m -> m.policyId, Function.identity())); + return new SnapshotLifecycleStats(runs, failed, timedOut, timeMs, policyStatsMap); + } + ); static { PARSER.declareLong(ConstructingObjectParser.constructorArg(), RETENTION_RUNS); @@ -67,8 +68,13 @@ public class SnapshotLifecycleStats implements ToXContentObject { } // Package visible for testing - private SnapshotLifecycleStats(long retentionRuns, long retentionFailed, long retentionTimedOut, long retentionTimeMs, - Map policyStats) { + private SnapshotLifecycleStats( + long retentionRuns, + long retentionFailed, + long retentionTimedOut, + long retentionTimeMs, + Map policyStats + ) { this.retentionRunCount = retentionRuns; this.retentionFailedCount = retentionFailed; this.retentionTimedOut = retentionTimedOut; @@ -148,11 +154,11 @@ public boolean equals(Object obj) { return false; } SnapshotLifecycleStats other = (SnapshotLifecycleStats) obj; - return retentionRunCount == other.retentionRunCount && - retentionFailedCount == other.retentionFailedCount && - retentionTimedOut == other.retentionTimedOut && - retentionTimeMs == other.retentionTimeMs && - Objects.equals(policyStats, other.policyStats); + return retentionRunCount == other.retentionRunCount + && retentionFailedCount == other.retentionFailedCount + && retentionTimedOut == other.retentionTimedOut + && retentionTimeMs == other.retentionTimeMs + && Objects.equals(policyStats, other.policyStats); } @Override @@ -173,16 +179,18 @@ public static class SnapshotPolicyStats implements ToXContentFragment { static final ParseField SNAPSHOTS_DELETED = new ParseField("snapshots_deleted"); static final ParseField SNAPSHOT_DELETION_FAILURES = new ParseField("snapshot_deletion_failures"); - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("snapshot_policy_stats", true, - a -> { - String id = (String) a[0]; - long taken = (long) a[1]; - long failed = (long) a[2]; - long deleted = (long) a[3]; - long deleteFailed = (long) a[4]; - return new SnapshotPolicyStats(id, taken, failed, deleted, deleteFailed); - }); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "snapshot_policy_stats", + true, + a -> { + String id = (String) a[0]; + long taken = (long) a[1]; + long failed = (long) a[2]; + long deleted = (long) a[3]; + long deleteFailed = (long) a[4]; + return new SnapshotPolicyStats(id, taken, failed, deleted, deleteFailed); + } + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), POLICY_ID); @@ -238,11 +246,11 @@ public boolean equals(Object obj) { return false; } SnapshotPolicyStats other = (SnapshotPolicyStats) obj; - return Objects.equals(policyId, other.policyId) && - snapshotsTaken == other.snapshotsTaken && - snapshotsFailed == other.snapshotsFailed && - snapshotsDeleted == other.snapshotsDeleted && - snapshotDeleteFailures == other.snapshotDeleteFailures; + return Objects.equals(policyId, other.policyId) + && snapshotsTaken == other.snapshotsTaken + && snapshotsFailed == other.snapshotsFailed + && snapshotsDeleted == other.snapshotsDeleted + && snapshotDeleteFailures == other.snapshotDeleteFailures; } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/slm/SnapshotRetentionConfiguration.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/slm/SnapshotRetentionConfiguration.java index 85bb75b1cccdb..53d10d119d1bf 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/slm/SnapshotRetentionConfiguration.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/slm/SnapshotRetentionConfiguration.java @@ -8,11 +8,11 @@ package org.elasticsearch.client.slm; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -28,13 +28,16 @@ public class SnapshotRetentionConfiguration implements ToXContentObject { private static final ParseField MINIMUM_SNAPSHOT_COUNT = new ParseField("min_count"); private static final ParseField MAXIMUM_SNAPSHOT_COUNT = new ParseField("max_count"); - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("snapshot_retention", true, a -> { + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "snapshot_retention", + true, + a -> { TimeValue expireAfter = a[0] == null ? null : TimeValue.parseTimeValue((String) a[0], EXPIRE_AFTER.getPreferredName()); Integer minCount = (Integer) a[1]; Integer maxCount = (Integer) a[2]; return new SnapshotRetentionConfiguration(expireAfter, minCount, maxCount); - }); + } + ); static { PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), EXPIRE_AFTER); @@ -46,9 +49,11 @@ public class SnapshotRetentionConfiguration implements ToXContentObject { private final Integer minimumSnapshotCount; private final Integer maximumSnapshotCount; - public SnapshotRetentionConfiguration(@Nullable TimeValue expireAfter, - @Nullable Integer minimumSnapshotCount, - @Nullable Integer maximumSnapshotCount) { + public SnapshotRetentionConfiguration( + @Nullable TimeValue expireAfter, + @Nullable Integer minimumSnapshotCount, + @Nullable Integer maximumSnapshotCount + ) { this.expireAfter = expireAfter; this.minimumSnapshotCount = minimumSnapshotCount; this.maximumSnapshotCount = maximumSnapshotCount; @@ -59,8 +64,12 @@ public SnapshotRetentionConfiguration(@Nullable TimeValue expireAfter, throw new IllegalArgumentException("maximum snapshot count must be at least 1, but was: " + this.maximumSnapshotCount); } if ((maximumSnapshotCount != null && minimumSnapshotCount != null) && this.minimumSnapshotCount > this.maximumSnapshotCount) { - throw new IllegalArgumentException("minimum snapshot count " + this.minimumSnapshotCount + - " cannot be larger than maximum snapshot count " + this.maximumSnapshotCount); + throw new IllegalArgumentException( + "minimum snapshot count " + + this.minimumSnapshotCount + + " cannot be larger than maximum snapshot count " + + this.maximumSnapshotCount + ); } } @@ -110,9 +119,9 @@ public boolean equals(Object obj) { return false; } SnapshotRetentionConfiguration other = (SnapshotRetentionConfiguration) obj; - return Objects.equals(this.expireAfter, other.expireAfter) && - Objects.equals(minimumSnapshotCount, other.minimumSnapshotCount) && - Objects.equals(maximumSnapshotCount, other.maximumSnapshotCount); + return Objects.equals(this.expireAfter, other.expireAfter) + && Objects.equals(minimumSnapshotCount, other.minimumSnapshotCount) + && Objects.equals(maximumSnapshotCount, other.maximumSnapshotCount); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/slm/StartSLMRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/slm/StartSLMRequest.java index 0697a9a1199d1..9aeae09215baf 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/slm/StartSLMRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/slm/StartSLMRequest.java @@ -10,5 +10,4 @@ import org.elasticsearch.client.TimedRequest; -public class StartSLMRequest extends TimedRequest { -} +public class StartSLMRequest extends TimedRequest {} diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/slm/StopSLMRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/slm/StopSLMRequest.java index bf9845fd762de..d0e0ddce79d10 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/slm/StopSLMRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/slm/StopSLMRequest.java @@ -10,5 +10,4 @@ import org.elasticsearch.client.TimedRequest; -public class StopSLMRequest extends TimedRequest { -} +public class StopSLMRequest extends TimedRequest {} diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/CancelTasksRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/CancelTasksRequest.java index 9124faa73f5d4..c4b4a34b0e872 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/CancelTasksRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/CancelTasksRequest.java @@ -24,7 +24,7 @@ public class CancelTasksRequest implements Validatable { private Optional taskId = Optional.empty(); private Boolean waitForCompletion; - CancelTasksRequest(){} + CancelTasksRequest() {} void setNodes(List nodes) { this.nodes.addAll(nodes); @@ -79,12 +79,12 @@ public boolean equals(Object o) { if (this == o) return true; if ((o instanceof CancelTasksRequest) == false) return false; CancelTasksRequest that = (CancelTasksRequest) o; - return Objects.equals(getNodes(), that.getNodes()) && - Objects.equals(getActions(), that.getActions()) && - Objects.equals(getTimeout(), that.getTimeout()) && - Objects.equals(getParentTaskId(), that.getParentTaskId()) && - Objects.equals(getTaskId(), that.getTaskId()) && - Objects.equals(waitForCompletion, that.waitForCompletion); + return Objects.equals(getNodes(), that.getNodes()) + && Objects.equals(getActions(), that.getActions()) + && Objects.equals(getTimeout(), that.getTimeout()) + && Objects.equals(getParentTaskId(), that.getParentTaskId()) + && Objects.equals(getTaskId(), that.getTaskId()) + && Objects.equals(waitForCompletion, that.waitForCompletion); } @Override @@ -94,14 +94,20 @@ public int hashCode() { @Override public String toString() { - return "CancelTasksRequest{" + - "nodes=" + nodes + - ", actions=" + actions + - ", timeout=" + timeout + - ", parentTaskId=" + parentTaskId + - ", taskId=" + taskId + - ", waitForCompletion=" + waitForCompletion + - '}'; + return "CancelTasksRequest{" + + "nodes=" + + nodes + + ", actions=" + + actions + + ", timeout=" + + timeout + + ", parentTaskId=" + + parentTaskId + + ", taskId=" + + taskId + + ", waitForCompletion=" + + waitForCompletion + + '}'; } public static class Builder { @@ -112,28 +118,28 @@ public static class Builder { private List nodesFilter = new ArrayList<>(); private Boolean waitForCompletion; - public Builder withTimeout(TimeValue timeout){ + public Builder withTimeout(TimeValue timeout) { this.timeout = Optional.of(timeout); return this; } - public Builder withTaskId(TaskId taskId){ + public Builder withTaskId(TaskId taskId) { this.taskId = Optional.of(taskId); return this; } - public Builder withParentTaskId(TaskId taskId){ + public Builder withParentTaskId(TaskId taskId) { this.parentTaskId = Optional.of(taskId); return this; } - public Builder withActionsFiltered(List actions){ + public Builder withActionsFiltered(List actions) { this.actionsFilter.clear(); this.actionsFilter.addAll(actions); return this; } - public Builder withNodesFiltered(List nodes){ + public Builder withNodesFiltered(List nodes) { this.nodesFilter.clear(); this.nodesFilter.addAll(nodes); return this; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/CancelTasksResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/CancelTasksResponse.java index 18146471967f6..9023a53fcfe25 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/CancelTasksResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/CancelTasksResponse.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.tasks; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -24,9 +24,7 @@ */ public class CancelTasksResponse extends ListTasksResponse { - CancelTasksResponse(List nodesInfoData, - List taskFailures, - List nodeFailures) { + CancelTasksResponse(List nodesInfoData, List taskFailures, List nodeFailures) { super(nodesInfoData, taskFailures, nodeFailures); } @@ -37,7 +35,9 @@ public static CancelTasksResponse fromXContent(final XContentParser parser) thro private static ConstructingObjectParser PARSER; static { - ConstructingObjectParser parser = new ConstructingObjectParser<>("cancel_tasks_response", true, + ConstructingObjectParser parser = new ConstructingObjectParser<>( + "cancel_tasks_response", + true, constructingObjects -> { int i = 0; @SuppressWarnings("unchecked") @@ -47,12 +47,19 @@ public static CancelTasksResponse fromXContent(final XContentParser parser) thro @SuppressWarnings("unchecked") List nodesInfoData = (List) constructingObjects[i]; return new CancelTasksResponse(nodesInfoData, tasksFailures, nodeFailures); - }); + } + ); - parser.declareObjectArray(optionalConstructorArg(), (p, c) -> - TaskOperationFailure.fromXContent(p), new ParseField("task_failures")); - parser.declareObjectArray(optionalConstructorArg(), (p, c) -> - ElasticsearchException.fromXContent(p), new ParseField("node_failures")); + parser.declareObjectArray( + optionalConstructorArg(), + (p, c) -> TaskOperationFailure.fromXContent(p), + new ParseField("task_failures") + ); + parser.declareObjectArray( + optionalConstructorArg(), + (p, c) -> ElasticsearchException.fromXContent(p), + new ParseField("node_failures") + ); parser.declareNamedObjects(optionalConstructorArg(), NodeData.PARSER, new ParseField("nodes")); PARSER = parser; } @@ -69,12 +76,17 @@ public int hashCode() { @Override public String toString() { - return "CancelTasksResponse{" + - "taskFailures=" + taskFailures + - ", nodeFailures=" + nodeFailures + - ", nodesInfoData=" + nodesInfoData + - ", tasks=" + tasks + - ", taskGroups=" + taskGroups + - '}'; + return "CancelTasksResponse{" + + "taskFailures=" + + taskFailures + + ", nodeFailures=" + + nodeFailures + + ", nodesInfoData=" + + nodesInfoData + + ", tasks=" + + tasks + + ", taskGroups=" + + taskGroups + + '}'; } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/ElasticsearchException.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/ElasticsearchException.java index b27b56920a69f..88bc922e01ac7 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/ElasticsearchException.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/ElasticsearchException.java @@ -6,8 +6,10 @@ * Side Public License, v 1. */ package org.elasticsearch.client.tasks; + import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; + import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; @@ -59,7 +61,7 @@ public List getSuppressed() { return suppressed; } - void addSuppressed(List suppressed){ + void addSuppressed(List suppressed) { this.suppressed.addAll(suppressed); } @@ -167,7 +169,7 @@ private static ElasticsearchException innerFromXContent(XContentParser parser, b } void addHeader(String key, List value) { - headers.put(key,value); + headers.put(key, value); } @@ -191,10 +193,10 @@ public boolean equals(Object o) { if (this == o) return true; if ((o instanceof ElasticsearchException) == false) return false; ElasticsearchException that = (ElasticsearchException) o; - return Objects.equals(getMsg(), that.getMsg()) && - Objects.equals(getCause(), that.getCause()) && - Objects.equals(getHeaders(), that.getHeaders()) && - Objects.equals(getSuppressed(), that.getSuppressed()); + return Objects.equals(getMsg(), that.getMsg()) + && Objects.equals(getCause(), that.getCause()) + && Objects.equals(getHeaders(), that.getHeaders()) + && Objects.equals(getSuppressed(), that.getSuppressed()); } @Override @@ -204,11 +206,16 @@ public int hashCode() { @Override public String toString() { - return "ElasticsearchException{" + - "msg='" + msg + '\'' + - ", cause=" + cause + - ", headers=" + headers + - ", suppressed=" + suppressed + - '}'; + return "ElasticsearchException{" + + "msg='" + + msg + + '\'' + + ", cause=" + + cause + + ", headers=" + + headers + + ", suppressed=" + + suppressed + + '}'; } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/GetTaskRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/GetTaskRequest.java index 958bbd81f8fd6..070bec0718511 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/GetTaskRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/GetTaskRequest.java @@ -89,9 +89,9 @@ public boolean equals(Object obj) { return false; } GetTaskRequest other = (GetTaskRequest) obj; - return Objects.equals(nodeId, other.nodeId) && - taskId == other.taskId && - waitForCompletion == other.waitForCompletion && - Objects.equals(timeout, other.timeout); + return Objects.equals(nodeId, other.nodeId) + && taskId == other.taskId + && waitForCompletion == other.waitForCompletion + && Objects.equals(timeout, other.timeout); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/GetTaskResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/GetTaskResponse.java index f463db3549340..64159b199488d 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/GetTaskResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/GetTaskResponse.java @@ -7,10 +7,10 @@ */ package org.elasticsearch.client.tasks; -import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.tasks.TaskInfo; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.tasks.TaskInfo; import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg; @@ -33,8 +33,11 @@ public TaskInfo getTaskInfo() { return taskInfo; } - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("get_task", - true, a -> new GetTaskResponse((boolean) a[0], (TaskInfo) a[1])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "get_task", + true, + a -> new GetTaskResponse((boolean) a[0], (TaskInfo) a[1]) + ); static { PARSER.declareBoolean(constructorArg(), COMPLETED); PARSER.declareObject(constructorArg(), (p, c) -> TaskInfo.fromXContent(p), TASK); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/ListTasksResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/ListTasksResponse.java index 84d10b0354517..793dfbc82683b 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/ListTasksResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/ListTasksResponse.java @@ -25,9 +25,7 @@ public class ListTasksResponse { protected final List tasks = new ArrayList<>(); protected final List taskGroups = new ArrayList<>(); - ListTasksResponse(List nodesInfoData, - List taskFailures, - List nodeFailures) { + ListTasksResponse(List nodesInfoData, List taskFailures, List nodeFailures) { if (taskFailures != null) { this.taskFailures.addAll(taskFailures); } @@ -37,12 +35,7 @@ public class ListTasksResponse { if (nodesInfoData != null) { this.nodesInfoData.addAll(nodesInfoData); } - this.tasks.addAll(this - .nodesInfoData - .stream() - .flatMap(nodeData -> nodeData.getTasks().stream()) - .collect(toList()) - ); + this.tasks.addAll(this.nodesInfoData.stream().flatMap(nodeData -> nodeData.getTasks().stream()).collect(toList())); this.taskGroups.addAll(buildTaskGroups()); } @@ -79,9 +72,7 @@ public List getTasks() { } public Map> getPerNodeTasks() { - return getTasks() - .stream() - .collect(groupingBy(TaskInfo::getNodeId)); + return getTasks().stream().collect(groupingBy(TaskInfo::getNodeId)); } public List getTaskFailures() { @@ -101,12 +92,11 @@ public boolean equals(Object o) { if (this == o) return true; if ((o instanceof ListTasksResponse) == false) return false; ListTasksResponse response = (ListTasksResponse) o; - return nodesInfoData.equals(response.nodesInfoData) && - Objects.equals - (getTaskFailures(), response.getTaskFailures()) && - Objects.equals(getNodeFailures(), response.getNodeFailures()) && - Objects.equals(getTasks(), response.getTasks()) && - Objects.equals(getTaskGroups(), response.getTaskGroups()); + return nodesInfoData.equals(response.nodesInfoData) + && Objects.equals(getTaskFailures(), response.getTaskFailures()) + && Objects.equals(getNodeFailures(), response.getNodeFailures()) + && Objects.equals(getTasks(), response.getTasks()) + && Objects.equals(getTaskGroups(), response.getTaskGroups()); } @Override @@ -116,12 +106,17 @@ public int hashCode() { @Override public String toString() { - return "CancelTasksResponse{" + - "nodesInfoData=" + nodesInfoData + - ", taskFailures=" + taskFailures + - ", nodeFailures=" + nodeFailures + - ", tasks=" + tasks + - ", taskGroups=" + taskGroups + - '}'; + return "CancelTasksResponse{" + + "nodesInfoData=" + + nodesInfoData + + ", taskFailures=" + + taskFailures + + ", nodeFailures=" + + nodeFailures + + ", tasks=" + + tasks + + ", taskGroups=" + + taskGroups + + '}'; } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/NodeData.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/NodeData.java index ff02fe2547604..1e33b7ede7e48 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/NodeData.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/NodeData.java @@ -7,14 +7,15 @@ */ package org.elasticsearch.client.tasks; +import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentParser; + import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; -import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.xcontent.ObjectParser; -import org.elasticsearch.xcontent.XContentParser; class NodeData { @@ -24,7 +25,7 @@ class NodeData { private String host; private String ip; private final List roles = new ArrayList<>(); - private final Map attributes = new HashMap<>(); + private final Map attributes = new HashMap<>(); private final List tasks = new ArrayList<>(); NodeData(String nodeId) { @@ -36,7 +37,7 @@ void setName(String name) { } public void setAttributes(Map attributes) { - if(attributes!=null){ + if (attributes != null) { this.attributes.putAll(attributes); } } @@ -54,7 +55,7 @@ void setIp(String ip) { } void setRoles(List roles) { - if(roles!=null){ + if (roles != null) { this.roles.addAll(roles); } } @@ -92,22 +93,34 @@ public List getTasks() { } void setTasks(List tasks) { - if(tasks!=null){ + if (tasks != null) { this.tasks.addAll(tasks); } } @Override public String toString() { - return "NodeData{" + - "nodeId='" + nodeId + '\'' + - ", name='" + name + '\'' + - ", transportAddress='" + transportAddress + '\'' + - ", host='" + host + '\'' + - ", ip='" + ip + '\'' + - ", roles=" + roles + - ", attributes=" + attributes + - '}'; + return "NodeData{" + + "nodeId='" + + nodeId + + '\'' + + ", name='" + + name + + '\'' + + ", transportAddress='" + + transportAddress + + '\'' + + ", host='" + + host + + '\'' + + ", ip='" + + ip + + '\'' + + ", roles=" + + roles + + ", attributes=" + + attributes + + '}'; } @Override @@ -115,14 +128,14 @@ public boolean equals(Object o) { if (this == o) return true; if ((o instanceof NodeData) == false) return false; NodeData nodeData = (NodeData) o; - return Objects.equals(getNodeId(), nodeData.getNodeId()) && - Objects.equals(getName(), nodeData.getName()) && - Objects.equals(getTransportAddress(), nodeData.getTransportAddress()) && - Objects.equals(getHost(), nodeData.getHost()) && - Objects.equals(getIp(), nodeData.getIp()) && - Objects.equals(getRoles(), nodeData.getRoles()) && - Objects.equals(getAttributes(), nodeData.getAttributes()) && - Objects.equals(getTasks(), nodeData.getTasks()); + return Objects.equals(getNodeId(), nodeData.getNodeId()) + && Objects.equals(getName(), nodeData.getName()) + && Objects.equals(getTransportAddress(), nodeData.getTransportAddress()) + && Objects.equals(getHost(), nodeData.getHost()) + && Objects.equals(getIp(), nodeData.getIp()) + && Objects.equals(getRoles(), nodeData.getRoles()) + && Objects.equals(getAttributes(), nodeData.getAttributes()) + && Objects.equals(getTasks(), nodeData.getTasks()); } @Override @@ -139,10 +152,7 @@ public int hashCode() { parser.declareString(NodeData::setHost, new ParseField("host")); parser.declareString(NodeData::setIp, new ParseField("ip")); parser.declareStringArray(NodeData::setRoles, new ParseField("roles")); - parser.declareField(NodeData::setAttributes, - (p, c) -> p.mapStrings(), - new ParseField("attributes"), - ObjectParser.ValueType.OBJECT); + parser.declareField(NodeData::setAttributes, (p, c) -> p.mapStrings(), new ParseField("attributes"), ObjectParser.ValueType.OBJECT); parser.declareNamedObjects(NodeData::setTasks, TaskInfo.PARSER, new ParseField("tasks")); PARSER = (XContentParser p, Void v, String nodeId) -> parser.parse(p, new NodeData(nodeId), null); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/TaskGroup.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/TaskGroup.java index 8f730be2d2b85..a8c0e299485fc 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/TaskGroup.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/TaskGroup.java @@ -23,10 +23,7 @@ public class TaskGroup { @Override public String toString() { - return "TaskGroup{" + - "task=" + task + - ", childTasks=" + childTasks + - '}'; + return "TaskGroup{" + "task=" + task + ", childTasks=" + childTasks + '}'; } private final List childTasks = new ArrayList<>(); @@ -45,8 +42,7 @@ public boolean equals(Object o) { if (this == o) return true; if ((o instanceof TaskGroup) == false) return false; TaskGroup taskGroup = (TaskGroup) o; - return Objects.equals(task, taskGroup.task) && - Objects.equals(getChildTasks(), taskGroup.getChildTasks()); + return Objects.equals(task, taskGroup.task) && Objects.equals(getChildTasks(), taskGroup.getChildTasks()); } @Override @@ -72,10 +68,7 @@ public TaskInfo getTaskInfo() { } public TaskGroup build() { - return new TaskGroup( - taskInfo, - childTasks.stream().map(TaskGroup.Builder::build).collect(Collectors.toList()) - ); + return new TaskGroup(taskInfo, childTasks.stream().map(TaskGroup.Builder::build).collect(Collectors.toList())); } } @@ -87,4 +80,3 @@ public List getChildTasks() { return childTasks; } } - diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/TaskId.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/TaskId.java index 367b12a2408a4..e733d82eea13d 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/TaskId.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/TaskId.java @@ -63,14 +63,12 @@ public String toString() { } } - @Override public boolean equals(Object o) { if (this == o) return true; if ((o instanceof TaskId) == false) return false; TaskId taskId = (TaskId) o; - return getId() == taskId.getId() && - Objects.equals(getNodeId(), taskId.getNodeId()); + return getId() == taskId.getId() && Objects.equals(getNodeId(), taskId.getNodeId()); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/TaskInfo.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/TaskInfo.java index 6d8187c7d4420..2c91be19b3db4 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/TaskInfo.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/TaskInfo.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.tasks; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.util.HashMap; @@ -154,51 +154,64 @@ public boolean equals(Object o) { if (this == o) return true; if ((o instanceof TaskInfo) == false) return false; TaskInfo taskInfo = (TaskInfo) o; - return getStartTime() == taskInfo.getStartTime() && - getRunningTimeNanos() == taskInfo.getRunningTimeNanos() && - isCancellable() == taskInfo.isCancellable() && - isCancelled() == taskInfo.isCancelled() && - Objects.equals(getTaskId(), taskInfo.getTaskId()) && - Objects.equals(getType(), taskInfo.getType()) && - Objects.equals(getAction(), taskInfo.getAction()) && - Objects.equals(getDescription(), taskInfo.getDescription()) && - Objects.equals(getParentTaskId(), taskInfo.getParentTaskId()) && - Objects.equals(status, taskInfo.status) && - Objects.equals(getHeaders(), taskInfo.getHeaders()); + return getStartTime() == taskInfo.getStartTime() + && getRunningTimeNanos() == taskInfo.getRunningTimeNanos() + && isCancellable() == taskInfo.isCancellable() + && isCancelled() == taskInfo.isCancelled() + && Objects.equals(getTaskId(), taskInfo.getTaskId()) + && Objects.equals(getType(), taskInfo.getType()) + && Objects.equals(getAction(), taskInfo.getAction()) + && Objects.equals(getDescription(), taskInfo.getDescription()) + && Objects.equals(getParentTaskId(), taskInfo.getParentTaskId()) + && Objects.equals(status, taskInfo.status) + && Objects.equals(getHeaders(), taskInfo.getHeaders()); } @Override public int hashCode() { return Objects.hash( - getTaskId(), - getType(), - getAction(), - getDescription(), - getStartTime(), - getRunningTimeNanos(), - isCancellable(), - isCancelled(), - getParentTaskId(), - status, - getHeaders() + getTaskId(), + getType(), + getAction(), + getDescription(), + getStartTime(), + getRunningTimeNanos(), + isCancellable(), + isCancelled(), + getParentTaskId(), + status, + getHeaders() ); } - @Override public String toString() { - return "TaskInfo{" + - "taskId=" + taskId + - ", type='" + type + '\'' + - ", action='" + action + '\'' + - ", description='" + description + '\'' + - ", startTime=" + startTime + - ", runningTimeNanos=" + runningTimeNanos + - ", cancellable=" + cancellable + - ", cancelled=" + cancelled + - ", parentTaskId=" + parentTaskId + - ", status=" + status + - ", headers=" + headers + - '}'; + return "TaskInfo{" + + "taskId=" + + taskId + + ", type='" + + type + + '\'' + + ", action='" + + action + + '\'' + + ", description='" + + description + + '\'' + + ", startTime=" + + startTime + + ", runningTimeNanos=" + + runningTimeNanos + + ", cancellable=" + + cancellable + + ", cancelled=" + + cancelled + + ", parentTaskId=" + + parentTaskId + + ", status=" + + status + + ", headers=" + + headers + + '}'; } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/TaskOperationFailure.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/TaskOperationFailure.java index daea614163d57..063fb955a8f2b 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/TaskOperationFailure.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/TaskOperationFailure.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.tasks; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.util.Objects; @@ -26,7 +26,7 @@ public class TaskOperationFailure { private final ElasticsearchException reason; private final String status; - public TaskOperationFailure(String nodeId, long taskId,String status, ElasticsearchException reason) { + public TaskOperationFailure(String nodeId, long taskId, String status, ElasticsearchException reason) { this.nodeId = nodeId; this.taskId = taskId; this.status = status; @@ -54,38 +54,49 @@ public boolean equals(Object o) { if (this == o) return true; if ((o instanceof TaskOperationFailure) == false) return false; TaskOperationFailure that = (TaskOperationFailure) o; - return getTaskId() == that.getTaskId() && - Objects.equals(getNodeId(), that.getNodeId()) && - Objects.equals(getReason(), that.getReason()) && - Objects.equals(getStatus(), that.getStatus()); + return getTaskId() == that.getTaskId() + && Objects.equals(getNodeId(), that.getNodeId()) + && Objects.equals(getReason(), that.getReason()) + && Objects.equals(getStatus(), that.getStatus()); } @Override public int hashCode() { return Objects.hash(getNodeId(), getTaskId(), getReason(), getStatus()); } + @Override public String toString() { - return "TaskOperationFailure{" + - "nodeId='" + nodeId + '\'' + - ", taskId=" + taskId + - ", reason=" + reason + - ", status='" + status + '\'' + - '}'; + return "TaskOperationFailure{" + + "nodeId='" + + nodeId + + '\'' + + ", taskId=" + + taskId + + ", reason=" + + reason + + ", status='" + + status + + '\'' + + '}'; } + public static TaskOperationFailure fromXContent(XContentParser parser) { return PARSER.apply(parser, null); } - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("task_info", true, constructorObjects -> { + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "task_info", + true, + constructorObjects -> { int i = 0; String nodeId = (String) constructorObjects[i++]; long taskId = (long) constructorObjects[i++]; String status = (String) constructorObjects[i++]; ElasticsearchException reason = (ElasticsearchException) constructorObjects[i]; return new TaskOperationFailure(nodeId, taskId, status, reason); - }); + } + ); static { PARSER.declareString(constructorArg(), new ParseField("node_id")); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/TaskSubmissionResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/TaskSubmissionResponse.java index 8e52ab46572d2..1dde406205a69 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/TaskSubmissionResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/tasks/TaskSubmissionResponse.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.tasks; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -21,7 +21,9 @@ public class TaskSubmissionResponse { public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( "task_submission_response", - true, a -> new TaskSubmissionResponse((String) a[0])); + true, + a -> new TaskSubmissionResponse((String) a[0]) + ); static { PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), TASK); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/textstructure/FindStructureRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/textstructure/FindStructureRequest.java index cd65b8e53a0ad..1413c5cc661c4 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/textstructure/FindStructureRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/textstructure/FindStructureRequest.java @@ -10,10 +10,10 @@ import org.elasticsearch.client.Validatable; import org.elasticsearch.client.ValidationException; import org.elasticsearch.client.textstructure.structurefinder.TextStructure; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentFragment; import org.elasticsearch.xcontent.XContentBuilder; @@ -57,8 +57,7 @@ public class FindStructureRequest implements Validatable, ToXContentFragment { private Boolean explain; private BytesReference sample; - public FindStructureRequest() { - } + public FindStructureRequest() {} public Integer getLinesToSample() { return linesToSample; @@ -272,8 +271,21 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws @Override public int hashCode() { - return Objects.hash(linesToSample, lineMergeSizeLimit, timeout, charset, format, columnNames, hasHeaderRow, delimiter, grokPattern, - timestampFormat, timestampField, explain, sample); + return Objects.hash( + linesToSample, + lineMergeSizeLimit, + timeout, + charset, + format, + columnNames, + hasHeaderRow, + delimiter, + grokPattern, + timestampFormat, + timestampField, + explain, + sample + ); } @Override @@ -288,18 +300,18 @@ public boolean equals(Object other) { } FindStructureRequest that = (FindStructureRequest) other; - return Objects.equals(this.linesToSample, that.linesToSample) && - Objects.equals(this.lineMergeSizeLimit, that.lineMergeSizeLimit) && - Objects.equals(this.timeout, that.timeout) && - Objects.equals(this.charset, that.charset) && - Objects.equals(this.format, that.format) && - Objects.equals(this.columnNames, that.columnNames) && - Objects.equals(this.hasHeaderRow, that.hasHeaderRow) && - Objects.equals(this.delimiter, that.delimiter) && - Objects.equals(this.grokPattern, that.grokPattern) && - Objects.equals(this.timestampFormat, that.timestampFormat) && - Objects.equals(this.timestampField, that.timestampField) && - Objects.equals(this.explain, that.explain) && - Objects.equals(this.sample, that.sample); + return Objects.equals(this.linesToSample, that.linesToSample) + && Objects.equals(this.lineMergeSizeLimit, that.lineMergeSizeLimit) + && Objects.equals(this.timeout, that.timeout) + && Objects.equals(this.charset, that.charset) + && Objects.equals(this.format, that.format) + && Objects.equals(this.columnNames, that.columnNames) + && Objects.equals(this.hasHeaderRow, that.hasHeaderRow) + && Objects.equals(this.delimiter, that.delimiter) + && Objects.equals(this.grokPattern, that.grokPattern) + && Objects.equals(this.timestampFormat, that.timestampFormat) + && Objects.equals(this.timestampField, that.timestampField) + && Objects.equals(this.explain, that.explain) + && Objects.equals(this.sample, that.sample); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/textstructure/structurefinder/FieldStats.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/textstructure/structurefinder/FieldStats.java index 5b18414e609dd..8e38fbbc0ddc1 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/textstructure/structurefinder/FieldStats.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/textstructure/structurefinder/FieldStats.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.textstructure.structurefinder; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -32,9 +32,21 @@ public class FieldStats implements ToXContentObject { public static final ParseField TOP_HITS = new ParseField("top_hits"); @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("field_stats", true, - a -> new FieldStats((long) a[0], (int) a[1], (Double) a[2], (Double) a[3], (Double) a[4], (Double) a[5], - (String) a[6], (String) a[7], (List>) a[8])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "field_stats", + true, + a -> new FieldStats( + (long) a[0], + (int) a[1], + (Double) a[2], + (Double) a[3], + (Double) a[4], + (Double) a[5], + (String) a[6], + (String) a[7], + (List>) a[8] + ) + ); static { PARSER.declareLong(ConstructingObjectParser.constructorArg(), COUNT); @@ -58,8 +70,17 @@ public class FieldStats implements ToXContentObject { private final String latestTimestamp; private final List> topHits; - FieldStats(long count, int cardinality, Double minValue, Double maxValue, Double meanValue, Double medianValue, - String earliestTimestamp, String latestTimestamp, List> topHits) { + FieldStats( + long count, + int cardinality, + Double minValue, + Double maxValue, + Double meanValue, + Double medianValue, + String earliestTimestamp, + String latestTimestamp, + List> topHits + ) { this.count = count; this.cardinality = cardinality; this.minValue = minValue; @@ -166,14 +187,14 @@ public boolean equals(Object other) { } FieldStats that = (FieldStats) other; - return this.count == that.count && - this.cardinality == that.cardinality && - Objects.equals(this.minValue, that.minValue) && - Objects.equals(this.maxValue, that.maxValue) && - Objects.equals(this.meanValue, that.meanValue) && - Objects.equals(this.medianValue, that.medianValue) && - Objects.equals(this.earliestTimestamp, that.earliestTimestamp) && - Objects.equals(this.latestTimestamp, that.latestTimestamp) && - Objects.equals(this.topHits, that.topHits); + return this.count == that.count + && this.cardinality == that.cardinality + && Objects.equals(this.minValue, that.minValue) + && Objects.equals(this.maxValue, that.maxValue) + && Objects.equals(this.meanValue, that.meanValue) + && Objects.equals(this.medianValue, that.medianValue) + && Objects.equals(this.earliestTimestamp, that.earliestTimestamp) + && Objects.equals(this.latestTimestamp, that.latestTimestamp) + && Objects.equals(this.topHits, that.topHits); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/textstructure/structurefinder/TextStructure.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/textstructure/structurefinder/TextStructure.java index 5bf5825e56d41..4abd1f3c7ce2b 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/textstructure/structurefinder/TextStructure.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/textstructure/structurefinder/TextStructure.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.textstructure.structurefinder; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -30,7 +30,10 @@ public class TextStructure implements ToXContentObject { public enum Format { - NDJSON, XML, DELIMITED, SEMI_STRUCTURED_TEXT; + NDJSON, + XML, + DELIMITED, + SEMI_STRUCTURED_TEXT; public static Format fromString(String name) { return valueOf(name.trim().toUpperCase(Locale.ROOT)); @@ -121,12 +124,30 @@ public String toString() { private final SortedMap fieldStats; private final List explanation; - private TextStructure(int numLinesAnalyzed, int numMessagesAnalyzed, String sampleStart, String charset, Boolean hasByteOrderMarker, - Format format, String multilineStartPattern, String excludeLinesPattern, List columnNames, - Boolean hasHeaderRow, Character delimiter, Character quote, Boolean shouldTrimFields, String grokPattern, - String timestampField, List jodaTimestampFormats, List javaTimestampFormats, - boolean needClientTimezone, Map mappings, Map ingestPipeline, - Map fieldStats, List explanation) { + private TextStructure( + int numLinesAnalyzed, + int numMessagesAnalyzed, + String sampleStart, + String charset, + Boolean hasByteOrderMarker, + Format format, + String multilineStartPattern, + String excludeLinesPattern, + List columnNames, + Boolean hasHeaderRow, + Character delimiter, + Character quote, + Boolean shouldTrimFields, + String grokPattern, + String timestampField, + List jodaTimestampFormats, + List javaTimestampFormats, + boolean needClientTimezone, + Map mappings, + Map ingestPipeline, + Map fieldStats, + List explanation + ) { this.numLinesAnalyzed = numLinesAnalyzed; this.numMessagesAnalyzed = numMessagesAnalyzed; @@ -308,9 +329,29 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws @Override public int hashCode() { - return Objects.hash(numLinesAnalyzed, numMessagesAnalyzed, sampleStart, charset, hasByteOrderMarker, format, - multilineStartPattern, excludeLinesPattern, columnNames, hasHeaderRow, delimiter, quote, shouldTrimFields, grokPattern, - timestampField, jodaTimestampFormats, javaTimestampFormats, needClientTimezone, mappings, fieldStats, explanation); + return Objects.hash( + numLinesAnalyzed, + numMessagesAnalyzed, + sampleStart, + charset, + hasByteOrderMarker, + format, + multilineStartPattern, + excludeLinesPattern, + columnNames, + hasHeaderRow, + delimiter, + quote, + shouldTrimFields, + grokPattern, + timestampField, + jodaTimestampFormats, + javaTimestampFormats, + needClientTimezone, + mappings, + fieldStats, + explanation + ); } @Override @@ -325,27 +366,27 @@ public boolean equals(Object other) { } TextStructure that = (TextStructure) other; - return this.numLinesAnalyzed == that.numLinesAnalyzed && - this.numMessagesAnalyzed == that.numMessagesAnalyzed && - Objects.equals(this.sampleStart, that.sampleStart) && - Objects.equals(this.charset, that.charset) && - Objects.equals(this.hasByteOrderMarker, that.hasByteOrderMarker) && - Objects.equals(this.format, that.format) && - Objects.equals(this.multilineStartPattern, that.multilineStartPattern) && - Objects.equals(this.excludeLinesPattern, that.excludeLinesPattern) && - Objects.equals(this.columnNames, that.columnNames) && - Objects.equals(this.hasHeaderRow, that.hasHeaderRow) && - Objects.equals(this.delimiter, that.delimiter) && - Objects.equals(this.quote, that.quote) && - Objects.equals(this.shouldTrimFields, that.shouldTrimFields) && - Objects.equals(this.grokPattern, that.grokPattern) && - Objects.equals(this.timestampField, that.timestampField) && - Objects.equals(this.jodaTimestampFormats, that.jodaTimestampFormats) && - Objects.equals(this.javaTimestampFormats, that.javaTimestampFormats) && - this.needClientTimezone == that.needClientTimezone && - Objects.equals(this.mappings, that.mappings) && - Objects.equals(this.fieldStats, that.fieldStats) && - Objects.equals(this.explanation, that.explanation); + return this.numLinesAnalyzed == that.numLinesAnalyzed + && this.numMessagesAnalyzed == that.numMessagesAnalyzed + && Objects.equals(this.sampleStart, that.sampleStart) + && Objects.equals(this.charset, that.charset) + && Objects.equals(this.hasByteOrderMarker, that.hasByteOrderMarker) + && Objects.equals(this.format, that.format) + && Objects.equals(this.multilineStartPattern, that.multilineStartPattern) + && Objects.equals(this.excludeLinesPattern, that.excludeLinesPattern) + && Objects.equals(this.columnNames, that.columnNames) + && Objects.equals(this.hasHeaderRow, that.hasHeaderRow) + && Objects.equals(this.delimiter, that.delimiter) + && Objects.equals(this.quote, that.quote) + && Objects.equals(this.shouldTrimFields, that.shouldTrimFields) + && Objects.equals(this.grokPattern, that.grokPattern) + && Objects.equals(this.timestampField, that.timestampField) + && Objects.equals(this.jodaTimestampFormats, that.jodaTimestampFormats) + && Objects.equals(this.javaTimestampFormats, that.javaTimestampFormats) + && this.needClientTimezone == that.needClientTimezone + && Objects.equals(this.mappings, that.mappings) + && Objects.equals(this.fieldStats, that.fieldStats) + && Objects.equals(this.explanation, that.explanation); } public static class Builder { @@ -493,10 +534,30 @@ Builder setExplanation(List explanation) { public TextStructure build() { - return new TextStructure(numLinesAnalyzed, numMessagesAnalyzed, sampleStart, charset, hasByteOrderMarker, format, - multilineStartPattern, excludeLinesPattern, columnNames, hasHeaderRow, delimiter, quote, shouldTrimFields, grokPattern, - timestampField, jodaTimestampFormats, javaTimestampFormats, needClientTimezone, mappings, ingestPipeline, fieldStats, - explanation); + return new TextStructure( + numLinesAnalyzed, + numMessagesAnalyzed, + sampleStart, + charset, + hasByteOrderMarker, + format, + multilineStartPattern, + excludeLinesPattern, + columnNames, + hasHeaderRow, + delimiter, + quote, + shouldTrimFields, + grokPattern, + timestampField, + jodaTimestampFormats, + javaTimestampFormats, + needClientTimezone, + mappings, + ingestPipeline, + fieldStats, + explanation + ); } } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/AcknowledgedTasksResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/AcknowledgedTasksResponse.java index e991ab353f884..9e83b3c4d3cd3 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/AcknowledgedTasksResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/AcknowledgedTasksResponse.java @@ -10,10 +10,10 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.action.TaskOperationFailure; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.TriFunction; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import java.util.Collections; import java.util.List; @@ -29,12 +29,16 @@ public class AcknowledgedTasksResponse { @SuppressWarnings("unchecked") protected static ConstructingObjectParser generateParser( - String name, - TriFunction, List, T> ctor, - String ackFieldName) { - - ConstructingObjectParser parser = new ConstructingObjectParser<>(name, true, - args -> ctor.apply((boolean) args[0], (List) args[1], (List) args[2])); + String name, + TriFunction, List, T> ctor, + String ackFieldName + ) { + + ConstructingObjectParser parser = new ConstructingObjectParser<>( + name, + true, + args -> ctor.apply((boolean) args[0], (List) args[1], (List) args[2]) + ); parser.declareBoolean(constructorArg(), new ParseField(ackFieldName)); parser.declareObjectArray(optionalConstructorArg(), (p, c) -> TaskOperationFailure.fromXContent(p), TASK_FAILURES); parser.declareObjectArray(optionalConstructorArg(), (p, c) -> ElasticsearchException.fromXContent(p), NODE_FAILURES); @@ -45,8 +49,11 @@ protected static ConstructingObjectParser< private List taskFailures; private List nodeFailures; - public AcknowledgedTasksResponse(boolean acknowledged, @Nullable List taskFailures, - @Nullable List nodeFailures) { + public AcknowledgedTasksResponse( + boolean acknowledged, + @Nullable List taskFailures, + @Nullable List nodeFailures + ) { this.acknowledged = acknowledged; this.taskFailures = taskFailures == null ? Collections.emptyList() : Collections.unmodifiableList(taskFailures); this.nodeFailures = nodeFailures == null ? Collections.emptyList() : Collections.unmodifiableList(nodeFailures); @@ -75,9 +82,7 @@ public boolean equals(Object obj) { } AcknowledgedTasksResponse other = (AcknowledgedTasksResponse) obj; - return acknowledged == other.acknowledged - && taskFailures.equals(other.taskFailures) - && nodeFailures.equals(other.nodeFailures); + return acknowledged == other.acknowledged && taskFailures.equals(other.taskFailures) && nodeFailures.equals(other.nodeFailures); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/DeleteTransformRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/DeleteTransformRequest.java index 2d7f36e746218..5ae07af98aaf7 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/DeleteTransformRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/DeleteTransformRequest.java @@ -14,7 +14,6 @@ import java.util.Objects; import java.util.Optional; - /** * Request to delete a transform */ diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/GetTransformRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/GetTransformRequest.java index 4b5804ab15685..8e8cb68bb6005 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/GetTransformRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/GetTransformRequest.java @@ -21,6 +21,7 @@ public class GetTransformRequest implements Validatable { public static final String EXCLUDE_GENERATED = "exclude_generated"; public static final String ALLOW_NO_MATCH = "allow_no_match"; + /** * Helper method to create a request that will get ALL Transforms * @return new {@link GetTransformRequest} object for the id "_all" diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/GetTransformResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/GetTransformResponse.java index 2ad3fbfc0ede4..e927bfbb4f7c2 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/GetTransformResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/GetTransformResponse.java @@ -10,8 +10,8 @@ import org.elasticsearch.client.transform.transforms.TransformConfig; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.util.List; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/GetTransformStatsResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/GetTransformStatsResponse.java index 8db23a20ab1c2..d4c230e1997f2 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/GetTransformStatsResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/GetTransformStatsResponse.java @@ -12,8 +12,8 @@ import org.elasticsearch.action.TaskOperationFailure; import org.elasticsearch.client.transform.transforms.TransformStats; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.util.Collections; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/PreviewTransformRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/PreviewTransformRequest.java index 68f5a87009565..89f6ee240459d 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/PreviewTransformRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/PreviewTransformRequest.java @@ -47,10 +47,7 @@ public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params par if (this.config != null) { return this.config.toXContent(builder, params); } else { - return builder - .startObject() - .field(TransformConfig.ID.getPreferredName(), this.transformId) - .endObject(); + return builder.startObject().field(TransformConfig.ID.getPreferredName(), this.transformId).endObject(); } } @@ -84,7 +81,6 @@ public boolean equals(Object obj) { return false; } PreviewTransformRequest other = (PreviewTransformRequest) obj; - return Objects.equals(transformId, other.transformId) - && Objects.equals(config, other.config); + return Objects.equals(transformId, other.transformId) && Objects.equals(config, other.config); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/PreviewTransformResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/PreviewTransformResponse.java index 47a5602a2dda7..637464d0f4f14 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/PreviewTransformResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/PreviewTransformResponse.java @@ -10,9 +10,9 @@ import org.elasticsearch.action.admin.indices.alias.Alias; import org.elasticsearch.client.indices.CreateIndexRequest; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/StartTransformRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/StartTransformRequest.java index 3db287086a746..fe68ab6d4521a 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/StartTransformRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/StartTransformRequest.java @@ -67,7 +67,6 @@ public boolean equals(Object obj) { return false; } StartTransformRequest other = (StartTransformRequest) obj; - return Objects.equals(this.id, other.id) - && Objects.equals(this.timeout, other.timeout); + return Objects.equals(this.id, other.id) && Objects.equals(this.timeout, other.timeout); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/StartTransformResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/StartTransformResponse.java index ab5e5f2e097e8..cc620000aec40 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/StartTransformResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/StartTransformResponse.java @@ -21,16 +21,21 @@ public class StartTransformResponse extends AcknowledgedTasksResponse { private static final String ACKNOWLEDGED = "acknowledged"; - private static final ConstructingObjectParser PARSER = - AcknowledgedTasksResponse.generateParser("start_transform_response", StartTransformResponse::new, - ACKNOWLEDGED); + private static final ConstructingObjectParser PARSER = AcknowledgedTasksResponse.generateParser( + "start_transform_response", + StartTransformResponse::new, + ACKNOWLEDGED + ); public static StartTransformResponse fromXContent(final XContentParser parser) throws IOException { return PARSER.parse(parser, null); } - public StartTransformResponse(boolean acknowledged, @Nullable List taskFailures, - @Nullable List nodeFailures) { + public StartTransformResponse( + boolean acknowledged, + @Nullable List taskFailures, + @Nullable List nodeFailures + ) { super(acknowledged, taskFailures, nodeFailures); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/StopTransformRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/StopTransformRequest.java index ac381389ba02f..8c7c3472a895f 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/StopTransformRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/StopTransformRequest.java @@ -99,10 +99,10 @@ public boolean equals(Object obj) { } StopTransformRequest other = (StopTransformRequest) obj; return Objects.equals(this.id, other.id) - && Objects.equals(this.waitForCompletion, other.waitForCompletion) - && Objects.equals(this.timeout, other.timeout) - && Objects.equals(this.waitForCheckpoint, other.waitForCheckpoint) - && Objects.equals(this.allowNoMatch, other.allowNoMatch); + && Objects.equals(this.waitForCompletion, other.waitForCompletion) + && Objects.equals(this.timeout, other.timeout) + && Objects.equals(this.waitForCheckpoint, other.waitForCheckpoint) + && Objects.equals(this.allowNoMatch, other.allowNoMatch); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/StopTransformResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/StopTransformResponse.java index 3f2a39fcc17ec..d2a27aeb41281 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/StopTransformResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/StopTransformResponse.java @@ -21,15 +21,21 @@ public class StopTransformResponse extends AcknowledgedTasksResponse { private static final String ACKNOWLEDGED = "acknowledged"; - private static final ConstructingObjectParser PARSER = AcknowledgedTasksResponse - .generateParser("stop_transform_response", StopTransformResponse::new, ACKNOWLEDGED); + private static final ConstructingObjectParser PARSER = AcknowledgedTasksResponse.generateParser( + "stop_transform_response", + StopTransformResponse::new, + ACKNOWLEDGED + ); public static StopTransformResponse fromXContent(final XContentParser parser) throws IOException { return PARSER.parse(parser, null); } - public StopTransformResponse(boolean acknowledged, @Nullable List taskFailures, - @Nullable List nodeFailures) { + public StopTransformResponse( + boolean acknowledged, + @Nullable List taskFailures, + @Nullable List nodeFailures + ) { super(acknowledged, taskFailures, nodeFailures); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/TransformNamedXContentProvider.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/TransformNamedXContentProvider.java index 8356aa57a1111..f0a5313f527dd 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/TransformNamedXContentProvider.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/TransformNamedXContentProvider.java @@ -12,9 +12,9 @@ import org.elasticsearch.client.transform.transforms.SyncConfig; import org.elasticsearch.client.transform.transforms.TimeRetentionPolicyConfig; import org.elasticsearch.client.transform.transforms.TimeSyncConfig; -import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.plugins.spi.NamedXContentProvider; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import java.util.Arrays; import java.util.List; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/DestConfig.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/DestConfig.java index cc8c7b1353cbb..4941f663d7a97 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/DestConfig.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/DestConfig.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.transform.transforms; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/NodeAttributes.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/NodeAttributes.java index de5dd799acbb9..77e418faaf6c4 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/NodeAttributes.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/NodeAttributes.java @@ -7,10 +7,10 @@ */ package org.elasticsearch.client.transform.transforms; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -31,27 +31,22 @@ public class NodeAttributes implements ToXContentObject { public static final ParseField ATTRIBUTES = new ParseField("attributes"); @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("node", true, - (a) -> { - int i = 0; - String id = (String) a[i++]; - String name = (String) a[i++]; - String ephemeralId = (String) a[i++]; - String transportAddress = (String) a[i++]; - Map attributes = (Map) a[i]; - return new NodeAttributes(id, name, ephemeralId, transportAddress, attributes); - }); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("node", true, (a) -> { + int i = 0; + String id = (String) a[i++]; + String name = (String) a[i++]; + String ephemeralId = (String) a[i++]; + String transportAddress = (String) a[i++]; + Map attributes = (Map) a[i]; + return new NodeAttributes(id, name, ephemeralId, transportAddress, attributes); + }); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), ID); PARSER.declareString(ConstructingObjectParser.constructorArg(), NAME); PARSER.declareString(ConstructingObjectParser.constructorArg(), EPHEMERAL_ID); PARSER.declareString(ConstructingObjectParser.constructorArg(), TRANSPORT_ADDRESS); - PARSER.declareField(ConstructingObjectParser.constructorArg(), - (p, c) -> p.mapStrings(), - ATTRIBUTES, - ObjectParser.ValueType.OBJECT); + PARSER.declareField(ConstructingObjectParser.constructorArg(), (p, c) -> p.mapStrings(), ATTRIBUTES, ObjectParser.ValueType.OBJECT); } private final String id; @@ -131,11 +126,11 @@ public boolean equals(Object other) { } NodeAttributes that = (NodeAttributes) other; - return Objects.equals(id, that.id) && - Objects.equals(name, that.name) && - Objects.equals(ephemeralId, that.ephemeralId) && - Objects.equals(transportAddress, that.transportAddress) && - Objects.equals(attributes, that.attributes); + return Objects.equals(id, that.id) + && Objects.equals(name, that.name) + && Objects.equals(ephemeralId, that.ephemeralId) + && Objects.equals(transportAddress, that.transportAddress) + && Objects.equals(attributes, that.attributes); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/QueryConfig.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/QueryConfig.java index e9f491ed4b831..da6bdc02fd002 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/QueryConfig.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/QueryConfig.java @@ -8,11 +8,11 @@ package org.elasticsearch.client.transform.transforms; +import org.elasticsearch.index.query.AbstractQueryBuilder; +import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.index.query.AbstractQueryBuilder; -import org.elasticsearch.index.query.QueryBuilder; import java.io.IOException; import java.util.Objects; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/SettingsConfig.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/SettingsConfig.java index 39efa7a9a7cf6..79adf4a132b87 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/SettingsConfig.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/SettingsConfig.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.transform.transforms; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/SourceConfig.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/SourceConfig.java index 1de9f1ea3286a..05b2d1d042ee8 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/SourceConfig.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/SourceConfig.java @@ -8,12 +8,12 @@ package org.elasticsearch.client.transform.transforms; -import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.index.query.QueryBuilder; +import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.index.query.QueryBuilder; -import org.elasticsearch.search.builder.SearchSourceBuilder; import java.io.IOException; import java.util.Arrays; @@ -24,7 +24,6 @@ import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg; import static org.elasticsearch.xcontent.ConstructingObjectParser.optionalConstructorArg; - /** * Class encapsulating all options for a {@link TransformConfig} gathering data */ @@ -33,17 +32,19 @@ public class SourceConfig implements ToXContentObject { public static final ParseField QUERY = new ParseField("query"); public static final ParseField INDEX = new ParseField("index"); - public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("transform_config_source", - true, - args -> { - @SuppressWarnings("unchecked") - String[] index = ((List)args[0]).toArray(new String[0]); - // default handling: if the user does not specify a query, we default to match_all - QueryConfig queryConfig = (QueryConfig) args[1]; - @SuppressWarnings("unchecked") - Map runtimeMappings = (Map) args[2]; - return new SourceConfig(index, queryConfig, runtimeMappings); - }); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "transform_config_source", + true, + args -> { + @SuppressWarnings("unchecked") + String[] index = ((List) args[0]).toArray(new String[0]); + // default handling: if the user does not specify a query, we default to match_all + QueryConfig queryConfig = (QueryConfig) args[1]; + @SuppressWarnings("unchecked") + Map runtimeMappings = (Map) args[2]; + return new SourceConfig(index, queryConfig, runtimeMappings); + } + ); static { PARSER.declareStringArray(constructorArg(), INDEX); PARSER.declareObject(optionalConstructorArg(), (p, c) -> QueryConfig.fromXContent(p), QUERY); @@ -122,7 +123,7 @@ public boolean equals(Object other) { } @Override - public int hashCode(){ + public int hashCode() { // Using Arrays.hashCode as Objects.hash does not deeply hash nested arrays. Since we are doing Array.equals, this is necessary int indexArrayHash = Arrays.hashCode(index); return Objects.hash(indexArrayHash, queryConfig, runtimeMappings); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/TimeRetentionPolicyConfig.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/TimeRetentionPolicyConfig.java index ea133131b2211..c5a04f44265e1 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/TimeRetentionPolicyConfig.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/TimeRetentionPolicyConfig.java @@ -8,10 +8,10 @@ package org.elasticsearch.client.transform.transforms; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/TimeSyncConfig.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/TimeSyncConfig.java index 425fc88fcf664..51fc3c1178fb0 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/TimeSyncConfig.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/TimeSyncConfig.java @@ -8,10 +8,10 @@ package org.elasticsearch.client.transform.transforms; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -31,13 +31,20 @@ public class TimeSyncConfig implements SyncConfig { private final String field; private final TimeValue delay; - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("time_sync_config", true, - args -> new TimeSyncConfig((String) args[0], args[1] != null ? (TimeValue) args[1] : TimeValue.ZERO)); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "time_sync_config", + true, + args -> new TimeSyncConfig((String) args[0], args[1] != null ? (TimeValue) args[1] : TimeValue.ZERO) + ); static { PARSER.declareString(constructorArg(), FIELD); - PARSER.declareField(optionalConstructorArg(), (p, c) -> TimeValue.parseTimeValue(p.textOrNull(), DELAY.getPreferredName()), DELAY, - ObjectParser.ValueType.STRING_OR_NULL); + PARSER.declareField( + optionalConstructorArg(), + (p, c) -> TimeValue.parseTimeValue(p.textOrNull(), DELAY.getPreferredName()), + DELAY, + ObjectParser.ValueType.STRING_OR_NULL + ); } public static TimeSyncConfig fromXContent(XContentParser parser) { @@ -82,8 +89,7 @@ public boolean equals(Object other) { final TimeSyncConfig that = (TimeSyncConfig) other; - return Objects.equals(this.field, that.field) - && Objects.equals(this.delay, that.delay); + return Objects.equals(this.field, that.field) && Objects.equals(this.delay, that.delay); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/TransformCheckpointStats.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/TransformCheckpointStats.java index cea61dd2124fc..1494e8fb10085 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/TransformCheckpointStats.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/TransformCheckpointStats.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.transform.transforms; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -34,15 +34,18 @@ public class TransformCheckpointStats { private final long timeUpperBoundMillis; public static final ConstructingObjectParser LENIENT_PARSER = new ConstructingObjectParser<>( - "transform_checkpoint_stats", true, args -> { - long checkpoint = args[0] == null ? 0L : (Long) args[0]; - TransformIndexerPosition position = (TransformIndexerPosition) args[1]; - TransformProgress checkpointProgress = (TransformProgress) args[2]; - long timestamp = args[3] == null ? 0L : (Long) args[3]; - long timeUpperBound = args[4] == null ? 0L : (Long) args[4]; - - return new TransformCheckpointStats(checkpoint, position, checkpointProgress, timestamp, timeUpperBound); - }); + "transform_checkpoint_stats", + true, + args -> { + long checkpoint = args[0] == null ? 0L : (Long) args[0]; + TransformIndexerPosition position = (TransformIndexerPosition) args[1]; + TransformProgress checkpointProgress = (TransformProgress) args[2]; + long timestamp = args[3] == null ? 0L : (Long) args[3]; + long timeUpperBound = args[4] == null ? 0L : (Long) args[4]; + + return new TransformCheckpointStats(checkpoint, position, checkpointProgress, timestamp, timeUpperBound); + } + ); static { LENIENT_PARSER.declareLong(optionalConstructorArg(), CHECKPOINT); @@ -56,9 +59,13 @@ public static TransformCheckpointStats fromXContent(XContentParser parser) throw return LENIENT_PARSER.parse(parser, null); } - public TransformCheckpointStats(final long checkpoint, final TransformIndexerPosition position, - final TransformProgress checkpointProgress, final long timestampMillis, - final long timeUpperBoundMillis) { + public TransformCheckpointStats( + final long checkpoint, + final TransformIndexerPosition position, + final TransformProgress checkpointProgress, + final long timestampMillis, + final long timeUpperBoundMillis + ) { this.checkpoint = checkpoint; this.position = position; this.checkpointProgress = checkpointProgress; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/TransformCheckpointingInfo.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/TransformCheckpointingInfo.java index 7666294306136..bf55e9a373d0c 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/TransformCheckpointingInfo.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/TransformCheckpointingInfo.java @@ -10,9 +10,9 @@ import org.elasticsearch.client.common.TimeUtil; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.time.Instant; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/TransformConfig.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/TransformConfig.java index 8c59c2bac3d4e..9edd8cc747cd1 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/TransformConfig.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/TransformConfig.java @@ -12,12 +12,12 @@ import org.elasticsearch.client.common.TimeUtil; import org.elasticsearch.client.transform.transforms.latest.LatestConfig; import org.elasticsearch.client.transform.transforms.pivot.PivotConfig; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/TransformConfigUpdate.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/TransformConfigUpdate.java index a706e35ae93b2..24ee4ee21172a 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/TransformConfigUpdate.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/TransformConfigUpdate.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.transform.transforms; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ToXContentObject; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/TransformIndexerPosition.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/TransformIndexerPosition.java index 73f201130b010..c1f940b281815 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/TransformIndexerPosition.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/TransformIndexerPosition.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.transform.transforms; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -35,9 +35,10 @@ public class TransformIndexerPosition { @SuppressWarnings("unchecked") public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "transform_indexer_position", - true, - args -> new TransformIndexerPosition((Map) args[0],(Map) args[1])); + "transform_indexer_position", + true, + args -> new TransformIndexerPosition((Map) args[0], (Map) args[1]) + ); static { PARSER.declareField(optionalConstructorArg(), XContentParser::mapOrdered, INDEXER_POSITION, ValueType.OBJECT); @@ -69,8 +70,7 @@ public boolean equals(Object other) { TransformIndexerPosition that = (TransformIndexerPosition) other; - return Objects.equals(this.indexerPosition, that.indexerPosition) && - Objects.equals(this.bucketPosition, that.bucketPosition); + return Objects.equals(this.indexerPosition, that.indexerPosition) && Objects.equals(this.bucketPosition, that.bucketPosition); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/TransformIndexerStats.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/TransformIndexerStats.java index 6d585282984cd..027f8eb89affd 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/TransformIndexerStats.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/TransformIndexerStats.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.transform.transforms; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/TransformProgress.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/TransformProgress.java index 565779eae546c..c50c7f41296e3 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/TransformProgress.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/TransformProgress.java @@ -9,8 +9,8 @@ package org.elasticsearch.client.transform.transforms; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.util.Objects; @@ -28,7 +28,8 @@ public class TransformProgress { public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( "transform_progress", true, - a -> new TransformProgress((Long) a[0], (Long)a[1], (Double)a[2], (Long)a[3], (Long)a[4])); + a -> new TransformProgress((Long) a[0], (Long) a[1], (Double) a[2], (Long) a[3], (Long) a[4]) + ); static { PARSER.declareLong(optionalConstructorArg(), TOTAL_DOCS); @@ -48,11 +49,7 @@ public static TransformProgress fromXContent(XContentParser parser) { private final long documentsProcessed; private final long documentsIndexed; - public TransformProgress(Long totalDocs, - Long remainingDocs, - Double percentComplete, - Long documentsProcessed, - Long documentsIndexed) { + public TransformProgress(Long totalDocs, Long remainingDocs, Double percentComplete, Long documentsProcessed, Long documentsIndexed) { this.totalDocs = totalDocs; this.remainingDocs = remainingDocs == null ? totalDocs : remainingDocs; this.percentComplete = percentComplete; @@ -102,7 +99,7 @@ public boolean equals(Object other) { } @Override - public int hashCode(){ + public int hashCode() { return Objects.hash(remainingDocs, totalDocs, percentComplete, documentsIndexed, documentsProcessed); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/TransformStats.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/TransformStats.java index 2d7b2695e98d4..6426283eaee7e 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/TransformStats.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/TransformStats.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.transform.transforms; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -30,19 +30,25 @@ public class TransformStats { public static final ParseField CHECKPOINTING_INFO_FIELD = new ParseField("checkpointing"); public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "data_frame_transform_state_and_stats_info", true, - a -> new TransformStats((String) a[0], (State) a[1], (String) a[2], - (NodeAttributes) a[3], (TransformIndexerStats) a[4], (TransformCheckpointingInfo) a[5])); + "data_frame_transform_state_and_stats_info", + true, + a -> new TransformStats( + (String) a[0], + (State) a[1], + (String) a[2], + (NodeAttributes) a[3], + (TransformIndexerStats) a[4], + (TransformCheckpointingInfo) a[5] + ) + ); static { PARSER.declareString(constructorArg(), ID); - PARSER.declareField(optionalConstructorArg(), p -> State.fromString(p.text()), STATE_FIELD, - ObjectParser.ValueType.STRING); + PARSER.declareField(optionalConstructorArg(), p -> State.fromString(p.text()), STATE_FIELD, ObjectParser.ValueType.STRING); PARSER.declareString(optionalConstructorArg(), REASON_FIELD); PARSER.declareField(optionalConstructorArg(), NodeAttributes.PARSER::apply, NODE_FIELD, ObjectParser.ValueType.OBJECT); PARSER.declareObject(constructorArg(), (p, c) -> TransformIndexerStats.fromXContent(p), STATS_FIELD); - PARSER.declareObject(optionalConstructorArg(), - (p, c) -> TransformCheckpointingInfo.fromXContent(p), CHECKPOINTING_INFO_FIELD); + PARSER.declareObject(optionalConstructorArg(), (p, c) -> TransformCheckpointingInfo.fromXContent(p), CHECKPOINTING_INFO_FIELD); } public static TransformStats fromXContent(XContentParser parser) throws IOException { @@ -56,8 +62,14 @@ public static TransformStats fromXContent(XContentParser parser) throws IOExcept private final TransformIndexerStats indexerStats; private final TransformCheckpointingInfo checkpointingInfo; - public TransformStats(String id, State state, String reason, NodeAttributes node, TransformIndexerStats stats, - TransformCheckpointingInfo checkpointingInfo) { + public TransformStats( + String id, + State state, + String reason, + NodeAttributes node, + TransformIndexerStats stats, + TransformCheckpointingInfo checkpointingInfo + ) { this.id = id; this.state = state; this.reason = reason; @@ -117,7 +129,13 @@ public boolean equals(Object other) { public enum State { - STARTED, INDEXING, ABORTING, STOPPING, STOPPED, FAILED, WAITING; + STARTED, + INDEXING, + ABORTING, + STOPPING, + STOPPED, + FAILED, + WAITING; public static State fromString(String name) { return valueOf(name.trim().toUpperCase(Locale.ROOT)); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/latest/LatestConfig.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/latest/LatestConfig.java index 0f4f1b852fd03..5c1c868d62143 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/latest/LatestConfig.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/latest/LatestConfig.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.transform.transforms.latest; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -35,8 +35,11 @@ public class LatestConfig implements ToXContentObject { private final String sort; @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(NAME, true, args -> new LatestConfig((List) args[0], (String) args[1])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + true, + args -> new LatestConfig((List) args[0], (String) args[1]) + ); static { PARSER.declareStringArray(constructorArg(), UNIQUE_KEY); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/pivot/AggregationConfig.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/pivot/AggregationConfig.java index b5e4e48daac79..79155bb40abff 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/pivot/AggregationConfig.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/pivot/AggregationConfig.java @@ -8,11 +8,11 @@ package org.elasticsearch.client.transform.transforms.pivot; +import org.elasticsearch.search.aggregations.AggregationBuilder; +import org.elasticsearch.search.aggregations.AggregatorFactories; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.search.aggregations.AggregationBuilder; -import org.elasticsearch.search.aggregations.AggregatorFactories; import java.io.IOException; import java.util.Collection; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/pivot/DateHistogramGroupSource.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/pivot/DateHistogramGroupSource.java index aedcae46fdce2..5cadd22b5bcd7 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/pivot/DateHistogramGroupSource.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/pivot/DateHistogramGroupSource.java @@ -8,16 +8,16 @@ package org.elasticsearch.client.transform.transforms.pivot; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; +import org.elasticsearch.script.Script; +import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentFragment; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.script.Script; -import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; import java.io.IOException; import java.time.ZoneId; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/pivot/GeoTileGroupSource.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/pivot/GeoTileGroupSource.java index 8b7645f161b16..0fdb706a394d6 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/pivot/GeoTileGroupSource.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/pivot/GeoTileGroupSource.java @@ -8,14 +8,14 @@ package org.elasticsearch.client.transform.transforms.pivot; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.geo.GeoBoundingBox; +import org.elasticsearch.search.aggregations.bucket.geogrid.GeoTileUtils; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.search.aggregations.bucket.geogrid.GeoTileUtils; import java.io.IOException; import java.util.Objects; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/pivot/GroupConfig.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/pivot/GroupConfig.java index 27a0518b2eae7..13b816c98e244 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/pivot/GroupConfig.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/pivot/GroupConfig.java @@ -126,7 +126,7 @@ private static void consumeUntilEndObject(XContentParser parser, int endObjectCo this.groups = groups; } - public Map getGroups() { + public Map getGroups() { return groups; } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/pivot/HistogramGroupSource.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/pivot/HistogramGroupSource.java index bfc5543a9919e..5ab4000064787 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/pivot/HistogramGroupSource.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/pivot/HistogramGroupSource.java @@ -8,13 +8,13 @@ package org.elasticsearch.client.transform.transforms.pivot; -import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.script.Script; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.script.Script; import java.io.IOException; import java.util.Objects; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/pivot/PivotConfig.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/pivot/PivotConfig.java index c4b838afa4da6..378a0f0346fdf 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/pivot/PivotConfig.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/pivot/PivotConfig.java @@ -8,12 +8,12 @@ package org.elasticsearch.client.transform.transforms.pivot; -import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.search.aggregations.AggregatorFactories; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.search.aggregations.AggregatorFactories; import java.io.IOException; import java.util.Objects; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/pivot/SingleGroupSource.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/pivot/SingleGroupSource.java index 720460f17e6a8..e73eb6d77c11e 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/pivot/SingleGroupSource.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/pivot/SingleGroupSource.java @@ -8,10 +8,10 @@ package org.elasticsearch.client.transform.transforms.pivot; +import org.elasticsearch.script.Script; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.script.Script; import java.io.IOException; import java.util.Locale; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/pivot/TermsGroupSource.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/pivot/TermsGroupSource.java index 7d6d1479589a2..9b5de5fb41022 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/pivot/TermsGroupSource.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/transform/transforms/pivot/TermsGroupSource.java @@ -8,12 +8,12 @@ package org.elasticsearch.client.transform.transforms.pivot; +import org.elasticsearch.script.Script; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.script.Script; import java.io.IOException; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/AckWatchRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/AckWatchRequest.java index 6a8cd5b13913e..2b8b8b36e5c59 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/AckWatchRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/AckWatchRequest.java @@ -40,8 +40,7 @@ private void validateIds(String watchId, String... actionIds) { if (actionId == null) { exception.addValidationError(String.format(Locale.ROOT, "action id may not be null")); } else if (PutWatchRequest.isValidId(actionId) == false) { - exception.addValidationError( - String.format(Locale.ROOT, "action id [%s] contains whitespace", actionId)); + exception.addValidationError(String.format(Locale.ROOT, "action id [%s] contains whitespace", actionId)); } } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/AckWatchResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/AckWatchResponse.java index 0fc9d183f0101..e53526b803f50 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/AckWatchResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/AckWatchResponse.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.watcher; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -34,14 +34,14 @@ public WatchStatus getStatus() { } private static final ParseField STATUS_FIELD = new ParseField("status"); - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("ack_watch_response", true, - a -> new AckWatchResponse((WatchStatus) a[0])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "ack_watch_response", + true, + a -> new AckWatchResponse((WatchStatus) a[0]) + ); static { - PARSER.declareObject(ConstructingObjectParser.constructorArg(), - (parser, context) -> WatchStatus.parse(parser), - STATUS_FIELD); + PARSER.declareObject(ConstructingObjectParser.constructorArg(), (parser, context) -> WatchStatus.parse(parser), STATUS_FIELD); } public static AckWatchResponse fromXContent(XContentParser parser) throws IOException { diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/ActionStatus.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/ActionStatus.java index 860b783da0e1e..57385a4a4fc93 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/ActionStatus.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/ActionStatus.java @@ -22,14 +22,19 @@ public class ActionStatus { private final AckStatus ackStatus; - @Nullable private final Execution lastExecution; - @Nullable private final Execution lastSuccessfulExecution; - @Nullable private final Throttle lastThrottle; - - public ActionStatus(AckStatus ackStatus, - @Nullable Execution lastExecution, - @Nullable Execution lastSuccessfulExecution, - @Nullable Throttle lastThrottle) { + @Nullable + private final Execution lastExecution; + @Nullable + private final Execution lastSuccessfulExecution; + @Nullable + private final Throttle lastThrottle; + + public ActionStatus( + AckStatus ackStatus, + @Nullable Execution lastExecution, + @Nullable Execution lastSuccessfulExecution, + @Nullable Throttle lastThrottle + ) { this.ackStatus = ackStatus; this.lastExecution = lastExecution; this.lastSuccessfulExecution = lastSuccessfulExecution; @@ -59,10 +64,10 @@ public boolean equals(Object o) { ActionStatus that = (ActionStatus) o; - return Objects.equals(ackStatus, that.ackStatus) && - Objects.equals(lastExecution, that.lastExecution) && - Objects.equals(lastSuccessfulExecution, that.lastSuccessfulExecution) && - Objects.equals(lastThrottle, that.lastThrottle); + return Objects.equals(ackStatus, that.ackStatus) + && Objects.equals(lastExecution, that.lastExecution) + && Objects.equals(lastSuccessfulExecution, that.lastSuccessfulExecution) + && Objects.equals(lastThrottle, that.lastThrottle); } @Override @@ -94,8 +99,11 @@ public static ActionStatus parse(String actionId, XContentParser parser) throws } } if (ackStatus == null) { - throw new ElasticsearchParseException("could not parse action status for [{}]. missing required field [{}]", - actionId, Field.ACK_STATUS.getPreferredName()); + throw new ElasticsearchParseException( + "could not parse action status for [{}]. missing required field [{}]", + actionId, + Field.ACK_STATUS.getPreferredName() + ); } return new ActionStatus(ackStatus, lastExecution, lastSuccessfulExecution, lastThrottle); } @@ -132,7 +140,7 @@ public boolean equals(Object o) { AckStatus ackStatus = (AckStatus) o; - return Objects.equals(timestamp, ackStatus.timestamp) && Objects.equals(state, ackStatus.state); + return Objects.equals(timestamp, ackStatus.timestamp) && Objects.equals(state, ackStatus.state); } @Override @@ -158,12 +166,20 @@ public static AckStatus parse(String actionId, XContentParser parser) throws IOE } } if (timestamp == null) { - throw new ElasticsearchParseException("could not parse action status for [{}]. missing required field [{}.{}]", - actionId, Field.ACK_STATUS.getPreferredName(), Field.TIMESTAMP.getPreferredName()); + throw new ElasticsearchParseException( + "could not parse action status for [{}]. missing required field [{}.{}]", + actionId, + Field.ACK_STATUS.getPreferredName(), + Field.TIMESTAMP.getPreferredName() + ); } if (state == null) { - throw new ElasticsearchParseException("could not parse action status for [{}]. missing required field [{}.{}]", - actionId, Field.ACK_STATUS.getPreferredName(), Field.ACK_STATUS_STATE.getPreferredName()); + throw new ElasticsearchParseException( + "could not parse action status for [{}]. missing required field [{}.{}]", + actionId, + Field.ACK_STATUS.getPreferredName(), + Field.ACK_STATUS_STATE.getPreferredName() + ); } return new AckStatus(timestamp, state); } @@ -208,9 +224,9 @@ public boolean equals(Object o) { Execution execution = (Execution) o; - return Objects.equals(successful, execution.successful) && - Objects.equals(timestamp, execution.timestamp) && - Objects.equals(reason, execution.reason); + return Objects.equals(successful, execution.successful) + && Objects.equals(timestamp, execution.timestamp) + && Objects.equals(reason, execution.reason); } @Override @@ -239,19 +255,31 @@ public static Execution parse(String actionId, XContentParser parser) throws IOE } } if (timestamp == null) { - throw new ElasticsearchParseException("could not parse action status for [{}]. missing required field [{}.{}]", - actionId, Field.LAST_EXECUTION.getPreferredName(), Field.TIMESTAMP.getPreferredName()); + throw new ElasticsearchParseException( + "could not parse action status for [{}]. missing required field [{}.{}]", + actionId, + Field.LAST_EXECUTION.getPreferredName(), + Field.TIMESTAMP.getPreferredName() + ); } if (successful == null) { - throw new ElasticsearchParseException("could not parse action status for [{}]. missing required field [{}.{}]", - actionId, Field.LAST_EXECUTION.getPreferredName(), Field.EXECUTION_SUCCESSFUL.getPreferredName()); + throw new ElasticsearchParseException( + "could not parse action status for [{}]. missing required field [{}.{}]", + actionId, + Field.LAST_EXECUTION.getPreferredName(), + Field.EXECUTION_SUCCESSFUL.getPreferredName() + ); } if (successful) { return successful(timestamp); } if (reason == null) { - throw new ElasticsearchParseException("could not parse action status for [{}]. missing required field for unsuccessful" + - " execution [{}.{}]", actionId, Field.LAST_EXECUTION.getPreferredName(), Field.REASON.getPreferredName()); + throw new ElasticsearchParseException( + "could not parse action status for [{}]. missing required field for unsuccessful" + " execution [{}.{}]", + actionId, + Field.LAST_EXECUTION.getPreferredName(), + Field.REASON.getPreferredName() + ); } return failure(timestamp, reason); } @@ -307,12 +335,20 @@ public static Throttle parse(String actionId, XContentParser parser) throws IOEx } } if (timestamp == null) { - throw new ElasticsearchParseException("could not parse action status for [{}]. missing required field [{}.{}]", - actionId, Field.LAST_THROTTLE.getPreferredName(), Field.TIMESTAMP.getPreferredName()); + throw new ElasticsearchParseException( + "could not parse action status for [{}]. missing required field [{}.{}]", + actionId, + Field.LAST_THROTTLE.getPreferredName(), + Field.TIMESTAMP.getPreferredName() + ); } if (reason == null) { - throw new ElasticsearchParseException("could not parse action status for [{}]. missing required field [{}.{}]", - actionId, Field.LAST_THROTTLE.getPreferredName(), Field.REASON.getPreferredName()); + throw new ElasticsearchParseException( + "could not parse action status for [{}]. missing required field [{}.{}]", + actionId, + Field.LAST_THROTTLE.getPreferredName(), + Field.REASON.getPreferredName() + ); } return new Throttle(timestamp, reason); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/ActivateWatchRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/ActivateWatchRequest.java index 355888b009b77..e402f484bdff7 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/ActivateWatchRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/ActivateWatchRequest.java @@ -20,7 +20,7 @@ public final class ActivateWatchRequest implements Validatable { private final String watchId; public ActivateWatchRequest(String watchId) { - this.watchId = Objects.requireNonNull(watchId, "Watch identifier is required"); + this.watchId = Objects.requireNonNull(watchId, "Watch identifier is required"); if (PutWatchRequest.isValidId(this.watchId) == false) { throw new IllegalArgumentException("Watch identifier contains whitespace"); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/ActivateWatchResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/ActivateWatchResponse.java index e4327c1ffe285..02524091a471f 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/ActivateWatchResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/ActivateWatchResponse.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.watcher; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -21,14 +21,14 @@ public final class ActivateWatchResponse { private static final ParseField STATUS_FIELD = new ParseField("status"); - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("activate_watch_response", true, - a -> new ActivateWatchResponse((WatchStatus) a[0])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "activate_watch_response", + true, + a -> new ActivateWatchResponse((WatchStatus) a[0]) + ); static { - PARSER.declareObject(ConstructingObjectParser.constructorArg(), - (parser, context) -> WatchStatus.parse(parser), - STATUS_FIELD); + PARSER.declareObject(ConstructingObjectParser.constructorArg(), (parser, context) -> WatchStatus.parse(parser), STATUS_FIELD); } private final WatchStatus status; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchRequest.java index 4ba7791c8b7fd..deb3f97c413d6 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchRequest.java @@ -8,7 +8,6 @@ package org.elasticsearch.client.watcher; import org.elasticsearch.client.Validatable; -import org.elasticsearch.client.watcher.PutWatchRequest; import java.util.Objects; @@ -28,4 +27,3 @@ public String getWatchId() { return watchId; } } - diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchResponse.java index c64830206510a..a5e031907b6cb 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchResponse.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.watcher; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -18,13 +18,13 @@ public class DeactivateWatchResponse { private WatchStatus status; private static final ParseField STATUS_FIELD = new ParseField("status"); - private static final ConstructingObjectParser PARSER - = new ConstructingObjectParser<>("x_pack_deactivate_watch_response", true, - (fields) -> new DeactivateWatchResponse((WatchStatus) fields[0])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "x_pack_deactivate_watch_response", + true, + (fields) -> new DeactivateWatchResponse((WatchStatus) fields[0]) + ); static { - PARSER.declareObject(ConstructingObjectParser.constructorArg(), - (parser, context) -> WatchStatus.parse(parser), - STATUS_FIELD); + PARSER.declareObject(ConstructingObjectParser.constructorArg(), (parser, context) -> WatchStatus.parse(parser), STATUS_FIELD); } public static DeactivateWatchResponse fromXContent(XContentParser parser) throws IOException { diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeleteWatchResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeleteWatchResponse.java index 8a6755e009083..756a5ff20b4b9 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeleteWatchResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeleteWatchResponse.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.watcher; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -16,8 +16,11 @@ public class DeleteWatchResponse { - private static final ObjectParser PARSER - = new ObjectParser<>("x_pack_delete_watch_response", true, DeleteWatchResponse::new); + private static final ObjectParser PARSER = new ObjectParser<>( + "x_pack_delete_watch_response", + true, + DeleteWatchResponse::new + ); static { PARSER.declareString(DeleteWatchResponse::setId, new ParseField("_id")); PARSER.declareLong(DeleteWatchResponse::setVersion, new ParseField("_version")); @@ -28,8 +31,7 @@ public class DeleteWatchResponse { private long version; private boolean found; - public DeleteWatchResponse() { - } + public DeleteWatchResponse() {} public DeleteWatchResponse(String id, long version, boolean found) { this.id = id; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/ExecuteWatchRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/ExecuteWatchRequest.java index fd6f1ba791434..64eea83fe3e6e 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/ExecuteWatchRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/ExecuteWatchRequest.java @@ -9,9 +9,9 @@ package org.elasticsearch.client.watcher; import org.elasticsearch.client.Validatable; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentType; @@ -27,7 +27,11 @@ public class ExecuteWatchRequest implements Validatable, ToXContentObject { public enum ActionExecutionMode { - SIMULATE, FORCE_SIMULATE, EXECUTE, FORCE_EXECUTE, SKIP + SIMULATE, + FORCE_SIMULATE, + EXECUTE, + FORCE_EXECUTE, + SKIP } private final String id; @@ -161,4 +165,3 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws return builder; } } - diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/ExecuteWatchResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/ExecuteWatchResponse.java index d6ced03f96e8c..cb8c77bdd26fc 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/ExecuteWatchResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/ExecuteWatchResponse.java @@ -9,10 +9,10 @@ package org.elasticsearch.client.watcher; import org.elasticsearch.ElasticsearchException; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; @@ -33,8 +33,7 @@ public class ExecuteWatchResponse { private Map data; - public ExecuteWatchResponse() { - } + public ExecuteWatchResponse() {} public ExecuteWatchResponse(String recordId, BytesReference contentSource) { this.recordId = recordId; @@ -64,8 +63,10 @@ public BytesReference getRecord() { public Map getRecordAsMap() { if (data == null) { // EMPTY is safe here because we never use namedObject - try (InputStream stream = contentSource.streamInput(); - XContentParser parser = XContentType.JSON.xContent().createParser(NamedXContentRegistry.EMPTY, null, stream)) { + try ( + InputStream stream = contentSource.streamInput(); + XContentParser parser = XContentType.JSON.xContent().createParser(NamedXContentRegistry.EMPTY, null, stream) + ) { data = (Map) XContentUtils.readValue(parser, parser.nextToken()); } catch (IOException ex) { throw new ElasticsearchException("failed to read value", ex); @@ -74,9 +75,11 @@ public Map getRecordAsMap() { return data; } - private static final ConstructingObjectParser PARSER - = new ConstructingObjectParser<>("x_pack_execute_watch_response", true, - (fields) -> new ExecuteWatchResponse((String)fields[0], (BytesReference) fields[1])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "x_pack_execute_watch_response", + true, + (fields) -> new ExecuteWatchResponse((String) fields[0], (BytesReference) fields[1]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), ID_FIELD); PARSER.declareObject(ConstructingObjectParser.constructorArg(), (p, c) -> readBytesReference(p), WATCH_FIELD); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/GetWatchResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/GetWatchResponse.java index 2a37548a9a3fc..389280ae6a811 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/GetWatchResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/GetWatchResponse.java @@ -7,12 +7,12 @@ */ package org.elasticsearch.client.watcher; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.lucene.uid.Versions; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; @@ -40,8 +40,15 @@ public GetWatchResponse(String id) { this(id, Versions.NOT_FOUND, UNASSIGNED_SEQ_NO, UNASSIGNED_PRIMARY_TERM, null, null, null); } - public GetWatchResponse(String id, long version, long seqNo, long primaryTerm, WatchStatus status, - BytesReference source, XContentType xContentType) { + public GetWatchResponse( + String id, + long version, + long seqNo, + long primaryTerm, + WatchStatus status, + BytesReference source, + XContentType xContentType + ) { this.id = id; this.version = version; this.status = status; @@ -101,11 +108,11 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; GetWatchResponse that = (GetWatchResponse) o; - return version == that.version && - Objects.equals(id, that.id) && - Objects.equals(status, that.status) && - Objects.equals(xContentType, that.xContentType) && - Objects.equals(source, that.source); + return version == that.version + && Objects.equals(id, that.id) + && Objects.equals(status, that.status) + && Objects.equals(xContentType, that.xContentType) + && Objects.equals(source, that.source); } @Override @@ -121,19 +128,28 @@ public int hashCode() { private static final ParseField STATUS_FIELD = new ParseField("status"); private static final ParseField WATCH_FIELD = new ParseField("watch"); - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("get_watch_response", true, - a -> { - boolean isFound = (boolean) a[1]; - if (isFound) { - XContentBuilder builder = (XContentBuilder) a[6]; - BytesReference source = BytesReference.bytes(builder); - return new GetWatchResponse((String) a[0], (long) a[2], (long) a[3], (long) a[4], (WatchStatus) a[5], - source, builder.contentType()); - } else { - return new GetWatchResponse((String) a[0]); - } - }); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "get_watch_response", + true, + a -> { + boolean isFound = (boolean) a[1]; + if (isFound) { + XContentBuilder builder = (XContentBuilder) a[6]; + BytesReference source = BytesReference.bytes(builder); + return new GetWatchResponse( + (String) a[0], + (long) a[2], + (long) a[3], + (long) a[4], + (WatchStatus) a[5], + source, + builder.contentType() + ); + } else { + return new GetWatchResponse((String) a[0]); + } + } + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), ID_FIELD); @@ -141,15 +157,17 @@ public int hashCode() { PARSER.declareLong(ConstructingObjectParser.optionalConstructorArg(), VERSION_FIELD); PARSER.declareLong(ConstructingObjectParser.optionalConstructorArg(), SEQ_NO_FIELD); PARSER.declareLong(ConstructingObjectParser.optionalConstructorArg(), PRIMARY_TERM_FIELD); - PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), - (parser, context) -> WatchStatus.parse(parser), STATUS_FIELD); - PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), - (parser, context) -> { - try (XContentBuilder builder = XContentBuilder.builder(parser.contentType().xContent())) { - builder.copyCurrentStructure(parser); - return builder; - } - }, WATCH_FIELD); + PARSER.declareObject( + ConstructingObjectParser.optionalConstructorArg(), + (parser, context) -> WatchStatus.parse(parser), + STATUS_FIELD + ); + PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), (parser, context) -> { + try (XContentBuilder builder = XContentBuilder.builder(parser.contentType().xContent())) { + builder.copyCurrentStructure(parser); + return builder; + } + }, WATCH_FIELD); } public static GetWatchResponse fromXContent(XContentParser parser) throws IOException { diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/PutWatchRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/PutWatchRequest.java index 11a79eb7dd83c..bd53a0c20eb9e 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/PutWatchRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/PutWatchRequest.java @@ -10,8 +10,8 @@ import org.elasticsearch.client.Validatable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.seqno.SequenceNumbers; +import org.elasticsearch.xcontent.XContentType; import java.util.Objects; import java.util.regex.Pattern; @@ -90,7 +90,7 @@ public XContentType xContentType() { */ public PutWatchRequest setIfSeqNo(long seqNo) { if (seqNo < 0 && seqNo != UNASSIGNED_SEQ_NO) { - throw new IllegalArgumentException("sequence numbers must be non negative. got [" + seqNo + "]."); + throw new IllegalArgumentException("sequence numbers must be non negative. got [" + seqNo + "]."); } ifSeqNo = seqNo; return this; @@ -130,7 +130,6 @@ public long ifPrimaryTerm() { return ifPrimaryTerm; } - public static boolean isValidId(String id) { return Strings.isEmpty(id) == false && NO_WS_PATTERN.matcher(id).matches(); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/PutWatchResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/PutWatchResponse.java index 55fa4ea6734e4..7d7c23ec2cac2 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/PutWatchResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/PutWatchResponse.java @@ -7,18 +7,21 @@ */ package org.elasticsearch.client.watcher; -import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.index.seqno.SequenceNumbers; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.index.seqno.SequenceNumbers; import java.io.IOException; import java.util.Objects; public class PutWatchResponse { - private static final ObjectParser PARSER - = new ObjectParser<>("x_pack_put_watch_response", true, PutWatchResponse::new); + private static final ObjectParser PARSER = new ObjectParser<>( + "x_pack_put_watch_response", + true, + PutWatchResponse::new + ); static { PARSER.declareString(PutWatchResponse::setId, new ParseField("_id")); @@ -34,8 +37,7 @@ public class PutWatchResponse { private long primaryTerm = SequenceNumbers.UNASSIGNED_PRIMARY_TERM; private boolean created; - public PutWatchResponse() { - } + public PutWatchResponse() {} public PutWatchResponse(String id, long version, long seqNo, long primaryTerm, boolean created) { this.id = id; @@ -92,9 +94,11 @@ public boolean equals(Object o) { PutWatchResponse that = (PutWatchResponse) o; - return Objects.equals(id, that.id) && Objects.equals(version, that.version) + return Objects.equals(id, that.id) + && Objects.equals(version, that.version) && Objects.equals(seqNo, that.seqNo) - && Objects.equals(primaryTerm, that.primaryTerm) && Objects.equals(created, that.created); + && Objects.equals(primaryTerm, that.primaryTerm) + && Objects.equals(created, that.created); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/QueuedWatch.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/QueuedWatch.java index 33ae744b470be..6af59862b5278 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/QueuedWatch.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/QueuedWatch.java @@ -17,13 +17,16 @@ public class QueuedWatch { @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("watcher_stats_node", true, (args, c) -> new QueuedWatch( + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "watcher_stats_node", + true, + (args, c) -> new QueuedWatch( (String) args[0], (String) args[1], ZonedDateTime.parse((String) args[2]), ZonedDateTime.parse((String) args[3]) - )); + ) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), new ParseField("watch_id")); @@ -32,7 +35,6 @@ public class QueuedWatch { PARSER.declareString(ConstructingObjectParser.constructorArg(), new ParseField("execution_time")); } - private final String watchId; private final String watchRecordId; private final ZonedDateTime triggeredTime; @@ -66,10 +68,10 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; QueuedWatch that = (QueuedWatch) o; - return Objects.equals(watchId, that.watchId) && - Objects.equals(watchRecordId, that.watchRecordId) && - Objects.equals(triggeredTime, that.triggeredTime) && - Objects.equals(executionTime, that.executionTime); + return Objects.equals(watchId, that.watchId) + && Objects.equals(watchRecordId, that.watchRecordId) + && Objects.equals(triggeredTime, that.triggeredTime) + && Objects.equals(executionTime, that.executionTime); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/WatchExecutionSnapshot.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/WatchExecutionSnapshot.java index 57e0034da57c7..0c549444ec35b 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/WatchExecutionSnapshot.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/WatchExecutionSnapshot.java @@ -19,16 +19,19 @@ public class WatchExecutionSnapshot { @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("watcher_stats_node", true, (args, c) -> new WatchExecutionSnapshot( + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "watcher_stats_node", + true, + (args, c) -> new WatchExecutionSnapshot( (String) args[0], (String) args[1], - ZonedDateTime.parse((String) args[2]), - ZonedDateTime.parse((String) args[3]), + ZonedDateTime.parse((String) args[2]), + ZonedDateTime.parse((String) args[3]), ExecutionPhase.valueOf(((String) args[4]).toUpperCase(Locale.ROOT)), args[5] == null ? null : ((List) args[5]).toArray(new String[0]), args[6] == null ? null : ((List) args[6]).toArray(new String[0]) - )); + ) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), new ParseField("watch_id")); @@ -48,8 +51,15 @@ public class WatchExecutionSnapshot { private final String[] executedActions; private final String[] executionStackTrace; - public WatchExecutionSnapshot(String watchId, String watchRecordId, ZonedDateTime triggeredTime, ZonedDateTime executionTime, - ExecutionPhase phase, String[] executedActions, String[] executionStackTrace) { + public WatchExecutionSnapshot( + String watchId, + String watchRecordId, + ZonedDateTime triggeredTime, + ZonedDateTime executionTime, + ExecutionPhase phase, + String[] executedActions, + String[] executionStackTrace + ) { this.watchId = watchId; this.watchRecordId = watchRecordId; this.triggeredTime = triggeredTime; @@ -92,13 +102,13 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; WatchExecutionSnapshot that = (WatchExecutionSnapshot) o; - return Objects.equals(watchId, that.watchId) && - Objects.equals(watchRecordId, that.watchRecordId) && - Objects.equals(triggeredTime, that.triggeredTime) && - Objects.equals(executionTime, that.executionTime) && - phase == that.phase && - Arrays.equals(executedActions, that.executedActions) && - Arrays.equals(executionStackTrace, that.executionStackTrace); + return Objects.equals(watchId, that.watchId) + && Objects.equals(watchRecordId, that.watchRecordId) + && Objects.equals(triggeredTime, that.triggeredTime) + && Objects.equals(executionTime, that.executionTime) + && phase == that.phase + && Arrays.equals(executedActions, that.executedActions) + && Arrays.equals(executionStackTrace, that.executionStackTrace); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/WatchStatus.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/WatchStatus.java index 396dcd622edfd..e459453c246a7 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/WatchStatus.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/WatchStatus.java @@ -35,15 +35,18 @@ public class WatchStatus { private final ZonedDateTime lastMetCondition; private final long version; private final Map actions; - @Nullable private Map headers; - - public WatchStatus(long version, - State state, - ExecutionState executionState, - ZonedDateTime lastChecked, - ZonedDateTime lastMetCondition, - Map actions, - Map headers) { + @Nullable + private Map headers; + + public WatchStatus( + long version, + State state, + ExecutionState executionState, + ZonedDateTime lastChecked, + ZonedDateTime lastMetCondition, + Map actions, + Map headers + ) { this.version = version; this.lastChecked = lastChecked; this.lastMetCondition = lastMetCondition; @@ -96,12 +99,12 @@ public boolean equals(Object o) { WatchStatus that = (WatchStatus) o; - return Objects.equals(lastChecked, that.lastChecked) && - Objects.equals(lastMetCondition, that.lastMetCondition) && - Objects.equals(version, that.version) && - Objects.equals(executionState, that.executionState) && - Objects.equals(actions, that.actions) && - Objects.equals(headers, that.headers); + return Objects.equals(lastChecked, that.lastChecked) + && Objects.equals(lastMetCondition, that.lastMetCondition) + && Objects.equals(version, that.version) + && Objects.equals(executionState, that.executionState) + && Objects.equals(actions, that.actions) + && Objects.equals(headers, that.headers); } @Override @@ -130,36 +133,47 @@ public static WatchStatus parse(XContentParser parser) throws IOException { try { state = State.parse(parser); } catch (ElasticsearchParseException e) { - throw new ElasticsearchParseException("could not parse watch status. failed to parse field [{}]", - e, currentFieldName); + throw new ElasticsearchParseException("could not parse watch status. failed to parse field [{}]", e, currentFieldName); } } else if (Field.VERSION.match(currentFieldName, parser.getDeprecationHandler())) { if (token.isValue()) { version = parser.longValue(); } else { - throw new ElasticsearchParseException("could not parse watch status. expecting field [{}] to hold a long " + - "value, found [{}] instead", currentFieldName, token); + throw new ElasticsearchParseException( + "could not parse watch status. expecting field [{}] to hold a long " + "value, found [{}] instead", + currentFieldName, + token + ); } } else if (Field.LAST_CHECKED.match(currentFieldName, parser.getDeprecationHandler())) { if (token.isValue()) { lastChecked = parseDate(currentFieldName, parser); } else { - throw new ElasticsearchParseException("could not parse watch status. expecting field [{}] to hold a date " + - "value, found [{}] instead", currentFieldName, token); + throw new ElasticsearchParseException( + "could not parse watch status. expecting field [{}] to hold a date " + "value, found [{}] instead", + currentFieldName, + token + ); } } else if (Field.LAST_MET_CONDITION.match(currentFieldName, parser.getDeprecationHandler())) { if (token.isValue()) { lastMetCondition = parseDate(currentFieldName, parser); } else { - throw new ElasticsearchParseException("could not parse watch status. expecting field [{}] to hold a date " + - "value, found [{}] instead", currentFieldName, token); + throw new ElasticsearchParseException( + "could not parse watch status. expecting field [{}] to hold a date " + "value, found [{}] instead", + currentFieldName, + token + ); } } else if (Field.EXECUTION_STATE.match(currentFieldName, parser.getDeprecationHandler())) { if (token.isValue()) { executionState = ExecutionState.resolve(parser.text()); } else { - throw new ElasticsearchParseException("could not parse watch status. expecting field [{}] to hold a string " + - "value, found [{}] instead", currentFieldName, token); + throw new ElasticsearchParseException( + "could not parse watch status. expecting field [{}] to hold a string " + "value, found [{}] instead", + currentFieldName, + token + ); } } else if (Field.ACTIONS.match(currentFieldName, parser.getDeprecationHandler())) { actions = new HashMap<>(); @@ -173,8 +187,11 @@ public static WatchStatus parse(XContentParser parser) throws IOException { } } } else { - throw new ElasticsearchParseException("could not parse watch status. expecting field [{}] to be an object, " + - "found [{}] instead", currentFieldName, token); + throw new ElasticsearchParseException( + "could not parse watch status. expecting field [{}] to be an object, " + "found [{}] instead", + currentFieldName, + token + ); } } else if (Field.HEADERS.match(currentFieldName, parser.getDeprecationHandler())) { if (token == XContentParser.Token.START_OBJECT) { diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/WatchStatusDateParser.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/WatchStatusDateParser.java index 4da29a5f94b07..f20dab54fd462 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/WatchStatusDateParser.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/WatchStatusDateParser.java @@ -11,8 +11,8 @@ import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.time.DateFormatter; import org.elasticsearch.common.time.DateFormatters; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.index.mapper.DateFieldMapper; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.time.Instant; @@ -39,8 +39,11 @@ public static ZonedDateTime parseDate(String fieldName, XContentParser parser) t if (token == XContentParser.Token.VALUE_NULL) { return null; } - throw new ElasticsearchParseException("could not parse date/time. expected date field [{}] " + - "to be either a number or a string but found [{}] instead", fieldName, token); + throw new ElasticsearchParseException( + "could not parse date/time. expected date field [{}] " + "to be either a number or a string but found [{}] instead", + fieldName, + token + ); } public static ZonedDateTime parseDate(String text) { diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/WatcherMetadata.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/WatcherMetadata.java index a1c1ddba60a69..a380114d0657a 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/WatcherMetadata.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/WatcherMetadata.java @@ -21,9 +21,10 @@ public WatcherMetadata(boolean manuallyStopped) { public boolean manuallyStopped() { return manuallyStopped; } + @Override public String toString() { - return "manuallyStopped["+ manuallyStopped +"]"; + return "manuallyStopped[" + manuallyStopped + "]"; } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/WatcherState.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/WatcherState.java index bf7dbaa462a28..26a873e7d776a 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/WatcherState.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/WatcherState.java @@ -40,4 +40,3 @@ public byte getId() { } } - diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/WatcherStatsRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/WatcherStatsRequest.java index c06c0d7460f1f..590eec690976b 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/WatcherStatsRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/WatcherStatsRequest.java @@ -18,7 +18,7 @@ public class WatcherStatsRequest implements Validatable { private final boolean includeCurrentWatches; private final boolean includeQueuedWatches; - public WatcherStatsRequest( ) { + public WatcherStatsRequest() { this(true, true); } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/WatcherStatsResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/WatcherStatsResponse.java index 6ee23029fd09e..7daaf74e9db0f 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/WatcherStatsResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/WatcherStatsResponse.java @@ -9,9 +9,9 @@ package org.elasticsearch.client.watcher; import org.elasticsearch.client.NodesResponseHeader; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.core.Tuple; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -71,17 +71,17 @@ public String getClusterName() { } @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("watcher_stats_response", true, - a -> new WatcherStatsResponse((NodesResponseHeader) a[0], (String) a[1], new WatcherMetadata((boolean) a[2]), - (List) a[3])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "watcher_stats_response", + true, + a -> new WatcherStatsResponse((NodesResponseHeader) a[0], (String) a[1], new WatcherMetadata((boolean) a[2]), (List) a[3]) + ); static { PARSER.declareObject(ConstructingObjectParser.constructorArg(), NodesResponseHeader::fromXContent, new ParseField("_nodes")); PARSER.declareString(ConstructingObjectParser.constructorArg(), new ParseField("cluster_name")); PARSER.declareBoolean(ConstructingObjectParser.constructorArg(), new ParseField("manually_stopped")); - PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), (p, c) -> Node.PARSER.apply(p, null), - new ParseField("stats")); + PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), (p, c) -> Node.PARSER.apply(p, null), new ParseField("stats")); } public static WatcherStatsResponse fromXContent(XContentParser parser) throws IOException { @@ -93,10 +93,10 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; WatcherStatsResponse that = (WatcherStatsResponse) o; - return Objects.equals(nodes, that.nodes) && - Objects.equals(header, that.header) && - Objects.equals(clusterName, that.clusterName) && - Objects.equals(watcherMetadata, that.watcherMetadata); + return Objects.equals(nodes, that.nodes) + && Objects.equals(header, that.header) + && Objects.equals(clusterName, that.clusterName) + && Objects.equals(watcherMetadata, that.watcherMetadata); } @Override @@ -107,8 +107,10 @@ public int hashCode() { public static class Node { @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("watcher_stats_node", true, (args, c) -> new Node( + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "watcher_stats_node", + true, + (args, c) -> new Node( (String) args[0], WatcherState.valueOf(((String) args[1]).toUpperCase(Locale.ROOT)), (long) args[2], @@ -118,21 +120,34 @@ public static class Node { (List) args[5], (Map) args[6] - )); + ) + ); - private static final ConstructingObjectParser, Void> THREAD_POOL_PARSER = - new ConstructingObjectParser<>("execution_thread_pool", true, (args, id) -> new Tuple<>((Long) args[0], (Long) args[1])); + private static final ConstructingObjectParser, Void> THREAD_POOL_PARSER = new ConstructingObjectParser<>( + "execution_thread_pool", + true, + (args, id) -> new Tuple<>((Long) args[0], (Long) args[1]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), new ParseField("node_id")); PARSER.declareString(ConstructingObjectParser.constructorArg(), new ParseField("watcher_state")); PARSER.declareLong(ConstructingObjectParser.constructorArg(), new ParseField("watch_count")); - PARSER.declareObject(ConstructingObjectParser.constructorArg(), THREAD_POOL_PARSER::apply, - new ParseField("execution_thread_pool")); - PARSER.declareObjectArray(ConstructingObjectParser.optionalConstructorArg(), WatchExecutionSnapshot.PARSER, - new ParseField("current_watches")); - PARSER.declareObjectArray(ConstructingObjectParser.optionalConstructorArg(), QueuedWatch.PARSER, - new ParseField("queued_watches")); + PARSER.declareObject( + ConstructingObjectParser.constructorArg(), + THREAD_POOL_PARSER::apply, + new ParseField("execution_thread_pool") + ); + PARSER.declareObjectArray( + ConstructingObjectParser.optionalConstructorArg(), + WatchExecutionSnapshot.PARSER, + new ParseField("current_watches") + ); + PARSER.declareObjectArray( + ConstructingObjectParser.optionalConstructorArg(), + QueuedWatch.PARSER, + new ParseField("queued_watches") + ); PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> p.map(), new ParseField("stats")); THREAD_POOL_PARSER.declareLong(ConstructingObjectParser.constructorArg(), new ParseField("queue_size")); @@ -149,9 +164,16 @@ public static class Node { private List queuedWatches; private Map stats; - - public Node(String nodeId, WatcherState watcherState, long watchesCount, long threadPoolQueueSize, long threadPoolMaxSize, - List snapshots, List queuedWatches, Map stats) { + public Node( + String nodeId, + WatcherState watcherState, + long watchesCount, + long threadPoolQueueSize, + long threadPoolMaxSize, + List snapshots, + List queuedWatches, + Map stats + ) { this.nodeId = nodeId; this.watcherState = watcherState; this.watchesCount = watchesCount; @@ -199,20 +221,28 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Node node = (Node) o; - return watchesCount == node.watchesCount && - threadPoolQueueSize == node.threadPoolQueueSize && - threadPoolMaxSize == node.threadPoolMaxSize && - Objects.equals(nodeId, node.nodeId) && - watcherState == node.watcherState && - Objects.equals(snapshots, node.snapshots) && - Objects.equals(queuedWatches, node.queuedWatches) && - Objects.equals(stats, node.stats); + return watchesCount == node.watchesCount + && threadPoolQueueSize == node.threadPoolQueueSize + && threadPoolMaxSize == node.threadPoolMaxSize + && Objects.equals(nodeId, node.nodeId) + && watcherState == node.watcherState + && Objects.equals(snapshots, node.snapshots) + && Objects.equals(queuedWatches, node.queuedWatches) + && Objects.equals(stats, node.stats); } @Override public int hashCode() { - return Objects.hash(nodeId, watcherState, watchesCount, threadPoolQueueSize, threadPoolMaxSize, snapshots, queuedWatches, - stats); + return Objects.hash( + nodeId, + watcherState, + watchesCount, + threadPoolQueueSize, + threadPoolMaxSize, + snapshots, + queuedWatches, + stats + ); } } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackInfoRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackInfoRequest.java index 11ae120489cf0..42993e91da26b 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackInfoRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackInfoRequest.java @@ -18,7 +18,9 @@ public class XPackInfoRequest implements Validatable { public enum Category { - BUILD, LICENSE, FEATURES; + BUILD, + LICENSE, + FEATURES; public static EnumSet toSet(String... categories) { EnumSet set = EnumSet.noneOf(Category.class); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackInfoResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackInfoResponse.java index 31a145f39a6f5..30dfdcbaf6291 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackInfoResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackInfoResponse.java @@ -9,9 +9,9 @@ import org.elasticsearch.client.license.LicenseStatus; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -34,9 +34,12 @@ public class XPackInfoResponse { public static final long BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS = Long.MAX_VALUE - TimeUnit.HOURS.toMillis(24 * 365); // TODO move this constant to License.java once we move License.java to the protocol jar - @Nullable private BuildInfo buildInfo; - @Nullable private LicenseInfo licenseInfo; - @Nullable private FeatureSetsInfo featureSetsInfo; + @Nullable + private BuildInfo buildInfo; + @Nullable + private LicenseInfo licenseInfo; + @Nullable + private FeatureSetsInfo featureSetsInfo; public XPackInfoResponse() {} @@ -74,8 +77,8 @@ public boolean equals(Object other) { if (this == other) return true; XPackInfoResponse rhs = (XPackInfoResponse) other; return Objects.equals(buildInfo, rhs.buildInfo) - && Objects.equals(licenseInfo, rhs.licenseInfo) - && Objects.equals(featureSetsInfo, rhs.featureSetsInfo); + && Objects.equals(licenseInfo, rhs.licenseInfo) + && Objects.equals(featureSetsInfo, rhs.featureSetsInfo); } @Override @@ -85,22 +88,28 @@ public int hashCode() { @Override public String toString() { - return "XPackInfoResponse{" + - "buildInfo=" + buildInfo + - ", licenseInfo=" + licenseInfo + - ", featureSetsInfo=" + featureSetsInfo + - '}'; + return "XPackInfoResponse{" + + "buildInfo=" + + buildInfo + + ", licenseInfo=" + + licenseInfo + + ", featureSetsInfo=" + + featureSetsInfo + + '}'; } private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "xpack_info_response", true, (a, v) -> { - BuildInfo buildInfo = (BuildInfo) a[0]; - LicenseInfo licenseInfo = (LicenseInfo) a[1]; - @SuppressWarnings("unchecked") // This is how constructing object parser works - List featureSets = (List) a[2]; - FeatureSetsInfo featureSetsInfo = featureSets == null ? null : new FeatureSetsInfo(new HashSet<>(featureSets)); - return new XPackInfoResponse(buildInfo, licenseInfo, featureSetsInfo); - }); + "xpack_info_response", + true, + (a, v) -> { + BuildInfo buildInfo = (BuildInfo) a[0]; + LicenseInfo licenseInfo = (LicenseInfo) a[1]; + @SuppressWarnings("unchecked") // This is how constructing object parser works + List featureSets = (List) a[2]; + FeatureSetsInfo featureSetsInfo = featureSets == null ? null : new FeatureSetsInfo(new HashSet<>(featureSets)); + return new XPackInfoResponse(buildInfo, licenseInfo, featureSetsInfo); + } + ); static { PARSER.declareObject(optionalConstructorArg(), BuildInfo.PARSER, new ParseField("build")); /* @@ -108,15 +117,16 @@ public String toString() { * optional but it can also be send as `null`. */ PARSER.declareField(optionalConstructorArg(), (p, v) -> { - if (p.currentToken() == XContentParser.Token.VALUE_NULL) { - return null; - } - return LicenseInfo.PARSER.parse(p, v); - }, - new ParseField("license"), ValueType.OBJECT_OR_NULL); - PARSER.declareNamedObjects(optionalConstructorArg(), - (p, c, name) -> FeatureSetsInfo.FeatureSet.PARSER.parse(p, name), - new ParseField("features")); + if (p.currentToken() == XContentParser.Token.VALUE_NULL) { + return null; + } + return LicenseInfo.PARSER.parse(p, v); + }, new ParseField("license"), ValueType.OBJECT_OR_NULL); + PARSER.declareNamedObjects( + optionalConstructorArg(), + (p, c, name) -> FeatureSetsInfo.FeatureSet.PARSER.parse(p, name), + new ParseField("features") + ); } public static XPackInfoResponse fromXContent(XContentParser parser) throws IOException { @@ -164,10 +174,10 @@ public boolean equals(Object other) { if (this == other) return true; LicenseInfo rhs = (LicenseInfo) other; return Objects.equals(uid, rhs.uid) - && Objects.equals(type, rhs.type) - && Objects.equals(mode, rhs.mode) - && Objects.equals(status, rhs.status) - && expiryDate == rhs.expiryDate; + && Objects.equals(type, rhs.type) + && Objects.equals(mode, rhs.mode) + && Objects.equals(status, rhs.status) + && expiryDate == rhs.expiryDate; } @Override @@ -177,25 +187,36 @@ public int hashCode() { @Override public String toString() { - return "LicenseInfo{" + - "uid='" + uid + '\'' + - ", type='" + type + '\'' + - ", mode='" + mode + '\'' + - ", status=" + status + - ", expiryDate=" + expiryDate + - '}'; + return "LicenseInfo{" + + "uid='" + + uid + + '\'' + + ", type='" + + type + + '\'' + + ", mode='" + + mode + + '\'' + + ", status=" + + status + + ", expiryDate=" + + expiryDate + + '}'; } private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "license_info", true, (a, v) -> { - String uid = (String) a[0]; - String type = (String) a[1]; - String mode = (String) a[2]; - LicenseStatus status = LicenseStatus.fromString((String) a[3]); - Long expiryDate = (Long) a[4]; - long primitiveExpiryDate = expiryDate == null ? BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS : expiryDate; - return new LicenseInfo(uid, type, mode, status, primitiveExpiryDate); - }); + "license_info", + true, + (a, v) -> { + String uid = (String) a[0]; + String type = (String) a[1]; + String mode = (String) a[2]; + LicenseStatus status = LicenseStatus.fromString((String) a[3]); + Long expiryDate = (Long) a[4]; + long primitiveExpiryDate = expiryDate == null ? BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS : expiryDate; + return new LicenseInfo(uid, type, mode, status, primitiveExpiryDate); + } + ); static { PARSER.declareString(constructorArg(), new ParseField("uid")); PARSER.declareString(constructorArg(), new ParseField("type")); @@ -227,8 +248,7 @@ public boolean equals(Object other) { if (other == null || other.getClass() != getClass()) return false; if (this == other) return true; BuildInfo rhs = (BuildInfo) other; - return Objects.equals(hash, rhs.hash) - && Objects.equals(timestamp, rhs.timestamp); + return Objects.equals(hash, rhs.hash) && Objects.equals(timestamp, rhs.timestamp); } @Override @@ -238,14 +258,14 @@ public int hashCode() { @Override public String toString() { - return "BuildInfo{" + - "hash='" + hash + '\'' + - ", timestamp='" + timestamp + '\'' + - '}'; + return "BuildInfo{" + "hash='" + hash + '\'' + ", timestamp='" + timestamp + '\'' + '}'; } private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "build_info", true, (a, v) -> new BuildInfo((String) a[0], (String) a[1])); + "build_info", + true, + (a, v) -> new BuildInfo((String) a[0], (String) a[1]) + ); static { PARSER.declareString(constructorArg(), new ParseField("hash")); PARSER.declareString(constructorArg(), new ParseField("date")); @@ -282,20 +302,25 @@ public int hashCode() { @Override public String toString() { - return "FeatureSetsInfo{" + - "featureSets=" + featureSets + - '}'; + return "FeatureSetsInfo{" + "featureSets=" + featureSets + '}'; } public static class FeatureSet { private final String name; - @Nullable private final String description; + @Nullable + private final String description; private final boolean available; private final boolean enabled; - @Nullable private final Map nativeCodeInfo; - - public FeatureSet(String name, @Nullable String description, boolean available, boolean enabled, - @Nullable Map nativeCodeInfo) { + @Nullable + private final Map nativeCodeInfo; + + public FeatureSet( + String name, + @Nullable String description, + boolean available, + boolean enabled, + @Nullable Map nativeCodeInfo + ) { this.name = name; this.description = description; this.available = available; @@ -331,10 +356,10 @@ public boolean equals(Object other) { if (this == other) return true; FeatureSet rhs = (FeatureSet) other; return Objects.equals(name, rhs.name) - && Objects.equals(description, rhs.description) - && available == rhs.available - && enabled == rhs.enabled - && Objects.equals(nativeCodeInfo, rhs.nativeCodeInfo); + && Objects.equals(description, rhs.description) + && available == rhs.available + && enabled == rhs.enabled + && Objects.equals(nativeCodeInfo, rhs.nativeCodeInfo); } @Override @@ -344,24 +369,34 @@ public int hashCode() { @Override public String toString() { - return "FeatureSet{" + - "name='" + name + '\'' + - ", description='" + description + '\'' + - ", available=" + available + - ", enabled=" + enabled + - ", nativeCodeInfo=" + nativeCodeInfo + - '}'; + return "FeatureSet{" + + "name='" + + name + + '\'' + + ", description='" + + description + + '\'' + + ", available=" + + available + + ", enabled=" + + enabled + + ", nativeCodeInfo=" + + nativeCodeInfo + + '}'; } private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "feature_set", true, (a, name) -> { - String description = (String) a[0]; - boolean available = (Boolean) a[1]; - boolean enabled = (Boolean) a[2]; - @SuppressWarnings("unchecked") // Matches up with declaration below - Map nativeCodeInfo = (Map) a[3]; - return new FeatureSet(name, description, available, enabled, nativeCodeInfo); - }); + "feature_set", + true, + (a, name) -> { + String description = (String) a[0]; + boolean available = (Boolean) a[1]; + boolean enabled = (Boolean) a[2]; + @SuppressWarnings("unchecked") // Matches up with declaration below + Map nativeCodeInfo = (Map) a[3]; + return new FeatureSet(name, description, available, enabled, nativeCodeInfo); + } + ); static { PARSER.declareString(optionalConstructorArg(), new ParseField("description")); PARSER.declareBoolean(constructorArg(), new ParseField("available")); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackUsageResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackUsageResponse.java index e7a5c5423d261..f26e0d203ac6d 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackUsageResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/xpack/XPackUsageResponse.java @@ -29,7 +29,7 @@ private XPackUsageResponse(Map> usages) { @SuppressWarnings("unchecked") private static Map castMap(Object value) { - return (Map)value; + return (Map) value; } /** Return a map from feature name to usage information for that feature. */ @@ -39,8 +39,9 @@ public Map> getUsages() { public static XPackUsageResponse fromXContent(XContentParser parser) throws IOException { Map rawMap = parser.map(); - Map> usages = rawMap.entrySet().stream().collect( - Collectors.toMap(Map.Entry::getKey, e -> castMap(e.getValue()))); + Map> usages = rawMap.entrySet() + .stream() + .collect(Collectors.toMap(Map.Entry::getKey, e -> castMap(e.getValue()))); return new XPackUsageResponse(usages); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/GeoIpStatsResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/GeoIpStatsResponseTests.java index b1418723ad633..673f2e6adf154 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/GeoIpStatsResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/GeoIpStatsResponseTests.java @@ -9,8 +9,8 @@ package org.elasticsearch; import org.elasticsearch.client.GeoIpStatsResponse; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.HashMap; @@ -24,10 +24,17 @@ protected GeoIpStatsResponse createTestInstance() { HashMap nodes = new HashMap<>(); int nodeCount = randomInt(10); for (int i = 0; i < nodeCount; i++) { - List databases = randomList(5, - () -> new GeoIpStatsResponse.DatabaseInfo(randomAlphaOfLength(5))); - nodes.put(randomAlphaOfLength(5), new GeoIpStatsResponse.NodeInfo(randomList(5, () -> randomAlphaOfLength(5)), - databases.stream().collect(Collectors.toMap(GeoIpStatsResponse.DatabaseInfo::getName, d -> d)))); + List databases = randomList( + 5, + () -> new GeoIpStatsResponse.DatabaseInfo(randomAlphaOfLength(5)) + ); + nodes.put( + randomAlphaOfLength(5), + new GeoIpStatsResponse.NodeInfo( + randomList(5, () -> randomAlphaOfLength(5)), + databases.stream().collect(Collectors.toMap(GeoIpStatsResponse.DatabaseInfo::getName, d -> d)) + ) + ); } return new GeoIpStatsResponse(randomInt(), randomInt(), randomNonNegativeLong(), randomInt(), randomInt(), nodes); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/AbstractRequestTestCase.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/AbstractRequestTestCase.java index f357d6b1561fe..44241684edeba 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/AbstractRequestTestCase.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/AbstractRequestTestCase.java @@ -9,13 +9,13 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContent; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; import java.io.IOException; @@ -37,10 +37,7 @@ public final void testFromXContent() throws IOException { final BytesReference bytes = toShuffledXContent(clientTestInstance, xContentType, ToXContent.EMPTY_PARAMS, randomBoolean()); final XContent xContent = XContentFactory.xContent(xContentType); - final XContentParser parser = xContent.createParser( - xContentRegistry(), - LoggingDeprecationHandler.INSTANCE, - bytes.streamInput()); + final XContentParser parser = xContent.createParser(xContentRegistry(), LoggingDeprecationHandler.INSTANCE, bytes.streamInput()); final S serverInstance = doParseToServerInstance(parser); assertInstances(serverInstance, clientTestInstance); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/AbstractResponseTestCase.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/AbstractResponseTestCase.java index 9670159414608..fb617beab35b6 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/AbstractResponseTestCase.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/AbstractResponseTestCase.java @@ -10,13 +10,13 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.collect.ImmutableOpenMap; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContent; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; import java.io.IOException; import java.util.Map; @@ -42,7 +42,8 @@ public final void testFromXContent() throws IOException { final XContentParser parser = xContent.createParser( NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, - bytes.streamInput()); + bytes.streamInput() + ); final C clientInstance = doParseToClientInstance(parser); assertInstances(serverTestInstance, clientInstance); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/BulkProcessorIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/BulkProcessorIT.java index 95fcea671bc04..6aa9be9e378b3 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/BulkProcessorIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/BulkProcessorIT.java @@ -9,6 +9,7 @@ package org.elasticsearch.client; import com.carrotsearch.randomizedtesting.generators.RandomPicks; + import org.elasticsearch.action.bulk.BulkItemResponse; import org.elasticsearch.action.bulk.BulkProcessor; import org.elasticsearch.action.bulk.BulkRequest; @@ -23,9 +24,9 @@ import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.search.SearchHit; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.search.SearchHit; import org.hamcrest.Matcher; import java.io.IOException; @@ -39,10 +40,10 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.IntStream; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.fieldFromSource; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.hasId; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.hasProperty; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.Matchers.both; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.either; @@ -59,8 +60,10 @@ public class BulkProcessorIT extends ESRestHighLevelClientTestCase { private static BulkProcessor.Builder initBulkProcessorBuilder(BulkProcessor.Listener listener) { return BulkProcessor.builder( - (request, bulkListener) -> highLevelClient().bulkAsync(request, RequestOptions.DEFAULT, - bulkListener), listener, "BulkProcessorIT"); + (request, bulkListener) -> highLevelClient().bulkAsync(request, RequestOptions.DEFAULT, bulkListener), + listener, + "BulkProcessorIT" + ); } public void testThatBulkProcessorCountIsCorrect() throws Exception { @@ -68,11 +71,15 @@ public void testThatBulkProcessorCountIsCorrect() throws Exception { BulkProcessorTestListener listener = new BulkProcessorTestListener(latch); int numDocs = randomIntBetween(10, 100); - try (BulkProcessor processor = initBulkProcessorBuilder(listener) - //let's make sure that the bulk action limit trips, one single execution will index all the documents - .setConcurrentRequests(randomIntBetween(0, 1)).setBulkActions(numDocs) - .setFlushInterval(TimeValue.timeValueHours(24)).setBulkSize(new ByteSizeValue(1, ByteSizeUnit.GB)) - .build()) { + try ( + BulkProcessor processor = initBulkProcessorBuilder(listener) + // let's make sure that the bulk action limit trips, one single execution will index all the documents + .setConcurrentRequests(randomIntBetween(0, 1)) + .setBulkActions(numDocs) + .setFlushInterval(TimeValue.timeValueHours(24)) + .setBulkSize(new ByteSizeValue(1, ByteSizeUnit.GB)) + .build() + ) { MultiGetRequest multiGetRequest = indexDocs(processor, numDocs); @@ -92,15 +99,20 @@ public void testBulkProcessorFlush() throws Exception { int numDocs = randomIntBetween(10, 100); - try (BulkProcessor processor = initBulkProcessorBuilder(listener) - //let's make sure that this bulk won't be automatically flushed - .setConcurrentRequests(randomIntBetween(0, 10)).setBulkActions(numDocs + randomIntBetween(1, 100)) - .setFlushInterval(TimeValue.timeValueHours(24)).setBulkSize(new ByteSizeValue(1, ByteSizeUnit.GB)).build()) { + try ( + BulkProcessor processor = initBulkProcessorBuilder(listener) + // let's make sure that this bulk won't be automatically flushed + .setConcurrentRequests(randomIntBetween(0, 10)) + .setBulkActions(numDocs + randomIntBetween(1, 100)) + .setFlushInterval(TimeValue.timeValueHours(24)) + .setBulkSize(new ByteSizeValue(1, ByteSizeUnit.GB)) + .build() + ) { MultiGetRequest multiGetRequest = indexDocs(processor, numDocs); assertThat(latch.await(randomInt(500), TimeUnit.MILLISECONDS), equalTo(false)); - //we really need an explicit flush as none of the bulk thresholds was reached + // we really need an explicit flush as none of the bulk thresholds was reached processor.flush(); latch.await(); @@ -127,10 +139,14 @@ public void testBulkProcessorConcurrentRequests() throws Exception { MultiGetRequest multiGetRequest; - try (BulkProcessor processor = initBulkProcessorBuilder(listener) - .setConcurrentRequests(concurrentRequests).setBulkActions(bulkActions) - //set interval and size to high values - .setFlushInterval(TimeValue.timeValueHours(24)).setBulkSize(new ByteSizeValue(1, ByteSizeUnit.GB)).build()) { + try ( + BulkProcessor processor = initBulkProcessorBuilder(listener).setConcurrentRequests(concurrentRequests) + .setBulkActions(bulkActions) + // set interval and size to high values + .setFlushInterval(TimeValue.timeValueHours(24)) + .setBulkSize(new ByteSizeValue(1, ByteSizeUnit.GB)) + .build() + ) { multiGetRequest = indexDocs(processor, numDocs); @@ -153,9 +169,9 @@ public void testBulkProcessorConcurrentRequests() throws Exception { for (BulkItemResponse bulkItemResponse : listener.bulkItems) { assertThat(bulkItemResponse.getFailureMessage(), bulkItemResponse.isFailed(), equalTo(false)); assertThat(bulkItemResponse.getIndex(), equalTo("test")); - //with concurrent requests > 1 we can't rely on the order of the bulk requests + // with concurrent requests > 1 we can't rely on the order of the bulk requests assertThat(Integer.valueOf(bulkItemResponse.getId()), both(greaterThan(0)).and(lessThanOrEqualTo(numDocs))); - //we do want to check that we don't get duplicate ids back + // we do want to check that we don't get duplicate ids back assertThat(ids.add(bulkItemResponse.getId()), equalTo(true)); } @@ -167,11 +183,12 @@ public void testBulkProcessorWaitOnClose() throws Exception { int numDocs = randomIntBetween(10, 100); BulkProcessor processor = initBulkProcessorBuilder(listener) - //let's make sure that the bulk action limit trips, one single execution will index all the documents - .setConcurrentRequests(randomIntBetween(0, 1)).setBulkActions(numDocs) - .setFlushInterval(TimeValue.timeValueHours(24)).setBulkSize(new ByteSizeValue(randomIntBetween(1, 10), - RandomPicks.randomFrom(random(), ByteSizeUnit.values()))) - .build(); + // let's make sure that the bulk action limit trips, one single execution will index all the documents + .setConcurrentRequests(randomIntBetween(0, 1)) + .setBulkActions(numDocs) + .setFlushInterval(TimeValue.timeValueHours(24)) + .setBulkSize(new ByteSizeValue(randomIntBetween(1, 10), RandomPicks.randomFrom(random(), ByteSizeUnit.values()))) + .build(); MultiGetRequest multiGetRequest = indexDocs(processor, numDocs); assertThat(processor.awaitClose(1, TimeUnit.MINUTES), is(true)); @@ -195,14 +212,16 @@ public void testBulkProcessorWaitOnClose() throws Exception { public void testBulkProcessorConcurrentRequestsReadOnlyIndex() throws Exception { Request request = new Request("PUT", "/test-ro"); - request.setJsonEntity("{\n" + - " \"settings\" : {\n" + - " \"index\" : {\n" + - " \"blocks.write\" : true\n" + - " }\n" + - " }\n" + - " \n" + - "}"); + request.setJsonEntity( + "{\n" + + " \"settings\" : {\n" + + " \"index\" : {\n" + + " \"blocks.write\" : true\n" + + " }\n" + + " }\n" + + " \n" + + "}" + ); Response response = client().performRequest(request); assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); @@ -221,22 +240,26 @@ public void testBulkProcessorConcurrentRequestsReadOnlyIndex() throws Exception MultiGetRequest multiGetRequest = new MultiGetRequest(); BulkProcessorTestListener listener = new BulkProcessorTestListener(latch, closeLatch); - try (BulkProcessor processor = initBulkProcessorBuilder(listener) - .setConcurrentRequests(concurrentRequests).setBulkActions(bulkActions) - //set interval and size to high values - .setFlushInterval(TimeValue.timeValueHours(24)).setBulkSize(new ByteSizeValue(1, ByteSizeUnit.GB)).build()) { + try ( + BulkProcessor processor = initBulkProcessorBuilder(listener).setConcurrentRequests(concurrentRequests) + .setBulkActions(bulkActions) + // set interval and size to high values + .setFlushInterval(TimeValue.timeValueHours(24)) + .setBulkSize(new ByteSizeValue(1, ByteSizeUnit.GB)) + .build() + ) { for (int i = 1; i <= numDocs; i++) { // let's make sure we get at least 1 item in the MultiGetRequest regardless of the randomising roulette if (randomBoolean() || multiGetRequest.getItems().size() == 0) { testDocs++; - processor.add(new IndexRequest("test").id(Integer.toString(testDocs)) - .source(XContentType.JSON, "field", "value")); + processor.add(new IndexRequest("test").id(Integer.toString(testDocs)).source(XContentType.JSON, "field", "value")); multiGetRequest.add("test", Integer.toString(testDocs)); } else { testReadOnlyDocs++; - processor.add(new IndexRequest("test-ro").id(Integer.toString(testReadOnlyDocs)) - .source(XContentType.JSON, "field", "value")); + processor.add( + new IndexRequest("test-ro").id(Integer.toString(testReadOnlyDocs)).source(XContentType.JSON, "field", "value") + ); } } } @@ -254,15 +277,15 @@ public void testBulkProcessorConcurrentRequestsReadOnlyIndex() throws Exception assertThat(bulkItemResponse.getIndex(), either(equalTo("test")).or(equalTo("test-ro"))); if (bulkItemResponse.getIndex().equals("test")) { assertThat(bulkItemResponse.isFailed(), equalTo(false)); - //with concurrent requests > 1 we can't rely on the order of the bulk requests + // with concurrent requests > 1 we can't rely on the order of the bulk requests assertThat(Integer.valueOf(bulkItemResponse.getId()), both(greaterThan(0)).and(lessThanOrEqualTo(testDocs))); - //we do want to check that we don't get duplicate ids back + // we do want to check that we don't get duplicate ids back assertThat(ids.add(bulkItemResponse.getId()), equalTo(true)); } else { assertThat(bulkItemResponse.isFailed(), equalTo(true)); - //with concurrent requests > 1 we can't rely on the order of the bulk requests + // with concurrent requests > 1 we can't rely on the order of the bulk requests assertThat(Integer.valueOf(bulkItemResponse.getId()), both(greaterThan(0)).and(lessThanOrEqualTo(testReadOnlyDocs))); - //we do want to check that we don't get duplicate ids back + // we do want to check that we don't get duplicate ids back assertThat(readOnlyIds.add(bulkItemResponse.getId()), equalTo(true)); } } @@ -297,7 +320,6 @@ public void testGlobalParametersAndSingleRequest() throws Exception { assertThat(hits, everyItem(hasProperty(fieldFromSource("user"), equalTo("some user")))); assertThat(hits, everyItem(hasProperty(fieldFromSource("fieldNameXYZ"), equalTo("valueXYZ")))); - Iterable blogs = searchAll(new SearchRequest("blogs").routing("routing")); assertThat(blogs, everyItem(hasProperty(fieldFromSource("title"), equalTo("some title")))); assertThat(blogs, everyItem(hasProperty(fieldFromSource("fieldNameXYZ"), equalTo("valueXYZ")))); @@ -312,14 +334,18 @@ public void testGlobalParametersAndBulkProcessor() throws Exception { { final CountDownLatch latch = new CountDownLatch(1); BulkProcessorTestListener listener = new BulkProcessorTestListener(latch); - try (BulkProcessor processor = initBulkProcessorBuilder(listener) - //let's make sure that the bulk action limit trips, one single execution will index all the documents - .setConcurrentRequests(randomIntBetween(0, 1)).setBulkActions(numDocs) - .setFlushInterval(TimeValue.timeValueHours(24)).setBulkSize(new ByteSizeValue(1, ByteSizeUnit.GB)) + try ( + BulkProcessor processor = initBulkProcessorBuilder(listener) + // let's make sure that the bulk action limit trips, one single execution will index all the documents + .setConcurrentRequests(randomIntBetween(0, 1)) + .setBulkActions(numDocs) + .setFlushInterval(TimeValue.timeValueHours(24)) + .setBulkSize(new ByteSizeValue(1, ByteSizeUnit.GB)) .setGlobalIndex("test") .setGlobalRouting("routing") .setGlobalPipeline("pipeline_id") - .build()) { + .build() + ) { indexDocs(processor, numDocs, null, "test", "pipeline_id"); latch.await(); @@ -340,19 +366,18 @@ public void testGlobalParametersAndBulkProcessor() throws Exception { @SuppressWarnings("unchecked") private Matcher[] expectedIds(int numDocs) { - return IntStream.rangeClosed(1, numDocs) - .boxed() - .map(n -> hasId(n.toString())) - .>toArray(Matcher[]::new); + return IntStream.rangeClosed(1, numDocs).boxed().map(n -> hasId(n.toString())).>toArray(Matcher[]::new); } - private MultiGetRequest indexDocs(BulkProcessor processor, int numDocs, String localIndex, - String globalIndex, String globalPipeline) throws Exception { + private MultiGetRequest indexDocs(BulkProcessor processor, int numDocs, String localIndex, String globalIndex, String globalPipeline) + throws Exception { MultiGetRequest multiGetRequest = new MultiGetRequest(); for (int i = 1; i <= numDocs; i++) { if (randomBoolean()) { - processor.add(new IndexRequest(localIndex).id(Integer.toString(i)) - .source(XContentType.JSON, "field", randomRealisticUnicodeOfLengthBetween(1, 30))); + processor.add( + new IndexRequest(localIndex).id(Integer.toString(i)) + .source(XContentType.JSON, "field", randomRealisticUnicodeOfLengthBetween(1, 30)) + ); } else { BytesArray data = bytesBulkRequest(localIndex, i); processor.add(data, globalIndex, globalPipeline, XContentType.JSON); @@ -372,10 +397,7 @@ private static BytesArray bytesBulkRequest(String localIndex, int id) throws IOE action.field("_id", Integer.toString(id)); action.endObject().endObject(); - XContentBuilder source = jsonBuilder() - .startObject() - .field("field", randomRealisticUnicodeOfLengthBetween(1, 30)) - .endObject(); + XContentBuilder source = jsonBuilder().startObject().field("field", randomRealisticUnicodeOfLengthBetween(1, 30)).endObject(); String request = Strings.toString(action) + "\n" + Strings.toString(source) + "\n"; return new BytesArray(request); @@ -391,8 +413,11 @@ private static void assertResponseItems(List bulkItemResponses for (BulkItemResponse bulkItemResponse : bulkItemResponses) { assertThat(bulkItemResponse.getIndex(), equalTo("test")); assertThat(bulkItemResponse.getId(), equalTo(Integer.toString(i++))); - assertThat("item " + i + " failed with cause: " + bulkItemResponse.getFailureMessage(), - bulkItemResponse.isFailed(), equalTo(false)); + assertThat( + "item " + i + " failed with cause: " + bulkItemResponse.getFailureMessage(), + bulkItemResponse.isFailed(), + equalTo(false) + ); } } @@ -441,5 +466,4 @@ public void afterBulk(long executionId, BulkRequest request, Throwable failure) } } - } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/BulkProcessorRetryIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/BulkProcessorRetryIT.java index 4cb771b9f399b..befc02492501a 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/BulkProcessorRetryIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/BulkProcessorRetryIT.java @@ -16,9 +16,9 @@ import org.elasticsearch.action.get.MultiGetRequest; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.transport.RemoteTransportException; +import org.elasticsearch.xcontent.XContentType; import java.util.Collections; import java.util.Iterator; @@ -37,8 +37,11 @@ public class BulkProcessorRetryIT extends ESRestHighLevelClientTestCase { private static final String INDEX_NAME = "index"; private static BulkProcessor.Builder initBulkProcessorBuilder(BulkProcessor.Listener listener) { - return BulkProcessor.builder((request, bulkListener) - -> highLevelClient().bulkAsync(request, RequestOptions.DEFAULT, bulkListener), listener, "BulkProcessorRetryIT"); + return BulkProcessor.builder( + (request, bulkListener) -> highLevelClient().bulkAsync(request, RequestOptions.DEFAULT, bulkListener), + listener, + "BulkProcessorRetryIT" + ); } public void testBulkRejectionLoadWithoutBackoff() throws Exception { @@ -59,8 +62,7 @@ private void executeBulkRejectionLoad(BackoffPolicy backoffPolicy, boolean rejec BulkProcessor bulkProcessor = initBulkProcessorBuilder(new BulkProcessor.Listener() { @Override - public void beforeBulk(long executionId, BulkRequest request) { - } + public void beforeBulk(long executionId, BulkRequest request) {} @Override public void afterBulk(long executionId, BulkRequest request, BulkResponse response) { @@ -75,10 +77,7 @@ public void afterBulk(long executionId, BulkRequest request, Throwable failure) responses.add(failure); latch.countDown(); } - }).setBulkActions(1) - .setConcurrentRequests(randomIntBetween(0, 100)) - .setBackoffPolicy(internalPolicy) - .build(); + }).setBulkActions(1).setConcurrentRequests(randomIntBetween(0, 100)).setBackoffPolicy(internalPolicy).build(); MultiGetRequest multiGetRequest = indexDocs(bulkProcessor, numberOfAsyncOps); latch.await(10, TimeUnit.SECONDS); @@ -146,8 +145,10 @@ private void assertRetriedCorrectly(CorrelatingBackoffPolicy internalPolicy, Obj private static MultiGetRequest indexDocs(BulkProcessor processor, int numDocs) { MultiGetRequest multiGetRequest = new MultiGetRequest(); for (int i = 1; i <= numDocs; i++) { - processor.add(new IndexRequest(INDEX_NAME).id(Integer.toString(i)) - .source(XContentType.JSON, "field", randomRealisticUnicodeOfCodepointLengthBetween(1, 30))); + processor.add( + new IndexRequest(INDEX_NAME).id(Integer.toString(i)) + .source(XContentType.JSON, "field", randomRealisticUnicodeOfCodepointLengthBetween(1, 30)) + ); multiGetRequest.add(INDEX_NAME, Integer.toString(i)); } return multiGetRequest; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/BulkRequestWithGlobalParametersIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/BulkRequestWithGlobalParametersIT.java index 9f4cb54595597..2ed0559f172d0 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/BulkRequestWithGlobalParametersIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/BulkRequestWithGlobalParametersIT.java @@ -12,8 +12,8 @@ import org.elasticsearch.action.bulk.BulkResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.search.SearchRequest; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.search.SearchHit; +import org.elasticsearch.xcontent.XContentType; import java.io.IOException; import java.util.function.Function; @@ -35,10 +35,8 @@ public void testGlobalPipelineOnBulkRequest() throws IOException { createFieldAddingPipleine("xyz", "fieldNameXYZ", "valueXYZ"); BulkRequest request = new BulkRequest(); - request.add(new IndexRequest("test").id("1") - .source(XContentType.JSON, "field", "bulk1")); - request.add(new IndexRequest("test").id("2") - .source(XContentType.JSON, "field", "bulk2")); + request.add(new IndexRequest("test").id("1").source(XContentType.JSON, "field", "bulk1")); + request.add(new IndexRequest("test").id("2").source(XContentType.JSON, "field", "bulk2")); request.pipeline("xyz"); bulk(request); @@ -54,12 +52,8 @@ public void testPipelineOnRequestOverridesGlobalPipeline() throws IOException { BulkRequest request = new BulkRequest(); request.pipeline("globalId"); - request.add(new IndexRequest("test").id("1") - .source(XContentType.JSON, "field", "bulk1") - .setPipeline("perIndexId")); - request.add(new IndexRequest("test").id("2") - .source(XContentType.JSON, "field", "bulk2") - .setPipeline("perIndexId")); + request.add(new IndexRequest("test").id("1").source(XContentType.JSON, "field", "bulk1").setPipeline("perIndexId")); + request.add(new IndexRequest("test").id("2").source(XContentType.JSON, "field", "bulk2").setPipeline("perIndexId")); bulk(request); @@ -87,19 +81,19 @@ public void testMixPipelineOnRequestAndGlobal() throws IOException { bulk(request); Iterable hits = searchAll("test"); - assertThat(hits, containsInAnyOrder( - both(hasId("1")) - .and(hasProperty(fieldFromSource("someNewField"), equalTo("someValue"))), - both(hasId("2")) - .and(hasProperty(fieldFromSource("fieldXYZ"), equalTo("valueXYZ"))))); + assertThat( + hits, + containsInAnyOrder( + both(hasId("1")).and(hasProperty(fieldFromSource("someNewField"), equalTo("someValue"))), + both(hasId("2")).and(hasProperty(fieldFromSource("fieldXYZ"), equalTo("valueXYZ"))) + ) + ); } public void testGlobalIndex() throws IOException { BulkRequest request = new BulkRequest("global_index"); - request.add(new IndexRequest().id("1") - .source(XContentType.JSON, "field", "bulk1")); - request.add(new IndexRequest().id("2") - .source(XContentType.JSON, "field", "bulk2")); + request.add(new IndexRequest().id("1").source(XContentType.JSON, "field", "bulk1")); + request.add(new IndexRequest().id("2").source(XContentType.JSON, "field", "bulk2")); bulk(request); @@ -110,28 +104,23 @@ public void testGlobalIndex() throws IOException { @SuppressWarnings("unchecked") public void testIndexGlobalAndPerRequest() throws IOException { BulkRequest request = new BulkRequest("global_index"); - request.add(new IndexRequest("local_index").id("1") - .source(XContentType.JSON, "field", "bulk1")); - request.add(new IndexRequest().id("2") // will take global index - .source(XContentType.JSON, "field", "bulk2")); + request.add(new IndexRequest("local_index").id("1").source(XContentType.JSON, "field", "bulk1")); + request.add( + new IndexRequest().id("2") // will take global index + .source(XContentType.JSON, "field", "bulk2") + ); bulk(request); Iterable hits = searchAll("local_index", "global_index"); - assertThat(hits, containsInAnyOrder( - both(hasId("1")) - .and(hasIndex("local_index")), - both(hasId("2")) - .and(hasIndex("global_index")))); + assertThat(hits, containsInAnyOrder(both(hasId("1")).and(hasIndex("local_index")), both(hasId("2")).and(hasIndex("global_index")))); } public void testGlobalRouting() throws IOException { createIndexWithMultipleShards("index"); BulkRequest request = new BulkRequest((String) null); - request.add(new IndexRequest("index").id("1") - .source(XContentType.JSON, "field", "bulk1")); - request.add(new IndexRequest("index").id("2") - .source(XContentType.JSON, "field", "bulk1")); + request.add(new IndexRequest("index").id("1").source(XContentType.JSON, "field", "bulk1")); + request.add(new IndexRequest("index").id("2").source(XContentType.JSON, "field", "bulk1")); request.routing("1"); bulk(request); @@ -145,11 +134,8 @@ public void testGlobalRouting() throws IOException { public void testMixLocalAndGlobalRouting() throws IOException { BulkRequest request = new BulkRequest((String) null); request.routing("globalRouting"); - request.add(new IndexRequest("index").id("1") - .source(XContentType.JSON, "field", "bulk1")); - request.add(new IndexRequest("index").id( "2") - .routing("localRouting") - .source(XContentType.JSON, "field", "bulk1")); + request.add(new IndexRequest("index").id("1").source(XContentType.JSON, "field", "bulk1")); + request.add(new IndexRequest("index").id("2").routing("localRouting").source(XContentType.JSON, "field", "bulk1")); bulk(request); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/CCRIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/CCRIT.java index 62bc270fedfdc..18cb69cab5984 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/CCRIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/CCRIT.java @@ -41,9 +41,9 @@ import org.elasticsearch.client.indices.CreateIndexResponse; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.seqno.ReplicationTracker; import org.elasticsearch.test.rest.yaml.ObjectPath; +import org.elasticsearch.xcontent.XContentType; import org.junit.Before; import java.io.IOException; @@ -82,8 +82,7 @@ public void testIndexFollowing() throws Exception { assertThat(putFollowResponse.isFollowIndexShardsAcked(), is(true)); assertThat(putFollowResponse.isIndexFollowingStarted(), is(true)); - IndexRequest indexRequest = new IndexRequest("leader") - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + IndexRequest indexRequest = new IndexRequest("leader").setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) .source("{}", XContentType.JSON); highLevelClient().index(indexRequest, RequestOptions.DEFAULT); @@ -94,8 +93,7 @@ public void testIndexFollowing() throws Exception { try { assertBusy(() -> { FollowInfoRequest followInfoRequest = new FollowInfoRequest("follower"); - FollowInfoResponse followInfoResponse = - execute(followInfoRequest, ccrClient::getFollowInfo, ccrClient::getFollowInfoAsync); + FollowInfoResponse followInfoResponse = execute(followInfoRequest, ccrClient::getFollowInfo, ccrClient::getFollowInfoAsync); assertThat(followInfoResponse.getInfos().size(), equalTo(1)); assertThat(followInfoResponse.getInfos().get(0).getFollowerIndex(), equalTo("follower")); assertThat(followInfoResponse.getInfos().get(0).getLeaderIndex(), equalTo("leader")); @@ -103,8 +101,11 @@ public void testIndexFollowing() throws Exception { assertThat(followInfoResponse.getInfos().get(0).getStatus(), equalTo(FollowInfoResponse.Status.ACTIVE)); FollowStatsRequest followStatsRequest = new FollowStatsRequest("follower"); - FollowStatsResponse followStatsResponse = - execute(followStatsRequest, ccrClient::getFollowStats, ccrClient::getFollowStatsAsync); + FollowStatsResponse followStatsResponse = execute( + followStatsRequest, + ccrClient::getFollowStats, + ccrClient::getFollowStatsAsync + ); List shardFollowStats = followStatsResponse.getIndicesFollowStats().getShardFollowStats("follower"); long followerGlobalCheckpoint = shardFollowStats.stream() .mapToLong(ShardFollowStats::getFollowerGlobalCheckpoint) @@ -117,19 +118,22 @@ public void testIndexFollowing() throws Exception { assertThat(followerSearchResponse.getHits().getTotalHits().value, equalTo(1L)); GetSettingsRequest followerSettingsRequest = new GetSettingsRequest().indices("follower"); - GetSettingsResponse followerSettingsResponse = - highLevelClient().indices().getSettings(followerSettingsRequest, RequestOptions.DEFAULT); + GetSettingsResponse followerSettingsResponse = highLevelClient().indices() + .getSettings(followerSettingsRequest, RequestOptions.DEFAULT); assertThat( IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.get(followerSettingsResponse.getIndexToSettings().get("follower")), - equalTo(0)); + equalTo(0) + ); }); } catch (Exception e) { IndicesFollowStats followStats = ccrClient.getCcrStats(new CcrStatsRequest(), RequestOptions.DEFAULT).getIndicesFollowStats(); for (Map.Entry> entry : followStats.getShardFollowStats().entrySet()) { for (ShardFollowStats shardFollowStats : entry.getValue()) { if (shardFollowStats.getFatalException() != null) { - logger.warn(new ParameterizedMessage("fatal shard follow exception {}", shardFollowStats.getShardId()), - shardFollowStats.getFatalException()); + logger.warn( + new ParameterizedMessage("fatal shard follow exception {}", shardFollowStats.getShardId()), + shardFollowStats.getFatalException() + ); } } } @@ -147,8 +151,11 @@ public void testIndexFollowing() throws Exception { assertBusy(() -> { FollowStatsRequest followStatsRequest = new FollowStatsRequest("follower"); - FollowStatsResponse followStatsResponse = - execute(followStatsRequest, ccrClient::getFollowStats, ccrClient::getFollowStatsAsync); + FollowStatsResponse followStatsResponse = execute( + followStatsRequest, + ccrClient::getFollowStats, + ccrClient::getFollowStatsAsync + ); List shardFollowStats = followStatsResponse.getIndicesFollowStats().getShardFollowStats("follower"); long followerGlobalCheckpoint = shardFollowStats.stream() .mapToLong(ShardFollowStats::getFollowerGlobalCheckpoint) @@ -168,8 +175,7 @@ public void testIndexFollowing() throws Exception { assertBusy(() -> { FollowInfoRequest followInfoRequest = new FollowInfoRequest("follower"); - FollowInfoResponse followInfoResponse = - execute(followInfoRequest, ccrClient::getFollowInfo, ccrClient::getFollowInfoAsync); + FollowInfoResponse followInfoResponse = execute(followInfoRequest, ccrClient::getFollowInfo, ccrClient::getFollowInfoAsync); assertThat(followInfoResponse.getInfos().size(), equalTo(1)); assertThat(followInfoResponse.getInfos().get(0).getFollowerIndex(), equalTo("follower")); assertThat(followInfoResponse.getInfos().get(0).getLeaderIndex(), equalTo("leader")); @@ -179,8 +185,8 @@ public void testIndexFollowing() throws Exception { // Need to close index prior to unfollowing it: CloseIndexRequest closeIndexRequest = new CloseIndexRequest("follower"); - org.elasticsearch.action.support.master.AcknowledgedResponse closeIndexReponse = - highLevelClient().indices().close(closeIndexRequest, RequestOptions.DEFAULT); + org.elasticsearch.action.support.master.AcknowledgedResponse closeIndexReponse = highLevelClient().indices() + .close(closeIndexRequest, RequestOptions.DEFAULT); assertThat(closeIndexReponse.isAcknowledged(), is(true)); UnfollowRequest unfollowRequest = new UnfollowRequest("follower"); @@ -217,10 +223,18 @@ public void testForgetFollower() throws IOException { AcknowledgedResponse pauseFollowResponse = execute(pauseFollowRequest, ccrClient::pauseFollow, ccrClient::pauseFollowAsync); assertTrue(pauseFollowResponse.isAcknowledged()); - final ForgetFollowerRequest forgetFollowerRequest = - new ForgetFollowerRequest(clusterName, "follower", followerIndexUUID, "local_cluster", "leader"); - final BroadcastResponse forgetFollowerResponse = - execute(forgetFollowerRequest, ccrClient::forgetFollower, ccrClient::forgetFollowerAsync); + final ForgetFollowerRequest forgetFollowerRequest = new ForgetFollowerRequest( + clusterName, + "follower", + followerIndexUUID, + "local_cluster", + "leader" + ); + final BroadcastResponse forgetFollowerResponse = execute( + forgetFollowerRequest, + ccrClient::forgetFollower, + ccrClient::forgetFollowerAsync + ); assertThat(forgetFollowerResponse.shards().total(), equalTo(numberOfShards)); assertThat(forgetFollowerResponse.shards().successful(), equalTo(numberOfShards)); assertThat(forgetFollowerResponse.shards().skipped(), equalTo(0)); @@ -246,22 +260,26 @@ public void testForgetFollower() throws IOException { public void testAutoFollowing() throws Exception { CcrClient ccrClient = highLevelClient().ccr(); - PutAutoFollowPatternRequest putAutoFollowPatternRequest = new PutAutoFollowPatternRequest("pattern1", - "local_cluster", - Collections.singletonList("logs-*"), - Collections.singletonList("logs-excluded")); + PutAutoFollowPatternRequest putAutoFollowPatternRequest = new PutAutoFollowPatternRequest( + "pattern1", + "local_cluster", + Collections.singletonList("logs-*"), + Collections.singletonList("logs-excluded") + ); putAutoFollowPatternRequest.setFollowIndexNamePattern("copy-{{leader_index}}"); final int followerNumberOfReplicas = randomIntBetween(0, 4); - final Settings autoFollowerPatternSettings = - Settings.builder().put("index.number_of_replicas", followerNumberOfReplicas).build(); + final Settings autoFollowerPatternSettings = Settings.builder().put("index.number_of_replicas", followerNumberOfReplicas).build(); putAutoFollowPatternRequest.setSettings(autoFollowerPatternSettings); - AcknowledgedResponse putAutoFollowPatternResponse = - execute(putAutoFollowPatternRequest, ccrClient::putAutoFollowPattern, ccrClient::putAutoFollowPatternAsync); + AcknowledgedResponse putAutoFollowPatternResponse = execute( + putAutoFollowPatternRequest, + ccrClient::putAutoFollowPattern, + ccrClient::putAutoFollowPatternAsync + ); assertThat(putAutoFollowPatternResponse.isAcknowledged(), is(true)); CreateIndexRequest createExcludedIndexRequest = new CreateIndexRequest("logs-excluded"); - CreateIndexResponse createExcludedIndexResponse = - highLevelClient().indices().create(createExcludedIndexRequest, RequestOptions.DEFAULT); + CreateIndexResponse createExcludedIndexResponse = highLevelClient().indices() + .create(createExcludedIndexRequest, RequestOptions.DEFAULT); assertThat(createExcludedIndexResponse.isAcknowledged(), is(true)); CreateIndexRequest createIndexRequest = new CreateIndexRequest("logs-20200101"); @@ -278,13 +296,18 @@ public void testAutoFollowing() throws Exception { assertThat(indexExists("copy-logs-20200101"), is(true)); assertThat( getIndexSettingsAsMap("copy-logs-20200101"), - hasEntry("index.number_of_replicas", Integer.toString(followerNumberOfReplicas))); + hasEntry("index.number_of_replicas", Integer.toString(followerNumberOfReplicas)) + ); assertThat(indexExists("copy-logs-excluded"), is(false)); - GetAutoFollowPatternRequest getAutoFollowPatternRequest = - randomBoolean() ? new GetAutoFollowPatternRequest("pattern1") : new GetAutoFollowPatternRequest(); - GetAutoFollowPatternResponse getAutoFollowPatternResponse = - execute(getAutoFollowPatternRequest, ccrClient::getAutoFollowPattern, ccrClient::getAutoFollowPatternAsync); + GetAutoFollowPatternRequest getAutoFollowPatternRequest = randomBoolean() + ? new GetAutoFollowPatternRequest("pattern1") + : new GetAutoFollowPatternRequest(); + GetAutoFollowPatternResponse getAutoFollowPatternResponse = execute( + getAutoFollowPatternRequest, + ccrClient::getAutoFollowPattern, + ccrClient::getAutoFollowPatternAsync + ); assertThat(getAutoFollowPatternResponse.getPatterns().size(), equalTo(1)); GetAutoFollowPatternResponse.Pattern pattern = getAutoFollowPatternResponse.getPatterns().get("pattern1"); assertThat(pattern, notNullValue()); @@ -296,8 +319,11 @@ public void testAutoFollowing() throws Exception { // Cleanup: final DeleteAutoFollowPatternRequest deleteAutoFollowPatternRequest = new DeleteAutoFollowPatternRequest("pattern1"); - AcknowledgedResponse deleteAutoFollowPatternResponse = - execute(deleteAutoFollowPatternRequest, ccrClient::deleteAutoFollowPattern, ccrClient::deleteAutoFollowPatternAsync); + AcknowledgedResponse deleteAutoFollowPatternResponse = execute( + deleteAutoFollowPatternRequest, + ccrClient::deleteAutoFollowPattern, + ccrClient::deleteAutoFollowPatternAsync + ); assertThat(deleteAutoFollowPatternResponse.isAcknowledged(), is(true)); PauseFollowRequest pauseFollowRequest = new PauseFollowRequest("copy-logs-20200101"); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/CcrRequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/CcrRequestConvertersTests.java index d35038cac266e..4eb411a5ee1a5 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/CcrRequestConvertersTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/CcrRequestConvertersTests.java @@ -42,7 +42,10 @@ public class CcrRequestConvertersTests extends ESTestCase { public void testPutFollow() throws Exception { - PutFollowRequest putFollowRequest = new PutFollowRequest(randomAlphaOfLength(4), randomAlphaOfLength(4), randomAlphaOfLength(4), + PutFollowRequest putFollowRequest = new PutFollowRequest( + randomAlphaOfLength(4), + randomAlphaOfLength(4), + randomAlphaOfLength(4), randomBoolean() ? randomFrom(ActiveShardCount.NONE, ActiveShardCount.ONE, ActiveShardCount.DEFAULT, ActiveShardCount.ALL) : null ); randomizeRequest(putFollowRequest); @@ -87,11 +90,12 @@ public void testUnfollow() { public void testForgetFollower() throws IOException { final ForgetFollowerRequest request = new ForgetFollowerRequest( - randomAlphaOfLength(8), - randomAlphaOfLength(8), - randomAlphaOfLength(8), - randomAlphaOfLength(8), - randomAlphaOfLength(8)); + randomAlphaOfLength(8), + randomAlphaOfLength(8), + randomAlphaOfLength(8), + randomAlphaOfLength(8), + randomAlphaOfLength(8) + ); final Request convertedRequest = CcrRequestConverters.forgetFollower(request); assertThat(convertedRequest.getMethod(), equalTo(HttpPost.METHOD_NAME)); assertThat(convertedRequest.getEndpoint(), equalTo("/" + request.leaderIndex() + "/_ccr/forget_follower")); @@ -100,7 +104,8 @@ public void testForgetFollower() throws IOException { } public void testPutAutofollowPattern() throws Exception { - PutAutoFollowPatternRequest putAutoFollowPatternRequest = new PutAutoFollowPatternRequest(randomAlphaOfLength(4), + PutAutoFollowPatternRequest putAutoFollowPatternRequest = new PutAutoFollowPatternRequest( + randomAlphaOfLength(4), randomAlphaOfLength(4), Arrays.asList(generateRandomStringArray(4, 4, false)), Arrays.asList(generateRandomStringArray(4, 4, false)) diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ClusterClientIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ClusterClientIT.java index 480278db72917..2317590bccba4 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ClusterClientIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ClusterClientIT.java @@ -76,14 +76,20 @@ public void testClusterPutSettings() throws IOException { setRequest.persistentSettings(map); RequestOptions options = RequestOptions.DEFAULT.toBuilder().setWarningsHandler(WarningsHandler.PERMISSIVE).build(); - ClusterUpdateSettingsResponse setResponse = execute(setRequest, highLevelClient().cluster()::putSettings, - highLevelClient().cluster()::putSettingsAsync, options); + ClusterUpdateSettingsResponse setResponse = execute( + setRequest, + highLevelClient().cluster()::putSettings, + highLevelClient().cluster()::putSettingsAsync, + options + ); assertAcked(setResponse); assertThat(setResponse.getTransientSettings().get(transientSettingKey), notNullValue()); assertThat(setResponse.getTransientSettings().get(persistentSettingKey), nullValue()); - assertThat(setResponse.getTransientSettings().get(transientSettingKey), - equalTo(transientSettingValue + ByteSizeUnit.BYTES.getSuffix())); + assertThat( + setResponse.getTransientSettings().get(transientSettingKey), + equalTo(transientSettingValue + ByteSizeUnit.BYTES.getSuffix()) + ); assertThat(setResponse.getPersistentSettings().get(transientSettingKey), nullValue()); assertThat(setResponse.getPersistentSettings().get(persistentSettingKey), notNullValue()); assertThat(setResponse.getPersistentSettings().get(persistentSettingKey), equalTo(persistentSettingValue)); @@ -98,8 +104,12 @@ public void testClusterPutSettings() throws IOException { resetRequest.transientSettings(Settings.builder().putNull(transientSettingKey)); resetRequest.persistentSettings("{\"" + persistentSettingKey + "\": null }", XContentType.JSON); - ClusterUpdateSettingsResponse resetResponse = execute(resetRequest, highLevelClient().cluster()::putSettings, - highLevelClient().cluster()::putSettingsAsync, options); + ClusterUpdateSettingsResponse resetResponse = execute( + resetRequest, + highLevelClient().cluster()::putSettings, + highLevelClient().cluster()::putSettingsAsync, + options + ); assertThat(resetResponse.getTransientSettings().get(transientSettingKey), equalTo(null)); assertThat(resetResponse.getPersistentSettings().get(persistentSettingKey), equalTo(null)); @@ -123,18 +133,28 @@ public void testClusterUpdatePersistentSettingNonExistent() { private void testClusterUpdateSettingNonExistent( final BiConsumer consumer, - String label) { + String label + ) { String setting = "no_idea_what_you_are_talking_about"; int value = 10; ClusterUpdateSettingsRequest clusterUpdateSettingsRequest = new ClusterUpdateSettingsRequest(); consumer.accept(Settings.builder().put(setting, value), clusterUpdateSettingsRequest); - ElasticsearchException exception = expectThrows(ElasticsearchException.class, () -> execute(clusterUpdateSettingsRequest, - highLevelClient().cluster()::putSettings, highLevelClient().cluster()::putSettingsAsync)); + ElasticsearchException exception = expectThrows( + ElasticsearchException.class, + () -> execute( + clusterUpdateSettingsRequest, + highLevelClient().cluster()::putSettings, + highLevelClient().cluster()::putSettingsAsync + ) + ); assertThat(exception.status(), equalTo(RestStatus.BAD_REQUEST)); - assertThat(exception.getMessage(), equalTo( - "Elasticsearch exception [type=illegal_argument_exception, reason=" - + label + " setting [" + setting + "], not recognized]")); + assertThat( + exception.getMessage(), + equalTo( + "Elasticsearch exception [type=illegal_argument_exception, reason=" + label + " setting [" + setting + "], not recognized]" + ) + ); } public void testClusterGetSettings() throws IOException { @@ -144,14 +164,16 @@ public void testClusterGetSettings() throws IOException { final String persistentSettingKey = EnableAllocationDecider.CLUSTER_ROUTING_ALLOCATION_ENABLE_SETTING.getKey(); final String persistentSettingValue = EnableAllocationDecider.Allocation.NONE.name(); - Settings transientSettings = - Settings.builder().put(transientSettingKey, transientSettingValue, ByteSizeUnit.BYTES).build(); + Settings transientSettings = Settings.builder().put(transientSettingKey, transientSettingValue, ByteSizeUnit.BYTES).build(); Settings persistentSettings = Settings.builder().put(persistentSettingKey, persistentSettingValue).build(); clusterUpdateSettings(persistentSettings, transientSettings); ClusterGetSettingsRequest request = new ClusterGetSettingsRequest(); ClusterGetSettingsResponse response = execute( - request, highLevelClient().cluster()::getSettings, highLevelClient().cluster()::getSettingsAsync); + request, + highLevelClient().cluster()::getSettings, + highLevelClient().cluster()::getSettingsAsync + ); assertEquals(persistentSettings, response.getPersistentSettings()); assertEquals(transientSettings, response.getTransientSettings()); assertEquals(0, response.getDefaultSettings().size()); @@ -164,14 +186,16 @@ public void testClusterGetSettingsWithDefault() throws IOException { final String persistentSettingKey = EnableAllocationDecider.CLUSTER_ROUTING_ALLOCATION_ENABLE_SETTING.getKey(); final String persistentSettingValue = EnableAllocationDecider.Allocation.NONE.name(); - Settings transientSettings = - Settings.builder().put(transientSettingKey, transientSettingValue, ByteSizeUnit.BYTES).build(); + Settings transientSettings = Settings.builder().put(transientSettingKey, transientSettingValue, ByteSizeUnit.BYTES).build(); Settings persistentSettings = Settings.builder().put(persistentSettingKey, persistentSettingValue).build(); clusterUpdateSettings(persistentSettings, transientSettings); ClusterGetSettingsRequest request = new ClusterGetSettingsRequest().includeDefaults(true); ClusterGetSettingsResponse response = execute( - request, highLevelClient().cluster()::getSettings, highLevelClient().cluster()::getSettingsAsync); + request, + highLevelClient().cluster()::getSettings, + highLevelClient().cluster()::getSettingsAsync + ); assertEquals(persistentSettings, response.getPersistentSettings()); assertEquals(transientSettings, response.getTransientSettings()); assertThat(response.getDefaultSettings().size(), greaterThan(0)); @@ -195,8 +219,7 @@ public void testClusterHealthYellowClusterLevel() throws IOException { request.timeout("5s"); ClusterHealthResponse response = execute(request, highLevelClient().cluster()::health, highLevelClient().cluster()::healthAsync); - logger.info("Shard stats\n{}", EntityUtils.toString( - client().performRequest(new Request("GET", "/_cat/shards")).getEntity())); + logger.info("Shard stats\n{}", EntityUtils.toString(client().performRequest(new Request("GET", "/_cat/shards")).getEntity())); assertThat(response.getIndices().size(), equalTo(0)); } @@ -216,8 +239,7 @@ public void testClusterHealthYellowIndicesLevel() throws IOException { request.level(ClusterHealthRequest.Level.INDICES); ClusterHealthResponse response = execute(request, highLevelClient().cluster()::health, highLevelClient().cluster()::healthAsync); - logger.info("Shard stats\n{}", EntityUtils.toString( - client().performRequest(new Request("GET", "/_cat/shards")).getEntity())); + logger.info("Shard stats\n{}", EntityUtils.toString(client().performRequest(new Request("GET", "/_cat/shards")).getEntity())); assertYellowShards(response); assertThat(response.getIndices().size(), equalTo(2)); for (Map.Entry entry : response.getIndices().entrySet()) { @@ -265,14 +287,14 @@ public void testClusterHealthYellowSpecificIndex() throws IOException { private static void assertYellowIndex(String indexName, ClusterIndexHealth indexHealth, boolean emptyShards) { assertThat(indexHealth, notNullValue()); - assertThat(indexHealth.getIndex(),equalTo(indexName)); - assertThat(indexHealth.getActivePrimaryShards(),equalTo(1)); - assertThat(indexHealth.getActiveShards(),equalTo(1)); - assertThat(indexHealth.getNumberOfReplicas(),equalTo(1)); - assertThat(indexHealth.getInitializingShards(),equalTo(0)); - assertThat(indexHealth.getUnassignedShards(),equalTo(1)); - assertThat(indexHealth.getRelocatingShards(),equalTo(0)); - assertThat(indexHealth.getStatus(),equalTo(ClusterHealthStatus.YELLOW)); + assertThat(indexHealth.getIndex(), equalTo(indexName)); + assertThat(indexHealth.getActivePrimaryShards(), equalTo(1)); + assertThat(indexHealth.getActiveShards(), equalTo(1)); + assertThat(indexHealth.getNumberOfReplicas(), equalTo(1)); + assertThat(indexHealth.getInitializingShards(), equalTo(0)); + assertThat(indexHealth.getUnassignedShards(), equalTo(1)); + assertThat(indexHealth.getRelocatingShards(), equalTo(0)); + assertThat(indexHealth.getStatus(), equalTo(ClusterHealthStatus.YELLOW)); if (emptyShards) { assertThat(indexHealth.getShards().size(), equalTo(0)); } else { @@ -315,9 +337,11 @@ public void testClusterHealthNotFoundIndex() throws IOException { assertThat(response.status(), equalTo(RestStatus.REQUEST_TIMEOUT)); assertThat(response.getStatus(), equalTo(ClusterHealthStatus.RED)); assertNoIndices(response); - assertWarnings("The HTTP status code for a cluster health timeout will be changed from 408 to 200 in a " + - "future version. Set the [return_200_for_cluster_health_timeout] query parameter to [true] to suppress this message and " + - "opt in to the future behaviour now."); + assertWarnings( + "The HTTP status code for a cluster health timeout will be changed from 408 to 200 in a " + + "future version. Set the [return_200_for_cluster_health_timeout] query parameter to [true] to suppress this message and " + + "opt in to the future behaviour now." + ); } public void testRemoteInfo() throws Exception { @@ -328,20 +352,21 @@ public void testRemoteInfo() throws Exception { settingsRequest.includeDefaults(true); ClusterGetSettingsResponse settingsResponse = highLevelClient().cluster().getSettings(settingsRequest, RequestOptions.DEFAULT); - List seeds = SniffConnectionStrategy.REMOTE_CLUSTER_SEEDS - .getConcreteSettingForNamespace(clusterAlias) - .get(settingsResponse.getPersistentSettings()); - int connectionsPerCluster = SniffConnectionStrategy.REMOTE_CONNECTIONS_PER_CLUSTER + List seeds = SniffConnectionStrategy.REMOTE_CLUSTER_SEEDS.getConcreteSettingForNamespace(clusterAlias) .get(settingsResponse.getPersistentSettings()); - TimeValue initialConnectionTimeout = RemoteClusterService.REMOTE_INITIAL_CONNECTION_TIMEOUT_SETTING - .get(settingsResponse.getPersistentSettings()); - boolean skipUnavailable = RemoteClusterService.REMOTE_CLUSTER_SKIP_UNAVAILABLE - .getConcreteSettingForNamespace(clusterAlias) + int connectionsPerCluster = SniffConnectionStrategy.REMOTE_CONNECTIONS_PER_CLUSTER.get(settingsResponse.getPersistentSettings()); + TimeValue initialConnectionTimeout = RemoteClusterService.REMOTE_INITIAL_CONNECTION_TIMEOUT_SETTING.get( + settingsResponse.getPersistentSettings() + ); + boolean skipUnavailable = RemoteClusterService.REMOTE_CLUSTER_SKIP_UNAVAILABLE.getConcreteSettingForNamespace(clusterAlias) .get(settingsResponse.getPersistentSettings()); RemoteInfoRequest request = new RemoteInfoRequest(); - RemoteInfoResponse response = execute(request, highLevelClient().cluster()::remoteInfo, - highLevelClient().cluster()::remoteInfoAsync); + RemoteInfoResponse response = execute( + request, + highLevelClient().cluster()::remoteInfo, + highLevelClient().cluster()::remoteInfoAsync + ); assertThat(response, notNullValue()); assertThat(response.getInfos().size(), equalTo(1)); @@ -364,40 +389,61 @@ public void testComponentTemplates() throws Exception { AliasMetadata alias = AliasMetadata.builder("alias").writeIndex(true).build(); Template template = new Template(settings, mappings, Map.of("alias", alias)); ComponentTemplate componentTemplate = new ComponentTemplate(template, 1L, new HashMap<>()); - PutComponentTemplateRequest putComponentTemplateRequest = - new PutComponentTemplateRequest().name(templateName).create(true).componentTemplate(componentTemplate); - - AcknowledgedResponse response = execute(putComponentTemplateRequest, - highLevelClient().cluster()::putComponentTemplate, highLevelClient().cluster()::putComponentTemplateAsync); + PutComponentTemplateRequest putComponentTemplateRequest = new PutComponentTemplateRequest().name(templateName) + .create(true) + .componentTemplate(componentTemplate); + + AcknowledgedResponse response = execute( + putComponentTemplateRequest, + highLevelClient().cluster()::putComponentTemplate, + highLevelClient().cluster()::putComponentTemplateAsync + ); assertThat(response.isAcknowledged(), equalTo(true)); ComponentTemplatesExistRequest componentTemplatesExistRequest = new ComponentTemplatesExistRequest(templateName); - boolean exist = execute(componentTemplatesExistRequest, - highLevelClient().cluster()::existsComponentTemplate, highLevelClient().cluster()::existsComponentTemplateAsync); + boolean exist = execute( + componentTemplatesExistRequest, + highLevelClient().cluster()::existsComponentTemplate, + highLevelClient().cluster()::existsComponentTemplateAsync + ); assertTrue(exist); GetComponentTemplatesRequest getComponentTemplatesRequest = new GetComponentTemplatesRequest(templateName); - GetComponentTemplatesResponse getResponse = execute(getComponentTemplatesRequest, - highLevelClient().cluster()::getComponentTemplate, highLevelClient().cluster()::getComponentTemplateAsync); + GetComponentTemplatesResponse getResponse = execute( + getComponentTemplatesRequest, + highLevelClient().cluster()::getComponentTemplate, + highLevelClient().cluster()::getComponentTemplateAsync + ); assertThat(getResponse.getComponentTemplates().size(), equalTo(1)); assertThat(getResponse.getComponentTemplates().containsKey(templateName), equalTo(true)); assertThat(getResponse.getComponentTemplates().get(templateName), equalTo(componentTemplate)); DeleteComponentTemplateRequest deleteComponentTemplateRequest = new DeleteComponentTemplateRequest(templateName); - response = execute(deleteComponentTemplateRequest, highLevelClient().cluster()::deleteComponentTemplate, - highLevelClient().cluster()::deleteComponentTemplateAsync); + response = execute( + deleteComponentTemplateRequest, + highLevelClient().cluster()::deleteComponentTemplate, + highLevelClient().cluster()::deleteComponentTemplateAsync + ); assertThat(response.isAcknowledged(), equalTo(true)); - ElasticsearchStatusException statusException = expectThrows(ElasticsearchStatusException.class, - () -> execute(getComponentTemplatesRequest, - highLevelClient().cluster()::getComponentTemplate, highLevelClient().cluster()::getComponentTemplateAsync)); + ElasticsearchStatusException statusException = expectThrows( + ElasticsearchStatusException.class, + () -> execute( + getComponentTemplatesRequest, + highLevelClient().cluster()::getComponentTemplate, + highLevelClient().cluster()::getComponentTemplateAsync + ) + ); assertThat(statusException.status(), equalTo(RestStatus.NOT_FOUND)); - exist = execute(componentTemplatesExistRequest, - highLevelClient().cluster()::existsComponentTemplate, highLevelClient().cluster()::existsComponentTemplateAsync); + exist = execute( + componentTemplatesExistRequest, + highLevelClient().cluster()::existsComponentTemplate, + highLevelClient().cluster()::existsComponentTemplateAsync + ); assertFalse(exist); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java index c7deac68438cf..cbda7f0aaf36c 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java @@ -39,15 +39,15 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.VersionType; import org.elasticsearch.index.get.GetResult; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptType; import org.elasticsearch.search.fetch.subphase.FetchSourceContext; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentType; import java.io.IOException; import java.time.LocalTime; @@ -75,7 +75,9 @@ public void testDelete() throws IOException { // Testing deletion String docId = "id"; IndexResponse indexResponse = highLevelClient().index( - new IndexRequest("index").id(docId).source(Collections.singletonMap("foo", "bar")), RequestOptions.DEFAULT); + new IndexRequest("index").id(docId).source(Collections.singletonMap("foo", "bar")), + RequestOptions.DEFAULT + ); assertThat(indexResponse.getSeqNo(), greaterThanOrEqualTo(0L)); DeleteRequest deleteRequest = new DeleteRequest("index", docId); if (randomBoolean()) { @@ -100,23 +102,35 @@ public void testDelete() throws IOException { // Testing version conflict String docId = "version_conflict"; highLevelClient().index( - new IndexRequest("index").id( docId).source(Collections.singletonMap("foo", "bar")), RequestOptions.DEFAULT); + new IndexRequest("index").id(docId).source(Collections.singletonMap("foo", "bar")), + RequestOptions.DEFAULT + ); DeleteRequest deleteRequest = new DeleteRequest("index", docId).setIfSeqNo(2).setIfPrimaryTerm(2); - ElasticsearchException exception = expectThrows(ElasticsearchException.class, - () -> execute(deleteRequest, highLevelClient()::delete, highLevelClient()::deleteAsync)); + ElasticsearchException exception = expectThrows( + ElasticsearchException.class, + () -> execute(deleteRequest, highLevelClient()::delete, highLevelClient()::deleteAsync) + ); assertEquals(RestStatus.CONFLICT, exception.status()); - assertEquals("Elasticsearch exception [type=version_conflict_engine_exception, reason=[" + docId + "]: " + - "version conflict, required seqNo [2], primary term [2]. current document has seqNo [3] and primary term [1]]", - exception.getMessage()); + assertEquals( + "Elasticsearch exception [type=version_conflict_engine_exception, reason=[" + + docId + + "]: " + + "version conflict, required seqNo [2], primary term [2]. current document has seqNo [3] and primary term [1]]", + exception.getMessage() + ); assertEquals("index", exception.getMetadata("es.index").get(0)); } { // Testing version type String docId = "version_type"; highLevelClient().index( - new IndexRequest("index").id(docId).source(Collections.singletonMap("foo", "bar")) - .versionType(VersionType.EXTERNAL).version(12), RequestOptions.DEFAULT); - DeleteRequest deleteRequest = new DeleteRequest("index", docId).versionType(VersionType.EXTERNAL).version(13); + new IndexRequest("index").id(docId) + .source(Collections.singletonMap("foo", "bar")) + .versionType(VersionType.EXTERNAL) + .version(12), + RequestOptions.DEFAULT + ); + DeleteRequest deleteRequest = new DeleteRequest("index", docId).versionType(VersionType.EXTERNAL).version(13); DeleteResponse deleteResponse = execute(deleteRequest, highLevelClient()::delete, highLevelClient()::deleteAsync); assertEquals("index", deleteResponse.getIndex()); assertEquals(docId, deleteResponse.getId()); @@ -126,23 +140,33 @@ public void testDelete() throws IOException { // Testing version type with a wrong version String docId = "wrong_version"; highLevelClient().index( - new IndexRequest("index").id(docId).source(Collections.singletonMap("foo", "bar")) - .versionType(VersionType.EXTERNAL).version(12), RequestOptions.DEFAULT); + new IndexRequest("index").id(docId) + .source(Collections.singletonMap("foo", "bar")) + .versionType(VersionType.EXTERNAL) + .version(12), + RequestOptions.DEFAULT + ); ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, () -> { - DeleteRequest deleteRequest = new DeleteRequest("index", docId).versionType(VersionType.EXTERNAL).version(10); + DeleteRequest deleteRequest = new DeleteRequest("index", docId).versionType(VersionType.EXTERNAL).version(10); execute(deleteRequest, highLevelClient()::delete, highLevelClient()::deleteAsync); }); assertEquals(RestStatus.CONFLICT, exception.status()); - assertEquals("Elasticsearch exception [type=version_conflict_engine_exception, reason=[" + - docId + "]: version conflict, current version [12] is higher or equal to the one provided [10]]", exception.getMessage()); + assertEquals( + "Elasticsearch exception [type=version_conflict_engine_exception, reason=[" + + docId + + "]: version conflict, current version [12] is higher or equal to the one provided [10]]", + exception.getMessage() + ); assertEquals("index", exception.getMetadata("es.index").get(0)); } { // Testing routing String docId = "routing"; - highLevelClient().index(new IndexRequest("index").id(docId).source(Collections.singletonMap("foo", "bar")).routing("foo"), - RequestOptions.DEFAULT); - DeleteRequest deleteRequest = new DeleteRequest("index", docId).routing("foo"); + highLevelClient().index( + new IndexRequest("index").id(docId).source(Collections.singletonMap("foo", "bar")).routing("foo"), + RequestOptions.DEFAULT + ); + DeleteRequest deleteRequest = new DeleteRequest("index", docId).routing("foo"); DeleteResponse deleteResponse = execute(deleteRequest, highLevelClient()::delete, highLevelClient()::deleteAsync); assertEquals("index", deleteResponse.getIndex()); assertEquals(docId, deleteResponse.getId()); @@ -221,20 +245,16 @@ public void testSourceDoesNotExist() throws IOException { final String noSourceIndex = "no_source"; { // Prepare - Settings settings = Settings.builder() - .put("number_of_shards", 1) - .put("number_of_replicas", 0) - .build(); + Settings settings = Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0).build(); String mapping = "\"_source\": {\"enabled\": false}"; createIndex(noSourceIndex, settings, mapping); assertEquals( RestStatus.OK, highLevelClient().bulk( - new BulkRequest() - .add(new IndexRequest(noSourceIndex).id("1") - .source(Collections.singletonMap("foo", 1), XContentType.JSON)) - .add(new IndexRequest(noSourceIndex).id("2") - .source(Collections.singletonMap("foo", 2), XContentType.JSON)) + new BulkRequest().add( + new IndexRequest(noSourceIndex).id("1").source(Collections.singletonMap("foo", 1), XContentType.JSON) + ) + .add(new IndexRequest(noSourceIndex).id("2").source(Collections.singletonMap("foo", 2), XContentType.JSON)) .setRefreshPolicy(RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT ).status() @@ -254,8 +274,10 @@ public void testSourceDoesNotExist() throws IOException { public void testGet() throws IOException { { GetRequest getRequest = new GetRequest("index", "id"); - ElasticsearchException exception = expectThrows(ElasticsearchException.class, - () -> execute(getRequest, highLevelClient()::get, highLevelClient()::getAsync)); + ElasticsearchException exception = expectThrows( + ElasticsearchException.class, + () -> execute(getRequest, highLevelClient()::get, highLevelClient()::getAsync) + ); assertEquals(RestStatus.NOT_FOUND, exception.status()); assertEquals("Elasticsearch exception [type=index_not_found_exception, reason=no such index [index]]", exception.getMessage()); assertEquals("index", exception.getMetadata("es.index").get(0)); @@ -267,11 +289,17 @@ public void testGet() throws IOException { highLevelClient().index(index, RequestOptions.DEFAULT); { GetRequest getRequest = new GetRequest("index", "id").version(2); - ElasticsearchException exception = expectThrows(ElasticsearchException.class, - () -> execute(getRequest, highLevelClient()::get, highLevelClient()::getAsync)); + ElasticsearchException exception = expectThrows( + ElasticsearchException.class, + () -> execute(getRequest, highLevelClient()::get, highLevelClient()::getAsync) + ); assertEquals(RestStatus.CONFLICT, exception.status()); - assertEquals("Elasticsearch exception [type=version_conflict_engine_exception, " + "reason=[id]: " + - "version conflict, current version [1] is different than the one provided [2]]", exception.getMessage()); + assertEquals( + "Elasticsearch exception [type=version_conflict_engine_exception, " + + "reason=[id]: " + + "version conflict, current version [1] is different than the one provided [2]]", + exception.getMessage() + ); assertEquals("index", exception.getMetadata("es.index").get(0)); } { @@ -311,9 +339,9 @@ public void testGet() throws IOException { { GetRequest getRequest = new GetRequest("index", "id"); if (randomBoolean()) { - getRequest.fetchSourceContext(new FetchSourceContext(true, new String[]{"field1"}, Strings.EMPTY_ARRAY)); + getRequest.fetchSourceContext(new FetchSourceContext(true, new String[] { "field1" }, Strings.EMPTY_ARRAY)); } else { - getRequest.fetchSourceContext(new FetchSourceContext(true, Strings.EMPTY_ARRAY, new String[]{"field2"})); + getRequest.fetchSourceContext(new FetchSourceContext(true, Strings.EMPTY_ARRAY, new String[] { "field2" })); } GetResponse getResponse = execute(getRequest, highLevelClient()::get, highLevelClient()::getAsync); assertEquals("index", getResponse.getIndex()); @@ -339,15 +367,19 @@ public void testMultiGet() throws IOException { assertNull(response.getResponses()[0].getResponse()); assertEquals("id1", response.getResponses()[0].getFailure().getId()); assertEquals("index", response.getResponses()[0].getFailure().getIndex()); - assertEquals("Elasticsearch exception [type=index_not_found_exception, reason=no such index [index]]", - response.getResponses()[0].getFailure().getFailure().getMessage()); + assertEquals( + "Elasticsearch exception [type=index_not_found_exception, reason=no such index [index]]", + response.getResponses()[0].getFailure().getFailure().getMessage() + ); assertTrue(response.getResponses()[1].isFailed()); assertNull(response.getResponses()[1].getResponse()); assertEquals("id2", response.getResponses()[1].getId()); assertEquals("index", response.getResponses()[1].getIndex()); - assertEquals("Elasticsearch exception [type=index_not_found_exception, reason=no such index [index]]", - response.getResponses()[1].getFailure().getFailure().getMessage()); + assertEquals( + "Elasticsearch exception [type=index_not_found_exception, reason=no such index [index]]", + response.getResponses()[1].getFailure().getFailure().getMessage() + ); } BulkRequest bulk = new BulkRequest(); bulk.setRefreshPolicy(RefreshPolicy.IMMEDIATE); @@ -382,8 +414,10 @@ public void testMultiGet() throws IOException { public void testGetSource() throws IOException { { GetSourceRequest getRequest = new GetSourceRequest("index", "id"); - ElasticsearchException exception = expectThrows(ElasticsearchException.class, - () -> execute(getRequest, highLevelClient()::getSource, highLevelClient()::getSourceAsync)); + ElasticsearchException exception = expectThrows( + ElasticsearchException.class, + () -> execute(getRequest, highLevelClient()::getSource, highLevelClient()::getSourceAsync) + ); assertEquals(RestStatus.NOT_FOUND, exception.status()); assertEquals("Elasticsearch exception [type=index_not_found_exception, reason=no such index [index]]", exception.getMessage()); assertEquals("index", exception.getMetadata("es.index").get(0)); @@ -403,11 +437,15 @@ public void testGetSource() throws IOException { } { GetSourceRequest getRequest = new GetSourceRequest("index", "does_not_exist"); - ElasticsearchException exception = expectThrows(ElasticsearchException.class, - () -> execute(getRequest, highLevelClient()::getSource, highLevelClient()::getSourceAsync)); + ElasticsearchException exception = expectThrows( + ElasticsearchException.class, + () -> execute(getRequest, highLevelClient()::getSource, highLevelClient()::getSourceAsync) + ); assertEquals(RestStatus.NOT_FOUND, exception.status()); - assertEquals("Elasticsearch exception [type=resource_not_found_exception, " + - "reason=Document not found [index]/[does_not_exist]]", exception.getMessage()); + assertEquals( + "Elasticsearch exception [type=resource_not_found_exception, " + "reason=Document not found [index]/[does_not_exist]]", + exception.getMessage() + ); } { GetSourceRequest getRequest = new GetSourceRequest("index", "id"); @@ -420,7 +458,7 @@ public void testGetSource() throws IOException { } { GetSourceRequest getRequest = new GetSourceRequest("index", "id"); - getRequest.fetchSourceContext(new FetchSourceContext(true, new String[]{"field1"}, Strings.EMPTY_ARRAY)); + getRequest.fetchSourceContext(new FetchSourceContext(true, new String[] { "field1" }, Strings.EMPTY_ARRAY)); GetSourceResponse response = execute(getRequest, highLevelClient()::getSource, highLevelClient()::getSourceAsync); Map expectedResponse = new HashMap<>(); expectedResponse.put("field1", "value1"); @@ -428,7 +466,7 @@ public void testGetSource() throws IOException { } { GetSourceRequest getRequest = new GetSourceRequest("index", "id"); - getRequest.fetchSourceContext(new FetchSourceContext(true, Strings.EMPTY_ARRAY, new String[]{"field1"})); + getRequest.fetchSourceContext(new FetchSourceContext(true, Strings.EMPTY_ARRAY, new String[] { "field1" })); GetSourceResponse response = execute(getRequest, highLevelClient()::getSource, highLevelClient()::getSourceAsync); Map expectedResponse = new HashMap<>(); expectedResponse.put("field2", "value2"); @@ -437,10 +475,15 @@ public void testGetSource() throws IOException { { GetSourceRequest getRequest = new GetSourceRequest("index", "id"); getRequest.fetchSourceContext(new FetchSourceContext(false)); - ElasticsearchException exception = expectThrows(ElasticsearchException.class, - () -> execute(getRequest, highLevelClient()::getSource, highLevelClient()::getSourceAsync)); - assertEquals("Elasticsearch exception [type=action_request_validation_exception, " + - "reason=Validation Failed: 1: fetching source can not be disabled;]", exception.getMessage()); + ElasticsearchException exception = expectThrows( + ElasticsearchException.class, + () -> execute(getRequest, highLevelClient()::getSource, highLevelClient()::getSourceAsync) + ); + assertEquals( + "Elasticsearch exception [type=action_request_validation_exception, " + + "reason=Validation Failed: 1: fetching source can not be disabled;]", + exception.getMessage() + ); } } @@ -493,9 +536,11 @@ public void testIndex() throws IOException { execute(wrongRequest, highLevelClient()::index, highLevelClient()::indexAsync); }); assertEquals(RestStatus.CONFLICT, exception.status()); - assertEquals("Elasticsearch exception [type=version_conflict_engine_exception, reason=[id]: " + - "version conflict, required seqNo [1], primary term [5]. current document has seqNo [2] and primary term [1]]", - exception.getMessage()); + assertEquals( + "Elasticsearch exception [type=version_conflict_engine_exception, reason=[id]: " + + "version conflict, required seqNo [1], primary term [5]. current document has seqNo [2] and primary term [1]]", + exception.getMessage() + ); assertEquals("index", exception.getMetadata("es.index").get(0)); } { @@ -508,8 +553,10 @@ public void testIndex() throws IOException { }); assertEquals(RestStatus.BAD_REQUEST, exception.status()); - assertEquals("Elasticsearch exception [type=illegal_argument_exception, " + - "reason=pipeline with id [missing] does not exist]", exception.getMessage()); + assertEquals( + "Elasticsearch exception [type=illegal_argument_exception, " + "reason=pipeline with id [missing] does not exist]", + exception.getMessage() + ); } { IndexRequest indexRequest = new IndexRequest("index").id("external_version_type"); @@ -533,13 +580,17 @@ public void testIndex() throws IOException { assertEquals("index", indexResponse.getIndex()); assertEquals("with_create_op_type", indexResponse.getId()); - ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, () -> { - execute(indexRequest, highLevelClient()::index, highLevelClient()::indexAsync); - }); + ElasticsearchStatusException exception = expectThrows( + ElasticsearchStatusException.class, + () -> { execute(indexRequest, highLevelClient()::index, highLevelClient()::indexAsync); } + ); assertEquals(RestStatus.CONFLICT, exception.status()); - assertEquals("Elasticsearch exception [type=version_conflict_engine_exception, reason=[with_create_op_type]: " + - "version conflict, document already exists (current version [1])]", exception.getMessage()); + assertEquals( + "Elasticsearch exception [type=version_conflict_engine_exception, reason=[with_create_op_type]: " + + "version conflict, document already exists (current version [1])]", + exception.getMessage() + ); } { ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, () -> { @@ -551,8 +602,11 @@ public void testIndex() throws IOException { }); assertEquals(RestStatus.NOT_FOUND, exception.status()); - assertEquals("Elasticsearch exception [type=index_not_found_exception, reason=no such index [index] and [require_alias]" + - " request flag is [true] and [index] is not an alias]", exception.getMessage()); + assertEquals( + "Elasticsearch exception [type=index_not_found_exception, reason=no such index [index] and [require_alias]" + + " request flag is [true] and [index] is not an alias]", + exception.getMessage() + ); } } @@ -561,19 +615,22 @@ public void testUpdate() throws IOException { UpdateRequest updateRequest = new UpdateRequest("index", "does_not_exist"); updateRequest.doc(singletonMap("field", "value"), randomFrom(XContentType.values())); - ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, () -> - execute(updateRequest, highLevelClient()::update, highLevelClient()::updateAsync)); + ElasticsearchStatusException exception = expectThrows( + ElasticsearchStatusException.class, + () -> execute(updateRequest, highLevelClient()::update, highLevelClient()::updateAsync) + ); assertEquals(RestStatus.NOT_FOUND, exception.status()); - assertEquals("Elasticsearch exception [type=document_missing_exception, reason=[does_not_exist]: document missing]", - exception.getMessage()); + assertEquals( + "Elasticsearch exception [type=document_missing_exception, reason=[does_not_exist]: document missing]", + exception.getMessage() + ); } { - IndexRequest indexRequest = new IndexRequest("index").id( "id"); + IndexRequest indexRequest = new IndexRequest("index").id("id"); indexRequest.source(singletonMap("field", "value")); IndexResponse indexResponse = highLevelClient().index(indexRequest, RequestOptions.DEFAULT); assertEquals(RestStatus.CREATED, indexResponse.status()); - long lastUpdateSeqNo; long lastUpdatePrimaryTerm; { @@ -598,9 +655,11 @@ public void testUpdate() throws IOException { updateRequest.setIfSeqNo(lastUpdateSeqNo + (randomBoolean() ? 0 : 1)); updateRequest.setIfPrimaryTerm(lastUpdatePrimaryTerm + 1); } - ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, () -> - execute(updateRequest, highLevelClient()::update, highLevelClient()::updateAsync)); - assertEquals(exception.toString(),RestStatus.CONFLICT, exception.status()); + ElasticsearchStatusException exception = expectThrows( + ElasticsearchStatusException.class, + () -> execute(updateRequest, highLevelClient()::update, highLevelClient()::updateAsync) + ); + assertEquals(exception.toString(), RestStatus.CONFLICT, exception.status()); assertThat(exception.getMessage(), containsString("Elasticsearch exception [type=version_conflict_engine_exception")); } { @@ -729,8 +788,10 @@ public void testUpdate() throws IOException { updateRequest.upsert(new IndexRequest().source(Collections.singletonMap("field", "upsert"), XContentType.YAML)); execute(updateRequest, highLevelClient()::update, highLevelClient()::updateAsync); }); - assertEquals("Update request cannot have different content types for doc [JSON] and upsert [YAML] documents", - exception.getMessage()); + assertEquals( + "Update request cannot have different content types for doc [JSON] and upsert [YAML] documents", + exception.getMessage() + ); } { ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, () -> { @@ -740,8 +801,11 @@ public void testUpdate() throws IOException { execute(updateRequest, highLevelClient()::update, highLevelClient()::updateAsync); }); assertEquals(RestStatus.NOT_FOUND, exception.status()); - assertEquals("Elasticsearch exception [type=index_not_found_exception, reason=no such index [index] and [require_alias]" + - " request flag is [true] and [index] is not an alias]", exception.getMessage()); + assertEquals( + "Elasticsearch exception [type=index_not_found_exception, reason=no such index [index] and [require_alias]" + + " request flag is [true] and [index] is not an alias]", + exception.getMessage() + ); } } @@ -760,16 +824,18 @@ public void testBulk() throws IOException { DocWriteRequest.OpType opType = randomFrom(DocWriteRequest.OpType.values()); if (opType == DocWriteRequest.OpType.DELETE) { if (erroneous == false) { - assertEquals(RestStatus.CREATED, - highLevelClient().index( - new IndexRequest("index").id(id).source("field", -1), RequestOptions.DEFAULT).status()); + assertEquals( + RestStatus.CREATED, + highLevelClient().index(new IndexRequest("index").id(id).source("field", -1), RequestOptions.DEFAULT).status() + ); } DeleteRequest deleteRequest = new DeleteRequest("index", id); bulkRequest.add(deleteRequest); } else { - BytesReference source = BytesReference.bytes(XContentBuilder.builder(xContentType.xContent()) - .startObject().field("id", i).endObject()); + BytesReference source = BytesReference.bytes( + XContentBuilder.builder(xContentType.xContent()).startObject().field("id", i).endObject() + ); if (opType == DocWriteRequest.OpType.INDEX) { IndexRequest indexRequest = new IndexRequest("index").id(id).source(source, xContentType); if (erroneous) { @@ -786,12 +852,12 @@ public void testBulk() throws IOException { bulkRequest.add(createRequest); } else if (opType == DocWriteRequest.OpType.UPDATE) { - UpdateRequest updateRequest = new UpdateRequest("index", id) - .doc(new IndexRequest().source(source, xContentType)); + UpdateRequest updateRequest = new UpdateRequest("index", id).doc(new IndexRequest().source(source, xContentType)); if (erroneous == false) { - assertEquals(RestStatus.CREATED, - highLevelClient().index( - new IndexRequest("index").id(id).source("field", -1), RequestOptions.DEFAULT).status()); + assertEquals( + RestStatus.CREATED, + highLevelClient().index(new IndexRequest("index").id(id).source("field", -1), RequestOptions.DEFAULT).status() + ); } bulkRequest.add(updateRequest); } @@ -834,13 +900,13 @@ public void afterBulk(long executionId, BulkRequest request, Throwable failure) } }; - try (BulkProcessor processor = BulkProcessor.builder( - (request, bulkListener) -> highLevelClient().bulkAsync(request, - RequestOptions.DEFAULT, bulkListener), listener, "CrudIT") - .setConcurrentRequests(0) - .setBulkSize(new ByteSizeValue(5, ByteSizeUnit.GB)) - .setBulkActions(nbItems + 1) - .build()) { + try ( + BulkProcessor processor = BulkProcessor.builder( + (request, bulkListener) -> highLevelClient().bulkAsync(request, RequestOptions.DEFAULT, bulkListener), + listener, + "CrudIT" + ).setConcurrentRequests(0).setBulkSize(new ByteSizeValue(5, ByteSizeUnit.GB)).setBulkActions(nbItems + 1).build() + ) { for (int i = 0; i < nbItems; i++) { String id = String.valueOf(i); boolean erroneous = randomBoolean(); @@ -849,9 +915,10 @@ public void afterBulk(long executionId, BulkRequest request, Throwable failure) DocWriteRequest.OpType opType = randomFrom(DocWriteRequest.OpType.values()); if (opType == DocWriteRequest.OpType.DELETE) { if (erroneous == false) { - assertEquals(RestStatus.CREATED, - highLevelClient().index( - new IndexRequest("index").id(id).source("field", -1), RequestOptions.DEFAULT).status()); + assertEquals( + RestStatus.CREATED, + highLevelClient().index(new IndexRequest("index").id(id).source("field", -1), RequestOptions.DEFAULT).status() + ); } DeleteRequest deleteRequest = new DeleteRequest("index", id); processor.add(deleteRequest); @@ -873,12 +940,13 @@ public void afterBulk(long executionId, BulkRequest request, Throwable failure) processor.add(createRequest); } else if (opType == DocWriteRequest.OpType.UPDATE) { - UpdateRequest updateRequest = new UpdateRequest("index", id) - .doc(new IndexRequest().source(xContentType, "id", i)); + UpdateRequest updateRequest = new UpdateRequest("index", id).doc(new IndexRequest().source(xContentType, "id", i)); if (erroneous == false) { - assertEquals(RestStatus.CREATED, - highLevelClient().index( - new IndexRequest("index").id(id).source("field", -1), RequestOptions.DEFAULT).status()); + assertEquals( + RestStatus.CREATED, + highLevelClient().index(new IndexRequest("index").id(id).source("field", -1), RequestOptions.DEFAULT) + .status() + ); } processor.add(updateRequest); } @@ -888,7 +956,6 @@ public void afterBulk(long executionId, BulkRequest request, Throwable failure) assertNull(requestRef.get()); } - BulkResponse bulkResponse = responseRef.get(); BulkRequest bulkRequest = requestRef.get(); @@ -924,8 +991,8 @@ private void validateBulkResponses(int nbItems, boolean[] errors, BulkResponse b public void testUrlEncode() throws IOException { String indexPattern = ""; - String expectedIndex = "logstash-" + - DateTimeFormatter.ofPattern("uuuu.MM.dd", Locale.ROOT) + String expectedIndex = "logstash-" + + DateTimeFormatter.ofPattern("uuuu.MM.dd", Locale.ROOT) .format(ZonedDateTime.now(ZoneOffset.UTC).withDayOfMonth(1).with(LocalTime.MIN)); { IndexRequest indexRequest = new IndexRequest(indexPattern).id("id#1"); @@ -962,7 +1029,7 @@ public void testUrlEncode() throws IOException { } public void testParamsEncode() throws IOException { - //parameters are encoded by the low-level client but let's test that everything works the same when we use the high-level one + // parameters are encoded by the low-level client but let's test that everything works the same when we use the high-level one String routing = "routing/中文value#1?"; { IndexRequest indexRequest = new IndexRequest("index").id("id"); @@ -1005,20 +1072,16 @@ public void testTermvectors() throws IOException { final String sourceIndex = "index1"; { // prepare : index docs - Settings settings = Settings.builder() - .put("number_of_shards", 1) - .put("number_of_replicas", 0) - .build(); + Settings settings = Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0).build(); String mappings = "\"properties\":{\"field\":{\"type\":\"text\"}}"; createIndex(sourceIndex, settings, mappings); assertEquals( RestStatus.OK, highLevelClient().bulk( - new BulkRequest() - .add(new IndexRequest(sourceIndex).id("1") - .source(Collections.singletonMap("field", "value1"), XContentType.JSON)) - .add(new IndexRequest(sourceIndex).id("2") - .source(Collections.singletonMap("field", "value2"), XContentType.JSON)) + new BulkRequest().add( + new IndexRequest(sourceIndex).id("1").source(Collections.singletonMap("field", "value1"), XContentType.JSON) + ) + .add(new IndexRequest(sourceIndex).id("2").source(Collections.singletonMap("field", "value2"), XContentType.JSON)) .setRefreshPolicy(RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT ).status() @@ -1032,11 +1095,19 @@ public void testTermvectors() throws IOException { TermVectorsResponse.TermVector.Token expectedToken = new TermVectorsResponse.TermVector.Token(0, 6, 0, null); TermVectorsResponse.TermVector.Term expectedTerm = new TermVectorsResponse.TermVector.Term( - "value1", 1, null, null, null, Collections.singletonList(expectedToken)); - TermVectorsResponse.TermVector.FieldStatistics expectedFieldStats = - new TermVectorsResponse.TermVector.FieldStatistics(2, 2, 2); - TermVectorsResponse.TermVector expectedTV = - new TermVectorsResponse.TermVector("field", expectedFieldStats, Collections.singletonList(expectedTerm)); + "value1", + 1, + null, + null, + null, + Collections.singletonList(expectedToken) + ); + TermVectorsResponse.TermVector.FieldStatistics expectedFieldStats = new TermVectorsResponse.TermVector.FieldStatistics(2, 2, 2); + TermVectorsResponse.TermVector expectedTV = new TermVectorsResponse.TermVector( + "field", + expectedFieldStats, + Collections.singletonList(expectedTerm) + ); List expectedTVlist = Collections.singletonList(expectedTV); assertThat(tvResponse.getIndex(), equalTo(sourceIndex)); @@ -1054,11 +1125,19 @@ public void testTermvectors() throws IOException { TermVectorsResponse.TermVector.Token expectedToken = new TermVectorsResponse.TermVector.Token(0, 6, 0, null); TermVectorsResponse.TermVector.Term expectedTerm = new TermVectorsResponse.TermVector.Term( - "valuex", 1, null, null, null, Collections.singletonList(expectedToken)); - TermVectorsResponse.TermVector.FieldStatistics expectedFieldStats = - new TermVectorsResponse.TermVector.FieldStatistics(2, 2, 2); - TermVectorsResponse.TermVector expectedTV = - new TermVectorsResponse.TermVector("field", expectedFieldStats, Collections.singletonList(expectedTerm)); + "valuex", + 1, + null, + null, + null, + Collections.singletonList(expectedToken) + ); + TermVectorsResponse.TermVector.FieldStatistics expectedFieldStats = new TermVectorsResponse.TermVector.FieldStatistics(2, 2, 2); + TermVectorsResponse.TermVector expectedTV = new TermVectorsResponse.TermVector( + "field", + expectedFieldStats, + Collections.singletonList(expectedTerm) + ); List expectedTVlist = Collections.singletonList(expectedTV); assertThat(tvResponse.getIndex(), equalTo(sourceIndex)); @@ -1071,8 +1150,10 @@ public void testTermvectors() throws IOException { public void testTermvectorsWithNonExistentIndex() { TermVectorsRequest request = new TermVectorsRequest("non-existent", "non-existent"); - ElasticsearchException exception = expectThrows(ElasticsearchException.class, - () -> execute(request, highLevelClient()::termvectors, highLevelClient()::termvectorsAsync)); + ElasticsearchException exception = expectThrows( + ElasticsearchException.class, + () -> execute(request, highLevelClient()::termvectors, highLevelClient()::termvectorsAsync) + ); assertEquals(RestStatus.NOT_FOUND, exception.status()); } @@ -1081,20 +1162,18 @@ public void testMultiTermvectors() throws IOException { final String sourceIndex = "index1"; { // prepare : index docs - Settings settings = Settings.builder() - .put("number_of_shards", 1) - .put("number_of_replicas", 0) - .build(); + Settings settings = Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0).build(); String mappings = "\"properties\":{\"field\":{\"type\":\"text\"}, \"field2\":{\"type\":\"text\"}}"; createIndex(sourceIndex, settings, mappings); assertEquals( RestStatus.OK, highLevelClient().bulk( - new BulkRequest() - .add(new IndexRequest(sourceIndex).id("1") - .source(Map.of("field", "value1", "field2", "hello world"), XContentType.JSON)) - .add(new IndexRequest(sourceIndex).id("2") - .source(Map.of("field", "value2", "field2", "foo var"), XContentType.JSON)) + new BulkRequest().add( + new IndexRequest(sourceIndex).id("1").source(Map.of("field", "value1", "field2", "hello world"), XContentType.JSON) + ) + .add( + new IndexRequest(sourceIndex).id("2").source(Map.of("field", "value2", "field2", "foo var"), XContentType.JSON) + ) .setRefreshPolicy(RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT ).status() @@ -1102,16 +1181,19 @@ public void testMultiTermvectors() throws IOException { } { // test _mtermvectors where MultiTermVectorsRequest is constructed with ids and a template - String[] expectedIds = {"1", "2"}; + String[] expectedIds = { "1", "2" }; TermVectorsRequest tvRequestTemplate = new TermVectorsRequest(sourceIndex, "fake_id"); tvRequestTemplate.setFields("field"); MultiTermVectorsRequest mtvRequest = new MultiTermVectorsRequest(expectedIds, tvRequestTemplate); - MultiTermVectorsResponse mtvResponse = - execute(mtvRequest, highLevelClient()::mtermvectors, highLevelClient()::mtermvectorsAsync); + MultiTermVectorsResponse mtvResponse = execute( + mtvRequest, + highLevelClient()::mtermvectors, + highLevelClient()::mtermvectorsAsync + ); List ids = new ArrayList<>(); - for (TermVectorsResponse tvResponse: mtvResponse.getTermVectorsResponses()) { + for (TermVectorsResponse tvResponse : mtvResponse.getTermVectorsResponses()) { assertThat(tvResponse.getIndex(), equalTo(sourceIndex)); assertTrue(tvResponse.getFound()); ids.add(tvResponse.getId()); @@ -1131,9 +1213,12 @@ public void testMultiTermvectors() throws IOException { TermVectorsRequest tvRequest2 = new TermVectorsRequest(sourceIndex, docBuilder); mtvRequest.add(tvRequest2); - MultiTermVectorsResponse mtvResponse = - execute(mtvRequest, highLevelClient()::mtermvectors, highLevelClient()::mtermvectorsAsync); - for (TermVectorsResponse tvResponse: mtvResponse.getTermVectorsResponses()) { + MultiTermVectorsResponse mtvResponse = execute( + mtvRequest, + highLevelClient()::mtermvectors, + highLevelClient()::mtermvectorsAsync + ); + for (TermVectorsResponse tvResponse : mtvResponse.getTermVectorsResponses()) { assertThat(tvResponse.getIndex(), equalTo(sourceIndex)); assertTrue(tvResponse.getFound()); } @@ -1154,8 +1239,11 @@ public void testMultiTermvectors() throws IOException { tvRequest3.setFields("field", "field2"); mtvRequest.add(tvRequest3); - MultiTermVectorsResponse mtvResponse = - execute(mtvRequest, highLevelClient()::mtermvectors, highLevelClient()::mtermvectorsAsync); + MultiTermVectorsResponse mtvResponse = execute( + mtvRequest, + highLevelClient()::mtermvectors, + highLevelClient()::mtermvectorsAsync + ); List> expectedRespFields = List.of(List.of("field"), List.of("field2"), List.of("field", "field2")); List responses = mtvResponse.getTermVectorsResponses(); assertEquals(expectedRespFields.size(), responses.size()); @@ -1166,7 +1254,8 @@ public void testMultiTermvectors() throws IOException { assertEquals(expectedRespFields.get(i).size(), tvResponse.getTermVectorsList().size()); assertEquals( expectedRespFields.get(i), - tvResponse.getTermVectorsList().stream().map(tv -> tv.getFieldName()).collect(Collectors.toList())); + tvResponse.getTermVectorsList().stream().map(tv -> tv.getFieldName()).collect(Collectors.toList()) + ); } } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/CustomRestHighLevelClientTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/CustomRestHighLevelClientTests.java index 4fac71e32ba1e..aa90cf5a4d599 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/CustomRestHighLevelClientTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/CustomRestHighLevelClientTests.java @@ -25,11 +25,11 @@ import org.elasticsearch.action.main.MainResponse; import org.elasticsearch.action.support.PlainActionFuture; import org.elasticsearch.cluster.ClusterName; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.RequestMatcher; +import org.elasticsearch.xcontent.XContentType; import org.junit.Before; import java.io.IOException; @@ -67,15 +67,12 @@ public void initClients() throws IOException { RestHighLevelClientTests.mockGetRoot(restClient); restHighLevelClient = new CustomRestClient(restClient); - doAnswer(inv -> mockPerformRequest((Request) inv.getArguments()[0])) - .when(restClient) - .performRequest(argThat(new RequestMatcher("GET", ENDPOINT)::matches)); + doAnswer(inv -> mockPerformRequest((Request) inv.getArguments()[0])).when(restClient) + .performRequest(argThat(new RequestMatcher("GET", ENDPOINT)::matches)); - doAnswer(inv -> mockPerformRequestAsync( - ((Request) inv.getArguments()[0]), - (ResponseListener) inv.getArguments()[1])) - .when(restClient) - .performRequestAsync(argThat(new RequestMatcher("GET", ENDPOINT)::matches), any(ResponseListener.class)); + doAnswer(inv -> mockPerformRequestAsync(((Request) inv.getArguments()[0]), (ResponseListener) inv.getArguments()[1])).when( + restClient + ).performRequestAsync(argThat(new RequestMatcher("GET", ENDPOINT)::matches), any(ResponseListener.class)); } } @@ -115,21 +112,21 @@ private static RequestOptions optionsForNodeName(String nodeName) { */ @SuppressForbidden(reason = "We're forced to uses Class#getDeclaredMethods() here because this test checks protected methods") public void testMethodsVisibility() { - final String[] methodNames = new String[]{"convertExistsResponse", - "parseEntity", - "parseResponseException", - "performRequest", - "performRequestAndParseEntity", - "performRequestAndParseOptionalEntity", - "performRequestAsync", - "performRequestAsyncAndParseEntity", - "performRequestAsyncAndParseOptionalEntity" - }; - - final Set protectedMethods = Arrays.stream(RestHighLevelClient.class.getDeclaredMethods()) - .filter(method -> Modifier.isProtected(method.getModifiers())) - .map(Method::getName) - .collect(Collectors.toCollection(TreeSet::new)); + final String[] methodNames = new String[] { + "convertExistsResponse", + "parseEntity", + "parseResponseException", + "performRequest", + "performRequestAndParseEntity", + "performRequestAndParseOptionalEntity", + "performRequestAsync", + "performRequestAsyncAndParseEntity", + "performRequestAsyncAndParseOptionalEntity" }; + + final Set protectedMethods = Arrays.stream(RestHighLevelClient.class.getDeclaredMethods()) + .filter(method -> Modifier.isProtected(method.getModifiers())) + .map(Method::getName) + .collect(Collectors.toCollection(TreeSet::new)); assertThat(protectedMethods, contains(methodNames)); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ESRestHighLevelClientTestCase.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ESRestHighLevelClientTestCase.java index bd779d2b1171b..83d7f19cd811f 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ESRestHighLevelClientTestCase.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ESRestHighLevelClientTestCase.java @@ -22,16 +22,13 @@ import org.elasticsearch.client.cluster.RemoteInfoRequest; import org.elasticsearch.client.cluster.RemoteInfoResponse; import org.elasticsearch.client.indices.CreateIndexRequest; -import org.elasticsearch.common.settings.SecureString; -import org.elasticsearch.core.Booleans; -import org.elasticsearch.core.CheckedRunnable; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.xcontent.json.JsonXContent; +import org.elasticsearch.core.Booleans; +import org.elasticsearch.core.CheckedRunnable; import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.ingest.Pipeline; import org.elasticsearch.search.SearchHit; @@ -39,6 +36,9 @@ import org.elasticsearch.tasks.RawTaskStatus; import org.elasticsearch.tasks.TaskId; import org.elasticsearch.test.rest.ESRestTestCase; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.xcontent.json.JsonXContent; import org.junit.AfterClass; import org.junit.Before; @@ -65,13 +65,12 @@ @SuppressWarnings("removal") public abstract class ESRestHighLevelClientTestCase extends ESRestTestCase { - public static final String IGNORE_THROTTLED_DEPRECATION_WARNING = "[ignore_throttled] parameter is deprecated because frozen " + - "indices have been deprecated. Consider cold or frozen tiers in place of frozen indices."; + public static final String IGNORE_THROTTLED_DEPRECATION_WARNING = "[ignore_throttled] parameter is deprecated because frozen " + + "indices have been deprecated. Consider cold or frozen tiers in place of frozen indices."; protected static final RequestOptions IGNORE_THROTTLED_WARNING = RequestOptions.DEFAULT.toBuilder() - .setWarningsHandler( - warnings -> List.of(IGNORE_THROTTLED_DEPRECATION_WARNING).equals(warnings) == false - ).build(); + .setWarningsHandler(warnings -> List.of(IGNORE_THROTTLED_DEPRECATION_WARNING).equals(warnings) == false) + .build(); protected static final String CONFLICT_PIPELINE_ID = "conflict_pipeline"; private static RestHighLevelClient restHighLevelClient; @@ -104,9 +103,7 @@ protected static RestHighLevelClient highLevelClient() { @Override protected Settings restAdminSettings() { String token = basicAuthHeaderValue("admin_user", new SecureString("admin-password".toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } protected static RestHighLevelClient adminHighLevelClient() { @@ -116,16 +113,20 @@ protected static RestHighLevelClient adminHighLevelClient() { /** * Executes the provided request using either the sync method or its async variant, both provided as functions */ - protected static Resp execute(Req request, SyncMethod syncMethod, - AsyncMethod asyncMethod) throws IOException { + protected static Resp execute(Req request, SyncMethod syncMethod, AsyncMethod asyncMethod) + throws IOException { return execute(request, syncMethod, asyncMethod, RequestOptions.DEFAULT); } /** * Executes the provided request using either the sync method or its async variant, both provided as functions */ - protected static Resp execute(Req request, SyncMethod syncMethod, - AsyncMethod asyncMethod, RequestOptions options) throws IOException { + protected static Resp execute( + Req request, + SyncMethod syncMethod, + AsyncMethod asyncMethod, + RequestOptions options + ) throws IOException { if (async == false) { return syncMethod.execute(request, options); } else { @@ -140,8 +141,11 @@ protected static Resp execute(Req request, SyncMethod syn * variant, both provided as functions. This variant is used when the call does * not have a request object (only headers and the request path). */ - protected static Resp execute(SyncMethodNoRequest syncMethodNoRequest, AsyncMethodNoRequest asyncMethodNoRequest, - RequestOptions requestOptions) throws IOException { + protected static Resp execute( + SyncMethodNoRequest syncMethodNoRequest, + AsyncMethodNoRequest asyncMethodNoRequest, + RequestOptions requestOptions + ) throws IOException { if (async == false) { return syncMethodNoRequest.execute(requestOptions); } else { @@ -185,16 +189,12 @@ protected static XContentBuilder buildRandomXContentPipeline(XContentBuilder pip { pipelineBuilder.startObject().startObject("set"); { - pipelineBuilder - .field("field", "foo") - .field("value", "bar"); + pipelineBuilder.field("field", "foo").field("value", "bar"); } pipelineBuilder.endObject().endObject(); pipelineBuilder.startObject().startObject("convert"); { - pipelineBuilder - .field("field", "rank") - .field("type", "integer"); + pipelineBuilder.field("field", "rank").field("type", "integer"); } pipelineBuilder.endObject().endObject(); } @@ -211,16 +211,15 @@ protected static XContentBuilder buildRandomXContentPipeline() throws IOExceptio } protected static void createFieldAddingPipleine(String id, String fieldName, String value) throws IOException { - XContentBuilder pipeline = jsonBuilder() + XContentBuilder pipeline = jsonBuilder().startObject() + .startArray("processors") .startObject() - .startArray("processors") - .startObject() - .startObject("set") - .field("field", fieldName) - .field("value", value) - .endObject() - .endObject() - .endArray() + .startObject("set") + .field("field", fieldName) + .field("value", value) + .endObject() + .endObject() + .endArray() .endObject(); createPipeline(new PutPipelineRequest(id, BytesReference.bytes(pipeline), XContentType.JSON)); @@ -232,40 +231,45 @@ protected static void createPipeline(String pipelineId) throws IOException { } protected static void createPipeline(PutPipelineRequest putPipelineRequest) throws IOException { - assertTrue(execute( - putPipelineRequest, highLevelClient().ingest()::putPipeline, highLevelClient().ingest()::putPipelineAsync).isAcknowledged()); + assertTrue( + execute(putPipelineRequest, highLevelClient().ingest()::putPipeline, highLevelClient().ingest()::putPipelineAsync) + .isAcknowledged() + ); } - protected static void clusterUpdateSettings(Settings persistentSettings, - Settings transientSettings) throws IOException { + protected static void clusterUpdateSettings(Settings persistentSettings, Settings transientSettings) throws IOException { ClusterUpdateSettingsRequest request = new ClusterUpdateSettingsRequest(); request.persistentSettings(persistentSettings); request.transientSettings(transientSettings); RequestOptions options = RequestOptions.DEFAULT.toBuilder().setWarningsHandler(WarningsHandler.PERMISSIVE).build(); - assertTrue(execute( - request, highLevelClient().cluster()::putSettings, highLevelClient().cluster()::putSettingsAsync, options).isAcknowledged()); + assertTrue( + execute(request, highLevelClient().cluster()::putSettings, highLevelClient().cluster()::putSettingsAsync, options) + .isAcknowledged() + ); } protected void putConflictPipeline() throws IOException { - final XContentBuilder pipelineBuilder = jsonBuilder() + final XContentBuilder pipelineBuilder = jsonBuilder().startObject() + .startArray("processors") .startObject() - .startArray("processors") - .startObject() - .startObject("set") - .field("field", "_version") - .field("value", 1) - .endObject() - .endObject() - .startObject() - .startObject("set") - .field("field", "_id") - .field("value", "1") - .endObject() - .endObject() - .endArray() + .startObject("set") + .field("field", "_version") + .field("value", 1) + .endObject() + .endObject() + .startObject() + .startObject("set") + .field("field", "_id") + .field("value", "1") + .endObject() + .endObject() + .endArray() .endObject(); - final PutPipelineRequest putPipelineRequest = new PutPipelineRequest(CONFLICT_PIPELINE_ID, BytesReference.bytes(pipelineBuilder), - pipelineBuilder.contentType()); + final PutPipelineRequest putPipelineRequest = new PutPipelineRequest( + CONFLICT_PIPELINE_ID, + BytesReference.bytes(pipelineBuilder), + pipelineBuilder.contentType() + ); assertTrue(highLevelClient().ingest().putPipeline(putPipelineRequest, RequestOptions.DEFAULT).isAcknowledged()); } @@ -274,10 +278,7 @@ protected Settings restClientSettings() { final String user = Objects.requireNonNull(System.getProperty("tests.rest.cluster.username")); final String pass = Objects.requireNonNull(System.getProperty("tests.rest.cluster.password")); final String token = "Basic " + Base64.getEncoder().encodeToString((user + ":" + pass).getBytes(StandardCharsets.UTF_8)); - return Settings.builder() - .put(super.restClientSettings()) - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(super.restClientSettings()).put(ThreadContext.PREFIX + ".Authorization", token).build(); } protected Iterable searchAll(String... indices) throws IOException { @@ -292,19 +293,15 @@ protected Iterable searchAll(SearchRequest searchRequest) throws IOEx } protected void refreshIndexes(String... indices) throws IOException { - String joinedIndices = Arrays.stream(indices) - .collect(Collectors.joining(",")); + String joinedIndices = Arrays.stream(indices).collect(Collectors.joining(",")); Response refreshResponse = client().performRequest(new Request("POST", "/" + joinedIndices + "/_refresh")); assertEquals(200, refreshResponse.getStatusLine().getStatusCode()); } protected void createIndexWithMultipleShards(String index) throws IOException { CreateIndexRequest indexRequest = new CreateIndexRequest(index); - int shards = randomIntBetween(8,10); - indexRequest.settings(Settings.builder() - .put("index.number_of_shards", shards) - .put("index.number_of_replicas", 0) - ); + int shards = randomIntBetween(8, 10); + indexRequest.settings(Settings.builder().put("index.number_of_shards", shards).put("index.number_of_replicas", 0)); highLevelClient().indices().create(indexRequest, RequestOptions.DEFAULT); } @@ -320,8 +317,7 @@ protected static void setupRemoteClusterConfig(String remoteClusterName) throws ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); updateSettingsRequest.persistentSettings(singletonMap("cluster.remote." + remoteClusterName + ".seeds", transportAddress)); RequestOptions options = RequestOptions.DEFAULT.toBuilder().setWarningsHandler(WarningsHandler.PERMISSIVE).build(); - ClusterUpdateSettingsResponse updateSettingsResponse = - restHighLevelClient.cluster().putSettings(updateSettingsRequest, options); + ClusterUpdateSettingsResponse updateSettingsResponse = restHighLevelClient.cluster().putSettings(updateSettingsRequest, options); assertThat(updateSettingsResponse.isAcknowledged(), is(true)); assertBusy(() -> { @@ -343,11 +339,11 @@ protected static TaskId findTaskToRethrottle(String actionName, String descripti do { ListTasksResponse list = highLevelClient().tasks().list(request, RequestOptions.DEFAULT); list.rethrowFailures("Finding tasks to rethrottle"); - List taskGroups = - list.getTaskGroups().stream() - .filter(taskGroup -> taskGroup.getTaskInfo().getDescription().equals(description)).collect(Collectors.toList()); - assertThat("tasks are left over from the last execution of this test", - taskGroups, hasSize(lessThan(2))); + List taskGroups = list.getTaskGroups() + .stream() + .filter(taskGroup -> taskGroup.getTaskInfo().getDescription().equals(description)) + .collect(Collectors.toList()); + assertThat("tasks are left over from the last execution of this test", taskGroups, hasSize(lessThan(2))); if (0 == taskGroups.size()) { // The parent task hasn't started yet continue; @@ -359,8 +355,10 @@ protected static TaskId findTaskToRethrottle(String actionName, String descripti return taskGroup.getTaskInfo().getTaskId(); } } while (System.nanoTime() - start < TimeUnit.SECONDS.toNanos(10)); - throw new AssertionError("Couldn't find tasks to rethrottle. Here are the running tasks " + - highLevelClient().tasks().list(request, RequestOptions.DEFAULT)); + throw new AssertionError( + "Couldn't find tasks to rethrottle. Here are the running tasks " + + highLevelClient().tasks().list(request, RequestOptions.DEFAULT) + ); } protected static CheckedRunnable checkTaskCompletionStatus(RestClient client, String taskId) { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/EnrichIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/EnrichIT.java index ca89be39c3c05..4518592365855 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/EnrichIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/EnrichIT.java @@ -30,13 +30,19 @@ public class EnrichIT extends ESRestHighLevelClientTestCase { public void testCRUD() throws Exception { - CreateIndexRequest createIndexRequest = new CreateIndexRequest("my-index") - .mapping(Map.of("properties", Map.of("enrich_key", Map.of("type", "keyword")))); + CreateIndexRequest createIndexRequest = new CreateIndexRequest("my-index").mapping( + Map.of("properties", Map.of("enrich_key", Map.of("type", "keyword"))) + ); highLevelClient().indices().create(createIndexRequest, RequestOptions.DEFAULT); final EnrichClient enrichClient = highLevelClient().enrich(); - PutPolicyRequest putPolicyRequest = - new PutPolicyRequest("my-policy", "match", List.of("my-index"), "enrich_key", List.of("enrich_value")); + PutPolicyRequest putPolicyRequest = new PutPolicyRequest( + "my-policy", + "match", + List.of("my-index"), + "enrich_key", + List.of("enrich_value") + ); AcknowledgedResponse putPolicyResponse = execute(putPolicyRequest, enrichClient::putPolicy, enrichClient::putPolicyAsync); assertThat(putPolicyResponse.isAcknowledged(), is(true)); @@ -59,13 +65,19 @@ public void testCRUD() throws Exception { assertThat(statsResponse.getCoordinatorStats().get(0).getExecutedSearchesTotal(), greaterThanOrEqualTo(0L)); ExecutePolicyRequest executePolicyRequest = new ExecutePolicyRequest("my-policy"); - ExecutePolicyResponse executePolicyResponse = - execute(executePolicyRequest, enrichClient::executePolicy, enrichClient::executePolicyAsync); + ExecutePolicyResponse executePolicyResponse = execute( + executePolicyRequest, + enrichClient::executePolicy, + enrichClient::executePolicyAsync + ); assertThat(executePolicyResponse.getExecutionStatus().getPhase(), equalTo("COMPLETE")); DeletePolicyRequest deletePolicyRequest = new DeletePolicyRequest("my-policy"); - AcknowledgedResponse deletePolicyResponse = - execute(deletePolicyRequest, enrichClient::deletePolicy, enrichClient::deletePolicyAsync); + AcknowledgedResponse deletePolicyResponse = execute( + deletePolicyRequest, + enrichClient::deletePolicy, + enrichClient::deletePolicyAsync + ); assertThat(deletePolicyResponse.isAcknowledged(), is(true)); getPolicyRequest = new GetPolicyRequest(); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/EqlIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/EqlIT.java index 64c92c06b29ff..2b95e2b61d786 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/EqlIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/EqlIT.java @@ -55,8 +55,8 @@ private void setupData() throws IOException { bulkRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); for (int i = 0; i < RECORD_COUNT; i++) { final IndexRequest indexRequest = new IndexRequest(INDEX_NAME); - indexRequest.source(jsonBuilder() - .startObject() + indexRequest.source( + jsonBuilder().startObject() .field("event_subtype_full", "already_running") .startObject("event") .field("category", "process") @@ -70,7 +70,8 @@ private void setupData() throws IOException { .field("subtype", "create") .field("@timestamp", String.format(Locale.ROOT, "2018-01-01T00:00:%02dZ", i)) .field("unique_pid", ((i % DIVIDER) == 0) ? 101 : 0) - .endObject()); + .endObject() + ); bulkRequest.add(indexRequest); } BulkResponse bulkResponse = highLevelClient().bulk(bulkRequest, RequestOptions.DEFAULT); @@ -129,8 +130,10 @@ public void testSimpleConditionSearch() throws Exception { public void testEqualsInFilterConditionSearch() throws Exception { EqlClient eql = highLevelClient().eql(); - EqlSearchRequest request = new EqlSearchRequest("index", - "process where event_type_full == \"process_event\" and serial_event_id in (1,3,5)"); + EqlSearchRequest request = new EqlSearchRequest( + "index", + "process where event_type_full == \"process_event\" and serial_event_id in (1,3,5)" + ); EqlSearchResponse response = execute(request, eql::search, eql::searchAsync); assertResponse(response, 3); @@ -168,7 +171,6 @@ public void testLargeMapping() throws Exception { client().performRequest(doc1); client().performRequest(new Request(HttpPost.METHOD_NAME, "/_refresh")); - EqlClient eql = highLevelClient().eql(); EqlSearchRequest request = new EqlSearchRequest(index, "process where true"); EqlSearchResponse response = execute(request, eql::search, eql::searchAsync); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/FeaturesIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/FeaturesIT.java index c9527bc123956..8e5e6ae0f5edd 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/FeaturesIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/FeaturesIT.java @@ -26,8 +26,11 @@ public class FeaturesIT extends ESRestHighLevelClientTestCase { public void testGetFeatures() throws IOException { GetFeaturesRequest request = new GetFeaturesRequest(); - GetFeaturesResponse response = execute(request, - highLevelClient().features()::getFeatures, highLevelClient().features()::getFeaturesAsync); + GetFeaturesResponse response = execute( + request, + highLevelClient().features()::getFeatures, + highLevelClient().features()::getFeaturesAsync + ); assertThat(response, notNullValue()); assertThat(response.getFeatures(), notNullValue()); @@ -48,15 +51,21 @@ public void testResetFeatures() throws IOException { RestHighLevelClient adminHighLevelClient = new RestHighLevelClient( adminClient(), (client) -> {}, - new SearchModule(Settings.EMPTY, Collections.emptyList()).getNamedXContents()); - ResetFeaturesResponse response = execute(request, + new SearchModule(Settings.EMPTY, Collections.emptyList()).getNamedXContents() + ); + ResetFeaturesResponse response = execute( + request, adminHighLevelClient.features()::resetFeatures, - adminHighLevelClient.features()::resetFeaturesAsync); + adminHighLevelClient.features()::resetFeaturesAsync + ); assertThat(response, notNullValue()); assertThat(response.getFeatureResetStatuses(), notNullValue()); assertThat(response.getFeatureResetStatuses().size(), greaterThan(1)); - assertTrue(response.getFeatureResetStatuses().stream().anyMatch( - feature -> "tasks".equals(feature.getFeatureName()) && "SUCCESS".equals(feature.getStatus()))); + assertTrue( + response.getFeatureResetStatuses() + .stream() + .anyMatch(feature -> "tasks".equals(feature.getFeatureName()) && "SUCCESS".equals(feature.getStatus())) + ); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/GetAliasesResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/GetAliasesResponseTests.java index b6c2493fd4c52..79bef5f9a86df 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/GetAliasesResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/GetAliasesResponseTests.java @@ -9,10 +9,10 @@ package org.elasticsearch.client; import org.elasticsearch.cluster.metadata.AliasMetadata; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.json.JsonXContent; import java.io.IOException; import java.util.HashMap; @@ -73,8 +73,8 @@ protected GetAliasesResponse doParseInstance(XContentParser parser) throws IOExc @Override protected Predicate getRandomFieldsExcludeFilter() { return p -> p.equals("") // do not add elements at the top-level as any element at this level is parsed as a new index - || p.endsWith(".aliases") // do not add new alias - || p.contains(".filter"); // do not insert random data into AliasMetadata#filter + || p.endsWith(".aliases") // do not add new alias + || p.contains(".filter"); // do not insert random data into AliasMetadata#filter } @Override @@ -92,44 +92,41 @@ protected void assertEqualInstances(GetAliasesResponse expectedInstance, GetAlia } public void testFromXContentWithElasticsearchException() throws IOException { - String xContent = - "{" + - " \"error\": {" + - " \"root_cause\": [" + - " {" + - " \"type\": \"index_not_found_exception\"," + - " \"reason\": \"no such index [index]\"," + - " \"resource.type\": \"index_or_alias\"," + - " \"resource.id\": \"index\"," + - " \"index_uuid\": \"_na_\"," + - " \"index\": \"index\"" + - " }" + - " ]," + - " \"type\": \"index_not_found_exception\"," + - " \"reason\": \"no such index [index]\"," + - " \"resource.type\": \"index_or_alias\"," + - " \"resource.id\": \"index\"," + - " \"index_uuid\": \"_na_\"," + - " \"index\": \"index\"" + - " }," + - " \"status\": 404" + - "}"; + String xContent = "{" + + " \"error\": {" + + " \"root_cause\": [" + + " {" + + " \"type\": \"index_not_found_exception\"," + + " \"reason\": \"no such index [index]\"," + + " \"resource.type\": \"index_or_alias\"," + + " \"resource.id\": \"index\"," + + " \"index_uuid\": \"_na_\"," + + " \"index\": \"index\"" + + " }" + + " ]," + + " \"type\": \"index_not_found_exception\"," + + " \"reason\": \"no such index [index]\"," + + " \"resource.type\": \"index_or_alias\"," + + " \"resource.id\": \"index\"," + + " \"index_uuid\": \"_na_\"," + + " \"index\": \"index\"" + + " }," + + " \"status\": 404" + + "}"; try (XContentParser parser = createParser(JsonXContent.jsonXContent, xContent)) { GetAliasesResponse getAliasesResponse = GetAliasesResponse.fromXContent(parser); assertThat(getAliasesResponse.getError(), nullValue()); assertThat(getAliasesResponse.status(), equalTo(RestStatus.NOT_FOUND)); - assertThat(getAliasesResponse.getException().getMessage(), - equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index [index]]")); + assertThat( + getAliasesResponse.getException().getMessage(), + equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index [index]]") + ); } } public void testFromXContentWithNoAliasFound() throws IOException { - String xContent = - "{" + - " \"error\": \"alias [aa] missing\"," + - " \"status\": 404" + - "}"; + String xContent = "{" + " \"error\": \"alias [aa] missing\"," + " \"status\": 404" + "}"; try (XContentParser parser = createParser(JsonXContent.jsonXContent, xContent)) { GetAliasesResponse getAliasesResponse = GetAliasesResponse.fromXContent(parser); assertThat(getAliasesResponse.status(), equalTo(RestStatus.NOT_FOUND)); @@ -139,16 +136,15 @@ public void testFromXContentWithNoAliasFound() throws IOException { } public void testFromXContentWithMissingAndFoundAlias() throws IOException { - String xContent = - "{" + - " \"error\": \"alias [something] missing\"," + - " \"status\": 404," + - " \"index\": {" + - " \"aliases\": {" + - " \"alias\": {}" + - " }" + - " }" + - "}"; + String xContent = "{" + + " \"error\": \"alias [something] missing\"," + + " \"status\": 404," + + " \"index\": {" + + " \"aliases\": {" + + " \"alias\": {}" + + " }" + + " }" + + "}"; final String index = "index"; try (XContentParser parser = createParser(JsonXContent.jsonXContent, xContent)) { GetAliasesResponse response = GetAliasesResponse.fromXContent(parser); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/GraphIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/GraphIT.java index 0b50c5a01b2ce..5f589030ae92d 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/GraphIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/GraphIT.java @@ -10,13 +10,13 @@ import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; import org.elasticsearch.action.ShardOperationFailedException; -import org.elasticsearch.index.query.QueryBuilder; -import org.elasticsearch.index.query.TermQueryBuilder; import org.elasticsearch.client.graph.GraphExploreRequest; import org.elasticsearch.client.graph.GraphExploreResponse; import org.elasticsearch.client.graph.Hop; import org.elasticsearch.client.graph.Vertex; import org.elasticsearch.client.graph.VertexRequest; +import org.elasticsearch.index.query.QueryBuilder; +import org.elasticsearch.index.query.TermQueryBuilder; import org.hamcrest.Matchers; import org.junit.Before; @@ -50,7 +50,6 @@ public void indexDocuments() throws IOException { doc5.setJsonEntity("{\"num\":[2,4], \"const\":\"foo\"}"); client().performRequest(doc5); - client().performRequest(new Request(HttpPost.METHOD_NAME, "/_refresh")); } @@ -63,7 +62,7 @@ public void testCleanExplore() throws Exception { QueryBuilder guidingQuery = null; if (i == 0) { guidingQuery = new TermQueryBuilder("const.keyword", "start"); - } else if (randomBoolean()){ + } else if (randomBoolean()) { guidingQuery = new TermQueryBuilder("const.keyword", "foo"); } Hop hop = graphExploreRequest.createNextHop(guidingQuery); @@ -89,7 +88,7 @@ public void testCleanExplore() throws Exception { } public void testBadExplore() throws Exception { - //Explore indices where lack of fielddata=true on one index leads to partial failures + // Explore indices where lack of fielddata=true on one index leads to partial failures GraphExploreRequest graphExploreRequest = new GraphExploreRequest(); graphExploreRequest.indices("index1", "index2", "index_no_field_data"); graphExploreRequest.useSignificance(false); @@ -98,7 +97,7 @@ public void testBadExplore() throws Exception { QueryBuilder guidingQuery = null; if (i == 0) { guidingQuery = new TermQueryBuilder("const.keyword", "start"); - } else if (randomBoolean()){ + } else if (randomBoolean()) { guidingQuery = new TermQueryBuilder("const.keyword", "foo"); } Hop hop = graphExploreRequest.createNextHop(guidingQuery); @@ -124,5 +123,4 @@ public void testBadExplore() throws Exception { } - } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/GraphRequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/GraphRequestConvertersTests.java index 13a50c1382f61..6463dee8f959b 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/GraphRequestConvertersTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/GraphRequestConvertersTests.java @@ -12,10 +12,10 @@ import org.elasticsearch.client.graph.GraphExploreRequest; import org.elasticsearch.client.graph.Hop; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.TermQueryBuilder; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentType; import java.util.HashMap; import java.util.Map; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/HighLevelRestClientCompressionIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/HighLevelRestClientCompressionIT.java index 6b5c6fc0e9ad7..e9657bf7d0d14 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/HighLevelRestClientCompressionIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/HighLevelRestClientCompressionIT.java @@ -28,9 +28,7 @@ public void testCompressesResponseIfRequested() throws IOException { client().performRequest(doc); client().performRequest(new Request(HttpPost.METHOD_NAME, "/_refresh")); - RequestOptions requestOptions = RequestOptions.DEFAULT.toBuilder() - .addHeader(HttpHeaders.ACCEPT_ENCODING, GZIP_ENCODING) - .build(); + RequestOptions requestOptions = RequestOptions.DEFAULT.toBuilder().addHeader(HttpHeaders.ACCEPT_ENCODING, GZIP_ENCODING).build(); SearchRequest searchRequest = new SearchRequest("company"); SearchResponse searchResponse = execute(searchRequest, highLevelClient()::search, highLevelClient()::searchAsync, requestOptions); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/HighLevelRestClientFilterPathIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/HighLevelRestClientFilterPathIT.java index c077565b0d7d9..01eaa6aee7e70 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/HighLevelRestClientFilterPathIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/HighLevelRestClientFilterPathIT.java @@ -28,9 +28,7 @@ public void testUsingFilterPathWithHitsIndexResultsIntoEmptyIndexNameInInnerHit( client().performRequest(doc); client().performRequest(new Request(HttpPost.METHOD_NAME, "/_refresh")); - RequestOptions requestOptions = RequestOptions.DEFAULT.toBuilder() - .addParameter(FILTER_PATH_PARAM, FILTER_PATH_PARAM_VALUE) - .build(); + RequestOptions requestOptions = RequestOptions.DEFAULT.toBuilder().addParameter(FILTER_PATH_PARAM, FILTER_PATH_PARAM_VALUE).build(); SearchRequest searchRequest = new SearchRequest("company_one"); SearchResponse searchResponse = execute(searchRequest, highLevelClient()::search, highLevelClient()::searchAsync, requestOptions); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndexLifecycleIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndexLifecycleIT.java index 2ac88eec77261..620fe92d0d6b9 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndexLifecycleIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndexLifecycleIT.java @@ -73,8 +73,13 @@ public void testRemoveIndexLifecyclePolicy() throws Exception { LifecyclePolicy policy = createRandomPolicy(policyName); ensurePrerequisites(policy); PutLifecyclePolicyRequest putRequest = new PutLifecyclePolicyRequest(policy); - assertAcked(execute(putRequest, highLevelClient().indexLifecycle()::putLifecyclePolicy, - highLevelClient().indexLifecycle()::putLifecyclePolicyAsync)); + assertAcked( + execute( + putRequest, + highLevelClient().indexLifecycle()::putLifecyclePolicy, + highLevelClient().indexLifecycle()::putLifecyclePolicyAsync + ) + ); createIndex("foo", Settings.builder().put("index.lifecycle.name", policyName).build()); createIndex("baz", Settings.builder().put("index.lifecycle.name", policyName).build()); @@ -90,8 +95,11 @@ public void testRemoveIndexLifecyclePolicy() throws Exception { indices.add("foo"); indices.add("rbh"); RemoveIndexLifecyclePolicyRequest removeReq = new RemoveIndexLifecyclePolicyRequest(indices); - RemoveIndexLifecyclePolicyResponse removeResp = execute(removeReq, highLevelClient().indexLifecycle()::removeIndexLifecyclePolicy, - highLevelClient().indexLifecycle()::removeIndexLifecyclePolicyAsync); + RemoveIndexLifecyclePolicyResponse removeResp = execute( + removeReq, + highLevelClient().indexLifecycle()::removeIndexLifecyclePolicy, + highLevelClient().indexLifecycle()::removeIndexLifecyclePolicyAsync + ); assertThat(removeResp.hasFailures(), is(false)); assertThat(removeResp.getFailedIndexes().isEmpty(), is(true)); @@ -107,8 +115,13 @@ public void testStartStopILM() throws Exception { LifecyclePolicy policy = createRandomPolicy(policyName); ensurePrerequisites(policy); PutLifecyclePolicyRequest putRequest = new PutLifecyclePolicyRequest(policy); - assertAcked(execute(putRequest, highLevelClient().indexLifecycle()::putLifecyclePolicy, - highLevelClient().indexLifecycle()::putLifecyclePolicyAsync)); + assertAcked( + execute( + putRequest, + highLevelClient().indexLifecycle()::putLifecyclePolicy, + highLevelClient().indexLifecycle()::putLifecyclePolicyAsync + ) + ); createIndex("foo", Settings.builder().put("index.lifecycle.name", "bar").build()); createIndex("baz", Settings.builder().put("index.lifecycle.name", "eggplant").build()); @@ -118,28 +131,38 @@ public void testStartStopILM() throws Exception { LifecycleManagementStatusResponse statusResponse = execute( statusRequest, highLevelClient().indexLifecycle()::lifecycleManagementStatus, - highLevelClient().indexLifecycle()::lifecycleManagementStatusAsync); + highLevelClient().indexLifecycle()::lifecycleManagementStatusAsync + ); assertEquals(statusResponse.getOperationMode(), OperationMode.RUNNING); StopILMRequest stopReq = new StopILMRequest(); - AcknowledgedResponse stopResponse = execute(stopReq, highLevelClient().indexLifecycle()::stopILM, - highLevelClient().indexLifecycle()::stopILMAsync); + AcknowledgedResponse stopResponse = execute( + stopReq, + highLevelClient().indexLifecycle()::stopILM, + highLevelClient().indexLifecycle()::stopILMAsync + ); assertTrue(stopResponse.isAcknowledged()); - - statusResponse = execute(statusRequest, highLevelClient().indexLifecycle()::lifecycleManagementStatus, - highLevelClient().indexLifecycle()::lifecycleManagementStatusAsync); - assertThat(statusResponse.getOperationMode(), - Matchers.anyOf(equalTo(OperationMode.STOPPING), - equalTo(OperationMode.STOPPED))); + statusResponse = execute( + statusRequest, + highLevelClient().indexLifecycle()::lifecycleManagementStatus, + highLevelClient().indexLifecycle()::lifecycleManagementStatusAsync + ); + assertThat(statusResponse.getOperationMode(), Matchers.anyOf(equalTo(OperationMode.STOPPING), equalTo(OperationMode.STOPPED))); StartILMRequest startReq = new StartILMRequest(); - AcknowledgedResponse startResponse = execute(startReq, highLevelClient().indexLifecycle()::startILM, - highLevelClient().indexLifecycle()::startILMAsync); + AcknowledgedResponse startResponse = execute( + startReq, + highLevelClient().indexLifecycle()::startILM, + highLevelClient().indexLifecycle()::startILMAsync + ); assertTrue(startResponse.isAcknowledged()); - statusResponse = execute(statusRequest, highLevelClient().indexLifecycle()::lifecycleManagementStatus, - highLevelClient().indexLifecycle()::lifecycleManagementStatusAsync); + statusResponse = execute( + statusRequest, + highLevelClient().indexLifecycle()::lifecycleManagementStatus, + highLevelClient().indexLifecycle()::lifecycleManagementStatusAsync + ); assertEquals(statusResponse.getOperationMode(), OperationMode.RUNNING); } @@ -171,20 +194,33 @@ public void testExplainLifecycle() throws Exception { LifecyclePolicy policy = new LifecyclePolicy(randomAlphaOfLength(10), lifecyclePhases); ensurePrerequisites(policy); PutLifecyclePolicyRequest putRequest = new PutLifecyclePolicyRequest(policy); - AcknowledgedResponse putResponse = execute(putRequest, highLevelClient().indexLifecycle()::putLifecyclePolicy, - highLevelClient().indexLifecycle()::putLifecyclePolicyAsync); + AcknowledgedResponse putResponse = execute( + putRequest, + highLevelClient().indexLifecycle()::putLifecyclePolicy, + highLevelClient().indexLifecycle()::putLifecyclePolicyAsync + ); assertTrue(putResponse.isAcknowledged()); GetLifecyclePolicyRequest getRequest = new GetLifecyclePolicyRequest(policy.getName()); - GetLifecyclePolicyResponse getResponse = execute(getRequest, highLevelClient().indexLifecycle()::getLifecyclePolicy, - highLevelClient().indexLifecycle()::getLifecyclePolicyAsync); + GetLifecyclePolicyResponse getResponse = execute( + getRequest, + highLevelClient().indexLifecycle()::getLifecyclePolicy, + highLevelClient().indexLifecycle()::getLifecyclePolicyAsync + ); long expectedPolicyModifiedDate = getResponse.getPolicies().get(policy.getName()).getModifiedDate(); + createIndex( + "foo-01", + Settings.builder().put("index.lifecycle.name", policy.getName()).put("index.lifecycle.rollover_alias", "foo-alias").build(), + "", + "\"foo-alias\" : {}" + ); - createIndex("foo-01", Settings.builder().put("index.lifecycle.name", policy.getName()) - .put("index.lifecycle.rollover_alias", "foo-alias").build(), "", "\"foo-alias\" : {}"); - - createIndex("baz-01", Settings.builder().put("index.lifecycle.name", policy.getName()) - .put("index.lifecycle.rollover_alias", "baz-alias").build(), "", "\"baz-alias\" : {}"); + createIndex( + "baz-01", + Settings.builder().put("index.lifecycle.name", policy.getName()).put("index.lifecycle.rollover_alias", "baz-alias").build(), + "", + "\"baz-alias\" : {}" + ); createIndex("squash", Settings.EMPTY); @@ -193,8 +229,11 @@ public void testExplainLifecycle() throws Exception { // ready to roll over assertBusy(() -> { ExplainLifecycleRequest req = new ExplainLifecycleRequest("foo-01", "baz-01", "squash"); - ExplainLifecycleResponse response = execute(req, highLevelClient().indexLifecycle()::explainLifecycle, - highLevelClient().indexLifecycle()::explainLifecycleAsync); + ExplainLifecycleResponse response = execute( + req, + highLevelClient().indexLifecycle()::explainLifecycle, + highLevelClient().indexLifecycle()::explainLifecycleAsync + ); Map indexResponses = response.getIndexResponses(); assertEquals(3, indexResponses.size()); IndexLifecycleExplainResponse fooResponse = indexResponses.get("foo-01"); @@ -204,8 +243,15 @@ public void testExplainLifecycle() throws Exception { assertEquals("hot", fooResponse.getPhase()); assertEquals("rollover", fooResponse.getAction()); assertEquals("check-rollover-ready", fooResponse.getStep()); - assertEquals(new PhaseExecutionInfo(policy.getName(), new Phase("", hotPhase.getMinimumAge(), hotPhase.getActions()), - 1L, expectedPolicyModifiedDate), fooResponse.getPhaseExecutionInfo()); + assertEquals( + new PhaseExecutionInfo( + policy.getName(), + new Phase("", hotPhase.getMinimumAge(), hotPhase.getActions()), + 1L, + expectedPolicyModifiedDate + ), + fooResponse.getPhaseExecutionInfo() + ); IndexLifecycleExplainResponse bazResponse = indexResponses.get("baz-01"); assertNotNull(bazResponse); assertTrue(bazResponse.managedByILM()); @@ -226,17 +272,32 @@ public void testDeleteLifecycle() throws IOException { LifecyclePolicy policy = createRandomPolicy(policyName); ensurePrerequisites(policy); PutLifecyclePolicyRequest putRequest = new PutLifecyclePolicyRequest(policy); - assertAcked(execute(putRequest, highLevelClient().indexLifecycle()::putLifecyclePolicy, - highLevelClient().indexLifecycle()::putLifecyclePolicyAsync)); + assertAcked( + execute( + putRequest, + highLevelClient().indexLifecycle()::putLifecyclePolicy, + highLevelClient().indexLifecycle()::putLifecyclePolicyAsync + ) + ); DeleteLifecyclePolicyRequest deleteRequest = new DeleteLifecyclePolicyRequest(policy.getName()); - assertAcked(execute(deleteRequest, highLevelClient().indexLifecycle()::deleteLifecyclePolicy, - highLevelClient().indexLifecycle()::deleteLifecyclePolicyAsync)); + assertAcked( + execute( + deleteRequest, + highLevelClient().indexLifecycle()::deleteLifecyclePolicy, + highLevelClient().indexLifecycle()::deleteLifecyclePolicyAsync + ) + ); GetLifecyclePolicyRequest getRequest = new GetLifecyclePolicyRequest(policyName); - ElasticsearchStatusException ex = expectThrows(ElasticsearchStatusException.class, - () -> execute(getRequest, highLevelClient().indexLifecycle()::getLifecyclePolicy, - highLevelClient().indexLifecycle()::getLifecyclePolicyAsync)); + ElasticsearchStatusException ex = expectThrows( + ElasticsearchStatusException.class, + () -> execute( + getRequest, + highLevelClient().indexLifecycle()::getLifecyclePolicy, + highLevelClient().indexLifecycle()::getLifecyclePolicyAsync + ) + ); assertEquals(404, ex.status().getStatus()); } @@ -246,12 +307,20 @@ public void testPutLifecycle() throws IOException { ensurePrerequisites(policy); PutLifecyclePolicyRequest putRequest = new PutLifecyclePolicyRequest(policy); - assertAcked(execute(putRequest, highLevelClient().indexLifecycle()::putLifecyclePolicy, - highLevelClient().indexLifecycle()::putLifecyclePolicyAsync)); + assertAcked( + execute( + putRequest, + highLevelClient().indexLifecycle()::putLifecyclePolicy, + highLevelClient().indexLifecycle()::putLifecyclePolicyAsync + ) + ); GetLifecyclePolicyRequest getRequest = new GetLifecyclePolicyRequest(name); - GetLifecyclePolicyResponse response = execute(getRequest, highLevelClient().indexLifecycle()::getLifecyclePolicy, - highLevelClient().indexLifecycle()::getLifecyclePolicyAsync); + GetLifecyclePolicyResponse response = execute( + getRequest, + highLevelClient().indexLifecycle()::getLifecyclePolicy, + highLevelClient().indexLifecycle()::getLifecyclePolicyAsync + ); assertEquals(policy, response.getPolicies().get(name).getPolicy()); } @@ -264,15 +333,24 @@ public void testGetMultipleLifecyclePolicies() throws IOException { policies[i] = createRandomPolicy(policyNames[i]); ensurePrerequisites(policies[i]); PutLifecyclePolicyRequest putRequest = new PutLifecyclePolicyRequest(policies[i]); - assertAcked(execute(putRequest, highLevelClient().indexLifecycle()::putLifecyclePolicy, - highLevelClient().indexLifecycle()::putLifecyclePolicyAsync)); + assertAcked( + execute( + putRequest, + highLevelClient().indexLifecycle()::putLifecyclePolicy, + highLevelClient().indexLifecycle()::putLifecyclePolicyAsync + ) + ); } GetLifecyclePolicyRequest getRequest = new GetLifecyclePolicyRequest(randomFrom(policyNames, null)); - GetLifecyclePolicyResponse response = execute(getRequest, highLevelClient().indexLifecycle()::getLifecyclePolicy, - highLevelClient().indexLifecycle()::getLifecyclePolicyAsync); + GetLifecyclePolicyResponse response = execute( + getRequest, + highLevelClient().indexLifecycle()::getLifecyclePolicy, + highLevelClient().indexLifecycle()::getLifecyclePolicyAsync + ); List retrievedPolicies = Arrays.stream(response.getPolicies().values().toArray()) - .map(p -> ((LifecyclePolicyMetadata) p).getPolicy()).collect(Collectors.toList()); + .map(p -> ((LifecyclePolicyMetadata) p).getPolicy()) + .collect(Collectors.toList()); assertThat(retrievedPolicies, hasItems(policies)); } @@ -281,26 +359,35 @@ public void testRetryLifecycleStep() throws IOException { LifecyclePolicy policy = createRandomPolicy(policyName); ensurePrerequisites(policy); PutLifecyclePolicyRequest putRequest = new PutLifecyclePolicyRequest(policy); - assertAcked(execute(putRequest, highLevelClient().indexLifecycle()::putLifecyclePolicy, - highLevelClient().indexLifecycle()::putLifecyclePolicyAsync)); + assertAcked( + execute( + putRequest, + highLevelClient().indexLifecycle()::putLifecyclePolicy, + highLevelClient().indexLifecycle()::putLifecyclePolicyAsync + ) + ); createIndex("retry", Settings.builder().put("index.lifecycle.name", policy.getName()).build()); RetryLifecyclePolicyRequest retryRequest = new RetryLifecyclePolicyRequest("retry"); - ElasticsearchStatusException ex = expectThrows(ElasticsearchStatusException.class, + ElasticsearchStatusException ex = expectThrows( + ElasticsearchStatusException.class, () -> execute( - retryRequest, highLevelClient().indexLifecycle()::retryLifecyclePolicy, + retryRequest, + highLevelClient().indexLifecycle()::retryLifecyclePolicy, highLevelClient().indexLifecycle()::retryLifecyclePolicyAsync ) ); assertEquals(400, ex.status().getStatus()); assertEquals( - "Elasticsearch exception [type=illegal_argument_exception, reason=cannot retry an action for an index [retry]" + - " that has not encountered an error when running a Lifecycle Policy]", + "Elasticsearch exception [type=illegal_argument_exception, reason=cannot retry an action for an index [retry]" + + " that has not encountered an error when running a Lifecycle Policy]", ex.getRootCause().getMessage() ); } public void ensurePrerequisites(LifecyclePolicy policy) throws IOException { - Set repositories = policy.getPhases().values().stream() + Set repositories = policy.getPhases() + .values() + .stream() .map(phase -> (SearchableSnapshotAction) phase.getActions().get(SearchableSnapshotAction.NAME)) .filter(Objects::nonNull) .map(action -> action.getSnapshotRepository()) @@ -315,7 +402,9 @@ public void ensurePrerequisites(LifecyclePolicy policy) throws IOException { createSnapshotRepo(repository, randomBoolean()); } - Set slmPolicies = policy.getPhases().values().stream() + Set slmPolicies = policy.getPhases() + .values() + .stream() .map(phase -> (WaitForSnapshotAction) phase.getActions().get(WaitForSnapshotAction.NAME)) .filter(Objects::nonNull) .map(action -> action.getPolicy()) @@ -328,27 +417,27 @@ public void ensurePrerequisites(LifecyclePolicy policy) throws IOException { } public static void createSnapshotRepo(String repoName, boolean compress) throws IOException { - PutRepositoryRequest request = new PutRepositoryRequest(repoName) - .type("fs") - .settings(Settings.builder() - .put("compress", compress) - .put("location", System.getProperty("tests.path.repo") + "/" + randomAlphaOfLengthBetween(4, 10)) - .put("max_snapshot_bytes_per_sec", "100m")); - assertTrue(highLevelClient().snapshot() - .createRepository(request, RequestOptions.DEFAULT) - .isAcknowledged()); + PutRepositoryRequest request = new PutRepositoryRequest(repoName).type("fs") + .settings( + Settings.builder() + .put("compress", compress) + .put("location", System.getProperty("tests.path.repo") + "/" + randomAlphaOfLengthBetween(4, 10)) + .put("max_snapshot_bytes_per_sec", "100m") + ); + assertTrue(highLevelClient().snapshot().createRepository(request, RequestOptions.DEFAULT).isAcknowledged()); } private void createSlmPolicy(String slmPolicy, String repo) throws IOException { - PutSnapshotLifecyclePolicyRequest request = new PutSnapshotLifecyclePolicyRequest(new SnapshotLifecyclePolicy( - slmPolicy, - "snap" + randomAlphaOfLengthBetween(5, 10).toLowerCase(Locale.ROOT), - "59 59 23 31 12 ? 2099", - repo, - null, - null)); - assertTrue(highLevelClient().indexLifecycle(). - putSnapshotLifecyclePolicy(request, RequestOptions.DEFAULT) - .isAcknowledged()); + PutSnapshotLifecyclePolicyRequest request = new PutSnapshotLifecyclePolicyRequest( + new SnapshotLifecyclePolicy( + slmPolicy, + "snap" + randomAlphaOfLengthBetween(5, 10).toLowerCase(Locale.ROOT), + "59 59 23 31 12 ? 2099", + repo, + null, + null + ) + ); + assertTrue(highLevelClient().indexLifecycle().putSnapshotLifecyclePolicy(request, RequestOptions.DEFAULT).isAcknowledged()); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndexLifecycleRequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndexLifecycleRequestConvertersTests.java index 3a9ce7bb973fd..0ff0c0d17aabd 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndexLifecycleRequestConvertersTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndexLifecycleRequestConvertersTests.java @@ -68,7 +68,7 @@ public void testPutLifecyclePolicy() throws Exception { } public void testDeleteLifecycle() { - String lifecycleName = randomAlphaOfLengthBetween(2,20); + String lifecycleName = randomAlphaOfLengthBetween(2, 20); DeleteLifecyclePolicyRequest req = new DeleteLifecyclePolicyRequest(lifecycleName); Map expectedParams = new HashMap<>(); setRandomMasterTimeout(req::setMasterTimeout, TimedRequest.DEFAULT_MASTER_NODE_TIMEOUT, expectedParams); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java index 8e7783db95ad1..0201c813b0bb3 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java @@ -135,10 +135,11 @@ public class IndicesClientIT extends ESRestHighLevelClientTestCase { public static final RequestOptions LEGACY_TEMPLATE_OPTIONS = RequestOptions.DEFAULT.toBuilder() - .setWarningsHandler(warnings -> List.of(RestPutIndexTemplateAction.DEPRECATION_WARNING).equals(warnings) == false).build(); + .setWarningsHandler(warnings -> List.of(RestPutIndexTemplateAction.DEPRECATION_WARNING).equals(warnings) == false) + .build(); - public static final String FROZEN_INDICES_DEPRECATION_WARNING = "Frozen indices are deprecated because they provide no benefit given " + - "improvements in heap memory utilization. They will be removed in a future release."; + public static final String FROZEN_INDICES_DEPRECATION_WARNING = "Frozen indices are deprecated because they provide no benefit given " + + "improvements in heap memory utilization. They will be removed in a future release."; public void testIndicesExists() throws IOException { // Index present @@ -148,11 +149,7 @@ public void testIndicesExists() throws IOException { GetIndexRequest request = new GetIndexRequest(indexName); - boolean response = execute( - request, - highLevelClient().indices()::exists, - highLevelClient().indices()::existsAsync - ); + boolean response = execute(request, highLevelClient().indices()::exists, highLevelClient().indices()::existsAsync); assertTrue(response); } @@ -162,11 +159,7 @@ public void testIndicesExists() throws IOException { GetIndexRequest request = new GetIndexRequest(indexName); - boolean response = execute( - request, - highLevelClient().indices()::exists, - highLevelClient().indices()::existsAsync - ); + boolean response = execute(request, highLevelClient().indices()::exists, highLevelClient().indices()::existsAsync); assertFalse(response); } @@ -179,16 +172,12 @@ public void testIndicesExists() throws IOException { GetIndexRequest request = new GetIndexRequest(existingIndex, nonExistentIndex); - boolean response = execute( - request, - highLevelClient().indices()::exists, - highLevelClient().indices()::existsAsync - ); + boolean response = execute(request, highLevelClient().indices()::exists, highLevelClient().indices()::existsAsync); assertFalse(response); } } - @SuppressWarnings({"unchecked", "rawtypes"}) + @SuppressWarnings({ "unchecked", "rawtypes" }) public void testCreateIndex() throws IOException { { // Create index @@ -197,8 +186,11 @@ public void testCreateIndex() throws IOException { CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName); - CreateIndexResponse createIndexResponse = - execute(createIndexRequest, highLevelClient().indices()::create, highLevelClient().indices()::createAsync); + CreateIndexResponse createIndexResponse = execute( + createIndexRequest, + highLevelClient().indices()::create, + highLevelClient().indices()::createAsync + ); assertTrue(createIndexResponse.isAcknowledged()); assertTrue(indexExists(indexName)); @@ -225,15 +217,20 @@ public void testCreateIndex() throws IOException { mappingBuilder.endObject().endObject().endObject(); createIndexRequest.mapping(mappingBuilder); - CreateIndexResponse createIndexResponse = - execute(createIndexRequest, highLevelClient().indices()::create, highLevelClient().indices()::createAsync); + CreateIndexResponse createIndexResponse = execute( + createIndexRequest, + highLevelClient().indices()::create, + highLevelClient().indices()::createAsync + ); assertTrue(createIndexResponse.isAcknowledged()); Map getIndexResponse = getAsMap(indexName); assertEquals("2", XContentMapValues.extractValue(indexName + ".settings.index.number_of_replicas", getIndexResponse)); - Map aliasData = - (Map)XContentMapValues.extractValue(indexName + ".aliases.alias_name", getIndexResponse); + Map aliasData = (Map) XContentMapValues.extractValue( + indexName + ".aliases.alias_name", + getIndexResponse + ); assertNotNull(aliasData); assertEquals("1", aliasData.get("index_routing")); Map filter = (Map) aliasData.get("filter"); @@ -246,23 +243,26 @@ public void testCreateIndex() throws IOException { public void testGetSettings() throws IOException { String indexName = "get_settings_index"; - Settings basicSettings = Settings.builder() - .put("number_of_shards", 1) - .put("number_of_replicas", 0) - .build(); + Settings basicSettings = Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0).build(); createIndex(indexName, basicSettings); GetSettingsRequest getSettingsRequest = new GetSettingsRequest().indices(indexName); - GetSettingsResponse getSettingsResponse = execute(getSettingsRequest, highLevelClient().indices()::getSettings, - highLevelClient().indices()::getSettingsAsync); + GetSettingsResponse getSettingsResponse = execute( + getSettingsRequest, + highLevelClient().indices()::getSettings, + highLevelClient().indices()::getSettingsAsync + ); assertNull(getSettingsResponse.getSetting(indexName, "index.refresh_interval")); assertEquals("1", getSettingsResponse.getSetting(indexName, "index.number_of_shards")); updateIndexSettings(indexName, Settings.builder().put("refresh_interval", "30s")); - GetSettingsResponse updatedResponse = execute(getSettingsRequest, highLevelClient().indices()::getSettings, - highLevelClient().indices()::getSettingsAsync); + GetSettingsResponse updatedResponse = execute( + getSettingsRequest, + highLevelClient().indices()::getSettings, + highLevelClient().indices()::getSettingsAsync + ); assertEquals("30s", updatedResponse.getSetting(indexName, "index.refresh_interval")); } @@ -271,8 +271,10 @@ public void testGetSettingsNonExistentIndex() throws IOException { assertFalse(indexExists(nonExistentIndex)); GetSettingsRequest getSettingsRequest = new GetSettingsRequest().indices(nonExistentIndex); - ElasticsearchException exception = expectThrows(ElasticsearchException.class, - () -> execute(getSettingsRequest, highLevelClient().indices()::getSettings, highLevelClient().indices()::getSettingsAsync)); + ElasticsearchException exception = expectThrows( + ElasticsearchException.class, + () -> execute(getSettingsRequest, highLevelClient().indices()::getSettings, highLevelClient().indices()::getSettingsAsync) + ); assertEquals(RestStatus.NOT_FOUND, exception.status()); } @@ -284,8 +286,11 @@ public void testGetSettingsFromMultipleIndices() throws IOException { createIndex(indexName2, Settings.builder().put("number_of_shards", 3).build()); GetSettingsRequest getSettingsRequest = new GetSettingsRequest().indices("get_multiple_settings*"); - GetSettingsResponse getSettingsResponse = execute(getSettingsRequest, highLevelClient().indices()::getSettings, - highLevelClient().indices()::getSettingsAsync); + GetSettingsResponse getSettingsResponse = execute( + getSettingsRequest, + highLevelClient().indices()::getSettings, + highLevelClient().indices()::getSettingsAsync + ); assertEquals("2", getSettingsResponse.getSetting(indexName1, "index.number_of_shards")); assertEquals("3", getSettingsResponse.getSetting(indexName2, "index.number_of_shards")); @@ -293,15 +298,15 @@ public void testGetSettingsFromMultipleIndices() throws IOException { public void testGetSettingsFiltered() throws IOException { String indexName = "get_settings_index"; - Settings basicSettings = Settings.builder() - .put("number_of_shards", 1) - .put("number_of_replicas", 0) - .build(); + Settings basicSettings = Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0).build(); createIndex(indexName, basicSettings); GetSettingsRequest getSettingsRequest = new GetSettingsRequest().indices(indexName).names("index.number_of_shards"); - GetSettingsResponse getSettingsResponse = execute(getSettingsRequest, highLevelClient().indices()::getSettings, - highLevelClient().indices()::getSettingsAsync); + GetSettingsResponse getSettingsResponse = execute( + getSettingsRequest, + highLevelClient().indices()::getSettings, + highLevelClient().indices()::getSettingsAsync + ); assertNull(getSettingsResponse.getSetting(indexName, "index.number_of_replicas")); assertEquals("1", getSettingsResponse.getSetting(indexName, "index.number_of_shards")); @@ -310,36 +315,37 @@ public void testGetSettingsFiltered() throws IOException { public void testGetSettingsWithDefaults() throws IOException { String indexName = "get_settings_index"; - Settings basicSettings = Settings.builder() - .put("number_of_shards", 1) - .put("number_of_replicas", 0) - .build(); + Settings basicSettings = Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0).build(); createIndex(indexName, basicSettings); GetSettingsRequest getSettingsRequest = new GetSettingsRequest().indices(indexName).includeDefaults(true); - GetSettingsResponse getSettingsResponse = execute(getSettingsRequest, highLevelClient().indices()::getSettings, - highLevelClient().indices()::getSettingsAsync); + GetSettingsResponse getSettingsResponse = execute( + getSettingsRequest, + highLevelClient().indices()::getSettings, + highLevelClient().indices()::getSettingsAsync + ); assertNotNull(getSettingsResponse.getSetting(indexName, "index.refresh_interval")); - assertEquals(IndexSettings.DEFAULT_REFRESH_INTERVAL, - getSettingsResponse.getIndexToDefaultSettings().get("get_settings_index").getAsTime("index.refresh_interval", null)); + assertEquals( + IndexSettings.DEFAULT_REFRESH_INTERVAL, + getSettingsResponse.getIndexToDefaultSettings().get("get_settings_index").getAsTime("index.refresh_interval", null) + ); assertEquals("1", getSettingsResponse.getSetting(indexName, "index.number_of_shards")); } public void testGetSettingsWithDefaultsFiltered() throws IOException { String indexName = "get_settings_index"; - Settings basicSettings = Settings.builder() - .put("number_of_shards", 1) - .put("number_of_replicas", 0) - .build(); + Settings basicSettings = Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0).build(); createIndex(indexName, basicSettings); - GetSettingsRequest getSettingsRequest = new GetSettingsRequest() - .indices(indexName) + GetSettingsRequest getSettingsRequest = new GetSettingsRequest().indices(indexName) .names("index.refresh_interval") .includeDefaults(true); - GetSettingsResponse getSettingsResponse = execute(getSettingsRequest, highLevelClient().indices()::getSettings, - highLevelClient().indices()::getSettingsAsync); + GetSettingsResponse getSettingsResponse = execute( + getSettingsRequest, + highLevelClient().indices()::getSettings, + highLevelClient().indices()::getSettingsAsync + ); assertNull(getSettingsResponse.getSetting(indexName, "index.number_of_replicas")); assertNull(getSettingsResponse.getSetting(indexName, "index.number_of_shards")); @@ -350,16 +356,16 @@ public void testGetSettingsWithDefaultsFiltered() throws IOException { @SuppressWarnings("unchecked") public void testGetIndex() throws IOException { String indexName = "get_index_test"; - Settings basicSettings = Settings.builder() - .put(SETTING_NUMBER_OF_SHARDS, 1) - .put(SETTING_NUMBER_OF_REPLICAS, 0) - .build(); + Settings basicSettings = Settings.builder().put(SETTING_NUMBER_OF_SHARDS, 1).put(SETTING_NUMBER_OF_REPLICAS, 0).build(); String mappings = "\"properties\":{\"field-1\":{\"type\":\"integer\"}}"; createIndex(indexName, basicSettings, mappings); GetIndexRequest getIndexRequest = new GetIndexRequest(indexName).includeDefaults(false); - GetIndexResponse getIndexResponse = - execute(getIndexRequest, highLevelClient().indices()::get, highLevelClient().indices()::getAsync); + GetIndexResponse getIndexResponse = execute( + getIndexRequest, + highLevelClient().indices()::get, + highLevelClient().indices()::getAsync + ); // default settings should be null assertNull(getIndexResponse.getSetting(indexName, "index.refresh_interval")); @@ -373,9 +379,9 @@ public void testGetIndex() throws IOException { assertEquals("{\"properties\":{\"field-1\":{\"type\":\"integer\"}}}", mappingMetadata.source().string()); Object o = mappingMetadata.getSourceAsMap().get("properties"); assertThat(o, instanceOf(Map.class)); - //noinspection unchecked + // noinspection unchecked assertThat(((Map) o).get("field-1"), instanceOf(Map.class)); - //noinspection unchecked + // noinspection unchecked Map fieldMapping = (Map) ((Map) o).get("field-1"); assertEquals("integer", fieldMapping.get("type")); } @@ -383,20 +389,22 @@ public void testGetIndex() throws IOException { @SuppressWarnings("unchecked") public void testGetIndexWithDefaults() throws IOException { String indexName = "get_index_test"; - Settings basicSettings = Settings.builder() - .put(SETTING_NUMBER_OF_SHARDS, 1) - .put(SETTING_NUMBER_OF_REPLICAS, 0) - .build(); + Settings basicSettings = Settings.builder().put(SETTING_NUMBER_OF_SHARDS, 1).put(SETTING_NUMBER_OF_REPLICAS, 0).build(); String mappings = "\"properties\":{\"field-1\":{\"type\":\"integer\"}}"; createIndex(indexName, basicSettings, mappings); GetIndexRequest getIndexRequest = new GetIndexRequest(indexName).includeDefaults(true); - GetIndexResponse getIndexResponse = - execute(getIndexRequest, highLevelClient().indices()::get, highLevelClient().indices()::getAsync); + GetIndexResponse getIndexResponse = execute( + getIndexRequest, + highLevelClient().indices()::get, + highLevelClient().indices()::getAsync + ); assertNotNull(getIndexResponse.getSetting(indexName, "index.refresh_interval")); - assertEquals(IndexSettings.DEFAULT_REFRESH_INTERVAL, - getIndexResponse.getDefaultSettings().get(indexName).getAsTime("index.refresh_interval", null)); + assertEquals( + IndexSettings.DEFAULT_REFRESH_INTERVAL, + getIndexResponse.getDefaultSettings().get(indexName).getAsTime("index.refresh_interval", null) + ); assertEquals("1", getIndexResponse.getSetting(indexName, SETTING_NUMBER_OF_SHARDS)); assertEquals("0", getIndexResponse.getSetting(indexName, SETTING_NUMBER_OF_REPLICAS)); assertNotNull(getIndexResponse.getMappings().get(indexName)); @@ -413,8 +421,10 @@ public void testGetIndexNonExistentIndex() throws IOException { assertFalse(indexExists(nonExistentIndex)); GetIndexRequest getIndexRequest = new GetIndexRequest(nonExistentIndex); - ElasticsearchException exception = expectThrows(ElasticsearchException.class, - () -> execute(getIndexRequest, highLevelClient().indices()::get, highLevelClient().indices()::getAsync)); + ElasticsearchException exception = expectThrows( + ElasticsearchException.class, + () -> execute(getIndexRequest, highLevelClient().indices()::get, highLevelClient().indices()::getAsync) + ); assertEquals(RestStatus.NOT_FOUND, exception.status()); } @@ -429,14 +439,15 @@ public void testPutMapping() throws IOException { mappingBuilder.endObject().endObject().endObject(); putMappingRequest.source(mappingBuilder); - AcknowledgedResponse putMappingResponse = execute(putMappingRequest, + AcknowledgedResponse putMappingResponse = execute( + putMappingRequest, highLevelClient().indices()::putMapping, - highLevelClient().indices()::putMappingAsync); + highLevelClient().indices()::putMappingAsync + ); assertTrue(putMappingResponse.isAcknowledged()); Map getIndexResponse = getAsMap(indexName); - assertEquals("text", XContentMapValues.extractValue(indexName + ".mappings.properties.field.type", - getIndexResponse)); + assertEquals("text", XContentMapValues.extractValue(indexName + ".mappings.properties.field.type", getIndexResponse)); } public void testGetMapping() throws IOException { @@ -450,9 +461,11 @@ public void testGetMapping() throws IOException { mappingBuilder.endObject().endObject().endObject(); putMappingRequest.source(mappingBuilder); - AcknowledgedResponse putMappingResponse = execute(putMappingRequest, + AcknowledgedResponse putMappingResponse = execute( + putMappingRequest, highLevelClient().indices()::putMapping, - highLevelClient().indices()::putMappingAsync); + highLevelClient().indices()::putMappingAsync + ); assertTrue(putMappingResponse.isAcknowledged()); Map getIndexResponse = getAsMap(indexName); @@ -463,7 +476,8 @@ public void testGetMapping() throws IOException { GetMappingsResponse getMappingsResponse = execute( request, highLevelClient().indices()::getMapping, - highLevelClient().indices()::getMappingAsync); + highLevelClient().indices()::getMappingAsync + ); Map mappings = getMappingsResponse.mappings().get(indexName).sourceAsMap(); Map type = new HashMap<>(); @@ -486,25 +500,28 @@ public void testGetFieldMapping() throws IOException { mappingBuilder.endObject().endObject().endObject(); putMappingRequest.source(mappingBuilder); - AcknowledgedResponse putMappingResponse = - execute(putMappingRequest, highLevelClient().indices()::putMapping, highLevelClient().indices()::putMappingAsync); + AcknowledgedResponse putMappingResponse = execute( + putMappingRequest, + highLevelClient().indices()::putMapping, + highLevelClient().indices()::putMappingAsync + ); assertTrue(putMappingResponse.isAcknowledged()); - GetFieldMappingsRequest getFieldMappingsRequest = new GetFieldMappingsRequest() - .indices(indexName) - .fields("field"); + GetFieldMappingsRequest getFieldMappingsRequest = new GetFieldMappingsRequest().indices(indexName).fields("field"); - GetFieldMappingsResponse getFieldMappingsResponse = - execute(getFieldMappingsRequest, - highLevelClient().indices()::getFieldMapping, - highLevelClient().indices()::getFieldMappingAsync); + GetFieldMappingsResponse getFieldMappingsResponse = execute( + getFieldMappingsRequest, + highLevelClient().indices()::getFieldMapping, + highLevelClient().indices()::getFieldMappingAsync + ); - final Map fieldMappingMap = - getFieldMappingsResponse.mappings().get(indexName); + final Map fieldMappingMap = getFieldMappingsResponse.mappings() + .get(indexName); - final GetFieldMappingsResponse.FieldMappingMetadata metadata = - new GetFieldMappingsResponse.FieldMappingMetadata("field", - new BytesArray("{\"field\":{\"type\":\"text\"}}")); + final GetFieldMappingsResponse.FieldMappingMetadata metadata = new GetFieldMappingsResponse.FieldMappingMetadata( + "field", + new BytesArray("{\"field\":{\"type\":\"text\"}}") + ); assertThat(fieldMappingMap, equalTo(Collections.singletonMap("field", metadata))); } @@ -515,8 +532,11 @@ public void testDeleteIndex() throws IOException { createIndex(indexName, Settings.EMPTY); DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(indexName); - AcknowledgedResponse deleteIndexResponse = - execute(deleteIndexRequest, highLevelClient().indices()::delete, highLevelClient().indices()::deleteAsync); + AcknowledgedResponse deleteIndexResponse = execute( + deleteIndexRequest, + highLevelClient().indices()::delete, + highLevelClient().indices()::deleteAsync + ); assertTrue(deleteIndexResponse.isAcknowledged()); assertFalse(indexExists(indexName)); @@ -528,8 +548,10 @@ public void testDeleteIndex() throws IOException { DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(nonExistentIndex); - ElasticsearchException exception = expectThrows(ElasticsearchException.class, - () -> execute(deleteIndexRequest, highLevelClient().indices()::delete, highLevelClient().indices()::deleteAsync)); + ElasticsearchException exception = expectThrows( + ElasticsearchException.class, + () -> execute(deleteIndexRequest, highLevelClient().indices()::delete, highLevelClient().indices()::deleteAsync) + ); assertEquals(RestStatus.NOT_FOUND, exception.status()); } } @@ -550,8 +572,11 @@ public void testUpdateAliases() throws IOException { } addAction.routing("routing").searchRouting("search_routing").filter("{\"term\":{\"year\":2016}}"); aliasesAddRequest.addAliasAction(addAction); - AcknowledgedResponse aliasesAddResponse = execute(aliasesAddRequest, highLevelClient().indices()::updateAliases, - highLevelClient().indices()::updateAliasesAsync); + AcknowledgedResponse aliasesAddResponse = execute( + aliasesAddRequest, + highLevelClient().indices()::updateAliases, + highLevelClient().indices()::updateAliasesAsync + ); assertTrue(aliasesAddResponse.isAcknowledged()); assertThat(aliasExists(alias), equalTo(true)); assertThat(aliasExists(index, alias), equalTo(true)); @@ -570,8 +595,11 @@ public void testUpdateAliases() throws IOException { aliasesAddRemoveRequest.addAliasAction(addAction); AliasActions removeAction = new AliasActions(AliasActions.Type.REMOVE).index(index).alias(alias); aliasesAddRemoveRequest.addAliasAction(removeAction); - AcknowledgedResponse aliasesAddRemoveResponse = execute(aliasesAddRemoveRequest, highLevelClient().indices()::updateAliases, - highLevelClient().indices()::updateAliasesAsync); + AcknowledgedResponse aliasesAddRemoveResponse = execute( + aliasesAddRemoveRequest, + highLevelClient().indices()::updateAliases, + highLevelClient().indices()::updateAliasesAsync + ); assertTrue(aliasesAddRemoveResponse.isAcknowledged()); assertThat(aliasExists(alias), equalTo(false)); assertThat(aliasExists(alias2), equalTo(true)); @@ -581,8 +609,11 @@ public void testUpdateAliases() throws IOException { IndicesAliasesRequest aliasesRemoveIndexRequest = new IndicesAliasesRequest(); AliasActions removeIndexAction = new AliasActions(AliasActions.Type.REMOVE_INDEX).index(index); aliasesRemoveIndexRequest.addAliasAction(removeIndexAction); - AcknowledgedResponse aliasesRemoveIndexResponse = execute(aliasesRemoveIndexRequest, highLevelClient().indices()::updateAliases, - highLevelClient().indices()::updateAliasesAsync); + AcknowledgedResponse aliasesRemoveIndexResponse = execute( + aliasesRemoveIndexRequest, + highLevelClient().indices()::updateAliases, + highLevelClient().indices()::updateAliasesAsync + ); assertTrue(aliasesRemoveIndexResponse.isAcknowledged()); assertThat(aliasExists(alias), equalTo(false)); assertThat(aliasExists(alias2), equalTo(false)); @@ -598,22 +629,34 @@ public void testAliasesNonExistentIndex() throws IOException { IndicesAliasesRequest nonExistentIndexRequest = new IndicesAliasesRequest(); nonExistentIndexRequest.addAliasAction(new AliasActions(AliasActions.Type.ADD).index(nonExistentIndex).alias(alias)); - ElasticsearchException exception = expectThrows(ElasticsearchException.class, () -> execute(nonExistentIndexRequest, - highLevelClient().indices()::updateAliases, highLevelClient().indices()::updateAliasesAsync)); + ElasticsearchException exception = expectThrows( + ElasticsearchException.class, + () -> execute( + nonExistentIndexRequest, + highLevelClient().indices()::updateAliases, + highLevelClient().indices()::updateAliasesAsync + ) + ); assertThat(exception.status(), equalTo(RestStatus.NOT_FOUND)); - assertThat(exception.getMessage(), - equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index [non_existent_index]]")); + assertThat( + exception.getMessage(), + equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index [non_existent_index]]") + ); assertThat(exception.getMetadata("es.index"), hasItem(nonExistentIndex)); createIndex(index, Settings.EMPTY); IndicesAliasesRequest mixedRequest = new IndicesAliasesRequest(); mixedRequest.addAliasAction(new AliasActions(AliasActions.Type.ADD).indices(index).aliases(alias)); mixedRequest.addAliasAction(new AliasActions(AliasActions.Type.REMOVE).indices(nonExistentIndex).alias(alias)); - exception = expectThrows(ElasticsearchStatusException.class, - () -> execute(mixedRequest, highLevelClient().indices()::updateAliases, highLevelClient().indices()::updateAliasesAsync)); + exception = expectThrows( + ElasticsearchStatusException.class, + () -> execute(mixedRequest, highLevelClient().indices()::updateAliases, highLevelClient().indices()::updateAliasesAsync) + ); assertThat(exception.status(), equalTo(RestStatus.NOT_FOUND)); - assertThat(exception.getMessage(), - equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index [non_existent_index]]")); + assertThat( + exception.getMessage(), + equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index [non_existent_index]]") + ); assertThat(exception.getMetadata("es.index"), hasItem(nonExistentIndex)); assertThat(exception.getMetadata("es.index"), not(hasItem(index))); assertThat(aliasExists(index, alias), equalTo(false)); @@ -622,11 +665,15 @@ public void testAliasesNonExistentIndex() throws IOException { IndicesAliasesRequest removeIndexRequest = new IndicesAliasesRequest(); removeIndexRequest.addAliasAction(new AliasActions(AliasActions.Type.ADD).index(nonExistentIndex).alias(alias)); removeIndexRequest.addAliasAction(new AliasActions(AliasActions.Type.REMOVE_INDEX).indices(nonExistentIndex)); - exception = expectThrows(ElasticsearchException.class, () -> execute(removeIndexRequest, highLevelClient().indices()::updateAliases, - highLevelClient().indices()::updateAliasesAsync)); + exception = expectThrows( + ElasticsearchException.class, + () -> execute(removeIndexRequest, highLevelClient().indices()::updateAliases, highLevelClient().indices()::updateAliasesAsync) + ); assertThat(exception.status(), equalTo(RestStatus.NOT_FOUND)); - assertThat(exception.getMessage(), - equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index [non_existent_index]]")); + assertThat( + exception.getMessage(), + equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index [non_existent_index]]") + ); assertThat(exception.getMetadata("es.index"), hasItem(nonExistentIndex)); assertThat(exception.getMetadata("es.index"), not(hasItem(index))); assertThat(aliasExists(index, alias), equalTo(false)); @@ -637,14 +684,19 @@ public void testOpenExistingIndex() throws IOException { String index = "index"; createIndex(index, Settings.EMPTY); closeIndex(index); - ResponseException exception = expectThrows(ResponseException.class, - () -> client().performRequest(new Request(HttpGet.METHOD_NAME, index + "/_search"))); + ResponseException exception = expectThrows( + ResponseException.class, + () -> client().performRequest(new Request(HttpGet.METHOD_NAME, index + "/_search")) + ); assertThat(exception.getResponse().getStatusLine().getStatusCode(), equalTo(RestStatus.BAD_REQUEST.getStatus())); assertThat(exception.getMessage().contains(index), equalTo(true)); OpenIndexRequest openIndexRequest = new OpenIndexRequest(index); - OpenIndexResponse openIndexResponse = execute(openIndexRequest, highLevelClient().indices()::open, - highLevelClient().indices()::openAsync); + OpenIndexResponse openIndexResponse = execute( + openIndexRequest, + highLevelClient().indices()::open, + highLevelClient().indices()::openAsync + ); assertTrue(openIndexResponse.isAcknowledged()); Response response = client().performRequest(new Request(HttpGet.METHOD_NAME, index + "/_search")); @@ -656,21 +708,33 @@ public void testOpenNonExistentIndex() throws IOException { assertFalse(indexExists(nonExistentIndex)); OpenIndexRequest openIndexRequest = new OpenIndexRequest(nonExistentIndex); - ElasticsearchException exception = expectThrows(ElasticsearchException.class, - () -> execute(openIndexRequest, highLevelClient().indices()::open, highLevelClient().indices()::openAsync)); + ElasticsearchException exception = expectThrows( + ElasticsearchException.class, + () -> execute(openIndexRequest, highLevelClient().indices()::open, highLevelClient().indices()::openAsync) + ); assertEquals(RestStatus.NOT_FOUND, exception.status()); OpenIndexRequest lenientOpenIndexRequest = new OpenIndexRequest(nonExistentIndex); lenientOpenIndexRequest.indicesOptions(IndicesOptions.lenientExpandOpen()); - OpenIndexResponse lenientOpenIndexResponse = execute(lenientOpenIndexRequest, highLevelClient().indices()::open, - highLevelClient().indices()::openAsync, IGNORE_THROTTLED_WARNING); + OpenIndexResponse lenientOpenIndexResponse = execute( + lenientOpenIndexRequest, + highLevelClient().indices()::open, + highLevelClient().indices()::openAsync, + IGNORE_THROTTLED_WARNING + ); assertThat(lenientOpenIndexResponse.isAcknowledged(), equalTo(true)); OpenIndexRequest strictOpenIndexRequest = new OpenIndexRequest(nonExistentIndex); strictOpenIndexRequest.indicesOptions(IndicesOptions.strictExpandOpen()); - ElasticsearchException strictException = expectThrows(ElasticsearchException.class, - () -> execute(openIndexRequest, highLevelClient().indices()::open, - highLevelClient().indices()::openAsync, IGNORE_THROTTLED_WARNING)); + ElasticsearchException strictException = expectThrows( + ElasticsearchException.class, + () -> execute( + openIndexRequest, + highLevelClient().indices()::open, + highLevelClient().indices()::openAsync, + IGNORE_THROTTLED_WARNING + ) + ); assertEquals(RestStatus.NOT_FOUND, strictException.status()); } @@ -683,8 +747,11 @@ public void testCloseExistingIndex() throws IOException { } CloseIndexRequest closeIndexRequest = new CloseIndexRequest(indices); - CloseIndexResponse closeIndexResponse = execute(closeIndexRequest, - highLevelClient().indices()::close, highLevelClient().indices()::closeAsync); + CloseIndexResponse closeIndexResponse = execute( + closeIndexRequest, + highLevelClient().indices()::close, + highLevelClient().indices()::closeAsync + ); assertTrue(closeIndexResponse.isAcknowledged()); assertTrue(closeIndexResponse.isShardsAcknowledged()); assertThat(closeIndexResponse.getIndices(), notNullValue()); @@ -693,8 +760,10 @@ public void testCloseExistingIndex() throws IOException { assertThat(indexResult.getIndex(), startsWith("index-")); assertThat(indexResult.hasFailures(), is(false)); - ResponseException exception = expectThrows(ResponseException.class, - () -> client().performRequest(new Request(HttpGet.METHOD_NAME, indexResult.getIndex() + "/_search"))); + ResponseException exception = expectThrows( + ResponseException.class, + () -> client().performRequest(new Request(HttpGet.METHOD_NAME, indexResult.getIndex() + "/_search")) + ); assertThat(exception.getResponse().getStatusLine().getStatusCode(), equalTo(RestStatus.BAD_REQUEST.getStatus())); assertThat(exception.getMessage().contains(indexResult.getIndex()), equalTo(true)); }); @@ -705,30 +774,34 @@ public void testCloseNonExistentIndex() throws IOException { assertFalse(indexExists(nonExistentIndex)); CloseIndexRequest closeIndexRequest = new CloseIndexRequest(nonExistentIndex); - ElasticsearchException exception = expectThrows(ElasticsearchException.class, - () -> execute(closeIndexRequest, highLevelClient().indices()::close, highLevelClient().indices()::closeAsync)); + ElasticsearchException exception = expectThrows( + ElasticsearchException.class, + () -> execute(closeIndexRequest, highLevelClient().indices()::close, highLevelClient().indices()::closeAsync) + ); assertEquals(RestStatus.NOT_FOUND, exception.status()); } public void testCloseEmptyOrNullIndex() { String[] indices = randomBoolean() ? Strings.EMPTY_ARRAY : null; CloseIndexRequest closeIndexRequest = new CloseIndexRequest(indices); - org.elasticsearch.client.ValidationException exception = expectThrows(org.elasticsearch.client.ValidationException.class, - () -> execute(closeIndexRequest, highLevelClient().indices()::close, highLevelClient().indices()::closeAsync)); + org.elasticsearch.client.ValidationException exception = expectThrows( + org.elasticsearch.client.ValidationException.class, + () -> execute(closeIndexRequest, highLevelClient().indices()::close, highLevelClient().indices()::closeAsync) + ); assertThat(exception.validationErrors().get(0), equalTo("index is missing")); } public void testRefresh() throws IOException { { String index = "index"; - Settings settings = Settings.builder() - .put("number_of_shards", 1) - .put("number_of_replicas", 0) - .build(); + Settings settings = Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0).build(); createIndex(index, settings); RefreshRequest refreshRequest = new RefreshRequest(index); - RefreshResponse refreshResponse = - execute(refreshRequest, highLevelClient().indices()::refresh, highLevelClient().indices()::refreshAsync); + RefreshResponse refreshResponse = execute( + refreshRequest, + highLevelClient().indices()::refresh, + highLevelClient().indices()::refreshAsync + ); assertThat(refreshResponse.getTotalShards(), equalTo(1)); assertThat(refreshResponse.getSuccessfulShards(), equalTo(1)); assertThat(refreshResponse.getFailedShards(), equalTo(0)); @@ -738,8 +811,10 @@ public void testRefresh() throws IOException { String nonExistentIndex = "non_existent_index"; assertFalse(indexExists(nonExistentIndex)); RefreshRequest refreshRequest = new RefreshRequest(nonExistentIndex); - ElasticsearchException exception = expectThrows(ElasticsearchException.class, - () -> execute(refreshRequest, highLevelClient().indices()::refresh, highLevelClient().indices()::refreshAsync)); + ElasticsearchException exception = expectThrows( + ElasticsearchException.class, + () -> execute(refreshRequest, highLevelClient().indices()::refresh, highLevelClient().indices()::refreshAsync) + ); assertEquals(RestStatus.NOT_FOUND, exception.status()); } } @@ -747,14 +822,14 @@ public void testRefresh() throws IOException { public void testFlush() throws IOException { { String index = "index"; - Settings settings = Settings.builder() - .put("number_of_shards", 1) - .put("number_of_replicas", 0) - .build(); + Settings settings = Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0).build(); createIndex(index, settings); FlushRequest flushRequest = new FlushRequest(index); - FlushResponse flushResponse = - execute(flushRequest, highLevelClient().indices()::flush, highLevelClient().indices()::flushAsync); + FlushResponse flushResponse = execute( + flushRequest, + highLevelClient().indices()::flush, + highLevelClient().indices()::flushAsync + ); assertThat(flushResponse.getTotalShards(), equalTo(1)); assertThat(flushResponse.getSuccessfulShards(), equalTo(1)); assertThat(flushResponse.getFailedShards(), equalTo(0)); @@ -764,8 +839,10 @@ public void testFlush() throws IOException { String nonExistentIndex = "non_existent_index"; assertFalse(indexExists(nonExistentIndex)); FlushRequest flushRequest = new FlushRequest(nonExistentIndex); - ElasticsearchException exception = expectThrows(ElasticsearchException.class, - () -> execute(flushRequest, highLevelClient().indices()::flush, highLevelClient().indices()::flushAsync)); + ElasticsearchException exception = expectThrows( + ElasticsearchException.class, + () -> execute(flushRequest, highLevelClient().indices()::flush, highLevelClient().indices()::flushAsync) + ); assertEquals(RestStatus.NOT_FOUND, exception.status()); } } @@ -773,14 +850,14 @@ public void testFlush() throws IOException { public void testClearCache() throws IOException { { String index = "index"; - Settings settings = Settings.builder() - .put("number_of_shards", 1) - .put("number_of_replicas", 0) - .build(); + Settings settings = Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0).build(); createIndex(index, settings); ClearIndicesCacheRequest clearCacheRequest = new ClearIndicesCacheRequest(index); - ClearIndicesCacheResponse clearCacheResponse = - execute(clearCacheRequest, highLevelClient().indices()::clearCache, highLevelClient().indices()::clearCacheAsync); + ClearIndicesCacheResponse clearCacheResponse = execute( + clearCacheRequest, + highLevelClient().indices()::clearCache, + highLevelClient().indices()::clearCacheAsync + ); assertThat(clearCacheResponse.getTotalShards(), equalTo(1)); assertThat(clearCacheResponse.getSuccessfulShards(), equalTo(1)); assertThat(clearCacheResponse.getFailedShards(), equalTo(0)); @@ -790,9 +867,10 @@ public void testClearCache() throws IOException { String nonExistentIndex = "non_existent_index"; assertFalse(indexExists(nonExistentIndex)); ClearIndicesCacheRequest clearCacheRequest = new ClearIndicesCacheRequest(nonExistentIndex); - ElasticsearchException exception = expectThrows(ElasticsearchException.class, - () -> execute(clearCacheRequest, highLevelClient().indices()::clearCache, - highLevelClient().indices()::clearCacheAsync)); + ElasticsearchException exception = expectThrows( + ElasticsearchException.class, + () -> execute(clearCacheRequest, highLevelClient().indices()::clearCache, highLevelClient().indices()::clearCacheAsync) + ); assertEquals(RestStatus.NOT_FOUND, exception.status()); } } @@ -800,14 +878,14 @@ public void testClearCache() throws IOException { public void testForceMerge() throws IOException { { String index = "index"; - Settings settings = Settings.builder() - .put("number_of_shards", 1) - .put("number_of_replicas", 0) - .build(); + Settings settings = Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0).build(); createIndex(index, settings); ForceMergeRequest forceMergeRequest = new ForceMergeRequest(index); - ForceMergeResponse forceMergeResponse = - execute(forceMergeRequest, highLevelClient().indices()::forcemerge, highLevelClient().indices()::forcemergeAsync); + ForceMergeResponse forceMergeResponse = execute( + forceMergeRequest, + highLevelClient().indices()::forcemerge, + highLevelClient().indices()::forcemergeAsync + ); assertThat(forceMergeResponse.getTotalShards(), equalTo(1)); assertThat(forceMergeResponse.getSuccessfulShards(), equalTo(1)); assertThat(forceMergeResponse.getFailedShards(), equalTo(0)); @@ -819,8 +897,10 @@ public void testForceMerge() throws IOException { String nonExistentIndex = "non_existent_index"; assertFalse(indexExists(nonExistentIndex)); ForceMergeRequest forceMergeRequest = new ForceMergeRequest(nonExistentIndex); - ElasticsearchException exception = expectThrows(ElasticsearchException.class, - () -> execute(forceMergeRequest, highLevelClient().indices()::forcemerge, highLevelClient().indices()::forcemergeAsync)); + ElasticsearchException exception = expectThrows( + ElasticsearchException.class, + () -> execute(forceMergeRequest, highLevelClient().indices()::forcemerge, highLevelClient().indices()::forcemergeAsync) + ); assertEquals(RestStatus.NOT_FOUND, exception.status()); assertThat(forceMergeRequest.getDescription(), containsString(nonExistentIndex)); @@ -848,78 +928,97 @@ public void testShrink() throws IOException { Map nodes = getAsMap("_nodes"); String firstNode = ((Map) nodes.get("nodes")).keySet().iterator().next(); createIndex("source", Settings.builder().put("index.number_of_shards", 4).put("index.number_of_replicas", 0).build()); - updateIndexSettings("source", Settings.builder().put("index.routing.allocation.require._name", firstNode) - .put("index.blocks.write", true)); + updateIndexSettings( + "source", + Settings.builder().put("index.routing.allocation.require._name", firstNode).put("index.blocks.write", true) + ); ResizeRequest resizeRequest = new ResizeRequest("target", "source"); resizeRequest.setResizeType(ResizeType.SHRINK); - Settings targetSettings = - Settings.builder() - .put("index.number_of_shards", 2) - .put("index.number_of_replicas", 0) - .putNull("index.routing.allocation.require._name") - .build(); - resizeRequest.setTargetIndex(new org.elasticsearch.action.admin.indices.create.CreateIndexRequest("target") - .settings(targetSettings) - .alias(new Alias("alias"))); - ResizeResponse resizeResponse = execute(resizeRequest, highLevelClient().indices()::shrink, - highLevelClient().indices()::shrinkAsync); + Settings targetSettings = Settings.builder() + .put("index.number_of_shards", 2) + .put("index.number_of_replicas", 0) + .putNull("index.routing.allocation.require._name") + .build(); + resizeRequest.setTargetIndex( + new org.elasticsearch.action.admin.indices.create.CreateIndexRequest("target").settings(targetSettings) + .alias(new Alias("alias")) + ); + ResizeResponse resizeResponse = execute( + resizeRequest, + highLevelClient().indices()::shrink, + highLevelClient().indices()::shrinkAsync + ); assertTrue(resizeResponse.isAcknowledged()); assertTrue(resizeResponse.isShardsAcknowledged()); Map getIndexResponse = getAsMap("target"); - Map indexSettings = (Map)XContentMapValues.extractValue("target.settings.index", getIndexResponse); + Map indexSettings = (Map) XContentMapValues.extractValue("target.settings.index", getIndexResponse); assertNotNull(indexSettings); assertEquals("2", indexSettings.get("number_of_shards")); assertEquals("0", indexSettings.get("number_of_replicas")); - Map aliasData = (Map)XContentMapValues.extractValue("target.aliases.alias", getIndexResponse); + Map aliasData = (Map) XContentMapValues.extractValue("target.aliases.alias", getIndexResponse); assertNotNull(aliasData); } @SuppressWarnings("unchecked") public void testSplit() throws IOException { - createIndex("source", Settings.builder().put("index.number_of_shards", 2).put("index.number_of_replicas", 0) - .put("index.number_of_routing_shards", 4).build()); + createIndex( + "source", + Settings.builder() + .put("index.number_of_shards", 2) + .put("index.number_of_replicas", 0) + .put("index.number_of_routing_shards", 4) + .build() + ); updateIndexSettings("source", Settings.builder().put("index.blocks.write", true)); ResizeRequest resizeRequest = new ResizeRequest("target", "source"); resizeRequest.setResizeType(ResizeType.SPLIT); Settings targetSettings = Settings.builder().put("index.number_of_shards", 4).put("index.number_of_replicas", 0).build(); - resizeRequest.setTargetIndex(new org.elasticsearch.action.admin.indices.create.CreateIndexRequest("target") - .settings(targetSettings) - .alias(new Alias("alias"))); + resizeRequest.setTargetIndex( + new org.elasticsearch.action.admin.indices.create.CreateIndexRequest("target").settings(targetSettings) + .alias(new Alias("alias")) + ); ResizeResponse resizeResponse = execute(resizeRequest, highLevelClient().indices()::split, highLevelClient().indices()::splitAsync); assertTrue(resizeResponse.isAcknowledged()); assertTrue(resizeResponse.isShardsAcknowledged()); Map getIndexResponse = getAsMap("target"); - Map indexSettings = (Map)XContentMapValues.extractValue("target.settings.index", getIndexResponse); + Map indexSettings = (Map) XContentMapValues.extractValue("target.settings.index", getIndexResponse); assertNotNull(indexSettings); assertEquals("4", indexSettings.get("number_of_shards")); assertEquals("0", indexSettings.get("number_of_replicas")); - Map aliasData = (Map)XContentMapValues.extractValue("target.aliases.alias", getIndexResponse); + Map aliasData = (Map) XContentMapValues.extractValue("target.aliases.alias", getIndexResponse); assertNotNull(aliasData); } @SuppressWarnings("unchecked") public void testClone() throws IOException { - createIndex("source", Settings.builder().put("index.number_of_shards", 2).put("index.number_of_replicas", 0) - .put("index.number_of_routing_shards", 4).build()); + createIndex( + "source", + Settings.builder() + .put("index.number_of_shards", 2) + .put("index.number_of_replicas", 0) + .put("index.number_of_routing_shards", 4) + .build() + ); updateIndexSettings("source", Settings.builder().put("index.blocks.write", true)); ResizeRequest resizeRequest = new ResizeRequest("target", "source"); resizeRequest.setResizeType(ResizeType.CLONE); Settings targetSettings = Settings.builder().put("index.number_of_shards", 2).put("index.number_of_replicas", 0).build(); - resizeRequest.setTargetIndex(new org.elasticsearch.action.admin.indices.create.CreateIndexRequest("target") - .settings(targetSettings) - .alias(new Alias("alias"))); + resizeRequest.setTargetIndex( + new org.elasticsearch.action.admin.indices.create.CreateIndexRequest("target").settings(targetSettings) + .alias(new Alias("alias")) + ); ResizeResponse resizeResponse = execute(resizeRequest, highLevelClient().indices()::clone, highLevelClient().indices()::cloneAsync); assertTrue(resizeResponse.isAcknowledged()); assertTrue(resizeResponse.isShardsAcknowledged()); Map getIndexResponse = getAsMap("target"); - Map indexSettings = (Map)XContentMapValues.extractValue("target.settings.index", getIndexResponse); + Map indexSettings = (Map) XContentMapValues.extractValue("target.settings.index", getIndexResponse); assertNotNull(indexSettings); assertEquals("2", indexSettings.get("number_of_shards")); assertEquals("0", indexSettings.get("number_of_replicas")); - Map aliasData = (Map)XContentMapValues.extractValue("target.aliases.alias", getIndexResponse); + Map aliasData = (Map) XContentMapValues.extractValue("target.aliases.alias", getIndexResponse); assertNotNull(aliasData); } @@ -929,8 +1028,11 @@ public void testRollover() throws IOException { rolloverRequest.addMaxIndexDocsCondition(1); { - RolloverResponse rolloverResponse = execute(rolloverRequest, highLevelClient().indices()::rollover, - highLevelClient().indices()::rolloverAsync); + RolloverResponse rolloverResponse = execute( + rolloverRequest, + highLevelClient().indices()::rollover, + highLevelClient().indices()::rolloverAsync + ); assertFalse(rolloverResponse.isRolledOver()); assertFalse(rolloverResponse.isDryRun()); Map conditionStatus = rolloverResponse.getConditionStatus(); @@ -941,15 +1043,20 @@ public void testRollover() throws IOException { } highLevelClient().index(new IndexRequest("test").id("1").source("field", "value"), RequestOptions.DEFAULT); - highLevelClient().index(new IndexRequest("test").id("2").source("field", "value") - .setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL), RequestOptions.DEFAULT); - //without the refresh the rollover may not happen as the number of docs seen may be off + highLevelClient().index( + new IndexRequest("test").id("2").source("field", "value").setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL), + RequestOptions.DEFAULT + ); + // without the refresh the rollover may not happen as the number of docs seen may be off { rolloverRequest.addMaxIndexAgeCondition(new TimeValue(1)); rolloverRequest.dryRun(true); - RolloverResponse rolloverResponse = execute(rolloverRequest, highLevelClient().indices()::rollover, - highLevelClient().indices()::rolloverAsync); + RolloverResponse rolloverResponse = execute( + rolloverRequest, + highLevelClient().indices()::rollover, + highLevelClient().indices()::rolloverAsync + ); assertFalse(rolloverResponse.isRolledOver()); assertTrue(rolloverResponse.isDryRun()); Map conditionStatus = rolloverResponse.getConditionStatus(); @@ -965,8 +1072,11 @@ public void testRollover() throws IOException { rolloverRequest.dryRun(false); rolloverRequest.addMaxIndexSizeCondition(new ByteSizeValue(1, ByteSizeUnit.MB)); rolloverRequest.addMaxPrimaryShardSizeCondition(new ByteSizeValue(1, ByteSizeUnit.MB)); - RolloverResponse rolloverResponse = execute(rolloverRequest, highLevelClient().indices()::rollover, - highLevelClient().indices()::rolloverAsync); + RolloverResponse rolloverResponse = execute( + rolloverRequest, + highLevelClient().indices()::rollover, + highLevelClient().indices()::rolloverAsync + ); assertTrue(rolloverResponse.isRolledOver()); assertFalse(rolloverResponse.isDryRun()); Map conditionStatus = rolloverResponse.getConditionStatus(); @@ -992,8 +1102,11 @@ public void testGetAlias() throws IOException { } { GetAliasesRequest getAliasesRequest = new GetAliasesRequest().aliases("alias1"); - GetAliasesResponse getAliasesResponse = execute(getAliasesRequest, highLevelClient().indices()::getAlias, - highLevelClient().indices()::getAliasAsync); + GetAliasesResponse getAliasesResponse = execute( + getAliasesRequest, + highLevelClient().indices()::getAlias, + highLevelClient().indices()::getAliasAsync + ); assertThat(getAliasesResponse.getAliases().size(), equalTo(1)); assertThat(getAliasesResponse.getAliases().get("index1").size(), equalTo(1)); @@ -1006,8 +1119,11 @@ public void testGetAlias() throws IOException { } { GetAliasesRequest getAliasesRequest = new GetAliasesRequest().aliases("alias*"); - GetAliasesResponse getAliasesResponse = execute(getAliasesRequest, highLevelClient().indices()::getAlias, - highLevelClient().indices()::getAliasAsync); + GetAliasesResponse getAliasesResponse = execute( + getAliasesRequest, + highLevelClient().indices()::getAlias, + highLevelClient().indices()::getAliasAsync + ); assertThat(getAliasesResponse.getAliases().size(), equalTo(2)); assertThat(getAliasesResponse.getAliases().get("index1").size(), equalTo(1)); @@ -1021,8 +1137,11 @@ public void testGetAlias() throws IOException { } { GetAliasesRequest getAliasesRequest = new GetAliasesRequest().aliases("_all"); - GetAliasesResponse getAliasesResponse = execute(getAliasesRequest, highLevelClient().indices()::getAlias, - highLevelClient().indices()::getAliasAsync); + GetAliasesResponse getAliasesResponse = execute( + getAliasesRequest, + highLevelClient().indices()::getAlias, + highLevelClient().indices()::getAliasAsync + ); assertThat(getAliasesResponse.getAliases().size(), equalTo(2)); assertThat(getAliasesResponse.getAliases().get("index1").size(), equalTo(1)); @@ -1036,8 +1155,11 @@ public void testGetAlias() throws IOException { } { GetAliasesRequest getAliasesRequest = new GetAliasesRequest().aliases("*"); - GetAliasesResponse getAliasesResponse = execute(getAliasesRequest, highLevelClient().indices()::getAlias, - highLevelClient().indices()::getAliasAsync); + GetAliasesResponse getAliasesResponse = execute( + getAliasesRequest, + highLevelClient().indices()::getAlias, + highLevelClient().indices()::getAliasAsync + ); assertThat(getAliasesResponse.getAliases().size(), equalTo(2)); assertThat(getAliasesResponse.getAliases().get("index1").size(), equalTo(1)); @@ -1051,11 +1173,17 @@ public void testGetAlias() throws IOException { } { GetAliasesRequest getAliasesRequest = new GetAliasesRequest().indices("_all"); - GetAliasesResponse getAliasesResponse = execute(getAliasesRequest, highLevelClient().indices()::getAlias, - highLevelClient().indices()::getAliasAsync); + GetAliasesResponse getAliasesResponse = execute( + getAliasesRequest, + highLevelClient().indices()::getAlias, + highLevelClient().indices()::getAliasAsync + ); - assertThat("Unexpected number of aliases, got: " + getAliasesResponse.getAliases().toString(), - getAliasesResponse.getAliases().size(), equalTo(3)); + assertThat( + "Unexpected number of aliases, got: " + getAliasesResponse.getAliases().toString(), + getAliasesResponse.getAliases().size(), + equalTo(3) + ); assertThat(getAliasesResponse.getAliases().get("index1").size(), equalTo(1)); AliasMetadata aliasMetadata1 = getAliasesResponse.getAliases().get("index1").iterator().next(); assertThat(aliasMetadata1, notNullValue()); @@ -1068,8 +1196,11 @@ public void testGetAlias() throws IOException { } { GetAliasesRequest getAliasesRequest = new GetAliasesRequest().indices("ind*"); - GetAliasesResponse getAliasesResponse = execute(getAliasesRequest, highLevelClient().indices()::getAlias, - highLevelClient().indices()::getAliasAsync); + GetAliasesResponse getAliasesResponse = execute( + getAliasesRequest, + highLevelClient().indices()::getAlias, + highLevelClient().indices()::getAliasAsync + ); assertThat(getAliasesResponse.getAliases().size(), equalTo(3)); assertThat(getAliasesResponse.getAliases().get("index1").size(), equalTo(1)); @@ -1084,8 +1215,11 @@ public void testGetAlias() throws IOException { } { GetAliasesRequest getAliasesRequest = new GetAliasesRequest(); - GetAliasesResponse getAliasesResponse = execute(getAliasesRequest, highLevelClient().indices()::getAlias, - highLevelClient().indices()::getAliasAsync); + GetAliasesResponse getAliasesResponse = execute( + getAliasesRequest, + highLevelClient().indices()::getAlias, + highLevelClient().indices()::getAliasAsync + ); assertThat(getAliasesResponse.getAliases().size(), equalTo(3)); assertThat(getAliasesResponse.getAliases().get("index1").size(), equalTo(1)); @@ -1109,16 +1243,24 @@ public void testGetAliasesNonExistentIndexOrAlias() throws IOException { String index = "index"; { GetAliasesRequest getAliasesRequest = new GetAliasesRequest().indices(index); - GetAliasesResponse getAliasesResponse = execute(getAliasesRequest, highLevelClient().indices()::getAlias, - highLevelClient().indices()::getAliasAsync); + GetAliasesResponse getAliasesResponse = execute( + getAliasesRequest, + highLevelClient().indices()::getAlias, + highLevelClient().indices()::getAliasAsync + ); assertThat(getAliasesResponse.status(), equalTo(RestStatus.NOT_FOUND)); - assertThat(getAliasesResponse.getException().getMessage(), - equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index [index]]")); + assertThat( + getAliasesResponse.getException().getMessage(), + equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index [index]]") + ); } { GetAliasesRequest getAliasesRequest = new GetAliasesRequest(alias); - GetAliasesResponse getAliasesResponse = execute(getAliasesRequest, highLevelClient().indices()::getAlias, - highLevelClient().indices()::getAliasAsync); + GetAliasesResponse getAliasesResponse = execute( + getAliasesRequest, + highLevelClient().indices()::getAlias, + highLevelClient().indices()::getAliasAsync + ); assertThat(getAliasesResponse.status(), equalTo(RestStatus.NOT_FOUND)); assertThat(getAliasesResponse.getError(), equalTo("alias [" + alias + "] missing")); assertThat(getAliasesResponse.getException(), nullValue()); @@ -1127,27 +1269,40 @@ public void testGetAliasesNonExistentIndexOrAlias() throws IOException { client().performRequest(new Request(HttpPut.METHOD_NAME, index + "/_alias/" + alias)); { GetAliasesRequest getAliasesRequest = new GetAliasesRequest().indices(index, "non_existent_index"); - GetAliasesResponse getAliasesResponse = execute(getAliasesRequest, highLevelClient().indices()::getAlias, - highLevelClient().indices()::getAliasAsync); + GetAliasesResponse getAliasesResponse = execute( + getAliasesRequest, + highLevelClient().indices()::getAlias, + highLevelClient().indices()::getAliasAsync + ); assertThat(getAliasesResponse.getAliases().size(), equalTo(0)); assertThat(getAliasesResponse.status(), equalTo(RestStatus.NOT_FOUND)); assertThat(getAliasesResponse.getError(), nullValue()); - assertThat(getAliasesResponse.getException().getMessage(), - equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index [non_existent_index]]")); + assertThat( + getAliasesResponse.getException().getMessage(), + equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index [non_existent_index]]") + ); } { GetAliasesRequest getAliasesRequest = new GetAliasesRequest().indices(index, "non_existent_index").aliases(alias); - GetAliasesResponse getAliasesResponse = execute(getAliasesRequest, highLevelClient().indices()::getAlias, - highLevelClient().indices()::getAliasAsync); + GetAliasesResponse getAliasesResponse = execute( + getAliasesRequest, + highLevelClient().indices()::getAlias, + highLevelClient().indices()::getAliasAsync + ); assertThat(getAliasesResponse.getAliases().size(), equalTo(0)); assertThat(getAliasesResponse.status(), equalTo(RestStatus.NOT_FOUND)); - assertThat(getAliasesResponse.getException().getMessage(), - equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index [non_existent_index]]")); + assertThat( + getAliasesResponse.getException().getMessage(), + equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index [non_existent_index]]") + ); } { GetAliasesRequest getAliasesRequest = new GetAliasesRequest().indices("non_existent_index*"); - GetAliasesResponse getAliasesResponse = execute(getAliasesRequest, highLevelClient().indices()::getAlias, - highLevelClient().indices()::getAliasAsync); + GetAliasesResponse getAliasesResponse = execute( + getAliasesRequest, + highLevelClient().indices()::getAlias, + highLevelClient().indices()::getAliasAsync + ); assertThat(getAliasesResponse.status(), equalTo(RestStatus.OK)); assertThat(getAliasesResponse.getAliases().size(), equalTo(0)); assertThat(getAliasesResponse.getException(), nullValue()); @@ -1155,8 +1310,11 @@ public void testGetAliasesNonExistentIndexOrAlias() throws IOException { } { GetAliasesRequest getAliasesRequest = new GetAliasesRequest().indices(index).aliases(alias, "non_existent_alias"); - GetAliasesResponse getAliasesResponse = execute(getAliasesRequest, highLevelClient().indices()::getAlias, - highLevelClient().indices()::getAliasAsync); + GetAliasesResponse getAliasesResponse = execute( + getAliasesRequest, + highLevelClient().indices()::getAlias, + highLevelClient().indices()::getAliasAsync + ); assertThat(getAliasesResponse.status(), equalTo(RestStatus.NOT_FOUND)); assertThat(getAliasesResponse.getError(), equalTo("alias [non_existent_alias] missing")); @@ -1180,8 +1338,11 @@ public void testGetAliasesNonExistentIndexOrAlias() throws IOException { } { GetAliasesRequest getAliasesRequest = new GetAliasesRequest().aliases("non_existent_alias*"); - GetAliasesResponse getAliasesResponse = execute(getAliasesRequest, highLevelClient().indices()::getAlias, - highLevelClient().indices()::getAliasAsync); + GetAliasesResponse getAliasesResponse = execute( + getAliasesRequest, + highLevelClient().indices()::getAlias, + highLevelClient().indices()::getAliasAsync + ); assertThat(getAliasesResponse.status(), equalTo(RestStatus.OK)); assertThat(getAliasesResponse.getAliases().size(), equalTo(0)); } @@ -1207,8 +1368,11 @@ public void testIndexPutSettings() throws IOException { assertThat(dynamicSetting.getDefault(Settings.EMPTY), not(dynamicSettingValue)); UpdateSettingsRequest dynamicSettingRequest = new UpdateSettingsRequest(index); dynamicSettingRequest.settings(Settings.builder().put(dynamicSettingKey, dynamicSettingValue).build()); - AcknowledgedResponse response = execute(dynamicSettingRequest, highLevelClient().indices()::putSettings, - highLevelClient().indices()::putSettingsAsync); + AcknowledgedResponse response = execute( + dynamicSettingRequest, + highLevelClient().indices()::putSettings, + highLevelClient().indices()::putSettingsAsync + ); assertTrue(response.isAcknowledged()); Map indexSettingsAsMap = getIndexSettingsAsMap(index); @@ -1217,18 +1381,23 @@ public void testIndexPutSettings() throws IOException { assertThat(staticSetting.getDefault(Settings.EMPTY), not(staticSettingValue)); UpdateSettingsRequest staticSettingRequest = new UpdateSettingsRequest(index); staticSettingRequest.settings(Settings.builder().put(staticSettingKey, staticSettingValue).build()); - ElasticsearchException exception = expectThrows(ElasticsearchException.class, () -> execute(staticSettingRequest, - highLevelClient().indices()::putSettings, highLevelClient().indices()::putSettingsAsync)); - assertThat(exception.getMessage(), - startsWith("Elasticsearch exception [type=illegal_argument_exception, " - + "reason=Can't update non dynamic settings [[index.shard.check_on_startup]] for open indices [[index/")); + ElasticsearchException exception = expectThrows( + ElasticsearchException.class, + () -> execute(staticSettingRequest, highLevelClient().indices()::putSettings, highLevelClient().indices()::putSettingsAsync) + ); + assertThat( + exception.getMessage(), + startsWith( + "Elasticsearch exception [type=illegal_argument_exception, " + + "reason=Can't update non dynamic settings [[index.shard.check_on_startup]] for open indices [[index/" + ) + ); indexSettingsAsMap = getIndexSettingsAsMap(index); assertNull(indexSettingsAsMap.get(staticSettingKey)); closeIndex(index); - response = execute(staticSettingRequest, highLevelClient().indices()::putSettings, - highLevelClient().indices()::putSettingsAsync); + response = execute(staticSettingRequest, highLevelClient().indices()::putSettings, highLevelClient().indices()::putSettingsAsync); assertTrue(response.isAcknowledged()); openIndex(index); indexSettingsAsMap = getIndexSettingsAsMap(index); @@ -1237,17 +1406,37 @@ public void testIndexPutSettings() throws IOException { assertThat(unmodifiableSetting.getDefault(Settings.EMPTY), not(unmodifiableSettingValue)); UpdateSettingsRequest unmodifiableSettingRequest = new UpdateSettingsRequest(index); unmodifiableSettingRequest.settings(Settings.builder().put(unmodifiableSettingKey, unmodifiableSettingValue).build()); - exception = expectThrows(ElasticsearchException.class, () -> execute(unmodifiableSettingRequest, - highLevelClient().indices()::putSettings, highLevelClient().indices()::putSettingsAsync)); - assertThat(exception.getMessage(), startsWith( + exception = expectThrows( + ElasticsearchException.class, + () -> execute( + unmodifiableSettingRequest, + highLevelClient().indices()::putSettings, + highLevelClient().indices()::putSettingsAsync + ) + ); + assertThat( + exception.getMessage(), + startsWith( "Elasticsearch exception [type=illegal_argument_exception, " - + "reason=Can't update non dynamic settings [[index.number_of_shards]] for open indices [[index/")); + + "reason=Can't update non dynamic settings [[index.number_of_shards]] for open indices [[index/" + ) + ); closeIndex(index); - exception = expectThrows(ElasticsearchException.class, () -> execute(unmodifiableSettingRequest, - highLevelClient().indices()::putSettings, highLevelClient().indices()::putSettingsAsync)); - assertThat(exception.getMessage(), startsWith( + exception = expectThrows( + ElasticsearchException.class, + () -> execute( + unmodifiableSettingRequest, + highLevelClient().indices()::putSettings, + highLevelClient().indices()::putSettingsAsync + ) + ); + assertThat( + exception.getMessage(), + startsWith( "Elasticsearch exception [type=illegal_argument_exception, " - + "reason=final index setting [index.number_of_shards], not updateable")); + + "reason=final index setting [index.number_of_shards], not updateable" + ) + ); } public void testIndexPutSettingNonExistent() throws IOException { @@ -1258,15 +1447,29 @@ public void testIndexPutSettingNonExistent() throws IOException { int value = 10; indexUpdateSettingsRequest.settings(Settings.builder().put(setting, value).build()); - ElasticsearchException exception = expectThrows(ElasticsearchException.class, () -> execute(indexUpdateSettingsRequest, - highLevelClient().indices()::putSettings, highLevelClient().indices()::putSettingsAsync)); + ElasticsearchException exception = expectThrows( + ElasticsearchException.class, + () -> execute( + indexUpdateSettingsRequest, + highLevelClient().indices()::putSettings, + highLevelClient().indices()::putSettingsAsync + ) + ); assertEquals(RestStatus.NOT_FOUND, exception.status()); - assertThat(exception.getMessage(), - equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index [index]]")); + assertThat( + exception.getMessage(), + equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index [index]]") + ); createIndex(index, Settings.EMPTY); - exception = expectThrows(ElasticsearchException.class, () -> execute(indexUpdateSettingsRequest, - highLevelClient().indices()::putSettings, highLevelClient().indices()::putSettingsAsync)); + exception = expectThrows( + ElasticsearchException.class, + () -> execute( + indexUpdateSettingsRequest, + highLevelClient().indices()::putSettings, + highLevelClient().indices()::putSettingsAsync + ) + ); assertThat(exception.status(), equalTo(RestStatus.BAD_REQUEST)); assertThat( exception.getMessage(), @@ -1280,16 +1483,20 @@ public void testIndexPutSettingNonExistent() throws IOException { @SuppressWarnings("unchecked") public void testPutTemplate() throws Exception { - PutIndexTemplateRequest putTemplateRequest = new PutIndexTemplateRequest("my-template", List.of("pattern-1", "name-*")) - .order(10) + PutIndexTemplateRequest putTemplateRequest = new PutIndexTemplateRequest("my-template", List.of("pattern-1", "name-*")).order(10) .create(randomBoolean()) .settings(Settings.builder().put("number_of_shards", "3").put("number_of_replicas", "0")) .mapping("{ \"properties\": { \"host_name\": { \"type\": \"keyword\" } } }", XContentType.JSON) .alias(new Alias("alias-1").indexRouting("abc")) - .alias(new Alias("alias-1").indexRouting("abc")).alias(new Alias("{index}-write").searchRouting("xyz")); + .alias(new Alias("alias-1").indexRouting("abc")) + .alias(new Alias("{index}-write").searchRouting("xyz")); - AcknowledgedResponse putTemplateResponse = execute(putTemplateRequest, - highLevelClient().indices()::putTemplate, highLevelClient().indices()::putTemplateAsync, LEGACY_TEMPLATE_OPTIONS); + AcknowledgedResponse putTemplateResponse = execute( + putTemplateRequest, + highLevelClient().indices()::putTemplate, + highLevelClient().indices()::putTemplateAsync, + LEGACY_TEMPLATE_OPTIONS + ); assertThat(putTemplateResponse.isAcknowledged(), equalTo(true)); Map templates = getAsMap("/_template/my-template"); @@ -1304,8 +1511,7 @@ public void testPutTemplate() throws Exception { } public void testPutTemplateWithTypesUsingUntypedAPI() throws Exception { - PutIndexTemplateRequest putTemplateRequest = new PutIndexTemplateRequest("my-template", List.of("pattern-1", "name-*")) - .order(10) + PutIndexTemplateRequest putTemplateRequest = new PutIndexTemplateRequest("my-template", List.of("pattern-1", "name-*")).order(10) .create(randomBoolean()) .settings(Settings.builder().put("number_of_shards", "3").put("number_of_replicas", "0")) .mapping( @@ -1320,61 +1526,83 @@ public void testPutTemplateWithTypesUsingUntypedAPI() throws Exception { + "}", XContentType.JSON ) - .alias(new Alias("alias-1").indexRouting("abc")).alias(new Alias("{index}-write").searchRouting("xyz")); - + .alias(new Alias("alias-1").indexRouting("abc")) + .alias(new Alias("{index}-write").searchRouting("xyz")); - ElasticsearchStatusException badMappingError = expectThrows(ElasticsearchStatusException.class, - () -> execute(putTemplateRequest, - highLevelClient().indices()::putTemplate, highLevelClient().indices()::putTemplateAsync)); - assertThat(badMappingError.getDetailedMessage(), - containsString("Root mapping definition has unsupported parameters: [my_doc_type")); + ElasticsearchStatusException badMappingError = expectThrows( + ElasticsearchStatusException.class, + () -> execute(putTemplateRequest, highLevelClient().indices()::putTemplate, highLevelClient().indices()::putTemplateAsync) + ); + assertThat( + badMappingError.getDetailedMessage(), + containsString("Root mapping definition has unsupported parameters: [my_doc_type") + ); } public void testPutTemplateBadRequests() throws Exception { RestHighLevelClient client = highLevelClient(); // Failed to validate because index patterns are missing - IllegalArgumentException withoutPatternError = expectThrows(IllegalArgumentException.class, - () -> new PutIndexTemplateRequest("t1", randomBoolean() ? null : List.of())); + IllegalArgumentException withoutPatternError = expectThrows( + IllegalArgumentException.class, + () -> new PutIndexTemplateRequest("t1", randomBoolean() ? null : List.of()) + ); assertThat(withoutPatternError.getMessage(), containsString("index patterns are missing")); // Create-only specified but an template exists already PutIndexTemplateRequest goodTemplate = new PutIndexTemplateRequest("t2", List.of("qa-*", "prod-*")); - assertTrue(execute(goodTemplate, client.indices()::putTemplate, client.indices()::putTemplateAsync, LEGACY_TEMPLATE_OPTIONS) - .isAcknowledged()); + assertTrue( + execute(goodTemplate, client.indices()::putTemplate, client.indices()::putTemplateAsync, LEGACY_TEMPLATE_OPTIONS) + .isAcknowledged() + ); goodTemplate.create(true); - ElasticsearchException alreadyExistsError = expectThrows(ElasticsearchException.class, - () -> execute(goodTemplate, client.indices()::putTemplate, client.indices()::putTemplateAsync, LEGACY_TEMPLATE_OPTIONS)); - assertThat(alreadyExistsError.getDetailedMessage(), - containsString("[type=illegal_argument_exception, reason=index_template [t2] already exists]")); + ElasticsearchException alreadyExistsError = expectThrows( + ElasticsearchException.class, + () -> execute(goodTemplate, client.indices()::putTemplate, client.indices()::putTemplateAsync, LEGACY_TEMPLATE_OPTIONS) + ); + assertThat( + alreadyExistsError.getDetailedMessage(), + containsString("[type=illegal_argument_exception, reason=index_template [t2] already exists]") + ); goodTemplate.create(false); - assertTrue(execute(goodTemplate, client.indices()::putTemplate, client.indices()::putTemplateAsync, LEGACY_TEMPLATE_OPTIONS) - .isAcknowledged()); + assertTrue( + execute(goodTemplate, client.indices()::putTemplate, client.indices()::putTemplateAsync, LEGACY_TEMPLATE_OPTIONS) + .isAcknowledged() + ); // Rejected due to unknown settings - PutIndexTemplateRequest unknownSettingTemplate = new PutIndexTemplateRequest("t3", List.of("any")) - .settings(Settings.builder().put("this-setting-does-not-exist", 100)); - ElasticsearchStatusException unknownSettingError = expectThrows(ElasticsearchStatusException.class, - () -> execute(unknownSettingTemplate, client.indices()::putTemplate, client.indices()::putTemplateAsync, - LEGACY_TEMPLATE_OPTIONS)); + PutIndexTemplateRequest unknownSettingTemplate = new PutIndexTemplateRequest("t3", List.of("any")).settings( + Settings.builder().put("this-setting-does-not-exist", 100) + ); + ElasticsearchStatusException unknownSettingError = expectThrows( + ElasticsearchStatusException.class, + () -> execute( + unknownSettingTemplate, + client.indices()::putTemplate, + client.indices()::putTemplateAsync, + LEGACY_TEMPLATE_OPTIONS + ) + ); assertThat(unknownSettingError.getDetailedMessage(), containsString("unknown setting [index.this-setting-does-not-exist]")); } - public void testValidateQuery() throws IOException{ + public void testValidateQuery() throws IOException { String index = "some_index"; createIndex(index, Settings.EMPTY); - QueryBuilder builder = QueryBuilders - .boolQuery() + QueryBuilder builder = QueryBuilders.boolQuery() .must(QueryBuilders.queryStringQuery("*:*")) .filter(QueryBuilders.termQuery("user", "kimchy")); ValidateQueryRequest request = new ValidateQueryRequest(index).query(builder); request.explain(randomBoolean()); - ValidateQueryResponse response = execute(request, highLevelClient().indices()::validateQuery, - highLevelClient().indices()::validateQueryAsync); + ValidateQueryResponse response = execute( + request, + highLevelClient().indices()::validateQuery, + highLevelClient().indices()::validateQueryAsync + ); assertTrue(response.isValid()); } - public void testInvalidValidateQuery() throws IOException{ + public void testInvalidValidateQuery() throws IOException { String index = "shakespeare"; createIndex(index, Settings.EMPTY); @@ -1392,41 +1620,54 @@ public void testInvalidValidateQuery() throws IOException{ ); assertOK(client().performRequest(postDoc)); - QueryBuilder builder = QueryBuilders - .queryStringQuery("line_id:foo") - .lenient(false); + QueryBuilder builder = QueryBuilders.queryStringQuery("line_id:foo").lenient(false); ValidateQueryRequest request = new ValidateQueryRequest(index).query(builder); request.explain(true); - ValidateQueryResponse response = execute(request, highLevelClient().indices()::validateQuery, - highLevelClient().indices()::validateQueryAsync); + ValidateQueryResponse response = execute( + request, + highLevelClient().indices()::validateQuery, + highLevelClient().indices()::validateQueryAsync + ); assertFalse(response.isValid()); } public void testCRUDIndexTemplate() throws Exception { RestHighLevelClient client = highLevelClient(); - PutIndexTemplateRequest putTemplate1 = new PutIndexTemplateRequest("template-1", List.of("pattern-1", "name-1")) - .alias(new Alias("alias-1")); - assertThat(execute(putTemplate1, client.indices()::putTemplate, client.indices()::putTemplateAsync, LEGACY_TEMPLATE_OPTIONS) - .isAcknowledged(), - equalTo(true)); - PutIndexTemplateRequest putTemplate2 = new PutIndexTemplateRequest("template-2", List.of("pattern-2", "name-2")) - .mapping("{\"properties\": { \"name\": { \"type\": \"text\" }}}", XContentType.JSON) - .settings(Settings.builder().put("number_of_shards", "2").put("number_of_replicas", "0")); - assertThat(execute(putTemplate2, client.indices()::putTemplate, client.indices()::putTemplateAsync, LEGACY_TEMPLATE_OPTIONS) - .isAcknowledged(), equalTo(true)); + PutIndexTemplateRequest putTemplate1 = new PutIndexTemplateRequest("template-1", List.of("pattern-1", "name-1")).alias( + new Alias("alias-1") + ); + assertThat( + execute(putTemplate1, client.indices()::putTemplate, client.indices()::putTemplateAsync, LEGACY_TEMPLATE_OPTIONS) + .isAcknowledged(), + equalTo(true) + ); + PutIndexTemplateRequest putTemplate2 = new PutIndexTemplateRequest("template-2", List.of("pattern-2", "name-2")).mapping( + "{\"properties\": { \"name\": { \"type\": \"text\" }}}", + XContentType.JSON + ).settings(Settings.builder().put("number_of_shards", "2").put("number_of_replicas", "0")); + assertThat( + execute(putTemplate2, client.indices()::putTemplate, client.indices()::putTemplateAsync, LEGACY_TEMPLATE_OPTIONS) + .isAcknowledged(), + equalTo(true) + ); GetIndexTemplatesResponse getTemplate1 = execute( - new GetIndexTemplatesRequest("template-1"), - client.indices()::getIndexTemplate, client.indices()::getIndexTemplateAsync); + new GetIndexTemplatesRequest("template-1"), + client.indices()::getIndexTemplate, + client.indices()::getIndexTemplateAsync + ); assertThat(getTemplate1.getIndexTemplates(), hasSize(1)); IndexTemplateMetadata template1 = getTemplate1.getIndexTemplates().get(0); assertThat(template1.name(), equalTo("template-1")); assertThat(template1.patterns(), contains("pattern-1", "name-1")); assertTrue(template1.aliases().containsKey("alias-1")); - GetIndexTemplatesResponse getTemplate2 = execute(new GetIndexTemplatesRequest("template-2"), - client.indices()::getIndexTemplate, client.indices()::getIndexTemplateAsync); + GetIndexTemplatesResponse getTemplate2 = execute( + new GetIndexTemplatesRequest("template-2"), + client.indices()::getIndexTemplate, + client.indices()::getIndexTemplateAsync + ); assertThat(getTemplate2.getIndexTemplates(), hasSize(1)); IndexTemplateMetadata template2 = getTemplate2.getIndexTemplates().get(0); assertThat(template2.name(), equalTo("template-2")); @@ -1440,43 +1681,86 @@ public void testCRUDIndexTemplate() throws Exception { Map props = (Map) template2.mappings().sourceAsMap().get("properties"); assertTrue(props.containsKey("name")); - - - List names = randomBoolean() - ? Arrays.asList("*plate-1", "template-2") - : Arrays.asList("template-*"); + List names = randomBoolean() ? Arrays.asList("*plate-1", "template-2") : Arrays.asList("template-*"); GetIndexTemplatesRequest getBothRequest = new GetIndexTemplatesRequest(names); GetIndexTemplatesResponse getBoth = execute( - getBothRequest, client.indices()::getIndexTemplate, client.indices()::getIndexTemplateAsync); + getBothRequest, + client.indices()::getIndexTemplate, + client.indices()::getIndexTemplateAsync + ); assertThat(getBoth.getIndexTemplates(), hasSize(2)); - assertThat(getBoth.getIndexTemplates().stream().map(IndexTemplateMetadata::name).toArray(), - arrayContainingInAnyOrder("template-1", "template-2")); + assertThat( + getBoth.getIndexTemplates().stream().map(IndexTemplateMetadata::name).toArray(), + arrayContainingInAnyOrder("template-1", "template-2") + ); GetIndexTemplatesRequest getAllRequest = new GetIndexTemplatesRequest(); GetIndexTemplatesResponse getAll = execute( - getAllRequest, client.indices()::getIndexTemplate, client.indices()::getIndexTemplateAsync); + getAllRequest, + client.indices()::getIndexTemplate, + client.indices()::getIndexTemplateAsync + ); assertThat(getAll.getIndexTemplates().size(), greaterThanOrEqualTo(2)); - assertThat(getAll.getIndexTemplates().stream().map(IndexTemplateMetadata::name) - .collect(Collectors.toList()), - hasItems("template-1", "template-2")); - - assertTrue(execute(new DeleteIndexTemplateRequest("template-1"), - client.indices()::deleteTemplate, client.indices()::deleteTemplateAsync).isAcknowledged()); - assertThat(expectThrows(ElasticsearchException.class, () -> execute(new GetIndexTemplatesRequest("template-1"), - client.indices()::getIndexTemplate, client.indices()::getIndexTemplateAsync)).status(), equalTo(RestStatus.NOT_FOUND)); - assertThat(expectThrows(ElasticsearchException.class, () -> execute(new DeleteIndexTemplateRequest("template-1"), - client.indices()::deleteTemplate, client.indices()::deleteTemplateAsync)).status(), equalTo(RestStatus.NOT_FOUND)); - - assertThat(execute(new GetIndexTemplatesRequest("template-*"), - client.indices()::getIndexTemplate, client.indices()::getIndexTemplateAsync).getIndexTemplates(), hasSize(1)); - assertThat(execute(new GetIndexTemplatesRequest("template-*"), - client.indices()::getIndexTemplate, client.indices()::getIndexTemplateAsync).getIndexTemplates() - .get(0).name(), equalTo("template-2")); - - assertTrue(execute(new DeleteIndexTemplateRequest("template-*"), - client.indices()::deleteTemplate, client.indices()::deleteTemplateAsync).isAcknowledged()); - assertThat(expectThrows(ElasticsearchException.class, () -> execute(new GetIndexTemplatesRequest("template-*"), - client.indices()::getIndexTemplate, client.indices()::getIndexTemplateAsync)).status(), equalTo(RestStatus.NOT_FOUND)); + assertThat( + getAll.getIndexTemplates().stream().map(IndexTemplateMetadata::name).collect(Collectors.toList()), + hasItems("template-1", "template-2") + ); + + assertTrue( + execute(new DeleteIndexTemplateRequest("template-1"), client.indices()::deleteTemplate, client.indices()::deleteTemplateAsync) + .isAcknowledged() + ); + assertThat( + expectThrows( + ElasticsearchException.class, + () -> execute( + new GetIndexTemplatesRequest("template-1"), + client.indices()::getIndexTemplate, + client.indices()::getIndexTemplateAsync + ) + ).status(), + equalTo(RestStatus.NOT_FOUND) + ); + assertThat( + expectThrows( + ElasticsearchException.class, + () -> execute( + new DeleteIndexTemplateRequest("template-1"), + client.indices()::deleteTemplate, + client.indices()::deleteTemplateAsync + ) + ).status(), + equalTo(RestStatus.NOT_FOUND) + ); + + assertThat( + execute(new GetIndexTemplatesRequest("template-*"), client.indices()::getIndexTemplate, client.indices()::getIndexTemplateAsync) + .getIndexTemplates(), + hasSize(1) + ); + assertThat( + execute(new GetIndexTemplatesRequest("template-*"), client.indices()::getIndexTemplate, client.indices()::getIndexTemplateAsync) + .getIndexTemplates() + .get(0) + .name(), + equalTo("template-2") + ); + + assertTrue( + execute(new DeleteIndexTemplateRequest("template-*"), client.indices()::deleteTemplate, client.indices()::deleteTemplateAsync) + .isAcknowledged() + ); + assertThat( + expectThrows( + ElasticsearchException.class, + () -> execute( + new GetIndexTemplatesRequest("template-*"), + client.indices()::getIndexTemplate, + client.indices()::getIndexTemplateAsync + ) + ).status(), + equalTo(RestStatus.NOT_FOUND) + ); } public void testIndexTemplatesExist() throws Exception { @@ -1485,17 +1769,14 @@ public void testIndexTemplatesExist() throws Exception { { for (String suffix : Arrays.asList("1", "2")) { - final PutIndexTemplateRequest putRequest = new PutIndexTemplateRequest("template-" + suffix, - List.of("pattern-" + suffix, "name-" + suffix)) - .alias(new Alias("alias-" + suffix)); + final PutIndexTemplateRequest putRequest = new PutIndexTemplateRequest( + "template-" + suffix, + List.of("pattern-" + suffix, "name-" + suffix) + ).alias(new Alias("alias-" + suffix)); assertTrue( - execute( - putRequest, - client.indices()::putTemplate, - client.indices()::putTemplateAsync, - LEGACY_TEMPLATE_OPTIONS - ).isAcknowledged() - ); + execute(putRequest, client.indices()::putTemplate, client.indices()::putTemplateAsync, LEGACY_TEMPLATE_OPTIONS) + .isAcknowledged() + ); final IndexTemplatesExistRequest existsRequest = new IndexTemplatesExistRequest("template-" + suffix); assertTrue(execute(existsRequest, client.indices()::existsTemplate, client.indices()::existsTemplateAsync)); @@ -1503,9 +1784,7 @@ public void testIndexTemplatesExist() throws Exception { } { - final List templateNames = randomBoolean() - ? Arrays.asList("*plate-1", "template-2") - : Arrays.asList("template-*"); + final List templateNames = randomBoolean() ? Arrays.asList("*plate-1", "template-2") : Arrays.asList("template-*"); final IndexTemplatesExistRequest bothRequest = new IndexTemplatesExistRequest(templateNames); assertTrue(execute(bothRequest, client.indices()::existsTemplate, client.indices()::existsTemplateAsync)); @@ -1536,8 +1815,11 @@ public void testReloadAnalyzer() throws IOException { createIndex("test", Settings.EMPTY); RestHighLevelClient client = highLevelClient(); - ReloadAnalyzersResponse reloadResponse = execute(new ReloadAnalyzersRequest("test"), client.indices()::reloadAnalyzers, - client.indices()::reloadAnalyzersAsync); + ReloadAnalyzersResponse reloadResponse = execute( + new ReloadAnalyzersRequest("test"), + client.indices()::reloadAnalyzers, + client.indices()::reloadAnalyzersAsync + ); assertNotNull(reloadResponse.shards()); assertTrue(reloadResponse.getReloadedDetails().containsKey("test")); } @@ -1551,8 +1833,11 @@ public void testDeleteAlias() throws IOException { IndicesAliasesRequest aliasesAddRemoveRequest = new IndicesAliasesRequest(); aliasesAddRemoveRequest.addAliasAction(new AliasActions(AliasActions.Type.ADD).indices(index).alias(alias)); aliasesAddRemoveRequest.addAliasAction(new AliasActions(AliasActions.Type.ADD).indices(index).alias(alias + "2")); - AcknowledgedResponse aliasResponse = execute(aliasesAddRemoveRequest, highLevelClient().indices()::updateAliases, - highLevelClient().indices()::updateAliasesAsync); + AcknowledgedResponse aliasResponse = execute( + aliasesAddRemoveRequest, + highLevelClient().indices()::updateAliases, + highLevelClient().indices()::updateAliasesAsync + ); assertTrue(aliasResponse.isAcknowledged()); assertThat(aliasExists(alias), equalTo(true)); assertThat(aliasExists(alias2), equalTo(true)); @@ -1560,9 +1845,11 @@ public void testDeleteAlias() throws IOException { assertThat(aliasExists(index, alias2), equalTo(true)); DeleteAliasRequest request = new DeleteAliasRequest(index, alias); - org.elasticsearch.client.core.AcknowledgedResponse aliasDeleteResponse = execute(request, + org.elasticsearch.client.core.AcknowledgedResponse aliasDeleteResponse = execute( + request, highLevelClient().indices()::deleteAlias, - highLevelClient().indices()::deleteAliasAsync); + highLevelClient().indices()::deleteAliasAsync + ); assertThat(aliasExists(alias), equalTo(false)); assertThat(aliasExists(alias2), equalTo(true)); @@ -1575,21 +1862,31 @@ public void testDataStreams() throws Exception { CompressedXContent mappings = new CompressedXContent("{\"properties\":{\"@timestamp\":{\"type\":\"date\"}}}"); Template template = new Template(null, mappings, null); - ComposableIndexTemplate indexTemplate = new ComposableIndexTemplate(Collections.singletonList(dataStreamName), template, - Collections.emptyList(), 1L, 1L, new HashMap<>(), new ComposableIndexTemplate.DataStreamTemplate(), null); - PutComposableIndexTemplateRequest putComposableIndexTemplateRequest = - new PutComposableIndexTemplateRequest().name("ds-template").create(true).indexTemplate(indexTemplate); - AcknowledgedResponse response = execute(putComposableIndexTemplateRequest, - highLevelClient().indices()::putIndexTemplate, highLevelClient().indices()::putIndexTemplateAsync); + ComposableIndexTemplate indexTemplate = new ComposableIndexTemplate( + Collections.singletonList(dataStreamName), + template, + Collections.emptyList(), + 1L, + 1L, + new HashMap<>(), + new ComposableIndexTemplate.DataStreamTemplate(), + null + ); + PutComposableIndexTemplateRequest putComposableIndexTemplateRequest = new PutComposableIndexTemplateRequest().name("ds-template") + .create(true) + .indexTemplate(indexTemplate); + AcknowledgedResponse response = execute( + putComposableIndexTemplateRequest, + highLevelClient().indices()::putIndexTemplate, + highLevelClient().indices()::putIndexTemplateAsync + ); assertThat(response.isAcknowledged(), equalTo(true)); CreateDataStreamRequest createDataStreamRequest = new CreateDataStreamRequest(dataStreamName); IndicesClient indices = highLevelClient().indices(); response = execute(createDataStreamRequest, indices::createDataStream, indices::createDataStreamAsync); assertThat(response.isAcknowledged(), equalTo(true)); - ensureHealth(dataStreamName, (request -> { - request.addParameter("wait_for_status", "yellow"); - })); + ensureHealth(dataStreamName, (request -> { request.addParameter("wait_for_status", "yellow"); })); GetDataStreamRequest getDataStreamRequest = new GetDataStreamRequest(dataStreamName); GetDataStreamResponse getDataStreamResponse = execute(getDataStreamRequest, indices::getDataStream, indices::getDataStreamAsync); @@ -1612,8 +1909,11 @@ public void testDataStreams() throws Exception { assertThat(dataStream.getIndices(), hasSize(1)); DataStreamsStatsRequest dataStreamsStatsRequest = new DataStreamsStatsRequest(); - DataStreamsStatsResponse dataStreamsStatsResponse = execute(dataStreamsStatsRequest, indices::dataStreamsStats, - indices::dataStreamsStatsAsync); + DataStreamsStatsResponse dataStreamsStatsResponse = execute( + dataStreamsStatsRequest, + indices::dataStreamsStats, + indices::dataStreamsStatsAsync + ); int dataStreamsCount = dataStreamsStatsResponse.getDataStreamCount(); assertThat(dataStreamsCount, equalTo(1)); int backingIndices = dataStreamsStatsResponse.getBackingIndices(); @@ -1642,8 +1942,10 @@ public void testDataStreams() throws Exception { getDataStreamRequest = new GetDataStreamRequest(dataStreamName); GetDataStreamRequest finalGetDataStreamRequest = getDataStreamRequest; - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, () -> execute(finalGetDataStreamRequest, - indices::getDataStream, indices::getDataStreamAsync)); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> execute(finalGetDataStreamRequest, indices::getDataStream, indices::getDataStreamAsync) + ); assertThat(e.status(), equalTo(RestStatus.NOT_FOUND)); } @@ -1654,42 +1956,71 @@ public void testIndexTemplates() throws Exception { AliasMetadata alias = AliasMetadata.builder("alias").writeIndex(true).build(); Template template = new Template(settings, mappings, Map.of("alias", alias)); List pattern = List.of("pattern"); - ComposableIndexTemplate indexTemplate = - new ComposableIndexTemplate(pattern, template, Collections.emptyList(), 1L, 1L, new HashMap<>(), null, null); - PutComposableIndexTemplateRequest putComposableIndexTemplateRequest = - new PutComposableIndexTemplateRequest().name(templateName).create(true).indexTemplate(indexTemplate); - - AcknowledgedResponse response = execute(putComposableIndexTemplateRequest, - highLevelClient().indices()::putIndexTemplate, highLevelClient().indices()::putIndexTemplateAsync); + ComposableIndexTemplate indexTemplate = new ComposableIndexTemplate( + pattern, + template, + Collections.emptyList(), + 1L, + 1L, + new HashMap<>(), + null, + null + ); + PutComposableIndexTemplateRequest putComposableIndexTemplateRequest = new PutComposableIndexTemplateRequest().name(templateName) + .create(true) + .indexTemplate(indexTemplate); + + AcknowledgedResponse response = execute( + putComposableIndexTemplateRequest, + highLevelClient().indices()::putIndexTemplate, + highLevelClient().indices()::putIndexTemplateAsync + ); assertThat(response.isAcknowledged(), equalTo(true)); ComposableIndexTemplateExistRequest composableIndexTemplateExistRequest = new ComposableIndexTemplateExistRequest(templateName); - boolean exist = execute(composableIndexTemplateExistRequest, - highLevelClient().indices()::existsIndexTemplate, highLevelClient().indices()::existsIndexTemplateAsync); + boolean exist = execute( + composableIndexTemplateExistRequest, + highLevelClient().indices()::existsIndexTemplate, + highLevelClient().indices()::existsIndexTemplateAsync + ); assertTrue(exist); GetComposableIndexTemplateRequest getComposableIndexTemplateRequest = new GetComposableIndexTemplateRequest(templateName); - GetComposableIndexTemplatesResponse getResponse = execute(getComposableIndexTemplateRequest, - highLevelClient().indices()::getIndexTemplate, highLevelClient().indices()::getIndexTemplateAsync); + GetComposableIndexTemplatesResponse getResponse = execute( + getComposableIndexTemplateRequest, + highLevelClient().indices()::getIndexTemplate, + highLevelClient().indices()::getIndexTemplateAsync + ); assertThat(getResponse.getIndexTemplates().size(), equalTo(1)); assertThat(getResponse.getIndexTemplates().containsKey(templateName), equalTo(true)); assertThat(getResponse.getIndexTemplates().get(templateName), equalTo(indexTemplate)); DeleteComposableIndexTemplateRequest deleteComposableIndexTemplateRequest = new DeleteComposableIndexTemplateRequest(templateName); - response = execute(deleteComposableIndexTemplateRequest, highLevelClient().indices()::deleteIndexTemplate, - highLevelClient().indices()::deleteIndexTemplateAsync); + response = execute( + deleteComposableIndexTemplateRequest, + highLevelClient().indices()::deleteIndexTemplate, + highLevelClient().indices()::deleteIndexTemplateAsync + ); assertThat(response.isAcknowledged(), equalTo(true)); - ElasticsearchStatusException statusException = expectThrows(ElasticsearchStatusException.class, - () -> execute(getComposableIndexTemplateRequest, - highLevelClient().indices()::getIndexTemplate, highLevelClient().indices()::getIndexTemplateAsync)); + ElasticsearchStatusException statusException = expectThrows( + ElasticsearchStatusException.class, + () -> execute( + getComposableIndexTemplateRequest, + highLevelClient().indices()::getIndexTemplate, + highLevelClient().indices()::getIndexTemplateAsync + ) + ); assertThat(statusException.status(), equalTo(RestStatus.NOT_FOUND)); - exist = execute(composableIndexTemplateExistRequest, - highLevelClient().indices()::existsIndexTemplate, highLevelClient().indices()::existsIndexTemplateAsync); + exist = execute( + composableIndexTemplateExistRequest, + highLevelClient().indices()::existsIndexTemplate, + highLevelClient().indices()::existsIndexTemplateAsync + ); assertFalse(exist); } @@ -1701,33 +2032,63 @@ public void testSimulateIndexTemplate() throws Exception { AliasMetadata alias = AliasMetadata.builder("alias").writeIndex(true).build(); Template template = new Template(settings, mappings, Map.of("alias", alias)); List pattern = List.of("pattern"); - ComposableIndexTemplate indexTemplate = - new ComposableIndexTemplate(pattern, template, Collections.emptyList(), 1L, 1L, new HashMap<>(), null, null); - PutComposableIndexTemplateRequest putComposableIndexTemplateRequest = - new PutComposableIndexTemplateRequest().name(templateName).create(true).indexTemplate(indexTemplate); - - AcknowledgedResponse response = execute(putComposableIndexTemplateRequest, - highLevelClient().indices()::putIndexTemplate, highLevelClient().indices()::putIndexTemplateAsync); + ComposableIndexTemplate indexTemplate = new ComposableIndexTemplate( + pattern, + template, + Collections.emptyList(), + 1L, + 1L, + new HashMap<>(), + null, + null + ); + PutComposableIndexTemplateRequest putComposableIndexTemplateRequest = new PutComposableIndexTemplateRequest().name(templateName) + .create(true) + .indexTemplate(indexTemplate); + + AcknowledgedResponse response = execute( + putComposableIndexTemplateRequest, + highLevelClient().indices()::putIndexTemplate, + highLevelClient().indices()::putIndexTemplateAsync + ); assertThat(response.isAcknowledged(), equalTo(true)); SimulateIndexTemplateRequest simulateIndexTemplateRequest = new SimulateIndexTemplateRequest("pattern"); AliasMetadata simulationAlias = AliasMetadata.builder("simulation-alias").writeIndex(true).build(); - ComposableIndexTemplate simulationTemplate = new ComposableIndexTemplate(pattern, new Template(null, null, - Map.of("simulation-alias", simulationAlias)), Collections.emptyList(), 2L, 1L, new HashMap<>(), null, null); - PutComposableIndexTemplateRequest newIndexTemplateReq = - new PutComposableIndexTemplateRequest().name("used-for-simulation").create(true).indexTemplate(indexTemplate); + ComposableIndexTemplate simulationTemplate = new ComposableIndexTemplate( + pattern, + new Template(null, null, Map.of("simulation-alias", simulationAlias)), + Collections.emptyList(), + 2L, + 1L, + new HashMap<>(), + null, + null + ); + PutComposableIndexTemplateRequest newIndexTemplateReq = new PutComposableIndexTemplateRequest().name("used-for-simulation") + .create(true) + .indexTemplate(indexTemplate); newIndexTemplateReq.indexTemplate(simulationTemplate); simulateIndexTemplateRequest.indexTemplateV2Request(newIndexTemplateReq); - SimulateIndexTemplateResponse simulateResponse = execute(simulateIndexTemplateRequest, - highLevelClient().indices()::simulateIndexTemplate, highLevelClient().indices()::simulateIndexTemplateAsync); + SimulateIndexTemplateResponse simulateResponse = execute( + simulateIndexTemplateRequest, + highLevelClient().indices()::simulateIndexTemplate, + highLevelClient().indices()::simulateIndexTemplateAsync + ); Map aliases = simulateResponse.resolvedTemplate().aliases(); assertThat(aliases, is(notNullValue())); - assertThat("the template we provided for the simulation has a higher priority than the one in the system", - aliases.get("simulation-alias"), is(notNullValue())); + assertThat( + "the template we provided for the simulation has a higher priority than the one in the system", + aliases.get("simulation-alias"), + is(notNullValue()) + ); assertThat(aliases.get("simulation-alias").getAlias(), is("simulation-alias")); - assertThat("existing template overlaps the higher priority template we provided for the simulation", - simulateResponse.overlappingTemplates().get("my-template").get(0), is("pattern")); + assertThat( + "existing template overlaps the higher priority template we provided for the simulation", + simulateResponse.overlappingTemplates().get("my-template").get(0), + is("pattern") + ); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesRequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesRequestConvertersTests.java index 2410d14f637ba..9c3357c8fa10d 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesRequestConvertersTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesRequestConvertersTests.java @@ -48,14 +48,14 @@ import org.elasticsearch.client.indices.ReloadAnalyzersRequest; import org.elasticsearch.client.indices.ResizeRequest; import org.elasticsearch.client.indices.rollover.RolloverRequest; -import org.elasticsearch.core.CheckedFunction; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeValue; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.CollectionUtils; -import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.core.CheckedFunction; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentType; import org.junit.Assert; import java.io.IOException; @@ -80,8 +80,7 @@ public class IndicesRequestConvertersTests extends ESTestCase { public void testAnalyzeRequest() throws Exception { - AnalyzeRequest indexAnalyzeRequest - = AnalyzeRequest.withIndexAnalyzer("test_index", "test_analyzer", "Here is some text"); + AnalyzeRequest indexAnalyzeRequest = AnalyzeRequest.withIndexAnalyzer("test_index", "test_analyzer", "Here is some text"); Request request = IndicesRequestConverters.analyze(indexAnalyzeRequest); assertThat(request.getEndpoint(), equalTo("/test_index/_analyze")); @@ -111,10 +110,11 @@ public void testIndicesExist() { } public void testIndicesExistEmptyIndices() { - LuceneTestCase.expectThrows(IllegalArgumentException.class, () - -> IndicesRequestConverters.indicesExist(new GetIndexRequest())); - LuceneTestCase.expectThrows(IllegalArgumentException.class, () - -> IndicesRequestConverters.indicesExist(new GetIndexRequest((String[]) null))); + LuceneTestCase.expectThrows(IllegalArgumentException.class, () -> IndicesRequestConverters.indicesExist(new GetIndexRequest())); + LuceneTestCase.expectThrows( + IllegalArgumentException.class, + () -> IndicesRequestConverters.indicesExist(new GetIndexRequest((String[]) null)) + ); } public void testCreateIndex() throws IOException { @@ -159,8 +159,11 @@ public void testPutMapping() throws IOException { Map expectedParams = new HashMap<>(); RequestConvertersTests.setRandomTimeout(putMappingRequest, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT, expectedParams); RequestConvertersTests.setRandomMasterTimeout(putMappingRequest, expectedParams); - RequestConvertersTests.setRandomIndicesOptions(putMappingRequest::indicesOptions, - putMappingRequest::indicesOptions, expectedParams); + RequestConvertersTests.setRandomIndicesOptions( + putMappingRequest::indicesOptions, + putMappingRequest::indicesOptions, + expectedParams + ); Request request = IndicesRequestConverters.putMapping(putMappingRequest); @@ -189,8 +192,11 @@ public void testGetMapping() { } Map expectedParams = new HashMap<>(); - RequestConvertersTests.setRandomIndicesOptions(getMappingRequest::indicesOptions, - getMappingRequest::indicesOptions, expectedParams); + RequestConvertersTests.setRandomIndicesOptions( + getMappingRequest::indicesOptions, + getMappingRequest::indicesOptions, + expectedParams + ); RequestConvertersTests.setRandomMasterTimeout(getMappingRequest, expectedParams); RequestConvertersTests.setRandomLocal(getMappingRequest::local, expectedParams); @@ -230,8 +236,11 @@ public void testGetFieldMapping() { } Map expectedParams = new HashMap<>(); - RequestConvertersTests.setRandomIndicesOptions(getFieldMappingsRequest::indicesOptions, getFieldMappingsRequest::indicesOptions, - expectedParams); + RequestConvertersTests.setRandomIndicesOptions( + getFieldMappingsRequest::indicesOptions, + getFieldMappingsRequest::indicesOptions, + expectedParams + ); Request request = IndicesRequestConverters.getFieldMapping(getFieldMappingsRequest); StringJoiner endpoint = new StringJoiner("/", "/", ""); @@ -284,8 +293,11 @@ public void testDeleteIndex() { RequestConvertersTests.setRandomTimeout(deleteIndexRequest::timeout, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT, expectedParams); RequestConvertersTests.setRandomMasterTimeout(deleteIndexRequest, expectedParams); - RequestConvertersTests.setRandomIndicesOptions(deleteIndexRequest::indicesOptions, deleteIndexRequest::indicesOptions, - expectedParams); + RequestConvertersTests.setRandomIndicesOptions( + deleteIndexRequest::indicesOptions, + deleteIndexRequest::indicesOptions, + expectedParams + ); Request request = IndicesRequestConverters.deleteIndex(deleteIndexRequest); Assert.assertEquals("/" + String.join(",", indices), request.getEndpoint()); @@ -301,8 +313,11 @@ public void testGetSettings() throws IOException { Map expectedParams = new HashMap<>(); RequestConvertersTests.setRandomMasterTimeout(getSettingsRequest, expectedParams); - RequestConvertersTests.setRandomIndicesOptions(getSettingsRequest::indicesOptions, getSettingsRequest::indicesOptions, - expectedParams); + RequestConvertersTests.setRandomIndicesOptions( + getSettingsRequest::indicesOptions, + getSettingsRequest::indicesOptions, + expectedParams + ); RequestConvertersTests.setRandomLocal(getSettingsRequest::local, expectedParams); @@ -411,11 +426,17 @@ public void testCloseIndex() { CloseIndexRequest closeIndexRequest = new CloseIndexRequest(indices); Map expectedParams = new HashMap<>(); - RequestConvertersTests.setRandomTimeout(timeout -> closeIndexRequest.setTimeout(TimeValue.parseTimeValue(timeout, "test")), - AcknowledgedRequest.DEFAULT_ACK_TIMEOUT, expectedParams); + RequestConvertersTests.setRandomTimeout( + timeout -> closeIndexRequest.setTimeout(TimeValue.parseTimeValue(timeout, "test")), + AcknowledgedRequest.DEFAULT_ACK_TIMEOUT, + expectedParams + ); RequestConvertersTests.setRandomMasterTimeout(closeIndexRequest, expectedParams); - RequestConvertersTests.setRandomIndicesOptions(closeIndexRequest::indicesOptions, closeIndexRequest::indicesOptions, - expectedParams); + RequestConvertersTests.setRandomIndicesOptions( + closeIndexRequest::indicesOptions, + closeIndexRequest::indicesOptions, + expectedParams + ); RequestConvertersTests.setRandomWaitForActiveShards(closeIndexRequest::waitForActiveShards, expectedParams); Request request = IndicesRequestConverters.closeIndex(closeIndexRequest); @@ -492,8 +513,11 @@ public void testForceMerge() { } Map expectedParams = new HashMap<>(); - RequestConvertersTests.setRandomIndicesOptions(forceMergeRequest::indicesOptions, forceMergeRequest::indicesOptions, - expectedParams); + RequestConvertersTests.setRandomIndicesOptions( + forceMergeRequest::indicesOptions, + forceMergeRequest::indicesOptions, + expectedParams + ); if (ESTestCase.randomBoolean()) { forceMergeRequest.maxNumSegments(ESTestCase.randomInt()); } @@ -529,8 +553,11 @@ public void testClearCache() { clearIndicesCacheRequest.indices(indices); } Map expectedParams = new HashMap<>(); - RequestConvertersTests.setRandomIndicesOptions(clearIndicesCacheRequest::indicesOptions, clearIndicesCacheRequest::indicesOptions, - expectedParams); + RequestConvertersTests.setRandomIndicesOptions( + clearIndicesCacheRequest::indicesOptions, + clearIndicesCacheRequest::indicesOptions, + expectedParams + ); if (ESTestCase.randomBoolean()) { clearIndicesCacheRequest.queryCache(ESTestCase.randomBoolean()); } @@ -575,8 +602,11 @@ public void testExistsAlias() { getAliasesRequest.aliases(aliases); Map expectedParams = new HashMap<>(); RequestConvertersTests.setRandomLocal(getAliasesRequest::local, expectedParams); - RequestConvertersTests.setRandomIndicesOptions(getAliasesRequest::indicesOptions, getAliasesRequest::indicesOptions, - expectedParams); + RequestConvertersTests.setRandomIndicesOptions( + getAliasesRequest::indicesOptions, + getAliasesRequest::indicesOptions, + expectedParams + ); Request request = IndicesRequestConverters.existsAlias(getAliasesRequest); StringJoiner expectedEndpoint = new StringJoiner("/", "/", ""); @@ -596,15 +626,19 @@ public void testExistsAlias() { public void testExistsAliasNoAliasNoIndex() { { GetAliasesRequest getAliasesRequest = new GetAliasesRequest(); - IllegalArgumentException iae = LuceneTestCase.expectThrows(IllegalArgumentException.class, - () -> IndicesRequestConverters.existsAlias(getAliasesRequest)); + IllegalArgumentException iae = LuceneTestCase.expectThrows( + IllegalArgumentException.class, + () -> IndicesRequestConverters.existsAlias(getAliasesRequest) + ); Assert.assertEquals("existsAlias requires at least an alias or an index", iae.getMessage()); } { GetAliasesRequest getAliasesRequest = new GetAliasesRequest((String[]) null); getAliasesRequest.indices((String[]) null); - IllegalArgumentException iae = LuceneTestCase.expectThrows(IllegalArgumentException.class, - () -> IndicesRequestConverters.existsAlias(getAliasesRequest)); + IllegalArgumentException iae = LuceneTestCase.expectThrows( + IllegalArgumentException.class, + () -> IndicesRequestConverters.existsAlias(getAliasesRequest) + ); Assert.assertEquals("existsAlias requires at least an alias or an index", iae.getMessage()); } } @@ -621,14 +655,16 @@ public void testShrink() throws IOException { resizeTest(ResizeType.SHRINK, IndicesRequestConverters::shrink); } - private void resizeTest(ResizeType resizeType, CheckedFunction function) - throws IOException { + private void resizeTest(ResizeType resizeType, CheckedFunction function) throws IOException { String[] indices = RequestConvertersTests.randomIndicesNames(2, 2); ResizeRequest resizeRequest = new ResizeRequest(indices[0], indices[1]); Map expectedParams = new HashMap<>(); RequestConvertersTests.setRandomMasterTimeout(resizeRequest, expectedParams); - RequestConvertersTests.setRandomTimeout(s -> resizeRequest.setTimeout(TimeValue.parseTimeValue(s, "timeout")), - resizeRequest.timeout(), expectedParams); + RequestConvertersTests.setRandomTimeout( + s -> resizeRequest.setTimeout(TimeValue.parseTimeValue(s, "timeout")), + resizeRequest.timeout(), + expectedParams + ); if (ESTestCase.randomBoolean()) { if (ESTestCase.randomBoolean()) { @@ -651,16 +687,22 @@ private void resizeTest(ResizeType resizeType, CheckedFunction expectedParams = new HashMap<>(); RequestConvertersTests.setRandomTimeout(rolloverRequest, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT, expectedParams); RequestConvertersTests.setRandomMasterTimeout(rolloverRequest, expectedParams); @@ -680,8 +722,7 @@ public void testRollover() throws IOException { randomAliases(rolloverRequest.getCreateIndexRequest()); } if (ESTestCase.randomBoolean()) { - rolloverRequest.getCreateIndexRequest().settings( - org.elasticsearch.index.RandomCreateIndexGenerator.randomIndexSettings()); + rolloverRequest.getCreateIndexRequest().settings(org.elasticsearch.index.RandomCreateIndexGenerator.randomIndexSettings()); } RequestConvertersTests.setRandomWaitForActiveShards(rolloverRequest.getCreateIndexRequest()::waitForActiveShards, expectedParams); @@ -689,8 +730,10 @@ public void testRollover() throws IOException { if (rolloverRequest.getNewIndexName() == null) { Assert.assertEquals("/" + rolloverRequest.getAlias() + "/_rollover", request.getEndpoint()); } else { - Assert.assertEquals("/" + rolloverRequest.getAlias() + "/_rollover/" + rolloverRequest.getNewIndexName(), - request.getEndpoint()); + Assert.assertEquals( + "/" + rolloverRequest.getAlias() + "/_rollover/" + rolloverRequest.getNewIndexName(), + request.getEndpoint() + ); } Assert.assertEquals(HttpPost.METHOD_NAME, request.getMethod()); RequestConvertersTests.assertToXContentBody(rolloverRequest, request.getEntity()); @@ -702,8 +745,11 @@ public void testGetAlias() { Map expectedParams = new HashMap<>(); RequestConvertersTests.setRandomLocal(getAliasesRequest::local, expectedParams); - RequestConvertersTests.setRandomIndicesOptions(getAliasesRequest::indicesOptions, getAliasesRequest::indicesOptions, - expectedParams); + RequestConvertersTests.setRandomIndicesOptions( + getAliasesRequest::indicesOptions, + getAliasesRequest::indicesOptions, + expectedParams + ); String[] indices = ESTestCase.randomBoolean() ? null : RequestConvertersTests.randomIndicesNames(0, 2); String[] aliases = ESTestCase.randomBoolean() ? null : RequestConvertersTests.randomIndicesNames(0, 2); @@ -734,8 +780,11 @@ public void testIndexPutSettings() throws IOException { Map expectedParams = new HashMap<>(); RequestConvertersTests.setRandomMasterTimeout(updateSettingsRequest, expectedParams); RequestConvertersTests.setRandomTimeout(updateSettingsRequest::timeout, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT, expectedParams); - RequestConvertersTests.setRandomIndicesOptions(updateSettingsRequest::indicesOptions, updateSettingsRequest::indicesOptions, - expectedParams); + RequestConvertersTests.setRandomIndicesOptions( + updateSettingsRequest::indicesOptions, + updateSettingsRequest::indicesOptions, + expectedParams + ); if (ESTestCase.randomBoolean()) { updateSettingsRequest.setPreserveExisting(ESTestCase.randomBoolean()); if (updateSettingsRequest.isPreserveExisting()) { @@ -762,9 +811,10 @@ public void testPutTemplateRequest() throws Exception { names.put("-#template", "-%23template"); names.put("foo^bar", "foo%5Ebar"); - PutIndexTemplateRequest putTemplateRequest = - new PutIndexTemplateRequest(ESTestCase.randomFrom(names.keySet()), - List.of(ESTestCase.generateRandomStringArray(20, 100, false, false))); + PutIndexTemplateRequest putTemplateRequest = new PutIndexTemplateRequest( + ESTestCase.randomFrom(names.keySet()), + List.of(ESTestCase.generateRandomStringArray(20, 100, false, false)) + ); if (ESTestCase.randomBoolean()) { putTemplateRequest.order(ESTestCase.randomInt()); } @@ -776,8 +826,14 @@ public void testPutTemplateRequest() throws Exception { } Map expectedParams = new HashMap<>(); if (ESTestCase.randomBoolean()) { - putTemplateRequest.mapping("{ \"properties\": { \"field-" + ESTestCase.randomInt() + - "\" : { \"type\" : \"" + ESTestCase.randomFrom("text", "keyword") + "\" }}}", XContentType.JSON); + putTemplateRequest.mapping( + "{ \"properties\": { \"field-" + + ESTestCase.randomInt() + + "\" : { \"type\" : \"" + + ESTestCase.randomFrom("text", "keyword") + + "\" }}}", + XContentType.JSON + ); } if (ESTestCase.randomBoolean()) { putTemplateRequest.alias(new Alias("alias-" + ESTestCase.randomInt())); @@ -798,6 +854,7 @@ public void testPutTemplateRequest() throws Exception { Assert.assertThat(request.getParameters(), equalTo(expectedParams)); RequestConvertersTests.assertToXContentBody(putTemplateRequest, request.getEntity()); } + public void testValidateQuery() throws Exception { String[] indices = ESTestCase.randomBoolean() ? null : RequestConvertersTests.randomIndicesNames(0, 5); ValidateQueryRequest validateQueryRequest; @@ -808,8 +865,11 @@ public void testValidateQuery() throws Exception { validateQueryRequest.indices(indices); } Map expectedParams = new HashMap<>(); - RequestConvertersTests.setRandomIndicesOptions(validateQueryRequest::indicesOptions, validateQueryRequest::indicesOptions, - expectedParams); + RequestConvertersTests.setRandomIndicesOptions( + validateQueryRequest::indicesOptions, + validateQueryRequest::indicesOptions, + expectedParams + ); validateQueryRequest.explain(ESTestCase.randomBoolean()); validateQueryRequest.rewrite(ESTestCase.randomBoolean()); validateQueryRequest.allShards(ESTestCase.randomBoolean()); @@ -842,8 +902,10 @@ public void testGetTemplateRequest() throws Exception { RequestConvertersTests.setRandomLocal(getTemplatesRequest::setLocal, expectedParams); Request request = IndicesRequestConverters.getTemplates(getTemplatesRequest); - Assert.assertThat(request.getEndpoint(), - equalTo("/_template/" + names.stream().map(encodes::get).collect(Collectors.joining(",")))); + Assert.assertThat( + request.getEndpoint(), + equalTo("/_template/" + names.stream().map(encodes::get).collect(Collectors.joining(","))) + ); Assert.assertThat(request.getParameters(), equalTo(expectedParams)); Assert.assertThat(request.getEntity(), nullValue()); @@ -854,11 +916,10 @@ public void testGetTemplateRequest() throws Exception { } public void testTemplatesExistRequest() { - final int numberOfNames = ESTestCase.usually() - ? 1 - : ESTestCase.randomIntBetween(2, 20); - final List names = Arrays.asList(ESTestCase.randomArray(numberOfNames, numberOfNames, String[]::new, - () -> ESTestCase.randomAlphaOfLengthBetween(1, 100))); + final int numberOfNames = ESTestCase.usually() ? 1 : ESTestCase.randomIntBetween(2, 20); + final List names = Arrays.asList( + ESTestCase.randomArray(numberOfNames, numberOfNames, String[]::new, () -> ESTestCase.randomAlphaOfLengthBetween(1, 100)) + ); final Map expectedParams = new HashMap<>(); final IndexTemplatesExistRequest indexTemplatesExistRequest = new IndexTemplatesExistRequest(names); RequestConvertersTests.setRandomMasterTimeout(indexTemplatesExistRequest::setMasterNodeTimeout, expectedParams); @@ -904,8 +965,7 @@ public void testReloadAnalyzers() { } ReloadAnalyzersRequest reloadRequest = new ReloadAnalyzersRequest(indices); Map expectedParams = new HashMap<>(); - RequestConvertersTests.setRandomIndicesOptions(reloadRequest::setIndicesOptions, reloadRequest::indicesOptions, - expectedParams); + RequestConvertersTests.setRandomIndicesOptions(reloadRequest::setIndicesOptions, reloadRequest::indicesOptions, expectedParams); Request request = IndicesRequestConverters.reloadAnalyzers(reloadRequest); Assert.assertThat(request.getMethod(), equalTo(HttpPost.METHOD_NAME)); Assert.assertThat(request.getEndpoint(), equalTo(endpoint + "/_reload_search_analyzers")); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/IngestClientIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/IngestClientIT.java index a2b2c1bee628b..c14abf51b7839 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/IngestClientIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/IngestClientIT.java @@ -20,9 +20,9 @@ import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.client.core.MainRequest; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.ingest.PipelineConfiguration; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.ingest.PipelineConfiguration; import java.io.IOException; import java.util.List; @@ -35,13 +35,13 @@ public class IngestClientIT extends ESRestHighLevelClientTestCase { public void testPutPipeline() throws IOException { String id = "some_pipeline_id"; XContentBuilder pipelineBuilder = buildRandomXContentPipeline(); - PutPipelineRequest request = new PutPipelineRequest( - id, - BytesReference.bytes(pipelineBuilder), - pipelineBuilder.contentType()); + PutPipelineRequest request = new PutPipelineRequest(id, BytesReference.bytes(pipelineBuilder), pipelineBuilder.contentType()); - AcknowledgedResponse putPipelineResponse = - execute(request, highLevelClient().ingest()::putPipeline, highLevelClient().ingest()::putPipelineAsync); + AcknowledgedResponse putPipelineResponse = execute( + request, + highLevelClient().ingest()::putPipeline, + highLevelClient().ingest()::putPipelineAsync + ); assertTrue(putPipelineResponse.isAcknowledged()); } @@ -49,22 +49,24 @@ public void testGetPipeline() throws IOException { String id = "some_pipeline_id"; XContentBuilder pipelineBuilder = buildRandomXContentPipeline(); { - PutPipelineRequest request = new PutPipelineRequest( - id, - BytesReference.bytes(pipelineBuilder), - pipelineBuilder.contentType() - ); + PutPipelineRequest request = new PutPipelineRequest(id, BytesReference.bytes(pipelineBuilder), pipelineBuilder.contentType()); createPipeline(request); } GetPipelineRequest request = new GetPipelineRequest(id); - GetPipelineResponse response = - execute(request, highLevelClient().ingest()::getPipeline, highLevelClient().ingest()::getPipelineAsync); + GetPipelineResponse response = execute( + request, + highLevelClient().ingest()::getPipeline, + highLevelClient().ingest()::getPipelineAsync + ); assertTrue(response.isFound()); assertEquals(response.pipelines().get(0).getId(), id); - PipelineConfiguration expectedConfig = - new PipelineConfiguration(id, BytesReference.bytes(pipelineBuilder), pipelineBuilder.contentType()); + PipelineConfiguration expectedConfig = new PipelineConfiguration( + id, + BytesReference.bytes(pipelineBuilder), + pipelineBuilder.contentType() + ); assertEquals(expectedConfig.getConfigAsMap(), response.pipelines().get(0).getConfigAsMap()); } @@ -73,8 +75,11 @@ public void testGetNonexistentPipeline() throws IOException { GetPipelineRequest request = new GetPipelineRequest(id); - GetPipelineResponse response = - execute(request, highLevelClient().ingest()::getPipeline, highLevelClient().ingest()::getPipelineAsync); + GetPipelineResponse response = execute( + request, + highLevelClient().ingest()::getPipeline, + highLevelClient().ingest()::getPipelineAsync + ); assertFalse(response.isFound()); } @@ -86,8 +91,11 @@ public void testDeletePipeline() throws IOException { DeletePipelineRequest request = new DeletePipelineRequest(id); - AcknowledgedResponse response = - execute(request, highLevelClient().ingest()::deletePipeline, highLevelClient().ingest()::deletePipelineAsync); + AcknowledgedResponse response = execute( + request, + highLevelClient().ingest()::deletePipeline, + highLevelClient().ingest()::deletePipelineAsync + ); assertTrue(response.isAcknowledged()); } @@ -107,8 +115,7 @@ public void testSimulatePipelineVerboseWithFailure() throws IOException { testSimulatePipeline(true, true); } - private void testSimulatePipeline(boolean isVerbose, - boolean isFailure) throws IOException { + private void testSimulatePipeline(boolean isVerbose, boolean isFailure) throws IOException { XContentType xContentType = randomFrom(XContentType.values()); XContentBuilder builder = XContentBuilder.builder(xContentType.xContent()); String rankValue = isFailure ? "non-int" : Integer.toString(1234); @@ -121,20 +128,23 @@ private void testSimulatePipeline(boolean isVerbose, builder.startObject() .field("_index", "index") .field("_id", "doc_" + 1) - .startObject("_source").field("foo", "rab_" + 1).field("rank", rankValue).endObject() + .startObject("_source") + .field("foo", "rab_" + 1) + .field("rank", rankValue) + .endObject() .endObject(); } builder.endArray(); } builder.endObject(); - SimulatePipelineRequest request = new SimulatePipelineRequest( - BytesReference.bytes(builder), - builder.contentType() - ); + SimulatePipelineRequest request = new SimulatePipelineRequest(BytesReference.bytes(builder), builder.contentType()); request.setVerbose(isVerbose); - SimulatePipelineResponse response = - execute(request, highLevelClient().ingest()::simulate, highLevelClient().ingest()::simulateAsync); + SimulatePipelineResponse response = execute( + request, + highLevelClient().ingest()::simulate, + highLevelClient().ingest()::simulateAsync + ); List results = response.getResults(); assertEquals(1, results.size()); if (isVerbose) { @@ -143,18 +153,15 @@ private void testSimulatePipeline(boolean isVerbose, assertEquals(2, verboseResult.getProcessorResults().size()); if (isFailure) { assertNotNull(verboseResult.getProcessorResults().get(1).getFailure()); - assertThat(verboseResult.getProcessorResults().get(1).getFailure().getMessage(), - containsString("unable to convert [non-int] to integer")); - } else { - assertEquals( - verboseResult.getProcessorResults().get(0).getIngestDocument() - .getFieldValue("foo", String.class), - "bar" + assertThat( + verboseResult.getProcessorResults().get(1).getFailure().getMessage(), + containsString("unable to convert [non-int] to integer") ); + } else { + assertEquals(verboseResult.getProcessorResults().get(0).getIngestDocument().getFieldValue("foo", String.class), "bar"); assertEquals( Integer.valueOf(1234), - verboseResult.getProcessorResults().get(1).getIngestDocument() - .getFieldValue("rank", Integer.class) + verboseResult.getProcessorResults().get(1).getIngestDocument().getFieldValue("rank", Integer.class) ); } } else { @@ -162,26 +169,21 @@ private void testSimulatePipeline(boolean isVerbose, SimulateDocumentBaseResult baseResult = (SimulateDocumentBaseResult) results.get(0); if (isFailure) { assertNotNull(baseResult.getFailure()); - assertThat(baseResult.getFailure().getMessage(), - containsString("unable to convert [non-int] to integer")); + assertThat(baseResult.getFailure().getMessage(), containsString("unable to convert [non-int] to integer")); } else { assertNotNull(baseResult.getIngestDocument()); - assertEquals( - baseResult.getIngestDocument().getFieldValue("foo", String.class), - "bar" - ); - assertEquals( - Integer.valueOf(1234), - baseResult.getIngestDocument() - .getFieldValue("rank", Integer.class) - ); + assertEquals(baseResult.getIngestDocument().getFieldValue("foo", String.class), "bar"); + assertEquals(Integer.valueOf(1234), baseResult.getIngestDocument().getFieldValue("rank", Integer.class)); } } } public void testGeoIpStats() throws IOException { - GeoIpStatsResponse response = execute(new MainRequest(), highLevelClient().ingest()::geoIpStats, - highLevelClient().ingest()::geoIpStatsAsync); + GeoIpStatsResponse response = execute( + new MainRequest(), + highLevelClient().ingest()::geoIpStats, + highLevelClient().ingest()::geoIpStatsAsync + ); assertEquals(0, response.getDatabasesCount()); assertEquals(0, response.getSkippedDownloads()); assertEquals(0, response.getSuccessfulDownloads()); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/IngestRequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/IngestRequestConvertersTests.java index f3d3461cfadaf..e087eb13a4219 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/IngestRequestConvertersTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/IngestRequestConvertersTests.java @@ -18,8 +18,8 @@ import org.elasticsearch.action.ingest.SimulatePipelineRequest; import org.elasticsearch.action.support.master.AcknowledgedRequest; import org.elasticsearch.common.bytes.BytesArray; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentType; import org.junit.Assert; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/LicenseIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/LicenseIT.java index a950abcb99bf4..df3cb64a90f1f 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/LicenseIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/LicenseIT.java @@ -54,8 +54,7 @@ public class LicenseIT extends ESRestHighLevelClientTestCase { @BeforeClass public static void checkForSnapshot() { - assumeTrue("Trial license used to rollback is only valid when tested against snapshot/test builds", - Build.CURRENT.isSnapshot()); + assumeTrue("Trial license used to rollback is only valid when tested against snapshot/test builds", Build.CURRENT.isSnapshot()); } @After @@ -95,27 +94,41 @@ public void testStartTrial() throws Exception { } public void testPutInvalidTrialLicense() throws Exception { - assumeTrue("Trial license is only valid when tested against snapshot/test builds", - Build.CURRENT.isSnapshot()); + assumeTrue("Trial license is only valid when tested against snapshot/test builds", Build.CURRENT.isSnapshot()); // use a hard-coded trial license for 20 yrs to be able to roll back from another licenses final String signature = "xx"; // Truncated, so it is expected to fail validation - final String licenseDefinition = Strings.toString(jsonBuilder() - .startObject() - .field("licenses", List.of( - Map.of( - "uid", "96fc37c6-6fc9-43e2-a40d-73143850cd72", - "type", "trial", - // 2018-10-16 07:02:48 UTC - "issue_date_in_millis", "1539673368158", - // 2038-10-11 07:02:48 UTC, 20 yrs later - "expiry_date_in_millis", "2170393368158", - "max_nodes", "5", - "issued_to", "client_rest-high-level_integTestCluster", - "issuer", "elasticsearch", - "start_date_in_millis", "-1", - "signature", signature))) - .endObject()); + final String licenseDefinition = Strings.toString( + jsonBuilder().startObject() + .field( + "licenses", + List.of( + Map.of( + "uid", + "96fc37c6-6fc9-43e2-a40d-73143850cd72", + "type", + "trial", + // 2018-10-16 07:02:48 UTC + "issue_date_in_millis", + "1539673368158", + // 2038-10-11 07:02:48 UTC, 20 yrs later + "expiry_date_in_millis", + "2170393368158", + "max_nodes", + "5", + "issued_to", + "client_rest-high-level_integTestCluster", + "issuer", + "elasticsearch", + "start_date_in_millis", + "-1", + "signature", + signature + ) + ) + ) + .endObject() + ); final PutLicenseRequest request = new PutLicenseRequest(); request.setAcknowledge(true); @@ -129,31 +142,45 @@ public void testPutInvalidTrialLicense() throws Exception { } public static void putTrialLicense() throws IOException { - assumeTrue("Trial license is only valid when tested against snapshot/test builds", - Build.CURRENT.isSnapshot()); + assumeTrue("Trial license is only valid when tested against snapshot/test builds", Build.CURRENT.isSnapshot()); // use a hard-coded trial license for 20 yrs to be able to roll back from another licenses final String signature = - "AAAABAAAAA3FXON9kGmNqmH+ASDWAAAAIAo5/x6hrsGh1GqqrJmy4qgmEC7gK0U4zQ6q5ZEMhm4jAAABAAcdKHL0BfM2uqTgT7BDuFxX5lb" - + "t/bHDVJ421Wwgm5p3IMbw/W13iiAHz0hhDziF7acJbc/y65L+BKGtVC1gSSHeLDHaAD66VrjKxfc7VbGyJIAYBOdujf0rheurmaD3IcNo" - + "/tWDjCdtTwrNziFkorsGcPadBP5Yc6csk3/Q74DlfiYweMBxLUfkBERwxwd5OQS6ujGvl/4bb8p5zXvOw8vMSaAXSXXnExP6lam+0934W" - + "0kHvU7IGk+fCUjOaiSWKSoE4TEcAtVNYj/oRoRtfQ1KQGpdCHxTHs1BimdZaG0nBHDsvhYlVVLSvHN6QzqsHWgFDG6JJxhtU872oTRSUHA="; - final String licenseDefinition = Strings.toString(jsonBuilder() - .startObject() - .field("licenses", List.of( - Map.of( - "uid", "96fc37c6-6fc9-43e2-a40d-73143850cd72", - "type", "trial", - // 2018-10-16 07:02:48 UTC - "issue_date_in_millis", "1539673368158", - // 2038-10-11 07:02:48 UTC, 20 yrs later - "expiry_date_in_millis", "2170393368158", - "max_nodes", "5", - "issued_to", "client_rest-high-level_integTestCluster", - "issuer", "elasticsearch", - "start_date_in_millis", "-1", - "signature", signature))) - .endObject()); + "AAAABAAAAA3FXON9kGmNqmH+ASDWAAAAIAo5/x6hrsGh1GqqrJmy4qgmEC7gK0U4zQ6q5ZEMhm4jAAABAAcdKHL0BfM2uqTgT7BDuFxX5lb" + + "t/bHDVJ421Wwgm5p3IMbw/W13iiAHz0hhDziF7acJbc/y65L+BKGtVC1gSSHeLDHaAD66VrjKxfc7VbGyJIAYBOdujf0rheurmaD3IcNo" + + "/tWDjCdtTwrNziFkorsGcPadBP5Yc6csk3/Q74DlfiYweMBxLUfkBERwxwd5OQS6ujGvl/4bb8p5zXvOw8vMSaAXSXXnExP6lam+0934W" + + "0kHvU7IGk+fCUjOaiSWKSoE4TEcAtVNYj/oRoRtfQ1KQGpdCHxTHs1BimdZaG0nBHDsvhYlVVLSvHN6QzqsHWgFDG6JJxhtU872oTRSUHA="; + final String licenseDefinition = Strings.toString( + jsonBuilder().startObject() + .field( + "licenses", + List.of( + Map.of( + "uid", + "96fc37c6-6fc9-43e2-a40d-73143850cd72", + "type", + "trial", + // 2018-10-16 07:02:48 UTC + "issue_date_in_millis", + "1539673368158", + // 2038-10-11 07:02:48 UTC, 20 yrs later + "expiry_date_in_millis", + "2170393368158", + "max_nodes", + "5", + "issued_to", + "client_rest-high-level_integTestCluster", + "issuer", + "elasticsearch", + "start_date_in_millis", + "-1", + "signature", + signature + ) + ) + ) + .endObject() + ); final PutLicenseRequest request = new PutLicenseRequest(); request.setAcknowledge(true); @@ -173,9 +200,13 @@ public void testStartBasic() throws Exception { assertThat(response.isAcknowledged(), equalTo(false)); assertThat(response.isBasicStarted(), equalTo(false)); assertThat(response.getErrorMessage(), equalTo("Operation failed: Needs acknowledgement.")); - assertThat(response.getAcknowledgeMessage(), - containsString("This license update requires acknowledgement. " + - "To acknowledge the license, please read the following messages and call /start_basic again")); + assertThat( + response.getAcknowledgeMessage(), + containsString( + "This license update requires acknowledgement. " + + "To acknowledge the license, please read the following messages and call /start_basic again" + ) + ); assertNotEmptyAcknowledgeMessages(response.getAcknowledgeMessages()); } // case where we acknowledge and the basic is started successfully diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/LicenseRequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/LicenseRequestConvertersTests.java index 7d4791dc977e7..7f03136994a93 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/LicenseRequestConvertersTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/LicenseRequestConvertersTests.java @@ -8,27 +8,26 @@ package org.elasticsearch.client; -import org.apache.http.client.methods.HttpPost; -import org.elasticsearch.client.license.StartTrialRequest; -import org.elasticsearch.action.support.master.AcknowledgedRequest; -import org.elasticsearch.client.license.StartBasicRequest; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; +import org.elasticsearch.action.support.master.AcknowledgedRequest; import org.elasticsearch.client.license.DeleteLicenseRequest; import org.elasticsearch.client.license.GetLicenseRequest; import org.elasticsearch.client.license.PutLicenseRequest; +import org.elasticsearch.client.license.StartBasicRequest; +import org.elasticsearch.client.license.StartTrialRequest; import org.elasticsearch.test.ESTestCase; import java.util.HashMap; import java.util.Map; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.nullValue; import static org.elasticsearch.client.RequestConvertersTests.setRandomMasterTimeout; import static org.elasticsearch.client.RequestConvertersTests.setRandomTimeout; +import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.is; - +import static org.hamcrest.CoreMatchers.nullValue; public class LicenseRequestConvertersTests extends ESTestCase { @@ -81,9 +80,7 @@ public void testDeleteLicense() { public void testStartTrial() { final boolean acknowledge = randomBoolean(); - final String licenseType = randomBoolean() - ? randomAlphaOfLengthBetween(3, 10) - : null; + final String licenseType = randomBoolean() ? randomAlphaOfLengthBetween(3, 10) : null; final Map expectedParams = new HashMap<>(); expectedParams.put("acknowledge", Boolean.toString(acknowledge)); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/MLRequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/MLRequestConvertersTests.java index 30243e9413715..68339e482ce40 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/MLRequestConvertersTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/MLRequestConvertersTests.java @@ -99,14 +99,14 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.search.SearchModule; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.search.SearchModule; -import org.elasticsearch.test.ESTestCase; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -185,7 +185,7 @@ public void testOpenJob() throws Exception { Request request = MLRequestConverters.openJob(openJobRequest); assertEquals(HttpPost.METHOD_NAME, request.getMethod()); assertEquals("/_ml/anomaly_detectors/" + jobId + "/_open", request.getEndpoint()); - assertEquals(requestEntityToString(request), "{\"job_id\":\""+ jobId +"\",\"timeout\":\"10m\"}"); + assertEquals(requestEntityToString(request), "{\"job_id\":\"" + jobId + "\",\"timeout\":\"10m\"}"); } public void testCloseJob() throws Exception { @@ -204,17 +204,20 @@ public void testCloseJob() throws Exception { request = MLRequestConverters.closeJob(closeJobRequest); assertEquals("/_ml/anomaly_detectors/" + jobId + ",otherjobs*/_close", request.getEndpoint()); - assertEquals("{\"job_id\":\"somejobid,otherjobs*\",\"timeout\":\"10m\",\"force\":true,\"allow_no_match\":false}", - requestEntityToString(request)); + assertEquals( + "{\"job_id\":\"somejobid,otherjobs*\",\"timeout\":\"10m\",\"force\":true,\"allow_no_match\":false}", + requestEntityToString(request) + ); } public void testDeleteExpiredData() throws Exception { - float requestsPerSec = randomBoolean() ? -1.0f : (float)randomDoubleBetween(0.0, 100000.0, false); + float requestsPerSec = randomBoolean() ? -1.0f : (float) randomDoubleBetween(0.0, 100000.0, false); String jobId = randomBoolean() ? null : randomAlphaOfLength(8); DeleteExpiredDataRequest deleteExpiredDataRequest = new DeleteExpiredDataRequest( jobId, requestsPerSec, - TimeValue.timeValueHours(1)); + TimeValue.timeValueHours(1) + ); Request request = MLRequestConverters.deleteExpiredData(deleteExpiredDataRequest); assertEquals(HttpDelete.METHOD_NAME, request.getMethod()); @@ -224,8 +227,10 @@ public void testDeleteExpiredData() throws Exception { if (jobId == null) { assertEquals("{\"requests_per_second\":" + requestsPerSec + ",\"timeout\":\"1h\"}", requestEntityToString(request)); } else { - assertEquals("{\"job_id\":\"" + jobId + "\",\"requests_per_second\":" + requestsPerSec + ",\"timeout\":\"1h\"}", - requestEntityToString(request)); + assertEquals( + "{\"job_id\":\"" + jobId + "\",\"requests_per_second\":" + requestsPerSec + ",\"timeout\":\"1h\"}", + requestEntityToString(request) + ); } } @@ -266,9 +271,12 @@ public void testFlushJob() throws Exception { flushJobRequest.setCalcInterim(true); request = MLRequestConverters.flushJob(flushJobRequest); assertEquals( - "{\"job_id\":\"" + jobId + "\",\"calc_interim\":true,\"start\":\"105\"," + - "\"end\":\"200\",\"advance_time\":\"100\",\"skip_time\":\"1000\"}", - requestEntityToString(request)); + "{\"job_id\":\"" + + jobId + + "\",\"calc_interim\":true,\"start\":\"105\"," + + "\"end\":\"200\",\"advance_time\":\"100\",\"skip_time\":\"1000\"}", + requestEntityToString(request) + ); } public void testForecastJob() throws Exception { @@ -365,9 +373,10 @@ public void testStopDatafeed() throws Exception { datafeedRequest.setAllowNoMatch(true); Request request = MLRequestConverters.stopDatafeed(datafeedRequest); assertEquals(HttpPost.METHOD_NAME, request.getMethod()); - assertEquals("/_ml/datafeeds/" + - Strings.collectionToCommaDelimitedString(datafeedRequest.getDatafeedIds()) + - "/_stop", request.getEndpoint()); + assertEquals( + "/_ml/datafeeds/" + Strings.collectionToCommaDelimitedString(datafeedRequest.getDatafeedIds()) + "/_stop", + request.getEndpoint() + ); try (XContentParser parser = createParser(JsonXContent.jsonXContent, request.getEntity().getContent())) { StopDatafeedRequest parsedDatafeedRequest = StopDatafeedRequest.PARSER.apply(parser, null); assertThat(parsedDatafeedRequest, equalTo(datafeedRequest)); @@ -427,15 +436,14 @@ public void testDeleteForecast() { request = MLRequestConverters.deleteForecast(deleteForecastRequest); assertEquals( - "/_ml/anomaly_detectors/" + - jobId + - "/_forecast/" + - Strings.collectionToCommaDelimitedString(deleteForecastRequest.getForecastIds()), - request.getEndpoint()); - assertEquals("10s", - request.getParameters().get(DeleteForecastRequest.TIMEOUT.getPreferredName())); - assertEquals(Boolean.toString(true), - request.getParameters().get(DeleteForecastRequest.ALLOW_NO_FORECASTS.getPreferredName())); + "/_ml/anomaly_detectors/" + + jobId + + "/_forecast/" + + Strings.collectionToCommaDelimitedString(deleteForecastRequest.getForecastIds()), + request.getEndpoint() + ); + assertEquals("10s", request.getParameters().get(DeleteForecastRequest.TIMEOUT.getPreferredName())); + assertEquals(Boolean.toString(true), request.getParameters().get(DeleteForecastRequest.ALLOW_NO_FORECASTS.getPreferredName())); } public void testDeleteModelSnapshot() { @@ -470,7 +478,6 @@ public void testGetCategories() throws IOException { GetCategoriesRequest getCategoriesRequest = new GetCategoriesRequest(jobId); getCategoriesRequest.setPageParams(new PageParams(100, 300)); - Request request = MLRequestConverters.getCategories(getCategoriesRequest); assertEquals(HttpGet.METHOD_NAME, request.getMethod()); assertEquals("/_ml/anomaly_detectors/" + jobId + "/results/categories", request.getEndpoint()); @@ -485,7 +492,6 @@ public void testGetModelSnapshots() throws IOException { GetModelSnapshotsRequest getModelSnapshotsRequest = new GetModelSnapshotsRequest(jobId); getModelSnapshotsRequest.setPageParams(new PageParams(100, 300)); - Request request = MLRequestConverters.getModelSnapshots(getModelSnapshotsRequest); assertEquals(HttpGet.METHOD_NAME, request.getMethod()); assertEquals("/_ml/anomaly_detectors/" + jobId + "/model_snapshots", request.getEndpoint()); @@ -518,10 +524,12 @@ public void testUpgradeJobModelSnapshot() { boolean waitForCompletion = randomBoolean(); boolean includeTimeout = randomBoolean(); boolean includeWaitForCompletion = randomBoolean(); - UpgradeJobModelSnapshotRequest upgradeJobModelSnapshotRequest = new UpgradeJobModelSnapshotRequest(jobId, + UpgradeJobModelSnapshotRequest upgradeJobModelSnapshotRequest = new UpgradeJobModelSnapshotRequest( + jobId, snapshotId, includeTimeout ? timeout : null, - includeWaitForCompletion ? waitForCompletion : null); + includeWaitForCompletion ? waitForCompletion : null + ); Request request = MLRequestConverters.upgradeJobSnapshot(upgradeJobModelSnapshotRequest); assertEquals(HttpPost.METHOD_NAME, request.getMethod()); @@ -545,8 +553,7 @@ public void testRevertModelSnapshot() throws IOException { Request request = MLRequestConverters.revertModelSnapshot(revertModelSnapshotRequest); assertEquals(HttpPost.METHOD_NAME, request.getMethod()); - assertEquals("/_ml/anomaly_detectors/" + jobId + "/model_snapshots/" + snapshotId + "/_revert", - request.getEndpoint()); + assertEquals("/_ml/anomaly_detectors/" + jobId + "/model_snapshots/" + snapshotId + "/_revert", request.getEndpoint()); try (XContentParser parser = createParser(JsonXContent.jsonXContent, request.getEntity().getContent())) { RevertModelSnapshotRequest parsedRequest = RevertModelSnapshotRequest.PARSER.apply(parser, null); assertThat(parsedRequest, equalTo(revertModelSnapshotRequest)); @@ -718,9 +725,11 @@ public void testGetCalendarEvents() throws IOException { public void testPostCalendarEvent() throws Exception { String calendarId = randomAlphaOfLength(10); - List events = Arrays.asList(ScheduledEventTests.testInstance(), + List events = Arrays.asList( + ScheduledEventTests.testInstance(), ScheduledEventTests.testInstance(), - ScheduledEventTests.testInstance()); + ScheduledEventTests.testInstance() + ); PostCalendarEventRequest postCalendarEventRequest = new PostCalendarEventRequest(calendarId, events); Request request = MLRequestConverters.postCalendarEvents(postCalendarEventRequest); @@ -745,9 +754,8 @@ public void testEstimateModelMemory() throws Exception { String byFieldName = randomAlphaOfLength(10); String influencerFieldName = randomAlphaOfLength(10); AnalysisConfig analysisConfig = AnalysisConfig.builder( - Collections.singletonList( - Detector.builder().setFunction("count").setByFieldName(byFieldName).build() - )).setInfluencers(Collections.singletonList(influencerFieldName)).build(); + Collections.singletonList(Detector.builder().setFunction("count").setByFieldName(byFieldName).build()) + ).setInfluencers(Collections.singletonList(influencerFieldName)).build(); EstimateModelMemoryRequest estimateModelMemoryRequest = new EstimateModelMemoryRequest(analysisConfig); estimateModelMemoryRequest.setOverallCardinality(Collections.singletonMap(byFieldName, randomNonNegativeLong())); estimateModelMemoryRequest.setMaxBucketCardinality(Collections.singletonMap(influencerFieldName, randomNonNegativeLong())); @@ -786,8 +794,7 @@ public void testGetDataFrameAnalytics() { String configId1 = randomAlphaOfLength(10); String configId2 = randomAlphaOfLength(10); String configId3 = randomAlphaOfLength(10); - GetDataFrameAnalyticsRequest getRequest = new GetDataFrameAnalyticsRequest(configId1, configId2, configId3) - .setAllowNoMatch(false) + GetDataFrameAnalyticsRequest getRequest = new GetDataFrameAnalyticsRequest(configId1, configId2, configId3).setAllowNoMatch(false) .setPageParams(new PageParams(100, 300)); Request request = MLRequestConverters.getDataFrameAnalytics(getRequest); @@ -821,8 +828,9 @@ public void testStartDataFrameAnalytics() { } public void testStartDataFrameAnalytics_WithTimeout() { - StartDataFrameAnalyticsRequest startRequest = new StartDataFrameAnalyticsRequest(randomAlphaOfLength(10)) - .setTimeout(TimeValue.timeValueMinutes(1)); + StartDataFrameAnalyticsRequest startRequest = new StartDataFrameAnalyticsRequest(randomAlphaOfLength(10)).setTimeout( + TimeValue.timeValueMinutes(1) + ); Request request = MLRequestConverters.startDataFrameAnalytics(startRequest); assertEquals(HttpPost.METHOD_NAME, request.getMethod()); assertEquals("/_ml/data_frame/analytics/" + startRequest.getId() + "/_start", request.getEndpoint()); @@ -839,17 +847,16 @@ public void testStopDataFrameAnalytics() { } public void testStopDataFrameAnalytics_WithParams() { - StopDataFrameAnalyticsRequest stopRequest = new StopDataFrameAnalyticsRequest(randomAlphaOfLength(10)) - .setTimeout(TimeValue.timeValueMinutes(1)) - .setAllowNoMatch(false) - .setForce(true); + StopDataFrameAnalyticsRequest stopRequest = new StopDataFrameAnalyticsRequest(randomAlphaOfLength(10)).setTimeout( + TimeValue.timeValueMinutes(1) + ).setAllowNoMatch(false).setForce(true); Request request = MLRequestConverters.stopDataFrameAnalytics(stopRequest); assertEquals(HttpPost.METHOD_NAME, request.getMethod()); assertEquals("/_ml/data_frame/analytics/" + stopRequest.getId() + "/_stop", request.getEndpoint()); - assertThat(request.getParameters(), allOf( - hasEntry("timeout", "1m"), - hasEntry("allow_no_match", "false"), - hasEntry("force", "true"))); + assertThat( + request.getParameters(), + allOf(hasEntry("timeout", "1m"), hasEntry("allow_no_match", "false"), hasEntry("force", "true")) + ); assertNull(request.getEntity()); } @@ -919,8 +926,7 @@ public void testGetTrainedModels() { String modelId1 = randomAlphaOfLength(10); String modelId2 = randomAlphaOfLength(10); String modelId3 = randomAlphaOfLength(10); - GetTrainedModelsRequest getRequest = new GetTrainedModelsRequest(modelId1, modelId2, modelId3) - .setAllowNoMatch(false) + GetTrainedModelsRequest getRequest = new GetTrainedModelsRequest(modelId1, modelId2, modelId3).setAllowNoMatch(false) .setDecompressDefinition(true) .includeDefinition() .setTags("tag1", "tag2") @@ -929,7 +935,8 @@ public void testGetTrainedModels() { Request request = MLRequestConverters.getTrainedModels(getRequest); assertEquals(HttpGet.METHOD_NAME, request.getMethod()); assertEquals("/_ml/trained_models/" + modelId1 + "," + modelId2 + "," + modelId3, request.getEndpoint()); - assertThat(request.getParameters(), + assertThat( + request.getParameters(), allOf( hasEntry("from", "100"), hasEntry("size", "300"), @@ -937,7 +944,8 @@ public void testGetTrainedModels() { hasEntry("decompress_definition", "true"), hasEntry("tags", "tag1,tag2"), hasEntry("include", "definition") - )); + ) + ); assertNull(request.getEntity()); } @@ -945,19 +953,13 @@ public void testGetTrainedModelsStats() { String modelId1 = randomAlphaOfLength(10); String modelId2 = randomAlphaOfLength(10); String modelId3 = randomAlphaOfLength(10); - GetTrainedModelsStatsRequest getRequest = new GetTrainedModelsStatsRequest(modelId1, modelId2, modelId3) - .setAllowNoMatch(false) + GetTrainedModelsStatsRequest getRequest = new GetTrainedModelsStatsRequest(modelId1, modelId2, modelId3).setAllowNoMatch(false) .setPageParams(new PageParams(100, 300)); Request request = MLRequestConverters.getTrainedModelsStats(getRequest); assertEquals(HttpGet.METHOD_NAME, request.getMethod()); assertEquals("/_ml/trained_models/" + modelId1 + "," + modelId2 + "," + modelId3 + "/_stats", request.getEndpoint()); - assertThat(request.getParameters(), - allOf( - hasEntry("from", "100"), - hasEntry("size", "300"), - hasEntry("allow_no_match", "false") - )); + assertThat(request.getParameters(), allOf(hasEntry("from", "100"), hasEntry("size", "300"), hasEntry("allow_no_match", "false"))); assertNull(request.getEntity()); } @@ -1072,7 +1074,7 @@ public void testUpdateFilter() throws IOException { Request request = MLRequestConverters.updateFilter(updateFilterRequest); assertEquals(HttpPost.METHOD_NAME, request.getMethod()); - assertThat(request.getEndpoint(), equalTo("/_ml/filters/"+filterId+"/_update")); + assertThat(request.getEndpoint(), equalTo("/_ml/filters/" + filterId + "/_update")); try (XContentParser parser = createParser(JsonXContent.jsonXContent, request.getEntity().getContent())) { UpdateFilterRequest parsedFilterRequest = UpdateFilterRequest.PARSER.apply(parser, null); assertThat(parsedFilterRequest, equalTo(updateFilterRequest)); @@ -1128,8 +1130,9 @@ protected NamedXContentRegistry xContentRegistry() { } private static Job createValidJob(String jobId) { - AnalysisConfig.Builder analysisConfig = AnalysisConfig.builder(Collections.singletonList( - Detector.builder().setFunction("count").build())); + AnalysisConfig.Builder analysisConfig = AnalysisConfig.builder( + Collections.singletonList(Detector.builder().setFunction("count").build()) + ); Job.Builder jobBuilder = Job.builder(jobId); jobBuilder.setAnalysisConfig(analysisConfig); return jobBuilder.build(); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/MachineLearningGetResultsIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/MachineLearningGetResultsIT.java index 7e4ff761209e0..0ff75f9aa762f 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/MachineLearningGetResultsIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/MachineLearningGetResultsIT.java @@ -15,10 +15,10 @@ import org.elasticsearch.client.ml.GetBucketsResponse; import org.elasticsearch.client.ml.GetCategoriesRequest; import org.elasticsearch.client.ml.GetCategoriesResponse; -import org.elasticsearch.client.ml.GetModelSnapshotsRequest; -import org.elasticsearch.client.ml.GetModelSnapshotsResponse; import org.elasticsearch.client.ml.GetInfluencersRequest; import org.elasticsearch.client.ml.GetInfluencersResponse; +import org.elasticsearch.client.ml.GetModelSnapshotsRequest; +import org.elasticsearch.client.ml.GetModelSnapshotsResponse; import org.elasticsearch.client.ml.GetOverallBucketsRequest; import org.elasticsearch.client.ml.GetOverallBucketsResponse; import org.elasticsearch.client.ml.GetRecordsRequest; @@ -34,8 +34,8 @@ import org.elasticsearch.client.ml.job.results.Influencer; import org.elasticsearch.client.ml.job.results.OverallBucket; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.test.XContentTestUtils; +import org.elasticsearch.xcontent.XContentType; import org.junit.After; import org.junit.Before; @@ -96,11 +96,27 @@ private void addBucketIndexRequest(long timestamp, boolean isInterim, BulkReques IndexRequest indexRequest = new IndexRequest(RESULTS_INDEX); double bucketScore = randomDoubleBetween(0.0, 100.0, true); bucketStats.report(bucketScore); - indexRequest.source("{\"job_id\":\"" + JOB_ID + "\", \"result_type\":\"bucket\", \"timestamp\": " + timestamp + "," + - "\"bucket_span\": 3600,\"is_interim\": " + isInterim + ", \"anomaly_score\": " + bucketScore + - ", \"bucket_influencers\":[{\"job_id\": \"" + JOB_ID + "\", \"result_type\":\"bucket_influencer\", " + - "\"influencer_field_name\": \"bucket_time\", \"timestamp\": " + timestamp + ", \"bucket_span\": 3600, " + - "\"is_interim\": " + isInterim + "}]}", XContentType.JSON); + indexRequest.source( + "{\"job_id\":\"" + + JOB_ID + + "\", \"result_type\":\"bucket\", \"timestamp\": " + + timestamp + + "," + + "\"bucket_span\": 3600,\"is_interim\": " + + isInterim + + ", \"anomaly_score\": " + + bucketScore + + ", \"bucket_influencers\":[{\"job_id\": \"" + + JOB_ID + + "\", \"result_type\":\"bucket_influencer\", " + + "\"influencer_field_name\": \"bucket_time\", \"timestamp\": " + + timestamp + + ", \"bucket_span\": 3600, " + + "\"is_interim\": " + + isInterim + + "}]}", + XContentType.JSON + ); bulkRequest.add(indexRequest); } @@ -119,17 +135,40 @@ private void addRecordIndexRequest(long timestamp, boolean isInterim, BulkReques double recordScore = randomDoubleBetween(0.0, 100.0, true); recordStats.report(recordScore); double p = randomDoubleBetween(0.0, 0.05, false); - indexRequest.source("{\"job_id\":\"" + JOB_ID + "\", \"result_type\":\"record\", \"timestamp\": " + timestamp + "," + - "\"bucket_span\": 3600,\"is_interim\": " + isInterim + ", \"record_score\": " + recordScore + ", \"probability\": " - + p + "}", XContentType.JSON); + indexRequest.source( + "{\"job_id\":\"" + + JOB_ID + + "\", \"result_type\":\"record\", \"timestamp\": " + + timestamp + + "," + + "\"bucket_span\": 3600,\"is_interim\": " + + isInterim + + ", \"record_score\": " + + recordScore + + ", \"probability\": " + + p + + "}", + XContentType.JSON + ); bulkRequest.add(indexRequest); } private void addCategoryIndexRequest(long categoryId, String categoryName, BulkRequest bulkRequest) { IndexRequest indexRequest = new IndexRequest(RESULTS_INDEX); - indexRequest.source("{\"job_id\":\"" + JOB_ID + "\", \"category_id\": " + categoryId + ", \"terms\": \"" + - categoryName + "\", \"regex\": \".*?" + categoryName + ".*\", \"max_matching_length\": 3, \"examples\": [\"" + - categoryName + "\"]}", XContentType.JSON); + indexRequest.source( + "{\"job_id\":\"" + + JOB_ID + + "\", \"category_id\": " + + categoryId + + ", \"terms\": \"" + + categoryName + + "\", \"regex\": \".*?" + + categoryName + + ".*\", \"max_matching_length\": 3, \"examples\": [\"" + + categoryName + + "\"]}", + XContentType.JSON + ); bulkRequest.add(indexRequest); } @@ -138,7 +177,7 @@ private void addCategoriesIndexRequests(BulkRequest bulkRequest) { List categories = Arrays.asList("AAL", "JZA", "JBU"); for (int i = 0; i < categories.size(); i++) { - addCategoryIndexRequest(i+1, categories.get(i), bulkRequest); + addCategoryIndexRequest(i + 1, categories.get(i), bulkRequest); } } @@ -296,16 +335,21 @@ public void testGetModelSnapshots() throws IOException { request.setDesc(false); request.setPageParams(new PageParams(0, 10000)); - GetModelSnapshotsResponse response = execute(request, machineLearningClient::getModelSnapshots, - machineLearningClient::getModelSnapshotsAsync); + GetModelSnapshotsResponse response = execute( + request, + machineLearningClient::getModelSnapshots, + machineLearningClient::getModelSnapshotsAsync + ); assertThat(response.count(), equalTo(4L)); assertThat(response.snapshots().size(), equalTo(4)); assertThat(response.snapshots().get(0).getJobId(), equalTo(JOB_ID)); assertThat(response.snapshots().get(0).getSnapshotId(), equalTo("1541587919")); assertThat(response.snapshots().get(0).getSnapshotDocCount(), equalTo(1)); - assertThat(response.snapshots().get(0).getDescription(), equalTo("State persisted due to job close at" + - " 2018-11-07T10:51:59+0000")); + assertThat( + response.snapshots().get(0).getDescription(), + equalTo("State persisted due to job close at" + " 2018-11-07T10:51:59+0000") + ); assertThat(response.snapshots().get(0).getSnapshotDocCount(), equalTo(1)); assertThat(response.snapshots().get(0).getTimestamp(), equalTo(new Date(1541587919000L))); assertThat(response.snapshots().get(0).getLatestRecordTimeStamp(), equalTo(new Date(1519931700000L))); @@ -319,15 +363,19 @@ public void testGetModelSnapshots() throws IOException { assertThat(response.snapshots().get(0).getModelSizeStats().getTotalOverFieldCount(), equalTo(0L)); assertThat(response.snapshots().get(0).getModelSizeStats().getTotalPartitionFieldCount(), equalTo(2L)); assertThat(response.snapshots().get(0).getModelSizeStats().getBucketAllocationFailuresCount(), equalTo(0L)); - assertThat(response.snapshots().get(0).getModelSizeStats().getMemoryStatus(), - equalTo(ModelSizeStats.MemoryStatus.fromString("ok"))); + assertThat( + response.snapshots().get(0).getModelSizeStats().getMemoryStatus(), + equalTo(ModelSizeStats.MemoryStatus.fromString("ok")) + ); assertThat(response.snapshots().get(0).getModelSizeStats().getAssignmentMemoryBasis(), nullValue()); assertThat(response.snapshots().get(1).getJobId(), equalTo(JOB_ID)); assertThat(response.snapshots().get(1).getSnapshotId(), equalTo("1541587929")); assertThat(response.snapshots().get(1).getSnapshotDocCount(), equalTo(1)); - assertThat(response.snapshots().get(1).getDescription(), equalTo("State persisted due to job close at" + - " 2018-11-07T10:52:09+0000")); + assertThat( + response.snapshots().get(1).getDescription(), + equalTo("State persisted due to job close at" + " 2018-11-07T10:52:09+0000") + ); assertThat(response.snapshots().get(1).getSnapshotDocCount(), equalTo(1)); assertThat(response.snapshots().get(1).getTimestamp(), equalTo(new Date(1541587929000L))); assertThat(response.snapshots().get(1).getLatestRecordTimeStamp(), equalTo(new Date(1519931700000L))); @@ -341,16 +389,22 @@ public void testGetModelSnapshots() throws IOException { assertThat(response.snapshots().get(1).getModelSizeStats().getTotalOverFieldCount(), equalTo(0L)); assertThat(response.snapshots().get(1).getModelSizeStats().getTotalPartitionFieldCount(), equalTo(2L)); assertThat(response.snapshots().get(1).getModelSizeStats().getBucketAllocationFailuresCount(), equalTo(0L)); - assertThat(response.snapshots().get(1).getModelSizeStats().getMemoryStatus(), - equalTo(ModelSizeStats.MemoryStatus.fromString("ok"))); - assertThat(response.snapshots().get(1).getModelSizeStats().getAssignmentMemoryBasis(), - equalTo(ModelSizeStats.AssignmentMemoryBasis.MODEL_MEMORY_LIMIT)); + assertThat( + response.snapshots().get(1).getModelSizeStats().getMemoryStatus(), + equalTo(ModelSizeStats.MemoryStatus.fromString("ok")) + ); + assertThat( + response.snapshots().get(1).getModelSizeStats().getAssignmentMemoryBasis(), + equalTo(ModelSizeStats.AssignmentMemoryBasis.MODEL_MEMORY_LIMIT) + ); assertThat(response.snapshots().get(2).getJobId(), equalTo(JOB_ID)); assertThat(response.snapshots().get(2).getSnapshotId(), equalTo("1541588919")); assertThat(response.snapshots().get(2).getSnapshotDocCount(), equalTo(1)); - assertThat(response.snapshots().get(2).getDescription(), equalTo("State persisted due to job close at" + - " 2018-11-07T11:08:39+0000")); + assertThat( + response.snapshots().get(2).getDescription(), + equalTo("State persisted due to job close at" + " 2018-11-07T11:08:39+0000") + ); assertThat(response.snapshots().get(2).getSnapshotDocCount(), equalTo(1)); assertThat(response.snapshots().get(2).getTimestamp(), equalTo(new Date(1541588919000L))); assertThat(response.snapshots().get(2).getLatestRecordTimeStamp(), equalTo(new Date(1519931700000L))); @@ -364,15 +418,19 @@ public void testGetModelSnapshots() throws IOException { assertThat(response.snapshots().get(2).getModelSizeStats().getTotalOverFieldCount(), equalTo(0L)); assertThat(response.snapshots().get(2).getModelSizeStats().getTotalPartitionFieldCount(), equalTo(2L)); assertThat(response.snapshots().get(2).getModelSizeStats().getBucketAllocationFailuresCount(), equalTo(0L)); - assertThat(response.snapshots().get(2).getModelSizeStats().getMemoryStatus(), - equalTo(ModelSizeStats.MemoryStatus.fromString("ok"))); + assertThat( + response.snapshots().get(2).getModelSizeStats().getMemoryStatus(), + equalTo(ModelSizeStats.MemoryStatus.fromString("ok")) + ); assertThat(response.snapshots().get(2).getModelSizeStats().getAssignmentMemoryBasis(), nullValue()); assertThat(response.snapshots().get(3).getJobId(), equalTo(JOB_ID)); assertThat(response.snapshots().get(3).getSnapshotId(), equalTo("1541589919")); assertThat(response.snapshots().get(3).getSnapshotDocCount(), equalTo(1)); - assertThat(response.snapshots().get(3).getDescription(), equalTo("State persisted due to job close at" + - " 2018-11-07T11:25:19+0000")); + assertThat( + response.snapshots().get(3).getDescription(), + equalTo("State persisted due to job close at" + " 2018-11-07T11:25:19+0000") + ); assertThat(response.snapshots().get(3).getSnapshotDocCount(), equalTo(1)); assertThat(response.snapshots().get(3).getTimestamp(), equalTo(new Date(1541589919000L))); assertThat(response.snapshots().get(3).getLatestRecordTimeStamp(), equalTo(new Date(1519931700000L))); @@ -386,8 +444,10 @@ public void testGetModelSnapshots() throws IOException { assertThat(response.snapshots().get(3).getModelSizeStats().getTotalOverFieldCount(), equalTo(0L)); assertThat(response.snapshots().get(3).getModelSizeStats().getTotalPartitionFieldCount(), equalTo(2L)); assertThat(response.snapshots().get(3).getModelSizeStats().getBucketAllocationFailuresCount(), equalTo(0L)); - assertThat(response.snapshots().get(3).getModelSizeStats().getMemoryStatus(), - equalTo(ModelSizeStats.MemoryStatus.fromString("ok"))); + assertThat( + response.snapshots().get(3).getModelSizeStats().getMemoryStatus(), + equalTo(ModelSizeStats.MemoryStatus.fromString("ok")) + ); assertThat(response.snapshots().get(3).getModelSizeStats().getAssignmentMemoryBasis(), nullValue()); } { @@ -396,16 +456,21 @@ public void testGetModelSnapshots() throws IOException { request.setDesc(true); request.setPageParams(new PageParams(0, 10000)); - GetModelSnapshotsResponse response = execute(request, machineLearningClient::getModelSnapshots, - machineLearningClient::getModelSnapshotsAsync); + GetModelSnapshotsResponse response = execute( + request, + machineLearningClient::getModelSnapshots, + machineLearningClient::getModelSnapshotsAsync + ); assertThat(response.count(), equalTo(4L)); assertThat(response.snapshots().size(), equalTo(4)); assertThat(response.snapshots().get(3).getJobId(), equalTo(JOB_ID)); assertThat(response.snapshots().get(3).getSnapshotId(), equalTo("1541587919")); assertThat(response.snapshots().get(3).getSnapshotDocCount(), equalTo(1)); - assertThat(response.snapshots().get(3).getDescription(), equalTo("State persisted due to job close at" + - " 2018-11-07T10:51:59+0000")); + assertThat( + response.snapshots().get(3).getDescription(), + equalTo("State persisted due to job close at" + " 2018-11-07T10:51:59+0000") + ); assertThat(response.snapshots().get(3).getSnapshotDocCount(), equalTo(1)); assertThat(response.snapshots().get(3).getTimestamp(), equalTo(new Date(1541587919000L))); assertThat(response.snapshots().get(3).getLatestRecordTimeStamp(), equalTo(new Date(1519931700000L))); @@ -419,15 +484,19 @@ public void testGetModelSnapshots() throws IOException { assertThat(response.snapshots().get(3).getModelSizeStats().getTotalOverFieldCount(), equalTo(0L)); assertThat(response.snapshots().get(3).getModelSizeStats().getTotalPartitionFieldCount(), equalTo(2L)); assertThat(response.snapshots().get(3).getModelSizeStats().getBucketAllocationFailuresCount(), equalTo(0L)); - assertThat(response.snapshots().get(3).getModelSizeStats().getMemoryStatus(), - equalTo(ModelSizeStats.MemoryStatus.fromString("ok"))); + assertThat( + response.snapshots().get(3).getModelSizeStats().getMemoryStatus(), + equalTo(ModelSizeStats.MemoryStatus.fromString("ok")) + ); assertThat(response.snapshots().get(3).getModelSizeStats().getAssignmentMemoryBasis(), nullValue()); assertThat(response.snapshots().get(2).getJobId(), equalTo(JOB_ID)); assertThat(response.snapshots().get(2).getSnapshotId(), equalTo("1541587929")); assertThat(response.snapshots().get(2).getSnapshotDocCount(), equalTo(1)); - assertThat(response.snapshots().get(2).getDescription(), equalTo("State persisted due to job close at" + - " 2018-11-07T10:52:09+0000")); + assertThat( + response.snapshots().get(2).getDescription(), + equalTo("State persisted due to job close at" + " 2018-11-07T10:52:09+0000") + ); assertThat(response.snapshots().get(2).getSnapshotDocCount(), equalTo(1)); assertThat(response.snapshots().get(2).getTimestamp(), equalTo(new Date(1541587929000L))); assertThat(response.snapshots().get(2).getLatestRecordTimeStamp(), equalTo(new Date(1519931700000L))); @@ -441,16 +510,22 @@ public void testGetModelSnapshots() throws IOException { assertThat(response.snapshots().get(2).getModelSizeStats().getTotalOverFieldCount(), equalTo(0L)); assertThat(response.snapshots().get(2).getModelSizeStats().getTotalPartitionFieldCount(), equalTo(2L)); assertThat(response.snapshots().get(2).getModelSizeStats().getBucketAllocationFailuresCount(), equalTo(0L)); - assertThat(response.snapshots().get(2).getModelSizeStats().getMemoryStatus(), - equalTo(ModelSizeStats.MemoryStatus.fromString("ok"))); - assertThat(response.snapshots().get(2).getModelSizeStats().getAssignmentMemoryBasis(), - equalTo(ModelSizeStats.AssignmentMemoryBasis.MODEL_MEMORY_LIMIT)); + assertThat( + response.snapshots().get(2).getModelSizeStats().getMemoryStatus(), + equalTo(ModelSizeStats.MemoryStatus.fromString("ok")) + ); + assertThat( + response.snapshots().get(2).getModelSizeStats().getAssignmentMemoryBasis(), + equalTo(ModelSizeStats.AssignmentMemoryBasis.MODEL_MEMORY_LIMIT) + ); assertThat(response.snapshots().get(1).getJobId(), equalTo(JOB_ID)); assertThat(response.snapshots().get(1).getSnapshotId(), equalTo("1541588919")); assertThat(response.snapshots().get(1).getSnapshotDocCount(), equalTo(1)); - assertThat(response.snapshots().get(1).getDescription(), equalTo("State persisted due to job close at" + - " 2018-11-07T11:08:39+0000")); + assertThat( + response.snapshots().get(1).getDescription(), + equalTo("State persisted due to job close at" + " 2018-11-07T11:08:39+0000") + ); assertThat(response.snapshots().get(1).getSnapshotDocCount(), equalTo(1)); assertThat(response.snapshots().get(1).getTimestamp(), equalTo(new Date(1541588919000L))); assertThat(response.snapshots().get(1).getLatestRecordTimeStamp(), equalTo(new Date(1519931700000L))); @@ -464,15 +539,19 @@ public void testGetModelSnapshots() throws IOException { assertThat(response.snapshots().get(1).getModelSizeStats().getTotalOverFieldCount(), equalTo(0L)); assertThat(response.snapshots().get(1).getModelSizeStats().getTotalPartitionFieldCount(), equalTo(2L)); assertThat(response.snapshots().get(1).getModelSizeStats().getBucketAllocationFailuresCount(), equalTo(0L)); - assertThat(response.snapshots().get(1).getModelSizeStats().getMemoryStatus(), - equalTo(ModelSizeStats.MemoryStatus.fromString("ok"))); + assertThat( + response.snapshots().get(1).getModelSizeStats().getMemoryStatus(), + equalTo(ModelSizeStats.MemoryStatus.fromString("ok")) + ); assertThat(response.snapshots().get(1).getModelSizeStats().getAssignmentMemoryBasis(), nullValue()); assertThat(response.snapshots().get(0).getJobId(), equalTo(JOB_ID)); assertThat(response.snapshots().get(0).getSnapshotId(), equalTo("1541589919")); assertThat(response.snapshots().get(0).getSnapshotDocCount(), equalTo(1)); - assertThat(response.snapshots().get(0).getDescription(), equalTo("State persisted due to job close at" + - " 2018-11-07T11:25:19+0000")); + assertThat( + response.snapshots().get(0).getDescription(), + equalTo("State persisted due to job close at" + " 2018-11-07T11:25:19+0000") + ); assertThat(response.snapshots().get(0).getSnapshotDocCount(), equalTo(1)); assertThat(response.snapshots().get(0).getTimestamp(), equalTo(new Date(1541589919000L))); assertThat(response.snapshots().get(0).getLatestRecordTimeStamp(), equalTo(new Date(1519931700000L))); @@ -486,8 +565,10 @@ public void testGetModelSnapshots() throws IOException { assertThat(response.snapshots().get(0).getModelSizeStats().getTotalOverFieldCount(), equalTo(0L)); assertThat(response.snapshots().get(0).getModelSizeStats().getTotalPartitionFieldCount(), equalTo(2L)); assertThat(response.snapshots().get(0).getModelSizeStats().getBucketAllocationFailuresCount(), equalTo(0L)); - assertThat(response.snapshots().get(0).getModelSizeStats().getMemoryStatus(), - equalTo(ModelSizeStats.MemoryStatus.fromString("ok"))); + assertThat( + response.snapshots().get(0).getModelSizeStats().getMemoryStatus(), + equalTo(ModelSizeStats.MemoryStatus.fromString("ok")) + ); assertThat(response.snapshots().get(0).getModelSizeStats().getAssignmentMemoryBasis(), nullValue()); } { @@ -496,16 +577,21 @@ public void testGetModelSnapshots() throws IOException { request.setDesc(false); request.setPageParams(new PageParams(0, 1)); - GetModelSnapshotsResponse response = execute(request, machineLearningClient::getModelSnapshots, - machineLearningClient::getModelSnapshotsAsync); + GetModelSnapshotsResponse response = execute( + request, + machineLearningClient::getModelSnapshots, + machineLearningClient::getModelSnapshotsAsync + ); assertThat(response.count(), equalTo(4L)); assertThat(response.snapshots().size(), equalTo(1)); assertThat(response.snapshots().get(0).getJobId(), equalTo(JOB_ID)); assertThat(response.snapshots().get(0).getSnapshotId(), equalTo("1541587919")); assertThat(response.snapshots().get(0).getSnapshotDocCount(), equalTo(1)); - assertThat(response.snapshots().get(0).getDescription(), equalTo("State persisted due to job close at" + - " 2018-11-07T10:51:59+0000")); + assertThat( + response.snapshots().get(0).getDescription(), + equalTo("State persisted due to job close at" + " 2018-11-07T10:51:59+0000") + ); assertThat(response.snapshots().get(0).getSnapshotDocCount(), equalTo(1)); assertThat(response.snapshots().get(0).getTimestamp(), equalTo(new Date(1541587919000L))); assertThat(response.snapshots().get(0).getLatestRecordTimeStamp(), equalTo(new Date(1519931700000L))); @@ -519,8 +605,10 @@ public void testGetModelSnapshots() throws IOException { assertThat(response.snapshots().get(0).getModelSizeStats().getTotalOverFieldCount(), equalTo(0L)); assertThat(response.snapshots().get(0).getModelSizeStats().getTotalPartitionFieldCount(), equalTo(2L)); assertThat(response.snapshots().get(0).getModelSizeStats().getBucketAllocationFailuresCount(), equalTo(0L)); - assertThat(response.snapshots().get(0).getModelSizeStats().getMemoryStatus(), - equalTo(ModelSizeStats.MemoryStatus.fromString("ok"))); + assertThat( + response.snapshots().get(0).getModelSizeStats().getMemoryStatus(), + equalTo(ModelSizeStats.MemoryStatus.fromString("ok")) + ); assertThat(response.snapshots().get(0).getModelSizeStats().getAssignmentMemoryBasis(), nullValue()); } { @@ -529,8 +617,11 @@ public void testGetModelSnapshots() throws IOException { request.setDesc(false); request.setPageParams(new PageParams(2, 3)); - GetModelSnapshotsResponse response = execute(request, machineLearningClient::getModelSnapshots, - machineLearningClient::getModelSnapshotsAsync); + GetModelSnapshotsResponse response = execute( + request, + machineLearningClient::getModelSnapshots, + machineLearningClient::getModelSnapshotsAsync + ); assertThat(response.count(), equalTo(4L)); assertThat(response.snapshots().size(), equalTo(2)); @@ -538,8 +629,10 @@ public void testGetModelSnapshots() throws IOException { assertThat(response.snapshots().get(0).getJobId(), equalTo(JOB_ID)); assertThat(response.snapshots().get(0).getSnapshotId(), equalTo("1541588919")); assertThat(response.snapshots().get(0).getSnapshotDocCount(), equalTo(1)); - assertThat(response.snapshots().get(0).getDescription(), equalTo("State persisted due to job close at" + - " 2018-11-07T11:08:39+0000")); + assertThat( + response.snapshots().get(0).getDescription(), + equalTo("State persisted due to job close at" + " 2018-11-07T11:08:39+0000") + ); assertThat(response.snapshots().get(0).getSnapshotDocCount(), equalTo(1)); assertThat(response.snapshots().get(0).getTimestamp(), equalTo(new Date(1541588919000L))); assertThat(response.snapshots().get(0).getLatestRecordTimeStamp(), equalTo(new Date(1519931700000L))); @@ -553,15 +646,19 @@ public void testGetModelSnapshots() throws IOException { assertThat(response.snapshots().get(0).getModelSizeStats().getTotalOverFieldCount(), equalTo(0L)); assertThat(response.snapshots().get(0).getModelSizeStats().getTotalPartitionFieldCount(), equalTo(2L)); assertThat(response.snapshots().get(0).getModelSizeStats().getBucketAllocationFailuresCount(), equalTo(0L)); - assertThat(response.snapshots().get(0).getModelSizeStats().getMemoryStatus(), - equalTo(ModelSizeStats.MemoryStatus.fromString("ok"))); + assertThat( + response.snapshots().get(0).getModelSizeStats().getMemoryStatus(), + equalTo(ModelSizeStats.MemoryStatus.fromString("ok")) + ); assertThat(response.snapshots().get(0).getModelSizeStats().getAssignmentMemoryBasis(), nullValue()); assertThat(response.snapshots().get(1).getJobId(), equalTo(JOB_ID)); assertThat(response.snapshots().get(1).getSnapshotId(), equalTo("1541589919")); assertThat(response.snapshots().get(1).getSnapshotDocCount(), equalTo(1)); - assertThat(response.snapshots().get(1).getDescription(), equalTo("State persisted due to job close at" + - " 2018-11-07T11:25:19+0000")); + assertThat( + response.snapshots().get(1).getDescription(), + equalTo("State persisted due to job close at" + " 2018-11-07T11:25:19+0000") + ); assertThat(response.snapshots().get(1).getSnapshotDocCount(), equalTo(1)); assertThat(response.snapshots().get(1).getTimestamp(), equalTo(new Date(1541589919000L))); assertThat(response.snapshots().get(1).getLatestRecordTimeStamp(), equalTo(new Date(1519931700000L))); @@ -575,16 +672,21 @@ public void testGetModelSnapshots() throws IOException { assertThat(response.snapshots().get(1).getModelSizeStats().getTotalOverFieldCount(), equalTo(0L)); assertThat(response.snapshots().get(1).getModelSizeStats().getTotalPartitionFieldCount(), equalTo(2L)); assertThat(response.snapshots().get(1).getModelSizeStats().getBucketAllocationFailuresCount(), equalTo(0L)); - assertThat(response.snapshots().get(1).getModelSizeStats().getMemoryStatus(), - equalTo(ModelSizeStats.MemoryStatus.fromString("ok"))); + assertThat( + response.snapshots().get(1).getModelSizeStats().getMemoryStatus(), + equalTo(ModelSizeStats.MemoryStatus.fromString("ok")) + ); assertThat(response.snapshots().get(1).getModelSizeStats().getAssignmentMemoryBasis(), nullValue()); } { GetModelSnapshotsRequest request = new GetModelSnapshotsRequest(JOB_ID); request.setSnapshotId("1541588919"); - GetModelSnapshotsResponse response = execute(request, machineLearningClient::getModelSnapshots, - machineLearningClient::getModelSnapshotsAsync); + GetModelSnapshotsResponse response = execute( + request, + machineLearningClient::getModelSnapshots, + machineLearningClient::getModelSnapshotsAsync + ); assertThat(response.count(), equalTo(1L)); assertThat(response.snapshots().size(), equalTo(1)); @@ -592,8 +694,10 @@ public void testGetModelSnapshots() throws IOException { assertThat(response.snapshots().get(0).getJobId(), equalTo(JOB_ID)); assertThat(response.snapshots().get(0).getSnapshotId(), equalTo("1541588919")); assertThat(response.snapshots().get(0).getSnapshotDocCount(), equalTo(1)); - assertThat(response.snapshots().get(0).getDescription(), equalTo("State persisted due to job close at" + - " 2018-11-07T11:08:39+0000")); + assertThat( + response.snapshots().get(0).getDescription(), + equalTo("State persisted due to job close at" + " 2018-11-07T11:08:39+0000") + ); assertThat(response.snapshots().get(0).getSnapshotDocCount(), equalTo(1)); assertThat(response.snapshots().get(0).getTimestamp(), equalTo(new Date(1541588919000L))); assertThat(response.snapshots().get(0).getLatestRecordTimeStamp(), equalTo(new Date(1519931700000L))); @@ -607,16 +711,21 @@ public void testGetModelSnapshots() throws IOException { assertThat(response.snapshots().get(0).getModelSizeStats().getTotalOverFieldCount(), equalTo(0L)); assertThat(response.snapshots().get(0).getModelSizeStats().getTotalPartitionFieldCount(), equalTo(2L)); assertThat(response.snapshots().get(0).getModelSizeStats().getBucketAllocationFailuresCount(), equalTo(0L)); - assertThat(response.snapshots().get(0).getModelSizeStats().getMemoryStatus(), - equalTo(ModelSizeStats.MemoryStatus.fromString("ok"))); + assertThat( + response.snapshots().get(0).getModelSizeStats().getMemoryStatus(), + equalTo(ModelSizeStats.MemoryStatus.fromString("ok")) + ); assertThat(response.snapshots().get(0).getModelSizeStats().getAssignmentMemoryBasis(), nullValue()); } { GetModelSnapshotsRequest request = new GetModelSnapshotsRequest(JOB_ID); request.setSnapshotId("1541586919"); // request a non-existent snapshotId - GetModelSnapshotsResponse response = execute(request, machineLearningClient::getModelSnapshots, - machineLearningClient::getModelSnapshotsAsync); + GetModelSnapshotsResponse response = execute( + request, + machineLearningClient::getModelSnapshots, + machineLearningClient::getModelSnapshotsAsync + ); assertThat(response.count(), equalTo(0L)); assertThat(response.snapshots().size(), equalTo(0)); @@ -628,16 +737,21 @@ public void testGetModelSnapshots() throws IOException { request.setStart("1541586919000"); request.setEnd("1541589019000"); - GetModelSnapshotsResponse response = execute(request, machineLearningClient::getModelSnapshots, - machineLearningClient::getModelSnapshotsAsync); + GetModelSnapshotsResponse response = execute( + request, + machineLearningClient::getModelSnapshots, + machineLearningClient::getModelSnapshotsAsync + ); assertThat(response.count(), equalTo(3L)); assertThat(response.snapshots().size(), equalTo(3)); assertThat(response.snapshots().get(0).getJobId(), equalTo(JOB_ID)); assertThat(response.snapshots().get(0).getSnapshotId(), equalTo("1541587919")); assertThat(response.snapshots().get(0).getSnapshotDocCount(), equalTo(1)); - assertThat(response.snapshots().get(0).getDescription(), equalTo("State persisted due to job close at" + - " 2018-11-07T10:51:59+0000")); + assertThat( + response.snapshots().get(0).getDescription(), + equalTo("State persisted due to job close at" + " 2018-11-07T10:51:59+0000") + ); assertThat(response.snapshots().get(0).getSnapshotDocCount(), equalTo(1)); assertThat(response.snapshots().get(0).getTimestamp(), equalTo(new Date(1541587919000L))); assertThat(response.snapshots().get(0).getLatestRecordTimeStamp(), equalTo(new Date(1519931700000L))); @@ -651,15 +765,19 @@ public void testGetModelSnapshots() throws IOException { assertThat(response.snapshots().get(0).getModelSizeStats().getTotalOverFieldCount(), equalTo(0L)); assertThat(response.snapshots().get(0).getModelSizeStats().getTotalPartitionFieldCount(), equalTo(2L)); assertThat(response.snapshots().get(0).getModelSizeStats().getBucketAllocationFailuresCount(), equalTo(0L)); - assertThat(response.snapshots().get(0).getModelSizeStats().getMemoryStatus(), - equalTo(ModelSizeStats.MemoryStatus.fromString("ok"))); + assertThat( + response.snapshots().get(0).getModelSizeStats().getMemoryStatus(), + equalTo(ModelSizeStats.MemoryStatus.fromString("ok")) + ); assertThat(response.snapshots().get(0).getModelSizeStats().getAssignmentMemoryBasis(), nullValue()); assertThat(response.snapshots().get(1).getJobId(), equalTo(JOB_ID)); assertThat(response.snapshots().get(1).getSnapshotId(), equalTo("1541587929")); assertThat(response.snapshots().get(1).getSnapshotDocCount(), equalTo(1)); - assertThat(response.snapshots().get(1).getDescription(), equalTo("State persisted due to job close at" + - " 2018-11-07T10:52:09+0000")); + assertThat( + response.snapshots().get(1).getDescription(), + equalTo("State persisted due to job close at" + " 2018-11-07T10:52:09+0000") + ); assertThat(response.snapshots().get(1).getSnapshotDocCount(), equalTo(1)); assertThat(response.snapshots().get(1).getTimestamp(), equalTo(new Date(1541587929000L))); assertThat(response.snapshots().get(1).getLatestRecordTimeStamp(), equalTo(new Date(1519931700000L))); @@ -673,16 +791,22 @@ public void testGetModelSnapshots() throws IOException { assertThat(response.snapshots().get(1).getModelSizeStats().getTotalOverFieldCount(), equalTo(0L)); assertThat(response.snapshots().get(1).getModelSizeStats().getTotalPartitionFieldCount(), equalTo(2L)); assertThat(response.snapshots().get(1).getModelSizeStats().getBucketAllocationFailuresCount(), equalTo(0L)); - assertThat(response.snapshots().get(1).getModelSizeStats().getMemoryStatus(), - equalTo(ModelSizeStats.MemoryStatus.fromString("ok"))); - assertThat(response.snapshots().get(1).getModelSizeStats().getAssignmentMemoryBasis(), - equalTo(ModelSizeStats.AssignmentMemoryBasis.MODEL_MEMORY_LIMIT)); + assertThat( + response.snapshots().get(1).getModelSizeStats().getMemoryStatus(), + equalTo(ModelSizeStats.MemoryStatus.fromString("ok")) + ); + assertThat( + response.snapshots().get(1).getModelSizeStats().getAssignmentMemoryBasis(), + equalTo(ModelSizeStats.AssignmentMemoryBasis.MODEL_MEMORY_LIMIT) + ); assertThat(response.snapshots().get(2).getJobId(), equalTo(JOB_ID)); assertThat(response.snapshots().get(2).getSnapshotId(), equalTo("1541588919")); assertThat(response.snapshots().get(2).getSnapshotDocCount(), equalTo(1)); - assertThat(response.snapshots().get(2).getDescription(), equalTo("State persisted due to job close at" + - " 2018-11-07T11:08:39+0000")); + assertThat( + response.snapshots().get(2).getDescription(), + equalTo("State persisted due to job close at" + " 2018-11-07T11:08:39+0000") + ); assertThat(response.snapshots().get(2).getSnapshotDocCount(), equalTo(1)); assertThat(response.snapshots().get(2).getTimestamp(), equalTo(new Date(1541588919000L))); assertThat(response.snapshots().get(2).getLatestRecordTimeStamp(), equalTo(new Date(1519931700000L))); @@ -696,8 +820,10 @@ public void testGetModelSnapshots() throws IOException { assertThat(response.snapshots().get(2).getModelSizeStats().getTotalOverFieldCount(), equalTo(0L)); assertThat(response.snapshots().get(2).getModelSizeStats().getTotalPartitionFieldCount(), equalTo(2L)); assertThat(response.snapshots().get(2).getModelSizeStats().getBucketAllocationFailuresCount(), equalTo(0L)); - assertThat(response.snapshots().get(2).getModelSizeStats().getMemoryStatus(), - equalTo(ModelSizeStats.MemoryStatus.fromString("ok"))); + assertThat( + response.snapshots().get(2).getModelSizeStats().getMemoryStatus(), + equalTo(ModelSizeStats.MemoryStatus.fromString("ok")) + ); assertThat(response.snapshots().get(2).getModelSizeStats().getAssignmentMemoryBasis(), nullValue()); } { @@ -706,16 +832,21 @@ public void testGetModelSnapshots() throws IOException { request.setDesc(false); request.setStart("1541589019000"); - GetModelSnapshotsResponse response = execute(request, machineLearningClient::getModelSnapshots, - machineLearningClient::getModelSnapshotsAsync); + GetModelSnapshotsResponse response = execute( + request, + machineLearningClient::getModelSnapshots, + machineLearningClient::getModelSnapshotsAsync + ); assertThat(response.count(), equalTo(1L)); assertThat(response.snapshots().size(), equalTo(1)); assertThat(response.snapshots().get(0).getJobId(), equalTo(JOB_ID)); assertThat(response.snapshots().get(0).getSnapshotId(), equalTo("1541589919")); assertThat(response.snapshots().get(0).getSnapshotDocCount(), equalTo(1)); - assertThat(response.snapshots().get(0).getDescription(), equalTo("State persisted due to job close at" + - " 2018-11-07T11:25:19+0000")); + assertThat( + response.snapshots().get(0).getDescription(), + equalTo("State persisted due to job close at" + " 2018-11-07T11:25:19+0000") + ); assertThat(response.snapshots().get(0).getSnapshotDocCount(), equalTo(1)); assertThat(response.snapshots().get(0).getTimestamp(), equalTo(new Date(1541589919000L))); assertThat(response.snapshots().get(0).getLatestRecordTimeStamp(), equalTo(new Date(1519931700000L))); @@ -729,8 +860,10 @@ public void testGetModelSnapshots() throws IOException { assertThat(response.snapshots().get(0).getModelSizeStats().getTotalOverFieldCount(), equalTo(0L)); assertThat(response.snapshots().get(0).getModelSizeStats().getTotalPartitionFieldCount(), equalTo(2L)); assertThat(response.snapshots().get(0).getModelSizeStats().getBucketAllocationFailuresCount(), equalTo(0L)); - assertThat(response.snapshots().get(0).getModelSizeStats().getMemoryStatus(), - equalTo(ModelSizeStats.MemoryStatus.fromString("ok"))); + assertThat( + response.snapshots().get(0).getModelSizeStats().getMemoryStatus(), + equalTo(ModelSizeStats.MemoryStatus.fromString("ok")) + ); assertThat(response.snapshots().get(0).getModelSizeStats().getAssignmentMemoryBasis(), nullValue()); } } @@ -751,8 +884,11 @@ public void testGetCategories() throws IOException { GetCategoriesRequest request = new GetCategoriesRequest(JOB_ID); request.setPageParams(new PageParams(0, 10000)); - GetCategoriesResponse response = execute(request, machineLearningClient::getCategories, - machineLearningClient::getCategoriesAsync); + GetCategoriesResponse response = execute( + request, + machineLearningClient::getCategories, + machineLearningClient::getCategoriesAsync + ); assertThat(response.count(), equalTo(3L)); assertThat(response.categories().size(), equalTo(3)); @@ -775,8 +911,11 @@ public void testGetCategories() throws IOException { GetCategoriesRequest request = new GetCategoriesRequest(JOB_ID); request.setPageParams(new PageParams(0, 1)); - GetCategoriesResponse response = execute(request, machineLearningClient::getCategories, - machineLearningClient::getCategoriesAsync); + GetCategoriesResponse response = execute( + request, + machineLearningClient::getCategories, + machineLearningClient::getCategoriesAsync + ); assertThat(response.count(), equalTo(3L)); assertThat(response.categories().size(), equalTo(1)); @@ -789,8 +928,11 @@ public void testGetCategories() throws IOException { GetCategoriesRequest request = new GetCategoriesRequest(JOB_ID); request.setPageParams(new PageParams(1, 2)); - GetCategoriesResponse response = execute(request, machineLearningClient::getCategories, - machineLearningClient::getCategoriesAsync); + GetCategoriesResponse response = execute( + request, + machineLearningClient::getCategories, + machineLearningClient::getCategoriesAsync + ); assertThat(response.count(), equalTo(3L)); assertThat(response.categories().size(), equalTo(2)); @@ -808,8 +950,11 @@ public void testGetCategories() throws IOException { GetCategoriesRequest request = new GetCategoriesRequest(JOB_ID); request.setCategoryId(0L); // request a non-existent category - GetCategoriesResponse response = execute(request, machineLearningClient::getCategories, - machineLearningClient::getCategoriesAsync); + GetCategoriesResponse response = execute( + request, + machineLearningClient::getCategories, + machineLearningClient::getCategoriesAsync + ); assertThat(response.count(), equalTo(0L)); assertThat(response.categories().size(), equalTo(0)); @@ -818,8 +963,11 @@ public void testGetCategories() throws IOException { GetCategoriesRequest request = new GetCategoriesRequest(JOB_ID); request.setCategoryId(1L); - GetCategoriesResponse response = execute(request, machineLearningClient::getCategories, - machineLearningClient::getCategoriesAsync); + GetCategoriesResponse response = execute( + request, + machineLearningClient::getCategories, + machineLearningClient::getCategoriesAsync + ); assertThat(response.count(), equalTo(1L)); assertThat(response.categories().size(), equalTo(1)); @@ -832,8 +980,11 @@ public void testGetCategories() throws IOException { GetCategoriesRequest request = new GetCategoriesRequest(JOB_ID); request.setCategoryId(2L); - GetCategoriesResponse response = execute(request, machineLearningClient::getCategories, - machineLearningClient::getCategoriesAsync); + GetCategoriesResponse response = execute( + request, + machineLearningClient::getCategories, + machineLearningClient::getCategoriesAsync + ); assertThat(response.count(), equalTo(1L)); assertThat(response.categories().get(0).getCategoryId(), equalTo(2L)); @@ -846,8 +997,11 @@ public void testGetCategories() throws IOException { GetCategoriesRequest request = new GetCategoriesRequest(JOB_ID); request.setCategoryId(3L); - GetCategoriesResponse response = execute(request, machineLearningClient::getCategories, - machineLearningClient::getCategoriesAsync); + GetCategoriesResponse response = execute( + request, + machineLearningClient::getCategories, + machineLearningClient::getCategoriesAsync + ); assertThat(response.count(), equalTo(1L)); assertThat(response.categories().get(0).getCategoryId(), equalTo(3L)); @@ -907,7 +1061,7 @@ public void testGetBuckets() throws IOException { assertThat(response.count(), equalTo(3L)); assertThat(response.buckets().get(0).getTimestamp().getTime(), equalTo(START_TIME_EPOCH_MS)); assertThat(response.buckets().get(1).getTimestamp().getTime(), equalTo(START_TIME_EPOCH_MS + 3600000L)); - assertThat(response.buckets().get(2).getTimestamp().getTime(), equalTo(START_TIME_EPOCH_MS + 2 * + 3600000L)); + assertThat(response.buckets().get(2).getTimestamp().getTime(), equalTo(START_TIME_EPOCH_MS + 2 * +3600000L)); } { GetBucketsRequest request = new GetBucketsRequest(JOB_ID); @@ -961,9 +1115,19 @@ public void testGetOverallBuckets() throws IOException { bulkRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); for (Bucket bucket : firstBuckets) { IndexRequest indexRequest = new IndexRequest(RESULTS_INDEX); - indexRequest.source("{\"job_id\":\"" + anotherJobId + "\", \"result_type\":\"bucket\", \"timestamp\": " + - bucket.getTimestamp().getTime() + "," + "\"bucket_span\": 3600,\"is_interim\": " + bucket.isInterim() - + ", \"anomaly_score\": " + String.valueOf(bucket.getAnomalyScore() + 10.0) + "}", XContentType.JSON); + indexRequest.source( + "{\"job_id\":\"" + + anotherJobId + + "\", \"result_type\":\"bucket\", \"timestamp\": " + + bucket.getTimestamp().getTime() + + "," + + "\"bucket_span\": 3600,\"is_interim\": " + + bucket.isInterim() + + ", \"anomaly_score\": " + + String.valueOf(bucket.getAnomalyScore() + 10.0) + + "}", + XContentType.JSON + ); bulkRequest.add(indexRequest); } highLevelClient().bulk(bulkRequest, RequestOptions.DEFAULT); @@ -971,8 +1135,11 @@ public void testGetOverallBuckets() throws IOException { { GetOverallBucketsRequest request = new GetOverallBucketsRequest(JOB_ID, anotherJobId); - GetOverallBucketsResponse response = execute(request, machineLearningClient::getOverallBuckets, - machineLearningClient::getOverallBucketsAsync); + GetOverallBucketsResponse response = execute( + request, + machineLearningClient::getOverallBuckets, + machineLearningClient::getOverallBucketsAsync + ); assertThat(response.count(), equalTo(241L)); List overallBuckets = response.overallBuckets(); @@ -985,8 +1152,11 @@ public void testGetOverallBuckets() throws IOException { GetOverallBucketsRequest request = new GetOverallBucketsRequest(JOB_ID, anotherJobId); request.setBucketSpan(TimeValue.timeValueHours(2)); - GetOverallBucketsResponse response = execute(request, machineLearningClient::getOverallBuckets, - machineLearningClient::getOverallBucketsAsync); + GetOverallBucketsResponse response = execute( + request, + machineLearningClient::getOverallBuckets, + machineLearningClient::getOverallBucketsAsync + ); assertThat(response.count(), equalTo(121L)); } @@ -995,8 +1165,11 @@ public void testGetOverallBuckets() throws IOException { GetOverallBucketsRequest request = new GetOverallBucketsRequest(JOB_ID, anotherJobId); request.setEnd(String.valueOf(end)); - GetOverallBucketsResponse response = execute(request, machineLearningClient::getOverallBuckets, - machineLearningClient::getOverallBucketsAsync); + GetOverallBucketsResponse response = execute( + request, + machineLearningClient::getOverallBuckets, + machineLearningClient::getOverallBucketsAsync + ); assertThat(response.count(), equalTo(10L)); assertThat(response.overallBuckets().get(0).getTimestamp().getTime(), equalTo(START_TIME_EPOCH_MS)); @@ -1006,8 +1179,11 @@ public void testGetOverallBuckets() throws IOException { GetOverallBucketsRequest request = new GetOverallBucketsRequest(JOB_ID, anotherJobId); request.setExcludeInterim(true); - GetOverallBucketsResponse response = execute(request, machineLearningClient::getOverallBuckets, - machineLearningClient::getOverallBucketsAsync); + GetOverallBucketsResponse response = execute( + request, + machineLearningClient::getOverallBuckets, + machineLearningClient::getOverallBucketsAsync + ); assertThat(response.count(), equalTo(240L)); assertThat(response.overallBuckets().stream().allMatch(b -> b.isInterim() == false), is(true)); @@ -1016,8 +1192,11 @@ public void testGetOverallBuckets() throws IOException { GetOverallBucketsRequest request = new GetOverallBucketsRequest(JOB_ID); request.setOverallScore(75.0); - GetOverallBucketsResponse response = execute(request, machineLearningClient::getOverallBuckets, - machineLearningClient::getOverallBucketsAsync); + GetOverallBucketsResponse response = execute( + request, + machineLearningClient::getOverallBuckets, + machineLearningClient::getOverallBucketsAsync + ); assertThat(response.count(), equalTo(bucketStats.criticalCount)); assertThat(response.overallBuckets().stream().allMatch(b -> b.getOverallScore() >= 75.0), is(true)); @@ -1027,8 +1206,11 @@ public void testGetOverallBuckets() throws IOException { GetOverallBucketsRequest request = new GetOverallBucketsRequest(JOB_ID, anotherJobId); request.setStart(String.valueOf(start)); - GetOverallBucketsResponse response = execute(request, machineLearningClient::getOverallBuckets, - machineLearningClient::getOverallBucketsAsync); + GetOverallBucketsResponse response = execute( + request, + machineLearningClient::getOverallBuckets, + machineLearningClient::getOverallBucketsAsync + ); assertThat(response.count(), equalTo(231L)); assertThat(response.overallBuckets().get(0).getTimestamp().getTime(), equalTo(start)); @@ -1038,8 +1220,11 @@ public void testGetOverallBuckets() throws IOException { request.setEnd(String.valueOf(START_TIME_EPOCH_MS + 3 * 3600000L)); request.setTopN(2); - GetOverallBucketsResponse response = execute(request, machineLearningClient::getOverallBuckets, - machineLearningClient::getOverallBucketsAsync); + GetOverallBucketsResponse response = execute( + request, + machineLearningClient::getOverallBuckets, + machineLearningClient::getOverallBucketsAsync + ); assertThat(response.count(), equalTo(3L)); List overallBuckets = response.overallBuckets(); @@ -1132,10 +1317,22 @@ public void testGetInfluencers() throws IOException { double score = isLast ? 90.0 : 42.0; IndexRequest indexRequest = new IndexRequest(RESULTS_INDEX); - indexRequest.source("{\"job_id\":\"" + JOB_ID + "\", \"result_type\":\"influencer\", \"timestamp\": " + - timestamp + "," + "\"bucket_span\": 3600,\"is_interim\": " + isInterim + ", \"influencer_score\": " + score + ", " + - "\"influencer_field_name\":\"my_influencer\", \"influencer_field_value\": \"inf_1\", \"probability\":" - + randomDouble() + "}", XContentType.JSON); + indexRequest.source( + "{\"job_id\":\"" + + JOB_ID + + "\", \"result_type\":\"influencer\", \"timestamp\": " + + timestamp + + "," + + "\"bucket_span\": 3600,\"is_interim\": " + + isInterim + + ", \"influencer_score\": " + + score + + ", " + + "\"influencer_field_name\":\"my_influencer\", \"influencer_field_value\": \"inf_1\", \"probability\":" + + randomDouble() + + "}", + XContentType.JSON + ); bulkRequest.add(indexRequest); timestamp += 3600000L; } @@ -1145,8 +1342,11 @@ public void testGetInfluencers() throws IOException { GetInfluencersRequest request = new GetInfluencersRequest(JOB_ID); request.setDescending(false); - GetInfluencersResponse response = execute(request, machineLearningClient::getInfluencers, - machineLearningClient::getInfluencersAsync); + GetInfluencersResponse response = execute( + request, + machineLearningClient::getInfluencers, + machineLearningClient::getInfluencersAsync + ); assertThat(response.count(), equalTo(5L)); } @@ -1157,8 +1357,11 @@ public void testGetInfluencers() throws IOException { request.setStart(String.valueOf(requestStart)); request.setEnd(String.valueOf(requestEnd)); - GetInfluencersResponse response = execute(request, machineLearningClient::getInfluencers, - machineLearningClient::getInfluencersAsync); + GetInfluencersResponse response = execute( + request, + machineLearningClient::getInfluencers, + machineLearningClient::getInfluencersAsync + ); assertThat(response.count(), equalTo(3L)); for (Influencer influencer : response.influencers()) { @@ -1172,8 +1375,11 @@ public void testGetInfluencers() throws IOException { request.setDescending(false); request.setPageParams(new PageParams(1, 2)); - GetInfluencersResponse response = execute(request, machineLearningClient::getInfluencers, - machineLearningClient::getInfluencersAsync); + GetInfluencersResponse response = execute( + request, + machineLearningClient::getInfluencers, + machineLearningClient::getInfluencersAsync + ); assertThat(response.influencers().size(), equalTo(2)); assertThat(response.influencers().get(0).getTimestamp().getTime(), equalTo(START_TIME_EPOCH_MS + 3600000L)); @@ -1183,8 +1389,11 @@ public void testGetInfluencers() throws IOException { GetInfluencersRequest request = new GetInfluencersRequest(JOB_ID); request.setExcludeInterim(true); - GetInfluencersResponse response = execute(request, machineLearningClient::getInfluencers, - machineLearningClient::getInfluencersAsync); + GetInfluencersResponse response = execute( + request, + machineLearningClient::getInfluencers, + machineLearningClient::getInfluencersAsync + ); assertThat(response.count(), equalTo(4L)); assertThat(response.influencers().stream().anyMatch(Influencer::isInterim), is(false)); @@ -1193,8 +1402,11 @@ public void testGetInfluencers() throws IOException { GetInfluencersRequest request = new GetInfluencersRequest(JOB_ID); request.setInfluencerScore(75.0); - GetInfluencersResponse response = execute(request, machineLearningClient::getInfluencers, - machineLearningClient::getInfluencersAsync); + GetInfluencersResponse response = execute( + request, + machineLearningClient::getInfluencers, + machineLearningClient::getInfluencersAsync + ); assertThat(response.count(), equalTo(1L)); assertThat(response.influencers().get(0).getInfluencerScore(), greaterThanOrEqualTo(75.0)); @@ -1204,8 +1416,11 @@ public void testGetInfluencers() throws IOException { request.setSort("probability"); request.setDescending(true); - GetInfluencersResponse response = execute(request, machineLearningClient::getInfluencers, - machineLearningClient::getInfluencersAsync); + GetInfluencersResponse response = execute( + request, + machineLearningClient::getInfluencers, + machineLearningClient::getInfluencersAsync + ); assertThat(response.influencers().size(), equalTo(5)); double previousProb = 1.0; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/MachineLearningIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/MachineLearningIT.java index d25cfa94759f7..c28e0cdf08e72 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/MachineLearningIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/MachineLearningIT.java @@ -8,6 +8,7 @@ package org.elasticsearch.client; import com.carrotsearch.randomizedtesting.generators.CodepointSetGenerator; + import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.Version; @@ -176,15 +177,15 @@ import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.query.MatchAllQueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.search.SearchHit; import org.elasticsearch.tasks.TaskId; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentType; import org.junit.After; import java.io.IOException; @@ -222,8 +223,13 @@ public class MachineLearningIT extends ESRestHighLevelClientTestCase { private static final RequestOptions POST_DATA_OPTIONS = RequestOptions.DEFAULT.toBuilder() - .setWarningsHandler(warnings -> Collections.singletonList("Posting data directly to anomaly detection jobs is deprecated, " + - "in a future major version it will be compulsory to use a datafeed").equals(warnings) == false).build(); + .setWarningsHandler( + warnings -> Collections.singletonList( + "Posting data directly to anomaly detection jobs is deprecated, " + + "in a future major version it will be compulsory to use a datafeed" + ).equals(warnings) == false + ) + .build(); @After public void cleanUp() throws IOException { @@ -284,9 +290,11 @@ public void testDeleteJob_GivenWaitForCompletionIsTrue() throws Exception { MachineLearningClient machineLearningClient = highLevelClient().machineLearning(); machineLearningClient.putJob(new PutJobRequest(job), RequestOptions.DEFAULT); - DeleteJobResponse response = execute(new DeleteJobRequest(jobId), + DeleteJobResponse response = execute( + new DeleteJobRequest(jobId), machineLearningClient::deleteJob, - machineLearningClient::deleteJobAsync); + machineLearningClient::deleteJobAsync + ); assertTrue(response.getAcknowledged()); assertNull(response.getTask()); @@ -337,9 +345,11 @@ public void testCloseJob() throws Exception { machineLearningClient.putJob(new PutJobRequest(job), RequestOptions.DEFAULT); machineLearningClient.openJob(new OpenJobRequest(jobId), RequestOptions.DEFAULT); - CloseJobResponse response = execute(new CloseJobRequest(jobId), + CloseJobResponse response = execute( + new CloseJobRequest(jobId), machineLearningClient::closeJob, - machineLearningClient::closeJobAsync); + machineLearningClient::closeJobAsync + ); assertTrue(response.isClosed()); } @@ -350,9 +360,11 @@ public void testFlushJob() throws Exception { machineLearningClient.putJob(new PutJobRequest(job), RequestOptions.DEFAULT); machineLearningClient.openJob(new OpenJobRequest(jobId), RequestOptions.DEFAULT); - FlushJobResponse response = execute(new FlushJobRequest(jobId), + FlushJobResponse response = execute( + new FlushJobRequest(jobId), machineLearningClient::flushJob, - machineLearningClient::flushJobAsync); + machineLearningClient::flushJobAsync + ); assertTrue(response.isFlushed()); } @@ -409,8 +421,10 @@ public void testGetJobStats() throws Exception { // Test when allow_no_match is false final GetJobStatsRequest erroredRequest = new GetJobStatsRequest("jobs-that-do-not-exist*"); erroredRequest.setAllowNoMatch(false); - ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, - () -> execute(erroredRequest, machineLearningClient::getJobStats, machineLearningClient::getJobStatsAsync)); + ElasticsearchStatusException exception = expectThrows( + ElasticsearchStatusException.class, + () -> execute(erroredRequest, machineLearningClient::getJobStats, machineLearningClient::getJobStatsAsync) + ); assertThat(exception.status().getStatus(), equalTo(404)); } @@ -422,10 +436,10 @@ public void testForecastJob() throws Exception { machineLearningClient.openJob(new OpenJobRequest(jobId), RequestOptions.DEFAULT); PostDataRequest.JsonBuilder builder = new PostDataRequest.JsonBuilder(); - for(int i = 0; i < 30; i++) { + for (int i = 0; i < 30; i++) { Map hashMap = new HashMap<>(); hashMap.put("total", randomInt(1000)); - hashMap.put("timestamp", (i+1)*1000); + hashMap.put("timestamp", (i + 1) * 1000); builder.addDoc(hashMap); } PostDataRequest postDataRequest = new PostDataRequest(jobId, builder); @@ -448,17 +462,21 @@ public void testPostData() throws Exception { machineLearningClient.openJob(new OpenJobRequest(jobId), RequestOptions.DEFAULT); PostDataRequest.JsonBuilder builder = new PostDataRequest.JsonBuilder(); - for(int i = 0; i < 10; i++) { + for (int i = 0; i < 10; i++) { Map hashMap = new HashMap<>(); hashMap.put("total", randomInt(1000)); - hashMap.put("timestamp", (i+1)*1000); + hashMap.put("timestamp", (i + 1) * 1000); builder.addDoc(hashMap); } PostDataRequest postDataRequest = new PostDataRequest(jobId, builder); // Post data is deprecated, so expect a deprecation warning - PostDataResponse response = execute(postDataRequest, machineLearningClient::postData, machineLearningClient::postDataAsync, - POST_DATA_OPTIONS); + PostDataResponse response = execute( + postDataRequest, + machineLearningClient::postData, + machineLearningClient::postDataAsync, + POST_DATA_OPTIONS + ); assertEquals(10, response.getDataCounts().getInputRecordCount()); assertEquals(0, response.getDataCounts().getOutOfOrderTimeStampCount()); } @@ -489,8 +507,11 @@ public void testPutDatafeed() throws Exception { String datafeedId = "datafeed-" + jobId; DatafeedConfig datafeedConfig = DatafeedConfig.builder(datafeedId, jobId).setIndices("some_data_index").build(); - PutDatafeedResponse response = execute(new PutDatafeedRequest(datafeedConfig), machineLearningClient::putDatafeed, - machineLearningClient::putDatafeedAsync); + PutDatafeedResponse response = execute( + new PutDatafeedRequest(datafeedConfig), + machineLearningClient::putDatafeed, + machineLearningClient::putDatafeedAsync + ); DatafeedConfig createdDatafeed = response.getResponse(); assertThat(createdDatafeed.getId(), equalTo(datafeedId)); @@ -515,9 +536,11 @@ public void testUpdateDatafeed() throws Exception { DatafeedUpdate datafeedUpdate = DatafeedUpdate.builder(datafeedId).setIndices("some_other_data_index").setScrollSize(10).build(); - response = execute(new UpdateDatafeedRequest(datafeedUpdate), + response = execute( + new UpdateDatafeedRequest(datafeedUpdate), machineLearningClient::updateDatafeed, - machineLearningClient::updateDatafeedAsync); + machineLearningClient::updateDatafeedAsync + ); DatafeedConfig updatedDatafeed = response.getResponse(); assertThat(datafeedUpdate.getId(), equalTo(updatedDatafeed.getId())); @@ -548,8 +571,10 @@ public void testGetDatafeed() throws Exception { assertEquals(2, response.count()); assertThat(response.datafeeds(), hasSize(2)); - assertThat(response.datafeeds().stream().map(DatafeedConfig::getId).collect(Collectors.toList()), - containsInAnyOrder(datafeedId1, datafeedId2)); + assertThat( + response.datafeeds().stream().map(DatafeedConfig::getId).collect(Collectors.toList()), + containsInAnyOrder(datafeedId1, datafeedId2) + ); } // Test getting a single one @@ -568,19 +593,26 @@ public void testGetDatafeed() throws Exception { assertTrue(response.count() == 2L); assertTrue(response.datafeeds().size() == 2L); - assertThat(response.datafeeds().stream().map(DatafeedConfig::getId).collect(Collectors.toList()), - hasItems(datafeedId1, datafeedId2)); + assertThat( + response.datafeeds().stream().map(DatafeedConfig::getId).collect(Collectors.toList()), + hasItems(datafeedId1, datafeedId2) + ); } // Test getting all datafeeds implicitly { - GetDatafeedResponse response = execute(new GetDatafeedRequest(), machineLearningClient::getDatafeed, - machineLearningClient::getDatafeedAsync); + GetDatafeedResponse response = execute( + new GetDatafeedRequest(), + machineLearningClient::getDatafeed, + machineLearningClient::getDatafeedAsync + ); assertTrue(response.count() >= 2L); assertTrue(response.datafeeds().size() >= 2L); - assertThat(response.datafeeds().stream().map(DatafeedConfig::getId).collect(Collectors.toList()), - hasItems(datafeedId1, datafeedId2)); + assertThat( + response.datafeeds().stream().map(DatafeedConfig::getId).collect(Collectors.toList()), + hasItems(datafeedId1, datafeedId2) + ); } // Test get missing pattern with allow_no_match set to true @@ -597,8 +629,10 @@ public void testGetDatafeed() throws Exception { GetDatafeedRequest request = new GetDatafeedRequest("missing-*"); request.setAllowNoMatch(false); - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> execute(request, machineLearningClient::getDatafeed, machineLearningClient::getDatafeedAsync)); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> execute(request, machineLearningClient::getDatafeed, machineLearningClient::getDatafeedAsync) + ); assertThat(e.status(), equalTo(RestStatus.NOT_FOUND)); } } @@ -613,8 +647,11 @@ public void testDeleteDatafeed() throws Exception { DatafeedConfig datafeedConfig = DatafeedConfig.builder(datafeedId, jobId).setIndices("some_data_index").build(); execute(new PutDatafeedRequest(datafeedConfig), machineLearningClient::putDatafeed, machineLearningClient::putDatafeedAsync); - AcknowledgedResponse response = execute(new DeleteDatafeedRequest(datafeedId), machineLearningClient::deleteDatafeed, - machineLearningClient::deleteDatafeedAsync); + AcknowledgedResponse response = execute( + new DeleteDatafeedRequest(datafeedId), + machineLearningClient::deleteDatafeed, + machineLearningClient::deleteDatafeedAsync + ); assertTrue(response.isAcknowledged()); } @@ -627,15 +664,15 @@ public void testStartDatafeed() throws Exception { createIndex(indexName, defaultMappingForTest()); BulkRequest bulk = new BulkRequest(); bulk.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - long now = (System.currentTimeMillis()/1000)*1000; + long now = (System.currentTimeMillis() / 1000) * 1000; long thePast = now - 60000; int i = 0; long pastCopy = thePast; - while(pastCopy < now) { + while (pastCopy < now) { IndexRequest doc = new IndexRequest(); doc.index(indexName); doc.id("id" + i); - doc.source("{\"total\":" +randomInt(1000) + ",\"timestamp\":"+ pastCopy +"}", XContentType.JSON); + doc.source("{\"total\":" + randomInt(1000) + ",\"timestamp\":" + pastCopy + "}", XContentType.JSON); bulk.add(doc); pastCopy += 1000; i++; @@ -653,17 +690,19 @@ public void testStartDatafeed() throws Exception { DatafeedConfig datafeed = DatafeedConfig.builder(datafeedId, jobId) .setIndices(indexName) .setQueryDelay(TimeValue.timeValueSeconds(1)) - .setFrequency(TimeValue.timeValueSeconds(1)).build(); + .setFrequency(TimeValue.timeValueSeconds(1)) + .build(); machineLearningClient.putDatafeed(new PutDatafeedRequest(datafeed), RequestOptions.DEFAULT); - StartDatafeedRequest startDatafeedRequest = new StartDatafeedRequest(datafeedId); startDatafeedRequest.setStart(String.valueOf(thePast)); // Should only process two documents startDatafeedRequest.setEnd(String.valueOf(thePast + 2000)); - StartDatafeedResponse response = execute(startDatafeedRequest, + StartDatafeedResponse response = execute( + startDatafeedRequest, machineLearningClient::startDatafeed, - machineLearningClient::startDatafeedAsync); + machineLearningClient::startDatafeedAsync + ); assertTrue(response.isStarted()); @@ -677,9 +716,11 @@ public void testStartDatafeed() throws Exception { StartDatafeedRequest wholeDataFeed = new StartDatafeedRequest(datafeedId); // Process all documents and end the stream wholeDataFeed.setEnd(String.valueOf(now)); - StartDatafeedResponse wholeResponse = execute(wholeDataFeed, + StartDatafeedResponse wholeResponse = execute( + wholeDataFeed, machineLearningClient::startDatafeed, - machineLearningClient::startDatafeedAsync); + machineLearningClient::startDatafeedAsync + ); assertTrue(wholeResponse.isStarted()); assertBusy(() -> { @@ -724,30 +765,38 @@ public void testStopDatafeed() throws Exception { { StopDatafeedRequest request = new StopDatafeedRequest(datafeedId1); request.setAllowNoMatch(false); - StopDatafeedResponse stopDatafeedResponse = execute(request, + StopDatafeedResponse stopDatafeedResponse = execute( + request, machineLearningClient::stopDatafeed, - machineLearningClient::stopDatafeedAsync); + machineLearningClient::stopDatafeedAsync + ); assertTrue(stopDatafeedResponse.isStopped()); } { StopDatafeedRequest request = new StopDatafeedRequest(datafeedId2, datafeedId3); request.setAllowNoMatch(false); - StopDatafeedResponse stopDatafeedResponse = execute(request, + StopDatafeedResponse stopDatafeedResponse = execute( + request, machineLearningClient::stopDatafeed, - machineLearningClient::stopDatafeedAsync); + machineLearningClient::stopDatafeedAsync + ); assertTrue(stopDatafeedResponse.isStopped()); } { - StopDatafeedResponse stopDatafeedResponse = execute(new StopDatafeedRequest("datafeed_that_doesnot_exist*"), + StopDatafeedResponse stopDatafeedResponse = execute( + new StopDatafeedRequest("datafeed_that_doesnot_exist*"), machineLearningClient::stopDatafeed, - machineLearningClient::stopDatafeedAsync); + machineLearningClient::stopDatafeedAsync + ); assertTrue(stopDatafeedResponse.isStopped()); } { StopDatafeedRequest request = new StopDatafeedRequest("datafeed_that_doesnot_exist*"); request.setAllowNoMatch(false); - ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, - () -> execute(request, machineLearningClient::stopDatafeed, machineLearningClient::stopDatafeedAsync)); + ElasticsearchStatusException exception = expectThrows( + ElasticsearchStatusException.class, + () -> execute(request, machineLearningClient::stopDatafeed, machineLearningClient::stopDatafeedAsync) + ); assertThat(exception.status().getStatus(), equalTo(404)); } } @@ -778,8 +827,11 @@ public void testGetDatafeedStats() throws Exception { GetDatafeedStatsRequest request = new GetDatafeedStatsRequest(datafeedId1); // Test getting specific - GetDatafeedStatsResponse response = - execute(request, machineLearningClient::getDatafeedStats, machineLearningClient::getDatafeedStatsAsync); + GetDatafeedStatsResponse response = execute( + request, + machineLearningClient::getDatafeedStats, + machineLearningClient::getDatafeedStatsAsync + ); assertEquals(1, response.count()); assertThat(response.datafeedStats(), hasSize(1)); @@ -792,31 +844,42 @@ public void testGetDatafeedStats() throws Exception { assertTrue(response.count() >= 2L); assertTrue(response.datafeedStats().size() >= 2L); - assertThat(response.datafeedStats().stream().map(DatafeedStats::getDatafeedId).collect(Collectors.toList()), - hasItems(datafeedId1, datafeedId2)); + assertThat( + response.datafeedStats().stream().map(DatafeedStats::getDatafeedId).collect(Collectors.toList()), + hasItems(datafeedId1, datafeedId2) + ); // Test getting all implicitly - response = - execute(new GetDatafeedStatsRequest(), machineLearningClient::getDatafeedStats, machineLearningClient::getDatafeedStatsAsync); + response = execute( + new GetDatafeedStatsRequest(), + machineLearningClient::getDatafeedStats, + machineLearningClient::getDatafeedStatsAsync + ); assertTrue(response.count() >= 2L); assertTrue(response.datafeedStats().size() >= 2L); - assertThat(response.datafeedStats().stream().map(DatafeedStats::getDatafeedId).collect(Collectors.toList()), - hasItems(datafeedId1, datafeedId2)); + assertThat( + response.datafeedStats().stream().map(DatafeedStats::getDatafeedId).collect(Collectors.toList()), + hasItems(datafeedId1, datafeedId2) + ); // Test getting all with wildcard request = new GetDatafeedStatsRequest("ml-get-datafeed-stats-test-id-*"); response = execute(request, machineLearningClient::getDatafeedStats, machineLearningClient::getDatafeedStatsAsync); assertEquals(2L, response.count()); assertThat(response.datafeedStats(), hasSize(2)); - assertThat(response.datafeedStats().stream().map(DatafeedStats::getDatafeedId).collect(Collectors.toList()), - hasItems(datafeedId1, datafeedId2)); + assertThat( + response.datafeedStats().stream().map(DatafeedStats::getDatafeedId).collect(Collectors.toList()), + hasItems(datafeedId1, datafeedId2) + ); // Test when allow_no_match is false final GetDatafeedStatsRequest erroredRequest = new GetDatafeedStatsRequest("datafeeds-that-do-not-exist*"); erroredRequest.setAllowNoMatch(false); - ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, - () -> execute(erroredRequest, machineLearningClient::getDatafeedStats, machineLearningClient::getDatafeedStatsAsync)); + ElasticsearchStatusException exception = expectThrows( + ElasticsearchStatusException.class, + () -> execute(erroredRequest, machineLearningClient::getDatafeedStats, machineLearningClient::getDatafeedStatsAsync) + ); assertThat(exception.status().getStatus(), equalTo(404)); } @@ -828,16 +891,16 @@ public void testPreviewDatafeed() throws Exception { createIndex(indexName, defaultMappingForTest()); BulkRequest bulk = new BulkRequest(); bulk.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - long now = (System.currentTimeMillis()/1000)*1000; + long now = (System.currentTimeMillis() / 1000) * 1000; long thePast = now - 60000; int i = 0; List totalTotals = new ArrayList<>(60); - while(thePast < now) { + while (thePast < now) { Integer total = randomInt(1000); IndexRequest doc = new IndexRequest(); doc.index(indexName); doc.id("id" + i); - doc.source("{\"total\":" + total + ",\"timestamp\":"+ thePast +"}", XContentType.JSON); + doc.source("{\"total\":" + total + ",\"timestamp\":" + thePast + "}", XContentType.JSON); bulk.add(doc); thePast += 1000; i++; @@ -855,14 +918,17 @@ public void testPreviewDatafeed() throws Exception { DatafeedConfig datafeed = DatafeedConfig.builder(datafeedId, jobId) .setIndices(indexName) .setQueryDelay(TimeValue.timeValueSeconds(1)) - .setFrequency(TimeValue.timeValueSeconds(1)).build(); + .setFrequency(TimeValue.timeValueSeconds(1)) + .build(); machineLearningClient.putDatafeed(new PutDatafeedRequest(datafeed), RequestOptions.DEFAULT); - PreviewDatafeedResponse response = execute(new PreviewDatafeedRequest(datafeedId), + PreviewDatafeedResponse response = execute( + new PreviewDatafeedRequest(datafeedId), machineLearningClient::previewDatafeed, - machineLearningClient::previewDatafeedAsync); + machineLearningClient::previewDatafeedAsync + ); - Integer[] totals = response.getDataList().stream().map(map -> (Integer)map.get("total")).toArray(Integer[]::new); + Integer[] totals = response.getDataList().stream().map(map -> (Integer) map.get("total")).toArray(Integer[]::new); assertThat(totalTotals, containsInAnyOrder(totals)); } @@ -870,14 +936,16 @@ public void testDeleteExpiredDataGivenNothingToDelete() throws Exception { // Tests that nothing goes wrong when there's nothing to delete MachineLearningClient machineLearningClient = highLevelClient().machineLearning(); - DeleteExpiredDataResponse response = execute(new DeleteExpiredDataRequest(), + DeleteExpiredDataResponse response = execute( + new DeleteExpiredDataRequest(), machineLearningClient::deleteExpiredData, - machineLearningClient::deleteExpiredDataAsync); + machineLearningClient::deleteExpiredDataAsync + ); assertTrue(response.getDeleted()); } - private String createExpiredData(String jobId) throws Exception { + private String createExpiredData(String jobId) throws Exception { String indexName = jobId + "-data"; // Set up the index and docs createIndex(indexName, defaultMappingForTest()); @@ -965,8 +1033,11 @@ public void testDeleteExpiredData() throws Exception { MachineLearningClient machineLearningClient = highLevelClient().machineLearning(); GetModelSnapshotsRequest getModelSnapshotsRequest = new GetModelSnapshotsRequest(jobId); - GetModelSnapshotsResponse getModelSnapshotsResponse = execute(getModelSnapshotsRequest, machineLearningClient::getModelSnapshots, - machineLearningClient::getModelSnapshotsAsync); + GetModelSnapshotsResponse getModelSnapshotsResponse = execute( + getModelSnapshotsRequest, + machineLearningClient::getModelSnapshots, + machineLearningClient::getModelSnapshotsAsync + ); assertEquals(2L, getModelSnapshotsResponse.count()); @@ -977,16 +1048,17 @@ public void testDeleteExpiredData() throws Exception { Iterable hits = searchAll(".ml-state*"); List target = new ArrayList<>(); hits.forEach(target::add); - long numMatches = target.stream() - .filter(c -> c.getId().startsWith("non_existing_job")) - .count(); + long numMatches = target.stream().filter(c -> c.getId().startsWith("non_existing_job")).count(); assertThat(numMatches, equalTo(1L)); } DeleteExpiredDataRequest request = new DeleteExpiredDataRequest(); - DeleteExpiredDataResponse response = execute(request, machineLearningClient::deleteExpiredData, - machineLearningClient::deleteExpiredDataAsync); + DeleteExpiredDataResponse response = execute( + request, + machineLearningClient::deleteExpiredData, + machineLearningClient::deleteExpiredDataAsync + ); assertTrue(response.getDeleted()); @@ -995,8 +1067,11 @@ public void testDeleteExpiredData() throws Exception { waitUntil(() -> false, 1, TimeUnit.SECONDS); GetModelSnapshotsRequest getModelSnapshotsRequest1 = new GetModelSnapshotsRequest(jobId); - GetModelSnapshotsResponse getModelSnapshotsResponse1 = execute(getModelSnapshotsRequest1, machineLearningClient::getModelSnapshots, - machineLearningClient::getModelSnapshotsAsync); + GetModelSnapshotsResponse getModelSnapshotsResponse1 = execute( + getModelSnapshotsRequest1, + machineLearningClient::getModelSnapshots, + machineLearningClient::getModelSnapshotsAsync + ); assertEquals(1L, getModelSnapshotsResponse1.count()); @@ -1007,9 +1082,7 @@ public void testDeleteExpiredData() throws Exception { Iterable hits = searchAll(".ml-state*"); List hitList = new ArrayList<>(); hits.forEach(hitList::add); - long numMatches = hitList.stream() - .filter(c -> c.getId().startsWith("non_existing_job")) - .count(); + long numMatches = hitList.stream().filter(c -> c.getId().startsWith("non_existing_job")).count(); assertThat(numMatches, equalTo(0L)); } @@ -1027,10 +1100,10 @@ public void testDeleteForecast() throws Exception { machineLearningClient.putJob(new PutJobRequest(noForecastsJob), RequestOptions.DEFAULT); PostDataRequest.JsonBuilder builder = new PostDataRequest.JsonBuilder(); - for(int i = 0; i < 30; i++) { + for (int i = 0; i < 30; i++) { Map hashMap = new HashMap<>(); hashMap.put("total", randomInt(1000)); - hashMap.put("timestamp", (i+1)*1000); + hashMap.put("timestamp", (i + 1) * 1000); builder.addDoc(hashMap); } @@ -1046,8 +1119,11 @@ public void testDeleteForecast() throws Exception { { DeleteForecastRequest request = new DeleteForecastRequest(jobId); request.setForecastIds(forecastJobResponse1.getForecastId(), forecastJobResponse2.getForecastId()); - AcknowledgedResponse response = execute(request, machineLearningClient::deleteForecast, - machineLearningClient::deleteForecastAsync); + AcknowledgedResponse response = execute( + request, + machineLearningClient::deleteForecast, + machineLearningClient::deleteForecastAsync + ); assertTrue(response.isAcknowledged()); assertFalse(forecastExists(jobId, forecastJobResponse1.getForecastId())); assertFalse(forecastExists(jobId, forecastJobResponse2.getForecastId())); @@ -1055,15 +1131,20 @@ public void testDeleteForecast() throws Exception { { DeleteForecastRequest request = DeleteForecastRequest.deleteAllForecasts(noForecastsJob.getId()); request.setAllowNoForecasts(true); - AcknowledgedResponse response = execute(request, machineLearningClient::deleteForecast, - machineLearningClient::deleteForecastAsync); + AcknowledgedResponse response = execute( + request, + machineLearningClient::deleteForecast, + machineLearningClient::deleteForecastAsync + ); assertTrue(response.isAcknowledged()); } { DeleteForecastRequest request = DeleteForecastRequest.deleteAllForecasts(noForecastsJob.getId()); request.setAllowNoForecasts(false); - ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, - () -> execute(request, machineLearningClient::deleteForecast, machineLearningClient::deleteForecastAsync)); + ElasticsearchStatusException exception = expectThrows( + ElasticsearchStatusException.class, + () -> execute(request, machineLearningClient::deleteForecast, machineLearningClient::deleteForecastAsync) + ); assertThat(exception.status().getStatus(), equalTo(404)); } } @@ -1088,8 +1169,11 @@ private boolean forecastExists(String jobId, String forecastId) throws Exception public void testPutCalendar() throws IOException { Calendar calendar = CalendarTests.testInstance(); MachineLearningClient machineLearningClient = highLevelClient().machineLearning(); - PutCalendarResponse putCalendarResponse = execute(new PutCalendarRequest(calendar), machineLearningClient::putCalendar, - machineLearningClient::putCalendarAsync); + PutCalendarResponse putCalendarResponse = execute( + new PutCalendarRequest(calendar), + machineLearningClient::putCalendar, + machineLearningClient::putCalendarAsync + ); assertThat(putCalendarResponse.getCalendar(), equalTo(calendar)); } @@ -1097,42 +1181,54 @@ public void testPutCalendar() throws IOException { public void testPutCalendarJob() throws IOException { Calendar calendar = new Calendar("put-calendar-job-id", Collections.singletonList("put-calendar-job-0"), null); MachineLearningClient machineLearningClient = highLevelClient().machineLearning(); - PutCalendarResponse putCalendarResponse = - machineLearningClient.putCalendar(new PutCalendarRequest(calendar), RequestOptions.DEFAULT); + PutCalendarResponse putCalendarResponse = machineLearningClient.putCalendar( + new PutCalendarRequest(calendar), + RequestOptions.DEFAULT + ); - assertThat(putCalendarResponse.getCalendar().getJobIds(), containsInAnyOrder( "put-calendar-job-0")); + assertThat(putCalendarResponse.getCalendar().getJobIds(), containsInAnyOrder("put-calendar-job-0")); String jobId1 = "put-calendar-job-1"; String jobId2 = "put-calendar-job-2"; PutCalendarJobRequest putCalendarJobRequest = new PutCalendarJobRequest(calendar.getId(), jobId1, jobId2); - putCalendarResponse = execute(putCalendarJobRequest, + putCalendarResponse = execute( + putCalendarJobRequest, machineLearningClient::putCalendarJob, - machineLearningClient::putCalendarJobAsync); + machineLearningClient::putCalendarJobAsync + ); assertThat(putCalendarResponse.getCalendar().getJobIds(), containsInAnyOrder(jobId1, jobId2, "put-calendar-job-0")); } public void testDeleteCalendarJob() throws IOException { - Calendar calendar = new Calendar("del-calendar-job-id", + Calendar calendar = new Calendar( + "del-calendar-job-id", Arrays.asList("del-calendar-job-0", "del-calendar-job-1", "del-calendar-job-2"), - null); + null + ); MachineLearningClient machineLearningClient = highLevelClient().machineLearning(); - PutCalendarResponse putCalendarResponse = - machineLearningClient.putCalendar(new PutCalendarRequest(calendar), RequestOptions.DEFAULT); + PutCalendarResponse putCalendarResponse = machineLearningClient.putCalendar( + new PutCalendarRequest(calendar), + RequestOptions.DEFAULT + ); - assertThat(putCalendarResponse.getCalendar().getJobIds(), - containsInAnyOrder("del-calendar-job-0", "del-calendar-job-1", "del-calendar-job-2")); + assertThat( + putCalendarResponse.getCalendar().getJobIds(), + containsInAnyOrder("del-calendar-job-0", "del-calendar-job-1", "del-calendar-job-2") + ); String jobId1 = "del-calendar-job-0"; String jobId2 = "del-calendar-job-2"; DeleteCalendarJobRequest deleteCalendarJobRequest = new DeleteCalendarJobRequest(calendar.getId(), jobId1, jobId2); - putCalendarResponse = execute(deleteCalendarJobRequest, + putCalendarResponse = execute( + deleteCalendarJobRequest, machineLearningClient::deleteCalendarJob, - machineLearningClient::deleteCalendarJobAsync); + machineLearningClient::deleteCalendarJobAsync + ); assertThat(putCalendarResponse.getCalendar().getJobIds(), containsInAnyOrder("del-calendar-job-1")); } @@ -1147,16 +1243,20 @@ public void testGetCalendars() throws Exception { GetCalendarsRequest getCalendarsRequest = new GetCalendarsRequest(); getCalendarsRequest.setCalendarId("_all"); - GetCalendarsResponse getCalendarsResponse = execute(getCalendarsRequest, machineLearningClient::getCalendars, - machineLearningClient::getCalendarsAsync); + GetCalendarsResponse getCalendarsResponse = execute( + getCalendarsRequest, + machineLearningClient::getCalendars, + machineLearningClient::getCalendarsAsync + ); assertEquals(2, getCalendarsResponse.count()); assertEquals(2, getCalendarsResponse.calendars().size()); - assertThat(getCalendarsResponse.calendars().stream().map(Calendar::getId).collect(Collectors.toList()), - hasItems(calendar1.getId(), calendar1.getId())); + assertThat( + getCalendarsResponse.calendars().stream().map(Calendar::getId).collect(Collectors.toList()), + hasItems(calendar1.getId(), calendar1.getId()) + ); getCalendarsRequest.setCalendarId(calendar1.getId()); - getCalendarsResponse = execute(getCalendarsRequest, machineLearningClient::getCalendars, - machineLearningClient::getCalendarsAsync); + getCalendarsResponse = execute(getCalendarsRequest, machineLearningClient::getCalendars, machineLearningClient::getCalendarsAsync); assertEquals(1, getCalendarsResponse.count()); assertEquals(calendar1, getCalendarsResponse.calendars().get(0)); } @@ -1164,18 +1264,24 @@ public void testGetCalendars() throws Exception { public void testDeleteCalendar() throws IOException { Calendar calendar = CalendarTests.testInstance(); MachineLearningClient machineLearningClient = highLevelClient().machineLearning(); - execute(new PutCalendarRequest(calendar), machineLearningClient::putCalendar, - machineLearningClient::putCalendarAsync); + execute(new PutCalendarRequest(calendar), machineLearningClient::putCalendar, machineLearningClient::putCalendarAsync); - AcknowledgedResponse response = execute(new DeleteCalendarRequest(calendar.getId()), - machineLearningClient::deleteCalendar, - machineLearningClient::deleteCalendarAsync); + AcknowledgedResponse response = execute( + new DeleteCalendarRequest(calendar.getId()), + machineLearningClient::deleteCalendar, + machineLearningClient::deleteCalendarAsync + ); assertTrue(response.isAcknowledged()); // calendar is missing - ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, - () -> execute(new DeleteCalendarRequest(calendar.getId()), machineLearningClient::deleteCalendar, - machineLearningClient::deleteCalendarAsync)); + ElasticsearchStatusException exception = expectThrows( + ElasticsearchStatusException.class, + () -> execute( + new DeleteCalendarRequest(calendar.getId()), + machineLearningClient::deleteCalendar, + machineLearningClient::deleteCalendarAsync + ) + ); assertThat(exception.status().getStatus(), equalTo(404)); } @@ -1193,18 +1299,22 @@ public void testGetCalendarEvent() throws Exception { { GetCalendarEventsRequest getCalendarEventsRequest = new GetCalendarEventsRequest(calendar.getId()); - GetCalendarEventsResponse getCalendarEventsResponse = execute(getCalendarEventsRequest, + GetCalendarEventsResponse getCalendarEventsResponse = execute( + getCalendarEventsRequest, machineLearningClient::getCalendarEvents, - machineLearningClient::getCalendarEventsAsync); + machineLearningClient::getCalendarEventsAsync + ); assertThat(getCalendarEventsResponse.events().size(), equalTo(3)); assertThat(getCalendarEventsResponse.count(), equalTo(3L)); } { GetCalendarEventsRequest getCalendarEventsRequest = new GetCalendarEventsRequest(calendar.getId()); getCalendarEventsRequest.setPageParams(new PageParams(1, 2)); - GetCalendarEventsResponse getCalendarEventsResponse = execute(getCalendarEventsRequest, + GetCalendarEventsResponse getCalendarEventsResponse = execute( + getCalendarEventsRequest, machineLearningClient::getCalendarEvents, - machineLearningClient::getCalendarEventsAsync); + machineLearningClient::getCalendarEventsAsync + ); assertThat(getCalendarEventsResponse.events().size(), equalTo(2)); assertThat(getCalendarEventsResponse.count(), equalTo(3L)); } @@ -1212,9 +1322,11 @@ public void testGetCalendarEvent() throws Exception { machineLearningClient.putJob(new PutJobRequest(buildJob("get-calendar-event-job")), RequestOptions.DEFAULT); GetCalendarEventsRequest getCalendarEventsRequest = new GetCalendarEventsRequest("_all"); getCalendarEventsRequest.setJobId("get-calendar-event-job"); - GetCalendarEventsResponse getCalendarEventsResponse = execute(getCalendarEventsRequest, + GetCalendarEventsResponse getCalendarEventsResponse = execute( + getCalendarEventsRequest, machineLearningClient::getCalendarEvents, - machineLearningClient::getCalendarEventsAsync); + machineLearningClient::getCalendarEventsAsync + ); assertThat(getCalendarEventsResponse.events().size(), equalTo(3)); assertThat(getCalendarEventsResponse.count(), equalTo(3L)); } @@ -1232,9 +1344,11 @@ public void testPostCalendarEvent() throws Exception { PostCalendarEventRequest postCalendarEventRequest = new PostCalendarEventRequest(calendar.getId(), events); - PostCalendarEventResponse postCalendarEventResponse = execute(postCalendarEventRequest, + PostCalendarEventResponse postCalendarEventResponse = execute( + postCalendarEventRequest, machineLearningClient::postCalendarEvent, - machineLearningClient::postCalendarEventAsync); + machineLearningClient::postCalendarEventAsync + ); assertThat(postCalendarEventResponse.getScheduledEvents(), containsInAnyOrder(events.toArray())); } @@ -1249,22 +1363,28 @@ public void testDeleteCalendarEvent() throws IOException { } machineLearningClient.postCalendarEvent(new PostCalendarEventRequest(calendar.getId(), events), RequestOptions.DEFAULT); - GetCalendarEventsResponse getCalendarEventsResponse = - machineLearningClient.getCalendarEvents(new GetCalendarEventsRequest(calendar.getId()), RequestOptions.DEFAULT); + GetCalendarEventsResponse getCalendarEventsResponse = machineLearningClient.getCalendarEvents( + new GetCalendarEventsRequest(calendar.getId()), + RequestOptions.DEFAULT + ); assertThat(getCalendarEventsResponse.events().size(), equalTo(3)); String deletedEvent = getCalendarEventsResponse.events().get(0).getEventId(); DeleteCalendarEventRequest deleteCalendarEventRequest = new DeleteCalendarEventRequest(calendar.getId(), deletedEvent); - AcknowledgedResponse response = execute(deleteCalendarEventRequest, + AcknowledgedResponse response = execute( + deleteCalendarEventRequest, machineLearningClient::deleteCalendarEvent, - machineLearningClient::deleteCalendarEventAsync); + machineLearningClient::deleteCalendarEventAsync + ); assertThat(response.isAcknowledged(), is(true)); - getCalendarEventsResponse = - machineLearningClient.getCalendarEvents(new GetCalendarEventsRequest(calendar.getId()), RequestOptions.DEFAULT); + getCalendarEventsResponse = machineLearningClient.getCalendarEvents( + new GetCalendarEventsRequest(calendar.getId()), + RequestOptions.DEFAULT + ); List remainingIds = getCalendarEventsResponse.events() .stream() .map(ScheduledEvent::getEventId) @@ -1280,16 +1400,17 @@ public void testEstimateModelMemory() throws Exception { String byFieldName = randomAlphaOfLength(10); String influencerFieldName = randomAlphaOfLength(10); AnalysisConfig analysisConfig = AnalysisConfig.builder( - Collections.singletonList( - Detector.builder().setFunction("count").setByFieldName(byFieldName).build() - )).setInfluencers(Collections.singletonList(influencerFieldName)).build(); + Collections.singletonList(Detector.builder().setFunction("count").setByFieldName(byFieldName).build()) + ).setInfluencers(Collections.singletonList(influencerFieldName)).build(); EstimateModelMemoryRequest estimateModelMemoryRequest = new EstimateModelMemoryRequest(analysisConfig); estimateModelMemoryRequest.setOverallCardinality(Collections.singletonMap(byFieldName, randomNonNegativeLong())); estimateModelMemoryRequest.setMaxBucketCardinality(Collections.singletonMap(influencerFieldName, randomNonNegativeLong())); EstimateModelMemoryResponse estimateModelMemoryResponse = execute( estimateModelMemoryRequest, - machineLearningClient::estimateModelMemory, machineLearningClient::estimateModelMemoryAsync); + machineLearningClient::estimateModelMemory, + machineLearningClient::estimateModelMemoryAsync + ); ByteSizeValue modelMemoryEstimate = estimateModelMemoryResponse.getModelMemoryEstimate(); assertThat(modelMemoryEstimate.getBytes(), greaterThanOrEqualTo(10000000L)); @@ -1300,12 +1421,8 @@ public void testPutDataFrameAnalyticsConfig_GivenOutlierDetectionAnalysis() thro String configId = "test-put-df-analytics-outlier-detection"; DataFrameAnalyticsConfig config = DataFrameAnalyticsConfig.builder() .setId(configId) - .setSource(DataFrameAnalyticsSource.builder() - .setIndex("put-test-source-index") - .build()) - .setDest(DataFrameAnalyticsDest.builder() - .setIndex("put-test-dest-index") - .build()) + .setSource(DataFrameAnalyticsSource.builder().setIndex("put-test-source-index").build()) + .setDest(DataFrameAnalyticsDest.builder().setIndex("put-test-dest-index").build()) .setAnalysis(org.elasticsearch.client.ml.dataframe.OutlierDetection.createDefault()) .setDescription("some description") .build(); @@ -1314,17 +1431,25 @@ public void testPutDataFrameAnalyticsConfig_GivenOutlierDetectionAnalysis() thro PutDataFrameAnalyticsResponse putDataFrameAnalyticsResponse = execute( new PutDataFrameAnalyticsRequest(config), - machineLearningClient::putDataFrameAnalytics, machineLearningClient::putDataFrameAnalyticsAsync); + machineLearningClient::putDataFrameAnalytics, + machineLearningClient::putDataFrameAnalyticsAsync + ); DataFrameAnalyticsConfig createdConfig = putDataFrameAnalyticsResponse.getConfig(); assertThat(createdConfig.getId(), equalTo(config.getId())); assertThat(createdConfig.getSource().getIndex(), equalTo(config.getSource().getIndex())); assertThat(createdConfig.getSource().getQueryConfig(), equalTo(new QueryConfig(new MatchAllQueryBuilder()))); // default value assertThat(createdConfig.getDest().getIndex(), equalTo(config.getDest().getIndex())); assertThat(createdConfig.getDest().getResultsField(), equalTo("ml")); // default value - assertThat(createdConfig.getAnalysis(), equalTo(org.elasticsearch.client.ml.dataframe.OutlierDetection.builder() - .setComputeFeatureInfluence(true) - .setOutlierFraction(0.05) - .setStandardizationEnabled(true).build())); + assertThat( + createdConfig.getAnalysis(), + equalTo( + org.elasticsearch.client.ml.dataframe.OutlierDetection.builder() + .setComputeFeatureInfluence(true) + .setOutlierFraction(0.05) + .setStandardizationEnabled(true) + .build() + ) + ); assertThat(createdConfig.getAnalyzedFields(), equalTo(config.getAnalyzedFields())); assertThat(createdConfig.getModelMemoryLimit(), equalTo(ByteSizeValue.parseBytesSizeValue("1gb", ""))); // default value assertThat(createdConfig.getDescription(), equalTo("some description")); @@ -1336,33 +1461,31 @@ public void testPutDataFrameAnalyticsConfig_GivenRegression() throws Exception { String configId = "test-put-df-analytics-regression"; DataFrameAnalyticsConfig config = DataFrameAnalyticsConfig.builder() .setId(configId) - .setSource(DataFrameAnalyticsSource.builder() - .setIndex("put-test-source-index") - .build()) - .setDest(DataFrameAnalyticsDest.builder() - .setIndex("put-test-dest-index") - .build()) - .setAnalysis(org.elasticsearch.client.ml.dataframe.Regression.builder("my_dependent_variable") - .setPredictionFieldName("my_dependent_variable_prediction") - .setTrainingPercent(80.0) - .setRandomizeSeed(42L) - .setLambda(1.0) - .setGamma(1.0) - .setEta(1.0) - .setMaxTrees(10) - .setFeatureBagFraction(0.5) - .setNumTopFeatureImportanceValues(3) - .setLossFunction(org.elasticsearch.client.ml.dataframe.Regression.LossFunction.MSLE) - .setLossFunctionParameter(1.0) - .setAlpha(0.5) - .setEtaGrowthRatePerTree(1.0) - .setSoftTreeDepthLimit(1.0) - .setSoftTreeDepthTolerance(0.1) - .setDownsampleFactor(0.5) - .setMaxOptimizationRoundsPerHyperparameter(3) - .setMaxOptimizationRoundsPerHyperparameter(3) - .setEarlyStoppingEnabled(false) - .build()) + .setSource(DataFrameAnalyticsSource.builder().setIndex("put-test-source-index").build()) + .setDest(DataFrameAnalyticsDest.builder().setIndex("put-test-dest-index").build()) + .setAnalysis( + org.elasticsearch.client.ml.dataframe.Regression.builder("my_dependent_variable") + .setPredictionFieldName("my_dependent_variable_prediction") + .setTrainingPercent(80.0) + .setRandomizeSeed(42L) + .setLambda(1.0) + .setGamma(1.0) + .setEta(1.0) + .setMaxTrees(10) + .setFeatureBagFraction(0.5) + .setNumTopFeatureImportanceValues(3) + .setLossFunction(org.elasticsearch.client.ml.dataframe.Regression.LossFunction.MSLE) + .setLossFunctionParameter(1.0) + .setAlpha(0.5) + .setEtaGrowthRatePerTree(1.0) + .setSoftTreeDepthLimit(1.0) + .setSoftTreeDepthTolerance(0.1) + .setDownsampleFactor(0.5) + .setMaxOptimizationRoundsPerHyperparameter(3) + .setMaxOptimizationRoundsPerHyperparameter(3) + .setEarlyStoppingEnabled(false) + .build() + ) .setDescription("this is a regression") .build(); @@ -1370,7 +1493,9 @@ public void testPutDataFrameAnalyticsConfig_GivenRegression() throws Exception { PutDataFrameAnalyticsResponse putDataFrameAnalyticsResponse = execute( new PutDataFrameAnalyticsRequest(config), - machineLearningClient::putDataFrameAnalytics, machineLearningClient::putDataFrameAnalyticsAsync); + machineLearningClient::putDataFrameAnalytics, + machineLearningClient::putDataFrameAnalyticsAsync + ); DataFrameAnalyticsConfig createdConfig = putDataFrameAnalyticsResponse.getConfig(); assertThat(createdConfig.getId(), equalTo(config.getId())); assertThat(createdConfig.getSource().getIndex(), equalTo(config.getSource().getIndex())); @@ -1388,33 +1513,32 @@ public void testPutDataFrameAnalyticsConfig_GivenClassification() throws Excepti String configId = "test-put-df-analytics-classification"; DataFrameAnalyticsConfig config = DataFrameAnalyticsConfig.builder() .setId(configId) - .setSource(DataFrameAnalyticsSource.builder() - .setIndex("put-test-source-index") - .build()) - .setDest(DataFrameAnalyticsDest.builder() - .setIndex("put-test-dest-index") - .build()) - .setAnalysis(org.elasticsearch.client.ml.dataframe.Classification.builder("my_dependent_variable") - .setPredictionFieldName("my_dependent_variable_prediction") - .setTrainingPercent(80.0) - .setRandomizeSeed(42L) - .setClassAssignmentObjective( - org.elasticsearch.client.ml.dataframe.Classification.ClassAssignmentObjective.MAXIMIZE_ACCURACY) - .setNumTopClasses(1) - .setLambda(1.0) - .setGamma(1.0) - .setEta(1.0) - .setMaxTrees(10) - .setFeatureBagFraction(0.5) - .setNumTopFeatureImportanceValues(3) - .setAlpha(0.5) - .setEtaGrowthRatePerTree(1.0) - .setSoftTreeDepthLimit(1.0) - .setSoftTreeDepthTolerance(0.1) - .setDownsampleFactor(0.5) - .setMaxOptimizationRoundsPerHyperparameter(3) - .setEarlyStoppingEnabled(false) - .build()) + .setSource(DataFrameAnalyticsSource.builder().setIndex("put-test-source-index").build()) + .setDest(DataFrameAnalyticsDest.builder().setIndex("put-test-dest-index").build()) + .setAnalysis( + org.elasticsearch.client.ml.dataframe.Classification.builder("my_dependent_variable") + .setPredictionFieldName("my_dependent_variable_prediction") + .setTrainingPercent(80.0) + .setRandomizeSeed(42L) + .setClassAssignmentObjective( + org.elasticsearch.client.ml.dataframe.Classification.ClassAssignmentObjective.MAXIMIZE_ACCURACY + ) + .setNumTopClasses(1) + .setLambda(1.0) + .setGamma(1.0) + .setEta(1.0) + .setMaxTrees(10) + .setFeatureBagFraction(0.5) + .setNumTopFeatureImportanceValues(3) + .setAlpha(0.5) + .setEtaGrowthRatePerTree(1.0) + .setSoftTreeDepthLimit(1.0) + .setSoftTreeDepthTolerance(0.1) + .setDownsampleFactor(0.5) + .setMaxOptimizationRoundsPerHyperparameter(3) + .setEarlyStoppingEnabled(false) + .build() + ) .setDescription("this is a classification") .build(); @@ -1422,7 +1546,9 @@ public void testPutDataFrameAnalyticsConfig_GivenClassification() throws Excepti PutDataFrameAnalyticsResponse putDataFrameAnalyticsResponse = execute( new PutDataFrameAnalyticsRequest(config), - machineLearningClient::putDataFrameAnalytics, machineLearningClient::putDataFrameAnalyticsAsync); + machineLearningClient::putDataFrameAnalytics, + machineLearningClient::putDataFrameAnalyticsAsync + ); DataFrameAnalyticsConfig createdConfig = putDataFrameAnalyticsResponse.getConfig(); assertThat(createdConfig.getId(), equalTo(config.getId())); assertThat(createdConfig.getSource().getIndex(), equalTo(config.getSource().getIndex())); @@ -1450,11 +1576,14 @@ public void testUpdateDataFrameAnalytics() throws Exception { machineLearningClient.putDataFrameAnalytics(new PutDataFrameAnalyticsRequest(config), RequestOptions.DEFAULT); - UpdateDataFrameAnalyticsRequest request = - new UpdateDataFrameAnalyticsRequest( - DataFrameAnalyticsConfigUpdate.builder().setId(config.getId()).setDescription("Updated description").build()); - PutDataFrameAnalyticsResponse response = - execute(request, machineLearningClient::updateDataFrameAnalytics, machineLearningClient::updateDataFrameAnalyticsAsync); + UpdateDataFrameAnalyticsRequest request = new UpdateDataFrameAnalyticsRequest( + DataFrameAnalyticsConfigUpdate.builder().setId(config.getId()).setDescription("Updated description").build() + ); + PutDataFrameAnalyticsResponse response = execute( + request, + machineLearningClient::updateDataFrameAnalytics, + machineLearningClient::updateDataFrameAnalyticsAsync + ); assertThat(response.getConfig().getDescription(), equalTo("Updated description")); GetDataFrameAnalyticsRequest getRequest = new GetDataFrameAnalyticsRequest(config.getId()); @@ -1467,12 +1596,8 @@ public void testGetDataFrameAnalyticsConfig_SingleConfig() throws Exception { String configId = "get-test-config"; DataFrameAnalyticsConfig config = DataFrameAnalyticsConfig.builder() .setId(configId) - .setSource(DataFrameAnalyticsSource.builder() - .setIndex("get-test-source-index") - .build()) - .setDest(DataFrameAnalyticsDest.builder() - .setIndex("get-test-dest-index") - .build()) + .setSource(DataFrameAnalyticsSource.builder().setIndex("get-test-source-index").build()) + .setDest(DataFrameAnalyticsDest.builder().setIndex("get-test-dest-index").build()) .setAnalysis(org.elasticsearch.client.ml.dataframe.OutlierDetection.createDefault()) .build(); @@ -1480,12 +1605,16 @@ public void testGetDataFrameAnalyticsConfig_SingleConfig() throws Exception { PutDataFrameAnalyticsResponse putDataFrameAnalyticsResponse = execute( new PutDataFrameAnalyticsRequest(config), - machineLearningClient::putDataFrameAnalytics, machineLearningClient::putDataFrameAnalyticsAsync); + machineLearningClient::putDataFrameAnalytics, + machineLearningClient::putDataFrameAnalyticsAsync + ); DataFrameAnalyticsConfig createdConfig = putDataFrameAnalyticsResponse.getConfig(); GetDataFrameAnalyticsResponse getDataFrameAnalyticsResponse = execute( new GetDataFrameAnalyticsRequest(configId), - machineLearningClient::getDataFrameAnalytics, machineLearningClient::getDataFrameAnalyticsAsync); + machineLearningClient::getDataFrameAnalytics, + machineLearningClient::getDataFrameAnalyticsAsync + ); assertThat(getDataFrameAnalyticsResponse.getAnalytics(), hasSize(1)); assertThat(getDataFrameAnalyticsResponse.getAnalytics(), contains(createdConfig)); } @@ -1501,18 +1630,16 @@ public void testGetDataFrameAnalyticsConfig_MultipleConfigs() throws Exception { String configId = configIdPrefix + i; DataFrameAnalyticsConfig config = DataFrameAnalyticsConfig.builder() .setId(configId) - .setSource(DataFrameAnalyticsSource.builder() - .setIndex("get-test-source-index") - .build()) - .setDest(DataFrameAnalyticsDest.builder() - .setIndex("get-test-dest-index") - .build()) + .setSource(DataFrameAnalyticsSource.builder().setIndex("get-test-source-index").build()) + .setDest(DataFrameAnalyticsDest.builder().setIndex("get-test-dest-index").build()) .setAnalysis(org.elasticsearch.client.ml.dataframe.OutlierDetection.createDefault()) .build(); PutDataFrameAnalyticsResponse putDataFrameAnalyticsResponse = execute( new PutDataFrameAnalyticsRequest(config), - machineLearningClient::putDataFrameAnalytics, machineLearningClient::putDataFrameAnalyticsAsync); + machineLearningClient::putDataFrameAnalytics, + machineLearningClient::putDataFrameAnalyticsAsync + ); DataFrameAnalyticsConfig createdConfig = putDataFrameAnalyticsResponse.getConfig(); createdConfigs.add(createdConfig); } @@ -1520,44 +1647,56 @@ public void testGetDataFrameAnalyticsConfig_MultipleConfigs() throws Exception { { GetDataFrameAnalyticsResponse getDataFrameAnalyticsResponse = execute( GetDataFrameAnalyticsRequest.getAllDataFrameAnalyticsRequest(), - machineLearningClient::getDataFrameAnalytics, machineLearningClient::getDataFrameAnalyticsAsync); + machineLearningClient::getDataFrameAnalytics, + machineLearningClient::getDataFrameAnalyticsAsync + ); assertThat(getDataFrameAnalyticsResponse.getAnalytics(), hasSize(numberOfConfigs)); assertThat(getDataFrameAnalyticsResponse.getAnalytics(), containsInAnyOrder(createdConfigs.toArray())); } { GetDataFrameAnalyticsResponse getDataFrameAnalyticsResponse = execute( new GetDataFrameAnalyticsRequest(configIdPrefix + "*"), - machineLearningClient::getDataFrameAnalytics, machineLearningClient::getDataFrameAnalyticsAsync); + machineLearningClient::getDataFrameAnalytics, + machineLearningClient::getDataFrameAnalyticsAsync + ); assertThat(getDataFrameAnalyticsResponse.getAnalytics(), hasSize(numberOfConfigs)); assertThat(getDataFrameAnalyticsResponse.getAnalytics(), containsInAnyOrder(createdConfigs.toArray())); } { GetDataFrameAnalyticsResponse getDataFrameAnalyticsResponse = execute( new GetDataFrameAnalyticsRequest(configIdPrefix + "9", configIdPrefix + "1", configIdPrefix + "4"), - machineLearningClient::getDataFrameAnalytics, machineLearningClient::getDataFrameAnalyticsAsync); + machineLearningClient::getDataFrameAnalytics, + machineLearningClient::getDataFrameAnalyticsAsync + ); assertThat(getDataFrameAnalyticsResponse.getAnalytics(), hasSize(3)); assertThat( getDataFrameAnalyticsResponse.getAnalytics(), - containsInAnyOrder(createdConfigs.get(1), createdConfigs.get(4), createdConfigs.get(9))); + containsInAnyOrder(createdConfigs.get(1), createdConfigs.get(4), createdConfigs.get(9)) + ); } { GetDataFrameAnalyticsRequest getDataFrameAnalyticsRequest = new GetDataFrameAnalyticsRequest(configIdPrefix + "*"); getDataFrameAnalyticsRequest.setPageParams(new PageParams(3, 4)); GetDataFrameAnalyticsResponse getDataFrameAnalyticsResponse = execute( getDataFrameAnalyticsRequest, - machineLearningClient::getDataFrameAnalytics, machineLearningClient::getDataFrameAnalyticsAsync); + machineLearningClient::getDataFrameAnalytics, + machineLearningClient::getDataFrameAnalyticsAsync + ); assertThat(getDataFrameAnalyticsResponse.getAnalytics(), hasSize(4)); assertThat( getDataFrameAnalyticsResponse.getAnalytics(), - containsInAnyOrder(createdConfigs.get(3), createdConfigs.get(4), createdConfigs.get(5), createdConfigs.get(6))); + containsInAnyOrder(createdConfigs.get(3), createdConfigs.get(4), createdConfigs.get(5), createdConfigs.get(6)) + ); } } public void testGetDataFrameAnalyticsConfig_ConfigNotFound() { MachineLearningClient machineLearningClient = highLevelClient().machineLearning(); GetDataFrameAnalyticsRequest request = new GetDataFrameAnalyticsRequest("config_that_does_not_exist"); - ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, - () -> execute(request, machineLearningClient::getDataFrameAnalytics, machineLearningClient::getDataFrameAnalyticsAsync)); + ElasticsearchStatusException exception = expectThrows( + ElasticsearchStatusException.class, + () -> execute(request, machineLearningClient::getDataFrameAnalytics, machineLearningClient::getDataFrameAnalyticsAsync) + ); assertThat(exception.status().getStatus(), equalTo(404)); } @@ -1571,22 +1710,22 @@ public void testGetDataFrameAnalyticsStats() throws Exception { String configId = "get-stats-test-config"; DataFrameAnalyticsConfig config = DataFrameAnalyticsConfig.builder() .setId(configId) - .setSource(DataFrameAnalyticsSource.builder() - .setIndex(sourceIndex) - .build()) - .setDest(DataFrameAnalyticsDest.builder() - .setIndex(destIndex) - .build()) + .setSource(DataFrameAnalyticsSource.builder().setIndex(sourceIndex).build()) + .setDest(DataFrameAnalyticsDest.builder().setIndex(destIndex).build()) .setAnalysis(org.elasticsearch.client.ml.dataframe.OutlierDetection.createDefault()) .build(); execute( new PutDataFrameAnalyticsRequest(config), - machineLearningClient::putDataFrameAnalytics, machineLearningClient::putDataFrameAnalyticsAsync); + machineLearningClient::putDataFrameAnalytics, + machineLearningClient::putDataFrameAnalyticsAsync + ); GetDataFrameAnalyticsStatsResponse statsResponse = execute( new GetDataFrameAnalyticsStatsRequest(configId), - machineLearningClient::getDataFrameAnalyticsStats, machineLearningClient::getDataFrameAnalyticsStatsAsync); + machineLearningClient::getDataFrameAnalyticsStats, + machineLearningClient::getDataFrameAnalyticsStatsAsync + ); assertThat(statsResponse.getAnalyticsStats(), hasSize(1)); DataFrameAnalyticsStats stats = statsResponse.getAnalyticsStats().get(0); @@ -1614,8 +1753,10 @@ public void testStartDataFrameAnalyticsConfig() throws Exception { String sourceIndex = "start-test-source-index"; String destIndex = "start-test-dest-index"; createIndex(sourceIndex, defaultMappingForTest()); - highLevelClient().index(new IndexRequest(sourceIndex).source(XContentType.JSON, "total", 10000) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT); + highLevelClient().index( + new IndexRequest(sourceIndex).source(XContentType.JSON, "total", 10000).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), + RequestOptions.DEFAULT + ); // Verify that the destination index does not exist. Otherwise, analytics' reindexing step would fail. assertFalse(highLevelClient().indices().exists(new GetIndexRequest(destIndex), RequestOptions.DEFAULT)); @@ -1624,23 +1765,23 @@ public void testStartDataFrameAnalyticsConfig() throws Exception { String configId = "start-test-config"; DataFrameAnalyticsConfig config = DataFrameAnalyticsConfig.builder() .setId(configId) - .setSource(DataFrameAnalyticsSource.builder() - .setIndex(sourceIndex) - .build()) - .setDest(DataFrameAnalyticsDest.builder() - .setIndex(destIndex) - .build()) + .setSource(DataFrameAnalyticsSource.builder().setIndex(sourceIndex).build()) + .setDest(DataFrameAnalyticsDest.builder().setIndex(destIndex).build()) .setAnalysis(org.elasticsearch.client.ml.dataframe.OutlierDetection.createDefault()) .build(); execute( new PutDataFrameAnalyticsRequest(config), - machineLearningClient::putDataFrameAnalytics, machineLearningClient::putDataFrameAnalyticsAsync); + machineLearningClient::putDataFrameAnalytics, + machineLearningClient::putDataFrameAnalyticsAsync + ); assertThat(getAnalyticsState(configId), equalTo(DataFrameAnalyticsState.STOPPED)); AcknowledgedResponse startDataFrameAnalyticsResponse = execute( new StartDataFrameAnalyticsRequest(configId), - machineLearningClient::startDataFrameAnalytics, machineLearningClient::startDataFrameAnalyticsAsync); + machineLearningClient::startDataFrameAnalytics, + machineLearningClient::startDataFrameAnalyticsAsync + ); assertTrue(startDataFrameAnalyticsResponse.isAcknowledged()); // Wait for the analytics to stop. @@ -1655,8 +1796,10 @@ public void testStopDataFrameAnalyticsConfig() throws Exception { String sourceIndex = "stop-test-source-index"; String destIndex = "stop-test-dest-index"; createIndex(sourceIndex, defaultMappingForTest()); - highLevelClient().index(new IndexRequest(sourceIndex).source(XContentType.JSON, "total", 10000) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT); + highLevelClient().index( + new IndexRequest(sourceIndex).source(XContentType.JSON, "total", 10000).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), + RequestOptions.DEFAULT + ); // Verify that the destination index does not exist. Otherwise, analytics' reindexing step would fail. assertFalse(highLevelClient().indices().exists(new GetIndexRequest(destIndex), RequestOptions.DEFAULT)); @@ -1665,38 +1808,48 @@ public void testStopDataFrameAnalyticsConfig() throws Exception { String configId = "stop-test-config"; DataFrameAnalyticsConfig config = DataFrameAnalyticsConfig.builder() .setId(configId) - .setSource(DataFrameAnalyticsSource.builder() - .setIndex(sourceIndex) - .build()) - .setDest(DataFrameAnalyticsDest.builder() - .setIndex(destIndex) - .build()) + .setSource(DataFrameAnalyticsSource.builder().setIndex(sourceIndex).build()) + .setDest(DataFrameAnalyticsDest.builder().setIndex(destIndex).build()) .setAnalysis(org.elasticsearch.client.ml.dataframe.OutlierDetection.createDefault()) .build(); execute( new PutDataFrameAnalyticsRequest(config), - machineLearningClient::putDataFrameAnalytics, machineLearningClient::putDataFrameAnalyticsAsync); + machineLearningClient::putDataFrameAnalytics, + machineLearningClient::putDataFrameAnalyticsAsync + ); assertThat(getAnalyticsState(configId), equalTo(DataFrameAnalyticsState.STOPPED)); AcknowledgedResponse startDataFrameAnalyticsResponse = execute( new StartDataFrameAnalyticsRequest(configId), - machineLearningClient::startDataFrameAnalytics, machineLearningClient::startDataFrameAnalyticsAsync); + machineLearningClient::startDataFrameAnalytics, + machineLearningClient::startDataFrameAnalyticsAsync + ); assertTrue(startDataFrameAnalyticsResponse.isAcknowledged()); - assertThat(getAnalyticsState(configId), anyOf(equalTo(DataFrameAnalyticsState.STARTED), - equalTo(DataFrameAnalyticsState.REINDEXING), equalTo(DataFrameAnalyticsState.ANALYZING))); + assertThat( + getAnalyticsState(configId), + anyOf( + equalTo(DataFrameAnalyticsState.STARTED), + equalTo(DataFrameAnalyticsState.REINDEXING), + equalTo(DataFrameAnalyticsState.ANALYZING) + ) + ); StopDataFrameAnalyticsResponse stopDataFrameAnalyticsResponse = execute( new StopDataFrameAnalyticsRequest(configId), - machineLearningClient::stopDataFrameAnalytics, machineLearningClient::stopDataFrameAnalyticsAsync); + machineLearningClient::stopDataFrameAnalytics, + machineLearningClient::stopDataFrameAnalyticsAsync + ); assertTrue(stopDataFrameAnalyticsResponse.isStopped()); assertThat(getAnalyticsState(configId), equalTo(DataFrameAnalyticsState.STOPPED)); } private DataFrameAnalyticsState getAnalyticsState(String configId) throws IOException { MachineLearningClient machineLearningClient = highLevelClient().machineLearning(); - GetDataFrameAnalyticsStatsResponse statsResponse = - machineLearningClient.getDataFrameAnalyticsStats(new GetDataFrameAnalyticsStatsRequest(configId), RequestOptions.DEFAULT); + GetDataFrameAnalyticsStatsResponse statsResponse = machineLearningClient.getDataFrameAnalyticsStats( + new GetDataFrameAnalyticsStatsRequest(configId), + RequestOptions.DEFAULT + ); assertThat(statsResponse.getAnalyticsStats(), hasSize(1)); DataFrameAnalyticsStats stats = statsResponse.getAnalyticsStats().get(0); return stats.getState(); @@ -1707,12 +1860,8 @@ public void testDeleteDataFrameAnalyticsConfig() throws Exception { String configId = "delete-test-config"; DataFrameAnalyticsConfig config = DataFrameAnalyticsConfig.builder() .setId(configId) - .setSource(DataFrameAnalyticsSource.builder() - .setIndex("delete-test-source-index") - .build()) - .setDest(DataFrameAnalyticsDest.builder() - .setIndex("delete-test-dest-index") - .build()) + .setSource(DataFrameAnalyticsSource.builder().setIndex("delete-test-source-index").build()) + .setDest(DataFrameAnalyticsDest.builder().setIndex("delete-test-dest-index").build()) .setAnalysis(org.elasticsearch.client.ml.dataframe.OutlierDetection.createDefault()) .build(); @@ -1720,46 +1869,57 @@ public void testDeleteDataFrameAnalyticsConfig() throws Exception { GetDataFrameAnalyticsResponse getDataFrameAnalyticsResponse = execute( new GetDataFrameAnalyticsRequest(configId + "*"), - machineLearningClient::getDataFrameAnalytics, machineLearningClient::getDataFrameAnalyticsAsync); + machineLearningClient::getDataFrameAnalytics, + machineLearningClient::getDataFrameAnalyticsAsync + ); assertThat(getDataFrameAnalyticsResponse.getAnalytics(), hasSize(0)); execute( new PutDataFrameAnalyticsRequest(config), - machineLearningClient::putDataFrameAnalytics, machineLearningClient::putDataFrameAnalyticsAsync); + machineLearningClient::putDataFrameAnalytics, + machineLearningClient::putDataFrameAnalyticsAsync + ); getDataFrameAnalyticsResponse = execute( new GetDataFrameAnalyticsRequest(configId + "*"), - machineLearningClient::getDataFrameAnalytics, machineLearningClient::getDataFrameAnalyticsAsync); + machineLearningClient::getDataFrameAnalytics, + machineLearningClient::getDataFrameAnalyticsAsync + ); assertThat(getDataFrameAnalyticsResponse.getAnalytics(), hasSize(1)); DeleteDataFrameAnalyticsRequest deleteRequest = new DeleteDataFrameAnalyticsRequest(configId); if (randomBoolean()) { deleteRequest.setForce(randomBoolean()); } - AcknowledgedResponse deleteDataFrameAnalyticsResponse = execute(deleteRequest, - machineLearningClient::deleteDataFrameAnalytics, machineLearningClient::deleteDataFrameAnalyticsAsync); + AcknowledgedResponse deleteDataFrameAnalyticsResponse = execute( + deleteRequest, + machineLearningClient::deleteDataFrameAnalytics, + machineLearningClient::deleteDataFrameAnalyticsAsync + ); assertTrue(deleteDataFrameAnalyticsResponse.isAcknowledged()); getDataFrameAnalyticsResponse = execute( new GetDataFrameAnalyticsRequest(configId + "*"), - machineLearningClient::getDataFrameAnalytics, machineLearningClient::getDataFrameAnalyticsAsync); + machineLearningClient::getDataFrameAnalytics, + machineLearningClient::getDataFrameAnalyticsAsync + ); assertThat(getDataFrameAnalyticsResponse.getAnalytics(), hasSize(0)); } public void testDeleteDataFrameAnalyticsConfig_ConfigNotFound() { MachineLearningClient machineLearningClient = highLevelClient().machineLearning(); DeleteDataFrameAnalyticsRequest request = new DeleteDataFrameAnalyticsRequest("config_that_does_not_exist"); - ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, - () -> execute( - request, machineLearningClient::deleteDataFrameAnalytics, machineLearningClient::deleteDataFrameAnalyticsAsync)); + ElasticsearchStatusException exception = expectThrows( + ElasticsearchStatusException.class, + () -> execute(request, machineLearningClient::deleteDataFrameAnalytics, machineLearningClient::deleteDataFrameAnalyticsAsync) + ); assertThat(exception.status().getStatus(), equalTo(404)); } public void testEvaluateDataFrame_OutlierDetection() throws IOException { String indexName = "evaluate-test-index"; createIndex(indexName, mappingForOutlierDetection()); - BulkRequest bulk = new BulkRequest() - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + BulkRequest bulk = new BulkRequest().setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) .add(docForOutlierDetection(indexName, "blue", false, 0.1)) // #0 .add(docForOutlierDetection(indexName, "blue", false, 0.2)) // #1 .add(docForOutlierDetection(indexName, "blue", false, 0.3)) // #2 @@ -1773,29 +1933,33 @@ public void testEvaluateDataFrame_OutlierDetection() throws IOException { highLevelClient().bulk(bulk, RequestOptions.DEFAULT); MachineLearningClient machineLearningClient = highLevelClient().machineLearning(); - EvaluateDataFrameRequest evaluateDataFrameRequest = - new EvaluateDataFrameRequest( - indexName, - null, - new OutlierDetection( - actualField, - probabilityField, - org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.PrecisionMetric.at(0.4, 0.5, 0.6), - org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.RecallMetric.at(0.5, 0.7), - ConfusionMatrixMetric.at(0.5), - org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.AucRocMetric.withCurve())); - - EvaluateDataFrameResponse evaluateDataFrameResponse = - execute(evaluateDataFrameRequest, machineLearningClient::evaluateDataFrame, machineLearningClient::evaluateDataFrameAsync); + EvaluateDataFrameRequest evaluateDataFrameRequest = new EvaluateDataFrameRequest( + indexName, + null, + new OutlierDetection( + actualField, + probabilityField, + org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.PrecisionMetric.at(0.4, 0.5, 0.6), + org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.RecallMetric.at(0.5, 0.7), + ConfusionMatrixMetric.at(0.5), + org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.AucRocMetric.withCurve() + ) + ); + + EvaluateDataFrameResponse evaluateDataFrameResponse = execute( + evaluateDataFrameRequest, + machineLearningClient::evaluateDataFrame, + machineLearningClient::evaluateDataFrameAsync + ); assertThat(evaluateDataFrameResponse.getEvaluationName(), equalTo(OutlierDetection.NAME)); assertThat(evaluateDataFrameResponse.getMetrics().size(), equalTo(4)); - org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.PrecisionMetric.Result precisionResult = - evaluateDataFrameResponse.getMetricByName( - org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.PrecisionMetric.NAME); + org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.PrecisionMetric.Result precisionResult = evaluateDataFrameResponse + .getMetricByName(org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.PrecisionMetric.NAME); assertThat( precisionResult.getMetricName(), - equalTo(org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.PrecisionMetric.NAME)); + equalTo(org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.PrecisionMetric.NAME) + ); // Precision is 3/5=0.6 as there were 3 true examples (#7, #8, #9) among the 5 positive examples (#3, #4, #7, #8, #9) assertThat(precisionResult.getScoreByThreshold("0.4"), closeTo(0.6, 1e-9)); // Precision is 2/3=0.(6) as there were 2 true examples (#8, #9) among the 3 positive examples (#4, #8, #9) @@ -1804,11 +1968,12 @@ public void testEvaluateDataFrame_OutlierDetection() throws IOException { assertThat(precisionResult.getScoreByThreshold("0.6"), closeTo(0.666666666, 1e-9)); assertNull(precisionResult.getScoreByThreshold("0.1")); - org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.RecallMetric.Result recallResult = - evaluateDataFrameResponse.getMetricByName(org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.RecallMetric.NAME); + org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.RecallMetric.Result recallResult = evaluateDataFrameResponse + .getMetricByName(org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.RecallMetric.NAME); assertThat( recallResult.getMetricName(), - equalTo(org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.RecallMetric.NAME)); + equalTo(org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.RecallMetric.NAME) + ); // Recall is 2/5=0.4 as there were 2 true positive examples (#8, #9) among the 5 true examples (#5, #6, #7, #8, #9) assertThat(recallResult.getScoreByThreshold("0.5"), closeTo(0.4, 1e-9)); // Recall is 2/5=0.4 as there were 2 true positive examples (#8, #9) among the 5 true examples (#5, #6, #7, #8, #9) @@ -1824,8 +1989,9 @@ public void testEvaluateDataFrame_OutlierDetection() throws IOException { assertThat(confusionMatrix.getFalseNegatives(), equalTo(3L)); // docs #5, #6 and #7 assertNull(confusionMatrixResult.getScoreByThreshold("0.1")); - AucRocResult aucRocResult = - evaluateDataFrameResponse.getMetricByName(org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.AucRocMetric.NAME); + AucRocResult aucRocResult = evaluateDataFrameResponse.getMetricByName( + org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.AucRocMetric.NAME + ); assertThat(aucRocResult.getMetricName(), equalTo(AucRocMetric.NAME)); assertThat(aucRocResult.getValue(), closeTo(0.70, 1e-3)); assertNotNull(aucRocResult.getCurve()); @@ -1843,8 +2009,7 @@ public void testEvaluateDataFrame_OutlierDetection() throws IOException { public void testEvaluateDataFrame_OutlierDetection_WithQuery() throws IOException { String indexName = "evaluate-with-query-test-index"; createIndex(indexName, mappingForOutlierDetection()); - BulkRequest bulk = new BulkRequest() - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + BulkRequest bulk = new BulkRequest().setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) .add(docForOutlierDetection(indexName, "blue", true, 1.0)) // #0 .add(docForOutlierDetection(indexName, "blue", true, 1.0)) // #1 .add(docForOutlierDetection(indexName, "blue", true, 1.0)) // #2 @@ -1858,15 +2023,18 @@ public void testEvaluateDataFrame_OutlierDetection_WithQuery() throws IOExceptio highLevelClient().bulk(bulk, RequestOptions.DEFAULT); MachineLearningClient machineLearningClient = highLevelClient().machineLearning(); - EvaluateDataFrameRequest evaluateDataFrameRequest = - new EvaluateDataFrameRequest( - indexName, - // Request only "blue" subset to be evaluated - new QueryConfig(QueryBuilders.termQuery(datasetField, "blue")), - new OutlierDetection(actualField, probabilityField, ConfusionMatrixMetric.at(0.5))); + EvaluateDataFrameRequest evaluateDataFrameRequest = new EvaluateDataFrameRequest( + indexName, + // Request only "blue" subset to be evaluated + new QueryConfig(QueryBuilders.termQuery(datasetField, "blue")), + new OutlierDetection(actualField, probabilityField, ConfusionMatrixMetric.at(0.5)) + ); - EvaluateDataFrameResponse evaluateDataFrameResponse = - execute(evaluateDataFrameRequest, machineLearningClient::evaluateDataFrame, machineLearningClient::evaluateDataFrameAsync); + EvaluateDataFrameResponse evaluateDataFrameResponse = execute( + evaluateDataFrameRequest, + machineLearningClient::evaluateDataFrame, + machineLearningClient::evaluateDataFrameAsync + ); assertThat(evaluateDataFrameResponse.getEvaluationName(), equalTo(OutlierDetection.NAME)); assertThat(evaluateDataFrameResponse.getMetrics().size(), equalTo(1)); @@ -1882,8 +2050,7 @@ public void testEvaluateDataFrame_OutlierDetection_WithQuery() throws IOExceptio public void testEvaluateDataFrame_Regression() throws IOException { String regressionIndex = "evaluate-regression-test-index"; createIndex(regressionIndex, mappingForRegression()); - BulkRequest regressionBulk = new BulkRequest() - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + BulkRequest regressionBulk = new BulkRequest().setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) .add(docForRegression(regressionIndex, 0.3, 0.1)) // #0 .add(docForRegression(regressionIndex, 0.3, 0.2)) // #1 .add(docForRegression(regressionIndex, 0.3, 0.3)) // #2 @@ -1897,20 +2064,24 @@ public void testEvaluateDataFrame_Regression() throws IOException { highLevelClient().bulk(regressionBulk, RequestOptions.DEFAULT); MachineLearningClient machineLearningClient = highLevelClient().machineLearning(); - EvaluateDataFrameRequest evaluateDataFrameRequest = - new EvaluateDataFrameRequest( - regressionIndex, - null, - new Regression( - actualRegression, - predictedRegression, - new MeanSquaredErrorMetric(), - new MeanSquaredLogarithmicErrorMetric(1.0), - new HuberMetric(1.0), - new RSquaredMetric())); - - EvaluateDataFrameResponse evaluateDataFrameResponse = - execute(evaluateDataFrameRequest, machineLearningClient::evaluateDataFrame, machineLearningClient::evaluateDataFrameAsync); + EvaluateDataFrameRequest evaluateDataFrameRequest = new EvaluateDataFrameRequest( + regressionIndex, + null, + new Regression( + actualRegression, + predictedRegression, + new MeanSquaredErrorMetric(), + new MeanSquaredLogarithmicErrorMetric(1.0), + new HuberMetric(1.0), + new RSquaredMetric() + ) + ); + + EvaluateDataFrameResponse evaluateDataFrameResponse = execute( + evaluateDataFrameRequest, + machineLearningClient::evaluateDataFrame, + machineLearningClient::evaluateDataFrameAsync + ); assertThat(evaluateDataFrameResponse.getEvaluationName(), equalTo(Regression.NAME)); assertThat(evaluateDataFrameResponse.getMetrics().size(), equalTo(4)); @@ -1918,8 +2089,9 @@ public void testEvaluateDataFrame_Regression() throws IOException { assertThat(mseResult.getMetricName(), equalTo(MeanSquaredErrorMetric.NAME)); assertThat(mseResult.getValue(), closeTo(0.061000000, 1e-9)); - MeanSquaredLogarithmicErrorMetric.Result msleResult = - evaluateDataFrameResponse.getMetricByName(MeanSquaredLogarithmicErrorMetric.NAME); + MeanSquaredLogarithmicErrorMetric.Result msleResult = evaluateDataFrameResponse.getMetricByName( + MeanSquaredLogarithmicErrorMetric.NAME + ); assertThat(msleResult.getMetricName(), equalTo(MeanSquaredLogarithmicErrorMetric.NAME)); assertThat(msleResult.getValue(), closeTo(0.02759231770210426, 1e-9)); @@ -1935,8 +2107,7 @@ public void testEvaluateDataFrame_Regression() throws IOException { public void testEvaluateDataFrame_Classification() throws IOException { String indexName = "evaluate-classification-test-index"; createIndex(indexName, mappingForClassification()); - BulkRequest regressionBulk = new BulkRequest() - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + BulkRequest regressionBulk = new BulkRequest().setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) .add(docForClassification(indexName, "cat", "cat", "dog", "ant")) .add(docForClassification(indexName, "cat", "cat", "dog", "ant")) .add(docForClassification(indexName, "cat", "cat", "horse", "dog")) @@ -1951,12 +2122,17 @@ public void testEvaluateDataFrame_Classification() throws IOException { MachineLearningClient machineLearningClient = highLevelClient().machineLearning(); { // AucRoc - EvaluateDataFrameRequest evaluateDataFrameRequest = - new EvaluateDataFrameRequest( - indexName, null, new Classification(actualClassField, null, topClassesField, AucRocMetric.forClassWithCurve("cat"))); - - EvaluateDataFrameResponse evaluateDataFrameResponse = - execute(evaluateDataFrameRequest, machineLearningClient::evaluateDataFrame, machineLearningClient::evaluateDataFrameAsync); + EvaluateDataFrameRequest evaluateDataFrameRequest = new EvaluateDataFrameRequest( + indexName, + null, + new Classification(actualClassField, null, topClassesField, AucRocMetric.forClassWithCurve("cat")) + ); + + EvaluateDataFrameResponse evaluateDataFrameResponse = execute( + evaluateDataFrameRequest, + machineLearningClient::evaluateDataFrame, + machineLearningClient::evaluateDataFrameAsync + ); assertThat(evaluateDataFrameResponse.getEvaluationName(), equalTo(Classification.NAME)); assertThat(evaluateDataFrameResponse.getMetrics().size(), equalTo(1)); @@ -1966,12 +2142,17 @@ public void testEvaluateDataFrame_Classification() throws IOException { assertNotNull(aucRocResult.getCurve()); } { // Accuracy - EvaluateDataFrameRequest evaluateDataFrameRequest = - new EvaluateDataFrameRequest( - indexName, null, new Classification(actualClassField, predictedClassField, null, new AccuracyMetric())); - - EvaluateDataFrameResponse evaluateDataFrameResponse = - execute(evaluateDataFrameRequest, machineLearningClient::evaluateDataFrame, machineLearningClient::evaluateDataFrameAsync); + EvaluateDataFrameRequest evaluateDataFrameRequest = new EvaluateDataFrameRequest( + indexName, + null, + new Classification(actualClassField, predictedClassField, null, new AccuracyMetric()) + ); + + EvaluateDataFrameResponse evaluateDataFrameResponse = execute( + evaluateDataFrameRequest, + machineLearningClient::evaluateDataFrame, + machineLearningClient::evaluateDataFrameAsync + ); assertThat(evaluateDataFrameResponse.getEvaluationName(), equalTo(Classification.NAME)); assertThat(evaluateDataFrameResponse.getMetrics().size(), equalTo(1)); @@ -1986,16 +2167,24 @@ public void testEvaluateDataFrame_Classification() throws IOException { // 6 out of 10 examples were classified correctly new PerClassSingleValue("cat", 0.6), // 8 out of 10 examples were classified correctly - new PerClassSingleValue("dog", 0.8)))); + new PerClassSingleValue("dog", 0.8) + ) + ) + ); assertThat(accuracyResult.getOverallAccuracy(), equalTo(0.6)); // 6 out of 10 examples were classified correctly } { // Precision - EvaluateDataFrameRequest evaluateDataFrameRequest = - new EvaluateDataFrameRequest( - indexName, null, new Classification(actualClassField, predictedClassField, null, new PrecisionMetric())); - - EvaluateDataFrameResponse evaluateDataFrameResponse = - execute(evaluateDataFrameRequest, machineLearningClient::evaluateDataFrame, machineLearningClient::evaluateDataFrameAsync); + EvaluateDataFrameRequest evaluateDataFrameRequest = new EvaluateDataFrameRequest( + indexName, + null, + new Classification(actualClassField, predictedClassField, null, new PrecisionMetric()) + ); + + EvaluateDataFrameResponse evaluateDataFrameResponse = execute( + evaluateDataFrameRequest, + machineLearningClient::evaluateDataFrame, + machineLearningClient::evaluateDataFrameAsync + ); assertThat(evaluateDataFrameResponse.getEvaluationName(), equalTo(Classification.NAME)); assertThat(evaluateDataFrameResponse.getMetrics().size(), equalTo(1)); @@ -2008,16 +2197,24 @@ public void testEvaluateDataFrame_Classification() throws IOException { // 3 out of 5 examples labeled as "cat" were classified correctly new PerClassSingleValue("cat", 0.6), // 3 out of 4 examples labeled as "dog" were classified correctly - new PerClassSingleValue("dog", 0.75)))); + new PerClassSingleValue("dog", 0.75) + ) + ) + ); assertThat(precisionResult.getAvgPrecision(), equalTo(0.675)); } { // Recall - EvaluateDataFrameRequest evaluateDataFrameRequest = - new EvaluateDataFrameRequest( - indexName, null, new Classification(actualClassField, predictedClassField, null, new RecallMetric())); - - EvaluateDataFrameResponse evaluateDataFrameResponse = - execute(evaluateDataFrameRequest, machineLearningClient::evaluateDataFrame, machineLearningClient::evaluateDataFrameAsync); + EvaluateDataFrameRequest evaluateDataFrameRequest = new EvaluateDataFrameRequest( + indexName, + null, + new Classification(actualClassField, predictedClassField, null, new RecallMetric()) + ); + + EvaluateDataFrameResponse evaluateDataFrameResponse = execute( + evaluateDataFrameRequest, + machineLearningClient::evaluateDataFrame, + machineLearningClient::evaluateDataFrameAsync + ); assertThat(evaluateDataFrameResponse.getEvaluationName(), equalTo(Classification.NAME)); assertThat(evaluateDataFrameResponse.getMetrics().size(), equalTo(1)); @@ -2032,23 +2229,30 @@ public void testEvaluateDataFrame_Classification() throws IOException { // 3 out of 4 examples labeled as "dog" were classified correctly new PerClassSingleValue("dog", 0.75), // no examples labeled as "ant" were classified correctly - new PerClassSingleValue("ant", 0.0)))); + new PerClassSingleValue("ant", 0.0) + ) + ) + ); assertThat(recallResult.getAvgRecall(), equalTo(0.45)); } { // No size provided for MulticlassConfusionMatrixMetric, default used instead - EvaluateDataFrameRequest evaluateDataFrameRequest = - new EvaluateDataFrameRequest( - indexName, - null, - new Classification(actualClassField, predictedClassField, null, new MulticlassConfusionMatrixMetric())); - - EvaluateDataFrameResponse evaluateDataFrameResponse = - execute(evaluateDataFrameRequest, machineLearningClient::evaluateDataFrame, machineLearningClient::evaluateDataFrameAsync); + EvaluateDataFrameRequest evaluateDataFrameRequest = new EvaluateDataFrameRequest( + indexName, + null, + new Classification(actualClassField, predictedClassField, null, new MulticlassConfusionMatrixMetric()) + ); + + EvaluateDataFrameResponse evaluateDataFrameResponse = execute( + evaluateDataFrameRequest, + machineLearningClient::evaluateDataFrame, + machineLearningClient::evaluateDataFrameAsync + ); assertThat(evaluateDataFrameResponse.getEvaluationName(), equalTo(Classification.NAME)); assertThat(evaluateDataFrameResponse.getMetrics().size(), equalTo(1)); - MulticlassConfusionMatrixMetric.Result mcmResult = - evaluateDataFrameResponse.getMetricByName(MulticlassConfusionMatrixMetric.NAME); + MulticlassConfusionMatrixMetric.Result mcmResult = evaluateDataFrameResponse.getMetricByName( + MulticlassConfusionMatrixMetric.NAME + ); assertThat(mcmResult.getMetricName(), equalTo(MulticlassConfusionMatrixMetric.NAME)); assertThat( mcmResult.getConfusionMatrix(), @@ -2060,40 +2264,53 @@ public void testEvaluateDataFrame_Classification() throws IOException { List.of( new MulticlassConfusionMatrixMetric.PredictedClass("ant", 0L), new MulticlassConfusionMatrixMetric.PredictedClass("cat", 1L), - new MulticlassConfusionMatrixMetric.PredictedClass("dog", 0L)), - 0L), + new MulticlassConfusionMatrixMetric.PredictedClass("dog", 0L) + ), + 0L + ), new MulticlassConfusionMatrixMetric.ActualClass( "cat", 5L, List.of( new MulticlassConfusionMatrixMetric.PredictedClass("ant", 0L), new MulticlassConfusionMatrixMetric.PredictedClass("cat", 3L), - new MulticlassConfusionMatrixMetric.PredictedClass("dog", 1L)), - 1L), + new MulticlassConfusionMatrixMetric.PredictedClass("dog", 1L) + ), + 1L + ), new MulticlassConfusionMatrixMetric.ActualClass( "dog", 4L, List.of( new MulticlassConfusionMatrixMetric.PredictedClass("ant", 0L), new MulticlassConfusionMatrixMetric.PredictedClass("cat", 1L), - new MulticlassConfusionMatrixMetric.PredictedClass("dog", 3L)), - 0L)))); + new MulticlassConfusionMatrixMetric.PredictedClass("dog", 3L) + ), + 0L + ) + ) + ) + ); assertThat(mcmResult.getOtherActualClassCount(), equalTo(0L)); } { // Explicit size provided for MulticlassConfusionMatrixMetric metric - EvaluateDataFrameRequest evaluateDataFrameRequest = - new EvaluateDataFrameRequest( - indexName, - null, - new Classification(actualClassField, predictedClassField, null, new MulticlassConfusionMatrixMetric(2))); - - EvaluateDataFrameResponse evaluateDataFrameResponse = - execute(evaluateDataFrameRequest, machineLearningClient::evaluateDataFrame, machineLearningClient::evaluateDataFrameAsync); + EvaluateDataFrameRequest evaluateDataFrameRequest = new EvaluateDataFrameRequest( + indexName, + null, + new Classification(actualClassField, predictedClassField, null, new MulticlassConfusionMatrixMetric(2)) + ); + + EvaluateDataFrameResponse evaluateDataFrameResponse = execute( + evaluateDataFrameRequest, + machineLearningClient::evaluateDataFrame, + machineLearningClient::evaluateDataFrameAsync + ); assertThat(evaluateDataFrameResponse.getEvaluationName(), equalTo(Classification.NAME)); assertThat(evaluateDataFrameResponse.getMetrics().size(), equalTo(1)); - MulticlassConfusionMatrixMetric.Result mcmResult = - evaluateDataFrameResponse.getMetricByName(MulticlassConfusionMatrixMetric.NAME); + MulticlassConfusionMatrixMetric.Result mcmResult = evaluateDataFrameResponse.getMetricByName( + MulticlassConfusionMatrixMetric.NAME + ); assertThat(mcmResult.getMetricName(), equalTo(MulticlassConfusionMatrixMetric.NAME)); assertThat( mcmResult.getConfusionMatrix(), @@ -2104,31 +2321,38 @@ public void testEvaluateDataFrame_Classification() throws IOException { 5L, List.of( new MulticlassConfusionMatrixMetric.PredictedClass("cat", 3L), - new MulticlassConfusionMatrixMetric.PredictedClass("dog", 1L)), - 1L), + new MulticlassConfusionMatrixMetric.PredictedClass("dog", 1L) + ), + 1L + ), new MulticlassConfusionMatrixMetric.ActualClass( "dog", 4L, List.of( new MulticlassConfusionMatrixMetric.PredictedClass("cat", 1L), - new MulticlassConfusionMatrixMetric.PredictedClass("dog", 3L)), - 0L) - ))); + new MulticlassConfusionMatrixMetric.PredictedClass("dog", 3L) + ), + 0L + ) + ) + ) + ); assertThat(mcmResult.getOtherActualClassCount(), equalTo(1L)); } } private static XContentBuilder defaultMappingForTest() throws IOException { - return XContentFactory.jsonBuilder().startObject() + return XContentFactory.jsonBuilder() + .startObject() .startObject("properties") - .startObject("timestamp") - .field("type", "date") - .endObject() - .startObject("total") - .field("type", "long") - .endObject() + .startObject("timestamp") + .field("type", "date") + .endObject() + .startObject("total") + .field("type", "long") + .endObject() .endObject() - .endObject(); + .endObject(); } private static final String datasetField = "dataset"; @@ -2136,24 +2360,24 @@ private static XContentBuilder defaultMappingForTest() throws IOException { private static final String probabilityField = "p"; private static XContentBuilder mappingForOutlierDetection() throws IOException { - return XContentFactory.jsonBuilder().startObject() + return XContentFactory.jsonBuilder() + .startObject() .startObject("properties") - .startObject(datasetField) - .field("type", "keyword") - .endObject() - .startObject(actualField) - .field("type", "keyword") - .endObject() - .startObject(probabilityField) - .field("type", "double") - .endObject() + .startObject(datasetField) + .field("type", "keyword") .endObject() - .endObject(); + .startObject(actualField) + .field("type", "keyword") + .endObject() + .startObject(probabilityField) + .field("type", "double") + .endObject() + .endObject() + .endObject(); } private static IndexRequest docForOutlierDetection(String indexName, String dataset, boolean isTrue, double p) { - return new IndexRequest() - .index(indexName) + return new IndexRequest().index(indexName) .source(XContentType.JSON, datasetField, dataset, actualField, Boolean.toString(isTrue), probabilityField, p); } @@ -2162,55 +2386,58 @@ private static IndexRequest docForOutlierDetection(String indexName, String data private static final String topClassesField = "top_classes"; private static XContentBuilder mappingForClassification() throws IOException { - return XContentFactory.jsonBuilder().startObject() + return XContentFactory.jsonBuilder() + .startObject() .startObject("properties") - .startObject(actualClassField) - .field("type", "keyword") - .endObject() - .startObject(predictedClassField) - .field("type", "keyword") - .endObject() - .startObject(topClassesField) - .field("type", "nested") - .endObject() + .startObject(actualClassField) + .field("type", "keyword") + .endObject() + .startObject(predictedClassField) + .field("type", "keyword") + .endObject() + .startObject(topClassesField) + .field("type", "nested") + .endObject() .endObject() - .endObject(); + .endObject(); } - private static IndexRequest docForClassification(String indexName, - String actualClass, - String... topPredictedClasses) { + private static IndexRequest docForClassification(String indexName, String actualClass, String... topPredictedClasses) { assert topPredictedClasses.length > 0; - return new IndexRequest() - .index(indexName) - .source(XContentType.JSON, - actualClassField, actualClass, - predictedClassField, topPredictedClasses[0], - topClassesField, IntStream.range(0, topPredictedClasses.length) + return new IndexRequest().index(indexName) + .source( + XContentType.JSON, + actualClassField, + actualClass, + predictedClassField, + topPredictedClasses[0], + topClassesField, + IntStream.range(0, topPredictedClasses.length) // Consecutive assigned probabilities are: 0.5, 0.25, 0.125, etc. .mapToObj(i -> Map.of("class_name", topPredictedClasses[i], "class_probability", 1.0 / (2 << i))) - .collect(Collectors.toList())); + .collect(Collectors.toList()) + ); } private static final String actualRegression = "regression_actual"; private static final String predictedRegression = "regression_predicted"; private static XContentBuilder mappingForRegression() throws IOException { - return XContentFactory.jsonBuilder().startObject() + return XContentFactory.jsonBuilder() + .startObject() .startObject("properties") - .startObject(actualRegression) - .field("type", "double") - .endObject() - .startObject(predictedRegression) - .field("type", "double") - .endObject() + .startObject(actualRegression) + .field("type", "double") .endObject() - .endObject(); + .startObject(predictedRegression) + .field("type", "double") + .endObject() + .endObject() + .endObject(); } private static IndexRequest docForRegression(String indexName, double actualValue, double predictedValue) { - return new IndexRequest() - .index(indexName) + return new IndexRequest().index(indexName) .source(XContentType.JSON, actualRegression, actualValue, predictedRegression, predictedValue); } @@ -2221,28 +2448,30 @@ private void createIndex(String indexName, XContentBuilder mapping) throws IOExc public void testExplainDataFrameAnalytics() throws IOException { String indexName = "explain-df-test-index"; createIndex(indexName, mappingForOutlierDetection()); - BulkRequest bulk1 = new BulkRequest() - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); + BulkRequest bulk1 = new BulkRequest().setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); for (int i = 0; i < 10; ++i) { bulk1.add(docForOutlierDetection(indexName, randomAlphaOfLength(10), randomBoolean(), randomDoubleBetween(0.0, 1.0, true))); } highLevelClient().bulk(bulk1, RequestOptions.DEFAULT); MachineLearningClient machineLearningClient = highLevelClient().machineLearning(); - ExplainDataFrameAnalyticsRequest explainRequest = - new ExplainDataFrameAnalyticsRequest( - DataFrameAnalyticsConfig.builder() - .setSource(DataFrameAnalyticsSource.builder().setIndex(indexName).build()) - .setAnalysis(org.elasticsearch.client.ml.dataframe.OutlierDetection.createDefault()) - .build()); + ExplainDataFrameAnalyticsRequest explainRequest = new ExplainDataFrameAnalyticsRequest( + DataFrameAnalyticsConfig.builder() + .setSource(DataFrameAnalyticsSource.builder().setIndex(indexName).build()) + .setAnalysis(org.elasticsearch.client.ml.dataframe.OutlierDetection.createDefault()) + .build() + ); // We are pretty liberal here as this test does not aim at verifying concrete numbers but rather end-to-end user workflow. ByteSizeValue lowerBound = new ByteSizeValue(1, ByteSizeUnit.KB); ByteSizeValue upperBound = new ByteSizeValue(1, ByteSizeUnit.GB); // Data Frame has 10 rows, expect that the returned estimates fall within (1kB, 1GB) range. - ExplainDataFrameAnalyticsResponse response1 = execute(explainRequest, machineLearningClient::explainDataFrameAnalytics, - machineLearningClient::explainDataFrameAnalyticsAsync); + ExplainDataFrameAnalyticsResponse response1 = execute( + explainRequest, + machineLearningClient::explainDataFrameAnalytics, + machineLearningClient::explainDataFrameAnalyticsAsync + ); MemoryEstimation memoryEstimation1 = response1.getMemoryEstimation(); assertThat(memoryEstimation1.getExpectedMemoryWithoutDisk(), allOf(greaterThanOrEqualTo(lowerBound), lessThan(upperBound))); @@ -2252,24 +2481,27 @@ public void testExplainDataFrameAnalytics() throws IOException { assertThat(fieldSelection.size(), equalTo(3)); assertThat(fieldSelection.stream().map(FieldSelection::getName).collect(Collectors.toList()), contains("dataset", "label", "p")); - BulkRequest bulk2 = new BulkRequest() - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); + BulkRequest bulk2 = new BulkRequest().setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); for (int i = 10; i < 100; ++i) { bulk2.add(docForOutlierDetection(indexName, randomAlphaOfLength(10), randomBoolean(), randomDoubleBetween(0.0, 1.0, true))); } highLevelClient().bulk(bulk2, RequestOptions.DEFAULT); // Data Frame now has 100 rows, expect that the returned estimates will be greater than or equal to the previous ones. - ExplainDataFrameAnalyticsResponse response2 = - execute( - explainRequest, machineLearningClient::explainDataFrameAnalytics, machineLearningClient::explainDataFrameAnalyticsAsync); + ExplainDataFrameAnalyticsResponse response2 = execute( + explainRequest, + machineLearningClient::explainDataFrameAnalytics, + machineLearningClient::explainDataFrameAnalyticsAsync + ); MemoryEstimation memoryEstimation2 = response2.getMemoryEstimation(); assertThat( memoryEstimation2.getExpectedMemoryWithoutDisk(), - allOf(greaterThanOrEqualTo(memoryEstimation1.getExpectedMemoryWithoutDisk()), lessThan(upperBound))); + allOf(greaterThanOrEqualTo(memoryEstimation1.getExpectedMemoryWithoutDisk()), lessThan(upperBound)) + ); assertThat( memoryEstimation2.getExpectedMemoryWithDisk(), - allOf(greaterThanOrEqualTo(memoryEstimation1.getExpectedMemoryWithDisk()), lessThan(upperBound))); + allOf(greaterThanOrEqualTo(memoryEstimation1.getExpectedMemoryWithDisk()), lessThan(upperBound)) + ); } public void testGetTrainedModels() throws Exception { @@ -2283,12 +2515,12 @@ public void testGetTrainedModels() throws Exception { { GetTrainedModelsResponse getTrainedModelsResponse = execute( - new GetTrainedModelsRequest(modelIdPrefix + 0) - .setDecompressDefinition(true) + new GetTrainedModelsRequest(modelIdPrefix + 0).setDecompressDefinition(true) .includeDefinition() .includeTotalFeatureImportance(), machineLearningClient::getTrainedModels, - machineLearningClient::getTrainedModelsAsync); + machineLearningClient::getTrainedModelsAsync + ); assertThat(getTrainedModelsResponse.getCount(), equalTo(1L)); assertThat(getTrainedModelsResponse.getTrainedModels(), hasSize(1)); @@ -2297,12 +2529,12 @@ public void testGetTrainedModels() throws Exception { assertThat(getTrainedModelsResponse.getTrainedModels().get(0).getModelId(), equalTo(modelIdPrefix + 0)); getTrainedModelsResponse = execute( - new GetTrainedModelsRequest(modelIdPrefix + 0) - .setDecompressDefinition(false) + new GetTrainedModelsRequest(modelIdPrefix + 0).setDecompressDefinition(false) .includeTotalFeatureImportance() .includeDefinition(), machineLearningClient::getTrainedModels, - machineLearningClient::getTrainedModelsAsync); + machineLearningClient::getTrainedModelsAsync + ); assertThat(getTrainedModelsResponse.getCount(), equalTo(1L)); assertThat(getTrainedModelsResponse.getTrainedModels(), hasSize(1)); @@ -2311,10 +2543,10 @@ public void testGetTrainedModels() throws Exception { assertThat(getTrainedModelsResponse.getTrainedModels().get(0).getModelId(), equalTo(modelIdPrefix + 0)); getTrainedModelsResponse = execute( - new GetTrainedModelsRequest(modelIdPrefix + 0) - .setDecompressDefinition(false), + new GetTrainedModelsRequest(modelIdPrefix + 0).setDecompressDefinition(false), machineLearningClient::getTrainedModels, - machineLearningClient::getTrainedModelsAsync); + machineLearningClient::getTrainedModelsAsync + ); assertThat(getTrainedModelsResponse.getCount(), equalTo(1L)); assertThat(getTrainedModelsResponse.getTrainedModels(), hasSize(1)); assertThat(getTrainedModelsResponse.getTrainedModels().get(0).getCompressedDefinition(), is(nullValue())); @@ -2325,26 +2557,33 @@ public void testGetTrainedModels() throws Exception { { GetTrainedModelsResponse getTrainedModelsResponse = execute( GetTrainedModelsRequest.getAllTrainedModelConfigsRequest(), - machineLearningClient::getTrainedModels, machineLearningClient::getTrainedModelsAsync); + machineLearningClient::getTrainedModels, + machineLearningClient::getTrainedModelsAsync + ); assertThat(getTrainedModelsResponse.getTrainedModels(), hasSize(numberOfModels + 1)); assertThat(getTrainedModelsResponse.getCount(), equalTo(5L + 1)); } { GetTrainedModelsResponse getTrainedModelsResponse = execute( new GetTrainedModelsRequest(modelIdPrefix + 4, modelIdPrefix + 2, modelIdPrefix + 3), - machineLearningClient::getTrainedModels, machineLearningClient::getTrainedModelsAsync); + machineLearningClient::getTrainedModels, + machineLearningClient::getTrainedModelsAsync + ); assertThat(getTrainedModelsResponse.getTrainedModels(), hasSize(3)); assertThat(getTrainedModelsResponse.getCount(), equalTo(3L)); } { GetTrainedModelsResponse getTrainedModelsResponse = execute( new GetTrainedModelsRequest(modelIdPrefix + "*").setPageParams(new PageParams(1, 2)), - machineLearningClient::getTrainedModels, machineLearningClient::getTrainedModelsAsync); + machineLearningClient::getTrainedModels, + machineLearningClient::getTrainedModelsAsync + ); assertThat(getTrainedModelsResponse.getTrainedModels(), hasSize(2)); assertThat(getTrainedModelsResponse.getCount(), equalTo(5L)); assertThat( getTrainedModelsResponse.getTrainedModels().stream().map(TrainedModelConfig::getModelId).collect(Collectors.toList()), - containsInAnyOrder(modelIdPrefix + 1, modelIdPrefix + 2)); + containsInAnyOrder(modelIdPrefix + 1, modelIdPrefix + 2) + ); } } @@ -2362,9 +2601,11 @@ public void testPutTrainedModel() throws Exception { .setInput(new TrainedModelInput(Arrays.asList("col1", "col2", "col3", "col4"))) .setDescription("test model") .build(); - PutTrainedModelResponse putTrainedModelResponse = execute(new PutTrainedModelRequest(trainedModelConfig), + PutTrainedModelResponse putTrainedModelResponse = execute( + new PutTrainedModelRequest(trainedModelConfig), machineLearningClient::putTrainedModel, - machineLearningClient::putTrainedModelAsync); + machineLearningClient::putTrainedModelAsync + ); TrainedModelConfig createdModel = putTrainedModelResponse.getResponse(); assertThat(createdModel.getModelId(), equalTo(modelId)); @@ -2376,16 +2617,19 @@ public void testPutTrainedModel() throws Exception { .setInput(new TrainedModelInput(Arrays.asList("col1", "col2", "col3", "col4"))) .setDescription("test model") .build(); - putTrainedModelResponse = execute(new PutTrainedModelRequest(trainedModelConfig), + putTrainedModelResponse = execute( + new PutTrainedModelRequest(trainedModelConfig), machineLearningClient::putTrainedModel, - machineLearningClient::putTrainedModelAsync); + machineLearningClient::putTrainedModelAsync + ); createdModel = putTrainedModelResponse.getResponse(); assertThat(createdModel.getModelId(), equalTo(modelIdCompressed)); GetTrainedModelsResponse getTrainedModelsResponse = execute( new GetTrainedModelsRequest(modelIdCompressed).setDecompressDefinition(true).includeDefinition(), machineLearningClient::getTrainedModels, - machineLearningClient::getTrainedModelsAsync); + machineLearningClient::getTrainedModelsAsync + ); assertThat(getTrainedModelsResponse.getCount(), equalTo(1L)); assertThat(getTrainedModelsResponse.getTrainedModels(), hasSize(1)); @@ -2411,7 +2655,8 @@ public void testPutTrainedModelAlias() throws Exception { GetTrainedModelsResponse getTrainedModelsResponse = execute( new GetTrainedModelsRequest("my-first-alias"), machineLearningClient::getTrainedModels, - machineLearningClient::getTrainedModelsAsync); + machineLearningClient::getTrainedModelsAsync + ); assertThat(getTrainedModelsResponse.getCount(), equalTo(1L)); assertThat(getTrainedModelsResponse.getTrainedModels(), hasSize(1)); @@ -2450,7 +2695,8 @@ public void testDeleteTrainedModelAlias() throws Exception { GetTrainedModelsResponse getTrainedModelsResponse = execute( new GetTrainedModelsRequest("my-first-deleted-alias"), machineLearningClient::getTrainedModels, - machineLearningClient::getTrainedModelsAsync); + machineLearningClient::getTrainedModelsAsync + ); assertThat(getTrainedModelsResponse.getCount(), equalTo(1L)); assertThat(getTrainedModelsResponse.getTrainedModels(), hasSize(1)); @@ -2462,12 +2708,14 @@ public void testDeleteTrainedModelAlias() throws Exception { machineLearningClient::deleteTrainedModelAliasAsync ); assertThat(acknowledgedResponse.isAcknowledged(), is(true)); - ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, + ElasticsearchStatusException exception = expectThrows( + ElasticsearchStatusException.class, () -> execute( new GetTrainedModelsRequest("my-first-deleted-alias"), machineLearningClient::getTrainedModels, machineLearningClient::getTrainedModelsAsync - )); + ) + ); assertThat(exception.status().getStatus(), equalTo(404)); } @@ -2480,28 +2728,31 @@ public void testGetTrainedModelsStats() throws Exception { putTrainedModel(modelId); } - String regressionPipeline = "{" + - " \"processors\": [\n" + - " {\n" + - " \"inference\": {\n" + - " \"target_field\": \"regression_value\",\n" + - " \"model_id\": \"" + modelIdPrefix + 0 + "\",\n" + - " \"inference_config\": {\"regression\": {}},\n" + - " \"field_map\": {\n" + - " \"col1\": \"col1\",\n" + - " \"col2\": \"col2\",\n" + - " \"col3\": \"col3\",\n" + - " \"col4\": \"col4\"\n" + - " }\n" + - " }\n" + - " }]}\n"; + String regressionPipeline = "{" + + " \"processors\": [\n" + + " {\n" + + " \"inference\": {\n" + + " \"target_field\": \"regression_value\",\n" + + " \"model_id\": \"" + + modelIdPrefix + + 0 + + "\",\n" + + " \"inference_config\": {\"regression\": {}},\n" + + " \"field_map\": {\n" + + " \"col1\": \"col1\",\n" + + " \"col2\": \"col2\",\n" + + " \"col3\": \"col3\",\n" + + " \"col4\": \"col4\"\n" + + " }\n" + + " }\n" + + " }]}\n"; String pipelineId = "regression-stats-pipeline"; - highLevelClient().ingest().putPipeline( - new PutPipelineRequest(pipelineId, - new BytesArray(regressionPipeline.getBytes(StandardCharsets.UTF_8)), - XContentType.JSON), - RequestOptions.DEFAULT); + highLevelClient().ingest() + .putPipeline( + new PutPipelineRequest(pipelineId, new BytesArray(regressionPipeline.getBytes(StandardCharsets.UTF_8)), XContentType.JSON), + RequestOptions.DEFAULT + ); highLevelClient().index( new IndexRequest("trained-models-stats-test-index").source("{\"col1\": 1}", XContentType.JSON).setPipeline(pipelineId), RequestOptions.DEFAULT @@ -2509,7 +2760,9 @@ public void testGetTrainedModelsStats() throws Exception { { GetTrainedModelsStatsResponse getTrainedModelsStatsResponse = execute( GetTrainedModelsStatsRequest.getAllTrainedModelStatsRequest(), - machineLearningClient::getTrainedModelsStats, machineLearningClient::getTrainedModelsStatsAsync); + machineLearningClient::getTrainedModelsStats, + machineLearningClient::getTrainedModelsStatsAsync + ); assertThat(getTrainedModelsStatsResponse.getTrainedModelStats(), hasSize(numberOfModels + 1)); assertThat(getTrainedModelsStatsResponse.getCount(), equalTo(5L + 1)); assertThat(getTrainedModelsStatsResponse.getTrainedModelStats().get(0).getPipelineCount(), equalTo(1)); @@ -2518,14 +2771,18 @@ public void testGetTrainedModelsStats() throws Exception { { GetTrainedModelsStatsResponse getTrainedModelsStatsResponse = execute( new GetTrainedModelsStatsRequest(modelIdPrefix + 4, modelIdPrefix + 2, modelIdPrefix + 3), - machineLearningClient::getTrainedModelsStats, machineLearningClient::getTrainedModelsStatsAsync); + machineLearningClient::getTrainedModelsStats, + machineLearningClient::getTrainedModelsStatsAsync + ); assertThat(getTrainedModelsStatsResponse.getTrainedModelStats(), hasSize(3)); assertThat(getTrainedModelsStatsResponse.getCount(), equalTo(3L)); } { GetTrainedModelsStatsResponse getTrainedModelsStatsResponse = execute( new GetTrainedModelsStatsRequest(modelIdPrefix + "*").setPageParams(new PageParams(1, 2)), - machineLearningClient::getTrainedModelsStats, machineLearningClient::getTrainedModelsStatsAsync); + machineLearningClient::getTrainedModelsStats, + machineLearningClient::getTrainedModelsStatsAsync + ); assertThat(getTrainedModelsStatsResponse.getTrainedModelStats(), hasSize(2)); assertThat(getTrainedModelsStatsResponse.getCount(), equalTo(5L)); assertThat( @@ -2533,7 +2790,8 @@ public void testGetTrainedModelsStats() throws Exception { .stream() .map(TrainedModelStats::getModelId) .collect(Collectors.toList()), - containsInAnyOrder(modelIdPrefix + 1, modelIdPrefix + 2)); + containsInAnyOrder(modelIdPrefix + 1, modelIdPrefix + 2) + ); } highLevelClient().ingest().deletePipeline(new DeletePipelineRequest(pipelineId), RequestOptions.DEFAULT); assertBusy(() -> { @@ -2550,20 +2808,24 @@ public void testDeleteTrainedModel() throws Exception { GetTrainedModelsResponse getTrainedModelsResponse = execute( new GetTrainedModelsRequest(modelId + "*").setAllowNoMatch(true), machineLearningClient::getTrainedModels, - machineLearningClient::getTrainedModelsAsync); + machineLearningClient::getTrainedModelsAsync + ); assertThat(getTrainedModelsResponse.getCount(), equalTo(1L)); assertThat(getTrainedModelsResponse.getTrainedModels(), hasSize(1)); AcknowledgedResponse deleteTrainedModelResponse = execute( new DeleteTrainedModelRequest(modelId), - machineLearningClient::deleteTrainedModel, machineLearningClient::deleteTrainedModelAsync); + machineLearningClient::deleteTrainedModel, + machineLearningClient::deleteTrainedModelAsync + ); assertTrue(deleteTrainedModelResponse.isAcknowledged()); getTrainedModelsResponse = execute( new GetTrainedModelsRequest(modelId + "*").setAllowNoMatch(true), machineLearningClient::getTrainedModels, - machineLearningClient::getTrainedModelsAsync); + machineLearningClient::getTrainedModelsAsync + ); assertThat(getTrainedModelsResponse.getCount(), equalTo(0L)); assertThat(getTrainedModelsResponse.getTrainedModels(), hasSize(0)); @@ -2575,13 +2837,16 @@ public void testGetPrepackagedModels() throws Exception { GetTrainedModelsResponse getTrainedModelsResponse = execute( new GetTrainedModelsRequest("lang_ident_model_1").includeDefinition(), machineLearningClient::getTrainedModels, - machineLearningClient::getTrainedModelsAsync); + machineLearningClient::getTrainedModelsAsync + ); assertThat(getTrainedModelsResponse.getCount(), equalTo(1L)); assertThat(getTrainedModelsResponse.getTrainedModels(), hasSize(1)); assertThat(getTrainedModelsResponse.getTrainedModels().get(0).getModelId(), equalTo("lang_ident_model_1")); - assertThat(getTrainedModelsResponse.getTrainedModels().get(0).getDefinition().getTrainedModel(), - instanceOf(LangIdentNeuralNetwork.class)); + assertThat( + getTrainedModelsResponse.getTrainedModels().get(0).getDefinition().getTrainedModel(), + instanceOf(LangIdentNeuralNetwork.class) + ); } public void testPutFilter() throws Exception { @@ -2592,9 +2857,11 @@ public void testPutFilter() throws Exception { .build(); MachineLearningClient machineLearningClient = highLevelClient().machineLearning(); - PutFilterResponse putFilterResponse = execute(new PutFilterRequest(mlFilter), + PutFilterResponse putFilterResponse = execute( + new PutFilterRequest(mlFilter), machineLearningClient::putFilter, - machineLearningClient::putFilterAsync); + machineLearningClient::putFilterAsync + ); MlFilter createdFilter = putFilterResponse.getResponse(); assertThat(createdFilter, equalTo(mlFilter)); @@ -2625,9 +2892,11 @@ public void testGetFilters() throws Exception { GetFiltersRequest getFiltersRequest = new GetFiltersRequest(); getFiltersRequest.setFilterId(filterId1); - GetFiltersResponse getFiltersResponse = execute(getFiltersRequest, + GetFiltersResponse getFiltersResponse = execute( + getFiltersRequest, machineLearningClient::getFilter, - machineLearningClient::getFilterAsync); + machineLearningClient::getFilterAsync + ); assertThat(getFiltersResponse.count(), equalTo(1L)); assertThat(getFiltersResponse.filters().get(0), equalTo(mlFilter1)); } @@ -2637,13 +2906,17 @@ public void testGetFilters() throws Exception { getFiltersRequest.setFrom(1); getFiltersRequest.setSize(2); - GetFiltersResponse getFiltersResponse = execute(getFiltersRequest, + GetFiltersResponse getFiltersResponse = execute( + getFiltersRequest, machineLearningClient::getFilter, - machineLearningClient::getFilterAsync); + machineLearningClient::getFilterAsync + ); assertThat(getFiltersResponse.count(), equalTo(3L)); assertThat(getFiltersResponse.filters().size(), equalTo(2)); - assertThat(getFiltersResponse.filters().stream().map(MlFilter::getId).collect(Collectors.toList()), - containsInAnyOrder("get-filter-test-2", "get-filter-test-3")); + assertThat( + getFiltersResponse.filters().stream().map(MlFilter::getId).collect(Collectors.toList()), + containsInAnyOrder("get-filter-test-2", "get-filter-test-3") + ); } } @@ -2661,9 +2934,8 @@ public void testUpdateFilter() throws Exception { updateFilterRequest.setAddItems(Arrays.asList("newItem1", "newItem2")); updateFilterRequest.setRemoveItems(Collections.singletonList("olditem1")); updateFilterRequest.setDescription("new description"); - MlFilter filter = execute(updateFilterRequest, - machineLearningClient::updateFilter, - machineLearningClient::updateFilterAsync).getResponse(); + MlFilter filter = execute(updateFilterRequest, machineLearningClient::updateFilter, machineLearningClient::updateFilterAsync) + .getResponse(); assertThat(filter.getDescription(), equalTo(updateFilterRequest.getDescription())); assertThat(filter.getItems(), contains("newItem1", "newItem2", "olditem2")); @@ -2677,21 +2949,27 @@ public void testDeleteFilter() throws Exception { .build(); MachineLearningClient machineLearningClient = highLevelClient().machineLearning(); - PutFilterResponse putFilterResponse = execute(new PutFilterRequest(mlFilter), + PutFilterResponse putFilterResponse = execute( + new PutFilterRequest(mlFilter), machineLearningClient::putFilter, - machineLearningClient::putFilterAsync); + machineLearningClient::putFilterAsync + ); MlFilter createdFilter = putFilterResponse.getResponse(); assertThat(createdFilter, equalTo(mlFilter)); DeleteFilterRequest deleteFilterRequest = new DeleteFilterRequest(filterId); - AcknowledgedResponse response = execute(deleteFilterRequest, machineLearningClient::deleteFilter, - machineLearningClient::deleteFilterAsync); + AcknowledgedResponse response = execute( + deleteFilterRequest, + machineLearningClient::deleteFilter, + machineLearningClient::deleteFilterAsync + ); assertTrue(response.isAcknowledged()); - ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, - () -> execute(deleteFilterRequest, machineLearningClient::deleteFilter, - machineLearningClient::deleteFilterAsync)); + ElasticsearchStatusException exception = expectThrows( + ElasticsearchStatusException.class, + () -> execute(deleteFilterRequest, machineLearningClient::deleteFilter, machineLearningClient::deleteFilterAsync) + ); assertThat(exception.status().getStatus(), equalTo(404)); } @@ -2714,12 +2992,9 @@ private static Job buildJobForExpiredDataTests(String jobId) { Job.Builder builder = new Job.Builder(jobId); builder.setDescription(randomAlphaOfLength(10)); - Detector detector = new Detector.Builder() - .setFunction("count") - .setDetectorDescription(randomAlphaOfLength(10)) - .build(); + Detector detector = new Detector.Builder().setFunction("count").setDetectorDescription(randomAlphaOfLength(10)).build(); AnalysisConfig.Builder configBuilder = new AnalysisConfig.Builder(Collections.singletonList(detector)); - //should not be random, see:https://github.com/elastic/ml-cpp/issues/208 + // should not be random, see:https://github.com/elastic/ml-cpp/issues/208 configBuilder.setBucketSpan(new TimeValue(1, TimeUnit.HOURS)); builder.setAnalysisConfig(configBuilder); builder.setModelSnapshotRetentionDays(1L); @@ -2737,13 +3012,12 @@ public static Job buildJob(String jobId) { Job.Builder builder = new Job.Builder(jobId); builder.setDescription(randomAlphaOfLength(10)); - Detector detector = new Detector.Builder() - .setFieldName("total") + Detector detector = new Detector.Builder().setFieldName("total") .setFunction("sum") .setDetectorDescription(randomAlphaOfLength(10)) .build(); AnalysisConfig.Builder configBuilder = new AnalysisConfig.Builder(Arrays.asList(detector)); - //should not be random, see:https://github.com/elastic/ml-cpp/issues/208 + // should not be random, see:https://github.com/elastic/ml-cpp/issues/208 configBuilder.setBucketSpan(new TimeValue(5, TimeUnit.SECONDS)); builder.setAnalysisConfig(configBuilder); builder.setAnalysisLimits(new AnalysisLimits(512L, 4L)); @@ -2791,9 +3065,11 @@ private void startDatafeed(String datafeedId, String start, String end) throws E StartDatafeedRequest startDatafeedRequest = new StartDatafeedRequest(datafeedId); startDatafeedRequest.setStart(start); startDatafeedRequest.setEnd(end); - StartDatafeedResponse response = execute(startDatafeedRequest, + StartDatafeedResponse response = execute( + startDatafeedRequest, machineLearningClient::startDatafeed, - machineLearningClient::startDatafeedAsync); + machineLearningClient::startDatafeedAsync + ); assertTrue(response.isStarted()); } @@ -2803,8 +3079,11 @@ private void updateModelSnapshotTimestamp(String jobId, String timestamp) throws MachineLearningClient machineLearningClient = highLevelClient().machineLearning(); GetModelSnapshotsRequest getModelSnapshotsRequest = new GetModelSnapshotsRequest(jobId); - GetModelSnapshotsResponse getModelSnapshotsResponse = execute(getModelSnapshotsRequest, machineLearningClient::getModelSnapshots, - machineLearningClient::getModelSnapshotsAsync); + GetModelSnapshotsResponse getModelSnapshotsResponse = execute( + getModelSnapshotsRequest, + machineLearningClient::getModelSnapshots, + machineLearningClient::getModelSnapshotsAsync + ); assertThat(getModelSnapshotsResponse.count(), greaterThanOrEqualTo(1L)); @@ -2824,7 +3103,8 @@ private String createAndPutDatafeed(String jobId, String indexName) throws IOExc DatafeedConfig datafeed = DatafeedConfig.builder(datafeedId, jobId) .setIndices(indexName) .setQueryDelay(TimeValue.timeValueSeconds(1)) - .setFrequency(TimeValue.timeValueSeconds(1)).build(); + .setFrequency(TimeValue.timeValueSeconds(1)) + .build(); highLevelClient().machineLearning().putDatafeed(new PutDatafeedRequest(datafeed), RequestOptions.DEFAULT); return datafeedId; } @@ -2836,38 +3116,58 @@ public void createModelSnapshot(String jobId, String snapshotId) throws IOExcept IndexRequest indexRequest = new IndexRequest(".ml-anomalies-shared").id(documentId); indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - indexRequest.source("{\"job_id\":\"" + jobId + "\", \"timestamp\":1541587919000, " + - "\"description\":\"State persisted due to job close at 2018-11-07T10:51:59+0000\", " + - "\"snapshot_id\":\"" + snapshotId + "\", \"snapshot_doc_count\":1, \"model_size_stats\":{" + - "\"job_id\":\"" + jobId + "\", \"result_type\":\"model_size_stats\",\"model_bytes\":51722, " + - "\"total_by_field_count\":3, \"total_over_field_count\":0, \"total_partition_field_count\":2," + - "\"bucket_allocation_failures_count\":0, \"memory_status\":\"ok\", \"log_time\":1541587919000, " + - "\"timestamp\":1519930800000}, \"latest_record_time_stamp\":1519931700000," + - "\"latest_result_time_stamp\":1519930800000, \"retain\":false, \"min_version\":\"" + Version.CURRENT.toString() + "\"}", - XContentType.JSON); + indexRequest.source( + "{\"job_id\":\"" + + jobId + + "\", \"timestamp\":1541587919000, " + + "\"description\":\"State persisted due to job close at 2018-11-07T10:51:59+0000\", " + + "\"snapshot_id\":\"" + + snapshotId + + "\", \"snapshot_doc_count\":1, \"model_size_stats\":{" + + "\"job_id\":\"" + + jobId + + "\", \"result_type\":\"model_size_stats\",\"model_bytes\":51722, " + + "\"total_by_field_count\":3, \"total_over_field_count\":0, \"total_partition_field_count\":2," + + "\"bucket_allocation_failures_count\":0, \"memory_status\":\"ok\", \"log_time\":1541587919000, " + + "\"timestamp\":1519930800000}, \"latest_record_time_stamp\":1519931700000," + + "\"latest_result_time_stamp\":1519930800000, \"retain\":false, \"min_version\":\"" + + Version.CURRENT.toString() + + "\"}", + XContentType.JSON + ); highLevelClient().index(indexRequest, RequestOptions.DEFAULT); } - public void createModelSnapshots(String jobId, List snapshotIds) throws IOException { Job job = MachineLearningIT.buildJob(jobId); highLevelClient().machineLearning().putJob(new PutJobRequest(job), RequestOptions.DEFAULT); - for(String snapshotId : snapshotIds) { + for (String snapshotId : snapshotIds) { String documentId = jobId + "_model_snapshot_" + snapshotId; IndexRequest indexRequest = new IndexRequest(".ml-anomalies-shared").id(documentId); indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - indexRequest.source("{\"job_id\":\"" + jobId + "\", \"timestamp\":1541587919000, " + - "\"description\":\"State persisted due to job close at 2018-11-07T10:51:59+0000\", " + - "\"snapshot_id\":\"" + snapshotId + "\", \"snapshot_doc_count\":1, \"model_size_stats\":{" + - "\"job_id\":\"" + jobId + "\", \"result_type\":\"model_size_stats\",\"model_bytes\":51722, " + - "\"total_by_field_count\":3, \"total_over_field_count\":0, \"total_partition_field_count\":2," + - "\"bucket_allocation_failures_count\":0, \"memory_status\":\"ok\", \"log_time\":1541587919000, " + - "\"timestamp\":1519930800000}, \"latest_record_time_stamp\":1519931700000," + - "\"latest_result_time_stamp\":1519930800000, \"retain\":false, " + - "\"quantiles\":{\"job_id\":\""+jobId+"\", \"timestamp\":1541587919000, " + - "\"quantile_state\":\"state\"}}", XContentType.JSON); + indexRequest.source( + "{\"job_id\":\"" + + jobId + + "\", \"timestamp\":1541587919000, " + + "\"description\":\"State persisted due to job close at 2018-11-07T10:51:59+0000\", " + + "\"snapshot_id\":\"" + + snapshotId + + "\", \"snapshot_doc_count\":1, \"model_size_stats\":{" + + "\"job_id\":\"" + + jobId + + "\", \"result_type\":\"model_size_stats\",\"model_bytes\":51722, " + + "\"total_by_field_count\":3, \"total_over_field_count\":0, \"total_partition_field_count\":2," + + "\"bucket_allocation_failures_count\":0, \"memory_status\":\"ok\", \"log_time\":1541587919000, " + + "\"timestamp\":1519930800000}, \"latest_record_time_stamp\":1519931700000," + + "\"latest_result_time_stamp\":1519930800000, \"retain\":false, " + + "\"quantiles\":{\"job_id\":\"" + + jobId + + "\", \"timestamp\":1541587919000, " + + "\"quantile_state\":\"state\"}}", + XContentType.JSON + ); highLevelClient().index(indexRequest, RequestOptions.DEFAULT); } } @@ -2882,8 +3182,11 @@ public void testDeleteModelSnapshot() throws IOException { DeleteModelSnapshotRequest request = new DeleteModelSnapshotRequest(jobId, snapshotId); - AcknowledgedResponse response = execute(request, machineLearningClient::deleteModelSnapshot, - machineLearningClient::deleteModelSnapshotAsync); + AcknowledgedResponse response = execute( + request, + machineLearningClient::deleteModelSnapshot, + machineLearningClient::deleteModelSnapshotAsync + ); assertTrue(response.isAcknowledged()); } @@ -2898,30 +3201,40 @@ public void testUpdateModelSnapshot() throws Exception { GetModelSnapshotsRequest getModelSnapshotsRequest = new GetModelSnapshotsRequest(jobId); - GetModelSnapshotsResponse getModelSnapshotsResponse1 = execute(getModelSnapshotsRequest, machineLearningClient::getModelSnapshots, - machineLearningClient::getModelSnapshotsAsync); + GetModelSnapshotsResponse getModelSnapshotsResponse1 = execute( + getModelSnapshotsRequest, + machineLearningClient::getModelSnapshots, + machineLearningClient::getModelSnapshotsAsync + ); assertEquals(getModelSnapshotsResponse1.count(), 1L); - assertEquals("State persisted due to job close at 2018-11-07T10:51:59+0000", - getModelSnapshotsResponse1.snapshots().get(0).getDescription()); + assertEquals( + "State persisted due to job close at 2018-11-07T10:51:59+0000", + getModelSnapshotsResponse1.snapshots().get(0).getDescription() + ); UpdateModelSnapshotRequest request = new UpdateModelSnapshotRequest(jobId, snapshotId); request.setDescription("Updated description"); request.setRetain(true); - UpdateModelSnapshotResponse response = execute(request, machineLearningClient::updateModelSnapshot, - machineLearningClient::updateModelSnapshotAsync); + UpdateModelSnapshotResponse response = execute( + request, + machineLearningClient::updateModelSnapshot, + machineLearningClient::updateModelSnapshotAsync + ); assertTrue(response.getAcknowledged()); assertEquals("Updated description", response.getModel().getDescription()); assertTrue(response.getModel().getRetain()); - GetModelSnapshotsResponse getModelSnapshotsResponse2 = execute(getModelSnapshotsRequest, machineLearningClient::getModelSnapshots, - machineLearningClient::getModelSnapshotsAsync); + GetModelSnapshotsResponse getModelSnapshotsResponse2 = execute( + getModelSnapshotsRequest, + machineLearningClient::getModelSnapshots, + machineLearningClient::getModelSnapshotsAsync + ); assertEquals(getModelSnapshotsResponse2.count(), 1L); - assertEquals("Updated description", - getModelSnapshotsResponse2.snapshots().get(0).getDescription()); + assertEquals("Updated description", getModelSnapshotsResponse2.snapshots().get(0).getDescription()); } public void testUpgradeJobSnapshot() throws Exception { @@ -2931,8 +3244,10 @@ public void testUpgradeJobSnapshot() throws Exception { createModelSnapshot(jobId, snapshotId); MachineLearningClient machineLearningClient = highLevelClient().machineLearning(); UpgradeJobModelSnapshotRequest request = new UpgradeJobModelSnapshotRequest(jobId, snapshotId, null, true); - ElasticsearchException ex = expectThrows(ElasticsearchException.class, - () -> execute(request, machineLearningClient::upgradeJobSnapshot, machineLearningClient::upgradeJobSnapshotAsync)); + ElasticsearchException ex = expectThrows( + ElasticsearchException.class, + () -> execute(request, machineLearningClient::upgradeJobSnapshot, machineLearningClient::upgradeJobSnapshotAsync) + ); assertThat( ex.getMessage(), containsString( @@ -2958,14 +3273,17 @@ public void testRevertModelSnapshot() throws IOException { MachineLearningClient machineLearningClient = highLevelClient().machineLearning(); - for (String snapshotId : snapshotIds){ + for (String snapshotId : snapshotIds) { RevertModelSnapshotRequest request = new RevertModelSnapshotRequest(jobId, snapshotId); if (randomBoolean()) { request.setDeleteInterveningResults(randomBoolean()); } - RevertModelSnapshotResponse response = execute(request, machineLearningClient::revertModelSnapshot, - machineLearningClient::revertModelSnapshotAsync); + RevertModelSnapshotResponse response = execute( + request, + machineLearningClient::revertModelSnapshot, + machineLearningClient::revertModelSnapshotAsync + ); ModelSnapshot model = response.getModel(); @@ -2979,19 +3297,22 @@ public void testEnableUpgradeMode() throws Exception { MlInfoResponse mlInfoResponse = machineLearningClient.getMlInfo(new MlInfoRequest(), RequestOptions.DEFAULT); assertThat(mlInfoResponse.getInfo().get("upgrade_mode"), equalTo(false)); - AcknowledgedResponse setUpgrademodeResponse = execute(new SetUpgradeModeRequest(true), + AcknowledgedResponse setUpgrademodeResponse = execute( + new SetUpgradeModeRequest(true), machineLearningClient::setUpgradeMode, - machineLearningClient::setUpgradeModeAsync); + machineLearningClient::setUpgradeModeAsync + ); assertThat(setUpgrademodeResponse.isAcknowledged(), is(true)); - mlInfoResponse = machineLearningClient.getMlInfo(new MlInfoRequest(), RequestOptions.DEFAULT); assertThat(mlInfoResponse.getInfo().get("upgrade_mode"), equalTo(true)); - setUpgrademodeResponse = execute(new SetUpgradeModeRequest(false), + setUpgrademodeResponse = execute( + new SetUpgradeModeRequest(false), machineLearningClient::setUpgradeMode, - machineLearningClient::setUpgradeModeAsync); + machineLearningClient::setUpgradeModeAsync + ); assertThat(setUpgrademodeResponse.isAcknowledged(), is(true)); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/MigrationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/MigrationIT.java index 67d4818d26fc0..02237739818ca 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/MigrationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/MigrationIT.java @@ -44,7 +44,8 @@ public void testGetFeatureUpgradeStatus() throws IOException { GetFeatureUpgradeStatusResponse response = highLevelClient().migration().getFeatureUpgradeStatus(request, RequestOptions.DEFAULT); assertThat(response.getUpgradeStatus(), equalTo("NO_MIGRATION_NEEDED")); assertThat(response.getFeatureUpgradeStatuses().size(), greaterThanOrEqualTo(1)); - Optional optionalTasksStatus = response.getFeatureUpgradeStatuses().stream() + Optional optionalTasksStatus = response.getFeatureUpgradeStatuses() + .stream() .filter(status -> "tasks".equals(status.getFeatureName())) .findFirst(); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/MlTestStateCleaner.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/MlTestStateCleaner.java index 419e9f8790e1a..025aae1bb6801 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/MlTestStateCleaner.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/MlTestStateCleaner.java @@ -51,16 +51,19 @@ public void clearMlMetadata() throws IOException { @SuppressWarnings("unchecked") private void deleteAllTrainedModelIngestPipelines() throws IOException { - Set pipelinesWithModels = client.machineLearning().getTrainedModelsStats( - new GetTrainedModelsStatsRequest("_all").setPageParams(new PageParams(0, 10_000)), RequestOptions.DEFAULT - ).getTrainedModelStats() + Set pipelinesWithModels = client.machineLearning() + .getTrainedModelsStats( + new GetTrainedModelsStatsRequest("_all").setPageParams(new PageParams(0, 10_000)), + RequestOptions.DEFAULT + ) + .getTrainedModelStats() .stream() .flatMap(stats -> { Map ingestStats = stats.getIngestStats(); if (ingestStats == null || ingestStats.isEmpty()) { return Stream.empty(); } - Map pipelines = (Map)ingestStats.get("pipelines"); + Map pipelines = (Map) ingestStats.get("pipelines"); if (pipelines == null || pipelines.isEmpty()) { return Stream.empty(); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/MockRestHighLevelTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/MockRestHighLevelTests.java index 9506b6fd6b296..863fd78d30a61 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/MockRestHighLevelTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/MockRestHighLevelTests.java @@ -54,10 +54,11 @@ private void setupClient() throws IOException { } public void testWarningFailure() { - WarningFailureException exception = expectThrows(WarningFailureException.class, - () -> client.info(RequestOptions.DEFAULT)); - assertThat(exception.getMessage(), equalTo("method [GET], host [http://localhost:9200], URI [/_blah], " + - "status line [HTTP/1.1 200 OK]")); + WarningFailureException exception = expectThrows(WarningFailureException.class, () -> client.info(RequestOptions.DEFAULT)); + assertThat( + exception.getMessage(), + equalTo("method [GET], host [http://localhost:9200], URI [/_blah], " + "status line [HTTP/1.1 200 OK]") + ); assertNull(exception.getCause()); assertThat(exception.getResponse().getWarnings(), equalTo(WARNINGS)); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/PingAndInfoIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/PingAndInfoIT.java index 1507368773f57..7def58d22a44b 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/PingAndInfoIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/PingAndInfoIT.java @@ -10,10 +10,10 @@ import org.apache.http.client.methods.HttpGet; import org.elasticsearch.client.core.MainResponse; +import org.elasticsearch.client.license.LicenseStatus; import org.elasticsearch.client.xpack.XPackInfoRequest; import org.elasticsearch.client.xpack.XPackInfoResponse; import org.elasticsearch.client.xpack.XPackInfoResponse.FeatureSetsInfo.FeatureSet; -import org.elasticsearch.client.license.LicenseStatus; import java.io.IOException; import java.util.EnumSet; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/RankEvalIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/RankEvalIT.java index cffa76fdc3f53..201271495abd1 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/RankEvalIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/RankEvalIT.java @@ -105,7 +105,7 @@ public void testRankEvalRequest() throws IOException { private static List createTestEvaluationSpec() { SearchSourceBuilder testQuery = new SearchSourceBuilder(); testQuery.query(new MatchAllQueryBuilder()); - List amsterdamRatedDocs = createRelevant("index" , "amsterdam1", "amsterdam2", "amsterdam3", "amsterdam4"); + List amsterdamRatedDocs = createRelevant("index", "amsterdam1", "amsterdam2", "amsterdam3", "amsterdam4"); amsterdamRatedDocs.addAll(createRelevant("index2", "amsterdam0")); RatedRequest amsterdamRequest = new RatedRequest("amsterdam_query", amsterdamRatedDocs, testQuery); RatedRequest berlinRequest = new RatedRequest("berlin_query", createRelevant("index", "berlin"), testQuery); @@ -120,9 +120,14 @@ private static List createTestEvaluationSpec() { */ public void testMetrics() throws IOException { List specifications = createTestEvaluationSpec(); - List> metrics = Arrays.asList(PrecisionAtK::new, RecallAtK::new, - MeanReciprocalRank::new, DiscountedCumulativeGain::new, () -> new ExpectedReciprocalRank(1)); - double expectedScores[] = new double[] {0.4285714285714286, 1.0, 0.75, 1.6408962261063627, 0.4407738095238095}; + List> metrics = Arrays.asList( + PrecisionAtK::new, + RecallAtK::new, + MeanReciprocalRank::new, + DiscountedCumulativeGain::new, + () -> new ExpectedReciprocalRank(1) + ); + double expectedScores[] = new double[] { 0.4285714285714286, 1.0, 0.75, 1.6408962261063627, 0.4407738095238095 }; int i = 0; for (Supplier metricSupplier : metrics) { RankEvalSpec spec = new RankEvalSpec(specifications, metricSupplier.get()); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ReindexIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ReindexIT.java index 1e91a96b52bb3..eff6cae79cc52 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ReindexIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ReindexIT.java @@ -18,7 +18,6 @@ import org.elasticsearch.action.support.WriteRequest; import org.elasticsearch.client.tasks.TaskSubmissionResponse; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.query.IdsQueryBuilder; import org.elasticsearch.index.reindex.BulkByScrollResponse; import org.elasticsearch.index.reindex.DeleteByQueryAction; @@ -27,6 +26,7 @@ import org.elasticsearch.rest.RestStatus; import org.elasticsearch.tasks.RawTaskStatus; import org.elasticsearch.tasks.TaskId; +import org.elasticsearch.xcontent.XContentType; import java.io.IOException; import java.util.Collections; @@ -48,23 +48,15 @@ public void testReindex() throws IOException { final String destinationIndex = "dest"; { // Prepare - Settings settings = Settings.builder() - .put("number_of_shards", 1) - .put("number_of_replicas", 0) - .build(); + Settings settings = Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0).build(); createIndex(sourceIndex, settings); createIndex(destinationIndex, settings); - BulkRequest bulkRequest = new BulkRequest() - .add(new IndexRequest(sourceIndex).id("1").source(Collections.singletonMap("foo", "bar"), XContentType.JSON)) + BulkRequest bulkRequest = new BulkRequest().add( + new IndexRequest(sourceIndex).id("1").source(Collections.singletonMap("foo", "bar"), XContentType.JSON) + ) .add(new IndexRequest(sourceIndex).id("2").source(Collections.singletonMap("foo2", "bar2"), XContentType.JSON)) .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - assertEquals( - RestStatus.OK, - highLevelClient().bulk( - bulkRequest, - RequestOptions.DEFAULT - ).status() - ); + assertEquals(RestStatus.OK, highLevelClient().bulk(bulkRequest, RequestOptions.DEFAULT).status()); } { // reindex one document with id 1 from source to destination @@ -96,14 +88,20 @@ public void testReindex() throws IOException { reindexRequest.setRefresh(true); reindexRequest.setRequireAlias(true); - ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, () -> { - execute(reindexRequest, highLevelClient()::reindex, highLevelClient()::reindexAsync); - }); + ElasticsearchStatusException exception = expectThrows( + ElasticsearchStatusException.class, + () -> { execute(reindexRequest, highLevelClient()::reindex, highLevelClient()::reindexAsync); } + ); assertEquals(RestStatus.NOT_FOUND, exception.status()); - assertEquals("Elasticsearch exception [type=index_not_found_exception, reason=no such index [" + - destinationIndex + "] and [require_alias] request flag is [true] and [" + - destinationIndex + "] is not an alias]", exception.getMessage()); + assertEquals( + "Elasticsearch exception [type=index_not_found_exception, reason=no such index [" + + destinationIndex + + "] and [require_alias] request flag is [true] and [" + + destinationIndex + + "] is not an alias]", + exception.getMessage() + ); } } @@ -112,23 +110,15 @@ public void testReindexTask() throws Exception { final String destinationIndex = "dest2"; { // Prepare - Settings settings = Settings.builder() - .put("number_of_shards", 1) - .put("number_of_replicas", 0) - .build(); + Settings settings = Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0).build(); createIndex(sourceIndex, settings); createIndex(destinationIndex, settings); - BulkRequest bulkRequest = new BulkRequest() - .add(new IndexRequest(sourceIndex).id("1").source(Collections.singletonMap("foo", "bar"), XContentType.JSON)) + BulkRequest bulkRequest = new BulkRequest().add( + new IndexRequest(sourceIndex).id("1").source(Collections.singletonMap("foo", "bar"), XContentType.JSON) + ) .add(new IndexRequest(sourceIndex).id("2").source(Collections.singletonMap("foo2", "bar2"), XContentType.JSON)) .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - assertEquals( - RestStatus.OK, - highLevelClient().bulk( - bulkRequest, - RequestOptions.DEFAULT - ).status() - ); + assertEquals(RestStatus.OK, highLevelClient().bulk(bulkRequest, RequestOptions.DEFAULT).status()); } { // tag::submit-reindex-task @@ -151,14 +141,12 @@ public void testReindexConflict() throws IOException { final String sourceIndex = "testreindexconflict_source"; final String destIndex = "testreindexconflict_dest"; - final Settings settings = Settings.builder() - .put("number_of_shards", 1) - .put("number_of_replicas", 0) - .build(); + final Settings settings = Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0).build(); createIndex(sourceIndex, settings); createIndex(destIndex, settings); - final BulkRequest bulkRequest = new BulkRequest() - .add(new IndexRequest(sourceIndex).id("1").source(Collections.singletonMap("foo", "bar"), XContentType.JSON)) + final BulkRequest bulkRequest = new BulkRequest().add( + new IndexRequest(sourceIndex).id("1").source(Collections.singletonMap("foo", "bar"), XContentType.JSON) + ) .add(new IndexRequest(sourceIndex).id("2").source(Collections.singletonMap("foo", "bar"), XContentType.JSON)) .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); assertThat(highLevelClient().bulk(bulkRequest, RequestOptions.DEFAULT).status(), equalTo(RestStatus.OK)); @@ -193,21 +181,16 @@ public void testDeleteByQuery() throws Exception { final String sourceIndex = "source1"; { // Prepare - Settings settings = Settings.builder() - .put("number_of_shards", 1) - .put("number_of_replicas", 0) - .build(); + Settings settings = Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0).build(); createIndex(sourceIndex, settings); assertEquals( RestStatus.OK, highLevelClient().bulk( - new BulkRequest() - .add(new IndexRequest(sourceIndex).id("1") - .source(Collections.singletonMap("foo", 1), XContentType.JSON)) - .add(new IndexRequest(sourceIndex).id("2") - .source(Collections.singletonMap("foo", 2), XContentType.JSON)) - .add(new IndexRequest(sourceIndex).id("3") - .source(Collections.singletonMap("foo", 3), XContentType.JSON)) + new BulkRequest().add( + new IndexRequest(sourceIndex).id("1").source(Collections.singletonMap("foo", 1), XContentType.JSON) + ) + .add(new IndexRequest(sourceIndex).id("2").source(Collections.singletonMap("foo", 2), XContentType.JSON)) + .add(new IndexRequest(sourceIndex).id("3").source(Collections.singletonMap("foo", 3), XContentType.JSON)) .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT ).status() @@ -219,8 +202,11 @@ public void testDeleteByQuery() throws Exception { deleteByQueryRequest.indices(sourceIndex); deleteByQueryRequest.setQuery(new IdsQueryBuilder().addIds("1")); deleteByQueryRequest.setRefresh(true); - BulkByScrollResponse bulkResponse = - execute(deleteByQueryRequest, highLevelClient()::deleteByQuery, highLevelClient()::deleteByQueryAsync); + BulkByScrollResponse bulkResponse = execute( + deleteByQueryRequest, + highLevelClient()::deleteByQuery, + highLevelClient()::deleteByQueryAsync + ); assertEquals(1, bulkResponse.getTotal()); assertEquals(1, bulkResponse.getDeleted()); assertEquals(0, bulkResponse.getNoops()); @@ -261,25 +247,35 @@ public void onFailure(Exception e) { TaskId taskIdToRethrottle = findTaskToRethrottle(DeleteByQueryAction.NAME, deleteByQueryRequest.getDescription()); float requestsPerSecond = 1000f; - ListTasksResponse response = execute(new RethrottleRequest(taskIdToRethrottle, requestsPerSecond), - highLevelClient()::deleteByQueryRethrottle, highLevelClient()::deleteByQueryRethrottleAsync); + ListTasksResponse response = execute( + new RethrottleRequest(taskIdToRethrottle, requestsPerSecond), + highLevelClient()::deleteByQueryRethrottle, + highLevelClient()::deleteByQueryRethrottleAsync + ); assertThat(response.getTaskFailures(), empty()); assertThat(response.getNodeFailures(), empty()); assertThat(response.getTasks(), hasSize(1)); assertEquals(taskIdToRethrottle, response.getTasks().get(0).getTaskId()); assertThat(response.getTasks().get(0).getStatus(), instanceOf(RawTaskStatus.class)); - assertEquals(Float.toString(requestsPerSecond), - ((RawTaskStatus) response.getTasks().get(0).getStatus()).toMap().get("requests_per_second").toString()); + assertEquals( + Float.toString(requestsPerSecond), + ((RawTaskStatus) response.getTasks().get(0).getStatus()).toMap().get("requests_per_second").toString() + ); assertTrue(taskFinished.await(10, TimeUnit.SECONDS)); // any rethrottling after the delete-by-query is done performed with the same taskId should result in a failure - response = execute(new RethrottleRequest(taskIdToRethrottle, requestsPerSecond), - highLevelClient()::deleteByQueryRethrottle, highLevelClient()::deleteByQueryRethrottleAsync); + response = execute( + new RethrottleRequest(taskIdToRethrottle, requestsPerSecond), + highLevelClient()::deleteByQueryRethrottle, + highLevelClient()::deleteByQueryRethrottleAsync + ); assertTrue(response.getTasks().isEmpty()); assertFalse(response.getNodeFailures().isEmpty()); assertEquals(1, response.getNodeFailures().size()); - assertEquals("Elasticsearch exception [type=resource_not_found_exception, reason=task [" + taskIdToRethrottle + "] is missing]", - response.getNodeFailures().get(0).getCause().getMessage()); + assertEquals( + "Elasticsearch exception [type=resource_not_found_exception, reason=task [" + taskIdToRethrottle + "] is missing]", + response.getNodeFailures().get(0).getCause().getMessage() + ); } } @@ -287,21 +283,16 @@ public void testDeleteByQueryTask() throws Exception { final String sourceIndex = "source456"; { // Prepare - Settings settings = Settings.builder() - .put("number_of_shards", 1) - .put("number_of_replicas", 0) - .build(); + Settings settings = Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0).build(); createIndex(sourceIndex, settings); assertEquals( RestStatus.OK, highLevelClient().bulk( - new BulkRequest() - .add(new IndexRequest(sourceIndex).id("1") - .source(Collections.singletonMap("foo", 1), XContentType.JSON)) - .add(new IndexRequest(sourceIndex).id("2") - .source(Collections.singletonMap("foo", 2), XContentType.JSON)) - .add(new IndexRequest(sourceIndex).id("3") - .source(Collections.singletonMap("foo", 3), XContentType.JSON)) + new BulkRequest().add( + new IndexRequest(sourceIndex).id("1").source(Collections.singletonMap("foo", 1), XContentType.JSON) + ) + .add(new IndexRequest(sourceIndex).id("2").source(Collections.singletonMap("foo", 2), XContentType.JSON)) + .add(new IndexRequest(sourceIndex).id("3").source(Collections.singletonMap("foo", 3), XContentType.JSON)) .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT ).status() diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java index 8cd75c45683fc..61e4429ffb486 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java @@ -49,20 +49,15 @@ import org.elasticsearch.client.core.TermVectorsRequest; import org.elasticsearch.client.indices.AnalyzeRequest; import org.elasticsearch.common.CheckedBiConsumer; -import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.io.Streams; import org.elasticsearch.common.lucene.uid.Versions; -import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.xcontent.json.JsonXContent; +import org.elasticsearch.core.RestApiVersion; +import org.elasticsearch.core.TimeValue; +import org.elasticsearch.core.Tuple; import org.elasticsearch.index.VersionType; import org.elasticsearch.index.query.MatchAllQueryBuilder; import org.elasticsearch.index.query.QueryBuilders; @@ -96,6 +91,11 @@ import org.elasticsearch.tasks.TaskId; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.RandomObjects; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.xcontent.json.JsonXContent; import org.hamcrest.Matchers; import java.io.IOException; @@ -282,8 +282,7 @@ public void testMultiGet() throws IOException { public void testMultiGetWithType() throws IOException { MultiGetRequest multiGetRequest = new MultiGetRequest(); - MultiGetRequest.Item item = new MultiGetRequest.Item(randomAlphaOfLength(4), - randomAlphaOfLength(4)); + MultiGetRequest.Item item = new MultiGetRequest.Item(randomAlphaOfLength(4), randomAlphaOfLength(4)); multiGetRequest.add(item); Request request = RequestConverters.multiGet(multiGetRequest); @@ -388,7 +387,11 @@ public void testReindex() throws IOException { Map expectedParams = new HashMap<>(); if (randomBoolean()) { XContentBuilder builder = JsonXContent.contentBuilder().prettyPrint(); - RemoteInfo remoteInfo = new RemoteInfo("http", "remote-host", 9200, null, + RemoteInfo remoteInfo = new RemoteInfo( + "http", + "remote-host", + 9200, + null, BytesReference.bytes(matchAllQuery().toXContent(builder, ToXContent.EMPTY_PARAMS)), "user", "pass", @@ -431,7 +434,7 @@ public void testReindex() throws IOException { reindexRequest.setSourceQuery(new TermQueryBuilder("foo", "fooval")); } if (randomBoolean()) { - int slices = randomIntBetween(0,4); + int slices = randomIntBetween(0, 4); reindexRequest.setSlices(slices); if (slices == 0) { expectedParams.put("slices", AbstractBulkByScrollRequest.AUTO_SLICES_VALUE); @@ -591,7 +594,7 @@ public void testRethrottle() { Float requestsPerSecond; Map expectedParams = new HashMap<>(); if (frequently()) { - requestsPerSecond = (float) randomDoubleBetween(0.0, 100.0, true); + requestsPerSecond = (float) randomDoubleBetween(0.0, 100.0, true); rethrottleRequest = new RethrottleRequest(taskId, requestsPerSecond); expectedParams.put(RethrottleRequest.REQUEST_PER_SECOND_PARAMETER, Float.toString(requestsPerSecond)); } else { @@ -601,10 +604,12 @@ public void testRethrottle() { expectedParams.put("group_by", "none"); List>> variants = new ArrayList<>(); variants.add(new Tuple>("_reindex", () -> RequestConverters.rethrottleReindex(rethrottleRequest))); - variants.add(new Tuple>("_update_by_query", - () -> RequestConverters.rethrottleUpdateByQuery(rethrottleRequest))); - variants.add(new Tuple>("_delete_by_query", - () -> RequestConverters.rethrottleDeleteByQuery(rethrottleRequest))); + variants.add( + new Tuple>("_update_by_query", () -> RequestConverters.rethrottleUpdateByQuery(rethrottleRequest)) + ); + variants.add( + new Tuple>("_delete_by_query", () -> RequestConverters.rethrottleDeleteByQuery(rethrottleRequest)) + ); for (Tuple> variant : variants) { Request request = variant.v2().get(); @@ -797,7 +802,7 @@ public void testUpdate() throws IOException { } } - private static void assertIfSeqNoAndTerm(DocWriteRequestrequest, DocWriteRequest parsedRequest) { + private static void assertIfSeqNoAndTerm(DocWriteRequest request, DocWriteRequest parsedRequest) { assertEquals(request.ifSeqNo(), parsedRequest.ifSeqNo()); assertEquals(request.ifPrimaryTerm(), parsedRequest.ifPrimaryTerm()); } @@ -820,8 +825,10 @@ public void testUpdateWithDifferentContentTypes() { updateRequest.upsert(new IndexRequest().source(singletonMap("field", "upsert"), XContentType.YAML)); RequestConverters.update(updateRequest); }); - assertEquals("Update request cannot have different content types for doc [JSON] and upsert [YAML] documents", - exception.getMessage()); + assertEquals( + "Update request cannot have different content types for doc [JSON] and upsert [YAML] documents", + exception.getMessage() + ); } public void testBulk() throws IOException { @@ -972,20 +979,23 @@ public void testBulkWithDifferentContentTypes() throws IOException { bulkRequest.add(new IndexRequest("index").id("1").source(singletonMap("field", "value"), XContentType.JSON)); IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> RequestConverters.bulk(bulkRequest)); assertEquals( - "Mismatching content-type found for request with content-type [JSON], " + "previous requests have content-type [SMILE]", - exception.getMessage()); + "Mismatching content-type found for request with content-type [JSON], " + "previous requests have content-type [SMILE]", + exception.getMessage() + ); } { BulkRequest bulkRequest = new BulkRequest(); bulkRequest.add(new IndexRequest("index").id("0").source(singletonMap("field", "value"), XContentType.JSON)); bulkRequest.add(new IndexRequest("index").id("1").source(singletonMap("field", "value"), XContentType.JSON)); - bulkRequest.add(new UpdateRequest("index", "2") - .doc(new IndexRequest().source(singletonMap("field", "value"), XContentType.JSON)) - .upsert(new IndexRequest().source(singletonMap("field", "value"), XContentType.SMILE))); + bulkRequest.add( + new UpdateRequest("index", "2").doc(new IndexRequest().source(singletonMap("field", "value"), XContentType.JSON)) + .upsert(new IndexRequest().source(singletonMap("field", "value"), XContentType.SMILE)) + ); IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> RequestConverters.bulk(bulkRequest)); assertEquals( - "Mismatching content-type found for request with content-type [SMILE], " + "previous requests have content-type [JSON]", - exception.getMessage()); + "Mismatching content-type found for request with content-type [SMILE], " + "previous requests have content-type [JSON]", + exception.getMessage() + ); } { XContentType xContentType = randomFrom(XContentType.CBOR, XContentType.YAML); @@ -997,24 +1007,23 @@ public void testBulkWithDifferentContentTypes() throws IOException { bulkRequest.add(new IndexRequest("index").id("4").source(singletonMap("field", "value"), XContentType.JSON)); bulkRequest.add(new IndexRequest("index").id("1").source(singletonMap("field", "value"), xContentType)); IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> RequestConverters.bulk(bulkRequest)); - assertEquals("Unsupported content-type found for request with content-type [" + xContentType - + "], only JSON and SMILE are supported", exception.getMessage()); + assertEquals( + "Unsupported content-type found for request with content-type [" + xContentType + "], only JSON and SMILE are supported", + exception.getMessage() + ); } } public void testGlobalPipelineOnBulkRequest() throws IOException { BulkRequest bulkRequest = new BulkRequest(); bulkRequest.pipeline("xyz"); - bulkRequest.add(new IndexRequest("test").id("11") - .source(XContentType.JSON, "field", "bulk1")); - bulkRequest.add(new IndexRequest("test").id("12") - .source(XContentType.JSON, "field", "bulk2")); - bulkRequest.add(new IndexRequest("test").id("13") - .source(XContentType.JSON, "field", "bulk3")); + bulkRequest.add(new IndexRequest("test").id("11").source(XContentType.JSON, "field", "bulk1")); + bulkRequest.add(new IndexRequest("test").id("12").source(XContentType.JSON, "field", "bulk2")); + bulkRequest.add(new IndexRequest("test").id("13").source(XContentType.JSON, "field", "bulk3")); Request request = RequestConverters.bulk(bulkRequest); - assertThat(request.getParameters(), Matchers.hasEntry("pipeline","xyz")); + assertThat(request.getParameters(), Matchers.hasEntry("pipeline", "xyz")); } public void testSearchNullSource() throws IOException { @@ -1090,17 +1099,23 @@ public static SearchSourceBuilder createTestSearchSourceBuilder() { searchSourceBuilder.query(new TermQueryBuilder(randomAlphaOfLengthBetween(3, 10), randomAlphaOfLengthBetween(3, 10))); } if (randomBoolean()) { - searchSourceBuilder.aggregation(new TermsAggregationBuilder(randomAlphaOfLengthBetween(3, 10)) - .userValueTypeHint(ValueType.STRING) - .field(randomAlphaOfLengthBetween(3, 10))); + searchSourceBuilder.aggregation( + new TermsAggregationBuilder(randomAlphaOfLengthBetween(3, 10)).userValueTypeHint(ValueType.STRING) + .field(randomAlphaOfLengthBetween(3, 10)) + ); } if (randomBoolean()) { - searchSourceBuilder.suggest(new SuggestBuilder().addSuggestion(randomAlphaOfLengthBetween(3, 10), - new CompletionSuggestionBuilder(randomAlphaOfLengthBetween(3, 10)))); + searchSourceBuilder.suggest( + new SuggestBuilder().addSuggestion( + randomAlphaOfLengthBetween(3, 10), + new CompletionSuggestionBuilder(randomAlphaOfLengthBetween(3, 10)) + ) + ); } if (randomBoolean()) { - searchSourceBuilder.addRescorer(new QueryRescorerBuilder( - new TermQueryBuilder(randomAlphaOfLengthBetween(3, 10), randomAlphaOfLengthBetween(3, 10)))); + searchSourceBuilder.addRescorer( + new QueryRescorerBuilder(new TermQueryBuilder(randomAlphaOfLengthBetween(3, 10), randomAlphaOfLengthBetween(3, 10))) + ); } if (randomBoolean()) { searchSourceBuilder.collapse(new CollapseBuilder(randomAlphaOfLengthBetween(3, 10))); @@ -1110,14 +1125,13 @@ public static SearchSourceBuilder createTestSearchSourceBuilder() { return searchSourceBuilder; } - public void testSearchNullIndicesAndTypes() { expectThrows(NullPointerException.class, () -> new SearchRequest((String[]) null)); expectThrows(NullPointerException.class, () -> new SearchRequest().indices((String[]) null)); } - public void testCountNotNullSource() throws IOException { - //as we create SearchSourceBuilder in CountRequest constructor + public void testCountNotNullSource() throws IOException { + // as we create SearchSourceBuilder in CountRequest constructor CountRequest countRequest = new CountRequest(); Request request = RequestConverters.count(countRequest); assertEquals(HttpPost.METHOD_NAME, request.getMethod()); @@ -1169,8 +1183,7 @@ public void testCountNullIndicesAndTypes() { expectThrows(NullPointerException.class, () -> new CountRequest().types((String[]) null)); } - private static void setRandomCountParams(CountRequest countRequest, - Map expectedParams) { + private static void setRandomCountParams(CountRequest countRequest, Map expectedParams) { if (randomBoolean()) { countRequest.routing(randomAlphaOfLengthBetween(3, 10)); expectedParams.put("routing", countRequest.routing()); @@ -1207,10 +1220,19 @@ public void testMultiSearch() throws IOException { // specified from msearch api, so unset other options: IndicesOptions randomlyGenerated = searchRequest.indicesOptions(); IndicesOptions msearchDefault = new MultiSearchRequest().indicesOptions(); - searchRequest.indicesOptions(IndicesOptions.fromOptions(randomlyGenerated.ignoreUnavailable(), - randomlyGenerated.allowNoIndices(), randomlyGenerated.expandWildcardsOpen(), randomlyGenerated.expandWildcardsClosed(), - msearchDefault.expandWildcardsHidden(), msearchDefault.allowAliasesToMultipleIndices(), - msearchDefault.forbidClosedIndices(), msearchDefault.ignoreAliases(), msearchDefault.ignoreThrottled())); + searchRequest.indicesOptions( + IndicesOptions.fromOptions( + randomlyGenerated.ignoreUnavailable(), + randomlyGenerated.allowNoIndices(), + randomlyGenerated.expandWildcardsOpen(), + randomlyGenerated.expandWildcardsClosed(), + msearchDefault.expandWildcardsHidden(), + msearchDefault.allowAliasesToMultipleIndices(), + msearchDefault.forbidClosedIndices(), + msearchDefault.ignoreAliases(), + msearchDefault.ignoreThrottled() + ) + ); multiSearchRequest.add(searchRequest); } @@ -1234,9 +1256,19 @@ public void testMultiSearch() throws IOException { } requests.add(searchRequest); }; - MultiSearchRequest.readMultiLineFormat(new BytesArray(EntityUtils.toByteArray(request.getEntity())), - REQUEST_BODY_CONTENT_TYPE.xContent(), consumer, null, multiSearchRequest.indicesOptions(), null, null, null, - xContentRegistry(), true, RestApiVersion.current()); + MultiSearchRequest.readMultiLineFormat( + new BytesArray(EntityUtils.toByteArray(request.getEntity())), + REQUEST_BODY_CONTENT_TYPE.xContent(), + consumer, + null, + multiSearchRequest.indicesOptions(), + null, + null, + null, + xContentRegistry(), + true, + RestApiVersion.current() + ); assertEquals(requests, multiSearchRequest.requests()); } @@ -1362,7 +1394,7 @@ public void testMultiSearchTemplate() throws Exception { Map expectedParams = new HashMap<>(); if (randomBoolean()) { - multiSearchTemplateRequest.maxConcurrentSearchRequests(randomIntBetween(1,10)); + multiSearchTemplateRequest.maxConcurrentSearchRequests(randomIntBetween(1, 10)); expectedParams.put("max_concurrent_searches", Integer.toString(multiSearchTemplateRequest.maxConcurrentSearchRequests())); } expectedParams.put(RestSearchAction.TYPED_KEYS_PARAM, "true"); @@ -1599,8 +1631,7 @@ public void testFieldCapsWithIndexFilter() throws IOException { String[] indices = randomIndicesNames(0, 5); String[] fields = generateRandomStringArray(5, 10, false, false); - FieldCapabilitiesRequest fieldCapabilitiesRequest = new FieldCapabilitiesRequest() - .indices(indices) + FieldCapabilitiesRequest fieldCapabilitiesRequest = new FieldCapabilitiesRequest().indices(indices) .fields(fields) .indexFilter(QueryBuilders.matchAllQuery()); @@ -1639,8 +1670,9 @@ public void testFieldCapsWithIndexFilter() throws IOException { public void testRankEval() throws Exception { RankEvalSpec spec = new RankEvalSpec( - Collections.singletonList(new RatedRequest("queryId", Collections.emptyList(), new SearchSourceBuilder())), - new PrecisionAtK()); + Collections.singletonList(new RatedRequest("queryId", Collections.emptyList(), new SearchSourceBuilder())), + new PrecisionAtK() + ); String[] indices = randomIndicesNames(0, 5); RankEvalRequest rankEvalRequest = new RankEvalRequest(spec, indices); Map expectedParams = new HashMap<>(); @@ -1673,10 +1705,7 @@ public void testPutScript() throws Exception { XContentType xContentType = randomFrom(XContentType.values()); try (XContentBuilder builder = XContentBuilder.builder(xContentType.xContent())) { builder.startObject(); - builder.startObject("script") - .field("lang", "painless") - .field("source", "Math.log(_score * 2) + params.multiplier") - .endObject(); + builder.startObject("script").field("lang", "painless").field("source", "Math.log(_score * 2) + params.multiplier").endObject(); builder.endObject(); putStoredScriptRequest.content(BytesReference.bytes(builder), xContentType); @@ -1701,8 +1730,7 @@ public void testPutScript() throws Exception { } public void testAnalyzeRequest() throws Exception { - AnalyzeRequest indexAnalyzeRequest - = AnalyzeRequest.withIndexAnalyzer("test_index", "test_analyzer", "Here is some text"); + AnalyzeRequest indexAnalyzeRequest = AnalyzeRequest.withIndexAnalyzer("test_index", "test_analyzer", "Here is some text"); Request request = RequestConverters.analyze(indexAnalyzeRequest); assertThat(request.getEndpoint(), equalTo("/test_index/_analyze")); @@ -1831,7 +1859,7 @@ public void testEndpointBuilderEncodeParts() { } { EndpointBuilder endpointBuilder = new EndpointBuilder().addCommaSeparatedPathParts(new String[] { "index1", "index2" }) - .addPathPartAsIs("cache/clear"); + .addPathPartAsIs("cache/clear"); assertEquals("/index1,index2/cache/clear", endpointBuilder.build()); } { @@ -1873,10 +1901,14 @@ public void testEndpoint() { assertEquals("/index/type/id/_endpoint", RequestConverters.endpoint("index", "type", "id", "_endpoint")); assertEquals("/index1,index2", RequestConverters.endpoint(new String[] { "index1", "index2" })); assertEquals("/index1,index2/_endpoint", RequestConverters.endpoint(new String[] { "index1", "index2" }, "_endpoint")); - assertEquals("/index1,index2/type1,type2/_endpoint", - RequestConverters.endpoint(new String[] { "index1", "index2" }, new String[] { "type1", "type2" }, "_endpoint")); - assertEquals("/index1,index2/_endpoint/suffix1,suffix2", - RequestConverters.endpoint(new String[] { "index1", "index2" }, "_endpoint", new String[] { "suffix1", "suffix2" })); + assertEquals( + "/index1,index2/type1,type2/_endpoint", + RequestConverters.endpoint(new String[] { "index1", "index2" }, new String[] { "type1", "type2" }, "_endpoint") + ); + assertEquals( + "/index1,index2/_endpoint/suffix1,suffix2", + RequestConverters.endpoint(new String[] { "index1", "index2" }, "_endpoint", new String[] { "suffix1", "suffix2" }) + ); } public void testCreateContentType() { @@ -1894,24 +1926,39 @@ public void testEnforceSameContentType() { XContentType bulkContentType = randomBoolean() ? xContentType : null; - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, - () -> enforceSameContentType(new IndexRequest().source(singletonMap("field", "value"), XContentType.CBOR), - bulkContentType)); - assertEquals("Unsupported content-type found for request with content-type [CBOR], only JSON and SMILE are supported", - exception.getMessage()); - - exception = expectThrows(IllegalArgumentException.class, - () -> enforceSameContentType(new IndexRequest().source(singletonMap("field", "value"), XContentType.YAML), - bulkContentType)); - assertEquals("Unsupported content-type found for request with content-type [YAML], only JSON and SMILE are supported", - exception.getMessage()); + IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> enforceSameContentType(new IndexRequest().source(singletonMap("field", "value"), XContentType.CBOR), bulkContentType) + ); + assertEquals( + "Unsupported content-type found for request with content-type [CBOR], only JSON and SMILE are supported", + exception.getMessage() + ); + + exception = expectThrows( + IllegalArgumentException.class, + () -> enforceSameContentType(new IndexRequest().source(singletonMap("field", "value"), XContentType.YAML), bulkContentType) + ); + assertEquals( + "Unsupported content-type found for request with content-type [YAML], only JSON and SMILE are supported", + exception.getMessage() + ); XContentType requestContentType = xContentType.canonical() == XContentType.JSON ? XContentType.SMILE : XContentType.JSON; - exception = expectThrows(IllegalArgumentException.class, - () -> enforceSameContentType(new IndexRequest().source(singletonMap("field", "value"), requestContentType), xContentType)); - assertEquals("Mismatching content-type found for request with content-type [" + requestContentType + "], " - + "previous requests have content-type [" + xContentType + "]", exception.getMessage()); + exception = expectThrows( + IllegalArgumentException.class, + () -> enforceSameContentType(new IndexRequest().source(singletonMap("field", "value"), requestContentType), xContentType) + ); + assertEquals( + "Mismatching content-type found for request with content-type [" + + requestContentType + + "], " + + "previous requests have content-type [" + + xContentType + + "]", + exception.getMessage() + ); } /** @@ -1943,8 +1990,7 @@ private static void randomizeFetchSourceContextParams(Consumer expectedParams) { + private static void setRandomSearchParams(SearchRequest searchRequest, Map expectedParams) { expectedParams.put(RestSearchAction.TYPED_KEYS_PARAM, "true"); if (randomBoolean()) { searchRequest.routing(randomAlphaOfLengthBetween(3, 10)); @@ -1993,8 +2039,11 @@ private static void setRandomSearchParams(SearchRequest searchRequest, } } - public static void setRandomIndicesOptions(Consumer setter, Supplier getter, - Map expectedParams) { + public static void setRandomIndicesOptions( + Consumer setter, + Supplier getter, + Map expectedParams + ) { if (randomBoolean()) { // randomly not set random indices options. @@ -2002,8 +2051,16 @@ public static void setRandomIndicesOptions(Consumer setter, Supp } IndicesOptions defaults = getter.get(); - IndicesOptions random = IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), true, false, - false, randomBoolean()); + IndicesOptions random = IndicesOptions.fromOptions( + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + true, + false, + false, + randomBoolean() + ); if (random.equals(defaults)) { // Random indices options is equal to the defaults, we expect no params to be set. return; @@ -2026,8 +2083,16 @@ public static void setRandomIndicesOptions(Consumer setter, Supp static IndicesOptions setRandomIndicesOptions(IndicesOptions indicesOptions, Map expectedParams) { if (randomBoolean()) { - indicesOptions = IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), - true, false, false, randomBoolean()); + indicesOptions = IndicesOptions.fromOptions( + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + true, + false, + false, + randomBoolean() + ); } expectedParams.put("ignore_unavailable", Boolean.toString(indicesOptions.ignoreUnavailable())); expectedParams.put("allow_no_indices", Boolean.toString(indicesOptions.allowNoIndices())); @@ -2075,9 +2140,11 @@ static void setRandomLocal(Consumer setter, Map expecte } static void setRandomTimeout(TimedRequest request, TimeValue defaultTimeout, Map expectedParams) { - setRandomTimeout(s -> - request.setTimeout(TimeValue.parseTimeValue(s, request.getClass().getName() + ".timeout")), - defaultTimeout, expectedParams); + setRandomTimeout( + s -> request.setTimeout(TimeValue.parseTimeValue(s, request.getClass().getName() + ".timeout")), + defaultTimeout, + expectedParams + ); } static void setRandomTimeout(Consumer setter, TimeValue defaultTimeout, Map expectedParams) { @@ -2090,8 +2157,7 @@ static void setRandomTimeout(Consumer setter, TimeValue defaultTimeout, } } - static void setRandomTimeoutTimeValue(Consumer setter, TimeValue defaultTimeout, - Map expectedParams) { + static void setRandomTimeoutTimeValue(Consumer setter, TimeValue defaultTimeout, Map expectedParams) { if (randomBoolean()) { TimeValue timeout = TimeValue.parseTimeValue(randomTimeValue(), "random_timeout"); setter.accept(timeout); @@ -2106,9 +2172,10 @@ static void setRandomMasterTimeout(MasterNodeRequest request, Map expectedParams) { - setRandomMasterTimeout(s -> - request.setMasterTimeout(TimeValue.parseTimeValue(s, request.getClass().getName() + ".masterNodeTimeout")), - expectedParams); + setRandomMasterTimeout( + s -> request.setMasterTimeout(TimeValue.parseTimeValue(s, request.getClass().getName() + ".masterNodeTimeout")), + expectedParams + ); } static void setRandomMasterTimeout(Consumer setter, Map expectedParams) { @@ -2135,8 +2202,11 @@ static void setRandomWaitForActiveShards(Consumer setter, Map< setRandomWaitForActiveShards(setter, ActiveShardCount.DEFAULT, expectedParams); } - static void setRandomWaitForActiveShards(Consumer setter, ActiveShardCount defaultActiveShardCount, - Map expectedParams) { + static void setRandomWaitForActiveShards( + Consumer setter, + ActiveShardCount defaultActiveShardCount, + Map expectedParams + ) { if (randomBoolean()) { int waitForActiveShardsInt = randomIntBetween(-1, 5); String waitForActiveShardsString; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientExtTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientExtTests.java index 883e891aff7c6..1343e9952ae43 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientExtTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientExtTests.java @@ -11,10 +11,10 @@ import org.apache.http.HttpEntity; import org.apache.http.entity.ContentType; import org.apache.http.nio.entity.NStringEntity; -import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.ESTestCase; import org.junit.Before; import java.io.IOException; @@ -52,7 +52,7 @@ public void testParseEntityCustomResponseSection() throws IOException { BaseCustomResponseSection customSection = restHighLevelClient.parseEntity(jsonEntity, BaseCustomResponseSection::fromXContent); assertThat(customSection, instanceOf(CustomResponseSection2.class)); CustomResponseSection2 customResponseSection2 = (CustomResponseSection2) customSection; - assertArrayEquals(new String[]{"item1", "item2"}, customResponseSection2.values); + assertArrayEquals(new String[] { "item1", "item2" }, customResponseSection2.values); } } @@ -64,10 +64,20 @@ private RestHighLevelClientExt(RestClient restClient) { private static List getNamedXContentsExt() { List entries = new ArrayList<>(); - entries.add(new NamedXContentRegistry.Entry(BaseCustomResponseSection.class, new ParseField("custom1"), - CustomResponseSection1::fromXContent)); - entries.add(new NamedXContentRegistry.Entry(BaseCustomResponseSection.class, new ParseField("custom2"), - CustomResponseSection2::fromXContent)); + entries.add( + new NamedXContentRegistry.Entry( + BaseCustomResponseSection.class, + new ParseField("custom1"), + CustomResponseSection1::fromXContent + ) + ); + entries.add( + new NamedXContentRegistry.Entry( + BaseCustomResponseSection.class, + new ParseField("custom2"), + CustomResponseSection2::fromXContent + ) + ); return entries; } } @@ -116,7 +126,7 @@ static CustomResponseSection2 fromXContent(XContentParser parser) throws IOExcep assertEquals("array", parser.currentName()); assertEquals(XContentParser.Token.START_ARRAY, parser.nextToken()); List values = new ArrayList<>(); - while(parser.nextToken().isValue()) { + while (parser.nextToken().isValue()) { values.add(parser.text()); } assertEquals(XContentParser.Token.END_ARRAY, parser.currentToken()); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientTests.java index 9a45bf037338e..0dcebad9948cd 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientTests.java @@ -175,11 +175,16 @@ public class RestHighLevelClientTests extends ESTestCase { */ private static final Set APIS_WITHOUT_REQUEST_OBJECT = Sets.newHashSet( // core - "ping", "info", + "ping", + "info", // security - "security.get_ssl_certificates", "security.authenticate", "security.get_user_privileges", "security.get_builtin_privileges", + "security.get_ssl_certificates", + "security.authenticate", + "security.get_user_privileges", + "security.get_builtin_privileges", // license - "license.get_trial_status", "license.get_basic_status" + "license.get_trial_status", + "license.get_basic_status" ); @@ -196,10 +201,14 @@ public void initClient() throws IOException { /** * Mock rest client to return a valid response to async GET with the current build "/" */ - static void mockGetRoot(RestClient restClient) throws IOException{ + static void mockGetRoot(RestClient restClient) throws IOException { Build build = new Build( - Build.Flavor.DEFAULT, Build.CURRENT.type(), Build.CURRENT.hash(), - Build.CURRENT.date(), false, Build.CURRENT.getQualifiedVersion() + Build.Flavor.DEFAULT, + Build.CURRENT.type(), + Build.CURRENT.hash(), + Build.CURRENT.date(), + false, + Build.CURRENT.getQualifiedVersion() ); mockGetRoot(restClient, build, true); @@ -236,13 +245,10 @@ public static void mockGetRoot(RestClient restClient, byte[] responseBody, boole when(response.getHeader("X-Elastic-Product")).thenReturn("Elasticsearch"); } - when(restClient - .performRequestAsync(argThat(new RequestMatcher("GET", "/")::matches), any())) - .thenAnswer(i -> { - ((ResponseListener)i.getArguments()[1]).onSuccess(response); - return Cancellable.NO_OP; - } - ); + when(restClient.performRequestAsync(argThat(new RequestMatcher("GET", "/")::matches), any())).thenAnswer(i -> { + ((ResponseListener) i.getArguments()[1]).onSuccess(response); + return Cancellable.NO_OP; + }); } public void testCloseIsIdempotent() throws IOException { @@ -274,9 +280,23 @@ public void testPingSocketTimeout() throws IOException { } public void testInfo() throws IOException { - MainResponse testInfo = new MainResponse("nodeName", new MainResponse.Version("number", "buildFlavor", "buildType", "buildHash", - "buildDate", true, "luceneVersion", "minimumWireCompatibilityVersion", "minimumIndexCompatibilityVersion"), - "clusterName", "clusterUuid", "You Know, for Search"); + MainResponse testInfo = new MainResponse( + "nodeName", + new MainResponse.Version( + "number", + "buildFlavor", + "buildType", + "buildHash", + "buildDate", + true, + "luceneVersion", + "minimumWireCompatibilityVersion", + "minimumIndexCompatibilityVersion" + ), + "clusterName", + "clusterUuid", + "You Know, for Search" + ); mockResponse((ToXContentFragment) (builder, params) -> { // taken from the server side MainResponse builder.field("name", testInfo.getNodeName()); @@ -301,12 +321,21 @@ public void testInfo() throws IOException { } public void testSearchScroll() throws IOException { - SearchResponse mockSearchResponse = new SearchResponse(new SearchResponseSections(SearchHits.empty(), InternalAggregations.EMPTY, - null, false, false, null, 1), randomAlphaOfLengthBetween(5, 10), 5, 5, 0, 100, ShardSearchFailure.EMPTY_ARRAY, - SearchResponse.Clusters.EMPTY); + SearchResponse mockSearchResponse = new SearchResponse( + new SearchResponseSections(SearchHits.empty(), InternalAggregations.EMPTY, null, false, false, null, 1), + randomAlphaOfLengthBetween(5, 10), + 5, + 5, + 0, + 100, + ShardSearchFailure.EMPTY_ARRAY, + SearchResponse.Clusters.EMPTY + ); mockResponse(mockSearchResponse); SearchResponse searchResponse = restHighLevelClient.scroll( - new SearchScrollRequest(randomAlphaOfLengthBetween(5, 10)), RequestOptions.DEFAULT); + new SearchScrollRequest(randomAlphaOfLengthBetween(5, 10)), + RequestOptions.DEFAULT + ); assertEquals(mockSearchResponse.getScrollId(), searchResponse.getScrollId()); assertEquals(0, searchResponse.getHits().getTotalHits().value); assertEquals(5, searchResponse.getTotalShards()); @@ -343,8 +372,10 @@ public ActionRequestValidationException validate() { }; { - ActionRequestValidationException actualException = expectThrows(ActionRequestValidationException.class, - () -> restHighLevelClient.performRequest(request, null, RequestOptions.DEFAULT, null, null)); + ActionRequestValidationException actualException = expectThrows( + ActionRequestValidationException.class, + () -> restHighLevelClient.performRequest(request, null, RequestOptions.DEFAULT, null, null) + ); assertSame(validationException, actualException); } { @@ -360,8 +391,10 @@ public void testParseEntity() throws IOException { assertEquals("Response body expected but not returned", ise.getMessage()); } { - IllegalStateException ise = expectThrows(IllegalStateException.class, - () -> restHighLevelClient.parseEntity(new NStringEntity("", (ContentType) null), null)); + IllegalStateException ise = expectThrows( + IllegalStateException.class, + () -> restHighLevelClient.parseEntity(new NStringEntity("", (ContentType) null), null) + ); assertEquals("Elasticsearch didn't return the [Content-Type] header, unable to parse response body", ise.getMessage()); } { @@ -420,8 +453,12 @@ public void testParseResponseException() throws IOException { { RestStatus restStatus = randomFrom(RestStatus.values()); HttpResponse httpResponse = new BasicHttpResponse(newStatusLine(restStatus)); - httpResponse.setEntity(new NStringEntity("{\"error\":\"test error message\",\"status\":" + restStatus.getStatus() + "}", - ContentType.APPLICATION_JSON)); + httpResponse.setEntity( + new NStringEntity( + "{\"error\":\"test error message\",\"status\":" + restStatus.getStatus() + "}", + ContentType.APPLICATION_JSON + ) + ); Response response = new Response(REQUEST_LINE, new HttpHost("localhost", 9200), httpResponse); ResponseException responseException = new ResponseException(response); ElasticsearchException elasticsearchException = restHighLevelClient.parseResponseException(responseException); @@ -463,15 +500,35 @@ public void testPerformRequestOnSuccess() throws IOException { Response mockResponse = new Response(REQUEST_LINE, new HttpHost("localhost", 9200), httpResponse); when(restClient.performRequest(any(Request.class))).thenReturn(mockResponse); { - Integer result = restHighLevelClient.performRequest(mainRequest, requestConverter, RequestOptions.DEFAULT, - response -> response.getStatusLine().getStatusCode(), Collections.emptySet()); + Integer result = restHighLevelClient.performRequest( + mainRequest, + requestConverter, + RequestOptions.DEFAULT, + response -> response.getStatusLine().getStatusCode(), + Collections.emptySet() + ); assertEquals(restStatus.getStatus(), result.intValue()); } { - IOException ioe = expectThrows(IOException.class, () -> restHighLevelClient.performRequest(mainRequest, - requestConverter, RequestOptions.DEFAULT, response -> {throw new IllegalStateException();}, Collections.emptySet())); - assertEquals("Unable to parse response body for Response{requestLine=GET / http/1.1, host=http://localhost:9200, " + - "response=http/1.1 " + restStatus.getStatus() + " " + restStatus.name() + "}", ioe.getMessage()); + IOException ioe = expectThrows( + IOException.class, + () -> restHighLevelClient.performRequest( + mainRequest, + requestConverter, + RequestOptions.DEFAULT, + response -> { throw new IllegalStateException(); }, + Collections.emptySet() + ) + ); + assertEquals( + "Unable to parse response body for Response{requestLine=GET / http/1.1, host=http://localhost:9200, " + + "response=http/1.1 " + + restStatus.getStatus() + + " " + + restStatus.name() + + "}", + ioe.getMessage() + ); } } @@ -483,9 +540,16 @@ public void testPerformRequestOnResponseExceptionWithoutEntity() throws IOExcept Response mockResponse = new Response(REQUEST_LINE, new HttpHost("localhost", 9200), httpResponse); ResponseException responseException = new ResponseException(mockResponse); when(restClient.performRequest(any(Request.class))).thenThrow(responseException); - ElasticsearchException elasticsearchException = expectThrows(ElasticsearchException.class, - () -> restHighLevelClient.performRequest(mainRequest, requestConverter, RequestOptions.DEFAULT, - response -> response.getStatusLine().getStatusCode(), Collections.emptySet())); + ElasticsearchException elasticsearchException = expectThrows( + ElasticsearchException.class, + () -> restHighLevelClient.performRequest( + mainRequest, + requestConverter, + RequestOptions.DEFAULT, + response -> response.getStatusLine().getStatusCode(), + Collections.emptySet() + ) + ); assertEquals(responseException.getMessage(), elasticsearchException.getMessage()); assertEquals(restStatus, elasticsearchException.status()); assertSame(responseException, elasticsearchException.getCause()); @@ -496,14 +560,22 @@ public void testPerformRequestOnResponseExceptionWithEntity() throws IOException CheckedFunction requestConverter = request -> new Request(HttpGet.METHOD_NAME, "/"); RestStatus restStatus = randomFrom(RestStatus.values()); HttpResponse httpResponse = new BasicHttpResponse(newStatusLine(restStatus)); - httpResponse.setEntity(new NStringEntity("{\"error\":\"test error message\",\"status\":" + restStatus.getStatus() + "}", - ContentType.APPLICATION_JSON)); + httpResponse.setEntity( + new NStringEntity("{\"error\":\"test error message\",\"status\":" + restStatus.getStatus() + "}", ContentType.APPLICATION_JSON) + ); Response mockResponse = new Response(REQUEST_LINE, new HttpHost("localhost", 9200), httpResponse); ResponseException responseException = new ResponseException(mockResponse); when(restClient.performRequest(any(Request.class))).thenThrow(responseException); - ElasticsearchException elasticsearchException = expectThrows(ElasticsearchException.class, - () -> restHighLevelClient.performRequest(mainRequest, requestConverter, RequestOptions.DEFAULT, - response -> response.getStatusLine().getStatusCode(), Collections.emptySet())); + ElasticsearchException elasticsearchException = expectThrows( + ElasticsearchException.class, + () -> restHighLevelClient.performRequest( + mainRequest, + requestConverter, + RequestOptions.DEFAULT, + response -> response.getStatusLine().getStatusCode(), + Collections.emptySet() + ) + ); assertEquals("Elasticsearch exception [type=exception, reason=test error message]", elasticsearchException.getMessage()); assertEquals(restStatus, elasticsearchException.status()); assertSame(responseException, elasticsearchException.getSuppressed()[0]); @@ -518,9 +590,16 @@ public void testPerformRequestOnResponseExceptionWithBrokenEntity() throws IOExc Response mockResponse = new Response(REQUEST_LINE, new HttpHost("localhost", 9200), httpResponse); ResponseException responseException = new ResponseException(mockResponse); when(restClient.performRequest(any(Request.class))).thenThrow(responseException); - ElasticsearchException elasticsearchException = expectThrows(ElasticsearchException.class, - () -> restHighLevelClient.performRequest(mainRequest, requestConverter, RequestOptions.DEFAULT, - response -> response.getStatusLine().getStatusCode(), Collections.emptySet())); + ElasticsearchException elasticsearchException = expectThrows( + ElasticsearchException.class, + () -> restHighLevelClient.performRequest( + mainRequest, + requestConverter, + RequestOptions.DEFAULT, + response -> response.getStatusLine().getStatusCode(), + Collections.emptySet() + ) + ); assertEquals("Unable to parse response body", elasticsearchException.getMessage()); assertEquals(restStatus, elasticsearchException.status()); assertSame(responseException, elasticsearchException.getCause()); @@ -536,9 +615,16 @@ public void testPerformRequestOnResponseExceptionWithBrokenEntity2() throws IOEx Response mockResponse = new Response(REQUEST_LINE, new HttpHost("localhost", 9200), httpResponse); ResponseException responseException = new ResponseException(mockResponse); when(restClient.performRequest(any(Request.class))).thenThrow(responseException); - ElasticsearchException elasticsearchException = expectThrows(ElasticsearchException.class, - () -> restHighLevelClient.performRequest(mainRequest, requestConverter, RequestOptions.DEFAULT, - response -> response.getStatusLine().getStatusCode(), Collections.emptySet())); + ElasticsearchException elasticsearchException = expectThrows( + ElasticsearchException.class, + () -> restHighLevelClient.performRequest( + mainRequest, + requestConverter, + RequestOptions.DEFAULT, + response -> response.getStatusLine().getStatusCode(), + Collections.emptySet() + ) + ); assertEquals("Unable to parse response body", elasticsearchException.getMessage()); assertEquals(restStatus, elasticsearchException.status()); assertSame(responseException, elasticsearchException.getCause()); @@ -552,9 +638,17 @@ public void testPerformRequestOnResponseExceptionWithIgnores() throws IOExceptio Response mockResponse = new Response(REQUEST_LINE, new HttpHost("localhost", 9200), httpResponse); ResponseException responseException = new ResponseException(mockResponse); when(restClient.performRequest(any(Request.class))).thenThrow(responseException); - //although we got an exception, we turn it into a successful response because the status code was provided among ignores - assertEquals(Integer.valueOf(404), restHighLevelClient.performRequest(mainRequest, requestConverter, RequestOptions.DEFAULT, - response -> response.getStatusLine().getStatusCode(), Collections.singleton(404))); + // although we got an exception, we turn it into a successful response because the status code was provided among ignores + assertEquals( + Integer.valueOf(404), + restHighLevelClient.performRequest( + mainRequest, + requestConverter, + RequestOptions.DEFAULT, + response -> response.getStatusLine().getStatusCode(), + Collections.singleton(404) + ) + ); } public void testPerformRequestOnResponseExceptionWithIgnoresErrorNoBody() throws IOException { @@ -564,9 +658,16 @@ public void testPerformRequestOnResponseExceptionWithIgnoresErrorNoBody() throws Response mockResponse = new Response(REQUEST_LINE, new HttpHost("localhost", 9200), httpResponse); ResponseException responseException = new ResponseException(mockResponse); when(restClient.performRequest(any(Request.class))).thenThrow(responseException); - ElasticsearchException elasticsearchException = expectThrows(ElasticsearchException.class, - () -> restHighLevelClient.performRequest(mainRequest, requestConverter, RequestOptions.DEFAULT, - response -> {throw new IllegalStateException();}, Collections.singleton(404))); + ElasticsearchException elasticsearchException = expectThrows( + ElasticsearchException.class, + () -> restHighLevelClient.performRequest( + mainRequest, + requestConverter, + RequestOptions.DEFAULT, + response -> { throw new IllegalStateException(); }, + Collections.singleton(404) + ) + ); assertEquals(RestStatus.NOT_FOUND, elasticsearchException.status()); assertSame(responseException, elasticsearchException.getCause()); assertEquals(responseException.getMessage(), elasticsearchException.getMessage()); @@ -576,14 +677,20 @@ public void testPerformRequestOnResponseExceptionWithIgnoresErrorValidBody() thr MainRequest mainRequest = new MainRequest(); CheckedFunction requestConverter = request -> new Request(HttpGet.METHOD_NAME, "/"); HttpResponse httpResponse = new BasicHttpResponse(newStatusLine(RestStatus.NOT_FOUND)); - httpResponse.setEntity(new NStringEntity("{\"error\":\"test error message\",\"status\":404}", - ContentType.APPLICATION_JSON)); + httpResponse.setEntity(new NStringEntity("{\"error\":\"test error message\",\"status\":404}", ContentType.APPLICATION_JSON)); Response mockResponse = new Response(REQUEST_LINE, new HttpHost("localhost", 9200), httpResponse); ResponseException responseException = new ResponseException(mockResponse); when(restClient.performRequest(any(Request.class))).thenThrow(responseException); - ElasticsearchException elasticsearchException = expectThrows(ElasticsearchException.class, - () -> restHighLevelClient.performRequest(mainRequest, requestConverter, RequestOptions.DEFAULT, - response -> {throw new IllegalStateException();}, Collections.singleton(404))); + ElasticsearchException elasticsearchException = expectThrows( + ElasticsearchException.class, + () -> restHighLevelClient.performRequest( + mainRequest, + requestConverter, + RequestOptions.DEFAULT, + response -> { throw new IllegalStateException(); }, + Collections.singleton(404) + ) + ); assertEquals(RestStatus.NOT_FOUND, elasticsearchException.status()); assertSame(responseException, elasticsearchException.getSuppressed()[0]); assertEquals("Elasticsearch exception [type=exception, reason=test error message]", elasticsearchException.getMessage()); @@ -593,7 +700,10 @@ public void testWrapResponseListenerOnSuccess() { { TrackingActionListener trackingActionListener = new TrackingActionListener(); ResponseListener responseListener = restHighLevelClient.wrapResponseListener( - response -> response.getStatusLine().getStatusCode(), trackingActionListener, Collections.emptySet()); + response -> response.getStatusLine().getStatusCode(), + trackingActionListener, + Collections.emptySet() + ); RestStatus restStatus = randomFrom(RestStatus.values()); HttpResponse httpResponse = new BasicHttpResponse(newStatusLine(restStatus)); responseListener.onSuccess(new Response(REQUEST_LINE, new HttpHost("localhost", 9200), httpResponse)); @@ -603,14 +713,24 @@ public void testWrapResponseListenerOnSuccess() { { TrackingActionListener trackingActionListener = new TrackingActionListener(); ResponseListener responseListener = restHighLevelClient.wrapResponseListener( - response -> {throw new IllegalStateException();}, trackingActionListener, Collections.emptySet()); + response -> { throw new IllegalStateException(); }, + trackingActionListener, + Collections.emptySet() + ); RestStatus restStatus = randomFrom(RestStatus.values()); HttpResponse httpResponse = new BasicHttpResponse(newStatusLine(restStatus)); responseListener.onSuccess(new Response(REQUEST_LINE, new HttpHost("localhost", 9200), httpResponse)); assertThat(trackingActionListener.exception.get(), instanceOf(IOException.class)); IOException ioe = (IOException) trackingActionListener.exception.get(); - assertEquals("Unable to parse response body for Response{requestLine=GET / http/1.1, host=http://localhost:9200, " + - "response=http/1.1 " + restStatus.getStatus() + " " + restStatus.name() + "}", ioe.getMessage()); + assertEquals( + "Unable to parse response body for Response{requestLine=GET / http/1.1, host=http://localhost:9200, " + + "response=http/1.1 " + + restStatus.getStatus() + + " " + + restStatus.name() + + "}", + ioe.getMessage() + ); assertThat(ioe.getCause(), instanceOf(IllegalStateException.class)); } } @@ -618,7 +738,10 @@ public void testWrapResponseListenerOnSuccess() { public void testWrapResponseListenerOnException() { TrackingActionListener trackingActionListener = new TrackingActionListener(); ResponseListener responseListener = restHighLevelClient.wrapResponseListener( - response -> response.getStatusLine().getStatusCode(), trackingActionListener, Collections.emptySet()); + response -> response.getStatusLine().getStatusCode(), + trackingActionListener, + Collections.emptySet() + ); IllegalStateException exception = new IllegalStateException(); responseListener.onFailure(exception); assertSame(exception, trackingActionListener.exception.get()); @@ -627,7 +750,10 @@ public void testWrapResponseListenerOnException() { public void testWrapResponseListenerOnResponseExceptionWithoutEntity() throws IOException { TrackingActionListener trackingActionListener = new TrackingActionListener(); ResponseListener responseListener = restHighLevelClient.wrapResponseListener( - response -> response.getStatusLine().getStatusCode(), trackingActionListener, Collections.emptySet()); + response -> response.getStatusLine().getStatusCode(), + trackingActionListener, + Collections.emptySet() + ); RestStatus restStatus = randomFrom(RestStatus.values()); HttpResponse httpResponse = new BasicHttpResponse(newStatusLine(restStatus)); Response response = new Response(REQUEST_LINE, new HttpHost("localhost", 9200), httpResponse); @@ -643,16 +769,20 @@ public void testWrapResponseListenerOnResponseExceptionWithoutEntity() throws IO public void testWrapResponseListenerOnResponseExceptionWithEntity() throws IOException { TrackingActionListener trackingActionListener = new TrackingActionListener(); ResponseListener responseListener = restHighLevelClient.wrapResponseListener( - response -> response.getStatusLine().getStatusCode(), trackingActionListener, Collections.emptySet()); + response -> response.getStatusLine().getStatusCode(), + trackingActionListener, + Collections.emptySet() + ); RestStatus restStatus = randomFrom(RestStatus.values()); HttpResponse httpResponse = new BasicHttpResponse(newStatusLine(restStatus)); - httpResponse.setEntity(new NStringEntity("{\"error\":\"test error message\",\"status\":" + restStatus.getStatus() + "}", - ContentType.APPLICATION_JSON)); + httpResponse.setEntity( + new NStringEntity("{\"error\":\"test error message\",\"status\":" + restStatus.getStatus() + "}", ContentType.APPLICATION_JSON) + ); Response response = new Response(REQUEST_LINE, new HttpHost("localhost", 9200), httpResponse); ResponseException responseException = new ResponseException(response); responseListener.onFailure(responseException); assertThat(trackingActionListener.exception.get(), instanceOf(ElasticsearchException.class)); - ElasticsearchException elasticsearchException = (ElasticsearchException)trackingActionListener.exception.get(); + ElasticsearchException elasticsearchException = (ElasticsearchException) trackingActionListener.exception.get(); assertEquals("Elasticsearch exception [type=exception, reason=test error message]", elasticsearchException.getMessage()); assertEquals(restStatus, elasticsearchException.status()); assertSame(responseException, elasticsearchException.getSuppressed()[0]); @@ -662,7 +792,10 @@ public void testWrapResponseListenerOnResponseExceptionWithBrokenEntity() throws { TrackingActionListener trackingActionListener = new TrackingActionListener(); ResponseListener responseListener = restHighLevelClient.wrapResponseListener( - response -> response.getStatusLine().getStatusCode(), trackingActionListener, Collections.emptySet()); + response -> response.getStatusLine().getStatusCode(), + trackingActionListener, + Collections.emptySet() + ); RestStatus restStatus = randomFrom(RestStatus.values()); HttpResponse httpResponse = new BasicHttpResponse(newStatusLine(restStatus)); httpResponse.setEntity(new NStringEntity("{\"error\":", ContentType.APPLICATION_JSON)); @@ -670,7 +803,7 @@ public void testWrapResponseListenerOnResponseExceptionWithBrokenEntity() throws ResponseException responseException = new ResponseException(response); responseListener.onFailure(responseException); assertThat(trackingActionListener.exception.get(), instanceOf(ElasticsearchException.class)); - ElasticsearchException elasticsearchException = (ElasticsearchException)trackingActionListener.exception.get(); + ElasticsearchException elasticsearchException = (ElasticsearchException) trackingActionListener.exception.get(); assertEquals("Unable to parse response body", elasticsearchException.getMessage()); assertEquals(restStatus, elasticsearchException.status()); assertSame(responseException, elasticsearchException.getCause()); @@ -679,7 +812,10 @@ public void testWrapResponseListenerOnResponseExceptionWithBrokenEntity() throws { TrackingActionListener trackingActionListener = new TrackingActionListener(); ResponseListener responseListener = restHighLevelClient.wrapResponseListener( - response -> response.getStatusLine().getStatusCode(), trackingActionListener, Collections.emptySet()); + response -> response.getStatusLine().getStatusCode(), + trackingActionListener, + Collections.emptySet() + ); RestStatus restStatus = randomFrom(RestStatus.values()); HttpResponse httpResponse = new BasicHttpResponse(newStatusLine(restStatus)); httpResponse.setEntity(new NStringEntity("{\"status\":" + restStatus.getStatus() + "}", ContentType.APPLICATION_JSON)); @@ -687,7 +823,7 @@ public void testWrapResponseListenerOnResponseExceptionWithBrokenEntity() throws ResponseException responseException = new ResponseException(response); responseListener.onFailure(responseException); assertThat(trackingActionListener.exception.get(), instanceOf(ElasticsearchException.class)); - ElasticsearchException elasticsearchException = (ElasticsearchException)trackingActionListener.exception.get(); + ElasticsearchException elasticsearchException = (ElasticsearchException) trackingActionListener.exception.get(); assertEquals("Unable to parse response body", elasticsearchException.getMessage()); assertEquals(restStatus, elasticsearchException.status()); assertSame(responseException, elasticsearchException.getCause()); @@ -698,28 +834,34 @@ public void testWrapResponseListenerOnResponseExceptionWithBrokenEntity() throws public void testWrapResponseListenerOnResponseExceptionWithIgnores() throws IOException { TrackingActionListener trackingActionListener = new TrackingActionListener(); ResponseListener responseListener = restHighLevelClient.wrapResponseListener( - response -> response.getStatusLine().getStatusCode(), trackingActionListener, Collections.singleton(404)); + response -> response.getStatusLine().getStatusCode(), + trackingActionListener, + Collections.singleton(404) + ); HttpResponse httpResponse = new BasicHttpResponse(newStatusLine(RestStatus.NOT_FOUND)); Response response = new Response(REQUEST_LINE, new HttpHost("localhost", 9200), httpResponse); ResponseException responseException = new ResponseException(response); responseListener.onFailure(responseException); - //although we got an exception, we turn it into a successful response because the status code was provided among ignores + // although we got an exception, we turn it into a successful response because the status code was provided among ignores assertNull(trackingActionListener.exception.get()); assertEquals(404, trackingActionListener.statusCode.get()); } public void testWrapResponseListenerOnResponseExceptionWithIgnoresErrorNoBody() throws IOException { TrackingActionListener trackingActionListener = new TrackingActionListener(); - //response parsing throws exception while handling ignores. same as when GetResponse#fromXContent throws error when trying - //to parse a 404 response which contains an error rather than a valid document not found response. + // response parsing throws exception while handling ignores. same as when GetResponse#fromXContent throws error when trying + // to parse a 404 response which contains an error rather than a valid document not found response. ResponseListener responseListener = restHighLevelClient.wrapResponseListener( - response -> { throw new IllegalStateException(); }, trackingActionListener, Collections.singleton(404)); + response -> { throw new IllegalStateException(); }, + trackingActionListener, + Collections.singleton(404) + ); HttpResponse httpResponse = new BasicHttpResponse(newStatusLine(RestStatus.NOT_FOUND)); Response response = new Response(REQUEST_LINE, new HttpHost("localhost", 9200), httpResponse); ResponseException responseException = new ResponseException(response); responseListener.onFailure(responseException); assertThat(trackingActionListener.exception.get(), instanceOf(ElasticsearchException.class)); - ElasticsearchException elasticsearchException = (ElasticsearchException)trackingActionListener.exception.get(); + ElasticsearchException elasticsearchException = (ElasticsearchException) trackingActionListener.exception.get(); assertEquals(RestStatus.NOT_FOUND, elasticsearchException.status()); assertSame(responseException, elasticsearchException.getCause()); assertEquals(responseException.getMessage(), elasticsearchException.getMessage()); @@ -727,18 +869,20 @@ public void testWrapResponseListenerOnResponseExceptionWithIgnoresErrorNoBody() public void testWrapResponseListenerOnResponseExceptionWithIgnoresErrorValidBody() throws IOException { TrackingActionListener trackingActionListener = new TrackingActionListener(); - //response parsing throws exception while handling ignores. same as when GetResponse#fromXContent throws error when trying - //to parse a 404 response which contains an error rather than a valid document not found response. + // response parsing throws exception while handling ignores. same as when GetResponse#fromXContent throws error when trying + // to parse a 404 response which contains an error rather than a valid document not found response. ResponseListener responseListener = restHighLevelClient.wrapResponseListener( - response -> { throw new IllegalStateException(); }, trackingActionListener, Collections.singleton(404)); + response -> { throw new IllegalStateException(); }, + trackingActionListener, + Collections.singleton(404) + ); HttpResponse httpResponse = new BasicHttpResponse(newStatusLine(RestStatus.NOT_FOUND)); - httpResponse.setEntity(new NStringEntity("{\"error\":\"test error message\",\"status\":404}", - ContentType.APPLICATION_JSON)); + httpResponse.setEntity(new NStringEntity("{\"error\":\"test error message\",\"status\":404}", ContentType.APPLICATION_JSON)); Response response = new Response(REQUEST_LINE, new HttpHost("localhost", 9200), httpResponse); ResponseException responseException = new ResponseException(response); responseListener.onFailure(responseException); assertThat(trackingActionListener.exception.get(), instanceOf(ElasticsearchException.class)); - ElasticsearchException elasticsearchException = (ElasticsearchException)trackingActionListener.exception.get(); + ElasticsearchException elasticsearchException = (ElasticsearchException) trackingActionListener.exception.get(); assertEquals(RestStatus.NOT_FOUND, elasticsearchException.status()); assertSame(responseException, elasticsearchException.getSuppressed()[0]); assertEquals("Elasticsearch exception [type=exception, reason=test error message]", elasticsearchException.getMessage()); @@ -821,7 +965,8 @@ public void testProvidedNamedXContents() { assertEquals(Integer.valueOf(3), categories.get(org.elasticsearch.client.ml.dataframe.evaluation.Evaluation.class)); assertThat(names, hasItems(OutlierDetection.NAME, Classification.NAME, Regression.NAME)); assertEquals(Integer.valueOf(13), categories.get(org.elasticsearch.client.ml.dataframe.evaluation.EvaluationMetric.class)); - assertThat(names, + assertThat( + names, hasItems( registeredMetricName(OutlierDetection.NAME, AucRocMetric.NAME), registeredMetricName(OutlierDetection.NAME, PrecisionMetric.NAME), @@ -830,16 +975,23 @@ public void testProvidedNamedXContents() { registeredMetricName(Classification.NAME, AucRocMetric.NAME), registeredMetricName(Classification.NAME, AccuracyMetric.NAME), registeredMetricName( - Classification.NAME, org.elasticsearch.client.ml.dataframe.evaluation.classification.PrecisionMetric.NAME), + Classification.NAME, + org.elasticsearch.client.ml.dataframe.evaluation.classification.PrecisionMetric.NAME + ), registeredMetricName( - Classification.NAME, org.elasticsearch.client.ml.dataframe.evaluation.classification.RecallMetric.NAME), + Classification.NAME, + org.elasticsearch.client.ml.dataframe.evaluation.classification.RecallMetric.NAME + ), registeredMetricName(Classification.NAME, MulticlassConfusionMatrixMetric.NAME), registeredMetricName(Regression.NAME, MeanSquaredErrorMetric.NAME), registeredMetricName(Regression.NAME, MeanSquaredLogarithmicErrorMetric.NAME), registeredMetricName(Regression.NAME, HuberMetric.NAME), - registeredMetricName(Regression.NAME, RSquaredMetric.NAME))); + registeredMetricName(Regression.NAME, RSquaredMetric.NAME) + ) + ); assertEquals(Integer.valueOf(13), categories.get(org.elasticsearch.client.ml.dataframe.evaluation.EvaluationMetric.Result.class)); - assertThat(names, + assertThat( + names, hasItems( registeredMetricName(OutlierDetection.NAME, AucRocMetric.NAME), registeredMetricName(OutlierDetection.NAME, PrecisionMetric.NAME), @@ -848,38 +1000,40 @@ public void testProvidedNamedXContents() { registeredMetricName(Classification.NAME, AucRocMetric.NAME), registeredMetricName(Classification.NAME, AccuracyMetric.NAME), registeredMetricName( - Classification.NAME, org.elasticsearch.client.ml.dataframe.evaluation.classification.PrecisionMetric.NAME), + Classification.NAME, + org.elasticsearch.client.ml.dataframe.evaluation.classification.PrecisionMetric.NAME + ), registeredMetricName( - Classification.NAME, org.elasticsearch.client.ml.dataframe.evaluation.classification.RecallMetric.NAME), + Classification.NAME, + org.elasticsearch.client.ml.dataframe.evaluation.classification.RecallMetric.NAME + ), registeredMetricName(Classification.NAME, MulticlassConfusionMatrixMetric.NAME), registeredMetricName(Regression.NAME, MeanSquaredErrorMetric.NAME), registeredMetricName(Regression.NAME, MeanSquaredLogarithmicErrorMetric.NAME), registeredMetricName(Regression.NAME, HuberMetric.NAME), - registeredMetricName(Regression.NAME, RSquaredMetric.NAME))); + registeredMetricName(Regression.NAME, RSquaredMetric.NAME) + ) + ); assertEquals(Integer.valueOf(6), categories.get(org.elasticsearch.client.ml.inference.preprocessing.PreProcessor.class)); - assertThat(names, - hasItems( - FrequencyEncoding.NAME, - OneHotEncoding.NAME, - TargetMeanEncoding.NAME, - CustomWordEmbedding.NAME, - NGram.NAME, - Multi.NAME - )); + assertThat( + names, + hasItems(FrequencyEncoding.NAME, OneHotEncoding.NAME, TargetMeanEncoding.NAME, CustomWordEmbedding.NAME, NGram.NAME, Multi.NAME) + ); assertEquals(Integer.valueOf(3), categories.get(org.elasticsearch.client.ml.inference.trainedmodel.TrainedModel.class)); assertThat(names, hasItems(Tree.NAME, Ensemble.NAME, LangIdentNeuralNetwork.NAME)); - assertEquals(Integer.valueOf(4), - categories.get(org.elasticsearch.client.ml.inference.trainedmodel.ensemble.OutputAggregator.class)); + assertEquals( + Integer.valueOf(4), + categories.get(org.elasticsearch.client.ml.inference.trainedmodel.ensemble.OutputAggregator.class) + ); assertThat(names, hasItems(WeightedMode.NAME, WeightedSum.NAME, LogisticRegression.NAME, Exponent.NAME)); - assertEquals(Integer.valueOf(2), - categories.get(org.elasticsearch.client.ml.inference.trainedmodel.InferenceConfig.class)); + assertEquals(Integer.valueOf(2), categories.get(org.elasticsearch.client.ml.inference.trainedmodel.InferenceConfig.class)); assertThat(names, hasItems(ClassificationConfig.NAME.getPreferredName(), RegressionConfig.NAME.getPreferredName())); } @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/70041") public void testApiNamingConventions() throws Exception { - //this list should be empty once the high-level client is feature complete - String[] notYetSupportedApi = new String[]{ + // this list should be empty once the high-level client is feature complete + String[] notYetSupportedApi = new String[] { "create", "get_script_context", "get_script_languages", @@ -891,9 +1045,8 @@ public void testApiNamingConventions() throws Exception { "indices.resolve_index", "indices.add_block", "open_point_in_time", - "close_point_in_time" - }; - //These API are not required for high-level client feature completeness + "close_point_in_time" }; + // These API are not required for high-level client feature completeness String[] notRequiredApi = new String[] { "cluster.allocation_explain", "cluster.pending_tasks", @@ -915,12 +1068,8 @@ public void testApiNamingConventions() throws Exception { "nodes.hot_threads", "nodes.usage", "nodes.reload_secure_settings", - "search_shards", - }; - List booleanReturnMethods = Arrays.asList( - "security.enable_user", - "security.disable_user", - "security.change_password"); + "search_shards", }; + List booleanReturnMethods = Arrays.asList("security.enable_user", "security.disable_user", "security.change_password"); Set deprecatedMethods = new HashSet<>(); deprecatedMethods.add("indices.force_merge"); deprecatedMethods.add("multi_get"); @@ -937,14 +1086,18 @@ public void testApiNamingConventions() throws Exception { topLevelMethodsExclusions.add("close"); Map> methods = Arrays.stream(RestHighLevelClient.class.getMethods()) - .filter(method -> method.getDeclaringClass().equals(RestHighLevelClient.class) - && topLevelMethodsExclusions.contains(method.getName()) == false) - .map(method -> Tuple.tuple(toSnakeCase(method.getName()), method)) - .flatMap(tuple -> tuple.v2().getReturnType().getName().endsWith("Client") - ? getSubClientMethods(tuple.v1(), tuple.v2().getReturnType()) : Stream.of(tuple)) - .filter(tuple -> tuple.v2().getAnnotation(Deprecated.class) == null) - .collect(Collectors.groupingBy(Tuple::v1, - Collectors.mapping(Tuple::v2, Collectors.toSet()))); + .filter( + method -> method.getDeclaringClass().equals(RestHighLevelClient.class) + && topLevelMethodsExclusions.contains(method.getName()) == false + ) + .map(method -> Tuple.tuple(toSnakeCase(method.getName()), method)) + .flatMap( + tuple -> tuple.v2().getReturnType().getName().endsWith("Client") + ? getSubClientMethods(tuple.v1(), tuple.v2().getReturnType()) + : Stream.of(tuple) + ) + .filter(tuple -> tuple.v2().getAnnotation(Deprecated.class) == null) + .collect(Collectors.groupingBy(Tuple::v1, Collectors.mapping(Tuple::v2, Collectors.toSet()))); // TODO remove in 8.0 - we will undeprecate indices.get_template because the current getIndexTemplate // impl will replace the existing getTemplate method. @@ -959,11 +1112,13 @@ public void testApiNamingConventions() throws Exception { String apiName = entry.getKey(); for (Method method : entry.getValue()) { - assertTrue("method [" + apiName + "] is not final", - Modifier.isFinal(method.getClass().getModifiers()) || Modifier.isFinal(method.getModifiers())); + assertTrue( + "method [" + apiName + "] is not final", + Modifier.isFinal(method.getClass().getModifiers()) || Modifier.isFinal(method.getModifiers()) + ); assertTrue("method [" + method + "] should be public", Modifier.isPublic(method.getModifiers())); - //we convert all the method names to snake case, hence we need to look for the '_async' suffix rather than 'Async' + // we convert all the method names to snake case, hence we need to look for the '_async' suffix rather than 'Async' if (apiName.endsWith("_async")) { assertAsyncMethod(methods, method, apiName); } else if (isSubmitTaskMethod(apiName)) { @@ -973,35 +1128,40 @@ public void testApiNamingConventions() throws Exception { apiUnsupported.remove(apiName); if (apiSpec.contains(apiName) == false) { if (deprecatedMethods.contains(apiName)) { - assertTrue("method [" + method.getName() + "], api [" + apiName + "] should be deprecated", - method.isAnnotationPresent(Deprecated.class)); + assertTrue( + "method [" + method.getName() + "], api [" + apiName + "] should be deprecated", + method.isAnnotationPresent(Deprecated.class) + ); } else { - //TODO xpack api are currently ignored, we need to load xpack yaml spec too - if (apiName.startsWith("xpack.") == false && - apiName.startsWith("license.") == false && - apiName.startsWith("machine_learning.") == false && - apiName.startsWith("rollup.") == false && - apiName.startsWith("watcher.") == false && - apiName.startsWith("graph.") == false && - apiName.startsWith("migration.") == false && - apiName.startsWith("security.") == false && - apiName.startsWith("index_lifecycle.") == false && - apiName.startsWith("ccr.") == false && - apiName.startsWith("enrich.") == false && - apiName.startsWith("transform.") == false && - apiName.startsWith("text_structure.") == false && - apiName.startsWith("searchable_snapshots.") == false && - apiName.startsWith("eql.") == false && - apiName.endsWith("freeze") == false && - apiName.endsWith("reload_analyzers") == false && - apiName.startsWith("async_search") == false && - // IndicesClientIT.getIndexTemplate should be renamed "getTemplate" in version 8.0 when we - // can get rid of 7.0's deprecated "getTemplate" - apiName.equals("indices.get_index_template") == false && - List.of("indices.data_streams_stats", + // TODO xpack api are currently ignored, we need to load xpack yaml spec too + if (apiName.startsWith("xpack.") == false + && apiName.startsWith("license.") == false + && apiName.startsWith("machine_learning.") == false + && apiName.startsWith("rollup.") == false + && apiName.startsWith("watcher.") == false + && apiName.startsWith("graph.") == false + && apiName.startsWith("migration.") == false + && apiName.startsWith("security.") == false + && apiName.startsWith("index_lifecycle.") == false + && apiName.startsWith("ccr.") == false + && apiName.startsWith("enrich.") == false + && apiName.startsWith("transform.") == false + && apiName.startsWith("text_structure.") == false + && apiName.startsWith("searchable_snapshots.") == false + && apiName.startsWith("eql.") == false + && apiName.endsWith("freeze") == false + && apiName.endsWith("reload_analyzers") == false + && apiName.startsWith("async_search") == false + && + // IndicesClientIT.getIndexTemplate should be renamed "getTemplate" in version 8.0 when we + // can get rid of 7.0's deprecated "getTemplate" + apiName.equals("indices.get_index_template") == false + && List.of( + "indices.data_streams_stats", "indices.delete_data_stream", "indices.create_data_stream", - "indices.get_data_stream").contains(apiName) == false) { + "indices.get_data_stream" + ).contains(apiName) == false) { apiNotFound.add(apiName); } } @@ -1009,19 +1169,26 @@ public void testApiNamingConventions() throws Exception { } } } - assertThat("Some client method doesn't match a corresponding API defined in the REST spec: " + apiNotFound, - apiNotFound.size(), equalTo(0)); + assertThat( + "Some client method doesn't match a corresponding API defined in the REST spec: " + apiNotFound, + apiNotFound.size(), + equalTo(0) + ); - //we decided not to support cat API in the high-level REST client, they are supposed to be used from a low-level client + // we decided not to support cat API in the high-level REST client, they are supposed to be used from a low-level client apiUnsupported.removeIf(api -> api.startsWith("cat.")); - Stream.concat(Arrays.stream(notYetSupportedApi), Arrays.stream(notRequiredApi)).forEach( - api -> assertTrue(api + " API is either not defined in the spec or already supported by the high-level client", - apiUnsupported.remove(api))); + Stream.concat(Arrays.stream(notYetSupportedApi), Arrays.stream(notRequiredApi)) + .forEach( + api -> assertTrue( + api + " API is either not defined in the spec or already supported by the high-level client", + apiUnsupported.remove(api) + ) + ); assertThat("Some API are not supported but they should be: " + apiUnsupported, apiUnsupported.size(), equalTo(0)); } - private static void doTestProductCompatibilityCheck( - boolean shouldBeAccepted, String version, boolean setProductHeader) throws Exception { + private static void doTestProductCompatibilityCheck(boolean shouldBeAccepted, String version, boolean setProductHeader) + throws Exception { // An endpoint different from "/" that returns a boolean GetSourceRequest apiRequest = new GetSourceRequest("foo", "bar"); @@ -1036,17 +1203,14 @@ private static void doTestProductCompatibilityCheck( Build build = new Build(Build.Flavor.DEFAULT, Build.Type.UNKNOWN, "hash", "date", false, version); mockGetRoot(restClient, build, setProductHeader); - when(restClient.performRequest(argThat(new RequestMatcher("HEAD", "/foo/_source/bar")::matches))) - .thenReturn(apiResponse); + when(restClient.performRequest(argThat(new RequestMatcher("HEAD", "/foo/_source/bar")::matches))).thenReturn(apiResponse); - RestHighLevelClient highLevelClient = new RestHighLevelClient(restClient, RestClient::close, Collections.emptyList()); + RestHighLevelClient highLevelClient = new RestHighLevelClient(restClient, RestClient::close, Collections.emptyList()); if (shouldBeAccepted) { assertTrue(highLevelClient.existsSource(apiRequest, RequestOptions.DEFAULT)); } else { - expectThrows(ElasticsearchException.class, () -> - highLevelClient.existsSource(apiRequest, RequestOptions.DEFAULT) - ); + expectThrows(ElasticsearchException.class, () -> highLevelClient.existsSource(apiRequest, RequestOptions.DEFAULT)); } } @@ -1084,35 +1248,31 @@ public void testProductCompatibilityTagline() throws Exception { when(apiStatus.getStatusCode()).thenReturn(200); Response apiResponse = mock(Response.class); when(apiResponse.getStatusLine()).thenReturn(apiStatus); - when(restClient.performRequest(argThat(new RequestMatcher("HEAD", "/foo/_source/bar")::matches))) - .thenReturn(apiResponse); + when(restClient.performRequest(argThat(new RequestMatcher("HEAD", "/foo/_source/bar")::matches))).thenReturn(apiResponse); RestHighLevelClient highLevelClient = new RestHighLevelClient(restClient, RestClient::close, Collections.emptyList()); - byte[] bytes = ("{" + - " 'cluster_name': '97b2b946a8494276822c3876d78d4f9c', " + - " 'cluster_uuid': 'SUXRYY1fQ5uMKEiykuR5ZA', " + - " 'version': { " + - " 'build_date': '2021-03-18T06:17:15.410153305Z', " + - " 'minimum_wire_compatibility_version': '6.8.0', " + - " 'build_hash': '78722783c38caa25a70982b5b042074cde5d3b3a', " + - " 'number': '7.12.0', " + - " 'lucene_version': '8.8.0', " + - " 'minimum_index_compatibility_version': '6.0.0-beta1', " + - " 'build_flavor': 'default', " + - " 'build_snapshot': false, " + - " 'build_type': 'docker' " + - " }, " + - " 'name': 'instance-0000000000', " + - " 'tagline': 'hello world'" + - "}" - ).replace('\'', '"').getBytes(StandardCharsets.UTF_8); + byte[] bytes = ("{" + + " 'cluster_name': '97b2b946a8494276822c3876d78d4f9c', " + + " 'cluster_uuid': 'SUXRYY1fQ5uMKEiykuR5ZA', " + + " 'version': { " + + " 'build_date': '2021-03-18T06:17:15.410153305Z', " + + " 'minimum_wire_compatibility_version': '6.8.0', " + + " 'build_hash': '78722783c38caa25a70982b5b042074cde5d3b3a', " + + " 'number': '7.12.0', " + + " 'lucene_version': '8.8.0', " + + " 'minimum_index_compatibility_version': '6.0.0-beta1', " + + " 'build_flavor': 'default', " + + " 'build_snapshot': false, " + + " 'build_type': 'docker' " + + " }, " + + " 'name': 'instance-0000000000', " + + " 'tagline': 'hello world'" + + "}").replace('\'', '"').getBytes(StandardCharsets.UTF_8); mockGetRoot(restClient, bytes, true); - expectThrows(ElasticsearchException.class, () -> - highLevelClient.existsSource(apiRequest, RequestOptions.DEFAULT) - ); + expectThrows(ElasticsearchException.class, () -> highLevelClient.existsSource(apiRequest, RequestOptions.DEFAULT)); } public void testProductCompatibilityFlavor() throws Exception { @@ -1123,37 +1283,33 @@ public void testProductCompatibilityFlavor() throws Exception { when(apiStatus.getStatusCode()).thenReturn(200); Response apiResponse = mock(Response.class); when(apiResponse.getStatusLine()).thenReturn(apiStatus); - when(restClient.performRequest(argThat(new RequestMatcher("HEAD", "/foo/_source/bar")::matches))) - .thenReturn(apiResponse); - - RestHighLevelClient highLevelClient = new RestHighLevelClient(restClient, RestClient::close, Collections.emptyList()); - - byte[] - bytes = ("{" + - " 'cluster_name': '97b2b946a8494276822c3876d78d4f9c', " + - " 'cluster_uuid': 'SUXRYY1fQ5uMKEiykuR5ZA', " + - " 'version': { " + - " 'build_date': '2021-03-18T06:17:15.410153305Z', " + - " 'minimum_wire_compatibility_version': '6.8.0', " + - " 'build_hash': '78722783c38caa25a70982b5b042074cde5d3b3a', " + - " 'number': '7.12.0', " + - " 'lucene_version': '8.8.0', " + - " 'minimum_index_compatibility_version': '6.0.0-beta1', " + + when(restClient.performRequest(argThat(new RequestMatcher("HEAD", "/foo/_source/bar")::matches))).thenReturn(apiResponse); + + RestHighLevelClient highLevelClient = new RestHighLevelClient(restClient, RestClient::close, Collections.emptyList()); + + byte[] bytes = ("{" + + " 'cluster_name': '97b2b946a8494276822c3876d78d4f9c', " + + " 'cluster_uuid': 'SUXRYY1fQ5uMKEiykuR5ZA', " + + " 'version': { " + + " 'build_date': '2021-03-18T06:17:15.410153305Z', " + + " 'minimum_wire_compatibility_version': '6.8.0', " + + " 'build_hash': '78722783c38caa25a70982b5b042074cde5d3b3a', " + + " 'number': '7.12.0', " + + " 'lucene_version': '8.8.0', " + + " 'minimum_index_compatibility_version': '6.0.0-beta1', " + + // Invalid flavor - " 'build_flavor': 'foo', " + - " 'build_snapshot': false, " + - " 'build_type': 'docker' " + - " }, " + - " 'name': 'instance-0000000000', " + - " 'tagline': 'You Know, for Search'" + - "}" - ).replace('\'', '"').getBytes(StandardCharsets.UTF_8); + " 'build_flavor': 'foo', " + + " 'build_snapshot': false, " + + " 'build_type': 'docker' " + + " }, " + + " 'name': 'instance-0000000000', " + + " 'tagline': 'You Know, for Search'" + + "}").replace('\'', '"').getBytes(StandardCharsets.UTF_8); mockGetRoot(restClient, bytes, true); - expectThrows(ElasticsearchException.class, () -> - highLevelClient.existsSource(apiRequest, RequestOptions.DEFAULT) - ); + expectThrows(ElasticsearchException.class, () -> highLevelClient.existsSource(apiRequest, RequestOptions.DEFAULT)); } public void testProductCompatibilityRequestFailure() throws Exception { @@ -1166,21 +1322,17 @@ public void testProductCompatibilityRequestFailure() throws Exception { when(apiStatus.getStatusCode()).thenReturn(200); Response apiResponse = mock(Response.class); when(apiResponse.getStatusLine()).thenReturn(apiStatus); - when(restClient.performRequest(argThat(new RequestMatcher("HEAD", "/foo/_source/bar")::matches))) - .thenReturn(apiResponse); + when(restClient.performRequest(argThat(new RequestMatcher("HEAD", "/foo/_source/bar")::matches))).thenReturn(apiResponse); // Have the verification request fail - when(restClient.performRequestAsync(argThat(new RequestMatcher("GET", "/")::matches), any())) - .thenAnswer(i -> { - ((ResponseListener)i.getArguments()[1]).onFailure(new IOException("Something bad happened")); - return Cancellable.NO_OP; - }); + when(restClient.performRequestAsync(argThat(new RequestMatcher("GET", "/")::matches), any())).thenAnswer(i -> { + ((ResponseListener) i.getArguments()[1]).onFailure(new IOException("Something bad happened")); + return Cancellable.NO_OP; + }); - RestHighLevelClient highLevelClient = new RestHighLevelClient(restClient, RestClient::close, Collections.emptyList()); + RestHighLevelClient highLevelClient = new RestHighLevelClient(restClient, RestClient::close, Collections.emptyList()); - expectThrows(ElasticsearchException.class, () -> { - highLevelClient.existsSource(apiRequest, RequestOptions.DEFAULT); - }); + expectThrows(ElasticsearchException.class, () -> { highLevelClient.existsSource(apiRequest, RequestOptions.DEFAULT); }); // Now have the validation request succeed Build build = new Build(Build.Flavor.DEFAULT, Build.Type.UNKNOWN, "hash", "date", false, "7.14.0"); @@ -1199,21 +1351,19 @@ public void testProductCompatibilityWithForbiddenInfoEndpoint() throws Exception when(apiStatus.getStatusCode()).thenReturn(200); Response apiResponse = mock(Response.class); when(apiResponse.getStatusLine()).thenReturn(apiStatus); - when(restClient.performRequest(argThat(new RequestMatcher("HEAD", "/foo/_source/bar")::matches))) - .thenReturn(apiResponse); + when(restClient.performRequest(argThat(new RequestMatcher("HEAD", "/foo/_source/bar")::matches))).thenReturn(apiResponse); // Have the info endpoint used for verification return a 403 (forbidden) - when(restClient.performRequestAsync(argThat(new RequestMatcher("GET", "/")::matches), any())) - .thenAnswer(i -> { - StatusLine infoStatus = mock(StatusLine.class); - when(apiStatus.getStatusCode()).thenReturn(HttpStatus.SC_FORBIDDEN); - Response infoResponse = mock(Response.class); - when(apiResponse.getStatusLine()).thenReturn(infoStatus); - ((ResponseListener)i.getArguments()[1]).onSuccess(infoResponse); - return Cancellable.NO_OP; - }); - - RestHighLevelClient highLevelClient = new RestHighLevelClient(restClient, RestClient::close, Collections.emptyList()); + when(restClient.performRequestAsync(argThat(new RequestMatcher("GET", "/")::matches), any())).thenAnswer(i -> { + StatusLine infoStatus = mock(StatusLine.class); + when(apiStatus.getStatusCode()).thenReturn(HttpStatus.SC_FORBIDDEN); + Response infoResponse = mock(Response.class); + when(apiResponse.getStatusLine()).thenReturn(infoStatus); + ((ResponseListener) i.getArguments()[1]).onSuccess(infoResponse); + return Cancellable.NO_OP; + }); + + RestHighLevelClient highLevelClient = new RestHighLevelClient(restClient, RestClient::close, Collections.emptyList()); // API request should succeed Build build = new Build(Build.Flavor.DEFAULT, Build.Type.UNKNOWN, "hash", "date", false, "7.14.0"); @@ -1226,12 +1376,14 @@ public void testCancellationForwarding() throws Exception { mockGetRoot(restClient); Cancellable cancellable = mock(Cancellable.class); - when(restClient.performRequestAsync(argThat(new RequestMatcher("HEAD", "/foo/_source/bar")::matches), any())) - .thenReturn(cancellable); + when(restClient.performRequestAsync(argThat(new RequestMatcher("HEAD", "/foo/_source/bar")::matches), any())).thenReturn( + cancellable + ); Cancellable result = restHighLevelClient.existsSourceAsync( new GetSourceRequest("foo", "bar"), - RequestOptions.DEFAULT, ActionListener.wrap(() -> {}) + RequestOptions.DEFAULT, + ActionListener.wrap(() -> {}) ); result.cancel(); @@ -1240,17 +1392,19 @@ public void testCancellationForwarding() throws Exception { public void testModifyHeader() { RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder(); - assertTrue(restHighLevelClient.modifyHeader(builder, - new BasicHeader("Content-Type", "application/json; Charset=UTF-16"), "Content-Type")); + assertTrue( + restHighLevelClient.modifyHeader(builder, new BasicHeader("Content-Type", "application/json; Charset=UTF-16"), "Content-Type") + ); - assertThat(builder.getHeaders().stream().map(h -> h.getName() + "=>" + h.getValue()).collect(Collectors.joining(",")), - containsString("Content-Type=>application/vnd.elasticsearch+json; compatible-with=7; Charset=UTF-16")); + assertThat( + builder.getHeaders().stream().map(h -> h.getName() + "=>" + h.getValue()).collect(Collectors.joining(",")), + containsString("Content-Type=>application/vnd.elasticsearch+json; compatible-with=7; Charset=UTF-16") + ); builder = RequestOptions.DEFAULT.toBuilder(); assertFalse(restHighLevelClient.modifyHeader(builder, new BasicHeader("Content-Type", "other"), "Content-Type")); - assertThat(builder.getHeaders().stream().map(h -> h.getName() + "=>" + h.getValue()).collect(Collectors.joining(",")), - equalTo("")); + assertThat(builder.getHeaders().stream().map(h -> h.getName() + "=>" + h.getValue()).collect(Collectors.joining(",")), equalTo("")); } public void testAddCompatibilityFor() { @@ -1260,25 +1414,31 @@ public void testAddCompatibilityFor() { // No request headers, use entity header assertTrue(restHighLevelClient.addCompatibilityFor(builder, entityHeader, headerName)); - assertThat(builder.getHeaders().stream().map(h -> h.getName() + "=>" + h.getValue()).collect(Collectors.joining(",")), - containsString("Content-Type=>application/vnd.elasticsearch+json; compatible-with=7")); + assertThat( + builder.getHeaders().stream().map(h -> h.getName() + "=>" + h.getValue()).collect(Collectors.joining(",")), + containsString("Content-Type=>application/vnd.elasticsearch+json; compatible-with=7") + ); // Request has a header, ignore entity header builder = RequestOptions.DEFAULT.toBuilder().addHeader("Content-Type", "application/yaml Charset=UTF-32"); assertTrue(restHighLevelClient.addCompatibilityFor(builder, entityHeader, headerName)); - assertThat(builder.getHeaders().stream().map(h -> h.getName() + "=>" + h.getValue()).collect(Collectors.joining(",")), - containsString("Content-Type=>application/vnd.elasticsearch+yaml; compatible-with=7 Charset=UTF-32")); + assertThat( + builder.getHeaders().stream().map(h -> h.getName() + "=>" + h.getValue()).collect(Collectors.joining(",")), + containsString("Content-Type=>application/vnd.elasticsearch+yaml; compatible-with=7 Charset=UTF-32") + ); // Request has no headers, and no entity, no changes builder = RequestOptions.DEFAULT.toBuilder(); assertFalse(restHighLevelClient.addCompatibilityFor(builder, null, headerName)); - assertThat(builder.getHeaders().stream().map(h -> h.getName() + "=>" + h.getValue()).collect(Collectors.joining(",")), - equalTo("")); + assertThat(builder.getHeaders().stream().map(h -> h.getName() + "=>" + h.getValue()).collect(Collectors.joining(",")), equalTo("")); } public void testModifyForCompatibility() { - final Function allHeaders = r -> - r.getOptions().getHeaders().stream().map(h -> h.getName() + "=>" + h.getValue()).collect(Collectors.joining(",")); + final Function allHeaders = r -> r.getOptions() + .getHeaders() + .stream() + .map(h -> h.getName() + "=>" + h.getValue()) + .collect(Collectors.joining(",")); Request req = new Request("POST", "/"); @@ -1291,9 +1451,13 @@ public void testModifyForCompatibility() { req.setEntity(new StringEntity("{}", ContentType.APPLICATION_JSON)); restHighLevelClient.modifyRequestForCompatibility(req); - assertThat(allHeaders.apply(req), - containsString("Content-Type=>application/vnd.elasticsearch+json; compatible-with=7; charset=UTF-8," + - "Accept=>application/vnd.elasticsearch+json; compatible-with=7")); + assertThat( + allHeaders.apply(req), + containsString( + "Content-Type=>application/vnd.elasticsearch+json; compatible-with=7; charset=UTF-8," + + "Accept=>application/vnd.elasticsearch+json; compatible-with=7" + ) + ); // With "Content-Type" headers already set req = new Request("POST", "/"); @@ -1301,63 +1465,96 @@ public void testModifyForCompatibility() { req.setOptions(RequestOptions.DEFAULT.toBuilder().addHeader("Content-Type", "application/json; Charset=UTF-16")); restHighLevelClient.modifyRequestForCompatibility(req); - assertThat(allHeaders.apply(req), - containsString("Content-Type=>application/vnd.elasticsearch+json; compatible-with=7; Charset=UTF-16," + - "Accept=>application/vnd.elasticsearch+json; compatible-with=7")); + assertThat( + allHeaders.apply(req), + containsString( + "Content-Type=>application/vnd.elasticsearch+json; compatible-with=7; Charset=UTF-16," + + "Accept=>application/vnd.elasticsearch+json; compatible-with=7" + ) + ); // With "Content-Type" and "Accept" headers already set req = new Request("POST", "/"); req.setEntity(new StringEntity("{}", ContentType.TEXT_PLAIN)); - req.setOptions(RequestOptions.DEFAULT.toBuilder() - .addHeader("Content-Type", "application/json; Charset=UTF-16") - .addHeader("Accept", "application/yaml; Charset=UTF-32")); + req.setOptions( + RequestOptions.DEFAULT.toBuilder() + .addHeader("Content-Type", "application/json; Charset=UTF-16") + .addHeader("Accept", "application/yaml; Charset=UTF-32") + ); restHighLevelClient.modifyRequestForCompatibility(req); - assertThat(allHeaders.apply(req), - containsString("Content-Type=>application/vnd.elasticsearch+json; compatible-with=7; Charset=UTF-16," + - "Accept=>application/vnd.elasticsearch+yaml; compatible-with=7; Charset=UTF-32")); + assertThat( + allHeaders.apply(req), + containsString( + "Content-Type=>application/vnd.elasticsearch+json; compatible-with=7; Charset=UTF-16," + + "Accept=>application/vnd.elasticsearch+yaml; compatible-with=7; Charset=UTF-32" + ) + ); } private static void assertSyncMethod(Method method, String apiName, List booleanReturnMethods) { - //A few methods return a boolean rather than a response object + // A few methods return a boolean rather than a response object if (apiName.equals("ping") || apiName.contains("exist") || booleanReturnMethods.contains(apiName)) { - assertThat("the return type for method [" + method + "] is incorrect", - method.getReturnType().getSimpleName(), equalTo("boolean")); + assertThat( + "the return type for method [" + method + "] is incorrect", + method.getReturnType().getSimpleName(), + equalTo("boolean") + ); } else { // It's acceptable for 404s to be represented as empty Optionals if (method.getReturnType().isAssignableFrom(Optional.class) == false) { - assertThat("the return type for method [" + method + "] is incorrect", - method.getReturnType().getSimpleName(), endsWith("Response")); + assertThat( + "the return type for method [" + method + "] is incorrect", + method.getReturnType().getSimpleName(), + endsWith("Response") + ); } } assertEquals("incorrect number of exceptions for method [" + method + "]", 1, method.getExceptionTypes().length); - //a few methods don't accept a request object as argument + // a few methods don't accept a request object as argument if (APIS_WITHOUT_REQUEST_OBJECT.contains(apiName)) { assertEquals("incorrect number of arguments for method [" + method + "]", 1, method.getParameterTypes().length); - assertThat("the parameter to method [" + method + "] is the wrong type", - method.getParameterTypes()[0], equalTo(RequestOptions.class)); + assertThat( + "the parameter to method [" + method + "] is the wrong type", + method.getParameterTypes()[0], + equalTo(RequestOptions.class) + ); } else { assertEquals("incorrect number of arguments for method [" + method + "]", 2, method.getParameterTypes().length); // This is no longer true for all methods. Some methods can contain these 2 args backwards because of deprecation if (method.getParameterTypes()[0].equals(RequestOptions.class)) { - assertThat("the first parameter to method [" + method + "] is the wrong type", - method.getParameterTypes()[0], equalTo(RequestOptions.class)); - assertThat("the second parameter to method [" + method + "] is the wrong type", - method.getParameterTypes()[1].getSimpleName(), endsWith("Request")); + assertThat( + "the first parameter to method [" + method + "] is the wrong type", + method.getParameterTypes()[0], + equalTo(RequestOptions.class) + ); + assertThat( + "the second parameter to method [" + method + "] is the wrong type", + method.getParameterTypes()[1].getSimpleName(), + endsWith("Request") + ); } else { - assertThat("the first parameter to method [" + method + "] is the wrong type", - method.getParameterTypes()[0].getSimpleName(), endsWith("Request")); - assertThat("the second parameter to method [" + method + "] is the wrong type", - method.getParameterTypes()[1], equalTo(RequestOptions.class)); + assertThat( + "the first parameter to method [" + method + "] is the wrong type", + method.getParameterTypes()[0].getSimpleName(), + endsWith("Request") + ); + assertThat( + "the second parameter to method [" + method + "] is the wrong type", + method.getParameterTypes()[1], + equalTo(RequestOptions.class) + ); } } } private static void assertAsyncMethod(Map> methods, Method method, String apiName) { - assertTrue("async method [" + method.getName() + "] doesn't have corresponding sync method", - methods.containsKey(apiName.substring(0, apiName.length() - 6))); + assertTrue( + "async method [" + method.getName() + "] doesn't have corresponding sync method", + methods.containsKey(apiName.substring(0, apiName.length() - 6)) + ); assertThat("async method [" + method + "] should return Cancellable", method.getReturnType(), equalTo(Cancellable.class)); assertEquals("async method [" + method + "] should not throw any exceptions", 0, method.getExceptionTypes().length); if (APIS_WITHOUT_REQUEST_OBJECT.contains(apiName.replaceAll("_async$", ""))) { @@ -1368,34 +1565,61 @@ private static void assertAsyncMethod(Map> methods, Method m assertEquals("async method [" + method + "] has the wrong number of arguments", 3, method.getParameterTypes().length); // This is no longer true for all methods. Some methods can contain these 2 args backwards because of deprecation if (method.getParameterTypes()[0].equals(RequestOptions.class)) { - assertThat("the first parameter to async method [" + method + "] should be a request type", - method.getParameterTypes()[0], equalTo(RequestOptions.class)); - assertThat("the second parameter to async method [" + method + "] is the wrong type", - method.getParameterTypes()[1].getSimpleName(), endsWith("Request")); + assertThat( + "the first parameter to async method [" + method + "] should be a request type", + method.getParameterTypes()[0], + equalTo(RequestOptions.class) + ); + assertThat( + "the second parameter to async method [" + method + "] is the wrong type", + method.getParameterTypes()[1].getSimpleName(), + endsWith("Request") + ); } else { - assertThat("the first parameter to async method [" + method + "] should be a request type", - method.getParameterTypes()[0].getSimpleName(), endsWith("Request")); - assertThat("the second parameter to async method [" + method + "] is the wrong type", - method.getParameterTypes()[1], equalTo(RequestOptions.class)); + assertThat( + "the first parameter to async method [" + method + "] should be a request type", + method.getParameterTypes()[0].getSimpleName(), + endsWith("Request") + ); + assertThat( + "the second parameter to async method [" + method + "] is the wrong type", + method.getParameterTypes()[1], + equalTo(RequestOptions.class) + ); } - assertThat("the third parameter to async method [" + method + "] is the wrong type", - method.getParameterTypes()[2], equalTo(ActionListener.class)); + assertThat( + "the third parameter to async method [" + method + "] is the wrong type", + method.getParameterTypes()[2], + equalTo(ActionListener.class) + ); } } - private static void assertSubmitTaskMethod(Map> methods, Method method, String apiName, - ClientYamlSuiteRestSpec restSpec) { + private static void assertSubmitTaskMethod( + Map> methods, + Method method, + String apiName, + ClientYamlSuiteRestSpec restSpec + ) { String methodName = extractMethodName(apiName); - assertTrue("submit task method [" + method.getName() + "] doesn't have corresponding sync method", - methods.containsKey(methodName)); + assertTrue("submit task method [" + method.getName() + "] doesn't have corresponding sync method", methods.containsKey(methodName)); assertEquals("submit task method [" + method + "] has the wrong number of arguments", 2, method.getParameterTypes().length); - assertThat("the first parameter to submit task method [" + method + "] is the wrong type", - method.getParameterTypes()[0].getSimpleName(), endsWith("Request")); - assertThat("the second parameter to submit task method [" + method + "] is the wrong type", - method.getParameterTypes()[1], equalTo(RequestOptions.class)); + assertThat( + "the first parameter to submit task method [" + method + "] is the wrong type", + method.getParameterTypes()[0].getSimpleName(), + endsWith("Request") + ); + assertThat( + "the second parameter to submit task method [" + method + "] is the wrong type", + method.getParameterTypes()[1], + equalTo(RequestOptions.class) + ); - assertThat("submit task method [" + method + "] must have wait_for_completion parameter in rest spec", - restSpec.getApi(methodName).getParams(), Matchers.hasKey("wait_for_completion")); + assertThat( + "submit task method [" + method + "] must have wait_for_completion parameter in rest spec", + restSpec.getApi(methodName).getParams(), + Matchers.hasKey("wait_for_completion") + ); } private static String extractMethodName(String apiName) { @@ -1407,10 +1631,14 @@ private static boolean isSubmitTaskMethod(String apiName) { } private static Stream> getSubClientMethods(String namespace, Class clientClass) { - return Arrays.stream(clientClass.getMethods()).filter(method -> method.getDeclaringClass().equals(clientClass)) - .map(method -> Tuple.tuple(namespace + "." + toSnakeCase(method.getName()), method)) - .flatMap(tuple -> tuple.v2().getReturnType().getName().endsWith("Client") - ? getSubClientMethods(tuple.v1(), tuple.v2().getReturnType()) : Stream.of(tuple)); + return Arrays.stream(clientClass.getMethods()) + .filter(method -> method.getDeclaringClass().equals(clientClass)) + .map(method -> Tuple.tuple(namespace + "." + toSnakeCase(method.getName()), method)) + .flatMap( + tuple -> tuple.v2().getReturnType().getName().endsWith("Client") + ? getSubClientMethods(tuple.v1(), tuple.v2().getReturnType()) + : Stream.of(tuple) + ); } private static String toSnakeCase(String camelCase) { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/RollupIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/RollupIT.java index 97b0194a7fc68..3d65a176a7475 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/RollupIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/RollupIT.java @@ -77,8 +77,13 @@ public class RollupIT extends ESRestHighLevelClientTestCase { double sum = 0.0d; int max = Integer.MIN_VALUE; int min = Integer.MAX_VALUE; - private static final List SUPPORTED_METRICS = Arrays.asList(MaxAggregationBuilder.NAME, MinAggregationBuilder.NAME, - SumAggregationBuilder.NAME, AvgAggregationBuilder.NAME, ValueCountAggregationBuilder.NAME); + private static final List SUPPORTED_METRICS = Arrays.asList( + MaxAggregationBuilder.NAME, + MinAggregationBuilder.NAME, + SumAggregationBuilder.NAME, + AvgAggregationBuilder.NAME, + ValueCountAggregationBuilder.NAME + ); private String id; private String indexPattern; @@ -105,11 +110,12 @@ public int indexDocs() throws Exception { final int value = randomIntBetween(0, 100); final IndexRequest indexRequest = new IndexRequest("docs"); - indexRequest.source(jsonBuilder() - .startObject() - .field("value", value) - .field("date", String.format(Locale.ROOT, "2018-01-01T00:%02d:%02dZ", minute, second)) - .endObject()); + indexRequest.source( + jsonBuilder().startObject() + .field("value", value) + .field("date", String.format(Locale.ROOT, "2018-01-01T00:%02d:%02dZ", minute, second)) + .endObject() + ); bulkRequest.add(indexRequest); sum += value; @@ -140,13 +146,13 @@ public int indexDocs() throws Exception { return numDocs; } - public void testDeleteRollupJob() throws Exception { final GroupConfig groups = new GroupConfig(new DateHistogramGroupConfig.CalendarInterval("date", DateHistogramInterval.DAY)); final List metrics = Collections.singletonList(new MetricConfig("value", SUPPORTED_METRICS)); final TimeValue timeout = TimeValue.timeValueSeconds(randomIntBetween(30, 600)); - PutRollupJobRequest putRollupJobRequest = - new PutRollupJobRequest(new RollupJobConfig(id, indexPattern, rollupIndex, cron, pageSize, groups, metrics, timeout)); + PutRollupJobRequest putRollupJobRequest = new PutRollupJobRequest( + new RollupJobConfig(id, indexPattern, rollupIndex, cron, pageSize, groups, metrics, timeout) + ); final RollupClient rollupClient = highLevelClient().rollup(); execute(putRollupJobRequest, rollupClient::putRollupJob, rollupClient::putRollupJobAsync); DeleteRollupJobRequest deleteRollupJobRequest = new DeleteRollupJobRequest(id); @@ -157,8 +163,10 @@ public void testDeleteRollupJob() throws Exception { public void testDeleteMissingRollupJob() { DeleteRollupJobRequest deleteRollupJobRequest = new DeleteRollupJobRequest(randomAlphaOfLength(10)); - ElasticsearchStatusException responseException = expectThrows(ElasticsearchStatusException.class,() -> highLevelClient().rollup() - .deleteRollupJob(deleteRollupJobRequest, RequestOptions.DEFAULT)); + ElasticsearchStatusException responseException = expectThrows( + ElasticsearchStatusException.class, + () -> highLevelClient().rollup().deleteRollupJob(deleteRollupJobRequest, RequestOptions.DEFAULT) + ); assertThat(responseException.status().getStatus(), is(404)); } @@ -168,8 +176,9 @@ public void testPutStartAndGetRollupJob() throws Exception { final List metrics = Collections.singletonList(new MetricConfig("value", SUPPORTED_METRICS)); final TimeValue timeout = TimeValue.timeValueSeconds(randomIntBetween(30, 600)); - PutRollupJobRequest putRollupJobRequest = - new PutRollupJobRequest(new RollupJobConfig(id, indexPattern, rollupIndex, cron, pageSize, groups, metrics, timeout)); + PutRollupJobRequest putRollupJobRequest = new PutRollupJobRequest( + new RollupJobConfig(id, indexPattern, rollupIndex, cron, pageSize, groups, metrics, timeout) + ); final RollupClient rollupClient = highLevelClient().rollup(); AcknowledgedResponse response = execute(putRollupJobRequest, rollupClient::putRollupJob, rollupClient::putRollupJobAsync); @@ -239,9 +248,7 @@ public void testPutStartAndGetRollupJob() throws Exception { public void testSearch() throws Exception { testPutStartAndGetRollupJob(); SearchRequest search = new SearchRequest(rollupIndex); - search.source(new SearchSourceBuilder() - .size(0) - .aggregation(new AvgAggregationBuilder("avg").field("value"))); + search.source(new SearchSourceBuilder().size(0).aggregation(new AvgAggregationBuilder("avg").field("value"))); SearchResponse response = highLevelClient().rollup().search(search, RequestOptions.DEFAULT); assertEquals(0, response.getFailedShards()); assertEquals(0, response.getHits().getTotalHits().value); @@ -269,11 +276,12 @@ public void testGetRollupCaps() throws Exception { final int value = randomIntBetween(0, 100); final IndexRequest indexRequest = new IndexRequest("docs"); - indexRequest.source(jsonBuilder() - .startObject() - .field("value", value) - .field("date", String.format(Locale.ROOT, "2018-01-01T00:%02d:%02dZ", minute, second)) - .endObject()); + indexRequest.source( + jsonBuilder().startObject() + .field("value", value) + .field("date", String.format(Locale.ROOT, "2018-01-01T00:%02d:%02dZ", minute, second)) + .endObject() + ); bulkRequest.add(indexRequest); values.add(value); @@ -291,7 +299,7 @@ public void testGetRollupCaps() throws Exception { BulkResponse bulkResponse = highLevelClient().bulk(bulkRequest, RequestOptions.DEFAULT); assertEquals(RestStatus.OK, bulkResponse.status()); - if (bulkResponse.hasFailures()) { + if (bulkResponse.hasFailures()) { for (BulkItemResponse itemResponse : bulkResponse.getItems()) { if (itemResponse.isFailed()) { logger.fatal(itemResponse.getFailureMessage()); @@ -313,8 +321,9 @@ public void testGetRollupCaps() throws Exception { final List metrics = Collections.singletonList(new MetricConfig("value", SUPPORTED_METRICS)); final TimeValue timeout = TimeValue.timeValueSeconds(randomIntBetween(30, 600)); - PutRollupJobRequest putRollupJobRequest = - new PutRollupJobRequest(new RollupJobConfig(id, indexPattern, rollupIndex, cron, pageSize, groups, metrics, timeout)); + PutRollupJobRequest putRollupJobRequest = new PutRollupJobRequest( + new RollupJobConfig(id, indexPattern, rollupIndex, cron, pageSize, groups, metrics, timeout) + ); final RollupClient rollupClient = highLevelClient().rollup(); AcknowledgedResponse response = execute(putRollupJobRequest, rollupClient::putRollupJob, rollupClient::putRollupJobAsync); @@ -324,8 +333,7 @@ public void testGetRollupCaps() throws Exception { highLevelClient().cluster().health(new ClusterHealthRequest(rollupIndex).waitForYellowStatus(), RequestOptions.DEFAULT); GetRollupCapsRequest getRollupCapsRequest = new GetRollupCapsRequest(indexPattern); - GetRollupCapsResponse capsResponse = highLevelClient().rollup() - .getRollupCapabilities(getRollupCapsRequest, RequestOptions.DEFAULT); + GetRollupCapsResponse capsResponse = highLevelClient().rollup().getRollupCapabilities(getRollupCapsRequest, RequestOptions.DEFAULT); assertNotNull(capsResponse); Map rolledPatterns = capsResponse.getJobs(); @@ -381,11 +389,12 @@ public void testGetRollupIndexCaps() throws Exception { final int value = randomIntBetween(0, 100); final IndexRequest indexRequest = new IndexRequest("docs"); - indexRequest.source(jsonBuilder() - .startObject() - .field("value", value) - .field("date", String.format(Locale.ROOT, "2018-01-01T00:%02d:%02dZ", minute, second)) - .endObject()); + indexRequest.source( + jsonBuilder().startObject() + .field("value", value) + .field("date", String.format(Locale.ROOT, "2018-01-01T00:%02d:%02dZ", minute, second)) + .endObject() + ); bulkRequest.add(indexRequest); values.add(value); @@ -403,7 +412,7 @@ public void testGetRollupIndexCaps() throws Exception { BulkResponse bulkResponse = highLevelClient().bulk(bulkRequest, RequestOptions.DEFAULT); assertEquals(RestStatus.OK, bulkResponse.status()); - if (bulkResponse.hasFailures()) { + if (bulkResponse.hasFailures()) { for (BulkItemResponse itemResponse : bulkResponse.getItems()) { if (itemResponse.isFailed()) { logger.fatal(itemResponse.getFailureMessage()); @@ -425,8 +434,9 @@ public void testGetRollupIndexCaps() throws Exception { final List metrics = Collections.singletonList(new MetricConfig("value", SUPPORTED_METRICS)); final TimeValue timeout = TimeValue.timeValueSeconds(randomIntBetween(30, 600)); - PutRollupJobRequest putRollupJobRequest = - new PutRollupJobRequest(new RollupJobConfig(id, indexPattern, rollupIndex, cron, pageSize, groups, metrics, timeout)); + PutRollupJobRequest putRollupJobRequest = new PutRollupJobRequest( + new RollupJobConfig(id, indexPattern, rollupIndex, cron, pageSize, groups, metrics, timeout) + ); final RollupClient rollupClient = highLevelClient().rollup(); AcknowledgedResponse response = execute(putRollupJobRequest, rollupClient::putRollupJob, rollupClient::putRollupJobAsync); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/SearchIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/SearchIT.java index fe685799fda1b..2fce3daffbdc5 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/SearchIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/SearchIT.java @@ -34,9 +34,6 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.query.MatchAllQueryBuilder; import org.elasticsearch.index.query.MatchQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; @@ -79,6 +76,9 @@ import org.elasticsearch.search.suggest.Suggest; import org.elasticsearch.search.suggest.SuggestBuilder; import org.elasticsearch.search.suggest.phrase.PhraseSuggestionBuilder; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.index.query.PinnedQueryBuilder; import org.hamcrest.Matchers; import org.junit.Before; @@ -90,11 +90,11 @@ import java.util.List; import java.util.Map; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertFirstHit; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSecondHit; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertToXContentEquivalent; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.hasId; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.Matchers.arrayContaining; import static org.hamcrest.Matchers.both; import static org.hamcrest.Matchers.containsString; @@ -140,15 +140,16 @@ public void indexDocuments() throws IOException { { Request create = new Request("PUT", "/index2"); create.setJsonEntity( - "{" + - " \"mappings\": {" + - " \"properties\": {" + - " \"rating\": {" + - " \"type\": \"keyword\"" + - " }" + - " }" + - " }" + - "}"); + "{" + + " \"mappings\": {" + + " \"properties\": {" + + " \"rating\": {" + + " \"type\": \"keyword\"" + + " }" + + " }" + + " }" + + "}" + ); client().performRequest(create); Request doc3 = new Request(HttpPut.METHOD_NAME, "/index2/_doc/3"); doc3.setJsonEntity("{\"id\":3, \"field\":\"value1\", \"rating\": \"good\"}"); @@ -170,20 +171,21 @@ public void indexDocuments() throws IOException { { Request create = new Request(HttpPut.METHOD_NAME, "/index4"); create.setJsonEntity( - "{" + - " \"mappings\": {" + - " \"properties\": {" + - " \"field1\": {" + - " \"type\": \"keyword\"," + - " \"store\": true" + - " }," + - " \"field2\": {" + - " \"type\": \"keyword\"," + - " \"store\": true" + - " }" + - " }" + - " }" + - "}"); + "{" + + " \"mappings\": {" + + " \"properties\": {" + + " \"field1\": {" + + " \"type\": \"keyword\"," + + " \"store\": true" + + " }," + + " \"field2\": {" + + " \"type\": \"keyword\"," + + " \"store\": true" + + " }" + + " }" + + " }" + + "}" + ); client().performRequest(create); Request doc1 = new Request(HttpPut.METHOD_NAME, "/index4/_doc/1"); doc1.setJsonEntity("{\"id\":1, \"field1\":\"value1\", \"field2\":\"value2\"}"); @@ -191,17 +193,18 @@ public void indexDocuments() throws IOException { Request createFilteredAlias = new Request(HttpPost.METHOD_NAME, "/_aliases"); createFilteredAlias.setJsonEntity( - "{" + - " \"actions\" : [" + - " {" + - " \"add\" : {" + - " \"index\" : \"index4\"," + - " \"alias\" : \"alias4\"," + - " \"filter\" : { \"term\" : { \"field2\" : \"value1\" } }" + - " }" + - " }" + - " ]" + - "}"); + "{" + + " \"actions\" : [" + + " {" + + " \"add\" : {" + + " \"index\" : \"index4\"," + + " \"alias\" : \"alias4\"," + + " \"filter\" : { \"term\" : { \"field2\" : \"value1\" } }" + + " }" + + " }" + + " ]" + + "}" + ); client().performRequest(createFilteredAlias); } @@ -255,8 +258,7 @@ public void testSearchMatchQuery() throws IOException { public void testSearchWithTermsAgg() throws IOException { SearchRequest searchRequest = new SearchRequest(); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); - searchSourceBuilder.aggregation(new TermsAggregationBuilder("agg1").userValueTypeHint(ValueType.STRING) - .field("type.keyword")); + searchSourceBuilder.aggregation(new TermsAggregationBuilder("agg1").userValueTypeHint(ValueType.STRING).field("type.keyword")); searchSourceBuilder.size(0); searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = execute(searchRequest, highLevelClient()::search, highLevelClient()::searchAsync); @@ -279,8 +281,9 @@ public void testSearchWithTermsAgg() throws IOException { public void testSearchWithRareTermsAgg() throws IOException { SearchRequest searchRequest = new SearchRequest(); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); - searchSourceBuilder.aggregation(new RareTermsAggregationBuilder("agg1").userValueTypeHint(ValueType.STRING) - .field("type.keyword").maxDocCount(2)); + searchSourceBuilder.aggregation( + new RareTermsAggregationBuilder("agg1").userValueTypeHint(ValueType.STRING).field("type.keyword").maxDocCount(2) + ); searchSourceBuilder.size(0); searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = execute(searchRequest, highLevelClient()::search, highLevelClient()::searchAsync); @@ -299,8 +302,9 @@ public void testSearchWithRareTermsAgg() throws IOException { public void testSearchWithCompositeAgg() throws IOException { SearchRequest searchRequest = new SearchRequest(); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); - List> sources - = Collections.singletonList(new TermsValuesSourceBuilder("terms").field("type.keyword").missingBucket(true).order("asc")); + List> sources = Collections.singletonList( + new TermsValuesSourceBuilder("terms").field("type.keyword").missingBucket(true).order("asc") + ); searchSourceBuilder.aggregation(AggregationBuilders.composite("composite", sources)); searchSourceBuilder.size(0); searchRequest.source(searchSourceBuilder); @@ -332,15 +336,18 @@ public void testSearchWithRangeAgg() throws IOException { searchSourceBuilder.size(0); searchRequest.source(searchSourceBuilder); - ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, - () -> execute(searchRequest, highLevelClient()::search, highLevelClient()::searchAsync)); + ElasticsearchStatusException exception = expectThrows( + ElasticsearchStatusException.class, + () -> execute(searchRequest, highLevelClient()::search, highLevelClient()::searchAsync) + ); assertEquals(RestStatus.BAD_REQUEST, exception.status()); } SearchRequest searchRequest = new SearchRequest("index"); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); - searchSourceBuilder.aggregation(new RangeAggregationBuilder("agg1").field("num") - .addRange("first", 0, 30).addRange("second", 31, 200)); + searchSourceBuilder.aggregation( + new RangeAggregationBuilder("agg1").field("num").addRange("first", 0, 30).addRange("second", 31, 200) + ); searchSourceBuilder.size(0); searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = execute(searchRequest, highLevelClient()::search, highLevelClient()::searchAsync); @@ -369,8 +376,7 @@ public void testSearchWithTermsAndRangeAgg() throws IOException { SearchRequest searchRequest = new SearchRequest("index"); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); TermsAggregationBuilder agg = new TermsAggregationBuilder("agg1").userValueTypeHint(ValueType.STRING).field("type.keyword"); - agg.subAggregation(new RangeAggregationBuilder("subagg").field("num") - .addRange("first", 0, 30).addRange("second", 31, 200)); + agg.subAggregation(new RangeAggregationBuilder("subagg").field("num").addRange("first", 0, 30).addRange("second", 31, 200)); searchSourceBuilder.aggregation(agg); searchSourceBuilder.size(0); searchRequest.source(searchSourceBuilder); @@ -423,9 +429,9 @@ public void testSearchWithTermsAndWeightedAvg() throws IOException { SearchRequest searchRequest = new SearchRequest("index"); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); TermsAggregationBuilder agg = new TermsAggregationBuilder("agg1").userValueTypeHint(ValueType.STRING).field("type.keyword"); - agg.subAggregation(new WeightedAvgAggregationBuilder("subagg") - .value(new MultiValuesSourceFieldConfig.Builder().setFieldName("num").build()) - .weight(new MultiValuesSourceFieldConfig.Builder().setFieldName("num2").build()) + agg.subAggregation( + new WeightedAvgAggregationBuilder("subagg").value(new MultiValuesSourceFieldConfig.Builder().setFieldName("num").build()) + .weight(new MultiValuesSourceFieldConfig.Builder().setFieldName("num2").build()) ); searchSourceBuilder.aggregation(agg); searchSourceBuilder.size(0); @@ -488,73 +494,78 @@ public void testSearchWithParentJoin() throws IOException { final String indexName = "child_example"; Request createIndex = new Request(HttpPut.METHOD_NAME, "/" + indexName); createIndex.setJsonEntity( - "{\n" + - " \"mappings\": {\n" + - " \"properties\" : {\n" + - " \"qa_join_field\" : {\n" + - " \"type\" : \"join\",\n" + - " \"relations\" : { \"question\" : \"answer\" }\n" + - " }\n" + - " }\n" + - " }" + - "}"); + "{\n" + + " \"mappings\": {\n" + + " \"properties\" : {\n" + + " \"qa_join_field\" : {\n" + + " \"type\" : \"join\",\n" + + " \"relations\" : { \"question\" : \"answer\" }\n" + + " }\n" + + " }\n" + + " }" + + "}" + ); client().performRequest(createIndex); Request questionDoc = new Request(HttpPut.METHOD_NAME, "/" + indexName + "/_doc/1"); questionDoc.setJsonEntity( - "{\n" + - " \"body\": \"

    I have Windows 2003 server and i bought a new Windows 2008 server...\",\n" + - " \"title\": \"Whats the best way to file transfer my site from server to a newer one?\",\n" + - " \"tags\": [\n" + - " \"windows-server-2003\",\n" + - " \"windows-server-2008\",\n" + - " \"file-transfer\"\n" + - " ],\n" + - " \"qa_join_field\" : \"question\"\n" + - "}"); + "{\n" + + " \"body\": \"

    I have Windows 2003 server and i bought a new Windows 2008 server...\",\n" + + " \"title\": \"Whats the best way to file transfer my site from server to a newer one?\",\n" + + " \"tags\": [\n" + + " \"windows-server-2003\",\n" + + " \"windows-server-2008\",\n" + + " \"file-transfer\"\n" + + " ],\n" + + " \"qa_join_field\" : \"question\"\n" + + "}" + ); client().performRequest(questionDoc); Request answerDoc1 = new Request(HttpPut.METHOD_NAME, "/" + indexName + "/_doc/2"); answerDoc1.addParameter("routing", "1"); answerDoc1.setJsonEntity( - "{\n" + - " \"owner\": {\n" + - " \"location\": \"Norfolk, United Kingdom\",\n" + - " \"display_name\": \"Sam\",\n" + - " \"id\": 48\n" + - " },\n" + - " \"body\": \"

    Unfortunately you're pretty much limited to FTP...\",\n" + - " \"qa_join_field\" : {\n" + - " \"name\" : \"answer\",\n" + - " \"parent\" : \"1\"\n" + - " },\n" + - " \"creation_date\": \"2009-05-04T13:45:37.030\"\n" + - "}"); + "{\n" + + " \"owner\": {\n" + + " \"location\": \"Norfolk, United Kingdom\",\n" + + " \"display_name\": \"Sam\",\n" + + " \"id\": 48\n" + + " },\n" + + " \"body\": \"

    Unfortunately you're pretty much limited to FTP...\",\n" + + " \"qa_join_field\" : {\n" + + " \"name\" : \"answer\",\n" + + " \"parent\" : \"1\"\n" + + " },\n" + + " \"creation_date\": \"2009-05-04T13:45:37.030\"\n" + + "}" + ); client().performRequest(answerDoc1); Request answerDoc2 = new Request(HttpPut.METHOD_NAME, "/" + indexName + "/_doc/3"); answerDoc2.addParameter("routing", "1"); answerDoc2.setJsonEntity( - "{\n" + - " \"owner\": {\n" + - " \"location\": \"Norfolk, United Kingdom\",\n" + - " \"display_name\": \"Troll\",\n" + - " \"id\": 49\n" + - " },\n" + - " \"body\": \"

    Use Linux...\",\n" + - " \"qa_join_field\" : {\n" + - " \"name\" : \"answer\",\n" + - " \"parent\" : \"1\"\n" + - " },\n" + - " \"creation_date\": \"2009-05-05T13:45:37.030\"\n" + - "}"); + "{\n" + + " \"owner\": {\n" + + " \"location\": \"Norfolk, United Kingdom\",\n" + + " \"display_name\": \"Troll\",\n" + + " \"id\": 49\n" + + " },\n" + + " \"body\": \"

    Use Linux...\",\n" + + " \"qa_join_field\" : {\n" + + " \"name\" : \"answer\",\n" + + " \"parent\" : \"1\"\n" + + " },\n" + + " \"creation_date\": \"2009-05-05T13:45:37.030\"\n" + + "}" + ); client().performRequest(answerDoc2); client().performRequest(new Request(HttpPost.METHOD_NAME, "/_refresh")); - TermsAggregationBuilder leafTermAgg = new TermsAggregationBuilder("top-names") - .userValueTypeHint(ValueType.STRING) - .field("owner.display_name.keyword").size(10); + TermsAggregationBuilder leafTermAgg = new TermsAggregationBuilder("top-names").userValueTypeHint(ValueType.STRING) + .field("owner.display_name.keyword") + .size(10); ChildrenAggregationBuilder childrenAgg = new ChildrenAggregationBuilder("to-answers", "answer").subAggregation(leafTermAgg); TermsAggregationBuilder termsAgg = new TermsAggregationBuilder("top-tags").userValueTypeHint(ValueType.STRING) - .field("tags.keyword") - .size(10).subAggregation(childrenAgg); + .field("tags.keyword") + .size(10) + .subAggregation(childrenAgg); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.size(0).aggregation(termsAgg); SearchRequest searchRequest = new SearchRequest(indexName); @@ -573,8 +584,10 @@ public void testSearchWithParentJoin() throws IOException { assertEquals(0, terms.getSumOfOtherDocCounts()); assertEquals(3, terms.getBuckets().size()); for (Terms.Bucket bucket : terms.getBuckets()) { - assertThat(bucket.getKeyAsString(), - either(equalTo("file-transfer")).or(equalTo("windows-server-2003")).or(equalTo("windows-server-2008"))); + assertThat( + bucket.getKeyAsString(), + either(equalTo("file-transfer")).or(equalTo("windows-server-2003")).or(equalTo("windows-server-2008")) + ); assertEquals(1, bucket.getDocCount()); assertEquals(1, bucket.getAggregations().asList().size()); Children children = bucket.getAggregations().get("to-answers"); @@ -595,8 +608,7 @@ public void testSearchWithParentJoin() throws IOException { public void testSearchWithSuggest() throws IOException { SearchRequest searchRequest = new SearchRequest("index"); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); - searchSourceBuilder.suggest(new SuggestBuilder().addSuggestion("sugg1", new PhraseSuggestionBuilder("type")) - .setGlobalText("type")); + searchSourceBuilder.suggest(new SuggestBuilder().addSuggestion("sugg1", new PhraseSuggestionBuilder("type")).setGlobalText("type")); searchSourceBuilder.size(0); searchRequest.source(searchSourceBuilder); @@ -609,14 +621,15 @@ public void testSearchWithSuggest() throws IOException { assertEquals(0, searchResponse.getHits().getHits().length); assertEquals(1, searchResponse.getSuggest().size()); - Suggest.Suggestion> sugg = searchResponse - .getSuggest().iterator().next(); + Suggest.Suggestion> sugg = searchResponse.getSuggest() + .iterator() + .next(); assertEquals("sugg1", sugg.getName()); for (Suggest.Suggestion.Entry options : sugg) { assertEquals("type", options.getText().string()); assertEquals(0, options.getOffset()); assertEquals(4, options.getLength()); - assertEquals(2 ,options.getOptions().size()); + assertEquals(2, options.getOptions().size()); for (Suggest.Suggestion.Entry.Option option : options) { assertThat(option.getScore(), greaterThan(0f)); assertThat(option.getText().string(), either(equalTo("type1")).or(equalTo("type2"))); @@ -631,8 +644,9 @@ public void testSearchWithWeirdScriptFields() throws Exception { client().performRequest(new Request("POST", "/test/_refresh")); { - SearchRequest searchRequest = new SearchRequest("test").source(SearchSourceBuilder.searchSource() - .scriptField("result", new Script("null"))); + SearchRequest searchRequest = new SearchRequest("test").source( + SearchSourceBuilder.searchSource().scriptField("result", new Script("null")) + ); SearchResponse searchResponse = execute(searchRequest, highLevelClient()::search, highLevelClient()::searchAsync); SearchHit searchHit = searchResponse.getHits().getAt(0); List values = searchHit.getFields().get("result").getValues(); @@ -641,8 +655,9 @@ public void testSearchWithWeirdScriptFields() throws Exception { assertNull(values.get(0)); } { - SearchRequest searchRequest = new SearchRequest("test").source(SearchSourceBuilder.searchSource() - .scriptField("result", new Script("new HashMap()"))); + SearchRequest searchRequest = new SearchRequest("test").source( + SearchSourceBuilder.searchSource().scriptField("result", new Script("new HashMap()")) + ); SearchResponse searchResponse = execute(searchRequest, highLevelClient()::search, highLevelClient()::searchAsync); SearchHit searchHit = searchResponse.getHits().getAt(0); List values = searchHit.getFields().get("result").getValues(); @@ -653,8 +668,9 @@ public void testSearchWithWeirdScriptFields() throws Exception { assertEquals(0, map.size()); } { - SearchRequest searchRequest = new SearchRequest("test").source(SearchSourceBuilder.searchSource() - .scriptField("result", new Script("new String[]{}"))); + SearchRequest searchRequest = new SearchRequest("test").source( + SearchSourceBuilder.searchSource().scriptField("result", new Script("new String[]{}")) + ); SearchResponse searchResponse = execute(searchRequest, highLevelClient()::search, highLevelClient()::searchAsync); SearchHit searchHit = searchResponse.getHits().getAt(0); List values = searchHit.getFields().get("result").getValues(); @@ -688,8 +704,11 @@ public void testSearchScroll() throws Exception { assertThat(((Number) hit.getSortValues()[0]).longValue(), equalTo(counter++)); } - searchResponse = execute(new SearchScrollRequest(searchResponse.getScrollId()).scroll(TimeValue.timeValueMinutes(2)), - highLevelClient()::scroll, highLevelClient()::scrollAsync); + searchResponse = execute( + new SearchScrollRequest(searchResponse.getScrollId()).scroll(TimeValue.timeValueMinutes(2)), + highLevelClient()::scroll, + highLevelClient()::scrollAsync + ); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(100L)); assertThat(searchResponse.getHits().getHits().length, equalTo(35)); @@ -697,8 +716,11 @@ public void testSearchScroll() throws Exception { assertEquals(counter++, ((Number) hit.getSortValues()[0]).longValue()); } - searchResponse = execute(new SearchScrollRequest(searchResponse.getScrollId()).scroll(TimeValue.timeValueMinutes(2)), - highLevelClient()::scroll, highLevelClient()::scrollAsync); + searchResponse = execute( + new SearchScrollRequest(searchResponse.getScrollId()).scroll(TimeValue.timeValueMinutes(2)), + highLevelClient()::scroll, + highLevelClient()::scrollAsync + ); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(100L)); assertThat(searchResponse.getHits().getHits().length, equalTo(30)); @@ -708,14 +730,19 @@ public void testSearchScroll() throws Exception { } finally { ClearScrollRequest clearScrollRequest = new ClearScrollRequest(); clearScrollRequest.addScrollId(searchResponse.getScrollId()); - ClearScrollResponse clearScrollResponse = execute(clearScrollRequest, - highLevelClient()::clearScroll, highLevelClient()::clearScrollAsync); + ClearScrollResponse clearScrollResponse = execute( + clearScrollRequest, + highLevelClient()::clearScroll, + highLevelClient()::clearScrollAsync + ); assertThat(clearScrollResponse.getNumFreed(), greaterThan(0)); assertTrue(clearScrollResponse.isSucceeded()); SearchScrollRequest scrollRequest = new SearchScrollRequest(searchResponse.getScrollId()).scroll(TimeValue.timeValueMinutes(2)); - ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, () -> execute(scrollRequest, - highLevelClient()::scroll, highLevelClient()::scrollAsync)); + ElasticsearchStatusException exception = expectThrows( + ElasticsearchStatusException.class, + () -> execute(scrollRequest, highLevelClient()::scroll, highLevelClient()::scrollAsync) + ); assertEquals(RestStatus.NOT_FOUND, exception.status()); assertThat(exception.getRootCause(), instanceOf(ElasticsearchException.class)); ElasticsearchException rootCause = (ElasticsearchException) exception.getRootCause(); @@ -735,8 +762,7 @@ public void testMultiSearch() throws Exception { searchRequest3.source().sort("id", SortOrder.ASC); multiSearchRequest.add(searchRequest3); - MultiSearchResponse multiSearchResponse = - execute(multiSearchRequest, highLevelClient()::msearch, highLevelClient()::msearchAsync); + MultiSearchResponse multiSearchResponse = execute(multiSearchRequest, highLevelClient()::msearch, highLevelClient()::msearchAsync); assertThat(multiSearchResponse.getTook().millis(), Matchers.greaterThanOrEqualTo(0L)); assertThat(multiSearchResponse.getResponses().length, Matchers.equalTo(3)); @@ -765,23 +791,28 @@ public void testMultiSearch() throws Exception { public void testMultiSearch_withAgg() throws Exception { MultiSearchRequest multiSearchRequest = new MultiSearchRequest(); SearchRequest searchRequest1 = new SearchRequest("index1"); - searchRequest1.source().size(0).aggregation(new TermsAggregationBuilder("name").userValueTypeHint(ValueType.STRING) - .field("field.keyword") - .order(BucketOrder.key(true))); + searchRequest1.source() + .size(0) + .aggregation( + new TermsAggregationBuilder("name").userValueTypeHint(ValueType.STRING).field("field.keyword").order(BucketOrder.key(true)) + ); multiSearchRequest.add(searchRequest1); SearchRequest searchRequest2 = new SearchRequest("index2"); - searchRequest2.source().size(0).aggregation(new TermsAggregationBuilder("name").userValueTypeHint(ValueType.STRING) - .field("field.keyword") - .order(BucketOrder.key(true))); + searchRequest2.source() + .size(0) + .aggregation( + new TermsAggregationBuilder("name").userValueTypeHint(ValueType.STRING).field("field.keyword").order(BucketOrder.key(true)) + ); multiSearchRequest.add(searchRequest2); SearchRequest searchRequest3 = new SearchRequest("index3"); - searchRequest3.source().size(0).aggregation(new TermsAggregationBuilder("name").userValueTypeHint(ValueType.STRING) - .field("field.keyword") - .order(BucketOrder.key(true))); + searchRequest3.source() + .size(0) + .aggregation( + new TermsAggregationBuilder("name").userValueTypeHint(ValueType.STRING).field("field.keyword").order(BucketOrder.key(true)) + ); multiSearchRequest.add(searchRequest3); - MultiSearchResponse multiSearchResponse = - execute(multiSearchRequest, highLevelClient()::msearch, highLevelClient()::msearchAsync); + MultiSearchResponse multiSearchResponse = execute(multiSearchRequest, highLevelClient()::msearch, highLevelClient()::msearchAsync); assertThat(multiSearchResponse.getTook().millis(), Matchers.greaterThanOrEqualTo(0L)); assertThat(multiSearchResponse.getResponses().length, Matchers.equalTo(3)); @@ -828,8 +859,7 @@ public void testMultiSearch_withQuery() throws Exception { searchRequest3.source().query(new TermsQueryBuilder("field", "value2")); multiSearchRequest.add(searchRequest3); - MultiSearchResponse multiSearchResponse = - execute(multiSearchRequest, highLevelClient()::msearch, highLevelClient()::msearchAsync); + MultiSearchResponse multiSearchResponse = execute(multiSearchRequest, highLevelClient()::msearch, highLevelClient()::msearchAsync); assertThat(multiSearchResponse.getTook().millis(), Matchers.greaterThanOrEqualTo(0L)); assertThat(multiSearchResponse.getResponses().length, Matchers.equalTo(3)); @@ -862,24 +892,33 @@ public void testMultiSearch_withQuery() throws Exception { assertThat(multiSearchResponse.getResponses()[0].isFailure(), Matchers.is(false)); SearchIT.assertSearchHeader(multiSearchResponse.getResponses()[0].getResponse()); assertThat(multiSearchResponse.getResponses()[0].getResponse().getHits().getTotalHits().value, Matchers.equalTo(1L)); - assertThat(multiSearchResponse.getResponses()[0].getResponse().getHits().getAt(0).getHighlightFields() - .get("field").fragments()[0].string(), Matchers.equalTo("value2")); + assertThat( + multiSearchResponse.getResponses()[0].getResponse().getHits().getAt(0).getHighlightFields().get("field").fragments()[0] + .string(), + Matchers.equalTo("value2") + ); assertThat(multiSearchResponse.getResponses()[1].getFailure(), Matchers.nullValue()); assertThat(multiSearchResponse.getResponses()[1].isFailure(), Matchers.is(false)); SearchIT.assertSearchHeader(multiSearchResponse.getResponses()[1].getResponse()); assertThat(multiSearchResponse.getResponses()[1].getResponse().getHits().getTotalHits().value, Matchers.equalTo(1L)); assertThat(multiSearchResponse.getResponses()[1].getResponse().getHits().getAt(0).getId(), Matchers.equalTo("4")); - assertThat(multiSearchResponse.getResponses()[1].getResponse().getHits().getAt(0).getHighlightFields() - .get("field").fragments()[0].string(), Matchers.equalTo("value2")); + assertThat( + multiSearchResponse.getResponses()[1].getResponse().getHits().getAt(0).getHighlightFields().get("field").fragments()[0] + .string(), + Matchers.equalTo("value2") + ); assertThat(multiSearchResponse.getResponses()[2].getFailure(), Matchers.nullValue()); assertThat(multiSearchResponse.getResponses()[2].isFailure(), Matchers.is(false)); SearchIT.assertSearchHeader(multiSearchResponse.getResponses()[2].getResponse()); assertThat(multiSearchResponse.getResponses()[2].getResponse().getHits().getTotalHits().value, Matchers.equalTo(1L)); assertThat(multiSearchResponse.getResponses()[2].getResponse().getHits().getAt(0).getId(), Matchers.equalTo("6")); - assertThat(multiSearchResponse.getResponses()[2].getResponse().getHits().getAt(0).getHighlightFields() - .get("field").fragments()[0].string(), Matchers.equalTo("value2")); + assertThat( + multiSearchResponse.getResponses()[2].getResponse().getHits().getAt(0).getHighlightFields().get("field").fragments()[0] + .string(), + Matchers.equalTo("value2") + ); } public void testMultiSearch_failure() throws Exception { @@ -891,8 +930,7 @@ public void testMultiSearch_failure() throws Exception { searchRequest2.source().query(new ScriptQueryBuilder(new Script(ScriptType.INLINE, "invalid", "code", Collections.emptyMap()))); multiSearchRequest.add(searchRequest2); - MultiSearchResponse multiSearchResponse = - execute(multiSearchRequest, highLevelClient()::msearch, highLevelClient()::msearchAsync); + MultiSearchResponse multiSearchResponse = execute(multiSearchRequest, highLevelClient()::msearch, highLevelClient()::msearchAsync); assertThat(multiSearchResponse.getTook().millis(), Matchers.greaterThanOrEqualTo(0L)); assertThat(multiSearchResponse.getResponses().length, Matchers.equalTo(2)); @@ -919,9 +957,11 @@ public void testSearchTemplate() throws IOException { searchTemplateRequest.setExplain(true); searchTemplateRequest.setProfile(true); - SearchTemplateResponse searchTemplateResponse = execute(searchTemplateRequest, + SearchTemplateResponse searchTemplateResponse = execute( + searchTemplateRequest, highLevelClient()::searchTemplate, - highLevelClient()::searchTemplateAsync); + highLevelClient()::searchTemplateAsync + ); assertNull(searchTemplateResponse.getSource()); @@ -946,10 +986,10 @@ public void testNonExistentSearchTemplate() { searchTemplateRequest.setScript("non-existent"); searchTemplateRequest.setScriptParams(Collections.emptyMap()); - ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, - () -> execute(searchTemplateRequest, - highLevelClient()::searchTemplate, - highLevelClient()::searchTemplateAsync)); + ElasticsearchStatusException exception = expectThrows( + ElasticsearchStatusException.class, + () -> execute(searchTemplateRequest, highLevelClient()::searchTemplate, highLevelClient()::searchTemplateAsync) + ); assertEquals(RestStatus.NOT_FOUND, exception.status()); } @@ -967,20 +1007,23 @@ public void testRenderSearchTemplate() throws IOException { // Setting simulate true causes the template to only be rendered. searchTemplateRequest.setSimulate(true); - SearchTemplateResponse searchTemplateResponse = execute(searchTemplateRequest, + SearchTemplateResponse searchTemplateResponse = execute( + searchTemplateRequest, highLevelClient()::searchTemplate, - highLevelClient()::searchTemplateAsync); + highLevelClient()::searchTemplateAsync + ); assertNull(searchTemplateResponse.getResponse()); BytesReference expectedSource = BytesReference.bytes( XContentFactory.jsonBuilder() .startObject() - .startObject("query") - .startObject("match") - .field("num", 10) - .endObject() - .endObject() - .endObject()); + .startObject("query") + .startObject("match") + .field("num", 10) + .endObject() + .endObject() + .endObject() + ); BytesReference actualSource = searchTemplateResponse.getSource(); assertNotNull(actualSource); @@ -988,7 +1031,6 @@ public void testRenderSearchTemplate() throws IOException { assertToXContentEquivalent(expectedSource, actualSource, XContentType.JSON); } - public void testMultiSearchTemplate() throws Exception { MultiSearchTemplateRequest multiSearchTemplateRequest = new MultiSearchTemplateRequest(); @@ -1003,7 +1045,6 @@ public void testMultiSearchTemplate() throws Exception { goodRequest.setProfile(true); multiSearchTemplateRequest.add(goodRequest); - SearchTemplateRequest badRequest = new SearchTemplateRequest(); badRequest.setRequest(new SearchRequest("index")); badRequest.setScriptType(ScriptType.INLINE); @@ -1014,17 +1055,18 @@ public void testMultiSearchTemplate() throws Exception { multiSearchTemplateRequest.add(badRequest); - MultiSearchTemplateResponse multiSearchTemplateResponse = - execute(multiSearchTemplateRequest, highLevelClient()::msearchTemplate, - highLevelClient()::msearchTemplateAsync); + MultiSearchTemplateResponse multiSearchTemplateResponse = execute( + multiSearchTemplateRequest, + highLevelClient()::msearchTemplate, + highLevelClient()::msearchTemplateAsync + ); Item[] responses = multiSearchTemplateResponse.getResponses(); assertEquals(2, responses.length); - assertNull(responses[0].getResponse().getSource()); - SearchResponse goodResponse =responses[0].getResponse().getResponse(); + SearchResponse goodResponse = responses[0].getResponse().getResponse(); assertNotNull(goodResponse); assertThat(responses[0].isFailure(), Matchers.is(false)); assertEquals(1, goodResponse.getHits().getTotalHits().value); @@ -1034,7 +1076,6 @@ public void testMultiSearchTemplate() throws Exception { assertNotNull(hit.getExplanation()); assertFalse(goodResponse.getProfileResults().isEmpty()); - assertNull(responses[0].getResponse().getSource()); assertThat(responses[1].isFailure(), Matchers.is(true)); assertNotNull(responses[1].getFailureMessage()); @@ -1053,7 +1094,6 @@ public void testMultiSearchTemplateAllBad() throws Exception { badRequest1.setScriptParams(scriptParams); multiSearchTemplateRequest.add(badRequest1); - SearchTemplateRequest badRequest2 = new SearchTemplateRequest(); badRequest2.setRequest(new SearchRequest("index")); badRequest2.setScriptType(ScriptType.INLINE); @@ -1065,9 +1105,10 @@ public void testMultiSearchTemplateAllBad() throws Exception { multiSearchTemplateRequest.add(badRequest2); // The whole HTTP request should fail if no nested search requests are valid - ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, - () -> execute(multiSearchTemplateRequest, highLevelClient()::msearchTemplate, - highLevelClient()::msearchTemplateAsync)); + ElasticsearchStatusException exception = expectThrows( + ElasticsearchStatusException.class, + () -> execute(multiSearchTemplateRequest, highLevelClient()::msearchTemplate, highLevelClient()::msearchTemplateAsync) + ); assertEquals(RestStatus.BAD_REQUEST, exception.status()); assertThat(exception.getMessage(), containsString("no requests added")); @@ -1117,9 +1158,9 @@ public void testExplain() throws IOException { } { ExplainRequest explainRequest = new ExplainRequest("index1", "1"); - explainRequest.query(QueryBuilders.boolQuery() - .must(QueryBuilders.termQuery("field", "value1")) - .must(QueryBuilders.termQuery("field", "value2"))); + explainRequest.query( + QueryBuilders.boolQuery().must(QueryBuilders.termQuery("field", "value1")).must(QueryBuilders.termQuery("field", "value2")) + ); ExplainResponse explainResponse = execute(explainRequest, highLevelClient()::explain, highLevelClient()::explainAsync); @@ -1137,12 +1178,16 @@ public void testExplainNonExistent() throws IOException { { ExplainRequest explainRequest = new ExplainRequest("non_existent_index", "1"); explainRequest.query(QueryBuilders.matchQuery("field", "value")); - ElasticsearchException exception = expectThrows(ElasticsearchException.class, - () -> execute(explainRequest, highLevelClient()::explain, highLevelClient()::explainAsync)); + ElasticsearchException exception = expectThrows( + ElasticsearchException.class, + () -> execute(explainRequest, highLevelClient()::explain, highLevelClient()::explainAsync) + ); assertThat(exception.status(), equalTo(RestStatus.NOT_FOUND)); assertThat(exception.getIndex().getName(), equalTo("non_existent_index")); - assertThat(exception.getDetailedMessage(), - containsString("Elasticsearch exception [type=index_not_found_exception, reason=no such index [non_existent_index]]")); + assertThat( + exception.getDetailedMessage(), + containsString("Elasticsearch exception [type=index_not_found_exception, reason=no such index [non_existent_index]]") + ); } { ExplainRequest explainRequest = new ExplainRequest("index1", "999"); @@ -1163,7 +1208,7 @@ public void testExplainWithStoredFields() throws IOException { { ExplainRequest explainRequest = new ExplainRequest("index4", "1"); explainRequest.query(QueryBuilders.matchAllQuery()); - explainRequest.storedFields(new String[]{"field1"}); + explainRequest.storedFields(new String[] { "field1" }); ExplainResponse explainResponse = execute(explainRequest, highLevelClient()::explain, highLevelClient()::explainAsync); @@ -1179,7 +1224,7 @@ public void testExplainWithStoredFields() throws IOException { { ExplainRequest explainRequest = new ExplainRequest("index4", "1"); explainRequest.query(QueryBuilders.matchAllQuery()); - explainRequest.storedFields(new String[]{"field1", "field2"}); + explainRequest.storedFields(new String[] { "field1", "field2" }); ExplainResponse explainResponse = execute(explainRequest, highLevelClient()::explain, highLevelClient()::explainAsync); @@ -1199,7 +1244,7 @@ public void testExplainWithFetchSource() throws IOException { { ExplainRequest explainRequest = new ExplainRequest("index4", "1"); explainRequest.query(QueryBuilders.matchAllQuery()); - explainRequest.fetchSourceContext(new FetchSourceContext(true, new String[]{"field1"}, null)); + explainRequest.fetchSourceContext(new FetchSourceContext(true, new String[] { "field1" }, null)); ExplainResponse explainResponse = execute(explainRequest, highLevelClient()::explain, highLevelClient()::explainAsync); @@ -1213,7 +1258,7 @@ public void testExplainWithFetchSource() throws IOException { { ExplainRequest explainRequest = new ExplainRequest("index4", "1"); explainRequest.query(QueryBuilders.matchAllQuery()); - explainRequest.fetchSourceContext(new FetchSourceContext(true, null, new String[] {"field2"})); + explainRequest.fetchSourceContext(new FetchSourceContext(true, null, new String[] { "field2" })); ExplainResponse explainResponse = execute(explainRequest, highLevelClient()::explain, highLevelClient()::explainAsync); @@ -1238,12 +1283,9 @@ public void testExplainWithAliasFilter() throws IOException { } public void testFieldCaps() throws IOException { - FieldCapabilitiesRequest request = new FieldCapabilitiesRequest() - .indices("index1", "index2") - .fields("rating", "field"); + FieldCapabilitiesRequest request = new FieldCapabilitiesRequest().indices("index1", "index2").fields("rating", "field"); - FieldCapabilitiesResponse response = execute(request, - highLevelClient()::fieldCaps, highLevelClient()::fieldCapsAsync); + FieldCapabilitiesResponse response = execute(request, highLevelClient()::fieldCaps, highLevelClient()::fieldCapsAsync); assertThat(response.getIndices(), arrayContaining("index1", "index2")); @@ -1253,11 +1295,29 @@ public void testFieldCaps() throws IOException { assertEquals(2, ratingResponse.size()); FieldCapabilities expectedKeywordCapabilities = new FieldCapabilities( - "rating", "keyword", false, true, true, new String[]{"index2"}, null, null, Collections.emptyMap()); + "rating", + "keyword", + false, + true, + true, + new String[] { "index2" }, + null, + null, + Collections.emptyMap() + ); assertEquals(expectedKeywordCapabilities, ratingResponse.get("keyword")); FieldCapabilities expectedLongCapabilities = new FieldCapabilities( - "rating", "long", false, true, true, new String[]{"index1"}, null, null, Collections.emptyMap()); + "rating", + "long", + false, + true, + true, + new String[] { "index1" }, + null, + null, + Collections.emptyMap() + ); assertEquals(expectedLongCapabilities, ratingResponse.get("long")); // Check the capabilities for the 'field' field. @@ -1266,27 +1326,33 @@ public void testFieldCaps() throws IOException { assertEquals(1, fieldResponse.size()); FieldCapabilities expectedTextCapabilities = new FieldCapabilities( - "field", "text", false, true, false, null, null, null, Collections.emptyMap()); + "field", + "text", + false, + true, + false, + null, + null, + null, + Collections.emptyMap() + ); assertEquals(expectedTextCapabilities, fieldResponse.get("text")); } public void testFieldCapsWithNonExistentFields() throws IOException { - FieldCapabilitiesRequest request = new FieldCapabilitiesRequest() - .indices("index2") - .fields("nonexistent"); + FieldCapabilitiesRequest request = new FieldCapabilitiesRequest().indices("index2").fields("nonexistent"); - FieldCapabilitiesResponse response = execute(request, - highLevelClient()::fieldCaps, highLevelClient()::fieldCapsAsync); + FieldCapabilitiesResponse response = execute(request, highLevelClient()::fieldCaps, highLevelClient()::fieldCapsAsync); assertTrue(response.get().isEmpty()); } public void testFieldCapsWithNonExistentIndices() { - FieldCapabilitiesRequest request = new FieldCapabilitiesRequest() - .indices("non-existent") - .fields("rating"); + FieldCapabilitiesRequest request = new FieldCapabilitiesRequest().indices("non-existent").fields("rating"); - ElasticsearchException exception = expectThrows(ElasticsearchException.class, - () -> execute(request, highLevelClient()::fieldCaps, highLevelClient()::fieldCapsAsync)); + ElasticsearchException exception = expectThrows( + ElasticsearchException.class, + () -> execute(request, highLevelClient()::fieldCaps, highLevelClient()::fieldCapsAsync) + ); assertEquals(RestStatus.NOT_FOUND, exception.status()); } @@ -1332,10 +1398,10 @@ public void testCountMultipleIndicesMatchQueryUsingConstructor() throws IOExcept CountRequest countRequest; if (randomBoolean()) { SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(new MatchQueryBuilder("field", "value1")); - countRequest = new CountRequest(new String[]{"index1", "index2", "index3"}, sourceBuilder); + countRequest = new CountRequest(new String[] { "index1", "index2", "index3" }, sourceBuilder); } else { QueryBuilder query = new MatchQueryBuilder("field", "value1"); - countRequest = new CountRequest(new String[]{"index1", "index2", "index3"}, query); + countRequest = new CountRequest(new String[] { "index1", "index2", "index3" }, query); } CountResponse countResponse = execute(countRequest, highLevelClient()::count, highLevelClient()::countAsync); assertCountHeader(countResponse); @@ -1377,8 +1443,11 @@ public void testSearchWithBasicLicensedQuery() throws IOException { assertSecondHit(searchResponse, hasId("1")); } { - PinnedQueryBuilder pinnedQuery = new PinnedQueryBuilder(new MatchAllQueryBuilder(), - new PinnedQueryBuilder.Item("index", "2"), new PinnedQueryBuilder.Item("index", "1")); + PinnedQueryBuilder pinnedQuery = new PinnedQueryBuilder( + new MatchAllQueryBuilder(), + new PinnedQueryBuilder.Item("index", "2"), + new PinnedQueryBuilder.Item("index", "1") + ); searchSourceBuilder.query(pinnedQuery); searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = execute(searchRequest, highLevelClient()::search, highLevelClient()::searchAsync); @@ -1419,8 +1488,11 @@ public void testPointInTime() throws Exception { } while (searchResponse.getHits().getHits().length > 0); assertThat(totalHits, equalTo(numDocs)); } finally { - ClosePointInTimeResponse closeResponse = execute(new ClosePointInTimeRequest(pitID), - highLevelClient()::closePointInTime, highLevelClient()::closePointInTimeAsync); + ClosePointInTimeResponse closeResponse = execute( + new ClosePointInTimeRequest(pitID), + highLevelClient()::closePointInTime, + highLevelClient()::closePointInTimeAsync + ); assertTrue(closeResponse.isSucceeded()); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/SearchableSnapshotsIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/SearchableSnapshotsIT.java index 69fe432e6334d..69b0b72fc0b74 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/SearchableSnapshotsIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/SearchableSnapshotsIT.java @@ -27,11 +27,11 @@ import org.elasticsearch.client.searchable_snapshots.CachesStatsResponse; import org.elasticsearch.client.searchable_snapshots.MountSnapshotRequest; import org.elasticsearch.common.unit.ByteSizeUnit; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.repositories.fs.FsRepository; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.search.builder.SearchSourceBuilder; +import org.elasticsearch.xcontent.XContentType; import org.junit.Before; import java.io.IOException; @@ -63,8 +63,7 @@ public void init() throws Exception { { final BulkRequest request = new BulkRequest().setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); for (int i = 0; i < 100; i++) { - request.add(new IndexRequest("index") - .source(XContentType.JSON, "num", i, "text", randomAlphaOfLengthBetween(3, 10))); + request.add(new IndexRequest("index").source(XContentType.JSON, "num", i, "text", randomAlphaOfLengthBetween(3, 10))); } final BulkResponse response = highLevelClient().bulk(request, RequestOptions.DEFAULT); assertThat(response.status(), is(RestStatus.OK)); @@ -80,8 +79,8 @@ public void init() throws Exception { } { - final CreateSnapshotRequest request = - new CreateSnapshotRequest("repository", "snapshot").waitForCompletion(true).includeGlobalState(false); + final CreateSnapshotRequest request = new CreateSnapshotRequest("repository", "snapshot").waitForCompletion(true) + .includeGlobalState(false); final CreateSnapshotResponse response = highLevelClient().snapshot().create(request, RequestOptions.DEFAULT); assertThat(response.getSnapshotInfo().status(), is(RestStatus.OK)); } @@ -95,8 +94,7 @@ public void init() throws Exception { public void testMountSnapshot() throws IOException { { - final MountSnapshotRequest request = new MountSnapshotRequest("repository", "snapshot", "index") - .waitForCompletion(true) + final MountSnapshotRequest request = new MountSnapshotRequest("repository", "snapshot", "index").waitForCompletion(true) .renamedIndex("renamed_index"); final SearchableSnapshotsClient client = new SearchableSnapshotsClient(highLevelClient()); final RestoreSnapshotResponse response = execute(request, client::mountSnapshot, client::mountSnapshotAsync); @@ -114,8 +112,7 @@ public void testMountSnapshot() throws IOException { public void testCacheStats() throws Exception { final SearchableSnapshotsClient client = new SearchableSnapshotsClient(highLevelClient()); { - final MountSnapshotRequest request = new MountSnapshotRequest("repository", "snapshot", "index") - .waitForCompletion(true) + final MountSnapshotRequest request = new MountSnapshotRequest("repository", "snapshot", "index").waitForCompletion(true) .renamedIndex("mounted_index") .storage(MountSnapshotRequest.Storage.SHARED_CACHE); final RestoreSnapshotResponse response = execute(request, client::mountSnapshot, client::mountSnapshotAsync); @@ -123,8 +120,9 @@ public void testCacheStats() throws Exception { } { - final SearchRequest request = new SearchRequest("mounted_index") - .source(new SearchSourceBuilder().query(QueryBuilders.rangeQuery("num").from(50))); + final SearchRequest request = new SearchRequest("mounted_index").source( + new SearchSourceBuilder().query(QueryBuilders.rangeQuery("num").from(50)) + ); final SearchResponse response = highLevelClient().search(request, RequestOptions.DEFAULT); assertThat(response.getHits().getTotalHits().value, is(50L)); assertThat(response.getHits().getHits()[0].getSourceAsMap(), aMapWithSize(2)); @@ -141,13 +139,16 @@ public void testCacheStats() throws Exception { for (String node : nodes.keySet()) { @SuppressWarnings("unchecked") - final Map threadPools = - (Map) extractValue((Map) nodes.get(node), "thread_pool"); + final Map threadPools = (Map) extractValue( + (Map) nodes.get(node), + "thread_pool" + ); assertNotNull("No thread pools on node " + node, threadPools); @SuppressWarnings("unchecked") - final Map threadPoolStats = - (Map) threadPools.get("searchable_snapshots_cache_fetch_async"); + final Map threadPoolStats = (Map) threadPools.get( + "searchable_snapshots_cache_fetch_async" + ); assertNotNull("No thread pools stats on node " + node, threadPoolStats); final Number active = (Number) extractValue(threadPoolStats, "active"); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/SearchableSnapshotsRequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/SearchableSnapshotsRequestConvertersTests.java index 591eac6017233..ad6ffa7319342 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/SearchableSnapshotsRequestConvertersTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/SearchableSnapshotsRequestConvertersTests.java @@ -23,9 +23,12 @@ public class SearchableSnapshotsRequestConvertersTests extends ESTestCase { - public void testMountSnapshot() throws IOException { - final MountSnapshotRequest request = - new MountSnapshotRequest(randomAlphaOfLength(8), randomAlphaOfLength(8), randomAlphaOfLength(8)); + public void testMountSnapshot() throws IOException { + final MountSnapshotRequest request = new MountSnapshotRequest( + randomAlphaOfLength(8), + randomAlphaOfLength(8), + randomAlphaOfLength(8) + ); if (randomBoolean()) { request.masterTimeout(TimeValue.parseTimeValue(randomTimeValue(), "master_timeout")); } @@ -57,7 +60,7 @@ public void testMountSnapshot() throws IOException { RequestConvertersTests.assertToXContentBody(request, result.getEntity()); } - public void testCachesStats() throws IOException { + public void testCachesStats() throws IOException { { final Request request = SearchableSnapshotsRequestConverters.cacheStats(new CachesStatsRequest()); assertThat(request.getMethod(), equalTo(HttpGet.METHOD_NAME)); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityIT.java index 8127b420da167..5dcd9465bfe8f 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityIT.java @@ -44,11 +44,11 @@ import java.util.Locale; import java.util.Map; +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.nullValue; @SuppressWarnings("removal") @@ -98,51 +98,68 @@ public void testAuthenticate() throws Exception { // authenticate correctly final String basicAuthHeader = basicAuthHeader(putUserRequest.getUser().getUsername(), putUserRequest.getPassword()); - final AuthenticateResponse authenticateResponse = execute(securityClient::authenticate, securityClient::authenticateAsync, - authorizationRequestOptions(basicAuthHeader)); + final AuthenticateResponse authenticateResponse = execute( + securityClient::authenticate, + securityClient::authenticateAsync, + authorizationRequestOptions(basicAuthHeader) + ); assertThat(authenticateResponse.getUser(), is(putUserRequest.getUser())); assertThat(authenticateResponse.enabled(), is(true)); assertThat(authenticateResponse.getAuthenticationType(), is("realm")); // get user - final GetUsersRequest getUsersRequest = - new GetUsersRequest(putUserRequest.getUser().getUsername()); - final GetUsersResponse getUsersResponse = - execute(getUsersRequest, securityClient::getUsers, securityClient::getUsersAsync); + final GetUsersRequest getUsersRequest = new GetUsersRequest(putUserRequest.getUser().getUsername()); + final GetUsersResponse getUsersResponse = execute(getUsersRequest, securityClient::getUsers, securityClient::getUsersAsync); ArrayList users = new ArrayList<>(); users.addAll(getUsersResponse.getUsers()); assertThat(users.get(0), is(putUserRequest.getUser())); // delete user - final DeleteUserRequest deleteUserRequest = - new DeleteUserRequest(putUserRequest.getUser().getUsername(), putUserRequest.getRefreshPolicy()); - - final DeleteUserResponse deleteUserResponse = - execute(deleteUserRequest, securityClient::deleteUser, securityClient::deleteUserAsync); + final DeleteUserRequest deleteUserRequest = new DeleteUserRequest( + putUserRequest.getUser().getUsername(), + putUserRequest.getRefreshPolicy() + ); + + final DeleteUserResponse deleteUserResponse = execute( + deleteUserRequest, + securityClient::deleteUser, + securityClient::deleteUserAsync + ); assertThat(deleteUserResponse.isAcknowledged(), is(true)); // authentication no longer works - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, () -> execute(securityClient::authenticate, - securityClient::authenticateAsync, authorizationRequestOptions(basicAuthHeader))); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> execute(securityClient::authenticate, securityClient::authenticateAsync, authorizationRequestOptions(basicAuthHeader)) + ); assertThat(e.getMessage(), containsString("unable to authenticate user [" + putUserRequest.getUser().getUsername() + "]")); // delete non-existing user - final DeleteUserResponse deleteUserResponse2 = - execute(deleteUserRequest, securityClient::deleteUser, securityClient::deleteUserAsync); + final DeleteUserResponse deleteUserResponse2 = execute( + deleteUserRequest, + securityClient::deleteUser, + securityClient::deleteUserAsync + ); assertThat(deleteUserResponse2.isAcknowledged(), is(false)); // Test the authenticate response for a service token { RestHighLevelClient client = highLevelClient(); - CreateServiceAccountTokenRequest createServiceAccountTokenRequest = - new CreateServiceAccountTokenRequest("elastic", "fleet-server", "token1"); - CreateServiceAccountTokenResponse createServiceAccountTokenResponse = - client.security().createServiceAccountToken(createServiceAccountTokenRequest, RequestOptions.DEFAULT); - - AuthenticateResponse response = client.security().authenticate( - RequestOptions.DEFAULT.toBuilder().addHeader( - "Authorization", "Bearer " + createServiceAccountTokenResponse.getValue().toString()).build()); + CreateServiceAccountTokenRequest createServiceAccountTokenRequest = new CreateServiceAccountTokenRequest( + "elastic", + "fleet-server", + "token1" + ); + CreateServiceAccountTokenResponse createServiceAccountTokenResponse = client.security() + .createServiceAccountToken(createServiceAccountTokenRequest, RequestOptions.DEFAULT); + + AuthenticateResponse response = client.security() + .authenticate( + RequestOptions.DEFAULT.toBuilder() + .addHeader("Authorization", "Bearer " + createServiceAccountTokenResponse.getValue().toString()) + .build() + ); User user = response.getUser(); boolean enabled = response.enabled(); @@ -226,13 +243,19 @@ private User randomUser(String username) { private static Role randomRole(String roleName) { final Role.Builder roleBuilder = Role.builder() - .name(roleName) - .clusterPrivileges(randomSubsetOf(randomInt(3), Role.ClusterPrivilegeName.ALL_ARRAY)) - .indicesPrivileges( - randomArray(3, IndicesPrivileges[]::new, () -> IndicesPrivilegesTests.createNewRandom("{\"match_all\": {}}"))) - .applicationResourcePrivileges(randomArray(3, ApplicationResourcePrivileges[]::new, - () -> ApplicationResourcePrivilegesTests.createNewRandom(randomAlphaOfLength(3).toLowerCase(Locale.ROOT)))) - .runAsPrivilege(randomArray(3, String[]::new, () -> randomAlphaOfLength(3))); + .name(roleName) + .clusterPrivileges(randomSubsetOf(randomInt(3), Role.ClusterPrivilegeName.ALL_ARRAY)) + .indicesPrivileges( + randomArray(3, IndicesPrivileges[]::new, () -> IndicesPrivilegesTests.createNewRandom("{\"match_all\": {}}")) + ) + .applicationResourcePrivileges( + randomArray( + 3, + ApplicationResourcePrivileges[]::new, + () -> ApplicationResourcePrivilegesTests.createNewRandom(randomAlphaOfLength(3).toLowerCase(Locale.ROOT)) + ) + ) + .runAsPrivilege(randomArray(3, String[]::new, () -> randomAlphaOfLength(3))); if (randomBoolean()) { roleBuilder.globalApplicationPrivileges(GlobalPrivilegesTests.buildRandomManageApplicationPrivilege()); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityRequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityRequestConvertersTests.java index 4fb1bd7818af7..3c3164ca103ad 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityRequestConvertersTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityRequestConvertersTests.java @@ -117,8 +117,10 @@ public void testGetUsers() { if (users.length == 0) { assertEquals("/_security/user", request.getEndpoint()); } else { - assertEquals("/_security/user/" + Strings.collectionToCommaDelimitedString(getUsersRequest.getUsernames()), - request.getEndpoint()); + assertEquals( + "/_security/user/" + Strings.collectionToCommaDelimitedString(getUsersRequest.getUsernames()), + request.getEndpoint() + ); } assertNull(request.getEntity()); assertEquals(Collections.emptyMap(), request.getParameters()); @@ -128,7 +130,7 @@ public void testPutRoleMapping() throws IOException { final String username = randomAlphaOfLengthBetween(4, 7); final String rolename = randomAlphaOfLengthBetween(4, 7); final String roleMappingName = randomAlphaOfLengthBetween(4, 7); - final String groupname = "cn="+randomAlphaOfLengthBetween(4, 7)+",dc=example,dc=com"; + final String groupname = "cn=" + randomAlphaOfLengthBetween(4, 7) + ",dc=example,dc=com"; final RefreshPolicy refreshPolicy = randomFrom(RefreshPolicy.values()); final Map expectedParams; if (refreshPolicy != RefreshPolicy.NONE) { @@ -138,11 +140,18 @@ public void testPutRoleMapping() throws IOException { } final RoleMapperExpression rules = AnyRoleMapperExpression.builder() - .addExpression(FieldRoleMapperExpression.ofUsername(username)) - .addExpression(FieldRoleMapperExpression.ofGroups(groupname)) - .build(); - final PutRoleMappingRequest putRoleMappingRequest = new PutRoleMappingRequest(roleMappingName, true, - Collections.singletonList(rolename), Collections.emptyList(), rules, null, refreshPolicy); + .addExpression(FieldRoleMapperExpression.ofUsername(username)) + .addExpression(FieldRoleMapperExpression.ofGroups(groupname)) + .build(); + final PutRoleMappingRequest putRoleMappingRequest = new PutRoleMappingRequest( + roleMappingName, + true, + Collections.singletonList(rolename), + Collections.emptyList(), + rules, + null, + refreshPolicy + ); final Request request = SecurityRequestConverters.putRoleMapping(putRoleMappingRequest); @@ -154,8 +163,12 @@ public void testPutRoleMapping() throws IOException { public void testGetRoleMappings() throws IOException { int noOfRoleMappingNames = randomIntBetween(0, 2); - final String[] roleMappingNames = - randomArray(noOfRoleMappingNames, noOfRoleMappingNames, String[]::new, () -> randomAlphaOfLength(5)); + final String[] roleMappingNames = randomArray( + noOfRoleMappingNames, + noOfRoleMappingNames, + String[]::new, + () -> randomAlphaOfLength(5) + ); final GetRoleMappingsRequest getRoleMappingsRequest = new GetRoleMappingsRequest(roleMappingNames); final Request request = SecurityRequestConverters.getRoleMappings(getRoleMappingsRequest); @@ -164,8 +177,10 @@ public void testGetRoleMappings() throws IOException { if (noOfRoleMappingNames == 0) { assertEquals("/_security/role_mapping", request.getEndpoint()); } else { - assertEquals("/_security/role_mapping/" + - Strings.collectionToCommaDelimitedString(getRoleMappingsRequest.getRoleMappingNames()), request.getEndpoint()); + assertEquals( + "/_security/role_mapping/" + Strings.collectionToCommaDelimitedString(getRoleMappingsRequest.getRoleMappingNames()), + request.getEndpoint() + ); } assertEquals(Collections.emptyMap(), request.getParameters()); assertNull(request.getEntity()); @@ -256,8 +271,10 @@ public void testGetRoles() { if (roles.length == 0) { assertEquals("/_security/role", request.getEndpoint()); } else { - assertEquals("/_security/role/" + Strings.collectionToCommaDelimitedString(getRolesRequest.getRoleNames()), - request.getEndpoint()); + assertEquals( + "/_security/role/" + Strings.collectionToCommaDelimitedString(getRolesRequest.getRoleNames()), + request.getEndpoint() + ); } assertNull(request.getEntity()); assertEquals(Collections.emptyMap(), request.getParameters()); @@ -309,7 +326,8 @@ public void testDelegatePkiAuthentication() throws Exception { X509Certificate mockCertificate = mock(X509Certificate.class); when(mockCertificate.getEncoded()).thenReturn(new byte[0]); DelegatePkiAuthenticationRequest delegatePkiAuthenticationRequest = new DelegatePkiAuthenticationRequest( - Arrays.asList(mockCertificate)); + Arrays.asList(mockCertificate) + ); Request request = SecurityRequestConverters.delegatePkiAuthentication(delegatePkiAuthenticationRequest); assertEquals(HttpPost.METHOD_NAME, request.getMethod()); assertEquals("/_security/delegate_pki", request.getEndpoint()); @@ -341,13 +359,14 @@ public void testGetAllPrivilegesForApplication() throws Exception { public void testGetMultipleApplicationPrivileges() throws Exception { final String application = randomAlphaOfLength(6); final int numberOfPrivileges = randomIntBetween(1, 5); - final String[] privilegeNames = - randomArray(numberOfPrivileges, numberOfPrivileges, String[]::new, () -> randomAlphaOfLength(5)); + final String[] privilegeNames = randomArray(numberOfPrivileges, numberOfPrivileges, String[]::new, () -> randomAlphaOfLength(5)); GetPrivilegesRequest getPrivilegesRequest = new GetPrivilegesRequest(application, privilegeNames); Request request = SecurityRequestConverters.getPrivileges(getPrivilegesRequest); assertEquals(HttpGet.METHOD_NAME, request.getMethod()); - assertEquals("/_security/privilege/" + application + "/" + Strings.arrayToCommaDelimitedString(privilegeNames), - request.getEndpoint()); + assertEquals( + "/_security/privilege/" + application + "/" + Strings.arrayToCommaDelimitedString(privilegeNames), + request.getEndpoint() + ); assertEquals(Collections.emptyMap(), request.getParameters()); assertNull(request.getEntity()); } @@ -366,12 +385,14 @@ public void testPutPrivileges() throws Exception { final List privileges = new ArrayList<>(); for (int count = 0; count < noOfApplicationPrivileges; count++) { final String[] actions = generateRandomStringArray(3, 5, false, false); - privileges.add(ApplicationPrivilege.builder() + privileges.add( + ApplicationPrivilege.builder() .application(randomAlphaOfLength(4)) .privilege(randomAlphaOfLengthBetween(3, 5)) .metadata(Collections.singletonMap("k1", "v1")) .actions(actions == null ? Collections.emptyList() : List.of(actions)) - .build()); + .build() + ); } final RefreshPolicy refreshPolicy = randomFrom(RefreshPolicy.values()); final Map expectedParams = getExpectedParamsFromRefreshPolicy(refreshPolicy); @@ -388,12 +409,17 @@ public void testDeletePrivileges() { final List privileges = randomSubsetOf(randomIntBetween(1, 3), "read", "write", "all"); final RefreshPolicy refreshPolicy = randomFrom(RefreshPolicy.values()); final Map expectedParams = getExpectedParamsFromRefreshPolicy(refreshPolicy); - DeletePrivilegesRequest deletePrivilegesRequest = - new DeletePrivilegesRequest(application, privileges.toArray(Strings.EMPTY_ARRAY), refreshPolicy); + DeletePrivilegesRequest deletePrivilegesRequest = new DeletePrivilegesRequest( + application, + privileges.toArray(Strings.EMPTY_ARRAY), + refreshPolicy + ); Request request = SecurityRequestConverters.deletePrivileges(deletePrivilegesRequest); assertEquals(HttpDelete.METHOD_NAME, request.getMethod()); - assertEquals("/_security/privilege/" + application + "/" + Strings.collectionToCommaDelimitedString(privileges), - request.getEndpoint()); + assertEquals( + "/_security/privilege/" + application + "/" + Strings.collectionToCommaDelimitedString(privileges), + request.getEndpoint() + ); assertEquals(expectedParams, request.getParameters()); assertNull(request.getEntity()); } @@ -406,15 +432,23 @@ public void testPutRole() throws IOException { final List applicationPrivilegeNames = Arrays.asList(randomArray(1, 3, String[]::new, () -> randomAlphaOfLength(5))); final List applicationResouceNames = Arrays.asList(randomArray(1, 3, String[]::new, () -> randomAlphaOfLength(5))); final ApplicationResourcePrivileges applicationResourcePrivilege = new ApplicationResourcePrivileges( - randomAlphaOfLengthBetween(4, 7), applicationPrivilegeNames, applicationResouceNames); + randomAlphaOfLengthBetween(4, 7), + applicationPrivilegeNames, + applicationResouceNames + ); final List indicesName = Arrays.asList(randomArray(1, 3, String[]::new, () -> randomAlphaOfLength(5))); final List indicesPrivilegeName = Arrays.asList(randomArray(1, 3, String[]::new, () -> randomAlphaOfLength(5))); final List indicesPrivilegeGrantedName = Arrays.asList(randomArray(3, String[]::new, () -> randomAlphaOfLength(5))); final List indicesPrivilegeDeniedName = Arrays.asList(randomArray(3, String[]::new, () -> randomAlphaOfLength(5))); final String indicesPrivilegeQuery = randomAlphaOfLengthBetween(0, 7); - final IndicesPrivileges indicesPrivilege = IndicesPrivileges.builder().indices(indicesName).privileges(indicesPrivilegeName) - .allowRestrictedIndices(randomBoolean()).grantedFields(indicesPrivilegeGrantedName).deniedFields(indicesPrivilegeDeniedName) - .query(indicesPrivilegeQuery).build(); + final IndicesPrivileges indicesPrivilege = IndicesPrivileges.builder() + .indices(indicesName) + .privileges(indicesPrivilegeName) + .allowRestrictedIndices(randomBoolean()) + .grantedFields(indicesPrivilegeGrantedName) + .deniedFields(indicesPrivilegeDeniedName) + .query(indicesPrivilegeQuery) + .build(); final RefreshPolicy refreshPolicy = randomFrom(RefreshPolicy.values()); final Map expectedParams; if (refreshPolicy != RefreshPolicy.NONE) { @@ -422,8 +456,14 @@ public void testPutRole() throws IOException { } else { expectedParams = Collections.emptyMap(); } - final Role role = Role.builder().name(roleName).clusterPrivileges(clusterPrivileges).indicesPrivileges(indicesPrivilege) - .applicationResourcePrivileges(applicationResourcePrivilege).runAsPrivilege(runAsPrivilege).metadata(metadata).build(); + final Role role = Role.builder() + .name(roleName) + .clusterPrivileges(clusterPrivileges) + .indicesPrivileges(indicesPrivilege) + .applicationResourcePrivileges(applicationResourcePrivilege) + .runAsPrivilege(runAsPrivilege) + .metadata(metadata) + .build(); final PutRoleRequest putRoleRequest = new PutRoleRequest(role, refreshPolicy); final Request request = SecurityRequestConverters.putRole(putRoleRequest); assertEquals(HttpPut.METHOD_NAME, request.getMethod()); @@ -452,8 +492,13 @@ public void testCreateApiKey() throws IOException { private CreateApiKeyRequest buildCreateApiKeyRequest() { final String name = randomAlphaOfLengthBetween(4, 7); - final List roles = Collections.singletonList(Role.builder().name("r1").clusterPrivileges(ClusterPrivilegeName.ALL) - .indicesPrivileges(IndicesPrivileges.builder().indices("ind-x").privileges(IndexPrivilegeName.ALL).build()).build()); + final List roles = Collections.singletonList( + Role.builder() + .name("r1") + .clusterPrivileges(ClusterPrivilegeName.ALL) + .indicesPrivileges(IndicesPrivileges.builder().indices("ind-x").privileges(IndexPrivilegeName.ALL).build()) + .build() + ); final TimeValue expiration = randomBoolean() ? null : TimeValue.timeValueHours(24); final RefreshPolicy refreshPolicy = randomFrom(RefreshPolicy.values()); final Map metadata = CreateApiKeyRequestTests.randomMetadata(); @@ -463,10 +508,15 @@ private CreateApiKeyRequest buildCreateApiKeyRequest() { public void testGrantApiKey() throws IOException { final CreateApiKeyRequest createApiKeyRequest = buildCreateApiKeyRequest(); - final GrantApiKeyRequest grantApiKeyRequest = new GrantApiKeyRequest(randomBoolean() - ? GrantApiKeyRequest.Grant.accessTokenGrant(randomAlphaOfLength(24)) - : GrantApiKeyRequest.Grant.passwordGrant(randomAlphaOfLengthBetween(4, 12), randomAlphaOfLengthBetween(14, 18).toCharArray()), - createApiKeyRequest); + final GrantApiKeyRequest grantApiKeyRequest = new GrantApiKeyRequest( + randomBoolean() + ? GrantApiKeyRequest.Grant.accessTokenGrant(randomAlphaOfLength(24)) + : GrantApiKeyRequest.Grant.passwordGrant( + randomAlphaOfLengthBetween(4, 12), + randomAlphaOfLengthBetween(14, 18).toCharArray() + ), + createApiKeyRequest + ); final Map expectedParams; final RefreshPolicy refreshPolicy = createApiKeyRequest.getRefreshPolicy(); if (refreshPolicy != RefreshPolicy.NONE) { @@ -512,7 +562,8 @@ public void testQueryApiKey() throws IOException { randomIntBetween(0, 100), randomIntBetween(0, 100), QueryApiKeyRequestTests.randomFieldSortBuilders(), - QueryApiKeyRequestTests.randomSearchAfterBuilder()); + QueryApiKeyRequestTests.randomSearchAfterBuilder() + ); final Request request = SecurityRequestConverters.queryApiKey(queryApiKeyRequest); assertEquals(HttpGet.METHOD_NAME, request.getMethod()); assertEquals("/_security/_query/api_key", request.getEndpoint()); @@ -539,12 +590,20 @@ public void testCreateServiceAccountToken() throws IOException { final String serviceName = randomAlphaOfLengthBetween(3, 8); final String tokenName = randomBoolean() ? randomAlphaOfLengthBetween(3, 8) : null; final RefreshPolicy refreshPolicy = randomBoolean() ? randomFrom(RefreshPolicy.values()) : null; - final CreateServiceAccountTokenRequest createServiceAccountTokenRequest = - new CreateServiceAccountTokenRequest(namespace, serviceName, tokenName, refreshPolicy); + final CreateServiceAccountTokenRequest createServiceAccountTokenRequest = new CreateServiceAccountTokenRequest( + namespace, + serviceName, + tokenName, + refreshPolicy + ); final Request request = SecurityRequestConverters.createServiceAccountToken(createServiceAccountTokenRequest); assertEquals(HttpPost.METHOD_NAME, request.getMethod()); - final String url = - "/_security/service/" + namespace + "/" + serviceName + "/credential/token" + (tokenName == null ? "" : "/" + tokenName); + final String url = "/_security/service/" + + namespace + + "/" + + serviceName + + "/credential/token" + + (tokenName == null ? "" : "/" + tokenName); assertEquals(url, request.getEndpoint()); if (refreshPolicy != null && refreshPolicy != RefreshPolicy.NONE) { assertEquals(refreshPolicy.getValue(), request.getParameters().get("refresh")); @@ -556,8 +615,12 @@ public void testDeleteServiceAccountToken() throws IOException { final String serviceName = randomAlphaOfLengthBetween(3, 8); final String tokenName = randomAlphaOfLengthBetween(3, 8); final RefreshPolicy refreshPolicy = randomBoolean() ? randomFrom(RefreshPolicy.values()) : null; - final DeleteServiceAccountTokenRequest deleteServiceAccountTokenRequest = - new DeleteServiceAccountTokenRequest(namespace, serviceName, tokenName, refreshPolicy); + final DeleteServiceAccountTokenRequest deleteServiceAccountTokenRequest = new DeleteServiceAccountTokenRequest( + namespace, + serviceName, + tokenName, + refreshPolicy + ); final Request request = SecurityRequestConverters.deleteServiceAccountToken(deleteServiceAccountTokenRequest); assertEquals("/_security/service/" + namespace + "/" + serviceName + "/credential/token/" + tokenName, request.getEndpoint()); if (refreshPolicy != null && refreshPolicy != RefreshPolicy.NONE) { @@ -568,10 +631,12 @@ public void testDeleteServiceAccountToken() throws IOException { public void testGetServiceAccountCredentials() { final String namespace = randomAlphaOfLengthBetween(3, 8); final String serviceName = randomAlphaOfLengthBetween(3, 8); - final GetServiceAccountCredentialsRequest getServiceAccountCredentialsRequest = - new GetServiceAccountCredentialsRequest(namespace, serviceName); + final GetServiceAccountCredentialsRequest getServiceAccountCredentialsRequest = new GetServiceAccountCredentialsRequest( + namespace, + serviceName + ); final Request request = SecurityRequestConverters.getServiceAccountCredentials(getServiceAccountCredentialsRequest); assertEquals(HttpGet.METHOD_NAME, request.getMethod()); assertEquals("/_security/service/" + namespace + "/" + serviceName + "/credential", request.getEndpoint()); } - } +} diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/SnapshotIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/SnapshotIT.java index 1f2ff9355b646..ed3a0932a3952 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/SnapshotIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/SnapshotIT.java @@ -30,12 +30,12 @@ import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.repositories.fs.FsRepository; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.snapshots.AbstractSnapshotIntegTestCase; import org.elasticsearch.snapshots.RestoreInfo; import org.elasticsearch.snapshots.SnapshotInfo; +import org.elasticsearch.xcontent.XContentType; import org.mockito.internal.util.collections.Sets; import java.io.IOException; @@ -57,15 +57,13 @@ private AcknowledgedResponse createTestRepository(String repository, String type PutRepositoryRequest request = new PutRepositoryRequest(repository); request.settings(settings, XContentType.JSON); request.type(type); - return execute(request, highLevelClient().snapshot()::createRepository, - highLevelClient().snapshot()::createRepositoryAsync); + return execute(request, highLevelClient().snapshot()::createRepository, highLevelClient().snapshot()::createRepositoryAsync); } private CreateSnapshotResponse createTestSnapshot(CreateSnapshotRequest createSnapshotRequest) throws IOException { // assumes the repository already exists - return execute(createSnapshotRequest, highLevelClient().snapshot()::create, - highLevelClient().snapshot()::createAsync); + return execute(createSnapshotRequest, highLevelClient().snapshot()::create, highLevelClient().snapshot()::createAsync); } public void testCreateRepository() throws IOException { @@ -79,9 +77,12 @@ public void testSnapshotGetRepositoriesUsingParams() throws IOException { assertTrue(createTestRepository("other", FsRepository.TYPE, "{\"location\": \".\"}").isAcknowledged()); GetRepositoriesRequest request = new GetRepositoriesRequest(); - request.repositories(new String[]{testRepository}); - GetRepositoriesResponse response = execute(request, highLevelClient().snapshot()::getRepository, - highLevelClient().snapshot()::getRepositoryAsync); + request.repositories(new String[] { testRepository }); + GetRepositoriesResponse response = execute( + request, + highLevelClient().snapshot()::getRepository, + highLevelClient().snapshot()::getRepositoryAsync + ); assertThat(1, equalTo(response.repositories().size())); } @@ -89,20 +90,27 @@ public void testSnapshotGetDefaultRepositories() throws IOException { assertTrue(createTestRepository("other", FsRepository.TYPE, "{\"location\": \".\"}").isAcknowledged()); assertTrue(createTestRepository("test", FsRepository.TYPE, "{\"location\": \".\"}").isAcknowledged()); - GetRepositoriesResponse response = execute(new GetRepositoriesRequest(), highLevelClient().snapshot()::getRepository, - highLevelClient().snapshot()::getRepositoryAsync); + GetRepositoriesResponse response = execute( + new GetRepositoriesRequest(), + highLevelClient().snapshot()::getRepository, + highLevelClient().snapshot()::getRepositoryAsync + ); assertThat(2, equalTo(response.repositories().size())); } public void testSnapshotGetRepositoriesNonExistent() { String repository = "doesnotexist"; - GetRepositoriesRequest request = new GetRepositoriesRequest(new String[]{repository}); - ElasticsearchException exception = expectThrows(ElasticsearchException.class, () -> execute(request, - highLevelClient().snapshot()::getRepository, highLevelClient().snapshot()::getRepositoryAsync)); + GetRepositoriesRequest request = new GetRepositoriesRequest(new String[] { repository }); + ElasticsearchException exception = expectThrows( + ElasticsearchException.class, + () -> execute(request, highLevelClient().snapshot()::getRepository, highLevelClient().snapshot()::getRepositoryAsync) + ); assertThat(exception.status(), equalTo(RestStatus.NOT_FOUND)); - assertThat(exception.getMessage(), equalTo( - "Elasticsearch exception [type=repository_missing_exception, reason=[" + repository + "] missing]")); + assertThat( + exception.getMessage(), + equalTo("Elasticsearch exception [type=repository_missing_exception, reason=[" + repository + "] missing]") + ); } public void testSnapshotDeleteRepository() throws IOException { @@ -110,13 +118,19 @@ public void testSnapshotDeleteRepository() throws IOException { assertTrue(createTestRepository(repository, FsRepository.TYPE, "{\"location\": \".\"}").isAcknowledged()); GetRepositoriesRequest request = new GetRepositoriesRequest(); - GetRepositoriesResponse response = execute(request, highLevelClient().snapshot()::getRepository, - highLevelClient().snapshot()::getRepositoryAsync); + GetRepositoriesResponse response = execute( + request, + highLevelClient().snapshot()::getRepository, + highLevelClient().snapshot()::getRepositoryAsync + ); assertThat(1, equalTo(response.repositories().size())); DeleteRepositoryRequest deleteRequest = new DeleteRepositoryRequest(repository); - AcknowledgedResponse deleteResponse = execute(deleteRequest, highLevelClient().snapshot()::deleteRepository, - highLevelClient().snapshot()::deleteRepositoryAsync); + AcknowledgedResponse deleteResponse = execute( + deleteRequest, + highLevelClient().snapshot()::deleteRepository, + highLevelClient().snapshot()::deleteRepositoryAsync + ); assertTrue(deleteResponse.isAcknowledged()); } @@ -126,8 +140,11 @@ public void testVerifyRepository() throws IOException { assertTrue(putRepositoryResponse.isAcknowledged()); VerifyRepositoryRequest request = new VerifyRepositoryRequest("test"); - VerifyRepositoryResponse response = execute(request, highLevelClient().snapshot()::verifyRepository, - highLevelClient().snapshot()::verifyRepositoryAsync); + VerifyRepositoryResponse response = execute( + request, + highLevelClient().snapshot()::verifyRepository, + highLevelClient().snapshot()::verifyRepositoryAsync + ); assertThat(response.getNodes().size(), equalTo(1)); } @@ -136,8 +153,11 @@ public void testCleanupRepository() throws IOException { assertTrue(putRepositoryResponse.isAcknowledged()); CleanupRepositoryRequest request = new CleanupRepositoryRequest("test"); - CleanupRepositoryResponse response = execute(request, highLevelClient().snapshot()::cleanupRepository, - highLevelClient().snapshot()::cleanupRepositoryAsync); + CleanupRepositoryResponse response = execute( + request, + highLevelClient().snapshot()::cleanupRepository, + highLevelClient().snapshot()::cleanupRepositoryAsync + ); assertThat(response.result().bytes(), equalTo(0L)); assertThat(response.result().blobs(), equalTo(0L)); } @@ -169,8 +189,9 @@ public void testCreateSnapshot() throws Exception { if (waitForCompletion == false) { // If we don't wait for the snapshot to complete we have to cancel it to not leak the snapshot task AcknowledgedResponse deleteResponse = execute( - new DeleteSnapshotRequest(repository, snapshot), - highLevelClient().snapshot()::delete, highLevelClient().snapshot()::deleteAsync + new DeleteSnapshotRequest(repository, snapshot), + highLevelClient().snapshot()::delete, + highLevelClient().snapshot()::deleteAsync ); assertTrue(deleteResponse.isAcknowledged()); } @@ -182,12 +203,10 @@ public void testGetSnapshots() throws IOException { String snapshot1 = "test_snapshot1"; String snapshot2 = "test_snapshot2"; - AcknowledgedResponse putRepositoryResponse = - createTestRepository(repository1, FsRepository.TYPE, "{\"location\": \"loc1\"}"); + AcknowledgedResponse putRepositoryResponse = createTestRepository(repository1, FsRepository.TYPE, "{\"location\": \"loc1\"}"); assertTrue(putRepositoryResponse.isAcknowledged()); - AcknowledgedResponse putRepositoryResponse2 = - createTestRepository(repository2, FsRepository.TYPE, "{\"location\": \"loc2\"}"); + AcknowledgedResponse putRepositoryResponse2 = createTestRepository(repository2, FsRepository.TYPE, "{\"location\": \"loc2\"}"); assertTrue(putRepositoryResponse2.isAcknowledged()); CreateSnapshotRequest createSnapshotRequest1 = new CreateSnapshotRequest(repository1, snapshot1); @@ -203,8 +222,8 @@ public void testGetSnapshots() throws IOException { assertEquals(RestStatus.OK, putSnapshotResponse2.status()); GetSnapshotsRequest request = new GetSnapshotsRequest( - randomFrom(new String[]{"_all"}, new String[]{"*"}, new String[]{repository1, repository2}), - randomFrom(new String[]{"_all"}, new String[]{"*"}, new String[]{snapshot1, snapshot2}) + randomFrom(new String[] { "_all" }, new String[] { "*" }, new String[] { repository1, repository2 }), + randomFrom(new String[] { "_all" }, new String[] { "*" }, new String[] { snapshot1, snapshot2 }) ); request.ignoreUnavailable(true); @@ -224,7 +243,6 @@ public void testGetSnapshots() throws IOException { assertThat(response.getSnapshots().get(1).repository(), equalTo(repository2)); } - public void testSnapshotsStatus() throws IOException { String testRepository = "test"; String testSnapshot = "snapshot"; @@ -244,9 +262,12 @@ public void testSnapshotsStatus() throws IOException { SnapshotsStatusRequest request = new SnapshotsStatusRequest(); request.repository(testRepository); - request.snapshots(new String[]{testSnapshot}); - SnapshotsStatusResponse response = execute(request, highLevelClient().snapshot()::status, - highLevelClient().snapshot()::statusAsync); + request.snapshots(new String[] { testSnapshot }); + SnapshotsStatusResponse response = execute( + request, + highLevelClient().snapshot()::status, + highLevelClient().snapshot()::statusAsync + ); assertThat(response.getSnapshots().size(), equalTo(1)); assertThat(response.getSnapshots().get(0).getSnapshot().getRepository(), equalTo(testRepository)); assertThat(response.getSnapshots().get(0).getSnapshot().getSnapshotId().getName(), equalTo(testSnapshot)); @@ -287,8 +308,11 @@ public void testRestoreSnapshot() throws IOException { request.featureStates(Collections.singletonList(NO_FEATURE_STATES_VALUE)); } - RestoreSnapshotResponse response = execute(request, highLevelClient().snapshot()::restore, - highLevelClient().snapshot()::restoreAsync); + RestoreSnapshotResponse response = execute( + request, + highLevelClient().snapshot()::restore, + highLevelClient().snapshot()::restoreAsync + ); RestoreInfo restoreInfo = response.getRestoreInfo(); assertThat(restoreInfo.name(), equalTo(testSnapshot)); @@ -305,11 +329,14 @@ public void testSnapshotHidden() throws IOException { AcknowledgedResponse putRepositoryResponse = createTestRepository(testRepository, FsRepository.TYPE, "{\"location\": \".\"}"); assertTrue(putRepositoryResponse.isAcknowledged()); - createIndex(testIndex, Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1,3)) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put(IndexMetadata.SETTING_INDEX_HIDDEN, true) - .build()); + createIndex( + testIndex, + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 3)) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put(IndexMetadata.SETTING_INDEX_HIDDEN, true) + .build() + ); assertTrue("index [" + testIndex + "] should have been created", indexExists(testIndex)); CreateSnapshotRequest createSnapshotRequest = new CreateSnapshotRequest(testRepository, testSnapshot); @@ -329,8 +356,11 @@ public void testSnapshotHidden() throws IOException { request.indices(randomFrom(testIndex, "test_*")); request.renamePattern(testIndex); - RestoreSnapshotResponse response = execute(request, highLevelClient().snapshot()::restore, - highLevelClient().snapshot()::restoreAsync); + RestoreSnapshotResponse response = execute( + request, + highLevelClient().snapshot()::restore, + highLevelClient().snapshot()::restoreAsync + ); RestoreInfo restoreInfo = response.getRestoreInfo(); assertThat(restoreInfo.name(), equalTo(testSnapshot)); @@ -365,7 +395,7 @@ public void testCloneSnapshot() throws IOException { String repository = "test_repository"; String snapshot = "source_snapshot"; String targetSnapshot = "target_snapshot"; - final String testIndex = "test_idx"; + final String testIndex = "test_idx"; createIndex(testIndex, Settings.EMPTY); assertTrue("index [" + testIndex + "] should have been created", indexExists(testIndex)); @@ -379,7 +409,7 @@ public void testCloneSnapshot() throws IOException { CreateSnapshotResponse createSnapshotResponse = createTestSnapshot(createSnapshotRequest); assertEquals(RestStatus.OK, createSnapshotResponse.status()); - CloneSnapshotRequest request = new CloneSnapshotRequest(repository, snapshot, targetSnapshot, new String[]{testIndex}); + CloneSnapshotRequest request = new CloneSnapshotRequest(repository, snapshot, targetSnapshot, new String[] { testIndex }); AcknowledgedResponse response = execute(request, highLevelClient().snapshot()::clone, highLevelClient().snapshot()::cloneAsync); assertTrue(response.isAcknowledged()); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/SnapshotRequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/SnapshotRequestConvertersTests.java index 71d0b714a479a..0e488e1f763f3 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/SnapshotRequestConvertersTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/SnapshotRequestConvertersTests.java @@ -22,9 +22,9 @@ import org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotRequest; import org.elasticsearch.action.admin.cluster.snapshots.status.SnapshotsStatusRequest; import org.elasticsearch.action.support.master.AcknowledgedRequest; -import org.elasticsearch.core.PathUtils; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeUnit; +import org.elasticsearch.core.PathUtils; import org.elasticsearch.repositories.fs.FsRepository; import org.elasticsearch.test.ESTestCase; @@ -49,7 +49,7 @@ public void testGetRepositories() { RequestConvertersTests.setRandomLocal(getRepositoriesRequest::local, expectedParams); if (randomBoolean()) { - String[] entries = new String[]{"a", "b", "c"}; + String[] entries = new String[] { "a", "b", "c" }; getRepositoriesRequest.repositories(entries); endpoint.append("/" + String.join(",", entries)); } @@ -74,7 +74,8 @@ public void testCreateRepository() throws IOException { .put(FsRepository.LOCATION_SETTING.getKey(), repositoryLocation) .put(FsRepository.COMPRESS_SETTING.getKey(), randomBoolean()) .put(FsRepository.CHUNK_SIZE_SETTING.getKey(), randomIntBetween(100, 1000), ByteSizeUnit.BYTES) - .build()); + .build() + ); Request request = SnapshotRequestConverters.createRepository(putRepositoryRequest); assertThat(request.getEndpoint(), equalTo(endpoint)); @@ -151,7 +152,7 @@ public void testGetSnapshots() { GetSnapshotsRequest getSnapshotsRequest = new GetSnapshotsRequest(); getSnapshotsRequest.repositories(repository1, repository2); - getSnapshotsRequest.snapshots(new String[]{snapshot1, snapshot2}); + getSnapshotsRequest.snapshots(new String[] { snapshot1, snapshot2 }); RequestConvertersTests.setRandomMasterTimeout(getSnapshotsRequest, expectedParams); if (randomBoolean()) { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/StoredScriptsIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/StoredScriptsIT.java index c9a0c10d35ef6..2c8096e83fe1f 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/StoredScriptsIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/StoredScriptsIT.java @@ -13,10 +13,10 @@ import org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptResponse; import org.elasticsearch.action.admin.cluster.storedscripts.PutStoredScriptRequest; import org.elasticsearch.common.bytes.BytesArray; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.script.Script; import org.elasticsearch.script.StoredScriptSource; +import org.elasticsearch.xcontent.XContentType; import java.util.Collections; import java.util.Map; @@ -30,32 +30,31 @@ public class StoredScriptsIT extends ESRestHighLevelClientTestCase { private static final String id = "calculate-score"; public void testGetStoredScript() throws Exception { - final StoredScriptSource scriptSource = - new StoredScriptSource("painless", - "Math.log(_score * 2) + params.my_modifier", - Collections.singletonMap(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType())); + final StoredScriptSource scriptSource = new StoredScriptSource( + "painless", + "Math.log(_score * 2) + params.my_modifier", + Collections.singletonMap(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType()) + ); - PutStoredScriptRequest request = - new PutStoredScriptRequest(id, "score", new BytesArray("{}"), XContentType.JSON, scriptSource); + PutStoredScriptRequest request = new PutStoredScriptRequest(id, "score", new BytesArray("{}"), XContentType.JSON, scriptSource); assertAcked(execute(request, highLevelClient()::putScript, highLevelClient()::putScriptAsync)); GetStoredScriptRequest getRequest = new GetStoredScriptRequest("calculate-score"); getRequest.masterNodeTimeout("50s"); - GetStoredScriptResponse getResponse = execute(getRequest, highLevelClient()::getScript, - highLevelClient()::getScriptAsync); + GetStoredScriptResponse getResponse = execute(getRequest, highLevelClient()::getScript, highLevelClient()::getScriptAsync); assertThat(getResponse.getSource(), equalTo(scriptSource)); } public void testDeleteStoredScript() throws Exception { - final StoredScriptSource scriptSource = - new StoredScriptSource("painless", - "Math.log(_score * 2) + params.my_modifier", - Collections.singletonMap(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType())); + final StoredScriptSource scriptSource = new StoredScriptSource( + "painless", + "Math.log(_score * 2) + params.my_modifier", + Collections.singletonMap(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType()) + ); - PutStoredScriptRequest request = - new PutStoredScriptRequest(id, "score", new BytesArray("{}"), XContentType.JSON, scriptSource); + PutStoredScriptRequest request = new PutStoredScriptRequest(id, "score", new BytesArray("{}"), XContentType.JSON, scriptSource); assertAcked(execute(request, highLevelClient()::putScript, highLevelClient()::putScriptAsync)); DeleteStoredScriptRequest deleteRequest = new DeleteStoredScriptRequest(id); @@ -65,20 +64,21 @@ public void testDeleteStoredScript() throws Exception { GetStoredScriptRequest getRequest = new GetStoredScriptRequest(id); - final ElasticsearchStatusException statusException = expectThrows(ElasticsearchStatusException.class, - () -> execute(getRequest, highLevelClient()::getScript, - highLevelClient()::getScriptAsync)); + final ElasticsearchStatusException statusException = expectThrows( + ElasticsearchStatusException.class, + () -> execute(getRequest, highLevelClient()::getScript, highLevelClient()::getScriptAsync) + ); assertThat(statusException.status(), equalTo(RestStatus.NOT_FOUND)); } public void testPutScript() throws Exception { - final StoredScriptSource scriptSource = - new StoredScriptSource("painless", - "Math.log(_score * 2) + params.my_modifier", - Collections.singletonMap(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType())); + final StoredScriptSource scriptSource = new StoredScriptSource( + "painless", + "Math.log(_score * 2) + params.my_modifier", + Collections.singletonMap(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType()) + ); - PutStoredScriptRequest request = - new PutStoredScriptRequest(id, "score", new BytesArray("{}"), XContentType.JSON, scriptSource); + PutStoredScriptRequest request = new PutStoredScriptRequest(id, "score", new BytesArray("{}"), XContentType.JSON, scriptSource); assertAcked(execute(request, highLevelClient()::putScript, highLevelClient()::putScriptAsync)); Map script = getAsMap("/_scripts/" + id); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/TasksIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/TasksIT.java index 5e0462c0cffec..5d2d0916b8a53 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/TasksIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/TasksIT.java @@ -21,9 +21,9 @@ import org.elasticsearch.client.tasks.TaskId; import org.elasticsearch.client.tasks.TaskSubmissionResponse; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.reindex.ReindexRequest; import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.xcontent.XContentType; import java.io.IOException; import java.util.Collections; @@ -69,10 +69,11 @@ public void testGetValidTask() throws Exception { Settings settings = Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0).build(); createIndex(sourceIndex, settings); createIndex(destinationIndex, settings); - BulkRequest bulkRequest = new BulkRequest() - .add(new IndexRequest(sourceIndex).id("1").source(Collections.singletonMap("foo", "bar"), XContentType.JSON)) - .add(new IndexRequest(sourceIndex).id("2").source(Collections.singletonMap("foo2", "bar2"), XContentType.JSON)) - .setRefreshPolicy(RefreshPolicy.IMMEDIATE); + BulkRequest bulkRequest = new BulkRequest().add( + new IndexRequest(sourceIndex).id("1").source(Collections.singletonMap("foo", "bar"), XContentType.JSON) + ) + .add(new IndexRequest(sourceIndex).id("2").source(Collections.singletonMap("foo2", "bar2"), XContentType.JSON)) + .setRefreshPolicy(RefreshPolicy.IMMEDIATE); assertEquals(RestStatus.OK, highLevelClient().bulk(bulkRequest, RequestOptions.DEFAULT).status()); final ReindexRequest reindexRequest = new ReindexRequest().setSourceIndices(sourceIndex).setDestIndex(destinationIndex); @@ -108,22 +109,18 @@ public void testGetInvalidTask() throws IOException { public void testCancelTasks() throws IOException { ListTasksRequest listRequest = new ListTasksRequest(); - ListTasksResponse listResponse = execute( - listRequest, - highLevelClient().tasks()::list, - highLevelClient().tasks()::listAsync - ); + ListTasksResponse listResponse = execute(listRequest, highLevelClient().tasks()::list, highLevelClient().tasks()::listAsync); // in this case, probably no task will actually be cancelled. // this is ok, that case is covered in TasksIT.testTasksCancellation org.elasticsearch.tasks.TaskInfo firstTask = listResponse.getTasks().get(0); String node = listResponse.getPerNodeTasks().keySet().iterator().next(); - CancelTasksRequest cancelTasksRequest = new CancelTasksRequest.Builder().withTaskId( - new TaskId(node, firstTask.getId()) - ).build(); - CancelTasksResponse response = execute(cancelTasksRequest, + CancelTasksRequest cancelTasksRequest = new CancelTasksRequest.Builder().withTaskId(new TaskId(node, firstTask.getId())).build(); + CancelTasksResponse response = execute( + cancelTasksRequest, highLevelClient().tasks()::cancel, - highLevelClient().tasks()::cancelAsync); + highLevelClient().tasks()::cancelAsync + ); // Since the task may or may not have been cancelled, assert that we received a response only // The actual testing of task cancellation is covered by TasksIT.testTasksCancellation assertThat(response, notNullValue()); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/TasksRequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/TasksRequestConvertersTests.java index 292250f7bccbc..7b115b1f4c6f1 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/TasksRequestConvertersTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/TasksRequestConvertersTests.java @@ -26,10 +26,14 @@ public class TasksRequestConvertersTests extends ESTestCase { public void testCancelTasks() { Map expectedParams = new HashMap<>(); - org.elasticsearch.client.tasks.TaskId taskId = - new org.elasticsearch.client.tasks.TaskId(randomAlphaOfLength(5), randomNonNegativeLong()); - org.elasticsearch.client.tasks.TaskId parentTaskId = - new org.elasticsearch.client.tasks.TaskId(randomAlphaOfLength(5), randomNonNegativeLong()); + org.elasticsearch.client.tasks.TaskId taskId = new org.elasticsearch.client.tasks.TaskId( + randomAlphaOfLength(5), + randomNonNegativeLong() + ); + org.elasticsearch.client.tasks.TaskId parentTaskId = new org.elasticsearch.client.tasks.TaskId( + randomAlphaOfLength(5), + randomNonNegativeLong() + ); CancelTasksRequest.Builder builder = new CancelTasksRequest.Builder().withTaskId(taskId).withParentTaskId(parentTaskId); expectedParams.put("task_id", taskId.toString()); expectedParams.put("parent_task_id", parentTaskId.toString()); @@ -99,8 +103,10 @@ public void testListTasks() { { ListTasksRequest request = new ListTasksRequest(); request.setTaskId(new TaskId(randomAlphaOfLength(5), randomNonNegativeLong())); - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () - -> TasksRequestConverters.listTasks(request)); + IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> TasksRequestConverters.listTasks(request) + ); assertEquals("TaskId cannot be used for list tasks request", exception.getMessage()); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/TextStructureIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/TextStructureIT.java index 2c94e56f90d79..de7bb7253159f 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/TextStructureIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/TextStructureIT.java @@ -7,26 +7,26 @@ */ package org.elasticsearch.client; +import org.elasticsearch.client.textstructure.FindStructureRequest; +import org.elasticsearch.client.textstructure.FindStructureResponse; +import org.elasticsearch.client.textstructure.structurefinder.TextStructure; + import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.Locale; -import org.elasticsearch.client.textstructure.FindStructureRequest; -import org.elasticsearch.client.textstructure.FindStructureResponse; -import org.elasticsearch.client.textstructure.structurefinder.TextStructure; - @SuppressWarnings("removal") public class TextStructureIT extends ESRestHighLevelClientTestCase { public void testFindFileStructure() throws IOException { - String sample = "{\"logger\":\"controller\",\"timestamp\":1478261151445,\"level\":\"INFO\"," + - "\"pid\":42,\"thread\":\"0x7fff7d2a8000\",\"message\":\"message 1\",\"class\":\"ml\"," + - "\"method\":\"core::SomeNoiseMaker\",\"file\":\"Noisemaker.cc\",\"line\":333}\n" + - "{\"logger\":\"controller\",\"timestamp\":1478261151445," + - "\"level\":\"INFO\",\"pid\":42,\"thread\":\"0x7fff7d2a8000\",\"message\":\"message 2\",\"class\":\"ml\"," + - "\"method\":\"core::SomeNoiseMaker\",\"file\":\"Noisemaker.cc\",\"line\":333}\n"; + String sample = "{\"logger\":\"controller\",\"timestamp\":1478261151445,\"level\":\"INFO\"," + + "\"pid\":42,\"thread\":\"0x7fff7d2a8000\",\"message\":\"message 1\",\"class\":\"ml\"," + + "\"method\":\"core::SomeNoiseMaker\",\"file\":\"Noisemaker.cc\",\"line\":333}\n" + + "{\"logger\":\"controller\",\"timestamp\":1478261151445," + + "\"level\":\"INFO\",\"pid\":42,\"thread\":\"0x7fff7d2a8000\",\"message\":\"message 2\",\"class\":\"ml\"," + + "\"method\":\"core::SomeNoiseMaker\",\"file\":\"Noisemaker.cc\",\"line\":333}\n"; TextStructureClient textStructureClient = highLevelClient().textStructure(); @@ -37,7 +37,8 @@ public void testFindFileStructure() throws IOException { request, textStructureClient::findStructure, textStructureClient::findStructureAsync, - RequestOptions.DEFAULT); + RequestOptions.DEFAULT + ); TextStructure structure = response.getFileStructure(); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/TextStructureRequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/TextStructureRequestConvertersTests.java index 52785b7a4eef1..cbb2f1b07b0ad 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/TextStructureRequestConvertersTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/TextStructureRequestConvertersTests.java @@ -8,10 +8,6 @@ package org.elasticsearch.client; -import java.io.ByteArrayOutputStream; -import java.nio.charset.StandardCharsets; -import java.util.Arrays; - import org.apache.http.client.methods.HttpPost; import org.elasticsearch.client.textstructure.FindStructureRequest; import org.elasticsearch.client.textstructure.FindStructureRequestTests; @@ -19,6 +15,10 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.test.ESTestCase; +import java.io.ByteArrayOutputStream; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; + public class TextStructureRequestConvertersTests extends ESTestCase { public void testFindFileStructure() throws Exception { @@ -51,8 +51,10 @@ public void testFindFileStructure() throws Exception { assertNull(request.getParameters().get("format")); } if (findStructureRequest.getColumnNames() != null) { - assertEquals(findStructureRequest.getColumnNames(), - Arrays.asList(Strings.splitStringByCommaToArray(request.getParameters().get("column_names")))); + assertEquals( + findStructureRequest.getColumnNames(), + Arrays.asList(Strings.splitStringByCommaToArray(request.getParameters().get("column_names"))) + ); } else { assertNull(request.getParameters().get("column_names")); } @@ -72,8 +74,7 @@ public void testFindFileStructure() throws Exception { assertNull(request.getParameters().get("quote")); } if (findStructureRequest.getShouldTrimFields() != null) { - assertEquals(findStructureRequest.getShouldTrimFields(), - Boolean.valueOf(request.getParameters().get("should_trim_fields"))); + assertEquals(findStructureRequest.getShouldTrimFields(), Boolean.valueOf(request.getParameters().get("should_trim_fields"))); } else { assertNull(request.getParameters().get("should_trim_fields")); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/TimedRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/TimedRequestTests.java index ec22d133a8718..82cec52e75494 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/TimedRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/TimedRequestTests.java @@ -14,15 +14,17 @@ public class TimedRequestTests extends ESTestCase { public void testDefaults() { - TimedRequest timedRequest = new TimedRequest(){}; + TimedRequest timedRequest = new TimedRequest() { + }; assertEquals(timedRequest.timeout(), TimedRequest.DEFAULT_ACK_TIMEOUT); assertEquals(timedRequest.masterNodeTimeout(), TimedRequest.DEFAULT_MASTER_NODE_TIMEOUT); } public void testNonDefaults() { - TimedRequest timedRequest = new TimedRequest(){}; + TimedRequest timedRequest = new TimedRequest() { + }; TimeValue timeout = TimeValue.timeValueSeconds(randomIntBetween(0, 1000)); - TimeValue masterTimeout = TimeValue.timeValueSeconds(randomIntBetween(0,1000)); + TimeValue masterTimeout = TimeValue.timeValueSeconds(randomIntBetween(0, 1000)); timedRequest.setTimeout(timeout); timedRequest.setMasterTimeout(masterTimeout); assertEquals(timedRequest.timeout(), timeout); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/UpdateByQueryIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/UpdateByQueryIT.java index 0adb33eb9fe86..85cae4bdf27bf 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/UpdateByQueryIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/UpdateByQueryIT.java @@ -17,7 +17,6 @@ import org.elasticsearch.action.support.WriteRequest; import org.elasticsearch.client.tasks.TaskSubmissionResponse; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.query.IdsQueryBuilder; import org.elasticsearch.index.reindex.BulkByScrollResponse; import org.elasticsearch.index.reindex.UpdateByQueryAction; @@ -26,6 +25,7 @@ import org.elasticsearch.script.Script; import org.elasticsearch.tasks.RawTaskStatus; import org.elasticsearch.tasks.TaskId; +import org.elasticsearch.xcontent.XContentType; import java.io.IOException; import java.util.Collections; @@ -46,19 +46,15 @@ public void testUpdateByQuery() throws Exception { final String sourceIndex = "source1"; { // Prepare - Settings settings = Settings.builder() - .put("number_of_shards", 1) - .put("number_of_replicas", 0) - .build(); + Settings settings = Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0).build(); createIndex(sourceIndex, settings); assertEquals( RestStatus.OK, highLevelClient().bulk( - new BulkRequest() - .add(new IndexRequest(sourceIndex).id("1") - .source(Collections.singletonMap("foo", 1), XContentType.JSON)) - .add(new IndexRequest(sourceIndex).id("2") - .source(Collections.singletonMap("foo", 2), XContentType.JSON)) + new BulkRequest().add( + new IndexRequest(sourceIndex).id("1").source(Collections.singletonMap("foo", 1), XContentType.JSON) + ) + .add(new IndexRequest(sourceIndex).id("2").source(Collections.singletonMap("foo", 2), XContentType.JSON)) .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT ).status() @@ -70,8 +66,11 @@ public void testUpdateByQuery() throws Exception { updateByQueryRequest.indices(sourceIndex); updateByQueryRequest.setQuery(new IdsQueryBuilder().addIds("1")); updateByQueryRequest.setRefresh(true); - BulkByScrollResponse bulkResponse = - execute(updateByQueryRequest, highLevelClient()::updateByQuery, highLevelClient()::updateByQueryAsync); + BulkByScrollResponse bulkResponse = execute( + updateByQueryRequest, + highLevelClient()::updateByQuery, + highLevelClient()::updateByQueryAsync + ); assertEquals(1, bulkResponse.getTotal()); assertEquals(1, bulkResponse.getUpdated()); assertEquals(0, bulkResponse.getNoops()); @@ -88,8 +87,11 @@ public void testUpdateByQuery() throws Exception { updateByQueryRequest.indices(sourceIndex); updateByQueryRequest.setScript(new Script("if (ctx._source.foo == 2) ctx._source.foo++;")); updateByQueryRequest.setRefresh(true); - BulkByScrollResponse bulkResponse = - execute(updateByQueryRequest, highLevelClient()::updateByQuery, highLevelClient()::updateByQueryAsync); + BulkByScrollResponse bulkResponse = execute( + updateByQueryRequest, + highLevelClient()::updateByQuery, + highLevelClient()::updateByQueryAsync + ); assertEquals(2, bulkResponse.getTotal()); assertEquals(2, bulkResponse.getUpdated()); assertEquals(0, bulkResponse.getDeleted()); @@ -102,8 +104,7 @@ public void testUpdateByQuery() throws Exception { assertEquals(0, bulkResponse.getSearchFailures().size()); assertEquals( 3, - (int) (highLevelClient().get(new GetRequest(sourceIndex, "2"), RequestOptions.DEFAULT) - .getSourceAsMap().get("foo")) + (int) (highLevelClient().get(new GetRequest(sourceIndex, "2"), RequestOptions.DEFAULT).getSourceAsMap().get("foo")) ); } { @@ -132,23 +133,33 @@ public void onFailure(Exception e) { TaskId taskIdToRethrottle = findTaskToRethrottle(UpdateByQueryAction.NAME, updateByQueryRequest.getDescription()); float requestsPerSecond = 1000f; - ListTasksResponse response = execute(new RethrottleRequest(taskIdToRethrottle, requestsPerSecond), - highLevelClient()::updateByQueryRethrottle, highLevelClient()::updateByQueryRethrottleAsync); + ListTasksResponse response = execute( + new RethrottleRequest(taskIdToRethrottle, requestsPerSecond), + highLevelClient()::updateByQueryRethrottle, + highLevelClient()::updateByQueryRethrottleAsync + ); assertThat(response.getTasks(), hasSize(1)); assertEquals(taskIdToRethrottle, response.getTasks().get(0).getTaskId()); assertThat(response.getTasks().get(0).getStatus(), instanceOf(RawTaskStatus.class)); - assertEquals(Float.toString(requestsPerSecond), - ((RawTaskStatus) response.getTasks().get(0).getStatus()).toMap().get("requests_per_second").toString()); + assertEquals( + Float.toString(requestsPerSecond), + ((RawTaskStatus) response.getTasks().get(0).getStatus()).toMap().get("requests_per_second").toString() + ); assertTrue(taskFinished.await(10, TimeUnit.SECONDS)); // any rethrottling after the update-by-query is done performed with the same taskId should result in a failure - response = execute(new RethrottleRequest(taskIdToRethrottle, requestsPerSecond), - highLevelClient()::updateByQueryRethrottle, highLevelClient()::updateByQueryRethrottleAsync); + response = execute( + new RethrottleRequest(taskIdToRethrottle, requestsPerSecond), + highLevelClient()::updateByQueryRethrottle, + highLevelClient()::updateByQueryRethrottleAsync + ); assertTrue(response.getTasks().isEmpty()); assertFalse(response.getNodeFailures().isEmpty()); assertEquals(1, response.getNodeFailures().size()); - assertEquals("Elasticsearch exception [type=resource_not_found_exception, reason=task [" + taskIdToRethrottle + "] is missing]", - response.getNodeFailures().get(0).getCause().getMessage()); + assertEquals( + "Elasticsearch exception [type=resource_not_found_exception, reason=task [" + taskIdToRethrottle + "] is missing]", + response.getNodeFailures().get(0).getCause().getMessage() + ); } } @@ -156,21 +167,16 @@ public void testUpdateByQueryTask() throws Exception { final String sourceIndex = "testupdatebyquerytask"; { // Prepare - Settings settings = Settings.builder() - .put("number_of_shards", 1) - .put("number_of_replicas", 0) - .build(); + Settings settings = Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0).build(); createIndex(sourceIndex, settings); assertEquals( RestStatus.OK, highLevelClient().bulk( - new BulkRequest() - .add(new IndexRequest(sourceIndex).id("1") - .source(Collections.singletonMap("foo", 1), XContentType.JSON)) - .add(new IndexRequest(sourceIndex).id("2") - .source(Collections.singletonMap("foo", 2), XContentType.JSON)) - .add(new IndexRequest(sourceIndex).id("3") - .source(Collections.singletonMap("foo", 3), XContentType.JSON)) + new BulkRequest().add( + new IndexRequest(sourceIndex).id("1").source(Collections.singletonMap("foo", 1), XContentType.JSON) + ) + .add(new IndexRequest(sourceIndex).id("2").source(Collections.singletonMap("foo", 2), XContentType.JSON)) + .add(new IndexRequest(sourceIndex).id("3").source(Collections.singletonMap("foo", 3), XContentType.JSON)) .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT ).status() @@ -197,13 +203,11 @@ public void testUpdateByQueryTask() throws Exception { public void testUpdateByQueryConflict() throws IOException { final String index = "testupdatebyqueryconflict"; - final Settings settings = Settings.builder() - .put("number_of_shards", 1) - .put("number_of_replicas", 0) - .build(); + final Settings settings = Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0).build(); createIndex(index, settings); - final BulkRequest bulkRequest = new BulkRequest() - .add(new IndexRequest(index).id("1").source(Collections.singletonMap("foo", "bar"), XContentType.JSON)) + final BulkRequest bulkRequest = new BulkRequest().add( + new IndexRequest(index).id("1").source(Collections.singletonMap("foo", "bar"), XContentType.JSON) + ) .add(new IndexRequest(index).id("2").source(Collections.singletonMap("foo", "bar"), XContentType.JSON)) .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); assertThat(highLevelClient().bulk(bulkRequest, RequestOptions.DEFAULT).status(), equalTo(RestStatus.OK)); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherIT.java index e86fd45b5e073..3f35abe0eab76 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherIT.java @@ -30,9 +30,9 @@ import org.elasticsearch.client.watcher.WatcherStatsResponse; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.rest.RestStatus; import org.elasticsearch.xcontent.ObjectPath; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.rest.RestStatus; import java.util.Map; @@ -45,21 +45,20 @@ public class WatcherIT extends ESRestHighLevelClientTestCase { public void testStartWatchService() throws Exception { - AcknowledgedResponse response = - highLevelClient().watcher().startWatchService(new StartWatchServiceRequest(), RequestOptions.DEFAULT); + AcknowledgedResponse response = highLevelClient().watcher() + .startWatchService(new StartWatchServiceRequest(), RequestOptions.DEFAULT); assertTrue(response.isAcknowledged()); WatcherStatsResponse stats = highLevelClient().watcher().watcherStats(new WatcherStatsRequest(), RequestOptions.DEFAULT); assertFalse(stats.getWatcherMetadata().manuallyStopped()); assertThat(stats.getNodes(), not(empty())); - for(WatcherStatsResponse.Node node : stats.getNodes()) { + for (WatcherStatsResponse.Node node : stats.getNodes()) { assertEquals(WatcherState.STARTED, node.getWatcherState()); } } public void testStopWatchService() throws Exception { - AcknowledgedResponse response = - highLevelClient().watcher().stopWatchService(new StopWatchServiceRequest(), RequestOptions.DEFAULT); + AcknowledgedResponse response = highLevelClient().watcher().stopWatchService(new StopWatchServiceRequest(), RequestOptions.DEFAULT); assertTrue(response.isAcknowledged()); WatcherStatsResponse stats = highLevelClient().watcher().watcherStats(new WatcherStatsRequest(), RequestOptions.DEFAULT); @@ -74,11 +73,11 @@ public void testPutWatch() throws Exception { assertThat(putWatchResponse.getVersion(), is(1L)); } - private static final String WATCH_JSON = "{ \n" + - " \"trigger\": { \"schedule\": { \"interval\": \"10h\" } },\n" + - " \"input\": { \"none\": {} },\n" + - " \"actions\": { \"logme\": { \"logging\": { \"text\": \"{{ctx.payload}}\" } } }\n" + - "}"; + private static final String WATCH_JSON = "{ \n" + + " \"trigger\": { \"schedule\": { \"interval\": \"10h\" } },\n" + + " \"input\": { \"none\": {} },\n" + + " \"actions\": { \"logme\": { \"logging\": { \"text\": \"{{ctx.payload}}\" } } }\n" + + "}"; private PutWatchResponse createWatch(String watchId) throws Exception { BytesReference bytesReference = new BytesArray(WATCH_JSON); @@ -90,15 +89,18 @@ public void testDeactivateWatch() throws Exception { // Deactivate a watch that exists String watchId = randomAlphaOfLength(10); createWatch(watchId); - DeactivateWatchResponse response = highLevelClient().watcher().deactivateWatch( - new DeactivateWatchRequest(watchId), RequestOptions.DEFAULT); + DeactivateWatchResponse response = highLevelClient().watcher() + .deactivateWatch(new DeactivateWatchRequest(watchId), RequestOptions.DEFAULT); assertThat(response.getStatus().state().isActive(), is(false)); } + public void testDeactivateWatch404() throws Exception { // Deactivate a watch that does not exist String watchId = randomAlphaOfLength(10); - ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, - () -> highLevelClient().watcher().deactivateWatch(new DeactivateWatchRequest(watchId), RequestOptions.DEFAULT)); + ElasticsearchStatusException exception = expectThrows( + ElasticsearchStatusException.class, + () -> highLevelClient().watcher().deactivateWatch(new DeactivateWatchRequest(watchId), RequestOptions.DEFAULT) + ); assertEquals(RestStatus.NOT_FOUND, exception.status()); } @@ -108,8 +110,8 @@ public void testDeleteWatch() throws Exception { { String watchId = randomAlphaOfLength(10); createWatch(watchId); - DeleteWatchResponse deleteWatchResponse = highLevelClient().watcher().deleteWatch(new DeleteWatchRequest(watchId), - RequestOptions.DEFAULT); + DeleteWatchResponse deleteWatchResponse = highLevelClient().watcher() + .deleteWatch(new DeleteWatchRequest(watchId), RequestOptions.DEFAULT); assertThat(deleteWatchResponse.getId(), is(watchId)); assertThat(deleteWatchResponse.getVersion(), is(2L)); assertThat(deleteWatchResponse.isFound(), is(true)); @@ -118,8 +120,8 @@ public void testDeleteWatch() throws Exception { // delete watch that does not exist { String watchId = randomAlphaOfLength(10); - DeleteWatchResponse deleteWatchResponse = highLevelClient().watcher().deleteWatch(new DeleteWatchRequest(watchId), - RequestOptions.DEFAULT); + DeleteWatchResponse deleteWatchResponse = highLevelClient().watcher() + .deleteWatch(new DeleteWatchRequest(watchId), RequestOptions.DEFAULT); assertThat(deleteWatchResponse.getId(), is(watchId)); assertThat(deleteWatchResponse.getVersion(), is(1L)); assertThat(deleteWatchResponse.isFound(), is(false)); @@ -133,8 +135,7 @@ public void testAckWatch() throws Exception { PutWatchResponse putWatchResponse = createWatch(watchId); assertThat(putWatchResponse.isCreated(), is(true)); - AckWatchResponse response = highLevelClient().watcher().ackWatch( - new AckWatchRequest(watchId, actionId), RequestOptions.DEFAULT); + AckWatchResponse response = highLevelClient().watcher().ackWatch(new AckWatchRequest(watchId, actionId), RequestOptions.DEFAULT); ActionStatus actionStatus = response.getStatus().actionStatus(actionId); assertEquals(AckStatus.State.AWAITS_SUCCESSFUL_EXECUTION, actionStatus.ackStatus().state()); @@ -145,47 +146,49 @@ public void testAckWatch() throws Exception { Response executeResponse = client().performRequest(executeWatchRequest); assertEquals(RestStatus.OK.getStatus(), executeResponse.getStatusLine().getStatusCode()); - response = highLevelClient().watcher().ackWatch( - new AckWatchRequest(watchId, actionId), RequestOptions.DEFAULT); + response = highLevelClient().watcher().ackWatch(new AckWatchRequest(watchId, actionId), RequestOptions.DEFAULT); actionStatus = response.getStatus().actionStatus(actionId); assertEquals(AckStatus.State.ACKED, actionStatus.ackStatus().state()); - ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, - () -> highLevelClient().watcher().ackWatch( - new AckWatchRequest("nonexistent"), RequestOptions.DEFAULT)); + ElasticsearchStatusException exception = expectThrows( + ElasticsearchStatusException.class, + () -> highLevelClient().watcher().ackWatch(new AckWatchRequest("nonexistent"), RequestOptions.DEFAULT) + ); assertEquals(RestStatus.NOT_FOUND, exception.status()); } public void testActivateWatchThatExists() throws Exception { String watchId = randomAlphaOfLength(10); createWatch(watchId); - ActivateWatchResponse activateWatchResponse1 = highLevelClient().watcher().activateWatch(new ActivateWatchRequest(watchId), - RequestOptions.DEFAULT); + ActivateWatchResponse activateWatchResponse1 = highLevelClient().watcher() + .activateWatch(new ActivateWatchRequest(watchId), RequestOptions.DEFAULT); assertThat(activateWatchResponse1.getStatus().state().isActive(), is(true)); - ActivateWatchResponse activateWatchResponse2 = highLevelClient().watcher().activateWatch(new ActivateWatchRequest(watchId), - RequestOptions.DEFAULT); + ActivateWatchResponse activateWatchResponse2 = highLevelClient().watcher() + .activateWatch(new ActivateWatchRequest(watchId), RequestOptions.DEFAULT); assertThat(activateWatchResponse2.getStatus().state().isActive(), is(true)); - assertThat(activateWatchResponse1.getStatus().state().getTimestamp(), - lessThan(activateWatchResponse2.getStatus().state().getTimestamp())); + assertThat( + activateWatchResponse1.getStatus().state().getTimestamp(), + lessThan(activateWatchResponse2.getStatus().state().getTimestamp()) + ); } public void testActivateWatchThatDoesNotExist() throws Exception { String watchId = randomAlphaOfLength(10); // exception when activating a not existing watcher - ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, () -> - highLevelClient().watcher().activateWatch(new ActivateWatchRequest(watchId), RequestOptions.DEFAULT)); + ElasticsearchStatusException exception = expectThrows( + ElasticsearchStatusException.class, + () -> highLevelClient().watcher().activateWatch(new ActivateWatchRequest(watchId), RequestOptions.DEFAULT) + ); assertEquals(RestStatus.NOT_FOUND, exception.status()); } - public void testExecuteWatchById() throws Exception { String watchId = randomAlphaOfLength(10); createWatch(watchId); - ExecuteWatchResponse response = highLevelClient().watcher() - .executeWatch(ExecuteWatchRequest.byId(watchId), RequestOptions.DEFAULT); + ExecuteWatchResponse response = highLevelClient().watcher().executeWatch(ExecuteWatchRequest.byId(watchId), RequestOptions.DEFAULT); assertThat(response.getRecordId(), containsString(watchId)); Map source = response.getRecordAsMap(); @@ -196,8 +199,10 @@ public void testExecuteWatchById() throws Exception { public void testExecuteWatchThatDoesNotExist() throws Exception { String watchId = randomAlphaOfLength(10); // exception when activating a not existing watcher - ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, () -> - highLevelClient().watcher().executeWatch(ExecuteWatchRequest.byId(watchId), RequestOptions.DEFAULT)); + ElasticsearchStatusException exception = expectThrows( + ElasticsearchStatusException.class, + () -> highLevelClient().watcher().executeWatch(ExecuteWatchRequest.byId(watchId), RequestOptions.DEFAULT) + ); assertEquals(RestStatus.NOT_FOUND, exception.status()); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherRequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherRequestConvertersTests.java index 468d2cd843c44..2a13be0cb7a4e 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherRequestConvertersTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherRequestConvertersTests.java @@ -25,8 +25,8 @@ import org.elasticsearch.client.watcher.WatcherStatsRequest; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesArray; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentType; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -133,8 +133,7 @@ public void testAckWatch() { assertEquals(HttpPut.METHOD_NAME, request.getMethod()); - StringJoiner expectedEndpoint = new StringJoiner("/", "/", "") - .add("_watcher").add("watch").add(watchId).add("_ack"); + StringJoiner expectedEndpoint = new StringJoiner("/", "/", "").add("_watcher").add("watch").add(watchId).add("_ack"); if (ackWatchRequest.getActionIds().length > 0) { String actionsParam = String.join(",", ackWatchRequest.getActionIds()); expectedEndpoint.add(actionsParam); @@ -166,7 +165,7 @@ public void testWatcherStatsRequest() { if (includeCurrent || includeQueued) { assertThat(request.getParameters(), hasKey("metric")); Set metric = Strings.tokenizeByCommaToSet(request.getParameters().get("metric")); - assertThat(metric, hasSize((includeCurrent?1:0) + (includeQueued?1:0))); + assertThat(metric, hasSize((includeCurrent ? 1 : 0) + (includeQueued ? 1 : 0))); Set expectedMetric = new HashSet<>(); if (includeCurrent) { expectedMetric.add("current_watches"); @@ -231,31 +230,28 @@ public void testExecuteWatchByIdRequest() throws IOException { String body = toString(req.getEntity()); if (setActionMode) { assertThat(body, containsString("\"action_modes\":{\"action1\":\"SIMULATE\"}")); - } - else { + } else { assertThat(body, not(containsString("action_modes"))); } if (useTriggerData) { assertThat(body, containsString("\"trigger_data\":" + triggerData)); - } - else { + } else { assertThat(body, not(containsString("trigger_data"))); } if (useAlternativeInput) { assertThat(body, containsString("\"alternative_input\":" + alternativeInput)); - } - else { + } else { assertThat(body, not(containsString("alternative_input"))); } assertThat(body, not(containsString("\"watch\":"))); } - private static final String WATCH_JSON = "{ \n" + - " \"trigger\": { \"schedule\": { \"interval\": \"10h\" } },\n" + - " \"input\": { \"none\": {} },\n" + - " \"actions\": { \"logme\": { \"logging\": { \"text\": \"{{ctx.payload}}\" } } }\n" + - "}"; + private static final String WATCH_JSON = "{ \n" + + " \"trigger\": { \"schedule\": { \"interval\": \"10h\" } },\n" + + " \"input\": { \"none\": {} },\n" + + " \"actions\": { \"logme\": { \"logging\": { \"text\": \"{{ctx.payload}}\" } } }\n" + + "}"; public void testExecuteInlineWatchRequest() throws IOException { boolean ignoreCondition = randomBoolean(); @@ -263,9 +259,7 @@ public void testExecuteInlineWatchRequest() throws IOException { ExecuteWatchRequest request = ExecuteWatchRequest.inline(WATCH_JSON); request.setIgnoreCondition(ignoreCondition); - expectThrows(IllegalArgumentException.class, () -> { - request.setRecordExecution(true); - }); + expectThrows(IllegalArgumentException.class, () -> { request.setRecordExecution(true); }); boolean setActionMode = randomBoolean(); if (setActionMode) { @@ -296,20 +290,17 @@ public void testExecuteInlineWatchRequest() throws IOException { String body = toString(req.getEntity()); if (setActionMode) { assertThat(body, containsString("\"action_modes\":{\"action1\":\"SIMULATE\"}")); - } - else { + } else { assertThat(body, not(containsString("action_modes"))); } if (useTriggerData) { assertThat(body, containsString("\"trigger_data\":" + triggerData)); - } - else { + } else { assertThat(body, not(containsString("trigger_data"))); } if (useAlternativeInput) { assertThat(body, containsString("\"alternative_input\":" + alternativeInput)); - } - else { + } else { assertThat(body, not(containsString("alternative_input"))); } assertThat(body, containsString("\"watch\":" + WATCH_JSON)); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/XPackInfoResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/XPackInfoResponseTests.java index 08e03b4689fcb..8d54020a512f6 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/XPackInfoResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/XPackInfoResponseTests.java @@ -7,14 +7,14 @@ */ package org.elasticsearch.client; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.protocol.xpack.XPackInfoResponse; import org.elasticsearch.protocol.xpack.XPackInfoResponse.BuildInfo; import org.elasticsearch.protocol.xpack.XPackInfoResponse.FeatureSetsInfo; import org.elasticsearch.protocol.xpack.XPackInfoResponse.FeatureSetsInfo.FeatureSet; import org.elasticsearch.protocol.xpack.XPackInfoResponse.LicenseInfo; import org.elasticsearch.protocol.xpack.license.LicenseStatus; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; import java.io.IOException; import java.util.HashSet; @@ -29,24 +29,30 @@ private BuildInfo convertHlrcToInternal(org.elasticsearch.client.xpack.XPackInfo private LicenseInfo convertHlrcToInternal(org.elasticsearch.client.xpack.XPackInfoResponse.LicenseInfo licenseInfo) { return licenseInfo != null - ? new LicenseInfo(licenseInfo.getUid(), licenseInfo.getType(), licenseInfo.getMode(), + ? new LicenseInfo( + licenseInfo.getUid(), + licenseInfo.getType(), + licenseInfo.getMode(), licenseInfo.getStatus() != null ? LicenseStatus.valueOf(licenseInfo.getStatus().name()) : null, - licenseInfo.getExpiryDate()) + licenseInfo.getExpiryDate() + ) : null; } private FeatureSetsInfo convertHlrcToInternal(org.elasticsearch.client.xpack.XPackInfoResponse.FeatureSetsInfo featureSetsInfo) { return featureSetsInfo != null - ? new FeatureSetsInfo(featureSetsInfo.getFeatureSets().values().stream() - .map(fs -> new FeatureSet(fs.name(), fs.available(), fs.enabled())) - .collect(Collectors.toSet())) + ? new FeatureSetsInfo( + featureSetsInfo.getFeatureSets() + .values() + .stream() + .map(fs -> new FeatureSet(fs.name(), fs.available(), fs.enabled())) + .collect(Collectors.toSet()) + ) : null; } private BuildInfo randomBuildInfo() { - return new BuildInfo( - randomAlphaOfLength(10), - randomAlphaOfLength(15)); + return new BuildInfo(randomAlphaOfLength(10), randomAlphaOfLength(15)); } private LicenseInfo randomLicenseInfo() { @@ -55,7 +61,8 @@ private LicenseInfo randomLicenseInfo() { randomAlphaOfLength(4), randomAlphaOfLength(5), randomFrom(LicenseStatus.values()), - randomLong()); + randomLong() + ); } private FeatureSetsInfo randomFeatureSetsInfo() { @@ -68,10 +75,7 @@ private FeatureSetsInfo randomFeatureSetsInfo() { } private FeatureSet randomFeatureSet() { - return new FeatureSet( - randomAlphaOfLength(5), - randomBoolean(), - randomBoolean()); + return new FeatureSet(randomAlphaOfLength(5), randomBoolean(), randomBoolean()); } @Override @@ -79,7 +83,8 @@ protected XPackInfoResponse createServerTestInstance(XContentType xContentType) return new XPackInfoResponse( randomBoolean() ? null : randomBuildInfo(), randomBoolean() ? null : randomLicenseInfo(), - randomBoolean() ? null : randomFeatureSetsInfo()); + randomBoolean() ? null : randomFeatureSetsInfo() + ); } @Override @@ -89,8 +94,11 @@ protected org.elasticsearch.client.xpack.XPackInfoResponse doParseToClientInstan @Override protected void assertInstances(XPackInfoResponse serverTestInstance, org.elasticsearch.client.xpack.XPackInfoResponse clientInstance) { - XPackInfoResponse serverInstance = new XPackInfoResponse(convertHlrcToInternal(clientInstance.getBuildInfo()), - convertHlrcToInternal(clientInstance.getLicenseInfo()), convertHlrcToInternal(clientInstance.getFeatureSetsInfo())); + XPackInfoResponse serverInstance = new XPackInfoResponse( + convertHlrcToInternal(clientInstance.getBuildInfo()), + convertHlrcToInternal(clientInstance.getLicenseInfo()), + convertHlrcToInternal(clientInstance.getFeatureSetsInfo()) + ); assertEquals(serverTestInstance, serverInstance); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/XPackRequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/XPackRequestConvertersTests.java index e0a6433f6f773..abf003f61e2a4 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/XPackRequestConvertersTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/XPackRequestConvertersTests.java @@ -27,19 +27,19 @@ public void testXPackInfo() { } int option = ESTestCase.between(0, 2); switch (option) { - case 0: - infoRequest.setCategories(EnumSet.allOf(XPackInfoRequest.Category.class)); - break; - case 1: - infoRequest.setCategories(EnumSet.of(XPackInfoRequest.Category.FEATURES)); - expectedParams.put("categories", "features"); - break; - case 2: - infoRequest.setCategories(EnumSet.of(XPackInfoRequest.Category.FEATURES, XPackInfoRequest.Category.BUILD)); - expectedParams.put("categories", "build,features"); - break; - default: - throw new IllegalArgumentException("invalid option [" + option + "]"); + case 0: + infoRequest.setCategories(EnumSet.allOf(XPackInfoRequest.Category.class)); + break; + case 1: + infoRequest.setCategories(EnumSet.of(XPackInfoRequest.Category.FEATURES)); + expectedParams.put("categories", "features"); + break; + case 2: + infoRequest.setCategories(EnumSet.of(XPackInfoRequest.Category.FEATURES, XPackInfoRequest.Category.BUILD)); + expectedParams.put("categories", "build,features"); + break; + default: + throw new IllegalArgumentException("invalid option [" + option + "]"); } Request request = XPackRequestConverters.info(infoRequest); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/analytics/AnalyticsAggsIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/analytics/AnalyticsAggsIT.java index c842c515b9202..c92b08bd1c53e 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/analytics/AnalyticsAggsIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/analytics/AnalyticsAggsIT.java @@ -16,9 +16,9 @@ import org.elasticsearch.client.ESRestHighLevelClientTestCase; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.indices.CreateIndexRequest; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.search.sort.FieldSortBuilder; import org.elasticsearch.search.sort.SortOrder; +import org.elasticsearch.xcontent.XContentType; import java.io.IOException; @@ -54,8 +54,7 @@ public void testStringStats() throws IOException { public void testTopMetricsDoubleMetric() throws IOException { indexTopMetricsDoubleTestData(); SearchRequest search = new SearchRequest("test"); - search.source().aggregation(new TopMetricsAggregationBuilder( - "test", new FieldSortBuilder("s").order(SortOrder.DESC), 1, "v")); + search.source().aggregation(new TopMetricsAggregationBuilder("test", new FieldSortBuilder("s").order(SortOrder.DESC), 1, "v")); SearchResponse response = highLevelClient().search(search, RequestOptions.DEFAULT); ParsedTopMetrics top = response.getAggregations().get("test"); assertThat(top.getTopMetrics(), hasSize(1)); @@ -67,8 +66,7 @@ public void testTopMetricsDoubleMetric() throws IOException { public void testTopMetricsLongMetric() throws IOException { indexTopMetricsLongTestData(); SearchRequest search = new SearchRequest("test"); - search.source().aggregation(new TopMetricsAggregationBuilder( - "test", new FieldSortBuilder("s").order(SortOrder.DESC), 1, "v")); + search.source().aggregation(new TopMetricsAggregationBuilder("test", new FieldSortBuilder("s").order(SortOrder.DESC), 1, "v")); SearchResponse response = highLevelClient().search(search, RequestOptions.DEFAULT); ParsedTopMetrics top = response.getAggregations().get("test"); assertThat(top.getTopMetrics(), hasSize(1)); @@ -80,8 +78,7 @@ public void testTopMetricsLongMetric() throws IOException { public void testTopMetricsDateMetric() throws IOException { indexTopMetricsDateTestData(); SearchRequest search = new SearchRequest("test"); - search.source().aggregation(new TopMetricsAggregationBuilder( - "test", new FieldSortBuilder("s").order(SortOrder.DESC), 1, "v")); + search.source().aggregation(new TopMetricsAggregationBuilder("test", new FieldSortBuilder("s").order(SortOrder.DESC), 1, "v")); SearchResponse response = highLevelClient().search(search, RequestOptions.DEFAULT); ParsedTopMetrics top = response.getAggregations().get("test"); assertThat(top.getTopMetrics(), hasSize(1)); @@ -93,8 +90,7 @@ public void testTopMetricsDateMetric() throws IOException { public void testTopMetricsManyMetrics() throws IOException { indexTopMetricsDoubleTestData(); SearchRequest search = new SearchRequest("test"); - search.source().aggregation(new TopMetricsAggregationBuilder( - "test", new FieldSortBuilder("s").order(SortOrder.DESC), 1, "v", "m")); + search.source().aggregation(new TopMetricsAggregationBuilder("test", new FieldSortBuilder("s").order(SortOrder.DESC), 1, "v", "m")); SearchResponse response = highLevelClient().search(search, RequestOptions.DEFAULT); ParsedTopMetrics top = response.getAggregations().get("test"); assertThat(top.getTopMetrics(), hasSize(1)); @@ -107,8 +103,7 @@ public void testTopMetricsManyMetrics() throws IOException { public void testTopMetricsSizeTwo() throws IOException { indexTopMetricsDoubleTestData(); SearchRequest search = new SearchRequest("test"); - search.source().aggregation(new TopMetricsAggregationBuilder( - "test", new FieldSortBuilder("s").order(SortOrder.DESC), 2, "v")); + search.source().aggregation(new TopMetricsAggregationBuilder("test", new FieldSortBuilder("s").order(SortOrder.DESC), 2, "v")); SearchResponse response = highLevelClient().search(search, RequestOptions.DEFAULT); ParsedTopMetrics top = response.getAggregations().get("test"); assertThat(top.getTopMetrics(), hasSize(2)); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/analytics/InferenceAggIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/analytics/InferenceAggIT.java index 204012c90e0a7..ae982a2e927cd 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/analytics/InferenceAggIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/analytics/InferenceAggIT.java @@ -23,11 +23,11 @@ import org.elasticsearch.client.ml.inference.trainedmodel.RegressionConfig; import org.elasticsearch.client.ml.inference.trainedmodel.tree.Tree; import org.elasticsearch.client.ml.inference.trainedmodel.tree.TreeNode; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.search.aggregations.bucket.terms.ParsedTerms; import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; import org.elasticsearch.search.aggregations.metrics.AvgAggregationBuilder; +import org.elasticsearch.xcontent.XContentType; import java.io.IOException; import java.util.Collections; @@ -64,7 +64,7 @@ public void testInferenceAgg() throws IOException { Map bucketPaths = new HashMap<>(); bucketPaths.put("cost", "avg_cost"); - InferencePipelineAggregationBuilder inferenceAgg = new InferencePipelineAggregationBuilder("infer", modelId, bucketPaths); + InferencePipelineAggregationBuilder inferenceAgg = new InferencePipelineAggregationBuilder("infer", modelId, bucketPaths); termsAgg.subAggregation(inferenceAgg); SearchRequest search = new SearchRequest(index); @@ -104,8 +104,7 @@ private void putTrainedModel(String modelId, List inputFields, Tree tree private void indexData(String index) throws IOException { CreateIndexRequest create = new CreateIndexRequest(index); - create.mapping("{\"properties\": {\"fruit\": {\"type\": \"keyword\"}," + - "\"cost\": {\"type\": \"double\"}}}", XContentType.JSON); + create.mapping("{\"properties\": {\"fruit\": {\"type\": \"keyword\"}," + "\"cost\": {\"type\": \"double\"}}}", XContentType.JSON); highLevelClient().indices().create(create, RequestOptions.DEFAULT); BulkRequest bulk = new BulkRequest(index).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); bulk.add(new IndexRequest().source(XContentType.JSON, "fruit", "apple", "cost", "1.2")); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/asyncsearch/AsyncSearchIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/asyncsearch/AsyncSearchIT.java index db4dd91f91eb9..c894876acc02f 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/asyncsearch/AsyncSearchIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/asyncsearch/AsyncSearchIT.java @@ -33,20 +33,14 @@ public class AsyncSearchIT extends ESRestHighLevelClientTestCase { public void testAsyncSearch() throws IOException { String index = "test-index"; createIndex(index, Settings.EMPTY); - BulkRequest bulkRequest = new BulkRequest() - .add(new IndexRequest(index).id("1").source(Collections.singletonMap("foo", "bar"), XContentType.JSON)) + BulkRequest bulkRequest = new BulkRequest().add( + new IndexRequest(index).id("1").source(Collections.singletonMap("foo", "bar"), XContentType.JSON) + ) .add(new IndexRequest(index).id("2").source(Collections.singletonMap("foo", "bar2"), XContentType.JSON)) .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - assertEquals( - RestStatus.OK, - highLevelClient().bulk( - bulkRequest, - RequestOptions.DEFAULT - ).status() - ); + assertEquals(RestStatus.OK, highLevelClient().bulk(bulkRequest, RequestOptions.DEFAULT).status()); - SearchSourceBuilder sourceBuilder = new SearchSourceBuilder() - .query(QueryBuilders.matchAllQuery()) + SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()) .aggregation(AggregationBuilders.terms("1").field("foo.keyword")); SubmitAsyncSearchRequest submitRequest = new SubmitAsyncSearchRequest(sourceBuilder, index); submitRequest.setKeepOnCompletion(true); @@ -81,8 +75,7 @@ public void testAsyncSearch() throws IOException { assertThat(terms.getBuckets().get(1).getDocCount(), equalTo(1L)); DeleteAsyncSearchRequest deleteRequest = new DeleteAsyncSearchRequest(submitResponse.getId()); - AcknowledgedResponse deleteAsyncSearchResponse = highLevelClient().asyncSearch().delete(deleteRequest, - RequestOptions.DEFAULT); + AcknowledgedResponse deleteAsyncSearchResponse = highLevelClient().asyncSearch().delete(deleteRequest, RequestOptions.DEFAULT); assertNotNull(deleteAsyncSearchResponse); assertNotNull(deleteAsyncSearchResponse.isAcknowledged()); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/asyncsearch/AsyncSearchResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/asyncsearch/AsyncSearchResponseTests.java index f888dc2120fdf..f9e841d7a79c4 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/asyncsearch/AsyncSearchResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/asyncsearch/AsyncSearchResponseTests.java @@ -12,16 +12,17 @@ import org.elasticsearch.action.search.SearchResponse.Clusters; import org.elasticsearch.action.search.ShardSearchFailure; import org.elasticsearch.client.AbstractResponseTestCase; +import org.elasticsearch.search.internal.InternalSearchResponse; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.search.internal.InternalSearchResponse; import java.io.IOException; import static org.hamcrest.Matchers.containsString; -public class AsyncSearchResponseTests - extends AbstractResponseTestCase { +public class AsyncSearchResponseTests extends AbstractResponseTestCase< + org.elasticsearch.xpack.core.search.action.AsyncSearchResponse, + AsyncSearchResponse> { @Override protected org.elasticsearch.xpack.core.search.action.AsyncSearchResponse createServerTestInstance(XContentType xContentType) { @@ -32,12 +33,28 @@ protected org.elasticsearch.xpack.core.search.action.AsyncSearchResponse createS String id = randomBoolean() ? null : randomAlphaOfLength(10); ElasticsearchException error = randomBoolean() ? null : new ElasticsearchException(randomAlphaOfLength(10)); // add search response, minimal object is okay since the full randomization of parsing is tested in SearchResponseTests - SearchResponse searchResponse = randomBoolean() ? null - : new SearchResponse(InternalSearchResponse.empty(), randomAlphaOfLength(10), 1, 1, 0, randomIntBetween(0, 10000), - ShardSearchFailure.EMPTY_ARRAY, Clusters.EMPTY); + SearchResponse searchResponse = randomBoolean() + ? null + : new SearchResponse( + InternalSearchResponse.empty(), + randomAlphaOfLength(10), + 1, + 1, + 0, + randomIntBetween(0, 10000), + ShardSearchFailure.EMPTY_ARRAY, + Clusters.EMPTY + ); org.elasticsearch.xpack.core.search.action.AsyncSearchResponse testResponse = - new org.elasticsearch.xpack.core.search.action.AsyncSearchResponse(id, searchResponse, error, isPartial, isRunning, - startTimeMillis, expirationTimeMillis); + new org.elasticsearch.xpack.core.search.action.AsyncSearchResponse( + id, + searchResponse, + error, + isPartial, + isRunning, + startTimeMillis, + expirationTimeMillis + ); return testResponse; } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/CcrStatsResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/CcrStatsResponseTests.java index 25e38af215e2b..da57d7e47b516 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/CcrStatsResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/CcrStatsResponseTests.java @@ -50,8 +50,10 @@ static NavigableMap> randomReadExcep final int count = randomIntBetween(0, 16); final NavigableMap> readExceptions = new TreeMap<>(); for (int i = 0; i < count; i++) { - readExceptions.put("" + i, Tuple.tuple(randomNonNegativeLong(), - new ElasticsearchException(new IllegalStateException("index [" + i + "]")))); + readExceptions.put( + "" + i, + Tuple.tuple(randomNonNegativeLong(), new ElasticsearchException(new IllegalStateException("index [" + i + "]"))) + ); } return readExceptions; } @@ -60,8 +62,10 @@ static NavigableMap readExceptions = new TreeMap<>(); for (int i = 0; i < count; i++) { - readExceptions.put("" + i, - new org.elasticsearch.xpack.core.ccr.AutoFollowStats.AutoFollowedCluster(randomLong(), randomNonNegativeLong())); + readExceptions.put( + "" + i, + new org.elasticsearch.xpack.core.ccr.AutoFollowStats.AutoFollowedCluster(randomLong(), randomNonNegativeLong()) + ); } return readExceptions; } @@ -99,7 +103,8 @@ static FollowStatsAction.StatsResponses createStatsResponse() { randomNonNegativeLong(), Collections.emptyNavigableMap(), randomNonNegativeLong(), - randomBoolean() ? new ElasticsearchException("fatal error") : null); + randomBoolean() ? new ElasticsearchException("fatal error") : null + ); responses.add(new FollowStatsAction.StatsResponse(status)); } return new FollowStatsAction.StatsResponses(Collections.emptyList(), Collections.emptyList(), responses); @@ -115,27 +120,38 @@ protected void assertInstances(CcrStatsAction.Response serverTestInstance, CcrSt { AutoFollowStats newAutoFollowStats = clientInstance.getAutoFollowStats(); org.elasticsearch.xpack.core.ccr.AutoFollowStats expectedAutoFollowStats = serverTestInstance.getAutoFollowStats(); - assertThat(newAutoFollowStats.getNumberOfSuccessfulFollowIndices(), - equalTo(expectedAutoFollowStats.getNumberOfSuccessfulFollowIndices())); - assertThat(newAutoFollowStats.getNumberOfFailedRemoteClusterStateRequests(), - equalTo(expectedAutoFollowStats.getNumberOfFailedRemoteClusterStateRequests())); - assertThat(newAutoFollowStats.getNumberOfFailedFollowIndices(), - equalTo(expectedAutoFollowStats.getNumberOfFailedFollowIndices())); - assertThat(newAutoFollowStats.getRecentAutoFollowErrors().size(), - equalTo(expectedAutoFollowStats.getRecentAutoFollowErrors().size())); - assertThat(newAutoFollowStats.getRecentAutoFollowErrors().keySet(), - equalTo(expectedAutoFollowStats.getRecentAutoFollowErrors().keySet())); - for (final Map.Entry> entry : - newAutoFollowStats.getRecentAutoFollowErrors().entrySet()) { + assertThat( + newAutoFollowStats.getNumberOfSuccessfulFollowIndices(), + equalTo(expectedAutoFollowStats.getNumberOfSuccessfulFollowIndices()) + ); + assertThat( + newAutoFollowStats.getNumberOfFailedRemoteClusterStateRequests(), + equalTo(expectedAutoFollowStats.getNumberOfFailedRemoteClusterStateRequests()) + ); + assertThat( + newAutoFollowStats.getNumberOfFailedFollowIndices(), + equalTo(expectedAutoFollowStats.getNumberOfFailedFollowIndices()) + ); + assertThat( + newAutoFollowStats.getRecentAutoFollowErrors().size(), + equalTo(expectedAutoFollowStats.getRecentAutoFollowErrors().size()) + ); + assertThat( + newAutoFollowStats.getRecentAutoFollowErrors().keySet(), + equalTo(expectedAutoFollowStats.getRecentAutoFollowErrors().keySet()) + ); + for (final Map.Entry> entry : newAutoFollowStats.getRecentAutoFollowErrors() + .entrySet()) { // x-content loses the exception - final Tuple expected = - expectedAutoFollowStats.getRecentAutoFollowErrors().get(entry.getKey()); + final Tuple expected = expectedAutoFollowStats.getRecentAutoFollowErrors() + .get(entry.getKey()); assertThat(entry.getValue().v2().getMessage(), containsString(expected.v2().getMessage())); assertThat(entry.getValue().v1(), equalTo(expected.v1())); assertNotNull(entry.getValue().v2().getCause()); assertThat( entry.getValue().v2().getCause(), - anyOf(instanceOf(ElasticsearchException.class), instanceOf(IllegalStateException.class))); + anyOf(instanceOf(ElasticsearchException.class), instanceOf(IllegalStateException.class)) + ); assertThat(entry.getValue().v2().getCause().getMessage(), containsString(expected.v2().getCause().getMessage())); } } @@ -145,14 +161,11 @@ protected void assertInstances(CcrStatsAction.Response serverTestInstance, CcrSt // sort by index name, then shard ID final Map> expectedIndicesFollowStats = new TreeMap<>(); for (final FollowStatsAction.StatsResponse statsResponse : serverTestInstance.getFollowStats().getStatsResponses()) { - expectedIndicesFollowStats.computeIfAbsent( - statsResponse.status().followerIndex(), - k -> new TreeMap<>()).put(statsResponse.status().getShardId(), statsResponse); + expectedIndicesFollowStats.computeIfAbsent(statsResponse.status().followerIndex(), k -> new TreeMap<>()) + .put(statsResponse.status().getShardId(), statsResponse); } - assertThat(newIndicesFollowStats.getShardFollowStats().size(), - equalTo(expectedIndicesFollowStats.size())); - assertThat(newIndicesFollowStats.getShardFollowStats().keySet(), - equalTo(expectedIndicesFollowStats.keySet())); + assertThat(newIndicesFollowStats.getShardFollowStats().size(), equalTo(expectedIndicesFollowStats.size())); + assertThat(newIndicesFollowStats.getShardFollowStats().keySet(), equalTo(expectedIndicesFollowStats.keySet())); for (Map.Entry> indexEntry : newIndicesFollowStats.getShardFollowStats().entrySet()) { List newStats = indexEntry.getValue(); Map expectedStats = expectedIndicesFollowStats.get(indexEntry.getKey()); @@ -165,46 +178,67 @@ protected void assertInstances(CcrStatsAction.Response serverTestInstance, CcrSt assertThat(actualShardFollowStats.getLeaderIndex(), equalTo(expectedShardFollowStats.leaderIndex())); assertThat(actualShardFollowStats.getFollowerIndex(), equalTo(expectedShardFollowStats.followerIndex())); assertThat(actualShardFollowStats.getShardId(), equalTo(expectedShardFollowStats.getShardId())); - assertThat(actualShardFollowStats.getLeaderGlobalCheckpoint(), - equalTo(expectedShardFollowStats.leaderGlobalCheckpoint())); + assertThat( + actualShardFollowStats.getLeaderGlobalCheckpoint(), + equalTo(expectedShardFollowStats.leaderGlobalCheckpoint()) + ); assertThat(actualShardFollowStats.getLeaderMaxSeqNo(), equalTo(expectedShardFollowStats.leaderMaxSeqNo())); - assertThat(actualShardFollowStats.getFollowerGlobalCheckpoint(), - equalTo(expectedShardFollowStats.followerGlobalCheckpoint())); + assertThat( + actualShardFollowStats.getFollowerGlobalCheckpoint(), + equalTo(expectedShardFollowStats.followerGlobalCheckpoint()) + ); assertThat(actualShardFollowStats.getLastRequestedSeqNo(), equalTo(expectedShardFollowStats.lastRequestedSeqNo())); - assertThat(actualShardFollowStats.getOutstandingReadRequests(), - equalTo(expectedShardFollowStats.outstandingReadRequests())); - assertThat(actualShardFollowStats.getOutstandingWriteRequests(), - equalTo(expectedShardFollowStats.outstandingWriteRequests())); - assertThat(actualShardFollowStats.getWriteBufferOperationCount(), - equalTo(expectedShardFollowStats.writeBufferOperationCount())); - assertThat(actualShardFollowStats.getFollowerMappingVersion(), - equalTo(expectedShardFollowStats.followerMappingVersion())); - assertThat(actualShardFollowStats.getFollowerSettingsVersion(), - equalTo(expectedShardFollowStats.followerSettingsVersion())); - assertThat(actualShardFollowStats.getFollowerAliasesVersion(), - equalTo(expectedShardFollowStats.followerAliasesVersion())); - assertThat(actualShardFollowStats.getTotalReadTimeMillis(), - equalTo(expectedShardFollowStats.totalReadTimeMillis())); - assertThat(actualShardFollowStats.getSuccessfulReadRequests(), - equalTo(expectedShardFollowStats.successfulReadRequests())); + assertThat( + actualShardFollowStats.getOutstandingReadRequests(), + equalTo(expectedShardFollowStats.outstandingReadRequests()) + ); + assertThat( + actualShardFollowStats.getOutstandingWriteRequests(), + equalTo(expectedShardFollowStats.outstandingWriteRequests()) + ); + assertThat( + actualShardFollowStats.getWriteBufferOperationCount(), + equalTo(expectedShardFollowStats.writeBufferOperationCount()) + ); + assertThat( + actualShardFollowStats.getFollowerMappingVersion(), + equalTo(expectedShardFollowStats.followerMappingVersion()) + ); + assertThat( + actualShardFollowStats.getFollowerSettingsVersion(), + equalTo(expectedShardFollowStats.followerSettingsVersion()) + ); + assertThat( + actualShardFollowStats.getFollowerAliasesVersion(), + equalTo(expectedShardFollowStats.followerAliasesVersion()) + ); + assertThat(actualShardFollowStats.getTotalReadTimeMillis(), equalTo(expectedShardFollowStats.totalReadTimeMillis())); + assertThat( + actualShardFollowStats.getSuccessfulReadRequests(), + equalTo(expectedShardFollowStats.successfulReadRequests()) + ); assertThat(actualShardFollowStats.getFailedReadRequests(), equalTo(expectedShardFollowStats.failedReadRequests())); assertThat(actualShardFollowStats.getOperationsReads(), equalTo(expectedShardFollowStats.operationsReads())); assertThat(actualShardFollowStats.getBytesRead(), equalTo(expectedShardFollowStats.bytesRead())); - assertThat(actualShardFollowStats.getTotalWriteTimeMillis(), - equalTo(expectedShardFollowStats.totalWriteTimeMillis())); - assertThat(actualShardFollowStats.getSuccessfulWriteRequests(), - equalTo(expectedShardFollowStats.successfulWriteRequests())); - assertThat(actualShardFollowStats.getFailedWriteRequests(), - equalTo(expectedShardFollowStats.failedWriteRequests())); + assertThat(actualShardFollowStats.getTotalWriteTimeMillis(), equalTo(expectedShardFollowStats.totalWriteTimeMillis())); + assertThat( + actualShardFollowStats.getSuccessfulWriteRequests(), + equalTo(expectedShardFollowStats.successfulWriteRequests()) + ); + assertThat(actualShardFollowStats.getFailedWriteRequests(), equalTo(expectedShardFollowStats.failedWriteRequests())); assertThat(actualShardFollowStats.getOperationWritten(), equalTo(expectedShardFollowStats.operationWritten())); - assertThat(actualShardFollowStats.getReadExceptions().size(), - equalTo(expectedShardFollowStats.readExceptions().size())); - assertThat(actualShardFollowStats.getReadExceptions().keySet(), - equalTo(expectedShardFollowStats.readExceptions().keySet())); - for (final Map.Entry> entry : - actualShardFollowStats.getReadExceptions().entrySet()) { - final Tuple expectedTuple = - expectedShardFollowStats.readExceptions().get(entry.getKey()); + assertThat( + actualShardFollowStats.getReadExceptions().size(), + equalTo(expectedShardFollowStats.readExceptions().size()) + ); + assertThat( + actualShardFollowStats.getReadExceptions().keySet(), + equalTo(expectedShardFollowStats.readExceptions().keySet()) + ); + for (final Map.Entry> entry : actualShardFollowStats.getReadExceptions() + .entrySet()) { + final Tuple expectedTuple = expectedShardFollowStats.readExceptions() + .get(entry.getKey()); assertThat(entry.getValue().v1(), equalTo(expectedTuple.v1())); // x-content loses the exception final ElasticsearchException expected = expectedTuple.v2(); @@ -212,11 +246,14 @@ protected void assertInstances(CcrStatsAction.Response serverTestInstance, CcrSt assertNotNull(entry.getValue().v2().getCause()); assertThat( entry.getValue().v2().getCause(), - anyOf(instanceOf(ElasticsearchException.class), instanceOf(IllegalStateException.class))); + anyOf(instanceOf(ElasticsearchException.class), instanceOf(IllegalStateException.class)) + ); assertThat(entry.getValue().v2().getCause().getMessage(), containsString(expected.getCause().getMessage())); } - assertThat(actualShardFollowStats.getTimeSinceLastReadMillis(), - equalTo(expectedShardFollowStats.timeSinceLastReadMillis())); + assertThat( + actualShardFollowStats.getTimeSinceLastReadMillis(), + equalTo(expectedShardFollowStats.timeSinceLastReadMillis()) + ); } } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/FollowConfigTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/FollowConfigTests.java index 1d3c1b7b7f050..9dbf8b8d881ba 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/FollowConfigTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/FollowConfigTests.java @@ -10,8 +10,8 @@ import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.ToXContent; import java.io.IOException; @@ -20,16 +20,11 @@ public class FollowConfigTests extends ESTestCase { public void testFromXContent() throws IOException { - xContentTester(this::createParser, - FollowConfigTests::createTestInstance, - (followConfig, xContentBuilder) -> { - xContentBuilder.startObject(); - followConfig.toXContentFragment(xContentBuilder, ToXContent.EMPTY_PARAMS); - xContentBuilder.endObject(); - }, - FollowConfig::fromXContent) - .supportsUnknownFields(true) - .test(); + xContentTester(this::createParser, FollowConfigTests::createTestInstance, (followConfig, xContentBuilder) -> { + xContentBuilder.startObject(); + followConfig.toXContentFragment(xContentBuilder, ToXContent.EMPTY_PARAMS); + xContentBuilder.endObject(); + }, FollowConfig::fromXContent).supportsUnknownFields(true).test(); } static FollowConfig createTestInstance() { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/FollowInfoResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/FollowInfoResponseTests.java index ddfce6f87ed70..65c24f47e4e30 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/FollowInfoResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/FollowInfoResponseTests.java @@ -36,8 +36,15 @@ protected FollowInfoAction.Response createServerTestInstance(XContentType xConte followParameters = randomFollowParameters(); } - infos.add(new FollowInfoAction.Response.FollowerInfo(randomAlphaOfLength(4), randomAlphaOfLength(4), randomAlphaOfLength(4), - randomFrom(FollowInfoAction.Response.Status.values()), followParameters)); + infos.add( + new FollowInfoAction.Response.FollowerInfo( + randomAlphaOfLength(4), + randomAlphaOfLength(4), + randomAlphaOfLength(4), + randomFrom(FollowInfoAction.Response.Status.values()), + followParameters + ) + ); } return new FollowInfoAction.Response(infos); } @@ -72,8 +79,10 @@ protected void assertInstances(FollowInfoAction.Response serverTestInstance, Fol assertThat(serverFollowInfo.getRemoteCluster(), equalTo(clientFollowerInfo.getRemoteCluster())); assertThat(serverFollowInfo.getLeaderIndex(), equalTo(clientFollowerInfo.getLeaderIndex())); assertThat(serverFollowInfo.getFollowerIndex(), equalTo(clientFollowerInfo.getFollowerIndex())); - assertThat(serverFollowInfo.getStatus().toString().toLowerCase(Locale.ROOT), - equalTo(clientFollowerInfo.getStatus().getName().toLowerCase(Locale.ROOT))); + assertThat( + serverFollowInfo.getStatus().toString().toLowerCase(Locale.ROOT), + equalTo(clientFollowerInfo.getStatus().getName().toLowerCase(Locale.ROOT)) + ); FollowParameters serverParams = serverFollowInfo.getParameters(); FollowConfig clientParams = clientFollowerInfo.getParameters(); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/FollowStatsResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/FollowStatsResponseTests.java index fb12f9a529c33..9e297a490b33e 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/FollowStatsResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/FollowStatsResponseTests.java @@ -47,14 +47,11 @@ protected void assertInstances(FollowStatsAction.StatsResponses serverTestInstan // sort by index name, then shard ID final Map> expectedIndicesFollowStats = new TreeMap<>(); for (final FollowStatsAction.StatsResponse statsResponse : serverTestInstance.getStatsResponses()) { - expectedIndicesFollowStats.computeIfAbsent( - statsResponse.status().followerIndex(), - k -> new TreeMap<>()).put(statsResponse.status().getShardId(), statsResponse); + expectedIndicesFollowStats.computeIfAbsent(statsResponse.status().followerIndex(), k -> new TreeMap<>()) + .put(statsResponse.status().getShardId(), statsResponse); } - assertThat(newIndicesFollowStats.getShardFollowStats().size(), - equalTo(expectedIndicesFollowStats.size())); - assertThat(newIndicesFollowStats.getShardFollowStats().keySet(), - equalTo(expectedIndicesFollowStats.keySet())); + assertThat(newIndicesFollowStats.getShardFollowStats().size(), equalTo(expectedIndicesFollowStats.size())); + assertThat(newIndicesFollowStats.getShardFollowStats().keySet(), equalTo(expectedIndicesFollowStats.keySet())); for (Map.Entry> indexEntry : newIndicesFollowStats.getShardFollowStats().entrySet()) { List newStats = indexEntry.getValue(); Map expectedStats = expectedIndicesFollowStats.get(indexEntry.getKey()); @@ -67,46 +64,52 @@ protected void assertInstances(FollowStatsAction.StatsResponses serverTestInstan assertThat(actualShardFollowStats.getLeaderIndex(), equalTo(expectedShardFollowStats.leaderIndex())); assertThat(actualShardFollowStats.getFollowerIndex(), equalTo(expectedShardFollowStats.followerIndex())); assertThat(actualShardFollowStats.getShardId(), equalTo(expectedShardFollowStats.getShardId())); - assertThat(actualShardFollowStats.getLeaderGlobalCheckpoint(), - equalTo(expectedShardFollowStats.leaderGlobalCheckpoint())); + assertThat(actualShardFollowStats.getLeaderGlobalCheckpoint(), equalTo(expectedShardFollowStats.leaderGlobalCheckpoint())); assertThat(actualShardFollowStats.getLeaderMaxSeqNo(), equalTo(expectedShardFollowStats.leaderMaxSeqNo())); - assertThat(actualShardFollowStats.getFollowerGlobalCheckpoint(), - equalTo(expectedShardFollowStats.followerGlobalCheckpoint())); + assertThat( + actualShardFollowStats.getFollowerGlobalCheckpoint(), + equalTo(expectedShardFollowStats.followerGlobalCheckpoint()) + ); assertThat(actualShardFollowStats.getLastRequestedSeqNo(), equalTo(expectedShardFollowStats.lastRequestedSeqNo())); - assertThat(actualShardFollowStats.getOutstandingReadRequests(), - equalTo(expectedShardFollowStats.outstandingReadRequests())); - assertThat(actualShardFollowStats.getOutstandingWriteRequests(), - equalTo(expectedShardFollowStats.outstandingWriteRequests())); - assertThat(actualShardFollowStats.getWriteBufferOperationCount(), - equalTo(expectedShardFollowStats.writeBufferOperationCount())); - assertThat(actualShardFollowStats.getFollowerMappingVersion(), - equalTo(expectedShardFollowStats.followerMappingVersion())); - assertThat(actualShardFollowStats.getFollowerSettingsVersion(), - equalTo(expectedShardFollowStats.followerSettingsVersion())); - assertThat(actualShardFollowStats.getFollowerAliasesVersion(), - equalTo(expectedShardFollowStats.followerAliasesVersion())); - assertThat(actualShardFollowStats.getTotalReadTimeMillis(), - equalTo(expectedShardFollowStats.totalReadTimeMillis())); - assertThat(actualShardFollowStats.getSuccessfulReadRequests(), - equalTo(expectedShardFollowStats.successfulReadRequests())); + assertThat( + actualShardFollowStats.getOutstandingReadRequests(), + equalTo(expectedShardFollowStats.outstandingReadRequests()) + ); + assertThat( + actualShardFollowStats.getOutstandingWriteRequests(), + equalTo(expectedShardFollowStats.outstandingWriteRequests()) + ); + assertThat( + actualShardFollowStats.getWriteBufferOperationCount(), + equalTo(expectedShardFollowStats.writeBufferOperationCount()) + ); + assertThat(actualShardFollowStats.getFollowerMappingVersion(), equalTo(expectedShardFollowStats.followerMappingVersion())); + assertThat( + actualShardFollowStats.getFollowerSettingsVersion(), + equalTo(expectedShardFollowStats.followerSettingsVersion()) + ); + assertThat(actualShardFollowStats.getFollowerAliasesVersion(), equalTo(expectedShardFollowStats.followerAliasesVersion())); + assertThat(actualShardFollowStats.getTotalReadTimeMillis(), equalTo(expectedShardFollowStats.totalReadTimeMillis())); + assertThat(actualShardFollowStats.getSuccessfulReadRequests(), equalTo(expectedShardFollowStats.successfulReadRequests())); assertThat(actualShardFollowStats.getFailedReadRequests(), equalTo(expectedShardFollowStats.failedReadRequests())); assertThat(actualShardFollowStats.getOperationsReads(), equalTo(expectedShardFollowStats.operationsReads())); assertThat(actualShardFollowStats.getBytesRead(), equalTo(expectedShardFollowStats.bytesRead())); - assertThat(actualShardFollowStats.getTotalWriteTimeMillis(), - equalTo(expectedShardFollowStats.totalWriteTimeMillis())); - assertThat(actualShardFollowStats.getSuccessfulWriteRequests(), - equalTo(expectedShardFollowStats.successfulWriteRequests())); - assertThat(actualShardFollowStats.getFailedWriteRequests(), - equalTo(expectedShardFollowStats.failedWriteRequests())); + assertThat(actualShardFollowStats.getTotalWriteTimeMillis(), equalTo(expectedShardFollowStats.totalWriteTimeMillis())); + assertThat( + actualShardFollowStats.getSuccessfulWriteRequests(), + equalTo(expectedShardFollowStats.successfulWriteRequests()) + ); + assertThat(actualShardFollowStats.getFailedWriteRequests(), equalTo(expectedShardFollowStats.failedWriteRequests())); assertThat(actualShardFollowStats.getOperationWritten(), equalTo(expectedShardFollowStats.operationWritten())); - assertThat(actualShardFollowStats.getReadExceptions().size(), - equalTo(expectedShardFollowStats.readExceptions().size())); - assertThat(actualShardFollowStats.getReadExceptions().keySet(), - equalTo(expectedShardFollowStats.readExceptions().keySet())); - for (final Map.Entry> entry : - actualShardFollowStats.getReadExceptions().entrySet()) { - final Tuple expectedTuple = - expectedShardFollowStats.readExceptions().get(entry.getKey()); + assertThat(actualShardFollowStats.getReadExceptions().size(), equalTo(expectedShardFollowStats.readExceptions().size())); + assertThat( + actualShardFollowStats.getReadExceptions().keySet(), + equalTo(expectedShardFollowStats.readExceptions().keySet()) + ); + for (final Map.Entry> entry : actualShardFollowStats.getReadExceptions() + .entrySet()) { + final Tuple expectedTuple = expectedShardFollowStats.readExceptions() + .get(entry.getKey()); assertThat(entry.getValue().v1(), equalTo(expectedTuple.v1())); // x-content loses the exception final ElasticsearchException expected = expectedTuple.v2(); @@ -114,11 +117,14 @@ protected void assertInstances(FollowStatsAction.StatsResponses serverTestInstan assertNotNull(entry.getValue().v2().getCause()); assertThat( entry.getValue().v2().getCause(), - anyOf(instanceOf(ElasticsearchException.class), instanceOf(IllegalStateException.class))); + anyOf(instanceOf(ElasticsearchException.class), instanceOf(IllegalStateException.class)) + ); assertThat(entry.getValue().v2().getCause().getMessage(), containsString(expected.getCause().getMessage())); } - assertThat(actualShardFollowStats.getTimeSinceLastReadMillis(), - equalTo(expectedShardFollowStats.timeSinceLastReadMillis())); + assertThat( + actualShardFollowStats.getTimeSinceLastReadMillis(), + equalTo(expectedShardFollowStats.timeSinceLastReadMillis()) + ); } } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/GetAutoFollowPatternResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/GetAutoFollowPatternResponseTests.java index 1c3ddfe60e50e..b938dd50e9a45 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/GetAutoFollowPatternResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/GetAutoFollowPatternResponseTests.java @@ -41,8 +41,9 @@ protected GetAutoFollowPatternAction.Response createServerTestInstance(XContentT List leaderIndexPatterns = Collections.singletonList(randomAlphaOfLength(4)); List leaderIndexExclusionsPatterns = randomList(0, randomIntBetween(1, 10), () -> randomAlphaOfLength(4)); String followIndexNamePattern = randomAlphaOfLength(4); - final Settings settings = - Settings.builder().put(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), randomIntBetween(0, 4)).build(); + final Settings settings = Settings.builder() + .put(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), randomIntBetween(0, 4)) + .build(); boolean active = randomBoolean(); Integer maxOutstandingReadRequests = null; @@ -77,7 +78,7 @@ protected GetAutoFollowPatternAction.Response createServerTestInstance(XContentT if (randomBoolean()) { maxWriteRequestSize = new ByteSizeValue(randomNonNegativeLong()); } - TimeValue maxRetryDelay = null; + TimeValue maxRetryDelay = null; if (randomBoolean()) { maxRetryDelay = new TimeValue(randomNonNegativeLong()); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/PutAutoFollowPatternRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/PutAutoFollowPatternRequestTests.java index da2510a5145c4..a17fb8a1b630e 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/PutAutoFollowPatternRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/PutAutoFollowPatternRequestTests.java @@ -27,7 +27,8 @@ public class PutAutoFollowPatternRequestTests extends AbstractRequestTestCase< @Override protected PutAutoFollowPatternRequest createClientTestInstance() { // Name isn't serialized, because it specified in url path, so no need to randomly generate it here. - PutAutoFollowPatternRequest putAutoFollowPatternRequest = new PutAutoFollowPatternRequest("name", + PutAutoFollowPatternRequest putAutoFollowPatternRequest = new PutAutoFollowPatternRequest( + "name", randomAlphaOfLength(4), Arrays.asList(generateRandomStringArray(4, 4, false)), Arrays.asList(generateRandomStringArray(4, 4, false)) diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/PutFollowRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/PutFollowRequestTests.java index 4f0e06033caa4..61635024fc24b 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/PutFollowRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/PutFollowRequestTests.java @@ -24,8 +24,7 @@ public class PutFollowRequestTests extends AbstractRequestTestCase { +public class RemoteInfoResponseTests extends AbstractResponseTestCase< + org.elasticsearch.action.admin.cluster.remote.RemoteInfoResponse, + RemoteInfoResponse> { @Override protected org.elasticsearch.action.admin.cluster.remote.RemoteInfoResponse createServerTestInstance(XContentType xContentType) { @@ -44,33 +45,36 @@ protected RemoteInfoResponse doParseToClientInstance(XContentParser parser) thro } @Override - protected void assertInstances(org.elasticsearch.action.admin.cluster.remote.RemoteInfoResponse serverTestInstance, - RemoteInfoResponse clientInstance) { + protected void assertInstances( + org.elasticsearch.action.admin.cluster.remote.RemoteInfoResponse serverTestInstance, + RemoteInfoResponse clientInstance + ) { assertThat(clientInstance.getInfos().size(), equalTo(serverTestInstance.getInfos().size())); - Map serverInfos = serverTestInstance.getInfos().stream() - .collect(toMap(RemoteConnectionInfo::getClusterAlias, identity())); + Map serverInfos = serverTestInstance.getInfos() + .stream() + .collect(toMap(RemoteConnectionInfo::getClusterAlias, identity())); for (org.elasticsearch.client.cluster.RemoteConnectionInfo clientRemoteInfo : clientInstance.getInfos()) { RemoteConnectionInfo serverRemoteInfo = serverInfos.get(clientRemoteInfo.getClusterAlias()); assertThat(clientRemoteInfo.getClusterAlias(), equalTo(serverRemoteInfo.getClusterAlias())); - assertThat(clientRemoteInfo.getInitialConnectionTimeoutString(), - equalTo(serverRemoteInfo.getInitialConnectionTimeout().toString())); + assertThat( + clientRemoteInfo.getInitialConnectionTimeoutString(), + equalTo(serverRemoteInfo.getInitialConnectionTimeout().toString()) + ); assertThat(clientRemoteInfo.isConnected(), equalTo(serverRemoteInfo.isConnected())); assertThat(clientRemoteInfo.isSkipUnavailable(), equalTo(serverRemoteInfo.isSkipUnavailable())); assertThat(clientRemoteInfo.getModeInfo().isConnected(), equalTo(serverRemoteInfo.getModeInfo().isConnected())); assertThat(clientRemoteInfo.getModeInfo().modeName(), equalTo(serverRemoteInfo.getModeInfo().modeName())); if (clientRemoteInfo.getModeInfo().modeName().equals(SniffModeInfo.NAME)) { - SniffModeInfo clientModeInfo = - (SniffModeInfo) clientRemoteInfo.getModeInfo(); - SniffConnectionStrategy.SniffModeInfo serverModeInfo = - (SniffConnectionStrategy.SniffModeInfo) serverRemoteInfo.getModeInfo(); + SniffModeInfo clientModeInfo = (SniffModeInfo) clientRemoteInfo.getModeInfo(); + SniffConnectionStrategy.SniffModeInfo serverModeInfo = (SniffConnectionStrategy.SniffModeInfo) serverRemoteInfo + .getModeInfo(); assertThat(clientModeInfo.getMaxConnectionsPerCluster(), equalTo(serverModeInfo.getMaxConnectionsPerCluster())); assertThat(clientModeInfo.getNumNodesConnected(), equalTo(serverModeInfo.getNumNodesConnected())); assertThat(clientModeInfo.getSeedNodes(), equalTo(serverModeInfo.getSeedNodes())); } else if (clientRemoteInfo.getModeInfo().modeName().equals(ProxyModeInfo.NAME)) { - ProxyModeInfo clientModeInfo = - (ProxyModeInfo) clientRemoteInfo.getModeInfo(); - ProxyConnectionStrategy.ProxyModeInfo serverModeInfo = - (ProxyConnectionStrategy.ProxyModeInfo) serverRemoteInfo.getModeInfo(); + ProxyModeInfo clientModeInfo = (ProxyModeInfo) clientRemoteInfo.getModeInfo(); + ProxyConnectionStrategy.ProxyModeInfo serverModeInfo = (ProxyConnectionStrategy.ProxyModeInfo) serverRemoteInfo + .getModeInfo(); assertThat(clientModeInfo.getAddress(), equalTo(serverModeInfo.getAddress())); assertThat(clientModeInfo.getServerName(), equalTo(serverModeInfo.getServerName())); assertThat(clientModeInfo.getMaxSocketConnections(), equalTo(serverModeInfo.getMaxSocketConnections())); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/common/ProtocolUtilsTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/common/ProtocolUtilsTests.java index ec32e0c6e7946..126226b24a4cc 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/common/ProtocolUtilsTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/common/ProtocolUtilsTests.java @@ -13,7 +13,7 @@ import java.util.HashMap; import java.util.Map; -public class ProtocolUtilsTests extends ESTestCase { +public class ProtocolUtilsTests extends ESTestCase { public void testMapStringEqualsAndHash() { assertTrue(ProtocolUtils.equals(null, null)); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/core/AcknowledgedResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/core/AcknowledgedResponseTests.java index a933294960587..eaddefeafe8ff 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/core/AcknowledgedResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/core/AcknowledgedResponseTests.java @@ -16,7 +16,8 @@ import static org.hamcrest.Matchers.is; -public class AcknowledgedResponseTests extends AbstractResponseTestCase { @Override @@ -30,8 +31,10 @@ protected AcknowledgedResponse doParseToClientInstance(XContentParser parser) th } @Override - protected void assertInstances(org.elasticsearch.action.support.master.AcknowledgedResponse serverTestInstance, - AcknowledgedResponse clientInstance) { + protected void assertInstances( + org.elasticsearch.action.support.master.AcknowledgedResponse serverTestInstance, + AcknowledgedResponse clientInstance + ) { assertThat(clientInstance.isAcknowledged(), is(serverTestInstance.isAcknowledged())); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/core/BroadcastResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/core/BroadcastResponseTests.java index c7c0174f3098f..251fe657861a2 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/core/BroadcastResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/core/BroadcastResponseTests.java @@ -10,9 +10,9 @@ import org.elasticsearch.action.support.DefaultShardOperationFailedException; import org.elasticsearch.client.AbstractResponseTestCase; +import org.elasticsearch.index.seqno.RetentionLeaseNotFoundException; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.index.seqno.RetentionLeaseNotFoundException; import java.io.IOException; import java.util.ArrayList; @@ -25,7 +25,8 @@ import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.in; -public class BroadcastResponseTests extends AbstractResponseTestCase { private String index; @@ -45,7 +46,8 @@ protected org.elasticsearch.action.support.broadcast.BroadcastResponse createSer final DefaultShardOperationFailedException failure = new DefaultShardOperationFailedException( index, randomValueOtherThanMany(shardIds::contains, () -> randomIntBetween(0, total - 1)), - new RetentionLeaseNotFoundException(id)); + new RetentionLeaseNotFoundException(id) + ); failures.add(failure); shardIds.add(failure.shardId()); } @@ -59,8 +61,10 @@ protected BroadcastResponse doParseToClientInstance(XContentParser parser) throw } @Override - protected void assertInstances(org.elasticsearch.action.support.broadcast.BroadcastResponse serverTestInstance, - BroadcastResponse clientInstance) { + protected void assertInstances( + org.elasticsearch.action.support.broadcast.BroadcastResponse serverTestInstance, + BroadcastResponse clientInstance + ) { assertThat(clientInstance.shards().total(), equalTo(serverTestInstance.getTotalShards())); assertThat(clientInstance.shards().successful(), equalTo(serverTestInstance.getSuccessfulShards())); assertThat(clientInstance.shards().skipped(), equalTo(0)); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/core/CountRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/core/CountRequestTests.java index eb21ea26254c5..b0fc5fadc23c4 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/core/CountRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/core/CountRequestTests.java @@ -12,14 +12,14 @@ import org.elasticsearch.client.AbstractRequestTestCase; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.ArrayUtils; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.index.query.MatchAllQueryBuilder; import org.elasticsearch.index.query.MatchQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.rest.action.RestActions; import org.elasticsearch.search.SearchModule; import org.elasticsearch.search.builder.SearchSourceBuilder; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.ArrayList; @@ -105,16 +105,25 @@ private CountRequest createCountRequest() { private CountRequest mutate(CountRequest countRequest) { CountRequest mutation = copyRequest(countRequest); List mutators = new ArrayList<>(); - mutators.add(() -> mutation.indices(ArrayUtils.concat(countRequest.indices(), new String[]{randomAlphaOfLength(10)}))); - mutators.add(() -> mutation.indicesOptions(randomValueOtherThan(countRequest.indicesOptions(), - () -> IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean())))); - mutators.add(() -> mutation.types(ArrayUtils.concat(countRequest.types(), new String[]{randomAlphaOfLength(10)}))); + mutators.add(() -> mutation.indices(ArrayUtils.concat(countRequest.indices(), new String[] { randomAlphaOfLength(10) }))); + mutators.add( + () -> mutation.indicesOptions( + randomValueOtherThan( + countRequest.indicesOptions(), + () -> IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean()) + ) + ) + ); + mutators.add(() -> mutation.types(ArrayUtils.concat(countRequest.types(), new String[] { randomAlphaOfLength(10) }))); mutators.add(() -> mutation.preference(randomValueOtherThan(countRequest.preference(), () -> randomAlphaOfLengthBetween(3, 10)))); mutators.add(() -> mutation.routing(randomValueOtherThan(countRequest.routing(), () -> randomAlphaOfLengthBetween(3, 10)))); mutators.add(() -> mutation.terminateAfter(randomValueOtherThan(countRequest.terminateAfter(), () -> randomIntBetween(0, 10)))); mutators.add(() -> mutation.minScore(randomValueOtherThan(countRequest.minScore(), () -> (float) randomIntBetween(0, 10)))); - mutators.add(() -> mutation.query(randomValueOtherThan(countRequest.query(), - () -> new MatchQueryBuilder(randomAlphaOfLength(4), randomAlphaOfLength(4))))); + mutators.add( + () -> mutation.query( + randomValueOtherThan(countRequest.query(), () -> new MatchQueryBuilder(randomAlphaOfLength(4), randomAlphaOfLength(4))) + ) + ); randomFrom(mutators).run(); return mutation; } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/core/CountResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/core/CountResponseTests.java index 46eede441529a..70ed1436f2037 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/core/CountResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/core/CountResponseTests.java @@ -11,13 +11,13 @@ import org.elasticsearch.action.search.ShardSearchFailure; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.common.ParsingException; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.index.Index; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.rest.action.RestActions; import org.elasticsearch.search.SearchShardTarget; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; @@ -29,15 +29,9 @@ public class CountResponseTests extends ESTestCase { // ElasticSearchException. Best effort: try to check that the original message appears somewhere in the rendered xContent // For more see ShardSearchFailureTests. public void testFromXContent() throws IOException { - xContentTester( - this::createParser, - this::createTestInstance, - this::toXContent, - CountResponse::fromXContent) - .supportsUnknownFields(false) - .assertEqualsConsumer(this::assertEqualInstances) - .assertToXContentEquivalence(false) - .test(); + xContentTester(this::createParser, this::createTestInstance, this::toXContent, CountResponse::fromXContent).supportsUnknownFields( + false + ).assertEqualsConsumer(this::assertEqualInstances).assertToXContentEquivalence(false).test(); } private CountResponse createTestInstance() { @@ -51,8 +45,12 @@ private CountResponse createTestInstance() { for (int i = 0; i < failures.length; i++) { failures[i] = createShardFailureTestItem(); } - CountResponse.ShardStats shardStats = new CountResponse.ShardStats(successfulShards, totalShards, skippedShards, - randomBoolean() ? ShardSearchFailure.EMPTY_ARRAY : failures); + CountResponse.ShardStats shardStats = new CountResponse.ShardStats( + successfulShards, + totalShards, + skippedShards, + randomBoolean() ? ShardSearchFailure.EMPTY_ARRAY : failures + ); return new CountResponse(count, terminatedEarly, shardStats); } @@ -67,8 +65,15 @@ private void toXContent(CountResponse response, XContentBuilder builder) throws } private void toXContent(CountResponse.ShardStats stats, XContentBuilder builder, ToXContent.Params params) throws IOException { - RestActions.buildBroadcastShardsHeader(builder, params, stats.getTotalShards(), stats.getSuccessfulShards(), stats - .getSkippedShards(), stats.getShardFailures().length, stats.getShardFailures()); + RestActions.buildBroadcastShardsHeader( + builder, + params, + stats.getTotalShards(), + stats.getSuccessfulShards(), + stats.getSkippedShards(), + stats.getShardFailures().length, + stats.getShardFailures() + ); } @SuppressWarnings("Duplicates") @@ -79,8 +84,11 @@ private static ShardSearchFailure createShardFailureTestItem() { if (randomBoolean()) { String nodeId = randomAlphaOfLengthBetween(5, 10); String indexName = randomAlphaOfLengthBetween(5, 10); - searchShardTarget = new SearchShardTarget(nodeId, - new ShardId(new Index(indexName, IndexMetadata.INDEX_UUID_NA_VALUE), randomInt()), null); + searchShardTarget = new SearchShardTarget( + nodeId, + new ShardId(new Index(indexName, IndexMetadata.INDEX_UUID_NA_VALUE), randomInt()), + null + ); } return new ShardSearchFailure(ex, searchShardTarget); } @@ -105,11 +113,15 @@ private void assertEqualInstances(CountResponse expectedInstance, CountResponse assertEquals(originalFailure.shard(), parsedFailure.shard()); assertEquals(originalFailure.shardId(), parsedFailure.shardId()); String originalMsg = originalFailure.getCause().getMessage(); - assertEquals(parsedFailure.getCause().getMessage(), "Elasticsearch exception [type=parsing_exception, reason=" + - originalMsg + "]"); + assertEquals( + parsedFailure.getCause().getMessage(), + "Elasticsearch exception [type=parsing_exception, reason=" + originalMsg + "]" + ); String nestedMsg = originalFailure.getCause().getCause().getMessage(); - assertEquals(parsedFailure.getCause().getCause().getMessage(), - "Elasticsearch exception [type=illegal_argument_exception, reason=" + nestedMsg + "]"); + assertEquals( + parsedFailure.getCause().getCause().getMessage(), + "Elasticsearch exception [type=illegal_argument_exception, reason=" + nestedMsg + "]" + ); } } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/core/GetSourceResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/core/GetSourceResponseTests.java index eb4452df98b33..98fba2a0ed1dc 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/core/GetSourceResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/core/GetSourceResponseTests.java @@ -11,9 +11,9 @@ import org.elasticsearch.client.AbstractResponseTestCase; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; @@ -23,8 +23,7 @@ import static org.hamcrest.CoreMatchers.equalTo; -public final class GetSourceResponseTests extends - AbstractResponseTestCase { +public final class GetSourceResponseTests extends AbstractResponseTestCase { static class SourceOnlyResponse implements ToXContentObject { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/core/MainResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/core/MainResponseTests.java index f9abea804322b..a535e1f261c04 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/core/MainResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/core/MainResponseTests.java @@ -12,9 +12,9 @@ import org.elasticsearch.Version; import org.elasticsearch.client.AbstractResponseTestCase; import org.elasticsearch.cluster.ClusterName; +import org.elasticsearch.test.VersionUtils; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.VersionUtils; import java.io.IOException; import java.util.Date; @@ -30,10 +30,14 @@ protected org.elasticsearch.action.main.MainResponse createServerTestInstance(XC final String date = new Date(randomNonNegativeLong()).toString(); Version version = VersionUtils.randomIndexCompatibleVersion(random()); Build build = new Build( - Build.Flavor.UNKNOWN, Build.Type.UNKNOWN, randomAlphaOfLength(8), date, randomBoolean(), + Build.Flavor.UNKNOWN, + Build.Type.UNKNOWN, + randomAlphaOfLength(8), + date, + randomBoolean(), version.toString() ); - return new org.elasticsearch.action.main.MainResponse(nodeName, version, clusterName, clusterUuid , build); + return new org.elasticsearch.action.main.MainResponse(nodeName, version, clusterName, clusterUuid, build); } @Override @@ -54,9 +58,13 @@ protected void assertInstances(org.elasticsearch.action.main.MainResponse server assertThat(serverTestInstance.getBuild().flavor().displayName(), equalTo(clientInstance.getVersion().getBuildFlavor())); assertThat(serverTestInstance.getBuild().type().displayName(), equalTo(clientInstance.getVersion().getBuildType())); assertThat(serverTestInstance.getVersion().luceneVersion.toString(), equalTo(clientInstance.getVersion().getLuceneVersion())); - assertThat(serverTestInstance.getVersion().minimumIndexCompatibilityVersion().toString(), - equalTo(clientInstance.getVersion().getMinimumIndexCompatibilityVersion())); - assertThat(serverTestInstance.getVersion().minimumCompatibilityVersion().toString(), - equalTo(clientInstance.getVersion().getMinimumWireCompatibilityVersion())); + assertThat( + serverTestInstance.getVersion().minimumIndexCompatibilityVersion().toString(), + equalTo(clientInstance.getVersion().getMinimumIndexCompatibilityVersion()) + ); + assertThat( + serverTestInstance.getVersion().minimumCompatibilityVersion().toString(), + equalTo(clientInstance.getVersion().getMinimumWireCompatibilityVersion()) + ); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/core/MultiTermVectorsResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/core/MultiTermVectorsResponseTests.java index e3a28c5aa3dd3..18ebbb8a652e8 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/core/MultiTermVectorsResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/core/MultiTermVectorsResponseTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.core; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.ArrayList; @@ -20,14 +20,9 @@ public class MultiTermVectorsResponseTests extends ESTestCase { public void testFromXContent() throws IOException { - xContentTester( - this::createParser, - this::createTestInstance, - this::toXContent, - MultiTermVectorsResponse::fromXContent) + xContentTester(this::createParser, this::createTestInstance, this::toXContent, MultiTermVectorsResponse::fromXContent) .supportsUnknownFields(true) - .randomFieldsExcludeFilter(field -> - field.endsWith("term_vectors") || field.endsWith("terms") || field.endsWith("tokens")) + .randomFieldsExcludeFilter(field -> field.endsWith("term_vectors") || field.endsWith("terms") || field.endsWith("tokens")) .test(); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/core/ShardsAcknowledgedResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/core/ShardsAcknowledgedResponseTests.java index b68794af99dbd..bcfa052aa2b74 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/core/ShardsAcknowledgedResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/core/ShardsAcknowledgedResponseTests.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.core; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; @@ -17,12 +17,12 @@ public class ShardsAcknowledgedResponseTests extends ESTestCase { public void testFromXContent() throws IOException { - xContentTester(this::createParser, + xContentTester( + this::createParser, this::createTestInstance, ShardsAcknowledgedResponseTests::toXContent, - ShardsAcknowledgedResponse::fromXContent) - .supportsUnknownFields(false) - .test(); + ShardsAcknowledgedResponse::fromXContent + ).supportsUnknownFields(false).test(); } private ShardsAcknowledgedResponse createTestInstance() { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/core/TermVectorsResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/core/TermVectorsResponseTests.java index 8cc842becd1d5..5efdaa35cbdff 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/core/TermVectorsResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/core/TermVectorsResponseTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.core; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.ArrayList; @@ -26,10 +26,9 @@ public void testFromXContent() throws IOException { this::createParser, TermVectorsResponseTests::createTestInstance, TermVectorsResponseTests::toXContent, - TermVectorsResponse::fromXContent) - .supportsUnknownFields(true) - .randomFieldsExcludeFilter(field -> - field.endsWith("term_vectors") || field.endsWith("terms") || field.endsWith("tokens")) + TermVectorsResponse::fromXContent + ).supportsUnknownFields(true) + .randomFieldsExcludeFilter(field -> field.endsWith("term_vectors") || field.endsWith("terms") || field.endsWith("tokens")) .test(); } @@ -89,7 +88,7 @@ private static void toXContent(TermVectorsResponse.TermVector tv, XContentBuilde for (TermVectorsResponse.TermVector.Token token : tokens) { builder.startObject(); if (token.getPosition() != null) builder.field("position", token.getPosition()); - if (token.getStartOffset()!= null) builder.field("start_offset", token.getStartOffset()); + if (token.getStartOffset() != null) builder.field("start_offset", token.getStartOffset()); if (token.getEndOffset() != null) builder.field("end_offset", token.getEndOffset()); if (token.getPayload() != null) builder.field("payload", token.getPayload()); builder.endObject(); @@ -104,15 +103,14 @@ private static void toXContent(TermVectorsResponse.TermVector tv, XContentBuilde builder.endObject(); } - static TermVectorsResponse createTestInstance() { String index = randomAlphaOfLength(5); - String id = String.valueOf(randomIntBetween(1,100)); + String id = String.valueOf(randomIntBetween(1, 100)); long version = randomNonNegativeLong(); long tookInMillis = randomNonNegativeLong(); boolean found = randomBoolean(); List tvList = null; - if (found){ + if (found) { boolean hasFieldStatistics = randomBoolean(); boolean hasTermStatistics = randomBoolean(); boolean hasScores = randomBoolean(); @@ -125,18 +123,24 @@ static TermVectorsResponse createTestInstance() { for (int i = 0; i < fieldsCount; i++) { String fieldName = randomValueOtherThanMany(usedFieldNames::contains, () -> randomAlphaOfLength(7)); usedFieldNames.add(fieldName); - tvList.add(randomTermVector( - fieldName, hasFieldStatistics, hasTermStatistics, hasScores, hasOffsets, hasPositions, hasPayloads)); + tvList.add( + randomTermVector(fieldName, hasFieldStatistics, hasTermStatistics, hasScores, hasOffsets, hasPositions, hasPayloads) + ); } } TermVectorsResponse tvresponse = new TermVectorsResponse(index, id, version, found, tookInMillis, tvList); return tvresponse; } - - - private static TermVectorsResponse.TermVector randomTermVector(String fieldName, boolean hasFieldStatistics, boolean hasTermStatistics, - boolean hasScores, boolean hasOffsets, boolean hasPositions, boolean hasPayloads) { + private static TermVectorsResponse.TermVector randomTermVector( + String fieldName, + boolean hasFieldStatistics, + boolean hasTermStatistics, + boolean hasScores, + boolean hasOffsets, + boolean hasPositions, + boolean hasPayloads + ) { TermVectorsResponse.TermVector.FieldStatistics fs = null; if (hasFieldStatistics) { long sumDocFreq = randomNonNegativeLong(); @@ -158,10 +162,16 @@ private static TermVectorsResponse.TermVector randomTermVector(String fieldName, return tv; } - private static TermVectorsResponse.TermVector.Term randomTerm(String termTxt, boolean hasTermStatistics, boolean hasScores, - boolean hasOffsets, boolean hasPositions, boolean hasPayloads) { + private static TermVectorsResponse.TermVector.Term randomTerm( + String termTxt, + boolean hasTermStatistics, + boolean hasScores, + boolean hasOffsets, + boolean hasPositions, + boolean hasPayloads + ) { - int termFreq = randomInt(10000); + int termFreq = randomInt(10000); Integer docFreq = null; Long totalTermFreq = null; Float score = null; @@ -171,7 +181,7 @@ private static TermVectorsResponse.TermVector.Term randomTerm(String termTxt, bo totalTermFreq = randomNonNegativeLong(); } if (hasScores) score = randomFloat(); - if (hasOffsets || hasPositions || hasPayloads ){ + if (hasOffsets || hasPositions || hasPayloads) { int tokensCount = randomIntBetween(1, 5); tokens = new ArrayList<>(tokensCount); for (int i = 0; i < tokensCount; i++) { @@ -185,13 +195,23 @@ private static TermVectorsResponse.TermVector.Term randomTerm(String termTxt, bo } if (hasPositions) position = randomInt(100); if (hasPayloads) payload = "payload" + randomAlphaOfLength(2); - TermVectorsResponse.TermVector.Token token = - new TermVectorsResponse.TermVector.Token(startOffset, endOffset, position, payload); + TermVectorsResponse.TermVector.Token token = new TermVectorsResponse.TermVector.Token( + startOffset, + endOffset, + position, + payload + ); tokens.add(token); } } - TermVectorsResponse.TermVector.Term term = - new TermVectorsResponse.TermVector.Term(termTxt, termFreq, docFreq, totalTermFreq, score, tokens); + TermVectorsResponse.TermVector.Term term = new TermVectorsResponse.TermVector.Term( + termTxt, + termFreq, + docFreq, + totalTermFreq, + score, + tokens + ); return term; } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/core/tasks/GetTaskResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/core/tasks/GetTaskResponseTests.java index 73b6dbecb8207..9b11affa1a0da 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/core/tasks/GetTaskResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/core/tasks/GetTaskResponseTests.java @@ -11,13 +11,13 @@ import org.elasticsearch.client.Requests; import org.elasticsearch.client.tasks.GetTaskResponse; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.tasks.RawTaskStatus; import org.elasticsearch.tasks.Task; import org.elasticsearch.tasks.TaskId; import org.elasticsearch.tasks.TaskInfo; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.Collections; @@ -28,15 +28,12 @@ public class GetTaskResponseTests extends ESTestCase { public void testFromXContent() throws IOException { - xContentTester( - this::createParser, - this::createTestInstance, - this::toXContent, - GetTaskResponse::fromXContent) - .supportsUnknownFields(true) + xContentTester(this::createParser, this::createTestInstance, this::toXContent, GetTaskResponse::fromXContent).supportsUnknownFields( + true + ) .assertEqualsConsumer(this::assertEqualInstances) .assertToXContentEquivalence(true) - .randomFieldsExcludeFilter(field ->field.endsWith("headers") || field.endsWith("status")) + .randomFieldsExcludeFilter(field -> field.endsWith("headers") || field.endsWith("status")) .test(); } @@ -71,21 +68,22 @@ static TaskInfo randomTaskInfo() { boolean cancellable = randomBoolean(); boolean cancelled = cancellable && randomBoolean(); TaskId parentTaskId = randomBoolean() ? TaskId.EMPTY_TASK_ID : randomTaskId(); - Map headers = randomBoolean() ? - Collections.emptyMap() : - Collections.singletonMap(randomAlphaOfLength(5), randomAlphaOfLength(5)); + Map headers = randomBoolean() + ? Collections.emptyMap() + : Collections.singletonMap(randomAlphaOfLength(5), randomAlphaOfLength(5)); return new TaskInfo( - taskId, - type, - action, - description, - status, - startTime, - runningTimeNanos, - cancellable, - cancelled, - parentTaskId, - headers); + taskId, + type, + action, + description, + status, + startTime, + runningTimeNanos, + cancellable, + cancelled, + parentTaskId, + headers + ); } private static TaskId randomTaskId() { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/AsyncSearchDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/AsyncSearchDocumentationIT.java index dc2673f786cb9..365760a2b7f16 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/AsyncSearchDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/AsyncSearchDocumentationIT.java @@ -36,9 +36,10 @@ @SuppressWarnings("removal") public class AsyncSearchDocumentationIT extends ESRestHighLevelClientTestCase { - @Before void setUpIndex() throws IOException { - CreateIndexResponse createIndexResponse = highLevelClient().indices().create(new CreateIndexRequest("my-index"), - RequestOptions.DEFAULT); + @Before + void setUpIndex() throws IOException { + CreateIndexResponse createIndexResponse = highLevelClient().indices() + .create(new CreateIndexRequest("my-index"), RequestOptions.DEFAULT); assertTrue(createIndexResponse.isAcknowledged()); } @@ -77,7 +78,6 @@ public void testSubmitAsyncSearch() throws Exception { response.getFailure(); // <7> // end::asyncsearch-submit-response - // tag::asyncsearch-submit-listener ActionListener listener = new ActionListener() { @@ -141,7 +141,6 @@ public void testGetAsyncSearch() throws Exception { response.getFailure(); // <7> // end::asyncsearch-get-response - // tag::asyncsearch-get-listener ActionListener listener = new ActionListener() { @@ -197,7 +196,6 @@ public void testDeleteAsyncSearch() throws Exception { response.isAcknowledged(); // <1> // end::asyncsearch-delete-response - // tag::asyncsearch-delete-listener ActionListener listener = new ActionListener() { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/CCRDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/CCRDocumentationIT.java index 0d0684af0e579..f015863f00534 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/CCRDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/CCRDocumentationIT.java @@ -104,7 +104,7 @@ public void testPutFollow() throws Exception { // Pause following and delete follower index, so that we can execute put follow api again: { PauseFollowRequest pauseFollowRequest = new PauseFollowRequest("follower"); - AcknowledgedResponse pauseFollowResponse = client.ccr().pauseFollow(pauseFollowRequest, RequestOptions.DEFAULT); + AcknowledgedResponse pauseFollowResponse = client.ccr().pauseFollow(pauseFollowRequest, RequestOptions.DEFAULT); assertThat(pauseFollowResponse.isAcknowledged(), is(true)); DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("follower"); @@ -144,7 +144,7 @@ public void onFailure(Exception e) { { PauseFollowRequest pauseFollowRequest = new PauseFollowRequest("follower"); - AcknowledgedResponse pauseFollowResponse = client.ccr().pauseFollow(pauseFollowRequest, RequestOptions.DEFAULT); + AcknowledgedResponse pauseFollowResponse = client.ccr().pauseFollow(pauseFollowRequest, RequestOptions.DEFAULT); assertThat(pauseFollowResponse.isAcknowledged(), is(true)); } } @@ -539,8 +539,11 @@ public void testDeleteAutoFollowPattern() throws Exception { // Put auto follow pattern, so that we can delete it: { - final PutAutoFollowPatternRequest putRequest = - new PutAutoFollowPatternRequest("my_pattern", "local", Collections.singletonList("logs-*")); + final PutAutoFollowPatternRequest putRequest = new PutAutoFollowPatternRequest( + "my_pattern", + "local", + Collections.singletonList("logs-*") + ); AcknowledgedResponse putResponse = client.ccr().putAutoFollowPattern(putRequest, RequestOptions.DEFAULT); assertThat(putResponse.isAcknowledged(), is(true)); } @@ -561,8 +564,11 @@ public void testDeleteAutoFollowPattern() throws Exception { // Put auto follow pattern, so that we can delete it again: { - final PutAutoFollowPatternRequest putRequest = - new PutAutoFollowPatternRequest("my_pattern", "local", Collections.singletonList("logs-*")); + final PutAutoFollowPatternRequest putRequest = new PutAutoFollowPatternRequest( + "my_pattern", + "local", + Collections.singletonList("logs-*") + ); AcknowledgedResponse putResponse = client.ccr().putAutoFollowPattern(putRequest, RequestOptions.DEFAULT); assertThat(putResponse.isAcknowledged(), is(true)); } @@ -599,8 +605,11 @@ public void testGetAutoFollowPattern() throws Exception { // Put auto follow pattern, so that we can get it: { - final PutAutoFollowPatternRequest putRequest = - new PutAutoFollowPatternRequest("my_pattern", "local", Collections.singletonList("logs-*")); + final PutAutoFollowPatternRequest putRequest = new PutAutoFollowPatternRequest( + "my_pattern", + "local", + Collections.singletonList("logs-*") + ); AcknowledgedResponse putResponse = client.ccr().putAutoFollowPattern(putRequest, RequestOptions.DEFAULT); assertThat(putResponse.isAcknowledged(), is(true)); } @@ -886,7 +895,7 @@ public void onFailure(Exception e) { { PauseFollowRequest pauseFollowRequest = new PauseFollowRequest("follower"); - AcknowledgedResponse pauseFollowResponse = client.ccr().pauseFollow(pauseFollowRequest, RequestOptions.DEFAULT); + AcknowledgedResponse pauseFollowResponse = client.ccr().pauseFollow(pauseFollowRequest, RequestOptions.DEFAULT); assertThat(pauseFollowResponse.isAcknowledged(), is(true)); } } @@ -953,7 +962,7 @@ public void onFailure(Exception e) { { PauseFollowRequest pauseFollowRequest = new PauseFollowRequest("follower"); - AcknowledgedResponse pauseFollowResponse = client.ccr().pauseFollow(pauseFollowRequest, RequestOptions.DEFAULT); + AcknowledgedResponse pauseFollowResponse = client.ccr().pauseFollow(pauseFollowRequest, RequestOptions.DEFAULT); assertThat(pauseFollowResponse.isAcknowledged(), is(true)); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/CRUDDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/CRUDDocumentationIT.java index 0b3c2103b243d..d6bf4c04eee0e 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/CRUDDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/CRUDDocumentationIT.java @@ -56,10 +56,6 @@ import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.index.VersionType; import org.elasticsearch.index.get.GetResult; import org.elasticsearch.index.query.MatchAllQueryBuilder; @@ -75,6 +71,10 @@ import org.elasticsearch.script.ScriptType; import org.elasticsearch.search.fetch.subphase.FetchSourceContext; import org.elasticsearch.tasks.TaskId; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.xcontent.json.JsonXContent; import java.util.Collections; import java.util.Date; @@ -237,9 +237,7 @@ public void testIndex() throws Exception { // end::index-optype } { - IndexRequest request = new IndexRequest("posts") - .id("async") - .source("field", "value"); + IndexRequest request = new IndexRequest("posts").id("async").source("field", "value"); ActionListener listener; // tag::index-execute-listener listener = new ActionListener() { @@ -276,13 +274,17 @@ public void testUpdate() throws Exception { assertSame(RestStatus.CREATED, indexResponse.status()); Request request = new Request("POST", "/_scripts/increment-field"); - request.setJsonEntity(Strings.toString(JsonXContent.contentBuilder() - .startObject() + request.setJsonEntity( + Strings.toString( + JsonXContent.contentBuilder() + .startObject() .startObject("script") - .field("lang", "painless") - .field("source", "ctx._source.field += params.count") + .field("lang", "painless") + .field("source", "ctx._source.field += params.count") + .endObject() .endObject() - .endObject())); + ) + ); Response response = client().performRequest(request); assertEquals(RestStatus.OK.getStatus(), response.getStatusLine().getStatusCode()); } @@ -616,8 +618,7 @@ public void testDelete() throws Exception { } { - IndexResponse indexResponse = client.index(new IndexRequest("posts").id("1").source("field", "value") - , RequestOptions.DEFAULT); + IndexResponse indexResponse = client.index(new IndexRequest("posts").id("1").source("field", "value"), RequestOptions.DEFAULT); assertSame(RestStatus.CREATED, indexResponse.status()); // tag::delete-conflict @@ -633,8 +634,10 @@ public void testDelete() throws Exception { // end::delete-conflict } { - IndexResponse indexResponse = client.index(new IndexRequest("posts").id("async").source("field", "value"), - RequestOptions.DEFAULT); + IndexResponse indexResponse = client.index( + new IndexRequest("posts").id("async").source("field", "value"), + RequestOptions.DEFAULT + ); assertSame(RestStatus.CREATED, indexResponse.status()); DeleteRequest request = new DeleteRequest("posts", "async"); @@ -784,18 +787,17 @@ public void onFailure(Exception e) { public void testReindex() throws Exception { RestHighLevelClient client = highLevelClient(); { - String mapping = - " \"properties\": {\n" + - " \"user\": {\n" + - " \"type\": \"text\"\n" + - " },\n" + - " \"field1\": {\n" + - " \"type\": \"integer\"\n" + - " },\n" + - " \"field2\": {\n" + - " \"type\": \"integer\"\n" + - " }\n" + - " }"; + String mapping = " \"properties\": {\n" + + " \"user\": {\n" + + " \"type\": \"text\"\n" + + " },\n" + + " \"field1\": {\n" + + " \"type\": \"integer\"\n" + + " },\n" + + " \"field2\": {\n" + + " \"type\": \"integer\"\n" + + " }\n" + + " }"; createIndex("source1", Settings.EMPTY, mapping); createIndex("source2", Settings.EMPTY, mapping); createPipeline("my_pipeline"); @@ -858,7 +860,6 @@ public void testReindex() throws Exception { request.setScroll(TimeValue.timeValueMinutes(10)); // <1> // end::reindex-request-scroll - // tag::reindex-execute BulkByScrollResponse bulkResponse = client.reindex(request, RequestOptions.DEFAULT); @@ -986,18 +987,17 @@ public void onFailure(Exception e) { public void testUpdateByQuery() throws Exception { RestHighLevelClient client = highLevelClient(); { - String mapping = - " \"properties\": {\n" + - " \"user\": {\n" + - " \"type\": \"text\"\n" + - " },\n" + - " \"field1\": {\n" + - " \"type\": \"integer\"\n" + - " },\n" + - " \"field2\": {\n" + - " \"type\": \"integer\"\n" + - " }\n" + - " }"; + String mapping = " \"properties\": {\n" + + " \"user\": {\n" + + " \"type\": \"text\"\n" + + " },\n" + + " \"field1\": {\n" + + " \"type\": \"integer\"\n" + + " },\n" + + " \"field2\": {\n" + + " \"type\": \"integer\"\n" + + " }\n" + + " }"; createIndex("source1", Settings.EMPTY, mapping); createIndex("source2", Settings.EMPTY, mapping); createPipeline("my_pipeline"); @@ -1110,18 +1110,17 @@ public void onFailure(Exception e) { public void testDeleteByQuery() throws Exception { RestHighLevelClient client = highLevelClient(); { - String mapping = - " \"properties\": {\n" + - " \"user\": {\n" + - " \"type\": \"text\"\n" + - " },\n" + - " \"field1\": {\n" + - " \"type\": \"integer\"\n" + - " },\n" + - " \"field2\": {\n" + - " \"type\": \"integer\"\n" + - " }\n" + - " }"; + String mapping = " \"properties\": {\n" + + " \"user\": {\n" + + " \"type\": \"text\"\n" + + " },\n" + + " \"field1\": {\n" + + " \"type\": \"integer\"\n" + + " },\n" + + " \"field2\": {\n" + + " \"type\": \"integer\"\n" + + " }\n" + + " }"; createIndex("source1", Settings.EMPTY, mapping); createIndex("source2", Settings.EMPTY, mapping); } @@ -1224,23 +1223,22 @@ public void testGet() throws Exception { { Request createIndex = new Request("PUT", "/posts"); createIndex.setJsonEntity( - "{\n" + - " \"mappings\" : {\n" + - " \"properties\" : {\n" + - " \"message\" : {\n" + - " \"type\": \"text\",\n" + - " \"store\": true\n" + - " }\n" + - " }\n" + - " }\n" + - "}"); + "{\n" + + " \"mappings\" : {\n" + + " \"properties\" : {\n" + + " \"message\" : {\n" + + " \"type\": \"text\",\n" + + " \"store\": true\n" + + " }\n" + + " }\n" + + " }\n" + + "}" + ); Response response = client().performRequest(createIndex); assertEquals(200, response.getStatusLine().getStatusCode()); IndexRequest indexRequest = new IndexRequest("posts").id("1") - .source("user", "kimchy", - "postDate", new Date(), - "message", "trying out Elasticsearch"); + .source("user", "kimchy", "postDate", new Date(), "message", "trying out Elasticsearch"); IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT); assertEquals(DocWriteResponse.Result.CREATED, indexResponse.getResult()); } @@ -1397,23 +1395,22 @@ public void testGetSource() throws Exception { { Request createIndex = new Request("PUT", "/posts"); createIndex.setJsonEntity( - "{\n" + - " \"mappings\" : {\n" + - " \"properties\" : {\n" + - " \"message\" : {\n" + - " \"type\": \"text\",\n" + - " \"store\": true\n" + - " }\n" + - " }\n" + - " }\n" + - "}"); + "{\n" + + " \"mappings\" : {\n" + + " \"properties\" : {\n" + + " \"message\" : {\n" + + " \"type\": \"text\",\n" + + " \"store\": true\n" + + " }\n" + + " }\n" + + " }\n" + + "}" + ); Response response = client().performRequest(createIndex); assertEquals(200, response.getStatusLine().getStatusCode()); IndexRequest indexRequest = new IndexRequest("posts").id("1") - .source("user", "kimchy", - "postDate", new Date(), - "message", "trying out Elasticsearch"); + .source("user", "kimchy", "postDate", new Date(), "message", "trying out Elasticsearch"); IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT); assertEquals(DocWriteResponse.Result.CREATED, indexResponse.getResult()); } @@ -1633,14 +1630,16 @@ public void afterBulk(long executionId, BulkRequest request, // Not entirely sure if _termvectors belongs to CRUD, and in the absence of a better place, will have it here public void testTermVectors() throws Exception { RestHighLevelClient client = highLevelClient(); - CreateIndexRequest authorsRequest = new CreateIndexRequest("authors") - .mapping(XContentFactory.jsonBuilder().startObject() + CreateIndexRequest authorsRequest = new CreateIndexRequest("authors").mapping( + XContentFactory.jsonBuilder() + .startObject() .startObject("properties") - .startObject("user") - .field("type", "keyword") - .endObject() + .startObject("user") + .field("type", "keyword") + .endObject() .endObject() - .endObject()); + .endObject() + ); CreateIndexResponse authorsResponse = client.indices().create(authorsRequest, RequestOptions.DEFAULT); assertTrue(authorsResponse.isAcknowledged()); client.index(new IndexRequest("index").id("1").source("user", "kimchy"), RequestOptions.DEFAULT); @@ -1697,7 +1696,6 @@ public void testTermVectors() throws Exception { client.termvectors(request, RequestOptions.DEFAULT); // end::term-vectors-execute - // tag::term-vectors-response String index = response.getIndex(); // <1> String id = response.getId(); // <2> @@ -1759,18 +1757,19 @@ public void onFailure(Exception e) { } - // Not entirely sure if _mtermvectors belongs to CRUD, and in the absence of a better place, will have it here public void testMultiTermVectors() throws Exception { RestHighLevelClient client = highLevelClient(); - CreateIndexRequest authorsRequest = new CreateIndexRequest("authors") - .mapping(XContentFactory.jsonBuilder().startObject() + CreateIndexRequest authorsRequest = new CreateIndexRequest("authors").mapping( + XContentFactory.jsonBuilder() + .startObject() .startObject("properties") - .startObject("user") - .field("type", "keyword") - .endObject() + .startObject("user") + .field("type", "keyword") + .endObject() + .endObject() .endObject() - .endObject()); + ); CreateIndexResponse authorsResponse = client.indices().create(authorsRequest, RequestOptions.DEFAULT); assertTrue(authorsResponse.isAcknowledged()); client.index(new IndexRequest("index").id("1").source("user", "kimchy"), RequestOptions.DEFAULT); @@ -1847,16 +1846,17 @@ public void testMultiGet() throws Exception { { Request createIndex = new Request("PUT", "/index"); createIndex.setJsonEntity( - "{\n" + - " \"mappings\" : {\n" + - " \"properties\" : {\n" + - " \"foo\" : {\n" + - " \"type\": \"text\",\n" + - " \"store\": true\n" + - " }\n" + - " }\n" + - " }\n" + - "}"); + "{\n" + + " \"mappings\" : {\n" + + " \"properties\" : {\n" + + " \"foo\" : {\n" + + " \"type\": \"text\",\n" + + " \"store\": true\n" + + " }\n" + + " }\n" + + " }\n" + + "}" + ); Response response = client().performRequest(createIndex); assertEquals(200, response.getStatusLine().getStatusCode()); } @@ -1865,10 +1865,10 @@ public void testMultiGet() throws Exception { source.put("foo", "val1"); source.put("bar", "val2"); source.put("baz", "val3"); - client.index(new IndexRequest("index") - .id("example_id") - .source(source) - .setRefreshPolicy(RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT); + client.index( + new IndexRequest("index").id("example_id").source(source).setRefreshPolicy(RefreshPolicy.IMMEDIATE), + RequestOptions.DEFAULT + ); { // tag::multi-get-request diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/ClusterClientDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/ClusterClientDocumentationIT.java index dfa0e7b9364d1..44b8e272804dd 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/ClusterClientDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/ClusterClientDocumentationIT.java @@ -42,9 +42,9 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.indices.recovery.RecoverySettings; import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.xcontent.XContentType; import java.io.IOException; import java.util.HashMap; @@ -140,8 +140,7 @@ public void testClusterPutSettings() throws IOException { Settings persistentSettingsResponse = response.getPersistentSettings(); // <3> // end::put-settings-response assertTrue(acknowledged); - assertThat(transientSettingsResponse.get(transientSettingKey), - equalTo(transientSettingValue + ByteSizeUnit.BYTES.getSuffix())); + assertThat(transientSettingsResponse.get(transientSettingKey), equalTo(transientSettingValue + ByteSizeUnit.BYTES.getSuffix())); assertThat(persistentSettingsResponse.get(persistentSettingKey), equalTo(persistentSettingValue)); // tag::put-settings-request-reset-transient @@ -452,7 +451,6 @@ public void testRemoteInfoAsync() throws Exception { RemoteInfoRequest request = new RemoteInfoRequest(); // end::remote-info-request - // tag::remote-info-execute-listener ActionListener listener = new ActionListener<>() { @@ -484,8 +482,8 @@ public void testGetComponentTemplates() throws Exception { { Template template = new Template(Settings.builder().put("index.number_of_replicas", 3).build(), null, null); ComponentTemplate componentTemplate = new ComponentTemplate(template, null, null); - PutComponentTemplateRequest putComponentTemplateRequest = - new PutComponentTemplateRequest().name("ct1").componentTemplate(componentTemplate); + PutComponentTemplateRequest putComponentTemplateRequest = new PutComponentTemplateRequest().name("ct1") + .componentTemplate(componentTemplate); client.cluster().putComponentTemplate(putComponentTemplateRequest, RequestOptions.DEFAULT); assertTrue(client.cluster().putComponentTemplate(putComponentTemplateRequest, RequestOptions.DEFAULT).isAcknowledged()); @@ -625,20 +623,16 @@ public void onFailure(Exception e) { public void testDeleteComponentTemplate() throws Exception { RestHighLevelClient client = highLevelClient(); { - PutComponentTemplateRequest request = new PutComponentTemplateRequest() - .name("ct1"); - - Settings settings = Settings.builder() - .put("index.number_of_shards", 3) - .put("index.number_of_replicas", 1) - .build(); - String mappingJson = "{\n" + - " \"properties\": {\n" + - " \"message\": {\n" + - " \"type\": \"text\"\n" + - " }\n" + - " }\n" + - "}"; + PutComponentTemplateRequest request = new PutComponentTemplateRequest().name("ct1"); + + Settings settings = Settings.builder().put("index.number_of_shards", 3).put("index.number_of_replicas", 1).build(); + String mappingJson = "{\n" + + " \"properties\": {\n" + + " \"message\": {\n" + + " \"type\": \"text\"\n" + + " }\n" + + " }\n" + + "}"; AliasMetadata twitterAlias = AliasMetadata.builder("twitter_alias").build(); Template template = new Template(settings, new CompressedXContent(mappingJson), Map.of("twitter_alias", twitterAlias)); @@ -664,13 +658,9 @@ public void testDeleteComponentTemplate() throws Exception { assertThat(acknowledged, equalTo(true)); { - PutComponentTemplateRequest request = new PutComponentTemplateRequest() - .name("ct1"); + PutComponentTemplateRequest request = new PutComponentTemplateRequest().name("ct1"); - Settings settings = Settings.builder() - .put("index.number_of_shards", 3) - .put("index.number_of_replicas", 1) - .build(); + Settings settings = Settings.builder().put("index.number_of_shards", 3).put("index.number_of_replicas", 1).build(); Template template = new Template(settings, null, null); request.componentTemplate(new ComponentTemplate(template, null, null)); assertTrue(client.cluster().putComponentTemplate(request, RequestOptions.DEFAULT).isAcknowledged()); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/EnrichDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/EnrichDocumentationIT.java index 993e76ed7973d..6a4834eb84cae 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/EnrichDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/EnrichDocumentationIT.java @@ -16,9 +16,9 @@ import org.elasticsearch.client.enrich.DeletePolicyRequest; import org.elasticsearch.client.enrich.ExecutePolicyRequest; import org.elasticsearch.client.enrich.ExecutePolicyResponse; -import org.elasticsearch.client.enrich.NamedPolicy; import org.elasticsearch.client.enrich.GetPolicyRequest; import org.elasticsearch.client.enrich.GetPolicyResponse; +import org.elasticsearch.client.enrich.NamedPolicy; import org.elasticsearch.client.enrich.PutPolicyRequest; import org.elasticsearch.client.enrich.StatsRequest; import org.elasticsearch.client.enrich.StatsResponse; @@ -48,8 +48,9 @@ public void cleanup() { public void testPutPolicy() throws Exception { RestHighLevelClient client = highLevelClient(); - CreateIndexRequest createIndexRequest = new CreateIndexRequest("users") - .mapping(Map.of("properties", Map.of("email", Map.of("type", "keyword")))); + CreateIndexRequest createIndexRequest = new CreateIndexRequest("users").mapping( + Map.of("properties", Map.of("email", Map.of("type", "keyword"))) + ); client.indices().create(createIndexRequest, RequestOptions.DEFAULT); // tag::enrich-put-policy-request @@ -98,14 +99,19 @@ public void testDeletePolicy() throws Exception { RestHighLevelClient client = highLevelClient(); { - CreateIndexRequest createIndexRequest = new CreateIndexRequest("users") - .mapping(Map.of("properties", Map.of("email", Map.of("type", "keyword")))); + CreateIndexRequest createIndexRequest = new CreateIndexRequest("users").mapping( + Map.of("properties", Map.of("email", Map.of("type", "keyword"))) + ); client.indices().create(createIndexRequest, RequestOptions.DEFAULT); // Add a policy, so that it can be deleted: PutPolicyRequest putPolicyRequest = new PutPolicyRequest( - "users-policy", "match", List.of("users"), - "email", List.of("address", "zip", "city", "state")); + "users-policy", + "match", + List.of("users"), + "email", + List.of("address", "zip", "city", "state") + ); client.enrich().putPolicy(putPolicyRequest, RequestOptions.DEFAULT); } @@ -153,13 +159,18 @@ public void onFailure(Exception e) { public void testGetPolicy() throws Exception { RestHighLevelClient client = highLevelClient(); - CreateIndexRequest createIndexRequest = new CreateIndexRequest("users") - .mapping(Map.of("properties", Map.of("email", Map.of("type", "keyword")))); + CreateIndexRequest createIndexRequest = new CreateIndexRequest("users").mapping( + Map.of("properties", Map.of("email", Map.of("type", "keyword"))) + ); client.indices().create(createIndexRequest, RequestOptions.DEFAULT); PutPolicyRequest putPolicyRequest = new PutPolicyRequest( - "users-policy", "match", List.of("users"), - "email", List.of("address", "zip", "city", "state")); + "users-policy", + "match", + List.of("users"), + "email", + List.of("address", "zip", "city", "state") + ); client.enrich().putPolicy(putPolicyRequest, RequestOptions.DEFAULT); // tag::enrich-get-policy-request @@ -255,12 +266,17 @@ public void testExecutePolicy() throws Exception { RestHighLevelClient client = highLevelClient(); { - CreateIndexRequest createIndexRequest = new CreateIndexRequest("users") - .mapping(Map.of("properties", Map.of("email", Map.of("type", "keyword")))); + CreateIndexRequest createIndexRequest = new CreateIndexRequest("users").mapping( + Map.of("properties", Map.of("email", Map.of("type", "keyword"))) + ); client.indices().create(createIndexRequest, RequestOptions.DEFAULT); PutPolicyRequest putPolicyRequest = new PutPolicyRequest( - "users-policy", "match", List.of("users"), - "email", List.of("address", "zip", "city", "state")); + "users-policy", + "match", + List.of("users"), + "email", + List.of("address", "zip", "city", "state") + ); client.enrich().putPolicy(putPolicyRequest, RequestOptions.DEFAULT); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/GraphDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/GraphDocumentationIT.java index 9a4531a1cbe24..771ec47fbe5ff 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/GraphDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/GraphDocumentationIT.java @@ -13,14 +13,14 @@ import org.elasticsearch.client.Request; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; -import org.elasticsearch.core.SuppressForbidden; -import org.elasticsearch.index.query.TermQueryBuilder; import org.elasticsearch.client.graph.Connection; import org.elasticsearch.client.graph.GraphExploreRequest; import org.elasticsearch.client.graph.GraphExploreResponse; import org.elasticsearch.client.graph.Hop; import org.elasticsearch.client.graph.Vertex; import org.elasticsearch.client.graph.VertexRequest; +import org.elasticsearch.core.SuppressForbidden; +import org.elasticsearch.index.query.TermQueryBuilder; import org.junit.Before; import java.io.IOException; @@ -29,7 +29,6 @@ @SuppressWarnings("removal") public class GraphDocumentationIT extends ESRestHighLevelClientTestCase { - @Before public void indexDocuments() throws IOException { // Create chain of doc IDs across indices 1->2->3 @@ -48,8 +47,6 @@ public void indexDocuments() throws IOException { public void testExplore() throws Exception { RestHighLevelClient client = highLevelClient(); - - // tag::x-pack-graph-explore-request GraphExploreRequest request = new GraphExploreRequest(); request.indices("index1", "index2"); @@ -69,7 +66,6 @@ public void testExplore() throws Exception { GraphExploreResponse exploreResponse = client.graph().explore(request, RequestOptions.DEFAULT); // <4> // end::x-pack-graph-explore-request - // tag::x-pack-graph-explore-response Collection v = exploreResponse.getVertices(); Collection c = exploreResponse.getConnections(); @@ -83,7 +79,6 @@ public void testExplore() throws Exception { } // end::x-pack-graph-explore-response - Collection initialVertices = exploreResponse.getVertices(); // tag::x-pack-graph-explore-expand diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/ILMDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/ILMDocumentationIT.java index 33f4b45d648ba..1be3802c36819 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/ILMDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/ILMDocumentationIT.java @@ -122,11 +122,8 @@ public void testPutLifecyclePolicy() throws Exception { // Delete the policy so it can be added again { - DeleteLifecyclePolicyRequest deleteRequest = - new DeleteLifecyclePolicyRequest("my_policy"); - AcknowledgedResponse deleteResponse = client.indexLifecycle() - .deleteLifecyclePolicy(deleteRequest, - RequestOptions.DEFAULT); + DeleteLifecyclePolicyRequest deleteRequest = new DeleteLifecyclePolicyRequest("my_policy"); + AcknowledgedResponse deleteResponse = client.indexLifecycle().deleteLifecyclePolicy(deleteRequest, RequestOptions.DEFAULT); assertTrue(deleteResponse.isAcknowledged()); } @@ -166,19 +163,13 @@ public void testDeletePolicy() throws IOException, InterruptedException { { Map phases = new HashMap<>(); Map hotActions = new HashMap<>(); - hotActions.put(RolloverAction.NAME, new RolloverAction( - new ByteSizeValue(50, ByteSizeUnit.GB), null, null, null)); + hotActions.put(RolloverAction.NAME, new RolloverAction(new ByteSizeValue(50, ByteSizeUnit.GB), null, null, null)); phases.put("hot", new Phase("hot", TimeValue.ZERO, hotActions)); - Map deleteActions = - Collections.singletonMap(DeleteAction.NAME, - new DeleteAction()); - phases.put("delete", - new Phase("delete", - new TimeValue(90, TimeUnit.DAYS), deleteActions)); + Map deleteActions = Collections.singletonMap(DeleteAction.NAME, new DeleteAction()); + phases.put("delete", new Phase("delete", new TimeValue(90, TimeUnit.DAYS), deleteActions)); LifecyclePolicy myPolicy = new LifecyclePolicy("my_policy", phases); putRequest = new PutLifecyclePolicyRequest(myPolicy); - AcknowledgedResponse putResponse = client.indexLifecycle(). - putLifecyclePolicy(putRequest, RequestOptions.DEFAULT); + AcknowledgedResponse putResponse = client.indexLifecycle().putLifecyclePolicy(putRequest, RequestOptions.DEFAULT); assertTrue(putResponse.isAcknowledged()); } @@ -200,8 +191,7 @@ public void testDeletePolicy() throws IOException, InterruptedException { // Put the policy again so we can delete it again { - AcknowledgedResponse putResponse = client.indexLifecycle(). - putLifecyclePolicy(putRequest, RequestOptions.DEFAULT); + AcknowledgedResponse putResponse = client.indexLifecycle().putLifecyclePolicy(putRequest, RequestOptions.DEFAULT); assertTrue(putResponse.isAcknowledged()); } @@ -241,16 +231,11 @@ public void testGetLifecyclePolicy() throws IOException, InterruptedException { { Map phases = new HashMap<>(); Map hotActions = new HashMap<>(); - hotActions.put(RolloverAction.NAME, new RolloverAction( - new ByteSizeValue(50, ByteSizeUnit.GB), null, null, null)); + hotActions.put(RolloverAction.NAME, new RolloverAction(new ByteSizeValue(50, ByteSizeUnit.GB), null, null, null)); phases.put("hot", new Phase("hot", TimeValue.ZERO, hotActions)); - Map deleteActions = - Collections.singletonMap(DeleteAction.NAME, - new DeleteAction()); - phases.put("delete", - new Phase("delete", - new TimeValue(90, TimeUnit.DAYS), deleteActions)); + Map deleteActions = Collections.singletonMap(DeleteAction.NAME, new DeleteAction()); + phases.put("delete", new Phase("delete", new TimeValue(90, TimeUnit.DAYS), deleteActions)); myPolicyAsPut = new LifecyclePolicy("my_policy", phases); PutLifecyclePolicyRequest putRequest = new PutLifecyclePolicyRequest(myPolicyAsPut); @@ -262,11 +247,9 @@ public void testGetLifecyclePolicy() throws IOException, InterruptedException { PutLifecyclePolicyRequest putRequest2 = new PutLifecyclePolicyRequest(otherPolicyAsPut); - AcknowledgedResponse putResponse = client.indexLifecycle(). - putLifecyclePolicy(putRequest, RequestOptions.DEFAULT); + AcknowledgedResponse putResponse = client.indexLifecycle().putLifecyclePolicy(putRequest, RequestOptions.DEFAULT); assertTrue(putResponse.isAcknowledged()); - AcknowledgedResponse putResponse2 = client.indexLifecycle(). - putLifecyclePolicy(putRequest2, RequestOptions.DEFAULT); + AcknowledgedResponse putResponse2 = client.indexLifecycle().putLifecyclePolicy(putRequest2, RequestOptions.DEFAULT); assertTrue(putResponse2.isAcknowledged()); } @@ -341,37 +324,38 @@ public void testExplainLifecycle() throws Exception { { Map phases = new HashMap<>(); Map hotActions = new HashMap<>(); - hotActions.put(RolloverAction.NAME, new RolloverAction( - new ByteSizeValue(50, ByteSizeUnit.GB), null, null, null)); + hotActions.put(RolloverAction.NAME, new RolloverAction(new ByteSizeValue(50, ByteSizeUnit.GB), null, null, null)); phases.put("hot", new Phase("hot", TimeValue.ZERO, hotActions)); - LifecyclePolicy policy = new LifecyclePolicy("my_policy", - phases); - PutLifecyclePolicyRequest putRequest = - new PutLifecyclePolicyRequest(policy); + LifecyclePolicy policy = new LifecyclePolicy("my_policy", phases); + PutLifecyclePolicyRequest putRequest = new PutLifecyclePolicyRequest(policy); client.indexLifecycle().putLifecyclePolicy(putRequest, RequestOptions.DEFAULT); - CreateIndexRequest createIndexRequest = new CreateIndexRequest("my_index-1") - .settings(Settings.builder() + CreateIndexRequest createIndexRequest = new CreateIndexRequest("my_index-1").settings( + Settings.builder() .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) .put("index.lifecycle.name", "my_policy") .put("index.lifecycle.rollover_alias", "my_alias") - .build()); + .build() + ); createIndexRequest.alias(new Alias("my_alias").writeIndex(true)); client.indices().create(createIndexRequest, RequestOptions.DEFAULT); - CreateIndexRequest createOtherIndexRequest = new CreateIndexRequest("other_index") - .settings(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .build()); + CreateIndexRequest createOtherIndexRequest = new CreateIndexRequest("other_index").settings( + Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).build() + ); client.indices().create(createOtherIndexRequest, RequestOptions.DEFAULT); - // wait for the policy to become active - assertBusy(() -> assertNotNull(client.indexLifecycle() - .explainLifecycle(new ExplainLifecycleRequest("my_index-1"), RequestOptions.DEFAULT) - .getIndexResponses().get("my_index-1").getAction())); + assertBusy( + () -> assertNotNull( + client.indexLifecycle() + .explainLifecycle(new ExplainLifecycleRequest("my_index-1"), RequestOptions.DEFAULT) + .getIndexResponses() + .get("my_index-1") + .getAction() + ) + ); } // tag::ilm-explain-lifecycle-request @@ -379,7 +363,6 @@ public void testExplainLifecycle() throws Exception { new ExplainLifecycleRequest("my_index-1", "other_index"); // <1> // end::ilm-explain-lifecycle-request - assertBusy(() -> { // tag::ilm-explain-lifecycle-execute ExplainLifecycleResponse response = client.indexLifecycle() @@ -507,9 +490,7 @@ public void onFailure(Exception e) { assertTrue(latch.await(30L, TimeUnit.SECONDS)); // Check that ILM is running again - LifecycleManagementStatusResponse response = - client.indexLifecycle() - .lifecycleManagementStatus(request, RequestOptions.DEFAULT); + LifecycleManagementStatusResponse response = client.indexLifecycle().lifecycleManagementStatus(request, RequestOptions.DEFAULT); OperationMode operationMode = response.getOperationMode(); assertEquals(OperationMode.RUNNING, operationMode); @@ -609,18 +590,17 @@ public void testRetryPolicy() throws Exception { warmActions.put(ShrinkAction.NAME, new ShrinkAction(3, null)); phases.put("warm", new Phase("warm", TimeValue.ZERO, warmActions)); - LifecyclePolicy policy = new LifecyclePolicy("my_policy", - phases); - PutLifecyclePolicyRequest putRequest = - new PutLifecyclePolicyRequest(policy); + LifecyclePolicy policy = new LifecyclePolicy("my_policy", phases); + PutLifecyclePolicyRequest putRequest = new PutLifecyclePolicyRequest(policy); client.indexLifecycle().putLifecyclePolicy(putRequest, RequestOptions.DEFAULT); - CreateIndexRequest createIndexRequest = new CreateIndexRequest("my_index") - .settings(Settings.builder() + CreateIndexRequest createIndexRequest = new CreateIndexRequest("my_index").settings( + Settings.builder() .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 2) .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) .put("index.lifecycle.name", "my_policy") - .build()); + .build() + ); client.indices().create(createIndexRequest, RequestOptions.DEFAULT); } @@ -629,7 +609,6 @@ public void testRetryPolicy() throws Exception { new RetryLifecyclePolicyRequest("my_index"); // <1> // end::ilm-retry-lifecycle-policy-request - try { // tag::ilm-retry-lifecycle-policy-execute AcknowledgedResponse response = client.indexLifecycle() @@ -645,8 +624,13 @@ public void testRetryPolicy() throws Exception { // the retry API might fail as the shrink action steps are retryable (ILM will stuck in the `check-target-shards-count` step // with no failure, the retry API will fail) // assert that's the exception we encountered (we want to test to fail if there is an actual error with the retry api) - assertThat(e.getMessage(), containsStringIgnoringCase("reason=cannot retry an action for an index [my_index] that has not " + - "encountered an error when running a Lifecycle Policy")); + assertThat( + e.getMessage(), + containsStringIgnoringCase( + "reason=cannot retry an action for an index [my_index] that has not " + + "encountered an error when running a Lifecycle Policy" + ) + ); } // tag::ilm-retry-lifecycle-policy-execute-listener @@ -681,21 +665,30 @@ public void testRemovePolicyFromIndex() throws Exception { // setup policy for index Map phases = new HashMap<>(); - phases.put("delete", new Phase("delete", TimeValue.timeValueHours(10L), - Collections.singletonMap(DeleteAction.NAME, new DeleteAction()))); + phases.put( + "delete", + new Phase("delete", TimeValue.timeValueHours(10L), Collections.singletonMap(DeleteAction.NAME, new DeleteAction())) + ); LifecyclePolicy policy = new LifecyclePolicy("my_policy", phases); PutLifecyclePolicyRequest putRequest = new PutLifecyclePolicyRequest(policy); client.indexLifecycle().putLifecyclePolicy(putRequest, RequestOptions.DEFAULT); - CreateIndexRequest createIndexRequest = new CreateIndexRequest("my_index") - .settings(Settings.builder() + CreateIndexRequest createIndexRequest = new CreateIndexRequest("my_index").settings( + Settings.builder() .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) .put("index.lifecycle.name", "my_policy") - .build()); + .build() + ); client.indices().create(createIndexRequest, RequestOptions.DEFAULT); - assertBusy(() -> assertTrue(client.indexLifecycle() - .explainLifecycle(new ExplainLifecycleRequest("my_index"), RequestOptions.DEFAULT) - .getIndexResponses().get("my_index").managedByILM())); + assertBusy( + () -> assertTrue( + client.indexLifecycle() + .explainLifecycle(new ExplainLifecycleRequest("my_index"), RequestOptions.DEFAULT) + .getIndexResponses() + .get("my_index") + .managedByILM() + ) + ); // tag::ilm-remove-lifecycle-policy-from-index-request List indices = new ArrayList<>(); @@ -704,7 +697,6 @@ public void testRemovePolicyFromIndex() throws Exception { new RemoveIndexLifecyclePolicyRequest(indices); // <1> // end::ilm-remove-lifecycle-policy-from-index-request - // tag::ilm-remove-lifecycle-policy-from-index-execute RemoveIndexLifecyclePolicyResponse response = client .indexLifecycle() @@ -724,9 +716,15 @@ public void testRemovePolicyFromIndex() throws Exception { // re-apply policy on index updateIndexSettings("my_index", Settings.builder().put("index.lifecycle.name", "my_policy")); - assertBusy(() -> assertTrue(client.indexLifecycle() - .explainLifecycle(new ExplainLifecycleRequest("my_index"), RequestOptions.DEFAULT) - .getIndexResponses().get("my_index").managedByILM())); + assertBusy( + () -> assertTrue( + client.indexLifecycle() + .explainLifecycle(new ExplainLifecycleRequest("my_index"), RequestOptions.DEFAULT) + .getIndexResponses() + .get("my_index") + .managedByILM() + ) + ); // tag::ilm-remove-lifecycle-policy-from-index-execute-listener ActionListener listener = @@ -772,8 +770,8 @@ public void testAddSnapshotLifecyclePolicy() throws Exception { repoRequest.settings(settingsBuilder); repoRequest.name("my_repository"); repoRequest.type(FsRepository.TYPE); - org.elasticsearch.action.support.master.AcknowledgedResponse response = - client.snapshot().createRepository(repoRequest, RequestOptions.DEFAULT); + org.elasticsearch.action.support.master.AcknowledgedResponse response = client.snapshot() + .createRepository(repoRequest, RequestOptions.DEFAULT); assertTrue(response.isAcknowledged()); //////// PUT @@ -915,24 +913,23 @@ public void onFailure(Exception e) { // We need a listener that will actually wait for the snapshot to be created CountDownLatch latch = new CountDownLatch(1); - executeListener = - new ActionListener<>() { - @Override - public void onResponse(ExecuteSnapshotLifecyclePolicyResponse r) { - try { - assertSnapshotExists(client, "my_repository", r.getSnapshotName()); - } catch (Exception e) { - // Ignore - } finally { - latch.countDown(); - } - } - - @Override - public void onFailure(Exception e) { + executeListener = new ActionListener<>() { + @Override + public void onResponse(ExecuteSnapshotLifecyclePolicyResponse r) { + try { + assertSnapshotExists(client, "my_repository", r.getSnapshotName()); + } catch (Exception e) { + // Ignore + } finally { latch.countDown(); - fail("failed to execute slm execute: " + e); } + } + + @Override + public void onFailure(Exception e) { + latch.countDown(); + fail("failed to execute slm execute: " + e); + } }; // tag::slm-execute-snapshot-lifecycle-policy-execute-async @@ -954,9 +951,7 @@ public void onFailure(Exception e) { SnapshotLifecycleStats.SnapshotPolicyStats policyStats = stats.getMetrics().get("policy_id"); // end::slm-get-snapshot-lifecycle-stats-execute - assertThat( - statsResp.getStats().getMetrics().get("policy_id").getSnapshotsTaken(), - greaterThanOrEqualTo(1L)); + assertThat(statsResp.getStats().getMetrics().get("policy_id").getSnapshotsTaken(), greaterThanOrEqualTo(1L)); //////// DELETE // tag::slm-delete-snapshot-lifecycle-policy-request @@ -1039,7 +1034,7 @@ public void onFailure(Exception e) { private void assertSnapshotExists(final RestHighLevelClient client, final String repo, final String snapshotName) throws Exception { assertBusy(() -> { - GetSnapshotsRequest getSnapshotsRequest = new GetSnapshotsRequest(new String[]{repo}, new String[]{snapshotName}); + GetSnapshotsRequest getSnapshotsRequest = new GetSnapshotsRequest(new String[] { repo }, new String[] { snapshotName }); try { final GetSnapshotsResponse snaps = client.snapshot().get(getSnapshotsRequest, RequestOptions.DEFAULT); Optional info = snaps.getSnapshots().stream().findFirst(); @@ -1116,9 +1111,7 @@ public void onFailure(Exception e) { assertTrue(latch.await(30L, TimeUnit.SECONDS)); // Check that SLM is running again - LifecycleManagementStatusResponse response = - client.indexLifecycle() - .getSLMStatus(request, RequestOptions.DEFAULT); + LifecycleManagementStatusResponse response = client.indexLifecycle().getSLMStatus(request, RequestOptions.DEFAULT); OperationMode operationMode = response.getOperationMode(); assertEquals(OperationMode.RUNNING, operationMode); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IndicesClientDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IndicesClientDocumentationIT.java index 3dac417e5244c..b321e92e71196 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IndicesClientDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IndicesClientDocumentationIT.java @@ -140,8 +140,7 @@ public void testIndicesExist() throws IOException { RestHighLevelClient client = highLevelClient(); { - CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("twitter"), - RequestOptions.DEFAULT); + CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("twitter"), RequestOptions.DEFAULT); assertTrue(createIndexResponse.isAcknowledged()); } @@ -202,6 +201,7 @@ public void onFailure(Exception e) { assertTrue(latch.await(30L, TimeUnit.SECONDS)); } } + public void testDeleteIndex() throws IOException { RestHighLevelClient client = highLevelClient(); @@ -571,9 +571,7 @@ public void testGetMapping() throws IOException { CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("twitter"), RequestOptions.DEFAULT); assertTrue(createIndexResponse.isAcknowledged()); PutMappingRequest request = new PutMappingRequest("twitter"); - request.source("{ \"properties\": { \"message\": { \"type\": \"text\" } } }", - XContentType.JSON - ); + request.source("{ \"properties\": { \"message\": { \"type\": \"text\" } } }", XContentType.JSON); AcknowledgedResponse putMappingResponse = client.indices().putMapping(request, RequestOptions.DEFAULT); assertTrue(putMappingResponse.isAcknowledged()); } @@ -620,9 +618,7 @@ public void testGetMappingAsync() throws Exception { CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("twitter"), RequestOptions.DEFAULT); assertTrue(createIndexResponse.isAcknowledged()); PutMappingRequest request = new PutMappingRequest("twitter"); - request.source("{ \"properties\": { \"message\": { \"type\": \"text\" } } }", - XContentType.JSON - ); + request.source("{ \"properties\": { \"message\": { \"type\": \"text\" } } }", XContentType.JSON); AcknowledgedResponse putMappingResponse = client.indices().putMapping(request, RequestOptions.DEFAULT); assertTrue(putMappingResponse.isAcknowledged()); } @@ -684,17 +680,18 @@ public void testGetFieldMapping() throws IOException, InterruptedException { assertTrue(createIndexResponse.isAcknowledged()); PutMappingRequest request = new PutMappingRequest("twitter"); request.source( - "{\n" + - " \"properties\": {\n" + - " \"message\": {\n" + - " \"type\": \"text\"\n" + - " },\n" + - " \"timestamp\": {\n" + - " \"type\": \"date\"\n" + - " }\n" + - " }\n" + - "}", // <1> - XContentType.JSON); + "{\n" + + " \"properties\": {\n" + + " \"message\": {\n" + + " \"type\": \"text\"\n" + + " },\n" + + " \"timestamp\": {\n" + + " \"type\": \"date\"\n" + + " }\n" + + " }\n" + + "}", // <1> + XContentType.JSON + ); AcknowledgedResponse putMappingResponse = client.indices().putMapping(request, RequestOptions.DEFAULT); assertTrue(putMappingResponse.isAcknowledged()); } @@ -749,10 +746,8 @@ public void onFailure(Exception e) { final CountDownLatch latch = new CountDownLatch(1); final ActionListener latchListener = new LatchedActionListener<>(listener, latch); listener = ActionListener.wrap(r -> { - final Map> mappings = - r.mappings(); - final Map fieldMappings = - mappings.get("twitter"); + final Map> mappings = r.mappings(); + final Map fieldMappings = mappings.get("twitter"); final GetFieldMappingsResponse.FieldMappingMetadata metadata1 = fieldMappings.get("message"); final String fullName = metadata1.fullName(); @@ -770,10 +765,8 @@ public void onFailure(Exception e) { assertTrue(latch.await(30L, TimeUnit.SECONDS)); } - } - public void testOpenIndex() throws Exception { RestHighLevelClient client = highLevelClient(); @@ -1008,8 +1001,8 @@ public void testGetSettings() throws Exception { { Settings settings = Settings.builder().put("number_of_shards", 3).build(); - CreateIndexResponse createIndexResponse = client.indices().create( - new CreateIndexRequest("index").settings(settings), RequestOptions.DEFAULT); + CreateIndexResponse createIndexResponse = client.indices() + .create(new CreateIndexRequest("index").settings(settings), RequestOptions.DEFAULT); assertTrue(createIndexResponse.isAcknowledged()); } @@ -1039,8 +1032,7 @@ public void testGetSettings() throws Exception { assertEquals("3", numberOfShardsString); assertEquals(Integer.valueOf(3), numberOfShards); - assertNull("refresh_interval returned but was never set!", - getSettingsResponse.getSetting("index", "index.refresh_interval")); + assertNull("refresh_interval returned but was never set!", getSettingsResponse.getSetting("index", "index.refresh_interval")); // tag::get-settings-execute-listener ActionListener listener = @@ -1073,8 +1065,8 @@ public void testGetSettingsWithDefaults() throws Exception { { Settings settings = Settings.builder().put("number_of_shards", 3).build(); - CreateIndexResponse createIndexResponse = client.indices().create( - new CreateIndexRequest("index").settings(settings), RequestOptions.DEFAULT); + CreateIndexResponse createIndexResponse = client.indices() + .create(new CreateIndexRequest("index").settings(settings), RequestOptions.DEFAULT); assertTrue(createIndexResponse.isAcknowledged()); } @@ -1099,16 +1091,13 @@ public void testGetSettingsWithDefaults() throws Exception { assertNotNull("with defaults enabled we should get a value for refresh_interval!", refreshInterval); assertEquals(refreshInterval, indexDefaultSettings.get("index.refresh_interval")); - ActionListener listener = - new ActionListener() { - @Override - public void onResponse(GetSettingsResponse GetSettingsResponse) { - } + ActionListener listener = new ActionListener() { + @Override + public void onResponse(GetSettingsResponse GetSettingsResponse) {} - @Override - public void onFailure(Exception e) { - } - }; + @Override + public void onFailure(Exception e) {} + }; // Replace the empty listener by a blocking listener in test final CountDownLatch latch = new CountDownLatch(1); @@ -1124,11 +1113,8 @@ public void testGetIndex() throws Exception { { Settings settings = Settings.builder().put("number_of_shards", 3).build(); String mappings = "{\"properties\":{\"field-1\":{\"type\":\"integer\"}}}"; - CreateIndexRequest createIndexRequest = new CreateIndexRequest("index") - .settings(settings) - .mapping(mappings, XContentType.JSON); - CreateIndexResponse createIndexResponse = client.indices().create( - createIndexRequest, RequestOptions.DEFAULT); + CreateIndexRequest createIndexRequest = new CreateIndexRequest("index").settings(settings).mapping(mappings, XContentType.JSON); + CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT); assertTrue(createIndexResponse.isAcknowledged()); } @@ -1161,8 +1147,7 @@ public void testGetIndex() throws Exception { // end::get-index-response assertEquals( - Collections.singletonMap("properties", - Collections.singletonMap("field-1", Collections.singletonMap("type", "integer"))), + Collections.singletonMap("properties", Collections.singletonMap("field-1", Collections.singletonMap("type", "integer"))), indexTypeMappings ); assertTrue(indexAliases.isEmpty()); @@ -1426,8 +1411,8 @@ public void testExistsAlias() throws Exception { RestHighLevelClient client = highLevelClient(); { - CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("index") - .alias(new Alias("alias")), RequestOptions.DEFAULT); + CreateIndexResponse createIndexResponse = client.indices() + .create(new CreateIndexRequest("index").alias(new Alias("alias")), RequestOptions.DEFAULT); assertTrue(createIndexResponse.isAcknowledged()); } @@ -1591,8 +1576,10 @@ public void testShrinkIndex() throws Exception { @SuppressWarnings("unchecked") String firstNode = ((Map) nodes.get("nodes")).keySet().iterator().next(); createIndex("source_index", Settings.builder().put("index.number_of_shards", 4).put("index.number_of_replicas", 0).build()); - updateIndexSettings("source_index", Settings.builder().put("index.routing.allocation.require._name", firstNode) - .put("index.blocks.write", true)); + updateIndexSettings( + "source_index", + Settings.builder().put("index.routing.allocation.require._name", firstNode).put("index.blocks.write", true) + ); } // tag::shrink-index-request @@ -1618,8 +1605,7 @@ public void testShrinkIndex() throws Exception { .putNull("index.routing.allocation.require._name")); // <2> // end::shrink-index-request-settings } else { - request.getTargetIndexRequest().settings(Settings.builder() - .putNull("index.routing.allocation.require._name")); + request.getTargetIndexRequest().settings(Settings.builder().putNull("index.routing.allocation.require._name")); // tag::shrink-index-request-maxPrimaryShardSize request.setMaxPrimaryShardSize(new ByteSizeValue(50, ByteSizeUnit.GB)); // <1> // end::shrink-index-request-maxPrimaryShardSize @@ -1668,8 +1654,14 @@ public void testSplitIndex() throws Exception { RestHighLevelClient client = highLevelClient(); { - createIndex("source_index", Settings.builder().put("index.number_of_shards", 2).put("index.number_of_replicas", 0) - .put("index.number_of_routing_shards", 4).build()); + createIndex( + "source_index", + Settings.builder() + .put("index.number_of_shards", 2) + .put("index.number_of_replicas", 0) + .put("index.number_of_routing_shards", 4) + .build() + ); updateIndexSettings("source_index", Settings.builder().put("index.blocks.write", true)); } @@ -1894,8 +1886,8 @@ public void testGetAlias() throws Exception { RestHighLevelClient client = highLevelClient(); { - CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("index").alias(new Alias("alias")), - RequestOptions.DEFAULT); + CreateIndexResponse createIndexResponse = client.indices() + .create(new CreateIndexRequest("index").alias(new Alias("alias")), RequestOptions.DEFAULT); assertTrue(createIndexResponse.isAcknowledged()); } @@ -2228,12 +2220,9 @@ public void onFailure(Exception e) { public void testGetTemplates() throws Exception { RestHighLevelClient client = highLevelClient(); { - PutIndexTemplateRequest putRequest = - new PutIndexTemplateRequest("my-template", List.of("pattern-1", "log-*")); + PutIndexTemplateRequest putRequest = new PutIndexTemplateRequest("my-template", List.of("pattern-1", "log-*")); putRequest.settings(Settings.builder().put("index.number_of_shards", 3).put("index.number_of_replicas", 1)); - putRequest.mapping("{ \"properties\": { \"message\": { \"type\": \"text\" } } }", - XContentType.JSON - ); + putRequest.mapping("{ \"properties\": { \"message\": { \"type\": \"text\" } } }", XContentType.JSON); assertTrue(client.indices().putTemplate(putRequest, LEGACY_TEMPLATE_OPTIONS).isAcknowledged()); } @@ -2288,14 +2277,13 @@ public void onFailure(Exception e) { public void testGetIndexTemplatesV2() throws Exception { RestHighLevelClient client = highLevelClient(); { - Template template = new Template(Settings.builder().put("index.number_of_shards", 3).put("index.number_of_replicas", 1).build(), + Template template = new Template( + Settings.builder().put("index.number_of_shards", 3).put("index.number_of_replicas", 1).build(), new CompressedXContent("{ \"properties\": { \"message\": { \"type\": \"text\" } } }"), - null); - PutComposableIndexTemplateRequest putRequest = new PutComposableIndexTemplateRequest() - .name("my-template") - .indexTemplate( - new ComposableIndexTemplate(List.of("pattern-1", "log-*"), template, null, null, null, null) - ); + null + ); + PutComposableIndexTemplateRequest putRequest = new PutComposableIndexTemplateRequest().name("my-template") + .indexTemplate(new ComposableIndexTemplate(List.of("pattern-1", "log-*"), template, null, null, null, null)); assertTrue(client.indices().putIndexTemplate(putRequest, RequestOptions.DEFAULT).isAcknowledged()); } @@ -2415,8 +2403,11 @@ public void testPutIndexTemplateV2() throws Exception { { Template template = new Template(Settings.builder().put("index.number_of_replicas", 3).build(), null, null); ComponentTemplate componentTemplate = new ComponentTemplate(template, null, null); - client.cluster().putComponentTemplate(new PutComponentTemplateRequest().name("ct1").componentTemplate(componentTemplate), - RequestOptions.DEFAULT); + client.cluster() + .putComponentTemplate( + new PutComponentTemplateRequest().name("ct1").componentTemplate(componentTemplate), + RequestOptions.DEFAULT + ); // tag::put-index-template-v2-request-component-template PutComposableIndexTemplateRequest request = new PutComposableIndexTemplateRequest() @@ -2500,10 +2491,15 @@ public void onFailure(Exception e) { public void testDeleteIndexTemplateV2() throws Exception { RestHighLevelClient client = highLevelClient(); { - PutComposableIndexTemplateRequest request = new PutComposableIndexTemplateRequest() - .name("my-template"); - ComposableIndexTemplate composableIndexTemplate = new ComposableIndexTemplate(List.of("pattern-1", "log-*"), - null, null, null, null, null); // <2> + PutComposableIndexTemplateRequest request = new PutComposableIndexTemplateRequest().name("my-template"); + ComposableIndexTemplate composableIndexTemplate = new ComposableIndexTemplate( + List.of("pattern-1", "log-*"), + null, + null, + null, + null, + null + ); // <2> request.indexTemplate(composableIndexTemplate); assertTrue(client.indices().putIndexTemplate(request, RequestOptions.DEFAULT).isAcknowledged()); } @@ -2526,10 +2522,15 @@ public void testDeleteIndexTemplateV2() throws Exception { assertThat(acknowledged, equalTo(true)); { - PutComposableIndexTemplateRequest request = new PutComposableIndexTemplateRequest() - .name("my-template"); - ComposableIndexTemplate composableIndexTemplate = new ComposableIndexTemplate(List.of("pattern-1", "log-*"), - null, null, null, null, null); // <2> + PutComposableIndexTemplateRequest request = new PutComposableIndexTemplateRequest().name("my-template"); + ComposableIndexTemplate composableIndexTemplate = new ComposableIndexTemplate( + List.of("pattern-1", "log-*"), + null, + null, + null, + null, + null + ); // <2> request.indexTemplate(composableIndexTemplate); assertTrue(client.indices().putIndexTemplate(request, RequestOptions.DEFAULT).isAcknowledged()); } @@ -2563,11 +2564,16 @@ public void testSimulateIndexTemplate() throws Exception { RestHighLevelClient client = highLevelClient(); { - PutComposableIndexTemplateRequest request = new PutComposableIndexTemplateRequest() - .name("my-template"); // <1> + PutComposableIndexTemplateRequest request = new PutComposableIndexTemplateRequest().name("my-template"); // <1> Template template = new Template(Settings.builder().put("index.number_of_replicas", 3).build(), null, null); - ComposableIndexTemplate composableIndexTemplate = new ComposableIndexTemplate(List.of("pattern-1", "log-*"), - template, null, null, null, null); + ComposableIndexTemplate composableIndexTemplate = new ComposableIndexTemplate( + List.of("pattern-1", "log-*"), + template, + null, + null, + null, + null + ); request.indexTemplate(composableIndexTemplate); assertTrue(client.indices().putIndexTemplate(request, RequestOptions.DEFAULT).isAcknowledged()); } @@ -2621,8 +2627,7 @@ public void onFailure(Exception e) { public void testTemplatesExist() throws Exception { final RestHighLevelClient client = highLevelClient(); { - final PutIndexTemplateRequest putRequest = new PutIndexTemplateRequest("my-template", - List.of("foo")); + final PutIndexTemplateRequest putRequest = new PutIndexTemplateRequest("my-template", List.of("foo")); assertTrue(client.indices().putTemplate(putRequest, LEGACY_TEMPLATE_OPTIONS).isAcknowledged()); } @@ -2806,15 +2811,17 @@ public void testAnalyze() throws IOException, InterruptedException { CreateIndexResponse resp = client.indices().create(req, RequestOptions.DEFAULT); assertTrue(resp.isAcknowledged()); - PutMappingRequest pmReq = new PutMappingRequest("my_index") - .source(XContentFactory.jsonBuilder().startObject() + PutMappingRequest pmReq = new PutMappingRequest("my_index").source( + XContentFactory.jsonBuilder() + .startObject() .startObject("properties") - .startObject("my_field") - .field("type", "text") - .field("analyzer", "english") - .endObject() + .startObject("my_field") + .field("type", "text") + .field("analyzer", "english") + .endObject() + .endObject() .endObject() - .endObject()); + ); AcknowledgedResponse pmResp = client.indices().putMapping(pmReq, RequestOptions.DEFAULT); assertTrue(pmResp.isAcknowledged()); @@ -2957,8 +2964,7 @@ public void onFailure(Exception e) { public void testDeleteTemplate() throws Exception { RestHighLevelClient client = highLevelClient(); { - PutIndexTemplateRequest putRequest = new PutIndexTemplateRequest("my-template", - List.of("pattern-1", "log-*")); + PutIndexTemplateRequest putRequest = new PutIndexTemplateRequest("my-template", List.of("pattern-1", "log-*")); putRequest.settings(Settings.builder().put("index.number_of_shards", 3)); assertTrue(client.indices().putTemplate(putRequest, LEGACY_TEMPLATE_OPTIONS).isAcknowledged()); } @@ -2983,8 +2989,7 @@ public void testDeleteTemplate() throws Exception { assertThat(acknowledged, equalTo(true)); { - PutIndexTemplateRequest putRequest = new PutIndexTemplateRequest("my-template", - List.of("pattern-1", "log-*")); + PutIndexTemplateRequest putRequest = new PutIndexTemplateRequest("my-template", List.of("pattern-1", "log-*")); putRequest.settings(Settings.builder().put("index.number_of_shards", 3)); assertTrue(client.indices().putTemplate(putRequest, LEGACY_TEMPLATE_OPTIONS).isAcknowledged()); } @@ -3097,24 +3102,16 @@ public void testDeleteAlias() throws Exception { } { IndicesAliasesRequest request = new IndicesAliasesRequest(); - AliasActions aliasAction = - new AliasActions(AliasActions.Type.ADD) - .index("index1") - .alias("alias1"); + AliasActions aliasAction = new AliasActions(AliasActions.Type.ADD).index("index1").alias("alias1"); request.addAliasAction(aliasAction); - AcknowledgedResponse indicesAliasesResponse = - client.indices().updateAliases(request, RequestOptions.DEFAULT); + AcknowledgedResponse indicesAliasesResponse = client.indices().updateAliases(request, RequestOptions.DEFAULT); assertTrue(indicesAliasesResponse.isAcknowledged()); } { IndicesAliasesRequest request = new IndicesAliasesRequest(); - AliasActions aliasAction = - new AliasActions(AliasActions.Type.ADD) - .index("index1") - .alias("alias2"); + AliasActions aliasAction = new AliasActions(AliasActions.Type.ADD).index("index1").alias("alias2"); request.addAliasAction(aliasAction); - AcknowledgedResponse indicesAliasesResponse = - client.indices().updateAliases(request, RequestOptions.DEFAULT); + AcknowledgedResponse indicesAliasesResponse = client.indices().updateAliases(request, RequestOptions.DEFAULT); assertTrue(indicesAliasesResponse.isAcknowledged()); } { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IngestClientDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IngestClientDocumentationIT.java index d7ba8113f00b3..d252c88a6677c 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IngestClientDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IngestClientDocumentationIT.java @@ -26,8 +26,8 @@ import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.ingest.PipelineConfiguration; +import org.elasticsearch.xcontent.XContentType; import java.io.IOException; import java.nio.charset.StandardCharsets; @@ -97,9 +97,8 @@ public void testPutPipelineAsync() throws Exception { RestHighLevelClient client = highLevelClient(); { - String source = - "{\"description\":\"my set of processors\"," + - "\"processors\":[{\"set\":{\"field\":\"foo\",\"value\":\"bar\"}}]}"; + String source = "{\"description\":\"my set of processors\"," + + "\"processors\":[{\"set\":{\"field\":\"foo\",\"value\":\"bar\"}}]}"; PutPipelineRequest request = new PutPipelineRequest( "my-pipeline-id", new BytesArray(source.getBytes(StandardCharsets.UTF_8)), @@ -328,7 +327,7 @@ public void testSimulatePipeline() throws IOException { } } // end::simulate-pipeline-response - assert(response.getResults().size() > 0); + assert (response.getResults().size() > 0); } } @@ -336,17 +335,16 @@ public void testSimulatePipelineAsync() throws Exception { RestHighLevelClient client = highLevelClient(); { - String source = - "{\"" + - "pipeline\":{" + - "\"description\":\"_description\"," + - "\"processors\":[{\"set\":{\"field\":\"field2\",\"value\":\"_value\"}}]" + - "}," + - "\"docs\":[" + - "{\"_index\":\"index\",\"_id\":\"id\",\"_source\":{\"foo\":\"bar\"}}," + - "{\"_index\":\"index\",\"_id\":\"id\",\"_source\":{\"foo\":\"rab\"}}" + - "]" + - "}"; + String source = "{\"" + + "pipeline\":{" + + "\"description\":\"_description\"," + + "\"processors\":[{\"set\":{\"field\":\"field2\",\"value\":\"_value\"}}]" + + "}," + + "\"docs\":[" + + "{\"_index\":\"index\",\"_id\":\"id\",\"_source\":{\"foo\":\"bar\"}}," + + "{\"_index\":\"index\",\"_id\":\"id\",\"_source\":{\"foo\":\"rab\"}}" + + "]" + + "}"; SimulatePipelineRequest request = new SimulatePipelineRequest( new BytesArray(source.getBytes(StandardCharsets.UTF_8)), XContentType.JSON diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/LicensingDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/LicensingDocumentationIT.java index f6a39abfe2b14..9b1c930d30744 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/LicensingDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/LicensingDocumentationIT.java @@ -15,21 +15,21 @@ import org.elasticsearch.client.ESRestHighLevelClientTestCase; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; -import org.elasticsearch.client.license.StartTrialRequest; -import org.elasticsearch.client.license.StartTrialResponse; -import org.elasticsearch.client.license.StartBasicRequest; -import org.elasticsearch.client.license.StartBasicResponse; -import org.elasticsearch.client.license.GetBasicStatusResponse; -import org.elasticsearch.client.license.GetTrialStatusResponse; -import org.elasticsearch.core.Booleans; -import org.junit.After; -import org.junit.BeforeClass; import org.elasticsearch.client.license.DeleteLicenseRequest; +import org.elasticsearch.client.license.GetBasicStatusResponse; import org.elasticsearch.client.license.GetLicenseRequest; import org.elasticsearch.client.license.GetLicenseResponse; +import org.elasticsearch.client.license.GetTrialStatusResponse; import org.elasticsearch.client.license.LicensesStatus; import org.elasticsearch.client.license.PutLicenseRequest; import org.elasticsearch.client.license.PutLicenseResponse; +import org.elasticsearch.client.license.StartBasicRequest; +import org.elasticsearch.client.license.StartBasicResponse; +import org.elasticsearch.client.license.StartTrialRequest; +import org.elasticsearch.client.license.StartTrialResponse; +import org.elasticsearch.core.Booleans; +import org.junit.After; +import org.junit.BeforeClass; import java.io.IOException; import java.util.Map; @@ -54,8 +54,7 @@ public class LicensingDocumentationIT extends ESRestHighLevelClientTestCase { @BeforeClass public static void checkForSnapshot() { - assumeTrue("Trial license used to rollback is only valid when tested against snapshot/test builds", - Build.CURRENT.isSnapshot()); + assumeTrue("Trial license used to rollback is only valid when tested against snapshot/test builds", Build.CURRENT.isSnapshot()); } @After @@ -65,16 +64,16 @@ public void rollbackToTrial() throws IOException { public void testLicense() throws Exception { RestHighLevelClient client = highLevelClient(); - String license = "{\"license\": {\"uid\":\"893361dc-9749-4997-93cb-802e3d7fa4a8\",\"type\":\"gold\"," + - "\"issue_date_in_millis\":1411948800000,\"expiry_date_in_millis\":1914278399999,\"max_nodes\":1,\"issued_to\":\"issued_to\"," + - "\"issuer\":\"issuer\",\"signature\":\"AAAAAgAAAA3U8+YmnvwC+CWsV/mRAAABmC9ZN0hjZDBGYnVyRXpCOW5Bb3FjZDAxOWpSbTVoMVZwUzRxVk1PSm" + - "kxakxZdW5IMlhlTHNoN1N2MXMvRFk4d3JTZEx3R3RRZ0pzU3lobWJKZnQvSEFva0ppTHBkWkprZWZSQi9iNmRQNkw1SlpLN0lDalZCS095MXRGN1lIZlpYcVVTTn" + - "FrcTE2dzhJZmZrdFQrN3JQeGwxb0U0MXZ0dDJHSERiZTVLOHNzSDByWnpoZEphZHBEZjUrTVBxRENNSXNsWWJjZllaODdzVmEzUjNiWktNWGM5TUhQV2plaUo4Q1" + - "JOUml4MXNuL0pSOEhQaVB2azhmUk9QVzhFeTFoM1Q0RnJXSG53MWk2K055c28zSmRnVkF1b2JSQkFLV2VXUmVHNDZ2R3o2VE1qbVNQS2lxOHN5bUErZlNIWkZSVm" + - "ZIWEtaSU9wTTJENDVvT1NCYklacUYyK2FwRW9xa0t6dldMbmMzSGtQc3FWOTgzZ3ZUcXMvQkt2RUZwMFJnZzlvL2d2bDRWUzh6UG5pdENGWFRreXNKNkE9PQAAAQ" + - "Be8GfzDm6T537Iuuvjetb3xK5dvg0K5NQapv+rczWcQFxgCuzbF8plkgetP1aAGZP4uRESDQPMlOCsx4d0UqqAm9f7GbBQ3l93P+PogInPFeEH9NvOmaAQovmxVM" + - "9SE6DsDqlX4cXSO+bgWpXPTd2LmpoQc1fXd6BZ8GeuyYpVHVKp9hVU0tAYjw6HzYOE7+zuO1oJYOxElqy66AnIfkvHrvni+flym3tE7tDTgsDRaz7W3iBhaqiSnt" + - "EqabEkvHdPHQdSR99XGaEvnHO1paK01/35iZF6OXHsF7CCj+558GRXiVxzueOe7TsGSSt8g7YjZwV9bRCyU7oB4B/nidgI\"}}"; + String license = "{\"license\": {\"uid\":\"893361dc-9749-4997-93cb-802e3d7fa4a8\",\"type\":\"gold\"," + + "\"issue_date_in_millis\":1411948800000,\"expiry_date_in_millis\":1914278399999,\"max_nodes\":1,\"issued_to\":\"issued_to\"," + + "\"issuer\":\"issuer\",\"signature\":\"AAAAAgAAAA3U8+YmnvwC+CWsV/mRAAABmC9ZN0hjZDBGYnVyRXpCOW5Bb3FjZDAxOWpSbTVoMVZwUzRxVk1PSm" + + "kxakxZdW5IMlhlTHNoN1N2MXMvRFk4d3JTZEx3R3RRZ0pzU3lobWJKZnQvSEFva0ppTHBkWkprZWZSQi9iNmRQNkw1SlpLN0lDalZCS095MXRGN1lIZlpYcVVTTn" + + "FrcTE2dzhJZmZrdFQrN3JQeGwxb0U0MXZ0dDJHSERiZTVLOHNzSDByWnpoZEphZHBEZjUrTVBxRENNSXNsWWJjZllaODdzVmEzUjNiWktNWGM5TUhQV2plaUo4Q1" + + "JOUml4MXNuL0pSOEhQaVB2azhmUk9QVzhFeTFoM1Q0RnJXSG53MWk2K055c28zSmRnVkF1b2JSQkFLV2VXUmVHNDZ2R3o2VE1qbVNQS2lxOHN5bUErZlNIWkZSVm" + + "ZIWEtaSU9wTTJENDVvT1NCYklacUYyK2FwRW9xa0t6dldMbmMzSGtQc3FWOTgzZ3ZUcXMvQkt2RUZwMFJnZzlvL2d2bDRWUzh6UG5pdENGWFRreXNKNkE9PQAAAQ" + + "Be8GfzDm6T537Iuuvjetb3xK5dvg0K5NQapv+rczWcQFxgCuzbF8plkgetP1aAGZP4uRESDQPMlOCsx4d0UqqAm9f7GbBQ3l93P+PogInPFeEH9NvOmaAQovmxVM" + + "9SE6DsDqlX4cXSO+bgWpXPTd2LmpoQc1fXd6BZ8GeuyYpVHVKp9hVU0tAYjw6HzYOE7+zuO1oJYOxElqy66AnIfkvHrvni+flym3tE7tDTgsDRaz7W3iBhaqiSnt" + + "EqabEkvHdPHQdSR99XGaEvnHO1paK01/35iZF6OXHsF7CCj+558GRXiVxzueOe7TsGSSt8g7YjZwV9bRCyU7oB4B/nidgI\"}}"; { //tag::put-license-execute PutLicenseRequest request = new PutLicenseRequest(); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/MigrationDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/MigrationDocumentationIT.java index 9fce69bafca37..16623f062dc0d 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/MigrationDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/MigrationDocumentationIT.java @@ -21,8 +21,8 @@ import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.cluster.health.ClusterHealthStatus; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.xcontent.XContentType; import java.io.IOException; import java.io.InputStream; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/MlClientDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/MlClientDocumentationIT.java index b41e40122c3fa..98b1001a5b2d8 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/MlClientDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/MlClientDocumentationIT.java @@ -198,15 +198,15 @@ import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.query.MatchAllQueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.aggregations.AggregatorFactories; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.fetch.subphase.FetchSourceContext; import org.elasticsearch.tasks.TaskId; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentType; import org.junit.After; import java.io.IOException; @@ -239,8 +239,13 @@ public class MlClientDocumentationIT extends ESRestHighLevelClientTestCase { private static final RequestOptions POST_DATA_OPTIONS = RequestOptions.DEFAULT.toBuilder() - .setWarningsHandler(warnings -> Collections.singletonList("Posting data directly to anomaly detection jobs is deprecated, " + - "in a future major version it will be compulsory to use a datafeed").equals(warnings) == false).build(); + .setWarningsHandler( + warnings -> Collections.singletonList( + "Posting data directly to anomaly detection jobs is deprecated, " + + "in a future major version it will be compulsory to use a datafeed" + ).equals(warnings) == false + ) + .build(); @After public void cleanUp() throws IOException { @@ -293,8 +298,7 @@ public void testCreateJob() throws Exception { } { String id = "job_2"; - Job.Builder jobBuilder = new Job.Builder(id) - .setAnalysisConfig(analysisConfigBuilder) + Job.Builder jobBuilder = new Job.Builder(id).setAnalysisConfig(analysisConfigBuilder) .setDataDescription(dataDescriptionBuilder) .setDescription("Total sum of requests"); @@ -351,8 +355,10 @@ public void testGetJob() throws Exception { // end::get-job-response assertEquals(2, response.count()); assertThat(response.jobs(), hasSize(2)); - assertThat(response.jobs().stream().map(Job::getId).collect(Collectors.toList()), - containsInAnyOrder(job.getId(), secondJob.getId())); + assertThat( + response.jobs().stream().map(Job::getId).collect(Collectors.toList()), + containsInAnyOrder(job.getId(), secondJob.getId()) + ); } { GetJobRequest request = new GetJobRequest("get-machine-learning-job1", "get-machine-learning-job*"); @@ -560,22 +566,24 @@ public void testUpdateJob() throws Exception { RestHighLevelClient client = highLevelClient(); String jobId = "test-update-job"; Job tempJob = MachineLearningIT.buildJob(jobId); - Job job = new Job.Builder(tempJob) - .setAnalysisConfig(new AnalysisConfig.Builder(tempJob.getAnalysisConfig()) - .setCategorizationFieldName("categorization-field") - .setDetector(0, + Job job = new Job.Builder(tempJob).setAnalysisConfig( + new AnalysisConfig.Builder(tempJob.getAnalysisConfig()).setCategorizationFieldName("categorization-field") + .setDetector( + 0, new Detector.Builder().setFieldName("total") .setFunction("sum") .setPartitionFieldName("mlcategory") .setDetectorDescription(randomAlphaOfLength(10)) - .build())) - .build(); + .build() + ) + ).build(); client.machineLearning().putJob(new PutJobRequest(job), RequestOptions.DEFAULT); { List detectionRules = Arrays.asList( - new DetectionRule.Builder(Arrays.asList(RuleCondition.createTime(Operator.GT, 100L))).build()); + new DetectionRule.Builder(Arrays.asList(RuleCondition.createTime(Operator.GT, 100L))).build() + ); Map customSettings = new HashMap<>(); customSettings.put("custom-setting-1", "custom-value"); @@ -600,7 +608,6 @@ public void testUpdateJob() throws Exception { .build(); // end::update-job-options - // tag::update-job-request UpdateJobRequest updateJobRequest = new UpdateJobRequest(update); // <1> // end::update-job-request @@ -957,9 +964,7 @@ public void testPreviewDatafeed() throws Exception { String datafeedId = job.getId() + "-feed"; String indexName = "preview_data_2"; createIndex(indexName); - DatafeedConfig datafeed = DatafeedConfig.builder(datafeedId, job.getId()) - .setIndices(indexName) - .build(); + DatafeedConfig datafeed = DatafeedConfig.builder(datafeedId, job.getId()).setIndices(indexName).build(); client.machineLearning().putDatafeed(new PutDatafeedRequest(datafeed), RequestOptions.DEFAULT); { // tag::preview-datafeed-request @@ -1014,9 +1019,7 @@ public void testStartDatafeed() throws Exception { String datafeedId = job.getId() + "-feed"; String indexName = "start_data_2"; createIndex(indexName); - DatafeedConfig datafeed = DatafeedConfig.builder(datafeedId, job.getId()) - .setIndices(indexName) - .build(); + DatafeedConfig datafeed = DatafeedConfig.builder(datafeedId, job.getId()).setIndices(indexName).build(); client.machineLearning().putDatafeed(new PutDatafeedRequest(datafeed), RequestOptions.DEFAULT); client.machineLearning().openJob(new OpenJobRequest(job.getId()), RequestOptions.DEFAULT); { @@ -1135,15 +1138,11 @@ public void testGetDatafeedStats() throws Exception { String datafeedId1 = job.getId() + "-feed"; String indexName = "datafeed_stats_data_2"; createIndex(indexName); - DatafeedConfig datafeed = DatafeedConfig.builder(datafeedId1, job.getId()) - .setIndices(indexName) - .build(); + DatafeedConfig datafeed = DatafeedConfig.builder(datafeedId1, job.getId()).setIndices(indexName).build(); client.machineLearning().putDatafeed(new PutDatafeedRequest(datafeed), RequestOptions.DEFAULT); String datafeedId2 = secondJob.getId() + "-feed"; - DatafeedConfig secondDatafeed = DatafeedConfig.builder(datafeedId2, secondJob.getId()) - .setIndices(indexName) - .build(); + DatafeedConfig secondDatafeed = DatafeedConfig.builder(datafeedId2, secondJob.getId()).setIndices(indexName).build(); client.machineLearning().putDatafeed(new PutDatafeedRequest(secondDatafeed), RequestOptions.DEFAULT); { @@ -1164,8 +1163,10 @@ public void testGetDatafeedStats() throws Exception { assertEquals(2, response.count()); assertThat(response.datafeedStats(), hasSize(2)); - assertThat(response.datafeedStats().stream().map(DatafeedStats::getDatafeedId).collect(Collectors.toList()), - containsInAnyOrder(datafeed.getId(), secondDatafeed.getId())); + assertThat( + response.datafeedStats().stream().map(DatafeedStats::getDatafeedId).collect(Collectors.toList()), + containsInAnyOrder(datafeed.getId(), secondDatafeed.getId()) + ); } { GetDatafeedStatsRequest request = new GetDatafeedStatsRequest("*"); @@ -1206,8 +1207,11 @@ public void testGetBuckets() throws IOException, InterruptedException { // Let us index a bucket IndexRequest indexRequest = new IndexRequest(".ml-anomalies-shared"); indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - indexRequest.source("{\"job_id\":\"test-get-buckets\", \"result_type\":\"bucket\", \"timestamp\": 1533081600000," + - "\"bucket_span\": 600,\"is_interim\": false, \"anomaly_score\": 80.0}", XContentType.JSON); + indexRequest.source( + "{\"job_id\":\"test-get-buckets\", \"result_type\":\"bucket\", \"timestamp\": 1533081600000," + + "\"bucket_span\": 600,\"is_interim\": false, \"anomaly_score\": 80.0}", + XContentType.JSON + ); client.index(indexRequest, RequestOptions.DEFAULT); { @@ -1366,10 +1370,10 @@ public void testDeleteForecast() throws Exception { client.machineLearning().putJob(new PutJobRequest(job), RequestOptions.DEFAULT); client.machineLearning().openJob(new OpenJobRequest(job.getId()), RequestOptions.DEFAULT); PostDataRequest.JsonBuilder builder = new PostDataRequest.JsonBuilder(); - for(int i = 0; i < 30; i++) { + for (int i = 0; i < 30; i++) { Map hashMap = new HashMap<>(); hashMap.put("total", randomInt(1000)); - hashMap.put("timestamp", (i+1)*1000); + hashMap.put("timestamp", (i + 1) * 1000); builder.addDoc(hashMap); } @@ -1378,8 +1382,8 @@ public void testDeleteForecast() throws Exception { client.machineLearning().postData(postDataRequest, POST_DATA_OPTIONS); client.machineLearning().flushJob(new FlushJobRequest(job.getId()), RequestOptions.DEFAULT); - ForecastJobResponse forecastJobResponse = client.machineLearning(). - forecastJob(new ForecastJobRequest(job.getId()), RequestOptions.DEFAULT); + ForecastJobResponse forecastJobResponse = client.machineLearning() + .forecastJob(new ForecastJobRequest(job.getId()), RequestOptions.DEFAULT); String forecastId = forecastJobResponse.getForecastId(); GetRequest request = new GetRequest(".ml-anomalies-" + job.getId()); @@ -1465,8 +1469,10 @@ public void testGetJobStats() throws Exception { assertEquals(2, response.count()); assertThat(response.jobStats(), hasSize(2)); - assertThat(response.jobStats().stream().map(JobStats::getJobId).collect(Collectors.toList()), - containsInAnyOrder(job.getId(), secondJob.getId())); + assertThat( + response.jobStats().stream().map(JobStats::getJobId).collect(Collectors.toList()), + containsInAnyOrder(job.getId(), secondJob.getId()) + ); } { GetJobStatsRequest request = new GetJobStatsRequest("get-machine-learning-job-stats1", "get-machine-learning-job-*"); @@ -1505,10 +1511,10 @@ public void testForecastJob() throws Exception { client.machineLearning().openJob(new OpenJobRequest(job.getId()), RequestOptions.DEFAULT); PostDataRequest.JsonBuilder builder = new PostDataRequest.JsonBuilder(); - for(int i = 0; i < 30; i++) { + for (int i = 0; i < 30; i++) { Map hashMap = new HashMap<>(); hashMap.put("total", randomInt(1000)); - hashMap.put("timestamp", (i+1)*1000); + hashMap.put("timestamp", (i + 1) * 1000); builder.addDoc(hashMap); } PostDataRequest postDataRequest = new PostDataRequest(job.getId(), builder); @@ -1582,14 +1588,20 @@ public void testGetOverallBuckets() throws IOException, InterruptedException { { IndexRequest indexRequest = new IndexRequest(".ml-anomalies-shared"); - indexRequest.source("{\"job_id\":\"test-get-overall-buckets-1\", \"result_type\":\"bucket\", \"timestamp\": 1533081600000," + - "\"bucket_span\": 600,\"is_interim\": false, \"anomaly_score\": 60.0}", XContentType.JSON); + indexRequest.source( + "{\"job_id\":\"test-get-overall-buckets-1\", \"result_type\":\"bucket\", \"timestamp\": 1533081600000," + + "\"bucket_span\": 600,\"is_interim\": false, \"anomaly_score\": 60.0}", + XContentType.JSON + ); bulkRequest.add(indexRequest); } { IndexRequest indexRequest = new IndexRequest(".ml-anomalies-shared"); - indexRequest.source("{\"job_id\":\"test-get-overall-buckets-2\", \"result_type\":\"bucket\", \"timestamp\": 1533081600000," + - "\"bucket_span\": 3600,\"is_interim\": false, \"anomaly_score\": 100.0}", XContentType.JSON); + indexRequest.source( + "{\"job_id\":\"test-get-overall-buckets-2\", \"result_type\":\"bucket\", \"timestamp\": 1533081600000," + + "\"bucket_span\": 3600,\"is_interim\": false, \"anomaly_score\": 100.0}", + XContentType.JSON + ); bulkRequest.add(indexRequest); } @@ -1677,8 +1689,11 @@ public void testGetRecords() throws IOException, InterruptedException { // Let us index a record IndexRequest indexRequest = new IndexRequest(".ml-anomalies-shared"); indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - indexRequest.source("{\"job_id\":\"test-get-records\", \"result_type\":\"record\", \"timestamp\": 1533081600000," + - "\"bucket_span\": 600,\"is_interim\": false, \"record_score\": 80.0}", XContentType.JSON); + indexRequest.source( + "{\"job_id\":\"test-get-records\", \"result_type\":\"record\", \"timestamp\": 1533081600000," + + "\"bucket_span\": 600,\"is_interim\": false, \"record_score\": 80.0}", + XContentType.JSON + ); client.index(indexRequest, RequestOptions.DEFAULT); { @@ -1774,7 +1789,6 @@ public void testPostData() throws Exception { PostDataRequest postDataRequest = new PostDataRequest("test-post-data", jsonBuilder); // <4> // end::post-data-request - // tag::post-data-request-options postDataRequest.setResetStart("2018-08-31T16:35:07+00:00"); // <1> postDataRequest.setResetEnd("2018-08-31T16:35:17+00:00"); // <2> @@ -1844,9 +1858,12 @@ public void testGetInfluencers() throws IOException, InterruptedException { // Let us index a record IndexRequest indexRequest = new IndexRequest(".ml-anomalies-shared"); indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - indexRequest.source("{\"job_id\":\"test-get-influencers\", \"result_type\":\"influencer\", \"timestamp\": 1533081600000," + - "\"bucket_span\": 600,\"is_interim\": false, \"influencer_score\": 80.0, \"influencer_field_name\": \"my_influencer\"," + - "\"influencer_field_value\":\"foo\"}", XContentType.JSON); + indexRequest.source( + "{\"job_id\":\"test-get-influencers\", \"result_type\":\"influencer\", \"timestamp\": 1533081600000," + + "\"bucket_span\": 600,\"is_interim\": false, \"influencer_score\": 80.0, \"influencer_field_name\": \"my_influencer\"," + + "\"influencer_field_value\":\"foo\"}", + XContentType.JSON + ); client.index(indexRequest, RequestOptions.DEFAULT); { @@ -1935,8 +1952,11 @@ public void testGetCategories() throws IOException, InterruptedException { // Let us index a category IndexRequest indexRequest = new IndexRequest(".ml-anomalies-shared"); indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - indexRequest.source("{\"job_id\": \"test-get-categories\", \"category_id\": 1, \"terms\": \"AAL\"," + - " \"regex\": \".*?AAL.*\", \"max_matching_length\": 3, \"examples\": [\"AAL\"]}", XContentType.JSON); + indexRequest.source( + "{\"job_id\": \"test-get-categories\", \"category_id\": 1, \"terms\": \"AAL\"," + + " \"regex\": \".*?AAL.*\", \"max_matching_length\": 3, \"examples\": [\"AAL\"]}", + XContentType.JSON + ); client.index(indexRequest, RequestOptions.DEFAULT); { @@ -2000,7 +2020,7 @@ public void testDeleteExpiredData() throws IOException, InterruptedException { String jobId = "test-delete-expired-data"; MachineLearningIT.buildJob(jobId); - { + { // tag::delete-expired-data-request DeleteExpiredDataRequest request = new DeleteExpiredDataRequest( // <1> null, // <2> @@ -2049,7 +2069,6 @@ public void onFailure(Exception e) { } } - public void testDeleteModelSnapshot() throws IOException, InterruptedException { RestHighLevelClient client = highLevelClient(); @@ -2061,14 +2080,23 @@ public void testDeleteModelSnapshot() throws IOException, InterruptedException { // Let us index a snapshot IndexRequest indexRequest = new IndexRequest(".ml-anomalies-shared"); indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - indexRequest.source("{\"job_id\":\"" + jobId + "\", \"timestamp\":1541587919000, " + - "\"description\":\"State persisted due to job close at 2018-11-07T10:51:59+0000\", " + - "\"snapshot_id\":\"" + snapshotId + "\", \"snapshot_doc_count\":1, \"model_size_stats\":{" + - "\"job_id\":\"" + jobId + "\", \"result_type\":\"model_size_stats\",\"model_bytes\":51722, " + - "\"total_by_field_count\":3, \"total_over_field_count\":0, \"total_partition_field_count\":2," + - "\"bucket_allocation_failures_count\":0, \"memory_status\":\"ok\", \"log_time\":1541587919000, " + - "\"timestamp\":1519930800000}, \"latest_record_time_stamp\":1519931700000," + - "\"latest_result_time_stamp\":1519930800000, \"retain\":false}", XContentType.JSON); + indexRequest.source( + "{\"job_id\":\"" + + jobId + + "\", \"timestamp\":1541587919000, " + + "\"description\":\"State persisted due to job close at 2018-11-07T10:51:59+0000\", " + + "\"snapshot_id\":\"" + + snapshotId + + "\", \"snapshot_doc_count\":1, \"model_size_stats\":{" + + "\"job_id\":\"" + + jobId + + "\", \"result_type\":\"model_size_stats\",\"model_bytes\":51722, " + + "\"total_by_field_count\":3, \"total_over_field_count\":0, \"total_partition_field_count\":2," + + "\"bucket_allocation_failures_count\":0, \"memory_status\":\"ok\", \"log_time\":1541587919000, " + + "\"timestamp\":1519930800000}, \"latest_record_time_stamp\":1519931700000," + + "\"latest_result_time_stamp\":1519930800000, \"retain\":false}", + XContentType.JSON + ); { client.index(indexRequest, RequestOptions.DEFAULT); @@ -2127,14 +2155,17 @@ public void testGetModelSnapshots() throws IOException, InterruptedException { // Let us index a snapshot IndexRequest indexRequest = new IndexRequest(".ml-anomalies-shared"); indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - indexRequest.source("{\"job_id\":\"test-get-model-snapshots\", \"timestamp\":1541587919000, " + - "\"description\":\"State persisted due to job close at 2018-11-07T10:51:59+0000\", " + - "\"snapshot_id\":\"1541587919\", \"snapshot_doc_count\":1, \"model_size_stats\":{" + - "\"job_id\":\"test-get-model-snapshots\", \"result_type\":\"model_size_stats\",\"model_bytes\":51722, " + - "\"total_by_field_count\":3, \"total_over_field_count\":0, \"total_partition_field_count\":2," + - "\"bucket_allocation_failures_count\":0, \"memory_status\":\"ok\", \"log_time\":1541587919000, " + - "\"timestamp\":1519930800000}, \"latest_record_time_stamp\":1519931700000," + - "\"latest_result_time_stamp\":1519930800000, \"retain\":false}", XContentType.JSON); + indexRequest.source( + "{\"job_id\":\"test-get-model-snapshots\", \"timestamp\":1541587919000, " + + "\"description\":\"State persisted due to job close at 2018-11-07T10:51:59+0000\", " + + "\"snapshot_id\":\"1541587919\", \"snapshot_doc_count\":1, \"model_size_stats\":{" + + "\"job_id\":\"test-get-model-snapshots\", \"result_type\":\"model_size_stats\",\"model_bytes\":51722, " + + "\"total_by_field_count\":3, \"total_over_field_count\":0, \"total_partition_field_count\":2," + + "\"bucket_allocation_failures_count\":0, \"memory_status\":\"ok\", \"log_time\":1541587919000, " + + "\"timestamp\":1519930800000}, \"latest_record_time_stamp\":1519931700000," + + "\"latest_result_time_stamp\":1519930800000, \"retain\":false}", + XContentType.JSON + ); client.index(indexRequest, RequestOptions.DEFAULT); { @@ -2225,16 +2256,19 @@ public void testRevertModelSnapshot() throws IOException, InterruptedException { String documentId = jobId + "_model_snapshot_" + snapshotId; IndexRequest indexRequest = new IndexRequest(".ml-anomalies-shared").id(documentId); indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - indexRequest.source("{\"job_id\":\"test-revert-model-snapshot\", \"timestamp\":1541587919000, " + - "\"description\":\"State persisted due to job close at 2018-11-07T10:51:59+0000\", " + - "\"snapshot_id\":\"1541587919\", \"snapshot_doc_count\":1, \"model_size_stats\":{" + - "\"job_id\":\"test-revert-model-snapshot\", \"result_type\":\"model_size_stats\",\"model_bytes\":51722, " + - "\"total_by_field_count\":3, \"total_over_field_count\":0, \"total_partition_field_count\":2," + - "\"bucket_allocation_failures_count\":0, \"memory_status\":\"ok\", \"log_time\":1541587919000, " + - "\"timestamp\":1519930800000}, \"latest_record_time_stamp\":1519931700000," + - "\"latest_result_time_stamp\":1519930800000, \"retain\":false, " + - "\"quantiles\":{\"job_id\":\"test-revert-model-snapshot\", \"timestamp\":1541587919000, " + - "\"quantile_state\":\"state\"}}", XContentType.JSON); + indexRequest.source( + "{\"job_id\":\"test-revert-model-snapshot\", \"timestamp\":1541587919000, " + + "\"description\":\"State persisted due to job close at 2018-11-07T10:51:59+0000\", " + + "\"snapshot_id\":\"1541587919\", \"snapshot_doc_count\":1, \"model_size_stats\":{" + + "\"job_id\":\"test-revert-model-snapshot\", \"result_type\":\"model_size_stats\",\"model_bytes\":51722, " + + "\"total_by_field_count\":3, \"total_over_field_count\":0, \"total_partition_field_count\":2," + + "\"bucket_allocation_failures_count\":0, \"memory_status\":\"ok\", \"log_time\":1541587919000, " + + "\"timestamp\":1519930800000}, \"latest_record_time_stamp\":1519931700000," + + "\"latest_result_time_stamp\":1519930800000, \"retain\":false, " + + "\"quantiles\":{\"job_id\":\"test-revert-model-snapshot\", \"timestamp\":1541587919000, " + + "\"quantile_state\":\"state\"}}", + XContentType.JSON + ); client.index(indexRequest, RequestOptions.DEFAULT); { @@ -2300,16 +2334,19 @@ public void testUpgradeJobSnapshot() throws IOException, InterruptedException { String documentId = jobId + "_model_snapshot_" + snapshotId; IndexRequest indexRequest = new IndexRequest(".ml-anomalies-shared").id(documentId); indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - indexRequest.source("{\"job_id\":\"test-upgrade-job-model-snapshot\", \"timestamp\":1541587919000, " + - "\"description\":\"State persisted due to job close at 2018-11-07T10:51:59+0000\", " + - "\"snapshot_id\":\"1541587919\", \"snapshot_doc_count\":1, \"model_size_stats\":{" + - "\"job_id\":\"test-revert-model-snapshot\", \"result_type\":\"model_size_stats\",\"model_bytes\":51722, " + - "\"total_by_field_count\":3, \"total_over_field_count\":0, \"total_partition_field_count\":2," + - "\"bucket_allocation_failures_count\":0, \"memory_status\":\"ok\", \"log_time\":1541587919000, " + - "\"timestamp\":1519930800000}, \"latest_record_time_stamp\":1519931700000," + - "\"latest_result_time_stamp\":1519930800000, \"retain\":false, " + - "\"quantiles\":{\"job_id\":\"test-revert-model-snapshot\", \"timestamp\":1541587919000, " + - "\"quantile_state\":\"state\"}}", XContentType.JSON); + indexRequest.source( + "{\"job_id\":\"test-upgrade-job-model-snapshot\", \"timestamp\":1541587919000, " + + "\"description\":\"State persisted due to job close at 2018-11-07T10:51:59+0000\", " + + "\"snapshot_id\":\"1541587919\", \"snapshot_doc_count\":1, \"model_size_stats\":{" + + "\"job_id\":\"test-revert-model-snapshot\", \"result_type\":\"model_size_stats\",\"model_bytes\":51722, " + + "\"total_by_field_count\":3, \"total_over_field_count\":0, \"total_partition_field_count\":2," + + "\"bucket_allocation_failures_count\":0, \"memory_status\":\"ok\", \"log_time\":1541587919000, " + + "\"timestamp\":1519930800000}, \"latest_record_time_stamp\":1519931700000," + + "\"latest_result_time_stamp\":1519930800000, \"retain\":false, " + + "\"quantiles\":{\"job_id\":\"test-revert-model-snapshot\", \"timestamp\":1541587919000, " + + "\"quantile_state\":\"state\"}}", + XContentType.JSON + ); client.index(indexRequest, RequestOptions.DEFAULT); { @@ -2378,14 +2415,17 @@ public void testUpdateModelSnapshot() throws IOException, InterruptedException { // Let us index a snapshot IndexRequest indexRequest = new IndexRequest(".ml-anomalies-shared").id(documentId); indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - indexRequest.source("{\"job_id\":\"test-update-model-snapshot\", \"timestamp\":1541587919000, " + - "\"description\":\"State persisted due to job close at 2018-11-07T10:51:59+0000\", " + - "\"snapshot_id\":\"1541587919\", \"snapshot_doc_count\":1, \"model_size_stats\":{" + - "\"job_id\":\"test-update-model-snapshot\", \"result_type\":\"model_size_stats\",\"model_bytes\":51722, " + - "\"total_by_field_count\":3, \"total_over_field_count\":0, \"total_partition_field_count\":2," + - "\"bucket_allocation_failures_count\":0, \"memory_status\":\"ok\", \"log_time\":1541587919000, " + - "\"timestamp\":1519930800000}, \"latest_record_time_stamp\":1519931700000," + - "\"latest_result_time_stamp\":1519930800000, \"retain\":false}", XContentType.JSON); + indexRequest.source( + "{\"job_id\":\"test-update-model-snapshot\", \"timestamp\":1541587919000, " + + "\"description\":\"State persisted due to job close at 2018-11-07T10:51:59+0000\", " + + "\"snapshot_id\":\"1541587919\", \"snapshot_doc_count\":1, \"model_size_stats\":{" + + "\"job_id\":\"test-update-model-snapshot\", \"result_type\":\"model_size_stats\",\"model_bytes\":51722, " + + "\"total_by_field_count\":3, \"total_over_field_count\":0, \"total_partition_field_count\":2," + + "\"bucket_allocation_failures_count\":0, \"memory_status\":\"ok\", \"log_time\":1541587919000, " + + "\"timestamp\":1519930800000}, \"latest_record_time_stamp\":1519931700000," + + "\"latest_result_time_stamp\":1519930800000, \"retain\":false}", + XContentType.JSON + ); client.index(indexRequest, RequestOptions.DEFAULT); { @@ -2411,7 +2451,8 @@ public void testUpdateModelSnapshot() throws IOException, InterruptedException { // end::update-model-snapshot-response assertTrue(acknowledged); - assertEquals("My Snapshot", modelSnapshot.getDescription()); } + assertEquals("My Snapshot", modelSnapshot.getDescription()); + } { UpdateModelSnapshotRequest request = new UpdateModelSnapshotRequest(jobId, snapshotId); @@ -2539,9 +2580,7 @@ public void onFailure(Exception e) { public void testDeleteCalendarJob() throws IOException, InterruptedException { RestHighLevelClient client = highLevelClient(); - Calendar calendar = new Calendar("holidays", - Arrays.asList("job_1", "job_group_1", "job_2"), - "A calendar for public holidays"); + Calendar calendar = new Calendar("holidays", Arrays.asList("job_1", "job_group_1", "job_2"), "A calendar for public holidays"); PutCalendarRequest putRequest = new PutCalendarRequest(calendar); client.machineLearning().putCalendar(putRequest, RequestOptions.DEFAULT); { @@ -2831,16 +2870,16 @@ public void onFailure(Exception e) { public void testDeleteCalendarEvent() throws IOException, InterruptedException { RestHighLevelClient client = highLevelClient(); - Calendar calendar = new Calendar("holidays", - Arrays.asList("job_1", "job_group_1", "job_2"), - "A calendar for public holidays"); + Calendar calendar = new Calendar("holidays", Arrays.asList("job_1", "job_group_1", "job_2"), "A calendar for public holidays"); PutCalendarRequest putRequest = new PutCalendarRequest(calendar); client.machineLearning().putCalendar(putRequest, RequestOptions.DEFAULT); - List events = Arrays.asList(ScheduledEventTests.testInstance(calendar.getId(), null), - ScheduledEventTests.testInstance(calendar.getId(), null)); + List events = Arrays.asList( + ScheduledEventTests.testInstance(calendar.getId(), null), + ScheduledEventTests.testInstance(calendar.getId(), null) + ); client.machineLearning().postCalendarEvent(new PostCalendarEventRequest("holidays", events), RequestOptions.DEFAULT); - GetCalendarEventsResponse getCalendarEventsResponse = - client.machineLearning().getCalendarEvents(new GetCalendarEventsRequest("holidays"), RequestOptions.DEFAULT); + GetCalendarEventsResponse getCalendarEventsResponse = client.machineLearning() + .getCalendarEvents(new GetCalendarEventsRequest("holidays"), RequestOptions.DEFAULT); { // tag::delete-calendar-event-request @@ -2861,8 +2900,10 @@ public void testDeleteCalendarEvent() throws IOException, InterruptedException { assertThat(acknowledged, is(true)); } { - DeleteCalendarEventRequest request = new DeleteCalendarEventRequest("holidays", - getCalendarEventsResponse.events().get(1).getEventId()); + DeleteCalendarEventRequest request = new DeleteCalendarEventRequest( + "holidays", + getCalendarEventsResponse.events().get(1).getEventId() + ); // tag::delete-calendar-event-execute-listener ActionListener listener = @@ -3180,9 +3221,7 @@ public void testUpdateDataFrameAnalytics() throws Exception { assertThat(updatedConfig.getModelMemoryLimit(), is(equalTo(new ByteSizeValue(128, ByteSizeUnit.MB)))); } { - DataFrameAnalyticsConfigUpdate update = DataFrameAnalyticsConfigUpdate.builder() - .setId("my-analytics-config") - .build(); + DataFrameAnalyticsConfigUpdate update = DataFrameAnalyticsConfigUpdate.builder().setId("my-analytics-config").build(); UpdateDataFrameAnalyticsRequest request = new UpdateDataFrameAnalyticsRequest(update); // tag::update-data-frame-analytics-execute-listener @@ -3270,7 +3309,9 @@ public void testStartDataFrameAnalytics() throws Exception { createIndex(DF_ANALYTICS_CONFIG.getSource().getIndex()[0]); highLevelClient().index( new IndexRequest(DF_ANALYTICS_CONFIG.getSource().getIndex()[0]).source(XContentType.JSON, "total", 10000) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT); + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), + RequestOptions.DEFAULT + ); RestHighLevelClient client = highLevelClient(); client.machineLearning().putDataFrameAnalytics(new PutDataFrameAnalyticsRequest(DF_ANALYTICS_CONFIG), RequestOptions.DEFAULT); { @@ -3292,7 +3333,9 @@ public void testStartDataFrameAnalytics() throws Exception { } assertBusy( () -> assertThat(getAnalyticsState(DF_ANALYTICS_CONFIG.getId()), equalTo(DataFrameAnalyticsState.STOPPED)), - 30, TimeUnit.SECONDS); + 30, + TimeUnit.SECONDS + ); { StartDataFrameAnalyticsRequest request = new StartDataFrameAnalyticsRequest("my-analytics-config"); @@ -3322,14 +3365,18 @@ public void onFailure(Exception e) { } assertBusy( () -> assertThat(getAnalyticsState(DF_ANALYTICS_CONFIG.getId()), equalTo(DataFrameAnalyticsState.STOPPED)), - 30, TimeUnit.SECONDS); + 30, + TimeUnit.SECONDS + ); } public void testStopDataFrameAnalytics() throws Exception { createIndex(DF_ANALYTICS_CONFIG.getSource().getIndex()[0]); highLevelClient().index( new IndexRequest(DF_ANALYTICS_CONFIG.getSource().getIndex()[0]).source(XContentType.JSON, "total", 10000) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT); + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), + RequestOptions.DEFAULT + ); RestHighLevelClient client = highLevelClient(); client.machineLearning().putDataFrameAnalytics(new PutDataFrameAnalyticsRequest(DF_ANALYTICS_CONFIG), RequestOptions.DEFAULT); { @@ -3350,7 +3397,9 @@ public void testStopDataFrameAnalytics() throws Exception { } assertBusy( () -> assertThat(getAnalyticsState(DF_ANALYTICS_CONFIG.getId()), equalTo(DataFrameAnalyticsState.STOPPED)), - 30, TimeUnit.SECONDS); + 30, + TimeUnit.SECONDS + ); { StopDataFrameAnalyticsRequest request = new StopDataFrameAnalyticsRequest("my-analytics-config"); @@ -3380,36 +3429,37 @@ public void onFailure(Exception e) { } assertBusy( () -> assertThat(getAnalyticsState(DF_ANALYTICS_CONFIG.getId()), equalTo(DataFrameAnalyticsState.STOPPED)), - 30, TimeUnit.SECONDS); + 30, + TimeUnit.SECONDS + ); } public void testEvaluateDataFrame_OutlierDetection() throws Exception { String indexName = "evaluate-test-index"; - CreateIndexRequest createIndexRequest = - new CreateIndexRequest(indexName) - .mapping(XContentFactory.jsonBuilder().startObject() - .startObject("properties") - .startObject("label") - .field("type", "keyword") - .endObject() - .startObject("p") - .field("type", "double") - .endObject() - .endObject() - .endObject()); - BulkRequest bulkRequest = - new BulkRequest(indexName) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .add(new IndexRequest().source(XContentType.JSON, "dataset", "blue", "label", false, "p", 0.1)) // #0 - .add(new IndexRequest().source(XContentType.JSON, "dataset", "blue", "label", false, "p", 0.2)) // #1 - .add(new IndexRequest().source(XContentType.JSON, "dataset", "blue", "label", false, "p", 0.3)) // #2 - .add(new IndexRequest().source(XContentType.JSON, "dataset", "blue", "label", false, "p", 0.4)) // #3 - .add(new IndexRequest().source(XContentType.JSON, "dataset", "blue", "label", false, "p", 0.7)) // #4 - .add(new IndexRequest().source(XContentType.JSON, "dataset", "blue", "label", true, "p", 0.2)) // #5 - .add(new IndexRequest().source(XContentType.JSON, "dataset", "blue", "label", true, "p", 0.3)) // #6 - .add(new IndexRequest().source(XContentType.JSON, "dataset", "blue", "label", true, "p", 0.4)) // #7 - .add(new IndexRequest().source(XContentType.JSON, "dataset", "blue", "label", true, "p", 0.8)) // #8 - .add(new IndexRequest().source(XContentType.JSON, "dataset", "blue", "label", true, "p", 0.9)); // #9 + CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName).mapping( + XContentFactory.jsonBuilder() + .startObject() + .startObject("properties") + .startObject("label") + .field("type", "keyword") + .endObject() + .startObject("p") + .field("type", "double") + .endObject() + .endObject() + .endObject() + ); + BulkRequest bulkRequest = new BulkRequest(indexName).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + .add(new IndexRequest().source(XContentType.JSON, "dataset", "blue", "label", false, "p", 0.1)) // #0 + .add(new IndexRequest().source(XContentType.JSON, "dataset", "blue", "label", false, "p", 0.2)) // #1 + .add(new IndexRequest().source(XContentType.JSON, "dataset", "blue", "label", false, "p", 0.3)) // #2 + .add(new IndexRequest().source(XContentType.JSON, "dataset", "blue", "label", false, "p", 0.4)) // #3 + .add(new IndexRequest().source(XContentType.JSON, "dataset", "blue", "label", false, "p", 0.7)) // #4 + .add(new IndexRequest().source(XContentType.JSON, "dataset", "blue", "label", true, "p", 0.2)) // #5 + .add(new IndexRequest().source(XContentType.JSON, "dataset", "blue", "label", true, "p", 0.3)) // #6 + .add(new IndexRequest().source(XContentType.JSON, "dataset", "blue", "label", true, "p", 0.4)) // #7 + .add(new IndexRequest().source(XContentType.JSON, "dataset", "blue", "label", true, "p", 0.8)) // #8 + .add(new IndexRequest().source(XContentType.JSON, "dataset", "blue", "label", true, "p", 0.9)); // #9 RestHighLevelClient client = highLevelClient(); client.indices().create(createIndexRequest, RequestOptions.DEFAULT); client.bulk(bulkRequest, RequestOptions.DEFAULT); @@ -3457,7 +3507,9 @@ public void testEvaluateDataFrame_OutlierDetection() throws Exception { org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.PrecisionMetric.NAME, org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.RecallMetric.NAME, ConfusionMatrixMetric.NAME, - org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.AucRocMetric.NAME)); + org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.AucRocMetric.NAME + ) + ); assertThat(precision, closeTo(0.6, 1e-9)); assertThat(confusionMatrix.getTruePositives(), equalTo(2L)); // docs #8 and #9 assertThat(confusionMatrix.getFalsePositives(), equalTo(1L)); // doc #4 @@ -3474,7 +3526,9 @@ public void testEvaluateDataFrame_OutlierDetection() throws Exception { org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.PrecisionMetric.at(0.4, 0.5, 0.6), org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.RecallMetric.at(0.5, 0.7), ConfusionMatrixMetric.at(0.5), - org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.AucRocMetric.withCurve())); + org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.AucRocMetric.withCurve() + ) + ); // tag::evaluate-data-frame-execute-listener ActionListener listener = new ActionListener<>() { @@ -3504,45 +3558,48 @@ public void onFailure(Exception e) { public void testEvaluateDataFrame_Classification() throws Exception { String indexName = "evaluate-classification-test-index"; - CreateIndexRequest createIndexRequest = - new CreateIndexRequest(indexName) - .mapping(XContentFactory.jsonBuilder().startObject() - .startObject("properties") - .startObject("actual_class") - .field("type", "keyword") - .endObject() - .startObject("predicted_class") - .field("type", "keyword") - .endObject() - .startObject("ml.top_classes") - .field("type", "nested") - .endObject() - .endObject() - .endObject()); + CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName).mapping( + XContentFactory.jsonBuilder() + .startObject() + .startObject("properties") + .startObject("actual_class") + .field("type", "keyword") + .endObject() + .startObject("predicted_class") + .field("type", "keyword") + .endObject() + .startObject("ml.top_classes") + .field("type", "nested") + .endObject() + .endObject() + .endObject() + ); BiFunction indexRequest = (actualClass, topPredictedClasses) -> { assert topPredictedClasses.length > 0; - return new IndexRequest() - .source(XContentType.JSON, - "actual_class", actualClass, - "predicted_class", topPredictedClasses[0], - "ml.top_classes", IntStream.range(0, topPredictedClasses.length) - // Consecutive assigned probabilities are: 0.5, 0.25, 0.125, etc. - .mapToObj(i -> Map.of("class_name", topPredictedClasses[i], "class_probability", 1.0 / (2 << i))) - .collect(toList())); + return new IndexRequest().source( + XContentType.JSON, + "actual_class", + actualClass, + "predicted_class", + topPredictedClasses[0], + "ml.top_classes", + IntStream.range(0, topPredictedClasses.length) + // Consecutive assigned probabilities are: 0.5, 0.25, 0.125, etc. + .mapToObj(i -> Map.of("class_name", topPredictedClasses[i], "class_probability", 1.0 / (2 << i))) + .collect(toList()) + ); }; - BulkRequest bulkRequest = - new BulkRequest(indexName) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .add(indexRequest.apply("cat", new String[]{"cat", "dog", "ant"})) // #0 - .add(indexRequest.apply("cat", new String[]{"cat", "dog", "ant"})) // #1 - .add(indexRequest.apply("cat", new String[]{"cat", "horse", "dog"})) // #2 - .add(indexRequest.apply("cat", new String[]{"dog", "cat", "mule"})) // #3 - .add(indexRequest.apply("cat", new String[]{"fox", "cat", "dog"})) // #4 - .add(indexRequest.apply("dog", new String[]{"cat", "dog", "mule"})) // #5 - .add(indexRequest.apply("dog", new String[]{"dog", "cat", "ant"})) // #6 - .add(indexRequest.apply("dog", new String[]{"dog", "cat", "ant"})) // #7 - .add(indexRequest.apply("dog", new String[]{"dog", "cat", "ant"})) // #8 - .add(indexRequest.apply("ant", new String[]{"cat", "ant", "wasp"})); // #9 + BulkRequest bulkRequest = new BulkRequest(indexName).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + .add(indexRequest.apply("cat", new String[] { "cat", "dog", "ant" })) // #0 + .add(indexRequest.apply("cat", new String[] { "cat", "dog", "ant" })) // #1 + .add(indexRequest.apply("cat", new String[] { "cat", "horse", "dog" })) // #2 + .add(indexRequest.apply("cat", new String[] { "dog", "cat", "mule" })) // #3 + .add(indexRequest.apply("cat", new String[] { "fox", "cat", "dog" })) // #4 + .add(indexRequest.apply("dog", new String[] { "cat", "dog", "mule" })) // #5 + .add(indexRequest.apply("dog", new String[] { "dog", "cat", "ant" })) // #6 + .add(indexRequest.apply("dog", new String[] { "dog", "cat", "ant" })) // #7 + .add(indexRequest.apply("dog", new String[] { "dog", "cat", "ant" })) // #8 + .add(indexRequest.apply("ant", new String[] { "cat", "ant", "wasp" })); // #9 RestHighLevelClient client = highLevelClient(); client.indices().create(createIndexRequest, RequestOptions.DEFAULT); client.bulk(bulkRequest, RequestOptions.DEFAULT); @@ -3602,17 +3659,23 @@ public void testEvaluateDataFrame_Classification() throws Exception { "ant", 1L, List.of(new PredictedClass("ant", 0L), new PredictedClass("cat", 1L), new PredictedClass("dog", 0L)), - 0L), + 0L + ), new ActualClass( "cat", 5L, List.of(new PredictedClass("ant", 0L), new PredictedClass("cat", 3L), new PredictedClass("dog", 1L)), - 1L), + 1L + ), new ActualClass( "dog", 4L, List.of(new PredictedClass("ant", 0L), new PredictedClass("cat", 1L), new PredictedClass("dog", 3L)), - 0L)))); + 0L + ) + ) + ) + ); assertThat(otherClassesCount, equalTo(0L)); assertThat(aucRocResult.getMetricName(), equalTo(AucRocMetric.NAME)); @@ -3622,31 +3685,30 @@ public void testEvaluateDataFrame_Classification() throws Exception { public void testEvaluateDataFrame_Regression() throws Exception { String indexName = "evaluate-classification-test-index"; - CreateIndexRequest createIndexRequest = - new CreateIndexRequest(indexName) - .mapping(XContentFactory.jsonBuilder().startObject() - .startObject("properties") - .startObject("actual_value") - .field("type", "double") - .endObject() - .startObject("predicted_value") - .field("type", "double") - .endObject() - .endObject() - .endObject()); - BulkRequest bulkRequest = - new BulkRequest(indexName) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .add(new IndexRequest().source(XContentType.JSON, "actual_value", 1.0, "predicted_value", 1.0)) // #0 - .add(new IndexRequest().source(XContentType.JSON, "actual_value", 1.0, "predicted_value", 0.9)) // #1 - .add(new IndexRequest().source(XContentType.JSON, "actual_value", 2.0, "predicted_value", 2.0)) // #2 - .add(new IndexRequest().source(XContentType.JSON, "actual_value", 1.5, "predicted_value", 1.4)) // #3 - .add(new IndexRequest().source(XContentType.JSON, "actual_value", 1.2, "predicted_value", 1.3)) // #4 - .add(new IndexRequest().source(XContentType.JSON, "actual_value", 1.7, "predicted_value", 2.0)) // #5 - .add(new IndexRequest().source(XContentType.JSON, "actual_value", 2.1, "predicted_value", 2.1)) // #6 - .add(new IndexRequest().source(XContentType.JSON, "actual_value", 2.5, "predicted_value", 2.7)) // #7 - .add(new IndexRequest().source(XContentType.JSON, "actual_value", 0.8, "predicted_value", 1.0)) // #8 - .add(new IndexRequest().source(XContentType.JSON, "actual_value", 2.5, "predicted_value", 2.4)); // #9 + CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName).mapping( + XContentFactory.jsonBuilder() + .startObject() + .startObject("properties") + .startObject("actual_value") + .field("type", "double") + .endObject() + .startObject("predicted_value") + .field("type", "double") + .endObject() + .endObject() + .endObject() + ); + BulkRequest bulkRequest = new BulkRequest(indexName).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + .add(new IndexRequest().source(XContentType.JSON, "actual_value", 1.0, "predicted_value", 1.0)) // #0 + .add(new IndexRequest().source(XContentType.JSON, "actual_value", 1.0, "predicted_value", 0.9)) // #1 + .add(new IndexRequest().source(XContentType.JSON, "actual_value", 2.0, "predicted_value", 2.0)) // #2 + .add(new IndexRequest().source(XContentType.JSON, "actual_value", 1.5, "predicted_value", 1.4)) // #3 + .add(new IndexRequest().source(XContentType.JSON, "actual_value", 1.2, "predicted_value", 1.3)) // #4 + .add(new IndexRequest().source(XContentType.JSON, "actual_value", 1.7, "predicted_value", 2.0)) // #5 + .add(new IndexRequest().source(XContentType.JSON, "actual_value", 2.1, "predicted_value", 2.1)) // #6 + .add(new IndexRequest().source(XContentType.JSON, "actual_value", 2.5, "predicted_value", 2.7)) // #7 + .add(new IndexRequest().source(XContentType.JSON, "actual_value", 0.8, "predicted_value", 1.0)) // #8 + .add(new IndexRequest().source(XContentType.JSON, "actual_value", 2.5, "predicted_value", 2.4)); // #9 RestHighLevelClient client = highLevelClient(); client.indices().create(createIndexRequest, RequestOptions.DEFAULT); client.bulk(bulkRequest, RequestOptions.DEFAULT); @@ -3690,9 +3752,7 @@ public void testEvaluateDataFrame_Regression() throws Exception { public void testExplainDataFrameAnalytics() throws Exception { createIndex("explain-df-test-source-index"); - BulkRequest bulkRequest = - new BulkRequest("explain-df-test-source-index") - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); + BulkRequest bulkRequest = new BulkRequest("explain-df-test-source-index").setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); for (int i = 0; i < 10; ++i) { bulkRequest.add(new IndexRequest().source(XContentType.JSON, "timestamp", 123456789L, "total", 10L)); } @@ -3780,7 +3840,7 @@ public void testGetTrainedModels() throws Exception { .setTags("regression") // <8> .setExcludeGenerated(false); // <9> // end::get-trained-models-request - request.setTags((List)null); + request.setTags((List) null); // tag::get-trained-models-execute GetTrainedModelsResponse response = client.machineLearning().getTrainedModels(request, RequestOptions.DEFAULT); @@ -3921,11 +3981,7 @@ public void testPutTrainedModelAlias() throws Exception { assertThat(acknowledged, is(true)); } { - PutTrainedModelAliasRequest request = new PutTrainedModelAliasRequest( - "my-second-alias", - "my-trained-model-with-alias", - false - ); + PutTrainedModelAliasRequest request = new PutTrainedModelAliasRequest("my-second-alias", "my-trained-model-with-alias", false); // tag::put-trained-model-alias-execute-listener ActionListener listener = new ActionListener<>() { @Override @@ -4019,7 +4075,6 @@ public void onFailure(Exception e) { } } - public void testGetTrainedModelsStats() throws Exception { putTrainedModel("my-trained-model"); RestHighLevelClient client = highLevelClient(); @@ -4475,8 +4530,9 @@ public void testEstimateModelMemory() throws Exception { assertThat(estimateInBytes, greaterThan(10000000L)); } { - AnalysisConfig analysisConfig = - AnalysisConfig.builder(Collections.singletonList(Detector.builder().setFunction("count").build())).build(); + AnalysisConfig analysisConfig = AnalysisConfig.builder( + Collections.singletonList(Detector.builder().setFunction("count").build()) + ).build(); EstimateModelMemoryRequest request = new EstimateModelMemoryRequest(analysisConfig); // tag::estimate-model-memory-execute-listener @@ -4519,23 +4575,25 @@ private String createFilter(RestHighLevelClient client) throws IOException { private void createIndex(String indexName) throws IOException { CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName); - createIndexRequest.mapping(XContentFactory.jsonBuilder().startObject() - .startObject("properties") + createIndexRequest.mapping( + XContentFactory.jsonBuilder() + .startObject() + .startObject("properties") .startObject("timestamp") - .field("type", "date") + .field("type", "date") .endObject() .startObject("total") - .field("type", "long") + .field("type", "long") .endObject() - .endObject() - .endObject()); + .endObject() + .endObject() + ); highLevelClient().indices().create(createIndexRequest, RequestOptions.DEFAULT); } private DataFrameAnalyticsState getAnalyticsState(String configId) throws IOException { - GetDataFrameAnalyticsStatsResponse statsResponse = - highLevelClient().machineLearning().getDataFrameAnalyticsStats( - new GetDataFrameAnalyticsStatsRequest(configId), RequestOptions.DEFAULT); + GetDataFrameAnalyticsStatsResponse statsResponse = highLevelClient().machineLearning() + .getDataFrameAnalyticsStats(new GetDataFrameAnalyticsStatsRequest(configId), RequestOptions.DEFAULT); assertThat(statsResponse.getAnalyticsStats(), hasSize(1)); DataFrameAnalyticsStats stats = statsResponse.getAnalyticsStats().get(0); return stats.getState(); @@ -4558,15 +4616,10 @@ protected NamedXContentRegistry xContentRegistry() { return new NamedXContentRegistry(new MlInferenceNamedXContentProvider().getNamedXContentParsers()); } - private static final DataFrameAnalyticsConfig DF_ANALYTICS_CONFIG = - DataFrameAnalyticsConfig.builder() - .setId("my-analytics-config") - .setSource(DataFrameAnalyticsSource.builder() - .setIndex("put-test-source-index") - .build()) - .setDest(DataFrameAnalyticsDest.builder() - .setIndex("put-test-dest-index") - .build()) - .setAnalysis(org.elasticsearch.client.ml.dataframe.OutlierDetection.createDefault()) - .build(); + private static final DataFrameAnalyticsConfig DF_ANALYTICS_CONFIG = DataFrameAnalyticsConfig.builder() + .setId("my-analytics-config") + .setSource(DataFrameAnalyticsSource.builder().setIndex("put-test-source-index").build()) + .setDest(DataFrameAnalyticsDest.builder().setIndex("put-test-dest-index").build()) + .setAnalysis(org.elasticsearch.client.ml.dataframe.OutlierDetection.createDefault()) + .build(); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/RollupDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/RollupDocumentationIT.java index ba8e683f33333..9ca78f5221150 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/RollupDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/RollupDocumentationIT.java @@ -51,8 +51,8 @@ import org.elasticsearch.core.TimeValue; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; -import org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregation; import org.elasticsearch.search.aggregations.metrics.MaxAggregationBuilder; +import org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregation; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.junit.Before; @@ -80,20 +80,21 @@ public void setUpDocs() throws IOException { bulkRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); for (int i = 0; i < 50; i++) { final IndexRequest indexRequest = new IndexRequest("docs"); - indexRequest.source(jsonBuilder() - .startObject() - .field("timestamp", String.format(Locale.ROOT, "2018-01-01T00:%02d:00Z", i)) - .field("hostname", 0) - .field("datacenter", 0) - .field("temperature", i) - .field("voltage", 0) - .field("load", 0) - .field("net_in", 0) - .field("net_out", 0) - .endObject()); + indexRequest.source( + jsonBuilder().startObject() + .field("timestamp", String.format(Locale.ROOT, "2018-01-01T00:%02d:00Z", i)) + .field("hostname", 0) + .field("datacenter", 0) + .field("temperature", i) + .field("voltage", 0) + .field("load", 0) + .field("net_in", 0) + .field("net_out", 0) + .endObject() + ); bulkRequest.add(indexRequest); } - BulkResponse bulkResponse = highLevelClient().bulk(bulkRequest, RequestOptions.DEFAULT); + BulkResponse bulkResponse = highLevelClient().bulk(bulkRequest, RequestOptions.DEFAULT); assertEquals(RestStatus.OK, bulkResponse.status()); assertFalse(bulkResponse.hasFailures()); @@ -186,7 +187,6 @@ public void testGetRollupJob() throws Exception { testCreateRollupJob(); RestHighLevelClient client = highLevelClient(); - // tag::x-pack-rollup-get-rollup-job-request GetRollupJobRequest getAll = new GetRollupJobRequest(); // <1> GetRollupJobRequest getJob = new GetRollupJobRequest("job_1"); // <2> @@ -296,7 +296,6 @@ public void testStopRollupJob() throws Exception { request.timeout(TimeValue.timeValueSeconds(10)); // <3> // end::rollup-stop-job-request - try { // tag::rollup-stop-job-execute RollupClient rc = client.rollup(); @@ -390,7 +389,11 @@ public void testGetRollupCaps() throws Exception { RestHighLevelClient client = highLevelClient(); DateHistogramGroupConfig dateHistogram = new DateHistogramGroupConfig.FixedInterval( - "timestamp", DateHistogramInterval.HOUR, new DateHistogramInterval("7d"), "UTC"); // <1> + "timestamp", + DateHistogramInterval.HOUR, + new DateHistogramInterval("7d"), + "UTC" + ); // <1> TermsGroupConfig terms = new TermsGroupConfig("hostname", "datacenter"); HistogramGroupConfig histogram = new HistogramGroupConfig(5L, "load", "net_in", "net_out"); GroupConfig groups = new GroupConfig(dateHistogram, histogram, terms); @@ -507,7 +510,11 @@ public void testGetRollupIndexCaps() throws Exception { RestHighLevelClient client = highLevelClient(); DateHistogramGroupConfig dateHistogram = new DateHistogramGroupConfig.FixedInterval( - "timestamp", DateHistogramInterval.HOUR, new DateHistogramInterval("7d"), "UTC"); // <1> + "timestamp", + DateHistogramInterval.HOUR, + new DateHistogramInterval("7d"), + "UTC" + ); // <1> TermsGroupConfig terms = new TermsGroupConfig("hostname", "datacenter"); HistogramGroupConfig histogram = new HistogramGroupConfig(5L, "load", "net_in", "net_out"); GroupConfig groups = new GroupConfig(dateHistogram, histogram, terms); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SearchDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SearchDocumentationIT.java index c8016be8168bf..4c72e37e9a095 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SearchDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SearchDocumentationIT.java @@ -48,8 +48,6 @@ import org.elasticsearch.common.document.DocumentField; import org.elasticsearch.common.text.Text; import org.elasticsearch.common.unit.Fuzziness; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.get.GetResult; import org.elasticsearch.index.query.MatchQueryBuilder; @@ -101,6 +99,8 @@ import org.elasticsearch.search.suggest.SuggestBuilders; import org.elasticsearch.search.suggest.SuggestionBuilder; import org.elasticsearch.search.suggest.term.TermSuggestion; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentType; import java.io.IOException; import java.util.ArrayList; @@ -125,7 +125,7 @@ @SuppressWarnings("removal") public class SearchDocumentationIT extends ESRestHighLevelClientTestCase { - @SuppressWarnings({"unused", "unchecked"}) + @SuppressWarnings({ "unused", "unchecked" }) public void testSearch() throws Exception { indexSearchTestData(); RestHighLevelClient client = highLevelClient(); @@ -300,12 +300,9 @@ public void testSearchRequestAggregations() throws IOException { RestHighLevelClient client = highLevelClient(); { BulkRequest request = new BulkRequest(); - request.add(new IndexRequest("posts").id("1") - .source(XContentType.JSON, "company", "Elastic", "age", 20)); - request.add(new IndexRequest("posts").id("2") - .source(XContentType.JSON, "company", "Elastic", "age", 30)); - request.add(new IndexRequest("posts").id("3") - .source(XContentType.JSON, "company", "Elastic", "age", 40)); + request.add(new IndexRequest("posts").id("1").source(XContentType.JSON, "company", "Elastic", "age", 20)); + request.add(new IndexRequest("posts").id("2").source(XContentType.JSON, "company", "Elastic", "age", 30)); + request.add(new IndexRequest("posts").id("3").source(XContentType.JSON, "company", "Elastic", "age", 40)); request.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); BulkResponse bulkResponse = client.bulk(request, RequestOptions.DEFAULT); assertSame(RestStatus.OK, bulkResponse.status()); @@ -371,7 +368,7 @@ public void testSearchRequestAggregations() throws IOException { } } - @SuppressWarnings({"unused", "rawtypes"}) + @SuppressWarnings({ "unused", "rawtypes" }) public void testSearchRequestSuggestions() throws IOException { RestHighLevelClient client = highLevelClient(); { @@ -419,15 +416,42 @@ public void testSearchRequestHighlighting() throws IOException { RestHighLevelClient client = highLevelClient(); { BulkRequest request = new BulkRequest(); - request.add(new IndexRequest("posts").id("1") - .source(XContentType.JSON, "title", "In which order are my Elasticsearch queries executed?", "user", - Arrays.asList("kimchy", "luca"), "innerObject", Collections.singletonMap("key", "value"))); - request.add(new IndexRequest("posts").id("2") - .source(XContentType.JSON, "title", "Current status and upcoming changes in Elasticsearch", "user", - Arrays.asList("kimchy", "christoph"), "innerObject", Collections.singletonMap("key", "value"))); - request.add(new IndexRequest("posts").id("3") - .source(XContentType.JSON, "title", "The Future of Federated Search in Elasticsearch", "user", - Arrays.asList("kimchy", "tanguy"), "innerObject", Collections.singletonMap("key", "value"))); + request.add( + new IndexRequest("posts").id("1") + .source( + XContentType.JSON, + "title", + "In which order are my Elasticsearch queries executed?", + "user", + Arrays.asList("kimchy", "luca"), + "innerObject", + Collections.singletonMap("key", "value") + ) + ); + request.add( + new IndexRequest("posts").id("2") + .source( + XContentType.JSON, + "title", + "Current status and upcoming changes in Elasticsearch", + "user", + Arrays.asList("kimchy", "christoph"), + "innerObject", + Collections.singletonMap("key", "value") + ) + ); + request.add( + new IndexRequest("posts").id("3") + .source( + XContentType.JSON, + "title", + "The Future of Federated Search in Elasticsearch", + "user", + Arrays.asList("kimchy", "tanguy"), + "innerObject", + Collections.singletonMap("key", "value") + ) + ); request.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); BulkResponse bulkResponse = client.bulk(request, RequestOptions.DEFAULT); assertSame(RestStatus.OK, bulkResponse.status()); @@ -446,9 +470,9 @@ public void testSearchRequestHighlighting() throws IOException { highlightBuilder.field(highlightUser); searchSourceBuilder.highlighter(highlightBuilder); // end::search-request-highlighting - searchSourceBuilder.query(QueryBuilders.boolQuery() - .should(matchQuery("title", "Elasticsearch")) - .should(matchQuery("user", "kimchy"))); + searchSourceBuilder.query( + QueryBuilders.boolQuery().should(matchQuery("title", "Elasticsearch")).should(matchQuery("user", "kimchy")) + ); searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); { @@ -482,8 +506,7 @@ public void testSearchRequestHighlighting() throws IOException { public void testSearchRequestProfiling() throws IOException { RestHighLevelClient client = highLevelClient(); { - IndexRequest request = new IndexRequest("posts").id("1") - .source(XContentType.JSON, "tags", "elasticsearch", "comments", 123); + IndexRequest request = new IndexRequest("posts").id("1").source(XContentType.JSON, "tags", "elasticsearch", "comments", 123); request.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL); IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT); assertSame(RestStatus.CREATED, indexResponse.status()); @@ -554,12 +577,16 @@ public void testScroll() throws Exception { RestHighLevelClient client = highLevelClient(); { BulkRequest request = new BulkRequest(); - request.add(new IndexRequest("posts").id("1") - .source(XContentType.JSON, "title", "In which order are my Elasticsearch queries executed?")); - request.add(new IndexRequest("posts").id("2") - .source(XContentType.JSON, "title", "Current status and upcoming changes in Elasticsearch")); - request.add(new IndexRequest("posts").id("3") - .source(XContentType.JSON, "title", "The Future of Federated Search in Elasticsearch")); + request.add( + new IndexRequest("posts").id("1") + .source(XContentType.JSON, "title", "In which order are my Elasticsearch queries executed?") + ); + request.add( + new IndexRequest("posts").id("2").source(XContentType.JSON, "title", "Current status and upcoming changes in Elasticsearch") + ); + request.add( + new IndexRequest("posts").id("3").source(XContentType.JSON, "title", "The Future of Federated Search in Elasticsearch") + ); request.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); BulkResponse bulkResponse = client.bulk(request, RequestOptions.DEFAULT); assertSame(RestStatus.OK, bulkResponse.status()); @@ -889,7 +916,6 @@ public void onFailure(Exception e) { assertTrue(latch.await(30L, TimeUnit.SECONDS)); } - @SuppressWarnings("unused") public void testMultiSearchTemplateWithInlineScript() throws Exception { indexSearchTestData(); @@ -972,9 +998,6 @@ public void testMultiSearchTemplateWithStoredScript() throws Exception { } // end::multi-search-template-request-stored - - - // tag::multi-search-template-execute MultiSearchTemplateResponse multiResponse = client.msearchTemplate(multiRequest, RequestOptions.DEFAULT); // end::multi-search-template-execute @@ -1028,7 +1051,6 @@ protected void registerQueryScript(RestClient restClient) throws IOException { assertEquals(RestStatus.OK.getStatus(), scriptResponse.getStatusLine().getStatusCode()); } - public void testExplain() throws Exception { indexSearchTestData(); RestHighLevelClient client = highLevelClient(); @@ -1142,9 +1164,9 @@ public void testFieldCaps() throws Exception { assertTrue(isSearchable); assertFalse(isAggregatable); - assertArrayEquals(indices, new String[]{"authors", "contributors"}); + assertArrayEquals(indices, new String[] { "authors", "contributors" }); assertNull(nonSearchableIndices); - assertArrayEquals(nonAggregatableIndices, new String[]{"authors"}); + assertArrayEquals(nonAggregatableIndices, new String[] { "authors" }); // tag::field-caps-execute-listener ActionListener listener = new ActionListener() { @@ -1300,52 +1322,86 @@ public void onFailure(Exception e) { } private void indexSearchTestData() throws IOException { - CreateIndexRequest authorsRequest = new CreateIndexRequest("authors") - .mapping(XContentFactory.jsonBuilder().startObject() + CreateIndexRequest authorsRequest = new CreateIndexRequest("authors").mapping( + XContentFactory.jsonBuilder() + .startObject() .startObject("properties") - .startObject("id") - .field("type", "keyword") - .endObject() - .startObject("user") - .field("type", "keyword") - .field("doc_values", "false") - .endObject() + .startObject("id") + .field("type", "keyword") + .endObject() + .startObject("user") + .field("type", "keyword") + .field("doc_values", "false") + .endObject() .endObject() - .endObject()); + .endObject() + ); CreateIndexResponse authorsResponse = highLevelClient().indices().create(authorsRequest, RequestOptions.DEFAULT); assertTrue(authorsResponse.isAcknowledged()); - CreateIndexRequest reviewersRequest = new CreateIndexRequest("contributors") - .mapping(XContentFactory.jsonBuilder().startObject() + CreateIndexRequest reviewersRequest = new CreateIndexRequest("contributors").mapping( + XContentFactory.jsonBuilder() + .startObject() .startObject("properties") - .startObject("id") - .field("type", "keyword") - .endObject() - .startObject("user") - .field("type", "keyword") - .field("store", "true") - .endObject() + .startObject("id") + .field("type", "keyword") + .endObject() + .startObject("user") + .field("type", "keyword") + .field("store", "true") .endObject() - .endObject()); + .endObject() + .endObject() + ); CreateIndexResponse reviewersResponse = highLevelClient().indices().create(reviewersRequest, RequestOptions.DEFAULT); assertTrue(reviewersResponse.isAcknowledged()); BulkRequest bulkRequest = new BulkRequest(); - bulkRequest.add(new IndexRequest("posts").id("1") - .source(XContentType.JSON, "id", 1, "title", "In which order are my Elasticsearch queries executed?", "user", - Arrays.asList("kimchy", "luca"), "innerObject", Collections.singletonMap("key", "value"))); - bulkRequest.add(new IndexRequest("posts").id("2") - .source(XContentType.JSON, "id", 2, "title", "Current status and upcoming changes in Elasticsearch", "user", - Arrays.asList("kimchy", "christoph"), "innerObject", Collections.singletonMap("key", "value"))); - bulkRequest.add(new IndexRequest("posts").id("3") - .source(XContentType.JSON, "id", 3, "title", "The Future of Federated Search in Elasticsearch", "user", - Arrays.asList("kimchy", "tanguy"), "innerObject", Collections.singletonMap("key", "value"))); - - bulkRequest.add(new IndexRequest("authors").id("1") - .source(XContentType.JSON, "id", 1, "user", "kimchy")); - bulkRequest.add(new IndexRequest("contributors").id("1") - .source(XContentType.JSON, "id", 1, "user", "tanguy")); + bulkRequest.add( + new IndexRequest("posts").id("1") + .source( + XContentType.JSON, + "id", + 1, + "title", + "In which order are my Elasticsearch queries executed?", + "user", + Arrays.asList("kimchy", "luca"), + "innerObject", + Collections.singletonMap("key", "value") + ) + ); + bulkRequest.add( + new IndexRequest("posts").id("2") + .source( + XContentType.JSON, + "id", + 2, + "title", + "Current status and upcoming changes in Elasticsearch", + "user", + Arrays.asList("kimchy", "christoph"), + "innerObject", + Collections.singletonMap("key", "value") + ) + ); + bulkRequest.add( + new IndexRequest("posts").id("3") + .source( + XContentType.JSON, + "id", + 3, + "title", + "The Future of Federated Search in Elasticsearch", + "user", + Arrays.asList("kimchy", "tanguy"), + "innerObject", + Collections.singletonMap("key", "value") + ) + ); + bulkRequest.add(new IndexRequest("authors").id("1").source(XContentType.JSON, "id", 1, "user", "kimchy")); + bulkRequest.add(new IndexRequest("contributors").id("1").source(XContentType.JSON, "id", 1, "user", "tanguy")); bulkRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); BulkResponse bulkResponse = highLevelClient().bulk(bulkRequest, RequestOptions.DEFAULT); @@ -1353,8 +1409,7 @@ private void indexSearchTestData() throws IOException { assertFalse(bulkResponse.hasFailures()); } - - @SuppressWarnings({"unused", "unchecked"}) + @SuppressWarnings({ "unused", "unchecked" }) public void testCount() throws Exception { indexCountTestData(); RestHighLevelClient client = highLevelClient(); @@ -1439,32 +1494,59 @@ public void onFailure(Exception e) { } private static void indexCountTestData() throws IOException { - CreateIndexRequest authorsRequest = new CreateIndexRequest("author") - .mapping(XContentFactory.jsonBuilder().startObject() + CreateIndexRequest authorsRequest = new CreateIndexRequest("author").mapping( + XContentFactory.jsonBuilder() + .startObject() .startObject("properties") - .startObject("user") - .field("type", "keyword") - .field("doc_values", "false") - .endObject() + .startObject("user") + .field("type", "keyword") + .field("doc_values", "false") .endObject() - .endObject()); + .endObject() + .endObject() + ); CreateIndexResponse authorsResponse = highLevelClient().indices().create(authorsRequest, RequestOptions.DEFAULT); assertTrue(authorsResponse.isAcknowledged()); BulkRequest bulkRequest = new BulkRequest(); - bulkRequest.add(new IndexRequest("blog").id("1") - .source(XContentType.JSON, "title", "Doubling Down on Open?", "user", - Collections.singletonList("kimchy"), "innerObject", Collections.singletonMap("key", "value"))); - bulkRequest.add(new IndexRequest("blog").id("2") - .source(XContentType.JSON, "title", "Swiftype Joins Forces with Elastic", "user", - Arrays.asList("kimchy", "matt"), "innerObject", Collections.singletonMap("key", "value"))); - bulkRequest.add(new IndexRequest("blog").id("3") - .source(XContentType.JSON, "title", "On Net Neutrality", "user", - Arrays.asList("tyler", "kimchy"), "innerObject", Collections.singletonMap("key", "value"))); - - bulkRequest.add(new IndexRequest("author").id("1") - .source(XContentType.JSON, "user", "kimchy")); + bulkRequest.add( + new IndexRequest("blog").id("1") + .source( + XContentType.JSON, + "title", + "Doubling Down on Open?", + "user", + Collections.singletonList("kimchy"), + "innerObject", + Collections.singletonMap("key", "value") + ) + ); + bulkRequest.add( + new IndexRequest("blog").id("2") + .source( + XContentType.JSON, + "title", + "Swiftype Joins Forces with Elastic", + "user", + Arrays.asList("kimchy", "matt"), + "innerObject", + Collections.singletonMap("key", "value") + ) + ); + bulkRequest.add( + new IndexRequest("blog").id("3") + .source( + XContentType.JSON, + "title", + "On Net Neutrality", + "user", + Arrays.asList("tyler", "kimchy"), + "innerObject", + Collections.singletonMap("key", "value") + ) + ); + bulkRequest.add(new IndexRequest("author").id("1").source(XContentType.JSON, "user", "kimchy")); bulkRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); BulkResponse bulkResponse = highLevelClient().bulk(bulkRequest, RequestOptions.DEFAULT); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SearchableSnapshotsDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SearchableSnapshotsDocumentationIT.java index 05e2289390c65..30e80e38beffa 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SearchableSnapshotsDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SearchableSnapshotsDocumentationIT.java @@ -29,10 +29,10 @@ import org.elasticsearch.client.searchable_snapshots.MountSnapshotRequest; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.repositories.fs.FsRepository; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.snapshots.RestoreInfo; +import org.elasticsearch.xcontent.XContentType; import java.io.IOException; import java.util.List; @@ -53,8 +53,7 @@ public void testMountSnapshot() throws IOException, InterruptedException { } { - final IndexRequest request = new IndexRequest("index") - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + final IndexRequest request = new IndexRequest("index").setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) .source("{}", XContentType.JSON); final IndexResponse response = client.index(request, RequestOptions.DEFAULT); assertThat(response.status(), is(RestStatus.CREATED)); @@ -69,8 +68,7 @@ public void testMountSnapshot() throws IOException, InterruptedException { } { - final CreateSnapshotRequest request = - new CreateSnapshotRequest("repository", "snapshot").waitForCompletion(true); + final CreateSnapshotRequest request = new CreateSnapshotRequest("repository", "snapshot").waitForCompletion(true); final CreateSnapshotResponse response = client.snapshot().create(request, RequestOptions.DEFAULT); assertThat(response.getSnapshotInfo().status(), is(RestStatus.OK)); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SecurityDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SecurityDocumentationIT.java index 1d4f3999d44c6..e6b103ccdda49 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SecurityDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SecurityDocumentationIT.java @@ -102,21 +102,19 @@ import org.elasticsearch.client.security.user.privileges.Role.ClusterPrivilegeName; import org.elasticsearch.client.security.user.privileges.Role.IndexPrivilegeName; import org.elasticsearch.client.security.user.privileges.UserIndicesPrivileges; -import org.elasticsearch.core.CheckedConsumer; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.common.util.set.Sets; +import org.elasticsearch.core.CheckedConsumer; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.searchafter.SearchAfterBuilder; import org.elasticsearch.search.sort.FieldSortBuilder; import org.elasticsearch.search.sort.SortOrder; import org.hamcrest.Matchers; -import javax.crypto.SecretKeyFactory; -import javax.crypto.spec.PBEKeySpec; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; @@ -141,6 +139,9 @@ import java.util.concurrent.TimeoutException; import java.util.stream.Collectors; +import javax.crypto.SecretKeyFactory; +import javax.crypto.spec.PBEKeySpec; + import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.containsString; @@ -164,14 +165,12 @@ public class SecurityDocumentationIT extends ESRestHighLevelClientTestCase { @Override protected Settings restAdminSettings() { String token = basicAuthHeaderValue("admin_user", new SecureString("admin-password".toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } public void testGetUsers() throws Exception { final RestHighLevelClient client = highLevelClient(); - String[] usernames = new String[]{"user1", "user2", "user3"}; + String[] usernames = new String[] { "user1", "user2", "user3" }; addUser(client, usernames[0], randomAlphaOfLengthBetween(14, 18)); addUser(client, usernames[1], randomAlphaOfLengthBetween(14, 18)); addUser(client, usernames[2], randomAlphaOfLengthBetween(14, 18)); @@ -286,7 +285,7 @@ public void testPutUser() throws Exception { byte[] salt = new byte[32]; // no need for secure random in a test; it could block and would not be reproducible anyway random().nextBytes(salt); - char[] password = new char[]{'t', 'e', 's', 't', '-', 'u', 's', 'e', 'r', '-', 'p', 'a', 's', 's', 'w', 'o', 'r', 'd'}; + char[] password = new char[] { 't', 'e', 's', 't', '-', 'u', 's', 'e', 'r', '-', 'p', 'a', 's', 's', 'w', 'o', 'r', 'd' }; User user = new User("example2", Collections.singletonList("superuser")); //tag::put-user-hash-request @@ -425,9 +424,15 @@ public void testPutRoleMapping() throws Exception { .addExpression(FieldRoleMapperExpression.ofUsername("*")) .addExpression(FieldRoleMapperExpression.ofGroups("cn=admins,dc=example,dc=com")) .build(); - final PutRoleMappingRequest request = new PutRoleMappingRequest("mapping-example", true, Collections.emptyList(), + final PutRoleMappingRequest request = new PutRoleMappingRequest( + "mapping-example", + true, + Collections.emptyList(), Collections.singletonList(new TemplateRoleName("{\"source\":\"{{username}}\"}", TemplateRoleName.Format.STRING)), - rules, null, RefreshPolicy.NONE); + rules, + null, + RefreshPolicy.NONE + ); // tag::put-role-mapping-execute-listener ActionListener listener = new ActionListener() { @Override @@ -464,22 +469,39 @@ public void testGetRoleMappings() throws Exception { final TemplateRoleName monitoring = new TemplateRoleName("{\"source\":\"monitoring\"}", TemplateRoleName.Format.STRING); final TemplateRoleName template = new TemplateRoleName("{\"source\":\"{{username}}\"}", TemplateRoleName.Format.STRING); - final RoleMapperExpression rules1 = AnyRoleMapperExpression.builder().addExpression(FieldRoleMapperExpression.ofUsername("*")) - .addExpression(FieldRoleMapperExpression.ofGroups("cn=admins,dc=example,dc=com")).build(); - final PutRoleMappingRequest putRoleMappingRequest1 = new PutRoleMappingRequest("mapping-example-1", true, Collections.emptyList(), - Arrays.asList(monitoring, template), rules1, null, RefreshPolicy.NONE); - final PutRoleMappingResponse putRoleMappingResponse1 = client.security().putRoleMapping(putRoleMappingRequest1, - RequestOptions.DEFAULT); + final RoleMapperExpression rules1 = AnyRoleMapperExpression.builder() + .addExpression(FieldRoleMapperExpression.ofUsername("*")) + .addExpression(FieldRoleMapperExpression.ofGroups("cn=admins,dc=example,dc=com")) + .build(); + final PutRoleMappingRequest putRoleMappingRequest1 = new PutRoleMappingRequest( + "mapping-example-1", + true, + Collections.emptyList(), + Arrays.asList(monitoring, template), + rules1, + null, + RefreshPolicy.NONE + ); + final PutRoleMappingResponse putRoleMappingResponse1 = client.security() + .putRoleMapping(putRoleMappingRequest1, RequestOptions.DEFAULT); boolean isCreated1 = putRoleMappingResponse1.isCreated(); assertTrue(isCreated1); - final RoleMapperExpression rules2 = AnyRoleMapperExpression.builder().addExpression(FieldRoleMapperExpression.ofGroups( - "cn=admins,dc=example,dc=com")).build(); + final RoleMapperExpression rules2 = AnyRoleMapperExpression.builder() + .addExpression(FieldRoleMapperExpression.ofGroups("cn=admins,dc=example,dc=com")) + .build(); final Map metadata2 = new HashMap<>(); metadata2.put("k1", "v1"); - final PutRoleMappingRequest putRoleMappingRequest2 = new PutRoleMappingRequest("mapping-example-2", true, - Arrays.asList("superuser"), Collections.emptyList(), rules2, metadata2, RefreshPolicy.NONE); - final PutRoleMappingResponse putRoleMappingResponse2 = client.security().putRoleMapping(putRoleMappingRequest2, - RequestOptions.DEFAULT); + final PutRoleMappingRequest putRoleMappingRequest2 = new PutRoleMappingRequest( + "mapping-example-2", + true, + Arrays.asList("superuser"), + Collections.emptyList(), + rules2, + metadata2, + RefreshPolicy.NONE + ); + final PutRoleMappingResponse putRoleMappingResponse2 = client.security() + .putRoleMapping(putRoleMappingRequest2, RequestOptions.DEFAULT); boolean isCreated2 = putRoleMappingResponse2.isCreated(); assertTrue(isCreated2); @@ -512,7 +534,7 @@ public void testGetRoleMappings() throws Exception { assertThat(mappings.size(), is(2)); for (ExpressionRoleMapping roleMapping : mappings) { assertThat(roleMapping.isEnabled(), is(true)); - assertThat(roleMapping.getName(), in(new String[]{"mapping-example-1", "mapping-example-2"})); + assertThat(roleMapping.getName(), in(new String[] { "mapping-example-1", "mapping-example-2" })); if (roleMapping.getName().equals("mapping-example-1")) { assertThat(roleMapping.getMetadata(), equalTo(Collections.emptyMap())); assertThat(roleMapping.getExpression(), equalTo(rules1)); @@ -537,7 +559,7 @@ public void testGetRoleMappings() throws Exception { assertThat(mappings.size(), is(2)); for (ExpressionRoleMapping roleMapping : mappings) { assertThat(roleMapping.isEnabled(), is(true)); - assertThat(roleMapping.getName(), in(new String[]{"mapping-example-1", "mapping-example-2"})); + assertThat(roleMapping.getName(), in(new String[] { "mapping-example-1", "mapping-example-2" })); if (roleMapping.getName().equals("mapping-example-1")) { assertThat(roleMapping.getMetadata(), equalTo(Collections.emptyMap())); assertThat(roleMapping.getExpression(), equalTo(rules1)); @@ -582,7 +604,7 @@ public void onFailure(Exception e) { public void testEnableUser() throws Exception { RestHighLevelClient client = highLevelClient(); - char[] password = new char[]{'t', 'e', 's', 't', '-', 'u', 's', 'e', 'r', '-', 'p', 'a', 's', 's', 'w', 'o', 'r', 'd'}; + char[] password = new char[] { 't', 'e', 's', 't', '-', 'u', 's', 'e', 'r', '-', 'p', 'a', 's', 's', 'w', 'o', 'r', 'd' }; User enable_user = new User("enable_user", Collections.singletonList("superuser")); PutUserRequest putUserRequest = PutUserRequest.withPassword(enable_user, password, true, RefreshPolicy.IMMEDIATE); PutUserResponse putUserResponse = client.security().putUser(putUserRequest, RequestOptions.DEFAULT); @@ -627,7 +649,7 @@ public void onFailure(Exception e) { public void testDisableUser() throws Exception { RestHighLevelClient client = highLevelClient(); - char[] password = new char[]{'t', 'e', 's', 't', '-', 'u', 's', 'e', 'r', '-', 'p', 'a', 's', 's', 'w', 'o', 'r', 'd'}; + char[] password = new char[] { 't', 'e', 's', 't', '-', 'u', 's', 'e', 'r', '-', 'p', 'a', 's', 's', 'w', 'o', 'r', 'd' }; User disable_user = new User("disable_user", Collections.singletonList("superuser")); PutUserRequest putUserRequest = PutUserRequest.withPassword(disable_user, password, true, RefreshPolicy.IMMEDIATE); PutUserResponse putUserResponse = client.security().putUser(putUserRequest, RequestOptions.DEFAULT); @@ -771,7 +793,7 @@ public void testAuthenticate() throws Exception { //end::authenticate-response assertThat(user.getUsername(), is("test_user")); - assertThat(user.getRoles(), contains(new String[]{"admin"})); + assertThat(user.getRoles(), contains(new String[] { "admin" })); assertThat(user.getFullName(), nullValue()); assertThat(user.getEmail(), nullValue()); assertThat(user.getMetadata().isEmpty(), is(true)); @@ -1142,8 +1164,12 @@ public void testClearServiceAccountTokenCache() throws Exception { } { - ClearServiceAccountTokenCacheRequest request = new ClearServiceAccountTokenCacheRequest("elastic", "fleet-server", - "token1", "token2"); + ClearServiceAccountTokenCacheRequest request = new ClearServiceAccountTokenCacheRequest( + "elastic", + "fleet-server", + "token1", + "token2" + ); //tag::clear-service-account-token-cache-execute-listener ActionListener listener = new ActionListener<>() { @Override @@ -1260,9 +1286,30 @@ public void onFailure(Exception e) { public void testChangePassword() throws Exception { RestHighLevelClient client = highLevelClient(); - char[] password = new char[]{'t', 'e', 's', 't', '-', 'u', 's', 'e', 'r', '-', 'p', 'a', 's', 's', 'w', 'o', 'r', 'd'}; - char[] newPassword = - new char[]{'n', 'e', 'w', '-', 't', 'e', 's', 't', '-', 'u', 's', 'e', 'r', '-', 'p', 'a', 's', 's', 'w', 'o', 'r', 'd'}; + char[] password = new char[] { 't', 'e', 's', 't', '-', 'u', 's', 'e', 'r', '-', 'p', 'a', 's', 's', 'w', 'o', 'r', 'd' }; + char[] newPassword = new char[] { + 'n', + 'e', + 'w', + '-', + 't', + 'e', + 's', + 't', + '-', + 'u', + 's', + 'e', + 'r', + '-', + 'p', + 'a', + 's', + 's', + 'w', + 'o', + 'r', + 'd' }; User user = new User("change_password_user", Collections.singletonList("superuser"), Collections.emptyMap(), null, null); PutUserRequest putUserRequest = PutUserRequest.withPassword(user, password, true, RefreshPolicy.NONE); PutUserResponse putUserResponse = client.security().putUser(putUserRequest, RequestOptions.DEFAULT); @@ -1309,8 +1356,15 @@ public void testDeleteRoleMapping() throws Exception { { // Create role mappings final RoleMapperExpression rules = FieldRoleMapperExpression.ofUsername("*"); - final PutRoleMappingRequest request = new PutRoleMappingRequest("mapping-example", true, - Collections.singletonList("superuser"), Collections.emptyList(), rules, null, RefreshPolicy.NONE); + final PutRoleMappingRequest request = new PutRoleMappingRequest( + "mapping-example", + true, + Collections.singletonList("superuser"), + Collections.emptyList(), + rules, + null, + RefreshPolicy.NONE + ); final PutRoleMappingResponse response = client.security().putRoleMapping(request, RequestOptions.DEFAULT); boolean isCreated = response.isCreated(); assertTrue(isCreated); @@ -1467,10 +1521,7 @@ public void onFailure(Exception e) { } private void addRole(String roleName) throws IOException { - final Role role = Role.builder() - .name(roleName) - .clusterPrivileges("all") - .build(); + final Role role = Role.builder().name(roleName).clusterPrivileges("all").build(); final PutRoleRequest request = new PutRoleRequest(role, RefreshPolicy.IMMEDIATE); highLevelClient().security().putRole(request, RequestOptions.DEFAULT); } @@ -1481,8 +1532,12 @@ public void testCreateToken() throws Exception { { // Setup user User token_user = new User("token_user", Collections.singletonList("kibana_user")); - PutUserRequest putUserRequest = PutUserRequest.withPassword(token_user, "test-user-password".toCharArray(), true, - RefreshPolicy.IMMEDIATE); + PutUserRequest putUserRequest = PutUserRequest.withPassword( + token_user, + "test-user-password".toCharArray(), + true, + RefreshPolicy.IMMEDIATE + ); PutUserResponse putUserResponse = client.security().putUser(putUserRequest, RequestOptions.DEFAULT); assertTrue(putUserResponse.isCreated()); } @@ -1627,8 +1682,8 @@ public void testInvalidateToken() throws Exception { // tag::invalidate-refresh-token-request InvalidateTokenRequest invalidateTokenRequest = InvalidateTokenRequest.refreshToken(refreshToken); // end::invalidate-refresh-token-request - InvalidateTokenResponse invalidateTokenResponse = - client.security().invalidateToken(invalidateTokenRequest, RequestOptions.DEFAULT); + InvalidateTokenResponse invalidateTokenResponse = client.security() + .invalidateToken(invalidateTokenRequest, RequestOptions.DEFAULT); assertTrue(invalidateTokenResponse.getErrors().isEmpty()); assertThat(invalidateTokenResponse.getInvalidatedTokens(), equalTo(1)); assertThat(invalidateTokenResponse.getPreviouslyInvalidatedTokens(), equalTo(0)); @@ -1638,8 +1693,8 @@ public void testInvalidateToken() throws Exception { // tag::invalidate-user-tokens-request InvalidateTokenRequest invalidateTokenRequest = InvalidateTokenRequest.userTokens("other_user"); // end::invalidate-user-tokens-request - InvalidateTokenResponse invalidateTokenResponse = - client.security().invalidateToken(invalidateTokenRequest, RequestOptions.DEFAULT); + InvalidateTokenResponse invalidateTokenResponse = client.security() + .invalidateToken(invalidateTokenRequest, RequestOptions.DEFAULT); assertTrue(invalidateTokenResponse.getErrors().isEmpty()); // We have one refresh and one access token for that user assertThat(invalidateTokenResponse.getInvalidatedTokens(), equalTo(2)); @@ -1650,8 +1705,8 @@ public void testInvalidateToken() throws Exception { // tag::invalidate-user-realm-tokens-request InvalidateTokenRequest invalidateTokenRequest = new InvalidateTokenRequest(null, null, "default_native", "extra_user"); // end::invalidate-user-realm-tokens-request - InvalidateTokenResponse invalidateTokenResponse = - client.security().invalidateToken(invalidateTokenRequest, RequestOptions.DEFAULT); + InvalidateTokenResponse invalidateTokenResponse = client.security() + .invalidateToken(invalidateTokenRequest, RequestOptions.DEFAULT); assertTrue(invalidateTokenResponse.getErrors().isEmpty()); // We have one refresh and one access token for that user in this realm assertThat(invalidateTokenResponse.getInvalidatedTokens(), equalTo(2)); @@ -1692,7 +1747,7 @@ public void onFailure(Exception e) { final InvalidateTokenResponse response = future.get(30, TimeUnit.SECONDS); assertNotNull(response); assertTrue(response.getErrors().isEmpty()); - //We still have 4 tokens ( 2 access_tokens and 2 refresh_tokens ) for the default_native realm + // We still have 4 tokens ( 2 access_tokens and 2 refresh_tokens ) for the default_native realm assertThat(response.getInvalidatedTokens(), equalTo(4)); assertThat(response.getPreviouslyInvalidatedTokens(), equalTo(0)); } @@ -1754,22 +1809,46 @@ public void onFailure(Exception e) { public void testGetPrivileges() throws Exception { final RestHighLevelClient client = highLevelClient(); - final ApplicationPrivilege readTestappPrivilege = - new ApplicationPrivilege("testapp", "read", Arrays.asList("action:login", "data:read/*"), null); + final ApplicationPrivilege readTestappPrivilege = new ApplicationPrivilege( + "testapp", + "read", + Arrays.asList("action:login", "data:read/*"), + null + ); final Map metadata = new HashMap<>(); metadata.put("key1", "value1"); - final ApplicationPrivilege writeTestappPrivilege = - new ApplicationPrivilege("testapp", "write", Arrays.asList("action:login", "data:write/*"), metadata); - final ApplicationPrivilege allTestappPrivilege = - new ApplicationPrivilege("testapp", "all", Arrays.asList("action:login", "data:write/*", "manage:*"), null); + final ApplicationPrivilege writeTestappPrivilege = new ApplicationPrivilege( + "testapp", + "write", + Arrays.asList("action:login", "data:write/*"), + metadata + ); + final ApplicationPrivilege allTestappPrivilege = new ApplicationPrivilege( + "testapp", + "all", + Arrays.asList("action:login", "data:write/*", "manage:*"), + null + ); final Map metadata2 = new HashMap<>(); metadata2.put("key2", "value2"); - final ApplicationPrivilege readTestapp2Privilege = - new ApplicationPrivilege("testapp2", "read", Arrays.asList("action:login", "data:read/*"), metadata2); - final ApplicationPrivilege writeTestapp2Privilege = - new ApplicationPrivilege("testapp2", "write", Arrays.asList("action:login", "data:write/*"), null); - final ApplicationPrivilege allTestapp2Privilege = - new ApplicationPrivilege("testapp2", "all", Arrays.asList("action:login", "data:write/*", "manage:*"), null); + final ApplicationPrivilege readTestapp2Privilege = new ApplicationPrivilege( + "testapp2", + "read", + Arrays.asList("action:login", "data:read/*"), + metadata2 + ); + final ApplicationPrivilege writeTestapp2Privilege = new ApplicationPrivilege( + "testapp2", + "write", + Arrays.asList("action:login", "data:write/*"), + null + ); + final ApplicationPrivilege allTestapp2Privilege = new ApplicationPrivilege( + "testapp2", + "all", + Arrays.asList("action:login", "data:write/*", "manage:*"), + null + ); { List applicationPrivileges = new ArrayList<>(); @@ -1811,8 +1890,9 @@ public void testGetPrivileges() throws Exception { assertNotNull(response); assertThat(response.getPrivileges().size(), equalTo(3)); - final GetPrivilegesResponse expectedResponse = - new GetPrivilegesResponse(Arrays.asList(readTestappPrivilege, writeTestappPrivilege, allTestappPrivilege)); + final GetPrivilegesResponse expectedResponse = new GetPrivilegesResponse( + Arrays.asList(readTestappPrivilege, writeTestappPrivilege, allTestappPrivilege) + ); assertThat(response, equalTo(expectedResponse)); //tag::get-privileges-response Set privileges = response.getPrivileges(); @@ -1841,9 +1921,16 @@ public void testGetPrivileges() throws Exception { assertNotNull(response); assertThat(response.getPrivileges().size(), equalTo(6)); - final GetPrivilegesResponse exptectedResponse = - new GetPrivilegesResponse(Arrays.asList(readTestappPrivilege, writeTestappPrivilege, allTestappPrivilege, - readTestapp2Privilege, writeTestapp2Privilege, allTestapp2Privilege)); + final GetPrivilegesResponse exptectedResponse = new GetPrivilegesResponse( + Arrays.asList( + readTestappPrivilege, + writeTestappPrivilege, + allTestappPrivilege, + readTestapp2Privilege, + writeTestapp2Privilege, + allTestapp2Privilege + ) + ); assertThat(response, equalTo(exptectedResponse)); } @@ -1916,12 +2003,14 @@ public void testPutPrivileges() throws Exception { { final List privileges = new ArrayList<>(); - privileges.add(ApplicationPrivilege.builder() - .application("app01") - .privilege("all") - .actions(List.of("action:login")) - .metadata(Collections.singletonMap("k1", "v1")) - .build()); + privileges.add( + ApplicationPrivilege.builder() + .application("app01") + .privilege("all") + .actions(List.of("action:login")) + .metadata(Collections.singletonMap("k1", "v1")) + .build() + ); final PutPrivilegesRequest putPrivilegesRequest = new PutPrivilegesRequest(privileges, RefreshPolicy.IMMEDIATE); // tag::put-privileges-execute-listener @@ -1958,21 +2047,15 @@ public void testDeletePrivilege() throws Exception { RestHighLevelClient client = highLevelClient(); { List applicationPrivileges = new ArrayList<>(); - applicationPrivileges.add(ApplicationPrivilege.builder() - .application("testapp") - .privilege("read") - .actions("action:login", "data:read/*") - .build()); - applicationPrivileges.add(ApplicationPrivilege.builder() - .application("testapp") - .privilege("write") - .actions("action:login", "data:write/*") - .build()); - applicationPrivileges.add(ApplicationPrivilege.builder() - .application("testapp") - .privilege("all") - .actions("action:login", "data:write/*") - .build()); + applicationPrivileges.add( + ApplicationPrivilege.builder().application("testapp").privilege("read").actions("action:login", "data:read/*").build() + ); + applicationPrivileges.add( + ApplicationPrivilege.builder().application("testapp").privilege("write").actions("action:login", "data:write/*").build() + ); + applicationPrivileges.add( + ApplicationPrivilege.builder().application("testapp").privilege("all").actions("action:login", "data:write/*").build() + ); PutPrivilegesRequest putPrivilegesRequest = new PutPrivilegesRequest(applicationPrivileges, RefreshPolicy.IMMEDIATE); PutPrivilegesResponse putPrivilegesResponse = client.security().putPrivileges(putPrivilegesRequest, RequestOptions.DEFAULT); @@ -2037,8 +2120,13 @@ public void onFailure(Exception e) { public void testCreateApiKey() throws Exception { RestHighLevelClient client = highLevelClient(); - List roles = Collections.singletonList(Role.builder().name("r1").clusterPrivileges(ClusterPrivilegeName.ALL) - .indicesPrivileges(IndicesPrivileges.builder().indices("ind-x").privileges(IndexPrivilegeName.ALL).build()).build()); + List roles = Collections.singletonList( + Role.builder() + .name("r1") + .clusterPrivileges(ClusterPrivilegeName.ALL) + .indicesPrivileges(IndicesPrivileges.builder().indices("ind-x").privileges(IndexPrivilegeName.ALL).build()) + .build() + ); final TimeValue expiration = TimeValue.timeValueHours(24); final RefreshPolicy refreshPolicy = randomFrom(RefreshPolicy.values()); final Map metadata = CreateApiKeyRequestTests.randomMetadata(); @@ -2108,9 +2196,13 @@ public void testGrantApiKey() throws Exception { addUser(client, username, passwordString); - List roles = Collections.singletonList(Role.builder().name("r1").clusterPrivileges(ClusterPrivilegeName.ALL) - .indicesPrivileges(IndicesPrivileges.builder().indices("ind-x").privileges(IndexPrivilegeName.ALL).build()).build()); - + List roles = Collections.singletonList( + Role.builder() + .name("r1") + .clusterPrivileges(ClusterPrivilegeName.ALL) + .indicesPrivileges(IndicesPrivileges.builder().indices("ind-x").privileges(IndexPrivilegeName.ALL).build()) + .build() + ); final Instant start = Instant.now(); final Map metadata = CreateApiKeyRequestTests.randomMetadata(); @@ -2206,8 +2298,13 @@ public void onFailure(Exception e) { public void testGetApiKey() throws Exception { RestHighLevelClient client = highLevelClient(); - List roles = Collections.singletonList(Role.builder().name("r1").clusterPrivileges(ClusterPrivilegeName.ALL) - .indicesPrivileges(IndicesPrivileges.builder().indices("ind-x").privileges(IndexPrivilegeName.ALL).build()).build()); + List roles = Collections.singletonList( + Role.builder() + .name("r1") + .clusterPrivileges(ClusterPrivilegeName.ALL) + .indicesPrivileges(IndicesPrivileges.builder().indices("ind-x").privileges(IndexPrivilegeName.ALL).build()) + .build() + ); final TimeValue expiration = TimeValue.timeValueHours(24); final RefreshPolicy refreshPolicy = randomFrom(RefreshPolicy.values()); final Map metadata = CreateApiKeyRequestTests.randomMetadata(); @@ -2218,8 +2315,16 @@ public void testGetApiKey() throws Exception { assertNotNull(createApiKeyResponse1.getKey()); assertNotNull(createApiKeyResponse1.getEncoded()); - final ApiKey expectedApiKeyInfo = new ApiKey(createApiKeyResponse1.getName(), createApiKeyResponse1.getId(), Instant.now(), - Instant.now().plusMillis(expiration.getMillis()), false, "test_user", "default_file", metadata); + final ApiKey expectedApiKeyInfo = new ApiKey( + createApiKeyResponse1.getName(), + createApiKeyResponse1.getId(), + Instant.now(), + Instant.now().plusMillis(expiration.getMillis()), + false, + "test_user", + "default_file", + metadata + ); { // tag::get-api-key-id-request GetApiKeyRequest getApiKeyRequest = GetApiKeyRequest.usingApiKeyId(createApiKeyResponse1.getId(), false); @@ -2363,8 +2468,13 @@ private void verifyApiKey(final ApiKey actual, final ApiKey expected) { public void testInvalidateApiKey() throws Exception { RestHighLevelClient client = highLevelClient(); - List roles = Collections.singletonList(Role.builder().name("r1").clusterPrivileges(ClusterPrivilegeName.ALL) - .indicesPrivileges(IndicesPrivileges.builder().indices("ind-x").privileges(IndexPrivilegeName.ALL).build()).build()); + List roles = Collections.singletonList( + Role.builder() + .name("r1") + .clusterPrivileges(ClusterPrivilegeName.ALL) + .indicesPrivileges(IndicesPrivileges.builder().indices("ind-x").privileges(IndexPrivilegeName.ALL).build()) + .build() + ); final TimeValue expiration = TimeValue.timeValueHours(24); final RefreshPolicy refreshPolicy = randomFrom(RefreshPolicy.values()); final Map metadata = CreateApiKeyRequestTests.randomMetadata(); @@ -2401,8 +2511,8 @@ public void testInvalidateApiKey() throws Exception { Arrays.asList("kI3QZHYBnpSXoDRq1XzR", "ko3SZHYBnpSXoDRqk3zm"), false); // end::invalidate-api-key-ids-request - InvalidateApiKeyResponse invalidateApiKeyResponse = client.security().invalidateApiKey(invalidateApiKeyRequest, - RequestOptions.DEFAULT); + InvalidateApiKeyResponse invalidateApiKeyResponse = client.security() + .invalidateApiKey(invalidateApiKeyRequest, RequestOptions.DEFAULT); final List errors = invalidateApiKeyResponse.getErrors(); final List invalidatedApiKeyIds = invalidateApiKeyResponse.getInvalidatedApiKeys(); @@ -2425,8 +2535,8 @@ public void testInvalidateApiKey() throws Exception { false); // end::invalidate-api-key-name-request - InvalidateApiKeyResponse invalidateApiKeyResponse = client.security().invalidateApiKey(invalidateApiKeyRequest, - RequestOptions.DEFAULT); + InvalidateApiKeyResponse invalidateApiKeyResponse = client.security() + .invalidateApiKey(invalidateApiKeyRequest, RequestOptions.DEFAULT); final List errors = invalidateApiKeyResponse.getErrors(); final List invalidatedApiKeyIds = invalidateApiKeyResponse.getInvalidatedApiKeys(); @@ -2449,8 +2559,8 @@ public void testInvalidateApiKey() throws Exception { InvalidateApiKeyRequest invalidateApiKeyRequest = InvalidateApiKeyRequest.usingRealmName("default_file"); // end::invalidate-realm-api-keys-request - InvalidateApiKeyResponse invalidateApiKeyResponse = client.security().invalidateApiKey(invalidateApiKeyRequest, - RequestOptions.DEFAULT); + InvalidateApiKeyResponse invalidateApiKeyResponse = client.security() + .invalidateApiKey(invalidateApiKeyRequest, RequestOptions.DEFAULT); final List errors = invalidateApiKeyResponse.getErrors(); final List invalidatedApiKeyIds = invalidateApiKeyResponse.getInvalidatedApiKeys(); @@ -2473,8 +2583,8 @@ public void testInvalidateApiKey() throws Exception { InvalidateApiKeyRequest invalidateApiKeyRequest = InvalidateApiKeyRequest.usingUserName("test_user"); // end::invalidate-user-api-keys-request - InvalidateApiKeyResponse invalidateApiKeyResponse = client.security().invalidateApiKey(invalidateApiKeyRequest, - RequestOptions.DEFAULT); + InvalidateApiKeyResponse invalidateApiKeyResponse = client.security() + .invalidateApiKey(invalidateApiKeyRequest, RequestOptions.DEFAULT); final List errors = invalidateApiKeyResponse.getErrors(); final List invalidatedApiKeyIds = invalidateApiKeyResponse.getInvalidatedApiKeys(); @@ -2567,8 +2677,8 @@ public void onFailure(Exception e) { InvalidateApiKeyRequest invalidateApiKeyRequest = InvalidateApiKeyRequest.forOwnedApiKeys(); // end::invalidate-api-keys-owned-by-authenticated-user-request - InvalidateApiKeyResponse invalidateApiKeyResponse = client.security().invalidateApiKey(invalidateApiKeyRequest, - RequestOptions.DEFAULT); + InvalidateApiKeyResponse invalidateApiKeyResponse = client.security() + .invalidateApiKey(invalidateApiKeyRequest, RequestOptions.DEFAULT); final List errors = invalidateApiKeyResponse.getErrors(); final List invalidatedApiKeyIds = invalidateApiKeyResponse.getInvalidatedApiKeys(); @@ -2584,13 +2694,21 @@ public void onFailure(Exception e) { public void testQueryApiKey() throws IOException, ExecutionException, InterruptedException, TimeoutException { RestHighLevelClient client = highLevelClient(); - final CreateApiKeyRequest createApiKeyRequest1 = new CreateApiKeyRequest("key-10000", List.of(), + final CreateApiKeyRequest createApiKeyRequest1 = new CreateApiKeyRequest( + "key-10000", + List.of(), randomBoolean() ? TimeValue.timeValueHours(24) : null, - RefreshPolicy.WAIT_UNTIL, Map.of("environment", "east-production")); + RefreshPolicy.WAIT_UNTIL, + Map.of("environment", "east-production") + ); final CreateApiKeyResponse createApiKeyResponse1 = client.security().createApiKey(createApiKeyRequest1, RequestOptions.DEFAULT); - final CreateApiKeyRequest createApiKeyRequest2 = new CreateApiKeyRequest("key-20000", List.of(), + final CreateApiKeyRequest createApiKeyRequest2 = new CreateApiKeyRequest( + "key-20000", + List.of(), randomBoolean() ? TimeValue.timeValueHours(24) : null, - RefreshPolicy.WAIT_UNTIL, Map.of("environment", "east-staging")); + RefreshPolicy.WAIT_UNTIL, + Map.of("environment", "east-staging") + ); final CreateApiKeyResponse createApiKeyResponse2 = client.security().createApiKey(createApiKeyRequest2, RequestOptions.DEFAULT); { @@ -2604,10 +2722,14 @@ public void testQueryApiKey() throws IOException, ExecutionException, Interrupte assertThat(queryApiKeyResponse.getTotal(), equalTo(2L)); assertThat(queryApiKeyResponse.getCount(), equalTo(2)); - assertThat(queryApiKeyResponse.getApiKeys().stream().map(ApiKey::getName).collect(Collectors.toUnmodifiableSet()), - equalTo(Set.of("key-10000", "key-20000"))); - assertThat(queryApiKeyResponse.getApiKeys().stream().map(ApiKey::getId).collect(Collectors.toUnmodifiableSet()), - equalTo(Set.of(createApiKeyResponse1.getId(), createApiKeyResponse2.getId()))); + assertThat( + queryApiKeyResponse.getApiKeys().stream().map(ApiKey::getName).collect(Collectors.toUnmodifiableSet()), + equalTo(Set.of("key-10000", "key-20000")) + ); + assertThat( + queryApiKeyResponse.getApiKeys().stream().map(ApiKey::getId).collect(Collectors.toUnmodifiableSet()), + equalTo(Set.of(createApiKeyResponse1.getId(), createApiKeyResponse2.getId())) + ); } { @@ -2700,10 +2822,14 @@ public void onFailure(Exception e) { assertThat(queryApiKeyResponse.getCount(), equalTo(2)); assertThat(queryApiKeyResponse.getApiKeys(), is(notNullValue())); assertThat(queryApiKeyResponse.getApiKeys().size(), is(2)); - assertThat(queryApiKeyResponse.getApiKeys().stream().map(ApiKey::getName).collect(Collectors.toUnmodifiableSet()), - equalTo(Set.of("key-10000", "key-20000"))); - assertThat(queryApiKeyResponse.getApiKeys().stream().map(ApiKey::getId).collect(Collectors.toUnmodifiableSet()), - equalTo(Set.of(createApiKeyResponse1.getId(), createApiKeyResponse2.getId()))); + assertThat( + queryApiKeyResponse.getApiKeys().stream().map(ApiKey::getName).collect(Collectors.toUnmodifiableSet()), + equalTo(Set.of("key-10000", "key-20000")) + ); + assertThat( + queryApiKeyResponse.getApiKeys().stream().map(ApiKey::getId).collect(Collectors.toUnmodifiableSet()), + equalTo(Set.of(createApiKeyResponse1.getId(), createApiKeyResponse2.getId())) + ); } } @@ -2825,8 +2951,11 @@ public void onFailure(Exception e) { public void testDeleteServiceAccountToken() throws IOException { RestHighLevelClient client = highLevelClient(); - final CreateServiceAccountTokenRequest createServiceAccountTokenRequest = - new CreateServiceAccountTokenRequest("elastic", "fleet-server", "test-token"); + final CreateServiceAccountTokenRequest createServiceAccountTokenRequest = new CreateServiceAccountTokenRequest( + "elastic", + "fleet-server", + "test-token" + ); client.security().createServiceAccountToken(createServiceAccountTokenRequest, RequestOptions.DEFAULT); { // tag::delete-service-account-token-request @@ -2847,8 +2976,11 @@ public void testDeleteServiceAccountToken() throws IOException { client.security().createServiceAccountToken(createServiceAccountTokenRequest, RequestOptions.DEFAULT); { - DeleteServiceAccountTokenRequest deleteServiceAccountTokenRequest = - new DeleteServiceAccountTokenRequest("elastic", "fleet-server", "test-token"); + DeleteServiceAccountTokenRequest deleteServiceAccountTokenRequest = new DeleteServiceAccountTokenRequest( + "elastic", + "fleet-server", + "test-token" + ); ActionListener listener; // tag::delete-service-account-token-execute-listener listener = new ActionListener() { @@ -2878,10 +3010,13 @@ public void onFailure(Exception e) { public void testGetServiceAccountCredentials() throws IOException { RestHighLevelClient client = highLevelClient(); - final CreateServiceAccountTokenRequest createServiceAccountTokenRequest = - new CreateServiceAccountTokenRequest("elastic", "fleet-server", "token2"); - final CreateServiceAccountTokenResponse createServiceAccountTokenResponse = - client.security().createServiceAccountToken(createServiceAccountTokenRequest, RequestOptions.DEFAULT); + final CreateServiceAccountTokenRequest createServiceAccountTokenRequest = new CreateServiceAccountTokenRequest( + "elastic", + "fleet-server", + "token2" + ); + final CreateServiceAccountTokenResponse createServiceAccountTokenResponse = client.security() + .createServiceAccountToken(createServiceAccountTokenRequest, RequestOptions.DEFAULT); assertThat(createServiceAccountTokenResponse.getName(), equalTo("token2")); { @@ -2917,8 +3052,10 @@ public void testGetServiceAccountCredentials() throws IOException { } { - final GetServiceAccountCredentialsRequest getServiceAccountCredentialsRequest = - new GetServiceAccountCredentialsRequest("elastic", "fleet-server"); + final GetServiceAccountCredentialsRequest getServiceAccountCredentialsRequest = new GetServiceAccountCredentialsRequest( + "elastic", + "fleet-server" + ); ActionListener listener; // tag::get-service-account-credentials-execute-listener @@ -2946,8 +3083,10 @@ public void onFailure(Exception e) { assertNotNull(future.actionGet()); assertThat(future.actionGet().getPrincipal(), equalTo("elastic/fleet-server")); assertThat(future.actionGet().getIndexTokenInfos().size(), greaterThanOrEqualTo(1)); - assertThat(future.actionGet().getIndexTokenInfos().stream().map(ServiceTokenInfo::getName).collect(Collectors.toSet()), - hasItem("token2")); + assertThat( + future.actionGet().getIndexTokenInfos().stream().map(ServiceTokenInfo::getName).collect(Collectors.toSet()), + hasItem("token2") + ); } } @@ -2982,7 +3121,8 @@ public void testDelegatePkiAuthentication() throws Exception { { DelegatePkiAuthenticationRequest request = new DelegatePkiAuthenticationRequest( - Arrays.asList(clientCertificate, intermediateCA)); + Arrays.asList(clientCertificate, intermediateCA) + ); ActionListener listener; //tag::delegate-pki-execute-listener diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SnapshotClientDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SnapshotClientDocumentationIT.java index 5383333b63e34..e47a46b5936aa 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SnapshotClientDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SnapshotClientDocumentationIT.java @@ -38,10 +38,9 @@ import org.elasticsearch.client.indices.CreateIndexRequest; import org.elasticsearch.cluster.SnapshotsInProgress; import org.elasticsearch.cluster.metadata.RepositoryMetadata; -import org.elasticsearch.core.Booleans; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.Booleans; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.repositories.fs.FsRepository; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.snapshots.RestoreInfo; @@ -49,6 +48,7 @@ import org.elasticsearch.snapshots.SnapshotInfo; import org.elasticsearch.snapshots.SnapshotShardFailure; import org.elasticsearch.snapshots.SnapshotState; +import org.elasticsearch.xcontent.XContentType; import java.io.IOException; import java.util.Collections; @@ -296,7 +296,6 @@ public void testRestoreSnapshot() throws IOException { request.includeAliases(false); // <1> // end::restore-snapshot-request-include-aliases - // tag::restore-snapshot-request-indices request.indices("test_index"); // <1> // end::restore-snapshot-request-indices @@ -803,7 +802,7 @@ public void testCloneSnapshot() throws IOException { String sourceSnapshotName = snapshotName; String targetSnapshotName = snapshotName + "_clone"; - String[] indices = new String[]{indexName}; + String[] indices = new String[] { indexName }; // tag::clone-snapshot-request CloneSnapshotRequest request = new CloneSnapshotRequest(repositoryName, sourceSnapshotName, targetSnapshotName, indices); @@ -842,7 +841,12 @@ public void testCloneSnapshotAsync() throws InterruptedException { RestHighLevelClient client = highLevelClient(); { String targetSnapshot = snapshotName + "_clone"; - CloneSnapshotRequest request = new CloneSnapshotRequest(repositoryName, snapshotName, targetSnapshot, new String[]{indexName}); + CloneSnapshotRequest request = new CloneSnapshotRequest( + repositoryName, + snapshotName, + targetSnapshot, + new String[] { indexName } + ); // tag::clone-snapshot-execute-listener ActionListener listener = diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/StoredScriptsDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/StoredScriptsDocumentationIT.java index d6bcf973e3a94..d050af3943a6c 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/StoredScriptsDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/StoredScriptsDocumentationIT.java @@ -21,11 +21,11 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.script.Script; +import org.elasticsearch.script.StoredScriptSource; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.script.Script; -import org.elasticsearch.script.StoredScriptSource; import java.io.IOException; import java.util.Collections; @@ -62,10 +62,11 @@ public class StoredScriptsDocumentationIT extends ESRestHighLevelClientTestCase public void testGetStoredScript() throws Exception { RestHighLevelClient client = highLevelClient(); - final StoredScriptSource scriptSource = - new StoredScriptSource("painless", - "Math.log(_score * 2) + params.my_modifier", - Collections.singletonMap(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType())); + final StoredScriptSource scriptSource = new StoredScriptSource( + "painless", + "Math.log(_score * 2) + params.my_modifier", + Collections.singletonMap(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType()) + ); putStoredScript("calculate-score", scriptSource); @@ -125,10 +126,11 @@ public void onFailure(Exception e) { public void testDeleteStoredScript() throws Exception { RestHighLevelClient client = highLevelClient(); - final StoredScriptSource scriptSource = - new StoredScriptSource("painless", - "Math.log(_score * 2) + params.my_modifier", - Collections.singletonMap(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType())); + final StoredScriptSource scriptSource = new StoredScriptSource( + "painless", + "Math.log(_score * 2) + params.my_modifier", + Collections.singletonMap(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType()) + ); putStoredScript("calculate-score", scriptSource); @@ -233,7 +235,6 @@ public void testPutScript() throws Exception { request.content(BytesReference.bytes(builder), XContentType.JSON); // <1> // end::put-stored-script-content-painless - // tag::put-stored-script-execute AcknowledgedResponse putStoredScriptResponse = client.putScript(request, RequestOptions.DEFAULT); // end::put-stored-script-execute @@ -298,8 +299,7 @@ public void onFailure(Exception e) { } private void putStoredScript(String id, StoredScriptSource scriptSource) throws IOException { - PutStoredScriptRequest request = - new PutStoredScriptRequest(id, "score", new BytesArray("{}"), XContentType.JSON, scriptSource); + PutStoredScriptRequest request = new PutStoredScriptRequest(id, "score", new BytesArray("{}"), XContentType.JSON, scriptSource); assertAcked(execute(request, highLevelClient()::putScript, highLevelClient()::putScriptAsync)); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/TasksClientDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/TasksClientDocumentationIT.java index bf5b2320653f5..c7d434def0dae 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/TasksClientDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/TasksClientDocumentationIT.java @@ -177,7 +177,6 @@ public void testCancelTasks() throws IOException { List groups = response.getTaskGroups(); // <2> // end::cancel-tasks-response-calc - // tag::cancel-tasks-response-failures List nodeFailures = response.getNodeFailures(); // <1> List taskFailures = response.getTaskFailures(); // <2> diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/TextStructureClientDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/TextStructureClientDocumentationIT.java index 5e6a8233f3785..def624c9b3495 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/TextStructureClientDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/TextStructureClientDocumentationIT.java @@ -7,13 +7,6 @@ */ package org.elasticsearch.client.documentation; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.Collections; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; - import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.LatchedActionListener; import org.elasticsearch.client.ESRestHighLevelClientTestCase; @@ -23,6 +16,13 @@ import org.elasticsearch.client.textstructure.FindStructureResponse; import org.elasticsearch.client.textstructure.structurefinder.TextStructure; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Collections; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + @SuppressWarnings("removal") public class TextStructureClientDocumentationIT extends ESRestHighLevelClientTestCase { @@ -30,12 +30,12 @@ public void testFindStructure() throws Exception { RestHighLevelClient client = highLevelClient(); Path anInterestingFile = createTempFile(); - String contents = "{\"logger\":\"controller\",\"timestamp\":1478261151445,\"level\":\"INFO\"," + - "\"pid\":42,\"thread\":\"0x7fff7d2a8000\",\"message\":\"message 1\",\"class\":\"ml\"," + - "\"method\":\"core::SomeNoiseMaker\",\"file\":\"Noisemaker.cc\",\"line\":333}\n" + - "{\"logger\":\"controller\",\"timestamp\":1478261151445," + - "\"level\":\"INFO\",\"pid\":42,\"thread\":\"0x7fff7d2a8000\",\"message\":\"message 2\",\"class\":\"ml\"," + - "\"method\":\"core::SomeNoiseMaker\",\"file\":\"Noisemaker.cc\",\"line\":333}\n"; + String contents = "{\"logger\":\"controller\",\"timestamp\":1478261151445,\"level\":\"INFO\"," + + "\"pid\":42,\"thread\":\"0x7fff7d2a8000\",\"message\":\"message 1\",\"class\":\"ml\"," + + "\"method\":\"core::SomeNoiseMaker\",\"file\":\"Noisemaker.cc\",\"line\":333}\n" + + "{\"logger\":\"controller\",\"timestamp\":1478261151445," + + "\"level\":\"INFO\",\"pid\":42,\"thread\":\"0x7fff7d2a8000\",\"message\":\"message 2\",\"class\":\"ml\"," + + "\"method\":\"core::SomeNoiseMaker\",\"file\":\"Noisemaker.cc\",\"line\":333}\n"; Files.write(anInterestingFile, Collections.singleton(contents), StandardCharsets.UTF_8); { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/WatcherDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/WatcherDocumentationIT.java index 64de29bee3ad4..002d79017fccd 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/WatcherDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/WatcherDocumentationIT.java @@ -15,18 +15,22 @@ import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.Response; import org.elasticsearch.client.RestHighLevelClient; -import org.elasticsearch.client.watcher.ActivateWatchRequest; -import org.elasticsearch.client.watcher.ActivateWatchResponse; import org.elasticsearch.client.watcher.AckWatchRequest; import org.elasticsearch.client.watcher.AckWatchResponse; import org.elasticsearch.client.watcher.ActionStatus; import org.elasticsearch.client.watcher.ActionStatus.AckStatus; +import org.elasticsearch.client.watcher.ActivateWatchRequest; +import org.elasticsearch.client.watcher.ActivateWatchResponse; import org.elasticsearch.client.watcher.DeactivateWatchRequest; import org.elasticsearch.client.watcher.DeactivateWatchResponse; +import org.elasticsearch.client.watcher.DeleteWatchRequest; +import org.elasticsearch.client.watcher.DeleteWatchResponse; import org.elasticsearch.client.watcher.ExecuteWatchRequest; import org.elasticsearch.client.watcher.ExecuteWatchResponse; import org.elasticsearch.client.watcher.GetWatchRequest; import org.elasticsearch.client.watcher.GetWatchResponse; +import org.elasticsearch.client.watcher.PutWatchRequest; +import org.elasticsearch.client.watcher.PutWatchResponse; import org.elasticsearch.client.watcher.StartWatchServiceRequest; import org.elasticsearch.client.watcher.StopWatchServiceRequest; import org.elasticsearch.client.watcher.WatchStatus; @@ -34,13 +38,9 @@ import org.elasticsearch.client.watcher.WatcherStatsResponse; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.rest.RestStatus; import org.elasticsearch.xcontent.ObjectPath; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.client.watcher.DeleteWatchRequest; -import org.elasticsearch.client.watcher.DeleteWatchResponse; -import org.elasticsearch.client.watcher.PutWatchRequest; -import org.elasticsearch.client.watcher.PutWatchResponse; -import org.elasticsearch.rest.RestStatus; import java.util.List; import java.util.Map; @@ -162,11 +162,13 @@ public void testWatcher() throws Exception { } { - BytesReference watch = new BytesArray("{ \n" + - " \"trigger\": { \"schedule\": { \"interval\": \"10h\" } },\n" + - " \"input\": { \"simple\": { \"foo\" : \"bar\" } },\n" + - " \"actions\": { \"logme\": { \"logging\": { \"text\": \"{{ctx.payload}}\" } } }\n" + - "}"); + BytesReference watch = new BytesArray( + "{ \n" + + " \"trigger\": { \"schedule\": { \"interval\": \"10h\" } },\n" + + " \"input\": { \"simple\": { \"foo\" : \"bar\" } },\n" + + " \"actions\": { \"logme\": { \"logging\": { \"text\": \"{{ctx.payload}}\" } } }\n" + + "}" + ); PutWatchRequest request = new PutWatchRequest("my_other_watch_id", watch, XContentType.JSON); // tag::x-pack-put-watch-execute-listener ActionListener listener = new ActionListener() { @@ -352,11 +354,11 @@ public void testExecuteInlineWatch() throws Exception { } { - String watchJson = "{ \n" + - " \"trigger\": { \"schedule\": { \"interval\": \"10h\" } },\n" + - " \"input\": { \"none\": {} },\n" + - " \"actions\": { \"logme\": { \"logging\": { \"text\": \"{{ctx.payload}}\" } } }\n" + - "}"; + String watchJson = "{ \n" + + " \"trigger\": { \"schedule\": { \"interval\": \"10h\" } },\n" + + " \"input\": { \"none\": {} },\n" + + " \"actions\": { \"logme\": { \"logging\": { \"text\": \"{{ctx.payload}}\" } } }\n" + + "}"; ExecuteWatchRequest request = ExecuteWatchRequest.inline(watchJson); // tag::x-pack-execute-watch-inline-execute-listener ActionListener listener = new ActionListener() { @@ -388,11 +390,13 @@ public void testAckWatch() throws Exception { RestHighLevelClient client = highLevelClient(); { - BytesReference watch = new BytesArray("{ \n" + - " \"trigger\": { \"schedule\": { \"interval\": \"10h\" } },\n" + - " \"input\": { \"simple\": { \"foo\" : \"bar\" } },\n" + - " \"actions\": { \"logme\": { \"logging\": { \"text\": \"{{ctx.payload}}\" } } }\n" + - "}"); + BytesReference watch = new BytesArray( + "{ \n" + + " \"trigger\": { \"schedule\": { \"interval\": \"10h\" } },\n" + + " \"input\": { \"simple\": { \"foo\" : \"bar\" } },\n" + + " \"actions\": { \"logme\": { \"logging\": { \"text\": \"{{ctx.payload}}\" } } }\n" + + "}" + ); PutWatchRequest putWatchRequest = new PutWatchRequest("my_watch_id", watch, XContentType.JSON); client.watcher().putWatch(putWatchRequest, RequestOptions.DEFAULT); @@ -454,11 +458,13 @@ public void testDeactivateWatch() throws Exception { RestHighLevelClient client = highLevelClient(); { - BytesReference watch = new BytesArray("{ \n" + - " \"trigger\": { \"schedule\": { \"interval\": \"10h\" } },\n" + - " \"input\": { \"simple\": { \"foo\" : \"bar\" } },\n" + - " \"actions\": { \"logme\": { \"logging\": { \"text\": \"{{ctx.payload}}\" } } }\n" + - "}"); + BytesReference watch = new BytesArray( + "{ \n" + + " \"trigger\": { \"schedule\": { \"interval\": \"10h\" } },\n" + + " \"input\": { \"simple\": { \"foo\" : \"bar\" } },\n" + + " \"actions\": { \"logme\": { \"logging\": { \"text\": \"{{ctx.payload}}\" } } }\n" + + "}" + ); PutWatchRequest putWatchRequest = new PutWatchRequest("my_watch_id", watch, XContentType.JSON); client.watcher().putWatch(putWatchRequest, RequestOptions.DEFAULT); } @@ -500,16 +506,17 @@ public void onFailure(Exception e) { } } - public void testActivateWatch() throws Exception { RestHighLevelClient client = highLevelClient(); { - BytesReference watch = new BytesArray("{ \n" + - " \"trigger\": { \"schedule\": { \"interval\": \"10h\" } },\n" + - " \"input\": { \"simple\": { \"foo\" : \"bar\" } },\n" + - " \"actions\": { \"logme\": { \"logging\": { \"text\": \"{{ctx.payload}}\" } } }\n" + - "}"); + BytesReference watch = new BytesArray( + "{ \n" + + " \"trigger\": { \"schedule\": { \"interval\": \"10h\" } },\n" + + " \"input\": { \"simple\": { \"foo\" : \"bar\" } },\n" + + " \"actions\": { \"logme\": { \"logging\": { \"text\": \"{{ctx.payload}}\" } } }\n" + + "}" + ); PutWatchRequest request = new PutWatchRequest("my_watch_id", watch, XContentType.JSON); request.setActive(false); // <1> PutWatchResponse response = client.watcher().putWatch(request, RequestOptions.DEFAULT); @@ -544,7 +551,7 @@ public void onFailure(Exception e) { }; //end::activate-watch-request-listener - //Replace the empty listener by a blocking listener in test + // Replace the empty listener by a blocking listener in test final CountDownLatch latch = new CountDownLatch(1); listener = new LatchedActionListener<>(listener, latch); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/enrich/ExecutePolicyResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/enrich/ExecutePolicyResponseTests.java index eefb274a90e36..42c5dbbc0acba 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/enrich/ExecutePolicyResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/enrich/ExecutePolicyResponseTests.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.enrich; import org.elasticsearch.client.AbstractResponseTestCase; +import org.elasticsearch.tasks.TaskId; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.tasks.TaskId; import org.elasticsearch.xpack.core.enrich.action.ExecuteEnrichPolicyAction; import org.elasticsearch.xpack.core.enrich.action.ExecuteEnrichPolicyStatus; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/enrich/GetPolicyResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/enrich/GetPolicyResponseTests.java index f97beac8b7e59..836d1adc4b5cd 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/enrich/GetPolicyResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/enrich/GetPolicyResponseTests.java @@ -45,26 +45,35 @@ protected GetPolicyResponse doParseToClientInstance(XContentParser parser) throw protected void assertInstances(GetEnrichPolicyAction.Response serverTestInstance, GetPolicyResponse clientInstance) { assertThat(clientInstance.getPolicies().size(), equalTo(serverTestInstance.getPolicies().size())); for (int i = 0; i < clientInstance.getPolicies().size(); i++) { - assertThat(clientInstance.getPolicies().get(i).getType(), - equalTo(serverTestInstance.getPolicies().get(i).getPolicy().getType())); - assertThat(clientInstance.getPolicies().get(i).getName(), - equalTo(serverTestInstance.getPolicies().get(i).getName())); - assertThat(clientInstance.getPolicies().get(i).getIndices(), - equalTo(serverTestInstance.getPolicies().get(i).getPolicy().getIndices())); - if (clientInstance.getPolicies().get(i).getQuery() != null) { - assertThat(clientInstance.getPolicies().get(i).getQuery(), - equalTo(serverTestInstance.getPolicies().get(i).getPolicy().getQuery().getQuery())); + assertThat( + clientInstance.getPolicies().get(i).getType(), + equalTo(serverTestInstance.getPolicies().get(i).getPolicy().getType()) + ); + assertThat(clientInstance.getPolicies().get(i).getName(), equalTo(serverTestInstance.getPolicies().get(i).getName())); + assertThat( + clientInstance.getPolicies().get(i).getIndices(), + equalTo(serverTestInstance.getPolicies().get(i).getPolicy().getIndices()) + ); + if (clientInstance.getPolicies().get(i).getQuery() != null) { + assertThat( + clientInstance.getPolicies().get(i).getQuery(), + equalTo(serverTestInstance.getPolicies().get(i).getPolicy().getQuery().getQuery()) + ); } else { assertThat(serverTestInstance.getPolicies().get(i).getPolicy().getQuery(), nullValue()); } - assertThat(clientInstance.getPolicies().get(i).getMatchField(), - equalTo(serverTestInstance.getPolicies().get(i).getPolicy().getMatchField())); - assertThat(clientInstance.getPolicies().get(i).getEnrichFields(), - equalTo(serverTestInstance.getPolicies().get(i).getPolicy().getEnrichFields())); + assertThat( + clientInstance.getPolicies().get(i).getMatchField(), + equalTo(serverTestInstance.getPolicies().get(i).getPolicy().getMatchField()) + ); + assertThat( + clientInstance.getPolicies().get(i).getEnrichFields(), + equalTo(serverTestInstance.getPolicies().get(i).getPolicy().getEnrichFields()) + ); } } - private static EnrichPolicy createRandomEnrichPolicy(XContentType xContentType){ + private static EnrichPolicy createRandomEnrichPolicy(XContentType xContentType) { try (XContentBuilder builder = XContentBuilder.builder(xContentType.xContent())) { builder.startObject(); builder.endObject(); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/enrich/PutPolicyRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/enrich/PutPolicyRequestTests.java index 622982b0a0f60..57709c44507dc 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/enrich/PutPolicyRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/enrich/PutPolicyRequestTests.java @@ -8,10 +8,10 @@ package org.elasticsearch.client.enrich; import org.elasticsearch.client.AbstractRequestTestCase; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.query.MatchAllQueryBuilder; import org.elasticsearch.test.EqualsHashCodeTestUtils; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.enrich.action.PutEnrichPolicyAction; import java.io.IOException; @@ -29,16 +29,23 @@ public void testValidate() { PutPolicyRequest request = createClientTestInstance(); assertThat(request.validate().isPresent(), is(false)); - Exception e = expectThrows(IllegalArgumentException.class, - () -> new PutPolicyRequest(request.getName(), request.getType(), request.getIndices(), null, request.getEnrichFields())); + Exception e = expectThrows( + IllegalArgumentException.class, + () -> new PutPolicyRequest(request.getName(), request.getType(), request.getIndices(), null, request.getEnrichFields()) + ); assertThat(e.getMessage(), containsString("matchField must be a non-null and non-empty string")); } public void testEqualsAndHashcode() { PutPolicyRequest testInstance = createTestInstance(); EqualsHashCodeTestUtils.checkEqualsAndHashCode(testInstance, (original) -> { - PutPolicyRequest copy = new PutPolicyRequest(original.getName(), original.getType(), original.getIndices(), - original.getMatchField(), original.getEnrichFields()); + PutPolicyRequest copy = new PutPolicyRequest( + original.getName(), + original.getType(), + original.getIndices(), + original.getMatchField(), + original.getEnrichFields() + ); copy.setQuery(original.getQuery()); return copy; }); @@ -83,8 +90,10 @@ protected void assertInstances(PutEnrichPolicyAction.Request serverInstance, Put assertThat(clientTestInstance.getIndices(), equalTo(serverInstance.getPolicy().getIndices())); if (clientTestInstance.getQuery() != null) { XContentType type = serverInstance.getPolicy().getQuery().getContentType(); - assertThat(PutPolicyRequest.asMap(clientTestInstance.getQuery(), XContentType.JSON), - equalTo(PutPolicyRequest.asMap(serverInstance.getPolicy().getQuery().getQuery(), type))); + assertThat( + PutPolicyRequest.asMap(clientTestInstance.getQuery(), XContentType.JSON), + equalTo(PutPolicyRequest.asMap(serverInstance.getPolicy().getQuery().getQuery(), type)) + ); } else { assertThat(serverInstance.getPolicy().getQuery(), nullValue()); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/enrich/StatsResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/enrich/StatsResponseTests.java index 32256b4daed08..98a0e019cbd6a 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/enrich/StatsResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/enrich/StatsResponseTests.java @@ -8,10 +8,10 @@ package org.elasticsearch.client.enrich; import org.elasticsearch.client.AbstractResponseTestCase; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.tasks.TaskId; import org.elasticsearch.tasks.TaskInfo; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.enrich.action.EnrichStatsAction; import java.io.IOException; @@ -38,12 +38,21 @@ protected EnrichStatsAction.Response createServerTestInstance(XContentType xCont for (int i = 0; i < numCoordinatingStats; i++) { String nodeId = randomAlphaOfLength(4); EnrichStatsAction.Response.CoordinatorStats stats = new EnrichStatsAction.Response.CoordinatorStats( - nodeId, randomIntBetween(0, 8096), randomIntBetween(0, 8096), randomNonNegativeLong(), - randomNonNegativeLong()); + nodeId, + randomIntBetween(0, 8096), + randomIntBetween(0, 8096), + randomNonNegativeLong(), + randomNonNegativeLong() + ); coordinatorStats.add(stats); cacheStats.add( - new EnrichStatsAction.Response.CacheStats(nodeId, randomNonNegativeLong(), randomNonNegativeLong(), - randomNonNegativeLong(), randomNonNegativeLong()) + new EnrichStatsAction.Response.CacheStats( + nodeId, + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong() + ) ); } return new EnrichStatsAction.Response(executingPolicies, coordinatorStats, cacheStats); @@ -97,20 +106,21 @@ private static TaskInfo randomTaskInfo() { boolean cancellable = randomBoolean(); boolean cancelled = cancellable && randomBoolean(); TaskId parentTaskId = TaskId.EMPTY_TASK_ID; - Map headers = randomBoolean() ? - Collections.emptyMap() : - Collections.singletonMap(randomAlphaOfLength(5), randomAlphaOfLength(5)); + Map headers = randomBoolean() + ? Collections.emptyMap() + : Collections.singletonMap(randomAlphaOfLength(5), randomAlphaOfLength(5)); return new TaskInfo( - taskId, - type, - action, - description, - null, - startTime, - runningTimeNanos, - cancellable, - cancelled, - parentTaskId, - headers); + taskId, + type, + action, + description, + null, + startTime, + runningTimeNanos, + cancellable, + cancelled, + parentTaskId, + headers + ); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/eql/EqlSearchRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/eql/EqlSearchRequestTests.java index 72cd8bf6ae775..bd238a1882625 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/eql/EqlSearchRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/eql/EqlSearchRequestTests.java @@ -11,10 +11,10 @@ import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.client.AbstractRequestTestCase; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchModule; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.List; @@ -64,15 +64,18 @@ protected org.elasticsearch.xpack.eql.action.EqlSearchRequest doParseToServerIns } @Override - protected void assertInstances(org.elasticsearch.xpack.eql.action.EqlSearchRequest serverInstance, EqlSearchRequest - clientTestInstance) { + protected void assertInstances( + org.elasticsearch.xpack.eql.action.EqlSearchRequest serverInstance, + EqlSearchRequest clientTestInstance + ) { assertThat(serverInstance.eventCategoryField(), equalTo(clientTestInstance.eventCategoryField())); assertThat(serverInstance.timestampField(), equalTo(clientTestInstance.timestampField())); assertThat(serverInstance.tiebreakerField(), equalTo(clientTestInstance.tiebreakerField())); assertThat(serverInstance.filter(), equalTo(clientTestInstance.filter())); assertThat(serverInstance.query(), equalTo(clientTestInstance.query())); - IndicesOptions actual = clientTestInstance.indicesOptions() == null ? - org.elasticsearch.xpack.eql.action.EqlSearchRequest.DEFAULT_INDICES_OPTIONS : clientTestInstance.indicesOptions(); + IndicesOptions actual = clientTestInstance.indicesOptions() == null + ? org.elasticsearch.xpack.eql.action.EqlSearchRequest.DEFAULT_INDICES_OPTIONS + : clientTestInstance.indicesOptions(); assertThat(serverInstance.indicesOptions(), equalTo(actual)); assertThat(serverInstance.indices(), equalTo(clientTestInstance.indices())); assertThat(serverInstance.fetchSize(), equalTo(clientTestInstance.fetchSize())); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/eql/EqlSearchResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/eql/EqlSearchResponseTests.java index 0387a3a233cdf..4a3a3ed85ca10 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/eql/EqlSearchResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/eql/EqlSearchResponseTests.java @@ -11,16 +11,16 @@ import org.apache.lucene.search.TotalHits; import org.elasticsearch.client.AbstractResponseTestCase; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.document.DocumentField; +import org.elasticsearch.core.Tuple; +import org.elasticsearch.search.lookup.SourceLookup; +import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.RandomObjects; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.search.lookup.SourceLookup; -import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.test.RandomObjects; import java.io.IOException; import java.util.ArrayList; @@ -34,7 +34,8 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; -public class EqlSearchResponseTests extends AbstractResponseTestCase { private static class RandomSource implements ToXContentObject { @@ -97,8 +98,14 @@ static List randomEv if (fetchFields.isEmpty() && randomBoolean()) { fetchFields = null; } - hits.add(new org.elasticsearch.xpack.eql.action.EqlSearchResponse.Event(String.valueOf(i), randomAlphaOfLength(10), bytes, - fetchFields)); + hits.add( + new org.elasticsearch.xpack.eql.action.EqlSearchResponse.Event( + String.valueOf(i), + randomAlphaOfLength(10), + bytes, + fetchFields + ) + ); } } if (randomBoolean()) { @@ -120,10 +127,18 @@ private static Tuple randomDocumentField(XContentT DocumentField listField = new DocumentField(randomAlphaOfLength(5), listValues); return Tuple.tuple(listField, listField); case 2: - List objectValues = randomList(1, 5, () -> - Map.of(randomAlphaOfLength(5), randomInt(), - randomAlphaOfLength(5), randomBoolean(), - randomAlphaOfLength(5), randomAlphaOfLength(10))); + List objectValues = randomList( + 1, + 5, + () -> Map.of( + randomAlphaOfLength(5), + randomInt(), + randomAlphaOfLength(5), + randomBoolean(), + randomAlphaOfLength(5), + randomAlphaOfLength(10) + ) + ); DocumentField objectField = new DocumentField(randomAlphaOfLength(5), objectValues); return Tuple.tuple(objectField, objectField); default: @@ -139,13 +154,21 @@ public static org.elasticsearch.xpack.eql.action.EqlSearchResponse createRandomE if (randomBoolean()) { return new org.elasticsearch.xpack.eql.action.EqlSearchResponse(hits, randomIntBetween(0, 1001), randomBoolean()); } else { - return new org.elasticsearch.xpack.eql.action.EqlSearchResponse(hits, randomIntBetween(0, 1001), randomBoolean(), - randomAlphaOfLength(10), randomBoolean(), randomBoolean()); + return new org.elasticsearch.xpack.eql.action.EqlSearchResponse( + hits, + randomIntBetween(0, 1001), + randomBoolean(), + randomAlphaOfLength(10), + randomBoolean(), + randomBoolean() + ); } } - public static org.elasticsearch.xpack.eql.action.EqlSearchResponse createRandomSequencesResponse(TotalHits totalHits, - XContentType xType) { + public static org.elasticsearch.xpack.eql.action.EqlSearchResponse createRandomSequencesResponse( + TotalHits totalHits, + XContentType xType + ) { int size = randomIntBetween(1, 10); List seq = null; if (randomBoolean()) { @@ -166,17 +189,23 @@ public static org.elasticsearch.xpack.eql.action.EqlSearchResponse createRandomS if (randomBoolean()) { return new org.elasticsearch.xpack.eql.action.EqlSearchResponse(hits, randomIntBetween(0, 1001), randomBoolean()); } else { - return new org.elasticsearch.xpack.eql.action.EqlSearchResponse(hits, randomIntBetween(0, 1001), randomBoolean(), - randomAlphaOfLength(10), randomBoolean(), randomBoolean()); + return new org.elasticsearch.xpack.eql.action.EqlSearchResponse( + hits, + randomIntBetween(0, 1001), + randomBoolean(), + randomAlphaOfLength(10), + randomBoolean(), + randomBoolean() + ); } } private static List> getKeysGenerators() { List> randoms = new ArrayList<>(); randoms.add(() -> generateRandomStringArray(6, 11, false)); - randoms.add(() -> randomArray(0, 6, Integer[]::new, ()-> randomInt())); - randoms.add(() -> randomArray(0, 6, Long[]::new, ()-> randomLong())); - randoms.add(() -> randomArray(0, 6, Boolean[]::new, ()-> randomBoolean())); + randoms.add(() -> randomArray(0, 6, Integer[]::new, () -> randomInt())); + randoms.add(() -> randomArray(0, 6, Long[]::new, () -> randomLong())); + randoms.add(() -> randomArray(0, 6, Boolean[]::new, () -> randomBoolean())); return randoms; } @@ -209,7 +238,9 @@ protected EqlSearchResponse doParseToClientInstance(XContentParser parser) throw @Override protected void assertInstances( - org.elasticsearch.xpack.eql.action.EqlSearchResponse serverTestInstance, EqlSearchResponse clientInstance) { + org.elasticsearch.xpack.eql.action.EqlSearchResponse serverTestInstance, + EqlSearchResponse clientInstance + ) { assertThat(serverTestInstance.took(), is(clientInstance.took())); assertThat(serverTestInstance.isTimeout(), is(clientInstance.isTimeout())); assertThat(serverTestInstance.hits().totalHits(), is(clientInstance.hits().totalHits())); @@ -223,8 +254,10 @@ protected void assertInstances( } else { assertThat(serverTestInstance.hits().sequences().size(), equalTo(clientInstance.hits().sequences().size())); for (int i = 0; i < serverTestInstance.hits().sequences().size(); i++) { - assertThat(serverTestInstance.hits().sequences().get(i).joinKeys(), - is(clientInstance.hits().sequences().get(i).joinKeys())); + assertThat( + serverTestInstance.hits().sequences().get(i).joinKeys(), + is(clientInstance.hits().sequences().get(i).joinKeys()) + ); assertEvents(serverTestInstance.hits().sequences().get(i).events(), clientInstance.hits().sequences().get(i).events()); } } @@ -236,8 +269,7 @@ private void assertEvents( ) { assertThat(serverEvents.size(), equalTo(clientEvents.size())); for (int j = 0; j < serverEvents.size(); j++) { - assertThat( - SourceLookup.sourceAsMap(serverEvents.get(j).source()), is(clientEvents.get(j).sourceAsMap())); + assertThat(SourceLookup.sourceAsMap(serverEvents.get(j).source()), is(clientEvents.get(j).sourceAsMap())); } } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/eql/EqlStatsResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/eql/EqlStatsResponseTests.java index 5f7cd40794931..eed691d7ef59b 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/eql/EqlStatsResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/eql/EqlStatsResponseTests.java @@ -51,8 +51,7 @@ private static Map buildRandomNodeStats(int featuresNumber) { @Override protected EqlStatsResponseToXContent createServerTestInstance(XContentType xContentType) { - NodesResponseHeader header = new NodesResponseHeader(randomInt(10), randomInt(10), - randomInt(10), Collections.emptyList()); + NodesResponseHeader header = new NodesResponseHeader(randomInt(10), randomInt(10), randomInt(10), Collections.emptyList()); String clusterName = randomAlphaOfLength(10); int nodeCount = randomInt(10); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/graph/GraphExploreResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/graph/GraphExploreResponseTests.java index 9b8df87c311f8..e025c11b09958 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/graph/GraphExploreResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/graph/GraphExploreResponseTests.java @@ -11,9 +11,9 @@ import org.elasticsearch.action.ShardOperationFailedException; import org.elasticsearch.action.search.ShardSearchFailure; import org.elasticsearch.client.AbstractResponseTestCase; +import org.elasticsearch.protocol.xpack.graph.Connection.ConnectionId; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.protocol.xpack.graph.Connection.ConnectionId; import org.junit.Assert; import java.io.IOException; @@ -24,8 +24,9 @@ import static org.hamcrest.Matchers.equalTo; -public class GraphExploreResponseTests extends - AbstractResponseTestCase { +public class GraphExploreResponseTests extends AbstractResponseTestCase< + org.elasticsearch.protocol.xpack.graph.GraphExploreResponse, + GraphExploreResponse> { @Override protected org.elasticsearch.protocol.xpack.graph.GraphExploreResponse createServerTestInstance(XContentType xContentType) { @@ -39,61 +40,72 @@ private static org.elasticsearch.protocol.xpack.graph.GraphExploreResponse creat long overallTookInMillis = randomNonNegativeLong(); Map vertices = new HashMap<>(); - Map connections = new HashMap<>(); - ShardOperationFailedException [] failures = new ShardOperationFailedException [numFailures]; + Map connections = new HashMap<>(); + ShardOperationFailedException[] failures = new ShardOperationFailedException[numFailures]; for (int i = 0; i < failures.length; i++) { failures[i] = new ShardSearchFailure(new ElasticsearchException("an error")); } - //Create random set of vertices + // Create random set of vertices for (int i = 0; i < numItems; i++) { - org.elasticsearch.protocol.xpack.graph.Vertex v = new org.elasticsearch.protocol.xpack.graph.Vertex("field1", - randomAlphaOfLength(5), randomDouble(), 0, - showDetails? randomIntBetween(100, 200):0, - showDetails? randomIntBetween(1, 100):0); + org.elasticsearch.protocol.xpack.graph.Vertex v = new org.elasticsearch.protocol.xpack.graph.Vertex( + "field1", + randomAlphaOfLength(5), + randomDouble(), + 0, + showDetails ? randomIntBetween(100, 200) : 0, + showDetails ? randomIntBetween(1, 100) : 0 + ); vertices.put(v.getId(), v); } - //Wire up half the vertices randomly - org.elasticsearch.protocol.xpack.graph.Vertex[] vs = - vertices.values().toArray(new org.elasticsearch.protocol.xpack.graph.Vertex[vertices.size()]); - for (int i = 0; i < numItems/2; i++) { - org.elasticsearch.protocol.xpack.graph.Vertex v1 = vs[randomIntBetween(0, vs.length-1)]; - org.elasticsearch.protocol.xpack.graph.Vertex v2 = vs[randomIntBetween(0, vs.length-1)]; - if(v1 != v2) { - org.elasticsearch.protocol.xpack.graph.Connection conn = new org.elasticsearch.protocol.xpack.graph.Connection(v1, v2, - randomDouble(), randomLongBetween(1, 10)); + // Wire up half the vertices randomly + org.elasticsearch.protocol.xpack.graph.Vertex[] vs = vertices.values() + .toArray(new org.elasticsearch.protocol.xpack.graph.Vertex[vertices.size()]); + for (int i = 0; i < numItems / 2; i++) { + org.elasticsearch.protocol.xpack.graph.Vertex v1 = vs[randomIntBetween(0, vs.length - 1)]; + org.elasticsearch.protocol.xpack.graph.Vertex v2 = vs[randomIntBetween(0, vs.length - 1)]; + if (v1 != v2) { + org.elasticsearch.protocol.xpack.graph.Connection conn = new org.elasticsearch.protocol.xpack.graph.Connection( + v1, + v2, + randomDouble(), + randomLongBetween(1, 10) + ); connections.put(conn.getId(), conn); } } - return new org.elasticsearch.protocol.xpack.graph.GraphExploreResponse(overallTookInMillis, timedOut, failures, - vertices, connections, showDetails); + return new org.elasticsearch.protocol.xpack.graph.GraphExploreResponse( + overallTookInMillis, + timedOut, + failures, + vertices, + connections, + showDetails + ); } - private static org.elasticsearch.protocol.xpack.graph.GraphExploreResponse createTestInstanceWithFailures() { return createInstance(randomIntBetween(1, 128)); } @Override - protected void assertInstances(org.elasticsearch.protocol.xpack.graph.GraphExploreResponse serverTestInstance, - GraphExploreResponse clientInstance) { + protected void assertInstances( + org.elasticsearch.protocol.xpack.graph.GraphExploreResponse serverTestInstance, + GraphExploreResponse clientInstance + ) { Assert.assertThat(serverTestInstance.getTook(), equalTo(clientInstance.getTook())); Assert.assertThat(serverTestInstance.isTimedOut(), equalTo(clientInstance.isTimedOut())); - Comparator serverComparator = - Comparator.comparing(o -> o.getId().toString()); - org.elasticsearch.protocol.xpack.graph.Connection[] serverConns = - serverTestInstance.getConnections().toArray(new org.elasticsearch.protocol.xpack.graph.Connection[0]); - Comparator clientComparator = - Comparator.comparing(o -> o.getId().toString()); - Connection[] clientConns = - clientInstance.getConnections().toArray(new Connection[0]); + Comparator serverComparator = Comparator.comparing(o -> o.getId().toString()); + org.elasticsearch.protocol.xpack.graph.Connection[] serverConns = serverTestInstance.getConnections() + .toArray(new org.elasticsearch.protocol.xpack.graph.Connection[0]); + Comparator clientComparator = Comparator.comparing(o -> o.getId().toString()); + Connection[] clientConns = clientInstance.getConnections().toArray(new Connection[0]); Arrays.sort(serverConns, serverComparator); Arrays.sort(clientConns, clientComparator); assertThat(serverConns.length, equalTo(clientConns.length)); - for (int i = 0; i < clientConns.length ; i++) { + for (int i = 0; i < clientConns.length; i++) { org.elasticsearch.protocol.xpack.graph.Connection serverConn = serverConns[i]; Connection clientConn = clientConns[i]; // String rep since they are different classes @@ -104,10 +116,10 @@ protected void assertInstances(org.elasticsearch.protocol.xpack.graph.GraphExplo assertThat(serverConn.getWeight(), equalTo(clientConn.getWeight())); } - //Sort the vertices lists before equality test (map insertion sequences can cause order differences) + // Sort the vertices lists before equality test (map insertion sequences can cause order differences) Comparator serverVertexComparator = Comparator.comparing(o -> o.getId().toString()); - org.elasticsearch.protocol.xpack.graph.Vertex[] serverVertices = - serverTestInstance.getVertices().toArray(new org.elasticsearch.protocol.xpack.graph.Vertex[0]); + org.elasticsearch.protocol.xpack.graph.Vertex[] serverVertices = serverTestInstance.getVertices() + .toArray(new org.elasticsearch.protocol.xpack.graph.Vertex[0]); Comparator clientVertexComparator = Comparator.comparing(o -> o.getId().toString()); Vertex[] clientVerticies = clientInstance.getVertices().toArray(new Vertex[0]); Arrays.sort(serverVertices, serverVertexComparator); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/AllocateActionTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/AllocateActionTests.java index 5f417d62c6a11..2df29ae185421 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/AllocateActionTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/AllocateActionTests.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ilm; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.util.Collections; import java.util.HashMap; @@ -69,19 +69,31 @@ public void testAllMapsNullOrEmpty() { Map include = randomBoolean() ? null : Collections.emptyMap(); Map exclude = randomBoolean() ? null : Collections.emptyMap(); Map require = randomBoolean() ? null : Collections.emptyMap(); - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, - () -> new AllocateAction(null, include, exclude, require)); - assertEquals("At least one of " + AllocateAction.INCLUDE_FIELD.getPreferredName() + ", " - + AllocateAction.EXCLUDE_FIELD.getPreferredName() + " or " + AllocateAction.REQUIRE_FIELD.getPreferredName() - + "must contain attributes for action " + AllocateAction.NAME, exception.getMessage()); + IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> new AllocateAction(null, include, exclude, require) + ); + assertEquals( + "At least one of " + + AllocateAction.INCLUDE_FIELD.getPreferredName() + + ", " + + AllocateAction.EXCLUDE_FIELD.getPreferredName() + + " or " + + AllocateAction.REQUIRE_FIELD.getPreferredName() + + "must contain attributes for action " + + AllocateAction.NAME, + exception.getMessage() + ); } public void testInvalidNumberOfReplicas() { Map include = randomMap(1, 5); Map exclude = randomBoolean() ? null : Collections.emptyMap(); Map require = randomBoolean() ? null : Collections.emptyMap(); - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, - () -> new AllocateAction(randomIntBetween(-1000, -1), include, exclude, require)); + IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> new AllocateAction(randomIntBetween(-1000, -1), include, exclude, require) + ); assertEquals("[" + AllocateAction.NUMBER_OF_REPLICAS_FIELD.getPreferredName() + "] must be >= 0", exception.getMessage()); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/DeleteActionTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/DeleteActionTests.java index a9107cbfa88aa..38ddcc5899d5b 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/DeleteActionTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/DeleteActionTests.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ilm; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; public class DeleteActionTests extends AbstractXContentTestCase { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/ExplainLifecycleRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/ExplainLifecycleRequestTests.java index 75b7daf8f4199..7cc0751c23f8e 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/ExplainLifecycleRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/ExplainLifecycleRequestTests.java @@ -30,8 +30,16 @@ public void testEmptyIndices() { private ExplainLifecycleRequest createTestInstance() { ExplainLifecycleRequest request = new ExplainLifecycleRequest(generateRandomStringArray(20, 20, false, false)); if (randomBoolean()) { - IndicesOptions indicesOptions = IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), - randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean()); + IndicesOptions indicesOptions = IndicesOptions.fromOptions( + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean() + ); request.indicesOptions(indicesOptions); } return request; @@ -41,16 +49,29 @@ private ExplainLifecycleRequest mutateInstance(ExplainLifecycleRequest instance) String[] indices = instance.getIndices(); IndicesOptions indicesOptions = instance.indicesOptions(); switch (between(0, 1)) { - case 0: - indices = randomValueOtherThanMany(i -> Arrays.equals(i, instance.getIndices()), - () -> generateRandomStringArray(20, 10, false, false)); - break; - case 1: - indicesOptions = randomValueOtherThan(indicesOptions, () -> IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), - randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean())); - break; - default: - throw new AssertionError("Illegal randomisation branch"); + case 0: + indices = randomValueOtherThanMany( + i -> Arrays.equals(i, instance.getIndices()), + () -> generateRandomStringArray(20, 10, false, false) + ); + break; + case 1: + indicesOptions = randomValueOtherThan( + indicesOptions, + () -> IndicesOptions.fromOptions( + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean() + ) + ); + break; + default: + throw new AssertionError("Illegal randomisation branch"); } ExplainLifecycleRequest newRequest = new ExplainLifecycleRequest(indices); newRequest.indicesOptions(indicesOptions); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/ExplainLifecycleResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/ExplainLifecycleResponseTests.java index b273aaf1a058a..3984e74f39b4d 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/ExplainLifecycleResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/ExplainLifecycleResponseTests.java @@ -8,11 +8,11 @@ package org.elasticsearch.client.ilm; import org.elasticsearch.cluster.ClusterModule; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.util.CollectionUtils; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; import java.util.HashMap; @@ -47,7 +47,11 @@ protected boolean assertToXContentEquivalence() { @Override protected NamedXContentRegistry xContentRegistry() { - return new NamedXContentRegistry(CollectionUtils.appendToCopy(ClusterModule.getNamedXWriteables(), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse))); + return new NamedXContentRegistry( + CollectionUtils.appendToCopy( + ClusterModule.getNamedXWriteables(), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse) + ) + ); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/ForceMergeActionTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/ForceMergeActionTests.java index cc9bb001d2082..bfb61457fd081 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/ForceMergeActionTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/ForceMergeActionTests.java @@ -8,12 +8,12 @@ package org.elasticsearch.client.ilm; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; @@ -42,8 +42,12 @@ static ForceMergeAction randomInstance() { public void testMissingMaxNumSegments() throws IOException { BytesReference emptyObject = BytesReference.bytes(JsonXContent.contentBuilder().startObject().endObject()); - XContentParser parser = XContentHelper.createParser(null, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, - emptyObject, XContentType.JSON); + XContentParser parser = XContentHelper.createParser( + null, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + emptyObject, + XContentType.JSON + ); Exception e = expectThrows(IllegalArgumentException.class, () -> ForceMergeAction.parse(parser)); assertThat(e.getMessage(), equalTo("Required [max_num_segments]")); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/FreezeActionTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/FreezeActionTests.java index 4061a6a937f11..075f3602a96d8 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/FreezeActionTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/FreezeActionTests.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ilm; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; public class FreezeActionTests extends AbstractXContentTestCase { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/GetLifecyclePolicyRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/GetLifecyclePolicyRequestTests.java index 81f366c93f062..1e704436d10c2 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/GetLifecyclePolicyRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/GetLifecyclePolicyRequestTests.java @@ -27,8 +27,10 @@ public void testValidation() { } public void testNullPolicyNameShouldFail() { - expectThrows(IllegalArgumentException.class, - () -> new GetLifecyclePolicyRequest(randomAlphaOfLengthBetween(2,20), null, randomAlphaOfLengthBetween(2,20))); + expectThrows( + IllegalArgumentException.class, + () -> new GetLifecyclePolicyRequest(randomAlphaOfLengthBetween(2, 20), null, randomAlphaOfLengthBetween(2, 20)) + ); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/GetLifecyclePolicyResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/GetLifecyclePolicyResponseTests.java index e79e6c3376507..807bd2130177e 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/GetLifecyclePolicyResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/GetLifecyclePolicyResponseTests.java @@ -9,11 +9,11 @@ package org.elasticsearch.client.ilm; import org.elasticsearch.cluster.ClusterModule; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.collect.ImmutableOpenMap; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; import java.util.ArrayList; @@ -50,8 +50,8 @@ protected boolean supportsUnknownFields() { @Override protected Predicate getRandomFieldsExcludeFilter() { return (field) -> - // phases is a list of Phase parsable entries only - field.endsWith(".phases") + // phases is a list of Phase parsable entries only + field.endsWith(".phases") // these are all meant to be maps of strings, so complex objects will confuse the parser || field.endsWith(".include") || field.endsWith(".exclude") @@ -66,21 +66,29 @@ protected Predicate getRandomFieldsExcludeFilter() { @Override protected NamedXContentRegistry xContentRegistry() { List entries = new ArrayList<>(ClusterModule.getNamedXWriteables()); - entries.addAll(Arrays.asList( - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(AllocateAction.NAME), AllocateAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ForceMergeAction.NAME), ForceMergeAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ReadOnlyAction.NAME), ReadOnlyAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(RolloverAction.NAME), RolloverAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ShrinkAction.NAME), ShrinkAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(FreezeAction.NAME), FreezeAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(WaitForSnapshotAction.NAME), - WaitForSnapshotAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(SetPriorityAction.NAME), SetPriorityAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(SearchableSnapshotAction.NAME), - SearchableSnapshotAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(UnfollowAction.NAME), UnfollowAction::parse) - )); + entries.addAll( + Arrays.asList( + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(AllocateAction.NAME), AllocateAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ForceMergeAction.NAME), ForceMergeAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ReadOnlyAction.NAME), ReadOnlyAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(RolloverAction.NAME), RolloverAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ShrinkAction.NAME), ShrinkAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(FreezeAction.NAME), FreezeAction::parse), + new NamedXContentRegistry.Entry( + LifecycleAction.class, + new ParseField(WaitForSnapshotAction.NAME), + WaitForSnapshotAction::parse + ), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(SetPriorityAction.NAME), SetPriorityAction::parse), + new NamedXContentRegistry.Entry( + LifecycleAction.class, + new ParseField(SearchableSnapshotAction.NAME), + SearchableSnapshotAction::parse + ), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(UnfollowAction.NAME), UnfollowAction::parse) + ) + ); return new NamedXContentRegistry(entries); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/IndexLifecycleExplainResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/IndexLifecycleExplainResponseTests.java index e5a343e065a9b..e65a8847fd343 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/IndexLifecycleExplainResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/IndexLifecycleExplainResponseTests.java @@ -9,15 +9,15 @@ package org.elasticsearch.client.ilm; import org.elasticsearch.cluster.ClusterModule; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.util.CollectionUtils; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; import java.util.Objects; @@ -43,7 +43,8 @@ private static IndexLifecycleExplainResponse randomUnmanagedIndexExplainResponse private static IndexLifecycleExplainResponse randomManagedIndexExplainResponse() { boolean stepNull = randomBoolean(); - return IndexLifecycleExplainResponse.newManagedIndexResponse(randomAlphaOfLength(10), + return IndexLifecycleExplainResponse.newManagedIndexResponse( + randomAlphaOfLength(10), randomAlphaOfLength(10), randomBoolean() ? null : randomLongBetween(0, System.currentTimeMillis()), stepNull ? null : randomAlphaOfLength(10), @@ -54,13 +55,16 @@ private static IndexLifecycleExplainResponse randomManagedIndexExplainResponse() stepNull ? null : randomNonNegativeLong(), stepNull ? null : randomNonNegativeLong(), randomBoolean() ? null : new BytesArray(new RandomStepInfo(() -> randomAlphaOfLength(10)).toString()), - randomBoolean() ? null : PhaseExecutionInfoTests.randomPhaseExecutionInfo("")); + randomBoolean() ? null : PhaseExecutionInfoTests.randomPhaseExecutionInfo("") + ); } public void testInvalidStepDetails() { final int numNull = randomIntBetween(1, 3); - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> - IndexLifecycleExplainResponse.newManagedIndexResponse(randomAlphaOfLength(10), + IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> IndexLifecycleExplainResponse.newManagedIndexResponse( + randomAlphaOfLength(10), randomAlphaOfLength(10), randomBoolean() ? null : randomNonNegativeLong(), (numNull == 1) ? null : randomAlphaOfLength(10), @@ -71,7 +75,9 @@ public void testInvalidStepDetails() { randomBoolean() ? null : randomNonNegativeLong(), randomBoolean() ? null : randomNonNegativeLong(), randomBoolean() ? null : new BytesArray(new RandomStepInfo(() -> randomAlphaOfLength(10)).toString()), - randomBoolean() ? null : PhaseExecutionInfoTests.randomPhaseExecutionInfo(""))); + randomBoolean() ? null : PhaseExecutionInfoTests.randomPhaseExecutionInfo("") + ) + ); assertThat(exception.getMessage(), startsWith("managed index response must have complete step details")); assertThat(exception.getMessage(), containsString("=null")); } @@ -99,8 +105,8 @@ protected boolean assertToXContentEquivalence() { @Override protected Predicate getRandomFieldsExcludeFilter() { return (field) -> - // actions are plucked from the named registry, and it fails if the action is not in the named registry - field.endsWith("phase_definition.actions") + // actions are plucked from the named registry, and it fails if the action is not in the named registry + field.endsWith("phase_definition.actions") // This is a bytes reference, so any new fields are tested for equality in this bytes reference. || field.contains("step_info"); } @@ -148,7 +154,11 @@ public String toString() { @Override protected NamedXContentRegistry xContentRegistry() { - return new NamedXContentRegistry(CollectionUtils.appendToCopy(ClusterModule.getNamedXWriteables(), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse))); + return new NamedXContentRegistry( + CollectionUtils.appendToCopy( + ClusterModule.getNamedXWriteables(), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse) + ) + ); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/LifecycleManagementStatusResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/LifecycleManagementStatusResponseTests.java index 605f4a2045efb..a5ca4aab3c7d1 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/LifecycleManagementStatusResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/LifecycleManagementStatusResponseTests.java @@ -8,12 +8,12 @@ package org.elasticsearch.client.ilm; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; import org.hamcrest.CoreMatchers; import java.io.IOException; @@ -25,13 +25,12 @@ public class LifecycleManagementStatusResponseTests extends ESTestCase { public void testFromXContent() throws IOException { - xContentTester(this::createParser, + xContentTester( + this::createParser, LifecycleManagementStatusResponseTests::createTestInstance, LifecycleManagementStatusResponseTests::toXContent, - LifecycleManagementStatusResponse::fromXContent) - .supportsUnknownFields(true) - .assertToXContentEquivalence(false) - .test(); + LifecycleManagementStatusResponse::fromXContent + ).supportsUnknownFields(true).assertToXContentEquivalence(false).test(); } private static XContentBuilder toXContent(LifecycleManagementStatusResponse response, XContentBuilder builder) throws IOException { @@ -52,18 +51,25 @@ public void testAllValidStatuses() { public void testXContent() throws IOException { XContentType xContentType = XContentType.JSON; - String mode = randomFrom(EnumSet.allOf(OperationMode.class) - .stream().map(Enum::name).collect(Collectors.toList())); - XContentParser parser = xContentType.xContent().createParser(NamedXContentRegistry.EMPTY, - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, "{\"operation_mode\" : \"" + mode + "\"}"); + String mode = randomFrom(EnumSet.allOf(OperationMode.class).stream().map(Enum::name).collect(Collectors.toList())); + XContentParser parser = xContentType.xContent() + .createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + "{\"operation_mode\" : \"" + mode + "\"}" + ); assertEquals(LifecycleManagementStatusResponse.fromXContent(parser).getOperationMode(), OperationMode.fromString(mode)); } public void testXContentInvalid() throws IOException { XContentType xContentType = XContentType.JSON; String mode = randomAlphaOfLength(10); - XContentParser parser = xContentType.xContent().createParser(NamedXContentRegistry.EMPTY, - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, "{\"operation_mode\" : \"" + mode + "\"}"); + XContentParser parser = xContentType.xContent() + .createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + "{\"operation_mode\" : \"" + mode + "\"}" + ); Exception e = expectThrows(IllegalArgumentException.class, () -> LifecycleManagementStatusResponse.fromXContent(parser)); assertThat(e.getMessage(), CoreMatchers.containsString("failed to parse field [operation_mode]")); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/LifecyclePolicyMetadataTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/LifecyclePolicyMetadataTests.java index 8891092b82d9c..8377abbb51185 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/LifecyclePolicyMetadataTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/LifecyclePolicyMetadataTests.java @@ -9,10 +9,10 @@ package org.elasticsearch.client.ilm; import org.elasticsearch.cluster.ClusterModule; -import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; import java.util.ArrayList; @@ -28,7 +28,7 @@ public class LifecyclePolicyMetadataTests extends AbstractXContentTestCase getRandomFieldsExcludeFilter() { return (field) -> - // phases is a list of Phase parsable entries only - field.endsWith(".phases") + // phases is a list of Phase parsable entries only + field.endsWith(".phases") // these are all meant to be maps of strings, so complex objects will confuse the parser || field.endsWith(".include") || field.endsWith(".exclude") @@ -60,21 +60,29 @@ protected Predicate getRandomFieldsExcludeFilter() { @Override protected NamedXContentRegistry xContentRegistry() { List entries = new ArrayList<>(ClusterModule.getNamedXWriteables()); - entries.addAll(Arrays.asList( - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(AllocateAction.NAME), AllocateAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ForceMergeAction.NAME), ForceMergeAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ReadOnlyAction.NAME), ReadOnlyAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(RolloverAction.NAME), RolloverAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ShrinkAction.NAME), ShrinkAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(WaitForSnapshotAction.NAME), - WaitForSnapshotAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(FreezeAction.NAME), FreezeAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(SetPriorityAction.NAME), SetPriorityAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(SearchableSnapshotAction.NAME), - SearchableSnapshotAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(UnfollowAction.NAME), UnfollowAction::parse) - )); + entries.addAll( + Arrays.asList( + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(AllocateAction.NAME), AllocateAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ForceMergeAction.NAME), ForceMergeAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ReadOnlyAction.NAME), ReadOnlyAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(RolloverAction.NAME), RolloverAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ShrinkAction.NAME), ShrinkAction::parse), + new NamedXContentRegistry.Entry( + LifecycleAction.class, + new ParseField(WaitForSnapshotAction.NAME), + WaitForSnapshotAction::parse + ), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(FreezeAction.NAME), FreezeAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(SetPriorityAction.NAME), SetPriorityAction::parse), + new NamedXContentRegistry.Entry( + LifecycleAction.class, + new ParseField(SearchableSnapshotAction.NAME), + SearchableSnapshotAction::parse + ), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(UnfollowAction.NAME), UnfollowAction::parse) + ) + ); return new NamedXContentRegistry(entries); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/LifecyclePolicyTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/LifecyclePolicyTests.java index 4bca3a0059c09..354a3d04c0a5f 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/LifecyclePolicyTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/LifecyclePolicyTests.java @@ -8,12 +8,12 @@ package org.elasticsearch.client.ilm; import org.elasticsearch.cluster.ClusterModule; -import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.set.Sets; +import org.elasticsearch.core.TimeValue; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; import java.util.ArrayList; import java.util.Arrays; @@ -30,10 +30,21 @@ public class LifecyclePolicyTests extends AbstractXContentTestCase { private static final Set VALID_HOT_ACTIONS = Sets.newHashSet(UnfollowAction.NAME, SetPriorityAction.NAME, RolloverAction.NAME); - private static final Set VALID_WARM_ACTIONS = Sets.newHashSet(UnfollowAction.NAME, SetPriorityAction.NAME, AllocateAction.NAME, - ForceMergeAction.NAME, ReadOnlyAction.NAME, ShrinkAction.NAME); - private static final Set VALID_COLD_ACTIONS = Sets.newHashSet(UnfollowAction.NAME, SetPriorityAction.NAME, AllocateAction.NAME, - FreezeAction.NAME, SearchableSnapshotAction.NAME); + private static final Set VALID_WARM_ACTIONS = Sets.newHashSet( + UnfollowAction.NAME, + SetPriorityAction.NAME, + AllocateAction.NAME, + ForceMergeAction.NAME, + ReadOnlyAction.NAME, + ShrinkAction.NAME + ); + private static final Set VALID_COLD_ACTIONS = Sets.newHashSet( + UnfollowAction.NAME, + SetPriorityAction.NAME, + AllocateAction.NAME, + FreezeAction.NAME, + SearchableSnapshotAction.NAME + ); private static final Set VALID_DELETE_ACTIONS = Sets.newHashSet(DeleteAction.NAME, WaitForSnapshotAction.NAME); private String lifecycleName; @@ -57,21 +68,29 @@ protected Predicate getRandomFieldsExcludeFilter() { @Override protected NamedXContentRegistry xContentRegistry() { List entries = new ArrayList<>(ClusterModule.getNamedXWriteables()); - entries.addAll(Arrays.asList( - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(AllocateAction.NAME), AllocateAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ForceMergeAction.NAME), ForceMergeAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ReadOnlyAction.NAME), ReadOnlyAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(RolloverAction.NAME), RolloverAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ShrinkAction.NAME), ShrinkAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(WaitForSnapshotAction.NAME), - WaitForSnapshotAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(FreezeAction.NAME), FreezeAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(SetPriorityAction.NAME), SetPriorityAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(SearchableSnapshotAction.NAME), - SearchableSnapshotAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(UnfollowAction.NAME), UnfollowAction::parse) - )); + entries.addAll( + Arrays.asList( + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(AllocateAction.NAME), AllocateAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ForceMergeAction.NAME), ForceMergeAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ReadOnlyAction.NAME), ReadOnlyAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(RolloverAction.NAME), RolloverAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ShrinkAction.NAME), ShrinkAction::parse), + new NamedXContentRegistry.Entry( + LifecycleAction.class, + new ParseField(WaitForSnapshotAction.NAME), + WaitForSnapshotAction::parse + ), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(FreezeAction.NAME), FreezeAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(SetPriorityAction.NAME), SetPriorityAction::parse), + new NamedXContentRegistry.Entry( + LifecycleAction.class, + new ParseField(SearchableSnapshotAction.NAME), + SearchableSnapshotAction::parse + ), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(UnfollowAction.NAME), UnfollowAction::parse) + ) + ); return new NamedXContentRegistry(entries); } @@ -87,9 +106,14 @@ public void testValidatePhases() { if (invalid) { phaseName += randomAlphaOfLength(5); } - Map phases = Collections.singletonMap(phaseName, - new Phase(phaseName, TimeValue.ZERO, phaseName.equals("delete") ? Collections.singletonMap(DeleteAction.NAME, - new DeleteAction()) : Collections.emptyMap())); + Map phases = Collections.singletonMap( + phaseName, + new Phase( + phaseName, + TimeValue.ZERO, + phaseName.equals("delete") ? Collections.singletonMap(DeleteAction.NAME, new DeleteAction()) : Collections.emptyMap() + ) + ); if (invalid) { Exception e = expectThrows(IllegalArgumentException.class, () -> new LifecyclePolicy(lifecycleName, phases)); assertThat(e.getMessage(), equalTo("Lifecycle does not support phase [" + phaseName + "]")); @@ -100,20 +124,18 @@ public void testValidatePhases() { public void testValidateHotPhase() { LifecycleAction invalidAction = null; - Map actions = randomSubsetOf(VALID_HOT_ACTIONS) - .stream().map(this::getTestAction).collect(Collectors.toMap(LifecycleAction::getName, Function.identity())); + Map actions = randomSubsetOf(VALID_HOT_ACTIONS).stream() + .map(this::getTestAction) + .collect(Collectors.toMap(LifecycleAction::getName, Function.identity())); if (randomBoolean()) { invalidAction = getTestAction(randomFrom("allocate", "forcemerge", "delete", "shrink")); actions.put(invalidAction.getName(), invalidAction); } - Map hotPhase = Collections.singletonMap("hot", - new Phase("hot", TimeValue.ZERO, actions)); + Map hotPhase = Collections.singletonMap("hot", new Phase("hot", TimeValue.ZERO, actions)); if (invalidAction != null) { - Exception e = expectThrows(IllegalArgumentException.class, - () -> new LifecyclePolicy(lifecycleName, hotPhase)); - assertThat(e.getMessage(), - equalTo("invalid action [" + invalidAction.getName() + "] defined in phase [hot]")); + Exception e = expectThrows(IllegalArgumentException.class, () -> new LifecyclePolicy(lifecycleName, hotPhase)); + assertThat(e.getMessage(), equalTo("invalid action [" + invalidAction.getName() + "] defined in phase [hot]")); } else { new LifecyclePolicy(lifecycleName, hotPhase); } @@ -121,20 +143,18 @@ public void testValidateHotPhase() { public void testValidateWarmPhase() { LifecycleAction invalidAction = null; - Map actions = randomSubsetOf(VALID_WARM_ACTIONS) - .stream().map(this::getTestAction).collect(Collectors.toMap(LifecycleAction::getName, Function.identity())); + Map actions = randomSubsetOf(VALID_WARM_ACTIONS).stream() + .map(this::getTestAction) + .collect(Collectors.toMap(LifecycleAction::getName, Function.identity())); if (randomBoolean()) { invalidAction = getTestAction(randomFrom("rollover", "delete")); actions.put(invalidAction.getName(), invalidAction); } - Map warmPhase = Collections.singletonMap("warm", - new Phase("warm", TimeValue.ZERO, actions)); + Map warmPhase = Collections.singletonMap("warm", new Phase("warm", TimeValue.ZERO, actions)); if (invalidAction != null) { - Exception e = expectThrows(IllegalArgumentException.class, - () -> new LifecyclePolicy(lifecycleName, warmPhase)); - assertThat(e.getMessage(), - equalTo("invalid action [" + invalidAction.getName() + "] defined in phase [warm]")); + Exception e = expectThrows(IllegalArgumentException.class, () -> new LifecyclePolicy(lifecycleName, warmPhase)); + assertThat(e.getMessage(), equalTo("invalid action [" + invalidAction.getName() + "] defined in phase [warm]")); } else { new LifecyclePolicy(lifecycleName, warmPhase); } @@ -142,20 +162,18 @@ public void testValidateWarmPhase() { public void testValidateColdPhase() { LifecycleAction invalidAction = null; - Map actions = randomSubsetOf(VALID_COLD_ACTIONS) - .stream().map(this::getTestAction).collect(Collectors.toMap(LifecycleAction::getName, Function.identity())); + Map actions = randomSubsetOf(VALID_COLD_ACTIONS).stream() + .map(this::getTestAction) + .collect(Collectors.toMap(LifecycleAction::getName, Function.identity())); if (randomBoolean()) { invalidAction = getTestAction(randomFrom("rollover", "delete", "forcemerge", "shrink")); actions.put(invalidAction.getName(), invalidAction); } - Map coldPhase = Collections.singletonMap("cold", - new Phase("cold", TimeValue.ZERO, actions)); + Map coldPhase = Collections.singletonMap("cold", new Phase("cold", TimeValue.ZERO, actions)); if (invalidAction != null) { - Exception e = expectThrows(IllegalArgumentException.class, - () -> new LifecyclePolicy(lifecycleName, coldPhase)); - assertThat(e.getMessage(), - equalTo("invalid action [" + invalidAction.getName() + "] defined in phase [cold]")); + Exception e = expectThrows(IllegalArgumentException.class, () -> new LifecyclePolicy(lifecycleName, coldPhase)); + assertThat(e.getMessage(), equalTo("invalid action [" + invalidAction.getName() + "] defined in phase [cold]")); } else { new LifecyclePolicy(lifecycleName, coldPhase); } @@ -163,20 +181,18 @@ public void testValidateColdPhase() { public void testValidateDeletePhase() { LifecycleAction invalidAction = null; - Map actions = VALID_DELETE_ACTIONS - .stream().map(this::getTestAction).collect(Collectors.toMap(LifecycleAction::getName, Function.identity())); + Map actions = VALID_DELETE_ACTIONS.stream() + .map(this::getTestAction) + .collect(Collectors.toMap(LifecycleAction::getName, Function.identity())); if (randomBoolean()) { invalidAction = getTestAction(randomFrom("allocate", "rollover", "forcemerge", "shrink")); actions.put(invalidAction.getName(), invalidAction); } - Map deletePhase = Collections.singletonMap("delete", - new Phase("delete", TimeValue.ZERO, actions)); + Map deletePhase = Collections.singletonMap("delete", new Phase("delete", TimeValue.ZERO, actions)); if (invalidAction != null) { - Exception e = expectThrows(IllegalArgumentException.class, - () -> new LifecyclePolicy(lifecycleName, deletePhase)); - assertThat(e.getMessage(), - equalTo("invalid action [" + invalidAction.getName() + "] defined in phase [delete]")); + Exception e = expectThrows(IllegalArgumentException.class, () -> new LifecyclePolicy(lifecycleName, deletePhase)); + assertThat(e.getMessage(), equalTo("invalid action [" + invalidAction.getName() + "] defined in phase [delete]")); } else { new LifecyclePolicy(lifecycleName, deletePhase); } @@ -188,15 +204,14 @@ public void testValidateEmptyDeletePhase() { Phase delete = new Phase("delete", TimeValue.ZERO, actions); Map phases = Collections.singletonMap("delete", delete); - Exception e = expectThrows(IllegalArgumentException.class, - () -> new LifecyclePolicy(lifecycleName, phases)); + Exception e = expectThrows(IllegalArgumentException.class, () -> new LifecyclePolicy(lifecycleName, phases)); assertThat(e.getMessage(), equalTo("phase [" + delete.getName() + "] must define actions")); } public static LifecyclePolicy createRandomPolicy(String lifecycleName) { List phaseNames = Arrays.asList("hot", "warm", "cold", "delete"); Map phases = new HashMap<>(phaseNames.size()); - Function> validActions = (phase) -> { + Function> validActions = (phase) -> { switch (phase) { case "hot": return VALID_HOT_ACTIONS; @@ -208,7 +223,8 @@ public static LifecyclePolicy createRandomPolicy(String lifecycleName) { return VALID_DELETE_ACTIONS; default: throw new IllegalArgumentException("invalid phase [" + phase + "]"); - }}; + } + }; Function allowEmptyActions = (phase) -> { switch (phase) { case "hot": @@ -219,8 +235,9 @@ public static LifecyclePolicy createRandomPolicy(String lifecycleName) { return false; default: throw new IllegalArgumentException("invalid phase [" + phase + "]"); - }}; - Function randomAction = (action) -> { + } + }; + Function randomAction = (action) -> { switch (action) { case AllocateAction.NAME: return AllocateActionTests.randomInstance(); @@ -246,11 +263,13 @@ public static LifecyclePolicy createRandomPolicy(String lifecycleName) { return SearchableSnapshotActionTests.randomInstance(); default: throw new IllegalArgumentException("invalid action [" + action + "]"); - }}; + } + }; TimeValue prev = null; for (String phase : phaseNames) { - TimeValue after = prev == null ? TimeValue.parseTimeValue(randomTimeValue(0, 10000, "s", "m", "h", "d"), "test_after") : - TimeValue.timeValueSeconds(prev.seconds() + randomIntBetween(60, 600)); + TimeValue after = prev == null + ? TimeValue.parseTimeValue(randomTimeValue(0, 10000, "s", "m", "h", "d"), "test_after") + : TimeValue.timeValueSeconds(prev.seconds() + randomIntBetween(60, 600)); prev = after; Map actions = new HashMap<>(); List actionNames; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/MigrateActionTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/MigrateActionTests.java index 18a3d306f8b1b..cc897b5f8f6bc 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/MigrateActionTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/MigrateActionTests.java @@ -7,9 +7,9 @@ */ package org.elasticsearch.client.ilm; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.test.EqualsHashCodeTestUtils; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -35,8 +35,10 @@ protected boolean supportsUnknownFields() { } public void testEqualsHashCode() { - EqualsHashCodeTestUtils.checkEqualsAndHashCode(createTestInstance(), + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + createTestInstance(), m -> new MigrateAction(m.isEnabled()), - m -> new MigrateAction(m.isEnabled() == false)); + m -> new MigrateAction(m.isEnabled() == false) + ); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/PhaseExecutionInfoTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/PhaseExecutionInfoTests.java index 74ec5ab1783ed..511db024c042d 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/PhaseExecutionInfoTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/PhaseExecutionInfoTests.java @@ -8,11 +8,11 @@ package org.elasticsearch.client.ilm; import org.elasticsearch.cluster.ClusterModule; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.util.CollectionUtils; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; import org.junit.Before; import java.io.IOException; @@ -21,8 +21,12 @@ public class PhaseExecutionInfoTests extends AbstractXContentTestCase { static PhaseExecutionInfo randomPhaseExecutionInfo(String phaseName) { - return new PhaseExecutionInfo(randomAlphaOfLength(5), PhaseTests.randomPhase(phaseName), - randomNonNegativeLong(), randomNonNegativeLong()); + return new PhaseExecutionInfo( + randomAlphaOfLength(5), + PhaseTests.randomPhase(phaseName), + randomNonNegativeLong(), + randomNonNegativeLong() + ); } String phaseName; @@ -55,7 +59,11 @@ protected boolean supportsUnknownFields() { @Override protected NamedXContentRegistry xContentRegistry() { - return new NamedXContentRegistry(CollectionUtils.appendToCopy(ClusterModule.getNamedXWriteables(), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse))); + return new NamedXContentRegistry( + CollectionUtils.appendToCopy( + ClusterModule.getNamedXWriteables(), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse) + ) + ); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/PhaseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/PhaseTests.java index b7b7e2c7c4521..f92e016d222f9 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/PhaseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/PhaseTests.java @@ -8,12 +8,12 @@ package org.elasticsearch.client.ilm; import org.elasticsearch.cluster.ClusterModule; -import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.CollectionUtils; +import org.elasticsearch.core.TimeValue; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; import org.junit.Before; import java.util.Collections; @@ -58,8 +58,12 @@ protected Predicate getRandomFieldsExcludeFilter() { @Override protected NamedXContentRegistry xContentRegistry() { - return new NamedXContentRegistry(CollectionUtils.appendToCopy(ClusterModule.getNamedXWriteables(), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse))); + return new NamedXContentRegistry( + CollectionUtils.appendToCopy( + ClusterModule.getNamedXWriteables(), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse) + ) + ); } @Override diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/ReadOnlyActionTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/ReadOnlyActionTests.java index aa718ff952a3b..cd8621008a407 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/ReadOnlyActionTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/ReadOnlyActionTests.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ilm; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; public class ReadOnlyActionTests extends AbstractXContentTestCase { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/RemoveIndexLifecyclePolicyRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/RemoveIndexLifecyclePolicyRequestTests.java index ee7bb86e7f319..6d71e266bb626 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/RemoveIndexLifecyclePolicyRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/RemoveIndexLifecyclePolicyRequestTests.java @@ -33,9 +33,19 @@ public void testValidate() { protected RemoveIndexLifecyclePolicyRequest createInstance() { if (randomBoolean()) { - return new RemoveIndexLifecyclePolicyRequest(Arrays.asList(generateRandomStringArray(20, 20, false)), - IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), - randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean())); + return new RemoveIndexLifecyclePolicyRequest( + Arrays.asList(generateRandomStringArray(20, 20, false)), + IndicesOptions.fromOptions( + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean() + ) + ); } else { return new RemoveIndexLifecyclePolicyRequest(Arrays.asList(generateRandomStringArray(20, 20, false))); } @@ -43,11 +53,19 @@ protected RemoveIndexLifecyclePolicyRequest createInstance() { private RemoveIndexLifecyclePolicyRequest copyInstance(RemoveIndexLifecyclePolicyRequest req) { if (req.indicesOptions() != null) { - return new RemoveIndexLifecyclePolicyRequest(new ArrayList<>(req.indices()), IndicesOptions.fromOptions( - req.indicesOptions().ignoreUnavailable(), req.indicesOptions().allowNoIndices(), - req.indicesOptions().expandWildcardsOpen(), req.indicesOptions().expandWildcardsClosed(), - req.indicesOptions().allowAliasesToMultipleIndices(), req.indicesOptions().forbidClosedIndices(), - req.indicesOptions().ignoreAliases(), req.indicesOptions().ignoreThrottled())); + return new RemoveIndexLifecyclePolicyRequest( + new ArrayList<>(req.indices()), + IndicesOptions.fromOptions( + req.indicesOptions().ignoreUnavailable(), + req.indicesOptions().allowNoIndices(), + req.indicesOptions().expandWildcardsOpen(), + req.indicesOptions().expandWildcardsClosed(), + req.indicesOptions().allowAliasesToMultipleIndices(), + req.indicesOptions().forbidClosedIndices(), + req.indicesOptions().ignoreAliases(), + req.indicesOptions().ignoreThrottled() + ) + ); } else { return new RemoveIndexLifecyclePolicyRequest(new ArrayList<>(req.indices())); } @@ -55,17 +73,32 @@ private RemoveIndexLifecyclePolicyRequest copyInstance(RemoveIndexLifecyclePolic private RemoveIndexLifecyclePolicyRequest mutateInstance(RemoveIndexLifecyclePolicyRequest req) { if (randomBoolean()) { - return new RemoveIndexLifecyclePolicyRequest(req.indices(), - randomValueOtherThan(req.indicesOptions(), () -> IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), - randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean()))); + return new RemoveIndexLifecyclePolicyRequest( + req.indices(), + randomValueOtherThan( + req.indicesOptions(), + () -> IndicesOptions.fromOptions( + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean() + ) + ) + ); } else { if (req.indicesOptions() != null) { return new RemoveIndexLifecyclePolicyRequest( randomValueOtherThan(req.indices(), () -> Arrays.asList(generateRandomStringArray(20, 20, false))), - req.indicesOptions()); + req.indicesOptions() + ); } else { return new RemoveIndexLifecyclePolicyRequest( - randomValueOtherThan(req.indices(), () -> Arrays.asList(generateRandomStringArray(20, 20, false)))); + randomValueOtherThan(req.indices(), () -> Arrays.asList(generateRandomStringArray(20, 20, false))) + ); } } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/RemoveIndexLifecyclePolicyResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/RemoveIndexLifecyclePolicyResponseTests.java index d6f064b39b007..bb60a8b718f46 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/RemoveIndexLifecyclePolicyResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/RemoveIndexLifecyclePolicyResponseTests.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.ilm; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.EqualsHashCodeTestUtils; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.ArrayList; @@ -39,23 +39,22 @@ private RemoveIndexLifecyclePolicyResponse copyInstance(RemoveIndexLifecyclePoli } private RemoveIndexLifecyclePolicyResponse mutateInstance(RemoveIndexLifecyclePolicyResponse req) { - return new RemoveIndexLifecyclePolicyResponse(randomValueOtherThan(req.getFailedIndexes(), - () -> Arrays.asList(generateRandomStringArray(20, 20, false)))); + return new RemoveIndexLifecyclePolicyResponse( + randomValueOtherThan(req.getFailedIndexes(), () -> Arrays.asList(generateRandomStringArray(20, 20, false))) + ); } public void testFromXContent() throws IOException { - xContentTester( - this::createParser, - this::createInstance, - this::toXContent, - RemoveIndexLifecyclePolicyResponse::fromXContent) - .supportsUnknownFields(true) - .test(); + xContentTester(this::createParser, this::createInstance, this::toXContent, RemoveIndexLifecyclePolicyResponse::fromXContent) + .supportsUnknownFields(true) + .test(); } public void testNullFailedIndices() { - IllegalArgumentException exception = - expectThrows(IllegalArgumentException.class, () -> new RemoveIndexLifecyclePolicyResponse(null)); + IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> new RemoveIndexLifecyclePolicyResponse(null) + ); assertEquals("failed_indexes cannot be null", exception.getMessage()); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/RolloverActionTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/RolloverActionTests.java index a0825d3725391..12100284387a0 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/RolloverActionTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/RolloverActionTests.java @@ -10,8 +10,8 @@ import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; public class RolloverActionTests extends AbstractXContentTestCase { @@ -32,21 +32,20 @@ protected RolloverAction createTestInstance() { static RolloverAction randomInstance() { ByteSizeUnit maxSizeUnit = randomFrom(ByteSizeUnit.values()); - ByteSizeValue maxSize = randomBoolean() - ? null : new ByteSizeValue(randomNonNegativeLong() / maxSizeUnit.toBytes(1), maxSizeUnit); + ByteSizeValue maxSize = randomBoolean() ? null : new ByteSizeValue(randomNonNegativeLong() / maxSizeUnit.toBytes(1), maxSizeUnit); ByteSizeUnit maxPrimaryShardSizeUnit = randomFrom(ByteSizeUnit.values()); ByteSizeValue maxPrimaryShardSize = randomBoolean() - ? null : new ByteSizeValue(randomNonNegativeLong() / maxPrimaryShardSizeUnit.toBytes(1), maxPrimaryShardSizeUnit); - TimeValue maxAge = randomBoolean() - ? null : TimeValue.parseTimeValue(randomPositiveTimeValue(), "rollover_action_test"); + ? null + : new ByteSizeValue(randomNonNegativeLong() / maxPrimaryShardSizeUnit.toBytes(1), maxPrimaryShardSizeUnit); + TimeValue maxAge = randomBoolean() ? null : TimeValue.parseTimeValue(randomPositiveTimeValue(), "rollover_action_test"); Long maxDocs = (maxSize == null && maxPrimaryShardSize == null && maxAge == null || randomBoolean()) - ? randomNonNegativeLong() : null; + ? randomNonNegativeLong() + : null; return new RolloverAction(maxSize, maxPrimaryShardSize, maxAge, maxDocs); } public void testNoConditions() { - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, - () -> new RolloverAction(null, null, null, null)); + IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> new RolloverAction(null, null, null, null)); assertEquals("At least one rollover condition must be set.", exception.getMessage()); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/SearchableSnapshotActionTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/SearchableSnapshotActionTests.java index b99c58794b0c5..d9e63a850e259 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/SearchableSnapshotActionTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/SearchableSnapshotActionTests.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ilm; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/SetPriorityActionTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/SetPriorityActionTests.java index 8939c180c3645..2dfcf1d6dff0f 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/SetPriorityActionTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/SetPriorityActionTests.java @@ -7,9 +7,9 @@ */ package org.elasticsearch.client.ilm; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.test.EqualsHashCodeTestUtils; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -41,7 +41,7 @@ public void testNonPositivePriority() { assertThat(e.getMessage(), equalTo("[priority] must be 0 or greater")); } - public void testNullPriorityAllowed(){ + public void testNullPriorityAllowed() { SetPriorityAction nullPriority = new SetPriorityAction(null); assertNull(nullPriority.recoveryPriority); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/ShrinkActionTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/ShrinkActionTests.java index 03c2b9898ee09..5f174d15d9942 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/ShrinkActionTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/ShrinkActionTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ilm; import org.elasticsearch.common.unit.ByteSizeValue; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/UnfollowActionTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/UnfollowActionTests.java index 340afb53ac85f..abdd6a9c65866 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/UnfollowActionTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/UnfollowActionTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ilm; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/WaitForSnapshotActionTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/WaitForSnapshotActionTests.java index 6534230b696d8..1ce3e58371c70 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/WaitForSnapshotActionTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ilm/WaitForSnapshotActionTests.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ilm; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import static org.hamcrest.Matchers.is; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/AnalyzeRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/AnalyzeRequestTests.java index 005b211195773..2defda6acf310 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/AnalyzeRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/AnalyzeRequestTests.java @@ -32,8 +32,7 @@ protected void assertInstances(AnalyzeAction.Request serverInstance, AnalyzeRequ if (serverInstance.tokenizer() != null) { assertEquals(serverInstance.tokenizer().name, clientTestInstance.tokenizer().name); assertEquals(serverInstance.tokenizer().definition, clientTestInstance.tokenizer().definition); - } - else { + } else { assertNull(clientTestInstance.tokenizer()); } assertEquals(serverInstance.field(), clientTestInstance.field()); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/AnalyzeResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/AnalyzeResponseTests.java index 161d390dd0b5b..428bd9c4b99cc 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/AnalyzeResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/AnalyzeResponseTests.java @@ -10,9 +10,9 @@ import org.elasticsearch.action.admin.indices.analyze.AnalyzeAction; import org.elasticsearch.client.AbstractResponseTestCase; +import org.elasticsearch.test.RandomObjects; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.RandomObjects; import java.io.IOException; import java.util.Arrays; @@ -30,20 +30,19 @@ protected AnalyzeAction.Response createServerTestInstance(XContentType xContentT AnalyzeAction.CharFilteredText[] charfilters = null; AnalyzeAction.AnalyzeTokenList[] tokenfilters = null; if (randomBoolean()) { - charfilters = new AnalyzeAction.CharFilteredText[]{ - new AnalyzeAction.CharFilteredText("my_charfilter", new String[]{"one two"}) - }; + charfilters = new AnalyzeAction.CharFilteredText[] { + new AnalyzeAction.CharFilteredText("my_charfilter", new String[] { "one two" }) }; } if (randomBoolean()) { - tokenfilters = new AnalyzeAction.AnalyzeTokenList[]{ + tokenfilters = new AnalyzeAction.AnalyzeTokenList[] { new AnalyzeAction.AnalyzeTokenList("my_tokenfilter_1", tokens), - new AnalyzeAction.AnalyzeTokenList("my_tokenfilter_2", tokens) - }; + new AnalyzeAction.AnalyzeTokenList("my_tokenfilter_2", tokens) }; } AnalyzeAction.DetailAnalyzeResponse dar = new AnalyzeAction.DetailAnalyzeResponse( charfilters, new AnalyzeAction.AnalyzeTokenList("my_tokenizer", tokens), - tokenfilters); + tokenfilters + ); return new AnalyzeAction.Response(null, dar); } return new AnalyzeAction.Response(Arrays.asList(tokens), null); @@ -59,8 +58,7 @@ protected void assertInstances(AnalyzeAction.Response serverTestInstance, Analyz if (serverTestInstance.detail() != null) { assertNotNull(clientInstance.detail()); assertInstances(serverTestInstance.detail(), clientInstance.detail()); - } - else { + } else { assertEquals(serverTestInstance.getTokens().size(), clientInstance.getTokens().size()); for (int i = 0; i < serverTestInstance.getTokens().size(); i++) { assertEqualTokens(serverTestInstance.getTokens().get(0), clientInstance.getTokens().get(0)); @@ -83,8 +81,7 @@ private static void assertInstances(AnalyzeAction.DetailAnalyzeResponse serverRe assertInstances(serverResponse.tokenizer(), clientResponse.tokenizer()); if (serverResponse.tokenfilters() == null) { assertNull(clientResponse.tokenfilters()); - } - else { + } else { assertEquals(serverResponse.tokenfilters().length, clientResponse.tokenfilters().length); for (int i = 0; i < serverResponse.tokenfilters().length; i++) { assertInstances(serverResponse.tokenfilters()[i], clientResponse.tokenfilters()[i]); @@ -92,8 +89,7 @@ private static void assertInstances(AnalyzeAction.DetailAnalyzeResponse serverRe } if (serverResponse.charfilters() == null) { assertNull(clientResponse.charfilters()); - } - else { + } else { assertEquals(serverResponse.charfilters().length, clientResponse.charfilters().length); for (int i = 0; i < serverResponse.charfilters().length; i++) { assertInstances(serverResponse.charfilters()[i], clientResponse.charfilters()[i]); @@ -101,12 +97,10 @@ private static void assertInstances(AnalyzeAction.DetailAnalyzeResponse serverRe } } - private static void assertInstances(AnalyzeAction.AnalyzeTokenList serverTokens, - DetailAnalyzeResponse.AnalyzeTokenList clientTokens) { + private static void assertInstances(AnalyzeAction.AnalyzeTokenList serverTokens, DetailAnalyzeResponse.AnalyzeTokenList clientTokens) { if (serverTokens == null) { assertNull(clientTokens); - } - else { + } else { assertEquals(serverTokens.getName(), clientTokens.getName()); assertEquals(serverTokens.getTokens().length, clientTokens.getTokens().length); for (int i = 0; i < serverTokens.getTokens().length; i++) { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/CloseIndexRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/CloseIndexRequestTests.java index 3fae1fe8fd020..1b6d31e22e330 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/CloseIndexRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/CloseIndexRequestTests.java @@ -56,7 +56,7 @@ public void testTimeout() { final TimeValue timeout = TimeValue.timeValueSeconds(randomIntBetween(0, 1000)); request.setTimeout(timeout); - final TimeValue masterTimeout = TimeValue.timeValueSeconds(randomIntBetween(0,1000)); + final TimeValue masterTimeout = TimeValue.timeValueSeconds(randomIntBetween(0, 1000)); request.setMasterTimeout(masterTimeout); assertEquals(request.timeout(), timeout); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/CloseIndexResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/CloseIndexResponseTests.java index 233ee9486f45b..34625cc0aba09 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/CloseIndexResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/CloseIndexResponseTests.java @@ -13,15 +13,15 @@ import org.elasticsearch.client.AbstractResponseTestCase; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.index.Index; +import org.elasticsearch.index.IndexNotFoundException; +import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.transport.ActionNotFoundTransportException; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContent; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.index.Index; -import org.elasticsearch.index.IndexNotFoundException; -import org.elasticsearch.rest.RestStatus; -import org.elasticsearch.transport.ActionNotFoundTransportException; import java.io.IOException; import java.util.ArrayList; @@ -36,8 +36,9 @@ import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; -public class CloseIndexResponseTests extends - AbstractResponseTestCase { +public class CloseIndexResponseTests extends AbstractResponseTestCase< + org.elasticsearch.action.admin.indices.close.CloseIndexResponse, + CloseIndexResponse> { @Override protected org.elasticsearch.action.admin.indices.close.CloseIndexResponse createServerTestInstance(XContentType xContentType) { @@ -89,15 +90,18 @@ protected CloseIndexResponse doParseToClientInstance(final XContentParser parser } @Override - protected void assertInstances(final org.elasticsearch.action.admin.indices.close.CloseIndexResponse serverInstance, - final CloseIndexResponse clientInstance) { + protected void assertInstances( + final org.elasticsearch.action.admin.indices.close.CloseIndexResponse serverInstance, + final CloseIndexResponse clientInstance + ) { assertNotSame(serverInstance, clientInstance); assertThat(clientInstance.isAcknowledged(), equalTo(serverInstance.isAcknowledged())); assertThat(clientInstance.isShardsAcknowledged(), equalTo(serverInstance.isShardsAcknowledged())); assertThat(clientInstance.getIndices(), hasSize(serverInstance.getIndices().size())); serverInstance.getIndices().forEach(expectedIndexResult -> { - List actualIndexResults = clientInstance.getIndices().stream() + List actualIndexResults = clientInstance.getIndices() + .stream() .filter(result -> result.getIndex().equals(expectedIndexResult.getIndex().getName())) .collect(Collectors.toList()); assertThat(actualIndexResults, hasSize(1)); @@ -118,10 +122,11 @@ protected void assertInstances(final org.elasticsearch.action.admin.indices.clos if (expectedIndexResult.getShards() != null) { assertThat(actualIndexResult.getException(), nullValue()); - List failedShardResults = - Arrays.stream(expectedIndexResult.getShards()) - .filter(org.elasticsearch.action.admin.indices.close.CloseIndexResponse.ShardResult::hasFailures) - .collect(Collectors.toList()); + List failedShardResults = Arrays.stream( + expectedIndexResult.getShards() + ) + .filter(org.elasticsearch.action.admin.indices.close.CloseIndexResponse.ShardResult::hasFailures) + .collect(Collectors.toList()); if (failedShardResults.isEmpty()) { assertThat(actualIndexResult.hasFailures(), is(false)); @@ -169,7 +174,8 @@ public final void testBwcFromXContent() throws IOException { final XContentParser parser = xContent.createParser( NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, - bytes.streamInput()); + bytes.streamInput() + ); final CloseIndexResponse actual = doParseToClientInstance(parser); assertThat(actual, notNullValue()); @@ -188,7 +194,8 @@ public final void testBwcFromXContent() throws IOException { final XContentParser parser = xContent.createParser( NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, - bytes.streamInput()); + bytes.streamInput() + ); final CloseIndexResponse actual = doParseToClientInstance(parser); assertThat(actual, notNullValue()); @@ -198,13 +205,17 @@ public final void testBwcFromXContent() throws IOException { } } - private org.elasticsearch.action.admin.indices.close.CloseIndexResponse.ShardResult.Failure newFailure(final String indexName, - final int shard, - final String nodeId) { - Exception exception = randomFrom(new IndexNotFoundException(indexName), + private org.elasticsearch.action.admin.indices.close.CloseIndexResponse.ShardResult.Failure newFailure( + final String indexName, + final int shard, + final String nodeId + ) { + Exception exception = randomFrom( + new IndexNotFoundException(indexName), new ActionNotFoundTransportException("test"), new IOException("boom", new NullPointerException()), - new ElasticsearchStatusException("something", RestStatus.TOO_MANY_REQUESTS)); + new ElasticsearchStatusException("something", RestStatus.TOO_MANY_REQUESTS) + ); return new org.elasticsearch.action.admin.indices.close.CloseIndexResponse.ShardResult.Failure(indexName, shard, exception, nodeId); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/CreateIndexRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/CreateIndexRequestTests.java index b83e9511d63ec..33c1bcd2f9052 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/CreateIndexRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/CreateIndexRequestTests.java @@ -9,8 +9,8 @@ package org.elasticsearch.client.indices; import org.elasticsearch.action.admin.indices.alias.Alias; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.Set; @@ -44,8 +44,10 @@ private void assertMappingsEqual(CreateIndexRequest expected, CreateIndexRequest assertNull(actual.mappings()); } else { assertNotNull(actual.mappings()); - try (XContentParser expectedJson = createParser(expected.mappingsXContentType().xContent(), expected.mappings()); - XContentParser actualJson = createParser(actual.mappingsXContentType().xContent(), actual.mappings())) { + try ( + XContentParser expectedJson = createParser(expected.mappingsXContentType().xContent(), expected.mappings()); + XContentParser actualJson = createParser(actual.mappingsXContentType().xContent(), actual.mappings()) + ) { assertEquals(expectedJson.map(), actualJson.map()); } catch (IOException e) { throw new RuntimeException(e); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/DataStreamsStatsResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/DataStreamsStatsResponseTests.java index 123c78b47428a..b588f2c0e139d 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/DataStreamsStatsResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/DataStreamsStatsResponseTests.java @@ -9,12 +9,12 @@ package org.elasticsearch.client.indices; import org.elasticsearch.ElasticsearchException; -import org.elasticsearch.xpack.core.action.DataStreamsStatsAction; import org.elasticsearch.action.support.DefaultShardOperationFailedException; import org.elasticsearch.client.AbstractResponseTestCase; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.xpack.core.action.DataStreamsStatsAction; import java.io.IOException; import java.util.ArrayList; @@ -42,20 +42,33 @@ protected DataStreamsStatsAction.Response createServerTestInstance(XContentType long storeSize = randomLongBetween(250, 1000000000); totalStoreSize += storeSize; long maximumTimestamp = randomRecentTimestamp(); - dataStreamStats.add(new DataStreamsStatsAction.DataStreamStats(dataStreamName, backingIndices, - new ByteSizeValue(storeSize), maximumTimestamp)); + dataStreamStats.add( + new DataStreamsStatsAction.DataStreamStats(dataStreamName, backingIndices, new ByteSizeValue(storeSize), maximumTimestamp) + ); } int totalShards = randomIntBetween(backingIndicesTotal, backingIndicesTotal * 3); int successfulShards = randomInt(totalShards); int failedShards = totalShards - successfulShards; List exceptions = new ArrayList<>(); for (int i = 0; i < failedShards; i++) { - exceptions.add(new DefaultShardOperationFailedException(randomAlphaOfLength(8).toLowerCase(Locale.getDefault()), - randomInt(totalShards), new ElasticsearchException("boom"))); + exceptions.add( + new DefaultShardOperationFailedException( + randomAlphaOfLength(8).toLowerCase(Locale.getDefault()), + randomInt(totalShards), + new ElasticsearchException("boom") + ) + ); } - return new DataStreamsStatsAction.Response(totalShards, successfulShards, failedShards, exceptions, - dataStreamCount, backingIndicesTotal, new ByteSizeValue(totalStoreSize), - dataStreamStats.toArray(DataStreamsStatsAction.DataStreamStats[]::new)); + return new DataStreamsStatsAction.Response( + totalShards, + successfulShards, + failedShards, + exceptions, + dataStreamCount, + backingIndicesTotal, + new ByteSizeValue(totalStoreSize), + dataStreamStats.toArray(DataStreamsStatsAction.DataStreamStats[]::new) + ); } @Override @@ -75,8 +88,7 @@ protected void assertInstances(DataStreamsStatsAction.Response serverTestInstanc assertEquals(serverTestInstance.getTotalStoreSize(), clientInstance.getTotalStoreSize()); assertEquals(serverTestInstance.getDataStreams().length, clientInstance.getDataStreams().size()); for (DataStreamsStatsAction.DataStreamStats serverStats : serverTestInstance.getDataStreams()) { - DataStreamsStatsResponse.DataStreamStats clientStats = clientInstance.getDataStreams() - .get(serverStats.getDataStream()); + DataStreamsStatsResponse.DataStreamStats clientStats = clientInstance.getDataStreams().get(serverStats.getDataStream()); assertEquals(serverStats.getDataStream(), clientStats.getDataStream()); assertEquals(serverStats.getBackingIndices(), clientStats.getBackingIndices()); assertEquals(serverStats.getStoreSize(), clientStats.getStoreSize()); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/GetComponentTemplatesResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/GetComponentTemplatesResponseTests.java index 5f5070d3f4a11..b040fe87ec539 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/GetComponentTemplatesResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/GetComponentTemplatesResponseTests.java @@ -13,9 +13,9 @@ import org.elasticsearch.cluster.metadata.Template; import org.elasticsearch.common.compress.CompressedXContent; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.test.ESTestCase; import java.io.IOException; import java.util.Collections; @@ -31,10 +31,8 @@ public void testFromXContent() throws Exception { this::createParser, GetComponentTemplatesResponseTests::createTestInstance, GetComponentTemplatesResponseTests::toXContent, - GetComponentTemplatesResponse::fromXContent) - .supportsUnknownFields(true) - .randomFieldsExcludeFilter(a -> true) - .test(); + GetComponentTemplatesResponse::fromXContent + ).supportsUnknownFields(true).randomFieldsExcludeFilter(a -> true).test(); } public static Template randomTemplate() { @@ -57,8 +55,10 @@ public static Map randomMeta() { if (randomBoolean()) { return Collections.singletonMap(randomAlphaOfLength(4), randomAlphaOfLength(4)); } else { - return Collections.singletonMap(randomAlphaOfLength(5), - Collections.singletonMap(randomAlphaOfLength(4), randomAlphaOfLength(4))); + return Collections.singletonMap( + randomAlphaOfLength(5), + Collections.singletonMap(randomAlphaOfLength(4), randomAlphaOfLength(4)) + ); } } @@ -118,8 +118,6 @@ private static CompressedXContent randomMappings() { } private static Settings randomSettings() { - return Settings.builder() - .put(randomAlphaOfLength(4), randomAlphaOfLength(10)) - .build(); + return Settings.builder().put(randomAlphaOfLength(4), randomAlphaOfLength(10)).build(); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/GetComposableIndexTemplatesResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/GetComposableIndexTemplatesResponseTests.java index a241fb235b2b6..339d024f6ab7b 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/GetComposableIndexTemplatesResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/GetComposableIndexTemplatesResponseTests.java @@ -9,9 +9,9 @@ package org.elasticsearch.client.indices; import org.elasticsearch.cluster.metadata.ComposableIndexTemplate; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.Arrays; @@ -30,10 +30,8 @@ public void testFromXContent() throws Exception { this::createParser, GetComposableIndexTemplatesResponseTests::createTestInstance, GetComposableIndexTemplatesResponseTests::toXContent, - GetComposableIndexTemplatesResponse::fromXContent) - .supportsUnknownFields(true) - .randomFieldsExcludeFilter(a -> true) - .test(); + GetComposableIndexTemplatesResponse::fromXContent + ).supportsUnknownFields(true).randomFieldsExcludeFilter(a -> true).test(); } private static GetComposableIndexTemplatesResponse createTestInstance() { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/GetDataStreamResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/GetDataStreamResponseTests.java index 74f60ae342dbd..bd466d24142c7 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/GetDataStreamResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/GetDataStreamResponseTests.java @@ -9,12 +9,12 @@ package org.elasticsearch.client.indices; import org.elasticsearch.client.AbstractResponseTestCase; -import org.elasticsearch.cluster.metadata.DataStreamTestHelper; import org.elasticsearch.cluster.health.ClusterHealthStatus; import org.elasticsearch.cluster.metadata.DataStream; +import org.elasticsearch.cluster.metadata.DataStreamTestHelper; +import org.elasticsearch.index.Index; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.index.Index; import org.elasticsearch.xpack.core.action.GetDataStreamAction; import org.elasticsearch.xpack.core.action.GetDataStreamAction.Response.DataStreamInfo; @@ -27,8 +27,12 @@ public class GetDataStreamResponseTests extends AbstractResponseTestCase getRandomFieldsExcludeFilter() { // allow random fields at the level of `index` and `index.mappings.field` // otherwise random field could be evaluated as index name or type name - return s -> false == (s.matches("(?[^.]+)") - || s.matches("(?[^.]+)\\.mappings\\.(?[^.]+)")); + return s -> false == (s.matches("(?[^.]+)") || s.matches("(?[^.]+)\\.mappings\\.(?[^.]+)")); } private static GetFieldMappingsResponse createTestInstance() { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/GetIndexResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/GetIndexResponseTests.java index 731e186fe16a0..524f516733ebc 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/GetIndexResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/GetIndexResponseTests.java @@ -16,10 +16,10 @@ import org.elasticsearch.common.collect.ImmutableOpenMap; import org.elasticsearch.common.settings.IndexScopedSettings; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.RandomCreateIndexGenerator; import org.elasticsearch.index.mapper.MapperService; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; import java.io.IOException; import java.util.ArrayList; @@ -31,7 +31,8 @@ import java.util.Map; import java.util.Objects; -public class GetIndexResponseTests extends AbstractResponseTestCase { @Override @@ -44,12 +45,12 @@ protected org.elasticsearch.action.admin.indices.get.GetIndexResponse createServ ImmutableOpenMap.Builder dataStreams = ImmutableOpenMap.builder(); IndexScopedSettings indexScopedSettings = IndexScopedSettings.DEFAULT_SCOPED_SETTINGS; boolean includeDefaults = randomBoolean(); - for (String index: indices) { + for (String index : indices) { mappings.put(index, createMappingsForIndex()); List aliasMetadataList = new ArrayList<>(); int aliasesNum = randomIntBetween(0, 3); - for (int i=0; i expectedMapping = - XContentHelper.convertToMap(mappingSource, true, xContentBuilder.contentType()).v2(); + Map expectedMapping = XContentHelper.convertToMap(mappingSource, true, xContentBuilder.contentType()) + .v2(); assertThat(result.mappings().sourceAsMap(), equalTo(expectedMapping.get("_doc"))); assertThat(result.aliases().size(), equalTo(esIMD.aliases().size())); - List expectedAliases = esIMD.aliases().values().stream() + List expectedAliases = esIMD.aliases() + .values() + .stream() .sorted(Comparator.comparing(AliasMetadata::alias)) .collect(Collectors.toList()); - List actualAliases = result.aliases().values().stream() + List actualAliases = result.aliases() + .values() + .stream() .sorted(Comparator.comparing(AliasMetadata::alias)) .collect(Collectors.toList()); for (int j = 0; j < result.aliases().size(); j++) { @@ -119,8 +131,7 @@ public void testParsingFromEsResponse() throws IOException { } private Predicate randomFieldsExcludeFilter() { - return (field) -> - field.isEmpty() + return (field) -> field.isEmpty() || field.endsWith("aliases") || field.endsWith("settings") || field.endsWith("settings.index") @@ -132,11 +143,10 @@ private Predicate randomFieldsExcludeFilter() { private static void assertEqualInstances(GetIndexTemplatesResponse expectedInstance, GetIndexTemplatesResponse newInstance) { assertEquals(expectedInstance, newInstance); // Check there's no doc types at the root of the mapping - Map expectedMap = XContentHelper.convertToMap( - new BytesArray(mappingString), true, XContentType.JSON).v2(); + Map expectedMap = XContentHelper.convertToMap(new BytesArray(mappingString), true, XContentType.JSON).v2(); for (IndexTemplateMetadata template : newInstance.getIndexTemplates()) { MappingMetadata mappingMD = template.mappings(); - if(mappingMD != null) { + if (mappingMD != null) { Map mappingAsMap = mappingMD.sourceAsMap(); assertEquals(expectedMap, mappingAsMap); } @@ -175,13 +185,13 @@ static GetIndexTemplatesResponse createTestInstance() { // As the client class GetIndexTemplatesResponse doesn't have toXContent method, adding this method here only for the test static void toXContent(GetIndexTemplatesResponse response, XContentBuilder builder) throws IOException { - //Create a server-side counterpart for the client-side class and call toXContent on it + // Create a server-side counterpart for the client-side class and call toXContent on it List serverIndexTemplates = new ArrayList<>(); List clientIndexTemplates = response.getIndexTemplates(); for (IndexTemplateMetadata clientITMD : clientIndexTemplates) { org.elasticsearch.cluster.metadata.IndexTemplateMetadata.Builder serverTemplateBuilder = - org.elasticsearch.cluster.metadata.IndexTemplateMetadata.builder(clientITMD.name()); + org.elasticsearch.cluster.metadata.IndexTemplateMetadata.builder(clientITMD.name()); serverTemplateBuilder.patterns(clientITMD.patterns()); @@ -199,8 +209,8 @@ static void toXContent(GetIndexTemplatesResponse response, XContentBuilder build serverIndexTemplates.add(serverTemplateBuilder.build()); } - org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse serverResponse = new - org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse(serverIndexTemplates); + org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse serverResponse = + new org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse(serverIndexTemplates); serverResponse.toXContent(builder, ToXContent.EMPTY_PARAMS); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/GetMappingsResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/GetMappingsResponseTests.java index 2172f39930ca1..e89e8ec462662 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/GetMappingsResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/GetMappingsResponseTests.java @@ -11,17 +11,18 @@ import org.elasticsearch.client.AbstractResponseTestCase; import org.elasticsearch.cluster.metadata.MappingMetadata; import org.elasticsearch.common.collect.ImmutableOpenMap; +import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.index.mapper.MapperService; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Objects; -public class GetMappingsResponseTests - extends AbstractResponseTestCase { +public class GetMappingsResponseTests extends AbstractResponseTestCase< + org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse, + GetMappingsResponse> { @Override protected org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse createServerTestInstance(XContentType xContentType) { @@ -39,8 +40,10 @@ protected GetMappingsResponse doParseToClientInstance(XContentParser parser) thr } @Override - protected void assertInstances(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse serverTestInstance, - GetMappingsResponse clientInstance) { + protected void assertInstances( + org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse serverTestInstance, + GetMappingsResponse clientInstance + ) { assertMapEquals(serverTestInstance.getMappings(), clientInstance.mappings()); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/PutIndexTemplateRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/PutIndexTemplateRequestTests.java index 3136797fa942e..c30966a304e37 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/PutIndexTemplateRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/PutIndexTemplateRequestTests.java @@ -10,11 +10,11 @@ import org.elasticsearch.action.admin.indices.alias.Alias; import org.elasticsearch.client.AbstractRequestTestCase; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; import java.io.IOException; import java.io.UncheckedIOException; @@ -23,7 +23,8 @@ import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; -public class PutIndexTemplateRequestTests extends AbstractRequestTestCase { public void testValidateErrorMessage() throws Exception { @@ -38,8 +39,10 @@ public void testValidateErrorMessage() throws Exception { @Override protected PutIndexTemplateRequest createClientTestInstance() { - PutIndexTemplateRequest request = new PutIndexTemplateRequest("test", - List.of(ESTestCase.generateRandomStringArray(20, 100, false, false))); + PutIndexTemplateRequest request = new PutIndexTemplateRequest( + "test", + List.of(ESTestCase.generateRandomStringArray(20, 100, false, false)) + ); if (randomBoolean()) { request.version(randomInt()); } @@ -61,11 +64,18 @@ protected PutIndexTemplateRequest createClientTestInstance() { } if (randomBoolean()) { try { - request.mapping(XContentFactory.jsonBuilder().startObject() - .startObject("_doc") - .startObject("properties") - .startObject("field-" + randomInt()).field("type", randomFrom("keyword", "text")).endObject() - .endObject().endObject().endObject()); + request.mapping( + XContentFactory.jsonBuilder() + .startObject() + .startObject("_doc") + .startObject("properties") + .startObject("field-" + randomInt()) + .field("type", randomFrom("keyword", "text")) + .endObject() + .endObject() + .endObject() + .endObject() + ); } catch (IOException ex) { throw new UncheckedIOException(ex); } @@ -77,14 +87,16 @@ protected PutIndexTemplateRequest createClientTestInstance() { } @Override - protected org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest doParseToServerInstance( - XContentParser parser) throws IOException { + protected org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest doParseToServerInstance(XContentParser parser) + throws IOException { return new org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest("test").source(parser.map()); } @Override - protected void assertInstances(org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest serverInstance, - PutIndexTemplateRequest clientTestInstance) { + protected void assertInstances( + org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest serverInstance, + PutIndexTemplateRequest clientTestInstance + ) { assertNotSame(serverInstance, clientTestInstance); assertThat(serverInstance.version(), equalTo(clientTestInstance.version())); assertThat(serverInstance.order(), equalTo(clientTestInstance.order())); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/PutMappingRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/PutMappingRequestTests.java index b0d383cfe68f0..ee4bbe494888f 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/PutMappingRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/PutMappingRequestTests.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.indices; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; import java.util.Map; @@ -49,8 +49,10 @@ protected boolean supportsUnknownFields() { @Override protected void assertEqualInstances(PutMappingRequest expected, PutMappingRequest actual) { if (actual.source() != null) { - try (XContentParser expectedJson = createParser(expected.xContentType().xContent(), expected.source()); - XContentParser actualJson = createParser(actual.xContentType().xContent(), actual.source())) { + try ( + XContentParser expectedJson = createParser(expected.xContentType().xContent(), expected.source()); + XContentParser actualJson = createParser(actual.xContentType().xContent(), actual.source()) + ) { assertEquals(expectedJson.mapOrdered(), actualJson.mapOrdered()); } catch (IOException e) { throw new RuntimeException(e); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/RandomCreateIndexGenerator.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/RandomCreateIndexGenerator.java index 181692440416b..2a49f9ae99f24 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/RandomCreateIndexGenerator.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/RandomCreateIndexGenerator.java @@ -31,8 +31,7 @@ public static CreateIndexRequest randomCreateIndexRequest() { // mapping definition for one that does not contain types. org.elasticsearch.action.admin.indices.create.CreateIndexRequest serverRequest = org.elasticsearch.index.RandomCreateIndexGenerator.randomCreateIndexRequest(); - return new CreateIndexRequest(serverRequest.index()) - .settings(serverRequest.settings()) + return new CreateIndexRequest(serverRequest.index()).settings(serverRequest.settings()) .aliases(serverRequest.aliases()) .mapping(randomMapping()); } catch (IOException e) { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/ReloadAnalyzersResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/ReloadAnalyzersResponseTests.java index 483d2bef92869..243ef219089a6 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/ReloadAnalyzersResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/ReloadAnalyzersResponseTests.java @@ -10,9 +10,9 @@ import org.elasticsearch.action.support.DefaultShardOperationFailedException; import org.elasticsearch.client.AbstractResponseTestCase; +import org.elasticsearch.index.seqno.RetentionLeaseNotFoundException; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.index.seqno.RetentionLeaseNotFoundException; import org.elasticsearch.xpack.core.action.ReloadAnalyzersResponse.ReloadDetails; import java.io.IOException; @@ -30,8 +30,9 @@ import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.in; -public class ReloadAnalyzersResponseTests - extends AbstractResponseTestCase { +public class ReloadAnalyzersResponseTests extends AbstractResponseTestCase< + org.elasticsearch.xpack.core.action.ReloadAnalyzersResponse, + ReloadAnalyzersResponse> { private String index; private String id; @@ -50,7 +51,8 @@ protected org.elasticsearch.xpack.core.action.ReloadAnalyzersResponse createServ final DefaultShardOperationFailedException failure = new DefaultShardOperationFailedException( index, randomValueOtherThanMany(shardIds::contains, () -> randomIntBetween(0, total - 1)), - new RetentionLeaseNotFoundException(id)); + new RetentionLeaseNotFoundException(id) + ); failures.add(failure); shardIds.add(failure.shardId()); } @@ -73,8 +75,10 @@ protected ReloadAnalyzersResponse doParseToClientInstance(XContentParser parser) } @Override - protected void assertInstances(org.elasticsearch.xpack.core.action.ReloadAnalyzersResponse serverTestInstance, - ReloadAnalyzersResponse clientInstance) { + protected void assertInstances( + org.elasticsearch.xpack.core.action.ReloadAnalyzersResponse serverTestInstance, + ReloadAnalyzersResponse clientInstance + ) { assertThat(clientInstance.shards().total(), equalTo(serverTestInstance.getTotalShards())); assertThat(clientInstance.shards().successful(), equalTo(serverTestInstance.getSuccessfulShards())); assertThat(clientInstance.shards().skipped(), equalTo(0)); @@ -89,7 +93,8 @@ protected void assertInstances(org.elasticsearch.xpack.core.action.ReloadAnalyze Map serverDetails = serverTestInstance.getReloadDetails(); assertThat(clientInstance.getReloadedDetails().size(), equalTo(serverDetails.size())); for (Entry entry : clientInstance - .getReloadedDetails().entrySet()) { + .getReloadedDetails() + .entrySet()) { String indexName = entry.getKey(); assertTrue(serverDetails.keySet().contains(indexName)); assertEquals(serverDetails.get(indexName).getIndexName(), entry.getValue().getIndexName()); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/ResizeRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/ResizeRequestTests.java index 6edaf9a575c5e..a205e75a96c4c 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/ResizeRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/ResizeRequestTests.java @@ -16,28 +16,32 @@ import java.io.IOException; import java.util.Arrays; -public class ResizeRequestTests extends AbstractRequestTestCase { @Override protected ResizeRequest createClientTestInstance() { - return new ResizeRequest("target", "source") - .setAliases(Arrays.asList(new Alias("target1"), new Alias("target2"))) + return new ResizeRequest("target", "source").setAliases(Arrays.asList(new Alias("target1"), new Alias("target2"))) .setSettings(Settings.builder().put("index.foo", "bar").build()); } @Override protected org.elasticsearch.action.admin.indices.shrink.ResizeRequest doParseToServerInstance(XContentParser parser) throws IOException { - org.elasticsearch.action.admin.indices.shrink.ResizeRequest req - = new org.elasticsearch.action.admin.indices.shrink.ResizeRequest("target", "source"); + org.elasticsearch.action.admin.indices.shrink.ResizeRequest req = new org.elasticsearch.action.admin.indices.shrink.ResizeRequest( + "target", + "source" + ); req.fromXContent(parser); return req; } @Override - protected void assertInstances(org.elasticsearch.action.admin.indices.shrink.ResizeRequest serverInstance, - ResizeRequest clientTestInstance) { + protected void assertInstances( + org.elasticsearch.action.admin.indices.shrink.ResizeRequest serverInstance, + ResizeRequest clientTestInstance + ) { assertEquals(serverInstance.getSourceIndex(), clientTestInstance.getSourceIndex()); assertEquals(serverInstance.getTargetIndexRequest().index(), clientTestInstance.getTargetIndex()); assertEquals(serverInstance.getTargetIndexRequest().settings(), clientTestInstance.getSettings()); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/ResizeResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/ResizeResponseTests.java index 50448771f4152..a67b6c59386d7 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/ResizeResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/ResizeResponseTests.java @@ -14,8 +14,9 @@ import java.io.IOException; -public class ResizeResponseTests extends - AbstractResponseTestCase { +public class ResizeResponseTests extends AbstractResponseTestCase< + org.elasticsearch.action.admin.indices.shrink.ResizeResponse, + ResizeResponse> { @Override protected org.elasticsearch.action.admin.indices.shrink.ResizeResponse createServerTestInstance(XContentType xContentType) { @@ -29,8 +30,10 @@ protected ResizeResponse doParseToClientInstance(XContentParser parser) throws I } @Override - protected void assertInstances(org.elasticsearch.action.admin.indices.shrink.ResizeResponse serverTestInstance, - ResizeResponse clientInstance) { + protected void assertInstances( + org.elasticsearch.action.admin.indices.shrink.ResizeResponse serverTestInstance, + ResizeResponse clientInstance + ) { assertEquals(serverTestInstance.isAcknowledged(), clientInstance.isAcknowledged()); assertEquals(serverTestInstance.isShardsAcknowledged(), clientInstance.isShardsAcknowledged()); assertEquals(serverTestInstance.index(), clientInstance.index()); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/rollover/RolloverRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/rollover/RolloverRequestTests.java index d072668b6a995..2ac9be0754988 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/rollover/RolloverRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/rollover/RolloverRequestTests.java @@ -22,7 +22,6 @@ import static org.hamcrest.Matchers.containsInAnyOrder; - public class RolloverRequestTests extends ESTestCase { public void testConstructorAndFieldAssignments() { // test constructor @@ -40,9 +39,11 @@ public void testConstructorAndFieldAssignments() { MaxDocsCondition maxDocsCondition = new MaxDocsCondition(10000L); MaxSizeCondition maxSizeCondition = new MaxSizeCondition(new ByteSizeValue(2000)); MaxPrimaryShardSizeCondition maxPrimaryShardSizeCondition = new MaxPrimaryShardSizeCondition(new ByteSizeValue(3000)); - Condition[] expectedConditions = new Condition[]{ - maxAgeCondition, maxDocsCondition, maxSizeCondition, maxPrimaryShardSizeCondition - }; + Condition[] expectedConditions = new Condition[] { + maxAgeCondition, + maxDocsCondition, + maxSizeCondition, + maxPrimaryShardSizeCondition }; rolloverRequest.addMaxIndexAgeCondition(maxAgeCondition.value()); rolloverRequest.addMaxIndexDocsCondition(maxDocsCondition.value()); rolloverRequest.addMaxIndexSizeCondition(maxSizeCondition.value()); @@ -52,8 +53,7 @@ public void testConstructorAndFieldAssignments() { } public void testValidation() { - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> - new RolloverRequest(null, null)); + IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> new RolloverRequest(null, null)); assertEquals("The index alias cannot be null!", exception.getMessage()); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/rollover/RolloverResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/rollover/RolloverResponseTests.java index 5258cbe841917..0d05f5bde59ec 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/rollover/RolloverResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/rollover/RolloverResponseTests.java @@ -15,8 +15,8 @@ import org.elasticsearch.action.admin.indices.rollover.MaxSizeCondition; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.ArrayList; @@ -43,10 +43,8 @@ public void testFromXContent() throws IOException { this::createParser, RolloverResponseTests::createTestInstance, RolloverResponseTests::toXContent, - RolloverResponse::fromXContent) - .supportsUnknownFields(true) - .randomFieldsExcludeFilter(getRandomFieldsExcludeFilter()) - .test(); + RolloverResponse::fromXContent + ).supportsUnknownFields(true).randomFieldsExcludeFilter(getRandomFieldsExcludeFilter()).test(); } private static RolloverResponse createTestInstance() { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/license/GetBasicStatusResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/license/GetBasicStatusResponseTests.java index cf6efd9f12c49..76f7a84b86930 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/license/GetBasicStatusResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/license/GetBasicStatusResponseTests.java @@ -13,8 +13,9 @@ import java.io.IOException; -public class GetBasicStatusResponseTests - extends AbstractResponseTestCase { +public class GetBasicStatusResponseTests extends AbstractResponseTestCase< + org.elasticsearch.license.GetBasicStatusResponse, + GetBasicStatusResponse> { @Override protected org.elasticsearch.license.GetBasicStatusResponse createServerTestInstance(XContentType xContentType) { @@ -27,10 +28,13 @@ protected GetBasicStatusResponse doParseToClientInstance(XContentParser parser) } @Override - protected void assertInstances(org.elasticsearch.license.GetBasicStatusResponse serverTestInstance, - GetBasicStatusResponse clientInstance) { - org.elasticsearch.license.GetBasicStatusResponse serverInstance = - new org.elasticsearch.license.GetBasicStatusResponse(clientInstance.isEligibleToStartBasic()); + protected void assertInstances( + org.elasticsearch.license.GetBasicStatusResponse serverTestInstance, + GetBasicStatusResponse clientInstance + ) { + org.elasticsearch.license.GetBasicStatusResponse serverInstance = new org.elasticsearch.license.GetBasicStatusResponse( + clientInstance.isEligibleToStartBasic() + ); assertEquals(serverTestInstance, serverInstance); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/license/GetTrialStatusResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/license/GetTrialStatusResponseTests.java index 0f21119a62d53..79212888d1fe7 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/license/GetTrialStatusResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/license/GetTrialStatusResponseTests.java @@ -13,8 +13,9 @@ import java.io.IOException; -public class GetTrialStatusResponseTests extends - AbstractResponseTestCase { +public class GetTrialStatusResponseTests extends AbstractResponseTestCase< + org.elasticsearch.license.GetTrialStatusResponse, + GetTrialStatusResponse> { @Override protected org.elasticsearch.license.GetTrialStatusResponse createServerTestInstance(XContentType xContentType) { @@ -27,10 +28,13 @@ protected GetTrialStatusResponse doParseToClientInstance(XContentParser parser) } @Override - protected void assertInstances(org.elasticsearch.license.GetTrialStatusResponse serverTestInstance, - GetTrialStatusResponse clientInstance) { - org.elasticsearch.license.GetTrialStatusResponse serverInstance = - new org.elasticsearch.license.GetTrialStatusResponse(clientInstance.isEligibleToStartTrial()); + protected void assertInstances( + org.elasticsearch.license.GetTrialStatusResponse serverTestInstance, + GetTrialStatusResponse clientInstance + ) { + org.elasticsearch.license.GetTrialStatusResponse serverInstance = new org.elasticsearch.license.GetTrialStatusResponse( + clientInstance.isEligibleToStartTrial() + ); assertEquals(serverInstance, serverTestInstance); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/license/PutLicenseResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/license/PutLicenseResponseTests.java index 93807bc5e8fea..0b2c6abb11b60 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/license/PutLicenseResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/license/PutLicenseResponseTests.java @@ -20,15 +20,17 @@ import static org.hamcrest.Matchers.equalTo; public class PutLicenseResponseTests extends AbstractResponseTestCase< - org.elasticsearch.protocol.xpack.license.PutLicenseResponse, PutLicenseResponse> { + org.elasticsearch.protocol.xpack.license.PutLicenseResponse, + PutLicenseResponse> { @Override protected org.elasticsearch.protocol.xpack.license.PutLicenseResponse createServerTestInstance(XContentType xContentType) { boolean acknowledged = randomBoolean(); - org.elasticsearch.protocol.xpack.license.LicensesStatus status = - randomFrom(org.elasticsearch.protocol.xpack.license.LicensesStatus.VALID, - org.elasticsearch.protocol.xpack.license.LicensesStatus.INVALID, - org.elasticsearch.protocol.xpack.license.LicensesStatus.EXPIRED); + org.elasticsearch.protocol.xpack.license.LicensesStatus status = randomFrom( + org.elasticsearch.protocol.xpack.license.LicensesStatus.VALID, + org.elasticsearch.protocol.xpack.license.LicensesStatus.INVALID, + org.elasticsearch.protocol.xpack.license.LicensesStatus.EXPIRED + ); String messageHeader; Map ackMessages; if (randomBoolean()) { @@ -66,12 +68,14 @@ protected PutLicenseResponse doParseToClientInstance(XContentParser parser) thro } @Override - protected void assertInstances(org.elasticsearch.protocol.xpack.license.PutLicenseResponse serverTestInstance, - PutLicenseResponse clientInstance) { + protected void assertInstances( + org.elasticsearch.protocol.xpack.license.PutLicenseResponse serverTestInstance, + PutLicenseResponse clientInstance + ) { assertThat(serverTestInstance.status().name(), equalTo(clientInstance.status().name())); assertThat(serverTestInstance.acknowledgeHeader(), equalTo(clientInstance.acknowledgeHeader())); assertThat(serverTestInstance.acknowledgeMessages().keySet(), equalTo(clientInstance.acknowledgeMessages().keySet())); - for(Map.Entry entry: serverTestInstance.acknowledgeMessages().entrySet()) { + for (Map.Entry entry : serverTestInstance.acknowledgeMessages().entrySet()) { assertTrue(Arrays.equals(entry.getValue(), clientInstance.acknowledgeMessages().get(entry.getKey()))); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/license/StartBasicResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/license/StartBasicResponseTests.java index e84dddd775765..095803684744d 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/license/StartBasicResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/license/StartBasicResponseTests.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.license; import org.elasticsearch.client.AbstractResponseTestCase; +import org.elasticsearch.license.PostStartBasicResponse; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.license.PostStartBasicResponse; import java.io.IOException; import java.util.Arrays; @@ -20,8 +20,7 @@ import static org.hamcrest.Matchers.equalTo; -public class StartBasicResponseTests extends AbstractResponseTestCase< - PostStartBasicResponse, StartBasicResponse> { +public class StartBasicResponseTests extends AbstractResponseTestCase { @Override protected PostStartBasicResponse createServerTestInstance(XContentType xContentType) { @@ -67,7 +66,7 @@ protected void assertInstances(PostStartBasicResponse serverTestInstance, StartB assertThat(serverTestInstance.getStatus().getErrorMessage(), equalTo(clientInstance.getErrorMessage())); assertThat(serverTestInstance.getAcknowledgeMessage(), equalTo(clientInstance.getAcknowledgeMessage())); assertThat(serverTestInstance.getAcknowledgeMessages().keySet(), equalTo(clientInstance.getAcknowledgeMessages().keySet())); - for(Map.Entry entry: serverTestInstance.getAcknowledgeMessages().entrySet()) { + for (Map.Entry entry : serverTestInstance.getAcknowledgeMessages().entrySet()) { assertTrue(Arrays.equals(entry.getValue(), clientInstance.getAcknowledgeMessages().get(entry.getKey()))); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/migration/DeprecationInfoResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/migration/DeprecationInfoResponseTests.java index 20082abcae268..1ea3f7a9753bb 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/migration/DeprecationInfoResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/migration/DeprecationInfoResponseTests.java @@ -8,10 +8,10 @@ package org.elasticsearch.client.migration; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.core.Tuple; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.EqualsHashCodeTestUtils; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.ArrayList; @@ -44,8 +44,8 @@ private void toXContent(DeprecationInfoResponse response, XContentBuilder builde builder.field("index_settings"); builder.startObject(); { - for (Map.Entry> entry : - response.getIndexSettingsIssues().entrySet()) { + for (Map.Entry> entry : response.getIndexSettingsIssues() + .entrySet()) { builder.field(entry.getKey()); builder.startArray(); for (DeprecationInfoResponse.DeprecationIssue issue : entry.getValue()) { @@ -66,10 +66,7 @@ private void toXContent(DeprecationInfoResponse response, XContentBuilder builde } private void toXContent(DeprecationInfoResponse.DeprecationIssue issue, XContentBuilder builder) throws IOException { - builder.startObject() - .field("level", issue.getLevel()) - .field("message", issue.getMessage()) - .field("url", issue.getUrl()); + builder.startObject().field("level", issue.getLevel()).field("message", issue.getMessage()).field("url", issue.getUrl()); if (issue.getDetails() != null) { builder.field("details", issue.getDetails()); } @@ -80,10 +77,8 @@ private void toXContent(DeprecationInfoResponse.DeprecationIssue issue, XContent builder.endObject(); } - private Map> createIndexSettingsIssues() { - Map> indexSettingsIssues = - new HashMap<>(); + Map> indexSettingsIssues = new HashMap<>(); for (int i = 0; i < randomIntBetween(1, 3); i++) { indexSettingsIssues.put(randomAlphaOfLengthBetween(1, 5), createRandomIssues(false)); } @@ -94,27 +89,38 @@ private List createRandomIssues(boolea List list = new ArrayList<>(); // the list of index settings cannot be zero, but the other lists can be, so this boolean is used to make the min number // of elements for this list. - int startingRandomNumber = canBeEmpty ? 0: 1; - for (int i =0; i < randomIntBetween(startingRandomNumber, 2); i++) { - list.add(new DeprecationInfoResponse.DeprecationIssue(randomFrom(WARNING, CRITICAL), - randomAlphaOfLength(5), - randomAlphaOfLength(5), - randomBoolean() ? randomAlphaOfLength(5) : null, - randomBoolean(), - randomBoolean() ? randomMap(1, 5, () -> new Tuple<>(randomAlphaOfLength(4), randomAlphaOfLength(4))) : null)); + int startingRandomNumber = canBeEmpty ? 0 : 1; + for (int i = 0; i < randomIntBetween(startingRandomNumber, 2); i++) { + list.add( + new DeprecationInfoResponse.DeprecationIssue( + randomFrom(WARNING, CRITICAL), + randomAlphaOfLength(5), + randomAlphaOfLength(5), + randomBoolean() ? randomAlphaOfLength(5) : null, + randomBoolean(), + randomBoolean() ? randomMap(1, 5, () -> new Tuple<>(randomAlphaOfLength(4), randomAlphaOfLength(4))) : null + ) + ); } return list; } private DeprecationInfoResponse createInstance() { - return new DeprecationInfoResponse(createRandomIssues(true), createRandomIssues(true), createIndexSettingsIssues(), - createRandomIssues(true)); + return new DeprecationInfoResponse( + createRandomIssues(true), + createRandomIssues(true), + createIndexSettingsIssues(), + createRandomIssues(true) + ); } private DeprecationInfoResponse copyInstance(DeprecationInfoResponse req) { - return new DeprecationInfoResponse(new ArrayList<>(req.getClusterSettingsIssues()), - new ArrayList<>(req.getNodeSettingsIssues()), new HashMap<>(req.getIndexSettingsIssues()), - new ArrayList<>(req.getMlSettingsIssues())); + return new DeprecationInfoResponse( + new ArrayList<>(req.getClusterSettingsIssues()), + new ArrayList<>(req.getNodeSettingsIssues()), + new HashMap<>(req.getIndexSettingsIssues()), + new ArrayList<>(req.getMlSettingsIssues()) + ); } private DeprecationInfoResponse mutateInstance(DeprecationInfoResponse req) { @@ -122,30 +128,31 @@ private DeprecationInfoResponse mutateInstance(DeprecationInfoResponse req) { } public void testFromXContent() throws IOException { - xContentTester( - this::createParser, - this::createInstance, - this::toXContent, - DeprecationInfoResponse::fromXContent) + xContentTester(this::createParser, this::createInstance, this::toXContent, DeprecationInfoResponse::fromXContent) .supportsUnknownFields(false) // old school parsing .test(); } public void testNullFailedIndices() { - NullPointerException exception = expectThrows(NullPointerException.class, - () -> new DeprecationInfoResponse(null, null, null, null)); + NullPointerException exception = expectThrows( + NullPointerException.class, + () -> new DeprecationInfoResponse(null, null, null, null) + ); assertEquals("cluster settings issues cannot be null", exception.getMessage()); - exception = expectThrows(NullPointerException.class, - () -> new DeprecationInfoResponse(Collections.emptyList(), null, null, null)); + exception = expectThrows(NullPointerException.class, () -> new DeprecationInfoResponse(Collections.emptyList(), null, null, null)); assertEquals("node settings issues cannot be null", exception.getMessage()); - exception = expectThrows(NullPointerException.class, - () -> new DeprecationInfoResponse(Collections.emptyList(), Collections.emptyList(), null, null)); + exception = expectThrows( + NullPointerException.class, + () -> new DeprecationInfoResponse(Collections.emptyList(), Collections.emptyList(), null, null) + ); assertEquals("index settings issues cannot be null", exception.getMessage()); - exception = expectThrows(NullPointerException.class, - () -> new DeprecationInfoResponse(Collections.emptyList(), Collections.emptyList(), Collections.emptyMap(), null)); + exception = expectThrows( + NullPointerException.class, + () -> new DeprecationInfoResponse(Collections.emptyList(), Collections.emptyList(), Collections.emptyMap(), null) + ); assertEquals("ml settings issues cannot be null", exception.getMessage()); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/migration/GetFeatureUpgradeStatusResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/migration/GetFeatureUpgradeStatusResponseTests.java index 7b3bb7cf6004b..9bebb48628905 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/migration/GetFeatureUpgradeStatusResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/migration/GetFeatureUpgradeStatusResponseTests.java @@ -21,7 +21,8 @@ import static org.hamcrest.Matchers.notNullValue; public class GetFeatureUpgradeStatusResponseTests extends AbstractResponseTestCase< - org.elasticsearch.action.admin.cluster.migration.GetFeatureUpgradeStatusResponse, GetFeatureUpgradeStatusResponse> { + org.elasticsearch.action.admin.cluster.migration.GetFeatureUpgradeStatusResponse, + GetFeatureUpgradeStatusResponse> { /** Our constructor should convert nulls to empty lists */ public void testConstructorHandlesNullLists() { @@ -33,9 +34,11 @@ public void testConstructorHandlesNullLists() { @Override protected org.elasticsearch.action.admin.cluster.migration.GetFeatureUpgradeStatusResponse createServerTestInstance( - XContentType xContentType) { + XContentType xContentType + ) { return new org.elasticsearch.action.admin.cluster.migration.GetFeatureUpgradeStatusResponse( - randomList(5, + randomList( + 5, () -> new org.elasticsearch.action.admin.cluster.migration.GetFeatureUpgradeStatusResponse.FeatureUpgradeStatus( randomAlphaOfLengthBetween(3, 20), randomFrom(Version.CURRENT, Version.CURRENT.minimumCompatibilityVersion()), @@ -48,7 +51,8 @@ protected org.elasticsearch.action.admin.cluster.migration.GetFeatureUpgradeStat null ) ) - )), + ) + ), randomFrom(org.elasticsearch.action.admin.cluster.migration.GetFeatureUpgradeStatusResponse.UpgradeStatus.values()) ); } @@ -61,7 +65,8 @@ protected GetFeatureUpgradeStatusResponse doParseToClientInstance(XContentParser @Override protected void assertInstances( org.elasticsearch.action.admin.cluster.migration.GetFeatureUpgradeStatusResponse serverTestInstance, - GetFeatureUpgradeStatusResponse clientInstance) { + GetFeatureUpgradeStatusResponse clientInstance + ) { assertThat(clientInstance.getUpgradeStatus(), equalTo(serverTestInstance.getUpgradeStatus().toString())); @@ -71,8 +76,8 @@ protected void assertInstances( assertThat(clientInstance.getFeatureUpgradeStatuses(), hasSize(serverTestInstance.getFeatureUpgradeStatuses().size())); for (int i = 0; i < clientInstance.getFeatureUpgradeStatuses().size(); i++) { - org.elasticsearch.action.admin.cluster.migration.GetFeatureUpgradeStatusResponse.FeatureUpgradeStatus serverTestStatus - = serverTestInstance.getFeatureUpgradeStatuses().get(i); + org.elasticsearch.action.admin.cluster.migration.GetFeatureUpgradeStatusResponse.FeatureUpgradeStatus serverTestStatus = + serverTestInstance.getFeatureUpgradeStatuses().get(i); GetFeatureUpgradeStatusResponse.FeatureUpgradeStatus clientStatus = clientInstance.getFeatureUpgradeStatuses().get(i); assertThat(clientStatus.getFeatureName(), equalTo(serverTestStatus.getFeatureName())); @@ -82,8 +87,8 @@ protected void assertInstances( assertThat(clientStatus.getIndexVersions(), hasSize(serverTestStatus.getIndexVersions().size())); for (int j = 0; i < clientStatus.getIndexVersions().size(); i++) { - org.elasticsearch.action.admin.cluster.migration.GetFeatureUpgradeStatusResponse.IndexInfo serverIndexInfo - = serverTestStatus.getIndexVersions().get(j); + org.elasticsearch.action.admin.cluster.migration.GetFeatureUpgradeStatusResponse.IndexInfo serverIndexInfo = + serverTestStatus.getIndexVersions().get(j); GetFeatureUpgradeStatusResponse.IndexVersion clientIndexVersion = clientStatus.getIndexVersions().get(j); assertThat(clientIndexVersion.getIndexName(), equalTo(serverIndexInfo.getIndexName())); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/migration/PostFeatureUpgradeResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/migration/PostFeatureUpgradeResponseTests.java index 270b9b5c7677a..e2e9c29e49ed3 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/migration/PostFeatureUpgradeResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/migration/PostFeatureUpgradeResponseTests.java @@ -24,7 +24,8 @@ import static org.hamcrest.Matchers.nullValue; public class PostFeatureUpgradeResponseTests extends AbstractResponseTestCase< - org.elasticsearch.action.admin.cluster.migration.PostFeatureUpgradeResponse, PostFeatureUpgradeResponse> { + org.elasticsearch.action.admin.cluster.migration.PostFeatureUpgradeResponse, + PostFeatureUpgradeResponse> { /** Our constructor should convert nulls to empty lists */ public void testConstructorHandlesNullLists() { @@ -35,14 +36,17 @@ public void testConstructorHandlesNullLists() { @Override protected org.elasticsearch.action.admin.cluster.migration.PostFeatureUpgradeResponse createServerTestInstance( - XContentType xContentType) { + XContentType xContentType + ) { if (randomBoolean()) { return new org.elasticsearch.action.admin.cluster.migration.PostFeatureUpgradeResponse( true, - randomList(5, + randomList( + 5, () -> new org.elasticsearch.action.admin.cluster.migration.PostFeatureUpgradeResponse.Feature( randomAlphaOfLengthBetween(5, 15) - )), + ) + ), null, null ); @@ -51,7 +55,8 @@ protected org.elasticsearch.action.admin.cluster.migration.PostFeatureUpgradeRes false, Collections.emptyList(), randomAlphaOfLengthBetween(10, 20), - new ElasticsearchException(randomAlphaOfLengthBetween(10, 20))); + new ElasticsearchException(randomAlphaOfLengthBetween(10, 20)) + ); } } @@ -63,15 +68,17 @@ protected PostFeatureUpgradeResponse doParseToClientInstance(XContentParser pars @Override protected void assertInstances( org.elasticsearch.action.admin.cluster.migration.PostFeatureUpgradeResponse serverTestInstance, - PostFeatureUpgradeResponse clientInstance) { + PostFeatureUpgradeResponse clientInstance + ) { assertThat(clientInstance.isAccepted(), equalTo(serverTestInstance.isAccepted())); assertThat(clientInstance.getFeatures(), hasSize(serverTestInstance.getFeatures().size())); for (int i = 0; i < clientInstance.getFeatures().size(); i++) { - org.elasticsearch.action.admin.cluster.migration.PostFeatureUpgradeResponse.Feature serverFeature - = serverTestInstance.getFeatures().get(i); + org.elasticsearch.action.admin.cluster.migration.PostFeatureUpgradeResponse.Feature serverFeature = serverTestInstance + .getFeatures() + .get(i); PostFeatureUpgradeResponse.Feature clientFeature = clientInstance.getFeatures().get(i); assertThat(clientFeature.getFeatureName(), equalTo(serverFeature.getFeatureName())); @@ -83,8 +90,10 @@ protected void assertInstances( assertThat(clientInstance.getElasticsearchException(), nullValue()); } else { assertThat(clientInstance.getElasticsearchException(), notNullValue()); - assertThat(clientInstance.getElasticsearchException().getMessage(), - containsString(serverTestInstance.getElasticsearchException().getMessage())); + assertThat( + clientInstance.getElasticsearchException().getMessage(), + containsString(serverTestInstance.getElasticsearchException().getMessage()) + ); } } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/CloseJobRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/CloseJobRequestTests.java index 505b182965ece..82fd0073cbc6d 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/CloseJobRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/CloseJobRequestTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.ArrayList; @@ -31,7 +31,6 @@ public void testWithNullJobIds() { assertEquals(exception.getMessage(), "jobIds must not contain null values"); } - @Override protected CloseJobRequest createTestInstance() { int jobCount = randomIntBetween(1, 10); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/CloseJobResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/CloseJobResponseTests.java index 03f46c2bab94c..d529c997af216 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/CloseJobResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/CloseJobResponseTests.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/DeleteCalendarEventRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/DeleteCalendarEventRequestTests.java index 54c7da3d4a3e7..d7916b2752368 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/DeleteCalendarEventRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/DeleteCalendarEventRequestTests.java @@ -13,14 +13,12 @@ public class DeleteCalendarEventRequestTests extends ESTestCase { public void testWithNullId() { - NullPointerException ex = expectThrows(NullPointerException.class, - () -> new DeleteCalendarEventRequest(null, "event1")); + NullPointerException ex = expectThrows(NullPointerException.class, () -> new DeleteCalendarEventRequest(null, "event1")); assertEquals("[calendar_id] must not be null.", ex.getMessage()); } public void testWithNullEvent() { - NullPointerException ex = expectThrows(NullPointerException.class, - () ->new DeleteCalendarEventRequest("calendarId", null)); + NullPointerException ex = expectThrows(NullPointerException.class, () -> new DeleteCalendarEventRequest("calendarId", null)); assertEquals("[event_id] must not be null.", ex.getMessage()); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/DeleteCalendarJobRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/DeleteCalendarJobRequestTests.java index ce51b116ef9d6..35cea9a9c71dd 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/DeleteCalendarJobRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/DeleteCalendarJobRequestTests.java @@ -13,20 +13,20 @@ public class DeleteCalendarJobRequestTests extends ESTestCase { public void testWithNullId() { - NullPointerException ex = expectThrows(NullPointerException.class, - () -> new DeleteCalendarJobRequest(null, "job1")); + NullPointerException ex = expectThrows(NullPointerException.class, () -> new DeleteCalendarJobRequest(null, "job1")); assertEquals("[calendar_id] must not be null.", ex.getMessage()); } public void testSetJobIds() { String calendarId = randomAlphaOfLength(10); - NullPointerException ex = expectThrows(NullPointerException.class, - () ->new DeleteCalendarJobRequest(calendarId, "job1", null)); + NullPointerException ex = expectThrows(NullPointerException.class, () -> new DeleteCalendarJobRequest(calendarId, "job1", null)); assertEquals("jobIds must not contain null values.", ex.getMessage()); - IllegalArgumentException illegalArgumentException = - expectThrows(IllegalArgumentException.class, () -> new DeleteCalendarJobRequest(calendarId)); + IllegalArgumentException illegalArgumentException = expectThrows( + IllegalArgumentException.class, + () -> new DeleteCalendarJobRequest(calendarId) + ); assertEquals("jobIds must not be empty.", illegalArgumentException.getMessage()); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/DeleteCalendarRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/DeleteCalendarRequestTests.java index da1d8433330f2..44687720bcb46 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/DeleteCalendarRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/DeleteCalendarRequestTests.java @@ -13,7 +13,6 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.not; - public class DeleteCalendarRequestTests extends ESTestCase { public void testWithNullId() { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/DeleteDataFrameAnalyticsRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/DeleteDataFrameAnalyticsRequestTests.java index 21d8ea7026336..dc0ad2270e48d 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/DeleteDataFrameAnalyticsRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/DeleteDataFrameAnalyticsRequestTests.java @@ -22,7 +22,9 @@ public void testValidate_Ok() { } public void testValidate_Failure() { - assertThat(new DeleteDataFrameAnalyticsRequest(null).validate().get().getMessage(), - containsString("data frame analytics id must not be null")); + assertThat( + new DeleteDataFrameAnalyticsRequest(null).validate().get().getMessage(), + containsString("data frame analytics id must not be null") + ); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/DeleteExpiredDataRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/DeleteExpiredDataRequestTests.java index 830c8e1bd3506..3bec55b3c9997 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/DeleteExpiredDataRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/DeleteExpiredDataRequestTests.java @@ -7,16 +7,15 @@ */ package org.elasticsearch.client.ml; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; - public class DeleteExpiredDataRequestTests extends AbstractXContentTestCase { private static ConstructingObjectParser PARSER = new ConstructingObjectParser<>( @@ -25,14 +24,17 @@ public class DeleteExpiredDataRequestTests extends AbstractXContentTestCase new DeleteExpiredDataRequest((String) a[0], (Float) a[1], (TimeValue) a[2]) ); static { - PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), - new ParseField(DeleteExpiredDataRequest.JOB_ID)); - PARSER.declareFloat(ConstructingObjectParser.optionalConstructorArg(), - new ParseField(DeleteExpiredDataRequest.REQUESTS_PER_SECOND)); - PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(), + PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), new ParseField(DeleteExpiredDataRequest.JOB_ID)); + PARSER.declareFloat( + ConstructingObjectParser.optionalConstructorArg(), + new ParseField(DeleteExpiredDataRequest.REQUESTS_PER_SECOND) + ); + PARSER.declareField( + ConstructingObjectParser.optionalConstructorArg(), (p, c) -> TimeValue.parseTimeValue(p.text(), DeleteExpiredDataRequest.TIMEOUT), new ParseField(DeleteExpiredDataRequest.TIMEOUT), - ObjectParser.ValueType.STRING); + ObjectParser.ValueType.STRING + ); } @Override @@ -40,7 +42,8 @@ protected DeleteExpiredDataRequest createTestInstance() { return new DeleteExpiredDataRequest( randomBoolean() ? null : randomAlphaOfLength(6), randomBoolean() ? null : randomFloat(), - randomBoolean() ? null : TimeValue.parseTimeValue(randomTimeValue(), "test")); + randomBoolean() ? null : TimeValue.parseTimeValue(randomTimeValue(), "test") + ); } @Override diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/DeleteExpiredDataResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/DeleteExpiredDataResponseTests.java index 65f3cca00e74e..4ea5637545c13 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/DeleteExpiredDataResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/DeleteExpiredDataResponseTests.java @@ -7,12 +7,11 @@ */ package org.elasticsearch.client.ml; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; - public class DeleteExpiredDataResponseTests extends AbstractXContentTestCase { @Override diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/DeleteForecastRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/DeleteForecastRequestTests.java index e99062f375991..e397933f747a9 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/DeleteForecastRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/DeleteForecastRequestTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml; import org.elasticsearch.client.ml.job.config.JobTests; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.ArrayList; @@ -24,7 +24,7 @@ protected DeleteForecastRequest createTestInstance() { if (randomBoolean()) { int length = randomInt(10); List ids = new ArrayList<>(length); - for(int i = 0; i < length; i++) { + for (int i = 0; i < length; i++) { ids.add(randomAlphaOfLength(10)); } deleteForecastRequest.setForecastIds(ids); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/DeleteJobResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/DeleteJobResponseTests.java index 86ef499f7071d..64bfe8d05fe5f 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/DeleteJobResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/DeleteJobResponseTests.java @@ -7,9 +7,9 @@ */ package org.elasticsearch.client.ml; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.tasks.TaskId; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/DeleteModelSnapshotRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/DeleteModelSnapshotRequestTests.java index 46db3d1523a6e..a351c999144f2 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/DeleteModelSnapshotRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/DeleteModelSnapshotRequestTests.java @@ -12,14 +12,18 @@ public class DeleteModelSnapshotRequestTests extends ESTestCase { public void test_WithNullJobId() { - NullPointerException ex = expectThrows(NullPointerException.class, () -> - new DeleteModelSnapshotRequest(null, randomAlphaOfLength(10))); + NullPointerException ex = expectThrows( + NullPointerException.class, + () -> new DeleteModelSnapshotRequest(null, randomAlphaOfLength(10)) + ); assertEquals("[job_id] must not be null", ex.getMessage()); } public void test_WithNullSnapshotId() { - NullPointerException ex = expectThrows(NullPointerException.class, () - -> new DeleteModelSnapshotRequest(randomAlphaOfLength(10), null)); + NullPointerException ex = expectThrows( + NullPointerException.class, + () -> new DeleteModelSnapshotRequest(randomAlphaOfLength(10), null) + ); assertEquals("[snapshot_id] must not be null", ex.getMessage()); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/DeleteTrainedModelRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/DeleteTrainedModelRequestTests.java index 35725b351ff25..b844311e02f36 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/DeleteTrainedModelRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/DeleteTrainedModelRequestTests.java @@ -22,7 +22,6 @@ public void testValidate_Ok() { } public void testValidate_Failure() { - assertThat(new DeleteTrainedModelRequest(null).validate().get().getMessage(), - containsString("trained model id must not be null")); + assertThat(new DeleteTrainedModelRequest(null).validate().get().getMessage(), containsString("trained model id must not be null")); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/EvaluateDataFrameRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/EvaluateDataFrameRequestTests.java index f5e01d4f59dee..aedd8c8068b47 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/EvaluateDataFrameRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/EvaluateDataFrameRequestTests.java @@ -14,11 +14,11 @@ import org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.OutlierDetectionTests; import org.elasticsearch.client.ml.dataframe.evaluation.regression.RegressionTests; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchModule; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.ArrayList; @@ -39,8 +39,11 @@ public static EvaluateDataFrameRequest createRandom() { QueryConfig queryConfig = randomBoolean() ? new QueryConfig(QueryBuilders.termQuery(randomAlphaOfLength(10), randomAlphaOfLength(10))) : null; - Evaluation evaluation = - randomFrom(OutlierDetectionTests.createRandom(), ClassificationTests.createRandom(), RegressionTests.createRandom()); + Evaluation evaluation = randomFrom( + OutlierDetectionTests.createRandom(), + ClassificationTests.createRandom(), + RegressionTests.createRandom() + ); return new EvaluateDataFrameRequest(indices, queryConfig, evaluation); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/EvaluateDataFrameResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/EvaluateDataFrameResponseTests.java index 1db9a03b81658..c4b52f46bc85f 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/EvaluateDataFrameResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/EvaluateDataFrameResponseTests.java @@ -12,17 +12,17 @@ import org.elasticsearch.client.ml.dataframe.evaluation.classification.AccuracyMetricResultTests; import org.elasticsearch.client.ml.dataframe.evaluation.classification.Classification; import org.elasticsearch.client.ml.dataframe.evaluation.classification.MulticlassConfusionMatrixMetricResultTests; -import org.elasticsearch.client.ml.dataframe.evaluation.regression.MeanSquaredErrorMetricResultTests; -import org.elasticsearch.client.ml.dataframe.evaluation.regression.RSquaredMetricResultTests; -import org.elasticsearch.client.ml.dataframe.evaluation.regression.Regression; import org.elasticsearch.client.ml.dataframe.evaluation.common.AucRocResultTests; -import org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.OutlierDetection; import org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.ConfusionMatrixMetricResultTests; +import org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.OutlierDetection; import org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.PrecisionMetricResultTests; import org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection.RecallMetricResultTests; +import org.elasticsearch.client.ml.dataframe.evaluation.regression.MeanSquaredErrorMetricResultTests; +import org.elasticsearch.client.ml.dataframe.evaluation.regression.RSquaredMetricResultTests; +import org.elasticsearch.client.ml.dataframe.evaluation.regression.Regression; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; import java.util.Arrays; @@ -41,13 +41,14 @@ public static EvaluateDataFrameResponse randomResponse() { AucRocResultTests.randomResult(), PrecisionMetricResultTests.randomResult(), RecallMetricResultTests.randomResult(), - ConfusionMatrixMetricResultTests.randomResult())); + ConfusionMatrixMetricResultTests.randomResult() + ) + ); break; case Regression.NAME: metrics = randomSubsetOf( - Arrays.asList( - MeanSquaredErrorMetricResultTests.randomResult(), - RSquaredMetricResultTests.randomResult())); + Arrays.asList(MeanSquaredErrorMetricResultTests.randomResult(), RSquaredMetricResultTests.randomResult()) + ); break; case Classification.NAME: metrics = randomSubsetOf( @@ -56,7 +57,9 @@ public static EvaluateDataFrameResponse randomResponse() { AccuracyMetricResultTests.randomResult(), org.elasticsearch.client.ml.dataframe.evaluation.classification.PrecisionMetricResultTests.randomResult(), org.elasticsearch.client.ml.dataframe.evaluation.classification.RecallMetricResultTests.randomResult(), - MulticlassConfusionMatrixMetricResultTests.randomResult())); + MulticlassConfusionMatrixMetricResultTests.randomResult() + ) + ); break; default: throw new AssertionError("Please add missing \"case\" variant to the \"switch\" statement"); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/ExplainDataFrameAnalyticsResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/ExplainDataFrameAnalyticsResponseTests.java index 751a83f26606f..8769694eacf88 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/ExplainDataFrameAnalyticsResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/ExplainDataFrameAnalyticsResponseTests.java @@ -11,8 +11,8 @@ import org.elasticsearch.client.ml.dataframe.explain.FieldSelectionTests; import org.elasticsearch.client.ml.dataframe.explain.MemoryEstimation; import org.elasticsearch.client.ml.dataframe.explain.MemoryEstimationTests; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.ArrayList; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/FlushJobRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/FlushJobRequestTests.java index 6137630589677..c292f0b914059 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/FlushJobRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/FlushJobRequestTests.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/FlushJobResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/FlushJobResponseTests.java index 88f659767c00a..db444edd9a51a 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/FlushJobResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/FlushJobResponseTests.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.Date; @@ -17,8 +17,7 @@ public class FlushJobResponseTests extends AbstractXContentTestCase { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetCalendarEventsResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetCalendarEventsResponseTests.java index b22bed91411cc..3a29474677d6e 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetCalendarEventsResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetCalendarEventsResponseTests.java @@ -10,8 +10,8 @@ import org.elasticsearch.client.ml.calendars.ScheduledEvent; import org.elasticsearch.client.ml.calendars.ScheduledEventTests; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.ArrayList; @@ -24,7 +24,7 @@ protected GetCalendarEventsResponse createTestInstance() { String calendarId = randomAlphaOfLength(10); List scheduledEvents = new ArrayList<>(); int count = randomIntBetween(0, 3); - for (int i=0; i { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetCalendarsResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetCalendarsResponseTests.java index d9d303863d13f..c64f8e5f65020 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetCalendarsResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetCalendarsResponseTests.java @@ -10,8 +10,8 @@ import org.elasticsearch.client.ml.calendars.Calendar; import org.elasticsearch.client.ml.calendars.CalendarTests; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.ArrayList; @@ -23,7 +23,7 @@ public class GetCalendarsResponseTests extends AbstractXContentTestCase calendars = new ArrayList<>(); int count = randomIntBetween(0, 3); - for (int i=0; i new GetDatafeedRequest("feed",null)); + Exception exception = expectThrows(NullPointerException.class, () -> new GetDatafeedRequest("feed", null)); assertEquals(exception.getMessage(), "datafeedIds must not contain null values"); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetDatafeedResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetDatafeedResponseTests.java index d5cf7e5ab297e..a0ec18926664b 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetDatafeedResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetDatafeedResponseTests.java @@ -9,8 +9,8 @@ import org.elasticsearch.client.ml.datafeed.DatafeedConfig; import org.elasticsearch.client.ml.datafeed.DatafeedConfigTests; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.ArrayList; @@ -23,7 +23,7 @@ public class GetDatafeedResponseTests extends AbstractXContentTestCase results = new ArrayList<>(count); - for(int i = 0; i < count; i++) { + for (int i = 0; i < count; i++) { DatafeedConfigTests.createRandomBuilder(); results.add(DatafeedConfigTests.createRandomBuilder()); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetDatafeedStatsRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetDatafeedStatsRequestTests.java index 78aff902af034..b05f065c1f76b 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetDatafeedStatsRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetDatafeedStatsRequestTests.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.ArrayList; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetDatafeedStatsResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetDatafeedStatsResponseTests.java index ba3d8c8dd0b37..9f26cef9d85f2 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetDatafeedStatsResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetDatafeedStatsResponseTests.java @@ -9,8 +9,8 @@ import org.elasticsearch.client.ml.datafeed.DatafeedStats; import org.elasticsearch.client.ml.datafeed.DatafeedStatsTests; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.ArrayList; @@ -24,7 +24,7 @@ protected GetDatafeedStatsResponse createTestInstance() { int count = randomIntBetween(1, 5); List results = new ArrayList<>(count); - for(int i = 0; i < count; i++) { + for (int i = 0; i < count; i++) { results.add(DatafeedStatsTests.createRandomInstance()); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetFiltersRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetFiltersRequestTests.java index 2698f88d1e561..2e53e126d1e4a 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetFiltersRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetFiltersRequestTests.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetFiltersResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetFiltersResponseTests.java index 96eca1db18a6b..6ebbd050683a9 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetFiltersResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetFiltersResponseTests.java @@ -9,8 +9,8 @@ import org.elasticsearch.client.ml.job.config.MlFilter; import org.elasticsearch.client.ml.job.config.MlFilterTests; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.ArrayList; @@ -22,7 +22,7 @@ public class GetFiltersResponseTests extends AbstractXContentTestCase results = new ArrayList<>(count); - for(int i = 0; i < count; i++) { + for (int i = 0; i < count; i++) { results.add(MlFilterTests.createRandomBuilder(randomAlphaOfLength(10))); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetInfluencersRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetInfluencersRequestTests.java index 761f0cdb462b5..a8772e5590749 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetInfluencersRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetInfluencersRequestTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml; import org.elasticsearch.client.core.PageParams; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetInfluencersResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetInfluencersResponseTests.java index 8925801f076fc..1f6d0e0aa9812 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetInfluencersResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetInfluencersResponseTests.java @@ -9,8 +9,8 @@ import org.elasticsearch.client.ml.job.results.Influencer; import org.elasticsearch.client.ml.job.results.InfluencerTests; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.ArrayList; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetJobRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetJobRequestTests.java index 56b68cc8cd17b..832322a7c2e9a 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetJobRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetJobRequestTests.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.ArrayList; @@ -24,7 +24,7 @@ public void testAllJobsRequest() { } public void testNewWithJobId() { - Exception exception = expectThrows(NullPointerException.class, () -> new GetJobRequest("job",null)); + Exception exception = expectThrows(NullPointerException.class, () -> new GetJobRequest("job", null)); assertEquals(exception.getMessage(), "jobIds must not contain null values"); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetJobResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetJobResponseTests.java index 2b38adf5e6882..c97353b274ef6 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetJobResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetJobResponseTests.java @@ -9,8 +9,8 @@ import org.elasticsearch.client.ml.job.config.Job; import org.elasticsearch.client.ml.job.config.JobTests; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.ArrayList; @@ -24,7 +24,7 @@ protected GetJobResponse createTestInstance() { int count = randomIntBetween(1, 5); List results = new ArrayList<>(count); - for(int i = 0; i < count; i++) { + for (int i = 0; i < count; i++) { results.add(JobTests.createRandomizedJobBuilder()); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetJobStatsRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetJobStatsRequestTests.java index 1dd33986ba290..7dbfee0d14e3c 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetJobStatsRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetJobStatsRequestTests.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.ArrayList; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetJobStatsResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetJobStatsResponseTests.java index 68dc33ac06c10..7d5a048783249 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetJobStatsResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetJobStatsResponseTests.java @@ -7,10 +7,10 @@ */ package org.elasticsearch.client.ml; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.client.ml.job.stats.JobStats; import org.elasticsearch.client.ml.job.stats.JobStatsTests; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.ArrayList; @@ -23,7 +23,7 @@ protected GetJobStatsResponse createTestInstance() { int count = randomIntBetween(1, 5); List results = new ArrayList<>(count); - for(int i = 0; i < count; i++) { + for (int i = 0; i < count; i++) { results.add(JobStatsTests.createRandomInstance()); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetModelSnapshotsRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetModelSnapshotsRequestTests.java index 426a607fa6a3d..260aceb1860d9 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetModelSnapshotsRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetModelSnapshotsRequestTests.java @@ -8,12 +8,11 @@ package org.elasticsearch.client.ml; import org.elasticsearch.client.core.PageParams; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; - public class GetModelSnapshotsRequestTests extends AbstractXContentTestCase { @Override @@ -21,8 +20,7 @@ protected GetModelSnapshotsRequest createTestInstance() { GetModelSnapshotsRequest request = new GetModelSnapshotsRequest(randomAlphaOfLengthBetween(1, 20)); if (randomBoolean()) { request.setSnapshotId(String.valueOf(randomNonNegativeLong())); - } - else { + } else { if (randomBoolean()) { request.setStart(String.valueOf(randomLong())); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetModelSnapshotsResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetModelSnapshotsResponseTests.java index 6b95214791f4e..14aa752369a29 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetModelSnapshotsResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetModelSnapshotsResponseTests.java @@ -9,8 +9,8 @@ import org.elasticsearch.client.ml.job.process.ModelSnapshot; import org.elasticsearch.client.ml.job.process.ModelSnapshotTests; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.ArrayList; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetOverallBucketsRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetOverallBucketsRequestTests.java index 597f6d3862912..54c8f1c0a4472 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetOverallBucketsRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetOverallBucketsRequestTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetOverallBucketsResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetOverallBucketsResponseTests.java index 80aaec33b8704..12f160b7f361c 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetOverallBucketsResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetOverallBucketsResponseTests.java @@ -9,8 +9,8 @@ import org.elasticsearch.client.ml.job.results.OverallBucket; import org.elasticsearch.client.ml.job.results.OverallBucketTests; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.ArrayList; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetRecordsRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetRecordsRequestTests.java index 84125734898c3..ef1cf445e268c 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetRecordsRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetRecordsRequestTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml; import org.elasticsearch.client.core.PageParams; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetRecordsResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetRecordsResponseTests.java index 78669d09b6c27..147c2d78345de 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetRecordsResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetRecordsResponseTests.java @@ -9,8 +9,8 @@ import org.elasticsearch.client.ml.job.results.AnomalyRecord; import org.elasticsearch.client.ml.job.results.AnomalyRecordTests; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.ArrayList; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetTrainedModelsRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetTrainedModelsRequestTests.java index 51f10d7ae44dc..6705e831eef3c 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetTrainedModelsRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetTrainedModelsRequestTests.java @@ -22,7 +22,9 @@ public void testValidate_Ok() { } public void testValidate_Failure() { - assertThat(new GetTrainedModelsRequest(new String[0]).validate().get().getMessage(), - containsString("trained model id must not be null")); + assertThat( + new GetTrainedModelsRequest(new String[0]).validate().get().getMessage(), + containsString("trained model id must not be null") + ); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetTrainedModelsStatsRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetTrainedModelsStatsRequestTests.java index 65cf981765c0a..5ba52e43bfa6e 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetTrainedModelsStatsRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetTrainedModelsStatsRequestTests.java @@ -22,7 +22,9 @@ public void testValidate_Ok() { } public void testValidate_Failure() { - assertThat(new GetTrainedModelsStatsRequest(new String[0]).validate().get().getMessage(), - containsString("trained model id must not be null")); + assertThat( + new GetTrainedModelsStatsRequest(new String[0]).validate().get().getMessage(), + containsString("trained model id must not be null") + ); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/NodeAttributesTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/NodeAttributesTests.java index 4df2d41ca9835..1a535f216788b 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/NodeAttributesTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/NodeAttributesTests.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.HashMap; @@ -20,15 +20,17 @@ public class NodeAttributesTests extends AbstractXContentTestCase attributes = new HashMap<>(numberOfAttributes); - for(int i = 0; i < numberOfAttributes; i++) { + for (int i = 0; i < numberOfAttributes; i++) { String val = randomAlphaOfLength(10); - attributes.put("key-"+i, val); + attributes.put("key-" + i, val); } - return new NodeAttributes(randomAlphaOfLength(10), + return new NodeAttributes( + randomAlphaOfLength(10), randomAlphaOfLength(10), randomAlphaOfLength(10), randomAlphaOfLength(10), - attributes); + attributes + ); } @Override diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/OpenJobRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/OpenJobRequestTests.java index cf6b4c5a0857c..9d66bcd593aeb 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/OpenJobRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/OpenJobRequestTests.java @@ -9,8 +9,8 @@ import org.elasticsearch.client.ml.job.config.JobTests; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/OpenJobResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/OpenJobResponseTests.java index 7d6e97add59ae..4b6b8c2c2f2a1 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/OpenJobResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/OpenJobResponseTests.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PostCalendarEventRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PostCalendarEventRequestTests.java index 1d678c41d05ef..65cb8ae715931 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PostCalendarEventRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PostCalendarEventRequestTests.java @@ -10,8 +10,8 @@ import org.elasticsearch.client.ml.calendars.ScheduledEvent; import org.elasticsearch.client.ml.calendars.ScheduledEventTests; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.ArrayList; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PostCalendarEventResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PostCalendarEventResponseTests.java index bfcfda7e6f097..c830fc03f8882 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PostCalendarEventResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PostCalendarEventResponseTests.java @@ -10,8 +10,8 @@ import org.elasticsearch.client.ml.calendars.ScheduledEvent; import org.elasticsearch.client.ml.calendars.ScheduledEventTests; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.ArrayList; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PostDataRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PostDataRequestTests.java index adf535fe47591..25cffd590913d 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PostDataRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PostDataRequestTests.java @@ -7,16 +7,15 @@ */ package org.elasticsearch.client.ml; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; - public class PostDataRequestTests extends AbstractXContentTestCase { @Override @@ -26,7 +25,7 @@ protected PostDataRequest createTestInstance() { PostDataRequest request = new PostDataRequest(jobId, contentType, new byte[0]); if (randomBoolean()) { - request.setResetEnd(randomAlphaOfLength(10)); + request.setResetEnd(randomAlphaOfLength(10)); } if (randomBoolean()) { request.setResetStart(randomAlphaOfLength(10)); @@ -60,17 +59,21 @@ public void testJsonBuilder() throws IOException { PostDataRequest request = new PostDataRequest(jobId, builder); - assertEquals("{\"entry1\":\"value1\",\"entry2\":\"value2\"}{\"entry3\":\"value3\"}{\"entry4\":\"value4\"}", - request.getContent().utf8ToString()); + assertEquals( + "{\"entry1\":\"value1\",\"entry2\":\"value2\"}{\"entry3\":\"value3\"}{\"entry4\":\"value4\"}", + request.getContent().utf8ToString() + ); assertEquals(XContentType.JSON, request.getXContentType()); assertEquals(jobId, request.getJobId()); } public void testFromByteArray() { String jobId = randomAlphaOfLength(10); - PostDataRequest request = new PostDataRequest(jobId, + PostDataRequest request = new PostDataRequest( + jobId, XContentType.JSON, - "{\"others\":{\"foo\":100}}".getBytes(StandardCharsets.UTF_8)); + "{\"others\":{\"foo\":100}}".getBytes(StandardCharsets.UTF_8) + ); assertEquals("{\"others\":{\"foo\":100}}", request.getContent().utf8ToString()); assertEquals(XContentType.JSON, request.getXContentType()); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PostDataResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PostDataResponseTests.java index 541decb78d07c..6b94af06f0dee 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PostDataResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PostDataResponseTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml; import org.elasticsearch.client.ml.job.process.DataCountsTests; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PreviewDatafeedRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PreviewDatafeedRequestTests.java index b491ebced781d..4067ba19edb0c 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PreviewDatafeedRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PreviewDatafeedRequestTests.java @@ -9,8 +9,8 @@ import org.elasticsearch.client.ml.datafeed.DatafeedConfigTests; import org.elasticsearch.client.ml.job.config.JobTests; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -18,9 +18,9 @@ public class PreviewDatafeedRequestTests extends AbstractXContentTestCase (String)map.get("airline")) - .collect(Collectors.toList()), containsInAnyOrder("JZA", "JBU", "KLM")); + assertThat( + response.getDataList().stream().map(map -> (String) map.get("airline")).collect(Collectors.toList()), + containsInAnyOrder("JZA", "JBU", "KLM") + ); rawData = "{\"key\":\"my_value\"}"; bytes = new BytesArray(rawData); response = new PreviewDatafeedResponse(bytes); - assertThat(response.getDataList() - .stream() - .map(map -> (String)map.get("key")) - .collect(Collectors.toList()), containsInAnyOrder("my_value")); + assertThat( + response.getDataList().stream().map(map -> (String) map.get("key")).collect(Collectors.toList()), + containsInAnyOrder("my_value") + ); } - //Because this is raw a BytesReference, the shuffling done via `AbstractXContentTestCase` is unacceptable and causes equality failures + // Because this is raw a BytesReference, the shuffling done via `AbstractXContentTestCase` is unacceptable and causes equality failures public void testSerializationDeserialization() throws IOException { for (int runs = 0; runs < 20; runs++) { XContentType xContentType = XContentType.JSON; @@ -81,7 +81,8 @@ public void testSerializationDeserialization() throws IOException { assertToXContentEquivalent( XContentHelper.toXContent(testInstance, xContentType, false), XContentHelper.toXContent(parsed, xContentType, false), - xContentType); + xContentType + ); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutCalendarActionResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutCalendarActionResponseTests.java index 025861252ed81..0dbbb11024326 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutCalendarActionResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutCalendarActionResponseTests.java @@ -8,6 +8,7 @@ package org.elasticsearch.client.ml; import com.carrotsearch.randomizedtesting.generators.CodepointSetGenerator; + import org.elasticsearch.client.AbstractResponseTestCase; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; @@ -47,7 +48,7 @@ protected PutCalendarResponse doParseToClientInstance(XContentParser parser) thr protected void assertInstances(PutCalendarAction.Response serverTestInstance, PutCalendarResponse clientInstance) { org.elasticsearch.client.ml.calendars.Calendar hlrcCalendar = clientInstance.getCalendar(); Calendar internalCalendar = new Calendar(hlrcCalendar.getId(), hlrcCalendar.getJobIds(), hlrcCalendar.getDescription()); - PutCalendarAction.Response convertedServerTestInstance =new PutCalendarAction.Response(internalCalendar); + PutCalendarAction.Response convertedServerTestInstance = new PutCalendarAction.Response(internalCalendar); assertThat(convertedServerTestInstance, equalTo(serverTestInstance)); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutCalendarJobRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutCalendarJobRequestTests.java index b01b331bd495e..94363a8cc008b 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutCalendarJobRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutCalendarJobRequestTests.java @@ -13,20 +13,20 @@ public class PutCalendarJobRequestTests extends ESTestCase { public void testWithNullId() { - NullPointerException ex = expectThrows(NullPointerException.class, - () -> new PutCalendarJobRequest(null, "job1")); + NullPointerException ex = expectThrows(NullPointerException.class, () -> new PutCalendarJobRequest(null, "job1")); assertEquals("[calendar_id] must not be null.", ex.getMessage()); } public void testSetJobIds() { String calendarId = randomAlphaOfLength(10); - NullPointerException ex = expectThrows(NullPointerException.class, - () ->new PutCalendarJobRequest(calendarId, "job1", null)); + NullPointerException ex = expectThrows(NullPointerException.class, () -> new PutCalendarJobRequest(calendarId, "job1", null)); assertEquals("jobIds must not contain null values.", ex.getMessage()); - IllegalArgumentException illegalArgumentException = - expectThrows(IllegalArgumentException.class, () -> new PutCalendarJobRequest(calendarId)); + IllegalArgumentException illegalArgumentException = expectThrows( + IllegalArgumentException.class, + () -> new PutCalendarJobRequest(calendarId) + ); assertEquals("jobIds must not be empty.", illegalArgumentException.getMessage()); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutCalendarRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutCalendarRequestTests.java index 6fa2fb108a0b8..923c4ed70a4b8 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutCalendarRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutCalendarRequestTests.java @@ -10,8 +10,8 @@ import org.elasticsearch.client.ml.calendars.Calendar; import org.elasticsearch.client.ml.calendars.CalendarTests; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutCalendarResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutCalendarResponseTests.java index c6b281d6ee6fd..e5363ea1a898d 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutCalendarResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutCalendarResponseTests.java @@ -9,8 +9,8 @@ package org.elasticsearch.client.ml; import org.elasticsearch.client.ml.calendars.CalendarTests; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutDataFrameAnalyticsRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutDataFrameAnalyticsRequestTests.java index da634d37058a4..1c42c65d4f6f0 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutDataFrameAnalyticsRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutDataFrameAnalyticsRequestTests.java @@ -13,10 +13,10 @@ import org.elasticsearch.client.ml.dataframe.DataFrameAnalyticsConfigTests; import org.elasticsearch.client.ml.dataframe.MlDataFrameAnalysisNamedXContentProvider; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.SearchModule; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.ArrayList; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutDatafeedRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutDatafeedRequestTests.java index c8a9676cbb5ea..3596c7d9321d0 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutDatafeedRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutDatafeedRequestTests.java @@ -9,9 +9,8 @@ import org.elasticsearch.client.ml.datafeed.DatafeedConfig; import org.elasticsearch.client.ml.datafeed.DatafeedConfigTests; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; - +import org.elasticsearch.xcontent.XContentParser; public class PutDatafeedRequestTests extends AbstractXContentTestCase { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutDatafeedResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutDatafeedResponseTests.java index d3d717782cd27..ceca8aed51aaa 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutDatafeedResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutDatafeedResponseTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml; import org.elasticsearch.client.ml.datafeed.DatafeedConfigTests; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.function.Predicate; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutFilterRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutFilterRequestTests.java index fc334d4cb02ba..52c739a2d2e56 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutFilterRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutFilterRequestTests.java @@ -9,9 +9,8 @@ import org.elasticsearch.client.ml.job.config.MlFilter; import org.elasticsearch.client.ml.job.config.MlFilterTests; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; - +import org.elasticsearch.xcontent.XContentParser; public class PutFilterRequestTests extends AbstractXContentTestCase { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutFilterResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutFilterResponseTests.java index e3ce9efadaa95..277f26eb3a2e8 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutFilterResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutFilterResponseTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml; import org.elasticsearch.client.ml.job.config.MlFilterTests; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutJobRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutJobRequestTests.java index 3529c9119b9d3..f93c12d5489e5 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutJobRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutJobRequestTests.java @@ -9,9 +9,8 @@ import org.elasticsearch.client.ml.job.config.Job; import org.elasticsearch.client.ml.job.config.JobTests; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; - +import org.elasticsearch.xcontent.XContentParser; public class PutJobRequestTests extends AbstractXContentTestCase { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutJobResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutJobResponseTests.java index a10e310ee32d3..b1f81498f23d1 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutJobResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutJobResponseTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml; import org.elasticsearch.client.ml.job.config.JobTests; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutTrainedModelActionRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutTrainedModelActionRequestTests.java index ead466a7f2bad..df60d29bdd4dc 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutTrainedModelActionRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutTrainedModelActionRequestTests.java @@ -10,9 +10,9 @@ import org.elasticsearch.client.ml.inference.MlInferenceNamedXContentProvider; import org.elasticsearch.client.ml.inference.TrainedModelConfig; import org.elasticsearch.client.ml.inference.TrainedModelConfigTests; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutTrainedModelActionResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutTrainedModelActionResponseTests.java index 25aa3aab8063b..305f5913d26a5 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutTrainedModelActionResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PutTrainedModelActionResponseTests.java @@ -10,9 +10,9 @@ import org.elasticsearch.client.ml.inference.MlInferenceNamedXContentProvider; import org.elasticsearch.client.ml.inference.TrainedModelConfig; import org.elasticsearch.client.ml.inference.TrainedModelConfigTests; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/RevertModelSnapshotRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/RevertModelSnapshotRequestTests.java index dd6633a16019f..4eef39dfcb538 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/RevertModelSnapshotRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/RevertModelSnapshotRequestTests.java @@ -7,12 +7,11 @@ */ package org.elasticsearch.client.ml; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; - public class RevertModelSnapshotRequestTests extends AbstractXContentTestCase { @Override diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/RevertModelSnapshotResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/RevertModelSnapshotResponseTests.java index 0c0f268e7ea67..e5705c36fb459 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/RevertModelSnapshotResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/RevertModelSnapshotResponseTests.java @@ -9,12 +9,11 @@ import org.elasticsearch.client.ml.job.process.ModelSnapshot; import org.elasticsearch.client.ml.job.process.ModelSnapshotTests; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; - public class RevertModelSnapshotResponseTests extends AbstractXContentTestCase { @Override diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/StartDataFrameAnalyticsRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/StartDataFrameAnalyticsRequestTests.java index 5563f137778b6..f43357a0ac59e 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/StartDataFrameAnalyticsRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/StartDataFrameAnalyticsRequestTests.java @@ -24,9 +24,13 @@ public void testValidate_Ok() { } public void testValidate_Failure() { - assertThat(new StartDataFrameAnalyticsRequest(null).validate().get().getMessage(), - containsString("data frame analytics id must not be null")); - assertThat(new StartDataFrameAnalyticsRequest(null).setTimeout(TimeValue.ZERO).validate().get().getMessage(), - containsString("data frame analytics id must not be null")); + assertThat( + new StartDataFrameAnalyticsRequest(null).validate().get().getMessage(), + containsString("data frame analytics id must not be null") + ); + assertThat( + new StartDataFrameAnalyticsRequest(null).setTimeout(TimeValue.ZERO).validate().get().getMessage(), + containsString("data frame analytics id must not be null") + ); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/StartDataFrameAnalyticsResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/StartDataFrameAnalyticsResponseTests.java index eb83d0affaab1..c54d6d1a428f2 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/StartDataFrameAnalyticsResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/StartDataFrameAnalyticsResponseTests.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/StartDatafeedRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/StartDatafeedRequestTests.java index 79e80dae95766..cb7c5e930e9f1 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/StartDatafeedRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/StartDatafeedRequestTests.java @@ -9,8 +9,8 @@ import org.elasticsearch.client.ml.datafeed.DatafeedConfigTests; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/StartDatafeedResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/StartDatafeedResponseTests.java index 30057eac354f1..2d918ede18dc6 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/StartDatafeedResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/StartDatafeedResponseTests.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/StopDataFrameAnalyticsRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/StopDataFrameAnalyticsRequestTests.java index 7bd587ca2db58..ba0bf6474e768 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/StopDataFrameAnalyticsRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/StopDataFrameAnalyticsRequestTests.java @@ -24,9 +24,13 @@ public void testValidate_Ok() { } public void testValidate_Failure() { - assertThat(new StopDataFrameAnalyticsRequest(null).validate().get().getMessage(), - containsString("data frame analytics id must not be null")); - assertThat(new StopDataFrameAnalyticsRequest(null).setTimeout(TimeValue.ZERO).validate().get().getMessage(), - containsString("data frame analytics id must not be null")); + assertThat( + new StopDataFrameAnalyticsRequest(null).validate().get().getMessage(), + containsString("data frame analytics id must not be null") + ); + assertThat( + new StopDataFrameAnalyticsRequest(null).setTimeout(TimeValue.ZERO).validate().get().getMessage(), + containsString("data frame analytics id must not be null") + ); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/StopDataFrameAnalyticsResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/StopDataFrameAnalyticsResponseTests.java index f7ba89de54d7f..6080b5d9a6cef 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/StopDataFrameAnalyticsResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/StopDataFrameAnalyticsResponseTests.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/StopDatafeedRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/StopDatafeedRequestTests.java index 7c5c3583ba019..fae19a174fd1b 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/StopDatafeedRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/StopDatafeedRequestTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.ArrayList; @@ -31,7 +31,6 @@ public void testWithNullDatafeedIds() { assertEquals(exception.getMessage(), "datafeedIds must not contain null values"); } - @Override protected StopDatafeedRequest createTestInstance() { int datafeedCount = randomIntBetween(1, 10); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/StopDatafeedResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/StopDatafeedResponseTests.java index 3b409599ef745..89394ccb40420 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/StopDatafeedResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/StopDatafeedResponseTests.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/UpdateDataFrameAnalyticsRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/UpdateDataFrameAnalyticsRequestTests.java index 61443100f2b5c..e6837857e35f1 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/UpdateDataFrameAnalyticsRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/UpdateDataFrameAnalyticsRequestTests.java @@ -13,10 +13,10 @@ import org.elasticsearch.client.ml.dataframe.DataFrameAnalyticsConfigUpdateTests; import org.elasticsearch.client.ml.dataframe.MlDataFrameAnalysisNamedXContentProvider; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.SearchModule; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.ArrayList; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/UpdateDatafeedRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/UpdateDatafeedRequestTests.java index 27a1de020b1fb..f24a892c53308 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/UpdateDatafeedRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/UpdateDatafeedRequestTests.java @@ -9,9 +9,8 @@ import org.elasticsearch.client.ml.datafeed.DatafeedUpdate; import org.elasticsearch.client.ml.datafeed.DatafeedUpdateTests; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; - +import org.elasticsearch.xcontent.XContentParser; public class UpdateDatafeedRequestTests extends AbstractXContentTestCase { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/UpdateFilterRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/UpdateFilterRequestTests.java index 3b2dc2fc42637..cf944bb095a33 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/UpdateFilterRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/UpdateFilterRequestTests.java @@ -7,13 +7,12 @@ */ package org.elasticsearch.client.ml; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.util.ArrayList; import java.util.List; - public class UpdateFilterRequestTests extends AbstractXContentTestCase { @Override diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/UpdateJobRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/UpdateJobRequestTests.java index c3a30a7fb5654..b4a91f83b23f3 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/UpdateJobRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/UpdateJobRequestTests.java @@ -10,9 +10,8 @@ import org.elasticsearch.client.ml.job.config.JobTests; import org.elasticsearch.client.ml.job.config.JobUpdate; import org.elasticsearch.client.ml.job.config.JobUpdateTests; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; - +import org.elasticsearch.xcontent.XContentParser; public class UpdateJobRequestTests extends AbstractXContentTestCase { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/UpdateModelSnapshotRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/UpdateModelSnapshotRequestTests.java index 0c971c144407b..9e17750dbadab 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/UpdateModelSnapshotRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/UpdateModelSnapshotRequestTests.java @@ -7,12 +7,11 @@ */ package org.elasticsearch.client.ml; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; - public class UpdateModelSnapshotRequestTests extends AbstractXContentTestCase { @Override diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/UpdateModelSnapshotResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/UpdateModelSnapshotResponseTests.java index 43845a1d9292e..bcd652fad9fe4 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/UpdateModelSnapshotResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/UpdateModelSnapshotResponseTests.java @@ -9,18 +9,17 @@ import org.elasticsearch.client.ml.job.process.ModelSnapshot; import org.elasticsearch.client.ml.job.process.ModelSnapshotTests; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; - public class UpdateModelSnapshotResponseTests extends AbstractXContentTestCase { @Override protected UpdateModelSnapshotResponse createTestInstance() { Boolean acknowledged = randomBoolean(); - ModelSnapshot.Builder modelBuilder = ModelSnapshotTests.createRandomizedBuilder(); + ModelSnapshot.Builder modelBuilder = ModelSnapshotTests.createRandomizedBuilder(); return new UpdateModelSnapshotResponse(acknowledged, modelBuilder); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/UpgradeJobModelSnapshotRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/UpgradeJobModelSnapshotRequestTests.java index de71552778249..268649f347f8d 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/UpgradeJobModelSnapshotRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/UpgradeJobModelSnapshotRequestTests.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -16,10 +16,12 @@ public class UpgradeJobModelSnapshotRequestTests extends AbstractXContentTestCas @Override protected UpgradeJobModelSnapshotRequest createTestInstance() { - return new UpgradeJobModelSnapshotRequest(randomAlphaOfLength(10), + return new UpgradeJobModelSnapshotRequest( + randomAlphaOfLength(10), randomAlphaOfLength(10), randomBoolean() ? null : randomTimeValue(), - randomBoolean() ? null : randomBoolean()); + randomBoolean() ? null : randomBoolean() + ); } @Override diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/UpgradeJobModelSnapshotResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/UpgradeJobModelSnapshotResponseTests.java index 440d570459109..3b792945291c1 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/UpgradeJobModelSnapshotResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/UpgradeJobModelSnapshotResponseTests.java @@ -7,18 +7,19 @@ */ package org.elasticsearch.client.ml; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; - public class UpgradeJobModelSnapshotResponseTests extends AbstractXContentTestCase { @Override protected UpgradeJobModelSnapshotResponse createTestInstance() { - return new UpgradeJobModelSnapshotResponse(randomBoolean() ? null : randomBoolean(), - randomBoolean() ? null : randomAlphaOfLength(10)); + return new UpgradeJobModelSnapshotResponse( + randomBoolean() ? null : randomBoolean(), + randomBoolean() ? null : randomAlphaOfLength(10) + ); } @Override diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/calendars/CalendarTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/calendars/CalendarTests.java index 783369a865701..ce904c1049e91 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/calendars/CalendarTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/calendars/CalendarTests.java @@ -9,8 +9,9 @@ package org.elasticsearch.client.ml.calendars; import com.carrotsearch.randomizedtesting.generators.CodepointSetGenerator; -import org.elasticsearch.xcontent.XContentParser; + import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.ArrayList; @@ -29,7 +30,7 @@ public static Calendar testInstance() { description = randomAlphaOfLength(20); } - CodepointSetGenerator generator = new CodepointSetGenerator("abcdefghijklmnopqrstuvwxyz".toCharArray()); + CodepointSetGenerator generator = new CodepointSetGenerator("abcdefghijklmnopqrstuvwxyz".toCharArray()); return new Calendar(generator.ofCodePointsLength(random(), 10, 10), items, description); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/calendars/ScheduledEventTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/calendars/ScheduledEventTests.java index 28dedbc322b7a..f2bbd7fb5aebb 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/calendars/ScheduledEventTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/calendars/ScheduledEventTests.java @@ -8,10 +8,10 @@ package org.elasticsearch.client.ml.calendars; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.time.DateUtils; -import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.core.Nullable; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.util.Date; @@ -19,15 +19,14 @@ public class ScheduledEventTests extends AbstractXContentTestCase scriptFields = new ArrayList<>(scriptsSize); for (int scriptIndex = 0; scriptIndex < scriptsSize; scriptIndex++) { - scriptFields.add(new ScriptField(randomAlphaOfLength(10), mockScript(randomAlphaOfLength(10)), - randomBoolean())); + scriptFields.add(new ScriptField(randomAlphaOfLength(10), mockScript(randomAlphaOfLength(10)), randomBoolean())); } builder.setScriptFields(scriptFields); } @@ -71,8 +71,12 @@ public static DatafeedConfig.Builder createRandomBuilder() { aggHistogramInterval = aggHistogramInterval > bucketSpanMillis ? bucketSpanMillis : aggHistogramInterval; aggHistogramInterval = aggHistogramInterval <= 0 ? 1 : aggHistogramInterval; MaxAggregationBuilder maxTime = AggregationBuilders.max("time").field("time"); - aggs.addAggregator(AggregationBuilders.dateHistogram("buckets") - .fixedInterval(new DateHistogramInterval(aggHistogramInterval + "ms")).subAggregation(maxTime).field("time")); + aggs.addAggregator( + AggregationBuilders.dateHistogram("buckets") + .fixedInterval(new DateHistogramInterval(aggHistogramInterval + "ms")) + .subAggregation(maxTime) + .field("time") + ); try { builder.setAggregations(aggs); } catch (IOException e) { @@ -102,11 +106,9 @@ public static DatafeedConfig.Builder createRandomBuilder() { builder.setMaxEmptySearches(randomIntBetween(10, 100)); } if (randomBoolean()) { - builder.setIndicesOptions(IndicesOptions.fromOptions(randomBoolean(), - randomBoolean(), - randomBoolean(), - randomBoolean(), - randomBoolean())); + builder.setIndicesOptions( + IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean()) + ); } if (randomBoolean()) { Map settings = new HashMap<>(); @@ -138,14 +140,14 @@ protected boolean supportsUnknownFields() { return false; } - private static final String FUTURE_DATAFEED = "{\n" + - " \"datafeed_id\": \"farequote-datafeed\",\n" + - " \"job_id\": \"farequote\",\n" + - " \"frequency\": \"1h\",\n" + - " \"indices\": [\"farequote1\", \"farequote2\"],\n" + - " \"tomorrows_technology_today\": \"amazing\",\n" + - " \"scroll_size\": 1234\n" + - "}"; + private static final String FUTURE_DATAFEED = "{\n" + + " \"datafeed_id\": \"farequote-datafeed\",\n" + + " \"job_id\": \"farequote\",\n" + + " \"frequency\": \"1h\",\n" + + " \"indices\": [\"farequote1\", \"farequote2\"],\n" + + " \"tomorrows_technology_today\": \"amazing\",\n" + + " \"scroll_size\": 1234\n" + + "}"; public void testFutureMetadataParse() throws IOException { XContentParser parser = XContentFactory.xContent(XContentType.JSON) diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/datafeed/DatafeedStatsTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/datafeed/DatafeedStatsTests.java index b582a5452e460..8eabca57e7c61 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/datafeed/DatafeedStatsTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/datafeed/DatafeedStatsTests.java @@ -9,8 +9,8 @@ import org.elasticsearch.client.ml.NodeAttributes; import org.elasticsearch.client.ml.NodeAttributesTests; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.HashMap; @@ -21,22 +21,28 @@ public class DatafeedStatsTests extends AbstractXContentTestCase public static DatafeedStats createRandomInstance() { String datafeedId = DatafeedConfigTests.randomValidDatafeedId(); - DatafeedState datafeedState = - randomFrom(DatafeedState.STARTED, DatafeedState.STARTING, DatafeedState.STOPPED, DatafeedState.STOPPING); + DatafeedState datafeedState = randomFrom( + DatafeedState.STARTED, + DatafeedState.STARTING, + DatafeedState.STOPPED, + DatafeedState.STOPPING + ); NodeAttributes nodeAttributes = null; if (randomBoolean()) { NodeAttributes randomAttributes = NodeAttributesTests.createRandom(); int numberOfAttributes = randomIntBetween(1, 10); Map attributes = new HashMap<>(numberOfAttributes); - for(int i = 0; i < numberOfAttributes; i++) { + for (int i = 0; i < numberOfAttributes; i++) { String val = randomAlphaOfLength(10); - attributes.put("ml.key-"+i, val); + attributes.put("ml.key-" + i, val); } - nodeAttributes = new NodeAttributes(randomAttributes.getId(), + nodeAttributes = new NodeAttributes( + randomAttributes.getId(), randomAttributes.getName(), randomAttributes.getEphemeralId(), randomAttributes.getTransportAddress(), - attributes); + attributes + ); } String assignmentReason = randomBoolean() ? randomAlphaOfLength(10) : null; DatafeedTimingStats timingStats = DatafeedTimingStatsTests.createRandomInstance(); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/datafeed/DatafeedTimingStatsTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/datafeed/DatafeedTimingStatsTests.java index e83cd82f16514..d76f1ce6d5240 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/datafeed/DatafeedTimingStatsTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/datafeed/DatafeedTimingStatsTests.java @@ -7,11 +7,11 @@ */ package org.elasticsearch.client.ml.datafeed; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; @@ -29,7 +29,8 @@ public static DatafeedTimingStats createRandomInstance() { randomLong(), randomDouble(), randomBoolean() ? null : randomDouble(), - randomBoolean() ? null : randomDouble()); + randomBoolean() ? null : randomDouble() + ); } @Override @@ -49,9 +50,10 @@ protected boolean supportsUnknownFields() { public void testParse_OptionalFieldsAbsent() throws IOException { String json = "{\"job_id\": \"my-job-id\"}"; - try (XContentParser parser = - XContentFactory.xContent(XContentType.JSON).createParser( - xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json)) { + try ( + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json) + ) { DatafeedTimingStats stats = DatafeedTimingStats.PARSER.apply(parser, null); assertThat(stats.getJobId(), equalTo(JOB_ID)); assertThat(stats.getSearchCount(), equalTo(0L)); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/datafeed/DatafeedUpdateTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/datafeed/DatafeedUpdateTests.java index fdaa20a30c6bf..9ac62eecc5f14 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/datafeed/DatafeedUpdateTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/datafeed/DatafeedUpdateTests.java @@ -9,12 +9,12 @@ import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.AggregatorFactories; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.ArrayList; @@ -46,8 +46,9 @@ public static DatafeedUpdate createRandom() { int scriptsSize = randomInt(3); List scriptFields = new ArrayList<>(scriptsSize); for (int scriptIndex = 0; scriptIndex < scriptsSize; scriptIndex++) { - scriptFields.add(new SearchSourceBuilder.ScriptField(randomAlphaOfLength(10), mockScript(randomAlphaOfLength(10)), - randomBoolean())); + scriptFields.add( + new SearchSourceBuilder.ScriptField(randomAlphaOfLength(10), mockScript(randomAlphaOfLength(10)), randomBoolean()) + ); } builder.setScriptFields(scriptFields); } @@ -76,11 +77,9 @@ public static DatafeedUpdate createRandom() { builder.setMaxEmptySearches(randomIntBetween(10, 100)); } if (randomBoolean()) { - builder.setIndicesOptions(IndicesOptions.fromOptions(randomBoolean(), - randomBoolean(), - randomBoolean(), - randomBoolean(), - randomBoolean())); + builder.setIndicesOptions( + IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean()) + ); } if (randomBoolean()) { Map settings = new HashMap<>(); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/datafeed/DelayedDataCheckConfigTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/datafeed/DelayedDataCheckConfigTests.java index 60511820e32f6..60337e023f417 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/datafeed/DelayedDataCheckConfigTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/datafeed/DelayedDataCheckConfigTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml.datafeed; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import static org.hamcrest.Matchers.equalTo; @@ -51,4 +51,3 @@ public static DelayedDataCheckConfig createRandomizedConfig() { return new DelayedDataCheckConfig(enabled, timeWindow); } } - diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/ClassificationTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/ClassificationTests.java index 6ffc9cd4e4487..0399a5d87623e 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/ClassificationTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/ClassificationTests.java @@ -11,9 +11,9 @@ import org.elasticsearch.client.ml.inference.preprocessing.FrequencyEncodingTests; import org.elasticsearch.client.ml.inference.preprocessing.OneHotEncodingTests; import org.elasticsearch.client.ml.inference.preprocessing.TargetMeanEncodingTests; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; import java.util.ArrayList; @@ -37,12 +37,17 @@ public static Classification randomClassification() { .setRandomizeSeed(randomBoolean() ? null : randomLong()) .setClassAssignmentObjective(randomBoolean() ? null : randomFrom(Classification.ClassAssignmentObjective.values())) .setNumTopClasses(randomBoolean() ? null : randomIntBetween(-1, 1000)) - .setFeatureProcessors(randomBoolean() ? null : - Stream.generate(() -> randomFrom(FrequencyEncodingTests.createRandom(), - OneHotEncodingTests.createRandom(), - TargetMeanEncodingTests.createRandom())) - .limit(randomIntBetween(1, 10)) - .collect(Collectors.toList())) + .setFeatureProcessors( + randomBoolean() + ? null + : Stream.generate( + () -> randomFrom( + FrequencyEncodingTests.createRandom(), + OneHotEncodingTests.createRandom(), + TargetMeanEncodingTests.createRandom() + ) + ).limit(randomIntBetween(1, 10)).collect(Collectors.toList()) + ) .setAlpha(randomBoolean() ? null : randomDoubleBetween(0.0, Double.MAX_VALUE, true)) .setEtaGrowthRatePerTree(randomBoolean() ? null : randomDoubleBetween(0.5, 2.0, true)) .setSoftTreeDepthLimit(randomBoolean() ? null : randomDoubleBetween(0.0, Double.MAX_VALUE, true)) diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsConfigTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsConfigTests.java index 0b1d7bfc5bfac..90714e11a07d4 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsConfigTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsConfigTests.java @@ -13,11 +13,11 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.SearchModule; import org.elasticsearch.search.fetch.subphase.FetchSourceContext; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.time.Instant; @@ -33,19 +33,22 @@ public class DataFrameAnalyticsConfigTests extends AbstractXContentTestCase { public static DataFrameAnalyticsConfig randomDataFrameAnalyticsConfig() { - DataFrameAnalyticsConfig.Builder builder = - DataFrameAnalyticsConfig.builder() - .setId(randomAlphaOfLengthBetween(1, 10)) - .setSource(randomSourceConfig()) - .setDest(randomDestConfig()) - .setAnalysis(randomOutlierDetection()); + DataFrameAnalyticsConfig.Builder builder = DataFrameAnalyticsConfig.builder() + .setId(randomAlphaOfLengthBetween(1, 10)) + .setSource(randomSourceConfig()) + .setDest(randomDestConfig()) + .setAnalysis(randomOutlierDetection()); if (randomBoolean()) { builder.setDescription(randomAlphaOfLength(20)); } if (randomBoolean()) { - builder.setAnalyzedFields(new FetchSourceContext(true, - generateRandomStringArray(10, 10, false, false), - generateRandomStringArray(10, 10, false, false))); + builder.setAnalyzedFields( + new FetchSourceContext( + true, + generateRandomStringArray(10, 10, false, false), + generateRandomStringArray(10, 10, false, false) + ) + ); } if (randomBoolean()) { builder.setModelMemoryLimit(new ByteSizeValue(randomIntBetween(1, 16), randomFrom(ByteSizeUnit.MB, ByteSizeUnit.GB))); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsConfigUpdateTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsConfigUpdateTests.java index b1005e42b803a..ae431ed282f90 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsConfigUpdateTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsConfigUpdateTests.java @@ -10,10 +10,10 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeValue; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.SearchModule; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.ArrayList; @@ -23,9 +23,7 @@ public class DataFrameAnalyticsConfigUpdateTests extends AbstractXContentTestCase { public static DataFrameAnalyticsConfigUpdate randomDataFrameAnalyticsConfigUpdate() { - DataFrameAnalyticsConfigUpdate.Builder builder = - DataFrameAnalyticsConfigUpdate.builder() - .setId(randomAlphaOfLengthBetween(1, 10)); + DataFrameAnalyticsConfigUpdate.Builder builder = DataFrameAnalyticsConfigUpdate.builder().setId(randomAlphaOfLengthBetween(1, 10)); if (randomBoolean()) { builder.setDescription(randomAlphaOfLength(20)); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsDestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsDestTests.java index 9794270cee3ed..578444ad91daa 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsDestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsDestTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml.dataframe; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsSourceTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsSourceTests.java index 650aef9c13cbe..750b8416565d5 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsSourceTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsSourceTests.java @@ -9,11 +9,11 @@ package org.elasticsearch.client.ml.dataframe; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.SearchModule; import org.elasticsearch.search.fetch.subphase.FetchSourceContext; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.HashMap; @@ -23,15 +23,16 @@ import static java.util.Collections.emptyList; import static org.elasticsearch.client.ml.dataframe.QueryConfigTests.randomQueryConfig; - public class DataFrameAnalyticsSourceTests extends AbstractXContentTestCase { public static DataFrameAnalyticsSource randomSourceConfig() { FetchSourceContext sourceFiltering = null; if (randomBoolean()) { - sourceFiltering = new FetchSourceContext(true, + sourceFiltering = new FetchSourceContext( + true, generateRandomStringArray(10, 10, false, false), - generateRandomStringArray(10, 10, false, false)); + generateRandomStringArray(10, 10, false, false) + ); } Map runtimeMappings = null; if (randomBoolean()) { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsStatsTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsStatsTests.java index 5242e44b45b9e..7a01350f285d1 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsStatsTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsStatsTests.java @@ -16,9 +16,9 @@ import org.elasticsearch.client.ml.dataframe.stats.common.MemoryUsageTests; import org.elasticsearch.client.ml.dataframe.stats.outlierdetection.OutlierDetectionStatsTests; import org.elasticsearch.client.ml.dataframe.stats.regression.RegressionStatsTests; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.test.ESTestCase; import java.io.IOException; import java.util.ArrayList; @@ -36,18 +36,20 @@ protected NamedXContentRegistry xContentRegistry() { } public void testFromXContent() throws IOException { - xContentTester(this::createParser, + xContentTester( + this::createParser, DataFrameAnalyticsStatsTests::randomDataFrameAnalyticsStats, DataFrameAnalyticsStatsTests::toXContent, - DataFrameAnalyticsStats::fromXContent) - .supportsUnknownFields(true) + DataFrameAnalyticsStats::fromXContent + ).supportsUnknownFields(true) .randomFieldsExcludeFilter(field -> field.startsWith("node.attributes") || field.startsWith("analysis_stats")) .test(); } public static DataFrameAnalyticsStats randomDataFrameAnalyticsStats() { - AnalysisStats analysisStats = randomBoolean() ? null : - randomFrom( + AnalysisStats analysisStats = randomBoolean() + ? null + : randomFrom( ClassificationStatsTests.createRandom(), OutlierDetectionStatsTests.createRandom(), RegressionStatsTests.createRandom() @@ -62,7 +64,8 @@ public static DataFrameAnalyticsStats randomDataFrameAnalyticsStats() { randomBoolean() ? null : MemoryUsageTests.createRandom(), analysisStats, randomBoolean() ? null : NodeAttributesTests.createRandom(), - randomBoolean() ? null : randomAlphaOfLengthBetween(1, 20)); + randomBoolean() ? null : randomAlphaOfLengthBetween(1, 20) + ); } private static List createRandomProgress() { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/OutlierDetectionTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/OutlierDetectionTests.java index 03ebd0e7b7b5f..e81df18b85e35 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/OutlierDetectionTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/OutlierDetectionTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml.dataframe; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -56,15 +56,14 @@ public void testGetParams_GivenDefaults() { } public void testGetParams_GivenExplicitValues() { - OutlierDetection outlierDetection = - OutlierDetection.builder() - .setNNeighbors(42) - .setMethod(OutlierDetection.Method.LDOF) - .setFeatureInfluenceThreshold(0.5) - .setComputeFeatureInfluence(true) - .setOutlierFraction(0.42) - .setStandardizationEnabled(false) - .build(); + OutlierDetection outlierDetection = OutlierDetection.builder() + .setNNeighbors(42) + .setMethod(OutlierDetection.Method.LDOF) + .setFeatureInfluenceThreshold(0.5) + .setComputeFeatureInfluence(true) + .setOutlierFraction(0.42) + .setStandardizationEnabled(false) + .build(); assertThat(outlierDetection.getNNeighbors(), equalTo(42)); assertThat(outlierDetection.getMethod(), equalTo(OutlierDetection.Method.LDOF)); assertThat(outlierDetection.getFeatureInfluenceThreshold(), closeTo(0.5, 1E-9)); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/PhaseProgressTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/PhaseProgressTests.java index b188909c534e3..e1124229a4734 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/PhaseProgressTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/PhaseProgressTests.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml.dataframe; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/QueryConfigTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/QueryConfigTests.java index 895a99cbfa692..1b8582d865287 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/QueryConfigTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/QueryConfigTests.java @@ -9,13 +9,13 @@ package org.elasticsearch.client.ml.dataframe; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.index.query.MatchAllQueryBuilder; import org.elasticsearch.index.query.MatchNoneQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.search.SearchModule; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/RegressionTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/RegressionTests.java index bf1c2be41fdeb..a16fc4440286d 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/RegressionTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/RegressionTests.java @@ -11,9 +11,9 @@ import org.elasticsearch.client.ml.inference.preprocessing.FrequencyEncodingTests; import org.elasticsearch.client.ml.inference.preprocessing.OneHotEncodingTests; import org.elasticsearch.client.ml.inference.preprocessing.TargetMeanEncodingTests; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; import java.util.ArrayList; @@ -36,12 +36,17 @@ public static Regression randomRegression() { .setTrainingPercent(randomBoolean() ? null : randomDoubleBetween(1.0, 100.0, true)) .setLossFunction(randomBoolean() ? null : randomFrom(Regression.LossFunction.values())) .setLossFunctionParameter(randomBoolean() ? null : randomDoubleBetween(1.0, Double.MAX_VALUE, true)) - .setFeatureProcessors(randomBoolean() ? null : - Stream.generate(() -> randomFrom(FrequencyEncodingTests.createRandom(), - OneHotEncodingTests.createRandom(), - TargetMeanEncodingTests.createRandom())) - .limit(randomIntBetween(1, 10)) - .collect(Collectors.toList())) + .setFeatureProcessors( + randomBoolean() + ? null + : Stream.generate( + () -> randomFrom( + FrequencyEncodingTests.createRandom(), + OneHotEncodingTests.createRandom(), + TargetMeanEncodingTests.createRandom() + ) + ).limit(randomIntBetween(1, 10)).collect(Collectors.toList()) + ) .setAlpha(randomBoolean() ? null : randomDoubleBetween(0.0, Double.MAX_VALUE, true)) .setEtaGrowthRatePerTree(randomBoolean() ? null : randomDoubleBetween(0.5, 2.0, true)) .setSoftTreeDepthLimit(randomBoolean() ? null : randomDoubleBetween(0.0, Double.MAX_VALUE, true)) diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/AccuracyMetricResultTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/AccuracyMetricResultTests.java index f7e698da598ab..277aa17760b01 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/AccuracyMetricResultTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/AccuracyMetricResultTests.java @@ -9,9 +9,9 @@ import org.elasticsearch.client.ml.dataframe.evaluation.MlEvaluationNamedXContentProvider; import org.elasticsearch.client.ml.dataframe.evaluation.classification.AccuracyMetric.Result; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; import java.util.ArrayList; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/AccuracyMetricTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/AccuracyMetricTests.java index 2d845881fe7a4..cb29300cd4768 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/AccuracyMetricTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/AccuracyMetricTests.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.ml.dataframe.evaluation.classification; import org.elasticsearch.client.ml.dataframe.evaluation.MlEvaluationNamedXContentProvider; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/AucRocMetricTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/AucRocMetricTests.java index 1ddf3363152c0..f6da91375fec6 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/AucRocMetricTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/AucRocMetricTests.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.ml.dataframe.evaluation.classification; import org.elasticsearch.client.ml.dataframe.evaluation.MlEvaluationNamedXContentProvider; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; @@ -22,9 +22,7 @@ protected NamedXContentRegistry xContentRegistry() { } public static AucRocMetric createRandom() { - return new AucRocMetric( - randomAlphaOfLengthBetween(1, 10), - randomBoolean() ? randomBoolean() : null); + return new AucRocMetric(randomAlphaOfLengthBetween(1, 10), randomBoolean() ? randomBoolean() : null); } @Override diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/ClassificationTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/ClassificationTests.java index e54ba37ff1f79..8d548ef5198e2 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/ClassificationTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/ClassificationTests.java @@ -9,9 +9,9 @@ import org.elasticsearch.client.ml.dataframe.evaluation.EvaluationMetric; import org.elasticsearch.client.ml.dataframe.evaluation.MlEvaluationNamedXContentProvider; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; import java.util.Arrays; @@ -26,19 +26,21 @@ protected NamedXContentRegistry xContentRegistry() { } public static Classification createRandom() { - List metrics = - randomSubsetOf( - Arrays.asList( - AucRocMetricTests.createRandom(), - AccuracyMetricTests.createRandom(), - PrecisionMetricTests.createRandom(), - RecallMetricTests.createRandom(), - MulticlassConfusionMatrixMetricTests.createRandom())); + List metrics = randomSubsetOf( + Arrays.asList( + AucRocMetricTests.createRandom(), + AccuracyMetricTests.createRandom(), + PrecisionMetricTests.createRandom(), + RecallMetricTests.createRandom(), + MulticlassConfusionMatrixMetricTests.createRandom() + ) + ); return new Classification( randomAlphaOfLength(10), randomBoolean() ? randomAlphaOfLength(10) : null, randomBoolean() ? randomAlphaOfLength(10) : null, - metrics.isEmpty() ? null : metrics); + metrics.isEmpty() ? null : metrics + ); } @Override diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/MulticlassConfusionMatrixMetricResultTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/MulticlassConfusionMatrixMetricResultTests.java index 83150f30fbe39..3750d89c1dc30 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/MulticlassConfusionMatrixMetricResultTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/MulticlassConfusionMatrixMetricResultTests.java @@ -11,9 +11,9 @@ import org.elasticsearch.client.ml.dataframe.evaluation.classification.MulticlassConfusionMatrixMetric.ActualClass; import org.elasticsearch.client.ml.dataframe.evaluation.classification.MulticlassConfusionMatrixMetric.PredictedClass; import org.elasticsearch.client.ml.dataframe.evaluation.classification.MulticlassConfusionMatrixMetric.Result; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; import java.util.ArrayList; @@ -43,7 +43,9 @@ public static Result randomResult() { classNames.get(i), randomBoolean() ? randomNonNegativeLong() : null, predictedClasses, - randomBoolean() ? randomNonNegativeLong() : null)); + randomBoolean() ? randomNonNegativeLong() : null + ) + ); } return new Result(actualClasses, randomBoolean() ? randomNonNegativeLong() : null); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/MulticlassConfusionMatrixMetricTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/MulticlassConfusionMatrixMetricTests.java index a7e6a9c017da1..a95c8cfd4a455 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/MulticlassConfusionMatrixMetricTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/MulticlassConfusionMatrixMetricTests.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.ml.dataframe.evaluation.classification; import org.elasticsearch.client.ml.dataframe.evaluation.MlEvaluationNamedXContentProvider; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/PerClassSingleValueTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/PerClassSingleValueTests.java index 41c710ba75b96..486282f87f334 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/PerClassSingleValueTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/PerClassSingleValueTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml.dataframe.evaluation.classification; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/PrecisionMetricResultTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/PrecisionMetricResultTests.java index 8fccc61e9e756..35dff04637fdf 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/PrecisionMetricResultTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/PrecisionMetricResultTests.java @@ -9,9 +9,9 @@ import org.elasticsearch.client.ml.dataframe.evaluation.MlEvaluationNamedXContentProvider; import org.elasticsearch.client.ml.dataframe.evaluation.classification.PrecisionMetric.Result; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; import java.util.ArrayList; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/PrecisionMetricTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/PrecisionMetricTests.java index fa17f2f504e13..389b44233905b 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/PrecisionMetricTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/PrecisionMetricTests.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.ml.dataframe.evaluation.classification; import org.elasticsearch.client.ml.dataframe.evaluation.MlEvaluationNamedXContentProvider; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/RecallMetricResultTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/RecallMetricResultTests.java index e0a562537d81a..48ced8779982d 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/RecallMetricResultTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/RecallMetricResultTests.java @@ -9,9 +9,9 @@ import org.elasticsearch.client.ml.dataframe.evaluation.MlEvaluationNamedXContentProvider; import org.elasticsearch.client.ml.dataframe.evaluation.classification.RecallMetric.Result; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; import java.util.ArrayList; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/RecallMetricTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/RecallMetricTests.java index d0173edcc74ab..168c88f5706f9 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/RecallMetricTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/classification/RecallMetricTests.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.ml.dataframe.evaluation.classification; import org.elasticsearch.client.ml.dataframe.evaluation.MlEvaluationNamedXContentProvider; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/common/AucRocPointTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/common/AucRocPointTests.java index 20510509120ba..2f54c539f6cf9 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/common/AucRocPointTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/common/AucRocPointTests.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml.dataframe.evaluation.common; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/common/AucRocResultTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/common/AucRocResultTests.java index 36063b63fa29a..7d719c47df319 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/common/AucRocResultTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/common/AucRocResultTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml.dataframe.evaluation.common; import org.elasticsearch.client.ml.dataframe.evaluation.EvaluationMetric; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.function.Predicate; @@ -21,10 +21,8 @@ public class AucRocResultTests extends AbstractXContentTestCase randomConfusionMatrix()) + Stream.generate(() -> randomConfusionMatrix()) .limit(randomIntBetween(1, 5)) - .collect(Collectors.toMap(v -> String.valueOf(randomDouble()), v -> v))); + .collect(Collectors.toMap(v -> String.valueOf(randomDouble()), v -> v)) + ); } @Override diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/outlierdetection/OutlierDetectionTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/outlierdetection/OutlierDetectionTests.java index dd52412e3133c..e9d896fb4c978 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/outlierdetection/OutlierDetectionTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/outlierdetection/OutlierDetectionTests.java @@ -9,9 +9,9 @@ import org.elasticsearch.client.ml.dataframe.evaluation.EvaluationMetric; import org.elasticsearch.client.ml.dataframe.evaluation.MlEvaluationNamedXContentProvider; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; import java.util.ArrayList; @@ -32,26 +32,17 @@ public static OutlierDetection createRandom() { metrics.add(AucRocMetricTests.createRandom()); } if (randomBoolean()) { - metrics.add(new PrecisionMetric(Arrays.asList(randomArray(1, - 4, - Double[]::new, - OutlierDetectionTests::randomDouble)))); + metrics.add(new PrecisionMetric(Arrays.asList(randomArray(1, 4, Double[]::new, OutlierDetectionTests::randomDouble)))); } if (randomBoolean()) { - metrics.add(new RecallMetric(Arrays.asList(randomArray(1, - 4, - Double[]::new, - OutlierDetectionTests::randomDouble)))); + metrics.add(new RecallMetric(Arrays.asList(randomArray(1, 4, Double[]::new, OutlierDetectionTests::randomDouble)))); } if (randomBoolean()) { - metrics.add(new ConfusionMatrixMetric(Arrays.asList(randomArray(1, - 4, - Double[]::new, - OutlierDetectionTests::randomDouble)))); + metrics.add(new ConfusionMatrixMetric(Arrays.asList(randomArray(1, 4, Double[]::new, OutlierDetectionTests::randomDouble)))); } - return randomBoolean() ? - new OutlierDetection(randomAlphaOfLength(10), randomAlphaOfLength(10)) : - new OutlierDetection(randomAlphaOfLength(10), randomAlphaOfLength(10), metrics.isEmpty() ? null : metrics); + return randomBoolean() + ? new OutlierDetection(randomAlphaOfLength(10), randomAlphaOfLength(10)) + : new OutlierDetection(randomAlphaOfLength(10), randomAlphaOfLength(10), metrics.isEmpty() ? null : metrics); } @Override diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/outlierdetection/PrecisionMetricResultTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/outlierdetection/PrecisionMetricResultTests.java index 9e1974f8cd545..a79038cf163cb 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/outlierdetection/PrecisionMetricResultTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/outlierdetection/PrecisionMetricResultTests.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.function.Predicate; @@ -19,10 +19,10 @@ public class PrecisionMetricResultTests extends AbstractXContentTestCase randomDouble()) + Stream.generate(() -> randomDouble()) .limit(randomIntBetween(1, 5)) - .collect(Collectors.toMap(v -> String.valueOf(randomDouble()), v -> v))); + .collect(Collectors.toMap(v -> String.valueOf(randomDouble()), v -> v)) + ); } @Override diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/outlierdetection/RecallMetricResultTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/outlierdetection/RecallMetricResultTests.java index d28a8bed69309..8bb03bfebd076 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/outlierdetection/RecallMetricResultTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/outlierdetection/RecallMetricResultTests.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml.dataframe.evaluation.outlierdetection; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.function.Predicate; @@ -19,10 +19,10 @@ public class RecallMetricResultTests extends AbstractXContentTestCase randomDouble()) + Stream.generate(() -> randomDouble()) .limit(randomIntBetween(1, 5)) - .collect(Collectors.toMap(v -> String.valueOf(randomDouble()), v -> v))); + .collect(Collectors.toMap(v -> String.valueOf(randomDouble()), v -> v)) + ); } @Override diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/HuberMetricResultTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/HuberMetricResultTests.java index f2c6a0ccf7812..bf03169c969c3 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/HuberMetricResultTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/HuberMetricResultTests.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.ml.dataframe.evaluation.regression; import org.elasticsearch.client.ml.dataframe.evaluation.MlEvaluationNamedXContentProvider; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/HuberMetricTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/HuberMetricTests.java index 3998e7ca6ceba..94bc81c7e271c 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/HuberMetricTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/HuberMetricTests.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.ml.dataframe.evaluation.regression; import org.elasticsearch.client.ml.dataframe.evaluation.MlEvaluationNamedXContentProvider; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/MeanSquaredErrorMetricResultTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/MeanSquaredErrorMetricResultTests.java index d2c50fa37ce12..d332fd430c0e2 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/MeanSquaredErrorMetricResultTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/MeanSquaredErrorMetricResultTests.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.ml.dataframe.evaluation.regression; import org.elasticsearch.client.ml.dataframe.evaluation.MlEvaluationNamedXContentProvider; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/MeanSquaredErrorMetricTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/MeanSquaredErrorMetricTests.java index 17a2d49ad7d5b..b5af31643dc09 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/MeanSquaredErrorMetricTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/MeanSquaredErrorMetricTests.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.ml.dataframe.evaluation.regression; import org.elasticsearch.client.ml.dataframe.evaluation.MlEvaluationNamedXContentProvider; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/MeanSquaredLogarithmicErrorMetricResultTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/MeanSquaredLogarithmicErrorMetricResultTests.java index 2907d21e7d72d..eabe71081a5b7 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/MeanSquaredLogarithmicErrorMetricResultTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/MeanSquaredLogarithmicErrorMetricResultTests.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.ml.dataframe.evaluation.regression; import org.elasticsearch.client.ml.dataframe.evaluation.MlEvaluationNamedXContentProvider; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/MeanSquaredLogarithmicErrorMetricTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/MeanSquaredLogarithmicErrorMetricTests.java index afdb12b3c7e58..98e4e8f01c834 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/MeanSquaredLogarithmicErrorMetricTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/MeanSquaredLogarithmicErrorMetricTests.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.ml.dataframe.evaluation.regression; import org.elasticsearch.client.ml.dataframe.evaluation.MlEvaluationNamedXContentProvider; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/RSquaredMetricResultTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/RSquaredMetricResultTests.java index 3e66d9945ff04..e687d6302ed64 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/RSquaredMetricResultTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/RSquaredMetricResultTests.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.ml.dataframe.evaluation.regression; import org.elasticsearch.client.ml.dataframe.evaluation.MlEvaluationNamedXContentProvider; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/RSquaredMetricTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/RSquaredMetricTests.java index 9a252521ed5fb..d93cf98da4734 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/RSquaredMetricTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/RSquaredMetricTests.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.ml.dataframe.evaluation.regression; import org.elasticsearch.client.ml.dataframe.evaluation.MlEvaluationNamedXContentProvider; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/RegressionTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/RegressionTests.java index 74208f20a8d8b..6667a90a0b5c3 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/RegressionTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/evaluation/regression/RegressionTests.java @@ -9,9 +9,9 @@ import org.elasticsearch.client.ml.dataframe.evaluation.EvaluationMetric; import org.elasticsearch.client.ml.dataframe.evaluation.MlEvaluationNamedXContentProvider; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; import java.util.ArrayList; @@ -39,9 +39,9 @@ public static Regression createRandom() { if (randomBoolean()) { metrics.add(new RSquaredMetric()); } - return randomBoolean() ? - new Regression(randomAlphaOfLength(10), randomAlphaOfLength(10)) : - new Regression(randomAlphaOfLength(10), randomAlphaOfLength(10), metrics.isEmpty() ? null : metrics); + return randomBoolean() + ? new Regression(randomAlphaOfLength(10), randomAlphaOfLength(10)) + : new Regression(randomAlphaOfLength(10), randomAlphaOfLength(10), metrics.isEmpty() ? null : metrics); } @Override diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/explain/FieldSelectionTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/explain/FieldSelectionTests.java index d9fe7f32eaa53..d5eb0b6482a1e 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/explain/FieldSelectionTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/explain/FieldSelectionTests.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml.dataframe.explain; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.Set; @@ -17,16 +17,11 @@ public class FieldSelectionTests extends AbstractXContentTestCase { public static FieldSelection createRandom() { - Set mappingTypes = randomSubsetOf(randomIntBetween(1, 3), "int", "float", "double", "text", "keyword", "ip") - .stream().collect(Collectors.toSet()); + Set mappingTypes = randomSubsetOf(randomIntBetween(1, 3), "int", "float", "double", "text", "keyword", "ip").stream() + .collect(Collectors.toSet()); FieldSelection.FeatureType featureType = randomBoolean() ? null : randomFrom(FieldSelection.FeatureType.values()); String reason = randomBoolean() ? null : randomAlphaOfLength(20); - return new FieldSelection(randomAlphaOfLength(10), - mappingTypes, - randomBoolean(), - randomBoolean(), - featureType, - reason); + return new FieldSelection(randomAlphaOfLength(10), mappingTypes, randomBoolean(), randomBoolean(), featureType, reason); } @Override diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/explain/MemoryEstimationTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/explain/MemoryEstimationTests.java index 1daca9d6c9055..28d74a63e7512 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/explain/MemoryEstimationTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/explain/MemoryEstimationTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml.dataframe.explain; import org.elasticsearch.common.unit.ByteSizeValue; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -18,7 +18,8 @@ public class MemoryEstimationTests extends AbstractXContentTestCase TrainedModelDefinition.fromXContent(parser).build(), - xContentRegistry()); + xContentRegistry() + ); // Did we inflate to the same object? assertThat(inflatedDefinition, equalTo(definition)); @@ -39,10 +41,14 @@ public void testInflateTooLargeStream() throws IOException { String firstDeflate = InferenceToXContentCompressor.deflate(definition); BytesReference inflatedBytes = InferenceToXContentCompressor.inflate(firstDeflate, 10L); assertThat(inflatedBytes.length(), equalTo(10)); - try(XContentParser parser = XContentHelper.createParser(xContentRegistry(), - LoggingDeprecationHandler.INSTANCE, - inflatedBytes, - XContentType.JSON)) { + try ( + XContentParser parser = XContentHelper.createParser( + xContentRegistry(), + LoggingDeprecationHandler.INSTANCE, + inflatedBytes, + XContentType.JSON + ) + ) { expectThrows(IOException.class, () -> TrainedModelConfig.fromXContent(parser)); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/NamedXContentObjectHelperTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/NamedXContentObjectHelperTests.java index e4d36a1cfe406..040988ef3c356 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/NamedXContentObjectHelperTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/NamedXContentObjectHelperTests.java @@ -7,16 +7,16 @@ */ package org.elasticsearch.client.ml.inference; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.search.SearchModule; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.search.SearchModule; -import org.elasticsearch.test.ESTestCase; import java.io.IOException; import java.util.ArrayList; @@ -31,8 +31,7 @@ public class NamedXContentObjectHelperTests extends ESTestCase { static class NamedTestObject implements NamedXContentObject { private String fieldValue; - public static final ObjectParser PARSER = - new ObjectParser<>("my_named_object", true, NamedTestObject::new); + public static final ObjectParser PARSER = new ObjectParser<>("my_named_object", true, NamedTestObject::new); static { PARSER.declareString(NamedTestObject::setFieldValue, new ParseField("my_field")); } @@ -91,9 +90,15 @@ public void testSerialize() throws IOException { @Override protected NamedXContentRegistry xContentRegistry() { List namedXContent = new ArrayList<>(); - namedXContent.addAll(Collections.singletonList(new NamedXContentRegistry.Entry(NamedXContentObject.class, - new ParseField("my_named_object"), - (p, c) -> NamedTestObject.PARSER.apply(p, null)))); + namedXContent.addAll( + Collections.singletonList( + new NamedXContentRegistry.Entry( + NamedXContentObject.class, + new ParseField("my_named_object"), + (p, c) -> NamedTestObject.PARSER.apply(p, null) + ) + ) + ); namedXContent.addAll(new SearchModule(Settings.EMPTY, Collections.emptyList()).getNamedXContents()); return new NamedXContentRegistry(namedXContent); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/TrainedModelConfigTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/TrainedModelConfigTests.java index 93ccbfe0b7bbb..567dd011e8020 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/TrainedModelConfigTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/TrainedModelConfigTests.java @@ -12,10 +12,10 @@ import org.elasticsearch.client.ml.inference.trainedmodel.RegressionConfigTests; import org.elasticsearch.client.ml.inference.trainedmodel.TargetType; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.SearchModule; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.time.Instant; @@ -27,7 +27,6 @@ import java.util.stream.Collectors; import java.util.stream.Stream; - public class TrainedModelConfigTests extends AbstractXContentTestCase { public static TrainedModelConfig createTestTrainedModelConfig() { @@ -41,21 +40,24 @@ public static TrainedModelConfig createTestTrainedModelConfig() { Instant.ofEpochMilli(randomNonNegativeLong()), randomBoolean() ? null : TrainedModelDefinitionTests.createRandomBuilder(targetType).build(), randomBoolean() ? null : randomAlphaOfLength(100), - randomBoolean() ? null : - Stream.generate(() -> randomAlphaOfLength(10)).limit(randomIntBetween(0, 5)).collect(Collectors.toList()), + randomBoolean() + ? null + : Stream.generate(() -> randomAlphaOfLength(10)).limit(randomIntBetween(0, 5)).collect(Collectors.toList()), randomBoolean() ? null : Collections.singletonMap(randomAlphaOfLength(10), randomAlphaOfLength(10)), randomBoolean() ? null : TrainedModelInputTests.createRandomInput(), randomBoolean() ? null : randomNonNegativeLong(), randomBoolean() ? null : randomNonNegativeLong(), randomBoolean() ? null : randomFrom("platinum", "basic"), - randomBoolean() ? null : - Stream.generate(() -> randomAlphaOfLength(10)) + randomBoolean() + ? null + : Stream.generate(() -> randomAlphaOfLength(10)) .limit(randomIntBetween(1, 10)) .collect(Collectors.toMap(Function.identity(), (k) -> randomAlphaOfLength(10))), - targetType.equals(TargetType.CLASSIFICATION) ? - ClassificationConfigTests.randomClassificationConfig() : - RegressionConfigTests.randomRegressionConfig(), - randomBoolean() ? null : IndexLocationTests.randomInstance()); + targetType.equals(TargetType.CLASSIFICATION) + ? ClassificationConfigTests.randomClassificationConfig() + : RegressionConfigTests.randomRegressionConfig(), + randomBoolean() ? null : IndexLocationTests.randomInstance() + ); } @Override diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/TrainedModelDefinitionTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/TrainedModelDefinitionTests.java index 5be1e97566ddb..c8ace6782aeb8 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/TrainedModelDefinitionTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/TrainedModelDefinitionTests.java @@ -16,10 +16,10 @@ import org.elasticsearch.client.ml.inference.trainedmodel.ensemble.EnsembleTests; import org.elasticsearch.client.ml.inference.trainedmodel.tree.TreeTests; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.SearchModule; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.ArrayList; @@ -30,7 +30,6 @@ import java.util.stream.Collectors; import java.util.stream.Stream; - public class TrainedModelDefinitionTests extends AbstractXContentTestCase { @Override @@ -54,19 +53,22 @@ public static TrainedModelDefinition.Builder createRandomBuilder() { public static TrainedModelDefinition.Builder createRandomBuilder(TargetType targetType) { int numberOfProcessors = randomIntBetween(1, 10); - return new TrainedModelDefinition.Builder() - .setPreProcessors( - randomBoolean() ? null : - Stream.generate(() -> randomFrom( + return new TrainedModelDefinition.Builder().setPreProcessors( + randomBoolean() + ? null + : Stream.generate( + () -> randomFrom( FrequencyEncodingTests.createRandom(), OneHotEncodingTests.createRandom(), TargetMeanEncodingTests.createRandom(), NGramTests.createRandom(), - MultiTests.createRandom())) - .limit(numberOfProcessors) - .collect(Collectors.toList())) - .setTrainedModel(randomFrom(TreeTests.buildRandomTree(Arrays.asList("foo", "bar"), 6, targetType), - EnsembleTests.createRandom(targetType))); + MultiTests.createRandom() + ) + ).limit(numberOfProcessors).collect(Collectors.toList()) + ) + .setTrainedModel( + randomFrom(TreeTests.buildRandomTree(Arrays.asList("foo", "bar"), 6, targetType), EnsembleTests.createRandom(targetType)) + ); } @Override diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/TrainedModelInputTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/TrainedModelInputTests.java index 2ea43f6c962d4..ccf6819dc084a 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/TrainedModelInputTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/TrainedModelInputTests.java @@ -7,15 +7,14 @@ */ package org.elasticsearch.client.ml.inference; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.Stream; - public class TrainedModelInputTests extends AbstractXContentTestCase { @Override @@ -34,9 +33,9 @@ protected Predicate getRandomFieldsExcludeFilter() { } public static TrainedModelInput createRandomInput() { - return new TrainedModelInput(Stream.generate(() -> randomAlphaOfLength(10)) - .limit(randomLongBetween(1, 10)) - .collect(Collectors.toList())); + return new TrainedModelInput( + Stream.generate(() -> randomAlphaOfLength(10)).limit(randomLongBetween(1, 10)).collect(Collectors.toList()) + ); } @Override diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/TrainedModelStatsTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/TrainedModelStatsTests.java index 00361b3406875..6db8747df47c8 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/TrainedModelStatsTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/TrainedModelStatsTests.java @@ -9,13 +9,13 @@ import org.elasticsearch.client.ml.inference.trainedmodel.InferenceStatsTests; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.ingest.IngestStats; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.ingest.IngestStats; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; import java.util.List; @@ -25,7 +25,6 @@ import java.util.stream.Collectors; import java.util.stream.Stream; - public class TrainedModelStatsTests extends AbstractXContentTestCase { @Override @@ -49,18 +48,20 @@ protected TrainedModelStats createTestInstance() { randomAlphaOfLength(10), randomBoolean() ? null : randomIngestStats(), randomInt(), - randomBoolean() ? null : InferenceStatsTests.randomInstance()); + randomBoolean() ? null : InferenceStatsTests.randomInstance() + ); } private Map randomIngestStats() { try { - List pipelineIds = Stream.generate(()-> randomAlphaOfLength(10)) + List pipelineIds = Stream.generate(() -> randomAlphaOfLength(10)) .limit(randomIntBetween(0, 10)) .collect(Collectors.toList()); IngestStats stats = new IngestStats( new IngestStats.Stats(randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong()), pipelineIds.stream().map(id -> new IngestStats.PipelineStat(id, randomStats())).collect(Collectors.toList()), - pipelineIds.stream().collect(Collectors.toMap(Function.identity(), (v) -> randomProcessorStats()))); + pipelineIds.stream().collect(Collectors.toMap(Function.identity(), (v) -> randomProcessorStats())) + ); try (XContentBuilder builder = XContentFactory.jsonBuilder()) { builder.startObject(); stats.toXContent(builder, ToXContent.EMPTY_PARAMS); @@ -73,7 +74,7 @@ private Map randomIngestStats() { } } - private IngestStats.Stats randomStats(){ + private IngestStats.Stats randomStats() { return new IngestStats.Stats(randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong()); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/preprocessing/CustomWordEmbeddingTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/preprocessing/CustomWordEmbeddingTests.java index 9eecf3511873e..0fcd89822ad57 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/preprocessing/CustomWordEmbeddingTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/preprocessing/CustomWordEmbeddingTests.java @@ -7,12 +7,11 @@ */ package org.elasticsearch.client.ml.inference.preprocessing; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; - public class CustomWordEmbeddingTests extends AbstractXContentTestCase { @Override diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/preprocessing/FrequencyEncodingTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/preprocessing/FrequencyEncodingTests.java index 98422e7b1a9db..2d1ceb08d6663 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/preprocessing/FrequencyEncodingTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/preprocessing/FrequencyEncodingTests.java @@ -7,15 +7,14 @@ */ package org.elasticsearch.client.ml.inference.preprocessing; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.function.Predicate; - public class FrequencyEncodingTests extends AbstractXContentTestCase { @Override @@ -48,10 +47,7 @@ public static FrequencyEncoding createRandom(String inputField) { for (int i = 0; i < valuesSize; i++) { valueMap.put(randomAlphaOfLength(10), randomDoubleBetween(0.0, 1.0, false)); } - return new FrequencyEncoding(inputField, - randomAlphaOfLength(10), - valueMap, - randomBoolean() ? null : randomBoolean()); + return new FrequencyEncoding(inputField, randomAlphaOfLength(10), valueMap, randomBoolean() ? null : randomBoolean()); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/preprocessing/MultiTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/preprocessing/MultiTests.java index 2f0192050a049..73faad80a3ba0 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/preprocessing/MultiTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/preprocessing/MultiTests.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.ml.inference.preprocessing; import org.elasticsearch.client.ml.inference.MlInferenceNamedXContentProvider; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; import java.util.ArrayList; @@ -20,7 +20,6 @@ import java.util.stream.Collectors; import java.util.stream.Stream; - public class MultiTests extends AbstractXContentTestCase { @Override @@ -55,11 +54,13 @@ public static Multi createRandom() { NGram nGram = new NGram(randomAlphaOfLength(10), Arrays.asList(1, 2), 0, 10, isCustom, "f"); List preProcessorList = new ArrayList<>(); preProcessorList.add(nGram); - Stream.generate(() -> randomFrom( - FrequencyEncodingTests.createRandom(randomFrom(nGram.outputFields())), - TargetMeanEncodingTests.createRandom(randomFrom(nGram.outputFields())), - OneHotEncodingTests.createRandom(randomFrom(nGram.outputFields())) - )).limit(randomIntBetween(1, 10)).forEach(preProcessorList::add); + Stream.generate( + () -> randomFrom( + FrequencyEncodingTests.createRandom(randomFrom(nGram.outputFields())), + TargetMeanEncodingTests.createRandom(randomFrom(nGram.outputFields())), + OneHotEncodingTests.createRandom(randomFrom(nGram.outputFields())) + ) + ).limit(randomIntBetween(1, 10)).forEach(preProcessorList::add); processors = preProcessorList; } else { processors = Stream.generate( diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/preprocessing/NGramTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/preprocessing/NGramTests.java index f430de1ea282a..79237754ff237 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/preprocessing/NGramTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/preprocessing/NGramTests.java @@ -7,14 +7,13 @@ */ package org.elasticsearch.client.ml.inference.preprocessing; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.stream.Collectors; import java.util.stream.IntStream; - public class NGramTests extends AbstractXContentTestCase { @Override @@ -34,12 +33,14 @@ protected NGram createTestInstance() { public static NGram createRandom() { int length = randomIntBetween(1, 10); - return new NGram(randomAlphaOfLength(10), + return new NGram( + randomAlphaOfLength(10), IntStream.range(1, Math.min(5, length + 1)).limit(5).boxed().collect(Collectors.toList()), randomBoolean() ? null : randomIntBetween(0, 10), randomBoolean() ? null : length, randomBoolean() ? null : randomBoolean(), - randomBoolean() ? null : randomAlphaOfLength(10)); + randomBoolean() ? null : randomAlphaOfLength(10) + ); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/preprocessing/OneHotEncodingTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/preprocessing/OneHotEncodingTests.java index 739163381f22d..2270bbff31ccb 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/preprocessing/OneHotEncodingTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/preprocessing/OneHotEncodingTests.java @@ -7,15 +7,14 @@ */ package org.elasticsearch.client.ml.inference.preprocessing; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.function.Predicate; - public class OneHotEncodingTests extends AbstractXContentTestCase { @Override @@ -48,9 +47,7 @@ public static OneHotEncoding createRandom(String inputField) { for (int i = 0; i < valuesSize; i++) { valueMap.put(randomAlphaOfLength(10), randomAlphaOfLength(10)); } - return new OneHotEncoding(inputField, - valueMap, - randomBoolean() ? null : randomBoolean()); + return new OneHotEncoding(inputField, valueMap, randomBoolean() ? null : randomBoolean()); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/preprocessing/TargetMeanEncodingTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/preprocessing/TargetMeanEncodingTests.java index cc4c1a00d00a9..38054700e9396 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/preprocessing/TargetMeanEncodingTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/preprocessing/TargetMeanEncodingTests.java @@ -7,15 +7,14 @@ */ package org.elasticsearch.client.ml.inference.preprocessing; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.function.Predicate; - public class TargetMeanEncodingTests extends AbstractXContentTestCase { @Override @@ -48,11 +47,7 @@ public static TargetMeanEncoding createRandom(String inputField) { for (int i = 0; i < valuesSize; i++) { valueMap.put(randomAlphaOfLength(10), randomDoubleBetween(0.0, 1.0, false)); } - return new TargetMeanEncoding(inputField, - randomAlphaOfLength(10), - valueMap, - randomDoubleBetween(0.0, 1.0, false), - true); + return new TargetMeanEncoding(inputField, randomAlphaOfLength(10), valueMap, randomDoubleBetween(0.0, 1.0, false), true); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/results/FeatureImportanceTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/results/FeatureImportanceTests.java index 4fa2c8a92c876..1d64107ba01b4 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/results/FeatureImportanceTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/results/FeatureImportanceTests.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.ml.inference.results; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.function.Supplier; @@ -30,11 +30,13 @@ protected FeatureImportance createTestInstance() { return new FeatureImportance( randomAlphaOfLength(10), randomBoolean() ? null : randomDoubleBetween(-10.0, 10.0, false), - randomBoolean() ? null : - Stream.generate(classNameGenerator) + randomBoolean() + ? null + : Stream.generate(classNameGenerator) .limit(randomLongBetween(2, 10)) .map(name -> new FeatureImportance.ClassImportance(name, randomDoubleBetween(-10, 10, false))) - .collect(Collectors.toList())); + .collect(Collectors.toList()) + ); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/results/TopClassEntryTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/results/TopClassEntryTests.java index 7153b7bf39fb0..87fcd26aeacea 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/results/TopClassEntryTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/results/TopClassEntryTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml.inference.results; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/ClassificationConfigTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/ClassificationConfigTests.java index 05470985c61f9..87f30884c6290 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/ClassificationConfigTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/ClassificationConfigTests.java @@ -8,19 +8,20 @@ package org.elasticsearch.client.ml.inference.trainedmodel; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; public class ClassificationConfigTests extends AbstractXContentTestCase { public static ClassificationConfig randomClassificationConfig() { - return new ClassificationConfig(randomBoolean() ? null : randomIntBetween(-1, 10), + return new ClassificationConfig( + randomBoolean() ? null : randomIntBetween(-1, 10), randomBoolean() ? null : randomAlphaOfLength(10), randomBoolean() ? null : randomAlphaOfLength(10), randomBoolean() ? null : randomIntBetween(0, 10) - ); + ); } @Override diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/InferenceStatsTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/InferenceStatsTests.java index c1666fc850dd0..6cd8b5dc76de5 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/InferenceStatsTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/InferenceStatsTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml.inference.trainedmodel; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.time.Instant; @@ -17,12 +17,13 @@ public class InferenceStatsTests extends AbstractXContentTestCase { public static InferenceStats randomInstance() { - return new InferenceStats(randomNonNegativeLong(), + return new InferenceStats( + randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), Instant.now() - ); + ); } @Override diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/RegressionConfigTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/RegressionConfigTests.java index d130a01f3a9a9..c417feb8f0cf1 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/RegressionConfigTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/RegressionConfigTests.java @@ -8,20 +8,17 @@ package org.elasticsearch.client.ml.inference.trainedmodel; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; public class RegressionConfigTests extends AbstractXContentTestCase { public static RegressionConfig randomRegressionConfig() { - return new RegressionConfig( - randomBoolean() ? null : randomAlphaOfLength(10), - randomBoolean() ? null : randomIntBetween(0, 10)); + return new RegressionConfig(randomBoolean() ? null : randomAlphaOfLength(10), randomBoolean() ? null : randomIntBetween(0, 10)); } - @Override protected RegressionConfig createTestInstance() { return randomRegressionConfig(); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/ensemble/EnsembleTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/ensemble/EnsembleTests.java index 8505a1baf4397..50ff0ee587242 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/ensemble/EnsembleTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/ensemble/EnsembleTests.java @@ -12,11 +12,11 @@ import org.elasticsearch.client.ml.inference.trainedmodel.TrainedModel; import org.elasticsearch.client.ml.inference.trainedmodel.tree.TreeTests; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.SearchModule; import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.ArrayList; @@ -26,7 +26,6 @@ import java.util.stream.Collectors; import java.util.stream.Stream; - public class EnsembleTests extends AbstractXContentTestCase { @Override @@ -50,9 +49,7 @@ public static Ensemble createRandom() { public static Ensemble createRandom(TargetType targetType) { int numberOfFeatures = randomIntBetween(1, 10); - List featureNames = Stream.generate(() -> randomAlphaOfLength(10)) - .limit(numberOfFeatures) - .collect(Collectors.toList()); + List featureNames = Stream.generate(() -> randomAlphaOfLength(10)).limit(numberOfFeatures).collect(Collectors.toList()); int numberOfModels = randomIntBetween(1, 10); List models = Stream.generate(() -> TreeTests.buildRandomTree(featureNames, 6, targetType)) .limit(numberOfModels) @@ -62,26 +59,27 @@ public static Ensemble createRandom(TargetType targetType) { categoryLabels = randomList(2, randomIntBetween(3, 10), () -> randomAlphaOfLength(10)); } List weights = Stream.generate(ESTestCase::randomDouble).limit(numberOfModels).collect(Collectors.toList()); - OutputAggregator outputAggregator = targetType == TargetType.REGRESSION ? - randomFrom(new WeightedSum(weights), new Exponent(weights)) : - randomFrom( - new WeightedMode( - categoryLabels != null ? categoryLabels.size() : randomIntBetween(2, 10), - weights), - new LogisticRegression(weights)); - double[] thresholds = randomBoolean() && targetType == TargetType.CLASSIFICATION ? - Stream.generate(ESTestCase::randomDouble) + OutputAggregator outputAggregator = targetType == TargetType.REGRESSION + ? randomFrom(new WeightedSum(weights), new Exponent(weights)) + : randomFrom( + new WeightedMode(categoryLabels != null ? categoryLabels.size() : randomIntBetween(2, 10), weights), + new LogisticRegression(weights) + ); + double[] thresholds = randomBoolean() && targetType == TargetType.CLASSIFICATION + ? Stream.generate(ESTestCase::randomDouble) .limit(categoryLabels == null ? randomIntBetween(1, 10) : categoryLabels.size()) .mapToDouble(Double::valueOf) - .toArray() : - null; + .toArray() + : null; - return new Ensemble(randomBoolean() ? featureNames : Collections.emptyList(), + return new Ensemble( + randomBoolean() ? featureNames : Collections.emptyList(), models, outputAggregator, targetType, categoryLabels, - thresholds); + thresholds + ); } @Override diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/ensemble/ExponentTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/ensemble/ExponentTests.java index 470bdf9aaa7c4..259e3f8d8db70 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/ensemble/ExponentTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/ensemble/ExponentTests.java @@ -7,15 +7,14 @@ */ package org.elasticsearch.client.ml.inference.trainedmodel.ensemble; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.stream.Collectors; import java.util.stream.Stream; - public class ExponentTests extends AbstractXContentTestCase { Exponent createTestInstance(int numberOfWeights) { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/ensemble/LogisticRegressionTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/ensemble/LogisticRegressionTests.java index 779683c6f1ea2..0ceef4fd45b2b 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/ensemble/LogisticRegressionTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/ensemble/LogisticRegressionTests.java @@ -7,15 +7,14 @@ */ package org.elasticsearch.client.ml.inference.trainedmodel.ensemble; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.stream.Collectors; import java.util.stream.Stream; - public class LogisticRegressionTests extends AbstractXContentTestCase { LogisticRegression createTestInstance(int numberOfWeights) { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/ensemble/WeightedModeTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/ensemble/WeightedModeTests.java index 12059e98f5a31..ce87dc21d8d2b 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/ensemble/WeightedModeTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/ensemble/WeightedModeTests.java @@ -7,21 +7,21 @@ */ package org.elasticsearch.client.ml.inference.trainedmodel.ensemble; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.stream.Collectors; import java.util.stream.Stream; - public class WeightedModeTests extends AbstractXContentTestCase { WeightedMode createTestInstance(int numberOfWeights) { return new WeightedMode( randomIntBetween(2, 10), - Stream.generate(ESTestCase::randomDouble).limit(numberOfWeights).collect(Collectors.toList())); + Stream.generate(ESTestCase::randomDouble).limit(numberOfWeights).collect(Collectors.toList()) + ); } @Override diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/ensemble/WeightedSumTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/ensemble/WeightedSumTests.java index 88b482260f79c..83751244e112a 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/ensemble/WeightedSumTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/ensemble/WeightedSumTests.java @@ -7,15 +7,14 @@ */ package org.elasticsearch.client.ml.inference.trainedmodel.ensemble; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.stream.Collectors; import java.util.stream.Stream; - public class WeightedSumTests extends AbstractXContentTestCase { WeightedSum createTestInstance(int numberOfWeights) { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/langident/LangIdentNeuralNetworkTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/langident/LangIdentNeuralNetworkTests.java index a6c14ad65b4e0..f1d228230b279 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/langident/LangIdentNeuralNetworkTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/langident/LangIdentNeuralNetworkTests.java @@ -7,14 +7,13 @@ */ package org.elasticsearch.client.ml.inference.trainedmodel.langident; +import org.elasticsearch.client.ml.inference.MlInferenceNamedXContentProvider; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; -import org.elasticsearch.client.ml.inference.MlInferenceNamedXContentProvider; import java.io.IOException; - public class LangIdentNeuralNetworkTests extends AbstractXContentTestCase { @Override @@ -33,9 +32,7 @@ protected LangIdentNeuralNetwork createTestInstance() { } public static LangIdentNeuralNetwork createRandom() { - return new LangIdentNeuralNetwork(randomAlphaOfLength(10), - LangNetLayerTests.createRandom(), - LangNetLayerTests.createRandom()); + return new LangIdentNeuralNetwork(randomAlphaOfLength(10), LangNetLayerTests.createRandom(), LangNetLayerTests.createRandom()); } @Override diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/langident/LangNetLayerTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/langident/LangNetLayerTests.java index 126c336840f5d..acb1ca87322e6 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/langident/LangNetLayerTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/langident/LangNetLayerTests.java @@ -7,14 +7,13 @@ */ package org.elasticsearch.client.ml.inference.trainedmodel.langident; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.stream.Stream; - public class LangNetLayerTests extends AbstractXContentTestCase { @Override @@ -38,7 +37,8 @@ public static LangNetLayer createRandom() { Stream.generate(ESTestCase::randomDouble).limit(numWeights).mapToDouble(Double::doubleValue).toArray(), numWeights, 1, - Stream.generate(ESTestCase::randomDouble).limit(numWeights).mapToDouble(Double::doubleValue).toArray()); + Stream.generate(ESTestCase::randomDouble).limit(numWeights).mapToDouble(Double::doubleValue).toArray() + ); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/tree/TreeNodeTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/tree/TreeNodeTests.java index f2ccb705e0f93..ee9a4215b6eac 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/tree/TreeNodeTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/tree/TreeNodeTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml.inference.trainedmodel.tree; import org.elasticsearch.client.ml.job.config.Operator; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.Collections; @@ -43,12 +43,14 @@ public static TreeNode createRandomLeafNode(double internalValue) { .build(); } - public static TreeNode.Builder createRandom(int nodeIndex, - Integer left, - Integer right, - Double threshold, - Integer featureIndex, - Operator operator) { + public static TreeNode.Builder createRandom( + int nodeIndex, + Integer left, + Integer right, + Double threshold, + Integer featureIndex, + Operator operator + ) { return TreeNode.builder(nodeIndex) .setLeafValue(left == null ? Collections.singletonList(randomDouble()) : null) .setDefaultLeft(randomBoolean() ? null : randomBoolean()) diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/tree/TreeTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/tree/TreeTests.java index 37c1fa671420a..f9b3b16085246 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/tree/TreeTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/trainedmodel/tree/TreeTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml.inference.trainedmodel.tree; import org.elasticsearch.client.ml.inference.trainedmodel.TargetType; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.ArrayList; @@ -17,7 +17,6 @@ import java.util.List; import java.util.function.Predicate; - public class TreeTests extends AbstractXContentTestCase { @Override @@ -46,26 +45,25 @@ public static Tree createRandom() { for (int i = 0; i < numberOfFeatures; i++) { featureNames.add(randomAlphaOfLength(10)); } - return buildRandomTree(featureNames, 6, randomFrom(TargetType.values())); + return buildRandomTree(featureNames, 6, randomFrom(TargetType.values())); } public static Tree buildRandomTree(List featureNames, int depth, TargetType targetType) { - int maxFeatureIndex = featureNames.size() -1; + int maxFeatureIndex = featureNames.size() - 1; Tree.Builder builder = Tree.builder(); builder.setFeatureNames(featureNames); TreeNode.Builder node = builder.addJunction(0, randomInt(maxFeatureIndex), true, randomDouble()); List childNodes = List.of(node.getLeftChild(), node.getRightChild()); - for (int i = 0; i < depth -1; i++) { + for (int i = 0; i < depth - 1; i++) { List nextNodes = new ArrayList<>(); for (int nodeId : childNodes) { - if (i == depth -2) { + if (i == depth - 2) { builder.addLeaf(nodeId, randomDouble()); } else { - TreeNode.Builder childNode = - builder.addJunction(nodeId, randomInt(maxFeatureIndex), true, randomDouble()); + TreeNode.Builder childNode = builder.addJunction(nodeId, randomInt(maxFeatureIndex), true, randomDouble()); nextNodes.add(childNode.getLeftChild()); nextNodes.add(childNode.getRightChild()); } @@ -76,9 +74,7 @@ public static Tree buildRandomTree(List featureNames, int depth, TargetT if (randomBoolean() && targetType.equals(TargetType.CLASSIFICATION)) { categoryLabels = Arrays.asList(generateRandomStringArray(randomIntBetween(1, 10), randomIntBetween(1, 10), false, false)); } - return builder.setClassificationLabels(categoryLabels) - .setTargetType(targetType) - .build(); + return builder.setClassificationLabels(categoryLabels).setTargetType(targetType).build(); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/AnalysisConfigTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/AnalysisConfigTests.java index 288a3402a7181..87438693f726d 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/AnalysisConfigTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/AnalysisConfigTests.java @@ -9,10 +9,10 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.SearchModule; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentParser; import java.util.ArrayList; import java.util.Arrays; @@ -76,8 +76,7 @@ public static AnalysisConfig.Builder createRandomized() { } if (randomBoolean()) { boolean enabled = randomBoolean(); - builder.setPerPartitionCategorizationConfig( - new PerPartitionCategorizationConfig(enabled, enabled && randomBoolean())); + builder.setPerPartitionCategorizationConfig(new PerPartitionCategorizationConfig(enabled, enabled && randomBoolean())); } } if (randomBoolean()) { @@ -111,7 +110,7 @@ protected boolean supportsUnknownFields() { public void testBuilder_WithNullDetectors() { AnalysisConfig.Builder builder = new AnalysisConfig.Builder(new ArrayList<>()); - NullPointerException ex = expectThrows(NullPointerException.class, () -> builder.setDetectors(null)); + NullPointerException ex = expectThrows(NullPointerException.class, () -> builder.setDetectors(null)); assertEquals("[detectors] must not be null", ex.getMessage()); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/AnalysisLimitsTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/AnalysisLimitsTests.java index b38130bb62e74..27c5ddd47bdb7 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/AnalysisLimitsTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/AnalysisLimitsTests.java @@ -7,12 +7,12 @@ */ package org.elasticsearch.client.ml.job.config; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; @@ -26,8 +26,10 @@ protected AnalysisLimits createTestInstance() { } public static AnalysisLimits createRandomized() { - return new AnalysisLimits(randomBoolean() ? (long) randomIntBetween(1, 1000000) : null, - randomBoolean() ? randomNonNegativeLong() : null); + return new AnalysisLimits( + randomBoolean() ? (long) randomIntBetween(1, 1000000) : null, + randomBoolean() ? randomNonNegativeLong() : null + ); } @Override @@ -38,7 +40,7 @@ protected AnalysisLimits doParseInstance(XContentParser parser) { public void testParseModelMemoryLimitGivenPositiveNumber() throws IOException { String json = "{\"model_memory_limit\": 2048}"; XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json); + .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json); AnalysisLimits limits = AnalysisLimits.PARSER.apply(parser, null); @@ -48,7 +50,7 @@ public void testParseModelMemoryLimitGivenPositiveNumber() throws IOException { public void testParseModelMemoryLimitGivenStringMultipleOfMBs() throws IOException { String json = "{\"model_memory_limit\":\"4g\"}"; XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json); + .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json); AnalysisLimits limits = AnalysisLimits.PARSER.apply(parser, null); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/CategorizationAnalyzerConfigTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/CategorizationAnalyzerConfigTests.java index 9bb90eb8cbd8a..c47e47f31dc44 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/CategorizationAnalyzerConfigTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/CategorizationAnalyzerConfigTests.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml.job.config; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.HashMap; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/DataDescriptionTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/DataDescriptionTests.java index 259dddf263ce5..f5519fa6dc102 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/DataDescriptionTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/DataDescriptionTests.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml.job.config; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import static org.elasticsearch.client.ml.job.config.DataDescription.DataFormat; import static org.hamcrest.Matchers.equalTo; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/DetectionRuleTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/DetectionRuleTests.java index 3315633c052a1..761315a75b81f 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/DetectionRuleTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/DetectionRuleTests.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml.job.config; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.util.ArrayList; import java.util.Collections; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/DetectorTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/DetectorTests.java index 5579f225ba7ca..d15a2a2b9d139 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/DetectorTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/DetectorTests.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml.job.config; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.util.ArrayList; import java.util.Collections; @@ -64,9 +64,9 @@ private Detector.Builder createDetector() { detector.setOverFieldName("over_field"); detector.setPartitionFieldName("partition"); detector.setUseNull(true); - DetectionRule rule = new DetectionRule.Builder(RuleScope.builder().exclude("partition", "partition_filter")) - .setActions(RuleAction.SKIP_RESULT) - .build(); + DetectionRule rule = new DetectionRule.Builder(RuleScope.builder().exclude("partition", "partition_filter")).setActions( + RuleAction.SKIP_RESULT + ).build(); detector.setRules(Collections.singletonList(rule)); return detector; } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/FilterRefTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/FilterRefTests.java index f3c0936f78884..188480eda9a4b 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/FilterRefTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/FilterRefTests.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml.job.config; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/JobTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/JobTests.java index 56a748f1c20da..0e7f79ace5a24 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/JobTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/JobTests.java @@ -8,15 +8,16 @@ package org.elasticsearch.client.ml.job.config; import com.carrotsearch.randomizedtesting.generators.CodepointSetGenerator; + import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.search.SearchModule; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.search.SearchModule; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; import java.util.ArrayList; @@ -27,20 +28,20 @@ public class JobTests extends AbstractXContentTestCase { - private static final String FUTURE_JOB = "{\n" + - " \"job_id\": \"farequote\",\n" + - " \"create_time\": 1234567890000,\n" + - " \"tomorrows_technology_today\": \"wow\",\n" + - " \"analysis_config\": {\n" + - " \"bucket_span\": \"1h\",\n" + - " \"something_new\": \"gasp\",\n" + - " \"detectors\": [{\"function\": \"metric\", \"field_name\": \"responsetime\", \"by_field_name\": \"airline\"}]\n" + - " },\n" + - " \"data_description\": {\n" + - " \"time_field\": \"time\",\n" + - " \"the_future\": 123\n" + - " }\n" + - "}"; + private static final String FUTURE_JOB = "{\n" + + " \"job_id\": \"farequote\",\n" + + " \"create_time\": 1234567890000,\n" + + " \"tomorrows_technology_today\": \"wow\",\n" + + " \"analysis_config\": {\n" + + " \"bucket_span\": \"1h\",\n" + + " \"something_new\": \"gasp\",\n" + + " \"detectors\": [{\"function\": \"metric\", \"field_name\": \"responsetime\", \"by_field_name\": \"airline\"}]\n" + + " },\n" + + " \"data_description\": {\n" + + " \"time_field\": \"time\",\n" + + " \"the_future\": 123\n" + + " }\n" + + "}"; @Override protected Job createTestInstance() { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/JobUpdateTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/JobUpdateTests.java index 91fe0d47d3213..6ee67cc736726 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/JobUpdateTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/JobUpdateTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml.job.config; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.util.ArrayList; import java.util.Arrays; @@ -81,7 +81,6 @@ public static JobUpdate createRandom(String jobId) { return update.build(); } - private static List createRandomDetectorUpdates() { int size = randomInt(10); List detectorUpdates = new ArrayList<>(size); @@ -93,8 +92,10 @@ private static List createRandomDetectorUpdates() { List detectionRules = null; if (randomBoolean()) { detectionRules = new ArrayList<>(); - detectionRules.add(new DetectionRule.Builder( - Collections.singletonList(new RuleCondition(RuleCondition.AppliesTo.ACTUAL, Operator.GT, 5))).build()); + detectionRules.add( + new DetectionRule.Builder(Collections.singletonList(new RuleCondition(RuleCondition.AppliesTo.ACTUAL, Operator.GT, 5))) + .build() + ); } detectorUpdates.add(new JobUpdate.DetectorUpdate(i, detectorDescription, detectionRules)); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/MlFilterTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/MlFilterTests.java index 83b5295ccc171..0982f5b2f9e28 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/MlFilterTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/MlFilterTests.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml.job.config; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.util.SortedSet; import java.util.TreeSet; @@ -54,8 +54,10 @@ public void testNullId() { } public void testNullItems() { - expectThrows(NullPointerException.class, - () -> MlFilter.builder(randomAlphaOfLength(10)).setItems((SortedSet) null).build()); + expectThrows( + NullPointerException.class, + () -> MlFilter.builder(randomAlphaOfLength(10)).setItems((SortedSet) null).build() + ); } public void testItemsAreSorted() { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/ModelPlotConfigTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/ModelPlotConfigTests.java index f0117f74b3de6..11c2003941391 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/ModelPlotConfigTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/ModelPlotConfigTests.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml.job.config; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; public class ModelPlotConfigTests extends AbstractXContentTestCase { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/PerPartitionCategorizationConfigTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/PerPartitionCategorizationConfigTests.java index fc32469ad706c..f0cbcc6618ae7 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/PerPartitionCategorizationConfigTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/PerPartitionCategorizationConfigTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml.job.config; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; public class PerPartitionCategorizationConfigTests extends AbstractXContentTestCase { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/RuleConditionTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/RuleConditionTests.java index 90a9f1a72313c..3a3979ac978cf 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/RuleConditionTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/RuleConditionTests.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml.job.config; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; public class RuleConditionTests extends AbstractXContentTestCase { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/RuleScopeTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/RuleScopeTests.java index 518f19f1271e5..1204a64d55b9d 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/RuleScopeTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/RuleScopeTests.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml.job.config; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -35,11 +35,7 @@ public void testGetReferencedFilters_GivenEmpty() { } public void testGetReferencedFilters_GivenMultipleFields() { - RuleScope scope = RuleScope.builder() - .include("foo", "filter1") - .exclude("bar", "filter2") - .include("foobar", "filter3") - .build(); + RuleScope scope = RuleScope.builder().include("foo", "filter1").exclude("bar", "filter2").include("foobar", "filter3").build(); assertThat(scope.getReferencedFilters(), contains("filter1", "filter2", "filter3")); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/process/DataCountsTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/process/DataCountsTests.java index 334444c98f2b4..7856fd8136644 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/process/DataCountsTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/process/DataCountsTests.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml.job.process; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.time.Instant; import java.time.ZonedDateTime; @@ -21,12 +21,25 @@ private static Date randomDate() { } public static DataCounts createTestInstance(String jobId) { - return new DataCounts(jobId, randomIntBetween(1, 1_000_000), - randomIntBetween(1, 1_000_000), randomIntBetween(1, 1_000_000), randomIntBetween(1, 1_000_000), - randomIntBetween(1, 1_000_000), randomIntBetween(1, 1_000_000), randomIntBetween(1, 1_000_000), - randomIntBetween(1, 1_000_000), randomIntBetween(1, 1_000_000), randomIntBetween(1, 1_000_000), - randomDate(), randomDate(), randomDate(), randomDate(), randomDate(), - randomBoolean() ? null : Instant.now()); + return new DataCounts( + jobId, + randomIntBetween(1, 1_000_000), + randomIntBetween(1, 1_000_000), + randomIntBetween(1, 1_000_000), + randomIntBetween(1, 1_000_000), + randomIntBetween(1, 1_000_000), + randomIntBetween(1, 1_000_000), + randomIntBetween(1, 1_000_000), + randomIntBetween(1, 1_000_000), + randomIntBetween(1, 1_000_000), + randomIntBetween(1, 1_000_000), + randomDate(), + randomDate(), + randomDate(), + randomDate(), + randomDate(), + randomBoolean() ? null : Instant.now() + ); } @Override diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/process/ModelSizeStatsTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/process/ModelSizeStatsTests.java index 83263bf5852e4..94773af4dcf9f 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/process/ModelSizeStatsTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/process/ModelSizeStatsTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml.job.process; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.util.Date; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/process/ModelSnapshotTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/process/ModelSnapshotTests.java index f595881fe4b9c..ce2ea4a82cd2d 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/process/ModelSnapshotTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/process/ModelSnapshotTests.java @@ -9,8 +9,8 @@ import org.elasticsearch.Version; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.util.Date; @@ -51,8 +51,7 @@ public void testEquals_GivenEqualModelSnapshots() { public void testEquals_GivenDifferentTimestamp() { ModelSnapshot modelSnapshot1 = createFullyPopulated().build(); - ModelSnapshot modelSnapshot2 = createFullyPopulated().setTimestamp( - new Date(modelSnapshot1.getTimestamp().getTime() + 1)).build(); + ModelSnapshot modelSnapshot2 = createFullyPopulated().setTimestamp(new Date(modelSnapshot1.getTimestamp().getTime() + 1)).build(); assertFalse(modelSnapshot1.equals(modelSnapshot2)); assertFalse(modelSnapshot2.equals(modelSnapshot1)); @@ -60,8 +59,7 @@ public void testEquals_GivenDifferentTimestamp() { public void testEquals_GivenDifferentDescription() { ModelSnapshot modelSnapshot1 = createFullyPopulated().build(); - ModelSnapshot modelSnapshot2 = createFullyPopulated() - .setDescription(modelSnapshot1.getDescription() + " blah").build(); + ModelSnapshot modelSnapshot2 = createFullyPopulated().setDescription(modelSnapshot1.getDescription() + " blah").build(); assertFalse(modelSnapshot1.equals(modelSnapshot2)); assertFalse(modelSnapshot2.equals(modelSnapshot1)); @@ -69,8 +67,7 @@ public void testEquals_GivenDifferentDescription() { public void testEquals_GivenDifferentId() { ModelSnapshot modelSnapshot1 = createFullyPopulated().build(); - ModelSnapshot modelSnapshot2 = createFullyPopulated() - .setSnapshotId(modelSnapshot1.getSnapshotId() + "_2").build(); + ModelSnapshot modelSnapshot2 = createFullyPopulated().setSnapshotId(modelSnapshot1.getSnapshotId() + "_2").build(); assertFalse(modelSnapshot1.equals(modelSnapshot2)); assertFalse(modelSnapshot2.equals(modelSnapshot1)); @@ -78,8 +75,7 @@ public void testEquals_GivenDifferentId() { public void testEquals_GivenDifferentDocCount() { ModelSnapshot modelSnapshot1 = createFullyPopulated().build(); - ModelSnapshot modelSnapshot2 = createFullyPopulated() - .setSnapshotDocCount(modelSnapshot1.getSnapshotDocCount() + 1).build(); + ModelSnapshot modelSnapshot2 = createFullyPopulated().setSnapshotDocCount(modelSnapshot1.getSnapshotDocCount() + 1).build(); assertFalse(modelSnapshot1.equals(modelSnapshot2)); assertFalse(modelSnapshot2.equals(modelSnapshot1)); @@ -97,9 +93,9 @@ public void testEquals_GivenDifferentModelSizeStats() { public void testEquals_GivenDifferentQuantiles() { ModelSnapshot modelSnapshot1 = createFullyPopulated().build(); - ModelSnapshot modelSnapshot2 = createFullyPopulated() - .setQuantiles(new Quantiles("foo", modelSnapshot1.getQuantiles().getTimestamp(), - "different state")).build(); + ModelSnapshot modelSnapshot2 = createFullyPopulated().setQuantiles( + new Quantiles("foo", modelSnapshot1.getQuantiles().getTimestamp(), "different state") + ).build(); assertFalse(modelSnapshot1.equals(modelSnapshot2)); assertFalse(modelSnapshot2.equals(modelSnapshot1)); @@ -108,7 +104,8 @@ public void testEquals_GivenDifferentQuantiles() { public void testEquals_GivenDifferentLatestResultTimestamp() { ModelSnapshot modelSnapshot1 = createFullyPopulated().build(); ModelSnapshot modelSnapshot2 = createFullyPopulated().setLatestResultTimeStamp( - new Date(modelSnapshot1.getLatestResultTimeStamp().getTime() + 1)).build(); + new Date(modelSnapshot1.getLatestResultTimeStamp().getTime() + 1) + ).build(); assertFalse(modelSnapshot1.equals(modelSnapshot2)); assertFalse(modelSnapshot2.equals(modelSnapshot1)); @@ -117,7 +114,8 @@ public void testEquals_GivenDifferentLatestResultTimestamp() { public void testEquals_GivenDifferentLatestRecordTimestamp() { ModelSnapshot modelSnapshot1 = createFullyPopulated().build(); ModelSnapshot modelSnapshot2 = createFullyPopulated().setLatestRecordTimeStamp( - new Date(modelSnapshot1.getLatestRecordTimeStamp().getTime() + 1)).build(); + new Date(modelSnapshot1.getLatestRecordTimeStamp().getTime() + 1) + ).build(); assertFalse(modelSnapshot1.equals(modelSnapshot2)); assertFalse(modelSnapshot2.equals(modelSnapshot1)); @@ -158,17 +156,15 @@ public static ModelSnapshot.Builder createRandomizedBuilder() { modelSnapshot.setSnapshotId(randomAlphaOfLengthBetween(1, 20)); modelSnapshot.setSnapshotDocCount(randomInt()); modelSnapshot.setModelSizeStats(ModelSizeStatsTests.createRandomized()); - modelSnapshot.setLatestResultTimeStamp( - new Date(TimeValue.parseTimeValue(randomTimeValue(), "test").millis())); - modelSnapshot.setLatestRecordTimeStamp( - new Date(TimeValue.parseTimeValue(randomTimeValue(), "test").millis())); + modelSnapshot.setLatestResultTimeStamp(new Date(TimeValue.parseTimeValue(randomTimeValue(), "test").millis())); + modelSnapshot.setLatestRecordTimeStamp(new Date(TimeValue.parseTimeValue(randomTimeValue(), "test").millis())); modelSnapshot.setQuantiles(QuantilesTests.createRandomized()); modelSnapshot.setRetain(randomBoolean()); return modelSnapshot; } @Override - protected ModelSnapshot doParseInstance(XContentParser parser){ + protected ModelSnapshot doParseInstance(XContentParser parser) { return ModelSnapshot.PARSER.apply(parser, null).build(); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/process/QuantilesTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/process/QuantilesTests.java index c3fed4fb124c4..1134d969b42db 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/process/QuantilesTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/process/QuantilesTests.java @@ -8,12 +8,11 @@ package org.elasticsearch.client.ml.job.process; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.util.Date; - public class QuantilesTests extends AbstractXContentTestCase { public void testEquals_GivenSameObject() { @@ -21,13 +20,11 @@ public void testEquals_GivenSameObject() { assertTrue(quantiles.equals(quantiles)); } - public void testEquals_GivenDifferentClassObject() { Quantiles quantiles = new Quantiles("foo", new Date(0L), "foo"); assertFalse(quantiles.equals("not a quantiles object")); } - public void testEquals_GivenEqualQuantilesObject() { Quantiles quantiles1 = new Quantiles("foo", new Date(0L), "foo"); @@ -37,7 +34,6 @@ public void testEquals_GivenEqualQuantilesObject() { assertTrue(quantiles2.equals(quantiles1)); } - public void testEquals_GivenDifferentState() { Quantiles quantiles1 = new Quantiles("foo", new Date(0L), "bar1"); @@ -47,7 +43,6 @@ public void testEquals_GivenDifferentState() { assertFalse(quantiles2.equals(quantiles1)); } - public void testHashCode_GivenEqualObject() { Quantiles quantiles1 = new Quantiles("foo", new Date(0L), "foo"); @@ -56,16 +51,17 @@ public void testHashCode_GivenEqualObject() { assertEquals(quantiles1.hashCode(), quantiles2.hashCode()); } - @Override protected Quantiles createTestInstance() { return createRandomized(); } public static Quantiles createRandomized() { - return new Quantiles(randomAlphaOfLengthBetween(1, 20), - new Date(TimeValue.parseTimeValue(randomTimeValue(), "test").millis()), - randomAlphaOfLengthBetween(0, 1000)); + return new Quantiles( + randomAlphaOfLengthBetween(1, 20), + new Date(TimeValue.parseTimeValue(randomTimeValue(), "test").millis()), + randomAlphaOfLengthBetween(0, 1000) + ); } @Override diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/process/TimingStatsTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/process/TimingStatsTests.java index 1015d14548b32..2ffc0c3205518 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/process/TimingStatsTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/process/TimingStatsTests.java @@ -7,11 +7,11 @@ */ package org.elasticsearch.client.ml.job.process; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; @@ -31,7 +31,8 @@ public static TimingStats createTestInstance(String jobId) { randomBoolean() ? null : randomDouble(), randomBoolean() ? null : randomDouble(), randomBoolean() ? null : randomDouble(), - randomBoolean() ? null : randomDouble()); + randomBoolean() ? null : randomDouble() + ); } @Override @@ -77,9 +78,10 @@ public void testConstructor_NullValues() { public void testParse_OptionalFieldsAbsent() throws IOException { String json = "{\"job_id\": \"my-job-id\"}"; - try (XContentParser parser = - XContentFactory.xContent(XContentType.JSON).createParser( - xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json)) { + try ( + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json) + ) { TimingStats stats = TimingStats.PARSER.apply(parser, null); assertThat(stats.getJobId(), equalTo(JOB_ID)); assertThat(stats.getBucketCount(), equalTo(0L)); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/results/AnomalyCauseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/results/AnomalyCauseTests.java index 6f8e4f75f7c9b..df748e17ec7b6 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/results/AnomalyCauseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/results/AnomalyCauseTests.java @@ -9,8 +9,8 @@ import org.elasticsearch.client.ml.job.config.DetectorFunction; import org.elasticsearch.common.geo.GeoPoint; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.util.ArrayList; import java.util.Arrays; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/results/AnomalyRecordTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/results/AnomalyRecordTests.java index 39896f24f6c69..ac0e9912dbd07 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/results/AnomalyRecordTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/results/AnomalyRecordTests.java @@ -9,8 +9,8 @@ import org.elasticsearch.client.ml.job.config.DetectorFunction; import org.elasticsearch.common.geo.GeoPoint; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.util.ArrayList; import java.util.Arrays; @@ -62,16 +62,16 @@ public static AnomalyRecord createTestInstance(String jobId) { } if (randomBoolean()) { int count = randomIntBetween(0, 9); - List influences = new ArrayList<>(); - for (int i=0; i influences = new ArrayList<>(); + for (int i = 0; i < count; i++) { influences.add(new Influence(randomAlphaOfLength(8), Collections.singletonList(randomAlphaOfLengthBetween(1, 28)))); } anomalyRecord.setInfluencers(influences); } if (randomBoolean()) { int count = randomIntBetween(0, 9); - List causes = new ArrayList<>(); - for (int i=0; i causes = new ArrayList<>(); + for (int i = 0; i < count; i++) { causes.add(new AnomalyCauseTests().createTestInstance()); } anomalyRecord.setCauses(causes); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/results/BucketInfluencerTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/results/BucketInfluencerTests.java index 1182b61327380..6a5bb243084c2 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/results/BucketInfluencerTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/results/BucketInfluencerTests.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml.job.results; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.util.Date; @@ -16,8 +16,11 @@ public class BucketInfluencerTests extends AbstractXContentTestCase { public static Influencer createTestInstance(String jobId) { - Influencer influencer = new Influencer(jobId, randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20), - new Date(randomNonNegativeLong()), randomNonNegativeLong()); + Influencer influencer = new Influencer( + jobId, + randomAlphaOfLengthBetween(1, 20), + randomAlphaOfLengthBetween(1, 20), + new Date(randomNonNegativeLong()), + randomNonNegativeLong() + ); influencer.setInterim(randomBoolean()); influencer.setInfluencerScore(randomDouble()); influencer.setInitialInfluencerScore(randomDouble()); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/results/OverallBucketTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/results/OverallBucketTests.java index 0d403ef99d654..c3e711ba63975 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/results/OverallBucketTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/results/OverallBucketTests.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml.job.results; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.util.ArrayList; import java.util.Date; @@ -30,10 +30,12 @@ public static OverallBucket createRandom() { for (int i = 0; i < jobCount; ++i) { jobs.add(new OverallBucket.JobInfo(randomAlphaOfLength(10), randomDoubleBetween(0.0, 100.0, true))); } - OverallBucket overallBucket = new OverallBucket(new Date(randomNonNegativeLong()), - randomIntBetween(60, 24 * 3600), - randomDoubleBetween(0.0, 100.0, true), - randomBoolean()); + OverallBucket overallBucket = new OverallBucket( + new Date(randomNonNegativeLong()), + randomIntBetween(60, 24 * 3600), + randomDoubleBetween(0.0, 100.0, true), + randomBoolean() + ); overallBucket.setJobs(jobs); return overallBucket; } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/stats/ForecastStatsTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/stats/ForecastStatsTests.java index 87e701e0e6434..362bb15ef905a 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/stats/ForecastStatsTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/stats/ForecastStatsTests.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.ml.job.stats; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.HashMap; @@ -22,7 +22,7 @@ public ForecastStats createTestInstance() { if (randomBoolean()) { return createRandom(1, 22); } - return new ForecastStats(0, null,null,null,null); + return new ForecastStats(0, null, null, null, null); } @Override @@ -46,7 +46,8 @@ public static ForecastStats createRandom(long minTotal, long maxTotal) { SimpleStatsTests.createRandom(), SimpleStatsTests.createRandom(), SimpleStatsTests.createRandom(), - createCountStats()); + createCountStats() + ); } private static Map createCountStats() { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/stats/JobStatsTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/stats/JobStatsTests.java index 900b96bef35d4..aeed791986ef5 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/stats/JobStatsTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/stats/JobStatsTests.java @@ -9,6 +9,8 @@ import org.elasticsearch.client.ml.NodeAttributes; import org.elasticsearch.client.ml.NodeAttributesTests; +import org.elasticsearch.client.ml.job.config.JobState; +import org.elasticsearch.client.ml.job.config.JobTests; import org.elasticsearch.client.ml.job.process.DataCounts; import org.elasticsearch.client.ml.job.process.DataCountsTests; import org.elasticsearch.client.ml.job.process.ModelSizeStats; @@ -16,15 +18,12 @@ import org.elasticsearch.client.ml.job.process.TimingStats; import org.elasticsearch.client.ml.job.process.TimingStatsTests; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.client.ml.job.config.JobState; -import org.elasticsearch.client.ml.job.config.JobTests; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.function.Predicate; - public class JobStatsTests extends AbstractXContentTestCase { public static JobStats createRandomInstance() { @@ -40,7 +39,16 @@ public static JobStats createRandomInstance() { TimeValue openTime = randomBoolean() ? TimeValue.timeValueMillis(randomIntBetween(1, 10000)) : null; return new JobStats( - jobId, dataCounts, state, modelSizeStats, timingStats, forecastStats, nodeAttributes, assigmentExplanation, openTime); + jobId, + dataCounts, + state, + modelSizeStats, + timingStats, + forecastStats, + nodeAttributes, + assigmentExplanation, + openTime + ); } @Override diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/stats/SimpleStatsTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/stats/SimpleStatsTests.java index a6d135b42b870..a0bb98b3eb093 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/stats/SimpleStatsTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/stats/SimpleStatsTests.java @@ -7,12 +7,11 @@ */ package org.elasticsearch.client.ml.job.stats; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; - public class SimpleStatsTests extends AbstractXContentTestCase { @Override diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/util/PageParamsTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/util/PageParamsTests.java index 9eeac25d85298..ba3449cb7b84a 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/util/PageParamsTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/util/PageParamsTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.ml.util; import org.elasticsearch.client.core.PageParams; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; public class PageParamsTests extends AbstractXContentTestCase { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/DeleteRollupJobRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/DeleteRollupJobRequestTests.java index 87344417bbd42..4312105c0c216 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/DeleteRollupJobRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/DeleteRollupJobRequestTests.java @@ -12,7 +12,7 @@ public class DeleteRollupJobRequestTests extends ESTestCase { public void testRequireConfiguration() { - final NullPointerException e = expectThrows(NullPointerException.class, ()-> new DeleteRollupJobRequest(null)); + final NullPointerException e = expectThrows(NullPointerException.class, () -> new DeleteRollupJobRequest(null)); assertEquals("id parameter must not be null", e.getMessage()); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/GetRollupIndexCapsRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/GetRollupIndexCapsRequestTests.java index b87f3a850d3a6..139c3d643cd4f 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/GetRollupIndexCapsRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/GetRollupIndexCapsRequestTests.java @@ -17,11 +17,11 @@ public void testNullOrEmptyIndices() { IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> new GetRollupIndexCapsRequest((String[]) null)); assertThat(e.getMessage(), equalTo("[indices] must not be null or empty")); - String[] indices = new String[]{}; + String[] indices = new String[] {}; e = expectThrows(IllegalArgumentException.class, () -> new GetRollupIndexCapsRequest(indices)); assertThat(e.getMessage(), equalTo("[indices] must not be null or empty")); - e = expectThrows(IllegalArgumentException.class, () -> new GetRollupIndexCapsRequest(new String[]{"foo", null})); + e = expectThrows(IllegalArgumentException.class, () -> new GetRollupIndexCapsRequest(new String[] { "foo", null })); assertThat(e.getMessage(), equalTo("[index] must not be null or empty")); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/GetRollupIndexCapsResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/GetRollupIndexCapsResponseTests.java index 00b55ce576eab..90e9e63227f79 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/GetRollupIndexCapsResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/GetRollupIndexCapsResponseTests.java @@ -32,8 +32,7 @@ protected void toXContent(GetRollupIndexCapsResponse response, XContentBuilder b @Override protected Predicate randomFieldsExcludeFilter() { - return (field) -> - { + return (field) -> { // base cannot have extra things in it return "".equals(field) // the field list expects to be a nested object of a certain type diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/GetRollupJobResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/GetRollupJobResponseTests.java index 6ec701d7a632b..365dd44e06855 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/GetRollupJobResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/GetRollupJobResponseTests.java @@ -14,9 +14,9 @@ import org.elasticsearch.client.rollup.GetRollupJobResponse.RollupIndexerJobStats; import org.elasticsearch.client.rollup.GetRollupJobResponse.RollupJobStatus; import org.elasticsearch.client.rollup.job.config.RollupJobConfigTests; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.test.ESTestCase; import java.io.IOException; import java.util.ArrayList; @@ -28,34 +28,36 @@ public class GetRollupJobResponseTests extends ESTestCase { public void testFromXContent() throws IOException { - xContentTester( - this::createParser, - this::createTestInstance, - this::toXContent, - GetRollupJobResponse::fromXContent) - .supportsUnknownFields(false) - .randomFieldsExcludeFilter(field -> - field.endsWith("status.current_position")) - .test(); + xContentTester(this::createParser, this::createTestInstance, this::toXContent, GetRollupJobResponse::fromXContent) + .supportsUnknownFields(false) + .randomFieldsExcludeFilter(field -> field.endsWith("status.current_position")) + .test(); } private GetRollupJobResponse createTestInstance() { int jobCount = between(1, 5); List jobs = new ArrayList<>(); for (int j = 0; j < jobCount; j++) { - jobs.add(new JobWrapper( - RollupJobConfigTests.randomRollupJobConfig(randomAlphaOfLength(5)), - randomStats(), - randomStatus())); + jobs.add(new JobWrapper(RollupJobConfigTests.randomRollupJobConfig(randomAlphaOfLength(5)), randomStats(), randomStatus())); } return new GetRollupJobResponse(jobs); } private RollupIndexerJobStats randomStats() { - return new RollupIndexerJobStats(randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), - randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), - randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), - randomNonNegativeLong()); + return new RollupIndexerJobStats( + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong() + ); } private RollupJobStatus randomStatus() { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/PutRollupJobRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/PutRollupJobRequestTests.java index 1c3e1fbdbed35..377dc2ebc5d7e 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/PutRollupJobRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/PutRollupJobRequestTests.java @@ -9,13 +9,12 @@ import org.elasticsearch.client.rollup.job.config.RollupJobConfig; import org.elasticsearch.client.rollup.job.config.RollupJobConfigTests; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.junit.Before; import java.io.IOException; - public class PutRollupJobRequestTests extends AbstractXContentTestCase { private String jobId; @@ -42,7 +41,7 @@ protected boolean supportsUnknownFields() { } public void testRequireConfiguration() { - final NullPointerException e = expectThrows(NullPointerException.class, ()-> new PutRollupJobRequest(null)); + final NullPointerException e = expectThrows(NullPointerException.class, () -> new PutRollupJobRequest(null)); assertEquals("rollup job configuration is required", e.getMessage()); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/RollupCapsResponseTestCase.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/RollupCapsResponseTestCase.java index 99376e45abcbb..c540aa0150268 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/RollupCapsResponseTestCase.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/RollupCapsResponseTestCase.java @@ -15,12 +15,12 @@ import org.elasticsearch.client.rollup.job.config.RollupJobConfigTests; import org.elasticsearch.client.rollup.job.config.TermsGroupConfig; import org.elasticsearch.common.Strings; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder; import org.elasticsearch.search.aggregations.bucket.histogram.HistogramAggregationBuilder; import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.junit.Before; import java.io.IOException; @@ -55,12 +55,7 @@ protected String[] shuffleFieldsExceptions() { } public void testFromXContent() throws IOException { - xContentTester( - this::createParser, - this::createTestInstance, - this::toXContent, - this::fromXContent) - .supportsUnknownFields(true) + xContentTester(this::createParser, this::createTestInstance, this::toXContent, this::fromXContent).supportsUnknownFields(true) .randomFieldsExcludeFilter(randomFieldsExcludeFilter()) .shuffleFieldsExceptions(shuffleFieldsExceptions()) .test(); @@ -68,16 +63,17 @@ public void testFromXContent() throws IOException { @Before private void setupIndices() throws IOException { - int numIndices = randomIntBetween(1,5); + int numIndices = randomIntBetween(1, 5); indices = new HashMap<>(numIndices); for (int i = 0; i < numIndices; i++) { String indexName = "index_" + randomAlphaOfLength(10); - int numJobs = randomIntBetween(1,5); + int numJobs = randomIntBetween(1, 5); List jobs = new ArrayList<>(numJobs); for (int j = 0; j < numJobs; j++) { RollupJobConfig config = RollupJobConfigTests.randomRollupJobConfig(randomAlphaOfLength(10)); - jobs.add(new RollupJobCaps(config.getId(), config.getIndexPattern(), - config.getRollupIndex(), createRollupFieldCaps(config))); + jobs.add( + new RollupJobCaps(config.getId(), config.getIndexPattern(), config.getRollupIndex(), createRollupFieldCaps(config)) + ); } RollableIndexCaps cap = new RollableIndexCaps(indexName, jobs); indices.put(indexName, cap); @@ -135,21 +131,22 @@ private static Map createRollupFieldCaps( final List metricsConfig = rollupJobConfig.getMetricsConfig(); if (metricsConfig.size() > 0) { rollupJobConfig.getMetricsConfig().forEach(metricConfig -> { - final List> metrics = metricConfig.getMetrics().stream() + final List> metrics = metricConfig.getMetrics() + .stream() .map(metric -> singletonMap("agg", (Object) metric)) .collect(Collectors.toList()); metrics.forEach(m -> { - List> caps = tempFieldCaps - .getOrDefault(metricConfig.getField(), new ArrayList<>()); + List> caps = tempFieldCaps.getOrDefault(metricConfig.getField(), new ArrayList<>()); caps.add(m); tempFieldCaps.put(metricConfig.getField(), caps); }); }); } - return Collections.unmodifiableMap(tempFieldCaps.entrySet() - .stream() - .collect(Collectors.toMap(Map.Entry::getKey, - e -> new RollupJobCaps.RollupFieldCaps(e.getValue())))); + return Collections.unmodifiableMap( + tempFieldCaps.entrySet() + .stream() + .collect(Collectors.toMap(Map.Entry::getKey, e -> new RollupJobCaps.RollupFieldCaps(e.getValue()))) + ); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/StartRollupJobRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/StartRollupJobRequestTests.java index 524891356f1a2..c95eb78d63773 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/StartRollupJobRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/StartRollupJobRequestTests.java @@ -18,13 +18,15 @@ public void testConstructor() { } public void testEqualsAndHash() { - EqualsHashCodeTestUtils.checkEqualsAndHashCode(new StartRollupJobRequest(randomAlphaOfLength(5)), - orig -> new StartRollupJobRequest(orig.getJobId()), - orig -> new StartRollupJobRequest(orig.getJobId() + "_suffix")); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + new StartRollupJobRequest(randomAlphaOfLength(5)), + orig -> new StartRollupJobRequest(orig.getJobId()), + orig -> new StartRollupJobRequest(orig.getJobId() + "_suffix") + ); } public void testRequireJobId() { - final NullPointerException e = expectThrows(NullPointerException.class, ()-> new StartRollupJobRequest(null)); + final NullPointerException e = expectThrows(NullPointerException.class, () -> new StartRollupJobRequest(null)); assertEquals("id parameter must not be null", e.getMessage()); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/StartRollupJobResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/StartRollupJobResponseTests.java index c69ade757ce1b..f092309039d07 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/StartRollupJobResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/StartRollupJobResponseTests.java @@ -17,13 +17,14 @@ public class StartRollupJobResponseTests extends ESTestCase { public void testFromXContent() throws IOException { - xContentTester(this::createParser, + xContentTester( + this::createParser, this::createTestInstance, AcknowledgedResponseTests::toXContent, - StartRollupJobResponse::fromXContent) - .supportsUnknownFields(false) - .test(); + StartRollupJobResponse::fromXContent + ).supportsUnknownFields(false).test(); } + private StartRollupJobResponse createTestInstance() { return new StartRollupJobResponse(randomBoolean()); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/StopRollupJobRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/StopRollupJobRequestTests.java index 625bf8af85c16..85dfc405818d6 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/StopRollupJobRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/StopRollupJobRequestTests.java @@ -18,13 +18,15 @@ public void testConstructor() { } public void testEqualsAndHash() { - EqualsHashCodeTestUtils.checkEqualsAndHashCode(new StopRollupJobRequest(randomAlphaOfLength(5)), - orig -> new StopRollupJobRequest(orig.getJobId()), - orig -> new StopRollupJobRequest(orig.getJobId() + "_suffix")); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + new StopRollupJobRequest(randomAlphaOfLength(5)), + orig -> new StopRollupJobRequest(orig.getJobId()), + orig -> new StopRollupJobRequest(orig.getJobId() + "_suffix") + ); } public void testRequireJobId() { - final NullPointerException e = expectThrows(NullPointerException.class, ()-> new StopRollupJobRequest(null)); + final NullPointerException e = expectThrows(NullPointerException.class, () -> new StopRollupJobRequest(null)); assertEquals("id parameter must not be null", e.getMessage()); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/StopRollupJobResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/StopRollupJobResponseTests.java index 05e094c9852e2..1e7ae9239ee8d 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/StopRollupJobResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/StopRollupJobResponseTests.java @@ -17,13 +17,14 @@ public class StopRollupJobResponseTests extends ESTestCase { public void testFromXContent() throws IOException { - xContentTester(this::createParser, + xContentTester( + this::createParser, this::createTestInstance, AcknowledgedResponseTests::toXContent, - StopRollupJobResponse::fromXContent) - .supportsUnknownFields(false) - .test(); + StopRollupJobResponse::fromXContent + ).supportsUnknownFields(false).test(); } + private StopRollupJobResponse createTestInstance() { return new StopRollupJobResponse(randomBoolean()); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/job/config/DateHistogramGroupConfigTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/job/config/DateHistogramGroupConfigTests.java index 2b3b988ba6fd5..e8620f704da0d 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/job/config/DateHistogramGroupConfigTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/job/config/DateHistogramGroupConfigTests.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.rollup.job.config; import org.elasticsearch.client.ValidationException; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.Optional; @@ -81,14 +81,14 @@ static DateHistogramGroupConfig randomDateHistogramGroupConfig() { final String field = randomAlphaOfLength(randomIntBetween(3, 10)); final DateHistogramInterval delay = randomBoolean() ? new DateHistogramInterval(randomPositiveTimeValue()) : null; final String timezone = randomBoolean() ? randomZone().toString() : null; - int i = randomIntBetween(0,2); + int i = randomIntBetween(0, 2); final DateHistogramInterval interval; switch (i) { case 0: interval = new DateHistogramInterval(randomPositiveTimeValue()); return new DateHistogramGroupConfig.FixedInterval(field, interval, delay, timezone); case 1: - interval = new DateHistogramInterval(randomTimeValue(1,1, "m", "h", "d", "w")); + interval = new DateHistogramInterval(randomTimeValue(1, 1, "m", "h", "d", "w")); return new DateHistogramGroupConfig.CalendarInterval(field, interval, delay, timezone); default: interval = new DateHistogramInterval(randomPositiveTimeValue()); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/job/config/GroupConfigTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/job/config/GroupConfigTests.java index 3a4b67be3a425..fa366bc5bd6be 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/job/config/GroupConfigTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/job/config/GroupConfigTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.rollup.job.config; import org.elasticsearch.client.ValidationException; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.Optional; @@ -57,8 +57,7 @@ public void testValidateDateHistogramGroupConfigWithErrors() { assertThat(validation.isPresent(), is(true)); ValidationException validationException = validation.get(); assertThat(validationException.validationErrors().size(), is(2)); - assertThat(validationException.validationErrors(), - containsInAnyOrder("Field name is required", "Interval is required")); + assertThat(validationException.validationErrors(), containsInAnyOrder("Field name is required", "Interval is required")); } public void testValidateHistogramGroupConfigWithErrors() { @@ -71,8 +70,10 @@ public void testValidateHistogramGroupConfigWithErrors() { assertThat(validation.isPresent(), is(true)); ValidationException validationException = validation.get(); assertThat(validationException.validationErrors().size(), is(2)); - assertThat(validationException.validationErrors(), - containsInAnyOrder("Fields must have at least one value", "Interval must be a positive long")); + assertThat( + validationException.validationErrors(), + containsInAnyOrder("Fields must have at least one value", "Interval must be a positive long") + ); } public void testValidateTermsGroupConfigWithErrors() { @@ -99,7 +100,7 @@ public void testValidate() { static GroupConfig randomGroupConfig() { DateHistogramGroupConfig dateHistogram = DateHistogramGroupConfigTests.randomDateHistogramGroupConfig(); HistogramGroupConfig histogram = randomBoolean() ? HistogramGroupConfigTests.randomHistogramGroupConfig() : null; - TermsGroupConfig terms = randomBoolean() ? TermsGroupConfigTests.randomTermsGroupConfig() : null; + TermsGroupConfig terms = randomBoolean() ? TermsGroupConfigTests.randomTermsGroupConfig() : null; return new GroupConfig(dateHistogram, histogram, terms); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/job/config/HistogramGroupConfigTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/job/config/HistogramGroupConfigTests.java index ed6391b5cf288..06cfc691fa7ed 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/job/config/HistogramGroupConfigTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/job/config/HistogramGroupConfigTests.java @@ -9,8 +9,8 @@ import org.elasticsearch.client.ValidationException; import org.elasticsearch.common.Strings; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.Optional; @@ -87,6 +87,7 @@ public void testValidate() { assertThat(validation, notNullValue()); assertThat(validation.isPresent(), is(false)); } + static HistogramGroupConfig randomHistogramGroupConfig() { final long interval = randomNonNegativeLong(); final String[] fields = new String[randomIntBetween(1, 10)]; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/job/config/MetricConfigTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/job/config/MetricConfigTests.java index 19d6527ea5aa7..45f7b2dee5921 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/job/config/MetricConfigTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/job/config/MetricConfigTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.rollup.job.config; import org.elasticsearch.client.ValidationException; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.ArrayList; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/job/config/RollupJobConfigTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/job/config/RollupJobConfigTests.java index 0c15dca752a0f..2c70b4f665a90 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/job/config/RollupJobConfigTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/job/config/RollupJobConfigTests.java @@ -9,9 +9,9 @@ import org.elasticsearch.client.ValidationException; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.junit.Before; import java.io.IOException; @@ -55,8 +55,16 @@ protected boolean supportsUnknownFields() { public void testValidateNullId() { final RollupJobConfig sample = randomRollupJobConfig(id); - final RollupJobConfig config = new RollupJobConfig(null, sample.getIndexPattern(), sample.getRollupIndex(), sample.getCron(), - sample.getPageSize(), sample.getGroupConfig(), sample.getMetricsConfig(), sample.getTimeout()); + final RollupJobConfig config = new RollupJobConfig( + null, + sample.getIndexPattern(), + sample.getRollupIndex(), + sample.getCron(), + sample.getPageSize(), + sample.getGroupConfig(), + sample.getMetricsConfig(), + sample.getTimeout() + ); Optional validation = config.validate(); assertThat(validation, notNullValue()); @@ -69,8 +77,16 @@ public void testValidateNullId() { public void testValidateEmptyId() { final RollupJobConfig sample = randomRollupJobConfig(id); - final RollupJobConfig config = new RollupJobConfig("", sample.getIndexPattern(), sample.getRollupIndex(), sample.getCron(), - sample.getPageSize(), sample.getGroupConfig(), sample.getMetricsConfig(), sample.getTimeout()); + final RollupJobConfig config = new RollupJobConfig( + "", + sample.getIndexPattern(), + sample.getRollupIndex(), + sample.getCron(), + sample.getPageSize(), + sample.getGroupConfig(), + sample.getMetricsConfig(), + sample.getTimeout() + ); Optional validation = config.validate(); assertThat(validation, notNullValue()); @@ -83,8 +99,16 @@ public void testValidateEmptyId() { public void testValidateNullIndexPattern() { final RollupJobConfig sample = randomRollupJobConfig(id); - final RollupJobConfig config = new RollupJobConfig(sample.getId(), null, sample.getRollupIndex(), sample.getCron(), - sample.getPageSize(), sample.getGroupConfig(), sample.getMetricsConfig(), sample.getTimeout()); + final RollupJobConfig config = new RollupJobConfig( + sample.getId(), + null, + sample.getRollupIndex(), + sample.getCron(), + sample.getPageSize(), + sample.getGroupConfig(), + sample.getMetricsConfig(), + sample.getTimeout() + ); Optional validation = config.validate(); assertThat(validation, notNullValue()); @@ -97,8 +121,16 @@ public void testValidateNullIndexPattern() { public void testValidateEmptyIndexPattern() { final RollupJobConfig sample = randomRollupJobConfig(id); - final RollupJobConfig config = new RollupJobConfig(sample.getId(), "", sample.getRollupIndex(), sample.getCron(), - sample.getPageSize(), sample.getGroupConfig(), sample.getMetricsConfig(), sample.getTimeout()); + final RollupJobConfig config = new RollupJobConfig( + sample.getId(), + "", + sample.getRollupIndex(), + sample.getCron(), + sample.getPageSize(), + sample.getGroupConfig(), + sample.getMetricsConfig(), + sample.getTimeout() + ); Optional validation = config.validate(); assertThat(validation, notNullValue()); @@ -111,23 +143,41 @@ public void testValidateEmptyIndexPattern() { public void testValidateMatchAllIndexPattern() { final RollupJobConfig sample = randomRollupJobConfig(id); - final RollupJobConfig config = new RollupJobConfig(sample.getId(), "*", sample.getRollupIndex(), sample.getCron(), - sample.getPageSize(), sample.getGroupConfig(), sample.getMetricsConfig(), sample.getTimeout()); + final RollupJobConfig config = new RollupJobConfig( + sample.getId(), + "*", + sample.getRollupIndex(), + sample.getCron(), + sample.getPageSize(), + sample.getGroupConfig(), + sample.getMetricsConfig(), + sample.getTimeout() + ); Optional validation = config.validate(); assertThat(validation, notNullValue()); assertThat(validation.isPresent(), is(true)); ValidationException validationException = validation.get(); assertThat(validationException.validationErrors().size(), is(1)); - assertThat(validationException.validationErrors(), - contains("Index pattern must not match all indices (as it would match it's own rollup index")); + assertThat( + validationException.validationErrors(), + contains("Index pattern must not match all indices (as it would match it's own rollup index") + ); } public void testValidateIndexPatternMatchesRollupIndex() { final RollupJobConfig sample = randomRollupJobConfig(id); - final RollupJobConfig config = new RollupJobConfig(sample.getId(), "rollup*", "rollup", sample.getCron(), - sample.getPageSize(), sample.getGroupConfig(), sample.getMetricsConfig(), sample.getTimeout()); + final RollupJobConfig config = new RollupJobConfig( + sample.getId(), + "rollup*", + "rollup", + sample.getCron(), + sample.getPageSize(), + sample.getGroupConfig(), + sample.getMetricsConfig(), + sample.getTimeout() + ); Optional validation = config.validate(); assertThat(validation, notNullValue()); @@ -140,8 +190,16 @@ public void testValidateIndexPatternMatchesRollupIndex() { public void testValidateSameIndexAndRollupPatterns() { final RollupJobConfig sample = randomRollupJobConfig(id); - final RollupJobConfig config = new RollupJobConfig(sample.getId(), "test", "test", sample.getCron(), - sample.getPageSize(), sample.getGroupConfig(), sample.getMetricsConfig(), sample.getTimeout()); + final RollupJobConfig config = new RollupJobConfig( + sample.getId(), + "test", + "test", + sample.getCron(), + sample.getPageSize(), + sample.getGroupConfig(), + sample.getMetricsConfig(), + sample.getTimeout() + ); Optional validation = config.validate(); assertThat(validation, notNullValue()); @@ -154,8 +212,16 @@ public void testValidateSameIndexAndRollupPatterns() { public void testValidateNullRollupPattern() { final RollupJobConfig sample = randomRollupJobConfig(id); - final RollupJobConfig config = new RollupJobConfig(sample.getId(), sample.getIndexPattern(), null, sample.getCron(), - sample.getPageSize(), sample.getGroupConfig(), sample.getMetricsConfig(), sample.getTimeout()); + final RollupJobConfig config = new RollupJobConfig( + sample.getId(), + sample.getIndexPattern(), + null, + sample.getCron(), + sample.getPageSize(), + sample.getGroupConfig(), + sample.getMetricsConfig(), + sample.getTimeout() + ); Optional validation = config.validate(); assertThat(validation, notNullValue()); @@ -168,8 +234,16 @@ public void testValidateNullRollupPattern() { public void testValidateEmptyRollupPattern() { final RollupJobConfig sample = randomRollupJobConfig(id); - final RollupJobConfig config = new RollupJobConfig(sample.getId(), sample.getIndexPattern(), "", sample.getCron(), - sample.getPageSize(), sample.getGroupConfig(), sample.getMetricsConfig(), sample.getTimeout()); + final RollupJobConfig config = new RollupJobConfig( + sample.getId(), + sample.getIndexPattern(), + "", + sample.getCron(), + sample.getPageSize(), + sample.getGroupConfig(), + sample.getMetricsConfig(), + sample.getTimeout() + ); Optional validation = config.validate(); assertThat(validation, notNullValue()); @@ -182,8 +256,16 @@ public void testValidateEmptyRollupPattern() { public void testValidateNullCron() { final RollupJobConfig sample = randomRollupJobConfig(id); - final RollupJobConfig config = new RollupJobConfig(sample.getId(), sample.getIndexPattern(), sample.getRollupIndex(), null, - sample.getPageSize(), sample.getGroupConfig(), sample.getMetricsConfig(), sample.getTimeout()); + final RollupJobConfig config = new RollupJobConfig( + sample.getId(), + sample.getIndexPattern(), + sample.getRollupIndex(), + null, + sample.getPageSize(), + sample.getGroupConfig(), + sample.getMetricsConfig(), + sample.getTimeout() + ); Optional validation = config.validate(); assertThat(validation, notNullValue()); @@ -196,8 +278,16 @@ public void testValidateNullCron() { public void testValidateEmptyCron() { final RollupJobConfig sample = randomRollupJobConfig(id); - final RollupJobConfig config = new RollupJobConfig(sample.getId(), sample.getIndexPattern(), sample.getRollupIndex(), "", - sample.getPageSize(), sample.getGroupConfig(), sample.getMetricsConfig(), sample.getTimeout()); + final RollupJobConfig config = new RollupJobConfig( + sample.getId(), + sample.getIndexPattern(), + sample.getRollupIndex(), + "", + sample.getPageSize(), + sample.getGroupConfig(), + sample.getMetricsConfig(), + sample.getTimeout() + ); Optional validation = config.validate(); assertThat(validation, notNullValue()); @@ -210,8 +300,16 @@ public void testValidateEmptyCron() { public void testValidatePageSize() { final RollupJobConfig sample = randomRollupJobConfig(id); - final RollupJobConfig config = new RollupJobConfig(sample.getId(), sample.getIndexPattern(), sample.getRollupIndex(), - sample.getCron(), 0, sample.getGroupConfig(), sample.getMetricsConfig(), sample.getTimeout()); + final RollupJobConfig config = new RollupJobConfig( + sample.getId(), + sample.getIndexPattern(), + sample.getRollupIndex(), + sample.getCron(), + 0, + sample.getGroupConfig(), + sample.getMetricsConfig(), + sample.getTimeout() + ); Optional validation = config.validate(); assertThat(validation, notNullValue()); @@ -224,8 +322,16 @@ public void testValidatePageSize() { public void testValidateGroupOrMetrics() { final RollupJobConfig sample = randomRollupJobConfig(id); - final RollupJobConfig config = new RollupJobConfig(sample.getId(), sample.getIndexPattern(), sample.getRollupIndex(), - sample.getCron(), sample.getPageSize(), null, null, sample.getTimeout()); + final RollupJobConfig config = new RollupJobConfig( + sample.getId(), + sample.getIndexPattern(), + sample.getRollupIndex(), + sample.getCron(), + sample.getPageSize(), + null, + null, + sample.getTimeout() + ); Optional validation = config.validate(); assertThat(validation, notNullValue()); @@ -239,8 +345,16 @@ public void testValidateGroupConfigWithErrors() { final GroupConfig groupConfig = new GroupConfig(null); final RollupJobConfig sample = randomRollupJobConfig(id); - final RollupJobConfig config = new RollupJobConfig(sample.getId(), sample.getIndexPattern(), sample.getRollupIndex(), - sample.getCron(), sample.getPageSize(), groupConfig, sample.getMetricsConfig(), sample.getTimeout()); + final RollupJobConfig config = new RollupJobConfig( + sample.getId(), + sample.getIndexPattern(), + sample.getRollupIndex(), + sample.getCron(), + sample.getPageSize(), + groupConfig, + sample.getMetricsConfig(), + sample.getTimeout() + ); Optional validation = config.validate(); assertThat(validation, notNullValue()); @@ -254,16 +368,26 @@ public void testValidateListOfMetricsWithErrors() { final List metricsConfigs = singletonList(new MetricConfig(null, null)); final RollupJobConfig sample = randomRollupJobConfig(id); - final RollupJobConfig config = new RollupJobConfig(sample.getId(), sample.getIndexPattern(), sample.getRollupIndex(), - sample.getCron(), sample.getPageSize(), sample.getGroupConfig(), metricsConfigs, sample.getTimeout()); + final RollupJobConfig config = new RollupJobConfig( + sample.getId(), + sample.getIndexPattern(), + sample.getRollupIndex(), + sample.getCron(), + sample.getPageSize(), + sample.getGroupConfig(), + metricsConfigs, + sample.getTimeout() + ); Optional validation = config.validate(); assertThat(validation, notNullValue()); assertThat(validation.isPresent(), is(true)); ValidationException validationException = validation.get(); assertThat(validationException.validationErrors().size(), is(2)); - assertThat(validationException.validationErrors(), - containsInAnyOrder("Field name is required", "Metrics must be a non-null, non-empty array of strings")); + assertThat( + validationException.validationErrors(), + containsInAnyOrder("Field name is required", "Metrics must be a non-null, non-empty array of strings") + ); } public static RollupJobConfig randomRollupJobConfig(final String id) { @@ -271,8 +395,9 @@ public static RollupJobConfig randomRollupJobConfig(final String id) { final String rollupIndex = "rollup_" + indexPattern; final String cron = randomCron(); final int pageSize = randomIntBetween(1, 100); - final TimeValue timeout = randomBoolean() ? null : - new TimeValue(randomIntBetween(0, 60), randomFrom(Arrays.asList(TimeUnit.MILLISECONDS, TimeUnit.SECONDS, TimeUnit.MINUTES))); + final TimeValue timeout = randomBoolean() + ? null + : new TimeValue(randomIntBetween(0, 60), randomFrom(Arrays.asList(TimeUnit.MILLISECONDS, TimeUnit.SECONDS, TimeUnit.MINUTES))); final GroupConfig groups = GroupConfigTests.randomGroupConfig(); final List metrics = new ArrayList<>(); @@ -286,12 +411,12 @@ public static RollupJobConfig randomRollupJobConfig(final String id) { } private static String randomCron() { - return (ESTestCase.randomBoolean() ? "*" : String.valueOf(ESTestCase.randomIntBetween(0, 59))) + //second - " " + (ESTestCase.randomBoolean() ? "*" : String.valueOf(ESTestCase.randomIntBetween(0, 59))) + //minute - " " + (ESTestCase.randomBoolean() ? "*" : String.valueOf(ESTestCase.randomIntBetween(0, 23))) + //hour - " " + (ESTestCase.randomBoolean() ? "*" : String.valueOf(ESTestCase.randomIntBetween(1, 31))) + //day of month - " " + (ESTestCase.randomBoolean() ? "*" : String.valueOf(ESTestCase.randomIntBetween(1, 12))) + //month - " ?" + //day of week - " " + (ESTestCase.randomBoolean() ? "*" : String.valueOf(ESTestCase.randomIntBetween(1970, 2199))); //year + return (ESTestCase.randomBoolean() ? "*" : String.valueOf(ESTestCase.randomIntBetween(0, 59))) + // second + " " + (ESTestCase.randomBoolean() ? "*" : String.valueOf(ESTestCase.randomIntBetween(0, 59))) + // minute + " " + (ESTestCase.randomBoolean() ? "*" : String.valueOf(ESTestCase.randomIntBetween(0, 23))) + // hour + " " + (ESTestCase.randomBoolean() ? "*" : String.valueOf(ESTestCase.randomIntBetween(1, 31))) + // day of month + " " + (ESTestCase.randomBoolean() ? "*" : String.valueOf(ESTestCase.randomIntBetween(1, 12))) + // month + " ?" + // day of week + " " + (ESTestCase.randomBoolean() ? "*" : String.valueOf(ESTestCase.randomIntBetween(1970, 2199))); // year } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/job/config/TermsGroupConfigTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/job/config/TermsGroupConfigTests.java index 42fcc832d0d2c..9c7859036bf64 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/job/config/TermsGroupConfigTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/job/config/TermsGroupConfigTests.java @@ -9,8 +9,8 @@ import org.elasticsearch.client.ValidationException; import org.elasticsearch.common.Strings; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.Optional; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/AuthenticateResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/AuthenticateResponseTests.java index 8deec181ee0ec..9730dab2276e3 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/AuthenticateResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/AuthenticateResponseTests.java @@ -9,10 +9,10 @@ package org.elasticsearch.client.security; import org.elasticsearch.client.security.user.User; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.EqualsHashCodeTestUtils; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.ArrayList; @@ -26,21 +26,16 @@ public class AuthenticateResponseTests extends ESTestCase { public void testFromXContent() throws IOException { - xContentTester( - this::createParser, - this::createTestInstance, - this::toXContent, - AuthenticateResponse::fromXContent) + xContentTester(this::createParser, this::createTestInstance, this::toXContent, AuthenticateResponse::fromXContent) .supportsUnknownFields(true) - //metadata and token are a series of kv pairs, so we dont want to add random fields here for test equality + // metadata and token are a series of kv pairs, so we dont want to add random fields here for test equality .randomFieldsExcludeFilter(f -> f.startsWith("metadata") || f.equals("token")) .test(); } public void testEqualsAndHashCode() { final AuthenticateResponse response = createTestInstance(); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(response, this::copy, - this::mutate); + EqualsHashCodeTestUtils.checkEqualsAndHashCode(response, this::copy, this::mutate); } protected AuthenticateResponse createTestInstance() { @@ -62,10 +57,11 @@ protected AuthenticateResponse createTestInstance() { final String email = randomFrom(random(), null, randomAlphaOfLengthBetween(0, 4)); final boolean enabled = randomBoolean(); final String authenticationRealmName = randomAlphaOfLength(5); - final String authenticationRealmType = randomFrom( - "service_account"); - final AuthenticateResponse.RealmInfo authenticationRealm = - new AuthenticateResponse.RealmInfo(authenticationRealmName, authenticationRealmType); + final String authenticationRealmType = randomFrom("service_account"); + final AuthenticateResponse.RealmInfo authenticationRealm = new AuthenticateResponse.RealmInfo( + authenticationRealmName, + authenticationRealmType + ); final AuthenticateResponse.RealmInfo lookupRealm; final Map tokenInfo; @@ -82,8 +78,13 @@ protected AuthenticateResponse createTestInstance() { final String authenticationType = randomFrom("realm", "api_key", "token", "anonymous", "internal"); return new AuthenticateResponse( - new User(username, roles, metadata, fullName, email), enabled, authenticationRealm, - lookupRealm, authenticationType, tokenInfo); + new User(username, roles, metadata, fullName, email), + enabled, + authenticationRealm, + lookupRealm, + authenticationType, + tokenInfo + ); } private void toXContent(AuthenticateResponse response, XContentBuilder builder) throws IOException { @@ -92,70 +93,192 @@ private void toXContent(AuthenticateResponse response, XContentBuilder builder) private AuthenticateResponse copy(AuthenticateResponse response) { final User originalUser = response.getUser(); - final User copyUser = new User(originalUser.getUsername(), originalUser.getRoles(), originalUser.getMetadata(), - originalUser.getFullName(), originalUser.getEmail()); - return new AuthenticateResponse(copyUser, response.enabled(), response.getAuthenticationRealm(), - response.getLookupRealm(), response.getAuthenticationType(), Map.copyOf(response.getToken())); + final User copyUser = new User( + originalUser.getUsername(), + originalUser.getRoles(), + originalUser.getMetadata(), + originalUser.getFullName(), + originalUser.getEmail() + ); + return new AuthenticateResponse( + copyUser, + response.enabled(), + response.getAuthenticationRealm(), + response.getLookupRealm(), + response.getAuthenticationType(), + Map.copyOf(response.getToken()) + ); } private AuthenticateResponse mutate(AuthenticateResponse response) { final User originalUser = response.getUser(); switch (randomIntBetween(1, 10)) { case 1: - return new AuthenticateResponse(new User(originalUser.getUsername() + "wrong", originalUser.getRoles(), - originalUser.getMetadata(), originalUser.getFullName(), originalUser.getEmail()), response.enabled(), - response.getAuthenticationRealm(), response.getLookupRealm(), response.getAuthenticationType(), response.getToken()); + return new AuthenticateResponse( + new User( + originalUser.getUsername() + "wrong", + originalUser.getRoles(), + originalUser.getMetadata(), + originalUser.getFullName(), + originalUser.getEmail() + ), + response.enabled(), + response.getAuthenticationRealm(), + response.getLookupRealm(), + response.getAuthenticationType(), + response.getToken() + ); case 2: final List wrongRoles = new ArrayList<>(originalUser.getRoles()); wrongRoles.add(randomAlphaOfLengthBetween(1, 4)); - return new AuthenticateResponse(new User(originalUser.getUsername(), wrongRoles, originalUser.getMetadata(), - originalUser.getFullName(), originalUser.getEmail()), response.enabled(), response.getAuthenticationRealm(), - response.getLookupRealm(), response.getAuthenticationType(), response.getToken()); + return new AuthenticateResponse( + new User( + originalUser.getUsername(), + wrongRoles, + originalUser.getMetadata(), + originalUser.getFullName(), + originalUser.getEmail() + ), + response.enabled(), + response.getAuthenticationRealm(), + response.getLookupRealm(), + response.getAuthenticationType(), + response.getToken() + ); case 3: final Map wrongMetadata = new HashMap<>(originalUser.getMetadata()); wrongMetadata.put("wrong_string", randomAlphaOfLengthBetween(0, 4)); - return new AuthenticateResponse(new User(originalUser.getUsername(), originalUser.getRoles(), wrongMetadata, - originalUser.getFullName(), originalUser.getEmail()), response.enabled(), response.getAuthenticationRealm(), - response.getLookupRealm(), response.getAuthenticationType(), response.getToken()); + return new AuthenticateResponse( + new User( + originalUser.getUsername(), + originalUser.getRoles(), + wrongMetadata, + originalUser.getFullName(), + originalUser.getEmail() + ), + response.enabled(), + response.getAuthenticationRealm(), + response.getLookupRealm(), + response.getAuthenticationType(), + response.getToken() + ); case 4: - return new AuthenticateResponse(new User(originalUser.getUsername(), originalUser.getRoles(), originalUser.getMetadata(), - originalUser.getFullName() + "wrong", originalUser.getEmail()), response.enabled(), - response.getAuthenticationRealm(), response.getLookupRealm(), response.getAuthenticationType()); + return new AuthenticateResponse( + new User( + originalUser.getUsername(), + originalUser.getRoles(), + originalUser.getMetadata(), + originalUser.getFullName() + "wrong", + originalUser.getEmail() + ), + response.enabled(), + response.getAuthenticationRealm(), + response.getLookupRealm(), + response.getAuthenticationType() + ); case 5: - return new AuthenticateResponse(new User(originalUser.getUsername(), originalUser.getRoles(), originalUser.getMetadata(), - originalUser.getFullName(), originalUser.getEmail() + "wrong"), response.enabled(), - response.getAuthenticationRealm(), response.getLookupRealm(), response.getAuthenticationType(), response.getToken()); + return new AuthenticateResponse( + new User( + originalUser.getUsername(), + originalUser.getRoles(), + originalUser.getMetadata(), + originalUser.getFullName(), + originalUser.getEmail() + "wrong" + ), + response.enabled(), + response.getAuthenticationRealm(), + response.getLookupRealm(), + response.getAuthenticationType(), + response.getToken() + ); case 6: - return new AuthenticateResponse(new User(originalUser.getUsername(), originalUser.getRoles(), originalUser.getMetadata(), - originalUser.getFullName(), originalUser.getEmail()), response.enabled() == false, response.getAuthenticationRealm(), - response.getLookupRealm(), response.getAuthenticationType(), response.getToken()); + return new AuthenticateResponse( + new User( + originalUser.getUsername(), + originalUser.getRoles(), + originalUser.getMetadata(), + originalUser.getFullName(), + originalUser.getEmail() + ), + response.enabled() == false, + response.getAuthenticationRealm(), + response.getLookupRealm(), + response.getAuthenticationType(), + response.getToken() + ); case 7: - return new AuthenticateResponse(new User(originalUser.getUsername(), originalUser.getRoles(), originalUser.getMetadata(), - originalUser.getFullName(), originalUser.getEmail()), response.enabled(), response.getAuthenticationRealm(), + return new AuthenticateResponse( + new User( + originalUser.getUsername(), + originalUser.getRoles(), + originalUser.getMetadata(), + originalUser.getFullName(), + originalUser.getEmail() + ), + response.enabled(), + response.getAuthenticationRealm(), new AuthenticateResponse.RealmInfo(randomAlphaOfLength(5), randomAlphaOfLength(5)), - response.getAuthenticationType(), response.getToken()); + response.getAuthenticationType(), + response.getToken() + ); case 8: - return new AuthenticateResponse(new User(originalUser.getUsername(), originalUser.getRoles(), originalUser.getMetadata(), - originalUser.getFullName(), originalUser.getEmail()), response.enabled(), - new AuthenticateResponse.RealmInfo(randomAlphaOfLength(5), randomAlphaOfLength(5)), response.getLookupRealm(), - response.getAuthenticationType(), response.getToken()); + return new AuthenticateResponse( + new User( + originalUser.getUsername(), + originalUser.getRoles(), + originalUser.getMetadata(), + originalUser.getFullName(), + originalUser.getEmail() + ), + response.enabled(), + new AuthenticateResponse.RealmInfo(randomAlphaOfLength(5), randomAlphaOfLength(5)), + response.getLookupRealm(), + response.getAuthenticationType(), + response.getToken() + ); case 9: - return new AuthenticateResponse(new User(originalUser.getUsername(), originalUser.getRoles(), originalUser.getMetadata(), - originalUser.getFullName(), originalUser.getEmail()), response.enabled(), response.getAuthenticationRealm(), + return new AuthenticateResponse( + new User( + originalUser.getUsername(), + originalUser.getRoles(), + originalUser.getMetadata(), + originalUser.getFullName(), + originalUser.getEmail() + ), + response.enabled(), + response.getAuthenticationRealm(), response.getLookupRealm(), - randomValueOtherThan(response.getAuthenticationType(), - () -> randomFrom("realm", "api_key", "token", "anonymous", "internal")), response.getToken()); + randomValueOtherThan( + response.getAuthenticationType(), + () -> randomFrom("realm", "api_key", "token", "anonymous", "internal") + ), + response.getToken() + ); default: - return new AuthenticateResponse(new User(originalUser.getUsername(), originalUser.getRoles(), originalUser.getMetadata(), - originalUser.getFullName(), originalUser.getEmail()), response.enabled(), response.getAuthenticationRealm(), + return new AuthenticateResponse( + new User( + originalUser.getUsername(), + originalUser.getRoles(), + originalUser.getMetadata(), + originalUser.getFullName(), + originalUser.getEmail() + ), + response.enabled(), + response.getAuthenticationRealm(), response.getLookupRealm(), response.getAuthenticationType(), - response.getToken() == null ? - Map.of("foo", "bar") : - randomFrom(Map.of( - "name", randomValueOtherThan(response.getToken().get("name"), () -> randomAlphaOfLengthBetween(3, 8)), - "type", randomValueOtherThan(response.getToken().get("type"), () -> randomAlphaOfLengthBetween(3, 8)) - ), null)); + response.getToken() == null + ? Map.of("foo", "bar") + : randomFrom( + Map.of( + "name", + randomValueOtherThan(response.getToken().get("name"), () -> randomAlphaOfLengthBetween(3, 8)), + "type", + randomValueOtherThan(response.getToken().get("type"), () -> randomAlphaOfLengthBetween(3, 8)) + ), + null + ) + ); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/ClearRealmCacheResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/ClearRealmCacheResponseTests.java index 95d624b7f2288..32b60d29a7cf0 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/ClearRealmCacheResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/ClearRealmCacheResponseTests.java @@ -10,11 +10,11 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.Strings; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.ESTestCase; import java.io.IOException; @@ -28,10 +28,16 @@ public class ClearRealmCacheResponseTests extends ESTestCase { public void testParseFromXContent() throws IOException { final ElasticsearchException exception = new ElasticsearchException("test"); final String nodesHeader = "\"_nodes\": { \"total\": 2, \"successful\": 1, \"failed\": 1, \"failures\": [ " - + Strings.toString(exception) + "] },"; + + Strings.toString(exception) + + "] },"; final String clusterName = "\"cluster_name\": \"cn\","; - try (XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, "{" + nodesHeader + clusterName + "\"nodes\" : {} }")) { + try ( + XContentParser parser = JsonXContent.jsonXContent.createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + "{" + nodesHeader + clusterName + "\"nodes\" : {} }" + ) + ) { ClearRealmCacheResponse response = ClearRealmCacheResponse.fromXContent(parser); assertNotNull(response); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/ClearRolesCacheResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/ClearRolesCacheResponseTests.java index eb6c75c75596c..2aa19a0a77821 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/ClearRolesCacheResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/ClearRolesCacheResponseTests.java @@ -10,11 +10,11 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.Strings; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.ESTestCase; import java.io.IOException; @@ -28,10 +28,16 @@ public class ClearRolesCacheResponseTests extends ESTestCase { public void testParseFromXContent() throws IOException { final ElasticsearchException exception = new ElasticsearchException("test"); final String nodesHeader = "\"_nodes\": { \"total\": 2, \"successful\": 1, \"failed\": 1, \"failures\": [ " - + Strings.toString(exception) + "] },"; + + Strings.toString(exception) + + "] },"; final String clusterName = "\"cluster_name\": \"cn\","; - try (XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, "{" + nodesHeader + clusterName + "\"nodes\" : {} }")) { + try ( + XContentParser parser = JsonXContent.jsonXContent.createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + "{" + nodesHeader + clusterName + "\"nodes\" : {} }" + ) + ) { ClearRolesCacheResponse response = ClearRolesCacheResponse.fromXContent(parser); assertNotNull(response); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/ClearServiceAccountTokenCacheRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/ClearServiceAccountTokenCacheRequestTests.java index 47134074bf9c0..308067ee9f7e2 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/ClearServiceAccountTokenCacheRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/ClearServiceAccountTokenCacheRequestTests.java @@ -22,8 +22,11 @@ public void testNewInstance() { final String serviceName = randomAlphaOfLengthBetween(3, 8); final String[] tokenNames = randomArray(0, 3, String[]::new, () -> randomAlphaOfLengthBetween(3, 8)); - final ClearServiceAccountTokenCacheRequest clearServiceAccountTokenCacheRequest = - new ClearServiceAccountTokenCacheRequest(namespace, serviceName, tokenNames); + final ClearServiceAccountTokenCacheRequest clearServiceAccountTokenCacheRequest = new ClearServiceAccountTokenCacheRequest( + namespace, + serviceName, + tokenNames + ); assertThat(clearServiceAccountTokenCacheRequest.getNamespace(), equalTo(namespace)); assertThat(clearServiceAccountTokenCacheRequest.getServiceName(), equalTo(serviceName)); @@ -37,9 +40,11 @@ public void testEqualsHashCode() { final ClearServiceAccountTokenCacheRequest request = new ClearServiceAccountTokenCacheRequest(namespace, serviceName, tokenNames); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(request, + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + request, original -> new ClearServiceAccountTokenCacheRequest(request.getNamespace(), request.getServiceName(), request.getTokenNames()), - this::mutateInstance); + this::mutateInstance + ); } private ClearServiceAccountTokenCacheRequest mutateInstance(ClearServiceAccountTokenCacheRequest request) { @@ -48,18 +53,23 @@ private ClearServiceAccountTokenCacheRequest mutateInstance(ClearServiceAccountT return new ClearServiceAccountTokenCacheRequest( randomValueOtherThan(request.getNamespace(), () -> randomAlphaOfLengthBetween(3, 8)), request.getServiceName(), - request.getTokenNames()); + request.getTokenNames() + ); case 1: return new ClearServiceAccountTokenCacheRequest( request.getNamespace(), randomValueOtherThan(request.getServiceName(), () -> randomAlphaOfLengthBetween(3, 8)), - request.getTokenNames()); + request.getTokenNames() + ); default: return new ClearServiceAccountTokenCacheRequest( request.getNamespace(), request.getServiceName(), - randomValueOtherThanMany(a -> Arrays.equals(a, request.getTokenNames()), - () -> randomArray(0, 3, String[]::new, () -> randomAlphaOfLengthBetween(3, 8)))); + randomValueOtherThanMany( + a -> Arrays.equals(a, request.getTokenNames()), + () -> randomArray(0, 3, String[]::new, () -> randomAlphaOfLengthBetween(3, 8)) + ) + ); } } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/CreateApiKeyRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/CreateApiKeyRequestTests.java index 0dcf2480a378b..35e3ce3edcc1c 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/CreateApiKeyRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/CreateApiKeyRequestTests.java @@ -12,11 +12,11 @@ import org.elasticsearch.client.security.user.privileges.Role; import org.elasticsearch.client.security.user.privileges.Role.ClusterPrivilegeName; import org.elasticsearch.client.security.user.privileges.Role.IndexPrivilegeName; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.EqualsHashCodeTestUtils; +import org.elasticsearch.xcontent.XContentType; import java.io.IOException; import java.util.ArrayList; @@ -33,87 +33,165 @@ public class CreateApiKeyRequestTests extends ESTestCase { public void test() throws IOException { List roles = new ArrayList<>(); - roles.add(Role.builder().name("r1").clusterPrivileges(ClusterPrivilegeName.ALL) - .indicesPrivileges(IndicesPrivileges.builder().indices("ind-x").privileges(IndexPrivilegeName.ALL).build()).build()); - roles.add(Role.builder().name("r2").clusterPrivileges(ClusterPrivilegeName.ALL) - .indicesPrivileges(IndicesPrivileges.builder().indices("ind-y").privileges(IndexPrivilegeName.ALL).build()).build()); + roles.add( + Role.builder() + .name("r1") + .clusterPrivileges(ClusterPrivilegeName.ALL) + .indicesPrivileges(IndicesPrivileges.builder().indices("ind-x").privileges(IndexPrivilegeName.ALL).build()) + .build() + ); + roles.add( + Role.builder() + .name("r2") + .clusterPrivileges(ClusterPrivilegeName.ALL) + .indicesPrivileges(IndicesPrivileges.builder().indices("ind-y").privileges(IndexPrivilegeName.ALL).build()) + .build() + ); final Map apiKeyMetadata = randomMetadata(); CreateApiKeyRequest createApiKeyRequest = new CreateApiKeyRequest("api-key", roles, null, null, apiKeyMetadata); - Map expected = new HashMap<>(Map.of( - "name", "api-key", - "role_descriptors", Map.of( - "r1", Map.of( - "applications", List.of(), - "cluster", List.of("all"), - "indices", List.of( - Map.of("names", List.of("ind-x"), "privileges", List.of("all"), "allow_restricted_indices", false)), - "metadata", Map.of(), - "run_as", List.of()), - "r2", Map.of( - "applications", List.of(), - "cluster", List.of("all"), - "indices", List.of( - Map.of("names", List.of("ind-y"), "privileges", List.of("all"), "allow_restricted_indices", false)), - "metadata", Map.of(), - "run_as", List.of())) - )); + Map expected = new HashMap<>( + Map.of( + "name", + "api-key", + "role_descriptors", + Map.of( + "r1", + Map.of( + "applications", + List.of(), + "cluster", + List.of("all"), + "indices", + List.of(Map.of("names", List.of("ind-x"), "privileges", List.of("all"), "allow_restricted_indices", false)), + "metadata", + Map.of(), + "run_as", + List.of() + ), + "r2", + Map.of( + "applications", + List.of(), + "cluster", + List.of("all"), + "indices", + List.of(Map.of("names", List.of("ind-y"), "privileges", List.of("all"), "allow_restricted_indices", false)), + "metadata", + Map.of(), + "run_as", + List.of() + ) + ) + ) + ); if (apiKeyMetadata != null) { expected.put("metadata", apiKeyMetadata); } assertThat( - XContentHelper.convertToMap(XContentHelper.toXContent( - createApiKeyRequest, XContentType.JSON, false), false, XContentType.JSON).v2(), - equalTo(expected)); + XContentHelper.convertToMap(XContentHelper.toXContent(createApiKeyRequest, XContentType.JSON, false), false, XContentType.JSON) + .v2(), + equalTo(expected) + ); } public void testEqualsHashCode() { final String name = randomAlphaOfLength(5); - List roles = Collections.singletonList(Role.builder().name("r1").clusterPrivileges(ClusterPrivilegeName.ALL) - .indicesPrivileges(IndicesPrivileges.builder().indices("ind-x").privileges(IndexPrivilegeName.ALL).build()).build()); + List roles = Collections.singletonList( + Role.builder() + .name("r1") + .clusterPrivileges(ClusterPrivilegeName.ALL) + .indicesPrivileges(IndicesPrivileges.builder().indices("ind-x").privileges(IndexPrivilegeName.ALL).build()) + .build() + ); final TimeValue expiration = null; final RefreshPolicy refreshPolicy = randomFrom(RefreshPolicy.values()); CreateApiKeyRequest createApiKeyRequest = new CreateApiKeyRequest(name, roles, expiration, refreshPolicy, randomMetadata()); EqualsHashCodeTestUtils.checkEqualsAndHashCode(createApiKeyRequest, (original) -> { - return new CreateApiKeyRequest(original.getName(), original.getRoles(), original.getExpiration(), original.getRefreshPolicy(), - original.getMetadata()); + return new CreateApiKeyRequest( + original.getName(), + original.getRoles(), + original.getExpiration(), + original.getRefreshPolicy(), + original.getMetadata() + ); }); EqualsHashCodeTestUtils.checkEqualsAndHashCode(createApiKeyRequest, (original) -> { - return new CreateApiKeyRequest(original.getName(), original.getRoles(), original.getExpiration(), original.getRefreshPolicy(), - original.getMetadata()); + return new CreateApiKeyRequest( + original.getName(), + original.getRoles(), + original.getExpiration(), + original.getRefreshPolicy(), + original.getMetadata() + ); }, CreateApiKeyRequestTests::mutateTestItem); } private static CreateApiKeyRequest mutateTestItem(CreateApiKeyRequest original) { switch (randomIntBetween(0, 4)) { - case 0: - return new CreateApiKeyRequest(randomAlphaOfLength(5), original.getRoles(), original.getExpiration(), - original.getRefreshPolicy(), original.getMetadata()); - case 1: - return new CreateApiKeyRequest(original.getName(), - Collections.singletonList(Role.builder().name(randomAlphaOfLength(6)).clusterPrivileges(ClusterPrivilegeName.ALL) + case 0: + return new CreateApiKeyRequest( + randomAlphaOfLength(5), + original.getRoles(), + original.getExpiration(), + original.getRefreshPolicy(), + original.getMetadata() + ); + case 1: + return new CreateApiKeyRequest( + original.getName(), + Collections.singletonList( + Role.builder() + .name(randomAlphaOfLength(6)) + .clusterPrivileges(ClusterPrivilegeName.ALL) .indicesPrivileges( - IndicesPrivileges.builder().indices(randomAlphaOfLength(4)).privileges(IndexPrivilegeName.ALL).build()) - .build()), - original.getExpiration(), original.getRefreshPolicy(), original.getMetadata()); - case 2: - return new CreateApiKeyRequest(original.getName(), original.getRoles(), TimeValue.timeValueSeconds(10000), - original.getRefreshPolicy(), original.getMetadata()); - case 3: - List values = Arrays.stream(RefreshPolicy.values()).filter(rp -> rp != original.getRefreshPolicy()) + IndicesPrivileges.builder().indices(randomAlphaOfLength(4)).privileges(IndexPrivilegeName.ALL).build() + ) + .build() + ), + original.getExpiration(), + original.getRefreshPolicy(), + original.getMetadata() + ); + case 2: + return new CreateApiKeyRequest( + original.getName(), + original.getRoles(), + TimeValue.timeValueSeconds(10000), + original.getRefreshPolicy(), + original.getMetadata() + ); + case 3: + List values = Arrays.stream(RefreshPolicy.values()) + .filter(rp -> rp != original.getRefreshPolicy()) .collect(Collectors.toList()); - return new CreateApiKeyRequest(original.getName(), original.getRoles(), original.getExpiration(), randomFrom(values), - original.getMetadata()); - case 4: - return new CreateApiKeyRequest(original.getName(), original.getRoles(), original.getExpiration(), original.getRefreshPolicy(), - randomValueOtherThan(original.getMetadata(), CreateApiKeyRequestTests::randomMetadata)); - default: - return new CreateApiKeyRequest(randomAlphaOfLength(5), original.getRoles(), original.getExpiration(), - original.getRefreshPolicy(), original.getMetadata()); + return new CreateApiKeyRequest( + original.getName(), + original.getRoles(), + original.getExpiration(), + randomFrom(values), + original.getMetadata() + ); + case 4: + return new CreateApiKeyRequest( + original.getName(), + original.getRoles(), + original.getExpiration(), + original.getRefreshPolicy(), + randomValueOtherThan(original.getMetadata(), CreateApiKeyRequestTests::randomMetadata) + ); + default: + return new CreateApiKeyRequest( + randomAlphaOfLength(5), + original.getRoles(), + original.getExpiration(), + original.getRefreshPolicy(), + original.getMetadata() + ); } } @@ -123,6 +201,7 @@ public static Map randomMetadata() { Map.of("status", "active", "level", 42, "nested", Map.of("foo", "bar")), Map.of("status", "active"), Map.of(), - null); + null + ); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/CreateApiKeyResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/CreateApiKeyResponseTests.java index 9a14b9c1455ca..efa559a43bc6a 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/CreateApiKeyResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/CreateApiKeyResponseTests.java @@ -8,15 +8,15 @@ package org.elasticsearch.client.security; -import org.elasticsearch.core.CharArrays; import org.elasticsearch.common.UUIDs; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.SecureString; +import org.elasticsearch.core.CharArrays; +import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.EqualsHashCodeTestUtils; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.test.EqualsHashCodeTestUtils; import java.io.IOException; import java.nio.charset.StandardCharsets; @@ -70,28 +70,43 @@ public void testEqualsHashCode() { final Instant expiration = Instant.ofEpochMilli(10000); CreateApiKeyResponse createApiKeyResponse = new CreateApiKeyResponse(name, id, apiKey, expiration); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(createApiKeyResponse, (original) -> { - return new CreateApiKeyResponse(original.getName(), original.getId(), original.getKey(), original.getExpiration()); - }); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(createApiKeyResponse, (original) -> { - return new CreateApiKeyResponse(original.getName(), original.getId(), original.getKey(), original.getExpiration()); - }, CreateApiKeyResponseTests::mutateTestItem); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + createApiKeyResponse, + (original) -> { + return new CreateApiKeyResponse(original.getName(), original.getId(), original.getKey(), original.getExpiration()); + } + ); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + createApiKeyResponse, + (original) -> { + return new CreateApiKeyResponse(original.getName(), original.getId(), original.getKey(), original.getExpiration()); + }, + CreateApiKeyResponseTests::mutateTestItem + ); } private static CreateApiKeyResponse mutateTestItem(CreateApiKeyResponse original) { switch (randomIntBetween(0, 3)) { - case 0: - return new CreateApiKeyResponse(randomAlphaOfLength(7), original.getId(), original.getKey(), original.getExpiration()); - case 1: - return new CreateApiKeyResponse(original.getName(), randomAlphaOfLengthBetween(4, 8), original.getKey(), - original.getExpiration()); - case 2: - return new CreateApiKeyResponse(original.getName(), original.getId(), UUIDs.randomBase64UUIDSecureString(), - original.getExpiration()); - case 3: - return new CreateApiKeyResponse(original.getName(), original.getId(), original.getKey(), Instant.ofEpochMilli(150000)); - default: - return new CreateApiKeyResponse(randomAlphaOfLength(7), original.getId(), original.getKey(), original.getExpiration()); + case 0: + return new CreateApiKeyResponse(randomAlphaOfLength(7), original.getId(), original.getKey(), original.getExpiration()); + case 1: + return new CreateApiKeyResponse( + original.getName(), + randomAlphaOfLengthBetween(4, 8), + original.getKey(), + original.getExpiration() + ); + case 2: + return new CreateApiKeyResponse( + original.getName(), + original.getId(), + UUIDs.randomBase64UUIDSecureString(), + original.getExpiration() + ); + case 3: + return new CreateApiKeyResponse(original.getName(), original.getId(), original.getKey(), Instant.ofEpochMilli(150000)); + default: + return new CreateApiKeyResponse(randomAlphaOfLength(7), original.getId(), original.getKey(), original.getExpiration()); } } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/CreateServiceAccountTokenRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/CreateServiceAccountTokenRequestTests.java index ddcc575d44cf0..6b2b04ea2a398 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/CreateServiceAccountTokenRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/CreateServiceAccountTokenRequestTests.java @@ -33,8 +33,12 @@ public void testNewInstance() { assertNull(request2.getRefreshPolicy()); final RefreshPolicy refreshPolicy = randomFrom(RefreshPolicy.values()); - final CreateServiceAccountTokenRequest request3 = - new CreateServiceAccountTokenRequest(namespace, serviceName, tokenName, refreshPolicy); + final CreateServiceAccountTokenRequest request3 = new CreateServiceAccountTokenRequest( + namespace, + serviceName, + tokenName, + refreshPolicy + ); assertThat(request3.getNamespace(), equalTo(namespace)); assertThat(request3.getServiceName(), equalTo(serviceName)); assertThat(request3.getTokenName(), equalTo(tokenName)); @@ -49,10 +53,16 @@ public void testEqualsHashCode() { final CreateServiceAccountTokenRequest request = new CreateServiceAccountTokenRequest(namespace, service, tokenName, refreshPolicy); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(request, + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + request, original -> new CreateServiceAccountTokenRequest( - request.getNamespace(), request.getServiceName(), request.getTokenName(), request.getRefreshPolicy()), - this::mutateInstance); + request.getNamespace(), + request.getServiceName(), + request.getTokenName(), + request.getRefreshPolicy() + ), + this::mutateInstance + ); } private CreateServiceAccountTokenRequest mutateInstance(CreateServiceAccountTokenRequest request) { @@ -62,25 +72,29 @@ private CreateServiceAccountTokenRequest mutateInstance(CreateServiceAccountToke randomValueOtherThan(request.getNamespace(), () -> randomAlphaOfLengthBetween(3, 8)), request.getServiceName(), request.getTokenName(), - request.getRefreshPolicy()); + request.getRefreshPolicy() + ); case 1: return new CreateServiceAccountTokenRequest( request.getNamespace(), randomValueOtherThan(request.getServiceName(), () -> randomAlphaOfLengthBetween(3, 8)), request.getTokenName(), - request.getRefreshPolicy()); + request.getRefreshPolicy() + ); case 2: return new CreateServiceAccountTokenRequest( request.getNamespace(), request.getServiceName(), randomValueOtherThan(request.getTokenName(), () -> randomAlphaOfLengthBetween(3, 8)), - request.getRefreshPolicy()); + request.getRefreshPolicy() + ); default: return new CreateServiceAccountTokenRequest( request.getNamespace(), request.getServiceName(), request.getTokenName(), - randomValueOtherThan(request.getRefreshPolicy(), () -> randomFrom(RefreshPolicy.values()))); + randomValueOtherThan(request.getRefreshPolicy(), () -> randomFrom(RefreshPolicy.values())) + ); } } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/CreateServiceAccountTokenResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/CreateServiceAccountTokenResponseTests.java index e33a3ccb8525b..83c8abe8119c0 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/CreateServiceAccountTokenResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/CreateServiceAccountTokenResponseTests.java @@ -17,17 +17,20 @@ import static org.hamcrest.Matchers.equalTo; -public class CreateServiceAccountTokenResponseTests - extends AbstractResponseTestCase { @Override protected org.elasticsearch.xpack.core.security.action.service.CreateServiceAccountTokenResponse createServerTestInstance( - XContentType xContentType) { + XContentType xContentType + ) { final String tokenName = randomAlphaOfLengthBetween(3, 8); final String value = randomAlphaOfLength(22); return org.elasticsearch.xpack.core.security.action.service.CreateServiceAccountTokenResponse.created( - tokenName, new SecureString(value.toCharArray())); + tokenName, + new SecureString(value.toCharArray()) + ); } @Override @@ -38,7 +41,8 @@ protected CreateServiceAccountTokenResponse doParseToClientInstance(XContentPars @Override protected void assertInstances( org.elasticsearch.xpack.core.security.action.service.CreateServiceAccountTokenResponse serverTestInstance, - CreateServiceAccountTokenResponse clientInstance) { + CreateServiceAccountTokenResponse clientInstance + ) { assertThat(serverTestInstance.getName(), equalTo(clientInstance.getName())); assertThat(serverTestInstance.getValue(), equalTo(clientInstance.getValue())); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/CreateTokenRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/CreateTokenRequestTests.java index 0110cdaf6fc96..55d19ecb7a706 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/CreateTokenRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/CreateTokenRequestTests.java @@ -60,10 +60,7 @@ public void testCreateTokenFromKerberosTicket() { assertThat(request.getPassword(), nullValue()); assertThat(request.getRefreshToken(), nullValue()); assertThat(new String(request.getKerberosTicket()), equalTo("top secret kerberos ticket")); - assertThat( - Strings.toString(request), - equalTo("{\"grant_type\":\"_kerberos\",\"kerberos_ticket\":\"top secret kerberos ticket\"}") - ); + assertThat(Strings.toString(request), equalTo("{\"grant_type\":\"_kerberos\",\"kerberos_ticket\":\"top secret kerberos ticket\"}")); } public void testEqualsAndHashCode() { @@ -74,34 +71,78 @@ public void testEqualsAndHashCode() { final String refreshToken = randomBoolean() ? null : randomAlphaOfLengthBetween(12, 24); final char[] kerberosTicket = randomBoolean() ? null : randomAlphaOfLengthBetween(8, 12).toCharArray(); final CreateTokenRequest request = new CreateTokenRequest(grantType, scope, username, password, refreshToken, kerberosTicket); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(request, - r -> new CreateTokenRequest(r.getGrantType(), r.getScope(), r.getUsername(), r.getPassword(), - r.getRefreshToken(), r.getKerberosTicket()), - this::mutate); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + request, + r -> new CreateTokenRequest( + r.getGrantType(), + r.getScope(), + r.getUsername(), + r.getPassword(), + r.getRefreshToken(), + r.getKerberosTicket() + ), + this::mutate + ); } private CreateTokenRequest mutate(CreateTokenRequest req) { switch (randomIntBetween(1, 6)) { - case 1: - return new CreateTokenRequest("g", req.getScope(), req.getUsername(), req.getPassword(), req.getRefreshToken(), - req.getKerberosTicket()); - case 2: - return new CreateTokenRequest(req.getGrantType(), "s", req.getUsername(), req.getPassword(), req.getRefreshToken(), - req.getKerberosTicket()); - case 3: - return new CreateTokenRequest(req.getGrantType(), req.getScope(), "u", req.getPassword(), req.getRefreshToken(), - req.getKerberosTicket()); - case 4: - final char[] password = { 'p' }; - return new CreateTokenRequest(req.getGrantType(), req.getScope(), req.getUsername(), password, req.getRefreshToken(), - req.getKerberosTicket()); - case 5: - final char[] kerberosTicket = { 'k' }; - return new CreateTokenRequest(req.getGrantType(), req.getScope(), req.getUsername(), req.getPassword(), req.getRefreshToken(), - kerberosTicket); - case 6: - return new CreateTokenRequest(req.getGrantType(), req.getScope(), req.getUsername(), req.getPassword(), "r", - req.getKerberosTicket()); + case 1: + return new CreateTokenRequest( + "g", + req.getScope(), + req.getUsername(), + req.getPassword(), + req.getRefreshToken(), + req.getKerberosTicket() + ); + case 2: + return new CreateTokenRequest( + req.getGrantType(), + "s", + req.getUsername(), + req.getPassword(), + req.getRefreshToken(), + req.getKerberosTicket() + ); + case 3: + return new CreateTokenRequest( + req.getGrantType(), + req.getScope(), + "u", + req.getPassword(), + req.getRefreshToken(), + req.getKerberosTicket() + ); + case 4: + final char[] password = { 'p' }; + return new CreateTokenRequest( + req.getGrantType(), + req.getScope(), + req.getUsername(), + password, + req.getRefreshToken(), + req.getKerberosTicket() + ); + case 5: + final char[] kerberosTicket = { 'k' }; + return new CreateTokenRequest( + req.getGrantType(), + req.getScope(), + req.getUsername(), + req.getPassword(), + req.getRefreshToken(), + kerberosTicket + ); + case 6: + return new CreateTokenRequest( + req.getGrantType(), + req.getScope(), + req.getUsername(), + req.getPassword(), + "r", + req.getKerberosTicket() + ); } throw new IllegalStateException("Bad random number"); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/CreateTokenResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/CreateTokenResponseTests.java index 439cd43ce2d33..cbf7212438618 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/CreateTokenResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/CreateTokenResponseTests.java @@ -10,10 +10,10 @@ import org.elasticsearch.client.security.user.User; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; import java.io.IOException; import java.util.Arrays; @@ -29,17 +29,17 @@ public void testFromXContent() throws IOException { final String scope = randomBoolean() ? null : randomAlphaOfLength(4); final String type = randomAlphaOfLength(6); final String kerberosAuthenticationResponseToken = randomBoolean() ? null : randomAlphaOfLength(7); - final AuthenticateResponse authentication = new AuthenticateResponse(new User(randomAlphaOfLength(7), - Arrays.asList( randomAlphaOfLength(9) )), - true, new AuthenticateResponse.RealmInfo(randomAlphaOfLength(5), randomAlphaOfLength(7) ), - new AuthenticateResponse.RealmInfo(randomAlphaOfLength(5), randomAlphaOfLength(5) ), "realm"); + final AuthenticateResponse authentication = new AuthenticateResponse( + new User(randomAlphaOfLength(7), Arrays.asList(randomAlphaOfLength(9))), + true, + new AuthenticateResponse.RealmInfo(randomAlphaOfLength(5), randomAlphaOfLength(7)), + new AuthenticateResponse.RealmInfo(randomAlphaOfLength(5), randomAlphaOfLength(5)), + "realm" + ); final XContentType xContentType = randomFrom(XContentType.values()); final XContentBuilder builder = XContentFactory.contentBuilder(xContentType); - builder.startObject() - .field("access_token", accessToken) - .field("type", type) - .field("expires_in", expiresIn.seconds()); + builder.startObject().field("access_token", accessToken).field("type", type).field("expires_in", expiresIn.seconds()); if (refreshToken != null || randomBoolean()) { builder.field("refresh_token", refreshToken); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DelegatePkiAuthenticationRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DelegatePkiAuthenticationRequestTests.java index 666a355d5569e..38cd07fc0de1d 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DelegatePkiAuthenticationRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DelegatePkiAuthenticationRequestTests.java @@ -30,17 +30,17 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -public class DelegatePkiAuthenticationRequestTests extends AbstractRequestTestCase { +public class DelegatePkiAuthenticationRequestTests extends AbstractRequestTestCase< + DelegatePkiAuthenticationRequest, + org.elasticsearch.xpack.core.security.action.DelegatePkiAuthenticationRequest> { public void testEmptyOrNullCertificateChain() throws Exception { - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> { - new DelegatePkiAuthenticationRequest((List)null); - }); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> { new DelegatePkiAuthenticationRequest((List) null); } + ); assertThat(e.getMessage(), is("certificate chain must not be empty or null")); - e = expectThrows(IllegalArgumentException.class, () -> { - new DelegatePkiAuthenticationRequest(Collections.emptyList()); - }); + e = expectThrows(IllegalArgumentException.class, () -> { new DelegatePkiAuthenticationRequest(Collections.emptyList()); }); assertThat(e.getMessage(), is("certificate chain must not be empty or null")); } @@ -65,13 +65,15 @@ protected DelegatePkiAuthenticationRequest createClientTestInstance() { @Override protected org.elasticsearch.xpack.core.security.action.DelegatePkiAuthenticationRequest doParseToServerInstance(XContentParser parser) - throws IOException { + throws IOException { return org.elasticsearch.xpack.core.security.action.DelegatePkiAuthenticationRequest.fromXContent(parser); } @Override - protected void assertInstances(org.elasticsearch.xpack.core.security.action.DelegatePkiAuthenticationRequest serverInstance, - DelegatePkiAuthenticationRequest clientTestInstance) { + protected void assertInstances( + org.elasticsearch.xpack.core.security.action.DelegatePkiAuthenticationRequest serverInstance, + DelegatePkiAuthenticationRequest clientTestInstance + ) { assertThat(serverInstance.getCertificateChain(), is(clientTestInstance.getCertificateChain())); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DelegatePkiAuthenticationResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DelegatePkiAuthenticationResponseTests.java index 091205f64144b..5e1dd555c1c39 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DelegatePkiAuthenticationResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DelegatePkiAuthenticationResponseTests.java @@ -25,16 +25,19 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; -public class DelegatePkiAuthenticationResponseTests extends - AbstractResponseTestCase { +public class DelegatePkiAuthenticationResponseTests extends AbstractResponseTestCase< + org.elasticsearch.xpack.core.security.action.DelegatePkiAuthenticationResponse, + DelegatePkiAuthenticationResponse> { @Override protected org.elasticsearch.xpack.core.security.action.DelegatePkiAuthenticationResponse createServerTestInstance( - XContentType xContentType) { - return new org.elasticsearch.xpack.core.security.action.DelegatePkiAuthenticationResponse(randomAlphaOfLength(6), - TimeValue.parseTimeValue(randomTimeValue(), getClass().getSimpleName() + ".expiresIn"), - createAuthentication()); + XContentType xContentType + ) { + return new org.elasticsearch.xpack.core.security.action.DelegatePkiAuthenticationResponse( + randomAlphaOfLength(6), + TimeValue.parseTimeValue(randomTimeValue(), getClass().getSimpleName() + ".expiresIn"), + createAuthentication() + ); } @Override @@ -43,8 +46,10 @@ protected DelegatePkiAuthenticationResponse doParseToClientInstance(XContentPars } @Override - protected void assertInstances(org.elasticsearch.xpack.core.security.action.DelegatePkiAuthenticationResponse serverTestInstance, - DelegatePkiAuthenticationResponse clientInstance) { + protected void assertInstances( + org.elasticsearch.xpack.core.security.action.DelegatePkiAuthenticationResponse serverTestInstance, + DelegatePkiAuthenticationResponse clientInstance + ) { assertThat(serverTestInstance.getAccessToken(), is(clientInstance.getAccessToken())); assertThat(serverTestInstance.getExpiresIn(), is(clientInstance.getExpiresIn())); assertThat(clientInstance.getType(), is("Bearer")); @@ -80,20 +85,40 @@ protected Authentication createAuthentication() { return new Authentication( new User(username, roles, fullName, email, metadata, true), new Authentication.RealmRef(authenticationRealmName, authenticationRealmType, nodeName), - new Authentication.RealmRef(lookupRealmName, lookupRealmType, nodeName), Version.CURRENT, authenticationType, metadata); + new Authentication.RealmRef(lookupRealmName, lookupRealmType, nodeName), + Version.CURRENT, + authenticationType, + metadata + ); } - AuthenticateResponse createServerAuthenticationResponse(Authentication authentication){ + AuthenticateResponse createServerAuthenticationResponse(Authentication authentication) { User user = authentication.getUser(); - org.elasticsearch.client.security.user.User cUser = new org.elasticsearch.client.security.user.User(user.principal(), - Arrays.asList(user.roles()), user.metadata(), user.fullName(), user.email()); - AuthenticateResponse.RealmInfo authenticatedBy = new AuthenticateResponse.RealmInfo(authentication.getAuthenticatedBy().getName(), - authentication.getAuthenticatedBy().getType()); - AuthenticateResponse.RealmInfo lookedUpBy = new AuthenticateResponse.RealmInfo(authentication.getLookedUpBy() == null? - authentication.getAuthenticatedBy().getName(): authentication.getLookedUpBy().getName(), - authentication.getLookedUpBy() == null? - authentication.getAuthenticatedBy().getType(): authentication.getLookedUpBy().getType()); - return new AuthenticateResponse(cUser, user.enabled(), authenticatedBy, lookedUpBy, - authentication.getAuthenticationType().toString().toLowerCase(Locale.ROOT)); + org.elasticsearch.client.security.user.User cUser = new org.elasticsearch.client.security.user.User( + user.principal(), + Arrays.asList(user.roles()), + user.metadata(), + user.fullName(), + user.email() + ); + AuthenticateResponse.RealmInfo authenticatedBy = new AuthenticateResponse.RealmInfo( + authentication.getAuthenticatedBy().getName(), + authentication.getAuthenticatedBy().getType() + ); + AuthenticateResponse.RealmInfo lookedUpBy = new AuthenticateResponse.RealmInfo( + authentication.getLookedUpBy() == null + ? authentication.getAuthenticatedBy().getName() + : authentication.getLookedUpBy().getName(), + authentication.getLookedUpBy() == null + ? authentication.getAuthenticatedBy().getType() + : authentication.getLookedUpBy().getType() + ); + return new AuthenticateResponse( + cUser, + user.enabled(), + authenticatedBy, + lookedUpBy, + authentication.getAuthenticationType().toString().toLowerCase(Locale.ROOT) + ); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteRoleMappingRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteRoleMappingRequestTests.java index a79365ab600fd..7e9e180337c2f 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteRoleMappingRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteRoleMappingRequestTests.java @@ -39,13 +39,16 @@ public void testEqualsHashCode() { final DeleteRoleMappingRequest deleteRoleMappingRequest = new DeleteRoleMappingRequest(name, refreshPolicy); assertNotNull(deleteRoleMappingRequest); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(deleteRoleMappingRequest, (original) -> { - return new DeleteRoleMappingRequest(original.getName(), original.getRefreshPolicy()); - }); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + deleteRoleMappingRequest, + (original) -> { return new DeleteRoleMappingRequest(original.getName(), original.getRefreshPolicy()); } + ); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(deleteRoleMappingRequest, (original) -> { - return new DeleteRoleMappingRequest(original.getName(), original.getRefreshPolicy()); - }, DeleteRoleMappingRequestTests::mutateTestItem); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + deleteRoleMappingRequest, + (original) -> { return new DeleteRoleMappingRequest(original.getName(), original.getRefreshPolicy()); }, + DeleteRoleMappingRequestTests::mutateTestItem + ); } @@ -53,8 +56,9 @@ private static DeleteRoleMappingRequest mutateTestItem(DeleteRoleMappingRequest if (randomBoolean()) { return new DeleteRoleMappingRequest(randomAlphaOfLength(5), original.getRefreshPolicy()); } else { - List values = Arrays.stream(RefreshPolicy.values()).filter(rp -> rp != original.getRefreshPolicy()).collect( - Collectors.toList()); + List values = Arrays.stream(RefreshPolicy.values()) + .filter(rp -> rp != original.getRefreshPolicy()) + .collect(Collectors.toList()); return new DeleteRoleMappingRequest(original.getName(), randomFrom(values)); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteRoleMappingResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteRoleMappingResponseTests.java index 16a19eca08b29..d3efb947dcbd4 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteRoleMappingResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteRoleMappingResponseTests.java @@ -8,11 +8,11 @@ package org.elasticsearch.client.security; +import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.EqualsHashCodeTestUtils; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.test.EqualsHashCodeTestUtils; import java.io.IOException; import java.util.Collections; @@ -23,8 +23,10 @@ public class DeleteRoleMappingResponseTests extends ESTestCase { public void testFromXContent() throws IOException { final String json = "{ \"found\" : \"true\" }"; - final DeleteRoleMappingResponse response = DeleteRoleMappingResponse.fromXContent(XContentType.JSON.xContent().createParser( - new NamedXContentRegistry(Collections.emptyList()), DeprecationHandler.IGNORE_DEPRECATIONS, json)); + final DeleteRoleMappingResponse response = DeleteRoleMappingResponse.fromXContent( + XContentType.JSON.xContent() + .createParser(new NamedXContentRegistry(Collections.emptyList()), DeprecationHandler.IGNORE_DEPRECATIONS, json) + ); final DeleteRoleMappingResponse expectedResponse = new DeleteRoleMappingResponse(true); assertThat(response, equalTo(expectedResponse)); } @@ -33,13 +35,16 @@ public void testEqualsHashCode() { final boolean found = randomBoolean(); final DeleteRoleMappingResponse deleteRoleMappingResponse = new DeleteRoleMappingResponse(found); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(deleteRoleMappingResponse, (original) -> { - return new DeleteRoleMappingResponse(original.isFound()); - }); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + deleteRoleMappingResponse, + (original) -> { return new DeleteRoleMappingResponse(original.isFound()); } + ); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(deleteRoleMappingResponse, (original) -> { - return new DeleteRoleMappingResponse(original.isFound()); - }, DeleteRoleMappingResponseTests::mutateTestItem); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + deleteRoleMappingResponse, + (original) -> { return new DeleteRoleMappingResponse(original.isFound()); }, + DeleteRoleMappingResponseTests::mutateTestItem + ); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteRoleResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteRoleResponseTests.java index 3640a52cc2432..3a09f61e3f8fd 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteRoleResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteRoleResponseTests.java @@ -9,12 +9,12 @@ package org.elasticsearch.client.security; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; import java.io.IOException; @@ -23,8 +23,7 @@ public class DeleteRoleResponseTests extends ESTestCase { public void testBasicParsing() throws IOException { XContentType contentType = randomFrom(XContentType.values()); final boolean found = randomBoolean(); - XContentBuilder builder = XContentFactory.contentBuilder(contentType).startObject() - .field("found", found).endObject(); + XContentBuilder builder = XContentFactory.contentBuilder(contentType).startObject().field("found", found).endObject(); BytesReference bytes = BytesReference.bytes(builder); DeleteRoleResponse response = parse(builder.contentType(), bytes); @@ -40,8 +39,7 @@ public void testParsingWithMissingField() throws IOException { } private DeleteRoleResponse parse(XContentType contentType, BytesReference bytes) throws IOException { - XContentParser parser = XContentFactory.xContent(contentType) - .createParser(NamedXContentRegistry.EMPTY, null, bytes.streamInput()); + XContentParser parser = XContentFactory.xContent(contentType).createParser(NamedXContentRegistry.EMPTY, null, bytes.streamInput()); parser.nextToken(); return DeleteRoleResponse.fromXContent(parser); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteServiceAccountTokenRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteServiceAccountTokenRequestTests.java index 21c008afcf991..8f5da59077c37 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteServiceAccountTokenRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteServiceAccountTokenRequestTests.java @@ -27,8 +27,12 @@ public void testNewInstance() { assertNull(request1.getRefreshPolicy()); final RefreshPolicy refreshPolicy = randomFrom(RefreshPolicy.values()); - final DeleteServiceAccountTokenRequest request2 = - new DeleteServiceAccountTokenRequest(namespace, serviceName, tokenName, refreshPolicy); + final DeleteServiceAccountTokenRequest request2 = new DeleteServiceAccountTokenRequest( + namespace, + serviceName, + tokenName, + refreshPolicy + ); assertThat(request2.getNamespace(), equalTo(namespace)); assertThat(request2.getServiceName(), equalTo(serviceName)); assertThat(request2.getTokenName(), equalTo(tokenName)); @@ -41,13 +45,23 @@ public void testEqualsHashCode() { final String tokenName = randomAlphaOfLengthBetween(3, 8); final RefreshPolicy refreshPolicy = randomBoolean() ? randomFrom(RefreshPolicy.values()) : null; - final DeleteServiceAccountTokenRequest request = - new DeleteServiceAccountTokenRequest(namespace, serviceName, tokenName, refreshPolicy); + final DeleteServiceAccountTokenRequest request = new DeleteServiceAccountTokenRequest( + namespace, + serviceName, + tokenName, + refreshPolicy + ); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(request, + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + request, original -> new DeleteServiceAccountTokenRequest( - request.getNamespace(), request.getServiceName(), request.getTokenName(), request.getRefreshPolicy()), - this::mutateInstance); + request.getNamespace(), + request.getServiceName(), + request.getTokenName(), + request.getRefreshPolicy() + ), + this::mutateInstance + ); } private DeleteServiceAccountTokenRequest mutateInstance(DeleteServiceAccountTokenRequest request) { @@ -57,25 +71,29 @@ private DeleteServiceAccountTokenRequest mutateInstance(DeleteServiceAccountToke randomValueOtherThan(request.getNamespace(), () -> randomAlphaOfLengthBetween(3, 8)), request.getServiceName(), request.getTokenName(), - request.getRefreshPolicy()); + request.getRefreshPolicy() + ); case 1: return new DeleteServiceAccountTokenRequest( request.getNamespace(), randomValueOtherThan(request.getServiceName(), () -> randomAlphaOfLengthBetween(3, 8)), request.getTokenName(), - request.getRefreshPolicy()); + request.getRefreshPolicy() + ); case 2: return new DeleteServiceAccountTokenRequest( request.getNamespace(), request.getServiceName(), randomValueOtherThan(request.getTokenName(), () -> randomAlphaOfLengthBetween(3, 8)), - request.getRefreshPolicy()); + request.getRefreshPolicy() + ); default: return new DeleteServiceAccountTokenRequest( request.getNamespace(), request.getServiceName(), request.getTokenName(), - randomValueOtherThan(request.getRefreshPolicy(), () -> randomFrom(RefreshPolicy.values()))); + randomValueOtherThan(request.getRefreshPolicy(), () -> randomFrom(RefreshPolicy.values())) + ); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteServiceAccountTokenResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteServiceAccountTokenResponseTests.java index 1750995346545..576fe0dce9a4c 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteServiceAccountTokenResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteServiceAccountTokenResponseTests.java @@ -16,13 +16,14 @@ import static org.hamcrest.Matchers.is; -public class DeleteServiceAccountTokenResponseTests - extends AbstractResponseTestCase { @Override protected org.elasticsearch.xpack.core.security.action.service.DeleteServiceAccountTokenResponse createServerTestInstance( - XContentType xContentType) { + XContentType xContentType + ) { return new org.elasticsearch.xpack.core.security.action.service.DeleteServiceAccountTokenResponse(randomBoolean()); } @@ -34,7 +35,8 @@ protected DeleteServiceAccountTokenResponse doParseToClientInstance(XContentPars @Override protected void assertInstances( org.elasticsearch.xpack.core.security.action.service.DeleteServiceAccountTokenResponse serverTestInstance, - DeleteServiceAccountTokenResponse clientInstance) { + DeleteServiceAccountTokenResponse clientInstance + ) { assertThat(serverTestInstance.found(), is(clientInstance.isAcknowledged())); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteUserRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteUserRequestTests.java index 4068f8e74dc01..da80cd5b51054 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteUserRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteUserRequestTests.java @@ -28,14 +28,18 @@ public void testDeleteUserRequest() { } public void testDeleteUserRequestThrowsExceptionForNullName() { - final NullPointerException ile = - expectThrows(NullPointerException.class, () -> new DeleteUserRequest(null, randomFrom(RefreshPolicy.values()))); + final NullPointerException ile = expectThrows( + NullPointerException.class, + () -> new DeleteUserRequest(null, randomFrom(RefreshPolicy.values())) + ); assertThat(ile.getMessage(), equalTo("user name is required")); } public void testDeleteUserRequestThrowsExceptionForNullRefreshPolicy() { - final NullPointerException ile = - expectThrows(NullPointerException.class, () -> new DeleteUserRequest(randomAlphaOfLength(10), null)); + final NullPointerException ile = expectThrows( + NullPointerException.class, + () -> new DeleteUserRequest(randomAlphaOfLength(10), null) + ); assertThat(ile.getMessage(), equalTo("refresh policy is required")); } @@ -45,13 +49,16 @@ public void testEqualsHashCode() { final DeleteUserRequest deleteUserRequest = new DeleteUserRequest(name, refreshPolicy); assertNotNull(deleteUserRequest); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(deleteUserRequest, (original) -> { - return new DeleteUserRequest(original.getName(), original.getRefreshPolicy()); - }); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + deleteUserRequest, + (original) -> { return new DeleteUserRequest(original.getName(), original.getRefreshPolicy()); } + ); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(deleteUserRequest, (original) -> { - return new DeleteUserRequest(original.getName(), original.getRefreshPolicy()); - }, DeleteUserRequestTests::mutateTestItem); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + deleteUserRequest, + (original) -> { return new DeleteUserRequest(original.getName(), original.getRefreshPolicy()); }, + DeleteUserRequestTests::mutateTestItem + ); } @@ -59,8 +66,9 @@ private static DeleteUserRequest mutateTestItem(DeleteUserRequest original) { if (randomBoolean()) { return new DeleteUserRequest(randomAlphaOfLength(10), original.getRefreshPolicy()); } else { - List values = Arrays.stream(RefreshPolicy.values()).filter(rp -> rp != original.getRefreshPolicy()).collect( - Collectors.toList()); + List values = Arrays.stream(RefreshPolicy.values()) + .filter(rp -> rp != original.getRefreshPolicy()) + .collect(Collectors.toList()); return new DeleteUserRequest(original.getName(), randomFrom(values)); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteUserResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteUserResponseTests.java index 4e359264d0079..2c8d79bf21824 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteUserResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteUserResponseTests.java @@ -9,12 +9,12 @@ package org.elasticsearch.client.security; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; import java.io.IOException; @@ -24,8 +24,7 @@ public void testParsingWithMissingField() throws IOException { XContentType contentType = randomFrom(XContentType.values()); XContentBuilder builder = XContentFactory.contentBuilder(contentType).startObject().endObject(); BytesReference bytes = BytesReference.bytes(builder); - XContentParser parser = XContentFactory.xContent(contentType) - .createParser(NamedXContentRegistry.EMPTY, null, bytes.streamInput()); + XContentParser parser = XContentFactory.xContent(contentType).createParser(NamedXContentRegistry.EMPTY, null, bytes.streamInput()); parser.nextToken(); expectThrows(IllegalArgumentException.class, () -> DeleteUserResponse.fromXContent(parser)); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/ExpressionRoleMappingTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/ExpressionRoleMappingTests.java index ff9cfc4584298..3d40c08b96d08 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/ExpressionRoleMappingTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/ExpressionRoleMappingTests.java @@ -9,11 +9,11 @@ package org.elasticsearch.client.security; import org.elasticsearch.client.security.support.expressiondsl.fields.FieldRoleMapperExpression; +import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.EqualsHashCodeTestUtils; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.test.EqualsHashCodeTestUtils; import java.io.IOException; import java.util.Collections; @@ -26,69 +26,122 @@ public class ExpressionRoleMappingTests extends ESTestCase { public void testExpressionRoleMappingParser() throws IOException { - final String json = - "{\n" + - " \"enabled\" : true,\n" + - " \"roles\" : [\n" + - " \"superuser\"\n" + - " ],\n" + - " \"rules\" : {\n" + - " \"field\" : {\n" + - " \"realm.name\" : \"kerb1\"\n" + - " }\n" + - " },\n" + - " \"metadata\" : { }\n" + - " }"; - final ExpressionRoleMapping expressionRoleMapping = ExpressionRoleMapping.PARSER.parse(XContentType.JSON.xContent().createParser( - new NamedXContentRegistry(Collections.emptyList()), DeprecationHandler.IGNORE_DEPRECATIONS, json), "example-role-mapping"); - final ExpressionRoleMapping expectedRoleMapping = new ExpressionRoleMapping("example-role-mapping", + final String json = "{\n" + + " \"enabled\" : true,\n" + + " \"roles\" : [\n" + + " \"superuser\"\n" + + " ],\n" + + " \"rules\" : {\n" + + " \"field\" : {\n" + + " \"realm.name\" : \"kerb1\"\n" + + " }\n" + + " },\n" + + " \"metadata\" : { }\n" + + " }"; + final ExpressionRoleMapping expressionRoleMapping = ExpressionRoleMapping.PARSER.parse( + XContentType.JSON.xContent() + .createParser(new NamedXContentRegistry(Collections.emptyList()), DeprecationHandler.IGNORE_DEPRECATIONS, json), + "example-role-mapping" + ); + final ExpressionRoleMapping expectedRoleMapping = new ExpressionRoleMapping( + "example-role-mapping", FieldRoleMapperExpression.ofKeyValues("realm.name", "kerb1"), - singletonList("superuser"), Collections.emptyList(), - null, true); + singletonList("superuser"), + Collections.emptyList(), + null, + true + ); assertThat(expressionRoleMapping, equalTo(expectedRoleMapping)); } public void testEqualsHashCode() { - final ExpressionRoleMapping expressionRoleMapping = new ExpressionRoleMapping("kerberosmapping", + final ExpressionRoleMapping expressionRoleMapping = new ExpressionRoleMapping( + "kerberosmapping", FieldRoleMapperExpression.ofKeyValues("realm.name", "kerb1"), - singletonList("superuser"), Collections.emptyList(), - null, true); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(expressionRoleMapping, original -> - new ExpressionRoleMapping(original.getName(), original.getExpression(), original.getRoles(), original.getRoleTemplates(), - original.getMetadata(), original.isEnabled()), ExpressionRoleMappingTests::mutateTestItem); + singletonList("superuser"), + Collections.emptyList(), + null, + true + ); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + expressionRoleMapping, + original -> new ExpressionRoleMapping( + original.getName(), + original.getExpression(), + original.getRoles(), + original.getRoleTemplates(), + original.getMetadata(), + original.isEnabled() + ), + ExpressionRoleMappingTests::mutateTestItem + ); } private static ExpressionRoleMapping mutateTestItem(ExpressionRoleMapping original) throws IOException { ExpressionRoleMapping mutated = null; switch (randomIntBetween(0, 5)) { - case 0: - mutated = new ExpressionRoleMapping("namechanged", FieldRoleMapperExpression.ofKeyValues("realm.name", "kerb1"), - singletonList("superuser"), Collections.emptyList(), null, true); - break; - case 1: - mutated = new ExpressionRoleMapping("kerberosmapping", FieldRoleMapperExpression.ofKeyValues("changed", "changed"), - singletonList("superuser"), Collections.emptyList(), null, true); - break; - case 2: - mutated = new ExpressionRoleMapping("kerberosmapping", FieldRoleMapperExpression.ofKeyValues("realm.name", "kerb1"), - singletonList("changed"), Collections.emptyList(), null, true); - break; - case 3: - Map metadata = new HashMap<>(); - metadata.put("a", "b"); - mutated = new ExpressionRoleMapping("kerberosmapping", FieldRoleMapperExpression.ofKeyValues("realm.name", "kerb1"), - singletonList("superuser"), Collections.emptyList(), metadata, true); - break; - case 4: - mutated = new ExpressionRoleMapping("kerberosmapping", FieldRoleMapperExpression.ofKeyValues("realm.name", "kerb1"), - Collections.emptyList(), - singletonList(new TemplateRoleName(Collections.singletonMap("source", "superuser"), TemplateRoleName.Format.STRING)), - null, true); - break; - case 5: - mutated = new ExpressionRoleMapping("kerberosmapping", FieldRoleMapperExpression.ofKeyValues("realm.name", "kerb1"), - singletonList("superuser"), Collections.emptyList(), null, false); - break; + case 0: + mutated = new ExpressionRoleMapping( + "namechanged", + FieldRoleMapperExpression.ofKeyValues("realm.name", "kerb1"), + singletonList("superuser"), + Collections.emptyList(), + null, + true + ); + break; + case 1: + mutated = new ExpressionRoleMapping( + "kerberosmapping", + FieldRoleMapperExpression.ofKeyValues("changed", "changed"), + singletonList("superuser"), + Collections.emptyList(), + null, + true + ); + break; + case 2: + mutated = new ExpressionRoleMapping( + "kerberosmapping", + FieldRoleMapperExpression.ofKeyValues("realm.name", "kerb1"), + singletonList("changed"), + Collections.emptyList(), + null, + true + ); + break; + case 3: + Map metadata = new HashMap<>(); + metadata.put("a", "b"); + mutated = new ExpressionRoleMapping( + "kerberosmapping", + FieldRoleMapperExpression.ofKeyValues("realm.name", "kerb1"), + singletonList("superuser"), + Collections.emptyList(), + metadata, + true + ); + break; + case 4: + mutated = new ExpressionRoleMapping( + "kerberosmapping", + FieldRoleMapperExpression.ofKeyValues("realm.name", "kerb1"), + Collections.emptyList(), + singletonList(new TemplateRoleName(Collections.singletonMap("source", "superuser"), TemplateRoleName.Format.STRING)), + null, + true + ); + break; + case 5: + mutated = new ExpressionRoleMapping( + "kerberosmapping", + FieldRoleMapperExpression.ofKeyValues("realm.name", "kerb1"), + singletonList("superuser"), + Collections.emptyList(), + null, + false + ); + break; } return mutated; } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetApiKeyRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetApiKeyRequestTests.java index eba15ee8a50cc..874a1016af064 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetApiKeyRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetApiKeyRequestTests.java @@ -41,25 +41,32 @@ public void testRequestValidation() { public void testRequestValidationFailureScenarios() throws IOException { String[][] inputs = new String[][] { - { randomNullOrEmptyString(), "user", "api-kid", "api-kname", "false" }, - { "realm", randomNullOrEmptyString(), "api-kid", "api-kname", "false" }, - { "realm", "user", "api-kid", randomNullOrEmptyString(), "false" }, - { randomNullOrEmptyString(), randomNullOrEmptyString(), "api-kid", "api-kname", "false" }, - { "realm", randomNullOrEmptyString(), randomNullOrEmptyString(), randomNullOrEmptyString(), "true"}, - { randomNullOrEmptyString(), "user", randomNullOrEmptyString(), randomNullOrEmptyString(), "true"} }; + { randomNullOrEmptyString(), "user", "api-kid", "api-kname", "false" }, + { "realm", randomNullOrEmptyString(), "api-kid", "api-kname", "false" }, + { "realm", "user", "api-kid", randomNullOrEmptyString(), "false" }, + { randomNullOrEmptyString(), randomNullOrEmptyString(), "api-kid", "api-kname", "false" }, + { "realm", randomNullOrEmptyString(), randomNullOrEmptyString(), randomNullOrEmptyString(), "true" }, + { randomNullOrEmptyString(), "user", randomNullOrEmptyString(), randomNullOrEmptyString(), "true" } }; String[] expectedErrorMessages = new String[] { - "username or realm name must not be specified when the api key id or api key name is specified", - "username or realm name must not be specified when the api key id or api key name is specified", - "username or realm name must not be specified when the api key id or api key name is specified", - "only one of [api key id, api key name] can be specified", - "neither username nor realm-name may be specified when retrieving owned API keys", - "neither username nor realm-name may be specified when retrieving owned API keys" }; + "username or realm name must not be specified when the api key id or api key name is specified", + "username or realm name must not be specified when the api key id or api key name is specified", + "username or realm name must not be specified when the api key id or api key name is specified", + "only one of [api key id, api key name] can be specified", + "neither username nor realm-name may be specified when retrieving owned API keys", + "neither username nor realm-name may be specified when retrieving owned API keys" }; for (int i = 0; i < inputs.length; i++) { final int caseNo = i; - IllegalArgumentException ve = expectThrows(IllegalArgumentException.class, - () -> new GetApiKeyRequest(inputs[caseNo][0], inputs[caseNo][1], inputs[caseNo][2], inputs[caseNo][3], - Boolean.valueOf(inputs[caseNo][4]))); + IllegalArgumentException ve = expectThrows( + IllegalArgumentException.class, + () -> new GetApiKeyRequest( + inputs[caseNo][0], + inputs[caseNo][1], + inputs[caseNo][2], + inputs[caseNo][3], + Boolean.valueOf(inputs[caseNo][4]) + ) + ); assertNotNull(ve); assertThat(ve.getMessage(), equalTo(expectedErrorMessages[caseNo])); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetApiKeyResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetApiKeyResponseTests.java index 1195c531900ab..9d1f3dd648740 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetApiKeyResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetApiKeyResponseTests.java @@ -10,11 +10,11 @@ import org.elasticsearch.client.security.support.ApiKey; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.EqualsHashCodeTestUtils; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.test.EqualsHashCodeTestUtils; import java.io.IOException; import java.time.Instant; @@ -25,12 +25,25 @@ public class GetApiKeyResponseTests extends ESTestCase { public void testFromXContent() throws IOException { - ApiKey apiKeyInfo1 = createApiKeyInfo("name1", "id-1", Instant.ofEpochMilli(100000L), Instant.ofEpochMilli(10000000L), false, - "user-a", "realm-x"); - ApiKey apiKeyInfo2 = createApiKeyInfo("name2", "id-2", Instant.ofEpochMilli(100000L), Instant.ofEpochMilli(10000000L), true, - "user-b", "realm-y"); - ApiKey apiKeyInfo3 = createApiKeyInfo(null, "id-3", Instant.ofEpochMilli(100000L), null, true, - "user-c", "realm-z"); + ApiKey apiKeyInfo1 = createApiKeyInfo( + "name1", + "id-1", + Instant.ofEpochMilli(100000L), + Instant.ofEpochMilli(10000000L), + false, + "user-a", + "realm-x" + ); + ApiKey apiKeyInfo2 = createApiKeyInfo( + "name2", + "id-2", + Instant.ofEpochMilli(100000L), + Instant.ofEpochMilli(10000000L), + true, + "user-b", + "realm-y" + ); + ApiKey apiKeyInfo3 = createApiKeyInfo(null, "id-3", Instant.ofEpochMilli(100000L), null, true, "user-c", "realm-z"); GetApiKeyResponse response = new GetApiKeyResponse(Arrays.asList(apiKeyInfo1, apiKeyInfo2, apiKeyInfo3)); final XContentType xContentType = randomFrom(XContentType.values()); final XContentBuilder builder = XContentFactory.contentBuilder(xContentType); @@ -44,48 +57,70 @@ private void toXContent(GetApiKeyResponse response, final XContentBuilder builde builder.startObject(); builder.startArray("api_keys"); for (ApiKey apiKey : response.getApiKeyInfos()) { - builder.startObject() - .field("id", apiKey.getId()) - .field("name", apiKey.getName()) - .field("creation", apiKey.getCreation().toEpochMilli()); - if (apiKey.getExpiration() != null) { - builder.field("expiration", apiKey.getExpiration().toEpochMilli()); - } - builder.field("invalidated", apiKey.isInvalidated()) - .field("username", apiKey.getUsername()) - .field("realm", apiKey.getRealm()); - builder.endObject(); + builder.startObject() + .field("id", apiKey.getId()) + .field("name", apiKey.getName()) + .field("creation", apiKey.getCreation().toEpochMilli()); + if (apiKey.getExpiration() != null) { + builder.field("expiration", apiKey.getExpiration().toEpochMilli()); + } + builder.field("invalidated", apiKey.isInvalidated()).field("username", apiKey.getUsername()).field("realm", apiKey.getRealm()); + builder.endObject(); } builder.endArray(); builder.endObject(); } public void testEqualsHashCode() { - ApiKey apiKeyInfo1 = createApiKeyInfo("name1", "id-1", Instant.ofEpochMilli(100000L), Instant.ofEpochMilli(10000000L), false, - "user-a", "realm-x"); + ApiKey apiKeyInfo1 = createApiKeyInfo( + "name1", + "id-1", + Instant.ofEpochMilli(100000L), + Instant.ofEpochMilli(10000000L), + false, + "user-a", + "realm-x" + ); GetApiKeyResponse response = new GetApiKeyResponse(Arrays.asList(apiKeyInfo1)); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(response, (original) -> { - return new GetApiKeyResponse(original.getApiKeyInfos()); - }); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(response, (original) -> { - return new GetApiKeyResponse(original.getApiKeyInfos()); - }, GetApiKeyResponseTests::mutateTestItem); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + response, + (original) -> { return new GetApiKeyResponse(original.getApiKeyInfos()); } + ); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + response, + (original) -> { return new GetApiKeyResponse(original.getApiKeyInfos()); }, + GetApiKeyResponseTests::mutateTestItem + ); } private static GetApiKeyResponse mutateTestItem(GetApiKeyResponse original) { - ApiKey apiKeyInfo = createApiKeyInfo("name2", "id-2", Instant.ofEpochMilli(100000L), Instant.ofEpochMilli(10000000L), true, - "user-b", "realm-y"); + ApiKey apiKeyInfo = createApiKeyInfo( + "name2", + "id-2", + Instant.ofEpochMilli(100000L), + Instant.ofEpochMilli(10000000L), + true, + "user-b", + "realm-y" + ); switch (randomIntBetween(0, 2)) { - case 0: - return new GetApiKeyResponse(Arrays.asList(apiKeyInfo)); - default: - return new GetApiKeyResponse(Arrays.asList(apiKeyInfo)); + case 0: + return new GetApiKeyResponse(Arrays.asList(apiKeyInfo)); + default: + return new GetApiKeyResponse(Arrays.asList(apiKeyInfo)); } } - private static ApiKey createApiKeyInfo(String name, String id, Instant creation, Instant expiration, boolean invalidated, - String username, String realm) { + private static ApiKey createApiKeyInfo( + String name, + String id, + Instant creation, + Instant expiration, + boolean invalidated, + String username, + String realm + ) { return new ApiKey(name, id, creation, expiration, invalidated, username, realm, null); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetPrivilegesRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetPrivilegesRequestTests.java index 345d6e39661d2..5a914f5e16185 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetPrivilegesRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetPrivilegesRequestTests.java @@ -18,37 +18,45 @@ public class GetPrivilegesRequestTests extends ESTestCase { public void testGetPrivilegesRequest() { final String applicationName = randomAlphaOfLength(5); final int numberOfPrivileges = randomIntBetween(0, 5); - final String[] privilegeNames = randomBoolean() ? null : randomArray(numberOfPrivileges, numberOfPrivileges, String[]::new, - () -> randomAlphaOfLength(5)); + final String[] privilegeNames = randomBoolean() + ? null + : randomArray(numberOfPrivileges, numberOfPrivileges, String[]::new, () -> randomAlphaOfLength(5)); final GetPrivilegesRequest getPrivilegesRequest = new GetPrivilegesRequest(applicationName, privilegeNames); assertThat(getPrivilegesRequest.getApplicationName(), equalTo(applicationName)); assertThat(getPrivilegesRequest.getPrivilegeNames(), equalTo(privilegeNames)); } public void testPrivilegeWithoutApplication() { - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> { - new GetPrivilegesRequest(null, randomAlphaOfLength(5)); - }); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> { new GetPrivilegesRequest(null, randomAlphaOfLength(5)); } + ); assertThat(e.getMessage(), equalTo("privilege cannot be specified when application is missing")); } public void testEqualsAndHashCode() { final String applicationName = randomAlphaOfLength(5); final int numberOfPrivileges = randomIntBetween(0, 5); - final String[] privilegeNames = - randomArray(numberOfPrivileges, numberOfPrivileges, String[]::new, () -> randomAlphaOfLength(5)); + final String[] privilegeNames = randomArray(numberOfPrivileges, numberOfPrivileges, String[]::new, () -> randomAlphaOfLength(5)); final GetPrivilegesRequest getPrivilegesRequest = new GetPrivilegesRequest(applicationName, privilegeNames); final EqualsHashCodeTestUtils.MutateFunction mutate = r -> { if (randomBoolean()) { final int numberOfNewPrivileges = randomIntBetween(1, 5); - final String[] newPrivilegeNames = - randomArray(numberOfNewPrivileges, numberOfNewPrivileges, String[]::new, () -> randomAlphaOfLength(5)); + final String[] newPrivilegeNames = randomArray( + numberOfNewPrivileges, + numberOfNewPrivileges, + String[]::new, + () -> randomAlphaOfLength(5) + ); return new GetPrivilegesRequest(applicationName, newPrivilegeNames); } else { return GetPrivilegesRequest.getApplicationPrivileges(randomAlphaOfLength(6)); } }; - EqualsHashCodeTestUtils.checkEqualsAndHashCode(getPrivilegesRequest, - r -> new GetPrivilegesRequest(r.getApplicationName(), r.getPrivilegeNames()), mutate); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + getPrivilegesRequest, + r -> new GetPrivilegesRequest(r.getApplicationName(), r.getPrivilegeNames()), + mutate + ); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetPrivilegesResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetPrivilegesResponseTests.java index fc2421bb07726..78b8cdb5c3997 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetPrivilegesResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetPrivilegesResponseTests.java @@ -9,11 +9,11 @@ package org.elasticsearch.client.security; import org.elasticsearch.client.security.user.privileges.ApplicationPrivilege; +import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.EqualsHashCodeTestUtils; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.test.EqualsHashCodeTestUtils; import java.io.IOException; import java.util.ArrayList; @@ -30,67 +30,100 @@ public class GetPrivilegesResponseTests extends ESTestCase { public void testFromXContent() throws IOException { - final String json = "{" + - " \"testapp\": {" + - " \"read\": {" + - " \"application\": \"testapp\"," + - " \"name\": \"read\"," + - " \"actions\": [ \"action:login\", \"data:read/*\" ]" + - " }," + - " \"write\": {" + - " \"application\": \"testapp\"," + - " \"name\": \"write\"," + - " \"actions\": [ \"action:login\", \"data:write/*\" ]," + - " \"metadata\": { \"key1\": \"value1\" }" + - " }," + - " \"all\": {" + - " \"application\": \"testapp\"," + - " \"name\": \"all\"," + - " \"actions\": [ \"action:login\", \"data:write/*\" , \"manage:*\"]" + - " }" + - " }," + - " \"testapp2\": {" + - " \"read\": {" + - " \"application\": \"testapp2\"," + - " \"name\": \"read\"," + - " \"actions\": [ \"action:login\", \"data:read/*\" ]," + - " \"metadata\": { \"key2\": \"value2\" }" + - " }," + - " \"write\": {" + - " \"application\": \"testapp2\"," + - " \"name\": \"write\"," + - " \"actions\": [ \"action:login\", \"data:write/*\" ]" + - " }," + - " \"all\": {" + - " \"application\": \"testapp2\"," + - " \"name\": \"all\"," + - " \"actions\": [ \"action:login\", \"data:write/*\" , \"manage:*\"]" + - " }" + - " }" + - "}"; + final String json = "{" + + " \"testapp\": {" + + " \"read\": {" + + " \"application\": \"testapp\"," + + " \"name\": \"read\"," + + " \"actions\": [ \"action:login\", \"data:read/*\" ]" + + " }," + + " \"write\": {" + + " \"application\": \"testapp\"," + + " \"name\": \"write\"," + + " \"actions\": [ \"action:login\", \"data:write/*\" ]," + + " \"metadata\": { \"key1\": \"value1\" }" + + " }," + + " \"all\": {" + + " \"application\": \"testapp\"," + + " \"name\": \"all\"," + + " \"actions\": [ \"action:login\", \"data:write/*\" , \"manage:*\"]" + + " }" + + " }," + + " \"testapp2\": {" + + " \"read\": {" + + " \"application\": \"testapp2\"," + + " \"name\": \"read\"," + + " \"actions\": [ \"action:login\", \"data:read/*\" ]," + + " \"metadata\": { \"key2\": \"value2\" }" + + " }," + + " \"write\": {" + + " \"application\": \"testapp2\"," + + " \"name\": \"write\"," + + " \"actions\": [ \"action:login\", \"data:write/*\" ]" + + " }," + + " \"all\": {" + + " \"application\": \"testapp2\"," + + " \"name\": \"all\"," + + " \"actions\": [ \"action:login\", \"data:write/*\" , \"manage:*\"]" + + " }" + + " }" + + "}"; - final GetPrivilegesResponse response = GetPrivilegesResponse.fromXContent(XContentType.JSON.xContent().createParser( - new NamedXContentRegistry(Collections.emptyList()), DeprecationHandler.IGNORE_DEPRECATIONS, json)); + final GetPrivilegesResponse response = GetPrivilegesResponse.fromXContent( + XContentType.JSON.xContent() + .createParser(new NamedXContentRegistry(Collections.emptyList()), DeprecationHandler.IGNORE_DEPRECATIONS, json) + ); - final ApplicationPrivilege readTestappPrivilege = - new ApplicationPrivilege("testapp", "read", Arrays.asList("action:login", "data:read/*"), null); + final ApplicationPrivilege readTestappPrivilege = new ApplicationPrivilege( + "testapp", + "read", + Arrays.asList("action:login", "data:read/*"), + null + ); final Map metadata = new HashMap<>(); metadata.put("key1", "value1"); - final ApplicationPrivilege writeTestappPrivilege = - new ApplicationPrivilege("testapp", "write", Arrays.asList("action:login", "data:write/*"), metadata); - final ApplicationPrivilege allTestappPrivilege = - new ApplicationPrivilege("testapp", "all", Arrays.asList("action:login", "data:write/*", "manage:*"), null); + final ApplicationPrivilege writeTestappPrivilege = new ApplicationPrivilege( + "testapp", + "write", + Arrays.asList("action:login", "data:write/*"), + metadata + ); + final ApplicationPrivilege allTestappPrivilege = new ApplicationPrivilege( + "testapp", + "all", + Arrays.asList("action:login", "data:write/*", "manage:*"), + null + ); final Map metadata2 = new HashMap<>(); metadata2.put("key2", "value2"); - final ApplicationPrivilege readTestapp2Privilege = - new ApplicationPrivilege("testapp2", "read", Arrays.asList("action:login", "data:read/*"), metadata2); - final ApplicationPrivilege writeTestapp2Privilege = - new ApplicationPrivilege("testapp2", "write", Arrays.asList("action:login", "data:write/*"), null); - final ApplicationPrivilege allTestapp2Privilege = - new ApplicationPrivilege("testapp2", "all", Arrays.asList("action:login", "data:write/*", "manage:*"), null); - final GetPrivilegesResponse exptectedResponse = - new GetPrivilegesResponse(Arrays.asList(readTestappPrivilege, writeTestappPrivilege, allTestappPrivilege, - readTestapp2Privilege, writeTestapp2Privilege, allTestapp2Privilege)); + final ApplicationPrivilege readTestapp2Privilege = new ApplicationPrivilege( + "testapp2", + "read", + Arrays.asList("action:login", "data:read/*"), + metadata2 + ); + final ApplicationPrivilege writeTestapp2Privilege = new ApplicationPrivilege( + "testapp2", + "write", + Arrays.asList("action:login", "data:write/*"), + null + ); + final ApplicationPrivilege allTestapp2Privilege = new ApplicationPrivilege( + "testapp2", + "all", + Arrays.asList("action:login", "data:write/*", "manage:*"), + null + ); + final GetPrivilegesResponse exptectedResponse = new GetPrivilegesResponse( + Arrays.asList( + readTestappPrivilege, + writeTestappPrivilege, + allTestappPrivilege, + readTestapp2Privilege, + writeTestapp2Privilege, + allTestapp2Privilege + ) + ); assertThat(response, equalTo(exptectedResponse)); } @@ -99,23 +132,32 @@ public void testEqualsHashCode() { final List privileges2 = new ArrayList<>(); final Map metadata = new HashMap<>(); metadata.put("key1", "value1"); - final ApplicationPrivilege writePrivilege = - new ApplicationPrivilege("testapp", "write", Arrays.asList("action:login", "data:write/*"), - metadata); - final ApplicationPrivilege readPrivilege = - new ApplicationPrivilege("testapp", "read", Arrays.asList("data:read/*", "action:login"), - metadata); + final ApplicationPrivilege writePrivilege = new ApplicationPrivilege( + "testapp", + "write", + Arrays.asList("action:login", "data:write/*"), + metadata + ); + final ApplicationPrivilege readPrivilege = new ApplicationPrivilege( + "testapp", + "read", + Arrays.asList("data:read/*", "action:login"), + metadata + ); privileges.add(readPrivilege); privileges.add(writePrivilege); privileges2.add(writePrivilege); privileges2.add(readPrivilege); final GetPrivilegesResponse response = new GetPrivilegesResponse(privileges); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(response, (original) -> { - return new GetPrivilegesResponse(original.getPrivileges()); - }); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(response, (original) -> { - return new GetPrivilegesResponse(original.getPrivileges()); - }, GetPrivilegesResponseTests::mutateTestItem); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + response, + (original) -> { return new GetPrivilegesResponse(original.getPrivileges()); } + ); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + response, + (original) -> { return new GetPrivilegesResponse(original.getPrivileges()); }, + GetPrivilegesResponseTests::mutateTestItem + ); } private static GetPrivilegesResponse mutateTestItem(GetPrivilegesResponse original) { @@ -127,8 +169,12 @@ private static GetPrivilegesResponse mutateTestItem(GetPrivilegesResponse origin return new GetPrivilegesResponse(privileges); } else { final List privileges = new ArrayList<>(); - final ApplicationPrivilege privilege = - new ApplicationPrivilege("testapp", "all", Arrays.asList("action:login", "data:write/*", "manage:*"), null); + final ApplicationPrivilege privilege = new ApplicationPrivilege( + "testapp", + "all", + Arrays.asList("action:login", "data:write/*", "manage:*"), + null + ); privileges.add(privilege); return new GetPrivilegesResponse(privileges); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetRoleMappingsRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetRoleMappingsRequestTests.java index e67ce6ae17b1d..ffdd73e91be18 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetRoleMappingsRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetRoleMappingsRequestTests.java @@ -18,8 +18,12 @@ public class GetRoleMappingsRequestTests extends ESTestCase { public void testGetRoleMappingsRequest() { int noOfRoleMappingNames = randomIntBetween(0, 2); - final String[] roleMappingNames = randomArray(noOfRoleMappingNames, noOfRoleMappingNames, String[]::new, () -> randomAlphaOfLength( - 5)); + final String[] roleMappingNames = randomArray( + noOfRoleMappingNames, + noOfRoleMappingNames, + String[]::new, + () -> randomAlphaOfLength(5) + ); final GetRoleMappingsRequest getRoleMappingsRequest = new GetRoleMappingsRequest(roleMappingNames); assertThat(getRoleMappingsRequest.getRoleMappingNames().size(), is(noOfRoleMappingNames)); assertThat(getRoleMappingsRequest.getRoleMappingNames(), containsInAnyOrder(roleMappingNames)); @@ -30,12 +34,15 @@ public void testEqualsHashCode() { final String[] roleMappingNames = randomArray(noOfRoleMappingNames, String[]::new, () -> randomAlphaOfLength(5)); final GetRoleMappingsRequest getRoleMappingsRequest = new GetRoleMappingsRequest(roleMappingNames); assertNotNull(getRoleMappingsRequest); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(getRoleMappingsRequest, (original) -> { - return new GetRoleMappingsRequest(original.getRoleMappingNames().toArray(new String[0])); - }); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(getRoleMappingsRequest, (original) -> { - return new GetRoleMappingsRequest(original.getRoleMappingNames().toArray(new String[0])); - }, GetRoleMappingsRequestTests::mutateTestItem); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + getRoleMappingsRequest, + (original) -> { return new GetRoleMappingsRequest(original.getRoleMappingNames().toArray(new String[0])); } + ); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + getRoleMappingsRequest, + (original) -> { return new GetRoleMappingsRequest(original.getRoleMappingNames().toArray(new String[0])); }, + GetRoleMappingsRequestTests::mutateTestItem + ); } private static GetRoleMappingsRequest mutateTestItem(GetRoleMappingsRequest original) { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetRoleMappingsResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetRoleMappingsResponseTests.java index 1718de20da7a5..ec5730356a12f 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetRoleMappingsResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetRoleMappingsResponseTests.java @@ -9,11 +9,11 @@ package org.elasticsearch.client.security; import org.elasticsearch.client.security.support.expressiondsl.fields.FieldRoleMapperExpression; +import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.EqualsHashCodeTestUtils; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.test.EqualsHashCodeTestUtils; import java.io.IOException; import java.util.ArrayList; @@ -25,76 +25,118 @@ public class GetRoleMappingsResponseTests extends ESTestCase { public void testFromXContent() throws IOException { - final String json = "{\n" + - " \"kerberosmapping\" : {\n" + - " \"enabled\" : true,\n" + - " \"roles\" : [\n" + - " \"superuser\"\n" + - " ],\n" + - " \"rules\" : {\n" + - " \"field\" : {\n" + - " \"realm.name\" : \"kerb1\"\n" + - " }\n" + - " },\n" + - " \"metadata\" : { }\n" + - " },\n" + - " \"ldapmapping\" : {\n" + - " \"enabled\" : false,\n" + - " \"roles\" : [\n" + - " \"monitoring\"\n" + - " ],\n" + - " \"rules\" : {\n" + - " \"field\" : {\n" + - " \"groups\" : \"cn=ipausers,cn=groups,cn=accounts,dc=ipademo,dc=local\"\n" + - " }\n" + - " },\n" + - " \"metadata\" : { }\n" + - " }\n" + - "}"; - final GetRoleMappingsResponse response = GetRoleMappingsResponse.fromXContent(XContentType.JSON.xContent().createParser( - new NamedXContentRegistry(Collections.emptyList()), DeprecationHandler.IGNORE_DEPRECATIONS, json)); + final String json = "{\n" + + " \"kerberosmapping\" : {\n" + + " \"enabled\" : true,\n" + + " \"roles\" : [\n" + + " \"superuser\"\n" + + " ],\n" + + " \"rules\" : {\n" + + " \"field\" : {\n" + + " \"realm.name\" : \"kerb1\"\n" + + " }\n" + + " },\n" + + " \"metadata\" : { }\n" + + " },\n" + + " \"ldapmapping\" : {\n" + + " \"enabled\" : false,\n" + + " \"roles\" : [\n" + + " \"monitoring\"\n" + + " ],\n" + + " \"rules\" : {\n" + + " \"field\" : {\n" + + " \"groups\" : \"cn=ipausers,cn=groups,cn=accounts,dc=ipademo,dc=local\"\n" + + " }\n" + + " },\n" + + " \"metadata\" : { }\n" + + " }\n" + + "}"; + final GetRoleMappingsResponse response = GetRoleMappingsResponse.fromXContent( + XContentType.JSON.xContent() + .createParser(new NamedXContentRegistry(Collections.emptyList()), DeprecationHandler.IGNORE_DEPRECATIONS, json) + ); final List expectedRoleMappingsList = new ArrayList<>(); - expectedRoleMappingsList.add(new ExpressionRoleMapping("kerberosmapping", FieldRoleMapperExpression.ofKeyValues("realm.name", - "kerb1"), Collections.singletonList("superuser"), Collections.emptyList(), null, true)); - expectedRoleMappingsList.add(new ExpressionRoleMapping("ldapmapping", FieldRoleMapperExpression.ofGroups( - "cn=ipausers,cn=groups,cn=accounts,dc=ipademo,dc=local"), Collections.singletonList("monitoring"), Collections.emptyList(), - null, false)); + expectedRoleMappingsList.add( + new ExpressionRoleMapping( + "kerberosmapping", + FieldRoleMapperExpression.ofKeyValues("realm.name", "kerb1"), + Collections.singletonList("superuser"), + Collections.emptyList(), + null, + true + ) + ); + expectedRoleMappingsList.add( + new ExpressionRoleMapping( + "ldapmapping", + FieldRoleMapperExpression.ofGroups("cn=ipausers,cn=groups,cn=accounts,dc=ipademo,dc=local"), + Collections.singletonList("monitoring"), + Collections.emptyList(), + null, + false + ) + ); final GetRoleMappingsResponse expectedResponse = new GetRoleMappingsResponse(expectedRoleMappingsList); assertThat(response, equalTo(expectedResponse)); } public void testEqualsHashCode() { final List roleMappingsList = new ArrayList<>(); - roleMappingsList.add(new ExpressionRoleMapping("kerberosmapping", FieldRoleMapperExpression.ofKeyValues("realm.name", - "kerb1"), Collections.singletonList("superuser"), Collections.emptyList(), null, true)); + roleMappingsList.add( + new ExpressionRoleMapping( + "kerberosmapping", + FieldRoleMapperExpression.ofKeyValues("realm.name", "kerb1"), + Collections.singletonList("superuser"), + Collections.emptyList(), + null, + true + ) + ); final GetRoleMappingsResponse response = new GetRoleMappingsResponse(roleMappingsList); assertNotNull(response); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(response, (original) -> { - return new GetRoleMappingsResponse(original.getMappings()); - }); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(response, (original) -> { - return new GetRoleMappingsResponse(original.getMappings()); - }, GetRoleMappingsResponseTests::mutateTestItem); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + response, + (original) -> { return new GetRoleMappingsResponse(original.getMappings()); } + ); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + response, + (original) -> { return new GetRoleMappingsResponse(original.getMappings()); }, + GetRoleMappingsResponseTests::mutateTestItem + ); } private static GetRoleMappingsResponse mutateTestItem(GetRoleMappingsResponse original) { GetRoleMappingsResponse mutated = null; - switch(randomIntBetween(0, 1)) { - case 0: - final List roleMappingsList1 = new ArrayList<>(); - roleMappingsList1.add(new ExpressionRoleMapping("ldapmapping", FieldRoleMapperExpression.ofGroups( - "cn=ipausers,cn=groups,cn=accounts,dc=ipademo,dc=local"), Collections.singletonList("monitoring"), Collections.emptyList(), - null, false)); - mutated = new GetRoleMappingsResponse(roleMappingsList1); - break; - case 1: - final List roleMappingsList2 = new ArrayList<>(); - ExpressionRoleMapping originalRoleMapping = original.getMappings().get(0); - roleMappingsList2.add(new ExpressionRoleMapping(originalRoleMapping.getName(), - FieldRoleMapperExpression.ofGroups("cn=ipausers,cn=groups,cn=accounts,dc=ipademo,dc=local"), originalRoleMapping.getRoles(), - Collections.emptyList(), originalRoleMapping.getMetadata(), originalRoleMapping.isEnabled() == false)); - mutated = new GetRoleMappingsResponse(roleMappingsList2); - break; + switch (randomIntBetween(0, 1)) { + case 0: + final List roleMappingsList1 = new ArrayList<>(); + roleMappingsList1.add( + new ExpressionRoleMapping( + "ldapmapping", + FieldRoleMapperExpression.ofGroups("cn=ipausers,cn=groups,cn=accounts,dc=ipademo,dc=local"), + Collections.singletonList("monitoring"), + Collections.emptyList(), + null, + false + ) + ); + mutated = new GetRoleMappingsResponse(roleMappingsList1); + break; + case 1: + final List roleMappingsList2 = new ArrayList<>(); + ExpressionRoleMapping originalRoleMapping = original.getMappings().get(0); + roleMappingsList2.add( + new ExpressionRoleMapping( + originalRoleMapping.getName(), + FieldRoleMapperExpression.ofGroups("cn=ipausers,cn=groups,cn=accounts,dc=ipademo,dc=local"), + originalRoleMapping.getRoles(), + Collections.emptyList(), + originalRoleMapping.getMetadata(), + originalRoleMapping.isEnabled() == false + ) + ); + mutated = new GetRoleMappingsResponse(roleMappingsList2); + break; } return mutated; } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetRolesRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetRolesRequestTests.java index 4e8cec820816c..c616770de23ca 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetRolesRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetRolesRequestTests.java @@ -27,12 +27,15 @@ public void testEqualsHashCode() { final String[] roles = randomArray(0, 5, String[]::new, () -> randomAlphaOfLength(5)); final GetRolesRequest getRolesRequest = new GetRolesRequest(roles); assertNotNull(getRolesRequest); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(getRolesRequest, (original) -> { - return new GetRolesRequest(original.getRoleNames().toArray(new String[0])); - }); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(getRolesRequest, (original) -> { - return new GetRolesRequest(original.getRoleNames().toArray(new String[0])); - }, GetRolesRequestTests::mutateTestItem); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + getRolesRequest, + (original) -> { return new GetRolesRequest(original.getRoleNames().toArray(new String[0])); } + ); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + getRolesRequest, + (original) -> { return new GetRolesRequest(original.getRoleNames().toArray(new String[0])); }, + GetRolesRequestTests::mutateTestItem + ); } private static GetRolesRequest mutateTestItem(GetRolesRequest original) { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetRolesResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetRolesResponseTests.java index d44e92fa9d9d5..768b26f720d25 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetRolesResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetRolesResponseTests.java @@ -10,11 +10,11 @@ import org.elasticsearch.client.security.user.privileges.IndicesPrivileges; import org.elasticsearch.client.security.user.privileges.Role; +import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.EqualsHashCodeTestUtils; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.test.EqualsHashCodeTestUtils; import java.io.IOException; import java.util.ArrayList; @@ -29,38 +29,38 @@ public class GetRolesResponseTests extends ESTestCase { public void testFromXContent() throws IOException { - String json = - "{\n" + - " \"my_admin_role\": {\n" + - " \"cluster\" : [ \"all\" ],\n" + - " \"indices\" : [\n" + - " {\n" + - " \"names\" : [ \"index1\", \"index2\" ],\n" + - " \"privileges\" : [ \"all\" ],\n" + - " \"allow_restricted_indices\" : true,\n" + - " \"field_security\" : {\n" + - " \"grant\" : [ \"title\", \"body\" ]}\n" + - " }\n" + - " ],\n" + - " \"applications\" : [ ],\n" + - " \"run_as\" : [ \"other_user\" ],\n" + - " \"metadata\" : {\n" + - " \"version\" : 1\n" + - " },\n" + - " \"transient_metadata\" : {\n" + - " \"enabled\" : true\n" + - " }\n" + - " }\n" + - "}"; - final GetRolesResponse response = GetRolesResponse.fromXContent((XContentType.JSON.xContent().createParser( - new NamedXContentRegistry(Collections.emptyList()), DeprecationHandler.IGNORE_DEPRECATIONS, json))); + String json = "{\n" + + " \"my_admin_role\": {\n" + + " \"cluster\" : [ \"all\" ],\n" + + " \"indices\" : [\n" + + " {\n" + + " \"names\" : [ \"index1\", \"index2\" ],\n" + + " \"privileges\" : [ \"all\" ],\n" + + " \"allow_restricted_indices\" : true,\n" + + " \"field_security\" : {\n" + + " \"grant\" : [ \"title\", \"body\" ]}\n" + + " }\n" + + " ],\n" + + " \"applications\" : [ ],\n" + + " \"run_as\" : [ \"other_user\" ],\n" + + " \"metadata\" : {\n" + + " \"version\" : 1\n" + + " },\n" + + " \"transient_metadata\" : {\n" + + " \"enabled\" : true\n" + + " }\n" + + " }\n" + + "}"; + final GetRolesResponse response = GetRolesResponse.fromXContent( + (XContentType.JSON.xContent() + .createParser(new NamedXContentRegistry(Collections.emptyList()), DeprecationHandler.IGNORE_DEPRECATIONS, json)) + ); assertThat(response.getRoles().size(), equalTo(1)); assertThat(response.getTransientMetadataMap().size(), equalTo(1)); final Role role = response.getRoles().get(0); assertThat(role.getName(), equalTo("my_admin_role")); assertThat(role.getClusterPrivileges().size(), equalTo(1)); - IndicesPrivileges expectedIndicesPrivileges = new IndicesPrivileges.Builder() - .indices("index1", "index2") + IndicesPrivileges expectedIndicesPrivileges = new IndicesPrivileges.Builder().indices("index1", "index2") .privileges("all") .grantedFields("title", "body") .allowRestrictedIndices(true) @@ -84,8 +84,7 @@ public void testFromXContent() throws IOException { public void testEqualsHashCode() { final List roles = new ArrayList<>(); final Map> transientMetadataMap = new HashMap<>(); - IndicesPrivileges indicesPrivileges = new IndicesPrivileges.Builder() - .indices("index1", "index2") + IndicesPrivileges indicesPrivileges = new IndicesPrivileges.Builder().indices("index1", "index2") .privileges("write", "monitor", "delete") .grantedFields("field1", "field2") .deniedFields("field3", "field4") @@ -104,8 +103,7 @@ public void testEqualsHashCode() { Map transientMetadata = new HashMap<>(); transientMetadata.put("transient_key", "transient_value"); transientMetadataMap.put(role.getName(), transientMetadata); - IndicesPrivileges indicesPrivileges2 = new IndicesPrivileges.Builder() - .indices("other_index1", "other_index2") + IndicesPrivileges indicesPrivileges2 = new IndicesPrivileges.Builder().indices("other_index1", "other_index2") .privileges("write", "monitor", "delete") .grantedFields("other_field1", "other_field2") .deniedFields("other_field3", "other_field4") @@ -125,12 +123,15 @@ public void testEqualsHashCode() { transientMetadata2.put("other_transient_key", "other_transient_value"); transientMetadataMap.put(role2.getName(), transientMetadata); final GetRolesResponse getRolesResponse = new GetRolesResponse(roles, transientMetadataMap); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(getRolesResponse, (original) -> { - return new GetRolesResponse(original.getRoles(), original.getTransientMetadataMap()); - }); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(getRolesResponse, (original) -> { - return new GetRolesResponse(original.getRoles(), original.getTransientMetadataMap()); - }, GetRolesResponseTests::mutateTestItem); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + getRolesResponse, + (original) -> { return new GetRolesResponse(original.getRoles(), original.getTransientMetadataMap()); } + ); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + getRolesResponse, + (original) -> { return new GetRolesResponse(original.getRoles(), original.getTransientMetadataMap()); }, + GetRolesResponseTests::mutateTestItem + ); } @@ -138,8 +139,7 @@ private static GetRolesResponse mutateTestItem(GetRolesResponse original) { final List roles = new ArrayList<>(); final Map> transientMetadataMap = new HashMap<>(); if (randomBoolean()) { - IndicesPrivileges indicesPrivileges = new IndicesPrivileges.Builder() - .indices("index1", "index2") + IndicesPrivileges indicesPrivileges = new IndicesPrivileges.Builder().indices("index1", "index2") .privileges("write", "monitor", "delete") .grantedFields("field1", "field2") .deniedFields("field3", "field4") @@ -160,8 +160,7 @@ private static GetRolesResponse mutateTestItem(GetRolesResponse original) { transientMetadataMap.put(role.getName(), transientMetadata); return new GetRolesResponse(roles, transientMetadataMap); } else { - IndicesPrivileges indicesPrivileges = new IndicesPrivileges.Builder() - .indices("index1_changed", "index2") + IndicesPrivileges indicesPrivileges = new IndicesPrivileges.Builder().indices("index1_changed", "index2") .privileges("write", "monitor", "delete") .grantedFields("field1", "field2") .deniedFields("field3", "field4") diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetServiceAccountCredentialsRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetServiceAccountCredentialsRequestTests.java index 197afa7d47c12..75a90519de502 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetServiceAccountCredentialsRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetServiceAccountCredentialsRequestTests.java @@ -29,19 +29,25 @@ public void testEqualsHashCode() { final String serviceName = randomAlphaOfLengthBetween(3, 8); final GetServiceAccountCredentialsRequest request = new GetServiceAccountCredentialsRequest(namespace, serviceName); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(request, + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + request, original -> new GetServiceAccountCredentialsRequest(request.getNamespace(), request.getServiceName()), - this::mutateInstance); + this::mutateInstance + ); } private GetServiceAccountCredentialsRequest mutateInstance(GetServiceAccountCredentialsRequest request) { switch (randomIntBetween(0, 1)) { case 0: - return new GetServiceAccountCredentialsRequest(randomValueOtherThan(request.getNamespace(), - () -> randomAlphaOfLengthBetween(3, 8)), request.getServiceName()); + return new GetServiceAccountCredentialsRequest( + randomValueOtherThan(request.getNamespace(), () -> randomAlphaOfLengthBetween(3, 8)), + request.getServiceName() + ); default: - return new GetServiceAccountCredentialsRequest(request.getNamespace(), - randomValueOtherThan(request.getServiceName(), () -> randomAlphaOfLengthBetween(3, 8))); + return new GetServiceAccountCredentialsRequest( + request.getNamespace(), + randomValueOtherThan(request.getServiceName(), () -> randomAlphaOfLengthBetween(3, 8)) + ); } } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetServiceAccountCredentialsResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetServiceAccountCredentialsResponseTests.java index f0d02a1bbb423..5d8a629c7e92a 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetServiceAccountCredentialsResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetServiceAccountCredentialsResponseTests.java @@ -28,24 +28,30 @@ import static org.hamcrest.Matchers.equalTo; -public class GetServiceAccountCredentialsResponseTests - extends AbstractResponseTestCase { @Override protected org.elasticsearch.xpack.core.security.action.service.GetServiceAccountCredentialsResponse createServerTestInstance( - XContentType xContentType) { + XContentType xContentType + ) { final String[] fileTokenNames = randomArray(3, 5, String[]::new, () -> randomAlphaOfLengthBetween(3, 8)); final GetServiceAccountCredentialsNodesResponse nodesResponse = new GetServiceAccountCredentialsNodesResponse( new ClusterName(randomAlphaOfLength(12)), - List.of(new GetServiceAccountCredentialsNodesResponse.Node(new DiscoveryNode(randomAlphaOfLength(10), - new TransportAddress(TransportAddress.META_ADDRESS, 9300), - Version.CURRENT), fileTokenNames)), - List.of(new FailedNodeException(randomAlphaOfLength(11), "error", new NoSuchFieldError("service_tokens")))); + List.of( + new GetServiceAccountCredentialsNodesResponse.Node( + new DiscoveryNode(randomAlphaOfLength(10), new TransportAddress(TransportAddress.META_ADDRESS, 9300), Version.CURRENT), + fileTokenNames + ) + ), + List.of(new FailedNodeException(randomAlphaOfLength(11), "error", new NoSuchFieldError("service_tokens"))) + ); return new org.elasticsearch.xpack.core.security.action.service.GetServiceAccountCredentialsResponse( randomAlphaOfLengthBetween(3, 8) + "/" + randomAlphaOfLengthBetween(3, 8), randomList(0, 5, () -> TokenInfo.indexToken(randomAlphaOfLengthBetween(3, 8))), - nodesResponse); + nodesResponse + ); } @Override @@ -56,21 +62,27 @@ protected GetServiceAccountCredentialsResponse doParseToClientInstance(XContentP @Override protected void assertInstances( org.elasticsearch.xpack.core.security.action.service.GetServiceAccountCredentialsResponse serverTestInstance, - GetServiceAccountCredentialsResponse clientInstance) { + GetServiceAccountCredentialsResponse clientInstance + ) { assertThat(serverTestInstance.getPrincipal(), equalTo(clientInstance.getPrincipal())); assertThat( - Stream.concat(serverTestInstance.getIndexTokenInfos().stream(), - serverTestInstance.getNodesResponse().getFileTokenInfos().stream()) + Stream.concat( + serverTestInstance.getIndexTokenInfos().stream(), + serverTestInstance.getNodesResponse().getFileTokenInfos().stream() + ) .map(tokenInfo -> new Tuple<>(tokenInfo.getName(), tokenInfo.getSource().name().toLowerCase(Locale.ROOT))) .collect(Collectors.toSet()), - equalTo(Stream.concat(clientInstance.getIndexTokenInfos().stream(), - clientInstance.getNodesResponse().getFileTokenInfos().stream()) - .map(info -> new Tuple<>(info.getName(), info.getSource())) - .collect(Collectors.toSet()))); + equalTo( + Stream.concat(clientInstance.getIndexTokenInfos().stream(), clientInstance.getNodesResponse().getFileTokenInfos().stream()) + .map(info -> new Tuple<>(info.getName(), info.getSource())) + .collect(Collectors.toSet()) + ) + ); assertThat( serverTestInstance.getNodesResponse().failures().size(), - equalTo(clientInstance.getNodesResponse().getHeader().getFailures().size())); + equalTo(clientInstance.getNodesResponse().getHeader().getFailures().size()) + ); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetServiceAccountsRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetServiceAccountsRequestTests.java index 932e0a1a5ee1a..63d49f35297c0 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetServiceAccountsRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetServiceAccountsRequestTests.java @@ -46,19 +46,25 @@ public void testEqualsHashCode() { final String serviceName = namespace == null ? null : (randomBoolean() ? randomAlphaOfLengthBetween(3, 8) : null); final GetServiceAccountsRequest request = new GetServiceAccountsRequest(namespace, serviceName); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(request, + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + request, original -> new GetServiceAccountsRequest(request.getNamespace(), request.getServiceName()), - this::mutateInstance); + this::mutateInstance + ); } private GetServiceAccountsRequest mutateInstance(GetServiceAccountsRequest request) { switch (randomIntBetween(0, 1)) { case 0: - return new GetServiceAccountsRequest(randomValueOtherThan(request.getNamespace(), - () -> randomAlphaOfLengthBetween(3, 8)), request.getServiceName()); + return new GetServiceAccountsRequest( + randomValueOtherThan(request.getNamespace(), () -> randomAlphaOfLengthBetween(3, 8)), + request.getServiceName() + ); default: - return new GetServiceAccountsRequest(request.getNamespace(), - randomValueOtherThan(request.getServiceName(), () -> randomAlphaOfLengthBetween(3, 8))); + return new GetServiceAccountsRequest( + request.getNamespace(), + randomValueOtherThan(request.getServiceName(), () -> randomAlphaOfLengthBetween(3, 8)) + ); } } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetServiceAccountsResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetServiceAccountsResponseTests.java index 36e4cbd957c11..ce4219f491be8 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetServiceAccountsResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetServiceAccountsResponseTests.java @@ -21,20 +21,23 @@ import static org.hamcrest.Matchers.equalTo; -public class GetServiceAccountsResponseTests - extends AbstractResponseTestCase { @Override protected org.elasticsearch.xpack.core.security.action.service.GetServiceAccountResponse createServerTestInstance( - XContentType xContentType) { + XContentType xContentType + ) { final String principal = randomAlphaOfLengthBetween(3, 8) + "/" + randomAlphaOfLengthBetween(3, 8); return new org.elasticsearch.xpack.core.security.action.service.GetServiceAccountResponse( - new org.elasticsearch.xpack.core.security.action.service.ServiceAccountInfo[]{ - new org.elasticsearch.xpack.core.security.action.service.ServiceAccountInfo(principal, - new RoleDescriptor(principal, + new org.elasticsearch.xpack.core.security.action.service.ServiceAccountInfo[] { + new org.elasticsearch.xpack.core.security.action.service.ServiceAccountInfo( + principal, + new RoleDescriptor( + principal, randomArray(1, 3, String[]::new, () -> randomAlphaOfLengthBetween(3, 8)), - new RoleDescriptor.IndicesPrivileges[]{ + new RoleDescriptor.IndicesPrivileges[] { RoleDescriptor.IndicesPrivileges.builder() .indices(randomArray(1, 5, String[]::new, () -> randomAlphaOfLengthBetween(3, 8))) .privileges(randomArray(1, 3, String[]::new, () -> randomAlphaOfLengthBetween(3, 8))) @@ -42,12 +45,11 @@ protected org.elasticsearch.xpack.core.security.action.service.GetServiceAccount RoleDescriptor.IndicesPrivileges.builder() .indices(randomArray(1, 5, String[]::new, () -> randomAlphaOfLengthBetween(3, 8))) .privileges(randomArray(1, 3, String[]::new, () -> randomAlphaOfLengthBetween(3, 8))) - .build() - }, + .build() }, Strings.EMPTY_ARRAY ) - ) - }); + ) } + ); } @Override @@ -58,7 +60,8 @@ protected GetServiceAccountsResponse doParseToClientInstance(XContentParser pars @Override protected void assertInstances( org.elasticsearch.xpack.core.security.action.service.GetServiceAccountResponse serverTestInstance, - GetServiceAccountsResponse clientInstance) { + GetServiceAccountsResponse clientInstance + ) { final org.elasticsearch.xpack.core.security.action.service.ServiceAccountInfo serverTestInstanceServiceAccountInfo = serverTestInstance.getServiceAccountInfos()[0]; final String principal = serverTestInstanceServiceAccountInfo.getPrincipal(); @@ -67,21 +70,24 @@ protected void assertInstances( assertThat(clientInstance.getServiceAccountInfos().size(), equalTo(1)); final ServiceAccountInfo serviceAccountInfo = clientInstance.getServiceAccountInfos().get(0); assertThat(serviceAccountInfo.getPrincipal(), equalTo(principal)); - assertThat(serviceAccountInfo.getRole(), equalTo( - Role.builder() - .name("role_descriptor") - .clusterPrivileges(roleDescriptor.getClusterPrivileges()) - .indicesPrivileges( - IndicesPrivileges.builder() - .indices(roleDescriptor.getIndicesPrivileges()[0].getIndices()) - .privileges(roleDescriptor.getIndicesPrivileges()[0].getPrivileges()) - .build(), - IndicesPrivileges.builder() - .indices(roleDescriptor.getIndicesPrivileges()[1].getIndices()) - .privileges(roleDescriptor.getIndicesPrivileges()[1].getPrivileges()) - .build() - ) - .build() - )); + assertThat( + serviceAccountInfo.getRole(), + equalTo( + Role.builder() + .name("role_descriptor") + .clusterPrivileges(roleDescriptor.getClusterPrivileges()) + .indicesPrivileges( + IndicesPrivileges.builder() + .indices(roleDescriptor.getIndicesPrivileges()[0].getIndices()) + .privileges(roleDescriptor.getIndicesPrivileges()[0].getPrivileges()) + .build(), + IndicesPrivileges.builder() + .indices(roleDescriptor.getIndicesPrivileges()[1].getIndices()) + .privileges(roleDescriptor.getIndicesPrivileges()[1].getPrivileges()) + .build() + ) + .build() + ) + ); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetSslCertificatesResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetSslCertificatesResponseTests.java index db8a08d8ceb33..67fbd6ecb9e9d 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetSslCertificatesResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetSslCertificatesResponseTests.java @@ -9,9 +9,9 @@ package org.elasticsearch.client.security; import org.elasticsearch.client.security.support.CertificateInfo; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.EqualsHashCodeTestUtils; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.ArrayList; @@ -23,36 +23,50 @@ public class GetSslCertificatesResponseTests extends ESTestCase { public void testFromXContent() throws IOException { - xContentTester( - this::createParser, - this::createTestInstance, - this::toXContent, - GetSslCertificatesResponse::fromXContent) + xContentTester(this::createParser, this::createTestInstance, this::toXContent, GetSslCertificatesResponse::fromXContent) .supportsUnknownFields(false) .test(); } + public void testEqualsAndHashCode() { final GetSslCertificatesResponse reponse = createTestInstance(); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(reponse, this::copy, - this::mutate); + EqualsHashCodeTestUtils.checkEqualsAndHashCode(reponse, this::copy, this::mutate); } protected GetSslCertificatesResponse createTestInstance() { - final CertificateInfo info1 = new CertificateInfo("certs/elastic-certificates.p12", "PKCS12", "instance", - "CN=Elastic Certificate Tool Autogenerated CA", "a20f0ee901e8f69dc633ff633e5cd5437cdb4137", - false, "2021-01-15T20:42:49.000Z"); - final CertificateInfo info2 = new CertificateInfo("certs/elastic-certificates.p12", "PKCS12", "ca", - "CN=Elastic Certificate Tool Autogenerated CA", "a20f0ee901e8f69dc633ff633e5cd5437cdb4137", - false, "2021-01-15T20:42:49.000Z"); - final CertificateInfo info3 = new CertificateInfo("certs/elastic-certificates.p12", "PKCS12", "instance", - "CN=instance", "a20f0ee901e8f69dc633ff633e5cd5437cdb4137", - true, "2021-01-15T20:44:32.000Z"); + final CertificateInfo info1 = new CertificateInfo( + "certs/elastic-certificates.p12", + "PKCS12", + "instance", + "CN=Elastic Certificate Tool Autogenerated CA", + "a20f0ee901e8f69dc633ff633e5cd5437cdb4137", + false, + "2021-01-15T20:42:49.000Z" + ); + final CertificateInfo info2 = new CertificateInfo( + "certs/elastic-certificates.p12", + "PKCS12", + "ca", + "CN=Elastic Certificate Tool Autogenerated CA", + "a20f0ee901e8f69dc633ff633e5cd5437cdb4137", + false, + "2021-01-15T20:42:49.000Z" + ); + final CertificateInfo info3 = new CertificateInfo( + "certs/elastic-certificates.p12", + "PKCS12", + "instance", + "CN=instance", + "a20f0ee901e8f69dc633ff633e5cd5437cdb4137", + true, + "2021-01-15T20:44:32.000Z" + ); return new GetSslCertificatesResponse(Arrays.asList(info1, info2, info3)); } private void toXContent(GetSslCertificatesResponse response, XContentBuilder builder) throws IOException { builder.startArray(); - for (CertificateInfo info : response.getCertificates()){ + for (CertificateInfo info : response.getCertificates()) { builder.startObject(); builder.field(CertificateInfo.PATH.getPreferredName(), info.getPath()); builder.field(CertificateInfo.FORMAT.getPreferredName(), info.getFormat()); @@ -72,27 +86,39 @@ private GetSslCertificatesResponse copy(GetSslCertificatesResponse original) { } private GetSslCertificatesResponse mutate(GetSslCertificatesResponse original) { - final int i = randomIntBetween(1,5); + final int i = randomIntBetween(1, 5); final List infoList = new ArrayList<>(original.getCertificates()); switch (i) { case 1: infoList.remove(0); return new GetSslCertificatesResponse(infoList); case 2: - final CertificateInfo info = new CertificateInfo("certs/elastic-certificates.crt", "PEM", "instance", - "CN=instance2", "a20f0ee901e8f64t33ff633e5cd5437cdb4137", - true, "2028-01-15T20:44:32.000Z"); + final CertificateInfo info = new CertificateInfo( + "certs/elastic-certificates.crt", + "PEM", + "instance", + "CN=instance2", + "a20f0ee901e8f64t33ff633e5cd5437cdb4137", + true, + "2028-01-15T20:44:32.000Z" + ); infoList.add(info); return new GetSslCertificatesResponse(infoList); case 3: - final CertificateInfo info2 = new CertificateInfo("certs/elastic-certificates.p12", "PKCS12", "instance", - "CN=instance1", "a20f0ee901e8f69dc633ff633e5cd5437cdb4137", - true, "2021-01-15T20:44:32.000Z"); + final CertificateInfo info2 = new CertificateInfo( + "certs/elastic-certificates.p12", + "PKCS12", + "instance", + "CN=instance1", + "a20f0ee901e8f69dc633ff633e5cd5437cdb4137", + true, + "2021-01-15T20:44:32.000Z" + ); infoList.remove(2); infoList.add(info2); return new GetSslCertificatesResponse(infoList); - default: - return new GetSslCertificatesResponse(Collections.emptyList()); + default: + return new GetSslCertificatesResponse(Collections.emptyList()); } } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetUserPrivilegesResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetUserPrivilegesResponseTests.java index 7726364c8ab06..3f0fbb2c876fb 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetUserPrivilegesResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetUserPrivilegesResponseTests.java @@ -12,9 +12,9 @@ import org.elasticsearch.client.security.user.privileges.IndicesPrivileges; import org.elasticsearch.client.security.user.privileges.UserIndicesPrivileges; import org.elasticsearch.common.util.iterable.Iterables; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; import java.util.Iterator; import java.util.List; @@ -30,32 +30,32 @@ public class GetUserPrivilegesResponseTests extends ESTestCase { public void testParse() throws Exception { - String json = "{" + - "\"cluster\":[\"manage\",\"manage_security\",\"monitor\"]," + - "\"global\":[" + - " {\"application\":{\"manage\":{\"applications\":[\"test-*\"]}}}," + - " {\"application\":{\"manage\":{\"applications\":[\"apps-*\"]}}}" + - "]," + - "\"indices\":[" + - " {\"names\":[\"test-1-*\"],\"privileges\":[\"read\"],\"allow_restricted_indices\": false}," + - " {\"names\":[\"test-4-*\"],\"privileges\":[\"read\"],\"allow_restricted_indices\": true," + - " \"field_security\":[{\"grant\":[\"*\"],\"except\":[\"private-*\"]}]}," + - " {\"names\":[\"test-6-*\",\"test-7-*\"],\"privileges\":[\"read\"],\"allow_restricted_indices\": true," + - " \"query\":[\"{\\\"term\\\":{\\\"test\\\":true}}\"]}," + - " {\"names\":[\"test-2-*\"],\"privileges\":[\"read\"],\"allow_restricted_indices\": false," + - " \"field_security\":[{\"grant\":[\"*\"],\"except\":[\"secret-*\",\"private-*\"]},{\"grant\":[\"apps-*\"]}]," + - " \"query\":[\"{\\\"term\\\":{\\\"test\\\":true}}\",\"{\\\"term\\\":{\\\"apps\\\":true}}\"]}," + - " {\"names\":[\"test-3-*\",\"test-6-*\"],\"privileges\":[\"read\",\"write\"],\"allow_restricted_indices\": true}," + - " {\"names\":[\"test-3-*\",\"test-4-*\",\"test-5-*\"],\"privileges\":[\"read\"],\"allow_restricted_indices\": false," + - " \"field_security\":[{\"grant\":[\"test-*\"]}]}," + - " {\"names\":[\"test-1-*\",\"test-9-*\"],\"privileges\":[\"all\"],\"allow_restricted_indices\": true}" + - "]," + - "\"applications\":[" + - " {\"application\":\"app-dne\",\"privileges\":[\"all\"],\"resources\":[\"*\"]}," + - " {\"application\":\"test-app\",\"privileges\":[\"read\"],\"resources\":[\"object/1\",\"object/2\"]}," + - " {\"application\":\"test-app\",\"privileges\":[\"user\",\"dne\"],\"resources\":[\"*\"]}" + - "]," + - "\"run_as\":[\"app-*\",\"test-*\"]}"; + String json = "{" + + "\"cluster\":[\"manage\",\"manage_security\",\"monitor\"]," + + "\"global\":[" + + " {\"application\":{\"manage\":{\"applications\":[\"test-*\"]}}}," + + " {\"application\":{\"manage\":{\"applications\":[\"apps-*\"]}}}" + + "]," + + "\"indices\":[" + + " {\"names\":[\"test-1-*\"],\"privileges\":[\"read\"],\"allow_restricted_indices\": false}," + + " {\"names\":[\"test-4-*\"],\"privileges\":[\"read\"],\"allow_restricted_indices\": true," + + " \"field_security\":[{\"grant\":[\"*\"],\"except\":[\"private-*\"]}]}," + + " {\"names\":[\"test-6-*\",\"test-7-*\"],\"privileges\":[\"read\"],\"allow_restricted_indices\": true," + + " \"query\":[\"{\\\"term\\\":{\\\"test\\\":true}}\"]}," + + " {\"names\":[\"test-2-*\"],\"privileges\":[\"read\"],\"allow_restricted_indices\": false," + + " \"field_security\":[{\"grant\":[\"*\"],\"except\":[\"secret-*\",\"private-*\"]},{\"grant\":[\"apps-*\"]}]," + + " \"query\":[\"{\\\"term\\\":{\\\"test\\\":true}}\",\"{\\\"term\\\":{\\\"apps\\\":true}}\"]}," + + " {\"names\":[\"test-3-*\",\"test-6-*\"],\"privileges\":[\"read\",\"write\"],\"allow_restricted_indices\": true}," + + " {\"names\":[\"test-3-*\",\"test-4-*\",\"test-5-*\"],\"privileges\":[\"read\"],\"allow_restricted_indices\": false," + + " \"field_security\":[{\"grant\":[\"test-*\"]}]}," + + " {\"names\":[\"test-1-*\",\"test-9-*\"],\"privileges\":[\"all\"],\"allow_restricted_indices\": true}" + + "]," + + "\"applications\":[" + + " {\"application\":\"app-dne\",\"privileges\":[\"all\"],\"resources\":[\"*\"]}," + + " {\"application\":\"test-app\",\"privileges\":[\"read\"],\"resources\":[\"object/1\",\"object/2\"]}," + + " {\"application\":\"test-app\",\"privileges\":[\"user\",\"dne\"],\"resources\":[\"*\"]}" + + "]," + + "\"run_as\":[\"app-*\",\"test-*\"]}"; final XContentParser parser = createParser(XContentType.JSON.xContent(), json); final GetUserPrivilegesResponse response = GetUserPrivilegesResponse.fromXContent(parser); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetUsersRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetUsersRequestTests.java index ea3131acaed10..53dfd3b1c1284 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetUsersRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetUsersRequestTests.java @@ -26,12 +26,15 @@ public void testEqualsHashCode() { final String[] users = randomArray(0, 5, String[]::new, () -> randomAlphaOfLength(5)); final GetUsersRequest getUsersRequest = new GetUsersRequest(users); assertNotNull(getUsersRequest); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(getUsersRequest, (original) -> { - return new GetUsersRequest(original.getUsernames().toArray(new String[0])); - }); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(getUsersRequest, (original) -> { - return new GetUsersRequest(original.getUsernames().toArray(new String[0])); - }, GetUsersRequestTests::mutateTestItem); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + getUsersRequest, + (original) -> { return new GetUsersRequest(original.getUsernames().toArray(new String[0])); } + ); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + getUsersRequest, + (original) -> { return new GetUsersRequest(original.getUsernames().toArray(new String[0])); }, + GetUsersRequestTests::mutateTestItem + ); } private static GetUsersRequest mutateTestItem(GetUsersRequest original) { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetUsersResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetUsersResponseTests.java index 21b7ce581612b..d9afb0c7beadf 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetUsersResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetUsersResponseTests.java @@ -9,12 +9,12 @@ import org.elasticsearch.client.security.user.User; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.EqualsHashCodeTestUtils; import org.elasticsearch.test.XContentTestUtils; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.xcontent.json.JsonXContent; import java.io.IOException; import java.util.ArrayList; @@ -31,10 +31,7 @@ public class GetUsersResponseTests extends ESTestCase { public void testFromXContent() throws IOException { - xContentTester(this::createParser, - GetUsersResponseTests::createTestInstance, - this::toXContent, - GetUsersResponse::fromXContent) + xContentTester(this::createParser, GetUsersResponseTests::createTestInstance, this::toXContent, GetUsersResponse::fromXContent) .supportsUnknownFields(false) .assertToXContentEquivalence(false) .test(); @@ -53,8 +50,12 @@ private XContentBuilder toXContentUser(User user, boolean enabled, XContentBuild // This sub object should support unknown fields, but metadata cannot contain complex extra objects or it will fail Predicate excludeFilter = path -> path.equals("metadata"); - BytesReference newBytes = XContentTestUtils.insertRandomFields(XContentType.JSON, BytesReference.bytes(tempBuilder), - excludeFilter, random()); + BytesReference newBytes = XContentTestUtils.insertRandomFields( + XContentType.JSON, + BytesReference.bytes(tempBuilder), + excludeFilter, + random() + ); builder.rawValue(newBytes.streamInput(), XContentType.JSON); return builder; } @@ -83,18 +84,26 @@ private static GetUsersResponse createTestInstance() { Map metadata = new HashMap<>(); metadata.put(randomAlphaOfLengthBetween(1, 5), randomInt()); - final User user1 = new User(randomAlphaOfLength(8), - Arrays.asList(new String[] {randomAlphaOfLength(5), randomAlphaOfLength(5)}), - metadata, randomAlphaOfLength(10), null); + final User user1 = new User( + randomAlphaOfLength(8), + Arrays.asList(new String[] { randomAlphaOfLength(5), randomAlphaOfLength(5) }), + metadata, + randomAlphaOfLength(10), + null + ); users.add(user1); enabledUsers.add(user1); Map metadata2 = new HashMap<>(); metadata2.put(randomAlphaOfLengthBetween(1, 5), randomInt()); metadata2.put(randomAlphaOfLengthBetween(1, 5), randomBoolean()); - final User user2 = new User(randomAlphaOfLength(8), - Arrays.asList(new String[] {randomAlphaOfLength(5), randomAlphaOfLength(5)}), - metadata2, randomAlphaOfLength(10), null); + final User user2 = new User( + randomAlphaOfLength(8), + Arrays.asList(new String[] { randomAlphaOfLength(5), randomAlphaOfLength(5) }), + metadata2, + randomAlphaOfLength(10), + null + ); users.add(user2); return new GetUsersResponse(toMap(users), toMap(enabledUsers)); } @@ -104,26 +113,32 @@ public void testEqualsHashCode() { final List enabledUsers = new ArrayList<>(); Map metadata = new HashMap<>(); metadata.put("intelligence", 1); - final User user1 = new User("testUser1", Arrays.asList(new String[] {"admin", "other_role1"}), - metadata, "Test User 1", null); + final User user1 = new User("testUser1", Arrays.asList(new String[] { "admin", "other_role1" }), metadata, "Test User 1", null); users.add(user1); enabledUsers.add(user1); Map metadata2 = new HashMap<>(); metadata2.put("intelligence", 9); metadata2.put("specialty", "geo"); - final User user2 = new User("testUser2", Arrays.asList(new String[] {"admin"}), - metadata2, "Test User 2", "testuser2@example.com"); + final User user2 = new User( + "testUser2", + Arrays.asList(new String[] { "admin" }), + metadata2, + "Test User 2", + "testuser2@example.com" + ); users.add(user2); enabledUsers.add(user2); final GetUsersResponse getUsersResponse = new GetUsersResponse(toMap(users), toMap(enabledUsers)); assertNotNull(getUsersResponse); EqualsHashCodeTestUtils.checkEqualsAndHashCode( - getUsersResponse, - (original) -> new GetUsersResponse(toMap(original.getUsers()), toMap(original.getEnabledUsers()))); + getUsersResponse, + (original) -> new GetUsersResponse(toMap(original.getUsers()), toMap(original.getEnabledUsers())) + ); EqualsHashCodeTestUtils.checkEqualsAndHashCode( - getUsersResponse, - (original) -> new GetUsersResponse(toMap(original.getUsers()), toMap(original.getEnabledUsers())), - GetUsersResponseTests::mutateTestItem); + getUsersResponse, + (original) -> new GetUsersResponse(toMap(original.getUsers()), toMap(original.getEnabledUsers())), + GetUsersResponseTests::mutateTestItem + ); } private static GetUsersResponse mutateTestItem(GetUsersResponse original) { @@ -132,16 +147,14 @@ private static GetUsersResponse mutateTestItem(GetUsersResponse original) { final List enabledUsers = new ArrayList<>(); Map metadata = new HashMap<>(); metadata.put("intelligence", 1); - final User user1 = new User("testUser1", Arrays.asList(new String[] {"admin", "other_role1"}), - metadata, "Test User 1", null); + final User user1 = new User("testUser1", Arrays.asList(new String[] { "admin", "other_role1" }), metadata, "Test User 1", null); users.add(user1); enabledUsers.add(user1); return new GetUsersResponse(toMap(users), toMap(enabledUsers)); } Map metadata = new HashMap<>(); metadata.put("intelligence", 5); // change intelligence - final User user1 = new User("testUser1", Arrays.asList(new String[] {"admin", "other_role1"}), - metadata, "Test User 1", null); + final User user1 = new User("testUser1", Arrays.asList(new String[] { "admin", "other_role1" }), metadata, "Test User 1", null); List newUsers = new ArrayList<>(original.getUsers()); List enabledUsers = new ArrayList<>(original.getEnabledUsers()); newUsers.clear(); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GrantApiKeyRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GrantApiKeyRequestTests.java index 12d75863ead97..3fc73a71f50bf 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GrantApiKeyRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GrantApiKeyRequestTests.java @@ -14,13 +14,13 @@ import org.elasticsearch.client.security.user.privileges.Role.IndexPrivilegeName; import org.elasticsearch.common.Strings; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.EqualsHashCodeTestUtils; +import org.elasticsearch.test.XContentTestUtils; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.test.EqualsHashCodeTestUtils; -import org.elasticsearch.test.XContentTestUtils; import java.io.IOException; import java.util.List; @@ -33,43 +33,56 @@ public class GrantApiKeyRequestTests extends ESTestCase { public void testToXContent() throws IOException { final Map apiKeyMetadata = CreateApiKeyRequestTests.randomMetadata(); - final CreateApiKeyRequest createApiKeyRequest = new CreateApiKeyRequest("api-key", List.of(), null, null, - apiKeyMetadata); + final CreateApiKeyRequest createApiKeyRequest = new CreateApiKeyRequest("api-key", List.of(), null, null, apiKeyMetadata); final GrantApiKeyRequest.Grant grant = GrantApiKeyRequest.Grant.passwordGrant("kamala.khan", "JerseyGirl!".toCharArray()); final GrantApiKeyRequest grantApiKeyRequest = new GrantApiKeyRequest(grant, createApiKeyRequest); final XContentBuilder builder = XContentFactory.jsonBuilder(); grantApiKeyRequest.toXContent(builder, ToXContent.EMPTY_PARAMS); final String output = Strings.toString(builder); - final String apiKeyMetadataString = apiKeyMetadata == null ? "" + final String apiKeyMetadataString = apiKeyMetadata == null + ? "" : ",\"metadata\":" + XContentTestUtils.convertToXContent(apiKeyMetadata, XContentType.JSON).utf8ToString(); - assertThat(output, equalTo( - "{" + - "\"grant_type\":\"password\"," + - "\"username\":\"kamala.khan\"," + - "\"password\":\"JerseyGirl!\"," + - "\"api_key\":{\"name\":\"api-key\",\"role_descriptors\":{}" + apiKeyMetadataString + "}" + - "}")); + assertThat( + output, + equalTo( + "{" + + "\"grant_type\":\"password\"," + + "\"username\":\"kamala.khan\"," + + "\"password\":\"JerseyGirl!\"," + + "\"api_key\":{\"name\":\"api-key\",\"role_descriptors\":{}" + + apiKeyMetadataString + + "}" + + "}" + ) + ); } public void testEqualsHashCode() { final String name = randomAlphaOfLength(5); - List roles = randomList(1, 3, () -> - Role.builder() + List roles = randomList( + 1, + 3, + () -> Role.builder() .name(randomAlphaOfLengthBetween(3, 8)) .clusterPrivileges(randomSubsetOf(randomIntBetween(1, 3), ClusterPrivilegeName.ALL_ARRAY)) .indicesPrivileges( - IndicesPrivileges - .builder() + IndicesPrivileges.builder() .indices(randomAlphaOfLengthBetween(4, 12)) .privileges(randomSubsetOf(randomIntBetween(1, 3), IndexPrivilegeName.ALL_ARRAY)) .build() - ).build() + ) + .build() ); final TimeValue expiration = randomBoolean() ? null : TimeValue.timeValueHours(randomIntBetween(4, 100)); final RefreshPolicy refreshPolicy = randomFrom(RefreshPolicy.values()); - final CreateApiKeyRequest createApiKeyRequest = new CreateApiKeyRequest(name, roles, expiration, refreshPolicy, - CreateApiKeyRequestTests.randomMetadata()); + final CreateApiKeyRequest createApiKeyRequest = new CreateApiKeyRequest( + name, + roles, + expiration, + refreshPolicy, + CreateApiKeyRequestTests.randomMetadata() + ); final GrantApiKeyRequest.Grant grant = randomBoolean() ? GrantApiKeyRequest.Grant.passwordGrant(randomAlphaOfLength(8), randomAlphaOfLengthBetween(6, 12).toCharArray()) : GrantApiKeyRequest.Grant.accessTokenGrant(randomAlphaOfLength(24)); @@ -78,7 +91,8 @@ public void testEqualsHashCode() { EqualsHashCodeTestUtils.checkEqualsAndHashCode( grantApiKeyRequest, original -> new GrantApiKeyRequest(clone(original.getGrant()), clone(original.getApiKeyRequest())), - GrantApiKeyRequestTests::mutateTestItem); + GrantApiKeyRequestTests::mutateTestItem + ); } private GrantApiKeyRequest.Grant clone(GrantApiKeyRequest.Grant grant) { @@ -105,12 +119,15 @@ private CreateApiKeyRequest clone(CreateApiKeyRequest apiKeyRequest) { private static GrantApiKeyRequest mutateTestItem(GrantApiKeyRequest original) { switch (randomIntBetween(0, 3)) { case 0: - return new GrantApiKeyRequest(original.getGrant().getGrantType().equals("password") - ? GrantApiKeyRequest.Grant.accessTokenGrant(randomAlphaOfLength(24)) - : GrantApiKeyRequest.Grant.passwordGrant(randomAlphaOfLength(8), randomAlphaOfLengthBetween(6, 12).toCharArray()), - original.getApiKeyRequest()); + return new GrantApiKeyRequest( + original.getGrant().getGrantType().equals("password") + ? GrantApiKeyRequest.Grant.accessTokenGrant(randomAlphaOfLength(24)) + : GrantApiKeyRequest.Grant.passwordGrant(randomAlphaOfLength(8), randomAlphaOfLengthBetween(6, 12).toCharArray()), + original.getApiKeyRequest() + ); case 1: - return new GrantApiKeyRequest(original.getGrant(), + return new GrantApiKeyRequest( + original.getGrant(), new CreateApiKeyRequest( randomAlphaOfLengthBetween(10, 15), original.getApiKeyRequest().getRoles(), @@ -120,7 +137,8 @@ private static GrantApiKeyRequest mutateTestItem(GrantApiKeyRequest original) { ) ); case 2: - return new GrantApiKeyRequest(original.getGrant(), + return new GrantApiKeyRequest( + original.getGrant(), new CreateApiKeyRequest( original.getApiKeyRequest().getName(), List.of(), // No role limits @@ -130,7 +148,8 @@ private static GrantApiKeyRequest mutateTestItem(GrantApiKeyRequest original) { ) ); case 3: - return new GrantApiKeyRequest(original.getGrant(), + return new GrantApiKeyRequest( + original.getGrant(), new CreateApiKeyRequest( original.getApiKeyRequest().getName(), original.getApiKeyRequest().getRoles(), @@ -140,7 +159,8 @@ private static GrantApiKeyRequest mutateTestItem(GrantApiKeyRequest original) { ) ); default: - return new GrantApiKeyRequest(original.getGrant(), + return new GrantApiKeyRequest( + original.getGrant(), new CreateApiKeyRequest( original.getApiKeyRequest().getName(), original.getApiKeyRequest().getRoles(), diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/HasPrivilegesRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/HasPrivilegesRequestTests.java index aac0701a4207e..28fb340c64ae7 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/HasPrivilegesRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/HasPrivilegesRequestTests.java @@ -12,10 +12,10 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.util.set.Sets; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.EqualsHashCodeTestUtils; import org.elasticsearch.test.XContentTestUtils; +import org.elasticsearch.xcontent.XContentType; import org.hamcrest.Matchers; import java.io.IOException; @@ -31,60 +31,79 @@ public class HasPrivilegesRequestTests extends ESTestCase { public void testToXContent() throws IOException { final HasPrivilegesRequest request = new HasPrivilegesRequest( new LinkedHashSet<>(Arrays.asList("monitor", "manage_watcher", "manage_ml")), - new LinkedHashSet<>(Arrays.asList( - IndicesPrivileges.builder().indices("index-001", "index-002").privileges("all") - .allowRestrictedIndices(true).build(), - IndicesPrivileges.builder().indices("index-003").privileges("read") - .build() - )), - new LinkedHashSet<>(Arrays.asList( - new ApplicationResourcePrivileges("myapp", Arrays.asList("read", "write"), Arrays.asList("*")), - new ApplicationResourcePrivileges("myapp", Arrays.asList("admin"), Arrays.asList("/data/*")) - )) + new LinkedHashSet<>( + Arrays.asList( + IndicesPrivileges.builder().indices("index-001", "index-002").privileges("all").allowRestrictedIndices(true).build(), + IndicesPrivileges.builder().indices("index-003").privileges("read").build() + ) + ), + new LinkedHashSet<>( + Arrays.asList( + new ApplicationResourcePrivileges("myapp", Arrays.asList("read", "write"), Arrays.asList("*")), + new ApplicationResourcePrivileges("myapp", Arrays.asList("admin"), Arrays.asList("/data/*")) + ) + ) ); String json = Strings.toString(request); final Map parsed = XContentHelper.convertToMap(XContentType.JSON.xContent(), json, false); - final Map expected = XContentHelper.convertToMap(XContentType.JSON.xContent(), "{" + - " \"cluster\":[\"monitor\",\"manage_watcher\",\"manage_ml\"]," + - " \"index\":[{" + - " \"names\":[\"index-001\",\"index-002\"]," + - " \"privileges\":[\"all\"]," + - " \"allow_restricted_indices\":true" + - " },{" + - " \"names\":[\"index-003\"]," + - " \"privileges\":[\"read\"]," + - " \"allow_restricted_indices\":false" + - " }]," + - " \"application\":[{" + - " \"application\":\"myapp\"," + - " \"privileges\":[\"read\",\"write\"]," + - " \"resources\":[\"*\"]" + - " },{" + - " \"application\":\"myapp\"," + - " \"privileges\":[\"admin\"]," + - " \"resources\":[\"/data/*\"]" + - " }]" + - "}", false); + final Map expected = XContentHelper.convertToMap( + XContentType.JSON.xContent(), + "{" + + " \"cluster\":[\"monitor\",\"manage_watcher\",\"manage_ml\"]," + + " \"index\":[{" + + " \"names\":[\"index-001\",\"index-002\"]," + + " \"privileges\":[\"all\"]," + + " \"allow_restricted_indices\":true" + + " },{" + + " \"names\":[\"index-003\"]," + + " \"privileges\":[\"read\"]," + + " \"allow_restricted_indices\":false" + + " }]," + + " \"application\":[{" + + " \"application\":\"myapp\"," + + " \"privileges\":[\"read\",\"write\"]," + + " \"resources\":[\"*\"]" + + " },{" + + " \"application\":\"myapp\"," + + " \"privileges\":[\"admin\"]," + + " \"resources\":[\"/data/*\"]" + + " }]" + + "}", + false + ); assertThat(XContentTestUtils.differenceBetweenMapsIgnoringArrayOrder(parsed, expected), Matchers.nullValue()); } public void testEqualsAndHashCode() { final Set cluster = Sets.newHashSet(randomArray(1, 3, String[]::new, () -> randomAlphaOfLengthBetween(3, 8))); - final Set indices = Sets.newHashSet(randomArray(1, 5, IndicesPrivileges[]::new, - () -> IndicesPrivileges.builder() - .indices(generateRandomStringArray(5, 12, false, false)) - .privileges(generateRandomStringArray(3, 8, false, false)) - .allowRestrictedIndices(randomBoolean()) - .build())); + final Set indices = Sets.newHashSet( + randomArray( + 1, + 5, + IndicesPrivileges[]::new, + () -> IndicesPrivileges.builder() + .indices(generateRandomStringArray(5, 12, false, false)) + .privileges(generateRandomStringArray(3, 8, false, false)) + .allowRestrictedIndices(randomBoolean()) + .build() + ) + ); final String[] privileges = generateRandomStringArray(3, 8, false, false); final String[] resources = generateRandomStringArray(2, 6, false, false); - final Set application = Sets.newHashSet(randomArray(1, 5, ApplicationResourcePrivileges[]::new, + final Set application = Sets.newHashSet( + randomArray( + 1, + 5, + ApplicationResourcePrivileges[]::new, () -> new ApplicationResourcePrivileges( - randomAlphaOfLengthBetween(5, 12), - privileges == null ? Collections.emptyList() : List.of(privileges), - resources == null ? Collections.emptyList() : List.of(resources)))); + randomAlphaOfLengthBetween(5, 12), + privileges == null ? Collections.emptyList() : List.of(privileges), + resources == null ? Collections.emptyList() : List.of(resources) + ) + ) + ); final HasPrivilegesRequest request = new HasPrivilegesRequest(cluster, indices, application); EqualsHashCodeTestUtils.checkEqualsAndHashCode(request, this::copy, this::mutate); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/HasPrivilegesResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/HasPrivilegesResponseTests.java index 6c59e1869156c..48bcf4752a31f 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/HasPrivilegesResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/HasPrivilegesResponseTests.java @@ -8,10 +8,10 @@ package org.elasticsearch.client.security; import org.elasticsearch.common.collect.MapBuilder; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.EqualsHashCodeTestUtils; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; import org.hamcrest.Matchers; import java.io.IOException; @@ -25,50 +25,50 @@ public class HasPrivilegesResponseTests extends ESTestCase { public void testParseValidResponse() throws IOException { - String json = "{" + - " \"username\": \"namor\"," + - " \"has_all_requested\": false," + - " \"cluster\" : {" + - " \"manage\" : false," + - " \"monitor\" : true" + - " }," + - " \"index\" : {" + - " \"index-01\": {" + - " \"read\" : true," + - " \"write\" : false" + - " }," + - " \"index-02\": {" + - " \"read\" : true," + - " \"write\" : true" + - " }," + - " \"index-03\": {" + - " \"read\" : false," + - " \"write\" : false" + - " }" + - " }," + - " \"application\" : {" + - " \"app01\" : {" + - " \"/object/1\" : {" + - " \"read\" : true," + - " \"write\" : false" + - " }," + - " \"/object/2\" : {" + - " \"read\" : true," + - " \"write\" : true" + - " }" + - " }," + - " \"app02\" : {" + - " \"/object/1\" : {" + - " \"read\" : false," + - " \"write\" : false" + - " }," + - " \"/object/3\" : {" + - " \"read\" : false," + - " \"write\" : true" + - " }" + - " }" + - " }" + - "}"; + String json = "{" + + " \"username\": \"namor\"," + + " \"has_all_requested\": false," + + " \"cluster\" : {" + + " \"manage\" : false," + + " \"monitor\" : true" + + " }," + + " \"index\" : {" + + " \"index-01\": {" + + " \"read\" : true," + + " \"write\" : false" + + " }," + + " \"index-02\": {" + + " \"read\" : true," + + " \"write\" : true" + + " }," + + " \"index-03\": {" + + " \"read\" : false," + + " \"write\" : false" + + " }" + + " }," + + " \"application\" : {" + + " \"app01\" : {" + + " \"/object/1\" : {" + + " \"read\" : true," + + " \"write\" : false" + + " }," + + " \"/object/2\" : {" + + " \"read\" : true," + + " \"write\" : true" + + " }" + + " }," + + " \"app02\" : {" + + " \"/object/1\" : {" + + " \"read\" : false," + + " \"write\" : false" + + " }," + + " \"/object/3\" : {" + + " \"read\" : false," + + " \"write\" : true" + + " }" + + " }" + + " }" + + "}"; final XContentParser parser = createParser(XContentType.JSON.xContent(), json); HasPrivilegesResponse response = HasPrivilegesResponse.fromXContent(parser); @@ -166,22 +166,30 @@ public void testHasApplicationPrivilege() { assertThat(response.hasApplicationPrivilege("a2", "/action/1", "execute"), Matchers.is(true)); assertThat(response.hasApplicationPrivilege("a2", "/action/*", "execute"), Matchers.is(false)); - final IllegalArgumentException iae1 = expectThrows(IllegalArgumentException.class, - () -> response.hasApplicationPrivilege("a0", "/data/1", "read")); + final IllegalArgumentException iae1 = expectThrows( + IllegalArgumentException.class, + () -> response.hasApplicationPrivilege("a0", "/data/1", "read") + ); assertThat(iae1.getMessage().toLowerCase(Locale.ROOT), Matchers.containsString("application [a0]")); - final IllegalArgumentException iae2 = expectThrows(IllegalArgumentException.class, - () -> response.hasApplicationPrivilege("a1", "/data/0", "read")); + final IllegalArgumentException iae2 = expectThrows( + IllegalArgumentException.class, + () -> response.hasApplicationPrivilege("a1", "/data/0", "read") + ); assertThat(iae2.getMessage().toLowerCase(Locale.ROOT), Matchers.containsString("application [a1]")); assertThat(iae2.getMessage().toLowerCase(Locale.ROOT), Matchers.containsString("resource [/data/0]")); - final IllegalArgumentException iae3 = expectThrows(IllegalArgumentException.class, - () -> response.hasApplicationPrivilege("a1", "/action/1", "execute")); + final IllegalArgumentException iae3 = expectThrows( + IllegalArgumentException.class, + () -> response.hasApplicationPrivilege("a1", "/action/1", "execute") + ); assertThat(iae3.getMessage().toLowerCase(Locale.ROOT), Matchers.containsString("application [a1]")); assertThat(iae3.getMessage().toLowerCase(Locale.ROOT), Matchers.containsString("resource [/action/1]")); - final IllegalArgumentException iae4 = expectThrows(IllegalArgumentException.class, - () -> response.hasApplicationPrivilege("a1", "/data/1", "write")); + final IllegalArgumentException iae4 = expectThrows( + IllegalArgumentException.class, + () -> response.hasApplicationPrivilege("a1", "/data/1", "write") + ); assertThat(iae4.getMessage().toLowerCase(Locale.ROOT), Matchers.containsString("application [a1]")); assertThat(iae4.getMessage().toLowerCase(Locale.ROOT), Matchers.containsString("resource [/data/1]")); assertThat(iae4.getMessage().toLowerCase(Locale.ROOT), Matchers.containsString("privilege [write]")); @@ -193,30 +201,57 @@ public void testEqualsAndHashCode() { } private HasPrivilegesResponse copy(HasPrivilegesResponse response) { - return new HasPrivilegesResponse(response.getUsername(), + return new HasPrivilegesResponse( + response.getUsername(), response.hasAllRequested(), response.getClusterPrivileges(), response.getIndexPrivileges(), - response.getApplicationPrivileges()); + response.getApplicationPrivileges() + ); } private HasPrivilegesResponse mutate(HasPrivilegesResponse request) { switch (randomIntBetween(1, 5)) { case 1: - return new HasPrivilegesResponse("_" + request.getUsername(), request.hasAllRequested(), - request.getClusterPrivileges(), request.getIndexPrivileges(), request.getApplicationPrivileges()); + return new HasPrivilegesResponse( + "_" + request.getUsername(), + request.hasAllRequested(), + request.getClusterPrivileges(), + request.getIndexPrivileges(), + request.getApplicationPrivileges() + ); case 2: - return new HasPrivilegesResponse(request.getUsername(), request.hasAllRequested() == false, - request.getClusterPrivileges(), request.getIndexPrivileges(), request.getApplicationPrivileges()); + return new HasPrivilegesResponse( + request.getUsername(), + request.hasAllRequested() == false, + request.getClusterPrivileges(), + request.getIndexPrivileges(), + request.getApplicationPrivileges() + ); case 3: - return new HasPrivilegesResponse(request.getUsername(), request.hasAllRequested(), - emptyMap(), request.getIndexPrivileges(), request.getApplicationPrivileges()); + return new HasPrivilegesResponse( + request.getUsername(), + request.hasAllRequested(), + emptyMap(), + request.getIndexPrivileges(), + request.getApplicationPrivileges() + ); case 4: - return new HasPrivilegesResponse(request.getUsername(), request.hasAllRequested(), - request.getClusterPrivileges(), emptyMap(), request.getApplicationPrivileges()); + return new HasPrivilegesResponse( + request.getUsername(), + request.hasAllRequested(), + request.getClusterPrivileges(), + emptyMap(), + request.getApplicationPrivileges() + ); case 5: - return new HasPrivilegesResponse(request.getUsername(), request.hasAllRequested(), - request.getClusterPrivileges(), request.getIndexPrivileges(), emptyMap()); + return new HasPrivilegesResponse( + request.getUsername(), + request.hasAllRequested(), + request.getClusterPrivileges(), + request.getIndexPrivileges(), + emptyMap() + ); } throw new IllegalStateException("The universe is broken (or the RNG is)"); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/InvalidateApiKeyRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/InvalidateApiKeyRequestTests.java index ef8e0ef0c3964..d755ea127dde8 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/InvalidateApiKeyRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/InvalidateApiKeyRequestTests.java @@ -29,7 +29,8 @@ public void testRequestValidation() { } else { request = InvalidateApiKeyRequest.usingApiKeyIds( IntStream.range(1, randomIntBetween(2, 5)).mapToObj(ignored -> randomAlphaOfLength(5)).collect(Collectors.toList()), - randomBoolean()); + randomBoolean() + ); } Optional ve = request.validate(); assertThat(ve.isPresent(), is(false)); @@ -52,27 +53,34 @@ public void testRequestValidation() { public void testRequestValidationFailureScenarios() throws IOException { String[][] inputs = new String[][] { - { randomNullOrEmptyString(), randomNullOrEmptyString(), randomNullOrEmptyString(), randomNullOrEmptyString(), "false" }, - { randomNullOrEmptyString(), "user", "api-kid", "api-kname", "false" }, - { "realm", randomNullOrEmptyString(), "api-kid", "api-kname", "false" }, - { "realm", "user", "api-kid", randomNullOrEmptyString(), "false" }, - { randomNullOrEmptyString(), randomNullOrEmptyString(), "api-kid", "api-kname", "false" }, - { "realm", randomNullOrEmptyString(), randomNullOrEmptyString(), randomNullOrEmptyString(), "true" }, - { randomNullOrEmptyString(), "user", randomNullOrEmptyString(), randomNullOrEmptyString(), "true" } }; + { randomNullOrEmptyString(), randomNullOrEmptyString(), randomNullOrEmptyString(), randomNullOrEmptyString(), "false" }, + { randomNullOrEmptyString(), "user", "api-kid", "api-kname", "false" }, + { "realm", randomNullOrEmptyString(), "api-kid", "api-kname", "false" }, + { "realm", "user", "api-kid", randomNullOrEmptyString(), "false" }, + { randomNullOrEmptyString(), randomNullOrEmptyString(), "api-kid", "api-kname", "false" }, + { "realm", randomNullOrEmptyString(), randomNullOrEmptyString(), randomNullOrEmptyString(), "true" }, + { randomNullOrEmptyString(), "user", randomNullOrEmptyString(), randomNullOrEmptyString(), "true" } }; String[] expectedErrorMessages = new String[] { - "One of [api key id(s), api key name, username, realm name] must be specified if [owner] flag is false", - "username or realm name must not be specified when the api key id(s) or api key name is specified", - "username or realm name must not be specified when the api key id(s) or api key name is specified", - "username or realm name must not be specified when the api key id(s) or api key name is specified", - "only one of [api key id(s), api key name] can be specified", - "neither username nor realm-name may be specified when invalidating owned API keys", - "neither username nor realm-name may be specified when invalidating owned API keys" }; + "One of [api key id(s), api key name, username, realm name] must be specified if [owner] flag is false", + "username or realm name must not be specified when the api key id(s) or api key name is specified", + "username or realm name must not be specified when the api key id(s) or api key name is specified", + "username or realm name must not be specified when the api key id(s) or api key name is specified", + "only one of [api key id(s), api key name] can be specified", + "neither username nor realm-name may be specified when invalidating owned API keys", + "neither username nor realm-name may be specified when invalidating owned API keys" }; for (int i = 0; i < inputs.length; i++) { final int caseNo = i; - IllegalArgumentException ve = expectThrows(IllegalArgumentException.class, - () -> new InvalidateApiKeyRequest(inputs[caseNo][0], inputs[caseNo][1], inputs[caseNo][3], - Boolean.valueOf(inputs[caseNo][4]), apiKeyIdToIds(inputs[caseNo][2]))); + IllegalArgumentException ve = expectThrows( + IllegalArgumentException.class, + () -> new InvalidateApiKeyRequest( + inputs[caseNo][0], + inputs[caseNo][1], + inputs[caseNo][3], + Boolean.valueOf(inputs[caseNo][4]), + apiKeyIdToIds(inputs[caseNo][2]) + ) + ); assertNotNull(ve); assertThat(ve.getMessage(), equalTo(expectedErrorMessages[caseNo])); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/InvalidateApiKeyResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/InvalidateApiKeyResponseTests.java index c99e6ddeae86f..9834bd559da10 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/InvalidateApiKeyResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/InvalidateApiKeyResponseTests.java @@ -11,12 +11,12 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.EqualsHashCodeTestUtils; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.test.EqualsHashCodeTestUtils; import java.io.IOException; import java.util.Arrays; @@ -33,14 +33,21 @@ public class InvalidateApiKeyResponseTests extends ESTestCase { public void testFromXContent() throws IOException { List invalidatedApiKeys = Arrays.asList(randomArray(2, 5, String[]::new, () -> randomAlphaOfLength(5))); List previouslyInvalidatedApiKeys = Arrays.asList(randomArray(2, 3, String[]::new, () -> randomAlphaOfLength(5))); - List errors = Arrays.asList(randomArray(2, 5, ElasticsearchException[]::new, - () -> new ElasticsearchException(randomAlphaOfLength(5), new IllegalArgumentException(randomAlphaOfLength(4))))); + List errors = Arrays.asList( + randomArray( + 2, + 5, + ElasticsearchException[]::new, + () -> new ElasticsearchException(randomAlphaOfLength(5), new IllegalArgumentException(randomAlphaOfLength(4))) + ) + ); final XContentType xContentType = randomFrom(XContentType.values()); final XContentBuilder builder = XContentFactory.contentBuilder(xContentType); - builder.startObject().array("invalidated_api_keys", invalidatedApiKeys.toArray(Strings.EMPTY_ARRAY)) - .array("previously_invalidated_api_keys", previouslyInvalidatedApiKeys.toArray(Strings.EMPTY_ARRAY)) - .field("error_count", errors.size()); + builder.startObject() + .array("invalidated_api_keys", invalidatedApiKeys.toArray(Strings.EMPTY_ARRAY)) + .array("previously_invalidated_api_keys", previouslyInvalidatedApiKeys.toArray(Strings.EMPTY_ARRAY)) + .field("error_count", errors.size()); if (errors.isEmpty() == false) { builder.field("error_details"); builder.startArray(); @@ -56,8 +63,10 @@ public void testFromXContent() throws IOException { final InvalidateApiKeyResponse response = InvalidateApiKeyResponse.fromXContent(createParser(xContentType.xContent(), xContent)); assertThat(response.getInvalidatedApiKeys(), containsInAnyOrder(invalidatedApiKeys.toArray(Strings.EMPTY_ARRAY))); - assertThat(response.getPreviouslyInvalidatedApiKeys(), - containsInAnyOrder(previouslyInvalidatedApiKeys.toArray(Strings.EMPTY_ARRAY))); + assertThat( + response.getPreviouslyInvalidatedApiKeys(), + containsInAnyOrder(previouslyInvalidatedApiKeys.toArray(Strings.EMPTY_ARRAY)) + ); assertThat(response.getErrors(), is(notNullValue())); assertThat(response.getErrors().size(), is(errors.size())); assertThat(response.getErrors().get(0).getCause().toString(), containsString("type=illegal_argument_exception")); @@ -67,34 +76,65 @@ public void testFromXContent() throws IOException { public void testEqualsHashCode() { List invalidatedApiKeys = Arrays.asList(randomArray(2, 5, String[]::new, () -> randomAlphaOfLength(5))); List previouslyInvalidatedApiKeys = Arrays.asList(randomArray(2, 3, String[]::new, () -> randomAlphaOfLength(5))); - List errors = Arrays.asList(randomArray(2, 5, ElasticsearchException[]::new, - () -> new ElasticsearchException(randomAlphaOfLength(5), new IllegalArgumentException(randomAlphaOfLength(4))))); - InvalidateApiKeyResponse invalidateApiKeyResponse = new InvalidateApiKeyResponse(invalidatedApiKeys, previouslyInvalidatedApiKeys, - errors); + List errors = Arrays.asList( + randomArray( + 2, + 5, + ElasticsearchException[]::new, + () -> new ElasticsearchException(randomAlphaOfLength(5), new IllegalArgumentException(randomAlphaOfLength(4))) + ) + ); + InvalidateApiKeyResponse invalidateApiKeyResponse = new InvalidateApiKeyResponse( + invalidatedApiKeys, + previouslyInvalidatedApiKeys, + errors + ); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(invalidateApiKeyResponse, (original) -> { - return new InvalidateApiKeyResponse(original.getInvalidatedApiKeys(), original.getPreviouslyInvalidatedApiKeys(), - original.getErrors()); - }); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(invalidateApiKeyResponse, (original) -> { - return new InvalidateApiKeyResponse(original.getInvalidatedApiKeys(), original.getPreviouslyInvalidatedApiKeys(), - original.getErrors()); - }, InvalidateApiKeyResponseTests::mutateTestItem); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + invalidateApiKeyResponse, + (original) -> { + return new InvalidateApiKeyResponse( + original.getInvalidatedApiKeys(), + original.getPreviouslyInvalidatedApiKeys(), + original.getErrors() + ); + } + ); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + invalidateApiKeyResponse, + (original) -> { + return new InvalidateApiKeyResponse( + original.getInvalidatedApiKeys(), + original.getPreviouslyInvalidatedApiKeys(), + original.getErrors() + ); + }, + InvalidateApiKeyResponseTests::mutateTestItem + ); } private static InvalidateApiKeyResponse mutateTestItem(InvalidateApiKeyResponse original) { switch (randomIntBetween(0, 2)) { - case 0: - return new InvalidateApiKeyResponse(Arrays.asList(randomArray(2, 5, String[]::new, () -> randomAlphaOfLength(5))), - original.getPreviouslyInvalidatedApiKeys(), original.getErrors()); - case 1: - return new InvalidateApiKeyResponse(original.getInvalidatedApiKeys(), Collections.emptyList(), original.getErrors()); - case 2: - return new InvalidateApiKeyResponse(original.getInvalidatedApiKeys(), original.getPreviouslyInvalidatedApiKeys(), - Collections.emptyList()); - default: - return new InvalidateApiKeyResponse(Arrays.asList(randomArray(2, 5, String[]::new, () -> randomAlphaOfLength(5))), - original.getPreviouslyInvalidatedApiKeys(), original.getErrors()); + case 0: + return new InvalidateApiKeyResponse( + Arrays.asList(randomArray(2, 5, String[]::new, () -> randomAlphaOfLength(5))), + original.getPreviouslyInvalidatedApiKeys(), + original.getErrors() + ); + case 1: + return new InvalidateApiKeyResponse(original.getInvalidatedApiKeys(), Collections.emptyList(), original.getErrors()); + case 2: + return new InvalidateApiKeyResponse( + original.getInvalidatedApiKeys(), + original.getPreviouslyInvalidatedApiKeys(), + Collections.emptyList() + ); + default: + return new InvalidateApiKeyResponse( + Arrays.asList(randomArray(2, 5, String[]::new, () -> randomAlphaOfLength(5))), + original.getPreviouslyInvalidatedApiKeys(), + original.getErrors() + ); } } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/InvalidateTokenRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/InvalidateTokenRequestTests.java index ef26e1856bfa5..b0401cc007c62 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/InvalidateTokenRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/InvalidateTokenRequestTests.java @@ -66,7 +66,8 @@ public void testInvalidateUserTokensInRealm() { public void testEqualsAndHashCode() { final String token = randomAlphaOfLength(8); final boolean accessToken = randomBoolean(); - final InvalidateTokenRequest request = accessToken ? InvalidateTokenRequest.accessToken(token) + final InvalidateTokenRequest request = accessToken + ? InvalidateTokenRequest.accessToken(token) : InvalidateTokenRequest.refreshToken(token); final EqualsHashCodeTestUtils.MutateFunction mutate = r -> { int randomCase = randomIntBetween(1, 4); @@ -83,7 +84,10 @@ public void testEqualsAndHashCode() { return new InvalidateTokenRequest(null, null, randomAlphaOfLength(5), randomAlphaOfLength(5)); } }; - EqualsHashCodeTestUtils.checkEqualsAndHashCode(request, - r -> new InvalidateTokenRequest(r.getAccessToken(), r.getRefreshToken()), mutate); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + request, + r -> new InvalidateTokenRequest(r.getAccessToken(), r.getRefreshToken()), + mutate + ); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/InvalidateTokenResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/InvalidateTokenResponseTests.java index b5b9225094dad..6cb786e979262 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/InvalidateTokenResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/InvalidateTokenResponseTests.java @@ -9,12 +9,12 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; import org.hamcrest.Matchers; import java.io.IOException; @@ -56,14 +56,18 @@ public void testFromXContentWithErrors() throws IOException { .field("error_count", 0) .startArray("error_details") .startObject(); - ElasticsearchException.generateThrowableXContent(builder, ToXContent.EMPTY_PARAMS, new ElasticsearchException("foo", - new IllegalArgumentException("bar"))); + ElasticsearchException.generateThrowableXContent( + builder, + ToXContent.EMPTY_PARAMS, + new ElasticsearchException("foo", new IllegalArgumentException("bar")) + ); builder.endObject().startObject(); - ElasticsearchException.generateThrowableXContent(builder, ToXContent.EMPTY_PARAMS, new ElasticsearchException("boo", - new IllegalArgumentException("far"))); - builder.endObject() - .endArray() - .endObject(); + ElasticsearchException.generateThrowableXContent( + builder, + ToXContent.EMPTY_PARAMS, + new ElasticsearchException("boo", new IllegalArgumentException("far")) + ); + builder.endObject().endArray().endObject(); BytesReference xContent = BytesReference.bytes(builder); try (XContentParser parser = createParser(xContentType.xContent(), xContent)) { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/KibanaEnrollmentResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/KibanaEnrollmentResponseTests.java index d34cc294cf9e2..caa90d32c3957 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/KibanaEnrollmentResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/KibanaEnrollmentResponseTests.java @@ -10,11 +10,11 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.SecureString; +import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.EqualsHashCodeTestUtils; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.test.EqualsHashCodeTestUtils; import java.io.IOException; @@ -23,7 +23,7 @@ public class KibanaEnrollmentResponseTests extends ESTestCase { public void testFromXContent() throws IOException { - final String tokenName = randomAlphaOfLengthBetween(8 ,14); + final String tokenName = randomAlphaOfLengthBetween(8, 14); final String tokenValue = randomAlphaOfLengthBetween(58, 70); final String httpCa = randomAlphaOfLength(50); @@ -45,17 +45,21 @@ public void testFromXContent() throws IOException { } public void testEqualsHashCode() { - final String tokenName = randomAlphaOfLengthBetween(8 ,14); + final String tokenName = randomAlphaOfLengthBetween(8, 14); final SecureString tokenValue = new SecureString(randomAlphaOfLengthBetween(58, 70).toCharArray()); final String httpCa = randomAlphaOfLength(50); KibanaEnrollmentResponse kibanaEnrollmentResponse = new KibanaEnrollmentResponse(tokenName, tokenValue, httpCa); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(kibanaEnrollmentResponse, - (original) -> new KibanaEnrollmentResponse(original.getTokenName(), original.getTokenValue(), original.getHttpCa())); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + kibanaEnrollmentResponse, + (original) -> new KibanaEnrollmentResponse(original.getTokenName(), original.getTokenValue(), original.getHttpCa()) + ); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(kibanaEnrollmentResponse, + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + kibanaEnrollmentResponse, (original) -> new KibanaEnrollmentResponse(original.getTokenName(), original.getTokenValue(), original.getHttpCa()), - KibanaEnrollmentResponseTests::mutateTestItem); + KibanaEnrollmentResponseTests::mutateTestItem + ); } private static KibanaEnrollmentResponse mutateTestItem(KibanaEnrollmentResponse original) { @@ -73,11 +77,7 @@ private static KibanaEnrollmentResponse mutateTestItem(KibanaEnrollmentResponse randomAlphaOfLength(52) ); case 2: - return new KibanaEnrollmentResponse( - randomAlphaOfLengthBetween(14, 20), - original.getTokenValue(), - randomAlphaOfLength(52) - ); + return new KibanaEnrollmentResponse(randomAlphaOfLengthBetween(14, 20), original.getTokenValue(), randomAlphaOfLength(52)); case 3: return new KibanaEnrollmentResponse( randomAlphaOfLengthBetween(14, 20), diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/PutPrivilegesRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/PutPrivilegesRequestTests.java index 825fbb4e40bca..21acffe6fe06e 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/PutPrivilegesRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/PutPrivilegesRequestTests.java @@ -11,11 +11,11 @@ import org.elasticsearch.client.security.user.privileges.ApplicationPrivilege; import org.elasticsearch.common.Strings; import org.elasticsearch.common.util.set.Sets; +import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.EqualsHashCodeTestUtils; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.test.EqualsHashCodeTestUtils; import java.io.IOException; import java.util.ArrayList; @@ -31,79 +31,90 @@ public class PutPrivilegesRequestTests extends ESTestCase { public void testConstructor() { final List privileges = randomFrom( - Arrays.asList(Collections.singletonList(ApplicationPrivilege.builder() + Arrays.asList( + Collections.singletonList( + ApplicationPrivilege.builder() .application("app01") .privilege("all") .actions(List.of("action:login", "action:logout")) .metadata(Collections.singletonMap("k1", "v1")) - .build()), - null, Collections.emptyList())); + .build() + ), + null, + Collections.emptyList() + ) + ); final RefreshPolicy refreshPolicy = randomFrom(RefreshPolicy.values()); if (privileges == null || privileges.isEmpty()) { - final IllegalArgumentException ile = expectThrows(IllegalArgumentException.class, - () -> new PutPrivilegesRequest(privileges, refreshPolicy)); + final IllegalArgumentException ile = expectThrows( + IllegalArgumentException.class, + () -> new PutPrivilegesRequest(privileges, refreshPolicy) + ); assertThat(ile.getMessage(), equalTo("privileges are required")); } else { final PutPrivilegesRequest putPrivilegesRequest = new PutPrivilegesRequest(privileges, refreshPolicy); - assertThat(putPrivilegesRequest.getPrivileges().values().stream().flatMap(List::stream).collect(Collectors.toList()), - equalTo(privileges)); + assertThat( + putPrivilegesRequest.getPrivileges().values().stream().flatMap(List::stream).collect(Collectors.toList()), + equalTo(privileges) + ); assertThat(putPrivilegesRequest.getRefreshPolicy(), equalTo(refreshPolicy)); } } public void testToXContent() throws IOException { final String expected = "{\n" - + " \"app01\" : {\n" - + " \"all\" : {\n" - + " \"application\" : \"app01\",\n" - + " \"name\" : \"all\",\n" - + " \"actions\" : [\n" - + " \"action:login\",\n" - + " \"action:logout\"\n" - + " ],\n" - + " \"metadata\" : {\n" - + " \"k1\" : \"v1\"\n" - + " }\n" - + " },\n" - + " \"read\" : {\n" - + " \"application\" : \"app01\",\n" - + " \"name\" : \"read\",\n" - + " \"actions\" : [\n" - + " \"data:read\"\n" - + " ]\n" + " }\n" - + " },\n" - + " \"app02\" : {\n" - + " \"all\" : {\n" - + " \"application\" : \"app02\",\n" - + " \"name\" : \"all\",\n" - + " \"actions\" : [\n" - + " \"action:login\",\n" - + " \"action:logout\"\n" - + " ],\n" - + " \"metadata\" : {\n" - + " \"k2\" : \"v2\"\n" - + " }\n" - + " }\n" - + " }\n" - + "}"; + + " \"app01\" : {\n" + + " \"all\" : {\n" + + " \"application\" : \"app01\",\n" + + " \"name\" : \"all\",\n" + + " \"actions\" : [\n" + + " \"action:login\",\n" + + " \"action:logout\"\n" + + " ],\n" + + " \"metadata\" : {\n" + + " \"k1\" : \"v1\"\n" + + " }\n" + + " },\n" + + " \"read\" : {\n" + + " \"application\" : \"app01\",\n" + + " \"name\" : \"read\",\n" + + " \"actions\" : [\n" + + " \"data:read\"\n" + + " ]\n" + + " }\n" + + " },\n" + + " \"app02\" : {\n" + + " \"all\" : {\n" + + " \"application\" : \"app02\",\n" + + " \"name\" : \"all\",\n" + + " \"actions\" : [\n" + + " \"action:login\",\n" + + " \"action:logout\"\n" + + " ],\n" + + " \"metadata\" : {\n" + + " \"k2\" : \"v2\"\n" + + " }\n" + + " }\n" + + " }\n" + + "}"; List privileges = new ArrayList<>(); - privileges.add(ApplicationPrivilege.builder() + privileges.add( + ApplicationPrivilege.builder() .application("app01") .privilege("all") .actions(List.of("action:login", "action:logout")) .metadata(Collections.singletonMap("k1", "v1")) - .build()); - privileges.add(ApplicationPrivilege.builder() - .application("app01") - .privilege("read") - .actions(List.of("data:read")) - .build()); - privileges.add(ApplicationPrivilege.builder() + .build() + ); + privileges.add(ApplicationPrivilege.builder().application("app01").privilege("read").actions(List.of("data:read")).build()); + privileges.add( + ApplicationPrivilege.builder() .application("app02") .privilege("all") .actions(List.of("action:login", "action:logout")) .metadata(Collections.singletonMap("k2", "v2")) - .build()); + .build() + ); final RefreshPolicy refreshPolicy = randomFrom(RefreshPolicy.values()); final PutPrivilegesRequest putPrivilegesRequest = new PutPrivilegesRequest(privileges, refreshPolicy); final XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint(); @@ -112,27 +123,34 @@ public void testToXContent() throws IOException { public void testEqualsHashCode() { final List privileges = new ArrayList<>(); - privileges.add(ApplicationPrivilege.builder() + privileges.add( + ApplicationPrivilege.builder() .application(randomAlphaOfLength(5)) .privilege(randomAlphaOfLength(3)) .actions(List.of(randomAlphaOfLength(5), randomAlphaOfLength(5))) .metadata(Collections.singletonMap(randomAlphaOfLength(3), randomAlphaOfLength(3))) - .build()); - privileges.add(ApplicationPrivilege.builder() + .build() + ); + privileges.add( + ApplicationPrivilege.builder() .application(randomAlphaOfLength(5)) .privilege(randomAlphaOfLength(3)) .actions(List.of(randomAlphaOfLength(5), randomAlphaOfLength(5))) .metadata(Collections.singletonMap(randomAlphaOfLength(3), randomAlphaOfLength(3))) - .build()); + .build() + ); final RefreshPolicy refreshPolicy = randomFrom(RefreshPolicy.values()); PutPrivilegesRequest putPrivilegesRequest = new PutPrivilegesRequest(privileges, refreshPolicy); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + putPrivilegesRequest, + (original) -> { return new PutPrivilegesRequest(privileges, refreshPolicy); } + ); EqualsHashCodeTestUtils.checkEqualsAndHashCode(putPrivilegesRequest, (original) -> { - return new PutPrivilegesRequest(privileges, refreshPolicy); - }); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(putPrivilegesRequest, (original) -> { - return new PutPrivilegesRequest(original.getPrivileges().values().stream().flatMap(List::stream).collect(Collectors.toList()), - original.getRefreshPolicy()); + return new PutPrivilegesRequest( + original.getPrivileges().values().stream().flatMap(List::stream).collect(Collectors.toList()), + original.getRefreshPolicy() + ); }, PutPrivilegesRequestTests::mutateTestItem); } @@ -140,20 +158,26 @@ private static PutPrivilegesRequest mutateTestItem(PutPrivilegesRequest original final Set policies = Sets.newHashSet(RefreshPolicy.values()); policies.remove(original.getRefreshPolicy()); switch (randomIntBetween(0, 1)) { - case 0: - final List privileges = new ArrayList<>(); - privileges.add(ApplicationPrivilege.builder() - .application(randomAlphaOfLength(5)) - .privilege(randomAlphaOfLength(3)) - .actions(List.of(randomAlphaOfLength(6))) - .build()); - return new PutPrivilegesRequest(privileges, original.getRefreshPolicy()); - case 1: - return new PutPrivilegesRequest(original.getPrivileges().values().stream().flatMap(List::stream).collect(Collectors.toList()), - randomFrom(policies)); - default: - return new PutPrivilegesRequest(original.getPrivileges().values().stream().flatMap(List::stream).collect(Collectors.toList()), - randomFrom(policies)); + case 0: + final List privileges = new ArrayList<>(); + privileges.add( + ApplicationPrivilege.builder() + .application(randomAlphaOfLength(5)) + .privilege(randomAlphaOfLength(3)) + .actions(List.of(randomAlphaOfLength(6))) + .build() + ); + return new PutPrivilegesRequest(privileges, original.getRefreshPolicy()); + case 1: + return new PutPrivilegesRequest( + original.getPrivileges().values().stream().flatMap(List::stream).collect(Collectors.toList()), + randomFrom(policies) + ); + default: + return new PutPrivilegesRequest( + original.getPrivileges().values().stream().flatMap(List::stream).collect(Collectors.toList()), + randomFrom(policies) + ); } } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/PutPrivilegesResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/PutPrivilegesResponseTests.java index acafd6ad8fb48..3040979e4113d 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/PutPrivilegesResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/PutPrivilegesResponseTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.security; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentType; import java.io.IOException; import java.util.Arrays; @@ -21,24 +21,25 @@ public class PutPrivilegesResponseTests extends ESTestCase { public void testFromXContent() throws IOException { - final String json = "{\n" + - " \"app02\": {\n" + - " \"all\": {\n" + - " \"created\": true\n" + - " }\n" + - " },\n" + - " \"app01\": {\n" + - " \"read\": {\n" + - " \"created\": false\n" + - " },\n" + - " \"write\": {\n" + - " \"created\": true\n" + - " }\n" + - " }\n" + - "}"; + final String json = "{\n" + + " \"app02\": {\n" + + " \"all\": {\n" + + " \"created\": true\n" + + " }\n" + + " },\n" + + " \"app01\": {\n" + + " \"read\": {\n" + + " \"created\": false\n" + + " },\n" + + " \"write\": {\n" + + " \"created\": true\n" + + " }\n" + + " }\n" + + "}"; - final PutPrivilegesResponse putPrivilegesResponse = PutPrivilegesResponse - .fromXContent(createParser(XContentType.JSON.xContent(), json)); + final PutPrivilegesResponse putPrivilegesResponse = PutPrivilegesResponse.fromXContent( + createParser(XContentType.JSON.xContent(), json) + ); assertThat(putPrivilegesResponse.wasCreated("app02", "all"), is(true)); assertThat(putPrivilegesResponse.wasCreated("app01", "read"), is(false)); @@ -49,27 +50,35 @@ public void testFromXContent() throws IOException { public void testGetStatusFailsForUnknownApplicationOrPrivilegeName() { final PutPrivilegesResponse putPrivilegesResponse = new PutPrivilegesResponse( - Collections.singletonMap("app-1", Collections.singletonMap("priv", true))); + Collections.singletonMap("app-1", Collections.singletonMap("priv", true)) + ); final boolean invalidAppName = randomBoolean(); final String applicationName = (invalidAppName) ? randomAlphaOfLength(4) : "app-1"; final String privilegeName = randomAlphaOfLength(4); - final IllegalArgumentException ile = expectThrows(IllegalArgumentException.class, - () -> putPrivilegesResponse.wasCreated(applicationName, privilegeName)); + final IllegalArgumentException ile = expectThrows( + IllegalArgumentException.class, + () -> putPrivilegesResponse.wasCreated(applicationName, privilegeName) + ); assertThat(ile.getMessage(), equalTo("application name or privilege name not found in the response")); } public void testGetStatusFailsForNullOrEmptyApplicationOrPrivilegeName() { final PutPrivilegesResponse putPrivilegesResponse = new PutPrivilegesResponse( - Collections.singletonMap("app-1", Collections.singletonMap("priv", true))); + Collections.singletonMap("app-1", Collections.singletonMap("priv", true)) + ); final boolean nullOrEmptyAppName = randomBoolean(); final String applicationName = (nullOrEmptyAppName) ? randomFrom(Arrays.asList("", " ", null)) : "app-1"; final String privilegeName = randomFrom(Arrays.asList("", " ", null)); - final IllegalArgumentException ile = expectThrows(IllegalArgumentException.class, - () -> putPrivilegesResponse.wasCreated(applicationName, privilegeName)); - assertThat(ile.getMessage(), - (nullOrEmptyAppName ? equalTo("application name is required") : equalTo("privilege name is required"))); + final IllegalArgumentException ile = expectThrows( + IllegalArgumentException.class, + () -> putPrivilegesResponse.wasCreated(applicationName, privilegeName) + ); + assertThat( + ile.getMessage(), + (nullOrEmptyAppName ? equalTo("application name is required") : equalTo("privilege name is required")) + ); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/PutRoleMappingRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/PutRoleMappingRequestTests.java index 96d8555a83654..bf979dcc86553 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/PutRoleMappingRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/PutRoleMappingRequestTests.java @@ -11,11 +11,11 @@ import org.elasticsearch.client.security.support.expressiondsl.RoleMapperExpression; import org.elasticsearch.client.security.support.expressiondsl.fields.FieldRoleMapperExpression; import org.elasticsearch.common.Strings; +import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.EqualsHashCodeTestUtils; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.test.EqualsHashCodeTestUtils; import java.io.IOException; import java.util.ArrayList; @@ -39,8 +39,15 @@ public void testPutRoleMappingRequest() { metadata.put("k1", "v1"); final RefreshPolicy refreshPolicy = randomFrom(RefreshPolicy.values()); - PutRoleMappingRequest putRoleMappingRequest = new PutRoleMappingRequest(name, enabled, roles, Collections.emptyList(), rules, - metadata, refreshPolicy); + PutRoleMappingRequest putRoleMappingRequest = new PutRoleMappingRequest( + name, + enabled, + roles, + Collections.emptyList(), + rules, + metadata, + refreshPolicy + ); assertNotNull(putRoleMappingRequest); assertThat(putRoleMappingRequest.getName(), equalTo(name)); assertThat(putRoleMappingRequest.isEnabled(), equalTo(enabled)); @@ -59,23 +66,27 @@ public void testPutRoleMappingRequestThrowsExceptionForNullOrEmptyName() { metadata.put("k1", "v1"); final RefreshPolicy refreshPolicy = randomFrom(RefreshPolicy.values()); - final IllegalArgumentException ile = expectThrows(IllegalArgumentException.class, - () -> new PutRoleMappingRequest(name, enabled, roles, Collections.emptyList(), rules, metadata, refreshPolicy)); + final IllegalArgumentException ile = expectThrows( + IllegalArgumentException.class, + () -> new PutRoleMappingRequest(name, enabled, roles, Collections.emptyList(), rules, metadata, refreshPolicy) + ); assertThat(ile.getMessage(), equalTo("role-mapping name is missing")); } public void testPutRoleMappingRequestThrowsExceptionForNullRoles() { final String name = randomAlphaOfLength(5); final boolean enabled = randomBoolean(); - final List roles = null ; + final List roles = null; final List roleTemplates = Collections.emptyList(); final RoleMapperExpression rules = FieldRoleMapperExpression.ofUsername("user"); final Map metadata = new HashMap<>(); metadata.put("k1", "v1"); final RefreshPolicy refreshPolicy = randomFrom(RefreshPolicy.values()); - final RuntimeException ex = expectThrows(RuntimeException.class, - () -> new PutRoleMappingRequest(name, enabled, roles, roleTemplates, rules, metadata, refreshPolicy)); + final RuntimeException ex = expectThrows( + RuntimeException.class, + () -> new PutRoleMappingRequest(name, enabled, roles, roleTemplates, rules, metadata, refreshPolicy) + ); assertThat(ex.getMessage(), equalTo("role-mapping roles cannot be null")); } @@ -89,8 +100,10 @@ public void testPutRoleMappingRequestThrowsExceptionForEmptyRoles() { metadata.put("k1", "v1"); final RefreshPolicy refreshPolicy = randomFrom(RefreshPolicy.values()); - final RuntimeException ex = expectThrows(RuntimeException.class, - () -> new PutRoleMappingRequest(name, enabled, roles, roleTemplates, rules, metadata, refreshPolicy)); + final RuntimeException ex = expectThrows( + RuntimeException.class, + () -> new PutRoleMappingRequest(name, enabled, roles, roleTemplates, rules, metadata, refreshPolicy) + ); assertThat(ex.getMessage(), equalTo("in a role-mapping, one of roles or role_templates is required")); } @@ -103,8 +116,10 @@ public void testPutRoleMappingRequestThrowsExceptionForNullRules() { metadata.put("k1", "v1"); final RefreshPolicy refreshPolicy = randomFrom(RefreshPolicy.values()); - expectThrows(NullPointerException.class, () -> new PutRoleMappingRequest(name, enabled, roles, Collections.emptyList(), rules, - metadata, refreshPolicy)); + expectThrows( + NullPointerException.class, + () -> new PutRoleMappingRequest(name, enabled, roles, Collections.emptyList(), rules, metadata, refreshPolicy) + ); } public void testPutRoleMappingRequestToXContent() throws IOException { @@ -116,8 +131,15 @@ public void testPutRoleMappingRequestToXContent() throws IOException { metadata.put("k1", "v1"); final RefreshPolicy refreshPolicy = randomFrom(RefreshPolicy.values()); - final PutRoleMappingRequest putRoleMappingRequest = new PutRoleMappingRequest(name, enabled, roles, Collections.emptyList(), rules, - metadata, refreshPolicy); + final PutRoleMappingRequest putRoleMappingRequest = new PutRoleMappingRequest( + name, + enabled, + roles, + Collections.emptyList(), + rules, + metadata, + refreshPolicy + ); final XContentBuilder builder = XContentFactory.jsonBuilder(); putRoleMappingRequest.toXContent(builder, ToXContent.EMPTY_PARAMS); @@ -151,16 +173,23 @@ public void testPutRoleMappingRequestWithTemplateToXContent() throws IOException final String name = randomAlphaOfLength(5); final boolean enabled = randomBoolean(); final List templates = Arrays.asList( - new TemplateRoleName(Collections.singletonMap("source" , "_realm_{{realm.name}}"), TemplateRoleName.Format.STRING), - new TemplateRoleName(Collections.singletonMap("source" , "some_role"), TemplateRoleName.Format.STRING) + new TemplateRoleName(Collections.singletonMap("source", "_realm_{{realm.name}}"), TemplateRoleName.Format.STRING), + new TemplateRoleName(Collections.singletonMap("source", "some_role"), TemplateRoleName.Format.STRING) ); final RoleMapperExpression rules = FieldRoleMapperExpression.ofUsername("user"); final Map metadata = new HashMap<>(); metadata.put("k1", "v1"); final RefreshPolicy refreshPolicy = randomFrom(RefreshPolicy.values()); - final PutRoleMappingRequest putRoleMappingRequest = new PutRoleMappingRequest(name, enabled, Collections.emptyList(), templates, - rules, metadata, refreshPolicy); + final PutRoleMappingRequest putRoleMappingRequest = new PutRoleMappingRequest( + name, + enabled, + Collections.emptyList(), + templates, + rules, + metadata, + refreshPolicy + ); final XContentBuilder builder = XContentFactory.jsonBuilder(); putRoleMappingRequest.toXContent(builder, ToXContent.EMPTY_PARAMS); @@ -208,51 +237,110 @@ public void testEqualsHashCode() { } else { roles = Collections.emptyList(); templates = Arrays.asList( - randomArray(1, 3, TemplateRoleName[]::new, + randomArray( + 1, + 3, + TemplateRoleName[]::new, () -> new TemplateRoleName(randomAlphaOfLengthBetween(12, 60), randomFrom(TemplateRoleName.Format.values())) - )); + ) + ); } final RoleMapperExpression rules = FieldRoleMapperExpression.ofUsername("user"); final Map metadata = new HashMap<>(); metadata.put("k1", "v1"); final RefreshPolicy refreshPolicy = randomFrom(RefreshPolicy.values()); - PutRoleMappingRequest putRoleMappingRequest = new PutRoleMappingRequest(name, enabled, roles, templates, rules, metadata, - refreshPolicy); + PutRoleMappingRequest putRoleMappingRequest = new PutRoleMappingRequest( + name, + enabled, + roles, + templates, + rules, + metadata, + refreshPolicy + ); assertNotNull(putRoleMappingRequest); EqualsHashCodeTestUtils.checkEqualsAndHashCode(putRoleMappingRequest, (original) -> { - return new PutRoleMappingRequest(original.getName(), original.isEnabled(), original.getRoles(), original.getRoleTemplates(), - original.getRules(), original.getMetadata(), original.getRefreshPolicy()); + return new PutRoleMappingRequest( + original.getName(), + original.isEnabled(), + original.getRoles(), + original.getRoleTemplates(), + original.getRules(), + original.getMetadata(), + original.getRefreshPolicy() + ); }, PutRoleMappingRequestTests::mutateTestItem); } private static PutRoleMappingRequest mutateTestItem(PutRoleMappingRequest original) { switch (randomIntBetween(0, 5)) { - case 0: - return new PutRoleMappingRequest(randomAlphaOfLength(5), original.isEnabled(), original.getRoles(), - original.getRoleTemplates(), original.getRules(), original.getMetadata(), original.getRefreshPolicy()); - case 1: - return new PutRoleMappingRequest(original.getName(), original.isEnabled() == false, original.getRoles(), - original.getRoleTemplates(), original.getRules(), original.getMetadata(), original.getRefreshPolicy()); - case 2: - return new PutRoleMappingRequest(original.getName(), original.isEnabled(), original.getRoles(), original.getRoleTemplates(), - FieldRoleMapperExpression.ofGroups("group"), original.getMetadata(), original.getRefreshPolicy()); - case 3: - return new PutRoleMappingRequest(original.getName(), original.isEnabled(), original.getRoles(), original.getRoleTemplates(), - original.getRules(), Collections.emptyMap(), original.getRefreshPolicy()); - case 4: - return new PutRoleMappingRequest(original.getName(), original.isEnabled(), original.getRoles(), original.getRoleTemplates(), - original.getRules(), original.getMetadata(), - randomValueOtherThan(original.getRefreshPolicy(), () -> randomFrom(RefreshPolicy.values()))); - case 5: - List roles = new ArrayList<>(original.getRoles()); - roles.add(randomAlphaOfLengthBetween(3, 5)); - return new PutRoleMappingRequest(original.getName(), original.isEnabled(), roles, Collections.emptyList(), - original.getRules(), original.getMetadata(), original.getRefreshPolicy()); + case 0: + return new PutRoleMappingRequest( + randomAlphaOfLength(5), + original.isEnabled(), + original.getRoles(), + original.getRoleTemplates(), + original.getRules(), + original.getMetadata(), + original.getRefreshPolicy() + ); + case 1: + return new PutRoleMappingRequest( + original.getName(), + original.isEnabled() == false, + original.getRoles(), + original.getRoleTemplates(), + original.getRules(), + original.getMetadata(), + original.getRefreshPolicy() + ); + case 2: + return new PutRoleMappingRequest( + original.getName(), + original.isEnabled(), + original.getRoles(), + original.getRoleTemplates(), + FieldRoleMapperExpression.ofGroups("group"), + original.getMetadata(), + original.getRefreshPolicy() + ); + case 3: + return new PutRoleMappingRequest( + original.getName(), + original.isEnabled(), + original.getRoles(), + original.getRoleTemplates(), + original.getRules(), + Collections.emptyMap(), + original.getRefreshPolicy() + ); + case 4: + return new PutRoleMappingRequest( + original.getName(), + original.isEnabled(), + original.getRoles(), + original.getRoleTemplates(), + original.getRules(), + original.getMetadata(), + randomValueOtherThan(original.getRefreshPolicy(), () -> randomFrom(RefreshPolicy.values())) + ); + case 5: + List roles = new ArrayList<>(original.getRoles()); + roles.add(randomAlphaOfLengthBetween(3, 5)); + return new PutRoleMappingRequest( + original.getName(), + original.isEnabled(), + roles, + Collections.emptyList(), + original.getRules(), + original.getMetadata(), + original.getRefreshPolicy() + ); - default: - throw new IllegalStateException("Bad random value"); + default: + throw new IllegalStateException("Bad random value"); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/PutRoleRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/PutRoleRequestTests.java index 2a454a0f5fd81..0c629dee90b96 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/PutRoleRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/PutRoleRequestTests.java @@ -17,8 +17,8 @@ import org.elasticsearch.client.security.user.privileges.IndicesPrivilegesTests; import org.elasticsearch.client.security.user.privileges.Role; import org.elasticsearch.core.Tuple; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.Arrays; @@ -26,8 +26,8 @@ import java.util.Locale; import java.util.Map; -import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.empty; +import static org.hamcrest.Matchers.is; public class PutRoleRequestTests extends AbstractXContentTestCase { @@ -52,16 +52,28 @@ protected boolean supportsUnknownFields() { } private static Role randomRole(String roleName) { - final Role.Builder roleBuilder = Role.builder().name(roleName) - .clusterPrivileges(randomSubsetOf(randomInt(3), Role.ClusterPrivilegeName.ALL_ARRAY)) - .indicesPrivileges( - randomArray(3, IndicesPrivileges[]::new, () -> IndicesPrivilegesTests.createNewRandom(randomAlphaOfLength(3)))) - .applicationResourcePrivileges(randomArray(3, ApplicationResourcePrivileges[]::new, - () -> ApplicationResourcePrivilegesTests.createNewRandom(randomAlphaOfLength(3).toLowerCase(Locale.ROOT)))) - .runAsPrivilege(randomArray(3, String[]::new, () -> randomAlphaOfLength(3))); + final Role.Builder roleBuilder = Role.builder() + .name(roleName) + .clusterPrivileges(randomSubsetOf(randomInt(3), Role.ClusterPrivilegeName.ALL_ARRAY)) + .indicesPrivileges( + randomArray(3, IndicesPrivileges[]::new, () -> IndicesPrivilegesTests.createNewRandom(randomAlphaOfLength(3))) + ) + .applicationResourcePrivileges( + randomArray( + 3, + ApplicationResourcePrivileges[]::new, + () -> ApplicationResourcePrivilegesTests.createNewRandom(randomAlphaOfLength(3).toLowerCase(Locale.ROOT)) + ) + ) + .runAsPrivilege(randomArray(3, String[]::new, () -> randomAlphaOfLength(3))); if (randomBoolean()) { - roleBuilder.globalApplicationPrivileges(new GlobalPrivileges(Arrays.asList( - randomArray(1, 3, GlobalOperationPrivilege[]::new, () -> GlobalPrivilegesTests.buildRandomGlobalScopedPrivilege())))); + roleBuilder.globalApplicationPrivileges( + new GlobalPrivileges( + Arrays.asList( + randomArray(1, 3, GlobalOperationPrivilege[]::new, () -> GlobalPrivilegesTests.buildRandomGlobalScopedPrivilege()) + ) + ) + ); } if (randomBoolean()) { final Map metadata = new HashMap<>(); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/PutUserRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/PutUserRequestTests.java index b116d48cb4e3a..ebd140c473ac4 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/PutUserRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/PutUserRequestTests.java @@ -10,8 +10,8 @@ import org.elasticsearch.client.security.user.User; import org.elasticsearch.common.Strings; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentType; import java.util.Arrays; import java.util.Collections; @@ -25,8 +25,13 @@ public class PutUserRequestTests extends ESTestCase { public void testBuildRequestWithPassword() throws Exception { - final User user = new User("hawkeye", Arrays.asList("kibana_user", "avengers"), - Collections.singletonMap("status", "active"), "Clinton Barton", null); + final User user = new User( + "hawkeye", + Arrays.asList("kibana_user", "avengers"), + Collections.singletonMap("status", "active"), + "Clinton Barton", + null + ); final char[] password = "f@rmb0y".toCharArray(); final PutUserRequest request = PutUserRequest.withPassword(user, password, true, RefreshPolicy.IMMEDIATE); String json = Strings.toString(request); @@ -46,8 +51,13 @@ public void testBuildRequestWithPassword() throws Exception { } public void testBuildRequestWithPasswordHash() throws Exception { - final User user = new User("hawkeye", Arrays.asList("kibana_user", "avengers"), - Collections.singletonMap("status", "active"), "Clinton Barton", null); + final User user = new User( + "hawkeye", + Arrays.asList("kibana_user", "avengers"), + Collections.singletonMap("status", "active"), + "Clinton Barton", + null + ); final char[] passwordHash = "$2a$04$iu1G4x3ZKVDNi6egZIjkFuIPja6elQXiBF1LdRVauV4TGog6FYOpi".toCharArray(); final PutUserRequest request = PutUserRequest.withPasswordHash(user, passwordHash, true, RefreshPolicy.IMMEDIATE); String json = Strings.toString(request); @@ -67,8 +77,13 @@ public void testBuildRequestWithPasswordHash() throws Exception { } public void testBuildRequestForUpdateOnly() throws Exception { - final User user = new User("hawkeye", Arrays.asList("kibana_user", "avengers"), - Collections.singletonMap("status", "active"), "Clinton Barton", null); + final User user = new User( + "hawkeye", + Arrays.asList("kibana_user", "avengers"), + Collections.singletonMap("status", "active"), + "Clinton Barton", + null + ); final char[] passwordHash = "$2a$04$iu1G4x3ZKVDNi6egZIjkFuIPja6elQXiBF1LdRVauV4TGog6FYOpi".toCharArray(); final PutUserRequest request = PutUserRequest.updateUser(user, true, RefreshPolicy.IMMEDIATE); String json = Strings.toString(request); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/QueryApiKeyRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/QueryApiKeyRequestTests.java index b92b9f94743ca..f786902d3eb2b 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/QueryApiKeyRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/QueryApiKeyRequestTests.java @@ -42,17 +42,25 @@ public void testNewInstance() { } public void testEqualsHashCode() { - final QueryApiKeyRequest request = new QueryApiKeyRequest(randomQueryBuilder(), + final QueryApiKeyRequest request = new QueryApiKeyRequest( + randomQueryBuilder(), randomIntBetween(0, 100), randomIntBetween(0, 100), randomFieldSortBuilders(), - randomSearchAfterBuilder()); - - EqualsHashCodeTestUtils.checkEqualsAndHashCode(request, original -> new QueryApiKeyRequest(original.getQueryBuilder(), - original.getFrom(), - original.getSize(), - original.getFieldSortBuilders(), - original.getSearchAfterBuilder()), this::mutateInstance); + randomSearchAfterBuilder() + ); + + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + request, + original -> new QueryApiKeyRequest( + original.getQueryBuilder(), + original.getFrom(), + original.getSize(), + original.getFieldSortBuilders(), + original.getSearchAfterBuilder() + ), + this::mutateInstance + ); } public void testValidation() { @@ -72,35 +80,45 @@ public void testValidation() { private QueryApiKeyRequest mutateInstance(QueryApiKeyRequest request) { switch (randomIntBetween(0, 5)) { case 0: - return new QueryApiKeyRequest(randomValueOtherThan(request.getQueryBuilder(), QueryApiKeyRequestTests::randomQueryBuilder), + return new QueryApiKeyRequest( + randomValueOtherThan(request.getQueryBuilder(), QueryApiKeyRequestTests::randomQueryBuilder), request.getFrom(), request.getSize(), request.getFieldSortBuilders(), - request.getSearchAfterBuilder()); + request.getSearchAfterBuilder() + ); case 1: - return new QueryApiKeyRequest(request.getQueryBuilder(), + return new QueryApiKeyRequest( + request.getQueryBuilder(), request.getFrom() + 1, request.getSize(), request.getFieldSortBuilders(), - request.getSearchAfterBuilder()); + request.getSearchAfterBuilder() + ); case 2: - return new QueryApiKeyRequest(request.getQueryBuilder(), + return new QueryApiKeyRequest( + request.getQueryBuilder(), request.getFrom(), request.getSize() + 1, request.getFieldSortBuilders(), - request.getSearchAfterBuilder()); + request.getSearchAfterBuilder() + ); case 3: - return new QueryApiKeyRequest(request.getQueryBuilder(), + return new QueryApiKeyRequest( + request.getQueryBuilder(), request.getFrom(), request.getSize(), randomValueOtherThan(request.getFieldSortBuilders(), QueryApiKeyRequestTests::randomFieldSortBuilders), - request.getSearchAfterBuilder()); + request.getSearchAfterBuilder() + ); default: - return new QueryApiKeyRequest(request.getQueryBuilder(), + return new QueryApiKeyRequest( + request.getQueryBuilder(), request.getFrom(), request.getSize(), request.getFieldSortBuilders(), - randomValueOtherThan(request.getSearchAfterBuilder(), QueryApiKeyRequestTests::randomSearchAfterBuilder)); + randomValueOtherThan(request.getSearchAfterBuilder(), QueryApiKeyRequestTests::randomSearchAfterBuilder) + ); } } @@ -110,15 +128,19 @@ public static QueryBuilder randomQueryBuilder() { case 0: return QueryBuilders.matchAllQuery(); case 1: - return QueryBuilders.termQuery(randomAlphaOfLengthBetween(3, 8), - randomFrom(randomAlphaOfLength(8), randomInt(), randomLong(), randomDouble(), randomFloat())); + return QueryBuilders.termQuery( + randomAlphaOfLengthBetween(3, 8), + randomFrom(randomAlphaOfLength(8), randomInt(), randomLong(), randomDouble(), randomFloat()) + ); case 2: return QueryBuilders.idsQuery().addIds(randomArray(1, 5, String[]::new, () -> randomAlphaOfLength(20))); case 3: return QueryBuilders.prefixQuery(randomAlphaOfLengthBetween(3, 8), randomAlphaOfLengthBetween(3, 8)); case 4: - return QueryBuilders.wildcardQuery(randomAlphaOfLengthBetween(3, 8), - randomAlphaOfLengthBetween(0, 3) + "*" + randomAlphaOfLengthBetween(0, 3)); + return QueryBuilders.wildcardQuery( + randomAlphaOfLengthBetween(3, 8), + randomAlphaOfLengthBetween(0, 3) + "*" + randomAlphaOfLengthBetween(0, 3) + ); case 5: return QueryBuilders.rangeQuery(randomAlphaOfLengthBetween(3, 8)).from(randomNonNegativeLong()).to(randomNonNegativeLong()); default: diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/QueryApiKeyResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/QueryApiKeyResponseTests.java index 791c226971cef..0f771d2c713e6 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/QueryApiKeyResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/QueryApiKeyResponseTests.java @@ -21,20 +21,26 @@ import static org.hamcrest.Matchers.equalTo; -public class QueryApiKeyResponseTests - extends AbstractResponseTestCase { +public class QueryApiKeyResponseTests extends AbstractResponseTestCase< + org.elasticsearch.xpack.core.security.action.apikey.QueryApiKeyResponse, + QueryApiKeyResponse> { @Override protected org.elasticsearch.xpack.core.security.action.apikey.QueryApiKeyResponse createServerTestInstance(XContentType xContentType) { final int count = randomIntBetween(0, 5); final int total = randomIntBetween(count, count + 5); final int nSortValues = randomIntBetween(0, 3); - return new org.elasticsearch.xpack.core.security.action.apikey.QueryApiKeyResponse(total, + return new org.elasticsearch.xpack.core.security.action.apikey.QueryApiKeyResponse( + total, IntStream.range(0, count) - .mapToObj(i -> new org.elasticsearch.xpack.core.security.action.apikey.QueryApiKeyResponse.Item( - randomApiKeyInfo(), - randSortValues(nSortValues))) - .collect(Collectors.toUnmodifiableList())); + .mapToObj( + i -> new org.elasticsearch.xpack.core.security.action.apikey.QueryApiKeyResponse.Item( + randomApiKeyInfo(), + randSortValues(nSortValues) + ) + ) + .collect(Collectors.toUnmodifiableList()) + ); } @Override @@ -44,7 +50,9 @@ protected QueryApiKeyResponse doParseToClientInstance(XContentParser parser) thr @Override protected void assertInstances( - org.elasticsearch.xpack.core.security.action.apikey.QueryApiKeyResponse serverTestInstance, QueryApiKeyResponse clientInstance) { + org.elasticsearch.xpack.core.security.action.apikey.QueryApiKeyResponse serverTestInstance, + QueryApiKeyResponse clientInstance + ) { assertThat(serverTestInstance.getTotal(), equalTo(clientInstance.getTotal())); assertThat(serverTestInstance.getCount(), equalTo(clientInstance.getCount())); for (int i = 0; i < serverTestInstance.getItems().length; i++) { @@ -53,7 +61,9 @@ protected void assertInstances( } private void assertApiKeyInfo( - org.elasticsearch.xpack.core.security.action.apikey.QueryApiKeyResponse.Item serverItem, ApiKey clientApiKeyInfo) { + org.elasticsearch.xpack.core.security.action.apikey.QueryApiKeyResponse.Item serverItem, + ApiKey clientApiKeyInfo + ) { assertThat(serverItem.getApiKey().getId(), equalTo(clientApiKeyInfo.getId())); assertThat(serverItem.getApiKey().getName(), equalTo(clientApiKeyInfo.getName())); assertThat(serverItem.getApiKey().getUsername(), equalTo(clientApiKeyInfo.getUsername())); @@ -66,7 +76,8 @@ private void assertApiKeyInfo( private org.elasticsearch.xpack.core.security.action.ApiKey randomApiKeyInfo() { final Instant creation = Instant.now(); - return new org.elasticsearch.xpack.core.security.action.ApiKey(randomAlphaOfLengthBetween(3, 8), + return new org.elasticsearch.xpack.core.security.action.ApiKey( + randomAlphaOfLengthBetween(3, 8), randomAlphaOfLength(20), creation, randomFrom(creation.plus(randomLongBetween(1, 10), ChronoUnit.DAYS), null), @@ -79,8 +90,12 @@ private org.elasticsearch.xpack.core.security.action.ApiKey randomApiKeyInfo() { private Object[] randSortValues(int nSortValues) { if (nSortValues > 0) { - return randomArray(nSortValues, nSortValues, Object[]::new, - () -> randomFrom(randomInt(Integer.MAX_VALUE), randomAlphaOfLength(8), randomBoolean())); + return randomArray( + nSortValues, + nSortValues, + Object[]::new, + () -> randomFrom(randomInt(Integer.MAX_VALUE), randomAlphaOfLength(8), randomBoolean()) + ); } else { return null; } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/hlrc/HasPrivilegesResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/hlrc/HasPrivilegesResponseTests.java index c13c5a7ec1326..2ff8ed581e5b0 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/hlrc/HasPrivilegesResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/hlrc/HasPrivilegesResponseTests.java @@ -40,34 +40,55 @@ public class HasPrivilegesResponseTests extends AbstractResponseTestCase< public void testToXContent() throws Exception { final org.elasticsearch.xpack.core.security.action.user.HasPrivilegesResponse response = - new org.elasticsearch.xpack.core.security.action.user.HasPrivilegesResponse("daredevil", - false, Collections.singletonMap("manage", true), + new org.elasticsearch.xpack.core.security.action.user.HasPrivilegesResponse( + "daredevil", + false, + Collections.singletonMap("manage", true), Arrays.asList( - ResourcePrivileges.builder("staff") - .addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap<>()).put("read", true) - .put("index", true).put("delete", false).put("manage", false).map()) - .build(), - ResourcePrivileges.builder("customers") - .addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap<>()).put("read", true) - .put("index", true).put("delete", true).put("manage", false).map()) - .build()), - Collections.emptyMap()); + ResourcePrivileges.builder("staff") + .addPrivileges( + MapBuilder.newMapBuilder(new LinkedHashMap<>()) + .put("read", true) + .put("index", true) + .put("delete", false) + .put("manage", false) + .map() + ) + .build(), + ResourcePrivileges.builder("customers") + .addPrivileges( + MapBuilder.newMapBuilder(new LinkedHashMap<>()) + .put("read", true) + .put("index", true) + .put("delete", true) + .put("manage", false) + .map() + ) + .build() + ), + Collections.emptyMap() + ); final XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent()); response.toXContent(builder, ToXContent.EMPTY_PARAMS); BytesReference bytes = BytesReference.bytes(builder); final String json = bytes.utf8ToString(); - Assert.assertThat(json, equalTo("{" + - "\"username\":\"daredevil\"," + - "\"has_all_requested\":false," + - "\"cluster\":{\"manage\":true}," + - "\"index\":{" + - "\"customers\":{\"read\":true,\"index\":true,\"delete\":true,\"manage\":false}," + - "\"staff\":{\"read\":true,\"index\":true,\"delete\":false,\"manage\":false}" + - "}," + - "\"application\":{}" + - "}")); + Assert.assertThat( + json, + equalTo( + "{" + + "\"username\":\"daredevil\"," + + "\"has_all_requested\":false," + + "\"cluster\":{\"manage\":true}," + + "\"index\":{" + + "\"customers\":{\"read\":true,\"index\":true,\"delete\":true,\"manage\":false}," + + "\"staff\":{\"read\":true,\"index\":true,\"delete\":false,\"manage\":false}" + + "}," + + "\"application\":{}" + + "}" + ) + ); } @Override @@ -81,7 +102,8 @@ protected HasPrivilegesResponse doParseToClientInstance(XContentParser parser) t } private static List toResourcePrivileges(Map> map) { - return map.entrySet().stream() + return map.entrySet() + .stream() .map(e -> ResourcePrivileges.builder(e.getKey()).addPrivileges(e.getValue()).build()) .collect(Collectors.toList()); } @@ -94,19 +116,22 @@ private org.elasticsearch.xpack.core.security.action.user.HasPrivilegesResponse } final Collection index = randomResourcePrivileges(); final Map> application = new HashMap<>(); - for (String app : randomArray(1, 3, String[]::new, - () -> randomAlphaOfLengthBetween(3, 6).toLowerCase(Locale.ROOT))) { + for (String app : randomArray(1, 3, String[]::new, () -> randomAlphaOfLengthBetween(3, 6).toLowerCase(Locale.ROOT))) { application.put(app, randomResourcePrivileges()); } - return new org.elasticsearch.xpack.core.security.action.user.HasPrivilegesResponse(username, randomBoolean(), - cluster, index, application); + return new org.elasticsearch.xpack.core.security.action.user.HasPrivilegesResponse( + username, + randomBoolean(), + cluster, + index, + application + ); } private Collection randomResourcePrivileges() { final Collection list = new ArrayList<>(); // Use hash set to force a unique set of resources - for (String resource : Sets.newHashSet(randomArray(1, 3, String[]::new, - () -> randomAlphaOfLengthBetween(2, 6)))) { + for (String resource : Sets.newHashSet(randomArray(1, 3, String[]::new, () -> randomAlphaOfLengthBetween(2, 6)))) { final Map privileges = new HashMap<>(); for (String priv : randomArray(1, 5, String[]::new, () -> randomAlphaOfLengthBetween(3, 8))) { privileges.put(priv, randomBoolean()); @@ -117,17 +142,21 @@ private Collection randomResourcePrivileges() { } @Override - protected void assertInstances(org.elasticsearch.xpack.core.security.action.user.HasPrivilegesResponse serverTestInstance, - HasPrivilegesResponse hlrc) { + protected void assertInstances( + org.elasticsearch.xpack.core.security.action.user.HasPrivilegesResponse serverTestInstance, + HasPrivilegesResponse hlrc + ) { org.elasticsearch.xpack.core.security.action.user.HasPrivilegesResponse other = new org.elasticsearch.xpack.core.security.action.user.HasPrivilegesResponse( hlrc.getUsername(), hlrc.hasAllRequested(), hlrc.getClusterPrivileges(), toResourcePrivileges(hlrc.getIndexPrivileges()), - hlrc.getApplicationPrivileges().entrySet().stream() + hlrc.getApplicationPrivileges() + .entrySet() + .stream() .collect(Collectors.toMap(Map.Entry::getKey, e -> toResourcePrivileges(e.getValue()))) - ); + ); assertEquals(serverTestInstance, other); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/support/expressiondsl/RoleMapperExpressionDslTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/support/expressiondsl/RoleMapperExpressionDslTests.java index 26050b55291d1..93cfa21d4418c 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/support/expressiondsl/RoleMapperExpressionDslTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/support/expressiondsl/RoleMapperExpressionDslTests.java @@ -13,10 +13,10 @@ import org.elasticsearch.client.security.support.expressiondsl.expressions.ExceptRoleMapperExpression; import org.elasticsearch.client.security.support.expressiondsl.fields.FieldRoleMapperExpression; import org.elasticsearch.common.Strings; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.test.ESTestCase; import java.io.IOException; import java.util.Date; @@ -28,59 +28,63 @@ public class RoleMapperExpressionDslTests extends ESTestCase { public void testRoleMapperExpressionToXContentType() throws IOException { final RoleMapperExpression allExpression = AllRoleMapperExpression.builder() - .addExpression(AnyRoleMapperExpression.builder() - .addExpression(FieldRoleMapperExpression.ofDN("*,ou=admin,dc=example,dc=com")) - .addExpression(FieldRoleMapperExpression.ofUsername("es-admin", "es-system")) - .build()) - .addExpression(FieldRoleMapperExpression.ofGroups("cn=people,dc=example,dc=com")) - .addExpression(new ExceptRoleMapperExpression(FieldRoleMapperExpression.ofMetadata("metadata.terminated_date", new Date( - 1537145401027L)))) - .build(); + .addExpression( + AnyRoleMapperExpression.builder() + .addExpression(FieldRoleMapperExpression.ofDN("*,ou=admin,dc=example,dc=com")) + .addExpression(FieldRoleMapperExpression.ofUsername("es-admin", "es-system")) + .build() + ) + .addExpression(FieldRoleMapperExpression.ofGroups("cn=people,dc=example,dc=com")) + .addExpression( + new ExceptRoleMapperExpression(FieldRoleMapperExpression.ofMetadata("metadata.terminated_date", new Date(1537145401027L))) + ) + .build(); final XContentBuilder builder = XContentFactory.jsonBuilder(); allExpression.toXContent(builder, ToXContent.EMPTY_PARAMS); final String output = Strings.toString(builder); - final String expected = - "{"+ - "\"all\":["+ - "{"+ - "\"any\":["+ - "{"+ - "\"field\":{"+ - "\"dn\":[\"*,ou=admin,dc=example,dc=com\"]"+ - "}"+ - "},"+ - "{"+ - "\"field\":{"+ - "\"username\":["+ - "\"es-admin\","+ - "\"es-system\""+ - "]"+ - "}"+ - "}"+ - "]"+ - "},"+ - "{"+ - "\"field\":{"+ - "\"groups\":[\"cn=people,dc=example,dc=com\"]"+ - "}"+ - "},"+ - "{"+ - "\"except\":{"+ - "\"field\":{"+ - "\"metadata.terminated_date\":[\"2018-09-17T00:50:01.027Z\"]"+ - "}"+ - "}"+ - "}"+ - "]"+ - "}"; + final String expected = "{" + + "\"all\":[" + + "{" + + "\"any\":[" + + "{" + + "\"field\":{" + + "\"dn\":[\"*,ou=admin,dc=example,dc=com\"]" + + "}" + + "}," + + "{" + + "\"field\":{" + + "\"username\":[" + + "\"es-admin\"," + + "\"es-system\"" + + "]" + + "}" + + "}" + + "]" + + "}," + + "{" + + "\"field\":{" + + "\"groups\":[\"cn=people,dc=example,dc=com\"]" + + "}" + + "}," + + "{" + + "\"except\":{" + + "\"field\":{" + + "\"metadata.terminated_date\":[\"2018-09-17T00:50:01.027Z\"]" + + "}" + + "}" + + "}" + + "]" + + "}"; assertThat(output, equalTo(expected)); } public void testFieldRoleMapperExpressionThrowsExceptionForMissingMetadataPrefix() { - final IllegalArgumentException ile = expectThrows(IllegalArgumentException.class, () -> FieldRoleMapperExpression.ofMetadata( - "terminated_date", new Date(1537145401027L))); + final IllegalArgumentException ile = expectThrows( + IllegalArgumentException.class, + () -> FieldRoleMapperExpression.ofMetadata("terminated_date", new Date(1537145401027L)) + ); assertThat(ile.getMessage(), equalTo("metadata key must have prefix 'metadata.'")); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/support/expressiondsl/parser/RoleMapperExpressionParserTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/support/expressiondsl/parser/RoleMapperExpressionParserTests.java index 51585e54ca8a8..a9a3bdbef6322 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/support/expressiondsl/parser/RoleMapperExpressionParserTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/support/expressiondsl/parser/RoleMapperExpressionParserTests.java @@ -12,13 +12,13 @@ import org.elasticsearch.client.security.support.expressiondsl.expressions.CompositeRoleMapperExpression; import org.elasticsearch.client.security.support.expressiondsl.fields.FieldRoleMapperExpression; import org.elasticsearch.common.Strings; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; import java.io.IOException; import java.util.Collections; @@ -40,16 +40,16 @@ public void testParseSimpleFieldExpression() throws Exception { } public void testParseComplexExpression() throws Exception { - String json = "{ \"any\": [" + - " { \"field\": { \"username\" : \"*@shield.gov\" } }, " + - " { \"all\": [" + - " { \"field\": { \"username\" : \"/.*\\\\@avengers\\\\.(net|org)/\" } }, " + - " { \"field\": { \"groups\" : [ \"admin\", \"operators\" ] } }, " + - " { \"except\":" + - " { \"field\": { \"groups\" : \"disavowed\" } }" + - " }" + - " ] }" + - "] }"; + String json = "{ \"any\": [" + + " { \"field\": { \"username\" : \"*@shield.gov\" } }, " + + " { \"all\": [" + + " { \"field\": { \"username\" : \"/.*\\\\@avengers\\\\.(net|org)/\" } }, " + + " { \"field\": { \"groups\" : [ \"admin\", \"operators\" ] } }, " + + " { \"except\":" + + " { \"field\": { \"groups\" : \"disavowed\" } }" + + " }" + + " ] }" + + "] }"; final RoleMapperExpression expr = parse(json); assertThat(expr, instanceOf(CompositeRoleMapperExpression.class)); @@ -57,33 +57,27 @@ public void testParseComplexExpression() throws Exception { assertThat(any.getElements(), iterableWithSize(2)); - final FieldRoleMapperExpression fieldShield = checkExpressionType(any.getElements().get(0), - FieldRoleMapperExpression.class); + final FieldRoleMapperExpression fieldShield = checkExpressionType(any.getElements().get(0), FieldRoleMapperExpression.class); assertThat(fieldShield.getField(), equalTo("username")); assertThat(fieldShield.getValues(), iterableWithSize(1)); assertThat(fieldShield.getValues().get(0), equalTo("*@shield.gov")); - final CompositeRoleMapperExpression all = checkExpressionType(any.getElements().get(1), - CompositeRoleMapperExpression.class); + final CompositeRoleMapperExpression all = checkExpressionType(any.getElements().get(1), CompositeRoleMapperExpression.class); assertThat(all.getElements(), iterableWithSize(3)); - final FieldRoleMapperExpression fieldAvengers = checkExpressionType(all.getElements().get(0), - FieldRoleMapperExpression.class); + final FieldRoleMapperExpression fieldAvengers = checkExpressionType(all.getElements().get(0), FieldRoleMapperExpression.class); assertThat(fieldAvengers.getField(), equalTo("username")); assertThat(fieldAvengers.getValues(), iterableWithSize(1)); assertThat(fieldAvengers.getValues().get(0), equalTo("/.*\\@avengers\\.(net|org)/")); - final FieldRoleMapperExpression fieldGroupsAdmin = checkExpressionType(all.getElements().get(1), - FieldRoleMapperExpression.class); + final FieldRoleMapperExpression fieldGroupsAdmin = checkExpressionType(all.getElements().get(1), FieldRoleMapperExpression.class); assertThat(fieldGroupsAdmin.getField(), equalTo("groups")); assertThat(fieldGroupsAdmin.getValues(), iterableWithSize(2)); assertThat(fieldGroupsAdmin.getValues().get(0), equalTo("admin")); assertThat(fieldGroupsAdmin.getValues().get(1), equalTo("operators")); - final CompositeRoleMapperExpression except = checkExpressionType(all.getElements().get(2), - CompositeRoleMapperExpression.class); - final FieldRoleMapperExpression fieldDisavowed = checkExpressionType(except.getElements().get(0), - FieldRoleMapperExpression.class); + final CompositeRoleMapperExpression except = checkExpressionType(all.getElements().get(2), CompositeRoleMapperExpression.class); + final FieldRoleMapperExpression fieldDisavowed = checkExpressionType(except.getElements().get(0), FieldRoleMapperExpression.class); assertThat(fieldDisavowed.getField(), equalTo("groups")); assertThat(fieldDisavowed.getValues(), iterableWithSize(1)); assertThat(fieldDisavowed.getValues().get(0), equalTo("disavowed")); @@ -103,8 +97,11 @@ private T checkExpressionType(RoleMapperExpression expr, Class type) { } private RoleMapperExpression parse(String json) throws IOException { - return new RoleMapperExpressionParser().parse("rules", XContentType.JSON.xContent().createParser(new NamedXContentRegistry( - Collections.emptyList()), DeprecationHandler.IGNORE_DEPRECATIONS, json)); + return new RoleMapperExpressionParser().parse( + "rules", + XContentType.JSON.xContent() + .createParser(new NamedXContentRegistry(Collections.emptyList()), DeprecationHandler.IGNORE_DEPRECATIONS, json) + ); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/user/privileges/ApplicationPrivilegeTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/user/privileges/ApplicationPrivilegeTests.java index d7fe19edba4ac..a1053f4dc8a5c 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/user/privileges/ApplicationPrivilegeTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/user/privileges/ApplicationPrivilegeTests.java @@ -9,13 +9,13 @@ package org.elasticsearch.client.security.user.privileges; import org.elasticsearch.common.Strings; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; import java.io.IOException; import java.util.Arrays; @@ -30,24 +30,29 @@ public class ApplicationPrivilegeTests extends ESTestCase { public void testFromXContentAndToXContent() throws IOException { - String json = - "{\n" - + " \"application\" : \"myapp\",\n" - + " \"name\" : \"read\",\n" - + " \"actions\" : [\n" - + " \"data:read/*\",\n" - + " \"action:login\"\n" - + " ],\n" - + " \"metadata\" : {\n" - + " \"description\" : \"Read access to myapp\"\n" - + " }\n" - + "}"; - final ApplicationPrivilege privilege = ApplicationPrivilege.fromXContent(XContentType.JSON.xContent().createParser( - new NamedXContentRegistry(Collections.emptyList()), DeprecationHandler.IGNORE_DEPRECATIONS, json)); + String json = "{\n" + + " \"application\" : \"myapp\",\n" + + " \"name\" : \"read\",\n" + + " \"actions\" : [\n" + + " \"data:read/*\",\n" + + " \"action:login\"\n" + + " ],\n" + + " \"metadata\" : {\n" + + " \"description\" : \"Read access to myapp\"\n" + + " }\n" + + "}"; + final ApplicationPrivilege privilege = ApplicationPrivilege.fromXContent( + XContentType.JSON.xContent() + .createParser(new NamedXContentRegistry(Collections.emptyList()), DeprecationHandler.IGNORE_DEPRECATIONS, json) + ); final Map metadata = new HashMap<>(); metadata.put("description", "Read access to myapp"); - final ApplicationPrivilege expectedPrivilege = - new ApplicationPrivilege("myapp", "read", Arrays.asList("data:read/*", "action:login"), metadata); + final ApplicationPrivilege expectedPrivilege = new ApplicationPrivilege( + "myapp", + "read", + Arrays.asList("data:read/*", "action:login"), + metadata + ); assertThat(privilege, equalTo(expectedPrivilege)); XContentBuilder builder = privilege.toXContent(XContentFactory.jsonBuilder().prettyPrint(), ToXContent.EMPTY_PARAMS); @@ -59,8 +64,10 @@ public void testEmptyApplicationName() { final Map metadata = new HashMap<>(); metadata.put("description", "Read access to myapp"); final String applicationName = randomBoolean() ? null : ""; - final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> - new ApplicationPrivilege(applicationName, "read", Arrays.asList("data:read/*", "action:login"), metadata)); + final IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> new ApplicationPrivilege(applicationName, "read", Arrays.asList("data:read/*", "action:login"), metadata) + ); assertThat(e.getMessage(), equalTo("application name must be provided")); } @@ -68,8 +75,10 @@ public void testEmptyPrivilegeName() { final Map metadata = new HashMap<>(); metadata.put("description", "Read access to myapp"); final String privilegenName = randomBoolean() ? null : ""; - final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> - new ApplicationPrivilege("myapp", privilegenName, Arrays.asList("data:read/*", "action:login"), metadata)); + final IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> new ApplicationPrivilege("myapp", privilegenName, Arrays.asList("data:read/*", "action:login"), metadata) + ); assertThat(e.getMessage(), equalTo("privilege name must be provided")); } @@ -77,8 +86,10 @@ public void testEmptyActions() { final Map metadata = new HashMap<>(); metadata.put("description", "Read access to myapp"); final List actions = randomBoolean() ? null : Collections.emptyList(); - final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> - new ApplicationPrivilege("myapp", "read", actions, metadata)); + final IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> new ApplicationPrivilege("myapp", "read", actions, metadata) + ); assertThat(e.getMessage(), equalTo("actions must be provided")); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/user/privileges/ApplicationResourcePrivilegesTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/user/privileges/ApplicationResourcePrivilegesTests.java index 5f2e02cf5978b..0137677940781 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/user/privileges/ApplicationResourcePrivilegesTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/user/privileges/ApplicationResourcePrivilegesTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.security.user.privileges; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.Arrays; @@ -21,9 +21,11 @@ public class ApplicationResourcePrivilegesTests extends AbstractXContentTestCase { public static ApplicationResourcePrivileges createNewRandom(String name) { - return new ApplicationResourcePrivileges(name, - Arrays.asList(randomArray(1, 8, size -> new String[size], () -> randomAlphaOfLengthBetween(1, 8))), - Arrays.asList(randomArray(1, 8, size -> new String[size], () -> randomAlphaOfLengthBetween(1, 8)))); + return new ApplicationResourcePrivileges( + name, + Arrays.asList(randomArray(1, 8, size -> new String[size], () -> randomAlphaOfLengthBetween(1, 8))), + Arrays.asList(randomArray(1, 8, size -> new String[size], () -> randomAlphaOfLengthBetween(1, 8))) + ); } @Override @@ -43,28 +45,40 @@ protected boolean supportsUnknownFields() { public void testEmptyApplicationName() { final String emptyApplicationName = randomBoolean() ? "" : null; - final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> new ApplicationResourcePrivileges(emptyApplicationName, - Arrays.asList(randomArray(1, 8, size -> new String[size], () -> randomAlphaOfLengthBetween(1, 8))), - Arrays.asList(randomArray(1, 8, size -> new String[size], () -> randomAlphaOfLengthBetween(1, 8))))); + final IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> new ApplicationResourcePrivileges( + emptyApplicationName, + Arrays.asList(randomArray(1, 8, size -> new String[size], () -> randomAlphaOfLengthBetween(1, 8))), + Arrays.asList(randomArray(1, 8, size -> new String[size], () -> randomAlphaOfLengthBetween(1, 8))) + ) + ); assertThat(e.getMessage(), is("application privileges must have an application name")); } public void testEmptyPrivileges() { final List emptyPrivileges = randomBoolean() ? Collections.emptyList() : null; - final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> new ApplicationResourcePrivileges(randomAlphaOfLengthBetween(1, 8), - emptyPrivileges, - Arrays.asList(randomArray(1, 8, size -> new String[size], () -> randomAlphaOfLengthBetween(1, 8))))); + final IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> new ApplicationResourcePrivileges( + randomAlphaOfLengthBetween(1, 8), + emptyPrivileges, + Arrays.asList(randomArray(1, 8, size -> new String[size], () -> randomAlphaOfLengthBetween(1, 8))) + ) + ); assertThat(e.getMessage(), is("application privileges must define at least one privilege")); } public void testEmptyResources() { final List emptyResources = randomBoolean() ? Collections.emptyList() : null; - final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> new ApplicationResourcePrivileges(randomAlphaOfLengthBetween(1, 8), - Arrays.asList(randomArray(1, 8, size -> new String[size], () -> randomAlphaOfLengthBetween(1, 8))), - emptyResources)); + final IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> new ApplicationResourcePrivileges( + randomAlphaOfLengthBetween(1, 8), + Arrays.asList(randomArray(1, 8, size -> new String[size], () -> randomAlphaOfLengthBetween(1, 8))), + emptyResources + ) + ); assertThat(e.getMessage(), is("application privileges must refer to at least one resource")); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/user/privileges/GlobalOperationPrivilegeTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/user/privileges/GlobalOperationPrivilegeTests.java index 1298531b99b8c..5a6f05ebad062 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/user/privileges/GlobalOperationPrivilegeTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/user/privileges/GlobalOperationPrivilegeTests.java @@ -32,11 +32,12 @@ public void testConstructor() { assertThat(globalOperationPrivilege.getRaw(), equalTo(privilege)); } else { if (category == null || operation == null) { - expectThrows(NullPointerException.class, - () -> new GlobalOperationPrivilege(category, operation, privilege)); + expectThrows(NullPointerException.class, () -> new GlobalOperationPrivilege(category, operation, privilege)); } else { - final IllegalArgumentException ile = expectThrows(IllegalArgumentException.class, - () -> new GlobalOperationPrivilege(category, operation, privilege)); + final IllegalArgumentException ile = expectThrows( + IllegalArgumentException.class, + () -> new GlobalOperationPrivilege(category, operation, privilege) + ); assertThat(ile.getMessage(), equalTo("privileges cannot be empty or null")); } } @@ -48,25 +49,31 @@ public void testEqualsHashCode() { final Map privilege = Collections.singletonMap(randomAlphaOfLength(4), randomAlphaOfLength(5)); GlobalOperationPrivilege globalOperationPrivilege = new GlobalOperationPrivilege(category, operation, privilege); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(globalOperationPrivilege, (original) -> { - return new GlobalOperationPrivilege(original.getCategory(), original.getOperation(), original.getRaw()); - }); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(globalOperationPrivilege, (original) -> { - return new GlobalOperationPrivilege(original.getCategory(), original.getOperation(), original.getRaw()); - }, GlobalOperationPrivilegeTests::mutateTestItem); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + globalOperationPrivilege, + (original) -> { return new GlobalOperationPrivilege(original.getCategory(), original.getOperation(), original.getRaw()); } + ); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + globalOperationPrivilege, + (original) -> { return new GlobalOperationPrivilege(original.getCategory(), original.getOperation(), original.getRaw()); }, + GlobalOperationPrivilegeTests::mutateTestItem + ); } private static GlobalOperationPrivilege mutateTestItem(GlobalOperationPrivilege original) { switch (randomIntBetween(0, 2)) { - case 0: - return new GlobalOperationPrivilege(randomAlphaOfLength(5), original.getOperation(), original.getRaw()); - case 1: - return new GlobalOperationPrivilege(original.getCategory(), randomAlphaOfLength(5), original.getRaw()); - case 2: - return new GlobalOperationPrivilege(original.getCategory(), original.getOperation(), - Collections.singletonMap(randomAlphaOfLength(4), randomAlphaOfLength(4))); - default: - return new GlobalOperationPrivilege(randomAlphaOfLength(5), original.getOperation(), original.getRaw()); + case 0: + return new GlobalOperationPrivilege(randomAlphaOfLength(5), original.getOperation(), original.getRaw()); + case 1: + return new GlobalOperationPrivilege(original.getCategory(), randomAlphaOfLength(5), original.getRaw()); + case 2: + return new GlobalOperationPrivilege( + original.getCategory(), + original.getOperation(), + Collections.singletonMap(randomAlphaOfLength(4), randomAlphaOfLength(4)) + ); + default: + return new GlobalOperationPrivilege(randomAlphaOfLength(5), original.getOperation(), original.getRaw()); } } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/user/privileges/GlobalPrivilegesTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/user/privileges/GlobalPrivilegesTests.java index b0c75d93d9fc8..a91c36cb2c724 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/user/privileges/GlobalPrivilegesTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/user/privileges/GlobalPrivilegesTests.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.security.user.privileges; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.test.EqualsHashCodeTestUtils; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.Arrays; @@ -46,8 +46,9 @@ public static GlobalOperationPrivilege buildRandomGlobalScopedPrivilege() { @Override protected GlobalPrivileges createTestInstance() { - final List privilegeList = Arrays - .asList(randomArray(1, 4, size -> new GlobalOperationPrivilege[size], () -> buildRandomGlobalScopedPrivilege())); + final List privilegeList = Arrays.asList( + randomArray(1, 4, size -> new GlobalOperationPrivilege[size], () -> buildRandomGlobalScopedPrivilege()) + ); return new GlobalPrivileges(privilegeList); } @@ -63,8 +64,10 @@ protected boolean supportsUnknownFields() { public void testEmptyOrNullGlobalOperationPrivilege() { final Map privilege = randomBoolean() ? null : Collections.emptyMap(); - final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> new GlobalOperationPrivilege(randomAlphaOfLength(2), randomAlphaOfLength(2), privilege)); + final IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> new GlobalOperationPrivilege(randomAlphaOfLength(2), randomAlphaOfLength(2), privilege) + ); assertThat(e.getMessage(), is("privileges cannot be empty or null")); } @@ -77,8 +80,11 @@ public void testEmptyOrNullGlobalPrivileges() { public void testDuplicateGlobalOperationPrivilege() { final GlobalOperationPrivilege privilege = buildRandomGlobalScopedPrivilege(); // duplicate - final GlobalOperationPrivilege privilege2 = new GlobalOperationPrivilege(privilege.getCategory(), privilege.getOperation(), - new HashMap<>(privilege.getRaw())); + final GlobalOperationPrivilege privilege2 = new GlobalOperationPrivilege( + privilege.getCategory(), + privilege.getOperation(), + new HashMap<>(privilege.getRaw()) + ); final GlobalPrivileges globalPrivilege = new GlobalPrivileges(Arrays.asList(privilege, privilege2)); assertThat(globalPrivilege.getPrivileges().size(), is(1)); assertThat(globalPrivilege.getPrivileges().iterator().next(), is(privilege)); @@ -86,27 +92,37 @@ public void testDuplicateGlobalOperationPrivilege() { public void testSameScopeGlobalOperationPrivilege() { final GlobalOperationPrivilege privilege = buildRandomGlobalScopedPrivilege(); - final GlobalOperationPrivilege sameOperationPrivilege = new GlobalOperationPrivilege(privilege.getCategory(), - privilege.getOperation(), buildRandomGlobalScopedPrivilege().getRaw()); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> new GlobalPrivileges(Arrays.asList(privilege, sameOperationPrivilege))); + final GlobalOperationPrivilege sameOperationPrivilege = new GlobalOperationPrivilege( + privilege.getCategory(), + privilege.getOperation(), + buildRandomGlobalScopedPrivilege().getRaw() + ); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> new GlobalPrivileges(Arrays.asList(privilege, sameOperationPrivilege)) + ); assertThat(e.getMessage(), is("Different privileges for the same category and operation are not permitted")); } public void testEqualsHashCode() { - final List privilegeList = Arrays - .asList(randomArray(1, 4, size -> new GlobalOperationPrivilege[size], () -> buildRandomGlobalScopedPrivilege())); + final List privilegeList = Arrays.asList( + randomArray(1, 4, size -> new GlobalOperationPrivilege[size], () -> buildRandomGlobalScopedPrivilege()) + ); GlobalPrivileges globalPrivileges = new GlobalPrivileges(privilegeList); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(globalPrivileges, (original) -> { - return new GlobalPrivileges(original.getPrivileges()); - }); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(globalPrivileges, (original) -> { - return new GlobalPrivileges(original.getPrivileges()); - }, (original) -> { - final List newList = Arrays - .asList(randomArray(1, 4, size -> new GlobalOperationPrivilege[size], () -> buildRandomGlobalScopedPrivilege())); - return new GlobalPrivileges(newList); - }); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + globalPrivileges, + (original) -> { return new GlobalPrivileges(original.getPrivileges()); } + ); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + globalPrivileges, + (original) -> { return new GlobalPrivileges(original.getPrivileges()); }, + (original) -> { + final List newList = Arrays.asList( + randomArray(1, 4, size -> new GlobalOperationPrivilege[size], () -> buildRandomGlobalScopedPrivilege()) + ); + return new GlobalPrivileges(newList); + } + ); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/user/privileges/IndicesPrivilegesTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/user/privileges/IndicesPrivilegesTests.java index 0b87ba020944d..ab09034fafeb3 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/user/privileges/IndicesPrivilegesTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/user/privileges/IndicesPrivilegesTests.java @@ -9,8 +9,8 @@ package org.elasticsearch.client.security.user.privileges; import org.elasticsearch.common.Strings; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.Arrays; @@ -39,8 +39,11 @@ public static IndicesPrivileges createNewRandom(String query) { } public void testToXContentWithNullFieldSecurity() { - final IndicesPrivileges privileges = IndicesPrivileges.builder().indices("abc").privileges("all") - .allowRestrictedIndices(randomBoolean()).build(); + final IndicesPrivileges privileges = IndicesPrivileges.builder() + .indices("abc") + .privileges("all") + .allowRestrictedIndices(randomBoolean()) + .build(); final String json = Strings.toString(privileges); assertThat(json, not(containsString("field_security"))); } @@ -73,7 +76,8 @@ public void testToXContentWithDeniedFieldsOnly() { @Override protected IndicesPrivileges createTestInstance() { return createNewRandom( - randomBoolean() ? null : "{ " + randomAlphaOfLengthBetween(1, 4) + " : " + randomAlphaOfLengthBetween(1, 4) + " }"); + randomBoolean() ? null : "{ " + randomAlphaOfLengthBetween(1, 4) + " : " + randomAlphaOfLengthBetween(1, 4) + " }" + ); } @Override diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/snapshots/GetFeaturesResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/snapshots/GetFeaturesResponseTests.java index 689fce50b6120..a54cc81d6c425 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/snapshots/GetFeaturesResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/snapshots/GetFeaturesResponseTests.java @@ -23,7 +23,8 @@ import static org.hamcrest.Matchers.is; public class GetFeaturesResponseTests extends AbstractResponseTestCase< - org.elasticsearch.action.admin.cluster.snapshots.features.GetSnapshottableFeaturesResponse, GetFeaturesResponse> { + org.elasticsearch.action.admin.cluster.snapshots.features.GetSnapshottableFeaturesResponse, + GetFeaturesResponse> { @Override protected org.elasticsearch.action.admin.cluster.snapshots.features.GetSnapshottableFeaturesResponse createServerTestInstance( diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/snapshots/ResetFeaturesResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/snapshots/ResetFeaturesResponseTests.java index 25bd455546c50..e230ab2803982 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/snapshots/ResetFeaturesResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/snapshots/ResetFeaturesResponseTests.java @@ -27,15 +27,16 @@ public class ResetFeaturesResponseTests extends AbstractResponseTestCase { @Override - protected ResetFeatureStateResponse createServerTestInstance( - XContentType xContentType) { + protected ResetFeatureStateResponse createServerTestInstance(XContentType xContentType) { return new org.elasticsearch.action.admin.cluster.snapshots.features.ResetFeatureStateResponse( randomList( 10, () -> randomBoolean() ? ResetFeatureStateResponse.ResetFeatureStateStatus.success(randomAlphaOfLengthBetween(6, 10)) : ResetFeatureStateResponse.ResetFeatureStateStatus.failure( - randomAlphaOfLengthBetween(6, 10), new ElasticsearchException("something went wrong")) + randomAlphaOfLengthBetween(6, 10), + new ElasticsearchException("something went wrong") + ) ) ); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/tasks/CancelTasksResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/tasks/CancelTasksResponseTests.java index 50244a6306f1f..388270e300839 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/tasks/CancelTasksResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/tasks/CancelTasksResponseTests.java @@ -17,12 +17,12 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.transport.TransportAddress; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.tasks.Task; import org.elasticsearch.tasks.TaskId; import org.elasticsearch.tasks.TaskInfo; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; import java.io.IOException; import java.net.InetAddress; @@ -37,7 +37,8 @@ import static java.util.Collections.emptyMap; import static java.util.Collections.emptySet; -public class CancelTasksResponseTests extends AbstractResponseTestCase { private static String NODE_ID = "node_id"; @@ -49,8 +50,7 @@ protected CancelTasksResponseTests.ByNodeCancelTasksResponse createServerTestIns List nodeFailures = new ArrayList<>(); for (int i = 0; i < randomIntBetween(1, 4); i++) { - taskFailures.add(new TaskOperationFailure(randomAlphaOfLength(4), (long) i, - new RuntimeException(randomAlphaOfLength(4)))); + taskFailures.add(new TaskOperationFailure(randomAlphaOfLength(4), (long) i, new RuntimeException(randomAlphaOfLength(4)))); } for (int i = 0; i < randomIntBetween(1, 4); i++) { nodeFailures.add(new ElasticsearchException(new RuntimeException(randomAlphaOfLength(10)))); @@ -58,18 +58,21 @@ protected CancelTasksResponseTests.ByNodeCancelTasksResponse createServerTestIns for (int i = 0; i < 4; i++) { boolean isCancellable = randomBoolean(); - tasks.add(new org.elasticsearch.tasks.TaskInfo( - new TaskId(NODE_ID, (long) i), - randomAlphaOfLength(4), - randomAlphaOfLength(4), - randomAlphaOfLength(10), - new FakeTaskStatus(randomAlphaOfLength(4), randomInt()), - randomLongBetween(1, 3), - randomIntBetween(5, 10), - isCancellable, - isCancellable && randomBoolean(), - new TaskId("node1", randomLong()), - Map.of("x-header-of", "some-value"))); + tasks.add( + new org.elasticsearch.tasks.TaskInfo( + new TaskId(NODE_ID, (long) i), + randomAlphaOfLength(4), + randomAlphaOfLength(4), + randomAlphaOfLength(10), + new FakeTaskStatus(randomAlphaOfLength(4), randomInt()), + randomLongBetween(1, 3), + randomIntBetween(5, 10), + isCancellable, + isCancellable && randomBoolean(), + new TaskId("node1", randomLong()), + Map.of("x-header-of", "some-value") + ) + ); } return new ByNodeCancelTasksResponse(tasks, taskFailures, nodeFailures); @@ -81,15 +84,16 @@ protected org.elasticsearch.client.tasks.CancelTasksResponse doParseToClientInst } @Override - protected void assertInstances(ByNodeCancelTasksResponse serverTestInstance, - org.elasticsearch.client.tasks.CancelTasksResponse clientInstance) { + protected void assertInstances( + ByNodeCancelTasksResponse serverTestInstance, + org.elasticsearch.client.tasks.CancelTasksResponse clientInstance + ) { // checking tasks List sTasks = serverTestInstance.getTasks(); List cTasks = clientInstance.getTasks(); - Map cTasksMap = - cTasks.stream().collect(Collectors.toMap(org.elasticsearch.client.tasks.TaskInfo::getTaskId, - Function.identity())); + Map cTasksMap = cTasks.stream() + .collect(Collectors.toMap(org.elasticsearch.client.tasks.TaskInfo::getTaskId, Function.identity())); for (TaskInfo ti : sTasks) { org.elasticsearch.client.tasks.TaskInfo taskInfo = cTasksMap.get( new org.elasticsearch.client.tasks.TaskId(ti.getTaskId().getNodeId(), ti.getTaskId().getId()) @@ -110,27 +114,23 @@ protected void assertInstances(ByNodeCancelTasksResponse serverTestInstance, } - //checking failures + // checking failures List serverNodeFailures = serverTestInstance.getNodeFailures(); List cNodeFailures = clientInstance.getNodeFailures(); - List sExceptionsMessages = serverNodeFailures.stream().map(x -> - org.elasticsearch.client.tasks.ElasticsearchException.buildMessage( - "exception", x.getMessage(), null) - ).collect(Collectors.toList() - ); - - List cExceptionsMessages = cNodeFailures.stream().map( - org.elasticsearch.client.tasks.ElasticsearchException::getMsg - ).collect(Collectors.toList()); + List sExceptionsMessages = serverNodeFailures.stream() + .map(x -> org.elasticsearch.client.tasks.ElasticsearchException.buildMessage("exception", x.getMessage(), null)) + .collect(Collectors.toList()); + + List cExceptionsMessages = cNodeFailures.stream() + .map(org.elasticsearch.client.tasks.ElasticsearchException::getMsg) + .collect(Collectors.toList()); assertEquals(new HashSet<>(sExceptionsMessages), new HashSet<>(cExceptionsMessages)); List sTaskFailures = serverTestInstance.getTaskFailures(); List cTaskFailures = clientInstance.getTaskFailures(); - Map cTasksFailuresMap = - cTaskFailures.stream().collect(Collectors.toMap( - org.elasticsearch.client.tasks.TaskOperationFailure::getTaskId, - Function.identity())); + Map cTasksFailuresMap = cTaskFailures.stream() + .collect(Collectors.toMap(org.elasticsearch.client.tasks.TaskOperationFailure::getTaskId, Function.identity())); for (TaskOperationFailure tof : sTaskFailures) { org.elasticsearch.client.tasks.TaskOperationFailure failure = cTasksFailuresMap.get(tof.getTaskId()); assertEquals(tof.getNodeId(), failure.getNodeId()); @@ -183,17 +183,17 @@ static class ByNodeCancelTasksResponse extends CancelTasksResponse { ByNodeCancelTasksResponse( List tasks, List taskFailures, - List nodeFailures) { + List nodeFailures + ) { super(tasks, taskFailures, nodeFailures); } - // it knows the hardcoded address space. @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { DiscoveryNodes.Builder dnBuilder = new DiscoveryNodes.Builder(); - InetAddress inetAddress = InetAddress.getByAddress(new byte[]{(byte) 192, (byte) 168, (byte) 0, (byte) 1}); + InetAddress inetAddress = InetAddress.getByAddress(new byte[] { (byte) 192, (byte) 168, (byte) 0, (byte) 1 }); TransportAddress transportAddress = new TransportAddress(inetAddress, randomIntBetween(0, 65535)); dnBuilder.add(new DiscoveryNode(NODE_ID, NODE_ID, transportAddress, emptyMap(), emptySet(), Version.CURRENT)); @@ -206,5 +206,3 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws } } } - - diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/tasks/ElasticsearchExceptionTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/tasks/ElasticsearchExceptionTests.java index 45a2184d3aa88..12e90c258cc8e 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/tasks/ElasticsearchExceptionTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/tasks/ElasticsearchExceptionTests.java @@ -14,7 +14,8 @@ import java.io.IOException; import java.util.List; -public class ElasticsearchExceptionTests extends AbstractResponseTestCase { @Override @@ -22,8 +23,8 @@ protected org.elasticsearch.ElasticsearchException createServerTestInstance(XCon IllegalStateException ies = new IllegalStateException("illegal_state"); IllegalArgumentException iae = new IllegalArgumentException("argument", ies); org.elasticsearch.ElasticsearchException exception = new org.elasticsearch.ElasticsearchException("elastic_exception", iae); - exception.addHeader("key","value"); - exception.addMetadata("es.meta","data"); + exception.addHeader("key", "value"); + exception.addMetadata("es.meta", "data"); exception.addSuppressed(new NumberFormatException("3/0")); return exception; } @@ -40,32 +41,31 @@ protected void assertInstances(org.elasticsearch.ElasticsearchException serverTe IllegalArgumentException sCauseLevel1 = (IllegalArgumentException) serverTestInstance.getCause(); ElasticsearchException cCauseLevel1 = clientInstance.getCause(); - assertTrue(sCauseLevel1 !=null); - assertTrue(cCauseLevel1 !=null); + assertTrue(sCauseLevel1 != null); + assertTrue(cCauseLevel1 != null); IllegalStateException causeLevel2 = (IllegalStateException) serverTestInstance.getCause().getCause(); ElasticsearchException cCauseLevel2 = clientInstance.getCause().getCause(); - assertTrue(causeLevel2 !=null); - assertTrue(cCauseLevel2 !=null); - + assertTrue(causeLevel2 != null); + assertTrue(cCauseLevel2 != null); ElasticsearchException cause = new ElasticsearchException( "Elasticsearch exception [type=illegal_state_exception, reason=illegal_state]" ); ElasticsearchException caused1 = new ElasticsearchException( - "Elasticsearch exception [type=illegal_argument_exception, reason=argument]",cause + "Elasticsearch exception [type=illegal_argument_exception, reason=argument]", + cause ); ElasticsearchException caused2 = new ElasticsearchException( - "Elasticsearch exception [type=exception, reason=elastic_exception]",caused1 + "Elasticsearch exception [type=exception, reason=elastic_exception]", + caused1 ); caused2.addHeader("key", List.of("value")); - ElasticsearchException supp = new ElasticsearchException( - "Elasticsearch exception [type=number_format_exception, reason=3/0]" - ); + ElasticsearchException supp = new ElasticsearchException("Elasticsearch exception [type=number_format_exception, reason=3/0]"); caused2.addSuppressed(List.of(supp)); - assertEquals(caused2,clientInstance); + assertEquals(caused2, clientInstance); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/tasks/TaskSubmissionResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/tasks/TaskSubmissionResponseTests.java index 47dd8d4d29d76..9c89afb9f59af 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/tasks/TaskSubmissionResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/tasks/TaskSubmissionResponseTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.tasks; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; @@ -18,11 +18,7 @@ public class TaskSubmissionResponseTests extends ESTestCase { public void testFromXContent() throws IOException { - xContentTester( - this::createParser, - this::createTestInstance, - this::toXContent, - TaskSubmissionResponse::fromXContent) + xContentTester(this::createParser, this::createTestInstance, this::toXContent, TaskSubmissionResponse::fromXContent) .supportsUnknownFields(true) .test(); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/textstructure/FindStructureRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/textstructure/FindStructureRequestTests.java index df0d5c6f02839..21a21ccf3267f 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/textstructure/FindStructureRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/textstructure/FindStructureRequestTests.java @@ -9,9 +9,9 @@ import org.elasticsearch.client.textstructure.structurefinder.TextStructure; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.ObjectParser; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; import java.nio.charset.Charset; @@ -19,14 +19,18 @@ public class FindStructureRequestTests extends AbstractXContentTestCase { - private static final ObjectParser PARSER = - new ObjectParser<>("find_file_structure_request", FindStructureRequest::new); + private static final ObjectParser PARSER = new ObjectParser<>( + "find_file_structure_request", + FindStructureRequest::new + ); static { PARSER.declareInt(FindStructureRequest::setLinesToSample, FindStructureRequest.LINES_TO_SAMPLE); PARSER.declareInt(FindStructureRequest::setLineMergeSizeLimit, FindStructureRequest.LINE_MERGE_SIZE_LIMIT); - PARSER.declareString((p, c) -> p.setTimeout(TimeValue.parseTimeValue(c, FindStructureRequest.TIMEOUT.getPreferredName())), - FindStructureRequest.TIMEOUT); + PARSER.declareString( + (p, c) -> p.setTimeout(TimeValue.parseTimeValue(c, FindStructureRequest.TIMEOUT.getPreferredName())), + FindStructureRequest.TIMEOUT + ); PARSER.declareString(FindStructureRequest::setCharset, FindStructureRequest.CHARSET); PARSER.declareString(FindStructureRequest::setFormat, FindStructureRequest.FORMAT); PARSER.declareStringArray(FindStructureRequest::setColumnNames, FindStructureRequest.COLUMN_NAMES); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/textstructure/FindStructureResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/textstructure/FindStructureResponseTests.java index 2975439c7b846..50f6f13eb90e7 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/textstructure/FindStructureResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/textstructure/FindStructureResponseTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.textstructure; import org.elasticsearch.client.textstructure.structurefinder.TextStructureTests; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.function.Predicate; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/textstructure/structurefinder/FieldStatsTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/textstructure/structurefinder/FieldStatsTests.java index 49981b3f5a7be..fc85c55198fa8 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/textstructure/structurefinder/FieldStatsTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/textstructure/structurefinder/FieldStatsTests.java @@ -7,9 +7,8 @@ */ package org.elasticsearch.client.textstructure.structurefinder; -import org.elasticsearch.client.textstructure.structurefinder.FieldStats; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.util.ArrayList; import java.util.LinkedHashMap; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/textstructure/structurefinder/TextStructureTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/textstructure/structurefinder/TextStructureTests.java index 88f60ff1565a0..b62d094b5f761 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/textstructure/structurefinder/TextStructureTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/textstructure/structurefinder/TextStructureTests.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.textstructure.structurefinder; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.nio.charset.Charset; import java.util.Arrays; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/AcknowledgedTasksResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/AcknowledgedTasksResponseTests.java index 06320aadee305..1107fa0588a7e 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/AcknowledgedTasksResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/AcknowledgedTasksResponseTests.java @@ -10,10 +10,10 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.action.TaskOperationFailure; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.ESTestCase; import java.io.IOException; import java.util.ArrayList; @@ -27,14 +27,15 @@ public class AcknowledgedTasksResponseTests extends ESTestCase { public void testFromXContent() throws IOException { - xContentTester(this::createParser, - this::createTestInstance, - AcknowledgedTasksResponseTests::toXContent, - AcknowledgedTasksResponseTests::fromXContent) - .assertEqualsConsumer(AcknowledgedTasksResponseTests::assertEqualInstances) - .assertToXContentEquivalence(false) - .supportsUnknownFields(false) - .test(); + xContentTester( + this::createParser, + this::createTestInstance, + AcknowledgedTasksResponseTests::toXContent, + AcknowledgedTasksResponseTests::fromXContent + ).assertEqualsConsumer(AcknowledgedTasksResponseTests::assertEqualInstances) + .assertToXContentEquivalence(false) + .supportsUnknownFields(false) + .test(); } // Serialisation of TaskOperationFailure and ElasticsearchException changes @@ -56,22 +57,22 @@ private static void assertListEquals(List expected, List actual, BiPre } assertEquals(expected.size(), actual.size()); - for (int i=0; i expected, - List actual) { - assertListEquals(expected, actual, (a, b) -> - Objects.equals(a.getNodeId(), b.getNodeId()) - && Objects.equals(a.getTaskId(), b.getTaskId()) - && Objects.equals(a.getStatus(), b.getStatus()) + public static void assertTaskOperationFailuresEqual(List expected, List actual) { + assertListEquals( + expected, + actual, + (a, b) -> Objects.equals(a.getNodeId(), b.getNodeId()) + && Objects.equals(a.getTaskId(), b.getTaskId()) + && Objects.equals(a.getStatus(), b.getStatus()) ); } - public static void assertNodeFailuresEqual(List expected, - List actual) { + public static void assertNodeFailuresEqual(List expected, List actual) { // actualException is a wrapped copy of expectedException so the // error messages won't be the same but actualException should contain // the error message from expectedException @@ -82,9 +83,8 @@ public static void assertNodeFailuresEqual(List expected } private static AcknowledgedTasksResponse fromXContent(XContentParser parser) { - return AcknowledgedTasksResponse.generateParser("ack_tasks_response", - AcknowledgedTasksResponse::new, "acknowleged") - .apply(parser, null); + return AcknowledgedTasksResponse.generateParser("ack_tasks_response", AcknowledgedTasksResponse::new, "acknowleged") + .apply(parser, null); } private AcknowledgedTasksResponse createTestInstance() { @@ -92,7 +92,7 @@ private AcknowledgedTasksResponse createTestInstance() { if (randomBoolean()) { taskFailures = new ArrayList<>(); int numTaskFailures = randomIntBetween(1, 4); - for (int i=0; i(); int numNodeFailures = randomIntBetween(1, 4); - for (int i=0; i nodeFailu } } } - - diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/DeleteTransformRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/DeleteTransformRequestTests.java index 5044c388e9147..7ec31ec4a9b56 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/DeleteTransformRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/DeleteTransformRequestTests.java @@ -16,7 +16,6 @@ public class DeleteTransformRequestTests extends ESTestCase { public void testValidate() { assertFalse(new DeleteTransformRequest("valid-id").validate().isPresent()); - assertThat(new DeleteTransformRequest(null).validate().get().getMessage(), - containsString("transform id must not be null")); + assertThat(new DeleteTransformRequest(null).validate().get().getMessage(), containsString("transform id must not be null")); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/GetTransformRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/GetTransformRequestTests.java index 30bb2d69a9f7f..856e0741309eb 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/GetTransformRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/GetTransformRequestTests.java @@ -15,7 +15,6 @@ public class GetTransformRequestTests extends ESTestCase { public void testValidate() { assertFalse(new GetTransformRequest("valid-id").validate().isPresent()); - assertThat(new GetTransformRequest(new String[0]).validate().get().getMessage(), - containsString("transform id must not be null")); + assertThat(new GetTransformRequest(new String[0]).validate().get().getMessage(), containsString("transform id must not be null")); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/GetTransformResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/GetTransformResponseTests.java index f35c4314b020b..e425e140ed7b9 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/GetTransformResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/GetTransformResponseTests.java @@ -11,10 +11,10 @@ import org.elasticsearch.client.transform.transforms.TransformConfig; import org.elasticsearch.client.transform.transforms.TransformConfigTests; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.search.SearchModule; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.ArrayList; @@ -24,22 +24,21 @@ import static org.elasticsearch.test.AbstractXContentTestCase.xContentTester; - public class GetTransformResponseTests extends ESTestCase { public void testXContentParser() throws IOException { - xContentTester(this::createParser, - GetTransformResponseTests::createTestInstance, - GetTransformResponseTests::toXContent, - GetTransformResponse::fromXContent) - .supportsUnknownFields(false) - .test(); + xContentTester( + this::createParser, + GetTransformResponseTests::createTestInstance, + GetTransformResponseTests::toXContent, + GetTransformResponse::fromXContent + ).supportsUnknownFields(false).test(); } private static GetTransformResponse createTestInstance() { int numTransforms = randomIntBetween(0, 3); List transforms = new ArrayList<>(); - for (int i=0; i { public static DestConfig randomDestConfig() { - return new DestConfig(randomAlphaOfLength(10), - randomBoolean() ? null : randomAlphaOfLength(10)); + return new DestConfig(randomAlphaOfLength(10), randomBoolean() ? null : randomAlphaOfLength(10)); } @Override diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/NodeAttributesTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/NodeAttributesTests.java index c73746a407f02..98ed152486a6d 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/NodeAttributesTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/NodeAttributesTests.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.transform.transforms; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.HashMap; @@ -20,15 +20,17 @@ public class NodeAttributesTests extends AbstractXContentTestCase attributes = new HashMap<>(numberOfAttributes); - for(int i = 0; i < numberOfAttributes; i++) { + for (int i = 0; i < numberOfAttributes; i++) { String val = randomAlphaOfLength(10); - attributes.put("key-"+i, val); + attributes.put("key-" + i, val); } - return new NodeAttributes(randomAlphaOfLength(10), + return new NodeAttributes( + randomAlphaOfLength(10), randomAlphaOfLength(10), randomAlphaOfLength(10), randomAlphaOfLength(10), - attributes); + attributes + ); } @Override diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/QueryConfigTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/QueryConfigTests.java index 4f6578bfca558..35b51fe7e4c0e 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/QueryConfigTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/QueryConfigTests.java @@ -9,13 +9,13 @@ package org.elasticsearch.client.transform.transforms; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.index.query.MatchAllQueryBuilder; import org.elasticsearch.index.query.MatchNoneQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.search.SearchModule; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/SettingsConfigTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/SettingsConfigTests.java index c35b3cd1a3a2a..ce7398aa7a645 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/SettingsConfigTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/SettingsConfigTests.java @@ -10,13 +10,13 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xpack.core.watcher.watch.Payload.XContent; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/SourceConfigTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/SourceConfigTests.java index ea46e0aec9cb4..f90a3fdecc8b7 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/SourceConfigTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/SourceConfigTests.java @@ -9,10 +9,10 @@ package org.elasticsearch.client.transform.transforms; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.SearchModule; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.Map; @@ -22,14 +22,14 @@ import static java.util.Collections.singletonMap; import static java.util.stream.Collectors.toMap; - public class SourceConfigTests extends AbstractXContentTestCase { public static SourceConfig randomSourceConfig() { return new SourceConfig( generateRandomStringArray(10, 10, false, false), QueryConfigTests.randomQueryConfig(), - randomRuntimeMappings()); + randomRuntimeMappings() + ); } private static Map randomRuntimeMappings() { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/TimeRetentionPolicyConfigTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/TimeRetentionPolicyConfigTests.java index 1e197d73006a3..2ceafcbb84696 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/TimeRetentionPolicyConfigTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/TimeRetentionPolicyConfigTests.java @@ -9,8 +9,8 @@ package org.elasticsearch.client.transform.transforms; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/TimeSyncConfigTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/TimeSyncConfigTests.java index 896081b663c2e..867c05de82d18 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/TimeSyncConfigTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/TimeSyncConfigTests.java @@ -9,8 +9,8 @@ package org.elasticsearch.client.transform.transforms; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/TransformCheckpointStatsTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/TransformCheckpointStatsTests.java index a3f86197a3380..e8f6b55a2a65f 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/TransformCheckpointStatsTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/TransformCheckpointStatsTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.transform.transforms; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; @@ -18,20 +18,22 @@ public class TransformCheckpointStatsTests extends ESTestCase { public void testFromXContent() throws IOException { - xContentTester(this::createParser, + xContentTester( + this::createParser, TransformCheckpointStatsTests::randomTransformCheckpointStats, TransformCheckpointStatsTests::toXContent, - TransformCheckpointStats::fromXContent) - .supportsUnknownFields(true) - .randomFieldsExcludeFilter(field -> field.startsWith("position")) - .test(); + TransformCheckpointStats::fromXContent + ).supportsUnknownFields(true).randomFieldsExcludeFilter(field -> field.startsWith("position")).test(); } public static TransformCheckpointStats randomTransformCheckpointStats() { - return new TransformCheckpointStats(randomLongBetween(1, 1_000_000), + return new TransformCheckpointStats( + randomLongBetween(1, 1_000_000), randomBoolean() ? null : TransformIndexerPositionTests.randomTransformIndexerPosition(), randomBoolean() ? null : TransformProgressTests.randomInstance(), - randomLongBetween(1, 1_000_000), randomLongBetween(0, 1_000_000)); + randomLongBetween(1, 1_000_000), + randomLongBetween(0, 1_000_000) + ); } public static void toXContent(TransformCheckpointStats stats, XContentBuilder builder) throws IOException { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/TransformCheckpointingInfoTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/TransformCheckpointingInfoTests.java index fcaa8edb67a49..168d3c9b09dcf 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/TransformCheckpointingInfoTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/TransformCheckpointingInfoTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.transform.transforms; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.time.Instant; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/TransformConfigTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/TransformConfigTests.java index bcd83d1c0c56b..bf01b876f5252 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/TransformConfigTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/TransformConfigTests.java @@ -17,10 +17,10 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.TimeValue; import org.elasticsearch.core.Tuple; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.SearchModule; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.time.Instant; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/TransformConfigUpdateTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/TransformConfigUpdateTests.java index 3e81b7ca17fdb..c9c5d92bc1632 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/TransformConfigUpdateTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/TransformConfigUpdateTests.java @@ -11,10 +11,10 @@ import org.elasticsearch.client.transform.TransformNamedXContentProvider; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.SearchModule; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.Collections; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/TransformIndexerPositionTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/TransformIndexerPositionTests.java index f60fe652c061f..f7ccfcbaa14ef 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/TransformIndexerPositionTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/TransformIndexerPositionTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.transform.transforms; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.LinkedHashMap; @@ -20,14 +20,14 @@ public class TransformIndexerPositionTests extends ESTestCase { public void testFromXContent() throws IOException { - xContentTester(this::createParser, - TransformIndexerPositionTests::randomTransformIndexerPosition, - TransformIndexerPositionTests::toXContent, - TransformIndexerPosition::fromXContent) - .supportsUnknownFields(true) - .randomFieldsExcludeFilter(field -> field.equals("indexer_position") || - field.equals("bucket_position")) - .test(); + xContentTester( + this::createParser, + TransformIndexerPositionTests::randomTransformIndexerPosition, + TransformIndexerPositionTests::toXContent, + TransformIndexerPosition::fromXContent + ).supportsUnknownFields(true) + .randomFieldsExcludeFilter(field -> field.equals("indexer_position") || field.equals("bucket_position")) + .test(); } public static TransformIndexerPosition randomTransformIndexerPosition() { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/TransformIndexerStatsTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/TransformIndexerStatsTests.java index 39a13daea0f2d..5481c0ce08d96 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/TransformIndexerStatsTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/TransformIndexerStatsTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.transform.transforms; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/TransformProgressTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/TransformProgressTests.java index c76aeb02fcc20..649b74c81d783 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/TransformProgressTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/TransformProgressTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.transform.transforms; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; @@ -18,12 +18,12 @@ public class TransformProgressTests extends ESTestCase { public void testFromXContent() throws IOException { - xContentTester(this::createParser, + xContentTester( + this::createParser, TransformProgressTests::randomInstance, TransformProgressTests::toXContent, - TransformProgress::fromXContent) - .supportsUnknownFields(true) - .test(); + TransformProgress::fromXContent + ).supportsUnknownFields(true).test(); } public static TransformProgress randomInstance() { @@ -32,7 +32,8 @@ public static TransformProgress randomInstance() { randomBoolean() ? null : randomNonNegativeLong(), randomBoolean() ? null : randomDouble(), randomBoolean() ? null : randomNonNegativeLong(), - randomBoolean() ? null : randomNonNegativeLong()); + randomBoolean() ? null : randomNonNegativeLong() + ); } public static void toXContent(TransformProgress progress, XContentBuilder builder) throws IOException { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/TransformStatsTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/TransformStatsTests.java index ab61a31c98f28..0d201b31acc99 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/TransformStatsTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/TransformStatsTests.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.transform.transforms; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.test.ESTestCase; import java.io.IOException; @@ -19,30 +19,32 @@ public class TransformStatsTests extends ESTestCase { public void testFromXContent() throws IOException { - xContentTester(this::createParser, + xContentTester( + this::createParser, TransformStatsTests::randomInstance, TransformStatsTests::toXContent, - TransformStats::fromXContent) - .supportsUnknownFields(true) - .randomFieldsExcludeFilter(field -> field.equals("node.attributes") || field.contains("position")) - .test(); + TransformStats::fromXContent + ).supportsUnknownFields(true) + .randomFieldsExcludeFilter(field -> field.equals("node.attributes") || field.contains("position")) + .test(); } public static TransformStats randomInstance() { - return new TransformStats(randomAlphaOfLength(10), + return new TransformStats( + randomAlphaOfLength(10), randomBoolean() ? null : randomFrom(TransformStats.State.values()), randomBoolean() ? null : randomAlphaOfLength(100), randomBoolean() ? null : NodeAttributesTests.createRandom(), TransformIndexerStatsTests.randomStats(), - randomBoolean() ? null : TransformCheckpointingInfoTests.randomTransformCheckpointingInfo()); + randomBoolean() ? null : TransformCheckpointingInfoTests.randomTransformCheckpointingInfo() + ); } public static void toXContent(TransformStats stats, XContentBuilder builder) throws IOException { builder.startObject(); builder.field(TransformStats.ID.getPreferredName(), stats.getId()); if (stats.getState() != null) { - builder.field(TransformStats.STATE_FIELD.getPreferredName(), - stats.getState().value()); + builder.field(TransformStats.STATE_FIELD.getPreferredName(), stats.getState().value()); } if (stats.getReason() != null) { builder.field(TransformStats.REASON_FIELD.getPreferredName(), stats.getReason()); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/hlrc/TimeSyncConfigTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/hlrc/TimeSyncConfigTests.java index a04ce33832e69..f56a26c807d84 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/hlrc/TimeSyncConfigTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/hlrc/TimeSyncConfigTests.java @@ -16,16 +16,21 @@ import java.io.IOException; -public class TimeSyncConfigTests - extends AbstractResponseTestCase { +public class TimeSyncConfigTests extends AbstractResponseTestCase< + org.elasticsearch.xpack.core.transform.transforms.TimeSyncConfig, + TimeSyncConfig> { public static org.elasticsearch.xpack.core.transform.transforms.TimeSyncConfig randomTimeSyncConfig() { - return new org.elasticsearch.xpack.core.transform.transforms.TimeSyncConfig(randomAlphaOfLengthBetween(1, 10), - new TimeValue(randomNonNegativeLong())); + return new org.elasticsearch.xpack.core.transform.transforms.TimeSyncConfig( + randomAlphaOfLengthBetween(1, 10), + new TimeValue(randomNonNegativeLong()) + ); } - public static void assertHlrcEquals(org.elasticsearch.xpack.core.transform.transforms.TimeSyncConfig serverTestInstance, - TimeSyncConfig clientInstance) { + public static void assertHlrcEquals( + org.elasticsearch.xpack.core.transform.transforms.TimeSyncConfig serverTestInstance, + TimeSyncConfig clientInstance + ) { assertEquals(serverTestInstance.getField(), clientInstance.getField()); assertEquals(serverTestInstance.getDelay(), clientInstance.getDelay()); } @@ -41,8 +46,10 @@ protected TimeSyncConfig doParseToClientInstance(XContentParser parser) throws I } @Override - protected void assertInstances(org.elasticsearch.xpack.core.transform.transforms.TimeSyncConfig serverTestInstance, - TimeSyncConfig clientInstance) { + protected void assertInstances( + org.elasticsearch.xpack.core.transform.transforms.TimeSyncConfig serverTestInstance, + TimeSyncConfig clientInstance + ) { assertHlrcEquals(serverTestInstance, clientInstance); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/hlrc/TransformCheckpointStatsTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/hlrc/TransformCheckpointStatsTests.java index 635e6f723a6d0..717ec3edf463e 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/hlrc/TransformCheckpointStatsTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/hlrc/TransformCheckpointStatsTests.java @@ -18,23 +18,27 @@ import static org.hamcrest.Matchers.equalTo; public class TransformCheckpointStatsTests extends AbstractResponseTestCase< - TransformCheckpointStats, - org.elasticsearch.client.transform.transforms.TransformCheckpointStats> { + TransformCheckpointStats, + org.elasticsearch.client.transform.transforms.TransformCheckpointStats> { - public static TransformCheckpointStats fromHlrc( - org.elasticsearch.client.transform.transforms.TransformCheckpointStats instance) { - return new TransformCheckpointStats(instance.getCheckpoint(), + public static TransformCheckpointStats fromHlrc(org.elasticsearch.client.transform.transforms.TransformCheckpointStats instance) { + return new TransformCheckpointStats( + instance.getCheckpoint(), TransformIndexerPositionTests.fromHlrc(instance.getPosition()), TransformProgressTests.fromHlrc(instance.getCheckpointProgress()), instance.getTimestampMillis(), - instance.getTimeUpperBoundMillis()); + instance.getTimeUpperBoundMillis() + ); } public static TransformCheckpointStats randomTransformCheckpointStats() { - return new TransformCheckpointStats(randomLongBetween(1, 1_000_000), + return new TransformCheckpointStats( + randomLongBetween(1, 1_000_000), TransformIndexerPositionTests.randomTransformIndexerPosition(), randomBoolean() ? null : TransformProgressTests.randomTransformProgress(), - randomLongBetween(1, 1_000_000), randomLongBetween(0, 1_000_000)); + randomLongBetween(1, 1_000_000), + randomLongBetween(0, 1_000_000) + ); } @Override @@ -49,22 +53,32 @@ protected org.elasticsearch.client.transform.transforms.TransformCheckpointStats } @Override - protected void assertInstances(TransformCheckpointStats serverTestInstance, - org.elasticsearch.client.transform.transforms.TransformCheckpointStats clientInstance) { + protected void assertInstances( + TransformCheckpointStats serverTestInstance, + org.elasticsearch.client.transform.transforms.TransformCheckpointStats clientInstance + ) { assertThat(serverTestInstance.getCheckpoint(), equalTo(clientInstance.getCheckpoint())); assertThat(serverTestInstance.getPosition().getBucketsPosition(), equalTo(clientInstance.getPosition().getBucketsPosition())); assertThat(serverTestInstance.getPosition().getIndexerPosition(), equalTo(clientInstance.getPosition().getIndexerPosition())); assertThat(serverTestInstance.getTimestampMillis(), equalTo(clientInstance.getTimestampMillis())); assertThat(serverTestInstance.getTimeUpperBoundMillis(), equalTo(clientInstance.getTimeUpperBoundMillis())); if (serverTestInstance.getCheckpointProgress() != null) { - assertThat(serverTestInstance.getCheckpointProgress().getDocumentsIndexed(), - equalTo(clientInstance.getCheckpointProgress().getDocumentsIndexed())); - assertThat(serverTestInstance.getCheckpointProgress().getDocumentsProcessed(), - equalTo(clientInstance.getCheckpointProgress().getDocumentsProcessed())); - assertThat(serverTestInstance.getCheckpointProgress().getPercentComplete(), - equalTo(clientInstance.getCheckpointProgress().getPercentComplete())); - assertThat(serverTestInstance.getCheckpointProgress().getTotalDocs(), - equalTo(clientInstance.getCheckpointProgress().getTotalDocs())); + assertThat( + serverTestInstance.getCheckpointProgress().getDocumentsIndexed(), + equalTo(clientInstance.getCheckpointProgress().getDocumentsIndexed()) + ); + assertThat( + serverTestInstance.getCheckpointProgress().getDocumentsProcessed(), + equalTo(clientInstance.getCheckpointProgress().getDocumentsProcessed()) + ); + assertThat( + serverTestInstance.getCheckpointProgress().getPercentComplete(), + equalTo(clientInstance.getCheckpointProgress().getPercentComplete()) + ); + assertThat( + serverTestInstance.getCheckpointProgress().getTotalDocs(), + equalTo(clientInstance.getCheckpointProgress().getTotalDocs()) + ); } } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/hlrc/TransformIndexerPositionTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/hlrc/TransformIndexerPositionTests.java index 1502c091cde19..df724e7826087 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/hlrc/TransformIndexerPositionTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/hlrc/TransformIndexerPositionTests.java @@ -19,11 +19,10 @@ import static org.hamcrest.Matchers.equalTo; public class TransformIndexerPositionTests extends AbstractResponseTestCase< - TransformIndexerPosition, - org.elasticsearch.client.transform.transforms.TransformIndexerPosition> { + TransformIndexerPosition, + org.elasticsearch.client.transform.transforms.TransformIndexerPosition> { - public static TransformIndexerPosition fromHlrc( - org.elasticsearch.client.transform.transforms.TransformIndexerPosition instance) { + public static TransformIndexerPosition fromHlrc(org.elasticsearch.client.transform.transforms.TransformIndexerPosition instance) { if (instance == null) { return null; } @@ -45,8 +44,10 @@ protected org.elasticsearch.client.transform.transforms.TransformIndexerPosition } @Override - protected void assertInstances(TransformIndexerPosition serverTestInstance, - org.elasticsearch.client.transform.transforms.TransformIndexerPosition clientInstance) { + protected void assertInstances( + TransformIndexerPosition serverTestInstance, + org.elasticsearch.client.transform.transforms.TransformIndexerPosition clientInstance + ) { assertThat(serverTestInstance.getIndexerPosition(), equalTo(clientInstance.getIndexerPosition())); assertThat(serverTestInstance.getBucketsPosition(), equalTo(clientInstance.getBucketsPosition())); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/hlrc/TransformProgressTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/hlrc/TransformProgressTests.java index 618633cd2c50b..52e16468ee92c 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/hlrc/TransformProgressTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/hlrc/TransformProgressTests.java @@ -16,18 +16,19 @@ import static org.hamcrest.Matchers.equalTo; public class TransformProgressTests extends AbstractResponseTestCase< - TransformProgress, - org.elasticsearch.client.transform.transforms.TransformProgress> { + TransformProgress, + org.elasticsearch.client.transform.transforms.TransformProgress> { - public static TransformProgress fromHlrc( - org.elasticsearch.client.transform.transforms.TransformProgress instance) { + public static TransformProgress fromHlrc(org.elasticsearch.client.transform.transforms.TransformProgress instance) { if (instance == null) { return null; } - return new TransformProgress(instance.getTotalDocs(), + return new TransformProgress( + instance.getTotalDocs(), instance.getRemainingDocs(), instance.getDocumentsProcessed(), - instance.getDocumentsIndexed()); + instance.getDocumentsIndexed() + ); } public static TransformProgress randomTransformProgress() { @@ -37,7 +38,8 @@ public static TransformProgress randomTransformProgress() { totalDocs, docsRemaining, totalDocs != null ? totalDocs - docsRemaining : randomNonNegativeLong(), - randomBoolean() ? null : randomNonNegativeLong()); + randomBoolean() ? null : randomNonNegativeLong() + ); } @Override @@ -51,8 +53,10 @@ protected org.elasticsearch.client.transform.transforms.TransformProgress doPars } @Override - protected void assertInstances(TransformProgress serverTestInstance, - org.elasticsearch.client.transform.transforms.TransformProgress clientInstance) { + protected void assertInstances( + TransformProgress serverTestInstance, + org.elasticsearch.client.transform.transforms.TransformProgress clientInstance + ) { assertThat(serverTestInstance.getTotalDocs(), equalTo(clientInstance.getTotalDocs())); assertThat(serverTestInstance.getDocumentsProcessed(), equalTo(clientInstance.getDocumentsProcessed())); assertThat(serverTestInstance.getPercentComplete(), equalTo(clientInstance.getPercentComplete())); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/latest/LatestConfigTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/latest/LatestConfigTests.java index 11895aeab2d3e..d254dbd895dcc 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/latest/LatestConfigTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/latest/LatestConfigTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.client.transform.transforms.latest; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.ArrayList; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/latest/hlrc/LatestConfigTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/latest/hlrc/LatestConfigTests.java index f5a7a94cfcf9a..1fbef45821771 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/latest/hlrc/LatestConfigTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/latest/hlrc/LatestConfigTests.java @@ -19,8 +19,9 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; -public class LatestConfigTests - extends AbstractResponseTestCase { +public class LatestConfigTests extends AbstractResponseTestCase< + LatestConfig, + org.elasticsearch.client.transform.transforms.latest.LatestConfig> { public static LatestConfig randomLatestConfig() { return new LatestConfig( diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/pivot/AggregationConfigTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/pivot/AggregationConfigTests.java index 610d5c8c35301..2d24b422bac68 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/pivot/AggregationConfigTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/pivot/AggregationConfigTests.java @@ -9,13 +9,13 @@ package org.elasticsearch.client.transform.transforms.pivot; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.SearchModule; import org.elasticsearch.search.aggregations.AggregationBuilder; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.AggregatorFactories; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.HashSet; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/pivot/DateHistogramGroupSourceTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/pivot/DateHistogramGroupSourceTests.java index 50762246c791e..3455a7bcfef48 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/pivot/DateHistogramGroupSourceTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/pivot/DateHistogramGroupSourceTests.java @@ -8,10 +8,10 @@ package org.elasticsearch.client.transform.transforms.pivot; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.script.Script; import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.function.Predicate; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/pivot/GeoTileGroupSourceTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/pivot/GeoTileGroupSourceTests.java index ee9983c524394..365947594206f 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/pivot/GeoTileGroupSourceTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/pivot/GeoTileGroupSourceTests.java @@ -10,11 +10,11 @@ import org.elasticsearch.common.geo.GeoBoundingBox; import org.elasticsearch.common.geo.GeoPoint; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.geo.GeometryTestUtils; import org.elasticsearch.geometry.Rectangle; import org.elasticsearch.search.aggregations.bucket.geogrid.GeoTileUtils; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.function.Predicate; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/pivot/GroupConfigTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/pivot/GroupConfigTests.java index 75d6589d586cd..4b604f35cee8d 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/pivot/GroupConfigTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/pivot/GroupConfigTests.java @@ -9,11 +9,11 @@ package org.elasticsearch.client.transform.transforms.pivot; import org.elasticsearch.common.bytes.BytesArray; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; import java.util.HashSet; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/pivot/HistogramGroupSourceTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/pivot/HistogramGroupSourceTests.java index e3701d24b3e9e..850cd86121023 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/pivot/HistogramGroupSourceTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/pivot/HistogramGroupSourceTests.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.transform.transforms.pivot; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.script.Script; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.function.Predicate; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/pivot/PivotConfigTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/pivot/PivotConfigTests.java index eb89b4195ce9a..cfbe2338c6d7c 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/pivot/PivotConfigTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/pivot/PivotConfigTests.java @@ -9,10 +9,10 @@ package org.elasticsearch.client.transform.transforms.pivot; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.SearchModule; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.Collections; @@ -21,9 +21,11 @@ public class PivotConfigTests extends AbstractXContentTestCase { public static PivotConfig randomPivotConfig() { - return new PivotConfig(GroupConfigTests.randomGroupConfig(), + return new PivotConfig( + GroupConfigTests.randomGroupConfig(), AggregationConfigTests.randomAggregationConfig(), - randomBoolean() ? null : randomIntBetween(10, 10_000)); + randomBoolean() ? null : randomIntBetween(10, 10_000) + ); } @Override diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/pivot/TermsGroupSourceTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/pivot/TermsGroupSourceTests.java index 3c061f0bb1e7f..4c6fe7f1eb624 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/pivot/TermsGroupSourceTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/pivot/TermsGroupSourceTests.java @@ -8,9 +8,9 @@ package org.elasticsearch.client.transform.transforms.pivot; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.script.Script; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.function.Predicate; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/pivot/hlrc/DateHistogramGroupSourceTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/pivot/hlrc/DateHistogramGroupSourceTests.java index 83d76d14cd940..a5fe669c2994a 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/pivot/hlrc/DateHistogramGroupSourceTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/pivot/hlrc/DateHistogramGroupSourceTests.java @@ -10,15 +10,15 @@ import org.elasticsearch.client.AbstractResponseTestCase; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.script.Script; +import org.elasticsearch.script.ScriptType; +import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.script.Script; -import org.elasticsearch.script.ScriptType; -import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; import org.elasticsearch.xpack.core.transform.transforms.pivot.DateHistogramGroupSource; import org.elasticsearch.xpack.core.transform.transforms.pivot.ScriptConfig; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/pivot/hlrc/GeoTileGroupSourceTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/pivot/hlrc/GeoTileGroupSourceTests.java index fe556c8124d3b..8d832603d9e04 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/pivot/hlrc/GeoTileGroupSourceTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/pivot/hlrc/GeoTileGroupSourceTests.java @@ -11,11 +11,11 @@ import org.elasticsearch.client.AbstractResponseTestCase; import org.elasticsearch.common.geo.GeoBoundingBox; import org.elasticsearch.common.geo.GeoPoint; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.geo.GeometryTestUtils; import org.elasticsearch.geometry.Rectangle; import org.elasticsearch.search.aggregations.bucket.geogrid.GeoTileUtils; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.transform.transforms.pivot.GeoTileGroupSource; import static org.hamcrest.Matchers.equalTo; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/AckWatchResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/AckWatchResponseTests.java index ffc64b652ab5e..42a0e82d9232a 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/AckWatchResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/AckWatchResponseTests.java @@ -9,14 +9,14 @@ package org.elasticsearch.client.watcher; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.XContentTestUtils; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParseException; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.test.XContentTestUtils; import java.io.IOException; import java.util.function.Predicate; @@ -31,12 +31,13 @@ public class AckWatchResponseTests extends ESTestCase { public void testBasicParsing() throws IOException { XContentType contentType = randomFrom(XContentType.values()); - XContentBuilder builder = XContentFactory.contentBuilder(contentType).startObject() + XContentBuilder builder = XContentFactory.contentBuilder(contentType) + .startObject() .startObject("status") - .field("version", 42) - .field("execution_state", ExecutionState.ACKNOWLEDGED) + .field("version", 42) + .field("execution_state", ExecutionState.ACKNOWLEDGED) .endObject() - .endObject(); + .endObject(); BytesReference bytes = BytesReference.bytes(builder); AckWatchResponse response = parse(builder.contentType(), bytes); @@ -56,9 +57,7 @@ public void testParsingWithMissingStatus() throws IOException { public void testParsingWithNullStatus() throws IOException { XContentType contentType = randomFrom(XContentType.values()); - XContentBuilder builder = XContentFactory.contentBuilder(contentType).startObject() - .nullField("status") - .endObject(); + XContentBuilder builder = XContentFactory.contentBuilder(contentType).startObject().nullField("status").endObject(); BytesReference bytes = BytesReference.bytes(builder); expectThrows(XContentParseException.class, () -> parse(builder.contentType(), bytes)); @@ -66,17 +65,17 @@ public void testParsingWithNullStatus() throws IOException { public void testParsingWithUnknownKeys() throws IOException { XContentType contentType = randomFrom(XContentType.values()); - XContentBuilder builder = XContentFactory.contentBuilder(contentType).startObject() + XContentBuilder builder = XContentFactory.contentBuilder(contentType) + .startObject() .startObject("status") - .field("version", 42) - .field("execution_state", ExecutionState.ACKNOWLEDGED) + .field("version", 42) + .field("execution_state", ExecutionState.ACKNOWLEDGED) .endObject() - .endObject(); + .endObject(); BytesReference bytes = BytesReference.bytes(builder); Predicate excludeFilter = field -> field.equals("status.actions"); - BytesReference bytesWithRandomFields = XContentTestUtils.insertRandomFields( - builder.contentType(), bytes, excludeFilter, random()); + BytesReference bytesWithRandomFields = XContentTestUtils.insertRandomFields(builder.contentType(), bytes, excludeFilter, random()); AckWatchResponse response = parse(builder.contentType(), bytesWithRandomFields); WatchStatus status = response.getStatus(); @@ -86,8 +85,7 @@ public void testParsingWithUnknownKeys() throws IOException { } private AckWatchResponse parse(XContentType contentType, BytesReference bytes) throws IOException { - XContentParser parser = XContentFactory.xContent(contentType) - .createParser(NamedXContentRegistry.EMPTY, null, bytes.streamInput()); + XContentParser parser = XContentFactory.xContent(contentType).createParser(NamedXContentRegistry.EMPTY, null, bytes.streamInput()); parser.nextToken(); return AckWatchResponse.fromXContent(parser); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/ActivateWatchResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/ActivateWatchResponseTests.java index 2efdf6225b432..7bb738a0648fc 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/ActivateWatchResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/ActivateWatchResponseTests.java @@ -9,14 +9,14 @@ package org.elasticsearch.client.watcher; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.XContentTestUtils; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParseException; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.test.XContentTestUtils; import java.io.IOException; import java.util.function.Predicate; @@ -31,15 +31,16 @@ public class ActivateWatchResponseTests extends ESTestCase { public void testBasicParsing() throws IOException { XContentType contentType = randomFrom(XContentType.values()); - XContentBuilder builder = XContentFactory.contentBuilder(contentType).startObject() + XContentBuilder builder = XContentFactory.contentBuilder(contentType) + .startObject() .startObject("status") - .field("version", 42) - .field("execution_state", ExecutionState.ACKNOWLEDGED) - .startObject("state") - .field("active", false) - .endObject() + .field("version", 42) + .field("execution_state", ExecutionState.ACKNOWLEDGED) + .startObject("state") + .field("active", false) + .endObject() .endObject() - .endObject(); + .endObject(); BytesReference bytes = BytesReference.bytes(builder); ActivateWatchResponse response = parse(builder.contentType(), bytes); @@ -60,9 +61,7 @@ public void testParsingWithMissingStatus() throws IOException { public void testParsingWithNullStatus() throws IOException { XContentType contentType = randomFrom(XContentType.values()); - XContentBuilder builder = XContentFactory.contentBuilder(contentType).startObject() - .nullField("status") - .endObject(); + XContentBuilder builder = XContentFactory.contentBuilder(contentType).startObject().nullField("status").endObject(); BytesReference bytes = BytesReference.bytes(builder); expectThrows(XContentParseException.class, () -> parse(builder.contentType(), bytes)); @@ -70,20 +69,20 @@ public void testParsingWithNullStatus() throws IOException { public void testParsingWithUnknownKeys() throws IOException { XContentType contentType = randomFrom(XContentType.values()); - XContentBuilder builder = XContentFactory.contentBuilder(contentType).startObject() + XContentBuilder builder = XContentFactory.contentBuilder(contentType) + .startObject() .startObject("status") - .field("version", 42) - .field("execution_state", ExecutionState.ACKNOWLEDGED) - .startObject("state") - .field("active", true) - .endObject() + .field("version", 42) + .field("execution_state", ExecutionState.ACKNOWLEDGED) + .startObject("state") + .field("active", true) + .endObject() .endObject() - .endObject(); + .endObject(); BytesReference bytes = BytesReference.bytes(builder); Predicate excludeFilter = field -> field.equals("status.actions"); - BytesReference bytesWithRandomFields = XContentTestUtils.insertRandomFields( - builder.contentType(), bytes, excludeFilter, random()); + BytesReference bytesWithRandomFields = XContentTestUtils.insertRandomFields(builder.contentType(), bytes, excludeFilter, random()); ActivateWatchResponse response = parse(builder.contentType(), bytesWithRandomFields); WatchStatus status = response.getStatus(); @@ -94,8 +93,7 @@ public void testParsingWithUnknownKeys() throws IOException { } private ActivateWatchResponse parse(XContentType contentType, BytesReference bytes) throws IOException { - XContentParser parser = XContentFactory.xContent(contentType) - .createParser(NamedXContentRegistry.EMPTY, null, bytes.streamInput()); + XContentParser parser = XContentFactory.xContent(contentType).createParser(NamedXContentRegistry.EMPTY, null, bytes.streamInput()); parser.nextToken(); return ActivateWatchResponse.fromXContent(parser); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeactivateWatchRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeactivateWatchRequestTests.java index 6e5a3d2fc305f..3aa3c579c06b0 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeactivateWatchRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeactivateWatchRequestTests.java @@ -21,8 +21,10 @@ public void testNullId() { } public void testInvalidId() { - IllegalArgumentException actual = expectThrows(IllegalArgumentException.class, - () -> new DeactivateWatchRequest("Watch id has spaces")); + IllegalArgumentException actual = expectThrows( + IllegalArgumentException.class, + () -> new DeactivateWatchRequest("Watch id has spaces") + ); assertNotNull(actual); assertThat(actual.getMessage(), is("watch id contains whitespace")); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeactivateWatchResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeactivateWatchResponseTests.java index be03357bf8c42..29ce5f7ef9793 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeactivateWatchResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeactivateWatchResponseTests.java @@ -7,14 +7,13 @@ */ package org.elasticsearch.client.watcher; - import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; import java.io.IOException; @@ -24,7 +23,8 @@ public void testBasicParsing() throws IOException { XContentType contentType = randomFrom(XContentType.values()); int version = randomInt(); ExecutionState executionState = randomFrom(ExecutionState.values()); - XContentBuilder builder = XContentFactory.contentBuilder(contentType).startObject() + XContentBuilder builder = XContentFactory.contentBuilder(contentType) + .startObject() .startObject("status") .field("version", version) .field("execution_state", executionState) @@ -39,8 +39,7 @@ public void testBasicParsing() throws IOException { } private DeactivateWatchResponse parse(XContentType contentType, BytesReference bytes) throws IOException { - XContentParser parser = XContentFactory.xContent(contentType) - .createParser(NamedXContentRegistry.EMPTY, null, bytes.streamInput()); + XContentParser parser = XContentFactory.xContent(contentType).createParser(NamedXContentRegistry.EMPTY, null, bytes.streamInput()); parser.nextToken(); return DeactivateWatchResponse.fromXContent(parser); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeleteWatchResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeleteWatchResponseTests.java index ddce1148a264d..f081a638df3ae 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeleteWatchResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeleteWatchResponseTests.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.watcher; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; @@ -17,13 +17,12 @@ public class DeleteWatchResponseTests extends ESTestCase { public void testFromXContent() throws IOException { - xContentTester(this::createParser, + xContentTester( + this::createParser, DeleteWatchResponseTests::createTestInstance, DeleteWatchResponseTests::toXContent, - DeleteWatchResponse::fromXContent) - .supportsUnknownFields(true) - .assertToXContentEquivalence(false) - .test(); + DeleteWatchResponse::fromXContent + ).supportsUnknownFields(true).assertToXContentEquivalence(false).test(); } private static XContentBuilder toXContent(DeleteWatchResponse response, XContentBuilder builder) throws IOException { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/GetWatchResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/GetWatchResponseTests.java index b9cd4d260e69e..43b211449bbba 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/GetWatchResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/GetWatchResponseTests.java @@ -78,18 +78,20 @@ private static BytesReference simpleWatch() { XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent()); builder.startObject() .startObject("trigger") - .startObject("schedule") - .field("interval", "10h") - .endObject() + .startObject("schedule") + .field("interval", "10h") + .endObject() .endObject() .startObject("input") - .startObject("none").endObject() + .startObject("none") + .endObject() .endObject() .startObject("actions") - .startObject("logme") - .field("text", "{{ctx.payload}}") - .endObject() - .endObject().endObject(); + .startObject("logme") + .field("text", "{{ctx.payload}}") + .endObject() + .endObject() + .endObject(); return BytesReference.bytes(builder); } catch (IOException e) { throw new AssertionError(e); @@ -144,15 +146,20 @@ private static WatchStatus convertWatchStatus(org.elasticsearch.client.watcher.W for (Map.Entry entry : status.getActions().entrySet()) { actions.put(entry.getKey(), convertActionStatus(entry.getValue())); } - return new WatchStatus(status.version(), + return new WatchStatus( + status.version(), convertWatchStatusState(status.state()), status.getExecutionState() == null ? null : convertWatchStatus(status.getExecutionState()), - status.lastChecked(), status.lastMetCondition(), actions, status.getHeaders() + status.lastChecked(), + status.lastMetCondition(), + actions, + status.getHeaders() ); } private static ActionStatus convertActionStatus(org.elasticsearch.client.watcher.ActionStatus actionStatus) { - return new ActionStatus(convertAckStatus(actionStatus.ackStatus()), + return new ActionStatus( + convertAckStatus(actionStatus.ackStatus()), actionStatus.lastExecution() == null ? null : convertActionStatusExecution(actionStatus.lastExecution()), actionStatus.lastSuccessfulExecution() == null ? null : convertActionStatusExecution(actionStatus.lastSuccessfulExecution()), actionStatus.lastThrottle() == null ? null : convertActionStatusThrottle(actionStatus.lastThrottle()) @@ -163,8 +170,7 @@ private static ActionStatus.AckStatus convertAckStatus(org.elasticsearch.client. return new ActionStatus.AckStatus(ackStatus.timestamp(), convertAckStatusState(ackStatus.state())); } - private static ActionStatus.AckStatus.State convertAckStatusState( - org.elasticsearch.client.watcher.ActionStatus.AckStatus.State state) { + private static ActionStatus.AckStatus.State convertAckStatusState(org.elasticsearch.client.watcher.ActionStatus.AckStatus.State state) { return ActionStatus.AckStatus.State.valueOf(state.name()); } @@ -176,8 +182,7 @@ private static ExecutionState convertWatchStatus(org.elasticsearch.client.watche return ExecutionState.valueOf(executionState.name()); } - private static ActionStatus.Execution convertActionStatusExecution( - org.elasticsearch.client.watcher.ActionStatus.Execution execution) { + private static ActionStatus.Execution convertActionStatusExecution(org.elasticsearch.client.watcher.ActionStatus.Execution execution) { if (execution.successful()) { return ActionStatus.Execution.successful(execution.timestamp()); } else { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/PutWatchResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/PutWatchResponseTests.java index cf2a9436b881b..4a45a0b74e495 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/PutWatchResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/PutWatchResponseTests.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.client.watcher; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; @@ -17,13 +17,12 @@ public class PutWatchResponseTests extends ESTestCase { public void testFromXContent() throws IOException { - xContentTester(this::createParser, + xContentTester( + this::createParser, PutWatchResponseTests::createTestInstance, PutWatchResponseTests::toXContent, - PutWatchResponse::fromXContent) - .supportsUnknownFields(true) - .assertToXContentEquivalence(false) - .test(); + PutWatchResponse::fromXContent + ).supportsUnknownFields(true).assertToXContentEquivalence(false).test(); } private static XContentBuilder toXContent(PutWatchResponse response, XContentBuilder builder) throws IOException { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/VerifyRepositoryResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/VerifyRepositoryResponseTests.java index ab35f693a6b64..b23362b718cfb 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/VerifyRepositoryResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/VerifyRepositoryResponseTests.java @@ -9,9 +9,9 @@ package org.elasticsearch.client.watcher; import org.elasticsearch.action.admin.cluster.repositories.verify.VerifyRepositoryResponse; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.test.ESTestCase; import java.io.IOException; import java.util.ArrayList; @@ -22,12 +22,13 @@ public class VerifyRepositoryResponseTests extends ESTestCase { public void testFromXContent() throws IOException { - xContentTester(this::createParser, + xContentTester( + this::createParser, VerifyRepositoryResponseTests::createTestInstance, VerifyRepositoryResponseTests::toXContent, - VerifyRepositoryResponse::fromXContent) - .supportsUnknownFields(true) - .shuffleFieldsExceptions(new String[] {"nodes"}) // do not mix up the order of nodes, it will cause the tests to fail + VerifyRepositoryResponse::fromXContent + ).supportsUnknownFields(true) + .shuffleFieldsExceptions(new String[] { "nodes" }) // do not mix up the order of nodes, it will cause the tests to fail .randomFieldsExcludeFilter((f) -> f.equals("nodes")) // everything in nodes needs to be a particular parseable object .assertToXContentEquivalence(false) .test(); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/WatchRequestValidationTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/WatchRequestValidationTests.java index 842f14ae8483c..ba66d7ec6fe3c 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/WatchRequestValidationTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/WatchRequestValidationTests.java @@ -10,8 +10,8 @@ import org.elasticsearch.client.ValidationException; import org.elasticsearch.common.bytes.BytesArray; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentType; import java.util.Optional; @@ -20,15 +20,13 @@ public class WatchRequestValidationTests extends ESTestCase { - public void testAcknowledgeWatchInvalidWatchId() { - ValidationException e = expectThrows(ValidationException.class, - () -> new AckWatchRequest("id with whitespaces")); + public void testAcknowledgeWatchInvalidWatchId() { + ValidationException e = expectThrows(ValidationException.class, () -> new AckWatchRequest("id with whitespaces")); assertThat(e.validationErrors(), hasItem("watch id contains whitespace")); } public void testAcknowledgeWatchInvalidActionId() { - ValidationException e = expectThrows(ValidationException.class, - () -> new AckWatchRequest("_id", "action id with whitespaces")); + ValidationException e = expectThrows(ValidationException.class, () -> new AckWatchRequest("_id", "action id with whitespaces")); assertThat(e.validationErrors(), hasItem("action id [action id with whitespaces] contains whitespace")); } @@ -40,50 +38,57 @@ public void testAcknowledgeWatchNullActionArray() { } public void testAcknowledgeWatchNullActionId() { - ValidationException e = expectThrows(ValidationException.class, - () -> new AckWatchRequest("_id", new String[] {null})); + ValidationException e = expectThrows(ValidationException.class, () -> new AckWatchRequest("_id", new String[] { null })); assertThat(e.validationErrors(), hasItem("action id may not be null")); } public void testDeleteWatchInvalidWatchId() { - final IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, - () -> new DeleteWatchRequest("id with whitespaces")); + final IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> new DeleteWatchRequest("id with whitespaces") + ); assertThat(exception.getMessage(), is("watch id contains whitespace")); } public void testDeleteWatchNullId() { - final NullPointerException exception = expectThrows(NullPointerException.class, - () -> new DeleteWatchRequest(null)); + final NullPointerException exception = expectThrows(NullPointerException.class, () -> new DeleteWatchRequest(null)); assertThat(exception.getMessage(), is("watch id is missing")); } public void testPutWatchInvalidWatchId() { - final IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, - () -> new PutWatchRequest("id with whitespaces", BytesArray.EMPTY, XContentType.JSON)); + final IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> new PutWatchRequest("id with whitespaces", BytesArray.EMPTY, XContentType.JSON) + ); assertThat(exception.getMessage(), is("watch id contains whitespace")); } public void testPutWatchNullId() { - final NullPointerException exception = expectThrows(NullPointerException.class, - () -> new PutWatchRequest(null, BytesArray.EMPTY, XContentType.JSON)); + final NullPointerException exception = expectThrows( + NullPointerException.class, + () -> new PutWatchRequest(null, BytesArray.EMPTY, XContentType.JSON) + ); assertThat(exception.getMessage(), is("watch id is missing")); } public void testPutWatchSourceNull() { - final NullPointerException exception = expectThrows(NullPointerException.class, - () -> new PutWatchRequest("foo", null, XContentType.JSON)); + final NullPointerException exception = expectThrows( + NullPointerException.class, + () -> new PutWatchRequest("foo", null, XContentType.JSON) + ); assertThat(exception.getMessage(), is("watch source is missing")); } public void testPutWatchContentNull() { - final NullPointerException exception = expectThrows(NullPointerException.class, - () -> new PutWatchRequest("foo", BytesArray.EMPTY, null)); + final NullPointerException exception = expectThrows( + NullPointerException.class, + () -> new PutWatchRequest("foo", BytesArray.EMPTY, null) + ); assertThat(exception.getMessage(), is("request body is missing")); } - public void testGetWatchInvalidWatchId() { - ValidationException e = expectThrows(ValidationException.class, - () -> new GetWatchRequest("id with whitespaces")); + public void testGetWatchInvalidWatchId() { + ValidationException e = expectThrows(ValidationException.class, () -> new GetWatchRequest("id with whitespaces")); assertThat(e.validationErrors(), hasItem("watch id contains whitespace")); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/WatchStatusTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/WatchStatusTests.java index cbfe634a32fc6..4e1aeb2a08d79 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/WatchStatusTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/WatchStatusTests.java @@ -9,13 +9,13 @@ package org.elasticsearch.client.watcher; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.XContentTestUtils; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.test.XContentTestUtils; import java.io.IOException; import java.time.Instant; @@ -31,8 +31,7 @@ public void testBasicParsing() throws IOException { boolean expectedActive = randomBoolean(); ActionStatus.AckStatus.State expectedAckState = randomFrom(ActionStatus.AckStatus.State.values()); - XContentBuilder builder = createTestXContent(expectedVersion, expectedExecutionState, - expectedActive, expectedAckState); + XContentBuilder builder = createTestXContent(expectedVersion, expectedExecutionState, expectedActive, expectedAckState); BytesReference bytes = BytesReference.bytes(builder); WatchStatus watchStatus = parse(builder.contentType(), bytes); @@ -75,13 +74,11 @@ public void testParsingWithUnknownKeys() throws IOException { boolean expectedActive = randomBoolean(); ActionStatus.AckStatus.State expectedAckState = randomFrom(ActionStatus.AckStatus.State.values()); - XContentBuilder builder = createTestXContent(expectedVersion, expectedExecutionState, - expectedActive, expectedAckState); + XContentBuilder builder = createTestXContent(expectedVersion, expectedExecutionState, expectedActive, expectedAckState); BytesReference bytes = BytesReference.bytes(builder); Predicate excludeFilter = field -> field.equals("actions"); - BytesReference bytesWithRandomFields = XContentTestUtils.insertRandomFields( - builder.contentType(), bytes, excludeFilter, random()); + BytesReference bytesWithRandomFields = XContentTestUtils.insertRandomFields(builder.contentType(), bytes, excludeFilter, random()); WatchStatus watchStatus = parse(builder.contentType(), bytesWithRandomFields); @@ -91,22 +88,23 @@ public void testParsingWithUnknownKeys() throws IOException { public void testOptionalFieldsParsing() throws IOException { XContentType contentType = randomFrom(XContentType.values()); - XContentBuilder builder = XContentFactory.contentBuilder(contentType).startObject() + XContentBuilder builder = XContentFactory.contentBuilder(contentType) + .startObject() .field("version", 42) .startObject("actions") - .startObject("test_index") - .startObject("ack") - .field("timestamp", "2015-05-26T18:04:27.763Z") - .field("state", "ackable") - .endObject() - .startObject("last_execution") - .field("timestamp", "2015-05-25T18:04:27.733Z") - .field("successful", false) - .field("reason", "failed to send email") - .endObject() - .endObject() + .startObject("test_index") + .startObject("ack") + .field("timestamp", "2015-05-26T18:04:27.763Z") + .field("state", "ackable") + .endObject() + .startObject("last_execution") + .field("timestamp", "2015-05-25T18:04:27.733Z") + .field("successful", false) + .field("reason", "failed to send email") + .endObject() .endObject() - .endObject(); + .endObject() + .endObject(); BytesReference bytes = BytesReference.bytes(builder); WatchStatus watchStatus = parse(builder.contentType(), bytes); @@ -116,47 +114,49 @@ public void testOptionalFieldsParsing() throws IOException { assertFalse(watchStatus.checked()); } - private XContentBuilder createTestXContent(int version, - ExecutionState executionState, - boolean active, - ActionStatus.AckStatus.State ackState) throws IOException { + private XContentBuilder createTestXContent( + int version, + ExecutionState executionState, + boolean active, + ActionStatus.AckStatus.State ackState + ) throws IOException { XContentType contentType = randomFrom(XContentType.values()); - return XContentFactory.contentBuilder(contentType).startObject() + return XContentFactory.contentBuilder(contentType) + .startObject() .field("version", version) .field("execution_state", executionState) .field("last_checked", 1432663467763L) .field("last_met_condition", "2015-05-26T18:04:27.763Z") .startObject("state") - .field("active", active) - .field("timestamp", "2015-05-26T18:04:27.723Z") + .field("active", active) + .field("timestamp", "2015-05-26T18:04:27.723Z") .endObject() .startObject("actions") - .startObject("test_index") - .startObject("ack") - .field("timestamp", "2015-05-26T18:04:27.763Z") - .field("state", ackState) - .endObject() - .startObject("last_execution") - .field("timestamp", "2015-05-25T18:04:27.733Z") - .field("successful", false) - .field("reason", "failed to send email") - .endObject() - .startObject("last_successful_execution") - .field("timestamp", "2015-05-25T18:04:27.773Z") - .field("successful", true) - .endObject() - .startObject("last_throttle") - .field("timestamp", "2015-04-25T18:05:23.445Z") - .field("reason", "throttling interval is set to [5 seconds] ...") - .endObject() - .endObject() + .startObject("test_index") + .startObject("ack") + .field("timestamp", "2015-05-26T18:04:27.763Z") + .field("state", ackState) + .endObject() + .startObject("last_execution") + .field("timestamp", "2015-05-25T18:04:27.733Z") + .field("successful", false) + .field("reason", "failed to send email") + .endObject() + .startObject("last_successful_execution") + .field("timestamp", "2015-05-25T18:04:27.773Z") + .field("successful", true) + .endObject() + .startObject("last_throttle") + .field("timestamp", "2015-04-25T18:05:23.445Z") + .field("reason", "throttling interval is set to [5 seconds] ...") + .endObject() + .endObject() .endObject() - .endObject(); + .endObject(); } private WatchStatus parse(XContentType contentType, BytesReference bytes) throws IOException { - XContentParser parser = XContentFactory.xContent(contentType) - .createParser(NamedXContentRegistry.EMPTY, null, bytes.streamInput()); + XContentParser parser = XContentFactory.xContent(contentType).createParser(NamedXContentRegistry.EMPTY, null, bytes.streamInput()); parser.nextToken(); return WatchStatus.parse(parser); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/WatcherStatsResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/WatcherStatsResponseTests.java index 57609d0bd2f70..022429d15d413 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/WatcherStatsResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/WatcherStatsResponseTests.java @@ -10,8 +10,8 @@ import org.elasticsearch.client.NodesResponseHeader; import org.elasticsearch.client.NodesResponseHeaderTestUtils; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.time.Instant; @@ -29,11 +29,7 @@ public class WatcherStatsResponseTests extends ESTestCase { public void testFromXContent() throws IOException { - xContentTester( - this::createParser, - this::createTestInstance, - this::toXContent, - WatcherStatsResponse::fromXContent) + xContentTester(this::createParser, this::createTestInstance, this::toXContent, WatcherStatsResponse::fromXContent) .supportsUnknownFields(true) .randomFieldsExcludeFilter(field -> field.endsWith("stats")) .test(); @@ -142,21 +138,33 @@ protected WatcherStatsResponse createTestInstance() { stackTrace[k] = randomAlphaOfLength(10); } } - snapshots.add(new WatchExecutionSnapshot(randomAlphaOfLength(10), randomAlphaOfLength(10), - ZonedDateTime.ofInstant(Instant.ofEpochMilli(randomInt()), ZoneOffset.UTC), - ZonedDateTime.ofInstant(Instant.ofEpochMilli(randomInt()), ZoneOffset.UTC), - randomFrom(ExecutionPhase.values()), actions, stackTrace)); + snapshots.add( + new WatchExecutionSnapshot( + randomAlphaOfLength(10), + randomAlphaOfLength(10), + ZonedDateTime.ofInstant(Instant.ofEpochMilli(randomInt()), ZoneOffset.UTC), + ZonedDateTime.ofInstant(Instant.ofEpochMilli(randomInt()), ZoneOffset.UTC), + randomFrom(ExecutionPhase.values()), + actions, + stackTrace + ) + ); } } List queuedWatches = null; - if(randomBoolean()) { + if (randomBoolean()) { int queuedWatchCount = randomInt(10); queuedWatches = new ArrayList<>(queuedWatchCount); - for (int j=0; j(statsCount); - for (int j=0; j { + org.elasticsearch.protocol.xpack.watcher.DeleteWatchResponse, + DeleteWatchResponse> { @Override protected org.elasticsearch.protocol.xpack.watcher.DeleteWatchResponse createServerTestInstance(XContentType xContentType) { @@ -33,8 +34,10 @@ protected DeleteWatchResponse doParseToClientInstance(XContentParser parser) thr } @Override - protected void assertInstances(org.elasticsearch.protocol.xpack.watcher.DeleteWatchResponse serverTestInstance, - DeleteWatchResponse clientInstance) { + protected void assertInstances( + org.elasticsearch.protocol.xpack.watcher.DeleteWatchResponse serverTestInstance, + DeleteWatchResponse clientInstance + ) { assertThat(clientInstance.getId(), equalTo(serverTestInstance.getId())); assertThat(clientInstance.getVersion(), equalTo(serverTestInstance.getVersion())); assertThat(clientInstance.isFound(), equalTo(serverTestInstance.isFound())); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/hlrc/ExecuteWatchResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/hlrc/ExecuteWatchResponseTests.java index eb1a7d15e366d..2f77807017438 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/hlrc/ExecuteWatchResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/hlrc/ExecuteWatchResponseTests.java @@ -21,7 +21,8 @@ import static org.hamcrest.Matchers.equalTo; public class ExecuteWatchResponseTests extends AbstractResponseTestCase< - ExecuteWatchResponse, org.elasticsearch.client.watcher.ExecuteWatchResponse> { + ExecuteWatchResponse, + org.elasticsearch.client.watcher.ExecuteWatchResponse> { @Override protected ExecuteWatchResponse createServerTestInstance(XContentType xContentType) { @@ -40,8 +41,7 @@ protected ExecuteWatchResponse createServerTestInstance(XContentType xContentTyp builder.endObject(); BytesReference bytes = BytesReference.bytes(builder); return new ExecuteWatchResponse(id, bytes, XContentType.JSON); - } - catch (IOException e) { + } catch (IOException e) { throw new AssertionError(e); } } @@ -52,8 +52,10 @@ protected org.elasticsearch.client.watcher.ExecuteWatchResponse doParseToClientI } @Override - protected void assertInstances(ExecuteWatchResponse serverTestInstance, - org.elasticsearch.client.watcher.ExecuteWatchResponse clientInstance) { + protected void assertInstances( + ExecuteWatchResponse serverTestInstance, + org.elasticsearch.client.watcher.ExecuteWatchResponse clientInstance + ) { assertThat(clientInstance.getRecordId(), equalTo(serverTestInstance.getRecordId())); assertThat(clientInstance.getRecordAsMap(), equalTo(serverTestInstance.getRecordSource().getAsMap())); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/hlrc/PutWatchResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/hlrc/PutWatchResponseTests.java index 2234350fe8324..c54d6795af9aa 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/hlrc/PutWatchResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/hlrc/PutWatchResponseTests.java @@ -17,7 +17,8 @@ import static org.hamcrest.Matchers.equalTo; public class PutWatchResponseTests extends AbstractResponseTestCase< - org.elasticsearch.protocol.xpack.watcher.PutWatchResponse, PutWatchResponse> { + org.elasticsearch.protocol.xpack.watcher.PutWatchResponse, + PutWatchResponse> { @Override protected org.elasticsearch.protocol.xpack.watcher.PutWatchResponse createServerTestInstance(XContentType xContentType) { @@ -35,8 +36,10 @@ protected PutWatchResponse doParseToClientInstance(XContentParser parser) throws } @Override - protected void assertInstances(org.elasticsearch.protocol.xpack.watcher.PutWatchResponse serverTestInstance, - PutWatchResponse clientInstance) { + protected void assertInstances( + org.elasticsearch.protocol.xpack.watcher.PutWatchResponse serverTestInstance, + PutWatchResponse clientInstance + ) { assertThat(clientInstance.getId(), equalTo(serverTestInstance.getId())); assertThat(clientInstance.getSeqNo(), equalTo(serverTestInstance.getSeqNo())); assertThat(clientInstance.getPrimaryTerm(), equalTo(serverTestInstance.getPrimaryTerm())); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/test/RequestMatcher.java b/client/rest-high-level/src/test/java/org/elasticsearch/test/RequestMatcher.java index 62f217321ba2a..61b84f1204fa8 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/test/RequestMatcher.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/test/RequestMatcher.java @@ -36,10 +36,6 @@ public boolean matches(Object actual) { @Override public void describeTo(Description description) { - description - .appendText("request to ") - .appendText(method) - .appendText(" ") - .appendText(endpoint); + description.appendText("request to ").appendText(method).appendText(" ").appendText(endpoint); } } diff --git a/client/rest/src/main/java/org/elasticsearch/client/Cancellable.java b/client/rest/src/main/java/org/elasticsearch/client/Cancellable.java index 4fafc4ba124da..eb3060aea224a 100644 --- a/client/rest/src/main/java/org/elasticsearch/client/Cancellable.java +++ b/client/rest/src/main/java/org/elasticsearch/client/Cancellable.java @@ -45,8 +45,7 @@ public abstract class Cancellable { static final Cancellable NO_OP = new Cancellable() { @Override - public void cancel() { - } + public void cancel() {} @Override void runIfNotCancelled(Runnable runnable) { diff --git a/client/rest/src/main/java/org/elasticsearch/client/DeadHostState.java b/client/rest/src/main/java/org/elasticsearch/client/DeadHostState.java index 2958379de1251..1858644ce5a07 100644 --- a/client/rest/src/main/java/org/elasticsearch/client/DeadHostState.java +++ b/client/rest/src/main/java/org/elasticsearch/client/DeadHostState.java @@ -58,8 +58,10 @@ final class DeadHostState implements Comparable { * @param previousDeadHostState the previous state of the host which allows us to increase the wait till the next retry attempt */ DeadHostState(DeadHostState previousDeadHostState) { - long timeoutNanos = (long)Math.min(MIN_CONNECTION_TIMEOUT_NANOS * 2 * Math.pow(2, previousDeadHostState.failedAttempts * 0.5 - 1), - MAX_CONNECTION_TIMEOUT_NANOS); + long timeoutNanos = (long) Math.min( + MIN_CONNECTION_TIMEOUT_NANOS * 2 * Math.pow(2, previousDeadHostState.failedAttempts * 0.5 - 1), + MAX_CONNECTION_TIMEOUT_NANOS + ); this.deadUntilNanos = previousDeadHostState.timeSupplier.get() + timeoutNanos; this.failedAttempts = previousDeadHostState.failedAttempts + 1; this.timeSupplier = previousDeadHostState.timeSupplier; @@ -89,18 +91,22 @@ int getFailedAttempts() { @Override public int compareTo(DeadHostState other) { if (timeSupplier != other.timeSupplier) { - throw new IllegalArgumentException("can't compare DeadHostStates holding different time suppliers as they may " + - "be based on different clocks"); + throw new IllegalArgumentException( + "can't compare DeadHostStates holding different time suppliers as they may " + "be based on different clocks" + ); } return Long.compare(deadUntilNanos, other.deadUntilNanos); } @Override public String toString() { - return "DeadHostState{" + - "failedAttempts=" + failedAttempts + - ", deadUntilNanos=" + deadUntilNanos + - ", timeSupplier=" + timeSupplier + - '}'; + return "DeadHostState{" + + "failedAttempts=" + + failedAttempts + + ", deadUntilNanos=" + + deadUntilNanos + + ", timeSupplier=" + + timeSupplier + + '}'; } } diff --git a/client/rest/src/main/java/org/elasticsearch/client/HasAttributeNodeSelector.java b/client/rest/src/main/java/org/elasticsearch/client/HasAttributeNodeSelector.java index ca1ef1b7b4cac..761249c8fe53c 100644 --- a/client/rest/src/main/java/org/elasticsearch/client/HasAttributeNodeSelector.java +++ b/client/rest/src/main/java/org/elasticsearch/client/HasAttributeNodeSelector.java @@ -59,8 +59,7 @@ public boolean equals(Object o) { return false; } HasAttributeNodeSelector that = (HasAttributeNodeSelector) o; - return Objects.equals(key, that.key) && - Objects.equals(value, that.value); + return Objects.equals(key, that.key) && Objects.equals(value, that.value); } @Override diff --git a/client/rest/src/main/java/org/elasticsearch/client/HeapBufferedAsyncResponseConsumer.java b/client/rest/src/main/java/org/elasticsearch/client/HeapBufferedAsyncResponseConsumer.java index a45400883726e..685224d227c71 100644 --- a/client/rest/src/main/java/org/elasticsearch/client/HeapBufferedAsyncResponseConsumer.java +++ b/client/rest/src/main/java/org/elasticsearch/client/HeapBufferedAsyncResponseConsumer.java @@ -73,8 +73,9 @@ protected void onResponseReceived(HttpResponse httpResponse) throws HttpExceptio protected void onEntityEnclosed(HttpEntity entity, ContentType contentType) throws IOException { long len = entity.getContentLength(); if (len > bufferLimitBytes) { - throw new ContentTooLongException("entity content is too long [" + len + - "] for the configured buffer limit [" + bufferLimitBytes + "]"); + throw new ContentTooLongException( + "entity content is too long [" + len + "] for the configured buffer limit [" + bufferLimitBytes + "]" + ); } if (len < 0) { len = 4096; diff --git a/client/rest/src/main/java/org/elasticsearch/client/HttpAsyncResponseConsumerFactory.java b/client/rest/src/main/java/org/elasticsearch/client/HttpAsyncResponseConsumerFactory.java index 9f275bb16cbc4..a0ecf9961b6da 100644 --- a/client/rest/src/main/java/org/elasticsearch/client/HttpAsyncResponseConsumerFactory.java +++ b/client/rest/src/main/java/org/elasticsearch/client/HttpAsyncResponseConsumerFactory.java @@ -48,7 +48,7 @@ public interface HttpAsyncResponseConsumerFactory { */ class HeapBufferedResponseConsumerFactory implements HttpAsyncResponseConsumerFactory { - //default buffer limit is 100MB + // default buffer limit is 100MB static final int DEFAULT_BUFFER_LIMIT = 100 * 1024 * 1024; private final int bufferLimit; diff --git a/client/rest/src/main/java/org/elasticsearch/client/LanguageRuntimeVersions.java b/client/rest/src/main/java/org/elasticsearch/client/LanguageRuntimeVersions.java index 466e116d54d8c..663f42d4475af 100644 --- a/client/rest/src/main/java/org/elasticsearch/client/LanguageRuntimeVersions.java +++ b/client/rest/src/main/java/org/elasticsearch/client/LanguageRuntimeVersions.java @@ -34,7 +34,7 @@ public static String getRuntimeMetadata() { StringBuilder s = new StringBuilder(); String version; - version= kotlinVersion(); + version = kotlinVersion(); if (version != null) { s.append(",kt=").append(version); } @@ -63,7 +63,7 @@ public static String getRuntimeMetadata() { } public static String kotlinVersion() { - //KotlinVersion.CURRENT.toString() + // KotlinVersion.CURRENT.toString() return keepMajorMinor(getStaticField("kotlin.KotlinVersion", "CURRENT")); } diff --git a/client/rest/src/main/java/org/elasticsearch/client/Node.java b/client/rest/src/main/java/org/elasticsearch/client/Node.java index 437b6760fabe2..b46587617972c 100644 --- a/client/rest/src/main/java/org/elasticsearch/client/Node.java +++ b/client/rest/src/main/java/org/elasticsearch/client/Node.java @@ -65,8 +65,7 @@ public class Node { * {@code host} are nullable and implementations of {@link NodeSelector} * need to decide what to do in their absence. */ - public Node(HttpHost host, Set boundHosts, String name, String version, - Roles roles, Map> attributes) { + public Node(HttpHost host, Set boundHosts, String name, String version, Roles roles, Map> attributes) { if (host == null) { throw new IllegalArgumentException("host cannot be null"); } @@ -189,6 +188,7 @@ public Roles(final Set roles) { public boolean isMasterEligible() { return roles.contains("master"); } + /** * Returns whether or not the node stores data. * @deprecated use {@link #hasDataRole()} or {@link #canContainData()} @@ -246,6 +246,7 @@ public boolean hasDataFrozenRole() { public boolean canContainData() { return hasDataRole() || roles.stream().anyMatch(role -> role.startsWith("data_")); } + /** * Returns whether or not the node runs ingest pipelines. */ diff --git a/client/rest/src/main/java/org/elasticsearch/client/NodeSelector.java b/client/rest/src/main/java/org/elasticsearch/client/NodeSelector.java index dbdb3aa006867..8060ecc8ad066 100644 --- a/client/rest/src/main/java/org/elasticsearch/client/NodeSelector.java +++ b/client/rest/src/main/java/org/elasticsearch/client/NodeSelector.java @@ -75,8 +75,8 @@ public void select(Iterable nodes) { Node node = itr.next(); if (node.getRoles() == null) continue; if (node.getRoles().isMasterEligible() - && false == node.getRoles().canContainData() - && false == node.getRoles().isIngest()) { + && false == node.getRoles().canContainData() + && false == node.getRoles().isIngest()) { itr.remove(); } } diff --git a/client/rest/src/main/java/org/elasticsearch/client/PersistentCredentialsAuthenticationStrategy.java b/client/rest/src/main/java/org/elasticsearch/client/PersistentCredentialsAuthenticationStrategy.java index 79518f750ed9c..b31f206ee3193 100644 --- a/client/rest/src/main/java/org/elasticsearch/client/PersistentCredentialsAuthenticationStrategy.java +++ b/client/rest/src/main/java/org/elasticsearch/client/PersistentCredentialsAuthenticationStrategy.java @@ -48,8 +48,13 @@ final class PersistentCredentialsAuthenticationStrategy extends TargetAuthentica @Override public void authFailed(HttpHost host, AuthScheme authScheme, HttpContext context) { if (logger.isDebugEnabled()) { - logger.debug("Authentication to " + host + " failed (scheme: " + authScheme.getSchemeName() - + "). Preserving credentials for next request"); + logger.debug( + "Authentication to " + + host + + " failed (scheme: " + + authScheme.getSchemeName() + + "). Preserving credentials for next request" + ); } // Do nothing. // The superclass implementation of method will clear the credentials from the cache, but we don't diff --git a/client/rest/src/main/java/org/elasticsearch/client/PreferHasAttributeNodeSelector.java b/client/rest/src/main/java/org/elasticsearch/client/PreferHasAttributeNodeSelector.java index 289380c418ea5..1d4bc7fddeb97 100644 --- a/client/rest/src/main/java/org/elasticsearch/client/PreferHasAttributeNodeSelector.java +++ b/client/rest/src/main/java/org/elasticsearch/client/PreferHasAttributeNodeSelector.java @@ -89,8 +89,7 @@ public boolean equals(Object o) { return false; } PreferHasAttributeNodeSelector that = (PreferHasAttributeNodeSelector) o; - return Objects.equals(key, that.key) && - Objects.equals(value, that.value); + return Objects.equals(key, that.key) && Objects.equals(value, that.value); } @Override diff --git a/client/rest/src/main/java/org/elasticsearch/client/Request.java b/client/rest/src/main/java/org/elasticsearch/client/Request.java index 8e55efecdf1bc..6423bee1cb44e 100644 --- a/client/rest/src/main/java/org/elasticsearch/client/Request.java +++ b/client/rest/src/main/java/org/elasticsearch/client/Request.java @@ -81,7 +81,7 @@ public void addParameter(String name, String value) { } } - public void addParameters(Map paramSource){ + public void addParameters(Map paramSource) { paramSource.forEach(this::addParameter); } @@ -174,10 +174,10 @@ public boolean equals(Object obj) { Request other = (Request) obj; return method.equals(other.method) - && endpoint.equals(other.endpoint) - && parameters.equals(other.parameters) - && Objects.equals(entity, other.entity) - && options.equals(other.options); + && endpoint.equals(other.endpoint) + && parameters.equals(other.parameters) + && Objects.equals(entity, other.entity) + && options.equals(other.options); } @Override diff --git a/client/rest/src/main/java/org/elasticsearch/client/RequestLogger.java b/client/rest/src/main/java/org/elasticsearch/client/RequestLogger.java index d061dc622fcaf..085bc5619451f 100644 --- a/client/rest/src/main/java/org/elasticsearch/client/RequestLogger.java +++ b/client/rest/src/main/java/org/elasticsearch/client/RequestLogger.java @@ -48,16 +48,23 @@ final class RequestLogger { private static final Log tracer = LogFactory.getLog("tracer"); - private RequestLogger() { - } + private RequestLogger() {} /** * Logs a request that yielded a response */ static void logResponse(Log logger, HttpUriRequest request, HttpHost host, HttpResponse httpResponse) { if (logger.isDebugEnabled()) { - logger.debug("request [" + request.getMethod() + " " + host + getUri(request.getRequestLine()) + - "] returned [" + httpResponse.getStatusLine() + "]"); + logger.debug( + "request [" + + request.getMethod() + + " " + + host + + getUri(request.getRequestLine()) + + "] returned [" + + httpResponse.getStatusLine() + + "]" + ); } if (logger.isWarnEnabled()) { Header[] warnings = httpResponse.getHeaders("Warning"); @@ -69,14 +76,14 @@ static void logResponse(Log logger, HttpUriRequest request, HttpHost host, HttpR String requestLine; try { requestLine = buildTraceRequest(request, host); - } catch(IOException e) { + } catch (IOException e) { requestLine = ""; tracer.trace("error while reading request for trace purposes", e); } String responseLine; try { responseLine = buildTraceResponse(httpResponse); - } catch(IOException e) { + } catch (IOException e) { responseLine = ""; tracer.trace("error while reading response for trace purposes", e); } @@ -104,8 +111,13 @@ static void logFailedRequest(Log logger, HttpUriRequest request, Node node, Exce } static String buildWarningMessage(HttpUriRequest request, HttpHost host, Header[] warnings) { - StringBuilder message = new StringBuilder("request [").append(request.getMethod()).append(" ").append(host) - .append(getUri(request.getRequestLine())).append("] returned ").append(warnings.length).append(" warnings: "); + StringBuilder message = new StringBuilder("request [").append(request.getMethod()) + .append(" ") + .append(host) + .append(getUri(request.getRequestLine())) + .append("] returned ") + .append(warnings.length) + .append(" warnings: "); for (int i = 0; i < warnings.length; i++) { if (i > 0) { message.append(","); @@ -120,7 +132,7 @@ static String buildWarningMessage(HttpUriRequest request, HttpHost host, Header[ */ static String buildTraceRequest(HttpUriRequest request, HttpHost host) throws IOException { String requestLine = "curl -iX " + request.getMethod() + " '" + host + getUri(request.getRequestLine()) + "'"; - if (request instanceof HttpEntityEnclosingRequest) { + if (request instanceof HttpEntityEnclosingRequest) { HttpEntityEnclosingRequest enclosingRequest = (HttpEntityEnclosingRequest) request; if (enclosingRequest.getEntity() != null) { requestLine += " -d '"; @@ -158,7 +170,7 @@ static String buildTraceResponse(HttpResponse httpResponse) throws IOException { } try (BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), charset))) { String line; - while( (line = reader.readLine()) != null) { + while ((line = reader.readLine()) != null) { responseLine.append("\n# ").append(line); } } diff --git a/client/rest/src/main/java/org/elasticsearch/client/RequestOptions.java b/client/rest/src/main/java/org/elasticsearch/client/RequestOptions.java index 6ddd3fa557966..bce3cd26eed1c 100644 --- a/client/rest/src/main/java/org/elasticsearch/client/RequestOptions.java +++ b/client/rest/src/main/java/org/elasticsearch/client/RequestOptions.java @@ -41,7 +41,12 @@ public final class RequestOptions { * Default request options. */ public static final RequestOptions DEFAULT = new Builder( - Collections.emptyList(), Collections.emptyMap(), HeapBufferedResponseConsumerFactory.DEFAULT, null, null).build(); + Collections.emptyList(), + Collections.emptyMap(), + HeapBufferedResponseConsumerFactory.DEFAULT, + null, + null + ).build(); private final List
    headers; private final Map parameters; @@ -161,8 +166,8 @@ public boolean equals(Object obj) { RequestOptions other = (RequestOptions) obj; return headers.equals(other.headers) - && httpAsyncResponseConsumerFactory.equals(other.httpAsyncResponseConsumerFactory) - && Objects.equals(warningsHandler, other.warningsHandler); + && httpAsyncResponseConsumerFactory.equals(other.httpAsyncResponseConsumerFactory) + && Objects.equals(warningsHandler, other.warningsHandler); } @Override @@ -182,9 +187,13 @@ public static class Builder { private WarningsHandler warningsHandler; private RequestConfig requestConfig; - private Builder(List
    headers, Map parameters, - HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory, - WarningsHandler warningsHandler, RequestConfig requestConfig) { + private Builder( + List
    headers, + Map parameters, + HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory, + WarningsHandler warningsHandler, + RequestConfig requestConfig + ) { this.headers = new ArrayList<>(headers); this.parameters = new HashMap<>(parameters); this.httpAsyncResponseConsumerFactory = httpAsyncResponseConsumerFactory; @@ -242,8 +251,10 @@ public Builder addParameter(String key, String value) { * client side. */ public Builder setHttpAsyncResponseConsumerFactory(HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory) { - this.httpAsyncResponseConsumerFactory = - Objects.requireNonNull(httpAsyncResponseConsumerFactory, "httpAsyncResponseConsumerFactory cannot be null"); + this.httpAsyncResponseConsumerFactory = Objects.requireNonNull( + httpAsyncResponseConsumerFactory, + "httpAsyncResponseConsumerFactory cannot be null" + ); return this; } @@ -298,8 +309,7 @@ public boolean equals(Object other) { } if (other instanceof ReqHeader) { Header otherHeader = (Header) other; - return Objects.equals(getName(), otherHeader.getName()) && - Objects.equals(getValue(), otherHeader.getValue()); + return Objects.equals(getName(), otherHeader.getName()) && Objects.equals(getValue(), otherHeader.getValue()); } return false; } diff --git a/client/rest/src/main/java/org/elasticsearch/client/Response.java b/client/rest/src/main/java/org/elasticsearch/client/Response.java index cfe313c744c52..9a1f21a6de64e 100644 --- a/client/rest/src/main/java/org/elasticsearch/client/Response.java +++ b/client/rest/src/main/java/org/elasticsearch/client/Response.java @@ -105,17 +105,16 @@ public HttpEntity getEntity() { * format (with quotes and leading space). Start/end of line characters and * atomic groups are used to prevent backtracking. */ - private static final Pattern WARNING_HEADER_DATE_PATTERN = Pattern.compile( - "^ " + // start of line, leading space - // quoted RFC 1123 date format - "\"" + // opening quote - "(?>Mon|Tue|Wed|Thu|Fri|Sat|Sun), " + // day of week, atomic group to prevent backtracking - "\\d{2} " + // 2-digit day - "(?>Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) " + // month, atomic group to prevent backtracking - "\\d{4} " + // 4-digit year - "\\d{2}:\\d{2}:\\d{2} " + // (two-digit hour):(two-digit minute):(two-digit second) - "GMT" + // GMT - "\"$"); // closing quote (optional, since an older version can still send a warn-date), end of line + private static final Pattern WARNING_HEADER_DATE_PATTERN = Pattern.compile("^ " + // start of line, leading space + // quoted RFC 1123 date format + "\"" + // opening quote + "(?>Mon|Tue|Wed|Thu|Fri|Sat|Sun), " + // day of week, atomic group to prevent backtracking + "\\d{2} " + // 2-digit day + "(?>Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) " + // month, atomic group to prevent backtracking + "\\d{4} " + // 4-digit year + "\\d{2}:\\d{2}:\\d{2} " + // (two-digit hour):(two-digit minute):(two-digit second) + "GMT" + // GMT + "\"$"); // closing quote (optional, since an older version can still send a warn-date), end of line /** * Length of RFC 1123 format (with quotes and leading space), used in @@ -202,10 +201,6 @@ HttpResponse getHttpResponse() { @Override public String toString() { - return "Response{" + - "requestLine=" + requestLine + - ", host=" + host + - ", response=" + response.getStatusLine() + - '}'; + return "Response{" + "requestLine=" + requestLine + ", host=" + host + ", response=" + response.getStatusLine() + '}'; } } diff --git a/client/rest/src/main/java/org/elasticsearch/client/ResponseException.java b/client/rest/src/main/java/org/elasticsearch/client/ResponseException.java index b4340755d09a9..52d59019a5fc0 100644 --- a/client/rest/src/main/java/org/elasticsearch/client/ResponseException.java +++ b/client/rest/src/main/java/org/elasticsearch/client/ResponseException.java @@ -40,7 +40,8 @@ public ResponseException(Response response) throws IOException { } static String buildMessage(Response response) throws IOException { - String message = String.format(Locale.ROOT, + String message = String.format( + Locale.ROOT, "method [%s], host [%s], URI [%s], status line [%s]", response.getRequestLine().getMethod(), response.getHost(), diff --git a/client/rest/src/main/java/org/elasticsearch/client/RestClient.java b/client/rest/src/main/java/org/elasticsearch/client/RestClient.java index c6a4236e9f841..562d1bc22d359 100644 --- a/client/rest/src/main/java/org/elasticsearch/client/RestClient.java +++ b/client/rest/src/main/java/org/elasticsearch/client/RestClient.java @@ -80,6 +80,7 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; import java.util.zip.GZIPOutputStream; + import javax.net.ssl.SSLHandshakeException; import static java.nio.charset.StandardCharsets.UTF_8; @@ -118,9 +119,16 @@ public class RestClient implements Closeable { private final WarningsHandler warningsHandler; private final boolean compressionEnabled; - RestClient(CloseableHttpAsyncClient client, Header[] defaultHeaders, List nodes, String pathPrefix, - FailureListener failureListener, NodeSelector nodeSelector, boolean strictDeprecationMode, - boolean compressionEnabled) { + RestClient( + CloseableHttpAsyncClient client, + Header[] defaultHeaders, + List nodes, + String pathPrefix, + FailureListener failureListener, + NodeSelector nodeSelector, + boolean strictDeprecationMode, + boolean compressionEnabled + ) { this.client = client; this.defaultHeaders = Collections.unmodifiableList(Arrays.asList(defaultHeaders)); this.failureListener = failureListener; @@ -171,7 +179,7 @@ public static RestClientBuilder builder(String cloudId) { port = 443; } - String url = decodedParts[1] + "." + domain; + String url = decodedParts[1] + "." + domain; return builder(new HttpHost(url, port, "https")); } @@ -218,8 +226,7 @@ public synchronized void setNodes(Collection nodes) { nodesByHost.put(node.getHost(), node); authCache.put(node.getHost(), new BasicScheme()); } - this.nodeTuple = new NodeTuple<>( - Collections.unmodifiableList(new ArrayList<>(nodesByHost.values())), authCache); + this.nodeTuple = new NodeTuple<>(Collections.unmodifiableList(new ArrayList<>(nodesByHost.values())), authCache); this.blacklist.clear(); } @@ -269,14 +276,13 @@ public Response performRequest(Request request) throws IOException { return performRequest(nextNodes(), internalRequest, null); } - private Response performRequest(final NodeTuple> tuple, - final InternalRequest request, - Exception previousException) throws IOException { + private Response performRequest(final NodeTuple> tuple, final InternalRequest request, Exception previousException) + throws IOException { RequestContext context = request.createContextForNextAttempt(tuple.nodes.next(), tuple.authCache); HttpResponse httpResponse; try { httpResponse = client.execute(context.requestProducer, context.asyncResponseConsumer, context.context, null).get(); - } catch(Exception e) { + } catch (Exception e) { RequestLogger.logFailedRequest(logger, request.httpRequest, context.node, e); onFailure(context.node); Exception cause = extractAndWrapCause(e); @@ -328,11 +334,11 @@ private ResponseOrResponseException convertResponse(InternalRequest request, Nod } ResponseException responseException = new ResponseException(response); if (isRetryStatus(statusCode)) { - //mark host dead and retry against next one + // mark host dead and retry against next one onFailure(node); return new ResponseOrResponseException(responseException); } - //mark host alive and don't retry, as the error should be a request problem + // mark host alive and don't retry, as the error should be a request problem onResponse(node); throw responseException; } @@ -365,9 +371,11 @@ public Cancellable performRequestAsync(Request request, ResponseListener respons } } - private void performRequestAsync(final NodeTuple> tuple, - final InternalRequest request, - final FailureTrackingResponseListener listener) { + private void performRequestAsync( + final NodeTuple> tuple, + final InternalRequest request, + final FailureTrackingResponseListener listener + ) { request.cancellable.runIfNotCancelled(() -> { final RequestContext context = request.createContextForNextAttempt(tuple.nodes.next(), tuple.authCache); client.execute(context.requestProducer, context.asyncResponseConsumer, context.context, new FutureCallback() { @@ -385,7 +393,7 @@ public void completed(HttpResponse httpResponse) { listener.onDefinitiveFailure(responseOrResponseException.responseException); } } - } catch(Exception e) { + } catch (Exception e) { listener.onDefinitiveFailure(e); } } @@ -401,7 +409,7 @@ public void failed(Exception failure) { } else { listener.onDefinitiveFailure(failure); } - } catch(Exception e) { + } catch (Exception e) { listener.onDefinitiveFailure(e); } } @@ -433,8 +441,12 @@ private NodeTuple> nextNodes() throws IOException { * Select nodes to try and sorts them so that the first one will be tried initially, then the following ones * if the previous attempt failed and so on. Package private for testing. */ - static Iterable selectNodes(NodeTuple> nodeTuple, Map blacklist, - AtomicInteger lastNodeIndex, NodeSelector nodeSelector) throws IOException { + static Iterable selectNodes( + NodeTuple> nodeTuple, + Map blacklist, + AtomicInteger lastNodeIndex, + NodeSelector nodeSelector + ) throws IOException { /* * Sort the nodes into living and dead lists. */ @@ -489,8 +501,9 @@ static Iterable selectNodes(NodeTuple> nodeTuple, Map params) uriBuilder.addParameter(param.getKey(), param.getValue()); } return uriBuilder.build(); - } catch(URISyntaxException e) { + } catch (URISyntaxException e) { throw new IllegalArgumentException(e.getMessage(), e); } } @@ -743,7 +757,7 @@ private class InternalRequest { this.request = request; Map params = new HashMap<>(request.getParameters()); params.putAll(request.getOptions().getParameters()); - //ignore is a special parameter supported by the clients, shouldn't be sent to es + // ignore is a special parameter supported by the clients, shouldn't be sent to es String ignoreString = params.remove("ignore"); this.ignoreErrorCodes = getIgnoreErrorCodes(ignoreString, request.getMethod()); URI uri = buildUri(pathPrefix, request.getEndpoint(), params); @@ -751,8 +765,9 @@ private class InternalRequest { this.cancellable = Cancellable.fromRequest(httpRequest); setHeaders(httpRequest, request.getOptions().getHeaders()); setRequestConfig(httpRequest, request.getOptions().getRequestConfig()); - this.warningsHandler = request.getOptions().getWarningsHandler() == null ? - RestClient.this.warningsHandler : request.getOptions().getWarningsHandler(); + this.warningsHandler = request.getOptions().getWarningsHandler() == null + ? RestClient.this.warningsHandler + : request.getOptions().getWarningsHandler(); } private void setHeaders(HttpRequest req, Collection
    requestHeaders) { @@ -792,10 +807,11 @@ private static class RequestContext { RequestContext(InternalRequest request, Node node, AuthCache authCache) { this.node = node; - //we stream the request body if the entity allows for it + // we stream the request body if the entity allows for it this.requestProducer = HttpAsyncMethods.create(node.getHost(), request.httpRequest); - this.asyncResponseConsumer = - request.request.getOptions().getHttpAsyncResponseConsumerFactory().createHttpAsyncResponseConsumer(); + this.asyncResponseConsumer = request.request.getOptions() + .getHttpAsyncResponseConsumerFactory() + .createHttpAsyncResponseConsumer(); this.context = HttpClientContext.create(); context.setAuthCache(authCache); } @@ -805,7 +821,7 @@ private static Set getIgnoreErrorCodes(String ignoreString, String requ Set ignoreErrorCodes; if (ignoreString == null) { if (HttpHead.METHOD_NAME.equals(requestMethod)) { - //404 never causes error if returned for a HEAD request + // 404 never causes error if returned for a HEAD request ignoreErrorCodes = Collections.singleton(404); } else { ignoreErrorCodes = Collections.emptySet(); @@ -814,7 +830,7 @@ private static Set getIgnoreErrorCodes(String ignoreString, String requ String[] ignoresArray = ignoreString.split(","); ignoreErrorCodes = new HashSet<>(); if (HttpHead.METHOD_NAME.equals(requestMethod)) { - //404 never causes error if returned for a HEAD request + // 404 never causes error if returned for a HEAD request ignoreErrorCodes.add(404); } for (String ignoreCode : ignoresArray) { @@ -853,12 +869,12 @@ private static Exception extractAndWrapCause(Exception exception) { throw new RuntimeException("thread waiting for the response was interrupted", exception); } if (exception instanceof ExecutionException) { - ExecutionException executionException = (ExecutionException)exception; + ExecutionException executionException = (ExecutionException) exception; Throwable t = executionException.getCause() == null ? executionException : executionException.getCause(); if (t instanceof Error) { - throw (Error)t; + throw (Error) t; } - exception = (Exception)t; + exception = (Exception) t; } if (exception instanceof ConnectTimeoutException) { ConnectTimeoutException e = new ConnectTimeoutException(exception.getMessage()); @@ -888,7 +904,7 @@ private static Exception extractAndWrapCause(Exception exception) { if (exception instanceof IOException) { return new IOException(exception.getMessage(), exception); } - if (exception instanceof RuntimeException){ + if (exception instanceof RuntimeException) { return new RuntimeException(exception.getMessage(), exception); } return new RuntimeException("error while performing request", exception); diff --git a/client/rest/src/main/java/org/elasticsearch/client/RestClientBuilder.java b/client/rest/src/main/java/org/elasticsearch/client/RestClientBuilder.java index 27550663b0e3e..4a76a8b659c61 100644 --- a/client/rest/src/main/java/org/elasticsearch/client/RestClientBuilder.java +++ b/client/rest/src/main/java/org/elasticsearch/client/RestClientBuilder.java @@ -39,6 +39,7 @@ import java.util.Locale; import java.util.Objects; import java.util.Properties; + import javax.net.ssl.SSLContext; /** @@ -91,24 +92,35 @@ public final class RestClientBuilder { VERSION = version; - USER_AGENT_HEADER_VALUE = String.format(Locale.ROOT, "elasticsearch-java/%s (Java/%s)", - VERSION.isEmpty() ? "Unknown" : VERSION, System.getProperty("java.version")); + USER_AGENT_HEADER_VALUE = String.format( + Locale.ROOT, + "elasticsearch-java/%s (Java/%s)", + VERSION.isEmpty() ? "Unknown" : VERSION, + System.getProperty("java.version") + ); VersionInfo httpClientVersion = null; try { - httpClientVersion = AccessController.doPrivileged((PrivilegedAction)() -> - VersionInfo.loadVersionInfo("org.apache.http.nio.client", HttpAsyncClientBuilder.class.getClassLoader()) + httpClientVersion = AccessController.doPrivileged( + (PrivilegedAction) () -> VersionInfo.loadVersionInfo( + "org.apache.http.nio.client", + HttpAsyncClientBuilder.class.getClassLoader() + ) ); } catch (Exception e) { // Keep unknown } // service, language, transport, followed by additional information - META_HEADER_VALUE = "es=" + VERSION + - ",jv=" + System.getProperty("java.specification.version") + - ",t=" + VERSION + - ",hc=" + (httpClientVersion == null ? "" : httpClientVersion.getRelease()) + - LanguageRuntimeVersions.getRuntimeMetadata(); + META_HEADER_VALUE = "es=" + + VERSION + + ",jv=" + + System.getProperty("java.specification.version") + + ",t=" + + VERSION + + ",hc=" + + (httpClientVersion == null ? "" : httpClientVersion.getRelease()) + + LanguageRuntimeVersions.getRuntimeMetadata(); } /** @@ -264,26 +276,37 @@ public RestClient build() { failureListener = new RestClient.FailureListener(); } CloseableHttpAsyncClient httpClient = AccessController.doPrivileged( - (PrivilegedAction) this::createHttpClient); - RestClient restClient = new RestClient(httpClient, defaultHeaders, nodes, - pathPrefix, failureListener, nodeSelector, strictDeprecationMode, compressionEnabled); + (PrivilegedAction) this::createHttpClient + ); + RestClient restClient = new RestClient( + httpClient, + defaultHeaders, + nodes, + pathPrefix, + failureListener, + nodeSelector, + strictDeprecationMode, + compressionEnabled + ); httpClient.start(); return restClient; } private CloseableHttpAsyncClient createHttpClient() { - //default timeouts are all infinite + // default timeouts are all infinite RequestConfig.Builder requestConfigBuilder = RequestConfig.custom() - .setConnectTimeout(DEFAULT_CONNECT_TIMEOUT_MILLIS) - .setSocketTimeout(DEFAULT_SOCKET_TIMEOUT_MILLIS); + .setConnectTimeout(DEFAULT_CONNECT_TIMEOUT_MILLIS) + .setSocketTimeout(DEFAULT_SOCKET_TIMEOUT_MILLIS); if (requestConfigCallback != null) { requestConfigBuilder = requestConfigCallback.customizeRequestConfig(requestConfigBuilder); } try { - HttpAsyncClientBuilder httpClientBuilder = HttpAsyncClientBuilder.create().setDefaultRequestConfig(requestConfigBuilder.build()) - //default settings for connection pooling may be too constraining - .setMaxConnPerRoute(DEFAULT_MAX_CONN_PER_ROUTE).setMaxConnTotal(DEFAULT_MAX_CONN_TOTAL) + HttpAsyncClientBuilder httpClientBuilder = HttpAsyncClientBuilder.create() + .setDefaultRequestConfig(requestConfigBuilder.build()) + // default settings for connection pooling may be too constraining + .setMaxConnPerRoute(DEFAULT_MAX_CONN_PER_ROUTE) + .setMaxConnTotal(DEFAULT_MAX_CONN_TOTAL) .setSSLContext(SSLContext.getDefault()) .setUserAgent(USER_AGENT_HEADER_VALUE) .setTargetAuthenticationStrategy(new PersistentCredentialsAuthenticationStrategy()); diff --git a/client/rest/src/test/java/org/elasticsearch/client/DeadHostStateTests.java b/client/rest/src/test/java/org/elasticsearch/client/DeadHostStateTests.java index 6326a1206be53..a7fc42f2c3a51 100644 --- a/client/rest/src/test/java/org/elasticsearch/client/DeadHostStateTests.java +++ b/client/rest/src/test/java/org/elasticsearch/client/DeadHostStateTests.java @@ -33,7 +33,7 @@ public class DeadHostStateTests extends RestClientTestCase { - private static long[] EXPECTED_TIMEOUTS_SECONDS = new long[]{60, 84, 120, 169, 240, 339, 480, 678, 960, 1357, 1800}; + private static long[] EXPECTED_TIMEOUTS_SECONDS = new long[] { 60, 84, 120, 169, 240, 339, 480, 678, 960, 1357, 1800 }; public void testInitialDeadHostStateDefaultTimeSupplier() { DeadHostState deadHostState = new DeadHostState(DeadHostState.DEFAULT_TIME_SUPPLIER); @@ -75,12 +75,13 @@ public void testCompareToTimeSupplier() { public void testCompareToDifferingTimeSupplier() { try { - new DeadHostState(DeadHostState.DEFAULT_TIME_SUPPLIER).compareTo( - new DeadHostState(() -> 0L)); + new DeadHostState(DeadHostState.DEFAULT_TIME_SUPPLIER).compareTo(new DeadHostState(() -> 0L)); fail("expected failure"); } catch (IllegalArgumentException e) { - assertEquals("can't compare DeadHostStates holding different time suppliers as they may " + - "be based on different clocks", e.getMessage()); + assertEquals( + "can't compare DeadHostStates holding different time suppliers as they may " + "be based on different clocks", + e.getMessage() + ); } } @@ -112,12 +113,14 @@ public void testDeadHostStateTimeouts() { assertThat(TimeUnit.NANOSECONDS.toSeconds(previous.getDeadUntilNanos()), equalTo(expectedTimeoutsSecond)); previous = new DeadHostState(previous); } - //check that from here on the timeout does not increase + // check that from here on the timeout does not increase int iters = randomIntBetween(5, 30); for (int i = 0; i < iters; i++) { DeadHostState deadHostState = new DeadHostState(previous); - assertThat(TimeUnit.NANOSECONDS.toSeconds(deadHostState.getDeadUntilNanos()), - equalTo(EXPECTED_TIMEOUTS_SECONDS[EXPECTED_TIMEOUTS_SECONDS.length - 1])); + assertThat( + TimeUnit.NANOSECONDS.toSeconds(deadHostState.getDeadUntilNanos()), + equalTo(EXPECTED_TIMEOUTS_SECONDS[EXPECTED_TIMEOUTS_SECONDS.length - 1]) + ); previous = deadHostState; } } diff --git a/client/rest/src/test/java/org/elasticsearch/client/FailureTrackingResponseListenerTests.java b/client/rest/src/test/java/org/elasticsearch/client/FailureTrackingResponseListenerTests.java index 70d997960a183..2c4e527d879ac 100644 --- a/client/rest/src/test/java/org/elasticsearch/client/FailureTrackingResponseListenerTests.java +++ b/client/rest/src/test/java/org/elasticsearch/client/FailureTrackingResponseListenerTests.java @@ -78,7 +78,7 @@ public void testOnFailure() { assertEquals(1, exception.getSuppressed().length); assertSame(expectedExceptions[i--], exception.getSuppressed()[0]); exception = exception.getSuppressed()[0]; - } while(i >= 0); + } while (i >= 0); } } diff --git a/client/rest/src/test/java/org/elasticsearch/client/HasAttributeNodeSelectorTests.java b/client/rest/src/test/java/org/elasticsearch/client/HasAttributeNodeSelectorTests.java index 8462421ef298c..efdeffda3bc96 100644 --- a/client/rest/src/test/java/org/elasticsearch/client/HasAttributeNodeSelectorTests.java +++ b/client/rest/src/test/java/org/elasticsearch/client/HasAttributeNodeSelectorTests.java @@ -63,9 +63,13 @@ private static Node dummyNode(Map> attributes) { if (randomBoolean()) { roles.add("ingest"); } - return new Node(new HttpHost("dummy"), Collections.emptySet(), - randomAsciiAlphanumOfLength(5), randomAsciiAlphanumOfLength(5), - new Roles(roles), - attributes); + return new Node( + new HttpHost("dummy"), + Collections.emptySet(), + randomAsciiAlphanumOfLength(5), + randomAsciiAlphanumOfLength(5), + new Roles(roles), + attributes + ); } } diff --git a/client/rest/src/test/java/org/elasticsearch/client/HeapBufferedAsyncResponseConsumerTests.java b/client/rest/src/test/java/org/elasticsearch/client/HeapBufferedAsyncResponseConsumerTests.java index 0a00672d79a95..b1d12c9da604d 100644 --- a/client/rest/src/test/java/org/elasticsearch/client/HeapBufferedAsyncResponseConsumerTests.java +++ b/client/rest/src/test/java/org/elasticsearch/client/HeapBufferedAsyncResponseConsumerTests.java @@ -50,7 +50,7 @@ public class HeapBufferedAsyncResponseConsumerTests extends RestClientTestCase { - //maximum buffer that this test ends up allocating is 50MB + // maximum buffer that this test ends up allocating is 50MB private static final int MAX_TEST_BUFFER_SIZE = 50 * 1024 * 1024; private static final int TEST_BUFFER_LIMIT = 10 * 1024 * 1024; @@ -66,7 +66,7 @@ public void testResponseProcessing() throws Exception { HttpResponse httpResponse = new BasicHttpResponse(statusLine); httpResponse.setEntity(new StringEntity("test", ContentType.TEXT_PLAIN)); - //everything goes well + // everything goes well consumer.responseReceived(httpResponse); consumer.consumeContent(contentDecoder, ioControl); consumer.responseCompleted(httpContext); @@ -89,12 +89,12 @@ public void testDefaultBufferLimit() throws Exception { public void testConfiguredBufferLimit() throws Exception { try { new HeapBufferedAsyncResponseConsumer(randomIntBetween(Integer.MIN_VALUE, 0)); - } catch(IllegalArgumentException e) { + } catch (IllegalArgumentException e) { assertEquals("bufferLimit must be greater than 0", e.getMessage()); } try { new HeapBufferedAsyncResponseConsumer(0); - } catch(IllegalArgumentException e) { + } catch (IllegalArgumentException e) { assertEquals("bufferLimit must be greater than 0", e.getMessage()); } int bufferLimit = randomIntBetween(1, MAX_TEST_BUFFER_SIZE - 100); @@ -103,16 +103,17 @@ public void testConfiguredBufferLimit() throws Exception { } public void testCanConfigureHeapBufferLimitFromOutsidePackage() throws ClassNotFoundException, NoSuchMethodException, - IllegalAccessException, InvocationTargetException, InstantiationException { + IllegalAccessException, InvocationTargetException, InstantiationException { int bufferLimit = randomIntBetween(1, Integer.MAX_VALUE); - //we use reflection to make sure that the class can be instantiated from the outside, and the constructor is public - Constructor constructor = - HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory.class.getConstructor(Integer.TYPE); + // we use reflection to make sure that the class can be instantiated from the outside, and the constructor is public + Constructor constructor = HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory.class.getConstructor( + Integer.TYPE + ); assertEquals(Modifier.PUBLIC, constructor.getModifiers() & Modifier.PUBLIC); Object object = constructor.newInstance(bufferLimit); assertThat(object, instanceOf(HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory.class)); HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory consumerFactory = - (HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory) object; + (HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory) object; HttpAsyncResponseConsumer consumer = consumerFactory.createHttpAsyncResponseConsumer(); assertThat(consumer, instanceOf(HeapBufferedAsyncResponseConsumer.class)); HeapBufferedAsyncResponseConsumer bufferedAsyncResponseConsumer = (HeapBufferedAsyncResponseConsumer) consumer; @@ -141,9 +142,11 @@ public long getContentLength() { contentLength.set(randomLongBetween(bufferLimit + 1, MAX_TEST_BUFFER_SIZE)); try { consumer.onEntityEnclosed(entity, ContentType.APPLICATION_JSON); - } catch(ContentTooLongException e) { - assertEquals("entity content is too long [" + entity.getContentLength() + - "] for the configured buffer limit [" + bufferLimit + "]", e.getMessage()); + } catch (ContentTooLongException e) { + assertEquals( + "entity content is too long [" + entity.getContentLength() + "] for the configured buffer limit [" + bufferLimit + "]", + e.getMessage() + ); } } } diff --git a/client/rest/src/test/java/org/elasticsearch/client/HostsTrackingFailureListener.java b/client/rest/src/test/java/org/elasticsearch/client/HostsTrackingFailureListener.java index 16aa50a3248c7..1e92cab899aa9 100644 --- a/client/rest/src/test/java/org/elasticsearch/client/HostsTrackingFailureListener.java +++ b/client/rest/src/test/java/org/elasticsearch/client/HostsTrackingFailureListener.java @@ -42,7 +42,7 @@ public void onFailure(Node node) { void assertCalled(List nodes) { HttpHost[] hosts = new HttpHost[nodes.size()]; - for (int i = 0 ; i < nodes.size(); i++) { + for (int i = 0; i < nodes.size(); i++) { hosts[i] = nodes.get(i).getHost(); } assertCalled(hosts); diff --git a/client/rest/src/test/java/org/elasticsearch/client/NodeSelectorTests.java b/client/rest/src/test/java/org/elasticsearch/client/NodeSelectorTests.java index 68e3e91facafb..43ca19f742932 100644 --- a/client/rest/src/test/java/org/elasticsearch/client/NodeSelectorTests.java +++ b/client/rest/src/test/java/org/elasticsearch/client/NodeSelectorTests.java @@ -75,11 +75,20 @@ public void testNotMasterOnly() { assertEquals(expected, nodes); } - private static Node dummyNode(boolean master, boolean data, boolean ingest){ + private static Node dummyNode(boolean master, boolean data, boolean ingest) { return dummyNode(master, data, ingest, false, false, false, false, false); } - private static Node dummyNode(boolean master, boolean data, boolean ingest, - boolean dataContent, boolean dataHot, boolean dataWarm, boolean dataCold, boolean dataFrozen) { + + private static Node dummyNode( + boolean master, + boolean data, + boolean ingest, + boolean dataContent, + boolean dataHot, + boolean dataWarm, + boolean dataCold, + boolean dataFrozen + ) { final Set roles = new TreeSet<>(); if (master) { roles.add("master"); @@ -105,10 +114,14 @@ private static Node dummyNode(boolean master, boolean data, boolean ingest, if (ingest) { roles.add("ingest"); } - return new Node(new HttpHost("dummy"), Collections.emptySet(), - randomAsciiAlphanumOfLength(5), randomAsciiAlphanumOfLength(5), - new Roles(roles), - Collections.>emptyMap()); + return new Node( + new HttpHost("dummy"), + Collections.emptySet(), + randomAsciiAlphanumOfLength(5), + randomAsciiAlphanumOfLength(5), + new Roles(roles), + Collections.>emptyMap() + ); } } diff --git a/client/rest/src/test/java/org/elasticsearch/client/NodeTests.java b/client/rest/src/test/java/org/elasticsearch/client/NodeTests.java index 03d850b01f438..e3603193e5441 100644 --- a/client/rest/src/test/java/org/elasticsearch/client/NodeTests.java +++ b/client/rest/src/test/java/org/elasticsearch/client/NodeTests.java @@ -43,53 +43,113 @@ public void testToString() { attributes.put("foo", singletonList("bar")); attributes.put("baz", Arrays.asList("bort", "zoom")); assertEquals("[host=http://1]", new Node(new HttpHost("1")).toString()); - assertEquals("[host=http://1, attributes={foo=[bar], baz=[bort, zoom]}]", - new Node(new HttpHost("1"), null, null, null, null, attributes).toString()); - assertEquals("[host=http://1, roles=data,ingest,master]", new Node(new HttpHost("1"), - null, null, null, new Roles(new TreeSet<>(Arrays.asList("master", "data", "ingest"))), null).toString()); - assertEquals("[host=http://1, version=ver]", new Node(new HttpHost("1"), - null, null, "ver", null, null).toString()); - assertEquals("[host=http://1, name=nam]", new Node(new HttpHost("1"), - null, "nam", null, null, null).toString()); - assertEquals("[host=http://1, bound=[http://1, http://2]]", new Node(new HttpHost("1"), - new HashSet<>(Arrays.asList(new HttpHost("1"), new HttpHost("2"))), null, null, null, null).toString()); assertEquals( - "[host=http://1, bound=[http://1, http://2], " - + "name=nam, version=ver, roles=master, attributes={foo=[bar], baz=[bort, zoom]}]", - new Node(new HttpHost("1"), new HashSet<>(Arrays.asList(new HttpHost("1"), new HttpHost("2"))), - "nam", "ver", new Roles(Collections.singleton("master")), attributes).toString()); + "[host=http://1, attributes={foo=[bar], baz=[bort, zoom]}]", + new Node(new HttpHost("1"), null, null, null, null, attributes).toString() + ); + assertEquals( + "[host=http://1, roles=data,ingest,master]", + new Node(new HttpHost("1"), null, null, null, new Roles(new TreeSet<>(Arrays.asList("master", "data", "ingest"))), null) + .toString() + ); + assertEquals("[host=http://1, version=ver]", new Node(new HttpHost("1"), null, null, "ver", null, null).toString()); + assertEquals("[host=http://1, name=nam]", new Node(new HttpHost("1"), null, "nam", null, null, null).toString()); + assertEquals( + "[host=http://1, bound=[http://1, http://2]]", + new Node(new HttpHost("1"), new HashSet<>(Arrays.asList(new HttpHost("1"), new HttpHost("2"))), null, null, null, null) + .toString() + ); + assertEquals( + "[host=http://1, bound=[http://1, http://2], " + + "name=nam, version=ver, roles=master, attributes={foo=[bar], baz=[bort, zoom]}]", + new Node( + new HttpHost("1"), + new HashSet<>(Arrays.asList(new HttpHost("1"), new HttpHost("2"))), + "nam", + "ver", + new Roles(Collections.singleton("master")), + attributes + ).toString() + ); } public void testEqualsAndHashCode() { HttpHost host = new HttpHost(randomAsciiAlphanumOfLength(5)); - Node node = new Node(host, - randomBoolean() ? null : singleton(host), - randomBoolean() ? null : randomAsciiAlphanumOfLength(5), - randomBoolean() ? null : randomAsciiAlphanumOfLength(5), - randomBoolean() ? null : new Roles(new TreeSet<>(Arrays.asList("master", "data", "ingest"))), - randomBoolean() ? null : singletonMap("foo", singletonList("bar"))); + Node node = new Node( + host, + randomBoolean() ? null : singleton(host), + randomBoolean() ? null : randomAsciiAlphanumOfLength(5), + randomBoolean() ? null : randomAsciiAlphanumOfLength(5), + randomBoolean() ? null : new Roles(new TreeSet<>(Arrays.asList("master", "data", "ingest"))), + randomBoolean() ? null : singletonMap("foo", singletonList("bar")) + ); assertFalse(node.equals(null)); assertTrue(node.equals(node)); assertEquals(node.hashCode(), node.hashCode()); - Node copy = new Node(host, node.getBoundHosts(), node.getName(), node.getVersion(), - node.getRoles(), node.getAttributes()); + Node copy = new Node(host, node.getBoundHosts(), node.getName(), node.getVersion(), node.getRoles(), node.getAttributes()); assertTrue(node.equals(copy)); assertEquals(node.hashCode(), copy.hashCode()); - assertFalse(node.equals(new Node(new HttpHost(host.toHostString() + "changed"), node.getBoundHosts(), - node.getName(), node.getVersion(), node.getRoles(), node.getAttributes()))); - assertFalse(node.equals(new Node(host, new HashSet<>(Arrays.asList(host, new HttpHost(host.toHostString() + "changed"))), - node.getName(), node.getVersion(), node.getRoles(), node.getAttributes()))); - assertFalse(node.equals(new Node(host, node.getBoundHosts(), node.getName() + "changed", - node.getVersion(), node.getRoles(), node.getAttributes()))); - assertFalse(node.equals(new Node(host, node.getBoundHosts(), node.getName(), - node.getVersion() + "changed", node.getRoles(), node.getAttributes()))); - assertFalse(node.equals(new Node(host, node.getBoundHosts(), node.getName(), - node.getVersion(), new Roles(Collections.emptySet()), node.getAttributes()))); - assertFalse(node.equals(new Node(host, node.getBoundHosts(), node.getName(), - node.getVersion(), node.getRoles(), singletonMap("bort", singletonList("bing"))))); + assertFalse( + node.equals( + new Node( + new HttpHost(host.toHostString() + "changed"), + node.getBoundHosts(), + node.getName(), + node.getVersion(), + node.getRoles(), + node.getAttributes() + ) + ) + ); + assertFalse( + node.equals( + new Node( + host, + new HashSet<>(Arrays.asList(host, new HttpHost(host.toHostString() + "changed"))), + node.getName(), + node.getVersion(), + node.getRoles(), + node.getAttributes() + ) + ) + ); + assertFalse( + node.equals( + new Node(host, node.getBoundHosts(), node.getName() + "changed", node.getVersion(), node.getRoles(), node.getAttributes()) + ) + ); + assertFalse( + node.equals( + new Node(host, node.getBoundHosts(), node.getName(), node.getVersion() + "changed", node.getRoles(), node.getAttributes()) + ) + ); + assertFalse( + node.equals( + new Node( + host, + node.getBoundHosts(), + node.getName(), + node.getVersion(), + new Roles(Collections.emptySet()), + node.getAttributes() + ) + ) + ); + assertFalse( + node.equals( + new Node( + host, + node.getBoundHosts(), + node.getName(), + node.getVersion(), + node.getRoles(), + singletonMap("bort", singletonList("bing")) + ) + ) + ); } - public void testDataRole(){ + public void testDataRole() { Roles roles = new Roles(new TreeSet<>(Arrays.asList("data_hot"))); assertTrue(roles.hasDataHotRole()); assertTrue(roles.canContainData()); diff --git a/client/rest/src/test/java/org/elasticsearch/client/PreferHasAttributeNodeSelectorTests.java b/client/rest/src/test/java/org/elasticsearch/client/PreferHasAttributeNodeSelectorTests.java index 5e5bfebb5c64a..5862543505871 100644 --- a/client/rest/src/test/java/org/elasticsearch/client/PreferHasAttributeNodeSelectorTests.java +++ b/client/rest/src/test/java/org/elasticsearch/client/PreferHasAttributeNodeSelectorTests.java @@ -73,9 +73,13 @@ private static Node dummyNode(Map> attributes) { if (randomBoolean()) { roles.add("ingest"); } - return new Node(new HttpHost("dummy"), Collections.emptySet(), - randomAsciiAlphanumOfLength(5), randomAsciiAlphanumOfLength(5), + return new Node( + new HttpHost("dummy"), + Collections.emptySet(), + randomAsciiAlphanumOfLength(5), + randomAsciiAlphanumOfLength(5), new Roles(roles), - attributes); + attributes + ); } } diff --git a/client/rest/src/test/java/org/elasticsearch/client/RequestLoggerTests.java b/client/rest/src/test/java/org/elasticsearch/client/RequestLoggerTests.java index 967e4a2ef185f..db79bb9abb8c6 100644 --- a/client/rest/src/test/java/org/elasticsearch/client/RequestLoggerTests.java +++ b/client/rest/src/test/java/org/elasticsearch/client/RequestLoggerTests.java @@ -70,13 +70,15 @@ public void testTraceRequest() throws IOException, URISyntaxException { expected += " -d '" + requestBody + "'"; HttpEntityEnclosingRequest enclosingRequest = (HttpEntityEnclosingRequest) request; HttpEntity entity; - switch(randomIntBetween(0, 4)) { + switch (randomIntBetween(0, 4)) { case 0: entity = new StringEntity(requestBody, ContentType.APPLICATION_JSON); break; case 1: - entity = new InputStreamEntity(new ByteArrayInputStream(requestBody.getBytes(StandardCharsets.UTF_8)), - ContentType.APPLICATION_JSON); + entity = new InputStreamEntity( + new ByteArrayInputStream(requestBody.getBytes(StandardCharsets.UTF_8)), + ContentType.APPLICATION_JSON + ); break; case 2: entity = new NStringEntity(requestBody, ContentType.APPLICATION_JSON); @@ -96,7 +98,7 @@ public void testTraceRequest() throws IOException, URISyntaxException { String traceRequest = RequestLogger.buildTraceRequest(request, host); assertThat(traceRequest, equalTo(expected)); if (hasBody) { - //check that the body is still readable as most entities are not repeatable + // check that the body is still readable as most entities are not repeatable String body = EntityUtils.toString(((HttpEntityEnclosingRequest) request).getEntity(), StandardCharsets.UTF_8); assertThat(body, equalTo(requestBody)); } @@ -122,28 +124,30 @@ public void testTraceResponse() throws IOException { expected += "\n# \"field\": \"value\""; expected += "\n# }"; HttpEntity entity; - switch(randomIntBetween(0, 2)) { - case 0: - entity = new StringEntity(responseBody, ContentType.APPLICATION_JSON); - break; - case 1: - //test a non repeatable entity - entity = new InputStreamEntity(new ByteArrayInputStream(responseBody.getBytes(StandardCharsets.UTF_8)), - ContentType.APPLICATION_JSON); - break; - case 2: - // Evil entity without a charset - entity = new StringEntity(responseBody, ContentType.create("application/json", (Charset) null)); - break; - default: - throw new UnsupportedOperationException(); + switch (randomIntBetween(0, 2)) { + case 0: + entity = new StringEntity(responseBody, ContentType.APPLICATION_JSON); + break; + case 1: + // test a non repeatable entity + entity = new InputStreamEntity( + new ByteArrayInputStream(responseBody.getBytes(StandardCharsets.UTF_8)), + ContentType.APPLICATION_JSON + ); + break; + case 2: + // Evil entity without a charset + entity = new StringEntity(responseBody, ContentType.create("application/json", (Charset) null)); + break; + default: + throw new UnsupportedOperationException(); } httpResponse.setEntity(entity); } String traceResponse = RequestLogger.buildTraceResponse(httpResponse); assertThat(traceResponse, equalTo(expected)); if (hasBody) { - //check that the body is still readable as most entities are not repeatable + // check that the body is still readable as most entities are not repeatable String body = EntityUtils.toString(httpResponse.getEntity(), StandardCharsets.UTF_8); assertThat(body, equalTo(responseBody)); } @@ -153,8 +157,12 @@ public void testResponseWarnings() throws Exception { HttpHost host = new HttpHost("localhost", 9200); HttpUriRequest request = randomHttpRequest(new URI("/index/type/_api")); int numWarnings = randomIntBetween(1, 5); - StringBuilder expected = new StringBuilder("request [").append(request.getMethod()).append(" ").append(host) - .append("/index/type/_api] returned ").append(numWarnings).append(" warnings: "); + StringBuilder expected = new StringBuilder("request [").append(request.getMethod()) + .append(" ") + .append(host) + .append("/index/type/_api] returned ") + .append(numWarnings) + .append(" warnings: "); Header[] warnings = new Header[numWarnings]; for (int i = 0; i < numWarnings; i++) { String warning = "this is warning number " + i; @@ -169,7 +177,7 @@ public void testResponseWarnings() throws Exception { private static HttpUriRequest randomHttpRequest(URI uri) { int requestType = randomIntBetween(0, 7); - switch(requestType) { + switch (requestType) { case 0: return new HttpGetWithEntity(uri); case 1: diff --git a/client/rest/src/test/java/org/elasticsearch/client/RequestOptionsTests.java b/client/rest/src/test/java/org/elasticsearch/client/RequestOptionsTests.java index 5e1c5e19dbb6d..9282c59317ddd 100644 --- a/client/rest/src/test/java/org/elasticsearch/client/RequestOptionsTests.java +++ b/client/rest/src/test/java/org/elasticsearch/client/RequestOptionsTests.java @@ -71,8 +71,8 @@ public void testAddHeader() { assertEquals(headers, options.getHeaders()); try { - options.getHeaders().add( - new RequestOptions.ReqHeader(randomAsciiAlphanumOfLengthBetween(5, 10), randomAsciiAlphanumOfLength(3))); + options.getHeaders() + .add(new RequestOptions.ReqHeader(randomAsciiAlphanumOfLengthBetween(5, 10), randomAsciiAlphanumOfLength(3))); fail("expected failure"); } catch (UnsupportedOperationException e) { assertNull(e.getMessage()); @@ -186,23 +186,23 @@ private static RequestOptions mutate(RequestOptions options) { RequestOptions.Builder mutant = options.toBuilder(); int mutationType = between(0, 2); switch (mutationType) { - case 0: - mutant.addHeader("extra", "m"); - return mutant.build(); - case 1: - mutant.setHttpAsyncResponseConsumerFactory(new HeapBufferedResponseConsumerFactory(5)); - return mutant.build(); - case 2: - mutant.setWarningsHandler(new WarningsHandler() { - @Override - public boolean warningsShouldFailRequest(List warnings) { - fail("never called"); - return false; - } - }); - return mutant.build(); - default: - throw new UnsupportedOperationException("Unknown mutation type [" + mutationType + "]"); + case 0: + mutant.addHeader("extra", "m"); + return mutant.build(); + case 1: + mutant.setHttpAsyncResponseConsumerFactory(new HeapBufferedResponseConsumerFactory(5)); + return mutant.build(); + case 2: + mutant.setWarningsHandler(new WarningsHandler() { + @Override + public boolean warningsShouldFailRequest(List warnings) { + fail("never called"); + return false; + } + }); + return mutant.build(); + default: + throw new UnsupportedOperationException("Unknown mutation type [" + mutationType + "]"); } } } diff --git a/client/rest/src/test/java/org/elasticsearch/client/RequestTests.java b/client/rest/src/test/java/org/elasticsearch/client/RequestTests.java index 141dfda1d3e19..43a0b640b6cb4 100644 --- a/client/rest/src/test/java/org/elasticsearch/client/RequestTests.java +++ b/client/rest/src/test/java/org/elasticsearch/client/RequestTests.java @@ -39,7 +39,7 @@ public class RequestTests extends RestClientTestCase { public void testConstructor() { - final String method = randomFrom(new String[] {"GET", "PUT", "POST", "HEAD", "DELETE"}); + final String method = randomFrom(new String[] { "GET", "PUT", "POST", "HEAD", "DELETE" }); final String endpoint = randomAsciiLettersOfLengthBetween(1, 10); try { @@ -62,7 +62,7 @@ public void testConstructor() { } public void testAddParameters() { - final String method = randomFrom(new String[] {"GET", "PUT", "POST", "HEAD", "DELETE"}); + final String method = randomFrom(new String[] { "GET", "PUT", "POST", "HEAD", "DELETE" }); final String endpoint = randomAsciiLettersOfLengthBetween(1, 10); int parametersCount = between(1, 3); final Map parameters = new HashMap<>(parametersCount); @@ -100,10 +100,11 @@ public void testAddParameters() { } public void testSetEntity() { - final String method = randomFrom(new String[] {"GET", "PUT", "POST", "HEAD", "DELETE"}); + final String method = randomFrom(new String[] { "GET", "PUT", "POST", "HEAD", "DELETE" }); final String endpoint = randomAsciiLettersOfLengthBetween(1, 10); - final HttpEntity entity = - randomBoolean() ? new StringEntity(randomAsciiLettersOfLengthBetween(1, 100), ContentType.TEXT_PLAIN) : null; + final HttpEntity entity = randomBoolean() + ? new StringEntity(randomAsciiLettersOfLengthBetween(1, 100), ContentType.TEXT_PLAIN) + : null; Request request = new Request(method, endpoint); request.setEntity(entity); @@ -111,7 +112,7 @@ public void testSetEntity() { } public void testSetJsonEntity() throws IOException { - final String method = randomFrom(new String[] {"GET", "PUT", "POST", "HEAD", "DELETE"}); + final String method = randomFrom(new String[] { "GET", "PUT", "POST", "HEAD", "DELETE" }); final String endpoint = randomAsciiLettersOfLengthBetween(1, 10); Request request = new Request(method, endpoint); @@ -126,7 +127,7 @@ public void testSetJsonEntity() throws IOException { } public void testSetOptions() { - final String method = randomFrom(new String[] {"GET", "PUT", "POST", "HEAD", "DELETE"}); + final String method = randomFrom(new String[] { "GET", "PUT", "POST", "HEAD", "DELETE" }); final String endpoint = randomAsciiLettersOfLengthBetween(1, 10); Request request = new Request(method, endpoint); @@ -170,8 +171,9 @@ public void testEqualsAndHashCode() { private static Request randomRequest() { Request request = new Request( - randomFrom(new String[] {"GET", "PUT", "DELETE", "POST", "HEAD", "OPTIONS"}), - randomAsciiAlphanumOfLength(5)); + randomFrom(new String[] { "GET", "PUT", "DELETE", "POST", "HEAD", "OPTIONS" }), + randomAsciiAlphanumOfLength(5) + ); int parameterCount = between(0, 5); for (int i = 0; i < parameterCount; i++) { @@ -182,11 +184,14 @@ private static Request randomRequest() { if (randomBoolean()) { request.setJsonEntity(randomAsciiAlphanumOfLength(10)); } else { - request.setEntity(randomFrom(new HttpEntity[] { - new StringEntity(randomAsciiAlphanumOfLength(10), ContentType.APPLICATION_JSON), - new NStringEntity(randomAsciiAlphanumOfLength(10), ContentType.APPLICATION_JSON), - new ByteArrayEntity(randomBytesOfLength(40), ContentType.APPLICATION_JSON) - })); + request.setEntity( + randomFrom( + new HttpEntity[] { + new StringEntity(randomAsciiAlphanumOfLength(10), ContentType.APPLICATION_JSON), + new NStringEntity(randomAsciiAlphanumOfLength(10), ContentType.APPLICATION_JSON), + new ByteArrayEntity(randomBytesOfLength(40), ContentType.APPLICATION_JSON) } + ) + ); } } @@ -217,19 +222,19 @@ private static Request mutate(Request request) { Request mutant = copy(request); int mutationType = between(0, 2); switch (mutationType) { - case 0: - mutant.addParameter(randomAsciiAlphanumOfLength(mutant.getParameters().size() + 4), "extra"); - return mutant; - case 1: - mutant.setJsonEntity("mutant"); // randomRequest can't produce this value - return mutant; - case 2: - RequestOptions.Builder options = mutant.getOptions().toBuilder(); - options.addHeader("extra", "m"); - mutant.setOptions(options); - return mutant; - default: - throw new UnsupportedOperationException("Unknown mutation type [" + mutationType + "]"); + case 0: + mutant.addParameter(randomAsciiAlphanumOfLength(mutant.getParameters().size() + 4), "extra"); + return mutant; + case 1: + mutant.setJsonEntity("mutant"); // randomRequest can't produce this value + return mutant; + case 2: + RequestOptions.Builder options = mutant.getOptions().toBuilder(); + options.addHeader("extra", "m"); + mutant.setOptions(options); + return mutant; + default: + throw new UnsupportedOperationException("Unknown mutation type [" + mutationType + "]"); } } diff --git a/client/rest/src/test/java/org/elasticsearch/client/ResponseExceptionTests.java b/client/rest/src/test/java/org/elasticsearch/client/ResponseExceptionTests.java index 0f196fd57b876..712eb6d6f4f9e 100644 --- a/client/rest/src/test/java/org/elasticsearch/client/ResponseExceptionTests.java +++ b/client/rest/src/test/java/org/elasticsearch/client/ResponseExceptionTests.java @@ -56,9 +56,11 @@ public void testResponseException() throws IOException { if (getRandom().nextBoolean()) { entity = new StringEntity(responseBody, ContentType.APPLICATION_JSON); } else { - //test a non repeatable entity - entity = new InputStreamEntity(new ByteArrayInputStream(responseBody.getBytes(StandardCharsets.UTF_8)), - ContentType.APPLICATION_JSON); + // test a non repeatable entity + entity = new InputStreamEntity( + new ByteArrayInputStream(responseBody.getBytes(StandardCharsets.UTF_8)), + ContentType.APPLICATION_JSON + ); } httpResponse.setEntity(entity); } @@ -75,7 +77,8 @@ public void testResponseException() throws IOException { assertNull(responseException.getResponse().getEntity()); } - String message = String.format(Locale.ROOT, + String message = String.format( + Locale.ROOT, "method [%s], host [%s], URI [%s], status line [%s]", response.getRequestLine().getMethod(), response.getHost(), diff --git a/client/rest/src/test/java/org/elasticsearch/client/RestClientBuilderIntegTests.java b/client/rest/src/test/java/org/elasticsearch/client/RestClientBuilderIntegTests.java index 410a254449bf2..b9e0e996c3f76 100644 --- a/client/rest/src/test/java/org/elasticsearch/client/RestClientBuilderIntegTests.java +++ b/client/rest/src/test/java/org/elasticsearch/client/RestClientBuilderIntegTests.java @@ -23,15 +23,12 @@ import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpsConfigurator; import com.sun.net.httpserver.HttpsServer; + import org.apache.http.HttpHost; import org.elasticsearch.mocksocket.MockHttpServer; import org.junit.AfterClass; import org.junit.BeforeClass; -import javax.net.ssl.KeyManagerFactory; -import javax.net.ssl.SSLContext; -import javax.net.ssl.SSLHandshakeException; -import javax.net.ssl.TrustManagerFactory; import java.io.IOException; import java.io.InputStream; import java.net.InetAddress; @@ -46,6 +43,11 @@ import java.security.cert.CertificateFactory; import java.security.spec.PKCS8EncodedKeySpec; +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLHandshakeException; +import javax.net.ssl.TrustManagerFactory; + import static org.hamcrest.Matchers.instanceOf; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; @@ -110,18 +112,25 @@ private RestClient buildRestClient() { private static SSLContext getSslContext() throws Exception { SSLContext sslContext = SSLContext.getInstance(getProtocol()); - try (InputStream certFile = RestClientBuilderIntegTests.class.getResourceAsStream("/test.crt"); - InputStream keyStoreFile = RestClientBuilderIntegTests.class.getResourceAsStream("/test_truststore.jks")) { + try ( + InputStream certFile = RestClientBuilderIntegTests.class.getResourceAsStream("/test.crt"); + InputStream keyStoreFile = RestClientBuilderIntegTests.class.getResourceAsStream("/test_truststore.jks") + ) { // Build a keystore of default type programmatically since we can't use JKS keystores to // init a KeyManagerFactory in FIPS 140 JVMs. KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); keyStore.load(null, "password".toCharArray()); CertificateFactory certFactory = CertificateFactory.getInstance("X.509"); - PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(Files.readAllBytes(Paths.get(RestClientBuilderIntegTests.class - .getResource("/test.der").toURI()))); + PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec( + Files.readAllBytes(Paths.get(RestClientBuilderIntegTests.class.getResource("/test.der").toURI())) + ); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); - keyStore.setKeyEntry("mykey", keyFactory.generatePrivate(privateKeySpec), "password".toCharArray(), - new Certificate[]{certFactory.generateCertificate(certFile)}); + keyStore.setKeyEntry( + "mykey", + keyFactory.generatePrivate(privateKeySpec), + "password".toCharArray(), + new Certificate[] { certFactory.generateCertificate(certFile) } + ); KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); kmf.init(keyStore, "password".toCharArray()); KeyStore trustStore = KeyStore.getInstance("JKS"); diff --git a/client/rest/src/test/java/org/elasticsearch/client/RestClientBuilderTests.java b/client/rest/src/test/java/org/elasticsearch/client/RestClientBuilderTests.java index bda6367537b3c..9e2f2c1b05c80 100644 --- a/client/rest/src/test/java/org/elasticsearch/client/RestClientBuilderTests.java +++ b/client/rest/src/test/java/org/elasticsearch/client/RestClientBuilderTests.java @@ -39,44 +39,44 @@ public class RestClientBuilderTests extends RestClientTestCase { public void testBuild() throws IOException { try { - RestClient.builder((HttpHost[])null); + RestClient.builder((HttpHost[]) null); fail("should have failed"); - } catch(IllegalArgumentException e) { + } catch (IllegalArgumentException e) { assertEquals("hosts must not be null nor empty", e.getMessage()); } try { RestClient.builder(new HttpHost[] {}); fail("should have failed"); - } catch(IllegalArgumentException e) { + } catch (IllegalArgumentException e) { assertEquals("hosts must not be null nor empty", e.getMessage()); } try { - RestClient.builder((Node[])null); + RestClient.builder((Node[]) null); fail("should have failed"); - } catch(IllegalArgumentException e) { + } catch (IllegalArgumentException e) { assertEquals("nodes must not be null or empty", e.getMessage()); } try { RestClient.builder(new Node[] {}); fail("should have failed"); - } catch(IllegalArgumentException e) { + } catch (IllegalArgumentException e) { assertEquals("nodes must not be null or empty", e.getMessage()); } try { RestClient.builder(new Node(new HttpHost("localhost", 9200)), null); fail("should have failed"); - } catch(IllegalArgumentException e) { + } catch (IllegalArgumentException e) { assertEquals("node cannot be null", e.getMessage()); } try { RestClient.builder(new HttpHost("localhost", 9200), null); fail("should have failed"); - } catch(IllegalArgumentException e) { + } catch (IllegalArgumentException e) { assertEquals("host cannot be null", e.getMessage()); } @@ -87,35 +87,35 @@ public void testBuild() throws IOException { try { RestClient.builder(new HttpHost("localhost", 9200)).setDefaultHeaders(null); fail("should have failed"); - } catch(NullPointerException e) { + } catch (NullPointerException e) { assertEquals("defaultHeaders must not be null", e.getMessage()); } try { - RestClient.builder(new HttpHost("localhost", 9200)).setDefaultHeaders(new Header[]{null}); + RestClient.builder(new HttpHost("localhost", 9200)).setDefaultHeaders(new Header[] { null }); fail("should have failed"); - } catch(NullPointerException e) { + } catch (NullPointerException e) { assertEquals("default header must not be null", e.getMessage()); } try { RestClient.builder(new HttpHost("localhost", 9200)).setFailureListener(null); fail("should have failed"); - } catch(NullPointerException e) { + } catch (NullPointerException e) { assertEquals("failureListener must not be null", e.getMessage()); } try { RestClient.builder(new HttpHost("localhost", 9200)).setHttpClientConfigCallback(null); fail("should have failed"); - } catch(NullPointerException e) { + } catch (NullPointerException e) { assertEquals("httpClientConfigCallback must not be null", e.getMessage()); } try { RestClient.builder(new HttpHost("localhost", 9200)).setRequestConfigCallback(null); fail("should have failed"); - } catch(NullPointerException e) { + } catch (NullPointerException e) { assertEquals("requestConfigCallback must not be null", e.getMessage()); } @@ -257,7 +257,7 @@ public void testDefaultConnectionRequestTimeout() throws IOException { public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) { RequestConfig requestConfig = requestConfigBuilder.build(); assertEquals(RequestConfig.DEFAULT.getConnectionRequestTimeout(), requestConfig.getConnectionRequestTimeout()); - //this way we get notified if the default ever changes + // this way we get notified if the default ever changes assertEquals(-1, requestConfig.getConnectionRequestTimeout()); return requestConfigBuilder; } diff --git a/client/rest/src/test/java/org/elasticsearch/client/RestClientGzipCompressionTests.java b/client/rest/src/test/java/org/elasticsearch/client/RestClientGzipCompressionTests.java index c544496505f75..608a8b61f8161 100644 --- a/client/rest/src/test/java/org/elasticsearch/client/RestClientGzipCompressionTests.java +++ b/client/rest/src/test/java/org/elasticsearch/client/RestClientGzipCompressionTests.java @@ -22,6 +22,7 @@ import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpServer; + import org.apache.http.HttpEntity; import org.apache.http.HttpHost; import org.apache.http.entity.ContentType; diff --git a/client/rest/src/test/java/org/elasticsearch/client/RestClientMultipleHostsIntegTests.java b/client/rest/src/test/java/org/elasticsearch/client/RestClientMultipleHostsIntegTests.java index d4947ba8f61f7..6608ff33bc57e 100644 --- a/client/rest/src/test/java/org/elasticsearch/client/RestClientMultipleHostsIntegTests.java +++ b/client/rest/src/test/java/org/elasticsearch/client/RestClientMultipleHostsIntegTests.java @@ -22,6 +22,7 @@ import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpServer; + import org.apache.http.HttpHost; import org.elasticsearch.mocksocket.MockHttpServer; import org.junit.AfterClass; @@ -96,7 +97,7 @@ private static RestClient buildRestClient(NodeSelector nodeSelector) { private static HttpServer createHttpServer() throws Exception { HttpServer httpServer = MockHttpServer.createHttp(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0); httpServer.start(); - //returns a different status code depending on the path + // returns a different status code depending on the path for (int statusCode : getAllStatusCodes()) { httpServer.createContext(pathPrefix + "/" + statusCode, new ResponseHandler(statusCode)); } @@ -130,8 +131,7 @@ public void handle(HttpExchange exchange) throws IOException { requestCameInLatch.countDown(); try { cancelHandlerLatch.await(); - } catch (InterruptedException ignore) { - } finally { + } catch (InterruptedException ignore) {} finally { exchange.sendResponseHeaders(200, 0); exchange.close(); } @@ -165,7 +165,7 @@ public static void stopHttpServers() throws IOException { @Before public void stopRandomHost() { - //verify that shutting down some hosts doesn't matter as long as one working host is left behind + // verify that shutting down some hosts doesn't matter as long as one working host is left behind if (httpServers.length > 1 && randomBoolean()) { List updatedHttpServers = new ArrayList<>(httpServers.length - 1); int nodeIndex = randomIntBetween(0, httpServers.length - 1); @@ -188,12 +188,12 @@ public void testSyncRequests() throws IOException { int numRequests = randomIntBetween(5, 20); for (int i = 0; i < numRequests; i++) { final String method = RestClientTestUtil.randomHttpMethod(getRandom()); - //we don't test status codes that are subject to retries as they interfere with hosts being stopped + // we don't test status codes that are subject to retries as they interfere with hosts being stopped final int statusCode = randomBoolean() ? randomOkStatusCode(getRandom()) : randomErrorNoRetryStatusCode(getRandom()); Response response; try { response = restClient.performRequest(new Request(method, "/" + statusCode)); - } catch(ResponseException responseException) { + } catch (ResponseException responseException) { response = responseException.getResponse(); } assertEquals(method, response.getRequestLine().getMethod()); @@ -208,7 +208,7 @@ public void testAsyncRequests() throws Exception { final List responses = new CopyOnWriteArrayList<>(); for (int i = 0; i < numRequests; i++) { final String method = RestClientTestUtil.randomHttpMethod(getRandom()); - //we don't test status codes that are subject to retries as they interfere with hosts being stopped + // we don't test status codes that are subject to retries as they interfere with hosts being stopped final int statusCode = randomBoolean() ? randomOkStatusCode(getRandom()) : randomErrorNoRetryStatusCode(getRandom()); restClient.performRequestAsync(new Request(method, "/" + statusCode), new ResponseListener() { @Override @@ -231,8 +231,7 @@ public void onFailure(Exception exception) { Response response = testResponse.getResponse(); assertEquals(testResponse.method, response.getRequestLine().getMethod()); assertEquals(testResponse.statusCode, response.getStatusLine().getStatusCode()); - assertEquals((pathPrefix.length() > 0 ? pathPrefix : "") + "/" + testResponse.statusCode, - response.getRequestLine().getUri()); + assertEquals((pathPrefix.length() > 0 ? pathPrefix : "") + "/" + testResponse.statusCode, response.getRequestLine().getUri()); } } @@ -258,7 +257,7 @@ public void onFailure(Exception exception) { } }); if (randomBoolean()) { - //we wait for the request to get to the server-side otherwise we almost always cancel + // we wait for the request to get to the server-side otherwise we almost always cancel // the request artificially on the client-side before even sending it waitForCancelHandler.awaitRequest(); } diff --git a/client/rest/src/test/java/org/elasticsearch/client/RestClientMultipleHostsTests.java b/client/rest/src/test/java/org/elasticsearch/client/RestClientMultipleHostsTests.java index ea409dd4b7d1d..e42a260800c45 100644 --- a/client/rest/src/test/java/org/elasticsearch/client/RestClientMultipleHostsTests.java +++ b/client/rest/src/test/java/org/elasticsearch/client/RestClientMultipleHostsTests.java @@ -20,6 +20,7 @@ package org.elasticsearch.client; import com.carrotsearch.randomizedtesting.generators.RandomNumbers; + import org.apache.http.Header; import org.apache.http.HttpHost; import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; @@ -85,8 +86,10 @@ public void testRoundRobinOkStatusCodes() throws Exception { Set hostsSet = hostsSet(); for (int j = 0; j < nodes.size(); j++) { int statusCode = randomOkStatusCode(getRandom()); - Response response = RestClientSingleHostTests.performRequestSyncOrAsync(restClient, - new Request(randomHttpMethod(getRandom()), "/" + statusCode)); + Response response = RestClientSingleHostTests.performRequestSyncOrAsync( + restClient, + new Request(randomHttpMethod(getRandom()), "/" + statusCode) + ); assertEquals(statusCode, response.getStatusLine().getStatusCode()); assertTrue("host not found: " + response.getHost(), hostsSet.remove(response.getHost())); } @@ -104,10 +107,12 @@ public void testRoundRobinNoRetryErrors() throws Exception { String method = randomHttpMethod(getRandom()); int statusCode = randomErrorNoRetryStatusCode(getRandom()); try { - Response response = RestClientSingleHostTests.performRequestSyncOrAsync(restClient, - new Request(method, "/" + statusCode)); + Response response = RestClientSingleHostTests.performRequestSyncOrAsync( + restClient, + new Request(method, "/" + statusCode) + ); if (method.equals("HEAD") && statusCode == 404) { - //no exception gets thrown although we got a 404 + // no exception gets thrown although we got a 404 assertEquals(404, response.getStatusLine().getStatusCode()); assertEquals(statusCode, response.getStatusLine().getStatusCode()); assertTrue("host not found: " + response.getHost(), hostsSet.remove(response.getHost())); @@ -132,31 +137,33 @@ public void testRoundRobinNoRetryErrors() throws Exception { public void testRoundRobinRetryErrors() throws Exception { RestClient restClient = createRestClient(NodeSelector.ANY); String retryEndpoint = randomErrorRetryEndpoint(); - try { + try { RestClientSingleHostTests.performRequestSyncOrAsync(restClient, new Request(randomHttpMethod(getRandom()), retryEndpoint)); fail("request should have failed"); } catch (ResponseException e) { Set hostsSet = hostsSet(); - //first request causes all the hosts to be blacklisted, the returned exception holds one suppressed exception each + // first request causes all the hosts to be blacklisted, the returned exception holds one suppressed exception each failureListener.assertCalled(nodes); do { Response response = e.getResponse(); assertEquals(Integer.parseInt(retryEndpoint.substring(1)), response.getStatusLine().getStatusCode()); - assertTrue("host [" + response.getHost() + "] not found, most likely used multiple times", - hostsSet.remove(response.getHost())); + assertTrue( + "host [" + response.getHost() + "] not found, most likely used multiple times", + hostsSet.remove(response.getHost()) + ); if (e.getSuppressed().length > 0) { assertEquals(1, e.getSuppressed().length); Throwable suppressed = e.getSuppressed()[0]; assertThat(suppressed, instanceOf(ResponseException.class)); - e = (ResponseException)suppressed; + e = (ResponseException) suppressed; } else { e = null; } - } while(e != null); + } while (e != null); assertEquals("every host should have been used but some weren't: " + hostsSet, 0, hostsSet.size()); } catch (IOException e) { Set hostsSet = hostsSet(); - //first request causes all the hosts to be blacklisted, the returned exception holds one suppressed exception each + // first request causes all the hosts to be blacklisted, the returned exception holds one suppressed exception each failureListener.assertCalled(nodes); do { HttpHost httpHost = HttpHost.create(e.getMessage()); @@ -169,47 +176,53 @@ public void testRoundRobinRetryErrors() throws Exception { } else { e = null; } - } while(e != null); + } while (e != null); assertEquals("every host should have been used but some weren't: " + hostsSet, 0, hostsSet.size()); } int numIters = RandomNumbers.randomIntBetween(getRandom(), 2, 5); for (int i = 1; i <= numIters; i++) { - //check that one different host is resurrected at each new attempt + // check that one different host is resurrected at each new attempt Set hostsSet = hostsSet(); for (int j = 0; j < nodes.size(); j++) { retryEndpoint = randomErrorRetryEndpoint(); - try { - RestClientSingleHostTests.performRequestSyncOrAsync(restClient, - new Request(randomHttpMethod(getRandom()), retryEndpoint)); + try { + RestClientSingleHostTests.performRequestSyncOrAsync( + restClient, + new Request(randomHttpMethod(getRandom()), retryEndpoint) + ); fail("request should have failed"); } catch (ResponseException e) { Response response = e.getResponse(); assertThat(response.getStatusLine().getStatusCode(), equalTo(Integer.parseInt(retryEndpoint.substring(1)))); - assertTrue("host [" + response.getHost() + "] not found, most likely used multiple times", - hostsSet.remove(response.getHost())); - //after the first request, all hosts are blacklisted, a single one gets resurrected each time + assertTrue( + "host [" + response.getHost() + "] not found, most likely used multiple times", + hostsSet.remove(response.getHost()) + ); + // after the first request, all hosts are blacklisted, a single one gets resurrected each time failureListener.assertCalled(response.getHost()); assertEquals(0, e.getSuppressed().length); } catch (IOException e) { HttpHost httpHost = HttpHost.create(e.getMessage()); assertTrue("host [" + httpHost + "] not found, most likely used multiple times", hostsSet.remove(httpHost)); - //after the first request, all hosts are blacklisted, a single one gets resurrected each time + // after the first request, all hosts are blacklisted, a single one gets resurrected each time failureListener.assertCalled(httpHost); assertEquals(0, e.getSuppressed().length); } } assertEquals("every host should have been used but some weren't: " + hostsSet, 0, hostsSet.size()); if (getRandom().nextBoolean()) { - //mark one host back alive through a successful request and check that all requests after that are sent to it + // mark one host back alive through a successful request and check that all requests after that are sent to it HttpHost selectedHost = null; int iters = RandomNumbers.randomIntBetween(getRandom(), 2, 10); for (int y = 0; y < iters; y++) { int statusCode = randomErrorNoRetryStatusCode(getRandom()); Response response; try { - response = RestClientSingleHostTests.performRequestSyncOrAsync(restClient, - new Request(randomHttpMethod(getRandom()), "/" + statusCode)); + response = RestClientSingleHostTests.performRequestSyncOrAsync( + restClient, + new Request(randomHttpMethod(getRandom()), "/" + statusCode) + ); } catch (ResponseException e) { response = e.getResponse(); } @@ -221,20 +234,22 @@ public void testRoundRobinRetryErrors() throws Exception { } } failureListener.assertNotCalled(); - //let the selected host catch up on number of failures, it gets selected a consecutive number of times as it's the one - //selected to be retried earlier (due to lower number of failures) till all the hosts have the same number of failures + // let the selected host catch up on number of failures, it gets selected a consecutive number of times as it's the one + // selected to be retried earlier (due to lower number of failures) till all the hosts have the same number of failures for (int y = 0; y < i + 1; y++) { retryEndpoint = randomErrorRetryEndpoint(); try { - RestClientSingleHostTests.performRequestSyncOrAsync(restClient, - new Request(randomHttpMethod(getRandom()), retryEndpoint)); + RestClientSingleHostTests.performRequestSyncOrAsync( + restClient, + new Request(randomHttpMethod(getRandom()), retryEndpoint) + ); fail("request should have failed"); } catch (ResponseException e) { Response response = e.getResponse(); assertThat(response.getStatusLine().getStatusCode(), equalTo(Integer.parseInt(retryEndpoint.substring(1)))); assertThat(response.getHost(), equalTo(selectedHost)); failureListener.assertCalled(selectedHost); - } catch(IOException e) { + } catch (IOException e) { HttpHost httpHost = HttpHost.create(e.getMessage()); assertThat(httpHost, equalTo(selectedHost)); failureListener.assertCalled(selectedHost); @@ -273,9 +288,9 @@ public void testSetNodes() throws Exception { RestClient restClient = createRestClient(NodeSelector.SKIP_DEDICATED_MASTERS); List newNodes = new ArrayList<>(nodes.size()); for (int i = 0; i < nodes.size(); i++) { - Node.Roles roles = i == 0 ? - new Node.Roles(new TreeSet<>(Arrays.asList("data", "ingest"))) : - new Node.Roles(new TreeSet<>(Arrays.asList("master"))); + Node.Roles roles = i == 0 + ? new Node.Roles(new TreeSet<>(Arrays.asList("data", "ingest"))) + : new Node.Roles(new TreeSet<>(Arrays.asList("master"))); newNodes.add(new Node(nodes.get(i).getHost(), null, null, null, roles, null)); } restClient.setNodes(newNodes); @@ -292,7 +307,7 @@ public void testSetNodes() throws Exception { } private static String randomErrorRetryEndpoint() { - switch(RandomNumbers.randomIntBetween(getRandom(), 0, 3)) { + switch (RandomNumbers.randomIntBetween(getRandom(), 0, 3)) { case 0: return "/" + randomErrorRetryStatusCode(getRandom()); case 1: diff --git a/client/rest/src/test/java/org/elasticsearch/client/RestClientSingleHostIntegTests.java b/client/rest/src/test/java/org/elasticsearch/client/RestClientSingleHostIntegTests.java index 2542669e70358..ff6719986539b 100644 --- a/client/rest/src/test/java/org/elasticsearch/client/RestClientSingleHostIntegTests.java +++ b/client/rest/src/test/java/org/elasticsearch/client/RestClientSingleHostIntegTests.java @@ -23,6 +23,7 @@ import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpServer; + import org.apache.http.Consts; import org.apache.http.Header; import org.apache.http.HttpHost; @@ -97,7 +98,7 @@ public void startHttpServer() throws Exception { private HttpServer createHttpServer() throws Exception { HttpServer mockServer = MockHttpServer.createHttp(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0); mockServer.start(); - //returns a different status code depending on the path + // returns a different status code depending on the path for (int statusCode : getAllStatusCodes()) { mockServer.createContext(pathPrefix + "/" + statusCode, new ResponseHandler(statusCode)); } @@ -118,8 +119,7 @@ void cancelDone() { public void handle(HttpExchange exchange) throws IOException { try { cancelHandlerLatch.await(); - } catch (InterruptedException ignore) { - } finally { + } catch (InterruptedException ignore) {} finally { exchange.sendResponseHeaders(200, 0); exchange.close(); } @@ -135,7 +135,7 @@ private static class ResponseHandler implements HttpHandler { @Override public void handle(HttpExchange httpExchange) throws IOException { - //copy request body to response body so we can verify it was sent + // copy request body to response body so we can verify it was sent StringBuilder body = new StringBuilder(); try (InputStreamReader reader = new InputStreamReader(httpExchange.getRequestBody(), Consts.UTF_8)) { char[] buffer = new char[256]; @@ -144,7 +144,7 @@ public void handle(HttpExchange httpExchange) throws IOException { body.append(buffer, 0, read); } } - //copy request headers to response headers so we can verify they were sent + // copy request headers to response headers so we can verify they were sent Headers requestHeaders = httpExchange.getRequestHeaders(); Headers responseHeaders = httpExchange.getResponseHeaders(); for (Map.Entry> header : requestHeaders.entrySet()) { @@ -167,7 +167,8 @@ private RestClient createRestClient(final boolean useAuth, final boolean usePree credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("user", "pass")); final RestClientBuilder restClientBuilder = RestClient.builder( - new HttpHost(httpServer.getAddress().getHostString(), httpServer.getAddress().getPort())).setDefaultHeaders(defaultHeaders); + new HttpHost(httpServer.getAddress().getHostString(), httpServer.getAddress().getPort()) + ).setDefaultHeaders(defaultHeaders); if (pathPrefix.length() > 0) { restClientBuilder.setPathPrefix(pathPrefix); } @@ -228,8 +229,9 @@ public void onFailure(Exception exception) { assertTrue("timeout waiting for requests to be sent", latch.await(10, TimeUnit.SECONDS)); if (exceptions.isEmpty() == false) { - AssertionError error = new AssertionError("expected no failures but got some. see suppressed for first 10 of [" - + exceptions.size() + "] failures"); + AssertionError error = new AssertionError( + "expected no failures but got some. see suppressed for first 10 of [" + exceptions.size() + "] failures" + ); for (Exception exception : exceptions.subList(0, Math.min(10, exceptions.size()))) { error.addSuppressed(exception); } @@ -271,7 +273,7 @@ public void testRequestResetAndAbort() throws Exception { HttpHost httpHost = new HttpHost(httpServer.getAddress().getHostString(), httpServer.getAddress().getPort()); HttpGet httpGet = new HttpGet(pathPrefix + "/200"); - //calling abort before the request is sent is a no-op + // calling abort before the request is sent is a no-op httpGet.abort(); assertTrue(httpGet.isAborted()); @@ -283,8 +285,8 @@ public void testRequestResetAndAbort() throws Exception { try { future.get(); fail("expected cancellation exception"); - } catch(CancellationException e) { - //expected + } catch (CancellationException e) { + // expected } assertTrue(future.isCancelled()); } @@ -297,8 +299,8 @@ public void testRequestResetAndAbort() throws Exception { try { assertTrue(future.isDone()); future.get(); - } catch(CancellationException e) { - //expected sometimes - if the future was cancelled before executing successfully + } catch (CancellationException e) { + // expected sometimes - if the future was cancelled before executing successfully } } { @@ -370,9 +372,9 @@ public void testAgentAndMetaHeader() throws Exception { assertTrue(header.matches("elasticsearch-java/[^ ]+ \\(Java/[^)].*\\)")); // Meta header should not be overriden, test custom UA - request.setOptions(RequestOptions.DEFAULT.toBuilder() - .addHeader(RestClientBuilder.META_HEADER_NAME, "foobar") - .addHeader("User-Agent", "baz")); + request.setOptions( + RequestOptions.DEFAULT.toBuilder().addHeader(RestClientBuilder.META_HEADER_NAME, "foobar").addHeader("User-Agent", "baz") + ); esResponse = RestClientSingleHostTests.performRequestSyncOrAsync(restClient, request); header = esResponse.getHeader(RestClientBuilder.META_HEADER_NAME); assertTrue(header.matches("^es=[^,]*,jv=[^,]+,t=[^,]*,hc=.*")); @@ -465,7 +467,7 @@ public void testEncodeParams() throws Exception { * Verify that credentials are sent on the first request with preemptive auth enabled (default when provided with credentials). */ public void testPreemptiveAuthEnabled() throws Exception { - final String[] methods = {"POST", "PUT", "GET", "DELETE"}; + final String[] methods = { "POST", "PUT", "GET", "DELETE" }; try (RestClient restClient = createRestClient(true, true, true)) { for (final String method : methods) { @@ -480,7 +482,7 @@ public void testPreemptiveAuthEnabled() throws Exception { * Verify that credentials are not sent on the first request with preemptive auth disabled. */ public void testPreemptiveAuthDisabled() throws Exception { - final String[] methods = {"POST", "PUT", "GET", "DELETE"}; + final String[] methods = { "POST", "PUT", "GET", "DELETE" }; try (RestClient restClient = createRestClient(true, false, true)) { for (final String method : methods) { @@ -495,12 +497,12 @@ public void testPreemptiveAuthDisabled() throws Exception { * Verify that credentials continue to be sent even if a 401 (Unauthorized) response is received */ public void testAuthCredentialsAreNotClearedOnAuthChallenge() throws Exception { - final String[] methods = {"POST", "PUT", "GET", "DELETE"}; + final String[] methods = { "POST", "PUT", "GET", "DELETE" }; try (RestClient restClient = createRestClient(true, true, true)) { for (final String method : methods) { Header realmHeader = new BasicHeader("WWW-Authenticate", "Basic realm=\"test\""); - final Response response401 = bodyTest(restClient, method, 401, new Header[]{realmHeader}); + final Response response401 = bodyTest(restClient, method, 401, new Header[] { realmHeader }); assertThat(response401.getHeader("Authorization"), startsWith("Basic")); final Response response200 = bodyTest(restClient, method, 200, new Header[0]); @@ -520,16 +522,18 @@ public void testUrlWithoutLeadingSlash() throws Exception { } else { { Response response = RestClientSingleHostTests.performRequestSyncOrAsync(restClient, new Request("GET", "200")); - //a trailing slash gets automatically added if a pathPrefix is configured + // a trailing slash gets automatically added if a pathPrefix is configured assertEquals(200, response.getStatusLine().getStatusCode()); } { - //pathPrefix is not required to start with '/', will be added automatically - try (RestClient restClient = RestClient.builder( - new HttpHost(httpServer.getAddress().getHostString(), httpServer.getAddress().getPort())) - .setPathPrefix(pathPrefix.substring(1)).build()) { + // pathPrefix is not required to start with '/', will be added automatically + try ( + RestClient restClient = RestClient.builder( + new HttpHost(httpServer.getAddress().getHostString(), httpServer.getAddress().getPort()) + ).setPathPrefix(pathPrefix.substring(1)).build() + ) { Response response = RestClientSingleHostTests.performRequestSyncOrAsync(restClient, new Request("GET", "200")); - //a trailing slash gets automatically added if a pathPrefix is configured + // a trailing slash gets automatically added if a pathPrefix is configured assertEquals(200, response.getStatusLine().getStatusCode()); } } @@ -557,7 +561,7 @@ private Response bodyTest(RestClient client, String method, int statusCode, Head Response esResponse; try { esResponse = RestClientSingleHostTests.performRequestSyncOrAsync(client, request); - } catch(ResponseException e) { + } catch (ResponseException e) { esResponse = e.getResponse(); } assertEquals(method, esResponse.getRequestLine().getMethod()); diff --git a/client/rest/src/test/java/org/elasticsearch/client/RestClientSingleHostTests.java b/client/rest/src/test/java/org/elasticsearch/client/RestClientSingleHostTests.java index 3c4a362083390..4d17f2c89ba79 100644 --- a/client/rest/src/test/java/org/elasticsearch/client/RestClientSingleHostTests.java +++ b/client/rest/src/test/java/org/elasticsearch/client/RestClientSingleHostTests.java @@ -71,6 +71,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.atomic.AtomicReference; + import javax.net.ssl.SSLHandshakeException; import static java.util.Collections.singletonList; @@ -87,8 +88,8 @@ import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import static org.mockito.ArgumentMatchers.nullable; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.nullable; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -117,39 +118,52 @@ public void createRestClient() { node = new Node(new HttpHost("localhost", 9200)); failureListener = new HostsTrackingFailureListener(); strictDeprecationMode = randomBoolean(); - restClient = new RestClient(this.httpClient, defaultHeaders, - singletonList(node), null, failureListener, NodeSelector.ANY, strictDeprecationMode, false); + restClient = new RestClient( + this.httpClient, + defaultHeaders, + singletonList(node), + null, + failureListener, + NodeSelector.ANY, + strictDeprecationMode, + false + ); } @SuppressWarnings("unchecked") static CloseableHttpAsyncClient mockHttpClient(final ExecutorService exec) { CloseableHttpAsyncClient httpClient = mock(CloseableHttpAsyncClient.class); - when(httpClient.execute(any(HttpAsyncRequestProducer.class), any(HttpAsyncResponseConsumer.class), - any(HttpClientContext.class), nullable(FutureCallback.class))).thenAnswer((Answer>) invocationOnMock -> { - final HttpAsyncRequestProducer requestProducer = (HttpAsyncRequestProducer) invocationOnMock.getArguments()[0]; - final FutureCallback futureCallback = - (FutureCallback) invocationOnMock.getArguments()[3]; - // Call the callback asynchronous to better simulate how async http client works - return exec.submit(() -> { - if (futureCallback != null) { - try { - HttpResponse httpResponse = responseOrException(requestProducer); - futureCallback.completed(httpResponse); - } catch(Exception e) { - futureCallback.failed(e); - } - return null; + when( + httpClient.execute( + any(HttpAsyncRequestProducer.class), + any(HttpAsyncResponseConsumer.class), + any(HttpClientContext.class), + nullable(FutureCallback.class) + ) + ).thenAnswer((Answer>) invocationOnMock -> { + final HttpAsyncRequestProducer requestProducer = (HttpAsyncRequestProducer) invocationOnMock.getArguments()[0]; + final FutureCallback futureCallback = (FutureCallback) invocationOnMock.getArguments()[3]; + // Call the callback asynchronous to better simulate how async http client works + return exec.submit(() -> { + if (futureCallback != null) { + try { + HttpResponse httpResponse = responseOrException(requestProducer); + futureCallback.completed(httpResponse); + } catch (Exception e) { + futureCallback.failed(e); } - return responseOrException(requestProducer); - }); + return null; + } + return responseOrException(requestProducer); }); + }); return httpClient; } private static HttpResponse responseOrException(HttpAsyncRequestProducer requestProducer) throws Exception { - final HttpUriRequest request = (HttpUriRequest)requestProducer.generateRequest(); + final HttpUriRequest request = (HttpUriRequest) requestProducer.generateRequest(); final HttpHost httpHost = requestProducer.getTarget(); - //return the desired status code or exception depending on the path + // return the desired status code or exception depending on the path switch (request.getURI().getPath()) { case "/soe": throw new SocketTimeoutException(httpHost.toString()); @@ -170,16 +184,15 @@ private static HttpResponse responseOrException(HttpAsyncRequestProducer request StatusLine statusLine = new BasicStatusLine(new ProtocolVersion("http", 1, 1), statusCode, ""); final HttpResponse httpResponse = new BasicHttpResponse(statusLine); - //return the same body that was sent + // return the same body that was sent if (request instanceof HttpEntityEnclosingRequest) { HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity(); if (entity != null) { - assertTrue("the entity is not repeatable, cannot set it to the response directly", - entity.isRepeatable()); + assertTrue("the entity is not repeatable, cannot set it to the response directly", entity.isRepeatable()); httpResponse.setEntity(entity); } } - //return the same headers that were sent + // return the same headers that were sent httpResponse.setHeaders(request.getAllHeaders()); return httpResponse; } @@ -202,9 +215,13 @@ public void testInternalHttpRequest() throws Exception { int times = 0; for (String httpMethod : getHttpMethods()) { HttpUriRequest expectedRequest = performRandomRequest(httpMethod); - verify(httpClient, times(++times)).execute(requestArgumentCaptor.capture(), - any(HttpAsyncResponseConsumer.class), any(HttpClientContext.class), nullable(FutureCallback.class)); - HttpUriRequest actualRequest = (HttpUriRequest)requestArgumentCaptor.getValue().generateRequest(); + verify(httpClient, times(++times)).execute( + requestArgumentCaptor.capture(), + any(HttpAsyncResponseConsumer.class), + any(HttpClientContext.class), + nullable(FutureCallback.class) + ); + HttpUriRequest actualRequest = (HttpUriRequest) requestArgumentCaptor.getValue().generateRequest(); assertEquals(expectedRequest.getURI(), actualRequest.getURI()); assertEquals(expectedRequest.getClass(), actualRequest.getClass()); assertArrayEquals(expectedRequest.getAllHeaders(), actualRequest.getAllHeaders()); @@ -252,7 +269,7 @@ public void testErrorStatusCodes() throws Exception { } } } - //error status codes should cause an exception to be thrown + // error status codes should cause an exception to be thrown for (int errorStatusCode : getAllErrorStatusCodes()) { try { Request request = new Request(method, "/" + errorStatusCode); @@ -261,12 +278,12 @@ public void testErrorStatusCodes() throws Exception { } Response response = restClient.performRequest(request); if (expectedIgnores.contains(errorStatusCode)) { - //no exception gets thrown although we got an error status code, as it was configured to be ignored + // no exception gets thrown although we got an error status code, as it was configured to be ignored assertEquals(errorStatusCode, response.getStatusLine().getStatusCode()); } else { fail("request should have failed"); } - } catch(ResponseException e) { + } catch (ResponseException e) { if (expectedIgnores.contains(errorStatusCode)) { throw e; } @@ -284,11 +301,11 @@ public void testErrorStatusCodes() throws Exception { public void testPerformRequestIOExceptions() throws Exception { for (String method : getHttpMethods()) { - //IOExceptions should be let bubble up + // IOExceptions should be let bubble up try { restClient.performRequest(new Request(method, "/ioe")); fail("request should have failed"); - } catch(IOException e) { + } catch (IOException e) { // And we do all that so the thrown exception has our method in the stacktrace assertExceptionStackContainsCallingMethod(e); } @@ -296,7 +313,7 @@ public void testPerformRequestIOExceptions() throws Exception { try { restClient.performRequest(new Request(method, "/coe")); fail("request should have failed"); - } catch(ConnectTimeoutException e) { + } catch (ConnectTimeoutException e) { // And we do all that so the thrown exception has our method in the stacktrace assertExceptionStackContainsCallingMethod(e); } @@ -304,7 +321,7 @@ public void testPerformRequestIOExceptions() throws Exception { try { restClient.performRequest(new Request(method, "/soe")); fail("request should have failed"); - } catch(SocketTimeoutException e) { + } catch (SocketTimeoutException e) { // And we do all that so the thrown exception has our method in the stacktrace assertExceptionStackContainsCallingMethod(e); } @@ -312,7 +329,7 @@ public void testPerformRequestIOExceptions() throws Exception { try { restClient.performRequest(new Request(method, "/closed")); fail("request should have failed"); - } catch(ConnectionClosedException e) { + } catch (ConnectionClosedException e) { // And we do all that so the thrown exception has our method in the stacktrace assertExceptionStackContainsCallingMethod(e); } @@ -320,7 +337,7 @@ public void testPerformRequestIOExceptions() throws Exception { try { restClient.performRequest(new Request(method, "/handshake")); fail("request should have failed"); - } catch(SSLHandshakeException e) { + } catch (SSLHandshakeException e) { // And we do all that so the thrown exception has our method in the stacktrace assertExceptionStackContainsCallingMethod(e); } @@ -376,7 +393,7 @@ public void testBody() throws Exception { try { restClient.performRequest(request); fail("request should have failed"); - } catch(ResponseException e) { + } catch (ResponseException e) { Response response = e.getResponse(); assertThat(response.getStatusLine().getStatusCode(), equalTo(errorStatusCode)); assertThat(EntityUtils.toString(response.getEntity()), equalTo(body)); @@ -390,7 +407,7 @@ public void testBody() throws Exception { try { performRequestSyncOrAsync(restClient, request); fail("request should have failed"); - } catch(UnsupportedOperationException e) { + } catch (UnsupportedOperationException e) { assertThat(e.getMessage(), equalTo(method + " with body is not supported")); } } @@ -413,7 +430,7 @@ public void testHeaders() throws Exception { Response esResponse; try { esResponse = performRequestSyncOrAsync(restClient, request); - } catch(ResponseException e) { + } catch (ResponseException e) { esResponse = e.getResponse(); } assertThat(esResponse.getStatusLine().getStatusCode(), equalTo(statusCode)); @@ -428,11 +445,10 @@ public void testDeprecationWarnings() throws Exception { assertDeprecationWarnings(singletonList(formatWarningWithoutDate(chars)), singletonList(chars)); assertDeprecationWarnings(singletonList(formatWarning(chars)), singletonList(chars)); assertDeprecationWarnings( - Arrays.asList(formatWarning(chars), "another one", "and another"), - Arrays.asList(chars, "another one", "and another")); - assertDeprecationWarnings( - Arrays.asList("ignorable one", "and another"), - Arrays.asList("ignorable one", "and another")); + Arrays.asList(formatWarning(chars), "another one", "and another"), + Arrays.asList(chars, "another one", "and another") + ); + assertDeprecationWarnings(Arrays.asList("ignorable one", "and another"), Arrays.asList("ignorable one", "and another")); assertDeprecationWarnings(singletonList("exact"), singletonList("exact")); assertDeprecationWarnings(Collections.emptyList(), Collections.emptyList()); @@ -546,7 +562,7 @@ private HttpUriRequest performRandomRequest(String method) throws Exception { } } if (randomBoolean()) { - //randomly add some ignore parameter, which doesn't get sent as part of the request + // randomly add some ignore parameter, which doesn't get sent as part of the request String ignore = Integer.toString(randomFrom(RestClientTestUtil.getAllErrorStatusCodes())); if (randomBoolean()) { ignore += "," + randomFrom(RestClientTestUtil.getAllErrorStatusCodes()); @@ -556,7 +572,7 @@ private HttpUriRequest performRandomRequest(String method) throws Exception { URI uri = uriBuilder.build(); HttpUriRequest expectedRequest; - switch(method) { + switch (method) { case "DELETE": expectedRequest = new HttpDeleteWithEntity(uri); break; @@ -610,14 +626,14 @@ private HttpUriRequest performRandomRequest(String method) throws Exception { } try { performRequestSyncOrAsync(restClient, request); - } catch(Exception e) { - //all good + } catch (Exception e) { + // all good } return expectedRequest; } static Response performRequestSyncOrAsync(RestClient restClient, Request request) throws Exception { - //randomize between sync and async methods + // randomize between sync and async methods if (randomBoolean()) { return restClient.performRequest(request); } else { @@ -661,8 +677,7 @@ private static void assertExceptionStackContainsCallingMethod(Throwable t) { // 2 is the caller, what we want StackTraceElement myMethod = Thread.currentThread().getStackTrace()[2]; for (StackTraceElement se : t.getStackTrace()) { - if (se.getClassName().equals(myMethod.getClassName()) - && se.getMethodName().equals(myMethod.getMethodName())) { + if (se.getClassName().equals(myMethod.getClassName()) && se.getMethodName().equals(myMethod.getMethodName())) { return; } } diff --git a/client/rest/src/test/java/org/elasticsearch/client/RestClientTests.java b/client/rest/src/test/java/org/elasticsearch/client/RestClientTests.java index 1cee60a897a74..ad888e5231863 100644 --- a/client/rest/src/test/java/org/elasticsearch/client/RestClientTests.java +++ b/client/rest/src/test/java/org/elasticsearch/client/RestClientTests.java @@ -172,10 +172,7 @@ public void testSetNodesWrongArguments() throws IOException { assertEquals("node cannot be null", e.getMessage()); } try (RestClient restClient = createRestClient()) { - restClient.setNodes(Arrays.asList( - new Node(new HttpHost("localhost", 9200)), - null, - new Node(new HttpHost("localhost", 9201)))); + restClient.setNodes(Arrays.asList(new Node(new HttpHost("localhost", 9200)), null, new Node(new HttpHost("localhost", 9201)))); fail("setNodes should have failed"); } catch (NullPointerException e) { assertEquals("node cannot be null", e.getMessage()); @@ -262,8 +259,8 @@ public String toString() { */ { String message = "NodeSelector [NONE] rejected all nodes, living [" - + "[host=http://1, version=1], [host=http://2, version=2], " - + "[host=http://3, version=3]] and dead []"; + + "[host=http://1, version=1], [host=http://2, version=2], " + + "[host=http://3, version=3]] and dead []"; assertEquals(message, assertSelectAllRejected(nodeTuple, emptyBlacklist, noNodes)); } @@ -300,8 +297,8 @@ public String toString() { * their nodes are blacklisted AND blocked. */ String message = "NodeSelector [NONE] rejected all nodes, living [] and dead [" - + "[host=http://1, version=1], [host=http://2, version=2], " - + "[host=http://3, version=3]]"; + + "[host=http://1, version=1], [host=http://2, version=2], " + + "[host=http://3, version=3]]"; assertEquals(message, assertSelectAllRejected(nodeTuple, blacklist, noNodes)); /* @@ -328,16 +325,19 @@ public String toString() { } } - private void assertSelectLivingHosts(List expectedNodes, NodeTuple> nodeTuple, - Map blacklist, NodeSelector nodeSelector) throws IOException { + private void assertSelectLivingHosts( + List expectedNodes, + NodeTuple> nodeTuple, + Map blacklist, + NodeSelector nodeSelector + ) throws IOException { int iterations = 1000; AtomicInteger lastNodeIndex = new AtomicInteger(0); assertEquals(expectedNodes, RestClient.selectNodes(nodeTuple, blacklist, lastNodeIndex, nodeSelector)); // Calling it again rotates the set of results for (int i = 1; i < iterations; i++) { Collections.rotate(expectedNodes, 1); - assertEquals("iteration " + i, expectedNodes, - RestClient.selectNodes(nodeTuple, blacklist, lastNodeIndex, nodeSelector)); + assertEquals("iteration " + i, expectedNodes, RestClient.selectNodes(nodeTuple, blacklist, lastNodeIndex, nodeSelector)); } } @@ -345,8 +345,11 @@ private void assertSelectLivingHosts(List expectedNodes, NodeTuple> nodeTuple, - Map blacklist, NodeSelector nodeSelector) { + private static String assertSelectAllRejected( + NodeTuple> nodeTuple, + Map blacklist, + NodeSelector nodeSelector + ) { try { RestClient.selectNodes(nodeTuple, blacklist, new AtomicInteger(0), nodeSelector); throw new AssertionError("expected selectHosts to fail"); @@ -371,25 +374,25 @@ public void testRoundRobin() throws IOException { } NodeTuple> nodeTuple = new NodeTuple<>(nodes, authCache); - //test the transition from negative to positive values + // test the transition from negative to positive values AtomicInteger lastNodeIndex = new AtomicInteger(-numNodes); assertNodes(nodeTuple, lastNodeIndex, 50); assertEquals(-numNodes + 50, lastNodeIndex.get()); - //test the highest positive values up to MAX_VALUE + // test the highest positive values up to MAX_VALUE lastNodeIndex.set(Integer.MAX_VALUE - numNodes * 10); assertNodes(nodeTuple, lastNodeIndex, numNodes * 10); assertEquals(Integer.MAX_VALUE, lastNodeIndex.get()); - //test the transition from MAX_VALUE to MIN_VALUE - //this is the only time where there is most likely going to be a jump from a node - //to another one that's not necessarily the next one. + // test the transition from MAX_VALUE to MIN_VALUE + // this is the only time where there is most likely going to be a jump from a node + // to another one that's not necessarily the next one. assertEquals(Integer.MIN_VALUE, lastNodeIndex.incrementAndGet()); assertNodes(nodeTuple, lastNodeIndex, 50); assertEquals(Integer.MIN_VALUE + 50, lastNodeIndex.get()); } - public void testIsRunning(){ + public void testIsRunning() { List nodes = Collections.singletonList(new Node(new HttpHost("localhost", 9200))); CloseableHttpAsyncClient client = mock(CloseableHttpAsyncClient.class); RestClient restClient = new RestClient(client, new Header[] {}, nodes, null, null, null, false, false); @@ -409,8 +412,12 @@ private static void assertNodes(NodeTuple> nodeTuple, AtomicInteger l */ int expectedOffset = distance > 0 ? nodeTuple.nodes.size() - distance : Math.abs(distance); for (int i = 0; i < runs; i++) { - Iterable selectedNodes = RestClient.selectNodes(nodeTuple, Collections.emptyMap(), - lastNodeIndex, NodeSelector.ANY); + Iterable selectedNodes = RestClient.selectNodes( + nodeTuple, + Collections.emptyMap(), + lastNodeIndex, + NodeSelector.ANY + ); List expectedNodes = nodeTuple.nodes; int index = 0; for (Node actualNode : selectedNodes) { diff --git a/client/rest/src/test/java/org/elasticsearch/client/documentation/RestClientDocumentation.java b/client/rest/src/test/java/org/elasticsearch/client/documentation/RestClientDocumentation.java index cb4374b93ea95..b159fff2ea651 100644 --- a/client/rest/src/test/java/org/elasticsearch/client/documentation/RestClientDocumentation.java +++ b/client/rest/src/test/java/org/elasticsearch/client/documentation/RestClientDocumentation.java @@ -48,7 +48,6 @@ import org.elasticsearch.client.RestClientBuilder; import org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback; -import javax.net.ssl.SSLContext; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; @@ -62,6 +61,8 @@ import java.util.Iterator; import java.util.concurrent.CountDownLatch; +import javax.net.ssl.SSLContext; + /** * This class is used to generate the Java low-level REST client documentation. * You need to wrap your code between two tags like: diff --git a/client/sniffer/src/main/java/org/elasticsearch/client/sniff/ElasticsearchNodesSniffer.java b/client/sniffer/src/main/java/org/elasticsearch/client/sniff/ElasticsearchNodesSniffer.java index c7e3b8534f607..3ac3f6288d7a6 100644 --- a/client/sniffer/src/main/java/org/elasticsearch/client/sniff/ElasticsearchNodesSniffer.java +++ b/client/sniffer/src/main/java/org/elasticsearch/client/sniff/ElasticsearchNodesSniffer.java @@ -22,6 +22,7 @@ import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonToken; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.http.HttpEntity; @@ -168,12 +169,11 @@ private static Node readNode(String nodeId, JsonParser parser, Scheme scheme) th URI publishAddressAsURI; // ES7 cname/ip:port format - if(address.contains("/")) { + if (address.contains("/")) { String[] cnameAndURI = address.split("/", 2); publishAddressAsURI = URI.create(scheme + "://" + cnameAndURI[1]); host = cnameAndURI[0]; - } - else { + } else { publishAddressAsURI = URI.create(scheme + "://" + address); host = publishAddressAsURI.getHost(); } @@ -181,8 +181,9 @@ private static Node readNode(String nodeId, JsonParser parser, Scheme scheme) th } else if (parser.currentToken() == JsonToken.START_ARRAY && "bound_address".equals(parser.getCurrentName())) { while (parser.nextToken() != JsonToken.END_ARRAY) { URI boundAddressAsURI = URI.create(scheme + "://" + parser.getValueAsString()); - boundHosts.add(new HttpHost(boundAddressAsURI.getHost(), boundAddressAsURI.getPort(), - boundAddressAsURI.getScheme())); + boundHosts.add( + new HttpHost(boundAddressAsURI.getHost(), boundAddressAsURI.getPort(), boundAddressAsURI.getScheme()) + ); } } else if (parser.getCurrentToken() == JsonToken.START_OBJECT) { parser.skipChildren(); @@ -219,7 +220,7 @@ private static Node readNode(String nodeId, JsonParser parser, Scheme scheme) th } } } - //http section is not present if http is not enabled on the node, ignore such nodes + // http section is not present if http is not enabled on the node, ignore such nodes if (publishedHost == null) { logger.debug("skipping node [" + nodeId + "] with http disabled"); return null; @@ -264,11 +265,9 @@ private static Node readNode(String nodeId, JsonParser parser, Scheme scheme) th } else { assert sawRoles : "didn't see roles for [" + nodeId + "]"; } - assert boundHosts.contains(publishedHost) : - "[" + nodeId + "] doesn't make sense! publishedHost should be in boundHosts"; + assert boundHosts.contains(publishedHost) : "[" + nodeId + "] doesn't make sense! publishedHost should be in boundHosts"; logger.trace("adding node [" + nodeId + "]"); - return new Node(publishedHost, boundHosts, name, version, new Roles(roles), - unmodifiableMap(realAttributes)); + return new Node(publishedHost, boundHosts, name, version, new Roles(roles), unmodifiableMap(realAttributes)); } /** @@ -277,29 +276,28 @@ private static Node readNode(String nodeId, JsonParser parser, Scheme scheme) th * either of those, or throws an IOException if the attribute * came back in a strange way. */ - private static Boolean v2RoleAttributeValue(Map> attributes, - String name, Boolean defaultValue) throws IOException { + private static Boolean v2RoleAttributeValue(Map> attributes, String name, Boolean defaultValue) + throws IOException { List valueList = attributes.remove(name); if (valueList == null) { return defaultValue; } if (valueList.size() != 1) { - throw new IOException("expected only a single attribute value for [" + name + "] but got " - + valueList); + throw new IOException("expected only a single attribute value for [" + name + "] but got " + valueList); } switch (valueList.get(0)) { - case "true": - return true; - case "false": - return false; - default: - throw new IOException("expected [" + name + "] to be either [true] or [false] but was [" - + valueList.get(0) + "]"); + case "true": + return true; + case "false": + return false; + default: + throw new IOException("expected [" + name + "] to be either [true] or [false] but was [" + valueList.get(0) + "]"); } } public enum Scheme { - HTTP("http"), HTTPS("https"); + HTTP("http"), + HTTPS("https"); private final String name; diff --git a/client/sniffer/src/main/java/org/elasticsearch/client/sniff/Sniffer.java b/client/sniffer/src/main/java/org/elasticsearch/client/sniff/Sniffer.java index b55378f239d20..ea434d054a90a 100644 --- a/client/sniffer/src/main/java/org/elasticsearch/client/sniff/Sniffer.java +++ b/client/sniffer/src/main/java/org/elasticsearch/client/sniff/Sniffer.java @@ -94,7 +94,7 @@ public void run() { * it will also schedule a new round after sniffAfterFailureDelay ms. */ public void sniffOnFailure() { - //sniffOnFailure does nothing until the initial sniffing round has been completed + // sniffOnFailure does nothing until the initial sniffing round has been completed if (initialized.get()) { /* * If sniffing is already running, there is no point in scheduling another round right after the current one. @@ -113,7 +113,9 @@ public void sniffOnFailure() { } enum TaskState { - WAITING, SKIPPED, STARTED + WAITING, + SKIPPED, + STARTED } class Task implements Runnable { @@ -141,12 +143,11 @@ public void run() { } finally { Task task = new Task(sniffIntervalMillis); Future future = scheduler.schedule(task, nextTaskDelay); - //tasks are run by a single threaded executor, so swapping is safe with a simple volatile variable + // tasks are run by a single threaded executor, so swapping is safe with a simple volatile variable ScheduledTask previousTask = nextScheduledTask; nextScheduledTask = new ScheduledTask(task, future); - assert initialized.get() == false || - previousTask.task.isSkipped() || previousTask.task.hasStarted() : "task that we are replacing is neither " + - "cancelled nor has it ever started"; + assert initialized.get() == false || previousTask.task.isSkipped() || previousTask.task.hasStarted() + : "task that we are replacing is neither " + "cancelled nor has it ever started"; } } diff --git a/client/sniffer/src/test/java/org/elasticsearch/client/sniff/ElasticsearchNodesSnifferParseTests.java b/client/sniffer/src/test/java/org/elasticsearch/client/sniff/ElasticsearchNodesSnifferParseTests.java index d3a3701c0e9c9..469245bb69b33 100644 --- a/client/sniffer/src/test/java/org/elasticsearch/client/sniff/ElasticsearchNodesSnifferParseTests.java +++ b/client/sniffer/src/test/java/org/elasticsearch/client/sniff/ElasticsearchNodesSnifferParseTests.java @@ -20,6 +20,7 @@ package org.elasticsearch.client.sniff; import com.fasterxml.jackson.core.JsonFactory; + import org.apache.http.HttpEntity; import org.apache.http.HttpHost; import org.apache.http.entity.ContentType; @@ -73,43 +74,50 @@ private void checkFile(String file, Node... expected) throws IOException { } public void test2x() throws IOException { - checkFile("2.0.0_nodes_http.json", - node(9200, "m1", "2.0.0", true, false, false), - node(9201, "m2", "2.0.0", true, true, false), - node(9202, "m3", "2.0.0", true, false, false), - node(9203, "d1", "2.0.0", false, true, false), - node(9204, "d2", "2.0.0", false, true, false), - node(9205, "d3", "2.0.0", false, true, false), - node(9206, "c1", "2.0.0", false, false, false), - node(9207, "c2", "2.0.0", false, false, false)); + checkFile( + "2.0.0_nodes_http.json", + node(9200, "m1", "2.0.0", true, false, false), + node(9201, "m2", "2.0.0", true, true, false), + node(9202, "m3", "2.0.0", true, false, false), + node(9203, "d1", "2.0.0", false, true, false), + node(9204, "d2", "2.0.0", false, true, false), + node(9205, "d3", "2.0.0", false, true, false), + node(9206, "c1", "2.0.0", false, false, false), + node(9207, "c2", "2.0.0", false, false, false) + ); } public void test5x() throws IOException { - checkFile("5.0.0_nodes_http.json", - node(9200, "m1", "5.0.0", true, false, true), - node(9201, "m2", "5.0.0", true, true, true), - node(9202, "m3", "5.0.0", true, false, true), - node(9203, "d1", "5.0.0", false, true, true), - node(9204, "d2", "5.0.0", false, true, true), - node(9205, "d3", "5.0.0", false, true, true), - node(9206, "c1", "5.0.0", false, false, true), - node(9207, "c2", "5.0.0", false, false, true)); + checkFile( + "5.0.0_nodes_http.json", + node(9200, "m1", "5.0.0", true, false, true), + node(9201, "m2", "5.0.0", true, true, true), + node(9202, "m3", "5.0.0", true, false, true), + node(9203, "d1", "5.0.0", false, true, true), + node(9204, "d2", "5.0.0", false, true, true), + node(9205, "d3", "5.0.0", false, true, true), + node(9206, "c1", "5.0.0", false, false, true), + node(9207, "c2", "5.0.0", false, false, true) + ); } public void test6x() throws IOException { - checkFile("6.0.0_nodes_http.json", - node(9200, "m1", "6.0.0", true, false, true), - node(9201, "m2", "6.0.0", true, true, true), - node(9202, "m3", "6.0.0", true, false, true), - node(9203, "d1", "6.0.0", false, true, true), - node(9204, "d2", "6.0.0", false, true, true), - node(9205, "d3", "6.0.0", false, true, true), - node(9206, "c1", "6.0.0", false, false, true), - node(9207, "c2", "6.0.0", false, false, true)); + checkFile( + "6.0.0_nodes_http.json", + node(9200, "m1", "6.0.0", true, false, true), + node(9201, "m2", "6.0.0", true, true, true), + node(9202, "m3", "6.0.0", true, false, true), + node(9203, "d1", "6.0.0", false, true, true), + node(9204, "d2", "6.0.0", false, true, true), + node(9205, "d3", "6.0.0", false, true, true), + node(9206, "c1", "6.0.0", false, false, true), + node(9207, "c2", "6.0.0", false, false, true) + ); } public void test7x() throws IOException { - checkFile("7.3.0_nodes_http.json", + checkFile( + "7.3.0_nodes_http.json", node(9200, "m1", "7.3.0", "master", "ingest"), node(9201, "m2", "7.3.0", "master", "data", "ingest"), node(9202, "m3", "7.3.0", "master", "ingest"), @@ -117,7 +125,8 @@ public void test7x() throws IOException { node(9204, "d2", "7.3.0", "data", "ingest"), node(9205, "d3", "7.3.0", "data", "ingest"), node(9206, "c1", "7.3.0", "ingest"), - node(9207, "c2", "7.3.0", "ingest")); + node(9207, "c2", "7.3.0", "ingest") + ); } public void testParsingPublishAddressWithPreES7Format() throws IOException { diff --git a/client/sniffer/src/test/java/org/elasticsearch/client/sniff/ElasticsearchNodesSnifferTests.java b/client/sniffer/src/test/java/org/elasticsearch/client/sniff/ElasticsearchNodesSnifferTests.java index ded1f5316f369..55ec747fcdbf5 100644 --- a/client/sniffer/src/test/java/org/elasticsearch/client/sniff/ElasticsearchNodesSnifferTests.java +++ b/client/sniffer/src/test/java/org/elasticsearch/client/sniff/ElasticsearchNodesSnifferTests.java @@ -27,6 +27,7 @@ import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpServer; + import org.apache.http.Consts; import org.apache.http.HttpHost; import org.apache.http.client.methods.HttpGet; @@ -90,7 +91,7 @@ public void testConstructorValidation() throws IOException { try { new ElasticsearchNodesSniffer(null, 1, ElasticsearchNodesSniffer.Scheme.HTTP); fail("should have failed"); - } catch(NullPointerException e) { + } catch (NullPointerException e) { assertEquals("restClient cannot be null", e.getMessage()); } HttpHost httpHost = new HttpHost(httpServer.getAddress().getHostString(), httpServer.getAddress().getPort()); @@ -102,8 +103,11 @@ public void testConstructorValidation() throws IOException { assertEquals(e.getMessage(), "scheme cannot be null"); } try { - new ElasticsearchNodesSniffer(restClient, RandomNumbers.randomIntBetween(getRandom(), Integer.MIN_VALUE, 0), - ElasticsearchNodesSniffer.Scheme.HTTP); + new ElasticsearchNodesSniffer( + restClient, + RandomNumbers.randomIntBetween(getRandom(), Integer.MIN_VALUE, 0), + ElasticsearchNodesSniffer.Scheme.HTTP + ); fail("should have failed"); } catch (IllegalArgumentException e) { assertEquals(e.getMessage(), "sniffRequestTimeoutMillis must be greater than 0"); @@ -121,17 +125,22 @@ public void testSniffNodes() throws IOException { fail("sniffNodes should have failed"); } assertEquals(sniffResponse.result, sniffedNodes); - } catch(ResponseException e) { + } catch (ResponseException e) { Response response = e.getResponse(); if (sniffResponse.isFailure) { - final String errorPrefix = "method [GET], host [" + httpHost + "], URI [/_nodes/http?timeout=" + sniffRequestTimeout + final String errorPrefix = "method [GET], host [" + + httpHost + + "], URI [/_nodes/http?timeout=" + + sniffRequestTimeout + "ms], status line [HTTP/1.1"; assertThat(e.getMessage(), startsWith(errorPrefix)); assertThat(e.getMessage(), containsString(Integer.toString(sniffResponse.nodesInfoResponseCode))); assertThat(response.getHost(), equalTo(httpHost)); assertThat(response.getStatusLine().getStatusCode(), equalTo(sniffResponse.nodesInfoResponseCode)); - assertThat(response.getRequestLine().toString(), - equalTo("GET /_nodes/http?timeout=" + sniffRequestTimeout + "ms HTTP/1.1")); + assertThat( + response.getRequestLine().toString(), + equalTo("GET /_nodes/http?timeout=" + sniffRequestTimeout + "ms HTTP/1.1") + ); } else { fail("sniffNodes should have succeeded: " + response.getStatusLine()); } @@ -238,10 +247,14 @@ private static SniffResponse buildSniffResponse(ElasticsearchNodesSniffer.Scheme nodeRoles.add("ingest"); } - Node node = new Node(publishHost, boundHosts, randomAsciiAlphanumOfLength(5), - randomAsciiAlphanumOfLength(5), - new Node.Roles(nodeRoles), - attributes); + Node node = new Node( + publishHost, + boundHosts, + randomAsciiAlphanumOfLength(5), + randomAsciiAlphanumOfLength(5), + new Node.Roles(nodeRoles), + attributes + ); generator.writeObjectFieldStart(nodeId); if (getRandom().nextBoolean()) { @@ -274,8 +287,9 @@ private static SniffResponse buildSniffResponse(ElasticsearchNodesSniffer.Scheme generator.writeEndObject(); } - List roles = Arrays.asList(new String[]{"master", "data", "ingest", - "data_content", "data_hot", "data_warm", "data_cold", "data_frozen"}); + List roles = Arrays.asList( + new String[] { "master", "data", "ingest", "data_content", "data_hot", "data_warm", "data_cold", "data_frozen" } + ); Collections.shuffle(roles, getRandom()); generator.writeArrayFieldStart("roles"); for (String role : roles) { diff --git a/client/sniffer/src/test/java/org/elasticsearch/client/sniff/SniffOnFailureListenerTests.java b/client/sniffer/src/test/java/org/elasticsearch/client/sniff/SniffOnFailureListenerTests.java index 3c3868d4cd514..226ef41798d68 100644 --- a/client/sniffer/src/test/java/org/elasticsearch/client/sniff/SniffOnFailureListenerTests.java +++ b/client/sniffer/src/test/java/org/elasticsearch/client/sniff/SniffOnFailureListenerTests.java @@ -35,14 +35,14 @@ public void testSetSniffer() throws Exception { try { listener.onFailure(null); fail("should have failed"); - } catch(IllegalStateException e) { + } catch (IllegalStateException e) { assertEquals("sniffer was not set, unable to sniff on failure", e.getMessage()); } try { listener.setSniffer(null); fail("should have failed"); - } catch(NullPointerException e) { + } catch (NullPointerException e) { assertEquals("sniffer must not be null", e.getMessage()); } @@ -52,7 +52,7 @@ public void testSetSniffer() throws Exception { try { listener.setSniffer(sniffer); fail("should have failed"); - } catch(IllegalStateException e) { + } catch (IllegalStateException e) { assertEquals("sniffer can only be set once", e.getMessage()); } listener.onFailure(new Node(new HttpHost("localhost", 9200))); diff --git a/client/sniffer/src/test/java/org/elasticsearch/client/sniff/SnifferBuilderTests.java b/client/sniffer/src/test/java/org/elasticsearch/client/sniff/SnifferBuilderTests.java index 57f9a21560417..fb1dcc330aaa2 100644 --- a/client/sniffer/src/test/java/org/elasticsearch/client/sniff/SnifferBuilderTests.java +++ b/client/sniffer/src/test/java/org/elasticsearch/client/sniff/SnifferBuilderTests.java @@ -19,6 +19,7 @@ package org.elasticsearch.client.sniff; import com.carrotsearch.randomizedtesting.generators.RandomNumbers; + import org.apache.http.HttpHost; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClientTestCase; @@ -40,33 +41,31 @@ public void testBuild() throws Exception { try { Sniffer.builder(null).build(); fail("should have failed"); - } catch(NullPointerException e) { + } catch (NullPointerException e) { assertEquals("restClient cannot be null", e.getMessage()); } try { Sniffer.builder(client).setSniffIntervalMillis(RandomNumbers.randomIntBetween(getRandom(), Integer.MIN_VALUE, 0)); fail("should have failed"); - } catch(IllegalArgumentException e) { + } catch (IllegalArgumentException e) { assertEquals("sniffIntervalMillis must be greater than 0", e.getMessage()); } try { Sniffer.builder(client).setSniffAfterFailureDelayMillis(RandomNumbers.randomIntBetween(getRandom(), Integer.MIN_VALUE, 0)); fail("should have failed"); - } catch(IllegalArgumentException e) { + } catch (IllegalArgumentException e) { assertEquals("sniffAfterFailureDelayMillis must be greater than 0", e.getMessage()); } - try { Sniffer.builder(client).setNodesSniffer(null); fail("should have failed"); - } catch(NullPointerException e) { + } catch (NullPointerException e) { assertEquals("nodesSniffer cannot be null", e.getMessage()); } - try (Sniffer sniffer = Sniffer.builder(client).build()) { assertNotNull(sniffer); } diff --git a/client/sniffer/src/test/java/org/elasticsearch/client/sniff/SnifferTests.java b/client/sniffer/src/test/java/org/elasticsearch/client/sniff/SnifferTests.java index 7be302c878845..d0aca03e6fbaf 100644 --- a/client/sniffer/src/test/java/org/elasticsearch/client/sniff/SnifferTests.java +++ b/client/sniffer/src/test/java/org/elasticsearch/client/sniff/SnifferTests.java @@ -91,7 +91,7 @@ public void shutdown() { }; CountingNodesSniffer nodesSniffer = new CountingNodesSniffer(); int iters = randomIntBetween(5, 30); - try (Sniffer sniffer = new Sniffer(restClient, nodesSniffer, noOpScheduler, 1000L, -1)){ + try (Sniffer sniffer = new Sniffer(restClient, nodesSniffer, noOpScheduler, 1000L, -1)) { { assertEquals(1, restClient.getNodes().size()); Node node = restClient.getNodes().get(0); @@ -118,7 +118,7 @@ public void shutdown() { assertEquals(expectedNodes, restClient.getNodes()); lastNodes = restClient.getNodes(); } - } catch(IOException e) { + } catch (IOException e) { if (nodesSniffer.failures.get() > failures) { failures++; assertEquals("communication breakdown", e.getMessage()); @@ -158,11 +158,11 @@ public Future schedule(Sniffer.Task task, long delayMillis) { assertEquals(sniffInterval, task.nextTaskDelay); int numberOfRuns = runs.getAndDecrement(); if (numberOfRuns == iters) { - //the first call is to schedule the first sniff round from the Sniffer constructor, with delay O + // the first call is to schedule the first sniff round from the Sniffer constructor, with delay O assertEquals(0L, delayMillis); assertEquals(sniffInterval, task.nextTaskDelay); } else { - //all of the subsequent times "schedule" is called with delay set to the configured sniff interval + // all of the subsequent times "schedule" is called with delay set to the configured sniff interval assertEquals(sniffInterval, delayMillis); assertEquals(sniffInterval, task.nextTaskDelay); if (numberOfRuns == 0) { @@ -170,7 +170,7 @@ public Future schedule(Sniffer.Task task, long delayMillis) { return null; } } - //we submit rather than scheduling to make the test quick and not depend on time + // we submit rather than scheduling to make the test quick and not depend on time Future future = executor.submit(task); futures.add(future); if (numberOfRuns == 1) { @@ -182,15 +182,15 @@ public Future schedule(Sniffer.Task task, long delayMillis) { @Override public void shutdown() { - //the executor is closed externally, shutdown is tested separately + // the executor is closed externally, shutdown is tested separately } }; try { new Sniffer(restClient, nodesSniffer, scheduler, sniffInterval, sniffAfterFailureDelay); assertTrue("timeout waiting for sniffing rounds to be completed", completionLatch.await(1000, TimeUnit.MILLISECONDS)); assertEquals(iters, futures.size()); - //the last future is the only one that may not be completed yet, as the count down happens - //while scheduling the next round which is still part of the execution of the runnable itself. + // the last future is the only one that may not be completed yet, as the count down happens + // while scheduling the next round which is still part of the execution of the runnable itself. assertTrue(lastTask.get().hasStarted()); lastFuture.get().get(); for (Future future : futures) { @@ -223,7 +223,7 @@ public void testClose() { @Override public Future schedule(Sniffer.Task task, long delayMillis) { if (initialized.compareAndSet(false, true)) { - //run from the same thread so the sniffer gets for sure initialized and the scheduled task gets cancelled on close + // run from the same thread so the sniffer gets for sure initialized and the scheduled task gets cancelled on close task.run(); } return future; @@ -259,8 +259,7 @@ public Future schedule(Sniffer.Task task, long delayMillis) { } @Override - public void shutdown() { - } + public void shutdown() {} }; Sniffer sniffer = new Sniffer(restClient, nodesSniffer, scheduler, sniffInterval, sniffAfterFailureDelay); @@ -307,8 +306,8 @@ public void run() { try { task.run(); } finally { - //we need to make sure that the sniffer is initialized, so the sniffOnFailure - //call does what it needs to do. Otherwise nothing happens until initialized. + // we need to make sure that the sniffer is initialized, so the sniffOnFailure + // call does what it needs to do. Otherwise nothing happens until initialized. initializingLatch.countDown(); } } @@ -349,8 +348,7 @@ private Future scheduleOrSubmit(Sniffer.Task task) { } @Override - public void shutdown() { - } + public void shutdown() {} }; final Sniffer sniffer = new Sniffer(restClient, nodesSniffer, scheduler, sniffInterval, sniffAfterFailureDelay); assertTrue("timeout waiting for sniffer to get initialized", initializingLatch.await(1000, TimeUnit.MILLISECONDS)); @@ -358,8 +356,8 @@ public void shutdown() { ExecutorService onFailureExecutor = Executors.newFixedThreadPool(randomIntBetween(5, 20)); Set> onFailureFutures = new CopyOnWriteArraySet<>(); try { - //with tasks executing quickly one after each other, it is very likely that the onFailure round gets skipped - //as another round is already running. We retry till enough runs get through as that's what we want to test. + // with tasks executing quickly one after each other, it is very likely that the onFailure round gets skipped + // as another round is already running. We retry till enough runs get through as that's what we want to test. while (onFailureTasks.size() < minNumOnFailureRounds) { onFailureFutures.add(onFailureExecutor.submit(new Runnable() { @Override @@ -430,13 +428,12 @@ private static boolean assertTaskCancelledOrCompleted(Sniffer.ScheduledTask task try { task.future.get(); fail("cancellation exception should have been thrown"); - } catch(CancellationException ignore) { - } + } catch (CancellationException ignore) {} return false; } else { try { assertNull(task.future.get()); - } catch(CancellationException ignore) { + } catch (CancellationException ignore) { assertTrue(task.future.isCancelled()); } assertTrue(task.future.isDone()); @@ -455,8 +452,7 @@ public Future schedule(Sniffer.Task task, long delayMillis) { } @Override - public void shutdown() { - } + public void shutdown() {} }; Sniffer sniffer = new Sniffer(restClient, nodesSniffer, noOpScheduler, 0L, 0L); ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); @@ -475,32 +471,32 @@ public void shutdown() { boolean skip = scheduledTask.skip(); try { assertNull(future.get()); - } catch(CancellationException ignore) { + } catch (CancellationException ignore) { assertTrue(future.isCancelled()); } if (skip) { - //the task was either cancelled before starting, in which case it will never start (thanks to Future#cancel), - //or skipped, in which case it will run but do nothing (thanks to Task#skip). - //Here we want to make sure that whenever skip returns true, the task either won't run or it won't do anything, - //otherwise we may end up with parallel sniffing tracks given that each task schedules the following one. We need to + // the task was either cancelled before starting, in which case it will never start (thanks to Future#cancel), + // or skipped, in which case it will run but do nothing (thanks to Task#skip). + // Here we want to make sure that whenever skip returns true, the task either won't run or it won't do anything, + // otherwise we may end up with parallel sniffing tracks given that each task schedules the following one. We need to // make sure that onFailure takes scheduling over while at the same time ordinary rounds don't go on. assertFalse(task.hasStarted()); assertTrue(task.isSkipped()); assertTrue(future.isCancelled()); assertTrue(future.isDone()); } else { - //if a future is cancelled when its execution has already started, future#get throws CancellationException before - //completion. The execution continues though so we use a latch to try and wait for the task to be completed. - //Here we want to make sure that whenever skip returns false, the task will be completed, otherwise we may be - //missing to schedule the following round, which means no sniffing will ever happen again besides on failure sniffing. + // if a future is cancelled when its execution has already started, future#get throws CancellationException before + // completion. The execution continues though so we use a latch to try and wait for the task to be completed. + // Here we want to make sure that whenever skip returns false, the task will be completed, otherwise we may be + // missing to schedule the following round, which means no sniffing will ever happen again besides on failure sniffing. assertTrue(wrapper.await()); - //the future may or may not be cancelled but the task has for sure started and completed + // the future may or may not be cancelled but the task has for sure started and completed assertTrue(task.toString(), task.hasStarted()); assertFalse(task.isSkipped()); assertTrue(future.isDone()); } - //subsequent cancel calls return false for sure + // subsequent cancel calls return false for sure int cancelCalls = randomIntBetween(1, 10); for (int j = 0; j < cancelCalls; j++) { assertFalse(scheduledTask.skip()); @@ -556,7 +552,7 @@ public List sniff() throws IOException { int run = runs.incrementAndGet(); if (rarely()) { failures.incrementAndGet(); - //check that if communication breaks, sniffer keeps on working + // check that if communication breaks, sniffer keeps on working throw new IOException("communication breakdown"); } if (rarely()) { @@ -596,13 +592,14 @@ public void shutdown() { ScheduledExecutorService scheduledExecutorService = mock(ScheduledExecutorService.class); final ScheduledFuture mockedFuture = mock(ScheduledFuture.class); - when(scheduledExecutorService.schedule(any(Runnable.class), any(Long.class), any(TimeUnit.class))) - .then(new Answer>() { - @Override - public ScheduledFuture answer(InvocationOnMock invocationOnMock) { - return mockedFuture; - } - }); + when(scheduledExecutorService.schedule(any(Runnable.class), any(Long.class), any(TimeUnit.class))).then( + new Answer>() { + @Override + public ScheduledFuture answer(InvocationOnMock invocationOnMock) { + return mockedFuture; + } + } + ); DefaultScheduler scheduler = new DefaultScheduler(scheduledExecutorService); long delay = randomLongBetween(1, Long.MAX_VALUE); Future future = scheduler.schedule(task, delay); diff --git a/client/test/src/main/java/org/elasticsearch/client/RestClientTestCase.java b/client/test/src/main/java/org/elasticsearch/client/RestClientTestCase.java index 2d9bf4574ce31..0528f7b758be3 100644 --- a/client/test/src/main/java/org/elasticsearch/client/RestClientTestCase.java +++ b/client/test/src/main/java/org/elasticsearch/client/RestClientTestCase.java @@ -30,6 +30,7 @@ import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope; import com.carrotsearch.randomizedtesting.annotations.ThreadLeakZombies; import com.carrotsearch.randomizedtesting.annotations.TimeoutSuite; + import org.apache.http.Header; import java.util.ArrayList; @@ -43,13 +44,11 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -@TestMethodProviders({ - JUnit3MethodProvider.class -}) -@SeedDecorators({MixWithSuiteName.class}) // See LUCENE-3995 for rationale. +@TestMethodProviders({ JUnit3MethodProvider.class }) +@SeedDecorators({ MixWithSuiteName.class }) // See LUCENE-3995 for rationale. @ThreadLeakScope(ThreadLeakScope.Scope.SUITE) @ThreadLeakGroup(ThreadLeakGroup.Group.MAIN) -@ThreadLeakAction({ThreadLeakAction.Action.WARN, ThreadLeakAction.Action.INTERRUPT}) +@ThreadLeakAction({ ThreadLeakAction.Action.WARN, ThreadLeakAction.Action.INTERRUPT }) @ThreadLeakZombies(ThreadLeakZombies.Consequence.IGNORE_REMAINING_TESTS) @ThreadLeakLingering(linger = 5000) // 5 sec lingering @TimeoutSuite(millis = 2 * 60 * 60 * 1000) @@ -65,8 +64,12 @@ public abstract class RestClientTestCase extends RandomizedTest { * @param ignoreHeaders header keys to be ignored as they are not part of default nor request headers, yet they * will be part of the actual ones */ - protected static void assertHeaders(final Header[] defaultHeaders, final Header[] requestHeaders, - final Header[] actualHeaders, final Set ignoreHeaders) { + protected static void assertHeaders( + final Header[] defaultHeaders, + final Header[] requestHeaders, + final Header[] actualHeaders, + final Set ignoreHeaders + ) { final Map> expectedHeaders = new HashMap<>(); final Set requestHeaderKeys = new HashSet<>(); for (final Header header : requestHeaders) { diff --git a/client/test/src/main/java/org/elasticsearch/client/RestClientTestUtil.java b/client/test/src/main/java/org/elasticsearch/client/RestClientTestUtil.java index a9982907f0cbf..3d5a5c706189d 100644 --- a/client/test/src/main/java/org/elasticsearch/client/RestClientTestUtil.java +++ b/client/test/src/main/java/org/elasticsearch/client/RestClientTestUtil.java @@ -22,6 +22,7 @@ import com.carrotsearch.randomizedtesting.generators.RandomNumbers; import com.carrotsearch.randomizedtesting.generators.RandomPicks; import com.carrotsearch.randomizedtesting.generators.RandomStrings; + import org.apache.http.Header; import org.apache.http.message.BasicHeader; @@ -32,7 +33,7 @@ final class RestClientTestUtil { - private static final String[] HTTP_METHODS = new String[]{"DELETE", "HEAD", "GET", "OPTIONS", "PATCH", "POST", "PUT", "TRACE"}; + private static final String[] HTTP_METHODS = new String[] { "DELETE", "HEAD", "GET", "OPTIONS", "PATCH", "POST", "PUT", "TRACE" }; private static final List ALL_STATUS_CODES; private static final List OK_STATUS_CODES = Arrays.asList(200, 201); private static final List ALL_ERROR_STATUS_CODES; @@ -96,7 +97,7 @@ static Header[] randomHeaders(Random random, final String baseName) { final Header[] headers = new Header[numHeaders]; for (int i = 0; i < numHeaders; i++) { String headerName = baseName; - //randomly exercise the code path that supports multiple headers with same key + // randomly exercise the code path that supports multiple headers with same key if (random.nextBoolean()) { headerName = headerName + i; } diff --git a/distribution/archives/integ-test-zip/src/test/java/org/elasticsearch/test/rest/CreatedLocationHeaderIT.java b/distribution/archives/integ-test-zip/src/test/java/org/elasticsearch/test/rest/CreatedLocationHeaderIT.java index 338ab3a0ec4b9..718a221559c4f 100644 --- a/distribution/archives/integ-test-zip/src/test/java/org/elasticsearch/test/rest/CreatedLocationHeaderIT.java +++ b/distribution/archives/integ-test-zip/src/test/java/org/elasticsearch/test/rest/CreatedLocationHeaderIT.java @@ -36,9 +36,7 @@ public void testIndexWithoutId() throws IOException { public void testUpsert() throws IOException { Request request = new Request("POST", "test/_update/1"); - request.setJsonEntity("{" - + "\"doc\": {\"test\": \"test\"}," - + "\"doc_as_upsert\": true}"); + request.setJsonEntity("{" + "\"doc\": {\"test\": \"test\"}," + "\"doc_as_upsert\": true}"); locationTestCase(client().performRequest(request)); } diff --git a/distribution/archives/integ-test-zip/src/test/java/org/elasticsearch/test/rest/JsonLogsFormatAndParseIT.java b/distribution/archives/integ-test-zip/src/test/java/org/elasticsearch/test/rest/JsonLogsFormatAndParseIT.java index f5a05283f53b1..e4efa6d909335 100644 --- a/distribution/archives/integ-test-zip/src/test/java/org/elasticsearch/test/rest/JsonLogsFormatAndParseIT.java +++ b/distribution/archives/integ-test-zip/src/test/java/org/elasticsearch/test/rest/JsonLogsFormatAndParseIT.java @@ -29,8 +29,10 @@ protected Matcher nodeNameMatcher() { @Override protected BufferedReader openReader(Path logFile) { - assumeFalse("Skipping test because it is being run against an external cluster.", - logFile.getFileName().toString().equals("--external--")); + assumeFalse( + "Skipping test because it is being run against an external cluster.", + logFile.getFileName().toString().equals("--external--") + ); return AccessController.doPrivileged((PrivilegedAction) () -> { try { return Files.newBufferedReader(logFile, StandardCharsets.UTF_8); diff --git a/distribution/archives/integ-test-zip/src/test/java/org/elasticsearch/test/rest/NodeRestUsageIT.java b/distribution/archives/integ-test-zip/src/test/java/org/elasticsearch/test/rest/NodeRestUsageIT.java index 37bd3efdbfa85..de8bcdd9d6b4f 100644 --- a/distribution/archives/integ-test-zip/src/test/java/org/elasticsearch/test/rest/NodeRestUsageIT.java +++ b/distribution/archives/integ-test-zip/src/test/java/org/elasticsearch/test/rest/NodeRestUsageIT.java @@ -50,8 +50,9 @@ public void testWithRestUsage() throws IOException { beforeCombinedRestUsage.put("nodes_stats_action", 0L); beforeCombinedRestUsage.put("delete_index_action", 0L); for (Map.Entry nodeEntry : beforeNodesMap.entrySet()) { - Map beforeRestActionUsage = (Map) ((Map) nodeEntry.getValue()) - .get("rest_actions"); + Map beforeRestActionUsage = (Map) ((Map) nodeEntry.getValue()).get( + "rest_actions" + ); assertThat(beforeRestActionUsage, notNullValue()); for (Map.Entry restActionEntry : beforeRestActionUsage.entrySet()) { Long currentUsage = beforeCombinedRestUsage.get(restActionEntry.getKey()); @@ -115,11 +116,18 @@ public void testWithRestUsage() throws IOException { } public void testMetricsWithAll() throws IOException { - ResponseException exception = expectThrows(ResponseException.class, - () -> client().performRequest(new Request("GET", "_nodes/usage/_all,rest_actions"))); + ResponseException exception = expectThrows( + ResponseException.class, + () -> client().performRequest(new Request("GET", "_nodes/usage/_all,rest_actions")) + ); assertNotNull(exception); - assertThat(exception.getMessage(), containsString("\"type\":\"illegal_argument_exception\"," - + "\"reason\":\"request [_nodes/usage/_all,rest_actions] contains _all and individual metrics [_all,rest_actions]\"")); + assertThat( + exception.getMessage(), + containsString( + "\"type\":\"illegal_argument_exception\"," + + "\"reason\":\"request [_nodes/usage/_all,rest_actions] contains _all and individual metrics [_all,rest_actions]\"" + ) + ); } @SuppressWarnings("unchecked") @@ -138,22 +146,22 @@ public void testAggregationUsage() throws IOException { Map> beforeCombinedAggsUsage = getTotalUsage(beforeNodesMap); // Do some requests to get some rest usage stats Request create = new Request("PUT", "/test"); - create.setJsonEntity("{\"mappings\": {\"properties\": { \"str\": {\"type\": \"keyword\"}, " + - "\"foo\": {\"type\": \"keyword\"}, \"num\": {\"type\": \"long\"}, \"start\": {\"type\": \"date\"} } }}"); + create.setJsonEntity( + "{\"mappings\": {\"properties\": { \"str\": {\"type\": \"keyword\"}, " + + "\"foo\": {\"type\": \"keyword\"}, \"num\": {\"type\": \"long\"}, \"start\": {\"type\": \"date\"} } }}" + ); client().performRequest(create); Request searchRequest = new Request("GET", "/test/_search"); - SearchSourceBuilder searchSource = new SearchSourceBuilder() - .aggregation(AggregationBuilders.terms("str_terms").field("str.keyword")) - .aggregation(AggregationBuilders.terms("num_terms").field("num")) - .aggregation(AggregationBuilders.avg("num_avg").field("num")); + SearchSourceBuilder searchSource = new SearchSourceBuilder().aggregation( + AggregationBuilders.terms("str_terms").field("str.keyword") + ).aggregation(AggregationBuilders.terms("num_terms").field("num")).aggregation(AggregationBuilders.avg("num_avg").field("num")); searchRequest.setJsonEntity(Strings.toString(searchSource)); searchRequest.setJsonEntity(Strings.toString(searchSource)); client().performRequest(searchRequest); searchRequest = new Request("GET", "/test/_search"); - searchSource = new SearchSourceBuilder() - .aggregation(AggregationBuilders.terms("start").field("start")) + searchSource = new SearchSourceBuilder().aggregation(AggregationBuilders.terms("start").field("start")) .aggregation(AggregationBuilders.avg("num1").field("num")) .aggregation(AggregationBuilders.avg("num2").field("num")) .aggregation(AggregationBuilders.terms("foo").field("foo.keyword")); @@ -178,11 +186,16 @@ public void testAggregationUsage() throws IOException { assertDiff(beforeCombinedAggsUsage, afterCombinedAggsUsage, "avg", "numeric", 3L); } - private void assertDiff(Map> before, Map> after, String agg, String vst, - long diff) { + private void assertDiff( + Map> before, + Map> after, + String agg, + String vst, + long diff + ) { Long valBefore = before.getOrDefault(agg, Collections.emptyMap()).getOrDefault(vst, 0L); Long valAfter = after.getOrDefault(agg, Collections.emptyMap()).getOrDefault(vst, 0L); - assertThat(agg + "." + vst, valAfter - valBefore, equalTo(diff) ); + assertThat(agg + "." + vst, valAfter - valBefore, equalTo(diff)); } private Map> getTotalUsage(Map nodeUsage) { @@ -192,11 +205,14 @@ private Map> getTotalUsage(Map nodeUsa Map beforeAggsUsage = (Map) ((Map) nodeEntry.getValue()).get("aggregations"); assertThat(beforeAggsUsage, notNullValue()); for (Map.Entry aggEntry : beforeAggsUsage.entrySet()) { - @SuppressWarnings("unchecked") Map aggMap = (Map) aggEntry.getValue(); + @SuppressWarnings("unchecked") + Map aggMap = (Map) aggEntry.getValue(); Map combinedAggMap = combined.computeIfAbsent(aggEntry.getKey(), k -> new HashMap<>()); - for (Map.Entry valSourceEntry : aggMap.entrySet()) { - combinedAggMap.put(valSourceEntry.getKey(), - combinedAggMap.getOrDefault(valSourceEntry.getKey(), 0L) + ((Number) valSourceEntry.getValue()).longValue()); + for (Map.Entry valSourceEntry : aggMap.entrySet()) { + combinedAggMap.put( + valSourceEntry.getKey(), + combinedAggMap.getOrDefault(valSourceEntry.getKey(), 0L) + ((Number) valSourceEntry.getValue()).longValue() + ); } } } @@ -204,7 +220,8 @@ private Map> getTotalUsage(Map nodeUsa } private int assertSuccess(Map responseBodyMap) { - @SuppressWarnings("unchecked") Map nodesResultMap = (Map) responseBodyMap.get("_nodes"); + @SuppressWarnings("unchecked") + Map nodesResultMap = (Map) responseBodyMap.get("_nodes"); assertThat(nodesResultMap, notNullValue()); Integer total = (Integer) nodesResultMap.get("total"); Integer successful = (Integer) nodesResultMap.get("successful"); diff --git a/distribution/archives/integ-test-zip/src/test/java/org/elasticsearch/test/rest/RequestsWithoutContentIT.java b/distribution/archives/integ-test-zip/src/test/java/org/elasticsearch/test/rest/RequestsWithoutContentIT.java index 84c9be62f1bf1..ba305333f2fb9 100644 --- a/distribution/archives/integ-test-zip/src/test/java/org/elasticsearch/test/rest/RequestsWithoutContentIT.java +++ b/distribution/archives/integ-test-zip/src/test/java/org/elasticsearch/test/rest/RequestsWithoutContentIT.java @@ -8,8 +8,8 @@ package org.elasticsearch.test.rest; -import org.elasticsearch.client.ResponseException; import org.elasticsearch.client.Request; +import org.elasticsearch.client.ResponseException; import java.io.IOException; @@ -18,56 +18,74 @@ public class RequestsWithoutContentIT extends ESRestTestCase { public void testIndexMissingBody() throws IOException { - ResponseException responseException = expectThrows(ResponseException.class, () -> - client().performRequest(new Request(randomBoolean() ? "POST" : "PUT", "/idx/_doc/123"))); + ResponseException responseException = expectThrows( + ResponseException.class, + () -> client().performRequest(new Request(randomBoolean() ? "POST" : "PUT", "/idx/_doc/123")) + ); assertResponseException(responseException, "request body is required"); } public void testBulkMissingBody() throws IOException { - ResponseException responseException = expectThrows(ResponseException.class, () -> - client().performRequest(new Request(randomBoolean() ? "POST" : "PUT", "/_bulk"))); + ResponseException responseException = expectThrows( + ResponseException.class, + () -> client().performRequest(new Request(randomBoolean() ? "POST" : "PUT", "/_bulk")) + ); assertResponseException(responseException, "request body is required"); } public void testPutSettingsMissingBody() throws IOException { - ResponseException responseException = expectThrows(ResponseException.class, () -> - client().performRequest(new Request("PUT", "/_settings"))); + ResponseException responseException = expectThrows( + ResponseException.class, + () -> client().performRequest(new Request("PUT", "/_settings")) + ); assertResponseException(responseException, "request body is required"); } public void testPutMappingsMissingBody() throws IOException { - ResponseException responseException = expectThrows(ResponseException.class, () -> - client().performRequest(new Request(randomBoolean() ? "POST" : "PUT", "/test_index/_mapping"))); + ResponseException responseException = expectThrows( + ResponseException.class, + () -> client().performRequest(new Request(randomBoolean() ? "POST" : "PUT", "/test_index/_mapping")) + ); assertResponseException(responseException, "request body is required"); } public void testPutIndexTemplateMissingBody() throws IOException { - ResponseException responseException = expectThrows(ResponseException.class, () -> - client().performRequest(new Request(randomBoolean() ? "PUT" : "POST", "/_template/my_template"))); + ResponseException responseException = expectThrows( + ResponseException.class, + () -> client().performRequest(new Request(randomBoolean() ? "PUT" : "POST", "/_template/my_template")) + ); assertResponseException(responseException, "request body is required"); } public void testMultiSearchMissingBody() throws IOException { - ResponseException responseException = expectThrows(ResponseException.class, () -> - client().performRequest(new Request(randomBoolean() ? "POST" : "GET", "/_msearch"))); + ResponseException responseException = expectThrows( + ResponseException.class, + () -> client().performRequest(new Request(randomBoolean() ? "POST" : "GET", "/_msearch")) + ); assertResponseException(responseException, "request body or source parameter is required"); } public void testPutPipelineMissingBody() throws IOException { - ResponseException responseException = expectThrows(ResponseException.class, () -> - client().performRequest(new Request("PUT", "/_ingest/pipeline/my_pipeline"))); + ResponseException responseException = expectThrows( + ResponseException.class, + () -> client().performRequest(new Request("PUT", "/_ingest/pipeline/my_pipeline")) + ); assertResponseException(responseException, "request body or source parameter is required"); } public void testSimulatePipelineMissingBody() throws IOException { - ResponseException responseException = expectThrows(ResponseException.class, () -> - client().performRequest(new Request(randomBoolean() ? "POST" : "GET", "/_ingest/pipeline/my_pipeline/_simulate"))); + ResponseException responseException = expectThrows( + ResponseException.class, + () -> client().performRequest(new Request(randomBoolean() ? "POST" : "GET", "/_ingest/pipeline/my_pipeline/_simulate")) + ); assertResponseException(responseException, "request body or source parameter is required"); } public void testPutScriptMissingBody() throws IOException { - ResponseException responseException = expectThrows(ResponseException.class, () -> - client().performRequest(new Request(randomBoolean() ? "POST" : "PUT", "/_scripts/lang"))); + ResponseException responseException = expectThrows( + ResponseException.class, + () -> client().performRequest(new Request(randomBoolean() ? "POST" : "PUT", "/_scripts/lang")) + ); assertResponseException(responseException, "request body is required"); } diff --git a/distribution/docker/src/test/java/org/elasticsearch/docker/test/DockerYmlTestSuiteIT.java b/distribution/docker/src/test/java/org/elasticsearch/docker/test/DockerYmlTestSuiteIT.java index 51e1b6e774322..2cba6ea4d4bda 100644 --- a/distribution/docker/src/test/java/org/elasticsearch/docker/test/DockerYmlTestSuiteIT.java +++ b/distribution/docker/src/test/java/org/elasticsearch/docker/test/DockerYmlTestSuiteIT.java @@ -8,12 +8,13 @@ package org.elasticsearch.docker.test; import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; + import org.elasticsearch.ElasticsearchException; import org.elasticsearch.client.Request; -import org.elasticsearch.core.PathUtils; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.PathUtils; import org.elasticsearch.test.rest.ESRestTestCase; import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate; import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase; @@ -43,8 +44,7 @@ public static Iterable parameters() throws Exception { @Override protected String getTestRestCluster() { String distribution = getDistribution(); - return new StringBuilder() - .append("localhost:") + return new StringBuilder().append("localhost:") .append(getProperty("test.fixtures.elasticsearch-" + distribution + "-1.tcp.9200")) .append(",") .append("localhost:") @@ -72,8 +72,10 @@ private boolean isOss() { private String getProperty(String key) { String value = System.getProperty(key); if (value == null) { - throw new IllegalStateException("Could not find system properties from test.fixtures. " + - "This test expects to run with the elasticsearch.test.fixtures Gradle plugin"); + throw new IllegalStateException( + "Could not find system properties from test.fixtures. " + + "This test expects to run with the elasticsearch.test.fixtures Gradle plugin" + ); } return value; } @@ -104,7 +106,7 @@ public static void getTrustedCert() { @AfterClass public static void clearTrustedCert() { - trustedCertFile = null; + trustedCertFile = null; } @Override diff --git a/docs/src/test/java/org/elasticsearch/smoketest/DocsClientYamlTestSuiteIT.java b/docs/src/test/java/org/elasticsearch/smoketest/DocsClientYamlTestSuiteIT.java index 061438f715bec..d4ed09b7662ba 100644 --- a/docs/src/test/java/org/elasticsearch/smoketest/DocsClientYamlTestSuiteIT.java +++ b/docs/src/test/java/org/elasticsearch/smoketest/DocsClientYamlTestSuiteIT.java @@ -20,12 +20,6 @@ import org.elasticsearch.client.Request; import org.elasticsearch.client.RestClient; import org.elasticsearch.common.util.CollectionUtils; -import org.elasticsearch.xcontent.ConstructingObjectParser; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.xcontent.XContentLocation; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentParser.Token; import org.elasticsearch.test.rest.ESRestTestCase; import org.elasticsearch.test.rest.yaml.ClientYamlDocsTestClient; import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate; @@ -35,6 +29,12 @@ import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase; import org.elasticsearch.test.rest.yaml.restspec.ClientYamlSuiteRestSpec; import org.elasticsearch.test.rest.yaml.section.ExecutableSection; +import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentLocation; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentParser.Token; import org.junit.After; import org.junit.Before; @@ -60,9 +60,12 @@ public DocsClientYamlTestSuiteIT(@Name("yaml") ClientYamlTestCandidate testCandi @ParametersFactory public static Iterable parameters() throws Exception { - NamedXContentRegistry executableSectionRegistry = new NamedXContentRegistry(CollectionUtils.appendToCopy( - ExecutableSection.DEFAULT_EXECUTABLE_CONTEXTS, new NamedXContentRegistry.Entry(ExecutableSection.class, - new ParseField("compare_analyzers"), CompareAnalyzers::parse))); + NamedXContentRegistry executableSectionRegistry = new NamedXContentRegistry( + CollectionUtils.appendToCopy( + ExecutableSection.DEFAULT_EXECUTABLE_CONTEXTS, + new NamedXContentRegistry.Entry(ExecutableSection.class, new ParseField("compare_analyzers"), CompareAnalyzers::parse) + ) + ); return ESClientYamlSuiteTestCase.createParameters(executableSectionRegistry); } @@ -72,8 +75,11 @@ protected void afterIfFailed(List errors) { String name = getTestName().split("=")[1]; name = name.substring(0, name.length() - 1); name = name.replaceAll("/([^/]+)$", ".asciidoc:$1"); - logger.error("This failing test was generated by documentation starting at {}. It may include many snippets. " - + "See docs/README.asciidoc for an explanation of test generation.", name); + logger.error( + "This failing test was generated by documentation starting at {}. It may include many snippets. " + + "See docs/README.asciidoc for an explanation of test generation.", + name + ); } @Override @@ -181,7 +187,7 @@ protected boolean preserveTemplatesUponCompletion() { protected boolean isSLMTest() { String testName = getTestName(); return testName != null && (testName.contains("/slm/") || testName.contains("\\slm\\") || (testName.contains("\\slm/")) || - // TODO: Remove after backport of https://github.com/elastic/elasticsearch/pull/48705 which moves SLM docs to correct folder + // TODO: Remove after backport of https://github.com/elastic/elasticsearch/pull/48705 which moves SLM docs to correct folder testName.contains("/ilm/") || testName.contains("\\ilm\\") || testName.contains("\\ilm/")); } @@ -224,18 +230,22 @@ protected boolean isXpackInfoTest() { * small number of tokens. */ private static class CompareAnalyzers implements ExecutableSection { - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("test_analyzer", false, (a, location) -> { + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "test_analyzer", + false, + (a, location) -> { String index = (String) a[0]; String first = (String) a[1]; String second = (String) a[2]; return new CompareAnalyzers(location, index, first, second); - }); + } + ); static { PARSER.declareString(constructorArg(), new ParseField("index")); PARSER.declareString(constructorArg(), new ParseField("first")); PARSER.declareString(constructorArg(), new ParseField("second")); } + private static CompareAnalyzers parse(XContentParser parser) throws IOException { XContentLocation location = parser.getTokenLocation(); CompareAnalyzers section = PARSER.parse(parser, location); @@ -281,29 +291,50 @@ public void execute(ClientYamlTestExecutionContext executionContext) throws IOEx b.append(' '); b.append(randomRealisticUnicodeOfCodepointLengthBetween(1, maxLength)); } - testText.add(b.toString() - // Don't look up stashed values - .replace("$", "\\$")); + testText.add( + b.toString() + // Don't look up stashed values + .replace("$", "\\$") + ); } Map body = new HashMap<>(2); body.put("analyzer", first); body.put("text", testText); - ClientYamlTestResponse response = executionContext.callApi("indices.analyze", singletonMap("index", index), - singletonList(body), emptyMap()); + ClientYamlTestResponse response = executionContext.callApi( + "indices.analyze", + singletonMap("index", index), + singletonList(body), + emptyMap() + ); Iterator firstTokens = ((List) response.evaluate("tokens")).iterator(); body.put("analyzer", second); - response = executionContext.callApi("indices.analyze", singletonMap("index", index), - singletonList(body), emptyMap()); + response = executionContext.callApi("indices.analyze", singletonMap("index", index), singletonList(body), emptyMap()); Iterator secondTokens = ((List) response.evaluate("tokens")).iterator(); Object previousFirst = null; Object previousSecond = null; while (firstTokens.hasNext()) { if (false == secondTokens.hasNext()) { - fail(second + " has fewer tokens than " + first + ". " - + first + " has [" + firstTokens.next() + "] but " + second + " is out of tokens. " - + first + "'s last token was [" + previousFirst + "] and " - + second + "'s last token was' [" + previousSecond + "]"); + fail( + second + + " has fewer tokens than " + + first + + ". " + + first + + " has [" + + firstTokens.next() + + "] but " + + second + + " is out of tokens. " + + first + + "'s last token was [" + + previousFirst + + "] and " + + second + + "'s last token was' [" + + previousSecond + + "]" + ); } Map firstToken = (Map) firstTokens.next(); Map secondToken = (Map) secondTokens.next(); @@ -311,8 +342,20 @@ public void execute(ClientYamlTestExecutionContext executionContext) throws IOEx String secondText = (String) secondToken.get("token"); // Check the text and produce an error message with the utf8 sequence if they don't match. if (false == secondText.equals(firstText)) { - fail("text differs: " + first + " was [" + firstText + "] but " + second + " was [" + secondText - + "]. In utf8 those are\n" + new BytesRef(firstText) + " and\n" + new BytesRef(secondText)); + fail( + "text differs: " + + first + + " was [" + + firstText + + "] but " + + second + + " was [" + + secondText + + "]. In utf8 those are\n" + + new BytesRef(firstText) + + " and\n" + + new BytesRef(secondText) + ); } // Now check the whole map just in case the text matches but something else differs assertEquals(firstToken, secondToken); @@ -320,10 +363,26 @@ public void execute(ClientYamlTestExecutionContext executionContext) throws IOEx previousSecond = secondToken; } if (secondTokens.hasNext()) { - fail(second + " has more tokens than " + first + ". " - + second + " has [" + secondTokens.next() + "] but " + first + " is out of tokens. " - + first + "'s last token was [" + previousFirst + "] and " - + second + "'s last token was' [" + previousSecond + "]"); + fail( + second + + " has more tokens than " + + first + + ". " + + second + + " has [" + + secondTokens.next() + + "] but " + + first + + " is out of tokens. " + + first + + "'s last token was [" + + previousFirst + + "] and " + + second + + "'s last token was' [" + + previousSecond + + "]" + ); } } } diff --git a/libs/cli/src/main/java/org/elasticsearch/cli/Command.java b/libs/cli/src/main/java/org/elasticsearch/cli/Command.java index 3aa999ae11a97..07b5c17c04cf4 100644 --- a/libs/cli/src/main/java/org/elasticsearch/cli/Command.java +++ b/libs/cli/src/main/java/org/elasticsearch/cli/Command.java @@ -34,8 +34,8 @@ public abstract class Command implements Closeable { private final OptionSpec helpOption = parser.acceptsAll(Arrays.asList("h", "help"), "Show help").forHelp(); private final OptionSpec silentOption = parser.acceptsAll(Arrays.asList("s", "silent"), "Show minimal output"); - private final OptionSpec verboseOption = - parser.acceptsAll(Arrays.asList("v", "verbose"), "Show verbose output").availableUnless(silentOption); + private final OptionSpec verboseOption = parser.acceptsAll(Arrays.asList("v", "verbose"), "Show verbose output") + .availableUnless(silentOption); /** * Construct the command with the specified command description and runnable to execute before main is invoked. @@ -58,9 +58,7 @@ public final int main(String[] args, Terminal terminal) throws Exception { try { this.close(); } catch (final IOException e) { - try ( - StringWriter sw = new StringWriter(); - PrintWriter pw = new PrintWriter(sw)) { + try (StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw)) { e.printStackTrace(pw); terminal.errorPrintln(sw.toString()); } catch (final IOException impossible) { diff --git a/libs/cli/src/main/java/org/elasticsearch/cli/SuppressForbidden.java b/libs/cli/src/main/java/org/elasticsearch/cli/SuppressForbidden.java index fd3b7a0c14bf4..54a1243305aeb 100644 --- a/libs/cli/src/main/java/org/elasticsearch/cli/SuppressForbidden.java +++ b/libs/cli/src/main/java/org/elasticsearch/cli/SuppressForbidden.java @@ -20,4 +20,3 @@ public @interface SuppressForbidden { String reason(); } - diff --git a/libs/cli/src/main/java/org/elasticsearch/cli/Terminal.java b/libs/cli/src/main/java/org/elasticsearch/cli/Terminal.java index 71c34c45fb41e..a8a7e42c6fa71 100644 --- a/libs/cli/src/main/java/org/elasticsearch/cli/Terminal.java +++ b/libs/cli/src/main/java/org/elasticsearch/cli/Terminal.java @@ -184,7 +184,7 @@ public static char[] readLineToCharArray(Reader reader, int maxLength) { len++; } - if (len > 0 && len < buf.length && buf[len-1] == '\r') { + if (len > 0 && len < buf.length && buf[len - 1] == '\r') { len--; } diff --git a/libs/core/src/main/java/org/elasticsearch/core/AbstractRefCounted.java b/libs/core/src/main/java/org/elasticsearch/core/AbstractRefCounted.java index d55c99f4a3b2b..70ac5f03ed597 100644 --- a/libs/core/src/main/java/org/elasticsearch/core/AbstractRefCounted.java +++ b/libs/core/src/main/java/org/elasticsearch/core/AbstractRefCounted.java @@ -67,8 +67,7 @@ public final boolean hasReferences() { * Called whenever the ref count is incremented or decremented. Can be overridden to record access to the instance for debugging * purposes. */ - protected void touch() { - } + protected void touch() {} protected void alreadyClosed() { final int currentRefCount = refCount.get(); diff --git a/libs/core/src/main/java/org/elasticsearch/core/Booleans.java b/libs/core/src/main/java/org/elasticsearch/core/Booleans.java index a1d8abfdd2bd9..a7201761f44a0 100644 --- a/libs/core/src/main/java/org/elasticsearch/core/Booleans.java +++ b/libs/core/src/main/java/org/elasticsearch/core/Booleans.java @@ -107,6 +107,7 @@ public static Boolean parseBooleanLenient(String value, Boolean defaultValue) { } return parseBooleanLenient(value, false); } + /** * Returns {@code false} if text is in "false", "0", "off", "no"; else, {@code true}. * @@ -163,8 +164,11 @@ public static boolean parseBooleanLenient(char[] text, int offset, int length, b return (text[offset] == 'o' && text[offset + 1] == 'f' && text[offset + 2] == 'f') == false; } if (length == 5) { - return (text[offset] == 'f' && text[offset + 1] == 'a' && text[offset + 2] == 'l' && text[offset + 3] == 's' && - text[offset + 4] == 'e') == false; + return (text[offset] == 'f' + && text[offset + 1] == 'a' + && text[offset + 2] == 'l' + && text[offset + 3] == 's' + && text[offset + 4] == 'e') == false; } return true; } @@ -190,15 +194,18 @@ public static boolean isBooleanLenient(char[] text, int offset, int length) { return (text[offset] == 'n' && text[offset + 1] == 'o') || (text[offset] == 'o' && text[offset + 1] == 'n'); } if (length == 3) { - return (text[offset] == 'o' && text[offset + 1] == 'f' && text[offset + 2] == 'f') || - (text[offset] == 'y' && text[offset + 1] == 'e' && text[offset + 2] == 's'); + return (text[offset] == 'o' && text[offset + 1] == 'f' && text[offset + 2] == 'f') + || (text[offset] == 'y' && text[offset + 1] == 'e' && text[offset + 2] == 's'); } if (length == 4) { return (text[offset] == 't' && text[offset + 1] == 'r' && text[offset + 2] == 'u' && text[offset + 3] == 'e'); } if (length == 5) { - return (text[offset] == 'f' && text[offset + 1] == 'a' && text[offset + 2] == 'l' && text[offset + 3] == 's' && - text[offset + 4] == 'e'); + return (text[offset] == 'f' + && text[offset + 1] == 'a' + && text[offset + 2] == 'l' + && text[offset + 3] == 's' + && text[offset + 4] == 'e'); } return false; } diff --git a/libs/core/src/main/java/org/elasticsearch/core/CompletableContext.java b/libs/core/src/main/java/org/elasticsearch/core/CompletableContext.java index dcae12f93781f..105d78b6ee11a 100644 --- a/libs/core/src/main/java/org/elasticsearch/core/CompletableContext.java +++ b/libs/core/src/main/java/org/elasticsearch/core/CompletableContext.java @@ -27,7 +27,7 @@ public void addListener(BiConsumer listener) { if (t == null) { listener.accept(v, null); } else { - assert (t instanceof Error) == false: "Cannot be error"; + assert (t instanceof Error) == false : "Cannot be error"; listener.accept(v, (Exception) t); } }; diff --git a/libs/core/src/main/java/org/elasticsearch/core/FastMath.java b/libs/core/src/main/java/org/elasticsearch/core/FastMath.java index 3f38d13adb08b..3db96c7de1a92 100644 --- a/libs/core/src/main/java/org/elasticsearch/core/FastMath.java +++ b/libs/core/src/main/java/org/elasticsearch/core/FastMath.java @@ -45,22 +45,22 @@ */ final class FastMath { - private FastMath() { } + private FastMath() {} - //-------------------------------------------------------------------------- + // -------------------------------------------------------------------------- // RE-USABLE CONSTANTS - //-------------------------------------------------------------------------- + // -------------------------------------------------------------------------- - private static final double ONE_DIV_F2 = 1/2.0; - private static final double ONE_DIV_F3 = 1/6.0; - private static final double ONE_DIV_F4 = 1/24.0; + private static final double ONE_DIV_F2 = 1 / 2.0; + private static final double ONE_DIV_F3 = 1 / 6.0; + private static final double ONE_DIV_F4 = 1 / 24.0; private static final double TWO_POW_N28 = Double.longBitsToDouble(0x3E30000000000000L); private static final double TWO_POW_66 = Double.longBitsToDouble(0x4410000000000000L); private static final double LOG_DOUBLE_MAX_VALUE = StrictMath.log(Double.MAX_VALUE); - //-------------------------------------------------------------------------- + // -------------------------------------------------------------------------- // CONSTANTS AND TABLES FOR ATAN - //-------------------------------------------------------------------------- + // -------------------------------------------------------------------------- // We use the formula atan(-x) = -atan(x) // ---> we only have to compute atan(x) on [0,+infinity[. @@ -70,9 +70,9 @@ private FastMath() { } // Supposed to be >= tan(67.7deg), as fdlibm code is supposed to work with values > 2.4375. private static final double ATAN_MAX_VALUE_FOR_TABS = StrictMath.tan(Math.toRadians(74.0)); - private static final int ATAN_TABS_SIZE = 1<<12 + 1; - private static final double ATAN_DELTA = ATAN_MAX_VALUE_FOR_TABS/(ATAN_TABS_SIZE - 1); - private static final double ATAN_INDEXER = 1/ATAN_DELTA; + private static final int ATAN_TABS_SIZE = 1 << 12 + 1; + private static final double ATAN_DELTA = ATAN_MAX_VALUE_FOR_TABS / (ATAN_TABS_SIZE - 1); + private static final double ATAN_INDEXER = 1 / ATAN_DELTA; private static final double[] atanTab = new double[ATAN_TABS_SIZE]; private static final double[] atanDer1DivF1Tab = new double[ATAN_TABS_SIZE]; private static final double[] atanDer2DivF2Tab = new double[ATAN_TABS_SIZE]; @@ -81,32 +81,32 @@ private FastMath() { } private static final double ATAN_HI3 = Double.longBitsToDouble(0x3ff921fb54442d18L); // 1.57079632679489655800e+00 atan(inf)hi private static final double ATAN_LO3 = Double.longBitsToDouble(0x3c91a62633145c07L); // 6.12323399573676603587e-17 atan(inf)lo - private static final double ATAN_AT0 = Double.longBitsToDouble(0x3fd555555555550dL); // 3.33333333333329318027e-01 + private static final double ATAN_AT0 = Double.longBitsToDouble(0x3fd555555555550dL); // 3.33333333333329318027e-01 private static final double ATAN_AT1 = Double.longBitsToDouble(0xbfc999999998ebc4L); // -1.99999999998764832476e-01 - private static final double ATAN_AT2 = Double.longBitsToDouble(0x3fc24924920083ffL); // 1.42857142725034663711e-01 + private static final double ATAN_AT2 = Double.longBitsToDouble(0x3fc24924920083ffL); // 1.42857142725034663711e-01 private static final double ATAN_AT3 = Double.longBitsToDouble(0xbfbc71c6fe231671L); // -1.11111104054623557880e-01 - private static final double ATAN_AT4 = Double.longBitsToDouble(0x3fb745cdc54c206eL); // 9.09088713343650656196e-02 + private static final double ATAN_AT4 = Double.longBitsToDouble(0x3fb745cdc54c206eL); // 9.09088713343650656196e-02 private static final double ATAN_AT5 = Double.longBitsToDouble(0xbfb3b0f2af749a6dL); // -7.69187620504482999495e-02 - private static final double ATAN_AT6 = Double.longBitsToDouble(0x3fb10d66a0d03d51L); // 6.66107313738753120669e-02 + private static final double ATAN_AT6 = Double.longBitsToDouble(0x3fb10d66a0d03d51L); // 6.66107313738753120669e-02 private static final double ATAN_AT7 = Double.longBitsToDouble(0xbfadde2d52defd9aL); // -5.83357013379057348645e-02 - private static final double ATAN_AT8 = Double.longBitsToDouble(0x3fa97b4b24760debL); // 4.97687799461593236017e-02 + private static final double ATAN_AT8 = Double.longBitsToDouble(0x3fa97b4b24760debL); // 4.97687799461593236017e-02 private static final double ATAN_AT9 = Double.longBitsToDouble(0xbfa2b4442c6a6c2fL); // -3.65315727442169155270e-02 private static final double ATAN_AT10 = Double.longBitsToDouble(0x3f90ad3ae322da11L); // 1.62858201153657823623e-02 static { // atan - for (int i=0;i ATAN_MAX_VALUE_FOR_TABS, or value is NaN // This part is derived from fdlibm. if (value < TWO_POW_66) { - double x = -1/value; - double x2 = x*x; - double x4 = x2*x2; - double s1 = x2*(ATAN_AT0+x4*(ATAN_AT2+x4*(ATAN_AT4+x4*(ATAN_AT6+x4*(ATAN_AT8+x4*ATAN_AT10))))); - double s2 = x4*(ATAN_AT1+x4*(ATAN_AT3+x4*(ATAN_AT5+x4*(ATAN_AT7+x4*ATAN_AT9)))); - double result = ATAN_HI3-((x*(s1+s2)-ATAN_LO3)-x); + double x = -1 / value; + double x2 = x * x; + double x4 = x2 * x2; + double s1 = x2 * (ATAN_AT0 + x4 * (ATAN_AT2 + x4 * (ATAN_AT4 + x4 * (ATAN_AT6 + x4 * (ATAN_AT8 + x4 * ATAN_AT10))))); + double s2 = x4 * (ATAN_AT1 + x4 * (ATAN_AT3 + x4 * (ATAN_AT5 + x4 * (ATAN_AT7 + x4 * ATAN_AT9)))); + double result = ATAN_HI3 - ((x * (s1 + s2) - ATAN_LO3) - x); return negateResult ? -result : result; } else { // value >= 2^66, or value is NaN if (Double.isNaN(value)) { return Double.NaN; } else { - return negateResult ? -Math.PI/2 : Math.PI/2; + return negateResult ? -Math.PI / 2 : Math.PI / 2; } } } diff --git a/libs/core/src/main/java/org/elasticsearch/core/Glob.java b/libs/core/src/main/java/org/elasticsearch/core/Glob.java index 7d10e962f0f9d..6f24bf88c53fe 100644 --- a/libs/core/src/main/java/org/elasticsearch/core/Glob.java +++ b/libs/core/src/main/java/org/elasticsearch/core/Glob.java @@ -51,9 +51,9 @@ public static boolean globMatch(String pattern, String str) { } return false; } - return (str.length() >= firstIndex && - pattern.substring(0, firstIndex).equals(str.substring(0, firstIndex)) && - globMatch(pattern.substring(firstIndex), str.substring(firstIndex))); + return (str.length() >= firstIndex + && pattern.substring(0, firstIndex).equals(str.substring(0, firstIndex)) + && globMatch(pattern.substring(firstIndex), str.substring(firstIndex))); } } diff --git a/libs/core/src/main/java/org/elasticsearch/core/Nullable.java b/libs/core/src/main/java/org/elasticsearch/core/Nullable.java index 9a9c91958b244..3194f1a2e668c 100644 --- a/libs/core/src/main/java/org/elasticsearch/core/Nullable.java +++ b/libs/core/src/main/java/org/elasticsearch/core/Nullable.java @@ -8,14 +8,15 @@ package org.elasticsearch.core; -import javax.annotation.CheckForNull; -import javax.annotation.meta.TypeQualifierNickname; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import javax.annotation.CheckForNull; +import javax.annotation.meta.TypeQualifierNickname; + /** * The presence of this annotation on a method parameter indicates that * {@code null} is an acceptable value for that parameter. It should not be @@ -27,6 +28,6 @@ @TypeQualifierNickname @CheckForNull @Retention(RetentionPolicy.RUNTIME) -@Target({ElementType.PARAMETER, ElementType.FIELD, ElementType.METHOD}) +@Target({ ElementType.PARAMETER, ElementType.FIELD, ElementType.METHOD }) public @interface Nullable { } diff --git a/libs/core/src/main/java/org/elasticsearch/core/PathUtils.java b/libs/core/src/main/java/org/elasticsearch/core/PathUtils.java index cc49a9d9291a3..71d89c5bf063b 100644 --- a/libs/core/src/main/java/org/elasticsearch/core/PathUtils.java +++ b/libs/core/src/main/java/org/elasticsearch/core/PathUtils.java @@ -72,7 +72,7 @@ public static Path get(Path[] roots, String path) { for (Path root : roots) { Path normalizedRoot = root.normalize(); Path normalizedPath = normalizedRoot.resolve(path).normalize(); - if(normalizedPath.startsWith(normalizedRoot)) { + if (normalizedPath.startsWith(normalizedRoot)) { return normalizedPath; } } diff --git a/libs/core/src/main/java/org/elasticsearch/core/RestApiVersion.java b/libs/core/src/main/java/org/elasticsearch/core/RestApiVersion.java index 670622d076819..bd2179886eace 100644 --- a/libs/core/src/main/java/org/elasticsearch/core/RestApiVersion.java +++ b/libs/core/src/main/java/org/elasticsearch/core/RestApiVersion.java @@ -32,7 +32,7 @@ public RestApiVersion previous() { return fromMajorVersion(major - 1); } - public boolean matches(Function restApiVersionFunctions){ + public boolean matches(Function restApiVersionFunctions) { return restApiVersionFunctions.apply(this); } diff --git a/libs/core/src/main/java/org/elasticsearch/core/SuppressForbidden.java b/libs/core/src/main/java/org/elasticsearch/core/SuppressForbidden.java index 949e366197eda..9a7344d08ac2d 100644 --- a/libs/core/src/main/java/org/elasticsearch/core/SuppressForbidden.java +++ b/libs/core/src/main/java/org/elasticsearch/core/SuppressForbidden.java @@ -7,11 +7,11 @@ */ package org.elasticsearch.core; - import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; + /** * Annotation to suppress forbidden-apis errors inside a whole class, a method, or a field. */ diff --git a/libs/core/src/main/java/org/elasticsearch/core/TimeValue.java b/libs/core/src/main/java/org/elasticsearch/core/TimeValue.java index 27d253edd7cf7..b74fe01e43821 100644 --- a/libs/core/src/main/java/org/elasticsearch/core/TimeValue.java +++ b/libs/core/src/main/java/org/elasticsearch/core/TimeValue.java @@ -371,8 +371,9 @@ public static TimeValue parseTimeValue(String sValue, TimeValue defaultValue, St return TimeValue.ZERO; } else { // Missing units: - throw new IllegalArgumentException("failed to parse setting [" + settingName + "] with value [" + sValue + - "] as a time value: unit is missing or unrecognized"); + throw new IllegalArgumentException( + "failed to parse setting [" + settingName + "] with value [" + sValue + "] as a time value: unit is missing or unrecognized" + ); } } @@ -382,13 +383,19 @@ private static long parse(final String initialInput, final String normalized, fi final long value = Long.parseLong(s); if (value < -1) { // -1 is magic, but reject any other negative values - throw new IllegalArgumentException("failed to parse setting [" + settingName + "] with value [" + initialInput + - "] as a time value: negative durations are not supported"); + throw new IllegalArgumentException( + "failed to parse setting [" + + settingName + + "] with value [" + + initialInput + + "] as a time value: negative durations are not supported" + ); } return value; } catch (final NumberFormatException e) { try { - @SuppressWarnings("unused") final double ignored = Double.parseDouble(s); + @SuppressWarnings("unused") + final double ignored = Double.parseDouble(s); throw new IllegalArgumentException("failed to parse [" + initialInput + "], fractional time values are not supported", e); } catch (final NumberFormatException ignored) { throw new IllegalArgumentException("failed to parse [" + initialInput + "]", e); diff --git a/libs/core/src/main/java/org/elasticsearch/core/internal/io/IOUtils.java b/libs/core/src/main/java/org/elasticsearch/core/internal/io/IOUtils.java index 8f2494f201337..4f5034a5c1ae2 100644 --- a/libs/core/src/main/java/org/elasticsearch/core/internal/io/IOUtils.java +++ b/libs/core/src/main/java/org/elasticsearch/core/internal/io/IOUtils.java @@ -151,7 +151,7 @@ public static void closeWhileHandlingException(final Closeable... objects) { */ public static void closeWhileHandlingException(final Iterable objects) { for (final Closeable object : objects) { - closeWhileHandlingException(object); + closeWhileHandlingException(object); } } @@ -162,8 +162,7 @@ public static void closeWhileHandlingException(final Closeable closeable) { // noinspection EmptyCatchBlock try { close(closeable); - } catch (final IOException | RuntimeException e) { - } + } catch (final IOException | RuntimeException e) {} } /** @@ -199,21 +198,17 @@ public static void deleteFilesIgnoringExceptions(final Collection unremoved = rm(new LinkedHashMap<>(), locations); + final LinkedHashMap unremoved = rm(new LinkedHashMap<>(), locations); if (unremoved.isEmpty() == false) { final StringBuilder b = new StringBuilder("could not remove the following files (in the order of attempts):\n"); - for (final Map.Entry kv : unremoved.entrySet()) { - b.append(" ") - .append(kv.getKey().toAbsolutePath()) - .append(": ") - .append(kv.getValue()) - .append("\n"); + for (final Map.Entry kv : unremoved.entrySet()) { + b.append(" ").append(kv.getKey().toAbsolutePath()).append(": ").append(kv.getValue()).append("\n"); } throw new IOException(b.toString()); } } - private static LinkedHashMap rm(final LinkedHashMap unremoved, final Path... locations) { + private static LinkedHashMap rm(final LinkedHashMap unremoved, final Path... locations) { if (locations != null) { for (final Path location : locations) { // TODO: remove this leniency @@ -308,9 +303,10 @@ public static void fsync(final Path fileToSync, final boolean isDir, final boole file.force(metaData); } catch (final IOException e) { if (isDir) { - assert (LINUX || MAC_OS_X) == false : - "on Linux and MacOSX fsyncing a directory should not throw IOException, "+ - "we just don't want to rely on that in production (undocumented); got: " + e; + assert (LINUX || MAC_OS_X) == false + : "on Linux and MacOSX fsyncing a directory should not throw IOException, " + + "we just don't want to rely on that in production (undocumented); got: " + + e; // ignore exception if it is a directory return; } diff --git a/libs/core/src/main/java/org/elasticsearch/core/internal/net/NetUtils.java b/libs/core/src/main/java/org/elasticsearch/core/internal/net/NetUtils.java index 769d4ea19cc6b..404842e67e361 100644 --- a/libs/core/src/main/java/org/elasticsearch/core/internal/net/NetUtils.java +++ b/libs/core/src/main/java/org/elasticsearch/core/internal/net/NetUtils.java @@ -68,7 +68,8 @@ public static void tryEnsureReasonableKeepAliveConfig(NetworkChannel socketChann if (keepalive.booleanValue()) { for (SocketOption option : Arrays.asList( NetUtils.getTcpKeepIdleSocketOptionOrNull(), - NetUtils.getTcpKeepIntervalSocketOptionOrNull())) { + NetUtils.getTcpKeepIntervalSocketOptionOrNull() + )) { setMinValueForSocketOption(socketChannel, option, 300); } } diff --git a/libs/core/src/main/java/org/elasticsearch/jdk/JarHell.java b/libs/core/src/main/java/org/elasticsearch/jdk/JarHell.java index 4e9e0a8c54a1f..6d4d4f59f9d45 100644 --- a/libs/core/src/main/java/org/elasticsearch/jdk/JarHell.java +++ b/libs/core/src/main/java/org/elasticsearch/jdk/JarHell.java @@ -8,8 +8,8 @@ package org.elasticsearch.jdk; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.core.PathUtils; +import org.elasticsearch.core.SuppressForbidden; import java.io.IOException; import java.net.MalformedURLException; @@ -70,7 +70,7 @@ public static void checkJarHell(Consumer output) throws IOException, URI output.accept("java.class.path: " + System.getProperty("java.class.path")); output.accept("sun.boot.class.path: " + System.getProperty("sun.boot.class.path")); if (loader instanceof URLClassLoader) { - output.accept("classloader urls: " + Arrays.toString(((URLClassLoader)loader).getURLs())); + output.accept("classloader urls: " + Arrays.toString(((URLClassLoader) loader).getURLs())); } checkJarHell(parseClassPath(), output); } @@ -80,7 +80,7 @@ public static void checkJarHell(Consumer output) throws IOException, URI * @return array of URLs * @throws IllegalStateException if the classpath contains empty elements */ - public static Set parseClassPath() { + public static Set parseClassPath() { return parseClassPath(System.getProperty("java.class.path")); } @@ -109,8 +109,12 @@ static Set parseClassPath(String classPath) { * Instead we just throw an exception, and keep it clean. */ if (element.isEmpty()) { - throw new IllegalStateException("Classpath should not contain empty elements! (outdated shell script from a previous" + - " version?) classpath='" + classPath + "'"); + throw new IllegalStateException( + "Classpath should not contain empty elements! (outdated shell script from a previous" + + " version?) classpath='" + + classPath + + "'" + ); } // we should be able to just Paths.get() each element, but unfortunately this is not the // whole story on how classpath parsing works: if you want to know, start at sun.misc.Launcher, @@ -127,15 +131,16 @@ static Set parseClassPath(String classPath) { } // now just parse as ordinary file try { - if (element .equals("/")) { + if (element.equals("/")) { // Eclipse adds this to the classpath when running unit tests... continue; } URL url = PathUtils.get(element).toUri().toURL(); // junit4.childvm.count if (urlElements.add(url) == false && element.endsWith(".jar")) { - throw new IllegalStateException("jar hell!" + System.lineSeparator() + - "duplicate jar [" + element + "] on classpath: " + classPath); + throw new IllegalStateException( + "jar hell!" + System.lineSeparator() + "duplicate jar [" + element + "] on classpath: " + classPath + ); } } catch (MalformedURLException e) { // should not happen, as we use the filesystem API @@ -158,7 +163,7 @@ public static void checkJarHell(Set urls, Consumer output) throws U // a "list" at all. So just exclude any elements underneath the java home String javaHome = System.getProperty("java.home"); output.accept("java.home: " + javaHome); - final Map clazzes = new HashMap<>(32768); + final Map clazzes = new HashMap<>(32768); Set seenJars = new HashSet<>(); for (final URL url : urls) { final Path path = PathUtils.get(url.toURI()); @@ -169,8 +174,7 @@ public static void checkJarHell(Set urls, Consumer output) throws U } if (path.toString().endsWith(".jar")) { if (seenJars.add(path) == false) { - throw new IllegalStateException("jar hell!" + System.lineSeparator() + - "duplicate jar on classpath: " + path); + throw new IllegalStateException("jar hell!" + System.lineSeparator() + "duplicate jar on classpath: " + path); } output.accept("examining jar: " + path); try (JarFile file = new JarFile(path.toString())) { @@ -228,12 +232,12 @@ private static void checkManifest(Manifest manifest, Path jar) { public static void checkVersionFormat(String targetVersion) { if (JavaVersion.isValid(targetVersion) == false) { throw new IllegalStateException( - String.format( - Locale.ROOT, - "version string must be a sequence of nonnegative decimal integers separated by \".\"'s and may have " + - "leading zeros but was %s", - targetVersion - ) + String.format( + Locale.ROOT, + "version string must be a sequence of nonnegative decimal integers separated by \".\"'s and may have " + + "leading zeros but was %s", + targetVersion + ) ); } } @@ -246,13 +250,13 @@ public static void checkJavaVersion(String resource, String targetVersion) { JavaVersion version = JavaVersion.parse(targetVersion); if (JavaVersion.current().compareTo(version) < 0) { throw new IllegalStateException( - String.format( - Locale.ROOT, - "%s requires Java %s:, your system: %s", - resource, - targetVersion, - JavaVersion.current().toString() - ) + String.format( + Locale.ROOT, + "%s requires Java %s:, your system: %s", + resource, + targetVersion, + JavaVersion.current().toString() + ) ); } } @@ -271,14 +275,29 @@ private static void checkClass(Map clazzes, String clazz, Path jar // throw a better exception in this ridiculous case. // unfortunately the zip file format allows this buggy possibility // UweSays: It can, but should be considered as bug :-) - throw new IllegalStateException("jar hell!" + System.lineSeparator() + - "class: " + clazz + System.lineSeparator() + - "exists multiple times in jar: " + jarpath + " !!!!!!!!!"); + throw new IllegalStateException( + "jar hell!" + + System.lineSeparator() + + "class: " + + clazz + + System.lineSeparator() + + "exists multiple times in jar: " + + jarpath + + " !!!!!!!!!" + ); } else { - throw new IllegalStateException("jar hell!" + System.lineSeparator() + - "class: " + clazz + System.lineSeparator() + - "jar1: " + previous + System.lineSeparator() + - "jar2: " + jarpath); + throw new IllegalStateException( + "jar hell!" + + System.lineSeparator() + + "class: " + + clazz + + System.lineSeparator() + + "jar1: " + + previous + + System.lineSeparator() + + "jar2: " + + jarpath + ); } } } diff --git a/libs/core/src/main/java/org/elasticsearch/jdk/JavaVersion.java b/libs/core/src/main/java/org/elasticsearch/jdk/JavaVersion.java index 2901bcb513d63..6c0de1c320dbc 100644 --- a/libs/core/src/main/java/org/elasticsearch/jdk/JavaVersion.java +++ b/libs/core/src/main/java/org/elasticsearch/jdk/JavaVersion.java @@ -80,10 +80,8 @@ public int compareTo(JavaVersion o) { for (int i = 0; i < len; i++) { int d = (i < version.size() ? version.get(i) : 0); int s = (i < o.version.size() ? o.version.get(i) : 0); - if (s < d) - return 1; - if (s > d) - return -1; + if (s < d) return 1; + if (s > d) return -1; } if (prePart != null && o.prePart == null) { return -1; @@ -97,11 +95,9 @@ public int compareTo(JavaVersion o) { private int comparePrePart(String leftPrePart, String rightPrePart) { if (leftPrePart.matches("\\d+")) { - return rightPrePart.matches("\\d+") ? - (new BigInteger(leftPrePart)).compareTo(new BigInteger(rightPrePart)) : -1; + return rightPrePart.matches("\\d+") ? (new BigInteger(leftPrePart)).compareTo(new BigInteger(rightPrePart)) : -1; } else { - return rightPrePart.matches("\\d+") ? - 1 : leftPrePart.compareTo(rightPrePart); + return rightPrePart.matches("\\d+") ? 1 : leftPrePart.compareTo(rightPrePart); } } diff --git a/libs/core/src/test/java/org/elasticsearch/common/unit/TimeValueTests.java b/libs/core/src/test/java/org/elasticsearch/common/unit/TimeValueTests.java index 87f4d0b8eb2cc..e3273c34bb32e 100644 --- a/libs/core/src/test/java/org/elasticsearch/common/unit/TimeValueTests.java +++ b/libs/core/src/test/java/org/elasticsearch/common/unit/TimeValueTests.java @@ -66,57 +66,35 @@ public void testMinusOne() { public void testParseTimeValue() { // Space is allowed before unit: - assertEquals(new TimeValue(10, TimeUnit.MILLISECONDS), - TimeValue.parseTimeValue("10 ms", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.MILLISECONDS), - TimeValue.parseTimeValue("10ms", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.MILLISECONDS), - TimeValue.parseTimeValue("10 MS", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.MILLISECONDS), - TimeValue.parseTimeValue("10MS", null, "test")); - - assertEquals(new TimeValue(10, TimeUnit.SECONDS), - TimeValue.parseTimeValue("10 s", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.SECONDS), - TimeValue.parseTimeValue("10s", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.SECONDS), - TimeValue.parseTimeValue("10 S", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.SECONDS), - TimeValue.parseTimeValue("10S", null, "test")); - - assertEquals(new TimeValue(10, TimeUnit.MINUTES), - TimeValue.parseTimeValue("10 m", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.MINUTES), - TimeValue.parseTimeValue("10m", null, "test")); - - assertEquals(new TimeValue(10, TimeUnit.HOURS), - TimeValue.parseTimeValue("10 h", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.HOURS), - TimeValue.parseTimeValue("10h", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.HOURS), - TimeValue.parseTimeValue("10 H", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.HOURS), - TimeValue.parseTimeValue("10H", null, "test")); - - assertEquals(new TimeValue(10, TimeUnit.DAYS), - TimeValue.parseTimeValue("10 d", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.DAYS), - TimeValue.parseTimeValue("10d", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.DAYS), - TimeValue.parseTimeValue("10 D", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.DAYS), - TimeValue.parseTimeValue("10D", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.MILLISECONDS), TimeValue.parseTimeValue("10 ms", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.MILLISECONDS), TimeValue.parseTimeValue("10ms", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.MILLISECONDS), TimeValue.parseTimeValue("10 MS", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.MILLISECONDS), TimeValue.parseTimeValue("10MS", null, "test")); + + assertEquals(new TimeValue(10, TimeUnit.SECONDS), TimeValue.parseTimeValue("10 s", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.SECONDS), TimeValue.parseTimeValue("10s", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.SECONDS), TimeValue.parseTimeValue("10 S", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.SECONDS), TimeValue.parseTimeValue("10S", null, "test")); + + assertEquals(new TimeValue(10, TimeUnit.MINUTES), TimeValue.parseTimeValue("10 m", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.MINUTES), TimeValue.parseTimeValue("10m", null, "test")); + + assertEquals(new TimeValue(10, TimeUnit.HOURS), TimeValue.parseTimeValue("10 h", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.HOURS), TimeValue.parseTimeValue("10h", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.HOURS), TimeValue.parseTimeValue("10 H", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.HOURS), TimeValue.parseTimeValue("10H", null, "test")); + + assertEquals(new TimeValue(10, TimeUnit.DAYS), TimeValue.parseTimeValue("10 d", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.DAYS), TimeValue.parseTimeValue("10d", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.DAYS), TimeValue.parseTimeValue("10 D", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.DAYS), TimeValue.parseTimeValue("10D", null, "test")); // Time values of months should throw an exception as months are not // supported. Note that this is the only unit that is not case sensitive // as `m` is the only character that is overloaded in terms of which // time unit is expected between the upper and lower case versions - expectThrows(IllegalArgumentException.class, () -> { - TimeValue.parseTimeValue("10 M", null, "test"); - }); - expectThrows(IllegalArgumentException.class, () -> { - TimeValue.parseTimeValue("10M", null, "test"); - }); + expectThrows(IllegalArgumentException.class, () -> { TimeValue.parseTimeValue("10 M", null, "test"); }); + expectThrows(IllegalArgumentException.class, () -> { TimeValue.parseTimeValue("10M", null, "test"); }); final int length = randomIntBetween(0, 8); final String zeros = new String(new char[length]).replace('\0', '0'); @@ -135,8 +113,7 @@ public void testRoundTrip() { public void testNonFractionalTimeValues() { final String s = randomAlphaOfLength(10) + randomTimeUnit(); - final IllegalArgumentException e = - expectThrows(IllegalArgumentException.class, () -> TimeValue.parseTimeValue(s, null, "test")); + final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> TimeValue.parseTimeValue(s, null, "test")); assertThat(e, hasToString(containsString("failed to parse [" + s + "]"))); assertThat(e, not(hasToString(containsString(FRACTIONAL_TIME_VALUES_ARE_NOT_SUPPORTED)))); assertThat(e.getCause(), instanceOf(NumberFormatException.class)); @@ -148,8 +125,7 @@ public void testFractionalTimeValues() { value = randomDouble(); } while (value == 0); final String s = Double.toString(randomIntBetween(0, 128) + value) + randomTimeUnit(); - final IllegalArgumentException e = - expectThrows(IllegalArgumentException.class, () -> TimeValue.parseTimeValue(s, null, "test")); + final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> TimeValue.parseTimeValue(s, null, "test")); assertThat(e, hasToString(containsString("failed to parse [" + s + "]"))); assertThat(e, hasToString(containsString(FRACTIONAL_TIME_VALUES_ARE_NOT_SUPPORTED))); assertThat(e.getCause(), instanceOf(NumberFormatException.class)); @@ -216,7 +192,7 @@ public void testCompareValue() { public void testCompareUnits() { long number = randomNonNegativeLong(); - TimeUnit randomUnit = randomValueOtherThan(TimeUnit.DAYS, ()->randomFrom(TimeUnit.values())); + TimeUnit randomUnit = randomValueOtherThan(TimeUnit.DAYS, () -> randomFrom(TimeUnit.values())); TimeValue firstValue = new TimeValue(number, randomUnit); TimeValue secondValue = new TimeValue(number, TimeUnit.DAYS); assertTrue(firstValue.compareTo(secondValue) < 0); @@ -233,11 +209,20 @@ public void testRejectsNegativeValuesDuringParsing() { final String settingName = "test-value"; final long negativeValue = randomLongBetween(Long.MIN_VALUE, -2); final String negativeTimeValueString = Long.toString(negativeValue) + randomTimeUnit(); - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, - () -> TimeValue.parseTimeValue(negativeTimeValueString, settingName)); - assertThat(ex.getMessage(), - equalTo("failed to parse setting [" + settingName + "] with value [" + negativeTimeValueString + - "] as a time value: negative durations are not supported")); + IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> TimeValue.parseTimeValue(negativeTimeValueString, settingName) + ); + assertThat( + ex.getMessage(), + equalTo( + "failed to parse setting [" + + settingName + + "] with value [" + + negativeTimeValueString + + "] as a time value: negative durations are not supported" + ) + ); } public void testRejectsNegativeValuesAtCreation() { @@ -247,7 +232,14 @@ public void testRejectsNegativeValuesAtCreation() { } private TimeUnit randomTimeUnitObject() { - return randomFrom(TimeUnit.NANOSECONDS, TimeUnit.MICROSECONDS, TimeUnit.MILLISECONDS, TimeUnit.SECONDS, - TimeUnit.MINUTES, TimeUnit.HOURS, TimeUnit.DAYS); + return randomFrom( + TimeUnit.NANOSECONDS, + TimeUnit.MICROSECONDS, + TimeUnit.MILLISECONDS, + TimeUnit.SECONDS, + TimeUnit.MINUTES, + TimeUnit.HOURS, + TimeUnit.DAYS + ); } } diff --git a/libs/core/src/test/java/org/elasticsearch/common/util/ESSloppyMathTests.java b/libs/core/src/test/java/org/elasticsearch/common/util/ESSloppyMathTests.java index 8920c2ed354cd..f886cd3efcf93 100644 --- a/libs/core/src/test/java/org/elasticsearch/common/util/ESSloppyMathTests.java +++ b/libs/core/src/test/java/org/elasticsearch/common/util/ESSloppyMathTests.java @@ -22,8 +22,8 @@ public class ESSloppyMathTests extends ESTestCase { public void testAtan() { assertTrue(Double.isNaN(atan(Double.NaN))); - assertEquals(-Math.PI/2, atan(Double.NEGATIVE_INFINITY), ATAN_DELTA); - assertEquals(Math.PI/2, atan(Double.POSITIVE_INFINITY), ATAN_DELTA); + assertEquals(-Math.PI / 2, atan(Double.NEGATIVE_INFINITY), ATAN_DELTA); + assertEquals(Math.PI / 2, atan(Double.POSITIVE_INFINITY), ATAN_DELTA); for (int i = 0; i < 10000; i++) { assertEquals(StrictMath.atan(i), atan(i), ATAN_DELTA); assertEquals(StrictMath.atan(-i), atan(-i), ATAN_DELTA); diff --git a/libs/core/src/test/java/org/elasticsearch/common/util/concurrent/RefCountedTests.java b/libs/core/src/test/java/org/elasticsearch/common/util/concurrent/RefCountedTests.java index 56d10f39f6c61..a373161c71d5b 100644 --- a/libs/core/src/test/java/org/elasticsearch/common/util/concurrent/RefCountedTests.java +++ b/libs/core/src/test/java/org/elasticsearch/common/util/concurrent/RefCountedTests.java @@ -58,7 +58,8 @@ public void testRefCount() { assertFalse(counted.tryIncRef()); assertThat( expectThrows(IllegalStateException.class, counted::incRef).getMessage(), - equalTo(AbstractRefCounted.ALREADY_CLOSED_MESSAGE)); + equalTo(AbstractRefCounted.ALREADY_CLOSED_MESSAGE) + ); assertThat(expectThrows(IllegalStateException.class, counted::ensureOpen).getMessage(), equalTo("closed")); } @@ -92,8 +93,10 @@ public void testMultiThreaded() throws InterruptedException { } counted.decRef(); assertThat(expectThrows(IllegalStateException.class, counted::ensureOpen).getMessage(), equalTo("closed")); - assertThat(expectThrows(IllegalStateException.class, counted::incRef).getMessage(), - equalTo(AbstractRefCounted.ALREADY_CLOSED_MESSAGE)); + assertThat( + expectThrows(IllegalStateException.class, counted::incRef).getMessage(), + equalTo(AbstractRefCounted.ALREADY_CLOSED_MESSAGE) + ); assertThat(counted.refCount(), is(0)); assertFalse(counted.hasReferences()); assertThat(exceptions, Matchers.emptyIterable()); diff --git a/libs/core/src/test/java/org/elasticsearch/core/internal/io/IOUtilsTests.java b/libs/core/src/test/java/org/elasticsearch/core/internal/io/IOUtilsTests.java index ad82901a9eaaa..f3bdaf5f43037 100644 --- a/libs/core/src/test/java/org/elasticsearch/core/internal/io/IOUtilsTests.java +++ b/libs/core/src/test/java/org/elasticsearch/core/internal/io/IOUtilsTests.java @@ -84,8 +84,8 @@ public void testCloseIterableWithIOExceptions() throws IOException { runTestCloseWithIOExceptions((Function>) Arrays::asList, IOUtils::close); } - private void runTestCloseWithIOExceptions( - final Function function, final CheckedConsumer close) throws IOException { + private void runTestCloseWithIOExceptions(final Function function, final CheckedConsumer close) + throws IOException { final int numberOfCloseables = randomIntBetween(1, 8); final Closeable[] closeables = new Closeable[numberOfCloseables]; final List indexesThatThrow = new ArrayList<>(numberOfCloseables); @@ -124,7 +124,9 @@ public void testDeleteFilesIgnoringExceptionsIterable() throws IOException { } private void runDeleteFilesIgnoringExceptionsTest( - final Function function, CheckedConsumer deleteFilesIgnoringExceptions) throws IOException { + final Function function, + CheckedConsumer deleteFilesIgnoringExceptions + ) throws IOException { final int numberOfFiles = randomIntBetween(0, 7); final Path[] files = new Path[numberOfFiles]; for (int i = 0; i < numberOfFiles; i++) { @@ -157,8 +159,9 @@ public void runTestRm(final boolean exception) throws IOException { for (int i = 0; i < numberOfLocations; i++) { if (exception && randomBoolean()) { final Path location = createTempDir(); - final FileSystem fs = - new AccessDeniedWhileDeletingFileSystem(location.getFileSystem()).getFileSystem(URI.create("file:///")); + final FileSystem fs = new AccessDeniedWhileDeletingFileSystem(location.getFileSystem()).getFileSystem( + URI.create("file:///") + ); final Path wrapped = new FilterPath(location, fs); locations[i] = wrapped.resolve(randomAlphaOfLength(8)); Files.createDirectory(locations[i]); @@ -232,10 +235,8 @@ private static final class AccessDeniedWhileOpeningDirectoryFileSystem extends F } @Override - public FileChannel newFileChannel( - final Path path, - final Set options, - final FileAttribute... attrs) throws IOException { + public FileChannel newFileChannel(final Path path, final Set options, final FileAttribute... attrs) + throws IOException { if (Files.isDirectory(path)) { throw new AccessDeniedException(path.toString()); } diff --git a/libs/core/src/test/java/org/elasticsearch/jdk/JarHellTests.java b/libs/core/src/test/java/org/elasticsearch/jdk/JarHellTests.java index eec2d80e1e1ca..db82ded6fb6cc 100644 --- a/libs/core/src/test/java/org/elasticsearch/jdk/JarHellTests.java +++ b/libs/core/src/test/java/org/elasticsearch/jdk/JarHellTests.java @@ -52,8 +52,7 @@ URL makeFile(Path dir, String name) throws IOException { public void testDifferentJars() throws Exception { Path dir = createTempDir(); - Set jars = asSet(makeJar(dir, "foo.jar", null, "DuplicateClass.class"), - makeJar(dir, "bar.jar", null, "DuplicateClass.class")); + Set jars = asSet(makeJar(dir, "foo.jar", null, "DuplicateClass.class"), makeJar(dir, "bar.jar", null, "DuplicateClass.class")); try { JarHell.checkJarHell(jars, logger::debug); fail("did not get expected exception"); @@ -68,10 +67,7 @@ public void testDifferentJars() throws Exception { public void testModuleInfo() throws Exception { Path dir = createTempDir(); JarHell.checkJarHell( - asSet( - makeJar(dir, "foo.jar", null, "module-info.class"), - makeJar(dir, "bar.jar", null, "module-info.class") - ), + asSet(makeJar(dir, "foo.jar", null, "module-info.class"), makeJar(dir, "bar.jar", null, "module-info.class")), logger::debug ); } @@ -79,10 +75,7 @@ public void testModuleInfo() throws Exception { public void testModuleInfoPackage() throws Exception { Path dir = createTempDir(); JarHell.checkJarHell( - asSet( - makeJar(dir, "foo.jar", null, "foo/bar/module-info.class"), - makeJar(dir, "bar.jar", null, "foo/bar/module-info.class") - ), + asSet(makeJar(dir, "foo.jar", null, "foo/bar/module-info.class"), makeJar(dir, "bar.jar", null, "foo/bar/module-info.class")), logger::debug ); } @@ -90,8 +83,7 @@ public void testModuleInfoPackage() throws Exception { public void testDirsOnClasspath() throws Exception { Path dir1 = createTempDir(); Path dir2 = createTempDir(); - Set dirs = asSet(makeFile(dir1, "DuplicateClass.class"), - makeFile(dir2, "DuplicateClass.class")); + Set dirs = asSet(makeFile(dir1, "DuplicateClass.class"), makeFile(dir2, "DuplicateClass.class")); try { JarHell.checkJarHell(dirs, logger::debug); fail("did not get expected exception"); @@ -106,8 +98,7 @@ public void testDirsOnClasspath() throws Exception { public void testDirAndJar() throws Exception { Path dir1 = createTempDir(); Path dir2 = createTempDir(); - Set dirs = asSet(makeJar(dir1, "foo.jar", null, "DuplicateClass.class"), - makeFile(dir2, "DuplicateClass.class")); + Set dirs = asSet(makeJar(dir1, "foo.jar", null, "DuplicateClass.class"), makeFile(dir2, "DuplicateClass.class")); try { JarHell.checkJarHell(dirs, logger::debug); fail("did not get expected exception"); @@ -148,7 +139,6 @@ public void testRequiredJDKVersionTooOld() throws Exception { } JavaVersion targetVersion = JavaVersion.parse(Strings.collectionToDelimitedString(target, ".")); - Manifest manifest = new Manifest(); Attributes attributes = manifest.getMainAttributes(); attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0.0"); @@ -174,8 +164,13 @@ public void testBadJDKVersionInJar() throws Exception { JarHell.checkJarHell(jars, logger::debug); fail("did not get expected exception"); } catch (IllegalStateException e) { - assertTrue(e.getMessage().equals("version string must be a sequence of nonnegative decimal integers separated " + - "by \".\"'s and may have leading zeros but was bogus")); + assertTrue( + e.getMessage() + .equals( + "version string must be a sequence of nonnegative decimal integers separated " + + "by \".\"'s and may have leading zeros but was bogus" + ) + ); } } @@ -190,7 +185,7 @@ public void testRequiredJDKVersionIsOK() throws Exception { } public void testValidVersions() { - String[] versions = new String[]{"1.7", "1.7.0", "0.1.7", "1.7.0.80"}; + String[] versions = new String[] { "1.7", "1.7.0", "0.1.7", "1.7.0.80" }; for (String version : versions) { try { JarHell.checkVersionFormat(version); @@ -201,13 +196,12 @@ public void testValidVersions() { } public void testInvalidVersions() { - String[] versions = new String[]{"", "1.7.0_80", "1.7."}; + String[] versions = new String[] { "", "1.7.0_80", "1.7." }; for (String version : versions) { try { JarHell.checkVersionFormat(version); fail("\"" + version + "\"" + " should be rejected as an invalid version format"); - } catch (IllegalStateException e) { - } + } catch (IllegalStateException e) {} } } diff --git a/libs/dissect/src/main/java/org/elasticsearch/dissect/DissectKey.java b/libs/dissect/src/main/java/org/elasticsearch/dissect/DissectKey.java index ec7c9659eb7af..f6cf21e478bc1 100644 --- a/libs/dissect/src/main/java/org/elasticsearch/dissect/DissectKey.java +++ b/libs/dissect/src/main/java/org/elasticsearch/dissect/DissectKey.java @@ -100,7 +100,7 @@ public final class DissectKey { * @param key The key to copy (except for the modifier) * @param modifier the modifier to use for this copy */ - DissectKey(DissectKey key, DissectKey.Modifier modifier){ + DissectKey(DissectKey key, DissectKey.Modifier modifier) { this.modifier = modifier; this.skipRightPadding = key.skipRightPadding; this.skip = key.skip; @@ -128,19 +128,29 @@ String getName() { return name; } - //generated + // generated @Override public String toString() { - return "DissectKey{" + - "modifier=" + modifier + - ", skip=" + skip + - ", appendPosition=" + appendPosition + - ", name='" + name + '\'' + - '}'; + return "DissectKey{" + + "modifier=" + + modifier + + ", skip=" + + skip + + ", appendPosition=" + + appendPosition + + ", name='" + + name + + '\'' + + '}'; } public enum Modifier { - NONE(""), APPEND_WITH_ORDER("/"), APPEND("+"), FIELD_NAME("*"), FIELD_VALUE("&"), NAMED_SKIP("?"); + NONE(""), + APPEND_WITH_ORDER("/"), + APPEND("+"), + FIELD_NAME("*"), + FIELD_VALUE("&"), + NAMED_SKIP("?"); private static final Pattern MODIFIER_PATTERN = Pattern.compile("[/+*&?]"); @@ -155,10 +165,13 @@ public String toString() { this.modifier = modifier; } - //package private for testing + // package private for testing static Modifier fromString(String modifier) { - return EnumSet.allOf(Modifier.class).stream().filter(km -> km.modifier.equals(modifier)) - .findFirst().orElseThrow(() -> new IllegalArgumentException("Found invalid modifier.")); //throw should never happen + return EnumSet.allOf(Modifier.class) + .stream() + .filter(km -> km.modifier.equals(modifier)) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException("Found invalid modifier.")); // throw should never happen } private static Modifier findModifier(String key) { diff --git a/libs/dissect/src/main/java/org/elasticsearch/dissect/DissectMatch.java b/libs/dissect/src/main/java/org/elasticsearch/dissect/DissectMatch.java index 57ba80a8f27a7..83a4fc23d9be8 100644 --- a/libs/dissect/src/main/java/org/elasticsearch/dissect/DissectMatch.java +++ b/libs/dissect/src/main/java/org/elasticsearch/dissect/DissectMatch.java @@ -35,7 +35,7 @@ final class DissectMatch { DissectMatch(String appendSeparator, int maxMatches, int maxResults, int appendCount, int referenceCount) { if (maxMatches <= 0 || maxResults <= 0) { - throw new IllegalArgumentException("Expected results are zero, can not construct DissectMatch");//should never happen + throw new IllegalArgumentException("Expected results are zero, can not construct DissectMatch");// should never happen } this.maxMatches = maxMatches; this.maxResults = maxResults; @@ -67,8 +67,8 @@ void add(DissectKey key, String value) { appendResults.computeIfAbsent(key.getName(), k -> new AppendResult(appendSeparator)).addValue(value, implicitAppendOrder++); break; case APPEND_WITH_ORDER: - appendResults.computeIfAbsent(key.getName(), - k -> new AppendResult(appendSeparator)).addValue(value, key.getAppendPosition()); + appendResults.computeIfAbsent(key.getName(), k -> new AppendResult(appendSeparator)) + .addValue(value, key.getAppendPosition()); break; case FIELD_NAME: referenceResults.computeIfAbsent(key.getName(), k -> new ReferenceResult()).setKey(value); diff --git a/libs/dissect/src/main/java/org/elasticsearch/dissect/DissectParser.java b/libs/dissect/src/main/java/org/elasticsearch/dissect/DissectParser.java index b4cb39127d834..6c2a307373543 100644 --- a/libs/dissect/src/main/java/org/elasticsearch/dissect/DissectParser.java +++ b/libs/dissect/src/main/java/org/elasticsearch/dissect/DissectParser.java @@ -87,10 +87,12 @@ public final class DissectParser { private static final Pattern KEY_DELIMITER_FIELD_PATTERN = Pattern.compile("%\\{([^}]*?)}(.+?(?=%\\{)|.*$)", Pattern.DOTALL); private static final EnumSet ASSOCIATE_MODIFIERS = EnumSet.of( DissectKey.Modifier.FIELD_NAME, - DissectKey.Modifier.FIELD_VALUE); + DissectKey.Modifier.FIELD_VALUE + ); private static final EnumSet APPEND_MODIFIERS = EnumSet.of( DissectKey.Modifier.APPEND, - DissectKey.Modifier.APPEND_WITH_ORDER); + DissectKey.Modifier.APPEND_WITH_ORDER + ); private static final Function KEY_NAME = val -> val.getKey().getName(); private final List matchPairs; private final String pattern; @@ -116,16 +118,19 @@ public DissectParser(String pattern, String appendSeparator) { dissectPairs.add(new DissectPair(key, delimiter)); } this.maxMatches = dissectPairs.size(); - this.maxResults = Long.valueOf(dissectPairs.stream() - .filter(dissectPair -> dissectPair.getKey().skip() == false).map(KEY_NAME).distinct().count()).intValue(); + this.maxResults = Long.valueOf( + dissectPairs.stream().filter(dissectPair -> dissectPair.getKey().skip() == false).map(KEY_NAME).distinct().count() + ).intValue(); if (this.maxMatches == 0 || maxResults == 0) { throw new DissectException.PatternParse(pattern, "Unable to find any keys or delimiters."); } - //append validation - look through all of the keys to see if there are any keys that need to participate in an append operation + // append validation - look through all of the keys to see if there are any keys that need to participate in an append operation // but don't have the '+' defined Set appendKeyNames = dissectPairs.stream() .filter(dissectPair -> APPEND_MODIFIERS.contains(dissectPair.getKey().getModifier())) - .map(KEY_NAME).distinct().collect(Collectors.toSet()); + .map(KEY_NAME) + .distinct() + .collect(Collectors.toSet()); if (appendKeyNames.size() > 0) { List modifiedMatchPairs = new ArrayList<>(dissectPairs.size()); for (DissectPair p : dissectPairs) { @@ -139,15 +144,18 @@ public DissectParser(String pattern, String appendSeparator) { } appendCount = appendKeyNames.size(); - //reference validation - ensure that '*' and '&' come in pairs + // reference validation - ensure that '*' and '&' come in pairs Map> referenceGroupings = dissectPairs.stream() .filter(dissectPair -> ASSOCIATE_MODIFIERS.contains(dissectPair.getKey().getModifier())) .collect(Collectors.groupingBy(KEY_NAME)); for (Map.Entry> entry : referenceGroupings.entrySet()) { if (entry.getValue().size() != 2) { - throw new DissectException.PatternParse(pattern, "Found invalid key/reference associations: '" - + entry.getValue().stream().map(KEY_NAME).collect(Collectors.joining(",")) + - "' Please ensure each '*' is matched with a matching '&"); + throw new DissectException.PatternParse( + pattern, + "Found invalid key/reference associations: '" + + entry.getValue().stream().map(KEY_NAME).collect(Collectors.joining(",")) + + "' Please ensure each '*' is matched with a matching '&" + ); } } @@ -155,7 +163,6 @@ public DissectParser(String pattern, String appendSeparator) { this.matchPairs = Collections.unmodifiableList(dissectPairs); } - /** *

    Entry point to dissect a string into it's parts.

    * @@ -187,38 +194,39 @@ public Map parse(String inputString) { */ DissectMatch dissectMatch = new DissectMatch(appendSeparator, maxMatches, maxResults, appendCount, referenceCount); Iterator it = matchPairs.iterator(); - //ensure leading delimiter matches - if (inputString != null && inputString.length() > leadingDelimiter.length() + // ensure leading delimiter matches + if (inputString != null + && inputString.length() > leadingDelimiter.length() && leadingDelimiter.equals(inputString.substring(0, leadingDelimiter.length()))) { byte[] input = inputString.getBytes(StandardCharsets.UTF_8); - //grab the first key/delimiter pair + // grab the first key/delimiter pair DissectPair dissectPair = it.next(); DissectKey key = dissectPair.getKey(); byte[] delimiter = dissectPair.getDelimiter().getBytes(StandardCharsets.UTF_8); - //start dissection after the first delimiter + // start dissection after the first delimiter int i = leadingDelimiter.length(); int valueStart = i; int lookAheadMatches; - //start walking the input string byte by byte, look ahead for matches where needed - //if a match is found jump forward to the end of the match + // start walking the input string byte by byte, look ahead for matches where needed + // if a match is found jump forward to the end of the match while (i < input.length) { lookAheadMatches = 0; - //potential match between delimiter and input string + // potential match between delimiter and input string if (delimiter.length > 0 && input[i] == delimiter[0]) { - //look ahead to see if the entire delimiter matches the input string + // look ahead to see if the entire delimiter matches the input string for (int j = 0; j < delimiter.length; j++) { if (i + j < input.length && input[i + j] == delimiter[j]) { lookAheadMatches++; } } - //found a full delimiter match + // found a full delimiter match if (lookAheadMatches == delimiter.length) { - //record the key/value tuple + // record the key/value tuple byte[] value = Arrays.copyOfRange(input, valueStart, i); dissectMatch.add(key, new String(value, StandardCharsets.UTF_8)); - //jump to the end of the match + // jump to the end of the match i += lookAheadMatches; - //look for consecutive delimiters (e.g. a,,,,d,e) + // look for consecutive delimiters (e.g. a,,,,d,e) while (i < input.length) { lookAheadMatches = 0; for (int j = 0; j < delimiter.length; j++) { @@ -226,32 +234,32 @@ public Map parse(String inputString) { lookAheadMatches++; } } - //found consecutive delimiters + // found consecutive delimiters if (lookAheadMatches == delimiter.length) { - //jump to the end of the match + // jump to the end of the match i += lookAheadMatches; if (key.skipRightPadding() == false) { - //progress the keys/delimiter if possible + // progress the keys/delimiter if possible if (it.hasNext() == false) { - break; //the while loop + break; // the while loop } dissectPair = it.next(); key = dissectPair.getKey(); - //add the key with an empty value for the empty delimiter + // add the key with an empty value for the empty delimiter dissectMatch.add(key, ""); } } else { - break; //the while loop + break; // the while loop } } - //progress the keys/delimiter if possible + // progress the keys/delimiter if possible if (it.hasNext() == false) { - break; //the for loop + break; // the for loop } dissectPair = it.next(); key = dissectPair.getKey(); delimiter = dissectPair.getDelimiter().getBytes(StandardCharsets.UTF_8); - //i is always one byte after the last found delimiter, aka the start of the next value + // i is always one byte after the last found delimiter, aka the start of the next value valueStart = i; } else { i++; @@ -260,9 +268,9 @@ public Map parse(String inputString) { i++; } } - //the last key, grab the rest of the input (unless consecutive delimiters already grabbed the last key) - //and there is no trailing delimiter - if (dissectMatch.fullyMatched() == false && delimiter.length == 0 ) { + // the last key, grab the rest of the input (unless consecutive delimiters already grabbed the last key) + // and there is no trailing delimiter + if (dissectMatch.fullyMatched() == false && delimiter.length == 0) { byte[] value = Arrays.copyOfRange(input, valueStart, input.length); String valueString = new String(value, StandardCharsets.UTF_8); dissectMatch.add(key, valueString); @@ -311,6 +319,3 @@ private String getDelimiter() { } } - - - diff --git a/libs/dissect/src/test/java/org/elasticsearch/dissect/DissectKeyTests.java b/libs/dissect/src/test/java/org/elasticsearch/dissect/DissectKeyTests.java index 258e0e8951a9c..631f525fdfe30 100644 --- a/libs/dissect/src/test/java/org/elasticsearch/dissect/DissectKeyTests.java +++ b/libs/dissect/src/test/java/org/elasticsearch/dissect/DissectKeyTests.java @@ -111,7 +111,8 @@ public void testRightPaddingModifiers() { public void testMultipleLeftModifiers() { String keyName = randomAlphaOfLengthBetween(1, 10); - List validModifiers = EnumSet.allOf(DissectKey.Modifier.class).stream() + List validModifiers = EnumSet.allOf(DissectKey.Modifier.class) + .stream() .filter(m -> m.equals(DissectKey.Modifier.NONE) == false) .map(DissectKey.Modifier::toString) .collect(Collectors.toList()); @@ -130,9 +131,10 @@ public void testSkipKey() { assertThat(dissectKey.getAppendPosition(), equalTo(0)); assertThat(dissectKey.getName(), equalTo(keyName)); } + public void testNamedSkipKey() { String keyName = "myname"; - DissectKey dissectKey = new DissectKey("?" +keyName); + DissectKey dissectKey = new DissectKey("?" + keyName); assertThat(dissectKey.getModifier(), equalTo(DissectKey.Modifier.NAMED_SKIP)); assertThat(dissectKey.skip(), is(true)); assertThat(dissectKey.skipRightPadding(), is(false)); @@ -142,16 +144,17 @@ public void testNamedSkipKey() { public void testSkipKeyWithPadding() { String keyName = ""; - DissectKey dissectKey = new DissectKey(keyName + "->"); + DissectKey dissectKey = new DissectKey(keyName + "->"); assertThat(dissectKey.getModifier(), equalTo(DissectKey.Modifier.NONE)); assertThat(dissectKey.skip(), is(true)); assertThat(dissectKey.skipRightPadding(), is(true)); assertThat(dissectKey.getAppendPosition(), equalTo(0)); assertThat(dissectKey.getName(), equalTo(keyName)); } + public void testNamedEmptySkipKeyWithPadding() { String keyName = ""; - DissectKey dissectKey = new DissectKey("?" +keyName + "->"); + DissectKey dissectKey = new DissectKey("?" + keyName + "->"); assertThat(dissectKey.getModifier(), equalTo(DissectKey.Modifier.NAMED_SKIP)); assertThat(dissectKey.skip(), is(true)); assertThat(dissectKey.skipRightPadding(), is(true)); @@ -160,7 +163,7 @@ public void testNamedEmptySkipKeyWithPadding() { } public void testInvalidModifiers() { - //should never happen due to regex + // should never happen due to regex IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> DissectKey.Modifier.fromString("x")); assertThat(e.getMessage(), CoreMatchers.containsString("invalid modifier")); } diff --git a/libs/dissect/src/test/java/org/elasticsearch/dissect/DissectMatchTests.java b/libs/dissect/src/test/java/org/elasticsearch/dissect/DissectMatchTests.java index 18a3b65905db8..133d8ebee73ec 100644 --- a/libs/dissect/src/test/java/org/elasticsearch/dissect/DissectMatchTests.java +++ b/libs/dissect/src/test/java/org/elasticsearch/dissect/DissectMatchTests.java @@ -27,8 +27,8 @@ public void testIllegalArgs() { public void testValidAndFullyMatched() { int expectedMatches = randomIntBetween(1, 26); DissectMatch dissectMatch = new DissectMatch("", expectedMatches, expectedMatches, 0, 0); - IntStream.range(97, 97 + expectedMatches) //allow for a-z values - .forEach(i -> dissectMatch.add(new DissectKey(new String(new byte[]{(byte) i}, StandardCharsets.UTF_8)), "")); + IntStream.range(97, 97 + expectedMatches) // allow for a-z values + .forEach(i -> dissectMatch.add(new DissectKey(new String(new byte[] { (byte) i }, StandardCharsets.UTF_8)), "")); assertThat(dissectMatch.fullyMatched(), equalTo(true)); assertThat(dissectMatch.isValid(dissectMatch.getResults()), equalTo(true)); } @@ -36,21 +36,21 @@ public void testValidAndFullyMatched() { public void testNotValidAndFullyMatched() { int expectedMatches = randomIntBetween(1, 26); DissectMatch dissectMatch = new DissectMatch("", expectedMatches, expectedMatches, 0, 0); - IntStream.range(97, 97 + expectedMatches - 1) //allow for a-z values - .forEach(i -> dissectMatch.add(new DissectKey(new String(new byte[]{(byte) i}, StandardCharsets.UTF_8)), "")); + IntStream.range(97, 97 + expectedMatches - 1) // allow for a-z values + .forEach(i -> dissectMatch.add(new DissectKey(new String(new byte[] { (byte) i }, StandardCharsets.UTF_8)), "")); assertThat(dissectMatch.fullyMatched(), equalTo(false)); assertThat(dissectMatch.isValid(dissectMatch.getResults()), equalTo(false)); } - public void testGetResultsIdempotent(){ + public void testGetResultsIdempotent() { int expectedMatches = randomIntBetween(1, 26); DissectMatch dissectMatch = new DissectMatch("", expectedMatches, expectedMatches, 0, 0); - IntStream.range(97, 97 + expectedMatches) //allow for a-z values - .forEach(i -> dissectMatch.add(new DissectKey(new String(new byte[]{(byte) i}, StandardCharsets.UTF_8)), "")); + IntStream.range(97, 97 + expectedMatches) // allow for a-z values + .forEach(i -> dissectMatch.add(new DissectKey(new String(new byte[] { (byte) i }, StandardCharsets.UTF_8)), "")); assertThat(dissectMatch.getResults(), equalTo(dissectMatch.getResults())); } - public void testAppend(){ + public void testAppend() { DissectMatch dissectMatch = new DissectMatch("-", 3, 1, 3, 0); dissectMatch.add(new DissectKey("+a"), "x"); dissectMatch.add(new DissectKey("+a"), "y"); @@ -60,7 +60,7 @@ public void testAppend(){ assertThat(results, equalTo(MapBuilder.newMapBuilder().put("a", "x-y-z").map())); } - public void testAppendWithOrder(){ + public void testAppendWithOrder() { DissectMatch dissectMatch = new DissectMatch("-", 3, 1, 3, 0); dissectMatch.add(new DissectKey("+a/3"), "x"); dissectMatch.add(new DissectKey("+a"), "y"); @@ -70,7 +70,7 @@ public void testAppendWithOrder(){ assertThat(results, equalTo(MapBuilder.newMapBuilder().put("a", "y-z-x").map())); } - public void testReference(){ + public void testReference() { DissectMatch dissectMatch = new DissectMatch("-", 2, 1, 0, 1); dissectMatch.add(new DissectKey("&a"), "x"); dissectMatch.add(new DissectKey("*a"), "y"); diff --git a/libs/dissect/src/test/java/org/elasticsearch/dissect/DissectParserTests.java b/libs/dissect/src/test/java/org/elasticsearch/dissect/DissectParserTests.java index dd48e309506d4..4ba5e0f7f621f 100644 --- a/libs/dissect/src/test/java/org/elasticsearch/dissect/DissectParserTests.java +++ b/libs/dissect/src/test/java/org/elasticsearch/dissect/DissectParserTests.java @@ -10,6 +10,7 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; + import org.elasticsearch.test.ESTestCase; import org.hamcrest.CoreMatchers; import org.hamcrest.Matchers; @@ -53,46 +54,103 @@ public void testLogstashSpecs() { assertMatch("%{a} %{b} %{+b} %{z}", "foo bar baz quux", Arrays.asList("a", "b", "z"), Arrays.asList("foo", "bar baz", "quux"), " "); assertMatch("%{a}------->%{b}", "foo------->bar baz quux", Arrays.asList("a", "b"), Arrays.asList("foo", "bar baz quux")); assertMatch("%{a}------->%{}", "foo------->bar baz quux", Arrays.asList("a"), Arrays.asList("foo")); - assertMatch("%{a} » %{b}»%{c}€%{d}", "foo » bar»baz€quux", - Arrays.asList("a", "b", "c", "d"), Arrays.asList("foo", "bar", "baz", "quux")); + assertMatch( + "%{a} » %{b}»%{c}€%{d}", + "foo » bar»baz€quux", + Arrays.asList("a", "b", "c", "d"), + Arrays.asList("foo", "bar", "baz", "quux") + ); assertMatch("%{a} %{b} %{+a}", "foo bar baz quux", Arrays.asList("a", "b"), Arrays.asList("foo baz quux", "bar"), " "); - //Logstash supports implicit ordering based anchored by the key without the '+' - //This implementation will only honor implicit ordering for appending right to left else explicit order (/N) is required. - //The results of this test differ from Logstash. - assertMatch("%{+a} %{a} %{+a} %{b}", "December 31 1999 quux", - Arrays.asList("a", "b"), Arrays.asList("December 31 1999", "quux"), " "); - //Same test as above, but with same result as Logstash using explicit ordering in the pattern - assertMatch("%{+a/1} %{a} %{+a/2} %{b}", "December 31 1999 quux", - Arrays.asList("a", "b"), Arrays.asList("31 December 1999", "quux"), " "); + // Logstash supports implicit ordering based anchored by the key without the '+' + // This implementation will only honor implicit ordering for appending right to left else explicit order (/N) is required. + // The results of this test differ from Logstash. + assertMatch( + "%{+a} %{a} %{+a} %{b}", + "December 31 1999 quux", + Arrays.asList("a", "b"), + Arrays.asList("December 31 1999", "quux"), + " " + ); + // Same test as above, but with same result as Logstash using explicit ordering in the pattern + assertMatch( + "%{+a/1} %{a} %{+a/2} %{b}", + "December 31 1999 quux", + Arrays.asList("a", "b"), + Arrays.asList("31 December 1999", "quux"), + " " + ); assertMatch("%{+a/2} %{+a/4} %{+a/1} %{+a/3}", "bar quux foo baz", Arrays.asList("a"), Arrays.asList("foo bar baz quux"), " "); assertMatch("%{+a} %{b}", "foo bar", Arrays.asList("a", "b"), Arrays.asList("foo", "bar")); - assertMatch("%{+a} %{b} %{+a} %{c}", "foo bar baz quux", - Arrays.asList("a", "b", "c"), Arrays.asList("foo baz", "bar", "quux"), " "); - assertMatch("%{} %{syslog_timestamp} %{hostname} %{rt}: %{reason} %{+reason} %{src_ip}/%{src_port}->%{dst_ip}/%{dst_port} " + - "%{polrt} %{+polrt} %{+polrt} %{from_zone} %{to_zone} %{rest}", - "42 2016-05-25T14:47:23Z host.name.com RT_FLOW - RT_FLOW_SESSION_DENY: session denied 2.2.2.20/60000->1.1.1.10/8090 None " + - "6(0) DEFAULT-DENY ZONE-UNTRUST ZONE-DMZ UNKNOWN UNKNOWN N/A(N/A) ge-0/0/0.0", - Arrays.asList("syslog_timestamp", "hostname", "rt", "reason", "src_ip", "src_port", "dst_ip", "dst_port", "polrt" - , "from_zone", "to_zone", "rest"), - Arrays.asList("2016-05-25T14:47:23Z", "host.name.com", "RT_FLOW - RT_FLOW_SESSION_DENY", "session denied", "2.2.2.20", "60000" - , "1.1.1.10", "8090", "None 6(0) DEFAULT-DENY", "ZONE-UNTRUST", "ZONE-DMZ", "UNKNOWN UNKNOWN N/A(N/A) ge-0/0/0.0"), " "); + assertMatch( + "%{+a} %{b} %{+a} %{c}", + "foo bar baz quux", + Arrays.asList("a", "b", "c"), + Arrays.asList("foo baz", "bar", "quux"), + " " + ); + assertMatch( + "%{} %{syslog_timestamp} %{hostname} %{rt}: %{reason} %{+reason} %{src_ip}/%{src_port}->%{dst_ip}/%{dst_port} " + + "%{polrt} %{+polrt} %{+polrt} %{from_zone} %{to_zone} %{rest}", + "42 2016-05-25T14:47:23Z host.name.com RT_FLOW - RT_FLOW_SESSION_DENY: session denied 2.2.2.20/60000->1.1.1.10/8090 None " + + "6(0) DEFAULT-DENY ZONE-UNTRUST ZONE-DMZ UNKNOWN UNKNOWN N/A(N/A) ge-0/0/0.0", + Arrays.asList( + "syslog_timestamp", + "hostname", + "rt", + "reason", + "src_ip", + "src_port", + "dst_ip", + "dst_port", + "polrt", + "from_zone", + "to_zone", + "rest" + ), + Arrays.asList( + "2016-05-25T14:47:23Z", + "host.name.com", + "RT_FLOW - RT_FLOW_SESSION_DENY", + "session denied", + "2.2.2.20", + "60000", + "1.1.1.10", + "8090", + "None 6(0) DEFAULT-DENY", + "ZONE-UNTRUST", + "ZONE-DMZ", + "UNKNOWN UNKNOWN N/A(N/A) ge-0/0/0.0" + ), + " " + ); assertBadKey("%{+/2}"); assertBadKey("%{&+a_field}"); - assertMatch("%{a->} %{b->}---%{c}", "foo bar------------baz", - Arrays.asList("a", "b", "c"), Arrays.asList("foo", "bar", "baz")); + assertMatch( + "%{a->} %{b->}---%{c}", + "foo bar------------baz", + Arrays.asList("a", "b", "c"), + Arrays.asList("foo", "bar", "baz") + ); assertMatch("%{->}-%{a}", "-----666", Arrays.asList("a"), Arrays.asList("666")); assertMatch("%{?skipme->}-%{a}", "-----666", Arrays.asList("a"), Arrays.asList("666")); - assertMatch("%{a},%{b},%{c},%{d},%{e},%{f}", "111,,333,,555,666", - Arrays.asList("a", "b", "c", "d", "e", "f"), Arrays.asList("111", "", "333", "", "555", "666")); + assertMatch( + "%{a},%{b},%{c},%{d},%{e},%{f}", + "111,,333,,555,666", + Arrays.asList("a", "b", "c", "d", "e", "f"), + Arrays.asList("111", "", "333", "", "555", "666") + ); assertMatch("%{a}.࿏.%{b}", "⟳༒.࿏.༒⟲", Arrays.asList("a", "b"), Arrays.asList("⟳༒", "༒⟲")); assertMatch("%{a}", "子", Arrays.asList("a"), Arrays.asList("子")); assertMatch("%{a}{\n}%{b}", "aaa{\n}bbb", Arrays.asList("a", "b"), Arrays.asList("aaa", "bbb")); assertMiss("MACHINE[%{a}] %{b}", "1234567890 MACHINE[foo] bar"); assertMiss("%{a} %{b} %{c}", "foo:bar:baz"); assertMatch("/var/%{key1}/log/%{key2}.log", "/var/foo/log/bar.log", Arrays.asList("key1", "key2"), Arrays.asList("foo", "bar")); - assertMatch("%{a->} %{b}-.-%{c}-%{d}-..-%{e}-%{f}-%{g}-%{h}", "foo bar-.-baz-1111-..-22-333-4444-55555", + assertMatch( + "%{a->} %{b}-.-%{c}-%{d}-..-%{e}-%{f}-%{g}-%{h}", + "foo bar-.-baz-1111-..-22-333-4444-55555", Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h"), - Arrays.asList("foo", "bar", "baz", "1111", "22", "333", "4444", "55555")); + Arrays.asList("foo", "bar", "baz", "1111", "22", "333", "4444", "55555") + ); } public void testBasicMatch() { @@ -100,12 +158,13 @@ public void testBasicMatch() { String keyFirstPattern = ""; String delimiterFirstInput = ""; String delimiterFirstPattern = ""; - //parallel arrays + // parallel arrays List expectedKeys = new ArrayList<>(Sets.newSet(generateRandomStringArray(100, 10, false, false))); List expectedValues = new ArrayList<>(expectedKeys.size()); for (String key : expectedKeys) { String value = randomAsciiAlphanumOfLengthBetween(1, 100); - String delimiter = Integer.toString(randomInt()); //int to ensures values and delimiters don't overlap, else validation can fail + String delimiter = Integer.toString(randomInt()); // int to ensures values and delimiters don't overlap, else validation can + // fail keyFirstPattern += "%{" + key + "}" + delimiter; valueFirstInput += value + delimiter; delimiterFirstPattern += delimiter + "%{" + key + "}"; @@ -121,7 +180,7 @@ public void testBasicMatchUnicode() { String keyFirstPattern = ""; String delimiterFirstInput = ""; String delimiterFirstPattern = ""; - //parallel arrays + // parallel arrays List expectedKeys = new ArrayList<>(); List expectedValues = new ArrayList<>(); for (int i = 0; i < randomIntBetween(1, 100); i++) { @@ -130,7 +189,8 @@ public void testBasicMatchUnicode() { key = randomAsciiAlphanumOfLengthBetween(1, 100); } String value = randomRealisticUnicodeOfCodepointLengthBetween(1, 100); - String delimiter = Integer.toString(randomInt()); //int to ensures values and delimiters don't overlap, else validation can fail + String delimiter = Integer.toString(randomInt()); // int to ensures values and delimiters don't overlap, else validation can + // fail keyFirstPattern += "%{" + key + "}" + delimiter; valueFirstInput += value + delimiter; delimiterFirstPattern += delimiter + "%{" + key + "}"; @@ -172,8 +232,12 @@ public void testAssociate() { assertMatch("%{*a} %{&a}", "foo bar", Arrays.asList("foo"), Arrays.asList("bar")); assertMatch("%{&a} %{*a}", "foo bar", Arrays.asList("bar"), Arrays.asList("foo")); assertMatch("%{*a} %{&a} %{*b} %{&b}", "foo bar baz lol", Arrays.asList("foo", "baz"), Arrays.asList("bar", "lol")); - assertMatch("%{*a} %{&a} %{c} %{*b} %{&b}", "foo bar x baz lol", - Arrays.asList("foo", "baz", "c"), Arrays.asList("bar", "lol", "x")); + assertMatch( + "%{*a} %{&a} %{c} %{*b} %{&b}", + "foo bar x baz lol", + Arrays.asList("foo", "baz", "c"), + Arrays.asList("bar", "lol", "x") + ); assertBadPattern("%{*a} %{a}"); assertBadPattern("%{a} %{&a}"); assertMiss("%{*a} %{&a} {a} %{*b} %{&b}", "foo bar x baz lol"); @@ -183,13 +247,17 @@ public void testPartialKeyDefinition() { assertMatch("%{a} %%{b},%{c}", "foo %bar,baz", Arrays.asList("a", "b", "c"), Arrays.asList("foo", "bar", "baz")); assertMatch("%{a} %{b},%%{c}", "foo bar,%baz", Arrays.asList("a", "b", "c"), Arrays.asList("foo", "bar", "baz")); assertMatch("%%{a} %{b},%{c}", "%foo bar,baz", Arrays.asList("a", "b", "c"), Arrays.asList("foo", "bar", "baz")); - assertMatch("%foo %{bar}", "%foo test", Arrays.asList("bar"), Arrays.asList("test")); + assertMatch("%foo %{bar}", "%foo test", Arrays.asList("bar"), Arrays.asList("test")); } public void testAppendAndAssociate() { assertMatch("%{a} %{+a} %{*b} %{&b}", "foo bar baz lol", Arrays.asList("a", "baz"), Arrays.asList("foobar", "lol")); - assertMatch("%{a->} %{+a/2} %{+a/1} %{*b} %{&b}", "foo bar baz lol x", - Arrays.asList("a", "lol"), Arrays.asList("foobazbar", "x")); + assertMatch( + "%{a->} %{+a/2} %{+a/1} %{*b} %{&b}", + "foo bar baz lol x", + Arrays.asList("a", "lol"), + Arrays.asList("foobazbar", "x") + ); } public void testEmptyKey() { @@ -213,19 +281,19 @@ public void testNamedSkipKey() { } public void testConsecutiveDelimiters() { - //leading + // leading assertMatch("%{->},%{a}", ",,,,,foo", Arrays.asList("a"), Arrays.asList("foo")); assertMatch("%{a->},%{b}", ",,,,,foo", Arrays.asList("a", "b"), Arrays.asList("", "foo")); - //trailing + // trailing assertMatch("%{a->},", "foo,,,,,", Arrays.asList("a"), Arrays.asList("foo")); assertMatch("%{a} %{b},", "foo bar,,,,,", Arrays.asList("a", "b"), Arrays.asList("foo", "bar")); assertMatch("%{a} %{b->},", "foo bar,,,,,", Arrays.asList("a", "b"), Arrays.asList("foo", "bar")); - //middle + // middle assertMatch("%{a->},%{b}", "foo,,,,,bar", Arrays.asList("a", "b"), Arrays.asList("foo", "bar")); assertMatch("%{a->} %{b}", "foo bar", Arrays.asList("a", "b"), Arrays.asList("foo", "bar")); assertMatch("%{a->}x%{b}", "fooxxxxxbar", Arrays.asList("a", "b"), Arrays.asList("foo", "bar")); assertMatch("%{a->} xyz%{b}", "foo xyz xyz xyz xyz xyzbar", Arrays.asList("a", "b"), Arrays.asList("foo", "bar")); - //skipped with empty values + // skipped with empty values assertMatch("%{a},%{b},%{c},%{d}", "foo,,,", Arrays.asList("a", "b", "c", "d"), Arrays.asList("foo", "", "", "")); assertMatch("%{a},%{b},%{c},%{d}", "foo,,bar,baz", Arrays.asList("a", "b", "c", "d"), Arrays.asList("foo", "", "bar", "baz")); assertMatch("%{a},%{b},%{c},%{d}", "foo,,,baz", Arrays.asList("a", "b", "c", "d"), Arrays.asList("foo", "", "", "baz")); @@ -242,16 +310,20 @@ public void testSkipRightPadding() { assertMatch("%{a->} %{b}", "foo bar", Arrays.asList("a", "b"), Arrays.asList("foo", "bar")); assertMatch("%{a->} %{b}", "foo bar", Arrays.asList("a", "b"), Arrays.asList("foo", "bar")); assertMatch("%{->} %{a}", "foo bar", Arrays.asList("a"), Arrays.asList("bar")); - assertMatch("%{a->} %{+a->} %{*b->} %{&b->} %{c}", "foo bar baz lol x", - Arrays.asList("a", "baz", "c"), Arrays.asList("foobar", "lol", "x")); + assertMatch( + "%{a->} %{+a->} %{*b->} %{&b->} %{c}", + "foo bar baz lol x", + Arrays.asList("a", "baz", "c"), + Arrays.asList("foobar", "lol", "x") + ); } public void testTrimmedEnd() { assertMatch("%{a} %{b}", "foo bar", Arrays.asList("a", "b"), Arrays.asList("foo", "bar")); assertMatch("%{a} %{b->} ", "foo bar ", Arrays.asList("a", "b"), Arrays.asList("foo", "bar")); - //only whitespace is trimmed in the absence of trailing characters + // only whitespace is trimmed in the absence of trailing characters assertMatch("%{a} %{b->}", "foo bar,,,,,,", Arrays.asList("a", "b"), Arrays.asList("foo", "bar,,,,,,")); - //consecutive delimiters + right padding can be used to skip over the trailing delimiters + // consecutive delimiters + right padding can be used to skip over the trailing delimiters assertMatch("%{a} %{b->},", "foo bar,,,,,,", Arrays.asList("a", "b"), Arrays.asList("foo", "bar")); } @@ -293,23 +365,50 @@ public void testBadPatternOrKey() { } public void testSyslog() { - assertMatch("%{timestamp} %{+timestamp} %{+timestamp} %{logsource} %{program}[%{pid}]: %{message}", + assertMatch( + "%{timestamp} %{+timestamp} %{+timestamp} %{logsource} %{program}[%{pid}]: %{message}", "Mar 16 00:01:25 evita postfix/smtpd[1713]: connect from camomile.cloud9.net[168.100.1.3]", Arrays.asList("timestamp", "logsource", "program", "pid", "message"), - Arrays.asList("Mar 16 00:01:25", "evita", "postfix/smtpd", "1713", "connect from camomile.cloud9.net[168.100.1.3]"), " "); + Arrays.asList("Mar 16 00:01:25", "evita", "postfix/smtpd", "1713", "connect from camomile.cloud9.net[168.100.1.3]"), + " " + ); } public void testApacheLog() { - assertMatch("%{clientip} %{ident} %{auth} [%{timestamp}] \"%{verb} %{request} HTTP/%{httpversion}\" %{response} %{bytes}" + - " \"%{referrer}\" \"%{agent}\" %{->}", - "31.184.238.164 - - [24/Jul/2014:05:35:37 +0530] \"GET /logs/access.log HTTP/1.0\" 200 69849 " + - "\"http://8rursodiol.enjin.com\" \"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) " + - "Chrome/30.0.1599.12785 YaBrowser/13.12.1599.12785 Safari/537.36\" \"www.dlwindianrailways.com\"", - Arrays.asList("clientip", "ident", "auth", "timestamp", "verb", "request", "httpversion", "response", "bytes", - "referrer", "agent"), - Arrays.asList("31.184.238.164", "-", "-", "24/Jul/2014:05:35:37 +0530", "GET", "/logs/access.log", "1.0", "200", "69849", - "http://8rursodiol.enjin.com", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36" + - " (KHTML, like Gecko) Chrome/30.0.1599.12785 YaBrowser/13.12.1599.12785 Safari/537.36")); + assertMatch( + "%{clientip} %{ident} %{auth} [%{timestamp}] \"%{verb} %{request} HTTP/%{httpversion}\" %{response} %{bytes}" + + " \"%{referrer}\" \"%{agent}\" %{->}", + "31.184.238.164 - - [24/Jul/2014:05:35:37 +0530] \"GET /logs/access.log HTTP/1.0\" 200 69849 " + + "\"http://8rursodiol.enjin.com\" \"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) " + + "Chrome/30.0.1599.12785 YaBrowser/13.12.1599.12785 Safari/537.36\" \"www.dlwindianrailways.com\"", + Arrays.asList( + "clientip", + "ident", + "auth", + "timestamp", + "verb", + "request", + "httpversion", + "response", + "bytes", + "referrer", + "agent" + ), + Arrays.asList( + "31.184.238.164", + "-", + "-", + "24/Jul/2014:05:35:37 +0530", + "GET", + "/logs/access.log", + "1.0", + "200", + "69849", + "http://8rursodiol.enjin.com", + "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36" + + " (KHTML, like Gecko) Chrome/30.0.1599.12785 YaBrowser/13.12.1599.12785 Safari/537.36" + ) + ); } /** diff --git a/libs/geo/src/main/java/org/elasticsearch/geometry/Circle.java b/libs/geo/src/main/java/org/elasticsearch/geometry/Circle.java index b09411faa6349..399711d95aaa9 100644 --- a/libs/geo/src/main/java/org/elasticsearch/geometry/Circle.java +++ b/libs/geo/src/main/java/org/elasticsearch/geometry/Circle.java @@ -37,7 +37,7 @@ public Circle(final double x, final double y, final double z, final double radiu this.x = x; this.radiusMeters = radiusMeters; this.z = z; - if (radiusMeters < 0 ) { + if (radiusMeters < 0) { throw new IllegalArgumentException("Circle radius [" + radiusMeters + "] cannot be negative"); } } diff --git a/libs/geo/src/main/java/org/elasticsearch/geometry/Line.java b/libs/geo/src/main/java/org/elasticsearch/geometry/Line.java index 7ed9063ecc671..904128ef2a4e5 100644 --- a/libs/geo/src/main/java/org/elasticsearch/geometry/Line.java +++ b/libs/geo/src/main/java/org/elasticsearch/geometry/Line.java @@ -132,8 +132,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Line line = (Line) o; - return Arrays.equals(y, line.y) && - Arrays.equals(x, line.x) && Arrays.equals(z, line.z); + return Arrays.equals(y, line.y) && Arrays.equals(x, line.x) && Arrays.equals(z, line.z); } @Override diff --git a/libs/geo/src/main/java/org/elasticsearch/geometry/LinearRing.java b/libs/geo/src/main/java/org/elasticsearch/geometry/LinearRing.java index 0dc54ccd9a209..67bc0392682ec 100644 --- a/libs/geo/src/main/java/org/elasticsearch/geometry/LinearRing.java +++ b/libs/geo/src/main/java/org/elasticsearch/geometry/LinearRing.java @@ -18,8 +18,7 @@ public class LinearRing extends Line { public static final LinearRing EMPTY = new LinearRing(); - private LinearRing() { - } + private LinearRing() {} public LinearRing(double[] x, double[] y) { this(x, y, null); @@ -32,10 +31,22 @@ public LinearRing(double[] x, double[] y, double[] z) { } int last = x.length - 1; if (x[0] != x[last] || y[0] != y[last] || (z != null && z[0] != z[last])) { - throw new IllegalArgumentException("first and last points of the linear ring must be the same (it must close itself):" + - " x[0]=" + x[0] + " x[" + last + "]=" + x[last] + - " y[0]=" + y[0] + " y[" + last + "]=" + y[last] + - (z == null ? "" : " z[0]=" + z[0] + " z[" + last + "]=" + z[last] )); + throw new IllegalArgumentException( + "first and last points of the linear ring must be the same (it must close itself):" + + " x[0]=" + + x[0] + + " x[" + + last + + "]=" + + x[last] + + " y[0]=" + + y[0] + + " y[" + + last + + "]=" + + y[last] + + (z == null ? "" : " z[0]=" + z[0] + " z[" + last + "]=" + z[last]) + ); } } @@ -51,8 +62,11 @@ public T visit(GeometryVisitor visitor) throws E @Override public String toString() { - return "linearring(x=" + Arrays.toString(getX()) + - ", y=" + Arrays.toString(getY()) + - (hasZ() ? ", z=" + Arrays.toString(getZ()) : "") + ")"; + return "linearring(x=" + + Arrays.toString(getX()) + + ", y=" + + Arrays.toString(getY()) + + (hasZ() ? ", z=" + Arrays.toString(getZ()) : "") + + ")"; } } diff --git a/libs/geo/src/main/java/org/elasticsearch/geometry/MultiLine.java b/libs/geo/src/main/java/org/elasticsearch/geometry/MultiLine.java index 8b3628aba7bbc..2e36cb5999127 100644 --- a/libs/geo/src/main/java/org/elasticsearch/geometry/MultiLine.java +++ b/libs/geo/src/main/java/org/elasticsearch/geometry/MultiLine.java @@ -16,8 +16,7 @@ public class MultiLine extends GeometryCollection { public static final MultiLine EMPTY = new MultiLine(); - private MultiLine() { - } + private MultiLine() {} public MultiLine(List lines) { super(lines); diff --git a/libs/geo/src/main/java/org/elasticsearch/geometry/MultiPoint.java b/libs/geo/src/main/java/org/elasticsearch/geometry/MultiPoint.java index 7e2d64cba6747..43871fae6f6b6 100644 --- a/libs/geo/src/main/java/org/elasticsearch/geometry/MultiPoint.java +++ b/libs/geo/src/main/java/org/elasticsearch/geometry/MultiPoint.java @@ -16,8 +16,7 @@ public class MultiPoint extends GeometryCollection { public static final MultiPoint EMPTY = new MultiPoint(); - private MultiPoint() { - } + private MultiPoint() {} public MultiPoint(List points) { super(points); diff --git a/libs/geo/src/main/java/org/elasticsearch/geometry/MultiPolygon.java b/libs/geo/src/main/java/org/elasticsearch/geometry/MultiPolygon.java index 63ea382e8b15b..5618f42b6aa49 100644 --- a/libs/geo/src/main/java/org/elasticsearch/geometry/MultiPolygon.java +++ b/libs/geo/src/main/java/org/elasticsearch/geometry/MultiPolygon.java @@ -16,8 +16,7 @@ public class MultiPolygon extends GeometryCollection { public static final MultiPolygon EMPTY = new MultiPolygon(); - private MultiPolygon() { - } + private MultiPolygon() {} public MultiPolygon(List polygons) { super(polygons); diff --git a/libs/geo/src/main/java/org/elasticsearch/geometry/Polygon.java b/libs/geo/src/main/java/org/elasticsearch/geometry/Polygon.java index bc2eb1acd4dc5..d6a08a8bec20f 100644 --- a/libs/geo/src/main/java/org/elasticsearch/geometry/Polygon.java +++ b/libs/geo/src/main/java/org/elasticsearch/geometry/Polygon.java @@ -110,8 +110,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Polygon polygon1 = (Polygon) o; - return Objects.equals(polygon, polygon1.polygon) && - Objects.equals(holes, polygon1.holes); + return Objects.equals(polygon, polygon1.polygon) && Objects.equals(holes, polygon1.holes); } @Override diff --git a/libs/geo/src/main/java/org/elasticsearch/geometry/Rectangle.java b/libs/geo/src/main/java/org/elasticsearch/geometry/Rectangle.java index ff5fc792ed0a1..fceb27c7515a2 100644 --- a/libs/geo/src/main/java/org/elasticsearch/geometry/Rectangle.java +++ b/libs/geo/src/main/java/org/elasticsearch/geometry/Rectangle.java @@ -58,6 +58,7 @@ private Rectangle() { public Rectangle(double minX, double maxX, double maxY, double minY) { this(minX, maxX, maxY, minY, Double.NaN, Double.NaN); } + /** * Constructs a bounding box by first validating the provided latitude and longitude coordinates */ @@ -109,7 +110,6 @@ public double getMinLon() { return minX; } - public double getMinAlt() { return minZ; } @@ -136,7 +136,6 @@ public String toString() { return WellKnownText.toWKT(this); } - @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/libs/geo/src/main/java/org/elasticsearch/geometry/package-info.java b/libs/geo/src/main/java/org/elasticsearch/geometry/package-info.java index a39d8e2e43748..182c35fc61809 100644 --- a/libs/geo/src/main/java/org/elasticsearch/geometry/package-info.java +++ b/libs/geo/src/main/java/org/elasticsearch/geometry/package-info.java @@ -6,7 +6,6 @@ * Side Public License, v 1. */ - /** * Common Geometry classes */ diff --git a/libs/geo/src/main/java/org/elasticsearch/geometry/utils/BitUtil.java b/libs/geo/src/main/java/org/elasticsearch/geometry/utils/BitUtil.java index 6c63c138004de..4cf2347c91ea7 100644 --- a/libs/geo/src/main/java/org/elasticsearch/geometry/utils/BitUtil.java +++ b/libs/geo/src/main/java/org/elasticsearch/geometry/utils/BitUtil.java @@ -12,13 +12,15 @@ */ public class BitUtil { // magic numbers for bit interleaving private static final long MAGIC[] = { - 0x5555555555555555L, 0x3333333333333333L, - 0x0F0F0F0F0F0F0F0FL, 0x00FF00FF00FF00FFL, - 0x0000FFFF0000FFFFL, 0x00000000FFFFFFFFL, - 0xAAAAAAAAAAAAAAAAL - }; + 0x5555555555555555L, + 0x3333333333333333L, + 0x0F0F0F0F0F0F0F0FL, + 0x00FF00FF00FF00FFL, + 0x0000FFFF0000FFFFL, + 0x00000000FFFFFFFFL, + 0xAAAAAAAAAAAAAAAAL }; // shift values for bit interleaving - private static final short SHIFT[] = {1, 2, 4, 8, 16}; + private static final short SHIFT[] = { 1, 2, 4, 8, 16 }; /** * Interleaves the first 32 bits of each long value @@ -39,7 +41,7 @@ public static long interleave(int even, int odd) { v2 = (v2 | (v2 << SHIFT[1])) & MAGIC[1]; v2 = (v2 | (v2 << SHIFT[0])) & MAGIC[0]; - return (v2<<1) | v1; + return (v2 << 1) | v1; } /** @@ -59,6 +61,6 @@ public static long deinterleave(long b) { * flip flops odd with even bits */ public static final long flipFlop(final long b) { - return ((b & MAGIC[6]) >>> 1) | ((b & MAGIC[0]) << 1 ); + return ((b & MAGIC[6]) >>> 1) | ((b & MAGIC[0]) << 1); } } diff --git a/libs/geo/src/main/java/org/elasticsearch/geometry/utils/GeographyValidator.java b/libs/geo/src/main/java/org/elasticsearch/geometry/utils/GeographyValidator.java index 630f5a0e415a2..83d19244dce24 100644 --- a/libs/geo/src/main/java/org/elasticsearch/geometry/utils/GeographyValidator.java +++ b/libs/geo/src/main/java/org/elasticsearch/geometry/utils/GeographyValidator.java @@ -66,7 +66,8 @@ public static GeometryValidator instance(boolean ignoreZValue) { protected void checkLatitude(double latitude) { if (Double.isNaN(latitude) || latitude < MIN_LAT_INCL || latitude > MAX_LAT_INCL) { throw new IllegalArgumentException( - "invalid latitude " + latitude + "; must be between " + MIN_LAT_INCL + " and " + MAX_LAT_INCL); + "invalid latitude " + latitude + "; must be between " + MIN_LAT_INCL + " and " + MAX_LAT_INCL + ); } } @@ -76,14 +77,16 @@ protected void checkLatitude(double latitude) { protected void checkLongitude(double longitude) { if (Double.isNaN(longitude) || longitude < MIN_LON_INCL || longitude > MAX_LON_INCL) { throw new IllegalArgumentException( - "invalid longitude " + longitude + "; must be between " + MIN_LON_INCL + " and " + MAX_LON_INCL); + "invalid longitude " + longitude + "; must be between " + MIN_LON_INCL + " and " + MAX_LON_INCL + ); } } protected void checkAltitude(double zValue) { if (ignoreZValue == false && Double.isNaN(zValue) == false) { - throw new IllegalArgumentException("found Z value [" + zValue + "] but [ignore_z_value] " - + "parameter is [" + ignoreZValue + "]"); + throw new IllegalArgumentException( + "found Z value [" + zValue + "] but [ignore_z_value] " + "parameter is [" + ignoreZValue + "]" + ); } } diff --git a/libs/geo/src/main/java/org/elasticsearch/geometry/utils/Geohash.java b/libs/geo/src/main/java/org/elasticsearch/geometry/utils/Geohash.java index f192d3810ff9c..90ae3b76e796d 100644 --- a/libs/geo/src/main/java/org/elasticsearch/geometry/utils/Geohash.java +++ b/libs/geo/src/main/java/org/elasticsearch/geometry/utils/Geohash.java @@ -24,21 +24,51 @@ * NOTE: this will replace {@code org.elasticsearch.common.geo.GeoHashUtils} */ public class Geohash { - private static final char[] BASE_32 = {'0', '1', '2', '3', '4', '5', '6', - '7', '8', '9', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', - 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; + private static final char[] BASE_32 = { + '0', + '1', + '2', + '3', + '4', + '5', + '6', + '7', + '8', + '9', + 'b', + 'c', + 'd', + 'e', + 'f', + 'g', + 'h', + 'j', + 'k', + 'm', + 'n', + 'p', + 'q', + 'r', + 's', + 't', + 'u', + 'v', + 'w', + 'x', + 'y', + 'z' }; private static final String BASE_32_STRING = new String(BASE_32); /** maximum precision for geohash strings */ public static final int PRECISION = 12; /** number of bits used for quantizing latitude and longitude values */ private static final short BITS = 32; - private static final double LAT_SCALE = (0x1L<<(BITS-1))/180.0D; - private static final double LAT_DECODE = 180.0D/(0x1L<>> 4) << shift); Point topRight = new Point(decodeLongitude(mortonHash), decodeLatitude(mortonHash)); return new Rectangle(bottomLeft.getX(), topRight.getX(), topRight.getY(), bottomLeft.getY()); } else { // We cannot go north of north pole, so just using 90 degrees instead of calculating it using // add 1 to lon to get lon of topRight, we are going to use 90 for lat - ghLong = BitUtil.interleave((int)lat, (int)(lon + 1)) << 4 | len; + ghLong = BitUtil.interleave((int) lat, (int) (lon + 1)) << 4 | len; final long mortonHash = BitUtil.flipFlop((ghLong >>> 4) << shift); Point topRight = new Point(decodeLongitude(mortonHash), decodeLatitude(mortonHash)); return new Rectangle(bottomLeft.getX(), topRight.getX(), 90D, bottomLeft.getY()); @@ -103,9 +132,9 @@ public static Rectangle toBoundingBox(final String geohash) { /** Array of geohashes one level below the baseGeohash. Sorted. */ public static String[] getSubGeohashes(String baseGeohash) { String[] hashes = new String[BASE_32.length]; - for (int i = 0; i < BASE_32.length; i++) {//note: already sorted + for (int i = 0; i < BASE_32.length; i++) {// note: already sorted char c = BASE_32[i]; - hashes[i] = baseGeohash+c; + hashes[i] = baseGeohash + c; } return hashes; } @@ -119,6 +148,7 @@ public static String[] getSubGeohashes(String baseGeohash) { public static Collection getNeighbors(String geohash) { return addNeighborsAtLevel(geohash, geohash.length(), new ArrayList(8)); } + /** * Add all geohashes of the cells next to a given geohash to a list. * @@ -138,8 +168,7 @@ public static final > E addNeighbors(String * @param neighbors list to add the neighbors to * @return the given list */ - public static final > E addNeighborsAtLevel(String geohash, - int level, E neighbors) { + public static final > E addNeighborsAtLevel(String geohash, int level, E neighbors) { String south = getNeighbor(geohash, level, 0, -1); String north = getNeighbor(geohash, level, 0, +1); if (north != null) { @@ -170,7 +199,7 @@ public static final > E addNeighborsAtLevel * @return geohash of the defined cell */ public static final String getNeighbor(String geohash, int level, int dx, int dy) { - int cell = BASE_32_STRING.indexOf(geohash.charAt(level -1)); + int cell = BASE_32_STRING.indexOf(geohash.charAt(level - 1)); // Decoding the Geohash bit pattern to determine grid coordinates int x0 = cell & 1; // first bit of x @@ -226,7 +255,7 @@ public static final long longEncode(String hash) { */ public static final long longEncode(final double lon, final double lat, final int level) { // shift to appropriate level - final short msf = (short)(((12 - level) * 5) + (MORTON_OFFSET - 2)); + final short msf = (short) (((12 - level) * 5) + (MORTON_OFFSET - 2)); return ((encodeLatLon(lat, lon) >>> msf) << 4) | level; } @@ -252,13 +281,13 @@ public static final String stringEncode(final double lon, final double lat, fina * Encode to a geohash string from the geohash based long format */ public static final String stringEncode(long geoHashLong) { - int level = (int)geoHashLong&15; + int level = (int) geoHashLong & 15; geoHashLong >>>= 4; char[] chars = new char[level]; do { - chars[--level] = BASE_32[(int) (geoHashLong&31L)]; - geoHashLong>>>=5; - } while(level > 0); + chars[--level] = BASE_32[(int) (geoHashLong & 31L)]; + geoHashLong >>>= 5; + } while (level > 0); return new String(chars); } @@ -275,9 +304,9 @@ private static long longEncode(final String hash, int length) { int level = length - 1; long b; long l = 0L; - for(char c : hash.toCharArray()) { - b = (long)(BASE_32_STRING.indexOf(c)); - l |= (b<<(level--*5)); + for (char c : hash.toCharArray()) { + b = (long) (BASE_32_STRING.indexOf(c)); + l |= (b << (level-- * 5)); if (level < 0) { // We cannot handle more than 12 levels break; @@ -296,12 +325,12 @@ public static long mortonEncode(final String hash) { int level = 11; long b; long l = 0L; - for(char c : hash.toCharArray()) { - b = (long)(BASE_32_STRING.indexOf(c)); + for (char c : hash.toCharArray()) { + b = (long) (BASE_32_STRING.indexOf(c)); if (b < 0) { throw new IllegalArgumentException("unsupported symbol [" + c + "] in geohash [" + hash + "]"); } - l |= (b<<((level--*5) + (MORTON_OFFSET - 2))); + l |= (b << ((level-- * 5) + (MORTON_OFFSET - 2))); if (level < 0) { // We cannot handle more than 12 levels break; @@ -327,7 +356,6 @@ private static long encodeLatLon(final double lat, final double lon) { return BitUtil.interleave(latEnc, lonEnc) >>> 2; } - /** encode latitude to integer */ public static int encodeLatitude(double latitude) { // the maximum possible value cannot be encoded without overflow diff --git a/libs/geo/src/main/java/org/elasticsearch/geometry/utils/StandardValidator.java b/libs/geo/src/main/java/org/elasticsearch/geometry/utils/StandardValidator.java index 73e0353589ee9..fd45b01565e38 100644 --- a/libs/geo/src/main/java/org/elasticsearch/geometry/utils/StandardValidator.java +++ b/libs/geo/src/main/java/org/elasticsearch/geometry/utils/StandardValidator.java @@ -32,7 +32,7 @@ public class StandardValidator implements GeometryValidator { private final boolean ignoreZValue; private StandardValidator(boolean ignoreZValue) { - this.ignoreZValue = ignoreZValue; + this.ignoreZValue = ignoreZValue; } public static GeometryValidator instance(boolean ignoreZValue) { @@ -41,8 +41,9 @@ public static GeometryValidator instance(boolean ignoreZValue) { protected void checkZ(double zValue) { if (ignoreZValue == false && Double.isNaN(zValue) == false) { - throw new IllegalArgumentException("found Z value [" + zValue + "] but [ignore_z_value] " - + "parameter is [" + ignoreZValue + "]"); + throw new IllegalArgumentException( + "found Z value [" + zValue + "] but [ignore_z_value] " + "parameter is [" + ignoreZValue + "]" + ); } } @@ -121,4 +122,3 @@ public Void visit(Rectangle rectangle) throws RuntimeException { } } } - diff --git a/libs/geo/src/main/java/org/elasticsearch/geometry/utils/WellKnownText.java b/libs/geo/src/main/java/org/elasticsearch/geometry/utils/WellKnownText.java index c7b2f641a0c44..3e1aabeb4063d 100644 --- a/libs/geo/src/main/java/org/elasticsearch/geometry/utils/WellKnownText.java +++ b/libs/geo/src/main/java/org/elasticsearch/geometry/utils/WellKnownText.java @@ -46,8 +46,7 @@ public class WellKnownText { private static final String EOF = "END-OF-STREAM"; private static final String EOL = "END-OF-LINE"; - private WellKnownText() { - } + private WellKnownText() {} public static String toWKT(Geometry geometry) { StringBuilder builder = new StringBuilder(); @@ -270,8 +269,8 @@ private static Geometry parseGeometry(StreamTokenizer stream, boolean coerce) th throw new IllegalArgumentException("Unknown geometry type: " + type); } - private static GeometryCollection parseGeometryCollection(StreamTokenizer stream, boolean coerce) - throws IOException, ParseException { + private static GeometryCollection parseGeometryCollection(StreamTokenizer stream, boolean coerce) throws IOException, + ParseException { if (nextEmptyOrOpen(stream).equals(EMPTY)) { return GeometryCollection.EMPTY; } @@ -416,8 +415,9 @@ private static Polygon parsePolygon(StreamTokenizer stream, boolean coerce) thro private static void closeLinearRingIfCoerced(ArrayList lats, ArrayList lons, ArrayList alts, boolean coerce) { if (coerce && lats.isEmpty() == false && lons.isEmpty() == false) { int last = lats.size() - 1; - if (lats.get(0).equals(lats.get(last)) == false || lons.get(0).equals(lons.get(last)) == false || - (alts.isEmpty() == false && alts.get(0).equals(alts.get(last)) == false)) { + if (lats.get(0).equals(lats.get(last)) == false + || lons.get(0).equals(lons.get(last)) == false + || (alts.isEmpty() == false && alts.get(0).equals(alts.get(last)) == false)) { lons.add(lons.get(0)); lats.add(lats.get(0)); if (alts.isEmpty() == false) { @@ -456,7 +456,6 @@ private static Rectangle parseBBox(StreamTokenizer stream) throws IOException, P return new Rectangle(minLon, maxLon, maxLat, minLat); } - private static Circle parseCircle(StreamTokenizer stream) throws IOException, ParseException { if (nextEmptyOrOpen(stream).equals(EMPTY)) { return Circle.EMPTY; @@ -531,8 +530,7 @@ private static String nextEmptyOrOpen(StreamTokenizer stream) throws IOException if (next.equals(EMPTY) || next.equals(LPAREN)) { return next; } - throw new ParseException("expected " + EMPTY + " or " + LPAREN - + " but found: " + tokenString(stream), stream.lineno()); + throw new ParseException("expected " + EMPTY + " or " + LPAREN + " but found: " + tokenString(stream), stream.lineno()); } private static String nextCloser(StreamTokenizer stream) throws IOException, ParseException { @@ -561,8 +559,7 @@ private static String nextCloserOrComma(StreamTokenizer stream) throws IOExcepti if (token.equals(COMMA) || token.equals(RPAREN)) { return token; } - throw new ParseException("expected " + COMMA + " or " + RPAREN - + " but found: " + tokenString(stream), stream.lineno()); + throw new ParseException("expected " + COMMA + " or " + RPAREN + " but found: " + tokenString(stream), stream.lineno()); } private static String getWKTName(Geometry geometry) { diff --git a/libs/geo/src/test/java/org/elasticsearch/geometry/BaseGeometryTestCase.java b/libs/geo/src/test/java/org/elasticsearch/geometry/BaseGeometryTestCase.java index 71adc1a75f9db..3493ad99c54f5 100644 --- a/libs/geo/src/test/java/org/elasticsearch/geometry/BaseGeometryTestCase.java +++ b/libs/geo/src/test/java/org/elasticsearch/geometry/BaseGeometryTestCase.java @@ -62,7 +62,8 @@ public Object visit(Circle circle) { @Override public Object visit(GeometryCollection collection) { - return verify(collection, "GeometryCollection"); } + return verify(collection, "GeometryCollection"); + } @Override public Object visit(Line line) { diff --git a/libs/geo/src/test/java/org/elasticsearch/geometry/CircleTests.java b/libs/geo/src/test/java/org/elasticsearch/geometry/CircleTests.java index 16b732597236a..bae91638bd802 100644 --- a/libs/geo/src/test/java/org/elasticsearch/geometry/CircleTests.java +++ b/libs/geo/src/test/java/org/elasticsearch/geometry/CircleTests.java @@ -20,15 +20,19 @@ public class CircleTests extends BaseGeometryTestCase { @Override protected Circle createTestInstance(boolean hasAlt) { if (hasAlt) { - return new Circle(randomDoubleBetween(-180, 180, true), randomDoubleBetween(-90, 90, true), randomDouble(), - randomDoubleBetween(0, 100, false)); - } else { + return new Circle( + randomDoubleBetween(-180, 180, true), + randomDoubleBetween(-90, 90, true), + randomDouble(), + randomDoubleBetween(0, 100, false) + ); + } else { return new Circle(randomDoubleBetween(-180, 180, true), randomDoubleBetween(-90, 90, true), randomDoubleBetween(0, 100, false)); } } public void testBasicSerialization() throws IOException, ParseException { - GeometryValidator validator = GeographyValidator.instance(true); + GeometryValidator validator = GeographyValidator.instance(true); assertEquals("CIRCLE (20.0 10.0 15.0)", WellKnownText.toWKT(new Circle(20, 10, 15))); assertEquals(new Circle(20, 10, 15), WellKnownText.fromWKT(validator, true, "circle (20.0 10.0 15.0)")); diff --git a/libs/geo/src/test/java/org/elasticsearch/geometry/GeometryCollectionTests.java b/libs/geo/src/test/java/org/elasticsearch/geometry/GeometryCollectionTests.java index f57a3dead0d4f..45842505ce5f7 100644 --- a/libs/geo/src/test/java/org/elasticsearch/geometry/GeometryCollectionTests.java +++ b/libs/geo/src/test/java/org/elasticsearch/geometry/GeometryCollectionTests.java @@ -27,11 +27,15 @@ protected GeometryCollection createTestInstance(boolean hasAlt) { public void testBasicSerialization() throws IOException, ParseException { GeometryValidator validator = GeographyValidator.instance(true); - assertEquals("GEOMETRYCOLLECTION (POINT (20.0 10.0),POINT EMPTY)", - WellKnownText.toWKT(new GeometryCollection(Arrays.asList(new Point(20, 10), Point.EMPTY)))); + assertEquals( + "GEOMETRYCOLLECTION (POINT (20.0 10.0),POINT EMPTY)", + WellKnownText.toWKT(new GeometryCollection(Arrays.asList(new Point(20, 10), Point.EMPTY))) + ); - assertEquals(new GeometryCollection(Arrays.asList(new Point(20, 10), Point.EMPTY)), - WellKnownText.fromWKT(validator, true, "GEOMETRYCOLLECTION (POINT (20.0 10.0),POINT EMPTY)")); + assertEquals( + new GeometryCollection(Arrays.asList(new Point(20, 10), Point.EMPTY)), + WellKnownText.fromWKT(validator, true, "GEOMETRYCOLLECTION (POINT (20.0 10.0),POINT EMPTY)") + ); assertEquals("GEOMETRYCOLLECTION EMPTY", WellKnownText.toWKT(GeometryCollection.EMPTY)); assertEquals(GeometryCollection.EMPTY, WellKnownText.fromWKT(validator, true, "GEOMETRYCOLLECTION EMPTY)")); @@ -45,12 +49,17 @@ public void testInitValidation() { ex = expectThrows(IllegalArgumentException.class, () -> new GeometryCollection<>(null)); assertEquals("the list of shapes cannot be null or empty", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, () -> new GeometryCollection<>( - Arrays.asList(new Point(20, 10), new Point(20, 10, 30)))); + ex = expectThrows( + IllegalArgumentException.class, + () -> new GeometryCollection<>(Arrays.asList(new Point(20, 10), new Point(20, 10, 30))) + ); assertEquals("all elements of the collection should have the same number of dimension", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, () -> StandardValidator.instance(false).validate( - new GeometryCollection(Collections.singletonList(new Point(20, 10, 30))))); + ex = expectThrows( + IllegalArgumentException.class, + () -> StandardValidator.instance(false) + .validate(new GeometryCollection(Collections.singletonList(new Point(20, 10, 30)))) + ); assertEquals("found Z value [30.0] but [ignore_z_value] parameter is [false]", ex.getMessage()); StandardValidator.instance(true).validate(new GeometryCollection(Collections.singletonList(new Point(20, 10, 30)))); diff --git a/libs/geo/src/test/java/org/elasticsearch/geometry/GeometryValidatorTests.java b/libs/geo/src/test/java/org/elasticsearch/geometry/GeometryValidatorTests.java index b96bffc555448..99e4d5a305ed6 100644 --- a/libs/geo/src/test/java/org/elasticsearch/geometry/GeometryValidatorTests.java +++ b/libs/geo/src/test/java/org/elasticsearch/geometry/GeometryValidatorTests.java @@ -62,7 +62,8 @@ public OneValidator() { protected void checkLatitude(double latitude) { if (Double.isNaN(latitude) || latitude < MIN_LAT_INCL || latitude > MAX_LAT_INCL) { throw new IllegalArgumentException( - "invalid latitude " + latitude + "; must be between " + MIN_LAT_INCL + " and " + MAX_LAT_INCL); + "invalid latitude " + latitude + "; must be between " + MIN_LAT_INCL + " and " + MAX_LAT_INCL + ); } } @@ -70,7 +71,8 @@ protected void checkLatitude(double latitude) { protected void checkLongitude(double longitude) { if (Double.isNaN(longitude) || longitude < MIN_LON_INCL || longitude > MAX_LON_INCL) { throw new IllegalArgumentException( - "invalid longitude " + longitude + "; must be between " + MIN_LON_INCL + " and " + MAX_LON_INCL); + "invalid longitude " + longitude + "; must be between " + MIN_LON_INCL + " and " + MAX_LON_INCL + ); } } @@ -78,7 +80,8 @@ protected void checkLongitude(double longitude) { protected void checkAltitude(double zValue) { if (Double.isNaN(zValue) == false && (zValue < MIN_ALT_INCL || zValue > MAX_ALT_INCL)) { throw new IllegalArgumentException( - "invalid altitude " + zValue + "; must be between " + MIN_ALT_INCL + " and " + MAX_ALT_INCL); + "invalid altitude " + zValue + "; must be between " + MIN_ALT_INCL + " and " + MAX_ALT_INCL + ); } } } @@ -103,11 +106,15 @@ public void testOneValidator() throws Exception { assertEquals("invalid longitude 2.0; must be between -1.0 and 1.0", ex.getMessage()); ex = expectThrows(IllegalArgumentException.class, () -> WellKnownText.fromWKT(validator, true, "LINESTRING (1 -1 0, 0 0 2)")); assertEquals("invalid altitude 2.0; must be between -1.0 and 1.0", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, () -> - WellKnownText.fromWKT(validator, true, "POLYGON ((0.3 0.1, 0.4 0.2, 5 0.3, 0.3 0.1))")); + ex = expectThrows( + IllegalArgumentException.class, + () -> WellKnownText.fromWKT(validator, true, "POLYGON ((0.3 0.1, 0.4 0.2, 5 0.3, 0.3 0.1))") + ); assertEquals("invalid longitude 5.0; must be between -1.0 and 1.0", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, () -> - WellKnownText.fromWKT(validator, true, "POLYGON ((0.3 0.1, 0.4 0.2, 0.5 0.3, 0.3 0.1), (0.5 1.5, 2.5 1.5, 2.0 1.0))")); + ex = expectThrows( + IllegalArgumentException.class, + () -> WellKnownText.fromWKT(validator, true, "POLYGON ((0.3 0.1, 0.4 0.2, 0.5 0.3, 0.3 0.1), (0.5 1.5, 2.5 1.5, 2.0 1.0))") + ); assertEquals("invalid latitude 1.5; must be between -1.0 and 1.0", ex.getMessage()); ex = expectThrows(IllegalArgumentException.class, () -> WellKnownText.fromWKT(validator, true, "MULTIPOINT (0 1, -2 1)")); assertEquals("invalid longitude -2.0; must be between -1.0 and 1.0", ex.getMessage()); diff --git a/libs/geo/src/test/java/org/elasticsearch/geometry/LineTests.java b/libs/geo/src/test/java/org/elasticsearch/geometry/LineTests.java index b29dd7adbbde2..3003a243c96af 100644 --- a/libs/geo/src/test/java/org/elasticsearch/geometry/LineTests.java +++ b/libs/geo/src/test/java/org/elasticsearch/geometry/LineTests.java @@ -25,13 +25,20 @@ protected Line createTestInstance(boolean hasAlt) { public void testBasicSerialization() throws IOException, ParseException { GeometryValidator validator = GeographyValidator.instance(true); - assertEquals("LINESTRING (3.0 1.0, 4.0 2.0)", WellKnownText.toWKT(new Line(new double[]{3, 4}, new double[]{1, 2}))); - assertEquals(new Line(new double[]{3, 4}, new double[]{1, 2}), WellKnownText.fromWKT(validator, true, "LINESTRING (3 1, 4 2)")); + assertEquals("LINESTRING (3.0 1.0, 4.0 2.0)", WellKnownText.toWKT(new Line(new double[] { 3, 4 }, new double[] { 1, 2 }))); + assertEquals( + new Line(new double[] { 3, 4 }, new double[] { 1, 2 }), + WellKnownText.fromWKT(validator, true, "LINESTRING (3 1, 4 2)") + ); - assertEquals("LINESTRING (3.0 1.0 5.0, 4.0 2.0 6.0)", WellKnownText.toWKT(new Line(new double[]{3, 4}, new double[]{1, 2}, - new double[]{5, 6}))); - assertEquals(new Line(new double[]{3, 4}, new double[]{1, 2}, new double[]{6, 5}), - WellKnownText.fromWKT(validator, true, "LINESTRING (3 1 6, 4 2 5)")); + assertEquals( + "LINESTRING (3.0 1.0 5.0, 4.0 2.0 6.0)", + WellKnownText.toWKT(new Line(new double[] { 3, 4 }, new double[] { 1, 2 }, new double[] { 5, 6 })) + ); + assertEquals( + new Line(new double[] { 3, 4 }, new double[] { 1, 2 }, new double[] { 6, 5 }), + WellKnownText.fromWKT(validator, true, "LINESTRING (3 1 6, 4 2 5)") + ); assertEquals("LINESTRING EMPTY", WellKnownText.toWKT(Line.EMPTY)); assertEquals(Line.EMPTY, WellKnownText.fromWKT(validator, true, "LINESTRING EMPTY)")); @@ -39,28 +46,38 @@ public void testBasicSerialization() throws IOException, ParseException { public void testInitValidation() { GeometryValidator validator = GeographyValidator.instance(true); - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, - () -> validator.validate(new Line(new double[]{3}, new double[]{1}))); + IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> validator.validate(new Line(new double[] { 3 }, new double[] { 1 })) + ); assertEquals("at least two points in the line is required", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, - () -> validator.validate(new Line(new double[]{3, 4, 500, 3}, new double[]{1, 2, 3, 1}))); + ex = expectThrows( + IllegalArgumentException.class, + () -> validator.validate(new Line(new double[] { 3, 4, 500, 3 }, new double[] { 1, 2, 3, 1 })) + ); assertEquals("invalid longitude 500.0; must be between -180.0 and 180.0", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, - () -> validator.validate(new Line(new double[]{3, 4, 5, 3}, new double[]{1, 100, 3, 1}))); + ex = expectThrows( + IllegalArgumentException.class, + () -> validator.validate(new Line(new double[] { 3, 4, 5, 3 }, new double[] { 1, 100, 3, 1 })) + ); assertEquals("invalid latitude 100.0; must be between -90.0 and 90.0", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, () -> StandardValidator.instance(false).validate( - new Line(new double[]{3, 4}, new double[]{1, 2}, new double[]{6, 5}))); + ex = expectThrows( + IllegalArgumentException.class, + () -> StandardValidator.instance(false).validate(new Line(new double[] { 3, 4 }, new double[] { 1, 2 }, new double[] { 6, 5 })) + ); assertEquals("found Z value [6.0] but [ignore_z_value] parameter is [false]", ex.getMessage()); - StandardValidator.instance(true).validate(new Line(new double[]{3, 4}, new double[]{1, 2}, new double[]{6, 5})); + StandardValidator.instance(true).validate(new Line(new double[] { 3, 4 }, new double[] { 1, 2 }, new double[] { 6, 5 })); } public void testWKTValidation() { - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, - () -> WellKnownText.fromWKT(GeographyValidator.instance(false), randomBoolean(), "linestring (3 1 6, 4 2 5)")); + IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> WellKnownText.fromWKT(GeographyValidator.instance(false), randomBoolean(), "linestring (3 1 6, 4 2 5)") + ); assertEquals("found Z value [6.0] but [ignore_z_value] parameter is [false]", ex.getMessage()); } } diff --git a/libs/geo/src/test/java/org/elasticsearch/geometry/LinearRingTests.java b/libs/geo/src/test/java/org/elasticsearch/geometry/LinearRingTests.java index 179178ed201a4..0ebe257d5aaa3 100644 --- a/libs/geo/src/test/java/org/elasticsearch/geometry/LinearRingTests.java +++ b/libs/geo/src/test/java/org/elasticsearch/geometry/LinearRingTests.java @@ -17,46 +17,61 @@ public class LinearRingTests extends ESTestCase { public void testBasicSerialization() { - UnsupportedOperationException ex = expectThrows(UnsupportedOperationException.class, - () -> WellKnownText.toWKT(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1}))); + UnsupportedOperationException ex = expectThrows( + UnsupportedOperationException.class, + () -> WellKnownText.toWKT(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 })) + ); assertEquals("line ring cannot be serialized using WKT", ex.getMessage()); } public void testInitValidation() { GeometryValidator validator = GeographyValidator.instance(true); - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, - () -> validator.validate(new LinearRing(new double[]{3, 4, 5}, new double[]{1, 2, 3}))); - assertEquals("first and last points of the linear ring must be the same (it must close itself): x[0]=3.0 x[2]=5.0 y[0]=1.0 " + - "y[2]=3.0", - ex.getMessage()); - - ex = expectThrows(IllegalArgumentException.class, - () -> validator.validate(new LinearRing(new double[]{3, 4, 3}, new double[]{1, 2, 1}, new double[]{1, 2, 3}))); - assertEquals("first and last points of the linear ring must be the same (it must close itself): x[0]=3.0 x[2]=3.0 y[0]=1.0 " + - "y[2]=1.0 z[0]=1.0 z[2]=3.0", - ex.getMessage()); - - ex = expectThrows(IllegalArgumentException.class, - () -> validator.validate(new LinearRing(new double[]{3}, new double[]{1}))); + IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> validator.validate(new LinearRing(new double[] { 3, 4, 5 }, new double[] { 1, 2, 3 })) + ); + assertEquals( + "first and last points of the linear ring must be the same (it must close itself): x[0]=3.0 x[2]=5.0 y[0]=1.0 " + "y[2]=3.0", + ex.getMessage() + ); + + ex = expectThrows( + IllegalArgumentException.class, + () -> validator.validate(new LinearRing(new double[] { 3, 4, 3 }, new double[] { 1, 2, 1 }, new double[] { 1, 2, 3 })) + ); + assertEquals( + "first and last points of the linear ring must be the same (it must close itself): x[0]=3.0 x[2]=3.0 y[0]=1.0 " + + "y[2]=1.0 z[0]=1.0 z[2]=3.0", + ex.getMessage() + ); + + ex = expectThrows(IllegalArgumentException.class, () -> validator.validate(new LinearRing(new double[] { 3 }, new double[] { 1 }))); assertEquals("at least two points in the line is required", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, - () -> validator.validate(new LinearRing(new double[]{3, 4, 500, 3}, new double[]{1, 2, 3, 1}))); + ex = expectThrows( + IllegalArgumentException.class, + () -> validator.validate(new LinearRing(new double[] { 3, 4, 500, 3 }, new double[] { 1, 2, 3, 1 })) + ); assertEquals("invalid longitude 500.0; must be between -180.0 and 180.0", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, - () -> validator.validate(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 100, 3, 1}))); + ex = expectThrows( + IllegalArgumentException.class, + () -> validator.validate(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 100, 3, 1 })) + ); assertEquals("invalid latitude 100.0; must be between -90.0 and 90.0", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, () -> StandardValidator.instance(false).validate( - new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1}, new double[]{1, 1, 1, 1}))); + ex = expectThrows( + IllegalArgumentException.class, + () -> StandardValidator.instance(false) + .validate(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }, new double[] { 1, 1, 1, 1 })) + ); assertEquals("found Z value [1.0] but [ignore_z_value] parameter is [false]", ex.getMessage()); - StandardValidator.instance(true).validate( - new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1}, new double[]{1, 1, 1, 1})); + StandardValidator.instance(true) + .validate(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }, new double[] { 1, 1, 1, 1 })); } public void testVisitor() { - BaseGeometryTestCase.testVisitor(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1})); + BaseGeometryTestCase.testVisitor(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 })); } } diff --git a/libs/geo/src/test/java/org/elasticsearch/geometry/MultiLineTests.java b/libs/geo/src/test/java/org/elasticsearch/geometry/MultiLineTests.java index 2a0264adc73ac..b7d47784026dd 100644 --- a/libs/geo/src/test/java/org/elasticsearch/geometry/MultiLineTests.java +++ b/libs/geo/src/test/java/org/elasticsearch/geometry/MultiLineTests.java @@ -34,21 +34,32 @@ protected MultiLine createTestInstance(boolean hasAlt) { public void testBasicSerialization() throws IOException, ParseException { GeometryValidator validator = GeographyValidator.instance(true); - assertEquals("MULTILINESTRING ((3.0 1.0, 4.0 2.0))", WellKnownText.toWKT( - new MultiLine(Collections.singletonList(new Line(new double[]{3, 4}, new double[]{1, 2}))))); - assertEquals(new MultiLine(Collections.singletonList(new Line(new double[]{3, 4}, new double[]{1, 2}))), - WellKnownText.fromWKT(validator, true, "MULTILINESTRING ((3 1, 4 2))")); + assertEquals( + "MULTILINESTRING ((3.0 1.0, 4.0 2.0))", + WellKnownText.toWKT(new MultiLine(Collections.singletonList(new Line(new double[] { 3, 4 }, new double[] { 1, 2 })))) + ); + assertEquals( + new MultiLine(Collections.singletonList(new Line(new double[] { 3, 4 }, new double[] { 1, 2 }))), + WellKnownText.fromWKT(validator, true, "MULTILINESTRING ((3 1, 4 2))") + ); assertEquals("MULTILINESTRING EMPTY", WellKnownText.toWKT(MultiLine.EMPTY)); assertEquals(MultiLine.EMPTY, WellKnownText.fromWKT(validator, true, "MULTILINESTRING EMPTY)")); } public void testValidation() { - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> StandardValidator.instance(false).validate( - new MultiLine(Collections.singletonList(new Line(new double[]{3, 4}, new double[]{1, 2}, new double[]{6, 5}))))); + IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> StandardValidator.instance(false) + .validate( + new MultiLine(Collections.singletonList(new Line(new double[] { 3, 4 }, new double[] { 1, 2 }, new double[] { 6, 5 }))) + ) + ); assertEquals("found Z value [6.0] but [ignore_z_value] parameter is [false]", ex.getMessage()); - StandardValidator.instance(true).validate( - new MultiLine(Collections.singletonList(new Line(new double[]{3, 4}, new double[]{1, 2}, new double[]{6, 5})))); + StandardValidator.instance(true) + .validate( + new MultiLine(Collections.singletonList(new Line(new double[] { 3, 4 }, new double[] { 1, 2 }, new double[] { 6, 5 }))) + ); } } diff --git a/libs/geo/src/test/java/org/elasticsearch/geometry/MultiPointTests.java b/libs/geo/src/test/java/org/elasticsearch/geometry/MultiPointTests.java index 39199a9171bd9..011d29322c9da 100644 --- a/libs/geo/src/test/java/org/elasticsearch/geometry/MultiPointTests.java +++ b/libs/geo/src/test/java/org/elasticsearch/geometry/MultiPointTests.java @@ -35,28 +35,36 @@ protected MultiPoint createTestInstance(boolean hasAlt) { public void testBasicSerialization() throws IOException, ParseException { GeometryValidator validator = GeographyValidator.instance(true); - assertEquals("MULTIPOINT (2.0 1.0)", WellKnownText.toWKT( - new MultiPoint(Collections.singletonList(new Point(2, 1))))); - assertEquals(new MultiPoint(Collections.singletonList(new Point(2, 1))), - WellKnownText.fromWKT(validator, true, "MULTIPOINT (2 1)")); + assertEquals("MULTIPOINT (2.0 1.0)", WellKnownText.toWKT(new MultiPoint(Collections.singletonList(new Point(2, 1))))); + assertEquals( + new MultiPoint(Collections.singletonList(new Point(2, 1))), + WellKnownText.fromWKT(validator, true, "MULTIPOINT (2 1)") + ); - assertEquals("MULTIPOINT (2.0 1.0, 3.0 4.0)", - WellKnownText.toWKT(new MultiPoint(Arrays.asList(new Point(2, 1), new Point(3, 4))))); - assertEquals(new MultiPoint(Arrays.asList(new Point(2, 1), new Point(3, 4))), - WellKnownText.fromWKT(validator, true, "MULTIPOINT (2 1, 3 4)")); + assertEquals("MULTIPOINT (2.0 1.0, 3.0 4.0)", WellKnownText.toWKT(new MultiPoint(Arrays.asList(new Point(2, 1), new Point(3, 4))))); + assertEquals( + new MultiPoint(Arrays.asList(new Point(2, 1), new Point(3, 4))), + WellKnownText.fromWKT(validator, true, "MULTIPOINT (2 1, 3 4)") + ); - assertEquals("MULTIPOINT (2.0 1.0 10.0, 3.0 4.0 20.0)", - WellKnownText.toWKT(new MultiPoint(Arrays.asList(new Point(2, 1, 10), new Point(3, 4, 20))))); - assertEquals(new MultiPoint(Arrays.asList(new Point(2, 1, 10), new Point(3, 4, 20))), - WellKnownText.fromWKT(validator, true, "MULTIPOINT (2 1 10, 3 4 20)")); + assertEquals( + "MULTIPOINT (2.0 1.0 10.0, 3.0 4.0 20.0)", + WellKnownText.toWKT(new MultiPoint(Arrays.asList(new Point(2, 1, 10), new Point(3, 4, 20)))) + ); + assertEquals( + new MultiPoint(Arrays.asList(new Point(2, 1, 10), new Point(3, 4, 20))), + WellKnownText.fromWKT(validator, true, "MULTIPOINT (2 1 10, 3 4 20)") + ); assertEquals("MULTIPOINT EMPTY", WellKnownText.toWKT(MultiPoint.EMPTY)); assertEquals(MultiPoint.EMPTY, WellKnownText.fromWKT(validator, true, "MULTIPOINT EMPTY)")); } public void testValidation() { - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> StandardValidator.instance(false).validate( - new MultiPoint(Collections.singletonList(new Point(2, 1, 3))))); + IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> StandardValidator.instance(false).validate(new MultiPoint(Collections.singletonList(new Point(2, 1, 3)))) + ); assertEquals("found Z value [3.0] but [ignore_z_value] parameter is [false]", ex.getMessage()); StandardValidator.instance(true).validate(new MultiPoint(Collections.singletonList(new Point(2, 1, 3)))); diff --git a/libs/geo/src/test/java/org/elasticsearch/geometry/MultiPolygonTests.java b/libs/geo/src/test/java/org/elasticsearch/geometry/MultiPolygonTests.java index 1c8340b87ba29..bb6046d60960f 100644 --- a/libs/geo/src/test/java/org/elasticsearch/geometry/MultiPolygonTests.java +++ b/libs/geo/src/test/java/org/elasticsearch/geometry/MultiPolygonTests.java @@ -34,26 +34,48 @@ protected MultiPolygon createTestInstance(boolean hasAlt) { public void testBasicSerialization() throws IOException, ParseException { GeometryValidator validator = GeographyValidator.instance(true); - assertEquals("MULTIPOLYGON (((3.0 1.0, 4.0 2.0, 5.0 3.0, 3.0 1.0)))", - WellKnownText.toWKT(new MultiPolygon(Collections.singletonList( - new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1})))))); - assertEquals(new MultiPolygon(Collections.singletonList( - new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1})))), - WellKnownText.fromWKT(validator, true, "MULTIPOLYGON (((3.0 1.0, 4.0 2.0, 5.0 3.0, 3.0 1.0)))")); + assertEquals( + "MULTIPOLYGON (((3.0 1.0, 4.0 2.0, 5.0 3.0, 3.0 1.0)))", + WellKnownText.toWKT( + new MultiPolygon( + Collections.singletonList(new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }))) + ) + ) + ); + assertEquals( + new MultiPolygon( + Collections.singletonList(new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }))) + ), + WellKnownText.fromWKT(validator, true, "MULTIPOLYGON (((3.0 1.0, 4.0 2.0, 5.0 3.0, 3.0 1.0)))") + ); assertEquals("MULTIPOLYGON EMPTY", WellKnownText.toWKT(MultiPolygon.EMPTY)); assertEquals(MultiPolygon.EMPTY, WellKnownText.fromWKT(validator, true, "MULTIPOLYGON EMPTY)")); } public void testValidation() { - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> StandardValidator.instance(false).validate( - new MultiPolygon(Collections.singletonList( - new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1}, new double[]{1, 2, 3, 1})) - )))); + IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> StandardValidator.instance(false) + .validate( + new MultiPolygon( + Collections.singletonList( + new Polygon( + new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }, new double[] { 1, 2, 3, 1 }) + ) + ) + ) + ) + ); assertEquals("found Z value [1.0] but [ignore_z_value] parameter is [false]", ex.getMessage()); - StandardValidator.instance(true).validate( - new MultiPolygon(Collections.singletonList( - new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1}, new double[]{1, 2, 3, 1}))))); + StandardValidator.instance(true) + .validate( + new MultiPolygon( + Collections.singletonList( + new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }, new double[] { 1, 2, 3, 1 })) + ) + ) + ); } } diff --git a/libs/geo/src/test/java/org/elasticsearch/geometry/PointTests.java b/libs/geo/src/test/java/org/elasticsearch/geometry/PointTests.java index aa35f658d48a5..40ae05802ae81 100644 --- a/libs/geo/src/test/java/org/elasticsearch/geometry/PointTests.java +++ b/libs/geo/src/test/java/org/elasticsearch/geometry/PointTests.java @@ -50,8 +50,10 @@ public void testInitValidation() { } public void testWKTValidation() { - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, - () -> WellKnownText.fromWKT(GeographyValidator.instance(false), randomBoolean(), "point (20.0 10.0 100.0)")); + IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> WellKnownText.fromWKT(GeographyValidator.instance(false), randomBoolean(), "point (20.0 10.0 100.0)") + ); assertEquals("found Z value [100.0] but [ignore_z_value] parameter is [false]", ex.getMessage()); } } diff --git a/libs/geo/src/test/java/org/elasticsearch/geometry/PolygonTests.java b/libs/geo/src/test/java/org/elasticsearch/geometry/PolygonTests.java index 0370e61283041..80e371b1f0993 100644 --- a/libs/geo/src/test/java/org/elasticsearch/geometry/PolygonTests.java +++ b/libs/geo/src/test/java/org/elasticsearch/geometry/PolygonTests.java @@ -26,65 +26,110 @@ protected Polygon createTestInstance(boolean hasAlt) { public void testBasicSerialization() throws IOException, ParseException { GeometryValidator validator = GeographyValidator.instance(true); - assertEquals("POLYGON ((3.0 1.0, 4.0 2.0, 5.0 3.0, 3.0 1.0))", - WellKnownText.toWKT(new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1})))); - assertEquals(new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1})), - WellKnownText.fromWKT(validator, true, "POLYGON ((3 1, 4 2, 5 3, 3 1))")); - - assertEquals("POLYGON ((3.0 1.0 5.0, 4.0 2.0 4.0, 5.0 3.0 3.0, 3.0 1.0 5.0))", - WellKnownText.toWKT(new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1}, new double[]{5, 4, 3, 5})))); - assertEquals(new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1}, new double[]{5, 4, 3, 5})), - WellKnownText.fromWKT(validator, true, "POLYGON ((3 1 5, 4 2 4, 5 3 3, 3 1 5))")); + assertEquals( + "POLYGON ((3.0 1.0, 4.0 2.0, 5.0 3.0, 3.0 1.0))", + WellKnownText.toWKT(new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }))) + ); + assertEquals( + new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 })), + WellKnownText.fromWKT(validator, true, "POLYGON ((3 1, 4 2, 5 3, 3 1))") + ); + + assertEquals( + "POLYGON ((3.0 1.0 5.0, 4.0 2.0 4.0, 5.0 3.0 3.0, 3.0 1.0 5.0))", + WellKnownText.toWKT( + new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }, new double[] { 5, 4, 3, 5 })) + ) + ); + assertEquals( + new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }, new double[] { 5, 4, 3, 5 })), + WellKnownText.fromWKT(validator, true, "POLYGON ((3 1 5, 4 2 4, 5 3 3, 3 1 5))") + ); // Auto closing in coerce mode - assertEquals(new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1})), - WellKnownText.fromWKT(validator, true, "POLYGON ((3 1, 4 2, 5 3))")); - assertEquals(new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1}, new double[]{5, 4, 3, 5})), - WellKnownText.fromWKT(validator, true, "POLYGON ((3 1 5, 4 2 4, 5 3 3))")); - assertEquals(new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1}), - Collections.singletonList(new LinearRing(new double[]{0.5, 2.5, 2.0, 0.5}, new double[]{1.5, 1.5, 1.0, 1.5}))), - WellKnownText.fromWKT(validator, true, "POLYGON ((3 1, 4 2, 5 3, 3 1), (0.5 1.5, 2.5 1.5, 2.0 1.0))")); + assertEquals( + new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 })), + WellKnownText.fromWKT(validator, true, "POLYGON ((3 1, 4 2, 5 3))") + ); + assertEquals( + new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }, new double[] { 5, 4, 3, 5 })), + WellKnownText.fromWKT(validator, true, "POLYGON ((3 1 5, 4 2 4, 5 3 3))") + ); + assertEquals( + new Polygon( + new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }), + Collections.singletonList(new LinearRing(new double[] { 0.5, 2.5, 2.0, 0.5 }, new double[] { 1.5, 1.5, 1.0, 1.5 })) + ), + WellKnownText.fromWKT(validator, true, "POLYGON ((3 1, 4 2, 5 3, 3 1), (0.5 1.5, 2.5 1.5, 2.0 1.0))") + ); assertEquals("POLYGON EMPTY", WellKnownText.toWKT(Polygon.EMPTY)); assertEquals(Polygon.EMPTY, WellKnownText.fromWKT(validator, true, "POLYGON EMPTY)")); } public void testInitValidation() { - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, - () -> new Polygon(new LinearRing(new double[]{3, 4, 3}, new double[]{1, 2, 1}))); + IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> new Polygon(new LinearRing(new double[] { 3, 4, 3 }, new double[] { 1, 2, 1 })) + ); assertEquals("at least 4 polygon points required", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, - () -> new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1}), null)); + ex = expectThrows( + IllegalArgumentException.class, + () -> new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }), null) + ); assertEquals("holes must not be null", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, - () -> new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1}, new double[]{5, 4, 3, 5}), - Collections.singletonList(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1})))); + ex = expectThrows( + IllegalArgumentException.class, + () -> new Polygon( + new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }, new double[] { 5, 4, 3, 5 }), + Collections.singletonList(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 })) + ) + ); assertEquals("holes must have the same number of dimensions as the polygon", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, () -> StandardValidator.instance(false).validate( - new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1}, new double[]{1, 2, 3, 1})))); + ex = expectThrows( + IllegalArgumentException.class, + () -> StandardValidator.instance(false) + .validate( + new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }, new double[] { 1, 2, 3, 1 })) + ) + ); assertEquals("found Z value [1.0] but [ignore_z_value] parameter is [false]", ex.getMessage()); - StandardValidator.instance(true).validate( - new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1}, new double[]{1, 2, 3, 1}))); + StandardValidator.instance(true) + .validate(new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }, new double[] { 1, 2, 3, 1 }))); } public void testWKTValidation() { - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, - () -> WellKnownText.fromWKT(GeographyValidator.instance(true), false, "polygon ((3 1 5, 4 2 4, 5 3 3))")); - assertEquals("first and last points of the linear ring must be the same (it must close itself): " + - "x[0]=3.0 x[2]=5.0 y[0]=1.0 y[2]=3.0 z[0]=5.0 z[2]=3.0", ex.getMessage()); - - ex = expectThrows(IllegalArgumentException.class, - () -> WellKnownText.fromWKT(GeographyValidator.instance(false), randomBoolean(), "polygon ((3 1 5, 4 2 4, 5 3 3, 3 1 5))")); + IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> WellKnownText.fromWKT(GeographyValidator.instance(true), false, "polygon ((3 1 5, 4 2 4, 5 3 3))") + ); + assertEquals( + "first and last points of the linear ring must be the same (it must close itself): " + + "x[0]=3.0 x[2]=5.0 y[0]=1.0 y[2]=3.0 z[0]=5.0 z[2]=3.0", + ex.getMessage() + ); + + ex = expectThrows( + IllegalArgumentException.class, + () -> WellKnownText.fromWKT(GeographyValidator.instance(false), randomBoolean(), "polygon ((3 1 5, 4 2 4, 5 3 3, 3 1 5))") + ); assertEquals("found Z value [5.0] but [ignore_z_value] parameter is [false]", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, - () -> WellKnownText.fromWKT(GeographyValidator.instance(randomBoolean()), false, - "polygon ((3 1, 4 2, 5 3, 3 1), (0.5 1.5, 2.5 1.5, 2.0 1.0))")); - assertEquals("first and last points of the linear ring must be the same (it must close itself): " + - "x[0]=0.5 x[2]=2.0 y[0]=1.5 y[2]=1.0", ex.getMessage()); + ex = expectThrows( + IllegalArgumentException.class, + () -> WellKnownText.fromWKT( + GeographyValidator.instance(randomBoolean()), + false, + "polygon ((3 1, 4 2, 5 3, 3 1), (0.5 1.5, 2.5 1.5, 2.0 1.0))" + ) + ); + assertEquals( + "first and last points of the linear ring must be the same (it must close itself): " + "x[0]=0.5 x[2]=2.0 y[0]=1.5 y[2]=1.0", + ex.getMessage() + ); } } diff --git a/libs/geo/src/test/java/org/elasticsearch/geometry/RectangleTests.java b/libs/geo/src/test/java/org/elasticsearch/geometry/RectangleTests.java index 7a442b58dce83..b89ea7fe279c2 100644 --- a/libs/geo/src/test/java/org/elasticsearch/geometry/RectangleTests.java +++ b/libs/geo/src/test/java/org/elasticsearch/geometry/RectangleTests.java @@ -35,24 +35,22 @@ public void testBasicSerialization() throws IOException, ParseException { public void testInitValidation() { GeometryValidator validator = GeographyValidator.instance(true); - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, - () -> validator.validate(new Rectangle(2, 3, 100, 1))); + IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> validator.validate(new Rectangle(2, 3, 100, 1))); assertEquals("invalid latitude 100.0; must be between -90.0 and 90.0", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, - () -> validator.validate(new Rectangle(200, 3, 2, 1))); + ex = expectThrows(IllegalArgumentException.class, () -> validator.validate(new Rectangle(200, 3, 2, 1))); assertEquals("invalid longitude 200.0; must be between -180.0 and 180.0", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, - () -> validator.validate(new Rectangle(2, 3, 1, 2))); + ex = expectThrows(IllegalArgumentException.class, () -> validator.validate(new Rectangle(2, 3, 1, 2))); assertEquals("max y cannot be less than min y", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, - () -> validator.validate(new Rectangle(2, 3, 2, 1, 5, Double.NaN))); + ex = expectThrows(IllegalArgumentException.class, () -> validator.validate(new Rectangle(2, 3, 2, 1, 5, Double.NaN))); assertEquals("only one z value is specified", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, () -> StandardValidator.instance(false).validate( - new Rectangle(50, 10, 40, 30, 20, 60))); + ex = expectThrows( + IllegalArgumentException.class, + () -> StandardValidator.instance(false).validate(new Rectangle(50, 10, 40, 30, 20, 60)) + ); assertEquals("found Z value [20.0] but [ignore_z_value] parameter is [false]", ex.getMessage()); StandardValidator.instance(true).validate(new Rectangle(50, 10, 40, 30, 20, 60)); diff --git a/libs/geo/src/test/java/org/elasticsearch/geometry/utils/GeoHashTests.java b/libs/geo/src/test/java/org/elasticsearch/geometry/utils/GeoHashTests.java index 45e92c850b4c4..615740763e3c2 100644 --- a/libs/geo/src/test/java/org/elasticsearch/geometry/utils/GeoHashTests.java +++ b/libs/geo/src/test/java/org/elasticsearch/geometry/utils/GeoHashTests.java @@ -23,15 +23,12 @@ public class GeoHashTests extends ESTestCase { public void testGeohashAsLongRoutines() { final GeoPoint expected = new GeoPoint(); final GeoPoint actual = new GeoPoint(); - //Ensure that for all points at all supported levels of precision + // Ensure that for all points at all supported levels of precision // that the long encoding of a geohash is compatible with its // String based counterpart - for (double lat=-90;lat<90;lat++) - { - for (double lng=-180;lng<180;lng++) - { - for(int p=1;p<=12;p++) - { + for (double lat = -90; lat < 90; lat++) { + for (double lng = -180; lng < 180; lng++) { + for (int p = 1; p <= 12; p++) { long geoAsLong = Geohash.longEncode(lng, lat, p); // string encode from geohashlong encoded location @@ -85,11 +82,11 @@ public void testLongGeohashes() { // Adding some random geohash characters at the end String extendedGeohash = geohash + randomGeohash(1, 10); GeoPoint actual = GeoPoint.fromGeohash(extendedGeohash); - assertEquals("Additional data points above 12 should be ignored [" + extendedGeohash + "]" , expected, actual); + assertEquals("Additional data points above 12 should be ignored [" + extendedGeohash + "]", expected, actual); Rectangle expectedBbox = Geohash.toBoundingBox(geohash); Rectangle actualBbox = Geohash.toBoundingBox(extendedGeohash); - assertEquals("Additional data points above 12 should be ignored [" + extendedGeohash + "]" , expectedBbox, actualBbox); + assertEquals("Additional data points above 12 should be ignored [" + extendedGeohash + "]", expectedBbox, actualBbox); } } @@ -123,14 +120,11 @@ public void testNeighbors() { assertThat(addNeighbors("r", new ArrayList<>()), containsInAnyOrder("0", "2", "8", "n", "p", "q", "w", "x")); // level1: simple case - assertThat(addNeighbors("dk", new ArrayList<>()), - containsInAnyOrder("d5", "d7", "de", "dh", "dj", "dm", "ds", "dt")); + assertThat(addNeighbors("dk", new ArrayList<>()), containsInAnyOrder("d5", "d7", "de", "dh", "dj", "dm", "ds", "dt")); // Level1: crossing cells - assertThat(addNeighbors("d5", new ArrayList<>()), - containsInAnyOrder("d4", "d6", "d7", "dh", "dk", "9f", "9g", "9u")); - assertThat(addNeighbors("d0", new ArrayList<>()), - containsInAnyOrder("d1", "d2", "d3", "9b", "9c", "6p", "6r", "3z")); + assertThat(addNeighbors("d5", new ArrayList<>()), containsInAnyOrder("d4", "d6", "d7", "dh", "dk", "9f", "9g", "9u")); + assertThat(addNeighbors("d0", new ArrayList<>()), containsInAnyOrder("d1", "d2", "d3", "9b", "9c", "6p", "6r", "3z")); } } diff --git a/libs/grok/src/main/java/org/elasticsearch/grok/Grok.java b/libs/grok/src/main/java/org/elasticsearch/grok/Grok.java index 150e928bd73c4..48a05e6ab3702 100644 --- a/libs/grok/src/main/java/org/elasticsearch/grok/Grok.java +++ b/libs/grok/src/main/java/org/elasticsearch/grok/Grok.java @@ -33,7 +33,7 @@ public final class Grok { - public static final String[] ECS_COMPATIBILITY_MODES = {"disabled", "v1"}; + public static final String[] ECS_COMPATIBILITY_MODES = { "disabled", "v1" }; /** * Patterns built in to the grok library. @@ -45,20 +45,26 @@ public final class Grok { private static final String SUBNAME_GROUP = "subname"; private static final String PATTERN_GROUP = "pattern"; private static final String DEFINITION_GROUP = "definition"; - private static final String GROK_PATTERN = - "%\\{" + - "(?" + - "(?[A-z0-9]+)" + - "(?::(?[[:alnum:]@\\[\\]_:.-]+))?" + - ")" + - "(?:=(?" + - "(?:[^{}]+|\\.+)+" + - ")" + - ")?" + "\\}"; - private static final Regex GROK_PATTERN_REGEX = new Regex(GROK_PATTERN.getBytes(StandardCharsets.UTF_8), 0, - GROK_PATTERN.getBytes(StandardCharsets.UTF_8).length, Option.NONE, UTF8Encoding.INSTANCE, Syntax.DEFAULT); - - private static final int MAX_TO_REGEX_ITERATIONS = 100_000; //sanity limit + private static final String GROK_PATTERN = "%\\{" + + "(?" + + "(?[A-z0-9]+)" + + "(?::(?[[:alnum:]@\\[\\]_:.-]+))?" + + ")" + + "(?:=(?" + + "(?:[^{}]+|\\.+)+" + + ")" + + ")?" + + "\\}"; + private static final Regex GROK_PATTERN_REGEX = new Regex( + GROK_PATTERN.getBytes(StandardCharsets.UTF_8), + 0, + GROK_PATTERN.getBytes(StandardCharsets.UTF_8).length, + Option.NONE, + UTF8Encoding.INSTANCE, + Syntax.DEFAULT + ); + + private static final int MAX_TO_REGEX_ITERATIONS = 100_000; // sanity limit private final Map patternBank; private final boolean namedCaptures; @@ -78,8 +84,13 @@ public Grok(Map patternBank, String grokPattern, MatcherWatchdog this(patternBank, grokPattern, namedCaptures, MatcherWatchdog.noop(), logCallBack); } - private Grok(Map patternBank, String grokPattern, boolean namedCaptures, MatcherWatchdog matcherWatchdog, - Consumer logCallBack) { + private Grok( + Map patternBank, + String grokPattern, + boolean namedCaptures, + MatcherWatchdog matcherWatchdog, + Consumer logCallBack + ) { this.patternBank = patternBank; this.namedCaptures = namedCaptures; this.matcherWatchdog = matcherWatchdog; @@ -88,8 +99,14 @@ private Grok(Map patternBank, String grokPattern, boolean namedC String expression = toRegex(grokPattern); byte[] expressionBytes = expression.getBytes(StandardCharsets.UTF_8); - this.compiledExpression = new Regex(expressionBytes, 0, expressionBytes.length, Option.DEFAULT, UTF8Encoding.INSTANCE, - message -> logCallBack.accept(message)); + this.compiledExpression = new Regex( + expressionBytes, + 0, + expressionBytes.length, + Option.DEFAULT, + UTF8Encoding.INSTANCE, + message -> logCallBack.accept(message) + ); List grokCaptureConfigs = new ArrayList<>(); for (Iterator entry = compiledExpression.namedBackrefIterator(); entry.hasNext();) { @@ -130,8 +147,13 @@ private void innerForbidCircularReferences(String patternName, List path if (path.isEmpty()) { message = "circular reference in pattern [" + patternName + "][" + pattern + "]"; } else { - message = "circular reference in pattern [" + path.remove(path.size() - 1) + "][" + pattern + - "] back to pattern [" + patternName + "]"; + message = "circular reference in pattern [" + + path.remove(path.size() - 1) + + "][" + + pattern + + "] back to pattern [" + + patternName + + "]"; // add rest of the path: if (path.isEmpty() == false) { message += " via patterns [" + String.join("=>", path) + "]"; @@ -166,8 +188,12 @@ private static boolean patternReferencesItself(String pattern, String patternNam } private String groupMatch(String name, Region region, String pattern) { - int number = GROK_PATTERN_REGEX.nameToBackrefNumber(name.getBytes(StandardCharsets.UTF_8), 0, - name.getBytes(StandardCharsets.UTF_8).length, region); + int number = GROK_PATTERN_REGEX.nameToBackrefNumber( + name.getBytes(StandardCharsets.UTF_8), + 0, + name.getBytes(StandardCharsets.UTF_8).length, + region + ); int begin = region.beg[number]; int end = region.end[number]; if (begin < 0) { // no match found @@ -281,8 +307,9 @@ public boolean match(byte[] utf8Bytes, int offset, int length, GrokCaptureExtrac matcherWatchdog.unregister(matcher); } if (result == Matcher.INTERRUPTED) { - throw new RuntimeException("grok pattern matching was interrupted after [" + - matcherWatchdog.maxExecutionTimeInMillis() + "] ms"); + throw new RuntimeException( + "grok pattern matching was interrupted after [" + matcherWatchdog.maxExecutionTimeInMillis() + "] ms" + ); } if (result == Matcher.FAILED) { return false; @@ -329,15 +356,50 @@ public static boolean isValidEcsCompatibilityMode(String ecsCompatibility) { private static Map loadPatterns(boolean ecsCompatibility) { String[] legacyPatternNames = { - "aws", "bacula", "bind", "bro", "exim", "firewalls", "grok-patterns", "haproxy", - "httpd", "java", "junos", "linux-syslog", "maven", "mcollective-patterns", "mongodb", "nagios", - "postgresql", "rails", "redis", "ruby", "squid" - }; + "aws", + "bacula", + "bind", + "bro", + "exim", + "firewalls", + "grok-patterns", + "haproxy", + "httpd", + "java", + "junos", + "linux-syslog", + "maven", + "mcollective-patterns", + "mongodb", + "nagios", + "postgresql", + "rails", + "redis", + "ruby", + "squid" }; String[] ecsPatternNames = { - "aws", "bacula", "bind", "bro", "exim", "firewalls", "grok-patterns", "haproxy", - "httpd", "java", "junos", "linux-syslog", "maven", "mcollective", "mongodb", "nagios", - "postgresql", "rails", "redis", "ruby", "squid", "zeek" - }; + "aws", + "bacula", + "bind", + "bro", + "exim", + "firewalls", + "grok-patterns", + "haproxy", + "httpd", + "java", + "junos", + "linux-syslog", + "maven", + "mcollective", + "mongodb", + "nagios", + "postgresql", + "rails", + "redis", + "ruby", + "squid", + "zeek" }; String[] patternNames = ecsCompatibility ? ecsPatternNames : legacyPatternNames; String directory = ecsCompatibility ? "/patterns/ecs-v1/" : "/patterns/legacy/"; @@ -372,4 +434,3 @@ private static void loadPatterns(Map patternBank, InputStream in } } - diff --git a/libs/grok/src/main/java/org/elasticsearch/grok/MatcherWatchdog.java b/libs/grok/src/main/java/org/elasticsearch/grok/MatcherWatchdog.java index ece8857032895..f0c70a07227c2 100644 --- a/libs/grok/src/main/java/org/elasticsearch/grok/MatcherWatchdog.java +++ b/libs/grok/src/main/java/org/elasticsearch/grok/MatcherWatchdog.java @@ -59,10 +59,12 @@ public interface MatcherWatchdog { * @param relativeTimeSupplier A supplier that returns relative time * @param scheduler A scheduler that is able to execute a command for each fixed interval */ - static MatcherWatchdog newInstance(long interval, - long maxExecutionTime, - LongSupplier relativeTimeSupplier, - BiConsumer scheduler) { + static MatcherWatchdog newInstance( + long interval, + long maxExecutionTime, + LongSupplier relativeTimeSupplier, + BiConsumer scheduler + ) { return new Default(interval, maxExecutionTime, relativeTimeSupplier, scheduler); } @@ -77,12 +79,10 @@ class Noop implements MatcherWatchdog { private static final Noop INSTANCE = new Noop(); - private Noop() { - } + private Noop() {} @Override - public void register(Matcher matcher) { - } + public void register(Matcher matcher) {} @Override public long maxExecutionTimeInMillis() { @@ -90,8 +90,7 @@ public long maxExecutionTimeInMillis() { } @Override - public void unregister(Matcher matcher) { - } + public void unregister(Matcher matcher) {} } class Default implements MatcherWatchdog { @@ -104,10 +103,7 @@ class Default implements MatcherWatchdog { private final AtomicBoolean running = new AtomicBoolean(false); final ConcurrentHashMap registry = new ConcurrentHashMap<>(); - private Default(long interval, - long maxExecutionTime, - LongSupplier relativeTimeSupplier, - BiConsumer scheduler) { + private Default(long interval, long maxExecutionTime, LongSupplier relativeTimeSupplier, BiConsumer scheduler) { this.interval = interval; this.maxExecutionTime = maxExecutionTime; this.relativeTimeSupplier = relativeTimeSupplier; diff --git a/libs/grok/src/test/java/org/elasticsearch/grok/GrokTests.java b/libs/grok/src/test/java/org/elasticsearch/grok/GrokTests.java index 41a56ab5b86f9..89b1c8390ce25 100644 --- a/libs/grok/src/test/java/org/elasticsearch/grok/GrokTests.java +++ b/libs/grok/src/test/java/org/elasticsearch/grok/GrokTests.java @@ -41,7 +41,6 @@ import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.nullValue; - public class GrokTests extends ESTestCase { public void testMatchWithoutCaptures() { @@ -154,14 +153,14 @@ private void testSimpleSyslogLine( assertEquals(pid.v2(), matches.get(pid.v1().getKey())); String[] logsource = new String[1]; - GrokCaptureExtracter logsourceExtracter = - namedConfig(grok, logSource.v1().getKey()) - .nativeExtracter(new ThrowingNativeExtracterMap() { - @Override - public GrokCaptureExtracter forString(Function, GrokCaptureExtracter> buildExtracter) { - return buildExtracter.apply(str -> logsource[0] = str); - } - }); + GrokCaptureExtracter logsourceExtracter = namedConfig(grok, logSource.v1().getKey()).nativeExtracter( + new ThrowingNativeExtracterMap() { + @Override + public GrokCaptureExtracter forString(Function, GrokCaptureExtracter> buildExtracter) { + return buildExtracter.apply(str -> logsource[0] = str); + } + } + ); assertThat(specificCapture(grok, line, logsourceExtracter), is(true)); assertThat(logsource[0], equalTo(logSource.v2())); } @@ -212,8 +211,8 @@ private void testSyslog5424Line( Tuple, Object> ts, Tuple, Object> ver ) { - String line = "<191>1 2009-06-30T18:30:00+02:00 paxton.local grokdebug 4123 - [id1 foo=\\\"bar\\\"][id2 baz=\\\"something\\\"] " + - "Hello, syslog."; + String line = "<191>1 2009-06-30T18:30:00+02:00 paxton.local grokdebug 4123 - [id1 foo=\\\"bar\\\"][id2 baz=\\\"something\\\"] " + + "Hello, syslog."; Grok grok = new Grok(Grok.getBuiltinPatterns(ecsCompatibility), "%{SYSLOG5424LINE}", logger::warn); assertCaptureConfig( grok, @@ -280,9 +279,10 @@ public void testUnicodeSyslog() { private void testUnicodeSyslog(boolean ecsCompatibility) { Grok grok = new Grok( Grok.getBuiltinPatterns(ecsCompatibility), - "<%{POSINT:syslog_pri}>%{SPACE}%{SYSLOGTIMESTAMP:syslog_timestamp} " + - "%{SYSLOGHOST:syslog_hostname} %{PROG:syslog_program}(:?)(?:\\[%{GREEDYDATA:syslog_pid}\\])?(:?) " + - "%{GREEDYDATA:syslog_message}", logger::warn + "<%{POSINT:syslog_pri}>%{SPACE}%{SYSLOGTIMESTAMP:syslog_timestamp} " + + "%{SYSLOGHOST:syslog_hostname} %{PROG:syslog_program}(:?)(?:\\[%{GREEDYDATA:syslog_pid}\\])?(:?) " + + "%{GREEDYDATA:syslog_message}", + logger::warn ); assertCaptureConfig( grok, @@ -295,9 +295,11 @@ private void testUnicodeSyslog(boolean ecsCompatibility) { Map.entry("syslog_timestamp", STRING) ) ); - Map matches = grok.captures("<22>Jan 4 07:50:46 mailmaster postfix/policy-spf[9454]: : " + - "SPF permerror (Junk encountered in record 'v=spf1 mx a:mail.domain.no ip4:192.168.0.4 �all'): Envelope-from: " + - "email@domain.no"); + Map matches = grok.captures( + "<22>Jan 4 07:50:46 mailmaster postfix/policy-spf[9454]: : " + + "SPF permerror (Junk encountered in record 'v=spf1 mx a:mail.domain.no ip4:192.168.0.4 �all'): Envelope-from: " + + "email@domain.no" + ); assertThat(matches.get("syslog_pri"), equalTo("22")); assertThat(matches.get("syslog_program"), equalTo("postfix/policy-spf")); assertThat(matches.get("tags"), nullValue()); @@ -336,21 +338,21 @@ private void testISO8601(boolean ecsCompatibility) { Grok grok = new Grok(Grok.getBuiltinPatterns(ecsCompatibility), "^%{TIMESTAMP_ISO8601}$", logger::warn); assertCaptureConfig(grok, Map.of()); List timeMessages = Arrays.asList( - "2001-01-01T00:00:00", - "1974-03-02T04:09:09", - "2010-05-03T08:18:18+00:00", - "2004-07-04T12:27:27-00:00", - "2001-09-05T16:36:36+0000", - "2001-11-06T20:45:45-0000", - "2001-12-07T23:54:54Z", - "2001-01-01T00:00:00.123456", - "1974-03-02T04:09:09.123456", - "2010-05-03T08:18:18.123456+00:00", - "2004-07-04T12:27:27.123456-00:00", - "2001-09-05T16:36:36.123456+0000", - "2001-11-06T20:45:45.123456-0000", - "2001-12-07T23:54:54.123456Z", - "2001-12-07T23:54:60.123456Z" // '60' second is a leap second. + "2001-01-01T00:00:00", + "1974-03-02T04:09:09", + "2010-05-03T08:18:18+00:00", + "2004-07-04T12:27:27-00:00", + "2001-09-05T16:36:36+0000", + "2001-11-06T20:45:45-0000", + "2001-12-07T23:54:54Z", + "2001-01-01T00:00:00.123456", + "1974-03-02T04:09:09.123456", + "2010-05-03T08:18:18.123456+00:00", + "2004-07-04T12:27:27.123456-00:00", + "2001-09-05T16:36:36.123456+0000", + "2001-11-06T20:45:45.123456-0000", + "2001-12-07T23:54:54.123456Z", + "2001-12-07T23:54:60.123456Z" // '60' second is a leap second. ); for (String msg : timeMessages) { assertThat(grok.match(msg), is(true)); @@ -366,27 +368,27 @@ private void testNotISO8601(boolean ecsCompatibility, List additionalCas Grok grok = new Grok(Grok.getBuiltinPatterns(ecsCompatibility), "^%{TIMESTAMP_ISO8601}$", logger::warn); assertCaptureConfig(grok, Map.of()); List timeMessages = Arrays.asList( - "2001-13-01T00:00:00", // invalid month - "2001-00-01T00:00:00", // invalid month - "2001-01-00T00:00:00", // invalid day - "2001-01-32T00:00:00", // invalid day - "2001-01-aT00:00:00", // invalid day - "2001-01-1aT00:00:00", // invalid day - "2001-01-01Ta0:00:00", // invalid hour - "2001-01-01T25:00:00", // invalid hour - "2001-01-01T01:60:00", // invalid minute - "2001-01-01T00:aa:00", // invalid minute - "2001-01-01T00:00:aa", // invalid second - "2001-01-01T00:00:-1", // invalid second - "2001-01-01T00:00:61", // invalid second - "2001-01-01T00:00:00A", // invalid timezone - "2001-01-01T00:00:00+", // invalid timezone - "2001-01-01T00:00:00+25", // invalid timezone - "2001-01-01T00:00:00+2500", // invalid timezone - "2001-01-01T00:00:00+25:00", // invalid timezone - "2001-01-01T00:00:00-25", // invalid timezone - "2001-01-01T00:00:00-2500", // invalid timezone - "2001-01-01T00:00:00-00:61" // invalid timezone + "2001-13-01T00:00:00", // invalid month + "2001-00-01T00:00:00", // invalid month + "2001-01-00T00:00:00", // invalid day + "2001-01-32T00:00:00", // invalid day + "2001-01-aT00:00:00", // invalid day + "2001-01-1aT00:00:00", // invalid day + "2001-01-01Ta0:00:00", // invalid hour + "2001-01-01T25:00:00", // invalid hour + "2001-01-01T01:60:00", // invalid minute + "2001-01-01T00:aa:00", // invalid minute + "2001-01-01T00:00:aa", // invalid second + "2001-01-01T00:00:-1", // invalid second + "2001-01-01T00:00:61", // invalid second + "2001-01-01T00:00:00A", // invalid timezone + "2001-01-01T00:00:00+", // invalid timezone + "2001-01-01T00:00:00+25", // invalid timezone + "2001-01-01T00:00:00+2500", // invalid timezone + "2001-01-01T00:00:00+25:00", // invalid timezone + "2001-01-01T00:00:00-25", // invalid timezone + "2001-01-01T00:00:00-2500", // invalid timezone + "2001-01-01T00:00:00-00:61" // invalid timezone ); List timesToTest = new ArrayList<>(timeMessages); timesToTest.addAll(additionalCases); @@ -558,7 +560,7 @@ public GrokCaptureExtracter forLong(Function double[] rating = new double[1]; GrokCaptureExtracter ratingExtracter = namedConfig(g, "rating").nativeExtracter(new ThrowingNativeExtracterMap() { - public GrokCaptureExtracter forDouble(java.util.function.Function buildExtracter) { + public GrokCaptureExtracter forDouble(java.util.function.Function buildExtracter) { return buildExtracter.apply(d -> rating[0] = d); } }); @@ -596,8 +598,8 @@ public void testGarbageTypeNameBecomesString() { } public void testApacheLog() { - final String agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.12785 " + - "YaBrowser/13.12.1599.12785 Safari/537.36"; + final String agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.12785 " + + "YaBrowser/13.12.1599.12785 Safari/537.36"; final String clientIp = "31.184.238.164"; final String timestamp = "24/Jul/2014:05:35:37 +0530"; final String verb = "GET"; @@ -652,9 +654,9 @@ public void testApacheLog( Tuple, Object> verb, List, Object>> additionalFields ) { - String logLine = "31.184.238.164 - - [24/Jul/2014:05:35:37 +0530] \"GET /logs/access.log HTTP/1.0\" 200 69849 " + - "\"http://8rursodiol.enjin.com\" \"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) " + - "Chrome/30.0.1599.12785 YaBrowser/13.12.1599.12785 Safari/537.36\" \"www.dlwindianrailways.com\""; + String logLine = "31.184.238.164 - - [24/Jul/2014:05:35:37 +0530] \"GET /logs/access.log HTTP/1.0\" 200 69849 " + + "\"http://8rursodiol.enjin.com\" \"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) " + + "Chrome/30.0.1599.12785 YaBrowser/13.12.1599.12785 Safari/537.36\" \"www.dlwindianrailways.com\""; Grok grok = new Grok(Grok.getBuiltinPatterns(ecsCompatibility), "%{COMBINEDAPACHELOG}", logger::warn); Map captureTypes = new HashMap<>(); @@ -696,8 +698,11 @@ public void testApacheLog( public void testComplete() { Map bank = new HashMap<>(); bank.put("MONTHDAY", "(?:(?:0[1-9])|(?:[12][0-9])|(?:3[01])|[1-9])"); - bank.put("MONTH", "\\b(?:Jan(?:uary|uar)?|Feb(?:ruary|ruar)?|M(?:a|ä)?r(?:ch|z)?|Apr(?:il)?|Ma(?:y|i)?|Jun(?:e|i)" + - "?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|O(?:c|k)?t(?:ober)?|Nov(?:ember)?|De(?:c|z)(?:ember)?)\\b"); + bank.put( + "MONTH", + "\\b(?:Jan(?:uary|uar)?|Feb(?:ruary|ruar)?|M(?:a|ä)?r(?:ch|z)?|Apr(?:il)?|Ma(?:y|i)?|Jun(?:e|i)" + + "?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|O(?:c|k)?t(?:ober)?|Nov(?:ember)?|De(?:c|z)(?:ember)?)\\b" + ); bank.put("MINUTE", "(?:[0-5][0-9])"); bank.put("YEAR", "(?>\\d\\d){1,2}"); bank.put("HOUR", "(?:2[0123]|[01]?[0-9])"); @@ -708,19 +713,25 @@ public void testComplete() { bank.put("WORD", "\\b\\w+\\b"); bank.put("BASE10NUM", "(?[+-]?(?:(?:[0-9]+(?:\\.[0-9]+)?)|(?:\\.[0-9]+)))"); bank.put("NUMBER", "(?:%{BASE10NUM})"); - bank.put("IPV6", "((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]" + - "\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4})" + - "{1,2})|:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:)" + - "{4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\" + - "d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]" + - "\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4})" + - "{1,5})" + - "|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))" + - "|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)" + - "(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}" + - ":((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:)))(%.+)?"); - bank.put("IPV4", "(?(?\"(?>\\\\.|[^\\\\\"]+)+\"|\"\"|(?>'(?>\\\\.|[^\\\\']+)+')|''|(?>`(?>\\\\.|[^\\\\`]+)+`)|``))"); - String text = "83.149.9.216 - - [19/Jul/2015:08:13:42 +0000] \"GET /presentations/logstash-monitorama-2013/images/" + - "kibana-dashboard3.png HTTP/1.1\" 200 171717 \"http://semicomplete.com/presentations/logstash-monitorama-2013/\" " + - "\"Mozilla" + - "/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.77 Safari/537.36\""; - String pattern = "%{IPORHOST:clientip} %{USER:ident} %{USER:auth} \\[%{HTTPDATE:timestamp}\\] \"%{WORD:verb} %{DATA:request} " + - "HTTP/%{NUMBER:httpversion}\" %{NUMBER:response:int} (?:-|%{NUMBER:bytes:int}) %{QS:referrer} %{QS:agent}"; + String text = "83.149.9.216 - - [19/Jul/2015:08:13:42 +0000] \"GET /presentations/logstash-monitorama-2013/images/" + + "kibana-dashboard3.png HTTP/1.1\" 200 171717 \"http://semicomplete.com/presentations/logstash-monitorama-2013/\" " + + "\"Mozilla" + + "/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.77 Safari/537.36\""; + String pattern = "%{IPORHOST:clientip} %{USER:ident} %{USER:auth} \\[%{HTTPDATE:timestamp}\\] \"%{WORD:verb} %{DATA:request} " + + "HTTP/%{NUMBER:httpversion}\" %{NUMBER:response:int} (?:-|%{NUMBER:bytes:int}) %{QS:referrer} %{QS:agent}"; Grok grok = new Grok(bank, pattern, logger::warn); assertCaptureConfig( @@ -764,8 +775,11 @@ public void testComplete() { expected.put("response", 200); expected.put("bytes", 171717); expected.put("referrer", "\"http://semicomplete.com/presentations/logstash-monitorama-2013/\""); - expected.put("agent", "\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) " + - "Chrome/32.0.1700.77 Safari/537.36\""); + expected.put( + "agent", + "\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) " + + "Chrome/32.0.1700.77 Safari/537.36\"" + ); Map actual = grok.captures(text); @@ -798,10 +812,10 @@ public void testExponentialExpressions() { private void testExponentialExpressions(boolean ecsCompatibility) { AtomicBoolean run = new AtomicBoolean(true); // to avoid a lingering thread when test has completed - String grokPattern = "Bonsuche mit folgender Anfrage: Belegart->\\[%{WORD:param2},(?(\\s*%{NOTSPACE})*)\\] " + - "Zustand->ABGESCHLOSSEN Kassennummer->%{WORD:param9} Bonnummer->%{WORD:param10} Datum->%{DATESTAMP_OTHER:param11}"; - String logLine = "Bonsuche mit folgender Anfrage: Belegart->[EINGESCHRAENKTER_VERKAUF, VERKAUF, NACHERFASSUNG] " + - "Zustand->ABGESCHLOSSEN Kassennummer->2 Bonnummer->6362 Datum->Mon Jan 08 00:00:00 UTC 2018"; + String grokPattern = "Bonsuche mit folgender Anfrage: Belegart->\\[%{WORD:param2},(?(\\s*%{NOTSPACE})*)\\] " + + "Zustand->ABGESCHLOSSEN Kassennummer->%{WORD:param9} Bonnummer->%{WORD:param10} Datum->%{DATESTAMP_OTHER:param11}"; + String logLine = "Bonsuche mit folgender Anfrage: Belegart->[EINGESCHRAENKTER_VERKAUF, VERKAUF, NACHERFASSUNG] " + + "Zustand->ABGESCHLOSSEN Kassennummer->2 Bonnummer->6362 Datum->Mon Jan 08 00:00:00 UTC 2018"; BiConsumer scheduler = (delay, command) -> { try { Thread.sleep(delay); @@ -899,14 +913,14 @@ private void testLogCallBack(boolean ecsCompatibility) { AtomicReference message = new AtomicReference<>(); Grok grok = new Grok(Grok.getBuiltinPatterns(ecsCompatibility), ".*\\[.*%{SPACE}*\\].*", message::set); grok.match("[foo]"); - //this message comes from Joni, so updates to Joni may change the expectation + // this message comes from Joni, so updates to Joni may change the expectation assertThat(message.get(), containsString("regular expression has redundant nested repeat operator")); } private void assertGrokedField(String fieldName) { String line = "foo"; // test both with and without ECS compatibility - for (boolean ecsCompatibility : new boolean[]{false, true}) { + for (boolean ecsCompatibility : new boolean[] { false, true }) { Grok grok = new Grok(Grok.getBuiltinPatterns(ecsCompatibility), "%{WORD:" + fieldName + "}", logger::warn); Map matches = grok.captures(line); assertEquals(line, matches.get(fieldName)); diff --git a/libs/grok/src/test/java/org/elasticsearch/grok/MatcherWatchdogTests.java b/libs/grok/src/test/java/org/elasticsearch/grok/MatcherWatchdogTests.java index 1a21f7fc8a00c..c78412d176c15 100644 --- a/libs/grok/src/test/java/org/elasticsearch/grok/MatcherWatchdogTests.java +++ b/libs/grok/src/test/java/org/elasticsearch/grok/MatcherWatchdogTests.java @@ -7,14 +7,15 @@ */ package org.elasticsearch.grok; +import org.elasticsearch.test.ESTestCase; +import org.joni.Matcher; +import org.mockito.Mockito; + import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; -import org.elasticsearch.test.ESTestCase; -import org.joni.Matcher; -import org.mockito.Mockito; import static org.hamcrest.Matchers.is; import static org.mockito.ArgumentMatchers.any; @@ -53,7 +54,8 @@ public void testInterrupt() throws Exception { watchdog.register(matcher); verify(matcher, timeout(9999).atLeastOnce()).interrupt(); interrupted.set(true); - while (run.get()) {} // wait here so that the size of the registry can be asserted + while (run.get()) { + } // wait here so that the size of the registry can be asserted watchdog.unregister(matcher); }); thread.start(); @@ -62,16 +64,18 @@ public void testInterrupt() throws Exception { assertThat(registry.size(), is(1)); }); run.set(false); - assertBusy(() -> { - assertThat(registry.size(), is(0)); - }); + assertBusy(() -> { assertThat(registry.size(), is(0)); }); } public void testIdleIfNothingRegistered() throws Exception { long interval = 1L; ScheduledExecutorService threadPool = mock(ScheduledExecutorService.class); - MatcherWatchdog watchdog = MatcherWatchdog.newInstance(interval, Long.MAX_VALUE, System::currentTimeMillis, - (delay, command) -> threadPool.schedule(command, delay, TimeUnit.MILLISECONDS)); + MatcherWatchdog watchdog = MatcherWatchdog.newInstance( + interval, + Long.MAX_VALUE, + System::currentTimeMillis, + (delay, command) -> threadPool.schedule(command, delay, TimeUnit.MILLISECONDS) + ); // Periodic action is not scheduled because no thread is registered verifyZeroInteractions(threadPool); CompletableFuture commandFuture = new CompletableFuture<>(); @@ -79,9 +83,7 @@ public void testIdleIfNothingRegistered() throws Exception { doAnswer(invocationOnMock -> { commandFuture.complete((Runnable) invocationOnMock.getArguments()[0]); return null; - }).when(threadPool).schedule( - any(Runnable.class), eq(interval), eq(TimeUnit.MILLISECONDS) - ); + }).when(threadPool).schedule(any(Runnable.class), eq(interval), eq(TimeUnit.MILLISECONDS)); Matcher matcher = mock(Matcher.class); watchdog.register(matcher); // Registering the first thread should have caused the command to get scheduled again diff --git a/libs/lz4/src/main/java/org/elasticsearch/lz4/ESLZ4Compressor.java b/libs/lz4/src/main/java/org/elasticsearch/lz4/ESLZ4Compressor.java index bdb06fa3b7ae7..417b596fb5b17 100644 --- a/libs/lz4/src/main/java/org/elasticsearch/lz4/ESLZ4Compressor.java +++ b/libs/lz4/src/main/java/org/elasticsearch/lz4/ESLZ4Compressor.java @@ -38,8 +38,7 @@ public class ESLZ4Compressor extends LZ4Compressor { public static final LZ4Compressor INSTANCE = new ESLZ4Compressor(); - ESLZ4Compressor() { - } + ESLZ4Compressor() {} static int compress64k(byte[] src, int srcOff, int srcLen, byte[] dest, int destOff, int destEnd) { int srcEnd = srcOff + srcLen; @@ -53,8 +52,7 @@ static int compress64k(byte[] src, int srcOff, int srcLen, byte[] dest, int dest Arrays.fill(hashTable, (short) 0); int sOff = srcOff + 1; - label53: - while(true) { + label53: while (true) { int forwardOff = sOff; int step = 1; int var16 = 1 << LZ4Constants.SKIP_STRENGTH; @@ -73,7 +71,7 @@ static int compress64k(byte[] src, int srcOff, int srcLen, byte[] dest, int dest ref = srcOff + SafeUtils.readShort(hashTable, excess); SafeUtils.writeShort(hashTable, excess, sOff - srcOff); // Modified to use explicit == false - } while(LZ4SafeUtils.readIntEquals(src, ref, sOff) == false); + } while (LZ4SafeUtils.readIntEquals(src, ref, sOff) == false); excess = LZ4SafeUtils.commonBytesBackward(src, ref, sOff, srcOff, anchor); sOff -= excess; @@ -94,8 +92,8 @@ static int compress64k(byte[] src, int srcOff, int srcLen, byte[] dest, int dest LZ4SafeUtils.wildArraycopy(src, anchor, dest, dOff, runLen); dOff += runLen; - while(true) { - SafeUtils.writeShortLE(dest, dOff, (short)(sOff - ref)); + while (true) { + SafeUtils.writeShortLE(dest, dOff, (short) (sOff - ref)); dOff += 2; sOff += 4; ref += 4; @@ -154,13 +152,12 @@ public int compress(byte[] src, int srcOff, int srcLen, byte[] dest, int destOff int[] hashTable = biggerHashTable.get(); Arrays.fill(hashTable, srcOff); - label63: - while(true) { + label63: while (true) { int forwardOff = sOff; int step = 1; int var18 = 1 << LZ4Constants.SKIP_STRENGTH; - while(true) { + while (true) { sOff = forwardOff; forwardOff += step; step = var18++ >>> LZ4Constants.SKIP_STRENGTH; @@ -193,7 +190,7 @@ public int compress(byte[] src, int srcOff, int srcLen, byte[] dest, int destOff LZ4SafeUtils.wildArraycopy(src, anchor, dest, dOff, runLen); dOff += runLen; - while(true) { + while (true) { SafeUtils.writeShortLE(dest, dOff, back); dOff += 2; sOff += 4; diff --git a/libs/lz4/src/main/java/org/elasticsearch/lz4/ESLZ4Decompressor.java b/libs/lz4/src/main/java/org/elasticsearch/lz4/ESLZ4Decompressor.java index ef887a9c5ae05..92fe095ae2369 100644 --- a/libs/lz4/src/main/java/org/elasticsearch/lz4/ESLZ4Decompressor.java +++ b/libs/lz4/src/main/java/org/elasticsearch/lz4/ESLZ4Decompressor.java @@ -31,8 +31,7 @@ public class ESLZ4Decompressor extends LZ4FastDecompressor { public static final LZ4FastDecompressor INSTANCE = new ESLZ4Decompressor(); - ESLZ4Decompressor() { - } + ESLZ4Decompressor() {} public int decompress(byte[] src, int srcOff, byte[] dest, int destOff, int destLen) { SafeUtils.checkRange(src, srcOff); @@ -48,13 +47,13 @@ public int decompress(byte[] src, int srcOff, byte[] dest, int destOff, int dest int sOff = srcOff; int dOff = destOff; - while(true) { + while (true) { int token = SafeUtils.readByte(src, sOff) & 255; ++sOff; int literalLen = token >>> 4; if (literalLen == 15) { byte len; - for(boolean var11 = true; (len = SafeUtils.readByte(src, sOff++)) == -1; literalLen += 255) { + for (boolean var11 = true; (len = SafeUtils.readByte(src, sOff++)) == -1; literalLen += 255) { } literalLen += len & 255; @@ -83,7 +82,7 @@ public int decompress(byte[] src, int srcOff, byte[] dest, int destOff, int dest int matchLen = token & 15; if (matchLen == 15) { byte len; - for(boolean var15 = true; (len = SafeUtils.readByte(src, sOff++)) == -1; matchLen += 255) { + for (boolean var15 = true; (len = SafeUtils.readByte(src, sOff++)) == -1; matchLen += 255) { } matchLen += len & 255; diff --git a/libs/lz4/src/main/java/org/elasticsearch/lz4/LZ4Constants.java b/libs/lz4/src/main/java/org/elasticsearch/lz4/LZ4Constants.java index b1b7713472de8..68a0851d969aa 100644 --- a/libs/lz4/src/main/java/org/elasticsearch/lz4/LZ4Constants.java +++ b/libs/lz4/src/main/java/org/elasticsearch/lz4/LZ4Constants.java @@ -27,8 +27,8 @@ enum LZ4Constants { ; - static final int DEFAULT_COMPRESSION_LEVEL = 8+1; - static final int MAX_COMPRESSION_LEVEL = 16+1; + static final int DEFAULT_COMPRESSION_LEVEL = 8 + 1; + static final int MAX_COMPRESSION_LEVEL = 16 + 1; static final int MEMORY_USAGE = 14; static final int NOT_COMPRESSIBLE_DETECTION_LEVEL = 6; diff --git a/libs/lz4/src/main/java/org/elasticsearch/lz4/SafeUtils.java b/libs/lz4/src/main/java/org/elasticsearch/lz4/SafeUtils.java index 242948f3ad395..657cd941fecdf 100644 --- a/libs/lz4/src/main/java/org/elasticsearch/lz4/SafeUtils.java +++ b/libs/lz4/src/main/java/org/elasticsearch/lz4/SafeUtils.java @@ -71,8 +71,8 @@ public static int readInt(byte[] buf, int i) { // Unused in forked instance, no need to optimize public static long readLongLE(byte[] buf, int i) { - return (buf[i] & 0xFFL) | ((buf[i+1] & 0xFFL) << 8) | ((buf[i+2] & 0xFFL) << 16) | ((buf[i+3] & 0xFFL) << 24) - | ((buf[i+4] & 0xFFL) << 32) | ((buf[i+5] & 0xFFL) << 40) | ((buf[i+6] & 0xFFL) << 48) | ((buf[i+7] & 0xFFL) << 56); + return (buf[i] & 0xFFL) | ((buf[i + 1] & 0xFFL) << 8) | ((buf[i + 2] & 0xFFL) << 16) | ((buf[i + 3] & 0xFFL) << 24) | ((buf[i + 4] + & 0xFFL) << 32) | ((buf[i + 5] & 0xFFL) << 40) | ((buf[i + 6] & 0xFFL) << 48) | ((buf[i + 7] & 0xFFL) << 56); } // Modified to use VarHandle diff --git a/libs/lz4/src/test/java/org/elasticsearch/lz4/AbstractLZ4TestCase.java b/libs/lz4/src/test/java/org/elasticsearch/lz4/AbstractLZ4TestCase.java index 98a5319506e99..d432c7868c40f 100644 --- a/libs/lz4/src/test/java/org/elasticsearch/lz4/AbstractLZ4TestCase.java +++ b/libs/lz4/src/test/java/org/elasticsearch/lz4/AbstractLZ4TestCase.java @@ -45,9 +45,13 @@ abstract class AbstractLZ4TestCase extends ESTestCase { public interface TesterBase { T allocate(int length); + T copyOf(byte[] array); + byte[] copyOf(T data, int off, int len); + int maxCompressedLength(int len); + void fill(T instance, byte b); // Modified to remove redundant modifiers @@ -131,49 +135,62 @@ public void fill(ByteBuffer instance, byte b) { public interface Tester extends TesterBase { int compress(LZ4Compressor compressor, T src, int srcOff, int srcLen, T dest, int destOff, int maxDestLen); + int decompress(LZ4FastDecompressor decompressor, T src, int srcOff, T dest, int destOff, int destLen); + int decompress(LZ4SafeDecompressor decompressor, T src, int srcOff, int srcLen, T dest, int destOff, int maxDestLen); // Modified to remove redundant modifiers class ByteArrayTester extends ByteArrayTesterBase implements Tester { @Override - public int compress(LZ4Compressor compressor, byte[] src, int srcOff, - int srcLen, byte[] dest, int destOff, int maxDestLen) { + public int compress(LZ4Compressor compressor, byte[] src, int srcOff, int srcLen, byte[] dest, int destOff, int maxDestLen) { return compressor.compress(src, srcOff, srcLen, dest, destOff, maxDestLen); } @Override - public int decompress(LZ4FastDecompressor decompressor, - byte[] src, int srcOff, byte[] dest, int destOff, int destLen) { + public int decompress(LZ4FastDecompressor decompressor, byte[] src, int srcOff, byte[] dest, int destOff, int destLen) { return decompressor.decompress(src, srcOff, dest, destOff, destLen); } @Override - public int decompress(LZ4SafeDecompressor decompressor, - byte[] src, int srcOff, int srcLen, byte[] dest, int destOff, int maxDestLen) { + public int decompress( + LZ4SafeDecompressor decompressor, + byte[] src, + int srcOff, + int srcLen, + byte[] dest, + int destOff, + int maxDestLen + ) { return decompressor.decompress(src, srcOff, srcLen, dest, destOff, maxDestLen); } } + // Modified to remove redundant modifiers Tester BYTE_ARRAY = new ByteArrayTester(); // Modified to remove redundant modifiers Tester BYTE_ARRAY_WITH_LENGTH = new ByteArrayTester() { @Override - public int compress(LZ4Compressor compressor, byte[] src, int srcOff, - int srcLen, byte[] dest, int destOff, int maxDestLen) { + public int compress(LZ4Compressor compressor, byte[] src, int srcOff, int srcLen, byte[] dest, int destOff, int maxDestLen) { return new LZ4CompressorWithLength(compressor).compress(src, srcOff, srcLen, dest, destOff, maxDestLen); } @Override - public int decompress(LZ4FastDecompressor decompressor, - byte[] src, int srcOff, byte[] dest, int destOff, int destLen) { + public int decompress(LZ4FastDecompressor decompressor, byte[] src, int srcOff, byte[] dest, int destOff, int destLen) { return new LZ4DecompressorWithLength(decompressor).decompress(src, srcOff, dest, destOff); } @Override - public int decompress(LZ4SafeDecompressor decompressor, - byte[] src, int srcOff, int srcLen, byte[] dest, int destOff, int maxDestLen) { + public int decompress( + LZ4SafeDecompressor decompressor, + byte[] src, + int srcOff, + int srcLen, + byte[] dest, + int destOff, + int maxDestLen + ) { return new LZ4DecompressorWithLength(decompressor).decompress(src, srcOff, srcLen, dest, destOff); } }; @@ -182,42 +199,69 @@ public int decompress(LZ4SafeDecompressor decompressor, class ByteBufferTester extends ByteBufferTesterBase implements Tester { @Override - public int compress(LZ4Compressor compressor, ByteBuffer src, int srcOff, - int srcLen, ByteBuffer dest, int destOff, int maxDestLen) { + public int compress( + LZ4Compressor compressor, + ByteBuffer src, + int srcOff, + int srcLen, + ByteBuffer dest, + int destOff, + int maxDestLen + ) { return compressor.compress(src, srcOff, srcLen, dest, destOff, maxDestLen); } @Override - public int decompress(LZ4FastDecompressor decompressor, ByteBuffer src, - int srcOff, ByteBuffer dest, int destOff, int destLen) { + public int decompress(LZ4FastDecompressor decompressor, ByteBuffer src, int srcOff, ByteBuffer dest, int destOff, int destLen) { return decompressor.decompress(src, srcOff, dest, destOff, destLen); } @Override - public int decompress(LZ4SafeDecompressor decompressor, ByteBuffer src, - int srcOff, int srcLen, ByteBuffer dest, int destOff, int maxDestLen) { + public int decompress( + LZ4SafeDecompressor decompressor, + ByteBuffer src, + int srcOff, + int srcLen, + ByteBuffer dest, + int destOff, + int maxDestLen + ) { return decompressor.decompress(src, srcOff, srcLen, dest, destOff, maxDestLen); } } + // Modified to remove redundant modifiers Tester BYTE_BUFFER = new ByteBufferTester(); // Modified to remove redundant modifiers Tester BYTE_BUFFER_WITH_LENGTH = new ByteBufferTester() { @Override - public int compress(LZ4Compressor compressor, ByteBuffer src, int srcOff, - int srcLen, ByteBuffer dest, int destOff, int maxDestLen) { + public int compress( + LZ4Compressor compressor, + ByteBuffer src, + int srcOff, + int srcLen, + ByteBuffer dest, + int destOff, + int maxDestLen + ) { return new LZ4CompressorWithLength(compressor).compress(src, srcOff, srcLen, dest, destOff, maxDestLen); } @Override - public int decompress(LZ4FastDecompressor decompressor, ByteBuffer src, - int srcOff, ByteBuffer dest, int destOff, int destLen) { + public int decompress(LZ4FastDecompressor decompressor, ByteBuffer src, int srcOff, ByteBuffer dest, int destOff, int destLen) { return new LZ4DecompressorWithLength(decompressor).decompress(src, srcOff, dest, destOff); } @Override - public int decompress(LZ4SafeDecompressor decompressor, ByteBuffer src, - int srcOff, int srcLen, ByteBuffer dest, int destOff, int maxDestLen) { + public int decompress( + LZ4SafeDecompressor decompressor, + ByteBuffer src, + int srcOff, + int srcLen, + ByteBuffer dest, + int destOff, + int maxDestLen + ) { return new LZ4DecompressorWithLength(decompressor).decompress(src, srcOff, srcLen, dest, destOff); } }; @@ -227,7 +271,9 @@ public int decompress(LZ4SafeDecompressor decompressor, ByteBuffer src, public interface SrcDestTester extends TesterBase { int compress(LZ4Compressor compressor, T src, T dest); + int decompress(LZ4FastDecompressor decompressor, T src, T dest); + int decompress(LZ4SafeDecompressor decompressor, T src, T dest); // Modified to remove redundant modifiers @@ -248,6 +294,7 @@ public int decompress(LZ4SafeDecompressor decompressor, byte[] src, byte[] dest) return decompressor.decompress(src, dest); } } + // Modified to remove redundant modifiers SrcDestTester BYTE_ARRAY = new ByteArrayTester(); // Modified to remove redundant modifiers @@ -292,6 +339,7 @@ public int decompress(LZ4SafeDecompressor decompressor, ByteBuffer src, ByteBuff return dest.position() - pos; } } + // Modified to remove redundant modifiers SrcDestTester BYTE_BUFFER = new ByteBufferTester(); // Modified to remove redundant modifiers @@ -321,6 +369,7 @@ public int decompress(LZ4SafeDecompressor decompressor, ByteBuffer src, ByteBuff protected class RandomBytes { private final byte[] bytes; + RandomBytes(int n) { assert n > 0 && n <= 256; bytes = new byte[n]; @@ -328,6 +377,7 @@ protected class RandomBytes { bytes[i] = (byte) randomInt(255); } } + byte next() { final int i = randomInt(bytes.length - 1); return bytes[i]; diff --git a/libs/lz4/src/test/java/org/elasticsearch/lz4/ESLZ4CompressorTests.java b/libs/lz4/src/test/java/org/elasticsearch/lz4/ESLZ4CompressorTests.java index 37e97b49452f2..10c8c8a3a4566 100644 --- a/libs/lz4/src/test/java/org/elasticsearch/lz4/ESLZ4CompressorTests.java +++ b/libs/lz4/src/test/java/org/elasticsearch/lz4/ESLZ4CompressorTests.java @@ -9,9 +9,7 @@ package org.elasticsearch.lz4; import net.jpountz.lz4.LZ4Compressor; - import net.jpountz.lz4.LZ4Factory; - import net.jpountz.lz4.LZ4FastDecompressor; import org.elasticsearch.common.io.stream.BytesStreamOutput; diff --git a/libs/lz4/src/test/java/org/elasticsearch/lz4/ESLZ4Tests.java b/libs/lz4/src/test/java/org/elasticsearch/lz4/ESLZ4Tests.java index f2a085b7b21cd..ff2c0b0378c2b 100644 --- a/libs/lz4/src/test/java/org/elasticsearch/lz4/ESLZ4Tests.java +++ b/libs/lz4/src/test/java/org/elasticsearch/lz4/ESLZ4Tests.java @@ -41,9 +41,7 @@ public class ESLZ4Tests extends AbstractLZ4TestCase { // Modified to only test ES decompressor instances - static LZ4FastDecompressor[] FAST_DECOMPRESSORS = new LZ4FastDecompressor[] { - ESLZ4Decompressor.INSTANCE - }; + static LZ4FastDecompressor[] FAST_DECOMPRESSORS = new LZ4FastDecompressor[] { ESLZ4Decompressor.INSTANCE }; // Modified to not test any SAFE_DECOMPRESSORS, as we do not support it static LZ4SafeDecompressor[] SAFE_DECOMPRESSORS = new LZ4SafeDecompressor[0]; @@ -108,17 +106,22 @@ public void testUncompressWorstCase(LZ4SafeDecompressor decompressor) { // Modified to delete testUncompressSafeWorstCase (we do not have "safe" decompressor) // Modified to only test 1 (fast) decompressor - public void testRoundTrip(byte[] data, int off, int len, - LZ4Compressor compressor, - LZ4FastDecompressor decompressor) { - for (Tester tester : Arrays.asList(Tester.BYTE_ARRAY, Tester.BYTE_BUFFER, Tester.BYTE_ARRAY_WITH_LENGTH, - Tester.BYTE_BUFFER_WITH_LENGTH)) { + public void testRoundTrip(byte[] data, int off, int len, LZ4Compressor compressor, LZ4FastDecompressor decompressor) { + for (Tester tester : Arrays.asList( + Tester.BYTE_ARRAY, + Tester.BYTE_BUFFER, + Tester.BYTE_ARRAY_WITH_LENGTH, + Tester.BYTE_BUFFER_WITH_LENGTH + )) { testRoundTrip(tester, data, off, len, compressor, decompressor); } if (data.length == len && off == 0) { - for (SrcDestTester tester : Arrays.asList(SrcDestTester.BYTE_ARRAY, SrcDestTester.BYTE_BUFFER, + for (SrcDestTester tester : Arrays.asList( + SrcDestTester.BYTE_ARRAY, + SrcDestTester.BYTE_BUFFER, SrcDestTester.BYTE_ARRAY_WITH_LENGTH, - SrcDestTester.BYTE_BUFFER_WITH_LENGTH)) { + SrcDestTester.BYTE_BUFFER_WITH_LENGTH + )) { testRoundTrip(tester, data, compressor, decompressor); } } @@ -127,26 +130,32 @@ public void testRoundTrip(byte[] data, int off, int len, // Modified to only test 1 (fast) decompressor public void testRoundTrip( Tester tester, - byte[] data, int off, int len, + byte[] data, + int off, + int len, LZ4Compressor compressor, - LZ4FastDecompressor decompressor) { + LZ4FastDecompressor decompressor + ) { final int maxCompressedLength = tester.maxCompressedLength(len); // "maxCompressedLength + 1" for the over-estimated compressed length test below final T compressed = tester.allocate(maxCompressedLength + 1); - final int compressedLen = tester.compress(compressor, - tester.copyOf(data), off, len, - compressed, 0, maxCompressedLength); + final int compressedLen = tester.compress(compressor, tester.copyOf(data), off, len, compressed, 0, maxCompressedLength); // Modified to compress using an unforked lz4-java compressor and verify that the results are same. T expectedCompressed = tester.allocate(maxCompressedLength + 1); LZ4Compressor unForkedCompressor = LZ4Factory.safeInstance().fastCompressor(); - final int expectedCompressedLen = tester.compress(unForkedCompressor, - tester.copyOf(data), off, len, - expectedCompressed, 0, maxCompressedLength); + final int expectedCompressedLen = tester.compress( + unForkedCompressor, + tester.copyOf(data), + off, + len, + expectedCompressed, + 0, + maxCompressedLength + ); assertEquals(expectedCompressedLen, compressedLen); assertArrayEquals(tester.copyOf(expectedCompressed, 0, expectedCompressedLen), tester.copyOf(compressed, 0, compressedLen)); - // test decompression final T restored = tester.allocate(len); assertEquals(compressedLen, tester.decompress(decompressor, compressed, 0, restored, 0, len)); @@ -154,11 +163,9 @@ public void testRoundTrip( // make sure it fails if the compression dest is not large enough tester.fill(restored, randomByte()); - final T compressed2 = tester.allocate(compressedLen-1); + final T compressed2 = tester.allocate(compressedLen - 1); try { - final int compressedLen2 = tester.compress(compressor, - tester.copyOf(data), off, len, - compressed2, 0, compressedLen - 1); + final int compressedLen2 = tester.compress(compressor, tester.copyOf(data), off, len, compressed2, 0, compressedLen - 1); // Compression can succeed even with the smaller dest // because the compressor is allowed to return different compression results // even when it is invoked with the same input data. @@ -184,7 +191,7 @@ public void testRoundTrip( } // decompression dest is too large - final T restored2 = tester.allocate(len+1); + final T restored2 = tester.allocate(len + 1); try { final int cpLen = tester.decompress(decompressor, compressed, 0, restored2, 0, len + 1); fail("compressedLen=" + cpLen); @@ -197,34 +204,29 @@ public void testRoundTrip( } // Modified to only test 1 (fast) decompressor - public void testRoundTrip(SrcDestTester tester, - byte[] data, - LZ4Compressor compressor, - LZ4FastDecompressor decompressor) { + public void testRoundTrip(SrcDestTester tester, byte[] data, LZ4Compressor compressor, LZ4FastDecompressor decompressor) { final T original = tester.copyOf(data); final int maxCompressedLength = tester.maxCompressedLength(data.length); final T compressed = tester.allocate(maxCompressedLength); - final int compressedLen = tester.compress(compressor, - original, - compressed); + final int compressedLen = tester.compress(compressor, original, compressed); if (original instanceof ByteBuffer) { - assertEquals(data.length, ((ByteBuffer)original).position()); - assertEquals(compressedLen, ((ByteBuffer)compressed).position()); - ((ByteBuffer)original).rewind(); - ((ByteBuffer)compressed).rewind(); + assertEquals(data.length, ((ByteBuffer) original).position()); + assertEquals(compressedLen, ((ByteBuffer) compressed).position()); + ((ByteBuffer) original).rewind(); + ((ByteBuffer) compressed).rewind(); } // test decompression final T restored = tester.allocate(data.length); assertEquals(compressedLen, tester.decompress(decompressor, compressed, restored)); if (original instanceof ByteBuffer) { - assertEquals(compressedLen, ((ByteBuffer)compressed).position()); - assertEquals(data.length, ((ByteBuffer)restored).position()); + assertEquals(compressedLen, ((ByteBuffer) compressed).position()); + assertEquals(data.length, ((ByteBuffer) restored).position()); } assertArrayEquals(data, tester.copyOf(restored, 0, data.length)); if (original instanceof ByteBuffer) { - ((ByteBuffer)compressed).rewind(); - ((ByteBuffer)restored).rewind(); + ((ByteBuffer) compressed).rewind(); + ((ByteBuffer) restored).rewind(); } // Modified to delete "safe" decompressor tests @@ -235,8 +237,10 @@ public void testRoundTrip(SrcDestTester tester, public void testRoundTrip(byte[] data, int off, int len) { // Modified to only test safe instance and forked instance for (LZ4Compressor compressor : Arrays.asList(LZ4Factory.safeInstance().fastCompressor(), ESLZ4Compressor.INSTANCE)) { - for (LZ4FastDecompressor decompressor : Arrays.asList(LZ4Factory.safeInstance().fastDecompressor(), - ESLZ4Decompressor.INSTANCE)) { + for (LZ4FastDecompressor decompressor : Arrays.asList( + LZ4Factory.safeInstance().fastDecompressor(), + ESLZ4Decompressor.INSTANCE + )) { testRoundTrip(data, off, len, compressor, decompressor); } } @@ -371,56 +375,1526 @@ public void testRandomData() { // https://github.com/jpountz/lz4-java/issues/12 public void testRoundtripIssue12() { - byte[] data = new byte[]{ - 14, 72, 14, 85, 3, 72, 14, 85, 3, 72, 14, 72, 14, 72, 14, 85, 3, 72, 14, 72, 14, 72, 14, 72, 14, 72, 14, 72, 14, 85, 3, 72, - 14, 85, 3, 72, 14, 85, 3, 72, 14, 85, 3, 72, 14, 85, 3, 72, 14, 85, 3, 72, 14, 50, 64, 0, 46, -1, 0, 0, 0, 29, 3, 85, - 8, -113, 0, 68, -97, 3, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, 85, 8, -113, 0, 68, -97, 3, - 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, - 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, - 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 50, 64, 0, 47, -105, 0, 0, 0, 30, 3, -97, 6, 0, 68, -113, - 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, 85, 8, -113, 0, 68, -97, 3, 0, 2, 3, 85, 8, -113, 0, 68, -97, 3, 0, 2, 3, 85, - 8, -113, 0, 68, -97, 3, 0, 2, -97, 6, 0, 2, 3, 85, 8, -113, 0, 68, -97, 3, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, - 6, 0, 68, -113, 0, 120, 64, 0, 48, 4, 0, 0, 0, 31, 34, 72, 29, 72, 37, 72, 35, 72, 45, 72, 23, 72, 46, 72, 20, 72, 40, 72, - 33, 72, 25, 72, 39, 72, 38, 72, 26, 72, 28, 72, 42, 72, 24, 72, 27, 72, 36, 72, 41, 72, 32, 72, 18, 72, 30, 72, 22, 72, 31, 72, - 43, 72, 19, 72, 34, 72, 29, 72, 37, 72, 35, 72, 45, 72, 23, 72, 46, 72, 20, 72, 40, 72, 33, 72, 25, 72, 39, 72, 38, 72, 26, 72, - 28, 72, 42, 72, 24, 72, 27, 72, 36, 72, 41, 72, 32, 72, 18, 72, 30, 72, 22, 72, 31, 72, 43, 72, 19, 72, 34, 72, 29, 72, 37, 72, - 35, 72, 45, 72, 23, 72, 46, 72, 20, 72, 40, 72, 33, 72, 25, 72, 39, 72, 38, 72, 26, 72, 28, 72, 42, 72, 24, 72, 27, 72, 36, 72, - 41, 72, 32, 72, 18, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 39, 24, 32, 34, 124, 0, 120, 64, 0, 48, 80, 0, 0, 0, 31, 30, 72, 22, 72, 31, 72, 43, 72, 19, 72, 34, 72, 29, 72, 37, 72, - 35, 72, 45, 72, 23, 72, 46, 72, 20, 72, 40, 72, 33, 72, 25, 72, 39, 72, 38, 72, 26, 72, 28, 72, 42, 72, 24, 72, 27, 72, 36, 72, - 41, 72, 32, 72, 18, 72, 30, 72, 22, 72, 31, 72, 43, 72, 19, 72, 34, 72, 29, 72, 37, 72, 35, 72, 45, 72, 23, 72, 46, 72, 20, 72, - 40, 72, 33, 72, 25, 72, 39, 72, 38, 72, 26, 72, 28, 72, 42, 72, 24, 72, 27, 72, 36, 72, 41, 72, 32, 72, 18, 72, 30, 72, 22, 72, - 31, 72, 43, 72, 19, 72, 34, 72, 29, 72, 37, 72, 35, 72, 45, 72, 23, 72, 46, 72, 20, 72, 40, 72, 33, 72, 25, 72, 39, 72, 38, 72, - 26, 72, 28, 72, 42, 72, 24, 72, 27, 72, 36, 72, 41, 72, 32, 72, 18, 72, 30, 72, 22, 72, 31, 72, 43, 72, 19, 72, 34, 72, 29, 72, - 37, 72, 35, 72, 45, 72, 23, 72, 46, 72, 20, 72, 40, 72, 33, 72, 25, 72, 39, 72, 38, 72, 26, 72, 28, 72, 42, 72, 24, 72, 27, 72, - 36, 72, 41, 72, 32, 72, 18, 72, 30, 72, 22, 72, 31, 72, 43, 72, 19, 72, 34, 72, 29, 72, 37, 72, 35, 72, 45, 72, 23, 72, 46, 72, - 20, 72, 40, 72, 33, 72, 25, 72, 39, 72, 38, 72, 26, 72, 28, 72, 42, 72, 24, 72, 27, 72, 36, 72, 41, 72, 32, 72, 18, 72, 30, 72, - 22, 72, 31, 72, 43, 72, 19, 72, 34, 72, 29, 72, 37, 72, 35, 72, 45, 72, 23, 72, 46, 72, 20, 72, 40, 72, 33, 72, 25, 72, 39, 72, - 38, 72, 26, 72, 28, 72, 42, 72, 24, 72, 27, 72, 36, 72, 41, 72, 32, 72, 18, 72, 30, 72, 22, 72, 31, 72, 43, 72, 19, 72, 34, 72, - 29, 72, 37, 72, 35, 72, 45, 72, 23, 72, 46, 72, 20, 72, 40, 72, 33, 72, 25, 72, 39, 72, 38, 72, 26, 72, 28, 72, 42, 72, 24, 72, - 27, 72, 36, 72, 41, 72, 32, 72, 18, 72, 30, 72, 22, 72, 31, 72, 43, 72, 19, 50, 64, 0, 49, 20, 0, 0, 0, 32, 3, -97, 6, 0, - 68, -113, 0, 2, 3, 85, 8, -113, 0, 68, -97, 3, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, - 6, 0, 68, -113, 0, 2, 3, 85, 8, -113, 0, 68, -97, 3, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, - 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, - 3, -97, 6, 0, 50, 64, 0, 50, 53, 0, 0, 0, 34, 3, -97, 6, 0, 68, -113, 0, 2, 3, 85, 8, -113, 0, 68, -113, 0, 2, 3, -97, - 6, 0, 68, -113, 0, 2, 3, 85, 8, -113, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, - -97, 6, 0, 68, -113, 0, 2, 3, 85, 8, -113, 0, 68, -97, 3, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, 85, 8, -113, 0, 68, -97, - 3, 0, 2, 3, 85, 8, -113, 0, 68, -97, 3, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, 85, 8, -113, 0, 68, -97, 3, 0, 2, 3, - 85, 8, -113, 0, 68, -97, 3, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, - 2, 3, 85, 8, -113, 0, 68, -97, 3, 0, 2, 3, 85, 8, -113, 0, 68, -97, 3, 0, 2, 3, 85, 8, -113, 0, 68, -97, 3, 0, 2, 3, - -97, 6, 0, 50, 64, 0, 51, 85, 0, 0, 0, 36, 3, 85, 8, -113, 0, 68, -97, 3, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, - 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, -97, 5, 0, 2, 3, 85, 8, -113, 0, 68, - -97, 3, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, - 68, -113, 0, 2, 3, -97, 6, 0, 50, -64, 0, 51, -45, 0, 0, 0, 37, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, - 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, -97, 6, 0, 68, -113, 0, 2, 3, 85, 8, -113, 0, 68, -113, 0, 2, 3, -97, - 6, 0, 68, -113, 0, 2, 3, 85, 8, -113, 0, 68, -97, 3, 0, 2, 3, 85, 8, -113, 0, 68, -97, 3, 0, 120, 64, 0, 52, -88, 0, 0, - 0, 39, 13, 85, 5, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 85, 5, 72, - 13, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 72, 13, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 85, - 5, 72, 13, 85, 5, 72, 13, 72, 13, 72, 13, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 85, - 5, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 85, - 5, 72, 13, 85, 5, 72, 13, 72, 13, 72, 13, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 85, 5, 72, 13, 72, 13, 85, 5, 72, 13, 72, - 13, 85, 5, 72, 13, 72, 13, 85, 5, 72, 13, -19, -24, -101, -35 - }; + byte[] data = new byte[] { + 14, + 72, + 14, + 85, + 3, + 72, + 14, + 85, + 3, + 72, + 14, + 72, + 14, + 72, + 14, + 85, + 3, + 72, + 14, + 72, + 14, + 72, + 14, + 72, + 14, + 72, + 14, + 72, + 14, + 85, + 3, + 72, + 14, + 85, + 3, + 72, + 14, + 85, + 3, + 72, + 14, + 85, + 3, + 72, + 14, + 85, + 3, + 72, + 14, + 85, + 3, + 72, + 14, + 50, + 64, + 0, + 46, + -1, + 0, + 0, + 0, + 29, + 3, + 85, + 8, + -113, + 0, + 68, + -97, + 3, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + 85, + 8, + -113, + 0, + 68, + -97, + 3, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 50, + 64, + 0, + 47, + -105, + 0, + 0, + 0, + 30, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + 85, + 8, + -113, + 0, + 68, + -97, + 3, + 0, + 2, + 3, + 85, + 8, + -113, + 0, + 68, + -97, + 3, + 0, + 2, + 3, + 85, + 8, + -113, + 0, + 68, + -97, + 3, + 0, + 2, + -97, + 6, + 0, + 2, + 3, + 85, + 8, + -113, + 0, + 68, + -97, + 3, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 120, + 64, + 0, + 48, + 4, + 0, + 0, + 0, + 31, + 34, + 72, + 29, + 72, + 37, + 72, + 35, + 72, + 45, + 72, + 23, + 72, + 46, + 72, + 20, + 72, + 40, + 72, + 33, + 72, + 25, + 72, + 39, + 72, + 38, + 72, + 26, + 72, + 28, + 72, + 42, + 72, + 24, + 72, + 27, + 72, + 36, + 72, + 41, + 72, + 32, + 72, + 18, + 72, + 30, + 72, + 22, + 72, + 31, + 72, + 43, + 72, + 19, + 72, + 34, + 72, + 29, + 72, + 37, + 72, + 35, + 72, + 45, + 72, + 23, + 72, + 46, + 72, + 20, + 72, + 40, + 72, + 33, + 72, + 25, + 72, + 39, + 72, + 38, + 72, + 26, + 72, + 28, + 72, + 42, + 72, + 24, + 72, + 27, + 72, + 36, + 72, + 41, + 72, + 32, + 72, + 18, + 72, + 30, + 72, + 22, + 72, + 31, + 72, + 43, + 72, + 19, + 72, + 34, + 72, + 29, + 72, + 37, + 72, + 35, + 72, + 45, + 72, + 23, + 72, + 46, + 72, + 20, + 72, + 40, + 72, + 33, + 72, + 25, + 72, + 39, + 72, + 38, + 72, + 26, + 72, + 28, + 72, + 42, + 72, + 24, + 72, + 27, + 72, + 36, + 72, + 41, + 72, + 32, + 72, + 18, + 16, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 39, + 24, + 32, + 34, + 124, + 0, + 120, + 64, + 0, + 48, + 80, + 0, + 0, + 0, + 31, + 30, + 72, + 22, + 72, + 31, + 72, + 43, + 72, + 19, + 72, + 34, + 72, + 29, + 72, + 37, + 72, + 35, + 72, + 45, + 72, + 23, + 72, + 46, + 72, + 20, + 72, + 40, + 72, + 33, + 72, + 25, + 72, + 39, + 72, + 38, + 72, + 26, + 72, + 28, + 72, + 42, + 72, + 24, + 72, + 27, + 72, + 36, + 72, + 41, + 72, + 32, + 72, + 18, + 72, + 30, + 72, + 22, + 72, + 31, + 72, + 43, + 72, + 19, + 72, + 34, + 72, + 29, + 72, + 37, + 72, + 35, + 72, + 45, + 72, + 23, + 72, + 46, + 72, + 20, + 72, + 40, + 72, + 33, + 72, + 25, + 72, + 39, + 72, + 38, + 72, + 26, + 72, + 28, + 72, + 42, + 72, + 24, + 72, + 27, + 72, + 36, + 72, + 41, + 72, + 32, + 72, + 18, + 72, + 30, + 72, + 22, + 72, + 31, + 72, + 43, + 72, + 19, + 72, + 34, + 72, + 29, + 72, + 37, + 72, + 35, + 72, + 45, + 72, + 23, + 72, + 46, + 72, + 20, + 72, + 40, + 72, + 33, + 72, + 25, + 72, + 39, + 72, + 38, + 72, + 26, + 72, + 28, + 72, + 42, + 72, + 24, + 72, + 27, + 72, + 36, + 72, + 41, + 72, + 32, + 72, + 18, + 72, + 30, + 72, + 22, + 72, + 31, + 72, + 43, + 72, + 19, + 72, + 34, + 72, + 29, + 72, + 37, + 72, + 35, + 72, + 45, + 72, + 23, + 72, + 46, + 72, + 20, + 72, + 40, + 72, + 33, + 72, + 25, + 72, + 39, + 72, + 38, + 72, + 26, + 72, + 28, + 72, + 42, + 72, + 24, + 72, + 27, + 72, + 36, + 72, + 41, + 72, + 32, + 72, + 18, + 72, + 30, + 72, + 22, + 72, + 31, + 72, + 43, + 72, + 19, + 72, + 34, + 72, + 29, + 72, + 37, + 72, + 35, + 72, + 45, + 72, + 23, + 72, + 46, + 72, + 20, + 72, + 40, + 72, + 33, + 72, + 25, + 72, + 39, + 72, + 38, + 72, + 26, + 72, + 28, + 72, + 42, + 72, + 24, + 72, + 27, + 72, + 36, + 72, + 41, + 72, + 32, + 72, + 18, + 72, + 30, + 72, + 22, + 72, + 31, + 72, + 43, + 72, + 19, + 72, + 34, + 72, + 29, + 72, + 37, + 72, + 35, + 72, + 45, + 72, + 23, + 72, + 46, + 72, + 20, + 72, + 40, + 72, + 33, + 72, + 25, + 72, + 39, + 72, + 38, + 72, + 26, + 72, + 28, + 72, + 42, + 72, + 24, + 72, + 27, + 72, + 36, + 72, + 41, + 72, + 32, + 72, + 18, + 72, + 30, + 72, + 22, + 72, + 31, + 72, + 43, + 72, + 19, + 72, + 34, + 72, + 29, + 72, + 37, + 72, + 35, + 72, + 45, + 72, + 23, + 72, + 46, + 72, + 20, + 72, + 40, + 72, + 33, + 72, + 25, + 72, + 39, + 72, + 38, + 72, + 26, + 72, + 28, + 72, + 42, + 72, + 24, + 72, + 27, + 72, + 36, + 72, + 41, + 72, + 32, + 72, + 18, + 72, + 30, + 72, + 22, + 72, + 31, + 72, + 43, + 72, + 19, + 50, + 64, + 0, + 49, + 20, + 0, + 0, + 0, + 32, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + 85, + 8, + -113, + 0, + 68, + -97, + 3, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + 85, + 8, + -113, + 0, + 68, + -97, + 3, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + -97, + 6, + 0, + 50, + 64, + 0, + 50, + 53, + 0, + 0, + 0, + 34, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + 85, + 8, + -113, + 0, + 68, + -113, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + 85, + 8, + -113, + 0, + 68, + -113, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + 85, + 8, + -113, + 0, + 68, + -97, + 3, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + 85, + 8, + -113, + 0, + 68, + -97, + 3, + 0, + 2, + 3, + 85, + 8, + -113, + 0, + 68, + -97, + 3, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + 85, + 8, + -113, + 0, + 68, + -97, + 3, + 0, + 2, + 3, + 85, + 8, + -113, + 0, + 68, + -97, + 3, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + 85, + 8, + -113, + 0, + 68, + -97, + 3, + 0, + 2, + 3, + 85, + 8, + -113, + 0, + 68, + -97, + 3, + 0, + 2, + 3, + 85, + 8, + -113, + 0, + 68, + -97, + 3, + 0, + 2, + 3, + -97, + 6, + 0, + 50, + 64, + 0, + 51, + 85, + 0, + 0, + 0, + 36, + 3, + 85, + 8, + -113, + 0, + 68, + -97, + 3, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + -97, + 5, + 0, + 2, + 3, + 85, + 8, + -113, + 0, + 68, + -97, + 3, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + -97, + 6, + 0, + 50, + -64, + 0, + 51, + -45, + 0, + 0, + 0, + 37, + 68, + -113, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + 85, + 8, + -113, + 0, + 68, + -113, + 0, + 2, + 3, + -97, + 6, + 0, + 68, + -113, + 0, + 2, + 3, + 85, + 8, + -113, + 0, + 68, + -97, + 3, + 0, + 2, + 3, + 85, + 8, + -113, + 0, + 68, + -97, + 3, + 0, + 120, + 64, + 0, + 52, + -88, + 0, + 0, + 0, + 39, + 13, + 85, + 5, + 72, + 13, + 85, + 5, + 72, + 13, + 85, + 5, + 72, + 13, + 72, + 13, + 85, + 5, + 72, + 13, + 85, + 5, + 72, + 13, + 85, + 5, + 72, + 13, + 85, + 5, + 72, + 13, + 72, + 13, + 85, + 5, + 72, + 13, + 85, + 5, + 72, + 13, + 72, + 13, + 72, + 13, + 85, + 5, + 72, + 13, + 85, + 5, + 72, + 13, + 85, + 5, + 72, + 13, + 85, + 5, + 72, + 13, + 85, + 5, + 72, + 13, + 85, + 5, + 72, + 13, + 72, + 13, + 72, + 13, + 72, + 13, + 85, + 5, + 72, + 13, + 85, + 5, + 72, + 13, + 72, + 13, + 85, + 5, + 72, + 13, + 85, + 5, + 72, + 13, + 85, + 5, + 72, + 13, + 85, + 5, + 72, + 13, + 85, + 5, + 72, + 13, + 85, + 5, + 72, + 13, + 85, + 5, + 72, + 13, + 85, + 5, + 72, + 13, + 85, + 5, + 72, + 13, + 85, + 5, + 72, + 13, + 85, + 5, + 72, + 13, + 85, + 5, + 72, + 13, + 72, + 13, + 72, + 13, + 72, + 13, + 85, + 5, + 72, + 13, + 85, + 5, + 72, + 13, + 85, + 5, + 72, + 13, + 72, + 13, + 85, + 5, + 72, + 13, + 72, + 13, + 85, + 5, + 72, + 13, + 72, + 13, + 85, + 5, + 72, + 13, + -19, + -24, + -101, + -35 }; testRoundTrip(data, 9, data.length - 9); } @@ -479,8 +1953,7 @@ private Sequence(int literalLen, int matchDec, int matchLen, int length) { @Override public String toString() { - return "Sequence [literalLen=" + literalLen + ", matchDec=" + matchDec - + ", matchLen=" + matchLen + "]"; + return "Sequence [literalLen=" + literalLen + ", matchDec=" + matchDec + ", matchLen=" + matchLen + "]"; } @Override @@ -490,19 +1963,13 @@ public int hashCode() { @Override public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; + if (this == obj) return true; + if (obj == null) return false; + if (getClass() != obj.getClass()) return false; Sequence other = (Sequence) obj; - if (literalLen != other.literalLen) - return false; - if (matchDec != other.matchDec) - return false; - if (matchLen != other.matchLen) - return false; + if (literalLen != other.literalLen) return false; + if (matchDec != other.matchDec) return false; + if (matchLen != other.matchLen) return false; return true; } diff --git a/libs/nio/src/main/java/org/elasticsearch/nio/BytesChannelContext.java b/libs/nio/src/main/java/org/elasticsearch/nio/BytesChannelContext.java index 7bf27cc3edd5e..fd07866550a96 100644 --- a/libs/nio/src/main/java/org/elasticsearch/nio/BytesChannelContext.java +++ b/libs/nio/src/main/java/org/elasticsearch/nio/BytesChannelContext.java @@ -13,8 +13,14 @@ public class BytesChannelContext extends SocketChannelContext { - public BytesChannelContext(NioSocketChannel channel, NioSelector selector, Config.Socket socketConfig, - Consumer exceptionHandler, NioChannelHandler handler, InboundChannelBuffer channelBuffer) { + public BytesChannelContext( + NioSocketChannel channel, + NioSelector selector, + Config.Socket socketConfig, + Consumer exceptionHandler, + NioChannelHandler handler, + InboundChannelBuffer channelBuffer + ) { super(channel, selector, socketConfig, exceptionHandler, handler, channelBuffer); } diff --git a/libs/nio/src/main/java/org/elasticsearch/nio/BytesWriteHandler.java b/libs/nio/src/main/java/org/elasticsearch/nio/BytesWriteHandler.java index 760e5d27f2712..22275b8c090c1 100644 --- a/libs/nio/src/main/java/org/elasticsearch/nio/BytesWriteHandler.java +++ b/libs/nio/src/main/java/org/elasticsearch/nio/BytesWriteHandler.java @@ -18,8 +18,8 @@ public abstract class BytesWriteHandler implements NioChannelHandler { private static final List EMPTY_LIST = Collections.emptyList(); public WriteOperation createWriteOperation(SocketChannelContext context, Object message, BiConsumer listener) { - assert message instanceof ByteBuffer[] : "This channel only supports messages that are of type: " + ByteBuffer[].class - + ". Found type: " + message.getClass() + "."; + assert message instanceof ByteBuffer[] + : "This channel only supports messages that are of type: " + ByteBuffer[].class + ". Found type: " + message.getClass() + "."; return new FlushReadyWrite(context, (ByteBuffer[]) message, listener); } diff --git a/libs/nio/src/main/java/org/elasticsearch/nio/ChannelFactory.java b/libs/nio/src/main/java/org/elasticsearch/nio/ChannelFactory.java index 0c87e6ef11bb2..48133445454e3 100644 --- a/libs/nio/src/main/java/org/elasticsearch/nio/ChannelFactory.java +++ b/libs/nio/src/main/java/org/elasticsearch/nio/ChannelFactory.java @@ -32,18 +32,43 @@ public abstract class ChannelFactory getListener() { } public boolean isFullyFlushed() { - assert length >= internalIndex : "Should never have an index that is greater than the length [length=" + length + ", index=" - + internalIndex + "]"; + assert length >= internalIndex + : "Should never have an index that is greater than the length [length=" + length + ", index=" + internalIndex + "]"; return internalIndex == length; } public void incrementIndex(int delta) { internalIndex += delta; - assert length >= internalIndex : "Should never increment index past length [length=" + length + ", post-increment index=" - + internalIndex + ", delta=" + delta + "]"; + assert length >= internalIndex + : "Should never increment index past length [length=" + + length + + ", post-increment index=" + + internalIndex + + ", delta=" + + delta + + "]"; } public ByteBuffer[] getBuffersToWrite() { diff --git a/libs/nio/src/main/java/org/elasticsearch/nio/InboundChannelBuffer.java b/libs/nio/src/main/java/org/elasticsearch/nio/InboundChannelBuffer.java index eb4182da4b91f..988a5d3c95f6c 100644 --- a/libs/nio/src/main/java/org/elasticsearch/nio/InboundChannelBuffer.java +++ b/libs/nio/src/main/java/org/elasticsearch/nio/InboundChannelBuffer.java @@ -111,8 +111,9 @@ public void release(long bytesToRelease) { */ public ByteBuffer[] sliceBuffersTo(long to) { if (to > capacity) { - throw new IndexOutOfBoundsException("can't slice a channel buffer with capacity [" + capacity + - "], with slice parameters to [" + to + "]"); + throw new IndexOutOfBoundsException( + "can't slice a channel buffer with capacity [" + capacity + "], with slice parameters to [" + to + "]" + ); } else if (to == 0) { return EMPTY_BYTE_BUFFER_ARRAY; } @@ -150,8 +151,9 @@ public ByteBuffer[] sliceBuffersTo(long to) { */ public Page[] sliceAndRetainPagesTo(long to) { if (to > capacity) { - throw new IndexOutOfBoundsException("can't slice a channel buffer with capacity [" + capacity + - "], with slice parameters to [" + to + "]"); + throw new IndexOutOfBoundsException( + "can't slice a channel buffer with capacity [" + capacity + "], with slice parameters to [" + to + "]" + ); } else if (to == 0) { return EMPTY_BYTE_PAGE_ARRAY; } @@ -189,8 +191,9 @@ public Page[] sliceAndRetainPagesTo(long to) { */ public ByteBuffer[] sliceBuffersFrom(long from) { if (from > capacity) { - throw new IndexOutOfBoundsException("can't slice a channel buffer with capacity [" + capacity + - "], with slice parameters from [" + from + "]"); + throw new IndexOutOfBoundsException( + "can't slice a channel buffer with capacity [" + capacity + "], with slice parameters from [" + from + "]" + ); } else if (from == capacity) { return EMPTY_BYTE_BUFFER_ARRAY; } @@ -218,8 +221,17 @@ public void incrementIndex(long delta) { long newIndex = delta + internalIndex; if (newIndex > capacity) { - throw new IllegalArgumentException("Cannot increment an index [" + internalIndex + "] with a delta [" + delta + - "] that will result in a new index [" + newIndex + "] that is greater than the capacity [" + capacity + "]."); + throw new IllegalArgumentException( + "Cannot increment an index [" + + internalIndex + + "] with a delta [" + + delta + + "] that will result in a new index [" + + newIndex + + "] that is greater than the capacity [" + + capacity + + "]." + ); } internalIndex = newIndex; } diff --git a/libs/nio/src/main/java/org/elasticsearch/nio/NioSelector.java b/libs/nio/src/main/java/org/elasticsearch/nio/NioSelector.java index 7504cd5f1ffe6..d50839f7e5cc5 100644 --- a/libs/nio/src/main/java/org/elasticsearch/nio/NioSelector.java +++ b/libs/nio/src/main/java/org/elasticsearch/nio/NioSelector.java @@ -104,8 +104,13 @@ public boolean isOnCurrentThread() { } public void assertOnSelectorThread() { - assert isOnCurrentThread() : "Must be on selector thread [" + thread.get().getName() + "} to perform this operation. " + - "Currently on thread [" + Thread.currentThread().getName() + "]."; + assert isOnCurrentThread() + : "Must be on selector thread [" + + thread.get().getName() + + "} to perform this operation. " + + "Currently on thread [" + + Thread.currentThread().getName() + + "]."; } /** @@ -162,10 +167,10 @@ void singleLoop() { try { processKey(sk); } catch (CancelledKeyException cke) { - eventHandler.genericChannelException((ChannelContext) sk.attachment(), cke); + eventHandler.genericChannelException((ChannelContext) sk.attachment(), cke); } } else { - eventHandler.genericChannelException((ChannelContext) sk.attachment(), new CancelledKeyException()); + eventHandler.genericChannelException((ChannelContext) sk.attachment(), new CancelledKeyException()); } } } @@ -186,8 +191,9 @@ void cleanupAndCloseChannels() { cleanupPendingWrites(); channelsToClose.addAll(channelsToRegister); channelsToRegister.clear(); - channelsToClose.addAll(selector.keys().stream() - .map(sk -> (ChannelContext) sk.attachment()).filter(Objects::nonNull).collect(Collectors.toList())); + channelsToClose.addAll( + selector.keys().stream().map(sk -> (ChannelContext) sk.attachment()).filter(Objects::nonNull).collect(Collectors.toList()) + ); closePendingChannels(); } diff --git a/libs/nio/src/main/java/org/elasticsearch/nio/NioSelectorGroup.java b/libs/nio/src/main/java/org/elasticsearch/nio/NioSelectorGroup.java index 97ef82acdf5b8..711f1122bd270 100644 --- a/libs/nio/src/main/java/org/elasticsearch/nio/NioSelectorGroup.java +++ b/libs/nio/src/main/java/org/elasticsearch/nio/NioSelectorGroup.java @@ -33,7 +33,6 @@ */ public class NioSelectorGroup implements NioGroup { - private final List dedicatedAcceptors; private final RoundRobinSupplier acceptorSupplier; @@ -51,8 +50,11 @@ public class NioSelectorGroup implements NioGroup { * @param eventHandlerFunction function for creating event handlers * @throws IOException occurs if there is a problem while opening a java.nio.Selector */ - public NioSelectorGroup(ThreadFactory threadFactory, int selectorCount, - Function, EventHandler> eventHandlerFunction) throws IOException { + public NioSelectorGroup( + ThreadFactory threadFactory, + int selectorCount, + Function, EventHandler> eventHandlerFunction + ) throws IOException { this(null, 0, threadFactory, selectorCount, eventHandlerFunction); } @@ -68,8 +70,13 @@ public NioSelectorGroup(ThreadFactory threadFactory, int selectorCount, * @param eventHandlerFunction function for creating event handlers * @throws IOException occurs if there is a problem while opening a java.nio.Selector */ - public NioSelectorGroup(ThreadFactory acceptorThreadFactory, int dedicatedAcceptorCount, ThreadFactory selectorThreadFactory, - int selectorCount, Function, EventHandler> eventHandlerFunction) throws IOException { + public NioSelectorGroup( + ThreadFactory acceptorThreadFactory, + int dedicatedAcceptorCount, + ThreadFactory selectorThreadFactory, + int selectorCount, + Function, EventHandler> eventHandlerFunction + ) throws IOException { dedicatedAcceptors = new ArrayList<>(dedicatedAcceptorCount); selectors = new ArrayList<>(selectorCount); diff --git a/libs/nio/src/main/java/org/elasticsearch/nio/NioServerSocketChannel.java b/libs/nio/src/main/java/org/elasticsearch/nio/NioServerSocketChannel.java index 1ff80f1172c9c..7df0f45c573bf 100644 --- a/libs/nio/src/main/java/org/elasticsearch/nio/NioServerSocketChannel.java +++ b/libs/nio/src/main/java/org/elasticsearch/nio/NioServerSocketChannel.java @@ -60,9 +60,7 @@ public ServerChannelContext getContext() { @Override public String toString() { - return "NioServerSocketChannel{" + - "localAddress=" + getLocalAddress() + - '}'; + return "NioServerSocketChannel{" + "localAddress=" + getLocalAddress() + '}'; } private void attemptToSetLocalAddress() { diff --git a/libs/nio/src/main/java/org/elasticsearch/nio/NioSocketChannel.java b/libs/nio/src/main/java/org/elasticsearch/nio/NioSocketChannel.java index 10f58bb5c6b97..10c7fe7de932a 100644 --- a/libs/nio/src/main/java/org/elasticsearch/nio/NioSocketChannel.java +++ b/libs/nio/src/main/java/org/elasticsearch/nio/NioSocketChannel.java @@ -64,9 +64,6 @@ public void addConnectListener(BiConsumer listener) { @Override public String toString() { - return "NioSocketChannel{" + - "localAddress=" + getLocalAddress() + - ", remoteAddress=" + getRemoteAddress() + - '}'; + return "NioSocketChannel{" + "localAddress=" + getLocalAddress() + ", remoteAddress=" + getRemoteAddress() + '}'; } } diff --git a/libs/nio/src/main/java/org/elasticsearch/nio/Page.java b/libs/nio/src/main/java/org/elasticsearch/nio/Page.java index 50a1c0207eca5..bc85e7dfb27f2 100644 --- a/libs/nio/src/main/java/org/elasticsearch/nio/Page.java +++ b/libs/nio/src/main/java/org/elasticsearch/nio/Page.java @@ -8,9 +8,9 @@ package org.elasticsearch.nio; +import org.elasticsearch.core.AbstractRefCounted; import org.elasticsearch.core.Releasable; import org.elasticsearch.core.Releasables; -import org.elasticsearch.core.AbstractRefCounted; import java.nio.ByteBuffer; diff --git a/libs/nio/src/main/java/org/elasticsearch/nio/SelectionKeyUtils.java b/libs/nio/src/main/java/org/elasticsearch/nio/SelectionKeyUtils.java index 0e93b31c6a64b..d6bdd42f8837c 100644 --- a/libs/nio/src/main/java/org/elasticsearch/nio/SelectionKeyUtils.java +++ b/libs/nio/src/main/java/org/elasticsearch/nio/SelectionKeyUtils.java @@ -76,7 +76,6 @@ public static void setAcceptInterested(SelectionKey selectionKey) throws Cancell selectionKey.interestOps(selectionKey.interestOps() | SelectionKey.OP_ACCEPT); } - /** * Checks for an interest in writes for this selection key. * diff --git a/libs/nio/src/main/java/org/elasticsearch/nio/ServerChannelContext.java b/libs/nio/src/main/java/org/elasticsearch/nio/ServerChannelContext.java index d6af6c8e58cb4..746840f08007c 100644 --- a/libs/nio/src/main/java/org/elasticsearch/nio/ServerChannelContext.java +++ b/libs/nio/src/main/java/org/elasticsearch/nio/ServerChannelContext.java @@ -34,9 +34,14 @@ public class ServerChannelContext extends ChannelContext { private final ChannelFactory channelFactory; private final CompletableContext bindContext = new CompletableContext<>(); - public ServerChannelContext(NioServerSocketChannel channel, ChannelFactory channelFactory, NioSelector selector, - Config.ServerSocket config, Consumer acceptor, - Consumer exceptionHandler) { + public ServerChannelContext( + NioServerSocketChannel channel, + ChannelFactory channelFactory, + NioSelector selector, + Config.ServerSocket config, + Consumer acceptor, + Consumer exceptionHandler + ) { super(channel.getRawChannel(), exceptionHandler); this.channel = channel; this.channelFactory = channelFactory; diff --git a/libs/nio/src/main/java/org/elasticsearch/nio/SocketChannelContext.java b/libs/nio/src/main/java/org/elasticsearch/nio/SocketChannelContext.java index a86fb53c295bc..655631c4623dd 100644 --- a/libs/nio/src/main/java/org/elasticsearch/nio/SocketChannelContext.java +++ b/libs/nio/src/main/java/org/elasticsearch/nio/SocketChannelContext.java @@ -53,9 +53,14 @@ public abstract class SocketChannelContext extends ChannelContext private boolean socketOptionsSet; private Exception connectException; - protected SocketChannelContext(NioSocketChannel channel, NioSelector selector, Config.Socket socketConfig, - Consumer exceptionHandler, NioChannelHandler channelHandler, - InboundChannelBuffer channelBuffer) { + protected SocketChannelContext( + NioSocketChannel channel, + NioSelector selector, + Config.Socket socketConfig, + Consumer exceptionHandler, + NioChannelHandler channelHandler, + InboundChannelBuffer channelBuffer + ) { super(channel.getRawChannel(), exceptionHandler); this.selector = selector; this.channel = channel; diff --git a/libs/nio/src/test/java/org/elasticsearch/nio/BytesChannelContextTests.java b/libs/nio/src/test/java/org/elasticsearch/nio/BytesChannelContextTests.java index cd37e48ec9a29..6c915c4ec810f 100644 --- a/libs/nio/src/test/java/org/elasticsearch/nio/BytesChannelContextTests.java +++ b/libs/nio/src/test/java/org/elasticsearch/nio/BytesChannelContextTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.nio; -import org.elasticsearch.core.CheckedFunction; import org.elasticsearch.common.util.PageCacheRecycler; +import org.elasticsearch.core.CheckedFunction; import org.elasticsearch.test.ESTestCase; import org.junit.Before; @@ -106,7 +106,6 @@ public void testPartialRead() throws IOException { return bytes.length; }); - when(readConsumer.apply(channelBuffer)).thenReturn(0); assertEquals(messageLength, context.read()); @@ -151,7 +150,7 @@ public void testReadLessThanZeroMeansReadyForClose() throws IOException { public void testQueuedWriteIsFlushedInFlushCall() throws Exception { assertFalse(context.readyForFlush()); - ByteBuffer[] buffers = {ByteBuffer.allocate(10)}; + ByteBuffer[] buffers = { ByteBuffer.allocate(10) }; FlushReadyWrite flushOperation = mock(FlushReadyWrite.class); context.queueWriteOperation(flushOperation); @@ -177,7 +176,7 @@ public void testPartialFlush() throws IOException { assertTrue(context.readyForFlush()); when(flushOperation.isFullyFlushed()).thenReturn(false); - when(flushOperation.getBuffersToWrite(anyInt())).thenReturn(new ByteBuffer[] {ByteBuffer.allocate(3)}); + when(flushOperation.getBuffersToWrite(anyInt())).thenReturn(new ByteBuffer[] { ByteBuffer.allocate(3) }); context.flushChannel(); verify(listener, times(0)).accept(null, null); @@ -191,8 +190,8 @@ public void testMultipleWritesPartialFlushes() throws IOException { BiConsumer listener2 = mock(BiConsumer.class); FlushReadyWrite flushOperation1 = mock(FlushReadyWrite.class); FlushReadyWrite flushOperation2 = mock(FlushReadyWrite.class); - when(flushOperation1.getBuffersToWrite(anyInt())).thenReturn(new ByteBuffer[] {ByteBuffer.allocate(3)}); - when(flushOperation2.getBuffersToWrite(anyInt())).thenReturn(new ByteBuffer[] {ByteBuffer.allocate(3)}); + when(flushOperation1.getBuffersToWrite(anyInt())).thenReturn(new ByteBuffer[] { ByteBuffer.allocate(3) }); + when(flushOperation2.getBuffersToWrite(anyInt())).thenReturn(new ByteBuffer[] { ByteBuffer.allocate(3) }); when(flushOperation1.getListener()).thenReturn(listener); when(flushOperation2.getListener()).thenReturn(listener2); @@ -220,7 +219,7 @@ public void testMultipleWritesPartialFlushes() throws IOException { public void testWhenIOExceptionThrownListenerIsCalled() throws IOException { assertFalse(context.readyForFlush()); - ByteBuffer[] buffers = {ByteBuffer.allocate(10)}; + ByteBuffer[] buffers = { ByteBuffer.allocate(10) }; FlushReadyWrite flushOperation = mock(FlushReadyWrite.class); context.queueWriteOperation(flushOperation); @@ -237,7 +236,7 @@ public void testWhenIOExceptionThrownListenerIsCalled() throws IOException { } public void testWriteIOExceptionMeansChannelReadyToClose() throws IOException { - ByteBuffer[] buffers = {ByteBuffer.allocate(10)}; + ByteBuffer[] buffers = { ByteBuffer.allocate(10) }; FlushReadyWrite flushOperation = mock(FlushReadyWrite.class); context.queueWriteOperation(flushOperation); diff --git a/libs/nio/src/test/java/org/elasticsearch/nio/ChannelFactoryTests.java b/libs/nio/src/test/java/org/elasticsearch/nio/ChannelFactoryTests.java index cf9340d499279..3d5fab74647ba 100644 --- a/libs/nio/src/test/java/org/elasticsearch/nio/ChannelFactoryTests.java +++ b/libs/nio/src/test/java/org/elasticsearch/nio/ChannelFactoryTests.java @@ -128,8 +128,11 @@ public NioSocketChannel createChannel(NioSelector selector, SocketChannel channe } @Override - public NioServerSocketChannel createServerChannel(NioSelector selector, ServerSocketChannel channel, - Config.ServerSocket socketConfig) { + public NioServerSocketChannel createServerChannel( + NioSelector selector, + ServerSocketChannel channel, + Config.ServerSocket socketConfig + ) { return new NioServerSocketChannel(channel); } diff --git a/libs/nio/src/test/java/org/elasticsearch/nio/EventHandlerTests.java b/libs/nio/src/test/java/org/elasticsearch/nio/EventHandlerTests.java index 49f4aaa127322..857fab8017925 100644 --- a/libs/nio/src/test/java/org/elasticsearch/nio/EventHandlerTests.java +++ b/libs/nio/src/test/java/org/elasticsearch/nio/EventHandlerTests.java @@ -210,7 +210,6 @@ public void testPostHandlingWillRemoveWriteIfNecessary() throws IOException { NioSocketChannel channel = mock(NioSocketChannel.class); when(channel.getContext()).thenReturn(context); - assertEquals(SelectionKey.OP_READ | SelectionKey.OP_WRITE, key.interestOps()); handler.postHandling(context); assertEquals(SelectionKey.OP_READ, key.interestOps()); @@ -230,9 +229,12 @@ public void testTaskExceptionWillCallExceptionHandler() throws Exception { private class DoNotRegisterSocketContext extends BytesChannelContext { - - DoNotRegisterSocketContext(NioSocketChannel channel, NioSelector selector, Consumer exceptionHandler, - NioChannelHandler handler) { + DoNotRegisterSocketContext( + NioSocketChannel channel, + NioSelector selector, + Consumer exceptionHandler, + NioChannelHandler handler + ) { super(channel, selector, getSocketConfig(), exceptionHandler, handler, InboundChannelBuffer.allocatingInstance()); } @@ -246,7 +248,6 @@ public void register() { private class DoNotRegisterServerContext extends ServerChannelContext { - @SuppressWarnings("unchecked") DoNotRegisterServerContext(NioServerSocketChannel channel, NioSelector selector, Consumer acceptor) { super(channel, channelFactory, selector, getServerSocketConfig(), acceptor, mock(Consumer.class)); @@ -265,7 +266,17 @@ private static Config.ServerSocket getServerSocketConfig() { } private static Config.Socket getSocketConfig() { - return new Config.Socket(randomBoolean(), randomBoolean(), -1, -1, -1, randomBoolean(), -1, -1, mock(InetSocketAddress.class), - randomBoolean()); + return new Config.Socket( + randomBoolean(), + randomBoolean(), + -1, + -1, + -1, + randomBoolean(), + -1, + -1, + mock(InetSocketAddress.class), + randomBoolean() + ); } } diff --git a/libs/nio/src/test/java/org/elasticsearch/nio/FlushOperationTests.java b/libs/nio/src/test/java/org/elasticsearch/nio/FlushOperationTests.java index 207f7bb63bf6a..c5b46e29edfa6 100644 --- a/libs/nio/src/test/java/org/elasticsearch/nio/FlushOperationTests.java +++ b/libs/nio/src/test/java/org/elasticsearch/nio/FlushOperationTests.java @@ -28,7 +28,7 @@ public void setFields() { } public void testFullyFlushedMarker() { - ByteBuffer[] buffers = {ByteBuffer.allocate(10)}; + ByteBuffer[] buffers = { ByteBuffer.allocate(10) }; FlushOperation writeOp = new FlushOperation(buffers, listener); writeOp.incrementIndex(10); @@ -37,7 +37,7 @@ public void testFullyFlushedMarker() { } public void testPartiallyFlushedMarker() { - ByteBuffer[] buffers = {ByteBuffer.allocate(10)}; + ByteBuffer[] buffers = { ByteBuffer.allocate(10) }; FlushOperation writeOp = new FlushOperation(buffers, listener); writeOp.incrementIndex(5); @@ -46,7 +46,7 @@ public void testPartiallyFlushedMarker() { } public void testMultipleFlushesWithCompositeBuffer() throws IOException { - ByteBuffer[] buffers = {ByteBuffer.allocate(10), ByteBuffer.allocate(15), ByteBuffer.allocate(3)}; + ByteBuffer[] buffers = { ByteBuffer.allocate(10), ByteBuffer.allocate(15), ByteBuffer.allocate(3) }; FlushOperation writeOp = new FlushOperation(buffers, listener); writeOp.incrementIndex(5); diff --git a/libs/nio/src/test/java/org/elasticsearch/nio/NioSelectorGroupTests.java b/libs/nio/src/test/java/org/elasticsearch/nio/NioSelectorGroupTests.java index 95cca774fdabb..d50cbd510691a 100644 --- a/libs/nio/src/test/java/org/elasticsearch/nio/NioSelectorGroupTests.java +++ b/libs/nio/src/test/java/org/elasticsearch/nio/NioSelectorGroupTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.nio; -import org.elasticsearch.core.CheckedRunnable; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.CheckedRunnable; import org.elasticsearch.test.ESTestCase; import java.io.IOException; @@ -27,8 +27,13 @@ public class NioSelectorGroupTests extends ESTestCase { @SuppressWarnings("unchecked") public void setUp() throws Exception { super.setUp(); - nioGroup = new NioSelectorGroup(daemonThreadFactory(Settings.EMPTY, "acceptor"), 1, - daemonThreadFactory(Settings.EMPTY, "selector"), 1, (s) -> new EventHandler(mock(Consumer.class), s)); + nioGroup = new NioSelectorGroup( + daemonThreadFactory(Settings.EMPTY, "acceptor"), + 1, + daemonThreadFactory(Settings.EMPTY, "selector"), + 1, + (s) -> new EventHandler(mock(Consumer.class), s) + ); } @Override @@ -47,11 +52,15 @@ public void testStartAndClose() throws IOException { public void testCannotOperateAfterClose() throws IOException { nioGroup.close(); - IllegalStateException ise = expectThrows(IllegalStateException.class, - () -> nioGroup.bindServerChannel(mock(InetSocketAddress.class), mock(ChannelFactory.class))); + IllegalStateException ise = expectThrows( + IllegalStateException.class, + () -> nioGroup.bindServerChannel(mock(InetSocketAddress.class), mock(ChannelFactory.class)) + ); assertEquals("NioGroup is closed.", ise.getMessage()); - ise = expectThrows(IllegalStateException.class, - () -> nioGroup.openChannel(mock(InetSocketAddress.class), mock(ChannelFactory.class))); + ise = expectThrows( + IllegalStateException.class, + () -> nioGroup.openChannel(mock(InetSocketAddress.class), mock(ChannelFactory.class)) + ); assertEquals("NioGroup is closed.", ise.getMessage()); } @@ -63,9 +72,13 @@ public void testCanCloseTwice() throws IOException { @SuppressWarnings("unchecked") public void testExceptionAtStartIsHandled() throws IOException { RuntimeException ex = new RuntimeException(); - CheckedRunnable ctor = () -> new NioSelectorGroup(r -> {throw ex;}, 1, + CheckedRunnable ctor = () -> new NioSelectorGroup( + r -> { throw ex; }, + 1, daemonThreadFactory(Settings.EMPTY, "selector"), - 1, (s) -> new EventHandler(mock(Consumer.class), s)); + 1, + (s) -> new EventHandler(mock(Consumer.class), s) + ); RuntimeException runtimeException = expectThrows(RuntimeException.class, ctor::run); assertSame(ex, runtimeException); // ctor starts threads. So we are testing that a failure to construct will stop threads. Our thread diff --git a/libs/nio/src/test/java/org/elasticsearch/nio/NioSelectorTests.java b/libs/nio/src/test/java/org/elasticsearch/nio/NioSelectorTests.java index 41d7469d527c0..2b70f85f42bbb 100644 --- a/libs/nio/src/test/java/org/elasticsearch/nio/NioSelectorTests.java +++ b/libs/nio/src/test/java/org/elasticsearch/nio/NioSelectorTests.java @@ -27,8 +27,8 @@ import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.BiConsumer; -import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.isNull; import static org.mockito.ArgumentMatchers.same; import static org.mockito.Mockito.doAnswer; @@ -48,7 +48,7 @@ public class NioSelectorTests extends ESTestCase { private SocketChannelContext channelContext; private ServerChannelContext serverChannelContext; private BiConsumer listener; - private ByteBuffer[] buffers = {ByteBuffer.allocate(1)}; + private ByteBuffer[] buffers = { ByteBuffer.allocate(1) }; private Selector rawSelector; @Before @@ -83,7 +83,7 @@ public void setUp() throws Exception { }).when(eventHandler).handleTask(any()); } - @SuppressWarnings({"unchecked", "rawtypes"}) + @SuppressWarnings({ "unchecked", "rawtypes" }) public void testQueueChannelForClosed() throws IOException { NioChannel channel = mock(NioChannel.class); ChannelContext context = mock(ChannelContext.class); @@ -97,7 +97,7 @@ public void testQueueChannelForClosed() throws IOException { verify(eventHandler).handleClose(context); } - @SuppressWarnings({"unchecked", "rawtypes"}) + @SuppressWarnings({ "unchecked", "rawtypes" }) public void testCloseException() throws IOException, InterruptedException { IOException ioException = new IOException(); NioChannel channel = mock(NioChannel.class); @@ -128,9 +128,7 @@ public void testNioDelayedTasksAreExecuted() throws IOException { public void testTaskExceptionsAreHandled() { RuntimeException taskException = new RuntimeException(); long nanoTime = System.nanoTime() - 1; - Runnable task = () -> { - throw taskException; - }; + Runnable task = () -> { throw taskException; }; selector.getTaskScheduler().scheduleAtRelativeTime(task, nanoTime); doAnswer((a) -> { @@ -144,8 +142,7 @@ public void testTaskExceptionsAreHandled() { public void testDefaultSelectorTimeoutIsUsedIfNoTaskSooner() throws IOException { long delay = new TimeValue(15, TimeUnit.MINUTES).nanos(); - selector.getTaskScheduler().scheduleAtRelativeTime(() -> { - }, System.nanoTime() + delay); + selector.getTaskScheduler().scheduleAtRelativeTime(() -> {}, System.nanoTime() + delay); selector.singleLoop(); verify(rawSelector).select(300); @@ -157,8 +154,7 @@ public void testSelectorTimeoutWillBeReducedIfTaskSooner() throws Exception { assertBusy(() -> { ArgumentCaptor captor = ArgumentCaptor.forClass(Long.class); long delay = new TimeValue(50, TimeUnit.MILLISECONDS).nanos(); - selector.getTaskScheduler().scheduleAtRelativeTime(() -> { - }, System.nanoTime() + delay); + selector.getTaskScheduler().scheduleAtRelativeTime(() -> {}, System.nanoTime() + delay); selector.singleLoop(); verify(rawSelector).select(captor.capture()); assertTrue(captor.getValue() > 0); diff --git a/libs/nio/src/test/java/org/elasticsearch/nio/SocketChannelContextTests.java b/libs/nio/src/test/java/org/elasticsearch/nio/SocketChannelContextTests.java index 6edef2c34abb4..de0eac65ab9bb 100644 --- a/libs/nio/src/test/java/org/elasticsearch/nio/SocketChannelContextTests.java +++ b/libs/nio/src/test/java/org/elasticsearch/nio/SocketChannelContextTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.nio; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.core.Releasable; +import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.test.ESTestCase; import org.junit.Before; import org.mockito.ArgumentCaptor; @@ -113,8 +113,18 @@ public void testRegisterInitiatesConnect() throws IOException { boolean tcpReuseAddress = randomBoolean(); int tcpSendBufferSize = randomIntBetween(1000, 2000); int tcpReceiveBufferSize = randomIntBetween(1000, 2000); - config = new Config.Socket(tcpNoDelay, tcpKeepAlive, tcpKeepIdle, tcpKeepInterval, tcpKeepCount, tcpReuseAddress, tcpSendBufferSize, - tcpReceiveBufferSize, address, isAccepted); + config = new Config.Socket( + tcpNoDelay, + tcpKeepAlive, + tcpKeepIdle, + tcpKeepInterval, + tcpKeepCount, + tcpReuseAddress, + tcpSendBufferSize, + tcpReceiveBufferSize, + address, + isAccepted + ); InboundChannelBuffer buffer = InboundChannelBuffer.allocatingInstance(); TestSocketChannelContext context = new TestSocketChannelContext(channel, selector, exceptionHandler, handler, buffer, config); context.register(); @@ -182,8 +192,18 @@ public void testConnectCanSetSocketOptions() throws IOException { boolean tcpReuseAddress = randomBoolean(); int tcpSendBufferSize = randomIntBetween(1000, 2000); int tcpReceiveBufferSize = randomIntBetween(1000, 2000); - config = new Config.Socket(tcpNoDelay, tcpKeepAlive, tcpKeepIdle, tcpKeepInterval, tcpKeepCount, tcpReuseAddress, tcpSendBufferSize, - tcpReceiveBufferSize, address, false); + config = new Config.Socket( + tcpNoDelay, + tcpKeepAlive, + tcpKeepIdle, + tcpKeepInterval, + tcpKeepCount, + tcpReuseAddress, + tcpSendBufferSize, + tcpReceiveBufferSize, + address, + false + ); InboundChannelBuffer buffer = InboundChannelBuffer.allocatingInstance(); TestSocketChannelContext context = new TestSocketChannelContext(channel, selector, exceptionHandler, handler, buffer, config); doThrow(new SocketException()).doNothing().when(rawSocket).setReuseAddress(tcpReuseAddress); @@ -206,7 +226,7 @@ public void testChannelActiveCallsHandler() throws IOException { public void testWriteFailsIfClosing() { context.closeChannel(); - ByteBuffer[] buffers = {ByteBuffer.wrap(createMessage(10))}; + ByteBuffer[] buffers = { ByteBuffer.wrap(createMessage(10)) }; context.sendMessage(buffers, listener); verify(listener).accept(isNull(Void.class), any(ClosedChannelException.class)); @@ -217,7 +237,7 @@ public void testSendMessageFromDifferentThreadIsQueuedWithSelector() throws Exce when(selector.isOnCurrentThread()).thenReturn(false); - ByteBuffer[] buffers = {ByteBuffer.wrap(createMessage(10))}; + ByteBuffer[] buffers = { ByteBuffer.wrap(createMessage(10)) }; WriteOperation writeOperation = mock(WriteOperation.class); when(handler.createWriteOperation(context, buffers, listener)).thenReturn(writeOperation); context.sendMessage(buffers, listener); @@ -231,7 +251,7 @@ public void testSendMessageFromDifferentThreadIsQueuedWithSelector() throws Exce public void testSendMessageFromSameThreadIsQueuedInChannel() { ArgumentCaptor writeOpCaptor = ArgumentCaptor.forClass(WriteOperation.class); - ByteBuffer[] buffers = {ByteBuffer.wrap(createMessage(10))}; + ByteBuffer[] buffers = { ByteBuffer.wrap(createMessage(10)) }; WriteOperation writeOperation = mock(WriteOperation.class); when(handler.createWriteOperation(context, buffers, listener)).thenReturn(writeOperation); context.sendMessage(buffers, listener); @@ -245,7 +265,7 @@ public void testSendMessageFromSameThreadIsQueuedInChannel() { public void testWriteIsQueuedInChannel() { assertFalse(context.readyForFlush()); - ByteBuffer[] buffer = {ByteBuffer.allocate(10)}; + ByteBuffer[] buffer = { ByteBuffer.allocate(10) }; FlushReadyWrite writeOperation = new FlushReadyWrite(context, buffer, listener); when(handler.writeToBytes(writeOperation)).thenReturn(Collections.singletonList(writeOperation)); context.queueWriteOperation(writeOperation); @@ -261,7 +281,7 @@ public void testHandleReadBytesWillCheckForNewFlushOperations() throws IOExcepti assertTrue(context.readyForFlush()); } - @SuppressWarnings({"unchecked", "varargs"}) + @SuppressWarnings({ "unchecked", "varargs" }) public void testFlushOpsClearedOnClose() throws Exception { try (SocketChannel realChannel = SocketChannel.open()) { when(channel.getRawChannel()).thenReturn(realChannel); @@ -270,11 +290,12 @@ public void testFlushOpsClearedOnClose() throws Exception { assertFalse(context.readyForFlush()); - ByteBuffer[] buffer = {ByteBuffer.allocate(10)}; + ByteBuffer[] buffer = { ByteBuffer.allocate(10) }; WriteOperation writeOperation = mock(WriteOperation.class); BiConsumer listener2 = mock(BiConsumer.class); - when(handler.writeToBytes(writeOperation)).thenReturn(Arrays.asList(new FlushOperation(buffer, listener), - new FlushOperation(buffer, listener2))); + when(handler.writeToBytes(writeOperation)).thenReturn( + Arrays.asList(new FlushOperation(buffer, listener), new FlushOperation(buffer, listener2)) + ); context.queueWriteOperation(writeOperation); assertTrue(context.readyForFlush()); @@ -289,21 +310,21 @@ public void testFlushOpsClearedOnClose() throws Exception { } } - @SuppressWarnings({"unchecked", "varargs"}) + @SuppressWarnings({ "unchecked", "varargs" }) public void testWillPollForFlushOpsToClose() throws Exception { try (SocketChannel realChannel = SocketChannel.open()) { when(channel.getRawChannel()).thenReturn(realChannel); InboundChannelBuffer channelBuffer = InboundChannelBuffer.allocatingInstance(); context = new TestSocketChannelContext(channel, selector, exceptionHandler, handler, channelBuffer); - - ByteBuffer[] buffer = {ByteBuffer.allocate(10)}; + ByteBuffer[] buffer = { ByteBuffer.allocate(10) }; BiConsumer listener2 = mock(BiConsumer.class); assertFalse(context.readyForFlush()); when(channel.isOpen()).thenReturn(true); - when(handler.pollFlushOperations()).thenReturn(Arrays.asList(new FlushOperation(buffer, listener), - new FlushOperation(buffer, listener2))); + when(handler.pollFlushOperations()).thenReturn( + Arrays.asList(new FlushOperation(buffer, listener), new FlushOperation(buffer, listener2)) + ); context.closeFromSelector(); verify(selector, times(1)).executeFailedListener(same(listener), any(ClosedChannelException.class)); @@ -318,8 +339,14 @@ public void testCloseClosesWriteProducer() throws IOException { when(channel.getRawChannel()).thenReturn(realChannel); when(channel.isOpen()).thenReturn(true); InboundChannelBuffer buffer = InboundChannelBuffer.allocatingInstance(); - BytesChannelContext context = new BytesChannelContext(channel, selector, mock(Config.Socket.class), exceptionHandler, handler, - buffer); + BytesChannelContext context = new BytesChannelContext( + channel, + selector, + mock(Config.Socket.class), + exceptionHandler, + handler, + buffer + ); context.closeFromSelector(); verify(handler).close(); } @@ -348,7 +375,7 @@ public void testReadToChannelBufferWillReadAsMuchAsIOBufferAllows() throws IOExc assertEquals(ioBuffer.capacity(), channelBuffer.getIndex()); } - public void testReadToChannelBufferHandlesIOException() throws IOException { + public void testReadToChannelBufferHandlesIOException() throws IOException { when(rawChannel.read(any(ByteBuffer.class))).thenThrow(new IOException()); InboundChannelBuffer channelBuffer = InboundChannelBuffer.allocatingInstance(); @@ -369,7 +396,7 @@ public void testReadToChannelBufferHandlesEOF() throws IOException { public void testFlushBuffersHandlesZeroFlush() throws IOException { when(rawChannel.write(any(ByteBuffer.class))).thenAnswer(consumeBufferAnswer(0)); - ByteBuffer[] buffers = {ByteBuffer.allocate(1023), ByteBuffer.allocate(1023)}; + ByteBuffer[] buffers = { ByteBuffer.allocate(1023), ByteBuffer.allocate(1023) }; FlushOperation flushOperation = new FlushOperation(buffers, listener); context.flushToChannel(flushOperation); assertEquals(2, flushOperation.getBuffersToWrite().length); @@ -386,7 +413,7 @@ public void testFlushBuffersHandlesPartialFlush() throws IOException { } }); - ByteBuffer[] buffers = {ByteBuffer.allocate(1023), ByteBuffer.allocate(1023)}; + ByteBuffer[] buffers = { ByteBuffer.allocate(1023), ByteBuffer.allocate(1023) }; FlushOperation flushOperation = new FlushOperation(buffers, listener); context.flushToChannel(flushOperation); assertEquals(1, flushOperation.getBuffersToWrite().length); @@ -403,7 +430,7 @@ public void testFlushBuffersHandlesFullFlush() throws IOException { } }); - ByteBuffer[] buffers = {ByteBuffer.allocate(1023), ByteBuffer.allocate(1023)}; + ByteBuffer[] buffers = { ByteBuffer.allocate(1023), ByteBuffer.allocate(1023) }; FlushOperation flushOperation = new FlushOperation(buffers, listener); context.flushToChannel(flushOperation); assertTrue(flushOperation.isFullyFlushed()); @@ -412,7 +439,7 @@ public void testFlushBuffersHandlesFullFlush() throws IOException { public void testFlushBuffersHandlesIOException() throws IOException { when(rawChannel.write(any(ByteBuffer.class))).thenThrow(new IOException()); - ByteBuffer[] buffers = {ByteBuffer.allocate(10), ByteBuffer.allocate(10)}; + ByteBuffer[] buffers = { ByteBuffer.allocate(10), ByteBuffer.allocate(10) }; FlushOperation flushOperation = new FlushOperation(buffers, listener); expectThrows(IOException.class, () -> context.flushToChannel(flushOperation)); assertTrue(context.closeNow()); @@ -428,7 +455,7 @@ public void testFlushBuffersHandlesIOExceptionSecondTimeThroughLoop() throws IOE } }); - ByteBuffer[] buffers = {ByteBuffer.allocate(1023), ByteBuffer.allocate(1023)}; + ByteBuffer[] buffers = { ByteBuffer.allocate(1023), ByteBuffer.allocate(1023) }; FlushOperation flushOperation = new FlushOperation(buffers, listener); expectThrows(IOException.class, () -> context.flushToChannel(flushOperation)); assertTrue(context.closeNow()); @@ -437,19 +464,40 @@ public void testFlushBuffersHandlesIOExceptionSecondTimeThroughLoop() throws IOE } private static Config.Socket getSocketConfig() { - return new Config.Socket(randomBoolean(), randomBoolean(), -1, -1, -1, randomBoolean(), -1, -1, mock(InetSocketAddress.class), - randomBoolean()); + return new Config.Socket( + randomBoolean(), + randomBoolean(), + -1, + -1, + -1, + randomBoolean(), + -1, + -1, + mock(InetSocketAddress.class), + randomBoolean() + ); } private static class TestSocketChannelContext extends SocketChannelContext { - private TestSocketChannelContext(NioSocketChannel channel, NioSelector selector, Consumer exceptionHandler, - NioChannelHandler readWriteHandler, InboundChannelBuffer channelBuffer) { + private TestSocketChannelContext( + NioSocketChannel channel, + NioSelector selector, + Consumer exceptionHandler, + NioChannelHandler readWriteHandler, + InboundChannelBuffer channelBuffer + ) { this(channel, selector, exceptionHandler, readWriteHandler, channelBuffer, getSocketConfig()); } - private TestSocketChannelContext(NioSocketChannel channel, NioSelector selector, Consumer exceptionHandler, - NioChannelHandler readWriteHandler, InboundChannelBuffer channelBuffer, Config.Socket config) { + private TestSocketChannelContext( + NioSocketChannel channel, + NioSelector selector, + Consumer exceptionHandler, + NioChannelHandler readWriteHandler, + InboundChannelBuffer channelBuffer, + Config.Socket config + ) { super(channel, selector, config, exceptionHandler, readWriteHandler, channelBuffer); } @@ -461,7 +509,7 @@ public int read() throws IOException { @Override public void flushChannel() throws IOException { - ByteBuffer[] byteBuffers = {ByteBuffer.allocate(10)}; + ByteBuffer[] byteBuffers = { ByteBuffer.allocate(10) }; flushToChannel(new FlushOperation(byteBuffers, (v, e) -> {})); } diff --git a/libs/plugin-classloader/src/main/java/org/elasticsearch/plugins/loader/ExtendedPluginsClassLoader.java b/libs/plugin-classloader/src/main/java/org/elasticsearch/plugins/loader/ExtendedPluginsClassLoader.java index 591a9aaedec87..379cbef9efcbb 100644 --- a/libs/plugin-classloader/src/main/java/org/elasticsearch/plugins/loader/ExtendedPluginsClassLoader.java +++ b/libs/plugin-classloader/src/main/java/org/elasticsearch/plugins/loader/ExtendedPluginsClassLoader.java @@ -42,7 +42,8 @@ protected Class findClass(String name) throws ClassNotFoundException { * Return a new classloader across the parent and extended loaders. */ public static ExtendedPluginsClassLoader create(ClassLoader parent, List extendedLoaders) { - return AccessController.doPrivileged((PrivilegedAction) - () -> new ExtendedPluginsClassLoader(parent, extendedLoaders)); + return AccessController.doPrivileged( + (PrivilegedAction) () -> new ExtendedPluginsClassLoader(parent, extendedLoaders) + ); } } diff --git a/libs/secure-sm/src/main/java/org/elasticsearch/secure_sm/SecureSM.java b/libs/secure-sm/src/main/java/org/elasticsearch/secure_sm/SecureSM.java index 11a88f544948f..a21e2ea764859 100644 --- a/libs/secure-sm/src/main/java/org/elasticsearch/secure_sm/SecureSM.java +++ b/libs/secure-sm/src/main/java/org/elasticsearch/secure_sm/SecureSM.java @@ -97,8 +97,7 @@ public static SecureSM createTestSecureSM() { // intellij test runner (before IDEA version 2019.3) "com\\.intellij\\.rt\\.execution\\.junit\\..*", // intellij test runner (since IDEA version 2019.3) - "com\\.intellij\\.rt\\.junit\\..*" - }; + "com\\.intellij\\.rt\\.junit\\..*" }; // java.security.debug support private static final boolean DEBUG = AccessController.doPrivileged(new PrivilegedAction() { @@ -212,15 +211,12 @@ protected void innerCheckExit(final int status) { AccessController.doPrivileged(new PrivilegedAction() { @Override public Void run() { - final String systemClassName = System.class.getName(), - runtimeClassName = Runtime.class.getName(); + final String systemClassName = System.class.getName(), runtimeClassName = Runtime.class.getName(); String exitMethodHit = null; for (final StackTraceElement se : Thread.currentThread().getStackTrace()) { final String className = se.getClassName(), methodName = se.getMethodName(); - if ( - ("exit".equals(methodName) || "halt".equals(methodName)) && - (systemClassName.equals(className) || runtimeClassName.equals(className)) - ) { + if (("exit".equals(methodName) || "halt".equals(methodName)) + && (systemClassName.equals(className) || runtimeClassName.equals(className))) { exitMethodHit = className + '#' + methodName + '(' + status + ')'; continue; } diff --git a/libs/secure-sm/src/test/java/org/elasticsearch/secure_sm/SecureSMTests.java b/libs/secure-sm/src/test/java/org/elasticsearch/secure_sm/SecureSMTests.java index b375b56c94b8f..6109bde551f52 100644 --- a/libs/secure-sm/src/test/java/org/elasticsearch/secure_sm/SecureSMTests.java +++ b/libs/secure-sm/src/test/java/org/elasticsearch/secure_sm/SecureSMTests.java @@ -49,8 +49,8 @@ public void testClassCanExit() { assertTrue(SecureSM.classCanExit("com.carrotsearch.ant.tasks.junit4.slave.JvmExit", SecureSM.TEST_RUNNER_PACKAGES)); assertTrue(SecureSM.classCanExit("org.eclipse.jdt.internal.junit.runner.RemoteTestRunner", SecureSM.TEST_RUNNER_PACKAGES)); assertTrue(SecureSM.classCanExit("com.intellij.rt.execution.junit.JUnitStarter", SecureSM.TEST_RUNNER_PACKAGES)); - assertTrue(SecureSM.classCanExit("org.elasticsearch.Foo", new String[]{"org.elasticsearch.Foo"})); - assertFalse(SecureSM.classCanExit("org.elasticsearch.Foo", new String[]{"org.elasticsearch.Bar"})); + assertTrue(SecureSM.classCanExit("org.elasticsearch.Foo", new String[] { "org.elasticsearch.Foo" })); + assertFalse(SecureSM.classCanExit("org.elasticsearch.Foo", new String[] { "org.elasticsearch.Bar" })); } public void testCreateThread() throws Exception { diff --git a/libs/secure-sm/src/test/java/org/elasticsearch/secure_sm/ThreadPermissionTests.java b/libs/secure-sm/src/test/java/org/elasticsearch/secure_sm/ThreadPermissionTests.java index 07091bd240f60..733d6bcd8ca64 100644 --- a/libs/secure-sm/src/test/java/org/elasticsearch/secure_sm/ThreadPermissionTests.java +++ b/libs/secure-sm/src/test/java/org/elasticsearch/secure_sm/ThreadPermissionTests.java @@ -20,7 +20,8 @@ public class ThreadPermissionTests extends TestCase { public void testEquals() { assertEquals(new ThreadPermission("modifyArbitraryThread"), new ThreadPermission("modifyArbitraryThread")); assertFalse(new ThreadPermission("modifyArbitraryThread").equals(new AllPermission())); - assertFalse(new ThreadPermission("modifyArbitraryThread").equals(new ThreadPermission("modifyArbitraryThreadGroup"))); } + assertFalse(new ThreadPermission("modifyArbitraryThread").equals(new ThreadPermission("modifyArbitraryThreadGroup"))); + } public void testImplies() { assertTrue(new ThreadPermission("modifyArbitraryThread").implies(new ThreadPermission("modifyArbitraryThread"))); diff --git a/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/CompositeTrustConfig.java b/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/CompositeTrustConfig.java index f27fca593a111..54c6057daa823 100644 --- a/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/CompositeTrustConfig.java +++ b/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/CompositeTrustConfig.java @@ -8,8 +8,6 @@ package org.elasticsearch.common.ssl; -import javax.net.ssl.TrustManagerFactory; -import javax.net.ssl.X509ExtendedTrustManager; import java.nio.file.Path; import java.security.GeneralSecurityException; import java.security.KeyStore; @@ -20,6 +18,9 @@ import java.util.Objects; import java.util.stream.Collectors; +import javax.net.ssl.TrustManagerFactory; +import javax.net.ssl.X509ExtendedTrustManager; + /** * A TrustConfiguration that merges trust anchors from a number of other trust configs to produce a single {@link X509ExtendedTrustManager}. */ @@ -51,16 +52,19 @@ public X509ExtendedTrustManager createTrustManager() { final KeyStore store = KeyStoreUtil.buildTrustStore(trustedIssuers); return KeyStoreUtil.createTrustManager(store, TrustManagerFactory.getDefaultAlgorithm()); } catch (GeneralSecurityException e) { - throw new SslConfigException("Cannot combine trust configurations [" - + configs.stream().map(SslTrustConfig::toString).collect(Collectors.joining(",")) - + "]", - e); + throw new SslConfigException( + "Cannot combine trust configurations [" + + configs.stream().map(SslTrustConfig::toString).collect(Collectors.joining(",")) + + "]", + e + ); } } @Override public Collection getConfiguredCertificates() { - return configs.stream().map(SslTrustConfig::getConfiguredCertificates) + return configs.stream() + .map(SslTrustConfig::getConfiguredCertificates) .flatMap(Collection::stream) .collect(Collectors.toUnmodifiableList()); } diff --git a/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/DefaultJdkTrustConfig.java b/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/DefaultJdkTrustConfig.java index 7ea8f1240c95c..1ed506a8813a3 100644 --- a/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/DefaultJdkTrustConfig.java +++ b/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/DefaultJdkTrustConfig.java @@ -10,8 +10,6 @@ import org.elasticsearch.core.Nullable; -import javax.net.ssl.TrustManagerFactory; -import javax.net.ssl.X509ExtendedTrustManager; import java.io.IOException; import java.nio.file.Path; import java.security.GeneralSecurityException; @@ -21,6 +19,9 @@ import java.util.List; import java.util.function.BiFunction; +import javax.net.ssl.TrustManagerFactory; +import javax.net.ssl.X509ExtendedTrustManager; + /** * This class represents a trust configuration that corresponds to the default trusted CAs of the JDK */ diff --git a/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/DerParser.java b/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/DerParser.java index 7b6f8aa5480bc..65c72181ee2cd 100644 --- a/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/DerParser.java +++ b/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/DerParser.java @@ -17,7 +17,6 @@ package org.elasticsearch.common.ssl; - import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; @@ -86,14 +85,16 @@ public Asn1Object readAsn1Object() throws IOException { // getLength() can return any 32 bit integer, so ensure that a corrupted encoding won't // force us into allocating a very large array if (length > maxAsnObjectLength) { - throw new IOException("Invalid DER: size of ASN.1 object to be parsed appears to be larger than the size of the key file " + - "itself."); + throw new IOException( + "Invalid DER: size of ASN.1 object to be parsed appears to be larger than the size of the key file " + "itself." + ); } byte[] value = new byte[length]; int n = derInputStream.read(value); if (n < length) { - throw new IOException("Invalid DER: stream too short, missing value. " + - "Could only read " + n + " out of " + length + " bytes"); + throw new IOException( + "Invalid DER: stream too short, missing value. " + "Could only read " + n + " out of " + length + " bytes" + ); } return new Asn1Object(tag, length, value); @@ -121,29 +122,23 @@ public Asn1Object readAsn1Object() throws IOException { private int getLength() throws IOException { int i = derInputStream.read(); - if (i == -1) - throw new IOException("Invalid DER: length missing"); + if (i == -1) throw new IOException("Invalid DER: length missing"); // A single byte short length - if ((i & ~0x7F) == 0) - return i; + if ((i & ~0x7F) == 0) return i; int num = i & 0x7F; // We can't handle length longer than 4 bytes - if (i >= 0xFF || num > 4) - throw new IOException("Invalid DER: length field too big (" - + i + ")"); //$NON-NLS-1$ + if (i >= 0xFF || num > 4) throw new IOException("Invalid DER: length field too big (" + i + ")"); //$NON-NLS-2$ byte[] bytes = new byte[num]; int n = derInputStream.read(bytes); - if (n < num) - throw new IOException("Invalid DER: length too short"); + if (n < num) throw new IOException("Invalid DER: length too short"); return new BigInteger(1, bytes).intValue(); } - /** * An ASN.1 TLV. The object is not parsed. It can * only handle integers. @@ -224,8 +219,7 @@ public DerParser getParser() throws IOException { * @return BigInteger */ public BigInteger getInteger() throws IOException { - if (type != Type.INTEGER) - throw new IOException("Invalid DER: object is not integer"); //$NON-NLS-1$ + if (type != Type.INTEGER) throw new IOException("Invalid DER: object is not integer"); //$NON-NLS-1$ return new BigInteger(value); } @@ -300,6 +294,7 @@ public String getOid() throws IOException { } private static final char[] HEX_DIGITS = "0123456789abcdef".toCharArray(); + private static String toHexString(byte[] bytes) { Objects.requireNonNull(bytes); StringBuilder sb = new StringBuilder(2 * bytes.length); diff --git a/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/DiagnosticTrustManager.java b/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/DiagnosticTrustManager.java index dd0d45606911d..7d55f05181ae1 100644 --- a/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/DiagnosticTrustManager.java +++ b/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/DiagnosticTrustManager.java @@ -8,10 +8,6 @@ package org.elasticsearch.common.ssl; -import javax.net.ssl.SSLEngine; -import javax.net.ssl.SSLSession; -import javax.net.ssl.SSLSocket; -import javax.net.ssl.X509ExtendedTrustManager; import java.net.Socket; import java.security.GeneralSecurityException; import java.security.cert.CertificateException; @@ -23,11 +19,15 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import javax.net.ssl.SSLEngine; +import javax.net.ssl.SSLSession; +import javax.net.ssl.SSLSocket; +import javax.net.ssl.X509ExtendedTrustManager; + import static org.elasticsearch.common.ssl.SslDiagnostics.getTrustDiagnosticFailure; public final class DiagnosticTrustManager extends X509ExtendedTrustManager { - /** * This interface exists because the ssl-config library does not depend on log4j, however the whole purpose of this class is to log * diagnostic messages, so it must be provided with a function by which it can do that. @@ -37,7 +37,6 @@ public interface DiagnosticLogger { void warning(String message, GeneralSecurityException cause); } - private final X509ExtendedTrustManager delegate; private final Supplier contextName; private final DiagnosticLogger logger; @@ -53,13 +52,18 @@ public DiagnosticTrustManager(X509ExtendedTrustManager delegate, Supplier cert.getSubjectX500Principal().getName(), List::of, - (List a, List b) -> { - final ArrayList list = new ArrayList<>(a.size() + b.size()); - list.addAll(a); - list.addAll(b); - return list; - })); + .collect( + Collectors.toMap( + cert -> cert.getSubjectX500Principal().getName(), + List::of, + (List a, List b) -> { + final ArrayList list = new ArrayList<>(a.size() + b.size()); + list.addAll(a); + list.addAll(b); + return list; + } + ) + ); } @Override diff --git a/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/KeyStoreUtil.java b/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/KeyStoreUtil.java index bbf895aa2e9d8..333c4c347bbac 100644 --- a/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/KeyStoreUtil.java +++ b/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/KeyStoreUtil.java @@ -10,12 +10,6 @@ import org.elasticsearch.core.Nullable; -import javax.net.ssl.KeyManager; -import javax.net.ssl.KeyManagerFactory; -import javax.net.ssl.TrustManager; -import javax.net.ssl.TrustManagerFactory; -import javax.net.ssl.X509ExtendedKeyManager; -import javax.net.ssl.X509ExtendedTrustManager; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; @@ -37,6 +31,13 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import javax.net.ssl.KeyManager; +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.TrustManager; +import javax.net.ssl.TrustManagerFactory; +import javax.net.ssl.X509ExtendedKeyManager; +import javax.net.ssl.X509ExtendedTrustManager; + /** * A variety of utility methods for working with or constructing {@link KeyStore} instances. */ @@ -95,8 +96,7 @@ public static KeyStore buildKeyStore(Collection certificateChain, P * The provided keystore is modified in place. */ public static KeyStore filter(KeyStore store, Predicate filter) { - stream(store, e -> new SslConfigException("Failed to apply filter to existing keystore", e)) - .filter(filter.negate()) + stream(store, e -> new SslConfigException("Failed to apply filter to existing keystore", e)).filter(filter.negate()) .forEach(e -> e.delete()); return store; } @@ -140,8 +140,8 @@ public static X509ExtendedKeyManager createKeyManager(Certificate[] certificateC /** * Creates a {@link X509ExtendedKeyManager} based on the key material in the provided {@link KeyStore} */ - public static X509ExtendedKeyManager createKeyManager(KeyStore keyStore, char[] password, - String algorithm) throws GeneralSecurityException { + public static X509ExtendedKeyManager createKeyManager(KeyStore keyStore, char[] password, String algorithm) + throws GeneralSecurityException { KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm); kmf.init(keyStore, password); KeyManager[] keyManagers = kmf.getKeyManagers(); @@ -150,8 +150,9 @@ public static X509ExtendedKeyManager createKeyManager(KeyStore keyStore, char[] return (X509ExtendedKeyManager) keyManager; } } - throw new SslConfigException("failed to find a X509ExtendedKeyManager in the key manager factory for [" + algorithm - + "] and keystore [" + keyStore + "]"); + throw new SslConfigException( + "failed to find a X509ExtendedKeyManager in the key manager factory for [" + algorithm + "] and keystore [" + keyStore + "]" + ); } /** @@ -167,8 +168,13 @@ public static X509ExtendedTrustManager createTrustManager(@Nullable KeyStore tru return (X509ExtendedTrustManager) trustManager; } } - throw new SslConfigException("failed to find a X509ExtendedTrustManager in the trust manager factory for [" + algorithm - + "] and truststore [" + trustStore + "]"); + throw new SslConfigException( + "failed to find a X509ExtendedTrustManager in the trust manager factory for [" + + algorithm + + "] and truststore [" + + trustStore + + "]" + ); } /** @@ -182,8 +188,10 @@ public static X509ExtendedTrustManager createTrustManager(Collection stream(KeyStore keyStore, - Function exceptionHandler) { + public static Stream stream( + KeyStore keyStore, + Function exceptionHandler + ) { try { return Collections.list(keyStore.aliases()).stream().map(a -> new KeyStoreEntry(keyStore, a, exceptionHandler)); } catch (KeyStoreException e) { @@ -295,5 +303,4 @@ public void delete() { } - } diff --git a/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/PemKeyConfig.java b/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/PemKeyConfig.java index 1a6608f78bd40..cf7b47575649e 100644 --- a/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/PemKeyConfig.java +++ b/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/PemKeyConfig.java @@ -28,7 +28,6 @@ import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.X509ExtendedKeyManager; - /** * A {@link SslKeyConfig} that reads from PEM formatted paths. */ @@ -93,8 +92,7 @@ public X509ExtendedKeyManager createKeyManager() { final KeyStore keyStore = KeyStoreUtil.buildKeyStore(certificates, privateKey, keyPassword); return KeyStoreUtil.createKeyManager(keyStore, keyPassword, KeyManagerFactory.getDefaultAlgorithm()); } catch (GeneralSecurityException e) { - throw new SslConfigException( - "failed to load a KeyManager for certificate/key pair [" + certPath + "], [" + keyPath + "]", e); + throw new SslConfigException("failed to load a KeyManager for certificate/key pair [" + certPath + "], [" + keyPath + "]", e); } } @@ -161,9 +159,9 @@ public boolean equals(Object o) { return false; } final PemKeyConfig that = (PemKeyConfig) o; - return Objects.equals(this.certificate, that.certificate) && - Objects.equals(this.key, that.key) && - Arrays.equals(this.keyPassword, that.keyPassword); + return Objects.equals(this.certificate, that.certificate) + && Objects.equals(this.key, that.key) + && Arrays.equals(this.keyPassword, that.keyPassword); } @Override diff --git a/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/PemTrustConfig.java b/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/PemTrustConfig.java index d34bea5130225..c89a2aafee8e5 100644 --- a/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/PemTrustConfig.java +++ b/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/PemTrustConfig.java @@ -8,8 +8,6 @@ package org.elasticsearch.common.ssl; -import javax.net.ssl.TrustManagerFactory; -import javax.net.ssl.X509ExtendedTrustManager; import java.io.IOException; import java.io.InputStream; import java.nio.file.Path; @@ -25,6 +23,9 @@ import java.util.Objects; import java.util.stream.Collectors; +import javax.net.ssl.TrustManagerFactory; +import javax.net.ssl.X509ExtendedTrustManager; + /** * A {@link org.elasticsearch.common.ssl.SslTrustConfig} that reads a list of PEM encoded trusted certificates (CAs) from the file * system. @@ -81,8 +82,7 @@ public X509ExtendedTrustManager createTrustManager() { final KeyStore store = KeyStoreUtil.buildTrustStore(certificates); return KeyStoreUtil.createTrustManager(store, TrustManagerFactory.getDefaultAlgorithm()); } catch (GeneralSecurityException e) { - throw new SslConfigException( - "cannot create trust using PEM certificates [" + SslFileUtil.pathsToString(paths) + "]", e); + throw new SslConfigException("cannot create trust using PEM certificates [" + SslFileUtil.pathsToString(paths) + "]", e); } } diff --git a/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/PemUtils.java b/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/PemUtils.java index 151bb6331cf9a..7fa3132b028c7 100644 --- a/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/PemUtils.java +++ b/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/PemUtils.java @@ -10,13 +10,6 @@ import org.elasticsearch.core.CharArrays; -import javax.crypto.Cipher; -import javax.crypto.EncryptedPrivateKeyInfo; -import javax.crypto.SecretKey; -import javax.crypto.SecretKeyFactory; -import javax.crypto.spec.IvParameterSpec; -import javax.crypto.spec.PBEKeySpec; -import javax.crypto.spec.SecretKeySpec; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; @@ -51,14 +44,22 @@ import java.util.Map; import java.util.function.Supplier; +import javax.crypto.Cipher; +import javax.crypto.EncryptedPrivateKeyInfo; +import javax.crypto.SecretKey; +import javax.crypto.SecretKeyFactory; +import javax.crypto.spec.IvParameterSpec; +import javax.crypto.spec.PBEKeySpec; +import javax.crypto.spec.SecretKeySpec; + public final class PemUtils { private static final String PKCS1_HEADER = "-----BEGIN RSA PRIVATE KEY-----"; private static final String PKCS1_FOOTER = "-----END RSA PRIVATE KEY-----"; private static final String OPENSSL_DSA_HEADER = "-----BEGIN DSA PRIVATE KEY-----"; private static final String OPENSSL_DSA_FOOTER = "-----END DSA PRIVATE KEY-----"; - private static final String OPENSSL_DSA_PARAMS_HEADER ="-----BEGIN DSA PARAMETERS-----"; - private static final String OPENSSL_DSA_PARAMS_FOOTER ="-----END DSA PARAMETERS-----"; + private static final String OPENSSL_DSA_PARAMS_HEADER = "-----BEGIN DSA PARAMETERS-----"; + private static final String OPENSSL_DSA_PARAMS_FOOTER = "-----END DSA PARAMETERS-----"; private static final String PKCS8_HEADER = "-----BEGIN PRIVATE KEY-----"; private static final String PKCS8_FOOTER = "-----END PRIVATE KEY-----"; private static final String PKCS8_ENCRYPTED_HEADER = "-----BEGIN ENCRYPTED PRIVATE KEY-----"; @@ -135,8 +136,11 @@ static PrivateKey parsePrivateKey(Path keyPath, Supplier passwordSupplie } else if (OPENSSL_EC_PARAMS_HEADER.equals(line.trim())) { return parseOpenSslEC(removeECHeaders(bReader), passwordSupplier); } else { - throw new SslConfigException("cannot read PEM private key [" + keyPath.toAbsolutePath() - + "] because the file does not contain a supported key format"); + throw new SslConfigException( + "cannot read PEM private key [" + + keyPath.toAbsolutePath() + + "] because the file does not contain a supported key format" + ); } } } @@ -221,7 +225,7 @@ private static PrivateKey parsePKCS8(BufferedReader bReader) throws IOException, * @throws IOException if the algorithm identifier can not be parsed from DER * @throws GeneralSecurityException if the private key can't be generated from the {@link PKCS8EncodedKeySpec} */ - public static PrivateKey parsePKCS8PemString(String pemString) throws IOException, GeneralSecurityException{ + public static PrivateKey parsePKCS8PemString(String pemString) throws IOException, GeneralSecurityException { byte[] keyBytes = Base64.getDecoder().decode(pemString); String keyAlgo = getKeyAlgorithmIdentifier(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(keyAlgo); @@ -353,8 +357,7 @@ private static PrivateKey parseOpenSslDsa(BufferedReader bReader, Supplier pemHeaders, St byte[] keyBytes = Base64.getDecoder().decode(keyContents); String procType = pemHeaders.get("Proc-Type"); if ("4,ENCRYPTED".equals(procType)) { - //We only handle PEM encryption + // We only handle PEM encryption String encryptionParameters = pemHeaders.get("DEK-Info"); if (null == encryptionParameters) { - //malformed pem + // malformed pem throw new IOException("Malformed PEM File, DEK-Info header is missing"); } char[] password = passwordSupplier.get(); @@ -477,8 +480,7 @@ private static byte[] possiblyDecryptPKCS1Key(Map pemHeaders, St * for the cipher * @throws IOException if the DEK-Info PEM header is invalid */ - private static Cipher getCipherFromParameters(String dekHeaderValue, char[] password) throws - GeneralSecurityException, IOException { + private static Cipher getCipherFromParameters(String dekHeaderValue, char[] password) throws GeneralSecurityException, IOException { final String padding = "PKCS5Padding"; final SecretKey encryptionKey; final String[] valueTokens = dekHeaderValue.split(","); @@ -563,8 +565,9 @@ private static byte[] hexStringToByteArray(String hexString) { } return data; } else { - throw new IllegalStateException("Hexadecimal string [" + hexString + - "] has odd length and cannot be converted to a byte array"); + throw new IllegalStateException( + "Hexadecimal string [" + hexString + "] has odd length and cannot be converted to a byte array" + ); } } @@ -575,8 +578,7 @@ private static byte[] hexStringToByteArray(String hexString) { * @return {@link ECPrivateKeySpec} * @throws IOException if the DER encoded key can't be parsed */ - private static ECPrivateKeySpec parseEcDer(byte[] keyBytes) throws IOException, - GeneralSecurityException { + private static ECPrivateKeySpec parseEcDer(byte[] keyBytes) throws IOException, GeneralSecurityException { DerParser parser = new DerParser(keyBytes); DerParser.Asn1Object sequence = parser.readAsn1Object(); parser = sequence.getParser(); @@ -660,8 +662,9 @@ private static String getKeyAlgorithmIdentifier(byte[] keyBytes) throws IOExcept case "1.2.840.10045.2.1": return "EC"; } - throw new GeneralSecurityException("Error parsing key algorithm identifier. Algorithm with OID [" + oidString + - "] is not supported"); + throw new GeneralSecurityException( + "Error parsing key algorithm identifier. Algorithm with OID [" + oidString + "] is not supported" + ); } public static List readCertificates(Collection certPaths) throws CertificateException, IOException { @@ -763,8 +766,9 @@ private static String getEcCurveNameFromOid(String oidString) throws GeneralSecu case "1.3.132.0.39": return "sect571r1"; } - throw new GeneralSecurityException("Error parsing EC named curve identifier. Named curve with OID: " + oidString - + " is not supported"); + throw new GeneralSecurityException( + "Error parsing EC named curve identifier. Named curve with OID: " + oidString + " is not supported" + ); } } diff --git a/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslClientAuthenticationMode.java b/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslClientAuthenticationMode.java index 49d83f98623fb..05b464934523b 100644 --- a/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslClientAuthenticationMode.java +++ b/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslClientAuthenticationMode.java @@ -7,13 +7,14 @@ */ package org.elasticsearch.common.ssl; -import javax.net.ssl.SSLParameters; import java.util.Collections; import java.util.LinkedHashMap; import java.util.Locale; import java.util.Map; import java.util.stream.Collectors; +import javax.net.ssl.SSLParameters; + /** * The client authentication mode that is used for SSL servers. */ @@ -82,8 +83,9 @@ public static SslClientAuthenticationMode parse(String value) { final SslClientAuthenticationMode mode = LOOKUP.get(value.toLowerCase(Locale.ROOT)); if (mode == null) { final String allowedValues = LOOKUP.keySet().stream().collect(Collectors.joining(",")); - throw new SslConfigException("could not resolve ssl client authentication, unknown value [" - + value + "], recognised values are [" + allowedValues + "]"); + throw new SslConfigException( + "could not resolve ssl client authentication, unknown value [" + value + "], recognised values are [" + allowedValues + "]" + ); } return mode; } diff --git a/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslConfiguration.java b/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslConfiguration.java index ec06f629edd65..97e6f53278f72 100644 --- a/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslConfiguration.java +++ b/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslConfiguration.java @@ -8,9 +8,6 @@ package org.elasticsearch.common.ssl; -import javax.net.ssl.SSLContext; -import javax.net.ssl.X509ExtendedKeyManager; -import javax.net.ssl.X509ExtendedTrustManager; import java.nio.file.Path; import java.security.GeneralSecurityException; import java.security.NoSuchAlgorithmException; @@ -25,6 +22,10 @@ import java.util.Objects; import java.util.Set; +import javax.net.ssl.SSLContext; +import javax.net.ssl.X509ExtendedKeyManager; +import javax.net.ssl.X509ExtendedTrustManager; + /** * A object encapsulating all necessary configuration for an SSL context (client or server). * The configuration itself is immutable, but the {@link #getKeyConfig() key config} and @@ -65,9 +66,15 @@ public class SslConfiguration { private final List ciphers; private final List supportedProtocols; - public SslConfiguration(boolean explicitlyConfigured, SslTrustConfig trustConfig, SslKeyConfig keyConfig, - SslVerificationMode verificationMode, SslClientAuthenticationMode clientAuth, - List ciphers, List supportedProtocols) { + public SslConfiguration( + boolean explicitlyConfigured, + SslTrustConfig trustConfig, + SslKeyConfig keyConfig, + SslVerificationMode verificationMode, + SslClientAuthenticationMode clientAuth, + List ciphers, + List supportedProtocols + ) { this.explicitlyConfigured = explicitlyConfigured; if (ciphers == null || ciphers.isEmpty()) { throw new SslConfigException("cannot configure SSL/TLS without any supported cipher suites"); @@ -161,20 +168,28 @@ private String contextProtocol() { return entry.getValue(); } } - throw new SslConfigException("no supported SSL/TLS protocol was found in the configured supported protocols: " - + supportedProtocols); + throw new SslConfigException( + "no supported SSL/TLS protocol was found in the configured supported protocols: " + supportedProtocols + ); } @Override public String toString() { - return getClass().getSimpleName() + '{' + - "trustConfig=" + trustConfig + - ", keyConfig=" + keyConfig + - ", verificationMode=" + verificationMode + - ", clientAuth=" + clientAuth + - ", ciphers=" + ciphers + - ", supportedProtocols=" + supportedProtocols + - '}'; + return getClass().getSimpleName() + + '{' + + "trustConfig=" + + trustConfig + + ", keyConfig=" + + keyConfig + + ", verificationMode=" + + verificationMode + + ", clientAuth=" + + clientAuth + + ", ciphers=" + + ciphers + + ", supportedProtocols=" + + supportedProtocols + + '}'; } @Override @@ -182,12 +197,12 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; final SslConfiguration that = (SslConfiguration) o; - return Objects.equals(this.trustConfig, that.trustConfig) && - Objects.equals(this.keyConfig, that.keyConfig) && - this.verificationMode == that.verificationMode && - this.clientAuth == that.clientAuth && - Objects.equals(this.ciphers, that.ciphers) && - Objects.equals(this.supportedProtocols, that.supportedProtocols); + return Objects.equals(this.trustConfig, that.trustConfig) + && Objects.equals(this.keyConfig, that.keyConfig) + && this.verificationMode == that.verificationMode + && this.clientAuth == that.clientAuth + && Objects.equals(this.ciphers, that.ciphers) + && Objects.equals(this.supportedProtocols, that.supportedProtocols); } @Override diff --git a/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslConfigurationKeys.java b/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslConfigurationKeys.java index a18d59bfc6e2a..7884a417c39c2 100644 --- a/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslConfigurationKeys.java +++ b/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslConfigurationKeys.java @@ -8,13 +8,14 @@ package org.elasticsearch.common.ssl; -import javax.net.ssl.TrustManagerFactory; import java.security.KeyStore; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; +import javax.net.ssl.TrustManagerFactory; + /** * Utility class for handling the standard setting keys for use in SSL configuration. * @@ -138,10 +139,20 @@ private SslConfigurationKeys() { */ public static List getStringKeys() { return Arrays.asList( - VERIFICATION_MODE, CLIENT_AUTH, - TRUSTSTORE_PATH, TRUSTSTORE_LEGACY_PASSWORD, TRUSTSTORE_TYPE, TRUSTSTORE_TYPE, - KEYSTORE_PATH, KEYSTORE_LEGACY_PASSWORD, KEYSTORE_LEGACY_KEY_PASSWORD, KEYSTORE_TYPE, KEYSTORE_ALGORITHM, - CERTIFICATE, KEY, KEY_LEGACY_PASSPHRASE + VERIFICATION_MODE, + CLIENT_AUTH, + TRUSTSTORE_PATH, + TRUSTSTORE_LEGACY_PASSWORD, + TRUSTSTORE_TYPE, + TRUSTSTORE_TYPE, + KEYSTORE_PATH, + KEYSTORE_LEGACY_PASSWORD, + KEYSTORE_LEGACY_KEY_PASSWORD, + KEYSTORE_TYPE, + KEYSTORE_ALGORITHM, + CERTIFICATE, + KEY, + KEY_LEGACY_PASSPHRASE ); } diff --git a/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslConfigurationLoader.java b/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslConfigurationLoader.java index 7b6530683298b..e8d3fbcdc828c 100644 --- a/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslConfigurationLoader.java +++ b/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslConfigurationLoader.java @@ -10,8 +10,6 @@ import org.elasticsearch.jdk.JavaVersion; -import javax.net.ssl.KeyManagerFactory; -import javax.net.ssl.TrustManagerFactory; import java.nio.file.Path; import java.security.KeyStore; import java.util.Arrays; @@ -21,6 +19,9 @@ import java.util.function.Function; import java.util.stream.Collectors; +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.TrustManagerFactory; + import static org.elasticsearch.common.ssl.KeyStoreUtil.inferKeyStoreType; import static org.elasticsearch.common.ssl.SslConfiguration.ORDERED_PROTOCOL_ALGORITHM_MAP; import static org.elasticsearch.common.ssl.SslConfigurationKeys.CERTIFICATE; @@ -60,8 +61,10 @@ public abstract class SslConfigurationLoader { static final List DEFAULT_PROTOCOLS = Collections.unmodifiableList( - ORDERED_PROTOCOL_ALGORITHM_MAP.containsKey("TLSv1.3") ? - Arrays.asList("TLSv1.3", "TLSv1.2", "TLSv1.1") : Arrays.asList("TLSv1.2", "TLSv1.1")); + ORDERED_PROTOCOL_ALGORITHM_MAP.containsKey("TLSv1.3") + ? Arrays.asList("TLSv1.3", "TLSv1.2", "TLSv1.1") + : Arrays.asList("TLSv1.2", "TLSv1.1") + ); private static final List JDK11_CIPHERS = List.of( // TLSv1.3 cipher has PFS, AEAD, hardware support @@ -154,8 +157,9 @@ public abstract class SslConfigurationLoader { "TLS_RSA_WITH_AES_128_CBC_SHA" ); - static final List DEFAULT_CIPHERS = - JavaVersion.current().compareTo(JavaVersion.parse("12")) > -1 ? JDK12_CIPHERS : JDK11_CIPHERS; + static final List DEFAULT_CIPHERS = JavaVersion.current().compareTo(JavaVersion.parse("12")) > -1 + ? JDK12_CIPHERS + : JDK11_CIPHERS; private static final char[] EMPTY_PASSWORD = new char[0]; private final String settingPrefix; @@ -238,7 +242,6 @@ public void setDefaultProtocols(List defaultProtocols) { this.defaultProtocols = defaultProtocols; } - /** * Apply a filter function to any keystore that is loaded. * @see StoreKeyConfig @@ -317,8 +320,9 @@ protected SslTrustConfig buildTrustConfig(Path basePath, SslVerificationMode ver final String trustStorePath = resolveSetting(TRUSTSTORE_PATH, Function.identity(), null); if (certificateAuthorities != null && trustStorePath != null) { - throw new SslConfigException("cannot specify both [" + settingPrefix + CERTIFICATE_AUTHORITIES + "] and [" + - settingPrefix + TRUSTSTORE_PATH + "]"); + throw new SslConfigException( + "cannot specify both [" + settingPrefix + CERTIFICATE_AUTHORITIES + "] and [" + settingPrefix + TRUSTSTORE_PATH + "]" + ); } if (verificationMode.isCertificateVerificationEnabled() == false) { return TrustEverythingConfig.TRUST_EVERYTHING; @@ -350,18 +354,21 @@ public SslKeyConfig buildKeyConfig(Path basePath) { final String keyStorePath = stringSetting(KEYSTORE_PATH); if (certificatePath != null && keyStorePath != null) { - throw new SslConfigException("cannot specify both [" + settingPrefix + CERTIFICATE + "] and [" + - settingPrefix + KEYSTORE_PATH + "]"); + throw new SslConfigException( + "cannot specify both [" + settingPrefix + CERTIFICATE + "] and [" + settingPrefix + KEYSTORE_PATH + "]" + ); } if (certificatePath != null || keyPath != null) { if (keyPath == null) { - throw new SslConfigException("cannot specify [" + settingPrefix + CERTIFICATE + "] without also setting [" + - settingPrefix + KEY + "]"); + throw new SslConfigException( + "cannot specify [" + settingPrefix + CERTIFICATE + "] without also setting [" + settingPrefix + KEY + "]" + ); } if (certificatePath == null) { - throw new SslConfigException("cannot specify [" + settingPrefix + KEYSTORE_PATH + "] without also setting [" + - settingPrefix + CERTIFICATE + "]"); + throw new SslConfigException( + "cannot specify [" + settingPrefix + KEYSTORE_PATH + "] without also setting [" + settingPrefix + CERTIFICATE + "]" + ); } final char[] password = resolvePasswordSetting(KEY_SECURE_PASSPHRASE, KEY_LEGACY_PASSPHRASE); return new PemKeyConfig(certificatePath, keyPath, password, basePath); @@ -400,8 +407,9 @@ private char[] resolvePasswordSetting(String secureSettingKey, String legacySett } } else { if (legacyPassword != null) { - throw new SslConfigException("cannot specify both [" + settingPrefix + secureSettingKey + "] and [" - + settingPrefix + legacySettingKey + "]"); + throw new SslConfigException( + "cannot specify both [" + settingPrefix + secureSettingKey + "] and [" + settingPrefix + legacySettingKey + "]" + ); } else { return securePassword; } diff --git a/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslDiagnostics.java b/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslDiagnostics.java index a3d4ad5086781..e2fc4b65e0a04 100644 --- a/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslDiagnostics.java +++ b/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslDiagnostics.java @@ -10,7 +10,6 @@ import org.elasticsearch.core.Nullable; -import javax.net.ssl.SSLSession; import java.security.cert.CertificateEncodingException; import java.security.cert.CertificateParsingException; import java.security.cert.X509Certificate; @@ -25,8 +24,9 @@ import java.util.stream.Collectors; import java.util.stream.IntStream; -public class SslDiagnostics { +import javax.net.ssl.SSLSession; +public class SslDiagnostics { public static List describeValidHostnames(X509Certificate certificate) { try { @@ -57,7 +57,8 @@ public static List describeValidHostnames(X509Certificate certificate) { } public enum PeerType { - CLIENT, SERVER + CLIENT, + SERVER } private static class IssuerTrust { @@ -158,12 +159,12 @@ boolean isSameCertificate() { "decipherOnly" }; private enum ExtendedKeyUsage { - serverAuth ("1.3.6.1.5.5.7.3.1"), - clientAuth ("1.3.6.1.5.5.7.3.2"), - codeSigning ("1.3.6.1.5.5.7.3.3"), - emailProtection ("1.3.6.1.5.5.7.3.4"), - timeStamping ("1.3.6.1.5.5.7.3.8"), - ocspSigning ("1.3.6.1.5.5.7.3.9"); + serverAuth("1.3.6.1.5.5.7.3.1"), + clientAuth("1.3.6.1.5.5.7.3.2"), + codeSigning("1.3.6.1.5.5.7.3.3"), + emailProtection("1.3.6.1.5.5.7.3.4"), + timeStamping("1.3.6.1.5.5.7.3.8"), + ocspSigning("1.3.6.1.5.5.7.3.9"); private String oid; @@ -186,12 +187,16 @@ public static String decodeOid(String oid) { * @param trustedIssuers A Map of DN to Certificate, for the issuers that were trusted in the context in which this failure occurred * (see {@link javax.net.ssl.X509TrustManager#getAcceptedIssuers()}) */ - public static String getTrustDiagnosticFailure(X509Certificate[] chain, PeerType peerType, SSLSession session, - String contextName, @Nullable Map> trustedIssuers) { + public static String getTrustDiagnosticFailure( + X509Certificate[] chain, + PeerType peerType, + SSLSession session, + String contextName, + @Nullable Map> trustedIssuers + ) { final String peerAddress = Optional.ofNullable(session).map(SSLSession::getPeerHost).orElse(""); - final StringBuilder message = new StringBuilder("failed to establish trust with ") - .append(peerType.name().toLowerCase(Locale.ROOT)) + final StringBuilder message = new StringBuilder("failed to establish trust with ").append(peerType.name().toLowerCase(Locale.ROOT)) .append(" at [") .append(peerAddress) .append("]; "); @@ -237,8 +242,7 @@ public static String getTrustDiagnosticFailure(X509Certificate[] chain, PeerType } if (isSelfIssued(peerCert)) { - message.append("; the certificate is ") - .append(describeSelfIssuedCertificate(peerCert, contextName, trustedIssuers)); + message.append("; the certificate is ").append(describeSelfIssuedCertificate(peerCert, contextName, trustedIssuers)); } else { final String issuerName = peerCert.getIssuerX500Principal().getName(); message.append("; the certificate is issued by [").append(issuerName).append("]"); @@ -246,7 +250,7 @@ public static String getTrustDiagnosticFailure(X509Certificate[] chain, PeerType message.append(" but the ") .append(peerType.name().toLowerCase(Locale.ROOT)) .append(" did not provide a copy of the issuing certificate in the certificate chain") - .append(describeIssuerTrust(contextName, trustedIssuers, peerCert, issuerName)); + .append(describeIssuerTrust(contextName, trustedIssuers, peerCert, issuerName)); } } @@ -281,8 +285,12 @@ public static String getTrustDiagnosticFailure(X509Certificate[] chain, PeerType return message.toString(); } - private static CharSequence describeIssuerTrust(String contextName, @Nullable Map> trustedIssuers, - X509Certificate certificate, String issuerName) { + private static CharSequence describeIssuerTrust( + String contextName, + @Nullable Map> trustedIssuers, + X509Certificate certificate, + String issuerName + ) { if (trustedIssuers == null) { return ""; } @@ -303,16 +311,15 @@ private static CharSequence describeIssuerTrust(String contextName, @Nullable Ma .append(contextName) .append("]) trusts [") .append(trust.issuerCerts.size()) - .append("] ").append(trust.issuerCerts.size() == 1 ? "certificate" : "certificates") + .append("] ") + .append(trust.issuerCerts.size() == 1 ? "certificate" : "certificates") .append(" with subject name [") .append(issuerName) .append("] and ") .append(fingerprintDescription(trust.issuerCerts)) .append(" but the signatures do not match"); } else { - message.append("; this ssl context ([") - .append(contextName) - .append("]) is not configured to trust that issuer"); + message.append("; this ssl context ([").append(contextName).append("]) is not configured to trust that issuer"); if (trustedIssuers.isEmpty()) { message.append(" or any other issuer"); @@ -324,9 +331,7 @@ private static CharSequence describeIssuerTrust(String contextName, @Nullable Ma .append("] with ") .append(fingerprintDescription(trustedIssuers.get(trustedIssuer))); } else { - message.append(" but trusts [") - .append(trustedIssuers.size()) - .append("] other issuers"); + message.append(" but trusts [").append(trustedIssuers.size()).append("] other issuers"); if (trustedIssuers.size() < 10) { // 10 is an arbitrary number, but printing out hundreds of trusted issuers isn't helpful message.append(" ([") @@ -339,16 +344,23 @@ private static CharSequence describeIssuerTrust(String contextName, @Nullable Ma return message; } - private static CharSequence describeSelfIssuedCertificate(X509Certificate certificate, String contextName, - @Nullable Map> trustedIssuers) { + private static CharSequence describeSelfIssuedCertificate( + X509Certificate certificate, + String contextName, + @Nullable Map> trustedIssuers + ) { if (trustedIssuers == null) { return "self-issued"; } final StringBuilder message = new StringBuilder(); final CertificateTrust trust = resolveCertificateTrust(trustedIssuers, certificate); - message.append("self-issued; the [").append(certificate.getIssuerX500Principal().getName()).append("] certificate ") + message.append("self-issued; the [") + .append(certificate.getIssuerX500Principal().getName()) + .append("] certificate ") .append(trust.isTrusted() ? "is" : "is not") - .append(" trusted in this ssl context ([").append(contextName).append("])"); + .append(" trusted in this ssl context ([") + .append(contextName) + .append("])"); if (trust.isTrusted()) { if (trust.isSameCertificate() == false) { if (trust.trustedCertificates.size() == 1) { @@ -466,23 +478,12 @@ private static String extendedKeyUsageDescription(X509Certificate certificate) { } private static Optional generateExtendedKeyUsageDescription(List oids) { - return oids.stream() - .map(ExtendedKeyUsage::decodeOid) - .reduce((x, y) -> x + ", " + y) - .map(str -> "extendedKeyUsage [" + str + "]"); + return oids.stream().map(ExtendedKeyUsage::decodeOid).reduce((x, y) -> x + ", " + y).map(str -> "extendedKeyUsage [" + str + "]"); } private static void addSessionDescription(SSLSession session, StringBuilder message) { - String cipherSuite = Optional.ofNullable(session) - .map(SSLSession::getCipherSuite) - .orElse(""); - String protocol = Optional.ofNullable(session) - .map(SSLSession::getProtocol) - .orElse(""); - message.append("; the session uses cipher suite [") - .append(cipherSuite) - .append("] and protocol [") - .append(protocol) - .append("]"); + String cipherSuite = Optional.ofNullable(session).map(SSLSession::getCipherSuite).orElse(""); + String protocol = Optional.ofNullable(session).map(SSLSession::getProtocol).orElse(""); + message.append("; the session uses cipher suite [").append(cipherSuite).append("] and protocol [").append(protocol).append("]"); } } diff --git a/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslFileUtil.java b/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslFileUtil.java index 2adf5ddc0974d..6a475e4a8d431 100644 --- a/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslFileUtil.java +++ b/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslFileUtil.java @@ -25,10 +25,7 @@ final class SslFileUtil { static String pathsToString(List paths) { - return paths.stream() - .map(Path::toAbsolutePath) - .map(Object::toString) - .collect(Collectors.joining(",")); + return paths.stream().map(Path::toAbsolutePath).map(Object::toString).collect(Collectors.joining(",")); } static SslConfigException ioException(String fileType, List paths, IOException cause) { @@ -87,7 +84,7 @@ static SslConfigException accessControlFailure(String fileType, List paths } else { message += "access to read one or more files is blocked"; } - message += "; SSL resources should be placed in the " ; + message += "; SSL resources should be placed in the "; if (basePath == null) { message += "Elasticsearch config directory"; } else { diff --git a/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslKeyConfig.java b/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslKeyConfig.java index 210f2221089d9..10545329d8440 100644 --- a/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslKeyConfig.java +++ b/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslKeyConfig.java @@ -10,13 +10,14 @@ import org.elasticsearch.core.Tuple; -import javax.net.ssl.X509ExtendedKeyManager; import java.nio.file.Path; import java.security.PrivateKey; import java.security.cert.X509Certificate; import java.util.Collection; import java.util.List; +import javax.net.ssl.X509ExtendedKeyManager; + /** * An interface for building a key manager at runtime. * The method for constructing the key manager is implementation dependent. @@ -58,4 +59,3 @@ default SslTrustConfig asTrustConfig() { } } - diff --git a/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslTrustConfig.java b/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslTrustConfig.java index e6b73583f09e4..d0e594dfc7de0 100644 --- a/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslTrustConfig.java +++ b/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslTrustConfig.java @@ -8,11 +8,12 @@ package org.elasticsearch.common.ssl; -import javax.net.ssl.X509ExtendedTrustManager; import java.nio.file.Path; import java.security.cert.Certificate; import java.util.Collection; +import javax.net.ssl.X509ExtendedTrustManager; + /** * An interface for building a trust manager at runtime. * The method for constructing the trust manager is implementation dependent. @@ -44,4 +45,3 @@ default boolean isSystemDefault() { return false; } } - diff --git a/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslVerificationMode.java b/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslVerificationMode.java index e1b563b21fd4f..9b0f93c8360f6 100644 --- a/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslVerificationMode.java +++ b/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslVerificationMode.java @@ -85,8 +85,13 @@ public static SslVerificationMode parse(String value) { final SslVerificationMode mode = LOOKUP.get(value.toLowerCase(Locale.ROOT)); if (mode == null) { final String allowedValues = LOOKUP.keySet().stream().collect(Collectors.joining(",")); - throw new SslConfigException("could not resolve ssl client verification mode, unknown value [" - + value + "], recognised values are [" + allowedValues + "]"); + throw new SslConfigException( + "could not resolve ssl client verification mode, unknown value [" + + value + + "], recognised values are [" + + allowedValues + + "]" + ); } return mode; } diff --git a/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/StoreKeyConfig.java b/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/StoreKeyConfig.java index 5258fc8e10e4a..e8429b92b8306 100644 --- a/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/StoreKeyConfig.java +++ b/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/StoreKeyConfig.java @@ -11,9 +11,6 @@ import org.elasticsearch.core.Nullable; import org.elasticsearch.core.Tuple; -import javax.net.ssl.KeyManagerFactory; -import javax.net.ssl.TrustManagerFactory; -import javax.net.ssl.X509ExtendedKeyManager; import java.io.IOException; import java.nio.file.Path; import java.security.AccessControlException; @@ -32,6 +29,10 @@ import java.util.function.Function; import java.util.stream.Collectors; +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.TrustManagerFactory; +import javax.net.ssl.X509ExtendedKeyManager; + /** * A {@link SslKeyConfig} that builds a Key Manager from a keystore file. */ @@ -55,8 +56,15 @@ public class StoreKeyConfig implements SslKeyConfig { * @param algorithm The algorithm to use for the Key Manager (see {@link KeyManagerFactory#getAlgorithm()}). * @param configBasePath The base path for configuration files (used for error handling) */ - public StoreKeyConfig(String path, char[] storePassword, String type, @Nullable Function filter, - char[] keyPassword, String algorithm, Path configBasePath) { + public StoreKeyConfig( + String path, + char[] storePassword, + String type, + @Nullable Function filter, + char[] keyPassword, + String algorithm, + Path configBasePath + ) { this.keystorePath = Objects.requireNonNull(path, "Keystore path cannot be null"); this.storePassword = Objects.requireNonNull(storePassword, "Keystore password cannot be null (but may be empty)"); this.type = Objects.requireNonNull(type, "Keystore type cannot be null"); @@ -120,17 +128,15 @@ public List> getKeys(boolean filterKeystore) public Collection getConfiguredCertificates() { final Path path = resolvePath(); final KeyStore keyStore = readKeyStore(path); - return KeyStoreUtil.stream(keyStore, ex -> keystoreException(path, ex)) - .flatMap(entry -> { - final List certificates = new ArrayList<>(); - boolean firstElement = true; - for (X509Certificate certificate : entry.getX509CertificateChain()) { - certificates.add(new StoredCertificate(certificate, keystorePath, type, entry.getAlias(), firstElement)); - firstElement = false; - } - return certificates.stream(); - }) - .collect(Collectors.toUnmodifiableList()); + return KeyStoreUtil.stream(keyStore, ex -> keystoreException(path, ex)).flatMap(entry -> { + final List certificates = new ArrayList<>(); + boolean firstElement = true; + for (X509Certificate certificate : entry.getX509CertificateChain()) { + certificates.add(new StoredCertificate(certificate, keystorePath, type, entry.getAlias(), firstElement)); + firstElement = false; + } + return certificates.stream(); + }).collect(Collectors.toUnmodifiableList()); } @Override diff --git a/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/StoreTrustConfig.java b/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/StoreTrustConfig.java index 47c0c31218e1c..9245730c6f65b 100644 --- a/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/StoreTrustConfig.java +++ b/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/StoreTrustConfig.java @@ -8,7 +8,6 @@ package org.elasticsearch.common.ssl; -import javax.net.ssl.X509ExtendedTrustManager; import java.io.IOException; import java.nio.file.Path; import java.security.AccessControlException; @@ -22,6 +21,8 @@ import java.util.Objects; import java.util.stream.Collectors; +import javax.net.ssl.X509ExtendedTrustManager; + /** * A {@link SslTrustConfig} that builds a Trust Manager from a keystore file. */ @@ -37,7 +38,7 @@ public final class StoreTrustConfig implements SslTrustConfig { * @param path The path to the keystore file * @param password The password for the keystore * @param type The {@link KeyStore#getType() type} of the keystore (typically "PKCS12" or "jks"). - * See {@link KeyStoreUtil#inferKeyStoreType}. + * See {@link KeyStoreUtil#inferKeyStoreType}. * @param algorithm The algorithm to use for the Trust Manager (see {@link javax.net.ssl.TrustManagerFactory#getAlgorithm()}). * @param requireTrustAnchors If true, the truststore will be checked to ensure that it contains at least one valid trust anchor. * @param configBasePath The base path for the configuration directory @@ -64,18 +65,15 @@ private Path resolvePath() { public Collection getConfiguredCertificates() { final Path path = resolvePath(); final KeyStore trustStore = readKeyStore(path); - return KeyStoreUtil.stream(trustStore, ex -> keystoreException(path, ex)) - .map(entry -> { - final X509Certificate certificate = entry.getX509Certificate(); - if (certificate != null) { - final boolean hasKey = entry.isKeyEntry(); - return new StoredCertificate(certificate, this.truststorePath, this.type, entry.getAlias(), hasKey); - } else { - return null; - } - }) - .filter(Objects::nonNull) - .collect(Collectors.toUnmodifiableList()); + return KeyStoreUtil.stream(trustStore, ex -> keystoreException(path, ex)).map(entry -> { + final X509Certificate certificate = entry.getX509Certificate(); + if (certificate != null) { + final boolean hasKey = entry.isKeyEntry(); + return new StoredCertificate(certificate, this.truststorePath, this.type, entry.getAlias(), hasKey); + } else { + return null; + } + }).filter(Objects::nonNull).collect(Collectors.toUnmodifiableList()); } @Override @@ -112,10 +110,10 @@ private SslConfigException keystoreException(Path path, GeneralSecurityException private String getAdditionalErrorDetails() { final String extra; if (password.length == 0) { - extra = "(no password was provided)"; - } else { - extra = "(a keystore password was provided)"; - } + extra = "(no password was provided)"; + } else { + extra = "(a keystore password was provided)"; + } return extra; } diff --git a/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/TrustEverythingConfig.java b/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/TrustEverythingConfig.java index 4936b3f78e8bb..568dbcc58846d 100644 --- a/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/TrustEverythingConfig.java +++ b/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/TrustEverythingConfig.java @@ -8,14 +8,15 @@ package org.elasticsearch.common.ssl; -import javax.net.ssl.SSLEngine; -import javax.net.ssl.X509ExtendedTrustManager; import java.net.Socket; import java.nio.file.Path; import java.security.cert.X509Certificate; import java.util.Collection; import java.util.List; +import javax.net.ssl.SSLEngine; +import javax.net.ssl.X509ExtendedTrustManager; + /** * A {@link SslTrustConfig} that trusts all certificates. Used when {@link SslVerificationMode#isCertificateVerificationEnabled()} is * {@code false}. @@ -35,28 +36,22 @@ private TrustEverythingConfig() { */ private static final X509ExtendedTrustManager TRUST_MANAGER = new X509ExtendedTrustManager() { @Override - public void checkClientTrusted(X509Certificate[] x509Certificates, String s, Socket socket) { - } + public void checkClientTrusted(X509Certificate[] x509Certificates, String s, Socket socket) {} @Override - public void checkServerTrusted(X509Certificate[] x509Certificates, String s, Socket socket) { - } + public void checkServerTrusted(X509Certificate[] x509Certificates, String s, Socket socket) {} @Override - public void checkClientTrusted(X509Certificate[] x509Certificates, String s, SSLEngine sslEngine) { - } + public void checkClientTrusted(X509Certificate[] x509Certificates, String s, SSLEngine sslEngine) {} @Override - public void checkServerTrusted(X509Certificate[] x509Certificates, String s, SSLEngine sslEngine) { - } + public void checkServerTrusted(X509Certificate[] x509Certificates, String s, SSLEngine sslEngine) {} @Override - public void checkClientTrusted(X509Certificate[] x509Certificates, String s) { - } + public void checkClientTrusted(X509Certificate[] x509Certificates, String s) {} @Override - public void checkServerTrusted(X509Certificate[] x509Certificates, String s) { - } + public void checkServerTrusted(X509Certificate[] x509Certificates, String s) {} @Override public X509Certificate[] getAcceptedIssuers() { diff --git a/libs/ssl-config/src/test/java/org/elasticsearch/common/ssl/DefaultJdkTrustConfigTests.java b/libs/ssl-config/src/test/java/org/elasticsearch/common/ssl/DefaultJdkTrustConfigTests.java index 029367d44b1bb..c27285af179d2 100644 --- a/libs/ssl-config/src/test/java/org/elasticsearch/common/ssl/DefaultJdkTrustConfigTests.java +++ b/libs/ssl-config/src/test/java/org/elasticsearch/common/ssl/DefaultJdkTrustConfigTests.java @@ -11,13 +11,14 @@ import org.elasticsearch.test.ESTestCase; import org.junit.Assert; -import javax.net.ssl.X509ExtendedTrustManager; import java.security.cert.X509Certificate; import java.util.Locale; import java.util.Optional; import java.util.function.BiFunction; import java.util.stream.Stream; +import javax.net.ssl.X509ExtendedTrustManager; + import static org.hamcrest.Matchers.emptyArray; import static org.hamcrest.Matchers.emptyIterable; import static org.hamcrest.Matchers.not; diff --git a/libs/ssl-config/src/test/java/org/elasticsearch/common/ssl/PemKeyConfigTests.java b/libs/ssl-config/src/test/java/org/elasticsearch/common/ssl/PemKeyConfigTests.java index dbb37e4521794..95d8900296bfb 100644 --- a/libs/ssl-config/src/test/java/org/elasticsearch/common/ssl/PemKeyConfigTests.java +++ b/libs/ssl-config/src/test/java/org/elasticsearch/common/ssl/PemKeyConfigTests.java @@ -47,7 +47,7 @@ public class PemKeyConfigTests extends ESTestCase { private Path configBasePath; @Before - public void setupPath(){ + public void setupPath() { configBasePath = getDataPath("/certs"); } @@ -122,30 +122,30 @@ public void testBuildKeyConfigUsingCertificateChain() throws Exception { assertThat(keys.get(0).v2().getSubjectDN().toString(), equalTo("CN=cert1")); } - public void testInvertedCertificateChainFailsToCreateKeyManager() throws Exception { - final String ca = "ca1/ca.crt"; - final String cert = "cert1/cert1.crt"; - final String key = "cert1/cert1.key"; - - final Path chain = createTempFile("chain", ".crt"); - // This is (intentionally) the wrong order. It should be cert + ca. - Files.write(chain, Files.readAllBytes(configBasePath.resolve(ca)), StandardOpenOption.APPEND); - Files.write(chain, Files.readAllBytes(configBasePath.resolve(cert)), StandardOpenOption.APPEND); - - final PemKeyConfig keyConfig = new PemKeyConfig(chain.toString(), key, new char[0], configBasePath); - final SslConfigException exception = expectThrows(SslConfigException.class, keyConfig::createKeyManager); - - assertThat(exception.getMessage(), containsString("failed to load a KeyManager")); - final Throwable cause = exception.getCause(); - assertThat(cause, notNullValue()); - if (inFipsJvm()) { - // BC FKS first checks that the key & cert match (they don't because the key is for 'cert1' not 'ca') - assertThat(cause.getMessage(), containsString("RSA keys do not have the same modulus")); - } else { - // SUN PKCS#12 first checks that the chain is correctly structured (it's not, due to the order) - assertThat(cause.getMessage(), containsString("Certificate chain is not valid")); - } - } + public void testInvertedCertificateChainFailsToCreateKeyManager() throws Exception { + final String ca = "ca1/ca.crt"; + final String cert = "cert1/cert1.crt"; + final String key = "cert1/cert1.key"; + + final Path chain = createTempFile("chain", ".crt"); + // This is (intentionally) the wrong order. It should be cert + ca. + Files.write(chain, Files.readAllBytes(configBasePath.resolve(ca)), StandardOpenOption.APPEND); + Files.write(chain, Files.readAllBytes(configBasePath.resolve(cert)), StandardOpenOption.APPEND); + + final PemKeyConfig keyConfig = new PemKeyConfig(chain.toString(), key, new char[0], configBasePath); + final SslConfigException exception = expectThrows(SslConfigException.class, keyConfig::createKeyManager); + + assertThat(exception.getMessage(), containsString("failed to load a KeyManager")); + final Throwable cause = exception.getCause(); + assertThat(cause, notNullValue()); + if (inFipsJvm()) { + // BC FKS first checks that the key & cert match (they don't because the key is for 'cert1' not 'ca') + assertThat(cause.getMessage(), containsString("RSA keys do not have the same modulus")); + } else { + // SUN PKCS#12 first checks that the chain is correctly structured (it's not, due to the order) + assertThat(cause.getMessage(), containsString("Certificate chain is not valid")); + } + } public void testKeyManagerFailsWithIncorrectPassword() throws Exception { final Path cert = getDataPath("/certs/cert2/cert2.crt"); @@ -196,7 +196,7 @@ public void testKeyConfigReloadsFileContents() throws Exception { assertFileNotFound(keyConfig, "certificate", cert); } - private Path[] resolve(String ... names) { + private Path[] resolve(String... names) { return Stream.of(names).map(configBasePath::resolve).toArray(Path[]::new); } @@ -215,10 +215,10 @@ private void assertCertificateAndKey(PemKeyConfig keyConfig, String certDN, Stri assertThat(certificate.getIssuerDN().getName(), is("CN=Test CA 1")); assertThat(certificate.getSubjectDN().getName(), is(certDN)); assertThat(certificate.getSubjectAlternativeNames(), iterableWithSize(2)); - assertThat(certificate.getSubjectAlternativeNames(), containsInAnyOrder( - Arrays.asList(DNS_NAME, "localhost"), - Arrays.asList(IP_NAME, "127.0.0.1") - )); + assertThat( + certificate.getSubjectAlternativeNames(), + containsInAnyOrder(Arrays.asList(DNS_NAME, "localhost"), Arrays.asList(IP_NAME, "127.0.0.1")) + ); for (int i = 0; i < caDN.length; i++) { final X509Certificate ca = chain[i + 1]; diff --git a/libs/ssl-config/src/test/java/org/elasticsearch/common/ssl/PemTrustConfigTests.java b/libs/ssl-config/src/test/java/org/elasticsearch/common/ssl/PemTrustConfigTests.java index eaaa89ac6a568..079651461a9d9 100644 --- a/libs/ssl-config/src/test/java/org/elasticsearch/common/ssl/PemTrustConfigTests.java +++ b/libs/ssl-config/src/test/java/org/elasticsearch/common/ssl/PemTrustConfigTests.java @@ -12,7 +12,6 @@ import org.hamcrest.Matchers; import org.junit.Before; -import javax.net.ssl.X509ExtendedTrustManager; import java.nio.file.Files; import java.nio.file.NoSuchFileException; import java.nio.file.Path; @@ -28,6 +27,8 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import javax.net.ssl.X509ExtendedTrustManager; + public class PemTrustConfigTests extends ESTestCase { private static final String CERTS_DIR = "/certs"; @@ -165,7 +166,7 @@ private byte[] generateRandomByteArrayOfLength(int length) { * ArrayIndexOutOfBoundsException. This check ensures that when we create random stream of bytes we do not create ASN.1 SEQUENCE * followed by zero length which fails the test intermittently. */ - while(checkRandomGeneratedBytesRepresentZeroLengthDerSequenceCausingArrayIndexOutOfBound(bytes)) { + while (checkRandomGeneratedBytesRepresentZeroLengthDerSequenceCausingArrayIndexOutOfBound(bytes)) { bytes = randomByteArrayOfLength(length); } return bytes; diff --git a/libs/ssl-config/src/test/java/org/elasticsearch/common/ssl/SslConfigurationLoaderTests.java b/libs/ssl-config/src/test/java/org/elasticsearch/common/ssl/SslConfigurationLoaderTests.java index 19ffb029974b6..07cff7c180d7f 100644 --- a/libs/ssl-config/src/test/java/org/elasticsearch/common/ssl/SslConfigurationLoaderTests.java +++ b/libs/ssl-config/src/test/java/org/elasticsearch/common/ssl/SslConfigurationLoaderTests.java @@ -8,19 +8,20 @@ package org.elasticsearch.common.ssl; -import org.elasticsearch.jdk.JavaVersion; import org.elasticsearch.common.settings.MockSecureSettings; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.jdk.JavaVersion; import org.elasticsearch.test.ESTestCase; -import javax.net.ssl.KeyManagerFactory; -import javax.net.ssl.TrustManagerFactory; import java.nio.file.Path; import java.util.Arrays; import java.util.List; import java.util.Locale; +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.TrustManagerFactory; + import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasItem; @@ -85,14 +86,14 @@ public void testBasicConfigurationOptions() { } public void testLoadTrustFromPemCAs() { - settings = Settings.builder() - .putList("test.ssl.certificate_authorities", "ca1/ca.crt", "ca2/ca.crt", "ca3/ca.crt") - .build(); + settings = Settings.builder().putList("test.ssl.certificate_authorities", "ca1/ca.crt", "ca2/ca.crt", "ca3/ca.crt").build(); final SslConfiguration configuration = loader.load(certRoot); final SslTrustConfig trustConfig = configuration.getTrustConfig(); assertThat(trustConfig, instanceOf(PemTrustConfig.class)); - assertThat(trustConfig.getDependentFiles(), - containsInAnyOrder(getDataPath("/certs/ca1/ca.crt"), getDataPath("/certs/ca2/ca.crt"), getDataPath("/certs/ca3/ca.crt"))); + assertThat( + trustConfig.getDependentFiles(), + containsInAnyOrder(getDataPath("/certs/ca1/ca.crt"), getDataPath("/certs/ca2/ca.crt"), getDataPath("/certs/ca3/ca.crt")) + ); assertThat(trustConfig.createTrustManager(), notNullValue()); } @@ -160,15 +161,19 @@ public void testLoadKeysFromPemFiles() { final SslConfiguration configuration = loader.load(certRoot); final SslKeyConfig keyConfig = configuration.getKeyConfig(); assertThat(keyConfig, instanceOf(PemKeyConfig.class)); - assertThat(keyConfig.getDependentFiles(), containsInAnyOrder( - getDataPath("/certs/" + certName + "/" + certName + ".crt"), getDataPath("/certs/" + certName + "/" + certName + ".key"))); + assertThat( + keyConfig.getDependentFiles(), + containsInAnyOrder( + getDataPath("/certs/" + certName + "/" + certName + ".crt"), + getDataPath("/certs/" + certName + "/" + certName + ".key") + ) + ); assertThat(keyConfig.createKeyManager(), notNullValue()); } public void testLoadKeysFromPKCS12() { assumeFalse("Can't use JKS/PKCS12 keystores in a FIPS JVM", inFipsJvm()); - final Settings.Builder builder = Settings.builder() - .put("test.ssl.keystore.path", "cert-all/certs.p12"); + final Settings.Builder builder = Settings.builder().put("test.ssl.keystore.path", "cert-all/certs.p12"); if (randomBoolean()) { builder.put("test.ssl.keystore.password", "p12-pass"); } else { @@ -191,8 +196,7 @@ public void testLoadKeysFromPKCS12() { public void testLoadKeysFromJKS() { assumeFalse("Can't use JKS/PKCS12 keystores in a FIPS JVM", inFipsJvm()); - final Settings.Builder builder = Settings.builder() - .put("test.ssl.keystore.path", "cert-all/certs.jks"); + final Settings.Builder builder = Settings.builder().put("test.ssl.keystore.path", "cert-all/certs.jks"); if (randomBoolean()) { builder.put("test.ssl.keystore.password", "jks-pass"); } else { diff --git a/libs/ssl-config/src/test/java/org/elasticsearch/common/ssl/SslConfigurationTests.java b/libs/ssl-config/src/test/java/org/elasticsearch/common/ssl/SslConfigurationTests.java index bb6c4de02fcf3..3eaba0af8810a 100644 --- a/libs/ssl-config/src/test/java/org/elasticsearch/common/ssl/SslConfigurationTests.java +++ b/libs/ssl-config/src/test/java/org/elasticsearch/common/ssl/SslConfigurationTests.java @@ -13,12 +13,13 @@ import org.hamcrest.Matchers; import org.mockito.Mockito; -import javax.net.ssl.SSLContext; import java.nio.file.Path; import java.util.Arrays; import java.util.Collections; import java.util.List; +import javax.net.ssl.SSLContext; + import static org.elasticsearch.common.ssl.SslConfigurationLoader.DEFAULT_CIPHERS; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; @@ -37,8 +38,15 @@ public void testBasicConstruction() { final SslClientAuthenticationMode clientAuth = randomFrom(SslClientAuthenticationMode.values()); final List ciphers = randomSubsetOf(randomIntBetween(1, DEFAULT_CIPHERS.size()), DEFAULT_CIPHERS); final List protocols = randomSubsetOf(randomIntBetween(1, 4), VALID_PROTOCOLS); - final SslConfiguration configuration = - new SslConfiguration(true, trustConfig, keyConfig, verificationMode, clientAuth, ciphers, protocols); + final SslConfiguration configuration = new SslConfiguration( + true, + trustConfig, + keyConfig, + verificationMode, + clientAuth, + ciphers, + protocols + ); assertThat(configuration.getTrustConfig(), is(trustConfig)); assertThat(configuration.getKeyConfig(), is(keyConfig)); @@ -62,39 +70,87 @@ public void testEqualsAndHashCode() { final SslClientAuthenticationMode clientAuth = randomFrom(SslClientAuthenticationMode.values()); final List ciphers = randomSubsetOf(randomIntBetween(1, DEFAULT_CIPHERS.size() - 1), DEFAULT_CIPHERS); final List protocols = randomSubsetOf(randomIntBetween(1, VALID_PROTOCOLS.length - 1), VALID_PROTOCOLS); - final SslConfiguration configuration = - new SslConfiguration(true, trustConfig, keyConfig, verificationMode, clientAuth, ciphers, protocols); - - EqualsHashCodeTestUtils.checkEqualsAndHashCode(configuration, - orig -> new SslConfiguration(true, orig.getTrustConfig(), orig.getKeyConfig(), orig.getVerificationMode(), orig.getClientAuth(), - orig.getCipherSuites(), orig.getSupportedProtocols()), + final SslConfiguration configuration = new SslConfiguration( + true, + trustConfig, + keyConfig, + verificationMode, + clientAuth, + ciphers, + protocols + ); + + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + configuration, + orig -> new SslConfiguration( + true, + orig.getTrustConfig(), + orig.getKeyConfig(), + orig.getVerificationMode(), + orig.getClientAuth(), + orig.getCipherSuites(), + orig.getSupportedProtocols() + ), orig -> { switch (randomIntBetween(1, 4)) { case 1: - return new SslConfiguration(true, orig.getTrustConfig(), orig.getKeyConfig(), + return new SslConfiguration( + true, + orig.getTrustConfig(), + orig.getKeyConfig(), randomValueOtherThan(orig.getVerificationMode(), () -> randomFrom(SslVerificationMode.values())), - orig.getClientAuth(), orig.getCipherSuites(), orig.getSupportedProtocols()); + orig.getClientAuth(), + orig.getCipherSuites(), + orig.getSupportedProtocols() + ); case 2: - return new SslConfiguration(true, orig.getTrustConfig(), orig.getKeyConfig(), orig.getVerificationMode(), + return new SslConfiguration( + true, + orig.getTrustConfig(), + orig.getKeyConfig(), + orig.getVerificationMode(), randomValueOtherThan(orig.getClientAuth(), () -> randomFrom(SslClientAuthenticationMode.values())), - orig.getCipherSuites(), orig.getSupportedProtocols()); + orig.getCipherSuites(), + orig.getSupportedProtocols() + ); case 3: - return new SslConfiguration(true, orig.getTrustConfig(), orig.getKeyConfig(), - orig.getVerificationMode(), orig.getClientAuth(), DEFAULT_CIPHERS, orig.getSupportedProtocols()); + return new SslConfiguration( + true, + orig.getTrustConfig(), + orig.getKeyConfig(), + orig.getVerificationMode(), + orig.getClientAuth(), + DEFAULT_CIPHERS, + orig.getSupportedProtocols() + ); case 4: default: - return new SslConfiguration(true, orig.getTrustConfig(), orig.getKeyConfig(), orig.getVerificationMode(), - orig.getClientAuth(), orig.getCipherSuites(), Arrays.asList(VALID_PROTOCOLS)); + return new SslConfiguration( + true, + orig.getTrustConfig(), + orig.getKeyConfig(), + orig.getVerificationMode(), + orig.getClientAuth(), + orig.getCipherSuites(), + Arrays.asList(VALID_PROTOCOLS) + ); } - }); + } + ); } public void testDependentFiles() { final SslTrustConfig trustConfig = Mockito.mock(SslTrustConfig.class); final SslKeyConfig keyConfig = Mockito.mock(SslKeyConfig.class); - final SslConfiguration configuration = new SslConfiguration(true, trustConfig, keyConfig, - randomFrom(SslVerificationMode.values()), randomFrom(SslClientAuthenticationMode.values()), - DEFAULT_CIPHERS, SslConfigurationLoader.DEFAULT_PROTOCOLS); + final SslConfiguration configuration = new SslConfiguration( + true, + trustConfig, + keyConfig, + randomFrom(SslVerificationMode.values()), + randomFrom(SslClientAuthenticationMode.values()), + DEFAULT_CIPHERS, + SslConfigurationLoader.DEFAULT_PROTOCOLS + ); final Path dir = createTempDir(); final Path file1 = dir.resolve(randomAlphaOfLength(1) + ".pem"); @@ -112,9 +168,15 @@ public void testBuildSslContext() { final SslTrustConfig trustConfig = Mockito.mock(SslTrustConfig.class); final SslKeyConfig keyConfig = Mockito.mock(SslKeyConfig.class); final String protocol = randomFrom(SslConfigurationLoader.DEFAULT_PROTOCOLS); - final SslConfiguration configuration = new SslConfiguration(true, trustConfig, keyConfig, - randomFrom(SslVerificationMode.values()), randomFrom(SslClientAuthenticationMode.values()), - DEFAULT_CIPHERS, Collections.singletonList(protocol)); + final SslConfiguration configuration = new SslConfiguration( + true, + trustConfig, + keyConfig, + randomFrom(SslVerificationMode.values()), + randomFrom(SslClientAuthenticationMode.values()), + DEFAULT_CIPHERS, + Collections.singletonList(protocol) + ); Mockito.when(trustConfig.createTrustManager()).thenReturn(null); Mockito.when(keyConfig.createKeyManager()).thenReturn(null); diff --git a/libs/ssl-config/src/test/java/org/elasticsearch/common/ssl/SslDiagnosticsTests.java b/libs/ssl-config/src/test/java/org/elasticsearch/common/ssl/SslDiagnosticsTests.java index e698ddcfed398..81f8af0ab7963 100644 --- a/libs/ssl-config/src/test/java/org/elasticsearch/common/ssl/SslDiagnosticsTests.java +++ b/libs/ssl-config/src/test/java/org/elasticsearch/common/ssl/SslDiagnosticsTests.java @@ -30,6 +30,7 @@ import java.util.Map; import java.util.stream.Collectors; import java.util.stream.Stream; + import javax.net.ssl.SSLSession; import javax.security.auth.x500.X500Principal; @@ -49,124 +50,193 @@ public void testDiagnosticMessageWhenServerProvidesAFullCertChainThatIsTrusted() X509Certificate[] chain = loadCertChain("cert1/cert1.crt", "ca1/ca.crt"); final SSLSession session = session("192.168.1.1"); final Map> trustIssuers = trust("ca1/ca.crt", "ca2/ca.crt", "ca3/ca.crt"); - final String message = SslDiagnostics.getTrustDiagnosticFailure(chain, SslDiagnostics.PeerType.SERVER, session, - "xpack.http.ssl", trustIssuers); - assertThat(message, Matchers.equalTo("failed to establish trust with server at [192.168.1.1];" + - " the server provided a certificate with subject name [CN=cert1]," + - " fingerprint [3bebe388a66362784afd6c51a9000961a4e10050]," + - " no keyUsage and no extendedKeyUsage;" + - " the session uses cipher suite [TLS_ECDHE_RSA_WITH_RC4_128_SHA] and protocol [SSLv3];" + - " the certificate has subject alternative names [DNS:localhost,IP:127.0.0.1];" + - " the certificate is issued by [CN=Test CA 1];" + - " the certificate is signed by" + - " (subject [CN=Test CA 1] fingerprint [2b7b0416391bdf86502505c23149022d2213dadc] {trusted issuer})" + - " which is self-issued; the [CN=Test CA 1] certificate is trusted in this ssl context ([xpack.http.ssl])")); + final String message = SslDiagnostics.getTrustDiagnosticFailure( + chain, + SslDiagnostics.PeerType.SERVER, + session, + "xpack.http.ssl", + trustIssuers + ); + assertThat( + message, + Matchers.equalTo( + "failed to establish trust with server at [192.168.1.1];" + + " the server provided a certificate with subject name [CN=cert1]," + + " fingerprint [3bebe388a66362784afd6c51a9000961a4e10050]," + + " no keyUsage and no extendedKeyUsage;" + + " the session uses cipher suite [TLS_ECDHE_RSA_WITH_RC4_128_SHA] and protocol [SSLv3];" + + " the certificate has subject alternative names [DNS:localhost,IP:127.0.0.1];" + + " the certificate is issued by [CN=Test CA 1];" + + " the certificate is signed by" + + " (subject [CN=Test CA 1] fingerprint [2b7b0416391bdf86502505c23149022d2213dadc] {trusted issuer})" + + " which is self-issued; the [CN=Test CA 1] certificate is trusted in this ssl context ([xpack.http.ssl])" + ) + ); } public void testDiagnosticMessageWhenServerProvidesAFullCertChainThatIsntTrusted() throws Exception { X509Certificate[] chain = loadCertChain("cert1/cert1.crt", "ca1/ca.crt"); final SSLSession session = session("192.168.1.1"); final Map> trustIssuers = trust("ca2/ca.crt", "ca3/ca.crt"); - final String message = SslDiagnostics.getTrustDiagnosticFailure(chain, SslDiagnostics.PeerType.SERVER, session, - "xpack.http.ssl", trustIssuers); - assertThat(message, Matchers.equalTo("failed to establish trust with server at [192.168.1.1];" + - " the server provided a certificate with subject name [CN=cert1]," + - " fingerprint [3bebe388a66362784afd6c51a9000961a4e10050]," + - " no keyUsage and no extendedKeyUsage;" + - " the session uses cipher suite [TLS_ECDHE_RSA_WITH_RC4_128_SHA] and protocol [SSLv3];" + - " the certificate has subject alternative names [DNS:localhost,IP:127.0.0.1];" + - " the certificate is issued by [CN=Test CA 1];" + - " the certificate is signed by (subject [CN=Test CA 1] fingerprint [2b7b0416391bdf86502505c23149022d2213dadc])" + - " which is self-issued; the [CN=Test CA 1] certificate is not trusted in this ssl context ([xpack.http.ssl])")); + final String message = SslDiagnostics.getTrustDiagnosticFailure( + chain, + SslDiagnostics.PeerType.SERVER, + session, + "xpack.http.ssl", + trustIssuers + ); + assertThat( + message, + Matchers.equalTo( + "failed to establish trust with server at [192.168.1.1];" + + " the server provided a certificate with subject name [CN=cert1]," + + " fingerprint [3bebe388a66362784afd6c51a9000961a4e10050]," + + " no keyUsage and no extendedKeyUsage;" + + " the session uses cipher suite [TLS_ECDHE_RSA_WITH_RC4_128_SHA] and protocol [SSLv3];" + + " the certificate has subject alternative names [DNS:localhost,IP:127.0.0.1];" + + " the certificate is issued by [CN=Test CA 1];" + + " the certificate is signed by (subject [CN=Test CA 1] fingerprint [2b7b0416391bdf86502505c23149022d2213dadc])" + + " which is self-issued; the [CN=Test CA 1] certificate is not trusted in this ssl context ([xpack.http.ssl])" + ) + ); } public void testDiagnosticMessageWithPartialChainAndUnknownTrustedIssuers() throws Exception { X509Certificate[] chain = loadCertChain("cert1/cert1.crt"); final SSLSession session = session("192.168.1.1"); final Map> trustIssuers = null; - final String message = SslDiagnostics.getTrustDiagnosticFailure(chain, SslDiagnostics.PeerType.SERVER, session, - "xpack.http.ssl", trustIssuers); - assertThat(message, Matchers.equalTo("failed to establish trust with server at [192.168.1.1];" + - " the server provided a certificate with subject name [CN=cert1]," + - " fingerprint [3bebe388a66362784afd6c51a9000961a4e10050]," + - " no keyUsage and no extendedKeyUsage;" + - " the session uses cipher suite [TLS_ECDHE_RSA_WITH_RC4_128_SHA] and protocol [SSLv3];" + - " the certificate has subject alternative names [DNS:localhost,IP:127.0.0.1];" + - " the certificate is issued by [CN=Test CA 1]" + - " but the server did not provide a copy of the issuing certificate in the certificate chain")); + final String message = SslDiagnostics.getTrustDiagnosticFailure( + chain, + SslDiagnostics.PeerType.SERVER, + session, + "xpack.http.ssl", + trustIssuers + ); + assertThat( + message, + Matchers.equalTo( + "failed to establish trust with server at [192.168.1.1];" + + " the server provided a certificate with subject name [CN=cert1]," + + " fingerprint [3bebe388a66362784afd6c51a9000961a4e10050]," + + " no keyUsage and no extendedKeyUsage;" + + " the session uses cipher suite [TLS_ECDHE_RSA_WITH_RC4_128_SHA] and protocol [SSLv3];" + + " the certificate has subject alternative names [DNS:localhost,IP:127.0.0.1];" + + " the certificate is issued by [CN=Test CA 1]" + + " but the server did not provide a copy of the issuing certificate in the certificate chain" + ) + ); } - public void testDiagnosticMessageWithFullChainAndUnknownTrustedIssuers() throws Exception { X509Certificate[] chain = loadCertChain("cert1/cert1.crt", "ca1/ca.crt"); final SSLSession session = session("192.168.1.1"); final Map> trustIssuers = null; - final String message = SslDiagnostics.getTrustDiagnosticFailure(chain, SslDiagnostics.PeerType.SERVER, session, - "xpack.http.ssl", trustIssuers); - assertThat(message, Matchers.equalTo("failed to establish trust with server at [192.168.1.1];" + - " the server provided a certificate with subject name [CN=cert1]," + - " fingerprint [3bebe388a66362784afd6c51a9000961a4e10050]," + - " no keyUsage and no extendedKeyUsage;" + - " the session uses cipher suite [TLS_ECDHE_RSA_WITH_RC4_128_SHA] and protocol [SSLv3];" + - " the certificate has subject alternative names [DNS:localhost,IP:127.0.0.1];" + - " the certificate is issued by [CN=Test CA 1];" + - " the certificate is signed by (subject [CN=Test CA 1] fingerprint [2b7b0416391bdf86502505c23149022d2213dadc])" + - " which is self-issued")); + final String message = SslDiagnostics.getTrustDiagnosticFailure( + chain, + SslDiagnostics.PeerType.SERVER, + session, + "xpack.http.ssl", + trustIssuers + ); + assertThat( + message, + Matchers.equalTo( + "failed to establish trust with server at [192.168.1.1];" + + " the server provided a certificate with subject name [CN=cert1]," + + " fingerprint [3bebe388a66362784afd6c51a9000961a4e10050]," + + " no keyUsage and no extendedKeyUsage;" + + " the session uses cipher suite [TLS_ECDHE_RSA_WITH_RC4_128_SHA] and protocol [SSLv3];" + + " the certificate has subject alternative names [DNS:localhost,IP:127.0.0.1];" + + " the certificate is issued by [CN=Test CA 1];" + + " the certificate is signed by (subject [CN=Test CA 1] fingerprint [2b7b0416391bdf86502505c23149022d2213dadc])" + + " which is self-issued" + ) + ); } public void testDiagnosticMessageWhenServerFullCertChainIsntTrustedButMimicIssuerExists() throws Exception { X509Certificate[] chain = loadCertChain("cert1/cert1.crt", "ca1/ca.crt"); final SSLSession session = session("192.168.1.1"); final Map> trustIssuers = trust("ca1-b/ca.crt", "ca2/ca.crt", "ca3/ca.crt"); - final String message = SslDiagnostics.getTrustDiagnosticFailure(chain, SslDiagnostics.PeerType.SERVER, session, - "xpack.http.ssl", trustIssuers); - assertThat(message, Matchers.equalTo("failed to establish trust with server at [192.168.1.1];" + - " the server provided a certificate with subject name [CN=cert1]," + - " fingerprint [3bebe388a66362784afd6c51a9000961a4e10050]," + - " no keyUsage and no extendedKeyUsage;" + - " the session uses cipher suite [TLS_ECDHE_RSA_WITH_RC4_128_SHA] and protocol [SSLv3];" + - " the certificate has subject alternative names [DNS:localhost,IP:127.0.0.1];" + - " the certificate is issued by [CN=Test CA 1];" + - " the certificate is signed by (subject [CN=Test CA 1] fingerprint [2b7b0416391bdf86502505c23149022d2213dadc])" + - " which is self-issued; the [CN=Test CA 1] certificate is not trusted in this ssl context ([xpack.http.ssl]);" + - " this ssl context does trust a certificate with subject [CN=Test CA 1]" + - " but the trusted certificate has fingerprint [b095bf2526be20783e1f26dfd69c7aae910e3663]")); + final String message = SslDiagnostics.getTrustDiagnosticFailure( + chain, + SslDiagnostics.PeerType.SERVER, + session, + "xpack.http.ssl", + trustIssuers + ); + assertThat( + message, + Matchers.equalTo( + "failed to establish trust with server at [192.168.1.1];" + + " the server provided a certificate with subject name [CN=cert1]," + + " fingerprint [3bebe388a66362784afd6c51a9000961a4e10050]," + + " no keyUsage and no extendedKeyUsage;" + + " the session uses cipher suite [TLS_ECDHE_RSA_WITH_RC4_128_SHA] and protocol [SSLv3];" + + " the certificate has subject alternative names [DNS:localhost,IP:127.0.0.1];" + + " the certificate is issued by [CN=Test CA 1];" + + " the certificate is signed by (subject [CN=Test CA 1] fingerprint [2b7b0416391bdf86502505c23149022d2213dadc])" + + " which is self-issued; the [CN=Test CA 1] certificate is not trusted in this ssl context ([xpack.http.ssl]);" + + " this ssl context does trust a certificate with subject [CN=Test CA 1]" + + " but the trusted certificate has fingerprint [b095bf2526be20783e1f26dfd69c7aae910e3663]" + ) + ); } public void testDiagnosticMessageWhenServerProvidesEndCertificateOnlyAndTheCertAuthIsTrusted() throws Exception { X509Certificate[] chain = loadCertChain("cert1/cert1.crt"); final SSLSession session = session("192.168.1.1"); final Map> trustIssuers = trust("ca1/ca.crt", "ca2/ca.crt", "ca3/ca.crt"); - final String message = SslDiagnostics.getTrustDiagnosticFailure(chain, SslDiagnostics.PeerType.SERVER, session, - "xpack.http.ssl", trustIssuers); - assertThat(message, Matchers.equalTo("failed to establish trust with server at [192.168.1.1];" + - " the server provided a certificate with subject name [CN=cert1]," + - " fingerprint [3bebe388a66362784afd6c51a9000961a4e10050]," + - " no keyUsage and no extendedKeyUsage;" + - " the session uses cipher suite [TLS_ECDHE_RSA_WITH_RC4_128_SHA] and protocol [SSLv3];" + - " the certificate has subject alternative names [DNS:localhost,IP:127.0.0.1];" + - " the certificate is issued by [CN=Test CA 1]" + - " but the server did not provide a copy of the issuing certificate in the certificate chain;" + - " the issuing certificate with fingerprint [2b7b0416391bdf86502505c23149022d2213dadc]" + - " is trusted in this ssl context ([xpack.http.ssl])")); + final String message = SslDiagnostics.getTrustDiagnosticFailure( + chain, + SslDiagnostics.PeerType.SERVER, + session, + "xpack.http.ssl", + trustIssuers + ); + assertThat( + message, + Matchers.equalTo( + "failed to establish trust with server at [192.168.1.1];" + + " the server provided a certificate with subject name [CN=cert1]," + + " fingerprint [3bebe388a66362784afd6c51a9000961a4e10050]," + + " no keyUsage and no extendedKeyUsage;" + + " the session uses cipher suite [TLS_ECDHE_RSA_WITH_RC4_128_SHA] and protocol [SSLv3];" + + " the certificate has subject alternative names [DNS:localhost,IP:127.0.0.1];" + + " the certificate is issued by [CN=Test CA 1]" + + " but the server did not provide a copy of the issuing certificate in the certificate chain;" + + " the issuing certificate with fingerprint [2b7b0416391bdf86502505c23149022d2213dadc]" + + " is trusted in this ssl context ([xpack.http.ssl])" + ) + ); } public void testDiagnosticMessageWhenServerProvidesEndCertificateOnlyButTheCertAuthIsNotTrusted() throws Exception { X509Certificate[] chain = loadCertChain("cert1/cert1.crt"); final SSLSession session = session("192.168.1.1"); final Map> trustIssuers = trust("ca2/ca.crt", "ca3/ca.crt"); - final String message = SslDiagnostics.getTrustDiagnosticFailure(chain, SslDiagnostics.PeerType.SERVER, session, - "xpack.http.ssl", trustIssuers); - assertThat(message, Matchers.equalTo("failed to establish trust with server at [192.168.1.1];" + - " the server provided a certificate with subject name [CN=cert1]," + - " fingerprint [3bebe388a66362784afd6c51a9000961a4e10050]," + - " no keyUsage and no extendedKeyUsage;" + - " the session uses cipher suite [TLS_ECDHE_RSA_WITH_RC4_128_SHA] and protocol [SSLv3];" + - " the certificate has subject alternative names [DNS:localhost,IP:127.0.0.1];" + - " the certificate is issued by [CN=Test CA 1]" + - " but the server did not provide a copy of the issuing certificate in the certificate chain;" + - " this ssl context ([xpack.http.ssl]) is not configured to trust that issuer" + - " but trusts [2] other issuers ([CN=Test CA 2, CN=Test CA 3])")); + final String message = SslDiagnostics.getTrustDiagnosticFailure( + chain, + SslDiagnostics.PeerType.SERVER, + session, + "xpack.http.ssl", + trustIssuers + ); + assertThat( + message, + Matchers.equalTo( + "failed to establish trust with server at [192.168.1.1];" + + " the server provided a certificate with subject name [CN=cert1]," + + " fingerprint [3bebe388a66362784afd6c51a9000961a4e10050]," + + " no keyUsage and no extendedKeyUsage;" + + " the session uses cipher suite [TLS_ECDHE_RSA_WITH_RC4_128_SHA] and protocol [SSLv3];" + + " the certificate has subject alternative names [DNS:localhost,IP:127.0.0.1];" + + " the certificate is issued by [CN=Test CA 1]" + + " but the server did not provide a copy of the issuing certificate in the certificate chain;" + + " this ssl context ([xpack.http.ssl]) is not configured to trust that issuer" + + " but trusts [2] other issuers ([CN=Test CA 2, CN=Test CA 3])" + ) + ); } public void testDiagnosticMessageWhenServerTrustsManyCAs() throws Exception { @@ -178,35 +248,57 @@ public void testDiagnosticMessageWhenServerTrustsManyCAs() throws Exception { for (int i = 0; i < numberOfCAs; i++) { trustIssuers.put("CN=Authority-" + i + ",OU=security,DC=example,DC=net", randomList(1, 3, () -> dummyCa)); } - final String message = SslDiagnostics.getTrustDiagnosticFailure(chain, SslDiagnostics.PeerType.CLIENT, session, - "xpack.security.http.ssl", trustIssuers); - assertThat(message, Matchers.equalTo("failed to establish trust with client at [192.168.1.2];" + - " the client provided a certificate with subject name [CN=cert1]," + - " fingerprint [3bebe388a66362784afd6c51a9000961a4e10050]," + - " no keyUsage and no extendedKeyUsage;" + - " the session uses cipher suite [TLS_ECDHE_RSA_WITH_RC4_128_SHA] and protocol [SSLv3];" + - " the certificate is issued by [CN=Test CA 1]" + - " but the client did not provide a copy of the issuing certificate in the certificate chain;" + - " this ssl context ([xpack.security.http.ssl]) is not configured to trust that issuer" + - " but trusts [" + numberOfCAs + "] other issuers")); + final String message = SslDiagnostics.getTrustDiagnosticFailure( + chain, + SslDiagnostics.PeerType.CLIENT, + session, + "xpack.security.http.ssl", + trustIssuers + ); + assertThat( + message, + Matchers.equalTo( + "failed to establish trust with client at [192.168.1.2];" + + " the client provided a certificate with subject name [CN=cert1]," + + " fingerprint [3bebe388a66362784afd6c51a9000961a4e10050]," + + " no keyUsage and no extendedKeyUsage;" + + " the session uses cipher suite [TLS_ECDHE_RSA_WITH_RC4_128_SHA] and protocol [SSLv3];" + + " the certificate is issued by [CN=Test CA 1]" + + " but the client did not provide a copy of the issuing certificate in the certificate chain;" + + " this ssl context ([xpack.security.http.ssl]) is not configured to trust that issuer" + + " but trusts [" + + numberOfCAs + + "] other issuers" + ) + ); } public void testDiagnosticMessageWhenServerProvidesEndCertificateOnlyWithMimicIssuer() throws Exception { X509Certificate[] chain = loadCertChain("cert1/cert1.crt"); final SSLSession session = session("192.168.1.1"); final Map> trustIssuers = trust("ca1-b/ca.crt", "ca2/ca.crt", "ca3/ca.crt"); - final String message = SslDiagnostics.getTrustDiagnosticFailure(chain, SslDiagnostics.PeerType.SERVER, session, - "xpack.http.ssl", trustIssuers); - assertThat(message, Matchers.equalTo("failed to establish trust with server at [192.168.1.1];" + - " the server provided a certificate with subject name [CN=cert1]," + - " fingerprint [3bebe388a66362784afd6c51a9000961a4e10050]," + - " no keyUsage and no extendedKeyUsage;" + - " the session uses cipher suite [TLS_ECDHE_RSA_WITH_RC4_128_SHA] and protocol [SSLv3];" + - " the certificate has subject alternative names [DNS:localhost,IP:127.0.0.1];" + - " the certificate is issued by [CN=Test CA 1]" + - " but the server did not provide a copy of the issuing certificate in the certificate chain;" + - " this ssl context ([xpack.http.ssl]) trusts [1] certificate with subject name [CN=Test CA 1]" + - " and fingerprint [b095bf2526be20783e1f26dfd69c7aae910e3663] but the signatures do not match")); + final String message = SslDiagnostics.getTrustDiagnosticFailure( + chain, + SslDiagnostics.PeerType.SERVER, + session, + "xpack.http.ssl", + trustIssuers + ); + assertThat( + message, + Matchers.equalTo( + "failed to establish trust with server at [192.168.1.1];" + + " the server provided a certificate with subject name [CN=cert1]," + + " fingerprint [3bebe388a66362784afd6c51a9000961a4e10050]," + + " no keyUsage and no extendedKeyUsage;" + + " the session uses cipher suite [TLS_ECDHE_RSA_WITH_RC4_128_SHA] and protocol [SSLv3];" + + " the certificate has subject alternative names [DNS:localhost,IP:127.0.0.1];" + + " the certificate is issued by [CN=Test CA 1]" + + " but the server did not provide a copy of the issuing certificate in the certificate chain;" + + " this ssl context ([xpack.http.ssl]) trusts [1] certificate with subject name [CN=Test CA 1]" + + " and fingerprint [b095bf2526be20783e1f26dfd69c7aae910e3663] but the signatures do not match" + ) + ); } public void testDiagnosticMessageWhenServerProvidesEndCertificateWithMultipleMimicIssuers() throws Exception { @@ -214,159 +306,290 @@ public void testDiagnosticMessageWhenServerProvidesEndCertificateWithMultipleMim final SSLSession session = session("192.168.1.9"); final X509Certificate ca1b = loadCertificate("ca1-b/ca.crt"); final Map> trustIssuers = trust(ca1b, cloneCertificateAsMock(ca1b)); - final String message = SslDiagnostics.getTrustDiagnosticFailure(chain, SslDiagnostics.PeerType.SERVER, session, - "xpack.http.ssl", trustIssuers); - assertThat(message, Matchers.equalTo("failed to establish trust with server at [192.168.1.9];" + - " the server provided a certificate with subject name [CN=cert1]," + - " fingerprint [3bebe388a66362784afd6c51a9000961a4e10050]," + - " no keyUsage and no extendedKeyUsage;" + - " the session uses cipher suite [TLS_ECDHE_RSA_WITH_RC4_128_SHA] and protocol [SSLv3];" + - " the certificate has subject alternative names [DNS:localhost,IP:127.0.0.1];" + - " the certificate is issued by [CN=Test CA 1]" + - " but the server did not provide a copy of the issuing certificate in the certificate chain;" + - " this ssl context ([xpack.http.ssl]) trusts [2] certificates with subject name [CN=Test CA 1]" + - " and fingerprint [b095bf2526be20783e1f26dfd69c7aae910e3663], fingerprint [" + MOCK_FINGERPRINT_1 + "]" + - " but the signatures do not match")); + final String message = SslDiagnostics.getTrustDiagnosticFailure( + chain, + SslDiagnostics.PeerType.SERVER, + session, + "xpack.http.ssl", + trustIssuers + ); + assertThat( + message, + Matchers.equalTo( + "failed to establish trust with server at [192.168.1.9];" + + " the server provided a certificate with subject name [CN=cert1]," + + " fingerprint [3bebe388a66362784afd6c51a9000961a4e10050]," + + " no keyUsage and no extendedKeyUsage;" + + " the session uses cipher suite [TLS_ECDHE_RSA_WITH_RC4_128_SHA] and protocol [SSLv3];" + + " the certificate has subject alternative names [DNS:localhost,IP:127.0.0.1];" + + " the certificate is issued by [CN=Test CA 1]" + + " but the server did not provide a copy of the issuing certificate in the certificate chain;" + + " this ssl context ([xpack.http.ssl]) trusts [2] certificates with subject name [CN=Test CA 1]" + + " and fingerprint [b095bf2526be20783e1f26dfd69c7aae910e3663], fingerprint [" + + MOCK_FINGERPRINT_1 + + "]" + + " but the signatures do not match" + ) + ); } public void testDiagnosticMessageWhenServerProvidePartialChainFromTrustedCA() throws Exception { - final X509Certificate rootCA = mockCertificateWithIssuer("CN=root-ca,DC=example,DC=com", MOCK_ENCODING_1, - Collections.emptyList(), null); - final X509Certificate issuingCA = mockCertificateWithIssuer("CN=issuing-ca,DC=example,DC=com", MOCK_ENCODING_2, - Collections.emptyList(), rootCA); - final X509Certificate localCA = mockCertificateWithIssuer("CN=ca,OU=windows,DC=example,DC=com", MOCK_ENCODING_3, - Collections.emptyList(), issuingCA); - final X509Certificate endCert = mockCertificateWithIssuer("CN=elastic1,OU=windows,DC=example,DC=com", MOCK_ENCODING_4, - Collections.emptyList(), localCA); + final X509Certificate rootCA = mockCertificateWithIssuer( + "CN=root-ca,DC=example,DC=com", + MOCK_ENCODING_1, + Collections.emptyList(), + null + ); + final X509Certificate issuingCA = mockCertificateWithIssuer( + "CN=issuing-ca,DC=example,DC=com", + MOCK_ENCODING_2, + Collections.emptyList(), + rootCA + ); + final X509Certificate localCA = mockCertificateWithIssuer( + "CN=ca,OU=windows,DC=example,DC=com", + MOCK_ENCODING_3, + Collections.emptyList(), + issuingCA + ); + final X509Certificate endCert = mockCertificateWithIssuer( + "CN=elastic1,OU=windows,DC=example,DC=com", + MOCK_ENCODING_4, + Collections.emptyList(), + localCA + ); final X509Certificate[] chain = { endCert, localCA, issuingCA }; final SSLSession session = session("192.168.1.5"); final Map> trustIssuers = trust(issuingCA, rootCA); - final String message = SslDiagnostics.getTrustDiagnosticFailure(chain, SslDiagnostics.PeerType.SERVER, session, - "xpack.security.authc.realms.ldap.ldap1.ssl", trustIssuers); - assertThat(message, Matchers.equalTo("failed to establish trust with server at [192.168.1.5];" + - " the server provided a certificate with subject name [CN=elastic1,OU=windows,DC=example,DC=com]" + - ", fingerprint [" + MOCK_FINGERPRINT_4 + "]," + - " keyUsage [digitalSignature, nonRepudiation] and extendedKeyUsage [serverAuth, codeSigning];" + - " the session uses cipher suite [TLS_ECDHE_RSA_WITH_RC4_128_SHA] and protocol [SSLv3];" + - " the certificate does not have any subject alternative names;" + - " the certificate is issued by [CN=ca,OU=windows,DC=example,DC=com];" + - " the certificate is" + - " signed by (subject [CN=ca,OU=windows,DC=example,DC=com] fingerprint [" + MOCK_FINGERPRINT_3 + "])" + - " signed by (subject [CN=issuing-ca,DC=example,DC=com] fingerprint [" + MOCK_FINGERPRINT_2 + "] {trusted issuer})" + - " which is issued by [CN=root-ca,DC=example,DC=com] (but that issuer certificate was not provided in the chain);" + - " the issuing certificate with fingerprint [" + MOCK_FINGERPRINT_1 + "]" + - " is trusted in this ssl context ([xpack.security.authc.realms.ldap.ldap1.ssl])")); + final String message = SslDiagnostics.getTrustDiagnosticFailure( + chain, + SslDiagnostics.PeerType.SERVER, + session, + "xpack.security.authc.realms.ldap.ldap1.ssl", + trustIssuers + ); + assertThat( + message, + Matchers.equalTo( + "failed to establish trust with server at [192.168.1.5];" + + " the server provided a certificate with subject name [CN=elastic1,OU=windows,DC=example,DC=com]" + + ", fingerprint [" + + MOCK_FINGERPRINT_4 + + "]," + + " keyUsage [digitalSignature, nonRepudiation] and extendedKeyUsage [serverAuth, codeSigning];" + + " the session uses cipher suite [TLS_ECDHE_RSA_WITH_RC4_128_SHA] and protocol [SSLv3];" + + " the certificate does not have any subject alternative names;" + + " the certificate is issued by [CN=ca,OU=windows,DC=example,DC=com];" + + " the certificate is" + + " signed by (subject [CN=ca,OU=windows,DC=example,DC=com] fingerprint [" + + MOCK_FINGERPRINT_3 + + "])" + + " signed by (subject [CN=issuing-ca,DC=example,DC=com] fingerprint [" + + MOCK_FINGERPRINT_2 + + "] {trusted issuer})" + + " which is issued by [CN=root-ca,DC=example,DC=com] (but that issuer certificate was not provided in the chain);" + + " the issuing certificate with fingerprint [" + + MOCK_FINGERPRINT_1 + + "]" + + " is trusted in this ssl context ([xpack.security.authc.realms.ldap.ldap1.ssl])" + ) + ); } public void testDiagnosticMessageWhenServerProvidePartialChainFromUntrustedCA() throws Exception { - final X509Certificate rootCA = mockCertificateWithIssuer("CN=root-ca,DC=example,DC=com", MOCK_ENCODING_1, - Collections.emptyList(), null); - final X509Certificate issuingCA = mockCertificateWithIssuer("CN=issuing-ca,DC=example,DC=com", MOCK_ENCODING_2, - Collections.emptyList(), rootCA); - final X509Certificate localCA = mockCertificateWithIssuer("CN=ca,OU=windows,DC=example,DC=com", MOCK_ENCODING_3, - Collections.emptyList(), issuingCA); - final X509Certificate endCert = mockCertificateWithIssuer("CN=elastic1,OU=windows,DC=example,DC=com", MOCK_ENCODING_4, - Collections.emptyList(), localCA); + final X509Certificate rootCA = mockCertificateWithIssuer( + "CN=root-ca,DC=example,DC=com", + MOCK_ENCODING_1, + Collections.emptyList(), + null + ); + final X509Certificate issuingCA = mockCertificateWithIssuer( + "CN=issuing-ca,DC=example,DC=com", + MOCK_ENCODING_2, + Collections.emptyList(), + rootCA + ); + final X509Certificate localCA = mockCertificateWithIssuer( + "CN=ca,OU=windows,DC=example,DC=com", + MOCK_ENCODING_3, + Collections.emptyList(), + issuingCA + ); + final X509Certificate endCert = mockCertificateWithIssuer( + "CN=elastic1,OU=windows,DC=example,DC=com", + MOCK_ENCODING_4, + Collections.emptyList(), + localCA + ); final X509Certificate[] chain = { endCert, localCA, issuingCA }; final SSLSession session = session("192.168.1.6"); final Map> trustIssuers = trust(Collections.emptyList()); - final String message = SslDiagnostics.getTrustDiagnosticFailure(chain, SslDiagnostics.PeerType.SERVER, session, - "xpack.security.authc.realms.ldap.ldap1.ssl", trustIssuers); - assertThat(message, Matchers.equalTo("failed to establish trust with server at [192.168.1.6];" + - " the server provided a certificate with subject name [CN=elastic1,OU=windows,DC=example,DC=com]" + - ", fingerprint [" + MOCK_FINGERPRINT_4 + "]," + - " keyUsage [digitalSignature, nonRepudiation] and extendedKeyUsage [serverAuth, codeSigning];" + - " the session uses cipher suite [TLS_ECDHE_RSA_WITH_RC4_128_SHA] and protocol [SSLv3];" + - " the certificate does not have any subject alternative names;" + - " the certificate is issued by [CN=ca,OU=windows,DC=example,DC=com];" + - " the certificate is" + - " signed by (subject [CN=ca,OU=windows,DC=example,DC=com] fingerprint [" + MOCK_FINGERPRINT_3 + "])" + - " signed by (subject [CN=issuing-ca,DC=example,DC=com] fingerprint [" + MOCK_FINGERPRINT_2 + "])" + - " which is issued by [CN=root-ca,DC=example,DC=com] (but that issuer certificate was not provided in the chain);" + - " this ssl context ([xpack.security.authc.realms.ldap.ldap1.ssl])" + - " is not configured to trust that issuer or any other issuer")); + final String message = SslDiagnostics.getTrustDiagnosticFailure( + chain, + SslDiagnostics.PeerType.SERVER, + session, + "xpack.security.authc.realms.ldap.ldap1.ssl", + trustIssuers + ); + assertThat( + message, + Matchers.equalTo( + "failed to establish trust with server at [192.168.1.6];" + + " the server provided a certificate with subject name [CN=elastic1,OU=windows,DC=example,DC=com]" + + ", fingerprint [" + + MOCK_FINGERPRINT_4 + + "]," + + " keyUsage [digitalSignature, nonRepudiation] and extendedKeyUsage [serverAuth, codeSigning];" + + " the session uses cipher suite [TLS_ECDHE_RSA_WITH_RC4_128_SHA] and protocol [SSLv3];" + + " the certificate does not have any subject alternative names;" + + " the certificate is issued by [CN=ca,OU=windows,DC=example,DC=com];" + + " the certificate is" + + " signed by (subject [CN=ca,OU=windows,DC=example,DC=com] fingerprint [" + + MOCK_FINGERPRINT_3 + + "])" + + " signed by (subject [CN=issuing-ca,DC=example,DC=com] fingerprint [" + + MOCK_FINGERPRINT_2 + + "])" + + " which is issued by [CN=root-ca,DC=example,DC=com] (but that issuer certificate was not provided in the chain);" + + " this ssl context ([xpack.security.authc.realms.ldap.ldap1.ssl])" + + " is not configured to trust that issuer or any other issuer" + ) + ); } public void testDiagnosticMessageWhenServerProvidesASelfSignedCertThatIsDirectlyTrusted() throws Exception { X509Certificate[] chain = loadCertChain("ca1/ca.crt"); final SSLSession session = session("192.168.1.1"); final Map> trustIssuers = trust("ca1/ca.crt", "ca2/ca.crt"); - final String message = SslDiagnostics.getTrustDiagnosticFailure(chain, SslDiagnostics.PeerType.SERVER, session, - "xpack.http.ssl", trustIssuers); - assertThat(message, Matchers.equalTo("failed to establish trust with server at [192.168.1.1];" + - " the server provided a certificate with subject name [CN=Test CA 1]" + - ", fingerprint [2b7b0416391bdf86502505c23149022d2213dadc], no keyUsage and no extendedKeyUsage;" + - " the session uses cipher suite [TLS_ECDHE_RSA_WITH_RC4_128_SHA] and protocol [SSLv3];" + - " the certificate does not have any subject alternative names;" + - " the certificate is self-issued; the [CN=Test CA 1]" + - " certificate is trusted in this ssl context ([xpack.http.ssl])")); + final String message = SslDiagnostics.getTrustDiagnosticFailure( + chain, + SslDiagnostics.PeerType.SERVER, + session, + "xpack.http.ssl", + trustIssuers + ); + assertThat( + message, + Matchers.equalTo( + "failed to establish trust with server at [192.168.1.1];" + + " the server provided a certificate with subject name [CN=Test CA 1]" + + ", fingerprint [2b7b0416391bdf86502505c23149022d2213dadc], no keyUsage and no extendedKeyUsage;" + + " the session uses cipher suite [TLS_ECDHE_RSA_WITH_RC4_128_SHA] and protocol [SSLv3];" + + " the certificate does not have any subject alternative names;" + + " the certificate is self-issued; the [CN=Test CA 1]" + + " certificate is trusted in this ssl context ([xpack.http.ssl])" + ) + ); } public void testDiagnosticMessageWhenServerProvidesASelfSignedCertThatIsNotTrusted() throws Exception { X509Certificate[] chain = loadCertChain("ca1/ca.crt"); final SSLSession session = session("192.168.10.10"); final Map> trustIssuers = Collections.emptyMap(); - final String message = SslDiagnostics.getTrustDiagnosticFailure(chain, SslDiagnostics.PeerType.SERVER, session, - "xpack.http.ssl", trustIssuers); - assertThat(message, Matchers.equalTo("failed to establish trust with server at [192.168.10.10];" + - " the server provided a certificate with subject name [CN=Test CA 1]" + - ", fingerprint [2b7b0416391bdf86502505c23149022d2213dadc], no keyUsage and no extendedKeyUsage;" + - " the session uses cipher suite [TLS_ECDHE_RSA_WITH_RC4_128_SHA] and protocol [SSLv3];" + - " the certificate does not have any subject alternative names;" + - " the certificate is self-issued; the [CN=Test CA 1]" + - " certificate is not trusted in this ssl context ([xpack.http.ssl])")); + final String message = SslDiagnostics.getTrustDiagnosticFailure( + chain, + SslDiagnostics.PeerType.SERVER, + session, + "xpack.http.ssl", + trustIssuers + ); + assertThat( + message, + Matchers.equalTo( + "failed to establish trust with server at [192.168.10.10];" + + " the server provided a certificate with subject name [CN=Test CA 1]" + + ", fingerprint [2b7b0416391bdf86502505c23149022d2213dadc], no keyUsage and no extendedKeyUsage;" + + " the session uses cipher suite [TLS_ECDHE_RSA_WITH_RC4_128_SHA] and protocol [SSLv3];" + + " the certificate does not have any subject alternative names;" + + " the certificate is self-issued; the [CN=Test CA 1]" + + " certificate is not trusted in this ssl context ([xpack.http.ssl])" + ) + ); } public void testDiagnosticMessageWhenServerProvidesASelfSignedCertWithMimicName() throws Exception { X509Certificate[] chain = loadCertChain("ca1/ca.crt"); final SSLSession session = session("192.168.1.1"); final Map> trustIssuers = trust("ca1-b/ca.crt", "ca2/ca.crt"); - final String message = SslDiagnostics.getTrustDiagnosticFailure(chain, SslDiagnostics.PeerType.SERVER, session, - "xpack.http.ssl", trustIssuers); - assertThat(message, Matchers.equalTo("failed to establish trust with server at [192.168.1.1];" + - " the server provided a certificate with subject name [CN=Test CA 1]" + - ", fingerprint [2b7b0416391bdf86502505c23149022d2213dadc], no keyUsage and no extendedKeyUsage;" + - " the session uses cipher suite [TLS_ECDHE_RSA_WITH_RC4_128_SHA] and protocol [SSLv3];" + - " the certificate does not have any subject alternative names;" + - " the certificate is self-issued; the [CN=Test CA 1]" + - " certificate is not trusted in this ssl context ([xpack.http.ssl]);" + - " this ssl context does trust a certificate with subject [CN=Test CA 1]" + - " but the trusted certificate has fingerprint [b095bf2526be20783e1f26dfd69c7aae910e3663]")); + final String message = SslDiagnostics.getTrustDiagnosticFailure( + chain, + SslDiagnostics.PeerType.SERVER, + session, + "xpack.http.ssl", + trustIssuers + ); + assertThat( + message, + Matchers.equalTo( + "failed to establish trust with server at [192.168.1.1];" + + " the server provided a certificate with subject name [CN=Test CA 1]" + + ", fingerprint [2b7b0416391bdf86502505c23149022d2213dadc], no keyUsage and no extendedKeyUsage;" + + " the session uses cipher suite [TLS_ECDHE_RSA_WITH_RC4_128_SHA] and protocol [SSLv3];" + + " the certificate does not have any subject alternative names;" + + " the certificate is self-issued; the [CN=Test CA 1]" + + " certificate is not trusted in this ssl context ([xpack.http.ssl]);" + + " this ssl context does trust a certificate with subject [CN=Test CA 1]" + + " but the trusted certificate has fingerprint [b095bf2526be20783e1f26dfd69c7aae910e3663]" + ) + ); } public void testDiagnosticMessageWithEmptyChain() throws Exception { X509Certificate[] chain = new X509Certificate[0]; final SSLSession session = session("192.168.1.2"); final Map> trustIssuers = Collections.emptyMap(); - final String message = SslDiagnostics.getTrustDiagnosticFailure(chain, SslDiagnostics.PeerType.SERVER, session, - "xpack.http.ssl", trustIssuers); - assertThat(message, Matchers.equalTo("failed to establish trust with server at [192.168.1.2];" + - " the server did not provide a certificate")); + final String message = SslDiagnostics.getTrustDiagnosticFailure( + chain, + SslDiagnostics.PeerType.SERVER, + session, + "xpack.http.ssl", + trustIssuers + ); + assertThat( + message, + Matchers.equalTo("failed to establish trust with server at [192.168.1.2];" + " the server did not provide a certificate") + ); } public void testDiagnosticMessageWhenServerProvidesAnEmailSubjAltName() throws Exception { final String subjectName = "CN=foo,DC=example,DC=com"; - final X509Certificate certificate = mockCertificateWithIssuer(subjectName, - MOCK_ENCODING_1, Collections.singletonList(List.of(1, "foo@example.com")), null); + final X509Certificate certificate = mockCertificateWithIssuer( + subjectName, + MOCK_ENCODING_1, + Collections.singletonList(List.of(1, "foo@example.com")), + null + ); X509Certificate[] chain = new X509Certificate[] { certificate }; final SSLSession session = session("192.168.1.3"); final Map> trustIssuers = trust(certificate); - final String message = SslDiagnostics.getTrustDiagnosticFailure(chain, SslDiagnostics.PeerType.SERVER, session, - "xpack.monitoring.exporters.elastic-cloud.ssl", trustIssuers); - assertThat(message, Matchers.equalTo("failed to establish trust with server at [192.168.1.3];" + - " the server provided a certificate with subject name [CN=foo,DC=example,DC=com]" + - ", fingerprint [" + MOCK_FINGERPRINT_1 + "]," + - " keyUsage [digitalSignature, nonRepudiation] and extendedKeyUsage [serverAuth, codeSigning];" + - " the session uses cipher suite [TLS_ECDHE_RSA_WITH_RC4_128_SHA] and protocol [SSLv3];" + - " the certificate does not have any DNS/IP subject alternative names;" + - " the certificate is self-issued;" + - " the [CN=foo,DC=example,DC=com] certificate is trusted in" + - " this ssl context ([xpack.monitoring.exporters.elastic-cloud.ssl])")); + final String message = SslDiagnostics.getTrustDiagnosticFailure( + chain, + SslDiagnostics.PeerType.SERVER, + session, + "xpack.monitoring.exporters.elastic-cloud.ssl", + trustIssuers + ); + assertThat( + message, + Matchers.equalTo( + "failed to establish trust with server at [192.168.1.3];" + + " the server provided a certificate with subject name [CN=foo,DC=example,DC=com]" + + ", fingerprint [" + + MOCK_FINGERPRINT_1 + + "]," + + " keyUsage [digitalSignature, nonRepudiation] and extendedKeyUsage [serverAuth, codeSigning];" + + " the session uses cipher suite [TLS_ECDHE_RSA_WITH_RC4_128_SHA] and protocol [SSLv3];" + + " the certificate does not have any DNS/IP subject alternative names;" + + " the certificate is self-issued;" + + " the [CN=foo,DC=example,DC=com] certificate is trusted in" + + " this ssl context ([xpack.monitoring.exporters.elastic-cloud.ssl])" + ) + ); } public void testDiagnosticMessageWhenServerCertificateHasNoKeyUsage() throws Exception { @@ -387,23 +610,41 @@ public void testDiagnosticMessageWhenServerCertificateHasNoKeyUsage() throws Exc final String protocol = randomFrom(SslConfigurationLoader.DEFAULT_PROTOCOLS); final SSLSession session = session(peerHost, cipherSuite, protocol); final Map> trustIssuers = trust(certificate); - final String message = SslDiagnostics.getTrustDiagnosticFailure(chain, SslDiagnostics.PeerType.SERVER, session, - "xpack.monitoring.exporters.elastic-cloud.ssl", trustIssuers); + final String message = SslDiagnostics.getTrustDiagnosticFailure( + chain, + SslDiagnostics.PeerType.SERVER, + session, + "xpack.monitoring.exporters.elastic-cloud.ssl", + trustIssuers + ); - assertThat(message, Matchers.equalTo("failed to establish trust with server at [" + peerHost + "];" + - " the server provided a certificate with subject name [CN=foo,DC=example,DC=com]" + - ", fingerprint [" + MOCK_FINGERPRINT_1 + "], no keyUsage and no extendedKeyUsage;" + - " the session uses cipher suite [" + cipherSuite + "] and protocol [" + protocol + "];" + - " the certificate does not have any DNS/IP subject alternative names;" + - " the certificate is self-issued;" + - " the [CN=foo,DC=example,DC=com] certificate is trusted" + - " in this ssl context ([xpack.monitoring.exporters.elastic-cloud.ssl])")); + assertThat( + message, + Matchers.equalTo( + "failed to establish trust with server at [" + + peerHost + + "];" + + " the server provided a certificate with subject name [CN=foo,DC=example,DC=com]" + + ", fingerprint [" + + MOCK_FINGERPRINT_1 + + "], no keyUsage and no extendedKeyUsage;" + + " the session uses cipher suite [" + + cipherSuite + + "] and protocol [" + + protocol + + "];" + + " the certificate does not have any DNS/IP subject alternative names;" + + " the certificate is self-issued;" + + " the [CN=foo,DC=example,DC=com] certificate is trusted" + + " in this ssl context ([xpack.monitoring.exporters.elastic-cloud.ssl])" + ) + ); } public void testDiagnosticMessageWhenServerCertificateHasKeyUsageAndNoExtendedKeyUsage() throws Exception { final String subjectName = "CN=foo,DC=example,DC=com"; - final boolean[] keyUsage = {true, false, true, true, true, false, false, false, false, false}; + final boolean[] keyUsage = { true, false, true, true, true, false, false, false, false, false }; final X509Certificate certificate = mockCertificateWithIssuer( subjectName, MOCK_ENCODING_1, @@ -419,25 +660,43 @@ public void testDiagnosticMessageWhenServerCertificateHasKeyUsageAndNoExtendedKe final String protocol = randomFrom(SslConfigurationLoader.DEFAULT_PROTOCOLS); final SSLSession session = session(peerHost, cipherSuite, protocol); final Map> trustIssuers = trust(certificate); - final String message = SslDiagnostics.getTrustDiagnosticFailure(chain, SslDiagnostics.PeerType.SERVER, session, - "xpack.monitoring.exporters.elastic-cloud.ssl", trustIssuers); + final String message = SslDiagnostics.getTrustDiagnosticFailure( + chain, + SslDiagnostics.PeerType.SERVER, + session, + "xpack.monitoring.exporters.elastic-cloud.ssl", + trustIssuers + ); - assertThat(message, Matchers.equalTo("failed to establish trust with server at [" + peerHost + "];" + - " the server provided a certificate with subject name [CN=foo,DC=example,DC=com]" + - ", fingerprint [" + MOCK_FINGERPRINT_1 + "]," + - " keyUsage [digitalSignature, keyEncipherment, dataEncipherment, keyAgreement]" + - " and no extendedKeyUsage;" + - " the session uses cipher suite [" + cipherSuite + "] and protocol [" + protocol + "];" + - " the certificate does not have any DNS/IP subject alternative names;" + - " the certificate is self-issued;" + - " the [CN=foo,DC=example,DC=com] certificate is trusted" + - " in this ssl context ([xpack.monitoring.exporters.elastic-cloud.ssl])")); + assertThat( + message, + Matchers.equalTo( + "failed to establish trust with server at [" + + peerHost + + "];" + + " the server provided a certificate with subject name [CN=foo,DC=example,DC=com]" + + ", fingerprint [" + + MOCK_FINGERPRINT_1 + + "]," + + " keyUsage [digitalSignature, keyEncipherment, dataEncipherment, keyAgreement]" + + " and no extendedKeyUsage;" + + " the session uses cipher suite [" + + cipherSuite + + "] and protocol [" + + protocol + + "];" + + " the certificate does not have any DNS/IP subject alternative names;" + + " the certificate is self-issued;" + + " the [CN=foo,DC=example,DC=com] certificate is trusted" + + " in this ssl context ([xpack.monitoring.exporters.elastic-cloud.ssl])" + ) + ); } public void testDiagnosticMessageWhenServerCertificateHasKeyUsageAndExtendedKeyUsage() throws Exception { final String subjectName = "CN=foo,DC=example,DC=com"; - final boolean[] keyUsage = {false, false, false, false, false, false, false, true, false}; + final boolean[] keyUsage = { false, false, false, false, false, false, false, true, false }; final X509Certificate certificate = mockCertificateWithIssuer( subjectName, MOCK_ENCODING_1, @@ -453,18 +712,36 @@ public void testDiagnosticMessageWhenServerCertificateHasKeyUsageAndExtendedKeyU final String protocol = randomFrom(SslConfigurationLoader.DEFAULT_PROTOCOLS); final SSLSession session = session(peerHost, cipherSuite, protocol); final Map> trustIssuers = trust(certificate); - final String message = SslDiagnostics.getTrustDiagnosticFailure(chain, SslDiagnostics.PeerType.SERVER, session, - "xpack.monitoring.exporters.elastic-cloud.ssl", trustIssuers); + final String message = SslDiagnostics.getTrustDiagnosticFailure( + chain, + SslDiagnostics.PeerType.SERVER, + session, + "xpack.monitoring.exporters.elastic-cloud.ssl", + trustIssuers + ); - assertThat(message, Matchers.equalTo("failed to establish trust with server at [" + peerHost + "];" + - " the server provided a certificate with subject name [CN=foo,DC=example,DC=com]" + - ", fingerprint [" + MOCK_FINGERPRINT_1 + "]," + - " keyUsage [encipherOnly] and extendedKeyUsage [serverAuth, clientAuth];" + - " the session uses cipher suite [" + cipherSuite + "] and protocol [" + protocol + "];" + - " the certificate does not have any DNS/IP subject alternative names;" + - " the certificate is self-issued;" + - " the [CN=foo,DC=example,DC=com] certificate is trusted" + - " in this ssl context ([xpack.monitoring.exporters.elastic-cloud.ssl])")); + assertThat( + message, + Matchers.equalTo( + "failed to establish trust with server at [" + + peerHost + + "];" + + " the server provided a certificate with subject name [CN=foo,DC=example,DC=com]" + + ", fingerprint [" + + MOCK_FINGERPRINT_1 + + "]," + + " keyUsage [encipherOnly] and extendedKeyUsage [serverAuth, clientAuth];" + + " the session uses cipher suite [" + + cipherSuite + + "] and protocol [" + + protocol + + "];" + + " the certificate does not have any DNS/IP subject alternative names;" + + " the certificate is self-issued;" + + " the [CN=foo,DC=example,DC=com] certificate is trusted" + + " in this ssl context ([xpack.monitoring.exporters.elastic-cloud.ssl])" + ) + ); } public void testDiagnosticMessageWhenServerCertificateHasOversizedKeyUsageAndUnrecognisedExtendedKeyUsage() throws Exception { @@ -486,18 +763,36 @@ public void testDiagnosticMessageWhenServerCertificateHasOversizedKeyUsageAndUnr final String protocol = randomFrom(SslConfigurationLoader.DEFAULT_PROTOCOLS); final SSLSession session = session(peerHost, cipherSuite, protocol); final Map> trustIssuers = trust(certificate); - final String message = SslDiagnostics.getTrustDiagnosticFailure(chain, SslDiagnostics.PeerType.SERVER, session, - "xpack.monitoring.exporters.elastic-cloud.ssl", trustIssuers); + final String message = SslDiagnostics.getTrustDiagnosticFailure( + chain, + SslDiagnostics.PeerType.SERVER, + session, + "xpack.monitoring.exporters.elastic-cloud.ssl", + trustIssuers + ); - assertThat(message, Matchers.equalTo("failed to establish trust with server at [" + peerHost + "];" + - " the server provided a certificate with subject name [CN=foo,DC=example,DC=com]" + - ", fingerprint [" + MOCK_FINGERPRINT_1 + "]," + - " keyUsage [keyCertSign, #9, #11] and extendedKeyUsage [timeStamping, 1.3.6.1.5.5.7.3.12];" + - " the session uses cipher suite [" + cipherSuite + "] and protocol [" + protocol + "];" + - " the certificate does not have any DNS/IP subject alternative names;" + - " the certificate is self-issued;" + - " the [CN=foo,DC=example,DC=com] certificate is trusted" + - " in this ssl context ([xpack.monitoring.exporters.elastic-cloud.ssl])")); + assertThat( + message, + Matchers.equalTo( + "failed to establish trust with server at [" + + peerHost + + "];" + + " the server provided a certificate with subject name [CN=foo,DC=example,DC=com]" + + ", fingerprint [" + + MOCK_FINGERPRINT_1 + + "]," + + " keyUsage [keyCertSign, #9, #11] and extendedKeyUsage [timeStamping, 1.3.6.1.5.5.7.3.12];" + + " the session uses cipher suite [" + + cipherSuite + + "] and protocol [" + + protocol + + "];" + + " the certificate does not have any DNS/IP subject alternative names;" + + " the certificate is self-issued;" + + " the [CN=foo,DC=example,DC=com] certificate is trusted" + + " in this ssl context ([xpack.monitoring.exporters.elastic-cloud.ssl])" + ) + ); } public void testDiagnosticMessageWhenACertificateHasAnInvalidEncoding() throws Exception { @@ -508,17 +803,27 @@ public void testDiagnosticMessageWhenACertificateHasAnInvalidEncoding() throws E final SSLSession session = session("192.168.1.6"); final Map> trustIssuers = trust(Collections.emptyList()); - final String message = SslDiagnostics.getTrustDiagnosticFailure(chain, SslDiagnostics.PeerType.SERVER, session, - "xpack.security.transport.ssl", trustIssuers); - assertThat(message, Matchers.equalTo("failed to establish trust with server at [192.168.1.6];" + - " the server provided a certificate with subject name [CN=foo,DC=example,DC=com]," + - " invalid encoding [java.security.cert.CertificateEncodingException: MOCK INVALID ENCODING]," + - " keyUsage [digitalSignature, nonRepudiation] and extendedKeyUsage [serverAuth, codeSigning];" + - " the session uses cipher suite [TLS_ECDHE_RSA_WITH_RC4_128_SHA] and protocol [SSLv3];" + - " the certificate does not have any subject alternative names;" + - " the certificate is self-issued;" + - " the [CN=foo,DC=example,DC=com] certificate is not trusted" + - " in this ssl context ([xpack.security.transport.ssl])")); + final String message = SslDiagnostics.getTrustDiagnosticFailure( + chain, + SslDiagnostics.PeerType.SERVER, + session, + "xpack.security.transport.ssl", + trustIssuers + ); + assertThat( + message, + Matchers.equalTo( + "failed to establish trust with server at [192.168.1.6];" + + " the server provided a certificate with subject name [CN=foo,DC=example,DC=com]," + + " invalid encoding [java.security.cert.CertificateEncodingException: MOCK INVALID ENCODING]," + + " keyUsage [digitalSignature, nonRepudiation] and extendedKeyUsage [serverAuth, codeSigning];" + + " the session uses cipher suite [TLS_ECDHE_RSA_WITH_RC4_128_SHA] and protocol [SSLv3];" + + " the certificate does not have any subject alternative names;" + + " the certificate is self-issued;" + + " the [CN=foo,DC=example,DC=com] certificate is not trusted" + + " in this ssl context ([xpack.security.transport.ssl])" + ) + ); } public void testDiagnosticMessageForClientCertificate() throws Exception { @@ -526,16 +831,26 @@ public void testDiagnosticMessageForClientCertificate() throws Exception { final SSLSession session = session("192.168.1.7"); final Map> trustIssuers = trust("ca1/ca.crt"); - final String message = SslDiagnostics.getTrustDiagnosticFailure(chain, SslDiagnostics.PeerType.CLIENT, session, - "xpack.security.transport.ssl", trustIssuers); - assertThat(message, Matchers.equalTo("failed to establish trust with client at [192.168.1.7];" + - " the client provided a certificate with subject name [CN=cert1]" + - ", fingerprint [3bebe388a66362784afd6c51a9000961a4e10050], no keyUsage and no extendedKeyUsage;" + - " the session uses cipher suite [TLS_ECDHE_RSA_WITH_RC4_128_SHA] and protocol [SSLv3];" + - " the certificate is issued by [CN=Test CA 1]" + - " but the client did not provide a copy of the issuing certificate in the certificate chain;" + - " the issuing certificate with fingerprint [2b7b0416391bdf86502505c23149022d2213dadc]" + - " is trusted in this ssl context ([xpack.security.transport.ssl])")); + final String message = SslDiagnostics.getTrustDiagnosticFailure( + chain, + SslDiagnostics.PeerType.CLIENT, + session, + "xpack.security.transport.ssl", + trustIssuers + ); + assertThat( + message, + Matchers.equalTo( + "failed to establish trust with client at [192.168.1.7];" + + " the client provided a certificate with subject name [CN=cert1]" + + ", fingerprint [3bebe388a66362784afd6c51a9000961a4e10050], no keyUsage and no extendedKeyUsage;" + + " the session uses cipher suite [TLS_ECDHE_RSA_WITH_RC4_128_SHA] and protocol [SSLv3];" + + " the certificate is issued by [CN=Test CA 1]" + + " but the client did not provide a copy of the issuing certificate in the certificate chain;" + + " the issuing certificate with fingerprint [2b7b0416391bdf86502505c23149022d2213dadc]" + + " is trusted in this ssl context ([xpack.security.transport.ssl])" + ) + ); } public void testDiagnosticMessageWhenCaHasNewIssuingCertificate() throws Exception { @@ -551,21 +866,31 @@ public void testDiagnosticMessageWhenCaHasNewIssuingCertificate() throws Excepti final SSLSession session = session("192.168.1.4"); final Map> trustIssuers = trust(oldCaCert); - final String message = SslDiagnostics.getTrustDiagnosticFailure(chain, SslDiagnostics.PeerType.SERVER, session, - "xpack.security.authc.realms.saml.saml1.ssl", trustIssuers); - assertThat(message, Matchers.equalTo("failed to establish trust with server at [192.168.1.4];" + - " the server provided a certificate with subject name [CN=cert1]," + - " fingerprint [3bebe388a66362784afd6c51a9000961a4e10050]," + - " no keyUsage and no extendedKeyUsage;" + - " the session uses cipher suite [TLS_ECDHE_RSA_WITH_RC4_128_SHA] and protocol [SSLv3];" + - " the certificate has subject alternative names [DNS:localhost,IP:127.0.0.1];" + - " the certificate is issued by [CN=Test CA 1];" + - " the certificate is signed by (subject [CN=Test CA 1]" + - " fingerprint [2b7b0416391bdf86502505c23149022d2213dadc] {trusted issuer})" + - " which is self-issued;" + - " the [CN=Test CA 1] certificate is trusted in this ssl context ([xpack.security.authc.realms.saml.saml1.ssl])" + - " because we trust a certificate with fingerprint [1f8ac10f23c5b5bc1167bda84b833e5c057a77d2]" + - " for the same public key")); + final String message = SslDiagnostics.getTrustDiagnosticFailure( + chain, + SslDiagnostics.PeerType.SERVER, + session, + "xpack.security.authc.realms.saml.saml1.ssl", + trustIssuers + ); + assertThat( + message, + Matchers.equalTo( + "failed to establish trust with server at [192.168.1.4];" + + " the server provided a certificate with subject name [CN=cert1]," + + " fingerprint [3bebe388a66362784afd6c51a9000961a4e10050]," + + " no keyUsage and no extendedKeyUsage;" + + " the session uses cipher suite [TLS_ECDHE_RSA_WITH_RC4_128_SHA] and protocol [SSLv3];" + + " the certificate has subject alternative names [DNS:localhost,IP:127.0.0.1];" + + " the certificate is issued by [CN=Test CA 1];" + + " the certificate is signed by (subject [CN=Test CA 1]" + + " fingerprint [2b7b0416391bdf86502505c23149022d2213dadc] {trusted issuer})" + + " which is self-issued;" + + " the [CN=Test CA 1] certificate is trusted in this ssl context ([xpack.security.authc.realms.saml.saml1.ssl])" + + " because we trust a certificate with fingerprint [1f8ac10f23c5b5bc1167bda84b833e5c057a77d2]" + + " for the same public key" + ) + ); } public X509Certificate cloneCertificateAsMock(X509Certificate clone) throws CertificateParsingException, CertificateEncodingException { @@ -579,11 +904,15 @@ public X509Certificate cloneCertificateAsMock(X509Certificate clone) throws Cert return cert; } - public X509Certificate mockCertificateWithIssuer(String principal, byte[] encoding, List> subjAltNames, - @Nullable X509Certificate issuer) throws CertificateException { + public X509Certificate mockCertificateWithIssuer( + String principal, + byte[] encoding, + List> subjAltNames, + @Nullable X509Certificate issuer + ) throws CertificateException { final List extendedKeyUsage = List.of("1.3.6.1.5.5.7.3.1", "1.3.6.1.5.5.7.3.3"); - final boolean[] keyUsage = {true, true, false, false, false, false, false, false, false}; + final boolean[] keyUsage = { true, true, false, false, false, false, false, false, false }; return mockCertificateWithIssuer(principal, encoding, subjAltNames, issuer, keyUsage, extendedKeyUsage); } @@ -593,7 +922,8 @@ private X509Certificate mockCertificateWithIssuer( byte[] encoding, List> subjAltNames, X509Certificate issuer, - boolean[] keyUsage, List extendedKeyUsage + boolean[] keyUsage, + List extendedKeyUsage ) throws CertificateParsingException, CertificateEncodingException { final X509Certificate cert = Mockito.mock(X509Certificate.class); final X500Principal x500Principal = new X500Principal(principal); @@ -621,8 +951,9 @@ private X509Certificate loadCertificate(String name) throws CertificateException if (certificates.size() == 1) { return (X509Certificate) certificates.get(0); } else { - throw new IllegalStateException("Expected 1 certificate in [" + path.toAbsolutePath() - + "] but found [" + certificates.size() + "] - " + certificates); + throw new IllegalStateException( + "Expected 1 certificate in [" + path.toAbsolutePath() + "] but found [" + certificates.size() + "] - " + certificates + ); } } @@ -638,13 +969,18 @@ private Map> trust(X509Certificate... caCerts) { private Map> trust(Collection caCerts) { return caCerts.stream() .map(X509Certificate.class::cast) - .collect(Collectors.toMap(x -> x.getSubjectX500Principal().getName(), List::of, - (List a, List b) -> { - List merge = new ArrayList<>(); - merge.addAll(a); - merge.addAll(b); - return merge; - })); + .collect( + Collectors.toMap( + x -> x.getSubjectX500Principal().getName(), + List::of, + (List a, List b) -> { + List merge = new ArrayList<>(); + merge.addAll(a); + merge.addAll(b); + return merge; + } + ) + ); } private SSLSession session(String peerHost) { diff --git a/libs/ssl-config/src/test/java/org/elasticsearch/common/ssl/StoreKeyConfigTests.java b/libs/ssl-config/src/test/java/org/elasticsearch/common/ssl/StoreKeyConfigTests.java index ea0b8248b4a42..82aaa67068321 100644 --- a/libs/ssl-config/src/test/java/org/elasticsearch/common/ssl/StoreKeyConfigTests.java +++ b/libs/ssl-config/src/test/java/org/elasticsearch/common/ssl/StoreKeyConfigTests.java @@ -188,10 +188,10 @@ private void assertKeysLoaded(StoreKeyConfig keyConfig, String... names) throws assertThat(certificate.getIssuerDN().getName(), is("CN=Test CA 1")); assertThat(certificate.getSubjectDN().getName(), is("CN=" + name)); assertThat(certificate.getSubjectAlternativeNames(), iterableWithSize(2)); - assertThat(certificate.getSubjectAlternativeNames(), containsInAnyOrder( - Arrays.asList(DNS_NAME, "localhost"), - Arrays.asList(IP_NAME, "127.0.0.1") - )); + assertThat( + certificate.getSubjectAlternativeNames(), + containsInAnyOrder(Arrays.asList(DNS_NAME, "localhost"), Arrays.asList(IP_NAME, "127.0.0.1")) + ); } final List> keys = keyConfig.getKeys(true); diff --git a/libs/ssl-config/src/test/java/org/elasticsearch/common/ssl/StoreTrustConfigTests.java b/libs/ssl-config/src/test/java/org/elasticsearch/common/ssl/StoreTrustConfigTests.java index 91ab353758c9a..e6bfbd4776eea 100644 --- a/libs/ssl-config/src/test/java/org/elasticsearch/common/ssl/StoreTrustConfigTests.java +++ b/libs/ssl-config/src/test/java/org/elasticsearch/common/ssl/StoreTrustConfigTests.java @@ -12,8 +12,6 @@ import org.hamcrest.Matchers; import org.junit.Before; -import javax.net.ssl.TrustManagerFactory; -import javax.net.ssl.X509ExtendedTrustManager; import java.io.IOException; import java.nio.file.Files; import java.nio.file.NoSuchFileException; @@ -26,6 +24,9 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import javax.net.ssl.TrustManagerFactory; +import javax.net.ssl.X509ExtendedTrustManager; + import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.instanceOf; diff --git a/libs/x-content/src/main/java/org/elasticsearch/xcontent/AbstractObjectParser.java b/libs/x-content/src/main/java/org/elasticsearch/xcontent/AbstractObjectParser.java index b72f14613d28b..f6f6d90c0faf8 100644 --- a/libs/x-content/src/main/java/org/elasticsearch/xcontent/AbstractObjectParser.java +++ b/libs/x-content/src/main/java/org/elasticsearch/xcontent/AbstractObjectParser.java @@ -8,7 +8,6 @@ package org.elasticsearch.xcontent; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.core.CheckedFunction; import org.elasticsearch.xcontent.ObjectParser.NamedObjectParser; import org.elasticsearch.xcontent.ObjectParser.ValueType; @@ -29,8 +28,12 @@ public abstract class AbstractObjectParser { * Declare some field. Usually it is easier to use {@link #declareString(BiConsumer, ParseField)} or * {@link #declareObject(BiConsumer, ContextParser, ParseField)} rather than call this directly. */ - public abstract void declareField(BiConsumer consumer, ContextParser parser, ParseField parseField, - ValueType type); + public abstract void declareField( + BiConsumer consumer, + ContextParser parser, + ParseField parseField, + ValueType type + ); /** * Declares a single named object. @@ -53,9 +56,11 @@ public abstract void declareField(BiConsumer consumer, ContextPars * @param parseField * the field to parse */ - public abstract void declareNamedObject(BiConsumer consumer, NamedObjectParser namedObjectParser, - ParseField parseField); - + public abstract void declareNamedObject( + BiConsumer consumer, + NamedObjectParser namedObjectParser, + ParseField parseField + ); /** * Declares named objects in the style of aggregations. These are named @@ -87,8 +92,11 @@ public abstract void declareNamedObject(BiConsumer consumer, Named * @param parseField * the field to parse */ - public abstract void declareNamedObjects(BiConsumer> consumer, NamedObjectParser namedObjectParser, - ParseField parseField); + public abstract void declareNamedObjects( + BiConsumer> consumer, + NamedObjectParser namedObjectParser, + ParseField parseField + ); /** * Declares named objects in the style of highlighting's field element. @@ -142,13 +150,21 @@ public abstract void declareNamedObjects(BiConsumer> consumer * @param parseField * the field to parse */ - public abstract void declareNamedObjects(BiConsumer> consumer, NamedObjectParser namedObjectParser, - Consumer orderedModeCallback, ParseField parseField); + public abstract void declareNamedObjects( + BiConsumer> consumer, + NamedObjectParser namedObjectParser, + Consumer orderedModeCallback, + ParseField parseField + ); public abstract String getName(); - public void declareField(BiConsumer consumer, CheckedFunction parser, - ParseField parseField, ValueType type) { + public void declareField( + BiConsumer consumer, + CheckedFunction parser, + ParseField parseField, + ValueType type + ) { if (parser == null) { throw new IllegalArgumentException("[parser] is required"); } @@ -162,10 +178,18 @@ public void declareObject(BiConsumer consumer, ContextParser void declareObjectOrNull(BiConsumer consumer, ContextParser objectParser, T nullValue, - ParseField field) { - declareField(consumer, (p, c) -> p.currentToken() == XContentParser.Token.VALUE_NULL ? nullValue : objectParser.parse(p, c), - field, ValueType.OBJECT_OR_NULL); + public void declareObjectOrNull( + BiConsumer consumer, + ContextParser objectParser, + T nullValue, + ParseField field + ) { + declareField( + consumer, + (p, c) -> p.currentToken() == XContentParser.Token.VALUE_NULL ? nullValue : objectParser.parse(p, c), + field, + ValueType.OBJECT_OR_NULL + ); } public void declareFloat(BiConsumer consumer, ParseField field) { @@ -177,8 +201,12 @@ public void declareFloat(BiConsumer consumer, ParseField field) { * Declare a float field that parses explicit {@code null}s in the json to a default value. */ public void declareFloatOrNull(BiConsumer consumer, float nullValue, ParseField field) { - declareField(consumer, p -> p.currentToken() == XContentParser.Token.VALUE_NULL ? nullValue : p.floatValue(), - field, ValueType.FLOAT_OR_NULL); + declareField( + consumer, + p -> p.currentToken() == XContentParser.Token.VALUE_NULL ? nullValue : p.floatValue(), + field, + ValueType.FLOAT_OR_NULL + ); } public void declareDouble(BiConsumer consumer, ParseField field) { @@ -190,8 +218,12 @@ public void declareDouble(BiConsumer consumer, ParseField field) * Declare a double field that parses explicit {@code null}s in the json to a default value. */ public void declareDoubleOrNull(BiConsumer consumer, double nullValue, ParseField field) { - declareField(consumer, p -> p.currentToken() == XContentParser.Token.VALUE_NULL ? nullValue : p.doubleValue(), - field, ValueType.DOUBLE_OR_NULL); + declareField( + consumer, + p -> p.currentToken() == XContentParser.Token.VALUE_NULL ? nullValue : p.doubleValue(), + field, + ValueType.DOUBLE_OR_NULL + ); } public void declareLong(BiConsumer consumer, ParseField field) { @@ -201,8 +233,12 @@ public void declareLong(BiConsumer consumer, ParseField field) { public void declareLongOrNull(BiConsumer consumer, long nullValue, ParseField field) { // Using a method reference here angers some compilers - declareField(consumer, p -> p.currentToken() == XContentParser.Token.VALUE_NULL ? nullValue : p.longValue(), - field, ValueType.LONG_OR_NULL); + declareField( + consumer, + p -> p.currentToken() == XContentParser.Token.VALUE_NULL ? nullValue : p.longValue(), + field, + ValueType.LONG_OR_NULL + ); } public void declareInt(BiConsumer consumer, ParseField field) { @@ -214,8 +250,12 @@ public void declareInt(BiConsumer consumer, ParseField field) { * Declare a double field that parses explicit {@code null}s in the json to a default value. */ public void declareIntOrNull(BiConsumer consumer, int nullValue, ParseField field) { - declareField(consumer, p -> p.currentToken() == XContentParser.Token.VALUE_NULL ? nullValue : p.intValue(), - field, ValueType.INT_OR_NULL); + declareField( + consumer, + p -> p.currentToken() == XContentParser.Token.VALUE_NULL ? nullValue : p.intValue(), + field, + ValueType.INT_OR_NULL + ); } public void declareString(BiConsumer consumer, ParseField field) { @@ -231,8 +271,12 @@ public void declareString(BiConsumer consumer, Function } public void declareStringOrNull(BiConsumer consumer, ParseField field) { - declareField(consumer, (p) -> p.currentToken() == XContentParser.Token.VALUE_NULL ? null : p.text(), field, - ValueType.STRING_OR_NULL); + declareField( + consumer, + (p) -> p.currentToken() == XContentParser.Token.VALUE_NULL ? null : p.text(), + field, + ValueType.STRING_OR_NULL + ); } public void declareBoolean(BiConsumer consumer, ParseField field) { @@ -247,8 +291,7 @@ public void declareObjectArray(BiConsumer> consumer, ContextP * like {@link #declareObjectArray(BiConsumer, ContextParser, ParseField)}, but can also handle single null values, * in which case the consumer isn't called */ - public < - T> void declareObjectArrayOrNull( + public void declareObjectArrayOrNull( BiConsumer> consumer, ContextParser objectParser, ParseField field @@ -284,8 +327,12 @@ public void declareIntArray(BiConsumer> consumer, ParseFiel /** * Declares a field that can contain an array of elements listed in the type ValueType enum */ - public void declareFieldArray(BiConsumer> consumer, ContextParser itemParser, - ParseField field, ValueType type) { + public void declareFieldArray( + BiConsumer> consumer, + ContextParser itemParser, + ParseField field, + ValueType type + ) { declareField(consumer, (p, c) -> parseArray(p, () -> itemParser.parse(p, c)), field, type); } @@ -360,14 +407,14 @@ private interface IOSupplier { private static List parseArray(XContentParser parser, IOSupplier supplier) throws IOException { List list = new ArrayList<>(); if (parser.currentToken().isValue() - || parser.currentToken() == XContentParser.Token.VALUE_NULL - || parser.currentToken() == XContentParser.Token.START_OBJECT) { + || parser.currentToken() == XContentParser.Token.VALUE_NULL + || parser.currentToken() == XContentParser.Token.START_OBJECT) { list.add(supplier.get()); // single value } else { while (parser.nextToken() != XContentParser.Token.END_ARRAY) { if (parser.currentToken().isValue() - || parser.currentToken() == XContentParser.Token.VALUE_NULL - || parser.currentToken() == XContentParser.Token.START_OBJECT) { + || parser.currentToken() == XContentParser.Token.VALUE_NULL + || parser.currentToken() == XContentParser.Token.START_OBJECT) { list.add(supplier.get()); } else { throw new IllegalStateException("expected value but got [" + parser.currentToken() + "]"); diff --git a/libs/x-content/src/main/java/org/elasticsearch/xcontent/ConstructingObjectParser.java b/libs/x-content/src/main/java/org/elasticsearch/xcontent/ConstructingObjectParser.java index e8ed7cd1faf4b..1f1a571a20be4 100644 --- a/libs/x-content/src/main/java/org/elasticsearch/xcontent/ConstructingObjectParser.java +++ b/libs/x-content/src/main/java/org/elasticsearch/xcontent/ConstructingObjectParser.java @@ -66,8 +66,10 @@ * Note: if optional constructor arguments aren't specified then the number of allocations is always the worst case. *

    */ -public final class ConstructingObjectParser extends AbstractObjectParser implements - BiFunction, ContextParser{ +public final class ConstructingObjectParser extends AbstractObjectParser + implements + BiFunction, + ContextParser { /** * Consumer that marks a field as a required constructor argument instead of a real object field. @@ -86,8 +88,7 @@ public final class ConstructingObjectParser extends AbstractObje /** * List of constructor names used for generating the error message if not all arrive. */ - private final Map> constructorArgInfos = - new EnumMap<>(RestApiVersion.class); + private final Map> constructorArgInfos = new EnumMap<>(RestApiVersion.class); private final ObjectParser objectParser; private final BiFunction builder; /** @@ -156,7 +157,7 @@ public Value apply(XContentParser parser, Context context) { try { return parse(parser, context); } catch (IOException e) { - throw new XContentParseException(parser.getTokenLocation(), "[" + objectParser.getName() + "] failed to parse object", e); + throw new XContentParseException(parser.getTokenLocation(), "[" + objectParser.getName() + "] failed to parse object", e); } } @@ -219,8 +220,11 @@ public void declareField(BiConsumer consumer, ContextParser void declareNamedObject(BiConsumer consumer, NamedObjectParser namedObjectParser, - ParseField parseField) { + public void declareNamedObject( + BiConsumer consumer, + NamedObjectParser namedObjectParser, + ParseField parseField + ) { if (consumer == null) { throw new IllegalArgumentException("[consumer] is required"); } @@ -248,8 +252,11 @@ public void declareNamedObject(BiConsumer consumer, NamedObjectPar } @Override - public void declareNamedObjects(BiConsumer> consumer, NamedObjectParser namedObjectParser, - ParseField parseField) { + public void declareNamedObjects( + BiConsumer> consumer, + NamedObjectParser namedObjectParser, + ParseField parseField + ) { if (consumer == null) { throw new IllegalArgumentException("[consumer] is required"); @@ -278,8 +285,12 @@ public void declareNamedObjects(BiConsumer> consumer, NamedOb } @Override - public void declareNamedObjects(BiConsumer> consumer, NamedObjectParser namedObjectParser, - Consumer orderedModeCallback, ParseField parseField) { + public void declareNamedObjects( + BiConsumer> consumer, + NamedObjectParser namedObjectParser, + Consumer orderedModeCallback, + ParseField parseField + ) { if (consumer == null) { throw new IllegalArgumentException("[consumer] is required"); } @@ -302,19 +313,27 @@ public void declareNamedObjects(BiConsumer> consumer, NamedOb * or expensive lookups whenever the constructor args come in. */ Map positions = addConstructorArg(consumer, parseField); - objectParser.declareNamedObjects((target, v) -> target.constructorArg(positions, v), namedObjectParser, - wrapOrderedModeCallBack(orderedModeCallback), parseField); + objectParser.declareNamedObjects( + (target, v) -> target.constructorArg(positions, v), + namedObjectParser, + wrapOrderedModeCallBack(orderedModeCallback), + parseField + ); } else { numberOfFields += 1; - objectParser.declareNamedObjects(queueingConsumer(consumer, parseField), namedObjectParser, - wrapOrderedModeCallBack(orderedModeCallback), parseField); + objectParser.declareNamedObjects( + queueingConsumer(consumer, parseField), + namedObjectParser, + wrapOrderedModeCallBack(orderedModeCallback), + parseField + ); } } int getNumberOfFields() { - assert this.constructorArgInfos.get(RestApiVersion.current()).size() - == this.constructorArgInfos.get(RestApiVersion.minimumSupported()).size() : - "Constructors must have same number of arguments per all compatible versions"; + assert this.constructorArgInfos.get(RestApiVersion.current()).size() == this.constructorArgInfos.get( + RestApiVersion.minimumSupported() + ).size() : "Constructors must have same number of arguments per all compatible versions"; return this.constructorArgInfos.get(RestApiVersion.current()).size(); } @@ -337,15 +356,15 @@ private Map addConstructorArg(BiConsumer consumer boolean required = consumer == REQUIRED_CONSTRUCTOR_ARG_MARKER; if (RestApiVersion.minimumSupported().matches(parseField.getForRestApiVersion())) { - constructorArgInfos.computeIfAbsent(RestApiVersion.minimumSupported(), (v)-> new ArrayList<>()) + constructorArgInfos.computeIfAbsent(RestApiVersion.minimumSupported(), (v) -> new ArrayList<>()) .add(new ConstructorArgInfo(parseField, required)); } if (RestApiVersion.current().matches(parseField.getForRestApiVersion())) { - constructorArgInfos.computeIfAbsent(RestApiVersion.current(), (v)-> new ArrayList<>()) + constructorArgInfos.computeIfAbsent(RestApiVersion.current(), (v) -> new ArrayList<>()) .add(new ConstructorArgInfo(parseField, required)); } - //calculate the positions for the arguments + // calculate the positions for the arguments return constructorArgInfos.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().size())); } @@ -400,8 +419,11 @@ private BiConsumer queueingConsumer(BiConsumer consumer try { consumer.accept(targetObject, v); } catch (Exception e) { - throw new XContentParseException(location, - "[" + objectParser.getName() + "] failed to parse field [" + parseField.getPreferredName() + "]", e); + throw new XContentParseException( + location, + "[" + objectParser.getName() + "] failed to parse field [" + parseField.getPreferredName() + "]", + e + ); } }); }; @@ -455,8 +477,7 @@ private class Target { Target(XContentParser parser, Context context) { this.parser = parser; this.context = context; - this.constructorArgs = new Object[constructorArgInfos - .getOrDefault(parser.getRestApiVersion(), Collections.emptyList()).size()]; + this.constructorArgs = new Object[constructorArgInfos.getOrDefault(parser.getRestApiVersion(), Collections.emptyList()).size()]; } /** @@ -475,9 +496,9 @@ private void constructorArg(Map positions, Object value * Queue a consumer that we'll call once the targetObject is built. If targetObject has been built this will fail because the caller * should have just applied the consumer immediately. */ - @SuppressWarnings({"unchecked", "rawtypes"}) + @SuppressWarnings({ "unchecked", "rawtypes" }) private void queue(Consumer queueMe) { - assert targetObject == null: "Don't queue after the targetObject has been built! Just apply the consumer directly."; + assert targetObject == null : "Don't queue after the targetObject has been built! Just apply the consumer directly."; if (queuedFields == null) { this.queuedFields = (Consumer[]) new Consumer[numberOfFields]; } @@ -517,9 +538,12 @@ private Value finish() { * use of ConstructingObjectParser. You should be using ObjectParser instead. Since this is more of a programmer error and the * parser ought to still work we just assert this. */ - assert false == constructorArgInfos.isEmpty() : "[" + objectParser.getName() + "] must configure at least one constructor " - + "argument. If it doesn't have any it should use ObjectParser instead of ConstructingObjectParser. This is a bug " - + "in the parser declaration."; + assert false == constructorArgInfos.isEmpty() + : "[" + + objectParser.getName() + + "] must configure at least one constructor " + + "argument. If it doesn't have any it should use ObjectParser instead of ConstructingObjectParser. This is a bug " + + "in the parser declaration."; // All missing constructor arguments were optional. Just build the target and return it. buildTarget(); return targetObject; @@ -536,11 +560,17 @@ private void buildTarget() { queuedFields[queuedFieldsCount].accept(targetObject); } } catch (XContentParseException e) { - throw new XContentParseException(e.getLocation(), - "failed to build [" + objectParser.getName() + "] after last required field arrived", e); + throw new XContentParseException( + e.getLocation(), + "failed to build [" + objectParser.getName() + "] after last required field arrived", + e + ); } catch (Exception e) { - throw new XContentParseException(null, - "Failed to build [" + objectParser.getName() + "] after last required field arrived", e); + throw new XContentParseException( + null, + "Failed to build [" + objectParser.getName() + "] after last required field arrived", + e + ); } } } diff --git a/libs/x-content/src/main/java/org/elasticsearch/xcontent/DeprecationHandler.java b/libs/x-content/src/main/java/org/elasticsearch/xcontent/DeprecationHandler.java index 17457b48bdd3c..285d4cfe01092 100644 --- a/libs/x-content/src/main/java/org/elasticsearch/xcontent/DeprecationHandler.java +++ b/libs/x-content/src/main/java/org/elasticsearch/xcontent/DeprecationHandler.java @@ -25,32 +25,61 @@ public interface DeprecationHandler { @Override public void logReplacedField(String parserName, Supplier location, String oldName, String replacedName) { if (parserName != null) { - throw new UnsupportedOperationException("deprecated fields not supported in [" + parserName + "] but got [" - + oldName + "] at [" + location.get() + "] which is a deprecated name for [" + replacedName + "]"); + throw new UnsupportedOperationException( + "deprecated fields not supported in [" + + parserName + + "] but got [" + + oldName + + "] at [" + + location.get() + + "] which is a deprecated name for [" + + replacedName + + "]" + ); } else { - throw new UnsupportedOperationException("deprecated fields not supported here but got [" - + oldName + "] which is a deprecated name for [" + replacedName + "]"); + throw new UnsupportedOperationException( + "deprecated fields not supported here but got [" + oldName + "] which is a deprecated name for [" + replacedName + "]" + ); } } + @Override public void logRenamedField(String parserName, Supplier location, String oldName, String currentName) { if (parserName != null) { - throw new UnsupportedOperationException("deprecated fields not supported in [" + parserName + "] but got [" - + oldName + "] at [" + location.get() + "] which has been replaced with [" + currentName + "]"); + throw new UnsupportedOperationException( + "deprecated fields not supported in [" + + parserName + + "] but got [" + + oldName + + "] at [" + + location.get() + + "] which has been replaced with [" + + currentName + + "]" + ); } else { - throw new UnsupportedOperationException("deprecated fields not supported here but got [" - + oldName + "] which has been replaced with [" + currentName + "]"); + throw new UnsupportedOperationException( + "deprecated fields not supported here but got [" + oldName + "] which has been replaced with [" + currentName + "]" + ); } } @Override public void logRemovedField(String parserName, Supplier location, String removedName) { if (parserName != null) { - throw new UnsupportedOperationException("deprecated fields not supported in [" + parserName + "] but got [" - + removedName + "] at [" + location.get() + "] which has been deprecated entirely"); + throw new UnsupportedOperationException( + "deprecated fields not supported in [" + + parserName + + "] but got [" + + removedName + + "] at [" + + location.get() + + "] which has been deprecated entirely" + ); } else { - throw new UnsupportedOperationException("deprecated fields not supported here but got [" - + removedName + "] which has been deprecated entirely"); + throw new UnsupportedOperationException( + "deprecated fields not supported here but got [" + removedName + "] which has been deprecated entirely" + ); } } }; @@ -102,8 +131,13 @@ public void logRemovedField(String parserName, Supplier locati * @see DeprecationHandler#logRenamedField(String, Supplier, String, String) * Emits a compatible api warning instead of deprecation warning when isCompatibleDeprecation is true */ - default void logRenamedField(String parserName, Supplier location, String oldName, String currentName, - boolean isCompatibleDeprecation) { + default void logRenamedField( + String parserName, + Supplier location, + String oldName, + String currentName, + boolean isCompatibleDeprecation + ) { logRenamedField(parserName, location, oldName, currentName); } @@ -111,8 +145,13 @@ default void logRenamedField(String parserName, Supplier locat * @see DeprecationHandler#logReplacedField(String, Supplier, String, String) * Emits a compatible api warning instead of deprecation warning when isCompatibleDeprecation is true */ - default void logReplacedField(String parserName, Supplier location, String oldName, String replacedName, - boolean isCompatibleDeprecation) { + default void logReplacedField( + String parserName, + Supplier location, + String oldName, + String replacedName, + boolean isCompatibleDeprecation + ) { logReplacedField(parserName, location, oldName, replacedName); } @@ -120,8 +159,12 @@ default void logReplacedField(String parserName, Supplier loca * @see DeprecationHandler#logRemovedField(String, Supplier, String) * Emits a compatible api warning instead of deprecation warning when isCompatibleDeprecation is true */ - default void logRemovedField(String parserName, Supplier location, String removedName, - boolean isCompatibleDeprecation) { + default void logRemovedField( + String parserName, + Supplier location, + String removedName, + boolean isCompatibleDeprecation + ) { logRemovedField(parserName, location, removedName); } diff --git a/libs/x-content/src/main/java/org/elasticsearch/xcontent/FilterXContentParser.java b/libs/x-content/src/main/java/org/elasticsearch/xcontent/FilterXContentParser.java index 9803b3f621200..877c3daeff636 100644 --- a/libs/x-content/src/main/java/org/elasticsearch/xcontent/FilterXContentParser.java +++ b/libs/x-content/src/main/java/org/elasticsearch/xcontent/FilterXContentParser.java @@ -74,8 +74,8 @@ public Map mapStrings() throws IOException { } @Override - public Map map( - Supplier> mapFactory, CheckedFunction mapValueParser) throws IOException { + public Map map(Supplier> mapFactory, CheckedFunction mapValueParser) + throws IOException { return in.map(mapFactory, mapValueParser); } diff --git a/libs/x-content/src/main/java/org/elasticsearch/xcontent/InstantiatingObjectParser.java b/libs/x-content/src/main/java/org/elasticsearch/xcontent/InstantiatingObjectParser.java index 89ccb670c5c3a..c46c36f8afae8 100644 --- a/libs/x-content/src/main/java/org/elasticsearch/xcontent/InstantiatingObjectParser.java +++ b/libs/x-content/src/main/java/org/elasticsearch/xcontent/InstantiatingObjectParser.java @@ -74,7 +74,9 @@ * } */ public class InstantiatingObjectParser - implements BiFunction, ContextParser { + implements + BiFunction, + ContextParser { public static Builder builder(String name, boolean ignoreUnknownFields, Class valueClass) { return new Builder<>(name, ignoreUnknownFields, valueClass); @@ -101,7 +103,7 @@ public Builder(String name, boolean ignoreUnknownFields, Class valueClass this.valueClass = valueClass; } - @SuppressWarnings({"unchecked", "checkstyle:HiddenField"}) + @SuppressWarnings({ "unchecked", "checkstyle:HiddenField" }) public InstantiatingObjectParser build() { Constructor constructor = null; int neededArguments = constructingObjectParser.getNumberOfFields(); @@ -109,8 +111,11 @@ public InstantiatingObjectParser build() { for (Constructor c : valueClass.getConstructors()) { if (c.getAnnotation(ParserConstructor.class) != null) { if (constructor != null) { - throw new IllegalArgumentException("More then one public constructor with @ParserConstructor annotation exist in " + - "the class " + valueClass.getName()); + throw new IllegalArgumentException( + "More then one public constructor with @ParserConstructor annotation exist in " + + "the class " + + valueClass.getName() + ); } if (c.getParameterCount() < neededArguments || c.getParameterCount() > neededArguments + 1) { throw new IllegalArgumentException( @@ -130,43 +135,61 @@ public InstantiatingObjectParser build() { for (Constructor c : valueClass.getConstructors()) { if (c.getParameterCount() == neededArguments) { if (constructor != null) { - throw new IllegalArgumentException("More then one public constructor with " + neededArguments + - " arguments found. The use of @ParserConstructor annotation is required for class " + valueClass.getName()); + throw new IllegalArgumentException( + "More then one public constructor with " + + neededArguments + + " arguments found. The use of @ParserConstructor annotation is required for class " + + valueClass.getName() + ); } constructor = c; } } } if (constructor == null) { - throw new IllegalArgumentException("No public constructors with " + neededArguments + " parameters exist in the class " + - valueClass.getName()); + throw new IllegalArgumentException( + "No public constructors with " + neededArguments + " parameters exist in the class " + valueClass.getName() + ); } this.constructor = (Constructor) constructor; return new InstantiatingObjectParser<>(constructingObjectParser); } @Override - public void declareField(BiConsumer consumer, ContextParser parser, ParseField parseField, - ObjectParser.ValueType type) { + public void declareField( + BiConsumer consumer, + ContextParser parser, + ParseField parseField, + ObjectParser.ValueType type + ) { constructingObjectParser.declareField(consumer, parser, parseField, type); } @Override - public void declareNamedObject(BiConsumer consumer, ObjectParser.NamedObjectParser namedObjectParser, - ParseField parseField) { + public void declareNamedObject( + BiConsumer consumer, + ObjectParser.NamedObjectParser namedObjectParser, + ParseField parseField + ) { constructingObjectParser.declareNamedObject(consumer, namedObjectParser, parseField); } @Override - public void declareNamedObjects(BiConsumer> consumer, - ObjectParser.NamedObjectParser namedObjectParser, ParseField parseField) { + public void declareNamedObjects( + BiConsumer> consumer, + ObjectParser.NamedObjectParser namedObjectParser, + ParseField parseField + ) { constructingObjectParser.declareNamedObjects(consumer, namedObjectParser, parseField); } @Override - public void declareNamedObjects(BiConsumer> consumer, - ObjectParser.NamedObjectParser namedObjectParser, - Consumer orderedModeCallback, ParseField parseField) { + public void declareNamedObjects( + BiConsumer> consumer, + ObjectParser.NamedObjectParser namedObjectParser, + Consumer orderedModeCallback, + ParseField parseField + ) { constructingObjectParser.declareNamedObjects(consumer, namedObjectParser, orderedModeCallback, parseField); } @@ -187,8 +210,9 @@ public void declareExclusiveFieldSet(String... exclusiveSet) { private Value buildInstance(Object[] args, Context context) { if (constructor == null) { - throw new IllegalArgumentException("InstantiatingObjectParser for type " + valueClass.getName() + " has to be finalized " + - "before the first use"); + throw new IllegalArgumentException( + "InstantiatingObjectParser for type " + valueClass.getName() + " has to be finalized " + "before the first use" + ); } try { if (constructor.getParameterCount() != args.length) { @@ -205,7 +229,6 @@ private Value buildInstance(Object[] args, Context context) { } } - private final ConstructingObjectParser constructingObjectParser; private InstantiatingObjectParser(ConstructingObjectParser constructingObjectParser) { diff --git a/libs/x-content/src/main/java/org/elasticsearch/xcontent/MediaTypeRegistry.java b/libs/x-content/src/main/java/org/elasticsearch/xcontent/MediaTypeRegistry.java index d8a6cd796c933..bd2b4f6f5ed1f 100644 --- a/libs/x-content/src/main/java/org/elasticsearch/xcontent/MediaTypeRegistry.java +++ b/libs/x-content/src/main/java/org/elasticsearch/xcontent/MediaTypeRegistry.java @@ -50,7 +50,7 @@ public Map parametersFor(String typeWithSubtype) { return parametersMap.get(typeWithSubtype); } - public MediaTypeRegistry register(T[] mediaTypes ) { + public MediaTypeRegistry register(T[] mediaTypes) { for (T mediaType : mediaTypes) { Set tuples = mediaType.headerValues(); for (MediaType.HeaderValue headerValue : tuples) { @@ -62,7 +62,7 @@ public MediaTypeRegistry register(T[] mediaTypes ) { return this; } - private Map convertPatterns(Map paramNameAndValueRegex) { + private Map convertPatterns(Map paramNameAndValueRegex) { Map parametersForMediaType = new HashMap<>(paramNameAndValueRegex.size()); for (Map.Entry params : paramNameAndValueRegex.entrySet()) { String parameterName = params.getKey().toLowerCase(Locale.ROOT); diff --git a/libs/x-content/src/main/java/org/elasticsearch/xcontent/NamedXContentRegistry.java b/libs/x-content/src/main/java/org/elasticsearch/xcontent/NamedXContentRegistry.java index 1150207ebe712..99de00159fbd1 100644 --- a/libs/x-content/src/main/java/org/elasticsearch/xcontent/NamedXContentRegistry.java +++ b/libs/x-content/src/main/java/org/elasticsearch/xcontent/NamedXContentRegistry.java @@ -54,10 +54,15 @@ public Entry(Class categoryClass, ParseField name, CheckedFunction parser.apply(p), name.getForRestApiVersion()); } - public Entry(Class categoryClass, ParseField name, CheckedFunction parser, - Function restApiCompatibility) { + public Entry( + Class categoryClass, + ParseField name, + CheckedFunction parser, + Function restApiCompatibility + ) { this(categoryClass, name, (p, c) -> parser.apply(p), restApiCompatibility); } + /** * Creates a new entry which can be stored by the registry. * Prefer {@link Entry#Entry(Class, ParseField, CheckedFunction)} unless you need a context to carry around while parsing. @@ -66,8 +71,12 @@ public Entry(Class categoryClass, ParseField name, ContextParser Entry(Class categoryClass, ParseField name, ContextParser parser, - Function restApiCompatibility) { + public Entry( + Class categoryClass, + ParseField name, + ContextParser parser, + Function restApiCompatibility + ) { this.categoryClass = Objects.requireNonNull(categoryClass); this.name = Objects.requireNonNull(name); this.parser = Objects.requireNonNull(parser); @@ -75,20 +84,18 @@ public Entry(Class categoryClass, ParseField name, ContextParser, Map>> registry; - + private final Map, Map>> registry; public NamedXContentRegistry(List entries) { this.registry = unmodifiableMap(createRegistry(entries)); } - - private Map, Map>> createRegistry(List entries){ + private Map, Map>> createRegistry(List entries) { if (entries.isEmpty()) { return emptyMap(); } - Map, Map>> newRegistry = new HashMap<>(); + Map, Map>> newRegistry = new HashMap<>(); for (Entry entry : entries) { for (String name : entry.name.getAllNamesIncludedDeprecated()) { if (RestApiVersion.minimumSupported().matches(entry.restApiCompatibility)) { @@ -102,19 +109,29 @@ private Map, Map>> createRegistry(Li return newRegistry; } - private void registerParsers(Map, Map>> newRegistry, - Entry entry, - String name, - RestApiVersion restApiVersion) { - final Map, Map> classRegistry = - newRegistry.computeIfAbsent(restApiVersion, (v) -> new HashMap<>()); - final Map parsers = - classRegistry.computeIfAbsent(entry.categoryClass, (v) -> new HashMap<>()); + private void registerParsers( + Map, Map>> newRegistry, + Entry entry, + String name, + RestApiVersion restApiVersion + ) { + final Map, Map> classRegistry = newRegistry.computeIfAbsent(restApiVersion, (v) -> new HashMap<>()); + final Map parsers = classRegistry.computeIfAbsent(entry.categoryClass, (v) -> new HashMap<>()); Object old = parsers.put(name, entry); if (old != null) { - throw new IllegalArgumentException("NamedXContent [" + entry.categoryClass.getName() + "][" + entry.name + "]" + - " is already registered for [" + old.getClass().getName() + "]," + - " cannot register [" + entry.parser.getClass().getName() + "]"); + throw new IllegalArgumentException( + "NamedXContent [" + + entry.categoryClass.getName() + + "][" + + entry.name + + "]" + + " is already registered for [" + + old.getClass().getName() + + "]," + + " cannot register [" + + entry.parser.getClass().getName() + + "]" + ); } } @@ -130,10 +147,9 @@ public T parseNamedObject(Class categoryClass, String name, XContentPa return categoryClass.cast(entry.parser.parse(parser, context)); } - //scope for testing + // scope for testing public Entry lookupParser(Class categoryClass, String name, XContentParser parser) { - Map parsers = registry.getOrDefault(parser.getRestApiVersion(), emptyMap()) - .get(categoryClass); + Map parsers = registry.getOrDefault(parser.getRestApiVersion(), emptyMap()).get(categoryClass); if (parsers == null) { if (registry.isEmpty()) { // The "empty" registry will never work so we throw a better exception as a hint. @@ -148,8 +164,10 @@ public Entry lookupParser(Class categoryClass, String name, XContentParse if (false == entry.name.match(name, parser.getDeprecationHandler())) { /* Note that this shouldn't happen because we already looked up the entry using the names but we need to call `match` anyway * because it is responsible for logging deprecation warnings. */ - throw new XContentParseException(parser.getTokenLocation(), - "unable to parse " + categoryClass.getSimpleName() + " with name [" + name + "]: parser didn't match"); + throw new XContentParseException( + parser.getTokenLocation(), + "unable to parse " + categoryClass.getSimpleName() + " with name [" + name + "]: parser didn't match" + ); } return entry; } diff --git a/libs/x-content/src/main/java/org/elasticsearch/xcontent/ObjectParser.java b/libs/x-content/src/main/java/org/elasticsearch/xcontent/ObjectParser.java index 6eb8bef86ebe7..fa5ce84155065 100644 --- a/libs/x-content/src/main/java/org/elasticsearch/xcontent/ObjectParser.java +++ b/libs/x-content/src/main/java/org/elasticsearch/xcontent/ObjectParser.java @@ -61,7 +61,9 @@ * {@link #declareField} which can be used to implement exceptional parsing operations not covered by the high level methods. */ public final class ObjectParser extends AbstractObjectParser - implements BiFunction, ContextParser{ + implements + BiFunction, + ContextParser { private final List requiredFieldSets = new ArrayList<>(); private final List exclusiveFieldSets = new ArrayList<>(); @@ -69,8 +71,10 @@ public final class ObjectParser extends AbstractObjectParser BiConsumer> fromList(Class c, - BiConsumer consumer) { + public static BiConsumer> fromList( + Class c, + BiConsumer consumer + ) { return (Value v, List l) -> { @SuppressWarnings("unchecked") ElementValue[] array = (ElementValue[]) Array.newInstance(c, l.size()); @@ -79,19 +83,30 @@ public static BiConsumer> fromLi } private interface UnknownFieldParser { - void acceptUnknownField(ObjectParser objectParser, String field, XContentLocation location, XContentParser parser, - Value value, Context context) throws IOException; + void acceptUnknownField( + ObjectParser objectParser, + String field, + XContentLocation location, + XContentParser parser, + Value value, + Context context + ) throws IOException; } private static UnknownFieldParser ignoreUnknown() { - return (op, f, l, p, v, c) -> p.skipChildren(); + return (op, f, l, p, v, c) -> p.skipChildren(); } private static UnknownFieldParser errorOnUnknown() { return (op, f, l, p, v, c) -> { - throw new XContentParseException(l, ErrorOnUnknown.IMPLEMENTATION.errorMessage(op.name, f, - op.fieldParserMap.getOrDefault(p.getRestApiVersion(), Collections.emptyMap()) - .keySet())); + throw new XContentParseException( + l, + ErrorOnUnknown.IMPLEMENTATION.errorMessage( + op.name, + f, + op.fieldParserMap.getOrDefault(p.getRestApiVersion(), Collections.emptyMap()).keySet() + ) + ); }; } @@ -125,8 +140,10 @@ private static UnknownFieldParser consumeUnknow consumer.accept(value, field, parser.list()); break; default: - throw new XContentParseException(parser.getTokenLocation(), - "[" + objectParser.name + "] cannot parse field [" + field + "] with value type [" + t + "]"); + throw new XContentParseException( + parser.getTokenLocation(), + "[" + objectParser.name + "] cannot parse field [" + field + "] with value type [" + t + "]" + ); } }; } @@ -140,9 +157,9 @@ private static UnknownFieldParser unk try { o = parser.namedObject(categoryClass, field, context); } catch (NamedObjectNotFoundException e) { - Set candidates = new HashSet<>(objectParser.fieldParserMap - .getOrDefault(parser.getRestApiVersion(), Collections.emptyMap()) - .keySet()); + Set candidates = new HashSet<>( + objectParser.fieldParserMap.getOrDefault(parser.getRestApiVersion(), Collections.emptyMap()).keySet() + ); e.getCandidates().forEach(candidates::add); String message = ErrorOnUnknown.IMPLEMENTATION.errorMessage(objectParser.name, field, candidates); throw new XContentParseException(location, message, e); @@ -196,9 +213,7 @@ public ObjectParser(String name, boolean ignoreUnknownFields, @Nullable Supplier } private static Function wrapValueSupplier(@Nullable Supplier valueSupplier) { - return valueSupplier == null ? c -> { - throw new NullPointerException(); - } : c -> valueSupplier.get(); + return valueSupplier == null ? c -> { throw new NullPointerException(); } : c -> valueSupplier.get(); } /** @@ -234,13 +249,14 @@ public ObjectParser( * @param unknownFieldParser how to parse unknown fields * @param valueBuilder builds the value from the context. Used when the ObjectParser is not passed a value. */ - private ObjectParser(String name, UnknownFieldParser unknownFieldParser, - @Nullable Function valueBuilder) { + private ObjectParser( + String name, + UnknownFieldParser unknownFieldParser, + @Nullable Function valueBuilder + ) { this.name = name; this.unknownFieldParser = unknownFieldParser; - this.valueBuilder = valueBuilder == null ? c -> { - throw new NullPointerException("valueBuilder is not set"); - } : valueBuilder; + this.valueBuilder = valueBuilder == null ? c -> { throw new NullPointerException("valueBuilder is not set"); } : valueBuilder; } /** @@ -330,11 +346,11 @@ public Value parse(XContentParser parser, Value value, Context context) throws I } private void throwExpectedStartObject(XContentParser parser, XContentParser.Token token) { - throw new XContentParseException(parser.getTokenLocation(), "[" + name + "] Expected START_OBJECT but was: " + token); + throw new XContentParseException(parser.getTokenLocation(), "[" + name + "] Expected START_OBJECT but was: " + token); } private void throwNoFieldFound(XContentParser parser) { - throw new XContentParseException(parser.getTokenLocation(), "[" + name + "] no field found"); + throw new XContentParseException(parser.getTokenLocation(), "[" + name + "] no field found"); } private void throwMissingRequiredFields(List requiredFields) { @@ -388,13 +404,14 @@ public Value apply(XContentParser parser, Context context) { try { return parse(parser, context); } catch (IOException e) { - throw new XContentParseException(parser.getTokenLocation(), "[" + name + "] failed to parse object", e); + throw new XContentParseException(parser.getTokenLocation(), "[" + name + "] failed to parse object", e); } } public interface Parser { void parse(XContentParser parser, Value value, Context context) throws IOException; } + public void declareField(Parser p, ParseField parseField, ValueType type) { if (parseField == null) { throw new IllegalArgumentException("[parseField] is required"); @@ -406,16 +423,17 @@ public void declareField(Parser p, ParseField parseField, ValueT for (String fieldValue : parseField.getAllNamesIncludedDeprecated()) { if (RestApiVersion.minimumSupported().matches(parseField.getForRestApiVersion())) { - Map nameToParserMap = - fieldParserMap.computeIfAbsent(RestApiVersion.minimumSupported(), (v) -> new HashMap<>()); + Map nameToParserMap = fieldParserMap.computeIfAbsent( + RestApiVersion.minimumSupported(), + (v) -> new HashMap<>() + ); FieldParser previousValue = nameToParserMap.putIfAbsent(fieldValue, fieldParser); if (previousValue != null) { throw new IllegalArgumentException("Parser already registered for name=[" + fieldValue + "]. " + previousValue); } } if (RestApiVersion.current().matches(parseField.getForRestApiVersion())) { - Map nameToParserMap = - fieldParserMap.computeIfAbsent(RestApiVersion.current(), (v) -> new HashMap<>()); + Map nameToParserMap = fieldParserMap.computeIfAbsent(RestApiVersion.current(), (v) -> new HashMap<>()); FieldParser previousValue = nameToParserMap.putIfAbsent(fieldValue, fieldParser); if (previousValue != null) { throw new IllegalArgumentException("Parser already registered for name=[" + fieldValue + "]. " + previousValue); @@ -426,8 +444,7 @@ public void declareField(Parser p, ParseField parseField, ValueT } @Override - public void declareField(BiConsumer consumer, ContextParser parser, ParseField parseField, - ValueType type) { + public void declareField(BiConsumer consumer, ContextParser parser, ParseField parseField, ValueType type) { if (consumer == null) { throw new IllegalArgumentException("[consumer] is required"); } @@ -437,8 +454,12 @@ public void declareField(BiConsumer consumer, ContextParser consumer.accept(v, parser.parse(p, c)), parseField, type); } - public void declareObjectOrDefault(BiConsumer consumer, BiFunction objectParser, - Supplier defaultValue, ParseField field) { + public void declareObjectOrDefault( + BiConsumer consumer, + BiFunction objectParser, + Supplier defaultValue, + ParseField field + ) { declareField((p, v, c) -> { if (p.currentToken() == XContentParser.Token.VALUE_BOOLEAN) { if (p.booleanValue()) { @@ -451,8 +472,7 @@ public void declareObjectOrDefault(BiConsumer consumer, BiFunction } @Override - public void declareNamedObject(BiConsumer consumer, NamedObjectParser namedObjectParser, - ParseField field) { + public void declareNamedObject(BiConsumer consumer, NamedObjectParser namedObjectParser, ParseField field) { BiFunction objectParser = (XContentParser p, Context c) -> { try { @@ -477,8 +497,12 @@ public void declareNamedObject(BiConsumer consumer, NamedObjectPar } @Override - public void declareNamedObjects(BiConsumer> consumer, NamedObjectParser namedObjectParser, - Consumer orderedModeCallback, ParseField field) { + public void declareNamedObjects( + BiConsumer> consumer, + NamedObjectParser namedObjectParser, + Consumer orderedModeCallback, + ParseField field + ) { // This creates and parses the named object BiFunction objectParser = (XContentParser p, Context c) -> { if (p.currentToken() != XContentParser.Token.FIELD_NAME) { @@ -511,10 +535,15 @@ public void declareNamedObjects(BiConsumer> consumer, NamedOb }, field, ValueType.OBJECT_ARRAY); } - private void parseObjectsInArray(Consumer orderedModeCallback, - ParseField field, BiFunction objectParser, - XContentParser p, Value v, Context c, - List fields) throws IOException { + private void parseObjectsInArray( + Consumer orderedModeCallback, + ParseField field, + BiFunction objectParser, + XContentParser p, + Value v, + Context c, + List fields + ) throws IOException { orderedModeCallback.accept(v); XContentParser.Token token; while ((token = p.nextToken()) != XContentParser.Token.END_ARRAY) { @@ -531,8 +560,13 @@ private void parseObjectsInArray(Consumer orderedModeCallback, } private XContentParseException wrapCanBeObjectOrArrayOfObjects(ParseField field, XContentParser p) { - return new XContentParseException(p.getTokenLocation(), "[" + field + "] can be a single object with any number of " - + "fields or an array where each entry is an object with a single field"); + return new XContentParseException( + p.getTokenLocation(), + "[" + + field + + "] can be a single object with any number of " + + "fields or an array where each entry is an object with a single field" + ); } private XContentParseException wrapParseError(ParseField field, XContentParser p, IOException e, String s) { @@ -544,8 +578,11 @@ private XContentParseException rethrowFieldParseFailure(ParseField field, XConte } @Override - public void declareNamedObjects(BiConsumer> consumer, NamedObjectParser namedObjectParser, - ParseField field) { + public void declareNamedObjects( + BiConsumer> consumer, + NamedObjectParser namedObjectParser, + ParseField field + ) { Consumer orderedModeCallback = (v) -> { throw new IllegalArgumentException("[" + field + "] doesn't support arrays. Use a single object with multiple fields."); }; @@ -675,12 +712,16 @@ void assertSupports(String parserName, XContentParser xContentParser, String cur xContentParser.getDeprecationHandler() ); if (match == false) { - throw new XContentParseException(xContentParser.getTokenLocation(), - "[" + parserName + "] parsefield doesn't accept: " + currentFieldName); + throw new XContentParseException( + xContentParser.getTokenLocation(), + "[" + parserName + "] parsefield doesn't accept: " + currentFieldName + ); } if (supportedTokens.contains(xContentParser.currentToken()) == false) { - throw new XContentParseException(xContentParser.getTokenLocation(), - "[" + parserName + "] " + currentFieldName + " doesn't support values of type: " + xContentParser.currentToken()); + throw new XContentParseException( + xContentParser.getTokenLocation(), + "[" + parserName + "] " + currentFieldName + " doesn't support values of type: " + xContentParser.currentToken() + ); } } @@ -690,15 +731,18 @@ public String toString() { String allReplacedWith = parseField.getAllReplacedWith(); String deprecated = ""; if (deprecatedNames != null && deprecatedNames.length > 0) { - deprecated = ", deprecated_names=" + Arrays.toString(deprecatedNames); + deprecated = ", deprecated_names=" + Arrays.toString(deprecatedNames); } - return "FieldParser{" + - "preferred_name=" + parseField.getPreferredName() + - ", supportedTokens=" + supportedTokens + - deprecated + - (allReplacedWith == null ? "" : ", replaced_with=" + allReplacedWith) + - ", type=" + type.name() + - '}'; + return "FieldParser{" + + "preferred_name=" + + parseField.getPreferredName() + + ", supportedTokens=" + + supportedTokens + + deprecated + + (allReplacedWith == null ? "" : ", replaced_with=" + allReplacedWith) + + ", type=" + + type.name() + + '}'; } } @@ -748,9 +792,6 @@ public EnumSet supportedTokens() { @Override public String toString() { - return "ObjectParser{" + - "name='" + name + '\'' + - ", fields=" + fieldParserMap + - '}'; + return "ObjectParser{" + "name='" + name + '\'' + ", fields=" + fieldParserMap + '}'; } } diff --git a/libs/x-content/src/main/java/org/elasticsearch/xcontent/ObjectPath.java b/libs/x-content/src/main/java/org/elasticsearch/xcontent/ObjectPath.java index 84b588b1d1a21..b9cde4bd8769f 100644 --- a/libs/x-content/src/main/java/org/elasticsearch/xcontent/ObjectPath.java +++ b/libs/x-content/src/main/java/org/elasticsearch/xcontent/ObjectPath.java @@ -19,8 +19,7 @@ public final class ObjectPath { private static final String[] EMPTY_ARRAY = new String[0]; - private ObjectPath() { - } + private ObjectPath() {} /** * Return the value within a given object at the specified path, or diff --git a/libs/x-content/src/main/java/org/elasticsearch/xcontent/ParseField.java b/libs/x-content/src/main/java/org/elasticsearch/xcontent/ParseField.java index 10415f62eb62a..8fd613b4f8a83 100644 --- a/libs/x-content/src/main/java/org/elasticsearch/xcontent/ParseField.java +++ b/libs/x-content/src/main/java/org/elasticsearch/xcontent/ParseField.java @@ -31,9 +31,13 @@ public class ParseField { private static final String[] EMPTY = new String[0]; - - private ParseField(String name, Function forRestApiVersion, String[] deprecatedNames, - boolean fullyDeprecated, String allReplacedWith) { + private ParseField( + String name, + Function forRestApiVersion, + String[] deprecatedNames, + boolean fullyDeprecated, + String allReplacedWith + ) { this.name = name; this.fullyDeprecated = fullyDeprecated; this.allReplacedWith = allReplacedWith; @@ -60,8 +64,7 @@ private ParseField(String name, Function forRestApiVers * accepted when strict matching is used. */ public ParseField(String name, String... deprecatedNames) { - this(name, RestApiVersion.onOrAfter(RestApiVersion.minimumSupported()) ,deprecatedNames, - false, null); + this(name, RestApiVersion.onOrAfter(RestApiVersion.minimumSupported()), deprecatedNames, false, null); } /** @@ -90,14 +93,12 @@ public ParseField withDeprecation(String... deprecatedNamesOverride) { return new ParseField(this.name, this.forRestApiVersion, deprecatedNamesOverride, this.fullyDeprecated, this.allReplacedWith); } - /** * Creates a new field with current name and deprecatedNames, but overrides forRestApiVersion * @param forRestApiVersionOverride - a boolean function indicating for what version a deprecated name is available */ public ParseField forRestApiVersion(Function forRestApiVersionOverride) { - return new ParseField(this.name, forRestApiVersionOverride, this.deprecatedNames, - this.fullyDeprecated, this.allReplacedWith); + return new ParseField(this.name, forRestApiVersionOverride, this.deprecatedNames, this.fullyDeprecated, this.allReplacedWith); } /** @@ -112,16 +113,20 @@ public Function getForRestApiVersion() { * with {@code allReplacedWith}. */ public ParseField withAllDeprecated(String allReplacedWithOverride) { - return new ParseField(this.name, this.forRestApiVersion, getAllNamesIncludedDeprecated(), - this.fullyDeprecated, allReplacedWithOverride); + return new ParseField( + this.name, + this.forRestApiVersion, + getAllNamesIncludedDeprecated(), + this.fullyDeprecated, + allReplacedWithOverride + ); } /** * Return a new ParseField where all field names are deprecated with no replacement */ public ParseField withAllDeprecated() { - return new ParseField(this.name, this.forRestApiVersion, getAllNamesIncludedDeprecated(), - true, this.allReplacedWith); + return new ParseField(this.name, this.forRestApiVersion, getAllNamesIncludedDeprecated(), true, this.allReplacedWith); } /** @@ -155,8 +160,8 @@ public boolean match(String parserName, Supplier location, Str if (fullyDeprecated == false && allReplacedWith == null && fieldName.equals(name)) { return true; } - boolean isCompatibleDeprecation = RestApiVersion.minimumSupported().matches(forRestApiVersion) && - RestApiVersion.current().matches(forRestApiVersion) == false; + boolean isCompatibleDeprecation = RestApiVersion.minimumSupported().matches(forRestApiVersion) + && RestApiVersion.current().matches(forRestApiVersion) == false; // Now try to match against one of the deprecated names. Note that if // the parse field is entirely deprecated (allReplacedWith != null) all @@ -199,7 +204,6 @@ public String[] getDeprecatedNames() { return deprecatedNames; } - public static class CommonFields { public static final ParseField FIELD = new ParseField("field"); public static final ParseField FIELDS = new ParseField("fields"); diff --git a/libs/x-content/src/main/java/org/elasticsearch/xcontent/ParsedMediaType.java b/libs/x-content/src/main/java/org/elasticsearch/xcontent/ParsedMediaType.java index dc414a48acc8f..71598d28af7d6 100644 --- a/libs/x-content/src/main/java/org/elasticsearch/xcontent/ParsedMediaType.java +++ b/libs/x-content/src/main/java/org/elasticsearch/xcontent/ParsedMediaType.java @@ -65,7 +65,8 @@ public static ParsedMediaType parseMediaType(String headerValue) { final String[] elements = headerValue.toLowerCase(Locale.ROOT).split("[\\s\\t]*;"); final String[] splitMediaType = elements[0].split("/"); - if ((splitMediaType.length == 2 && TCHAR_PATTERN.matcher(splitMediaType[0].trim()).matches() + if ((splitMediaType.length == 2 + && TCHAR_PATTERN.matcher(splitMediaType[0].trim()).matches() && TCHAR_PATTERN.matcher(splitMediaType[1].trim()).matches()) == false) { throw new IllegalArgumentException("invalid media-type [" + headerValue + "]"); } @@ -78,7 +79,7 @@ public static ParsedMediaType parseMediaType(String headerValue) { if (paramsAsString.isEmpty()) { continue; } - //spaces are allowed between parameters, but not between '=' sign + // spaces are allowed between parameters, but not between '=' sign String[] keyValueParam = paramsAsString.split("="); if (keyValueParam.length != 2 || hasTrailingSpace(keyValueParam[0]) || hasLeadingSpace(keyValueParam[1])) { throw new IllegalArgumentException("invalid parameters for header [" + headerValue + "]"); @@ -87,8 +88,12 @@ public static ParsedMediaType parseMediaType(String headerValue) { String parameterValue = keyValueParam[1].toLowerCase(Locale.ROOT).trim(); parameters.put(parameterName, parameterValue); } - return new ParsedMediaType(headerValue, splitMediaType[0].trim().toLowerCase(Locale.ROOT), - splitMediaType[1].trim().toLowerCase(Locale.ROOT), parameters); + return new ParsedMediaType( + headerValue, + splitMediaType[0].trim().toLowerCase(Locale.ROOT), + splitMediaType[1].trim().toLowerCase(Locale.ROOT), + parameters + ); } } return null; @@ -97,8 +102,7 @@ public static ParsedMediaType parseMediaType(String headerValue) { public static ParsedMediaType parseMediaType(XContentType requestContentType, Map parameters) { ParsedMediaType parsedMediaType = requestContentType.toParsedMediaType(); - return new ParsedMediaType(parsedMediaType.originalHeaderValue, - parsedMediaType.type, parsedMediaType.subType, parameters); + return new ParsedMediaType(parsedMediaType.originalHeaderValue, parsedMediaType.type, parsedMediaType.subType, parameters); } // simplistic check for media ranges. do not validate if this is a correct header @@ -107,19 +111,20 @@ private static boolean isMediaRange(String headerValue) { } private static boolean hasTrailingSpace(String s) { - return s.length() == 0 || Character.isWhitespace(s.charAt(s.length()-1)); + return s.length() == 0 || Character.isWhitespace(s.charAt(s.length() - 1)); } private static boolean hasLeadingSpace(String s) { return s.length() == 0 || Character.isWhitespace(s.charAt(0)); } + /** * Resolves this instance to a MediaType instance defined in given MediaTypeRegistry. * Performs validation against parameters. * @param mediaTypeRegistry a registry where a mapping between a raw media type to an instance MediaType is defined * @return a MediaType instance or null if no media type could be found or if a known parameter do not passes validation */ - public T toMediaType(MediaTypeRegistry mediaTypeRegistry) { + public T toMediaType(MediaTypeRegistry mediaTypeRegistry) { T someType = mediaTypeRegistry.typeWithSubtypeToMediaType(mediaTypeWithoutParameters()); if (someType != null) { @@ -139,7 +144,7 @@ private boolean isValidParameter(String paramName, String value, Map params) { + // used in testing + public String responseContentTypeHeader(Map params) { return mediaTypeWithoutParameters() + formatParameters(params); } private String formatParameters(Map params) { - String joined = params.entrySet().stream() - .map(e -> e.getKey() + "=" + e.getValue()) - .collect(Collectors.joining(";")); + String joined = params.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.joining(";")); return joined.isEmpty() ? "" : ";" + joined; } diff --git a/libs/x-content/src/main/java/org/elasticsearch/xcontent/XContent.java b/libs/x-content/src/main/java/org/elasticsearch/xcontent/XContent.java index 227518b44c209..2d9eb09653251 100644 --- a/libs/x-content/src/main/java/org/elasticsearch/xcontent/XContent.java +++ b/libs/x-content/src/main/java/org/elasticsearch/xcontent/XContent.java @@ -8,8 +8,8 @@ package org.elasticsearch.xcontent; -import org.elasticsearch.xcontent.support.filtering.FilterPath; import org.elasticsearch.core.RestApiVersion; +import org.elasticsearch.xcontent.support.filtering.FilterPath; import java.io.IOException; import java.io.InputStream; @@ -79,12 +79,23 @@ XContentParser createParser(NamedXContentRegistry xContentRegistry, DeprecationH /** * Creates a parser over the provided bytes. */ - XContentParser createParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, byte[] data, int offset, int length) throws IOException; + XContentParser createParser( + NamedXContentRegistry xContentRegistry, + DeprecationHandler deprecationHandler, + byte[] data, + int offset, + int length + ) throws IOException; - XContentParser createParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, byte[] data, int offset, int length, FilterPath[] includes, - FilterPath[] excludes) throws IOException; + XContentParser createParser( + NamedXContentRegistry xContentRegistry, + DeprecationHandler deprecationHandler, + byte[] data, + int offset, + int length, + FilterPath[] includes, + FilterPath[] excludes + ) throws IOException; /** * Creates a parser over the provided reader. @@ -97,11 +108,20 @@ XContentParser createParser(NamedXContentRegistry xContentRegistry, DeprecationH * * @param restApiVersion - indicates if the N-1 or N compatible XContent parsing logic will be used. */ - XContentParser createParserForCompatibility(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, - InputStream is, RestApiVersion restApiVersion) throws IOException; + XContentParser createParserForCompatibility( + NamedXContentRegistry xContentRegistry, + DeprecationHandler deprecationHandler, + InputStream is, + RestApiVersion restApiVersion + ) throws IOException; - XContentParser createParserForCompatibility(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, byte[] data, int offset, int length, - RestApiVersion restApiVersion) throws IOException; + XContentParser createParserForCompatibility( + NamedXContentRegistry xContentRegistry, + DeprecationHandler deprecationHandler, + byte[] data, + int offset, + int length, + RestApiVersion restApiVersion + ) throws IOException; } diff --git a/libs/x-content/src/main/java/org/elasticsearch/xcontent/XContentBuilder.java b/libs/x-content/src/main/java/org/elasticsearch/xcontent/XContentBuilder.java index 6920e8b85c740..91bd5af8237ff 100644 --- a/libs/x-content/src/main/java/org/elasticsearch/xcontent/XContentBuilder.java +++ b/libs/x-content/src/main/java/org/elasticsearch/xcontent/XContentBuilder.java @@ -69,11 +69,14 @@ public static XContentBuilder builder(XContent xContent) throws IOException { * @throws IOException if an {@link IOException} occurs while building the content */ public static XContentBuilder builder(XContent xContent, RestApiVersion restApiVersion) throws IOException { - return new XContentBuilder(xContent, new ByteArrayOutputStream(), + return new XContentBuilder( + xContent, + new ByteArrayOutputStream(), Collections.emptySet(), Collections.emptySet(), xContent.type().toParsedMediaType(), - restApiVersion); + restApiVersion + ); } /** @@ -89,8 +92,14 @@ public static XContentBuilder builder(XContent xContent, RestApiVersion restApiV * @throws IOException if an {@link IOException} occurs while building the content */ public static XContentBuilder builder(XContentType xContentType, Set includes, Set excludes) throws IOException { - return new XContentBuilder(xContentType.xContent(), new ByteArrayOutputStream(), includes, excludes, - xContentType.toParsedMediaType(), RestApiVersion.current()); + return new XContentBuilder( + xContentType.xContent(), + new ByteArrayOutputStream(), + includes, + excludes, + xContentType.toParsedMediaType(), + RestApiVersion.current() + ); } private static final Map, Writer> WRITERS; @@ -135,12 +144,13 @@ public static XContentBuilder builder(XContentType xContentType, Set inc Map, HumanReadableTransformer> addlTransformers = service.getXContentHumanReadableTransformers(); Map, Function> addlDateTransformers = service.getDateTransformers(); - addlWriters.forEach((key, value) -> Objects.requireNonNull(value, - "invalid null xcontent writer for class " + key)); - addlTransformers.forEach((key, value) -> Objects.requireNonNull(value, - "invalid null xcontent transformer for human readable class " + key)); - dateTransformers.forEach((key, value) -> Objects.requireNonNull(value, - "invalid null xcontent date transformer for class " + key)); + addlWriters.forEach((key, value) -> Objects.requireNonNull(value, "invalid null xcontent writer for class " + key)); + addlTransformers.forEach( + (key, value) -> Objects.requireNonNull(value, "invalid null xcontent transformer for human readable class " + key) + ); + dateTransformers.forEach( + (key, value) -> Objects.requireNonNull(value, "invalid null xcontent date transformer for class " + key) + ); writers.putAll(addlWriters); humanReadableTransformer.putAll(addlTransformers); @@ -184,8 +194,6 @@ public interface HumanReadableTransformer { */ private boolean humanReadable = false; - - /** * Constructs a new builder using the provided XContent and an OutputStream. Make sure * to call {@link #close()} when the builder is done with. @@ -193,6 +201,7 @@ public interface HumanReadableTransformer { public XContentBuilder(XContent xContent, OutputStream bos) throws IOException { this(xContent, bos, Collections.emptySet(), Collections.emptySet(), xContent.type().toParsedMediaType(), RestApiVersion.current()); } + /** * Constructs a new builder using the provided XContent, an OutputStream and * some filters. If filters are specified, only those values matching a @@ -214,8 +223,13 @@ public XContentBuilder(XContentType xContentType, OutputStream bos, Set * @param excludes the exclusive filters: only fields and objects that don't match the exclusive filters will be written to the output. * @param responseContentType a content-type header value to be send back on a response */ - public XContentBuilder(XContent xContent, OutputStream os, Set includes, Set excludes, - ParsedMediaType responseContentType) throws IOException { + public XContentBuilder( + XContent xContent, + OutputStream os, + Set includes, + Set excludes, + ParsedMediaType responseContentType + ) throws IOException { this(xContent, os, includes, excludes, responseContentType, RestApiVersion.current()); } @@ -233,8 +247,14 @@ public XContentBuilder(XContent xContent, OutputStream os, Set includes, * @param responseContentType a content-type header value to be send back on a response * @param restApiVersion a rest api version indicating with which version the XContent is compatible with. */ - public XContentBuilder(XContent xContent, OutputStream os, Set includes, Set excludes, - ParsedMediaType responseContentType, RestApiVersion restApiVersion) throws IOException { + public XContentBuilder( + XContent xContent, + OutputStream os, + Set includes, + Set excludes, + ParsedMediaType responseContentType, + RestApiVersion restApiVersion + ) throws IOException { this.bos = os; assert responseContentType != null : "generated response cannot be null"; this.responseContentType = responseContentType; @@ -645,7 +665,6 @@ public XContentBuilder value(BigInteger value) throws IOException { return this; } - //////////////////////////////////////////////////////////////////////////// // BigDecimal ////////////////////////////////// @@ -763,7 +782,6 @@ public XContentBuilder utf8Value(byte[] bytes, int offset, int length) throws IO return this; } - //////////////////////////////////////////////////////////////////////////// // Date ////////////////////////////////// @@ -784,8 +802,7 @@ public XContentBuilder timeField(String name, Object timeValue) throws IOExcepti * {@link Long} class. */ public XContentBuilder timeField(String name, String readableName, long value) throws IOException { - assert name.equals(readableName) == false : - "expected raw and readable field names to differ, but they were both: " + name; + assert name.equals(readableName) == false : "expected raw and readable field names to differ, but they were both: " + name; if (humanReadable) { Function longTransformer = DATE_TRANSFORMERS.get(Long.class); if (longTransformer == null) { @@ -875,7 +892,7 @@ private void unknownValue(Object value, boolean ensureNoSelfReferences) throws I if (writer != null) { writer.write(this, value); } else if (value instanceof Path) { - //Path implements Iterable and causes endless recursion and a StackOverFlow if treated as an Iterable here + // Path implements Iterable and causes endless recursion and a StackOverFlow if treated as an Iterable here value((Path) value); } else if (value instanceof Map) { @SuppressWarnings("unchecked") @@ -1049,7 +1066,7 @@ private XContentBuilder value(Iterable values, boolean ensureNoSelfReferences } if (values instanceof Path) { - //treat as single value + // treat as single value value((Path) values); } else { // checks that the iterable does not contain references to itself because @@ -1076,8 +1093,8 @@ private XContentBuilder value(Iterable values, boolean ensureNoSelfReferences ////////////////////////////////// public XContentBuilder humanReadableField(String rawFieldName, String readableFieldName, Object value) throws IOException { - assert rawFieldName.equals(readableFieldName) == false : - "expected raw and readable field names to differ, but they were both: " + rawFieldName; + assert rawFieldName.equals(readableFieldName) == false + : "expected raw and readable field names to differ, but they were both: " + rawFieldName; if (humanReadable) { field(readableFieldName, Objects.toString(value)); } @@ -1095,10 +1112,9 @@ public XContentBuilder humanReadableField(String rawFieldName, String readableFi // Misc. ////////////////////////////////// - public XContentBuilder percentageField(String rawFieldName, String readableFieldName, double percentage) throws IOException { - assert rawFieldName.equals(readableFieldName) == false : - "expected raw and readable field names to differ, but they were both: " + rawFieldName; + assert rawFieldName.equals(readableFieldName) == false + : "expected raw and readable field names to differ, but they were both: " + rawFieldName; if (humanReadable) { field(readableFieldName, String.format(Locale.ROOT, "%1.1f%%", percentage)); } @@ -1221,7 +1237,7 @@ private static Iterable convert(Object value) { return null; } if (value instanceof Map) { - return ((Map) value).values(); + return ((Map) value).values(); } else if ((value instanceof Iterable) && (value instanceof Path == false)) { return (Iterable) value; } else if (value instanceof Object[]) { diff --git a/libs/x-content/src/main/java/org/elasticsearch/xcontent/XContentFactory.java b/libs/x-content/src/main/java/org/elasticsearch/xcontent/XContentFactory.java index 02c4fe15ebfd2..cca70b2f1a00c 100644 --- a/libs/x-content/src/main/java/org/elasticsearch/xcontent/XContentFactory.java +++ b/libs/x-content/src/main/java/org/elasticsearch/xcontent/XContentFactory.java @@ -10,6 +10,7 @@ import com.fasterxml.jackson.dataformat.cbor.CBORConstants; import com.fasterxml.jackson.dataformat.smile.SmileConstants; + import org.elasticsearch.xcontent.cbor.CborXContent; import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xcontent.smile.SmileXContent; @@ -146,9 +147,9 @@ public static XContentType xContentType(CharSequence content) { } // Should we throw a failure here? Smile idea is to use it in bytes.... if (length > 2 - && first == SmileConstants.HEADER_BYTE_1 - && content.charAt(1) == SmileConstants.HEADER_BYTE_2 - && content.charAt(2) == SmileConstants.HEADER_BYTE_3) { + && first == SmileConstants.HEADER_BYTE_1 + && content.charAt(1) == SmileConstants.HEADER_BYTE_2 + && content.charAt(2) == SmileConstants.HEADER_BYTE_3) { return XContentType.SMILE; } if (length > 2 && first == '-' && content.charAt(1) == '-' && content.charAt(2) == '-') { @@ -290,9 +291,9 @@ public static XContentType xContentType(byte[] bytes, int offset, int length) { return XContentType.JSON; } if (length > 2 - && first == SmileConstants.HEADER_BYTE_1 - && bytes[offset + 1] == SmileConstants.HEADER_BYTE_2 - && bytes[offset + 2] == SmileConstants.HEADER_BYTE_3) { + && first == SmileConstants.HEADER_BYTE_1 + && bytes[offset + 1] == SmileConstants.HEADER_BYTE_2 + && bytes[offset + 2] == SmileConstants.HEADER_BYTE_3) { return XContentType.SMILE; } if (length > 2 && first == '-' && bytes[offset + 1] == '-' && bytes[offset + 2] == '-') { diff --git a/libs/x-content/src/main/java/org/elasticsearch/xcontent/XContentParser.java b/libs/x-content/src/main/java/org/elasticsearch/xcontent/XContentParser.java index f9f2677fe8b2c..2a079d0bf8966 100644 --- a/libs/x-content/src/main/java/org/elasticsearch/xcontent/XContentParser.java +++ b/libs/x-content/src/main/java/org/elasticsearch/xcontent/XContentParser.java @@ -107,7 +107,12 @@ public boolean isValue() { } enum NumberType { - INT, BIG_INTEGER, LONG, FLOAT, DOUBLE, BIG_DECIMAL + INT, + BIG_INTEGER, + LONG, + FLOAT, + DOUBLE, + BIG_DECIMAL } XContentType contentType(); @@ -137,8 +142,8 @@ enum NumberType { * @param map value type * @return {@link Map} object */ - Map map( - Supplier> mapFactory, CheckedFunction mapValueParser) throws IOException; + Map map(Supplier> mapFactory, CheckedFunction mapValueParser) + throws IOException; List list() throws IOException; diff --git a/libs/x-content/src/main/java/org/elasticsearch/xcontent/XContentSubParser.java b/libs/x-content/src/main/java/org/elasticsearch/xcontent/XContentSubParser.java index cc455aae8ca8b..23285167cc750 100644 --- a/libs/x-content/src/main/java/org/elasticsearch/xcontent/XContentSubParser.java +++ b/libs/x-content/src/main/java/org/elasticsearch/xcontent/XContentSubParser.java @@ -104,8 +104,8 @@ public Map mapStrings() throws IOException { } @Override - public Map map( - Supplier> mapFactory, CheckedFunction mapValueParser) throws IOException { + public Map map(Supplier> mapFactory, CheckedFunction mapValueParser) + throws IOException { return parser.map(mapFactory, mapValueParser); } diff --git a/libs/x-content/src/main/java/org/elasticsearch/xcontent/XContentType.java b/libs/x-content/src/main/java/org/elasticsearch/xcontent/XContentType.java index 9847ba660277e..56fff226114f8 100644 --- a/libs/x-content/src/main/java/org/elasticsearch/xcontent/XContentType.java +++ b/libs/x-content/src/main/java/org/elasticsearch/xcontent/XContentType.java @@ -47,10 +47,7 @@ public XContent xContent() { @Override public Set headerValues() { - return Set.of( - new HeaderValue("application/json"), - new HeaderValue("application/x-ndjson"), - new HeaderValue("application/*")); + return Set.of(new HeaderValue("application/json"), new HeaderValue("application/x-ndjson"), new HeaderValue("application/*")); } }, /** @@ -74,8 +71,7 @@ public XContent xContent() { @Override public Set headerValues() { - return Set.of( - new HeaderValue("application/smile")); + return Set.of(new HeaderValue("application/smile")); } }, /** @@ -99,8 +95,7 @@ public XContent xContent() { @Override public Set headerValues() { - return Set.of( - new HeaderValue("application/yaml")); + return Set.of(new HeaderValue("application/yaml")); } }, /** @@ -124,8 +119,7 @@ public XContent xContent() { @Override public Set headerValues() { - return Set.of( - new HeaderValue("application/cbor")); + return Set.of(new HeaderValue("application/cbor")); } }, /** @@ -150,10 +144,9 @@ public XContent xContent() { @Override public Set headerValues() { return Set.of( - new HeaderValue(VENDOR_APPLICATION_PREFIX + "json", - Map.of(COMPATIBLE_WITH_PARAMETER_NAME, VERSION_PATTERN)), - new HeaderValue(VENDOR_APPLICATION_PREFIX + "x-ndjson", - Map.of(COMPATIBLE_WITH_PARAMETER_NAME, VERSION_PATTERN))); + new HeaderValue(VENDOR_APPLICATION_PREFIX + "json", Map.of(COMPATIBLE_WITH_PARAMETER_NAME, VERSION_PATTERN)), + new HeaderValue(VENDOR_APPLICATION_PREFIX + "x-ndjson", Map.of(COMPATIBLE_WITH_PARAMETER_NAME, VERSION_PATTERN)) + ); } @Override @@ -182,9 +175,7 @@ public XContent xContent() { @Override public Set headerValues() { - return Set.of( - new HeaderValue(VENDOR_APPLICATION_PREFIX + "smile", - Map.of(COMPATIBLE_WITH_PARAMETER_NAME, VERSION_PATTERN))); + return Set.of(new HeaderValue(VENDOR_APPLICATION_PREFIX + "smile", Map.of(COMPATIBLE_WITH_PARAMETER_NAME, VERSION_PATTERN))); } @Override @@ -213,9 +204,7 @@ public XContent xContent() { @Override public Set headerValues() { - return Set.of( - new HeaderValue(VENDOR_APPLICATION_PREFIX + "yaml", - Map.of(COMPATIBLE_WITH_PARAMETER_NAME, VERSION_PATTERN))); + return Set.of(new HeaderValue(VENDOR_APPLICATION_PREFIX + "yaml", Map.of(COMPATIBLE_WITH_PARAMETER_NAME, VERSION_PATTERN))); } @Override @@ -244,9 +233,7 @@ public XContent xContent() { @Override public Set headerValues() { - return Set.of( - new HeaderValue(VENDOR_APPLICATION_PREFIX + "cbor", - Map.of(COMPATIBLE_WITH_PARAMETER_NAME, VERSION_PATTERN))); + return Set.of(new HeaderValue(VENDOR_APPLICATION_PREFIX + "cbor", Map.of(COMPATIBLE_WITH_PARAMETER_NAME, VERSION_PATTERN))); } @Override @@ -255,11 +242,13 @@ public XContentType canonical() { } }; - public static final MediaTypeRegistry MEDIA_TYPE_REGISTRY = new MediaTypeRegistry() - .register(XContentType.values()); + public static final MediaTypeRegistry MEDIA_TYPE_REGISTRY = new MediaTypeRegistry().register( + XContentType.values() + ); public static final String VENDOR_APPLICATION_PREFIX = "application/vnd.elasticsearch+"; private final ParsedMediaType mediaType = ParsedMediaType.parseMediaType(mediaTypeWithoutParameters()); + /** * Accepts a format string, which is most of the time is equivalent to MediaType's subtype i.e. application/json * and attempts to match the value to an {@link XContentType}. @@ -279,8 +268,7 @@ public static XContentType fromFormat(String format) { public static XContentType fromMediaType(String mediaTypeHeaderValue) throws IllegalArgumentException { ParsedMediaType parsedMediaType = ParsedMediaType.parseMediaType(mediaTypeHeaderValue); if (parsedMediaType != null) { - return parsedMediaType - .toMediaType(MEDIA_TYPE_REGISTRY); + return parsedMediaType.toMediaType(MEDIA_TYPE_REGISTRY); } return null; } @@ -294,9 +282,7 @@ public static XContentType fromMediaType(String mediaTypeHeaderValue) throws Ill public static Byte parseVersion(String mediaType) { ParsedMediaType parsedMediaType = ParsedMediaType.parseMediaType(mediaType); if (parsedMediaType != null) { - String version = parsedMediaType - .getParameters() - .get(COMPATIBLE_WITH_PARAMETER_NAME); + String version = parsedMediaType.getParameters().get(COMPATIBLE_WITH_PARAMETER_NAME); return version != null ? Byte.parseByte(version) : null; } return null; @@ -326,7 +312,7 @@ public ParsedMediaType toParsedMediaType() { * Example: XContentType.VND_JSON has a canonical XContentType.JSON * XContentType.JSON has a canonical XContentType.JSON */ - public XContentType canonical(){ + public XContentType canonical() { return this; } } diff --git a/libs/x-content/src/main/java/org/elasticsearch/xcontent/XContentUtils.java b/libs/x-content/src/main/java/org/elasticsearch/xcontent/XContentUtils.java index 23ed3bd03785c..5d6fecea6c1a0 100644 --- a/libs/x-content/src/main/java/org/elasticsearch/xcontent/XContentUtils.java +++ b/libs/x-content/src/main/java/org/elasticsearch/xcontent/XContentUtils.java @@ -12,8 +12,7 @@ public final class XContentUtils { - private XContentUtils() { - } + private XContentUtils() {} /** * Convert a {@link XContentParser.Token} to a value diff --git a/libs/x-content/src/main/java/org/elasticsearch/xcontent/cbor/CborXContent.java b/libs/x-content/src/main/java/org/elasticsearch/xcontent/cbor/CborXContent.java index d43e3b10b225c..b0443b9b20008 100644 --- a/libs/x-content/src/main/java/org/elasticsearch/xcontent/cbor/CborXContent.java +++ b/libs/x-content/src/main/java/org/elasticsearch/xcontent/cbor/CborXContent.java @@ -13,6 +13,7 @@ import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.dataformat.cbor.CBORFactory; +import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContent; @@ -22,7 +23,6 @@ import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.support.filtering.FilterPath; -import org.elasticsearch.core.RestApiVersion; import java.io.ByteArrayInputStream; import java.io.IOException; @@ -52,8 +52,7 @@ public static XContentBuilder contentBuilder() throws IOException { cborXContent = new CborXContent(); } - private CborXContent() { - } + private CborXContent() {} @Override public XContentType type() { @@ -71,14 +70,14 @@ public XContentGenerator createGenerator(OutputStream os, Set includes, } @Override - public XContentParser createParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, String content) throws IOException { + public XContentParser createParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, String content) + throws IOException { return new CborXContentParser(xContentRegistry, deprecationHandler, cborFactory.createParser(content)); } @Override - public XContentParser createParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, InputStream is) throws IOException { + public XContentParser createParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, InputStream is) + throws IOException { return new CborXContentParser(xContentRegistry, deprecationHandler, cborFactory.createParser(is)); } @@ -101,14 +100,19 @@ public XContentParser createParser( } @Override - public XContentParser createParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, byte[] data) throws IOException { + public XContentParser createParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, byte[] data) + throws IOException { return createParser(xContentRegistry, deprecationHandler, data, 0, data.length); } @Override - public XContentParser createParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, byte[] data, int offset, int length) throws IOException { + public XContentParser createParser( + NamedXContentRegistry xContentRegistry, + DeprecationHandler deprecationHandler, + byte[] data, + int offset, + int length + ) throws IOException { return createParserForCompatibility(xContentRegistry, deprecationHandler, data, offset, length, RestApiVersion.current()); } @@ -133,23 +137,30 @@ public XContentParser createParser( } @Override - public XContentParser createParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, Reader reader) throws IOException { + public XContentParser createParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, Reader reader) + throws IOException { return new CborXContentParser(xContentRegistry, deprecationHandler, cborFactory.createParser(reader)); } @Override - public XContentParser createParserForCompatibility(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, InputStream is, - RestApiVersion restApiVersion) - throws IOException { + public XContentParser createParserForCompatibility( + NamedXContentRegistry xContentRegistry, + DeprecationHandler deprecationHandler, + InputStream is, + RestApiVersion restApiVersion + ) throws IOException { return new CborXContentParser(xContentRegistry, deprecationHandler, cborFactory.createParser(is), restApiVersion); } @Override - public XContentParser createParserForCompatibility(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, - byte[] data, int offset, int length, RestApiVersion restApiVersion) - throws IOException { + public XContentParser createParserForCompatibility( + NamedXContentRegistry xContentRegistry, + DeprecationHandler deprecationHandler, + byte[] data, + int offset, + int length, + RestApiVersion restApiVersion + ) throws IOException { return new CborXContentParser( xContentRegistry, deprecationHandler, diff --git a/libs/x-content/src/main/java/org/elasticsearch/xcontent/cbor/CborXContentGenerator.java b/libs/x-content/src/main/java/org/elasticsearch/xcontent/cbor/CborXContentGenerator.java index d9e262e568f36..592b6b88f4205 100644 --- a/libs/x-content/src/main/java/org/elasticsearch/xcontent/cbor/CborXContentGenerator.java +++ b/libs/x-content/src/main/java/org/elasticsearch/xcontent/cbor/CborXContentGenerator.java @@ -9,6 +9,7 @@ package org.elasticsearch.xcontent.cbor; import com.fasterxml.jackson.core.JsonGenerator; + import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.json.JsonXContentGenerator; diff --git a/libs/x-content/src/main/java/org/elasticsearch/xcontent/cbor/CborXContentParser.java b/libs/x-content/src/main/java/org/elasticsearch/xcontent/cbor/CborXContentParser.java index 75e36a60d0d32..54a37935c8a8d 100644 --- a/libs/x-content/src/main/java/org/elasticsearch/xcontent/cbor/CborXContentParser.java +++ b/libs/x-content/src/main/java/org/elasticsearch/xcontent/cbor/CborXContentParser.java @@ -9,6 +9,7 @@ package org.elasticsearch.xcontent.cbor; import com.fasterxml.jackson.core.JsonParser; + import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; @@ -18,14 +19,16 @@ public class CborXContentParser extends JsonXContentParser { - public CborXContentParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, JsonParser parser) { + public CborXContentParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, JsonParser parser) { super(xContentRegistry, deprecationHandler, parser); } - public CborXContentParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, JsonParser parser, - RestApiVersion restApiVersion) { + public CborXContentParser( + NamedXContentRegistry xContentRegistry, + DeprecationHandler deprecationHandler, + JsonParser parser, + RestApiVersion restApiVersion + ) { super(xContentRegistry, deprecationHandler, parser, restApiVersion); } diff --git a/libs/x-content/src/main/java/org/elasticsearch/xcontent/json/JsonXContent.java b/libs/x-content/src/main/java/org/elasticsearch/xcontent/json/JsonXContent.java index 10df2c1c10d8d..126abc0dd1b79 100644 --- a/libs/x-content/src/main/java/org/elasticsearch/xcontent/json/JsonXContent.java +++ b/libs/x-content/src/main/java/org/elasticsearch/xcontent/json/JsonXContent.java @@ -13,6 +13,7 @@ import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonParser; +import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContent; @@ -21,7 +22,6 @@ import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.support.filtering.FilterPath; -import org.elasticsearch.core.RestApiVersion; import java.io.ByteArrayInputStream; import java.io.IOException; @@ -38,6 +38,7 @@ public class JsonXContent implements XContent { public static XContentBuilder contentBuilder() throws IOException { return XContentBuilder.builder(jsonXContent); } + private static final JsonFactory jsonFactory; public static final JsonXContent jsonXContent; @@ -53,8 +54,7 @@ public static XContentBuilder contentBuilder() throws IOException { jsonXContent = new JsonXContent(); } - private JsonXContent() { - } + private JsonXContent() {} @Override public XContentType type() { @@ -72,14 +72,14 @@ public XContentGenerator createGenerator(OutputStream os, Set includes, } @Override - public XContentParser createParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, String content) throws IOException { + public XContentParser createParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, String content) + throws IOException { return new JsonXContentParser(xContentRegistry, deprecationHandler, jsonFactory.createParser(content)); } @Override - public XContentParser createParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, InputStream is) throws IOException { + public XContentParser createParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, InputStream is) + throws IOException { return new JsonXContentParser(xContentRegistry, deprecationHandler, jsonFactory.createParser(is)); } @@ -102,14 +102,19 @@ public XContentParser createParser( } @Override - public XContentParser createParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, byte[] data) throws IOException { + public XContentParser createParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, byte[] data) + throws IOException { return createParser(xContentRegistry, deprecationHandler, data, 0, data.length); } @Override - public XContentParser createParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, byte[] data, int offset, int length) throws IOException { + public XContentParser createParser( + NamedXContentRegistry xContentRegistry, + DeprecationHandler deprecationHandler, + byte[] data, + int offset, + int length + ) throws IOException { return createParserForCompatibility(xContentRegistry, deprecationHandler, data, offset, length, RestApiVersion.current()); } @@ -134,22 +139,30 @@ public XContentParser createParser( } @Override - public XContentParser createParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, Reader reader) throws IOException { + public XContentParser createParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, Reader reader) + throws IOException { return new JsonXContentParser(xContentRegistry, deprecationHandler, jsonFactory.createParser(reader)); } @Override - public XContentParser createParserForCompatibility(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, InputStream is, - RestApiVersion restApiVersion) throws IOException { + public XContentParser createParserForCompatibility( + NamedXContentRegistry xContentRegistry, + DeprecationHandler deprecationHandler, + InputStream is, + RestApiVersion restApiVersion + ) throws IOException { return new JsonXContentParser(xContentRegistry, deprecationHandler, jsonFactory.createParser(is), restApiVersion); } @Override - public XContentParser createParserForCompatibility(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, - byte[] data, int offset, int length, RestApiVersion restApiVersion) - throws IOException { + public XContentParser createParserForCompatibility( + NamedXContentRegistry xContentRegistry, + DeprecationHandler deprecationHandler, + byte[] data, + int offset, + int length, + RestApiVersion restApiVersion + ) throws IOException { return new JsonXContentParser( xContentRegistry, deprecationHandler, diff --git a/libs/x-content/src/main/java/org/elasticsearch/xcontent/json/JsonXContentGenerator.java b/libs/x-content/src/main/java/org/elasticsearch/xcontent/json/JsonXContentGenerator.java index d39d68438f6d1..ca038f9466405 100644 --- a/libs/x-content/src/main/java/org/elasticsearch/xcontent/json/JsonXContentGenerator.java +++ b/libs/x-content/src/main/java/org/elasticsearch/xcontent/json/JsonXContentGenerator.java @@ -17,7 +17,9 @@ import com.fasterxml.jackson.core.util.DefaultIndenter; import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; import com.fasterxml.jackson.core.util.JsonGeneratorDelegate; + import org.elasticsearch.core.CheckedConsumer; +import org.elasticsearch.core.internal.io.Streams; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContent; @@ -26,7 +28,6 @@ import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.support.filtering.FilterPathBasedFilter; -import org.elasticsearch.core.internal.io.Streams; import java.io.BufferedInputStream; import java.io.IOException; @@ -157,7 +158,6 @@ public void writeEndObject() throws IOException { generator.writeEndObject(); } - @Override public void writeStartArray() throws IOException { generator.writeStartArray(); @@ -327,11 +327,13 @@ public void writeRawField(String name, InputStream content) throws IOException { public void writeRawField(String name, InputStream content, XContentType contentType) throws IOException { if (mayWriteRawData(contentType) == false) { // EMPTY is safe here because we never call namedObject when writing raw data - try (XContentParser parser = XContentFactory.xContent(contentType) + try ( + XContentParser parser = XContentFactory.xContent(contentType) // It's okay to pass the throwing deprecation handler // because we should not be writing raw fields when // generating JSON - .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, content)) { + .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, content) + ) { parser.nextToken(); writeFieldName(name); copyCurrentStructure(parser); @@ -364,10 +366,7 @@ private boolean mayWriteRawData(XContentType contentType) { // or the content is in a different format than the current generator, // we need to copy the whole structure so that it will be correctly // filtered or converted - return supportsRawWrites() - && isFiltered() == false - && contentType == contentType() - && prettyPrint == false; + return supportsRawWrites() && isFiltered() == false && contentType == contentType() && prettyPrint == false; } /** Whether this generator supports writing raw data directly */ @@ -377,10 +376,12 @@ protected boolean supportsRawWrites() { protected void copyRawValue(InputStream stream, XContent xContent) throws IOException { // EMPTY is safe here because we never call namedObject - try (XContentParser parser = xContent - // It's okay to pass the throwing deprecation handler because we - // should not be writing raw fields when generating JSON - .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, stream)) { + try ( + XContentParser parser = xContent + // It's okay to pass the throwing deprecation handler because we + // should not be writing raw fields when generating JSON + .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, stream) + ) { copyCurrentStructure(parser); } } @@ -451,7 +452,7 @@ public void close() throws IOException { return; } JsonStreamContext context = generator.getOutputContext(); - if ((context != null) && (context.inRoot() == false)) { + if ((context != null) && (context.inRoot() == false)) { throw new IOException("Unclosed object or array found"); } if (writeLineFeedAtEnd) { diff --git a/libs/x-content/src/main/java/org/elasticsearch/xcontent/json/JsonXContentParser.java b/libs/x-content/src/main/java/org/elasticsearch/xcontent/json/JsonXContentParser.java index c210233af2661..13e2aeff45f7c 100644 --- a/libs/x-content/src/main/java/org/elasticsearch/xcontent/json/JsonXContentParser.java +++ b/libs/x-content/src/main/java/org/elasticsearch/xcontent/json/JsonXContentParser.java @@ -13,6 +13,8 @@ import com.fasterxml.jackson.core.JsonToken; import com.fasterxml.jackson.core.filter.FilteringParserDelegate; +import org.elasticsearch.core.RestApiVersion; +import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentLocation; @@ -20,8 +22,6 @@ import org.elasticsearch.xcontent.support.AbstractXContentParser; import org.elasticsearch.xcontent.support.filtering.FilterPath; import org.elasticsearch.xcontent.support.filtering.FilterPathBasedFilter; -import org.elasticsearch.core.RestApiVersion; -import org.elasticsearch.core.internal.io.IOUtils; import java.io.IOException; import java.nio.CharBuffer; @@ -30,15 +30,17 @@ public class JsonXContentParser extends AbstractXContentParser { final JsonParser parser; - public JsonXContentParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, JsonParser parser) { + public JsonXContentParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, JsonParser parser) { super(xContentRegistry, deprecationHandler, RestApiVersion.current()); this.parser = parser; } - public JsonXContentParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, JsonParser parser, - RestApiVersion restApiVersion) { + public JsonXContentParser( + NamedXContentRegistry xContentRegistry, + DeprecationHandler deprecationHandler, + JsonParser parser, + RestApiVersion restApiVersion + ) { super(xContentRegistry, deprecationHandler, restApiVersion); this.parser = parser; } diff --git a/libs/x-content/src/main/java/org/elasticsearch/xcontent/smile/SmileXContent.java b/libs/x-content/src/main/java/org/elasticsearch/xcontent/smile/SmileXContent.java index 696865a242830..c88e63df50c08 100644 --- a/libs/x-content/src/main/java/org/elasticsearch/xcontent/smile/SmileXContent.java +++ b/libs/x-content/src/main/java/org/elasticsearch/xcontent/smile/SmileXContent.java @@ -14,6 +14,7 @@ import com.fasterxml.jackson.dataformat.smile.SmileFactory; import com.fasterxml.jackson.dataformat.smile.SmileGenerator; +import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContent; @@ -22,7 +23,6 @@ import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.support.filtering.FilterPath; -import org.elasticsearch.core.RestApiVersion; import java.io.ByteArrayInputStream; import java.io.IOException; @@ -54,8 +54,7 @@ public static XContentBuilder contentBuilder() throws IOException { smileXContent = new SmileXContent(); } - private SmileXContent() { - } + private SmileXContent() {} @Override public XContentType type() { @@ -73,14 +72,14 @@ public XContentGenerator createGenerator(OutputStream os, Set includes, } @Override - public XContentParser createParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, String content) throws IOException { + public XContentParser createParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, String content) + throws IOException { return new SmileXContentParser(xContentRegistry, deprecationHandler, smileFactory.createParser(content)); } @Override - public XContentParser createParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, InputStream is) throws IOException { + public XContentParser createParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, InputStream is) + throws IOException { return new SmileXContentParser(xContentRegistry, deprecationHandler, smileFactory.createParser(is)); } @@ -103,14 +102,19 @@ public XContentParser createParser( } @Override - public XContentParser createParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, byte[] data) throws IOException { + public XContentParser createParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, byte[] data) + throws IOException { return createParser(xContentRegistry, deprecationHandler, data, 0, data.length); } @Override - public XContentParser createParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, byte[] data, int offset, int length) throws IOException { + public XContentParser createParser( + NamedXContentRegistry xContentRegistry, + DeprecationHandler deprecationHandler, + byte[] data, + int offset, + int length + ) throws IOException { return createParserForCompatibility(xContentRegistry, deprecationHandler, data, offset, length, RestApiVersion.current()); } @@ -135,22 +139,30 @@ public XContentParser createParser( } @Override - public XContentParser createParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, Reader reader) throws IOException { + public XContentParser createParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, Reader reader) + throws IOException { return new SmileXContentParser(xContentRegistry, deprecationHandler, smileFactory.createParser(reader)); } @Override - public XContentParser createParserForCompatibility(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, InputStream is, - RestApiVersion restApiVersion) throws IOException { + public XContentParser createParserForCompatibility( + NamedXContentRegistry xContentRegistry, + DeprecationHandler deprecationHandler, + InputStream is, + RestApiVersion restApiVersion + ) throws IOException { return new SmileXContentParser(xContentRegistry, deprecationHandler, smileFactory.createParser(is), restApiVersion); } @Override - public XContentParser createParserForCompatibility(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, - byte[] data, int offset, int length, RestApiVersion restApiVersion) - throws IOException { + public XContentParser createParserForCompatibility( + NamedXContentRegistry xContentRegistry, + DeprecationHandler deprecationHandler, + byte[] data, + int offset, + int length, + RestApiVersion restApiVersion + ) throws IOException { return new SmileXContentParser( xContentRegistry, deprecationHandler, diff --git a/libs/x-content/src/main/java/org/elasticsearch/xcontent/smile/SmileXContentGenerator.java b/libs/x-content/src/main/java/org/elasticsearch/xcontent/smile/SmileXContentGenerator.java index ce57578673ecf..b083fdee2e5e0 100644 --- a/libs/x-content/src/main/java/org/elasticsearch/xcontent/smile/SmileXContentGenerator.java +++ b/libs/x-content/src/main/java/org/elasticsearch/xcontent/smile/SmileXContentGenerator.java @@ -9,6 +9,7 @@ package org.elasticsearch.xcontent.smile; import com.fasterxml.jackson.core.JsonGenerator; + import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.json.JsonXContentGenerator; diff --git a/libs/x-content/src/main/java/org/elasticsearch/xcontent/smile/SmileXContentParser.java b/libs/x-content/src/main/java/org/elasticsearch/xcontent/smile/SmileXContentParser.java index d5db33a576bd8..d7c15e182cdd8 100644 --- a/libs/x-content/src/main/java/org/elasticsearch/xcontent/smile/SmileXContentParser.java +++ b/libs/x-content/src/main/java/org/elasticsearch/xcontent/smile/SmileXContentParser.java @@ -9,6 +9,7 @@ package org.elasticsearch.xcontent.smile; import com.fasterxml.jackson.core.JsonParser; + import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; @@ -18,14 +19,16 @@ public class SmileXContentParser extends JsonXContentParser { - public SmileXContentParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, JsonParser parser) { + public SmileXContentParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, JsonParser parser) { super(xContentRegistry, deprecationHandler, parser); } - public SmileXContentParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, JsonParser parser, - RestApiVersion restApiVersion) { + public SmileXContentParser( + NamedXContentRegistry xContentRegistry, + DeprecationHandler deprecationHandler, + JsonParser parser, + RestApiVersion restApiVersion + ) { super(xContentRegistry, deprecationHandler, parser, restApiVersion); } diff --git a/libs/x-content/src/main/java/org/elasticsearch/xcontent/support/AbstractXContentParser.java b/libs/x-content/src/main/java/org/elasticsearch/xcontent/support/AbstractXContentParser.java index b1c599f43037b..27fc3d16ef788 100644 --- a/libs/x-content/src/main/java/org/elasticsearch/xcontent/support/AbstractXContentParser.java +++ b/libs/x-content/src/main/java/org/elasticsearch/xcontent/support/AbstractXContentParser.java @@ -39,8 +39,8 @@ public abstract class AbstractXContentParser implements XContentParser { private static void checkCoerceString(boolean coerce, Class clazz) { if (coerce == false) { - //Need to throw type IllegalArgumentException as current catch logic in - //NumberFieldMapper.parseCreateField relies on this for "malformed" value detection + // Need to throw type IllegalArgumentException as current catch logic in + // NumberFieldMapper.parseCreateField relies on this for "malformed" value detection throw new IllegalArgumentException(clazz.getSimpleName() + " value passed as String"); } } @@ -49,8 +49,11 @@ private static void checkCoerceString(boolean coerce, Class cl private final DeprecationHandler deprecationHandler; private final RestApiVersion restApiVersion; - public AbstractXContentParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, - RestApiVersion restApiVersion) { + public AbstractXContentParser( + NamedXContentRegistry xContentRegistry, + DeprecationHandler deprecationHandler, + RestApiVersion restApiVersion + ) { this.xContentRegistry = xContentRegistry; this.deprecationHandler = deprecationHandler; this.restApiVersion = restApiVersion; @@ -61,7 +64,7 @@ public AbstractXContentParser(NamedXContentRegistry xContentRegistry, Deprecatio } // The 3rd party parsers we rely on are known to silently truncate fractions: see - // http://fasterxml.github.io/jackson-core/javadoc/2.3.0/com/fasterxml/jackson/core/JsonParser.html#getShortValue() + // http://fasterxml.github.io/jackson-core/javadoc/2.3.0/com/fasterxml/jackson/core/JsonParser.html#getShortValue() // If this behaviour is flagged as undesirable and any truncation occurs // then this method is called to trigger the"malformed" handling logic void ensureNumberConversion(boolean coerce, long result, Class clazz) throws IOException { @@ -169,8 +172,8 @@ private static long toLong(String stringValue, boolean coerce) { final BigInteger bigIntegerValue; try { final BigDecimal bigDecimalValue = new BigDecimal(stringValue); - if (bigDecimalValue.compareTo(BIGDECIMAL_GREATER_THAN_LONG_MAX_VALUE) >= 0 || - bigDecimalValue.compareTo(BIGDECIMAL_LESS_THAN_LONG_MIN_VALUE) <= 0) { + if (bigDecimalValue.compareTo(BIGDECIMAL_GREATER_THAN_LONG_MAX_VALUE) >= 0 + || bigDecimalValue.compareTo(BIGDECIMAL_LESS_THAN_LONG_MIN_VALUE) <= 0) { throw new IllegalArgumentException("Value [" + stringValue + "] is out of range for a long"); } bigIntegerValue = coerce ? bigDecimalValue.toBigInteger() : bigDecimalValue.toBigIntegerExact(); @@ -224,7 +227,6 @@ public float floatValue(boolean coerce) throws IOException { protected abstract float doFloatValue() throws IOException; - @Override public double doubleValue() throws IOException { return doubleValue(DEFAULT_NUMBER_COERCE_POLICY); @@ -274,8 +276,8 @@ public Map mapStrings() throws IOException { } @Override - public Map map( - Supplier> mapFactory, CheckedFunction mapValueParser) throws IOException { + public Map map(Supplier> mapFactory, CheckedFunction mapValueParser) + throws IOException { final Map map = mapFactory.get(); if (findNonEmptyMapStart(this) == false) { return map; @@ -314,8 +316,11 @@ private static Map readMapSafe(XContentParser parser, Supplier readMapEntries(XContentParser parser, Supplier> mapFactory, - Map map) throws IOException { + private static Map readMapEntries( + XContentParser parser, + Supplier> mapFactory, + Map map + ) throws IOException { assert parser.currentToken() == Token.FIELD_NAME : "Expected field name but saw [" + parser.currentToken() + "]"; do { // Must point to field name @@ -356,8 +361,10 @@ private static void skipToListStart(XContentParser parser) throws IOException { token = parser.nextToken(); } if (token != XContentParser.Token.START_ARRAY) { - throw new XContentParseException(parser.getTokenLocation(), "Failed to parse list: expecting " - + XContentParser.Token.START_ARRAY + " but got " + token); + throw new XContentParseException( + parser.getTokenLocation(), + "Failed to parse list: expecting " + XContentParser.Token.START_ARRAY + " but got " + token + ); } } @@ -382,22 +389,28 @@ public static Object readValue(XContentParser parser, Supplier> mapFactory) throws IOException { - assert currentToken == parser.currentToken() : "Supplied current token [" + currentToken + - "] is different from actual parser current token [" + parser.currentToken() + "]"; + private static Object readValueUnsafe(Token currentToken, XContentParser parser, Supplier> mapFactory) + throws IOException { + assert currentToken == parser.currentToken() + : "Supplied current token [" + currentToken + "] is different from actual parser current token [" + parser.currentToken() + "]"; switch (currentToken) { - case VALUE_STRING: return parser.text(); - case VALUE_NUMBER: return parser.numberValue(); - case VALUE_BOOLEAN: return parser.booleanValue(); + case VALUE_STRING: + return parser.text(); + case VALUE_NUMBER: + return parser.numberValue(); + case VALUE_BOOLEAN: + return parser.booleanValue(); case START_OBJECT: { final Map map = mapFactory.get(); return parser.nextToken() != Token.FIELD_NAME ? map : readMapEntries(parser, mapFactory, map); } - case START_ARRAY: return readListUnsafe(parser, mapFactory); - case VALUE_EMBEDDED_OBJECT: return parser.binaryValue(); + case START_ARRAY: + return readListUnsafe(parser, mapFactory); + case VALUE_EMBEDDED_OBJECT: + return parser.binaryValue(); case VALUE_NULL: - default: return null; + default: + return null; } } diff --git a/libs/x-content/src/main/java/org/elasticsearch/xcontent/support/MapXContentParser.java b/libs/x-content/src/main/java/org/elasticsearch/xcontent/support/MapXContentParser.java index ab7aed4fcdbc1..97b947e761d23 100644 --- a/libs/x-content/src/main/java/org/elasticsearch/xcontent/support/MapXContentParser.java +++ b/libs/x-content/src/main/java/org/elasticsearch/xcontent/support/MapXContentParser.java @@ -36,21 +36,26 @@ public static XContentParser wrapObject(Object sourceMap) throws IOException { XContentParser parser = new MapXContentParser( NamedXContentRegistry.EMPTY, DeprecationHandler.IGNORE_DEPRECATIONS, - Collections.singletonMap("dummy_field", sourceMap), XContentType.JSON); + Collections.singletonMap("dummy_field", sourceMap), + XContentType.JSON + ); parser.nextToken(); // start object parser.nextToken(); // field name parser.nextToken(); // field value return parser; } - public MapXContentParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, Map map, - XContentType xContentType) { + public MapXContentParser( + NamedXContentRegistry xContentRegistry, + DeprecationHandler deprecationHandler, + Map map, + XContentType xContentType + ) { super(xContentRegistry, deprecationHandler); this.xContentType = xContentType; this.iterator = new MapIterator(null, null, map); } - @Override protected boolean doBooleanValue() throws IOException { if (iterator != null && iterator.currentValue() instanceof Boolean) { diff --git a/libs/x-content/src/main/java/org/elasticsearch/xcontent/support/filtering/FilterPath.java b/libs/x-content/src/main/java/org/elasticsearch/xcontent/support/filtering/FilterPath.java index 393bd8b69c77f..cddb5f4890b65 100644 --- a/libs/x-content/src/main/java/org/elasticsearch/xcontent/support/filtering/FilterPath.java +++ b/libs/x-content/src/main/java/org/elasticsearch/xcontent/support/filtering/FilterPath.java @@ -6,7 +6,6 @@ * Side Public License, v 1. */ - package org.elasticsearch.xcontent.support.filtering; import org.elasticsearch.core.Glob; @@ -91,7 +90,7 @@ public static FilterPath[] compile(Set filters) { private static FilterPath parse(final String filter, final String segment) { int end = segment.length(); - for (int i = 0; i < end; ) { + for (int i = 0; i < end;) { char c = segment.charAt(i); if (c == '.') { diff --git a/libs/x-content/src/main/java/org/elasticsearch/xcontent/support/filtering/FilterPathBasedFilter.java b/libs/x-content/src/main/java/org/elasticsearch/xcontent/support/filtering/FilterPathBasedFilter.java index 30a09f959a236..451d897ac870a 100644 --- a/libs/x-content/src/main/java/org/elasticsearch/xcontent/support/filtering/FilterPathBasedFilter.java +++ b/libs/x-content/src/main/java/org/elasticsearch/xcontent/support/filtering/FilterPathBasedFilter.java @@ -85,7 +85,6 @@ private TokenFilter evaluate(String name, FilterPath[] filterPaths) { return NO_MATCHING; } - @Override public TokenFilter includeProperty(String name) { TokenFilter filter = evaluate(name, filters); diff --git a/libs/x-content/src/main/java/org/elasticsearch/xcontent/yaml/YamlXContent.java b/libs/x-content/src/main/java/org/elasticsearch/xcontent/yaml/YamlXContent.java index 68f1ac2bbf27b..2f9e5bc941846 100644 --- a/libs/x-content/src/main/java/org/elasticsearch/xcontent/yaml/YamlXContent.java +++ b/libs/x-content/src/main/java/org/elasticsearch/xcontent/yaml/YamlXContent.java @@ -11,6 +11,7 @@ import com.fasterxml.jackson.core.JsonEncoding; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; + import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; @@ -46,8 +47,7 @@ public static XContentBuilder contentBuilder() throws IOException { yamlXContent = new YamlXContent(); } - private YamlXContent() { - } + private YamlXContent() {} @Override public XContentType type() { @@ -65,14 +65,14 @@ public XContentGenerator createGenerator(OutputStream os, Set includes, } @Override - public XContentParser createParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, String content) throws IOException { + public XContentParser createParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, String content) + throws IOException { return new YamlXContentParser(xContentRegistry, deprecationHandler, yamlFactory.createParser(content)); } @Override - public XContentParser createParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, InputStream is) throws IOException { + public XContentParser createParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, InputStream is) + throws IOException { return new YamlXContentParser(xContentRegistry, deprecationHandler, yamlFactory.createParser(is)); } @@ -95,14 +95,19 @@ public XContentParser createParser( } @Override - public XContentParser createParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, byte[] data) throws IOException { + public XContentParser createParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, byte[] data) + throws IOException { return createParser(xContentRegistry, deprecationHandler, data, 0, data.length); } @Override - public XContentParser createParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, byte[] data, int offset, int length) throws IOException { + public XContentParser createParser( + NamedXContentRegistry xContentRegistry, + DeprecationHandler deprecationHandler, + byte[] data, + int offset, + int length + ) throws IOException { return createParserForCompatibility(xContentRegistry, deprecationHandler, data, offset, length, RestApiVersion.current()); } @@ -127,22 +132,30 @@ public XContentParser createParser( } @Override - public XContentParser createParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, Reader reader) throws IOException { + public XContentParser createParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, Reader reader) + throws IOException { return new YamlXContentParser(xContentRegistry, deprecationHandler, yamlFactory.createParser(reader)); } @Override - public XContentParser createParserForCompatibility(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, InputStream is, - RestApiVersion restApiVersion) throws IOException { + public XContentParser createParserForCompatibility( + NamedXContentRegistry xContentRegistry, + DeprecationHandler deprecationHandler, + InputStream is, + RestApiVersion restApiVersion + ) throws IOException { return new YamlXContentParser(xContentRegistry, deprecationHandler, yamlFactory.createParser(is), restApiVersion); } @Override - public XContentParser createParserForCompatibility(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, byte[] data, int offset, int length, - RestApiVersion restApiVersion) throws IOException { + public XContentParser createParserForCompatibility( + NamedXContentRegistry xContentRegistry, + DeprecationHandler deprecationHandler, + byte[] data, + int offset, + int length, + RestApiVersion restApiVersion + ) throws IOException { return new YamlXContentParser( xContentRegistry, deprecationHandler, @@ -151,5 +164,4 @@ public XContentParser createParserForCompatibility(NamedXContentRegistry xConten ); } - } diff --git a/libs/x-content/src/main/java/org/elasticsearch/xcontent/yaml/YamlXContentGenerator.java b/libs/x-content/src/main/java/org/elasticsearch/xcontent/yaml/YamlXContentGenerator.java index a2318359e9c8e..a53a8b0fced39 100644 --- a/libs/x-content/src/main/java/org/elasticsearch/xcontent/yaml/YamlXContentGenerator.java +++ b/libs/x-content/src/main/java/org/elasticsearch/xcontent/yaml/YamlXContentGenerator.java @@ -9,6 +9,7 @@ package org.elasticsearch.xcontent.yaml; import com.fasterxml.jackson.core.JsonGenerator; + import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.json.JsonXContentGenerator; diff --git a/libs/x-content/src/main/java/org/elasticsearch/xcontent/yaml/YamlXContentParser.java b/libs/x-content/src/main/java/org/elasticsearch/xcontent/yaml/YamlXContentParser.java index b51aa5fd1f50d..5f1e646f2e1e9 100644 --- a/libs/x-content/src/main/java/org/elasticsearch/xcontent/yaml/YamlXContentParser.java +++ b/libs/x-content/src/main/java/org/elasticsearch/xcontent/yaml/YamlXContentParser.java @@ -9,6 +9,7 @@ package org.elasticsearch.xcontent.yaml; import com.fasterxml.jackson.core.JsonParser; + import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; @@ -18,14 +19,16 @@ public class YamlXContentParser extends JsonXContentParser { - public YamlXContentParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, JsonParser parser) { + public YamlXContentParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, JsonParser parser) { super(xContentRegistry, deprecationHandler, parser); } - public YamlXContentParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, JsonParser parser, - RestApiVersion restApiVersion) { + public YamlXContentParser( + NamedXContentRegistry xContentRegistry, + DeprecationHandler deprecationHandler, + JsonParser parser, + RestApiVersion restApiVersion + ) { super(xContentRegistry, deprecationHandler, parser, restApiVersion); } @@ -40,7 +43,6 @@ public YamlXContentParser( super(xContentRegistry, deprecationHandler, parser, restApiVersion, includes, excludes); } - @Override public XContentType contentType() { return XContentType.YAML; diff --git a/libs/x-content/src/test/java/org/elasticsearch/xcontent/ConstructingObjectParserTests.java b/libs/x-content/src/test/java/org/elasticsearch/xcontent/ConstructingObjectParserTests.java index 0025faa30b0dd..0cb6f59597a4c 100644 --- a/libs/x-content/src/test/java/org/elasticsearch/xcontent/ConstructingObjectParserTests.java +++ b/libs/x-content/src/test/java/org/elasticsearch/xcontent/ConstructingObjectParserTests.java @@ -8,13 +8,13 @@ package org.elasticsearch.xcontent; +import org.elasticsearch.common.logging.DeprecationLogger; import org.elasticsearch.core.CheckedFunction; import org.elasticsearch.core.Nullable; import org.elasticsearch.core.RestApiVersion; -import org.elasticsearch.common.logging.DeprecationLogger; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ObjectParserTests.NamedObject; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.ESTestCase; import org.hamcrest.Matcher; import java.io.ByteArrayOutputStream; @@ -35,22 +35,40 @@ public class ConstructingObjectParserTests extends ESTestCase { public void testNullDeclares() { ConstructingObjectParser objectParser = new ConstructingObjectParser<>("foo", a -> null); - Exception e = expectThrows(IllegalArgumentException.class, - () -> objectParser.declareField(null, (r, c) -> null, new ParseField("test"), ObjectParser.ValueType.STRING)); + Exception e = expectThrows( + IllegalArgumentException.class, + () -> objectParser.declareField(null, (r, c) -> null, new ParseField("test"), ObjectParser.ValueType.STRING) + ); assertEquals("[consumer] is required", e.getMessage()); - e = expectThrows(IllegalArgumentException.class, () -> objectParser.declareField( - (o, v) -> {}, (ContextParser) null, - new ParseField("test"), ObjectParser.ValueType.STRING)); + e = expectThrows( + IllegalArgumentException.class, + () -> objectParser.declareField( + (o, v) -> {}, + (ContextParser) null, + new ParseField("test"), + ObjectParser.ValueType.STRING + ) + ); assertEquals("[parser] is required", e.getMessage()); - e = expectThrows(IllegalArgumentException.class, () -> objectParser.declareField( - (o, v) -> {}, (CheckedFunction) null, - new ParseField("test"), ObjectParser.ValueType.STRING)); + e = expectThrows( + IllegalArgumentException.class, + () -> objectParser.declareField( + (o, v) -> {}, + (CheckedFunction) null, + new ParseField("test"), + ObjectParser.ValueType.STRING + ) + ); assertEquals("[parser] is required", e.getMessage()); - e = expectThrows(IllegalArgumentException.class, () -> objectParser.declareField( - (o, v) -> {}, (r, c) -> null, null, ObjectParser.ValueType.STRING)); + e = expectThrows( + IllegalArgumentException.class, + () -> objectParser.declareField((o, v) -> {}, (r, c) -> null, null, ObjectParser.ValueType.STRING) + ); assertEquals("[parseField] is required", e.getMessage()); - e = expectThrows(IllegalArgumentException.class, () -> objectParser.declareField( - (o, v) -> {}, (r, c) -> null, new ParseField("test"), null)); + e = expectThrows( + IllegalArgumentException.class, + () -> objectParser.declareField((o, v) -> {}, (r, c) -> null, new ParseField("test"), null) + ); assertEquals("[type] is required", e.getMessage()); } @@ -85,8 +103,9 @@ public void testRandomOrder() throws Exception { public void testMissingAllConstructorArgs() throws IOException { XContentParser parser = createParser(JsonXContent.jsonXContent, "{ \"mineral\": 1 }"); - ConstructingObjectParser objectParser = randomBoolean() ? HasCtorArguments.PARSER - : HasCtorArguments.PARSER_VEGETABLE_OPTIONAL; + ConstructingObjectParser objectParser = randomBoolean() + ? HasCtorArguments.PARSER + : HasCtorArguments.PARSER_VEGETABLE_OPTIONAL; IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> objectParser.apply(parser, null)); if (objectParser == HasCtorArguments.PARSER) { assertEquals("Required [animal, vegetable]", e.getMessage()); @@ -103,16 +122,17 @@ public void testMissingAllConstructorArgsButNotRequired() throws IOException { public void testMissingSecondConstructorArg() throws IOException { XContentParser parser = createParser(JsonXContent.jsonXContent, "{ \"mineral\": 1, \"animal\": \"cat\" }"); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> HasCtorArguments.PARSER.apply(parser, null)); + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> HasCtorArguments.PARSER.apply(parser, null)); assertEquals("Required [vegetable]", e.getMessage()); } public void testMissingSecondConstructorArgButNotRequired() throws IOException { XContentParser parser = createParser(JsonXContent.jsonXContent, "{ \"mineral\": 1, \"animal\": \"cat\" }"); @SuppressWarnings("unchecked") - HasCtorArguments parsed = randomFrom(HasCtorArguments.PARSER_VEGETABLE_OPTIONAL, HasCtorArguments.PARSER_ALL_OPTIONAL).apply(parser, - null); + HasCtorArguments parsed = randomFrom(HasCtorArguments.PARSER_VEGETABLE_OPTIONAL, HasCtorArguments.PARSER_ALL_OPTIONAL).apply( + parser, + null + ); assertEquals(1, parsed.mineral); assertEquals("cat", parsed.animal); } @@ -120,8 +140,10 @@ public void testMissingSecondConstructorArgButNotRequired() throws IOException { public void testMissingFirstConstructorArg() throws IOException { XContentParser parser = createParser(JsonXContent.jsonXContent, "{ \"mineral\": 1, \"vegetable\": 2 }"); @SuppressWarnings("unchecked") - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> randomFrom(HasCtorArguments.PARSER, HasCtorArguments.PARSER_VEGETABLE_OPTIONAL).apply(parser, null)); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> randomFrom(HasCtorArguments.PARSER, HasCtorArguments.PARSER_VEGETABLE_OPTIONAL).apply(parser, null) + ); assertEquals("Required [animal]", e.getMessage()); } @@ -139,12 +161,16 @@ public void testBadParam() throws IOException { // included in the exception "{\n" + " \"animal\": \"cat\",\n" + " \"vegetable\": 2,\n" + " \"a\": \"supercalifragilisticexpialidocious\"\n" + "}" ); - XContentParseException e = expectThrows(XContentParseException.class, - () -> randomFrom(HasCtorArguments.ALL_PARSERS).apply(parser, null)); + XContentParseException e = expectThrows( + XContentParseException.class, + () -> randomFrom(HasCtorArguments.ALL_PARSERS).apply(parser, null) + ); assertThat(e.getMessage(), containsString("[has_required_arguments] failed to parse field [a]")); assertEquals(4, e.getLineNumber()); - assertEquals("[a] must be less than 10 characters in length but was [supercalifragilisticexpialidocious]", - e.getCause().getMessage()); + assertEquals( + "[a] must be less than 10 characters in length but was [supercalifragilisticexpialidocious]", + e.getCause().getMessage() + ); } public void testBadParamBeforeObjectBuilt() throws IOException { @@ -154,8 +180,10 @@ public void testBadParamBeforeObjectBuilt() throws IOException { // included in the exception "{\n" + " \"a\": \"supercalifragilisticexpialidocious\",\n" + " \"animal\": \"cat\"\n," + " \"vegetable\": 2\n" + "}" ); - XContentParseException e = expectThrows(XContentParseException.class, - () -> randomFrom(HasCtorArguments.ALL_PARSERS).apply(parser, null)); + XContentParseException e = expectThrows( + XContentParseException.class, + () -> randomFrom(HasCtorArguments.ALL_PARSERS).apply(parser, null) + ); assertThat(e.getMessage(), containsString("[has_required_arguments] failed to parse field [vegetable]")); assertEquals(4, e.getLineNumber()); e = (XContentParseException) e.getCause(); @@ -164,21 +192,27 @@ public void testBadParamBeforeObjectBuilt() throws IOException { e = (XContentParseException) e.getCause(); assertThat(e.getMessage(), containsString("[has_required_arguments] failed to parse field [a]")); assertEquals(2, e.getLineNumber()); - assertEquals("[a] must be less than 10 characters in length but was [supercalifragilisticexpialidocious]", - e.getCause().getMessage()); + assertEquals( + "[a] must be less than 10 characters in length but was [supercalifragilisticexpialidocious]", + e.getCause().getMessage() + ); } public void testConstructorArgsMustBeConfigured() throws IOException { - class NoConstructorArgs { - } + class NoConstructorArgs {} ConstructingObjectParser parser = new ConstructingObjectParser<>( - "constructor_args_required", (a) -> new NoConstructorArgs()); + "constructor_args_required", + (a) -> new NoConstructorArgs() + ); try { parser.apply(createParser(JsonXContent.jsonXContent, "{}"), null); fail("Expected AssertionError"); } catch (AssertionError e) { - assertEquals("[constructor_args_required] must configure at least one constructor argument. If it doesn't have any it should " - + "use ObjectParser instead of ConstructingObjectParser. This is a bug in the parser declaration.", e.getMessage()); + assertEquals( + "[constructor_args_required] must configure at least one constructor argument. If it doesn't have any it should " + + "use ObjectParser instead of ConstructingObjectParser. This is a bug in the parser declaration.", + e.getMessage() + ); } } @@ -198,13 +232,16 @@ class CalledOneTime { } boolean fooSet = false; + void setFoo(String foo) { assertFalse(fooSet); fooSet = true; } } - ConstructingObjectParser parser = new ConstructingObjectParser<>("one_time_test", - (a) -> new CalledOneTime((String) a[0])); + ConstructingObjectParser parser = new ConstructingObjectParser<>( + "one_time_test", + (a) -> new CalledOneTime((String) a[0]) + ); parser.declareString(CalledOneTime::setFoo, new ParseField("foo")); parser.declareString(ctorArgOptional ? optionalConstructorArg() : constructorArg(), new ParseField("yeah")); @@ -230,12 +267,16 @@ public void testIgnoreUnknownFields() throws IOException { XContentParser parser = createParser(JsonXContent.jsonXContent, "{ \"test\" : \"foo\", \"junk\" : 2 }"); class TestStruct { public final String test; + TestStruct(String test) { this.test = test; } } - ConstructingObjectParser objectParser = new ConstructingObjectParser<>("foo", true, a -> - new TestStruct((String) a[0])); + ConstructingObjectParser objectParser = new ConstructingObjectParser<>( + "foo", + true, + a -> new TestStruct((String) a[0]) + ); objectParser.declareString(constructorArg(), new ParseField("test")); TestStruct s = objectParser.apply(parser, null); assertEquals(s.test, "foo"); @@ -328,15 +369,19 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws public static final ConstructingObjectParser PARSER_VEGETABLE_OPTIONAL = buildParser(true, false); public static final ConstructingObjectParser PARSER_ALL_OPTIONAL = buildParser(false, false); - public static final List> ALL_PARSERS = - List.of(PARSER, PARSER_VEGETABLE_OPTIONAL, PARSER_ALL_OPTIONAL); + public static final List> ALL_PARSERS = List.of( + PARSER, + PARSER_VEGETABLE_OPTIONAL, + PARSER_ALL_OPTIONAL + ); public static final ConstructingObjectParser PARSER_INT_CONTEXT = buildContextParser(); - private static ConstructingObjectParser buildParser(boolean animalRequired, - boolean vegetableRequired) { + private static ConstructingObjectParser buildParser(boolean animalRequired, boolean vegetableRequired) { ConstructingObjectParser parser = new ConstructingObjectParser<>( - "has_required_arguments", a -> new HasCtorArguments((String) a[0], (Integer) a[1])); + "has_required_arguments", + a -> new HasCtorArguments((String) a[0], (Integer) a[1]) + ); parser.declareString(animalRequired ? constructorArg() : optionalConstructorArg(), new ParseField("animal")); parser.declareInt(vegetableRequired ? constructorArg() : optionalConstructorArg(), new ParseField("vegetable")); declareSetters(parser); @@ -345,7 +390,10 @@ private static ConstructingObjectParser buildParser(bool private static ConstructingObjectParser buildContextParser() { ConstructingObjectParser parser = new ConstructingObjectParser<>( - "has_required_arguments", false, (args, ctx) -> new HasCtorArguments((String) args[0], ctx)); + "has_required_arguments", + false, + (args, ctx) -> new HasCtorArguments((String) args[0], ctx) + ); parser.declareString(constructorArg(), new ParseField("animal")); declareSetters(parser); return parser; @@ -394,9 +442,13 @@ public void testParseNamedObjectTwoFieldsInArray() throws IOException { ); XContentParseException e = expectThrows(XContentParseException.class, () -> NamedObjectHolder.PARSER.apply(parser, null)); assertThat(e.getMessage(), containsString("[named_object_holder] failed to parse field [named]")); - assertThat(e.getCause().getMessage(), - containsString("[named] can be a single object with any number of fields " + - "or an array where each entry is an object with a single field")); + assertThat( + e.getCause().getMessage(), + containsString( + "[named] can be a single object with any number of fields " + + "or an array where each entry is an object with a single field" + ) + ); } public void testParseNamedObjectTwoFieldsInArrayConstructorArg() throws IOException { @@ -406,27 +458,39 @@ public void testParseNamedObjectTwoFieldsInArrayConstructorArg() throws IOExcept ); XContentParseException e = expectThrows(XContentParseException.class, () -> NamedObjectHolder.PARSER.apply(parser, null)); assertThat(e.getMessage(), containsString("[named_object_holder] failed to parse field [named_in_constructor]")); - assertThat(e.getCause().getMessage(), - containsString("[named_in_constructor] can be a single object with any number of fields " - + "or an array where each entry is an object with a single field")); + assertThat( + e.getCause().getMessage(), + containsString( + "[named_in_constructor] can be a single object with any number of fields " + + "or an array where each entry is an object with a single field" + ) + ); } public void testParseNamedObjectNoFieldsInArray() throws IOException { XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"named\": [ {} ], \"named_in_constructor\": [ {\"a\": {}} ]}"); XContentParseException e = expectThrows(XContentParseException.class, () -> NamedObjectHolder.PARSER.apply(parser, null)); assertThat(e.getMessage(), containsString("[named_object_holder] failed to parse field [named]")); - assertThat(e.getCause().getMessage(), - containsString("[named] can be a single object with any number of fields " + - "or an array where each entry is an object with a single field")); + assertThat( + e.getCause().getMessage(), + containsString( + "[named] can be a single object with any number of fields " + + "or an array where each entry is an object with a single field" + ) + ); } public void testParseNamedObjectNoFieldsInArrayConstructorArg() throws IOException { XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"named\": [ {\"a\": {}} ], \"named_in_constructor\": [ {} ]}"); XContentParseException e = expectThrows(XContentParseException.class, () -> NamedObjectHolder.PARSER.apply(parser, null)); assertThat(e.getMessage(), containsString("[named_object_holder] failed to parse field [named_in_constructor]")); - assertThat(e.getCause().getMessage(), - containsString("[named_in_constructor] can be a single object with any number of fields " - + "or an array where each entry is an object with a single field")); + assertThat( + e.getCause().getMessage(), + containsString( + "[named_in_constructor] can be a single object with any number of fields " + + "or an array where each entry is an object with a single field" + ) + ); } public void testParseNamedObjectJunkInArray() throws IOException { @@ -436,9 +500,13 @@ public void testParseNamedObjectJunkInArray() throws IOException { ); XContentParseException e = expectThrows(XContentParseException.class, () -> NamedObjectHolder.PARSER.apply(parser, null)); assertThat(e.getMessage(), containsString("[named_object_holder] failed to parse field [named]")); - assertThat(e.getCause().getMessage(), - containsString("[named] can be a single object with any number of fields " + - "or an array where each entry is an object with a single field")); + assertThat( + e.getCause().getMessage(), + containsString( + "[named] can be a single object with any number of fields " + + "or an array where each entry is an object with a single field" + ) + ); } public void testParseNamedObjectJunkInArrayConstructorArg() throws IOException { @@ -448,9 +516,13 @@ public void testParseNamedObjectJunkInArrayConstructorArg() throws IOException { ); XContentParseException e = expectThrows(XContentParseException.class, () -> NamedObjectHolder.PARSER.apply(parser, null)); assertThat(e.getMessage(), containsString("[named_object_holder] failed to parse field [named_in_constructor]")); - assertThat(e.getCause().getMessage(), - containsString("[named_in_constructor] can be a single object with any number of fields " - + "or an array where each entry is an object with a single field")); + assertThat( + e.getCause().getMessage(), + containsString( + "[named_in_constructor] can be a single object with any number of fields " + + "or an array where each entry is an object with a single field" + ) + ); } public void testParseNamedObjectInOrderNotSupported() throws IOException { @@ -461,10 +533,15 @@ public void testParseNamedObjectInOrderNotSupported() throws IOException { // Create our own parser for this test so we can disable support for the "ordered" mode specified by the array above @SuppressWarnings("unchecked") - ConstructingObjectParser objectParser = new ConstructingObjectParser<>("named_object_holder", - a -> new NamedObjectHolder(((List) a[0]))); - objectParser.declareNamedObjects(ConstructingObjectParser.constructorArg(), NamedObject.PARSER, - new ParseField("named_in_constructor")); + ConstructingObjectParser objectParser = new ConstructingObjectParser<>( + "named_object_holder", + a -> new NamedObjectHolder(((List) a[0])) + ); + objectParser.declareNamedObjects( + ConstructingObjectParser.constructorArg(), + NamedObject.PARSER, + new ParseField("named_in_constructor") + ); objectParser.declareNamedObjects(NamedObjectHolder::setNamed, NamedObject.PARSER, new ParseField("named")); // Now firing the xml through it fails @@ -481,28 +558,45 @@ public void testParseNamedObjectInOrderNotSupportedConstructorArg() throws IOExc // Create our own parser for this test so we can disable support for the "ordered" mode specified by the array above @SuppressWarnings("unchecked") - ConstructingObjectParser objectParser = new ConstructingObjectParser<>("named_object_holder", - a -> new NamedObjectHolder(((List) a[0]))); - objectParser.declareNamedObjects(ConstructingObjectParser.constructorArg(), NamedObject.PARSER, - new ParseField("named_in_constructor")); + ConstructingObjectParser objectParser = new ConstructingObjectParser<>( + "named_object_holder", + a -> new NamedObjectHolder(((List) a[0])) + ); + objectParser.declareNamedObjects( + ConstructingObjectParser.constructorArg(), + NamedObject.PARSER, + new ParseField("named_in_constructor") + ); objectParser.declareNamedObjects(NamedObjectHolder::setNamed, NamedObject.PARSER, new ParseField("named")); // Now firing the xml through it fails XContentParseException e = expectThrows(XContentParseException.class, () -> objectParser.apply(parser, null)); assertThat(e.getMessage(), containsString("[named_object_holder] failed to parse field [named_in_constructor]")); - assertThat(e.getCause().getMessage(), - containsString("[named_in_constructor] doesn't support arrays. Use a single object with multiple fields.")); + assertThat( + e.getCause().getMessage(), + containsString("[named_in_constructor] doesn't support arrays. Use a single object with multiple fields.") + ); } static class NamedObjectHolder { @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("named_object_holder", - a -> new NamedObjectHolder(((List) a[0]))); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "named_object_holder", + a -> new NamedObjectHolder(((List) a[0])) + ); static { - PARSER.declareNamedObjects(ConstructingObjectParser.constructorArg(), NamedObject.PARSER, NamedObjectHolder::keepNamedInOrder, - new ParseField("named_in_constructor")); - PARSER.declareNamedObjects(NamedObjectHolder::setNamed, NamedObject.PARSER, NamedObjectHolder::keepNamedInOrder, - new ParseField("named")); + PARSER.declareNamedObjects( + ConstructingObjectParser.constructorArg(), + NamedObject.PARSER, + NamedObjectHolder::keepNamedInOrder, + new ParseField("named_in_constructor") + ); + PARSER.declareNamedObjects( + NamedObjectHolder::setNamed, + NamedObject.PARSER, + NamedObjectHolder::keepNamedInOrder, + new ParseField("named") + ); } private List named; @@ -527,10 +621,12 @@ public void testRequiredAndExclusiveFields() throws IOException { class TestStruct { final String a; final long b; + TestStruct(String a) { this.a = a; this.b = 0; } + TestStruct(long b) { this.a = null; this.b = b; @@ -563,28 +659,33 @@ class TestStruct { assertThat(e.getMessage(), containsString("Required one of fields [a, b], but none were specified.")); } - //migrating name and type from old_string_name:String to new_int_name:int + // migrating name and type from old_string_name:String to new_int_name:int public static class StructWithCompatibleFields { // real usage would have RestApiVersion.V_7 instead of currentVersion or minimumSupported - static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("struct_with_compatible_fields", a -> new StructWithCompatibleFields((Integer)a[0])); + static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "struct_with_compatible_fields", + a -> new StructWithCompatibleFields((Integer) a[0]) + ); static { // declare a field with `new_name` being preferable, and old_name deprecated. // The declaration is only available for lookup when parser has compatibility set - PARSER.declareInt(constructorArg(), - new ParseField("new_name", "old_name") - .forRestApiVersion(RestApiVersion.equalTo(RestApiVersion.minimumSupported()))); + PARSER.declareInt( + constructorArg(), + new ParseField("new_name", "old_name").forRestApiVersion(RestApiVersion.equalTo(RestApiVersion.minimumSupported())) + ); // declare `new_name` to be parsed when compatibility is NOT used - PARSER.declareInt(constructorArg(), - new ParseField("new_name") - .forRestApiVersion(RestApiVersion.equalTo(RestApiVersion.current()))); + PARSER.declareInt( + constructorArg(), + new ParseField("new_name").forRestApiVersion(RestApiVersion.equalTo(RestApiVersion.current())) + ); // declare `old_name` to throw exception when compatibility is NOT used - PARSER.declareInt((r,s) -> failWithException(), - new ParseField("old_name") - .forRestApiVersion(RestApiVersion.equalTo(RestApiVersion.current()))); + PARSER.declareInt( + (r, s) -> failWithException(), + new ParseField("old_name").forRestApiVersion(RestApiVersion.equalTo(RestApiVersion.current())) + ); } private int intField; @@ -600,34 +701,51 @@ private static void failWithException() { public void testCompatibleFieldDeclarations() throws IOException { { // new_name is the only way to parse when compatibility is not set - XContentParser parser = createParserWithCompatibilityFor(JsonXContent.jsonXContent, "{\"new_name\": 1}", - RestApiVersion.current()); + XContentParser parser = createParserWithCompatibilityFor( + JsonXContent.jsonXContent, + "{\"new_name\": 1}", + RestApiVersion.current() + ); StructWithCompatibleFields o = StructWithCompatibleFields.PARSER.parse(parser, null); assertEquals(1, o.intField); } { // old_name results with an exception when compatibility is not set - XContentParser parser = createParserWithCompatibilityFor(JsonXContent.jsonXContent, "{\"old_name\": 1}", - RestApiVersion.current()); + XContentParser parser = createParserWithCompatibilityFor( + JsonXContent.jsonXContent, + "{\"old_name\": 1}", + RestApiVersion.current() + ); expectThrows(IllegalArgumentException.class, () -> StructWithCompatibleFields.PARSER.parse(parser, null)); } { // new_name is allowed to be parsed with compatibility - XContentParser parser = createParserWithCompatibilityFor(JsonXContent.jsonXContent, "{\"new_name\": 1}", - RestApiVersion.minimumSupported()); + XContentParser parser = createParserWithCompatibilityFor( + JsonXContent.jsonXContent, + "{\"new_name\": 1}", + RestApiVersion.minimumSupported() + ); StructWithCompatibleFields o = StructWithCompatibleFields.PARSER.parse(parser, null); assertEquals(1, o.intField); } { // old_name is allowed to be parsed with compatibility, but results in deprecation - XContentParser parser = createParserWithCompatibilityFor(JsonXContent.jsonXContent, "{\"old_name\": 1}", - RestApiVersion.minimumSupported()); + XContentParser parser = createParserWithCompatibilityFor( + JsonXContent.jsonXContent, + "{\"old_name\": 1}", + RestApiVersion.minimumSupported() + ); StructWithCompatibleFields o = StructWithCompatibleFields.PARSER.parse(parser, null); assertEquals(1, o.intField); - assertWarnings(false, new DeprecationWarning(DeprecationLogger.CRITICAL, "[struct_with_compatible_fields][1:14] " + - "Deprecated field [old_name] used, expected [new_name] instead")); + assertWarnings( + false, + new DeprecationWarning( + DeprecationLogger.CRITICAL, + "[struct_with_compatible_fields][1:14] " + "Deprecated field [old_name] used, expected [new_name] instead" + ) + ); } } @@ -636,27 +754,29 @@ public static class StructRemovalField { private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(StructRemovalField.class); // real usage would have RestApiVersion.V_7 instead of currentVersion or minimumSupported - static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("struct_removal", a -> new StructRemovalField((String)a[0])); + static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "struct_removal", + a -> new StructRemovalField((String) a[0]) + ); static { - //we still need to have something to pass to a constructor. Otherwise use ObjectParser + // we still need to have something to pass to a constructor. Otherwise use ObjectParser PARSER.declareString(constructorArg(), new ParseField("second_field")); - - // declare a field with `old_name` being preferable, no deprecated name. // deprecated field name results in a deprecation warning with a suggestion what field to use. // the field was removed so there is nothing to suggest. // The deprecation shoudl be done manually - PARSER.declareInt(logWarningDoNothing("old_name"), - new ParseField("old_name") - .forRestApiVersion(RestApiVersion.equalTo(RestApiVersion.minimumSupported()))); + PARSER.declareInt( + logWarningDoNothing("old_name"), + new ParseField("old_name").forRestApiVersion(RestApiVersion.equalTo(RestApiVersion.minimumSupported())) + ); // declare `old_name` to throw exception when compatibility is NOT used - PARSER.declareInt((r,s) -> failWithException(), - new ParseField("old_name") - .forRestApiVersion(RestApiVersion.equalTo(RestApiVersion.current()))); + PARSER.declareInt( + (r, s) -> failWithException(), + new ParseField("old_name").forRestApiVersion(RestApiVersion.equalTo(RestApiVersion.current())) + ); } private final String secondField; @@ -666,8 +786,10 @@ public StructRemovalField(String secondField) { } private static BiConsumer logWarningDoNothing(String old_name) { - return (struct,value) -> deprecationLogger.compatibleCritical("struct_removal", - "The field old_name has been removed and is being ignored"); + return (struct, value) -> deprecationLogger.compatibleCritical( + "struct_removal", + "The field old_name has been removed and is being ignored" + ); } private static void failWithException() { @@ -678,17 +800,21 @@ private static void failWithException() { public void testRemovalOfField() throws IOException { { // old_name with NO compatibility is resulting in an exception - XContentParser parser = createParserWithCompatibilityFor(JsonXContent.jsonXContent, + XContentParser parser = createParserWithCompatibilityFor( + JsonXContent.jsonXContent, "{\"old_name\": 1, \"second_field\": \"someString\"}", - RestApiVersion.current()); + RestApiVersion.current() + ); expectThrows(XContentParseException.class, () -> StructRemovalField.PARSER.parse(parser, null)); } { // old_name with compatibility is still parsed, but ignored and results in a warning - XContentParser parser = createParserWithCompatibilityFor(JsonXContent.jsonXContent, + XContentParser parser = createParserWithCompatibilityFor( + JsonXContent.jsonXContent, "{\"old_name\": 1, \"second_field\": \"someString\"}", - RestApiVersion.minimumSupported()); + RestApiVersion.minimumSupported() + ); StructRemovalField parse = StructRemovalField.PARSER.parse(parser, null); assertWarnings("The field old_name has been removed and is being ignored"); @@ -703,12 +829,16 @@ class DoubleFieldDeclaration { this.intField = intField; } } - ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("double_field_declaration", a -> new DoubleFieldDeclaration((int)a[0])); + ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "double_field_declaration", + a -> new DoubleFieldDeclaration((int) a[0]) + ); PARSER.declareInt(constructorArg(), new ParseField("name")); - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, - () -> PARSER.declareInt(constructorArg(), new ParseField("name"))); + IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> PARSER.declareInt(constructorArg(), new ParseField("name")) + ); assertThat(exception, instanceOf(IllegalArgumentException.class)); assertThat(exception.getMessage(), startsWith("Parser already registered for name=[name]")); diff --git a/libs/x-content/src/test/java/org/elasticsearch/xcontent/InstantiatingObjectParserTests.java b/libs/x-content/src/test/java/org/elasticsearch/xcontent/InstantiatingObjectParserTests.java index 34f02b373582e..8661a417491f3 100644 --- a/libs/x-content/src/test/java/org/elasticsearch/xcontent/InstantiatingObjectParserTests.java +++ b/libs/x-content/src/test/java/org/elasticsearch/xcontent/InstantiatingObjectParserTests.java @@ -54,9 +54,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; NoAnnotations that = (NoAnnotations) o; - return a == that.a && - c == that.c && - Objects.equals(b, that.b); + return a == that.a && c == that.c && Objects.equals(b, that.b); } @Override @@ -91,9 +89,10 @@ public void testAmbiguousConstructor() { builder.declareInt(constructorArg(), new ParseField("a")); builder.declareString(constructorArg(), new ParseField("b")); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, builder::build); - assertThat(e.getMessage(), containsString( - "More then one public constructor with 2 arguments found. The use of @ParserConstructor annotation is required" - )); + assertThat( + e.getMessage(), + containsString("More then one public constructor with 2 arguments found. The use of @ParserConstructor annotation is required") + ); } public void testPrivateConstructor() { @@ -126,8 +125,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; LonelyArgument that = (LonelyArgument) o; - return a == that.a && - Objects.equals(b, that.b); + return a == that.a && Objects.equals(b, that.b); } @Override @@ -188,9 +186,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Annotations that = (Annotations) o; - return a == that.a && - c == that.c && - Objects.equals(b, that.b); + return a == that.a && c == that.c && Objects.equals(b, that.b); } @Override @@ -229,12 +225,16 @@ class DoubleFieldDeclaration { } } - InstantiatingObjectParser.Builder builder = - InstantiatingObjectParser.builder("double_declaration", DoubleFieldDeclaration.class); + InstantiatingObjectParser.Builder builder = InstantiatingObjectParser.builder( + "double_declaration", + DoubleFieldDeclaration.class + ); builder.declareInt(constructorArg(), new ParseField("name")); - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, - () -> builder.declareInt(constructorArg(), new ParseField("name"))); + IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> builder.declareInt(constructorArg(), new ParseField("name")) + ); assertThat(exception, instanceOf(IllegalArgumentException.class)); assertThat(exception.getMessage(), startsWith("Parser already registered for name=[name]")); @@ -254,7 +254,6 @@ public ContextArgument(int a, String b) { this(a, b, -1); } - public ContextArgument(int a, String b, long c) { this(null, a, b, c); } @@ -279,9 +278,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; ContextArgument that = (ContextArgument) o; - return a == that.a && - c == that.c && - Objects.equals(b, that.b); + return a == that.a && c == that.c && Objects.equals(b, that.b); } @Override diff --git a/libs/x-content/src/test/java/org/elasticsearch/xcontent/MapXContentParserTests.java b/libs/x-content/src/test/java/org/elasticsearch/xcontent/MapXContentParserTests.java index 15c2104799570..48d176dc9e955 100644 --- a/libs/x-content/src/test/java/org/elasticsearch/xcontent/MapXContentParserTests.java +++ b/libs/x-content/src/test/java/org/elasticsearch/xcontent/MapXContentParserTests.java @@ -8,12 +8,12 @@ package org.elasticsearch.xcontent; +import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; import org.elasticsearch.core.CheckedConsumer; -import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xcontent.support.MapXContentParser; -import org.elasticsearch.test.ESTestCase; import java.io.IOException; import java.util.EnumSet; @@ -53,7 +53,7 @@ public void testSimpleMap() throws IOException { builder.endObject(); } builder.endObject(); - builder.field("bytes", new byte[]{1, 2, 3}); + builder.field("bytes", new byte[] { 1, 2, 3 }); builder.nullField("nothing"); builder.endObject(); }); @@ -112,8 +112,14 @@ private void compareTokens(CheckedConsumer consume } try (XContentParser parser = createParser(xContentType.xContent(), BytesReference.bytes(builder))) { - try (XContentParser mapParser = new MapXContentParser( - xContentRegistry(), LoggingDeprecationHandler.INSTANCE, map, xContentType)) { + try ( + XContentParser mapParser = new MapXContentParser( + xContentRegistry(), + LoggingDeprecationHandler.INSTANCE, + map, + xContentType + ) + ) { assertEquals(parser.contentType(), mapParser.contentType().canonical()); XContentParser.Token token; assertEquals(parser.currentToken(), mapParser.currentToken()); @@ -124,8 +130,7 @@ private void compareTokens(CheckedConsumer consume assertEquals(token, mapToken); assertEquals(parser.currentName(), mapParser.currentName()); if (token != null && (token.isValue() || token == XContentParser.Token.VALUE_NULL)) { - if ((xContentType.canonical() != XContentType.YAML) || - token != XContentParser.Token.VALUE_EMBEDDED_OBJECT) { + if ((xContentType.canonical() != XContentType.YAML) || token != XContentParser.Token.VALUE_EMBEDDED_OBJECT) { // YAML struggles with converting byte arrays into text, because it // does weird base64 decoding to the values. We don't do this // weirdness in the MapXContentParser, so don't try to stringify it. @@ -139,8 +144,8 @@ private void compareTokens(CheckedConsumer consume case VALUE_NUMBER: assertEquals(parser.numberType(), mapParser.numberType()); assertEquals(parser.numberValue(), mapParser.numberValue()); - if (parser.numberType() == XContentParser.NumberType.LONG || - parser.numberType() == XContentParser.NumberType.INT) { + if (parser.numberType() == XContentParser.NumberType.LONG + || parser.numberType() == XContentParser.NumberType.INT) { assertEquals(parser.longValue(), mapParser.longValue()); if (parser.longValue() <= Integer.MAX_VALUE && parser.longValue() >= Integer.MIN_VALUE) { assertEquals(parser.intValue(), mapParser.intValue()); diff --git a/libs/x-content/src/test/java/org/elasticsearch/xcontent/ObjectParserTests.java b/libs/x-content/src/test/java/org/elasticsearch/xcontent/ObjectParserTests.java index 8882d38358f0c..b754d1360a2a2 100644 --- a/libs/x-content/src/test/java/org/elasticsearch/xcontent/ObjectParserTests.java +++ b/libs/x-content/src/test/java/org/elasticsearch/xcontent/ObjectParserTests.java @@ -7,15 +7,15 @@ */ package org.elasticsearch.xcontent; +import org.elasticsearch.common.Strings; import org.elasticsearch.common.logging.DeprecationLogger; import org.elasticsearch.common.xcontent.XContentParserUtils; import org.elasticsearch.core.CheckedFunction; import org.elasticsearch.core.RestApiVersion; -import org.elasticsearch.common.Strings; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ObjectParser.NamedObjectParser; import org.elasticsearch.xcontent.ObjectParser.ValueType; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.ESTestCase; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -49,6 +49,7 @@ class TestStruct { public String test; int testNumber; List ints = new ArrayList<>(); + public void setTestNumber(int testNumber) { this.testNumber = testNumber; } @@ -71,22 +72,40 @@ public void setInts(List ints) { public void testNullDeclares() { ObjectParser objectParser = new ObjectParser<>("foo"); - Exception e = expectThrows(IllegalArgumentException.class, - () -> objectParser.declareField(null, (r, c) -> null, new ParseField("test"), ObjectParser.ValueType.STRING)); + Exception e = expectThrows( + IllegalArgumentException.class, + () -> objectParser.declareField(null, (r, c) -> null, new ParseField("test"), ObjectParser.ValueType.STRING) + ); assertEquals("[consumer] is required", e.getMessage()); - e = expectThrows(IllegalArgumentException.class, () -> objectParser.declareField( - (o, v) -> {}, (ContextParser) null, - new ParseField("test"), ObjectParser.ValueType.STRING)); + e = expectThrows( + IllegalArgumentException.class, + () -> objectParser.declareField( + (o, v) -> {}, + (ContextParser) null, + new ParseField("test"), + ObjectParser.ValueType.STRING + ) + ); assertEquals("[parser] is required", e.getMessage()); - e = expectThrows(IllegalArgumentException.class, () -> objectParser.declareField( - (o, v) -> {}, (CheckedFunction) null, - new ParseField("test"), ObjectParser.ValueType.STRING)); + e = expectThrows( + IllegalArgumentException.class, + () -> objectParser.declareField( + (o, v) -> {}, + (CheckedFunction) null, + new ParseField("test"), + ObjectParser.ValueType.STRING + ) + ); assertEquals("[parser] is required", e.getMessage()); - e = expectThrows(IllegalArgumentException.class, () -> objectParser.declareField( - (o, v) -> {}, (r, c) -> null, null, ObjectParser.ValueType.STRING)); + e = expectThrows( + IllegalArgumentException.class, + () -> objectParser.declareField((o, v) -> {}, (r, c) -> null, null, ObjectParser.ValueType.STRING) + ); assertEquals("[parseField] is required", e.getMessage()); - e = expectThrows(IllegalArgumentException.class, () -> objectParser.declareField( - (o, v) -> {}, (r, c) -> null, new ParseField("test"), null)); + e = expectThrows( + IllegalArgumentException.class, + () -> objectParser.declareField((o, v) -> {}, (r, c) -> null, new ParseField("test"), null) + ); assertEquals("[type] is required", e.getMessage()); } @@ -118,16 +137,16 @@ URI parseURI(XContentParser parser) throws IOException { String host = ""; int port = 0; XContentParser.Token token; - while (( token = parser.currentToken()) != XContentParser.Token.END_OBJECT) { + while ((token = parser.currentToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { fieldName = parser.currentName(); - } else if (token == XContentParser.Token.VALUE_STRING){ + } else if (token == XContentParser.Token.VALUE_STRING) { if (fieldName.equals("host")) { host = parser.text(); } else { throw new IllegalStateException("boom"); } - } else if (token == XContentParser.Token.VALUE_NUMBER){ + } else if (token == XContentParser.Token.VALUE_NUMBER) { if (fieldName.equals("port")) { port = parser.intValue(); } else { @@ -142,6 +161,7 @@ URI parseURI(XContentParser parser) throws IOException { class Foo { public String name; public URI uri; + public void setName(String name) { this.name = name; } @@ -175,15 +195,14 @@ public URI parseURI(XContentParser parser) { objectParser.declareString(Foo::setName, new ParseField("name")); objectParser.declareObjectOrDefault(Foo::setURI, (p, s) -> s.parseURI(p), () -> null, new ParseField("url")); Foo s = objectParser.parse(parser, new Foo(), new CustomParseContext(new ClassicParser())); - assertEquals(s.uri.getHost(), "foobar"); - assertEquals(s.uri.getPort(), 80); + assertEquals(s.uri.getHost(), "foobar"); + assertEquals(s.uri.getPort(), 80); assertEquals(s.name, "foobarbaz"); } public void testExceptions() throws IOException { class TestStruct { - public void setTest(int test) { - } + public void setTest(int test) {} } ObjectParser objectParser = new ObjectParser<>("the_parser"); TestStruct s = new TestStruct(); @@ -212,8 +231,10 @@ class TestStruct { objectParser.declareField((i, v, c) -> v.test = i.text(), new ParseField("test", "old_test"), ObjectParser.ValueType.STRING); objectParser.parse(parser, s, null); assertEquals("foo", s.test); - assertWarnings(false, new DeprecationWarning(DeprecationLogger.CRITICAL, "[foo][1:15] Deprecated field [old_test] used, " + - "expected [test] instead")); + assertWarnings( + false, + new DeprecationWarning(DeprecationLogger.CRITICAL, "[foo][1:15] Deprecated field [old_test] used, " + "expected [test] instead") + ); } public void testFailOnValueType() throws IOException { @@ -240,8 +261,7 @@ class TestStruct { TestStruct s = new TestStruct(); s.object = new TestStruct(); objectParser.declareField((i, c, x) -> c.test = i.intValue(), new ParseField("test"), ValueType.INT); - objectParser.declareField((i, c, x) -> objectParser.parse(parser, c.object, null), new ParseField("object"), - ValueType.OBJECT); + objectParser.declareField((i, c, x) -> objectParser.parse(parser, c.object, null), new ParseField("object"), ValueType.OBJECT); objectParser.parse(parser, s, null); assertEquals(s.test, 1); assertEquals(s.object.test, 2); @@ -310,7 +330,8 @@ public void setObjectArray(List objectArray) { } enum TestEnum { - FOO, BAR + FOO, + BAR } public void testParseEnumFromString() throws IOException { @@ -411,39 +432,51 @@ class TestStruct { List string_array_field; boolean null_value; String string_or_null = "adsfsa"; + public void setInt_field(int int_field) { this.int_field = int_field; } + public void setNullableIntField(int nullableIntField) { this.nullableIntField = nullableIntField; } + public void setLong_field(long long_field) { this.long_field = long_field; } + public void setFloat_field(float float_field) { this.float_field = float_field; } + public void setDouble_field(double double_field) { this.double_field = double_field; } + public void setNullableDoubleField(double nullableDoubleField) { this.nullableDoubleField = nullableDoubleField; } + public void setString_field(String string_field) { this.string_field = string_field; } + public void setInt_array_field(List int_array_field) { this.int_array_field = int_array_field; } + public void setLong_array_field(List long_array_field) { this.long_array_field = long_array_field; } + public void setFloat_array_field(List float_array_field) { this.float_array_field = float_array_field; } + public void setDouble_array_field(List double_array_field) { this.double_array_field = double_array_field; } + public void setString_array_field(List string_array_field) { this.string_array_field = string_array_field; } @@ -499,8 +532,7 @@ public void setString_or_null(String string_or_null) { } public void testParseNamedObject() throws IOException { - XContentParser parser = createParser(JsonXContent.jsonXContent, - "{\"named\": { \"a\": {\"foo\" : 11} }, \"bar\": \"baz\"}"); + XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"named\": { \"a\": {\"foo\" : 11} }, \"bar\": \"baz\"}"); NamedObjectHolder h = NamedObjectHolder.PARSER.apply(parser, null); assertEquals("a", h.named.name); assertEquals(11, h.named.foo); @@ -533,35 +565,46 @@ public void testParseNamedObjectsTwoFieldsInArray() throws IOException { XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"named\": [ {\"a\": {}, \"b\": {}}]}"); XContentParseException e = expectThrows(XContentParseException.class, () -> NamedObjectsHolder.PARSER.apply(parser, null)); assertThat(e.getMessage(), containsString("[named_objects_holder] failed to parse field [named]")); - assertThat(e.getCause().getMessage(), - containsString("[named] can be a single object with any number of fields " + - "or an array where each entry is an object with a single field")); + assertThat( + e.getCause().getMessage(), + containsString( + "[named] can be a single object with any number of fields " + + "or an array where each entry is an object with a single field" + ) + ); } public void testParseNamedObjectsNoFieldsInArray() throws IOException { XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"named\": [ {} ]}"); XContentParseException e = expectThrows(XContentParseException.class, () -> NamedObjectsHolder.PARSER.apply(parser, null)); assertThat(e.getMessage(), containsString("[named_objects_holder] failed to parse field [named]")); - assertThat(e.getCause().getMessage(), - containsString("[named] can be a single object with any number of fields " + - "or an array where each entry is an object with a single field")); + assertThat( + e.getCause().getMessage(), + containsString( + "[named] can be a single object with any number of fields " + + "or an array where each entry is an object with a single field" + ) + ); } public void testParseNamedObjectsJunkInArray() throws IOException { XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"named\": [ \"junk\" ] }"); XContentParseException e = expectThrows(XContentParseException.class, () -> NamedObjectsHolder.PARSER.apply(parser, null)); assertThat(e.getMessage(), containsString("[named_objects_holder] failed to parse field [named]")); - assertThat(e.getCause().getMessage(), - containsString("[named] can be a single object with any number of fields " + - "or an array where each entry is an object with a single field")); + assertThat( + e.getCause().getMessage(), + containsString( + "[named] can be a single object with any number of fields " + + "or an array where each entry is an object with a single field" + ) + ); } public void testParseNamedObjectsInOrderNotSupported() throws IOException { XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"named\": [ {\"a\": {}} ] }"); // Create our own parser for this test so we can disable support for the "ordered" mode specified by the array above - ObjectParser objectParser = new ObjectParser<>("named_object_holder", - NamedObjectsHolder::new); + ObjectParser objectParser = new ObjectParser<>("named_object_holder", NamedObjectsHolder::new); objectParser.declareNamedObjects(NamedObjectsHolder::setNamed, NamedObject.PARSER, new ParseField("named")); // Now firing the xml through it fails @@ -668,8 +711,12 @@ public void setArray(List testArray) { ObjectParser objectParser = new ObjectParser<>("foo"); TestStruct s = new TestStruct(); - objectParser.declareFieldArray(TestStruct::setArray, (p, c) -> XContentParserUtils.parseFieldsValue(p), - new ParseField("test_array"), ValueType.VALUE_ARRAY); + objectParser.declareFieldArray( + TestStruct::setArray, + (p, c) -> XContentParserUtils.parseFieldsValue(p), + new ParseField("test_array"), + ValueType.VALUE_ARRAY + ); objectParser.declareIntArray(TestStruct::setInts, new ParseField("int_array")); objectParser.parse(parser, s, null); assertEquals(s.testArray, Arrays.asList(1, null, "3", 4.2)); @@ -702,50 +749,57 @@ public void setArray(List testArray) { public void testNoopDeclareObject() throws IOException { ObjectParser, Void> parser = new ObjectParser<>("noopy", AtomicReference::new); parser.declareString(AtomicReference::set, new ParseField("body")); - parser.declareObject((a,b) -> {}, (p, c) -> null, new ParseField("noop")); + parser.declareObject((a, b) -> {}, (p, c) -> null, new ParseField("noop")); assertEquals("i", parser.parse(createParser(JsonXContent.jsonXContent, "{\"body\": \"i\"}"), null).get()); - Exception garbageException = expectThrows(IllegalStateException.class, () -> parser.parse( - createParser(JsonXContent.jsonXContent, "{\"noop\": {\"garbage\": \"shouldn't\"}}"), - null)); + Exception garbageException = expectThrows( + IllegalStateException.class, + () -> parser.parse(createParser(JsonXContent.jsonXContent, "{\"noop\": {\"garbage\": \"shouldn't\"}}"), null) + ); assertEquals("parser for [noop] did not end on END_OBJECT", garbageException.getMessage()); - Exception sneakyException = expectThrows(IllegalStateException.class, () -> parser.parse( - createParser(JsonXContent.jsonXContent, "{\"noop\": {\"body\": \"shouldn't\"}}"), - null)); + Exception sneakyException = expectThrows( + IllegalStateException.class, + () -> parser.parse(createParser(JsonXContent.jsonXContent, "{\"noop\": {\"body\": \"shouldn't\"}}"), null) + ); assertEquals("parser for [noop] did not end on END_OBJECT", sneakyException.getMessage()); } public void testNoopDeclareField() throws IOException { ObjectParser, Void> parser = new ObjectParser<>("noopy", AtomicReference::new); parser.declareString(AtomicReference::set, new ParseField("body")); - parser.declareField((a,b) -> {}, (p, c) -> null, new ParseField("noop"), ValueType.STRING_ARRAY); + parser.declareField((a, b) -> {}, (p, c) -> null, new ParseField("noop"), ValueType.STRING_ARRAY); assertEquals("i", parser.parse(createParser(JsonXContent.jsonXContent, "{\"body\": \"i\"}"), null).get()); - Exception e = expectThrows(IllegalStateException.class, () -> parser.parse( - createParser(JsonXContent.jsonXContent, "{\"noop\": [\"ignored\"]}"), - null)); + Exception e = expectThrows( + IllegalStateException.class, + () -> parser.parse(createParser(JsonXContent.jsonXContent, "{\"noop\": [\"ignored\"]}"), null) + ); assertEquals("parser for [noop] did not end on END_ARRAY", e.getMessage()); } public void testNoopDeclareObjectArray() { ObjectParser, Void> parser = new ObjectParser<>("noopy", AtomicReference::new); parser.declareString(AtomicReference::set, new ParseField("body")); - parser.declareObjectArray((a,b) -> {}, (p, c) -> null, new ParseField("noop")); + parser.declareObjectArray((a, b) -> {}, (p, c) -> null, new ParseField("noop")); - XContentParseException garbageError = expectThrows(XContentParseException.class, () -> parser.parse( - createParser(JsonXContent.jsonXContent, "{\"noop\": [{\"garbage\": \"shouldn't\"}}]"), - null)); + XContentParseException garbageError = expectThrows( + XContentParseException.class, + () -> parser.parse(createParser(JsonXContent.jsonXContent, "{\"noop\": [{\"garbage\": \"shouldn't\"}}]"), null) + ); assertEquals("expected value but got [FIELD_NAME]", garbageError.getCause().getMessage()); - XContentParseException sneakyError = expectThrows(XContentParseException.class, () -> parser.parse( - createParser(JsonXContent.jsonXContent, "{\"noop\": [{\"body\": \"shouldn't\"}}]"), - null)); + XContentParseException sneakyError = expectThrows( + XContentParseException.class, + () -> parser.parse(createParser(JsonXContent.jsonXContent, "{\"noop\": [{\"body\": \"shouldn't\"}}]"), null) + ); assertEquals("expected value but got [FIELD_NAME]", sneakyError.getCause().getMessage()); } // singular static class NamedObjectHolder { - public static final ObjectParser PARSER = new ObjectParser<>("named_object_holder", - NamedObjectHolder::new); + public static final ObjectParser PARSER = new ObjectParser<>( + "named_object_holder", + NamedObjectHolder::new + ); static { PARSER.declareNamedObject(NamedObjectHolder::setNamed, NamedObject.PARSER, new ParseField("named")); PARSER.declareString(NamedObjectHolder::setBar, new ParseField("bar")); @@ -765,11 +819,17 @@ public void setBar(String bar) { // plural static class NamedObjectsHolder { - public static final ObjectParser PARSER = new ObjectParser<>("named_objects_holder", - NamedObjectsHolder::new); + public static final ObjectParser PARSER = new ObjectParser<>( + "named_objects_holder", + NamedObjectsHolder::new + ); static { - PARSER.declareNamedObjects(NamedObjectsHolder::setNamed, NamedObject.PARSER, NamedObjectsHolder::keepNamedInOrder, - new ParseField("named")); + PARSER.declareNamedObjects( + NamedObjectsHolder::setNamed, + NamedObject.PARSER, + NamedObjectsHolder::keepNamedInOrder, + new ParseField("named") + ); } private List named; @@ -807,16 +867,19 @@ public void setFoo(int foo) { private static class ObjectWithArbitraryFields { String name; Map fields = new HashMap<>(); + void setField(String key, Object value) { fields.put(key, value); } + void setName(String name) { this.name = name; } } public void testConsumeUnknownFields() throws IOException { - XContentParser parser = createParser(JsonXContent.jsonXContent, + XContentParser parser = createParser( + JsonXContent.jsonXContent, "{\n" + " \"test\" : \"foo\",\n" + " \"test_number\" : 2,\n" @@ -825,9 +888,13 @@ public void testConsumeUnknownFields() throws IOException { + " \"test_null\" : null,\n" + " \"test_array\": [1,2,3,4],\n" + " \"test_nested\": { \"field\" : \"value\", \"field2\" : [ \"list1\", \"list2\" ] }\n" - + "}"); - ObjectParser op - = new ObjectParser<>("unknown", ObjectWithArbitraryFields::setField, ObjectWithArbitraryFields::new); + + "}" + ); + ObjectParser op = new ObjectParser<>( + "unknown", + ObjectWithArbitraryFields::setField, + ObjectWithArbitraryFields::new + ); op.declareString(ObjectWithArbitraryFields::setName, new ParseField("name")); ObjectWithArbitraryFields o = op.parse(parser, null); @@ -859,7 +926,7 @@ private void setB(long value) { ObjectParser objectParser = new ObjectParser<>("foo", true, TestStruct::new); objectParser.declareLong(TestStruct::setA, new ParseField("a")); objectParser.declareLong(TestStruct::setB, new ParseField("b")); - objectParser.declareRequiredFieldSet(new String[]{"a", "b"}); + objectParser.declareRequiredFieldSet(new String[] { "a", "b" }); TestStruct obj = objectParser.apply(parser, null); assertThat(obj.a, equalTo(123L)); @@ -869,7 +936,7 @@ private void setB(long value) { objectParser = new ObjectParser<>("foo", true, TestStruct::new); objectParser.declareLong(TestStruct::setA, new ParseField("a")); objectParser.declareLong(TestStruct::setB, new ParseField("b")); - objectParser.declareRequiredFieldSet(new String[]{"a", "b"}); + objectParser.declareRequiredFieldSet(new String[] { "a", "b" }); obj = objectParser.apply(parser, null); assertThat(obj.a, nullValue()); @@ -879,7 +946,7 @@ private void setB(long value) { objectParser = new ObjectParser<>("foo", true, TestStruct::new); objectParser.declareLong(TestStruct::setA, new ParseField("a")); objectParser.declareLong(TestStruct::setB, new ParseField("b")); - objectParser.declareRequiredFieldSet(new String[]{"a", "b"}); + objectParser.declareRequiredFieldSet(new String[] { "a", "b" }); obj = objectParser.apply(parser, null); assertThat(obj.a, equalTo(123L)); @@ -917,12 +984,14 @@ public void testMultipleRequiredFieldSet() throws IOException { objectParser.declareLong(TestStruct::setB, new ParseField("b")); objectParser.declareLong(TestStruct::setC, new ParseField("c")); objectParser.declareLong(TestStruct::setD, new ParseField("d")); - objectParser.declareRequiredFieldSet(new String[]{"a", "b"}); - objectParser.declareRequiredFieldSet(new String[]{"c", "d"}); + objectParser.declareRequiredFieldSet(new String[] { "a", "b" }); + objectParser.declareRequiredFieldSet(new String[] { "c", "d" }); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> objectParser.apply(parser, null)); - assertThat(e.getMessage(), equalTo("Required one of fields [a, b], but none were specified. " + - "Required one of fields [c, d], but none were specified. ")); + assertThat( + e.getMessage(), + equalTo("Required one of fields [a, b], but none were specified. " + "Required one of fields [c, d], but none were specified. ") + ); } public void testExclusiveFieldSet() throws IOException { @@ -947,26 +1016,31 @@ public void testExclusiveFieldSet() throws IOException { assertThat(e.getMessage(), containsString("The following fields are not allowed together: [a, b]")); e = expectThrows(IllegalArgumentException.class, () -> parser.parse(badmulti, null)); - assertThat(e.getMessage(), - containsString("allowed together: [a, b] The following fields are not allowed together: [c, d]")); + assertThat(e.getMessage(), containsString("allowed together: [a, b] The following fields are not allowed together: [c, d]")); } @Override protected NamedXContentRegistry xContentRegistry() { - return new NamedXContentRegistry(Arrays.asList( - new NamedXContentRegistry.Entry(Object.class, new ParseField("str"), p -> p.text()), - new NamedXContentRegistry.Entry(Object.class, new ParseField("int"), p -> p.intValue()), - new NamedXContentRegistry.Entry(Object.class, new ParseField("float"), p -> p.floatValue()), - new NamedXContentRegistry.Entry(Object.class, new ParseField("bool"), p -> p.booleanValue()) - )); + return new NamedXContentRegistry( + Arrays.asList( + new NamedXContentRegistry.Entry(Object.class, new ParseField("str"), p -> p.text()), + new NamedXContentRegistry.Entry(Object.class, new ParseField("int"), p -> p.intValue()), + new NamedXContentRegistry.Entry(Object.class, new ParseField("float"), p -> p.floatValue()), + new NamedXContentRegistry.Entry(Object.class, new ParseField("bool"), p -> p.booleanValue()) + ) + ); } private static class TopLevelNamedXConent { public static final ObjectParser PARSER = new ObjectParser<>( - "test", Object.class, TopLevelNamedXConent::setNamed, TopLevelNamedXConent::new + "test", + Object.class, + TopLevelNamedXConent::setNamed, + TopLevelNamedXConent::new ); Object named; + void setNamed(Object named) { if (this.named != null) { throw new IllegalArgumentException("Only one [named] allowed!"); @@ -1015,24 +1089,29 @@ public void testContextBuilder() throws IOException { public static class StructWithCompatibleFields { // real usage would have RestApiVersion.V_7 instead of currentVersion or minimumSupported - static final ObjectParser PARSER = - new ObjectParser<>("struct_with_compatible_fields", StructWithCompatibleFields::new); + static final ObjectParser PARSER = new ObjectParser<>( + "struct_with_compatible_fields", + StructWithCompatibleFields::new + ); static { // declare a field with `new_name` being preferable, and old_name deprecated. // The declaration is only available for lookup when parser has compatibility set - PARSER.declareInt(StructWithCompatibleFields::setIntField, - new ParseField("new_name", "old_name") - .forRestApiVersion(RestApiVersion.equalTo(RestApiVersion.minimumSupported()))); + PARSER.declareInt( + StructWithCompatibleFields::setIntField, + new ParseField("new_name", "old_name").forRestApiVersion(RestApiVersion.equalTo(RestApiVersion.minimumSupported())) + ); // declare `new_name` to be parsed when compatibility is NOT used - PARSER.declareInt(StructWithCompatibleFields::setIntField, - new ParseField("new_name") - .forRestApiVersion(RestApiVersion.onOrAfter(RestApiVersion.current()))); + PARSER.declareInt( + StructWithCompatibleFields::setIntField, + new ParseField("new_name").forRestApiVersion(RestApiVersion.onOrAfter(RestApiVersion.current())) + ); // declare `old_name` to throw exception when compatibility is NOT used - PARSER.declareInt((r,s) -> failWithException(), - new ParseField("old_name") - .forRestApiVersion(RestApiVersion.onOrAfter(RestApiVersion.current()))); + PARSER.declareInt( + (r, s) -> failWithException(), + new ParseField("old_name").forRestApiVersion(RestApiVersion.onOrAfter(RestApiVersion.current())) + ); } private static void failWithException() { @@ -1041,7 +1120,7 @@ private static void failWithException() { private int intField; - private void setIntField(int intField) { + private void setIntField(int intField) { this.intField = intField; } } @@ -1049,53 +1128,74 @@ private void setIntField(int intField) { public void testCompatibleFieldDeclarations() throws IOException { { // new_name is the only way to parse when compatibility is not set - XContentParser parser = createParserWithCompatibilityFor(JsonXContent.jsonXContent, "{\"new_name\": 1}", - RestApiVersion.current()); + XContentParser parser = createParserWithCompatibilityFor( + JsonXContent.jsonXContent, + "{\"new_name\": 1}", + RestApiVersion.current() + ); StructWithCompatibleFields o = StructWithCompatibleFields.PARSER.parse(parser, null); assertEquals(1, o.intField); } { // old_name results with an exception when compatibility is not set - XContentParser parser = createParserWithCompatibilityFor(JsonXContent.jsonXContent, "{\"old_name\": 1}", - RestApiVersion.current()); + XContentParser parser = createParserWithCompatibilityFor( + JsonXContent.jsonXContent, + "{\"old_name\": 1}", + RestApiVersion.current() + ); expectThrows(IllegalArgumentException.class, () -> StructWithCompatibleFields.PARSER.parse(parser, null)); } { // new_name is allowed to be parsed with compatibility - XContentParser parser = createParserWithCompatibilityFor(JsonXContent.jsonXContent, "{\"new_name\": 1}", - RestApiVersion.minimumSupported()); + XContentParser parser = createParserWithCompatibilityFor( + JsonXContent.jsonXContent, + "{\"new_name\": 1}", + RestApiVersion.minimumSupported() + ); StructWithCompatibleFields o = StructWithCompatibleFields.PARSER.parse(parser, null); assertEquals(1, o.intField); } { // old_name is allowed to be parsed with compatibility, but results in deprecation - XContentParser parser = createParserWithCompatibilityFor(JsonXContent.jsonXContent, "{\"old_name\": 1}", - RestApiVersion.minimumSupported()); + XContentParser parser = createParserWithCompatibilityFor( + JsonXContent.jsonXContent, + "{\"old_name\": 1}", + RestApiVersion.minimumSupported() + ); StructWithCompatibleFields o = StructWithCompatibleFields.PARSER.parse(parser, null); assertEquals(1, o.intField); - assertWarnings(false, new DeprecationWarning(DeprecationLogger.CRITICAL, "[struct_with_compatible_fields][1:14] " + - "Deprecated field [old_name] used, expected [new_name] instead")); + assertWarnings( + false, + new DeprecationWarning( + DeprecationLogger.CRITICAL, + "[struct_with_compatible_fields][1:14] " + "Deprecated field [old_name] used, expected [new_name] instead" + ) + ); } } + public static class StructWithOnOrAfterField { // real usage would have exact version like RestApiVersion.V_7 (equal to current version) instead of minimumSupported - static final ObjectParser PARSER = - new ObjectParser<>("struct_with_on_or_after_field", StructWithOnOrAfterField::new); + static final ObjectParser PARSER = new ObjectParser<>( + "struct_with_on_or_after_field", + StructWithOnOrAfterField::new + ); static { // in real usage you would use a real version like RestApiVersion.V_8 and expect it to parse for version V_9, V_10 etc - PARSER.declareInt(StructWithOnOrAfterField::setIntField, - new ParseField("new_name") - .forRestApiVersion(RestApiVersion.onOrAfter(RestApiVersion.minimumSupported()))); + PARSER.declareInt( + StructWithOnOrAfterField::setIntField, + new ParseField("new_name").forRestApiVersion(RestApiVersion.onOrAfter(RestApiVersion.minimumSupported())) + ); } private int intField; - private void setIntField(int intField) { + private void setIntField(int intField) { this.intField = intField; } } @@ -1105,14 +1205,20 @@ public void testFieldsForVersionsOnOrAfter() throws IOException { // to do this, we assume a version N is minimum (so that the test passes for future releases) and the N+1 is current() // new name is accessed in "current" version - lets assume the current is minimumSupported - XContentParser parser = createParserWithCompatibilityFor(JsonXContent.jsonXContent, "{\"new_name\": 1}", - RestApiVersion.minimumSupported()); + XContentParser parser = createParserWithCompatibilityFor( + JsonXContent.jsonXContent, + "{\"new_name\": 1}", + RestApiVersion.minimumSupported() + ); StructWithOnOrAfterField o1 = StructWithOnOrAfterField.PARSER.parse(parser, null); assertEquals(1, o1.intField); // new name is accessed in "future" version - lets assume the future is currentVersion (minimumSupported+1) - XContentParser futureParser = createParserWithCompatibilityFor(JsonXContent.jsonXContent, "{\"new_name\": 1}", - RestApiVersion.current()); + XContentParser futureParser = createParserWithCompatibilityFor( + JsonXContent.jsonXContent, + "{\"new_name\": 1}", + RestApiVersion.current() + ); StructWithOnOrAfterField o2 = StructWithOnOrAfterField.PARSER.parse(futureParser, null); assertEquals(1, o2.intField); } @@ -1121,17 +1227,18 @@ public void testDoubleDeclarationThrowsException() throws IOException { class DoubleFieldDeclaration { private int intField; - private void setIntField(int intField) { + private void setIntField(int intField) { this.intField = intField; } } - ObjectParser PARSER = - new ObjectParser<>("double_field_declaration", DoubleFieldDeclaration::new); + ObjectParser PARSER = new ObjectParser<>("double_field_declaration", DoubleFieldDeclaration::new); PARSER.declareInt(DoubleFieldDeclaration::setIntField, new ParseField("name")); - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, - () -> PARSER.declareInt(DoubleFieldDeclaration::setIntField, new ParseField("name"))); + IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> PARSER.declareInt(DoubleFieldDeclaration::setIntField, new ParseField("name")) + ); assertThat(exception, instanceOf(IllegalArgumentException.class)); assertThat(exception.getMessage(), startsWith("Parser already registered for name=[name]")); diff --git a/libs/x-content/src/test/java/org/elasticsearch/xcontent/ObjectPathTests.java b/libs/x-content/src/test/java/org/elasticsearch/xcontent/ObjectPathTests.java index 4f9dcb3cbf673..162ce73d60995 100644 --- a/libs/x-content/src/test/java/org/elasticsearch/xcontent/ObjectPathTests.java +++ b/libs/x-content/src/test/java/org/elasticsearch/xcontent/ObjectPathTests.java @@ -9,7 +9,6 @@ package org.elasticsearch.xcontent; import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.xcontent.ObjectPath; import java.util.ArrayList; import java.util.Arrays; diff --git a/libs/x-content/src/test/java/org/elasticsearch/xcontent/ParseFieldTests.java b/libs/x-content/src/test/java/org/elasticsearch/xcontent/ParseFieldTests.java index dc663f4455a5e..b067b28f6cbeb 100644 --- a/libs/x-content/src/test/java/org/elasticsearch/xcontent/ParseFieldTests.java +++ b/libs/x-content/src/test/java/org/elasticsearch/xcontent/ParseFieldTests.java @@ -7,10 +7,7 @@ */ package org.elasticsearch.xcontent; -import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; -import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.xcontent.XContentLocation; import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.test.ESTestCase; @@ -25,7 +22,7 @@ public class ParseFieldTests extends ESTestCase { public void testParse() { String name = "foo_bar"; ParseField field = new ParseField(name); - String[] deprecated = new String[]{"barFoo", "bar_foo", "Foobar"}; + String[] deprecated = new String[] { "barFoo", "bar_foo", "Foobar" }; ParseField withDeprecations = field.withDeprecation(deprecated); assertThat(field, not(sameInstance(withDeprecations))); assertThat(field.match(name, LoggingDeprecationHandler.INSTANCE), is(true)); @@ -44,7 +41,7 @@ public void testParse() { public void testAllDeprecated() { String name = "like_text"; - String[] deprecated = new String[]{"text", "same_as_text"}; + String[] deprecated = new String[] { "text", "same_as_text" }; ParseField field = new ParseField(name).withDeprecation(deprecated).withAllDeprecated("like"); assertFalse(field.match("not a field name", LoggingDeprecationHandler.INSTANCE)); assertTrue(field.match("text", LoggingDeprecationHandler.INSTANCE)); @@ -57,7 +54,7 @@ public void testAllDeprecated() { public void testDeprecatedWithNoReplacement() { String name = "dep"; - String[] alternatives = new String[]{"old_dep", "new_dep"}; + String[] alternatives = new String[] { "old_dep", "new_dep" }; ParseField field = new ParseField(name).withDeprecation(alternatives).withAllDeprecated(); assertFalse(field.match("not a field name", LoggingDeprecationHandler.INSTANCE)); assertTrue(field.match("dep", LoggingDeprecationHandler.INSTANCE)); @@ -81,33 +78,44 @@ class TestDeprecationHandler implements DeprecationHandler { public boolean compatibleWarningsUsed = false; @Override - public void logRenamedField(String parserName, Supplier location, String oldName, String currentName) { - } + public void logRenamedField(String parserName, Supplier location, String oldName, String currentName) {} @Override - public void logReplacedField(String parserName, Supplier location, String oldName, String replacedName) { - } + public void logReplacedField(String parserName, Supplier location, String oldName, String replacedName) {} @Override - public void logRemovedField(String parserName, Supplier location, String removedName) { - } + public void logRemovedField(String parserName, Supplier location, String removedName) {} @Override - public void logRenamedField(String parserName, Supplier location, String oldName, String currentName, - boolean isCompatibleDeprecation) { + public void logRenamedField( + String parserName, + Supplier location, + String oldName, + String currentName, + boolean isCompatibleDeprecation + ) { this.compatibleWarningsUsed = isCompatibleDeprecation; } @Override - public void logReplacedField(String parserName, Supplier location, String oldName, String replacedName, - boolean isCompatibleDeprecation) { + public void logReplacedField( + String parserName, + Supplier location, + String oldName, + String replacedName, + boolean isCompatibleDeprecation + ) { this.compatibleWarningsUsed = isCompatibleDeprecation; } @Override - public void logRemovedField(String parserName, Supplier location, String removedName, - boolean isCompatibleDeprecation) { + public void logRemovedField( + String parserName, + Supplier location, + String removedName, + boolean isCompatibleDeprecation + ) { this.compatibleWarningsUsed = isCompatibleDeprecation; } } @@ -116,38 +124,39 @@ public void testCompatibleLoggerIsUsed() { { // a field deprecated in previous version and now available under old name only in compatible api // emitting compatible logs - ParseField field = new ParseField("new_name", "old_name") - .forRestApiVersion(RestApiVersion.equalTo(RestApiVersion.minimumSupported())); + ParseField field = new ParseField("new_name", "old_name").forRestApiVersion( + RestApiVersion.equalTo(RestApiVersion.minimumSupported()) + ); - TestDeprecationHandler testDeprecationHandler = new TestDeprecationHandler(); + TestDeprecationHandler testDeprecationHandler = new TestDeprecationHandler(); assertTrue(field.match("old_name", testDeprecationHandler)); - assertThat(testDeprecationHandler.compatibleWarningsUsed , is(true)); + assertThat(testDeprecationHandler.compatibleWarningsUsed, is(true)); } { - //a regular newly deprecated field. Emitting deprecation logs instead of compatible logs + // a regular newly deprecated field. Emitting deprecation logs instead of compatible logs ParseField field = new ParseField("new_name", "old_name"); - TestDeprecationHandler testDeprecationHandler = new TestDeprecationHandler(); + TestDeprecationHandler testDeprecationHandler = new TestDeprecationHandler(); assertTrue(field.match("old_name", testDeprecationHandler)); - assertThat(testDeprecationHandler.compatibleWarningsUsed , is(false)); + assertThat(testDeprecationHandler.compatibleWarningsUsed, is(false)); } } public void testCompatibleWarnings() { - ParseField field = new ParseField("new_name", "old_name") - .forRestApiVersion(RestApiVersion.equalTo(RestApiVersion.minimumSupported())); + ParseField field = new ParseField("new_name", "old_name").forRestApiVersion( + RestApiVersion.equalTo(RestApiVersion.minimumSupported()) + ); assertTrue(field.match("new_name", LoggingDeprecationHandler.INSTANCE)); ensureNoWarnings(); assertTrue(field.match("old_name", LoggingDeprecationHandler.INSTANCE)); assertWarnings("Deprecated field [old_name] used, expected [new_name] instead"); - ParseField allDepField = new ParseField("dep", "old_name") - .withAllDeprecated() + ParseField allDepField = new ParseField("dep", "old_name").withAllDeprecated() .forRestApiVersion(RestApiVersion.equalTo(RestApiVersion.minimumSupported())); assertTrue(allDepField.match("dep", LoggingDeprecationHandler.INSTANCE)); diff --git a/libs/x-content/src/test/java/org/elasticsearch/xcontent/ParsedMediaTypeTests.java b/libs/x-content/src/test/java/org/elasticsearch/xcontent/ParsedMediaTypeTests.java index f2a3b944b9482..b7e3d640b4d7c 100644 --- a/libs/x-content/src/test/java/org/elasticsearch/xcontent/ParsedMediaTypeTests.java +++ b/libs/x-content/src/test/java/org/elasticsearch/xcontent/ParsedMediaTypeTests.java @@ -9,8 +9,6 @@ package org.elasticsearch.xcontent; import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.xcontent.MediaTypeRegistry; -import org.elasticsearch.xcontent.ParsedMediaType; import java.util.Collections; import java.util.Map; @@ -21,59 +19,68 @@ public class ParsedMediaTypeTests extends ESTestCase { - MediaTypeRegistry mediaTypeRegistry = new MediaTypeRegistry() - .register(XContentType.values()); + MediaTypeRegistry mediaTypeRegistry = new MediaTypeRegistry().register(XContentType.values()); public void testCanonicalParsing() { - assertThat(ParsedMediaType.parseMediaType("application/json") - .toMediaType(mediaTypeRegistry), equalTo(XContentType.JSON)); - assertThat(ParsedMediaType.parseMediaType("application/yaml") - .toMediaType(mediaTypeRegistry), equalTo(XContentType.YAML)); - assertThat(ParsedMediaType.parseMediaType("application/smile") - .toMediaType(mediaTypeRegistry), equalTo(XContentType.SMILE)); - assertThat(ParsedMediaType.parseMediaType("application/cbor") - .toMediaType(mediaTypeRegistry), equalTo(XContentType.CBOR)); - - assertThat(ParsedMediaType.parseMediaType("application/vnd.elasticsearch+json;compatible-with=7") - .toMediaType(mediaTypeRegistry), equalTo(XContentType.VND_JSON)); - assertThat(ParsedMediaType.parseMediaType("application/vnd.elasticsearch+yaml;compatible-with=7") - .toMediaType(mediaTypeRegistry), equalTo(XContentType.VND_YAML)); - assertThat(ParsedMediaType.parseMediaType("application/vnd.elasticsearch+smile;compatible-with=7") - .toMediaType(mediaTypeRegistry), equalTo(XContentType.VND_SMILE)); - assertThat(ParsedMediaType.parseMediaType("application/vnd.elasticsearch+cbor;compatible-with=7") - .toMediaType(mediaTypeRegistry), equalTo(XContentType.VND_CBOR)); + assertThat(ParsedMediaType.parseMediaType("application/json").toMediaType(mediaTypeRegistry), equalTo(XContentType.JSON)); + assertThat(ParsedMediaType.parseMediaType("application/yaml").toMediaType(mediaTypeRegistry), equalTo(XContentType.YAML)); + assertThat(ParsedMediaType.parseMediaType("application/smile").toMediaType(mediaTypeRegistry), equalTo(XContentType.SMILE)); + assertThat(ParsedMediaType.parseMediaType("application/cbor").toMediaType(mediaTypeRegistry), equalTo(XContentType.CBOR)); + + assertThat( + ParsedMediaType.parseMediaType("application/vnd.elasticsearch+json;compatible-with=7").toMediaType(mediaTypeRegistry), + equalTo(XContentType.VND_JSON) + ); + assertThat( + ParsedMediaType.parseMediaType("application/vnd.elasticsearch+yaml;compatible-with=7").toMediaType(mediaTypeRegistry), + equalTo(XContentType.VND_YAML) + ); + assertThat( + ParsedMediaType.parseMediaType("application/vnd.elasticsearch+smile;compatible-with=7").toMediaType(mediaTypeRegistry), + equalTo(XContentType.VND_SMILE) + ); + assertThat( + ParsedMediaType.parseMediaType("application/vnd.elasticsearch+cbor;compatible-with=7").toMediaType(mediaTypeRegistry), + equalTo(XContentType.VND_CBOR) + ); } public void testJsonWithParameters() throws Exception { String mediaType = "application/vnd.elasticsearch+json"; - assertThat(ParsedMediaType.parseMediaType(mediaType).getParameters(), - equalTo(Collections.emptyMap())); - assertThat(ParsedMediaType.parseMediaType(mediaType + ";").getParameters(), - equalTo(Collections.emptyMap())); - assertThat(ParsedMediaType.parseMediaType(mediaType + "; charset=UTF-8").getParameters(), - equalTo(Map.of("charset", "utf-8"))); - assertThat(ParsedMediaType.parseMediaType(mediaType + "; compatible-with=123;charset=UTF-8").getParameters(), - equalTo(Map.of("charset", "utf-8", "compatible-with", "123"))); + assertThat(ParsedMediaType.parseMediaType(mediaType).getParameters(), equalTo(Collections.emptyMap())); + assertThat(ParsedMediaType.parseMediaType(mediaType + ";").getParameters(), equalTo(Collections.emptyMap())); + assertThat(ParsedMediaType.parseMediaType(mediaType + "; charset=UTF-8").getParameters(), equalTo(Map.of("charset", "utf-8"))); + assertThat( + ParsedMediaType.parseMediaType(mediaType + "; compatible-with=123;charset=UTF-8").getParameters(), + equalTo(Map.of("charset", "utf-8", "compatible-with", "123")) + ); } public void testWhiteSpaceInTypeSubtype() { String mediaType = " application/vnd.elasticsearch+json "; - assertThat(ParsedMediaType.parseMediaType(mediaType).toMediaType(mediaTypeRegistry), - equalTo(XContentType.VND_JSON)); - - assertThat(ParsedMediaType.parseMediaType(mediaType + "; compatible-with=123; charset=UTF-8").getParameters(), - equalTo(Map.of("charset", "utf-8", "compatible-with", "123"))); - assertThat(ParsedMediaType.parseMediaType(mediaType + "; compatible-with=123;\n charset=UTF-8").getParameters(), - equalTo(Map.of("charset", "utf-8", "compatible-with", "123"))); + assertThat(ParsedMediaType.parseMediaType(mediaType).toMediaType(mediaTypeRegistry), equalTo(XContentType.VND_JSON)); + + assertThat( + ParsedMediaType.parseMediaType(mediaType + "; compatible-with=123; charset=UTF-8").getParameters(), + equalTo(Map.of("charset", "utf-8", "compatible-with", "123")) + ); + assertThat( + ParsedMediaType.parseMediaType(mediaType + "; compatible-with=123;\n charset=UTF-8").getParameters(), + equalTo(Map.of("charset", "utf-8", "compatible-with", "123")) + ); } public void testInvalidParameters() { String mediaType = "application/vnd.elasticsearch+json"; - expectThrows(IllegalArgumentException.class, () -> ParsedMediaType.parseMediaType(mediaType + "; keyvalueNoEqualsSign") - .toMediaType(mediaTypeRegistry)); - - expectThrows(IllegalArgumentException.class, () -> ParsedMediaType.parseMediaType(mediaType + "; key=") - .toMediaType(mediaTypeRegistry)); + expectThrows( + IllegalArgumentException.class, + () -> ParsedMediaType.parseMediaType(mediaType + "; keyvalueNoEqualsSign").toMediaType(mediaTypeRegistry) + ); + + expectThrows( + IllegalArgumentException.class, + () -> ParsedMediaType.parseMediaType(mediaType + "; key=").toMediaType(mediaTypeRegistry) + ); } public void testXContentTypes() { @@ -88,8 +95,10 @@ public void testWithParameters() { assertEquals(Collections.emptyMap(), ParsedMediaType.parseMediaType(mediaType).getParameters()); assertEquals(Collections.emptyMap(), ParsedMediaType.parseMediaType(mediaType + ";").getParameters()); assertEquals(Map.of("charset", "utf-8"), ParsedMediaType.parseMediaType(mediaType + "; charset=UTF-8").getParameters()); - assertEquals(Map.of("charset", "utf-8", "compatible-with", "123"), - ParsedMediaType.parseMediaType(mediaType + "; compatible-with=123;charset=UTF-8").getParameters()); + assertEquals( + Map.of("charset", "utf-8", "compatible-with", "123"), + ParsedMediaType.parseMediaType(mediaType + "; compatible-with=123;charset=UTF-8").getParameters() + ); } public void testEmptyParams() { @@ -101,19 +110,24 @@ public void testEmptyParams() { public void testMalformedParameters() { String mediaType = "application/foo"; - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, - () -> ParsedMediaType.parseMediaType(mediaType + "; charsetunknown")); + IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> ParsedMediaType.parseMediaType(mediaType + "; charsetunknown") + ); assertThat(exception.getMessage(), equalTo("invalid parameters for header [application/foo; charsetunknown]")); - exception = expectThrows(IllegalArgumentException.class, - () -> ParsedMediaType.parseMediaType(mediaType + "; char=set=unknown")); + exception = expectThrows(IllegalArgumentException.class, () -> ParsedMediaType.parseMediaType(mediaType + "; char=set=unknown")); assertThat(exception.getMessage(), equalTo("invalid parameters for header [application/foo; char=set=unknown]")); // do not allow white space in parameters between `=` - exception = expectThrows(IllegalArgumentException.class, - () -> ParsedMediaType.parseMediaType(mediaType + " ; compatible-with = 123 ; charset=UTF-8")); - assertThat(exception.getMessage(), - equalTo("invalid parameters for header [application/foo ; compatible-with = 123 ; charset=UTF-8]")); + exception = expectThrows( + IllegalArgumentException.class, + () -> ParsedMediaType.parseMediaType(mediaType + " ; compatible-with = 123 ; charset=UTF-8") + ); + assertThat( + exception.getMessage(), + equalTo("invalid parameters for header [application/foo ; compatible-with = 123 ; charset=UTF-8]") + ); expectThrows(IllegalArgumentException.class, () -> ParsedMediaType.parseMediaType(mediaType + ";k =y")); expectThrows(IllegalArgumentException.class, () -> ParsedMediaType.parseMediaType(mediaType + ";k= y")); @@ -126,61 +140,94 @@ public void testIgnoredMediaTypes() { // When using curl */* is used a default Accept header when not specified by a user assertThat(ParsedMediaType.parseMediaType("*/*"), is(nullValue())); - // This media type is defined in sun.net.www.protocol.http.HttpURLConnection as a default Accept header // and used when a header was not set on a request // It should be treated as if a user did not specify a header value String mediaType = "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"; assertThat(ParsedMediaType.parseMediaType(mediaType), is(nullValue())); - //example accept header used by a browser - mediaType = "text/html,application/xhtml+xml,application/xml;q=0.9," + - "image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"; + // example accept header used by a browser + mediaType = "text/html,application/xhtml+xml,application/xml;q=0.9," + + "image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"; ParsedMediaType parsedMediaType = ParsedMediaType.parseMediaType(mediaType); assertThat(parsedMediaType, equalTo(null)); } public void testParseMediaTypeFromXContentType() { - assertThat(ParsedMediaType.parseMediaType(XContentType.YAML, Collections.emptyMap()) - .toMediaType(mediaTypeRegistry), equalTo(XContentType.YAML)); - assertThat(ParsedMediaType.parseMediaType(XContentType.SMILE, Collections.emptyMap()) - .toMediaType(mediaTypeRegistry), equalTo(XContentType.SMILE)); - assertThat(ParsedMediaType.parseMediaType(XContentType.CBOR, Collections.emptyMap()) - .toMediaType(mediaTypeRegistry), equalTo(XContentType.CBOR)); - - assertThat(ParsedMediaType.parseMediaType(XContentType.VND_JSON, Map.of("compatible-with", "7")) - .toMediaType(mediaTypeRegistry), equalTo(XContentType.VND_JSON)); - assertThat(ParsedMediaType.parseMediaType(XContentType.VND_YAML, Map.of("compatible-with", "7")) - .toMediaType(mediaTypeRegistry), equalTo(XContentType.VND_YAML)); - assertThat(ParsedMediaType.parseMediaType(XContentType.VND_SMILE, Map.of("compatible-with", "7")) - .toMediaType(mediaTypeRegistry), equalTo(XContentType.VND_SMILE)); - assertThat(ParsedMediaType.parseMediaType(XContentType.VND_CBOR, Map.of("compatible-with", "7")) - .toMediaType(mediaTypeRegistry), equalTo(XContentType.VND_CBOR)); + assertThat( + ParsedMediaType.parseMediaType(XContentType.YAML, Collections.emptyMap()).toMediaType(mediaTypeRegistry), + equalTo(XContentType.YAML) + ); + assertThat( + ParsedMediaType.parseMediaType(XContentType.SMILE, Collections.emptyMap()).toMediaType(mediaTypeRegistry), + equalTo(XContentType.SMILE) + ); + assertThat( + ParsedMediaType.parseMediaType(XContentType.CBOR, Collections.emptyMap()).toMediaType(mediaTypeRegistry), + equalTo(XContentType.CBOR) + ); + + assertThat( + ParsedMediaType.parseMediaType(XContentType.VND_JSON, Map.of("compatible-with", "7")).toMediaType(mediaTypeRegistry), + equalTo(XContentType.VND_JSON) + ); + assertThat( + ParsedMediaType.parseMediaType(XContentType.VND_YAML, Map.of("compatible-with", "7")).toMediaType(mediaTypeRegistry), + equalTo(XContentType.VND_YAML) + ); + assertThat( + ParsedMediaType.parseMediaType(XContentType.VND_SMILE, Map.of("compatible-with", "7")).toMediaType(mediaTypeRegistry), + equalTo(XContentType.VND_SMILE) + ); + assertThat( + ParsedMediaType.parseMediaType(XContentType.VND_CBOR, Map.of("compatible-with", "7")).toMediaType(mediaTypeRegistry), + equalTo(XContentType.VND_CBOR) + ); } public void testResponseContentTypeHeader() { - assertThat(ParsedMediaType.parseMediaType(XContentType.JSON, Collections.emptyMap()) - .responseContentTypeHeader(), equalTo("application/json")); - assertThat(ParsedMediaType.parseMediaType(XContentType.YAML, Collections.emptyMap()) - .responseContentTypeHeader(), equalTo("application/yaml")); - assertThat(ParsedMediaType.parseMediaType(XContentType.SMILE, Collections.emptyMap()) - .responseContentTypeHeader(), equalTo("application/smile")); - assertThat(ParsedMediaType.parseMediaType(XContentType.CBOR, Collections.emptyMap()) - .responseContentTypeHeader(), equalTo("application/cbor")); - - assertThat(ParsedMediaType.parseMediaType(XContentType.VND_JSON, Map.of("compatible-with", "7")) - .responseContentTypeHeader(), equalTo("application/vnd.elasticsearch+json;compatible-with=7")); - assertThat(ParsedMediaType.parseMediaType(XContentType.VND_YAML, Map.of("compatible-with", "7")) - .responseContentTypeHeader(), equalTo("application/vnd.elasticsearch+yaml;compatible-with=7")); - assertThat(ParsedMediaType.parseMediaType(XContentType.VND_SMILE, Map.of("compatible-with", "7")) - .responseContentTypeHeader(), equalTo("application/vnd.elasticsearch+smile;compatible-with=7")); - assertThat(ParsedMediaType.parseMediaType(XContentType.VND_CBOR, Map.of("compatible-with", "7")) - .responseContentTypeHeader(), equalTo("application/vnd.elasticsearch+cbor;compatible-with=7")); - - assertThat(ParsedMediaType.parseMediaType(XContentType.JSON, Map.of("charset", "utf-8")) - .responseContentTypeHeader(), equalTo("application/json;charset=utf-8")); - assertThat(ParsedMediaType.parseMediaType(XContentType.JSON, Map.of("charset", "UTF-8")) - .responseContentTypeHeader(), equalTo("application/json;charset=UTF-8")); + assertThat( + ParsedMediaType.parseMediaType(XContentType.JSON, Collections.emptyMap()).responseContentTypeHeader(), + equalTo("application/json") + ); + assertThat( + ParsedMediaType.parseMediaType(XContentType.YAML, Collections.emptyMap()).responseContentTypeHeader(), + equalTo("application/yaml") + ); + assertThat( + ParsedMediaType.parseMediaType(XContentType.SMILE, Collections.emptyMap()).responseContentTypeHeader(), + equalTo("application/smile") + ); + assertThat( + ParsedMediaType.parseMediaType(XContentType.CBOR, Collections.emptyMap()).responseContentTypeHeader(), + equalTo("application/cbor") + ); + + assertThat( + ParsedMediaType.parseMediaType(XContentType.VND_JSON, Map.of("compatible-with", "7")).responseContentTypeHeader(), + equalTo("application/vnd.elasticsearch+json;compatible-with=7") + ); + assertThat( + ParsedMediaType.parseMediaType(XContentType.VND_YAML, Map.of("compatible-with", "7")).responseContentTypeHeader(), + equalTo("application/vnd.elasticsearch+yaml;compatible-with=7") + ); + assertThat( + ParsedMediaType.parseMediaType(XContentType.VND_SMILE, Map.of("compatible-with", "7")).responseContentTypeHeader(), + equalTo("application/vnd.elasticsearch+smile;compatible-with=7") + ); + assertThat( + ParsedMediaType.parseMediaType(XContentType.VND_CBOR, Map.of("compatible-with", "7")).responseContentTypeHeader(), + equalTo("application/vnd.elasticsearch+cbor;compatible-with=7") + ); + + assertThat( + ParsedMediaType.parseMediaType(XContentType.JSON, Map.of("charset", "utf-8")).responseContentTypeHeader(), + equalTo("application/json;charset=utf-8") + ); + assertThat( + ParsedMediaType.parseMediaType(XContentType.JSON, Map.of("charset", "UTF-8")).responseContentTypeHeader(), + equalTo("application/json;charset=UTF-8") + ); } } diff --git a/libs/x-content/src/test/java/org/elasticsearch/xcontent/SimpleStruct.java b/libs/x-content/src/test/java/org/elasticsearch/xcontent/SimpleStruct.java index f3a648119f60c..afba4514bda81 100644 --- a/libs/x-content/src/test/java/org/elasticsearch/xcontent/SimpleStruct.java +++ b/libs/x-content/src/test/java/org/elasticsearch/xcontent/SimpleStruct.java @@ -30,9 +30,11 @@ static SimpleStruct fromXContent(XContentParser parser) { private static final ParseField S = new ParseField("s"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>( - "simple_struct", true, args -> new SimpleStruct((int) args[0], (double) args[1], (String) args[2])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "simple_struct", + true, + args -> new SimpleStruct((int) args[0], (double) args[1], (String) args[2]) + ); static { PARSER.declareInt(constructorArg(), I); @@ -52,8 +54,7 @@ static SimpleStruct fromXContent(XContentParser parser) { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder - .startObject() + return builder.startObject() .field(I.getPreferredName(), i) .field(D.getPreferredName(), d) .field(S.getPreferredName(), s) @@ -78,4 +79,3 @@ public String toString() { return Strings.toString(this); } } - diff --git a/libs/x-content/src/test/java/org/elasticsearch/xcontent/XContentParserTests.java b/libs/x-content/src/test/java/org/elasticsearch/xcontent/XContentParserTests.java index 1d18aa9a7bdb8..5a7ba68914086 100644 --- a/libs/x-content/src/test/java/org/elasticsearch/xcontent/XContentParserTests.java +++ b/libs/x-content/src/test/java/org/elasticsearch/xcontent/XContentParserTests.java @@ -9,11 +9,12 @@ package org.elasticsearch.xcontent; import com.fasterxml.jackson.core.JsonParseException; + import org.elasticsearch.common.CheckedSupplier; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.json.JsonXContent; import java.io.IOException; import java.util.Arrays; @@ -141,8 +142,8 @@ public void testReadMapStrings() throws IOException { } public void testMap() throws IOException { - String source = "{\"i\": {\"_doc\": {\"f1\": {\"type\": \"text\", \"analyzer\": \"english\"}, " + - "\"f2\": {\"type\": \"object\", \"properties\": {\"sub1\": {\"type\": \"keyword\", \"foo\": 17}}}}}}"; + String source = "{\"i\": {\"_doc\": {\"f1\": {\"type\": \"text\", \"analyzer\": \"english\"}, " + + "\"f2\": {\"type\": \"object\", \"properties\": {\"sub1\": {\"type\": \"keyword\", \"foo\": 17}}}}}}"; Map f1 = new HashMap<>(); f1.put("type", "text"); f1.put("analyzer", "english"); @@ -249,9 +250,7 @@ public void testReadBooleans() throws IOException { } public void testEmptyList() throws IOException { - XContentBuilder builder = XContentFactory.jsonBuilder().startObject() - .startArray("some_array") - .endArray().endObject(); + XContentBuilder builder = XContentFactory.jsonBuilder().startObject().startArray("some_array").endArray().endObject(); try (XContentParser parser = createParser(JsonXContent.jsonXContent, Strings.toString(builder))) { assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken()); @@ -266,12 +265,14 @@ public void testEmptyList() throws IOException { } public void testSimpleList() throws IOException { - XContentBuilder builder = XContentFactory.jsonBuilder().startObject() - .startArray("some_array") - .value(1) - .value(3) - .value(0) - .endArray().endObject(); + XContentBuilder builder = XContentFactory.jsonBuilder() + .startObject() + .startArray("some_array") + .value(1) + .value(3) + .value(0) + .endArray() + .endObject(); try (XContentParser parser = createParser(JsonXContent.jsonXContent, Strings.toString(builder))) { assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken()); @@ -286,12 +287,20 @@ public void testSimpleList() throws IOException { } public void testNestedList() throws IOException { - XContentBuilder builder = XContentFactory.jsonBuilder().startObject() - .startArray("some_array") - .startArray().endArray() - .startArray().value(1).value(3).endArray() - .startArray().value(2).endArray() - .endArray().endObject(); + XContentBuilder builder = XContentFactory.jsonBuilder() + .startObject() + .startArray("some_array") + .startArray() + .endArray() + .startArray() + .value(1) + .value(3) + .endArray() + .startArray() + .value(2) + .endArray() + .endArray() + .endObject(); try (XContentParser parser = createParser(JsonXContent.jsonXContent, Strings.toString(builder))) { assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken()); @@ -301,18 +310,21 @@ public void testNestedList() throws IOException { // sometimes read the start array token, sometimes not assertEquals(XContentParser.Token.START_ARRAY, parser.nextToken()); } - assertEquals( - Arrays.asList(Collections.emptyList(), Arrays.asList(1, 3), Arrays.asList(2)), - parser.list()); + assertEquals(Arrays.asList(Collections.emptyList(), Arrays.asList(1, 3), Arrays.asList(2)), parser.list()); } } public void testNestedMapInList() throws IOException { - XContentBuilder builder = XContentFactory.jsonBuilder().startObject() - .startArray("some_array") - .startObject().field("foo", "bar").endObject() - .startObject().endObject() - .endArray().endObject(); + XContentBuilder builder = XContentFactory.jsonBuilder() + .startObject() + .startArray("some_array") + .startObject() + .field("foo", "bar") + .endObject() + .startObject() + .endObject() + .endArray() + .endObject(); try (XContentParser parser = createParser(JsonXContent.jsonXContent, Strings.toString(builder))) { assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken()); @@ -322,18 +334,16 @@ public void testNestedMapInList() throws IOException { // sometimes read the start array token, sometimes not assertEquals(XContentParser.Token.START_ARRAY, parser.nextToken()); } - assertEquals( - Arrays.asList(singletonMap("foo", "bar"), emptyMap()), - parser.list()); + assertEquals(Arrays.asList(singletonMap("foo", "bar"), emptyMap()), parser.list()); } } public void testGenericMap() throws IOException { - String content = "{" + - "\"c\": { \"i\": 3, \"d\": 0.3, \"s\": \"ccc\" }, " + - "\"a\": { \"i\": 1, \"d\": 0.1, \"s\": \"aaa\" }, " + - "\"b\": { \"i\": 2, \"d\": 0.2, \"s\": \"bbb\" }" + - "}"; + String content = "{" + + "\"c\": { \"i\": 3, \"d\": 0.3, \"s\": \"ccc\" }, " + + "\"a\": { \"i\": 1, \"d\": 0.1, \"s\": \"aaa\" }, " + + "\"b\": { \"i\": 2, \"d\": 0.2, \"s\": \"bbb\" }" + + "}"; SimpleStruct structA = new SimpleStruct(1, 0.1, "aaa"); SimpleStruct structB = new SimpleStruct(2, 0.2, "bbb"); SimpleStruct structC = new SimpleStruct(3, 0.3, "ccc"); @@ -347,11 +357,11 @@ public void testGenericMap() throws IOException { } public void testGenericMapOrdered() throws IOException { - String content = "{" + - "\"c\": { \"i\": 3, \"d\": 0.3, \"s\": \"ccc\" }, " + - "\"a\": { \"i\": 1, \"d\": 0.1, \"s\": \"aaa\" }, " + - "\"b\": { \"i\": 2, \"d\": 0.2, \"s\": \"bbb\" }" + - "}"; + String content = "{" + + "\"c\": { \"i\": 3, \"d\": 0.3, \"s\": \"ccc\" }, " + + "\"a\": { \"i\": 1, \"d\": 0.1, \"s\": \"aaa\" }, " + + "\"b\": { \"i\": 2, \"d\": 0.2, \"s\": \"bbb\" }" + + "}"; SimpleStruct structA = new SimpleStruct(1, 0.1, "aaa"); SimpleStruct structB = new SimpleStruct(2, 0.2, "bbb"); SimpleStruct structC = new SimpleStruct(3, 0.3, "ccc"); @@ -366,15 +376,16 @@ public void testGenericMapOrdered() throws IOException { } public void testGenericMap_Failure_MapContainingUnparsableValue() throws IOException { - String content = "{" + - "\"a\": { \"i\": 1, \"d\": 0.1, \"s\": \"aaa\" }, " + - "\"b\": { \"i\": 2, \"d\": 0.2, \"s\": 666 }, " + - "\"c\": { \"i\": 3, \"d\": 0.3, \"s\": \"ccc\" }" + - "}"; + String content = "{" + + "\"a\": { \"i\": 1, \"d\": 0.1, \"s\": \"aaa\" }, " + + "\"b\": { \"i\": 2, \"d\": 0.2, \"s\": 666 }, " + + "\"c\": { \"i\": 3, \"d\": 0.3, \"s\": \"ccc\" }" + + "}"; try (XContentParser parser = createParser(JsonXContent.jsonXContent, content)) { XContentParseException exception = expectThrows( XContentParseException.class, - () -> parser.map(HashMap::new, SimpleStruct::fromXContent)); + () -> parser.map(HashMap::new, SimpleStruct::fromXContent) + ); assertThat(exception, hasMessage(containsString("s doesn't support values of type: VALUE_NUMBER"))); } } @@ -405,7 +416,7 @@ public void testSubParserObject() throws IOException { subParser.skipChildren(); } - } finally { + } finally { assertFalse(subParser.isClosed()); subParser.close(); assertTrue(subParser.isClosed()); @@ -450,7 +461,7 @@ public void testSubParserArray() throws IOException { subParser.skipChildren(); } - } finally { + } finally { assertFalse(subParser.isClosed()); subParser.close(); assertTrue(subParser.isClosed()); @@ -475,7 +486,6 @@ public void testCreateSubParserAtAWrongPlace() throws IOException { } } - public void testCreateRootSubParser() throws IOException { XContentBuilder builder = XContentFactory.jsonBuilder(); int numberOfTokens = generateRandomObjectForMarking(builder); @@ -501,9 +511,7 @@ public void testCreateRootSubParser() throws IOException { * Returns the number of tokens in the marked field */ private static int generateRandomObjectForMarking(XContentBuilder builder) throws IOException { - builder.startObject() - .field("first_field", "foo") - .field("marked_field"); + builder.startObject().field("first_field", "foo").field("marked_field"); int numberOfTokens = generateRandomObject(builder, 0); builder.field("last_field", "bar").endObject(); return numberOfTokens; @@ -522,37 +530,32 @@ public static int generateRandomObject(XContentBuilder builder, int level) throw } private static int generateRandomValue(XContentBuilder builder, int level) throws IOException { - @SuppressWarnings("unchecked") CheckedSupplier fieldGenerator = randomFrom( - () -> { - builder.value(randomInt()); - return 1; - }, - () -> { - builder.value(randomAlphaOfLength(10)); + @SuppressWarnings("unchecked") + CheckedSupplier fieldGenerator = randomFrom(() -> { + builder.value(randomInt()); + return 1; + }, () -> { + builder.value(randomAlphaOfLength(10)); + return 1; + }, () -> { + builder.value(randomDouble()); + return 1; + }, () -> { + if (level < 3) { + // don't need to go too deep + return generateRandomObject(builder, level + 1); + } else { + builder.value(0); return 1; - }, - () -> { - builder.value(randomDouble()); + } + }, () -> { + if (level < 5) { // don't need to go too deep + return generateRandomArray(builder, level); + } else { + builder.value(0); return 1; - }, - () -> { - if (level < 3) { - // don't need to go too deep - return generateRandomObject(builder, level + 1); - } else { - builder.value(0); - return 1; - } - }, - () -> { - if (level < 5) { // don't need to go too deep - return generateRandomArray(builder, level); - } else { - builder.value(0); - return 1; - } } - ); + }); return fieldGenerator.get(); } diff --git a/libs/x-content/src/test/java/org/elasticsearch/xcontent/support/filtering/AbstractXContentFilteringTestCase.java b/libs/x-content/src/test/java/org/elasticsearch/xcontent/support/filtering/AbstractXContentFilteringTestCase.java index af7e9aae149d8..de64e7cc07cb9 100644 --- a/libs/x-content/src/test/java/org/elasticsearch/xcontent/support/filtering/AbstractXContentFilteringTestCase.java +++ b/libs/x-content/src/test/java/org/elasticsearch/xcontent/support/filtering/AbstractXContentFilteringTestCase.java @@ -115,12 +115,16 @@ static void assertXContentBuilderAsString(final XContentBuilder expected, final static void assertXContentBuilderAsBytes(final XContentBuilder expected, final XContentBuilder actual) { XContent xContent = XContentFactory.xContent(actual.contentType()); try ( - XContentParser jsonParser = - xContent.createParser(NamedXContentRegistry.EMPTY, - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, BytesReference.bytes(expected).streamInput()); - XContentParser testParser = - xContent.createParser(NamedXContentRegistry.EMPTY, - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, BytesReference.bytes(actual).streamInput()); + XContentParser jsonParser = xContent.createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + BytesReference.bytes(expected).streamInput() + ); + XContentParser testParser = xContent.createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + BytesReference.bytes(actual).streamInput() + ); ) { while (true) { XContentParser.Token token1 = jsonParser.nextToken(); diff --git a/libs/x-content/src/test/java/org/elasticsearch/xcontent/support/filtering/FilterPathGeneratorFilteringTests.java b/libs/x-content/src/test/java/org/elasticsearch/xcontent/support/filtering/FilterPathGeneratorFilteringTests.java index ad669a3e61b5d..a3cff5ad7e226 100644 --- a/libs/x-content/src/test/java/org/elasticsearch/xcontent/support/filtering/FilterPathGeneratorFilteringTests.java +++ b/libs/x-content/src/test/java/org/elasticsearch/xcontent/support/filtering/FilterPathGeneratorFilteringTests.java @@ -24,8 +24,8 @@ public class FilterPathGeneratorFilteringTests extends ESTestCase { private final JsonFactory JSON_FACTORY = new JsonFactory(); public void testInclusiveFilters() throws Exception { - final String SAMPLE = "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + - "{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"; + final String SAMPLE = "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + + "{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"; assertResult(SAMPLE, "a", true, "{'a':0}"); assertResult(SAMPLE, "b", true, "{'b':true}"); @@ -70,80 +70,213 @@ public void testInclusiveFilters() throws Exception { } public void testExclusiveFilters() throws Exception { - final String SAMPLE = "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + - "{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"; - - assertResult(SAMPLE, "a", false, "{'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + - "{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, "b", false, "{'a':0,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + - "{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, "c", false, "{'a':0,'b':true,'d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + - "{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, "d", false, "{'a':0,'b':true,'c':'c_value','e':[{'f1':'f1_value','f2':'f2_value'}," + - "{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + final String SAMPLE = "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + + "{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"; + + assertResult( + SAMPLE, + "a", + false, + "{'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + + "{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}" + ); + assertResult( + SAMPLE, + "b", + false, + "{'a':0,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + + "{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}" + ); + assertResult( + SAMPLE, + "c", + false, + "{'a':0,'b':true,'d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + + "{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}" + ); + assertResult( + SAMPLE, + "d", + false, + "{'a':0,'b':true,'c':'c_value','e':[{'f1':'f1_value','f2':'f2_value'}," + + "{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}" + ); assertResult(SAMPLE, "e", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, "h", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + - "{'g1':'g1_value','g2':'g2_value'}]}"); - assertResult(SAMPLE, "z", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + - "{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - - assertResult(SAMPLE, "e.f1", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f2':'f2_value'}," + - "{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, "e.f2", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value'}," + - "{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, "e.f*", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'g1':'g1_value','g2':'g2_value'}]," + - "'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, "e.*2", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value'},{'g1':'g1_value'}]," + - "'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - - assertResult(SAMPLE, "h.i", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + - "{'g1':'g1_value','g2':'g2_value'}]}"); - assertResult(SAMPLE, "h.i.j", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + - "{'g1':'g1_value','g2':'g2_value'}]}"); - assertResult(SAMPLE, "h.i.j.k", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + - "{'g1':'g1_value','g2':'g2_value'}]}"); - assertResult(SAMPLE, "h.i.j.k.l", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + - "{'g1':'g1_value','g2':'g2_value'}]}"); - - assertResult(SAMPLE, "h.*", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + - "{'g1':'g1_value','g2':'g2_value'}]}"); - assertResult(SAMPLE, "*.i", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + - "{'g1':'g1_value','g2':'g2_value'}]}"); - - assertResult(SAMPLE, "*.i.j", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + - "{'g1':'g1_value','g2':'g2_value'}]}"); - assertResult(SAMPLE, "h.*.j", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + - "{'g1':'g1_value','g2':'g2_value'}]}"); - assertResult(SAMPLE, "h.i.*", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + - "{'g1':'g1_value','g2':'g2_value'}]}"); - - assertResult(SAMPLE, "*.i.j.k", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + - "{'g1':'g1_value','g2':'g2_value'}]}"); - assertResult(SAMPLE, "h.*.j.k", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + - "{'g1':'g1_value','g2':'g2_value'}]}"); - assertResult(SAMPLE, "h.i.*.k", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + - "{'g1':'g1_value','g2':'g2_value'}]}"); - assertResult(SAMPLE, "h.i.j.*", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + - "{'g1':'g1_value','g2':'g2_value'}]}"); - - assertResult(SAMPLE, "*.i.j.k.l", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + - "{'g1':'g1_value','g2':'g2_value'}]}"); - assertResult(SAMPLE, "h.*.j.k.l", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + - "{'g1':'g1_value','g2':'g2_value'}]}"); - assertResult(SAMPLE, "h.i.*.k.l", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + - "{'g1':'g1_value','g2':'g2_value'}]}"); - assertResult(SAMPLE, "h.i.j.*.l", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + - "{'g1':'g1_value','g2':'g2_value'}]}"); - assertResult(SAMPLE, "h.i.j.k.*", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + - "{'g1':'g1_value','g2':'g2_value'}]}"); - - assertResult(SAMPLE, "h.*.j.*.l", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + - "{'g1':'g1_value','g2':'g2_value'}]}"); - assertResult(SAMPLE, "**.l", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + - "{'g1':'g1_value','g2':'g2_value'}]}"); - - assertResult(SAMPLE, "**.*2", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value'}," + - "{'g1':'g1_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult( + SAMPLE, + "h", + false, + "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + "{'g1':'g1_value','g2':'g2_value'}]}" + ); + assertResult( + SAMPLE, + "z", + false, + "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + + "{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}" + ); + + assertResult( + SAMPLE, + "e.f1", + false, + "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f2':'f2_value'}," + + "{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}" + ); + assertResult( + SAMPLE, + "e.f2", + false, + "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value'}," + + "{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}" + ); + assertResult( + SAMPLE, + "e.f*", + false, + "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'g1':'g1_value','g2':'g2_value'}]," + "'h':{'i':{'j':{'k':{'l':'l_value'}}}}}" + ); + assertResult( + SAMPLE, + "e.*2", + false, + "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value'},{'g1':'g1_value'}]," + + "'h':{'i':{'j':{'k':{'l':'l_value'}}}}}" + ); + + assertResult( + SAMPLE, + "h.i", + false, + "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + "{'g1':'g1_value','g2':'g2_value'}]}" + ); + assertResult( + SAMPLE, + "h.i.j", + false, + "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + "{'g1':'g1_value','g2':'g2_value'}]}" + ); + assertResult( + SAMPLE, + "h.i.j.k", + false, + "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + "{'g1':'g1_value','g2':'g2_value'}]}" + ); + assertResult( + SAMPLE, + "h.i.j.k.l", + false, + "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + "{'g1':'g1_value','g2':'g2_value'}]}" + ); + + assertResult( + SAMPLE, + "h.*", + false, + "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + "{'g1':'g1_value','g2':'g2_value'}]}" + ); + assertResult( + SAMPLE, + "*.i", + false, + "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + "{'g1':'g1_value','g2':'g2_value'}]}" + ); + + assertResult( + SAMPLE, + "*.i.j", + false, + "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + "{'g1':'g1_value','g2':'g2_value'}]}" + ); + assertResult( + SAMPLE, + "h.*.j", + false, + "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + "{'g1':'g1_value','g2':'g2_value'}]}" + ); + assertResult( + SAMPLE, + "h.i.*", + false, + "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + "{'g1':'g1_value','g2':'g2_value'}]}" + ); + + assertResult( + SAMPLE, + "*.i.j.k", + false, + "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + "{'g1':'g1_value','g2':'g2_value'}]}" + ); + assertResult( + SAMPLE, + "h.*.j.k", + false, + "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + "{'g1':'g1_value','g2':'g2_value'}]}" + ); + assertResult( + SAMPLE, + "h.i.*.k", + false, + "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + "{'g1':'g1_value','g2':'g2_value'}]}" + ); + assertResult( + SAMPLE, + "h.i.j.*", + false, + "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + "{'g1':'g1_value','g2':'g2_value'}]}" + ); + + assertResult( + SAMPLE, + "*.i.j.k.l", + false, + "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + "{'g1':'g1_value','g2':'g2_value'}]}" + ); + assertResult( + SAMPLE, + "h.*.j.k.l", + false, + "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + "{'g1':'g1_value','g2':'g2_value'}]}" + ); + assertResult( + SAMPLE, + "h.i.*.k.l", + false, + "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + "{'g1':'g1_value','g2':'g2_value'}]}" + ); + assertResult( + SAMPLE, + "h.i.j.*.l", + false, + "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + "{'g1':'g1_value','g2':'g2_value'}]}" + ); + assertResult( + SAMPLE, + "h.i.j.k.*", + false, + "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + "{'g1':'g1_value','g2':'g2_value'}]}" + ); + + assertResult( + SAMPLE, + "h.*.j.*.l", + false, + "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + "{'g1':'g1_value','g2':'g2_value'}]}" + ); + assertResult( + SAMPLE, + "**.l", + false, + "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + "{'g1':'g1_value','g2':'g2_value'}]}" + ); + + assertResult( + SAMPLE, + "**.*2", + false, + "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value'}," + + "{'g1':'g1_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}" + ); } @@ -159,8 +292,14 @@ public void testExclusiveFiltersWithDots() throws Exception { private void assertResult(String input, String filter, boolean inclusive, String expected) throws Exception { try (BytesStreamOutput os = new BytesStreamOutput()) { - try (FilteringGeneratorDelegate generator = new FilteringGeneratorDelegate(JSON_FACTORY.createGenerator(os), - new FilterPathBasedFilter(Collections.singleton(filter), inclusive), true, true)) { + try ( + FilteringGeneratorDelegate generator = new FilteringGeneratorDelegate( + JSON_FACTORY.createGenerator(os), + new FilterPathBasedFilter(Collections.singleton(filter), inclusive), + true, + true + ) + ) { try (JsonParser parser = JSON_FACTORY.createParser(replaceQuotes(input))) { while (parser.nextToken() != null) { generator.copyCurrentStructure(parser); diff --git a/libs/x-content/src/test/java/org/elasticsearch/xcontent/support/filtering/FilterPathTests.java b/libs/x-content/src/test/java/org/elasticsearch/xcontent/support/filtering/FilterPathTests.java index c8a65e90a4c3a..a3a29481ff5ee 100644 --- a/libs/x-content/src/test/java/org/elasticsearch/xcontent/support/filtering/FilterPathTests.java +++ b/libs/x-content/src/test/java/org/elasticsearch/xcontent/support/filtering/FilterPathTests.java @@ -158,7 +158,6 @@ public void testFilterPathWithEscapedDots() { assertThat(filterPath.getSegment(), is(emptyString())); assertSame(filterPath, FilterPath.EMPTY); - input = "w\\.0.0\\.t"; filterPaths = FilterPath.compile(singleton(input)); diff --git a/modules/analysis-common/src/internalClusterTest/java/org/elasticsearch/analysis/common/QueryStringWithAnalyzersIT.java b/modules/analysis-common/src/internalClusterTest/java/org/elasticsearch/analysis/common/QueryStringWithAnalyzersIT.java index e474a7ce698c0..d3514694a6002 100644 --- a/modules/analysis-common/src/internalClusterTest/java/org/elasticsearch/analysis/common/QueryStringWithAnalyzersIT.java +++ b/modules/analysis-common/src/internalClusterTest/java/org/elasticsearch/analysis/common/QueryStringWithAnalyzersIT.java @@ -31,8 +31,12 @@ protected Collection> nodePlugins() { * Validates that we properly split fields using the word delimiter filter in query_string. */ public void testCustomWordDelimiterQueryString() { - assertAcked(client().admin().indices().prepareCreate("test") - .setSettings(Settings.builder() + assertAcked( + client().admin() + .indices() + .prepareCreate("test") + .setSettings( + Settings.builder() .put("analysis.analyzer.my_analyzer.type", "custom") .put("analysis.analyzer.my_analyzer.tokenizer", "whitespace") .put("analysis.analyzer.my_analyzer.filter", "custom_word_delimiter") @@ -43,21 +47,17 @@ public void testCustomWordDelimiterQueryString() { .put("analysis.filter.custom_word_delimiter.catenate_words", "false") .put("analysis.filter.custom_word_delimiter.split_on_case_change", "false") .put("analysis.filter.custom_word_delimiter.split_on_numerics", "false") - .put("analysis.filter.custom_word_delimiter.stem_english_possessive", "false")) - .setMapping( - "field1", "type=text,analyzer=my_analyzer", - "field2", "type=text,analyzer=my_analyzer")); - - client().prepareIndex("test").setId("1").setSource( - "field1", "foo bar baz", - "field2", "not needed").get(); + .put("analysis.filter.custom_word_delimiter.stem_english_possessive", "false") + ) + .setMapping("field1", "type=text,analyzer=my_analyzer", "field2", "type=text,analyzer=my_analyzer") + ); + + client().prepareIndex("test").setId("1").setSource("field1", "foo bar baz", "field2", "not needed").get(); refresh(); - SearchResponse response = client() - .prepareSearch("test") - .setQuery( - queryStringQuery("foo.baz").defaultOperator(Operator.AND) - .field("field1").field("field2")).get(); + SearchResponse response = client().prepareSearch("test") + .setQuery(queryStringQuery("foo.baz").defaultOperator(Operator.AND).field("field1").field("field2")) + .get(); assertHitCount(response, 1L); } } diff --git a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/ASCIIFoldingTokenFilterFactory.java b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/ASCIIFoldingTokenFilterFactory.java index 2fafba4c8807b..1fff166eb53b5 100644 --- a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/ASCIIFoldingTokenFilterFactory.java +++ b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/ASCIIFoldingTokenFilterFactory.java @@ -10,27 +10,25 @@ import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.miscellaneous.ASCIIFoldingFilter; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.env.Environment; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.analysis.AbstractTokenFilterFactory; import org.elasticsearch.index.analysis.NormalizingTokenFilterFactory; import org.elasticsearch.index.analysis.TokenFilterFactory; +import org.elasticsearch.xcontent.ParseField; /** * Factory for ASCIIFoldingFilter. */ -public class ASCIIFoldingTokenFilterFactory extends AbstractTokenFilterFactory - implements NormalizingTokenFilterFactory { +public class ASCIIFoldingTokenFilterFactory extends AbstractTokenFilterFactory implements NormalizingTokenFilterFactory { public static final ParseField PRESERVE_ORIGINAL = new ParseField("preserve_original"); public static final boolean DEFAULT_PRESERVE_ORIGINAL = false; private final boolean preserveOriginal; - public ASCIIFoldingTokenFilterFactory(IndexSettings indexSettings, Environment environment, - String name, Settings settings) { + public ASCIIFoldingTokenFilterFactory(IndexSettings indexSettings, Environment environment, String name, Settings settings) { super(indexSettings, name, settings); preserveOriginal = settings.getAsBoolean(PRESERVE_ORIGINAL.getPreferredName(), DEFAULT_PRESERVE_ORIGINAL); } diff --git a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/AnalysisPainlessExtension.java b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/AnalysisPainlessExtension.java index e658f39a2c723..4bbe159820a56 100644 --- a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/AnalysisPainlessExtension.java +++ b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/AnalysisPainlessExtension.java @@ -19,8 +19,10 @@ public class AnalysisPainlessExtension implements PainlessExtension { - private static final Whitelist WHITELIST = - WhitelistLoader.loadFromResourceFiles(AnalysisPainlessExtension.class, "painless_whitelist.txt"); + private static final Whitelist WHITELIST = WhitelistLoader.loadFromResourceFiles( + AnalysisPainlessExtension.class, + "painless_whitelist.txt" + ); @Override public Map, List> getContextWhitelists() { diff --git a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/AnalysisPredicateScript.java b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/AnalysisPredicateScript.java index 8e5390ff9ea34..3ce6021bfb63d 100644 --- a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/AnalysisPredicateScript.java +++ b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/AnalysisPredicateScript.java @@ -100,7 +100,7 @@ public interface Factory { AnalysisPredicateScript newInstance(); } - public static final String[] PARAMETERS = new String[]{ "token" }; + public static final String[] PARAMETERS = new String[] { "token" }; public static final ScriptContext CONTEXT = new ScriptContext<>("analysis", Factory.class); } diff --git a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/BrazilianStemTokenFilterFactory.java b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/BrazilianStemTokenFilterFactory.java index 5b566bf62ec3b..bbd5bd3138e03 100644 --- a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/BrazilianStemTokenFilterFactory.java +++ b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/BrazilianStemTokenFilterFactory.java @@ -8,10 +8,10 @@ package org.elasticsearch.analysis.common; +import org.apache.lucene.analysis.CharArraySet; import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.br.BrazilianStemFilter; import org.apache.lucene.analysis.miscellaneous.SetKeywordMarkerFilter; -import org.apache.lucene.analysis.CharArraySet; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.env.Environment; import org.elasticsearch.index.IndexSettings; diff --git a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/CJKBigramFilterFactory.java b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/CJKBigramFilterFactory.java index a57bd4b6a3833..09e0767c054ef 100644 --- a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/CJKBigramFilterFactory.java +++ b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/CJKBigramFilterFactory.java @@ -10,12 +10,12 @@ import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.cjk.CJKBigramFilter; -import org.elasticsearch.lucene.analysis.miscellaneous.DisableGraphAttribute; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.env.Environment; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.analysis.AbstractTokenFilterFactory; import org.elasticsearch.index.analysis.TokenFilterFactory; +import org.elasticsearch.lucene.analysis.miscellaneous.DisableGraphAttribute; import java.util.Arrays; import java.util.HashSet; diff --git a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/CharGroupTokenizerFactory.java b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/CharGroupTokenizerFactory.java index 46803eb02ec31..a079bcb8add54 100644 --- a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/CharGroupTokenizerFactory.java +++ b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/CharGroupTokenizerFactory.java @@ -19,7 +19,7 @@ import java.util.HashSet; import java.util.Set; -public class CharGroupTokenizerFactory extends AbstractTokenizerFactory{ +public class CharGroupTokenizerFactory extends AbstractTokenizerFactory { static final String MAX_TOKEN_LENGTH = "max_token_length"; @@ -43,8 +43,7 @@ public CharGroupTokenizerFactory(IndexSettings indexSettings, Environment enviro if (c.length() == 1) { tokenizeOnChars.add((int) c.charAt(0)); - } - else if (c.charAt(0) == '\\') { + } else if (c.charAt(0) == '\\') { tokenizeOnChars.add((int) parseEscapedChar(c)); } else { switch (c) { @@ -74,8 +73,7 @@ private char parseEscapedChar(final String s) { int len = s.length(); char c = s.charAt(0); if (c == '\\') { - if (1 >= len) - throw new RuntimeException("Invalid escaped char in [" + s + "]"); + if (1 >= len) throw new RuntimeException("Invalid escaped char in [" + s + "]"); c = s.charAt(1); switch (c) { case '\\': diff --git a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/CharMatcher.java b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/CharMatcher.java index 71da658de9d2a..d334e142f0f75 100644 --- a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/CharMatcher.java +++ b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/CharMatcher.java @@ -57,16 +57,16 @@ public boolean isTokenChar(int c) { @Override public boolean isTokenChar(int c) { switch (Character.getType(c)) { - case Character.START_PUNCTUATION: - case Character.END_PUNCTUATION: - case Character.OTHER_PUNCTUATION: - case Character.CONNECTOR_PUNCTUATION: - case Character.DASH_PUNCTUATION: - case Character.INITIAL_QUOTE_PUNCTUATION: - case Character.FINAL_QUOTE_PUNCTUATION: - return true; - default: - return false; + case Character.START_PUNCTUATION: + case Character.END_PUNCTUATION: + case Character.OTHER_PUNCTUATION: + case Character.CONNECTOR_PUNCTUATION: + case Character.DASH_PUNCTUATION: + case Character.INITIAL_QUOTE_PUNCTUATION: + case Character.FINAL_QUOTE_PUNCTUATION: + return true; + default: + return false; } } }, @@ -74,13 +74,13 @@ public boolean isTokenChar(int c) { @Override public boolean isTokenChar(int c) { switch (Character.getType(c)) { - case Character.CURRENCY_SYMBOL: - case Character.MATH_SYMBOL: - case Character.OTHER_SYMBOL: - case Character.MODIFIER_SYMBOL: - return true; - default: - return false; + case Character.CURRENCY_SYMBOL: + case Character.MATH_SYMBOL: + case Character.OTHER_SYMBOL: + case Character.MODIFIER_SYMBOL: + return true; + default: + return false; } } } @@ -88,36 +88,39 @@ public boolean isTokenChar(int c) { final class Builder { private final Set matchers; + Builder() { matchers = new HashSet<>(); } + public Builder or(CharMatcher matcher) { matchers.add(matcher); return this; } + public CharMatcher build() { switch (matchers.size()) { - case 0: - return new CharMatcher() { - @Override - public boolean isTokenChar(int c) { - return false; - } - }; - case 1: - return matchers.iterator().next(); - default: - return new CharMatcher() { - @Override - public boolean isTokenChar(int c) { - for (CharMatcher matcher : matchers) { - if (matcher.isTokenChar(c)) { - return true; + case 0: + return new CharMatcher() { + @Override + public boolean isTokenChar(int c) { + return false; + } + }; + case 1: + return matchers.iterator().next(); + default: + return new CharMatcher() { + @Override + public boolean isTokenChar(int c) { + for (CharMatcher matcher : matchers) { + if (matcher.isTokenChar(c)) { + return true; + } } + return false; } - return false; - } - }; + }; } } } diff --git a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/CommonAnalysisPlugin.java b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/CommonAnalysisPlugin.java index 6896b4b94781b..bce6238023f2b 100644 --- a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/CommonAnalysisPlugin.java +++ b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/CommonAnalysisPlugin.java @@ -65,7 +65,6 @@ import org.apache.lucene.analysis.lt.LithuanianAnalyzer; import org.apache.lucene.analysis.lv.LatvianAnalyzer; import org.apache.lucene.analysis.miscellaneous.ASCIIFoldingFilter; -import org.elasticsearch.lucene.analysis.miscellaneous.DisableGraphAttribute; import org.apache.lucene.analysis.miscellaneous.KeywordRepeatFilter; import org.apache.lucene.analysis.miscellaneous.LengthFilter; import org.apache.lucene.analysis.miscellaneous.LimitTokenCountFilter; @@ -109,7 +108,6 @@ import org.elasticsearch.common.logging.DeprecationLogger; import org.elasticsearch.common.regex.Regex; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.env.Environment; import org.elasticsearch.env.NodeEnvironment; import org.elasticsearch.index.IndexSettings; @@ -123,6 +121,7 @@ import org.elasticsearch.index.analysis.TokenizerFactory; import org.elasticsearch.indices.analysis.AnalysisModule.AnalysisProvider; import org.elasticsearch.indices.analysis.PreBuiltCacheFactory.CachingStrategy; +import org.elasticsearch.lucene.analysis.miscellaneous.DisableGraphAttribute; import org.elasticsearch.plugins.AnalysisPlugin; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.plugins.ScriptPlugin; @@ -131,6 +130,7 @@ import org.elasticsearch.script.ScriptService; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.watcher.ResourceWatcherService; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.tartarus.snowball.ext.DutchStemmer; import org.tartarus.snowball.ext.FrenchStemmer; @@ -151,12 +151,19 @@ public class CommonAnalysisPlugin extends Plugin implements AnalysisPlugin, Scri private final SetOnce scriptService = new SetOnce<>(); @Override - public Collection createComponents(Client client, ClusterService clusterService, ThreadPool threadPool, - ResourceWatcherService resourceWatcherService, ScriptService scriptService, - NamedXContentRegistry xContentRegistry, Environment environment, - NodeEnvironment nodeEnvironment, NamedWriteableRegistry namedWriteableRegistry, - IndexNameExpressionResolver expressionResolver, - Supplier repositoriesServiceSupplier) { + public Collection createComponents( + Client client, + ClusterService clusterService, + ThreadPool threadPool, + ResourceWatcherService resourceWatcherService, + ScriptService scriptService, + NamedXContentRegistry xContentRegistry, + Environment environment, + NodeEnvironment nodeEnvironment, + NamedWriteableRegistry namedWriteableRegistry, + IndexNameExpressionResolver expressionResolver, + Supplier repositoriesServiceSupplier + ) { this.scriptService.set(scriptService); return Collections.emptyList(); } @@ -227,8 +234,10 @@ public Map> getTokenFilters() { filters.put("classic", ClassicFilterFactory::new); filters.put("czech_stem", CzechStemTokenFilterFactory::new); filters.put("common_grams", requiresAnalysisSettings(CommonGramsTokenFilterFactory::new)); - filters.put("condition", - requiresAnalysisSettings((i, e, n, s) -> new ScriptedConditionTokenFilterFactory(i, n, s, scriptService.get()))); + filters.put( + "condition", + requiresAnalysisSettings((i, e, n, s) -> new ScriptedConditionTokenFilterFactory(i, n, s, scriptService.get())) + ); filters.put("decimal_digit", DecimalDigitFilterFactory::new); filters.put("delimited_payload", DelimitedPayloadTokenFilterFactory::new); filters.put("dictionary_decompounder", requiresAnalysisSettings(DictionaryCompoundWordTokenFilterFactory::new)); @@ -240,12 +249,16 @@ public Map> getTokenFilters() { public TokenStream create(TokenStream tokenStream) { if (indexSettings.getIndexVersionCreated().onOrAfter(org.elasticsearch.Version.V_8_0_0)) { throw new IllegalArgumentException( - "The [edgeNGram] token filter name was deprecated in 6.4 and cannot be used in new indices. " - + "Please change the filter name to [edge_ngram] instead."); + "The [edgeNGram] token filter name was deprecated in 6.4 and cannot be used in new indices. " + + "Please change the filter name to [edge_ngram] instead." + ); } else { - deprecationLogger.critical(DeprecationCategory.ANALYSIS, "edgeNGram_deprecation", + deprecationLogger.critical( + DeprecationCategory.ANALYSIS, + "edgeNGram_deprecation", "The [edgeNGram] token filter name is deprecated and will be removed in a future version. " - + "Please change the filter name to [edge_ngram] instead."); + + "Please change the filter name to [edge_ngram] instead." + ); } return super.create(tokenStream); } @@ -277,12 +290,16 @@ public TokenStream create(TokenStream tokenStream) { public TokenStream create(TokenStream tokenStream) { if (indexSettings.getIndexVersionCreated().onOrAfter(org.elasticsearch.Version.V_8_0_0)) { throw new IllegalArgumentException( - "The [nGram] token filter name was deprecated in 6.4 and cannot be used in new indices. " - + "Please change the filter name to [ngram] instead."); + "The [nGram] token filter name was deprecated in 6.4 and cannot be used in new indices. " + + "Please change the filter name to [ngram] instead." + ); } else { - deprecationLogger.critical(DeprecationCategory.ANALYSIS, "nGram_deprecation", + deprecationLogger.critical( + DeprecationCategory.ANALYSIS, + "nGram_deprecation", "The [nGram] token filter name is deprecated and will be removed in a future version. " - + "Please change the filter name to [ngram] instead."); + + "Please change the filter name to [ngram] instead." + ); } return super.create(tokenStream); } @@ -293,8 +310,10 @@ public TokenStream create(TokenStream tokenStream) { filters.put("pattern_replace", requiresAnalysisSettings(PatternReplaceTokenFilterFactory::new)); filters.put("persian_normalization", PersianNormalizationFilterFactory::new); filters.put("porter_stem", PorterStemTokenFilterFactory::new); - filters.put("predicate_token_filter", - requiresAnalysisSettings((i, e, n, s) -> new PredicateTokenFilterScriptFactory(i, n, s, scriptService.get()))); + filters.put( + "predicate_token_filter", + requiresAnalysisSettings((i, e, n, s) -> new PredicateTokenFilterScriptFactory(i, n, s, scriptService.get())) + ); filters.put("remove_duplicates", RemoveDuplicatesTokenFilterFactory::new); filters.put("reverse", ReverseTokenFilterFactory::new); filters.put("russian_stem", RussianStemTokenFilterFactory::new); @@ -333,24 +352,34 @@ public Map> getTokenizers() { tokenizers.put("thai", ThaiTokenizerFactory::new); tokenizers.put("nGram", (IndexSettings indexSettings, Environment environment, String name, Settings settings) -> { if (indexSettings.getIndexVersionCreated().onOrAfter(org.elasticsearch.Version.V_8_0_0)) { - throw new IllegalArgumentException("The [nGram] tokenizer name was deprecated in 7.6. " - + "Please use the tokenizer name to [ngram] for indices created in versions 8 or higher instead."); + throw new IllegalArgumentException( + "The [nGram] tokenizer name was deprecated in 7.6. " + + "Please use the tokenizer name to [ngram] for indices created in versions 8 or higher instead." + ); } else if (indexSettings.getIndexVersionCreated().onOrAfter(org.elasticsearch.Version.V_7_6_0)) { - deprecationLogger.critical(DeprecationCategory.ANALYSIS, "nGram_tokenizer_deprecation", + deprecationLogger.critical( + DeprecationCategory.ANALYSIS, + "nGram_tokenizer_deprecation", "The [nGram] tokenizer name is deprecated and will be removed in a future version. " - + "Please change the tokenizer name to [ngram] instead."); + + "Please change the tokenizer name to [ngram] instead." + ); } return new NGramTokenizerFactory(indexSettings, environment, name, settings); }); tokenizers.put("ngram", NGramTokenizerFactory::new); tokenizers.put("edgeNGram", (IndexSettings indexSettings, Environment environment, String name, Settings settings) -> { if (indexSettings.getIndexVersionCreated().onOrAfter(org.elasticsearch.Version.V_8_0_0)) { - throw new IllegalArgumentException("The [edgeNGram] tokenizer name was deprecated in 7.6. " - + "Please use the tokenizer name to [edge_nGram] for indices created in versions 8 or higher instead."); + throw new IllegalArgumentException( + "The [edgeNGram] tokenizer name was deprecated in 7.6. " + + "Please use the tokenizer name to [edge_nGram] for indices created in versions 8 or higher instead." + ); } else if (indexSettings.getIndexVersionCreated().onOrAfter(org.elasticsearch.Version.V_7_6_0)) { - deprecationLogger.critical(DeprecationCategory.ANALYSIS, "edgeNGram_tokenizer_deprecation", + deprecationLogger.critical( + DeprecationCategory.ANALYSIS, + "edgeNGram_tokenizer_deprecation", "The [edgeNGram] tokenizer name is deprecated and will be removed in a future version. " - + "Please change the tokenizer name to [edge_ngram] instead."); + + "Please change the tokenizer name to [edge_ngram] instead." + ); } return new EdgeNGramTokenizerFactory(indexSettings, environment, name, settings); }); @@ -372,11 +401,20 @@ public Map> getTokenizers() { @Override public List getPreBuiltAnalyzerProviderFactories() { List analyzers = new ArrayList<>(); - analyzers.add(new PreBuiltAnalyzerProviderFactory("pattern", CachingStrategy.ELASTICSEARCH, - () -> new PatternAnalyzer(Regex.compile("\\W+" /*PatternAnalyzer.NON_WORD_PATTERN*/, null), true, - CharArraySet.EMPTY_SET))); - analyzers.add(new PreBuiltAnalyzerProviderFactory("snowball", CachingStrategy.LUCENE, - () -> new SnowballAnalyzer("English", EnglishAnalyzer.ENGLISH_STOP_WORDS_SET))); + analyzers.add( + new PreBuiltAnalyzerProviderFactory( + "pattern", + CachingStrategy.ELASTICSEARCH, + () -> new PatternAnalyzer(Regex.compile("\\W+" /*PatternAnalyzer.NON_WORD_PATTERN*/, null), true, CharArraySet.EMPTY_SET) + ) + ); + analyzers.add( + new PreBuiltAnalyzerProviderFactory( + "snowball", + CachingStrategy.LUCENE, + () -> new SnowballAnalyzer("English", EnglishAnalyzer.ENGLISH_STOP_WORDS_SET) + ) + ); // Language analyzers: analyzers.add(new PreBuiltAnalyzerProviderFactory("arabic", CachingStrategy.LUCENE, ArabicAnalyzer::new)); @@ -387,8 +425,13 @@ public List getPreBuiltAnalyzerProviderFactorie analyzers.add(new PreBuiltAnalyzerProviderFactory("bulgarian", CachingStrategy.LUCENE, BulgarianAnalyzer::new)); analyzers.add(new PreBuiltAnalyzerProviderFactory("catalan", CachingStrategy.LUCENE, CatalanAnalyzer::new)); // chinese analyzer: only for old indices, best effort - analyzers.add(new PreBuiltAnalyzerProviderFactory("chinese", CachingStrategy.ONE, - () -> new StandardAnalyzer(EnglishAnalyzer.ENGLISH_STOP_WORDS_SET))); + analyzers.add( + new PreBuiltAnalyzerProviderFactory( + "chinese", + CachingStrategy.ONE, + () -> new StandardAnalyzer(EnglishAnalyzer.ENGLISH_STOP_WORDS_SET) + ) + ); analyzers.add(new PreBuiltAnalyzerProviderFactory("cjk", CachingStrategy.LUCENE, CJKAnalyzer::new)); analyzers.add(new PreBuiltAnalyzerProviderFactory("czech", CachingStrategy.LUCENE, CzechAnalyzer::new)); analyzers.add(new PreBuiltAnalyzerProviderFactory("danish", CachingStrategy.LUCENE, DanishAnalyzer::new)); @@ -439,19 +482,27 @@ public List getPreConfiguredTokenFilters() { filters.add(PreConfiguredTokenFilter.singleton("cjk_bigram", false, CJKBigramFilter::new)); filters.add(PreConfiguredTokenFilter.singleton("cjk_width", true, CJKWidthFilter::new)); filters.add(PreConfiguredTokenFilter.singleton("classic", false, ClassicFilter::new)); - filters.add(PreConfiguredTokenFilter.singleton("common_grams", false, false, - input -> new CommonGramsFilter(input, CharArraySet.EMPTY_SET))); + filters.add( + PreConfiguredTokenFilter.singleton("common_grams", false, false, input -> new CommonGramsFilter(input, CharArraySet.EMPTY_SET)) + ); filters.add(PreConfiguredTokenFilter.singleton("czech_stem", false, CzechStemFilter::new)); filters.add(PreConfiguredTokenFilter.singleton("decimal_digit", true, DecimalDigitFilter::new)); - filters.add(PreConfiguredTokenFilter.singleton("delimited_payload", false, input -> - new DelimitedPayloadTokenFilter(input, - DelimitedPayloadTokenFilterFactory.DEFAULT_DELIMITER, - DelimitedPayloadTokenFilterFactory.DEFAULT_ENCODER))); + filters.add( + PreConfiguredTokenFilter.singleton( + "delimited_payload", + false, + input -> new DelimitedPayloadTokenFilter( + input, + DelimitedPayloadTokenFilterFactory.DEFAULT_DELIMITER, + DelimitedPayloadTokenFilterFactory.DEFAULT_ENCODER + ) + ) + ); filters.add(PreConfiguredTokenFilter.singleton("dutch_stem", false, input -> new SnowballFilter(input, new DutchStemmer()))); - filters.add(PreConfiguredTokenFilter.singleton("edge_ngram", false, false, input -> - new EdgeNGramTokenFilter(input, 1))); - filters.add(PreConfiguredTokenFilter.singleton("elision", true, - input -> new ElisionFilter(input, FrenchAnalyzer.DEFAULT_ARTICLES))); + filters.add(PreConfiguredTokenFilter.singleton("edge_ngram", false, false, input -> new EdgeNGramTokenFilter(input, 1))); + filters.add( + PreConfiguredTokenFilter.singleton("elision", true, input -> new ElisionFilter(input, FrenchAnalyzer.DEFAULT_ARTICLES)) + ); filters.add(PreConfiguredTokenFilter.singleton("french_stem", false, input -> new SnowballFilter(input, new FrenchStemmer()))); filters.add(PreConfiguredTokenFilter.singleton("german_normalization", true, GermanNormalizationFilter::new)); filters.add(PreConfiguredTokenFilter.singleton("german_stem", false, GermanStemFilter::new)); @@ -461,10 +512,17 @@ public List getPreConfiguredTokenFilters() { filters.add(PreConfiguredTokenFilter.singleton("kstem", false, KStemFilter::new)); // TODO this one seems useless filters.add(PreConfiguredTokenFilter.singleton("length", false, input -> new LengthFilter(input, 0, Integer.MAX_VALUE))); - filters.add(PreConfiguredTokenFilter.singleton("limit", false, input -> - new LimitTokenCountFilter(input, - LimitTokenCountFilterFactory.DEFAULT_MAX_TOKEN_COUNT, - LimitTokenCountFilterFactory.DEFAULT_CONSUME_ALL_TOKENS))); + filters.add( + PreConfiguredTokenFilter.singleton( + "limit", + false, + input -> new LimitTokenCountFilter( + input, + LimitTokenCountFilterFactory.DEFAULT_MAX_TOKEN_COUNT, + LimitTokenCountFilterFactory.DEFAULT_CONSUME_ALL_TOKENS + ) + ) + ); filters.add(PreConfiguredTokenFilter.singleton("ngram", false, false, reader -> new NGramTokenFilter(reader, 1, 2, false))); filters.add(PreConfiguredTokenFilter.singleton("persian_normalization", true, PersianNormalizationFilter::new)); filters.add(PreConfiguredTokenFilter.singleton("porter_stem", false, PorterStemFilter::new)); @@ -487,28 +545,39 @@ public List getPreConfiguredTokenFilters() { filters.add(PreConfiguredTokenFilter.singleton("sorani_normalization", true, SoraniNormalizationFilter::new)); filters.add(PreConfiguredTokenFilter.singleton("stemmer", false, PorterStemFilter::new)); // The stop filter is in lucene-core but the English stop words set is in lucene-analyzers-common - filters.add(PreConfiguredTokenFilter.singleton("stop", false, - input -> new StopFilter(input, EnglishAnalyzer.ENGLISH_STOP_WORDS_SET))); + filters.add( + PreConfiguredTokenFilter.singleton("stop", false, input -> new StopFilter(input, EnglishAnalyzer.ENGLISH_STOP_WORDS_SET)) + ); filters.add(PreConfiguredTokenFilter.singleton("trim", true, TrimFilter::new)); filters.add(PreConfiguredTokenFilter.singleton("truncate", false, input -> new TruncateTokenFilter(input, 10))); filters.add(PreConfiguredTokenFilter.singleton("type_as_payload", false, TypeAsPayloadTokenFilter::new)); filters.add(PreConfiguredTokenFilter.singleton("unique", false, UniqueTokenFilter::new)); filters.add(PreConfiguredTokenFilter.singleton("uppercase", true, UpperCaseFilter::new)); - filters.add(PreConfiguredTokenFilter.singleton("word_delimiter", false, false, input -> - new WordDelimiterFilter(input, - WordDelimiterFilter.GENERATE_WORD_PARTS - | WordDelimiterFilter.GENERATE_NUMBER_PARTS - | WordDelimiterFilter.SPLIT_ON_CASE_CHANGE - | WordDelimiterFilter.SPLIT_ON_NUMERICS - | WordDelimiterFilter.STEM_ENGLISH_POSSESSIVE, null))); + filters.add( + PreConfiguredTokenFilter.singleton( + "word_delimiter", + false, + false, + input -> new WordDelimiterFilter( + input, + WordDelimiterFilter.GENERATE_WORD_PARTS | WordDelimiterFilter.GENERATE_NUMBER_PARTS + | WordDelimiterFilter.SPLIT_ON_CASE_CHANGE | WordDelimiterFilter.SPLIT_ON_NUMERICS + | WordDelimiterFilter.STEM_ENGLISH_POSSESSIVE, + null + ) + ) + ); filters.add(PreConfiguredTokenFilter.elasticsearchVersion("word_delimiter_graph", false, false, (input, version) -> { boolean adjustOffsets = version.onOrAfter(Version.V_7_3_0); - return new WordDelimiterGraphFilter(input, adjustOffsets, WordDelimiterIterator.DEFAULT_WORD_DELIM_TABLE, - WordDelimiterGraphFilter.GENERATE_WORD_PARTS - | WordDelimiterGraphFilter.GENERATE_NUMBER_PARTS - | WordDelimiterGraphFilter.SPLIT_ON_CASE_CHANGE - | WordDelimiterGraphFilter.SPLIT_ON_NUMERICS - | WordDelimiterGraphFilter.STEM_ENGLISH_POSSESSIVE, null); + return new WordDelimiterGraphFilter( + input, + adjustOffsets, + WordDelimiterIterator.DEFAULT_WORD_DELIM_TABLE, + WordDelimiterGraphFilter.GENERATE_WORD_PARTS | WordDelimiterGraphFilter.GENERATE_NUMBER_PARTS + | WordDelimiterGraphFilter.SPLIT_ON_CASE_CHANGE | WordDelimiterGraphFilter.SPLIT_ON_NUMERICS + | WordDelimiterGraphFilter.STEM_ENGLISH_POSSESSIVE, + null + ); })); return filters; } @@ -538,23 +607,33 @@ public List getPreConfiguredTokenizers() { // Temporary shim for aliases. TODO deprecate after they are moved tokenizers.add(PreConfiguredTokenizer.elasticsearchVersion("nGram", (version) -> { if (version.onOrAfter(org.elasticsearch.Version.V_8_0_0)) { - throw new IllegalArgumentException("The [nGram] tokenizer name was deprecated in 7.6. " - + "Please use the tokenizer name to [ngram] for indices created in versions 8 or higher instead."); + throw new IllegalArgumentException( + "The [nGram] tokenizer name was deprecated in 7.6. " + + "Please use the tokenizer name to [ngram] for indices created in versions 8 or higher instead." + ); } else if (version.onOrAfter(org.elasticsearch.Version.V_7_6_0)) { - deprecationLogger.critical(DeprecationCategory.ANALYSIS, "nGram_tokenizer_deprecation", + deprecationLogger.critical( + DeprecationCategory.ANALYSIS, + "nGram_tokenizer_deprecation", "The [nGram] tokenizer name is deprecated and will be removed in a future version. " - + "Please change the tokenizer name to [ngram] instead."); + + "Please change the tokenizer name to [ngram] instead." + ); } return new NGramTokenizer(); })); tokenizers.add(PreConfiguredTokenizer.elasticsearchVersion("edgeNGram", (version) -> { if (version.onOrAfter(org.elasticsearch.Version.V_8_0_0)) { - throw new IllegalArgumentException("The [edgeNGram] tokenizer name was deprecated in 7.6. " - + "Please use the tokenizer name to [edge_ngram] for indices created in versions 8 or higher instead."); + throw new IllegalArgumentException( + "The [edgeNGram] tokenizer name was deprecated in 7.6. " + + "Please use the tokenizer name to [edge_ngram] for indices created in versions 8 or higher instead." + ); } else if (version.onOrAfter(org.elasticsearch.Version.V_7_6_0)) { - deprecationLogger.critical(DeprecationCategory.ANALYSIS, "edgeNGram_tokenizer_deprecation", + deprecationLogger.critical( + DeprecationCategory.ANALYSIS, + "edgeNGram_tokenizer_deprecation", "The [edgeNGram] tokenizer name is deprecated and will be removed in a future version. " - + "Please change the tokenizer name to [edge_ngram] instead."); + + "Please change the tokenizer name to [edge_ngram] instead." + ); } if (version.onOrAfter(Version.V_7_3_0)) { return new EdgeNGramTokenizer(NGramTokenizer.DEFAULT_MIN_NGRAM_SIZE, NGramTokenizer.DEFAULT_MAX_NGRAM_SIZE); diff --git a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/CommonGramsTokenFilterFactory.java b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/CommonGramsTokenFilterFactory.java index af3ecd0b94ef2..1da02b9aa44b1 100644 --- a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/CommonGramsTokenFilterFactory.java +++ b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/CommonGramsTokenFilterFactory.java @@ -35,7 +35,8 @@ public class CommonGramsTokenFilterFactory extends AbstractTokenFilterFactory { if (this.words == null) { throw new IllegalArgumentException( - "missing or empty [common_words] or [common_words_path] configuration for common_grams token filter"); + "missing or empty [common_words] or [common_words_path] configuration for common_grams token filter" + ); } } @@ -54,4 +55,3 @@ public TokenFilterFactory getSynonymFilter() { throw new IllegalArgumentException("Token filter [" + name() + "] cannot be used to parse synonyms"); } } - diff --git a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/DictionaryCompoundWordTokenFilterFactory.java b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/DictionaryCompoundWordTokenFilterFactory.java index a50309fc3f027..22f6762286f11 100644 --- a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/DictionaryCompoundWordTokenFilterFactory.java +++ b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/DictionaryCompoundWordTokenFilterFactory.java @@ -14,7 +14,6 @@ import org.elasticsearch.env.Environment; import org.elasticsearch.index.IndexSettings; - /** * Uses the {@link org.apache.lucene.analysis.compound.DictionaryCompoundWordTokenFilter} to decompound tokens using a dictionary. * @@ -28,7 +27,6 @@ public class DictionaryCompoundWordTokenFilterFactory extends AbstractCompoundWo @Override public TokenStream create(TokenStream tokenStream) { - return new DictionaryCompoundWordTokenFilter(tokenStream, wordList, minWordSize, - minSubwordSize, maxSubwordSize, onlyLongestMatch); + return new DictionaryCompoundWordTokenFilter(tokenStream, wordList, minWordSize, minSubwordSize, maxSubwordSize, onlyLongestMatch); } } diff --git a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/ESSolrSynonymParser.java b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/ESSolrSynonymParser.java index c1d499352a31c..486b83166e45b 100644 --- a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/ESSolrSynonymParser.java +++ b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/ESSolrSynonymParser.java @@ -8,8 +8,8 @@ package org.elasticsearch.analysis.common; -import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.synonym.SolrSynonymParser; import org.apache.lucene.util.CharsRef; diff --git a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/ESWordnetSynonymParser.java b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/ESWordnetSynonymParser.java index b587afde0f43c..6466335449e1b 100644 --- a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/ESWordnetSynonymParser.java +++ b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/ESWordnetSynonymParser.java @@ -8,8 +8,8 @@ package org.elasticsearch.analysis.common; -import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.synonym.WordnetSynonymParser; import org.apache.lucene.util.CharsRef; diff --git a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/EdgeNGramTokenFilterFactory.java b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/EdgeNGramTokenFilterFactory.java index 98880c352f02b..bbc2a3afcb589 100644 --- a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/EdgeNGramTokenFilterFactory.java +++ b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/EdgeNGramTokenFilterFactory.java @@ -17,7 +17,6 @@ import org.elasticsearch.index.analysis.AbstractTokenFilterFactory; import org.elasticsearch.index.analysis.TokenFilterFactory; - public class EdgeNGramTokenFilterFactory extends AbstractTokenFilterFactory { private final int minGram; @@ -39,10 +38,13 @@ public class EdgeNGramTokenFilterFactory extends AbstractTokenFilterFactory { } static int parseSide(String side) { - switch(side) { - case "front": return SIDE_FRONT; - case "back": return SIDE_BACK; - default: throw new IllegalArgumentException("invalid side: " + side); + switch (side) { + case "front": + return SIDE_FRONT; + case "back": + return SIDE_BACK; + default: + throw new IllegalArgumentException("invalid side: " + side); } } diff --git a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/EstonianAnalyzerProvider.java b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/EstonianAnalyzerProvider.java index f74ce52af2bf1..d4aa2c3ab3e7f 100644 --- a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/EstonianAnalyzerProvider.java +++ b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/EstonianAnalyzerProvider.java @@ -29,5 +29,7 @@ public class EstonianAnalyzerProvider extends AbstractIndexAnalyzerProvider rules = Analysis.getWordSet(env, settings, "keywords"); if (rules == null) { throw new IllegalArgumentException( - "keyword filter requires either `keywords`, `keywords_path`, " + - "or `keywords_pattern` to be configured"); + "keyword filter requires either `keywords`, `keywords_path`, " + "or `keywords_pattern` to be configured" + ); } // a set of keywords (or a path to them) is specified keywordLookup = new CharArraySet(rules, ignoreCase); diff --git a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/LowerCaseTokenFilterFactory.java b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/LowerCaseTokenFilterFactory.java index c0d894185a277..448f7ad41dfa3 100644 --- a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/LowerCaseTokenFilterFactory.java +++ b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/LowerCaseTokenFilterFactory.java @@ -53,5 +53,3 @@ public TokenStream create(TokenStream tokenStream) { } } - - diff --git a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/MappingCharFilterFactory.java b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/MappingCharFilterFactory.java index b16348ce2fb80..0142aa1be90c0 100644 --- a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/MappingCharFilterFactory.java +++ b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/MappingCharFilterFactory.java @@ -58,8 +58,7 @@ private void parseRules(List rules, NormalizeCharMap.Builder map) { } String lhs = parseString(m.group(1).trim()); String rhs = parseString(m.group(2).trim()); - if (lhs == null || rhs == null) - throw new RuntimeException("Invalid Mapping Rule : [" + rule + "]. Illegal mapping."); + if (lhs == null || rhs == null) throw new RuntimeException("Invalid Mapping Rule : [" + rule + "]. Illegal mapping."); map.add(lhs, rhs); } } @@ -73,8 +72,7 @@ private String parseString(String s) { while (readPos < len) { char c = s.charAt(readPos++); if (c == '\\') { - if (readPos >= len) - throw new RuntimeException("Invalid escaped char in [" + s + "]"); + if (readPos >= len) throw new RuntimeException("Invalid escaped char in [" + s + "]"); c = s.charAt(readPos++); switch (c) { case '\\': @@ -96,8 +94,7 @@ private String parseString(String s) { c = '\f'; break; case 'u': - if (readPos + 3 >= len) - throw new RuntimeException("Invalid escaped char in [" + s + "]"); + if (readPos + 3 >= len) throw new RuntimeException("Invalid escaped char in [" + s + "]"); c = (char) Integer.parseInt(s.substring(readPos, readPos + 4), 16); readPos += 4; break; diff --git a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/MultiplexerTokenFilterFactory.java b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/MultiplexerTokenFilterFactory.java index e00d7a21560ca..9bd239b444805 100644 --- a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/MultiplexerTokenFilterFactory.java +++ b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/MultiplexerTokenFilterFactory.java @@ -50,9 +50,12 @@ public TokenFilterFactory getSynonymFilter() { } @Override - public TokenFilterFactory getChainAwareTokenFilterFactory(TokenizerFactory tokenizer, List charFilters, - List previousTokenFilters, - Function allFilters) { + public TokenFilterFactory getChainAwareTokenFilterFactory( + TokenizerFactory tokenizer, + List charFilters, + List previousTokenFilters, + Function allFilters + ) { List filters = new ArrayList<>(); if (preserveOriginal) { filters.add(IDENTITY_FILTER); diff --git a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/NGramTokenFilterFactory.java b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/NGramTokenFilterFactory.java index d81ba94069b28..c929b4710bf49 100644 --- a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/NGramTokenFilterFactory.java +++ b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/NGramTokenFilterFactory.java @@ -16,7 +16,6 @@ import org.elasticsearch.index.analysis.AbstractTokenFilterFactory; import org.elasticsearch.index.analysis.TokenFilterFactory; - public class NGramTokenFilterFactory extends AbstractTokenFilterFactory { private final int minGram; private final int maxGram; @@ -32,8 +31,13 @@ public class NGramTokenFilterFactory extends AbstractTokenFilterFactory { if (ngramDiff > maxAllowedNgramDiff) { throw new IllegalArgumentException( "The difference between max_gram and min_gram in NGram Tokenizer must be less than or equal to: [" - + maxAllowedNgramDiff + "] but was [" + ngramDiff + "]. This limit can be set by changing the [" - + IndexSettings.MAX_NGRAM_DIFF_SETTING.getKey() + "] index level setting."); + + maxAllowedNgramDiff + + "] but was [" + + ngramDiff + + "]. This limit can be set by changing the [" + + IndexSettings.MAX_NGRAM_DIFF_SETTING.getKey() + + "] index level setting." + ); } preserveOriginal = settings.getAsBoolean(PRESERVE_ORIG_KEY, false); } diff --git a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/NGramTokenizerFactory.java b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/NGramTokenizerFactory.java index ae2c68fb87ab7..e58d1d6fc7c04 100644 --- a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/NGramTokenizerFactory.java +++ b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/NGramTokenizerFactory.java @@ -46,9 +46,9 @@ public class NGramTokenizerFactory extends AbstractTokenizerFactory { // Populate with unicode categories from java.lang.Character for (Field field : Character.class.getFields()) { if (field.getName().startsWith("DIRECTIONALITY") == false - && Modifier.isPublic(field.getModifiers()) - && Modifier.isStatic(field.getModifiers()) - && field.getType() == byte.class) { + && Modifier.isPublic(field.getModifiers()) + && Modifier.isStatic(field.getModifiers()) + && field.getType() == byte.class) { try { matchers.put(field.getName().toLowerCase(Locale.ROOT), CharMatcher.ByUnicodeCategory.of(field.getByte(null))); } catch (Exception e) { @@ -71,8 +71,14 @@ static CharMatcher parseTokenChars(Settings settings) { CharMatcher matcher = MATCHERS.get(characterClass); if (matcher == null) { if (characterClass.equals("custom") == false) { - throw new IllegalArgumentException("Unknown token type: '" + characterClass + "', must be one of " + Stream - .of(MATCHERS.keySet(), Collections.singleton("custom")).flatMap(x -> x.stream()).collect(Collectors.toSet())); + throw new IllegalArgumentException( + "Unknown token type: '" + + characterClass + + "', must be one of " + + Stream.of(MATCHERS.keySet(), Collections.singleton("custom")) + .flatMap(x -> x.stream()) + .collect(Collectors.toSet()) + ); } String customCharacters = settings.get("custom_token_chars"); if (customCharacters == null) { @@ -102,8 +108,13 @@ public boolean isTokenChar(int c) { if (ngramDiff > maxAllowedNgramDiff) { throw new IllegalArgumentException( "The difference between max_gram and min_gram in NGram Tokenizer must be less than or equal to: [" - + maxAllowedNgramDiff + "] but was [" + ngramDiff + "]. This limit can be set by changing the [" - + IndexSettings.MAX_NGRAM_DIFF_SETTING.getKey() + "] index level setting."); + + maxAllowedNgramDiff + + "] but was [" + + ngramDiff + + "]. This limit can be set by changing the [" + + IndexSettings.MAX_NGRAM_DIFF_SETTING.getKey() + + "] index level setting." + ); } this.matcher = parseTokenChars(settings); } diff --git a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/PatternCaptureGroupTokenFilterFactory.java b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/PatternCaptureGroupTokenFilterFactory.java index acd1b92327a06..9af3f280c0f89 100644 --- a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/PatternCaptureGroupTokenFilterFactory.java +++ b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/PatternCaptureGroupTokenFilterFactory.java @@ -7,7 +7,6 @@ */ package org.elasticsearch.analysis.common; - import org.apache.lucene.analysis.TokenFilter; import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.pattern.PatternCaptureGroupTokenFilter; diff --git a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/PorterStemTokenFilterFactory.java b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/PorterStemTokenFilterFactory.java index cf6f2b1501710..0bd480064a8cc 100644 --- a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/PorterStemTokenFilterFactory.java +++ b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/PorterStemTokenFilterFactory.java @@ -26,5 +26,3 @@ public TokenStream create(TokenStream tokenStream) { return new PorterStemFilter(tokenStream); } } - - diff --git a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/ScriptedConditionTokenFilterFactory.java b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/ScriptedConditionTokenFilterFactory.java index 93276959ac923..1c6b02ea524b6 100644 --- a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/ScriptedConditionTokenFilterFactory.java +++ b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/ScriptedConditionTokenFilterFactory.java @@ -34,8 +34,7 @@ public class ScriptedConditionTokenFilterFactory extends AbstractTokenFilterFact private final AnalysisPredicateScript.Factory factory; private final List filterNames; - ScriptedConditionTokenFilterFactory(IndexSettings indexSettings, String name, - Settings settings, ScriptService scriptService) { + ScriptedConditionTokenFilterFactory(IndexSettings indexSettings, String name, Settings settings, ScriptService scriptService) { super(indexSettings, name, settings); Settings scriptSettings = settings.getAsSettings("script"); @@ -57,16 +56,20 @@ public TokenStream create(TokenStream tokenStream) { } @Override - public TokenFilterFactory getChainAwareTokenFilterFactory(TokenizerFactory tokenizer, List charFilters, - List previousTokenFilters, - Function allFilters) { + public TokenFilterFactory getChainAwareTokenFilterFactory( + TokenizerFactory tokenizer, + List charFilters, + List previousTokenFilters, + Function allFilters + ) { List filters = new ArrayList<>(); List existingChain = new ArrayList<>(previousTokenFilters); for (String filter : filterNames) { TokenFilterFactory tff = allFilters.apply(filter); if (tff == null) { - throw new IllegalArgumentException("ScriptedConditionTokenFilter [" + name() + - "] refers to undefined token filter [" + filter + "]"); + throw new IllegalArgumentException( + "ScriptedConditionTokenFilter [" + name() + "] refers to undefined token filter [" + filter + "]" + ); } tff = tff.getChainAwareTokenFilterFactory(tokenizer, charFilters, existingChain, allFilters); filters.add(tff); @@ -97,8 +100,7 @@ private static class ScriptedConditionTokenFilter extends ConditionalTokenFilter private final AnalysisPredicateScript script; private final AnalysisPredicateScript.Token token; - ScriptedConditionTokenFilter(TokenStream input, Function inputFactory, - AnalysisPredicateScript script) { + ScriptedConditionTokenFilter(TokenStream input, Function inputFactory, AnalysisPredicateScript script) { super(input, inputFactory); this.script = script; this.token = new AnalysisPredicateScript.Token(this); diff --git a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/SnowballAnalyzer.java b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/SnowballAnalyzer.java index 6f3a901d73290..ce67ac06eba79 100644 --- a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/SnowballAnalyzer.java +++ b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/SnowballAnalyzer.java @@ -31,37 +31,33 @@ */ @Deprecated public final class SnowballAnalyzer extends Analyzer { - private String name; - private CharArraySet stopSet; + private String name; + private CharArraySet stopSet; - /** Builds the named analyzer with no stop words. */ - SnowballAnalyzer(String name) { - this.name = name; - } + /** Builds the named analyzer with no stop words. */ + SnowballAnalyzer(String name) { + this.name = name; + } - /** Builds the named analyzer with the given stop words. */ - SnowballAnalyzer(String name, CharArraySet stopWords) { - this(name); - stopSet = CharArraySet.unmodifiableSet(CharArraySet.copy(stopWords)); - } + /** Builds the named analyzer with the given stop words. */ + SnowballAnalyzer(String name, CharArraySet stopWords) { + this(name); + stopSet = CharArraySet.unmodifiableSet(CharArraySet.copy(stopWords)); + } - /** Constructs a {@link StandardTokenizer} filtered by a {@link LowerCaseFilter}, a {@link StopFilter}, + /** Constructs a {@link StandardTokenizer} filtered by a {@link LowerCaseFilter}, a {@link StopFilter}, and a {@link SnowballFilter} */ - @Override - public TokenStreamComponents createComponents(String fieldName) { - final Tokenizer tokenizer = new StandardTokenizer(); - TokenStream result = tokenizer; - // remove the possessive 's for english stemmers - if (name.equals("English") || name.equals("Porter") || name.equals("Lovins")) - result = new EnglishPossessiveFilter(result); - // Use a special lowercase filter for turkish, the stemmer expects it. - if (name.equals("Turkish")) - result = new TurkishLowerCaseFilter(result); - else - result = new LowerCaseFilter(result); - if (stopSet != null) - result = new StopFilter(result, stopSet); - result = new SnowballFilter(result, name); - return new TokenStreamComponents(tokenizer, result); - } + @Override + public TokenStreamComponents createComponents(String fieldName) { + final Tokenizer tokenizer = new StandardTokenizer(); + TokenStream result = tokenizer; + // remove the possessive 's for english stemmers + if (name.equals("English") || name.equals("Porter") || name.equals("Lovins")) result = new EnglishPossessiveFilter(result); + // Use a special lowercase filter for turkish, the stemmer expects it. + if (name.equals("Turkish")) result = new TurkishLowerCaseFilter(result); + else result = new LowerCaseFilter(result); + if (stopSet != null) result = new StopFilter(result, stopSet); + result = new SnowballFilter(result, name); + return new TokenStreamComponents(tokenizer, result); + } } diff --git a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/SnowballAnalyzerProvider.java b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/SnowballAnalyzerProvider.java index 4de0b491a305b..a52da493cf257 100644 --- a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/SnowballAnalyzerProvider.java +++ b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/SnowballAnalyzerProvider.java @@ -35,11 +35,17 @@ */ public class SnowballAnalyzerProvider extends AbstractIndexAnalyzerProvider { private static final Map DEFAULT_LANGUAGE_STOP_WORDS = Map.of( - "English", EnglishAnalyzer.ENGLISH_STOP_WORDS_SET, - "Dutch", DutchAnalyzer.getDefaultStopSet(), - "German", GermanAnalyzer.getDefaultStopSet(), - "German2", GermanAnalyzer.getDefaultStopSet(), - "French", FrenchAnalyzer.getDefaultStopSet()); + "English", + EnglishAnalyzer.ENGLISH_STOP_WORDS_SET, + "Dutch", + DutchAnalyzer.getDefaultStopSet(), + "German", + GermanAnalyzer.getDefaultStopSet(), + "German2", + GermanAnalyzer.getDefaultStopSet(), + "French", + FrenchAnalyzer.getDefaultStopSet() + ); private final SnowballAnalyzer analyzer; diff --git a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/StemmerTokenFilterFactory.java b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/StemmerTokenFilterFactory.java index 642e2fccf0618..89e656c83911f 100644 --- a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/StemmerTokenFilterFactory.java +++ b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/StemmerTokenFilterFactory.java @@ -118,138 +118,139 @@ public TokenStream create(TokenStream tokenStream) { // English stemmers } else if ("english".equalsIgnoreCase(language)) { return new PorterStemFilter(tokenStream); - } else if ("light_english".equalsIgnoreCase(language) || "lightEnglish".equalsIgnoreCase(language) - || "kstem".equalsIgnoreCase(language)) { - return new KStemFilter(tokenStream); - } else if ("lovins".equalsIgnoreCase(language)) { - return new SnowballFilter(tokenStream, new LovinsStemmer()); - } else if ("porter".equalsIgnoreCase(language)) { - return new PorterStemFilter(tokenStream); - } else if ("porter2".equalsIgnoreCase(language)) { - return new SnowballFilter(tokenStream, new EnglishStemmer()); - } else if ("minimal_english".equalsIgnoreCase(language) || "minimalEnglish".equalsIgnoreCase(language)) { - return new EnglishMinimalStemFilter(tokenStream); - } else if ("possessive_english".equalsIgnoreCase(language) || "possessiveEnglish".equalsIgnoreCase(language)) { - return new EnglishPossessiveFilter(tokenStream); - - } else if ("estonian".equalsIgnoreCase(language)) { - return new SnowballFilter(tokenStream, new EstonianStemmer()); - - // Finnish stemmers - } else if ("finnish".equalsIgnoreCase(language)) { - return new SnowballFilter(tokenStream, new FinnishStemmer()); - } else if ("light_finish".equalsIgnoreCase(language) || "lightFinish".equalsIgnoreCase(language)) { - // leaving this for backward compatibility - return new FinnishLightStemFilter(tokenStream); - } else if ("light_finnish".equalsIgnoreCase(language) || "lightFinnish".equalsIgnoreCase(language)) { - return new FinnishLightStemFilter(tokenStream); - - // French stemmers - } else if ("french".equalsIgnoreCase(language)) { - return new SnowballFilter(tokenStream, new FrenchStemmer()); - } else if ("light_french".equalsIgnoreCase(language) || "lightFrench".equalsIgnoreCase(language)) { - return new FrenchLightStemFilter(tokenStream); - } else if ("minimal_french".equalsIgnoreCase(language) || "minimalFrench".equalsIgnoreCase(language)) { - return new FrenchMinimalStemFilter(tokenStream); - - // Galician stemmers - } else if ("galician".equalsIgnoreCase(language)) { - return new GalicianStemFilter(tokenStream); - } else if ("minimal_galician".equalsIgnoreCase(language)) { - return new GalicianMinimalStemFilter(tokenStream); - - // German stemmers - } else if ("german".equalsIgnoreCase(language)) { - return new SnowballFilter(tokenStream, new GermanStemmer()); - } else if ("german2".equalsIgnoreCase(language)) { - return new SnowballFilter(tokenStream, new German2Stemmer()); - } else if ("light_german".equalsIgnoreCase(language) || "lightGerman".equalsIgnoreCase(language)) { - return new GermanLightStemFilter(tokenStream); - } else if ("minimal_german".equalsIgnoreCase(language) || "minimalGerman".equalsIgnoreCase(language)) { - return new GermanMinimalStemFilter(tokenStream); - - } else if ("greek".equalsIgnoreCase(language)) { - return new GreekStemFilter(tokenStream); - } else if ("hindi".equalsIgnoreCase(language)) { - return new HindiStemFilter(tokenStream); - - // Hungarian stemmers - } else if ("hungarian".equalsIgnoreCase(language)) { - return new SnowballFilter(tokenStream, new HungarianStemmer()); - } else if ("light_hungarian".equalsIgnoreCase(language) || "lightHungarian".equalsIgnoreCase(language)) { - return new HungarianLightStemFilter(tokenStream); - - } else if ("indonesian".equalsIgnoreCase(language)) { - return new IndonesianStemFilter(tokenStream); - - // Irish stemmer - } else if ("irish".equalsIgnoreCase(language)) { - return new SnowballFilter(tokenStream, new IrishStemmer()); - - // Italian stemmers - } else if ("italian".equalsIgnoreCase(language)) { - return new SnowballFilter(tokenStream, new ItalianStemmer()); - } else if ("light_italian".equalsIgnoreCase(language) || "lightItalian".equalsIgnoreCase(language)) { - return new ItalianLightStemFilter(tokenStream); - - } else if ("latvian".equalsIgnoreCase(language)) { - return new LatvianStemFilter(tokenStream); - - } else if ("lithuanian".equalsIgnoreCase(language)) { - return new SnowballFilter(tokenStream, new LithuanianStemmer()); - - // Norwegian (Bokmål) stemmers - } else if ("norwegian".equalsIgnoreCase(language)) { - return new SnowballFilter(tokenStream, new NorwegianStemmer()); - } else if ("light_norwegian".equalsIgnoreCase(language) || "lightNorwegian".equalsIgnoreCase(language)) { - return new NorwegianLightStemFilter(tokenStream); - } else if ("minimal_norwegian".equalsIgnoreCase(language) || "minimalNorwegian".equals(language)) { - return new NorwegianMinimalStemFilter(tokenStream); - - // Norwegian (Nynorsk) stemmers - } else if ("light_nynorsk".equalsIgnoreCase(language) || "lightNynorsk".equalsIgnoreCase(language)) { - return new NorwegianLightStemFilter(tokenStream, NorwegianLightStemmer.NYNORSK); - } else if ("minimal_nynorsk".equalsIgnoreCase(language) || "minimalNynorsk".equalsIgnoreCase(language)) { - return new NorwegianMinimalStemFilter(tokenStream, NorwegianLightStemmer.NYNORSK); - - // Portuguese stemmers - } else if ("portuguese".equalsIgnoreCase(language)) { - return new SnowballFilter(tokenStream, new PortugueseStemmer()); - } else if ("light_portuguese".equalsIgnoreCase(language) || "lightPortuguese".equalsIgnoreCase(language)) { - return new PortugueseLightStemFilter(tokenStream); - } else if ("minimal_portuguese".equalsIgnoreCase(language) || "minimalPortuguese".equalsIgnoreCase(language)) { - return new PortugueseMinimalStemFilter(tokenStream); - } else if ("portuguese_rslp".equalsIgnoreCase(language)) { - return new PortugueseStemFilter(tokenStream); - - } else if ("romanian".equalsIgnoreCase(language)) { - return new SnowballFilter(tokenStream, new RomanianStemmer()); - - // Russian stemmers - } else if ("russian".equalsIgnoreCase(language)) { - return new SnowballFilter(tokenStream, new RussianStemmer()); - } else if ("light_russian".equalsIgnoreCase(language) || "lightRussian".equalsIgnoreCase(language)) { - return new RussianLightStemFilter(tokenStream); - - // Spanish stemmers - } else if ("spanish".equalsIgnoreCase(language)) { - return new SnowballFilter(tokenStream, new SpanishStemmer()); - } else if ("light_spanish".equalsIgnoreCase(language) || "lightSpanish".equalsIgnoreCase(language)) { - return new SpanishLightStemFilter(tokenStream); - - // Sorani Kurdish stemmer - } else if ("sorani".equalsIgnoreCase(language)) { - return new SoraniStemFilter(tokenStream); - - // Swedish stemmers - } else if ("swedish".equalsIgnoreCase(language)) { - return new SnowballFilter(tokenStream, new SwedishStemmer()); - } else if ("light_swedish".equalsIgnoreCase(language) || "lightSwedish".equalsIgnoreCase(language)) { - return new SwedishLightStemFilter(tokenStream); - - } else if ("turkish".equalsIgnoreCase(language)) { - return new SnowballFilter(tokenStream, new TurkishStemmer()); - } + } else if ("light_english".equalsIgnoreCase(language) + || "lightEnglish".equalsIgnoreCase(language) + || "kstem".equalsIgnoreCase(language)) { + return new KStemFilter(tokenStream); + } else if ("lovins".equalsIgnoreCase(language)) { + return new SnowballFilter(tokenStream, new LovinsStemmer()); + } else if ("porter".equalsIgnoreCase(language)) { + return new PorterStemFilter(tokenStream); + } else if ("porter2".equalsIgnoreCase(language)) { + return new SnowballFilter(tokenStream, new EnglishStemmer()); + } else if ("minimal_english".equalsIgnoreCase(language) || "minimalEnglish".equalsIgnoreCase(language)) { + return new EnglishMinimalStemFilter(tokenStream); + } else if ("possessive_english".equalsIgnoreCase(language) || "possessiveEnglish".equalsIgnoreCase(language)) { + return new EnglishPossessiveFilter(tokenStream); + + } else if ("estonian".equalsIgnoreCase(language)) { + return new SnowballFilter(tokenStream, new EstonianStemmer()); + + // Finnish stemmers + } else if ("finnish".equalsIgnoreCase(language)) { + return new SnowballFilter(tokenStream, new FinnishStemmer()); + } else if ("light_finish".equalsIgnoreCase(language) || "lightFinish".equalsIgnoreCase(language)) { + // leaving this for backward compatibility + return new FinnishLightStemFilter(tokenStream); + } else if ("light_finnish".equalsIgnoreCase(language) || "lightFinnish".equalsIgnoreCase(language)) { + return new FinnishLightStemFilter(tokenStream); + + // French stemmers + } else if ("french".equalsIgnoreCase(language)) { + return new SnowballFilter(tokenStream, new FrenchStemmer()); + } else if ("light_french".equalsIgnoreCase(language) || "lightFrench".equalsIgnoreCase(language)) { + return new FrenchLightStemFilter(tokenStream); + } else if ("minimal_french".equalsIgnoreCase(language) || "minimalFrench".equalsIgnoreCase(language)) { + return new FrenchMinimalStemFilter(tokenStream); + + // Galician stemmers + } else if ("galician".equalsIgnoreCase(language)) { + return new GalicianStemFilter(tokenStream); + } else if ("minimal_galician".equalsIgnoreCase(language)) { + return new GalicianMinimalStemFilter(tokenStream); + + // German stemmers + } else if ("german".equalsIgnoreCase(language)) { + return new SnowballFilter(tokenStream, new GermanStemmer()); + } else if ("german2".equalsIgnoreCase(language)) { + return new SnowballFilter(tokenStream, new German2Stemmer()); + } else if ("light_german".equalsIgnoreCase(language) || "lightGerman".equalsIgnoreCase(language)) { + return new GermanLightStemFilter(tokenStream); + } else if ("minimal_german".equalsIgnoreCase(language) || "minimalGerman".equalsIgnoreCase(language)) { + return new GermanMinimalStemFilter(tokenStream); + + } else if ("greek".equalsIgnoreCase(language)) { + return new GreekStemFilter(tokenStream); + } else if ("hindi".equalsIgnoreCase(language)) { + return new HindiStemFilter(tokenStream); + + // Hungarian stemmers + } else if ("hungarian".equalsIgnoreCase(language)) { + return new SnowballFilter(tokenStream, new HungarianStemmer()); + } else if ("light_hungarian".equalsIgnoreCase(language) || "lightHungarian".equalsIgnoreCase(language)) { + return new HungarianLightStemFilter(tokenStream); + + } else if ("indonesian".equalsIgnoreCase(language)) { + return new IndonesianStemFilter(tokenStream); + + // Irish stemmer + } else if ("irish".equalsIgnoreCase(language)) { + return new SnowballFilter(tokenStream, new IrishStemmer()); + + // Italian stemmers + } else if ("italian".equalsIgnoreCase(language)) { + return new SnowballFilter(tokenStream, new ItalianStemmer()); + } else if ("light_italian".equalsIgnoreCase(language) || "lightItalian".equalsIgnoreCase(language)) { + return new ItalianLightStemFilter(tokenStream); + + } else if ("latvian".equalsIgnoreCase(language)) { + return new LatvianStemFilter(tokenStream); + + } else if ("lithuanian".equalsIgnoreCase(language)) { + return new SnowballFilter(tokenStream, new LithuanianStemmer()); + + // Norwegian (Bokmål) stemmers + } else if ("norwegian".equalsIgnoreCase(language)) { + return new SnowballFilter(tokenStream, new NorwegianStemmer()); + } else if ("light_norwegian".equalsIgnoreCase(language) || "lightNorwegian".equalsIgnoreCase(language)) { + return new NorwegianLightStemFilter(tokenStream); + } else if ("minimal_norwegian".equalsIgnoreCase(language) || "minimalNorwegian".equals(language)) { + return new NorwegianMinimalStemFilter(tokenStream); + + // Norwegian (Nynorsk) stemmers + } else if ("light_nynorsk".equalsIgnoreCase(language) || "lightNynorsk".equalsIgnoreCase(language)) { + return new NorwegianLightStemFilter(tokenStream, NorwegianLightStemmer.NYNORSK); + } else if ("minimal_nynorsk".equalsIgnoreCase(language) || "minimalNynorsk".equalsIgnoreCase(language)) { + return new NorwegianMinimalStemFilter(tokenStream, NorwegianLightStemmer.NYNORSK); + + // Portuguese stemmers + } else if ("portuguese".equalsIgnoreCase(language)) { + return new SnowballFilter(tokenStream, new PortugueseStemmer()); + } else if ("light_portuguese".equalsIgnoreCase(language) || "lightPortuguese".equalsIgnoreCase(language)) { + return new PortugueseLightStemFilter(tokenStream); + } else if ("minimal_portuguese".equalsIgnoreCase(language) || "minimalPortuguese".equalsIgnoreCase(language)) { + return new PortugueseMinimalStemFilter(tokenStream); + } else if ("portuguese_rslp".equalsIgnoreCase(language)) { + return new PortugueseStemFilter(tokenStream); + + } else if ("romanian".equalsIgnoreCase(language)) { + return new SnowballFilter(tokenStream, new RomanianStemmer()); + + // Russian stemmers + } else if ("russian".equalsIgnoreCase(language)) { + return new SnowballFilter(tokenStream, new RussianStemmer()); + } else if ("light_russian".equalsIgnoreCase(language) || "lightRussian".equalsIgnoreCase(language)) { + return new RussianLightStemFilter(tokenStream); + + // Spanish stemmers + } else if ("spanish".equalsIgnoreCase(language)) { + return new SnowballFilter(tokenStream, new SpanishStemmer()); + } else if ("light_spanish".equalsIgnoreCase(language) || "lightSpanish".equalsIgnoreCase(language)) { + return new SpanishLightStemFilter(tokenStream); + + // Sorani Kurdish stemmer + } else if ("sorani".equalsIgnoreCase(language)) { + return new SoraniStemFilter(tokenStream); + + // Swedish stemmers + } else if ("swedish".equalsIgnoreCase(language)) { + return new SnowballFilter(tokenStream, new SwedishStemmer()); + } else if ("light_swedish".equalsIgnoreCase(language) || "lightSwedish".equalsIgnoreCase(language)) { + return new SwedishLightStemFilter(tokenStream); + + } else if ("turkish".equalsIgnoreCase(language)) { + return new SnowballFilter(tokenStream, new TurkishStemmer()); + } return new SnowballFilter(tokenStream, language); } diff --git a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/SynonymGraphTokenFilterFactory.java b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/SynonymGraphTokenFilterFactory.java index 0f4a76337d78c..c2710f6f1859a 100644 --- a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/SynonymGraphTokenFilterFactory.java +++ b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/SynonymGraphTokenFilterFactory.java @@ -25,8 +25,7 @@ public class SynonymGraphTokenFilterFactory extends SynonymTokenFilterFactory { - SynonymGraphTokenFilterFactory(IndexSettings indexSettings, Environment env, - String name, Settings settings) { + SynonymGraphTokenFilterFactory(IndexSettings indexSettings, Environment env, String name, Settings settings) { super(indexSettings, env, name, settings); } @@ -36,9 +35,12 @@ public TokenStream create(TokenStream tokenStream) { } @Override - public TokenFilterFactory getChainAwareTokenFilterFactory(TokenizerFactory tokenizer, List charFilters, - List previousTokenFilters, - Function allFilters) { + public TokenFilterFactory getChainAwareTokenFilterFactory( + TokenizerFactory tokenizer, + List charFilters, + List previousTokenFilters, + Function allFilters + ) { final Analyzer analyzer = buildSynonymAnalyzer(tokenizer, charFilters, previousTokenFilters, allFilters); final SynonymMap synonyms = buildSynonyms(analyzer, getRulesFromSettings(environment)); final String name = name(); diff --git a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/SynonymTokenFilterFactory.java b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/SynonymTokenFilterFactory.java index feeea15589dce..9a11f33adae75 100644 --- a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/SynonymTokenFilterFactory.java +++ b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/SynonymTokenFilterFactory.java @@ -41,15 +41,17 @@ public class SynonymTokenFilterFactory extends AbstractTokenFilterFactory { protected final Environment environment; protected final AnalysisMode analysisMode; - SynonymTokenFilterFactory(IndexSettings indexSettings, Environment env, - String name, Settings settings) { + SynonymTokenFilterFactory(IndexSettings indexSettings, Environment env, String name, Settings settings) { super(indexSettings, name, settings); this.settings = settings; if (settings.get("ignore_case") != null) { - DEPRECATION_LOGGER.critical(DeprecationCategory.ANALYSIS, "synonym_ignore_case_option", - "The ignore_case option on the synonym_graph filter is deprecated. " + - "Instead, insert a lowercase filter in the filter chain before the synonym_graph filter."); + DEPRECATION_LOGGER.critical( + DeprecationCategory.ANALYSIS, + "synonym_ignore_case_option", + "The ignore_case option on the synonym_graph filter is deprecated. " + + "Instead, insert a lowercase filter in the filter chain before the synonym_graph filter." + ); } this.expand = settings.getAsBoolean("expand", true); @@ -71,9 +73,12 @@ public TokenStream create(TokenStream tokenStream) { } @Override - public TokenFilterFactory getChainAwareTokenFilterFactory(TokenizerFactory tokenizer, List charFilters, - List previousTokenFilters, - Function allFilters) { + public TokenFilterFactory getChainAwareTokenFilterFactory( + TokenizerFactory tokenizer, + List charFilters, + List previousTokenFilters, + Function allFilters + ) { final Analyzer analyzer = buildSynonymAnalyzer(tokenizer, charFilters, previousTokenFilters, allFilters); final SynonymMap synonyms = buildSynonyms(analyzer, getRulesFromSettings(environment)); final String name = name(); @@ -103,12 +108,17 @@ public AnalysisMode getAnalysisMode() { }; } - Analyzer buildSynonymAnalyzer(TokenizerFactory tokenizer, List charFilters, - List tokenFilters, Function allFilters) { - return new CustomAnalyzer(tokenizer, charFilters.toArray(new CharFilterFactory[0]), - tokenFilters.stream() - .map(TokenFilterFactory::getSynonymFilter) - .toArray(TokenFilterFactory[]::new)); + Analyzer buildSynonymAnalyzer( + TokenizerFactory tokenizer, + List charFilters, + List tokenFilters, + Function allFilters + ) { + return new CustomAnalyzer( + tokenizer, + charFilters.toArray(new CharFilterFactory[0]), + tokenFilters.stream().map(TokenFilterFactory::getSynonymFilter).toArray(TokenFilterFactory[]::new) + ); } SynonymMap buildSynonyms(Analyzer analyzer, Reader rules) { diff --git a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/ThaiAnalyzerProvider.java b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/ThaiAnalyzerProvider.java index 844299d7c555e..05866d557958e 100644 --- a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/ThaiAnalyzerProvider.java +++ b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/ThaiAnalyzerProvider.java @@ -21,8 +21,7 @@ public class ThaiAnalyzerProvider extends AbstractIndexAnalyzerProvider rules) { } String lhs = parseString(m.group(1).trim()); Byte rhs = parseType(m.group(2).trim()); - if (lhs.length() != 1) - throw new RuntimeException("Invalid Mapping Rule : [" - + rule + "]. Only a single character is allowed."); - if (rhs == null) - throw new RuntimeException("Invalid Mapping Rule : [" + rule + "]. Illegal type."); + if (lhs.length() != 1) throw new RuntimeException("Invalid Mapping Rule : [" + rule + "]. Only a single character is allowed."); + if (rhs == null) throw new RuntimeException("Invalid Mapping Rule : [" + rule + "]. Illegal type."); typeMap.put(lhs.charAt(0), rhs); } // ensure the table is always at least as big as DEFAULT_WORD_DELIM_TABLE for performance - byte types[] = new byte[Math.max( - typeMap.lastKey() + 1, WordDelimiterIterator.DEFAULT_WORD_DELIM_TABLE.length)]; + byte types[] = new byte[Math.max(typeMap.lastKey() + 1, WordDelimiterIterator.DEFAULT_WORD_DELIM_TABLE.length)]; for (int i = 0; i < types.length; i++) types[i] = WordDelimiterIterator.getType(i); for (Map.Entry mapping : typeMap.entrySet()) @@ -139,20 +131,13 @@ static byte[] parseTypes(Collection rules) { } private static Byte parseType(String s) { - if (s.equals("LOWER")) - return WordDelimiterFilter.LOWER; - else if (s.equals("UPPER")) - return WordDelimiterFilter.UPPER; - else if (s.equals("ALPHA")) - return WordDelimiterFilter.ALPHA; - else if (s.equals("DIGIT")) - return WordDelimiterFilter.DIGIT; - else if (s.equals("ALPHANUM")) - return WordDelimiterFilter.ALPHANUM; - else if (s.equals("SUBWORD_DELIM")) - return WordDelimiterFilter.SUBWORD_DELIM; - else - return null; + if (s.equals("LOWER")) return WordDelimiterFilter.LOWER; + else if (s.equals("UPPER")) return WordDelimiterFilter.UPPER; + else if (s.equals("ALPHA")) return WordDelimiterFilter.ALPHA; + else if (s.equals("DIGIT")) return WordDelimiterFilter.DIGIT; + else if (s.equals("ALPHANUM")) return WordDelimiterFilter.ALPHANUM; + else if (s.equals("SUBWORD_DELIM")) return WordDelimiterFilter.SUBWORD_DELIM; + else return null; } private static String parseString(String s) { @@ -163,8 +148,7 @@ private static String parseString(String s) { while (readPos < len) { char c = s.charAt(readPos++); if (c == '\\') { - if (readPos >= len) - throw new RuntimeException("Invalid escaped char in [" + s + "]"); + if (readPos >= len) throw new RuntimeException("Invalid escaped char in [" + s + "]"); c = s.charAt(readPos++); switch (c) { case '\\': @@ -186,8 +170,7 @@ private static String parseString(String s) { c = '\f'; break; case 'u': - if (readPos + 3 >= len) - throw new RuntimeException("Invalid escaped char in [" + s + "]"); + if (readPos + 3 >= len) throw new RuntimeException("Invalid escaped char in [" + s + "]"); c = (char) Integer.parseInt(s.substring(readPos, readPos + 4), 16); readPos += 4; break; diff --git a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/XLowerCaseTokenizer.java b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/XLowerCaseTokenizer.java index 0adbe00a6f9c4..392af4cd567b0 100644 --- a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/XLowerCaseTokenizer.java +++ b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/XLowerCaseTokenizer.java @@ -61,8 +61,8 @@ public final boolean incrementToken() throws IOException { assert start == -1; start = offset + bufferIndex - charCount; end = start; - } else if (length >= buffer.length-1) { // check if a supplementary could run out of bounds - buffer = termAtt.resizeBuffer(2+length); // make sure a supplementary fits in the buffer + } else if (length >= buffer.length - 1) { // check if a supplementary could run out of bounds + buffer = termAtt.resizeBuffer(2 + length); // make sure a supplementary fits in the buffer } end += charCount; length += Character.toChars(Character.toLowerCase(c), buffer, length); // buffer it, normalized diff --git a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/ASCIIFoldingTokenFilterFactoryTests.java b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/ASCIIFoldingTokenFilterFactoryTests.java index 33e55135b8cfd..32c16d01333bf 100644 --- a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/ASCIIFoldingTokenFilterFactoryTests.java +++ b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/ASCIIFoldingTokenFilterFactoryTests.java @@ -23,14 +23,15 @@ public class ASCIIFoldingTokenFilterFactoryTests extends ESTokenStreamTestCase { public void testDefault() throws IOException { ESTestCase.TestAnalysis analysis = AnalysisTestsHelper.createTestAnalysisFromSettings( - Settings.builder() - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .put("index.analysis.filter.my_ascii_folding.type", "asciifolding") - .build(), - new CommonAnalysisPlugin()); + Settings.builder() + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) + .put("index.analysis.filter.my_ascii_folding.type", "asciifolding") + .build(), + new CommonAnalysisPlugin() + ); TokenFilterFactory tokenFilter = analysis.tokenFilter.get("my_ascii_folding"); String source = "Ansprüche"; - String[] expected = new String[]{"Anspruche"}; + String[] expected = new String[] { "Anspruche" }; Tokenizer tokenizer = new WhitespaceTokenizer(); tokenizer.setReader(new StringReader(source)); assertTokenStreamContents(tokenFilter.create(tokenizer), expected); @@ -38,15 +39,16 @@ public void testDefault() throws IOException { public void testPreserveOriginal() throws IOException { ESTestCase.TestAnalysis analysis = AnalysisTestsHelper.createTestAnalysisFromSettings( - Settings.builder() - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .put("index.analysis.filter.my_ascii_folding.type", "asciifolding") - .put("index.analysis.filter.my_ascii_folding.preserve_original", true) - .build(), - new CommonAnalysisPlugin()); + Settings.builder() + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) + .put("index.analysis.filter.my_ascii_folding.type", "asciifolding") + .put("index.analysis.filter.my_ascii_folding.preserve_original", true) + .build(), + new CommonAnalysisPlugin() + ); TokenFilterFactory tokenFilter = analysis.tokenFilter.get("my_ascii_folding"); String source = "Ansprüche"; - String[] expected = new String[]{"Anspruche", "Ansprüche"}; + String[] expected = new String[] { "Anspruche", "Ansprüche" }; Tokenizer tokenizer = new WhitespaceTokenizer(); tokenizer.setReader(new StringReader(source)); assertTokenStreamContents(tokenFilter.create(tokenizer), expected); @@ -54,7 +56,7 @@ public void testPreserveOriginal() throws IOException { // but the multi-term aware component still emits a single token tokenizer = new WhitespaceTokenizer(); tokenizer.setReader(new StringReader(source)); - expected = new String[]{"Anspruche"}; + expected = new String[] { "Anspruche" }; assertTokenStreamContents(tokenFilter.normalize(tokenizer), expected); } } diff --git a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/BaseWordDelimiterTokenFilterFactoryTestCase.java b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/BaseWordDelimiterTokenFilterFactoryTestCase.java index 559dabde4bc64..51522b33ecdde 100644 --- a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/BaseWordDelimiterTokenFilterFactoryTestCase.java +++ b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/BaseWordDelimiterTokenFilterFactoryTestCase.java @@ -7,7 +7,6 @@ */ package org.elasticsearch.analysis.common; - import org.apache.lucene.analysis.Tokenizer; import org.apache.lucene.analysis.core.WhitespaceTokenizer; import org.elasticsearch.common.settings.Settings; @@ -33,15 +32,15 @@ public BaseWordDelimiterTokenFilterFactoryTestCase(String type) { public void testDefault() throws IOException { ESTestCase.TestAnalysis analysis = AnalysisTestsHelper.createTestAnalysisFromSettings( - Settings.builder() - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .put("index.analysis.filter.my_word_delimiter.type", type) - .build(), - new CommonAnalysisPlugin()); + Settings.builder() + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) + .put("index.analysis.filter.my_word_delimiter.type", type) + .build(), + new CommonAnalysisPlugin() + ); TokenFilterFactory tokenFilter = analysis.tokenFilter.get("my_word_delimiter"); String source = "PowerShot 500-42 wi-fi wi-fi-4000 j2se O'Neil's"; - String[] expected = new String[]{"Power", "Shot", "500", "42", "wi", "fi", "wi", - "fi", "4000", "j", "2", "se", "O", "Neil"}; + String[] expected = new String[] { "Power", "Shot", "500", "42", "wi", "fi", "wi", "fi", "4000", "j", "2", "se", "O", "Neil" }; Tokenizer tokenizer = new WhitespaceTokenizer(); tokenizer.setReader(new StringReader(source)); assertTokenStreamContents(tokenFilter.create(tokenizer), expected); @@ -49,17 +48,17 @@ public void testDefault() throws IOException { public void testCatenateWords() throws IOException { ESTestCase.TestAnalysis analysis = AnalysisTestsHelper.createTestAnalysisFromSettings( - Settings.builder() - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .put("index.analysis.filter.my_word_delimiter.type", type) - .put("index.analysis.filter.my_word_delimiter.catenate_words", "true") - .put("index.analysis.filter.my_word_delimiter.generate_word_parts", "false") - .build(), - new CommonAnalysisPlugin()); + Settings.builder() + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) + .put("index.analysis.filter.my_word_delimiter.type", type) + .put("index.analysis.filter.my_word_delimiter.catenate_words", "true") + .put("index.analysis.filter.my_word_delimiter.generate_word_parts", "false") + .build(), + new CommonAnalysisPlugin() + ); TokenFilterFactory tokenFilter = analysis.tokenFilter.get("my_word_delimiter"); String source = "PowerShot 500-42 wi-fi wi-fi-4000 j2se O'Neil's"; - String[] expected = new String[] { "PowerShot", "500", "42", "wifi", "wifi", "4000", "j", - "2", "se", "ONeil" }; + String[] expected = new String[] { "PowerShot", "500", "42", "wifi", "wifi", "4000", "j", "2", "se", "ONeil" }; Tokenizer tokenizer = new WhitespaceTokenizer(); tokenizer.setReader(new StringReader(source)); assertTokenStreamContents(tokenFilter.create(tokenizer), expected); @@ -67,17 +66,17 @@ public void testCatenateWords() throws IOException { public void testCatenateNumbers() throws IOException { ESTestCase.TestAnalysis analysis = AnalysisTestsHelper.createTestAnalysisFromSettings( - Settings.builder() - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .put("index.analysis.filter.my_word_delimiter.type", type) - .put("index.analysis.filter.my_word_delimiter.generate_number_parts", "false") - .put("index.analysis.filter.my_word_delimiter.catenate_numbers", "true") - .build(), - new CommonAnalysisPlugin()); + Settings.builder() + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) + .put("index.analysis.filter.my_word_delimiter.type", type) + .put("index.analysis.filter.my_word_delimiter.generate_number_parts", "false") + .put("index.analysis.filter.my_word_delimiter.catenate_numbers", "true") + .build(), + new CommonAnalysisPlugin() + ); TokenFilterFactory tokenFilter = analysis.tokenFilter.get("my_word_delimiter"); String source = "PowerShot 500-42 wi-fi wi-fi-4000 j2se O'Neil's"; - String[] expected = new String[] { "Power", "Shot", "50042", "wi", "fi", "wi", "fi", "4000", - "j", "2", "se", "O", "Neil" }; + String[] expected = new String[] { "Power", "Shot", "50042", "wi", "fi", "wi", "fi", "4000", "j", "2", "se", "O", "Neil" }; Tokenizer tokenizer = new WhitespaceTokenizer(); tokenizer.setReader(new StringReader(source)); assertTokenStreamContents(tokenFilter.create(tokenizer), expected); @@ -85,17 +84,18 @@ public void testCatenateNumbers() throws IOException { public void testCatenateAll() throws IOException { ESTestCase.TestAnalysis analysis = AnalysisTestsHelper.createTestAnalysisFromSettings( - Settings.builder() - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .put("index.analysis.filter.my_word_delimiter.type", type) - .put("index.analysis.filter.my_word_delimiter.generate_word_parts", "false") - .put("index.analysis.filter.my_word_delimiter.generate_number_parts", "false") - .put("index.analysis.filter.my_word_delimiter.catenate_all", "true") - .build(), - new CommonAnalysisPlugin()); + Settings.builder() + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) + .put("index.analysis.filter.my_word_delimiter.type", type) + .put("index.analysis.filter.my_word_delimiter.generate_word_parts", "false") + .put("index.analysis.filter.my_word_delimiter.generate_number_parts", "false") + .put("index.analysis.filter.my_word_delimiter.catenate_all", "true") + .build(), + new CommonAnalysisPlugin() + ); TokenFilterFactory tokenFilter = analysis.tokenFilter.get("my_word_delimiter"); String source = "PowerShot 500-42 wi-fi wi-fi-4000 j2se O'Neil's"; - String[] expected = new String[]{"PowerShot", "50042", "wifi", "wifi4000", "j2se", "ONeil"}; + String[] expected = new String[] { "PowerShot", "50042", "wifi", "wifi4000", "j2se", "ONeil" }; Tokenizer tokenizer = new WhitespaceTokenizer(); tokenizer.setReader(new StringReader(source)); assertTokenStreamContents(tokenFilter.create(tokenizer), expected); @@ -103,15 +103,16 @@ public void testCatenateAll() throws IOException { public void testSplitOnCaseChange() throws IOException { ESTestCase.TestAnalysis analysis = AnalysisTestsHelper.createTestAnalysisFromSettings( - Settings.builder() - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .put("index.analysis.filter.my_word_delimiter.type", type) - .put("index.analysis.filter.my_word_delimiter.split_on_case_change", "false") - .build(), - new CommonAnalysisPlugin()); + Settings.builder() + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) + .put("index.analysis.filter.my_word_delimiter.type", type) + .put("index.analysis.filter.my_word_delimiter.split_on_case_change", "false") + .build(), + new CommonAnalysisPlugin() + ); TokenFilterFactory tokenFilter = analysis.tokenFilter.get("my_word_delimiter"); String source = "PowerShot"; - String[] expected = new String[]{"PowerShot"}; + String[] expected = new String[] { "PowerShot" }; Tokenizer tokenizer = new WhitespaceTokenizer(); tokenizer.setReader(new StringReader(source)); assertTokenStreamContents(tokenFilter.create(tokenizer), expected); @@ -119,17 +120,36 @@ public void testSplitOnCaseChange() throws IOException { public void testPreserveOriginal() throws IOException { ESTestCase.TestAnalysis analysis = AnalysisTestsHelper.createTestAnalysisFromSettings( - Settings.builder() - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .put("index.analysis.filter.my_word_delimiter.type", type) - .put("index.analysis.filter.my_word_delimiter.preserve_original", "true") - .build(), - new CommonAnalysisPlugin()); + Settings.builder() + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) + .put("index.analysis.filter.my_word_delimiter.type", type) + .put("index.analysis.filter.my_word_delimiter.preserve_original", "true") + .build(), + new CommonAnalysisPlugin() + ); TokenFilterFactory tokenFilter = analysis.tokenFilter.get("my_word_delimiter"); String source = "PowerShot 500-42 wi-fi wi-fi-4000 j2se O'Neil's"; - String[] expected = new String[] { "PowerShot", "Power", "Shot", "500-42", "500", "42", - "wi-fi", "wi", "fi", "wi-fi-4000", "wi", "fi", "4000", "j2se", "j", "2", "se", - "O'Neil's", "O", "Neil" }; + String[] expected = new String[] { + "PowerShot", + "Power", + "Shot", + "500-42", + "500", + "42", + "wi-fi", + "wi", + "fi", + "wi-fi-4000", + "wi", + "fi", + "4000", + "j2se", + "j", + "2", + "se", + "O'Neil's", + "O", + "Neil" }; Tokenizer tokenizer = new WhitespaceTokenizer(); tokenizer.setReader(new StringReader(source)); assertTokenStreamContents(tokenFilter.create(tokenizer), expected); @@ -137,16 +157,16 @@ public void testPreserveOriginal() throws IOException { public void testStemEnglishPossessive() throws IOException { ESTestCase.TestAnalysis analysis = AnalysisTestsHelper.createTestAnalysisFromSettings( - Settings.builder() - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .put("index.analysis.filter.my_word_delimiter.type", type) - .put("index.analysis.filter.my_word_delimiter.stem_english_possessive", "false") - .build(), - new CommonAnalysisPlugin()); + Settings.builder() + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) + .put("index.analysis.filter.my_word_delimiter.type", type) + .put("index.analysis.filter.my_word_delimiter.stem_english_possessive", "false") + .build(), + new CommonAnalysisPlugin() + ); TokenFilterFactory tokenFilter = analysis.tokenFilter.get("my_word_delimiter"); String source = "PowerShot 500-42 wi-fi wi-fi-4000 j2se O'Neil's"; - String[] expected = new String[] { "Power", "Shot", "500", "42", "wi", "fi", "wi", "fi", - "4000", "j", "2", "se", "O", "Neil", "s" }; + String[] expected = new String[] { "Power", "Shot", "500", "42", "wi", "fi", "wi", "fi", "4000", "j", "2", "se", "O", "Neil", "s" }; Tokenizer tokenizer = new WhitespaceTokenizer(); tokenizer.setReader(new StringReader(source)); assertTokenStreamContents(tokenFilter.create(tokenizer), expected); diff --git a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/CJKFilterFactoryTests.java b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/CJKFilterFactoryTests.java index 3511cbe371761..af9c6b79882e1 100644 --- a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/CJKFilterFactoryTests.java +++ b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/CJKFilterFactoryTests.java @@ -10,10 +10,10 @@ import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.Tokenizer; -import org.elasticsearch.lucene.analysis.miscellaneous.DisableGraphAttribute; import org.apache.lucene.analysis.standard.StandardTokenizer; import org.elasticsearch.index.analysis.AnalysisTestsHelper; import org.elasticsearch.index.analysis.TokenFilterFactory; +import org.elasticsearch.lucene.analysis.miscellaneous.DisableGraphAttribute; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.ESTokenStreamTestCase; import org.junit.Before; @@ -34,7 +34,7 @@ public void setup() throws IOException { public void testDefault() throws IOException { TokenFilterFactory tokenFilter = analysis.tokenFilter.get("cjk_bigram"); String source = "多くの学生が試験に落ちた。"; - String[] expected = new String[]{"多く", "くの", "の学", "学生", "生が", "が試", "試験", "験に", "に落", "落ち", "ちた" }; + String[] expected = new String[] { "多く", "くの", "の学", "学生", "生が", "が試", "試験", "験に", "に落", "落ち", "ちた" }; Tokenizer tokenizer = new StandardTokenizer(); tokenizer.setReader(new StringReader(source)); assertTokenStreamContents(tokenFilter.create(tokenizer), expected); @@ -43,7 +43,7 @@ public void testDefault() throws IOException { public void testNoFlags() throws IOException { TokenFilterFactory tokenFilter = analysis.tokenFilter.get("cjk_no_flags"); String source = "多くの学生が試験に落ちた。"; - String[] expected = new String[]{"多く", "くの", "の学", "学生", "生が", "が試", "試験", "験に", "に落", "落ち", "ちた" }; + String[] expected = new String[] { "多く", "くの", "の学", "学生", "生が", "が試", "試験", "験に", "に落", "落ち", "ちた" }; Tokenizer tokenizer = new StandardTokenizer(); tokenizer.setReader(new StringReader(source)); assertTokenStreamContents(tokenFilter.create(tokenizer), expected); @@ -52,7 +52,7 @@ public void testNoFlags() throws IOException { public void testHanOnly() throws IOException { TokenFilterFactory tokenFilter = analysis.tokenFilter.get("cjk_han_only"); String source = "多くの学生が試験に落ちた。"; - String[] expected = new String[]{"多", "く", "の", "学生", "が", "試験", "に", "落", "ち", "た" }; + String[] expected = new String[] { "多", "く", "の", "学生", "が", "試験", "に", "落", "ち", "た" }; Tokenizer tokenizer = new StandardTokenizer(); tokenizer.setReader(new StringReader(source)); assertTokenStreamContents(tokenFilter.create(tokenizer), expected); @@ -61,7 +61,7 @@ public void testHanOnly() throws IOException { public void testHanUnigramOnly() throws IOException { TokenFilterFactory tokenFilter = analysis.tokenFilter.get("cjk_han_unigram_only"); String source = "多くの学生が試験に落ちた。"; - String[] expected = new String[]{"多", "く", "の", "学", "学生", "生", "が", "試", "試験", "験", "に", "落", "ち", "た" }; + String[] expected = new String[] { "多", "く", "の", "学", "学生", "生", "が", "試", "試験", "験", "に", "落", "ち", "た" }; Tokenizer tokenizer = new StandardTokenizer(); tokenizer.setReader(new StringReader(source)); assertTokenStreamContents(tokenFilter.create(tokenizer), expected); diff --git a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/CharGroupTokenizerFactoryTests.java b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/CharGroupTokenizerFactoryTests.java index c8792aa5f5d56..31c086cfe4ef8 100644 --- a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/CharGroupTokenizerFactoryTests.java +++ b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/CharGroupTokenizerFactoryTests.java @@ -9,6 +9,7 @@ package org.elasticsearch.analysis.common; import com.carrotsearch.randomizedtesting.generators.RandomStrings; + import org.apache.lucene.analysis.Tokenizer; import org.apache.lucene.analysis.util.CharTokenizer; import org.elasticsearch.common.settings.Settings; @@ -30,23 +31,25 @@ public void testParseTokenChars() { IndexSettings indexProperties = IndexSettingsModule.newIndexSettings(index, indexSettings); final String name = "cg"; for (String[] conf : Arrays.asList( - new String[] { "\\v" }, - new String[] { "\\u00245" }, - new String[] { "commas" }, - new String[] { "a", "b", "c", "\\$" })) { + new String[] { "\\v" }, + new String[] { "\\u00245" }, + new String[] { "commas" }, + new String[] { "a", "b", "c", "\\$" } + )) { final Settings settings = newAnalysisSettingsBuilder().putList("tokenize_on_chars", conf).build(); expectThrows(RuntimeException.class, () -> new CharGroupTokenizerFactory(indexProperties, null, name, settings).create()); } for (String[] conf : Arrays.asList( - new String[0], - new String[] { "\\n" }, - new String[] { "\\u0024" }, - new String[] { "whitespace" }, - new String[] { "a", "b", "c" }, - new String[] { "a", "b", "c", "\\r" }, - new String[] { "\\r" }, - new String[] { "f", "o", "o", "symbol" })) { + new String[0], + new String[] { "\\n" }, + new String[] { "\\u0024" }, + new String[] { "whitespace" }, + new String[] { "a", "b", "c" }, + new String[] { "a", "b", "c", "\\r" }, + new String[] { "\\r" }, + new String[] { "f", "o", "o", "symbol" } + )) { final Settings settings = newAnalysisSettingsBuilder().putList("tokenize_on_chars", Arrays.asList(conf)).build(); new CharGroupTokenizerFactory(indexProperties, null, name, settings).create(); // no exception @@ -59,22 +62,21 @@ public void testMaxTokenLength() throws IOException { IndexSettings indexProperties = IndexSettingsModule.newIndexSettings(index, indexSettings); final String name = "cg"; - String[] conf = new String[] {"-"}; + String[] conf = new String[] { "-" }; - final Settings defaultLengthSettings = newAnalysisSettingsBuilder() - .putList("tokenize_on_chars", conf) - .build(); + final Settings defaultLengthSettings = newAnalysisSettingsBuilder().putList("tokenize_on_chars", conf).build(); CharTokenizer tokenizer = (CharTokenizer) new CharGroupTokenizerFactory(indexProperties, null, name, defaultLengthSettings) .create(); String textWithVeryLongToken = RandomStrings.randomAsciiAlphanumOfLength(random(), 256).concat("-trailing"); try (Reader reader = new StringReader(textWithVeryLongToken)) { tokenizer.setReader(reader); - assertTokenStreamContents(tokenizer, new String[] { textWithVeryLongToken.substring(0, 255), - textWithVeryLongToken.substring(255, 256), "trailing"}); + assertTokenStreamContents( + tokenizer, + new String[] { textWithVeryLongToken.substring(0, 255), textWithVeryLongToken.substring(255, 256), "trailing" } + ); } - final Settings analysisSettings = newAnalysisSettingsBuilder() - .putList("tokenize_on_chars", conf) + final Settings analysisSettings = newAnalysisSettingsBuilder().putList("tokenize_on_chars", conf) .put("max_token_length", 2) .build(); tokenizer = (CharTokenizer) new CharGroupTokenizerFactory(indexProperties, null, name, analysisSettings).create(); @@ -83,20 +85,22 @@ public void testMaxTokenLength() throws IOException { assertTokenStreamContents(tokenizer, new String[] { "on", "e", "tw", "o", "th", "re", "e" }); } - final Settings tooLongLengthSettings = newAnalysisSettingsBuilder() - .putList("tokenize_on_chars", conf) + final Settings tooLongLengthSettings = newAnalysisSettingsBuilder().putList("tokenize_on_chars", conf) .put("max_token_length", 1024 * 1024 + 1) .build(); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> new CharGroupTokenizerFactory(indexProperties, null, name, tooLongLengthSettings).create()); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> new CharGroupTokenizerFactory(indexProperties, null, name, tooLongLengthSettings).create() + ); assertEquals("maxTokenLen must be greater than 0 and less than 1048576 passed: 1048577", e.getMessage()); - final Settings negativeLengthSettings = newAnalysisSettingsBuilder() - .putList("tokenize_on_chars", conf) + final Settings negativeLengthSettings = newAnalysisSettingsBuilder().putList("tokenize_on_chars", conf) .put("max_token_length", -1) .build(); - e = expectThrows(IllegalArgumentException.class, - () -> new CharGroupTokenizerFactory(indexProperties, null, name, negativeLengthSettings).create()); + e = expectThrows( + IllegalArgumentException.class, + () -> new CharGroupTokenizerFactory(indexProperties, null, name, negativeLengthSettings).create() + ); assertEquals("maxTokenLen must be greater than 0 and less than 1048576 passed: -1", e.getMessage()); } @@ -105,9 +109,13 @@ public void testTokenization() throws IOException { final String name = "cg"; final Settings indexSettings = newAnalysisSettingsBuilder().build(); final Settings settings = newAnalysisSettingsBuilder().putList("tokenize_on_chars", "whitespace", ":", "\\u0024").build(); - Tokenizer tokenizer = new CharGroupTokenizerFactory(IndexSettingsModule.newIndexSettings(index, indexSettings), - null, name, settings).create(); + Tokenizer tokenizer = new CharGroupTokenizerFactory( + IndexSettingsModule.newIndexSettings(index, indexSettings), + null, + name, + settings + ).create(); tokenizer.setReader(new StringReader("foo bar $34 test:test2")); - assertTokenStreamContents(tokenizer, new String[] {"foo", "bar", "34", "test", "test2"}); + assertTokenStreamContents(tokenizer, new String[] { "foo", "bar", "34", "test", "test2" }); } } diff --git a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/CommonAnalysisFactoryTests.java b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/CommonAnalysisFactoryTests.java index de8c4d1d0f927..fb7cdc0938c2a 100644 --- a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/CommonAnalysisFactoryTests.java +++ b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/CommonAnalysisFactoryTests.java @@ -143,13 +143,13 @@ protected Map> getTokenFilters() { @Override protected Map> getCharFilters() { Map> filters = new TreeMap<>(super.getCharFilters()); - filters.put("htmlstrip", HtmlStripCharFilterFactory.class); - filters.put("mapping", MappingCharFilterFactory.class); + filters.put("htmlstrip", HtmlStripCharFilterFactory.class); + filters.put("mapping", MappingCharFilterFactory.class); filters.put("patternreplace", PatternReplaceCharFilterFactory.class); // TODO: these charfilters are not yet exposed: useful? // handling of zwnj for persian - filters.put("persian", Void.class); + filters.put("persian", Void.class); return filters; } @@ -260,12 +260,16 @@ public void testAllTokenFiltersMarked() { } private void markedTestCase(String name, Map> map) { - List unmarked = map.entrySet().stream() - .filter(e -> e.getValue() == MovedToAnalysisCommon.class) - .map(Map.Entry::getKey) - .sorted() - .collect(toList()); - assertEquals(name + " marked in AnalysisFactoryTestCase as moved to analysis-common " - + "but not mapped here", emptyList(), unmarked); + List unmarked = map.entrySet() + .stream() + .filter(e -> e.getValue() == MovedToAnalysisCommon.class) + .map(Map.Entry::getKey) + .sorted() + .collect(toList()); + assertEquals( + name + " marked in AnalysisFactoryTestCase as moved to analysis-common " + "but not mapped here", + emptyList(), + unmarked + ); } } diff --git a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/CommonAnalysisPluginTests.java b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/CommonAnalysisPluginTests.java index bb9dcc992757b..8e587816d61b0 100644 --- a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/CommonAnalysisPluginTests.java +++ b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/CommonAnalysisPluginTests.java @@ -28,9 +28,9 @@ public class CommonAnalysisPluginTests extends ESTestCase { * logs a warning for earlier indices when the filter is used as a custom filter */ public void testNGramFilterInCustomAnalyzerDeprecationError() throws IOException { - final Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()) - .put(IndexMetadata.SETTING_VERSION_CREATED, - VersionUtils.randomVersionBetween(random(), Version.V_8_0_0, Version.CURRENT)) + final Settings settings = Settings.builder() + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()) + .put(IndexMetadata.SETTING_VERSION_CREATED, VersionUtils.randomVersionBetween(random(), Version.V_8_0_0, Version.CURRENT)) .put("index.analysis.analyzer.custom_analyzer.type", "custom") .put("index.analysis.analyzer.custom_analyzer.tokenizer", "standard") .putList("index.analysis.analyzer.custom_analyzer.filter", "my_ngram") @@ -38,22 +38,31 @@ public void testNGramFilterInCustomAnalyzerDeprecationError() throws IOException .build(); try (CommonAnalysisPlugin commonAnalysisPlugin = new CommonAnalysisPlugin()) { - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, - () -> createTestAnalysis(IndexSettingsModule.newIndexSettings("index", settings), settings, commonAnalysisPlugin)); - assertEquals("The [nGram] token filter name was deprecated in 6.4 and cannot be used in new indices. " - + "Please change the filter name to [ngram] instead.", ex.getMessage()); + IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> createTestAnalysis(IndexSettingsModule.newIndexSettings("index", settings), settings, commonAnalysisPlugin) + ); + assertEquals( + "The [nGram] token filter name was deprecated in 6.4 and cannot be used in new indices. " + + "Please change the filter name to [ngram] instead.", + ex.getMessage() + ); } - final Settings settingsPre7 = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()) - .put(IndexMetadata.SETTING_VERSION_CREATED, VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, Version.V_7_6_0)) - .put("index.analysis.analyzer.custom_analyzer.type", "custom") - .put("index.analysis.analyzer.custom_analyzer.tokenizer", "standard") - .putList("index.analysis.analyzer.custom_analyzer.filter", "my_ngram").put("index.analysis.filter.my_ngram.type", "nGram") - .build(); + final Settings settingsPre7 = Settings.builder() + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()) + .put(IndexMetadata.SETTING_VERSION_CREATED, VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, Version.V_7_6_0)) + .put("index.analysis.analyzer.custom_analyzer.type", "custom") + .put("index.analysis.analyzer.custom_analyzer.tokenizer", "standard") + .putList("index.analysis.analyzer.custom_analyzer.filter", "my_ngram") + .put("index.analysis.filter.my_ngram.type", "nGram") + .build(); try (CommonAnalysisPlugin commonAnalysisPlugin = new CommonAnalysisPlugin()) { createTestAnalysis(IndexSettingsModule.newIndexSettings("index", settingsPre7), settingsPre7, commonAnalysisPlugin); - assertWarnings("The [nGram] token filter name is deprecated and will be removed in a future version. " - + "Please change the filter name to [ngram] instead."); + assertWarnings( + "The [nGram] token filter name is deprecated and will be removed in a future version. " + + "Please change the filter name to [ngram] instead." + ); } } @@ -62,9 +71,9 @@ public void testNGramFilterInCustomAnalyzerDeprecationError() throws IOException * logs a warning for earlier indices when the filter is used as a custom filter */ public void testEdgeNGramFilterInCustomAnalyzerDeprecationError() throws IOException { - final Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()) - .put(IndexMetadata.SETTING_VERSION_CREATED, - VersionUtils.randomVersionBetween(random(), Version.V_8_0_0, Version.CURRENT)) + final Settings settings = Settings.builder() + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()) + .put(IndexMetadata.SETTING_VERSION_CREATED, VersionUtils.randomVersionBetween(random(), Version.V_8_0_0, Version.CURRENT)) .put("index.analysis.analyzer.custom_analyzer.type", "custom") .put("index.analysis.analyzer.custom_analyzer.tokenizer", "standard") .putList("index.analysis.analyzer.custom_analyzer.filter", "my_ngram") @@ -72,26 +81,32 @@ public void testEdgeNGramFilterInCustomAnalyzerDeprecationError() throws IOExcep .build(); try (CommonAnalysisPlugin commonAnalysisPlugin = new CommonAnalysisPlugin()) { - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, - () -> createTestAnalysis(IndexSettingsModule.newIndexSettings("index", settings), settings, commonAnalysisPlugin)); - assertEquals("The [edgeNGram] token filter name was deprecated in 6.4 and cannot be used in new indices. " - + "Please change the filter name to [edge_ngram] instead.", ex.getMessage()); + IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> createTestAnalysis(IndexSettingsModule.newIndexSettings("index", settings), settings, commonAnalysisPlugin) + ); + assertEquals( + "The [edgeNGram] token filter name was deprecated in 6.4 and cannot be used in new indices. " + + "Please change the filter name to [edge_ngram] instead.", + ex.getMessage() + ); } - final Settings settingsPre7 = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()) - .put(IndexMetadata.SETTING_VERSION_CREATED, - VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, Version.V_7_6_0)) - .put("index.analysis.analyzer.custom_analyzer.type", "custom") - .put("index.analysis.analyzer.custom_analyzer.tokenizer", "standard") - .putList("index.analysis.analyzer.custom_analyzer.filter", "my_ngram") - .put("index.analysis.filter.my_ngram.type", "edgeNGram") - .build(); + final Settings settingsPre7 = Settings.builder() + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()) + .put(IndexMetadata.SETTING_VERSION_CREATED, VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, Version.V_7_6_0)) + .put("index.analysis.analyzer.custom_analyzer.type", "custom") + .put("index.analysis.analyzer.custom_analyzer.tokenizer", "standard") + .putList("index.analysis.analyzer.custom_analyzer.filter", "my_ngram") + .put("index.analysis.filter.my_ngram.type", "edgeNGram") + .build(); try (CommonAnalysisPlugin commonAnalysisPlugin = new CommonAnalysisPlugin()) { - createTestAnalysis(IndexSettingsModule.newIndexSettings("index", settingsPre7), - settingsPre7, commonAnalysisPlugin); - assertWarnings("The [edgeNGram] token filter name is deprecated and will be removed in a future version. " - + "Please change the filter name to [edge_ngram] instead."); + createTestAnalysis(IndexSettingsModule.newIndexSettings("index", settingsPre7), settingsPre7, commonAnalysisPlugin); + assertWarnings( + "The [edgeNGram] token filter name is deprecated and will be removed in a future version. " + + "Please change the filter name to [edge_ngram] instead." + ); } } @@ -101,74 +116,162 @@ public void testEdgeNGramFilterInCustomAnalyzerDeprecationError() throws IOExcep */ public void testNGramTokenizerDeprecation() throws IOException { // tests for prebuilt tokenizer - doTestPrebuiltTokenizerDeprecation("nGram", "ngram", - VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, Version.V_7_5_2), false); - doTestPrebuiltTokenizerDeprecation("edgeNGram", "edge_ngram", - VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, Version.V_7_5_2), false); - doTestPrebuiltTokenizerDeprecation("nGram", "ngram", - VersionUtils.randomVersionBetween(random(), Version.V_7_6_0, - Version.max(Version.V_7_6_0, VersionUtils.getPreviousVersion(Version.V_8_0_0))), - true); - doTestPrebuiltTokenizerDeprecation("edgeNGram", "edge_ngram", - VersionUtils.randomVersionBetween(random(), Version.V_7_6_0, - Version.max(Version.V_7_6_0, VersionUtils.getPreviousVersion(Version.V_8_0_0))), true); - expectThrows(IllegalArgumentException.class, () -> doTestPrebuiltTokenizerDeprecation("nGram", "ngram", - VersionUtils.randomVersionBetween(random(), Version.V_8_0_0, Version.CURRENT), true)); - expectThrows(IllegalArgumentException.class, () -> doTestPrebuiltTokenizerDeprecation("edgeNGram", "edge_ngram", - VersionUtils.randomVersionBetween(random(), Version.V_8_0_0, Version.CURRENT), true)); + doTestPrebuiltTokenizerDeprecation( + "nGram", + "ngram", + VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, Version.V_7_5_2), + false + ); + doTestPrebuiltTokenizerDeprecation( + "edgeNGram", + "edge_ngram", + VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, Version.V_7_5_2), + false + ); + doTestPrebuiltTokenizerDeprecation( + "nGram", + "ngram", + VersionUtils.randomVersionBetween( + random(), + Version.V_7_6_0, + Version.max(Version.V_7_6_0, VersionUtils.getPreviousVersion(Version.V_8_0_0)) + ), + true + ); + doTestPrebuiltTokenizerDeprecation( + "edgeNGram", + "edge_ngram", + VersionUtils.randomVersionBetween( + random(), + Version.V_7_6_0, + Version.max(Version.V_7_6_0, VersionUtils.getPreviousVersion(Version.V_8_0_0)) + ), + true + ); + expectThrows( + IllegalArgumentException.class, + () -> doTestPrebuiltTokenizerDeprecation( + "nGram", + "ngram", + VersionUtils.randomVersionBetween(random(), Version.V_8_0_0, Version.CURRENT), + true + ) + ); + expectThrows( + IllegalArgumentException.class, + () -> doTestPrebuiltTokenizerDeprecation( + "edgeNGram", + "edge_ngram", + VersionUtils.randomVersionBetween(random(), Version.V_8_0_0, Version.CURRENT), + true + ) + ); // same batch of tests for custom tokenizer definition in the settings - doTestCustomTokenizerDeprecation("nGram", "ngram", - VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, Version.V_7_5_2), false); - doTestCustomTokenizerDeprecation("edgeNGram", "edge_ngram", - VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, Version.V_7_5_2), false); - doTestCustomTokenizerDeprecation("nGram", "ngram", - VersionUtils.randomVersionBetween(random(), Version.V_7_6_0, - Version.max(Version.V_7_6_0, VersionUtils.getPreviousVersion(Version.V_8_0_0))), - true); - doTestCustomTokenizerDeprecation("edgeNGram", "edge_ngram", - VersionUtils.randomVersionBetween(random(), Version.V_7_6_0, - Version.max(Version.V_7_6_0, VersionUtils.getPreviousVersion(Version.V_8_0_0))), true); - expectThrows(IllegalArgumentException.class, () -> doTestCustomTokenizerDeprecation("nGram", "ngram", - VersionUtils.randomVersionBetween(random(), Version.V_8_0_0, Version.CURRENT), true)); - expectThrows(IllegalArgumentException.class, () -> doTestCustomTokenizerDeprecation("edgeNGram", "edge_ngram", - VersionUtils.randomVersionBetween(random(), Version.V_8_0_0, Version.CURRENT), true)); + doTestCustomTokenizerDeprecation( + "nGram", + "ngram", + VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, Version.V_7_5_2), + false + ); + doTestCustomTokenizerDeprecation( + "edgeNGram", + "edge_ngram", + VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, Version.V_7_5_2), + false + ); + doTestCustomTokenizerDeprecation( + "nGram", + "ngram", + VersionUtils.randomVersionBetween( + random(), + Version.V_7_6_0, + Version.max(Version.V_7_6_0, VersionUtils.getPreviousVersion(Version.V_8_0_0)) + ), + true + ); + doTestCustomTokenizerDeprecation( + "edgeNGram", + "edge_ngram", + VersionUtils.randomVersionBetween( + random(), + Version.V_7_6_0, + Version.max(Version.V_7_6_0, VersionUtils.getPreviousVersion(Version.V_8_0_0)) + ), + true + ); + expectThrows( + IllegalArgumentException.class, + () -> doTestCustomTokenizerDeprecation( + "nGram", + "ngram", + VersionUtils.randomVersionBetween(random(), Version.V_8_0_0, Version.CURRENT), + true + ) + ); + expectThrows( + IllegalArgumentException.class, + () -> doTestCustomTokenizerDeprecation( + "edgeNGram", + "edge_ngram", + VersionUtils.randomVersionBetween(random(), Version.V_8_0_0, Version.CURRENT), + true + ) + ); } public void doTestPrebuiltTokenizerDeprecation(String deprecatedName, String replacement, Version version, boolean expectWarning) - throws IOException { - final Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()) - .put(IndexMetadata.SETTING_VERSION_CREATED, version).build(); + throws IOException { + final Settings settings = Settings.builder() + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()) + .put(IndexMetadata.SETTING_VERSION_CREATED, version) + .build(); try (CommonAnalysisPlugin commonAnalysisPlugin = new CommonAnalysisPlugin()) { Map tokenizers = createTestAnalysis( - IndexSettingsModule.newIndexSettings("index", settings), settings, commonAnalysisPlugin).tokenizer; + IndexSettingsModule.newIndexSettings("index", settings), + settings, + commonAnalysisPlugin + ).tokenizer; TokenizerFactory tokenizerFactory = tokenizers.get(deprecatedName); Tokenizer tokenizer = tokenizerFactory.create(); assertNotNull(tokenizer); if (expectWarning) { - assertWarnings("The [" + deprecatedName + "] tokenizer name is deprecated and will be removed in a future version. " - + "Please change the tokenizer name to [" + replacement + "] instead."); + assertWarnings( + "The [" + + deprecatedName + + "] tokenizer name is deprecated and will be removed in a future version. " + + "Please change the tokenizer name to [" + + replacement + + "] instead." + ); } } } public void doTestCustomTokenizerDeprecation(String deprecatedName, String replacement, Version version, boolean expectWarning) - throws IOException { - final Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()) + throws IOException { + final Settings settings = Settings.builder() + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()) .put(IndexMetadata.SETTING_VERSION_CREATED, version) .put("index.analysis.analyzer.custom_analyzer.type", "custom") .put("index.analysis.analyzer.custom_analyzer.tokenizer", "my_tokenizer") .put("index.analysis.tokenizer.my_tokenizer.type", deprecatedName) - .build(); + .build(); try (CommonAnalysisPlugin commonAnalysisPlugin = new CommonAnalysisPlugin()) { createTestAnalysis(IndexSettingsModule.newIndexSettings("index", settings), settings, commonAnalysisPlugin); if (expectWarning) { - assertWarnings("The [" + deprecatedName + "] tokenizer name is deprecated and will be removed in a future version. " - + "Please change the tokenizer name to [" + replacement + "] instead."); + assertWarnings( + "The [" + + deprecatedName + + "] tokenizer name is deprecated and will be removed in a future version. " + + "Please change the tokenizer name to [" + + replacement + + "] instead." + ); } } } diff --git a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/CommonGramsTokenFilterFactoryTests.java b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/CommonGramsTokenFilterFactoryTests.java index cd5748b7a8cc5..e62910d4d1bbd 100644 --- a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/CommonGramsTokenFilterFactoryTests.java +++ b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/CommonGramsTokenFilterFactoryTests.java @@ -29,25 +29,25 @@ public class CommonGramsTokenFilterFactoryTests extends ESTokenStreamTestCase { public void testDefault() throws IOException { Settings settings = Settings.builder() - .put("index.analysis.filter.common_grams_default.type", "common_grams") - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .build(); + .put("index.analysis.filter.common_grams_default.type", "common_grams") + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) + .build(); try { AnalysisTestsHelper.createTestAnalysisFromSettings(settings, new CommonAnalysisPlugin()); Assert.fail("[common_words] or [common_words_path] is set"); - } catch (IllegalArgumentException e) { - } catch (IOException e) { + } catch (IllegalArgumentException e) {} catch (IOException e) { fail("expected IAE"); } } public void testWithoutCommonWordsMatch() throws IOException { { - Settings settings = Settings.builder().put("index.analysis.filter.common_grams_default.type", "common_grams") - .putList("index.analysis.filter.common_grams_default.common_words", "chromosome", "protein") - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .build(); + Settings settings = Settings.builder() + .put("index.analysis.filter.common_grams_default.type", "common_grams") + .putList("index.analysis.filter.common_grams_default.common_words", "chromosome", "protein") + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) + .build(); ESTestCase.TestAnalysis analysis = createTestAnalysisFromSettings(settings); { @@ -61,11 +61,12 @@ public void testWithoutCommonWordsMatch() throws IOException { } { - Settings settings = Settings.builder().put("index.analysis.filter.common_grams_default.type", "common_grams") - .put("index.analysis.filter.common_grams_default.query_mode", false) - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .putList("index.analysis.filter.common_grams_default.common_words", "chromosome", "protein") - .build(); + Settings settings = Settings.builder() + .put("index.analysis.filter.common_grams_default.type", "common_grams") + .put("index.analysis.filter.common_grams_default.query_mode", false) + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) + .putList("index.analysis.filter.common_grams_default.common_words", "chromosome", "protein") + .build(); ESTestCase.TestAnalysis analysis = createTestAnalysisFromSettings(settings); { TokenFilterFactory tokenFilter = analysis.tokenFilter.get("common_grams_default"); @@ -80,45 +81,85 @@ public void testWithoutCommonWordsMatch() throws IOException { public void testSettings() throws IOException { { - Settings settings = Settings.builder().put("index.analysis.filter.common_grams_1.type", "common_grams") - .put("index.analysis.filter.common_grams_1.ignore_case", true) - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .putList("index.analysis.filter.common_grams_1.common_words", "the", "Or", "Not", "a", "is", "an", "they", "are") - .build(); + Settings settings = Settings.builder() + .put("index.analysis.filter.common_grams_1.type", "common_grams") + .put("index.analysis.filter.common_grams_1.ignore_case", true) + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) + .putList("index.analysis.filter.common_grams_1.common_words", "the", "Or", "Not", "a", "is", "an", "they", "are") + .build(); ESTestCase.TestAnalysis analysis = createTestAnalysisFromSettings(settings); TokenFilterFactory tokenFilter = analysis.tokenFilter.get("common_grams_1"); String source = "the quick brown is a fox or noT"; - String[] expected = new String[] { "the", "the_quick", "quick", "brown", "brown_is", "is", "is_a", "a", - "a_fox", "fox", "fox_or", "or", "or_noT", "noT" }; + String[] expected = new String[] { + "the", + "the_quick", + "quick", + "brown", + "brown_is", + "is", + "is_a", + "a", + "a_fox", + "fox", + "fox_or", + "or", + "or_noT", + "noT" }; Tokenizer tokenizer = new WhitespaceTokenizer(); tokenizer.setReader(new StringReader(source)); assertTokenStreamContents(tokenFilter.create(tokenizer), expected); } { - Settings settings = Settings.builder().put("index.analysis.filter.common_grams_2.type", "common_grams") - .put("index.analysis.filter.common_grams_2.ignore_case", false) - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .putList("index.analysis.filter.common_grams_2.common_words", "the", "Or", "noT", "a", "is", "an", "they", "are") - .build(); + Settings settings = Settings.builder() + .put("index.analysis.filter.common_grams_2.type", "common_grams") + .put("index.analysis.filter.common_grams_2.ignore_case", false) + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) + .putList("index.analysis.filter.common_grams_2.common_words", "the", "Or", "noT", "a", "is", "an", "they", "are") + .build(); ESTestCase.TestAnalysis analysis = createTestAnalysisFromSettings(settings); TokenFilterFactory tokenFilter = analysis.tokenFilter.get("common_grams_2"); String source = "the quick brown is a fox or why noT"; - String[] expected = new String[] { "the", "the_quick", "quick", "brown", "brown_is", "is", "is_a", "a", "" + - "a_fox", "fox", "or", "why", "why_noT", "noT" }; + String[] expected = new String[] { + "the", + "the_quick", + "quick", + "brown", + "brown_is", + "is", + "is_a", + "a", + "" + "a_fox", + "fox", + "or", + "why", + "why_noT", + "noT" }; Tokenizer tokenizer = new WhitespaceTokenizer(); tokenizer.setReader(new StringReader(source)); assertTokenStreamContents(tokenFilter.create(tokenizer), expected); } { - Settings settings = Settings.builder().put("index.analysis.filter.common_grams_3.type", "common_grams") - .putList("index.analysis.filter.common_grams_3.common_words", "the", "or", "not", "a", "is", "an", "they", "are") - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .build(); + Settings settings = Settings.builder() + .put("index.analysis.filter.common_grams_3.type", "common_grams") + .putList("index.analysis.filter.common_grams_3.common_words", "the", "or", "not", "a", "is", "an", "they", "are") + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) + .build(); ESTestCase.TestAnalysis analysis = createTestAnalysisFromSettings(settings); TokenFilterFactory tokenFilter = analysis.tokenFilter.get("common_grams_3"); String source = "the quick brown is a fox Or noT"; - String[] expected = new String[] { "the", "the_quick", "quick", "brown", "brown_is", "is", "is_a", "a", - "a_fox", "fox", "Or", "noT" }; + String[] expected = new String[] { + "the", + "the_quick", + "quick", + "brown", + "brown_is", + "is", + "is_a", + "a", + "a_fox", + "fox", + "Or", + "noT" }; Tokenizer tokenizer = new WhitespaceTokenizer(); tokenizer.setReader(new StringReader(source)); assertTokenStreamContents(tokenFilter.create(tokenizer), expected); @@ -128,37 +169,58 @@ public void testSettings() throws IOException { public void testCommonGramsAnalysis() throws IOException { String json = "/org/elasticsearch/analysis/common/commongrams.json"; Settings settings = Settings.builder() - .loadFromStream(json, getClass().getResourceAsStream(json), false) - .put(Environment.PATH_HOME_SETTING.getKey(), createHome()) - .build(); + .loadFromStream(json, getClass().getResourceAsStream(json), false) + .put(Environment.PATH_HOME_SETTING.getKey(), createHome()) + .build(); { - IndexAnalyzers indexAnalyzers = createTestAnalysisFromSettings(settings) - .indexAnalyzers; + IndexAnalyzers indexAnalyzers = createTestAnalysisFromSettings(settings).indexAnalyzers; Analyzer analyzer = indexAnalyzers.get("commongramsAnalyzer").analyzer(); String source = "the quick brown is a fox or not"; - String[] expected = new String[] { "the", "quick", "quick_brown", "brown", "brown_is", "is", "a", "a_fox", - "fox", "fox_or", "or", "not" }; + String[] expected = new String[] { + "the", + "quick", + "quick_brown", + "brown", + "brown_is", + "is", + "a", + "a_fox", + "fox", + "fox_or", + "or", + "not" }; assertTokenStreamContents(analyzer.tokenStream("test", source), expected); } { - IndexAnalyzers indexAnalyzers = createTestAnalysisFromSettings(settings) - .indexAnalyzers; + IndexAnalyzers indexAnalyzers = createTestAnalysisFromSettings(settings).indexAnalyzers; Analyzer analyzer = indexAnalyzers.get("commongramsAnalyzer_file").analyzer(); String source = "the quick brown is a fox or not"; - String[] expected = new String[] { "the", "quick", "quick_brown", "brown", "brown_is", "is", "a", "a_fox", - "fox", "fox_or", "or", "not" }; + String[] expected = new String[] { + "the", + "quick", + "quick_brown", + "brown", + "brown_is", + "is", + "a", + "a_fox", + "fox", + "fox_or", + "or", + "not" }; assertTokenStreamContents(analyzer.tokenStream("test", source), expected); } } public void testQueryModeSettings() throws IOException { { - Settings settings = Settings.builder().put("index.analysis.filter.common_grams_1.type", "common_grams") - .put("index.analysis.filter.common_grams_1.query_mode", true) - .putList("index.analysis.filter.common_grams_1.common_words", "the", "Or", "Not", "a", "is", "an", "they", "are") - .put("index.analysis.filter.common_grams_1.ignore_case", true) - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .build(); + Settings settings = Settings.builder() + .put("index.analysis.filter.common_grams_1.type", "common_grams") + .put("index.analysis.filter.common_grams_1.query_mode", true) + .putList("index.analysis.filter.common_grams_1.common_words", "the", "Or", "Not", "a", "is", "an", "they", "are") + .put("index.analysis.filter.common_grams_1.ignore_case", true) + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) + .build(); ESTestCase.TestAnalysis analysis = createTestAnalysisFromSettings(settings); TokenFilterFactory tokenFilter = analysis.tokenFilter.get("common_grams_1"); String source = "the quick brown is a fox or noT"; @@ -168,12 +230,13 @@ public void testQueryModeSettings() throws IOException { assertTokenStreamContents(tokenFilter.create(tokenizer), expected); } { - Settings settings = Settings.builder().put("index.analysis.filter.common_grams_2.type", "common_grams") - .put("index.analysis.filter.common_grams_2.query_mode", true) - .putList("index.analysis.filter.common_grams_2.common_words", "the", "Or", "noT", "a", "is", "an", "they", "are") - .put("index.analysis.filter.common_grams_2.ignore_case", false) - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .build(); + Settings settings = Settings.builder() + .put("index.analysis.filter.common_grams_2.type", "common_grams") + .put("index.analysis.filter.common_grams_2.query_mode", true) + .putList("index.analysis.filter.common_grams_2.common_words", "the", "Or", "noT", "a", "is", "an", "they", "are") + .put("index.analysis.filter.common_grams_2.ignore_case", false) + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) + .build(); ESTestCase.TestAnalysis analysis = createTestAnalysisFromSettings(settings); TokenFilterFactory tokenFilter = analysis.tokenFilter.get("common_grams_2"); String source = "the quick brown is a fox or why noT"; @@ -183,11 +246,12 @@ public void testQueryModeSettings() throws IOException { assertTokenStreamContents(tokenFilter.create(tokenizer), expected); } { - Settings settings = Settings.builder().put("index.analysis.filter.common_grams_3.type", "common_grams") - .put("index.analysis.filter.common_grams_3.query_mode", true) - .putList("index.analysis.filter.common_grams_3.common_words", "the", "Or", "noT", "a", "is", "an", "they", "are") - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .build(); + Settings settings = Settings.builder() + .put("index.analysis.filter.common_grams_3.type", "common_grams") + .put("index.analysis.filter.common_grams_3.query_mode", true) + .putList("index.analysis.filter.common_grams_3.common_words", "the", "Or", "noT", "a", "is", "an", "they", "are") + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) + .build(); ESTestCase.TestAnalysis analysis = createTestAnalysisFromSettings(settings); TokenFilterFactory tokenFilter = analysis.tokenFilter.get("common_grams_3"); String source = "the quick brown is a fox or why noT"; @@ -197,11 +261,12 @@ public void testQueryModeSettings() throws IOException { assertTokenStreamContents(tokenFilter.create(tokenizer), expected); } { - Settings settings = Settings.builder().put("index.analysis.filter.common_grams_4.type", "common_grams") - .put("index.analysis.filter.common_grams_4.query_mode", true) - .putList("index.analysis.filter.common_grams_4.common_words", "the", "or", "not", "a", "is", "an", "they", "are") - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .build(); + Settings settings = Settings.builder() + .put("index.analysis.filter.common_grams_4.type", "common_grams") + .put("index.analysis.filter.common_grams_4.query_mode", true) + .putList("index.analysis.filter.common_grams_4.common_words", "the", "or", "not", "a", "is", "an", "they", "are") + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) + .build(); ESTestCase.TestAnalysis analysis = createTestAnalysisFromSettings(settings); TokenFilterFactory tokenFilter = analysis.tokenFilter.get("common_grams_4"); String source = "the quick brown is a fox Or noT"; @@ -215,20 +280,18 @@ public void testQueryModeSettings() throws IOException { public void testQueryModeCommonGramsAnalysis() throws IOException { String json = "/org/elasticsearch/analysis/common/commongrams_query_mode.json"; Settings settings = Settings.builder() - .loadFromStream(json, getClass().getResourceAsStream(json), false) + .loadFromStream(json, getClass().getResourceAsStream(json), false) .put(Environment.PATH_HOME_SETTING.getKey(), createHome()) - .build(); + .build(); { - IndexAnalyzers indexAnalyzers = createTestAnalysisFromSettings(settings) - .indexAnalyzers; + IndexAnalyzers indexAnalyzers = createTestAnalysisFromSettings(settings).indexAnalyzers; Analyzer analyzer = indexAnalyzers.get("commongramsAnalyzer").analyzer(); String source = "the quick brown is a fox or not"; String[] expected = new String[] { "the", "quick_brown", "brown_is", "is", "a_fox", "fox_or", "or", "not" }; assertTokenStreamContents(analyzer.tokenStream("test", source), expected); } { - IndexAnalyzers indexAnalyzers = createTestAnalysisFromSettings(settings) - .indexAnalyzers; + IndexAnalyzers indexAnalyzers = createTestAnalysisFromSettings(settings).indexAnalyzers; Analyzer analyzer = indexAnalyzers.get("commongramsAnalyzer_file").analyzer(); String source = "the quick brown is a fox or not"; String[] expected = new String[] { "the", "quick_brown", "brown_is", "is", "a_fox", "fox_or", "or", "not" }; diff --git a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/CompoundAnalysisTests.java b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/CompoundAnalysisTests.java index fcbfb65267eb7..615519ba70dde 100644 --- a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/CompoundAnalysisTests.java +++ b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/CompoundAnalysisTests.java @@ -48,12 +48,14 @@ public void testDefaultsCompoundAnalysis() throws Exception { } public void testDictionaryDecompounder() throws Exception { - Settings[] settingsArr = new Settings[]{getJsonSettings(), getYamlSettings()}; + Settings[] settingsArr = new Settings[] { getJsonSettings(), getYamlSettings() }; for (Settings settings : settingsArr) { List terms = analyze(settings, "decompoundingAnalyzer", "donaudampfschiff spargelcremesuppe"); MatcherAssert.assertThat(terms.size(), equalTo(8)); - MatcherAssert.assertThat(terms, - hasItems("donau", "dampf", "schiff", "donaudampfschiff", "spargel", "creme", "suppe", "spargelcremesuppe")); + MatcherAssert.assertThat( + terms, + hasItems("donau", "dampf", "schiff", "donaudampfschiff", "spargel", "creme", "suppe", "spargelcremesuppe") + ); } assertWarnings("Setting [version] on analysis component [custom7] has no effect and is deprecated"); } @@ -64,7 +66,7 @@ private List analyze(Settings settings, String analyzerName, String text IndexAnalyzers indexAnalyzers = analysisModule.getAnalysisRegistry().build(idxSettings); Analyzer analyzer = indexAnalyzers.get(analyzerName).analyzer(); - TokenStream stream = analyzer.tokenStream("" , text); + TokenStream stream = analyzer.tokenStream("", text); stream.reset(); CharTermAttribute termAtt = stream.addAttribute(CharTermAttribute.class); @@ -89,18 +91,18 @@ public Map> getTokenFilters() { private Settings getJsonSettings() throws IOException { String json = "/org/elasticsearch/analysis/common/test1.json"; return Settings.builder() - .loadFromStream(json, getClass().getResourceAsStream(json), false) - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .build(); + .loadFromStream(json, getClass().getResourceAsStream(json), false) + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) + .build(); } private Settings getYamlSettings() throws IOException { String yaml = "/org/elasticsearch/analysis/common/test1.yml"; return Settings.builder() - .loadFromStream(yaml, getClass().getResourceAsStream(yaml), false) - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .build(); + .loadFromStream(yaml, getClass().getResourceAsStream(yaml), false) + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) + .build(); } } diff --git a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/DisableGraphQueryTests.java b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/DisableGraphQueryTests.java index 776e53f4edee2..8da4729db1367 100644 --- a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/DisableGraphQueryTests.java +++ b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/DisableGraphQueryTests.java @@ -9,20 +9,20 @@ package org.elasticsearch.analysis.common; import org.apache.lucene.index.Term; +import org.apache.lucene.search.BooleanClause; +import org.apache.lucene.search.BooleanQuery; +import org.apache.lucene.search.MultiPhraseQuery; +import org.apache.lucene.search.PhraseQuery; import org.apache.lucene.search.Query; import org.apache.lucene.search.SynonymQuery; -import org.apache.lucene.search.BooleanQuery; -import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.TermQuery; -import org.apache.lucene.search.PhraseQuery; -import org.apache.lucene.search.MultiPhraseQuery; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.IndexService; import org.elasticsearch.index.query.MatchPhraseQueryBuilder; import org.elasticsearch.index.query.MatchQueryBuilder; import org.elasticsearch.index.query.MultiMatchQueryBuilder; -import org.elasticsearch.index.query.SearchExecutionContext; import org.elasticsearch.index.query.QueryStringQueryBuilder; +import org.elasticsearch.index.query.SearchExecutionContext; import org.elasticsearch.index.query.SimpleQueryStringBuilder; import org.elasticsearch.index.query.SimpleQueryStringFlag; import org.elasticsearch.index.search.MatchQueryParser; @@ -68,81 +68,56 @@ public void setup() { .put("index.analysis.analyzer.text_shingle.tokenizer", "whitespace") .put("index.analysis.analyzer.text_shingle.filter", "lowercase, shingle") .put("index.analysis.analyzer.text_shingle_unigram.tokenizer", "whitespace") - .put("index.analysis.analyzer.text_shingle_unigram.filter", - "lowercase, shingle_unigram") + .put("index.analysis.analyzer.text_shingle_unigram.filter", "lowercase, shingle_unigram") .build(); - indexService = createIndex("test", settings, "t", - "text_shingle", "type=text,analyzer=text_shingle", - "text_shingle_unigram", "type=text,analyzer=text_shingle_unigram"); + indexService = createIndex( + "test", + settings, + "t", + "text_shingle", + "type=text,analyzer=text_shingle", + "text_shingle_unigram", + "type=text,analyzer=text_shingle_unigram" + ); searchExecutionContext = indexService.newSearchExecutionContext(0, 0, null, () -> 0L, null, emptyMap()); // parsed queries for "text_shingle_unigram:(foo bar baz)" with query parsers // that ignores position length attribute - expectedQueryWithUnigram= new BooleanQuery.Builder() - .add( - new SynonymQuery.Builder("text_shingle_unigram") - .addTerm(new Term("text_shingle_unigram", "foo")) - .addTerm(new Term("text_shingle_unigram", "foo bar")) - .build(), - BooleanClause.Occur.SHOULD) + expectedQueryWithUnigram = new BooleanQuery.Builder().add( + new SynonymQuery.Builder("text_shingle_unigram").addTerm(new Term("text_shingle_unigram", "foo")) + .addTerm(new Term("text_shingle_unigram", "foo bar")) + .build(), + BooleanClause.Occur.SHOULD + ) .add( - new SynonymQuery.Builder("text_shingle_unigram") - .addTerm(new Term("text_shingle_unigram", "bar")) + new SynonymQuery.Builder("text_shingle_unigram").addTerm(new Term("text_shingle_unigram", "bar")) .addTerm(new Term("text_shingle_unigram", "bar baz")) .build(), - BooleanClause.Occur.SHOULD) - .add( - new TermQuery( - new Term("text_shingle_unigram", "baz") - ), BooleanClause.Occur.SHOULD) + BooleanClause.Occur.SHOULD + ) + .add(new TermQuery(new Term("text_shingle_unigram", "baz")), BooleanClause.Occur.SHOULD) .build(); // parsed query for "text_shingle_unigram:\"foo bar baz\" with query parsers // that ignores position length attribute - expectedPhraseQueryWithUnigram = new MultiPhraseQuery.Builder() - .add( - new Term[] { - new Term("text_shingle_unigram", "foo"), - new Term("text_shingle_unigram", "foo bar") - }, 0) - .add( - new Term[] { - new Term("text_shingle_unigram", "bar"), - new Term("text_shingle_unigram", "bar baz") - }, 1) - .add( - new Term[] { - new Term("text_shingle_unigram", "baz"), - }, 2) + expectedPhraseQueryWithUnigram = new MultiPhraseQuery.Builder().add( + new Term[] { new Term("text_shingle_unigram", "foo"), new Term("text_shingle_unigram", "foo bar") }, + 0 + ) + .add(new Term[] { new Term("text_shingle_unigram", "bar"), new Term("text_shingle_unigram", "bar baz") }, 1) + .add(new Term[] { new Term("text_shingle_unigram", "baz"), }, 2) .build(); // parsed query for "text_shingle:(foo bar baz) - expectedQuery = new BooleanQuery.Builder() - .add( - new TermQuery(new Term("text_shingle", "foo bar")), - BooleanClause.Occur.SHOULD - ) - .add( - new TermQuery(new Term("text_shingle","bar baz")), - BooleanClause.Occur.SHOULD - ) - .add( - new TermQuery(new Term("text_shingle","baz biz")), - BooleanClause.Occur.SHOULD - ) + expectedQuery = new BooleanQuery.Builder().add(new TermQuery(new Term("text_shingle", "foo bar")), BooleanClause.Occur.SHOULD) + .add(new TermQuery(new Term("text_shingle", "bar baz")), BooleanClause.Occur.SHOULD) + .add(new TermQuery(new Term("text_shingle", "baz biz")), BooleanClause.Occur.SHOULD) .build(); // parsed query for "text_shingle:"foo bar baz" - expectedPhraseQuery = new PhraseQuery.Builder() - .add( - new Term("text_shingle", "foo bar") - ) - .add( - new Term("text_shingle","bar baz") - ) - .add( - new Term("text_shingle","baz biz") - ) + expectedPhraseQuery = new PhraseQuery.Builder().add(new Term("text_shingle", "foo bar")) + .add(new Term("text_shingle", "bar baz")) + .add(new Term("text_shingle", "baz biz")) .build(); } @@ -155,20 +130,17 @@ public void cleanup() { } public void testMatchPhraseQuery() throws IOException { - MatchPhraseQueryBuilder builder = - new MatchPhraseQueryBuilder("text_shingle_unigram", "foo bar baz"); + MatchPhraseQueryBuilder builder = new MatchPhraseQueryBuilder("text_shingle_unigram", "foo bar baz"); Query query = builder.toQuery(searchExecutionContext); assertThat(expectedPhraseQueryWithUnigram, equalTo(query)); - builder = - new MatchPhraseQueryBuilder("text_shingle", "foo bar baz biz"); + builder = new MatchPhraseQueryBuilder("text_shingle", "foo bar baz biz"); query = builder.toQuery(searchExecutionContext); assertThat(expectedPhraseQuery, equalTo(query)); } public void testMatchQuery() throws IOException { - MatchQueryBuilder builder = - new MatchQueryBuilder("text_shingle_unigram", "foo bar baz"); + MatchQueryBuilder builder = new MatchQueryBuilder("text_shingle_unigram", "foo bar baz"); Query query = builder.toQuery(searchExecutionContext); assertThat(expectedQueryWithUnigram, equalTo(query)); @@ -178,8 +150,7 @@ public void testMatchQuery() throws IOException { } public void testMultiMatchQuery() throws IOException { - MultiMatchQueryBuilder builder = new MultiMatchQueryBuilder("foo bar baz", - "text_shingle_unigram"); + MultiMatchQueryBuilder builder = new MultiMatchQueryBuilder("foo bar baz", "text_shingle_unigram"); Query query = builder.toQuery(searchExecutionContext); assertThat(expectedQueryWithUnigram, equalTo(query)); diff --git a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/ESSolrSynonymParserTests.java b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/ESSolrSynonymParserTests.java index 6e89fa9211a92..99712519da3ab 100644 --- a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/ESSolrSynonymParserTests.java +++ b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/ESSolrSynonymParserTests.java @@ -28,23 +28,20 @@ public class ESSolrSynonymParserTests extends ESTokenStreamTestCase { public void testLenientParser() throws IOException, ParseException { ESSolrSynonymParser parser = new ESSolrSynonymParser(true, false, true, new StandardAnalyzer()); - String rules = - "&,and\n" + - "come,advance,approach\n"; + String rules = "&,and\n" + "come,advance,approach\n"; StringReader rulesReader = new StringReader(rules); parser.parse(rulesReader); SynonymMap synonymMap = parser.build(); Tokenizer tokenizer = new StandardTokenizer(); tokenizer.setReader(new StringReader("approach quietly then advance & destroy")); TokenStream ts = new SynonymFilter(tokenizer, synonymMap, false); - assertTokenStreamContents(ts, new String[]{"come", "quietly", "then", "come", "destroy"}); + assertTokenStreamContents(ts, new String[] { "come", "quietly", "then", "come", "destroy" }); } public void testLenientParserWithSomeIncorrectLines() throws IOException, ParseException { CharArraySet stopSet = new CharArraySet(1, true); stopSet.add("bar"); - ESSolrSynonymParser parser = - new ESSolrSynonymParser(true, false, true, new StandardAnalyzer(stopSet)); + ESSolrSynonymParser parser = new ESSolrSynonymParser(true, false, true, new StandardAnalyzer(stopSet)); String rules = "foo,bar,baz"; StringReader rulesReader = new StringReader(rules); parser.parse(rulesReader); @@ -52,14 +49,12 @@ public void testLenientParserWithSomeIncorrectLines() throws IOException, ParseE Tokenizer tokenizer = new StandardTokenizer(); tokenizer.setReader(new StringReader("first word is foo, then bar and lastly baz")); TokenStream ts = new SynonymFilter(new StopFilter(tokenizer, stopSet), synonymMap, false); - assertTokenStreamContents(ts, new String[]{"first", "word", "is", "foo", "then", "and", "lastly", "foo"}); + assertTokenStreamContents(ts, new String[] { "first", "word", "is", "foo", "then", "and", "lastly", "foo" }); } public void testNonLenientParser() { ESSolrSynonymParser parser = new ESSolrSynonymParser(true, false, false, new StandardAnalyzer()); - String rules = - "&,and=>and\n" + - "come,advance,approach\n"; + String rules = "&,and=>and\n" + "come,advance,approach\n"; StringReader rulesReader = new StringReader(rules); ParseException ex = expectThrows(ParseException.class, () -> parser.parse(rulesReader)); assertThat(ex.getMessage(), containsString("Invalid synonym rule at line 1")); diff --git a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/ESWordnetSynonymParserTests.java b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/ESWordnetSynonymParserTests.java index 02e365b2ee93b..2bf4c6c830e37 100644 --- a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/ESWordnetSynonymParserTests.java +++ b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/ESWordnetSynonymParserTests.java @@ -28,47 +28,41 @@ public class ESWordnetSynonymParserTests extends ESTokenStreamTestCase { public void testLenientParser() throws IOException, ParseException { ESWordnetSynonymParser parser = new ESWordnetSynonymParser(true, false, true, new StandardAnalyzer()); - String rules = - "s(100000001,1,'&',a,1,0).\n" + - "s(100000001,2,'and',a,1,0).\n" + - "s(100000002,1,'come',v,1,0).\n" + - "s(100000002,2,'advance',v,1,0).\n" + - "s(100000002,3,'approach',v,1,0)."; + String rules = "s(100000001,1,'&',a,1,0).\n" + + "s(100000001,2,'and',a,1,0).\n" + + "s(100000002,1,'come',v,1,0).\n" + + "s(100000002,2,'advance',v,1,0).\n" + + "s(100000002,3,'approach',v,1,0)."; StringReader rulesReader = new StringReader(rules); parser.parse(rulesReader); SynonymMap synonymMap = parser.build(); Tokenizer tokenizer = new StandardTokenizer(); tokenizer.setReader(new StringReader("approach quietly then advance & destroy")); TokenStream ts = new SynonymFilter(tokenizer, synonymMap, false); - assertTokenStreamContents(ts, new String[]{"come", "quietly", "then", "come", "destroy"}); + assertTokenStreamContents(ts, new String[] { "come", "quietly", "then", "come", "destroy" }); } public void testLenientParserWithSomeIncorrectLines() throws IOException, ParseException { CharArraySet stopSet = new CharArraySet(1, true); stopSet.add("bar"); - ESWordnetSynonymParser parser = - new ESWordnetSynonymParser(true, false, true, new StandardAnalyzer(stopSet)); - String rules = - "s(100000001,1,'foo',v,1,0).\n" + - "s(100000001,2,'bar',v,1,0).\n" + - "s(100000001,3,'baz',v,1,0)."; + ESWordnetSynonymParser parser = new ESWordnetSynonymParser(true, false, true, new StandardAnalyzer(stopSet)); + String rules = "s(100000001,1,'foo',v,1,0).\n" + "s(100000001,2,'bar',v,1,0).\n" + "s(100000001,3,'baz',v,1,0)."; StringReader rulesReader = new StringReader(rules); parser.parse(rulesReader); SynonymMap synonymMap = parser.build(); Tokenizer tokenizer = new StandardTokenizer(); tokenizer.setReader(new StringReader("first word is foo, then bar and lastly baz")); TokenStream ts = new SynonymFilter(new StopFilter(tokenizer, stopSet), synonymMap, false); - assertTokenStreamContents(ts, new String[]{"first", "word", "is", "foo", "then", "and", "lastly", "foo"}); + assertTokenStreamContents(ts, new String[] { "first", "word", "is", "foo", "then", "and", "lastly", "foo" }); } public void testNonLenientParser() { ESWordnetSynonymParser parser = new ESWordnetSynonymParser(true, false, false, new StandardAnalyzer()); - String rules = - "s(100000001,1,'&',a,1,0).\n" + - "s(100000001,2,'and',a,1,0).\n" + - "s(100000002,1,'come',v,1,0).\n" + - "s(100000002,2,'advance',v,1,0).\n" + - "s(100000002,3,'approach',v,1,0)."; + String rules = "s(100000001,1,'&',a,1,0).\n" + + "s(100000001,2,'and',a,1,0).\n" + + "s(100000002,1,'come',v,1,0).\n" + + "s(100000002,2,'advance',v,1,0).\n" + + "s(100000002,3,'approach',v,1,0)."; StringReader rulesReader = new StringReader(rules); ParseException ex = expectThrows(ParseException.class, () -> parser.parse(rulesReader)); assertThat(ex.getMessage(), containsString("Invalid synonym rule at line 1")); diff --git a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/EdgeNGramTokenFilterFactoryTests.java b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/EdgeNGramTokenFilterFactoryTests.java index d77e840b6c6b7..5bb7b4d38c1ff 100644 --- a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/EdgeNGramTokenFilterFactoryTests.java +++ b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/EdgeNGramTokenFilterFactoryTests.java @@ -28,10 +28,11 @@ public void testDefault() throws IOException { .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) .put("index.analysis.filter.my_edge_ngram.type", "edge_ngram") .build(), - new CommonAnalysisPlugin()); + new CommonAnalysisPlugin() + ); TokenFilterFactory tokenFilter = analysis.tokenFilter.get("my_edge_ngram"); String source = "foo"; - String[] expected = new String[]{"f", "fo"}; + String[] expected = new String[] { "f", "fo" }; Tokenizer tokenizer = new StandardTokenizer(); tokenizer.setReader(new StringReader(source)); assertTokenStreamContents(tokenFilter.create(tokenizer), expected); @@ -44,10 +45,11 @@ public void testPreserveOriginal() throws IOException { .put("index.analysis.filter.my_edge_ngram.type", "edge_ngram") .put("index.analysis.filter.my_edge_ngram.preserve_original", true) .build(), - new CommonAnalysisPlugin()); + new CommonAnalysisPlugin() + ); TokenFilterFactory tokenFilter = analysis.tokenFilter.get("my_edge_ngram"); String source = "foo"; - String[] expected = new String[]{"f", "fo", "foo"}; + String[] expected = new String[] { "f", "fo", "foo" }; Tokenizer tokenizer = new StandardTokenizer(); tokenizer.setReader(new StringReader(source)); assertTokenStreamContents(tokenFilter.create(tokenizer), expected); diff --git a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/EdgeNGramTokenizerTests.java b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/EdgeNGramTokenizerTests.java index 2b21195e3f8c3..40367401e2784 100644 --- a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/EdgeNGramTokenizerTests.java +++ b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/EdgeNGramTokenizerTests.java @@ -30,39 +30,44 @@ public class EdgeNGramTokenizerTests extends ESTokenStreamTestCase { private IndexAnalyzers buildAnalyzers(Version version, String tokenizer) throws IOException { - Settings settings = Settings.builder() - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .build(); + Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build(); Settings indexSettings = Settings.builder() .put(IndexMetadata.SETTING_VERSION_CREATED, version) .put("index.analysis.analyzer.my_analyzer.tokenizer", tokenizer) .build(); IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("index", indexSettings); - return new AnalysisModule(TestEnvironment.newEnvironment(settings), - Collections.singletonList(new CommonAnalysisPlugin())).getAnalysisRegistry().build(idxSettings); + return new AnalysisModule(TestEnvironment.newEnvironment(settings), Collections.singletonList(new CommonAnalysisPlugin())) + .getAnalysisRegistry() + .build(idxSettings); } public void testPreConfiguredTokenizer() throws IOException { // Before 7.3 we return ngrams of length 1 only { - Version version = VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, - VersionUtils.getPreviousVersion(Version.V_7_3_0)); + Version version = VersionUtils.randomVersionBetween( + random(), + Version.V_7_0_0, + VersionUtils.getPreviousVersion(Version.V_7_3_0) + ); try (IndexAnalyzers indexAnalyzers = buildAnalyzers(version, "edge_ngram")) { NamedAnalyzer analyzer = indexAnalyzers.get("my_analyzer"); assertNotNull(analyzer); - assertAnalyzesTo(analyzer, "test", new String[]{"t"}); + assertAnalyzesTo(analyzer, "test", new String[] { "t" }); } } // Check deprecated name as well { - Version version = VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, - VersionUtils.getPreviousVersion(Version.V_7_3_0)); + Version version = VersionUtils.randomVersionBetween( + random(), + Version.V_7_0_0, + VersionUtils.getPreviousVersion(Version.V_7_3_0) + ); try (IndexAnalyzers indexAnalyzers = buildAnalyzers(version, "edgeNGram")) { NamedAnalyzer analyzer = indexAnalyzers.get("my_analyzer"); assertNotNull(analyzer); - assertAnalyzesTo(analyzer, "test", new String[]{"t"}); + assertAnalyzesTo(analyzer, "test", new String[] { "t" }); } } @@ -71,18 +76,21 @@ public void testPreConfiguredTokenizer() throws IOException { try (IndexAnalyzers indexAnalyzers = buildAnalyzers(Version.CURRENT, "edge_ngram")) { NamedAnalyzer analyzer = indexAnalyzers.get("my_analyzer"); assertNotNull(analyzer); - assertAnalyzesTo(analyzer, "test", new String[]{"t", "te"}); + assertAnalyzesTo(analyzer, "test", new String[] { "t", "te" }); } } // Check deprecated name as well, needs version before 8.0 because throws IAE after that { - try (IndexAnalyzers indexAnalyzers = buildAnalyzers( + try ( + IndexAnalyzers indexAnalyzers = buildAnalyzers( VersionUtils.randomVersionBetween(random(), Version.V_7_3_0, VersionUtils.getPreviousVersion(Version.V_8_0_0)), - "edgeNGram")) { + "edgeNGram" + ) + ) { NamedAnalyzer analyzer = indexAnalyzers.get("my_analyzer"); assertNotNull(analyzer); - assertAnalyzesTo(analyzer, "test", new String[]{"t", "te"}); + assertAnalyzesTo(analyzer, "test", new String[] { "t", "te" }); } } @@ -94,12 +102,19 @@ public void testCustomTokenChars() throws IOException { final String name = "engr"; final Settings indexSettings = newAnalysisSettingsBuilder().put(IndexSettings.MAX_NGRAM_DIFF_SETTING.getKey(), 2).build(); - final Settings settings = newAnalysisSettingsBuilder().put("min_gram", 2).put("max_gram", 3) - .putList("token_chars", "letter", "custom").put("custom_token_chars","_-").build(); - Tokenizer tokenizer = new EdgeNGramTokenizerFactory(IndexSettingsModule.newIndexSettings(index, indexSettings), null, name, - settings).create(); + final Settings settings = newAnalysisSettingsBuilder().put("min_gram", 2) + .put("max_gram", 3) + .putList("token_chars", "letter", "custom") + .put("custom_token_chars", "_-") + .build(); + Tokenizer tokenizer = new EdgeNGramTokenizerFactory( + IndexSettingsModule.newIndexSettings(index, indexSettings), + null, + name, + settings + ).create(); tokenizer.setReader(new StringReader("Abc -gh _jk =lm")); - assertTokenStreamContents(tokenizer, new String[] {"Ab", "Abc", "-g", "-gh", "_j", "_jk", "lm"}); + assertTokenStreamContents(tokenizer, new String[] { "Ab", "Abc", "-g", "-gh", "_j", "_jk", "lm" }); } } diff --git a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/ElisionFilterFactoryTests.java b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/ElisionFilterFactoryTests.java index a3bc78588b610..de2d0b550f500 100644 --- a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/ElisionFilterFactoryTests.java +++ b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/ElisionFilterFactoryTests.java @@ -23,8 +23,10 @@ public void testElisionFilterWithNoArticles() throws IOException { .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) .build(); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> AnalysisTestsHelper.createTestAnalysisFromSettings(settings, new CommonAnalysisPlugin())); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> AnalysisTestsHelper.createTestAnalysisFromSettings(settings, new CommonAnalysisPlugin()) + ); assertEquals("elision filter requires [articles] or [articles_path] setting", e.getMessage()); } diff --git a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/FingerprintAnalyzerTests.java b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/FingerprintAnalyzerTests.java index b5ce9aceca390..99e00833eb071 100644 --- a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/FingerprintAnalyzerTests.java +++ b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/FingerprintAnalyzerTests.java @@ -16,34 +16,27 @@ public class FingerprintAnalyzerTests extends ESTokenStreamTestCase { public void testFingerprint() throws Exception { Analyzer a = new FingerprintAnalyzer(CharArraySet.EMPTY_SET, ' ', 255); - assertAnalyzesTo(a, "foo bar@baz Baz $ foo foo FOO. FoO", - new String[]{"bar baz foo"}); + assertAnalyzesTo(a, "foo bar@baz Baz $ foo foo FOO. FoO", new String[] { "bar baz foo" }); } public void testReusableTokenStream() throws Exception { Analyzer a = new FingerprintAnalyzer(CharArraySet.EMPTY_SET, ' ', 255); - assertAnalyzesTo(a, "foo bar baz Baz foo foo FOO. FoO", - new String[]{"bar baz foo"}); - assertAnalyzesTo(a, "xyz XYZ abc 123.2 abc", - new String[]{"123.2 abc xyz"}); + assertAnalyzesTo(a, "foo bar baz Baz foo foo FOO. FoO", new String[] { "bar baz foo" }); + assertAnalyzesTo(a, "xyz XYZ abc 123.2 abc", new String[] { "123.2 abc xyz" }); } public void testAsciifolding() throws Exception { Analyzer a = new FingerprintAnalyzer(CharArraySet.EMPTY_SET, ' ', 255); - assertAnalyzesTo(a, "gödel escher bach", - new String[]{"bach escher godel"}); + assertAnalyzesTo(a, "gödel escher bach", new String[] { "bach escher godel" }); - assertAnalyzesTo(a, "gödel godel escher bach", - new String[]{"bach escher godel"}); + assertAnalyzesTo(a, "gödel godel escher bach", new String[] { "bach escher godel" }); } public void testLimit() throws Exception { Analyzer a = new FingerprintAnalyzer(CharArraySet.EMPTY_SET, ' ', 3); - assertAnalyzesTo(a, "e d c b a", - new String[]{}); + assertAnalyzesTo(a, "e d c b a", new String[] {}); - assertAnalyzesTo(a, "b a", - new String[]{"a b"}); + assertAnalyzesTo(a, "b a", new String[] { "a b" }); } } diff --git a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/FlattenGraphTokenFilterFactoryTests.java b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/FlattenGraphTokenFilterFactoryTests.java index d64106e222b1f..ec5dc0e25dea0 100644 --- a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/FlattenGraphTokenFilterFactoryTests.java +++ b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/FlattenGraphTokenFilterFactoryTests.java @@ -30,27 +30,32 @@ public void testBasic() throws IOException { Settings settings = newAnalysisSettingsBuilder().build(); // "wow that's funny" and "what the fudge" are separate side paths, in parallel with "wtf", on input: - TokenStream in = new CannedTokenStream(0, 12, new Token[] { - token("wtf", 1, 5, 0, 3), - token("what", 0, 1, 0, 3), - token("wow", 0, 3, 0, 3), - token("the", 1, 1, 0, 3), - token("fudge", 1, 3, 0, 3), - token("that's", 1, 1, 0, 3), - token("funny", 1, 1, 0, 3), - token("happened", 1, 1, 4, 12) - }); + TokenStream in = new CannedTokenStream( + 0, + 12, + new Token[] { + token("wtf", 1, 5, 0, 3), + token("what", 0, 1, 0, 3), + token("wow", 0, 3, 0, 3), + token("the", 1, 1, 0, 3), + token("fudge", 1, 3, 0, 3), + token("that's", 1, 1, 0, 3), + token("funny", 1, 1, 0, 3), + token("happened", 1, 1, 4, 12) } + ); TokenStream tokens = new FlattenGraphTokenFilterFactory(indexProperties, null, name, settings).create(in); // ... but on output, it's flattened to wtf/what/wow that's/the fudge/funny happened: - assertTokenStreamContents(tokens, - new String[] {"wtf", "what", "wow", "the", "that's", "fudge", "funny", "happened"}, - new int[] {0, 0, 0, 0, 0, 0, 0, 4}, - new int[] {3, 3, 3, 3, 3, 3, 3, 12}, - new int[] {1, 0, 0, 1, 0, 1, 0, 1}, - new int[] {3, 1, 1, 1, 1, 1, 1, 1}, - 12); + assertTokenStreamContents( + tokens, + new String[] { "wtf", "what", "wow", "the", "that's", "fudge", "funny", "happened" }, + new int[] { 0, 0, 0, 0, 0, 0, 0, 4 }, + new int[] { 3, 3, 3, 3, 3, 3, 3, 12 }, + new int[] { 1, 0, 0, 1, 0, 1, 0, 1 }, + new int[] { 3, 1, 1, 1, 1, 1, 1, 1 }, + 12 + ); } private static Token token(String term, int posInc, int posLength, int startOffset, int endOffset) { diff --git a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/HighlighterWithAnalyzersTests.java b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/HighlighterWithAnalyzersTests.java index ec46ebac072c5..d321ef06ee9f8 100644 --- a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/HighlighterWithAnalyzersTests.java +++ b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/HighlighterWithAnalyzersTests.java @@ -10,21 +10,20 @@ import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.query.Operator; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; import org.elasticsearch.test.ESIntegTestCase; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; import java.io.IOException; import java.util.Arrays; import java.util.Collection; import static org.elasticsearch.client.Requests.searchRequest; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.index.query.QueryBuilders.boolQuery; import static org.elasticsearch.index.query.QueryBuilders.matchPhrasePrefixQuery; import static org.elasticsearch.index.query.QueryBuilders.matchPhraseQuery; @@ -34,6 +33,7 @@ import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHighlight; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.Matchers.anyOf; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.startsWith; @@ -45,26 +45,28 @@ protected Collection> nodePlugins() { } public void testNgramHighlightingWithBrokenPositions() throws IOException { - assertAcked(prepareCreate("test") - .setMapping(jsonBuilder() - .startObject() - .startObject("_doc") - .startObject("properties") - .startObject("name") - .field("type", "text") - .startObject("fields") - .startObject("autocomplete") - .field("type", "text") - .field("analyzer", "autocomplete") - .field("search_analyzer", "search_autocomplete") - .field("term_vector", "with_positions_offsets") - .endObject() - .endObject() - .endObject() - .endObject() - .endObject() - .endObject()) - .setSettings(Settings.builder() + assertAcked( + prepareCreate("test").setMapping( + jsonBuilder().startObject() + .startObject("_doc") + .startObject("properties") + .startObject("name") + .field("type", "text") + .startObject("fields") + .startObject("autocomplete") + .field("type", "text") + .field("analyzer", "autocomplete") + .field("search_analyzer", "search_autocomplete") + .field("term_vector", "with_positions_offsets") + .endObject() + .endObject() + .endObject() + .endObject() + .endObject() + .endObject() + ) + .setSettings( + Settings.builder() .put(indexSettings()) .put(IndexSettings.MAX_NGRAM_DIFF_SETTING.getKey(), 19) .put("analysis.tokenizer.autocomplete.max_gram", 20) @@ -72,15 +74,31 @@ public void testNgramHighlightingWithBrokenPositions() throws IOException { .put("analysis.tokenizer.autocomplete.token_chars", "letter,digit") .put("analysis.tokenizer.autocomplete.type", "ngram") .put("analysis.filter.wordDelimiter.type", "word_delimiter") - .putList("analysis.filter.wordDelimiter.type_table", - "& => ALPHANUM", "| => ALPHANUM", "! => ALPHANUM", - "? => ALPHANUM", ". => ALPHANUM", "- => ALPHANUM", - "# => ALPHANUM", "% => ALPHANUM", "+ => ALPHANUM", - ", => ALPHANUM", "~ => ALPHANUM", ": => ALPHANUM", - "/ => ALPHANUM", "^ => ALPHANUM", "$ => ALPHANUM", - "@ => ALPHANUM", ") => ALPHANUM", "( => ALPHANUM", - "] => ALPHANUM", "[ => ALPHANUM", "} => ALPHANUM", - "{ => ALPHANUM") + .putList( + "analysis.filter.wordDelimiter.type_table", + "& => ALPHANUM", + "| => ALPHANUM", + "! => ALPHANUM", + "? => ALPHANUM", + ". => ALPHANUM", + "- => ALPHANUM", + "# => ALPHANUM", + "% => ALPHANUM", + "+ => ALPHANUM", + ", => ALPHANUM", + "~ => ALPHANUM", + ": => ALPHANUM", + "/ => ALPHANUM", + "^ => ALPHANUM", + "$ => ALPHANUM", + "@ => ALPHANUM", + ") => ALPHANUM", + "( => ALPHANUM", + "] => ALPHANUM", + "[ => ALPHANUM", + "} => ALPHANUM", + "{ => ALPHANUM" + ) .put("analysis.filter.wordDelimiter.type.split_on_numerics", false) .put("analysis.filter.wordDelimiter.generate_word_parts", true) .put("analysis.filter.wordDelimiter.generate_number_parts", false) @@ -89,19 +107,18 @@ public void testNgramHighlightingWithBrokenPositions() throws IOException { .put("analysis.filter.wordDelimiter.catenate_all", false) .put("analysis.analyzer.autocomplete.tokenizer", "autocomplete") - .putList("analysis.analyzer.autocomplete.filter", - "lowercase", "wordDelimiter") + .putList("analysis.analyzer.autocomplete.filter", "lowercase", "wordDelimiter") .put("analysis.analyzer.search_autocomplete.tokenizer", "whitespace") - .putList("analysis.analyzer.search_autocomplete.filter", - "lowercase", "wordDelimiter"))); - client().prepareIndex("test").setId("1") - .setSource("name", "ARCOTEL Hotels Deutschland").get(); + .putList("analysis.analyzer.search_autocomplete.filter", "lowercase", "wordDelimiter") + ) + ); + client().prepareIndex("test").setId("1").setSource("name", "ARCOTEL Hotels Deutschland").get(); refresh(); SearchResponse search = client().prepareSearch("test") - .setQuery(matchQuery("name.autocomplete", "deut tel").operator(Operator.OR)) - .highlighter(new HighlightBuilder().field("name.autocomplete")).get(); - assertHighlight(search, 0, "name.autocomplete", 0, - equalTo("ARCOTEL Hotels Deutschland")); + .setQuery(matchQuery("name.autocomplete", "deut tel").operator(Operator.OR)) + .highlighter(new HighlightBuilder().field("name.autocomplete")) + .get(); + assertHighlight(search, 0, "name.autocomplete", 0, equalTo("ARCOTEL Hotels Deutschland")); } public void testMultiPhraseCutoff() throws IOException { @@ -109,48 +126,70 @@ public void testMultiPhraseCutoff() throws IOException { * MultiPhraseQuery can literally kill an entire node if there are too many terms in the * query. We cut off and extract terms if there are more than 16 terms in the query */ - assertAcked(prepareCreate("test") - .setMapping("body", "type=text,analyzer=custom_analyzer," - + "search_analyzer=custom_analyzer,term_vector=with_positions_offsets") + assertAcked( + prepareCreate("test").setMapping( + "body", + "type=text,analyzer=custom_analyzer," + "search_analyzer=custom_analyzer,term_vector=with_positions_offsets" + ) .setSettings( - Settings.builder().put(indexSettings()) - .put("analysis.filter.wordDelimiter.type", "word_delimiter") - .put("analysis.filter.wordDelimiter.type.split_on_numerics", false) - .put("analysis.filter.wordDelimiter.generate_word_parts", true) - .put("analysis.filter.wordDelimiter.generate_number_parts", true) - .put("analysis.filter.wordDelimiter.catenate_words", true) - .put("analysis.filter.wordDelimiter.catenate_numbers", true) - .put("analysis.filter.wordDelimiter.catenate_all", false) - .put("analysis.analyzer.custom_analyzer.tokenizer", "whitespace") - .putList("analysis.analyzer.custom_analyzer.filter", - "lowercase", "wordDelimiter")) + Settings.builder() + .put(indexSettings()) + .put("analysis.filter.wordDelimiter.type", "word_delimiter") + .put("analysis.filter.wordDelimiter.type.split_on_numerics", false) + .put("analysis.filter.wordDelimiter.generate_word_parts", true) + .put("analysis.filter.wordDelimiter.generate_number_parts", true) + .put("analysis.filter.wordDelimiter.catenate_words", true) + .put("analysis.filter.wordDelimiter.catenate_numbers", true) + .put("analysis.filter.wordDelimiter.catenate_all", false) + .put("analysis.analyzer.custom_analyzer.tokenizer", "whitespace") + .putList("analysis.analyzer.custom_analyzer.filter", "lowercase", "wordDelimiter") + ) ); ensureGreen(); - client().prepareIndex("test").setId("1") - .setSource("body", "Test: http://www.facebook.com http://elasticsearch.org " + client().prepareIndex("test") + .setId("1") + .setSource( + "body", + "Test: http://www.facebook.com http://elasticsearch.org " + "http://xing.com http://cnn.com http://quora.com http://twitter.com this is " + "a test for highlighting feature Test: http://www.facebook.com " + "http://elasticsearch.org http://xing.com http://cnn.com http://quora.com " - + "http://twitter.com this is a test for highlighting feature") + + "http://twitter.com this is a test for highlighting feature" + ) .get(); refresh(); SearchResponse search = client().prepareSearch() - .setQuery(matchPhraseQuery("body", "Test: http://www.facebook.com ")) - .highlighter(new HighlightBuilder().field("body").highlighterType("fvh")).get(); + .setQuery(matchPhraseQuery("body", "Test: http://www.facebook.com ")) + .highlighter(new HighlightBuilder().field("body").highlighterType("fvh")) + .get(); assertHighlight(search, 0, "body", 0, startsWith("Test: http://www.facebook.com")); - search = client() - .prepareSearch() - .setQuery(matchPhraseQuery("body", "Test: http://www.facebook.com " + search = client().prepareSearch() + .setQuery( + matchPhraseQuery( + "body", + "Test: http://www.facebook.com " + "http://elasticsearch.org http://xing.com http://cnn.com " + "http://quora.com http://twitter.com this is a test for highlighting " + "feature Test: http://www.facebook.com http://elasticsearch.org " + "http://xing.com http://cnn.com http://quora.com http://twitter.com this " - + "is a test for highlighting feature")) - .highlighter(new HighlightBuilder().field("body").highlighterType("fvh")).execute().actionGet(); - assertHighlight(search, 0, "body", 0, equalTo("Test: " - + "http://www.facebook.com http://elasticsearch.org " - + "http://xing.com http://cnn.com http://quora.com")); + + "is a test for highlighting feature" + ) + ) + .highlighter(new HighlightBuilder().field("body").highlighterType("fvh")) + .execute() + .actionGet(); + assertHighlight( + search, + 0, + "body", + 0, + equalTo( + "Test: " + + "http://www.facebook.com http://elasticsearch.org " + + "http://xing.com http://cnn.com http://quora.com" + ) + ); } public void testSynonyms() throws IOException { @@ -161,36 +200,28 @@ public void testSynonyms() throws IOException { .put("index.analysis.filter.synonym.type", "synonym") .putList("index.analysis.filter.synonym.synonyms", "fast,quick"); - assertAcked(prepareCreate("test").setSettings(builder.build()) - .setMapping("field1", - "type=text,term_vector=with_positions_offsets,search_analyzer=synonym," + - "analyzer=standard,index_options=offsets")); + assertAcked( + prepareCreate("test").setSettings(builder.build()) + .setMapping( + "field1", + "type=text,term_vector=with_positions_offsets,search_analyzer=synonym," + "analyzer=standard,index_options=offsets" + ) + ); ensureGreen(); - client().prepareIndex("test").setId("0").setSource( - "field1", "The quick brown fox jumps over the lazy dog").get(); + client().prepareIndex("test").setId("0").setSource("field1", "The quick brown fox jumps over the lazy dog").get(); refresh(); - for (String highlighterType : new String[] {"plain", "fvh", "unified"}) { + for (String highlighterType : new String[] { "plain", "fvh", "unified" }) { logger.info("--> highlighting (type=" + highlighterType + ") and searching on field1"); - SearchSourceBuilder source = searchSource() - .query(matchQuery("field1", "quick brown fox").operator(Operator.AND)) - .highlighter( - highlight() - .field("field1") - .order("score") - .preTags("") - .postTags("") - .highlighterType(highlighterType)); + SearchSourceBuilder source = searchSource().query(matchQuery("field1", "quick brown fox").operator(Operator.AND)) + .highlighter(highlight().field("field1").order("score").preTags("").postTags("").highlighterType(highlighterType)); SearchResponse searchResponse = client().search(searchRequest("test").source(source)).actionGet(); - assertHighlight(searchResponse, 0, "field1", 0, 1, - equalTo("The quick brown fox jumps over the lazy dog")); + assertHighlight(searchResponse, 0, "field1", 0, 1, equalTo("The quick brown fox jumps over the lazy dog")); - source = searchSource() - .query(matchQuery("field1", "fast brown fox").operator(Operator.AND)) + source = searchSource().query(matchQuery("field1", "fast brown fox").operator(Operator.AND)) .highlighter(highlight().field("field1").order("score").preTags("").postTags("")); searchResponse = client().search(searchRequest("test").source(source)).actionGet(); - assertHighlight(searchResponse, 0, "field1", 0, 1, - equalTo("The quick brown fox jumps over the lazy dog")); + assertHighlight(searchResponse, 0, "field1", 0, 1, equalTo("The quick brown fox jumps over the lazy dog")); } } @@ -206,93 +237,142 @@ public void testPhrasePrefix() throws IOException { ensureGreen(); - client().prepareIndex("first_test_index").setId("0").setSource( - "field0", "The quick brown fox jumps over the lazy dog", - "field1", "The quick brown fox jumps over the lazy dog").get(); - client().prepareIndex("first_test_index").setId("1").setSource("field1", - "The quick browse button is a fancy thing, right bro?").get(); + client().prepareIndex("first_test_index") + .setId("0") + .setSource("field0", "The quick brown fox jumps over the lazy dog", "field1", "The quick brown fox jumps over the lazy dog") + .get(); + client().prepareIndex("first_test_index") + .setId("1") + .setSource("field1", "The quick browse button is a fancy thing, right bro?") + .get(); refresh(); logger.info("--> highlighting and searching on field0"); - SearchSourceBuilder source = searchSource() - .query(matchPhrasePrefixQuery("field0", "bro")) + SearchSourceBuilder source = searchSource().query(matchPhrasePrefixQuery("field0", "bro")) .highlighter(highlight().field("field0").order("score").preTags("").postTags("")); SearchResponse searchResponse = client().search(searchRequest("first_test_index").source(source)).actionGet(); assertHighlight(searchResponse, 0, "field0", 0, 1, equalTo("The quick brown fox jumps over the lazy dog")); - source = searchSource() - .query(matchPhrasePrefixQuery("field0", "quick bro")) + source = searchSource().query(matchPhrasePrefixQuery("field0", "quick bro")) .highlighter(highlight().field("field0").order("score").preTags("").postTags("")); searchResponse = client().search(searchRequest("first_test_index").source(source)).actionGet(); - assertHighlight(searchResponse, 0, "field0", 0, 1, - equalTo("The quick brown fox jumps over the lazy dog")); + assertHighlight(searchResponse, 0, "field0", 0, 1, equalTo("The quick brown fox jumps over the lazy dog")); logger.info("--> highlighting and searching on field1"); - source = searchSource() - .query(boolQuery() - .should(matchPhrasePrefixQuery("field1", "test")) - .should(matchPhrasePrefixQuery("field1", "bro")) - ) - .highlighter(highlight().field("field1").order("score").preTags("").postTags("")); + source = searchSource().query( + boolQuery().should(matchPhrasePrefixQuery("field1", "test")).should(matchPhrasePrefixQuery("field1", "bro")) + ).highlighter(highlight().field("field1").order("score").preTags("").postTags("")); searchResponse = client().search(searchRequest("first_test_index").source(source)).actionGet(); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(2L)); for (int i = 0; i < 2; i++) { - assertHighlight(searchResponse, i, "field1", 0, 1, anyOf( - equalTo("The quick browse button is a fancy thing, right bro?"), - equalTo("The quick brown fox jumps over the lazy dog"))); + assertHighlight( + searchResponse, + i, + "field1", + 0, + 1, + anyOf( + equalTo("The quick browse button is a fancy thing, right bro?"), + equalTo("The quick brown fox jumps over the lazy dog") + ) + ); } - source = searchSource() - .query(matchPhrasePrefixQuery("field1", "quick bro")) + source = searchSource().query(matchPhrasePrefixQuery("field1", "quick bro")) .highlighter(highlight().field("field1").order("score").preTags("").postTags("")); searchResponse = client().search(searchRequest("first_test_index").source(source)).actionGet(); - assertHighlight(searchResponse, 0, "field1", 0, 1, anyOf( - equalTo("The quick browse button is a fancy thing, right bro?"), - equalTo("The quick brown fox jumps over the lazy dog"))); - assertHighlight(searchResponse, 1, "field1", 0, 1, anyOf( - equalTo("The quick browse button is a fancy thing, right bro?"), - equalTo("The quick brown fox jumps over the lazy dog"))); + assertHighlight( + searchResponse, + 0, + "field1", + 0, + 1, + anyOf( + equalTo("The quick browse button is a fancy thing, right bro?"), + equalTo("The quick brown fox jumps over the lazy dog") + ) + ); + assertHighlight( + searchResponse, + 1, + "field1", + 0, + 1, + anyOf( + equalTo("The quick browse button is a fancy thing, right bro?"), + equalTo("The quick brown fox jumps over the lazy dog") + ) + ); - assertAcked(prepareCreate("second_test_index").setSettings(builder.build()).setMapping( - "field4", "type=text,term_vector=with_positions_offsets,analyzer=synonym", - "field3", "type=text,analyzer=synonym")); + assertAcked( + prepareCreate("second_test_index").setSettings(builder.build()) + .setMapping( + "field4", + "type=text,term_vector=with_positions_offsets,analyzer=synonym", + "field3", + "type=text,analyzer=synonym" + ) + ); // with synonyms - client().prepareIndex("second_test_index").setId("0").setSource( - "type", "type2", - "field4", "The quick brown fox jumps over the lazy dog", - "field3", "The quick brown fox jumps over the lazy dog").get(); - client().prepareIndex("second_test_index").setId("1").setSource( - "type", "type2", - "field4", "The quick browse button is a fancy thing, right bro?").get(); - client().prepareIndex("second_test_index").setId("2").setSource( - "type", "type2", - "field4", "a quick fast blue car").get(); + client().prepareIndex("second_test_index") + .setId("0") + .setSource( + "type", + "type2", + "field4", + "The quick brown fox jumps over the lazy dog", + "field3", + "The quick brown fox jumps over the lazy dog" + ) + .get(); + client().prepareIndex("second_test_index") + .setId("1") + .setSource("type", "type2", "field4", "The quick browse button is a fancy thing, right bro?") + .get(); + client().prepareIndex("second_test_index").setId("2").setSource("type", "type2", "field4", "a quick fast blue car").get(); refresh(); - source = searchSource().postFilter(termQuery("type", "type2")).query(matchPhrasePrefixQuery("field3", "fast bro")) + source = searchSource().postFilter(termQuery("type", "type2")) + .query(matchPhrasePrefixQuery("field3", "fast bro")) .highlighter(highlight().field("field3").order("score").preTags("").postTags("")); searchResponse = client().search(searchRequest("second_test_index").source(source)).actionGet(); - assertHighlight(searchResponse, 0, "field3", 0, 1, - equalTo("The quick brown fox jumps over the lazy dog")); + assertHighlight(searchResponse, 0, "field3", 0, 1, equalTo("The quick brown fox jumps over the lazy dog")); logger.info("--> highlighting and searching on field4"); - source = searchSource().postFilter(termQuery("type", "type2")).query(matchPhrasePrefixQuery("field4", "the fast bro")) + source = searchSource().postFilter(termQuery("type", "type2")) + .query(matchPhrasePrefixQuery("field4", "the fast bro")) .highlighter(highlight().field("field4").order("score").preTags("").postTags("")); searchResponse = client().search(searchRequest("second_test_index").source(source)).actionGet(); - assertHighlight(searchResponse, 0, "field4", 0, 1, anyOf( - equalTo("The quick browse button is a fancy thing, right bro?"), - equalTo("The quick brown fox jumps over the lazy dog"))); - assertHighlight(searchResponse, 1, "field4", 0, 1, anyOf( - equalTo("The quick browse button is a fancy thing, right bro?"), - equalTo("The quick brown fox jumps over the lazy dog"))); + assertHighlight( + searchResponse, + 0, + "field4", + 0, + 1, + anyOf( + equalTo("The quick browse button is a fancy thing, right bro?"), + equalTo("The quick brown fox jumps over the lazy dog") + ) + ); + assertHighlight( + searchResponse, + 1, + "field4", + 0, + 1, + anyOf( + equalTo("The quick browse button is a fancy thing, right bro?"), + equalTo("The quick brown fox jumps over the lazy dog") + ) + ); logger.info("--> highlighting and searching on field4"); source = searchSource().postFilter(termQuery("type", "type2")) @@ -300,17 +380,31 @@ public void testPhrasePrefix() throws IOException { .highlighter(highlight().field("field4").order("score").preTags("").postTags("")); searchResponse = client().search(searchRequest("second_test_index").source(source)).actionGet(); - assertHighlight(searchResponse, 0, "field4", 0, 1, - anyOf(equalTo("a quick fast blue car"), - equalTo("a quick fast blue car"))); + assertHighlight( + searchResponse, + 0, + "field4", + 0, + 1, + anyOf(equalTo("a quick fast blue car"), equalTo("a quick fast blue car")) + ); } public static XContentBuilder type1TermVectorMapping() throws IOException { - return XContentFactory.jsonBuilder().startObject().startObject("_doc") + return XContentFactory.jsonBuilder() + .startObject() + .startObject("_doc") .startObject("properties") - .startObject("field1").field("type", "text").field("term_vector", "with_positions_offsets").endObject() - .startObject("field2").field("type", "text").field("term_vector", "with_positions_offsets").endObject() + .startObject("field1") + .field("type", "text") + .field("term_vector", "with_positions_offsets") + .endObject() + .startObject("field2") + .field("type", "text") + .field("term_vector", "with_positions_offsets") + .endObject() + .endObject() .endObject() - .endObject().endObject(); + .endObject(); } } diff --git a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/KeepFilterFactoryTests.java b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/KeepFilterFactoryTests.java index 61c399da2ce98..82f8d0b5420dc 100644 --- a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/KeepFilterFactoryTests.java +++ b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/KeepFilterFactoryTests.java @@ -28,51 +28,49 @@ public class KeepFilterFactoryTests extends ESTokenStreamTestCase { public void testLoadWithoutSettings() throws IOException { ESTestCase.TestAnalysis analysis = AnalysisTestsHelper.createTestAnalysisFromClassPath( - createTempDir(), RESOURCE, new CommonAnalysisPlugin()); + createTempDir(), + RESOURCE, + new CommonAnalysisPlugin() + ); TokenFilterFactory tokenFilter = analysis.tokenFilter.get("keep"); Assert.assertNull(tokenFilter); } public void testLoadOverConfiguredSettings() { Settings settings = Settings.builder() - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .put("index.analysis.filter.broken_keep_filter.type", "keep") - .put("index.analysis.filter.broken_keep_filter.keep_words_path", "does/not/exists.txt") - .put("index.analysis.filter.broken_keep_filter.keep_words", "[\"Hello\", \"worlD\"]") - .build(); + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) + .put("index.analysis.filter.broken_keep_filter.type", "keep") + .put("index.analysis.filter.broken_keep_filter.keep_words_path", "does/not/exists.txt") + .put("index.analysis.filter.broken_keep_filter.keep_words", "[\"Hello\", \"worlD\"]") + .build(); try { AnalysisTestsHelper.createTestAnalysisFromSettings(settings, new CommonAnalysisPlugin()); Assert.fail("path and array are configured"); - } catch (IllegalArgumentException e) { - } catch (IOException e) { + } catch (IllegalArgumentException e) {} catch (IOException e) { fail("expected IAE"); } } public void testKeepWordsPathSettings() { Settings settings = Settings.builder() - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .put("index.analysis.filter.non_broken_keep_filter.type", "keep") - .put("index.analysis.filter.non_broken_keep_filter.keep_words_path", "does/not/exists.txt") - .build(); + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) + .put("index.analysis.filter.non_broken_keep_filter.type", "keep") + .put("index.analysis.filter.non_broken_keep_filter.keep_words_path", "does/not/exists.txt") + .build(); try { // test our none existing setup is picked up AnalysisTestsHelper.createTestAnalysisFromSettings(settings, new CommonAnalysisPlugin()); fail("expected an exception due to non existent keep_words_path"); - } catch (IllegalArgumentException e) { - } catch (IOException e) { + } catch (IllegalArgumentException e) {} catch (IOException e) { fail("expected IAE"); } - settings = Settings.builder().put(settings) - .putList("index.analysis.filter.non_broken_keep_filter.keep_words", "test") - .build(); + settings = Settings.builder().put(settings).putList("index.analysis.filter.non_broken_keep_filter.keep_words", "test").build(); try { // test our none existing setup is picked up AnalysisTestsHelper.createTestAnalysisFromSettings(settings, new CommonAnalysisPlugin()); fail("expected an exception indicating that you can't use [keep_words_path] with [keep_words] "); - } catch (IllegalArgumentException e) { - } catch (IOException e) { + } catch (IllegalArgumentException e) {} catch (IOException e) { fail("expected IAE"); } @@ -80,25 +78,31 @@ public void testKeepWordsPathSettings() { public void testCaseInsensitiveMapping() throws IOException { ESTestCase.TestAnalysis analysis = AnalysisTestsHelper.createTestAnalysisFromClassPath( - createTempDir(), RESOURCE, new CommonAnalysisPlugin()); + createTempDir(), + RESOURCE, + new CommonAnalysisPlugin() + ); TokenFilterFactory tokenFilter = analysis.tokenFilter.get("my_keep_filter"); assertThat(tokenFilter, instanceOf(KeepWordFilterFactory.class)); String source = "hello small world"; - String[] expected = new String[]{"hello", "world"}; + String[] expected = new String[] { "hello", "world" }; Tokenizer tokenizer = new WhitespaceTokenizer(); tokenizer.setReader(new StringReader(source)); - assertTokenStreamContents(tokenFilter.create(tokenizer), expected, new int[]{1, 2}); + assertTokenStreamContents(tokenFilter.create(tokenizer), expected, new int[] { 1, 2 }); } public void testCaseSensitiveMapping() throws IOException { ESTestCase.TestAnalysis analysis = AnalysisTestsHelper.createTestAnalysisFromClassPath( - createTempDir(), RESOURCE, new CommonAnalysisPlugin()); + createTempDir(), + RESOURCE, + new CommonAnalysisPlugin() + ); TokenFilterFactory tokenFilter = analysis.tokenFilter.get("my_case_sensitive_keep_filter"); assertThat(tokenFilter, instanceOf(KeepWordFilterFactory.class)); String source = "Hello small world"; - String[] expected = new String[]{"Hello"}; + String[] expected = new String[] { "Hello" }; Tokenizer tokenizer = new WhitespaceTokenizer(); tokenizer.setReader(new StringReader(source)); - assertTokenStreamContents(tokenFilter.create(tokenizer), expected, new int[]{1}); + assertTokenStreamContents(tokenFilter.create(tokenizer), expected, new int[] { 1 }); } } diff --git a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/KeepTypesFilterFactoryTests.java b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/KeepTypesFilterFactoryTests.java index 1c811306dd6be..06dd33d2b1740 100644 --- a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/KeepTypesFilterFactoryTests.java +++ b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/KeepTypesFilterFactoryTests.java @@ -27,13 +27,16 @@ public class KeepTypesFilterFactoryTests extends ESTokenStreamTestCase { private static final String BASE_SETTING = "index.analysis.filter.keep_numbers"; public void testKeepTypesInclude() throws IOException { - Settings.Builder settingsBuilder = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .put(BASE_SETTING + ".type", "keep_types") - .putList(BASE_SETTING + "." + KeepTypesFilterFactory.KEEP_TYPES_KEY, new String[] { "", "" }); + Settings.Builder settingsBuilder = Settings.builder() + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) + .put(BASE_SETTING + ".type", "keep_types") + .putList(BASE_SETTING + "." + KeepTypesFilterFactory.KEEP_TYPES_KEY, new String[] { "", "" }); // either use default mode or set "include" mode explicitly if (random().nextBoolean()) { - settingsBuilder.put(BASE_SETTING + "." + KeepTypesFilterFactory.KEEP_TYPES_MODE_KEY, - KeepTypesFilterFactory.KeepTypesMode.INCLUDE); + settingsBuilder.put( + BASE_SETTING + "." + KeepTypesFilterFactory.KEEP_TYPES_MODE_KEY, + KeepTypesFilterFactory.KeepTypesMode.INCLUDE + ); } Settings settings = settingsBuilder.build(); ESTestCase.TestAnalysis analysis = AnalysisTestsHelper.createTestAnalysisFromSettings(settings, new CommonAnalysisPlugin()); @@ -47,10 +50,12 @@ public void testKeepTypesInclude() throws IOException { } public void testKeepTypesExclude() throws IOException { - Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .put(BASE_SETTING + ".type", "keep_types") - .putList(BASE_SETTING + "." + KeepTypesFilterFactory.KEEP_TYPES_KEY, new String[] { "", "" }) - .put(BASE_SETTING + "." + KeepTypesFilterFactory.KEEP_TYPES_MODE_KEY, KeepTypesFilterFactory.KeepTypesMode.EXCLUDE).build(); + Settings settings = Settings.builder() + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) + .put(BASE_SETTING + ".type", "keep_types") + .putList(BASE_SETTING + "." + KeepTypesFilterFactory.KEEP_TYPES_KEY, new String[] { "", "" }) + .put(BASE_SETTING + "." + KeepTypesFilterFactory.KEEP_TYPES_MODE_KEY, KeepTypesFilterFactory.KeepTypesMode.EXCLUDE) + .build(); ESTestCase.TestAnalysis analysis = AnalysisTestsHelper.createTestAnalysisFromSettings(settings, new CommonAnalysisPlugin()); TokenFilterFactory tokenFilter = analysis.tokenFilter.get("keep_numbers"); assertThat(tokenFilter, instanceOf(KeepTypesFilterFactory.class)); @@ -62,12 +67,16 @@ public void testKeepTypesExclude() throws IOException { } public void testKeepTypesException() throws IOException { - Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .put(BASE_SETTING + ".type", "keep_types") - .putList(BASE_SETTING + "." + KeepTypesFilterFactory.KEEP_TYPES_KEY, new String[] { "", "" }) - .put(BASE_SETTING + "." + KeepTypesFilterFactory.KEEP_TYPES_MODE_KEY, "bad_parameter").build(); - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, - () -> AnalysisTestsHelper.createTestAnalysisFromSettings(settings, new CommonAnalysisPlugin())); + Settings settings = Settings.builder() + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) + .put(BASE_SETTING + ".type", "keep_types") + .putList(BASE_SETTING + "." + KeepTypesFilterFactory.KEEP_TYPES_KEY, new String[] { "", "" }) + .put(BASE_SETTING + "." + KeepTypesFilterFactory.KEEP_TYPES_MODE_KEY, "bad_parameter") + .build(); + IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> AnalysisTestsHelper.createTestAnalysisFromSettings(settings, new CommonAnalysisPlugin()) + ); assertEquals("`keep_types` tokenfilter mode can only be [include] or [exclude] but was [bad_parameter].", ex.getMessage()); } } diff --git a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/KeywordMarkerFilterFactoryTests.java b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/KeywordMarkerFilterFactoryTests.java index 5a587b72888c4..0242b4dcbd3a7 100644 --- a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/KeywordMarkerFilterFactoryTests.java +++ b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/KeywordMarkerFilterFactoryTests.java @@ -48,8 +48,7 @@ public void testKeywordSet() throws IOException { assertThat(filter, instanceOf(SetKeywordMarkerFilter.class)); NamedAnalyzer analyzer = analysis.indexAnalyzers.get("my_keyword"); // jogging is not part of the keywords set, so verify that its the only stemmed word - assertAnalyzesTo(analyzer, "running jogging sleeping", - new String[] { "running", "jog", "sleeping" }); + assertAnalyzesTo(analyzer, "running jogging sleeping", new String[] { "running", "jog", "sleeping" }); } /** @@ -87,9 +86,10 @@ public void testCannotSpecifyBothKeywordsAndPattern() throws IOException { .put("index.analysis.analyzer.my_keyword.filter", "my_keyword, porter_stem") .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) .build(); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> AnalysisTestsHelper.createTestAnalysisFromSettings(settings, new CommonAnalysisPlugin())); - assertEquals("cannot specify both `keywords_pattern` and `keywords` or `keywords_path`", - e.getMessage()); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> AnalysisTestsHelper.createTestAnalysisFromSettings(settings, new CommonAnalysisPlugin()) + ); + assertEquals("cannot specify both `keywords_pattern` and `keywords` or `keywords_path`", e.getMessage()); } } diff --git a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/LimitTokenCountFilterFactoryTests.java b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/LimitTokenCountFilterFactoryTests.java index 95f0417ea19e0..f8cd8aea9c5e5 100644 --- a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/LimitTokenCountFilterFactoryTests.java +++ b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/LimitTokenCountFilterFactoryTests.java @@ -23,9 +23,9 @@ public class LimitTokenCountFilterFactoryTests extends ESTokenStreamTestCase { public void testDefault() throws IOException { Settings settings = Settings.builder() - .put("index.analysis.filter.limit_default.type", "limit") - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .build(); + .put("index.analysis.filter.limit_default.type", "limit") + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) + .build(); ESTestCase.TestAnalysis analysis = createTestAnalysisFromSettings(settings); { TokenFilterFactory tokenFilter = analysis.tokenFilter.get("limit_default"); @@ -48,11 +48,11 @@ public void testDefault() throws IOException { public void testSettings() throws IOException { { Settings settings = Settings.builder() - .put("index.analysis.filter.limit_1.type", "limit") - .put("index.analysis.filter.limit_1.max_token_count", 3) - .put("index.analysis.filter.limit_1.consume_all_tokens", true) - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .build(); + .put("index.analysis.filter.limit_1.type", "limit") + .put("index.analysis.filter.limit_1.max_token_count", 3) + .put("index.analysis.filter.limit_1.consume_all_tokens", true) + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) + .build(); ESTestCase.TestAnalysis analysis = createTestAnalysisFromSettings(settings); TokenFilterFactory tokenFilter = analysis.tokenFilter.get("limit_1"); String source = "the quick brown fox"; @@ -63,11 +63,11 @@ public void testSettings() throws IOException { } { Settings settings = Settings.builder() - .put("index.analysis.filter.limit_1.type", "limit") - .put("index.analysis.filter.limit_1.max_token_count", 3) - .put("index.analysis.filter.limit_1.consume_all_tokens", false) - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .build(); + .put("index.analysis.filter.limit_1.type", "limit") + .put("index.analysis.filter.limit_1.max_token_count", 3) + .put("index.analysis.filter.limit_1.consume_all_tokens", false) + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) + .build(); ESTestCase.TestAnalysis analysis = createTestAnalysisFromSettings(settings); TokenFilterFactory tokenFilter = analysis.tokenFilter.get("limit_1"); String source = "the quick brown fox"; @@ -79,11 +79,11 @@ public void testSettings() throws IOException { { Settings settings = Settings.builder() - .put("index.analysis.filter.limit_1.type", "limit") - .put("index.analysis.filter.limit_1.max_token_count", 17) - .put("index.analysis.filter.limit_1.consume_all_tokens", true) - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .build(); + .put("index.analysis.filter.limit_1.type", "limit") + .put("index.analysis.filter.limit_1.max_token_count", 17) + .put("index.analysis.filter.limit_1.consume_all_tokens", true) + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) + .build(); ESTestCase.TestAnalysis analysis = createTestAnalysisFromSettings(settings); TokenFilterFactory tokenFilter = analysis.tokenFilter.get("limit_1"); String source = "the quick brown fox"; diff --git a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/MassiveWordListTests.java b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/MassiveWordListTests.java index 8706cad0e6773..0c7c41fec693d 100644 --- a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/MassiveWordListTests.java +++ b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/MassiveWordListTests.java @@ -27,13 +27,18 @@ public void testCreateIndexWithMassiveWordList() { for (int i = 0; i < wordList.length; i++) { wordList[i] = "hello world"; } - client().admin().indices().prepareCreate("test").setSettings(Settings.builder() - .put("index.number_of_shards", 1) - .put("analysis.analyzer.test_analyzer.type", "custom") - .put("analysis.analyzer.test_analyzer.tokenizer", "standard") - .putList("analysis.analyzer.test_analyzer.filter", "dictionary_decompounder", "lowercase") - .put("analysis.filter.dictionary_decompounder.type", "dictionary_decompounder") - .putList("analysis.filter.dictionary_decompounder.word_list", wordList) - ).get(); + client().admin() + .indices() + .prepareCreate("test") + .setSettings( + Settings.builder() + .put("index.number_of_shards", 1) + .put("analysis.analyzer.test_analyzer.type", "custom") + .put("analysis.analyzer.test_analyzer.tokenizer", "standard") + .putList("analysis.analyzer.test_analyzer.filter", "dictionary_decompounder", "lowercase") + .put("analysis.filter.dictionary_decompounder.type", "dictionary_decompounder") + .putList("analysis.filter.dictionary_decompounder.word_list", wordList) + ) + .get(); } } diff --git a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/MinHashFilterFactoryTests.java b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/MinHashFilterFactoryTests.java index c3974d198e282..4525d42586f71 100644 --- a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/MinHashFilterFactoryTests.java +++ b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/MinHashFilterFactoryTests.java @@ -25,9 +25,7 @@ public void testDefault() throws IOException { int default_hash_count = 1; int default_bucket_size = 512; int default_hash_set_size = 1; - Settings settings = Settings.builder() - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .build(); + Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build(); ESTestCase.TestAnalysis analysis = AnalysisTestsHelper.createTestAnalysisFromSettings(settings, new CommonAnalysisPlugin()); TokenFilterFactory tokenFilter = analysis.tokenFilter.get("min_hash"); String source = "the quick brown fox"; @@ -36,8 +34,7 @@ public void testDefault() throws IOException { // with_rotation is true by default, and hash_set_size is 1, so even though the source doesn't // have enough tokens to fill all the buckets, we still expect 512 tokens. - assertStreamHasNumberOfTokens(tokenFilter.create(tokenizer), - default_hash_count * default_bucket_size * default_hash_set_size); + assertStreamHasNumberOfTokens(tokenFilter.create(tokenizer), default_hash_count * default_bucket_size * default_hash_set_size); } public void testSettings() throws IOException { diff --git a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/MultiplexerTokenFilterTests.java b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/MultiplexerTokenFilterTests.java index b988e35bb2945..4d350e5f399e8 100644 --- a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/MultiplexerTokenFilterTests.java +++ b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/MultiplexerTokenFilterTests.java @@ -26,9 +26,7 @@ public class MultiplexerTokenFilterTests extends ESTokenStreamTestCase { public void testMultiplexingFilter() throws IOException { - Settings settings = Settings.builder() - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .build(); + Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build(); Settings indexSettings = Settings.builder() .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) .put("index.analysis.filter.t.type", "truncate") @@ -41,30 +39,27 @@ public void testMultiplexingFilter() throws IOException { .build(); IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("index", indexSettings); - IndexAnalyzers indexAnalyzers = new AnalysisModule(TestEnvironment.newEnvironment(settings), - Collections.singletonList(new CommonAnalysisPlugin())).getAnalysisRegistry().build(idxSettings); + IndexAnalyzers indexAnalyzers = new AnalysisModule( + TestEnvironment.newEnvironment(settings), + Collections.singletonList(new CommonAnalysisPlugin()) + ).getAnalysisRegistry().build(idxSettings); try (NamedAnalyzer analyzer = indexAnalyzers.get("myAnalyzer")) { assertNotNull(analyzer); - assertAnalyzesTo(analyzer, "ONe tHree", new String[]{ - "ONe", "on", "ONE", "tHree", "th", "THREE" - }, new int[]{ - 1, 0, 0, 1, 0, 0 - }); + assertAnalyzesTo( + analyzer, + "ONe tHree", + new String[] { "ONe", "on", "ONE", "tHree", "th", "THREE" }, + new int[] { 1, 0, 0, 1, 0, 0 } + ); // Duplicates are removed - assertAnalyzesTo(analyzer, "ONe THREE", new String[]{ - "ONe", "on", "ONE", "THREE", "th" - }, new int[]{ - 1, 0, 0, 1, 0, 0 - }); + assertAnalyzesTo(analyzer, "ONe THREE", new String[] { "ONe", "on", "ONE", "THREE", "th" }, new int[] { 1, 0, 0, 1, 0, 0 }); } } public void testMultiplexingNoOriginal() throws IOException { - Settings settings = Settings.builder() - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .build(); + Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build(); Settings indexSettings = Settings.builder() .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) .put("index.analysis.filter.t.type", "truncate") @@ -78,16 +73,14 @@ public void testMultiplexingNoOriginal() throws IOException { .build(); IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("index", indexSettings); - IndexAnalyzers indexAnalyzers = new AnalysisModule(TestEnvironment.newEnvironment(settings), - Collections.singletonList(new CommonAnalysisPlugin())).getAnalysisRegistry().build(idxSettings); + IndexAnalyzers indexAnalyzers = new AnalysisModule( + TestEnvironment.newEnvironment(settings), + Collections.singletonList(new CommonAnalysisPlugin()) + ).getAnalysisRegistry().build(idxSettings); try (NamedAnalyzer analyzer = indexAnalyzers.get("myAnalyzer")) { assertNotNull(analyzer); - assertAnalyzesTo(analyzer, "ONe tHree", new String[]{ - "on", "ONE", "th", "THREE" - }, new int[]{ - 1, 0, 1, 0, - }); + assertAnalyzesTo(analyzer, "ONe tHree", new String[] { "on", "ONE", "th", "THREE" }, new int[] { 1, 0, 1, 0, }); } } diff --git a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/NGramTokenFilterFactoryTests.java b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/NGramTokenFilterFactoryTests.java index ec9cedb1995ed..ab37cada806b7 100644 --- a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/NGramTokenFilterFactoryTests.java +++ b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/NGramTokenFilterFactoryTests.java @@ -28,10 +28,11 @@ public void testDefault() throws IOException { .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) .put("index.analysis.filter.my_ngram.type", "ngram") .build(), - new CommonAnalysisPlugin()); + new CommonAnalysisPlugin() + ); TokenFilterFactory tokenFilter = analysis.tokenFilter.get("my_ngram"); String source = "foo"; - String[] expected = new String[]{"f", "fo", "o", "oo", "o"}; + String[] expected = new String[] { "f", "fo", "o", "oo", "o" }; Tokenizer tokenizer = new StandardTokenizer(); tokenizer.setReader(new StringReader(source)); assertTokenStreamContents(tokenFilter.create(tokenizer), expected); @@ -44,10 +45,11 @@ public void testPreserveOriginal() throws IOException { .put("index.analysis.filter.my_ngram.type", "ngram") .put("index.analysis.filter.my_ngram.preserve_original", true) .build(), - new CommonAnalysisPlugin()); + new CommonAnalysisPlugin() + ); TokenFilterFactory tokenFilter = analysis.tokenFilter.get("my_ngram"); String source = "foo"; - String[] expected = new String[]{"f", "fo", "o", "oo", "o", "foo"}; + String[] expected = new String[] { "f", "fo", "o", "oo", "o", "foo" }; Tokenizer tokenizer = new StandardTokenizer(); tokenizer.setReader(new StringReader(source)); assertTokenStreamContents(tokenFilter.create(tokenizer), expected); diff --git a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/NGramTokenizerFactoryTests.java b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/NGramTokenizerFactoryTests.java index 28593bceb0ee8..17b36afe59923 100644 --- a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/NGramTokenizerFactoryTests.java +++ b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/NGramTokenizerFactoryTests.java @@ -37,30 +37,43 @@ public void testParseTokenChars() { final Settings indexSettings = newAnalysisSettingsBuilder().build(); final IndexSettings indexProperties = IndexSettingsModule.newIndexSettings(index, indexSettings); for (String tokenChars : Arrays.asList("letter", " digit ", "punctuation", "DIGIT", "CoNtRoL", "dash_punctuation")) { - final Settings settings = newAnalysisSettingsBuilder().put("min_gram", 2).put("max_gram", 3) - .put("token_chars", tokenChars).build(); + final Settings settings = newAnalysisSettingsBuilder().put("min_gram", 2) + .put("max_gram", 3) + .put("token_chars", tokenChars) + .build(); new NGramTokenizerFactory(indexProperties, null, name, settings).create(); // no exception } { - final Settings settings = newAnalysisSettingsBuilder().put("min_gram", 2).put("max_gram", 3) - .put("token_chars", "DIRECTIONALITY_UNDEFINED").build(); - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, - () -> new NGramTokenizerFactory(indexProperties, null, name, settings).create()); + final Settings settings = newAnalysisSettingsBuilder().put("min_gram", 2) + .put("max_gram", 3) + .put("token_chars", "DIRECTIONALITY_UNDEFINED") + .build(); + IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> new NGramTokenizerFactory(indexProperties, null, name, settings).create() + ); assertEquals("Unknown token type: 'directionality_undefined'", ex.getMessage().substring(0, 46)); assertTrue(ex.getMessage().contains("custom")); } { - final Settings settings = newAnalysisSettingsBuilder().put("min_gram", 2).put("max_gram", 3).put("token_chars", "custom") - .put("custom_token_chars", "_-").build(); + final Settings settings = newAnalysisSettingsBuilder().put("min_gram", 2) + .put("max_gram", 3) + .put("token_chars", "custom") + .put("custom_token_chars", "_-") + .build(); new NGramTokenizerFactory(indexProperties, null, name, settings).create(); // no exception } { - final Settings settings = newAnalysisSettingsBuilder().put("min_gram", 2).put("max_gram", 3).put("token_chars", "custom") - .build(); - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, - () -> new NGramTokenizerFactory(indexProperties, null, name, settings).create()); + final Settings settings = newAnalysisSettingsBuilder().put("min_gram", 2) + .put("max_gram", 3) + .put("token_chars", "custom") + .build(); + IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> new NGramTokenizerFactory(indexProperties, null, name, settings).create() + ); assertEquals("Token type: 'custom' requires setting `custom_token_chars`", ex.getMessage()); } } @@ -70,12 +83,14 @@ public void testNoTokenChars() throws IOException { final String name = "ngr"; final Settings indexSettings = newAnalysisSettingsBuilder().put(IndexSettings.MAX_NGRAM_DIFF_SETTING.getKey(), 2).build(); - final Settings settings = newAnalysisSettingsBuilder().put("min_gram", 2).put("max_gram", 4) - .putList("token_chars", new String[0]).build(); + final Settings settings = newAnalysisSettingsBuilder().put("min_gram", 2) + .put("max_gram", 4) + .putList("token_chars", new String[0]) + .build(); Tokenizer tokenizer = new NGramTokenizerFactory(IndexSettingsModule.newIndexSettings(index, indexSettings), null, name, settings) .create(); tokenizer.setReader(new StringReader("1.34")); - assertTokenStreamContents(tokenizer, new String[] {"1.", "1.3", "1.34", ".3", ".34", "34"}); + assertTokenStreamContents(tokenizer, new String[] { "1.", "1.3", "1.34", ".3", ".34", "34" }); } public void testCustomTokenChars() throws IOException { @@ -83,12 +98,15 @@ public void testCustomTokenChars() throws IOException { final String name = "ngr"; final Settings indexSettings = newAnalysisSettingsBuilder().put(IndexSettings.MAX_NGRAM_DIFF_SETTING.getKey(), 2).build(); - final Settings settings = newAnalysisSettingsBuilder().put("min_gram", 2).put("max_gram", 3) - .putList("token_chars", "letter", "custom").put("custom_token_chars","_-").build(); + final Settings settings = newAnalysisSettingsBuilder().put("min_gram", 2) + .put("max_gram", 3) + .putList("token_chars", "letter", "custom") + .put("custom_token_chars", "_-") + .build(); Tokenizer tokenizer = new NGramTokenizerFactory(IndexSettingsModule.newIndexSettings(index, indexSettings), null, name, settings) .create(); tokenizer.setReader(new StringReader("Abc -gh _jk =lm")); - assertTokenStreamContents(tokenizer, new String[] {"Ab", "Abc", "bc", "-g", "-gh", "gh", "_j", "_jk", "jk", "lm"}); + assertTokenStreamContents(tokenizer, new String[] { "Ab", "Abc", "bc", "-g", "-gh", "gh", "_j", "_jk", "jk", "lm" }); } public void testPreTokenization() throws IOException { @@ -96,19 +114,21 @@ public void testPreTokenization() throws IOException { final Index index = new Index("test", "_na_"); final String name = "ngr"; final Settings indexSettings = newAnalysisSettingsBuilder().build(); - Settings settings = newAnalysisSettingsBuilder().put("min_gram", 2).put("max_gram", 3) - .put("token_chars", "letter,digit").build(); + Settings settings = newAnalysisSettingsBuilder().put("min_gram", 2).put("max_gram", 3).put("token_chars", "letter,digit").build(); Tokenizer tokenizer = new NGramTokenizerFactory(IndexSettingsModule.newIndexSettings(index, indexSettings), null, name, settings) .create(); tokenizer.setReader(new StringReader("Åbc déf g\uD801\uDC00f ")); - assertTokenStreamContents(tokenizer, - new String[] {"Åb", "Åbc", "bc", "dé", "déf", "éf", "g\uD801\uDC00", "g\uD801\uDC00f", "\uD801\uDC00f"}); - settings = newAnalysisSettingsBuilder().put("min_gram", 2).put("max_gram", 3) - .put("token_chars", "letter,digit,punctuation,whitespace,symbol").build(); + assertTokenStreamContents( + tokenizer, + new String[] { "Åb", "Åbc", "bc", "dé", "déf", "éf", "g\uD801\uDC00", "g\uD801\uDC00f", "\uD801\uDC00f" } + ); + settings = newAnalysisSettingsBuilder().put("min_gram", 2) + .put("max_gram", 3) + .put("token_chars", "letter,digit,punctuation,whitespace,symbol") + .build(); tokenizer = new NGramTokenizerFactory(IndexSettingsModule.newIndexSettings(index, indexSettings), null, name, settings).create(); tokenizer.setReader(new StringReader(" a!$ 9")); - assertTokenStreamContents(tokenizer, - new String[] {" a", " a!", "a!", "a!$", "!$", "!$ ", "$ ", "$ 9", " 9"}); + assertTokenStreamContents(tokenizer, new String[] { " a", " a!", "a!", "a!$", "!$", "!$ ", "$ ", "$ 9", " 9" }); } public void testPreTokenizationEdge() throws IOException { @@ -117,18 +137,22 @@ public void testPreTokenizationEdge() throws IOException { final String name = "ngr"; final Settings indexSettings = newAnalysisSettingsBuilder().build(); Settings settings = newAnalysisSettingsBuilder().put("min_gram", 2).put("max_gram", 3).put("token_chars", "letter,digit").build(); - Tokenizer tokenizer = - new EdgeNGramTokenizerFactory(IndexSettingsModule.newIndexSettings(index, indexSettings), null, name, settings).create(); + Tokenizer tokenizer = new EdgeNGramTokenizerFactory( + IndexSettingsModule.newIndexSettings(index, indexSettings), + null, + name, + settings + ).create(); tokenizer.setReader(new StringReader("Åbc déf g\uD801\uDC00f ")); - assertTokenStreamContents(tokenizer, - new String[] {"Åb", "Åbc", "dé", "déf", "g\uD801\uDC00", "g\uD801\uDC00f"}); - settings = newAnalysisSettingsBuilder().put("min_gram", 2).put("max_gram", 3) - .put("token_chars", "letter,digit,punctuation,whitespace,symbol").build(); + assertTokenStreamContents(tokenizer, new String[] { "Åb", "Åbc", "dé", "déf", "g\uD801\uDC00", "g\uD801\uDC00f" }); + settings = newAnalysisSettingsBuilder().put("min_gram", 2) + .put("max_gram", 3) + .put("token_chars", "letter,digit,punctuation,whitespace,symbol") + .build(); tokenizer = new EdgeNGramTokenizerFactory(IndexSettingsModule.newIndexSettings(index, indexSettings), null, name, settings) .create(); tokenizer.setReader(new StringReader(" a!$ 9")); - assertTokenStreamContents(tokenizer, - new String[] {" a", " a!"}); + assertTokenStreamContents(tokenizer, new String[] { " a", " a!" }); } public void testBackwardsCompatibilityEdgeNgramTokenFilter() throws Exception { @@ -146,9 +170,12 @@ public void testBackwardsCompatibilityEdgeNgramTokenFilter() throws Exception { Settings indexSettings = newAnalysisSettingsBuilder().put(IndexMetadata.SETTING_VERSION_CREATED, v.id).build(); Tokenizer tokenizer = new MockTokenizer(); tokenizer.setReader(new StringReader("foo bar")); - TokenStream edgeNGramTokenFilter = - new EdgeNGramTokenFilterFactory(IndexSettingsModule.newIndexSettings(index, indexSettings), null, name, settings) - .create(tokenizer); + TokenStream edgeNGramTokenFilter = new EdgeNGramTokenFilterFactory( + IndexSettingsModule.newIndexSettings(index, indexSettings), + null, + name, + settings + ).create(tokenizer); if (reverse) { assertThat(edgeNGramTokenFilter, instanceOf(ReverseStringFilter.class)); } else { @@ -161,7 +188,7 @@ public void testBackwardsCompatibilityEdgeNgramTokenFilter() throws Exception { * test that throws an error when trying to get a NGramTokenizer where difference between max_gram and min_gram * is greater than the allowed value of max_ngram_diff */ - public void testMaxNGramDiffException() throws Exception{ + public void testMaxNGramDiffException() throws Exception { final Index index = new Index("test", "_na_"); final String name = "ngr"; final Settings indexSettings = newAnalysisSettingsBuilder().build(); @@ -173,12 +200,19 @@ public void testMaxNGramDiffException() throws Exception{ int max_gram = min_gram + ngramDiff; final Settings settings = newAnalysisSettingsBuilder().put("min_gram", min_gram).put("max_gram", max_gram).build(); - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> - new NGramTokenizerFactory(indexProperties, null, name, settings).create()); + IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> new NGramTokenizerFactory(indexProperties, null, name, settings).create() + ); assertEquals( "The difference between max_gram and min_gram in NGram Tokenizer must be less than or equal to: [" - + maxAllowedNgramDiff + "] but was [" + ngramDiff + "]. This limit can be set by changing the [" - + IndexSettings.MAX_NGRAM_DIFF_SETTING.getKey() + "] index level setting.", - ex.getMessage()); + + maxAllowedNgramDiff + + "] but was [" + + ngramDiff + + "]. This limit can be set by changing the [" + + IndexSettings.MAX_NGRAM_DIFF_SETTING.getKey() + + "] index level setting.", + ex.getMessage() + ); } } diff --git a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/PathHierarchyTokenizerFactoryTests.java b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/PathHierarchyTokenizerFactoryTests.java index 65d7010b24eb2..64b54e4166134 100644 --- a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/PathHierarchyTokenizerFactoryTests.java +++ b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/PathHierarchyTokenizerFactoryTests.java @@ -24,73 +24,105 @@ public class PathHierarchyTokenizerFactoryTests extends ESTokenStreamTestCase { public void testDefaults() throws IOException { final Index index = new Index("test", "_na_"); final Settings indexSettings = newAnalysisSettingsBuilder().build(); - Tokenizer tokenizer = new PathHierarchyTokenizerFactory(IndexSettingsModule.newIndexSettings(index, indexSettings), null, - "path-hierarchy-tokenizer", Settings.EMPTY).create(); + Tokenizer tokenizer = new PathHierarchyTokenizerFactory( + IndexSettingsModule.newIndexSettings(index, indexSettings), + null, + "path-hierarchy-tokenizer", + Settings.EMPTY + ).create(); tokenizer.setReader(new StringReader("/one/two/three")); - assertTokenStreamContents(tokenizer, new String[] {"/one", "/one/two", "/one/two/three"}); + assertTokenStreamContents(tokenizer, new String[] { "/one", "/one/two", "/one/two/three" }); } public void testReverse() throws IOException { final Index index = new Index("test", "_na_"); final Settings indexSettings = newAnalysisSettingsBuilder().build(); Settings settings = newAnalysisSettingsBuilder().put("reverse", true).build(); - Tokenizer tokenizer = new PathHierarchyTokenizerFactory(IndexSettingsModule.newIndexSettings(index, indexSettings), null, - "path-hierarchy-tokenizer", settings).create(); + Tokenizer tokenizer = new PathHierarchyTokenizerFactory( + IndexSettingsModule.newIndexSettings(index, indexSettings), + null, + "path-hierarchy-tokenizer", + settings + ).create(); tokenizer.setReader(new StringReader("/one/two/three")); - assertTokenStreamContents(tokenizer, new String[] {"/one/two/three", "one/two/three", "two/three", "three"}); + assertTokenStreamContents(tokenizer, new String[] { "/one/two/three", "one/two/three", "two/three", "three" }); } public void testDelimiter() throws IOException { final Index index = new Index("test", "_na_"); final Settings indexSettings = newAnalysisSettingsBuilder().build(); Settings settings = newAnalysisSettingsBuilder().put("delimiter", "-").build(); - Tokenizer tokenizer = new PathHierarchyTokenizerFactory(IndexSettingsModule.newIndexSettings(index, indexSettings), null, - "path-hierarchy-tokenizer", settings).create(); + Tokenizer tokenizer = new PathHierarchyTokenizerFactory( + IndexSettingsModule.newIndexSettings(index, indexSettings), + null, + "path-hierarchy-tokenizer", + settings + ).create(); tokenizer.setReader(new StringReader("/one/two/three")); - assertTokenStreamContents(tokenizer, new String[] {"/one/two/three"}); + assertTokenStreamContents(tokenizer, new String[] { "/one/two/three" }); tokenizer.setReader(new StringReader("one-two-three")); - assertTokenStreamContents(tokenizer, new String[] {"one", "one-two", "one-two-three"}); + assertTokenStreamContents(tokenizer, new String[] { "one", "one-two", "one-two-three" }); } public void testReplace() throws IOException { final Index index = new Index("test", "_na_"); final Settings indexSettings = newAnalysisSettingsBuilder().build(); Settings settings = newAnalysisSettingsBuilder().put("replacement", "-").build(); - Tokenizer tokenizer = new PathHierarchyTokenizerFactory(IndexSettingsModule.newIndexSettings(index, indexSettings), null, - "path-hierarchy-tokenizer", settings).create(); + Tokenizer tokenizer = new PathHierarchyTokenizerFactory( + IndexSettingsModule.newIndexSettings(index, indexSettings), + null, + "path-hierarchy-tokenizer", + settings + ).create(); tokenizer.setReader(new StringReader("/one/two/three")); - assertTokenStreamContents(tokenizer, new String[] {"-one", "-one-two", "-one-two-three"}); + assertTokenStreamContents(tokenizer, new String[] { "-one", "-one-two", "-one-two-three" }); tokenizer.setReader(new StringReader("one-two-three")); - assertTokenStreamContents(tokenizer, new String[] {"one-two-three"}); + assertTokenStreamContents(tokenizer, new String[] { "one-two-three" }); } public void testSkip() throws IOException { final Index index = new Index("test", "_na_"); final Settings indexSettings = newAnalysisSettingsBuilder().build(); Settings settings = newAnalysisSettingsBuilder().put("skip", 2).build(); - Tokenizer tokenizer = new PathHierarchyTokenizerFactory(IndexSettingsModule.newIndexSettings(index, indexSettings), null, - "path-hierarchy-tokenizer", settings).create(); + Tokenizer tokenizer = new PathHierarchyTokenizerFactory( + IndexSettingsModule.newIndexSettings(index, indexSettings), + null, + "path-hierarchy-tokenizer", + settings + ).create(); tokenizer.setReader(new StringReader("/one/two/three/four/five")); - assertTokenStreamContents(tokenizer, new String[] {"/three", "/three/four", "/three/four/five"}); + assertTokenStreamContents(tokenizer, new String[] { "/three", "/three/four", "/three/four/five" }); } public void testDelimiterExceptions() { final Index index = new Index("test", "_na_"); final Settings indexSettings = newAnalysisSettingsBuilder().build(); { - String delimiter = RandomPicks.randomFrom(random(), new String[] {"--", ""}); + String delimiter = RandomPicks.randomFrom(random(), new String[] { "--", "" }); Settings settings = newAnalysisSettingsBuilder().put("delimiter", delimiter).build(); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> new PathHierarchyTokenizerFactory(IndexSettingsModule.newIndexSettings(index, indexSettings), null, - "path-hierarchy-tokenizer", settings).create()); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> new PathHierarchyTokenizerFactory( + IndexSettingsModule.newIndexSettings(index, indexSettings), + null, + "path-hierarchy-tokenizer", + settings + ).create() + ); assertEquals("delimiter must be a one char value", e.getMessage()); } { - String replacement = RandomPicks.randomFrom(random(), new String[] {"--", ""}); + String replacement = RandomPicks.randomFrom(random(), new String[] { "--", "" }); Settings settings = newAnalysisSettingsBuilder().put("replacement", replacement).build(); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> new PathHierarchyTokenizerFactory(IndexSettingsModule.newIndexSettings(index, indexSettings), null, - "path-hierarchy-tokenizer", settings).create()); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> new PathHierarchyTokenizerFactory( + IndexSettingsModule.newIndexSettings(index, indexSettings), + null, + "path-hierarchy-tokenizer", + settings + ).create() + ); assertEquals("replacement must be a one char value", e.getMessage()); } } diff --git a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/PatternAnalyzerTests.java b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/PatternAnalyzerTests.java index b053db5c258bf..bd9f476454c5c 100644 --- a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/PatternAnalyzerTests.java +++ b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/PatternAnalyzerTests.java @@ -22,90 +22,96 @@ */ public class PatternAnalyzerTests extends ESTokenStreamTestCase { - /** - * Test PatternAnalyzer when it is configured with a non-word pattern. - */ - public void testNonWordPattern() throws IOException { - // Split on non-letter pattern, do not lowercase, no stopwords - PatternAnalyzer a = new PatternAnalyzer(Pattern.compile("\\W+"), false, null); - assertAnalyzesTo(a, "The quick brown Fox,the abcd1234 (56.78) dc.", - new String[] { "The", "quick", "brown", "Fox", "the", "abcd1234", "56", "78", "dc" }); - - // split on non-letter pattern, lowercase, english stopwords - PatternAnalyzer b = new PatternAnalyzer(Pattern.compile("\\W+"), true, - EnglishAnalyzer.ENGLISH_STOP_WORDS_SET); - assertAnalyzesTo(b, "The quick brown Fox,the abcd1234 (56.78) dc.", - new String[] { "quick", "brown", "fox", "abcd1234", "56", "78", "dc" }); - } - - /** - * Test PatternAnalyzer when it is configured with a whitespace pattern. - * Behavior can be similar to WhitespaceAnalyzer (depending upon options) - */ - public void testWhitespacePattern() throws IOException { - // Split on whitespace patterns, do not lowercase, no stopwords - PatternAnalyzer a = new PatternAnalyzer(Pattern.compile("\\s+"), false, null); - assertAnalyzesTo(a, "The quick brown Fox,the abcd1234 (56.78) dc.", - new String[] { "The", "quick", "brown", "Fox,the", "abcd1234", "(56.78)", "dc." }); - - // Split on whitespace patterns, lowercase, english stopwords - PatternAnalyzer b = new PatternAnalyzer(Pattern.compile("\\s+"), true, - EnglishAnalyzer.ENGLISH_STOP_WORDS_SET); - assertAnalyzesTo(b, "The quick brown Fox,the abcd1234 (56.78) dc.", - new String[] { "quick", "brown", "fox,the", "abcd1234", "(56.78)", "dc." }); - } - - /** - * Test PatternAnalyzer when it is configured with a custom pattern. In this - * case, text is tokenized on the comma "," - */ - public void testCustomPattern() throws IOException { - // Split on comma, do not lowercase, no stopwords - PatternAnalyzer a = new PatternAnalyzer(Pattern.compile(","), false, null); - assertAnalyzesTo(a, "Here,Are,some,Comma,separated,words,", - new String[] { "Here", "Are", "some", "Comma", "separated", "words" }); - - // split on comma, lowercase, english stopwords - PatternAnalyzer b = new PatternAnalyzer(Pattern.compile(","), true, - EnglishAnalyzer.ENGLISH_STOP_WORDS_SET); - assertAnalyzesTo(b, "Here,Are,some,Comma,separated,words,", - new String[] { "here", "some", "comma", "separated", "words" }); - } - - /** - * Test PatternAnalyzer against a large document. - */ - public void testHugeDocument() throws IOException { - StringBuilder document = new StringBuilder(); - // 5000 a's - char largeWord[] = new char[5000]; - Arrays.fill(largeWord, 'a'); - document.append(largeWord); - - // a space - document.append(' '); - - // 2000 b's - char largeWord2[] = new char[2000]; - Arrays.fill(largeWord2, 'b'); - document.append(largeWord2); - - // Split on whitespace patterns, do not lowercase, no stopwords - PatternAnalyzer a = new PatternAnalyzer(Pattern.compile("\\s+"), false, null); - assertAnalyzesTo(a, document.toString(), - new String[] { new String(largeWord), new String(largeWord2) }); - } - - /** blast some random strings through the analyzer */ - public void testRandomStrings() throws Exception { - Analyzer a = new PatternAnalyzer(Pattern.compile(","), true, EnglishAnalyzer.ENGLISH_STOP_WORDS_SET); - checkRandomData(random(), a, 10000*RANDOM_MULTIPLIER); - } - - public void testNormalize() { - PatternAnalyzer a = new PatternAnalyzer(Pattern.compile("\\s+"), false, null); - assertEquals(new BytesRef("FooBar"), a.normalize("dummy", "FooBar")); - a = new PatternAnalyzer(Pattern.compile("\\s+"), true, null); - assertEquals(new BytesRef("foobar"), a.normalize("dummy", "FooBar")); - } + /** + * Test PatternAnalyzer when it is configured with a non-word pattern. + */ + public void testNonWordPattern() throws IOException { + // Split on non-letter pattern, do not lowercase, no stopwords + PatternAnalyzer a = new PatternAnalyzer(Pattern.compile("\\W+"), false, null); + assertAnalyzesTo( + a, + "The quick brown Fox,the abcd1234 (56.78) dc.", + new String[] { "The", "quick", "brown", "Fox", "the", "abcd1234", "56", "78", "dc" } + ); + + // split on non-letter pattern, lowercase, english stopwords + PatternAnalyzer b = new PatternAnalyzer(Pattern.compile("\\W+"), true, EnglishAnalyzer.ENGLISH_STOP_WORDS_SET); + assertAnalyzesTo( + b, + "The quick brown Fox,the abcd1234 (56.78) dc.", + new String[] { "quick", "brown", "fox", "abcd1234", "56", "78", "dc" } + ); + } + + /** + * Test PatternAnalyzer when it is configured with a whitespace pattern. + * Behavior can be similar to WhitespaceAnalyzer (depending upon options) + */ + public void testWhitespacePattern() throws IOException { + // Split on whitespace patterns, do not lowercase, no stopwords + PatternAnalyzer a = new PatternAnalyzer(Pattern.compile("\\s+"), false, null); + assertAnalyzesTo( + a, + "The quick brown Fox,the abcd1234 (56.78) dc.", + new String[] { "The", "quick", "brown", "Fox,the", "abcd1234", "(56.78)", "dc." } + ); + + // Split on whitespace patterns, lowercase, english stopwords + PatternAnalyzer b = new PatternAnalyzer(Pattern.compile("\\s+"), true, EnglishAnalyzer.ENGLISH_STOP_WORDS_SET); + assertAnalyzesTo( + b, + "The quick brown Fox,the abcd1234 (56.78) dc.", + new String[] { "quick", "brown", "fox,the", "abcd1234", "(56.78)", "dc." } + ); + } + + /** + * Test PatternAnalyzer when it is configured with a custom pattern. In this + * case, text is tokenized on the comma "," + */ + public void testCustomPattern() throws IOException { + // Split on comma, do not lowercase, no stopwords + PatternAnalyzer a = new PatternAnalyzer(Pattern.compile(","), false, null); + assertAnalyzesTo(a, "Here,Are,some,Comma,separated,words,", new String[] { "Here", "Are", "some", "Comma", "separated", "words" }); + + // split on comma, lowercase, english stopwords + PatternAnalyzer b = new PatternAnalyzer(Pattern.compile(","), true, EnglishAnalyzer.ENGLISH_STOP_WORDS_SET); + assertAnalyzesTo(b, "Here,Are,some,Comma,separated,words,", new String[] { "here", "some", "comma", "separated", "words" }); + } + + /** + * Test PatternAnalyzer against a large document. + */ + public void testHugeDocument() throws IOException { + StringBuilder document = new StringBuilder(); + // 5000 a's + char largeWord[] = new char[5000]; + Arrays.fill(largeWord, 'a'); + document.append(largeWord); + + // a space + document.append(' '); + + // 2000 b's + char largeWord2[] = new char[2000]; + Arrays.fill(largeWord2, 'b'); + document.append(largeWord2); + + // Split on whitespace patterns, do not lowercase, no stopwords + PatternAnalyzer a = new PatternAnalyzer(Pattern.compile("\\s+"), false, null); + assertAnalyzesTo(a, document.toString(), new String[] { new String(largeWord), new String(largeWord2) }); + } + + /** blast some random strings through the analyzer */ + public void testRandomStrings() throws Exception { + Analyzer a = new PatternAnalyzer(Pattern.compile(","), true, EnglishAnalyzer.ENGLISH_STOP_WORDS_SET); + checkRandomData(random(), a, 10000 * RANDOM_MULTIPLIER); + } + + public void testNormalize() { + PatternAnalyzer a = new PatternAnalyzer(Pattern.compile("\\s+"), false, null); + assertEquals(new BytesRef("FooBar"), a.normalize("dummy", "FooBar")); + a = new PatternAnalyzer(Pattern.compile("\\s+"), true, null); + assertEquals(new BytesRef("foobar"), a.normalize("dummy", "FooBar")); + } } diff --git a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/PatternCaptureTokenFilterTests.java b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/PatternCaptureTokenFilterTests.java index 11fbd33cdb4df..a8e4aeba4080f 100644 --- a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/PatternCaptureTokenFilterTests.java +++ b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/PatternCaptureTokenFilterTests.java @@ -25,31 +25,35 @@ public class PatternCaptureTokenFilterTests extends ESTokenStreamTestCase { public void testPatternCaptureTokenFilter() throws Exception { String json = "/org/elasticsearch/analysis/common/pattern_capture.json"; Settings settings = Settings.builder() - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()) - .loadFromStream(json, getClass().getResourceAsStream(json), false) - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .build(); + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()) + .loadFromStream(json, getClass().getResourceAsStream(json), false) + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .build(); IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("index", settings); IndexAnalyzers indexAnalyzers = createTestAnalysis(idxSettings, settings, new CommonAnalysisPlugin()).indexAnalyzers; NamedAnalyzer analyzer1 = indexAnalyzers.get("single"); - assertTokenStreamContents(analyzer1.tokenStream("test", "foobarbaz"), new String[]{"foobarbaz","foobar","foo"}); + assertTokenStreamContents(analyzer1.tokenStream("test", "foobarbaz"), new String[] { "foobarbaz", "foobar", "foo" }); NamedAnalyzer analyzer2 = indexAnalyzers.get("multi"); - assertTokenStreamContents(analyzer2.tokenStream("test", "abc123def"), new String[]{"abc123def","abc","123","def"}); + assertTokenStreamContents(analyzer2.tokenStream("test", "abc123def"), new String[] { "abc123def", "abc", "123", "def" }); NamedAnalyzer analyzer3 = indexAnalyzers.get("preserve"); - assertTokenStreamContents(analyzer3.tokenStream("test", "foobarbaz"), new String[]{"foobar","foo"}); + assertTokenStreamContents(analyzer3.tokenStream("test", "foobarbaz"), new String[] { "foobar", "foo" }); } public void testNoPatterns() { try { - new PatternCaptureGroupTokenFilterFactory(IndexSettingsModule.newIndexSettings("test", Settings.EMPTY), null, - "pattern_capture", Settings.builder().put("pattern", "foobar").build()); - fail ("Expected IllegalArgumentException"); + new PatternCaptureGroupTokenFilterFactory( + IndexSettingsModule.newIndexSettings("test", Settings.EMPTY), + null, + "pattern_capture", + Settings.builder().put("pattern", "foobar").build() + ); + fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException e) { assertThat(e.getMessage(), containsString("required setting 'patterns' is missing")); } diff --git a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/PredicateTokenScriptFilterTests.java b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/PredicateTokenScriptFilterTests.java index 411d62d971cfa..b2a68c7cc25c6 100644 --- a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/PredicateTokenScriptFilterTests.java +++ b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/PredicateTokenScriptFilterTests.java @@ -29,9 +29,7 @@ public class PredicateTokenScriptFilterTests extends ESTokenStreamTestCase { public void testSimpleFilter() throws IOException { - Settings settings = Settings.builder() - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .build(); + Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build(); Settings indexSettings = Settings.builder() .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) .put("index.analysis.filter.f.type", "predicate_token_filter") @@ -50,7 +48,7 @@ public boolean execute(Token token) { }; @SuppressWarnings("unchecked") - ScriptService scriptService = new ScriptService(indexSettings, Collections.emptyMap(), Collections.emptyMap()){ + ScriptService scriptService = new ScriptService(indexSettings, Collections.emptyMap(), Collections.emptyMap()) { @Override public FactoryType compile(Script script, ScriptContext context) { assertEquals(context, AnalysisPredicateScript.CONTEXT); @@ -61,16 +59,13 @@ public FactoryType compile(Script script, ScriptContext FactoryType compile(Script script, ScriptContext context) { assertEquals(context, AnalysisPredicateScript.CONTEXT); @@ -61,16 +59,13 @@ public FactoryType compile(Script script, ScriptContexta", // no keys "a,=>b" // empty key )) { - expectThrows(RuntimeException.class, String.format( - Locale.ROOT, "Should fail for invalid rule: '%s'", rule - ), () -> create(rule)); + expectThrows( + RuntimeException.class, + String.format(Locale.ROOT, "Should fail for invalid rule: '%s'", rule), + () -> create(rule) + ); } } public void testRulesOk() throws IOException { - TokenFilterFactory tokenFilterFactory = create( - "a => 1", - "b,c => 2" - ); + TokenFilterFactory tokenFilterFactory = create("a => 1", "b,c => 2"); Tokenizer tokenizer = new WhitespaceTokenizer(); tokenizer.setReader(new StringReader("a b c")); - assertTokenStreamContents(tokenFilterFactory.create(tokenizer), new String[]{"1", "2", "2"}); + assertTokenStreamContents(tokenFilterFactory.create(tokenizer), new String[] { "1", "2", "2" }); } } diff --git a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/StemmerTokenFilterFactoryTests.java b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/StemmerTokenFilterFactoryTests.java index 0a8d9f3888bcb..4d0d4b65abdc1 100644 --- a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/StemmerTokenFilterFactoryTests.java +++ b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/StemmerTokenFilterFactoryTests.java @@ -39,13 +39,13 @@ public void testEnglishFilterFactory() throws IOException { for (int i = 0; i < iters; i++) { Version v = VersionUtils.randomVersion(random()); Settings settings = Settings.builder() - .put("index.analysis.filter.my_english.type", "stemmer") - .put("index.analysis.filter.my_english.language", "english") - .put("index.analysis.analyzer.my_english.tokenizer","whitespace") - .put("index.analysis.analyzer.my_english.filter","my_english") - .put(SETTING_VERSION_CREATED,v) - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .build(); + .put("index.analysis.filter.my_english.type", "stemmer") + .put("index.analysis.filter.my_english.language", "english") + .put("index.analysis.analyzer.my_english.tokenizer", "whitespace") + .put("index.analysis.analyzer.my_english.filter", "my_english") + .put(SETTING_VERSION_CREATED, v) + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) + .build(); ESTestCase.TestAnalysis analysis = AnalysisTestsHelper.createTestAnalysisFromSettings(settings, PLUGIN); TokenFilterFactory tokenFilter = analysis.tokenFilter.get("my_english"); @@ -56,7 +56,7 @@ public void testEnglishFilterFactory() throws IOException { IndexAnalyzers indexAnalyzers = analysis.indexAnalyzers; NamedAnalyzer analyzer = indexAnalyzers.get("my_english"); assertThat(create, instanceOf(PorterStemFilter.class)); - assertAnalyzesTo(analyzer, "consolingly", new String[]{"consolingli"}); + assertAnalyzesTo(analyzer, "consolingly", new String[] { "consolingli" }); } } @@ -66,13 +66,13 @@ public void testPorter2FilterFactory() throws IOException { Version v = VersionUtils.randomVersion(random()); Settings settings = Settings.builder() - .put("index.analysis.filter.my_porter2.type", "stemmer") - .put("index.analysis.filter.my_porter2.language", "porter2") - .put("index.analysis.analyzer.my_porter2.tokenizer","whitespace") - .put("index.analysis.analyzer.my_porter2.filter","my_porter2") - .put(SETTING_VERSION_CREATED,v) - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .build(); + .put("index.analysis.filter.my_porter2.type", "stemmer") + .put("index.analysis.filter.my_porter2.language", "porter2") + .put("index.analysis.analyzer.my_porter2.tokenizer", "whitespace") + .put("index.analysis.analyzer.my_porter2.filter", "my_porter2") + .put(SETTING_VERSION_CREATED, v) + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) + .build(); ESTestCase.TestAnalysis analysis = AnalysisTestsHelper.createTestAnalysisFromSettings(settings, PLUGIN); TokenFilterFactory tokenFilter = analysis.tokenFilter.get("my_porter2"); @@ -83,18 +83,23 @@ public void testPorter2FilterFactory() throws IOException { IndexAnalyzers indexAnalyzers = analysis.indexAnalyzers; NamedAnalyzer analyzer = indexAnalyzers.get("my_porter2"); assertThat(create, instanceOf(SnowballFilter.class)); - assertAnalyzesTo(analyzer, "possibly", new String[]{"possibl"}); + assertAnalyzesTo(analyzer, "possibly", new String[] { "possibl" }); } } public void testMultipleLanguagesThrowsException() throws IOException { Version v = VersionUtils.randomVersion(random()); - Settings settings = Settings.builder().put("index.analysis.filter.my_english.type", "stemmer") - .putList("index.analysis.filter.my_english.language", "english", "light_english").put(SETTING_VERSION_CREATED, v) - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build(); + Settings settings = Settings.builder() + .put("index.analysis.filter.my_english.type", "stemmer") + .putList("index.analysis.filter.my_english.language", "english", "light_english") + .put(SETTING_VERSION_CREATED, v) + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) + .build(); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> AnalysisTestsHelper.createTestAnalysisFromSettings(settings, PLUGIN)); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> AnalysisTestsHelper.createTestAnalysisFromSettings(settings, PLUGIN) + ); assertEquals("Invalid stemmer class specified: [english, light_english]", e.getMessage()); } } diff --git a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/SynonymsAnalysisTests.java b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/SynonymsAnalysisTests.java index 3372a7a9e130a..fe7156ef7213b 100644 --- a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/SynonymsAnalysisTests.java +++ b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/SynonymsAnalysisTests.java @@ -53,10 +53,11 @@ public void testSynonymsAnalysis() throws IOException { Files.copy(synonymsWordnet, config.resolve("synonyms_wordnet.txt")); String json = "/org/elasticsearch/analysis/common/synonyms.json"; - Settings settings = Settings.builder(). - loadFromStream(json, getClass().getResourceAsStream(json), false) - .put(Environment.PATH_HOME_SETTING.getKey(), home) - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).build(); + Settings settings = Settings.builder() + .loadFromStream(json, getClass().getResourceAsStream(json), false) + .put(Environment.PATH_HOME_SETTING.getKey(), home) + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .build(); IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("index", settings); indexAnalyzers = createTestAnalysis(idxSettings, settings, new CommonAnalysisPlugin()).indexAnalyzers; @@ -83,7 +84,7 @@ public void testSynonymWordDeleteByAnalyzer() throws IOException { .put("index.analysis.filter.stop_within_synonym.type", "stop") .putList("index.analysis.filter.stop_within_synonym.stopwords", "kimchy", "elasticsearch") .put("index.analysis.analyzer.synonymAnalyzerWithStopSynonymBeforeSynonym.tokenizer", "whitespace") - .putList("index.analysis.analyzer.synonymAnalyzerWithStopSynonymBeforeSynonym.filter", "stop_within_synonym","synonym") + .putList("index.analysis.analyzer.synonymAnalyzerWithStopSynonymBeforeSynonym.filter", "stop_within_synonym", "synonym") .build(); IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("index", settings); try { @@ -104,7 +105,7 @@ public void testExpandSynonymWordDeleteByAnalyzer() throws IOException { .put("index.analysis.filter.stop_within_synonym.type", "stop") .putList("index.analysis.filter.stop_within_synonym.stopwords", "kimchy", "elasticsearch") .put("index.analysis.analyzer.synonymAnalyzerExpandWithStopBeforeSynonym.tokenizer", "whitespace") - .putList("index.analysis.analyzer.synonymAnalyzerExpandWithStopBeforeSynonym.filter", "stop_within_synonym","synonym_expand") + .putList("index.analysis.analyzer.synonymAnalyzerExpandWithStopBeforeSynonym.filter", "stop_within_synonym", "synonym_expand") .build(); IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("index", settings); try { @@ -132,9 +133,12 @@ public void testSynonymsWrappedByMultiplexer() throws IOException { IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("index", settings); indexAnalyzers = createTestAnalysis(idxSettings, settings, new CommonAnalysisPlugin()).indexAnalyzers; - BaseTokenStreamTestCase.assertAnalyzesTo(indexAnalyzers.get("synonymAnalyzer"), "Some developers are odd", - new String[]{ "some", "developers", "develop", "programm", "are", "odd" }, - new int[]{ 1, 1, 0, 0, 1, 1 }); + BaseTokenStreamTestCase.assertAnalyzesTo( + indexAnalyzers.get("synonymAnalyzer"), + "Some developers are odd", + new String[] { "some", "developers", "develop", "programm", "are", "odd" }, + new int[] { 1, 1, 0, 0, 1, 1 } + ); } public void testAsciiFoldingFilterForSynonyms() throws IOException { @@ -149,9 +153,12 @@ public void testAsciiFoldingFilterForSynonyms() throws IOException { IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("index", settings); indexAnalyzers = createTestAnalysis(idxSettings, settings, new CommonAnalysisPlugin()).indexAnalyzers; - BaseTokenStreamTestCase.assertAnalyzesTo(indexAnalyzers.get("synonymAnalyzer"), "høj", - new String[]{ "hoj", "height" }, - new int[]{ 1, 0 }); + BaseTokenStreamTestCase.assertAnalyzesTo( + indexAnalyzers.get("synonymAnalyzer"), + "høj", + new String[] { "hoj", "height" }, + new int[] { 1, 0 } + ); } public void testPreconfigured() throws IOException { @@ -166,9 +173,12 @@ public void testPreconfigured() throws IOException { IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("index", settings); indexAnalyzers = createTestAnalysis(idxSettings, settings, new CommonAnalysisPlugin()).indexAnalyzers; - BaseTokenStreamTestCase.assertAnalyzesTo(indexAnalyzers.get("my_analyzer"), "würst", - new String[]{ "wurst", "sausage"}, - new int[]{ 1, 0 }); + BaseTokenStreamTestCase.assertAnalyzesTo( + indexAnalyzers.get("my_analyzer"), + "würst", + new String[] { "wurst", "sausage" }, + new int[] { 1, 0 } + ); } public void testChainedSynonymFilters() throws IOException { @@ -185,15 +195,18 @@ public void testChainedSynonymFilters() throws IOException { IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("index", settings); indexAnalyzers = createTestAnalysis(idxSettings, settings, new CommonAnalysisPlugin()).indexAnalyzers; - BaseTokenStreamTestCase.assertAnalyzesTo(indexAnalyzers.get("syn"), "term1", - new String[]{ "term1", "term3", "term2" }, new int[]{ 1, 0, 0 }); + BaseTokenStreamTestCase.assertAnalyzesTo( + indexAnalyzers.get("syn"), + "term1", + new String[] { "term1", "term3", "term2" }, + new int[] { 1, 0, 0 } + ); } public void testShingleFilters() { Settings settings = Settings.builder() - .put(IndexMetadata.SETTING_VERSION_CREATED, - VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, Version.CURRENT)) + .put(IndexMetadata.SETTING_VERSION_CREATED, VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, Version.CURRENT)) .put("path.home", createTempDir().toString()) .put("index.analysis.filter.synonyms.type", "synonym") .putList("index.analysis.filter.synonyms.synonyms", "programmer, developer") @@ -203,9 +216,10 @@ public void testShingleFilters() { .build(); IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("index", settings); - expectThrows(IllegalArgumentException.class, () -> { - indexAnalyzers = createTestAnalysis(idxSettings, settings, new CommonAnalysisPlugin()).indexAnalyzers; - }); + expectThrows( + IllegalArgumentException.class, + () -> { indexAnalyzers = createTestAnalysis(idxSettings, settings, new CommonAnalysisPlugin()).indexAnalyzers; } + ); } @@ -219,9 +233,7 @@ public void testTokenFiltersBypassSynonymAnalysis() throws IOException { .build(); IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("index", settings); - String[] bypassingFactories = new String[]{ - "dictionary_decompounder" - }; + String[] bypassingFactories = new String[] { "dictionary_decompounder" }; CommonAnalysisPlugin plugin = new CommonAnalysisPlugin(); for (String factory : bypassingFactories) { @@ -238,14 +250,12 @@ public void testTokenFiltersBypassSynonymAnalysis() throws IOException { } public void testPreconfiguredTokenFilters() throws IOException { - Set disallowedFilters = new HashSet<>(Arrays.asList( - "common_grams", "edge_ngram", "keyword_repeat", "ngram", "shingle", - "word_delimiter", "word_delimiter_graph" - )); + Set disallowedFilters = new HashSet<>( + Arrays.asList("common_grams", "edge_ngram", "keyword_repeat", "ngram", "shingle", "word_delimiter", "word_delimiter_graph") + ); Settings settings = Settings.builder() - .put(IndexMetadata.SETTING_VERSION_CREATED, - VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, Version.CURRENT)) + .put(IndexMetadata.SETTING_VERSION_CREATED, VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, Version.CURRENT)) .put("path.home", createTempDir().toString()) .build(); IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("index", settings); @@ -254,10 +264,11 @@ public void testPreconfiguredTokenFilters() throws IOException { try (CommonAnalysisPlugin plugin = new CommonAnalysisPlugin()) { for (PreConfiguredTokenFilter tf : plugin.getPreConfiguredTokenFilters()) { if (disallowedFilters.contains(tf.getName())) { - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - "Expected exception for factory " + tf.getName(), () -> { - tf.get(idxSettings, null, tf.getName(), settings).getSynonymFilter(); - }); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + "Expected exception for factory " + tf.getName(), + () -> { tf.get(idxSettings, null, tf.getName(), settings).getSynonymFilter(); } + ); assertEquals(tf.getName(), "Token filter [" + tf.getName() + "] cannot be used to parse synonyms", e.getMessage()); disallowedFiltersTested.add(tf.getName()); } else { @@ -271,8 +282,7 @@ public void testPreconfiguredTokenFilters() throws IOException { public void testDisallowedTokenFilters() throws IOException { Settings settings = Settings.builder() - .put(IndexMetadata.SETTING_VERSION_CREATED, - VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, Version.CURRENT)) + .put(IndexMetadata.SETTING_VERSION_CREATED, VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, Version.CURRENT)) .put("path.home", createTempDir().toString()) .putList("common_words", "a", "b") .put("output_unigrams", "true") @@ -280,23 +290,28 @@ public void testDisallowedTokenFilters() throws IOException { IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("index", settings); CommonAnalysisPlugin plugin = new CommonAnalysisPlugin(); - String[] disallowedFactories = new String[]{ - "multiplexer", "cjk_bigram", "common_grams", "ngram", "edge_ngram", - "word_delimiter", "word_delimiter_graph", "fingerprint" - }; + String[] disallowedFactories = new String[] { + "multiplexer", + "cjk_bigram", + "common_grams", + "ngram", + "edge_ngram", + "word_delimiter", + "word_delimiter_graph", + "fingerprint" }; for (String factory : disallowedFactories) { TokenFilterFactory tff = plugin.getTokenFilters().get(factory).get(idxSettings, null, factory, settings); TokenizerFactory tok = new KeywordTokenizerFactory(idxSettings, null, "keyword", settings); SynonymTokenFilterFactory stff = new SynonymTokenFilterFactory(idxSettings, null, "synonym", settings); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, "Expected IllegalArgumentException for factory " + factory, - () -> stff.buildSynonymAnalyzer(tok, Collections.emptyList(), Collections.singletonList(tff), null)); + () -> stff.buildSynonymAnalyzer(tok, Collections.emptyList(), Collections.singletonList(tff), null) + ); - assertEquals(factory, "Token filter [" + factory - + "] cannot be used to parse synonyms", - e.getMessage()); + assertEquals(factory, "Token filter [" + factory + "] cannot be used to parse synonyms", e.getMessage()); } } diff --git a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/TrimTokenFilterTests.java b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/TrimTokenFilterTests.java index 7ccd1df81c952..3c4387a4f30c7 100644 --- a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/TrimTokenFilterTests.java +++ b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/TrimTokenFilterTests.java @@ -30,7 +30,7 @@ public void testNormalizer() throws IOException { NamedAnalyzer normalizer = analysis.indexAnalyzers.getNormalizer("my_normalizer"); assertNotNull(normalizer); assertEquals("my_normalizer", normalizer.name()); - assertTokenStreamContents(normalizer.tokenStream("foo", " bar "), new String[] {"bar"}); + assertTokenStreamContents(normalizer.tokenStream("foo", " bar "), new String[] { "bar" }); assertEquals(new BytesRef("bar"), normalizer.normalize("foo", " bar ")); } diff --git a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/WhitespaceTokenizerFactoryTests.java b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/WhitespaceTokenizerFactoryTests.java index 1922ccd3f5b61..be9cc6b84454e 100644 --- a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/WhitespaceTokenizerFactoryTests.java +++ b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/WhitespaceTokenizerFactoryTests.java @@ -30,8 +30,12 @@ public class WhitespaceTokenizerFactoryTests extends ESTestCase { public void testSimpleWhiteSpaceTokenizer() throws IOException { final Settings indexSettings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).build(); IndexSettings indexProperties = IndexSettingsModule.newIndexSettings(new Index("test", "_na_"), indexSettings); - WhitespaceTokenizer tokenizer = (WhitespaceTokenizer) new WhitespaceTokenizerFactory(indexProperties, null, "whitespace_maxlen", - Settings.EMPTY).create(); + WhitespaceTokenizer tokenizer = (WhitespaceTokenizer) new WhitespaceTokenizerFactory( + indexProperties, + null, + "whitespace_maxlen", + Settings.EMPTY + ).create(); try (Reader reader = new StringReader("one, two, three")) { tokenizer.setReader(reader); @@ -43,8 +47,12 @@ public void testMaxTokenLength() throws IOException { final Settings indexSettings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).build(); IndexSettings indexProperties = IndexSettingsModule.newIndexSettings(new Index("test", "_na_"), indexSettings); final Settings settings = Settings.builder().put(WhitespaceTokenizerFactory.MAX_TOKEN_LENGTH, 2).build(); - WhitespaceTokenizer tokenizer = (WhitespaceTokenizer) new WhitespaceTokenizerFactory(indexProperties, null, "whitespace_maxlen", - settings).create(); + WhitespaceTokenizer tokenizer = (WhitespaceTokenizer) new WhitespaceTokenizerFactory( + indexProperties, + null, + "whitespace_maxlen", + settings + ).create(); try (Reader reader = new StringReader("one, two, three")) { tokenizer.setReader(reader); assertTokenStreamContents(tokenizer, new String[] { "on", "e,", "tw", "o,", "th", "re", "e" }); @@ -52,7 +60,7 @@ public void testMaxTokenLength() throws IOException { final Settings defaultSettings = Settings.EMPTY; tokenizer = (WhitespaceTokenizer) new WhitespaceTokenizerFactory(indexProperties, null, "whitespace_maxlen", defaultSettings) - .create(); + .create(); String veryLongToken = RandomStrings.randomAsciiAlphanumOfLength(random(), 256); try (Reader reader = new StringReader(veryLongToken)) { tokenizer.setReader(reader); @@ -60,13 +68,17 @@ public void testMaxTokenLength() throws IOException { } final Settings tooLongSettings = Settings.builder().put(WhitespaceTokenizerFactory.MAX_TOKEN_LENGTH, 1024 * 1024 + 1).build(); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> new WhitespaceTokenizerFactory(indexProperties, null, "whitespace_maxlen", tooLongSettings).create()); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> new WhitespaceTokenizerFactory(indexProperties, null, "whitespace_maxlen", tooLongSettings).create() + ); assertEquals("maxTokenLen must be greater than 0 and less than 1048576 passed: 1048577", e.getMessage()); final Settings negativeSettings = Settings.builder().put(WhitespaceTokenizerFactory.MAX_TOKEN_LENGTH, -1).build(); - e = expectThrows(IllegalArgumentException.class, - () -> new WhitespaceTokenizerFactory(indexProperties, null, "whitespace_maxlen", negativeSettings).create()); + e = expectThrows( + IllegalArgumentException.class, + () -> new WhitespaceTokenizerFactory(indexProperties, null, "whitespace_maxlen", negativeSettings).create() + ); assertEquals("maxTokenLen must be greater than 0 and less than 1048576 passed: -1", e.getMessage()); } } diff --git a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/WordDelimiterGraphTokenFilterFactoryTests.java b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/WordDelimiterGraphTokenFilterFactoryTests.java index 9e31439d1a8a8..c2252027f355e 100644 --- a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/WordDelimiterGraphTokenFilterFactoryTests.java +++ b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/WordDelimiterGraphTokenFilterFactoryTests.java @@ -28,35 +28,56 @@ import java.io.StringReader; import java.util.Collections; -public class WordDelimiterGraphTokenFilterFactoryTests - extends BaseWordDelimiterTokenFilterFactoryTestCase { +public class WordDelimiterGraphTokenFilterFactoryTests extends BaseWordDelimiterTokenFilterFactoryTestCase { public WordDelimiterGraphTokenFilterFactoryTests() { super("word_delimiter_graph"); } public void testMultiTerms() throws IOException { ESTestCase.TestAnalysis analysis = AnalysisTestsHelper.createTestAnalysisFromSettings( - Settings.builder() - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .put("index.analysis.filter.my_word_delimiter.type", type) - .put("index.analysis.filter.my_word_delimiter.catenate_all", "true") - .put("index.analysis.filter.my_word_delimiter.preserve_original", "true") - .build(), - new CommonAnalysisPlugin()); + Settings.builder() + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) + .put("index.analysis.filter.my_word_delimiter.type", type) + .put("index.analysis.filter.my_word_delimiter.catenate_all", "true") + .put("index.analysis.filter.my_word_delimiter.preserve_original", "true") + .build(), + new CommonAnalysisPlugin() + ); TokenFilterFactory tokenFilter = analysis.tokenFilter.get("my_word_delimiter"); String source = "PowerShot 500-42 wi-fi wi-fi-4000 j2se O'Neil's"; - String[] expected = new String[] { "PowerShot", "PowerShot", "Power", "Shot", "500-42", - "50042", "500", "42", "wi-fi", "wifi", "wi", "fi", "wi-fi-4000", "wifi4000", "wi", - "fi", "4000", "j2se", "j2se", "j", "2", "se", "O'Neil's", "ONeil", "O", "Neil" }; + String[] expected = new String[] { + "PowerShot", + "PowerShot", + "Power", + "Shot", + "500-42", + "50042", + "500", + "42", + "wi-fi", + "wifi", + "wi", + "fi", + "wi-fi-4000", + "wifi4000", + "wi", + "fi", + "4000", + "j2se", + "j2se", + "j", + "2", + "se", + "O'Neil's", + "ONeil", + "O", + "Neil" }; Tokenizer tokenizer = new WhitespaceTokenizer(); tokenizer.setReader(new StringReader(source)); - int[] expectedIncr = new int[] { 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, - 1, 1, 1, 0, 0, 1 }; - int[] expectedPosLen = new int[] { 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 3, 3, 1, 1, 1, 3, 3, - 1, 1, 1, 2, 2, 1, 1 }; - assertTokenStreamContents(tokenFilter.create(tokenizer), expected, null, null, null, - expectedIncr, expectedPosLen, null); + int[] expectedIncr = new int[] { 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1 }; + int[] expectedPosLen = new int[] { 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 3, 3, 1, 1, 1, 3, 3, 1, 1, 1, 2, 2, 1, 1 }; + assertTokenStreamContents(tokenFilter.create(tokenizer), expected, null, null, null, expectedIncr, expectedPosLen, null); } /** @@ -64,24 +85,33 @@ public void testMultiTerms() throws IOException { */ public void testPartsAndCatenate() throws IOException { ESTestCase.TestAnalysis analysis = AnalysisTestsHelper.createTestAnalysisFromSettings( - Settings.builder() - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .put("index.analysis.filter.my_word_delimiter.type", type) - .put("index.analysis.filter.my_word_delimiter.catenate_words", "true") - .put("index.analysis.filter.my_word_delimiter.generate_word_parts", "true") - .build(), - new CommonAnalysisPlugin()); + Settings.builder() + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) + .put("index.analysis.filter.my_word_delimiter.type", type) + .put("index.analysis.filter.my_word_delimiter.catenate_words", "true") + .put("index.analysis.filter.my_word_delimiter.generate_word_parts", "true") + .build(), + new CommonAnalysisPlugin() + ); TokenFilterFactory tokenFilter = analysis.tokenFilter.get("my_word_delimiter"); String source = "PowerShot"; - int[] expectedIncr = new int[]{1, 0, 1}; - int[] expectedPosLen = new int[]{2, 1, 1}; - int[] expectedStartOffsets = new int[]{0, 0, 5}; - int[] expectedEndOffsets = new int[]{9, 5, 9}; - String[] expected = new String[]{"PowerShot", "Power", "Shot" }; + int[] expectedIncr = new int[] { 1, 0, 1 }; + int[] expectedPosLen = new int[] { 2, 1, 1 }; + int[] expectedStartOffsets = new int[] { 0, 0, 5 }; + int[] expectedEndOffsets = new int[] { 9, 5, 9 }; + String[] expected = new String[] { "PowerShot", "Power", "Shot" }; Tokenizer tokenizer = new WhitespaceTokenizer(); tokenizer.setReader(new StringReader(source)); - assertTokenStreamContents(tokenFilter.create(tokenizer), expected, expectedStartOffsets, expectedEndOffsets, null, - expectedIncr, expectedPosLen, null); + assertTokenStreamContents( + tokenFilter.create(tokenizer), + expected, + expectedStartOffsets, + expectedEndOffsets, + null, + expectedIncr, + expectedPosLen, + null + ); } public void testAdjustingOffsets() throws IOException { @@ -93,81 +123,90 @@ public void testAdjustingOffsets() throws IOException { .put("index.analysis.filter.my_word_delimiter.generate_word_parts", "true") .put("index.analysis.filter.my_word_delimiter.adjust_offsets", "false") .build(), - new CommonAnalysisPlugin()); + new CommonAnalysisPlugin() + ); TokenFilterFactory tokenFilter = analysis.tokenFilter.get("my_word_delimiter"); String source = "PowerShot"; - int[] expectedIncr = new int[]{1, 0, 1}; - int[] expectedPosLen = new int[]{2, 1, 1}; - int[] expectedStartOffsets = new int[]{0, 0, 0}; - int[] expectedEndOffsets = new int[]{9, 9, 9}; - String[] expected = new String[]{"PowerShot", "Power", "Shot" }; + int[] expectedIncr = new int[] { 1, 0, 1 }; + int[] expectedPosLen = new int[] { 2, 1, 1 }; + int[] expectedStartOffsets = new int[] { 0, 0, 0 }; + int[] expectedEndOffsets = new int[] { 9, 9, 9 }; + String[] expected = new String[] { "PowerShot", "Power", "Shot" }; Tokenizer tokenizer = new WhitespaceTokenizer(); tokenizer.setReader(new StringReader(source)); - assertTokenStreamContents(tokenFilter.create(tokenizer), expected, expectedStartOffsets, expectedEndOffsets, null, - expectedIncr, expectedPosLen, null); + assertTokenStreamContents( + tokenFilter.create(tokenizer), + expected, + expectedStartOffsets, + expectedEndOffsets, + null, + expectedIncr, + expectedPosLen, + null + ); } public void testIgnoreKeywords() throws IOException { - //test with keywords but ignore is false (default behavior) + // test with keywords but ignore is false (default behavior) Settings settings = Settings.builder() - .put("index.analysis.filter.my_word_delimiter.type", type) - .put("index.analysis.filter.my_word_delimiter.generate_word_parts", "true") - .put("index.analysis.filter.my_keyword.type", "keyword_marker") - .put("index.analysis.filter.my_keyword.keywords", "PowerHungry") - .put("index.analysis.analyzer.my_analyzer.type", "custom") - .put("index.analysis.analyzer.my_analyzer.tokenizer", "whitespace") - .put("index.analysis.analyzer.my_analyzer.filter", "my_keyword, my_word_delimiter") - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .build(); + .put("index.analysis.filter.my_word_delimiter.type", type) + .put("index.analysis.filter.my_word_delimiter.generate_word_parts", "true") + .put("index.analysis.filter.my_keyword.type", "keyword_marker") + .put("index.analysis.filter.my_keyword.keywords", "PowerHungry") + .put("index.analysis.analyzer.my_analyzer.type", "custom") + .put("index.analysis.analyzer.my_analyzer.tokenizer", "whitespace") + .put("index.analysis.analyzer.my_analyzer.filter", "my_keyword, my_word_delimiter") + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) + .build(); ESTestCase.TestAnalysis analysis = AnalysisTestsHelper.createTestAnalysisFromSettings(settings, new CommonAnalysisPlugin()); String source = "PowerShot PowerHungry"; - int[] expectedStartOffsets = new int[]{0, 5, 10, 15}; - int[] expectedEndOffsets = new int[]{5, 9, 15, 21}; - String[] expected = new String[]{"Power", "Shot", "Power", "Hungry"}; + int[] expectedStartOffsets = new int[] { 0, 5, 10, 15 }; + int[] expectedEndOffsets = new int[] { 5, 9, 15, 21 }; + String[] expected = new String[] { "Power", "Shot", "Power", "Hungry" }; NamedAnalyzer analyzer = analysis.indexAnalyzers.get("my_analyzer"); assertAnalyzesTo(analyzer, source, expected, expectedStartOffsets, expectedEndOffsets); - //test with keywords but ignore_keywords is set as true - settings = Settings.builder().put(settings) - .put("index.analysis.filter.my_word_delimiter.ignore_keywords", "true") - .build(); + // test with keywords but ignore_keywords is set as true + settings = Settings.builder().put(settings).put("index.analysis.filter.my_word_delimiter.ignore_keywords", "true").build(); analysis = AnalysisTestsHelper.createTestAnalysisFromSettings(settings, new CommonAnalysisPlugin()); analyzer = analysis.indexAnalyzers.get("my_analyzer"); - expectedStartOffsets = new int[]{0, 5, 10}; - expectedEndOffsets = new int[]{5, 9, 21}; - expected = new String[]{"Power", "Shot", "PowerHungry"}; + expectedStartOffsets = new int[] { 0, 5, 10 }; + expectedEndOffsets = new int[] { 5, 9, 21 }; + expected = new String[] { "Power", "Shot", "PowerHungry" }; assertAnalyzesTo(analyzer, source, expected, expectedStartOffsets, expectedEndOffsets); } public void testPreconfiguredFilter() throws IOException { // Before 7.3 we don't adjust offsets { - Settings settings = Settings.builder() - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .build(); + Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build(); Settings indexSettings = Settings.builder() - .put(IndexMetadata.SETTING_VERSION_CREATED, - VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, VersionUtils.getPreviousVersion(Version.V_7_3_0))) + .put( + IndexMetadata.SETTING_VERSION_CREATED, + VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, VersionUtils.getPreviousVersion(Version.V_7_3_0)) + ) .put("index.analysis.analyzer.my_analyzer.tokenizer", "standard") .putList("index.analysis.analyzer.my_analyzer.filter", "word_delimiter_graph") .build(); IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("index", indexSettings); - try (IndexAnalyzers indexAnalyzers = new AnalysisModule(TestEnvironment.newEnvironment(settings), - Collections.singletonList(new CommonAnalysisPlugin())).getAnalysisRegistry().build(idxSettings)) { + try ( + IndexAnalyzers indexAnalyzers = new AnalysisModule( + TestEnvironment.newEnvironment(settings), + Collections.singletonList(new CommonAnalysisPlugin()) + ).getAnalysisRegistry().build(idxSettings) + ) { NamedAnalyzer analyzer = indexAnalyzers.get("my_analyzer"); assertNotNull(analyzer); - assertAnalyzesTo(analyzer, "h100", new String[]{"h", "100"}, new int[]{ 0, 0 }, new int[]{ 4, 4 }); + assertAnalyzesTo(analyzer, "h100", new String[] { "h", "100" }, new int[] { 0, 0 }, new int[] { 4, 4 }); } } // Afger 7.3 we do adjust offsets { - Settings settings = Settings.builder() - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .build(); + Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build(); Settings indexSettings = Settings.builder() .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) .put("index.analysis.analyzer.my_analyzer.tokenizer", "standard") @@ -175,12 +214,16 @@ public void testPreconfiguredFilter() throws IOException { .build(); IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("index", indexSettings); - try (IndexAnalyzers indexAnalyzers = new AnalysisModule(TestEnvironment.newEnvironment(settings), - Collections.singletonList(new CommonAnalysisPlugin())).getAnalysisRegistry().build(idxSettings)) { + try ( + IndexAnalyzers indexAnalyzers = new AnalysisModule( + TestEnvironment.newEnvironment(settings), + Collections.singletonList(new CommonAnalysisPlugin()) + ).getAnalysisRegistry().build(idxSettings) + ) { NamedAnalyzer analyzer = indexAnalyzers.get("my_analyzer"); assertNotNull(analyzer); - assertAnalyzesTo(analyzer, "h100", new String[]{"h", "100"}, new int[]{ 0, 1 }, new int[]{ 1, 4 }); + assertAnalyzesTo(analyzer, "h100", new String[] { "h", "100" }, new int[] { 0, 1 }, new int[] { 1, 4 }); } } diff --git a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/WordDelimiterTokenFilterFactoryTests.java b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/WordDelimiterTokenFilterFactoryTests.java index 2e9357ae73f25..7ee34c42f32ed 100644 --- a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/WordDelimiterTokenFilterFactoryTests.java +++ b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/WordDelimiterTokenFilterFactoryTests.java @@ -7,7 +7,6 @@ */ package org.elasticsearch.analysis.common; - import org.apache.lucene.analysis.Tokenizer; import org.apache.lucene.analysis.core.WhitespaceTokenizer; import org.elasticsearch.common.settings.Settings; @@ -19,8 +18,7 @@ import java.io.IOException; import java.io.StringReader; -public class WordDelimiterTokenFilterFactoryTests - extends BaseWordDelimiterTokenFilterFactoryTestCase { +public class WordDelimiterTokenFilterFactoryTests extends BaseWordDelimiterTokenFilterFactoryTestCase { public WordDelimiterTokenFilterFactoryTests() { super("word_delimiter"); } @@ -30,16 +28,17 @@ public WordDelimiterTokenFilterFactoryTests() { */ public void testPartsAndCatenate() throws IOException { ESTestCase.TestAnalysis analysis = AnalysisTestsHelper.createTestAnalysisFromSettings( - Settings.builder() - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .put("index.analysis.filter.my_word_delimiter.type", type) - .put("index.analysis.filter.my_word_delimiter.catenate_words", "true") - .put("index.analysis.filter.my_word_delimiter.generate_word_parts", "true") - .build(), - new CommonAnalysisPlugin()); + Settings.builder() + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) + .put("index.analysis.filter.my_word_delimiter.type", type) + .put("index.analysis.filter.my_word_delimiter.catenate_words", "true") + .put("index.analysis.filter.my_word_delimiter.generate_word_parts", "true") + .build(), + new CommonAnalysisPlugin() + ); TokenFilterFactory tokenFilter = analysis.tokenFilter.get("my_word_delimiter"); String source = "PowerShot"; - String[] expected = new String[]{"Power", "PowerShot", "Shot" }; + String[] expected = new String[] { "Power", "PowerShot", "Shot" }; Tokenizer tokenizer = new WhitespaceTokenizer(); tokenizer.setReader(new StringReader(source)); assertTokenStreamContents(tokenFilter.create(tokenizer), expected); diff --git a/modules/analysis-common/src/yamlRestTest/java/org/elasticsearch/analysis/common/CommonAnalysisClientYamlTestSuiteIT.java b/modules/analysis-common/src/yamlRestTest/java/org/elasticsearch/analysis/common/CommonAnalysisClientYamlTestSuiteIT.java index dfef189b3edc0..08c6d7d5cd040 100644 --- a/modules/analysis-common/src/yamlRestTest/java/org/elasticsearch/analysis/common/CommonAnalysisClientYamlTestSuiteIT.java +++ b/modules/analysis-common/src/yamlRestTest/java/org/elasticsearch/analysis/common/CommonAnalysisClientYamlTestSuiteIT.java @@ -14,7 +14,7 @@ import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase; public class CommonAnalysisClientYamlTestSuiteIT extends ESClientYamlSuiteTestCase { - public CommonAnalysisClientYamlTestSuiteIT(@Name("yaml")ClientYamlTestCandidate testCandidate) { + public CommonAnalysisClientYamlTestSuiteIT(@Name("yaml") ClientYamlTestCandidate testCandidate) { super(testCandidate); } diff --git a/modules/ingest-common/src/internalClusterTest/java/org/elasticsearch/ingest/common/IngestRestartIT.java b/modules/ingest-common/src/internalClusterTest/java/org/elasticsearch/ingest/common/IngestRestartIT.java index 4bc82afad69b2..9df7c1988ccb7 100644 --- a/modules/ingest-common/src/internalClusterTest/java/org/elasticsearch/ingest/common/IngestRestartIT.java +++ b/modules/ingest-common/src/internalClusterTest/java/org/elasticsearch/ingest/common/IngestRestartIT.java @@ -13,13 +13,13 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.ingest.IngestStats; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.script.MockScriptEngine; import org.elasticsearch.script.MockScriptPlugin; import org.elasticsearch.test.ESIntegTestCase; import org.elasticsearch.test.InternalTestCluster; +import org.elasticsearch.xcontent.XContentType; import java.util.Arrays; import java.util.Collection; @@ -53,9 +53,7 @@ protected Map, Object>> pluginScripts() { return Map.of("my_script", ctx -> { ctx.put("z", 0); return null; - }, "throwing_script", ctx -> { - throw new RuntimeException("this script always fails"); - }); + }, "throwing_script", ctx -> { throw new RuntimeException("this script always fails"); }); } } @@ -63,26 +61,38 @@ public void testFailureInConditionalProcessor() { internalCluster().ensureAtLeastNumDataNodes(1); internalCluster().startMasterOnlyNode(); final String pipelineId = "foo"; - client().admin().cluster().preparePutPipeline(pipelineId, - new BytesArray("{\n" + - " \"processors\" : [\n" + - " {\"set\" : {\"field\": \"any_field\", \"value\": \"any_value\"}},\n" + - " {\"set\" : {" + "" + - " \"if\" : " + "{\"lang\": \"" + MockScriptEngine.NAME + "\", \"source\": \"throwing_script\"}," + - " \"field\": \"any_field2\"," + - " \"value\": \"any_value2\"}" + - " }\n" + - " ]\n" + - "}"), XContentType.JSON).get(); + client().admin() + .cluster() + .preparePutPipeline( + pipelineId, + new BytesArray( + "{\n" + + " \"processors\" : [\n" + + " {\"set\" : {\"field\": \"any_field\", \"value\": \"any_value\"}},\n" + + " {\"set\" : {" + + "" + + " \"if\" : " + + "{\"lang\": \"" + + MockScriptEngine.NAME + + "\", \"source\": \"throwing_script\"}," + + " \"field\": \"any_field2\"," + + " \"value\": \"any_value2\"}" + + " }\n" + + " ]\n" + + "}" + ), + XContentType.JSON + ) + .get(); Exception e = expectThrows( Exception.class, - () -> - client().prepareIndex("index").setId("1") - .setSource("x", 0) - .setPipeline(pipelineId) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .get() + () -> client().prepareIndex("index") + .setId("1") + .setSource("x", 0) + .setPipeline(pipelineId) + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + .get() ); assertTrue(e.getMessage().contains("this script always fails")); @@ -101,19 +111,23 @@ public void testScriptDisabled() throws Exception { String pipelineIdWithScript = pipelineIdWithoutScript + "_script"; internalCluster().startNode(); - BytesReference pipelineWithScript = new BytesArray("{\n" + - " \"processors\" : [\n" + - " {\"script\" : {\"lang\": \"" + MockScriptEngine.NAME + "\", \"source\": \"my_script\"}}\n" + - " ]\n" + - "}"); - BytesReference pipelineWithoutScript = new BytesArray("{\n" + - " \"processors\" : [\n" + - " {\"set\" : {\"field\": \"y\", \"value\": 0}}\n" + - " ]\n" + - "}"); + BytesReference pipelineWithScript = new BytesArray( + "{\n" + + " \"processors\" : [\n" + + " {\"script\" : {\"lang\": \"" + + MockScriptEngine.NAME + + "\", \"source\": \"my_script\"}}\n" + + " ]\n" + + "}" + ); + BytesReference pipelineWithoutScript = new BytesArray( + "{\n" + " \"processors\" : [\n" + " {\"set\" : {\"field\": \"y\", \"value\": 0}}\n" + " ]\n" + "}" + ); - Consumer checkPipelineExists = (id) -> assertThat(client().admin().cluster().prepareGetPipeline(id) - .get().pipelines().get(0).getId(), equalTo(id)); + Consumer checkPipelineExists = (id) -> assertThat( + client().admin().cluster().prepareGetPipeline(id).get().pipelines().get(0).getId(), + equalTo(id) + ); client().admin().cluster().preparePutPipeline(pipelineIdWithScript, pipelineWithScript, XContentType.JSON).get(); client().admin().cluster().preparePutPipeline(pipelineIdWithoutScript, pipelineWithoutScript, XContentType.JSON).get(); @@ -121,7 +135,6 @@ public void testScriptDisabled() throws Exception { checkPipelineExists.accept(pipelineIdWithScript); checkPipelineExists.accept(pipelineIdWithoutScript); - internalCluster().restartNode(internalCluster().getMasterName(), new InternalTestCluster.RestartCallback() { @Override @@ -134,23 +147,35 @@ public Settings onNodeStopped(String nodeName) { checkPipelineExists.accept(pipelineIdWithoutScript); checkPipelineExists.accept(pipelineIdWithScript); - client().prepareIndex("index").setId("1") + client().prepareIndex("index") + .setId("1") .setSource("x", 0) .setPipeline(pipelineIdWithoutScript) .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) .get(); - IllegalStateException exception = expectThrows(IllegalStateException.class, - () -> client().prepareIndex("index").setId("2") + IllegalStateException exception = expectThrows( + IllegalStateException.class, + () -> client().prepareIndex("index") + .setId("2") .setSource("x", 0) .setPipeline(pipelineIdWithScript) .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .get()); - assertThat(exception.getMessage(), - equalTo("pipeline with id [" + pipelineIdWithScript + "] could not be loaded, caused by " + - "[org.elasticsearch.ElasticsearchParseException: Error updating pipeline with id [" + pipelineIdWithScript + "]; " + - "org.elasticsearch.ElasticsearchException: java.lang.IllegalArgumentException: cannot execute [inline] scripts; " + - "java.lang.IllegalArgumentException: cannot execute [inline] scripts]")); + .get() + ); + assertThat( + exception.getMessage(), + equalTo( + "pipeline with id [" + + pipelineIdWithScript + + "] could not be loaded, caused by " + + "[org.elasticsearch.ElasticsearchParseException: Error updating pipeline with id [" + + pipelineIdWithScript + + "]; " + + "org.elasticsearch.ElasticsearchException: java.lang.IllegalArgumentException: cannot execute [inline] scripts; " + + "java.lang.IllegalArgumentException: cannot execute [inline] scripts]" + ) + ); Map source = client().prepareGet("index", "1").get().getSource(); assertThat(source.get("x"), equalTo(0)); @@ -160,24 +185,31 @@ public Settings onNodeStopped(String nodeName) { public void testPipelineWithScriptProcessorThatHasStoredScript() throws Exception { internalCluster().startNode(); - client().admin().cluster().preparePutStoredScript() - .setId("1") - .setContent(new BytesArray("{\"script\": {\"lang\": \"" + MockScriptEngine.NAME + - "\", \"source\": \"my_script\"} }"), XContentType.JSON) - .get(); - BytesReference pipeline = new BytesArray("{\n" + - " \"processors\" : [\n" + - " {\"set\" : {\"field\": \"y\", \"value\": 0}},\n" + - " {\"script\" : {\"id\": \"1\"}}\n" + - " ]\n" + - "}"); + client().admin() + .cluster() + .preparePutStoredScript() + .setId("1") + .setContent( + new BytesArray("{\"script\": {\"lang\": \"" + MockScriptEngine.NAME + "\", \"source\": \"my_script\"} }"), + XContentType.JSON + ) + .get(); + BytesReference pipeline = new BytesArray( + "{\n" + + " \"processors\" : [\n" + + " {\"set\" : {\"field\": \"y\", \"value\": 0}},\n" + + " {\"script\" : {\"id\": \"1\"}}\n" + + " ]\n" + + "}" + ); client().admin().cluster().preparePutPipeline("_id", pipeline, XContentType.JSON).get(); - client().prepareIndex("index").setId("1") - .setSource("x", 0) - .setPipeline("_id") - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .get(); + client().prepareIndex("index") + .setId("1") + .setSource("x", 0) + .setPipeline("_id") + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + .get(); Map source = client().prepareGet("index", "1").get().getSource(); assertThat(source.get("x"), equalTo(0)); @@ -191,11 +223,12 @@ public void testPipelineWithScriptProcessorThatHasStoredScript() throws Exceptio internalCluster().fullRestart(); ensureYellow("index"); - client().prepareIndex("index").setId("2") - .setSource("x", 0) - .setPipeline("_id") - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .get(); + client().prepareIndex("index") + .setId("2") + .setSource("x", 0) + .setPipeline("_id") + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + .get(); source = client().prepareGet("index", "2").get().getSource(); assertThat(source.get("x"), equalTo(0)); @@ -207,18 +240,17 @@ public void testWithDedicatedIngestNode() throws Exception { String node = internalCluster().startNode(); String ingestNode = internalCluster().startNode(onlyRole(DiscoveryNodeRole.INGEST_ROLE)); - BytesReference pipeline = new BytesArray("{\n" + - " \"processors\" : [\n" + - " {\"set\" : {\"field\": \"y\", \"value\": 0}}\n" + - " ]\n" + - "}"); + BytesReference pipeline = new BytesArray( + "{\n" + " \"processors\" : [\n" + " {\"set\" : {\"field\": \"y\", \"value\": 0}}\n" + " ]\n" + "}" + ); client().admin().cluster().preparePutPipeline("_id", pipeline, XContentType.JSON).get(); - client().prepareIndex("index").setId("1") - .setSource("x", 0) - .setPipeline("_id") - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .get(); + client().prepareIndex("index") + .setId("1") + .setSource("x", 0) + .setPipeline("_id") + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + .get(); Map source = client().prepareGet("index", "1").get().getSource(); assertThat(source.get("x"), equalTo(0)); @@ -227,11 +259,12 @@ public void testWithDedicatedIngestNode() throws Exception { logger.info("Stopping"); internalCluster().restartNode(node, new InternalTestCluster.RestartCallback()); - client(ingestNode).prepareIndex("index").setId("2") - .setSource("x", 0) - .setPipeline("_id") - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .get(); + client(ingestNode).prepareIndex("index") + .setId("2") + .setSource("x", 0) + .setPipeline("_id") + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + .get(); source = client(ingestNode).prepareGet("index", "2").get().getSource(); assertThat(source.get("x"), equalTo(0)); diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/AbstractStringProcessor.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/AbstractStringProcessor.java index df397718e688b..78c90b811261b 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/AbstractStringProcessor.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/AbstractStringProcessor.java @@ -65,8 +65,17 @@ public final IngestDocument execute(IngestDocument document) { if (value instanceof String) { newList.add(process((String) value)); } else { - throw new IllegalArgumentException("value [" + value + "] of type [" + value.getClass().getName() + - "] in list field [" + field + "] cannot be cast to [" + String.class.getName() + "]"); + throw new IllegalArgumentException( + "value [" + + value + + "] of type [" + + value.getClass().getName() + + "] in list field [" + + field + + "] cannot be cast to [" + + String.class.getName() + + "]" + ); } } newValue = newList; @@ -74,8 +83,9 @@ public final IngestDocument execute(IngestDocument document) { if (val instanceof String) { newValue = process((String) val); } else { - throw new IllegalArgumentException("field [" + field + "] of type [" + val.getClass().getName() + "] cannot be cast to [" + - String.class.getName() + "]"); + throw new IllegalArgumentException( + "field [" + field + "] of type [" + val.getClass().getName() + "] cannot be cast to [" + String.class.getName() + "]" + ); } } @@ -94,8 +104,12 @@ protected Factory(String processorType) { } @Override - public AbstractStringProcessor create(Map registry, String tag, - String description, Map config) throws Exception { + public AbstractStringProcessor create( + Map registry, + String tag, + String description, + Map config + ) throws Exception { String field = ConfigurationUtils.readStringProperty(processorType, tag, config, "field"); boolean ignoreMissing = ConfigurationUtils.readBooleanProperty(processorType, tag, config, "ignore_missing", false); String targetField = ConfigurationUtils.readStringProperty(processorType, tag, config, "target_field", field); @@ -103,8 +117,13 @@ public AbstractStringProcessor create(Map registry return newProcessor(tag, description, config, field, ignoreMissing, targetField); } - protected abstract AbstractStringProcessor newProcessor(String processorTag, String description, - Map config, String field, - boolean ignoreMissing, String targetField); + protected abstract AbstractStringProcessor newProcessor( + String processorTag, + String description, + Map config, + String field, + boolean ignoreMissing, + String targetField + ); } } diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/AppendProcessor.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/AppendProcessor.java index 557cf214cc453..273b76955060b 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/AppendProcessor.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/AppendProcessor.java @@ -67,8 +67,12 @@ public Factory(ScriptService scriptService) { } @Override - public AppendProcessor create(Map registry, String processorTag, - String description, Map config) throws Exception { + public AppendProcessor create( + Map registry, + String processorTag, + String description, + Map config + ) throws Exception { String field = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "field"); Object value = ConfigurationUtils.readObject(TYPE, processorTag, config, "value"); boolean allowDuplicates = ConfigurationUtils.readBooleanProperty(TYPE, processorTag, config, "allow_duplicates", true); diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/BytesProcessor.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/BytesProcessor.java index c5801a09f113e..d63f1e60fa52d 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/BytesProcessor.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/BytesProcessor.java @@ -45,8 +45,14 @@ public Factory() { } @Override - protected BytesProcessor newProcessor(String tag, String description, Map config, String field, - boolean ignoreMissing, String targetField) { + protected BytesProcessor newProcessor( + String tag, + String description, + Map config, + String field, + boolean ignoreMissing, + String targetField + ) { return new BytesProcessor(tag, description, field, ignoreMissing, targetField); } } diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/CommunityIdProcessor.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/CommunityIdProcessor.java index 629135ebfe925..11f4306489131 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/CommunityIdProcessor.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/CommunityIdProcessor.java @@ -160,10 +160,19 @@ public static String apply( Object destinationPort, Object icmpType, Object icmpCode, - int seed) { + int seed + ) { - Flow flow = buildFlow(sourceIpAddrString, destIpAddrString, ianaNumber, () -> transport, () -> sourcePort, () -> destinationPort, - icmpType, icmpCode); + Flow flow = buildFlow( + sourceIpAddrString, + destIpAddrString, + ianaNumber, + () -> transport, + () -> sourcePort, + () -> destinationPort, + icmpType, + icmpCode + ); if (flow == null) { throw new IllegalArgumentException("unable to construct flow from document"); @@ -180,13 +189,21 @@ public static String apply( Object sourcePort, Object destinationPort, Object icmpType, - Object icmpCode) { + Object icmpCode + ) { return apply(sourceIpAddrString, destIpAddrString, ianaNumber, transport, sourcePort, destinationPort, icmpType, icmpCode, 0); } - private static Flow buildFlow(String sourceIpAddrString, String destIpAddrString, Object ianaNumber, - Supplier transport, Supplier sourcePort, Supplier destinationPort, - Object icmpType, Object icmpCode) { + private static Flow buildFlow( + String sourceIpAddrString, + String destIpAddrString, + Object ianaNumber, + Supplier transport, + Supplier sourcePort, + Supplier destinationPort, + Object icmpType, + Object icmpCode + ) { if (sourceIpAddrString == null) { return null; } diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/ConvertProcessor.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/ConvertProcessor.java index 35f1728822acf..ef8dd28bd9c32 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/ConvertProcessor.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/ConvertProcessor.java @@ -37,12 +37,13 @@ public Object convert(Object value) { return Integer.decode(strValue); } return Integer.parseInt(strValue); - } catch(NumberFormatException e) { + } catch (NumberFormatException e) { throw new IllegalArgumentException("unable to convert [" + value + "] to integer", e); } } - }, LONG { + }, + LONG { @Override public Object convert(Object value) { try { @@ -51,29 +52,32 @@ public Object convert(Object value) { return Long.decode(strValue); } return Long.parseLong(strValue); - } catch(NumberFormatException e) { + } catch (NumberFormatException e) { throw new IllegalArgumentException("unable to convert [" + value + "] to long", e); } } - }, DOUBLE { + }, + DOUBLE { @Override public Object convert(Object value) { try { return Double.parseDouble(value.toString()); - } catch(NumberFormatException e) { + } catch (NumberFormatException e) { throw new IllegalArgumentException("unable to convert [" + value + "] to double", e); } } - }, FLOAT { + }, + FLOAT { @Override public Object convert(Object value) { try { return Float.parseFloat(value.toString()); - } catch(NumberFormatException e) { + } catch (NumberFormatException e) { throw new IllegalArgumentException("unable to convert [" + value + "] to float", e); } } - }, BOOLEAN { + }, + BOOLEAN { @Override public Object convert(Object value) { if (value.toString().equalsIgnoreCase("true")) { @@ -84,23 +88,26 @@ public Object convert(Object value) { throw new IllegalArgumentException("[" + value + "] is not a boolean value, cannot convert to boolean"); } } - }, IP { + }, + IP { @Override public Object convert(Object value) { // IllegalArgumentException is thrown if unable to convert InetAddresses.forString((String) value); return value; } - }, STRING { + }, + STRING { @Override public Object convert(Object value) { return value.toString(); } - }, AUTO { + }, + AUTO { @Override public Object convert(Object value) { if ((value instanceof String) == false) { - return value; + return value; } try { return BOOLEAN.convert(value); @@ -131,9 +138,13 @@ public String toString() { public static Type fromString(String processorTag, String propertyName, String type) { try { return Type.valueOf(type.toUpperCase(Locale.ROOT)); - } catch(IllegalArgumentException e) { - throw newConfigurationException(TYPE, processorTag, propertyName, "type [" + type + - "] not supported, cannot convert field."); + } catch (IllegalArgumentException e) { + throw newConfigurationException( + TYPE, + processorTag, + propertyName, + "type [" + type + "] not supported, cannot convert field." + ); } } } @@ -201,8 +212,12 @@ public String getType() { public static final class Factory implements Processor.Factory { @Override - public ConvertProcessor create(Map registry, String processorTag, - String description, Map config) throws Exception { + public ConvertProcessor create( + Map registry, + String processorTag, + String description, + Map config + ) throws Exception { String field = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "field"); String typeProperty = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "type"); String targetField = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "target_field", field); diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/CsvParser.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/CsvParser.java index 24663644b36c9..6739ce5a41b4e 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/CsvParser.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/CsvParser.java @@ -18,7 +18,10 @@ final class CsvParser { private static final char TAB = '\t'; private enum State { - START, UNQUOTED, QUOTED, QUOTED_END + START, + UNQUOTED, + QUOTED, + QUOTED_END } private final char quote; @@ -70,7 +73,7 @@ void process(String line) { } } - //we've reached end of string, we need to handle last field + // we've reached end of string, we need to handle last field switch (state) { case UNQUOTED: setField(length); diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/CsvProcessor.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/CsvProcessor.java index 561b12309ade2..36ed43ee98e49 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/CsvProcessor.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/CsvProcessor.java @@ -35,7 +35,7 @@ public final class CsvProcessor extends AbstractProcessor { public static final String TYPE = "csv"; - //visible for testing + // visible for testing final String field; final String[] headers; final boolean trim; @@ -44,8 +44,17 @@ public final class CsvProcessor extends AbstractProcessor { final boolean ignoreMissing; final Object emptyValue; - CsvProcessor(String tag, String description, String field, String[] headers, boolean trim, char separator, char quote, - boolean ignoreMissing, Object emptyValue) { + CsvProcessor( + String tag, + String description, + String field, + String[] headers, + boolean trim, + char separator, + char quote, + boolean ignoreMissing, + Object emptyValue + ) { super(tag, description); this.field = field; this.headers = headers; @@ -79,8 +88,12 @@ public String getType() { public static final class Factory implements org.elasticsearch.ingest.Processor.Factory { @Override - public CsvProcessor create(Map registry, String processorTag, - String description, Map config) { + public CsvProcessor create( + Map registry, + String processorTag, + String description, + Map config + ) { String field = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "field"); String quote = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "quote", "\""); if (quote.length() != 1) { @@ -92,7 +105,7 @@ public CsvProcessor create(Map registry, String proce } boolean trim = ConfigurationUtils.readBooleanProperty(TYPE, processorTag, config, "trim", false); Object emptyValue = null; - if(config.containsKey("empty_value")){ + if (config.containsKey("empty_value")) { emptyValue = ConfigurationUtils.readObject(TYPE, processorTag, config, "empty_value"); } boolean ignoreMissing = ConfigurationUtils.readBooleanProperty(TYPE, processorTag, config, "ignore_missing", false); @@ -100,8 +113,17 @@ public CsvProcessor create(Map registry, String proce if (targetFields.isEmpty()) { throw newConfigurationException(TYPE, processorTag, "target_fields", "target fields list can't be empty"); } - return new CsvProcessor(processorTag, description, field, targetFields.toArray(String[]::new), trim, separator.charAt(0), - quote.charAt(0), ignoreMissing, emptyValue); + return new CsvProcessor( + processorTag, + description, + field, + targetFields.toArray(String[]::new), + trim, + separator.charAt(0), + quote.charAt(0), + ignoreMissing, + emptyValue + ); } } } diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateFormat.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateFormat.java index 295e056a20b7e..a9bc6c2e88c3d 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateFormat.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateFormat.java @@ -37,9 +37,8 @@ enum DateFormat { Function getFunction(String format, ZoneId timezone, Locale locale) { return (date) -> { TemporalAccessor accessor = DateFormatter.forPattern("iso8601").parse(date); - //even though locale could be set to en-us, Locale.ROOT (following iso8601 calendar data rules) should be used - return DateFormatters.from(accessor, Locale.ROOT, timezone) - .withZoneSameInstant(timezone); + // even though locale could be set to en-us, Locale.ROOT (following iso8601 calendar data rules) should be used + return DateFormatters.from(accessor, Locale.ROOT, timezone).withZoneSameInstant(timezone); }; } @@ -69,12 +68,18 @@ private long parseMillis(String date) { long base = Long.parseLong(date.substring(1, 16), 16); // 1356138046000 long rest = Long.parseLong(date.substring(16, 24), 16); - return ((base * 1000) - 10000) + (rest/1000000); + return ((base * 1000) - 10000) + (rest / 1000000); } }, Java { - private final List FIELDS = - Arrays.asList(NANO_OF_SECOND, SECOND_OF_DAY, MINUTE_OF_DAY, HOUR_OF_DAY, DAY_OF_MONTH, MONTH_OF_YEAR); + private final List FIELDS = Arrays.asList( + NANO_OF_SECOND, + SECOND_OF_DAY, + MINUTE_OF_DAY, + HOUR_OF_DAY, + DAY_OF_MONTH, + MONTH_OF_YEAR + ); @Override Function getFunction(String format, ZoneId zoneId, Locale locale) { @@ -83,8 +88,7 @@ Function getFunction(String format, ZoneId zoneId, Locale format = format.substring(1); } - DateFormatter dateFormatter = DateFormatter.forPattern(format) - .withLocale(locale); + DateFormatter dateFormatter = DateFormatter.forPattern(format).withLocale(locale); final DateFormatter formatter = dateFormatter; return text -> { @@ -105,8 +109,7 @@ Function getFunction(String format, ZoneId zoneId, Locale accessor = newTime.withZoneSameLocal(zoneId); } - return DateFormatters.from(accessor, locale, zoneId) - .withZoneSameInstant(zoneId); + return DateFormatters.from(accessor, locale, zoneId).withZoneSameInstant(zoneId); }; } diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateIndexNameProcessor.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateIndexNameProcessor.java index 55ab5209c7b31..4aa76f63f76a1 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateIndexNameProcessor.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateIndexNameProcessor.java @@ -39,9 +39,16 @@ public final class DateIndexNameProcessor extends AbstractProcessor { private final ZoneId timezone; private final List> dateFormats; - DateIndexNameProcessor(String tag, String description, String field, List> dateFormats, - ZoneId timezone, TemplateScript.Factory indexNamePrefixTemplate, TemplateScript.Factory dateRoundingTemplate, - TemplateScript.Factory indexNameFormatTemplate) { + DateIndexNameProcessor( + String tag, + String description, + String field, + List> dateFormats, + ZoneId timezone, + TemplateScript.Factory indexNamePrefixTemplate, + TemplateScript.Factory dateRoundingTemplate, + TemplateScript.Factory indexNameFormatTemplate + ) { super(tag, description); this.field = field; this.timezone = timezone; @@ -67,7 +74,7 @@ public IngestDocument execute(IngestDocument ingestDocument) throws Exception { try { dateTime = dateParser.apply(date); } catch (Exception e) { - //try the next parser and keep track of the exceptions + // try the next parser and keep track of the exceptions lastException = ExceptionsHelper.useOrSuppress(lastException, e); } } @@ -82,15 +89,20 @@ public IngestDocument execute(IngestDocument ingestDocument) throws Exception { DateFormatter formatter = DateFormatter.forPattern(indexNameFormat); // use UTC instead of Z is string representation of UTC, so behaviour is the same between 6.x and 7 String zone = timezone.equals(ZoneOffset.UTC) ? "UTC" : timezone.getId(); - StringBuilder builder = new StringBuilder() - .append('<') - .append(indexNamePrefix) - .append('{') - .append(formatter.format(dateTime)).append("||/").append(dateRounding) - .append('{').append(indexNameFormat).append('|').append(zone).append('}') - .append('}') - .append('>'); - String dynamicIndexName = builder.toString(); + StringBuilder builder = new StringBuilder().append('<') + .append(indexNamePrefix) + .append('{') + .append(formatter.format(dateTime)) + .append("||/") + .append(dateRounding) + .append('{') + .append(indexNameFormat) + .append('|') + .append(zone) + .append('}') + .append('}') + .append('>'); + String dynamicIndexName = builder.toString(); ingestDocument.setFieldValue(IngestDocument.Metadata.INDEX.getFieldName(), dynamicIndexName); return ingestDocument; } @@ -133,8 +145,12 @@ public Factory(ScriptService scriptService) { } @Override - public DateIndexNameProcessor create(Map registry, String tag, - String description, Map config) throws Exception { + public DateIndexNameProcessor create( + Map registry, + String tag, + String description, + Map config + ) throws Exception { String localeString = ConfigurationUtils.readOptionalStringProperty(TYPE, tag, config, "locale"); String timezoneString = ConfigurationUtils.readOptionalStringProperty(TYPE, tag, config, "timezone"); ZoneId timezone = timezoneString == null ? ZoneOffset.UTC : ZoneId.of(timezoneString); @@ -158,16 +174,39 @@ public DateIndexNameProcessor create(Map registry, St String field = ConfigurationUtils.readStringProperty(TYPE, tag, config, "field"); String indexNamePrefix = ConfigurationUtils.readStringProperty(TYPE, tag, config, "index_name_prefix", ""); - TemplateScript.Factory indexNamePrefixTemplate = - ConfigurationUtils.compileTemplate(TYPE, tag, "index_name_prefix", indexNamePrefix, scriptService); + TemplateScript.Factory indexNamePrefixTemplate = ConfigurationUtils.compileTemplate( + TYPE, + tag, + "index_name_prefix", + indexNamePrefix, + scriptService + ); String dateRounding = ConfigurationUtils.readStringProperty(TYPE, tag, config, "date_rounding"); - TemplateScript.Factory dateRoundingTemplate = - ConfigurationUtils.compileTemplate(TYPE, tag, "date_rounding", dateRounding, scriptService); + TemplateScript.Factory dateRoundingTemplate = ConfigurationUtils.compileTemplate( + TYPE, + tag, + "date_rounding", + dateRounding, + scriptService + ); String indexNameFormat = ConfigurationUtils.readStringProperty(TYPE, tag, config, "index_name_format", "yyyy-MM-dd"); - TemplateScript.Factory indexNameFormatTemplate = - ConfigurationUtils.compileTemplate(TYPE, tag, "index_name_format", indexNameFormat, scriptService); - return new DateIndexNameProcessor(tag, description, field, dateFormats, timezone, indexNamePrefixTemplate, - dateRoundingTemplate, indexNameFormatTemplate); + TemplateScript.Factory indexNameFormatTemplate = ConfigurationUtils.compileTemplate( + TYPE, + tag, + "index_name_format", + indexNameFormat, + scriptService + ); + return new DateIndexNameProcessor( + tag, + description, + field, + dateFormats, + timezone, + indexNamePrefixTemplate, + dateRoundingTemplate, + indexNameFormatTemplate + ); } } diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateProcessor.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateProcessor.java index 57471d76bdf1b..e6baafa3a9750 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateProcessor.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateProcessor.java @@ -9,9 +9,9 @@ package org.elasticsearch.ingest.common; import org.elasticsearch.ExceptionsHelper; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.time.DateFormatter; import org.elasticsearch.common.util.LocaleUtils; +import org.elasticsearch.core.Nullable; import org.elasticsearch.ingest.AbstractProcessor; import org.elasticsearch.ingest.ConfigurationUtils; import org.elasticsearch.ingest.IngestDocument; @@ -43,13 +43,28 @@ public final class DateProcessor extends AbstractProcessor { private final List, Function>> dateParsers; private final String outputFormat; - DateProcessor(String tag, String description, @Nullable TemplateScript.Factory timezone, @Nullable TemplateScript.Factory locale, - String field, List formats, String targetField) { + DateProcessor( + String tag, + String description, + @Nullable TemplateScript.Factory timezone, + @Nullable TemplateScript.Factory locale, + String field, + List formats, + String targetField + ) { this(tag, description, timezone, locale, field, formats, targetField, DEFAULT_OUTPUT_FORMAT); } - DateProcessor(String tag, String description, @Nullable TemplateScript.Factory timezone, @Nullable TemplateScript.Factory locale, - String field, List formats, String targetField, String outputFormat) { + DateProcessor( + String tag, + String description, + @Nullable TemplateScript.Factory timezone, + @Nullable TemplateScript.Factory locale, + String field, + List formats, + String targetField, + String outputFormat + ) { super(tag, description); this.timezone = timezone; this.locale = locale; @@ -88,7 +103,7 @@ public IngestDocument execute(IngestDocument ingestDocument) { try { dateTime = dateParser.apply(ingestDocument.getSourceAndMetadata()).apply(value); } catch (Exception e) { - //try the next parser and keep track of the exceptions + // try the next parser and keep track of the exceptions lastException = ExceptionsHelper.useOrSuppress(lastException, e); } } @@ -138,33 +153,48 @@ public Factory(ScriptService scriptService) { this.scriptService = scriptService; } - public DateProcessor create(Map registry, String processorTag, - String description, Map config) throws Exception { + public DateProcessor create( + Map registry, + String processorTag, + String description, + Map config + ) throws Exception { String field = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "field"); String targetField = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "target_field", DEFAULT_TARGET_FIELD); String timezoneString = ConfigurationUtils.readOptionalStringProperty(TYPE, processorTag, config, "timezone"); TemplateScript.Factory compiledTimezoneTemplate = null; if (timezoneString != null) { - compiledTimezoneTemplate = ConfigurationUtils.compileTemplate(TYPE, processorTag, - "timezone", timezoneString, scriptService); + compiledTimezoneTemplate = ConfigurationUtils.compileTemplate( + TYPE, + processorTag, + "timezone", + timezoneString, + scriptService + ); } String localeString = ConfigurationUtils.readOptionalStringProperty(TYPE, processorTag, config, "locale"); TemplateScript.Factory compiledLocaleTemplate = null; if (localeString != null) { - compiledLocaleTemplate = ConfigurationUtils.compileTemplate(TYPE, processorTag, - "locale", localeString, scriptService); + compiledLocaleTemplate = ConfigurationUtils.compileTemplate(TYPE, processorTag, "locale", localeString, scriptService); } List formats = ConfigurationUtils.readList(TYPE, processorTag, config, "formats"); - String outputFormat = - ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "output_format", DEFAULT_OUTPUT_FORMAT); + String outputFormat = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "output_format", DEFAULT_OUTPUT_FORMAT); try { DateFormatter.forPattern(outputFormat); } catch (Exception e) { throw new IllegalArgumentException("invalid output format [" + outputFormat + "]", e); } - return new DateProcessor(processorTag, description, compiledTimezoneTemplate, compiledLocaleTemplate, field, formats, - targetField, outputFormat); + return new DateProcessor( + processorTag, + description, + compiledTimezoneTemplate, + compiledLocaleTemplate, + field, + formats, + targetField, + outputFormat + ); } } } diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DissectProcessor.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DissectProcessor.java index 9f500c81b198c..929f77192846a 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DissectProcessor.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DissectProcessor.java @@ -19,7 +19,7 @@ public final class DissectProcessor extends AbstractProcessor { public static final String TYPE = "dissect"; - //package private members for testing + // package private members for testing final String field; final boolean ignoreMissing; final String pattern; @@ -55,8 +55,12 @@ public String getType() { public static final class Factory implements Processor.Factory { @Override - public DissectProcessor create(Map registry, String processorTag, String description, - Map config) { + public DissectProcessor create( + Map registry, + String processorTag, + String description, + Map config + ) { String field = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "field"); String pattern = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "pattern"); String appendSeparator = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "append_separator", ""); diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DotExpanderProcessor.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DotExpanderProcessor.java index 76fdfbf03747e..cf660683096d7 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DotExpanderProcessor.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DotExpanderProcessor.java @@ -81,8 +81,9 @@ private void expandDot(IngestDocument ingestDocument, String path, String field, if (ingestDocument.hasField(partialPath)) { Object val = ingestDocument.getFieldValue(partialPath, Object.class); if ((val instanceof Map) == false) { - throw new IllegalArgumentException("cannot expend [" + path + "], because [" + partialPath + - "] is not an object field, but a value field"); + throw new IllegalArgumentException( + "cannot expend [" + path + "], because [" + partialPath + "] is not an object field, but a value field" + ); } } else { break; @@ -110,22 +111,33 @@ String getField() { public static final class Factory implements Processor.Factory { @Override - public Processor create(Map processorFactories, String tag, String description, - Map config) throws Exception { + public Processor create( + Map processorFactories, + String tag, + String description, + Map config + ) throws Exception { String field = ConfigurationUtils.readStringProperty(TYPE, tag, config, "field"); if (field.contains(".") == false && field.equals("*") == false) { - throw ConfigurationUtils.newConfigurationException(ConfigurationUtils.TAG_KEY, tag, "field", - "field does not contain a dot and is not a wildcard"); + throw ConfigurationUtils.newConfigurationException( + ConfigurationUtils.TAG_KEY, + tag, + "field", + "field does not contain a dot and is not a wildcard" + ); } if (field.indexOf('.') == 0 || field.lastIndexOf('.') == field.length() - 1) { - throw ConfigurationUtils.newConfigurationException(ConfigurationUtils.TAG_KEY, tag, "field", - "Field can't start or end with a dot"); + throw ConfigurationUtils.newConfigurationException( + ConfigurationUtils.TAG_KEY, + tag, + "field", + "Field can't start or end with a dot" + ); } int firstIndex = -1; for (int index = field.indexOf('.'); index != -1; index = field.indexOf('.', index + 1)) { if (index - firstIndex == 1) { - throw ConfigurationUtils.newConfigurationException(ConfigurationUtils.TAG_KEY, tag, "field", - "No space between dots"); + throw ConfigurationUtils.newConfigurationException(ConfigurationUtils.TAG_KEY, tag, "field", "No space between dots"); } firstIndex = index; } diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/FailProcessor.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/FailProcessor.java index 3497afcbef513..3d08c72f522b9 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/FailProcessor.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/FailProcessor.java @@ -55,13 +55,21 @@ public Factory(ScriptService scriptService) { } @Override - public FailProcessor create(Map registry, String processorTag, - String description, Map config) throws Exception { + public FailProcessor create( + Map registry, + String processorTag, + String description, + Map config + ) throws Exception { String message = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "message"); - TemplateScript.Factory compiledTemplate = ConfigurationUtils.compileTemplate(TYPE, processorTag, - "message", message, scriptService); + TemplateScript.Factory compiledTemplate = ConfigurationUtils.compileTemplate( + TYPE, + processorTag, + "message", + message, + scriptService + ); return new FailProcessor(processorTag, description, compiledTemplate); } } } - diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/FailProcessorException.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/FailProcessorException.java index 9c10a2ead2386..6cfda53ab2e2f 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/FailProcessorException.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/FailProcessorException.java @@ -24,4 +24,3 @@ public FailProcessorException(String message) { super(message); } } - diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/ForEachProcessor.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/ForEachProcessor.java index 98646f4ef41ab..09372a238a54b 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/ForEachProcessor.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/ForEachProcessor.java @@ -71,13 +71,20 @@ public void execute(IngestDocument ingestDocument, BiConsumer list = (List) o; innerExecuteList(0, new ArrayList<>(list), new ArrayList<>(list.size()), ingestDocument, handler); } else { - throw new IllegalArgumentException("field [" + field + "] of type [" + o.getClass().getName() + "] cannot be cast to a " + - "list or map"); + throw new IllegalArgumentException( + "field [" + field + "] of type [" + o.getClass().getName() + "] cannot be cast to a " + "list or map" + ); } } - void innerExecuteMap(int keyIndex, Map map, List keys, Map newValues, IngestDocument document, - BiConsumer handler) { + void innerExecuteMap( + int keyIndex, + Map map, + List keys, + Map newValues, + IngestDocument document, + BiConsumer handler + ) { for (; keyIndex < keys.size(); keyIndex++) { AtomicBoolean shouldContinueHere = new AtomicBoolean(); String key = (String) keys.get(keyIndex); @@ -109,8 +116,13 @@ void innerExecuteMap(int keyIndex, Map map, List keys, Map values, List newValues, IngestDocument document, - BiConsumer handler) { + void innerExecuteList( + int index, + List values, + List newValues, + IngestDocument document, + BiConsumer handler + ) { for (; index < values.size(); index++) { AtomicBoolean shouldContinueHere = new AtomicBoolean(); Object value = values.get(index); @@ -163,8 +175,8 @@ public static final class Factory implements Processor.Factory { } @Override - public ForEachProcessor create(Map factories, String tag, - String description, Map config) throws Exception { + public ForEachProcessor create(Map factories, String tag, String description, Map config) + throws Exception { String field = readStringProperty(TYPE, tag, config, "field"); boolean ignoreMissing = readBooleanProperty(TYPE, tag, config, "ignore_missing", false); Map> processorConfig = readMap(TYPE, tag, config, "processor"); @@ -173,8 +185,7 @@ public ForEachProcessor create(Map factories, String throw newConfigurationException(TYPE, tag, "processor", "Must specify exactly one processor type"); } Map.Entry> entry = entries.iterator().next(); - Processor processor = - ConfigurationUtils.readProcessor(factories, scriptService, entry.getKey(), entry.getValue()); + Processor processor = ConfigurationUtils.readProcessor(factories, scriptService, entry.getKey(), entry.getValue()); return new ForEachProcessor(tag, description, field, processor, ignoreMissing); } } diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/GrokProcessor.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/GrokProcessor.java index 73f26b895ff57..bda1f6fd3cd13 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/GrokProcessor.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/GrokProcessor.java @@ -37,8 +37,16 @@ public final class GrokProcessor extends AbstractProcessor { private final boolean traceMatch; private final boolean ignoreMissing; - GrokProcessor(String tag, String description, Map patternBank, List matchPatterns, String matchField, - boolean traceMatch, boolean ignoreMissing, MatcherWatchdog matcherWatchdog) { + GrokProcessor( + String tag, + String description, + Map patternBank, + List matchPatterns, + String matchField, + boolean traceMatch, + boolean ignoreMissing, + MatcherWatchdog matcherWatchdog + ) { super(tag, description); this.matchField = matchField; this.matchPatterns = matchPatterns; @@ -71,9 +79,7 @@ public IngestDocument execute(IngestDocument ingestDocument) throws Exception { if (matchPatterns.size() > 1) { @SuppressWarnings("unchecked") HashMap matchMap = (HashMap) ingestDocument.getFieldValue(PATTERN_MATCH_KEY, Object.class); - matchMap.keySet().stream().findFirst().ifPresent((index) -> { - ingestDocument.setFieldValue(PATTERN_MATCH_KEY, index); - }); + matchMap.keySet().stream().findFirst().ifPresent((index) -> { ingestDocument.setFieldValue(PATTERN_MATCH_KEY, index); }); } else { ingestDocument.setFieldValue(PATTERN_MATCH_KEY, "0"); } @@ -120,7 +126,7 @@ static String combinePatterns(List patterns, boolean traceMatch) { combinedPattern = combinedPattern + "|" + valueWrap; } } - } else { + } else { combinedPattern = patterns.get(0); } @@ -136,14 +142,23 @@ public Factory(MatcherWatchdog matcherWatchdog) { } @Override - public GrokProcessor create(Map registry, String processorTag, - String description, Map config) throws Exception { + public GrokProcessor create( + Map registry, + String processorTag, + String description, + Map config + ) throws Exception { String matchField = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "field"); List matchPatterns = ConfigurationUtils.readList(TYPE, processorTag, config, "patterns"); boolean traceMatch = ConfigurationUtils.readBooleanProperty(TYPE, processorTag, config, "trace_match", false); boolean ignoreMissing = ConfigurationUtils.readBooleanProperty(TYPE, processorTag, config, "ignore_missing", false); - String ecsCompatibility = - ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "ecs_compatibility", DEFAULT_ECS_COMPATIBILITY_MODE); + String ecsCompatibility = ConfigurationUtils.readStringProperty( + TYPE, + processorTag, + config, + "ecs_compatibility", + DEFAULT_ECS_COMPATIBILITY_MODE + ); if (Grok.isValidEcsCompatibilityMode(ecsCompatibility) == false) { throw newConfigurationException(TYPE, processorTag, "ecs_compatibility", "unsupported mode '" + ecsCompatibility + "'"); } @@ -152,19 +167,29 @@ public GrokProcessor create(Map registry, String proc throw newConfigurationException(TYPE, processorTag, "patterns", "List of patterns must not be empty"); } Map customPatternBank = ConfigurationUtils.readOptionalMap(TYPE, processorTag, config, "pattern_definitions"); - Map patternBank = new HashMap<>( - Grok.getBuiltinPatterns(ecsCompatibility) - ); + Map patternBank = new HashMap<>(Grok.getBuiltinPatterns(ecsCompatibility)); if (customPatternBank != null) { patternBank.putAll(customPatternBank); } try { - return new GrokProcessor(processorTag, description, patternBank, matchPatterns, matchField, traceMatch, ignoreMissing, - matcherWatchdog); + return new GrokProcessor( + processorTag, + description, + patternBank, + matchPatterns, + matchField, + traceMatch, + ignoreMissing, + matcherWatchdog + ); } catch (Exception e) { - throw newConfigurationException(TYPE, processorTag, "patterns", - "Invalid regex pattern found in: " + matchPatterns + ". " + e.getMessage()); + throw newConfigurationException( + TYPE, + processorTag, + "patterns", + "Invalid regex pattern found in: " + matchPatterns + ". " + e.getMessage() + ); } } diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/GrokProcessorGetAction.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/GrokProcessorGetAction.java index 2b61265dfd290..a21d91a4fe2dd 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/GrokProcessorGetAction.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/GrokProcessorGetAction.java @@ -19,14 +19,14 @@ import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.xcontent.ToXContentObject; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.grok.Grok; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestToXContentListener; import org.elasticsearch.tasks.Task; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.ToXContentObject; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.List; @@ -133,7 +133,8 @@ public TransportAction(TransportService transportService, ActionFilters actionFi TransportService transportService, ActionFilters actionFilters, Map legacyGrokPatterns, - Map ecsV1GrokPatterns) { + Map ecsV1GrokPatterns + ) { super(NAME, transportService, actionFilters, Request::new); this.legacyGrokPatterns = legacyGrokPatterns; this.sortedLegacyGrokPatterns = new TreeMap<>(this.legacyGrokPatterns); @@ -144,10 +145,12 @@ public TransportAction(TransportService transportService, ActionFilters actionFi @Override protected void doExecute(Task task, Request request, ActionListener listener) { try { - listener.onResponse(new Response( - request.getEcsCompatibility().equals(Grok.ECS_COMPATIBILITY_MODES[0]) - ? request.sorted() ? sortedLegacyGrokPatterns : legacyGrokPatterns - : request.sorted() ? sortedEcsV1GrokPatterns : ecsV1GrokPatterns + listener.onResponse( + new Response( + request.getEcsCompatibility().equals(Grok.ECS_COMPATIBILITY_MODES[0]) + ? request.sorted() ? sortedLegacyGrokPatterns : legacyGrokPatterns + : request.sorted() ? sortedEcsV1GrokPatterns + : ecsV1GrokPatterns ) ); } catch (Exception e) { diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/GsubProcessor.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/GsubProcessor.java index 9bfe8b6e4a067..d93ca025469dd 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/GsubProcessor.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/GsubProcessor.java @@ -25,8 +25,15 @@ public final class GsubProcessor extends AbstractStringProcessor { private final Pattern pattern; private final String replacement; - GsubProcessor(String tag, String description, String field, Pattern pattern, String replacement, boolean ignoreMissing, - String targetField) { + GsubProcessor( + String tag, + String description, + String field, + Pattern pattern, + String replacement, + boolean ignoreMissing, + String targetField + ) { super(tag, description, ignoreMissing, targetField, field); this.pattern = pattern; this.replacement = replacement; @@ -57,8 +64,14 @@ public Factory() { } @Override - protected GsubProcessor newProcessor(String processorTag, String description, Map config, String field, - boolean ignoreMissing, String targetField) { + protected GsubProcessor newProcessor( + String processorTag, + String description, + Map config, + String field, + boolean ignoreMissing, + String targetField + ) { String pattern = readStringProperty(TYPE, processorTag, config, "pattern"); String replacement = readStringProperty(TYPE, processorTag, config, "replacement"); Pattern searchPattern; diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/HtmlStripProcessor.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/HtmlStripProcessor.java index 8917a36654f5c..b6d96d1b3f9af 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/HtmlStripProcessor.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/HtmlStripProcessor.java @@ -55,8 +55,14 @@ public Factory() { } @Override - protected HtmlStripProcessor newProcessor(String tag, String description, Map config, String field, - boolean ignoreMissing, String targetField) { + protected HtmlStripProcessor newProcessor( + String tag, + String description, + Map config, + String field, + boolean ignoreMissing, + String targetField + ) { return new HtmlStripProcessor(tag, description, field, ignoreMissing, targetField); } } diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/IngestCommonPlugin.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/IngestCommonPlugin.java index 945ce13957124..4aa57b7928e22 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/IngestCommonPlugin.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/IngestCommonPlugin.java @@ -38,64 +38,75 @@ public class IngestCommonPlugin extends Plugin implements ActionPlugin, IngestPlugin { - static final Setting WATCHDOG_INTERVAL = - Setting.timeSetting("ingest.grok.watchdog.interval", TimeValue.timeValueSeconds(1), Setting.Property.NodeScope); - static final Setting WATCHDOG_MAX_EXECUTION_TIME = - Setting.timeSetting("ingest.grok.watchdog.max_execution_time", TimeValue.timeValueSeconds(1), Setting.Property.NodeScope); + static final Setting WATCHDOG_INTERVAL = Setting.timeSetting( + "ingest.grok.watchdog.interval", + TimeValue.timeValueSeconds(1), + Setting.Property.NodeScope + ); + static final Setting WATCHDOG_MAX_EXECUTION_TIME = Setting.timeSetting( + "ingest.grok.watchdog.max_execution_time", + TimeValue.timeValueSeconds(1), + Setting.Property.NodeScope + ); - public IngestCommonPlugin() { - } + public IngestCommonPlugin() {} @Override public Map getProcessors(Processor.Parameters parameters) { return Map.ofEntries( - entry(DateProcessor.TYPE, new DateProcessor.Factory(parameters.scriptService)), - entry(SetProcessor.TYPE, new SetProcessor.Factory(parameters.scriptService)), - entry(AppendProcessor.TYPE, new AppendProcessor.Factory(parameters.scriptService)), - entry(RenameProcessor.TYPE, new RenameProcessor.Factory(parameters.scriptService)), - entry(RemoveProcessor.TYPE, new RemoveProcessor.Factory(parameters.scriptService)), - entry(SplitProcessor.TYPE, new SplitProcessor.Factory()), - entry(JoinProcessor.TYPE, new JoinProcessor.Factory()), - entry(UppercaseProcessor.TYPE, new UppercaseProcessor.Factory()), - entry(LowercaseProcessor.TYPE, new LowercaseProcessor.Factory()), - entry(TrimProcessor.TYPE, new TrimProcessor.Factory()), - entry(ConvertProcessor.TYPE, new ConvertProcessor.Factory()), - entry(GsubProcessor.TYPE, new GsubProcessor.Factory()), - entry(FailProcessor.TYPE, new FailProcessor.Factory(parameters.scriptService)), - entry(ForEachProcessor.TYPE, new ForEachProcessor.Factory(parameters.scriptService)), - entry(DateIndexNameProcessor.TYPE, new DateIndexNameProcessor.Factory(parameters.scriptService)), - entry(SortProcessor.TYPE, new SortProcessor.Factory()), - entry(GrokProcessor.TYPE, new GrokProcessor.Factory(createGrokThreadWatchdog(parameters))), - entry(ScriptProcessor.TYPE, new ScriptProcessor.Factory(parameters.scriptService)), - entry(DotExpanderProcessor.TYPE, new DotExpanderProcessor.Factory()), - entry(JsonProcessor.TYPE, new JsonProcessor.Factory()), - entry(KeyValueProcessor.TYPE, new KeyValueProcessor.Factory(parameters.scriptService)), - entry(URLDecodeProcessor.TYPE, new URLDecodeProcessor.Factory()), - entry(BytesProcessor.TYPE, new BytesProcessor.Factory()), - entry(PipelineProcessor.TYPE, new PipelineProcessor.Factory(parameters.ingestService)), - entry(DissectProcessor.TYPE, new DissectProcessor.Factory()), - entry(DropProcessor.TYPE, new DropProcessor.Factory()), - entry(HtmlStripProcessor.TYPE, new HtmlStripProcessor.Factory()), - entry(CsvProcessor.TYPE, new CsvProcessor.Factory()), - entry(UriPartsProcessor.TYPE, new UriPartsProcessor.Factory()), - entry(NetworkDirectionProcessor.TYPE, new NetworkDirectionProcessor.Factory(parameters.scriptService)), - entry(CommunityIdProcessor.TYPE, new CommunityIdProcessor.Factory()), - entry(FingerprintProcessor.TYPE, new FingerprintProcessor.Factory()), - entry(RegisteredDomainProcessor.TYPE, new RegisteredDomainProcessor.Factory()) - ); + entry(DateProcessor.TYPE, new DateProcessor.Factory(parameters.scriptService)), + entry(SetProcessor.TYPE, new SetProcessor.Factory(parameters.scriptService)), + entry(AppendProcessor.TYPE, new AppendProcessor.Factory(parameters.scriptService)), + entry(RenameProcessor.TYPE, new RenameProcessor.Factory(parameters.scriptService)), + entry(RemoveProcessor.TYPE, new RemoveProcessor.Factory(parameters.scriptService)), + entry(SplitProcessor.TYPE, new SplitProcessor.Factory()), + entry(JoinProcessor.TYPE, new JoinProcessor.Factory()), + entry(UppercaseProcessor.TYPE, new UppercaseProcessor.Factory()), + entry(LowercaseProcessor.TYPE, new LowercaseProcessor.Factory()), + entry(TrimProcessor.TYPE, new TrimProcessor.Factory()), + entry(ConvertProcessor.TYPE, new ConvertProcessor.Factory()), + entry(GsubProcessor.TYPE, new GsubProcessor.Factory()), + entry(FailProcessor.TYPE, new FailProcessor.Factory(parameters.scriptService)), + entry(ForEachProcessor.TYPE, new ForEachProcessor.Factory(parameters.scriptService)), + entry(DateIndexNameProcessor.TYPE, new DateIndexNameProcessor.Factory(parameters.scriptService)), + entry(SortProcessor.TYPE, new SortProcessor.Factory()), + entry(GrokProcessor.TYPE, new GrokProcessor.Factory(createGrokThreadWatchdog(parameters))), + entry(ScriptProcessor.TYPE, new ScriptProcessor.Factory(parameters.scriptService)), + entry(DotExpanderProcessor.TYPE, new DotExpanderProcessor.Factory()), + entry(JsonProcessor.TYPE, new JsonProcessor.Factory()), + entry(KeyValueProcessor.TYPE, new KeyValueProcessor.Factory(parameters.scriptService)), + entry(URLDecodeProcessor.TYPE, new URLDecodeProcessor.Factory()), + entry(BytesProcessor.TYPE, new BytesProcessor.Factory()), + entry(PipelineProcessor.TYPE, new PipelineProcessor.Factory(parameters.ingestService)), + entry(DissectProcessor.TYPE, new DissectProcessor.Factory()), + entry(DropProcessor.TYPE, new DropProcessor.Factory()), + entry(HtmlStripProcessor.TYPE, new HtmlStripProcessor.Factory()), + entry(CsvProcessor.TYPE, new CsvProcessor.Factory()), + entry(UriPartsProcessor.TYPE, new UriPartsProcessor.Factory()), + entry(NetworkDirectionProcessor.TYPE, new NetworkDirectionProcessor.Factory(parameters.scriptService)), + entry(CommunityIdProcessor.TYPE, new CommunityIdProcessor.Factory()), + entry(FingerprintProcessor.TYPE, new FingerprintProcessor.Factory()), + entry(RegisteredDomainProcessor.TYPE, new RegisteredDomainProcessor.Factory()) + ); } @Override public List> getActions() { return Collections.singletonList( - new ActionHandler<>(GrokProcessorGetAction.INSTANCE, GrokProcessorGetAction.TransportAction.class)); + new ActionHandler<>(GrokProcessorGetAction.INSTANCE, GrokProcessorGetAction.TransportAction.class) + ); } @Override - public List getRestHandlers(Settings settings, RestController restController, ClusterSettings clusterSettings, - IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter, - IndexNameExpressionResolver indexNameExpressionResolver, - Supplier nodesInCluster) { + public List getRestHandlers( + Settings settings, + RestController restController, + ClusterSettings clusterSettings, + IndexScopedSettings indexScopedSettings, + SettingsFilter settingsFilter, + IndexNameExpressionResolver indexNameExpressionResolver, + Supplier nodesInCluster + ) { return Collections.singletonList(new GrokProcessorGetAction.RestAction()); } @@ -107,8 +118,12 @@ public List> getSettings() { private static MatcherWatchdog createGrokThreadWatchdog(Processor.Parameters parameters) { long intervalMillis = WATCHDOG_INTERVAL.get(parameters.env.settings()).getMillis(); long maxExecutionTimeMillis = WATCHDOG_MAX_EXECUTION_TIME.get(parameters.env.settings()).getMillis(); - return MatcherWatchdog.newInstance(intervalMillis, maxExecutionTimeMillis, - parameters.relativeTimeSupplier, parameters.scheduler::apply); + return MatcherWatchdog.newInstance( + intervalMillis, + maxExecutionTimeMillis, + parameters.relativeTimeSupplier, + parameters.scheduler::apply + ); } } diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/JoinProcessor.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/JoinProcessor.java index 2a956711cceba..ca664206e575a 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/JoinProcessor.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/JoinProcessor.java @@ -54,9 +54,7 @@ public IngestDocument execute(IngestDocument document) { if (list == null) { throw new IllegalArgumentException("field [" + field + "] is null, cannot join."); } - String joined = list.stream() - .map(Object::toString) - .collect(Collectors.joining(separator)); + String joined = list.stream().map(Object::toString).collect(Collectors.joining(separator)); document.setFieldValue(targetField, joined); return document; } @@ -68,8 +66,12 @@ public String getType() { public static final class Factory implements Processor.Factory { @Override - public JoinProcessor create(Map registry, String processorTag, - String description, Map config) throws Exception { + public JoinProcessor create( + Map registry, + String processorTag, + String description, + Map config + ) throws Exception { String field = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "field"); String separator = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "separator"); String targetField = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "target_field", field); @@ -77,4 +79,3 @@ public JoinProcessor create(Map registry, String proc } } } - diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/JsonProcessor.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/JsonProcessor.java index b179c02e0a80a..83cd59bc1b4be 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/JsonProcessor.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/JsonProcessor.java @@ -10,14 +10,14 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.xcontent.DeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.ingest.AbstractProcessor; import org.elasticsearch.ingest.ConfigurationUtils; import org.elasticsearch.ingest.IngestDocument; import org.elasticsearch.ingest.Processor; +import org.elasticsearch.xcontent.DeprecationHandler; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.json.JsonXContent; import java.io.IOException; import java.io.InputStream; @@ -40,8 +40,15 @@ public final class JsonProcessor extends AbstractProcessor { private final ConflictStrategy addToRootConflictStrategy; private final boolean allowDuplicateKeys; - JsonProcessor(String tag, String description, String field, String targetField, boolean addToRoot, - ConflictStrategy addToRootConflictStrategy, boolean allowDuplicateKeys) { + JsonProcessor( + String tag, + String description, + String field, + String targetField, + boolean addToRoot, + ConflictStrategy addToRootConflictStrategy, + boolean allowDuplicateKeys + ) { super(tag, description); this.field = field; this.targetField = targetField; @@ -68,9 +75,14 @@ public ConflictStrategy getAddToRootConflictStrategy() { public static Object apply(Object fieldValue, boolean allowDuplicateKeys) { BytesReference bytesRef = fieldValue == null ? new BytesArray("null") : new BytesArray(fieldValue.toString()); - try (InputStream stream = bytesRef.streamInput(); - XContentParser parser = JsonXContent.jsonXContent - .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, stream)) { + try ( + InputStream stream = bytesRef.streamInput(); + XContentParser parser = JsonXContent.jsonXContent.createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + stream + ) + ) { parser.allowDuplicateKeys(allowDuplicateKeys); XContentParser.Token token = parser.nextToken(); Object value = null; @@ -162,14 +174,22 @@ public static ConflictStrategy fromString(String conflictStrategy) { public static final class Factory implements Processor.Factory { @Override - public JsonProcessor create(Map registry, String processorTag, - String description, Map config) throws Exception { + public JsonProcessor create( + Map registry, + String processorTag, + String description, + Map config + ) throws Exception { String field = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "field"); String targetField = ConfigurationUtils.readOptionalStringProperty(TYPE, processorTag, config, "target_field"); boolean addToRoot = ConfigurationUtils.readBooleanProperty(TYPE, processorTag, config, "add_to_root", false); boolean allowDuplicateKeys = ConfigurationUtils.readBooleanProperty(TYPE, processorTag, config, "allow_duplicate_keys", false); - String conflictStrategyString = ConfigurationUtils.readOptionalStringProperty(TYPE, processorTag, config, - "add_to_root_conflict_strategy"); + String conflictStrategyString = ConfigurationUtils.readOptionalStringProperty( + TYPE, + processorTag, + config, + "add_to_root_conflict_strategy" + ); boolean hasConflictStrategy = conflictStrategyString != null; if (conflictStrategyString == null) { conflictStrategyString = ConflictStrategy.REPLACE.name(); @@ -178,26 +198,44 @@ public JsonProcessor create(Map registry, String proc try { addToRootConflictStrategy = ConflictStrategy.fromString(conflictStrategyString); } catch (IllegalArgumentException e) { - throw newConfigurationException(TYPE, processorTag, "add_to_root_conflict_strategy", "conflict strategy [" + - conflictStrategyString + "] not supported, cannot convert field."); + throw newConfigurationException( + TYPE, + processorTag, + "add_to_root_conflict_strategy", + "conflict strategy [" + conflictStrategyString + "] not supported, cannot convert field." + ); } if (addToRoot && targetField != null) { - throw newConfigurationException(TYPE, processorTag, "target_field", - "Cannot set a target field while also setting `add_to_root` to true"); + throw newConfigurationException( + TYPE, + processorTag, + "target_field", + "Cannot set a target field while also setting `add_to_root` to true" + ); } if (addToRoot == false && hasConflictStrategy) { - throw newConfigurationException(TYPE, processorTag, "add_to_root_conflict_strategy", - "Cannot set `add_to_root_conflict_strategy` if `add_to_root` is false"); + throw newConfigurationException( + TYPE, + processorTag, + "add_to_root_conflict_strategy", + "Cannot set `add_to_root_conflict_strategy` if `add_to_root` is false" + ); } if (targetField == null) { targetField = field; } - return new JsonProcessor(processorTag, description, field, targetField, addToRoot, addToRootConflictStrategy, - allowDuplicateKeys); + return new JsonProcessor( + processorTag, + description, + field, + targetField, + addToRoot, + addToRootConflictStrategy, + allowDuplicateKeys + ); } } } - diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/KeyValueProcessor.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/KeyValueProcessor.java index d46ed71f68186..68c573216bc94 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/KeyValueProcessor.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/KeyValueProcessor.java @@ -43,9 +43,21 @@ public final class KeyValueProcessor extends AbstractProcessor { private final boolean ignoreMissing; private final Consumer execution; - KeyValueProcessor(String tag, String description, TemplateScript.Factory field, String fieldSplit, String valueSplit, - Set includeKeys, Set excludeKeys, TemplateScript.Factory targetField, boolean ignoreMissing, - String trimKey, String trimValue, boolean stripBrackets, String prefix) { + KeyValueProcessor( + String tag, + String description, + TemplateScript.Factory field, + String fieldSplit, + String valueSplit, + Set includeKeys, + Set excludeKeys, + TemplateScript.Factory targetField, + boolean ignoreMissing, + String trimKey, + String trimValue, + boolean stripBrackets, + String prefix + ) { super(tag, description); this.field = field; this.targetField = targetField; @@ -55,16 +67,33 @@ public final class KeyValueProcessor extends AbstractProcessor { this.excludeKeys = excludeKeys; this.ignoreMissing = ignoreMissing; this.execution = buildExecution( - fieldSplit, valueSplit, field, includeKeys, excludeKeys, targetField, ignoreMissing, trimKey, trimValue, - stripBrackets, prefix + fieldSplit, + valueSplit, + field, + includeKeys, + excludeKeys, + targetField, + ignoreMissing, + trimKey, + trimValue, + stripBrackets, + prefix ); } - private static Consumer buildExecution(String fieldSplit, String valueSplit, TemplateScript.Factory field, - Set includeKeys, Set excludeKeys, - TemplateScript.Factory targetField, boolean ignoreMissing, - String trimKey, String trimValue, boolean stripBrackets, - String prefix) { + private static Consumer buildExecution( + String fieldSplit, + String valueSplit, + TemplateScript.Factory field, + Set includeKeys, + Set excludeKeys, + TemplateScript.Factory targetField, + boolean ignoreMissing, + String trimKey, + String trimValue, + boolean stripBrackets, + String prefix + ) { final Predicate keyFilter; if (includeKeys == null) { if (excludeKeys == null) { @@ -211,16 +240,18 @@ public Factory(ScriptService scriptService) { } @Override - public KeyValueProcessor create(Map registry, String processorTag, - String description, Map config) throws Exception { + public KeyValueProcessor create( + Map registry, + String processorTag, + String description, + Map config + ) throws Exception { String field = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "field"); - TemplateScript.Factory fieldTemplate = ConfigurationUtils.compileTemplate(TYPE, processorTag, - "field", field, scriptService); + TemplateScript.Factory fieldTemplate = ConfigurationUtils.compileTemplate(TYPE, processorTag, "field", field, scriptService); String targetField = ConfigurationUtils.readOptionalStringProperty(TYPE, processorTag, config, "target_field"); TemplateScript.Factory targetFieldTemplate = null; if (targetField != null) { - targetFieldTemplate = ConfigurationUtils.compileTemplate(TYPE, processorTag, - "target_field", targetField, scriptService); + targetFieldTemplate = ConfigurationUtils.compileTemplate(TYPE, processorTag, "target_field", targetField, scriptService); } String fieldSplit = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "field_split"); @@ -228,8 +259,7 @@ public KeyValueProcessor create(Map registry, String String trimKey = ConfigurationUtils.readOptionalStringProperty(TYPE, processorTag, config, "trim_key"); String trimValue = ConfigurationUtils.readOptionalStringProperty(TYPE, processorTag, config, "trim_value"); String prefix = ConfigurationUtils.readOptionalStringProperty(TYPE, processorTag, config, "prefix"); - boolean stripBrackets = - ConfigurationUtils.readBooleanProperty(TYPE, processorTag, config, "strip_brackets", false); + boolean stripBrackets = ConfigurationUtils.readBooleanProperty(TYPE, processorTag, config, "strip_brackets", false); Set includeKeys = null; Set excludeKeys = null; List includeKeysList = ConfigurationUtils.readOptionalList(TYPE, processorTag, config, "include_keys"); @@ -242,8 +272,19 @@ public KeyValueProcessor create(Map registry, String } boolean ignoreMissing = ConfigurationUtils.readBooleanProperty(TYPE, processorTag, config, "ignore_missing", false); return new KeyValueProcessor( - processorTag, description, fieldTemplate, fieldSplit, valueSplit, includeKeys, excludeKeys, targetFieldTemplate, - ignoreMissing, trimKey, trimValue, stripBrackets, prefix + processorTag, + description, + fieldTemplate, + fieldSplit, + valueSplit, + includeKeys, + excludeKeys, + targetFieldTemplate, + ignoreMissing, + trimKey, + trimValue, + stripBrackets, + prefix ); } } diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/LowercaseProcessor.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/LowercaseProcessor.java index 7dab3e3ab2e02..6f71ef382a3aa 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/LowercaseProcessor.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/LowercaseProcessor.java @@ -45,8 +45,14 @@ public Factory() { } @Override - protected LowercaseProcessor newProcessor(String tag, String description, Map config, String field, - boolean ignoreMissing, String targetField) { + protected LowercaseProcessor newProcessor( + String tag, + String description, + Map config, + String field, + boolean ignoreMissing, + String targetField + ) { return new LowercaseProcessor(tag, description, field, ignoreMissing, targetField); } } diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/NetworkDirectionProcessor.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/NetworkDirectionProcessor.java index bbdb1c607d705..8b40bfee544b7 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/NetworkDirectionProcessor.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/NetworkDirectionProcessor.java @@ -284,7 +284,8 @@ public NetworkDirectionProcessor create( throw newConfigurationException( TYPE, processorTag, - "internal_networks", "and [internal_networks_field] cannot both be used in the same processor" + "internal_networks", + "and [internal_networks_field] cannot both be used in the same processor" ); } diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/Processors.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/Processors.java index 80f2aafd310f4..5a5e1b5144c0c 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/Processors.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/Processors.java @@ -108,7 +108,8 @@ public static String communityId( Object destinationPort, Object icmpType, Object icmpCode, - int seed) { + int seed + ) { return CommunityIdProcessor.apply( sourceIpAddrString, destIpAddrString, @@ -143,15 +144,18 @@ public static String communityId( Object sourcePort, Object destinationPort, Object icmpType, - Object icmpCode) { - return CommunityIdProcessor.apply(sourceIpAddrString, + Object icmpCode + ) { + return CommunityIdProcessor.apply( + sourceIpAddrString, destIpAddrString, ianaNumber, transport, sourcePort, destinationPort, icmpType, - icmpCode); + icmpCode + ); } /* diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/ProcessorsWhitelistExtension.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/ProcessorsWhitelistExtension.java index d9ac9e8fa5751..9f736dc28ee2c 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/ProcessorsWhitelistExtension.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/ProcessorsWhitelistExtension.java @@ -20,8 +20,10 @@ public class ProcessorsWhitelistExtension implements PainlessExtension { - private static final Whitelist WHITELIST = - WhitelistLoader.loadFromResourceFiles(ProcessorsWhitelistExtension.class, "processors_whitelist.txt"); + private static final Whitelist WHITELIST = WhitelistLoader.loadFromResourceFiles( + ProcessorsWhitelistExtension.class, + "processors_whitelist.txt" + ); @Override public Map, List> getContextWhitelists() { diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/RegisteredDomainProcessor.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/RegisteredDomainProcessor.java index d9f8eb8eac4c4..0afc659baa282 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/RegisteredDomainProcessor.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/RegisteredDomainProcessor.java @@ -26,13 +26,7 @@ public class RegisteredDomainProcessor extends AbstractProcessor { private final String targetField; private final boolean ignoreMissing; - RegisteredDomainProcessor( - String tag, - String description, - String field, - String targetField, - boolean ignoreMissing - ) { + RegisteredDomainProcessor(String tag, String description, String field, String targetField, boolean ignoreMissing) { super(tag, description); this.field = field; this.targetField = targetField; @@ -174,13 +168,7 @@ public RegisteredDomainProcessor create( String targetField = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "target_field", DEFAULT_TARGET_FIELD); boolean ignoreMissing = ConfigurationUtils.readBooleanProperty(TYPE, processorTag, config, "ignore_missing", true); - return new RegisteredDomainProcessor( - processorTag, - description, - field, - targetField, - ignoreMissing - ); + return new RegisteredDomainProcessor(processorTag, description, field, targetField, ignoreMissing); } } } diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/RemoveProcessor.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/RemoveProcessor.java index ec4178d19c469..f525c7cc1a73a 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/RemoveProcessor.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/RemoveProcessor.java @@ -69,8 +69,12 @@ public Factory(ScriptService scriptService) { } @Override - public RemoveProcessor create(Map registry, String processorTag, - String description, Map config) throws Exception { + public RemoveProcessor create( + Map registry, + String processorTag, + String description, + Map config + ) throws Exception { final List fields = new ArrayList<>(); final Object field = ConfigurationUtils.readObject(TYPE, processorTag, config, "field"); if (field instanceof List) { @@ -89,4 +93,3 @@ public RemoveProcessor create(Map registry, String pr } } } - diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/RenameProcessor.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/RenameProcessor.java index 76eec9778fa79..d73eb1906ac5b 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/RenameProcessor.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/RenameProcessor.java @@ -28,8 +28,13 @@ public final class RenameProcessor extends AbstractProcessor { private final TemplateScript.Factory targetField; private final boolean ignoreMissing; - RenameProcessor(String tag, String description, TemplateScript.Factory field, TemplateScript.Factory targetField, - boolean ignoreMissing) { + RenameProcessor( + String tag, + String description, + TemplateScript.Factory field, + TemplateScript.Factory targetField, + boolean ignoreMissing + ) { super(tag, description); this.field = field; this.targetField = targetField; @@ -93,16 +98,24 @@ public Factory(ScriptService scriptService) { } @Override - public RenameProcessor create(Map registry, String processorTag, - String description, Map config) throws Exception { + public RenameProcessor create( + Map registry, + String processorTag, + String description, + Map config + ) throws Exception { String field = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "field"); - TemplateScript.Factory fieldTemplate = ConfigurationUtils.compileTemplate(TYPE, processorTag, - "field", field, scriptService); + TemplateScript.Factory fieldTemplate = ConfigurationUtils.compileTemplate(TYPE, processorTag, "field", field, scriptService); String targetField = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "target_field"); - TemplateScript.Factory targetFieldTemplate = ConfigurationUtils.compileTemplate(TYPE, processorTag, - "target_field", targetField, scriptService); + TemplateScript.Factory targetFieldTemplate = ConfigurationUtils.compileTemplate( + TYPE, + processorTag, + "target_field", + targetField, + scriptService + ); boolean ignoreMissing = ConfigurationUtils.readBooleanProperty(TYPE, processorTag, config, "ignore_missing", false); - return new RenameProcessor(processorTag, description, fieldTemplate, targetFieldTemplate , ignoreMissing); + return new RenameProcessor(processorTag, description, fieldTemplate, targetFieldTemplate, ignoreMissing); } } } diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/ScriptProcessor.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/ScriptProcessor.java index ae5aef0dc8c15..9cb74c31eb895 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/ScriptProcessor.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/ScriptProcessor.java @@ -8,15 +8,10 @@ package org.elasticsearch.ingest.common; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.util.CollectionUtils; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.xcontent.json.JsonXContent; +import org.elasticsearch.core.Nullable; import org.elasticsearch.ingest.AbstractProcessor; import org.elasticsearch.ingest.IngestDocument; import org.elasticsearch.ingest.Processor; @@ -25,6 +20,11 @@ import org.elasticsearch.script.ScriptException; import org.elasticsearch.script.ScriptService; import org.elasticsearch.script.ScriptType; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.xcontent.json.JsonXContent; import java.io.InputStream; import java.util.Arrays; @@ -51,8 +51,13 @@ public final class ScriptProcessor extends AbstractProcessor { * @param precompiledIngestScript The {@link Script} precompiled * @param scriptService The {@link ScriptService} used to execute the script. */ - ScriptProcessor(String tag, String description, Script script, @Nullable IngestScript precompiledIngestScript, - ScriptService scriptService) { + ScriptProcessor( + String tag, + String description, + Script script, + @Nullable IngestScript precompiledIngestScript, + ScriptService scriptService + ) { super(tag, description); this.script = script; this.precompiledIngestScript = precompiledIngestScript; @@ -99,12 +104,18 @@ public Factory(ScriptService scriptService) { } @Override - public ScriptProcessor create(Map registry, String processorTag, - String description, Map config) throws Exception { - try (XContentBuilder builder = XContentBuilder.builder(JsonXContent.jsonXContent).map(config); - InputStream stream = BytesReference.bytes(builder).streamInput(); - XContentParser parser = XContentType.JSON.xContent().createParser(NamedXContentRegistry.EMPTY, - LoggingDeprecationHandler.INSTANCE, stream)) { + public ScriptProcessor create( + Map registry, + String processorTag, + String description, + Map config + ) throws Exception { + try ( + XContentBuilder builder = XContentBuilder.builder(JsonXContent.jsonXContent).map(config); + InputStream stream = BytesReference.bytes(builder).streamInput(); + XContentParser parser = XContentType.JSON.xContent() + .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream) + ) { Script script = Script.parse(parser); Arrays.asList("id", "source", "inline", "lang", "params", "options").forEach(config::remove); diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/SetProcessor.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/SetProcessor.java index b6a09162b32dd..229b796b89c75 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/SetProcessor.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/SetProcessor.java @@ -39,8 +39,15 @@ public final class SetProcessor extends AbstractProcessor { this(tag, description, field, value, copyFrom, true, false); } - SetProcessor(String tag, String description, TemplateScript.Factory field, ValueSource value, String copyFrom, boolean overrideEnabled, - boolean ignoreEmptyValue) { + SetProcessor( + String tag, + String description, + TemplateScript.Factory field, + ValueSource value, + String copyFrom, + boolean overrideEnabled, + boolean ignoreEmptyValue + ) { super(tag, description); this.overrideEnabled = overrideEnabled; this.field = field; @@ -96,8 +103,12 @@ public Factory(ScriptService scriptService) { } @Override - public SetProcessor create(Map registry, String processorTag, - String description, Map config) throws Exception { + public SetProcessor create( + Map registry, + String processorTag, + String description, + Map config + ) throws Exception { String field = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "field"); String copyFrom = ConfigurationUtils.readOptionalStringProperty(TYPE, processorTag, config, "copy_from"); String mediaType = ConfigurationUtils.readMediaTypeProperty(TYPE, processorTag, config, "media_type", "application/json"); @@ -108,8 +119,12 @@ public SetProcessor create(Map registry, String proce } else { Object value = config.remove("value"); if (value != null) { - throw newConfigurationException(TYPE, processorTag, "copy_from", - "cannot set both `copy_from` and `value` in the same processor"); + throw newConfigurationException( + TYPE, + processorTag, + "copy_from", + "cannot set both `copy_from` and `value` in the same processor" + ); } } @@ -117,14 +132,7 @@ public SetProcessor create(Map registry, String proce TemplateScript.Factory compiledTemplate = ConfigurationUtils.compileTemplate(TYPE, processorTag, "field", field, scriptService); boolean ignoreEmptyValue = ConfigurationUtils.readBooleanProperty(TYPE, processorTag, config, "ignore_empty_value", false); - return new SetProcessor( - processorTag, - description, - compiledTemplate, - valueSource, - copyFrom, - overrideEnabled, - ignoreEmptyValue); + return new SetProcessor(processorTag, description, compiledTemplate, valueSource, copyFrom, overrideEnabled, ignoreEmptyValue); } } } diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/SortProcessor.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/SortProcessor.java index 977b4ff8718e1..084a11d26a837 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/SortProcessor.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/SortProcessor.java @@ -30,7 +30,8 @@ public final class SortProcessor extends AbstractProcessor { public static final String DEFAULT_ORDER = "asc"; public enum SortOrder { - ASCENDING("asc"), DESCENDING("desc"); + ASCENDING("asc"), + DESCENDING("desc"); private final String direction; @@ -53,8 +54,7 @@ public static SortOrder fromString(String value) { } else if (value.equals(DESCENDING.toString())) { return DESCENDING; } - throw new IllegalArgumentException("Sort direction [" + value + "] not recognized." - + " Valid values are: [asc, desc]"); + throw new IllegalArgumentException("Sort direction [" + value + "] not recognized." + " Valid values are: [asc, desc]"); } } @@ -110,18 +110,18 @@ public String getType() { public static final class Factory implements Processor.Factory { @Override - public SortProcessor create(Map registry, String processorTag, - String description, Map config) throws Exception { + public SortProcessor create( + Map registry, + String processorTag, + String description, + Map config + ) throws Exception { String field = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, FIELD); String targetField = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "target_field", field); try { SortOrder direction = SortOrder.fromString( - ConfigurationUtils.readStringProperty( - TYPE, - processorTag, - config, - ORDER, - DEFAULT_ORDER)); + ConfigurationUtils.readStringProperty(TYPE, processorTag, config, ORDER, DEFAULT_ORDER) + ); return new SortProcessor(processorTag, description, field, direction, targetField); } catch (IllegalArgumentException e) { throw ConfigurationUtils.newConfigurationException(TYPE, processorTag, ORDER, e.getMessage()); @@ -129,4 +129,3 @@ public SortProcessor create(Map registry, String proc } } } - diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/SplitProcessor.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/SplitProcessor.java index 7ad49102ac6ae..05fc3bc68c2fc 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/SplitProcessor.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/SplitProcessor.java @@ -33,8 +33,15 @@ public final class SplitProcessor extends AbstractProcessor { private final boolean preserveTrailing; private final String targetField; - SplitProcessor(String tag, String description, String field, String separator, boolean ignoreMissing, boolean preserveTrailing, - String targetField) { + SplitProcessor( + String tag, + String description, + String field, + String separator, + boolean ignoreMissing, + boolean preserveTrailing, + String targetField + ) { super(tag, description); this.field = field; this.separator = separator; @@ -55,7 +62,9 @@ boolean isIgnoreMissing() { return ignoreMissing; } - boolean isPreserveTrailing() { return preserveTrailing; } + boolean isPreserveTrailing() { + return preserveTrailing; + } String getTargetField() { return targetField; @@ -85,8 +94,12 @@ public String getType() { public static class Factory implements Processor.Factory { @Override - public SplitProcessor create(Map registry, String processorTag, - String description, Map config) throws Exception { + public SplitProcessor create( + Map registry, + String processorTag, + String description, + Map config + ) throws Exception { String field = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "field"); boolean ignoreMissing = ConfigurationUtils.readBooleanProperty(TYPE, processorTag, config, "ignore_missing", false); boolean preserveTrailing = ConfigurationUtils.readBooleanProperty(TYPE, processorTag, config, "preserve_trailing", false); diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/TrimProcessor.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/TrimProcessor.java index 5883222f19a23..292b9098e7460 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/TrimProcessor.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/TrimProcessor.java @@ -39,10 +39,15 @@ public Factory() { } @Override - protected TrimProcessor newProcessor(String tag, String description, Map config, String field, - boolean ignoreMissing, String targetField) { + protected TrimProcessor newProcessor( + String tag, + String description, + Map config, + String field, + boolean ignoreMissing, + String targetField + ) { return new TrimProcessor(tag, description, field, ignoreMissing, targetField); } } } - diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/URLDecodeProcessor.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/URLDecodeProcessor.java index 8e4e79ea46fbb..b0a68ebe000a2 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/URLDecodeProcessor.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/URLDecodeProcessor.java @@ -48,8 +48,14 @@ public Factory() { } @Override - protected URLDecodeProcessor newProcessor(String tag, String description, Map config, String field, - boolean ignoreMissing, String targetField) { + protected URLDecodeProcessor newProcessor( + String tag, + String description, + Map config, + String field, + boolean ignoreMissing, + String targetField + ) { return new URLDecodeProcessor(tag, description, field, ignoreMissing, targetField); } } diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/UppercaseProcessor.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/UppercaseProcessor.java index 592c7bed63731..ec8a234b620b0 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/UppercaseProcessor.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/UppercaseProcessor.java @@ -44,8 +44,14 @@ public Factory() { } @Override - protected UppercaseProcessor newProcessor(String tag, String description, Map config, String field, - boolean ignoreMissing, String targetField) { + protected UppercaseProcessor newProcessor( + String tag, + String description, + Map config, + String field, + boolean ignoreMissing, + String targetField + ) { return new UppercaseProcessor(tag, description, field, ignoreMissing, targetField); } } diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/AbstractStringProcessorFactoryTestCase.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/AbstractStringProcessorFactoryTestCase.java index 84beb9a84b5ea..b0e74000da573 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/AbstractStringProcessorFactoryTestCase.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/AbstractStringProcessorFactoryTestCase.java @@ -85,7 +85,7 @@ public void testCreateMissingField() throws Exception { try { factory.create(null, null, null, config); fail("factory create should have failed"); - } catch(ElasticsearchParseException e) { + } catch (ElasticsearchParseException e) { assertThat(e.getMessage(), equalTo("[field] required property is missing")); } } diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/AbstractStringProcessorTestCase.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/AbstractStringProcessorTestCase.java index 4ad36e39919f8..af108feecb829 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/AbstractStringProcessorTestCase.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/AbstractStringProcessorTestCase.java @@ -107,16 +107,23 @@ public void testNonStringValue() throws Exception { IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>()); ingestDocument.setFieldValue(fieldName, randomInt()); Exception e = expectThrows(Exception.class, () -> processor.execute(ingestDocument)); - assertThat(e.getMessage(), equalTo("field [" + fieldName + - "] of type [java.lang.Integer] cannot be cast to [java.lang.String]")); + assertThat(e.getMessage(), equalTo("field [" + fieldName + "] of type [java.lang.Integer] cannot be cast to [java.lang.String]")); List fieldValueList = new ArrayList<>(); int randomValue = randomInt(); fieldValueList.add(randomValue); ingestDocument.setFieldValue(fieldName, fieldValueList); Exception exception = expectThrows(Exception.class, () -> processor.execute(ingestDocument)); - assertThat(exception.getMessage(), equalTo("value [" + randomValue + "] of type [java.lang.Integer] in list field [" + fieldName + - "] cannot be cast to [java.lang.String]")); + assertThat( + exception.getMessage(), + equalTo( + "value [" + + randomValue + + "] of type [java.lang.Integer] in list field [" + + fieldName + + "] cannot be cast to [java.lang.String]" + ) + ); } public void testNonStringValueWithIgnoreMissing() throws Exception { @@ -125,16 +132,23 @@ public void testNonStringValueWithIgnoreMissing() throws Exception { IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>()); ingestDocument.setFieldValue(fieldName, randomInt()); Exception e = expectThrows(Exception.class, () -> processor.execute(ingestDocument)); - assertThat(e.getMessage(), equalTo("field [" + fieldName + - "] of type [java.lang.Integer] cannot be cast to [java.lang.String]")); + assertThat(e.getMessage(), equalTo("field [" + fieldName + "] of type [java.lang.Integer] cannot be cast to [java.lang.String]")); List fieldValueList = new ArrayList<>(); int randomValue = randomInt(); fieldValueList.add(randomValue); ingestDocument.setFieldValue(fieldName, fieldValueList); Exception exception = expectThrows(Exception.class, () -> processor.execute(ingestDocument)); - assertThat(exception.getMessage(), equalTo("value [" + randomValue + "] of type [java.lang.Integer] in list field [" + fieldName + - "] cannot be cast to [java.lang.String]")); + assertThat( + exception.getMessage(), + equalTo( + "value [" + + randomValue + + "] of type [java.lang.Integer] in list field [" + + fieldName + + "] cannot be cast to [java.lang.String]" + ) + ); } public void testTargetField() throws Exception { diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/AppendProcessorFactoryTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/AppendProcessorFactoryTests.java index 060aad95971c8..612a11cf7216f 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/AppendProcessorFactoryTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/AppendProcessorFactoryTests.java @@ -55,7 +55,7 @@ public void testCreateNoFieldPresent() throws Exception { try { factory.create(null, null, null, config); fail("factory create should have failed"); - } catch(ElasticsearchParseException e) { + } catch (ElasticsearchParseException e) { assertThat(e.getMessage(), equalTo("[field] required property is missing")); } } @@ -66,7 +66,7 @@ public void testCreateNoValuePresent() throws Exception { try { factory.create(null, null, null, config); fail("factory create should have failed"); - } catch(ElasticsearchParseException e) { + } catch (ElasticsearchParseException e) { assertThat(e.getMessage(), equalTo("[value] required property is missing")); } } @@ -78,7 +78,7 @@ public void testCreateNullValue() throws Exception { try { factory.create(null, null, null, config); fail("factory create should have failed"); - } catch(ElasticsearchParseException e) { + } catch (ElasticsearchParseException e) { assertThat(e.getMessage(), equalTo("[value] required property is missing")); } } @@ -89,8 +89,10 @@ public void testInvalidMustacheTemplate() throws Exception { config.put("field", "{{field1}}"); config.put("value", "value1"); String processorTag = randomAlphaOfLength(10); - ElasticsearchException exception = expectThrows(ElasticsearchException.class, - () -> factory.create(null, processorTag, null, config)); + ElasticsearchException exception = expectThrows( + ElasticsearchException.class, + () -> factory.create(null, processorTag, null, config) + ); assertThat(exception.getMessage(), equalTo("java.lang.RuntimeException: could not compile script")); assertThat(exception.getMetadata("es.processor_tag").get(0), equalTo(processorTag)); } @@ -107,8 +109,10 @@ public void testMediaType() throws Exception { assertThat(appendProcessor.getTag(), equalTo(processorTag)); // invalid media type - expectedMediaType = randomValueOtherThanMany(m -> Arrays.asList(ConfigurationUtils.VALID_MEDIA_TYPES).contains(m), - () -> randomAlphaOfLengthBetween(5, 9)); + expectedMediaType = randomValueOtherThanMany( + m -> Arrays.asList(ConfigurationUtils.VALID_MEDIA_TYPES).contains(m), + () -> randomAlphaOfLengthBetween(5, 9) + ); final Map config2 = new HashMap<>(); config2.put("field", "field1"); config2.put("value", "value1"); diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/AppendProcessorTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/AppendProcessorTests.java index 96d49437133a1..b86ba39d60615 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/AppendProcessorTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/AppendProcessorTests.java @@ -212,9 +212,13 @@ public void testAppendingToListWithDuplicatesDisallowed() throws Exception { } private static Processor createAppendProcessor(String fieldName, Object fieldValue, boolean allowDuplicates) { - return new AppendProcessor(randomAlphaOfLength(10), - null, new TestTemplateService.MockTemplateScript.Factory(fieldName), - ValueSource.wrap(fieldValue, TestTemplateService.instance()), allowDuplicates); + return new AppendProcessor( + randomAlphaOfLength(10), + null, + new TestTemplateService.MockTemplateScript.Factory(fieldName), + ValueSource.wrap(fieldValue, TestTemplateService.instance()), + allowDuplicates + ); } private enum Scalar { @@ -223,27 +227,32 @@ private enum Scalar { Object randomValue() { return randomInt(); } - }, DOUBLE { + }, + DOUBLE { @Override Object randomValue() { return randomDouble(); } - }, FLOAT { + }, + FLOAT { @Override Object randomValue() { return randomFloat(); } - }, BOOLEAN { + }, + BOOLEAN { @Override Object randomValue() { return randomBoolean(); } - }, STRING { + }, + STRING { @Override Object randomValue() { return randomAlphaOfLengthBetween(1, 10); } - }, MAP { + }, + MAP { @Override Object randomValue() { int numItems = randomIntBetween(1, 10); @@ -253,7 +262,8 @@ Object randomValue() { } return map; } - }, NULL { + }, + NULL { @Override Object randomValue() { return null; diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/BytesProcessorTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/BytesProcessorTests.java index 0cb6338d28f93..1b35ea427c59d 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/BytesProcessorTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/BytesProcessorTests.java @@ -29,7 +29,7 @@ protected AbstractStringProcessor newProcessor(String field, boolean ignor @Override protected String modifyInput(String input) { - //largest value that allows all results < Long.MAX_VALUE bytes + // largest value that allows all results < Long.MAX_VALUE bytes long randomNumber = randomLongBetween(1, Long.MAX_VALUE / ByteSizeUnit.PB.toBytes(1)); ByteSizeUnit randomUnit = randomFrom(ByteSizeUnit.values()); modifiedInput = randomNumber + randomUnit.getSuffix(); @@ -51,10 +51,14 @@ public void testTooLarge() { String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, "8912pb"); Processor processor = newProcessor(fieldName, randomBoolean(), fieldName); ElasticsearchException exception = expectThrows(ElasticsearchException.class, () -> processor.execute(ingestDocument)); - assertThat(exception.getMessage(), - CoreMatchers.equalTo("failed to parse setting [Ingest Field] with value [8912pb] as a size in bytes")); - assertThat(exception.getCause().getMessage(), - CoreMatchers.containsString("Values greater than 9223372036854775807 bytes are not supported")); + assertThat( + exception.getMessage(), + CoreMatchers.equalTo("failed to parse setting [Ingest Field] with value [8912pb] as a size in bytes") + ); + assertThat( + exception.getCause().getMessage(), + CoreMatchers.containsString("Values greater than 9223372036854775807 bytes are not supported") + ); } public void testNotBytes() { @@ -62,8 +66,7 @@ public void testNotBytes() { String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, "junk"); Processor processor = newProcessor(fieldName, randomBoolean(), fieldName); ElasticsearchException exception = expectThrows(ElasticsearchException.class, () -> processor.execute(ingestDocument)); - assertThat(exception.getMessage(), - CoreMatchers.equalTo("failed to parse setting [Ingest Field] with value [junk]")); + assertThat(exception.getMessage(), CoreMatchers.equalTo("failed to parse setting [Ingest Field] with value [junk]")); } public void testMissingUnits() { @@ -71,8 +74,7 @@ public void testMissingUnits() { String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, "1"); Processor processor = newProcessor(fieldName, randomBoolean(), fieldName); ElasticsearchException exception = expectThrows(ElasticsearchException.class, () -> processor.execute(ingestDocument)); - assertThat(exception.getMessage(), - CoreMatchers.containsString("unit is missing or unrecognized")); + assertThat(exception.getMessage(), CoreMatchers.containsString("unit is missing or unrecognized")); } public void testFractional() throws Exception { @@ -81,7 +83,8 @@ public void testFractional() throws Exception { Processor processor = newProcessor(fieldName, randomBoolean(), fieldName); processor.execute(ingestDocument); assertThat(ingestDocument.getFieldValue(fieldName, expectedResultType()), equalTo(1126L)); - assertWarnings("Fractional bytes values are deprecated. Use non-fractional bytes values instead: [1.1kb] found for setting " + - "[Ingest Field]"); + assertWarnings( + "Fractional bytes values are deprecated. Use non-fractional bytes values instead: [1.1kb] found for setting " + "[Ingest Field]" + ); } } diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/CommunityIdProcessorTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/CommunityIdProcessorTests.java index ba777771b8c2e..349339943a3dc 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/CommunityIdProcessorTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/CommunityIdProcessorTests.java @@ -318,10 +318,7 @@ public void testIgnoreMissingIsFalse() throws Exception { var source = (Map) event.get("source"); source.remove("ip"); - IllegalArgumentException e = expectThrows( - IllegalArgumentException.class, - () -> testCommunityIdProcessor(event, 0, null, false) - ); + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> testCommunityIdProcessor(event, 0, null, false)); assertThat(e.getMessage(), containsString("field [ip] not present as part of path [source.ip]")); } diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/ConvertProcessorTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/ConvertProcessorTests.java index 8712b88d6438d..3771718947a5f 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/ConvertProcessorTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/ConvertProcessorTests.java @@ -91,7 +91,7 @@ public void testConvertIntError() throws Exception { try { processor.execute(ingestDocument); fail("processor execute should have failed"); - } catch(IllegalArgumentException e) { + } catch (IllegalArgumentException e) { assertThat(e.getMessage(), equalTo("unable to convert [" + value + "] to integer")); } } @@ -161,7 +161,7 @@ public void testConvertLongError() throws Exception { try { processor.execute(ingestDocument); fail("processor execute should have failed"); - } catch(IllegalArgumentException e) { + } catch (IllegalArgumentException e) { assertThat(e.getMessage(), equalTo("unable to convert [" + value + "] to long")); } } @@ -204,7 +204,7 @@ public void testConvertDoubleError() throws Exception { try { processor.execute(ingestDocument); fail("processor execute should have failed"); - } catch(IllegalArgumentException e) { + } catch (IllegalArgumentException e) { assertThat(e.getMessage(), equalTo("unable to convert [" + value + "] to double")); } } @@ -247,7 +247,7 @@ public void testConvertFloatError() throws Exception { try { processor.execute(ingestDocument); fail("processor execute should have failed"); - } catch(IllegalArgumentException e) { + } catch (IllegalArgumentException e) { assertThat(e.getMessage(), equalTo("unable to convert [" + value + "] to float")); } } @@ -293,7 +293,7 @@ public void testConvertBooleanError() throws Exception { if (randomBoolean()) { fieldValue = "string-" + randomAlphaOfLengthBetween(1, 10); } else { - //verify that only proper boolean values are supported and we are strict about it + // verify that only proper boolean values are supported and we are strict about it fieldValue = randomFrom("on", "off", "yes", "no", "0", "1"); } ingestDocument.setFieldValue(fieldName, fieldValue); @@ -302,7 +302,7 @@ public void testConvertBooleanError() throws Exception { try { processor.execute(ingestDocument); fail("processor execute should have failed"); - } catch(Exception e) { + } catch (Exception e) { assertThat(e.getMessage(), equalTo("[" + fieldValue + "] is not a boolean value, cannot convert to boolean")); } } @@ -382,7 +382,7 @@ public void testConvertString() throws Exception { IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random()); Object fieldValue; String expectedFieldValue; - switch(randomIntBetween(0, 2)) { + switch (randomIntBetween(0, 2)) { case 0: float randomFloat = randomFloat(); fieldValue = randomFloat; @@ -416,7 +416,7 @@ public void testConvertStringList() throws Exception { for (int j = 0; j < numItems; j++) { Object randomValue; String randomValueString; - switch(randomIntBetween(0, 2)) { + switch (randomIntBetween(0, 2)) { case 0: float randomFloat = randomFloat(); randomValue = randomFloat; @@ -500,7 +500,7 @@ public void testConvertNullFieldWithIgnoreMissing() throws Exception { public void testAutoConvertNotString() throws Exception { Object randomValue; - switch(randomIntBetween(0, 2)) { + switch (randomIntBetween(0, 2)) { case 0: float randomFloat = randomFloat(); randomValue = randomFloat; @@ -535,8 +535,10 @@ public void testAutoConvertStringNotMatched() throws Exception { public void testAutoConvertMatchBoolean() throws Exception { boolean randomBoolean = randomBoolean(); String booleanString = Boolean.toString(randomBoolean); - IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), - Collections.singletonMap("field", booleanString)); + IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument( + random(), + Collections.singletonMap("field", booleanString) + ); Processor processor = new ConvertProcessor(randomAlphaOfLength(10), null, "field", "field", Type.AUTO, false); processor.execute(ingestDocument); Object convertedValue = ingestDocument.getFieldValue("field", Object.class); @@ -566,7 +568,7 @@ public void testAutoConvertMatchLong() throws Exception { public void testAutoConvertDoubleNotMatched() throws Exception { double randomDouble = randomDouble(); String randomString = Double.toString(randomDouble); - float randomFloat = Float.parseFloat(randomString); + float randomFloat = Float.parseFloat(randomString); IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.singletonMap("field", randomString)); Processor processor = new ConvertProcessor(randomAlphaOfLength(10), null, "field", "field", Type.AUTO, false); processor.execute(ingestDocument); diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/CsvProcessorFactoryTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/CsvProcessorFactoryTests.java index 64ba18b50c368..c4a631d05c351 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/CsvProcessorFactoryTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/CsvProcessorFactoryTests.java @@ -33,7 +33,7 @@ public void testProcessorIsCreated() { CsvProcessor csv = factory.create(null, "csv", null, properties); assertThat(csv, notNullValue()); assertThat(csv.field, equalTo("field")); - assertThat(csv.headers, equalTo(new String[]{"target"})); + assertThat(csv.headers, equalTo(new String[] { "target" })); assertThat(csv.quote, equalTo('|')); assertThat(csv.separator, equalTo('/')); assertThat(csv.emptyValue, equalTo("empty")); diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/CsvProcessorTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/CsvProcessorTests.java index 99396a37bbdb2..e111d5f18d210 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/CsvProcessorTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/CsvProcessorTests.java @@ -10,6 +10,7 @@ import com.carrotsearch.randomizedtesting.annotations.Name; import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; + import org.elasticsearch.ingest.IngestDocument; import org.elasticsearch.ingest.RandomDocumentPicks; import org.elasticsearch.test.ESTestCase; @@ -23,12 +24,11 @@ public class CsvProcessorTests extends ESTestCase { - private static final Character[] SEPARATORS = new Character[]{',', ';', '|', '.', '\t'}; - private static final String[] QUOTES = new String[]{"'", "\"", ""}; + private static final Character[] SEPARATORS = new Character[] { ',', ';', '|', '.', '\t' }; + private static final String[] QUOTES = new String[] { "'", "\"", "" }; private final String quote; private final char separator; - public CsvProcessorTests(@Name("quote") String quote, @Name("separator") char separator) { this.quote = quote; this.separator = separator; @@ -39,7 +39,7 @@ public static Iterable parameters() { LinkedList list = new LinkedList<>(); for (Character separator : SEPARATORS) { for (String quote : QUOTES) { - list.add(new Object[]{quote, separator}); + list.add(new Object[] { quote, separator }); } } return list; @@ -149,7 +149,7 @@ public void testLessHeadersThanFields() { } public void testSingleField() { - String[] headers = new String[]{randomAlphaOfLengthBetween(5, 10)}; + String[] headers = new String[] { randomAlphaOfLengthBetween(5, 10) }; String value = randomAlphaOfLengthBetween(5, 10); String csv = quote + value + quote; @@ -162,8 +162,10 @@ public void testEscapedQuote() { int numItems = randomIntBetween(2, 10); Map items = new LinkedHashMap<>(); for (int i = 0; i < numItems; i++) { - items.put(randomAlphaOfLengthBetween(5, 10), randomAlphaOfLengthBetween(5, 10) + quote + quote + randomAlphaOfLengthBetween(5 - , 10) + quote + quote); + items.put( + randomAlphaOfLengthBetween(5, 10), + randomAlphaOfLengthBetween(5, 10) + quote + quote + randomAlphaOfLengthBetween(5, 10) + quote + quote + ); } String[] headers = items.keySet().toArray(new String[numItems]); String csv = items.values().stream().map(v -> quote + v + quote).collect(Collectors.joining(separator + "")); @@ -178,16 +180,17 @@ public void testQuotedStrings() { int numItems = randomIntBetween(2, 10); Map items = new LinkedHashMap<>(); for (int i = 0; i < numItems; i++) { - items.put(randomAlphaOfLengthBetween(5, 10), - separator + randomAlphaOfLengthBetween(5, 10) + separator + "\n\r" + randomAlphaOfLengthBetween(5, 10)); + items.put( + randomAlphaOfLengthBetween(5, 10), + separator + randomAlphaOfLengthBetween(5, 10) + separator + "\n\r" + randomAlphaOfLengthBetween(5, 10) + ); } String[] headers = items.keySet().toArray(new String[numItems]); String csv = items.values().stream().map(v -> quote + v + quote).collect(Collectors.joining(separator + "")); IngestDocument ingestDocument = processDocument(headers, csv); - items.forEach((key, value) -> assertEquals(value.replace(quote + quote, quote), ingestDocument.getFieldValue(key, - String.class))); + items.forEach((key, value) -> assertEquals(value.replace(quote + quote, quote), ingestDocument.getFieldValue(key, String.class))); } public void testEmptyFields() { @@ -197,32 +200,42 @@ public void testEmptyFields() { items.put(randomAlphaOfLengthBetween(5, 10), randomAlphaOfLengthBetween(5, 10)); } String[] headers = items.keySet().toArray(new String[numItems]); - String csv = - items.values().stream().map(v -> quote + v + quote).limit(numItems - 1).skip(3).collect(Collectors.joining(separator + "")); - - IngestDocument ingestDocument = processDocument(headers, - "" + separator + "" + separator + "" + separator + csv + separator + separator + - "abc"); + String csv = items.values() + .stream() + .map(v -> quote + v + quote) + .limit(numItems - 1) + .skip(3) + .collect(Collectors.joining(separator + "")); + + IngestDocument ingestDocument = processDocument( + headers, + "" + separator + "" + separator + "" + separator + csv + separator + separator + "abc" + ); items.keySet().stream().limit(3).forEach(key -> assertFalse(ingestDocument.hasField(key))); - items.entrySet().stream().limit(numItems - 1).skip(3).forEach(e -> assertEquals(e.getValue(), - ingestDocument.getFieldValue(e.getKey(), String.class))); + items.entrySet() + .stream() + .limit(numItems - 1) + .skip(3) + .forEach(e -> assertEquals(e.getValue(), ingestDocument.getFieldValue(e.getKey(), String.class))); items.keySet().stream().skip(numItems - 1).forEach(key -> assertFalse(ingestDocument.hasField(key))); } public void testWrongStrings() throws Exception { assumeTrue("single run only", quote.isEmpty()); - expectThrows(IllegalArgumentException.class, () -> processDocument(new String[]{"a"}, "abc\"abc")); - expectThrows(IllegalArgumentException.class, () -> processDocument(new String[]{"a"}, "\"abc\"asd")); - expectThrows(IllegalArgumentException.class, () -> processDocument(new String[]{"a"}, "\"abcasd")); - expectThrows(IllegalArgumentException.class, () -> processDocument(new String[]{"a"}, "abc\nabc")); - expectThrows(IllegalArgumentException.class, () -> processDocument(new String[]{"a"}, "abc\rabc")); + expectThrows(IllegalArgumentException.class, () -> processDocument(new String[] { "a" }, "abc\"abc")); + expectThrows(IllegalArgumentException.class, () -> processDocument(new String[] { "a" }, "\"abc\"asd")); + expectThrows(IllegalArgumentException.class, () -> processDocument(new String[] { "a" }, "\"abcasd")); + expectThrows(IllegalArgumentException.class, () -> processDocument(new String[] { "a" }, "abc\nabc")); + expectThrows(IllegalArgumentException.class, () -> processDocument(new String[] { "a" }, "abc\rabc")); } public void testQuotedWhitespaces() { assumeFalse("quote needed", quote.isEmpty()); - IngestDocument document = processDocument(new String[]{"a", "b", "c", "d"}, - " abc " + separator + " def" + separator + "ghi " + separator + " " + quote + " ooo " + quote); + IngestDocument document = processDocument( + new String[] { "a", "b", "c", "d" }, + " abc " + separator + " def" + separator + "ghi " + separator + " " + quote + " ooo " + quote + ); assertEquals("abc", document.getFieldValue("a", String.class)); assertEquals("def", document.getFieldValue("b", String.class)); assertEquals("ghi", document.getFieldValue("c", String.class)); @@ -231,9 +244,27 @@ public void testQuotedWhitespaces() { public void testUntrimmed() { assumeFalse("quote needed", quote.isEmpty()); - IngestDocument document = processDocument(new String[]{"a", "b", "c", "d", "e", "f"}, - " abc " + separator + " def" + separator + "ghi " + separator + " " - + quote + "ooo" + quote + " " + separator + " " + quote + "jjj" + quote + " ", false); + IngestDocument document = processDocument( + new String[] { "a", "b", "c", "d", "e", "f" }, + " abc " + + separator + + " def" + + separator + + "ghi " + + separator + + " " + + quote + + "ooo" + + quote + + " " + + separator + + " " + + quote + + "jjj" + + quote + + " ", + false + ); assertEquals(" abc ", document.getFieldValue("a", String.class)); assertEquals(" def", document.getFieldValue("b", String.class)); assertEquals("ghi ", document.getFieldValue("c", String.class)); @@ -249,10 +280,29 @@ public void testIgnoreMissing() { if (ingestDocument.hasField(fieldName)) { ingestDocument.removeField(fieldName); } - CsvProcessor processor = new CsvProcessor(randomAlphaOfLength(5), null, fieldName, new String[]{"a"}, false, ',', '"', true, null); + CsvProcessor processor = new CsvProcessor( + randomAlphaOfLength(5), + null, + fieldName, + new String[] { "a" }, + false, + ',', + '"', + true, + null + ); processor.execute(ingestDocument); - CsvProcessor processor2 = new CsvProcessor(randomAlphaOfLength(5), null, fieldName, new String[]{"a"}, false, - ',', '"', false, null); + CsvProcessor processor2 = new CsvProcessor( + randomAlphaOfLength(5), + null, + fieldName, + new String[] { "a" }, + false, + ',', + '"', + false, + null + ); expectThrows(IllegalArgumentException.class, () -> processor2.execute(ingestDocument)); } @@ -284,8 +334,17 @@ private IngestDocument processDocument(String[] headers, String csv, boolean tri ingestDocument.setFieldValue(fieldName, csv); char quoteChar = quote.isEmpty() ? '"' : quote.charAt(0); - CsvProcessor processor = new CsvProcessor(randomAlphaOfLength(5), null, fieldName, headers, trim, separator, quoteChar, false, - emptyValue); + CsvProcessor processor = new CsvProcessor( + randomAlphaOfLength(5), + null, + fieldName, + headers, + trim, + separator, + quoteChar, + false, + emptyValue + ); processor.execute(ingestDocument); diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DateFormatTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DateFormatTests.java index d353364ee0ff7..2d3b3b9a4a9df 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DateFormatTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DateFormatTests.java @@ -25,25 +25,37 @@ public class DateFormatTests extends ESTestCase { public void testParseJava() { - Function javaFunction = DateFormat.Java.getFunction("MMM dd HH:mm:ss Z", - ZoneOffset.ofHours(-8), Locale.ENGLISH); - assertThat(javaFunction.apply("Nov 24 01:29:01 -0800").toInstant() - .atZone(ZoneId.of("GMT-8")) - .format(DateTimeFormatter.ofPattern("MM dd HH:mm:ss", Locale.ENGLISH)), - equalTo("11 24 01:29:01")); + Function javaFunction = DateFormat.Java.getFunction( + "MMM dd HH:mm:ss Z", + ZoneOffset.ofHours(-8), + Locale.ENGLISH + ); + assertThat( + javaFunction.apply("Nov 24 01:29:01 -0800") + .toInstant() + .atZone(ZoneId.of("GMT-8")) + .format(DateTimeFormatter.ofPattern("MM dd HH:mm:ss", Locale.ENGLISH)), + equalTo("11 24 01:29:01") + ); } public void testParseYearOfEraJavaWithTimeZone() { - Function javaFunction = DateFormat.Java.getFunction("yyyy-MM-dd'T'HH:mm:ss.SSSZZ", - ZoneOffset.UTC, Locale.ROOT); + Function javaFunction = DateFormat.Java.getFunction( + "yyyy-MM-dd'T'HH:mm:ss.SSSZZ", + ZoneOffset.UTC, + Locale.ROOT + ); ZonedDateTime datetime = javaFunction.apply("2018-02-05T13:44:56.657+0100"); String expectedDateTime = DateFormatter.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX").withZone(ZoneOffset.UTC).format(datetime); assertThat(expectedDateTime, is("2018-02-05T12:44:56.657Z")); } public void testParseYearJavaWithTimeZone() { - Function javaFunction = DateFormat.Java.getFunction("uuuu-MM-dd'T'HH:mm:ss.SSSZZ", - ZoneOffset.UTC, Locale.ROOT); + Function javaFunction = DateFormat.Java.getFunction( + "uuuu-MM-dd'T'HH:mm:ss.SSSZZ", + ZoneOffset.UTC, + Locale.ROOT + ); ZonedDateTime datetime = javaFunction.apply("2018-02-05T13:44:56.657+0100"); String expectedDateTime = DateFormatter.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX").withZone(ZoneOffset.UTC).format(datetime); assertThat(expectedDateTime, is("2018-02-05T12:44:56.657Z")); @@ -63,7 +75,7 @@ public void testParseWeekBasedYearAndWeek() { ZoneId timezone = DateUtils.of("Europe/Amsterdam"); Function javaFunction = DateFormat.Java.getFunction(format, timezone, Locale.ROOT); ZonedDateTime dateTime = javaFunction.apply("2020-33"); - assertThat(dateTime, equalTo(ZonedDateTime.of(2020,8,10,0,0,0,0,timezone))); + assertThat(dateTime, equalTo(ZonedDateTime.of(2020, 8, 10, 0, 0, 0, 0, timezone))); } public void testParseWeekBasedYear() { @@ -71,7 +83,7 @@ public void testParseWeekBasedYear() { ZoneId timezone = DateUtils.of("Europe/Amsterdam"); Function javaFunction = DateFormat.Java.getFunction(format, timezone, Locale.ROOT); ZonedDateTime dateTime = javaFunction.apply("2019"); - assertThat(dateTime, equalTo(ZonedDateTime.of(2018,12,31,0,0,0,0,timezone))); + assertThat(dateTime, equalTo(ZonedDateTime.of(2018, 12, 31, 0, 0, 0, 0, timezone))); } public void testParseWeekBasedWithLocale() { @@ -79,8 +91,8 @@ public void testParseWeekBasedWithLocale() { ZoneId timezone = DateUtils.of("Europe/Amsterdam"); Function javaFunction = DateFormat.Java.getFunction(format, timezone, Locale.US); ZonedDateTime dateTime = javaFunction.apply("2020-33"); - //33rd week of 2020 starts on 9th August 2020 as per US locale - assertThat(dateTime, equalTo(ZonedDateTime.of(2020,8,9,0,0,0,0,timezone))); + // 33rd week of 2020 starts on 9th August 2020 as per US locale + assertThat(dateTime, equalTo(ZonedDateTime.of(2020, 8, 9, 0, 0, 0, 0, timezone))); } public void testNoTimezoneOnPatternAndOverride() { @@ -122,32 +134,43 @@ public void testDefaultHourDefaultedToTimezoneOverride() { } public void testParseUnixMs() { - assertThat(DateFormat.UnixMs.getFunction(null, ZoneOffset.UTC, null).apply("1000500").toInstant().toEpochMilli(), - equalTo(1000500L)); + assertThat( + DateFormat.UnixMs.getFunction(null, ZoneOffset.UTC, null).apply("1000500").toInstant().toEpochMilli(), + equalTo(1000500L) + ); } public void testParseUnix() { - assertThat(DateFormat.Unix.getFunction(null, ZoneOffset.UTC, null).apply("1000.5").toInstant().toEpochMilli(), - equalTo(1000500L)); + assertThat(DateFormat.Unix.getFunction(null, ZoneOffset.UTC, null).apply("1000.5").toInstant().toEpochMilli(), equalTo(1000500L)); } public void testParseUnixWithMsPrecision() { - assertThat(DateFormat.Unix.getFunction(null, ZoneOffset.UTC, null).apply("1495718015").toInstant().toEpochMilli(), - equalTo(1495718015000L)); + assertThat( + DateFormat.Unix.getFunction(null, ZoneOffset.UTC, null).apply("1495718015").toInstant().toEpochMilli(), + equalTo(1495718015000L) + ); } public void testParseISO8601() { - assertThat(DateFormat.Iso8601.getFunction(null, ZoneOffset.UTC, null).apply("2001-01-01T00:00:00-0800") - .toInstant().toEpochMilli(), equalTo(978336000000L)); - assertThat(DateFormat.Iso8601.getFunction(null, ZoneOffset.UTC, null).apply("2001-01-01T00:00:00-0800").toString(), - equalTo("2001-01-01T08:00Z")); + assertThat( + DateFormat.Iso8601.getFunction(null, ZoneOffset.UTC, null).apply("2001-01-01T00:00:00-0800").toInstant().toEpochMilli(), + equalTo(978336000000L) + ); + assertThat( + DateFormat.Iso8601.getFunction(null, ZoneOffset.UTC, null).apply("2001-01-01T00:00:00-0800").toString(), + equalTo("2001-01-01T08:00Z") + ); } public void testParseWhenZoneNotPresentInText() { - assertThat(DateFormat.Iso8601.getFunction(null, ZoneOffset.of("+0100"), null).apply("2001-01-01T00:00:00") - .toInstant().toEpochMilli(), equalTo(978303600000L)); - assertThat(DateFormat.Iso8601.getFunction(null, ZoneOffset.of("+0100"), null).apply("2001-01-01T00:00:00").toString(), - equalTo("2001-01-01T00:00+01:00")); + assertThat( + DateFormat.Iso8601.getFunction(null, ZoneOffset.of("+0100"), null).apply("2001-01-01T00:00:00").toInstant().toEpochMilli(), + equalTo(978303600000L) + ); + assertThat( + DateFormat.Iso8601.getFunction(null, ZoneOffset.of("+0100"), null).apply("2001-01-01T00:00:00").toString(), + equalTo("2001-01-01T00:00+01:00") + ); } public void testParseISO8601Failure() { @@ -155,16 +178,18 @@ public void testParseISO8601Failure() { try { function.apply("2001-01-0:00-0800"); fail("parse should have failed"); - } catch(IllegalArgumentException e) { - //all good + } catch (IllegalArgumentException e) { + // all good } } public void testTAI64NParse() { String input = "4000000050d506482dbdf024"; String expected = "2012-12-22T03:00:46.767+02:00"; - assertThat(DateFormat.Tai64n.getFunction(null, ZoneOffset.ofHours(2), null) - .apply((randomBoolean() ? "@" : "") + input).toString(), equalTo(expected)); + assertThat( + DateFormat.Tai64n.getFunction(null, ZoneOffset.ofHours(2), null).apply((randomBoolean() ? "@" : "") + input).toString(), + equalTo(expected) + ); } public void testFromString() { diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DateIndexNameProcessorTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DateIndexNameProcessorTests.java index 3e790cf0da5e4..a8c0de2cfd69d 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DateIndexNameProcessorTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DateIndexNameProcessorTests.java @@ -26,45 +26,78 @@ public class DateIndexNameProcessorTests extends ESTestCase { public void testJavaPattern() throws Exception { Function function = DateFormat.Java.getFunction("yyyy-MM-dd'T'HH:mm:ss.SSSXX", ZoneOffset.UTC, Locale.ROOT); - DateIndexNameProcessor processor = createProcessor("_field", Collections.singletonList(function), - ZoneOffset.UTC, "events-", "y", "yyyyMMdd"); - IngestDocument document = new IngestDocument("_index", "_id", null, null, null, - Collections.singletonMap("_field", "2016-04-25T12:24:20.101Z")); + DateIndexNameProcessor processor = createProcessor( + "_field", + Collections.singletonList(function), + ZoneOffset.UTC, + "events-", + "y", + "yyyyMMdd" + ); + IngestDocument document = new IngestDocument( + "_index", + "_id", + null, + null, + null, + Collections.singletonMap("_field", "2016-04-25T12:24:20.101Z") + ); processor.execute(document); assertThat(document.getSourceAndMetadata().get("_index"), equalTo("")); } - public void testTAI64N()throws Exception { + public void testTAI64N() throws Exception { Function function = DateFormat.Tai64n.getFunction(null, ZoneOffset.UTC, null); - DateIndexNameProcessor dateProcessor = createProcessor("_field", Collections.singletonList(function), - ZoneOffset.UTC, "events-", "m", "yyyyMMdd"); - IngestDocument document = new IngestDocument("_index", "_id", null, null, null, - Collections.singletonMap("_field", (randomBoolean() ? "@" : "") + "4000000050d506482dbdf024")); + DateIndexNameProcessor dateProcessor = createProcessor( + "_field", + Collections.singletonList(function), + ZoneOffset.UTC, + "events-", + "m", + "yyyyMMdd" + ); + IngestDocument document = new IngestDocument( + "_index", + "_id", + null, + null, + null, + Collections.singletonMap("_field", (randomBoolean() ? "@" : "") + "4000000050d506482dbdf024") + ); dateProcessor.execute(document); assertThat(document.getSourceAndMetadata().get("_index"), equalTo("")); } - public void testUnixMs()throws Exception { + public void testUnixMs() throws Exception { Function function = DateFormat.UnixMs.getFunction(null, ZoneOffset.UTC, null); - DateIndexNameProcessor dateProcessor = createProcessor("_field", Collections.singletonList(function), - ZoneOffset.UTC, "events-", "m", "yyyyMMdd"); - IngestDocument document = new IngestDocument("_index", "_id", null, null, null, - Collections.singletonMap("_field", "1000500")); + DateIndexNameProcessor dateProcessor = createProcessor( + "_field", + Collections.singletonList(function), + ZoneOffset.UTC, + "events-", + "m", + "yyyyMMdd" + ); + IngestDocument document = new IngestDocument("_index", "_id", null, null, null, Collections.singletonMap("_field", "1000500")); dateProcessor.execute(document); assertThat(document.getSourceAndMetadata().get("_index"), equalTo("")); - document = new IngestDocument("_index", "_id", null, null, null, - Collections.singletonMap("_field", 1000500L)); + document = new IngestDocument("_index", "_id", null, null, null, Collections.singletonMap("_field", 1000500L)); dateProcessor.execute(document); assertThat(document.getSourceAndMetadata().get("_index"), equalTo("")); } - public void testUnix()throws Exception { + public void testUnix() throws Exception { Function function = DateFormat.Unix.getFunction(null, ZoneOffset.UTC, null); - DateIndexNameProcessor dateProcessor = createProcessor("_field", Collections.singletonList(function), - ZoneOffset.UTC, "events-", "m", "yyyyMMdd"); - IngestDocument document = new IngestDocument("_index", "_id", null, null, null, - Collections.singletonMap("_field", "1000.5")); + DateIndexNameProcessor dateProcessor = createProcessor( + "_field", + Collections.singletonList(function), + ZoneOffset.UTC, + "events-", + "m", + "yyyyMMdd" + ); + IngestDocument document = new IngestDocument("_index", "_id", null, null, null, Collections.singletonMap("_field", "1000.5")); dateProcessor.execute(document); assertThat(document.getSourceAndMetadata().get("_index"), equalTo("")); } @@ -76,23 +109,48 @@ public void testTemplatedFields() throws Exception { String date = Integer.toString(randomInt()); Function dateTimeFunction = DateFormat.Unix.getFunction(null, ZoneOffset.UTC, null); - DateIndexNameProcessor dateProcessor = createProcessor("_field", - Collections.singletonList(dateTimeFunction), ZoneOffset.UTC, indexNamePrefix, - dateRounding, indexNameFormat); + DateIndexNameProcessor dateProcessor = createProcessor( + "_field", + Collections.singletonList(dateTimeFunction), + ZoneOffset.UTC, + indexNamePrefix, + dateRounding, + indexNameFormat + ); - IngestDocument document = new IngestDocument("_index", "_id", null, null, null, - Collections.singletonMap("_field", date)); + IngestDocument document = new IngestDocument("_index", "_id", null, null, null, Collections.singletonMap("_field", date)); dateProcessor.execute(document); - assertThat(document.getSourceAndMetadata().get("_index"), - equalTo("<"+indexNamePrefix+"{" + DateFormatter.forPattern(indexNameFormat) - .format(dateTimeFunction.apply(date))+"||/"+dateRounding+"{"+indexNameFormat+"|UTC}}>")); + assertThat( + document.getSourceAndMetadata().get("_index"), + equalTo( + "<" + + indexNamePrefix + + "{" + + DateFormatter.forPattern(indexNameFormat).format(dateTimeFunction.apply(date)) + + "||/" + + dateRounding + + "{" + + indexNameFormat + + "|UTC}}>" + ) + ); } - private DateIndexNameProcessor createProcessor(String field, List> dateFormats, - ZoneId timezone, String indexNamePrefix, String dateRounding, - String indexNameFormat) { - return new DateIndexNameProcessor(randomAlphaOfLength(10), null, field, dateFormats, timezone, + private DateIndexNameProcessor createProcessor( + String field, + List> dateFormats, + ZoneId timezone, + String indexNamePrefix, + String dateRounding, + String indexNameFormat + ) { + return new DateIndexNameProcessor( + randomAlphaOfLength(10), + null, + field, + dateFormats, + timezone, new TestTemplateService.MockTemplateScript.Factory(indexNamePrefix), new TestTemplateService.MockTemplateScript.Factory(dateRounding), new TestTemplateService.MockTemplateScript.Factory(indexNameFormat) diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DateProcessorFactoryTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DateProcessorFactoryTests.java index e3deddad5fa92..5245a336acf41 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DateProcessorFactoryTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DateProcessorFactoryTests.java @@ -56,7 +56,7 @@ public void testMatchFieldIsMandatory() throws Exception { try { factory.create(null, null, null, config); fail("processor creation should have failed"); - } catch(ElasticsearchParseException e) { + } catch (ElasticsearchParseException e) { assertThat(e.getMessage(), containsString("[field] required property is missing")); } } @@ -71,7 +71,7 @@ public void testMatchFormatsIsMandatory() throws Exception { try { factory.create(null, null, null, config); fail("processor creation should have failed"); - } catch(ElasticsearchParseException e) { + } catch (ElasticsearchParseException e) { assertThat(e.getMessage(), containsString("[formats] required property is missing")); } } @@ -119,7 +119,7 @@ public void testParseMatchFormatsFailure() throws Exception { try { factory.create(null, null, null, config); fail("processor creation should have failed"); - } catch(ElasticsearchParseException e) { + } catch (ElasticsearchParseException e) { assertThat(e.getMessage(), containsString("[formats] property isn't a list, but of type [java.lang.String]")); } } diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DateProcessorTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DateProcessorTests.java index 5b1275ef29c43..2beba89adfd1a 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DateProcessorTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DateProcessorTests.java @@ -40,9 +40,15 @@ private TemplateScript.Factory templatize(ZoneId timezone) { } public void testJavaPattern() { - DateProcessor dateProcessor = new DateProcessor(randomAlphaOfLength(10), - null, templatize(ZoneId.of("Europe/Amsterdam")), templatize(Locale.ENGLISH), - "date_as_string", Collections.singletonList("yyyy dd MM HH:mm:ss"), "date_as_date"); + DateProcessor dateProcessor = new DateProcessor( + randomAlphaOfLength(10), + null, + templatize(ZoneId.of("Europe/Amsterdam")), + templatize(Locale.ENGLISH), + "date_as_string", + Collections.singletonList("yyyy dd MM HH:mm:ss"), + "date_as_date" + ); Map document = new HashMap<>(); document.put("date_as_string", "2010 12 06 11:05:15"); IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document); @@ -55,9 +61,15 @@ public void testJavaPatternMultipleFormats() { matchFormats.add("yyyy dd MM"); matchFormats.add("dd/MM/yyyy"); matchFormats.add("dd-MM-yyyy"); - DateProcessor dateProcessor = new DateProcessor(randomAlphaOfLength(10), - null, templatize(ZoneId.of("Europe/Amsterdam")), templatize(Locale.ENGLISH), - "date_as_string", matchFormats, "date_as_date"); + DateProcessor dateProcessor = new DateProcessor( + randomAlphaOfLength(10), + null, + templatize(ZoneId.of("Europe/Amsterdam")), + templatize(Locale.ENGLISH), + "date_as_string", + matchFormats, + "date_as_date" + ); Map document = new HashMap<>(); document.put("date_as_string", "2010 12 06"); @@ -83,15 +95,21 @@ null, templatize(ZoneId.of("Europe/Amsterdam")), templatize(Locale.ENGLISH), try { dateProcessor.execute(ingestDocument); fail("processor should have failed due to not supported date format"); - } catch(IllegalArgumentException e) { + } catch (IllegalArgumentException e) { assertThat(e.getMessage(), containsString("unable to parse date [2010]")); } } public void testJavaPatternNoTimezone() { - DateProcessor dateProcessor = new DateProcessor(randomAlphaOfLength(10), - null, null, null, - "date_as_string", Arrays.asList("yyyy dd MM HH:mm:ss XXX"), "date_as_date"); + DateProcessor dateProcessor = new DateProcessor( + randomAlphaOfLength(10), + null, + null, + null, + "date_as_string", + Arrays.asList("yyyy dd MM HH:mm:ss XXX"), + "date_as_date" + ); Map document = new HashMap<>(); document.put("date_as_string", "2010 12 06 00:00:00 -02:00"); @@ -102,14 +120,20 @@ public void testJavaPatternNoTimezone() { public void testInvalidJavaPattern() { try { - DateProcessor processor = new DateProcessor(randomAlphaOfLength(10), - null, templatize(ZoneOffset.UTC), templatize(randomLocale(random())), - "date_as_string", Collections.singletonList("invalid pattern"), "date_as_date"); + DateProcessor processor = new DateProcessor( + randomAlphaOfLength(10), + null, + templatize(ZoneOffset.UTC), + templatize(randomLocale(random())), + "date_as_string", + Collections.singletonList("invalid pattern"), + "date_as_date" + ); Map document = new HashMap<>(); document.put("date_as_string", "2010"); processor.execute(RandomDocumentPicks.randomIngestDocument(random(), document)); fail("date processor execution should have failed"); - } catch(IllegalArgumentException e) { + } catch (IllegalArgumentException e) { assertThat(e.getMessage(), equalTo("unable to parse date [2010]")); assertThat(e.getCause().getMessage(), equalTo("Invalid format: [invalid pattern]: Unknown pattern letter: i")); } @@ -117,9 +141,15 @@ null, templatize(ZoneOffset.UTC), templatize(randomLocale(random())), public void testJavaPatternLocale() { assumeFalse("Can't run in a FIPS JVM, Joda parse date error", inFipsJvm()); - DateProcessor dateProcessor = new DateProcessor(randomAlphaOfLength(10), - null, templatize(ZoneId.of("Europe/Amsterdam")), templatize(Locale.ITALIAN), - "date_as_string", Collections.singletonList("yyyy dd MMMM"), "date_as_date"); + DateProcessor dateProcessor = new DateProcessor( + randomAlphaOfLength(10), + null, + templatize(ZoneId.of("Europe/Amsterdam")), + templatize(Locale.ITALIAN), + "date_as_string", + Collections.singletonList("yyyy dd MMMM"), + "date_as_date" + ); Map document = new HashMap<>(); document.put("date_as_string", "2010 12 giugno"); IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document); @@ -129,9 +159,15 @@ null, templatize(ZoneId.of("Europe/Amsterdam")), templatize(Locale.ITALIAN), public void testJavaPatternEnglishLocale() { // Since testJavaPatternLocale is muted in FIPS mode, test that we can correctly parse dates in english - DateProcessor dateProcessor = new DateProcessor(randomAlphaOfLength(10), - null, templatize(ZoneId.of("Europe/Amsterdam")), templatize(Locale.ENGLISH), - "date_as_string", Collections.singletonList("yyyy dd MMMM"), "date_as_date"); + DateProcessor dateProcessor = new DateProcessor( + randomAlphaOfLength(10), + null, + templatize(ZoneId.of("Europe/Amsterdam")), + templatize(Locale.ENGLISH), + "date_as_string", + Collections.singletonList("yyyy dd MMMM"), + "date_as_date" + ); Map document = new HashMap<>(); document.put("date_as_string", "2010 12 June"); IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document); @@ -141,21 +177,35 @@ null, templatize(ZoneId.of("Europe/Amsterdam")), templatize(Locale.ENGLISH), public void testJavaPatternDefaultYear() { String format = randomFrom("dd/MM", "8dd/MM"); - DateProcessor dateProcessor = new DateProcessor(randomAlphaOfLength(10), - null, templatize(ZoneId.of("Europe/Amsterdam")), templatize(Locale.ENGLISH), - "date_as_string", Collections.singletonList(format), "date_as_date"); + DateProcessor dateProcessor = new DateProcessor( + randomAlphaOfLength(10), + null, + templatize(ZoneId.of("Europe/Amsterdam")), + templatize(Locale.ENGLISH), + "date_as_string", + Collections.singletonList(format), + "date_as_date" + ); Map document = new HashMap<>(); document.put("date_as_string", "12/06"); IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document); dateProcessor.execute(ingestDocument); - assertThat(ingestDocument.getFieldValue("date_as_date", String.class), - equalTo(ZonedDateTime.now().getYear() + "-06-12T00:00:00.000+02:00")); + assertThat( + ingestDocument.getFieldValue("date_as_date", String.class), + equalTo(ZonedDateTime.now().getYear() + "-06-12T00:00:00.000+02:00") + ); } public void testTAI64N() { - DateProcessor dateProcessor = new DateProcessor(randomAlphaOfLength(10), null, templatize(ZoneOffset.ofHours(2)), + DateProcessor dateProcessor = new DateProcessor( + randomAlphaOfLength(10), + null, + templatize(ZoneOffset.ofHours(2)), templatize(randomLocale(random())), - "date_as_string", Collections.singletonList("TAI64N"), "date_as_date"); + "date_as_string", + Collections.singletonList("TAI64N"), + "date_as_date" + ); Map document = new HashMap<>(); String dateAsString = (randomBoolean() ? "@" : "") + "4000000050d506482dbdf024"; document.put("date_as_string", dateAsString); @@ -165,8 +215,15 @@ public void testTAI64N() { } public void testUnixMs() { - DateProcessor dateProcessor = new DateProcessor(randomAlphaOfLength(10), null, templatize(ZoneOffset.UTC), - templatize(randomLocale(random())), "date_as_string", Collections.singletonList("UNIX_MS"), "date_as_date"); + DateProcessor dateProcessor = new DateProcessor( + randomAlphaOfLength(10), + null, + templatize(ZoneOffset.UTC), + templatize(randomLocale(random())), + "date_as_string", + Collections.singletonList("UNIX_MS"), + "date_as_date" + ); Map document = new HashMap<>(); document.put("date_as_string", "1000500"); IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document); @@ -181,9 +238,15 @@ public void testUnixMs() { } public void testUnix() { - DateProcessor dateProcessor = new DateProcessor(randomAlphaOfLength(10), null, templatize(ZoneOffset.UTC), + DateProcessor dateProcessor = new DateProcessor( + randomAlphaOfLength(10), + null, + templatize(ZoneOffset.UTC), templatize(randomLocale(random())), - "date_as_string", Collections.singletonList("UNIX"), "date_as_date"); + "date_as_string", + Collections.singletonList("UNIX"), + "date_as_date" + ); Map document = new HashMap<>(); document.put("date_as_string", "1000.5"); IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document); @@ -192,33 +255,57 @@ public void testUnix() { } public void testInvalidTimezone() { - DateProcessor processor = new DateProcessor(randomAlphaOfLength(10), - null, new TestTemplateService.MockTemplateScript.Factory("invalid_timezone"), templatize(randomLocale(random())), - "date_as_string", Collections.singletonList("yyyy"), "date_as_date"); + DateProcessor processor = new DateProcessor( + randomAlphaOfLength(10), + null, + new TestTemplateService.MockTemplateScript.Factory("invalid_timezone"), + templatize(randomLocale(random())), + "date_as_string", + Collections.singletonList("yyyy"), + "date_as_date" + ); Map document = new HashMap<>(); document.put("date_as_string", "2010"); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> processor.execute(RandomDocumentPicks.randomIngestDocument(random(), document))); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> processor.execute(RandomDocumentPicks.randomIngestDocument(random(), document)) + ); assertThat(e.getMessage(), equalTo("unable to parse date [2010]")); assertThat(e.getCause().getMessage(), equalTo("Unknown time-zone ID: invalid_timezone")); } public void testInvalidLocale() { - DateProcessor processor = new DateProcessor(randomAlphaOfLength(10), - null, templatize(ZoneOffset.UTC), new TestTemplateService.MockTemplateScript.Factory("invalid_locale"), - "date_as_string", Collections.singletonList("yyyy"), "date_as_date"); + DateProcessor processor = new DateProcessor( + randomAlphaOfLength(10), + null, + templatize(ZoneOffset.UTC), + new TestTemplateService.MockTemplateScript.Factory("invalid_locale"), + "date_as_string", + Collections.singletonList("yyyy"), + "date_as_date" + ); Map document = new HashMap<>(); document.put("date_as_string", "2010"); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> processor.execute(RandomDocumentPicks.randomIngestDocument(random(), document))); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> processor.execute(RandomDocumentPicks.randomIngestDocument(random(), document)) + ); assertThat(e.getMessage(), equalTo("unable to parse date [2010]")); assertThat(e.getCause().getMessage(), equalTo("Unknown language: invalid")); } public void testOutputFormat() { long nanosAfterEpoch = randomLongBetween(1, 999999); - DateProcessor processor = new DateProcessor(randomAlphaOfLength(10), null, null, null, - "date_as_string", Collections.singletonList("iso8601"), "date_as_date", "HH:mm:ss.SSSSSSSSS"); + DateProcessor processor = new DateProcessor( + randomAlphaOfLength(10), + null, + null, + null, + "date_as_string", + Collections.singletonList("iso8601"), + "date_as_date", + "HH:mm:ss.SSSSSSSSS" + ); Map document = new HashMap<>(); document.put("date_as_string", Instant.EPOCH.plusNanos(nanosAfterEpoch).toString()); IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document); diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DissectProcessorTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DissectProcessorTests.java index 843bdee6b9bdc..c15cc80ecbcc6 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DissectProcessorTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DissectProcessorTests.java @@ -29,8 +29,14 @@ public class DissectProcessorTests extends ESTestCase { public void testMatch() { - IngestDocument ingestDocument = new IngestDocument("_index", "_id", null, null, null, - Collections.singletonMap("message", "foo,bar,baz")); + IngestDocument ingestDocument = new IngestDocument( + "_index", + "_id", + null, + null, + null, + Collections.singletonMap("message", "foo,bar,baz") + ); DissectProcessor dissectProcessor = new DissectProcessor("", null, "message", "%{a},%{b},%{c}", "", true); dissectProcessor.execute(ingestDocument); assertThat(ingestDocument.getFieldValue("a", String.class), equalTo("foo")); @@ -39,11 +45,14 @@ public void testMatch() { } public void testMatchOverwrite() { - IngestDocument ingestDocument = new IngestDocument("_index", "_id", null, null, null, - MapBuilder.newMapBuilder() - .put("message", "foo,bar,baz") - .put("a", "willgetstompped") - .map()); + IngestDocument ingestDocument = new IngestDocument( + "_index", + "_id", + null, + null, + null, + MapBuilder.newMapBuilder().put("message", "foo,bar,baz").put("a", "willgetstompped").map() + ); assertThat(ingestDocument.getFieldValue("a", String.class), equalTo("willgetstompped")); DissectProcessor dissectProcessor = new DissectProcessor("", null, "message", "%{a},%{b},%{c}", "", true); dissectProcessor.execute(ingestDocument); @@ -53,10 +62,22 @@ public void testMatchOverwrite() { } public void testAdvancedMatch() { - IngestDocument ingestDocument = new IngestDocument("_index", "_id", null, null, null, - Collections.singletonMap("message", "foo bar,,,,,,,baz nope:notagain 😊 🐇 🙃")); - DissectProcessor dissectProcessor = - new DissectProcessor("", null, "message", "%{a->} %{*b->},%{&b} %{}:%{?skipme} %{+smile/2} 🐇 %{+smile/1}", "::::", true); + IngestDocument ingestDocument = new IngestDocument( + "_index", + "_id", + null, + null, + null, + Collections.singletonMap("message", "foo bar,,,,,,,baz nope:notagain 😊 🐇 🙃") + ); + DissectProcessor dissectProcessor = new DissectProcessor( + "", + null, + "message", + "%{a->} %{*b->},%{&b} %{}:%{?skipme} %{+smile/2} 🐇 %{+smile/1}", + "::::", + true + ); dissectProcessor.execute(ingestDocument); assertThat(ingestDocument.getFieldValue("a", String.class), equalTo("foo")); assertThat(ingestDocument.getFieldValue("bar", String.class), equalTo("baz")); @@ -66,8 +87,14 @@ public void testAdvancedMatch() { } public void testMiss() { - IngestDocument ingestDocument = new IngestDocument("_index", "_id", null, null, null, - Collections.singletonMap("message", "foo:bar,baz")); + IngestDocument ingestDocument = new IngestDocument( + "_index", + "_id", + null, + null, + null, + Collections.singletonMap("message", "foo:bar,baz") + ); DissectProcessor dissectProcessor = new DissectProcessor("", null, "message", "%{a},%{b},%{c}", "", true); DissectException e = expectThrows(DissectException.class, () -> dissectProcessor.execute(ingestDocument)); assertThat(e.getMessage(), CoreMatchers.containsString("Unable to find match for dissect pattern")); @@ -85,8 +112,10 @@ public void testNonStringValueWithIgnoreMissing() { public void testNullValueWithIgnoreMissing() throws Exception { String fieldName = RandomDocumentPicks.randomFieldName(random()); Processor processor = new DissectProcessor("", null, fieldName, "%{a},%{b},%{c}", "", true); - IngestDocument originalIngestDocument = RandomDocumentPicks - .randomIngestDocument(random(), Collections.singletonMap(fieldName, null)); + IngestDocument originalIngestDocument = RandomDocumentPicks.randomIngestDocument( + random(), + Collections.singletonMap(fieldName, null) + ); IngestDocument ingestDocument = new IngestDocument(originalIngestDocument); processor.execute(ingestDocument); assertIngestDocument(originalIngestDocument, ingestDocument); @@ -95,8 +124,10 @@ public void testNullValueWithIgnoreMissing() throws Exception { public void testNullValueWithOutIgnoreMissing() { String fieldName = RandomDocumentPicks.randomFieldName(random()); Processor processor = new DissectProcessor("", null, fieldName, "%{a},%{b},%{c}", "", false); - IngestDocument originalIngestDocument = RandomDocumentPicks - .randomIngestDocument(random(), Collections.singletonMap(fieldName, null)); + IngestDocument originalIngestDocument = RandomDocumentPicks.randomIngestDocument( + random(), + Collections.singletonMap(fieldName, null) + ); IngestDocument ingestDocument = new IngestDocument(originalIngestDocument); expectThrows(IllegalArgumentException.class, () -> processor.execute(ingestDocument)); } diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DotExpanderProcessorFactoryTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DotExpanderProcessorFactoryTests.java index a153f736a3662..a4f8a6b0a2b58 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DotExpanderProcessorFactoryTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DotExpanderProcessorFactoryTests.java @@ -39,7 +39,7 @@ public void testCreate() throws Exception { public void testValidFields() throws Exception { DotExpanderProcessor.Factory factory = new DotExpanderProcessor.Factory(); - String[] fields = new String[] {"a.b", "a.b.c", "a.b.c.d", "ab.cd"}; + String[] fields = new String[] { "a.b", "a.b.c", "a.b.c.d", "ab.cd" }; for (String field : fields) { Map config = new HashMap<>(); config.put("field", field); @@ -61,7 +61,7 @@ public void testCreate_fieldMissing() throws Exception { public void testCreate_invalidFields() throws Exception { DotExpanderProcessor.Factory factory = new DotExpanderProcessor.Factory(); - String[] fields = new String[] {"a", "abc"}; + String[] fields = new String[] { "a", "abc" }; for (String field : fields) { Map config = new HashMap<>(); config.put("field", field); @@ -69,7 +69,7 @@ public void testCreate_invalidFields() throws Exception { assertThat(e.getMessage(), equalTo("[field] field does not contain a dot and is not a wildcard")); } - fields = new String[] {".a", "a.", "."}; + fields = new String[] { ".a", "a.", "." }; for (String field : fields) { Map config = new HashMap<>(); config.put("field", field); @@ -77,7 +77,7 @@ public void testCreate_invalidFields() throws Exception { assertThat(e.getMessage(), equalTo("[field] Field can't start or end with a dot")); } - fields = new String[] {"a..b", "a...b", "a.b..c", "abc.def..hij"}; + fields = new String[] { "a..b", "a...b", "a.b..c", "abc.def..hij" }; for (String field : fields) { Map config = new HashMap<>(); config.put("field", field); diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DotExpanderProcessorTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DotExpanderProcessorTests.java index d3349302b1a22..4dfa92fb5598d 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DotExpanderProcessorTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/DotExpanderProcessorTests.java @@ -76,8 +76,13 @@ public void testEscapeFields_valueField() throws Exception { // so because foo is no branch field but a value field the `foo.bar` field can't be expanded // into [foo].[bar], so foo should be renamed first into `[foo].[bar]: IngestDocument document = new IngestDocument(source, Collections.emptyMap()); - Processor processor = new RenameProcessor("_tag", null, new TestTemplateService.MockTemplateScript.Factory("foo"), - new TestTemplateService.MockTemplateScript.Factory("foo.bar"), false); + Processor processor = new RenameProcessor( + "_tag", + null, + new TestTemplateService.MockTemplateScript.Factory("foo"), + new TestTemplateService.MockTemplateScript.Factory("foo.bar"), + false + ); processor.execute(document); processor = new DotExpanderProcessor("_tag", null, null, "foo.bar"); processor.execute(document); @@ -132,36 +137,35 @@ public void testEscapeFields_path() throws Exception { assertThat(document.getFieldValue("field.foo.bar.baz", String.class), equalTo("value")); } - public void testEscapeFields_doNothingIfFieldNotInSourceDoc() throws Exception { - //asking to expand a (literal) field that is not present in the source document + // asking to expand a (literal) field that is not present in the source document Map source = new HashMap<>(); source.put("foo.bar", "baz1"); IngestDocument document = new IngestDocument(source, Collections.emptyMap()); - //abc.def does not exist in source, so don't mutate document + // abc.def does not exist in source, so don't mutate document DotExpanderProcessor processor = new DotExpanderProcessor("_tag", null, null, "abc.def"); processor.execute(document); - //hasField returns false since it requires the expanded form, which is not expanded since we did not ask for it to be + // hasField returns false since it requires the expanded form, which is not expanded since we did not ask for it to be assertFalse(document.hasField("foo.bar")); - //nothing has changed + // nothing has changed assertEquals(document.getSourceAndMetadata().get("foo.bar"), "baz1"); - //abc.def is not found anywhere + // abc.def is not found anywhere assertFalse(document.hasField("abc.def")); assertFalse(document.getSourceAndMetadata().containsKey("abc")); assertFalse(document.getSourceAndMetadata().containsKey("abc.def")); - //asking to expand a (literal) field that does not exist, but the nested field does exist + // asking to expand a (literal) field that does not exist, but the nested field does exist source = new HashMap<>(); Map inner = new HashMap<>(); inner.put("bar", "baz1"); source.put("foo", inner); document = new IngestDocument(source, Collections.emptyMap()); - //foo.bar, the literal value (as opposed to nested value) does not exist in source, so don't mutate document + // foo.bar, the literal value (as opposed to nested value) does not exist in source, so don't mutate document processor = new DotExpanderProcessor("_tag", null, null, "foo.bar"); processor.execute(document); - //hasField returns true because the nested/expanded form exists in the source document + // hasField returns true because the nested/expanded form exists in the source document assertTrue(document.hasField("foo.bar")); - //nothing changed + // nothing changed assertThat(document.getFieldValue("foo", Map.class).size(), equalTo(1)); assertThat(document.getFieldValue("foo.bar", String.class), equalTo("baz1")); } diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/FailProcessorFactoryTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/FailProcessorFactoryTests.java index 42e83951373d5..654cfef2caa68 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/FailProcessorFactoryTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/FailProcessorFactoryTests.java @@ -43,7 +43,7 @@ public void testCreateMissingMessageField() throws Exception { try { factory.create(null, null, null, config); fail("factory create should have failed"); - } catch(ElasticsearchParseException e) { + } catch (ElasticsearchParseException e) { assertThat(e.getMessage(), equalTo("[message] required property is missing")); } } @@ -53,8 +53,10 @@ public void testInvalidMustacheTemplate() throws Exception { Map config = new HashMap<>(); config.put("message", "{{error}}"); String processorTag = randomAlphaOfLength(10); - ElasticsearchException exception = expectThrows(ElasticsearchException.class, () -> factory.create(null, processorTag, - null, config)); + ElasticsearchException exception = expectThrows( + ElasticsearchException.class, + () -> factory.create(null, processorTag, null, config) + ); assertThat(exception.getMessage(), equalTo("java.lang.RuntimeException: could not compile script")); assertThat(exception.getMetadata("es.processor_tag").get(0), equalTo(processorTag)); } diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/FailProcessorTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/FailProcessorTests.java index f5a904cfd51b1..db417dd68425c 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/FailProcessorTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/FailProcessorTests.java @@ -21,8 +21,7 @@ public class FailProcessorTests extends ESTestCase { public void test() throws Exception { IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random()); String message = randomAlphaOfLength(10); - Processor processor = new FailProcessor(randomAlphaOfLength(10), - null, new TestTemplateService.MockTemplateScript.Factory(message)); + Processor processor = new FailProcessor(randomAlphaOfLength(10), null, new TestTemplateService.MockTemplateScript.Factory(message)); try { processor.execute(ingestDocument); fail("fail processor should throw an exception"); diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/ForEachProcessorFactoryTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/ForEachProcessorFactoryTests.java index 5b2aaedeb85e0..e5bd1910aa924 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/ForEachProcessorFactoryTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/ForEachProcessorFactoryTests.java @@ -29,7 +29,7 @@ public class ForEachProcessorFactoryTests extends ESTestCase { private final Consumer genericExecutor = Runnable::run; public void testCreate() throws Exception { - Processor processor = new TestProcessor(ingestDocument -> { }); + Processor processor = new TestProcessor(ingestDocument -> {}); Map registry = new HashMap<>(); registry.put("_name", (r, t, description, c) -> processor); ForEachProcessor.Factory forEachFactory = new ForEachProcessor.Factory(scriptService); @@ -45,7 +45,7 @@ public void testCreate() throws Exception { } public void testSetIgnoreMissing() throws Exception { - Processor processor = new TestProcessor(ingestDocument -> { }); + Processor processor = new TestProcessor(ingestDocument -> {}); Map registry = new HashMap<>(); registry.put("_name", (r, t, description, c) -> processor); ForEachProcessor.Factory forEachFactory = new ForEachProcessor.Factory(scriptService); @@ -62,7 +62,7 @@ public void testSetIgnoreMissing() throws Exception { } public void testCreateWithTooManyProcessorTypes() throws Exception { - Processor processor = new TestProcessor(ingestDocument -> { }); + Processor processor = new TestProcessor(ingestDocument -> {}); Map registry = new HashMap<>(); registry.put("_first", (r, t, description, c) -> processor); registry.put("_second", (r, t, description, c) -> processor); @@ -83,13 +83,15 @@ public void testCreateWithNonExistingProcessorType() throws Exception { Map config = new HashMap<>(); config.put("field", "_field"); config.put("processor", Collections.singletonMap("_name", Collections.emptyMap())); - Exception expectedException = expectThrows(ElasticsearchParseException.class, - () -> forEachFactory.create(Collections.emptyMap(), null, null, config)); + Exception expectedException = expectThrows( + ElasticsearchParseException.class, + () -> forEachFactory.create(Collections.emptyMap(), null, null, config) + ); assertThat(expectedException.getMessage(), equalTo("No processor type exists with name [_name]")); } public void testCreateWithMissingField() throws Exception { - Processor processor = new TestProcessor(ingestDocument -> { }); + Processor processor = new TestProcessor(ingestDocument -> {}); Map registry = new HashMap<>(); registry.put("_name", (r, t, description, c) -> processor); ForEachProcessor.Factory forEachFactory = new ForEachProcessor.Factory(scriptService); diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/ForEachProcessorTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/ForEachProcessorTests.java index bbf9b7da12351..5fb93b73098d5 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/ForEachProcessorTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/ForEachProcessorTests.java @@ -41,14 +41,10 @@ public void testExecuteWithAsyncProcessor() throws Exception { values.add("foo"); values.add("bar"); values.add("baz"); - IngestDocument ingestDocument = new IngestDocument( - "_index", "_id", null, null, null, Collections.singletonMap("values", values) - ); + IngestDocument ingestDocument = new IngestDocument("_index", "_id", null, null, null, Collections.singletonMap("values", values)); - ForEachProcessor processor = new ForEachProcessor("_tag", null, "values", new AsyncUpperCaseProcessor("_ingest._value"), - false); - processor.execute(ingestDocument, (result, e) -> { - }); + ForEachProcessor processor = new ForEachProcessor("_tag", null, "values", new AsyncUpperCaseProcessor("_ingest._value"), false); + processor.execute(ingestDocument, (result, e) -> {}); assertBusy(() -> { @SuppressWarnings("unchecked") @@ -62,7 +58,12 @@ public void testExecuteWithAsyncProcessor() throws Exception { public void testExecuteWithFailure() throws Exception { IngestDocument ingestDocument = new IngestDocument( - "_index", "_id", null, null, null, Collections.singletonMap("values", Arrays.asList("a", "b", "c")) + "_index", + "_id", + null, + null, + null, + Collections.singletonMap("values", Arrays.asList("a", "b", "c")) ); TestProcessor testProcessor = new TestProcessor(id -> { @@ -72,7 +73,7 @@ public void testExecuteWithFailure() throws Exception { }); ForEachProcessor processor = new ForEachProcessor("_tag", null, "values", testProcessor, false); Exception[] exceptions = new Exception[1]; - processor.execute(ingestDocument, (result, e) -> {exceptions[0] = e;}); + processor.execute(ingestDocument, (result, e) -> { exceptions[0] = e; }); assertThat(exceptions[0].getMessage(), equalTo("failure")); assertThat(testProcessor.getInvokedCounter(), equalTo(3)); assertThat(ingestDocument.getFieldValue("values", List.class), equalTo(Arrays.asList("a", "b", "c"))); @@ -87,7 +88,10 @@ public void testExecuteWithFailure() throws Exception { }); Processor onFailureProcessor = new TestProcessor(ingestDocument1 -> {}); processor = new ForEachProcessor( - "_tag", null, "values", new CompoundProcessor(false, Arrays.asList(testProcessor), Arrays.asList(onFailureProcessor)), + "_tag", + null, + "values", + new CompoundProcessor(false, Arrays.asList(testProcessor), Arrays.asList(onFailureProcessor)), false ); processor.execute(ingestDocument, (result, e) -> {}); @@ -99,9 +103,7 @@ public void testMetadataAvailable() throws Exception { List> values = new ArrayList<>(); values.add(new HashMap<>()); values.add(new HashMap<>()); - IngestDocument ingestDocument = new IngestDocument( - "_index", "_id", null, null, null, Collections.singletonMap("values", values) - ); + IngestDocument ingestDocument = new IngestDocument("_index", "_id", null, null, null, Collections.singletonMap("values", values)); TestProcessor innerProcessor = new TestProcessor(id -> { id.setFieldValue("_ingest._value.index", id.getSourceAndMetadata().get("_index")); @@ -131,9 +133,18 @@ public void testRestOfTheDocumentIsAvailable() throws Exception { IngestDocument ingestDocument = new IngestDocument("_index", "_id", null, null, null, document); ForEachProcessor processor = new ForEachProcessor( - "_tag", null, "values", new SetProcessor("_tag", - null, new TestTemplateService.MockTemplateScript.Factory("_ingest._value.new_field"), - (model) -> model.get("other"), null), false); + "_tag", + null, + "values", + new SetProcessor( + "_tag", + null, + new TestTemplateService.MockTemplateScript.Factory("_ingest._value.new_field"), + (model) -> model.get("other"), + null + ), + false + ); processor.execute(ingestDocument, (result, e) -> {}); assertThat(ingestDocument.getFieldValue("values.0.new_field", String.class), equalTo("value")); @@ -145,22 +156,22 @@ public void testRestOfTheDocumentIsAvailable() throws Exception { public void testRandom() { Processor innerProcessor = new Processor() { - @Override - public IngestDocument execute(IngestDocument ingestDocument) { - String existingValue = ingestDocument.getFieldValue("_ingest._value", String.class); - ingestDocument.setFieldValue("_ingest._value", existingValue + "."); - return ingestDocument; - } + @Override + public IngestDocument execute(IngestDocument ingestDocument) { + String existingValue = ingestDocument.getFieldValue("_ingest._value", String.class); + ingestDocument.setFieldValue("_ingest._value", existingValue + "."); + return ingestDocument; + } - @Override - public String getType() { - return null; - } + @Override + public String getType() { + return null; + } - @Override - public String getTag() { - return null; - } + @Override + public String getTag() { + return null; + } @Override public String getDescription() { @@ -168,11 +179,9 @@ public String getDescription() { } }; int numValues = randomIntBetween(1, 10000); - List values = IntStream.range(0, numValues).mapToObj(i->"").collect(Collectors.toList()); + List values = IntStream.range(0, numValues).mapToObj(i -> "").collect(Collectors.toList()); - IngestDocument ingestDocument = new IngestDocument( - "_index", "_id", null, null, null, Collections.singletonMap("values", values) - ); + IngestDocument ingestDocument = new IngestDocument("_index", "_id", null, null, null, Collections.singletonMap("values", values)); ForEachProcessor processor = new ForEachProcessor("_tag", null, "values", innerProcessor, false); processor.execute(ingestDocument, (result, e) -> {}); @@ -188,17 +197,21 @@ public void testModifyFieldsOutsideArray() throws Exception { values.add("string"); values.add(1); values.add(null); - IngestDocument ingestDocument = new IngestDocument( - "_index", "_id", null, null, null, Collections.singletonMap("values", values) - ); + IngestDocument ingestDocument = new IngestDocument("_index", "_id", null, null, null, Collections.singletonMap("values", values)); TemplateScript.Factory template = new TestTemplateService.MockTemplateScript.Factory("errors"); ForEachProcessor processor = new ForEachProcessor( - "_tag", null, "values", new CompoundProcessor(false, + "_tag", + null, + "values", + new CompoundProcessor( + false, List.of(new UppercaseProcessor("_tag_upper", null, "_ingest._value", false, "_ingest._value")), List.of(new AppendProcessor("_tag", null, template, (model) -> (Collections.singletonList("added")), true)) - ), false); + ), + false + ); processor.execute(ingestDocument, (result, e) -> {}); List result = ingestDocument.getFieldValue("values", List.class); @@ -218,12 +231,11 @@ public void testScalarValueAllowsUnderscoreValueFieldToRemainAccessible() throws Map source = new HashMap<>(); source.put("_value", "new_value"); source.put("values", values); - IngestDocument ingestDocument = new IngestDocument( - "_index", "_id", null, null, null, source - ); + IngestDocument ingestDocument = new IngestDocument("_index", "_id", null, null, null, source); - TestProcessor processor = new TestProcessor(doc -> doc.setFieldValue("_ingest._value", - doc.getFieldValue("_source._value", String.class))); + TestProcessor processor = new TestProcessor( + doc -> doc.setFieldValue("_ingest._value", doc.getFieldValue("_source._value", String.class)) + ); ForEachProcessor forEachProcessor = new ForEachProcessor("_tag", null, "values", processor, false); forEachProcessor.execute(ingestDocument, (result, e) -> {}); @@ -249,16 +261,18 @@ public void testNestedForEach() throws Exception { value.put("values2", innerValues); values.add(value); - IngestDocument ingestDocument = new IngestDocument( - "_index", "_id", null, null, null, Collections.singletonMap("values1", values) - ); + IngestDocument ingestDocument = new IngestDocument("_index", "_id", null, null, null, Collections.singletonMap("values1", values)); TestProcessor testProcessor = new TestProcessor( - doc -> doc.setFieldValue("_ingest._value", doc.getFieldValue("_ingest._value", String.class).toUpperCase(Locale.ENGLISH)) + doc -> doc.setFieldValue("_ingest._value", doc.getFieldValue("_ingest._value", String.class).toUpperCase(Locale.ENGLISH)) ); ForEachProcessor processor = new ForEachProcessor( - "_tag", null, "values1", new ForEachProcessor("_tag", null, "_ingest._value.values2", testProcessor, false), - false); + "_tag", + null, + "values1", + new ForEachProcessor("_tag", null, "_ingest._value.values2", testProcessor, false), + false + ); processor.execute(ingestDocument, (result, e) -> {}); List result = ingestDocument.getFieldValue("values1.0.values2", List.class); @@ -280,27 +294,29 @@ public void testNestedForEachWithMapIteration() throws Exception { List visitedKeys = new ArrayList<>(); List visitedValues = new ArrayList<>(); - TestProcessor testProcessor = new TestProcessor( - doc -> { - String key = (String) doc.getIngestMetadata().get("_key"); - Object value = doc.getIngestMetadata().get("_value"); - visitedKeys.add(key); - visitedValues.add(value); - - // change some of the keys - if (key.startsWith("bar")) { - doc.setFieldValue("_ingest._key", "bar2"); - } - // change some of the values - if (key.startsWith("baz")) { - doc.setFieldValue("_ingest._value", ((Integer) value) * 2); - } + TestProcessor testProcessor = new TestProcessor(doc -> { + String key = (String) doc.getIngestMetadata().get("_key"); + Object value = doc.getIngestMetadata().get("_value"); + visitedKeys.add(key); + visitedValues.add(value); + + // change some of the keys + if (key.startsWith("bar")) { + doc.setFieldValue("_ingest._key", "bar2"); } - ); + // change some of the values + if (key.startsWith("baz")) { + doc.setFieldValue("_ingest._value", ((Integer) value) * 2); + } + }); ForEachProcessor processor = new ForEachProcessor( - "_tag", null, "field", new ForEachProcessor("_tag", null, "_ingest._value", testProcessor, false), - false); + "_tag", + null, + "field", + new ForEachProcessor("_tag", null, "_ingest._value", testProcessor, false), + false + ); processor.execute(ingestDocument, (result, e) -> {}); assertThat(testProcessor.getInvokedCounter(), equalTo(10)); @@ -309,7 +325,8 @@ public void testNestedForEachWithMapIteration() throws Exception { arrayContainingInAnyOrder("foo1", "bar1", "baz1", "foo2", "bar2", "baz2", "foo3", "bar3", "baz3", "otherKey") ); assertThat(visitedValues.toArray(), arrayContainingInAnyOrder(1, 2, 3, 4, 5, 6, 7, 8, 9, 42)); - assertThat(ingestDocument.getFieldValue("field", Map.class).entrySet().toArray(), + assertThat( + ingestDocument.getFieldValue("field", Map.class).entrySet().toArray(), arrayContainingInAnyOrder( Map.entry("foo", Map.of("foo1", 1, "bar2", 2, "baz1", 6)), Map.entry("bar", Map.of("foo2", 4, "bar2", 5, "baz2", 12)), @@ -319,9 +336,7 @@ public void testNestedForEachWithMapIteration() throws Exception { } public void testIgnoreMissing() throws Exception { - IngestDocument originalIngestDocument = new IngestDocument( - "_index", "_id", null, null, null, Collections.emptyMap() - ); + IngestDocument originalIngestDocument = new IngestDocument("_index", "_id", null, null, null, Collections.emptyMap()); IngestDocument ingestDocument = new IngestDocument(originalIngestDocument); TestProcessor testProcessor = new TestProcessor(doc -> {}); ForEachProcessor processor = new ForEachProcessor("_tag", null, "_ingest._value", testProcessor, true); @@ -333,7 +348,7 @@ public void testIgnoreMissing() throws Exception { public void testAppendingToTheSameField() { IngestDocument originalIngestDocument = new IngestDocument("_index", "_id", null, null, null, Map.of("field", List.of("a", "b"))); IngestDocument ingestDocument = new IngestDocument(originalIngestDocument); - TestProcessor testProcessor = new TestProcessor(id->id.appendFieldValue("field", "a")); + TestProcessor testProcessor = new TestProcessor(id -> id.appendFieldValue("field", "a")); ForEachProcessor processor = new ForEachProcessor("_tag", null, "field", testProcessor, true); processor.execute(ingestDocument, (result, e) -> {}); assertThat(testProcessor.getInvokedCounter(), equalTo(2)); @@ -375,8 +390,10 @@ public void testMapIteration() { assertThat(testProcessor.getInvokedCounter(), equalTo(3)); assertThat(encounteredKeys.toArray(), arrayContainingInAnyOrder("foo", "bar", "baz")); assertThat(encounteredValues.toArray(), arrayContainingInAnyOrder(1, 2, 3)); - assertThat(ingestDocument.getFieldValue("field", Map.class).entrySet().toArray(), - arrayContainingInAnyOrder(Map.entry("foo", 1), Map.entry("bar2", 2), Map.entry("baz", 33))); + assertThat( + ingestDocument.getFieldValue("field", Map.class).entrySet().toArray(), + arrayContainingInAnyOrder(Map.entry("foo", 1), Map.entry("bar2", 2), Map.entry("baz", 33)) + ); } public void testRemovalOfMapKey() { @@ -398,8 +415,10 @@ public void testRemovalOfMapKey() { assertThat(testProcessor.getInvokedCounter(), equalTo(3)); assertThat(encounteredKeys.toArray(), arrayContainingInAnyOrder("foo", "bar", "baz")); assertThat(encounteredValues.toArray(), arrayContainingInAnyOrder(1, 2, 3)); - assertThat(ingestDocument.getFieldValue("field", Map.class).entrySet().toArray(), - arrayContainingInAnyOrder(Map.entry("foo", 1), Map.entry("baz", 3))); + assertThat( + ingestDocument.getFieldValue("field", Map.class).entrySet().toArray(), + arrayContainingInAnyOrder(Map.entry("foo", 1), Map.entry("baz", 3)) + ); } public void testMapIterationWithAsyncProcessor() throws Exception { @@ -412,27 +431,29 @@ public void testMapIterationWithAsyncProcessor() throws Exception { List visitedKeys = new ArrayList<>(); List visitedValues = new ArrayList<>(); - TestAsyncProcessor testProcessor = new TestAsyncProcessor( - doc -> { - String key = (String) doc.getIngestMetadata().get("_key"); - Object value = doc.getIngestMetadata().get("_value"); - visitedKeys.add(key); - visitedValues.add(value); - - // change some of the keys - if (key.startsWith("bar")) { - doc.setFieldValue("_ingest._key", "bar2"); - } - // change some of the values - if (key.startsWith("baz")) { - doc.setFieldValue("_ingest._value", ((Integer) value) * 2); - } + TestAsyncProcessor testProcessor = new TestAsyncProcessor(doc -> { + String key = (String) doc.getIngestMetadata().get("_key"); + Object value = doc.getIngestMetadata().get("_value"); + visitedKeys.add(key); + visitedValues.add(value); + + // change some of the keys + if (key.startsWith("bar")) { + doc.setFieldValue("_ingest._key", "bar2"); } - ); + // change some of the values + if (key.startsWith("baz")) { + doc.setFieldValue("_ingest._value", ((Integer) value) * 2); + } + }); ForEachProcessor processor = new ForEachProcessor( - "_tag", null, "field", new ForEachProcessor("_tag", null, "_ingest._value", testProcessor, false), - false); + "_tag", + null, + "field", + new ForEachProcessor("_tag", null, "_ingest._value", testProcessor, false), + false + ); processor.execute(ingestDocument, (result, e) -> {}); assertBusy(() -> { @@ -442,7 +463,8 @@ public void testMapIterationWithAsyncProcessor() throws Exception { arrayContainingInAnyOrder("foo1", "bar1", "baz1", "foo2", "bar2", "baz2", "foo3", "bar3", "baz3", "otherKey") ); assertThat(visitedValues.toArray(), arrayContainingInAnyOrder(1, 2, 3, 4, 5, 6, 7, 8, 9, 42)); - assertThat(ingestDocument.getFieldValue("field", Map.class).entrySet().toArray(), + assertThat( + ingestDocument.getFieldValue("field", Map.class).entrySet().toArray(), arrayContainingInAnyOrder( Map.entry("foo", Map.of("foo1", 1, "bar2", 2, "baz1", 6)), Map.entry("bar", Map.of("foo2", 4, "bar2", 5, "baz2", 12)), diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/GrokProcessorFactoryTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/GrokProcessorFactoryTests.java index c9d0c0f49e6ee..300f58de884ea 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/GrokProcessorFactoryTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/GrokProcessorFactoryTests.java @@ -105,8 +105,10 @@ public void testCreateWithInvalidPatternDefinition() throws Exception { config.put("patterns", Collections.singletonList("%{MY_PATTERN:name}!")); config.put("pattern_definitions", Collections.singletonMap("MY_PATTERN", "[")); ElasticsearchParseException e = expectThrows(ElasticsearchParseException.class, () -> factory.create(null, null, null, config)); - assertThat(e.getMessage(), - equalTo("[patterns] Invalid regex pattern found in: [%{MY_PATTERN:name}!]. premature end of char-class")); + assertThat( + e.getMessage(), + equalTo("[patterns] Invalid regex pattern found in: [%{MY_PATTERN:name}!]. premature end of char-class") + ); } public void testCreateWithInvalidEcsCompatibilityMode() throws Exception { diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/GrokProcessorGetActionTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/GrokProcessorGetActionTests.java index d12719c5f317f..bf7b18814ca48 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/GrokProcessorGetActionTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/GrokProcessorGetActionTests.java @@ -13,13 +13,13 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.StreamInput; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.grok.Grok; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.json.JsonXContent; import java.util.ArrayList; import java.util.Collections; @@ -72,13 +72,13 @@ public void testResponseSorting() { new ActionListener<>() { @Override public void onResponse(GrokProcessorGetAction.Response response) { - receivedResponse[0] = response; - } + receivedResponse[0] = response; + } @Override public void onFailure(Exception e) { - fail(); - } + fail(); + } } ); assertThat(receivedResponse[0], notNullValue()); @@ -91,13 +91,13 @@ public void onFailure(Exception e) { new ActionListener<>() { @Override public void onResponse(GrokProcessorGetAction.Response response) { - receivedResponse[0] = response; - } + receivedResponse[0] = response; + } @Override public void onFailure(Exception e) { - fail(); - } + fail(); + } } ); assertThat(receivedResponse[0], notNullValue()); @@ -115,21 +115,17 @@ public void testEcsCompatibilityMode() { ECS_TEST_PATTERNS ); GrokProcessorGetAction.Response[] receivedResponse = new GrokProcessorGetAction.Response[1]; - transportAction.doExecute( - null, - new GrokProcessorGetAction.Request(true, Grok.ECS_COMPATIBILITY_MODES[1]), - new ActionListener<>() { - @Override - public void onResponse(GrokProcessorGetAction.Response response) { - receivedResponse[0] = response; - } + transportAction.doExecute(null, new GrokProcessorGetAction.Request(true, Grok.ECS_COMPATIBILITY_MODES[1]), new ActionListener<>() { + @Override + public void onResponse(GrokProcessorGetAction.Response response) { + receivedResponse[0] = response; + } - @Override - public void onFailure(Exception e) { - fail(); - } + @Override + public void onFailure(Exception e) { + fail(); } - ); + }); assertThat(receivedResponse[0], notNullValue()); assertThat(receivedResponse[0].getGrokPatterns().keySet().toArray(), equalTo(sortedKeys.toArray())); } diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/GrokProcessorTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/GrokProcessorTests.java index 4c1f3e475fb73..5957639b010ba 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/GrokProcessorTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/GrokProcessorTests.java @@ -21,15 +21,22 @@ import static org.elasticsearch.ingest.IngestDocumentMatcher.assertIngestDocument; import static org.hamcrest.Matchers.equalTo; - public class GrokProcessorTests extends ESTestCase { public void testMatch() throws Exception { String fieldName = RandomDocumentPicks.randomFieldName(random()); IngestDocument doc = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>()); doc.setFieldValue(fieldName, "1"); - GrokProcessor processor = new GrokProcessor(randomAlphaOfLength(10), null, Collections.singletonMap("ONE", "1"), - Collections.singletonList("%{ONE:one}"), fieldName, false, false, MatcherWatchdog.noop()); + GrokProcessor processor = new GrokProcessor( + randomAlphaOfLength(10), + null, + Collections.singletonMap("ONE", "1"), + Collections.singletonList("%{ONE:one}"), + fieldName, + false, + false, + MatcherWatchdog.noop() + ); processor.execute(doc); assertThat(doc.getFieldValue("one", String.class), equalTo("1")); } @@ -38,8 +45,16 @@ public void testIgnoreCase() throws Exception { String fieldName = RandomDocumentPicks.randomFieldName(random()); IngestDocument doc = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>()); doc.setFieldValue(fieldName, "A"); - GrokProcessor processor = new GrokProcessor(randomAlphaOfLength(10), null, Collections.emptyMap(), - Collections.singletonList("(?(?i)A)"), fieldName, false, false, MatcherWatchdog.noop()); + GrokProcessor processor = new GrokProcessor( + randomAlphaOfLength(10), + null, + Collections.emptyMap(), + Collections.singletonList("(?(?i)A)"), + fieldName, + false, + false, + MatcherWatchdog.noop() + ); processor.execute(doc); assertThat(doc.getFieldValue("a", String.class), equalTo("A")); } @@ -48,8 +63,16 @@ public void testNoMatch() { String fieldName = RandomDocumentPicks.randomFieldName(random()); IngestDocument doc = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>()); doc.setFieldValue(fieldName, "23"); - GrokProcessor processor = new GrokProcessor(randomAlphaOfLength(10), null, Collections.singletonMap("ONE", "1"), - Collections.singletonList("%{ONE:one}"), fieldName, false, false, MatcherWatchdog.noop()); + GrokProcessor processor = new GrokProcessor( + randomAlphaOfLength(10), + null, + Collections.singletonMap("ONE", "1"), + Collections.singletonList("%{ONE:one}"), + fieldName, + false, + false, + MatcherWatchdog.noop() + ); Exception e = expectThrows(Exception.class, () -> processor.execute(doc)); assertThat(e.getMessage(), equalTo("Provided Grok expressions do not match field value: [23]")); } @@ -58,9 +81,19 @@ public void testNoMatchingPatternName() { String fieldName = RandomDocumentPicks.randomFieldName(random()); IngestDocument doc = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>()); doc.setFieldValue(fieldName, "23"); - Exception e = expectThrows(IllegalArgumentException.class, () -> new GrokProcessor(randomAlphaOfLength(10), - null, Collections.singletonMap("ONE", "1"), Collections.singletonList("%{NOTONE:not_one}"), fieldName, - false, false, MatcherWatchdog.noop())); + Exception e = expectThrows( + IllegalArgumentException.class, + () -> new GrokProcessor( + randomAlphaOfLength(10), + null, + Collections.singletonMap("ONE", "1"), + Collections.singletonList("%{NOTONE:not_one}"), + fieldName, + false, + false, + MatcherWatchdog.noop() + ) + ); assertThat(e.getMessage(), equalTo("Unable to find pattern [NOTONE] in Grok's pattern dictionary")); } @@ -69,8 +102,16 @@ public void testMatchWithoutCaptures() throws Exception { IngestDocument originalDoc = new IngestDocument(new HashMap<>(), new HashMap<>()); originalDoc.setFieldValue(fieldName, fieldName); IngestDocument doc = new IngestDocument(originalDoc); - GrokProcessor processor = new GrokProcessor(randomAlphaOfLength(10), null, Collections.emptyMap(), - Collections.singletonList(fieldName), fieldName, false, false, MatcherWatchdog.noop()); + GrokProcessor processor = new GrokProcessor( + randomAlphaOfLength(10), + null, + Collections.emptyMap(), + Collections.singletonList(fieldName), + fieldName, + false, + false, + MatcherWatchdog.noop() + ); processor.execute(doc); assertThat(doc, equalTo(originalDoc)); } @@ -79,8 +120,16 @@ public void testNullField() { String fieldName = RandomDocumentPicks.randomFieldName(random()); IngestDocument doc = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>()); doc.setFieldValue(fieldName, null); - GrokProcessor processor = new GrokProcessor(randomAlphaOfLength(10), null, Collections.singletonMap("ONE", "1"), - Collections.singletonList("%{ONE:one}"), fieldName, false, false, MatcherWatchdog.noop()); + GrokProcessor processor = new GrokProcessor( + randomAlphaOfLength(10), + null, + Collections.singletonMap("ONE", "1"), + Collections.singletonList("%{ONE:one}"), + fieldName, + false, + false, + MatcherWatchdog.noop() + ); Exception e = expectThrows(Exception.class, () -> processor.execute(doc)); assertThat(e.getMessage(), equalTo("field [" + fieldName + "] is null, cannot process it.")); } @@ -90,8 +139,16 @@ public void testNullFieldWithIgnoreMissing() throws Exception { IngestDocument originalIngestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>()); originalIngestDocument.setFieldValue(fieldName, null); IngestDocument ingestDocument = new IngestDocument(originalIngestDocument); - GrokProcessor processor = new GrokProcessor(randomAlphaOfLength(10), null, Collections.singletonMap("ONE", "1"), - Collections.singletonList("%{ONE:one}"), fieldName, false, true, MatcherWatchdog.noop()); + GrokProcessor processor = new GrokProcessor( + randomAlphaOfLength(10), + null, + Collections.singletonMap("ONE", "1"), + Collections.singletonList("%{ONE:one}"), + fieldName, + false, + true, + MatcherWatchdog.noop() + ); processor.execute(ingestDocument); assertIngestDocument(originalIngestDocument, ingestDocument); } @@ -100,8 +157,16 @@ public void testNotStringField() { String fieldName = RandomDocumentPicks.randomFieldName(random()); IngestDocument doc = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>()); doc.setFieldValue(fieldName, 1); - GrokProcessor processor = new GrokProcessor(randomAlphaOfLength(10), null, Collections.singletonMap("ONE", "1"), - Collections.singletonList("%{ONE:one}"), fieldName, false, false, MatcherWatchdog.noop()); + GrokProcessor processor = new GrokProcessor( + randomAlphaOfLength(10), + null, + Collections.singletonMap("ONE", "1"), + Collections.singletonList("%{ONE:one}"), + fieldName, + false, + false, + MatcherWatchdog.noop() + ); Exception e = expectThrows(Exception.class, () -> processor.execute(doc)); assertThat(e.getMessage(), equalTo("field [" + fieldName + "] of type [java.lang.Integer] cannot be cast to [java.lang.String]")); } @@ -110,8 +175,16 @@ public void testNotStringFieldWithIgnoreMissing() { String fieldName = RandomDocumentPicks.randomFieldName(random()); IngestDocument doc = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>()); doc.setFieldValue(fieldName, 1); - GrokProcessor processor = new GrokProcessor(randomAlphaOfLength(10), null, Collections.singletonMap("ONE", "1"), - Collections.singletonList("%{ONE:one}"), fieldName, false, true, MatcherWatchdog.noop()); + GrokProcessor processor = new GrokProcessor( + randomAlphaOfLength(10), + null, + Collections.singletonMap("ONE", "1"), + Collections.singletonList("%{ONE:one}"), + fieldName, + false, + true, + MatcherWatchdog.noop() + ); Exception e = expectThrows(Exception.class, () -> processor.execute(doc)); assertThat(e.getMessage(), equalTo("field [" + fieldName + "] of type [java.lang.Integer] cannot be cast to [java.lang.String]")); } @@ -119,8 +192,16 @@ public void testNotStringFieldWithIgnoreMissing() { public void testMissingField() { String fieldName = "foo.bar"; IngestDocument doc = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>()); - GrokProcessor processor = new GrokProcessor(randomAlphaOfLength(10), null, Collections.singletonMap("ONE", "1"), - Collections.singletonList("%{ONE:one}"), fieldName, false, false, MatcherWatchdog.noop()); + GrokProcessor processor = new GrokProcessor( + randomAlphaOfLength(10), + null, + Collections.singletonMap("ONE", "1"), + Collections.singletonList("%{ONE:one}"), + fieldName, + false, + false, + MatcherWatchdog.noop() + ); Exception e = expectThrows(Exception.class, () -> processor.execute(doc)); assertThat(e.getMessage(), equalTo("field [foo] not present as part of path [foo.bar]")); } @@ -129,8 +210,16 @@ public void testMissingFieldWithIgnoreMissing() throws Exception { String fieldName = "foo.bar"; IngestDocument originalIngestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>()); IngestDocument ingestDocument = new IngestDocument(originalIngestDocument); - GrokProcessor processor = new GrokProcessor(randomAlphaOfLength(10), null, Collections.singletonMap("ONE", "1"), - Collections.singletonList("%{ONE:one}"), fieldName, false, true, MatcherWatchdog.noop()); + GrokProcessor processor = new GrokProcessor( + randomAlphaOfLength(10), + null, + Collections.singletonMap("ONE", "1"), + Collections.singletonList("%{ONE:one}"), + fieldName, + false, + true, + MatcherWatchdog.noop() + ); processor.execute(ingestDocument); assertIngestDocument(originalIngestDocument, ingestDocument); } @@ -143,8 +232,16 @@ public void testMultiplePatternsWithMatchReturn() throws Exception { patternBank.put("ONE", "1"); patternBank.put("TWO", "2"); patternBank.put("THREE", "3"); - GrokProcessor processor = new GrokProcessor(randomAlphaOfLength(10), null, patternBank, - Arrays.asList("%{ONE:one}", "%{TWO:two}", "%{THREE:three}"), fieldName, false, false, MatcherWatchdog.noop()); + GrokProcessor processor = new GrokProcessor( + randomAlphaOfLength(10), + null, + patternBank, + Arrays.asList("%{ONE:one}", "%{TWO:two}", "%{THREE:three}"), + fieldName, + false, + false, + MatcherWatchdog.noop() + ); processor.execute(doc); assertThat(doc.hasField("one"), equalTo(false)); assertThat(doc.getFieldValue("two", String.class), equalTo("2")); @@ -159,8 +256,16 @@ public void testSetMetadata() throws Exception { patternBank.put("ONE", "1"); patternBank.put("TWO", "2"); patternBank.put("THREE", "3"); - GrokProcessor processor = new GrokProcessor(randomAlphaOfLength(10), null, patternBank, - Arrays.asList("%{ONE:one}", "%{TWO:two}", "%{THREE:three}"), fieldName, true, false, MatcherWatchdog.noop()); + GrokProcessor processor = new GrokProcessor( + randomAlphaOfLength(10), + null, + patternBank, + Arrays.asList("%{ONE:one}", "%{TWO:two}", "%{THREE:three}"), + fieldName, + true, + false, + MatcherWatchdog.noop() + ); processor.execute(doc); assertThat(doc.hasField("one"), equalTo(false)); assertThat(doc.getFieldValue("two", String.class), equalTo("2")); @@ -174,8 +279,16 @@ public void testTraceWithOnePattern() throws Exception { doc.setFieldValue(fieldName, "first1"); Map patternBank = new HashMap<>(); patternBank.put("ONE", "1"); - GrokProcessor processor = new GrokProcessor(randomAlphaOfLength(10), null, patternBank, - Arrays.asList("%{ONE:one}"), fieldName, true, false, MatcherWatchdog.noop()); + GrokProcessor processor = new GrokProcessor( + randomAlphaOfLength(10), + null, + patternBank, + Arrays.asList("%{ONE:one}"), + fieldName, + true, + false, + MatcherWatchdog.noop() + ); processor.execute(doc); assertThat(doc.hasField("one"), equalTo(true)); assertThat(doc.getFieldValue("_ingest._grok_match_index", String.class), equalTo("0")); @@ -205,8 +318,16 @@ public void testCombineSamePatternNameAcrossPatterns() throws Exception { patternBank.put("ONE", "1"); patternBank.put("TWO", "2"); patternBank.put("THREE", "3"); - GrokProcessor processor = new GrokProcessor(randomAlphaOfLength(10), null, patternBank, Arrays.asList("%{ONE:first}-%{TWO:second}", - "%{ONE:first}-%{THREE:second}"), fieldName, randomBoolean(), randomBoolean(), MatcherWatchdog.noop()); + GrokProcessor processor = new GrokProcessor( + randomAlphaOfLength(10), + null, + patternBank, + Arrays.asList("%{ONE:first}-%{TWO:second}", "%{ONE:first}-%{THREE:second}"), + fieldName, + randomBoolean(), + randomBoolean(), + MatcherWatchdog.noop() + ); processor.execute(doc); assertThat(doc.getFieldValue("first", String.class), equalTo("1")); assertThat(doc.getFieldValue("second", String.class), equalTo("3")); @@ -218,23 +339,37 @@ public void testFirstWinNamedCapture() throws Exception { doc.setFieldValue(fieldName, "12"); Map patternBank = new HashMap<>(); patternBank.put("ONETWO", "1|2"); - GrokProcessor processor = new GrokProcessor(randomAlphaOfLength(10), null, patternBank, - Collections.singletonList("%{ONETWO:first}%{ONETWO:first}"), fieldName, randomBoolean(), randomBoolean(), - MatcherWatchdog.noop()); + GrokProcessor processor = new GrokProcessor( + randomAlphaOfLength(10), + null, + patternBank, + Collections.singletonList("%{ONETWO:first}%{ONETWO:first}"), + fieldName, + randomBoolean(), + randomBoolean(), + MatcherWatchdog.noop() + ); processor.execute(doc); assertThat(doc.getFieldValue("first", String.class), equalTo("1")); } - public void testUnmatchedNamesNotIncludedInDocument() throws Exception { + public void testUnmatchedNamesNotIncludedInDocument() throws Exception { String fieldName = RandomDocumentPicks.randomFieldName(random()); IngestDocument doc = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>()); doc.setFieldValue(fieldName, "3"); Map patternBank = new HashMap<>(); patternBank.put("ONETWO", "1|2"); patternBank.put("THREE", "3"); - GrokProcessor processor = new GrokProcessor(randomAlphaOfLength(10), null, patternBank, - Collections.singletonList("%{ONETWO:first}|%{THREE:second}"), fieldName, randomBoolean(), randomBoolean(), - MatcherWatchdog.noop()); + GrokProcessor processor = new GrokProcessor( + randomAlphaOfLength(10), + null, + patternBank, + Collections.singletonList("%{ONETWO:first}|%{THREE:second}"), + fieldName, + randomBoolean(), + randomBoolean(), + MatcherWatchdog.noop() + ); processor.execute(doc); assertFalse(doc.hasField("first")); assertThat(doc.getFieldValue("second", String.class), equalTo("3")); diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/GsubProcessorFactoryTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/GsubProcessorFactoryTests.java index abd5375075850..0e0d13cb92695 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/GsubProcessorFactoryTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/GsubProcessorFactoryTests.java @@ -45,7 +45,7 @@ public void testCreateNoPatternPresent() throws Exception { try { factory.create(null, null, null, config); fail("factory create should have failed"); - } catch(ElasticsearchParseException e) { + } catch (ElasticsearchParseException e) { assertThat(e.getMessage(), equalTo("[pattern] required property is missing")); } } @@ -58,7 +58,7 @@ public void testCreateNoReplacementPresent() throws Exception { try { factory.create(null, null, null, config); fail("factory create should have failed"); - } catch(ElasticsearchParseException e) { + } catch (ElasticsearchParseException e) { assertThat(e.getMessage(), equalTo("[replacement] required property is missing")); } } @@ -72,7 +72,7 @@ public void testCreateInvalidPattern() throws Exception { try { factory.create(null, null, null, config); fail("factory create should have failed"); - } catch(ElasticsearchParseException e) { + } catch (ElasticsearchParseException e) { assertThat(e.getMessage(), containsString("[pattern] Invalid regex pattern. Unclosed character class")); } } diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/JoinProcessorTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/JoinProcessorTests.java index 4141ecc755260..5ee9ee383638b 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/JoinProcessorTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/JoinProcessorTests.java @@ -23,7 +23,7 @@ public class JoinProcessorTests extends ESTestCase { - private static final String[] SEPARATORS = new String[]{"-", "_", "."}; + private static final String[] SEPARATORS = new String[] { "-", "_", "." }; public void testJoinStrings() throws Exception { IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random()); @@ -72,7 +72,7 @@ public void testJoinNonListField() throws Exception { Processor processor = new JoinProcessor(randomAlphaOfLength(10), null, fieldName, "-", fieldName); try { processor.execute(ingestDocument); - } catch(IllegalArgumentException e) { + } catch (IllegalArgumentException e) { assertThat(e.getMessage(), equalTo("field [" + fieldName + "] of type [java.lang.String] cannot be cast to [java.util.List]")); } } @@ -83,7 +83,7 @@ public void testJoinNonExistingField() throws Exception { Processor processor = new JoinProcessor(randomAlphaOfLength(10), null, fieldName, "-", fieldName); try { processor.execute(ingestDocument); - } catch(IllegalArgumentException e) { + } catch (IllegalArgumentException e) { assertThat(e.getMessage(), containsString("not present as part of path [" + fieldName + "]")); } } @@ -93,7 +93,7 @@ public void testJoinNullValue() throws Exception { Processor processor = new JoinProcessor(randomAlphaOfLength(10), null, "field", "-", "field"); try { processor.execute(ingestDocument); - } catch(IllegalArgumentException e) { + } catch (IllegalArgumentException e) { assertThat(e.getMessage(), equalTo("field [field] is null, cannot join.")); } } diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/JsonProcessorFactoryTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/JsonProcessorFactoryTests.java index e48158c76ed02..c6d7d9deff806 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/JsonProcessorFactoryTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/JsonProcessorFactoryTests.java @@ -61,8 +61,10 @@ public void testCreateWithDefaultTarget() throws Exception { public void testCreateWithMissingField() throws Exception { Map config = new HashMap<>(); String processorTag = randomAlphaOfLength(10); - ElasticsearchException exception = expectThrows(ElasticsearchParseException.class, - () -> FACTORY.create(null, processorTag, null, config)); + ElasticsearchException exception = expectThrows( + ElasticsearchParseException.class, + () -> FACTORY.create(null, processorTag, null, config) + ); assertThat(exception.getMessage(), equalTo("[field] required property is missing")); } @@ -73,8 +75,10 @@ public void testCreateWithBothTargetFieldAndAddToRoot() throws Exception { config.put("field", randomField); config.put("target_field", randomTargetField); config.put("add_to_root", true); - ElasticsearchException exception = expectThrows(ElasticsearchParseException.class, - () -> FACTORY.create(null, randomAlphaOfLength(10), null, config)); + ElasticsearchException exception = expectThrows( + ElasticsearchParseException.class, + () -> FACTORY.create(null, randomAlphaOfLength(10), null, config) + ); assertThat(exception.getMessage(), equalTo("[target_field] Cannot set a target field while also setting `add_to_root` to true")); } @@ -92,17 +96,25 @@ public void testRecursiveMergeStrategy() throws Exception { } public void testMergeStrategyWithoutAddToRoot() throws Exception { - ElasticsearchException exception = expectThrows(ElasticsearchParseException.class, - () -> getJsonProcessorWithMergeStrategy("replace", false)); - assertThat(exception.getMessage(), - equalTo("[add_to_root_conflict_strategy] Cannot set `add_to_root_conflict_strategy` if `add_to_root` is false")); + ElasticsearchException exception = expectThrows( + ElasticsearchParseException.class, + () -> getJsonProcessorWithMergeStrategy("replace", false) + ); + assertThat( + exception.getMessage(), + equalTo("[add_to_root_conflict_strategy] Cannot set `add_to_root_conflict_strategy` if `add_to_root` is false") + ); } public void testUnknownMergeStrategy() throws Exception { - ElasticsearchException exception = expectThrows(ElasticsearchParseException.class, - () -> getJsonProcessorWithMergeStrategy("foo", true)); - assertThat(exception.getMessage(), - equalTo("[add_to_root_conflict_strategy] conflict strategy [foo] not supported, cannot convert field.")); + ElasticsearchException exception = expectThrows( + ElasticsearchParseException.class, + () -> getJsonProcessorWithMergeStrategy("foo", true) + ); + assertThat( + exception.getMessage(), + equalTo("[add_to_root_conflict_strategy] conflict strategy [foo] not supported, cannot convert field.") + ); } private JsonProcessor getJsonProcessorWithMergeStrategy(String mergeStrategy, boolean addToRoot) throws Exception { diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/JsonProcessorTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/JsonProcessorTests.java index 7ce41cace0744..d21b06cb74356 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/JsonProcessorTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/JsonProcessorTests.java @@ -9,13 +9,13 @@ package org.elasticsearch.ingest.common; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.ingest.IngestDocument; import org.elasticsearch.ingest.RandomDocumentPicks; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.xcontent.json.JsonXContent; import java.util.Arrays; import java.util.HashMap; @@ -55,8 +55,12 @@ public void testInvalidValue() { IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document); Exception exception = expectThrows(IllegalArgumentException.class, () -> jsonProcessor.execute(ingestDocument)); - assertThat(exception.getCause().getMessage(), containsString("Unrecognized token 'blah': " + - "was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')")); + assertThat( + exception.getCause().getMessage(), + containsString( + "Unrecognized token 'blah': " + "was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')" + ) + ); } public void testByteArray() { @@ -68,9 +72,7 @@ public void testByteArray() { Exception exception = expectThrows(IllegalArgumentException.class, () -> jsonProcessor.execute(ingestDocument)); assertThat( exception.getCause().getMessage(), - containsString( - "Unrecognized token 'B': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')" - ) + containsString("Unrecognized token 'B': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')") ); } @@ -178,8 +180,10 @@ public void testDuplicateKeys() throws Exception { assertEquals("see", sourceAndMetadata.get("c")); JsonProcessor strictJsonProcessor = new JsonProcessor(processorTag, null, "a", null, true, REPLACE, false); - Exception exception = expectThrows(IllegalArgumentException.class, () -> - strictJsonProcessor.execute(RandomDocumentPicks.randomIngestDocument(random(), document))); + Exception exception = expectThrows( + IllegalArgumentException.class, + () -> strictJsonProcessor.execute(RandomDocumentPicks.randomIngestDocument(random(), document)) + ); assertThat(exception.getMessage(), containsString("Duplicate field 'a'")); } diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/KeyValueProcessorFactoryTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/KeyValueProcessorFactoryTests.java index b14d13d2d6e5d..3495428c95670 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/KeyValueProcessorFactoryTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/KeyValueProcessorFactoryTests.java @@ -73,8 +73,10 @@ public void testCreateWithAllFieldsSet() throws Exception { public void testCreateWithMissingField() { Map config = new HashMap<>(); String processorTag = randomAlphaOfLength(10); - ElasticsearchException exception = expectThrows(ElasticsearchParseException.class, - () -> factory.create(null, processorTag, null, config)); + ElasticsearchException exception = expectThrows( + ElasticsearchParseException.class, + () -> factory.create(null, processorTag, null, config) + ); assertThat(exception.getMessage(), equalTo("[field] required property is missing")); } @@ -82,8 +84,10 @@ public void testCreateWithMissingFieldSplit() { Map config = new HashMap<>(); config.put("field", "field1"); String processorTag = randomAlphaOfLength(10); - ElasticsearchException exception = expectThrows(ElasticsearchParseException.class, - () -> factory.create(null, processorTag, null, config)); + ElasticsearchException exception = expectThrows( + ElasticsearchParseException.class, + () -> factory.create(null, processorTag, null, config) + ); assertThat(exception.getMessage(), equalTo("[field_split] required property is missing")); } @@ -92,8 +96,10 @@ public void testCreateWithMissingValueSplit() { config.put("field", "field1"); config.put("field_split", "&"); String processorTag = randomAlphaOfLength(10); - ElasticsearchException exception = expectThrows(ElasticsearchParseException.class, - () -> factory.create(null, processorTag, null, config)); + ElasticsearchException exception = expectThrows( + ElasticsearchParseException.class, + () -> factory.create(null, processorTag, null, config) + ); assertThat(exception.getMessage(), equalTo("[value_split] required property is missing")); } } diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/KeyValueProcessorTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/KeyValueProcessorTests.java index ce1ab8d8811e8..44eed26117239 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/KeyValueProcessorTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/KeyValueProcessorTests.java @@ -42,7 +42,7 @@ public void test() throws Exception { public void testRootTarget() throws Exception { IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.emptyMap()); ingestDocument.setFieldValue("myField", "first=hello&second=world&second=universe"); - Processor processor = createKvProcessor("myField", "&", "=", null, null,null, false); + Processor processor = createKvProcessor("myField", "&", "=", null, null, null, false); processor.execute(ingestDocument); assertThat(ingestDocument.getFieldValue("first", String.class), equalTo("hello")); assertThat(ingestDocument.getFieldValue("second", List.class), equalTo(Arrays.asList("world", "universe"))); @@ -51,7 +51,7 @@ public void testRootTarget() throws Exception { public void testKeySameAsSourceField() throws Exception { IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.emptyMap()); ingestDocument.setFieldValue("first", "first=hello"); - Processor processor = createKvProcessor("first", "&", "=", null, null,null, false); + Processor processor = createKvProcessor("first", "&", "=", null, null, null, false); processor.execute(ingestDocument); assertThat(ingestDocument.getFieldValue("first", List.class), equalTo(Arrays.asList("first=hello", "hello"))); } @@ -59,8 +59,7 @@ public void testKeySameAsSourceField() throws Exception { public void testIncludeKeys() throws Exception { IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random()); String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, "first=hello&second=world&second=universe"); - Processor processor = createKvProcessor(fieldName, "&", "=", - Sets.newHashSet("first"), null, "target", false); + Processor processor = createKvProcessor(fieldName, "&", "=", Sets.newHashSet("first"), null, "target", false); processor.execute(ingestDocument); assertThat(ingestDocument.getFieldValue("target.first", String.class), equalTo("hello")); assertFalse(ingestDocument.hasField("target.second")); @@ -69,8 +68,7 @@ public void testIncludeKeys() throws Exception { public void testExcludeKeys() throws Exception { IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random()); String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, "first=hello&second=world&second=universe"); - Processor processor = createKvProcessor(fieldName, "&", "=", - null, Sets.newHashSet("second"), "target", false); + Processor processor = createKvProcessor(fieldName, "&", "=", null, Sets.newHashSet("second"), "target", false); processor.execute(ingestDocument); assertThat(ingestDocument.getFieldValue("target.first", String.class), equalTo("hello")); assertFalse(ingestDocument.hasField("target.second")); @@ -78,10 +76,20 @@ public void testExcludeKeys() throws Exception { public void testIncludeAndExcludeKeys() throws Exception { IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random()); - String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, - "first=hello&second=world&second=universe&third=bar"); - Processor processor = createKvProcessor(fieldName, "&", "=", - Sets.newHashSet("first", "second"), Sets.newHashSet("first", "second"), "target", false); + String fieldName = RandomDocumentPicks.addRandomField( + random(), + ingestDocument, + "first=hello&second=world&second=universe&third=bar" + ); + Processor processor = createKvProcessor( + fieldName, + "&", + "=", + Sets.newHashSet("first", "second"), + Sets.newHashSet("first", "second"), + "target", + false + ); processor.execute(ingestDocument); assertFalse(ingestDocument.hasField("target.first")); assertFalse(ingestDocument.hasField("target.second")); @@ -90,16 +98,17 @@ public void testIncludeAndExcludeKeys() throws Exception { public void testMissingField() throws Exception { IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.emptyMap()); - Processor processor = createKvProcessor("unknown", "&", - "=", null, null, "target", false); + Processor processor = createKvProcessor("unknown", "&", "=", null, null, "target", false); IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> processor.execute(ingestDocument)); assertThat(exception.getMessage(), equalTo("field [unknown] doesn't exist")); } public void testNullValueWithIgnoreMissing() throws Exception { String fieldName = RandomDocumentPicks.randomFieldName(random()); - IngestDocument originalIngestDocument = RandomDocumentPicks.randomIngestDocument(random(), - Collections.singletonMap(fieldName, null)); + IngestDocument originalIngestDocument = RandomDocumentPicks.randomIngestDocument( + random(), + Collections.singletonMap(fieldName, null) + ); IngestDocument ingestDocument = new IngestDocument(originalIngestDocument); Processor processor = createKvProcessor(fieldName, "", "", null, null, "target", true); processor.execute(ingestDocument); @@ -141,9 +150,11 @@ public void testTrimKeyAndValue() throws Exception { public void testTrimMultiCharSequence() throws Exception { IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random()); - String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, - "to=, orig_to=, %+relay=mail.example.com[private/dovecot-lmtp]," + - " delay=2.2, delays=1.9/0.01/0.01/0.21, dsn=2.0.0, status=sent " + String fieldName = RandomDocumentPicks.addRandomField( + random(), + ingestDocument, + "to=, orig_to=, %+relay=mail.example.com[private/dovecot-lmtp]," + + " delay=2.2, delays=1.9/0.01/0.01/0.21, dsn=2.0.0, status=sent " ); Processor processor = createKvProcessor(fieldName, " ", "=", null, null, "target", false, "%+", "<>,", false, null); processor.execute(ingestDocument); @@ -159,7 +170,9 @@ public void testTrimMultiCharSequence() throws Exception { public void testStripBrackets() throws Exception { IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random()); String fieldName = RandomDocumentPicks.addRandomField( - random(), ingestDocument, "first=&second=\"world\"&second=(universe)&third=&fourth=[bar]&fifth='last'" + random(), + ingestDocument, + "first=&second=\"world\"&second=(universe)&third=&fourth=[bar]&fifth='last'" ); Processor processor = createKvProcessor(fieldName, "&", "=", null, null, "target", false, null, null, true, null); processor.execute(ingestDocument); @@ -179,18 +192,43 @@ public void testAddPrefix() throws Exception { assertThat(ingestDocument.getFieldValue("target.arg_second", List.class), equalTo(Arrays.asList("world", "universe"))); } - private static KeyValueProcessor createKvProcessor(String field, String fieldSplit, String valueSplit, Set includeKeys, - Set excludeKeys, String targetField, - boolean ignoreMissing) throws Exception { + private static KeyValueProcessor createKvProcessor( + String field, + String fieldSplit, + String valueSplit, + Set includeKeys, + Set excludeKeys, + String targetField, + boolean ignoreMissing + ) throws Exception { return createKvProcessor( - field, fieldSplit, valueSplit, includeKeys, excludeKeys, targetField, ignoreMissing, null, null, false, null + field, + fieldSplit, + valueSplit, + includeKeys, + excludeKeys, + targetField, + ignoreMissing, + null, + null, + false, + null ); } - private static KeyValueProcessor createKvProcessor(String field, String fieldSplit, String valueSplit, Set includeKeys, - Set excludeKeys, String targetField, boolean ignoreMissing, - String trimKey, String trimValue, boolean stripBrackets, - String prefix) throws Exception { + private static KeyValueProcessor createKvProcessor( + String field, + String fieldSplit, + String valueSplit, + Set includeKeys, + Set excludeKeys, + String targetField, + boolean ignoreMissing, + String trimKey, + String trimValue, + boolean stripBrackets, + String prefix + ) throws Exception { Map config = new HashMap<>(); config.put("field", field); config.put("field_split", fieldSplit); diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/NetworkDirectionProcessorFactoryTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/NetworkDirectionProcessorFactoryTests.java index 47a12625eed5b..deece8362678c 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/NetworkDirectionProcessorFactoryTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/NetworkDirectionProcessorFactoryTests.java @@ -9,13 +9,13 @@ package org.elasticsearch.ingest.common; import org.elasticsearch.ElasticsearchParseException; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.ingest.TestTemplateService; +import org.elasticsearch.test.ESTestCase; import org.junit.Before; import java.util.ArrayList; -import java.util.HashMap; import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/NetworkDirectionProcessorTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/NetworkDirectionProcessorTests.java index 7788ba1963c15..51bde3967179e 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/NetworkDirectionProcessorTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/NetworkDirectionProcessorTests.java @@ -8,16 +8,15 @@ package org.elasticsearch.ingest.common; -import org.elasticsearch.ingest.IngestDocument; -import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.ingest.TestTemplateService; import org.elasticsearch.ElasticsearchParseException; +import org.elasticsearch.ingest.IngestDocument; import org.elasticsearch.ingest.TestTemplateService; +import org.elasticsearch.test.ESTestCase; +import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; -import java.util.ArrayList; import java.util.Map; import static org.elasticsearch.ingest.common.NetworkDirectionProcessor.Factory.DEFAULT_TARGET; @@ -167,16 +166,12 @@ public void testInternalNetworksAndField() throws Exception { config.put("internal_networks", networks); ElasticsearchParseException e = expectThrows( ElasticsearchParseException.class, - () -> new NetworkDirectionProcessor.Factory(TestTemplateService.instance()).create( - null, - processorTag, - null, - config - ) + () -> new NetworkDirectionProcessor.Factory(TestTemplateService.instance()).create(null, processorTag, null, config) + ); + assertThat( + e.getMessage(), + containsString("[internal_networks] and [internal_networks_field] cannot both be used in the same processor") ); - assertThat(e.getMessage(), containsString( - "[internal_networks] and [internal_networks_field] cannot both be used in the same processor" - )); } private void testNetworkDirectionProcessor( diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/RegisteredDomainProcessorFactoryTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/RegisteredDomainProcessorFactoryTests.java index f47db1e9a09a1..f5100298c63d5 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/RegisteredDomainProcessorFactoryTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/RegisteredDomainProcessorFactoryTests.java @@ -54,7 +54,6 @@ public void testCreateDefaults() throws Exception { assertThat(publicSuffixProcessor.getTargetField(), equalTo(RegisteredDomainProcessor.Factory.DEFAULT_TARGET_FIELD)); } - public void testFieldRequired() throws Exception { HashMap config = new HashMap<>(); String processorTag = randomAlphaOfLength(10); diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/RegisteredDomainProcessorTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/RegisteredDomainProcessorTests.java index c75ae93a1923e..f63aff0518072 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/RegisteredDomainProcessorTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/RegisteredDomainProcessorTests.java @@ -40,13 +40,7 @@ public void testBasic() throws Exception { "global.ssl.fastly.net", "1" ); - testRegisteredDomainProcessor( - buildEvent("www.books.amazon.co.uk"), - "www.books.amazon.co.uk", - "amazon.co.uk", - "co.uk", - "www.books" - ); + testRegisteredDomainProcessor(buildEvent("www.books.amazon.co.uk"), "www.books.amazon.co.uk", "amazon.co.uk", "co.uk", "www.books"); } public void testUseRoot() throws Exception { @@ -57,13 +51,7 @@ public void testUseRoot() throws Exception { String topLevelDomainField = "top_level_domain"; String subdomainField = "subdomain"; - var processor = new RegisteredDomainProcessor( - null, - null, - "domain", - "", - false - ); + var processor = new RegisteredDomainProcessor(null, null, "domain", "", false); IngestDocument input = new IngestDocument(source, Map.of()); IngestDocument output = processor.execute(input); @@ -85,16 +73,9 @@ public void testError() throws Exception { ); assertThat(e.getMessage(), containsString("unable to set domain information for document")); e = expectThrows( - IllegalArgumentException.class, - () -> testRegisteredDomainProcessor( - buildEvent("$"), - null, - null, - null, - null, - false - ) - ); + IllegalArgumentException.class, + () -> testRegisteredDomainProcessor(buildEvent("$"), null, null, null, null, false) + ); assertThat(e.getMessage(), containsString("unable to set domain information for document")); } @@ -121,13 +102,7 @@ private void testRegisteredDomainProcessor( String topLevelDomainField = "url.top_level_domain"; String subdomainField = "url.subdomain"; - var processor = new RegisteredDomainProcessor( - null, - null, - "domain", - "url", - ignoreMissing - ); + var processor = new RegisteredDomainProcessor(null, null, "domain", "url", ignoreMissing); IngestDocument input = new IngestDocument(source, Map.of()); IngestDocument output = processor.execute(input); diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/RemoveProcessorFactoryTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/RemoveProcessorFactoryTests.java index 8a76a96df7e3b..5149a4a135abd 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/RemoveProcessorFactoryTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/RemoveProcessorFactoryTests.java @@ -46,9 +46,13 @@ public void testCreateMultipleFields() throws Exception { String processorTag = randomAlphaOfLength(10); RemoveProcessor removeProcessor = factory.create(null, processorTag, null, config); assertThat(removeProcessor.getTag(), equalTo(processorTag)); - assertThat(removeProcessor.getFields().stream() - .map(template -> template.newInstance(Collections.emptyMap()).execute()) - .collect(Collectors.toList()), equalTo(Arrays.asList("field1", "field2"))); + assertThat( + removeProcessor.getFields() + .stream() + .map(template -> template.newInstance(Collections.emptyMap()).execute()) + .collect(Collectors.toList()), + equalTo(Arrays.asList("field1", "field2")) + ); } public void testCreateMissingField() throws Exception { @@ -56,7 +60,7 @@ public void testCreateMissingField() throws Exception { try { factory.create(null, null, null, config); fail("factory create should have failed"); - } catch(ElasticsearchParseException e) { + } catch (ElasticsearchParseException e) { assertThat(e.getMessage(), equalTo("[field] required property is missing")); } } @@ -66,8 +70,10 @@ public void testInvalidMustacheTemplate() throws Exception { Map config = new HashMap<>(); config.put("field", "{{field1}}"); String processorTag = randomAlphaOfLength(10); - ElasticsearchException exception = expectThrows(ElasticsearchException.class, - () -> factory.create(null, processorTag, null, config)); + ElasticsearchException exception = expectThrows( + ElasticsearchException.class, + () -> factory.create(null, processorTag, null, config) + ); assertThat(exception.getMessage(), equalTo("java.lang.RuntimeException: could not compile script")); assertThat(exception.getMetadata("es.processor_tag").get(0), equalTo(processorTag)); } diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/RemoveProcessorTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/RemoveProcessorTests.java index bb74ede8378a7..097c36d3d8ab1 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/RemoveProcessorTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/RemoveProcessorTests.java @@ -26,8 +26,12 @@ public class RemoveProcessorTests extends ESTestCase { public void testRemoveFields() throws Exception { IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random()); String field = RandomDocumentPicks.randomExistingFieldName(random(), ingestDocument); - Processor processor = new RemoveProcessor(randomAlphaOfLength(10), - null, Collections.singletonList(new TestTemplateService.MockTemplateScript.Factory(field)), false); + Processor processor = new RemoveProcessor( + randomAlphaOfLength(10), + null, + Collections.singletonList(new TestTemplateService.MockTemplateScript.Factory(field)), + false + ); processor.execute(ingestDocument); assertThat(ingestDocument.hasField(field), equalTo(false)); } @@ -42,7 +46,7 @@ public void testRemoveNonExistingField() throws Exception { try { processor.execute(ingestDocument); fail("remove field should have failed"); - } catch(IllegalArgumentException e) { + } catch (IllegalArgumentException e) { assertThat(e.getMessage(), containsString("not present as part of path [" + fieldName + "]")); } } diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/RenameProcessorFactoryTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/RenameProcessorFactoryTests.java index 83a4f0d8b5ba1..ad1f6f0962deb 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/RenameProcessorFactoryTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/RenameProcessorFactoryTests.java @@ -59,7 +59,7 @@ public void testCreateNoFieldPresent() throws Exception { try { factory.create(null, null, null, config); fail("factory create should have failed"); - } catch(ElasticsearchParseException e) { + } catch (ElasticsearchParseException e) { assertThat(e.getMessage(), equalTo("[field] required property is missing")); } } @@ -70,7 +70,7 @@ public void testCreateNoToPresent() throws Exception { try { factory.create(null, null, null, config); fail("factory create should have failed"); - } catch(ElasticsearchParseException e) { + } catch (ElasticsearchParseException e) { assertThat(e.getMessage(), equalTo("[target_field] required property is missing")); } } diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/RenameProcessorTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/RenameProcessorTests.java index 160ba4871ff46..518da7061aa4c 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/RenameProcessorTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/RenameProcessorTests.java @@ -70,7 +70,7 @@ public void testRenameArrayElement() throws Exception { try { processor.execute(ingestDocument); fail("processor execute should have failed"); - } catch(IllegalArgumentException e) { + } catch (IllegalArgumentException e) { assertThat(e.getMessage(), equalTo("[3] is out of bounds for array with length [2] as part of path [list.3]")); assertThat(actualList.size(), equalTo(2)); assertThat(actualList.get(0), equalTo("item2")); @@ -81,12 +81,11 @@ public void testRenameArrayElement() throws Exception { public void testRenameNonExistingField() throws Exception { IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>()); String fieldName = RandomDocumentPicks.randomFieldName(random()); - Processor processor = createRenameProcessor(fieldName, - RandomDocumentPicks.randomFieldName(random()), false); + Processor processor = createRenameProcessor(fieldName, RandomDocumentPicks.randomFieldName(random()), false); try { processor.execute(ingestDocument); fail("processor execute should have failed"); - } catch(IllegalArgumentException e) { + } catch (IllegalArgumentException e) { assertThat(e.getMessage(), equalTo("field [" + fieldName + "] doesn't exist")); } } @@ -95,13 +94,11 @@ public void testRenameNonExistingFieldWithIgnoreMissing() throws Exception { IngestDocument originalIngestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>()); IngestDocument ingestDocument = new IngestDocument(originalIngestDocument); String fieldName = RandomDocumentPicks.randomFieldName(random()); - Processor processor = createRenameProcessor(fieldName, - RandomDocumentPicks.randomFieldName(random()), true); + Processor processor = createRenameProcessor(fieldName, RandomDocumentPicks.randomFieldName(random()), true); processor.execute(ingestDocument); assertIngestDocument(originalIngestDocument, ingestDocument); - Processor processor1 = createRenameProcessor("", - RandomDocumentPicks.randomFieldName(random()), true); + Processor processor1 = createRenameProcessor("", RandomDocumentPicks.randomFieldName(random()), true); processor1.execute(ingestDocument); assertIngestDocument(originalIngestDocument, ingestDocument); } @@ -109,12 +106,15 @@ public void testRenameNonExistingFieldWithIgnoreMissing() throws Exception { public void testRenameNewFieldAlreadyExists() throws Exception { IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random()); String fieldName = RandomDocumentPicks.randomExistingFieldName(random(), ingestDocument); - Processor processor = createRenameProcessor(RandomDocumentPicks.randomExistingFieldName( - random(), ingestDocument), fieldName, false); + Processor processor = createRenameProcessor( + RandomDocumentPicks.randomExistingFieldName(random(), ingestDocument), + fieldName, + false + ); try { processor.execute(ingestDocument); fail("processor execute should have failed"); - } catch(IllegalArgumentException e) { + } catch (IllegalArgumentException e) { assertThat(e.getMessage(), equalTo("field [" + fieldName + "] already exists")); } } @@ -152,8 +152,8 @@ public Object put(String key, Object value) { try { processor.execute(ingestDocument); fail("processor execute should have failed"); - } catch(UnsupportedOperationException e) { - //the set failed, the old field has not been removed + } catch (UnsupportedOperationException e) { + // the set failed, the old field has not been removed assertThat(ingestDocument.getSourceAndMetadata().containsKey("list"), equalTo(true)); assertThat(ingestDocument.getSourceAndMetadata().containsKey("new_field"), equalTo(false)); } @@ -177,7 +177,7 @@ public Object remove(Object key) { processor.execute(ingestDocument); fail("processor execute should have failed"); } catch (UnsupportedOperationException e) { - //the set failed, the old field has not been removed + // the set failed, the old field has not been removed assertThat(ingestDocument.getSourceAndMetadata().containsKey("list"), equalTo(true)); assertThat(ingestDocument.getSourceAndMetadata().containsKey("new_field"), equalTo(false)); } @@ -192,10 +192,12 @@ public void testRenameLeafIntoBranch() throws Exception { assertThat(ingestDocument.getFieldValue("foo", Map.class), equalTo(Collections.singletonMap("bar", "bar"))); assertThat(ingestDocument.getFieldValue("foo.bar", String.class), equalTo("bar")); - Processor processor2 = createRenameProcessor( "foo.bar", "foo.bar.baz", false); + Processor processor2 = createRenameProcessor("foo.bar", "foo.bar.baz", false); processor2.execute(ingestDocument); - assertThat(ingestDocument.getFieldValue("foo", Map.class), equalTo(Collections.singletonMap("bar", - Collections.singletonMap("baz", "bar")))); + assertThat( + ingestDocument.getFieldValue("foo", Map.class), + equalTo(Collections.singletonMap("bar", Collections.singletonMap("baz", "bar"))) + ); assertThat(ingestDocument.getFieldValue("foo.bar", Map.class), equalTo(Collections.singletonMap("baz", "bar"))); assertThat(ingestDocument.getFieldValue("foo.bar.baz", String.class), equalTo("bar")); @@ -206,7 +208,12 @@ public void testRenameLeafIntoBranch() throws Exception { } private RenameProcessor createRenameProcessor(String field, String targetField, boolean ignoreMissing) { - return new RenameProcessor(randomAlphaOfLength(10), null, new TestTemplateService.MockTemplateScript.Factory(field), - new TestTemplateService.MockTemplateScript.Factory(targetField), ignoreMissing); + return new RenameProcessor( + randomAlphaOfLength(10), + null, + new TestTemplateService.MockTemplateScript.Factory(field), + new TestTemplateService.MockTemplateScript.Factory(targetField), + ignoreMissing + ); } } diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/ScriptProcessorFactoryTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/ScriptProcessorFactoryTests.java index 5628d778abca1..2f025802a6138 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/ScriptProcessorFactoryTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/ScriptProcessorFactoryTests.java @@ -10,7 +10,6 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentParseException; import org.elasticsearch.script.IngestScript; import org.elasticsearch.script.MockScriptEngine; import org.elasticsearch.script.Script; @@ -19,6 +18,7 @@ import org.elasticsearch.script.ScriptService; import org.elasticsearch.script.ScriptType; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentParseException; import org.junit.Before; import java.util.Collections; @@ -35,9 +35,7 @@ public class ScriptProcessorFactoryTests extends ESTestCase { private ScriptProcessor.Factory factory; - private static final Map INGEST_SCRIPT_PARAM_TO_TYPE = Map.of( - "id", "stored", - "source", "inline"); + private static final Map INGEST_SCRIPT_PARAM_TO_TYPE = Map.of("id", "stored", "source", "inline"); @Before public void init() { @@ -80,8 +78,10 @@ public void testFactoryValidationForMultipleScriptingTypes() throws Exception { configMap.put("source", "bar"); configMap.put("lang", "mockscript"); - XContentParseException exception = expectThrows(XContentParseException.class, - () -> factory.create(null, randomAlphaOfLength(10), null, configMap)); + XContentParseException exception = expectThrows( + XContentParseException.class, + () -> factory.create(null, randomAlphaOfLength(10), null, configMap) + ); assertThat(exception.getMessage(), containsString("[script] failed to parse field [source]")); } @@ -89,8 +89,10 @@ public void testFactoryValidationAtLeastOneScriptingType() throws Exception { Map configMap = new HashMap<>(); configMap.put("lang", "mockscript"); - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, - () -> factory.create(null, randomAlphaOfLength(10), null, configMap)); + IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> factory.create(null, randomAlphaOfLength(10), null, configMap) + ); assertThat(exception.getMessage(), is("must specify either [source] for an inline script or [id] for a stored script")); } @@ -110,33 +112,40 @@ public void testInlineBackcompat() throws Exception { public void testFactoryInvalidateWithInvalidCompiledScript() throws Exception { String randomType = randomFrom("source", "id"); ScriptService mockedScriptService = mock(ScriptService.class); - ScriptException thrownException = new ScriptException("compile-time exception", new RuntimeException(), - Collections.emptyList(), "script", "mockscript"); + ScriptException thrownException = new ScriptException( + "compile-time exception", + new RuntimeException(), + Collections.emptyList(), + "script", + "mockscript" + ); when(mockedScriptService.compile(any(), any())).thenThrow(thrownException); factory = new ScriptProcessor.Factory(mockedScriptService); Map configMap = new HashMap<>(); configMap.put(randomType, "my_script"); - ElasticsearchException exception = expectThrows(ElasticsearchException.class, - () -> factory.create(null, randomAlphaOfLength(10), null, configMap)); + ElasticsearchException exception = expectThrows( + ElasticsearchException.class, + () -> factory.create(null, randomAlphaOfLength(10), null, configMap) + ); assertThat(exception.getMessage(), is("compile-time exception")); } public void testInlineIsCompiled() throws Exception { String scriptName = "foo"; - ScriptService scriptService = new ScriptService(Settings.builder().build(), + ScriptService scriptService = new ScriptService( + Settings.builder().build(), Collections.singletonMap( - Script.DEFAULT_SCRIPT_LANG, new MockScriptEngine( - Script.DEFAULT_SCRIPT_LANG, - Collections.singletonMap(scriptName, ctx -> { - ctx.put("foo", "bar"); - return null; - }), - Collections.emptyMap() - ) - ), new HashMap<>(ScriptModule.CORE_CONTEXTS)); + Script.DEFAULT_SCRIPT_LANG, + new MockScriptEngine(Script.DEFAULT_SCRIPT_LANG, Collections.singletonMap(scriptName, ctx -> { + ctx.put("foo", "bar"); + return null; + }), Collections.emptyMap()) + ), + new HashMap<>(ScriptModule.CORE_CONTEXTS) + ); factory = new ScriptProcessor.Factory(scriptService); Map configMap = new HashMap<>(); diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/ScriptProcessorTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/ScriptProcessorTests.java index bae8ed6f3023f..5e12c15d2bba7 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/ScriptProcessorTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/ScriptProcessorTests.java @@ -37,21 +37,17 @@ public class ScriptProcessorTests extends ESTestCase { @Before public void setupScripting() { String scriptName = "script"; - scriptService = new ScriptService(Settings.builder().build(), + scriptService = new ScriptService( + Settings.builder().build(), Collections.singletonMap( - Script.DEFAULT_SCRIPT_LANG, new MockScriptEngine( - Script.DEFAULT_SCRIPT_LANG, - Collections.singletonMap( - scriptName, ctx -> { - Integer bytesIn = (Integer) ctx.get("bytes_in"); - Integer bytesOut = (Integer) ctx.get("bytes_out"); - ctx.put("bytes_total", bytesIn + bytesOut); - ctx.put("_dynamic_templates", Map.of("foo", "bar")); - return null; - } - ), - Collections.emptyMap() - ) + Script.DEFAULT_SCRIPT_LANG, + new MockScriptEngine(Script.DEFAULT_SCRIPT_LANG, Collections.singletonMap(scriptName, ctx -> { + Integer bytesIn = (Integer) ctx.get("bytes_in"); + Integer bytesOut = (Integer) ctx.get("bytes_out"); + ctx.put("bytes_total", bytesIn + bytesOut); + ctx.put("_dynamic_templates", Map.of("foo", "bar")); + return null; + }), Collections.emptyMap()) ), new HashMap<>(ScriptModule.CORE_CONTEXTS) ); diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/SetProcessorFactoryTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/SetProcessorFactoryTests.java index 4e48736c2c8d0..1c873e02e039b 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/SetProcessorFactoryTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/SetProcessorFactoryTests.java @@ -64,7 +64,7 @@ public void testCreateNoFieldPresent() throws Exception { try { factory.create(null, null, null, config); fail("factory create should have failed"); - } catch(ElasticsearchParseException e) { + } catch (ElasticsearchParseException e) { assertThat(e.getMessage(), equalTo("[field] required property is missing")); } } @@ -75,7 +75,7 @@ public void testCreateNoValuePresent() throws Exception { try { factory.create(null, null, null, config); fail("factory create should have failed"); - } catch(ElasticsearchParseException e) { + } catch (ElasticsearchParseException e) { assertThat(e.getMessage(), equalTo("[value] required property is missing")); } } @@ -87,7 +87,7 @@ public void testCreateNullValue() throws Exception { try { factory.create(null, null, null, config); fail("factory create should have failed"); - } catch(ElasticsearchParseException e) { + } catch (ElasticsearchParseException e) { assertThat(e.getMessage(), equalTo("[value] required property is missing")); } } @@ -98,8 +98,10 @@ public void testInvalidMustacheTemplate() throws Exception { config.put("field", "{{field1}}"); config.put("value", "value1"); String processorTag = randomAlphaOfLength(10); - ElasticsearchException exception = expectThrows(ElasticsearchException.class, - () -> factory.create(null, processorTag, null, config)); + ElasticsearchException exception = expectThrows( + ElasticsearchException.class, + () -> factory.create(null, processorTag, null, config) + ); assertThat(exception.getMessage(), equalTo("java.lang.RuntimeException: could not compile script")); assertThat(exception.getMetadata("es.processor_tag").get(0), equalTo(processorTag)); } @@ -121,8 +123,10 @@ public void testCreateWithCopyFromAndValue() throws Exception { config.put("copy_from", "field2"); config.put("value", "value1"); String processorTag = randomAlphaOfLength(10); - ElasticsearchException exception = expectThrows(ElasticsearchException.class, - () -> factory.create(null, processorTag, null, config)); + ElasticsearchException exception = expectThrows( + ElasticsearchException.class, + () -> factory.create(null, processorTag, null, config) + ); assertThat(exception.getMessage(), equalTo("[copy_from] cannot set both `copy_from` and `value` in the same processor")); } @@ -138,8 +142,10 @@ public void testMediaType() throws Exception { assertThat(setProcessor.getTag(), equalTo(processorTag)); // invalid media type - expectedMediaType = randomValueOtherThanMany(m -> Arrays.asList(ConfigurationUtils.VALID_MEDIA_TYPES).contains(m), - () -> randomAlphaOfLengthBetween(5, 9)); + expectedMediaType = randomValueOtherThanMany( + m -> Arrays.asList(ConfigurationUtils.VALID_MEDIA_TYPES).contains(m), + () -> randomAlphaOfLengthBetween(5, 9) + ); final Map config2 = new HashMap<>(); config2.put("field", "field1"); config2.put("value", "value1"); diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/SetProcessorTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/SetProcessorTests.java index fefe8f918dfda..9001992f20f52 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/SetProcessorTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/SetProcessorTests.java @@ -44,7 +44,7 @@ public void testSetExistingFields() throws Exception { public void testSetNewFields() throws Exception { IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>()); - //used to verify that there are no conflicts between subsequent fields going to be added + // used to verify that there are no conflicts between subsequent fields going to be added IngestDocument testIngestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>()); Object fieldValue = RandomDocumentPicks.randomFieldValue(random()); String fieldName = RandomDocumentPicks.addRandomField(random(), testIngestDocument, fieldValue); @@ -61,9 +61,11 @@ public void testSetFieldsTypeMismatch() throws Exception { try { processor.execute(ingestDocument); fail("processor execute should have failed"); - } catch(IllegalArgumentException e) { - assertThat(e.getMessage(), equalTo("cannot set [inner] with parent object of type [java.lang.String] as " + - "part of path [field.inner]")); + } catch (IllegalArgumentException e) { + assertThat( + e.getMessage(), + equalTo("cannot set [inner] with parent object of type [java.lang.String] as " + "part of path [field.inner]") + ); } } @@ -142,7 +144,8 @@ public void testSetDynamicTemplates() throws Exception { IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random()); int iters = between(1, 3); for (int i = 0; i < iters; i++) { - Map dynamicTemplates = IntStream.range(0, between(0, 3)).boxed() + Map dynamicTemplates = IntStream.range(0, between(0, 3)) + .boxed() .collect(Collectors.toMap(n -> "field-" + n, n -> randomFrom("int", "geo_point", "keyword"))); Processor processor = createSetProcessor(Metadata.DYNAMIC_TEMPLATES.getFieldName(), dynamicTemplates, null, true, false); processor.execute(ingestDocument); @@ -251,9 +254,21 @@ public void testCopyFromDeepCopiesNonPrimitiveMutableTypes() throws Exception { assertThat(ingestDocument.getFieldValue(targetField, Object.class), equalTo(preservedDate)); } - private static Processor createSetProcessor(String fieldName, Object fieldValue, String copyFrom, boolean overrideEnabled, - boolean ignoreEmptyValue) { - return new SetProcessor(randomAlphaOfLength(10), null, new TestTemplateService.MockTemplateScript.Factory(fieldName), - ValueSource.wrap(fieldValue, TestTemplateService.instance()), copyFrom, overrideEnabled, ignoreEmptyValue); + private static Processor createSetProcessor( + String fieldName, + Object fieldValue, + String copyFrom, + boolean overrideEnabled, + boolean ignoreEmptyValue + ) { + return new SetProcessor( + randomAlphaOfLength(10), + null, + new TestTemplateService.MockTemplateScript.Factory(fieldName), + ValueSource.wrap(fieldValue, TestTemplateService.instance()), + copyFrom, + overrideEnabled, + ignoreEmptyValue + ); } } diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/SortProcessorFactoryTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/SortProcessorFactoryTests.java index 3919a1ff2ea63..51abbf3058379 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/SortProcessorFactoryTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/SortProcessorFactoryTests.java @@ -90,7 +90,7 @@ public void testCreateMissingField() throws Exception { try { factory.create(null, null, null, config); fail("factory create should have failed"); - } catch(ElasticsearchParseException e) { + } catch (ElasticsearchParseException e) { assertThat(e.getMessage(), equalTo("[field] required property is missing")); } } diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/SortProcessorTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/SortProcessorTests.java index 71c623d726a76..7d5a642166544 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/SortProcessorTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/SortProcessorTests.java @@ -51,7 +51,7 @@ public void testSortStrings() throws Exception { public void testSortIntegersNonRandom() throws Exception { IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random()); - Integer[] expectedResult = new Integer[]{1,2,3,4,5,10,20,21,22,50,100}; + Integer[] expectedResult = new Integer[] { 1, 2, 3, 4, 5, 10, 20, 21, 22, 50, 100 }; List fieldValue = new ArrayList<>(expectedResult.length); fieldValue.addAll(Arrays.asList(expectedResult).subList(0, expectedResult.length)); Collections.shuffle(fieldValue, random()); @@ -236,7 +236,7 @@ public void testSortNonListField() throws Exception { Processor processor = new SortProcessor(randomAlphaOfLength(10), null, fieldName, order, fieldName); try { processor.execute(ingestDocument); - } catch(IllegalArgumentException e) { + } catch (IllegalArgumentException e) { assertThat(e.getMessage(), equalTo("field [" + fieldName + "] of type [java.lang.String] cannot be cast to [java.util.List]")); } } @@ -248,7 +248,7 @@ public void testSortNonExistingField() throws Exception { Processor processor = new SortProcessor(randomAlphaOfLength(10), null, fieldName, order, fieldName); try { processor.execute(ingestDocument); - } catch(IllegalArgumentException e) { + } catch (IllegalArgumentException e) { assertThat(e.getMessage(), containsString("not present as part of path [" + fieldName + "]")); } } @@ -259,7 +259,7 @@ public void testSortNullValue() throws Exception { Processor processor = new SortProcessor(randomAlphaOfLength(10), null, "field", order, "field"); try { processor.execute(ingestDocument); - } catch(IllegalArgumentException e) { + } catch (IllegalArgumentException e) { assertThat(e.getMessage(), equalTo("field [field] is null, cannot sort.")); } } @@ -279,8 +279,7 @@ public void testDescendingSortWithTargetField() throws Exception { String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, fieldValue); String targetFieldName = fieldName + "foo"; - Processor processor = new SortProcessor(randomAlphaOfLength(10), null, fieldName, - SortOrder.DESCENDING, targetFieldName); + Processor processor = new SortProcessor(randomAlphaOfLength(10), null, fieldName, SortOrder.DESCENDING, targetFieldName); processor.execute(ingestDocument); assertEquals(ingestDocument.getFieldValue(targetFieldName, List.class), expectedResult); } @@ -300,8 +299,7 @@ public void testAscendingSortWithTargetField() throws Exception { String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, fieldValue); String targetFieldName = fieldName + "foo"; - Processor processor = new SortProcessor(randomAlphaOfLength(10), null, fieldName, - SortOrder.ASCENDING, targetFieldName); + Processor processor = new SortProcessor(randomAlphaOfLength(10), null, fieldName, SortOrder.ASCENDING, targetFieldName); processor.execute(ingestDocument); assertEquals(ingestDocument.getFieldValue(targetFieldName, List.class), expectedResult); } diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/SplitProcessorFactoryTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/SplitProcessorFactoryTests.java index c9de26202442d..891f67bd45a62 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/SplitProcessorFactoryTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/SplitProcessorFactoryTests.java @@ -39,7 +39,7 @@ public void testCreateNoFieldPresent() throws Exception { try { factory.create(null, null, null, config); fail("factory create should have failed"); - } catch(ElasticsearchParseException e) { + } catch (ElasticsearchParseException e) { assertThat(e.getMessage(), equalTo("[field] required property is missing")); } } @@ -51,7 +51,7 @@ public void testCreateNoSeparatorPresent() throws Exception { try { factory.create(null, null, null, config); fail("factory create should have failed"); - } catch(ElasticsearchParseException e) { + } catch (ElasticsearchParseException e) { assertThat(e.getMessage(), equalTo("[separator] required property is missing")); } } diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/SplitProcessorTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/SplitProcessorTests.java index 8ad2e60fce579..f25a585f0393c 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/SplitProcessorTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/SplitProcessorTests.java @@ -46,8 +46,7 @@ public void testSplitFieldNotFound() throws Exception { } public void testSplitNullValue() throws Exception { - IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), - Collections.singletonMap("field", null)); + IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.singletonMap("field", null)); Processor processor = new SplitProcessor(randomAlphaOfLength(10), null, "field", "\\.", false, false, "field"); try { processor.execute(ingestDocument); @@ -59,8 +58,10 @@ public void testSplitNullValue() throws Exception { public void testSplitNullValueWithIgnoreMissing() throws Exception { String fieldName = RandomDocumentPicks.randomFieldName(random()); - IngestDocument originalIngestDocument = RandomDocumentPicks.randomIngestDocument(random(), - Collections.singletonMap(fieldName, null)); + IngestDocument originalIngestDocument = RandomDocumentPicks.randomIngestDocument( + random(), + Collections.singletonMap(fieldName, null) + ); IngestDocument ingestDocument = new IngestDocument(originalIngestDocument); Processor processor = new SplitProcessor(randomAlphaOfLength(10), null, fieldName, "\\.", true, false, fieldName); processor.execute(ingestDocument); @@ -84,8 +85,10 @@ public void testSplitNonStringValue() throws Exception { processor.execute(ingestDocument); fail("split processor should have failed"); } catch (IllegalArgumentException e) { - assertThat(e.getMessage(), equalTo("field [" + fieldName + "] of type [java.lang.Integer] cannot be cast " + - "to [java.lang.String]")); + assertThat( + e.getMessage(), + equalTo("field [" + fieldName + "] of type [java.lang.Integer] cannot be cast " + "to [java.lang.String]") + ); } } @@ -99,11 +102,13 @@ public void testSplitAppendable() throws Exception { IngestDocument ingestDocument = new IngestDocument(source, new HashMap<>()); splitProcessor.execute(ingestDocument); @SuppressWarnings("unchecked") - List flags = (List)ingestDocument.getFieldValue("flags", List.class); + List flags = (List) ingestDocument.getFieldValue("flags", List.class); assertThat(flags, equalTo(Arrays.asList("new", "hot", "super", "fun", "interesting"))); ingestDocument.appendFieldValue("flags", "additional_flag"); - assertThat(ingestDocument.getFieldValue("flags", List.class), equalTo(Arrays.asList("new", "hot", "super", - "fun", "interesting", "additional_flag"))); + assertThat( + ingestDocument.getFieldValue("flags", List.class), + equalTo(Arrays.asList("new", "hot", "super", "fun", "interesting", "additional_flag")) + ); } public void testSplitWithTargetField() throws Exception { diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/UriPartsProcessorTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/UriPartsProcessorTests.java index 40b8bb24f65d9..e22fc374736a3 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/UriPartsProcessorTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/UriPartsProcessorTests.java @@ -148,7 +148,7 @@ public void testUriParts() throws Exception { ); } - public void testUrlWithCharactersNotToleratedByUri() throws Exception { + public void testUrlWithCharactersNotToleratedByUri() throws Exception { testUriParsing( "http://www.google.com/path with spaces", Map.of("scheme", "http", "domain", "www.google.com", "path", "/path with spaces") diff --git a/modules/ingest-common/src/yamlRestTest/java/org/elasticsearch/ingest/common/IngestCommonClientYamlTestSuiteIT.java b/modules/ingest-common/src/yamlRestTest/java/org/elasticsearch/ingest/common/IngestCommonClientYamlTestSuiteIT.java index 813c236e2c84c..fa9e4d32000f7 100644 --- a/modules/ingest-common/src/yamlRestTest/java/org/elasticsearch/ingest/common/IngestCommonClientYamlTestSuiteIT.java +++ b/modules/ingest-common/src/yamlRestTest/java/org/elasticsearch/ingest/common/IngestCommonClientYamlTestSuiteIT.java @@ -25,4 +25,3 @@ public static Iterable parameters() throws Exception { return ESClientYamlSuiteTestCase.createParameters(); } } - diff --git a/modules/ingest-geoip/qa/file-based-update/src/test/java/org/elasticsearch/ingest/geoip/UpdateDatabasesIT.java b/modules/ingest-geoip/qa/file-based-update/src/test/java/org/elasticsearch/ingest/geoip/UpdateDatabasesIT.java index 24fda3ed4021b..f79b9e01aa854 100644 --- a/modules/ingest-geoip/qa/file-based-update/src/test/java/org/elasticsearch/ingest/geoip/UpdateDatabasesIT.java +++ b/modules/ingest-geoip/qa/file-based-update/src/test/java/org/elasticsearch/ingest/geoip/UpdateDatabasesIT.java @@ -12,8 +12,8 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.core.PathUtils; -import org.elasticsearch.xcontent.ObjectPath; import org.elasticsearch.test.rest.ESRestTestCase; +import org.elasticsearch.xcontent.ObjectPath; import java.io.IOException; import java.nio.file.Files; @@ -30,8 +30,8 @@ public class UpdateDatabasesIT extends ESRestTestCase { public void test() throws Exception { - String body = "{\"pipeline\":{\"processors\":[{\"geoip\":{\"field\":\"ip\"}}]}," + - "\"docs\":[{\"_index\":\"index\",\"_id\":\"id\",\"_source\":{\"ip\":\"89.160.20.128\"}}]}"; + String body = "{\"pipeline\":{\"processors\":[{\"geoip\":{\"field\":\"ip\"}}]}," + + "\"docs\":[{\"_index\":\"index\",\"_id\":\"id\",\"_source\":{\"ip\":\"89.160.20.128\"}}]}"; Request simulatePipelineRequest = new Request("POST", "/_ingest/pipeline/_simulate"); simulatePipelineRequest.setJsonEntity(body); { @@ -49,8 +49,10 @@ public void test() throws Exception { assertThat(Files.exists(configPath), is(true)); Path ingestGeoipDatabaseDir = configPath.resolve("ingest-geoip"); Files.createDirectory(ingestGeoipDatabaseDir); - Files.copy(UpdateDatabasesIT.class.getResourceAsStream("/GeoLite2-City-Test.mmdb"), - ingestGeoipDatabaseDir.resolve("GeoLite2-City.mmdb")); + Files.copy( + UpdateDatabasesIT.class.getResourceAsStream("/GeoLite2-City-Test.mmdb"), + ingestGeoipDatabaseDir.resolve("GeoLite2-City.mmdb") + ); // Ensure that a config database has been setup: { @@ -76,9 +78,7 @@ public void test() throws Exception { @Override protected Settings restClientSettings() { String token = basicAuthHeaderValue("admin", new SecureString("admin-password".toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } } diff --git a/modules/ingest-geoip/src/internalClusterTest/java/org/elasticsearch/ingest/geoip/AbstractGeoIpIT.java b/modules/ingest-geoip/src/internalClusterTest/java/org/elasticsearch/ingest/geoip/AbstractGeoIpIT.java index 04e87b5031acf..c4abfc7d9d861 100644 --- a/modules/ingest-geoip/src/internalClusterTest/java/org/elasticsearch/ingest/geoip/AbstractGeoIpIT.java +++ b/modules/ingest-geoip/src/internalClusterTest/java/org/elasticsearch/ingest/geoip/AbstractGeoIpIT.java @@ -37,13 +37,16 @@ protected Settings nodeSettings(final int nodeOrdinal, final Settings otherSetti Files.createDirectories(databasePath); Files.copy( new ByteArrayInputStream(StreamsUtils.copyToBytesFromClasspath("/GeoLite2-City.mmdb")), - databasePath.resolve("GeoLite2-City.mmdb")); + databasePath.resolve("GeoLite2-City.mmdb") + ); Files.copy( new ByteArrayInputStream(StreamsUtils.copyToBytesFromClasspath("/GeoLite2-Country.mmdb")), - databasePath.resolve("GeoLite2-Country.mmdb")); + databasePath.resolve("GeoLite2-Country.mmdb") + ); Files.copy( new ByteArrayInputStream(StreamsUtils.copyToBytesFromClasspath("/GeoLite2-ASN.mmdb")), - databasePath.resolve("GeoLite2-ASN.mmdb")); + databasePath.resolve("GeoLite2-ASN.mmdb") + ); } catch (final IOException e) { throw new UncheckedIOException(e); } @@ -58,9 +61,15 @@ public static class IngestGeoIpSettingsPlugin extends Plugin { @Override public List> getSettings() { - return List.of(Setting.simpleString("ingest.geoip.database_path", Setting.Property.NodeScope), - Setting.timeSetting("ingest.geoip.database_validity", TimeValue.timeValueDays(3), Setting.Property.NodeScope, - Setting.Property.Dynamic)); + return List.of( + Setting.simpleString("ingest.geoip.database_path", Setting.Property.NodeScope), + Setting.timeSetting( + "ingest.geoip.database_validity", + TimeValue.timeValueDays(3), + Setting.Property.NodeScope, + Setting.Property.Dynamic + ) + ); } } } diff --git a/modules/ingest-geoip/src/internalClusterTest/java/org/elasticsearch/ingest/geoip/GeoIpDownloaderIT.java b/modules/ingest-geoip/src/internalClusterTest/java/org/elasticsearch/ingest/geoip/GeoIpDownloaderIT.java index 4d12b66290f25..1e285d6324785 100644 --- a/modules/ingest-geoip/src/internalClusterTest/java/org/elasticsearch/ingest/geoip/GeoIpDownloaderIT.java +++ b/modules/ingest-geoip/src/internalClusterTest/java/org/elasticsearch/ingest/geoip/GeoIpDownloaderIT.java @@ -19,25 +19,25 @@ import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.internal.io.IOUtils; -import org.elasticsearch.ingest.IngestDocument; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.env.Environment; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.MatchQueryBuilder; import org.elasticsearch.index.query.RangeQueryBuilder; +import org.elasticsearch.ingest.IngestDocument; import org.elasticsearch.ingest.geoip.stats.GeoIpDownloaderStatsAction; -import org.elasticsearch.reindex.ReindexPlugin; import org.elasticsearch.persistent.PersistentTaskParams; import org.elasticsearch.persistent.PersistentTasksCustomMetadata; import org.elasticsearch.plugins.Plugin; +import org.elasticsearch.reindex.ReindexPlugin; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.test.junit.annotations.TestLogging; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.xcontent.json.JsonXContent; import org.junit.After; import java.io.ByteArrayInputStream; @@ -97,12 +97,15 @@ protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { public void cleanUp() throws Exception { deleteDatabasesInConfigDirectory(); - ClusterUpdateSettingsResponse settingsResponse = client().admin().cluster() + ClusterUpdateSettingsResponse settingsResponse = client().admin() + .cluster() .prepareUpdateSettings() - .setPersistentSettings(Settings.builder() - .putNull(GeoIpDownloaderTaskExecutor.ENABLED_SETTING.getKey()) - .putNull(GeoIpDownloader.POLL_INTERVAL_SETTING.getKey()) - .putNull("ingest.geoip.database_validity")) + .setPersistentSettings( + Settings.builder() + .putNull(GeoIpDownloaderTaskExecutor.ENABLED_SETTING.getKey()) + .putNull(GeoIpDownloader.POLL_INTERVAL_SETTING.getKey()) + .putNull("ingest.geoip.database_validity") + ) .get(); assertTrue(settingsResponse.isAcknowledged()); @@ -114,15 +117,19 @@ public void cleanUp() throws Exception { } }); assertBusy(() -> { - GeoIpDownloaderStatsAction.Response response = - client().execute(GeoIpDownloaderStatsAction.INSTANCE, new GeoIpDownloaderStatsAction.Request()).actionGet(); + GeoIpDownloaderStatsAction.Response response = client().execute( + GeoIpDownloaderStatsAction.INSTANCE, + new GeoIpDownloaderStatsAction.Request() + ).actionGet(); assertThat(response.getStats().getDatabasesCount(), equalTo(0)); assertThat(response.getNodes(), not(empty())); for (GeoIpDownloaderStatsAction.NodeResponse nodeResponse : response.getNodes()) { assertThat(nodeResponse.getConfigDatabases(), empty()); assertThat(nodeResponse.getDatabases(), empty()); - assertThat(nodeResponse.getFilesInTemp().stream().filter(s -> s.endsWith(".txt") == false).collect(Collectors.toList()), - empty()); + assertThat( + nodeResponse.getFilesInTemp().stream().filter(s -> s.endsWith(".txt") == false).collect(Collectors.toList()), + empty() + ); } }); assertBusy(() -> { @@ -141,12 +148,11 @@ public void cleanUp() throws Exception { @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/75221") public void testInvalidTimestamp() throws Exception { assumeTrue("only test with fixture to have stable results", ENDPOINT != null); - ClusterUpdateSettingsResponse settingsResponse = - client().admin().cluster() - .prepareUpdateSettings() - .setPersistentSettings(Settings.builder() - .put(GeoIpDownloaderTaskExecutor.ENABLED_SETTING.getKey(), true)) - .get(); + ClusterUpdateSettingsResponse settingsResponse = client().admin() + .cluster() + .prepareUpdateSettings() + .setPersistentSettings(Settings.builder().put(GeoIpDownloaderTaskExecutor.ENABLED_SETTING.getKey(), true)) + .get(); assertTrue(settingsResponse.isAcknowledged()); assertBusy(() -> { GeoIpTaskState state = getGeoIpTaskState(); @@ -156,15 +162,15 @@ public void testInvalidTimestamp() throws Exception { putPipeline(); verifyUpdatedDatabase(); - settingsResponse = - client().admin().cluster() - .prepareUpdateSettings() - .setPersistentSettings(Settings.builder() - .put("ingest.geoip.database_validity", TimeValue.timeValueMillis(1))) - .get(); + settingsResponse = client().admin() + .cluster() + .prepareUpdateSettings() + .setPersistentSettings(Settings.builder().put("ingest.geoip.database_validity", TimeValue.timeValueMillis(1))) + .get(); assertTrue(settingsResponse.isAcknowledged()); Thread.sleep(10); - settingsResponse = client().admin().cluster() + settingsResponse = client().admin() + .cluster() .prepareUpdateSettings() .setPersistentSettings(Settings.builder().put(GeoIpDownloader.POLL_INTERVAL_SETTING.getKey(), TimeValue.timeValueDays(2))) .get(); @@ -192,18 +198,17 @@ public void testInvalidTimestamp() throws Exception { assertFalse(result.getIngestDocument().hasField("ip-asn")); assertFalse(result.getIngestDocument().hasField("ip-country")); }); - settingsResponse = - client().admin().cluster() - .prepareUpdateSettings() - .setPersistentSettings(Settings.builder() - .putNull("ingest.geoip.database_validity")) - .get(); + settingsResponse = client().admin() + .cluster() + .prepareUpdateSettings() + .setPersistentSettings(Settings.builder().putNull("ingest.geoip.database_validity")) + .get(); assertTrue(settingsResponse.isAcknowledged()); assertBusy(() -> { for (Path geoIpTmpDir : geoIpTmpDirs) { try (Stream files = Files.list(geoIpTmpDir)) { Set names = files.map(f -> f.getFileName().toString()).collect(Collectors.toSet()); - assertThat(names, hasItems("GeoLite2-ASN.mmdb","GeoLite2-City.mmdb","GeoLite2-Country.mmdb")); + assertThat(names, hasItems("GeoLite2-ASN.mmdb", "GeoLite2-City.mmdb", "GeoLite2-Country.mmdb")); } } }); @@ -213,7 +218,8 @@ public void testUpdatedTimestamp() throws Exception { assumeTrue("only test with fixture to have stable results", ENDPOINT != null); testGeoIpDatabasesDownload(); long lastCheck = getGeoIpTaskState().getDatabases().get("GeoLite2-ASN.mmdb").getLastCheck(); - ClusterUpdateSettingsResponse settingsResponse = client().admin().cluster() + ClusterUpdateSettingsResponse settingsResponse = client().admin() + .cluster() .prepareUpdateSettings() .setPersistentSettings(Settings.builder().put(GeoIpDownloader.POLL_INTERVAL_SETTING.getKey(), TimeValue.timeValueDays(2))) .get(); @@ -223,7 +229,8 @@ public void testUpdatedTimestamp() throws Exception { } public void testGeoIpDatabasesDownload() throws Exception { - ClusterUpdateSettingsResponse settingsResponse = client().admin().cluster() + ClusterUpdateSettingsResponse settingsResponse = client().admin() + .cluster() .prepareUpdateSettings() .setPersistentSettings(Settings.builder().put(GeoIpDownloaderTaskExecutor.ENABLED_SETTING.getKey(), true)) .get(); @@ -239,11 +246,8 @@ public void testGeoIpDatabasesDownload() throws Exception { GeoIpTaskState state = (GeoIpTaskState) getTask().getState(); assertEquals(Set.of("GeoLite2-ASN.mmdb", "GeoLite2-City.mmdb", "GeoLite2-Country.mmdb"), state.getDatabases().keySet()); GeoIpTaskState.Metadata metadata = state.get(id); - BoolQueryBuilder queryBuilder = new BoolQueryBuilder() - .filter(new MatchQueryBuilder("name", id)) - .filter(new RangeQueryBuilder("chunk") - .from(metadata.getFirstChunk()) - .to(metadata.getLastChunk(), true)); + BoolQueryBuilder queryBuilder = new BoolQueryBuilder().filter(new MatchQueryBuilder("name", id)) + .filter(new RangeQueryBuilder("chunk").from(metadata.getFirstChunk()).to(metadata.getLastChunk(), true)); int size = metadata.getLastChunk() - metadata.getFirstChunk() + 1; SearchResponse res = client().prepareSearch(GeoIpDownloader.DATABASES_INDEX) .setSize(size) @@ -313,10 +317,21 @@ public void testUseGeoIpProcessorWithDownloadedDBs() throws Exception { for (Path geoipTmpDir : geoipTmpDirs) { try (Stream list = Files.list(geoipTmpDir)) { List files = list.map(Path::getFileName).map(Path::toString).collect(Collectors.toList()); - assertThat(files, containsInAnyOrder("GeoLite2-City.mmdb", "GeoLite2-Country.mmdb", "GeoLite2-ASN.mmdb", - "GeoLite2-City.mmdb_COPYRIGHT.txt", "GeoLite2-Country.mmdb_COPYRIGHT.txt", "GeoLite2-ASN.mmdb_COPYRIGHT.txt", - "GeoLite2-City.mmdb_LICENSE.txt", "GeoLite2-Country.mmdb_LICENSE.txt", "GeoLite2-ASN.mmdb_LICENSE.txt", - "GeoLite2-ASN.mmdb_README.txt")); + assertThat( + files, + containsInAnyOrder( + "GeoLite2-City.mmdb", + "GeoLite2-Country.mmdb", + "GeoLite2-ASN.mmdb", + "GeoLite2-City.mmdb_COPYRIGHT.txt", + "GeoLite2-Country.mmdb_COPYRIGHT.txt", + "GeoLite2-ASN.mmdb_COPYRIGHT.txt", + "GeoLite2-City.mmdb_LICENSE.txt", + "GeoLite2-Country.mmdb_LICENSE.txt", + "GeoLite2-ASN.mmdb_LICENSE.txt", + "GeoLite2-ASN.mmdb_README.txt" + ) + ); } } }); @@ -348,8 +363,17 @@ public void testStartWithNoDatabases() throws Exception { assertThat(result.getFailure(), nullValue()); assertThat(result.getIngestDocument(), notNullValue()); Map source = result.getIngestDocument().getSourceAndMetadata(); - assertThat(source, hasEntry("tags", List.of("_geoip_database_unavailable_GeoLite2-City.mmdb", - "_geoip_database_unavailable_GeoLite2-Country.mmdb", "_geoip_database_unavailable_GeoLite2-ASN.mmdb"))); + assertThat( + source, + hasEntry( + "tags", + List.of( + "_geoip_database_unavailable_GeoLite2-City.mmdb", + "_geoip_database_unavailable_GeoLite2-Country.mmdb", + "_geoip_database_unavailable_GeoLite2-ASN.mmdb" + ) + ) + ); } // Enable downloader: @@ -461,7 +485,11 @@ private void putPipeline() throws IOException { } private List getGeoIpTmpDirs() throws IOException { - final Set ids = clusterService().state().nodes().getDataNodes().values().stream() + final Set ids = clusterService().state() + .nodes() + .getDataNodes() + .values() + .stream() .map(DiscoveryNode::getId) .collect(Collectors.toSet()); // All nodes share the same geoip base dir in the shared tmp dir: @@ -483,27 +511,33 @@ private void setupDatabasesInConfigDirectory() throws Exception { .forEach(path -> { try { Files.createDirectories(path); - Files.copy(GeoIpDownloaderIT.class.getResourceAsStream("/GeoLite2-City.mmdb"), - path.resolve("GeoLite2-City.mmdb")); - Files.copy(GeoIpDownloaderIT.class.getResourceAsStream("/GeoLite2-ASN.mmdb"), - path.resolve("GeoLite2-ASN.mmdb")); - Files.copy(GeoIpDownloaderIT.class.getResourceAsStream("/GeoLite2-Country.mmdb"), - path.resolve("GeoLite2-Country.mmdb")); + Files.copy(GeoIpDownloaderIT.class.getResourceAsStream("/GeoLite2-City.mmdb"), path.resolve("GeoLite2-City.mmdb")); + Files.copy(GeoIpDownloaderIT.class.getResourceAsStream("/GeoLite2-ASN.mmdb"), path.resolve("GeoLite2-ASN.mmdb")); + Files.copy( + GeoIpDownloaderIT.class.getResourceAsStream("/GeoLite2-Country.mmdb"), + path.resolve("GeoLite2-Country.mmdb") + ); } catch (IOException e) { throw new UncheckedIOException(e); } }); assertBusy(() -> { - GeoIpDownloaderStatsAction.Response response = - client().execute(GeoIpDownloaderStatsAction.INSTANCE, new GeoIpDownloaderStatsAction.Request()).actionGet(); + GeoIpDownloaderStatsAction.Response response = client().execute( + GeoIpDownloaderStatsAction.INSTANCE, + new GeoIpDownloaderStatsAction.Request() + ).actionGet(); assertThat(response.getNodes(), not(empty())); for (GeoIpDownloaderStatsAction.NodeResponse nodeResponse : response.getNodes()) { - assertThat(nodeResponse.getConfigDatabases(), - containsInAnyOrder("GeoLite2-Country.mmdb", "GeoLite2-City.mmdb", "GeoLite2-ASN.mmdb")); + assertThat( + nodeResponse.getConfigDatabases(), + containsInAnyOrder("GeoLite2-Country.mmdb", "GeoLite2-City.mmdb", "GeoLite2-ASN.mmdb") + ); assertThat(nodeResponse.getDatabases(), empty()); - assertThat(nodeResponse.getFilesInTemp().stream().filter(s -> s.endsWith(".txt") == false).collect(Collectors.toList()), - empty()); + assertThat( + nodeResponse.getFilesInTemp().stream().filter(s -> s.endsWith(".txt") == false).collect(Collectors.toList()), + empty() + ); } }); } @@ -522,8 +556,10 @@ private void deleteDatabasesInConfigDirectory() throws Exception { }); assertBusy(() -> { - GeoIpDownloaderStatsAction.Response response = - client().execute(GeoIpDownloaderStatsAction.INSTANCE, new GeoIpDownloaderStatsAction.Request()).actionGet(); + GeoIpDownloaderStatsAction.Response response = client().execute( + GeoIpDownloaderStatsAction.INSTANCE, + new GeoIpDownloaderStatsAction.Request() + ).actionGet(); assertThat(response.getNodes(), not(empty())); for (GeoIpDownloaderStatsAction.NodeResponse nodeResponse : response.getNodes()) { assertThat(nodeResponse.getConfigDatabases(), empty()); diff --git a/modules/ingest-geoip/src/internalClusterTest/java/org/elasticsearch/ingest/geoip/GeoIpDownloaderStatsIT.java b/modules/ingest-geoip/src/internalClusterTest/java/org/elasticsearch/ingest/geoip/GeoIpDownloaderStatsIT.java index 33b6da9bcc193..288547b6a72d8 100644 --- a/modules/ingest-geoip/src/internalClusterTest/java/org/elasticsearch/ingest/geoip/GeoIpDownloaderStatsIT.java +++ b/modules/ingest-geoip/src/internalClusterTest/java/org/elasticsearch/ingest/geoip/GeoIpDownloaderStatsIT.java @@ -11,15 +11,15 @@ import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsResponse; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.reindex.ReindexPlugin; import org.elasticsearch.ingest.geoip.stats.GeoIpDownloaderStatsAction; import org.elasticsearch.plugins.Plugin; +import org.elasticsearch.reindex.ReindexPlugin; import org.elasticsearch.test.ESIntegTestCase; import org.elasticsearch.test.XContentTestUtils; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; import org.junit.After; import java.io.IOException; @@ -57,7 +57,8 @@ protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { @After public void disableDownloader() { - ClusterUpdateSettingsResponse settingsResponse = client().admin().cluster() + ClusterUpdateSettingsResponse settingsResponse = client().admin() + .cluster() .prepareUpdateSettings() .setPersistentSettings(Settings.builder().put(GeoIpDownloaderTaskExecutor.ENABLED_SETTING.getKey(), (String) null)) .get(); @@ -75,8 +76,8 @@ public void testStats() throws Exception { assertThat(jsonMapView.get("stats.total_download_time"), equalTo(0)); assertEquals(0, jsonMapView.>get("nodes").size()); - - ClusterUpdateSettingsResponse settingsResponse = client().admin().cluster() + ClusterUpdateSettingsResponse settingsResponse = client().admin() + .cluster() .prepareUpdateSettings() .setPersistentSettings(Settings.builder().put(GeoIpDownloaderTaskExecutor.ENABLED_SETTING.getKey(), true)) .get(); @@ -94,8 +95,10 @@ public void testStats() throws Exception { assertThat(nodes.values(), hasSize(greaterThan(0))); for (Map>> value : nodes.values()) { assertThat(value, hasKey("databases")); - assertThat(value.get("databases").stream().map(m -> m.get("name")).collect(Collectors.toSet()), - containsInAnyOrder("GeoLite2-City.mmdb", "GeoLite2-ASN.mmdb", "GeoLite2-Country.mmdb")); + assertThat( + value.get("databases").stream().map(m -> m.get("name")).collect(Collectors.toSet()), + containsInAnyOrder("GeoLite2-City.mmdb", "GeoLite2-ASN.mmdb", "GeoLite2-Country.mmdb") + ); } }); } diff --git a/modules/ingest-geoip/src/internalClusterTest/java/org/elasticsearch/ingest/geoip/GeoIpProcessorNonIngestNodeIT.java b/modules/ingest-geoip/src/internalClusterTest/java/org/elasticsearch/ingest/geoip/GeoIpProcessorNonIngestNodeIT.java index d4cf5f7116bd2..75b9e1eb9fce6 100644 --- a/modules/ingest-geoip/src/internalClusterTest/java/org/elasticsearch/ingest/geoip/GeoIpProcessorNonIngestNodeIT.java +++ b/modules/ingest-geoip/src/internalClusterTest/java/org/elasticsearch/ingest/geoip/GeoIpProcessorNonIngestNodeIT.java @@ -14,12 +14,12 @@ import org.elasticsearch.action.ingest.PutPipelineRequest; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.ingest.IngestService; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.test.NodeRoles; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.xcontent.json.JsonXContent; import java.io.IOException; import java.util.Arrays; @@ -107,13 +107,13 @@ public void testLazyLoading() throws IOException { assertDatabaseLoadStatus(ingestNode, true); // the geo-IP database should still not be loaded on the non-ingest nodes Arrays.stream(internalCluster().getNodeNames()) - .filter(node -> node.equals(ingestNode) == false) - .forEach(node -> assertDatabaseLoadStatus(node, false)); + .filter(node -> node.equals(ingestNode) == false) + .forEach(node -> assertDatabaseLoadStatus(node, false)); } private void assertDatabaseLoadStatus(final String node, final boolean loaded) { final IngestService ingestService = internalCluster().getInstance(IngestService.class, node); - final GeoIpProcessor.Factory factory = (GeoIpProcessor.Factory)ingestService.getProcessorFactories().get("geoip"); + final GeoIpProcessor.Factory factory = (GeoIpProcessor.Factory) ingestService.getProcessorFactories().get("geoip"); for (final DatabaseReaderLazyLoader loader : factory.getAllDatabases()) { if (loaded) { assertNotNull(loader.databaseReader.get()); diff --git a/modules/ingest-geoip/src/internalClusterTest/java/org/elasticsearch/ingest/geoip/ReloadingDatabasesWhilePerformingGeoLookupsIT.java b/modules/ingest-geoip/src/internalClusterTest/java/org/elasticsearch/ingest/geoip/ReloadingDatabasesWhilePerformingGeoLookupsIT.java index dbbd924620227..42d4f108febac 100644 --- a/modules/ingest-geoip/src/internalClusterTest/java/org/elasticsearch/ingest/geoip/ReloadingDatabasesWhilePerformingGeoLookupsIT.java +++ b/modules/ingest-geoip/src/internalClusterTest/java/org/elasticsearch/ingest/geoip/ReloadingDatabasesWhilePerformingGeoLookupsIT.java @@ -62,17 +62,19 @@ public void test() throws Exception { ClusterService clusterService = mock(ClusterService.class); when(clusterService.state()).thenReturn(ClusterState.EMPTY_STATE); GeoIpProcessor.Factory factory = new GeoIpProcessor.Factory(databaseNodeService, clusterService); - Files.copy(ConfigDatabases.class.getResourceAsStream("/GeoLite2-City-Test.mmdb"), - geoIpTmpDir.resolve("GeoLite2-City.mmdb")); - Files.copy(ConfigDatabases.class.getResourceAsStream("/GeoLite2-City-Test.mmdb"), - geoIpTmpDir.resolve("GeoLite2-City-Test.mmdb")); + Files.copy(ConfigDatabases.class.getResourceAsStream("/GeoLite2-City-Test.mmdb"), geoIpTmpDir.resolve("GeoLite2-City.mmdb")); + Files.copy(ConfigDatabases.class.getResourceAsStream("/GeoLite2-City-Test.mmdb"), geoIpTmpDir.resolve("GeoLite2-City-Test.mmdb")); databaseNodeService.updateDatabase("GeoLite2-City.mmdb", "md5", geoIpTmpDir.resolve("GeoLite2-City.mmdb")); databaseNodeService.updateDatabase("GeoLite2-City-Test.mmdb", "md5", geoIpTmpDir.resolve("GeoLite2-City-Test.mmdb")); lazyLoadReaders(databaseNodeService); final GeoIpProcessor processor1 = (GeoIpProcessor) factory.create(null, "_tag", null, new HashMap<>(Map.of("field", "_field"))); - final GeoIpProcessor processor2 = (GeoIpProcessor) factory.create(null, "_tag", null, - new HashMap<>(Map.of("field", "_field", "database_file", "GeoLite2-City-Test.mmdb"))); + final GeoIpProcessor processor2 = (GeoIpProcessor) factory.create( + null, + "_tag", + null, + new HashMap<>(Map.of("field", "_field", "database_file", "GeoLite2-City-Test.mmdb")) + ); final AtomicBoolean completed = new AtomicBoolean(false); final int numberOfDatabaseUpdates = randomIntBetween(2, 4); @@ -85,12 +87,24 @@ public void test() throws Exception { ingestThreads[id] = new Thread(() -> { while (completed.get() == false) { try { - IngestDocument document1 = - new IngestDocument("index", "id", "routing", 1L, VersionType.EXTERNAL, Map.of("_field", "89.160.20.128")); + IngestDocument document1 = new IngestDocument( + "index", + "id", + "routing", + 1L, + VersionType.EXTERNAL, + Map.of("_field", "89.160.20.128") + ); processor1.execute(document1); assertThat(document1.getSourceAndMetadata().get("geoip"), notNullValue()); - IngestDocument document2 = - new IngestDocument("index", "id", "routing", 1L, VersionType.EXTERNAL, Map.of("_field", "89.160.20.128")); + IngestDocument document2 = new IngestDocument( + "index", + "id", + "routing", + 1L, + VersionType.EXTERNAL, + Map.of("_field", "89.160.20.128") + ); processor2.execute(document2); assertThat(document2.getSourceAndMetadata().get("geoip"), notNullValue()); numberOfIngestRuns.incrementAndGet(); @@ -118,13 +132,17 @@ public void test() throws Exception { assertThat(previous1.current(), equalTo(-1)); }); } else { - Files.copy(ConfigDatabases.class.getResourceAsStream("/GeoLite2-City-Test.mmdb"), - geoIpTmpDir.resolve("GeoLite2-City.mmdb"), StandardCopyOption.REPLACE_EXISTING); + Files.copy( + ConfigDatabases.class.getResourceAsStream("/GeoLite2-City-Test.mmdb"), + geoIpTmpDir.resolve("GeoLite2-City.mmdb"), + StandardCopyOption.REPLACE_EXISTING + ); databaseNodeService.updateDatabase("GeoLite2-City.mmdb", "md5", geoIpTmpDir.resolve("GeoLite2-City.mmdb")); } DatabaseReaderLazyLoader previous2 = databaseNodeService.get("GeoLite2-City-Test.mmdb"); - InputStream source = ConfigDatabases.class.getResourceAsStream(i % 2 == 0 ? "/GeoIP2-City-Test.mmdb" : - "/GeoLite2-City-Test.mmdb"); + InputStream source = ConfigDatabases.class.getResourceAsStream( + i % 2 == 0 ? "/GeoIP2-City-Test.mmdb" : "/GeoLite2-City-Test.mmdb" + ); Files.copy(source, geoIpTmpDir.resolve("GeoLite2-City-Test.mmdb"), StandardCopyOption.REPLACE_EXISTING); databaseNodeService.updateDatabase("GeoLite2-City-Test.mmdb", "md5", geoIpTmpDir.resolve("GeoLite2-City-Test.mmdb")); @@ -170,8 +188,13 @@ private static DatabaseNodeService createRegistry(Path geoIpConfigDir, Path geoI GeoIpCache cache = new GeoIpCache(0); ConfigDatabases configDatabases = new ConfigDatabases(geoIpConfigDir, cache); copyDatabaseFiles(geoIpConfigDir, configDatabases); - DatabaseNodeService databaseNodeService = - new DatabaseNodeService(geoIpTmpDir, mock(Client.class), cache, configDatabases, Runnable::run); + DatabaseNodeService databaseNodeService = new DatabaseNodeService( + geoIpTmpDir, + mock(Client.class), + cache, + configDatabases, + Runnable::run + ); databaseNodeService.initialize("nodeId", mock(ResourceWatcherService.class), mock(IngestService.class)); return databaseNodeService; } diff --git a/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/DatabaseNodeService.java b/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/DatabaseNodeService.java index 2a35bca54090d..87053f176da7c 100644 --- a/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/DatabaseNodeService.java +++ b/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/DatabaseNodeService.java @@ -19,9 +19,9 @@ import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.routing.IndexRoutingTable; +import org.elasticsearch.common.hash.MessageDigests; import org.elasticsearch.core.CheckedConsumer; import org.elasticsearch.core.CheckedRunnable; -import org.elasticsearch.common.hash.MessageDigests; import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.env.Environment; import org.elasticsearch.index.query.TermQueryBuilder; @@ -102,11 +102,7 @@ public final class DatabaseNodeService implements Closeable { ); } - DatabaseNodeService(Path tmpDir, - Client client, - GeoIpCache cache, - ConfigDatabases configDatabases, - Consumer genericExecutor) { + DatabaseNodeService(Path tmpDir, Client client, GeoIpCache cache, ConfigDatabases configDatabases, Consumer genericExecutor) { this.client = client; this.cache = cache; this.geoipTmpBaseDirectory = tmpDir.resolve("geoip-databases"); @@ -159,8 +155,7 @@ public DatabaseReaderLazyLoader getDatabase(String name) { // There is a need for reference counting in order to avoid using an instance // that gets closed while using it. (this can happen during a database update) while (true) { - DatabaseReaderLazyLoader instance = - databases.getOrDefault(name, configDatabases.getDatabase(name)); + DatabaseReaderLazyLoader instance = databases.getOrDefault(name, configDatabases.getDatabase(name)); if (instance == null || instance.preLookup()) { return instance; } @@ -201,36 +196,40 @@ void checkDatabases(ClusterState state) { return; } - PersistentTasksCustomMetadata.PersistentTask task = - PersistentTasksCustomMetadata.getTaskWithId(state, GeoIpDownloader.GEOIP_DOWNLOADER); + PersistentTasksCustomMetadata.PersistentTask task = PersistentTasksCustomMetadata.getTaskWithId( + state, + GeoIpDownloader.GEOIP_DOWNLOADER + ); // Empty state will purge stale entries in databases map. GeoIpTaskState taskState = task == null || task.getState() == null ? GeoIpTaskState.EMPTY : (GeoIpTaskState) task.getState(); - taskState.getDatabases().entrySet().stream() - .filter(e -> e.getValue().isValid(state.getMetadata().settings())) - .forEach(e -> { - String name = e.getKey(); - GeoIpTaskState.Metadata metadata = e.getValue(); - DatabaseReaderLazyLoader reference = databases.get(name); - String remoteMd5 = metadata.getMd5(); - String localMd5 = reference != null ? reference.getMd5() : null; - if (Objects.equals(localMd5, remoteMd5)) { - LOGGER.debug("Current reference of [{}] is up to date [{}] with was recorded in CS [{}]", name, localMd5, remoteMd5); - return; - } + taskState.getDatabases().entrySet().stream().filter(e -> e.getValue().isValid(state.getMetadata().settings())).forEach(e -> { + String name = e.getKey(); + GeoIpTaskState.Metadata metadata = e.getValue(); + DatabaseReaderLazyLoader reference = databases.get(name); + String remoteMd5 = metadata.getMd5(); + String localMd5 = reference != null ? reference.getMd5() : null; + if (Objects.equals(localMd5, remoteMd5)) { + LOGGER.debug("Current reference of [{}] is up to date [{}] with was recorded in CS [{}]", name, localMd5, remoteMd5); + return; + } - try { - retrieveAndUpdateDatabase(name, metadata); - } catch (Exception ex) { - LOGGER.error((Supplier) () -> new ParameterizedMessage("attempt to download database [{}] failed", name), ex); - } - }); + try { + retrieveAndUpdateDatabase(name, metadata); + } catch (Exception ex) { + LOGGER.error((Supplier) () -> new ParameterizedMessage("attempt to download database [{}] failed", name), ex); + } + }); List staleEntries = new ArrayList<>(databases.keySet()); - staleEntries.removeAll(taskState.getDatabases().entrySet().stream() - .filter(e->e.getValue().isValid(state.getMetadata().settings())) - .map(Map.Entry::getKey) - .collect(Collectors.toSet())); + staleEntries.removeAll( + taskState.getDatabases() + .entrySet() + .stream() + .filter(e -> e.getValue().isValid(state.getMetadata().settings())) + .map(Map.Entry::getKey) + .collect(Collectors.toSet()) + ); removeStaleEntries(staleEntries); } @@ -274,11 +273,14 @@ void retrieveAndUpdateDatabase(String databaseName, GeoIpTaskState.Metadata meta Path databaseFile = geoipTmpDirectory.resolve(databaseName); // tarball contains .mmdb, LICENSE.txt, COPYRIGHTS.txt and optional README.txt files. // we store mmdb file as is and prepend database name to all other entries to avoid conflicts - try (TarInputStream is = - new TarInputStream(new GZIPInputStream(new BufferedInputStream(Files.newInputStream(databaseTmpGzFile)), 8192))) { + try ( + TarInputStream is = new TarInputStream( + new GZIPInputStream(new BufferedInputStream(Files.newInputStream(databaseTmpGzFile)), 8192) + ) + ) { TarInputStream.TarEntry entry; while ((entry = is.getNextEntry()) != null) { - //there might be ./ entry in tar, we should skip it + // there might be ./ entry in tar, we should skip it if (entry.isNotFile()) { continue; } @@ -306,7 +308,8 @@ void retrieveAndUpdateDatabase(String databaseName, GeoIpTaskState.Metadata meta ioe.addSuppressed(failure); LOGGER.error("Unable to delete tmp database file after failure", ioe); } - }); + } + ); } void updateDatabase(String databaseFileName, String recordedMd5, Path file) { @@ -324,11 +327,20 @@ void updateDatabase(String databaseFileName, String recordedMd5, Path file) { for (var id : ids) { try { ingestService.reloadPipeline(id); - LOGGER.debug("successfully reloaded pipeline [{}] after downloading of database [{}] for the first time", - id, databaseFileName); + LOGGER.debug( + "successfully reloaded pipeline [{}] after downloading of database [{}] for the first time", + id, + databaseFileName + ); } catch (Exception e) { - LOGGER.debug((Supplier) () -> new ParameterizedMessage( - "failed to reload pipeline [{}] after downloading of database [{}]", id, databaseFileName), e); + LOGGER.debug( + (Supplier) () -> new ParameterizedMessage( + "failed to reload pipeline [{}] after downloading of database [{}]", + id, + databaseFileName + ), + e + ); } } } @@ -352,12 +364,14 @@ void removeStaleEntries(Collection staleEntries) { } } - void retrieveDatabase(String databaseName, - String expectedMd5, - GeoIpTaskState.Metadata metadata, - CheckedConsumer chunkConsumer, - CheckedRunnable completedHandler, - Consumer failureHandler) { + void retrieveDatabase( + String databaseName, + String expectedMd5, + GeoIpTaskState.Metadata metadata, + CheckedConsumer chunkConsumer, + CheckedRunnable completedHandler, + Consumer failureHandler + ) { // Need to run the search from a different thread, since this is executed from cluster state applier thread: genericExecutor.accept(() -> { MessageDigest md = MessageDigests.md5(); @@ -390,8 +404,9 @@ void retrieveDatabase(String databaseName, if (Objects.equals(expectedMd5, actualMd5)) { completedHandler.run(); } else { - failureHandler.accept(new RuntimeException("expected md5 hash [" + expectedMd5 + - "], but got md5 hash [" + actualMd5 + "]")); + failureHandler.accept( + new RuntimeException("expected md5 hash [" + expectedMd5 + "], but got md5 hash [" + actualMd5 + "]") + ); } } catch (Exception e) { failureHandler.accept(e); diff --git a/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/DatabaseReaderLazyLoader.java b/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/DatabaseReaderLazyLoader.java index 891437ebedae4..2e79bd3733767 100644 --- a/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/DatabaseReaderLazyLoader.java +++ b/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/DatabaseReaderLazyLoader.java @@ -16,13 +16,14 @@ import com.maxmind.geoip2.model.AsnResponse; import com.maxmind.geoip2.model.CityResponse; import com.maxmind.geoip2.model.CountryResponse; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.lucene.util.SetOnce; import org.elasticsearch.SpecialPermission; -import org.elasticsearch.core.Booleans; import org.elasticsearch.common.CheckedBiFunction; import org.elasticsearch.common.CheckedSupplier; +import org.elasticsearch.core.Booleans; import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.core.internal.io.IOUtils; @@ -44,8 +45,7 @@ */ class DatabaseReaderLazyLoader implements Closeable { - private static final boolean LOAD_DATABASE_ON_HEAP = - Booleans.parseBoolean(System.getProperty("es.geoip.load_db_on_heap", "false")); + private static final boolean LOAD_DATABASE_ON_HEAP = Booleans.parseBoolean(System.getProperty("es.geoip.load_db_on_heap", "false")); private static final Logger LOGGER = LogManager.getLogger(DatabaseReaderLazyLoader.class); @@ -91,7 +91,7 @@ final String getDatabaseType() throws IOException { if (fileSize <= 512) { throw new IOException("unexpected file length [" + fileSize + "] for [" + databasePath + "]"); } - final int[] databaseTypeMarker = {'d', 'a', 't', 'a', 'b', 'a', 's', 'e', '_', 't', 'y', 'p', 'e'}; + final int[] databaseTypeMarker = { 'd', 'a', 't', 'a', 'b', 'a', 's', 'e', '_', 't', 'y', 'p', 'e' }; try (InputStream in = databaseInputStream()) { // read the last 512 bytes final long skipped = in.skip(fileSize - 512); @@ -178,19 +178,20 @@ int current() { return currentUsages.get(); } - private T getResponse(InetAddress ipAddress, - CheckedBiFunction responseProvider) { + private T getResponse( + InetAddress ipAddress, + CheckedBiFunction responseProvider + ) { SpecialPermission.check(); - return AccessController.doPrivileged((PrivilegedAction) () -> - cache.putIfAbsent(ipAddress, databasePath.toString(), ip -> { - try { - return responseProvider.apply(get(), ipAddress); - } catch (AddressNotFoundException e) { - throw new GeoIpProcessor.AddressNotFoundRuntimeException(e); - } catch (Exception e) { - throw new RuntimeException(e); - } - })); + return AccessController.doPrivileged((PrivilegedAction) () -> cache.putIfAbsent(ipAddress, databasePath.toString(), ip -> { + try { + return responseProvider.apply(get(), ipAddress); + } catch (AddressNotFoundException e) { + throw new GeoIpProcessor.AddressNotFoundRuntimeException(e); + } catch (Exception e) { + throw new RuntimeException(e); + } + })); } DatabaseReader get() throws IOException { diff --git a/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/GeoIpCache.java b/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/GeoIpCache.java index 8caa5e0fd6304..2df2bfda26a32 100644 --- a/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/GeoIpCache.java +++ b/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/GeoIpCache.java @@ -9,6 +9,7 @@ import com.maxmind.db.NodeCache; import com.maxmind.geoip2.model.AbstractResponse; + import org.elasticsearch.common.cache.Cache; import org.elasticsearch.common.cache.CacheBuilder; @@ -26,7 +27,7 @@ final class GeoIpCache { private final Cache cache; - //package private for testing + // package private for testing GeoIpCache(long maxSize) { if (maxSize < 0) { throw new IllegalArgumentException("geoip max cache size must be 0 or greater"); @@ -35,13 +36,15 @@ final class GeoIpCache { } @SuppressWarnings("unchecked") - T putIfAbsent(InetAddress ip, - String databasePath, - Function retrieveFunction) { + T putIfAbsent( + InetAddress ip, + String databasePath, + Function retrieveFunction + ) { - //can't use cache.computeIfAbsent due to the elevated permissions for the jackson (run via the cache loader) + // can't use cache.computeIfAbsent due to the elevated permissions for the jackson (run via the cache loader) CacheKey cacheKey = new CacheKey(ip, databasePath); - //intentionally non-locking for simplicity...it's OK if we re-put the same key/value in the cache during a race condition. + // intentionally non-locking for simplicity...it's OK if we re-put the same key/value in the cache during a race condition. AbstractResponse response = cache.get(cacheKey); if (response == null) { response = retrieveFunction.apply(ip); @@ -50,7 +53,7 @@ T putIfAbsent(InetAddress ip, return (T) response; } - //only useful for testing + // only useful for testing AbstractResponse get(InetAddress ip, String databasePath) { CacheKey cacheKey = new CacheKey(ip, databasePath); return cache.get(cacheKey); @@ -87,17 +90,16 @@ private CacheKey(InetAddress ip, String databasePath) { this.databasePath = databasePath; } - //generated + // generated @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; CacheKey cacheKey = (CacheKey) o; - return Objects.equals(ip, cacheKey.ip) && - Objects.equals(databasePath, cacheKey.databasePath); + return Objects.equals(ip, cacheKey.ip) && Objects.equals(databasePath, cacheKey.databasePath); } - //generated + // generated @Override public int hashCode() { return Objects.hash(ip, databasePath); diff --git a/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/GeoIpDownloader.java b/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/GeoIpDownloader.java index eee296492a4c1..61a7ef1cd37ef 100644 --- a/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/GeoIpDownloader.java +++ b/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/GeoIpDownloader.java @@ -21,10 +21,6 @@ import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Setting.Property; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.DeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.MatchQueryBuilder; @@ -38,6 +34,10 @@ import org.elasticsearch.tasks.TaskId; import org.elasticsearch.threadpool.Scheduler; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.DeprecationHandler; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; import java.io.IOException; import java.io.InputStream; @@ -57,10 +57,18 @@ public class GeoIpDownloader extends AllocatedPersistentTask { private static final Logger logger = LogManager.getLogger(GeoIpDownloader.class); - public static final Setting POLL_INTERVAL_SETTING = Setting.timeSetting("ingest.geoip.downloader.poll.interval", - TimeValue.timeValueDays(3), TimeValue.timeValueDays(1), Property.Dynamic, Property.NodeScope); - public static final Setting ENDPOINT_SETTING = Setting.simpleString("ingest.geoip.downloader.endpoint", - "https://geoip.elastic.co/v1/database", Property.NodeScope); + public static final Setting POLL_INTERVAL_SETTING = Setting.timeSetting( + "ingest.geoip.downloader.poll.interval", + TimeValue.timeValueDays(3), + TimeValue.timeValueDays(1), + Property.Dynamic, + Property.NodeScope + ); + public static final Setting ENDPOINT_SETTING = Setting.simpleString( + "ingest.geoip.downloader.endpoint", + "https://geoip.elastic.co/v1/database", + Property.NodeScope + ); public static final String GEOIP_DOWNLOADER = "geoip-downloader"; static final String DATABASES_INDEX = ".geoip_databases"; @@ -73,14 +81,25 @@ public class GeoIpDownloader extends AllocatedPersistentTask { private final ThreadPool threadPool; private final String endpoint; - //visible for testing + // visible for testing protected volatile GeoIpTaskState state; private volatile TimeValue pollInterval; private volatile Scheduler.ScheduledCancellable scheduled; private volatile GeoIpDownloaderStats stats = GeoIpDownloaderStats.EMPTY; - GeoIpDownloader(Client client, HttpClient httpClient, ClusterService clusterService, ThreadPool threadPool, Settings settings, - long id, String type, String action, String description, TaskId parentTask, Map headers) { + GeoIpDownloader( + Client client, + HttpClient httpClient, + ClusterService clusterService, + ThreadPool threadPool, + Settings settings, + long id, + String type, + String action, + String description, + TaskId parentTask, + Map headers + ) { super(id, type, action, description, parentTask, headers); this.httpClient = httpClient; this.client = client; @@ -98,7 +117,7 @@ public void setPollInterval(TimeValue pollInterval) { } } - //visible for testing + // visible for testing void updateDatabases() throws IOException { logger.info("updating geoip databases"); List> response = fetchDatabasesOverview(); @@ -114,13 +133,15 @@ private List fetchDatabasesOverview() throws IOException { String url = endpoint + "?elastic_geoip_service_tos=agree"; logger.info("fetching geoip databases overview from [" + url + "]"); byte[] data = httpClient.getBytes(url); - try (XContentParser parser = XContentType.JSON.xContent().createParser(NamedXContentRegistry.EMPTY, - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, data)) { + try ( + XContentParser parser = XContentType.JSON.xContent() + .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, data) + ) { return (List) parser.list(); } } - //visible for testing + // visible for testing void processDatabase(Map databaseInfo) { String name = databaseInfo.get("name").toString().replace(".tgz", "") + ".mmdb"; String md5 = (String) databaseInfo.get("md5_hash"); @@ -131,7 +152,7 @@ void processDatabase(Map databaseInfo) { logger.info("updating geoip database [" + name + "]"); String url = databaseInfo.get("url").toString(); if (url.startsWith("http") == false) { - //relative url, add it after last slash (i.e resolve sibling) or at the end if there's no slash after http[s]:// + // relative url, add it after last slash (i.e resolve sibling) or at the end if there's no slash after http[s]:// int lastSlash = endpoint.substring(8).lastIndexOf('/'); url = (lastSlash != -1 ? endpoint.substring(0, lastSlash + 8) : endpoint) + "/" + url; } @@ -152,23 +173,27 @@ void processDatabase(Map databaseInfo) { } } - //visible for testing + // visible for testing void deleteOldChunks(String name, int firstChunk) { - BoolQueryBuilder queryBuilder = new BoolQueryBuilder() - .filter(new MatchQueryBuilder("name", name)) + BoolQueryBuilder queryBuilder = new BoolQueryBuilder().filter(new MatchQueryBuilder("name", name)) .filter(new RangeQueryBuilder("chunk").to(firstChunk, false)); DeleteByQueryRequest request = new DeleteByQueryRequest(); request.indices(DATABASES_INDEX); request.setQuery(queryBuilder); - client.execute(DeleteByQueryAction.INSTANCE, request, ActionListener.wrap(r -> { - }, e -> logger.warn("could not delete old chunks for geoip database [" + name + "]", e))); + client.execute( + DeleteByQueryAction.INSTANCE, + request, + ActionListener.wrap(r -> {}, e -> logger.warn("could not delete old chunks for geoip database [" + name + "]", e)) + ); } - //visible for testing + // visible for testing protected void updateTimestamp(String name, Metadata old) { logger.info("geoip database [" + name + "] is up to date, updated timestamp"); - state = state.put(name, new Metadata(old.getLastUpdate(), old.getFirstChunk(), old.getLastChunk(), old.getMd5(), - System.currentTimeMillis())); + state = state.put( + name, + new Metadata(old.getLastUpdate(), old.getFirstChunk(), old.getLastChunk(), old.getMd5(), System.currentTimeMillis()) + ); stats = stats.skippedDownload(); updateTaskState(); } @@ -179,13 +204,12 @@ void updateTaskState() { state = ((GeoIpTaskState) future.actionGet().getState()); } - //visible for testing + // visible for testing int indexChunks(String name, InputStream is, int chunk, String expectedMd5, long timestamp) throws IOException { MessageDigest md = MessageDigests.md5(); for (byte[] buf = getChunk(is); buf.length != 0; buf = getChunk(is)) { md.update(buf); - IndexRequest indexRequest = new IndexRequest(DATABASES_INDEX) - .id(name + "_" + chunk + "_" + timestamp) + IndexRequest indexRequest = new IndexRequest(DATABASES_INDEX).id(name + "_" + chunk + "_" + timestamp) .create(true) .source(XContentType.SMILE, "name", name, "chunk", chunk, "data", buf); client.index(indexRequest).actionGet(); @@ -207,7 +231,7 @@ int indexChunks(String name, InputStream is, int chunk, String expectedMd5, long return chunk; } - //visible for testing + // visible for testing byte[] getChunk(InputStream is) throws IOException { byte[] buf = new byte[MAX_CHUNK_SIZE]; int chunkSize = 0; @@ -246,16 +270,21 @@ void runDownloader() { } private void cleanDatabases() { - long expiredDatabases = state.getDatabases().entrySet().stream() + long expiredDatabases = state.getDatabases() + .entrySet() + .stream() .filter(e -> e.getValue().isValid(clusterService.state().metadata().settings()) == false) .peek(e -> { String name = e.getKey(); Metadata meta = e.getValue(); deleteOldChunks(name, meta.getLastChunk() + 1); - state = state.put(name, new Metadata(meta.getLastUpdate(), meta.getFirstChunk(), meta.getLastChunk(), meta.getMd5(), - meta.getLastCheck() - 1)); + state = state.put( + name, + new Metadata(meta.getLastUpdate(), meta.getFirstChunk(), meta.getLastChunk(), meta.getMd5(), meta.getLastCheck() - 1) + ); updateTaskState(); - }).count(); + }) + .count(); stats = stats.expiredDatabases((int) expiredDatabases); } diff --git a/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/GeoIpDownloaderTaskExecutor.java b/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/GeoIpDownloaderTaskExecutor.java index 5ef7077406b1e..4fe23e6e571a7 100644 --- a/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/GeoIpDownloaderTaskExecutor.java +++ b/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/GeoIpDownloaderTaskExecutor.java @@ -42,10 +42,15 @@ */ public final class GeoIpDownloaderTaskExecutor extends PersistentTasksExecutor implements ClusterStateListener { - private static final boolean ENABLED_DEFAULT = - "false".equals(System.getProperty("ingest.geoip.downloader.enabled.default", "true")) == false; - public static final Setting ENABLED_SETTING = Setting.boolSetting("ingest.geoip.downloader.enabled", ENABLED_DEFAULT, - Setting.Property.Dynamic, Setting.Property.NodeScope); + private static final boolean ENABLED_DEFAULT = "false".equals( + System.getProperty("ingest.geoip.downloader.enabled.default", "true") + ) == false; + public static final Setting ENABLED_SETTING = Setting.boolSetting( + "ingest.geoip.downloader.enabled", + ENABLED_DEFAULT, + Setting.Property.Dynamic, + Setting.Property.NodeScope + ); private static final Logger logger = LogManager.getLogger(GeoIpDownloader.class); @@ -76,11 +81,9 @@ private void setEnabled(boolean enabled) { return; } if (enabled) { - startTask(() -> { - }); + startTask(() -> {}); } else { - stopTask(() -> { - }); + stopTask(() -> {}); } } @@ -96,20 +99,36 @@ protected void nodeOperation(AllocatedPersistentTask task, GeoIpTaskParams param } @Override - protected GeoIpDownloader createTask(long id, String type, String action, TaskId parentTaskId, - PersistentTasksCustomMetadata.PersistentTask taskInProgress, - Map headers) { - return new GeoIpDownloader(client, httpClient, clusterService, threadPool, settings, id, type, action, - getDescription(taskInProgress), parentTaskId, headers); + protected GeoIpDownloader createTask( + long id, + String type, + String action, + TaskId parentTaskId, + PersistentTasksCustomMetadata.PersistentTask taskInProgress, + Map headers + ) { + return new GeoIpDownloader( + client, + httpClient, + clusterService, + threadPool, + settings, + id, + type, + action, + getDescription(taskInProgress), + parentTaskId, + headers + ); } @Override public void clusterChanged(ClusterChangedEvent event) { - if(event.state().blocks().hasGlobalBlock(GatewayService.STATE_NOT_RECOVERED_BLOCK)){ - //wait for state recovered + if (event.state().blocks().hasGlobalBlock(GatewayService.STATE_NOT_RECOVERED_BLOCK)) { + // wait for state recovered return; } - //bootstrap downloader after first cluster start + // bootstrap downloader after first cluster start clusterService.removeListener(this); if (event.localNodeMaster()) { if (ENABLED_SETTING.get(event.state().getMetadata().settings(), settings)) { @@ -121,30 +140,37 @@ public void clusterChanged(ClusterChangedEvent event) { } private void startTask(Runnable onFailure) { - persistentTasksService.sendStartRequest(GEOIP_DOWNLOADER, GEOIP_DOWNLOADER, new GeoIpTaskParams(), ActionListener.wrap(r -> { - }, e -> { - if (e instanceof ResourceAlreadyExistsException == false) { - logger.error("failed to create geoip downloader task", e); - onFailure.run(); - } - })); + persistentTasksService.sendStartRequest( + GEOIP_DOWNLOADER, + GEOIP_DOWNLOADER, + new GeoIpTaskParams(), + ActionListener.wrap(r -> {}, e -> { + if (e instanceof ResourceAlreadyExistsException == false) { + logger.error("failed to create geoip downloader task", e); + onFailure.run(); + } + }) + ); } private void stopTask(Runnable onFailure) { - ActionListener> listener = ActionListener.wrap(r -> { - }, e -> { + ActionListener> listener = ActionListener.wrap(r -> {}, e -> { if (e instanceof ResourceNotFoundException == false) { logger.error("failed to remove geoip downloader task", e); onFailure.run(); } }); - persistentTasksService.sendRemoveRequest(GEOIP_DOWNLOADER, ActionListener.runAfter(listener, () -> - client.admin().indices().prepareDelete(DATABASES_INDEX).execute(ActionListener.wrap(rr -> { - }, e -> { - if (e instanceof ResourceNotFoundException == false) { - logger.warn("failed to remove " + DATABASES_INDEX, e); - } - })))); + persistentTasksService.sendRemoveRequest( + GEOIP_DOWNLOADER, + ActionListener.runAfter( + listener, + () -> client.admin().indices().prepareDelete(DATABASES_INDEX).execute(ActionListener.wrap(rr -> {}, e -> { + if (e instanceof ResourceNotFoundException == false) { + logger.warn("failed to remove " + DATABASES_INDEX, e); + } + })) + ) + ); } public GeoIpDownloader getCurrentTask() { diff --git a/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/GeoIpProcessor.java b/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/GeoIpProcessor.java index 903a32b3e8a62..f444edb364189 100644 --- a/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/GeoIpProcessor.java +++ b/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/GeoIpProcessor.java @@ -55,8 +55,8 @@ public final class GeoIpProcessor extends AbstractProcessor { private static final DeprecationLogger DEPRECATION_LOGGER = DeprecationLogger.getLogger(GeoIpProcessor.class); - static final String DEFAULT_DATABASES_DEPRECATION_MESSAGE = "the [fallback_to_default_databases] has been deprecated," + - " because Elasticsearch no longer includes the default Maxmind geoip databases. This setting will be removed in Elasticsearch 9.0"; + static final String DEFAULT_DATABASES_DEPRECATION_MESSAGE = "the [fallback_to_default_databases] has been deprecated," + + " because Elasticsearch no longer includes the default Maxmind geoip databases. This setting will be removed in Elasticsearch 9.0"; public static final String TYPE = "geoip"; private static final String CITY_DB_SUFFIX = "-City"; @@ -95,7 +95,8 @@ public final class GeoIpProcessor extends AbstractProcessor { final Set properties, final boolean ignoreMissing, final boolean firstOnly, - final String databaseFile) { + final String databaseFile + ) { super(tag, description); this.field = field; this.isValid = isValid; @@ -187,8 +188,10 @@ private Map getGeoData(DatabaseReaderLazyLoader lazyLoader, Stri geoData = Collections.emptyMap(); } } else { - throw new ElasticsearchParseException("Unsupported database type [" + lazyLoader.getDatabaseType() - + "]", new IllegalStateException()); + throw new ElasticsearchParseException( + "Unsupported database type [" + lazyLoader.getDatabaseType() + "]", + new IllegalStateException() + ); } return geoData; } finally { @@ -359,16 +362,23 @@ private Map retrieveAsnGeoData(DatabaseReaderLazyLoader lazyLoad } public static final class Factory implements Processor.Factory { - static final Set DEFAULT_CITY_PROPERTIES = Collections.unmodifiableSet(EnumSet.of( - Property.CONTINENT_NAME, Property.COUNTRY_NAME, Property.COUNTRY_ISO_CODE, Property.REGION_ISO_CODE, - Property.REGION_NAME, Property.CITY_NAME, Property.LOCATION - )); - static final Set DEFAULT_COUNTRY_PROPERTIES = Collections.unmodifiableSet(EnumSet.of( - Property.CONTINENT_NAME, Property.COUNTRY_NAME, Property.COUNTRY_ISO_CODE - )); - static final Set DEFAULT_ASN_PROPERTIES = Collections.unmodifiableSet(EnumSet.of( - Property.IP, Property.ASN, Property.ORGANIZATION_NAME, Property.NETWORK - )); + static final Set DEFAULT_CITY_PROPERTIES = Collections.unmodifiableSet( + EnumSet.of( + Property.CONTINENT_NAME, + Property.COUNTRY_NAME, + Property.COUNTRY_ISO_CODE, + Property.REGION_ISO_CODE, + Property.REGION_NAME, + Property.CITY_NAME, + Property.LOCATION + ) + ); + static final Set DEFAULT_COUNTRY_PROPERTIES = Collections.unmodifiableSet( + EnumSet.of(Property.CONTINENT_NAME, Property.COUNTRY_NAME, Property.COUNTRY_ISO_CODE) + ); + static final Set DEFAULT_ASN_PROPERTIES = Collections.unmodifiableSet( + EnumSet.of(Property.IP, Property.ASN, Property.ORGANIZATION_NAME, Property.NETWORK) + ); private final DatabaseNodeService databaseNodeService; private final ClusterService clusterService; @@ -386,7 +396,9 @@ public Factory(DatabaseNodeService databaseNodeService, ClusterService clusterSe public Processor create( final Map registry, final String processorTag, - final String description, final Map config) throws IOException { + final String description, + final Map config + ) throws IOException { String ipField = readStringProperty(TYPE, processorTag, config, "field"); String targetField = readStringProperty(TYPE, processorTag, config, "target_field", "geoip"); String databaseFile = readStringProperty(TYPE, processorTag, config, "database_file", "GeoLite2-City.mmdb"); @@ -404,8 +416,7 @@ public Processor create( if (useDatabaseUnavailableProcessor(lazyLoader, databaseFile)) { return new DatabaseUnavailableProcessor(processorTag, description, databaseFile); } else if (lazyLoader == null) { - throw newConfigurationException(TYPE, processorTag, - "database_file", "database file [" + databaseFile + "] doesn't exist"); + throw newConfigurationException(TYPE, processorTag, "database_file", "database file [" + databaseFile + "] doesn't exist"); } final String databaseType; try { @@ -433,8 +444,12 @@ public Processor create( } else if (databaseType.endsWith(ASN_DB_SUFFIX)) { properties = DEFAULT_ASN_PROPERTIES; } else { - throw newConfigurationException(TYPE, processorTag, "database_file", "Unsupported database type [" - + databaseType + "]"); + throw newConfigurationException( + TYPE, + processorTag, + "database_file", + "Unsupported database type [" + databaseType + "]" + ); } } CheckedSupplier supplier = () -> { @@ -449,8 +464,8 @@ public Processor create( // For example overwriting a geoip lite city db with geoip city db is a valid change, but the db type is slightly different, // by checking just the suffix this assertion doesn't fail. String expectedSuffix = databaseType.substring(databaseType.lastIndexOf('-')); - assert loader.getDatabaseType().endsWith(expectedSuffix) : "database type [" + loader.getDatabaseType() + - "] doesn't match with expected suffix [" + expectedSuffix + "]"; + assert loader.getDatabaseType().endsWith(expectedSuffix) + : "database type [" + loader.getDatabaseType() + "] doesn't match with expected suffix [" + expectedSuffix + "]"; return loader; }; Supplier isValid = () -> { @@ -470,14 +485,28 @@ public Processor create( boolean valid = metadata.isValid(currentState.metadata().settings()); if (valid && metadata.isCloseToExpiration()) { - HeaderWarning.addWarning(DeprecationLogger.CRITICAL, "database [{}] was not updated for over 25 days, geoip processor" + - " will stop working if there is no update for 30 days", databaseFile); + HeaderWarning.addWarning( + DeprecationLogger.CRITICAL, + "database [{}] was not updated for over 25 days, geoip processor" + + " will stop working if there is no update for 30 days", + databaseFile + ); } return valid; }; - return new GeoIpProcessor(processorTag, description, ipField, supplier, isValid, targetField, properties, ignoreMissing, - firstOnly, databaseFile); + return new GeoIpProcessor( + processorTag, + description, + ipField, + supplier, + isValid, + targetField, + properties, + ignoreMissing, + firstOnly, + databaseFile + ); } private static boolean useDatabaseUnavailableProcessor(DatabaseReaderLazyLoader loader, String databaseName) { @@ -493,7 +522,7 @@ private static boolean useDatabaseUnavailableProcessor(DatabaseReaderLazyLoader // Geoip2's AddressNotFoundException is checked and due to the fact that we need run their code // inside a PrivilegedAction code block, we are forced to catch any checked exception and rethrow // it with an unchecked exception. - //package private for testing + // package private for testing static final class AddressNotFoundRuntimeException extends RuntimeException { AddressNotFoundRuntimeException(Throwable cause) { @@ -517,15 +546,27 @@ enum Property { NETWORK; static final EnumSet ALL_CITY_PROPERTIES = EnumSet.of( - Property.IP, Property.COUNTRY_ISO_CODE, Property.COUNTRY_NAME, Property.CONTINENT_NAME, - Property.REGION_ISO_CODE, Property.REGION_NAME, Property.CITY_NAME, Property.TIMEZONE, + Property.IP, + Property.COUNTRY_ISO_CODE, + Property.COUNTRY_NAME, + Property.CONTINENT_NAME, + Property.REGION_ISO_CODE, + Property.REGION_NAME, + Property.CITY_NAME, + Property.TIMEZONE, Property.LOCATION ); static final EnumSet ALL_COUNTRY_PROPERTIES = EnumSet.of( - Property.IP, Property.CONTINENT_NAME, Property.COUNTRY_NAME, Property.COUNTRY_ISO_CODE + Property.IP, + Property.CONTINENT_NAME, + Property.COUNTRY_NAME, + Property.COUNTRY_ISO_CODE ); static final EnumSet ALL_ASN_PROPERTIES = EnumSet.of( - Property.IP, Property.ASN, Property.ORGANIZATION_NAME, Property.NETWORK + Property.IP, + Property.ASN, + Property.ORGANIZATION_NAME, + Property.NETWORK ); public static Property parseProperty(String databaseType, String value) { @@ -545,8 +586,9 @@ public static Property parseProperty(String databaseType, String value) { } return property; } catch (IllegalArgumentException e) { - throw new IllegalArgumentException("illegal property value [" + value + "]. valid values are " + - Arrays.toString(validProperties.toArray())); + throw new IllegalArgumentException( + "illegal property value [" + value + "]. valid values are " + Arrays.toString(validProperties.toArray()) + ); } } } diff --git a/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/GeoIpTaskParams.java b/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/GeoIpTaskParams.java index 8cd65fa8a9fcf..6997964ace890 100644 --- a/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/GeoIpTaskParams.java +++ b/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/GeoIpTaskParams.java @@ -11,10 +11,10 @@ import org.elasticsearch.Version; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.persistent.PersistentTaskParams; import org.elasticsearch.xcontent.ObjectParser; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.persistent.PersistentTaskParams; import java.io.IOException; @@ -24,11 +24,9 @@ class GeoIpTaskParams implements PersistentTaskParams { public static final ObjectParser PARSER = new ObjectParser<>(GEOIP_DOWNLOADER, true, GeoIpTaskParams::new); - GeoIpTaskParams() { - } + GeoIpTaskParams() {} - GeoIpTaskParams(StreamInput in) { - } + GeoIpTaskParams(StreamInput in) {} @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { @@ -48,8 +46,7 @@ public Version getMinimalSupportedVersion() { } @Override - public void writeTo(StreamOutput out) { - } + public void writeTo(StreamOutput out) {} public static GeoIpTaskParams fromXContent(XContentParser parser) { return PARSER.apply(parser, null); diff --git a/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/GeoIpTaskState.java b/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/GeoIpTaskState.java index 4b2d803419c4b..f18f1e10af323 100644 --- a/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/GeoIpTaskState.java +++ b/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/GeoIpTaskState.java @@ -13,14 +13,14 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.VersionedNamedWriteable; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.TimeValue; +import org.elasticsearch.core.Tuple; +import org.elasticsearch.persistent.PersistentTaskState; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.core.TimeValue; -import org.elasticsearch.core.Tuple; -import org.elasticsearch.persistent.PersistentTaskState; import java.io.IOException; import java.time.Instant; @@ -32,9 +32,9 @@ import java.util.Objects; import java.util.stream.Collectors; +import static org.elasticsearch.ingest.geoip.GeoIpDownloader.GEOIP_DOWNLOADER; import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg; import static org.elasticsearch.xcontent.ConstructingObjectParser.optionalConstructorArg; -import static org.elasticsearch.ingest.geoip.GeoIpDownloader.GEOIP_DOWNLOADER; class GeoIpTaskState implements PersistentTaskState, VersionedNamedWriteable { @@ -43,12 +43,14 @@ class GeoIpTaskState implements PersistentTaskState, VersionedNamedWriteable { static final GeoIpTaskState EMPTY = new GeoIpTaskState(Collections.emptyMap()); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(GEOIP_DOWNLOADER, true, - args -> { - List> databases = (List>) args[0]; - return new GeoIpTaskState(databases.stream().collect(Collectors.toMap(Tuple::v1, Tuple::v2))); - }); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + GEOIP_DOWNLOADER, + true, + args -> { + List> databases = (List>) args[0]; + return new GeoIpTaskState(databases.stream().collect(Collectors.toMap(Tuple::v1, Tuple::v2))); + } + ); static { PARSER.declareNamedObjects(constructorArg(), (p, c, name) -> Tuple.tuple(name, Metadata.fromXContent(p)), DATABASES); @@ -65,11 +67,10 @@ public static GeoIpTaskState fromXContent(XContentParser parser) throws IOExcept } GeoIpTaskState(StreamInput input) throws IOException { - databases = Collections.unmodifiableMap(input.readMap(StreamInput::readString, - in -> { - long lastUpdate = in.readLong(); - return new Metadata(lastUpdate, in.readVInt(), in.readVInt(), in.readString(), in.readLong()); - })); + databases = Collections.unmodifiableMap(input.readMap(StreamInput::readString, in -> { + long lastUpdate = in.readLong(); + return new Metadata(lastUpdate, in.readVInt(), in.readVInt(), in.readString(), in.readLong()); + })); } public GeoIpTaskState put(String name, Metadata metadata) { @@ -147,10 +148,17 @@ static class Metadata implements ToXContentObject { private static final ParseField LAST_CHUNK = new ParseField("last_chunk"); private static final ParseField MD5 = new ParseField("md5"); - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(NAME, true, - args -> new Metadata((long) args[0], (int) args[1], (int) args[2], (String) args[3], (long) (args[4] == null ? args[0] : - args[4]))); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + true, + args -> new Metadata( + (long) args[0], + (int) args[1], + (int) args[2], + (String) args[3], + (long) (args[4] == null ? args[0] : args[4]) + ) + ); static { PARSER.declareLong(constructorArg(), LAST_UPDATE); @@ -186,7 +194,7 @@ public long getLastUpdate() { return lastUpdate; } - public boolean isCloseToExpiration(){ + public boolean isCloseToExpiration() { return Instant.ofEpochMilli(lastCheck).isBefore(Instant.now().minus(25, ChronoUnit.DAYS)); } diff --git a/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/IngestGeoIpPlugin.java b/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/IngestGeoIpPlugin.java index a66149efe519f..4b7ab9438a46e 100644 --- a/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/IngestGeoIpPlugin.java +++ b/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/IngestGeoIpPlugin.java @@ -17,7 +17,6 @@ import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; import org.elasticsearch.cluster.node.DiscoveryNodes; import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.settings.ClusterSettings; import org.elasticsearch.common.settings.IndexScopedSettings; @@ -25,8 +24,6 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsFilter; import org.elasticsearch.common.settings.SettingsModule; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.env.Environment; import org.elasticsearch.env.NodeEnvironment; import org.elasticsearch.indices.SystemIndexDescriptor; @@ -51,6 +48,9 @@ import org.elasticsearch.tasks.Task; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.watcher.ResourceWatcherService; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.Closeable; import java.io.IOException; @@ -63,16 +63,15 @@ import java.util.Set; import java.util.function.Supplier; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.index.mapper.MapperService.SINGLE_MAPPING_NAME; import static org.elasticsearch.ingest.IngestService.INGEST_ORIGIN; import static org.elasticsearch.ingest.geoip.GeoIpDownloader.DATABASES_INDEX; import static org.elasticsearch.ingest.geoip.GeoIpDownloader.DATABASES_INDEX_PATTERN; import static org.elasticsearch.ingest.geoip.GeoIpDownloader.GEOIP_DOWNLOADER; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; public class IngestGeoIpPlugin extends Plugin implements IngestPlugin, SystemIndexPlugin, Closeable, PersistentTaskPlugin, ActionPlugin { - public static final Setting CACHE_SIZE = - Setting.longSetting("ingest.geoip.cache_size", 1000, 0, Setting.Property.NodeScope); + public static final Setting CACHE_SIZE = Setting.longSetting("ingest.geoip.cache_size", 1000, 0, Setting.Property.NodeScope); static Set DEFAULT_DATABASE_FILENAMES = Set.of("GeoLite2-ASN.mmdb", "GeoLite2-City.mmdb", "GeoLite2-Country.mmdb"); @@ -82,8 +81,12 @@ public class IngestGeoIpPlugin extends Plugin implements IngestPlugin, SystemInd @Override public List> getSettings() { - return Arrays.asList(CACHE_SIZE, GeoIpDownloader.ENDPOINT_SETTING, GeoIpDownloader.POLL_INTERVAL_SETTING, - GeoIpDownloaderTaskExecutor.ENABLED_SETTING); + return Arrays.asList( + CACHE_SIZE, + GeoIpDownloader.ENDPOINT_SETTING, + GeoIpDownloader.POLL_INTERVAL_SETTING, + GeoIpDownloaderTaskExecutor.ENABLED_SETTING + ); } @Override @@ -98,17 +101,19 @@ public Map getProcessors(Processor.Parameters paramet } @Override - public Collection createComponents(Client client, - ClusterService clusterService, - ThreadPool threadPool, - ResourceWatcherService resourceWatcherService, - ScriptService scriptService, - NamedXContentRegistry xContentRegistry, - Environment environment, - NodeEnvironment nodeEnvironment, - NamedWriteableRegistry namedWriteableRegistry, - IndexNameExpressionResolver indexNameExpressionResolver, - Supplier repositoriesServiceSupplier) { + public Collection createComponents( + Client client, + ClusterService clusterService, + ThreadPool threadPool, + ResourceWatcherService resourceWatcherService, + ScriptService scriptService, + NamedXContentRegistry xContentRegistry, + Environment environment, + NodeEnvironment nodeEnvironment, + NamedWriteableRegistry namedWriteableRegistry, + IndexNameExpressionResolver indexNameExpressionResolver, + Supplier repositoriesServiceSupplier + ) { try { String nodeId = nodeEnvironment.nodeId(); databaseRegistry.get().initialize(nodeId, resourceWatcherService, ingestService.get()); @@ -126,9 +131,13 @@ public void close() throws IOException { } @Override - public List> getPersistentTasksExecutor(ClusterService clusterService, ThreadPool threadPool, - Client client, SettingsModule settingsModule, - IndexNameExpressionResolver expressionResolver) { + public List> getPersistentTasksExecutor( + ClusterService clusterService, + ThreadPool threadPool, + Client client, + SettingsModule settingsModule, + IndexNameExpressionResolver expressionResolver + ) { return List.of(geoIpDownloaderTaskExecutor); } @@ -138,25 +147,33 @@ public List> getPersistentTasksExecutor(ClusterServic } @Override - public List getRestHandlers(Settings settings, RestController restController, ClusterSettings clusterSettings, - IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter, - IndexNameExpressionResolver indexNameExpressionResolver, - Supplier nodesInCluster) { + public List getRestHandlers( + Settings settings, + RestController restController, + ClusterSettings clusterSettings, + IndexScopedSettings indexScopedSettings, + SettingsFilter settingsFilter, + IndexNameExpressionResolver indexNameExpressionResolver, + Supplier nodesInCluster + ) { return List.of(new RestGeoIpDownloaderStatsAction()); } @Override public List getNamedXContent() { - return List.of(new NamedXContentRegistry.Entry(PersistentTaskParams.class, new ParseField(GEOIP_DOWNLOADER), - GeoIpTaskParams::fromXContent), - new NamedXContentRegistry.Entry(PersistentTaskState.class, new ParseField(GEOIP_DOWNLOADER), GeoIpTaskState::fromXContent)); + return List.of( + new NamedXContentRegistry.Entry(PersistentTaskParams.class, new ParseField(GEOIP_DOWNLOADER), GeoIpTaskParams::fromXContent), + new NamedXContentRegistry.Entry(PersistentTaskState.class, new ParseField(GEOIP_DOWNLOADER), GeoIpTaskState::fromXContent) + ); } @Override public List getNamedWriteables() { - return List.of(new NamedWriteableRegistry.Entry(PersistentTaskState.class, GEOIP_DOWNLOADER, GeoIpTaskState::new), + return List.of( + new NamedWriteableRegistry.Entry(PersistentTaskState.class, GEOIP_DOWNLOADER, GeoIpTaskState::new), new NamedWriteableRegistry.Entry(PersistentTaskParams.class, GEOIP_DOWNLOADER, GeoIpTaskParams::new), - new NamedWriteableRegistry.Entry(Task.Status.class, GEOIP_DOWNLOADER, GeoIpDownloaderStats::new)); + new NamedWriteableRegistry.Entry(Task.Status.class, GEOIP_DOWNLOADER, GeoIpDownloaderStats::new) + ); } @Override @@ -165,11 +182,13 @@ public Collection getSystemIndexDescriptors(Settings sett .setIndexPattern(DATABASES_INDEX_PATTERN) .setDescription("GeoIP databases") .setMappings(mappings()) - .setSettings(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put(IndexMetadata.SETTING_AUTO_EXPAND_REPLICAS, "0-1") - .build()) + .setSettings( + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put(IndexMetadata.SETTING_AUTO_EXPAND_REPLICAS, "0-1") + .build() + ) .setOrigin(INGEST_ORIGIN) .setVersionMetaKey("version") .setPrimaryIndex(DATABASES_INDEX) @@ -190,8 +209,7 @@ public String getFeatureDescription() { private static XContentBuilder mappings() { try { - return jsonBuilder() - .startObject() + return jsonBuilder().startObject() .startObject(SINGLE_MAPPING_NAME) .startObject("_meta") .field("version", Version.CURRENT) diff --git a/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/TarInputStream.java b/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/TarInputStream.java index 2b1d4e98bebef..a9fc2f20503a3 100644 --- a/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/TarInputStream.java +++ b/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/TarInputStream.java @@ -32,7 +32,7 @@ class TarInputStream extends FilterInputStream { public TarEntry getNextEntry() throws IOException { if (currentEntry != null) { - //go to the end of the current entry + // go to the end of the current entry skipN(remaining); if (reminder != 0) { skipN(512 - reminder); @@ -53,7 +53,7 @@ public TarEntry getNextEntry() throws IOException { boolean notFile = (buf[156] != 0 && buf[156] != '0') || name.endsWith("/"); - if(notFile){ + if (notFile) { remaining = 0; reminder = 0; } else { @@ -121,4 +121,3 @@ public boolean isNotFile() { } } } - diff --git a/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/stats/GeoIpDownloaderStats.java b/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/stats/GeoIpDownloaderStats.java index 36d2e5be9757a..88b7693f3f5bf 100644 --- a/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/stats/GeoIpDownloaderStats.java +++ b/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/stats/GeoIpDownloaderStats.java @@ -11,12 +11,12 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.ingest.geoip.GeoIpDownloader; +import org.elasticsearch.tasks.Task; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.ingest.geoip.GeoIpDownloader; -import org.elasticsearch.tasks.Task; import java.io.IOException; import java.util.Objects; @@ -26,8 +26,9 @@ public class GeoIpDownloaderStats implements Task.Status { public static final GeoIpDownloaderStats EMPTY = new GeoIpDownloaderStats(0, 0, 0, 0, 0, 0); public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "geoip_downloader_stats", a -> new GeoIpDownloaderStats((int) a[0], (int) a[1], (long) a[2], (int) a[3], (int) a[4], - a[5] == null ? 0 : (int) a[5])); + "geoip_downloader_stats", + a -> new GeoIpDownloaderStats((int) a[0], (int) a[1], (long) a[2], (int) a[3], (int) a[4], a[5] == null ? 0 : (int) a[5]) + ); private static final ParseField SUCCESSFUL_DOWNLOADS = new ParseField("successful_downloads"); private static final ParseField FAILED_DOWNLOADS = new ParseField("failed_downloads"); @@ -61,8 +62,14 @@ public GeoIpDownloaderStats(StreamInput in) throws IOException { expiredDatabases = in.readVInt(); } - private GeoIpDownloaderStats(int successfulDownloads, int failedDownloads, long totalDownloadTime, int databasesCount, - int skippedDownloads, int expiredDatabases) { + private GeoIpDownloaderStats( + int successfulDownloads, + int failedDownloads, + long totalDownloadTime, + int databasesCount, + int skippedDownloads, + int expiredDatabases + ) { this.successfulDownloads = successfulDownloads; this.failedDownloads = failedDownloads; this.totalDownloadTime = totalDownloadTime; @@ -96,28 +103,58 @@ public int getExpiredDatabases() { } public GeoIpDownloaderStats skippedDownload() { - return new GeoIpDownloaderStats(successfulDownloads, failedDownloads, totalDownloadTime, databasesCount, skippedDownloads + 1, - expiredDatabases); + return new GeoIpDownloaderStats( + successfulDownloads, + failedDownloads, + totalDownloadTime, + databasesCount, + skippedDownloads + 1, + expiredDatabases + ); } public GeoIpDownloaderStats successfulDownload(long downloadTime) { - return new GeoIpDownloaderStats(successfulDownloads + 1, failedDownloads, totalDownloadTime + Math.max(downloadTime, 0), - databasesCount, skippedDownloads, expiredDatabases); + return new GeoIpDownloaderStats( + successfulDownloads + 1, + failedDownloads, + totalDownloadTime + Math.max(downloadTime, 0), + databasesCount, + skippedDownloads, + expiredDatabases + ); } public GeoIpDownloaderStats failedDownload() { - return new GeoIpDownloaderStats(successfulDownloads, failedDownloads + 1, totalDownloadTime, databasesCount, skippedDownloads, - expiredDatabases); + return new GeoIpDownloaderStats( + successfulDownloads, + failedDownloads + 1, + totalDownloadTime, + databasesCount, + skippedDownloads, + expiredDatabases + ); } public GeoIpDownloaderStats count(int databasesCount) { - return new GeoIpDownloaderStats(successfulDownloads, failedDownloads, totalDownloadTime, databasesCount, skippedDownloads, - expiredDatabases); + return new GeoIpDownloaderStats( + successfulDownloads, + failedDownloads, + totalDownloadTime, + databasesCount, + skippedDownloads, + expiredDatabases + ); } public GeoIpDownloaderStats expiredDatabases(int expiredDatabases) { - return new GeoIpDownloaderStats(successfulDownloads, failedDownloads, totalDownloadTime, databasesCount, skippedDownloads, - expiredDatabases); + return new GeoIpDownloaderStats( + successfulDownloads, + failedDownloads, + totalDownloadTime, + databasesCount, + skippedDownloads, + expiredDatabases + ); } @Override @@ -152,12 +189,12 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; GeoIpDownloaderStats that = (GeoIpDownloaderStats) o; - return successfulDownloads == that.successfulDownloads && - failedDownloads == that.failedDownloads && - totalDownloadTime == that.totalDownloadTime && - databasesCount == that.databasesCount && - skippedDownloads == that.skippedDownloads && - expiredDatabases == that.expiredDatabases; + return successfulDownloads == that.successfulDownloads + && failedDownloads == that.failedDownloads + && totalDownloadTime == that.totalDownloadTime + && databasesCount == that.databasesCount + && skippedDownloads == that.skippedDownloads + && expiredDatabases == that.expiredDatabases; } @Override diff --git a/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/stats/GeoIpDownloaderStatsAction.java b/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/stats/GeoIpDownloaderStatsAction.java index 72790c9478ba8..a160dfeec9b4a 100644 --- a/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/stats/GeoIpDownloaderStatsAction.java +++ b/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/stats/GeoIpDownloaderStatsAction.java @@ -19,9 +19,9 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.transport.TransportRequest; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.transport.TransportRequest; import java.io.IOException; import java.util.List; @@ -169,8 +169,13 @@ protected NodeResponse(StreamInput in) throws IOException { configDatabases = in.getVersion().onOrAfter(Version.V_8_0_0) ? in.readSet(StreamInput::readString) : null; } - protected NodeResponse(DiscoveryNode node, GeoIpDownloaderStats stats, Set databases, Set filesInTemp, - Set configDatabases) { + protected NodeResponse( + DiscoveryNode node, + GeoIpDownloaderStats stats, + Set databases, + Set filesInTemp, + Set configDatabases + ) { super(node); this.stats = stats; this.databases = databases; @@ -213,10 +218,10 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; NodeResponse that = (NodeResponse) o; - return stats.equals(that.stats) && - databases.equals(that.databases) && - filesInTemp.equals(that.filesInTemp) && - Objects.equals(configDatabases, that.configDatabases); + return stats.equals(that.stats) + && databases.equals(that.databases) + && filesInTemp.equals(that.filesInTemp) + && Objects.equals(configDatabases, that.configDatabases); } @Override diff --git a/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/stats/GeoIpDownloaderStatsTransportAction.java b/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/stats/GeoIpDownloaderStatsTransportAction.java index 4248560039612..93a86b35582e2 100644 --- a/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/stats/GeoIpDownloaderStatsTransportAction.java +++ b/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/stats/GeoIpDownloaderStatsTransportAction.java @@ -29,19 +29,32 @@ import java.io.IOException; import java.util.List; -public class GeoIpDownloaderStatsTransportAction extends TransportNodesAction { +public class GeoIpDownloaderStatsTransportAction extends TransportNodesAction { private final TransportService transportService; private final DatabaseNodeService registry; private final GeoIpDownloaderTaskExecutor geoIpDownloaderTaskExecutor; @Inject - public GeoIpDownloaderStatsTransportAction(TransportService transportService, ClusterService clusterService, - ThreadPool threadPool, ActionFilters actionFilters, DatabaseNodeService registry, - GeoIpDownloaderTaskExecutor geoIpDownloaderTaskExecutor) { - super(GeoIpDownloaderStatsAction.NAME, threadPool, clusterService, transportService, actionFilters, Request::new, - NodeRequest::new, ThreadPool.Names.MANAGEMENT, NodeResponse.class); + public GeoIpDownloaderStatsTransportAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + DatabaseNodeService registry, + GeoIpDownloaderTaskExecutor geoIpDownloaderTaskExecutor + ) { + super( + GeoIpDownloaderStatsAction.NAME, + threadPool, + clusterService, + transportService, + actionFilters, + Request::new, + NodeRequest::new, + ThreadPool.Names.MANAGEMENT, + NodeResponse.class + ); this.transportService = transportService; this.registry = registry; this.geoIpDownloaderTaskExecutor = geoIpDownloaderTaskExecutor; @@ -66,7 +79,12 @@ protected NodeResponse newNodeResponse(StreamInput in, DiscoveryNode node) throw protected NodeResponse nodeOperation(NodeRequest request, Task task) { GeoIpDownloader geoIpTask = geoIpDownloaderTaskExecutor.getCurrentTask(); GeoIpDownloaderStats stats = geoIpTask == null || geoIpTask.getStatus() == null ? null : geoIpTask.getStatus(); - return new NodeResponse(transportService.getLocalNode(), stats, registry.getAvailableDatabases(), registry.getFilesInTemp(), - registry.getConfigDatabases()); + return new NodeResponse( + transportService.getLocalNode(), + stats, + registry.getAvailableDatabases(), + registry.getFilesInTemp(), + registry.getConfigDatabases() + ); } } diff --git a/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/stats/RestGeoIpDownloaderStatsAction.java b/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/stats/RestGeoIpDownloaderStatsAction.java index 5d20480565265..10bc863c912af 100644 --- a/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/stats/RestGeoIpDownloaderStatsAction.java +++ b/modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/stats/RestGeoIpDownloaderStatsAction.java @@ -31,7 +31,10 @@ public List routes() { @Override protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) { - return channel -> client.execute(GeoIpDownloaderStatsAction.INSTANCE, new GeoIpDownloaderStatsAction.Request(), - new RestToXContentListener<>(channel)); + return channel -> client.execute( + GeoIpDownloaderStatsAction.INSTANCE, + new GeoIpDownloaderStatsAction.Request(), + new RestToXContentListener<>(channel) + ); } } diff --git a/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/ConfigDatabasesTests.java b/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/ConfigDatabasesTests.java index 6834d32838c35..88deaa27383dd 100644 --- a/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/ConfigDatabasesTests.java +++ b/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/ConfigDatabasesTests.java @@ -9,6 +9,7 @@ package org.elasticsearch.ingest.geoip; import com.maxmind.geoip2.model.CityResponse; + import org.elasticsearch.common.network.InetAddresses; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.TimeValue; @@ -126,8 +127,11 @@ public void testDatabasesUpdateExistingConfDatabase() throws Exception { assertThat(cache.count(), equalTo(1)); } - Files.copy(ConfigDatabases.class.getResourceAsStream("/GeoLite2-City-Test.mmdb"), configDir.resolve("GeoLite2-City.mmdb"), - StandardCopyOption.REPLACE_EXISTING); + Files.copy( + ConfigDatabases.class.getResourceAsStream("/GeoLite2-City-Test.mmdb"), + configDir.resolve("GeoLite2-City.mmdb"), + StandardCopyOption.REPLACE_EXISTING + ); assertBusy(() -> { assertThat(configDatabases.getConfigDatabases().size(), equalTo(1)); assertThat(cache.count(), equalTo(0)); diff --git a/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/DatabaseNodeServiceTests.java b/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/DatabaseNodeServiceTests.java index 7ea2ebf4c1980..82cd1a2cb5b49 100644 --- a/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/DatabaseNodeServiceTests.java +++ b/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/DatabaseNodeServiceTests.java @@ -9,6 +9,7 @@ package org.elasticsearch.ingest.geoip; import com.maxmind.db.InvalidDatabaseException; + import org.apache.lucene.search.TotalHits; import org.apache.lucene.util.LuceneTestCase; import org.elasticsearch.Version; @@ -29,15 +30,13 @@ import org.elasticsearch.cluster.routing.RoutingTable; import org.elasticsearch.cluster.routing.ShardRouting; import org.elasticsearch.cluster.routing.UnassignedInfo; -import org.elasticsearch.core.CheckedConsumer; -import org.elasticsearch.core.CheckedRunnable; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.hash.MessageDigests; import org.elasticsearch.common.io.Streams; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.CheckedConsumer; +import org.elasticsearch.core.CheckedRunnable; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.Index; import org.elasticsearch.index.query.TermQueryBuilder; import org.elasticsearch.index.shard.ShardId; @@ -49,6 +48,8 @@ import org.elasticsearch.threadpool.TestThreadPool; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.watcher.ResourceWatcherService; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; import org.junit.After; import org.junit.Before; import org.mockito.ArgumentCaptor; @@ -134,15 +135,15 @@ public void testCheckDatabases() throws Exception { String md5 = mockSearches("GeoIP2-City.mmdb", 5, 14); String taskId = GeoIpDownloader.GEOIP_DOWNLOADER; PersistentTask task = new PersistentTask<>(taskId, GeoIpDownloader.GEOIP_DOWNLOADER, new GeoIpTaskParams(), 1, null); - task = new PersistentTask<>(task, new GeoIpTaskState(Map.of("GeoIP2-City.mmdb", - new GeoIpTaskState.Metadata(10, 5, 14, md5, 10)))); + task = new PersistentTask<>(task, new GeoIpTaskState(Map.of("GeoIP2-City.mmdb", new GeoIpTaskState.Metadata(10, 5, 14, md5, 10)))); PersistentTasksCustomMetadata tasksCustomMetadata = new PersistentTasksCustomMetadata(1L, Map.of(taskId, task)); ClusterState state = ClusterState.builder(new ClusterName("name")) .metadata(Metadata.builder().putCustom(TYPE, tasksCustomMetadata).build()) - .nodes(new DiscoveryNodes.Builder() - .add(new DiscoveryNode("_id1", buildNewFakeTransportAddress(), Version.CURRENT)) - .localNodeId("_id1")) + .nodes( + new DiscoveryNodes.Builder().add(new DiscoveryNode("_id1", buildNewFakeTransportAddress(), Version.CURRENT)) + .localNodeId("_id1") + ) .routingTable(createIndexRoutingTable()) .build(); @@ -161,15 +162,18 @@ public void testCheckDatabases() throws Exception { assertEquals(0, files.count()); } - task = new PersistentTask<>(task, new GeoIpTaskState(Map.of("GeoIP2-City.mmdb", - new GeoIpTaskState.Metadata(10, 5, 14, md5, System.currentTimeMillis())))); + task = new PersistentTask<>( + task, + new GeoIpTaskState(Map.of("GeoIP2-City.mmdb", new GeoIpTaskState.Metadata(10, 5, 14, md5, System.currentTimeMillis()))) + ); tasksCustomMetadata = new PersistentTasksCustomMetadata(1L, Map.of(taskId, task)); state = ClusterState.builder(new ClusterName("name")) .metadata(Metadata.builder().putCustom(TYPE, tasksCustomMetadata).build()) - .nodes(new DiscoveryNodes.Builder() - .add(new DiscoveryNode("_id1", buildNewFakeTransportAddress(), Version.CURRENT)) - .localNodeId("_id1")) + .nodes( + new DiscoveryNodes.Builder().add(new DiscoveryNode("_id1", buildNewFakeTransportAddress(), Version.CURRENT)) + .localNodeId("_id1") + ) .routingTable(createIndexRoutingTable()) .build(); // Database should be downloaded @@ -182,7 +186,7 @@ public void testCheckDatabases() throws Exception { } // First time GeoIP2-City.mmdb is downloaded, so a pipeline reload can happen: verify(ingestService, times(numPipelinesToBeReloaded)).reloadPipeline(anyString()); - //30 days check passed but we mocked mmdb data so parsing will fail + // 30 days check passed but we mocked mmdb data so parsing will fail expectThrows(InvalidDatabaseException.class, database::get); } @@ -195,10 +199,18 @@ public void testCheckDatabases_dontCheckDatabaseOnNonIngestNode() throws Excepti ClusterState state = ClusterState.builder(new ClusterName("name")) .metadata(Metadata.builder().putCustom(TYPE, tasksCustomMetadata).build()) - .nodes(new DiscoveryNodes.Builder() - .add(new DiscoveryNode("_name1", "_id1", buildNewFakeTransportAddress(), Map.of(), - Set.of(DiscoveryNodeRole.MASTER_ROLE), Version.CURRENT)) - .localNodeId("_id1")) + .nodes( + new DiscoveryNodes.Builder().add( + new DiscoveryNode( + "_name1", + "_id1", + buildNewFakeTransportAddress(), + Map.of(), + Set.of(DiscoveryNodeRole.MASTER_ROLE), + Version.CURRENT + ) + ).localNodeId("_id1") + ) .routingTable(createIndexRoutingTable()) .build(); @@ -219,9 +231,10 @@ public void testCheckDatabases_dontCheckDatabaseWhenNoDatabasesIndex() throws Ex ClusterState state = ClusterState.builder(new ClusterName("name")) .metadata(Metadata.builder().putCustom(TYPE, tasksCustomMetadata).build()) - .nodes(new DiscoveryNodes.Builder() - .add(new DiscoveryNode("_id1", buildNewFakeTransportAddress(), Version.CURRENT)) - .localNodeId("_id1")) + .nodes( + new DiscoveryNodes.Builder().add(new DiscoveryNode("_id1", buildNewFakeTransportAddress(), Version.CURRENT)) + .localNodeId("_id1") + ) .build(); databaseNodeService.checkDatabases(state); @@ -237,9 +250,10 @@ public void testCheckDatabases_dontCheckDatabaseWhenGeoIpDownloadTask() throws E ClusterState state = ClusterState.builder(new ClusterName("name")) .metadata(Metadata.builder().putCustom(TYPE, tasksCustomMetadata).build()) - .nodes(new DiscoveryNodes.Builder() - .add(new DiscoveryNode("_id1", buildNewFakeTransportAddress(), Version.CURRENT)) - .localNodeId("_id1")) + .nodes( + new DiscoveryNodes.Builder().add(new DiscoveryNode("_id1", buildNewFakeTransportAddress(), Version.CURRENT)) + .localNodeId("_id1") + ) .routingTable(createIndexRoutingTable()) .build(); @@ -285,8 +299,10 @@ public void testRetrieveDatabaseCorruption() throws Exception { ArgumentCaptor exceptionCaptor = ArgumentCaptor.forClass(Exception.class); verify(failureHandler, times(1)).accept(exceptionCaptor.capture()); assertThat(exceptionCaptor.getAllValues().size(), equalTo(1)); - assertThat(exceptionCaptor.getAllValues().get(0).getMessage(), equalTo("expected md5 hash [different], " + - "but got md5 hash [" + md5 + "]")); + assertThat( + exceptionCaptor.getAllValues().get(0).getMessage(), + equalTo("expected md5 hash [different], " + "but got md5 hash [" + md5 + "]") + ); verify(chunkConsumer, times(10)).accept(any()); verify(completedHandler, times(0)).run(); verify(client, times(10)).search(any()); @@ -329,9 +345,17 @@ private String mockSearches(String databaseName, int firstChunk, int lastChunk) throw new UncheckedIOException(ex); } - SearchHits hits = new SearchHits(new SearchHit[]{hit}, new TotalHits(1, TotalHits.Relation.EQUAL_TO), 1f); - SearchResponse searchResponse = - new SearchResponse(new SearchResponseSections(hits, null, null, false, null, null, 0), null, 1, 1, 0, 1L, null, null); + SearchHits hits = new SearchHits(new SearchHit[] { hit }, new TotalHits(1, TotalHits.Relation.EQUAL_TO), 1f); + SearchResponse searchResponse = new SearchResponse( + new SearchResponseSections(hits, null, null, false, null, null, 0), + null, + 1, + 1, + 0, + 1L, + null, + null + ); @SuppressWarnings("unchecked") ActionFuture actionFuture = mock(ActionFuture.class); when(actionFuture.actionGet()).thenReturn(searchResponse); @@ -384,7 +408,7 @@ private static List gzip(String name, String content, int chunks) throws int chunkSize = all.length / chunks; List data = new ArrayList<>(); - for (int from = 0; from < all.length; ) { + for (int from = 0; from < all.length;) { int to = from + chunkSize; if (to > all.length) { to = all.length; diff --git a/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/GeoIpCacheTests.java b/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/GeoIpCacheTests.java index 9826dcda88151..4d68ac7806edf 100644 --- a/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/GeoIpCacheTests.java +++ b/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/GeoIpCacheTests.java @@ -9,6 +9,7 @@ package org.elasticsearch.ingest.geoip; import com.maxmind.geoip2.model.AbstractResponse; + import org.elasticsearch.common.network.InetAddresses; import org.elasticsearch.test.ESTestCase; @@ -21,13 +22,12 @@ public void testCachesAndEvictsResults() { AbstractResponse response1 = mock(AbstractResponse.class); AbstractResponse response2 = mock(AbstractResponse.class); - //add a key + // add a key AbstractResponse cachedResponse = cache.putIfAbsent(InetAddresses.forString("127.0.0.1"), "path/to/db", ip -> response1); assertSame(cachedResponse, response1); assertSame(cachedResponse, cache.putIfAbsent(InetAddresses.forString("127.0.0.1"), "path/to/db", ip -> response1)); assertSame(cachedResponse, cache.get(InetAddresses.forString("127.0.0.1"), "path/to/db")); - // evict old key by adding another value cachedResponse = cache.putIfAbsent(InetAddresses.forString("127.0.0.2"), "path/to/db", ip -> response2); assertSame(cachedResponse, response2); @@ -49,14 +49,19 @@ public void testCacheKey() { public void testThrowsFunctionsException() { GeoIpCache cache = new GeoIpCache(1); - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, - () -> cache.putIfAbsent(InetAddresses.forString("127.0.0.1"), "path/to/db", - ip -> { throw new IllegalArgumentException("bad"); })); + IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> cache.putIfAbsent( + InetAddresses.forString("127.0.0.1"), + "path/to/db", + ip -> { throw new IllegalArgumentException("bad"); } + ) + ); assertEquals("bad", ex.getMessage()); } public void testInvalidInit() { - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> new GeoIpCache(-1)); + IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> new GeoIpCache(-1)); assertEquals("geoip max cache size must be 0 or greater", ex.getMessage()); } } diff --git a/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/GeoIpDownloaderTests.java b/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/GeoIpDownloaderTests.java index 131d1100b0df5..5d9ec2183513e 100644 --- a/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/GeoIpDownloaderTests.java +++ b/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/GeoIpDownloaderTests.java @@ -27,14 +27,14 @@ import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.settings.ClusterSettings; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.node.Node; import org.elasticsearch.persistent.PersistentTaskState; import org.elasticsearch.persistent.PersistentTasksCustomMetadata.PersistentTask; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.client.NoOpClient; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; import org.hamcrest.Matchers; import org.junit.After; import org.junit.Before; @@ -71,13 +71,28 @@ public void setup() { httpClient = mock(HttpClient.class); clusterService = mock(ClusterService.class); threadPool = new ThreadPool(Settings.builder().put(Node.NODE_NAME_SETTING.getKey(), "test").build()); - when(clusterService.getClusterSettings()).thenReturn(new ClusterSettings(Settings.EMPTY, - Set.of(GeoIpDownloader.ENDPOINT_SETTING, GeoIpDownloader.POLL_INTERVAL_SETTING, GeoIpDownloaderTaskExecutor.ENABLED_SETTING))); + when(clusterService.getClusterSettings()).thenReturn( + new ClusterSettings( + Settings.EMPTY, + Set.of(GeoIpDownloader.ENDPOINT_SETTING, GeoIpDownloader.POLL_INTERVAL_SETTING, GeoIpDownloaderTaskExecutor.ENABLED_SETTING) + ) + ); ClusterState state = ClusterState.builder(ClusterName.DEFAULT).build(); when(clusterService.state()).thenReturn(state); client = new MockClient(threadPool); - geoIpDownloader = new GeoIpDownloader(client, httpClient, clusterService, threadPool, Settings.EMPTY, - 1, "", "", "", EMPTY_TASK_ID, Collections.emptyMap()); + geoIpDownloader = new GeoIpDownloader( + client, + httpClient, + clusterService, + threadPool, + Settings.EMPTY, + 1, + "", + "", + "", + EMPTY_TASK_ID, + Collections.emptyMap() + ); } @After @@ -99,9 +114,9 @@ public int read() { } public void testGetChunkLessThanChunkSize() throws IOException { - ByteArrayInputStream is = new ByteArrayInputStream(new byte[]{1, 2, 3, 4}); + ByteArrayInputStream is = new ByteArrayInputStream(new byte[] { 1, 2, 3, 4 }); byte[] chunk = geoIpDownloader.getChunk(is); - assertArrayEquals(new byte[]{1, 2, 3, 4}, chunk); + assertArrayEquals(new byte[] { 1, 2, 3, 4 }, chunk); chunk = geoIpDownloader.getChunk(is); assertArrayEquals(new byte[0], chunk); @@ -147,11 +162,11 @@ public int read() throws IOException { public void testIndexChunksNoData() throws IOException { client.addHandler(FlushAction.INSTANCE, (FlushRequest request, ActionListener flushResponseActionListener) -> { - assertArrayEquals(new String[] {GeoIpDownloader.DATABASES_INDEX}, request.indices()); + assertArrayEquals(new String[] { GeoIpDownloader.DATABASES_INDEX }, request.indices()); flushResponseActionListener.onResponse(mock(FlushResponse.class)); }); client.addHandler(RefreshAction.INSTANCE, (RefreshRequest request, ActionListener flushResponseActionListener) -> { - assertArrayEquals(new String[] {GeoIpDownloader.DATABASES_INDEX}, request.indices()); + assertArrayEquals(new String[] { GeoIpDownloader.DATABASES_INDEX }, request.indices()); flushResponseActionListener.onResponse(mock(RefreshResponse.class)); }); @@ -161,16 +176,18 @@ public void testIndexChunksNoData() throws IOException { public void testIndexChunksMd5Mismatch() { client.addHandler(FlushAction.INSTANCE, (FlushRequest request, ActionListener flushResponseActionListener) -> { - assertArrayEquals(new String[] {GeoIpDownloader.DATABASES_INDEX}, request.indices()); + assertArrayEquals(new String[] { GeoIpDownloader.DATABASES_INDEX }, request.indices()); flushResponseActionListener.onResponse(mock(FlushResponse.class)); }); client.addHandler(RefreshAction.INSTANCE, (RefreshRequest request, ActionListener flushResponseActionListener) -> { - assertArrayEquals(new String[] {GeoIpDownloader.DATABASES_INDEX}, request.indices()); + assertArrayEquals(new String[] { GeoIpDownloader.DATABASES_INDEX }, request.indices()); flushResponseActionListener.onResponse(mock(RefreshResponse.class)); }); - IOException exception = expectThrows(IOException.class, () -> geoIpDownloader.indexChunks("test", - new ByteArrayInputStream(new byte[0]), 0, "123123", 0)); + IOException exception = expectThrows( + IOException.class, + () -> geoIpDownloader.indexChunks("test", new ByteArrayInputStream(new byte[0]), 0, "123123", 0) + ); assertEquals("md5 checksum mismatch, expected [123123], actual [d41d8cd98f00b204e9800998ecf8427e]", exception.getMessage()); } @@ -199,11 +216,11 @@ public void testIndexChunks() throws IOException { listener.onResponse(mock(IndexResponse.class)); }); client.addHandler(FlushAction.INSTANCE, (FlushRequest request, ActionListener flushResponseActionListener) -> { - assertArrayEquals(new String[] {GeoIpDownloader.DATABASES_INDEX}, request.indices()); + assertArrayEquals(new String[] { GeoIpDownloader.DATABASES_INDEX }, request.indices()); flushResponseActionListener.onResponse(mock(FlushResponse.class)); }); client.addHandler(RefreshAction.INSTANCE, (RefreshRequest request, ActionListener flushResponseActionListener) -> { - assertArrayEquals(new String[] {GeoIpDownloader.DATABASES_INDEX}, request.indices()); + assertArrayEquals(new String[] { GeoIpDownloader.DATABASES_INDEX }, request.indices()); flushResponseActionListener.onResponse(mock(RefreshResponse.class)); }); @@ -217,8 +234,19 @@ public void testProcessDatabaseNew() throws IOException { ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]); when(httpClient.get("http://a.b/t1")).thenReturn(bais); - geoIpDownloader = new GeoIpDownloader(client, httpClient, clusterService, threadPool, Settings.EMPTY, - 1, "", "", "", EMPTY_TASK_ID, Collections.emptyMap()) { + geoIpDownloader = new GeoIpDownloader( + client, + httpClient, + clusterService, + threadPool, + Settings.EMPTY, + 1, + "", + "", + "", + EMPTY_TASK_ID, + Collections.emptyMap() + ) { @Override void updateTaskState() { assertEquals(0, state.get("test").getFirstChunk()); @@ -252,8 +280,19 @@ public void testProcessDatabaseUpdate() throws IOException { ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]); when(httpClient.get("http://a.b/t1")).thenReturn(bais); - geoIpDownloader = new GeoIpDownloader(client, httpClient, clusterService, threadPool, Settings.EMPTY, - 1, "", "", "", EMPTY_TASK_ID, Collections.emptyMap()) { + geoIpDownloader = new GeoIpDownloader( + client, + httpClient, + clusterService, + threadPool, + Settings.EMPTY, + 1, + "", + "", + "", + EMPTY_TASK_ID, + Collections.emptyMap() + ) { @Override void updateTaskState() { assertEquals(9, state.get("test.mmdb").getFirstChunk()); @@ -283,15 +322,25 @@ void deleteOldChunks(String name, int firstChunk) { geoIpDownloader.processDatabase(Map.of("name", "test.tgz", "url", "http://a.b/t1", "md5_hash", "1")); } - public void testProcessDatabaseSame() throws IOException { GeoIpTaskState.Metadata metadata = new GeoIpTaskState.Metadata(0, 4, 10, "1", 0); GeoIpTaskState taskState = GeoIpTaskState.EMPTY.put("test.mmdb", metadata); ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]); when(httpClient.get("a.b/t1")).thenReturn(bais); - geoIpDownloader = new GeoIpDownloader(client, httpClient, clusterService, threadPool, Settings.EMPTY, - 1, "", "", "", EMPTY_TASK_ID, Collections.emptyMap()) { + geoIpDownloader = new GeoIpDownloader( + client, + httpClient, + clusterService, + threadPool, + Settings.EMPTY, + 1, + "", + "", + "", + EMPTY_TASK_ID, + Collections.emptyMap() + ) { @Override void updateTaskState() { fail(); @@ -320,8 +369,19 @@ void deleteOldChunks(String name, int firstChunk) { @SuppressWarnings("unchecked") public void testUpdateTaskState() { - geoIpDownloader = new GeoIpDownloader(client, httpClient, clusterService, threadPool, Settings.EMPTY, - 1, "", "", "", EMPTY_TASK_ID, Collections.emptyMap()) { + geoIpDownloader = new GeoIpDownloader( + client, + httpClient, + clusterService, + threadPool, + Settings.EMPTY, + 1, + "", + "", + "", + EMPTY_TASK_ID, + Collections.emptyMap() + ) { @Override public void updatePersistentTaskState(PersistentTaskState state, ActionListener> listener) { assertSame(GeoIpTaskState.EMPTY, state); @@ -336,8 +396,19 @@ public void updatePersistentTaskState(PersistentTaskState state, ActionListener< @SuppressWarnings("unchecked") public void testUpdateTaskStateError() { - geoIpDownloader = new GeoIpDownloader(client, httpClient, clusterService, threadPool, Settings.EMPTY, - 1, "", "", "", EMPTY_TASK_ID, Collections.emptyMap()) { + geoIpDownloader = new GeoIpDownloader( + client, + httpClient, + clusterService, + threadPool, + Settings.EMPTY, + 1, + "", + "", + "", + EMPTY_TASK_ID, + Collections.emptyMap() + ) { @Override public void updatePersistentTaskState(PersistentTaskState state, ActionListener> listener) { assertSame(GeoIpTaskState.EMPTY, state); @@ -360,12 +431,21 @@ public void testUpdateDatabases() throws IOException { builder.map(Map.of("a", 2, "name", "a.tgz")); builder.endArray(); builder.close(); - when(httpClient.getBytes("a.b?elastic_geoip_service_tos=agree")) - .thenReturn(baos.toByteArray()); + when(httpClient.getBytes("a.b?elastic_geoip_service_tos=agree")).thenReturn(baos.toByteArray()); Iterator> it = maps.iterator(); - geoIpDownloader = new GeoIpDownloader(client, httpClient, clusterService, threadPool, + geoIpDownloader = new GeoIpDownloader( + client, + httpClient, + clusterService, + threadPool, Settings.builder().put(ENDPOINT_SETTING.getKey(), "a.b").build(), - 1, "", "", "", EMPTY_TASK_ID, Collections.emptyMap()) { + 1, + "", + "", + "", + EMPTY_TASK_ID, + Collections.emptyMap() + ) { @Override void processDatabase(Map databaseInfo) { assertEquals(it.next(), databaseInfo); @@ -383,20 +463,24 @@ private MockClient(ThreadPool threadPool) { super(threadPool); } - public void addHandler(ActionType action, - BiConsumer> listener) { + public void addHandler( + ActionType action, + BiConsumer> listener + ) { handlers.put(action, listener); } @SuppressWarnings("unchecked") @Override - protected void doExecute(ActionType action, - Request request, - ActionListener listener) { + protected void doExecute( + ActionType action, + Request request, + ActionListener listener + ) { if (handlers.containsKey(action)) { - BiConsumer> biConsumer = - (BiConsumer>) handlers.get(action); + BiConsumer> biConsumer = (BiConsumer>) handlers.get( + action + ); biConsumer.accept(request, listener); } else { throw new IllegalStateException("unexpected action called [" + action.name() + "]"); diff --git a/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/GeoIpProcessorFactoryTests.java b/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/GeoIpProcessorFactoryTests.java index f11e4ec7ed692..b382506424bfd 100644 --- a/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/GeoIpProcessorFactoryTests.java +++ b/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/GeoIpProcessorFactoryTests.java @@ -187,8 +187,14 @@ public void testBuildWithCountryDbAndAsnFields() throws Exception { String asnProperty = RandomPicks.randomFrom(Randomness.get(), asnOnlyProperties).toString(); config.put("properties", Collections.singletonList(asnProperty)); Exception e = expectThrows(ElasticsearchParseException.class, () -> factory.create(null, null, null, config)); - assertThat(e.getMessage(), equalTo("[properties] illegal property value [" + asnProperty + - "]. valid values are [IP, COUNTRY_ISO_CODE, COUNTRY_NAME, CONTINENT_NAME]")); + assertThat( + e.getMessage(), + equalTo( + "[properties] illegal property value [" + + asnProperty + + "]. valid values are [IP, COUNTRY_ISO_CODE, COUNTRY_NAME, CONTINENT_NAME]" + ) + ); } public void testBuildWithAsnDbAndCityFields() throws Exception { @@ -201,13 +207,17 @@ public void testBuildWithAsnDbAndCityFields() throws Exception { String cityProperty = RandomPicks.randomFrom(Randomness.get(), cityOnlyProperties).toString(); config.put("properties", Collections.singletonList(cityProperty)); Exception e = expectThrows(ElasticsearchParseException.class, () -> factory.create(null, null, null, config)); - assertThat(e.getMessage(), equalTo("[properties] illegal property value [" + cityProperty + - "]. valid values are [IP, ASN, ORGANIZATION_NAME, NETWORK]")); + assertThat( + e.getMessage(), + equalTo("[properties] illegal property value [" + cityProperty + "]. valid values are [IP, ASN, ORGANIZATION_NAME, NETWORK]") + ); } public void testBuildNonExistingDbFile() throws Exception { - Files.copy(GeoIpProcessorFactoryTests.class.getResourceAsStream("/GeoLite2-City-Test.mmdb"), - geoipTmpDir.resolve("GeoLite2-City.mmdb")); + Files.copy( + GeoIpProcessorFactoryTests.class.getResourceAsStream("/GeoLite2-City-Test.mmdb"), + geoipTmpDir.resolve("GeoLite2-City.mmdb") + ); databaseNodeService.updateDatabase("GeoLite2-City.mmdb", "md5", geoipTmpDir.resolve("GeoLite2-City.mmdb")); GeoIpProcessor.Factory factory = new GeoIpProcessor.Factory(databaseNodeService, clusterService); @@ -260,8 +270,13 @@ public void testBuildIllegalFieldOption() throws Exception { config1.put("field", "_field"); config1.put("properties", Collections.singletonList("invalid")); Exception e = expectThrows(ElasticsearchParseException.class, () -> factory.create(null, null, null, config1)); - assertThat(e.getMessage(), equalTo("[properties] illegal property value [invalid]. valid values are [IP, COUNTRY_ISO_CODE, " + - "COUNTRY_NAME, CONTINENT_NAME, REGION_ISO_CODE, REGION_NAME, CITY_NAME, TIMEZONE, LOCATION]")); + assertThat( + e.getMessage(), + equalTo( + "[properties] illegal property value [invalid]. valid values are [IP, COUNTRY_ISO_CODE, " + + "COUNTRY_NAME, CONTINENT_NAME, REGION_ISO_CODE, REGION_NAME, CITY_NAME, TIMEZONE, LOCATION]" + ) + ); Map config2 = new HashMap<>(); config2.put("field", "_field"); @@ -448,12 +463,18 @@ public void testDatabaseNotReadyYet() throws Exception { document.put("source_field", "89.160.20.128"); IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document); - GeoIpProcessor.DatabaseUnavailableProcessor processor = - (GeoIpProcessor.DatabaseUnavailableProcessor) factory.create(null, null, null, config); + GeoIpProcessor.DatabaseUnavailableProcessor processor = (GeoIpProcessor.DatabaseUnavailableProcessor) factory.create( + null, + null, + null, + config + ); processor.execute(ingestDocument); assertThat(ingestDocument.getSourceAndMetadata().get("geoip"), nullValue()); - assertThat(ingestDocument.getSourceAndMetadata().get("tags"), - equalTo(List.of("_geoip_database_unavailable_GeoLite2-City.mmdb"))); + assertThat( + ingestDocument.getSourceAndMetadata().get("tags"), + equalTo(List.of("_geoip_database_unavailable_GeoLite2-City.mmdb")) + ); } copyDatabaseFile(geoipTmpDir, "GeoLite2-City-Test.mmdb"); diff --git a/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/GeoIpProcessorTests.java b/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/GeoIpProcessorTests.java index 258fcb0a53bf4..a96589bd29423 100644 --- a/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/GeoIpProcessorTests.java +++ b/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/GeoIpProcessorTests.java @@ -36,8 +36,18 @@ public class GeoIpProcessorTests extends ESTestCase { public void testCity() throws Exception { - GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), null, "source_field", loader("/GeoLite2-City.mmdb"), - () -> true, "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, false, "filename"); + GeoIpProcessor processor = new GeoIpProcessor( + randomAlphaOfLength(10), + null, + "source_field", + loader("/GeoLite2-City.mmdb"), + () -> true, + "target_field", + EnumSet.allOf(GeoIpProcessor.Property.class), + false, + false, + "filename" + ); Map document = new HashMap<>(); document.put("source_field", "8.8.8.8"); @@ -60,18 +70,40 @@ public void testCity() throws Exception { } public void testNullValueWithIgnoreMissing() throws Exception { - GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), null, "source_field", loader("/GeoLite2-City.mmdb"), - () -> true, "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), true, false, "filename"); - IngestDocument originalIngestDocument = RandomDocumentPicks.randomIngestDocument(random(), - Collections.singletonMap("source_field", null)); + GeoIpProcessor processor = new GeoIpProcessor( + randomAlphaOfLength(10), + null, + "source_field", + loader("/GeoLite2-City.mmdb"), + () -> true, + "target_field", + EnumSet.allOf(GeoIpProcessor.Property.class), + true, + false, + "filename" + ); + IngestDocument originalIngestDocument = RandomDocumentPicks.randomIngestDocument( + random(), + Collections.singletonMap("source_field", null) + ); IngestDocument ingestDocument = new IngestDocument(originalIngestDocument); processor.execute(ingestDocument); assertIngestDocument(originalIngestDocument, ingestDocument); } public void testNonExistentWithIgnoreMissing() throws Exception { - GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), null, "source_field", loader("/GeoLite2-City.mmdb"), - () -> true, "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), true, false, "filename"); + GeoIpProcessor processor = new GeoIpProcessor( + randomAlphaOfLength(10), + null, + "source_field", + loader("/GeoLite2-City.mmdb"), + () -> true, + "target_field", + EnumSet.allOf(GeoIpProcessor.Property.class), + true, + false, + "filename" + ); IngestDocument originalIngestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.emptyMap()); IngestDocument ingestDocument = new IngestDocument(originalIngestDocument); processor.execute(ingestDocument); @@ -79,18 +111,40 @@ public void testNonExistentWithIgnoreMissing() throws Exception { } public void testNullWithoutIgnoreMissing() throws Exception { - GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), null, "source_field", loader("/GeoLite2-City.mmdb"), - () -> true, "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, false, "filename"); - IngestDocument originalIngestDocument = RandomDocumentPicks.randomIngestDocument(random(), - Collections.singletonMap("source_field", null)); + GeoIpProcessor processor = new GeoIpProcessor( + randomAlphaOfLength(10), + null, + "source_field", + loader("/GeoLite2-City.mmdb"), + () -> true, + "target_field", + EnumSet.allOf(GeoIpProcessor.Property.class), + false, + false, + "filename" + ); + IngestDocument originalIngestDocument = RandomDocumentPicks.randomIngestDocument( + random(), + Collections.singletonMap("source_field", null) + ); IngestDocument ingestDocument = new IngestDocument(originalIngestDocument); Exception exception = expectThrows(Exception.class, () -> processor.execute(ingestDocument)); assertThat(exception.getMessage(), equalTo("field [source_field] is null, cannot extract geoip information.")); } public void testNonExistentWithoutIgnoreMissing() throws Exception { - GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), null, "source_field", loader("/GeoLite2-City.mmdb"), - () -> true, "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, false, "filename"); + GeoIpProcessor processor = new GeoIpProcessor( + randomAlphaOfLength(10), + null, + "source_field", + loader("/GeoLite2-City.mmdb"), + () -> true, + "target_field", + EnumSet.allOf(GeoIpProcessor.Property.class), + false, + false, + "filename" + ); IngestDocument originalIngestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.emptyMap()); IngestDocument ingestDocument = new IngestDocument(originalIngestDocument); Exception exception = expectThrows(Exception.class, () -> processor.execute(ingestDocument)); @@ -98,8 +152,18 @@ public void testNonExistentWithoutIgnoreMissing() throws Exception { } public void testCity_withIpV6() throws Exception { - GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), null, "source_field", loader("/GeoLite2-City.mmdb"), - () -> true, "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, false, "filename"); + GeoIpProcessor processor = new GeoIpProcessor( + randomAlphaOfLength(10), + null, + "source_field", + loader("/GeoLite2-City.mmdb"), + () -> true, + "target_field", + EnumSet.allOf(GeoIpProcessor.Property.class), + false, + false, + "filename" + ); String address = "2602:306:33d3:8000::3257:9652"; Map document = new HashMap<>(); @@ -126,8 +190,18 @@ public void testCity_withIpV6() throws Exception { } public void testCityWithMissingLocation() throws Exception { - GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), null, "source_field", loader("/GeoLite2-City.mmdb"), - () -> true, "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, false, "filename"); + GeoIpProcessor processor = new GeoIpProcessor( + randomAlphaOfLength(10), + null, + "source_field", + loader("/GeoLite2-City.mmdb"), + () -> true, + "target_field", + EnumSet.allOf(GeoIpProcessor.Property.class), + false, + false, + "filename" + ); Map document = new HashMap<>(); document.put("source_field", "80.231.5.0"); @@ -142,8 +216,18 @@ public void testCityWithMissingLocation() throws Exception { } public void testCountry() throws Exception { - GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), null, "source_field", loader("/GeoLite2-Country.mmdb"), - () -> true, "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, false, "filename"); + GeoIpProcessor processor = new GeoIpProcessor( + randomAlphaOfLength(10), + null, + "source_field", + loader("/GeoLite2-Country.mmdb"), + () -> true, + "target_field", + EnumSet.allOf(GeoIpProcessor.Property.class), + false, + false, + "filename" + ); Map document = new HashMap<>(); document.put("source_field", "82.170.213.79"); @@ -161,8 +245,18 @@ public void testCountry() throws Exception { } public void testCountryWithMissingLocation() throws Exception { - GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), null, "source_field", loader("/GeoLite2-Country.mmdb"), - () -> true, "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, false, "filename"); + GeoIpProcessor processor = new GeoIpProcessor( + randomAlphaOfLength(10), + null, + "source_field", + loader("/GeoLite2-Country.mmdb"), + () -> true, + "target_field", + EnumSet.allOf(GeoIpProcessor.Property.class), + false, + false, + "filename" + ); Map document = new HashMap<>(); document.put("source_field", "80.231.5.0"); @@ -178,8 +272,18 @@ public void testCountryWithMissingLocation() throws Exception { public void testAsn() throws Exception { String ip = "82.171.64.0"; - GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), null, "source_field", loader("/GeoLite2-ASN.mmdb"), - () -> true, "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, false, "filename"); + GeoIpProcessor processor = new GeoIpProcessor( + randomAlphaOfLength(10), + null, + "source_field", + loader("/GeoLite2-ASN.mmdb"), + () -> true, + "target_field", + EnumSet.allOf(GeoIpProcessor.Property.class), + false, + false, + "filename" + ); Map document = new HashMap<>(); document.put("source_field", ip); @@ -197,8 +301,18 @@ public void testAsn() throws Exception { } public void testAddressIsNotInTheDatabase() throws Exception { - GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), null, "source_field", loader("/GeoLite2-City.mmdb"), - () -> true, "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, false, "filename"); + GeoIpProcessor processor = new GeoIpProcessor( + randomAlphaOfLength(10), + null, + "source_field", + loader("/GeoLite2-City.mmdb"), + () -> true, + "target_field", + EnumSet.allOf(GeoIpProcessor.Property.class), + false, + false, + "filename" + ); Map document = new HashMap<>(); document.put("source_field", "127.0.0.1"); @@ -211,8 +325,18 @@ public void testAddressIsNotInTheDatabase() throws Exception { * Don't silently do DNS lookups or anything trappy on bogus data */ public void testInvalid() throws Exception { - GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), null, "source_field", loader("/GeoLite2-City.mmdb"), - () -> true, "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, false, "filename"); + GeoIpProcessor processor = new GeoIpProcessor( + randomAlphaOfLength(10), + null, + "source_field", + loader("/GeoLite2-City.mmdb"), + () -> true, + "target_field", + EnumSet.allOf(GeoIpProcessor.Property.class), + false, + false, + "filename" + ); Map document = new HashMap<>(); document.put("source_field", "www.google.com"); @@ -222,8 +346,18 @@ public void testInvalid() throws Exception { } public void testListAllValid() throws Exception { - GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), null, "source_field", loader("/GeoLite2-City.mmdb"), - () -> true, "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, false, "filename"); + GeoIpProcessor processor = new GeoIpProcessor( + randomAlphaOfLength(10), + null, + "source_field", + loader("/GeoLite2-City.mmdb"), + () -> true, + "target_field", + EnumSet.allOf(GeoIpProcessor.Property.class), + false, + false, + "filename" + ); Map document = new HashMap<>(); document.put("source_field", Arrays.asList("8.8.8.8", "82.171.64.0")); @@ -242,8 +376,18 @@ public void testListAllValid() throws Exception { } public void testListPartiallyValid() throws Exception { - GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), null, "source_field", loader("/GeoLite2-City.mmdb"), - () -> true, "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, false, "filename"); + GeoIpProcessor processor = new GeoIpProcessor( + randomAlphaOfLength(10), + null, + "source_field", + loader("/GeoLite2-City.mmdb"), + () -> true, + "target_field", + EnumSet.allOf(GeoIpProcessor.Property.class), + false, + false, + "filename" + ); Map document = new HashMap<>(); document.put("source_field", Arrays.asList("8.8.8.8", "127.0.0.1")); @@ -262,8 +406,18 @@ public void testListPartiallyValid() throws Exception { } public void testListNoMatches() throws Exception { - GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), null, "source_field", loader("/GeoLite2-City.mmdb"), - () -> true, "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, false, "filename"); + GeoIpProcessor processor = new GeoIpProcessor( + randomAlphaOfLength(10), + null, + "source_field", + loader("/GeoLite2-City.mmdb"), + () -> true, + "target_field", + EnumSet.allOf(GeoIpProcessor.Property.class), + false, + false, + "filename" + ); Map document = new HashMap<>(); document.put("source_field", Arrays.asList("127.0.0.1", "127.0.0.1")); @@ -274,8 +428,18 @@ public void testListNoMatches() throws Exception { } public void testListFirstOnly() throws Exception { - GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), null, "source_field", loader("/GeoLite2-City.mmdb"), - () -> true, "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, true, "filename"); + GeoIpProcessor processor = new GeoIpProcessor( + randomAlphaOfLength(10), + null, + "source_field", + loader("/GeoLite2-City.mmdb"), + () -> true, + "target_field", + EnumSet.allOf(GeoIpProcessor.Property.class), + false, + true, + "filename" + ); Map document = new HashMap<>(); document.put("source_field", Arrays.asList("8.8.8.8", "127.0.0.1")); @@ -292,8 +456,18 @@ public void testListFirstOnly() throws Exception { } public void testListFirstOnlyNoMatches() throws Exception { - GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), null, "source_field", loader("/GeoLite2-City.mmdb"), - () -> true, "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, true, "filename"); + GeoIpProcessor processor = new GeoIpProcessor( + randomAlphaOfLength(10), + null, + "source_field", + loader("/GeoLite2-City.mmdb"), + () -> true, + "target_field", + EnumSet.allOf(GeoIpProcessor.Property.class), + false, + true, + "filename" + ); Map document = new HashMap<>(); document.put("source_field", Arrays.asList("127.0.0.1", "127.0.0.2")); @@ -304,8 +478,18 @@ public void testListFirstOnlyNoMatches() throws Exception { } public void testInvalidDatabase() throws Exception { - GeoIpProcessor processor = new GeoIpProcessor(randomAlphaOfLength(10), null, "source_field", loader("/GeoLite2-City.mmdb"), - () -> false, "target_field", EnumSet.allOf(GeoIpProcessor.Property.class), false, true, "filename"); + GeoIpProcessor processor = new GeoIpProcessor( + randomAlphaOfLength(10), + null, + "source_field", + loader("/GeoLite2-City.mmdb"), + () -> false, + "target_field", + EnumSet.allOf(GeoIpProcessor.Property.class), + false, + true, + "filename" + ); Map document = new HashMap<>(); document.put("source_field", Arrays.asList("127.0.0.1", "127.0.0.2")); @@ -318,8 +502,8 @@ public void testInvalidDatabase() throws Exception { private CheckedSupplier loader(final String path) { final Supplier databaseInputStreamSupplier = () -> GeoIpProcessor.class.getResourceAsStream(path); - final CheckedSupplier loader = - () -> new DatabaseReader.Builder(databaseInputStreamSupplier.get()).build(); + final CheckedSupplier loader = () -> new DatabaseReader.Builder(databaseInputStreamSupplier.get()) + .build(); final GeoIpCache cache = new GeoIpCache(1000); DatabaseReaderLazyLoader lazyLoader = new DatabaseReaderLazyLoader(cache, PathUtils.get(path), null, loader) { diff --git a/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/GeoIpTaskStateSerializationTests.java b/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/GeoIpTaskStateSerializationTests.java index 69b4a21c86890..bfbedb1dda837 100644 --- a/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/GeoIpTaskStateSerializationTests.java +++ b/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/GeoIpTaskStateSerializationTests.java @@ -9,8 +9,8 @@ package org.elasticsearch.ingest.geoip; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -30,8 +30,13 @@ protected GeoIpTaskState createTestInstance() { GeoIpTaskState state = GeoIpTaskState.EMPTY; int databaseCount = randomInt(20); for (int i = 0; i < databaseCount; i++) { - GeoIpTaskState.Metadata metadata = new GeoIpTaskState.Metadata(randomLong(), randomInt(), randomInt(), - randomAlphaOfLength(32), randomLong()); + GeoIpTaskState.Metadata metadata = new GeoIpTaskState.Metadata( + randomLong(), + randomInt(), + randomInt(), + randomAlphaOfLength(32), + randomLong() + ); state = state.put(randomAlphaOfLengthBetween(5, 10), metadata); } return state; diff --git a/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/TarInputStreamTests.java b/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/TarInputStreamTests.java index 7173df65c5efd..ec3487cb92b83 100644 --- a/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/TarInputStreamTests.java +++ b/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/TarInputStreamTests.java @@ -10,6 +10,7 @@ import com.carrotsearch.randomizedtesting.annotations.Name; import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; + import org.elasticsearch.test.ESTestCase; import java.io.IOException; @@ -31,8 +32,7 @@ public TarInputStreamTests(@Name("path") String path, @Name("entries") List parameters() throws Exception { - Object[][] entries = new Object[][]{ - createTest("tar1.tar", - new Entry("a.txt", "aaa\n", false)), - createTest("tar2.tar", - new Entry("a.txt", "aaa\n", false), - new Entry("b.txt", "bbbbbb\n", false)), - createTest("tar3.tar", + Object[][] entries = new Object[][] { + createTest("tar1.tar", new Entry("a.txt", "aaa\n", false)), + createTest("tar2.tar", new Entry("a.txt", "aaa\n", false), new Entry("b.txt", "bbbbbb\n", false)), + createTest( + "tar3.tar", new Entry("c.txt", Stream.generate(() -> "-").limit(512).collect(Collectors.joining()), false), - new Entry("b.txt", "bbbbbb\n", false)), - createTest("tar4.tar", + new Entry("b.txt", "bbbbbb\n", false) + ), + createTest( + "tar4.tar", new Entry("./", null, true), new Entry("./b.txt", "bbb\n", false), - new Entry("./a.txt", "aaa\n", false)) - }; + new Entry("./a.txt", "aaa\n", false) + ) }; return Arrays.asList(entries); } private static Object[] createTest(String name, Entry... entries) { - return new Object[]{name, Arrays.asList(entries)}; + return new Object[] { name, Arrays.asList(entries) }; } private static class Entry { diff --git a/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/stats/GeoIpDownloaderStatsActionNodeResponseSerializingTests.java b/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/stats/GeoIpDownloaderStatsActionNodeResponseSerializingTests.java index cb914eee8f893..9e49d33089eab 100644 --- a/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/stats/GeoIpDownloaderStatsActionNodeResponseSerializingTests.java +++ b/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/stats/GeoIpDownloaderStatsActionNodeResponseSerializingTests.java @@ -15,8 +15,8 @@ import java.util.Set; -public class GeoIpDownloaderStatsActionNodeResponseSerializingTests extends - AbstractWireSerializingTestCase { +public class GeoIpDownloaderStatsActionNodeResponseSerializingTests extends AbstractWireSerializingTestCase< + GeoIpDownloaderStatsAction.NodeResponse> { @Override protected Writeable.Reader instanceReader() { @@ -33,7 +33,12 @@ static GeoIpDownloaderStatsAction.NodeResponse createRandomInstance() { Set databases = Set.copyOf(randomList(10, () -> randomAlphaOfLengthBetween(5, 10))); Set files = Set.copyOf(randomList(10, () -> randomAlphaOfLengthBetween(5, 10))); Set configDatabases = Set.copyOf(randomList(10, () -> randomAlphaOfLengthBetween(5, 10))); - return new GeoIpDownloaderStatsAction.NodeResponse(node, GeoIpDownloaderStatsSerializingTests.createRandomInstance(), databases, - files, configDatabases); + return new GeoIpDownloaderStatsAction.NodeResponse( + node, + GeoIpDownloaderStatsSerializingTests.createRandomInstance(), + databases, + files, + configDatabases + ); } } diff --git a/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/stats/GeoIpDownloaderStatsActionResponseSerializingTests.java b/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/stats/GeoIpDownloaderStatsActionResponseSerializingTests.java index b2066682c4d4b..756b012b996f8 100644 --- a/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/stats/GeoIpDownloaderStatsActionResponseSerializingTests.java +++ b/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/stats/GeoIpDownloaderStatsActionResponseSerializingTests.java @@ -15,8 +15,8 @@ import java.util.Collections; import java.util.List; -public class GeoIpDownloaderStatsActionResponseSerializingTests extends - AbstractWireSerializingTestCase { +public class GeoIpDownloaderStatsActionResponseSerializingTests extends AbstractWireSerializingTestCase< + GeoIpDownloaderStatsAction.Response> { @Override protected Writeable.Reader instanceReader() { @@ -25,8 +25,10 @@ protected Writeable.Reader instanceReader() @Override protected GeoIpDownloaderStatsAction.Response createTestInstance() { - List nodeResponses = randomList(10, - GeoIpDownloaderStatsActionNodeResponseSerializingTests::createRandomInstance); + List nodeResponses = randomList( + 10, + GeoIpDownloaderStatsActionNodeResponseSerializingTests::createRandomInstance + ); return new GeoIpDownloaderStatsAction.Response(ClusterName.DEFAULT, nodeResponses, Collections.emptyList()); } } diff --git a/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/stats/GeoIpDownloaderStatsSerializingTests.java b/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/stats/GeoIpDownloaderStatsSerializingTests.java index 7ff2674bff3f5..c290b086dd139 100644 --- a/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/stats/GeoIpDownloaderStatsSerializingTests.java +++ b/modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/stats/GeoIpDownloaderStatsSerializingTests.java @@ -9,8 +9,8 @@ package org.elasticsearch.ingest.geoip.stats; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/modules/ingest-user-agent/src/main/java/org/elasticsearch/ingest/useragent/DeviceTypeParser.java b/modules/ingest-user-agent/src/main/java/org/elasticsearch/ingest/useragent/DeviceTypeParser.java index 97b0f4160577c..6ca60cb709a16 100644 --- a/modules/ingest-user-agent/src/main/java/org/elasticsearch/ingest/useragent/DeviceTypeParser.java +++ b/modules/ingest-user-agent/src/main/java/org/elasticsearch/ingest/useragent/DeviceTypeParser.java @@ -24,8 +24,8 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; -import static org.elasticsearch.ingest.useragent.UserAgentParser.readParserConfigurations; import static org.elasticsearch.ingest.useragent.UserAgentParser.VersionedName; +import static org.elasticsearch.ingest.useragent.UserAgentParser.readParserConfigurations; public class DeviceTypeParser { @@ -41,8 +41,8 @@ public class DeviceTypeParser { public void init(InputStream regexStream) throws IOException { // EMPTY is safe here because we don't use namedObject - XContentParser yamlParser = XContentFactory.xContent(XContentType.YAML).createParser(NamedXContentRegistry.EMPTY, - LoggingDeprecationHandler.INSTANCE, regexStream); + XContentParser yamlParser = XContentFactory.xContent(XContentType.YAML) + .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, regexStream); XContentParser.Token token = yamlParser.nextToken(); @@ -55,8 +55,7 @@ public void init(InputStream regexStream) throws IOException { List> parserConfigurations = readParserConfigurations(yamlParser); ArrayList subPatterns = new ArrayList<>(); for (Map map : parserConfigurations) { - subPatterns.add(new DeviceTypeSubPattern(Pattern.compile((map.get("regex"))), - map.get("replacement"))); + subPatterns.add(new DeviceTypeSubPattern(Pattern.compile((map.get("regex"))), map.get("replacement"))); } deviceTypePatterns.put(currentName, subPatterns); } @@ -116,7 +115,6 @@ public String findDeviceType(VersionedName userAgent, VersionedName os, Versione } } - if (extractedDeviceTypes.contains(robot)) { return robot; } diff --git a/modules/ingest-user-agent/src/main/java/org/elasticsearch/ingest/useragent/IngestUserAgentPlugin.java b/modules/ingest-user-agent/src/main/java/org/elasticsearch/ingest/useragent/IngestUserAgentPlugin.java index dfaed02a2323e..7bac845da02bb 100644 --- a/modules/ingest-user-agent/src/main/java/org/elasticsearch/ingest/useragent/IngestUserAgentPlugin.java +++ b/modules/ingest-user-agent/src/main/java/org/elasticsearch/ingest/useragent/IngestUserAgentPlugin.java @@ -27,8 +27,12 @@ public class IngestUserAgentPlugin extends Plugin implements IngestPlugin { - private final Setting CACHE_SIZE_SETTING = Setting.longSetting("ingest.user_agent.cache_size", 1000, 0, - Setting.Property.NodeScope); + private final Setting CACHE_SIZE_SETTING = Setting.longSetting( + "ingest.user_agent.cache_size", + 1000, + 0, + Setting.Property.NodeScope + ); static final String DEFAULT_PARSER_NAME = "_default_"; @@ -38,7 +42,8 @@ public Map getProcessors(Processor.Parameters paramet if (Files.exists(userAgentConfigDirectory) == false && Files.isDirectory(userAgentConfigDirectory)) { throw new IllegalStateException( - "the user agent directory [" + userAgentConfigDirectory + "] containing the regex file doesn't exist"); + "the user agent directory [" + userAgentConfigDirectory + "] containing the regex file doesn't exist" + ); } long cacheSize = CACHE_SIZE_SETTING.get(parameters.env.settings()); @@ -54,21 +59,31 @@ public Map getProcessors(Processor.Parameters paramet static Map createUserAgentParsers(Path userAgentConfigDirectory, UserAgentCache cache) throws IOException { Map userAgentParsers = new HashMap<>(); - UserAgentParser defaultParser = new UserAgentParser(DEFAULT_PARSER_NAME, + UserAgentParser defaultParser = new UserAgentParser( + DEFAULT_PARSER_NAME, IngestUserAgentPlugin.class.getResourceAsStream("/regexes.yml"), - IngestUserAgentPlugin.class.getResourceAsStream("/device_type_regexes.yml"), cache); + IngestUserAgentPlugin.class.getResourceAsStream("/device_type_regexes.yml"), + cache + ); userAgentParsers.put(DEFAULT_PARSER_NAME, defaultParser); if (Files.exists(userAgentConfigDirectory) && Files.isDirectory(userAgentConfigDirectory)) { PathMatcher pathMatcher = userAgentConfigDirectory.getFileSystem().getPathMatcher("glob:**.yml"); - try (Stream regexFiles = Files.find(userAgentConfigDirectory, 1, - (path, attr) -> attr.isRegularFile() && pathMatcher.matches(path))) { + try ( + Stream regexFiles = Files.find( + userAgentConfigDirectory, + 1, + (path, attr) -> attr.isRegularFile() && pathMatcher.matches(path) + ) + ) { Iterable iterable = regexFiles::iterator; for (Path path : iterable) { String parserName = path.getFileName().toString(); - try (InputStream regexStream = Files.newInputStream(path, StandardOpenOption.READ); - InputStream deviceTypeRegexStream = IngestUserAgentPlugin.class.getResourceAsStream("/device_type_regexes.yml")) { + try ( + InputStream regexStream = Files.newInputStream(path, StandardOpenOption.READ); + InputStream deviceTypeRegexStream = IngestUserAgentPlugin.class.getResourceAsStream("/device_type_regexes.yml") + ) { userAgentParsers.put(parserName, new UserAgentParser(parserName, regexStream, deviceTypeRegexStream, cache)); } } diff --git a/modules/ingest-user-agent/src/main/java/org/elasticsearch/ingest/useragent/UserAgentCache.java b/modules/ingest-user-agent/src/main/java/org/elasticsearch/ingest/useragent/UserAgentCache.java index 9fc4ddb2bb16b..800becbacd47e 100644 --- a/modules/ingest-user-agent/src/main/java/org/elasticsearch/ingest/useragent/UserAgentCache.java +++ b/modules/ingest-user-agent/src/main/java/org/elasticsearch/ingest/useragent/UserAgentCache.java @@ -40,8 +40,8 @@ private static final class CompositeCacheKey { @Override public boolean equals(Object obj) { - if(obj != null && obj instanceof CompositeCacheKey) { - CompositeCacheKey s = (CompositeCacheKey)obj; + if (obj != null && obj instanceof CompositeCacheKey) { + CompositeCacheKey s = (CompositeCacheKey) obj; return parserName.equals(s.parserName) && userAgent.equals(s.userAgent); } return false; diff --git a/modules/ingest-user-agent/src/main/java/org/elasticsearch/ingest/useragent/UserAgentParser.java b/modules/ingest-user-agent/src/main/java/org/elasticsearch/ingest/useragent/UserAgentParser.java index 9658458d57420..861a46ea819d9 100644 --- a/modules/ingest-user-agent/src/main/java/org/elasticsearch/ingest/useragent/UserAgentParser.java +++ b/modules/ingest-user-agent/src/main/java/org/elasticsearch/ingest/useragent/UserAgentParser.java @@ -14,6 +14,7 @@ import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; + import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; @@ -48,8 +49,8 @@ final class UserAgentParser { private void init(InputStream regexStream) throws IOException { // EMPTY is safe here because we don't use namedObject - XContentParser yamlParser = XContentFactory.xContent(XContentType.YAML).createParser(NamedXContentRegistry.EMPTY, - LoggingDeprecationHandler.INSTANCE, regexStream); + XContentParser yamlParser = XContentFactory.xContent(XContentType.YAML) + .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, regexStream); XContentParser.Token token = yamlParser.nextToken(); @@ -61,26 +62,46 @@ private void init(InputStream regexStream) throws IOException { List> parserConfigurations = readParserConfigurations(yamlParser); for (Map map : parserConfigurations) { - uaPatterns.add(new UserAgentSubpattern(compilePattern(map.get("regex"), map.get("regex_flag")), - map.get("family_replacement"), map.get("v1_replacement"), map.get("v2_replacement"), - map.get("v3_replacement"), map.get("v4_replacement"))); + uaPatterns.add( + new UserAgentSubpattern( + compilePattern(map.get("regex"), map.get("regex_flag")), + map.get("family_replacement"), + map.get("v1_replacement"), + map.get("v2_replacement"), + map.get("v3_replacement"), + map.get("v4_replacement") + ) + ); } - } - else if (token == XContentParser.Token.FIELD_NAME && yamlParser.currentName().equals("os_parsers")) { + } else if (token == XContentParser.Token.FIELD_NAME && yamlParser.currentName().equals("os_parsers")) { List> parserConfigurations = readParserConfigurations(yamlParser); for (Map map : parserConfigurations) { - osPatterns.add(new UserAgentSubpattern(compilePattern(map.get("regex"), map.get("regex_flag")), - map.get("os_replacement"), map.get("os_v1_replacement"), map.get("os_v2_replacement"), - map.get("os_v3_replacement"), map.get("os_v4_replacement"))); + osPatterns.add( + new UserAgentSubpattern( + compilePattern(map.get("regex"), map.get("regex_flag")), + map.get("os_replacement"), + map.get("os_v1_replacement"), + map.get("os_v2_replacement"), + map.get("os_v3_replacement"), + map.get("os_v4_replacement") + ) + ); } - } - else if (token == XContentParser.Token.FIELD_NAME && yamlParser.currentName().equals("device_parsers")) { + } else if (token == XContentParser.Token.FIELD_NAME && yamlParser.currentName().equals("device_parsers")) { List> parserConfigurations = readParserConfigurations(yamlParser); for (Map map : parserConfigurations) { - devicePatterns.add(new UserAgentSubpattern(compilePattern(map.get("regex"), map.get("regex_flag")), - map.get("device_replacement"), null, null, null, null)); + devicePatterns.add( + new UserAgentSubpattern( + compilePattern(map.get("regex"), map.get("regex_flag")), + map.get("device_replacement"), + null, + null, + null, + null + ) + ); } } } @@ -218,61 +239,67 @@ static final class UserAgentSubpattern { private final Pattern pattern; private final String nameReplacement, v1Replacement, v2Replacement, v3Replacement, v4Replacement; - UserAgentSubpattern(Pattern pattern, String nameReplacement, - String v1Replacement, String v2Replacement, String v3Replacement, String v4Replacement) { - this.pattern = pattern; - this.nameReplacement = nameReplacement; - this.v1Replacement = v1Replacement; - this.v2Replacement = v2Replacement; - this.v3Replacement = v3Replacement; - this.v4Replacement = v4Replacement; + UserAgentSubpattern( + Pattern pattern, + String nameReplacement, + String v1Replacement, + String v2Replacement, + String v3Replacement, + String v4Replacement + ) { + this.pattern = pattern; + this.nameReplacement = nameReplacement; + this.v1Replacement = v1Replacement; + this.v2Replacement = v2Replacement; + this.v3Replacement = v3Replacement; + this.v4Replacement = v4Replacement; } public VersionedName match(String agentString) { - String name = null, major = null, minor = null, patch = null, build = null; - Matcher matcher = pattern.matcher(agentString); + String name = null, major = null, minor = null, patch = null, build = null; + Matcher matcher = pattern.matcher(agentString); + + if (matcher.find() == false) { + return null; + } - if (matcher.find() == false) { - return null; - } + int groupCount = matcher.groupCount(); - int groupCount = matcher.groupCount(); + if (nameReplacement != null) { + if (nameReplacement.contains("$1") && groupCount >= 1 && matcher.group(1) != null) { + name = nameReplacement.replaceFirst("\\$1", Matcher.quoteReplacement(matcher.group(1))); + } else { + name = nameReplacement; + } + } else if (groupCount >= 1) { + name = matcher.group(1); + } - if (nameReplacement != null) { - if (nameReplacement.contains("$1") && groupCount >= 1 && matcher.group(1) != null) { - name = nameReplacement.replaceFirst("\\$1", Matcher.quoteReplacement(matcher.group(1))); - } else { - name = nameReplacement; + if (v1Replacement != null) { + major = v1Replacement; + } else if (groupCount >= 2) { + major = matcher.group(2); } - } else if (groupCount >= 1) { - name = matcher.group(1); - } - - if (v1Replacement != null) { - major = v1Replacement; - } else if (groupCount >= 2) { - major = matcher.group(2); - } - - if (v2Replacement != null) { - minor = v2Replacement; - } else if (groupCount >= 3) { - minor = matcher.group(3); - } - - if (v3Replacement != null) { - patch = v3Replacement; - } else if (groupCount >= 4) { - patch = matcher.group(4); - } - - if (v4Replacement != null) { - build = v4Replacement; - } else if (groupCount >= 5) { - build = matcher.group(5); - } - - return name == null ? null : new VersionedName(name, major, minor, patch, build); + + if (v2Replacement != null) { + minor = v2Replacement; + } else if (groupCount >= 3) { + minor = matcher.group(3); + } + + if (v3Replacement != null) { + patch = v3Replacement; + } else if (groupCount >= 4) { + patch = matcher.group(4); + } + + if (v4Replacement != null) { + build = v4Replacement; + } else if (groupCount >= 5) { + build = matcher.group(5); + } + + return name == null ? null : new VersionedName(name, major, minor, patch, build); } - } + } } diff --git a/modules/ingest-user-agent/src/main/java/org/elasticsearch/ingest/useragent/UserAgentProcessor.java b/modules/ingest-user-agent/src/main/java/org/elasticsearch/ingest/useragent/UserAgentProcessor.java index a973d3d3e14e0..e8391f93a27d0 100644 --- a/modules/ingest-user-agent/src/main/java/org/elasticsearch/ingest/useragent/UserAgentProcessor.java +++ b/modules/ingest-user-agent/src/main/java/org/elasticsearch/ingest/useragent/UserAgentProcessor.java @@ -41,8 +41,16 @@ public class UserAgentProcessor extends AbstractProcessor { private final boolean extractDeviceType; private final boolean ignoreMissing; - public UserAgentProcessor(String tag, String description, String field, String targetField, UserAgentParser parser, - Set properties, boolean extractDeviceType, boolean ignoreMissing) { + public UserAgentProcessor( + String tag, + String description, + String field, + String targetField, + UserAgentParser parser, + Set properties, + boolean extractDeviceType, + boolean ignoreMissing + ) { super(tag, description); this.field = field; this.targetField = targetField; @@ -183,8 +191,12 @@ public Factory(Map userAgentParsers) { } @Override - public UserAgentProcessor create(Map factories, String processorTag, - String description, Map config) throws Exception { + public UserAgentProcessor create( + Map factories, + String processorTag, + String description, + Map config + ) throws Exception { String field = readStringProperty(TYPE, processorTag, config, "field"); String targetField = readStringProperty(TYPE, processorTag, config, "target_field", "user_agent"); String regexFilename = readStringProperty(TYPE, processorTag, config, "regex_file", IngestUserAgentPlugin.DEFAULT_PARSER_NAME); @@ -193,14 +205,21 @@ public UserAgentProcessor create(Map factories, Strin boolean ignoreMissing = readBooleanProperty(TYPE, processorTag, config, "ignore_missing", false); Object ecsValue = config.remove("ecs"); if (ecsValue != null) { - deprecationLogger.critical(DeprecationCategory.SETTINGS, "ingest_useragent_ecs_settings", - "setting [ecs] is deprecated as ECS format is the default and only option"); + deprecationLogger.critical( + DeprecationCategory.SETTINGS, + "ingest_useragent_ecs_settings", + "setting [ecs] is deprecated as ECS format is the default and only option" + ); } UserAgentParser parser = userAgentParsers.get(regexFilename); if (parser == null) { - throw newConfigurationException(TYPE, processorTag, - "regex_file", "regex file [" + regexFilename + "] doesn't exist (has to exist at node startup)"); + throw newConfigurationException( + TYPE, + processorTag, + "regex_file", + "regex file [" + regexFilename + "] doesn't exist (has to exist at node startup)" + ); } final Set properties; @@ -217,8 +236,16 @@ public UserAgentProcessor create(Map factories, Strin properties = EnumSet.allOf(Property.class); } - return new - UserAgentProcessor(processorTag, description, field, targetField, parser, properties, extractDeviceType, ignoreMissing); + return new UserAgentProcessor( + processorTag, + description, + field, + targetField, + parser, + properties, + extractDeviceType, + ignoreMissing + ); } } @@ -234,8 +261,12 @@ public static Property parseProperty(String propertyName) { try { return valueOf(propertyName.toUpperCase(Locale.ROOT)); } catch (IllegalArgumentException e) { - throw new IllegalArgumentException("illegal property value [" + propertyName + "]. valid values are " + - Arrays.toString(EnumSet.allOf(Property.class).toArray())); + throw new IllegalArgumentException( + "illegal property value [" + + propertyName + + "]. valid values are " + + Arrays.toString(EnumSet.allOf(Property.class).toArray()) + ); } } } diff --git a/modules/ingest-user-agent/src/test/java/org/elasticsearch/ingest/useragent/DeviceTypeParserTests.java b/modules/ingest-user-agent/src/test/java/org/elasticsearch/ingest/useragent/DeviceTypeParserTests.java index 115521dd4a912..f3bca428ac0fd 100644 --- a/modules/ingest-user-agent/src/test/java/org/elasticsearch/ingest/useragent/DeviceTypeParserTests.java +++ b/modules/ingest-user-agent/src/test/java/org/elasticsearch/ingest/useragent/DeviceTypeParserTests.java @@ -7,13 +7,13 @@ */ package org.elasticsearch.ingest.useragent; + import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; - import org.junit.BeforeClass; import java.io.IOException; @@ -23,10 +23,7 @@ import java.util.List; import java.util.Map; - import static org.elasticsearch.ingest.useragent.UserAgentParser.VersionedName; - - import static org.elasticsearch.ingest.useragent.UserAgentParser.readParserConfigurations; import static org.hamcrest.Matchers.is; @@ -35,8 +32,8 @@ public class DeviceTypeParserTests extends ESTestCase { private static DeviceTypeParser deviceTypeParser; private ArrayList> readTestDevices(InputStream regexStream, String keyName) throws IOException { - XContentParser yamlParser = XContentFactory.xContent(XContentType.YAML).createParser(NamedXContentRegistry.EMPTY, - LoggingDeprecationHandler.INSTANCE, regexStream); + XContentParser yamlParser = XContentFactory.xContent(XContentType.YAML) + .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, regexStream); XContentParser.Token token = yamlParser.nextToken(); @@ -67,7 +64,7 @@ private ArrayList> readTestDevices(InputStream regexStre return testDevices; } - private static VersionedName getVersionName(String name){ + private static VersionedName getVersionName(String name) { return new VersionedName(name, null, null, null, null); } @@ -135,7 +132,11 @@ public void testWindowDesktop() throws Exception { public void testRobotAgentString() throws Exception { String deviceType = deviceTypeParser.findDeviceType( - "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:63.0.247) Gecko/20100101 Firefox/63.0.247 Site24x7", null, null, null); + "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:63.0.247) Gecko/20100101 Firefox/63.0.247 Site24x7", + null, + null, + null + ); assertThat(deviceType, is("Robot")); } diff --git a/modules/ingest-user-agent/src/test/java/org/elasticsearch/ingest/useragent/UserAgentProcessorFactoryTests.java b/modules/ingest-user-agent/src/test/java/org/elasticsearch/ingest/useragent/UserAgentProcessorFactoryTests.java index 9b51c108e777d..b81fa3e37b00e 100644 --- a/modules/ingest-user-agent/src/test/java/org/elasticsearch/ingest/useragent/UserAgentProcessorFactoryTests.java +++ b/modules/ingest-user-agent/src/test/java/org/elasticsearch/ingest/useragent/UserAgentProcessorFactoryTests.java @@ -45,9 +45,12 @@ public static void createUserAgentParsers() throws IOException { Files.createDirectories(userAgentConfigDir); // Copy file, leaving out the device parsers at the end - try (BufferedReader reader = new BufferedReader( - new InputStreamReader(UserAgentProcessor.class.getResourceAsStream("/regexes.yml"), StandardCharsets.UTF_8)); - BufferedWriter writer = Files.newBufferedWriter(userAgentConfigDir.resolve(regexWithoutDevicesFilename));) { + try ( + BufferedReader reader = new BufferedReader( + new InputStreamReader(UserAgentProcessor.class.getResourceAsStream("/regexes.yml"), StandardCharsets.UTF_8) + ); + BufferedWriter writer = Files.newBufferedWriter(userAgentConfigDir.resolve(regexWithoutDevicesFilename)); + ) { String line; while ((line = reader.readLine()) != null) { if (line.startsWith("device_parsers:")) { @@ -181,8 +184,10 @@ public void testInvalidProperty() throws Exception { config.put("properties", Collections.singletonList("invalid")); ElasticsearchParseException e = expectThrows(ElasticsearchParseException.class, () -> factory.create(null, null, null, config)); - assertThat(e.getMessage(), equalTo("[properties] illegal property value [invalid]. valid values are [NAME, OS, DEVICE, " + - "ORIGINAL, VERSION]")); + assertThat( + e.getMessage(), + equalTo("[properties] illegal property value [invalid]. valid values are [NAME, OS, DEVICE, " + "ORIGINAL, VERSION]") + ); } public void testInvalidPropertiesType() throws Exception { diff --git a/modules/ingest-user-agent/src/test/java/org/elasticsearch/ingest/useragent/UserAgentProcessorTests.java b/modules/ingest-user-agent/src/test/java/org/elasticsearch/ingest/useragent/UserAgentProcessorTests.java index f487e4ae8fc3c..52896f65a753d 100644 --- a/modules/ingest-user-agent/src/test/java/org/elasticsearch/ingest/useragent/UserAgentProcessorTests.java +++ b/modules/ingest-user-agent/src/test/java/org/elasticsearch/ingest/useragent/UserAgentProcessorTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.ingest.useragent; -import org.elasticsearch.ingest.RandomDocumentPicks; import org.elasticsearch.ingest.IngestDocument; +import org.elasticsearch.ingest.RandomDocumentPicks; import org.elasticsearch.test.ESTestCase; import org.junit.BeforeClass; @@ -39,23 +39,49 @@ public static void setupProcessor() throws IOException { UserAgentParser parser = new UserAgentParser(randomAlphaOfLength(10), regexStream, deviceTypeRegexStream, new UserAgentCache(1000)); - processor = new UserAgentProcessor(randomAlphaOfLength(10), null, "source_field", "target_field", parser, - EnumSet.allOf(UserAgentProcessor.Property.class), true, false); + processor = new UserAgentProcessor( + randomAlphaOfLength(10), + null, + "source_field", + "target_field", + parser, + EnumSet.allOf(UserAgentProcessor.Property.class), + true, + false + ); } public void testNullValueWithIgnoreMissing() throws Exception { - UserAgentProcessor processor = new UserAgentProcessor(randomAlphaOfLength(10), null, "source_field", "target_field", null, - EnumSet.allOf(UserAgentProcessor.Property.class), false, true); - IngestDocument originalIngestDocument = RandomDocumentPicks.randomIngestDocument(random(), - Collections.singletonMap("source_field", null)); + UserAgentProcessor processor = new UserAgentProcessor( + randomAlphaOfLength(10), + null, + "source_field", + "target_field", + null, + EnumSet.allOf(UserAgentProcessor.Property.class), + false, + true + ); + IngestDocument originalIngestDocument = RandomDocumentPicks.randomIngestDocument( + random(), + Collections.singletonMap("source_field", null) + ); IngestDocument ingestDocument = new IngestDocument(originalIngestDocument); processor.execute(ingestDocument); assertIngestDocument(originalIngestDocument, ingestDocument); } public void testNonExistentWithIgnoreMissing() throws Exception { - UserAgentProcessor processor = new UserAgentProcessor(randomAlphaOfLength(10), null, "source_field", "target_field", null, - EnumSet.allOf(UserAgentProcessor.Property.class), false, true); + UserAgentProcessor processor = new UserAgentProcessor( + randomAlphaOfLength(10), + null, + "source_field", + "target_field", + null, + EnumSet.allOf(UserAgentProcessor.Property.class), + false, + true + ); IngestDocument originalIngestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.emptyMap()); IngestDocument ingestDocument = new IngestDocument(originalIngestDocument); processor.execute(ingestDocument); @@ -63,18 +89,36 @@ public void testNonExistentWithIgnoreMissing() throws Exception { } public void testNullWithoutIgnoreMissing() throws Exception { - UserAgentProcessor processor = new UserAgentProcessor(randomAlphaOfLength(10), null, "source_field", "target_field", null, - EnumSet.allOf(UserAgentProcessor.Property.class), false, false); - IngestDocument originalIngestDocument = RandomDocumentPicks.randomIngestDocument(random(), - Collections.singletonMap("source_field", null)); + UserAgentProcessor processor = new UserAgentProcessor( + randomAlphaOfLength(10), + null, + "source_field", + "target_field", + null, + EnumSet.allOf(UserAgentProcessor.Property.class), + false, + false + ); + IngestDocument originalIngestDocument = RandomDocumentPicks.randomIngestDocument( + random(), + Collections.singletonMap("source_field", null) + ); IngestDocument ingestDocument = new IngestDocument(originalIngestDocument); Exception exception = expectThrows(Exception.class, () -> processor.execute(ingestDocument)); assertThat(exception.getMessage(), equalTo("field [source_field] is null, cannot parse user-agent.")); } public void testNonExistentWithoutIgnoreMissing() throws Exception { - UserAgentProcessor processor = new UserAgentProcessor(randomAlphaOfLength(10), null, "source_field", "target_field", null, - EnumSet.allOf(UserAgentProcessor.Property.class), false, false); + UserAgentProcessor processor = new UserAgentProcessor( + randomAlphaOfLength(10), + null, + "source_field", + "target_field", + null, + EnumSet.allOf(UserAgentProcessor.Property.class), + false, + false + ); IngestDocument originalIngestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.emptyMap()); IngestDocument ingestDocument = new IngestDocument(originalIngestDocument); Exception exception = expectThrows(Exception.class, () -> processor.execute(ingestDocument)); @@ -84,8 +128,10 @@ public void testNonExistentWithoutIgnoreMissing() throws Exception { @SuppressWarnings("unchecked") public void testCommonBrowser() throws Exception { Map document = new HashMap<>(); - document.put("source_field", - "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.149 Safari/537.36"); + document.put( + "source_field", + "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.149 Safari/537.36" + ); IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document); processor.execute(ingestDocument); @@ -111,8 +157,10 @@ public void testCommonBrowser() throws Exception { @SuppressWarnings("unchecked") public void testWindowsOS() throws Exception { Map document = new HashMap<>(); - document.put("source_field", - "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36"); + document.put( + "source_field", + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36" + ); IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document); processor.execute(ingestDocument); @@ -138,9 +186,11 @@ public void testWindowsOS() throws Exception { @SuppressWarnings("unchecked") public void testUncommonDevice() throws Exception { Map document = new HashMap<>(); - document.put("source_field", - "Mozilla/5.0 (Linux; U; Android 3.0; en-us; Xoom Build/HRI39) AppleWebKit/525.10+ " - + "(KHTML, like Gecko) Version/3.0.4 Mobile Safari/523.12.2"); + document.put( + "source_field", + "Mozilla/5.0 (Linux; U; Android 3.0; en-us; Xoom Build/HRI39) AppleWebKit/525.10+ " + + "(KHTML, like Gecko) Version/3.0.4 Mobile Safari/523.12.2" + ); IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document); processor.execute(ingestDocument); @@ -167,8 +217,7 @@ public void testUncommonDevice() throws Exception { @SuppressWarnings("unchecked") public void testSpider() throws Exception { Map document = new HashMap<>(); - document.put("source_field", - "Mozilla/5.0 (compatible; EasouSpider; +http://www.easou.com/search/spider.html)"); + document.put("source_field", "Mozilla/5.0 (compatible; EasouSpider; +http://www.easou.com/search/spider.html)"); IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document); processor.execute(ingestDocument); @@ -191,9 +240,11 @@ public void testSpider() throws Exception { @SuppressWarnings("unchecked") public void testTablet() throws Exception { Map document = new HashMap<>(); - document.put("source_field", - "Mozilla/5.0 (iPad; CPU OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) " + - "Version/12.1 Mobile/15E148 Safari/604.1"); + document.put( + "source_field", + "Mozilla/5.0 (iPad; CPU OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) " + + "Version/12.1 Mobile/15E148 Safari/604.1" + ); IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document); processor.execute(ingestDocument); @@ -221,8 +272,7 @@ public void testTablet() throws Exception { @SuppressWarnings("unchecked") public void testUnknown() throws Exception { Map document = new HashMap<>(); - document.put("source_field", - "Something I made up v42.0.1"); + document.put("source_field", "Something I made up v42.0.1"); IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document); processor.execute(ingestDocument); @@ -247,15 +297,22 @@ public void testUnknown() throws Exception { @SuppressWarnings("unchecked") public void testExtractDeviceTypeDisabled() { Map document = new HashMap<>(); - document.put("source_field", - "Something I made up v42.0.1"); + document.put("source_field", "Something I made up v42.0.1"); IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document); InputStream regexStream = UserAgentProcessor.class.getResourceAsStream("/regexes.yml"); InputStream deviceTypeRegexStream = UserAgentProcessor.class.getResourceAsStream("/device_type_regexes.yml"); UserAgentParser parser = new UserAgentParser(randomAlphaOfLength(10), regexStream, deviceTypeRegexStream, new UserAgentCache(1000)); - UserAgentProcessor processor = new UserAgentProcessor(randomAlphaOfLength(10), null, "source_field", "target_field", parser, - EnumSet.allOf(UserAgentProcessor.Property.class), false, false); + UserAgentProcessor processor = new UserAgentProcessor( + randomAlphaOfLength(10), + null, + "source_field", + "target_field", + parser, + EnumSet.allOf(UserAgentProcessor.Property.class), + false, + false + ); processor.execute(ingestDocument); Map data = ingestDocument.getSourceAndMetadata(); diff --git a/modules/lang-expression/src/internalClusterTest/java/org/elasticsearch/script/expression/MoreExpressionIT.java b/modules/lang-expression/src/internalClusterTest/java/org/elasticsearch/script/expression/MoreExpressionIT.java index 194555c589310..232f3a3960943 100644 --- a/modules/lang-expression/src/internalClusterTest/java/org/elasticsearch/script/expression/MoreExpressionIT.java +++ b/modules/lang-expression/src/internalClusterTest/java/org/elasticsearch/script/expression/MoreExpressionIT.java @@ -14,8 +14,6 @@ import org.elasticsearch.action.search.SearchType; import org.elasticsearch.action.update.UpdateRequestBuilder; import org.elasticsearch.common.lucene.search.function.CombineFunction; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders; import org.elasticsearch.index.query.functionscore.ScriptScoreFunctionBuilder; @@ -31,6 +29,8 @@ import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.test.ESIntegTestCase; import org.elasticsearch.test.hamcrest.ElasticsearchAssertions; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; import java.util.Collection; import java.util.Collections; @@ -39,12 +39,12 @@ import java.util.Map; import static org.elasticsearch.action.support.WriteRequest.RefreshPolicy.IMMEDIATE; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.search.aggregations.AggregationBuilders.histogram; import static org.elasticsearch.search.aggregations.AggregationBuilders.sum; import static org.elasticsearch.search.aggregations.PipelineAggregatorBuilders.bucketScript; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.notNullValue; @@ -68,8 +68,8 @@ private SearchRequestBuilder buildRequest(String script, Object... params) { SearchRequestBuilder req = client().prepareSearch().setIndices("test"); req.setQuery(QueryBuilders.matchAllQuery()) - .addSort(SortBuilders.fieldSort("id").order(SortOrder.ASC).unmappedType("long")) - .addScriptField("foo", new Script(ScriptType.INLINE, "expression", script, paramsMap)); + .addSort(SortBuilders.fieldSort("id").order(SortOrder.ASC).unmappedType("long")) + .addScriptField("foo", new Script(ScriptType.INLINE, "expression", script, paramsMap)); return req; } @@ -104,12 +104,15 @@ public void testBasicUsingDotValue() throws Exception { public void testScore() throws Exception { createIndex("test"); ensureGreen("test"); - indexRandom(true, - client().prepareIndex("test").setId("1").setSource("text", "hello goodbye"), - client().prepareIndex("test").setId("2").setSource("text", "hello hello hello goodbye"), - client().prepareIndex("test").setId("3").setSource("text", "hello hello goodebye")); + indexRandom( + true, + client().prepareIndex("test").setId("1").setSource("text", "hello goodbye"), + client().prepareIndex("test").setId("2").setSource("text", "hello hello hello goodbye"), + client().prepareIndex("test").setId("3").setSource("text", "hello hello goodebye") + ); ScriptScoreFunctionBuilder score = ScoreFunctionBuilders.scriptFunction( - new Script(ScriptType.INLINE, "expression", "1 / _score", Collections.emptyMap())); + new Script(ScriptType.INLINE, "expression", "1 / _score", Collections.emptyMap()) + ); SearchRequestBuilder req = client().prepareSearch().setIndices("test"); req.setQuery(QueryBuilders.functionScoreQuery(QueryBuilders.termQuery("text", "hello"), score).boostMode(CombineFunction.REPLACE)); req.setSearchType(SearchType.DFS_QUERY_THEN_FETCH); // make sure DF is consistent @@ -123,8 +126,7 @@ public void testScore() throws Exception { req = client().prepareSearch().setIndices("test"); req.setQuery(QueryBuilders.functionScoreQuery(QueryBuilders.termQuery("text", "hello"), score).boostMode(CombineFunction.REPLACE)); - score = ScoreFunctionBuilders.scriptFunction( - new Script(ScriptType.INLINE, "expression", "1 / _score", Collections.emptyMap())); + score = ScoreFunctionBuilders.scriptFunction(new Script(ScriptType.INLINE, "expression", "1 / _score", Collections.emptyMap())); req.addAggregation(AggregationBuilders.max("max_score").script((score).getScript())); req.setSearchType(SearchType.DFS_QUERY_THEN_FETCH); // make sure DF is consistent rsp = req.get(); @@ -134,11 +136,11 @@ public void testScore() throws Exception { public void testDateMethods() throws Exception { ElasticsearchAssertions.assertAcked(prepareCreate("test").setMapping("date0", "type=date", "date1", "type=date")); ensureGreen("test"); - indexRandom(true, - client().prepareIndex("test").setId("1") - .setSource("id", 1, "date0", "2015-04-28T04:02:07Z", "date1", "1985-09-01T23:11:01Z"), - client().prepareIndex("test").setId("2") - .setSource("id", 2, "date0", "2013-12-25T11:56:45Z", "date1", "1983-10-13T23:15:00Z")); + indexRandom( + true, + client().prepareIndex("test").setId("1").setSource("id", 1, "date0", "2015-04-28T04:02:07Z", "date1", "1985-09-01T23:11:01Z"), + client().prepareIndex("test").setId("2").setSource("id", 2, "date0", "2013-12-25T11:56:45Z", "date1", "1983-10-13T23:15:00Z") + ); SearchResponse rsp = buildRequest("doc['date0'].getSeconds() - doc['date0'].getMinutes()").get(); assertEquals(2, rsp.getHits().getTotalHits().value); SearchHits hits = rsp.getHits(); @@ -164,11 +166,11 @@ public void testDateMethods() throws Exception { public void testDateObjectMethods() throws Exception { ElasticsearchAssertions.assertAcked(prepareCreate("test").setMapping("date0", "type=date", "date1", "type=date")); ensureGreen("test"); - indexRandom(true, - client().prepareIndex("test").setId("1") - .setSource("id", 1, "date0", "2015-04-28T04:02:07Z", "date1", "1985-09-01T23:11:01Z"), - client().prepareIndex("test").setId("2") - .setSource("id", 2, "date0", "2013-12-25T11:56:45Z", "date1", "1983-10-13T23:15:00Z")); + indexRandom( + true, + client().prepareIndex("test").setId("1").setSource("id", 1, "date0", "2015-04-28T04:02:07Z", "date1", "1985-09-01T23:11:01Z"), + client().prepareIndex("test").setId("2").setSource("id", 2, "date0", "2013-12-25T11:56:45Z", "date1", "1983-10-13T23:15:00Z") + ); SearchResponse rsp = buildRequest("doc['date0'].date.secondOfMinute - doc['date0'].date.minuteOfHour").get(); assertEquals(2, rsp.getHits().getTotalHits().value); SearchHits hits = rsp.getHits(); @@ -192,16 +194,15 @@ public void testDateObjectMethods() throws Exception { } public void testMultiValueMethods() throws Exception { - ElasticsearchAssertions.assertAcked(prepareCreate("test").setMapping( - "double0", "type=double", - "double1", "type=double", - "double2", "type=double")); + ElasticsearchAssertions.assertAcked( + prepareCreate("test").setMapping("double0", "type=double", "double1", "type=double", "double2", "type=double") + ); ensureGreen("test"); Map doc1 = new HashMap<>(); doc1.put("id", 1); - doc1.put("double0", new Double[]{5.0d, 1.0d, 1.5d}); - doc1.put("double1", new Double[]{1.2d, 2.4d}); + doc1.put("double0", new Double[] { 5.0d, 1.0d, 1.5d }); + doc1.put("double1", new Double[] { 1.2d, 2.4d }); doc1.put("double2", 3.0d); Map doc2 = new HashMap<>(); @@ -211,14 +212,15 @@ public void testMultiValueMethods() throws Exception { Map doc3 = new HashMap<>(); doc3.put("id", 3); - doc3.put("double0", new Double[]{5.0d, 1.0d, 1.5d, -1.5d}); + doc3.put("double0", new Double[] { 5.0d, 1.0d, 1.5d, -1.5d }); doc3.put("double1", 4.0d); - indexRandom(true, - client().prepareIndex("test").setId("1").setSource(doc1), - client().prepareIndex("test").setId("2").setSource(doc2), - client().prepareIndex("test").setId("3").setSource(doc3)); - + indexRandom( + true, + client().prepareIndex("test").setId("1").setSource(doc1), + client().prepareIndex("test").setId("2").setSource(doc2), + client().prepareIndex("test").setId("3").setSource(doc3) + ); SearchResponse rsp = buildRequest("doc['double0'].count() + doc['double1'].count()").get(); assertSearchResponse(rsp); @@ -303,19 +305,27 @@ public void testInvalidDateMethodCall() throws Exception { buildRequest("doc['double'].getYear()").get(); fail(); } catch (SearchPhaseExecutionException e) { - assertThat(e.toString() + "should have contained IllegalArgumentException", - e.toString().contains("IllegalArgumentException"), equalTo(true)); - assertThat(e.toString() + "should have contained does not exist for numeric field", - e.toString().contains("does not exist for numeric field"), equalTo(true)); + assertThat( + e.toString() + "should have contained IllegalArgumentException", + e.toString().contains("IllegalArgumentException"), + equalTo(true) + ); + assertThat( + e.toString() + "should have contained does not exist for numeric field", + e.toString().contains("does not exist for numeric field"), + equalTo(true) + ); } } public void testSparseField() throws Exception { ElasticsearchAssertions.assertAcked(prepareCreate("test").setMapping("x", "type=long", "y", "type=long")); ensureGreen("test"); - indexRandom(true, - client().prepareIndex("test").setId("1").setSource("id", 1, "x", 4), - client().prepareIndex("test").setId("2").setSource("id", 2, "y", 2)); + indexRandom( + true, + client().prepareIndex("test").setId("1").setSource("id", 1, "x", 4), + client().prepareIndex("test").setId("2").setSource("id", 2, "y", 2) + ); SearchResponse rsp = buildRequest("doc['x'] + 1").get(); ElasticsearchAssertions.assertSearchResponse(rsp); SearchHits hits = rsp.getHits(); @@ -332,20 +342,24 @@ public void testMissingField() throws Exception { buildRequest("doc['bogus']").get(); fail("Expected missing field to cause failure"); } catch (SearchPhaseExecutionException e) { - assertThat(e.toString() + "should have contained ScriptException", - e.toString().contains("ScriptException"), equalTo(true)); - assertThat(e.toString() + "should have contained missing field error", - e.toString().contains("does not exist in mappings"), equalTo(true)); + assertThat(e.toString() + "should have contained ScriptException", e.toString().contains("ScriptException"), equalTo(true)); + assertThat( + e.toString() + "should have contained missing field error", + e.toString().contains("does not exist in mappings"), + equalTo(true) + ); } } public void testParams() throws Exception { createIndex("test"); ensureGreen("test"); - indexRandom(true, - client().prepareIndex("test").setId("1").setSource("id", 1, "x", 10), - client().prepareIndex("test").setId("2").setSource("id", 2, "x", 3), - client().prepareIndex("test").setId("3").setSource("id", 3, "x", 5)); + indexRandom( + true, + client().prepareIndex("test").setId("1").setSource("id", 1, "x", 10), + client().prepareIndex("test").setId("2").setSource("id", 2, "x", 3), + client().prepareIndex("test").setId("3").setSource("id", 3, "x", 5) + ); // a = int, b = double, c = long String script = "doc['x'] * a + b + ((c + doc['x']) > 5000000009 ? 1 : 0)"; SearchResponse rsp = buildRequest(script, "a", 2, "b", 3.5, "c", 5000000000L).get(); @@ -362,10 +376,8 @@ public void testCompileFailure() { buildRequest("garbage%@#%@").get(); fail("Expected expression compilation failure"); } catch (SearchPhaseExecutionException e) { - assertThat(e.toString() + "should have contained ScriptException", - e.toString().contains("ScriptException"), equalTo(true)); - assertThat(e.toString() + "should have contained compilation failure", - e.toString().contains("compile error"), equalTo(true)); + assertThat(e.toString() + "should have contained ScriptException", e.toString().contains("ScriptException"), equalTo(true)); + assertThat(e.toString() + "should have contained compilation failure", e.toString().contains("compile error"), equalTo(true)); } } @@ -375,10 +387,12 @@ public void testNonNumericParam() { buildRequest("a", "a", "astring").get(); fail("Expected string parameter to cause failure"); } catch (SearchPhaseExecutionException e) { - assertThat(e.toString() + "should have contained ScriptException", - e.toString().contains("ScriptException"), equalTo(true)); - assertThat(e.toString() + "should have contained non-numeric parameter error", - e.toString().contains("must be a numeric type"), equalTo(true)); + assertThat(e.toString() + "should have contained ScriptException", e.toString().contains("ScriptException"), equalTo(true)); + assertThat( + e.toString() + "should have contained non-numeric parameter error", + e.toString().contains("must be a numeric type"), + equalTo(true) + ); } } @@ -388,10 +402,12 @@ public void testNonNumericField() { buildRequest("doc['text.keyword']").get(); fail("Expected text field to cause execution failure"); } catch (SearchPhaseExecutionException e) { - assertThat(e.toString() + "should have contained ScriptException", - e.toString().contains("ScriptException"), equalTo(true)); - assertThat(e.toString() + "should have contained non-numeric field error", - e.toString().contains("must be numeric"), equalTo(true)); + assertThat(e.toString() + "should have contained ScriptException", e.toString().contains("ScriptException"), equalTo(true)); + assertThat( + e.toString() + "should have contained non-numeric field error", + e.toString().contains("must be numeric"), + equalTo(true) + ); } } @@ -401,10 +417,12 @@ public void testInvalidGlobalVariable() { buildRequest("bogus").get(); fail("Expected bogus variable to cause execution failure"); } catch (SearchPhaseExecutionException e) { - assertThat(e.toString() + "should have contained ScriptException", - e.toString().contains("ScriptException"), equalTo(true)); - assertThat(e.toString() + "should have contained unknown variable error", - e.toString().contains("Unknown variable"), equalTo(true)); + assertThat(e.toString() + "should have contained ScriptException", e.toString().contains("ScriptException"), equalTo(true)); + assertThat( + e.toString() + "should have contained unknown variable error", + e.toString().contains("Unknown variable"), + equalTo(true) + ); } } @@ -414,10 +432,12 @@ public void testDocWithoutField() { buildRequest("doc").get(); fail("Expected doc variable without field to cause execution failure"); } catch (SearchPhaseExecutionException e) { - assertThat(e.toString() + "should have contained ScriptException", - e.toString().contains("ScriptException"), equalTo(true)); - assertThat(e.toString() + "should have contained a missing specific field error", - e.toString().contains("must be used with a specific field"), equalTo(true)); + assertThat(e.toString() + "should have contained ScriptException", e.toString().contains("ScriptException"), equalTo(true)); + assertThat( + e.toString() + "should have contained a missing specific field error", + e.toString().contains("must be used with a specific field"), + equalTo(true) + ); } } @@ -427,10 +447,12 @@ public void testInvalidFieldMember() { buildRequest("doc['foo'].bogus").get(); fail("Expected bogus field member to cause execution failure"); } catch (SearchPhaseExecutionException e) { - assertThat(e.toString() + "should have contained ScriptException", - e.toString().contains("ScriptException"), equalTo(true)); - assertThat(e.toString() + "should have contained member variable [bogus] does not exist", - e.toString().contains("Member variable [bogus] does not exist"), equalTo(true)); + assertThat(e.toString() + "should have contained ScriptException", e.toString().contains("ScriptException"), equalTo(true)); + assertThat( + e.toString() + "should have contained member variable [bogus] does not exist", + e.toString().contains("Member variable [bogus] does not exist"), + equalTo(true) + ); } } @@ -438,26 +460,30 @@ public void testSpecialValueVariable() throws Exception { // i.e. _value for aggregations createIndex("test"); ensureGreen("test"); - indexRandom(true, - client().prepareIndex("test").setId("1").setSource("x", 5, "y", 1.2), - client().prepareIndex("test").setId("2").setSource("x", 10, "y", 1.4), - client().prepareIndex("test").setId("3").setSource("x", 13, "y", 1.8)); + indexRandom( + true, + client().prepareIndex("test").setId("1").setSource("x", 5, "y", 1.2), + client().prepareIndex("test").setId("2").setSource("x", 10, "y", 1.4), + client().prepareIndex("test").setId("3").setSource("x", 13, "y", 1.8) + ); SearchRequestBuilder req = client().prepareSearch().setIndices("test"); req.setQuery(QueryBuilders.matchAllQuery()) - .addAggregation( - AggregationBuilders.stats("int_agg").field("x") - .script(new Script(ScriptType.INLINE, - ExpressionScriptEngine.NAME, "_value * 3", Collections.emptyMap()))) - .addAggregation( - AggregationBuilders.stats("double_agg").field("y") - .script(new Script(ScriptType.INLINE, - ExpressionScriptEngine.NAME, "_value - 1.1", Collections.emptyMap()))) - .addAggregation( - AggregationBuilders.stats("const_agg").field("x") // specifically to test a script w/o _value - .script(new Script(ScriptType.INLINE, - ExpressionScriptEngine.NAME, "3.0", Collections.emptyMap())) - ); + .addAggregation( + AggregationBuilders.stats("int_agg") + .field("x") + .script(new Script(ScriptType.INLINE, ExpressionScriptEngine.NAME, "_value * 3", Collections.emptyMap())) + ) + .addAggregation( + AggregationBuilders.stats("double_agg") + .field("y") + .script(new Script(ScriptType.INLINE, ExpressionScriptEngine.NAME, "_value - 1.1", Collections.emptyMap())) + ) + .addAggregation( + AggregationBuilders.stats("const_agg") + .field("x") // specifically to test a script w/o _value + .script(new Script(ScriptType.INLINE, ExpressionScriptEngine.NAME, "3.0", Collections.emptyMap())) + ); SearchResponse rsp = req.get(); assertEquals(3, rsp.getHits().getTotalHits().value); @@ -478,20 +504,22 @@ public void testSpecialValueVariable() throws Exception { public void testStringSpecialValueVariable() throws Exception { // i.e. expression script for term aggregations, which is not allowed - assertAcked(client().admin().indices().prepareCreate("test") - .setMapping("text", "type=keyword").get()); + assertAcked(client().admin().indices().prepareCreate("test").setMapping("text", "type=keyword").get()); ensureGreen("test"); - indexRandom(true, - client().prepareIndex("test").setId("1").setSource("text", "hello"), - client().prepareIndex("test").setId("2").setSource("text", "goodbye"), - client().prepareIndex("test").setId("3").setSource("text", "hello")); + indexRandom( + true, + client().prepareIndex("test").setId("1").setSource("text", "hello"), + client().prepareIndex("test").setId("2").setSource("text", "goodbye"), + client().prepareIndex("test").setId("3").setSource("text", "hello") + ); SearchRequestBuilder req = client().prepareSearch().setIndices("test"); req.setQuery(QueryBuilders.matchAllQuery()) - .addAggregation( - AggregationBuilders.terms("term_agg").field("text") - .script( - new Script(ScriptType.INLINE, ExpressionScriptEngine.NAME, "_value", Collections.emptyMap()))); + .addAggregation( + AggregationBuilders.terms("term_agg") + .field("text") + .script(new Script(ScriptType.INLINE, ExpressionScriptEngine.NAME, "_value", Collections.emptyMap())) + ); String message; try { @@ -503,10 +531,8 @@ public void testStringSpecialValueVariable() throws Exception { } catch (SearchPhaseExecutionException e) { message = e.toString(); } - assertThat(message + "should have contained ScriptException", - message.contains("ScriptException"), equalTo(true)); - assertThat(message + "should have contained text variable error", - message.contains("text variable"), equalTo(true)); + assertThat(message + "should have contained ScriptException", message.contains("ScriptException"), equalTo(true)); + assertThat(message + "should have contained text variable error", message.contains("text variable"), equalTo(true)); } // test to make sure expressions are not allowed to be used as update scripts @@ -532,26 +558,38 @@ public void testInvalidUpdateScript() throws Exception { public void testPipelineAggregationScript() throws Exception { createIndex("agg_index"); ensureGreen("agg_index"); - indexRandom(true, - client().prepareIndex("agg_index").setId("1").setSource("one", 1.0, "two", 2.0, "three", 3.0, "four", 4.0), - client().prepareIndex("agg_index").setId("2").setSource("one", 2.0, "two", 2.0, "three", 3.0, "four", 4.0), - client().prepareIndex("agg_index").setId("3").setSource("one", 3.0, "two", 2.0, "three", 3.0, "four", 4.0), - client().prepareIndex("agg_index").setId("4").setSource("one", 4.0, "two", 2.0, "three", 3.0, "four", 4.0), - client().prepareIndex("agg_index").setId("5").setSource("one", 5.0, "two", 2.0, "three", 3.0, "four", 4.0)); - SearchResponse response = client() - .prepareSearch("agg_index") - .addAggregation( - histogram("histogram") - .field("one") - .interval(2) - .subAggregation(sum("twoSum").field("two")) - .subAggregation(sum("threeSum").field("three")) - .subAggregation(sum("fourSum").field("four")) - .subAggregation(bucketScript("totalSum", - new Script(ScriptType.INLINE, - ExpressionScriptEngine.NAME, "_value0 + _value1 + _value2", Collections.emptyMap()), - "twoSum", "threeSum", "fourSum"))) - .execute().actionGet(); + indexRandom( + true, + client().prepareIndex("agg_index").setId("1").setSource("one", 1.0, "two", 2.0, "three", 3.0, "four", 4.0), + client().prepareIndex("agg_index").setId("2").setSource("one", 2.0, "two", 2.0, "three", 3.0, "four", 4.0), + client().prepareIndex("agg_index").setId("3").setSource("one", 3.0, "two", 2.0, "three", 3.0, "four", 4.0), + client().prepareIndex("agg_index").setId("4").setSource("one", 4.0, "two", 2.0, "three", 3.0, "four", 4.0), + client().prepareIndex("agg_index").setId("5").setSource("one", 5.0, "two", 2.0, "three", 3.0, "four", 4.0) + ); + SearchResponse response = client().prepareSearch("agg_index") + .addAggregation( + histogram("histogram").field("one") + .interval(2) + .subAggregation(sum("twoSum").field("two")) + .subAggregation(sum("threeSum").field("three")) + .subAggregation(sum("fourSum").field("four")) + .subAggregation( + bucketScript( + "totalSum", + new Script( + ScriptType.INLINE, + ExpressionScriptEngine.NAME, + "_value0 + _value1 + _value2", + Collections.emptyMap() + ), + "twoSum", + "threeSum", + "fourSum" + ) + ) + ) + .execute() + .actionGet(); Histogram histogram = response.getAggregations().get("histogram"); assertThat(histogram, notNullValue()); @@ -577,15 +615,28 @@ public void testPipelineAggregationScript() throws Exception { } public void testGeo() throws Exception { - XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().startObject().startObject("_doc") - .startObject("properties").startObject("location").field("type", "geo_point"); + XContentBuilder xContentBuilder = XContentFactory.jsonBuilder() + .startObject() + .startObject("_doc") + .startObject("properties") + .startObject("location") + .field("type", "geo_point"); xContentBuilder.endObject().endObject().endObject().endObject(); assertAcked(prepareCreate("test").setMapping(xContentBuilder)); ensureGreen(); - client().prepareIndex("test").setId("1").setSource(jsonBuilder().startObject() - .field("name", "test") - .startObject("location").field("lat", 61.5240).field("lon", 105.3188).endObject() - .endObject()).execute().actionGet(); + client().prepareIndex("test") + .setId("1") + .setSource( + jsonBuilder().startObject() + .field("name", "test") + .startObject("location") + .field("lat", 61.5240) + .field("lon", 105.3188) + .endObject() + .endObject() + ) + .execute() + .actionGet(); refresh(); // access .lat SearchResponse rsp = buildRequest("doc['location'].lat").get(); @@ -610,15 +661,21 @@ public void testGeo() throws Exception { } public void testBoolean() throws Exception { - XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().startObject().startObject("_doc") - .startObject("properties").startObject("vip").field("type", "boolean"); + XContentBuilder xContentBuilder = XContentFactory.jsonBuilder() + .startObject() + .startObject("_doc") + .startObject("properties") + .startObject("vip") + .field("type", "boolean"); xContentBuilder.endObject().endObject().endObject().endObject(); assertAcked(prepareCreate("test").setMapping(xContentBuilder)); ensureGreen(); - indexRandom(true, - client().prepareIndex("test").setId("1").setSource("id", 1, "price", 1.0, "vip", true), - client().prepareIndex("test").setId("2").setSource("id", 2, "price", 2.0, "vip", false), - client().prepareIndex("test").setId("3").setSource("id", 3, "price", 2.0, "vip", false)); + indexRandom( + true, + client().prepareIndex("test").setId("1").setSource("id", 1, "price", 1.0, "vip", true), + client().prepareIndex("test").setId("2").setSource("id", 2, "price", 2.0, "vip", false), + client().prepareIndex("test").setId("3").setSource("id", 3, "price", 2.0, "vip", false) + ); // access .value SearchResponse rsp = buildRequest("doc['vip'].value").get(); assertSearchResponse(rsp); @@ -646,9 +703,11 @@ public void testBoolean() throws Exception { public void testFilterScript() throws Exception { createIndex("test"); ensureGreen("test"); - indexRandom(true, + indexRandom( + true, client().prepareIndex("test").setId("1").setSource("id", 1, "foo", 1.0), - client().prepareIndex("test").setId("2").setSource("id", 2, "foo", 0.0)); + client().prepareIndex("test").setId("2").setSource("id", 2, "foo", 0.0) + ); SearchRequestBuilder builder = buildRequest("doc['foo'].value"); Script script = new Script(ScriptType.INLINE, "expression", "doc['foo'].value", Collections.emptyMap()); builder.setQuery(QueryBuilders.boolQuery().filter(QueryBuilders.scriptQuery(script))); diff --git a/modules/lang-expression/src/internalClusterTest/java/org/elasticsearch/script/expression/StoredExpressionIT.java b/modules/lang-expression/src/internalClusterTest/java/org/elasticsearch/script/expression/StoredExpressionIT.java index 46665649a081b..53b93c2d98590 100644 --- a/modules/lang-expression/src/internalClusterTest/java/org/elasticsearch/script/expression/StoredExpressionIT.java +++ b/modules/lang-expression/src/internalClusterTest/java/org/elasticsearch/script/expression/StoredExpressionIT.java @@ -10,13 +10,13 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptType; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.test.ESIntegTestCase; +import org.elasticsearch.xcontent.XContentType; import java.io.IOException; import java.util.Collection; @@ -39,33 +39,39 @@ protected Collection> nodePlugins() { } public void testAllOpsDisabledIndexedScripts() throws IOException { - client().admin().cluster().preparePutStoredScript() - .setId("script1") - .setContent(new BytesArray("{\"script\": {\"lang\": \"expression\", \"source\": \"2\"} }"), XContentType.JSON) - .get(); + client().admin() + .cluster() + .preparePutStoredScript() + .setId("script1") + .setContent(new BytesArray("{\"script\": {\"lang\": \"expression\", \"source\": \"2\"} }"), XContentType.JSON) + .get(); client().prepareIndex("test").setId("1").setSource("{\"theField\":\"foo\"}", XContentType.JSON).get(); try { - client().prepareUpdate("test", "1") - .setScript(new Script(ScriptType.STORED, null, "script1", Collections.emptyMap())).get(); + client().prepareUpdate("test", "1").setScript(new Script(ScriptType.STORED, null, "script1", Collections.emptyMap())).get(); fail("update script should have been rejected"); - } catch(Exception e) { + } catch (Exception e) { assertThat(e.getMessage(), containsString("failed to execute script")); assertThat(e.getCause().getMessage(), containsString("Failed to compile stored script [script1] using lang [expression]")); } try { client().prepareSearch() - .setSource(new SearchSourceBuilder().scriptField("test1", - new Script(ScriptType.STORED, null, "script1", Collections.emptyMap()))) - .setIndices("test").get(); + .setSource( + new SearchSourceBuilder().scriptField("test1", new Script(ScriptType.STORED, null, "script1", Collections.emptyMap())) + ) + .setIndices("test") + .get(); fail("search script should have been rejected"); - } catch(Exception e) { + } catch (Exception e) { assertThat(e.toString(), containsString("cannot execute scripts using [field] context")); } try { client().prepareSearch("test") - .setSource( - new SearchSourceBuilder().aggregation(AggregationBuilders.terms("test").script( - new Script(ScriptType.STORED, null, "script1", Collections.emptyMap())))).get(); + .setSource( + new SearchSourceBuilder().aggregation( + AggregationBuilders.terms("test").script(new Script(ScriptType.STORED, null, "script1", Collections.emptyMap())) + ) + ) + .get(); } catch (Exception e) { assertThat(e.toString(), containsString("cannot execute scripts using [aggs] context")); } diff --git a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/CountMethodValueSource.java b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/CountMethodValueSource.java index a631946eac397..0b8c6c45d7500 100644 --- a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/CountMethodValueSource.java +++ b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/CountMethodValueSource.java @@ -10,8 +10,8 @@ import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.search.DoubleValues; -import org.elasticsearch.index.fielddata.LeafNumericFieldData; import org.elasticsearch.index.fielddata.IndexFieldData; +import org.elasticsearch.index.fielddata.LeafNumericFieldData; import org.elasticsearch.index.fielddata.SortedNumericDoubleValues; import java.io.IOException; diff --git a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/DateField.java b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/DateField.java index 49a88b393656b..b1177bed091e8 100644 --- a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/DateField.java +++ b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/DateField.java @@ -22,28 +22,28 @@ final class DateField { private DateField() {} // supported variables - static final String VALUE_VARIABLE = "value"; - static final String EMPTY_VARIABLE = "empty"; - static final String LENGTH_VARIABLE = "length"; + static final String VALUE_VARIABLE = "value"; + static final String EMPTY_VARIABLE = "empty"; + static final String LENGTH_VARIABLE = "length"; // supported methods - static final String GETVALUE_METHOD = "getValue"; - static final String ISEMPTY_METHOD = "isEmpty"; - static final String SIZE_METHOD = "size"; - static final String MINIMUM_METHOD = "min"; - static final String MAXIMUM_METHOD = "max"; - static final String AVERAGE_METHOD = "avg"; - static final String MEDIAN_METHOD = "median"; - static final String SUM_METHOD = "sum"; - static final String COUNT_METHOD = "count"; + static final String GETVALUE_METHOD = "getValue"; + static final String ISEMPTY_METHOD = "isEmpty"; + static final String SIZE_METHOD = "size"; + static final String MINIMUM_METHOD = "min"; + static final String MAXIMUM_METHOD = "max"; + static final String AVERAGE_METHOD = "avg"; + static final String MEDIAN_METHOD = "median"; + static final String SUM_METHOD = "sum"; + static final String COUNT_METHOD = "count"; // date-specific - static final String GET_YEAR_METHOD = "getYear"; - static final String GET_MONTH_METHOD = "getMonth"; + static final String GET_YEAR_METHOD = "getYear"; + static final String GET_MONTH_METHOD = "getMonth"; static final String GET_DAY_OF_MONTH_METHOD = "getDayOfMonth"; - static final String GET_HOUR_OF_DAY_METHOD = "getHourOfDay"; - static final String GET_MINUTES_METHOD = "getMinutes"; - static final String GET_SECONDS_METHOD = "getSeconds"; + static final String GET_HOUR_OF_DAY_METHOD = "getHourOfDay"; + static final String GET_MINUTES_METHOD = "getMinutes"; + static final String GET_SECONDS_METHOD = "getSeconds"; static DoubleValuesSource getVariable(IndexFieldData fieldData, String fieldName, String variable) { switch (variable) { diff --git a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/DateMethodValueSource.java b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/DateMethodValueSource.java index cc4ec7deaf201..360ec5c3046db 100644 --- a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/DateMethodValueSource.java +++ b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/DateMethodValueSource.java @@ -10,8 +10,8 @@ import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.search.DoubleValues; -import org.elasticsearch.index.fielddata.LeafNumericFieldData; import org.elasticsearch.index.fielddata.IndexFieldData; +import org.elasticsearch.index.fielddata.LeafNumericFieldData; import org.elasticsearch.index.fielddata.NumericDoubleValues; import org.elasticsearch.search.MultiValueMode; @@ -44,7 +44,7 @@ public DoubleValues getValues(LeafReaderContext leaf, DoubleValues scores) { return new DoubleValues() { @Override public double doubleValue() throws IOException { - calendar.setTimeInMillis((long)docValues.doubleValue()); + calendar.setTimeInMillis((long) docValues.doubleValue()); return calendar.get(calendarType); } diff --git a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/DateObject.java b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/DateObject.java index 78bbfd9dde167..a98a16ed5c96c 100644 --- a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/DateObject.java +++ b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/DateObject.java @@ -24,44 +24,44 @@ final class DateObject { private DateObject() {} // supported variables - static final String CENTURY_OF_ERA_VARIABLE = "centuryOfEra"; - static final String DAY_OF_MONTH_VARIABLE = "dayOfMonth"; - static final String DAY_OF_WEEK_VARIABLE = "dayOfWeek"; - static final String DAY_OF_YEAR_VARIABLE = "dayOfYear"; - static final String ERA_VARIABLE = "era"; - static final String HOUR_OF_DAY_VARIABLE = "hourOfDay"; - static final String MILLIS_OF_DAY_VARIABLE = "millisOfDay"; - static final String MILLIS_OF_SECOND_VARIABLE = "millisOfSecond"; - static final String MINUTE_OF_DAY_VARIABLE = "minuteOfDay"; - static final String MINUTE_OF_HOUR_VARIABLE = "minuteOfHour"; - static final String MONTH_OF_YEAR_VARIABLE = "monthOfYear"; - static final String SECOND_OF_DAY_VARIABLE = "secondOfDay"; - static final String SECOND_OF_MINUTE_VARIABLE = "secondOfMinute"; - static final String WEEK_OF_WEEK_YEAR_VARIABLE = "weekOfWeekyear"; - static final String WEEK_YEAR_VARIABLE = "weekyear"; - static final String YEAR_VARIABLE = "year"; - static final String YEAR_OF_CENTURY_VARIABLE = "yearOfCentury"; - static final String YEAR_OF_ERA_VARIABLE = "yearOfEra"; + static final String CENTURY_OF_ERA_VARIABLE = "centuryOfEra"; + static final String DAY_OF_MONTH_VARIABLE = "dayOfMonth"; + static final String DAY_OF_WEEK_VARIABLE = "dayOfWeek"; + static final String DAY_OF_YEAR_VARIABLE = "dayOfYear"; + static final String ERA_VARIABLE = "era"; + static final String HOUR_OF_DAY_VARIABLE = "hourOfDay"; + static final String MILLIS_OF_DAY_VARIABLE = "millisOfDay"; + static final String MILLIS_OF_SECOND_VARIABLE = "millisOfSecond"; + static final String MINUTE_OF_DAY_VARIABLE = "minuteOfDay"; + static final String MINUTE_OF_HOUR_VARIABLE = "minuteOfHour"; + static final String MONTH_OF_YEAR_VARIABLE = "monthOfYear"; + static final String SECOND_OF_DAY_VARIABLE = "secondOfDay"; + static final String SECOND_OF_MINUTE_VARIABLE = "secondOfMinute"; + static final String WEEK_OF_WEEK_YEAR_VARIABLE = "weekOfWeekyear"; + static final String WEEK_YEAR_VARIABLE = "weekyear"; + static final String YEAR_VARIABLE = "year"; + static final String YEAR_OF_CENTURY_VARIABLE = "yearOfCentury"; + static final String YEAR_OF_ERA_VARIABLE = "yearOfEra"; // supported methods - static final String GETCENTURY_OF_ERA_METHOD = "getCenturyOfEra"; - static final String GETDAY_OF_MONTH_METHOD = "getDayOfMonth"; - static final String GETDAY_OF_WEEK_METHOD = "getDayOfWeek"; - static final String GETDAY_OF_YEAR_METHOD = "getDayOfYear"; - static final String GETERA_METHOD = "getEra"; - static final String GETHOUR_OF_DAY_METHOD = "getHourOfDay"; - static final String GETMILLIS_OF_DAY_METHOD = "getMillisOfDay"; - static final String GETMILLIS_OF_SECOND_METHOD = "getMillisOfSecond"; - static final String GETMINUTE_OF_DAY_METHOD = "getMinuteOfDay"; - static final String GETMINUTE_OF_HOUR_METHOD = "getMinuteOfHour"; - static final String GETMONTH_OF_YEAR_METHOD = "getMonthOfYear"; - static final String GETSECOND_OF_DAY_METHOD = "getSecondOfDay"; - static final String GETSECOND_OF_MINUTE_METHOD = "getSecondOfMinute"; - static final String GETWEEK_OF_WEEK_YEAR_METHOD = "getWeekOfWeekyear"; - static final String GETWEEK_YEAR_METHOD = "getWeekyear"; - static final String GETYEAR_METHOD = "getYear"; - static final String GETYEAR_OF_CENTURY_METHOD = "getYearOfCentury"; - static final String GETYEAR_OF_ERA_METHOD = "getYearOfEra"; + static final String GETCENTURY_OF_ERA_METHOD = "getCenturyOfEra"; + static final String GETDAY_OF_MONTH_METHOD = "getDayOfMonth"; + static final String GETDAY_OF_WEEK_METHOD = "getDayOfWeek"; + static final String GETDAY_OF_YEAR_METHOD = "getDayOfYear"; + static final String GETERA_METHOD = "getEra"; + static final String GETHOUR_OF_DAY_METHOD = "getHourOfDay"; + static final String GETMILLIS_OF_DAY_METHOD = "getMillisOfDay"; + static final String GETMILLIS_OF_SECOND_METHOD = "getMillisOfSecond"; + static final String GETMINUTE_OF_DAY_METHOD = "getMinuteOfDay"; + static final String GETMINUTE_OF_HOUR_METHOD = "getMinuteOfHour"; + static final String GETMONTH_OF_YEAR_METHOD = "getMonthOfYear"; + static final String GETSECOND_OF_DAY_METHOD = "getSecondOfDay"; + static final String GETSECOND_OF_MINUTE_METHOD = "getSecondOfMinute"; + static final String GETWEEK_OF_WEEK_YEAR_METHOD = "getWeekOfWeekyear"; + static final String GETWEEK_YEAR_METHOD = "getWeekyear"; + static final String GETYEAR_METHOD = "getYear"; + static final String GETYEAR_OF_CENTURY_METHOD = "getYearOfCentury"; + static final String GETYEAR_OF_ERA_METHOD = "getYearOfEra"; static DoubleValuesSource getVariable(IndexFieldData fieldData, String fieldName, String variable) { switch (variable) { @@ -92,11 +92,19 @@ static DoubleValuesSource getVariable(IndexFieldData fieldData, String fieldN case SECOND_OF_MINUTE_VARIABLE: return new DateObjectValueSource(fieldData, MultiValueMode.MIN, variable, ZonedDateTime::getSecond); case WEEK_OF_WEEK_YEAR_VARIABLE: - return new DateObjectValueSource(fieldData, MultiValueMode.MIN, variable, - zdt -> zdt.get(DateFormatters.WEEK_FIELDS_ROOT.weekOfWeekBasedYear())); + return new DateObjectValueSource( + fieldData, + MultiValueMode.MIN, + variable, + zdt -> zdt.get(DateFormatters.WEEK_FIELDS_ROOT.weekOfWeekBasedYear()) + ); case WEEK_YEAR_VARIABLE: - return new DateObjectValueSource(fieldData, MultiValueMode.MIN, variable, - zdt -> zdt.get(DateFormatters.WEEK_FIELDS_ROOT.weekBasedYear())); + return new DateObjectValueSource( + fieldData, + MultiValueMode.MIN, + variable, + zdt -> zdt.get(DateFormatters.WEEK_FIELDS_ROOT.weekBasedYear()) + ); case YEAR_VARIABLE: return new DateObjectValueSource(fieldData, MultiValueMode.MIN, variable, ZonedDateTime::getYear); case YEAR_OF_CENTURY_VARIABLE: @@ -104,8 +112,9 @@ static DoubleValuesSource getVariable(IndexFieldData fieldData, String fieldN case YEAR_OF_ERA_VARIABLE: return new DateObjectValueSource(fieldData, MultiValueMode.MIN, variable, zdt -> zdt.get(ChronoField.YEAR_OF_ERA)); default: - throw new IllegalArgumentException("Member variable [" + variable + - "] does not exist for date object on field [" + fieldName + "]."); + throw new IllegalArgumentException( + "Member variable [" + variable + "] does not exist for date object on field [" + fieldName + "]." + ); } } @@ -138,11 +147,19 @@ static DoubleValuesSource getMethod(IndexFieldData fieldData, String fieldNam case GETSECOND_OF_MINUTE_METHOD: return new DateObjectValueSource(fieldData, MultiValueMode.MIN, method, ZonedDateTime::getSecond); case GETWEEK_OF_WEEK_YEAR_METHOD: - return new DateObjectValueSource(fieldData, MultiValueMode.MIN, method, - zdt -> zdt.get(DateFormatters.WEEK_FIELDS_ROOT.weekOfWeekBasedYear())); + return new DateObjectValueSource( + fieldData, + MultiValueMode.MIN, + method, + zdt -> zdt.get(DateFormatters.WEEK_FIELDS_ROOT.weekOfWeekBasedYear()) + ); case GETWEEK_YEAR_METHOD: - return new DateObjectValueSource(fieldData, MultiValueMode.MIN, method, - zdt -> zdt.get(DateFormatters.WEEK_FIELDS_ROOT.weekBasedYear())); + return new DateObjectValueSource( + fieldData, + MultiValueMode.MIN, + method, + zdt -> zdt.get(DateFormatters.WEEK_FIELDS_ROOT.weekBasedYear()) + ); case GETYEAR_METHOD: return new DateObjectValueSource(fieldData, MultiValueMode.MIN, method, ZonedDateTime::getYear); case GETYEAR_OF_CENTURY_METHOD: @@ -150,8 +167,9 @@ static DoubleValuesSource getMethod(IndexFieldData fieldData, String fieldNam case GETYEAR_OF_ERA_METHOD: return new DateObjectValueSource(fieldData, MultiValueMode.MIN, method, zdt -> zdt.get(ChronoField.YEAR_OF_ERA)); default: - throw new IllegalArgumentException("Member method [" + method + - "] does not exist for date object on field [" + fieldName + "]."); + throw new IllegalArgumentException( + "Member method [" + method + "] does not exist for date object on field [" + fieldName + "]." + ); } } } diff --git a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/DateObjectValueSource.java b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/DateObjectValueSource.java index 48b268964400b..f9b67c427bba1 100644 --- a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/DateObjectValueSource.java +++ b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/DateObjectValueSource.java @@ -28,8 +28,12 @@ class DateObjectValueSource extends FieldDataValueSource { final String methodName; final ToIntFunction function; - DateObjectValueSource(IndexFieldData indexFieldData, MultiValueMode multiValueMode, - String methodName, ToIntFunction function) { + DateObjectValueSource( + IndexFieldData indexFieldData, + MultiValueMode multiValueMode, + String methodName, + ToIntFunction function + ) { super(indexFieldData, multiValueMode); Objects.requireNonNull(methodName); @@ -45,7 +49,7 @@ public DoubleValues getValues(LeafReaderContext leaf, DoubleValues scores) { return new DoubleValues() { @Override public double doubleValue() throws IOException { - return function.applyAsInt(ZonedDateTime.ofInstant(Instant.ofEpochMilli((long)docValues.doubleValue()), ZoneOffset.UTC)); + return function.applyAsInt(ZonedDateTime.ofInstant(Instant.ofEpochMilli((long) docValues.doubleValue()), ZoneOffset.UTC)); } @Override diff --git a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/EmptyMemberValueSource.java b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/EmptyMemberValueSource.java index 43dc94aca49e0..a3b4c1e3394c7 100644 --- a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/EmptyMemberValueSource.java +++ b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/EmptyMemberValueSource.java @@ -10,8 +10,8 @@ import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.search.DoubleValues; -import org.elasticsearch.index.fielddata.LeafNumericFieldData; import org.elasticsearch.index.fielddata.IndexFieldData; +import org.elasticsearch.index.fielddata.LeafNumericFieldData; import org.elasticsearch.index.fielddata.SortedNumericDoubleValues; import java.io.IOException; diff --git a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionAggregationScript.java b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionAggregationScript.java index 5edd9423bc0ca..ec9435d9386b5 100644 --- a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionAggregationScript.java +++ b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionAggregationScript.java @@ -8,7 +8,6 @@ package org.elasticsearch.script.expression; -import java.io.IOException; import org.apache.lucene.expressions.Bindings; import org.apache.lucene.expressions.Expression; import org.apache.lucene.expressions.SimpleBindings; @@ -18,6 +17,8 @@ import org.elasticsearch.script.AggregationScript; import org.elasticsearch.script.GeneralScriptException; +import java.io.IOException; + /** * A bridge to evaluate an {@link Expression} against {@link Bindings} in the context * of a {@link AggregationScript}. @@ -82,7 +83,7 @@ public void setNextAggregationValue(Object value) { // _value isn't used in script if specialValue == null if (specialValue != null) { if (value instanceof Number) { - specialValue.setValue(((Number)value).doubleValue()); + specialValue.setValue(((Number) value).doubleValue()); } else { throw new GeneralScriptException("Cannot use expression with text variable using " + exprScript); } diff --git a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionNumberSortScript.java b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionNumberSortScript.java index 504a706ceaf28..8cbb6cedd39a1 100644 --- a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionNumberSortScript.java +++ b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionNumberSortScript.java @@ -8,7 +8,6 @@ package org.elasticsearch.script.expression; -import java.io.IOException; import org.apache.lucene.expressions.Bindings; import org.apache.lucene.expressions.Expression; import org.apache.lucene.expressions.SimpleBindings; @@ -20,6 +19,8 @@ import org.elasticsearch.script.LeafReaderContextSupplier; import org.elasticsearch.script.NumberSortScript; +import java.io.IOException; + /** * A bridge to evaluate an {@link Expression} against {@link Bindings} in the context * of a {@link NumberSortScript}. @@ -40,7 +41,7 @@ class ExpressionNumberSortScript implements NumberSortScript.LeafFactory { @Override public NumberSortScript newInstance(final DocReader reader) throws IOException { - // Use DocReader to get the leaf context while transitioning to DocReader for Painless. DocReader for expressions should follow. + // Use DocReader to get the leaf context while transitioning to DocReader for Painless. DocReader for expressions should follow. if (reader instanceof LeafReaderContextSupplier == false) { throw new IllegalStateException( "Expected LeafReaderContextSupplier when creating expression NumberSortScript instead of [" + reader + "]" diff --git a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionPlugin.java b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionPlugin.java index b176ccdae9c93..9c39a9c0297f5 100644 --- a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionPlugin.java +++ b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionPlugin.java @@ -8,14 +8,14 @@ package org.elasticsearch.script.expression; -import java.util.Collection; - import org.elasticsearch.common.settings.Settings; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.plugins.ScriptPlugin; import org.elasticsearch.script.ScriptContext; import org.elasticsearch.script.ScriptEngine; +import java.util.Collection; + public class ExpressionPlugin extends Plugin implements ScriptPlugin { @Override diff --git a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionScoreScript.java b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionScoreScript.java index fd89ccc6405cb..159851affd004 100644 --- a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionScoreScript.java +++ b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionScoreScript.java @@ -44,7 +44,7 @@ public boolean needs_score() { @Override public ScoreScript newInstance(final DocReader reader) throws IOException { - // Use DocReader to get the leaf context while transitioning to DocReader for Painless. DocReader for expressions should follow. + // Use DocReader to get the leaf context while transitioning to DocReader for Painless. DocReader for expressions should follow. if (reader instanceof LeafReaderContextSupplier == false) { throw new IllegalStateException( "Expected LeafReaderContextSupplier when creating expression ExpressionScoreScript instead of [" + reader + "]" diff --git a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionScriptEngine.java b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionScriptEngine.java index f352a32f2d27c..0d2f71ade6e51 100644 --- a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionScriptEngine.java +++ b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionScriptEngine.java @@ -54,89 +54,89 @@ public class ExpressionScriptEngine implements ScriptEngine { public static final String NAME = "expression"; - private static Map, Function> contexts = Map.of( + private static Map, Function> contexts = Map.of( BucketAggregationScript.CONTEXT, - ExpressionScriptEngine::newBucketAggregationScriptFactory, + ExpressionScriptEngine::newBucketAggregationScriptFactory, BucketAggregationSelectorScript.CONTEXT, - (Expression expr) -> { - BucketAggregationScript.Factory factory = newBucketAggregationScriptFactory(expr); - BucketAggregationSelectorScript.Factory wrappedFactory = parameters -> new BucketAggregationSelectorScript(parameters) { - @Override - public boolean execute() { - return factory.newInstance(getParams()).execute().doubleValue() == 1.0; - } - }; - return wrappedFactory; - }, - - FilterScript.CONTEXT, - (Expression expr) -> new FilterScript.Factory() { + (Expression expr) -> { + BucketAggregationScript.Factory factory = newBucketAggregationScriptFactory(expr); + BucketAggregationSelectorScript.Factory wrappedFactory = parameters -> new BucketAggregationSelectorScript(parameters) { @Override - public boolean isResultDeterministic() { - return true; + public boolean execute() { + return factory.newInstance(getParams()).execute().doubleValue() == 1.0; } + }; + return wrappedFactory; + }, - @Override - public FilterScript.LeafFactory newFactory(Map params, SearchLookup lookup) { - return newFilterScript(expr, lookup, params); - } - }, + FilterScript.CONTEXT, + (Expression expr) -> new FilterScript.Factory() { + @Override + public boolean isResultDeterministic() { + return true; + } + + @Override + public FilterScript.LeafFactory newFactory(Map params, SearchLookup lookup) { + return newFilterScript(expr, lookup, params); + } + }, ScoreScript.CONTEXT, - (Expression expr) -> new ScoreScript.Factory() { - @Override - public ScoreScript.LeafFactory newFactory(Map params, SearchLookup lookup) { - return newScoreScript(expr, lookup, params); - } + (Expression expr) -> new ScoreScript.Factory() { + @Override + public ScoreScript.LeafFactory newFactory(Map params, SearchLookup lookup) { + return newScoreScript(expr, lookup, params); + } - @Override - public boolean isResultDeterministic() { - return true; - } - }, + @Override + public boolean isResultDeterministic() { + return true; + } + }, TermsSetQueryScript.CONTEXT, - (Expression expr) -> (TermsSetQueryScript.Factory) (p, lookup) -> newTermsSetQueryScript(expr, lookup, p), + (Expression expr) -> (TermsSetQueryScript.Factory) (p, lookup) -> newTermsSetQueryScript(expr, lookup, p), AggregationScript.CONTEXT, - (Expression expr) -> new AggregationScript.Factory() { - @Override - public AggregationScript.LeafFactory newFactory(Map params, SearchLookup lookup) { - return newAggregationScript(expr, lookup, params); - } + (Expression expr) -> new AggregationScript.Factory() { + @Override + public AggregationScript.LeafFactory newFactory(Map params, SearchLookup lookup) { + return newAggregationScript(expr, lookup, params); + } - @Override - public boolean isResultDeterministic() { - return true; - } - }, + @Override + public boolean isResultDeterministic() { + return true; + } + }, NumberSortScript.CONTEXT, - (Expression expr) -> new NumberSortScript.Factory() { - @Override - public NumberSortScript.LeafFactory newFactory(Map params, SearchLookup lookup) { - return newSortScript(expr, lookup, params); - } + (Expression expr) -> new NumberSortScript.Factory() { + @Override + public NumberSortScript.LeafFactory newFactory(Map params, SearchLookup lookup) { + return newSortScript(expr, lookup, params); + } - @Override - public boolean isResultDeterministic() { - return true; - } - }, + @Override + public boolean isResultDeterministic() { + return true; + } + }, FieldScript.CONTEXT, - (Expression expr) -> new FieldScript.Factory() { - @Override - public FieldScript.LeafFactory newFactory(Map params, SearchLookup lookup) { - return newFieldScript(expr, lookup, params); - } + (Expression expr) -> new FieldScript.Factory() { + @Override + public FieldScript.LeafFactory newFactory(Map params, SearchLookup lookup) { + return newFieldScript(expr, lookup, params); + } - @Override - public boolean isResultDeterministic() { - return true; - } + @Override + public boolean isResultDeterministic() { + return true; } + } ); @Override @@ -145,12 +145,7 @@ public String getType() { } @Override - public T compile( - String scriptName, - String scriptSource, - ScriptContext context, - Map params - ) { + public T compile(String scriptName, String scriptSource, ScriptContext context, Map params) { // classloader created here final SecurityManager sm = System.getSecurityManager(); SpecialPermission.check(); @@ -194,8 +189,7 @@ public Set> getSupportedContexts() { private static BucketAggregationScript.Factory newBucketAggregationScriptFactory(Expression expr) { return parameters -> { - ReplaceableConstDoubleValues[] functionValuesArray = - new ReplaceableConstDoubleValues[expr.variables.length]; + ReplaceableConstDoubleValues[] functionValuesArray = new ReplaceableConstDoubleValues[expr.variables.length]; Map functionValuesMap = new HashMap<>(); for (int i = 0; i < expr.variables.length; ++i) { functionValuesArray[i] = new ReplaceableConstDoubleValues(); @@ -207,12 +201,24 @@ public Double execute() { getParams().forEach((name, value) -> { ReplaceableConstDoubleValues placeholder = functionValuesMap.get(name); if (placeholder == null) { - throw new IllegalArgumentException("Error using " + expr + ". " + - "The variable [" + name + "] does not exist in the executable expressions script."); + throw new IllegalArgumentException( + "Error using " + + expr + + ". " + + "The variable [" + + name + + "] does not exist in the executable expressions script." + ); } else if (value instanceof Number == false) { - throw new IllegalArgumentException("Error using " + expr + ". " + - "Executable expressions scripts can only process numbers." + - " The variable [" + name + "] is not a number."); + throw new IllegalArgumentException( + "Error using " + + expr + + ". " + + "Executable expressions scripts can only process numbers." + + " The variable [" + + name + + "] is not a number." + ); } else { placeholder.setValue(((Number) value).doubleValue()); } @@ -250,8 +256,11 @@ private static NumberSortScript.LeafFactory newSortScript(Expression expr, Searc return new ExpressionNumberSortScript(expr, bindings, needsScores); } - private static TermsSetQueryScript.LeafFactory newTermsSetQueryScript(Expression expr, SearchLookup lookup, - @Nullable Map vars) { + private static TermsSetQueryScript.LeafFactory newTermsSetQueryScript( + Expression expr, + SearchLookup lookup, + @Nullable Map vars + ) { // NOTE: if we need to do anything complicated with bindings in the future, we can just extend Bindings, // instead of complicating SimpleBindings (which should stay simple) SimpleBindings bindings = new SimpleBindings(); @@ -272,8 +281,11 @@ private static TermsSetQueryScript.LeafFactory newTermsSetQueryScript(Expression return new ExpressionTermSetQueryScript(expr, bindings); } - private static AggregationScript.LeafFactory newAggregationScript(Expression expr, SearchLookup lookup, - @Nullable Map vars) { + private static AggregationScript.LeafFactory newAggregationScript( + Expression expr, + SearchLookup lookup, + @Nullable Map vars + ) { // NOTE: if we need to do anything complicated with bindings in the future, we can just extend Bindings, // instead of complicating SimpleBindings (which should stay simple) SimpleBindings bindings = new SimpleBindings(); @@ -337,6 +349,7 @@ private static FilterScript.LeafFactory newFilterScript(Expression expr, SearchL public boolean execute() { return script.execute(null) != 0.0; } + @Override public void setDocument(int docid) { script.setDocument(docid); @@ -488,13 +501,13 @@ private static DoubleValuesSource getDocValueSource(String variable, SearchLooku // TODO: document and/or error if params contains _score? // NOTE: by checking for the variable in params first, it allows masking document fields with a global constant, // but if we were to reverse it, we could provide a way to supply dynamic defaults for documents missing the field? - private static void bindFromParams(@Nullable final Map params, - final SimpleBindings bindings, final String variable) throws ParseException { + private static void bindFromParams(@Nullable final Map params, final SimpleBindings bindings, final String variable) + throws ParseException { // NOTE: by checking for the variable in vars first, it allows masking document fields with a global constant, // but if we were to reverse it, we could provide a way to supply dynamic defaults for documents missing the field? Object value = params.get(variable); if (value instanceof Number) { - bindings.add(variable, DoubleValuesSource.constant(((Number)value).doubleValue())); + bindings.add(variable, DoubleValuesSource.constant(((Number) value).doubleValue())); } else { throw new ParseException("Parameter [" + variable + "] must be a numeric type", 0); } diff --git a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionTermSetQueryScript.java b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionTermSetQueryScript.java index 1843d377d0b8c..8dec2e93b6d00 100644 --- a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionTermSetQueryScript.java +++ b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionTermSetQueryScript.java @@ -8,7 +8,6 @@ package org.elasticsearch.script.expression; -import java.io.IOException; import org.apache.lucene.expressions.Bindings; import org.apache.lucene.expressions.Expression; import org.apache.lucene.expressions.SimpleBindings; @@ -18,6 +17,8 @@ import org.elasticsearch.script.GeneralScriptException; import org.elasticsearch.script.TermsSetQueryScript; +import java.io.IOException; + /** * A bridge to evaluate an {@link Expression} against {@link Bindings} in the context * of a {@link TermsSetQueryScript}. diff --git a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/FieldDataValueSource.java b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/FieldDataValueSource.java index 458fb5203a7c5..656a420cdfcb6 100644 --- a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/FieldDataValueSource.java +++ b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/FieldDataValueSource.java @@ -11,8 +11,8 @@ import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.queries.function.ValueSource; import org.apache.lucene.search.DoubleValues; -import org.elasticsearch.index.fielddata.LeafNumericFieldData; import org.elasticsearch.index.fielddata.IndexFieldData; +import org.elasticsearch.index.fielddata.LeafNumericFieldData; import org.elasticsearch.index.fielddata.NumericDoubleValues; import org.elasticsearch.search.MultiValueMode; diff --git a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/GeoField.java b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/GeoField.java index df97cf0f98c3f..74af9cf742480 100644 --- a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/GeoField.java +++ b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/GeoField.java @@ -19,14 +19,14 @@ final class GeoField { private GeoField() {} // supported variables - static final String EMPTY_VARIABLE = "empty"; - static final String LAT_VARIABLE = "lat"; - static final String LON_VARIABLE = "lon"; + static final String EMPTY_VARIABLE = "empty"; + static final String LAT_VARIABLE = "lat"; + static final String LON_VARIABLE = "lon"; // supported methods - static final String ISEMPTY_METHOD = "isEmpty"; - static final String GETLAT_METHOD = "getLat"; - static final String GETLON_METHOD = "getLon"; + static final String ISEMPTY_METHOD = "isEmpty"; + static final String GETLAT_METHOD = "getLat"; + static final String GETLON_METHOD = "getLon"; static DoubleValuesSource getVariable(IndexFieldData fieldData, String fieldName, String variable) { switch (variable) { diff --git a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/GeoLatitudeValueSource.java b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/GeoLatitudeValueSource.java index ee1da9b9df696..d64671ec06688 100644 --- a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/GeoLatitudeValueSource.java +++ b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/GeoLatitudeValueSource.java @@ -10,8 +10,8 @@ import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.search.DoubleValues; -import org.elasticsearch.index.fielddata.LeafGeoPointFieldData; import org.elasticsearch.index.fielddata.IndexFieldData; +import org.elasticsearch.index.fielddata.LeafGeoPointFieldData; import org.elasticsearch.index.fielddata.MultiGeoPointValues; import java.io.IOException; diff --git a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/GeoLongitudeValueSource.java b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/GeoLongitudeValueSource.java index 287be71489991..33f3602e2d702 100644 --- a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/GeoLongitudeValueSource.java +++ b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/GeoLongitudeValueSource.java @@ -10,8 +10,8 @@ import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.search.DoubleValues; -import org.elasticsearch.index.fielddata.LeafGeoPointFieldData; import org.elasticsearch.index.fielddata.IndexFieldData; +import org.elasticsearch.index.fielddata.LeafGeoPointFieldData; import org.elasticsearch.index.fielddata.MultiGeoPointValues; import java.io.IOException; diff --git a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/NumericField.java b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/NumericField.java index a594dbee28374..1e57f39506b64 100644 --- a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/NumericField.java +++ b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/NumericField.java @@ -20,20 +20,20 @@ final class NumericField { private NumericField() {} // supported variables - static final String VALUE_VARIABLE = "value"; - static final String EMPTY_VARIABLE = "empty"; - static final String LENGTH_VARIABLE = "length"; + static final String VALUE_VARIABLE = "value"; + static final String EMPTY_VARIABLE = "empty"; + static final String LENGTH_VARIABLE = "length"; // supported methods - static final String GETVALUE_METHOD = "getValue"; - static final String ISEMPTY_METHOD = "isEmpty"; - static final String SIZE_METHOD = "size"; - static final String MINIMUM_METHOD = "min"; - static final String MAXIMUM_METHOD = "max"; - static final String AVERAGE_METHOD = "avg"; - static final String MEDIAN_METHOD = "median"; - static final String SUM_METHOD = "sum"; - static final String COUNT_METHOD = "count"; + static final String GETVALUE_METHOD = "getValue"; + static final String ISEMPTY_METHOD = "isEmpty"; + static final String SIZE_METHOD = "size"; + static final String MINIMUM_METHOD = "min"; + static final String MAXIMUM_METHOD = "max"; + static final String AVERAGE_METHOD = "avg"; + static final String MEDIAN_METHOD = "median"; + static final String SUM_METHOD = "sum"; + static final String COUNT_METHOD = "count"; static DoubleValuesSource getVariable(IndexFieldData fieldData, String fieldName, String variable) { switch (variable) { @@ -44,8 +44,9 @@ static DoubleValuesSource getVariable(IndexFieldData fieldData, String fieldN case LENGTH_VARIABLE: return new CountMethodValueSource(fieldData); default: - throw new IllegalArgumentException("Member variable [" + variable + "] does not exist for " + - "numeric field [" + fieldName + "]."); + throw new IllegalArgumentException( + "Member variable [" + variable + "] does not exist for " + "numeric field [" + fieldName + "]." + ); } } diff --git a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ReplaceableConstDoubleValueSource.java b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ReplaceableConstDoubleValueSource.java index 84b7a45b3123d..50a70fccdcd44 100644 --- a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ReplaceableConstDoubleValueSource.java +++ b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ReplaceableConstDoubleValueSource.java @@ -38,10 +38,8 @@ public boolean needsScores() { @Override public Explanation explain(LeafReaderContext ctx, int docId, Explanation scoreExplanation) throws IOException { - if (fv.advanceExact(docId)) - return Explanation.match((float)fv.doubleValue(), "ReplaceableConstDoubleValues"); - else - return Explanation.noMatch("ReplaceableConstDoubleValues"); + if (fv.advanceExact(docId)) return Explanation.match((float) fv.doubleValue(), "ReplaceableConstDoubleValues"); + else return Explanation.noMatch("ReplaceableConstDoubleValues"); } @Override diff --git a/modules/lang-expression/src/test/java/org/elasticsearch/script/expression/ExpressionFieldScriptTests.java b/modules/lang-expression/src/test/java/org/elasticsearch/script/expression/ExpressionFieldScriptTests.java index afb5566044182..0615bfe0ff566 100644 --- a/modules/lang-expression/src/test/java/org/elasticsearch/script/expression/ExpressionFieldScriptTests.java +++ b/modules/lang-expression/src/test/java/org/elasticsearch/script/expression/ExpressionFieldScriptTests.java @@ -58,16 +58,12 @@ private FieldScript.LeafFactory compile(String expression) { } public void testCompileError() { - ScriptException e = expectThrows(ScriptException.class, () -> { - compile("doc['field'].value * *@#)(@$*@#$ + 4"); - }); + ScriptException e = expectThrows(ScriptException.class, () -> { compile("doc['field'].value * *@#)(@$*@#$ + 4"); }); assertTrue(e.getCause() instanceof ParseException); } public void testLinkError() { - ScriptException e = expectThrows(ScriptException.class, () -> { - compile("doc['nonexistent'].value * 5"); - }); + ScriptException e = expectThrows(ScriptException.class, () -> { compile("doc['nonexistent'].value * 5"); }); assertTrue(e.getCause() instanceof ParseException); } diff --git a/modules/lang-expression/src/test/java/org/elasticsearch/script/expression/ExpressionNumberSortScriptTests.java b/modules/lang-expression/src/test/java/org/elasticsearch/script/expression/ExpressionNumberSortScriptTests.java index 40dbfafd89d97..83af54da7f288 100644 --- a/modules/lang-expression/src/test/java/org/elasticsearch/script/expression/ExpressionNumberSortScriptTests.java +++ b/modules/lang-expression/src/test/java/org/elasticsearch/script/expression/ExpressionNumberSortScriptTests.java @@ -54,22 +54,17 @@ public void setUp() throws Exception { } private NumberSortScript.LeafFactory compile(String expression) { - NumberSortScript.Factory factory = - service.compile(null, expression, NumberSortScript.CONTEXT, Collections.emptyMap()); + NumberSortScript.Factory factory = service.compile(null, expression, NumberSortScript.CONTEXT, Collections.emptyMap()); return factory.newFactory(Collections.emptyMap(), lookup); } public void testCompileError() { - ScriptException e = expectThrows(ScriptException.class, () -> { - compile("doc['field'].value * *@#)(@$*@#$ + 4"); - }); + ScriptException e = expectThrows(ScriptException.class, () -> { compile("doc['field'].value * *@#)(@$*@#$ + 4"); }); assertTrue(e.getCause() instanceof ParseException); } public void testLinkError() { - ScriptException e = expectThrows(ScriptException.class, () -> { - compile("doc['nonexistent'].value * 5"); - }); + ScriptException e = expectThrows(ScriptException.class, () -> { compile("doc['nonexistent'].value * 5"); }); assertTrue(e.getCause() instanceof ParseException); } diff --git a/modules/lang-expression/src/test/java/org/elasticsearch/script/expression/ExpressionTermsSetQueryTests.java b/modules/lang-expression/src/test/java/org/elasticsearch/script/expression/ExpressionTermsSetQueryTests.java index eab71e190ce20..b6dd7d3d82b16 100644 --- a/modules/lang-expression/src/test/java/org/elasticsearch/script/expression/ExpressionTermsSetQueryTests.java +++ b/modules/lang-expression/src/test/java/org/elasticsearch/script/expression/ExpressionTermsSetQueryTests.java @@ -49,27 +49,21 @@ public void setUp() throws Exception { when(fieldData.load(anyObject())).thenReturn(atomicFieldData); service = new ExpressionScriptEngine(); - lookup = new SearchLookup(field -> field.equals("field") ? fieldType : null, - (ignored, lookup) -> fieldData); + lookup = new SearchLookup(field -> field.equals("field") ? fieldType : null, (ignored, lookup) -> fieldData); } private TermsSetQueryScript.LeafFactory compile(String expression) { - TermsSetQueryScript.Factory factory = - service.compile(null, expression, TermsSetQueryScript.CONTEXT, Collections.emptyMap()); + TermsSetQueryScript.Factory factory = service.compile(null, expression, TermsSetQueryScript.CONTEXT, Collections.emptyMap()); return factory.newFactory(Collections.emptyMap(), lookup); } public void testCompileError() { - ScriptException e = expectThrows(ScriptException.class, () -> { - compile("doc['field'].value * *@#)(@$*@#$ + 4"); - }); + ScriptException e = expectThrows(ScriptException.class, () -> { compile("doc['field'].value * *@#)(@$*@#$ + 4"); }); assertTrue(e.getCause() instanceof ParseException); } public void testLinkError() { - ScriptException e = expectThrows(ScriptException.class, () -> { - compile("doc['nonexistent'].value * 5"); - }); + ScriptException e = expectThrows(ScriptException.class, () -> { compile("doc['nonexistent'].value * 5"); }); assertTrue(e.getCause() instanceof ParseException); } diff --git a/modules/lang-expression/src/yamlRestTest/java/org/elasticsearch/script/expression/LangExpressionClientYamlTestSuiteIT.java b/modules/lang-expression/src/yamlRestTest/java/org/elasticsearch/script/expression/LangExpressionClientYamlTestSuiteIT.java index 6ff34f220c5ac..dcbba01564295 100644 --- a/modules/lang-expression/src/yamlRestTest/java/org/elasticsearch/script/expression/LangExpressionClientYamlTestSuiteIT.java +++ b/modules/lang-expression/src/yamlRestTest/java/org/elasticsearch/script/expression/LangExpressionClientYamlTestSuiteIT.java @@ -25,4 +25,3 @@ public static Iterable parameters() throws Exception { return ESClientYamlSuiteTestCase.createParameters(); } } - diff --git a/modules/lang-mustache/src/internalClusterTest/java/org/elasticsearch/script/mustache/MultiSearchTemplateIT.java b/modules/lang-mustache/src/internalClusterTest/java/org/elasticsearch/script/mustache/MultiSearchTemplateIT.java index 700d3c38214a8..112d9d33cbb69 100644 --- a/modules/lang-mustache/src/internalClusterTest/java/org/elasticsearch/script/mustache/MultiSearchTemplateIT.java +++ b/modules/lang-mustache/src/internalClusterTest/java/org/elasticsearch/script/mustache/MultiSearchTemplateIT.java @@ -22,8 +22,8 @@ import java.util.HashMap; import java.util.Map; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.Matchers.arrayWithSize; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.greaterThan; @@ -42,18 +42,21 @@ public void testBasic() throws Exception { final int numDocs = randomIntBetween(10, 100); IndexRequestBuilder[] indexRequestBuilders = new IndexRequestBuilder[numDocs]; for (int i = 0; i < numDocs; i++) { - indexRequestBuilders[i] = client().prepareIndex("msearch").setId(String.valueOf(i)) - .setSource("odd", (i % 2 == 0), "group", (i % 3)); + indexRequestBuilders[i] = client().prepareIndex("msearch") + .setId(String.valueOf(i)) + .setSource("odd", (i % 2 == 0), "group", (i % 3)); } indexRandom(true, indexRequestBuilders); - final String template = Strings.toString(jsonBuilder().startObject() - .startObject("query") - .startObject("{{query_type}}") - .field("{{field_name}}", "{{field_value}}") - .endObject() - .endObject() - .endObject()); + final String template = Strings.toString( + jsonBuilder().startObject() + .startObject("query") + .startObject("{{query_type}}") + .field("{{field_name}}", "{{field_value}}") + .endObject() + .endObject() + .endObject() + ); MultiSearchTemplateRequest multiRequest = new MultiSearchTemplateRequest(); @@ -132,23 +135,23 @@ public void testBasic() throws Exception { SearchTemplateResponse searchTemplateResponse1 = response1.getResponse(); assertThat(searchTemplateResponse1.hasResponse(), is(true)); assertHitCount(searchTemplateResponse1.getResponse(), (numDocs / 2) + (numDocs % 2)); - assertThat(searchTemplateResponse1.getSource().utf8ToString(), - equalTo("{\"query\":{\"match\":{\"odd\":\"true\"}}}")); + assertThat(searchTemplateResponse1.getSource().utf8ToString(), equalTo("{\"query\":{\"match\":{\"odd\":\"true\"}}}")); MultiSearchTemplateResponse.Item response2 = response.getResponses()[1]; assertThat(response2.isFailure(), is(false)); SearchTemplateResponse searchTemplateResponse2 = response2.getResponse(); assertThat(searchTemplateResponse2.hasResponse(), is(false)); - assertThat(searchTemplateResponse2.getSource().utf8ToString(), - equalTo("{\"query\":{\"match_phrase_prefix\":{\"message\":\"quick brown f\"}}}")); + assertThat( + searchTemplateResponse2.getSource().utf8ToString(), + equalTo("{\"query\":{\"match_phrase_prefix\":{\"message\":\"quick brown f\"}}}") + ); MultiSearchTemplateResponse.Item response3 = response.getResponses()[2]; assertThat(response3.isFailure(), is(false)); SearchTemplateResponse searchTemplateResponse3 = response3.getResponse(); assertThat(searchTemplateResponse3.hasResponse(), is(true)); assertHitCount(searchTemplateResponse3.getResponse(), (numDocs / 2)); - assertThat(searchTemplateResponse3.getSource().utf8ToString(), - equalTo("{\"query\":{\"term\":{\"odd\":\"false\"}}}")); + assertThat(searchTemplateResponse3.getSource().utf8ToString(), equalTo("{\"query\":{\"term\":{\"odd\":\"false\"}}}")); MultiSearchTemplateResponse.Item response4 = response.getResponses()[3]; assertThat(response4.isFailure(), is(true)); @@ -159,7 +162,6 @@ public void testBasic() throws Exception { assertThat(response5.isFailure(), is(false)); SearchTemplateResponse searchTemplateResponse5 = response5.getResponse(); assertThat(searchTemplateResponse5.hasResponse(), is(false)); - assertThat(searchTemplateResponse5.getSource().utf8ToString(), - equalTo("{\"query\":{\"terms\":{\"group\":[1,2,3,]}}}")); + assertThat(searchTemplateResponse5.getSource().utf8ToString(), equalTo("{\"query\":{\"terms\":{\"group\":[1,2,3,]}}}")); } } diff --git a/modules/lang-mustache/src/internalClusterTest/java/org/elasticsearch/script/mustache/SearchTemplateIT.java b/modules/lang-mustache/src/internalClusterTest/java/org/elasticsearch/script/mustache/SearchTemplateIT.java index e24aabff21c0d..3163408b21181 100644 --- a/modules/lang-mustache/src/internalClusterTest/java/org/elasticsearch/script/mustache/SearchTemplateIT.java +++ b/modules/lang-mustache/src/internalClusterTest/java/org/elasticsearch/script/mustache/SearchTemplateIT.java @@ -12,11 +12,11 @@ import org.elasticsearch.action.bulk.BulkRequestBuilder; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.common.bytes.BytesArray; -import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.script.ScriptType; import org.elasticsearch.test.ESSingleNodeTestCase; +import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.xcontent.json.JsonXContent; import org.junit.Before; import java.io.IOException; @@ -25,9 +25,9 @@ import java.util.HashMap; import java.util.Map; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; @@ -44,12 +44,8 @@ protected Collection> getPlugins() { @Before public void setup() throws IOException { createIndex("test"); - client().prepareIndex("test").setId("1") - .setSource(jsonBuilder().startObject().field("text", "value1").endObject()) - .get(); - client().prepareIndex("test").setId("2") - .setSource(jsonBuilder().startObject().field("text", "value2").endObject()) - .get(); + client().prepareIndex("test").setId("1").setSource(jsonBuilder().startObject().field("text", "value1").endObject()).get(); + client().prepareIndex("test").setId("2").setSource(jsonBuilder().startObject().field("text", "value2").endObject()).get(); client().admin().indices().prepareRefresh().get(); } @@ -60,19 +56,20 @@ public void testSearchRequestFail() throws Exception { SearchRequest searchRequest = new SearchRequest(); searchRequest.indices("_all"); - expectThrows(Exception.class, () -> new SearchTemplateRequestBuilder(client()) - .setRequest(searchRequest) + expectThrows( + Exception.class, + () -> new SearchTemplateRequestBuilder(client()).setRequest(searchRequest) .setScript(query) .setScriptType(ScriptType.INLINE) .setScriptParams(randomBoolean() ? null : Collections.emptyMap()) - .get()); + .get() + ); - SearchTemplateResponse searchResponse = new SearchTemplateRequestBuilder(client()) - .setRequest(searchRequest) - .setScript(query) - .setScriptType(ScriptType.INLINE) - .setScriptParams(Collections.singletonMap("my_size", 1)) - .get(); + SearchTemplateResponse searchResponse = new SearchTemplateRequestBuilder(client()).setRequest(searchRequest) + .setScript(query) + .setScriptType(ScriptType.INLINE) + .setScriptParams(Collections.singletonMap("my_size", 1)) + .get(); assertThat(searchResponse.getResponse().getHits().getHits().length, equalTo(1)); } @@ -83,12 +80,12 @@ public void testSearchRequestFail() throws Exception { public void testTemplateQueryAsEscapedString() throws Exception { SearchRequest searchRequest = new SearchRequest(); searchRequest.indices("_all"); - String query = - "{" + " \"source\" : \"{ \\\"size\\\": \\\"{{size}}\\\", \\\"query\\\":{\\\"match_all\\\":{}}}\"," - + " \"params\":{" - + " \"size\": 1" - + " }" - + "}"; + String query = "{" + + " \"source\" : \"{ \\\"size\\\": \\\"{{size}}\\\", \\\"query\\\":{\\\"match_all\\\":{}}}\"," + + " \"params\":{" + + " \"size\": 1" + + " }" + + "}"; SearchTemplateRequest request = SearchTemplateRequest.fromXContent(createParser(JsonXContent.jsonXContent, query)); request.setRequest(searchRequest); SearchTemplateResponse searchResponse = client().execute(SearchTemplateAction.INSTANCE, request).get(); @@ -102,14 +99,13 @@ public void testTemplateQueryAsEscapedString() throws Exception { public void testTemplateQueryAsEscapedStringStartingWithConditionalClause() throws Exception { SearchRequest searchRequest = new SearchRequest(); searchRequest.indices("_all"); - String templateString = - "{" - + " \"source\" : \"{ {{#use_size}} \\\"size\\\": \\\"{{size}}\\\", {{/use_size}} \\\"query\\\":{\\\"match_all\\\":{}}}\"," - + " \"params\":{" - + " \"size\": 1," - + " \"use_size\": true" - + " }" - + "}"; + String templateString = "{" + + " \"source\" : \"{ {{#use_size}} \\\"size\\\": \\\"{{size}}\\\", {{/use_size}} \\\"query\\\":{\\\"match_all\\\":{}}}\"," + + " \"params\":{" + + " \"size\": 1," + + " \"use_size\": true" + + " }" + + "}"; SearchTemplateRequest request = SearchTemplateRequest.fromXContent(createParser(JsonXContent.jsonXContent, templateString)); request.setRequest(searchRequest); SearchTemplateResponse searchResponse = client().execute(SearchTemplateAction.INSTANCE, request).get(); @@ -123,14 +119,13 @@ public void testTemplateQueryAsEscapedStringStartingWithConditionalClause() thro public void testTemplateQueryAsEscapedStringWithConditionalClauseAtEnd() throws Exception { SearchRequest searchRequest = new SearchRequest(); searchRequest.indices("_all"); - String templateString = - "{" - + " \"source\" : \"{ \\\"query\\\":{\\\"match_all\\\":{}} {{#use_size}}, \\\"size\\\": \\\"{{size}}\\\" {{/use_size}} }\"," - + " \"params\":{" - + " \"size\": 1," - + " \"use_size\": true" - + " }" - + "}"; + String templateString = "{" + + " \"source\" : \"{ \\\"query\\\":{\\\"match_all\\\":{}} {{#use_size}}, \\\"size\\\": \\\"{{size}}\\\" {{/use_size}} }\"," + + " \"params\":{" + + " \"size\": 1," + + " \"use_size\": true" + + " }" + + "}"; SearchTemplateRequest request = SearchTemplateRequest.fromXContent(createParser(JsonXContent.jsonXContent, templateString)); request.setRequest(searchRequest); SearchTemplateResponse searchResponse = client().execute(SearchTemplateAction.INSTANCE, request).get(); @@ -138,27 +133,31 @@ public void testTemplateQueryAsEscapedStringWithConditionalClauseAtEnd() throws } public void testIndexedTemplateClient() throws Exception { - assertAcked(client().admin().cluster().preparePutStoredScript() + assertAcked( + client().admin() + .cluster() + .preparePutStoredScript() .setId("testTemplate") .setContent( new BytesArray( - "{" + - " \"script\": {" + - " \"lang\": \"mustache\"," + - " \"source\": {" + - " \"query\": {" + - " \"match\": {" + - " \"theField\": \"{{fieldParam}}\"" + - " }" + - " }" + - " }" + - " }" + - "}" + "{" + + " \"script\": {" + + " \"lang\": \"mustache\"," + + " \"source\": {" + + " \"query\": {" + + " \"match\": {" + + " \"theField\": \"{{fieldParam}}\"" + + " }" + + " }" + + " }" + + " }" + + "}" ), - XContentType.JSON)); + XContentType.JSON + ) + ); - GetStoredScriptResponse getResponse = client().admin().cluster() - .prepareGetStoredScript("testTemplate").get(); + GetStoredScriptResponse getResponse = client().admin().cluster().prepareGetStoredScript("testTemplate").get(); assertNotNull(getResponse.getSource()); BulkRequestBuilder bulkRequestBuilder = client().prepareBulk(); @@ -173,10 +172,11 @@ public void testIndexedTemplateClient() throws Exception { Map templateParams = new HashMap<>(); templateParams.put("fieldParam", "foo"); - SearchTemplateResponse searchResponse = new SearchTemplateRequestBuilder(client()) - .setRequest(new SearchRequest("test")) - .setScript("testTemplate").setScriptType(ScriptType.STORED).setScriptParams(templateParams) - .get(); + SearchTemplateResponse searchResponse = new SearchTemplateRequestBuilder(client()).setRequest(new SearchRequest("test")) + .setScript("testTemplate") + .setScriptType(ScriptType.STORED) + .setScriptParams(templateParams) + .get(); assertHitCount(searchResponse.getResponse(), 4); assertAcked(client().admin().cluster().prepareDeleteStoredScript("testTemplate")); @@ -187,29 +187,22 @@ public void testIndexedTemplateClient() throws Exception { public void testIndexedTemplate() throws Exception { - String script = - "{" + - " \"script\": {" + - " \"lang\": \"mustache\"," + - " \"source\": {" + - " \"query\": {" + - " \"match\": {" + - " \"theField\": \"{{fieldParam}}\"" + - " }" + - " }" + - " }" + - " }" + - "}"; - - assertAcked( - client().admin().cluster().preparePutStoredScript().setId("1a").setContent(new BytesArray(script), XContentType.JSON) - ); - assertAcked( - client().admin().cluster().preparePutStoredScript().setId("2").setContent(new BytesArray(script), XContentType.JSON) - ); - assertAcked( - client().admin().cluster().preparePutStoredScript().setId("3").setContent(new BytesArray(script), XContentType.JSON) - ); + String script = "{" + + " \"script\": {" + + " \"lang\": \"mustache\"," + + " \"source\": {" + + " \"query\": {" + + " \"match\": {" + + " \"theField\": \"{{fieldParam}}\"" + + " }" + + " }" + + " }" + + " }" + + "}"; + + assertAcked(client().admin().cluster().preparePutStoredScript().setId("1a").setContent(new BytesArray(script), XContentType.JSON)); + assertAcked(client().admin().cluster().preparePutStoredScript().setId("2").setContent(new BytesArray(script), XContentType.JSON)); + assertAcked(client().admin().cluster().preparePutStoredScript().setId("3").setContent(new BytesArray(script), XContentType.JSON)); BulkRequestBuilder bulkRequestBuilder = client().prepareBulk(); bulkRequestBuilder.add(client().prepareIndex("test").setId("1").setSource("{\"theField\":\"foo\"}", XContentType.JSON)); @@ -223,26 +216,28 @@ public void testIndexedTemplate() throws Exception { Map templateParams = new HashMap<>(); templateParams.put("fieldParam", "foo"); - SearchTemplateResponse searchResponse = new SearchTemplateRequestBuilder(client()) - .setRequest(new SearchRequest().indices("test")) - .setScript("1a") - .setScriptType(ScriptType.STORED) - .setScriptParams(templateParams) - .get(); + SearchTemplateResponse searchResponse = new SearchTemplateRequestBuilder(client()).setRequest(new SearchRequest().indices("test")) + .setScript("1a") + .setScriptType(ScriptType.STORED) + .setScriptParams(templateParams) + .get(); assertHitCount(searchResponse.getResponse(), 4); - expectThrows(ResourceNotFoundException.class, () -> new SearchTemplateRequestBuilder(client()) - .setRequest(new SearchRequest().indices("test")) + expectThrows( + ResourceNotFoundException.class, + () -> new SearchTemplateRequestBuilder(client()).setRequest(new SearchRequest().indices("test")) .setScript("1000") .setScriptType(ScriptType.STORED) .setScriptParams(templateParams) - .get()); + .get() + ); templateParams.put("fieldParam", "bar"); - searchResponse = new SearchTemplateRequestBuilder(client()) - .setRequest(new SearchRequest("test")) - .setScript("2").setScriptType(ScriptType.STORED).setScriptParams(templateParams) - .get(); + searchResponse = new SearchTemplateRequestBuilder(client()).setRequest(new SearchRequest("test")) + .setScript("2") + .setScriptType(ScriptType.STORED) + .setScriptParams(templateParams) + .get(); assertHitCount(searchResponse.getResponse(), 1); } @@ -251,30 +246,30 @@ public void testIndexedTemplateOverwrite() throws Exception { createIndex("testindex"); ensureGreen("testindex"); - client().prepareIndex("testindex").setId("1") - .setSource(jsonBuilder().startObject().field("searchtext", "dev1").endObject()) - .get(); + client().prepareIndex("testindex").setId("1").setSource(jsonBuilder().startObject().field("searchtext", "dev1").endObject()).get(); client().admin().indices().prepareRefresh().get(); int iterations = randomIntBetween(2, 11); - String query = - "{" + - " \"script\": {" + - " \"lang\": \"mustache\"," + - " \"source\": {" + - " \"query\": {" + - " \"match_phrase_prefix\": {" + - " \"searchtext\": {" + - " \"query\": \"{{P_Keyword1}}\"," + - " \"slop\": {{slop}}" + - " }" + - " }" + - " }" + - " }" + - " }" + - "}"; + String query = "{" + + " \"script\": {" + + " \"lang\": \"mustache\"," + + " \"source\": {" + + " \"query\": {" + + " \"match_phrase_prefix\": {" + + " \"searchtext\": {" + + " \"query\": \"{{P_Keyword1}}\"," + + " \"slop\": {{slop}}" + + " }" + + " }" + + " }" + + " }" + + " }" + + "}"; for (int i = 1; i < iterations; i++) { - assertAcked(client().admin().cluster().preparePutStoredScript() + assertAcked( + client().admin() + .cluster() + .preparePutStoredScript() .setId("git01") .setContent(new BytesArray(query.replace("{{slop}}", Integer.toString(-1))), XContentType.JSON) ); @@ -285,47 +280,52 @@ public void testIndexedTemplateOverwrite() throws Exception { Map templateParams = new HashMap<>(); templateParams.put("P_Keyword1", "dev"); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> new SearchTemplateRequestBuilder(client()) - .setRequest(new SearchRequest("testindex")) - .setScript("git01").setScriptType(ScriptType.STORED).setScriptParams(templateParams) - .get()); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> new SearchTemplateRequestBuilder(client()).setRequest(new SearchRequest("testindex")) + .setScript("git01") + .setScriptType(ScriptType.STORED) + .setScriptParams(templateParams) + .get() + ); assertThat(e.getMessage(), containsString("No negative slop allowed")); - assertAcked(client().admin().cluster().preparePutStoredScript() + assertAcked( + client().admin() + .cluster() + .preparePutStoredScript() .setId("git01") .setContent(new BytesArray(query.replace("{{slop}}", Integer.toString(0))), XContentType.JSON) ); - SearchTemplateResponse searchResponse = new SearchTemplateRequestBuilder(client()) - .setRequest(new SearchRequest("testindex")) - .setScript("git01").setScriptType(ScriptType.STORED).setScriptParams(templateParams) - .get(); + SearchTemplateResponse searchResponse = new SearchTemplateRequestBuilder(client()).setRequest(new SearchRequest("testindex")) + .setScript("git01") + .setScriptType(ScriptType.STORED) + .setScriptParams(templateParams) + .get(); assertHitCount(searchResponse.getResponse(), 1); } } public void testIndexedTemplateWithArray() throws Exception { - String multiQuery = - "{\n" + - " \"script\": {\n" + - " \"lang\": \"mustache\",\n" + - " \"source\": {\n" + - " \"query\": {\n" + - " \"terms\": {\n" + - " \"theField\": [\n" + - " \"{{#fieldParam}}\",\n" + - " \"{{.}}\",\n" + - " \"{{/fieldParam}}\"\n" + - " ]\n" + - " }\n" + - " }\n" + - " }\n" + - " }\n" + - "}"; + String multiQuery = "{\n" + + " \"script\": {\n" + + " \"lang\": \"mustache\",\n" + + " \"source\": {\n" + + " \"query\": {\n" + + " \"terms\": {\n" + + " \"theField\": [\n" + + " \"{{#fieldParam}}\",\n" + + " \"{{.}}\",\n" + + " \"{{/fieldParam}}\"\n" + + " ]\n" + + " }\n" + + " }\n" + + " }\n" + + " }\n" + + "}"; assertAcked( - client().admin().cluster().preparePutStoredScript() - .setId("4") - .setContent(new BytesArray(multiQuery), XContentType.JSON) + client().admin().cluster().preparePutStoredScript().setId("4").setContent(new BytesArray(multiQuery), XContentType.JSON) ); BulkRequestBuilder bulkRequestBuilder = client().prepareBulk(); bulkRequestBuilder.add(client().prepareIndex("test").setId("1").setSource("{\"theField\":\"foo\"}", XContentType.JSON)); @@ -337,13 +337,14 @@ public void testIndexedTemplateWithArray() throws Exception { client().admin().indices().prepareRefresh().get(); Map arrayTemplateParams = new HashMap<>(); - String[] fieldParams = {"foo", "bar"}; + String[] fieldParams = { "foo", "bar" }; arrayTemplateParams.put("fieldParam", fieldParams); - SearchTemplateResponse searchResponse = new SearchTemplateRequestBuilder(client()) - .setRequest(new SearchRequest("test")) - .setScript("4").setScriptType(ScriptType.STORED).setScriptParams(arrayTemplateParams) - .get(); + SearchTemplateResponse searchResponse = new SearchTemplateRequestBuilder(client()).setRequest(new SearchRequest("test")) + .setScript("4") + .setScriptType(ScriptType.STORED) + .setScriptParams(arrayTemplateParams) + .get(); assertHitCount(searchResponse.getResponse(), 5); } diff --git a/modules/lang-mustache/src/javaRestTest/java/org/elasticsearch/script/mustache/SearchTemplateWithoutContentIT.java b/modules/lang-mustache/src/javaRestTest/java/org/elasticsearch/script/mustache/SearchTemplateWithoutContentIT.java index 98fbddccad733..6188b6b2d1537 100644 --- a/modules/lang-mustache/src/javaRestTest/java/org/elasticsearch/script/mustache/SearchTemplateWithoutContentIT.java +++ b/modules/lang-mustache/src/javaRestTest/java/org/elasticsearch/script/mustache/SearchTemplateWithoutContentIT.java @@ -19,15 +19,19 @@ public class SearchTemplateWithoutContentIT extends ESRestTestCase { public void testSearchTemplateMissingBody() throws IOException { - ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest( - new Request(randomBoolean() ? "POST" : "GET", "/_search/template"))); + ResponseException responseException = expectThrows( + ResponseException.class, + () -> client().performRequest(new Request(randomBoolean() ? "POST" : "GET", "/_search/template")) + ); assertEquals(400, responseException.getResponse().getStatusLine().getStatusCode()); assertThat(responseException.getMessage(), containsString("request body or source parameter is required")); } public void testMultiSearchTemplateMissingBody() throws IOException { - ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest( - new Request(randomBoolean() ? "POST" : "GET", "/_msearch/template"))); + ResponseException responseException = expectThrows( + ResponseException.class, + () -> client().performRequest(new Request(randomBoolean() ? "POST" : "GET", "/_msearch/template")) + ); assertEquals(400, responseException.getResponse().getStatusLine().getStatusCode()); assertThat(responseException.getMessage(), containsString("request body or source parameter is required")); } diff --git a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/CustomMustacheFactory.java b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/CustomMustacheFactory.java index c2938cc6c7334..e013ffcdc4d6c 100644 --- a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/CustomMustacheFactory.java +++ b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/CustomMustacheFactory.java @@ -19,6 +19,7 @@ import com.github.mustachejava.codes.DefaultMustache; import com.github.mustachejava.codes.IterableCode; import com.github.mustachejava.codes.WriteCode; + import org.elasticsearch.common.Strings; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentType; @@ -48,11 +49,17 @@ public class CustomMustacheFactory extends DefaultMustacheFactory { private static final String DEFAULT_MEDIA_TYPE = JSON_MEDIA_TYPE; private static final Map> ENCODERS = Map.of( - V7_JSON_MEDIA_TYPE_WITH_CHARSET, JsonEscapeEncoder::new, - JSON_MEDIA_TYPE_WITH_CHARSET, JsonEscapeEncoder::new, - JSON_MEDIA_TYPE, JsonEscapeEncoder::new, - PLAIN_TEXT_MEDIA_TYPE, DefaultEncoder::new, - X_WWW_FORM_URLENCODED_MEDIA_TYPE, UrlEncoder::new); + V7_JSON_MEDIA_TYPE_WITH_CHARSET, + JsonEscapeEncoder::new, + JSON_MEDIA_TYPE_WITH_CHARSET, + JsonEscapeEncoder::new, + JSON_MEDIA_TYPE, + JsonEscapeEncoder::new, + PLAIN_TEXT_MEDIA_TYPE, + DefaultEncoder::new, + X_WWW_FORM_URLENCODED_MEDIA_TYPE, + UrlEncoder::new + ); private final Encoder encoder; diff --git a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/CustomReflectionObjectHandler.java b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/CustomReflectionObjectHandler.java index c221da40ba690..d00a737235ee5 100644 --- a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/CustomReflectionObjectHandler.java +++ b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/CustomReflectionObjectHandler.java @@ -9,16 +9,17 @@ package org.elasticsearch.script.mustache; import com.github.mustachejava.reflect.ReflectionObjectHandler; + import org.elasticsearch.common.util.CollectionUtils; import org.elasticsearch.common.util.iterable.Iterables; import java.lang.reflect.Array; import java.util.AbstractMap; import java.util.Collection; -import java.util.Set; +import java.util.HashMap; import java.util.Iterator; import java.util.Map; -import java.util.HashMap; +import java.util.Set; final class CustomReflectionObjectHandler extends ReflectionObjectHandler { diff --git a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/MultiSearchTemplateRequest.java b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/MultiSearchTemplateRequest.java index 809c0157eaa3e..b24198f10f1f3 100644 --- a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/MultiSearchTemplateRequest.java +++ b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/MultiSearchTemplateRequest.java @@ -124,9 +124,9 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; MultiSearchTemplateRequest that = (MultiSearchTemplateRequest) o; - return maxConcurrentSearchRequests == that.maxConcurrentSearchRequests && - Objects.equals(requests, that.requests) && - Objects.equals(indicesOptions, that.indicesOptions); + return maxConcurrentSearchRequests == that.maxConcurrentSearchRequests + && Objects.equals(requests, that.requests) + && Objects.equals(indicesOptions, that.indicesOptions); } @Override @@ -134,8 +134,7 @@ public int hashCode() { return Objects.hash(maxConcurrentSearchRequests, requests, indicesOptions); } - public static byte[] writeMultiLineFormat(MultiSearchTemplateRequest multiSearchTemplateRequest, - XContent xContent) throws IOException { + public static byte[] writeMultiLineFormat(MultiSearchTemplateRequest multiSearchTemplateRequest, XContent xContent) throws IOException { ByteArrayOutputStream output = new ByteArrayOutputStream(); for (SearchTemplateRequest templateRequest : multiSearchTemplateRequest.requests()) { final SearchRequest searchRequest = templateRequest.getRequest(); diff --git a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/MultiSearchTemplateResponse.java b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/MultiSearchTemplateResponse.java index c2f7345e05bb5..3dff5b0f4a853 100644 --- a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/MultiSearchTemplateResponse.java +++ b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/MultiSearchTemplateResponse.java @@ -12,11 +12,11 @@ import org.elasticsearch.Version; import org.elasticsearch.action.ActionResponse; import org.elasticsearch.action.search.MultiSearchResponse; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; @@ -164,14 +164,14 @@ static final class Fields { } public static MultiSearchTemplateResponse fromXContext(XContentParser parser) { - //The MultiSearchTemplateResponse is identical to the multi search response so we reuse the parsing logic in multi search response + // The MultiSearchTemplateResponse is identical to the multi search response so we reuse the parsing logic in multi search response MultiSearchResponse mSearchResponse = MultiSearchResponse.fromXContext(parser); org.elasticsearch.action.search.MultiSearchResponse.Item[] responses = mSearchResponse.getResponses(); Item[] templateResponses = new Item[responses.length]; int i = 0; for (org.elasticsearch.action.search.MultiSearchResponse.Item item : responses) { SearchTemplateResponse stResponse = null; - if(item.getResponse() != null){ + if (item.getResponse() != null) { stResponse = new SearchTemplateResponse(); stResponse.setResponse(item.getResponse()); } diff --git a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/MustachePlugin.java b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/MustachePlugin.java index 22cf911d945de..0f79e44464eea 100644 --- a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/MustachePlugin.java +++ b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/MustachePlugin.java @@ -33,23 +33,32 @@ public class MustachePlugin extends Plugin implements ScriptPlugin, ActionPlugin, SearchPlugin { @Override - public ScriptEngine getScriptEngine(Settings settings, Collection>contexts) { + public ScriptEngine getScriptEngine(Settings settings, Collection> contexts) { return new MustacheScriptEngine(); } @Override public List> getActions() { - return Arrays.asList(new ActionHandler<>(SearchTemplateAction.INSTANCE, TransportSearchTemplateAction.class), - new ActionHandler<>(MultiSearchTemplateAction.INSTANCE, TransportMultiSearchTemplateAction.class)); + return Arrays.asList( + new ActionHandler<>(SearchTemplateAction.INSTANCE, TransportSearchTemplateAction.class), + new ActionHandler<>(MultiSearchTemplateAction.INSTANCE, TransportMultiSearchTemplateAction.class) + ); } @Override - public List getRestHandlers(Settings settings, RestController restController, ClusterSettings clusterSettings, - IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter, IndexNameExpressionResolver indexNameExpressionResolver, - Supplier nodesInCluster) { + public List getRestHandlers( + Settings settings, + RestController restController, + ClusterSettings clusterSettings, + IndexScopedSettings indexScopedSettings, + SettingsFilter settingsFilter, + IndexNameExpressionResolver indexNameExpressionResolver, + Supplier nodesInCluster + ) { return Arrays.asList( - new RestSearchTemplateAction(), - new RestMultiSearchTemplateAction(settings), - new RestRenderSearchTemplateAction()); + new RestSearchTemplateAction(), + new RestMultiSearchTemplateAction(settings), + new RestRenderSearchTemplateAction() + ); } } diff --git a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/MustacheScriptEngine.java b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/MustacheScriptEngine.java index 659d385ec2b47..2e441a3c4dce0 100644 --- a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/MustacheScriptEngine.java +++ b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/MustacheScriptEngine.java @@ -10,6 +10,7 @@ import com.github.mustachejava.Mustache; import com.github.mustachejava.MustacheException; import com.github.mustachejava.MustacheFactory; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.message.ParameterizedMessage; @@ -52,12 +53,7 @@ public final class MustacheScriptEngine implements ScriptEngine { * @return a compiled template object for later execution. * */ @Override - public T compile( - String templateName, - String templateSource, - ScriptContext context, - Map options - ) { + public T compile(String templateName, String templateSource, ScriptContext context, Map options) { if (context.instanceClazz.equals(TemplateScript.class) == false) { throw new IllegalArgumentException("mustache engine does not know how to handle context [" + context.name + "]"); } diff --git a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/RestMultiSearchTemplateAction.java b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/RestMultiSearchTemplateAction.java index 20f7163c2ae8f..22d17084c0114 100644 --- a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/RestMultiSearchTemplateAction.java +++ b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/RestMultiSearchTemplateAction.java @@ -9,8 +9,8 @@ package org.elasticsearch.script.mustache; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestToXContentListener; @@ -39,7 +39,6 @@ public class RestMultiSearchTemplateAction extends BaseRestHandler { RESPONSE_PARAMS = Collections.unmodifiableSet(responseParams); } - private final boolean allowExplicitIndex; public RestMultiSearchTemplateAction(Settings settings) { @@ -53,12 +52,9 @@ public List routes() { new Route(POST, "/_msearch/template"), new Route(GET, "/{index}/_msearch/template"), new Route(POST, "/{index}/_msearch/template"), - Route.builder(GET, "/{index}/{type}/_msearch/template") - .deprecated(TYPES_DEPRECATION_MESSAGE, RestApiVersion.V_7) - .build(), - Route.builder(POST, "/{index}/{type}/_msearch/template") - .deprecated(TYPES_DEPRECATION_MESSAGE, RestApiVersion.V_7) - .build()); + Route.builder(GET, "/{index}/{type}/_msearch/template").deprecated(TYPES_DEPRECATION_MESSAGE, RestApiVersion.V_7).build(), + Route.builder(POST, "/{index}/{type}/_msearch/template").deprecated(TYPES_DEPRECATION_MESSAGE, RestApiVersion.V_7).build() + ); } @Override @@ -85,17 +81,21 @@ public static MultiSearchTemplateRequest parseRequest(RestRequest restRequest, b multiRequest.maxConcurrentSearchRequests(restRequest.paramAsInt("max_concurrent_searches", 0)); } - RestMultiSearchAction.parseMultiLineRequest(restRequest, multiRequest.indicesOptions(), allowExplicitIndex, - (searchRequest, bytes) -> { - SearchTemplateRequest searchTemplateRequest = SearchTemplateRequest.fromXContent(bytes); - if (searchTemplateRequest.getScript() != null) { - searchTemplateRequest.setRequest(searchRequest); - multiRequest.add(searchTemplateRequest); - } else { - throw new IllegalArgumentException("Malformed search template"); - } - RestSearchAction.checkRestTotalHits(restRequest, searchRequest); - }); + RestMultiSearchAction.parseMultiLineRequest( + restRequest, + multiRequest.indicesOptions(), + allowExplicitIndex, + (searchRequest, bytes) -> { + SearchTemplateRequest searchTemplateRequest = SearchTemplateRequest.fromXContent(bytes); + if (searchTemplateRequest.getScript() != null) { + searchTemplateRequest.setRequest(searchRequest); + multiRequest.add(searchTemplateRequest); + } else { + throw new IllegalArgumentException("Malformed search template"); + } + RestSearchAction.checkRestTotalHits(restRequest, searchRequest); + } + ); return multiRequest; } diff --git a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/RestRenderSearchTemplateAction.java b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/RestRenderSearchTemplateAction.java index b67e1e9a41887..87210c1026511 100644 --- a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/RestRenderSearchTemplateAction.java +++ b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/RestRenderSearchTemplateAction.java @@ -9,11 +9,11 @@ package org.elasticsearch.script.mustache; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestToXContentListener; import org.elasticsearch.script.ScriptType; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.List; @@ -29,7 +29,8 @@ public List routes() { new Route(GET, "/_render/template"), new Route(POST, "/_render/template"), new Route(GET, "/_render/template/{id}"), - new Route(POST, "/_render/template/{id}")); + new Route(POST, "/_render/template/{id}") + ); } @Override diff --git a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/RestSearchTemplateAction.java b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/RestSearchTemplateAction.java index 8ec26e720e3d6..ea56866db1b14 100644 --- a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/RestSearchTemplateAction.java +++ b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/RestSearchTemplateAction.java @@ -11,11 +11,11 @@ import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.core.RestApiVersion; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestStatusToXContentListener; import org.elasticsearch.rest.action.search.RestSearchAction; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.Arrays; @@ -49,7 +49,8 @@ public List routes() { .build(), Route.builder(POST, "/{index}/{type}/_search/template") .deprecated(RestSearchAction.TYPES_DEPRECATION_MESSAGE, RestApiVersion.V_7) - .build()); + .build() + ); } @Override @@ -62,7 +63,12 @@ public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client // Creates the search request with all required params SearchRequest searchRequest = new SearchRequest(); RestSearchAction.parseSearchRequest( - searchRequest, request, null, client.getNamedWriteableRegistry(), size -> searchRequest.source().size(size)); + searchRequest, + request, + null, + client.getNamedWriteableRegistry(), + size -> searchRequest.source().size(size) + ); // Creates the search template request SearchTemplateRequest searchTemplateRequest; diff --git a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/SearchTemplateRequest.java b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/SearchTemplateRequest.java index b0da5a84aae01..bd01100e23b55 100644 --- a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/SearchTemplateRequest.java +++ b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/SearchTemplateRequest.java @@ -12,17 +12,17 @@ import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.CompositeIndicesRequest; import org.elasticsearch.action.search.SearchRequest; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.script.ScriptType; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.script.ScriptType; import java.io.IOException; import java.util.Map; @@ -75,13 +75,13 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; SearchTemplateRequest request1 = (SearchTemplateRequest) o; - return simulate == request1.simulate && - explain == request1.explain && - profile == request1.profile && - Objects.equals(request, request1.request) && - scriptType == request1.scriptType && - Objects.equals(script, request1.script) && - Objects.equals(scriptParams, request1.scriptParams); + return simulate == request1.simulate + && explain == request1.explain + && profile == request1.profile + && Objects.equals(request, request1.request) + && scriptType == request1.scriptType + && Objects.equals(script, request1.script) + && Objects.equals(scriptParams, request1.scriptParams); } @Override @@ -172,9 +172,7 @@ public ActionRequestValidationException validate() { private static final ObjectParser PARSER; static { PARSER = new ObjectParser<>("search_template"); - PARSER.declareField((parser, request, s) -> - request.setScriptParams(parser.map()) - , PARAMS_FIELD, ObjectParser.ValueType.OBJECT); + PARSER.declareField((parser, request, s) -> request.setScriptParams(parser.map()), PARAMS_FIELD, ObjectParser.ValueType.OBJECT); PARSER.declareString((request, s) -> { request.setScriptType(ScriptType.STORED); request.setScript(s); @@ -184,7 +182,7 @@ public ActionRequestValidationException validate() { PARSER.declareField((parser, request, value) -> { request.setScriptType(ScriptType.INLINE); if (parser.currentToken() == XContentParser.Token.START_OBJECT) { - //convert the template to json which is the only supported XContentType (see CustomMustacheFactory#createEncoder) + // convert the template to json which is the only supported XContentType (see CustomMustacheFactory#createEncoder) try (XContentBuilder builder = XContentFactory.jsonBuilder()) { request.setScript(Strings.toString(builder.copyCurrentStructure(parser))); } catch (IOException e) { diff --git a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/SearchTemplateRequestBuilder.java b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/SearchTemplateRequestBuilder.java index 523284729c767..d775d10224f72 100644 --- a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/SearchTemplateRequestBuilder.java +++ b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/SearchTemplateRequestBuilder.java @@ -15,8 +15,7 @@ import java.util.Map; -public class SearchTemplateRequestBuilder - extends ActionRequestBuilder { +public class SearchTemplateRequestBuilder extends ActionRequestBuilder { SearchTemplateRequestBuilder(ElasticsearchClient client, SearchTemplateAction action) { super(client, action, new SearchTemplateRequest()); diff --git a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/SearchTemplateResponse.java b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/SearchTemplateResponse.java index 2f682ae28ae4c..2e8fd3d64b032 100644 --- a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/SearchTemplateResponse.java +++ b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/SearchTemplateResponse.java @@ -10,16 +10,16 @@ import org.elasticsearch.action.ActionResponse; import org.elasticsearch.action.search.SearchResponse; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.StatusToXContentObject; +import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.rest.RestStatus; import java.io.IOException; import java.io.InputStream; @@ -34,8 +34,7 @@ public class SearchTemplateResponse extends ActionResponse implements StatusToXC /** Contains the search response, if any **/ private SearchResponse response; - SearchTemplateResponse() { - } + SearchTemplateResponse() {} SearchTemplateResponse(StreamInput in) throws IOException { super(in); @@ -80,17 +79,13 @@ public static SearchTemplateResponse fromXContent(XContentParser parser) throws if (contentAsMap.containsKey(TEMPLATE_OUTPUT_FIELD.getPreferredName())) { Object source = contentAsMap.get(TEMPLATE_OUTPUT_FIELD.getPreferredName()); - XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON) - .value(source); + XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON).value(source); searchTemplateResponse.setSource(BytesReference.bytes(builder)); } else { XContentType contentType = parser.contentType(); - XContentBuilder builder = XContentFactory.contentBuilder(contentType) - .map(contentAsMap); - XContentParser searchResponseParser = contentType.xContent().createParser( - parser.getXContentRegistry(), - parser.getDeprecationHandler(), - BytesReference.bytes(builder).streamInput()); + XContentBuilder builder = XContentFactory.contentBuilder(contentType).map(contentAsMap); + XContentParser searchResponseParser = contentType.xContent() + .createParser(parser.getXContentRegistry(), parser.getDeprecationHandler(), BytesReference.bytes(builder).streamInput()); SearchResponse searchResponse = SearchResponse.fromXContent(searchResponseParser); searchTemplateResponse.setResponse(searchResponse); @@ -104,7 +99,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws response.toXContent(builder, params); } else { builder.startObject(); - //we can assume the template is always json as we convert it before compiling it + // we can assume the template is always json as we convert it before compiling it try (InputStream stream = source.streamInput()) { builder.rawField(TEMPLATE_OUTPUT_FIELD.getPreferredName(), stream, XContentType.JSON); } diff --git a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/TransportMultiSearchTemplateAction.java b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/TransportMultiSearchTemplateAction.java index 6af8d269f32a4..1f3b858952b78 100644 --- a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/TransportMultiSearchTemplateAction.java +++ b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/TransportMultiSearchTemplateAction.java @@ -16,10 +16,10 @@ import org.elasticsearch.action.support.HandledTransportAction; import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.common.inject.Inject; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.script.ScriptService; import org.elasticsearch.tasks.Task; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.NamedXContentRegistry; import java.util.ArrayList; import java.util.List; @@ -33,8 +33,13 @@ public class TransportMultiSearchTemplateAction extends HandledTransportAction CustomMustacheFactory.createEncoder("non-existent")); + final IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> CustomMustacheFactory.createEncoder("non-existent") + ); assertThat(e.getMessage(), equalTo("No encoder found for media type [non-existent]")); } @@ -38,19 +40,29 @@ public void testCreateEncoder() { } { - final IllegalArgumentException e = - expectThrows(IllegalArgumentException.class, () -> CustomMustacheFactory.createEncoder("test")); + final IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> CustomMustacheFactory.createEncoder("test") + ); assertThat(e.getMessage(), equalTo("No encoder found for media type [test]")); } - assertThat(CustomMustacheFactory.createEncoder(CustomMustacheFactory.JSON_MEDIA_TYPE_WITH_CHARSET), - instanceOf(CustomMustacheFactory.JsonEscapeEncoder.class)); - assertThat(CustomMustacheFactory.createEncoder(CustomMustacheFactory.JSON_MEDIA_TYPE), - instanceOf(CustomMustacheFactory.JsonEscapeEncoder.class)); - assertThat(CustomMustacheFactory.createEncoder(CustomMustacheFactory.PLAIN_TEXT_MEDIA_TYPE), - instanceOf(CustomMustacheFactory.DefaultEncoder.class)); - assertThat(CustomMustacheFactory.createEncoder(CustomMustacheFactory.X_WWW_FORM_URLENCODED_MEDIA_TYPE), - instanceOf(CustomMustacheFactory.UrlEncoder.class)); + assertThat( + CustomMustacheFactory.createEncoder(CustomMustacheFactory.JSON_MEDIA_TYPE_WITH_CHARSET), + instanceOf(CustomMustacheFactory.JsonEscapeEncoder.class) + ); + assertThat( + CustomMustacheFactory.createEncoder(CustomMustacheFactory.JSON_MEDIA_TYPE), + instanceOf(CustomMustacheFactory.JsonEscapeEncoder.class) + ); + assertThat( + CustomMustacheFactory.createEncoder(CustomMustacheFactory.PLAIN_TEXT_MEDIA_TYPE), + instanceOf(CustomMustacheFactory.DefaultEncoder.class) + ); + assertThat( + CustomMustacheFactory.createEncoder(CustomMustacheFactory.X_WWW_FORM_URLENCODED_MEDIA_TYPE), + instanceOf(CustomMustacheFactory.UrlEncoder.class) + ); } public void testJsonEscapeEncoder() { diff --git a/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/MultiSearchTemplateRequestTests.java b/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/MultiSearchTemplateRequestTests.java index e79b93f65d148..f6fcfbb69de3d 100644 --- a/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/MultiSearchTemplateRequestTests.java +++ b/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/MultiSearchTemplateRequestTests.java @@ -10,7 +10,6 @@ import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.common.bytes.BytesArray; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.script.ScriptType; import org.elasticsearch.search.Scroll; @@ -18,6 +17,7 @@ import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.StreamsUtils; import org.elasticsearch.test.rest.FakeRestRequest; +import org.elasticsearch.xcontent.XContentType; import java.io.IOException; import java.nio.charset.StandardCharsets; @@ -32,8 +32,8 @@ public class MultiSearchTemplateRequestTests extends ESTestCase { public void testParseRequest() throws Exception { byte[] data = StreamsUtils.copyToBytesFromClasspath("/org/elasticsearch/script/mustache/simple-msearch-template.json"); - RestRequest restRequest = new FakeRestRequest.Builder(xContentRegistry()) - .withContent(new BytesArray(data), XContentType.JSON).build(); + RestRequest restRequest = new FakeRestRequest.Builder(xContentRegistry()).withContent(new BytesArray(data), XContentType.JSON) + .build(); MultiSearchTemplateRequest request = RestMultiSearchTemplateAction.parseRequest(restRequest, true); @@ -66,10 +66,10 @@ public void testParseRequest() throws Exception { } public void testParseWithCarriageReturn() throws Exception { - final String content = "{\"index\":[\"test0\", \"test1\"], \"request_cache\": true}\r\n" + - "{\"source\": {\"query\" : {\"match_{{template}}\" :{}}}, \"params\": {\"template\": \"all\" } }\r\n"; - RestRequest restRequest = new FakeRestRequest.Builder(xContentRegistry()) - .withContent(new BytesArray(content), XContentType.JSON).build(); + final String content = "{\"index\":[\"test0\", \"test1\"], \"request_cache\": true}\r\n" + + "{\"source\": {\"query\" : {\"match_{{template}}\" :{}}}, \"params\": {\"template\": \"all\" } }\r\n"; + RestRequest restRequest = new FakeRestRequest.Builder(xContentRegistry()).withContent(new BytesArray(content), XContentType.JSON) + .build(); MultiSearchTemplateRequest request = RestMultiSearchTemplateAction.parseRequest(restRequest, true); @@ -88,8 +88,7 @@ public void testParseWithCarriageReturn() throws Exception { public void testMaxConcurrentSearchRequests() { MultiSearchTemplateRequest request = new MultiSearchTemplateRequest(); request.maxConcurrentSearchRequests(randomIntBetween(1, Integer.MAX_VALUE)); - expectThrows(IllegalArgumentException.class, () -> - request.maxConcurrentSearchRequests(randomIntBetween(Integer.MIN_VALUE, 0))); + expectThrows(IllegalArgumentException.class, () -> request.maxConcurrentSearchRequests(randomIntBetween(Integer.MIN_VALUE, 0))); } public void testMultiSearchTemplateToJson() throws Exception { @@ -97,7 +96,7 @@ public void testMultiSearchTemplateToJson() throws Exception { MultiSearchTemplateRequest multiSearchTemplateRequest = new MultiSearchTemplateRequest(); for (int i = 0; i < numSearchRequests; i++) { // Create a random request. - String[] indices = {"test"}; + String[] indices = { "test" }; SearchRequest searchRequest = new SearchRequest(indices); // scroll is not supported in the current msearch or msearchtemplate api, so unset it: searchRequest.scroll((Scroll) null); @@ -117,12 +116,12 @@ public void testMultiSearchTemplateToJson() throws Exception { multiSearchTemplateRequest.add(searchTemplateRequest); } - //Serialize the request + // Serialize the request String serialized = toJsonString(multiSearchTemplateRequest); - //Deserialize the request - RestRequest restRequest = new FakeRestRequest.Builder(xContentRegistry()) - .withContent(new BytesArray(serialized), XContentType.JSON).build(); + // Deserialize the request + RestRequest restRequest = new FakeRestRequest.Builder(xContentRegistry()).withContent(new BytesArray(serialized), XContentType.JSON) + .build(); MultiSearchTemplateRequest deser = RestMultiSearchTemplateAction.parseRequest(restRequest, true); // For object equality purposes need to set the search requests' source to non-null diff --git a/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/MultiSearchTemplateResponseTests.java b/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/MultiSearchTemplateResponseTests.java index bacfedd229a3a..eeee9c0688573 100644 --- a/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/MultiSearchTemplateResponseTests.java +++ b/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/MultiSearchTemplateResponseTests.java @@ -11,10 +11,10 @@ import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.ShardSearchFailure; import org.elasticsearch.common.Strings; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.internal.InternalSearchResponse; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.function.Predicate; @@ -41,8 +41,16 @@ protected MultiSearchTemplateResponse createTestInstance() { InternalSearchResponse internalSearchResponse = InternalSearchResponse.empty(); SearchResponse.Clusters clusters = randomClusters(); SearchTemplateResponse searchTemplateResponse = new SearchTemplateResponse(); - SearchResponse searchResponse = new SearchResponse(internalSearchResponse, null, totalShards, - successfulShards, skippedShards, tookInMillis, ShardSearchFailure.EMPTY_ARRAY, clusters); + SearchResponse searchResponse = new SearchResponse( + internalSearchResponse, + null, + totalShards, + successfulShards, + skippedShards, + tookInMillis, + ShardSearchFailure.EMPTY_ARRAY, + clusters + ); searchTemplateResponse.setResponse(searchResponse); items[i] = new MultiSearchTemplateResponse.Item(searchTemplateResponse, null); } @@ -56,7 +64,7 @@ private static SearchResponse.Clusters randomClusters() { return new SearchResponse.Clusters(totalClusters, successfulClusters, skippedClusters); } - private static MultiSearchTemplateResponse createTestInstanceWithFailures() { + private static MultiSearchTemplateResponse createTestInstanceWithFailures() { int numItems = randomIntBetween(0, 128); long overallTookInMillis = randomNonNegativeLong(); MultiSearchTemplateResponse.Item[] items = new MultiSearchTemplateResponse.Item[numItems]; @@ -70,8 +78,16 @@ private static MultiSearchTemplateResponse createTestInstanceWithFailures() { InternalSearchResponse internalSearchResponse = InternalSearchResponse.empty(); SearchResponse.Clusters clusters = randomClusters(); SearchTemplateResponse searchTemplateResponse = new SearchTemplateResponse(); - SearchResponse searchResponse = new SearchResponse(internalSearchResponse, null, totalShards, - successfulShards, skippedShards, tookInMillis, ShardSearchFailure.EMPTY_ARRAY, clusters); + SearchResponse searchResponse = new SearchResponse( + internalSearchResponse, + null, + totalShards, + successfulShards, + skippedShards, + tookInMillis, + ShardSearchFailure.EMPTY_ARRAY, + clusters + ); searchTemplateResponse.setResponse(searchResponse); items[i] = new MultiSearchTemplateResponse.Item(searchTemplateResponse, null); } else { @@ -119,13 +135,22 @@ protected void assertEqualInstances(MultiSearchTemplateResponse expectedInstance */ public void testFromXContentWithFailures() throws IOException { Supplier instanceSupplier = MultiSearchTemplateResponseTests::createTestInstanceWithFailures; - //with random fields insertion in the inner exceptions, some random stuff may be parsed back as metadata, - //but that does not bother our assertions, as we only want to test that we don't break. + // with random fields insertion in the inner exceptions, some random stuff may be parsed back as metadata, + // but that does not bother our assertions, as we only want to test that we don't break. boolean supportsUnknownFields = true; - //exceptions are not of the same type whenever parsed back + // exceptions are not of the same type whenever parsed back boolean assertToXContentEquivalence = false; - AbstractXContentTestCase.testFromXContent(NUMBER_OF_TEST_RUNS, instanceSupplier, supportsUnknownFields, Strings.EMPTY_ARRAY, - getRandomFieldsExcludeFilterWhenResultHasErrors(), this::createParser, this::doParseInstance, - this::assertEqualInstances, assertToXContentEquivalence, ToXContent.EMPTY_PARAMS); + AbstractXContentTestCase.testFromXContent( + NUMBER_OF_TEST_RUNS, + instanceSupplier, + supportsUnknownFields, + Strings.EMPTY_ARRAY, + getRandomFieldsExcludeFilterWhenResultHasErrors(), + this::createParser, + this::doParseInstance, + this::assertEqualInstances, + assertToXContentEquivalence, + ToXContent.EMPTY_PARAMS + ); } } diff --git a/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/MustacheScriptEngineTests.java b/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/MustacheScriptEngineTests.java index a0dc8b6f506df..f96ff25e283b4 100644 --- a/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/MustacheScriptEngineTests.java +++ b/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/MustacheScriptEngineTests.java @@ -9,11 +9,11 @@ import com.github.mustachejava.MustacheFactory; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.script.TemplateScript; import org.elasticsearch.script.Script; +import org.elasticsearch.script.TemplateScript; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.json.JsonXContent; import org.junit.Before; import java.io.IOException; @@ -40,34 +40,40 @@ public void setup() { public void testSimpleParameterReplace() { Map compileParams = Collections.singletonMap("content_type", "application/json"); { - String template = "GET _search {\"query\": " + "{\"boosting\": {" + "\"positive\": {\"match\": {\"body\": \"gift\"}}," - + "\"negative\": {\"term\": {\"body\": {\"value\": \"solr\"}" + "}}, \"negative_boost\": {{boost_val}} } }}"; + String template = "GET _search {\"query\": " + + "{\"boosting\": {" + + "\"positive\": {\"match\": {\"body\": \"gift\"}}," + + "\"negative\": {\"term\": {\"body\": {\"value\": \"solr\"}" + + "}}, \"negative_boost\": {{boost_val}} } }}"; Map vars = new HashMap<>(); vars.put("boost_val", "0.3"); String o = qe.compile(null, template, TemplateScript.CONTEXT, compileParams).newInstance(vars).execute(); - assertEquals("GET _search {\"query\": {\"boosting\": {\"positive\": {\"match\": {\"body\": \"gift\"}}," + assertEquals( + "GET _search {\"query\": {\"boosting\": {\"positive\": {\"match\": {\"body\": \"gift\"}}," + "\"negative\": {\"term\": {\"body\": {\"value\": \"solr\"}}}, \"negative_boost\": 0.3 } }}", - o); + o + ); } { - String template = "GET _search {\"query\": " + "{\"boosting\": {" + "\"positive\": {\"match\": {\"body\": \"gift\"}}," - + "\"negative\": {\"term\": {\"body\": {\"value\": \"{{body_val}}\"}" + "}}, \"negative_boost\": {{boost_val}} } }}"; + String template = "GET _search {\"query\": " + + "{\"boosting\": {" + + "\"positive\": {\"match\": {\"body\": \"gift\"}}," + + "\"negative\": {\"term\": {\"body\": {\"value\": \"{{body_val}}\"}" + + "}}, \"negative_boost\": {{boost_val}} } }}"; Map vars = new HashMap<>(); vars.put("boost_val", "0.3"); vars.put("body_val", "\"quick brown\""); String o = qe.compile(null, template, TemplateScript.CONTEXT, compileParams).newInstance(vars).execute(); - assertEquals("GET _search {\"query\": {\"boosting\": {\"positive\": {\"match\": {\"body\": \"gift\"}}," + assertEquals( + "GET _search {\"query\": {\"boosting\": {\"positive\": {\"match\": {\"body\": \"gift\"}}," + "\"negative\": {\"term\": {\"body\": {\"value\": \"\\\"quick brown\\\"\"}}}, \"negative_boost\": 0.3 } }}", - o); + o + ); } } public void testSimple() throws IOException { - String templateString = - "{" - + "\"source\":{\"match_{{template}}\": {}}," - + "\"params\":{\"template\":\"all\"}" - + "}"; + String templateString = "{" + "\"source\":{\"match_{{template}}\": {}}," + "\"params\":{\"template\":\"all\"}" + "}"; XContentParser parser = createParser(JsonXContent.jsonXContent, templateString); Script script = Script.parse(parser); TemplateScript.Factory compiled = qe.compile(null, script.getIdOrCode(), TemplateScript.CONTEXT, Collections.emptyMap()); @@ -76,13 +82,13 @@ public void testSimple() throws IOException { } public void testParseTemplateAsSingleStringWithConditionalClause() throws IOException { - String templateString = - "{" - + " \"source\" : \"{ \\\"match_{{#use_it}}{{template}}{{/use_it}}\\\":{} }\"," + " \"params\":{" - + " \"template\":\"all\"," - + " \"use_it\": true" - + " }" - + "}"; + String templateString = "{" + + " \"source\" : \"{ \\\"match_{{#use_it}}{{template}}{{/use_it}}\\\":{} }\"," + + " \"params\":{" + + " \"template\":\"all\"," + + " \"use_it\": true" + + " }" + + "}"; XContentParser parser = createParser(JsonXContent.jsonXContent, templateString); Script script = Script.parse(parser); TemplateScript.Factory compiled = qe.compile(null, script.getIdOrCode(), TemplateScript.CONTEXT, Collections.emptyMap()); @@ -102,42 +108,42 @@ public void testEscapeJson() throws IOException { assertThat(writer.toString(), equalTo("\\n")); } - Character[] specialChars = new Character[]{ - '\"', - '\\', - '\u0000', - '\u0001', - '\u0002', - '\u0003', - '\u0004', - '\u0005', - '\u0006', - '\u0007', - '\u0008', - '\u0009', - '\u000B', - '\u000C', - '\u000E', - '\u000F', - '\u001F'}; - String[] escapedChars = new String[]{ - "\\\"", - "\\\\", - "\\u0000", - "\\u0001", - "\\u0002", - "\\u0003", - "\\u0004", - "\\u0005", - "\\u0006", - "\\u0007", - "\\u0008", - "\\u0009", - "\\u000B", - "\\u000C", - "\\u000E", - "\\u000F", - "\\u001F"}; + Character[] specialChars = new Character[] { + '\"', + '\\', + '\u0000', + '\u0001', + '\u0002', + '\u0003', + '\u0004', + '\u0005', + '\u0006', + '\u0007', + '\u0008', + '\u0009', + '\u000B', + '\u000C', + '\u000E', + '\u000F', + '\u001F' }; + String[] escapedChars = new String[] { + "\\\"", + "\\\\", + "\\u0000", + "\\u0001", + "\\u0002", + "\\u0003", + "\\u0004", + "\\u0005", + "\\u0006", + "\\u0007", + "\\u0008", + "\\u0009", + "\\u000B", + "\\u000C", + "\\u000E", + "\\u000F", + "\\u001F" }; int iters = scaledRandomIntBetween(100, 1000); for (int i = 0; i < iters; i++) { int rounds = scaledRandomIntBetween(1, 20); @@ -178,13 +184,12 @@ private String getChars() { * */ private static boolean isEscapeChar(char c) { switch (c) { - case '"': - case '\\': - return true; + case '"': + case '\\': + return true; } - if (c < '\u002F') - return true; + if (c < '\u002F') return true; return false; } } diff --git a/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/MustacheTests.java b/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/MustacheTests.java index 9a925e3b5aef7..15fb1d1d98173 100644 --- a/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/MustacheTests.java +++ b/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/MustacheTests.java @@ -8,12 +8,12 @@ package org.elasticsearch.script.mustache; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.script.ScriptEngine; import org.elasticsearch.script.ScriptException; import org.elasticsearch.script.TemplateScript; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentBuilder; import org.hamcrest.Matcher; import java.net.URLEncoder; @@ -41,7 +41,8 @@ public class MustacheTests extends ESTestCase { private ScriptEngine engine = new MustacheScriptEngine(); public void testBasics() { - String template = "GET _search {\"query\": " + "{\"boosting\": {" + String template = "GET _search {\"query\": " + + "{\"boosting\": {" + "\"positive\": {\"match\": {\"body\": \"gift\"}}," + "\"negative\": {\"term\": {\"body\": {\"value\": \"solr\"}" + "}}, \"negative_boost\": {{boost_val}} } }}"; @@ -50,20 +51,18 @@ public void testBasics() { TemplateScript.Factory factory = engine.compile(null, template, TemplateScript.CONTEXT, Collections.emptyMap()); TemplateScript result = factory.newInstance(params); assertEquals( - "Mustache templating broken", - "GET _search {\"query\": {\"boosting\": {\"positive\": {\"match\": {\"body\": \"gift\"}}," - + "\"negative\": {\"term\": {\"body\": {\"value\": \"solr\"}}}, \"negative_boost\": 0.2 } }}", - result.execute() + "Mustache templating broken", + "GET _search {\"query\": {\"boosting\": {\"positive\": {\"match\": {\"body\": \"gift\"}}," + + "\"negative\": {\"term\": {\"body\": {\"value\": \"solr\"}}}, \"negative_boost\": 0.2 } }}", + result.execute() ); } public void testArrayAccess() throws Exception { String template = "{{data.0}} {{data.1}}"; - TemplateScript.Factory factory = engine.compile(null, template, TemplateScript.CONTEXT, Collections.emptyMap()); + TemplateScript.Factory factory = engine.compile(null, template, TemplateScript.CONTEXT, Collections.emptyMap()); Map vars = new HashMap<>(); - Object data = randomFrom( - new String[] { "foo", "bar" }, - Arrays.asList("foo", "bar")); + Object data = randomFrom(new String[] { "foo", "bar" }, Arrays.asList("foo", "bar")); vars.put("data", data); assertThat(factory.newInstance(vars).execute(), equalTo("foo bar")); @@ -81,7 +80,7 @@ public void testArrayInArrayAccess() throws Exception { TemplateScript.Factory factory = engine.compile(null, template, TemplateScript.CONTEXT, Collections.emptyMap()); Map vars = new HashMap<>(); Object data = randomFrom( - new String[][] { new String[] { "foo", "bar" }}, + new String[][] { new String[] { "foo", "bar" } }, Collections.singletonList(new String[] { "foo", "bar" }), singleton(new String[] { "foo", "bar" }) ); @@ -95,7 +94,8 @@ public void testMapInArrayAccess() throws Exception { Map vars = new HashMap<>(); Object data = randomFrom( new Object[] { singletonMap("key", "foo"), singletonMap("key", "bar") }, - Arrays.asList(singletonMap("key", "foo"), singletonMap("key", "bar"))); + Arrays.asList(singletonMap("key", "foo"), singletonMap("key", "bar")) + ); vars.put("data", data); assertThat(factory.newInstance(vars).execute(), equalTo("foo bar")); @@ -108,7 +108,6 @@ public void testMapInArrayAccess() throws Exception { assertThat(output, both(containsString("foo")).and(containsString("bar"))); } - public void testSizeAccessForCollectionsAndArrays() throws Exception { String[] randomArrayValues = generateRandomStringArray(10, 20, false); List randomList = Arrays.asList(generateRandomStringArray(10, 20, false)); @@ -174,23 +173,31 @@ public void testMultipleMapsToJSON() throws Exception { Map ctx = singletonMap("ctx", humans); - assertScript("{{#toJson}}.{{/toJson}}", ctx, - equalTo("{\"ctx\":{\"first\":{\"name\":\"John Smith\",\"age\":42,\"height\":1.84},\"second\":" + - "{\"name\":\"Dave Smith\",\"age\":27,\"height\":1.71}}}")); + assertScript( + "{{#toJson}}.{{/toJson}}", + ctx, + equalTo( + "{\"ctx\":{\"first\":{\"name\":\"John Smith\",\"age\":42,\"height\":1.84},\"second\":" + + "{\"name\":\"Dave Smith\",\"age\":27,\"height\":1.71}}}" + ) + ); - assertScript("{{#toJson}}ctx{{/toJson}}", ctx, - equalTo("{\"first\":{\"name\":\"John Smith\",\"age\":42,\"height\":1.84},\"second\":" + - "{\"name\":\"Dave Smith\",\"age\":27,\"height\":1.71}}")); + assertScript( + "{{#toJson}}ctx{{/toJson}}", + ctx, + equalTo( + "{\"first\":{\"name\":\"John Smith\",\"age\":42,\"height\":1.84},\"second\":" + + "{\"name\":\"Dave Smith\",\"age\":27,\"height\":1.71}}" + ) + ); - assertScript("{{#toJson}}ctx.first{{/toJson}}", ctx, - equalTo("{\"name\":\"John Smith\",\"age\":42,\"height\":1.84}")); + assertScript("{{#toJson}}ctx.first{{/toJson}}", ctx, equalTo("{\"name\":\"John Smith\",\"age\":42,\"height\":1.84}")); - assertScript("{{#toJson}}ctx.second{{/toJson}}", ctx, - equalTo("{\"name\":\"Dave Smith\",\"age\":27,\"height\":1.71}")); + assertScript("{{#toJson}}ctx.second{{/toJson}}", ctx, equalTo("{\"name\":\"Dave Smith\",\"age\":27,\"height\":1.71}")); } public void testSimpleArrayToJSON() throws Exception { - String[] array = new String[]{"one", "two", "three"}; + String[] array = new String[] { "one", "two", "three" }; Map ctx = singletonMap("array", array); assertScript("{{#toJson}}.{{/toJson}}", ctx, equalTo("{\"array\":[\"one\",\"two\",\"three\"]}")); @@ -229,81 +236,86 @@ public void testsUnsupportedTagsToJson() { public void testEmbeddedToJSON() throws Exception { XContentBuilder builder = jsonBuilder().startObject() - .startArray("bulks") - .startObject() - .field("index", "index-1") - .field("type", "type-1") - .field("id", 1) - .endObject() - .startObject() - .field("index", "index-2") - .field("type", "type-2") - .field("id", 2) - .endObject() - .endArray() - .endObject(); - - Map ctx = - singletonMap("ctx", XContentHelper.convertToMap(BytesReference.bytes(builder), false, builder.contentType()).v2()); - - assertScript("{{#ctx.bulks}}{{#toJson}}.{{/toJson}}{{/ctx.bulks}}", ctx, - equalTo("{\"index\":\"index-1\",\"id\":1,\"type\":\"type-1\"}{\"index\":\"index-2\",\"id\":2,\"type\":\"type-2\"}")); - - assertScript("{{#ctx.bulks}}<{{#toJson}}id{{/toJson}}>{{/ctx.bulks}}", ctx, - equalTo("<1><2>")); + .startArray("bulks") + .startObject() + .field("index", "index-1") + .field("type", "type-1") + .field("id", 1) + .endObject() + .startObject() + .field("index", "index-2") + .field("type", "type-2") + .field("id", 2) + .endObject() + .endArray() + .endObject(); + + Map ctx = singletonMap( + "ctx", + XContentHelper.convertToMap(BytesReference.bytes(builder), false, builder.contentType()).v2() + ); + + assertScript( + "{{#ctx.bulks}}{{#toJson}}.{{/toJson}}{{/ctx.bulks}}", + ctx, + equalTo("{\"index\":\"index-1\",\"id\":1,\"type\":\"type-1\"}{\"index\":\"index-2\",\"id\":2,\"type\":\"type-2\"}") + ); + + assertScript("{{#ctx.bulks}}<{{#toJson}}id{{/toJson}}>{{/ctx.bulks}}", ctx, equalTo("<1><2>")); } public void testSimpleArrayJoin() throws Exception { String template = "{{#join}}array{{/join}}"; - assertScript(template, singletonMap("array", new String[]{"one", "two", "three"}), equalTo("one,two,three")); - assertScript(template, singletonMap("array", new int[]{1, 2, 3}), equalTo("1,2,3")); - assertScript(template, singletonMap("array", new long[]{1L, 2L, 3L}), equalTo("1,2,3")); - assertScript(template, singletonMap("array", new double[]{1.5, 2.5, 3.5}), equalTo("1.5,2.5,3.5")); - assertScript(template, singletonMap("array", new boolean[]{true, false, true}), equalTo("true,false,true")); - assertScript(template, singletonMap("array", new boolean[]{true, false, true}), equalTo("true,false,true")); + assertScript(template, singletonMap("array", new String[] { "one", "two", "three" }), equalTo("one,two,three")); + assertScript(template, singletonMap("array", new int[] { 1, 2, 3 }), equalTo("1,2,3")); + assertScript(template, singletonMap("array", new long[] { 1L, 2L, 3L }), equalTo("1,2,3")); + assertScript(template, singletonMap("array", new double[] { 1.5, 2.5, 3.5 }), equalTo("1.5,2.5,3.5")); + assertScript(template, singletonMap("array", new boolean[] { true, false, true }), equalTo("true,false,true")); + assertScript(template, singletonMap("array", new boolean[] { true, false, true }), equalTo("true,false,true")); } public void testEmbeddedArrayJoin() throws Exception { XContentBuilder builder = jsonBuilder().startObject() - .startArray("people") - .startObject() - .field("name", "John Smith") - .startArray("emails") - .value("john@smith.com") - .value("john.smith@email.com") - .value("jsmith@email.com") - .endArray() - .endObject() - .startObject() - .field("name", "John Doe") - .startArray("emails") - .value("john@doe.com") - .value("john.doe@email.com") - .value("jdoe@email.com") - .endArray() - .endObject() - .endArray() - .endObject(); - - Map ctx = - singletonMap("ctx", XContentHelper.convertToMap(BytesReference.bytes(builder), false, builder.contentType()).v2()); - - assertScript("{{#join}}ctx.people.0.emails{{/join}}", ctx, - equalTo("john@smith.com,john.smith@email.com,jsmith@email.com")); - - assertScript("{{#join}}ctx.people.1.emails{{/join}}", ctx, - equalTo("john@doe.com,john.doe@email.com,jdoe@email.com")); - - assertScript("{{#ctx.people}}to: {{#join}}emails{{/join}};{{/ctx.people}}", ctx, - equalTo("to: john@smith.com,john.smith@email.com,jsmith@email.com;to: john@doe.com,john.doe@email.com,jdoe@email.com;")); + .startArray("people") + .startObject() + .field("name", "John Smith") + .startArray("emails") + .value("john@smith.com") + .value("john.smith@email.com") + .value("jsmith@email.com") + .endArray() + .endObject() + .startObject() + .field("name", "John Doe") + .startArray("emails") + .value("john@doe.com") + .value("john.doe@email.com") + .value("jdoe@email.com") + .endArray() + .endObject() + .endArray() + .endObject(); + + Map ctx = singletonMap( + "ctx", + XContentHelper.convertToMap(BytesReference.bytes(builder), false, builder.contentType()).v2() + ); + + assertScript("{{#join}}ctx.people.0.emails{{/join}}", ctx, equalTo("john@smith.com,john.smith@email.com,jsmith@email.com")); + + assertScript("{{#join}}ctx.people.1.emails{{/join}}", ctx, equalTo("john@doe.com,john.doe@email.com,jdoe@email.com")); + + assertScript( + "{{#ctx.people}}to: {{#join}}emails{{/join}};{{/ctx.people}}", + ctx, + equalTo("to: john@smith.com,john.smith@email.com,jsmith@email.com;to: john@doe.com,john.doe@email.com,jdoe@email.com;") + ); } public void testJoinWithToJson() { - Map params = singletonMap("terms", - Arrays.asList(singletonMap("term", "foo"), singletonMap("term", "bar"))); + Map params = singletonMap("terms", Arrays.asList(singletonMap("term", "foo"), singletonMap("term", "bar"))); - assertScript("{{#join}}{{#toJson}}terms{{/toJson}}{{/join}}", params, - equalTo("[{\"term\":\"foo\"},{\"term\":\"bar\"}]")); + assertScript("{{#join}}{{#toJson}}terms{{/toJson}}{{/join}}", params, equalTo("[{\"term\":\"foo\"},{\"term\":\"bar\"}]")); } public void testsUnsupportedTagsJoin() { @@ -331,12 +343,12 @@ public void testJoinWithCustomDelimiter() { public void testUrlEncoder() { Map urls = new HashMap<>(); - urls.put("https://www.elastic.co", - "https%3A%2F%2Fwww.elastic.co"); - urls.put("", - "%3Clogstash-%7Bnow%2Fd%7D%3E"); - urls.put("?query=(foo:A OR baz:B) AND title:/joh?n(ath[oa]n)/ AND date:{* TO 2012-01}", - "%3Fquery%3D%28foo%3AA+OR+baz%3AB%29+AND+title%3A%2Fjoh%3Fn%28ath%5Boa%5Dn%29%2F+AND+date%3A%7B*+TO+2012-01%7D"); + urls.put("https://www.elastic.co", "https%3A%2F%2Fwww.elastic.co"); + urls.put("", "%3Clogstash-%7Bnow%2Fd%7D%3E"); + urls.put( + "?query=(foo:A OR baz:B) AND title:/joh?n(ath[oa]n)/ AND date:{* TO 2012-01}", + "%3Fquery%3D%28foo%3AA+OR+baz%3AB%29+AND+title%3A%2Fjoh%3Fn%28ath%5Boa%5Dn%29%2F+AND+date%3A%7B*+TO+2012-01%7D" + ); for (Map.Entry url : urls.entrySet()) { assertScript("{{#url}}{{params}}{{/url}}", singletonMap("params", url.getKey()), equalTo(url.getValue())); @@ -344,27 +356,44 @@ public void testUrlEncoder() { } public void testUrlEncoderWithParam() throws Exception { - assertScript("{{#url}}{{index}}{{/url}}", singletonMap("index", ""), - equalTo("%3Clogstash-%7Bnow%2Fd%7BYYYY.MM.dd%7C%2B12%3A00%7D%7D%3E")); + assertScript( + "{{#url}}{{index}}{{/url}}", + singletonMap("index", ""), + equalTo("%3Clogstash-%7Bnow%2Fd%7BYYYY.MM.dd%7C%2B12%3A00%7D%7D%3E") + ); final String random = randomAlphaOfLength(10); - assertScript("{{#url}}prefix_{{s}}{{/url}}", singletonMap("s", random), - equalTo("prefix_" + URLEncoder.encode(random, StandardCharsets.UTF_8.name()))); + assertScript( + "{{#url}}prefix_{{s}}{{/url}}", + singletonMap("s", random), + equalTo("prefix_" + URLEncoder.encode(random, StandardCharsets.UTF_8.name())) + ); } public void testUrlEncoderWithJoin() { Map params = singletonMap("emails", Arrays.asList("john@smith.com", "john.smith@email.com", "jsmith@email.com")); - assertScript("?query={{#url}}{{#join}}emails{{/join}}{{/url}}", params, - equalTo("?query=john%40smith.com%2Cjohn.smith%40email.com%2Cjsmith%40email.com")); + assertScript( + "?query={{#url}}{{#join}}emails{{/join}}{{/url}}", + params, + equalTo("?query=john%40smith.com%2Cjohn.smith%40email.com%2Cjsmith%40email.com") + ); - params = singletonMap("indices", new String[]{"", "", ""}); - assertScript("{{#url}}https://localhost:9200/{{#join}}indices{{/join}}/_stats{{/url}}", params, - equalTo("https%3A%2F%2Flocalhost%3A9200%2F%3Clogstash-%7Bnow%2Fd-2d%7D" + - "%3E%2C%3Clogstash-%7Bnow%2Fd-1d%7D%3E%2C%3Clogstash-%7Bnow%2Fd%7D%3E%2F_stats")); + params = singletonMap("indices", new String[] { "", "", "" }); + assertScript( + "{{#url}}https://localhost:9200/{{#join}}indices{{/join}}/_stats{{/url}}", + params, + equalTo( + "https%3A%2F%2Flocalhost%3A9200%2F%3Clogstash-%7Bnow%2Fd-2d%7D" + + "%3E%2C%3Clogstash-%7Bnow%2Fd-1d%7D%3E%2C%3Clogstash-%7Bnow%2Fd%7D%3E%2F_stats" + ) + ); - params = singletonMap("fibonacci", new int[]{1, 1, 2, 3, 5, 8, 13, 21, 34, 55}); - assertScript("{{#url}}{{#join delimiter='+'}}fibonacci{{/join delimiter='+'}}{{/url}}", params, - equalTo("1%2B1%2B2%2B3%2B5%2B8%2B13%2B21%2B34%2B55")); + params = singletonMap("fibonacci", new int[] { 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 }); + assertScript( + "{{#url}}{{#join delimiter='+'}}fibonacci{{/join delimiter='+'}}{{/url}}", + params, + equalTo("1%2B1%2B2%2B3%2B5%2B8%2B13%2B21%2B34%2B55") + ); } private void assertScript(String script, Map vars, Matcher matcher) { diff --git a/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/RestMultiSearchTemplateActionTests.java b/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/RestMultiSearchTemplateActionTests.java index 3aaa314580fde..f2abe137d671f 100644 --- a/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/RestMultiSearchTemplateActionTests.java +++ b/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/RestMultiSearchTemplateActionTests.java @@ -8,13 +8,13 @@ package org.elasticsearch.script.mustache; -import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.test.rest.FakeRestRequest; import org.elasticsearch.test.rest.RestActionTestCase; +import org.elasticsearch.xcontent.XContentType; import org.junit.Before; import org.mockito.Mockito; @@ -29,37 +29,30 @@ public class RestMultiSearchTemplateActionTests extends RestActionTestCase { @Before public void setUpAction() { controller().registerHandler(new RestMultiSearchTemplateAction(Settings.EMPTY)); - //todo how to workaround this? we get AssertionError without this + // todo how to workaround this? we get AssertionError without this verifyingClient.setExecuteVerifier((actionType, request) -> Mockito.mock(MultiSearchTemplateResponse.class)); verifyingClient.setExecuteLocallyVerifier((actionType, request) -> Mockito.mock(MultiSearchTemplateResponse.class)); } public void testTypeInPath() { - String content = "{ \"index\": \"some_index\" } \n" + - "{\"source\": {\"query\" : {\"match_all\" :{}}}} \n"; + String content = "{ \"index\": \"some_index\" } \n" + "{\"source\": {\"query\" : {\"match_all\" :{}}}} \n"; BytesArray bytesContent = new BytesArray(content.getBytes(StandardCharsets.UTF_8)); - RestRequest request = new FakeRestRequest.Builder(xContentRegistry()) - .withHeaders(Map.of("Content-Type", contentTypeHeader, "Accept", contentTypeHeader)) - .withMethod(RestRequest.Method.GET) - .withPath("/some_index/some_type/_msearch/template") - .withContent(bytesContent, null) - .build(); + RestRequest request = new FakeRestRequest.Builder(xContentRegistry()).withHeaders( + Map.of("Content-Type", contentTypeHeader, "Accept", contentTypeHeader) + ).withMethod(RestRequest.Method.GET).withPath("/some_index/some_type/_msearch/template").withContent(bytesContent, null).build(); dispatchRequest(request); assertWarnings(RestMultiSearchTemplateAction.TYPES_DEPRECATION_MESSAGE); } public void testTypeInBody() { - String content = "{ \"index\": \"some_index\", \"type\": \"some_type\" } \n" + - "{\"source\": {\"query\" : {\"match_all\" :{}}}} \n"; + String content = "{ \"index\": \"some_index\", \"type\": \"some_type\" } \n" + "{\"source\": {\"query\" : {\"match_all\" :{}}}} \n"; BytesArray bytesContent = new BytesArray(content.getBytes(StandardCharsets.UTF_8)); - RestRequest request = new FakeRestRequest.Builder(xContentRegistry()) - .withHeaders(Map.of("Content-Type", contentTypeHeader, "Accept", contentTypeHeader)) - .withPath("/some_index/_msearch/template") - .withContent(bytesContent, null) - .build(); + RestRequest request = new FakeRestRequest.Builder(xContentRegistry()).withHeaders( + Map.of("Content-Type", contentTypeHeader, "Accept", contentTypeHeader) + ).withPath("/some_index/_msearch/template").withContent(bytesContent, null).build(); dispatchRequest(request); assertWarnings(RestMultiSearchTemplateAction.TYPES_DEPRECATION_MESSAGE); diff --git a/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/RestSearchTemplateActionTests.java b/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/RestSearchTemplateActionTests.java index 3470b276fb750..a7a23f20f2eef 100644 --- a/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/RestSearchTemplateActionTests.java +++ b/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/RestSearchTemplateActionTests.java @@ -32,11 +32,9 @@ public void setUpAction() { } public void testTypeInPath() { - RestRequest request = new FakeRestRequest.Builder(xContentRegistry()) - .withHeaders(Map.of("Content-Type", contentTypeHeader, "Accept", contentTypeHeader)) - .withMethod(RestRequest.Method.GET) - .withPath("/some_index/some_type/_search/template") - .build(); + RestRequest request = new FakeRestRequest.Builder(xContentRegistry()).withHeaders( + Map.of("Content-Type", contentTypeHeader, "Accept", contentTypeHeader) + ).withMethod(RestRequest.Method.GET).withPath("/some_index/some_type/_search/template").build(); dispatchRequest(request); assertWarnings(RestSearchAction.TYPES_DEPRECATION_MESSAGE); @@ -46,12 +44,9 @@ public void testTypeParameter() { Map params = new HashMap<>(); params.put("type", "some_type"); - RestRequest request = new FakeRestRequest.Builder(xContentRegistry()) - .withHeaders(Map.of("Content-Type", contentTypeHeader, "Accept", contentTypeHeader)) - .withMethod(RestRequest.Method.GET) - .withPath("/some_index/_search/template") - .withParams(params) - .build(); + RestRequest request = new FakeRestRequest.Builder(xContentRegistry()).withHeaders( + Map.of("Content-Type", contentTypeHeader, "Accept", contentTypeHeader) + ).withMethod(RestRequest.Method.GET).withPath("/some_index/_search/template").withParams(params).build(); dispatchRequest(request); assertWarnings(RestSearchAction.TYPES_DEPRECATION_MESSAGE); diff --git a/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/SearchTemplateRequestTests.java b/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/SearchTemplateRequestTests.java index 72d0c5eb42d46..81b3aa4147b1b 100644 --- a/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/SearchTemplateRequestTests.java +++ b/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/SearchTemplateRequestTests.java @@ -37,10 +37,10 @@ protected Writeable.Reader instanceReader() { protected SearchTemplateRequest mutateInstance(SearchTemplateRequest instance) throws IOException { List> mutators = new ArrayList<>(); - mutators.add(request -> request.setScriptType( - randomValueOtherThan(request.getScriptType(), () -> randomFrom(ScriptType.values())))); - mutators.add(request -> request.setScript( - randomValueOtherThan(request.getScript(), () -> randomAlphaOfLength(50)))); + mutators.add( + request -> request.setScriptType(randomValueOtherThan(request.getScriptType(), () -> randomFrom(ScriptType.values()))) + ); + mutators.add(request -> request.setScript(randomValueOtherThan(request.getScript(), () -> randomAlphaOfLength(50)))); mutators.add(request -> { Map mutatedScriptParams = new HashMap<>(request.getScriptParams()); @@ -53,8 +53,14 @@ protected SearchTemplateRequest mutateInstance(SearchTemplateRequest instance) t mutators.add(request -> request.setExplain(request.isExplain() == false)); mutators.add(request -> request.setSimulate(request.isSimulate() == false)); - mutators.add(request -> request.setRequest(randomValueOtherThan(request.getRequest(), - () -> RandomSearchRequestGenerator.randomSearchRequest(SearchSourceBuilder::searchSource)))); + mutators.add( + request -> request.setRequest( + randomValueOtherThan( + request.getRequest(), + () -> RandomSearchRequestGenerator.randomSearchRequest(SearchSourceBuilder::searchSource) + ) + ) + ); SearchTemplateRequest mutatedInstance = copyInstance(instance); Consumer mutator = randomFrom(mutators); @@ -62,7 +68,6 @@ protected SearchTemplateRequest mutateInstance(SearchTemplateRequest instance) t return mutatedInstance; } - public static SearchTemplateRequest createRandomRequest() { SearchTemplateRequest request = new SearchTemplateRequest(); request.setScriptType(randomFrom(ScriptType.values())); @@ -78,8 +83,7 @@ public static SearchTemplateRequest createRandomRequest() { request.setProfile(randomBoolean()); request.setSimulate(randomBoolean()); - request.setRequest(RandomSearchRequestGenerator.randomSearchRequest( - SearchSourceBuilder::searchSource)); + request.setRequest(RandomSearchRequestGenerator.randomSearchRequest(SearchSourceBuilder::searchSource)); return request; } } diff --git a/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/SearchTemplateRequestXContentTests.java b/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/SearchTemplateRequestXContentTests.java index c39e92886a9b8..57425f0b89269 100644 --- a/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/SearchTemplateRequestXContentTests.java +++ b/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/SearchTemplateRequestXContentTests.java @@ -9,6 +9,8 @@ package org.elasticsearch.script.mustache; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.script.ScriptType; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; @@ -16,8 +18,6 @@ import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.script.ScriptType; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; import java.util.HashMap; @@ -51,11 +51,12 @@ protected SearchTemplateRequest doParseInstance(XContentParser parser) throws IO @Override protected void assertEqualInstances(SearchTemplateRequest expectedInstance, SearchTemplateRequest newInstance) { assertTrue( - expectedInstance.isExplain() == newInstance.isExplain() && - expectedInstance.isProfile() == newInstance.isProfile() && - expectedInstance.getScriptType() == newInstance.getScriptType() && - Objects.equals(expectedInstance.getScript(), newInstance.getScript()) && - Objects.equals(expectedInstance.getScriptParams(), newInstance.getScriptParams())); + expectedInstance.isExplain() == newInstance.isExplain() + && expectedInstance.isProfile() == newInstance.isProfile() + && expectedInstance.getScriptType() == newInstance.getScriptType() + && Objects.equals(expectedInstance.getScript(), newInstance.getScript()) + && Objects.equals(expectedInstance.getScriptParams(), newInstance.getScriptParams()) + ); } @Override @@ -78,21 +79,19 @@ public void testToXContentWithInlineTemplate() throws IOException { XContentType contentType = randomFrom(XContentType.values()); XContentBuilder expectedRequest = XContentFactory.contentBuilder(contentType) .startObject() - .field("source", "{\"query\": { \"match\" : { \"{{my_field}}\" : \"{{my_value}}\" } } }") - .startObject("params") - .field("my_field", "foo") - .field("my_value", "bar") - .endObject() - .field("explain", false) - .field("profile", true) + .field("source", "{\"query\": { \"match\" : { \"{{my_field}}\" : \"{{my_value}}\" } } }") + .startObject("params") + .field("my_field", "foo") + .field("my_value", "bar") + .endObject() + .field("explain", false) + .field("profile", true) .endObject(); XContentBuilder actualRequest = XContentFactory.contentBuilder(contentType); request.toXContent(actualRequest, ToXContent.EMPTY_PARAMS); - assertToXContentEquivalent(BytesReference.bytes(expectedRequest), - BytesReference.bytes(actualRequest), - contentType); + assertToXContentEquivalent(BytesReference.bytes(expectedRequest), BytesReference.bytes(actualRequest), contentType); } public void testToXContentWithStoredTemplate() throws IOException { @@ -110,38 +109,35 @@ public void testToXContentWithStoredTemplate() throws IOException { XContentType contentType = randomFrom(XContentType.values()); XContentBuilder expectedRequest = XContentFactory.contentBuilder(contentType) .startObject() - .field("id", "match_template") - .startObject("params") - .field("my_field", "foo") - .field("my_value", "bar") - .endObject() - .field("explain", true) - .field("profile", false) + .field("id", "match_template") + .startObject("params") + .field("my_field", "foo") + .field("my_value", "bar") + .endObject() + .field("explain", true) + .field("profile", false) .endObject(); XContentBuilder actualRequest = XContentFactory.contentBuilder(contentType); request.toXContent(actualRequest, ToXContent.EMPTY_PARAMS); - assertToXContentEquivalent( - BytesReference.bytes(expectedRequest), - BytesReference.bytes(actualRequest), - contentType); + assertToXContentEquivalent(BytesReference.bytes(expectedRequest), BytesReference.bytes(actualRequest), contentType); } public void testFromXContentWithEmbeddedTemplate() throws Exception { - String source = "{" + - " 'source' : {\n" + - " 'query': {\n" + - " 'terms': {\n" + - " 'status': [\n" + - " '{{#status}}',\n" + - " '{{.}}',\n" + - " '{{/status}}'\n" + - " ]\n" + - " }\n" + - " }\n" + - " }" + - "}"; + String source = "{" + + " 'source' : {\n" + + " 'query': {\n" + + " 'terms': {\n" + + " 'status': [\n" + + " '{{#status}}',\n" + + " '{{.}}',\n" + + " '{{/status}}'\n" + + " ]\n" + + " }\n" + + " }\n" + + " }" + + "}"; SearchTemplateRequest request = SearchTemplateRequest.fromXContent(newParser(source)); assertThat(request.getScript(), equalTo("{\"query\":{\"terms\":{\"status\":[\"{{#status}}\",\"{{.}}\",\"{{/status}}\"]}}}")); @@ -150,17 +146,17 @@ public void testFromXContentWithEmbeddedTemplate() throws Exception { } public void testFromXContentWithEmbeddedTemplateAndParams() throws Exception { - String source = "{" + - " 'source' : {" + - " 'query': { 'match' : { '{{my_field}}' : '{{my_value}}' } }," + - " 'size' : '{{my_size}}'" + - " }," + - " 'params' : {" + - " 'my_field' : 'foo'," + - " 'my_value' : 'bar'," + - " 'my_size' : 5" + - " }" + - "}"; + String source = "{" + + " 'source' : {" + + " 'query': { 'match' : { '{{my_field}}' : '{{my_value}}' } }," + + " 'size' : '{{my_size}}'" + + " }," + + " 'params' : {" + + " 'my_field' : 'foo'," + + " 'my_value' : 'bar'," + + " 'my_size' : 5" + + " }" + + "}"; SearchTemplateRequest request = SearchTemplateRequest.fromXContent(newParser(source)); assertThat(request.getScript(), equalTo("{\"query\":{\"match\":{\"{{my_field}}\":\"{{my_value}}\"}},\"size\":\"{{my_size}}\"}")); diff --git a/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/SearchTemplateResponseTests.java b/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/SearchTemplateResponseTests.java index 810bec0b7fa71..ae2b1a7fbaa0e 100644 --- a/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/SearchTemplateResponseTests.java +++ b/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/SearchTemplateResponseTests.java @@ -12,15 +12,15 @@ import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.ShardSearchFailure; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.search.SearchHit; +import org.elasticsearch.search.SearchHits; +import org.elasticsearch.search.internal.InternalSearchResponse; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.search.SearchHit; -import org.elasticsearch.search.SearchHits; -import org.elasticsearch.search.internal.InternalSearchResponse; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; import java.util.Collections; @@ -57,19 +57,27 @@ private static SearchResponse createSearchResponse() { int skippedShards = randomIntBetween(0, totalShards); InternalSearchResponse internalSearchResponse = InternalSearchResponse.empty(); - return new SearchResponse(internalSearchResponse, null, totalShards, successfulShards, - skippedShards, tookInMillis, ShardSearchFailure.EMPTY_ARRAY, SearchResponse.Clusters.EMPTY); + return new SearchResponse( + internalSearchResponse, + null, + totalShards, + successfulShards, + skippedShards, + tookInMillis, + ShardSearchFailure.EMPTY_ARRAY, + SearchResponse.Clusters.EMPTY + ); } private static BytesReference createSource() { try { XContentBuilder source = XContentFactory.jsonBuilder() .startObject() - .startObject("query") - .startObject("match") - .field(randomAlphaOfLength(5), randomAlphaOfLength(10)) - .endObject() - .endObject() + .startObject("query") + .startObject("match") + .field(randomAlphaOfLength(5), randomAlphaOfLength(10)) + .endObject() + .endObject() .endObject(); return BytesReference.bytes(source); } catch (IOException e) { @@ -123,33 +131,30 @@ public void testSourceToXContent() throws IOException { XContentBuilder source = XContentFactory.jsonBuilder() .startObject() - .startObject("query") - .startObject("terms") - .field("status", new String[]{"pending", "published"}) - .endObject() - .endObject() + .startObject("query") + .startObject("terms") + .field("status", new String[] { "pending", "published" }) + .endObject() + .endObject() .endObject(); response.setSource(BytesReference.bytes(source)); XContentType contentType = randomFrom(XContentType.values()); XContentBuilder expectedResponse = XContentFactory.contentBuilder(contentType) .startObject() - .startObject("template_output") - .startObject("query") - .startObject("terms") - .field("status", new String[]{"pending", "published"}) - .endObject() - .endObject() - .endObject() + .startObject("template_output") + .startObject("query") + .startObject("terms") + .field("status", new String[] { "pending", "published" }) + .endObject() + .endObject() + .endObject() .endObject(); XContentBuilder actualResponse = XContentFactory.contentBuilder(contentType); response.toXContent(actualResponse, ToXContent.EMPTY_PARAMS); - assertToXContentEquivalent( - BytesReference.bytes(expectedResponse), - BytesReference.bytes(actualResponse), - contentType); + assertToXContentEquivalent(BytesReference.bytes(expectedResponse), BytesReference.bytes(actualResponse), contentType); } public void testSearchResponseToXContent() throws IOException { @@ -158,9 +163,24 @@ public void testSearchResponseToXContent() throws IOException { SearchHit[] hits = new SearchHit[] { hit }; InternalSearchResponse internalSearchResponse = new InternalSearchResponse( - new SearchHits(hits, new TotalHits(100, TotalHits.Relation.EQUAL_TO), 1.5f), null, null, null, false, null, 1); - SearchResponse searchResponse = new SearchResponse(internalSearchResponse, null, - 0, 0, 0, 0, ShardSearchFailure.EMPTY_ARRAY, SearchResponse.Clusters.EMPTY); + new SearchHits(hits, new TotalHits(100, TotalHits.Relation.EQUAL_TO), 1.5f), + null, + null, + null, + false, + null, + 1 + ); + SearchResponse searchResponse = new SearchResponse( + internalSearchResponse, + null, + 0, + 0, + 0, + 0, + ShardSearchFailure.EMPTY_ARRAY, + SearchResponse.Clusters.EMPTY + ); SearchTemplateResponse response = new SearchTemplateResponse(); response.setResponse(searchResponse); @@ -168,35 +188,32 @@ public void testSearchResponseToXContent() throws IOException { XContentType contentType = randomFrom(XContentType.values()); XContentBuilder expectedResponse = XContentFactory.contentBuilder(contentType) .startObject() - .field("took", 0) - .field("timed_out", false) - .startObject("_shards") - .field("total", 0) - .field("successful", 0) - .field("skipped", 0) - .field("failed", 0) - .endObject() - .startObject("hits") - .startObject("total") - .field("value", 100) - .field("relation", "eq") - .endObject() - .field("max_score", 1.5F) - .startArray("hits") - .startObject() - .field("_id", "id") - .field("_score", 2.0F) - .endObject() - .endArray() - .endObject() + .field("took", 0) + .field("timed_out", false) + .startObject("_shards") + .field("total", 0) + .field("successful", 0) + .field("skipped", 0) + .field("failed", 0) + .endObject() + .startObject("hits") + .startObject("total") + .field("value", 100) + .field("relation", "eq") + .endObject() + .field("max_score", 1.5F) + .startArray("hits") + .startObject() + .field("_id", "id") + .field("_score", 2.0F) + .endObject() + .endArray() + .endObject() .endObject(); XContentBuilder actualResponse = XContentFactory.contentBuilder(contentType); response.toXContent(actualResponse, ToXContent.EMPTY_PARAMS); - assertToXContentEquivalent( - BytesReference.bytes(expectedResponse), - BytesReference.bytes(actualResponse), - contentType); + assertToXContentEquivalent(BytesReference.bytes(expectedResponse), BytesReference.bytes(actualResponse), contentType); } } diff --git a/modules/lang-mustache/src/yamlRestTest/java/org/elasticsearch/script/mustache/LangMustacheClientYamlTestSuiteIT.java b/modules/lang-mustache/src/yamlRestTest/java/org/elasticsearch/script/mustache/LangMustacheClientYamlTestSuiteIT.java index 3593562529751..af7002705a3c0 100644 --- a/modules/lang-mustache/src/yamlRestTest/java/org/elasticsearch/script/mustache/LangMustacheClientYamlTestSuiteIT.java +++ b/modules/lang-mustache/src/yamlRestTest/java/org/elasticsearch/script/mustache/LangMustacheClientYamlTestSuiteIT.java @@ -25,4 +25,3 @@ public static Iterable parameters() throws Exception { return ESClientYamlSuiteTestCase.createParameters(); } } - diff --git a/modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/Whitelist.java b/modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/Whitelist.java index 8752bd41aefb2..30937ebcbd773 100644 --- a/modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/Whitelist.java +++ b/modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/Whitelist.java @@ -40,8 +40,13 @@ public final class Whitelist { public final List whitelistInstanceBindings; /** Standard constructor. All values must be not {@code null}. */ - public Whitelist(ClassLoader classLoader, List whitelistClasses, List whitelistImportedMethods, - List whitelistClassBindings, List whitelistInstanceBindings) { + public Whitelist( + ClassLoader classLoader, + List whitelistClasses, + List whitelistImportedMethods, + List whitelistClassBindings, + List whitelistInstanceBindings + ) { this.classLoader = Objects.requireNonNull(classLoader); this.whitelistClasses = Collections.unmodifiableList(Objects.requireNonNull(whitelistClasses)); diff --git a/modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/WhitelistClass.java b/modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/WhitelistClass.java index a543a0eae31cc..2130f9343dfa3 100644 --- a/modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/WhitelistClass.java +++ b/modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/WhitelistClass.java @@ -51,9 +51,14 @@ public final class WhitelistClass { public final Map, Object> painlessAnnotations; /** Standard constructor. All values must be not {@code null}. */ - public WhitelistClass(String origin, String javaClassName, - List whitelistConstructors, List whitelistMethods, List whitelistFields, - List painlessAnnotations) { + public WhitelistClass( + String origin, + String javaClassName, + List whitelistConstructors, + List whitelistMethods, + List whitelistFields, + List painlessAnnotations + ) { this.origin = Objects.requireNonNull(origin); this.javaClassName = Objects.requireNonNull(javaClassName); @@ -65,9 +70,12 @@ public WhitelistClass(String origin, String javaClassName, if (painlessAnnotations.isEmpty()) { this.painlessAnnotations = Collections.emptyMap(); } else { - this.painlessAnnotations = Collections.unmodifiableMap(Objects.requireNonNull(painlessAnnotations).stream() + this.painlessAnnotations = Collections.unmodifiableMap( + Objects.requireNonNull(painlessAnnotations) + .stream() .map(painlessAnnotation -> new AbstractMap.SimpleEntry<>(painlessAnnotation.getClass(), painlessAnnotation)) - .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))); + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)) + ); } } } diff --git a/modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/WhitelistClassBinding.java b/modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/WhitelistClassBinding.java index 08689967f1145..43931ad0f3f42 100644 --- a/modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/WhitelistClassBinding.java +++ b/modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/WhitelistClassBinding.java @@ -48,9 +48,14 @@ public class WhitelistClassBinding { public final Map, Object> painlessAnnotations; /** Standard constructor. All values must be not {@code null}. */ - public WhitelistClassBinding(String origin, String targetJavaClassName, - String methodName, String returnCanonicalTypeName, List canonicalTypeNameParameters, - List painlessAnnotations) { + public WhitelistClassBinding( + String origin, + String targetJavaClassName, + String methodName, + String returnCanonicalTypeName, + List canonicalTypeNameParameters, + List painlessAnnotations + ) { this.origin = Objects.requireNonNull(origin); this.targetJavaClassName = Objects.requireNonNull(targetJavaClassName); @@ -62,9 +67,12 @@ public WhitelistClassBinding(String origin, String targetJavaClassName, if (painlessAnnotations.isEmpty()) { this.painlessAnnotations = Collections.emptyMap(); } else { - this.painlessAnnotations = Collections.unmodifiableMap(Objects.requireNonNull(painlessAnnotations).stream() + this.painlessAnnotations = Collections.unmodifiableMap( + Objects.requireNonNull(painlessAnnotations) + .stream() .map(painlessAnnotation -> new AbstractMap.SimpleEntry<>(painlessAnnotation.getClass(), painlessAnnotation)) - .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))); + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)) + ); } } } diff --git a/modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/WhitelistConstructor.java b/modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/WhitelistConstructor.java index aea0694b5fd0c..c4ac56be70e35 100644 --- a/modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/WhitelistConstructor.java +++ b/modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/WhitelistConstructor.java @@ -43,9 +43,12 @@ public WhitelistConstructor(String origin, List canonicalTypeNameParamet if (painlessAnnotations.isEmpty()) { this.painlessAnnotations = Collections.emptyMap(); } else { - this.painlessAnnotations = Collections.unmodifiableMap(Objects.requireNonNull(painlessAnnotations).stream() + this.painlessAnnotations = Collections.unmodifiableMap( + Objects.requireNonNull(painlessAnnotations) + .stream() .map(painlessAnnotation -> new AbstractMap.SimpleEntry<>(painlessAnnotation.getClass(), painlessAnnotation)) - .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))); + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)) + ); } } } diff --git a/modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/WhitelistField.java b/modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/WhitelistField.java index 2bd7f331f8278..c1a3c43196647 100644 --- a/modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/WhitelistField.java +++ b/modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/WhitelistField.java @@ -43,9 +43,12 @@ public WhitelistField(String origin, String fieldName, String canonicalTypeNameP if (painlessAnnotations.isEmpty()) { this.painlessAnnotations = Collections.emptyMap(); } else { - this.painlessAnnotations = Collections.unmodifiableMap(Objects.requireNonNull(painlessAnnotations).stream() + this.painlessAnnotations = Collections.unmodifiableMap( + Objects.requireNonNull(painlessAnnotations) + .stream() .map(painlessAnnotation -> new AbstractMap.SimpleEntry<>(painlessAnnotation.getClass(), painlessAnnotation)) - .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))); + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)) + ); } } } diff --git a/modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/WhitelistInstanceBinding.java b/modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/WhitelistInstanceBinding.java index 91682d10b8e1b..405e1024af7c0 100644 --- a/modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/WhitelistInstanceBinding.java +++ b/modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/WhitelistInstanceBinding.java @@ -44,9 +44,14 @@ public class WhitelistInstanceBinding { public final Map, Object> painlessAnnotations; /** Standard constructor. All values must be not {@code null}. */ - public WhitelistInstanceBinding(String origin, Object targetInstance, - String methodName, String returnCanonicalTypeName, List canonicalTypeNameParameters, - List painlessAnnotations) { + public WhitelistInstanceBinding( + String origin, + Object targetInstance, + String methodName, + String returnCanonicalTypeName, + List canonicalTypeNameParameters, + List painlessAnnotations + ) { this.origin = Objects.requireNonNull(origin); this.targetInstance = Objects.requireNonNull(targetInstance); @@ -58,9 +63,12 @@ public WhitelistInstanceBinding(String origin, Object targetInstance, if (painlessAnnotations.isEmpty()) { this.painlessAnnotations = Collections.emptyMap(); } else { - this.painlessAnnotations = Collections.unmodifiableMap(Objects.requireNonNull(painlessAnnotations).stream() + this.painlessAnnotations = Collections.unmodifiableMap( + Objects.requireNonNull(painlessAnnotations) + .stream() .map(painlessAnnotation -> new AbstractMap.SimpleEntry<>(painlessAnnotation.getClass(), painlessAnnotation)) - .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))); + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)) + ); } } } diff --git a/modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/WhitelistLoader.java b/modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/WhitelistLoader.java index 534db7c050915..4709d5adacb0f 100644 --- a/modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/WhitelistLoader.java +++ b/modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/WhitelistLoader.java @@ -144,14 +144,17 @@ public static Whitelist loadFromResourceFiles(Class resource, Map whitelistStatics = new ArrayList<>(); List whitelistClassBindings = new ArrayList<>(); - // Execute a single pass through the whitelist text files. This will gather all the + // Execute a single pass through the whitelist text files. This will gather all the // constructors, methods, augmented methods, and fields for each whitelisted class. for (String filepath : filepaths) { String line; int number = -1; - try (LineNumberReader reader = new LineNumberReader( - new InputStreamReader(resource.getResourceAsStream(filepath), StandardCharsets.UTF_8))) { + try ( + LineNumberReader reader = new LineNumberReader( + new InputStreamReader(resource.getResourceAsStream(filepath), StandardCharsets.UTF_8) + ) + ) { String parseType = null; String whitelistClassOrigin = null; @@ -176,7 +179,8 @@ public static Whitelist loadFromResourceFiles(Class resource, Map resource, Map resource, Map resource, Map resource, Map resource, Map resource, Map resource, Map resource, Map resource, Map annotations; int annotationIndex = line.indexOf('@'); - annotations = annotationIndex == -1 ? - Collections.emptyList() : parseWhitelistAnnotations(parsers, line.substring(annotationIndex)); + annotations = annotationIndex == -1 + ? Collections.emptyList() + : parseWhitelistAnnotations(parsers, line.substring(annotationIndex)); - whitelistConstructors.add(new WhitelistConstructor( - origin, Arrays.asList(canonicalTypeNameParameters), annotations)); + whitelistConstructors.add( + new WhitelistConstructor(origin, Arrays.asList(canonicalTypeNameParameters), annotations) + ); - // Handle the case for a method or augmented method definition. - // Expects the following format: ID ID? ID '(' ( ID ( ',' ID )* )? ')' annotations? '\n' + // Handle the case for a method or augmented method definition. + // Expects the following format: ID ID? ID '(' ( ID ( ',' ID )* )? ')' annotations? '\n' } else if (line.contains("(")) { // Parse the tokens prior to the method parameters. int parameterStartIndex = line.indexOf('('); @@ -380,11 +420,13 @@ public static Whitelist loadFromResourceFiles(Class resource, Map resource, Map annotations; int annotationIndex = line.indexOf('@'); - annotations = annotationIndex == -1 ? - Collections.emptyList() : parseWhitelistAnnotations(parsers, line.substring(annotationIndex)); - - whitelistMethods.add(new WhitelistMethod(origin, javaAugmentedClassName, methodName, - returnCanonicalTypeName, Arrays.asList(canonicalTypeNameParameters), - annotations)); - - // Handle the case for a field definition. - // Expects the following format: ID ID annotations? '\n' + annotations = annotationIndex == -1 + ? Collections.emptyList() + : parseWhitelistAnnotations(parsers, line.substring(annotationIndex)); + + whitelistMethods.add( + new WhitelistMethod( + origin, + javaAugmentedClassName, + methodName, + returnCanonicalTypeName, + Arrays.asList(canonicalTypeNameParameters), + annotations + ) + ); + + // Handle the case for a field definition. + // Expects the following format: ID ID annotations? '\n' } else { // Parse the annotations if they exist. List annotations; @@ -439,13 +489,12 @@ public static Whitelist loadFromResourceFiles(Class resource, Map)resource::getClassLoader); + ClassLoader loader = AccessController.doPrivileged((PrivilegedAction) resource::getClassLoader); return new Whitelist(loader, whitelistClasses, whitelistStatics, whitelistClassBindings, Collections.emptyList()); } - private static List parseWhitelistAnnotations( - Map parsers, String line) { + private static List parseWhitelistAnnotations(Map parsers, String line) { List annotations; @@ -500,8 +549,9 @@ private static List parseWhitelistAnnotations( String argumentValue = argumentKeyValue[1]; - if (argumentValue.length() < 3 || argumentValue.charAt(0) != '"' || - argumentValue.charAt(argumentValue.length() - 1) != '"') { + if (argumentValue.length() < 3 + || argumentValue.charAt(0) != '"' + || argumentValue.charAt(argumentValue.length() - 1) != '"') { throw new IllegalArgumentException("invalid annotation: expected key=\"value\" [" + line + "]"); } diff --git a/modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/WhitelistMethod.java b/modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/WhitelistMethod.java index fa8145ba986cf..8451d1c9f3ef4 100644 --- a/modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/WhitelistMethod.java +++ b/modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/WhitelistMethod.java @@ -61,9 +61,14 @@ public class WhitelistMethod { * augmentedCanonicalClassName; augmentedCanonicalClassName will be {@code null} unless the method * is augmented as described in the class documentation. */ - public WhitelistMethod(String origin, String augmentedCanonicalClassName, String methodName, - String returnCanonicalTypeName, List canonicalTypeNameParameters, - List painlessAnnotations) { + public WhitelistMethod( + String origin, + String augmentedCanonicalClassName, + String methodName, + String returnCanonicalTypeName, + List canonicalTypeNameParameters, + List painlessAnnotations + ) { this.origin = Objects.requireNonNull(origin); this.augmentedCanonicalClassName = augmentedCanonicalClassName; @@ -74,9 +79,12 @@ public WhitelistMethod(String origin, String augmentedCanonicalClassName, String if (painlessAnnotations.isEmpty()) { this.painlessAnnotations = Collections.emptyMap(); } else { - this.painlessAnnotations = Collections.unmodifiableMap(Objects.requireNonNull(painlessAnnotations).stream() + this.painlessAnnotations = Collections.unmodifiableMap( + Objects.requireNonNull(painlessAnnotations) + .stream() .map(painlessAnnotation -> new AbstractMap.SimpleEntry<>(painlessAnnotation.getClass(), painlessAnnotation)) - .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))); + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)) + ); } } } diff --git a/modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/annotation/DynamicTypeAnnotationParser.java b/modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/annotation/DynamicTypeAnnotationParser.java index acd585a61426a..7a04c909bc173 100644 --- a/modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/annotation/DynamicTypeAnnotationParser.java +++ b/modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/annotation/DynamicTypeAnnotationParser.java @@ -20,7 +20,7 @@ private DynamicTypeAnnotationParser() {} public Object parse(Map arguments) { if (arguments.isEmpty() == false) { throw new IllegalArgumentException( - "unexpected parameters for [@" + DynamicTypeAnnotation.NAME + "] annotation, found " + arguments + "unexpected parameters for [@" + DynamicTypeAnnotation.NAME + "] annotation, found " + arguments ); } diff --git a/modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/annotation/InjectConstantAnnotation.java b/modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/annotation/InjectConstantAnnotation.java index a229f5db0ea7e..d33426fe0ef48 100644 --- a/modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/annotation/InjectConstantAnnotation.java +++ b/modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/annotation/InjectConstantAnnotation.java @@ -19,6 +19,7 @@ public class InjectConstantAnnotation { public static final String NAME = "inject_constant"; public final List injects; + public InjectConstantAnnotation(List injects) { this.injects = Collections.unmodifiableList(injects); } diff --git a/modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/annotation/WhitelistAnnotationParser.java b/modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/annotation/WhitelistAnnotationParser.java index ab3a19ffff29d..339dadd6375a7 100644 --- a/modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/annotation/WhitelistAnnotationParser.java +++ b/modules/lang-painless/spi/src/main/java/org/elasticsearch/painless/spi/annotation/WhitelistAnnotationParser.java @@ -21,15 +21,15 @@ public interface WhitelistAnnotationParser { Map BASE_ANNOTATION_PARSERS = Collections.unmodifiableMap( - Stream.of( - new AbstractMap.SimpleEntry<>(NoImportAnnotation.NAME, NoImportAnnotationParser.INSTANCE), - new AbstractMap.SimpleEntry<>(DeprecatedAnnotation.NAME, DeprecatedAnnotationParser.INSTANCE), - new AbstractMap.SimpleEntry<>(NonDeterministicAnnotation.NAME, NonDeterministicAnnotationParser.INSTANCE), - new AbstractMap.SimpleEntry<>(InjectConstantAnnotation.NAME, InjectConstantAnnotationParser.INSTANCE), - new AbstractMap.SimpleEntry<>(CompileTimeOnlyAnnotation.NAME, CompileTimeOnlyAnnotationParser.INSTANCE), - new AbstractMap.SimpleEntry<>(AugmentedAnnotation.NAME, AugmentedAnnotationParser.INSTANCE), - new AbstractMap.SimpleEntry<>(DynamicTypeAnnotation.NAME, DynamicTypeAnnotationParser.INSTANCE) - ).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)) + Stream.of( + new AbstractMap.SimpleEntry<>(NoImportAnnotation.NAME, NoImportAnnotationParser.INSTANCE), + new AbstractMap.SimpleEntry<>(DeprecatedAnnotation.NAME, DeprecatedAnnotationParser.INSTANCE), + new AbstractMap.SimpleEntry<>(NonDeterministicAnnotation.NAME, NonDeterministicAnnotationParser.INSTANCE), + new AbstractMap.SimpleEntry<>(InjectConstantAnnotation.NAME, InjectConstantAnnotationParser.INSTANCE), + new AbstractMap.SimpleEntry<>(CompileTimeOnlyAnnotation.NAME, CompileTimeOnlyAnnotationParser.INSTANCE), + new AbstractMap.SimpleEntry<>(AugmentedAnnotation.NAME, AugmentedAnnotationParser.INSTANCE), + new AbstractMap.SimpleEntry<>(DynamicTypeAnnotation.NAME, DynamicTypeAnnotationParser.INSTANCE) + ).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)) ); Object parse(Map arguments); diff --git a/modules/lang-painless/spi/src/test/java/org/elasticsearch/painless/WhitelistLoaderTests.java b/modules/lang-painless/spi/src/test/java/org/elasticsearch/painless/WhitelistLoaderTests.java index 0a77bd77aee17..ddd69bb828a61 100644 --- a/modules/lang-painless/spi/src/test/java/org/elasticsearch/painless/WhitelistLoaderTests.java +++ b/modules/lang-painless/spi/src/test/java/org/elasticsearch/painless/WhitelistLoaderTests.java @@ -25,17 +25,23 @@ public class WhitelistLoaderTests extends ESTestCase { public void testUnknownAnnotations() { Map parsers = new HashMap<>(WhitelistAnnotationParser.BASE_ANNOTATION_PARSERS); - RuntimeException expected = expectThrows(RuntimeException.class, () -> { - WhitelistLoader.loadFromResourceFiles(Whitelist.class, parsers, "org.elasticsearch.painless.annotation.unknown"); - }); - assertEquals( - "invalid annotation: parser not found for [unknownAnnotation] [@unknownAnnotation]", expected.getCause().getMessage() + RuntimeException expected = expectThrows( + RuntimeException.class, + () -> { WhitelistLoader.loadFromResourceFiles(Whitelist.class, parsers, "org.elasticsearch.painless.annotation.unknown"); } ); + assertEquals("invalid annotation: parser not found for [unknownAnnotation] [@unknownAnnotation]", expected.getCause().getMessage()); assertEquals(IllegalArgumentException.class, expected.getCause().getClass()); - expected = expectThrows(RuntimeException.class, () -> { - WhitelistLoader.loadFromResourceFiles(Whitelist.class, parsers, "org.elasticsearch.painless.annotation.unknown_with_options"); - }); + expected = expectThrows( + RuntimeException.class, + () -> { + WhitelistLoader.loadFromResourceFiles( + Whitelist.class, + parsers, + "org.elasticsearch.painless.annotation.unknown_with_options" + ); + } + ); assertEquals( "invalid annotation: parser not found for [unknownAnnotationWithMessage] [@unknownAnnotationWithMessage[arg=\"arg value\"]]", expected.getCause().getMessage() @@ -60,16 +66,18 @@ public void testAnnotations() { for (WhitelistMethod whitelistMethod : whitelistClass.whitelistMethods) { if ("deprecatedMethod".equals(whitelistMethod.methodName)) { - assertEquals("use another method", - ((DeprecatedAnnotation)whitelistMethod.painlessAnnotations.get(DeprecatedAnnotation.class)).getMessage()); + assertEquals( + "use another method", + ((DeprecatedAnnotation) whitelistMethod.painlessAnnotations.get(DeprecatedAnnotation.class)).getMessage() + ); assertEquals(1, whitelistMethod.painlessAnnotations.size()); ++count; } if ("annotatedTestMethod".equals(whitelistMethod.methodName)) { - AnnotationTestObject.TestAnnotation ta = - ((AnnotationTestObject.TestAnnotation)whitelistMethod.painlessAnnotations.get( - AnnotationTestObject.TestAnnotation.class)); + AnnotationTestObject.TestAnnotation ta = ((AnnotationTestObject.TestAnnotation) whitelistMethod.painlessAnnotations.get( + AnnotationTestObject.TestAnnotation.class + )); assertEquals("one", ta.getOne()); assertEquals("two", ta.getTwo()); assertEquals("three", ta.getThree()); @@ -78,11 +86,13 @@ public void testAnnotations() { } if ("annotatedMultipleMethod".equals(whitelistMethod.methodName)) { - assertEquals("test", - ((DeprecatedAnnotation)whitelistMethod.painlessAnnotations.get(DeprecatedAnnotation.class)).getMessage()); - AnnotationTestObject.TestAnnotation ta = - ((AnnotationTestObject.TestAnnotation)whitelistMethod.painlessAnnotations.get( - AnnotationTestObject.TestAnnotation.class)); + assertEquals( + "test", + ((DeprecatedAnnotation) whitelistMethod.painlessAnnotations.get(DeprecatedAnnotation.class)).getMessage() + ); + AnnotationTestObject.TestAnnotation ta = ((AnnotationTestObject.TestAnnotation) whitelistMethod.painlessAnnotations.get( + AnnotationTestObject.TestAnnotation.class + )); assertEquals("one", ta.getOne()); assertEquals("two", ta.getTwo()); assertEquals("three", ta.getThree()); diff --git a/modules/lang-painless/src/doc/java/org/elasticsearch/painless/ContextApiSpecGenerator.java b/modules/lang-painless/src/doc/java/org/elasticsearch/painless/ContextApiSpecGenerator.java index 4da95c70d0a12..08430e95f6607 100644 --- a/modules/lang-painless/src/doc/java/org/elasticsearch/painless/ContextApiSpecGenerator.java +++ b/modules/lang-painless/src/doc/java/org/elasticsearch/painless/ContextApiSpecGenerator.java @@ -8,12 +8,12 @@ package org.elasticsearch.painless; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.core.PathUtils; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.painless.action.PainlessContextInfo; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; import java.io.FileInputStream; import java.io.IOException; @@ -41,9 +41,13 @@ public static void main(String[] args) throws IOException { } Path json = rootDir.resolve("painless-common.json"); - try (PrintStream jsonStream = new PrintStream( - Files.newOutputStream(json, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE), - false, StandardCharsets.UTF_8.name())) { + try ( + PrintStream jsonStream = new PrintStream( + Files.newOutputStream(json, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE), + false, + StandardCharsets.UTF_8.name() + ) + ) { XContentBuilder builder = XContentFactory.jsonBuilder(jsonStream); builder.startObject(); @@ -54,9 +58,13 @@ public static void main(String[] args) throws IOException { for (PainlessInfoJson.Context context : infos.contexts) { json = rootDir.resolve("painless-" + context.getName() + ".json"); - try (PrintStream jsonStream = new PrintStream( - Files.newOutputStream(json, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE), - false, StandardCharsets.UTF_8.name())) { + try ( + PrintStream jsonStream = new PrintStream( + Files.newOutputStream(json, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE), + false, + StandardCharsets.UTF_8.name() + ) + ) { XContentBuilder builder = XContentFactory.jsonBuilder(jsonStream); context.toXContent(builder, null); @@ -85,12 +93,10 @@ private static JavaClassFilesystemResolver getJdkSrc() { return new JavaClassFilesystemResolver(PathUtils.get(jdksrc)); } HashMap packageSources = new HashMap<>(); - for (String packageSourceString: packageSourcesString.split(";")) { + for (String packageSourceString : packageSourcesString.split(";")) { String[] packageSource = packageSourceString.split(":", 2); if (packageSource.length != 2) { - throw new IllegalArgumentException( - "Bad format for packageSources. Format :;: ..." - ); + throw new IllegalArgumentException("Bad format for packageSources. Format :;: ..."); } packageSources.put(packageSource[0], PathUtils.get(packageSource[1])); } diff --git a/modules/lang-painless/src/doc/java/org/elasticsearch/painless/ContextDocGenerator.java b/modules/lang-painless/src/doc/java/org/elasticsearch/painless/ContextDocGenerator.java index 227c5924dafba..8b363aa1d3f08 100644 --- a/modules/lang-painless/src/doc/java/org/elasticsearch/painless/ContextDocGenerator.java +++ b/modules/lang-painless/src/doc/java/org/elasticsearch/painless/ContextDocGenerator.java @@ -8,8 +8,8 @@ package org.elasticsearch.painless; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.core.PathUtils; +import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.painless.action.PainlessContextClassBindingInfo; import org.elasticsearch.painless.action.PainlessContextClassInfo; @@ -102,9 +102,11 @@ private static Set createSharedStatics(List context } } - return staticInfoCounts.entrySet().stream().filter( - e -> e.getValue() == contextInfos.size() - ).map(Map.Entry::getKey).collect(Collectors.toSet()); + return staticInfoCounts.entrySet() + .stream() + .filter(e -> e.getValue() == contextInfos.size()) + .map(Map.Entry::getKey) + .collect(Collectors.toSet()); } private static List createContextStatics(PainlessContextInfo contextInfo) { @@ -126,9 +128,11 @@ private static Set createSharedClasses(List e.getValue() == contextInfos.size() - ).map(Map.Entry::getKey).collect(Collectors.toSet()); + return classInfoCounts.entrySet() + .stream() + .filter(e -> e.getValue() == contextInfos.size()) + .map(Map.Entry::getKey) + .collect(Collectors.toSet()); } @SuppressForbidden(reason = "resolve api docs directory with environment") @@ -159,14 +163,22 @@ private static void printAutomatedMessage(PrintStream stream) { stream.println(); } - private static void printSharedIndexPage(Path sharedDir, Map javaNamesToDisplayNames, - List staticInfos, List classInfos) throws IOException { + private static void printSharedIndexPage( + Path sharedDir, + Map javaNamesToDisplayNames, + List staticInfos, + List classInfos + ) throws IOException { Path sharedIndexPath = sharedDir.resolve("index.asciidoc"); - try (PrintStream sharedIndexStream = new PrintStream( + try ( + PrintStream sharedIndexStream = new PrintStream( Files.newOutputStream(sharedIndexPath, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE), - false, StandardCharsets.UTF_8.name())) { + false, + StandardCharsets.UTF_8.name() + ) + ) { printAutomatedMessage(sharedIndexStream); @@ -179,14 +191,23 @@ private static void printSharedIndexPage(Path sharedDir, Map jav } } - private static void printContextIndexPage(Path contextDir, Map javaNamesToDisplayNames, - PainlessContextInfo contextInfo, List staticInfos, List classInfos) throws IOException { + private static void printContextIndexPage( + Path contextDir, + Map javaNamesToDisplayNames, + PainlessContextInfo contextInfo, + List staticInfos, + List classInfos + ) throws IOException { Path contextIndexPath = contextDir.resolve("index.asciidoc"); - try (PrintStream contextIndexStream = new PrintStream( + try ( + PrintStream contextIndexStream = new PrintStream( Files.newOutputStream(contextIndexPath, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE), - false, StandardCharsets.UTF_8.name())) { + false, + StandardCharsets.UTF_8.name() + ) + ) { printAutomatedMessage(contextIndexStream); @@ -196,31 +217,39 @@ private static void printContextIndexPage(Path contextDir, Map j contextIndexStream.println("The following specialized API is available in the " + getContextName(contextInfo) + " context."); contextIndexStream.println(); contextIndexStream.println( - "* See the <<" + SHARED_HEADER + ", " + SHARED_NAME + " API>> for further API available in all contexts."); + "* See the <<" + SHARED_HEADER + ", " + SHARED_NAME + " API>> for further API available in all contexts." + ); printIndex(contextIndexStream, getContextHeader(contextInfo), javaNamesToDisplayNames, staticInfos, classInfos); } } - private static void printIndex(PrintStream indexStream, String contextHeader, Map javaNamesToDisplayNames, - List staticInfos, List classInfos) { + private static void printIndex( + PrintStream indexStream, + String contextHeader, + Map javaNamesToDisplayNames, + List staticInfos, + List classInfos + ) { String currentPackageName = null; if (staticInfos.isEmpty() == false) { indexStream.println(); indexStream.println("==== Static Methods"); - indexStream.println("The following methods are directly callable without a class/instance qualifier. " + - "Note parameters denoted by a (*) are treated as read-only values."); + indexStream.println( + "The following methods are directly callable without a class/instance qualifier. " + + "Note parameters denoted by a (*) are treated as read-only values." + ); indexStream.println(); for (Object staticInfo : staticInfos) { if (staticInfo instanceof PainlessContextMethodInfo) { - printMethod(indexStream, javaNamesToDisplayNames, false, (PainlessContextMethodInfo)staticInfo); + printMethod(indexStream, javaNamesToDisplayNames, false, (PainlessContextMethodInfo) staticInfo); } else if (staticInfo instanceof PainlessContextClassBindingInfo) { - printClassBinding(indexStream, javaNamesToDisplayNames, (PainlessContextClassBindingInfo)staticInfo); + printClassBinding(indexStream, javaNamesToDisplayNames, (PainlessContextClassBindingInfo) staticInfo); } else if (staticInfo instanceof PainlessContextInstanceBindingInfo) { - printInstanceBinding(indexStream, javaNamesToDisplayNames, (PainlessContextInstanceBindingInfo)staticInfo); + printInstanceBinding(indexStream, javaNamesToDisplayNames, (PainlessContextInstanceBindingInfo) staticInfo); } else { throw new IllegalArgumentException("unexpected static info type"); } @@ -230,8 +259,10 @@ private static void printIndex(PrintStream indexStream, String contextHeader, Ma if (classInfos.isEmpty() == false) { indexStream.println(); indexStream.println("==== Classes By Package"); - indexStream.println("The following classes are available grouped by their respective packages. Click on a class " + - "to view details about the available methods and fields."); + indexStream.println( + "The following classes are available grouped by their respective packages. Click on a class " + + "to view details about the available methods and fields." + ); indexStream.println(); for (PainlessContextClassInfo classInfo : classInfos) { @@ -242,8 +273,14 @@ private static void printIndex(PrintStream indexStream, String contextHeader, Ma indexStream.println(); indexStream.println("==== " + currentPackageName); - indexStream.println("<<" + getPackageHeader(contextHeader, currentPackageName) + ", " + - "Expand details for " + currentPackageName + ">>"); + indexStream.println( + "<<" + + getPackageHeader(contextHeader, currentPackageName) + + ", " + + "Expand details for " + + currentPackageName + + ">>" + ); indexStream.println(); } @@ -258,38 +295,64 @@ private static void printIndex(PrintStream indexStream, String contextHeader, Ma } private static void printSharedPackagesPages( - Path sharedDir, Map javaNamesToDisplayNames, List classInfos) throws IOException { + Path sharedDir, + Map javaNamesToDisplayNames, + List classInfos + ) throws IOException { Path sharedClassesPath = sharedDir.resolve("packages.asciidoc"); - try (PrintStream sharedPackagesStream = new PrintStream( + try ( + PrintStream sharedPackagesStream = new PrintStream( Files.newOutputStream(sharedClassesPath, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE), - false, StandardCharsets.UTF_8.name())) { + false, + StandardCharsets.UTF_8.name() + ) + ) { printAutomatedMessage(sharedPackagesStream); printPackages(sharedPackagesStream, SHARED_NAME, SHARED_HEADER, javaNamesToDisplayNames, Collections.emptySet(), classInfos); } } - private static void printContextPackagesPages(Path contextDir, Map javaNamesToDisplayNames, - Set excludes, PainlessContextInfo contextInfo, List classInfos) - throws IOException { + private static void printContextPackagesPages( + Path contextDir, + Map javaNamesToDisplayNames, + Set excludes, + PainlessContextInfo contextInfo, + List classInfos + ) throws IOException { Path contextPackagesPath = contextDir.resolve("packages.asciidoc"); - try (PrintStream contextPackagesStream = new PrintStream( + try ( + PrintStream contextPackagesStream = new PrintStream( Files.newOutputStream(contextPackagesPath, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE), - false, StandardCharsets.UTF_8.name())) { + false, + StandardCharsets.UTF_8.name() + ) + ) { printAutomatedMessage(contextPackagesStream); - printPackages(contextPackagesStream, - getContextName(contextInfo), getContextHeader(contextInfo), javaNamesToDisplayNames, excludes, classInfos); + printPackages( + contextPackagesStream, + getContextName(contextInfo), + getContextHeader(contextInfo), + javaNamesToDisplayNames, + excludes, + classInfos + ); } } - private static void printPackages(PrintStream packagesStream, String contextName, String contextHeader, - Map javaNamesToDisplayNames, Set excludes, List classInfos) - { + private static void printPackages( + PrintStream packagesStream, + String contextName, + String contextHeader, + Map javaNamesToDisplayNames, + Set excludes, + List classInfos + ) { String currentPackageName = null; @@ -306,8 +369,9 @@ private static void printPackages(PrintStream packagesStream, String contextName packagesStream.println(); packagesStream.println("[role=\"exclude\",id=\"" + getPackageHeader(contextHeader, currentPackageName) + "\"]"); packagesStream.println("=== " + contextName + " API for package " + currentPackageName); - packagesStream.println("See the <<" + contextHeader + ", " + contextName + " API>> " + - "for a high-level overview of all packages and classes."); + packagesStream.println( + "See the <<" + contextHeader + ", " + contextName + " API>> " + "for a high-level overview of all packages and classes." + ); } String className = ContextGeneratorCommon.getType(javaNamesToDisplayNames, classInfo.getName()); @@ -341,13 +405,17 @@ private static void printPackages(PrintStream packagesStream, String contextName packagesStream.println(); } - private static void printRootIndexPage(Path rootDir, - List contextInfos, Set isSpecialized) throws IOException { + private static void printRootIndexPage(Path rootDir, List contextInfos, Set isSpecialized) + throws IOException { Path rootIndexPath = rootDir.resolve("index.asciidoc"); - try (PrintStream rootIndexStream = new PrintStream( + try ( + PrintStream rootIndexStream = new PrintStream( Files.newOutputStream(rootIndexPath, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE), - false, StandardCharsets.UTF_8.name())) { + false, + StandardCharsets.UTF_8.name() + ) + ) { printAutomatedMessage(rootIndexStream); @@ -382,8 +450,11 @@ private static void printRootIndexPage(Path rootDir, } private static void printConstructor( - PrintStream stream, Map javaNamesToDisplayNames, - String className, PainlessContextConstructorInfo constructorInfo) { + PrintStream stream, + Map javaNamesToDisplayNames, + String className, + PainlessContextConstructorInfo constructorInfo + ) { stream.print("* "); @@ -395,9 +466,7 @@ private static void printConstructor( stream.print("("); - for (int parameterIndex = 0; - parameterIndex < constructorInfo.getParameters().size(); - ++parameterIndex) { + for (int parameterIndex = 0; parameterIndex < constructorInfo.getParameters().size(); ++parameterIndex) { stream.print(ContextGeneratorCommon.getType(javaNamesToDisplayNames, constructorInfo.getParameters().get(parameterIndex))); @@ -410,8 +479,11 @@ private static void printConstructor( } private static void printMethod( - PrintStream stream, Map javaNamesToDisplayNames, - boolean isStatic, PainlessContextMethodInfo methodInfo) { + PrintStream stream, + Map javaNamesToDisplayNames, + boolean isStatic, + PainlessContextMethodInfo methodInfo + ) { stream.print("* " + (isStatic ? "static " : "")); stream.print(ContextGeneratorCommon.getType(javaNamesToDisplayNames, methodInfo.getRtn()) + " "); @@ -424,9 +496,7 @@ private static void printMethod( stream.print("("); - for (int parameterIndex = 0; - parameterIndex < methodInfo.getParameters().size(); - ++parameterIndex) { + for (int parameterIndex = 0; parameterIndex < methodInfo.getParameters().size(); ++parameterIndex) { stream.print(ContextGeneratorCommon.getType(javaNamesToDisplayNames, methodInfo.getParameters().get(parameterIndex))); @@ -439,19 +509,25 @@ private static void printMethod( } private static void printClassBinding( - PrintStream stream, Map javaNamesToDisplayNames, PainlessContextClassBindingInfo classBindingInfo) { - - stream.print("* " + - ContextGeneratorCommon.getType(javaNamesToDisplayNames, classBindingInfo.getRtn()) + - " " + - classBindingInfo.getName() + - "("); + PrintStream stream, + Map javaNamesToDisplayNames, + PainlessContextClassBindingInfo classBindingInfo + ) { + + stream.print( + "* " + + ContextGeneratorCommon.getType(javaNamesToDisplayNames, classBindingInfo.getRtn()) + + " " + + classBindingInfo.getName() + + "(" + ); for (int parameterIndex = 0; parameterIndex < classBindingInfo.getParameters().size(); ++parameterIndex) { // temporary fix to not print org.elasticsearch.script.ScoreScript parameter until // class instance bindings are created and the information is appropriately added to the context info classes if ("org.elasticsearch.script.ScoreScript".equals( - ContextGeneratorCommon.getType(javaNamesToDisplayNames, classBindingInfo.getParameters().get(parameterIndex)))) { + ContextGeneratorCommon.getType(javaNamesToDisplayNames, classBindingInfo.getParameters().get(parameterIndex)) + )) { continue; } @@ -470,13 +546,18 @@ private static void printClassBinding( } private static void printInstanceBinding( - PrintStream stream, Map javaNamesToDisplayNames, PainlessContextInstanceBindingInfo instanceBindingInfo) { - - stream.print("* " + - ContextGeneratorCommon.getType(javaNamesToDisplayNames, instanceBindingInfo.getRtn()) + - " " + - instanceBindingInfo.getName() + - "("); + PrintStream stream, + Map javaNamesToDisplayNames, + PainlessContextInstanceBindingInfo instanceBindingInfo + ) { + + stream.print( + "* " + + ContextGeneratorCommon.getType(javaNamesToDisplayNames, instanceBindingInfo.getRtn()) + + " " + + instanceBindingInfo.getName() + + "(" + ); for (int parameterIndex = 0; parameterIndex < instanceBindingInfo.getParameters().size(); ++parameterIndex) { stream.print(ContextGeneratorCommon.getType(javaNamesToDisplayNames, instanceBindingInfo.getParameters().get(parameterIndex))); @@ -490,8 +571,11 @@ private static void printInstanceBinding( } private static void printField( - PrintStream stream, Map javaNamesToDisplayNames, - boolean isStatic, PainlessContextFieldInfo fieldInfo) { + PrintStream stream, + Map javaNamesToDisplayNames, + boolean isStatic, + PainlessContextFieldInfo fieldInfo + ) { stream.print("* " + (isStatic ? "static " : "")); stream.print(ContextGeneratorCommon.getType(javaNamesToDisplayNames, fieldInfo.getType()) + " "); @@ -514,9 +598,7 @@ private static String getConstructorJavaDocLink(PainlessContextConstructorInfo c javaDocLink.append(constructorInfo.getDeclaring().replace('.', '/')); javaDocLink.append(".html#("); - for (int parameterIndex = 0; - parameterIndex < constructorInfo.getParameters().size(); - ++parameterIndex) { + for (int parameterIndex = 0; parameterIndex < constructorInfo.getParameters().size(); ++parameterIndex) { javaDocLink.append(getLinkType(constructorInfo.getParameters().get(parameterIndex))); @@ -539,9 +621,7 @@ private static String getMethodJavaDocLink(PainlessContextMethodInfo methodInfo) javaDocLink.append(methodInfo.getName()); javaDocLink.append("("); - for (int parameterIndex = 0; - parameterIndex < methodInfo.getParameters().size(); - ++parameterIndex) { + for (int parameterIndex = 0; parameterIndex < methodInfo.getParameters().size(); ++parameterIndex) { javaDocLink.append(getLinkType(methodInfo.getParameters().get(parameterIndex))); @@ -633,21 +713,21 @@ private static List sortStaticInfos(Set staticExcludes, List sortStaticInfos(Set staticExcludes, List sortClassInfos( - Set classExcludes, List classInfos) { + Set classExcludes, + List classInfos + ) { classInfos = new ArrayList<>(classInfos); - classInfos.removeIf(v -> - "void".equals(v.getName()) || "boolean".equals(v.getName()) || "byte".equals(v.getName()) || - "short".equals(v.getName()) || "char".equals(v.getName()) || "int".equals(v.getName()) || - "long".equals(v.getName()) || "float".equals(v.getName()) || "double".equals(v.getName()) || - "org.elasticsearch.painless.lookup.def".equals(v.getName()) || - isInternalClass(v.getName()) || classExcludes.contains(v) + classInfos.removeIf( + v -> "void".equals(v.getName()) + || "boolean".equals(v.getName()) + || "byte".equals(v.getName()) + || "short".equals(v.getName()) + || "char".equals(v.getName()) + || "int".equals(v.getName()) + || "long".equals(v.getName()) + || "float".equals(v.getName()) + || "double".equals(v.getName()) + || "org.elasticsearch.painless.lookup.def".equals(v.getName()) + || isInternalClass(v.getName()) + || classExcludes.contains(v) ); classInfos.sort((c1, c2) -> { @@ -704,8 +793,7 @@ private static Map getDisplayNames(List getDisplayNames(List getContextInfos() throws IOException { - URLConnection getContextNames = new URL( - "http://" + System.getProperty("cluster.uri") + "/_scripts/painless/_context").openConnection(); + URLConnection getContextNames = new URL("http://" + System.getProperty("cluster.uri") + "/_scripts/painless/_context") + .openConnection(); XContentParser parser = JsonXContent.jsonXContent.createParser(null, null, getContextNames.getInputStream()); parser.nextToken(); parser.nextToken(); @SuppressWarnings("unchecked") - List contextNames = (List)(Object)parser.list(); + List contextNames = (List) (Object) parser.list(); parser.close(); - ((HttpURLConnection)getContextNames).disconnect(); + ((HttpURLConnection) getContextNames).disconnect(); List contextInfos = new ArrayList<>(); for (String contextName : contextNames) { URLConnection getContextInfo = new URL( - "http://" + System.getProperty("cluster.uri") + "/_scripts/painless/_context?context=" + contextName).openConnection(); + "http://" + System.getProperty("cluster.uri") + "/_scripts/painless/_context?context=" + contextName + ).openConnection(); parser = JsonXContent.jsonXContent.createParser(null, null, getContextInfo.getInputStream()); contextInfos.add(PainlessContextInfo.fromXContent(parser)); - ((HttpURLConnection)getContextInfo).disconnect(); + ((HttpURLConnection) getContextInfo).disconnect(); } contextInfos.sort(Comparator.comparing(PainlessContextInfo::getName)); @@ -80,8 +81,7 @@ private static Map getDisplayNames(Collection getDisplayNames(Collection sortClassInfos(Collection unsortedClassInfos) { List classInfos = new ArrayList<>(unsortedClassInfos); - classInfos.removeIf(v -> - "void".equals(v.getName()) || "boolean".equals(v.getName()) || "byte".equals(v.getName()) || - "short".equals(v.getName()) || "char".equals(v.getName()) || "int".equals(v.getName()) || - "long".equals(v.getName()) || "float".equals(v.getName()) || "double".equals(v.getName()) || - "org.elasticsearch.painless.lookup.def".equals(v.getName()) || - isInternalClass(v.getName()) + classInfos.removeIf( + v -> "void".equals(v.getName()) + || "boolean".equals(v.getName()) + || "byte".equals(v.getName()) + || "short".equals(v.getName()) + || "char".equals(v.getName()) + || "int".equals(v.getName()) + || "long".equals(v.getName()) + || "float".equals(v.getName()) + || "double".equals(v.getName()) + || "org.elasticsearch.painless.lookup.def".equals(v.getName()) + || isInternalClass(v.getName()) ); classInfos.sort((c1, c2) -> { @@ -130,15 +136,15 @@ public static List sortClassInfos(Collection excludeCommonClassInfos( @@ -203,16 +209,18 @@ public PainlessInfos(List contextInfos, JavadocExtractor ex } } - private Set getCommon(List contexts, Function> getter) { + private Set getCommon(List contexts, Function> getter) { Map infoCounts = new HashMap<>(); for (PainlessContextInfo contextInfo : contexts) { for (T info : getter.apply(contextInfo)) { infoCounts.merge(info, 1, Integer::sum); } } - return infoCounts.entrySet().stream().filter( - e -> e.getValue() == contexts.size() - ).map(Map.Entry::getKey).collect(Collectors.toSet()); + return infoCounts.entrySet() + .stream() + .filter(e -> e.getValue() == contexts.size()) + .map(Map.Entry::getKey) + .collect(Collectors.toSet()); } } } diff --git a/modules/lang-painless/src/doc/java/org/elasticsearch/painless/JavadocExtractor.java b/modules/lang-painless/src/doc/java/org/elasticsearch/painless/JavadocExtractor.java index 0e933eb280336..fd74c2f7752c1 100644 --- a/modules/lang-painless/src/doc/java/org/elasticsearch/painless/JavadocExtractor.java +++ b/modules/lang-painless/src/doc/java/org/elasticsearch/painless/JavadocExtractor.java @@ -22,6 +22,7 @@ import com.github.javaparser.javadoc.description.JavadocDescription; import com.github.javaparser.javadoc.description.JavadocDescriptionElement; import com.github.javaparser.javadoc.description.JavadocInlineTag; + import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; @@ -44,22 +45,21 @@ public class JavadocExtractor { private final JavaClassResolver resolver; private final Map cache = new HashMap<>(); - private static final String GPLv2 = "This code is free software; you can redistribute it and/or" + - " modify it under the terms of the GNU General Public License version 2 only, as published" + - " by the Free Software Foundation."; + private static final String GPLv2 = "This code is free software; you can redistribute it and/or" + + " modify it under the terms of the GNU General Public License version 2 only, as published" + + " by the Free Software Foundation."; - private static final String ESv2 = "Copyright Elasticsearch B.V. and/or licensed to Elasticsearch" + - " B.V. under one or more contributor license agreements. Licensed under the Elastic License 2.0" + - " and the Server Side Public License, v 1; you may not use this file except in compliance with," + - " at your election, the Elastic License 2.0 or the Server Side Public License, v 1."; + private static final String ESv2 = "Copyright Elasticsearch B.V. and/or licensed to Elasticsearch" + + " B.V. under one or more contributor license agreements. Licensed under the Elastic License 2.0" + + " and the Server Side Public License, v 1; you may not use this file except in compliance with," + + " at your election, the Elastic License 2.0 or the Server Side Public License, v 1."; - private static final String[] LICENSES = new String[]{GPLv2, ESv2}; + private static final String[] LICENSES = new String[] { GPLv2, ESv2 }; public JavadocExtractor(JavaClassResolver resolver) { this.resolver = resolver; } - public ParsedJavaClass parseClass(String className) throws IOException { ParsedJavaClass parsed = cache.get(className); if (parsed != null) { @@ -86,7 +86,7 @@ public static class ParsedJavaClass { private boolean valid = false; private boolean validated = false; - public ParsedJavaClass(String ... licenses) { + public ParsedJavaClass(String... licenses) { methods = new HashMap<>(); fields = new HashMap<>(); constructors = new HashMap<>(); @@ -126,9 +126,7 @@ public ParsedMethod getAugmentedMethod(String methodName, String receiverType, L @Override public String toString() { - return "ParsedJavaClass{" + - "methods=" + methods + - '}'; + return "ParsedJavaClass{" + "methods=" + methods + '}'; } public void putMethod(MethodDeclaration declaration) { @@ -138,11 +136,8 @@ public void putMethod(MethodDeclaration declaration) { methods.put( MethodSignature.fromDeclaration(declaration), new ParsedMethod( - declaration.getJavadoc().map(JavadocExtractor::clean).orElse(null), - declaration.getParameters() - .stream() - .map(p -> stripTypeParameters(p.getName().asString())) - .collect(Collectors.toList()) + declaration.getJavadoc().map(JavadocExtractor::clean).orElse(null), + declaration.getParameters().stream().map(p -> stripTypeParameters(p.getName().asString())).collect(Collectors.toList()) ) ); } @@ -154,11 +149,8 @@ public void putConstructor(ConstructorDeclaration declaration) { constructors.put( declaration.getParameters().stream().map(p -> stripTypeParameters(p.getType().asString())).collect(Collectors.toList()), new ParsedMethod( - declaration.getJavadoc().map(JavadocExtractor::clean).orElse(null), - declaration.getParameters() - .stream() - .map(p -> p.getName().asString()) - .collect(Collectors.toList()) + declaration.getJavadoc().map(JavadocExtractor::clean).orElse(null), + declaration.getParameters().stream().map(p -> p.getName().asString()).collect(Collectors.toList()) ) ); } @@ -184,7 +176,7 @@ public void putField(FieldDeclaration declaration) { private static String stripTypeParameters(String type) { int start = 0; int count = 0; - for (int i=0; i parameterTypes) { public static MethodSignature fromDeclaration(MethodDeclaration declaration) { return new MethodSignature( - declaration.getNameAsString(), - declaration.getParameters() - .stream() - .map(p -> stripTypeParameters(p.getType().asString())) - .collect(Collectors.toList()) + declaration.getNameAsString(), + declaration.getParameters().stream().map(p -> stripTypeParameters(p.getType().asString())).collect(Collectors.toList()) ); } @@ -225,8 +214,7 @@ public boolean equals(Object o) { if (this == o) return true; if ((o instanceof MethodSignature) == false) return false; MethodSignature that = (MethodSignature) o; - return Objects.equals(name, that.name) && - Objects.equals(parameterTypes, that.parameterTypes); + return Objects.equals(name, that.name) && Objects.equals(parameterTypes, that.parameterTypes); } @Override @@ -286,10 +274,10 @@ public ParsedJavadoc asAugmented(String receiverName) { } public boolean isEmpty() { - return param.size() == 0 && - (description == null || description.isEmpty()) && - (returns == null || returns.isEmpty()) && - thrws.size() == 0; + return param.size() == 0 + && (description == null || description.isEmpty()) + && (returns == null || returns.isEmpty()) + && thrws.size() == 0; } @Override @@ -323,7 +311,7 @@ public static ParsedJavadoc clean(Javadoc javadoc) { List cleaned = new ArrayList<>(description.getElements().size() + tags.size()); cleaned.addAll(stripInlineTags(description)); ParsedJavadoc parsed = new ParsedJavadoc(cleaned(cleaned)); - for (JavadocBlockTag tag: tags) { + for (JavadocBlockTag tag : tags) { String tagName = tag.getTagName(); // https://docs.oracle.com/en/java/javase/14/docs/specs/javadoc/doc-comment-spec.html#standard-tags // ignore author, deprecated, hidden, provides, uses, see, serial*, since and version. @@ -348,9 +336,9 @@ private static String cleaned(List segments) { private static List stripInlineTags(JavadocDescription description) { List elements = description.getElements(); List stripped = new ArrayList<>(elements.size()); - for (JavadocDescriptionElement element: elements) { + for (JavadocDescriptionElement element : elements) { if (element instanceof JavadocInlineTag) { - stripped.add(((JavadocInlineTag)element).getContent()); + stripped.add(((JavadocInlineTag) element).getContent()); } else { stripped.add(element.toText()); } diff --git a/modules/lang-painless/src/doc/java/org/elasticsearch/painless/PainlessInfoJson.java b/modules/lang-painless/src/doc/java/org/elasticsearch/painless/PainlessInfoJson.java index c8185cfa57569..9dcfdba314f9f 100644 --- a/modules/lang-painless/src/doc/java/org/elasticsearch/painless/PainlessInfoJson.java +++ b/modules/lang-painless/src/doc/java/org/elasticsearch/painless/PainlessInfoJson.java @@ -8,9 +8,6 @@ package org.elasticsearch.painless; -import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.xcontent.ToXContentObject; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.painless.action.PainlessContextClassBindingInfo; import org.elasticsearch.painless.action.PainlessContextClassInfo; import org.elasticsearch.painless.action.PainlessContextConstructorInfo; @@ -18,6 +15,9 @@ import org.elasticsearch.painless.action.PainlessContextInfo; import org.elasticsearch.painless.action.PainlessContextInstanceBindingInfo; import org.elasticsearch.painless.action.PainlessContextMethodInfo; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.ToXContentObject; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.ArrayList; @@ -34,10 +34,10 @@ public static class Context implements ToXContentObject { private final List instanceBindings; public Context( - PainlessContextInfo info, - Set commonClassInfos, - Map javaNamesToDisplayNames, - JavadocExtractor extractor + PainlessContextInfo info, + Set commonClassInfos, + Map javaNamesToDisplayNames, + JavadocExtractor extractor ) throws IOException { this.name = info.getName(); List classInfos = ContextGeneratorCommon.excludeCommonClassInfos(commonClassInfos, info.getClasses()); @@ -50,9 +50,9 @@ public Context( } public Context( - PainlessContextInfo info, - Set commonClassInfos, - Map javaNamesToDisplayNames + PainlessContextInfo info, + Set commonClassInfos, + Map javaNamesToDisplayNames ) { this.name = info.getName(); List classInfos = ContextGeneratorCommon.excludeCommonClassInfos(commonClassInfos, info.getClasses()); @@ -91,13 +91,13 @@ public static class Class implements ToXContentObject { private final List fields; private Class( - String name, - boolean imported, - List constructors, - List staticMethods, - List methods, - List staticFields, - List fields + String name, + boolean imported, + List constructors, + List staticMethods, + List methods, + List staticFields, + List fields ) { this.name = name; this.imported = imported; @@ -133,7 +133,8 @@ public static List fromInfos( public static List fromInfos(List infos, Map javaNamesToDisplayNames) { List classes = new ArrayList<>(infos.size()); for (PainlessContextClassInfo info : infos) { - classes.add(new Class( + classes.add( + new Class( javaNamesToDisplayNames.get(info.getName()), info.isImported(), Constructor.fromInfos(info.getConstructors(), javaNamesToDisplayNames), @@ -141,7 +142,8 @@ public static List fromInfos(List infos, Map toDisplayParameterTypes(List rawParameterTypes, Map javaNamesToDisplayNames) { List displayParameterTypes = new ArrayList<>(rawParameterTypes.size()); - for (String rawParameterType: rawParameterTypes) { + for (String rawParameterType : rawParameterTypes) { displayParameterTypes.add(ContextGeneratorCommon.getType(javaNamesToDisplayNames, rawParameterType)); } return displayParameterTypes; @@ -181,12 +183,12 @@ public static class Method implements ToXContentObject { public static final ParseField JAVADOC = new ParseField("javadoc"); private Method( - String declaring, - String name, - String rtn, - JavadocExtractor.ParsedJavadoc javadoc, - List parameters, - List parameterNames + String declaring, + String name, + String rtn, + JavadocExtractor.ParsedJavadoc javadoc, + List parameters, + List parameterNames ) { this.declaring = declaring; this.name = name; @@ -198,7 +200,7 @@ private Method( public static List fromInfos(List infos, Map javaNamesToDisplayNames) { List methods = new ArrayList<>(infos.size()); - for (PainlessContextMethodInfo info: infos) { + for (PainlessContextMethodInfo info : infos) { String returnType = ContextGeneratorCommon.getType(javaNamesToDisplayNames, info.getRtn()); List parameterTypes = toDisplayParameterTypes(info.getParameters(), javaNamesToDisplayNames); methods.add(new Method(info.getDeclaring(), info.getName(), returnType, null, parameterTypes, null)); @@ -207,14 +209,14 @@ public static List fromInfos(List infos, Map< } public static List fromInfos( - List infos, - Map javaNamesToDisplayNames, - JavadocExtractor.ParsedJavaClass parsed, - JavadocExtractor extractor, - String className + List infos, + Map javaNamesToDisplayNames, + JavadocExtractor.ParsedJavaClass parsed, + JavadocExtractor extractor, + String className ) throws IOException { List methods = new ArrayList<>(infos.size()); - for (PainlessContextMethodInfo info: infos) { + for (PainlessContextMethodInfo info : infos) { JavadocExtractor.ParsedJavadoc javadoc = null; List parameterNames = null; @@ -234,14 +236,16 @@ public static List fromInfos( parameterNames = parsedMethod.parameterNames; } - methods.add(new Method( + methods.add( + new Method( info.getDeclaring(), name, ContextGeneratorCommon.getType(javaNamesToDisplayNames, info.getRtn()), javadoc, parameterTypes, parameterNames - )); + ) + ); } return methods; } @@ -275,10 +279,10 @@ public static class Constructor implements ToXContentObject { public static final ParseField PARAMETER_NAMES = new ParseField("parameter_names"); private Constructor( - String declaring, - List parameters, - List parameterNames, - JavadocExtractor.ParsedJavadoc javadoc + String declaring, + List parameters, + List parameterNames, + JavadocExtractor.ParsedJavadoc javadoc ) { this.declaring = declaring; this.parameters = parameters; @@ -288,7 +292,7 @@ private Constructor( public static List fromInfos(List infos, Map javaNamesToDisplayNames) { List constructors = new ArrayList<>(infos.size()); - for (PainlessContextConstructorInfo info: infos) { + for (PainlessContextConstructorInfo info : infos) { List parameterTypes = toDisplayParameterTypes(info.getParameters(), javaNamesToDisplayNames); constructors.add(new Constructor(info.getDeclaring(), parameterTypes, null, null)); } @@ -296,14 +300,14 @@ public static List fromInfos(List i } private static List fromInfos( - List infos, - Map javaNamesToDisplayNames, - JavadocExtractor.ParsedJavaClass parsed, - JavadocExtractor extractor, - String className + List infos, + Map javaNamesToDisplayNames, + JavadocExtractor.ParsedJavaClass parsed, + JavadocExtractor extractor, + String className ) throws IOException { List constructors = new ArrayList<>(infos.size()); - for (PainlessContextConstructorInfo info: infos) { + for (PainlessContextConstructorInfo info : infos) { List parameterTypes = toDisplayParameterTypes(info.getParameters(), javaNamesToDisplayNames); List parameterNames = null; JavadocExtractor.ParsedJavadoc javadoc = null; @@ -356,7 +360,7 @@ private Field(String declaring, String name, String type, String javadoc) { public static List fromInfos(List infos, Map javaNamesToDisplayNames) { List fields = new ArrayList<>(infos.size()); - for (PainlessContextFieldInfo info: infos) { + for (PainlessContextFieldInfo info : infos) { String type = ContextGeneratorCommon.getType(javaNamesToDisplayNames, info.getType()); fields.add(new Field(info.getDeclaring(), info.getName(), type, null)); } @@ -364,12 +368,12 @@ public static List fromInfos(List infos, Map fromInfos( - List infos, - Map javaNamesToDisplayNames, - JavadocExtractor.ParsedJavaClass pj + List infos, + Map javaNamesToDisplayNames, + JavadocExtractor.ParsedJavaClass pj ) { List fields = new ArrayList<>(infos.size()); - for (PainlessContextFieldInfo info: infos) { + for (PainlessContextFieldInfo info : infos) { String name = info.getName(); String type = ContextGeneratorCommon.getType(javaNamesToDisplayNames, info.getType()); fields.add(new Field(info.getDeclaring(), name, type, pj.getField(name))); diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/AnalyzerCaster.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/AnalyzerCaster.java index c3646b86a445b..00294dab64790 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/AnalyzerCaster.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/AnalyzerCaster.java @@ -378,17 +378,23 @@ public static PainlessCast getLegalCast(Location location, Class actual, Clas } } - if ( - (actual == def.class && expected != void.class) || - (actual != void.class && expected == def.class) || - expected.isAssignableFrom(actual) || - (actual.isAssignableFrom(expected) && explicit) - ) { + if ((actual == def.class && expected != void.class) + || (actual != void.class && expected == def.class) + || expected.isAssignableFrom(actual) + || (actual.isAssignableFrom(expected) && explicit)) { return PainlessCast.originalTypetoTargetType(actual, expected, explicit); } else { - throw location.createError(new ClassCastException("Cannot cast from " + - "[" + PainlessLookupUtility.typeToCanonicalTypeName(actual) + "] to " + - "[" + PainlessLookupUtility.typeToCanonicalTypeName(expected) + "].")); + throw location.createError( + new ClassCastException( + "Cannot cast from " + + "[" + + PainlessLookupUtility.typeToCanonicalTypeName(actual) + + "] to " + + "[" + + PainlessLookupUtility.typeToCanonicalTypeName(expected) + + "]." + ) + ); } } @@ -399,32 +405,43 @@ public static Object constCast(Location location, Object constant, PainlessCast if (fsort == tsort) { return constant; } else if (fsort == String.class && tsort == char.class) { - return Utility.StringTochar((String)constant); + return Utility.StringTochar((String) constant); } else if (fsort == char.class && tsort == String.class) { - return Utility.charToString((char)constant); + return Utility.charToString((char) constant); } else if (fsort.isPrimitive() && fsort != boolean.class && tsort.isPrimitive() && tsort != boolean.class) { Number number; if (fsort == char.class) { - number = (int)(char)constant; + number = (int) (char) constant; } else { - number = (Number)constant; + number = (Number) constant; } - if (tsort == byte.class) return number.byteValue(); + if (tsort == byte.class) return number.byteValue(); else if (tsort == short.class) return number.shortValue(); - else if (tsort == char.class) return (char)number.intValue(); + else if (tsort == char.class) return (char) number.intValue(); else if (tsort == int.class) return number.intValue(); else if (tsort == long.class) return number.longValue(); else if (tsort == float.class) return number.floatValue(); else if (tsort == double.class) return number.doubleValue(); else { - throw location.createError(new IllegalStateException("Cannot cast from " + - "[" + cast.originalType.getCanonicalName() + "] to [" + cast.targetType.getCanonicalName() + "].")); + throw location.createError( + new IllegalStateException( + "Cannot cast from " + + "[" + + cast.originalType.getCanonicalName() + + "] to [" + + cast.targetType.getCanonicalName() + + "]." + ) + ); } } else { - throw location.createError(new IllegalStateException("Cannot cast from " + - "[" + cast.originalType.getCanonicalName() + "] to [" + cast.targetType.getCanonicalName() + "].")); + throw location.createError( + new IllegalStateException( + "Cannot cast from " + "[" + cast.originalType.getCanonicalName() + "] to [" + cast.targetType.getCanonicalName() + "]." + ) + ); } } @@ -453,12 +470,16 @@ public static Class promoteNumeric(Class from0, Class from1, boolean de if (from0 == long.class || from1 == long.class) { return long.class; - } else if (from0 == int.class || from1 == int.class || - from0 == char.class || from1 == char.class || - from0 == short.class || from1 == short.class || - from0 == byte.class || from1 == byte.class) { - return int.class; - } + } else if (from0 == int.class + || from1 == int.class + || from0 == char.class + || from1 == char.class + || from0 == short.class + || from1 == short.class + || from0 == byte.class + || from1 == byte.class) { + return int.class; + } return null; } @@ -529,8 +550,8 @@ public static Class promoteConditional(Class from0, Class from1) { } } else if (from1 == char.class) { if (from0 == short.class || from0 == byte.class) { - return int.class; - } else { + return int.class; + } else { return null; } } else { diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/ClassWriter.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/ClassWriter.java index 8bfbbfa47e1c6..d274b294774dc 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/ClassWriter.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/ClassWriter.java @@ -23,7 +23,7 @@ * Manages the top level writers for class and possibly * clinit if necessary. */ -public class ClassWriter implements Closeable { +public class ClassWriter implements Closeable { /** * Converts Java reflection modifiers to ASM access constants. @@ -34,17 +34,17 @@ public class ClassWriter implements Closeable { public static int buildAccess(int modifiers, boolean synthetic) { int access = synthetic ? Opcodes.ACC_SYNTHETIC : 0; - if (Modifier.isFinal(modifiers)) access |= Opcodes.ACC_FINAL; - if (Modifier.isInterface(modifiers)) access |= Opcodes.ACC_INTERFACE; - if (Modifier.isNative(modifiers)) access |= Opcodes.ACC_NATIVE; - if (Modifier.isPrivate(modifiers)) access |= Opcodes.ACC_PRIVATE; - if (Modifier.isProtected(modifiers)) access |= Opcodes.ACC_PROTECTED; - if (Modifier.isPublic(modifiers)) access |= Opcodes.ACC_PUBLIC; - if (Modifier.isStatic(modifiers)) access |= Opcodes.ACC_STATIC; - if (Modifier.isStrict(modifiers)) access |= Opcodes.ACC_STRICT; + if (Modifier.isFinal(modifiers)) access |= Opcodes.ACC_FINAL; + if (Modifier.isInterface(modifiers)) access |= Opcodes.ACC_INTERFACE; + if (Modifier.isNative(modifiers)) access |= Opcodes.ACC_NATIVE; + if (Modifier.isPrivate(modifiers)) access |= Opcodes.ACC_PRIVATE; + if (Modifier.isProtected(modifiers)) access |= Opcodes.ACC_PROTECTED; + if (Modifier.isPublic(modifiers)) access |= Opcodes.ACC_PUBLIC; + if (Modifier.isStatic(modifiers)) access |= Opcodes.ACC_STATIC; + if (Modifier.isStrict(modifiers)) access |= Opcodes.ACC_STRICT; if (Modifier.isSynchronized(modifiers)) access |= Opcodes.ACC_SYNCHRONIZED; - if (Modifier.isTransient(modifiers)) access |= Opcodes.ACC_TRANSIENT; - if (Modifier.isVolatile(modifiers)) access |= Opcodes.ACC_VOLATILE; + if (Modifier.isTransient(modifiers)) access |= Opcodes.ACC_TRANSIENT; + if (Modifier.isVolatile(modifiers)) access |= Opcodes.ACC_VOLATILE; return access; } @@ -55,8 +55,16 @@ public static int buildAccess(int modifiers, boolean synthetic) { protected final org.objectweb.asm.ClassWriter classWriter; protected final ClassVisitor classVisitor; - public ClassWriter(CompilerSettings compilerSettings, BitSet statements, Printer debugStream, - Class baseClass, int classFrames, int classAccess, String className, String[] classInterfaces) { + public ClassWriter( + CompilerSettings compilerSettings, + BitSet statements, + Printer debugStream, + Class baseClass, + int classFrames, + int classAccess, + String className, + String[] classInterfaces + ) { this.compilerSettings = compilerSettings; this.statements = statements; @@ -73,8 +81,14 @@ public ClassWriter(CompilerSettings compilerSettings, BitSet statements, Printer } classVisitor = visitor; - classVisitor.visit(WriterConstants.CLASS_VERSION, classAccess, className, null, - Type.getType(baseClass).getInternalName(), classInterfaces); + classVisitor.visit( + WriterConstants.CLASS_VERSION, + classAccess, + className, + null, + Type.getType(baseClass).getInternalName(), + classInterfaces + ); } public ClassVisitor getClassVisitor() { diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/Compiler.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/Compiler.java index 6bcba534ad542..d30874e65f0f0 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/Compiler.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/Compiler.java @@ -214,7 +214,7 @@ ScriptScope compile(Loader loader, String name, String source, CompilerSettings new PainlessSemanticHeaderPhase().visitClass(root, scriptScope); new PainlessSemanticAnalysisPhase().visitClass(root, scriptScope); new PainlessUserTreeToIRTreePhase().visitClass(root, scriptScope); - ClassNode classNode = (ClassNode)scriptScope.getDecoration(root, IRNodeDecoration.class).getIRNode(); + ClassNode classNode = (ClassNode) scriptScope.getDecoration(root, IRNodeDecoration.class).getIRNode(); new DefaultStringConcatenationOptimizationPhase().visitClass(classNode, null); new DefaultConstantFoldingOptimizationPhase().visitClass(classNode, null); new DefaultStaticConstantExtractionPhase().visitClass(classNode, scriptScope); @@ -249,7 +249,7 @@ byte[] compile(String name, String source, CompilerSettings settings, Printer de new PainlessSemanticHeaderPhase().visitClass(root, scriptScope); new PainlessSemanticAnalysisPhase().visitClass(root, scriptScope); new PainlessUserTreeToIRTreePhase().visitClass(root, scriptScope); - ClassNode classNode = (ClassNode)scriptScope.getDecoration(root, IRNodeDecoration.class).getIRNode(); + ClassNode classNode = (ClassNode) scriptScope.getDecoration(root, IRNodeDecoration.class).getIRNode(); new DefaultStringConcatenationOptimizationPhase().visitClass(classNode, null); new DefaultConstantFoldingOptimizationPhase().visitClass(classNode, null); new DefaultStaticConstantExtractionPhase().visitClass(classNode, scriptScope); @@ -262,10 +262,15 @@ byte[] compile(String name, String source, CompilerSettings settings, Printer de /** * Runs the two-pass compiler to generate a Painless script with option visitors for each major phase. */ - byte[] compile(String name, String source, CompilerSettings settings, Printer debugStream, - UserTreeVisitor semanticPhaseVisitor, - UserTreeVisitor irPhaseVisitor, - IRTreeVisitor asmPhaseVisitor) { + byte[] compile( + String name, + String source, + CompilerSettings settings, + Printer debugStream, + UserTreeVisitor semanticPhaseVisitor, + UserTreeVisitor irPhaseVisitor, + IRTreeVisitor asmPhaseVisitor + ) { String scriptName = Location.computeSourceName(name); ScriptClassInfo scriptClassInfo = new ScriptClassInfo(painlessLookup, scriptClass); SClass root = Walker.buildPainlessTree(scriptName, source, settings); @@ -282,7 +287,7 @@ byte[] compile(String name, String source, CompilerSettings settings, Printer de irPhaseVisitor.visitClass(root, scriptScope); } - ClassNode classNode = (ClassNode)scriptScope.getDecoration(root, IRNodeDecoration.class).getIRNode(); + ClassNode classNode = (ClassNode) scriptScope.getDecoration(root, IRNodeDecoration.class).getIRNode(); new DefaultStringConcatenationOptimizationPhase().visitClass(classNode, null); new DefaultConstantFoldingOptimizationPhase().visitClass(classNode, null); new DefaultStaticConstantExtractionPhase().visitClass(classNode, scriptScope); diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/CompilerSettings.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/CompilerSettings.java index 9ca6f767e4ff0..946e9e85308dc 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/CompilerSettings.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/CompilerSettings.java @@ -24,14 +24,22 @@ public final class CompilerSettings { * disabled. If {@code use-limit}, the default, regexes are enabled but limited in complexity according to the * {@code script.painless.regex.limit-factor} setting. */ - public static final Setting REGEX_ENABLED = - new Setting<>("script.painless.regex.enabled", RegexEnabled.LIMITED.value, RegexEnabled::parse, Property.NodeScope); + public static final Setting REGEX_ENABLED = new Setting<>( + "script.painless.regex.enabled", + RegexEnabled.LIMITED.value, + RegexEnabled::parse, + Property.NodeScope + ); /** * How complex can a regex be? This is the number of characters that can be considered expressed as a multiple of string length. */ - public static final Setting REGEX_LIMIT_FACTOR = - Setting.intSetting("script.painless.regex.limit-factor", 6, 1, Property.NodeScope); + public static final Setting REGEX_LIMIT_FACTOR = Setting.intSetting( + "script.painless.regex.limit-factor", + 6, + 1, + Property.NodeScope + ); /** * Constant to be used when specifying the maximum loop counter when compiling a script. @@ -75,7 +83,6 @@ public final class CompilerSettings { */ private RegexEnabled regexesEnabled = RegexEnabled.LIMITED; - /** * How complex can regexes be? Expressed as a multiple of the input string. */ @@ -104,7 +111,7 @@ public void setMaxLoopCounter(int max) { * parsing problems. */ public boolean isPicky() { - return picky; + return picky; } /** @@ -112,7 +119,7 @@ public boolean isPicky() { * @see #isPicky */ public void setPicky(boolean picky) { - this.picky = picky; + this.picky = picky; } /** @@ -188,6 +195,7 @@ public enum RegexEnabled { TRUE("true"), FALSE("false"), LIMITED("limited"); + final String value; RegexEnabled(String value) { diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/Def.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/Def.java index b6f6c10a3859b..a58125340c9b3 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/Def.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/Def.java @@ -55,38 +55,75 @@ public final class Def { private static final class ArrayLengthHelper { private static final MethodHandles.Lookup PRIVATE_METHOD_HANDLES_LOOKUP = MethodHandles.lookup(); - private static final Map,MethodHandle> ARRAY_TYPE_MH_MAPPING = Collections.unmodifiableMap( - Stream.of(boolean[].class, byte[].class, short[].class, int[].class, long[].class, - char[].class, float[].class, double[].class, Object[].class) - .collect(Collectors.toMap(Function.identity(), type -> { - try { - return PRIVATE_METHOD_HANDLES_LOOKUP.findStatic( - PRIVATE_METHOD_HANDLES_LOOKUP.lookupClass(), "getArrayLength", MethodType.methodType(int.class, type)); - } catch (ReflectiveOperationException e) { - throw new AssertionError(e); - } - })) + private static final Map, MethodHandle> ARRAY_TYPE_MH_MAPPING = Collections.unmodifiableMap( + Stream.of( + boolean[].class, + byte[].class, + short[].class, + int[].class, + long[].class, + char[].class, + float[].class, + double[].class, + Object[].class + ).collect(Collectors.toMap(Function.identity(), type -> { + try { + return PRIVATE_METHOD_HANDLES_LOOKUP.findStatic( + PRIVATE_METHOD_HANDLES_LOOKUP.lookupClass(), + "getArrayLength", + MethodType.methodType(int.class, type) + ); + } catch (ReflectiveOperationException e) { + throw new AssertionError(e); + } + })) ); private static final MethodHandle OBJECT_ARRAY_MH = ARRAY_TYPE_MH_MAPPING.get(Object[].class); - static int getArrayLength(final boolean[] array) { return array.length; } - static int getArrayLength(final byte[] array) { return array.length; } - static int getArrayLength(final short[] array) { return array.length; } - static int getArrayLength(final int[] array) { return array.length; } - static int getArrayLength(final long[] array) { return array.length; } - static int getArrayLength(final char[] array) { return array.length; } - static int getArrayLength(final float[] array) { return array.length; } - static int getArrayLength(final double[] array) { return array.length; } - static int getArrayLength(final Object[] array) { return array.length; } + static int getArrayLength(final boolean[] array) { + return array.length; + } + + static int getArrayLength(final byte[] array) { + return array.length; + } + + static int getArrayLength(final short[] array) { + return array.length; + } + + static int getArrayLength(final int[] array) { + return array.length; + } + + static int getArrayLength(final long[] array) { + return array.length; + } + + static int getArrayLength(final char[] array) { + return array.length; + } + + static int getArrayLength(final float[] array) { + return array.length; + } + + static int getArrayLength(final double[] array) { + return array.length; + } + + static int getArrayLength(final Object[] array) { + return array.length; + } static MethodHandle arrayLengthGetter(Class arrayType) { if (arrayType.isArray() == false) { throw new IllegalArgumentException("type must be an array"); } - return (ARRAY_TYPE_MH_MAPPING.containsKey(arrayType)) ? - ARRAY_TYPE_MH_MAPPING.get(arrayType) : - OBJECT_ARRAY_MH.asType(OBJECT_ARRAY_MH.type().changeParameterType(0, arrayType)); + return (ARRAY_TYPE_MH_MAPPING.containsKey(arrayType)) + ? ARRAY_TYPE_MH_MAPPING.get(arrayType) + : OBJECT_ARRAY_MH.asType(OBJECT_ARRAY_MH.type().changeParameterType(0, arrayType)); } private ArrayLengthHelper() {} @@ -113,15 +150,21 @@ private ArrayLengthHelper() {} final MethodHandles.Lookup methodHandlesLookup = MethodHandles.publicLookup(); try { - MAP_GET = methodHandlesLookup.findVirtual(Map.class , "get", MethodType.methodType(Object.class, Object.class)); - MAP_PUT = methodHandlesLookup.findVirtual(Map.class , "put", MethodType.methodType(Object.class, Object.class, Object.class)); + MAP_GET = methodHandlesLookup.findVirtual(Map.class, "get", MethodType.methodType(Object.class, Object.class)); + MAP_PUT = methodHandlesLookup.findVirtual(Map.class, "put", MethodType.methodType(Object.class, Object.class, Object.class)); LIST_GET = methodHandlesLookup.findVirtual(List.class, "get", MethodType.methodType(Object.class, int.class)); LIST_SET = methodHandlesLookup.findVirtual(List.class, "set", MethodType.methodType(Object.class, int.class, Object.class)); ITERATOR = methodHandlesLookup.findVirtual(Iterable.class, "iterator", MethodType.methodType(Iterator.class)); - MAP_INDEX_NORMALIZE = methodHandlesLookup.findStatic(Def.class, "mapIndexNormalize", - MethodType.methodType(Object.class, Map.class, Object.class)); - LIST_INDEX_NORMALIZE = methodHandlesLookup.findStatic(Def.class, "listIndexNormalize", - MethodType.methodType(int.class, List.class, int.class)); + MAP_INDEX_NORMALIZE = methodHandlesLookup.findStatic( + Def.class, + "mapIndexNormalize", + MethodType.methodType(Object.class, Map.class, Object.class) + ); + LIST_INDEX_NORMALIZE = methodHandlesLookup.findStatic( + Def.class, + "listIndexNormalize", + MethodType.methodType(int.class, List.class, int.class) + ); } catch (final ReflectiveOperationException roe) { throw new AssertionError(roe); } @@ -130,8 +173,11 @@ private ArrayLengthHelper() {} // https://bugs.openjdk.java.net/browse/JDK-8156915 MethodHandle arrayLengthMHFactory; try { - arrayLengthMHFactory = methodHandlesLookup.findStatic(MethodHandles.class, "arrayLength", - MethodType.methodType(MethodHandle.class, Class.class)); + arrayLengthMHFactory = methodHandlesLookup.findStatic( + MethodHandles.class, + "arrayLength", + MethodType.methodType(MethodHandle.class, Class.class) + ); } catch (final ReflectiveOperationException roe) { arrayLengthMHFactory = null; } @@ -180,60 +226,76 @@ static MethodHandle arrayLengthGetter(Class arrayType) { * @throws IllegalArgumentException if no matching whitelisted method was found. * @throws Throwable if a method reference cannot be converted to an functional interface */ - static MethodHandle lookupMethod(PainlessLookup painlessLookup, FunctionTable functions, Map constants, - MethodHandles.Lookup methodHandlesLookup, MethodType callSiteType, Class receiverClass, String name, Object[] args) - throws Throwable { - - String recipeString = (String) args[0]; - int numArguments = callSiteType.parameterCount(); - // simple case: no lambdas - if (recipeString.isEmpty()) { - PainlessMethod painlessMethod = painlessLookup.lookupRuntimePainlessMethod(receiverClass, name, numArguments - 1); - - if (painlessMethod == null) { - throw new IllegalArgumentException("dynamic method " + - "[" + typeToCanonicalTypeName(receiverClass) + ", " + name + "/" + (numArguments - 1) + "] not found"); - } - - MethodHandle handle = painlessMethod.methodHandle; - Object[] injections = PainlessLookupUtility.buildInjections(painlessMethod, constants); - - if (injections.length > 0) { - // method handle contains the "this" pointer so start injections at 1 - handle = MethodHandles.insertArguments(handle, 1, injections); - } - - return handle; - } - - // convert recipe string to a bitset for convenience (the code below should be refactored...) - BitSet lambdaArgs = new BitSet(recipeString.length()); - for (int i = 0; i < recipeString.length(); i++) { - lambdaArgs.set(recipeString.charAt(i)); - } - - // otherwise: first we have to compute the "real" arity. This is because we have extra arguments: - // e.g. f(a, g(x), b, h(y), i()) looks like f(a, g, x, b, h, y, i). - int arity = callSiteType.parameterCount() - 1; - int upTo = 1; - for (int i = 1; i < numArguments; i++) { - if (lambdaArgs.get(i - 1)) { - Def.Encoding signature = new Def.Encoding((String) args[upTo++]); - arity -= signature.numCaptures; - // arity in painlessLookup does not include 'this' reference - if (signature.needsInstance) { - arity--; - } - } - } - - // lookup the method with the proper arity, then we know everything (e.g. interface types of parameters). - // based on these we can finally link any remaining lambdas that were deferred. - PainlessMethod method = painlessLookup.lookupRuntimePainlessMethod(receiverClass, name, arity); + static MethodHandle lookupMethod( + PainlessLookup painlessLookup, + FunctionTable functions, + Map constants, + MethodHandles.Lookup methodHandlesLookup, + MethodType callSiteType, + Class receiverClass, + String name, + Object[] args + ) throws Throwable { + + String recipeString = (String) args[0]; + int numArguments = callSiteType.parameterCount(); + // simple case: no lambdas + if (recipeString.isEmpty()) { + PainlessMethod painlessMethod = painlessLookup.lookupRuntimePainlessMethod(receiverClass, name, numArguments - 1); + + if (painlessMethod == null) { + throw new IllegalArgumentException( + "dynamic method " + + "[" + + typeToCanonicalTypeName(receiverClass) + + ", " + + name + + "/" + + (numArguments - 1) + + "] not found" + ); + } + + MethodHandle handle = painlessMethod.methodHandle; + Object[] injections = PainlessLookupUtility.buildInjections(painlessMethod, constants); + + if (injections.length > 0) { + // method handle contains the "this" pointer so start injections at 1 + handle = MethodHandles.insertArguments(handle, 1, injections); + } + + return handle; + } + + // convert recipe string to a bitset for convenience (the code below should be refactored...) + BitSet lambdaArgs = new BitSet(recipeString.length()); + for (int i = 0; i < recipeString.length(); i++) { + lambdaArgs.set(recipeString.charAt(i)); + } + + // otherwise: first we have to compute the "real" arity. This is because we have extra arguments: + // e.g. f(a, g(x), b, h(y), i()) looks like f(a, g, x, b, h, y, i). + int arity = callSiteType.parameterCount() - 1; + int upTo = 1; + for (int i = 1; i < numArguments; i++) { + if (lambdaArgs.get(i - 1)) { + Def.Encoding signature = new Def.Encoding((String) args[upTo++]); + arity -= signature.numCaptures; + // arity in painlessLookup does not include 'this' reference + if (signature.needsInstance) { + arity--; + } + } + } + + // lookup the method with the proper arity, then we know everything (e.g. interface types of parameters). + // based on these we can finally link any remaining lambdas that were deferred. + PainlessMethod method = painlessLookup.lookupRuntimePainlessMethod(receiverClass, name, arity); if (method == null) { throw new IllegalArgumentException( - "dynamic method [" + typeToCanonicalTypeName(receiverClass) + ", " + name + "/" + arity + "] not found"); + "dynamic method [" + typeToCanonicalTypeName(receiverClass) + ", " + name + "/" + arity + "] not found" + ); } MethodHandle handle = method.methodHandle; @@ -244,67 +306,76 @@ static MethodHandle lookupMethod(PainlessLookup painlessLookup, FunctionTable fu handle = MethodHandles.insertArguments(handle, 1, injections); } - int replaced = 0; - upTo = 1; - for (int i = 1; i < numArguments; i++) { - // its a functional reference, replace the argument with an impl - if (lambdaArgs.get(i - 1)) { - Def.Encoding defEncoding = new Encoding((String) args[upTo++]); - MethodHandle filter; - Class interfaceType = method.typeParameters.get(i - 1 - replaced - (defEncoding.needsInstance ? 1 : 0)); - if (defEncoding.isStatic) { - // the implementation is strongly typed, now that we know the interface type, - // we have everything. - filter = lookupReferenceInternal(painlessLookup, - functions, - constants, - methodHandlesLookup, - interfaceType, - defEncoding.symbol, - defEncoding.methodName, - defEncoding.numCaptures, - defEncoding.needsInstance - ); + int replaced = 0; + upTo = 1; + for (int i = 1; i < numArguments; i++) { + // its a functional reference, replace the argument with an impl + if (lambdaArgs.get(i - 1)) { + Def.Encoding defEncoding = new Encoding((String) args[upTo++]); + MethodHandle filter; + Class interfaceType = method.typeParameters.get(i - 1 - replaced - (defEncoding.needsInstance ? 1 : 0)); + if (defEncoding.isStatic) { + // the implementation is strongly typed, now that we know the interface type, + // we have everything. + filter = lookupReferenceInternal( + painlessLookup, + functions, + constants, + methodHandlesLookup, + interfaceType, + defEncoding.symbol, + defEncoding.methodName, + defEncoding.numCaptures, + defEncoding.needsInstance + ); } else { - // the interface type is now known, but we need to get the implementation. - // this is dynamically based on the receiver type (and cached separately, underneath - // this cache). It won't blow up since we never nest here (just references) - Class[] captures = new Class[defEncoding.numCaptures]; - for (int capture = 0; capture < captures.length; capture++) { - captures[capture] = callSiteType.parameterType(i + 1 + capture); - } - MethodType nestedType = MethodType.methodType(interfaceType, captures); - CallSite nested = DefBootstrap.bootstrap(painlessLookup, - functions, - constants, - methodHandlesLookup, - defEncoding.methodName, - nestedType, - 0, - DefBootstrap.REFERENCE, - PainlessLookupUtility.typeToCanonicalTypeName(interfaceType)); - filter = nested.dynamicInvoker(); + // the interface type is now known, but we need to get the implementation. + // this is dynamically based on the receiver type (and cached separately, underneath + // this cache). It won't blow up since we never nest here (just references) + Class[] captures = new Class[defEncoding.numCaptures]; + for (int capture = 0; capture < captures.length; capture++) { + captures[capture] = callSiteType.parameterType(i + 1 + capture); + } + MethodType nestedType = MethodType.methodType(interfaceType, captures); + CallSite nested = DefBootstrap.bootstrap( + painlessLookup, + functions, + constants, + methodHandlesLookup, + defEncoding.methodName, + nestedType, + 0, + DefBootstrap.REFERENCE, + PainlessLookupUtility.typeToCanonicalTypeName(interfaceType) + ); + filter = nested.dynamicInvoker(); } - // the filter now ignores the signature (placeholder) on the stack - filter = MethodHandles.dropArguments(filter, 0, String.class); - handle = MethodHandles.collectArguments(handle, i - (defEncoding.needsInstance ? 1 : 0), filter); - i += defEncoding.numCaptures; - replaced += defEncoding.numCaptures; - } - } - - return handle; - } - - /** - * Returns an implementation of interfaceClass that calls receiverClass.name - *

    - * This is just like LambdaMetaFactory, only with a dynamic type. The interface type is known, - * so we simply need to lookup the matching implementation method based on receiver type. - */ - static MethodHandle lookupReference(PainlessLookup painlessLookup, FunctionTable functions, Map constants, - MethodHandles.Lookup methodHandlesLookup, String interfaceClass, Class receiverClass, String name) - throws Throwable { + // the filter now ignores the signature (placeholder) on the stack + filter = MethodHandles.dropArguments(filter, 0, String.class); + handle = MethodHandles.collectArguments(handle, i - (defEncoding.needsInstance ? 1 : 0), filter); + i += defEncoding.numCaptures; + replaced += defEncoding.numCaptures; + } + } + + return handle; + } + + /** + * Returns an implementation of interfaceClass that calls receiverClass.name + *

    + * This is just like LambdaMetaFactory, only with a dynamic type. The interface type is known, + * so we simply need to lookup the matching implementation method based on receiver type. + */ + static MethodHandle lookupReference( + PainlessLookup painlessLookup, + FunctionTable functions, + Map constants, + MethodHandles.Lookup methodHandlesLookup, + String interfaceClass, + Class receiverClass, + String name + ) throws Throwable { Class interfaceType = painlessLookup.canonicalTypeNameToType(interfaceClass); if (interfaceType == null) { @@ -318,39 +389,64 @@ static MethodHandle lookupReference(PainlessLookup painlessLookup, FunctionTable PainlessMethod implMethod = painlessLookup.lookupRuntimePainlessMethod(receiverClass, name, arity); if (implMethod == null) { throw new IllegalArgumentException( - "dynamic method [" + typeToCanonicalTypeName(receiverClass) + ", " + name + "/" + arity + "] not found"); - } - - return lookupReferenceInternal(painlessLookup, functions, constants, - methodHandlesLookup, interfaceType, PainlessLookupUtility.typeToCanonicalTypeName(implMethod.targetClass), - implMethod.javaMethod.getName(), 1, false); - } + "dynamic method [" + typeToCanonicalTypeName(receiverClass) + ", " + name + "/" + arity + "] not found" + ); + } + + return lookupReferenceInternal( + painlessLookup, + functions, + constants, + methodHandlesLookup, + interfaceType, + PainlessLookupUtility.typeToCanonicalTypeName(implMethod.targetClass), + implMethod.javaMethod.getName(), + 1, + false + ); + } - /** Returns a method handle to an implementation of clazz, given method reference signature. */ + /** Returns a method handle to an implementation of clazz, given method reference signature. */ private static MethodHandle lookupReferenceInternal( - PainlessLookup painlessLookup, FunctionTable functions, Map constants, - MethodHandles.Lookup methodHandlesLookup, Class clazz, String type, String call, int captures, - boolean needsScriptInstance) throws Throwable { - - final FunctionRef ref = - FunctionRef.create(painlessLookup, functions, null, clazz, type, call, captures, constants, needsScriptInstance); + PainlessLookup painlessLookup, + FunctionTable functions, + Map constants, + MethodHandles.Lookup methodHandlesLookup, + Class clazz, + String type, + String call, + int captures, + boolean needsScriptInstance + ) throws Throwable { + + final FunctionRef ref = FunctionRef.create( + painlessLookup, + functions, + null, + clazz, + type, + call, + captures, + constants, + needsScriptInstance + ); Class[] parameters = ref.factoryMethodParameters(needsScriptInstance ? methodHandlesLookup.lookupClass() : null); MethodType factoryMethodType = MethodType.methodType(clazz, parameters); final CallSite callSite = LambdaBootstrap.lambdaBootstrap( - methodHandlesLookup, - ref.interfaceMethodName, - factoryMethodType, - ref.interfaceMethodType, - ref.delegateClassName, - ref.delegateInvokeType, - ref.delegateMethodName, - ref.delegateMethodType, - ref.isDelegateInterface ? 1 : 0, - ref.isDelegateAugmented ? 1 : 0, - ref.delegateInjections + methodHandlesLookup, + ref.interfaceMethodName, + factoryMethodType, + ref.interfaceMethodType, + ref.delegateClassName, + ref.delegateInvokeType, + ref.delegateMethodName, + ref.delegateMethodType, + ref.isDelegateInterface ? 1 : 0, + ref.isDelegateAugmented ? 1 : 0, + ref.delegateInjections ); return callSite.dynamicInvoker().asType(MethodType.methodType(clazz, parameters)); - } + } /** * Looks up handle for a dynamic field getter (field load) @@ -406,8 +502,7 @@ static MethodHandle lookupGetter(PainlessLookup painlessLookup, Class receive } } - throw new IllegalArgumentException( - "dynamic getter [" + typeToCanonicalTypeName(receiverClass) + ", " + name + "] not found"); + throw new IllegalArgumentException("dynamic getter [" + typeToCanonicalTypeName(receiverClass) + ", " + name + "] not found"); } /** @@ -459,8 +554,7 @@ static MethodHandle lookupSetter(PainlessLookup painlessLookup, Class receive } } - throw new IllegalArgumentException( - "dynamic setter [" + typeToCanonicalTypeName(receiverClass) + ", " + name + "] not found"); + throw new IllegalArgumentException("dynamic setter [" + typeToCanonicalTypeName(receiverClass) + ", " + name + "] not found"); } /** @@ -479,8 +573,9 @@ static MethodHandle lookupIndexNormalize(Class receiverClass) { } else if (List.class.isAssignableFrom(receiverClass)) { return LIST_INDEX_NORMALIZE; } - throw new IllegalArgumentException("Attempting to address a non-array-like type " + - "[" + receiverClass.getCanonicalName() + "] as an array."); + throw new IllegalArgumentException( + "Attempting to address a non-array-like type " + "[" + receiverClass.getCanonicalName() + "] as an array." + ); } /** @@ -498,8 +593,9 @@ static MethodHandle lookupArrayStore(Class receiverClass) { } else if (List.class.isAssignableFrom(receiverClass)) { return LIST_SET; } - throw new IllegalArgumentException("Attempting to address a non-array type " + - "[" + receiverClass.getCanonicalName() + "] as an array."); + throw new IllegalArgumentException( + "Attempting to address a non-array type " + "[" + receiverClass.getCanonicalName() + "] as an array." + ); } /** @@ -517,8 +613,9 @@ static MethodHandle lookupArrayLoad(Class receiverClass) { } else if (List.class.isAssignableFrom(receiverClass)) { return LIST_GET; } - throw new IllegalArgumentException("Attempting to address a non-array type " + - "[" + receiverClass.getCanonicalName() + "] as an array."); + throw new IllegalArgumentException( + "Attempting to address a non-array type " + "[" + receiverClass.getCanonicalName() + "] as an array." + ); } /** Helper class for isolating MethodHandles and methods to get iterators over arrays @@ -529,17 +626,28 @@ static MethodHandle lookupArrayLoad(Class receiverClass) { private static final class ArrayIteratorHelper { private static final MethodHandles.Lookup PRIVATE_METHOD_HANDLES_LOOKUP = MethodHandles.lookup(); - private static final Map,MethodHandle> ARRAY_TYPE_MH_MAPPING = Collections.unmodifiableMap( - Stream.of(boolean[].class, byte[].class, short[].class, int[].class, long[].class, - char[].class, float[].class, double[].class, Object[].class) - .collect(Collectors.toMap(Function.identity(), type -> { - try { - return PRIVATE_METHOD_HANDLES_LOOKUP.findStatic( - PRIVATE_METHOD_HANDLES_LOOKUP.lookupClass(), "iterator", MethodType.methodType(Iterator.class, type)); - } catch (ReflectiveOperationException e) { - throw new AssertionError(e); - } - })) + private static final Map, MethodHandle> ARRAY_TYPE_MH_MAPPING = Collections.unmodifiableMap( + Stream.of( + boolean[].class, + byte[].class, + short[].class, + int[].class, + long[].class, + char[].class, + float[].class, + double[].class, + Object[].class + ).collect(Collectors.toMap(Function.identity(), type -> { + try { + return PRIVATE_METHOD_HANDLES_LOOKUP.findStatic( + PRIVATE_METHOD_HANDLES_LOOKUP.lookupClass(), + "iterator", + MethodType.methodType(Iterator.class, type) + ); + } catch (ReflectiveOperationException e) { + throw new AssertionError(e); + } + })) ); private static final MethodHandle OBJECT_ARRAY_MH = ARRAY_TYPE_MH_MAPPING.get(Object[].class); @@ -547,64 +655,144 @@ private static final class ArrayIteratorHelper { static Iterator iterator(final boolean[] array) { return new Iterator() { int index = 0; - @Override public boolean hasNext() { return index < array.length; } - @Override public Boolean next() { return array[index++]; } + + @Override + public boolean hasNext() { + return index < array.length; + } + + @Override + public Boolean next() { + return array[index++]; + } }; } + static Iterator iterator(final byte[] array) { return new Iterator() { int index = 0; - @Override public boolean hasNext() { return index < array.length; } - @Override public Byte next() { return array[index++]; } + + @Override + public boolean hasNext() { + return index < array.length; + } + + @Override + public Byte next() { + return array[index++]; + } }; } + static Iterator iterator(final short[] array) { return new Iterator() { int index = 0; - @Override public boolean hasNext() { return index < array.length; } - @Override public Short next() { return array[index++]; } + + @Override + public boolean hasNext() { + return index < array.length; + } + + @Override + public Short next() { + return array[index++]; + } }; } + static Iterator iterator(final int[] array) { return new Iterator() { int index = 0; - @Override public boolean hasNext() { return index < array.length; } - @Override public Integer next() { return array[index++]; } + + @Override + public boolean hasNext() { + return index < array.length; + } + + @Override + public Integer next() { + return array[index++]; + } }; } + static Iterator iterator(final long[] array) { return new Iterator() { int index = 0; - @Override public boolean hasNext() { return index < array.length; } - @Override public Long next() { return array[index++]; } + + @Override + public boolean hasNext() { + return index < array.length; + } + + @Override + public Long next() { + return array[index++]; + } }; } + static Iterator iterator(final char[] array) { return new Iterator() { int index = 0; - @Override public boolean hasNext() { return index < array.length; } - @Override public Character next() { return array[index++]; } + + @Override + public boolean hasNext() { + return index < array.length; + } + + @Override + public Character next() { + return array[index++]; + } }; } + static Iterator iterator(final float[] array) { return new Iterator() { int index = 0; - @Override public boolean hasNext() { return index < array.length; } - @Override public Float next() { return array[index++]; } + + @Override + public boolean hasNext() { + return index < array.length; + } + + @Override + public Float next() { + return array[index++]; + } }; } + static Iterator iterator(final double[] array) { return new Iterator() { int index = 0; - @Override public boolean hasNext() { return index < array.length; } - @Override public Double next() { return array[index++]; } + + @Override + public boolean hasNext() { + return index < array.length; + } + + @Override + public Double next() { + return array[index++]; + } }; } + static Iterator iterator(final Object[] array) { return new Iterator() { int index = 0; - @Override public boolean hasNext() { return index < array.length; } - @Override public Object next() { return array[index++]; } + + @Override + public boolean hasNext() { + return index < array.length; + } + + @Override + public Object next() { + return array[index++]; + } }; } @@ -612,13 +800,14 @@ static MethodHandle newIterator(Class arrayType) { if (arrayType.isArray() == false) { throw new IllegalArgumentException("type must be an array"); } - return (ARRAY_TYPE_MH_MAPPING.containsKey(arrayType)) ? - ARRAY_TYPE_MH_MAPPING.get(arrayType) : - OBJECT_ARRAY_MH.asType(OBJECT_ARRAY_MH.type().changeParameterType(0, arrayType)); + return (ARRAY_TYPE_MH_MAPPING.containsKey(arrayType)) + ? ARRAY_TYPE_MH_MAPPING.get(arrayType) + : OBJECT_ARRAY_MH.asType(OBJECT_ARRAY_MH.type().changeParameterType(0, arrayType)); } private ArrayIteratorHelper() {} } + /** * Returns a method handle to do iteration (for enhanced for loop) * @param receiverClass Class of the array to load the value from @@ -638,255 +827,301 @@ static MethodHandle lookupIterator(Class receiverClass) { public static boolean defToboolean(final Object value) { if (value instanceof Boolean) { - return (boolean)value; + return (boolean) value; } else { - throw new ClassCastException("cannot cast " + - "def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " + - boolean.class.getCanonicalName()); + throw new ClassCastException( + "cannot cast " + + "def [" + + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + + "] to " + + boolean.class.getCanonicalName() + ); } } public static byte defTobyteImplicit(final Object value) { if (value instanceof Byte) { - return (byte)value; + return (byte) value; } else { - throw new ClassCastException("cannot implicitly cast " + - "def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " + - byte.class.getCanonicalName()); + throw new ClassCastException( + "cannot implicitly cast " + + "def [" + + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + + "] to " + + byte.class.getCanonicalName() + ); } } public static short defToshortImplicit(final Object value) { if (value instanceof Byte) { - return (byte)value; + return (byte) value; } else if (value instanceof Short) { - return (short)value; + return (short) value; } else { - throw new ClassCastException("cannot implicitly cast " + - "def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " + - short.class.getCanonicalName()); + throw new ClassCastException( + "cannot implicitly cast " + + "def [" + + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + + "] to " + + short.class.getCanonicalName() + ); } } public static char defTocharImplicit(final Object value) { if (value instanceof Character) { - return (char)value; + return (char) value; } else { - throw new ClassCastException("cannot implicitly cast " + - "def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " + - char.class.getCanonicalName()); + throw new ClassCastException( + "cannot implicitly cast " + + "def [" + + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + + "] to " + + char.class.getCanonicalName() + ); } } public static int defTointImplicit(final Object value) { if (value instanceof Byte) { - return (byte)value; + return (byte) value; } else if (value instanceof Short) { - return (short)value; + return (short) value; } else if (value instanceof Character) { - return (char)value; + return (char) value; } else if (value instanceof Integer) { - return (int)value; + return (int) value; } else { - throw new ClassCastException("cannot implicitly cast " + - "def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " + - int.class.getCanonicalName()); + throw new ClassCastException( + "cannot implicitly cast " + + "def [" + + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + + "] to " + + int.class.getCanonicalName() + ); } } public static long defTolongImplicit(final Object value) { if (value instanceof Byte) { - return (byte)value; + return (byte) value; } else if (value instanceof Short) { - return (short)value; + return (short) value; } else if (value instanceof Character) { - return (char)value; + return (char) value; } else if (value instanceof Integer) { - return (int)value; + return (int) value; } else if (value instanceof Long) { - return (long)value; + return (long) value; } else { - throw new ClassCastException("cannot implicitly cast " + - "def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " + - long.class.getCanonicalName()); + throw new ClassCastException( + "cannot implicitly cast " + + "def [" + + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + + "] to " + + long.class.getCanonicalName() + ); } } public static float defTofloatImplicit(final Object value) { if (value instanceof Byte) { - return (byte)value; + return (byte) value; } else if (value instanceof Short) { - return (short)value; + return (short) value; } else if (value instanceof Character) { - return (char)value; + return (char) value; } else if (value instanceof Integer) { - return (int)value; + return (int) value; } else if (value instanceof Long) { - return (long)value; + return (long) value; } else if (value instanceof Float) { - return (float)value; + return (float) value; } else { - throw new ClassCastException("cannot implicitly cast " + - "def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " + - float.class.getCanonicalName()); + throw new ClassCastException( + "cannot implicitly cast " + + "def [" + + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + + "] to " + + float.class.getCanonicalName() + ); } } public static double defTodoubleImplicit(final Object value) { if (value instanceof Byte) { - return (byte)value; + return (byte) value; } else if (value instanceof Short) { - return (short)value; + return (short) value; } else if (value instanceof Character) { - return (char)value; + return (char) value; } else if (value instanceof Integer) { - return (int)value; + return (int) value; } else if (value instanceof Long) { - return (long)value; + return (long) value; } else if (value instanceof Float) { - return (float)value; + return (float) value; } else if (value instanceof Double) { - return (double)value; + return (double) value; } else { - throw new ClassCastException("cannot implicitly cast " + - "def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " + - double.class.getCanonicalName()); + throw new ClassCastException( + "cannot implicitly cast " + + "def [" + + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + + "] to " + + double.class.getCanonicalName() + ); } } public static byte defTobyteExplicit(final Object value) { if (value instanceof Character) { - return (byte)(char)value; - } else if ( - value instanceof Byte || - value instanceof Short || - value instanceof Integer || - value instanceof Long || - value instanceof Float || - value instanceof Double - ) { - return ((Number)value).byteValue(); - } else { - throw new ClassCastException("cannot explicitly cast " + - "def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " + - byte.class.getCanonicalName()); - } + return (byte) (char) value; + } else if (value instanceof Byte + || value instanceof Short + || value instanceof Integer + || value instanceof Long + || value instanceof Float + || value instanceof Double) { + return ((Number) value).byteValue(); + } else { + throw new ClassCastException( + "cannot explicitly cast " + + "def [" + + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + + "] to " + + byte.class.getCanonicalName() + ); + } } public static short defToshortExplicit(final Object value) { if (value instanceof Character) { - return (short)(char)value; - } else if ( - value instanceof Byte || - value instanceof Short || - value instanceof Integer || - value instanceof Long || - value instanceof Float || - value instanceof Double - ) { - return ((Number)value).shortValue(); - } else { - throw new ClassCastException("cannot explicitly cast " + - "def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " + - short.class.getCanonicalName()); - } + return (short) (char) value; + } else if (value instanceof Byte + || value instanceof Short + || value instanceof Integer + || value instanceof Long + || value instanceof Float + || value instanceof Double) { + return ((Number) value).shortValue(); + } else { + throw new ClassCastException( + "cannot explicitly cast " + + "def [" + + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + + "] to " + + short.class.getCanonicalName() + ); + } } public static char defTocharExplicit(final Object value) { if (value instanceof String) { - return Utility.StringTochar((String)value); + return Utility.StringTochar((String) value); } else if (value instanceof Character) { - return (char)value; - } else if ( - value instanceof Byte || - value instanceof Short || - value instanceof Integer || - value instanceof Long || - value instanceof Float || - value instanceof Double - ) { - return (char)((Number)value).intValue(); - } else { - throw new ClassCastException("cannot explicitly cast " + - "def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " + - char.class.getCanonicalName()); - } + return (char) value; + } else if (value instanceof Byte + || value instanceof Short + || value instanceof Integer + || value instanceof Long + || value instanceof Float + || value instanceof Double) { + return (char) ((Number) value).intValue(); + } else { + throw new ClassCastException( + "cannot explicitly cast " + + "def [" + + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + + "] to " + + char.class.getCanonicalName() + ); + } } public static int defTointExplicit(final Object value) { if (value instanceof Character) { - return (char)value; - } else if ( - value instanceof Byte || - value instanceof Short || - value instanceof Integer || - value instanceof Long || - value instanceof Float || - value instanceof Double - ) { - return ((Number)value).intValue(); - } else { - throw new ClassCastException("cannot explicitly cast " + - "def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " + - int.class.getCanonicalName()); - } + return (char) value; + } else if (value instanceof Byte + || value instanceof Short + || value instanceof Integer + || value instanceof Long + || value instanceof Float + || value instanceof Double) { + return ((Number) value).intValue(); + } else { + throw new ClassCastException( + "cannot explicitly cast " + + "def [" + + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + + "] to " + + int.class.getCanonicalName() + ); + } } public static long defTolongExplicit(final Object value) { if (value instanceof Character) { - return (char)value; - } else if ( - value instanceof Byte || - value instanceof Short || - value instanceof Integer || - value instanceof Long || - value instanceof Float || - value instanceof Double - ) { - return ((Number)value).longValue(); - } else { - throw new ClassCastException("cannot explicitly cast " + - "def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " + - long.class.getCanonicalName()); - } + return (char) value; + } else if (value instanceof Byte + || value instanceof Short + || value instanceof Integer + || value instanceof Long + || value instanceof Float + || value instanceof Double) { + return ((Number) value).longValue(); + } else { + throw new ClassCastException( + "cannot explicitly cast " + + "def [" + + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + + "] to " + + long.class.getCanonicalName() + ); + } } public static float defTofloatExplicit(final Object value) { if (value instanceof Character) { - return (char)value; - } else if ( - value instanceof Byte || - value instanceof Short || - value instanceof Integer || - value instanceof Long || - value instanceof Float || - value instanceof Double - ) { - return ((Number)value).floatValue(); - } else { - throw new ClassCastException("cannot explicitly cast " + - "float [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " + - byte.class.getCanonicalName()); - } + return (char) value; + } else if (value instanceof Byte + || value instanceof Short + || value instanceof Integer + || value instanceof Long + || value instanceof Float + || value instanceof Double) { + return ((Number) value).floatValue(); + } else { + throw new ClassCastException( + "cannot explicitly cast " + + "float [" + + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + + "] to " + + byte.class.getCanonicalName() + ); + } } public static double defTodoubleExplicit(final Object value) { if (value instanceof Character) { - return (char)value; - } else if ( - value instanceof Byte || - value instanceof Short || - value instanceof Integer || - value instanceof Long || - value instanceof Float || - value instanceof Double - ) { - return ((Number)value).doubleValue(); - } else { - throw new ClassCastException("cannot explicitly cast " + - "def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " + - byte.class.getCanonicalName()); - } + return (char) value; + } else if (value instanceof Byte + || value instanceof Short + || value instanceof Integer + || value instanceof Long + || value instanceof Float + || value instanceof Double) { + return ((Number) value).doubleValue(); + } else { + throw new ClassCastException( + "cannot explicitly cast " + + "def [" + + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + + "] to " + + byte.class.getCanonicalName() + ); + } } // Conversion methods for def to boxed types. @@ -895,11 +1130,15 @@ public static Boolean defToBoolean(final Object value) { if (value == null) { return null; } else if (value instanceof Boolean) { - return (Boolean)value; + return (Boolean) value; } else { - throw new ClassCastException("cannot implicitly cast " + - "def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " + - Boolean.class.getCanonicalName()); + throw new ClassCastException( + "cannot implicitly cast " + + "def [" + + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + + "] to " + + Boolean.class.getCanonicalName() + ); } } @@ -907,11 +1146,15 @@ public static Byte defToByteImplicit(final Object value) { if (value == null) { return null; } else if (value instanceof Byte) { - return (Byte)value; + return (Byte) value; } else { - throw new ClassCastException("cannot implicitly cast " + - "def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " + - Byte.class.getCanonicalName()); + throw new ClassCastException( + "cannot implicitly cast " + + "def [" + + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + + "] to " + + Byte.class.getCanonicalName() + ); } } @@ -919,13 +1162,17 @@ public static Short defToShortImplicit(final Object value) { if (value == null) { return null; } else if (value instanceof Byte) { - return (short)(byte)value; + return (short) (byte) value; } else if (value instanceof Short) { - return (Short)value; + return (Short) value; } else { - throw new ClassCastException("cannot implicitly cast " + - "def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " + - Short.class.getCanonicalName()); + throw new ClassCastException( + "cannot implicitly cast " + + "def [" + + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + + "] to " + + Short.class.getCanonicalName() + ); } } @@ -933,11 +1180,15 @@ public static Character defToCharacterImplicit(final Object value) { if (value == null) { return null; } else if (value instanceof Character) { - return (Character)value; + return (Character) value; } else { - throw new ClassCastException("cannot implicitly cast " + - "def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " + - Character.class.getCanonicalName()); + throw new ClassCastException( + "cannot implicitly cast " + + "def [" + + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + + "] to " + + Character.class.getCanonicalName() + ); } } @@ -945,17 +1196,21 @@ public static Integer defToIntegerImplicit(final Object value) { if (value == null) { return null; } else if (value instanceof Byte) { - return (int)(byte)value; + return (int) (byte) value; } else if (value instanceof Short) { - return (int)(short)value; + return (int) (short) value; } else if (value instanceof Character) { - return (int)(char)value; + return (int) (char) value; } else if (value instanceof Integer) { - return (Integer)value; + return (Integer) value; } else { - throw new ClassCastException("cannot implicitly cast " + - "def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " + - Integer.class.getCanonicalName()); + throw new ClassCastException( + "cannot implicitly cast " + + "def [" + + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + + "] to " + + Integer.class.getCanonicalName() + ); } } @@ -963,19 +1218,23 @@ public static Long defToLongImplicit(final Object value) { if (value == null) { return null; } else if (value instanceof Byte) { - return (long)(byte)value; + return (long) (byte) value; } else if (value instanceof Short) { - return (long)(short)value; + return (long) (short) value; } else if (value instanceof Character) { - return (long)(char)value; + return (long) (char) value; } else if (value instanceof Integer) { - return (long)(int)value; + return (long) (int) value; } else if (value instanceof Long) { - return (Long)value; + return (Long) value; } else { - throw new ClassCastException("cannot implicitly cast " + - "def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " + - Long.class.getCanonicalName()); + throw new ClassCastException( + "cannot implicitly cast " + + "def [" + + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + + "] to " + + Long.class.getCanonicalName() + ); } } @@ -983,21 +1242,25 @@ public static Float defToFloatImplicit(final Object value) { if (value == null) { return null; } else if (value instanceof Byte) { - return (float)(byte)value; + return (float) (byte) value; } else if (value instanceof Short) { - return (float)(short)value; + return (float) (short) value; } else if (value instanceof Character) { - return (float)(char)value; + return (float) (char) value; } else if (value instanceof Integer) { - return (float)(int)value; + return (float) (int) value; } else if (value instanceof Long) { - return (float)(long)value; + return (float) (long) value; } else if (value instanceof Float) { - return (Float)value; + return (Float) value; } else { - throw new ClassCastException("cannot implicitly cast " + - "def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " + - Float.class.getCanonicalName()); + throw new ClassCastException( + "cannot implicitly cast " + + "def [" + + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + + "] to " + + Float.class.getCanonicalName() + ); } } @@ -1005,23 +1268,27 @@ public static Double defToDoubleImplicit(final Object value) { if (value == null) { return null; } else if (value instanceof Byte) { - return (double)(byte)value; + return (double) (byte) value; } else if (value instanceof Short) { - return (double)(short)value; + return (double) (short) value; } else if (value instanceof Character) { - return (double)(char)value; + return (double) (char) value; } else if (value instanceof Integer) { - return (double)(int)value; + return (double) (int) value; } else if (value instanceof Long) { - return (double)(long)value; + return (double) (long) value; } else if (value instanceof Float) { - return (double)(float)value; + return (double) (float) value; } else if (value instanceof Double) { return (Double) value; } else { - throw new ClassCastException("cannot implicitly cast " + - "def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " + - Double.class.getCanonicalName()); + throw new ClassCastException( + "cannot implicitly cast " + + "def [" + + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + + "] to " + + Double.class.getCanonicalName() + ); } } @@ -1029,160 +1296,178 @@ public static Byte defToByteExplicit(final Object value) { if (value == null) { return null; } else if (value instanceof Character) { - return (byte)(char)value; - } else if ( - value instanceof Byte || - value instanceof Short || - value instanceof Integer || - value instanceof Long || - value instanceof Float || - value instanceof Double - ) { - return ((Number)value).byteValue(); - } else { - throw new ClassCastException("cannot explicitly cast " + - "def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " + - Byte.class.getCanonicalName()); - } + return (byte) (char) value; + } else if (value instanceof Byte + || value instanceof Short + || value instanceof Integer + || value instanceof Long + || value instanceof Float + || value instanceof Double) { + return ((Number) value).byteValue(); + } else { + throw new ClassCastException( + "cannot explicitly cast " + + "def [" + + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + + "] to " + + Byte.class.getCanonicalName() + ); + } } public static Short defToShortExplicit(final Object value) { if (value == null) { return null; } else if (value instanceof Character) { - return (short)(char)value; - } else if ( - value instanceof Byte || - value instanceof Short || - value instanceof Integer || - value instanceof Long || - value instanceof Float || - value instanceof Double - ) { - return ((Number)value).shortValue(); - } else { - throw new ClassCastException("cannot explicitly cast " + - "def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " + - Short.class.getCanonicalName()); - } + return (short) (char) value; + } else if (value instanceof Byte + || value instanceof Short + || value instanceof Integer + || value instanceof Long + || value instanceof Float + || value instanceof Double) { + return ((Number) value).shortValue(); + } else { + throw new ClassCastException( + "cannot explicitly cast " + + "def [" + + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + + "] to " + + Short.class.getCanonicalName() + ); + } } public static Character defToCharacterExplicit(final Object value) { if (value == null) { return null; } else if (value instanceof String) { - return Utility.StringTochar((String)value); + return Utility.StringTochar((String) value); } else if (value instanceof Character) { - return (Character)value; - } else if ( - value instanceof Byte || - value instanceof Short || - value instanceof Integer || - value instanceof Long || - value instanceof Float || - value instanceof Double - ) { - return (char)((Number)value).intValue(); - } else { - throw new ClassCastException("cannot explicitly cast " + - "def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " + - Character.class.getCanonicalName()); - } + return (Character) value; + } else if (value instanceof Byte + || value instanceof Short + || value instanceof Integer + || value instanceof Long + || value instanceof Float + || value instanceof Double) { + return (char) ((Number) value).intValue(); + } else { + throw new ClassCastException( + "cannot explicitly cast " + + "def [" + + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + + "] to " + + Character.class.getCanonicalName() + ); + } } public static Integer defToIntegerExplicit(final Object value) { if (value == null) { return null; } else if (value instanceof Character) { - return (int)(char)value; - } else if ( - value instanceof Byte || - value instanceof Short || - value instanceof Integer || - value instanceof Long || - value instanceof Float || - value instanceof Double - ) { - return ((Number)value).intValue(); - } else { - throw new ClassCastException("cannot explicitly cast " + - "def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " + - Integer.class.getCanonicalName()); - } + return (int) (char) value; + } else if (value instanceof Byte + || value instanceof Short + || value instanceof Integer + || value instanceof Long + || value instanceof Float + || value instanceof Double) { + return ((Number) value).intValue(); + } else { + throw new ClassCastException( + "cannot explicitly cast " + + "def [" + + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + + "] to " + + Integer.class.getCanonicalName() + ); + } } public static Long defToLongExplicit(final Object value) { if (value == null) { return null; } else if (value instanceof Character) { - return (long)(char)value; - } else if ( - value instanceof Byte || - value instanceof Short || - value instanceof Integer || - value instanceof Long || - value instanceof Float || - value instanceof Double - ) { - return ((Number)value).longValue(); - } else { - throw new ClassCastException("cannot explicitly cast " + - "def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " + - Long.class.getCanonicalName()); - } + return (long) (char) value; + } else if (value instanceof Byte + || value instanceof Short + || value instanceof Integer + || value instanceof Long + || value instanceof Float + || value instanceof Double) { + return ((Number) value).longValue(); + } else { + throw new ClassCastException( + "cannot explicitly cast " + + "def [" + + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + + "] to " + + Long.class.getCanonicalName() + ); + } } public static Float defToFloatExplicit(final Object value) { if (value == null) { return null; } else if (value instanceof Character) { - return (float)(char)value; - } else if ( - value instanceof Byte || - value instanceof Short || - value instanceof Integer || - value instanceof Long || - value instanceof Float || - value instanceof Double - ) { - return ((Number)value).floatValue(); - } else { - throw new ClassCastException("cannot explicitly cast " + - "def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " + - Float.class.getCanonicalName()); - } + return (float) (char) value; + } else if (value instanceof Byte + || value instanceof Short + || value instanceof Integer + || value instanceof Long + || value instanceof Float + || value instanceof Double) { + return ((Number) value).floatValue(); + } else { + throw new ClassCastException( + "cannot explicitly cast " + + "def [" + + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + + "] to " + + Float.class.getCanonicalName() + ); + } } public static Double defToDoubleExplicit(final Object value) { if (value == null) { return null; } else if (value instanceof Character) { - return (double)(char)value; - } else if ( - value instanceof Byte || - value instanceof Short || - value instanceof Integer || - value instanceof Long || - value instanceof Float || - value instanceof Double - ) { - return ((Number)value).doubleValue(); - } else { - throw new ClassCastException("cannot explicitly cast " + - "def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " + - Double.class.getCanonicalName()); - } + return (double) (char) value; + } else if (value instanceof Byte + || value instanceof Short + || value instanceof Integer + || value instanceof Long + || value instanceof Float + || value instanceof Double) { + return ((Number) value).doubleValue(); + } else { + throw new ClassCastException( + "cannot explicitly cast " + + "def [" + + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + + "] to " + + Double.class.getCanonicalName() + ); + } } public static String defToStringImplicit(final Object value) { if (value == null) { return null; } else if (value instanceof String) { - return (String)value; + return (String) value; } else { - throw new ClassCastException("cannot implicitly cast " + - "def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " + - String.class.getCanonicalName()); + throw new ClassCastException( + "cannot implicitly cast " + + "def [" + + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + + "] to " + + String.class.getCanonicalName() + ); } } @@ -1190,13 +1475,17 @@ public static String defToStringExplicit(final Object value) { if (value == null) { return null; } else if (value instanceof Character) { - return Utility.charToString((char)value); + return Utility.charToString((char) value); } else if (value instanceof String) { - return (String)value; + return (String) value; } else { - throw new ClassCastException("cannot explicitly cast " + - "def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " + - String.class.getCanonicalName()); + throw new ClassCastException( + "cannot explicitly cast " + + "def [" + + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + + "] to " + + String.class.getCanonicalName() + ); } } @@ -1221,44 +1510,80 @@ public static int listIndexNormalize(final List value, int index) { private static final class ArrayIndexNormalizeHelper { private static final MethodHandles.Lookup PRIVATE_METHOD_HANDLES_LOOKUP = MethodHandles.lookup(); - private static final Map,MethodHandle> ARRAY_TYPE_MH_MAPPING = Collections.unmodifiableMap( - Stream.of(boolean[].class, byte[].class, short[].class, int[].class, long[].class, - char[].class, float[].class, double[].class, Object[].class) - .collect(Collectors.toMap(Function.identity(), type -> { - try { - return PRIVATE_METHOD_HANDLES_LOOKUP.findStatic(PRIVATE_METHOD_HANDLES_LOOKUP.lookupClass(), "normalizeIndex", - MethodType.methodType(int.class, type, int.class)); - } catch (ReflectiveOperationException e) { - throw new AssertionError(e); - } - })) + private static final Map, MethodHandle> ARRAY_TYPE_MH_MAPPING = Collections.unmodifiableMap( + Stream.of( + boolean[].class, + byte[].class, + short[].class, + int[].class, + long[].class, + char[].class, + float[].class, + double[].class, + Object[].class + ).collect(Collectors.toMap(Function.identity(), type -> { + try { + return PRIVATE_METHOD_HANDLES_LOOKUP.findStatic( + PRIVATE_METHOD_HANDLES_LOOKUP.lookupClass(), + "normalizeIndex", + MethodType.methodType(int.class, type, int.class) + ); + } catch (ReflectiveOperationException e) { + throw new AssertionError(e); + } + })) ); private static final MethodHandle OBJECT_ARRAY_MH = ARRAY_TYPE_MH_MAPPING.get(Object[].class); - static int normalizeIndex(final boolean[] array, final int index) { return index >= 0 ? index : index + array.length; } - static int normalizeIndex(final byte[] array, final int index) { return index >= 0 ? index : index + array.length; } - static int normalizeIndex(final short[] array, final int index) { return index >= 0 ? index : index + array.length; } - static int normalizeIndex(final int[] array, final int index) { return index >= 0 ? index : index + array.length; } - static int normalizeIndex(final long[] array, final int index) { return index >= 0 ? index : index + array.length; } - static int normalizeIndex(final char[] array, final int index) { return index >= 0 ? index : index + array.length; } - static int normalizeIndex(final float[] array, final int index) { return index >= 0 ? index : index + array.length; } - static int normalizeIndex(final double[] array, final int index) { return index >= 0 ? index : index + array.length; } - static int normalizeIndex(final Object[] array, final int index) { return index >= 0 ? index : index + array.length; } + static int normalizeIndex(final boolean[] array, final int index) { + return index >= 0 ? index : index + array.length; + } + + static int normalizeIndex(final byte[] array, final int index) { + return index >= 0 ? index : index + array.length; + } + + static int normalizeIndex(final short[] array, final int index) { + return index >= 0 ? index : index + array.length; + } + + static int normalizeIndex(final int[] array, final int index) { + return index >= 0 ? index : index + array.length; + } + + static int normalizeIndex(final long[] array, final int index) { + return index >= 0 ? index : index + array.length; + } + + static int normalizeIndex(final char[] array, final int index) { + return index >= 0 ? index : index + array.length; + } + + static int normalizeIndex(final float[] array, final int index) { + return index >= 0 ? index : index + array.length; + } + + static int normalizeIndex(final double[] array, final int index) { + return index >= 0 ? index : index + array.length; + } + + static int normalizeIndex(final Object[] array, final int index) { + return index >= 0 ? index : index + array.length; + } static MethodHandle arrayIndexNormalizer(Class arrayType) { if (arrayType.isArray() == false) { throw new IllegalArgumentException("type must be an array"); } - return (ARRAY_TYPE_MH_MAPPING.containsKey(arrayType)) ? - ARRAY_TYPE_MH_MAPPING.get(arrayType) : - OBJECT_ARRAY_MH.asType(OBJECT_ARRAY_MH.type().changeParameterType(0, arrayType)); + return (ARRAY_TYPE_MH_MAPPING.containsKey(arrayType)) + ? ARRAY_TYPE_MH_MAPPING.get(arrayType) + : OBJECT_ARRAY_MH.asType(OBJECT_ARRAY_MH.type().changeParameterType(0, arrayType)); } private ArrayIndexNormalizeHelper() {} } - public static class Encoding { public final boolean isStatic; public final boolean needsInstance; @@ -1282,11 +1607,7 @@ public Encoding(boolean isStatic, boolean needsInstance, String symbol, String m this.symbol = Objects.requireNonNull(symbol); this.methodName = Objects.requireNonNull(methodName); this.numCaptures = numCaptures; - this.encoding = (isStatic ? "S" : "D") + (needsInstance ? "t" : "f") + - symbol + "." + - methodName + "," + - numCaptures; - + this.encoding = (isStatic ? "S" : "D") + (needsInstance ? "t" : "f") + symbol + "." + methodName + "," + numCaptures; if ("this".equals(symbol)) { if (isStatic == false) { @@ -1294,8 +1615,9 @@ public Encoding(boolean isStatic, boolean needsInstance, String symbol, String m } } else { if (needsInstance) { - throw new IllegalArgumentException("Def.Encoding symbol must be 'this', not [" + symbol + "] if needsInstance," + - " encoding [" + encoding + "]"); + throw new IllegalArgumentException( + "Def.Encoding symbol must be 'this', not [" + symbol + "] if needsInstance," + " encoding [" + encoding + "]" + ); } } @@ -1303,8 +1625,9 @@ public Encoding(boolean isStatic, boolean needsInstance, String symbol, String m throw new IllegalArgumentException("methodName must be non-empty, encoding [" + encoding + "]"); } if (numCaptures < 0) { - throw new IllegalArgumentException("numCaptures must be non-negative, not [" + numCaptures + "]," + - " encoding: [" + encoding + "]"); + throw new IllegalArgumentException( + "numCaptures must be non-negative, not [" + numCaptures + "]," + " encoding: [" + encoding + "]" + ); } } @@ -1312,8 +1635,16 @@ public Encoding(boolean isStatic, boolean needsInstance, String symbol, String m public Encoding(String encoding) { this.encoding = Objects.requireNonNull(encoding); if (encoding.length() < 6) { - throw new IllegalArgumentException("Encoding too short. Minimum 6, given [" + encoding.length() + "]," + - " encoding: [" + encoding + "], format: " + FORMAT + ""); + throw new IllegalArgumentException( + "Encoding too short. Minimum 6, given [" + + encoding.length() + + "]," + + " encoding: [" + + encoding + + "], format: " + + FORMAT + + "" + ); } // 'S' or 'D' @@ -1324,23 +1655,46 @@ public Encoding(String encoding) { int dotIndex = encoding.lastIndexOf('.'); if (dotIndex < 2) { - throw new IllegalArgumentException("Invalid symbol, could not find '.' at expected position after index 1, instead found" + - " index [" + dotIndex + "], encoding: [" + encoding + "], format: " + FORMAT); + throw new IllegalArgumentException( + "Invalid symbol, could not find '.' at expected position after index 1, instead found" + + " index [" + + dotIndex + + "], encoding: [" + + encoding + + "], format: " + + FORMAT + ); } this.symbol = encoding.substring(2, dotIndex); int commaIndex = encoding.indexOf(','); if (commaIndex <= dotIndex) { - throw new IllegalArgumentException("Invalid symbol, could not find ',' at expected position after '.' at" + - " [" + dotIndex + "], instead found index [" + commaIndex + "], encoding: [" + encoding + "], format: " + FORMAT); + throw new IllegalArgumentException( + "Invalid symbol, could not find ',' at expected position after '.' at" + + " [" + + dotIndex + + "], instead found index [" + + commaIndex + + "], encoding: [" + + encoding + + "], format: " + + FORMAT + ); } this.methodName = encoding.substring(dotIndex + 1, commaIndex); if (commaIndex == encoding.length() - 1) { - throw new IllegalArgumentException("Invalid symbol, could not find ',' at expected position, instead found" + - " index [" + commaIndex + "], encoding: [" + encoding + "], format: " + FORMAT); + throw new IllegalArgumentException( + "Invalid symbol, could not find ',' at expected position, instead found" + + " index [" + + commaIndex + + "], encoding: [" + + encoding + + "], format: " + + FORMAT + ); } this.numCaptures = Integer.parseUnsignedInt(encoding.substring(commaIndex + 1)); @@ -1356,8 +1710,11 @@ public boolean equals(Object o) { if (this == o) return true; if ((o instanceof Encoding) == false) return false; Encoding encoding1 = (Encoding) o; - return isStatic == encoding1.isStatic && needsInstance == encoding1.needsInstance && numCaptures == encoding1.numCaptures - && Objects.equals(symbol, encoding1.symbol) && Objects.equals(methodName, encoding1.methodName) + return isStatic == encoding1.isStatic + && needsInstance == encoding1.needsInstance + && numCaptures == encoding1.numCaptures + && Objects.equals(symbol, encoding1.symbol) + && Objects.equals(methodName, encoding1.methodName) && Objects.equals(encoding, encoding1.encoding); } diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/DefBootstrap.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/DefBootstrap.java index 11fdfd902f4c3..5fb478c8d5a03 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/DefBootstrap.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/DefBootstrap.java @@ -104,8 +104,17 @@ static final class PIC extends MutableCallSite { private final Object[] args; int depth; // pkg-protected for testing - PIC(PainlessLookup painlessLookup, FunctionTable functions, Map constants, - MethodHandles.Lookup methodHandlesLookup, String name, MethodType type, int initialDepth, int flavor, Object[] args) { + PIC( + PainlessLookup painlessLookup, + FunctionTable functions, + Map constants, + MethodHandles.Lookup methodHandlesLookup, + String name, + MethodType type, + int initialDepth, + int flavor, + Object[] args + ) { super(type); if (type.parameterType(0) != Object.class) { throw new BootstrapMethodError("The receiver type (1st arg) of invokedynamic descriptor must be Object."); @@ -119,9 +128,7 @@ static final class PIC extends MutableCallSite { this.args = args; this.depth = initialDepth; - MethodHandle fallback = FALLBACK.bindTo(this) - .asCollector(Object[].class, type.parameterCount()) - .asType(type); + MethodHandle fallback = FALLBACK.bindTo(this).asCollector(Object[].class, type.parameterCount()).asType(type); setTarget(fallback); } @@ -149,7 +156,7 @@ static boolean checkClass(Class clazz, Object receiver) { * Does a slow lookup against the whitelist. */ private MethodHandle lookup(int flavor, String name, Class receiver) throws Throwable { - switch(flavor) { + switch (flavor) { case METHOD_CALL: return Def.lookupMethod(painlessLookup, functions, constants, methodHandlesLookup, type(), receiver, name, args); case LOAD: @@ -166,7 +173,8 @@ private MethodHandle lookup(int flavor, String name, Class receiver) throws T return Def.lookupReference(painlessLookup, functions, constants, methodHandlesLookup, (String) args[0], receiver, name); case INDEX_NORMALIZE: return Def.lookupIndexNormalize(receiver); - default: throw new AssertionError(); + default: + throw new AssertionError(); } } @@ -188,9 +196,11 @@ protected MethodHandle computeValue(Class receiverType) { } } }; - MethodHandle lookup = - MethodHandles.filterArguments(CLASSVALUE_GET.bindTo(megamorphicCache), 0, - MethodHandles.insertArguments(CHECK_NULL, 1, name)); + MethodHandle lookup = MethodHandles.filterArguments( + CLASSVALUE_GET.bindTo(megamorphicCache), + 0, + MethodHandles.insertArguments(CHECK_NULL, 1, name) + ); lookup = lookup.asType(lookup.type().changeReturnType(MethodHandle.class)); return MethodHandles.foldArguments(MethodHandles.exactInvoker(type), lookup); } @@ -230,14 +240,26 @@ Object fallback(final Object[] callArgs) throws Throwable { final MethodHandles.Lookup methodHandlesLookup = MethodHandles.lookup(); final MethodHandles.Lookup publicMethodHandlesLookup = MethodHandles.publicLookup(); try { - CHECK_NULL = methodHandlesLookup.findStatic(PIC.class, "checkNull", - MethodType.methodType(Class.class, Object.class, String.class)); - CHECK_CLASS = methodHandlesLookup.findStatic(methodHandlesLookup.lookupClass(), "checkClass", - MethodType.methodType(boolean.class, Class.class, Object.class)); - FALLBACK = methodHandlesLookup.findVirtual(methodHandlesLookup.lookupClass(), "fallback", - MethodType.methodType(Object.class, Object[].class)); - CLASSVALUE_GET = publicMethodHandlesLookup.findVirtual(ClassValue.class, "get", - MethodType.methodType(Object.class, Class.class)); + CHECK_NULL = methodHandlesLookup.findStatic( + PIC.class, + "checkNull", + MethodType.methodType(Class.class, Object.class, String.class) + ); + CHECK_CLASS = methodHandlesLookup.findStatic( + methodHandlesLookup.lookupClass(), + "checkClass", + MethodType.methodType(boolean.class, Class.class, Object.class) + ); + FALLBACK = methodHandlesLookup.findVirtual( + methodHandlesLookup.lookupClass(), + "fallback", + MethodType.methodType(Object.class, Object[].class) + ); + CLASSVALUE_GET = publicMethodHandlesLookup.findVirtual( + ClassValue.class, + "get", + MethodType.methodType(Object.class, Class.class) + ); } catch (ReflectiveOperationException e) { throw new AssertionError(e); } @@ -263,9 +285,7 @@ static final class MIC extends MutableCallSite { initialized = true; } - MethodHandle fallback = FALLBACK.bindTo(this) - .asCollector(Object[].class, type.parameterCount()) - .asType(type); + MethodHandle fallback = FALLBACK.bindTo(this).asCollector(Object[].class, type.parameterCount()).asType(type); setTarget(fallback); } @@ -274,7 +294,7 @@ static final class MIC extends MutableCallSite { * Does a slow lookup for the operator */ private MethodHandle lookup(Object[] args) throws Throwable { - switch(flavor) { + switch (flavor) { case UNARY_OPERATOR: case SHIFT_OPERATOR: // shifts are treated as unary, as java allows long arguments without a cast (but bits are ignored) @@ -297,7 +317,8 @@ private MethodHandle lookup(Object[] args) throws Throwable { } return binary; } - default: throw new AssertionError(); + default: + throw new AssertionError(); } } @@ -345,35 +366,31 @@ Object fallback(Object[] args) throws Throwable { if (type.parameterType(1) != Object.class) { // case 1: only the receiver is unknown, just check that MethodHandle unaryTest = CHECK_LHS.bindTo(clazz0); - test = unaryTest.asType(unaryTest.type() - .changeParameterType(0, type.parameterType(0))); + test = unaryTest.asType(unaryTest.type().changeParameterType(0, type.parameterType(0))); } else if (type.parameterType(0) != Object.class) { // case 2: only the argument is unknown, just check that MethodHandle unaryTest = CHECK_RHS.bindTo(clazz0).bindTo(clazz1); - test = unaryTest.asType(unaryTest.type() - .changeParameterType(0, type.parameterType(0)) - .changeParameterType(1, type.parameterType(1))); + test = unaryTest.asType( + unaryTest.type().changeParameterType(0, type.parameterType(0)).changeParameterType(1, type.parameterType(1)) + ); } else { // case 3: check both receiver and argument MethodHandle binaryTest = CHECK_BOTH.bindTo(clazz0).bindTo(clazz1); - test = binaryTest.asType(binaryTest.type() - .changeParameterType(0, type.parameterType(0)) - .changeParameterType(1, type.parameterType(1))); + test = binaryTest.asType( + binaryTest.type().changeParameterType(0, type.parameterType(0)).changeParameterType(1, type.parameterType(1)) + ); } } else { // unary operator MethodHandle receiverTest = CHECK_LHS.bindTo(args[0].getClass()); - test = receiverTest.asType(receiverTest.type() - .changeParameterType(0, type.parameterType(0))); + test = receiverTest.asType(receiverTest.type().changeParameterType(0, type.parameterType(0))); } MethodHandle guard = MethodHandles.guardWithTest(test, target, getTarget()); // very special cases, where even the receiver can be null (see JLS rules for string concat) // we wrap + with an NPE catcher, and use our generic method in that case. if (flavor == BINARY_OPERATOR && (flags & OPERATOR_ALLOWS_NULL) != 0) { - MethodHandle handler = MethodHandles.dropArguments(lookupGeneric().asType(type()), - 0, - NullPointerException.class); + MethodHandle handler = MethodHandles.dropArguments(lookupGeneric().asType(type()), 0, NullPointerException.class); guard = MethodHandles.catchException(guard, NullPointerException.class, handler); } @@ -414,14 +431,26 @@ static boolean checkBoth(Class left, Class right, Object leftObject, Objec static { final MethodHandles.Lookup methodHandlesLookup = MethodHandles.lookup(); try { - CHECK_LHS = methodHandlesLookup.findStatic(methodHandlesLookup.lookupClass(), "checkLHS", - MethodType.methodType(boolean.class, Class.class, Object.class)); - CHECK_RHS = methodHandlesLookup.findStatic(methodHandlesLookup.lookupClass(), "checkRHS", - MethodType.methodType(boolean.class, Class.class, Class.class, Object.class, Object.class)); - CHECK_BOTH = methodHandlesLookup.findStatic(methodHandlesLookup.lookupClass(), "checkBoth", - MethodType.methodType(boolean.class, Class.class, Class.class, Object.class, Object.class)); - FALLBACK = methodHandlesLookup.findVirtual(methodHandlesLookup.lookupClass(), "fallback", - MethodType.methodType(Object.class, Object[].class)); + CHECK_LHS = methodHandlesLookup.findStatic( + methodHandlesLookup.lookupClass(), + "checkLHS", + MethodType.methodType(boolean.class, Class.class, Object.class) + ); + CHECK_RHS = methodHandlesLookup.findStatic( + methodHandlesLookup.lookupClass(), + "checkRHS", + MethodType.methodType(boolean.class, Class.class, Class.class, Object.class, Object.class) + ); + CHECK_BOTH = methodHandlesLookup.findStatic( + methodHandlesLookup.lookupClass(), + "checkBoth", + MethodType.methodType(boolean.class, Class.class, Class.class, Object.class, Object.class) + ); + FALLBACK = methodHandlesLookup.findVirtual( + methodHandlesLookup.lookupClass(), + "fallback", + MethodType.methodType(Object.class, Object[].class) + ); } catch (ReflectiveOperationException e) { throw new AssertionError(e); } @@ -442,10 +471,19 @@ static boolean checkBoth(Class left, Class right, Object leftObject, Objec * see https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-6.html#jvms-6.5.invokedynamic */ @SuppressWarnings("unchecked") - public static CallSite bootstrap(PainlessLookup painlessLookup, FunctionTable functions, Map constants, - MethodHandles.Lookup methodHandlesLookup, String name, MethodType type, int initialDepth, int flavor, Object... args) { + public static CallSite bootstrap( + PainlessLookup painlessLookup, + FunctionTable functions, + Map constants, + MethodHandles.Lookup methodHandlesLookup, + String name, + MethodType type, + int initialDepth, + int flavor, + Object... args + ) { // validate arguments - switch(flavor) { + switch (flavor) { // "function-call" like things get a polymorphic cache case METHOD_CALL: if (args.length == 0) { @@ -492,7 +530,7 @@ public static CallSite bootstrap(PainlessLookup painlessLookup, FunctionTable fu if (args[0] instanceof Integer == false) { throw new BootstrapMethodError("Illegal parameter for reference call: " + args[0]); } - int flags = (int)args[0]; + int flags = (int) args[0]; if ((flags & OPERATOR_ALLOWS_NULL) != 0 && flavor != BINARY_OPERATOR) { // we just don't need it anywhere else. throw new BootstrapMethodError("This parameter is only supported for BINARY_OPERATORs"); diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/DefMath.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/DefMath.java index d6f3b7cbe5624..0a29ff80a45bd 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/DefMath.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/DefMath.java @@ -53,19 +53,18 @@ private static boolean not(boolean v) { private static Object not(Object unary) { if (unary instanceof Long) { - return ~(Long)unary; + return ~(Long) unary; } else if (unary instanceof Integer) { - return ~(Integer)unary; + return ~(Integer) unary; } else if (unary instanceof Short) { - return ~(Short)unary; + return ~(Short) unary; } else if (unary instanceof Character) { - return ~(Character)unary; + return ~(Character) unary; } else if (unary instanceof Byte) { - return ~(Byte)unary; + return ~(Byte) unary; } - throw new ClassCastException("Cannot apply [~] operation to type " + - "[" + unary.getClass().getCanonicalName() + "]."); + throw new ClassCastException("Cannot apply [~] operation to type " + "[" + unary.getClass().getCanonicalName() + "]."); } // unary negation and plus: applicable to all numeric types @@ -92,23 +91,22 @@ private static boolean neg(boolean v) { private static Object neg(final Object unary) { if (unary instanceof Double) { - return -(double)unary; + return -(double) unary; } else if (unary instanceof Long) { - return -(long)unary; + return -(long) unary; } else if (unary instanceof Integer) { - return -(int)unary; + return -(int) unary; } else if (unary instanceof Float) { - return -(float)unary; + return -(float) unary; } else if (unary instanceof Short) { - return -(short)unary; + return -(short) unary; } else if (unary instanceof Character) { - return -(char)unary; + return -(char) unary; } else if (unary instanceof Byte) { - return -(byte)unary; + return -(byte) unary; } - throw new ClassCastException("Cannot apply [-] operation to type " + - "[" + unary.getClass().getCanonicalName() + "]."); + throw new ClassCastException("Cannot apply [-] operation to type " + "[" + unary.getClass().getCanonicalName() + "]."); } private static int plus(int v) { @@ -133,23 +131,22 @@ private static boolean plus(boolean v) { private static Object plus(final Object unary) { if (unary instanceof Double) { - return +(double)unary; + return +(double) unary; } else if (unary instanceof Long) { - return +(long)unary; + return +(long) unary; } else if (unary instanceof Integer) { - return +(int)unary; + return +(int) unary; } else if (unary instanceof Float) { - return +(float)unary; + return +(float) unary; } else if (unary instanceof Short) { - return +(short)unary; + return +(short) unary; } else if (unary instanceof Character) { - return +(char)unary; + return +(char) unary; } else if (unary instanceof Byte) { - return +(byte)unary; + return +(byte) unary; } - throw new ClassCastException("Cannot apply [+] operation to type " + - "[" + unary.getClass().getCanonicalName() + "]."); + throw new ClassCastException("Cannot apply [+] operation to type " + "[" + unary.getClass().getCanonicalName() + "]."); } // multiplication/division/remainder/subtraction: applicable to all integer types @@ -178,43 +175,49 @@ private static Object mul(Object left, Object right) { if (left instanceof Number) { if (right instanceof Number) { if (left instanceof Double || right instanceof Double) { - return ((Number)left).doubleValue() * ((Number)right).doubleValue(); + return ((Number) left).doubleValue() * ((Number) right).doubleValue(); } else if (left instanceof Float || right instanceof Float) { - return ((Number)left).floatValue() * ((Number)right).floatValue(); + return ((Number) left).floatValue() * ((Number) right).floatValue(); } else if (left instanceof Long || right instanceof Long) { - return ((Number)left).longValue() * ((Number)right).longValue(); + return ((Number) left).longValue() * ((Number) right).longValue(); } else { - return ((Number)left).intValue() * ((Number)right).intValue(); + return ((Number) left).intValue() * ((Number) right).intValue(); } } else if (right instanceof Character) { if (left instanceof Double) { - return ((Number)left).doubleValue() * (char)right; + return ((Number) left).doubleValue() * (char) right; } else if (left instanceof Long) { - return ((Number)left).longValue() * (char)right; + return ((Number) left).longValue() * (char) right; } else if (left instanceof Float) { - return ((Number)left).floatValue() * (char)right; + return ((Number) left).floatValue() * (char) right; } else { - return ((Number)left).intValue() * (char)right; + return ((Number) left).intValue() * (char) right; } } } else if (left instanceof Character) { if (right instanceof Number) { if (right instanceof Double) { - return (char)left * ((Number)right).doubleValue(); + return (char) left * ((Number) right).doubleValue(); } else if (right instanceof Long) { - return (char)left * ((Number)right).longValue(); + return (char) left * ((Number) right).longValue(); } else if (right instanceof Float) { - return (char)left * ((Number)right).floatValue(); + return (char) left * ((Number) right).floatValue(); } else { - return (char)left * ((Number)right).intValue(); + return (char) left * ((Number) right).intValue(); } } else if (right instanceof Character) { - return (char)left * (char)right; + return (char) left * (char) right; } } - throw new ClassCastException("Cannot apply [*] operation to types " + - "[" + left.getClass().getCanonicalName() + "] and [" + right.getClass().getCanonicalName() + "]."); + throw new ClassCastException( + "Cannot apply [*] operation to types " + + "[" + + left.getClass().getCanonicalName() + + "] and [" + + right.getClass().getCanonicalName() + + "]." + ); } private static int div(int a, int b) { @@ -241,43 +244,49 @@ private static Object div(Object left, Object right) { if (left instanceof Number) { if (right instanceof Number) { if (left instanceof Double || right instanceof Double) { - return ((Number)left).doubleValue() / ((Number)right).doubleValue(); + return ((Number) left).doubleValue() / ((Number) right).doubleValue(); } else if (left instanceof Float || right instanceof Float) { - return ((Number)left).floatValue() / ((Number)right).floatValue(); + return ((Number) left).floatValue() / ((Number) right).floatValue(); } else if (left instanceof Long || right instanceof Long) { - return ((Number)left).longValue() / ((Number)right).longValue(); + return ((Number) left).longValue() / ((Number) right).longValue(); } else { - return ((Number)left).intValue() / ((Number)right).intValue(); + return ((Number) left).intValue() / ((Number) right).intValue(); } } else if (right instanceof Character) { if (left instanceof Double) { - return ((Number)left).doubleValue() / (char)right; + return ((Number) left).doubleValue() / (char) right; } else if (left instanceof Long) { - return ((Number)left).longValue() / (char)right; + return ((Number) left).longValue() / (char) right; } else if (left instanceof Float) { - return ((Number)left).floatValue() / (char)right; + return ((Number) left).floatValue() / (char) right; } else { - return ((Number)left).intValue() / (char)right; + return ((Number) left).intValue() / (char) right; } } } else if (left instanceof Character) { if (right instanceof Number) { if (right instanceof Double) { - return (char)left / ((Number)right).doubleValue(); + return (char) left / ((Number) right).doubleValue(); } else if (right instanceof Long) { - return (char)left / ((Number)right).longValue(); + return (char) left / ((Number) right).longValue(); } else if (right instanceof Float) { - return (char)left / ((Number)right).floatValue(); + return (char) left / ((Number) right).floatValue(); } else { - return (char)left / ((Number)right).intValue(); + return (char) left / ((Number) right).intValue(); } } else if (right instanceof Character) { - return (char)left / (char)right; + return (char) left / (char) right; } } - throw new ClassCastException("Cannot apply [/] operation to types " + - "[" + left.getClass().getCanonicalName() + "] and [" + right.getClass().getCanonicalName() + "]."); + throw new ClassCastException( + "Cannot apply [/] operation to types " + + "[" + + left.getClass().getCanonicalName() + + "] and [" + + right.getClass().getCanonicalName() + + "]." + ); } private static int rem(int a, int b) { @@ -304,43 +313,49 @@ private static Object rem(Object left, Object right) { if (left instanceof Number) { if (right instanceof Number) { if (left instanceof Double || right instanceof Double) { - return ((Number)left).doubleValue() % ((Number)right).doubleValue(); + return ((Number) left).doubleValue() % ((Number) right).doubleValue(); } else if (left instanceof Float || right instanceof Float) { - return ((Number)left).floatValue() % ((Number)right).floatValue(); + return ((Number) left).floatValue() % ((Number) right).floatValue(); } else if (left instanceof Long || right instanceof Long) { - return ((Number)left).longValue() % ((Number)right).longValue(); + return ((Number) left).longValue() % ((Number) right).longValue(); } else { - return ((Number)left).intValue() % ((Number)right).intValue(); + return ((Number) left).intValue() % ((Number) right).intValue(); } } else if (right instanceof Character) { if (left instanceof Double) { - return ((Number)left).doubleValue() % (char)right; + return ((Number) left).doubleValue() % (char) right; } else if (left instanceof Long) { - return ((Number)left).longValue() % (char)right; + return ((Number) left).longValue() % (char) right; } else if (left instanceof Float) { - return ((Number)left).floatValue() % (char)right; + return ((Number) left).floatValue() % (char) right; } else { - return ((Number)left).intValue() % (char)right; + return ((Number) left).intValue() % (char) right; } } } else if (left instanceof Character) { if (right instanceof Number) { if (right instanceof Double) { - return (char)left % ((Number)right).doubleValue(); + return (char) left % ((Number) right).doubleValue(); } else if (right instanceof Long) { - return (char)left % ((Number)right).longValue(); + return (char) left % ((Number) right).longValue(); } else if (right instanceof Float) { - return (char)left % ((Number)right).floatValue(); + return (char) left % ((Number) right).floatValue(); } else { - return (char)left % ((Number)right).intValue(); + return (char) left % ((Number) right).intValue(); } } else if (right instanceof Character) { - return (char)left % (char)right; + return (char) left % (char) right; } } - throw new ClassCastException("Cannot apply [%] operation to types " + - "[" + left.getClass().getCanonicalName() + "] and [" + right.getClass().getCanonicalName() + "]."); + throw new ClassCastException( + "Cannot apply [%] operation to types " + + "[" + + left.getClass().getCanonicalName() + + "] and [" + + right.getClass().getCanonicalName() + + "]." + ); } // addition: applicable to all numeric types. @@ -374,43 +389,49 @@ private static Object add(Object left, Object right) { } else if (left instanceof Number) { if (right instanceof Number) { if (left instanceof Double || right instanceof Double) { - return ((Number)left).doubleValue() + ((Number)right).doubleValue(); + return ((Number) left).doubleValue() + ((Number) right).doubleValue(); } else if (left instanceof Float || right instanceof Float) { - return ((Number)left).floatValue() + ((Number)right).floatValue(); + return ((Number) left).floatValue() + ((Number) right).floatValue(); } else if (left instanceof Long || right instanceof Long) { - return ((Number)left).longValue() + ((Number)right).longValue(); + return ((Number) left).longValue() + ((Number) right).longValue(); } else { - return ((Number)left).intValue() + ((Number)right).intValue(); + return ((Number) left).intValue() + ((Number) right).intValue(); } } else if (right instanceof Character) { if (left instanceof Double) { - return ((Number)left).doubleValue() + (char)right; + return ((Number) left).doubleValue() + (char) right; } else if (left instanceof Long) { - return ((Number)left).longValue() + (char)right; + return ((Number) left).longValue() + (char) right; } else if (left instanceof Float) { - return ((Number)left).floatValue() + (char)right; + return ((Number) left).floatValue() + (char) right; } else { - return ((Number)left).intValue() + (char)right; + return ((Number) left).intValue() + (char) right; } } } else if (left instanceof Character) { if (right instanceof Number) { if (right instanceof Double) { - return (char)left + ((Number)right).doubleValue(); + return (char) left + ((Number) right).doubleValue(); } else if (right instanceof Long) { - return (char)left + ((Number)right).longValue(); + return (char) left + ((Number) right).longValue(); } else if (right instanceof Float) { - return (char)left + ((Number)right).floatValue(); + return (char) left + ((Number) right).floatValue(); } else { - return (char)left + ((Number)right).intValue(); + return (char) left + ((Number) right).intValue(); } } else if (right instanceof Character) { - return (char)left + (char)right; + return (char) left + (char) right; } } - throw new ClassCastException("Cannot apply [+] operation to types " + - "[" + left.getClass().getCanonicalName() + "] and [" + right.getClass().getCanonicalName() + "]."); + throw new ClassCastException( + "Cannot apply [+] operation to types " + + "[" + + left.getClass().getCanonicalName() + + "] and [" + + right.getClass().getCanonicalName() + + "]." + ); } private static int sub(int a, int b) { @@ -437,43 +458,49 @@ private static Object sub(Object left, Object right) { if (left instanceof Number) { if (right instanceof Number) { if (left instanceof Double || right instanceof Double) { - return ((Number)left).doubleValue() - ((Number)right).doubleValue(); + return ((Number) left).doubleValue() - ((Number) right).doubleValue(); } else if (left instanceof Float || right instanceof Float) { - return ((Number)left).floatValue() - ((Number)right).floatValue(); + return ((Number) left).floatValue() - ((Number) right).floatValue(); } else if (left instanceof Long || right instanceof Long) { - return ((Number)left).longValue() - ((Number)right).longValue(); + return ((Number) left).longValue() - ((Number) right).longValue(); } else { - return ((Number)left).intValue() - ((Number)right).intValue(); + return ((Number) left).intValue() - ((Number) right).intValue(); } } else if (right instanceof Character) { if (left instanceof Double) { - return ((Number)left).doubleValue() - (char)right; + return ((Number) left).doubleValue() - (char) right; } else if (left instanceof Long) { - return ((Number)left).longValue() - (char)right; + return ((Number) left).longValue() - (char) right; } else if (left instanceof Float) { - return ((Number)left).floatValue() - (char)right; + return ((Number) left).floatValue() - (char) right; } else { - return ((Number)left).intValue() - (char)right; + return ((Number) left).intValue() - (char) right; } } } else if (left instanceof Character) { if (right instanceof Number) { if (right instanceof Double) { - return (char)left - ((Number)right).doubleValue(); + return (char) left - ((Number) right).doubleValue(); } else if (right instanceof Long) { - return (char)left - ((Number)right).longValue(); + return (char) left - ((Number) right).longValue(); } else if (right instanceof Float) { - return (char)left - ((Number)right).floatValue(); + return (char) left - ((Number) right).floatValue(); } else { - return (char)left - ((Number)right).intValue(); + return (char) left - ((Number) right).intValue(); } } else if (right instanceof Character) { - return (char)left - (char)right; + return (char) left - (char) right; } } - throw new ClassCastException("Cannot apply [-] operation to types " + - "[" + left.getClass().getCanonicalName() + "] and [" + right.getClass().getCanonicalName() + "]."); + throw new ClassCastException( + "Cannot apply [-] operation to types " + + "[" + + left.getClass().getCanonicalName() + + "] and [" + + right.getClass().getCanonicalName() + + "]." + ); } // eq: applicable to any arbitrary type, including nulls for both arguments!!! @@ -502,50 +529,50 @@ private static boolean eq(Object left, Object right) { if (left != null && right != null) { if (left instanceof Double) { if (right instanceof Number) { - return (double)left == ((Number)right).doubleValue(); + return (double) left == ((Number) right).doubleValue(); } else if (right instanceof Character) { - return (double)left == (char)right; + return (double) left == (char) right; } } else if (right instanceof Double) { if (left instanceof Number) { - return ((Number)left).doubleValue() == (double)right; + return ((Number) left).doubleValue() == (double) right; } else if (left instanceof Character) { - return (char)left == ((Number)right).doubleValue(); + return (char) left == ((Number) right).doubleValue(); } } else if (left instanceof Float) { if (right instanceof Number) { - return (float)left == ((Number)right).floatValue(); + return (float) left == ((Number) right).floatValue(); } else if (right instanceof Character) { - return (float)left == (char)right; + return (float) left == (char) right; } } else if (right instanceof Float) { if (left instanceof Number) { - return ((Number)left).floatValue() == (float)right; + return ((Number) left).floatValue() == (float) right; } else if (left instanceof Character) { - return (char)left == ((Number)right).floatValue(); + return (char) left == ((Number) right).floatValue(); } } else if (left instanceof Long) { if (right instanceof Number) { - return (long)left == ((Number)right).longValue(); + return (long) left == ((Number) right).longValue(); } else if (right instanceof Character) { - return (long)left == (char)right; + return (long) left == (char) right; } } else if (right instanceof Long) { if (left instanceof Number) { - return ((Number)left).longValue() == (long)right; + return ((Number) left).longValue() == (long) right; } else if (left instanceof Character) { - return (char)left == ((Number)right).longValue(); + return (char) left == ((Number) right).longValue(); } } else if (left instanceof Number) { if (right instanceof Number) { - return ((Number)left).intValue() == ((Number)right).intValue(); + return ((Number) left).intValue() == ((Number) right).intValue(); } else if (right instanceof Character) { - return ((Number)left).intValue() == (char)right; + return ((Number) left).intValue() == (char) right; } } else if (right instanceof Number && left instanceof Character) { - return (char)left == ((Number)right).intValue(); + return (char) left == ((Number) right).intValue(); } else if (left instanceof Character && right instanceof Character) { - return (char)left == (char)right; + return (char) left == (char) right; } return left.equals(right); @@ -580,43 +607,49 @@ private static boolean lt(Object left, Object right) { if (left instanceof Number) { if (right instanceof Number) { if (left instanceof Double || right instanceof Double) { - return ((Number)left).doubleValue() < ((Number)right).doubleValue(); + return ((Number) left).doubleValue() < ((Number) right).doubleValue(); } else if (left instanceof Float || right instanceof Float) { - return ((Number)left).floatValue() < ((Number)right).floatValue(); + return ((Number) left).floatValue() < ((Number) right).floatValue(); } else if (left instanceof Long || right instanceof Long) { - return ((Number)left).longValue() < ((Number)right).longValue(); + return ((Number) left).longValue() < ((Number) right).longValue(); } else { - return ((Number)left).intValue() < ((Number)right).intValue(); + return ((Number) left).intValue() < ((Number) right).intValue(); } } else if (right instanceof Character) { if (left instanceof Double) { - return ((Number)left).doubleValue() < (char)right; + return ((Number) left).doubleValue() < (char) right; } else if (left instanceof Long) { - return ((Number)left).longValue() < (char)right; + return ((Number) left).longValue() < (char) right; } else if (left instanceof Float) { - return ((Number)left).floatValue() < (char)right; + return ((Number) left).floatValue() < (char) right; } else { - return ((Number)left).intValue() < (char)right; + return ((Number) left).intValue() < (char) right; } } } else if (left instanceof Character) { if (right instanceof Number) { if (right instanceof Double) { - return (char)left < ((Number)right).doubleValue(); + return (char) left < ((Number) right).doubleValue(); } else if (right instanceof Long) { - return (char)left < ((Number)right).longValue(); + return (char) left < ((Number) right).longValue(); } else if (right instanceof Float) { - return (char)left < ((Number)right).floatValue(); + return (char) left < ((Number) right).floatValue(); } else { - return (char)left < ((Number)right).intValue(); + return (char) left < ((Number) right).intValue(); } } else if (right instanceof Character) { - return (char)left < (char)right; + return (char) left < (char) right; } } - throw new ClassCastException("Cannot apply [<] operation to types " + - "[" + left.getClass().getCanonicalName() + "] and [" + right.getClass().getCanonicalName() + "]."); + throw new ClassCastException( + "Cannot apply [<] operation to types " + + "[" + + left.getClass().getCanonicalName() + + "] and [" + + right.getClass().getCanonicalName() + + "]." + ); } private static boolean lte(int a, int b) { @@ -643,43 +676,49 @@ private static boolean lte(Object left, Object right) { if (left instanceof Number) { if (right instanceof Number) { if (left instanceof Double || right instanceof Double) { - return ((Number)left).doubleValue() <= ((Number)right).doubleValue(); + return ((Number) left).doubleValue() <= ((Number) right).doubleValue(); } else if (left instanceof Float || right instanceof Float) { - return ((Number)left).floatValue() <= ((Number)right).floatValue(); + return ((Number) left).floatValue() <= ((Number) right).floatValue(); } else if (left instanceof Long || right instanceof Long) { - return ((Number)left).longValue() <= ((Number)right).longValue(); + return ((Number) left).longValue() <= ((Number) right).longValue(); } else { - return ((Number)left).intValue() <= ((Number)right).intValue(); + return ((Number) left).intValue() <= ((Number) right).intValue(); } } else if (right instanceof Character) { if (left instanceof Double) { - return ((Number)left).doubleValue() <= (char)right; + return ((Number) left).doubleValue() <= (char) right; } else if (left instanceof Long) { - return ((Number)left).longValue() <= (char)right; + return ((Number) left).longValue() <= (char) right; } else if (left instanceof Float) { - return ((Number)left).floatValue() <= (char)right; + return ((Number) left).floatValue() <= (char) right; } else { - return ((Number)left).intValue() <= (char)right; + return ((Number) left).intValue() <= (char) right; } } } else if (left instanceof Character) { if (right instanceof Number) { if (right instanceof Double) { - return (char)left <= ((Number)right).doubleValue(); + return (char) left <= ((Number) right).doubleValue(); } else if (right instanceof Long) { - return (char)left <= ((Number)right).longValue(); + return (char) left <= ((Number) right).longValue(); } else if (right instanceof Float) { - return (char)left <= ((Number)right).floatValue(); + return (char) left <= ((Number) right).floatValue(); } else { - return (char)left <= ((Number)right).intValue(); + return (char) left <= ((Number) right).intValue(); } } else if (right instanceof Character) { - return (char)left <= (char)right; + return (char) left <= (char) right; } } - throw new ClassCastException("Cannot apply [<=] operation to types " + - "[" + left.getClass().getCanonicalName() + "] and [" + right.getClass().getCanonicalName() + "]."); + throw new ClassCastException( + "Cannot apply [<=] operation to types " + + "[" + + left.getClass().getCanonicalName() + + "] and [" + + right.getClass().getCanonicalName() + + "]." + ); } private static boolean gt(int a, int b) { @@ -706,43 +745,49 @@ private static boolean gt(Object left, Object right) { if (left instanceof Number) { if (right instanceof Number) { if (left instanceof Double || right instanceof Double) { - return ((Number)left).doubleValue() > ((Number)right).doubleValue(); + return ((Number) left).doubleValue() > ((Number) right).doubleValue(); } else if (left instanceof Float || right instanceof Float) { - return ((Number)left).floatValue() > ((Number)right).floatValue(); + return ((Number) left).floatValue() > ((Number) right).floatValue(); } else if (left instanceof Long || right instanceof Long) { - return ((Number)left).longValue() > ((Number)right).longValue(); + return ((Number) left).longValue() > ((Number) right).longValue(); } else { - return ((Number)left).intValue() > ((Number)right).intValue(); + return ((Number) left).intValue() > ((Number) right).intValue(); } } else if (right instanceof Character) { if (left instanceof Double) { - return ((Number)left).doubleValue() > (char)right; + return ((Number) left).doubleValue() > (char) right; } else if (left instanceof Long) { - return ((Number)left).longValue() > (char)right; + return ((Number) left).longValue() > (char) right; } else if (left instanceof Float) { - return ((Number)left).floatValue() > (char)right; + return ((Number) left).floatValue() > (char) right; } else { - return ((Number)left).intValue() > (char)right; + return ((Number) left).intValue() > (char) right; } } } else if (left instanceof Character) { if (right instanceof Number) { if (right instanceof Double) { - return (char)left > ((Number)right).doubleValue(); + return (char) left > ((Number) right).doubleValue(); } else if (right instanceof Long) { - return (char)left > ((Number)right).longValue(); + return (char) left > ((Number) right).longValue(); } else if (right instanceof Float) { - return (char)left > ((Number)right).floatValue(); + return (char) left > ((Number) right).floatValue(); } else { - return (char)left > ((Number)right).intValue(); + return (char) left > ((Number) right).intValue(); } } else if (right instanceof Character) { - return (char)left > (char)right; + return (char) left > (char) right; } } - throw new ClassCastException("Cannot apply [>] operation to types " + - "[" + left.getClass().getCanonicalName() + "] and [" + right.getClass().getCanonicalName() + "]."); + throw new ClassCastException( + "Cannot apply [>] operation to types " + + "[" + + left.getClass().getCanonicalName() + + "] and [" + + right.getClass().getCanonicalName() + + "]." + ); } private static boolean gte(int a, int b) { @@ -769,43 +814,49 @@ private static boolean gte(Object left, Object right) { if (left instanceof Number) { if (right instanceof Number) { if (left instanceof Double || right instanceof Double) { - return ((Number)left).doubleValue() >= ((Number)right).doubleValue(); + return ((Number) left).doubleValue() >= ((Number) right).doubleValue(); } else if (left instanceof Float || right instanceof Float) { - return ((Number)left).floatValue() >= ((Number)right).floatValue(); + return ((Number) left).floatValue() >= ((Number) right).floatValue(); } else if (left instanceof Long || right instanceof Long) { - return ((Number)left).longValue() >= ((Number)right).longValue(); + return ((Number) left).longValue() >= ((Number) right).longValue(); } else { - return ((Number)left).intValue() >= ((Number)right).intValue(); + return ((Number) left).intValue() >= ((Number) right).intValue(); } } else if (right instanceof Character) { if (left instanceof Double) { - return ((Number)left).doubleValue() >= (char)right; + return ((Number) left).doubleValue() >= (char) right; } else if (left instanceof Long) { - return ((Number)left).longValue() >= (char)right; + return ((Number) left).longValue() >= (char) right; } else if (left instanceof Float) { - return ((Number)left).floatValue() >= (char)right; + return ((Number) left).floatValue() >= (char) right; } else { - return ((Number)left).intValue() >= (char)right; + return ((Number) left).intValue() >= (char) right; } } } else if (left instanceof Character) { if (right instanceof Number) { if (right instanceof Double) { - return (char)left >= ((Number)right).doubleValue(); + return (char) left >= ((Number) right).doubleValue(); } else if (right instanceof Long) { - return (char)left >= ((Number)right).longValue(); + return (char) left >= ((Number) right).longValue(); } else if (right instanceof Float) { - return (char)left >= ((Number)right).floatValue(); + return (char) left >= ((Number) right).floatValue(); } else { - return (char)left >= ((Number)right).intValue(); + return (char) left >= ((Number) right).intValue(); } } else if (right instanceof Character) { - return (char)left >= (char)right; + return (char) left >= (char) right; } } - throw new ClassCastException("Cannot apply [>] operation to types " + - "[" + left.getClass().getCanonicalName() + "] and [" + right.getClass().getCanonicalName() + "]."); + throw new ClassCastException( + "Cannot apply [>] operation to types " + + "[" + + left.getClass().getCanonicalName() + + "] and [" + + right.getClass().getCanonicalName() + + "]." + ); } // helper methods to convert an integral according to numeric promotion @@ -813,11 +864,11 @@ private static boolean gte(Object left, Object right) { private static long longIntegralValue(Object o) { if (o instanceof Long) { - return (long)o; + return (long) o; } else if (o instanceof Integer || o instanceof Short || o instanceof Byte) { - return ((Number)o).longValue(); + return ((Number) o).longValue(); } else if (o instanceof Character) { - return (char)o; + return (char) o; } else { throw new ClassCastException("Cannot convert [" + o.getClass().getCanonicalName() + "] to an integral value."); } @@ -825,9 +876,9 @@ private static long longIntegralValue(Object o) { private static int intIntegralValue(Object o) { if (o instanceof Integer || o instanceof Short || o instanceof Byte) { - return ((Number)o).intValue(); + return ((Number) o).intValue(); } else if (o instanceof Character) { - return (char)o; + return (char) o; } else { throw new ClassCastException("Cannot convert [" + o.getClass().getCanonicalName() + "] to an integral value."); } @@ -857,7 +908,7 @@ private static boolean and(boolean a, boolean b) { private static Object and(Object left, Object right) { if (left instanceof Boolean && right instanceof Boolean) { - return (boolean)left & (boolean)right; + return (boolean) left & (boolean) right; } else if (left instanceof Long || right instanceof Long) { return longIntegralValue(left) & longIntegralValue(right); } else { @@ -887,7 +938,7 @@ private static boolean xor(boolean a, boolean b) { private static Object xor(Object left, Object right) { if (left instanceof Boolean && right instanceof Boolean) { - return (boolean)left ^ (boolean)right; + return (boolean) left ^ (boolean) right; } else if (left instanceof Long || right instanceof Long) { return longIntegralValue(left) ^ longIntegralValue(right); } else { @@ -917,7 +968,7 @@ private static boolean or(boolean a, boolean b) { private static Object or(Object left, Object right) { if (left instanceof Boolean && right instanceof Boolean) { - return (boolean)left | (boolean)right; + return (boolean) left | (boolean) right; } else if (left instanceof Long || right instanceof Long) { return longIntegralValue(left) | longIntegralValue(right); } else { @@ -950,7 +1001,7 @@ private static boolean lsh(boolean a, long b) { public static Object lsh(Object left, long right) { if (left instanceof Long) { - return (long)(left) << right; + return (long) (left) << right; } else { return intIntegralValue(left) << right; } @@ -978,7 +1029,7 @@ private static boolean rsh(boolean a, long b) { public static Object rsh(Object left, long right) { if (left instanceof Long) { - return (long)left >> right; + return (long) left >> right; } else { return intIntegralValue(left) >> right; } @@ -1006,7 +1057,7 @@ private static boolean ush(boolean a, long b) { public static Object ush(Object left, long right) { if (left instanceof Long) { - return (long)(left) >>> right; + return (long) (left) >>> right; } else { return intIntegralValue(left) >>> right; } @@ -1060,35 +1111,35 @@ private static Class promote(Class a, Class b) { private static final MethodHandles.Lookup PRIVATE_METHOD_HANDLES_LOOKUP = MethodHandles.lookup(); - private static final Map,Map> TYPE_OP_MAPPING = Collections.unmodifiableMap( + private static final Map, Map> TYPE_OP_MAPPING = Collections.unmodifiableMap( Stream.of(boolean.class, int.class, long.class, float.class, double.class, Object.class) .collect(Collectors.toMap(Function.identity(), type -> { try { - Map map = new HashMap<>(); + Map map = new HashMap<>(); MethodType unary = MethodType.methodType(type, type); MethodType binary = MethodType.methodType(type, type, type); MethodType comparison = MethodType.methodType(boolean.class, type, type); MethodType shift = MethodType.methodType(type, type, long.class); Class clazz = PRIVATE_METHOD_HANDLES_LOOKUP.lookupClass(); - map.put("not", PRIVATE_METHOD_HANDLES_LOOKUP.findStatic(clazz, "not", unary)); - map.put("neg", PRIVATE_METHOD_HANDLES_LOOKUP.findStatic(clazz, "neg", unary)); - map.put("plus", PRIVATE_METHOD_HANDLES_LOOKUP.findStatic(clazz, "plus", unary)); - map.put("mul", PRIVATE_METHOD_HANDLES_LOOKUP.findStatic(clazz, "mul", binary)); - map.put("div", PRIVATE_METHOD_HANDLES_LOOKUP.findStatic(clazz, "div", binary)); - map.put("rem", PRIVATE_METHOD_HANDLES_LOOKUP.findStatic(clazz, "rem", binary)); - map.put("add", PRIVATE_METHOD_HANDLES_LOOKUP.findStatic(clazz, "add", binary)); - map.put("sub", PRIVATE_METHOD_HANDLES_LOOKUP.findStatic(clazz, "sub", binary)); - map.put("and", PRIVATE_METHOD_HANDLES_LOOKUP.findStatic(clazz, "and", binary)); - map.put("or", PRIVATE_METHOD_HANDLES_LOOKUP.findStatic(clazz, "or", binary)); - map.put("xor", PRIVATE_METHOD_HANDLES_LOOKUP.findStatic(clazz, "xor", binary)); - map.put("eq", PRIVATE_METHOD_HANDLES_LOOKUP.findStatic(clazz, "eq", comparison)); - map.put("lt", PRIVATE_METHOD_HANDLES_LOOKUP.findStatic(clazz, "lt", comparison)); - map.put("lte", PRIVATE_METHOD_HANDLES_LOOKUP.findStatic(clazz, "lte", comparison)); - map.put("gt", PRIVATE_METHOD_HANDLES_LOOKUP.findStatic(clazz, "gt", comparison)); - map.put("gte", PRIVATE_METHOD_HANDLES_LOOKUP.findStatic(clazz, "gte", comparison)); - map.put("lsh", PRIVATE_METHOD_HANDLES_LOOKUP.findStatic(clazz, "lsh", shift)); - map.put("rsh", PRIVATE_METHOD_HANDLES_LOOKUP.findStatic(clazz, "rsh", shift)); - map.put("ush", PRIVATE_METHOD_HANDLES_LOOKUP.findStatic(clazz, "ush", shift)); + map.put("not", PRIVATE_METHOD_HANDLES_LOOKUP.findStatic(clazz, "not", unary)); + map.put("neg", PRIVATE_METHOD_HANDLES_LOOKUP.findStatic(clazz, "neg", unary)); + map.put("plus", PRIVATE_METHOD_HANDLES_LOOKUP.findStatic(clazz, "plus", unary)); + map.put("mul", PRIVATE_METHOD_HANDLES_LOOKUP.findStatic(clazz, "mul", binary)); + map.put("div", PRIVATE_METHOD_HANDLES_LOOKUP.findStatic(clazz, "div", binary)); + map.put("rem", PRIVATE_METHOD_HANDLES_LOOKUP.findStatic(clazz, "rem", binary)); + map.put("add", PRIVATE_METHOD_HANDLES_LOOKUP.findStatic(clazz, "add", binary)); + map.put("sub", PRIVATE_METHOD_HANDLES_LOOKUP.findStatic(clazz, "sub", binary)); + map.put("and", PRIVATE_METHOD_HANDLES_LOOKUP.findStatic(clazz, "and", binary)); + map.put("or", PRIVATE_METHOD_HANDLES_LOOKUP.findStatic(clazz, "or", binary)); + map.put("xor", PRIVATE_METHOD_HANDLES_LOOKUP.findStatic(clazz, "xor", binary)); + map.put("eq", PRIVATE_METHOD_HANDLES_LOOKUP.findStatic(clazz, "eq", comparison)); + map.put("lt", PRIVATE_METHOD_HANDLES_LOOKUP.findStatic(clazz, "lt", comparison)); + map.put("lte", PRIVATE_METHOD_HANDLES_LOOKUP.findStatic(clazz, "lte", comparison)); + map.put("gt", PRIVATE_METHOD_HANDLES_LOOKUP.findStatic(clazz, "gt", comparison)); + map.put("gte", PRIVATE_METHOD_HANDLES_LOOKUP.findStatic(clazz, "gte", comparison)); + map.put("lsh", PRIVATE_METHOD_HANDLES_LOOKUP.findStatic(clazz, "lsh", shift)); + map.put("rsh", PRIVATE_METHOD_HANDLES_LOOKUP.findStatic(clazz, "rsh", shift)); + map.put("ush", PRIVATE_METHOD_HANDLES_LOOKUP.findStatic(clazz, "ush", shift)); return map; } catch (ReflectiveOperationException e) { throw new AssertionError(e); @@ -1165,9 +1216,9 @@ static Object dynamicCast(Class clazz, Object value) { /** Slowly returns a Number for o. Just for supporting dynamicCast */ static Number getNumber(Object o) { if (o instanceof Number) { - return (Number)o; + return (Number) o; } else if (o instanceof Character) { - return Integer.valueOf((char)o); + return Integer.valueOf((char) o); } else { throw new ClassCastException("Cannot convert [" + o.getClass() + "] to a Number"); } @@ -1178,12 +1229,16 @@ static Number getNumber(Object o) { static { final MethodHandles.Lookup methodHandlesLookup = MethodHandles.lookup(); try { - DYNAMIC_CAST = methodHandlesLookup.findStatic(methodHandlesLookup.lookupClass(), - "dynamicCast", - MethodType.methodType(Object.class, Class.class, Object.class)); - DYNAMIC_RECEIVER_CAST = methodHandlesLookup.findStatic(methodHandlesLookup.lookupClass(), - "dynamicReceiverCast", - MethodType.methodType(Object.class, Object.class, Object.class)); + DYNAMIC_CAST = methodHandlesLookup.findStatic( + methodHandlesLookup.lookupClass(), + "dynamicCast", + MethodType.methodType(Object.class, Class.class, Object.class) + ); + DYNAMIC_RECEIVER_CAST = methodHandlesLookup.findStatic( + methodHandlesLookup.lookupClass(), + "dynamicReceiverCast", + MethodType.methodType(Object.class, Object.class, Object.class) + ); } catch (ReflectiveOperationException e) { throw new AssertionError(e); } @@ -1192,9 +1247,9 @@ static Number getNumber(Object o) { /** Looks up generic method, with a dynamic cast to the receiver's type. (compound assignment) */ public static MethodHandle dynamicCast(MethodHandle target) { // adapt dynamic receiver cast to the generic method - MethodHandle cast = DYNAMIC_RECEIVER_CAST.asType(MethodType.methodType(target.type().returnType(), - target.type().returnType(), - target.type().parameterType(0))); + MethodHandle cast = DYNAMIC_RECEIVER_CAST.asType( + MethodType.methodType(target.type().returnType(), target.type().returnType(), target.type().parameterType(0)) + ); // drop the RHS parameter cast = MethodHandles.dropArguments(cast, 2, target.type().parameterType(1)); // combine: f(x,y) -> g(f(x,y), x, y); diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/FunctionRef.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/FunctionRef.java index ff99e0d28da90..677199bdb9087 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/FunctionRef.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/FunctionRef.java @@ -48,9 +48,17 @@ public class FunctionRef { * @param constants constants used for injection when necessary * @param needsScriptInstance uses an instance method and so receiver must be captured. */ - public static FunctionRef create(PainlessLookup painlessLookup, FunctionTable functionTable, Location location, - Class targetClass, String typeName, String methodName, int numberOfCaptures, Map constants, - boolean needsScriptInstance) { + public static FunctionRef create( + PainlessLookup painlessLookup, + FunctionTable functionTable, + Location location, + Class targetClass, + String typeName, + String methodName, + int numberOfCaptures, + Map constants, + boolean needsScriptInstance + ) { Objects.requireNonNull(painlessLookup); Objects.requireNonNull(targetClass); @@ -64,8 +72,16 @@ public static FunctionRef create(PainlessLookup painlessLookup, FunctionTable fu interfaceMethod = painlessLookup.lookupFunctionalInterfacePainlessMethod(targetClass); if (interfaceMethod == null) { - throw new IllegalArgumentException("cannot convert function reference [" + typeName + "::" + methodName + "] " + - "to a non-functional interface [" + targetClassName + "]"); + throw new IllegalArgumentException( + "cannot convert function reference [" + + typeName + + "::" + + methodName + + "] " + + "to a non-functional interface [" + + targetClassName + + "]" + ); } String interfaceMethodName = interfaceMethod.javaMethod.getName(); @@ -93,9 +109,19 @@ public static FunctionRef create(PainlessLookup painlessLookup, FunctionTable fu LocalFunction localFunction = functionTable.getFunction(localFunctionKey); if (localFunction == null) { - throw new IllegalArgumentException("function reference [this::" + localFunctionKey + "] " + - "matching [" + targetClassName + ", " + interfaceMethodName + "/" + interfaceTypeParametersSize + "] " + - "not found" + (localFunctionKey.contains("$") ? " due to an incorrect number of arguments" : "") + throw new IllegalArgumentException( + "function reference [this::" + + localFunctionKey + + "] " + + "matching [" + + targetClassName + + ", " + + interfaceMethodName + + "/" + + interfaceTypeParametersSize + + "] " + + "not found" + + (localFunctionKey.contains("$") ? " due to an incorrect number of arguments" : "") ); } @@ -117,9 +143,21 @@ public static FunctionRef create(PainlessLookup painlessLookup, FunctionTable fu PainlessConstructor painlessConstructor = painlessLookup.lookupPainlessConstructor(typeName, interfaceTypeParametersSize); if (painlessConstructor == null) { - throw new IllegalArgumentException("function reference [" + typeName + "::new/" + interfaceTypeParametersSize + "] " + - "matching [" + targetClassName + ", " + interfaceMethodName + "/" + interfaceTypeParametersSize + "] " + - "not found"); + throw new IllegalArgumentException( + "function reference [" + + typeName + + "::new/" + + interfaceTypeParametersSize + + "] " + + "matching [" + + targetClassName + + ", " + + interfaceMethodName + + "/" + + interfaceTypeParametersSize + + "] " + + "not found" + ); } delegateClassName = painlessConstructor.javaConstructor.getDeclaringClass().getName(); @@ -138,24 +176,53 @@ public static FunctionRef create(PainlessLookup painlessLookup, FunctionTable fu } boolean captured = numberOfCaptures == 1; - PainlessMethod painlessMethod = - painlessLookup.lookupPainlessMethod(typeName, true, methodName, interfaceTypeParametersSize); + PainlessMethod painlessMethod = painlessLookup.lookupPainlessMethod( + typeName, + true, + methodName, + interfaceTypeParametersSize + ); if (painlessMethod == null) { - painlessMethod = painlessLookup.lookupPainlessMethod(typeName, false, methodName, - captured ? interfaceTypeParametersSize : interfaceTypeParametersSize - 1); + painlessMethod = painlessLookup.lookupPainlessMethod( + typeName, + false, + methodName, + captured ? interfaceTypeParametersSize : interfaceTypeParametersSize - 1 + ); if (painlessMethod == null) { throw new IllegalArgumentException( - "function reference " + "[" + typeName + "::" + methodName + "/" + interfaceTypeParametersSize + "] " + - "matching [" + targetClassName + ", " + interfaceMethodName + "/" + interfaceTypeParametersSize + "] " + - "not found"); + "function reference " + + "[" + + typeName + + "::" + + methodName + + "/" + + interfaceTypeParametersSize + + "] " + + "matching [" + + targetClassName + + ", " + + interfaceMethodName + + "/" + + interfaceTypeParametersSize + + "] " + + "not found" + ); } } else if (captured) { throw new IllegalArgumentException( - "cannot use a static method as a function reference " + - "[" + typeName + "::" + methodName + "/" + interfaceTypeParametersSize + "] " + - "with a non-static captured variable"); + "cannot use a static method as a function reference " + + "[" + + typeName + + "::" + + methodName + + "/" + + interfaceTypeParametersSize + + "] " + + "with a non-static captured variable" + ); } delegateClassName = painlessMethod.javaMethod.getDeclaringClass().getName(); @@ -177,12 +244,12 @@ public static FunctionRef create(PainlessLookup painlessLookup, FunctionTable fu // Object rather than for the interface; we change the first parameter to match // the interface type so the constant interface method reference is correctly // written to the constant pool - if (delegateInvokeType != H_INVOKESTATIC && - painlessMethod.javaMethod.getDeclaringClass() != painlessMethod.methodType.parameterType(0)) { + if (delegateInvokeType != H_INVOKESTATIC + && painlessMethod.javaMethod.getDeclaringClass() != painlessMethod.methodType.parameterType(0)) { if (painlessMethod.methodType.parameterType(0) != Object.class) { throw new IllegalStateException("internal error"); } - + delegateMethodType = delegateMethodType.changeParameterType(0, painlessMethod.javaMethod.getDeclaringClass()); } @@ -210,14 +277,24 @@ public static FunctionRef create(PainlessLookup painlessLookup, FunctionTable fu } } - MethodType factoryMethodType = MethodType.methodType(targetClass, - delegateMethodType.dropParameterTypes(numberOfCaptures, delegateMethodType.parameterCount())); + MethodType factoryMethodType = MethodType.methodType( + targetClass, + delegateMethodType.dropParameterTypes(numberOfCaptures, delegateMethodType.parameterCount()) + ); delegateMethodType = delegateMethodType.dropParameterTypes(0, numberOfCaptures); - return new FunctionRef(interfaceMethodName, interfaceMethodType, - delegateClassName, isDelegateInterface, isDelegateAugmented, - delegateInvokeType, delegateMethodName, delegateMethodType, delegateInjections, - factoryMethodType, needsScriptInstance ? WriterConstants.CLASS_TYPE : null + return new FunctionRef( + interfaceMethodName, + interfaceMethodType, + delegateClassName, + isDelegateInterface, + isDelegateAugmented, + delegateInvokeType, + delegateMethodName, + delegateMethodType, + delegateInjections, + factoryMethodType, + needsScriptInstance ? WriterConstants.CLASS_TYPE : null ); } catch (IllegalArgumentException iae) { if (location != null) { @@ -252,10 +329,18 @@ public static FunctionRef create(PainlessLookup painlessLookup, FunctionTable fu public final Type factoryMethodReceiver; private FunctionRef( - String interfaceMethodName, MethodType interfaceMethodType, - String delegateClassName, boolean isDelegateInterface, boolean isDelegateAugmented, - int delegateInvokeType, String delegateMethodName, MethodType delegateMethodType, Object[] delegateInjections, - MethodType factoryMethodType, Type factoryMethodReceiver) { + String interfaceMethodName, + MethodType interfaceMethodType, + String delegateClassName, + boolean isDelegateInterface, + boolean isDelegateAugmented, + int delegateInvokeType, + String delegateMethodName, + MethodType delegateMethodType, + Object[] delegateInjections, + MethodType factoryMethodType, + Type factoryMethodReceiver + ) { this.interfaceMethodName = interfaceMethodName; this.interfaceMethodType = interfaceMethodType; diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/Globals.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/Globals.java index 67488095a4256..043940011b55e 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/Globals.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/Globals.java @@ -16,7 +16,7 @@ * Program-wide globals (initializers, synthetic methods, etc) */ public class Globals { - private final Map constantInitializers = new HashMap<>(); + private final Map constantInitializers = new HashMap<>(); private final BitSet statements; /** Create a new Globals from the set of statement boundaries */ @@ -32,7 +32,7 @@ public void addConstantInitializer(Constant constant) { } /** Returns the current initializers */ - public Map getConstantInitializers() { + public Map getConstantInitializers() { return constantInitializers; } diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/LambdaBootstrap.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/LambdaBootstrap.java index 6d8d58e43ede3..54bfa3c86c711 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/LambdaBootstrap.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/LambdaBootstrap.java @@ -191,19 +191,19 @@ private Capture(int count, Class type) { * @throws LambdaConversionException Thrown when an illegal type conversion occurs at link time */ public static CallSite lambdaBootstrap( - Lookup lookup, - String interfaceMethodName, - MethodType factoryMethodType, - MethodType interfaceMethodType, - String delegateClassName, - int delegateInvokeType, - String delegateMethodName, - MethodType delegateMethodType, - int isDelegateInterface, - int isDelegateAugmented, - Object... injections) - throws LambdaConversionException { - Compiler.Loader loader = (Compiler.Loader)lookup.lookupClass().getClassLoader(); + Lookup lookup, + String interfaceMethodName, + MethodType factoryMethodType, + MethodType interfaceMethodType, + String delegateClassName, + int delegateInvokeType, + String delegateMethodName, + MethodType delegateMethodType, + int isDelegateInterface, + int isDelegateAugmented, + Object... injections + ) throws LambdaConversionException { + Compiler.Loader loader = (Compiler.Loader) lookup.lookupClass().getClassLoader(); String lambdaClassName = Type.getInternalName(lookup.lookupClass()) + "$$Lambda" + loader.newLambdaIdentifier(); Type lambdaClassType = Type.getObjectType(lambdaClassName); Type delegateClassType = Type.getObjectType(delegateClassName.replace('.', '/')); @@ -224,9 +224,21 @@ public static CallSite lambdaBootstrap( delegateInvokeType = H_INVOKESTATIC; } - generateInterfaceMethod(cw, factoryMethodType, lambdaClassType, interfaceMethodName, - interfaceMethodType, delegateClassType, delegateInvokeType, - delegateMethodName, delegateMethodType, isDelegateInterface == 1, isDelegateAugmented == 1, captures, injections); + generateInterfaceMethod( + cw, + factoryMethodType, + lambdaClassType, + interfaceMethodName, + interfaceMethodType, + delegateClassType, + delegateInvokeType, + delegateMethodName, + delegateMethodType, + isDelegateInterface == 1, + isDelegateAugmented == 1, + captures, + injections + ); endLambdaClass(cw); @@ -242,13 +254,12 @@ public static CallSite lambdaBootstrap( * Validates some conversions at link time. Currently, only ensures that the lambda method * with a return value cannot delegate to a delegate method with no return type. */ - private static void validateTypes(MethodType interfaceMethodType, MethodType delegateMethodType) - throws LambdaConversionException { + private static void validateTypes(MethodType interfaceMethodType, MethodType delegateMethodType) throws LambdaConversionException { - if (interfaceMethodType.returnType() != void.class && - delegateMethodType.returnType() == void.class) { - throw new LambdaConversionException("lambda expects return type [" - + interfaceMethodType.returnType() + "], but found return type [void]"); + if (interfaceMethodType.returnType() != void.class && delegateMethodType.returnType() == void.class) { + throw new LambdaConversionException( + "lambda expects return type [" + interfaceMethodType.returnType() + "], but found return type [void]" + ); } } @@ -260,8 +271,7 @@ private static ClassWriter beginLambdaClass(String lambdaClassName, Class lam int modifiers = ACC_PUBLIC | ACC_SUPER | ACC_FINAL | ACC_SYNTHETIC; ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); - cw.visit(CLASS_VERSION, - modifiers, lambdaClassName, null, baseClass, new String[] { Type.getInternalName(lambdaInterface) }); + cw.visit(CLASS_VERSION, modifiers, lambdaClassName, null, baseClass, new String[] { Type.getInternalName(lambdaInterface) }); return cw; } @@ -277,12 +287,10 @@ private static Capture[] generateCaptureFields(ClassWriter cw, MethodType factor Capture[] captures = new Capture[captureTotal]; for (int captureCount = 0; captureCount < captureTotal; ++captureCount) { - captures[captureCount] = - new Capture(captureCount, factoryMethodType.parameterType(captureCount)); + captures[captureCount] = new Capture(captureCount, factoryMethodType.parameterType(captureCount)); int modifiers = ACC_PRIVATE | ACC_FINAL; - FieldVisitor fv = cw.visitField( - modifiers, captures[captureCount].name, captures[captureCount].desc, null, null); + FieldVisitor fv = cw.visitField(modifiers, captures[captureCount].name, captures[captureCount].desc, null, null); fv.visitEnd(); } @@ -294,21 +302,19 @@ private static Capture[] generateCaptureFields(ClassWriter cw, MethodType factor * arguments if any and store them in their respective * member fields. */ - private static void generateLambdaConstructor( - ClassWriter cw, - Type lambdaClassType, - MethodType factoryMethodType, - Capture[] captures) { + private static void generateLambdaConstructor(ClassWriter cw, Type lambdaClassType, MethodType factoryMethodType, Capture[] captures) { String conDesc = factoryMethodType.changeReturnType(void.class).toMethodDescriptorString(); Method conMeth = new Method(CTOR_METHOD_NAME, conDesc); Type baseConType = Type.getType(Object.class); - Method baseConMeth = new Method(CTOR_METHOD_NAME, - MethodType.methodType(void.class).toMethodDescriptorString()); + Method baseConMeth = new Method(CTOR_METHOD_NAME, MethodType.methodType(void.class).toMethodDescriptorString()); int modifiers = (captures.length > 0) ? ACC_PRIVATE : ACC_PUBLIC; - GeneratorAdapter constructor = new GeneratorAdapter(modifiers, conMeth, - cw.visitMethod(modifiers, CTOR_METHOD_NAME, conDesc, null, null)); + GeneratorAdapter constructor = new GeneratorAdapter( + modifiers, + conMeth, + cw.visitMethod(modifiers, CTOR_METHOD_NAME, conDesc, null, null) + ); constructor.visitCode(); constructor.loadThis(); constructor.invokeConstructor(baseConType, baseConMeth); @@ -316,8 +322,7 @@ private static void generateLambdaConstructor( for (int captureCount = 0; captureCount < captures.length; ++captureCount) { constructor.loadThis(); constructor.loadArg(captureCount); - constructor.putField( - lambdaClassType, captures[captureCount].name, captures[captureCount].type); + constructor.putField(lambdaClassType, captures[captureCount].name, captures[captureCount].type); } constructor.returnValue(); @@ -338,15 +343,22 @@ private static void generateLambdaConstructor( /** * Generates a factory method to delegate to constructors. */ - private static void generateStaticCtorDelegator(ClassWriter cw, int access, String delegatorMethodName, - Type delegateClassType, MethodType delegateMethodType) { + private static void generateStaticCtorDelegator( + ClassWriter cw, + int access, + String delegatorMethodName, + Type delegateClassType, + MethodType delegateMethodType + ) { Method wrapperMethod = new Method(delegatorMethodName, delegateMethodType.toMethodDescriptorString()); - Method constructorMethod = - new Method(CTOR_METHOD_NAME, delegateMethodType.changeReturnType(void.class).toMethodDescriptorString()); + Method constructorMethod = new Method(CTOR_METHOD_NAME, delegateMethodType.changeReturnType(void.class).toMethodDescriptorString()); int modifiers = access | ACC_STATIC; - GeneratorAdapter factory = new GeneratorAdapter(modifiers, wrapperMethod, - cw.visitMethod(modifiers, delegatorMethodName, delegateMethodType.toMethodDescriptorString(), null, null)); + GeneratorAdapter factory = new GeneratorAdapter( + modifiers, + wrapperMethod, + cw.visitMethod(modifiers, delegatorMethodName, delegateMethodType.toMethodDescriptorString(), null, null) + ); factory.visitCode(); factory.newInstance(delegateClassType); factory.dup(); @@ -361,34 +373,36 @@ private static void generateStaticCtorDelegator(ClassWriter cw, int access, Stri * with {@code INVOKEDYNAMIC} using the {@link #delegateBootstrap} type converter. */ private static void generateInterfaceMethod( - ClassWriter cw, - MethodType factoryMethodType, - Type lambdaClassType, - String interfaceMethodName, - MethodType interfaceMethodType, - Type delegateClassType, - int delegateInvokeType, - String delegateMethodName, - MethodType delegateMethodType, - boolean isDelegateInterface, - boolean isDelegateAugmented, - Capture[] captures, - Object... injections) - throws LambdaConversionException { + ClassWriter cw, + MethodType factoryMethodType, + Type lambdaClassType, + String interfaceMethodName, + MethodType interfaceMethodType, + Type delegateClassType, + int delegateInvokeType, + String delegateMethodName, + MethodType delegateMethodType, + boolean isDelegateInterface, + boolean isDelegateAugmented, + Capture[] captures, + Object... injections + ) throws LambdaConversionException { String lamDesc = interfaceMethodType.toMethodDescriptorString(); Method lamMeth = new Method(lambdaClassType.getInternalName(), lamDesc); int modifiers = ACC_PUBLIC; - GeneratorAdapter iface = new GeneratorAdapter(modifiers, lamMeth, - cw.visitMethod(modifiers, interfaceMethodName, lamDesc, null, null)); + GeneratorAdapter iface = new GeneratorAdapter( + modifiers, + lamMeth, + cw.visitMethod(modifiers, interfaceMethodName, lamDesc, null, null) + ); iface.visitCode(); // Loads any captured variables onto the stack. for (int captureCount = 0; captureCount < captures.length; ++captureCount) { iface.loadThis(); - iface.getField( - lambdaClassType, captures[captureCount].name, captures[captureCount].type); + iface.getField(lambdaClassType, captures[captureCount].name, captures[captureCount].type); } // Loads any passed in arguments onto the stack. @@ -398,19 +412,16 @@ private static void generateInterfaceMethod( // Handles the case for a lambda function or a static reference method. // interfaceMethodType and delegateMethodType both have the captured types - // inserted into their type signatures. This later allows the delegate + // inserted into their type signatures. This later allows the delegate // method to be invoked dynamically and have the interface method types // appropriately converted to the delegate method types. // Example: Integer::parseInt // Example: something.each(x -> x + 1) if (delegateInvokeType == H_INVOKESTATIC) { - interfaceMethodType = - interfaceMethodType.insertParameterTypes(0, factoryMethodType.parameterArray()); + interfaceMethodType = interfaceMethodType.insertParameterTypes(0, factoryMethodType.parameterArray()); functionalInterfaceWithCaptures = interfaceMethodType.toMethodDescriptorString(); - delegateMethodType = - delegateMethodType.insertParameterTypes(0, factoryMethodType.parameterArray()); - } else if (delegateInvokeType == H_INVOKEVIRTUAL || - delegateInvokeType == H_INVOKEINTERFACE) { + delegateMethodType = delegateMethodType.insertParameterTypes(0, factoryMethodType.parameterArray()); + } else if (delegateInvokeType == H_INVOKEVIRTUAL || delegateInvokeType == H_INVOKEINTERFACE) { // Handles the case for a virtual or interface reference method with no captures. // delegateMethodType drops the 'this' parameter because it will be re-inserted // when the method handle for the dynamically invoked delegate method is created. @@ -420,19 +431,19 @@ private static void generateInterfaceMethod( delegateClassType = Type.getType(clazz); delegateMethodType = delegateMethodType.dropParameterTypes(0, 1); functionalInterfaceWithCaptures = interfaceMethodType.toMethodDescriptorString(); - // Handles the case for a virtual or interface reference method with 'this' - // captured. interfaceMethodType inserts the 'this' type into its - // method signature. This later allows the delegate - // method to be invoked dynamically and have the interface method types - // appropriately converted to the delegate method types. - // Example: something::toString + // Handles the case for a virtual or interface reference method with 'this' + // captured. interfaceMethodType inserts the 'this' type into its + // method signature. This later allows the delegate + // method to be invoked dynamically and have the interface method types + // appropriately converted to the delegate method types. + // Example: something::toString } else { Class clazz = factoryMethodType.parameterType(0); delegateClassType = Type.getType(clazz); // functionalInterfaceWithCaptures needs to add the receiver and other captures List parameters = interfaceMethodType.parameterList().stream().map(Type::getType).collect(Collectors.toList()); - parameters.add(0, delegateClassType); + parameters.add(0, delegateClassType); for (int i = 1; i < captures.length; i++) { parameters.add(i, captures[i].type); } @@ -447,14 +458,16 @@ private static void generateInterfaceMethod( } } } else { - throw new IllegalStateException( - "unexpected invocation type [" + delegateInvokeType + "]"); + throw new IllegalStateException("unexpected invocation type [" + delegateInvokeType + "]"); } - Handle delegateHandle = - new Handle(delegateInvokeType, delegateClassType.getInternalName(), - delegateMethodName, delegateMethodType.toMethodDescriptorString(), - isDelegateInterface); + Handle delegateHandle = new Handle( + delegateInvokeType, + delegateClassType.getInternalName(), + delegateMethodName, + delegateMethodType.toMethodDescriptorString(), + isDelegateInterface + ); // Fill in args for indy. Always add the delegate handle and // whether it's static or not then injections as necessary. Object[] args = new Object[2 + injections.length]; @@ -462,10 +475,11 @@ private static void generateInterfaceMethod( args[1] = delegateInvokeType == H_INVOKESTATIC && isDelegateAugmented == false ? 0 : 1; System.arraycopy(injections, 0, args, 2, injections.length); iface.invokeDynamic( - delegateMethodName, - Type.getMethodType(functionalInterfaceWithCaptures).getDescriptor(), - DELEGATE_BOOTSTRAP_HANDLE, - args); + delegateMethodName, + Type.getMethodType(functionalInterfaceWithCaptures).getDescriptor(), + DELEGATE_BOOTSTRAP_HANDLE, + args + ); iface.returnValue(); iface.endMethod(); @@ -482,29 +496,24 @@ private static void endLambdaClass(ClassWriter cw) { * Defines the {@link Class} for the lambda class using the same {@link Compiler.Loader} * that originally defined the class for the Painless script. */ - private static Class createLambdaClass( - Compiler.Loader loader, - ClassWriter cw, - Type lambdaClassType) { + private static Class createLambdaClass(Compiler.Loader loader, ClassWriter cw, Type lambdaClassType) { byte[] classBytes = cw.toByteArray(); // DEBUG: // new ClassReader(classBytes).accept(new TraceClassVisitor(new PrintWriter(System.out)), ClassReader.SKIP_DEBUG); - return AccessController.doPrivileged((PrivilegedAction>)() -> - loader.defineLambda(lambdaClassType.getClassName(), classBytes)); + return AccessController.doPrivileged( + (PrivilegedAction>) () -> loader.defineLambda(lambdaClassType.getClassName(), classBytes) + ); } /** * Creates an {@link ConstantCallSite} that will return the same instance * of the generated lambda class every time this linked factory method is called. */ - private static CallSite createNoCaptureCallSite( - MethodType factoryMethodType, - Class lambdaClass) { + private static CallSite createNoCaptureCallSite(MethodType factoryMethodType, Class lambdaClass) { try { - return new ConstantCallSite(MethodHandles.constant( - factoryMethodType.returnType(), lambdaClass.getConstructor().newInstance())); + return new ConstantCallSite(MethodHandles.constant(factoryMethodType.returnType(), lambdaClass.getConstructor().newInstance())); } catch (ReflectiveOperationException exception) { throw new IllegalStateException("unable to instantiate lambda class", exception); } @@ -513,14 +522,10 @@ private static CallSite createNoCaptureCallSite( /** * Creates an {@link ConstantCallSite} */ - private static CallSite createCaptureCallSite( - Lookup lookup, - MethodType factoryMethodType, - Class lambdaClass) { + private static CallSite createCaptureCallSite(Lookup lookup, MethodType factoryMethodType, Class lambdaClass) { try { - return new ConstantCallSite( - lookup.findStatic(lambdaClass, LAMBDA_FACTORY_METHOD_NAME, factoryMethodType)); + return new ConstantCallSite(lookup.findStatic(lambdaClass, LAMBDA_FACTORY_METHOD_NAME, factoryMethodType)); } catch (ReflectiveOperationException exception) { throw new IllegalStateException("unable to create lambda class", exception); } @@ -534,12 +539,14 @@ private static CallSite createCaptureCallSite( * of either a lot more code or requiring many {@link Class}es to be looked * up at link-time. */ - public static CallSite delegateBootstrap(Lookup lookup, - String delegateMethodName, - MethodType interfaceMethodType, - MethodHandle delegateMethodHandle, - int isVirtual, - Object... injections) { + public static CallSite delegateBootstrap( + Lookup lookup, + String delegateMethodName, + MethodType interfaceMethodType, + MethodHandle delegateMethodHandle, + int isVirtual, + Object... injections + ) { if (injections.length > 0) { delegateMethodHandle = MethodHandles.insertArguments(delegateMethodHandle, isVirtual, injections); diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/MethodWriter.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/MethodWriter.java index 919543d54f1fd..93e9ce5415561 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/MethodWriter.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/MethodWriter.java @@ -90,12 +90,16 @@ public final class MethodWriter extends GeneratorAdapter { private final BitSet statements; private final CompilerSettings settings; - private final Deque> stringConcatArgs = - (INDY_STRING_CONCAT_BOOTSTRAP_HANDLE == null) ? null : new ArrayDeque<>(); + private final Deque> stringConcatArgs = (INDY_STRING_CONCAT_BOOTSTRAP_HANDLE == null) ? null : new ArrayDeque<>(); public MethodWriter(int access, Method method, ClassVisitor cw, BitSet statements, CompilerSettings settings) { - super(Opcodes.ASM5, cw.visitMethod(access, method.getName(), method.getDescriptor(), null, null), - access, method.getName(), method.getDescriptor()); + super( + Opcodes.ASM5, + cw.visitMethod(access, method.getName(), method.getDescriptor(), null, null), + access, + method.getName(), + method.getDescriptor() + ); this.statements = statements; this.settings = settings; @@ -112,7 +116,7 @@ public void writeStatementOffset(Location location) { // (e.g. nodes get assigned wrong offsets by antlr walker) // TODO: introduce a way to ignore internal statements so this assert is not triggered // TODO: https://github.com/elastic/elasticsearch/issues/51836 - //assert statements.get(offset) == false; + // assert statements.get(offset) == false; statements.set(offset); } @@ -167,44 +171,44 @@ public void writeCast(PainlessCast cast) { box(getType(cast.boxTargetType)); } else if (cast.originalType == def.class) { if (cast.explicitCast) { - if (cast.targetType == boolean.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_BOOLEAN); - else if (cast.targetType == byte.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_BYTE_EXPLICIT); - else if (cast.targetType == short.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_SHORT_EXPLICIT); - else if (cast.targetType == char.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_CHAR_EXPLICIT); - else if (cast.targetType == int.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_INT_EXPLICIT); - else if (cast.targetType == long.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_LONG_EXPLICIT); - else if (cast.targetType == float.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_FLOAT_EXPLICIT); - else if (cast.targetType == double.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_DOUBLE_EXPLICIT); - else if (cast.targetType == Boolean.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_BOOLEAN); - else if (cast.targetType == Byte.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_BYTE_EXPLICIT); - else if (cast.targetType == Short.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_SHORT_EXPLICIT); + if (cast.targetType == boolean.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_BOOLEAN); + else if (cast.targetType == byte.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_BYTE_EXPLICIT); + else if (cast.targetType == short.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_SHORT_EXPLICIT); + else if (cast.targetType == char.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_CHAR_EXPLICIT); + else if (cast.targetType == int.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_INT_EXPLICIT); + else if (cast.targetType == long.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_LONG_EXPLICIT); + else if (cast.targetType == float.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_FLOAT_EXPLICIT); + else if (cast.targetType == double.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_DOUBLE_EXPLICIT); + else if (cast.targetType == Boolean.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_BOOLEAN); + else if (cast.targetType == Byte.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_BYTE_EXPLICIT); + else if (cast.targetType == Short.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_SHORT_EXPLICIT); else if (cast.targetType == Character.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_CHARACTER_EXPLICIT); - else if (cast.targetType == Integer.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_INTEGER_EXPLICIT); - else if (cast.targetType == Long.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_LONG_EXPLICIT); - else if (cast.targetType == Float.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_FLOAT_EXPLICIT); - else if (cast.targetType == Double.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_DOUBLE_EXPLICIT); - else if (cast.targetType == String.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_STRING_EXPLICIT); + else if (cast.targetType == Integer.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_INTEGER_EXPLICIT); + else if (cast.targetType == Long.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_LONG_EXPLICIT); + else if (cast.targetType == Float.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_FLOAT_EXPLICIT); + else if (cast.targetType == Double.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_DOUBLE_EXPLICIT); + else if (cast.targetType == String.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_STRING_EXPLICIT); else { writeCast(cast.originalType, cast.targetType); } } else { - if (cast.targetType == boolean.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_BOOLEAN); - else if (cast.targetType == byte.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_BYTE_IMPLICIT); - else if (cast.targetType == short.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_SHORT_IMPLICIT); - else if (cast.targetType == char.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_CHAR_IMPLICIT); - else if (cast.targetType == int.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_INT_IMPLICIT); - else if (cast.targetType == long.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_LONG_IMPLICIT); - else if (cast.targetType == float.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_FLOAT_IMPLICIT); - else if (cast.targetType == double.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_DOUBLE_IMPLICIT); - else if (cast.targetType == Boolean.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_BOOLEAN); - else if (cast.targetType == Byte.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_BYTE_IMPLICIT); - else if (cast.targetType == Short.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_SHORT_IMPLICIT); + if (cast.targetType == boolean.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_BOOLEAN); + else if (cast.targetType == byte.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_BYTE_IMPLICIT); + else if (cast.targetType == short.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_SHORT_IMPLICIT); + else if (cast.targetType == char.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_CHAR_IMPLICIT); + else if (cast.targetType == int.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_INT_IMPLICIT); + else if (cast.targetType == long.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_LONG_IMPLICIT); + else if (cast.targetType == float.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_FLOAT_IMPLICIT); + else if (cast.targetType == double.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_DOUBLE_IMPLICIT); + else if (cast.targetType == Boolean.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_BOOLEAN); + else if (cast.targetType == Byte.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_BYTE_IMPLICIT); + else if (cast.targetType == Short.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_SHORT_IMPLICIT); else if (cast.targetType == Character.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_CHARACTER_IMPLICIT); - else if (cast.targetType == Integer.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_INTEGER_IMPLICIT); - else if (cast.targetType == Long.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_LONG_IMPLICIT); - else if (cast.targetType == Float.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_FLOAT_IMPLICIT); - else if (cast.targetType == Double.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_DOUBLE_IMPLICIT); - else if (cast.targetType == String.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_STRING_IMPLICIT); + else if (cast.targetType == Integer.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_INTEGER_IMPLICIT); + else if (cast.targetType == Long.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_LONG_IMPLICIT); + else if (cast.targetType == Float.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_FLOAT_IMPLICIT); + else if (cast.targetType == Double.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_DOUBLE_IMPLICIT); + else if (cast.targetType == String.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_STRING_IMPLICIT); else { writeCast(cast.originalType, cast.targetType); } @@ -290,24 +294,24 @@ public void writeAppendStrings(Class clazz) { } } else { // Java 8: push a StringBuilder append - if (clazz == boolean.class) invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_BOOLEAN); - else if (clazz == char.class) invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_CHAR); - else if (clazz == byte.class || - clazz == short.class || - clazz == int.class) invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_INT); - else if (clazz == long.class) invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_LONG); - else if (clazz == float.class) invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_FLOAT); - else if (clazz == double.class) invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_DOUBLE); - else if (clazz == String.class) invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_STRING); - else invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_OBJECT); + if (clazz == boolean.class) invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_BOOLEAN); + else if (clazz == char.class) invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_CHAR); + else if (clazz == byte.class || clazz == short.class || clazz == int.class) invokeVirtual( + STRINGBUILDER_TYPE, + STRINGBUILDER_APPEND_INT + ); + else if (clazz == long.class) invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_LONG); + else if (clazz == float.class) invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_FLOAT); + else if (clazz == double.class) invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_DOUBLE); + else if (clazz == String.class) invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_STRING); + else invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_OBJECT); } } public void writeToStrings() { if (INDY_STRING_CONCAT_BOOTSTRAP_HANDLE != null) { // Java 9+: use type information and push invokeDynamic - final String desc = Type.getMethodDescriptor(STRING_TYPE, - stringConcatArgs.pop().stream().toArray(Type[]::new)); + final String desc = Type.getMethodDescriptor(STRING_TYPE, stringConcatArgs.pop().stream().toArray(Type[]::new)); invokeDynamic("concat", desc, INDY_STRING_CONCAT_BOOTSTRAP_HANDLE); } else { // Java 8: call toString() on StringBuilder @@ -316,8 +320,14 @@ public void writeToStrings() { } /** Writes a dynamic binary instruction: returnType, lhs, and rhs can be different */ - public void writeDynamicBinaryInstruction(Location location, Class returnType, Class lhs, Class rhs, - Operation operation, int flags) { + public void writeDynamicBinaryInstruction( + Location location, + Class returnType, + Class lhs, + Class rhs, + Operation operation, + int flags + ) { Type methodType = Type.getMethodType(getType(returnType), getType(lhs), getType(rhs)); switch (operation) { @@ -368,10 +378,13 @@ public void writeDynamicBinaryInstruction(Location location, Class returnType /** Writes a static binary instruction */ public void writeBinaryInstruction(Location location, Class clazz, Operation operation) { - if ( (clazz == float.class || clazz == double.class) && - (operation == Operation.LSH || operation == Operation.USH || - operation == Operation.RSH || operation == Operation.BWAND || - operation == Operation.XOR || operation == Operation.BWOR)) { + if ((clazz == float.class || clazz == double.class) + && (operation == Operation.LSH + || operation == Operation.USH + || operation == Operation.RSH + || operation == Operation.BWAND + || operation == Operation.XOR + || operation == Operation.BWOR)) { throw location.createError(new IllegalStateException("Illegal tree structure.")); } @@ -480,8 +493,13 @@ public void invokeMethodCall(PainlessMethod painlessMethod) { // true to reference the appropriate class constant when calling a static interface // method since java 8 did not check, but java 9 and 10 do if (painlessMethod.javaMethod.getDeclaringClass().isInterface()) { - visitMethodInsn(Opcodes.INVOKESTATIC, type.getInternalName(), - painlessMethod.javaMethod.getName(), method.getDescriptor(), true); + visitMethodInsn( + Opcodes.INVOKESTATIC, + type.getInternalName(), + painlessMethod.javaMethod.getName(), + method.getDescriptor(), + true + ); } else { invokeStatic(type, method); } diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/Operation.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/Operation.java index 410821db1f0e5..967a0b43122f0 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/Operation.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/Operation.java @@ -16,31 +16,31 @@ */ public enum Operation { - MUL ( "*" , "multiplication" ), - DIV ( "/" , "division" ), - REM ( "%" , "remainder" ), - ADD ( "+" , "addition" ), - SUB ( "-" , "subtraction" ), - FIND ( "=~" , "find" ), - MATCH ( "==~" , "match" ), - LSH ( "<<" , "left shift" ), - RSH ( ">>" , "right shift" ), - USH ( ">>>" , "unsigned shift" ), - BWNOT ( "~" , "bitwise not" ), - BWAND ( "&" , "bitwise and" ), - XOR ( "^" , "bitwise xor" ), - BWOR ( "|" , "boolean or" ), - NOT ( "!" , "boolean not" ), - AND ( "&&" , "boolean and" ), - OR ( "||" , "boolean or" ), - LT ( "<" , "less than" ), - LTE ( "<=" , "less than or equals" ), - GT ( ">" , "greater than" ), - GTE ( ">=" , "greater than or equals" ), - EQ ( "==" , "equals" ), - EQR ( "===" , "reference equals" ), - NE ( "!=" , "not equals" ), - NER ( "!==" , "reference not equals" ); + MUL("*", "multiplication"), + DIV("/", "division"), + REM("%", "remainder"), + ADD("+", "addition"), + SUB("-", "subtraction"), + FIND("=~", "find"), + MATCH("==~", "match"), + LSH("<<", "left shift"), + RSH(">>", "right shift"), + USH(">>>", "unsigned shift"), + BWNOT("~", "bitwise not"), + BWAND("&", "bitwise and"), + XOR("^", "bitwise xor"), + BWOR("|", "boolean or"), + NOT("!", "boolean not"), + AND("&&", "boolean and"), + OR("||", "boolean or"), + LT("<", "less than"), + LTE("<=", "less than or equals"), + GT(">", "greater than"), + GTE(">=", "greater than or equals"), + EQ("==", "equals"), + EQR("===", "reference equals"), + NE("!=", "not equals"), + NER("!==", "reference not equals"); public final String symbol; public final String name; diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/PainlessError.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/PainlessError.java index 8ca782b671548..7903258069dc0 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/PainlessError.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/PainlessError.java @@ -23,6 +23,6 @@ public class PainlessError extends Error { * @param message The error message. */ public PainlessError(final String message) { - super(message); + super(message); } } diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/PainlessPlugin.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/PainlessPlugin.java index 485dd84b9b120..5c22128ed8ac7 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/PainlessPlugin.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/PainlessPlugin.java @@ -8,7 +8,6 @@ package org.elasticsearch.painless; - import org.apache.lucene.util.SetOnce; import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionResponse; @@ -22,7 +21,6 @@ import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsFilter; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.env.Environment; import org.elasticsearch.env.NodeEnvironment; import org.elasticsearch.painless.action.PainlessContextAction; @@ -44,6 +42,7 @@ import org.elasticsearch.script.ScriptService; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.watcher.ResourceWatcherService; +import org.elasticsearch.xcontent.NamedXContentRegistry; import java.util.ArrayList; import java.util.Arrays; @@ -76,11 +75,10 @@ public final class PainlessPlugin extends Plugin implements ScriptPlugin, Extens "java.util.function.txt", "java.util.regex.txt", "java.util.stream.txt", - "java.nio.txt" - }; - public static final List BASE_WHITELISTS = - Collections.singletonList(WhitelistLoader.loadFromResourceFiles( - PainlessPlugin.class, WhitelistAnnotationParser.BASE_ANNOTATION_PARSERS, BASE_WHITELIST_FILES)); + "java.nio.txt" }; + public static final List BASE_WHITELISTS = Collections.singletonList( + WhitelistLoader.loadFromResourceFiles(PainlessPlugin.class, WhitelistAnnotationParser.BASE_ANNOTATION_PARSERS, BASE_WHITELIST_FILES) + ); /* * Contexts from Core that need custom whitelists can add them to the map below. @@ -94,8 +92,10 @@ public final class PainlessPlugin extends Plugin implements ScriptPlugin, Extens List contextWhitelists = new ArrayList<>(); if (PainlessPlugin.class.getResourceAsStream("org.elasticsearch.script." + context.name.replace('-', '_') + ".txt") != null) { contextWhitelists.add( - WhitelistLoader.loadFromResourceFiles(PainlessPlugin.class, - "org.elasticsearch.script." + context.name.replace('-', '_') + ".txt") + WhitelistLoader.loadFromResourceFiles( + PainlessPlugin.class, + "org.elasticsearch.script." + context.name.replace('-', '_') + ".txt" + ) ); } @@ -132,12 +132,19 @@ public ScriptEngine getScriptEngine(Settings settings, Collection createComponents(Client client, ClusterService clusterService, ThreadPool threadPool, - ResourceWatcherService resourceWatcherService, ScriptService scriptService, - NamedXContentRegistry xContentRegistry, Environment environment, - NodeEnvironment nodeEnvironment, NamedWriteableRegistry namedWriteableRegistry, - IndexNameExpressionResolver expressionResolver, - Supplier repositoriesServiceSupplier) { + public Collection createComponents( + Client client, + ClusterService clusterService, + ThreadPool threadPool, + ResourceWatcherService resourceWatcherService, + ScriptService scriptService, + NamedXContentRegistry xContentRegistry, + Environment environment, + NodeEnvironment nodeEnvironment, + NamedWriteableRegistry namedWriteableRegistry, + IndexNameExpressionResolver expressionResolver, + Supplier repositoriesServiceSupplier + ) { // this is a hack to bind the painless script engine in guice (all components are added to guice), so that // the painless context api. this is a temporary measure until transport actions do no require guice return Collections.singletonList(painlessScriptEngine.get()); @@ -150,7 +157,8 @@ public List> getSettings() { @Override public void loadExtensions(ExtensionLoader loader) { - loader.loadExtensions(PainlessExtension.class).stream() + loader.loadExtensions(PainlessExtension.class) + .stream() .flatMap(extension -> extension.getContextWhitelists().entrySet().stream()) .forEach(entry -> { List existing = whitelists.computeIfAbsent(entry.getKey(), c -> new ArrayList<>()); @@ -172,10 +180,15 @@ public List> getContexts() { } @Override - public List getRestHandlers(Settings settings, RestController restController, ClusterSettings clusterSettings, - IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter, - IndexNameExpressionResolver indexNameExpressionResolver, - Supplier nodesInCluster) { + public List getRestHandlers( + Settings settings, + RestController restController, + ClusterSettings clusterSettings, + IndexScopedSettings indexScopedSettings, + SettingsFilter settingsFilter, + IndexNameExpressionResolver indexNameExpressionResolver, + Supplier nodesInCluster + ) { List handlers = new ArrayList<>(); handlers.add(new PainlessExecuteAction.RestAction()); handlers.add(new PainlessContextAction.RestAction()); diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/PainlessScript.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/PainlessScript.java index 3d3ee5ff53010..2bd9acf5bae0c 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/PainlessScript.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/PainlessScript.java @@ -77,7 +77,7 @@ default ScriptException convertToScriptException(Throwable t, Map, List, List> entry : contexts.entrySet()) { ScriptContext context = entry.getKey(); PainlessLookup lookup = PainlessLookupBuilder.buildFromWhitelists(entry.getValue()); - contextsToCompilers.put(context, - new Compiler(context.instanceClazz, context.factoryClazz, context.statefulFactoryClazz, lookup)); + contextsToCompilers.put( + context, + new Compiler(context.instanceClazz, context.factoryClazz, context.statefulFactoryClazz, lookup) + ); contextsToLookups.put(context, lookup); } @@ -112,12 +112,7 @@ public String getType() { } @Override - public T compile( - String scriptName, - String scriptSource, - ScriptContext context, - Map params - ) { + public T compile(String scriptName, String scriptSource, ScriptContext context, Map params) { Compiler compiler = contextsToCompilers.get(context); // Check we ourselves are not being called by unprivileged code. @@ -156,11 +151,7 @@ public Set> getSupportedContexts() { * @param The factory class. * @return A factory class that will return script instances. */ - private Type generateStatefulFactory( - Loader loader, - ScriptContext context, - ScriptScope scriptScope - ) { + private Type generateStatefulFactory(Loader loader, ScriptContext context, ScriptScope scriptScope) { int classFrames = ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS; int classAccess = Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER | Opcodes.ACC_FINAL; String interfaceBase = Type.getType(context.statefulFactoryClazz).getInternalName(); @@ -181,17 +172,29 @@ private Type generateStatefulFactory( } for (int count = 0; count < newFactory.getParameterTypes().length; ++count) { - writer.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, "$arg" + count, - Type.getType(newFactory.getParameterTypes()[count]).getDescriptor(), null, null).visitEnd(); + writer.visitField( + Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, + "$arg" + count, + Type.getType(newFactory.getParameterTypes()[count]).getDescriptor(), + null, + null + ).visitEnd(); } - org.objectweb.asm.commons.Method base = - new org.objectweb.asm.commons.Method("", MethodType.methodType(void.class).toMethodDescriptorString()); - org.objectweb.asm.commons.Method init = new org.objectweb.asm.commons.Method("", - MethodType.methodType(void.class, newFactory.getParameterTypes()).toMethodDescriptorString()); - - GeneratorAdapter constructor = new GeneratorAdapter(Opcodes.ASM5, init, - writer.visitMethod(Opcodes.ACC_PUBLIC, init.getName(), init.getDescriptor(), null, null)); + org.objectweb.asm.commons.Method base = new org.objectweb.asm.commons.Method( + "", + MethodType.methodType(void.class).toMethodDescriptorString() + ); + org.objectweb.asm.commons.Method init = new org.objectweb.asm.commons.Method( + "", + MethodType.methodType(void.class, newFactory.getParameterTypes()).toMethodDescriptorString() + ); + + GeneratorAdapter constructor = new GeneratorAdapter( + Opcodes.ASM5, + init, + writer.visitMethod(Opcodes.ACC_PUBLIC, init.getName(), init.getDescriptor(), null, null) + ); constructor.visitCode(); constructor.loadThis(); constructor.invokeConstructor(OBJECT_TYPE, base); @@ -215,18 +218,24 @@ private Type generateStatefulFactory( } } - org.objectweb.asm.commons.Method instance = new org.objectweb.asm.commons.Method(newInstance.getName(), - MethodType.methodType(newInstance.getReturnType(), newInstance.getParameterTypes()).toMethodDescriptorString()); + org.objectweb.asm.commons.Method instance = new org.objectweb.asm.commons.Method( + newInstance.getName(), + MethodType.methodType(newInstance.getReturnType(), newInstance.getParameterTypes()).toMethodDescriptorString() + ); List> parameters = new ArrayList<>(Arrays.asList(newFactory.getParameterTypes())); parameters.addAll(Arrays.asList(newInstance.getParameterTypes())); - org.objectweb.asm.commons.Method constru = new org.objectweb.asm.commons.Method("", - MethodType.methodType(void.class, parameters.toArray(new Class[] {})).toMethodDescriptorString()); + org.objectweb.asm.commons.Method constru = new org.objectweb.asm.commons.Method( + "", + MethodType.methodType(void.class, parameters.toArray(new Class[] {})).toMethodDescriptorString() + ); - GeneratorAdapter adapter = new GeneratorAdapter(Opcodes.ASM5, instance, - writer.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL, - instance.getName(), instance.getDescriptor(), null, null)); + GeneratorAdapter adapter = new GeneratorAdapter( + Opcodes.ASM5, + instance, + writer.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL, instance.getName(), instance.getDescriptor(), null, null) + ); adapter.visitCode(); adapter.newInstance(WriterConstants.CLASS_TYPE); adapter.dup(); @@ -262,14 +271,9 @@ private Type generateStatefulFactory( * @param The factory class. * @return A factory class that will return script instances. */ - private T generateFactory( - Loader loader, - ScriptContext context, - Type classType, - ScriptScope scriptScope - ) { + private T generateFactory(Loader loader, ScriptContext context, Type classType, ScriptScope scriptScope) { int classFrames = ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS; - int classAccess = Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER| Opcodes.ACC_FINAL; + int classAccess = Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER | Opcodes.ACC_FINAL; String interfaceBase = Type.getType(context.factoryClazz).getInternalName(); String className = interfaceBase + "$Factory"; String[] classInterfaces = new String[] { interfaceBase }; @@ -277,11 +281,16 @@ private T generateFactory( ClassWriter writer = new ClassWriter(classFrames); writer.visit(WriterConstants.CLASS_VERSION, classAccess, className, null, OBJECT_TYPE.getInternalName(), classInterfaces); - org.objectweb.asm.commons.Method init = - new org.objectweb.asm.commons.Method("", MethodType.methodType(void.class).toMethodDescriptorString()); + org.objectweb.asm.commons.Method init = new org.objectweb.asm.commons.Method( + "", + MethodType.methodType(void.class).toMethodDescriptorString() + ); - GeneratorAdapter constructor = new GeneratorAdapter(Opcodes.ASM5, init, - writer.visitMethod(Opcodes.ACC_PUBLIC, init.getName(), init.getDescriptor(), null, null)); + GeneratorAdapter constructor = new GeneratorAdapter( + Opcodes.ASM5, + init, + writer.visitMethod(Opcodes.ACC_PUBLIC, init.getName(), init.getDescriptor(), null, null) + ); constructor.visitCode(); constructor.loadThis(); constructor.invokeConstructor(OBJECT_TYPE, init); @@ -299,14 +308,20 @@ private T generateFactory( } } - org.objectweb.asm.commons.Method instance = new org.objectweb.asm.commons.Method(reflect.getName(), - MethodType.methodType(reflect.getReturnType(), reflect.getParameterTypes()).toMethodDescriptorString()); - org.objectweb.asm.commons.Method constru = new org.objectweb.asm.commons.Method("", - MethodType.methodType(void.class, reflect.getParameterTypes()).toMethodDescriptorString()); - - GeneratorAdapter adapter = new GeneratorAdapter(Opcodes.ASM5, instance, - writer.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL, - instance.getName(), instance.getDescriptor(), null, null)); + org.objectweb.asm.commons.Method instance = new org.objectweb.asm.commons.Method( + reflect.getName(), + MethodType.methodType(reflect.getReturnType(), reflect.getParameterTypes()).toMethodDescriptorString() + ); + org.objectweb.asm.commons.Method constru = new org.objectweb.asm.commons.Method( + "", + MethodType.methodType(void.class, reflect.getParameterTypes()).toMethodDescriptorString() + ); + + GeneratorAdapter adapter = new GeneratorAdapter( + Opcodes.ASM5, + instance, + writer.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL, instance.getName(), instance.getDescriptor(), null, null) + ); adapter.visitCode(); adapter.newInstance(classType); adapter.dup(); @@ -318,11 +333,16 @@ private T generateFactory( writeNeedsMethods(context.factoryClazz, writer, scriptScope.getUsedVariables()); String methodName = "isResultDeterministic"; - org.objectweb.asm.commons.Method isResultDeterministic = new org.objectweb.asm.commons.Method(methodName, - MethodType.methodType(boolean.class).toMethodDescriptorString()); - - GeneratorAdapter deterAdapter = new GeneratorAdapter(Opcodes.ASM5, isResultDeterministic, - writer.visitMethod(Opcodes.ACC_PUBLIC, methodName, isResultDeterministic.getDescriptor(), null, null)); + org.objectweb.asm.commons.Method isResultDeterministic = new org.objectweb.asm.commons.Method( + methodName, + MethodType.methodType(boolean.class).toMethodDescriptorString() + ); + + GeneratorAdapter deterAdapter = new GeneratorAdapter( + Opcodes.ASM5, + isResultDeterministic, + writer.visitMethod(Opcodes.ACC_PUBLIC, methodName, isResultDeterministic.getDescriptor(), null, null) + ); deterAdapter.visitCode(); deterAdapter.push(scriptScope.isDeterministic()); deterAdapter.returnValue(); @@ -336,23 +356,31 @@ private T generateFactory( } catch (Exception exception) { // Catch everything to let the user know this is something caused internally. throw new IllegalStateException( - "An internal error occurred attempting to define the factory class [" + className + "].", exception); + "An internal error occurred attempting to define the factory class [" + className + "].", + exception + ); } } private void writeNeedsMethods(Class clazz, ClassWriter writer, Set extractedVariables) { for (Method method : clazz.getMethods()) { - if (method.getName().startsWith("needs") && - method.getReturnType().equals(boolean.class) && method.getParameterTypes().length == 0) { + if (method.getName().startsWith("needs") + && method.getReturnType().equals(boolean.class) + && method.getParameterTypes().length == 0) { String name = method.getName(); name = name.substring(5); name = Character.toLowerCase(name.charAt(0)) + name.substring(1); - org.objectweb.asm.commons.Method needs = new org.objectweb.asm.commons.Method(method.getName(), - MethodType.methodType(boolean.class).toMethodDescriptorString()); + org.objectweb.asm.commons.Method needs = new org.objectweb.asm.commons.Method( + method.getName(), + MethodType.methodType(boolean.class).toMethodDescriptorString() + ); - GeneratorAdapter adapter = new GeneratorAdapter(Opcodes.ASM5, needs, - writer.visitMethod(Opcodes.ACC_PUBLIC, needs.getName(), needs.getDescriptor(), null, null)); + GeneratorAdapter adapter = new GeneratorAdapter( + Opcodes.ASM5, + needs, + writer.visitMethod(Opcodes.ACC_PUBLIC, needs.getName(), needs.getDescriptor(), null, null) + ); adapter.visitCode(); adapter.push(extractedVariables.contains(name)); adapter.returnValue(); diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/ScriptClassInfo.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/ScriptClassInfo.java index f7c677d7835c4..869cdb54ca7c1 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/ScriptClassInfo.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/ScriptClassInfo.java @@ -59,25 +59,38 @@ public ScriptClassInfo(PainlessLookup painlessLookup, Class baseClass) { returnType = m.getReturnType(); } else { throw new IllegalArgumentException( - "Painless can only implement interfaces that have a single method named [execute] but [" + baseClass.getName() - + "] has more than one."); + "Painless can only implement interfaces that have a single method named [execute] but [" + + baseClass.getName() + + "] has more than one." + ); } - } else if (m.getName().startsWith("needs") && - m.getReturnType() == boolean.class && - m.getParameterTypes().length == 0) { + } else if (m.getName().startsWith("needs") && m.getReturnType() == boolean.class && m.getParameterTypes().length == 0) { needsMethods.add(new org.objectweb.asm.commons.Method(m.getName(), NEEDS_PARAMETER_METHOD_TYPE.toMethodDescriptorString())); - } else if (m.getName().startsWith("get") && - m.getName().equals("getClass") == false && - Modifier.isStatic(m.getModifiers()) == false) { - getReturns.add( - definitionTypeForClass(painlessLookup, m.getReturnType(), componentType -> "[" + m.getName() + "] has unknown return " + - "type [" + componentType.getName() + "]. Painless can only support getters with return types that are " + - "whitelisted.")); + } else if (m.getName().startsWith("get") + && m.getName().equals("getClass") == false + && Modifier.isStatic(m.getModifiers()) == false) { + getReturns.add( + definitionTypeForClass( + painlessLookup, + m.getReturnType(), + componentType -> "[" + + m.getName() + + "] has unknown return " + + "type [" + + componentType.getName() + + "]. Painless can only support getters with return types that are " + + "whitelisted." + ) + ); - getMethods.add(new org.objectweb.asm.commons.Method(m.getName(), - MethodType.methodType(m.getReturnType()).toMethodDescriptorString())); + getMethods.add( + new org.objectweb.asm.commons.Method( + m.getName(), + MethodType.methodType(m.getReturnType()).toMethodDescriptorString() + ) + ); - } + } } if (executeMethod == null) { @@ -86,18 +99,24 @@ public ScriptClassInfo(PainlessLookup painlessLookup, Class baseClass) { ArrayList converters = new ArrayList<>(); FunctionTable.LocalFunction defConverter = null; for (java.lang.reflect.Method m : baseClass.getMethods()) { - if (m.getName().startsWith("convertFrom") && - m.getParameterTypes().length == 1 && - m.getReturnType() == returnType && - Modifier.isStatic(m.getModifiers())) { + if (m.getName().startsWith("convertFrom") + && m.getParameterTypes().length == 1 + && m.getReturnType() == returnType + && Modifier.isStatic(m.getModifiers())) { if (m.getName().equals("convertFromDef")) { if (m.getParameterTypes()[0] != Object.class) { - throw new IllegalStateException("convertFromDef must take a single Object as an argument, " + - "not [" + m.getParameterTypes()[0] + "]"); + throw new IllegalStateException( + "convertFromDef must take a single Object as an argument, " + "not [" + m.getParameterTypes()[0] + "]" + ); } - defConverter = new FunctionTable.LocalFunction(m.getName(), m.getReturnType(), Arrays.asList(m.getParameterTypes()), - true, true); + defConverter = new FunctionTable.LocalFunction( + m.getName(), + m.getReturnType(), + Arrays.asList(m.getParameterTypes()), + true, + true + ); } else { converters.add( new FunctionTable.LocalFunction(m.getName(), m.getReturnType(), Arrays.asList(m.getParameterTypes()), true, true) @@ -110,17 +129,24 @@ public ScriptClassInfo(PainlessLookup painlessLookup, Class baseClass) { MethodType methodType = MethodType.methodType(executeMethod.getReturnType(), executeMethod.getParameterTypes()); this.executeMethod = new org.objectweb.asm.commons.Method(executeMethod.getName(), methodType.toMethodDescriptorString()); - executeMethodReturnType = definitionTypeForClass(painlessLookup, executeMethod.getReturnType(), - componentType -> "Painless can only implement execute methods returning a whitelisted type but [" + baseClass.getName() - + "#execute] returns [" + componentType.getName() + "] which isn't whitelisted."); + executeMethodReturnType = definitionTypeForClass( + painlessLookup, + executeMethod.getReturnType(), + componentType -> "Painless can only implement execute methods returning a whitelisted type but [" + + baseClass.getName() + + "#execute] returns [" + + componentType.getName() + + "] which isn't whitelisted." + ); // Look up the argument List arguments = new ArrayList<>(); String[] argumentNamesConstant = readArgumentNamesConstant(baseClass); Class[] types = executeMethod.getParameterTypes(); if (argumentNamesConstant.length != types.length) { - throw new IllegalArgumentException("[" + baseClass.getName() + "#ARGUMENTS] has length [2] but [" - + baseClass.getName() + "#execute] takes [1] argument."); + throw new IllegalArgumentException( + "[" + baseClass.getName() + "#ARGUMENTS] has length [2] but [" + baseClass.getName() + "#execute] takes [1] argument." + ); } for (int arg = 0; arg < types.length; arg++) { arguments.add(methodArgument(painlessLookup, types[arg], argumentNamesConstant[arg])); @@ -204,13 +230,23 @@ public String getName() { } private MethodArgument methodArgument(PainlessLookup painlessLookup, Class clazz, String argName) { - Class defClass = definitionTypeForClass(painlessLookup, clazz, componentType -> "[" + argName + "] is of unknown type [" - + componentType.getName() + ". Painless interfaces can only accept arguments that are of whitelisted types."); + Class defClass = definitionTypeForClass( + painlessLookup, + clazz, + componentType -> "[" + + argName + + "] is of unknown type [" + + componentType.getName() + + ". Painless interfaces can only accept arguments that are of whitelisted types." + ); return new MethodArgument(defClass, argName); } - private static Class definitionTypeForClass(PainlessLookup painlessLookup, Class type, - Function, String> unknownErrorMessageSource) { + private static Class definitionTypeForClass( + PainlessLookup painlessLookup, + Class type, + Function, String> unknownErrorMessageSource + ) { type = PainlessLookupUtility.javaTypeToType(type); Class componentType = type; @@ -230,12 +266,21 @@ private static String[] readArgumentNamesConstant(Class iface) { try { argumentNamesField = iface.getField("PARAMETERS"); } catch (NoSuchFieldException e) { - throw new IllegalArgumentException("Painless needs a constant [String[] PARAMETERS] on all interfaces it implements with the " - + "names of the method arguments but [" + iface.getName() + "] doesn't have one.", e); + throw new IllegalArgumentException( + "Painless needs a constant [String[] PARAMETERS] on all interfaces it implements with the " + + "names of the method arguments but [" + + iface.getName() + + "] doesn't have one.", + e + ); } if (false == argumentNamesField.getType().equals(String[].class)) { - throw new IllegalArgumentException("Painless needs a constant [String[] PARAMETERS] on all interfaces it implements with the " - + "names of the method arguments but [" + iface.getName() + "] doesn't have one."); + throw new IllegalArgumentException( + "Painless needs a constant [String[] PARAMETERS] on all interfaces it implements with the " + + "names of the method arguments but [" + + iface.getName() + + "] doesn't have one." + ); } try { return (String[]) argumentNamesField.get(null); diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/Utility.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/Utility.java index 03608224d31e0..b4e2f2a553830 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/Utility.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/Utility.java @@ -20,13 +20,15 @@ public static String charToString(final char value) { public static char StringTochar(final String value) { if (value == null) { - throw new ClassCastException("cannot cast " + - "null " + String.class.getCanonicalName() + " to " + char.class.getCanonicalName()); + throw new ClassCastException( + "cannot cast " + "null " + String.class.getCanonicalName() + " to " + char.class.getCanonicalName() + ); } if (value.length() != 1) { - throw new ClassCastException("cannot cast " + - String.class.getCanonicalName() + " with length not equal to one to " + char.class.getCanonicalName()); + throw new ClassCastException( + "cannot cast " + String.class.getCanonicalName() + " with length not equal to one to " + char.class.getCanonicalName() + ); } return value.charAt(0); diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/WriterConstants.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/WriterConstants.java index 2e211cbfc8c75..b30d45733cec4 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/WriterConstants.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/WriterConstants.java @@ -40,7 +40,7 @@ public final class WriterConstants { public static final String CTOR_METHOD_NAME = ""; - public static final Method CLINIT = getAsmMethod(void.class, ""); + public static final Method CLINIT = getAsmMethod(void.class, ""); public static final Type PAINLESS_ERROR_TYPE = Type.getType(PainlessError.class); @@ -67,63 +67,101 @@ public final class WriterConstants { public static final Method MATCHER_MATCHES = getAsmMethod(boolean.class, "matches"); public static final Method MATCHER_FIND = getAsmMethod(boolean.class, "find"); - public static final Method DEF_BOOTSTRAP_METHOD = getAsmMethod(CallSite.class, "$bootstrapDef", MethodHandles.Lookup.class, - String.class, MethodType.class, int.class, int.class, Object[].class); - static final Handle DEF_BOOTSTRAP_HANDLE = new Handle(Opcodes.H_INVOKESTATIC, CLASS_TYPE.getInternalName(), "$bootstrapDef", - DEF_BOOTSTRAP_METHOD.getDescriptor(), false); + public static final Method DEF_BOOTSTRAP_METHOD = getAsmMethod( + CallSite.class, + "$bootstrapDef", + MethodHandles.Lookup.class, + String.class, + MethodType.class, + int.class, + int.class, + Object[].class + ); + static final Handle DEF_BOOTSTRAP_HANDLE = new Handle( + Opcodes.H_INVOKESTATIC, + CLASS_TYPE.getInternalName(), + "$bootstrapDef", + DEF_BOOTSTRAP_METHOD.getDescriptor(), + false + ); public static final Type DEF_UTIL_TYPE = Type.getType(Def.class); public static final Method DEF_TO_P_BOOLEAN = getAsmMethod(boolean.class, "defToboolean", Object.class); - public static final Method DEF_TO_P_BYTE_IMPLICIT = getAsmMethod(byte.class , "defTobyteImplicit" , Object.class); - public static final Method DEF_TO_P_SHORT_IMPLICIT = getAsmMethod(short.class , "defToshortImplicit" , Object.class); - public static final Method DEF_TO_P_CHAR_IMPLICIT = getAsmMethod(char.class , "defTocharImplicit" , Object.class); - public static final Method DEF_TO_P_INT_IMPLICIT = getAsmMethod(int.class , "defTointImplicit" , Object.class); - public static final Method DEF_TO_P_LONG_IMPLICIT = getAsmMethod(long.class , "defTolongImplicit" , Object.class); - public static final Method DEF_TO_P_FLOAT_IMPLICIT = getAsmMethod(float.class , "defTofloatImplicit" , Object.class); - public static final Method DEF_TO_P_DOUBLE_IMPLICIT = getAsmMethod(double.class , "defTodoubleImplicit" , Object.class); - public static final Method DEF_TO_P_BYTE_EXPLICIT = getAsmMethod(byte.class , "defTobyteExplicit" , Object.class); - public static final Method DEF_TO_P_SHORT_EXPLICIT = getAsmMethod(short.class , "defToshortExplicit" , Object.class); - public static final Method DEF_TO_P_CHAR_EXPLICIT = getAsmMethod(char.class , "defTocharExplicit" , Object.class); - public static final Method DEF_TO_P_INT_EXPLICIT = getAsmMethod(int.class , "defTointExplicit" , Object.class); - public static final Method DEF_TO_P_LONG_EXPLICIT = getAsmMethod(long.class , "defTolongExplicit" , Object.class); - public static final Method DEF_TO_P_FLOAT_EXPLICIT = getAsmMethod(float.class , "defTofloatExplicit" , Object.class); - public static final Method DEF_TO_P_DOUBLE_EXPLICIT = getAsmMethod(double.class , "defTodoubleExplicit" , Object.class); + public static final Method DEF_TO_P_BYTE_IMPLICIT = getAsmMethod(byte.class, "defTobyteImplicit", Object.class); + public static final Method DEF_TO_P_SHORT_IMPLICIT = getAsmMethod(short.class, "defToshortImplicit", Object.class); + public static final Method DEF_TO_P_CHAR_IMPLICIT = getAsmMethod(char.class, "defTocharImplicit", Object.class); + public static final Method DEF_TO_P_INT_IMPLICIT = getAsmMethod(int.class, "defTointImplicit", Object.class); + public static final Method DEF_TO_P_LONG_IMPLICIT = getAsmMethod(long.class, "defTolongImplicit", Object.class); + public static final Method DEF_TO_P_FLOAT_IMPLICIT = getAsmMethod(float.class, "defTofloatImplicit", Object.class); + public static final Method DEF_TO_P_DOUBLE_IMPLICIT = getAsmMethod(double.class, "defTodoubleImplicit", Object.class); + public static final Method DEF_TO_P_BYTE_EXPLICIT = getAsmMethod(byte.class, "defTobyteExplicit", Object.class); + public static final Method DEF_TO_P_SHORT_EXPLICIT = getAsmMethod(short.class, "defToshortExplicit", Object.class); + public static final Method DEF_TO_P_CHAR_EXPLICIT = getAsmMethod(char.class, "defTocharExplicit", Object.class); + public static final Method DEF_TO_P_INT_EXPLICIT = getAsmMethod(int.class, "defTointExplicit", Object.class); + public static final Method DEF_TO_P_LONG_EXPLICIT = getAsmMethod(long.class, "defTolongExplicit", Object.class); + public static final Method DEF_TO_P_FLOAT_EXPLICIT = getAsmMethod(float.class, "defTofloatExplicit", Object.class); + public static final Method DEF_TO_P_DOUBLE_EXPLICIT = getAsmMethod(double.class, "defTodoubleExplicit", Object.class); public static final Method DEF_TO_B_BOOLEAN = getAsmMethod(Boolean.class, "defToBoolean", Object.class); - public static final Method DEF_TO_B_BYTE_IMPLICIT = getAsmMethod(Byte.class , "defToByteImplicit" , Object.class); - public static final Method DEF_TO_B_SHORT_IMPLICIT = getAsmMethod(Short.class , "defToShortImplicit" , Object.class); - public static final Method DEF_TO_B_CHARACTER_IMPLICIT = getAsmMethod(Character.class , "defToCharacterImplicit" , Object.class); - public static final Method DEF_TO_B_INTEGER_IMPLICIT = getAsmMethod(Integer.class , "defToIntegerImplicit" , Object.class); - public static final Method DEF_TO_B_LONG_IMPLICIT = getAsmMethod(Long.class , "defToLongImplicit" , Object.class); - public static final Method DEF_TO_B_FLOAT_IMPLICIT = getAsmMethod(Float.class , "defToFloatImplicit" , Object.class); - public static final Method DEF_TO_B_DOUBLE_IMPLICIT = getAsmMethod(Double.class , "defToDoubleImplicit" , Object.class); - public static final Method DEF_TO_B_BYTE_EXPLICIT = getAsmMethod(Byte.class , "defToByteExplicit" , Object.class); - public static final Method DEF_TO_B_SHORT_EXPLICIT = getAsmMethod(Short.class , "defToShortExplicit" , Object.class); - public static final Method DEF_TO_B_CHARACTER_EXPLICIT = getAsmMethod(Character.class , "defToCharacterExplicit" , Object.class); - public static final Method DEF_TO_B_INTEGER_EXPLICIT = getAsmMethod(Integer.class , "defToIntegerExplicit" , Object.class); - public static final Method DEF_TO_B_LONG_EXPLICIT = getAsmMethod(Long.class , "defToLongExplicit" , Object.class); - public static final Method DEF_TO_B_FLOAT_EXPLICIT = getAsmMethod(Float.class , "defToFloatExplicit" , Object.class); - public static final Method DEF_TO_B_DOUBLE_EXPLICIT = getAsmMethod(Double.class , "defToDoubleExplicit" , Object.class); + public static final Method DEF_TO_B_BYTE_IMPLICIT = getAsmMethod(Byte.class, "defToByteImplicit", Object.class); + public static final Method DEF_TO_B_SHORT_IMPLICIT = getAsmMethod(Short.class, "defToShortImplicit", Object.class); + public static final Method DEF_TO_B_CHARACTER_IMPLICIT = getAsmMethod(Character.class, "defToCharacterImplicit", Object.class); + public static final Method DEF_TO_B_INTEGER_IMPLICIT = getAsmMethod(Integer.class, "defToIntegerImplicit", Object.class); + public static final Method DEF_TO_B_LONG_IMPLICIT = getAsmMethod(Long.class, "defToLongImplicit", Object.class); + public static final Method DEF_TO_B_FLOAT_IMPLICIT = getAsmMethod(Float.class, "defToFloatImplicit", Object.class); + public static final Method DEF_TO_B_DOUBLE_IMPLICIT = getAsmMethod(Double.class, "defToDoubleImplicit", Object.class); + public static final Method DEF_TO_B_BYTE_EXPLICIT = getAsmMethod(Byte.class, "defToByteExplicit", Object.class); + public static final Method DEF_TO_B_SHORT_EXPLICIT = getAsmMethod(Short.class, "defToShortExplicit", Object.class); + public static final Method DEF_TO_B_CHARACTER_EXPLICIT = getAsmMethod(Character.class, "defToCharacterExplicit", Object.class); + public static final Method DEF_TO_B_INTEGER_EXPLICIT = getAsmMethod(Integer.class, "defToIntegerExplicit", Object.class); + public static final Method DEF_TO_B_LONG_EXPLICIT = getAsmMethod(Long.class, "defToLongExplicit", Object.class); + public static final Method DEF_TO_B_FLOAT_EXPLICIT = getAsmMethod(Float.class, "defToFloatExplicit", Object.class); + public static final Method DEF_TO_B_DOUBLE_EXPLICIT = getAsmMethod(Double.class, "defToDoubleExplicit", Object.class); public static final Method DEF_TO_STRING_IMPLICIT = getAsmMethod(String.class, "defToStringImplicit", Object.class); public static final Method DEF_TO_STRING_EXPLICIT = getAsmMethod(String.class, "defToStringExplicit", Object.class); /** invokedynamic bootstrap for lambda expression/method references */ - public static final MethodType LAMBDA_BOOTSTRAP_TYPE = - MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, String.class, MethodType.class, - MethodType.class, String.class, int.class, String.class, MethodType.class, int.class, int.class, Object[].class); - public static final Handle LAMBDA_BOOTSTRAP_HANDLE = - new Handle(Opcodes.H_INVOKESTATIC, Type.getInternalName(LambdaBootstrap.class), - "lambdaBootstrap", LAMBDA_BOOTSTRAP_TYPE.toMethodDescriptorString(), false); - public static final MethodType DELEGATE_BOOTSTRAP_TYPE = - MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, String.class, MethodType.class, MethodHandle.class, - int.class, Object[].class); - public static final Handle DELEGATE_BOOTSTRAP_HANDLE = - new Handle(Opcodes.H_INVOKESTATIC, Type.getInternalName(LambdaBootstrap.class), - "delegateBootstrap", DELEGATE_BOOTSTRAP_TYPE.toMethodDescriptorString(), false); + public static final MethodType LAMBDA_BOOTSTRAP_TYPE = MethodType.methodType( + CallSite.class, + MethodHandles.Lookup.class, + String.class, + MethodType.class, + MethodType.class, + String.class, + int.class, + String.class, + MethodType.class, + int.class, + int.class, + Object[].class + ); + public static final Handle LAMBDA_BOOTSTRAP_HANDLE = new Handle( + Opcodes.H_INVOKESTATIC, + Type.getInternalName(LambdaBootstrap.class), + "lambdaBootstrap", + LAMBDA_BOOTSTRAP_TYPE.toMethodDescriptorString(), + false + ); + public static final MethodType DELEGATE_BOOTSTRAP_TYPE = MethodType.methodType( + CallSite.class, + MethodHandles.Lookup.class, + String.class, + MethodType.class, + MethodHandle.class, + int.class, + Object[].class + ); + public static final Handle DELEGATE_BOOTSTRAP_HANDLE = new Handle( + Opcodes.H_INVOKESTATIC, + Type.getInternalName(LambdaBootstrap.class), + "delegateBootstrap", + DELEGATE_BOOTSTRAP_TYPE.toMethodDescriptorString(), + false + ); /** dynamic invokedynamic bootstrap for indy string concats (Java 9+) */ public static final Handle INDY_STRING_CONCAT_BOOTSTRAP_HANDLE; @@ -148,16 +186,16 @@ public final class WriterConstants { public static final Type STRING_TYPE = Type.getType(String.class); public static final Type STRINGBUILDER_TYPE = Type.getType(StringBuilder.class); - public static final Method STRINGBUILDER_CONSTRUCTOR = getAsmMethod(void.class, CTOR_METHOD_NAME); + public static final Method STRINGBUILDER_CONSTRUCTOR = getAsmMethod(void.class, CTOR_METHOD_NAME); public static final Method STRINGBUILDER_APPEND_BOOLEAN = getAsmMethod(StringBuilder.class, "append", boolean.class); - public static final Method STRINGBUILDER_APPEND_CHAR = getAsmMethod(StringBuilder.class, "append", char.class); - public static final Method STRINGBUILDER_APPEND_INT = getAsmMethod(StringBuilder.class, "append", int.class); - public static final Method STRINGBUILDER_APPEND_LONG = getAsmMethod(StringBuilder.class, "append", long.class); - public static final Method STRINGBUILDER_APPEND_FLOAT = getAsmMethod(StringBuilder.class, "append", float.class); - public static final Method STRINGBUILDER_APPEND_DOUBLE = getAsmMethod(StringBuilder.class, "append", double.class); - public static final Method STRINGBUILDER_APPEND_STRING = getAsmMethod(StringBuilder.class, "append", String.class); - public static final Method STRINGBUILDER_APPEND_OBJECT = getAsmMethod(StringBuilder.class, "append", Object.class); - public static final Method STRINGBUILDER_TOSTRING = getAsmMethod(String.class, "toString"); + public static final Method STRINGBUILDER_APPEND_CHAR = getAsmMethod(StringBuilder.class, "append", char.class); + public static final Method STRINGBUILDER_APPEND_INT = getAsmMethod(StringBuilder.class, "append", int.class); + public static final Method STRINGBUILDER_APPEND_LONG = getAsmMethod(StringBuilder.class, "append", long.class); + public static final Method STRINGBUILDER_APPEND_FLOAT = getAsmMethod(StringBuilder.class, "append", float.class); + public static final Method STRINGBUILDER_APPEND_DOUBLE = getAsmMethod(StringBuilder.class, "append", double.class); + public static final Method STRINGBUILDER_APPEND_STRING = getAsmMethod(StringBuilder.class, "append", String.class); + public static final Method STRINGBUILDER_APPEND_OBJECT = getAsmMethod(StringBuilder.class, "append", Object.class); + public static final Method STRINGBUILDER_TOSTRING = getAsmMethod(String.class, "toString"); public static final Type OBJECTS_TYPE = Type.getType(Objects.class); public static final Method EQUALS = getAsmMethod(boolean.class, "equals", Object.class, Object.class); diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessContextAction.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessContextAction.java index 174cdfe418c9e..5f159821bbe1b 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessContextAction.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessContextAction.java @@ -16,13 +16,10 @@ import org.elasticsearch.action.support.ActionFilters; import org.elasticsearch.action.support.HandledTransportAction; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.ToXContentObject; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.painless.PainlessScriptEngine; import org.elasticsearch.painless.lookup.PainlessLookup; import org.elasticsearch.rest.BaseRestHandler; @@ -31,6 +28,9 @@ import org.elasticsearch.script.ScriptContext; import org.elasticsearch.tasks.Task; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.ToXContentObject; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.ArrayList; @@ -142,7 +142,7 @@ public static class TransportAction extends HandledTransportAction)Request::new); + super(NAME, transportService, actionFilters, (Writeable.Reader) Request::new); this.painlessScriptEngine = painlessScriptEngine; } @@ -152,15 +152,18 @@ protected void doExecute(Task task, Request request, ActionListener li PainlessContextInfo painlessContextInfo; if (request.scriptContextName == null) { - scriptContextNames = - painlessScriptEngine.getContextsToLookups().keySet().stream().map(v -> v.name).collect(Collectors.toList()); + scriptContextNames = painlessScriptEngine.getContextsToLookups() + .keySet() + .stream() + .map(v -> v.name) + .collect(Collectors.toList()); painlessContextInfo = null; } else { ScriptContext scriptContext = null; PainlessLookup painlessLookup = null; - for (Map.Entry, PainlessLookup> contextLookupEntry : - painlessScriptEngine.getContextsToLookups().entrySet()) { + for (Map.Entry, PainlessLookup> contextLookupEntry : painlessScriptEngine.getContextsToLookups() + .entrySet()) { if (contextLookupEntry.getKey().name.equals(request.getScriptContextName())) { scriptContext = contextLookupEntry.getKey(); painlessLookup = contextLookupEntry.getValue(); diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessContextClassBindingInfo.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessContextClassBindingInfo.java index 92c6a8c357671..2e3a56d2af98c 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessContextClassBindingInfo.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessContextClassBindingInfo.java @@ -8,17 +8,17 @@ package org.elasticsearch.painless.action; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.painless.lookup.PainlessClassBinding; +import org.elasticsearch.painless.lookup.PainlessLookupUtility; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.painless.lookup.PainlessClassBinding; -import org.elasticsearch.painless.lookup.PainlessLookupUtility; import java.io.IOException; import java.util.Collections; @@ -36,15 +36,8 @@ public class PainlessContextClassBindingInfo implements Writeable, ToXContentObj @SuppressWarnings("unchecked") private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - PainlessContextClassBindingInfo.class.getCanonicalName(), - (v) -> - new PainlessContextClassBindingInfo( - (String)v[0], - (String)v[1], - (String)v[2], - (int)v[3], - (List)v[4] - ) + PainlessContextClassBindingInfo.class.getCanonicalName(), + (v) -> new PainlessContextClassBindingInfo((String) v[0], (String) v[1], (String) v[2], (int) v[3], (List) v[4]) ); static { @@ -63,11 +56,11 @@ public class PainlessContextClassBindingInfo implements Writeable, ToXContentObj public PainlessContextClassBindingInfo(PainlessClassBinding painlessClassBinding) { this( - painlessClassBinding.javaMethod.getDeclaringClass().getName(), - painlessClassBinding.javaMethod.getName(), - painlessClassBinding.returnType.getName(), - painlessClassBinding.javaConstructor.getParameterCount(), - painlessClassBinding.typeParameters.stream().map(Class::getName).collect(Collectors.toList()) + painlessClassBinding.javaMethod.getDeclaringClass().getName(), + painlessClassBinding.javaMethod.getName(), + painlessClassBinding.returnType.getName(), + painlessClassBinding.javaConstructor.getParameterCount(), + painlessClassBinding.typeParameters.stream().map(Class::getName).collect(Collectors.toList()) ); } @@ -122,11 +115,11 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; PainlessContextClassBindingInfo that = (PainlessContextClassBindingInfo) o; - return readOnly == that.readOnly && - Objects.equals(declaring, that.declaring) && - Objects.equals(name, that.name) && - Objects.equals(rtn, that.rtn) && - Objects.equals(parameters, that.parameters); + return readOnly == that.readOnly + && Objects.equals(declaring, that.declaring) + && Objects.equals(name, that.name) + && Objects.equals(rtn, that.rtn) + && Objects.equals(parameters, that.parameters); } @Override @@ -136,13 +129,21 @@ public int hashCode() { @Override public String toString() { - return "PainlessContextClassBindingInfo{" + - "declaring='" + declaring + '\'' + - ", name='" + name + '\'' + - ", rtn='" + rtn + '\'' + - ", readOnly=" + readOnly + - ", parameters=" + parameters + - '}'; + return "PainlessContextClassBindingInfo{" + + "declaring='" + + declaring + + '\'' + + ", name='" + + name + + '\'' + + ", rtn='" + + rtn + + '\'' + + ", readOnly=" + + readOnly + + ", parameters=" + + parameters + + '}'; } public String getDeclaring() { diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessContextClassInfo.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessContextClassInfo.java index 80560a82d92ca..af180a401e75d 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessContextClassInfo.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessContextClassInfo.java @@ -8,15 +8,15 @@ package org.elasticsearch.painless.action; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.painless.lookup.PainlessClass; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.painless.lookup.PainlessClass; import java.io.IOException; import java.util.ArrayList; @@ -38,32 +38,38 @@ public class PainlessContextClassInfo implements Writeable, ToXContentObject { @SuppressWarnings("unchecked") private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - PainlessContextClassInfo.class.getCanonicalName(), - (v) -> - new PainlessContextClassInfo( - (String)v[0], - (boolean)v[1], - (List)v[2], - (List)v[3], - (List)v[4], - (List)v[5], - (List)v[6] - ) + PainlessContextClassInfo.class.getCanonicalName(), + (v) -> new PainlessContextClassInfo( + (String) v[0], + (boolean) v[1], + (List) v[2], + (List) v[3], + (List) v[4], + (List) v[5], + (List) v[6] + ) ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), NAME); PARSER.declareBoolean(ConstructingObjectParser.constructorArg(), IMPORTED); - PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), - (p, c) -> PainlessContextConstructorInfo.fromXContent(p), CONSTRUCTORS); - PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), - (p, c) -> PainlessContextMethodInfo.fromXContent(p), STATIC_METHODS); - PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), - (p, c) -> PainlessContextMethodInfo.fromXContent(p), METHODS); - PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), - (p, c) -> PainlessContextFieldInfo.fromXContent(p), STATIC_FIELDS); - PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), - (p, c) -> PainlessContextFieldInfo.fromXContent(p), FIELDS); + PARSER.declareObjectArray( + ConstructingObjectParser.constructorArg(), + (p, c) -> PainlessContextConstructorInfo.fromXContent(p), + CONSTRUCTORS + ); + PARSER.declareObjectArray( + ConstructingObjectParser.constructorArg(), + (p, c) -> PainlessContextMethodInfo.fromXContent(p), + STATIC_METHODS + ); + PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), (p, c) -> PainlessContextMethodInfo.fromXContent(p), METHODS); + PARSER.declareObjectArray( + ConstructingObjectParser.constructorArg(), + (p, c) -> PainlessContextFieldInfo.fromXContent(p), + STATIC_FIELDS + ); + PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), (p, c) -> PainlessContextFieldInfo.fromXContent(p), FIELDS); } private final String name; @@ -76,20 +82,25 @@ public class PainlessContextClassInfo implements Writeable, ToXContentObject { public PainlessContextClassInfo(Class javaClass, boolean imported, PainlessClass painlessClass) { this( - javaClass.getName(), - imported, - painlessClass.constructors.values().stream().map(PainlessContextConstructorInfo::new).collect(Collectors.toList()), - painlessClass.staticMethods.values().stream().map(PainlessContextMethodInfo::new).collect(Collectors.toList()), - painlessClass.methods.values().stream().map(PainlessContextMethodInfo::new).collect(Collectors.toList()), - painlessClass.staticFields.values().stream().map(PainlessContextFieldInfo::new).collect(Collectors.toList()), - painlessClass.fields.values().stream().map(PainlessContextFieldInfo::new).collect(Collectors.toList()) + javaClass.getName(), + imported, + painlessClass.constructors.values().stream().map(PainlessContextConstructorInfo::new).collect(Collectors.toList()), + painlessClass.staticMethods.values().stream().map(PainlessContextMethodInfo::new).collect(Collectors.toList()), + painlessClass.methods.values().stream().map(PainlessContextMethodInfo::new).collect(Collectors.toList()), + painlessClass.staticFields.values().stream().map(PainlessContextFieldInfo::new).collect(Collectors.toList()), + painlessClass.fields.values().stream().map(PainlessContextFieldInfo::new).collect(Collectors.toList()) ); } - public PainlessContextClassInfo(String name, boolean imported, - List constructors, - List staticMethods, List methods, - List staticFields, List fields) { + public PainlessContextClassInfo( + String name, + boolean imported, + List constructors, + List staticMethods, + List methods, + List staticFields, + List fields + ) { this.name = Objects.requireNonNull(name); this.imported = imported; @@ -159,13 +170,13 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; PainlessContextClassInfo that = (PainlessContextClassInfo) o; - return imported == that.imported && - Objects.equals(name, that.name) && - Objects.equals(constructors, that.constructors) && - Objects.equals(staticMethods, that.staticMethods) && - Objects.equals(methods, that.methods) && - Objects.equals(staticFields, that.staticFields) && - Objects.equals(fields, that.fields); + return imported == that.imported + && Objects.equals(name, that.name) + && Objects.equals(constructors, that.constructors) + && Objects.equals(staticMethods, that.staticMethods) + && Objects.equals(methods, that.methods) + && Objects.equals(staticFields, that.staticFields) + && Objects.equals(fields, that.fields); } @Override @@ -175,15 +186,23 @@ public int hashCode() { @Override public String toString() { - return "PainlessContextClassInfo{" + - "name='" + name + '\'' + - ", imported=" + imported + - ", constructors=" + constructors + - ", staticMethods=" + staticMethods + - ", methods=" + methods + - ", staticFields=" + staticFields + - ", fields=" + fields + - '}'; + return "PainlessContextClassInfo{" + + "name='" + + name + + '\'' + + ", imported=" + + imported + + ", constructors=" + + constructors + + ", staticMethods=" + + staticMethods + + ", methods=" + + methods + + ", staticFields=" + + staticFields + + ", fields=" + + fields + + '}'; } public String getName() { diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessContextConstructorInfo.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessContextConstructorInfo.java index 81c3825012bb4..53fe0722a1f73 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessContextConstructorInfo.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessContextConstructorInfo.java @@ -8,17 +8,17 @@ package org.elasticsearch.painless.action; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.painless.lookup.PainlessConstructor; +import org.elasticsearch.painless.lookup.PainlessLookupUtility; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.painless.lookup.PainlessConstructor; -import org.elasticsearch.painless.lookup.PainlessLookupUtility; import java.io.IOException; import java.util.Collections; @@ -36,12 +36,8 @@ public class PainlessContextConstructorInfo implements Writeable, ToXContentObje @SuppressWarnings("unchecked") private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - PainlessContextConstructorInfo.class.getCanonicalName(), - (v) -> - new PainlessContextConstructorInfo( - (String)v[0], - (List)v[1] - ) + PainlessContextConstructorInfo.class.getCanonicalName(), + (v) -> new PainlessContextConstructorInfo((String) v[0], (List) v[1]) ); static { @@ -50,11 +46,9 @@ public class PainlessContextConstructorInfo implements Writeable, ToXContentObje } public PainlessContextConstructorInfo(PainlessConstructor painlessConstructor) { - this ( - painlessConstructor.javaConstructor.getDeclaringClass().getName(), - painlessConstructor.typeParameters.stream() - .map(c -> PainlessContextTypeInfo.getType(c.getName())) - .collect(Collectors.toList()) + this( + painlessConstructor.javaConstructor.getDeclaringClass().getName(), + painlessConstructor.typeParameters.stream().map(c -> PainlessContextTypeInfo.getType(c.getName())).collect(Collectors.toList()) ); } @@ -97,8 +91,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; PainlessContextConstructorInfo that = (PainlessContextConstructorInfo) o; - return Objects.equals(declaring, that.declaring) && - Objects.equals(parameters, that.parameters); + return Objects.equals(declaring, that.declaring) && Objects.equals(parameters, that.parameters); } @Override @@ -108,10 +101,7 @@ public int hashCode() { @Override public String toString() { - return "PainlessContextConstructorInfo{" + - "declaring='" + declaring + '\'' + - ", parameters=" + parameters + - '}'; + return "PainlessContextConstructorInfo{" + "declaring='" + declaring + '\'' + ", parameters=" + parameters + '}'; } public String getDeclaring() { diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessContextFieldInfo.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessContextFieldInfo.java index b0461fc2de57f..d43f3608949c3 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessContextFieldInfo.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessContextFieldInfo.java @@ -8,16 +8,16 @@ package org.elasticsearch.painless.action; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.painless.lookup.PainlessField; +import org.elasticsearch.painless.lookup.PainlessLookupUtility; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.painless.lookup.PainlessField; -import org.elasticsearch.painless.lookup.PainlessLookupUtility; import java.io.IOException; import java.util.Objects; @@ -29,13 +29,8 @@ public class PainlessContextFieldInfo implements Writeable, ToXContentObject { public static final ParseField TYPE = new ParseField("type"); private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - PainlessContextFieldInfo.class.getCanonicalName(), - (v) -> - new PainlessContextFieldInfo( - (String)v[0], - (String)v[1], - (String)v[2] - ) + PainlessContextFieldInfo.class.getCanonicalName(), + (v) -> new PainlessContextFieldInfo((String) v[0], (String) v[1], (String) v[2]) ); static { @@ -50,9 +45,9 @@ public class PainlessContextFieldInfo implements Writeable, ToXContentObject { public PainlessContextFieldInfo(PainlessField painlessField) { this( - painlessField.javaField.getDeclaringClass().getName(), - painlessField.javaField.getName(), - PainlessContextTypeInfo.getType(painlessField.typeParameter.getName()) + painlessField.javaField.getDeclaringClass().getName(), + painlessField.javaField.getName(), + PainlessContextTypeInfo.getType(painlessField.typeParameter.getName()) ); } @@ -99,9 +94,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; PainlessContextFieldInfo that = (PainlessContextFieldInfo) o; - return Objects.equals(declaring, that.declaring) && - Objects.equals(name, that.name) && - Objects.equals(type, that.type); + return Objects.equals(declaring, that.declaring) && Objects.equals(name, that.name) && Objects.equals(type, that.type); } @Override @@ -111,11 +104,7 @@ public int hashCode() { @Override public String toString() { - return "PainlessContextFieldInfo{" + - "declaring='" + declaring + '\'' + - ", name='" + name + '\'' + - ", type='" + type + '\'' + - '}'; + return "PainlessContextFieldInfo{" + "declaring='" + declaring + '\'' + ", name='" + name + '\'' + ", type='" + type + '\'' + '}'; } public String getDeclaring() { diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessContextInfo.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessContextInfo.java index 413c4be0615a9..b72cac65bb545 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessContextInfo.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessContextInfo.java @@ -8,19 +8,19 @@ package org.elasticsearch.painless.action; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.ConstructingObjectParser; -import org.elasticsearch.xcontent.ToXContentObject; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.painless.lookup.PainlessClassBinding; import org.elasticsearch.painless.lookup.PainlessInstanceBinding; import org.elasticsearch.painless.lookup.PainlessLookup; import org.elasticsearch.painless.lookup.PainlessMethod; import org.elasticsearch.script.ScriptContext; +import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.ToXContentObject; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.ArrayList; @@ -40,27 +40,34 @@ public class PainlessContextInfo implements Writeable, ToXContentObject { @SuppressWarnings("unchecked") private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - PainlessContextInfo.class.getCanonicalName(), - (v) -> - new PainlessContextInfo( - (String)v[0], - (List)v[1], - (List)v[2], - (List)v[3], - (List)v[4] - ) + PainlessContextInfo.class.getCanonicalName(), + (v) -> new PainlessContextInfo( + (String) v[0], + (List) v[1], + (List) v[2], + (List) v[3], + (List) v[4] + ) ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), NAME); - PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), - (p, c) -> PainlessContextClassInfo.fromXContent(p), CLASSES); - PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), - (p, c) -> PainlessContextMethodInfo.fromXContent(p), IMPORTED_METHODS); - PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), - (p, c) -> PainlessContextClassBindingInfo.fromXContent(p), CLASS_BINDINGS); - PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), - (p, c) -> PainlessContextInstanceBindingInfo.fromXContent(p), INSTANCE_BINDINGS); + PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), (p, c) -> PainlessContextClassInfo.fromXContent(p), CLASSES); + PARSER.declareObjectArray( + ConstructingObjectParser.constructorArg(), + (p, c) -> PainlessContextMethodInfo.fromXContent(p), + IMPORTED_METHODS + ); + PARSER.declareObjectArray( + ConstructingObjectParser.constructorArg(), + (p, c) -> PainlessContextClassBindingInfo.fromXContent(p), + CLASS_BINDINGS + ); + PARSER.declareObjectArray( + ConstructingObjectParser.constructorArg(), + (p, c) -> PainlessContextInstanceBindingInfo.fromXContent(p), + INSTANCE_BINDINGS + ); } private final String name; @@ -71,43 +78,59 @@ public class PainlessContextInfo implements Writeable, ToXContentObject { public PainlessContextInfo(ScriptContext scriptContext, PainlessLookup painlessLookup) { this( - scriptContext.name, - painlessLookup.getClasses().stream().map( - javaClass -> new PainlessContextClassInfo( - javaClass, - javaClass == painlessLookup.canonicalTypeNameToType( - javaClass.getName().substring(javaClass.getName().lastIndexOf('.') + 1).replace('$', '.')), - painlessLookup.lookupPainlessClass(javaClass)) - ).collect(Collectors.toList()), - painlessLookup.getImportedPainlessMethodsKeys().stream().map(importedPainlessMethodKey -> { - String[] split = importedPainlessMethodKey.split("/"); - String importedPainlessMethodName = split[0]; - int importedPainlessMethodArity = Integer.parseInt(split[1]); - PainlessMethod importedPainlessMethod = - painlessLookup.lookupImportedPainlessMethod(importedPainlessMethodName, importedPainlessMethodArity); - return new PainlessContextMethodInfo(importedPainlessMethod); - }).collect(Collectors.toList()), - painlessLookup.getPainlessClassBindingsKeys().stream().map(painlessClassBindingKey -> { - String[] split = painlessClassBindingKey.split("/"); - String painlessClassBindingName = split[0]; - int painlessClassBindingArity = Integer.parseInt(split[1]); - PainlessClassBinding painlessClassBinding = - painlessLookup.lookupPainlessClassBinding(painlessClassBindingName, painlessClassBindingArity); - return new PainlessContextClassBindingInfo(painlessClassBinding); - }).collect(Collectors.toList()), - painlessLookup.getPainlessInstanceBindingsKeys().stream().map(painlessInstanceBindingKey -> { - String[] split = painlessInstanceBindingKey.split("/"); - String painlessInstanceBindingName = split[0]; - int painlessInstanceBindingArity = Integer.parseInt(split[1]); - PainlessInstanceBinding painlessInstanceBinding = - painlessLookup.lookupPainlessInstanceBinding(painlessInstanceBindingName, painlessInstanceBindingArity); - return new PainlessContextInstanceBindingInfo(painlessInstanceBinding); - }).collect(Collectors.toList()) + scriptContext.name, + painlessLookup.getClasses() + .stream() + .map( + javaClass -> new PainlessContextClassInfo( + javaClass, + javaClass == painlessLookup.canonicalTypeNameToType( + javaClass.getName().substring(javaClass.getName().lastIndexOf('.') + 1).replace('$', '.') + ), + painlessLookup.lookupPainlessClass(javaClass) + ) + ) + .collect(Collectors.toList()), + painlessLookup.getImportedPainlessMethodsKeys().stream().map(importedPainlessMethodKey -> { + String[] split = importedPainlessMethodKey.split("/"); + String importedPainlessMethodName = split[0]; + int importedPainlessMethodArity = Integer.parseInt(split[1]); + PainlessMethod importedPainlessMethod = painlessLookup.lookupImportedPainlessMethod( + importedPainlessMethodName, + importedPainlessMethodArity + ); + return new PainlessContextMethodInfo(importedPainlessMethod); + }).collect(Collectors.toList()), + painlessLookup.getPainlessClassBindingsKeys().stream().map(painlessClassBindingKey -> { + String[] split = painlessClassBindingKey.split("/"); + String painlessClassBindingName = split[0]; + int painlessClassBindingArity = Integer.parseInt(split[1]); + PainlessClassBinding painlessClassBinding = painlessLookup.lookupPainlessClassBinding( + painlessClassBindingName, + painlessClassBindingArity + ); + return new PainlessContextClassBindingInfo(painlessClassBinding); + }).collect(Collectors.toList()), + painlessLookup.getPainlessInstanceBindingsKeys().stream().map(painlessInstanceBindingKey -> { + String[] split = painlessInstanceBindingKey.split("/"); + String painlessInstanceBindingName = split[0]; + int painlessInstanceBindingArity = Integer.parseInt(split[1]); + PainlessInstanceBinding painlessInstanceBinding = painlessLookup.lookupPainlessInstanceBinding( + painlessInstanceBindingName, + painlessInstanceBindingArity + ); + return new PainlessContextInstanceBindingInfo(painlessInstanceBinding); + }).collect(Collectors.toList()) ); } - public PainlessContextInfo(String name, List classes, List importedMethods, - List classBindings, List instanceBindings) { + public PainlessContextInfo( + String name, + List classes, + List importedMethods, + List classBindings, + List instanceBindings + ) { this.name = Objects.requireNonNull(name); classes = new ArrayList<>(Objects.requireNonNull(classes)); classes.sort(Comparator.comparing(PainlessContextClassInfo::getSortValue)); @@ -162,11 +185,11 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; PainlessContextInfo that = (PainlessContextInfo) o; - return Objects.equals(name, that.name) && - Objects.equals(classes, that.classes) && - Objects.equals(importedMethods, that.importedMethods) && - Objects.equals(classBindings, that.classBindings) && - Objects.equals(instanceBindings, that.instanceBindings); + return Objects.equals(name, that.name) + && Objects.equals(classes, that.classes) + && Objects.equals(importedMethods, that.importedMethods) + && Objects.equals(classBindings, that.classBindings) + && Objects.equals(instanceBindings, that.instanceBindings); } @Override @@ -176,13 +199,19 @@ public int hashCode() { @Override public String toString() { - return "PainlessContextInfo{" + - "name='" + name + '\'' + - ", classes=" + classes + - ", importedMethods=" + importedMethods + - ", classBindings=" + classBindings + - ", instanceBindings=" + instanceBindings + - '}'; + return "PainlessContextInfo{" + + "name='" + + name + + '\'' + + ", classes=" + + classes + + ", importedMethods=" + + importedMethods + + ", classBindings=" + + classBindings + + ", instanceBindings=" + + instanceBindings + + '}'; } public String getName() { diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessContextInstanceBindingInfo.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessContextInstanceBindingInfo.java index d77a862f59883..af5c98904149f 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessContextInstanceBindingInfo.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessContextInstanceBindingInfo.java @@ -8,17 +8,17 @@ package org.elasticsearch.painless.action; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.painless.lookup.PainlessInstanceBinding; +import org.elasticsearch.painless.lookup.PainlessLookupUtility; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.painless.lookup.PainlessInstanceBinding; -import org.elasticsearch.painless.lookup.PainlessLookupUtility; import java.io.IOException; import java.util.Collections; @@ -35,14 +35,8 @@ public class PainlessContextInstanceBindingInfo implements Writeable, ToXContent @SuppressWarnings("unchecked") private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - PainlessContextInstanceBindingInfo.class.getCanonicalName(), - (v) -> - new PainlessContextInstanceBindingInfo( - (String)v[0], - (String)v[1], - (String)v[2], - (List)v[3] - ) + PainlessContextInstanceBindingInfo.class.getCanonicalName(), + (v) -> new PainlessContextInstanceBindingInfo((String) v[0], (String) v[1], (String) v[2], (List) v[3]) ); static { @@ -59,10 +53,10 @@ public class PainlessContextInstanceBindingInfo implements Writeable, ToXContent public PainlessContextInstanceBindingInfo(PainlessInstanceBinding painlessInstanceBinding) { this( - painlessInstanceBinding.javaMethod.getDeclaringClass().getName(), - painlessInstanceBinding.javaMethod.getName(), - painlessInstanceBinding.returnType.getName(), - painlessInstanceBinding.typeParameters.stream().map(Class::getName).collect(Collectors.toList()) + painlessInstanceBinding.javaMethod.getDeclaringClass().getName(), + painlessInstanceBinding.javaMethod.getName(), + painlessInstanceBinding.returnType.getName(), + painlessInstanceBinding.typeParameters.stream().map(Class::getName).collect(Collectors.toList()) ); } @@ -114,10 +108,10 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; PainlessContextInstanceBindingInfo that = (PainlessContextInstanceBindingInfo) o; - return Objects.equals(declaring, that.declaring) && - Objects.equals(name, that.name) && - Objects.equals(rtn, that.rtn) && - Objects.equals(parameters, that.parameters); + return Objects.equals(declaring, that.declaring) + && Objects.equals(name, that.name) + && Objects.equals(rtn, that.rtn) + && Objects.equals(parameters, that.parameters); } @Override @@ -127,12 +121,19 @@ public int hashCode() { @Override public String toString() { - return "PainlessContextInstanceBindingInfo{" + - "declaring='" + declaring + '\'' + - ", name='" + name + '\'' + - ", rtn='" + rtn + '\'' + - ", parameters=" + parameters + - '}'; + return "PainlessContextInstanceBindingInfo{" + + "declaring='" + + declaring + + '\'' + + ", name='" + + name + + '\'' + + ", rtn='" + + rtn + + '\'' + + ", parameters=" + + parameters + + '}'; } public String getDeclaring() { diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessContextMethodInfo.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessContextMethodInfo.java index bba26e43c2530..579cd84974782 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessContextMethodInfo.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessContextMethodInfo.java @@ -8,17 +8,17 @@ package org.elasticsearch.painless.action; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.painless.lookup.PainlessLookupUtility; +import org.elasticsearch.painless.lookup.PainlessMethod; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.painless.lookup.PainlessLookupUtility; -import org.elasticsearch.painless.lookup.PainlessMethod; import java.io.IOException; import java.util.Collections; @@ -35,14 +35,8 @@ public class PainlessContextMethodInfo implements Writeable, ToXContentObject { @SuppressWarnings("unchecked") private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - PainlessContextMethodInfo.class.getCanonicalName(), - (v) -> - new PainlessContextMethodInfo( - (String)v[0], - (String)v[1], - (String)v[2], - (List)v[3] - ) + PainlessContextMethodInfo.class.getCanonicalName(), + (v) -> new PainlessContextMethodInfo((String) v[0], (String) v[1], (String) v[2], (List) v[3]) ); static { @@ -59,10 +53,10 @@ public class PainlessContextMethodInfo implements Writeable, ToXContentObject { public PainlessContextMethodInfo(PainlessMethod painlessMethod) { this( - painlessMethod.javaMethod.getDeclaringClass().getName(), - painlessMethod.javaMethod.getName(), - PainlessContextTypeInfo.getType(painlessMethod.returnType.getName()), - painlessMethod.typeParameters.stream().map(c -> PainlessContextTypeInfo.getType(c.getName())).collect(Collectors.toList()) + painlessMethod.javaMethod.getDeclaringClass().getName(), + painlessMethod.javaMethod.getName(), + PainlessContextTypeInfo.getType(painlessMethod.returnType.getName()), + painlessMethod.typeParameters.stream().map(c -> PainlessContextTypeInfo.getType(c.getName())).collect(Collectors.toList()) ); } @@ -113,10 +107,10 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; PainlessContextMethodInfo that = (PainlessContextMethodInfo) o; - return Objects.equals(declaring, that.declaring) && - Objects.equals(name, that.name) && - Objects.equals(rtn, that.rtn) && - Objects.equals(parameters, that.parameters); + return Objects.equals(declaring, that.declaring) + && Objects.equals(name, that.name) + && Objects.equals(rtn, that.rtn) + && Objects.equals(parameters, that.parameters); } @Override @@ -126,12 +120,19 @@ public int hashCode() { @Override public String toString() { - return "PainlessContextMethodInfo{" + - "declaring='" + declaring + '\'' + - ", name='" + name + '\'' + - ", rtn='" + rtn + '\'' + - ", parameters=" + parameters + - '}'; + return "PainlessContextMethodInfo{" + + "declaring='" + + declaring + + '\'' + + ", name='" + + name + + '\'' + + ", rtn='" + + rtn + + '\'' + + ", parameters=" + + parameters + + '}'; } public String getDeclaring() { diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessExecuteAction.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessExecuteAction.java index 439c9f1a0ecb5..47303d0dd3bf4 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessExecuteAction.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessExecuteAction.java @@ -34,23 +34,16 @@ import org.elasticsearch.cluster.routing.ShardsIterator; import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.CheckedBiFunction; -import org.elasticsearch.common.geo.GeometryFormatterFactory; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.geo.GeoPoint; +import org.elasticsearch.common.geo.GeometryFormatterFactory; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.network.NetworkAddress; -import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.ToXContentObject; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.geometry.Point; import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexService; @@ -86,6 +79,13 @@ import org.elasticsearch.search.lookup.SearchLookup; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.ToXContentObject; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; import java.io.IOException; import java.util.ArrayList; @@ -116,7 +116,9 @@ public static class Request extends SingleShardRequest implements ToXCo private static final ParseField CONTEXT_FIELD = new ParseField("context"); private static final ParseField CONTEXT_SETUP_FIELD = new ParseField("context_setup"); private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "painless_execute_request", args -> new Request((Script) args[0], (String) args[1], (ContextSetup) args[2])); + "painless_execute_request", + args -> new Request((Script) args[0], (String) args[1], (ContextSetup) args[2]) + ); static { PARSER.declareObject(ConstructingObjectParser.constructorArg(), (p, c) -> Script.parse(p), SCRIPT_FIELD); @@ -150,9 +152,10 @@ static class ContextSetup implements Writeable, ToXContentObject { private static final ParseField INDEX_FIELD = new ParseField("index"); private static final ParseField DOCUMENT_FIELD = new ParseField("document"); private static final ParseField QUERY_FIELD = new ParseField("query"); - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("execute_script_context", - args -> new ContextSetup((String) args[0], (BytesReference) args[1], (QueryBuilder) args[2])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "execute_script_context", + args -> new ContextSetup((String) args[0], (BytesReference) args[1], (QueryBuilder) args[2]) + ); static { PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), INDEX_FIELD); @@ -162,8 +165,11 @@ static class ContextSetup implements Writeable, ToXContentObject { return BytesReference.bytes(b); } }, DOCUMENT_FIELD); - PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> - AbstractQueryBuilder.parseInnerQueryBuilder(p), QUERY_FIELD); + PARSER.declareObject( + ConstructingObjectParser.optionalConstructorArg(), + (p, c) -> AbstractQueryBuilder.parseInnerQueryBuilder(p), + QUERY_FIELD + ); } private final String index; @@ -188,7 +194,7 @@ static ContextSetup parse(XContentParser parser, Void context) throws IOExceptio index = in.readOptionalString(); document = in.readOptionalBytesReference(); String xContentType = in.readOptionalString(); - if (xContentType != null) { + if (xContentType != null) { this.xContentType = XContentType.fromMediaType(xContentType); } query = in.readOptionalNamedWriteable(QueryBuilder.class); @@ -219,10 +225,10 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; ContextSetup that = (ContextSetup) o; - return Objects.equals(index, that.index) && - Objects.equals(document, that.document) && - Objects.equals(query, that.query) && - Objects.equals(xContentType, that.xContentType); + return Objects.equals(index, that.index) + && Objects.equals(document, that.document) + && Objects.equals(query, that.query) + && Objects.equals(xContentType, that.xContentType); } @Override @@ -234,18 +240,23 @@ public int hashCode() { public void writeTo(StreamOutput out) throws IOException { out.writeOptionalString(index); out.writeOptionalBytesReference(document); - out.writeOptionalString(xContentType != null ? xContentType.mediaTypeWithoutParameters(): null); + out.writeOptionalString(xContentType != null ? xContentType.mediaTypeWithoutParameters() : null); out.writeOptionalNamedWriteable(query); } @Override public String toString() { - return "ContextSetup{" + - ", index='" + index + '\'' + - ", document=" + document + - ", query=" + query + - ", xContentType=" + xContentType + - '}'; + return "ContextSetup{" + + ", index='" + + index + + '\'' + + ", document=" + + document + + ", query=" + + query + + ", xContentType=" + + xContentType + + '}'; } @Override @@ -257,8 +268,14 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws } if (document != null) { builder.field(DOCUMENT_FIELD.getPreferredName()); - try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, - LoggingDeprecationHandler.INSTANCE, document, xContentType)) { + try ( + XContentParser parser = XContentHelper.createParser( + NamedXContentRegistry.EMPTY, + LoggingDeprecationHandler.INSTANCE, + document, + xContentType + ) + ) { builder.generator().copyCurrentStructure(parser); } } @@ -353,9 +370,9 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Request request = (Request) o; - return Objects.equals(script, request.script) && - Objects.equals(context, request.context) && - Objects.equals(contextSetup, request.contextSetup); + return Objects.equals(script, request.script) + && Objects.equals(context, request.context) + && Objects.equals(contextSetup, request.contextSetup); } @Override @@ -365,11 +382,7 @@ public int hashCode() { @Override public String toString() { - return "Request{" + - "script=" + script + - "context=" + context + - ", contextSetup=" + contextSetup + - '}'; + return "Request{" + "script=" + script + "context=" + context + ", contextSetup=" + contextSetup + '}'; } static boolean needDocumentAndIndex(ScriptContext scriptContext) { @@ -453,14 +466,28 @@ public static class TransportAction extends TransportSingleShardAction { ScoreScript.Factory factory = scriptService.compile(request.script, ScoreScript.CONTEXT); SearchLookup lookup = context.lookup(); - ScoreScript.LeafFactory leafFactory = - factory.newFactory(request.getScript().getParams(), lookup); + ScoreScript.LeafFactory leafFactory = factory.newFactory(request.getScript().getParams(), lookup); ScoreScript scoreScript = leafFactory.newInstance(new DocValuesDocReader(lookup, leafReaderContext)); scoreScript.setDocument(0); @@ -555,8 +580,11 @@ static Response innerShardOperation(Request request, ScriptService scriptService } else if (scriptContext == BooleanFieldScript.CONTEXT) { return prepareRamIndex(request, (context, leafReaderContext) -> { BooleanFieldScript.Factory factory = scriptService.compile(request.script, BooleanFieldScript.CONTEXT); - BooleanFieldScript.LeafFactory leafFactory = - factory.newFactory(BooleanFieldScript.CONTEXT.name, request.getScript().getParams(), context.lookup()); + BooleanFieldScript.LeafFactory leafFactory = factory.newFactory( + BooleanFieldScript.CONTEXT.name, + request.getScript().getParams(), + context.lookup() + ); BooleanFieldScript booleanFieldScript = leafFactory.newInstance(leafReaderContext); List booleans = new ArrayList<>(); booleanFieldScript.runForDoc(0, booleans::add); @@ -565,8 +593,12 @@ static Response innerShardOperation(Request request, ScriptService scriptService } else if (scriptContext == DateFieldScript.CONTEXT) { return prepareRamIndex(request, (context, leafReaderContext) -> { DateFieldScript.Factory factory = scriptService.compile(request.script, DateFieldScript.CONTEXT); - DateFieldScript.LeafFactory leafFactory = factory.newFactory(DateFieldScript.CONTEXT.name, - request.getScript().getParams(), context.lookup(), DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER); + DateFieldScript.LeafFactory leafFactory = factory.newFactory( + DateFieldScript.CONTEXT.name, + request.getScript().getParams(), + context.lookup(), + DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER + ); DateFieldScript dateFieldScript = leafFactory.newInstance(leafReaderContext); List dates = new ArrayList<>(); dateFieldScript.runForDoc(0, d -> dates.add(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.formatMillis(d))); @@ -575,8 +607,11 @@ static Response innerShardOperation(Request request, ScriptService scriptService } else if (scriptContext == DoubleFieldScript.CONTEXT) { return prepareRamIndex(request, (context, leafReaderContext) -> { DoubleFieldScript.Factory factory = scriptService.compile(request.script, DoubleFieldScript.CONTEXT); - DoubleFieldScript.LeafFactory leafFactory = - factory.newFactory(DoubleFieldScript.CONTEXT.name, request.getScript().getParams(), context.lookup()); + DoubleFieldScript.LeafFactory leafFactory = factory.newFactory( + DoubleFieldScript.CONTEXT.name, + request.getScript().getParams(), + context.lookup() + ); DoubleFieldScript doubleFieldScript = leafFactory.newInstance(leafReaderContext); List doubles = new ArrayList<>(); doubleFieldScript.runForDoc(0, doubles::add); @@ -585,21 +620,29 @@ static Response innerShardOperation(Request request, ScriptService scriptService } else if (scriptContext == GeoPointFieldScript.CONTEXT) { return prepareRamIndex(request, (context, leafReaderContext) -> { GeoPointFieldScript.Factory factory = scriptService.compile(request.script, GeoPointFieldScript.CONTEXT); - GeoPointFieldScript.LeafFactory leafFactory = - factory.newFactory(GeoPointFieldScript.CONTEXT.name, request.getScript().getParams(), context.lookup()); + GeoPointFieldScript.LeafFactory leafFactory = factory.newFactory( + GeoPointFieldScript.CONTEXT.name, + request.getScript().getParams(), + context.lookup() + ); GeoPointFieldScript geoPointFieldScript = leafFactory.newInstance(leafReaderContext); List points = new ArrayList<>(); geoPointFieldScript.runGeoPointForDoc(0, gp -> points.add(new GeoPoint(gp))); // convert geo points to the standard format of the fields api - Function, List> format = - GeometryFormatterFactory.getFormatter(GeometryFormatterFactory.GEOJSON, p -> new Point(p.lon(), p.lat())); + Function, List> format = GeometryFormatterFactory.getFormatter( + GeometryFormatterFactory.GEOJSON, + p -> new Point(p.lon(), p.lat()) + ); return new Response(format.apply(points)); }, indexService); } else if (scriptContext == IpFieldScript.CONTEXT) { return prepareRamIndex(request, (context, leafReaderContext) -> { IpFieldScript.Factory factory = scriptService.compile(request.script, IpFieldScript.CONTEXT); - IpFieldScript.LeafFactory leafFactory = - factory.newFactory(IpFieldScript.CONTEXT.name, request.getScript().getParams(), context.lookup()); + IpFieldScript.LeafFactory leafFactory = factory.newFactory( + IpFieldScript.CONTEXT.name, + request.getScript().getParams(), + context.lookup() + ); IpFieldScript ipFieldScript = leafFactory.newInstance(leafReaderContext); List ips = new ArrayList<>(); ipFieldScript.runForDoc(0, ip -> { @@ -614,8 +657,11 @@ static Response innerShardOperation(Request request, ScriptService scriptService } else if (scriptContext == LongFieldScript.CONTEXT) { return prepareRamIndex(request, (context, leafReaderContext) -> { LongFieldScript.Factory factory = scriptService.compile(request.script, LongFieldScript.CONTEXT); - LongFieldScript.LeafFactory leafFactory = - factory.newFactory(LongFieldScript.CONTEXT.name, request.getScript().getParams(), context.lookup()); + LongFieldScript.LeafFactory leafFactory = factory.newFactory( + LongFieldScript.CONTEXT.name, + request.getScript().getParams(), + context.lookup() + ); LongFieldScript longFieldScript = leafFactory.newInstance(leafReaderContext); List longs = new ArrayList<>(); longFieldScript.runForDoc(0, longs::add); @@ -624,8 +670,11 @@ static Response innerShardOperation(Request request, ScriptService scriptService } else if (scriptContext == StringFieldScript.CONTEXT) { return prepareRamIndex(request, (context, leafReaderContext) -> { StringFieldScript.Factory factory = scriptService.compile(request.script, StringFieldScript.CONTEXT); - StringFieldScript.LeafFactory leafFactory = - factory.newFactory(StringFieldScript.CONTEXT.name, request.getScript().getParams(), context.lookup()); + StringFieldScript.LeafFactory leafFactory = factory.newFactory( + StringFieldScript.CONTEXT.name, + request.getScript().getParams(), + context.lookup() + ); StringFieldScript stringFieldScript = leafFactory.newInstance(leafReaderContext); List keywords = new ArrayList<>(); stringFieldScript.runForDoc(0, keywords::add); @@ -634,8 +683,11 @@ static Response innerShardOperation(Request request, ScriptService scriptService } else if (scriptContext == CompositeFieldScript.CONTEXT) { return prepareRamIndex(request, (context, leafReaderContext) -> { CompositeFieldScript.Factory factory = scriptService.compile(request.script, CompositeFieldScript.CONTEXT); - CompositeFieldScript.LeafFactory leafFactory = - factory.newFactory(CompositeFieldScript.CONTEXT.name, request.getScript().getParams(), context.lookup()); + CompositeFieldScript.LeafFactory leafFactory = factory.newFactory( + CompositeFieldScript.CONTEXT.name, + request.getScript().getParams(), + context.lookup() + ); CompositeFieldScript compositeFieldScript = leafFactory.newInstance(leafReaderContext); return new Response(compositeFieldScript.runForDoc(0)); }, indexService); @@ -644,9 +696,11 @@ static Response innerShardOperation(Request request, ScriptService scriptService } } - private static Response prepareRamIndex(Request request, - CheckedBiFunction handler, - IndexService indexService) throws IOException { + private static Response prepareRamIndex( + Request request, + CheckedBiFunction handler, + IndexService indexService + ) throws IOException { Analyzer defaultAnalyzer = indexService.getIndexAnalyzers().getDefaultIndexAnalyzer(); @@ -658,18 +712,24 @@ private static Response prepareRamIndex(Request request, SourceToParse sourceToParse = new SourceToParse(index, "_id", document, xContentType); MappingLookup mappingLookup = indexService.mapperService().mappingLookup(); DocumentParser documentParser = indexService.mapperService().documentParser(); - //Note that we are not doing anything with dynamic mapping updates, hence fields that are not mapped but are present - //in the sample doc are not accessible from the script through doc['field']. - //This is a problem especially for indices that have no mappings, as no fields will be accessible, neither through doc - //nor _source (if there are no mappings there are no metadata fields). + // Note that we are not doing anything with dynamic mapping updates, hence fields that are not mapped but are present + // in the sample doc are not accessible from the script through doc['field']. + // This is a problem especially for indices that have no mappings, as no fields will be accessible, neither through doc + // nor _source (if there are no mappings there are no metadata fields). ParsedDocument parsedDocument = documentParser.parseDocument(sourceToParse, mappingLookup); indexWriter.addDocuments(parsedDocument.docs()); try (IndexReader indexReader = DirectoryReader.open(indexWriter)) { final IndexSearcher searcher = new IndexSearcher(indexReader); searcher.setQueryCache(null); final long absoluteStartMillis = System.currentTimeMillis(); - SearchExecutionContext context = - indexService.newSearchExecutionContext(0, 0, searcher, () -> absoluteStartMillis, null, emptyMap()); + SearchExecutionContext context = indexService.newSearchExecutionContext( + 0, + 0, + searcher, + () -> absoluteStartMillis, + null, + emptyMap() + ); return handler.apply(context, indexReader.leaves().get(0)); } } @@ -681,9 +741,7 @@ public static class RestAction extends BaseRestHandler { @Override public List routes() { - return List.of( - new Route(GET, "/_scripts/painless/_execute"), - new Route(POST, "/_scripts/painless/_execute")); + return List.of(new Route(GET, "/_scripts/painless/_execute"), new Route(POST, "/_scripts/painless/_execute")); } @Override diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/EnhancedPainlessLexer.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/EnhancedPainlessLexer.java index 296161d3296fd..622dd5e46d522 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/EnhancedPainlessLexer.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/EnhancedPainlessLexer.java @@ -52,8 +52,11 @@ public void recover(final LexerNoViableAltException lnvae) { if ((firstChar == '\'' || firstChar == '"') && text.length() - 2 > 0 && text.charAt(text.length() - 2) == '\\') { /* Use a simple heuristic to guess if the unrecognized characters were trying to be a string but has a broken escape sequence. * If it was add an extra message about valid string escape sequences. */ - message += " The only valid escape sequences in strings starting with [" + firstChar + "] are [\\\\] and [\\" - + firstChar + "]."; + message += " The only valid escape sequences in strings starting with [" + + firstChar + + "] are [\\\\] and [\\" + + firstChar + + "]."; } throw location.createError(new IllegalArgumentException(message, lnvae)); } @@ -65,18 +68,18 @@ protected boolean isSlashRegex() { return true; } switch (lastToken.getType()) { - case PainlessLexer.RBRACE: - case PainlessLexer.RP: - case PainlessLexer.OCTAL: - case PainlessLexer.HEX: - case PainlessLexer.INTEGER: - case PainlessLexer.DECIMAL: - case PainlessLexer.ID: - case PainlessLexer.DOTINTEGER: - case PainlessLexer.DOTID: - return false; - default: - return true; + case PainlessLexer.RBRACE: + case PainlessLexer.RP: + case PainlessLexer.OCTAL: + case PainlessLexer.HEX: + case PainlessLexer.INTEGER: + case PainlessLexer.DECIMAL: + case PainlessLexer.ID: + case PainlessLexer.DOTINTEGER: + case PainlessLexer.DOTID: + return false; + default: + return true; } } } diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/PainlessLexer.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/PainlessLexer.java index 0ef95c671d898..320ab38f39ce2 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/PainlessLexer.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/PainlessLexer.java @@ -1,386 +1,610 @@ // ANTLR GENERATED CODE: DO NOT EDIT package org.elasticsearch.painless.antlr; -import org.antlr.v4.runtime.Lexer; -import org.antlr.v4.runtime.CharStream; -import org.antlr.v4.runtime.Token; -import org.antlr.v4.runtime.TokenStream; + import org.antlr.v4.runtime.*; +import org.antlr.v4.runtime.CharStream; +import org.antlr.v4.runtime.Lexer; import org.antlr.v4.runtime.atn.*; import org.antlr.v4.runtime.dfa.DFA; import org.antlr.v4.runtime.misc.*; -@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"}) +@SuppressWarnings({ "all", "warnings", "unchecked", "unused", "cast" }) abstract class PainlessLexer extends Lexer { - static { RuntimeMetaData.checkVersion("4.5.3", RuntimeMetaData.VERSION); } + static { + RuntimeMetaData.checkVersion("4.5.3", RuntimeMetaData.VERSION); + } - protected static final DFA[] _decisionToDFA; - protected static final PredictionContextCache _sharedContextCache = - new PredictionContextCache(); - public static final int - WS=1, COMMENT=2, LBRACK=3, RBRACK=4, LBRACE=5, RBRACE=6, LP=7, RP=8, DOT=9, - NSDOT=10, COMMA=11, SEMICOLON=12, IF=13, IN=14, ELSE=15, WHILE=16, DO=17, - FOR=18, CONTINUE=19, BREAK=20, RETURN=21, NEW=22, TRY=23, CATCH=24, THROW=25, - THIS=26, INSTANCEOF=27, BOOLNOT=28, BWNOT=29, MUL=30, DIV=31, REM=32, - ADD=33, SUB=34, LSH=35, RSH=36, USH=37, LT=38, LTE=39, GT=40, GTE=41, - EQ=42, EQR=43, NE=44, NER=45, BWAND=46, XOR=47, BWOR=48, BOOLAND=49, BOOLOR=50, - COND=51, COLON=52, ELVIS=53, REF=54, ARROW=55, FIND=56, MATCH=57, INCR=58, - DECR=59, ASSIGN=60, AADD=61, ASUB=62, AMUL=63, ADIV=64, AREM=65, AAND=66, - AXOR=67, AOR=68, ALSH=69, ARSH=70, AUSH=71, OCTAL=72, HEX=73, INTEGER=74, - DECIMAL=75, STRING=76, REGEX=77, TRUE=78, FALSE=79, NULL=80, PRIMITIVE=81, - DEF=82, ID=83, DOTINTEGER=84, DOTID=85; - public static final int AFTER_DOT = 1; - public static String[] modeNames = { - "DEFAULT_MODE", "AFTER_DOT" - }; + protected static final DFA[] _decisionToDFA; + protected static final PredictionContextCache _sharedContextCache = new PredictionContextCache(); + public static final int WS = 1, COMMENT = 2, LBRACK = 3, RBRACK = 4, LBRACE = 5, RBRACE = 6, LP = 7, RP = 8, DOT = 9, NSDOT = 10, + COMMA = 11, SEMICOLON = 12, IF = 13, IN = 14, ELSE = 15, WHILE = 16, DO = 17, FOR = 18, CONTINUE = 19, BREAK = 20, RETURN = 21, + NEW = 22, TRY = 23, CATCH = 24, THROW = 25, THIS = 26, INSTANCEOF = 27, BOOLNOT = 28, BWNOT = 29, MUL = 30, DIV = 31, REM = 32, + ADD = 33, SUB = 34, LSH = 35, RSH = 36, USH = 37, LT = 38, LTE = 39, GT = 40, GTE = 41, EQ = 42, EQR = 43, NE = 44, NER = 45, + BWAND = 46, XOR = 47, BWOR = 48, BOOLAND = 49, BOOLOR = 50, COND = 51, COLON = 52, ELVIS = 53, REF = 54, ARROW = 55, FIND = 56, + MATCH = 57, INCR = 58, DECR = 59, ASSIGN = 60, AADD = 61, ASUB = 62, AMUL = 63, ADIV = 64, AREM = 65, AAND = 66, AXOR = 67, AOR = + 68, ALSH = 69, ARSH = 70, AUSH = 71, OCTAL = 72, HEX = 73, INTEGER = 74, DECIMAL = 75, STRING = 76, REGEX = 77, TRUE = 78, + FALSE = 79, NULL = 80, PRIMITIVE = 81, DEF = 82, ID = 83, DOTINTEGER = 84, DOTID = 85; + public static final int AFTER_DOT = 1; + public static String[] modeNames = { "DEFAULT_MODE", "AFTER_DOT" }; - public static final String[] ruleNames = { - "WS", "COMMENT", "LBRACK", "RBRACK", "LBRACE", "RBRACE", "LP", "RP", "DOT", - "NSDOT", "COMMA", "SEMICOLON", "IF", "IN", "ELSE", "WHILE", "DO", "FOR", - "CONTINUE", "BREAK", "RETURN", "NEW", "TRY", "CATCH", "THROW", "THIS", - "INSTANCEOF", "BOOLNOT", "BWNOT", "MUL", "DIV", "REM", "ADD", "SUB", "LSH", - "RSH", "USH", "LT", "LTE", "GT", "GTE", "EQ", "EQR", "NE", "NER", "BWAND", - "XOR", "BWOR", "BOOLAND", "BOOLOR", "COND", "COLON", "ELVIS", "REF", "ARROW", - "FIND", "MATCH", "INCR", "DECR", "ASSIGN", "AADD", "ASUB", "AMUL", "ADIV", - "AREM", "AAND", "AXOR", "AOR", "ALSH", "ARSH", "AUSH", "OCTAL", "HEX", - "INTEGER", "DECIMAL", "STRING", "REGEX", "TRUE", "FALSE", "NULL", "PRIMITIVE", - "DEF", "ID", "DOTINTEGER", "DOTID" - }; + public static final String[] ruleNames = { + "WS", + "COMMENT", + "LBRACK", + "RBRACK", + "LBRACE", + "RBRACE", + "LP", + "RP", + "DOT", + "NSDOT", + "COMMA", + "SEMICOLON", + "IF", + "IN", + "ELSE", + "WHILE", + "DO", + "FOR", + "CONTINUE", + "BREAK", + "RETURN", + "NEW", + "TRY", + "CATCH", + "THROW", + "THIS", + "INSTANCEOF", + "BOOLNOT", + "BWNOT", + "MUL", + "DIV", + "REM", + "ADD", + "SUB", + "LSH", + "RSH", + "USH", + "LT", + "LTE", + "GT", + "GTE", + "EQ", + "EQR", + "NE", + "NER", + "BWAND", + "XOR", + "BWOR", + "BOOLAND", + "BOOLOR", + "COND", + "COLON", + "ELVIS", + "REF", + "ARROW", + "FIND", + "MATCH", + "INCR", + "DECR", + "ASSIGN", + "AADD", + "ASUB", + "AMUL", + "ADIV", + "AREM", + "AAND", + "AXOR", + "AOR", + "ALSH", + "ARSH", + "AUSH", + "OCTAL", + "HEX", + "INTEGER", + "DECIMAL", + "STRING", + "REGEX", + "TRUE", + "FALSE", + "NULL", + "PRIMITIVE", + "DEF", + "ID", + "DOTINTEGER", + "DOTID" }; - private static final String[] _LITERAL_NAMES = { - null, null, null, "'{'", "'}'", "'['", "']'", "'('", "')'", "'.'", "'?.'", - "','", "';'", "'if'", "'in'", "'else'", "'while'", "'do'", "'for'", "'continue'", - "'break'", "'return'", "'new'", "'try'", "'catch'", "'throw'", "'this'", - "'instanceof'", "'!'", "'~'", "'*'", "'/'", "'%'", "'+'", "'-'", "'<<'", - "'>>'", "'>>>'", "'<'", "'<='", "'>'", "'>='", "'=='", "'==='", "'!='", - "'!=='", "'&'", "'^'", "'|'", "'&&'", "'||'", "'?'", "':'", "'?:'", "'::'", - "'->'", "'=~'", "'==~'", "'++'", "'--'", "'='", "'+='", "'-='", "'*='", - "'/='", "'%='", "'&='", "'^='", "'|='", "'<<='", "'>>='", "'>>>='", null, - null, null, null, null, null, "'true'", "'false'", "'null'", null, "'def'" - }; - private static final String[] _SYMBOLIC_NAMES = { - null, "WS", "COMMENT", "LBRACK", "RBRACK", "LBRACE", "RBRACE", "LP", "RP", - "DOT", "NSDOT", "COMMA", "SEMICOLON", "IF", "IN", "ELSE", "WHILE", "DO", - "FOR", "CONTINUE", "BREAK", "RETURN", "NEW", "TRY", "CATCH", "THROW", - "THIS", "INSTANCEOF", "BOOLNOT", "BWNOT", "MUL", "DIV", "REM", "ADD", - "SUB", "LSH", "RSH", "USH", "LT", "LTE", "GT", "GTE", "EQ", "EQR", "NE", - "NER", "BWAND", "XOR", "BWOR", "BOOLAND", "BOOLOR", "COND", "COLON", "ELVIS", - "REF", "ARROW", "FIND", "MATCH", "INCR", "DECR", "ASSIGN", "AADD", "ASUB", - "AMUL", "ADIV", "AREM", "AAND", "AXOR", "AOR", "ALSH", "ARSH", "AUSH", - "OCTAL", "HEX", "INTEGER", "DECIMAL", "STRING", "REGEX", "TRUE", "FALSE", - "NULL", "PRIMITIVE", "DEF", "ID", "DOTINTEGER", "DOTID" - }; - public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); + private static final String[] _LITERAL_NAMES = { + null, + null, + null, + "'{'", + "'}'", + "'['", + "']'", + "'('", + "')'", + "'.'", + "'?.'", + "','", + "';'", + "'if'", + "'in'", + "'else'", + "'while'", + "'do'", + "'for'", + "'continue'", + "'break'", + "'return'", + "'new'", + "'try'", + "'catch'", + "'throw'", + "'this'", + "'instanceof'", + "'!'", + "'~'", + "'*'", + "'/'", + "'%'", + "'+'", + "'-'", + "'<<'", + "'>>'", + "'>>>'", + "'<'", + "'<='", + "'>'", + "'>='", + "'=='", + "'==='", + "'!='", + "'!=='", + "'&'", + "'^'", + "'|'", + "'&&'", + "'||'", + "'?'", + "':'", + "'?:'", + "'::'", + "'->'", + "'=~'", + "'==~'", + "'++'", + "'--'", + "'='", + "'+='", + "'-='", + "'*='", + "'/='", + "'%='", + "'&='", + "'^='", + "'|='", + "'<<='", + "'>>='", + "'>>>='", + null, + null, + null, + null, + null, + null, + "'true'", + "'false'", + "'null'", + null, + "'def'" }; + private static final String[] _SYMBOLIC_NAMES = { + null, + "WS", + "COMMENT", + "LBRACK", + "RBRACK", + "LBRACE", + "RBRACE", + "LP", + "RP", + "DOT", + "NSDOT", + "COMMA", + "SEMICOLON", + "IF", + "IN", + "ELSE", + "WHILE", + "DO", + "FOR", + "CONTINUE", + "BREAK", + "RETURN", + "NEW", + "TRY", + "CATCH", + "THROW", + "THIS", + "INSTANCEOF", + "BOOLNOT", + "BWNOT", + "MUL", + "DIV", + "REM", + "ADD", + "SUB", + "LSH", + "RSH", + "USH", + "LT", + "LTE", + "GT", + "GTE", + "EQ", + "EQR", + "NE", + "NER", + "BWAND", + "XOR", + "BWOR", + "BOOLAND", + "BOOLOR", + "COND", + "COLON", + "ELVIS", + "REF", + "ARROW", + "FIND", + "MATCH", + "INCR", + "DECR", + "ASSIGN", + "AADD", + "ASUB", + "AMUL", + "ADIV", + "AREM", + "AAND", + "AXOR", + "AOR", + "ALSH", + "ARSH", + "AUSH", + "OCTAL", + "HEX", + "INTEGER", + "DECIMAL", + "STRING", + "REGEX", + "TRUE", + "FALSE", + "NULL", + "PRIMITIVE", + "DEF", + "ID", + "DOTINTEGER", + "DOTID" }; + public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); - /** - * @deprecated Use {@link #VOCABULARY} instead. - */ - @Deprecated - public static final String[] tokenNames; - static { - tokenNames = new String[_SYMBOLIC_NAMES.length]; - for (int i = 0; i < tokenNames.length; i++) { - tokenNames[i] = VOCABULARY.getLiteralName(i); - if (tokenNames[i] == null) { - tokenNames[i] = VOCABULARY.getSymbolicName(i); - } + /** + * @deprecated Use {@link #VOCABULARY} instead. + */ + @Deprecated + public static final String[] tokenNames; + static { + tokenNames = new String[_SYMBOLIC_NAMES.length]; + for (int i = 0; i < tokenNames.length; i++) { + tokenNames[i] = VOCABULARY.getLiteralName(i); + if (tokenNames[i] == null) { + tokenNames[i] = VOCABULARY.getSymbolicName(i); + } - if (tokenNames[i] == null) { - tokenNames[i] = ""; - } + if (tokenNames[i] == null) { + tokenNames[i] = ""; + } + } } - } - - @Override - @Deprecated - public String[] getTokenNames() { - return tokenNames; - } - @Override - - public Vocabulary getVocabulary() { - return VOCABULARY; - } + @Override + @Deprecated + public String[] getTokenNames() { + return tokenNames; + } + @Override - /** Is the preceding {@code /} a the beginning of a regex (true) or a division (false). */ - protected abstract boolean isSlashRegex(); + public Vocabulary getVocabulary() { + return VOCABULARY; + } + /** Is the preceding {@code /} a the beginning of a regex (true) or a division (false). */ + protected abstract boolean isSlashRegex(); - public PainlessLexer(CharStream input) { - super(input); - _interp = new LexerATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache); - } + public PainlessLexer(CharStream input) { + super(input); + _interp = new LexerATNSimulator(this, _ATN, _decisionToDFA, _sharedContextCache); + } - @Override - public String getGrammarFileName() { return "PainlessLexer.g4"; } + @Override + public String getGrammarFileName() { + return "PainlessLexer.g4"; + } - @Override - public String[] getRuleNames() { return ruleNames; } + @Override + public String[] getRuleNames() { + return ruleNames; + } - @Override - public String getSerializedATN() { return _serializedATN; } + @Override + public String getSerializedATN() { + return _serializedATN; + } - @Override - public String[] getModeNames() { return modeNames; } + @Override + public String[] getModeNames() { + return modeNames; + } - @Override - public ATN getATN() { return _ATN; } + @Override + public ATN getATN() { + return _ATN; + } - @Override - public boolean sempred(RuleContext _localctx, int ruleIndex, int predIndex) { - switch (ruleIndex) { - case 30: - return DIV_sempred((RuleContext)_localctx, predIndex); - case 76: - return REGEX_sempred((RuleContext)_localctx, predIndex); + @Override + public boolean sempred(RuleContext _localctx, int ruleIndex, int predIndex) { + switch (ruleIndex) { + case 30: + return DIV_sempred((RuleContext) _localctx, predIndex); + case 76: + return REGEX_sempred((RuleContext) _localctx, predIndex); + } + return true; } - return true; - } - private boolean DIV_sempred(RuleContext _localctx, int predIndex) { - switch (predIndex) { - case 0: - return isSlashRegex() == false ; + + private boolean DIV_sempred(RuleContext _localctx, int predIndex) { + switch (predIndex) { + case 0: + return isSlashRegex() == false; + } + return true; } - return true; - } - private boolean REGEX_sempred(RuleContext _localctx, int predIndex) { - switch (predIndex) { - case 1: - return isSlashRegex() ; + + private boolean REGEX_sempred(RuleContext _localctx, int predIndex) { + switch (predIndex) { + case 1: + return isSlashRegex(); + } + return true; } - return true; - } - public static final String _serializedATN = - "\3\u0430\ud6d1\u8206\uad2d\u4417\uaef1\u8d80\uaadd\2W\u027a\b\1\b\1\4"+ - "\2\t\2\4\3\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b\t\b\4\t\t\t\4\n\t\n"+ - "\4\13\t\13\4\f\t\f\4\r\t\r\4\16\t\16\4\17\t\17\4\20\t\20\4\21\t\21\4\22"+ - "\t\22\4\23\t\23\4\24\t\24\4\25\t\25\4\26\t\26\4\27\t\27\4\30\t\30\4\31"+ - "\t\31\4\32\t\32\4\33\t\33\4\34\t\34\4\35\t\35\4\36\t\36\4\37\t\37\4 \t"+ - " \4!\t!\4\"\t\"\4#\t#\4$\t$\4%\t%\4&\t&\4\'\t\'\4(\t(\4)\t)\4*\t*\4+\t"+ - "+\4,\t,\4-\t-\4.\t.\4/\t/\4\60\t\60\4\61\t\61\4\62\t\62\4\63\t\63\4\64"+ - "\t\64\4\65\t\65\4\66\t\66\4\67\t\67\48\t8\49\t9\4:\t:\4;\t;\4<\t<\4=\t"+ - "=\4>\t>\4?\t?\4@\t@\4A\tA\4B\tB\4C\tC\4D\tD\4E\tE\4F\tF\4G\tG\4H\tH\4"+ - "I\tI\4J\tJ\4K\tK\4L\tL\4M\tM\4N\tN\4O\tO\4P\tP\4Q\tQ\4R\tR\4S\tS\4T\t"+ - "T\4U\tU\4V\tV\3\2\6\2\u00b0\n\2\r\2\16\2\u00b1\3\2\3\2\3\3\3\3\3\3\3\3"+ - "\7\3\u00ba\n\3\f\3\16\3\u00bd\13\3\3\3\3\3\3\3\3\3\3\3\7\3\u00c4\n\3\f"+ - "\3\16\3\u00c7\13\3\3\3\3\3\5\3\u00cb\n\3\3\3\3\3\3\4\3\4\3\5\3\5\3\6\3"+ - "\6\3\7\3\7\3\b\3\b\3\t\3\t\3\n\3\n\3\n\3\n\3\13\3\13\3\13\3\13\3\13\3"+ - "\f\3\f\3\r\3\r\3\16\3\16\3\16\3\17\3\17\3\17\3\20\3\20\3\20\3\20\3\20"+ - "\3\21\3\21\3\21\3\21\3\21\3\21\3\22\3\22\3\22\3\23\3\23\3\23\3\23\3\24"+ - "\3\24\3\24\3\24\3\24\3\24\3\24\3\24\3\24\3\25\3\25\3\25\3\25\3\25\3\25"+ - "\3\26\3\26\3\26\3\26\3\26\3\26\3\26\3\27\3\27\3\27\3\27\3\30\3\30\3\30"+ - "\3\30\3\31\3\31\3\31\3\31\3\31\3\31\3\32\3\32\3\32\3\32\3\32\3\32\3\33"+ - "\3\33\3\33\3\33\3\33\3\34\3\34\3\34\3\34\3\34\3\34\3\34\3\34\3\34\3\34"+ - "\3\34\3\35\3\35\3\36\3\36\3\37\3\37\3 \3 \3 \3!\3!\3\"\3\"\3#\3#\3$\3"+ - "$\3$\3%\3%\3%\3&\3&\3&\3&\3\'\3\'\3(\3(\3(\3)\3)\3*\3*\3*\3+\3+\3+\3,"+ - "\3,\3,\3,\3-\3-\3-\3.\3.\3.\3.\3/\3/\3\60\3\60\3\61\3\61\3\62\3\62\3\62"+ - "\3\63\3\63\3\63\3\64\3\64\3\65\3\65\3\66\3\66\3\66\3\67\3\67\3\67\38\3"+ - "8\38\39\39\39\3:\3:\3:\3:\3;\3;\3;\3<\3<\3<\3=\3=\3>\3>\3>\3?\3?\3?\3"+ - "@\3@\3@\3A\3A\3A\3B\3B\3B\3C\3C\3C\3D\3D\3D\3E\3E\3E\3F\3F\3F\3F\3G\3"+ - "G\3G\3G\3H\3H\3H\3H\3H\3I\3I\6I\u01ba\nI\rI\16I\u01bb\3I\5I\u01bf\nI\3"+ - "J\3J\3J\6J\u01c4\nJ\rJ\16J\u01c5\3J\5J\u01c9\nJ\3K\3K\3K\7K\u01ce\nK\f"+ - "K\16K\u01d1\13K\5K\u01d3\nK\3K\5K\u01d6\nK\3L\3L\3L\7L\u01db\nL\fL\16"+ - "L\u01de\13L\5L\u01e0\nL\3L\3L\6L\u01e4\nL\rL\16L\u01e5\5L\u01e8\nL\3L"+ - "\3L\5L\u01ec\nL\3L\6L\u01ef\nL\rL\16L\u01f0\5L\u01f3\nL\3L\5L\u01f6\n"+ - "L\3M\3M\3M\3M\3M\3M\7M\u01fe\nM\fM\16M\u0201\13M\3M\3M\3M\3M\3M\3M\3M"+ - "\7M\u020a\nM\fM\16M\u020d\13M\3M\5M\u0210\nM\3N\3N\3N\3N\6N\u0216\nN\r"+ - "N\16N\u0217\3N\3N\7N\u021c\nN\fN\16N\u021f\13N\3N\3N\3O\3O\3O\3O\3O\3"+ - "P\3P\3P\3P\3P\3P\3Q\3Q\3Q\3Q\3Q\3R\3R\3R\3R\3R\3R\3R\3R\3R\3R\3R\3R\3"+ - "R\3R\3R\3R\3R\3R\3R\3R\3R\3R\3R\3R\3R\3R\3R\3R\3R\3R\3R\3R\3R\3R\3R\3"+ - "R\3R\3R\5R\u0259\nR\3S\3S\3S\3S\3T\3T\7T\u0261\nT\fT\16T\u0264\13T\3U"+ - "\3U\3U\7U\u0269\nU\fU\16U\u026c\13U\5U\u026e\nU\3U\3U\3V\3V\7V\u0274\n"+ - "V\fV\16V\u0277\13V\3V\3V\7\u00bb\u00c5\u01ff\u020b\u0217\2W\4\3\6\4\b"+ - "\5\n\6\f\7\16\b\20\t\22\n\24\13\26\f\30\r\32\16\34\17\36\20 \21\"\22$"+ - "\23&\24(\25*\26,\27.\30\60\31\62\32\64\33\66\348\35:\36<\37> @!B\"D#F"+ - "$H%J&L\'N(P)R*T+V,X-Z.\\/^\60`\61b\62d\63f\64h\65j\66l\67n8p9r:t;v|?~@\u0080A\u0082B\u0084C\u0086D\u0088E\u008aF\u008cG\u008eH\u0090I"+ - "\u0092J\u0094K\u0096L\u0098M\u009aN\u009cO\u009eP\u00a0Q\u00a2R\u00a4"+ - "S\u00a6T\u00a8U\u00aaV\u00acW\4\2\3\25\5\2\13\f\17\17\"\"\4\2\f\f\17\17"+ - "\3\2\629\4\2NNnn\4\2ZZzz\5\2\62;CHch\3\2\63;\3\2\62;\b\2FFHHNNffhhnn\4"+ - "\2GGgg\4\2--//\6\2FFHHffhh\4\2$$^^\4\2))^^\3\2\f\f\4\2\f\f\61\61\t\2W"+ - "Weekknouuwwzz\5\2C\\aac|\6\2\62;C\\aac|\u02a0\2\4\3\2\2\2\2\6\3\2\2\2"+ - "\2\b\3\2\2\2\2\n\3\2\2\2\2\f\3\2\2\2\2\16\3\2\2\2\2\20\3\2\2\2\2\22\3"+ - "\2\2\2\2\24\3\2\2\2\2\26\3\2\2\2\2\30\3\2\2\2\2\32\3\2\2\2\2\34\3\2\2"+ - "\2\2\36\3\2\2\2\2 \3\2\2\2\2\"\3\2\2\2\2$\3\2\2\2\2&\3\2\2\2\2(\3\2\2"+ - "\2\2*\3\2\2\2\2,\3\2\2\2\2.\3\2\2\2\2\60\3\2\2\2\2\62\3\2\2\2\2\64\3\2"+ - "\2\2\2\66\3\2\2\2\28\3\2\2\2\2:\3\2\2\2\2<\3\2\2\2\2>\3\2\2\2\2@\3\2\2"+ - "\2\2B\3\2\2\2\2D\3\2\2\2\2F\3\2\2\2\2H\3\2\2\2\2J\3\2\2\2\2L\3\2\2\2\2"+ - "N\3\2\2\2\2P\3\2\2\2\2R\3\2\2\2\2T\3\2\2\2\2V\3\2\2\2\2X\3\2\2\2\2Z\3"+ - "\2\2\2\2\\\3\2\2\2\2^\3\2\2\2\2`\3\2\2\2\2b\3\2\2\2\2d\3\2\2\2\2f\3\2"+ - "\2\2\2h\3\2\2\2\2j\3\2\2\2\2l\3\2\2\2\2n\3\2\2\2\2p\3\2\2\2\2r\3\2\2\2"+ - "\2t\3\2\2\2\2v\3\2\2\2\2x\3\2\2\2\2z\3\2\2\2\2|\3\2\2\2\2~\3\2\2\2\2\u0080"+ - "\3\2\2\2\2\u0082\3\2\2\2\2\u0084\3\2\2\2\2\u0086\3\2\2\2\2\u0088\3\2\2"+ - "\2\2\u008a\3\2\2\2\2\u008c\3\2\2\2\2\u008e\3\2\2\2\2\u0090\3\2\2\2\2\u0092"+ - "\3\2\2\2\2\u0094\3\2\2\2\2\u0096\3\2\2\2\2\u0098\3\2\2\2\2\u009a\3\2\2"+ - "\2\2\u009c\3\2\2\2\2\u009e\3\2\2\2\2\u00a0\3\2\2\2\2\u00a2\3\2\2\2\2\u00a4"+ - "\3\2\2\2\2\u00a6\3\2\2\2\2\u00a8\3\2\2\2\3\u00aa\3\2\2\2\3\u00ac\3\2\2"+ - "\2\4\u00af\3\2\2\2\6\u00ca\3\2\2\2\b\u00ce\3\2\2\2\n\u00d0\3\2\2\2\f\u00d2"+ - "\3\2\2\2\16\u00d4\3\2\2\2\20\u00d6\3\2\2\2\22\u00d8\3\2\2\2\24\u00da\3"+ - "\2\2\2\26\u00de\3\2\2\2\30\u00e3\3\2\2\2\32\u00e5\3\2\2\2\34\u00e7\3\2"+ - "\2\2\36\u00ea\3\2\2\2 \u00ed\3\2\2\2\"\u00f2\3\2\2\2$\u00f8\3\2\2\2&\u00fb"+ - "\3\2\2\2(\u00ff\3\2\2\2*\u0108\3\2\2\2,\u010e\3\2\2\2.\u0115\3\2\2\2\60"+ - "\u0119\3\2\2\2\62\u011d\3\2\2\2\64\u0123\3\2\2\2\66\u0129\3\2\2\28\u012e"+ - "\3\2\2\2:\u0139\3\2\2\2<\u013b\3\2\2\2>\u013d\3\2\2\2@\u013f\3\2\2\2B"+ - "\u0142\3\2\2\2D\u0144\3\2\2\2F\u0146\3\2\2\2H\u0148\3\2\2\2J\u014b\3\2"+ - "\2\2L\u014e\3\2\2\2N\u0152\3\2\2\2P\u0154\3\2\2\2R\u0157\3\2\2\2T\u0159"+ - "\3\2\2\2V\u015c\3\2\2\2X\u015f\3\2\2\2Z\u0163\3\2\2\2\\\u0166\3\2\2\2"+ - "^\u016a\3\2\2\2`\u016c\3\2\2\2b\u016e\3\2\2\2d\u0170\3\2\2\2f\u0173\3"+ - "\2\2\2h\u0176\3\2\2\2j\u0178\3\2\2\2l\u017a\3\2\2\2n\u017d\3\2\2\2p\u0180"+ - "\3\2\2\2r\u0183\3\2\2\2t\u0186\3\2\2\2v\u018a\3\2\2\2x\u018d\3\2\2\2z"+ - "\u0190\3\2\2\2|\u0192\3\2\2\2~\u0195\3\2\2\2\u0080\u0198\3\2\2\2\u0082"+ - "\u019b\3\2\2\2\u0084\u019e\3\2\2\2\u0086\u01a1\3\2\2\2\u0088\u01a4\3\2"+ - "\2\2\u008a\u01a7\3\2\2\2\u008c\u01aa\3\2\2\2\u008e\u01ae\3\2\2\2\u0090"+ - "\u01b2\3\2\2\2\u0092\u01b7\3\2\2\2\u0094\u01c0\3\2\2\2\u0096\u01d2\3\2"+ - "\2\2\u0098\u01df\3\2\2\2\u009a\u020f\3\2\2\2\u009c\u0211\3\2\2\2\u009e"+ - "\u0222\3\2\2\2\u00a0\u0227\3\2\2\2\u00a2\u022d\3\2\2\2\u00a4\u0258\3\2"+ - "\2\2\u00a6\u025a\3\2\2\2\u00a8\u025e\3\2\2\2\u00aa\u026d\3\2\2\2\u00ac"+ - "\u0271\3\2\2\2\u00ae\u00b0\t\2\2\2\u00af\u00ae\3\2\2\2\u00b0\u00b1\3\2"+ - "\2\2\u00b1\u00af\3\2\2\2\u00b1\u00b2\3\2\2\2\u00b2\u00b3\3\2\2\2\u00b3"+ - "\u00b4\b\2\2\2\u00b4\5\3\2\2\2\u00b5\u00b6\7\61\2\2\u00b6\u00b7\7\61\2"+ - "\2\u00b7\u00bb\3\2\2\2\u00b8\u00ba\13\2\2\2\u00b9\u00b8\3\2\2\2\u00ba"+ - "\u00bd\3\2\2\2\u00bb\u00bc\3\2\2\2\u00bb\u00b9\3\2\2\2\u00bc\u00be\3\2"+ - "\2\2\u00bd\u00bb\3\2\2\2\u00be\u00cb\t\3\2\2\u00bf\u00c0\7\61\2\2\u00c0"+ - "\u00c1\7,\2\2\u00c1\u00c5\3\2\2\2\u00c2\u00c4\13\2\2\2\u00c3\u00c2\3\2"+ - "\2\2\u00c4\u00c7\3\2\2\2\u00c5\u00c6\3\2\2\2\u00c5\u00c3\3\2\2\2\u00c6"+ - "\u00c8\3\2\2\2\u00c7\u00c5\3\2\2\2\u00c8\u00c9\7,\2\2\u00c9\u00cb\7\61"+ - "\2\2\u00ca\u00b5\3\2\2\2\u00ca\u00bf\3\2\2\2\u00cb\u00cc\3\2\2\2\u00cc"+ - "\u00cd\b\3\2\2\u00cd\7\3\2\2\2\u00ce\u00cf\7}\2\2\u00cf\t\3\2\2\2\u00d0"+ - "\u00d1\7\177\2\2\u00d1\13\3\2\2\2\u00d2\u00d3\7]\2\2\u00d3\r\3\2\2\2\u00d4"+ - "\u00d5\7_\2\2\u00d5\17\3\2\2\2\u00d6\u00d7\7*\2\2\u00d7\21\3\2\2\2\u00d8"+ - "\u00d9\7+\2\2\u00d9\23\3\2\2\2\u00da\u00db\7\60\2\2\u00db\u00dc\3\2\2"+ - "\2\u00dc\u00dd\b\n\3\2\u00dd\25\3\2\2\2\u00de\u00df\7A\2\2\u00df\u00e0"+ - "\7\60\2\2\u00e0\u00e1\3\2\2\2\u00e1\u00e2\b\13\3\2\u00e2\27\3\2\2\2\u00e3"+ - "\u00e4\7.\2\2\u00e4\31\3\2\2\2\u00e5\u00e6\7=\2\2\u00e6\33\3\2\2\2\u00e7"+ - "\u00e8\7k\2\2\u00e8\u00e9\7h\2\2\u00e9\35\3\2\2\2\u00ea\u00eb\7k\2\2\u00eb"+ - "\u00ec\7p\2\2\u00ec\37\3\2\2\2\u00ed\u00ee\7g\2\2\u00ee\u00ef\7n\2\2\u00ef"+ - "\u00f0\7u\2\2\u00f0\u00f1\7g\2\2\u00f1!\3\2\2\2\u00f2\u00f3\7y\2\2\u00f3"+ - "\u00f4\7j\2\2\u00f4\u00f5\7k\2\2\u00f5\u00f6\7n\2\2\u00f6\u00f7\7g\2\2"+ - "\u00f7#\3\2\2\2\u00f8\u00f9\7f\2\2\u00f9\u00fa\7q\2\2\u00fa%\3\2\2\2\u00fb"+ - "\u00fc\7h\2\2\u00fc\u00fd\7q\2\2\u00fd\u00fe\7t\2\2\u00fe\'\3\2\2\2\u00ff"+ - "\u0100\7e\2\2\u0100\u0101\7q\2\2\u0101\u0102\7p\2\2\u0102\u0103\7v\2\2"+ - "\u0103\u0104\7k\2\2\u0104\u0105\7p\2\2\u0105\u0106\7w\2\2\u0106\u0107"+ - "\7g\2\2\u0107)\3\2\2\2\u0108\u0109\7d\2\2\u0109\u010a\7t\2\2\u010a\u010b"+ - "\7g\2\2\u010b\u010c\7c\2\2\u010c\u010d\7m\2\2\u010d+\3\2\2\2\u010e\u010f"+ - "\7t\2\2\u010f\u0110\7g\2\2\u0110\u0111\7v\2\2\u0111\u0112\7w\2\2\u0112"+ - "\u0113\7t\2\2\u0113\u0114\7p\2\2\u0114-\3\2\2\2\u0115\u0116\7p\2\2\u0116"+ - "\u0117\7g\2\2\u0117\u0118\7y\2\2\u0118/\3\2\2\2\u0119\u011a\7v\2\2\u011a"+ - "\u011b\7t\2\2\u011b\u011c\7{\2\2\u011c\61\3\2\2\2\u011d\u011e\7e\2\2\u011e"+ - "\u011f\7c\2\2\u011f\u0120\7v\2\2\u0120\u0121\7e\2\2\u0121\u0122\7j\2\2"+ - "\u0122\63\3\2\2\2\u0123\u0124\7v\2\2\u0124\u0125\7j\2\2\u0125\u0126\7"+ - "t\2\2\u0126\u0127\7q\2\2\u0127\u0128\7y\2\2\u0128\65\3\2\2\2\u0129\u012a"+ - "\7v\2\2\u012a\u012b\7j\2\2\u012b\u012c\7k\2\2\u012c\u012d\7u\2\2\u012d"+ - "\67\3\2\2\2\u012e\u012f\7k\2\2\u012f\u0130\7p\2\2\u0130\u0131\7u\2\2\u0131"+ - "\u0132\7v\2\2\u0132\u0133\7c\2\2\u0133\u0134\7p\2\2\u0134\u0135\7e\2\2"+ - "\u0135\u0136\7g\2\2\u0136\u0137\7q\2\2\u0137\u0138\7h\2\2\u01389\3\2\2"+ - "\2\u0139\u013a\7#\2\2\u013a;\3\2\2\2\u013b\u013c\7\u0080\2\2\u013c=\3"+ - "\2\2\2\u013d\u013e\7,\2\2\u013e?\3\2\2\2\u013f\u0140\7\61\2\2\u0140\u0141"+ - "\6 \2\2\u0141A\3\2\2\2\u0142\u0143\7\'\2\2\u0143C\3\2\2\2\u0144\u0145"+ - "\7-\2\2\u0145E\3\2\2\2\u0146\u0147\7/\2\2\u0147G\3\2\2\2\u0148\u0149\7"+ - ">\2\2\u0149\u014a\7>\2\2\u014aI\3\2\2\2\u014b\u014c\7@\2\2\u014c\u014d"+ - "\7@\2\2\u014dK\3\2\2\2\u014e\u014f\7@\2\2\u014f\u0150\7@\2\2\u0150\u0151"+ - "\7@\2\2\u0151M\3\2\2\2\u0152\u0153\7>\2\2\u0153O\3\2\2\2\u0154\u0155\7"+ - ">\2\2\u0155\u0156\7?\2\2\u0156Q\3\2\2\2\u0157\u0158\7@\2\2\u0158S\3\2"+ - "\2\2\u0159\u015a\7@\2\2\u015a\u015b\7?\2\2\u015bU\3\2\2\2\u015c\u015d"+ - "\7?\2\2\u015d\u015e\7?\2\2\u015eW\3\2\2\2\u015f\u0160\7?\2\2\u0160\u0161"+ - "\7?\2\2\u0161\u0162\7?\2\2\u0162Y\3\2\2\2\u0163\u0164\7#\2\2\u0164\u0165"+ - "\7?\2\2\u0165[\3\2\2\2\u0166\u0167\7#\2\2\u0167\u0168\7?\2\2\u0168\u0169"+ - "\7?\2\2\u0169]\3\2\2\2\u016a\u016b\7(\2\2\u016b_\3\2\2\2\u016c\u016d\7"+ - "`\2\2\u016da\3\2\2\2\u016e\u016f\7~\2\2\u016fc\3\2\2\2\u0170\u0171\7("+ - "\2\2\u0171\u0172\7(\2\2\u0172e\3\2\2\2\u0173\u0174\7~\2\2\u0174\u0175"+ - "\7~\2\2\u0175g\3\2\2\2\u0176\u0177\7A\2\2\u0177i\3\2\2\2\u0178\u0179\7"+ - "<\2\2\u0179k\3\2\2\2\u017a\u017b\7A\2\2\u017b\u017c\7<\2\2\u017cm\3\2"+ - "\2\2\u017d\u017e\7<\2\2\u017e\u017f\7<\2\2\u017fo\3\2\2\2\u0180\u0181"+ - "\7/\2\2\u0181\u0182\7@\2\2\u0182q\3\2\2\2\u0183\u0184\7?\2\2\u0184\u0185"+ - "\7\u0080\2\2\u0185s\3\2\2\2\u0186\u0187\7?\2\2\u0187\u0188\7?\2\2\u0188"+ - "\u0189\7\u0080\2\2\u0189u\3\2\2\2\u018a\u018b\7-\2\2\u018b\u018c\7-\2"+ - "\2\u018cw\3\2\2\2\u018d\u018e\7/\2\2\u018e\u018f\7/\2\2\u018fy\3\2\2\2"+ - "\u0190\u0191\7?\2\2\u0191{\3\2\2\2\u0192\u0193\7-\2\2\u0193\u0194\7?\2"+ - "\2\u0194}\3\2\2\2\u0195\u0196\7/\2\2\u0196\u0197\7?\2\2\u0197\177\3\2"+ - "\2\2\u0198\u0199\7,\2\2\u0199\u019a\7?\2\2\u019a\u0081\3\2\2\2\u019b\u019c"+ - "\7\61\2\2\u019c\u019d\7?\2\2\u019d\u0083\3\2\2\2\u019e\u019f\7\'\2\2\u019f"+ - "\u01a0\7?\2\2\u01a0\u0085\3\2\2\2\u01a1\u01a2\7(\2\2\u01a2\u01a3\7?\2"+ - "\2\u01a3\u0087\3\2\2\2\u01a4\u01a5\7`\2\2\u01a5\u01a6\7?\2\2\u01a6\u0089"+ - "\3\2\2\2\u01a7\u01a8\7~\2\2\u01a8\u01a9\7?\2\2\u01a9\u008b\3\2\2\2\u01aa"+ - "\u01ab\7>\2\2\u01ab\u01ac\7>\2\2\u01ac\u01ad\7?\2\2\u01ad\u008d\3\2\2"+ - "\2\u01ae\u01af\7@\2\2\u01af\u01b0\7@\2\2\u01b0\u01b1\7?\2\2\u01b1\u008f"+ - "\3\2\2\2\u01b2\u01b3\7@\2\2\u01b3\u01b4\7@\2\2\u01b4\u01b5\7@\2\2\u01b5"+ - "\u01b6\7?\2\2\u01b6\u0091\3\2\2\2\u01b7\u01b9\7\62\2\2\u01b8\u01ba\t\4"+ - "\2\2\u01b9\u01b8\3\2\2\2\u01ba\u01bb\3\2\2\2\u01bb\u01b9\3\2\2\2\u01bb"+ - "\u01bc\3\2\2\2\u01bc\u01be\3\2\2\2\u01bd\u01bf\t\5\2\2\u01be\u01bd\3\2"+ - "\2\2\u01be\u01bf\3\2\2\2\u01bf\u0093\3\2\2\2\u01c0\u01c1\7\62\2\2\u01c1"+ - "\u01c3\t\6\2\2\u01c2\u01c4\t\7\2\2\u01c3\u01c2\3\2\2\2\u01c4\u01c5\3\2"+ - "\2\2\u01c5\u01c3\3\2\2\2\u01c5\u01c6\3\2\2\2\u01c6\u01c8\3\2\2\2\u01c7"+ - "\u01c9\t\5\2\2\u01c8\u01c7\3\2\2\2\u01c8\u01c9\3\2\2\2\u01c9\u0095\3\2"+ - "\2\2\u01ca\u01d3\7\62\2\2\u01cb\u01cf\t\b\2\2\u01cc\u01ce\t\t\2\2\u01cd"+ - "\u01cc\3\2\2\2\u01ce\u01d1\3\2\2\2\u01cf\u01cd\3\2\2\2\u01cf\u01d0\3\2"+ - "\2\2\u01d0\u01d3\3\2\2\2\u01d1\u01cf\3\2\2\2\u01d2\u01ca\3\2\2\2\u01d2"+ - "\u01cb\3\2\2\2\u01d3\u01d5\3\2\2\2\u01d4\u01d6\t\n\2\2\u01d5\u01d4\3\2"+ - "\2\2\u01d5\u01d6\3\2\2\2\u01d6\u0097\3\2\2\2\u01d7\u01e0\7\62\2\2\u01d8"+ - "\u01dc\t\b\2\2\u01d9\u01db\t\t\2\2\u01da\u01d9\3\2\2\2\u01db\u01de\3\2"+ - "\2\2\u01dc\u01da\3\2\2\2\u01dc\u01dd\3\2\2\2\u01dd\u01e0\3\2\2\2\u01de"+ - "\u01dc\3\2\2\2\u01df\u01d7\3\2\2\2\u01df\u01d8\3\2\2\2\u01e0\u01e7\3\2"+ - "\2\2\u01e1\u01e3\5\24\n\2\u01e2\u01e4\t\t\2\2\u01e3\u01e2\3\2\2\2\u01e4"+ - "\u01e5\3\2\2\2\u01e5\u01e3\3\2\2\2\u01e5\u01e6\3\2\2\2\u01e6\u01e8\3\2"+ - "\2\2\u01e7\u01e1\3\2\2\2\u01e7\u01e8\3\2\2\2\u01e8\u01f2\3\2\2\2\u01e9"+ - "\u01eb\t\13\2\2\u01ea\u01ec\t\f\2\2\u01eb\u01ea\3\2\2\2\u01eb\u01ec\3"+ - "\2\2\2\u01ec\u01ee\3\2\2\2\u01ed\u01ef\t\t\2\2\u01ee\u01ed\3\2\2\2\u01ef"+ - "\u01f0\3\2\2\2\u01f0\u01ee\3\2\2\2\u01f0\u01f1\3\2\2\2\u01f1\u01f3\3\2"+ - "\2\2\u01f2\u01e9\3\2\2\2\u01f2\u01f3\3\2\2\2\u01f3\u01f5\3\2\2\2\u01f4"+ - "\u01f6\t\r\2\2\u01f5\u01f4\3\2\2\2\u01f5\u01f6\3\2\2\2\u01f6\u0099\3\2"+ - "\2\2\u01f7\u01ff\7$\2\2\u01f8\u01f9\7^\2\2\u01f9\u01fe\7$\2\2\u01fa\u01fb"+ - "\7^\2\2\u01fb\u01fe\7^\2\2\u01fc\u01fe\n\16\2\2\u01fd\u01f8\3\2\2\2\u01fd"+ - "\u01fa\3\2\2\2\u01fd\u01fc\3\2\2\2\u01fe\u0201\3\2\2\2\u01ff\u0200\3\2"+ - "\2\2\u01ff\u01fd\3\2\2\2\u0200\u0202\3\2\2\2\u0201\u01ff\3\2\2\2\u0202"+ - "\u0210\7$\2\2\u0203\u020b\7)\2\2\u0204\u0205\7^\2\2\u0205\u020a\7)\2\2"+ - "\u0206\u0207\7^\2\2\u0207\u020a\7^\2\2\u0208\u020a\n\17\2\2\u0209\u0204"+ - "\3\2\2\2\u0209\u0206\3\2\2\2\u0209\u0208\3\2\2\2\u020a\u020d\3\2\2\2\u020b"+ - "\u020c\3\2\2\2\u020b\u0209\3\2\2\2\u020c\u020e\3\2\2\2\u020d\u020b\3\2"+ - "\2\2\u020e\u0210\7)\2\2\u020f\u01f7\3\2\2\2\u020f\u0203\3\2\2\2\u0210"+ - "\u009b\3\2\2\2\u0211\u0215\7\61\2\2\u0212\u0213\7^\2\2\u0213\u0216\n\20"+ - "\2\2\u0214\u0216\n\21\2\2\u0215\u0212\3\2\2\2\u0215\u0214\3\2\2\2\u0216"+ - "\u0217\3\2\2\2\u0217\u0218\3\2\2\2\u0217\u0215\3\2\2\2\u0218\u0219\3\2"+ - "\2\2\u0219\u021d\7\61\2\2\u021a\u021c\t\22\2\2\u021b\u021a\3\2\2\2\u021c"+ - "\u021f\3\2\2\2\u021d\u021b\3\2\2\2\u021d\u021e\3\2\2\2\u021e\u0220\3\2"+ - "\2\2\u021f\u021d\3\2\2\2\u0220\u0221\6N\3\2\u0221\u009d\3\2\2\2\u0222"+ - "\u0223\7v\2\2\u0223\u0224\7t\2\2\u0224\u0225\7w\2\2\u0225\u0226\7g\2\2"+ - "\u0226\u009f\3\2\2\2\u0227\u0228\7h\2\2\u0228\u0229\7c\2\2\u0229\u022a"+ - "\7n\2\2\u022a\u022b\7u\2\2\u022b\u022c\7g\2\2\u022c\u00a1\3\2\2\2\u022d"+ - "\u022e\7p\2\2\u022e\u022f\7w\2\2\u022f\u0230\7n\2\2\u0230\u0231\7n\2\2"+ - "\u0231\u00a3\3\2\2\2\u0232\u0233\7d\2\2\u0233\u0234\7q\2\2\u0234\u0235"+ - "\7q\2\2\u0235\u0236\7n\2\2\u0236\u0237\7g\2\2\u0237\u0238\7c\2\2\u0238"+ - "\u0259\7p\2\2\u0239\u023a\7d\2\2\u023a\u023b\7{\2\2\u023b\u023c\7v\2\2"+ - "\u023c\u0259\7g\2\2\u023d\u023e\7u\2\2\u023e\u023f\7j\2\2\u023f\u0240"+ - "\7q\2\2\u0240\u0241\7t\2\2\u0241\u0259\7v\2\2\u0242\u0243\7e\2\2\u0243"+ - "\u0244\7j\2\2\u0244\u0245\7c\2\2\u0245\u0259\7t\2\2\u0246\u0247\7k\2\2"+ - "\u0247\u0248\7p\2\2\u0248\u0259\7v\2\2\u0249\u024a\7n\2\2\u024a\u024b"+ - "\7q\2\2\u024b\u024c\7p\2\2\u024c\u0259\7i\2\2\u024d\u024e\7h\2\2\u024e"+ - "\u024f\7n\2\2\u024f\u0250\7q\2\2\u0250\u0251\7c\2\2\u0251\u0259\7v\2\2"+ - "\u0252\u0253\7f\2\2\u0253\u0254\7q\2\2\u0254\u0255\7w\2\2\u0255\u0256"+ - "\7d\2\2\u0256\u0257\7n\2\2\u0257\u0259\7g\2\2\u0258\u0232\3\2\2\2\u0258"+ - "\u0239\3\2\2\2\u0258\u023d\3\2\2\2\u0258\u0242\3\2\2\2\u0258\u0246\3\2"+ - "\2\2\u0258\u0249\3\2\2\2\u0258\u024d\3\2\2\2\u0258\u0252\3\2\2\2\u0259"+ - "\u00a5\3\2\2\2\u025a\u025b\7f\2\2\u025b\u025c\7g\2\2\u025c\u025d\7h\2"+ - "\2\u025d\u00a7\3\2\2\2\u025e\u0262\t\23\2\2\u025f\u0261\t\24\2\2\u0260"+ - "\u025f\3\2\2\2\u0261\u0264\3\2\2\2\u0262\u0260\3\2\2\2\u0262\u0263\3\2"+ - "\2\2\u0263\u00a9\3\2\2\2\u0264\u0262\3\2\2\2\u0265\u026e\7\62\2\2\u0266"+ - "\u026a\t\b\2\2\u0267\u0269\t\t\2\2\u0268\u0267\3\2\2\2\u0269\u026c\3\2"+ - "\2\2\u026a\u0268\3\2\2\2\u026a\u026b\3\2\2\2\u026b\u026e\3\2\2\2\u026c"+ - "\u026a\3\2\2\2\u026d\u0265\3\2\2\2\u026d\u0266\3\2\2\2\u026e\u026f\3\2"+ - "\2\2\u026f\u0270\bU\4\2\u0270\u00ab\3\2\2\2\u0271\u0275\t\23\2\2\u0272"+ - "\u0274\t\24\2\2\u0273\u0272\3\2\2\2\u0274\u0277\3\2\2\2\u0275\u0273\3"+ - "\2\2\2\u0275\u0276\3\2\2\2\u0276\u0278\3\2\2\2\u0277\u0275\3\2\2\2\u0278"+ - "\u0279\bV\4\2\u0279\u00ad\3\2\2\2$\2\3\u00b1\u00bb\u00c5\u00ca\u01bb\u01be"+ - "\u01c5\u01c8\u01cf\u01d2\u01d5\u01dc\u01df\u01e5\u01e7\u01eb\u01f0\u01f2"+ - "\u01f5\u01fd\u01ff\u0209\u020b\u020f\u0215\u0217\u021d\u0258\u0262\u026a"+ - "\u026d\u0275\5\b\2\2\4\3\2\4\2\2"; - public static final ATN _ATN = - new ATNDeserializer().deserialize(_serializedATN.toCharArray()); - static { - _decisionToDFA = new DFA[_ATN.getNumberOfDecisions()]; - for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) { - _decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i); + public static final String _serializedATN = "\3\u0430\ud6d1\u8206\uad2d\u4417\uaef1\u8d80\uaadd\2W\u027a\b\1\b\1\4" + + "\2\t\2\4\3\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b\t\b\4\t\t\t\4\n\t\n" + + "\4\13\t\13\4\f\t\f\4\r\t\r\4\16\t\16\4\17\t\17\4\20\t\20\4\21\t\21\4\22" + + "\t\22\4\23\t\23\4\24\t\24\4\25\t\25\4\26\t\26\4\27\t\27\4\30\t\30\4\31" + + "\t\31\4\32\t\32\4\33\t\33\4\34\t\34\4\35\t\35\4\36\t\36\4\37\t\37\4 \t" + + " \4!\t!\4\"\t\"\4#\t#\4$\t$\4%\t%\4&\t&\4\'\t\'\4(\t(\4)\t)\4*\t*\4+\t" + + "+\4,\t,\4-\t-\4.\t.\4/\t/\4\60\t\60\4\61\t\61\4\62\t\62\4\63\t\63\4\64" + + "\t\64\4\65\t\65\4\66\t\66\4\67\t\67\48\t8\49\t9\4:\t:\4;\t;\4<\t<\4=\t" + + "=\4>\t>\4?\t?\4@\t@\4A\tA\4B\tB\4C\tC\4D\tD\4E\tE\4F\tF\4G\tG\4H\tH\4" + + "I\tI\4J\tJ\4K\tK\4L\tL\4M\tM\4N\tN\4O\tO\4P\tP\4Q\tQ\4R\tR\4S\tS\4T\t" + + "T\4U\tU\4V\tV\3\2\6\2\u00b0\n\2\r\2\16\2\u00b1\3\2\3\2\3\3\3\3\3\3\3\3" + + "\7\3\u00ba\n\3\f\3\16\3\u00bd\13\3\3\3\3\3\3\3\3\3\3\3\7\3\u00c4\n\3\f" + + "\3\16\3\u00c7\13\3\3\3\3\3\5\3\u00cb\n\3\3\3\3\3\3\4\3\4\3\5\3\5\3\6\3" + + "\6\3\7\3\7\3\b\3\b\3\t\3\t\3\n\3\n\3\n\3\n\3\13\3\13\3\13\3\13\3\13\3" + + "\f\3\f\3\r\3\r\3\16\3\16\3\16\3\17\3\17\3\17\3\20\3\20\3\20\3\20\3\20" + + "\3\21\3\21\3\21\3\21\3\21\3\21\3\22\3\22\3\22\3\23\3\23\3\23\3\23\3\24" + + "\3\24\3\24\3\24\3\24\3\24\3\24\3\24\3\24\3\25\3\25\3\25\3\25\3\25\3\25" + + "\3\26\3\26\3\26\3\26\3\26\3\26\3\26\3\27\3\27\3\27\3\27\3\30\3\30\3\30" + + "\3\30\3\31\3\31\3\31\3\31\3\31\3\31\3\32\3\32\3\32\3\32\3\32\3\32\3\33" + + "\3\33\3\33\3\33\3\33\3\34\3\34\3\34\3\34\3\34\3\34\3\34\3\34\3\34\3\34" + + "\3\34\3\35\3\35\3\36\3\36\3\37\3\37\3 \3 \3 \3!\3!\3\"\3\"\3#\3#\3$\3" + + "$\3$\3%\3%\3%\3&\3&\3&\3&\3\'\3\'\3(\3(\3(\3)\3)\3*\3*\3*\3+\3+\3+\3," + + "\3,\3,\3,\3-\3-\3-\3.\3.\3.\3.\3/\3/\3\60\3\60\3\61\3\61\3\62\3\62\3\62" + + "\3\63\3\63\3\63\3\64\3\64\3\65\3\65\3\66\3\66\3\66\3\67\3\67\3\67\38\3" + + "8\38\39\39\39\3:\3:\3:\3:\3;\3;\3;\3<\3<\3<\3=\3=\3>\3>\3>\3?\3?\3?\3" + + "@\3@\3@\3A\3A\3A\3B\3B\3B\3C\3C\3C\3D\3D\3D\3E\3E\3E\3F\3F\3F\3F\3G\3" + + "G\3G\3G\3H\3H\3H\3H\3H\3I\3I\6I\u01ba\nI\rI\16I\u01bb\3I\5I\u01bf\nI\3" + + "J\3J\3J\6J\u01c4\nJ\rJ\16J\u01c5\3J\5J\u01c9\nJ\3K\3K\3K\7K\u01ce\nK\f" + + "K\16K\u01d1\13K\5K\u01d3\nK\3K\5K\u01d6\nK\3L\3L\3L\7L\u01db\nL\fL\16" + + "L\u01de\13L\5L\u01e0\nL\3L\3L\6L\u01e4\nL\rL\16L\u01e5\5L\u01e8\nL\3L" + + "\3L\5L\u01ec\nL\3L\6L\u01ef\nL\rL\16L\u01f0\5L\u01f3\nL\3L\5L\u01f6\n" + + "L\3M\3M\3M\3M\3M\3M\7M\u01fe\nM\fM\16M\u0201\13M\3M\3M\3M\3M\3M\3M\3M" + + "\7M\u020a\nM\fM\16M\u020d\13M\3M\5M\u0210\nM\3N\3N\3N\3N\6N\u0216\nN\r" + + "N\16N\u0217\3N\3N\7N\u021c\nN\fN\16N\u021f\13N\3N\3N\3O\3O\3O\3O\3O\3" + + "P\3P\3P\3P\3P\3P\3Q\3Q\3Q\3Q\3Q\3R\3R\3R\3R\3R\3R\3R\3R\3R\3R\3R\3R\3" + + "R\3R\3R\3R\3R\3R\3R\3R\3R\3R\3R\3R\3R\3R\3R\3R\3R\3R\3R\3R\3R\3R\3R\3" + + "R\3R\3R\5R\u0259\nR\3S\3S\3S\3S\3T\3T\7T\u0261\nT\fT\16T\u0264\13T\3U" + + "\3U\3U\7U\u0269\nU\fU\16U\u026c\13U\5U\u026e\nU\3U\3U\3V\3V\7V\u0274\n" + + "V\fV\16V\u0277\13V\3V\3V\7\u00bb\u00c5\u01ff\u020b\u0217\2W\4\3\6\4\b" + + "\5\n\6\f\7\16\b\20\t\22\n\24\13\26\f\30\r\32\16\34\17\36\20 \21\"\22$" + + "\23&\24(\25*\26,\27.\30\60\31\62\32\64\33\66\348\35:\36<\37> @!B\"D#F" + + "$H%J&L\'N(P)R*T+V,X-Z.\\/^\60`\61b\62d\63f\64h\65j\66l\67n8p9r:t;v|?~@\u0080A\u0082B\u0084C\u0086D\u0088E\u008aF\u008cG\u008eH\u0090I" + + "\u0092J\u0094K\u0096L\u0098M\u009aN\u009cO\u009eP\u00a0Q\u00a2R\u00a4" + + "S\u00a6T\u00a8U\u00aaV\u00acW\4\2\3\25\5\2\13\f\17\17\"\"\4\2\f\f\17\17" + + "\3\2\629\4\2NNnn\4\2ZZzz\5\2\62;CHch\3\2\63;\3\2\62;\b\2FFHHNNffhhnn\4" + + "\2GGgg\4\2--//\6\2FFHHffhh\4\2$$^^\4\2))^^\3\2\f\f\4\2\f\f\61\61\t\2W" + + "Weekknouuwwzz\5\2C\\aac|\6\2\62;C\\aac|\u02a0\2\4\3\2\2\2\2\6\3\2\2\2" + + "\2\b\3\2\2\2\2\n\3\2\2\2\2\f\3\2\2\2\2\16\3\2\2\2\2\20\3\2\2\2\2\22\3" + + "\2\2\2\2\24\3\2\2\2\2\26\3\2\2\2\2\30\3\2\2\2\2\32\3\2\2\2\2\34\3\2\2" + + "\2\2\36\3\2\2\2\2 \3\2\2\2\2\"\3\2\2\2\2$\3\2\2\2\2&\3\2\2\2\2(\3\2\2" + + "\2\2*\3\2\2\2\2,\3\2\2\2\2.\3\2\2\2\2\60\3\2\2\2\2\62\3\2\2\2\2\64\3\2" + + "\2\2\2\66\3\2\2\2\28\3\2\2\2\2:\3\2\2\2\2<\3\2\2\2\2>\3\2\2\2\2@\3\2\2" + + "\2\2B\3\2\2\2\2D\3\2\2\2\2F\3\2\2\2\2H\3\2\2\2\2J\3\2\2\2\2L\3\2\2\2\2" + + "N\3\2\2\2\2P\3\2\2\2\2R\3\2\2\2\2T\3\2\2\2\2V\3\2\2\2\2X\3\2\2\2\2Z\3" + + "\2\2\2\2\\\3\2\2\2\2^\3\2\2\2\2`\3\2\2\2\2b\3\2\2\2\2d\3\2\2\2\2f\3\2" + + "\2\2\2h\3\2\2\2\2j\3\2\2\2\2l\3\2\2\2\2n\3\2\2\2\2p\3\2\2\2\2r\3\2\2\2" + + "\2t\3\2\2\2\2v\3\2\2\2\2x\3\2\2\2\2z\3\2\2\2\2|\3\2\2\2\2~\3\2\2\2\2\u0080" + + "\3\2\2\2\2\u0082\3\2\2\2\2\u0084\3\2\2\2\2\u0086\3\2\2\2\2\u0088\3\2\2" + + "\2\2\u008a\3\2\2\2\2\u008c\3\2\2\2\2\u008e\3\2\2\2\2\u0090\3\2\2\2\2\u0092" + + "\3\2\2\2\2\u0094\3\2\2\2\2\u0096\3\2\2\2\2\u0098\3\2\2\2\2\u009a\3\2\2" + + "\2\2\u009c\3\2\2\2\2\u009e\3\2\2\2\2\u00a0\3\2\2\2\2\u00a2\3\2\2\2\2\u00a4" + + "\3\2\2\2\2\u00a6\3\2\2\2\2\u00a8\3\2\2\2\3\u00aa\3\2\2\2\3\u00ac\3\2\2" + + "\2\4\u00af\3\2\2\2\6\u00ca\3\2\2\2\b\u00ce\3\2\2\2\n\u00d0\3\2\2\2\f\u00d2" + + "\3\2\2\2\16\u00d4\3\2\2\2\20\u00d6\3\2\2\2\22\u00d8\3\2\2\2\24\u00da\3" + + "\2\2\2\26\u00de\3\2\2\2\30\u00e3\3\2\2\2\32\u00e5\3\2\2\2\34\u00e7\3\2" + + "\2\2\36\u00ea\3\2\2\2 \u00ed\3\2\2\2\"\u00f2\3\2\2\2$\u00f8\3\2\2\2&\u00fb" + + "\3\2\2\2(\u00ff\3\2\2\2*\u0108\3\2\2\2,\u010e\3\2\2\2.\u0115\3\2\2\2\60" + + "\u0119\3\2\2\2\62\u011d\3\2\2\2\64\u0123\3\2\2\2\66\u0129\3\2\2\28\u012e" + + "\3\2\2\2:\u0139\3\2\2\2<\u013b\3\2\2\2>\u013d\3\2\2\2@\u013f\3\2\2\2B" + + "\u0142\3\2\2\2D\u0144\3\2\2\2F\u0146\3\2\2\2H\u0148\3\2\2\2J\u014b\3\2" + + "\2\2L\u014e\3\2\2\2N\u0152\3\2\2\2P\u0154\3\2\2\2R\u0157\3\2\2\2T\u0159" + + "\3\2\2\2V\u015c\3\2\2\2X\u015f\3\2\2\2Z\u0163\3\2\2\2\\\u0166\3\2\2\2" + + "^\u016a\3\2\2\2`\u016c\3\2\2\2b\u016e\3\2\2\2d\u0170\3\2\2\2f\u0173\3" + + "\2\2\2h\u0176\3\2\2\2j\u0178\3\2\2\2l\u017a\3\2\2\2n\u017d\3\2\2\2p\u0180" + + "\3\2\2\2r\u0183\3\2\2\2t\u0186\3\2\2\2v\u018a\3\2\2\2x\u018d\3\2\2\2z" + + "\u0190\3\2\2\2|\u0192\3\2\2\2~\u0195\3\2\2\2\u0080\u0198\3\2\2\2\u0082" + + "\u019b\3\2\2\2\u0084\u019e\3\2\2\2\u0086\u01a1\3\2\2\2\u0088\u01a4\3\2" + + "\2\2\u008a\u01a7\3\2\2\2\u008c\u01aa\3\2\2\2\u008e\u01ae\3\2\2\2\u0090" + + "\u01b2\3\2\2\2\u0092\u01b7\3\2\2\2\u0094\u01c0\3\2\2\2\u0096\u01d2\3\2" + + "\2\2\u0098\u01df\3\2\2\2\u009a\u020f\3\2\2\2\u009c\u0211\3\2\2\2\u009e" + + "\u0222\3\2\2\2\u00a0\u0227\3\2\2\2\u00a2\u022d\3\2\2\2\u00a4\u0258\3\2" + + "\2\2\u00a6\u025a\3\2\2\2\u00a8\u025e\3\2\2\2\u00aa\u026d\3\2\2\2\u00ac" + + "\u0271\3\2\2\2\u00ae\u00b0\t\2\2\2\u00af\u00ae\3\2\2\2\u00b0\u00b1\3\2" + + "\2\2\u00b1\u00af\3\2\2\2\u00b1\u00b2\3\2\2\2\u00b2\u00b3\3\2\2\2\u00b3" + + "\u00b4\b\2\2\2\u00b4\5\3\2\2\2\u00b5\u00b6\7\61\2\2\u00b6\u00b7\7\61\2" + + "\2\u00b7\u00bb\3\2\2\2\u00b8\u00ba\13\2\2\2\u00b9\u00b8\3\2\2\2\u00ba" + + "\u00bd\3\2\2\2\u00bb\u00bc\3\2\2\2\u00bb\u00b9\3\2\2\2\u00bc\u00be\3\2" + + "\2\2\u00bd\u00bb\3\2\2\2\u00be\u00cb\t\3\2\2\u00bf\u00c0\7\61\2\2\u00c0" + + "\u00c1\7,\2\2\u00c1\u00c5\3\2\2\2\u00c2\u00c4\13\2\2\2\u00c3\u00c2\3\2" + + "\2\2\u00c4\u00c7\3\2\2\2\u00c5\u00c6\3\2\2\2\u00c5\u00c3\3\2\2\2\u00c6" + + "\u00c8\3\2\2\2\u00c7\u00c5\3\2\2\2\u00c8\u00c9\7,\2\2\u00c9\u00cb\7\61" + + "\2\2\u00ca\u00b5\3\2\2\2\u00ca\u00bf\3\2\2\2\u00cb\u00cc\3\2\2\2\u00cc" + + "\u00cd\b\3\2\2\u00cd\7\3\2\2\2\u00ce\u00cf\7}\2\2\u00cf\t\3\2\2\2\u00d0" + + "\u00d1\7\177\2\2\u00d1\13\3\2\2\2\u00d2\u00d3\7]\2\2\u00d3\r\3\2\2\2\u00d4" + + "\u00d5\7_\2\2\u00d5\17\3\2\2\2\u00d6\u00d7\7*\2\2\u00d7\21\3\2\2\2\u00d8" + + "\u00d9\7+\2\2\u00d9\23\3\2\2\2\u00da\u00db\7\60\2\2\u00db\u00dc\3\2\2" + + "\2\u00dc\u00dd\b\n\3\2\u00dd\25\3\2\2\2\u00de\u00df\7A\2\2\u00df\u00e0" + + "\7\60\2\2\u00e0\u00e1\3\2\2\2\u00e1\u00e2\b\13\3\2\u00e2\27\3\2\2\2\u00e3" + + "\u00e4\7.\2\2\u00e4\31\3\2\2\2\u00e5\u00e6\7=\2\2\u00e6\33\3\2\2\2\u00e7" + + "\u00e8\7k\2\2\u00e8\u00e9\7h\2\2\u00e9\35\3\2\2\2\u00ea\u00eb\7k\2\2\u00eb" + + "\u00ec\7p\2\2\u00ec\37\3\2\2\2\u00ed\u00ee\7g\2\2\u00ee\u00ef\7n\2\2\u00ef" + + "\u00f0\7u\2\2\u00f0\u00f1\7g\2\2\u00f1!\3\2\2\2\u00f2\u00f3\7y\2\2\u00f3" + + "\u00f4\7j\2\2\u00f4\u00f5\7k\2\2\u00f5\u00f6\7n\2\2\u00f6\u00f7\7g\2\2" + + "\u00f7#\3\2\2\2\u00f8\u00f9\7f\2\2\u00f9\u00fa\7q\2\2\u00fa%\3\2\2\2\u00fb" + + "\u00fc\7h\2\2\u00fc\u00fd\7q\2\2\u00fd\u00fe\7t\2\2\u00fe\'\3\2\2\2\u00ff" + + "\u0100\7e\2\2\u0100\u0101\7q\2\2\u0101\u0102\7p\2\2\u0102\u0103\7v\2\2" + + "\u0103\u0104\7k\2\2\u0104\u0105\7p\2\2\u0105\u0106\7w\2\2\u0106\u0107" + + "\7g\2\2\u0107)\3\2\2\2\u0108\u0109\7d\2\2\u0109\u010a\7t\2\2\u010a\u010b" + + "\7g\2\2\u010b\u010c\7c\2\2\u010c\u010d\7m\2\2\u010d+\3\2\2\2\u010e\u010f" + + "\7t\2\2\u010f\u0110\7g\2\2\u0110\u0111\7v\2\2\u0111\u0112\7w\2\2\u0112" + + "\u0113\7t\2\2\u0113\u0114\7p\2\2\u0114-\3\2\2\2\u0115\u0116\7p\2\2\u0116" + + "\u0117\7g\2\2\u0117\u0118\7y\2\2\u0118/\3\2\2\2\u0119\u011a\7v\2\2\u011a" + + "\u011b\7t\2\2\u011b\u011c\7{\2\2\u011c\61\3\2\2\2\u011d\u011e\7e\2\2\u011e" + + "\u011f\7c\2\2\u011f\u0120\7v\2\2\u0120\u0121\7e\2\2\u0121\u0122\7j\2\2" + + "\u0122\63\3\2\2\2\u0123\u0124\7v\2\2\u0124\u0125\7j\2\2\u0125\u0126\7" + + "t\2\2\u0126\u0127\7q\2\2\u0127\u0128\7y\2\2\u0128\65\3\2\2\2\u0129\u012a" + + "\7v\2\2\u012a\u012b\7j\2\2\u012b\u012c\7k\2\2\u012c\u012d\7u\2\2\u012d" + + "\67\3\2\2\2\u012e\u012f\7k\2\2\u012f\u0130\7p\2\2\u0130\u0131\7u\2\2\u0131" + + "\u0132\7v\2\2\u0132\u0133\7c\2\2\u0133\u0134\7p\2\2\u0134\u0135\7e\2\2" + + "\u0135\u0136\7g\2\2\u0136\u0137\7q\2\2\u0137\u0138\7h\2\2\u01389\3\2\2" + + "\2\u0139\u013a\7#\2\2\u013a;\3\2\2\2\u013b\u013c\7\u0080\2\2\u013c=\3" + + "\2\2\2\u013d\u013e\7,\2\2\u013e?\3\2\2\2\u013f\u0140\7\61\2\2\u0140\u0141" + + "\6 \2\2\u0141A\3\2\2\2\u0142\u0143\7\'\2\2\u0143C\3\2\2\2\u0144\u0145" + + "\7-\2\2\u0145E\3\2\2\2\u0146\u0147\7/\2\2\u0147G\3\2\2\2\u0148\u0149\7" + + ">\2\2\u0149\u014a\7>\2\2\u014aI\3\2\2\2\u014b\u014c\7@\2\2\u014c\u014d" + + "\7@\2\2\u014dK\3\2\2\2\u014e\u014f\7@\2\2\u014f\u0150\7@\2\2\u0150\u0151" + + "\7@\2\2\u0151M\3\2\2\2\u0152\u0153\7>\2\2\u0153O\3\2\2\2\u0154\u0155\7" + + ">\2\2\u0155\u0156\7?\2\2\u0156Q\3\2\2\2\u0157\u0158\7@\2\2\u0158S\3\2" + + "\2\2\u0159\u015a\7@\2\2\u015a\u015b\7?\2\2\u015bU\3\2\2\2\u015c\u015d" + + "\7?\2\2\u015d\u015e\7?\2\2\u015eW\3\2\2\2\u015f\u0160\7?\2\2\u0160\u0161" + + "\7?\2\2\u0161\u0162\7?\2\2\u0162Y\3\2\2\2\u0163\u0164\7#\2\2\u0164\u0165" + + "\7?\2\2\u0165[\3\2\2\2\u0166\u0167\7#\2\2\u0167\u0168\7?\2\2\u0168\u0169" + + "\7?\2\2\u0169]\3\2\2\2\u016a\u016b\7(\2\2\u016b_\3\2\2\2\u016c\u016d\7" + + "`\2\2\u016da\3\2\2\2\u016e\u016f\7~\2\2\u016fc\3\2\2\2\u0170\u0171\7(" + + "\2\2\u0171\u0172\7(\2\2\u0172e\3\2\2\2\u0173\u0174\7~\2\2\u0174\u0175" + + "\7~\2\2\u0175g\3\2\2\2\u0176\u0177\7A\2\2\u0177i\3\2\2\2\u0178\u0179\7" + + "<\2\2\u0179k\3\2\2\2\u017a\u017b\7A\2\2\u017b\u017c\7<\2\2\u017cm\3\2" + + "\2\2\u017d\u017e\7<\2\2\u017e\u017f\7<\2\2\u017fo\3\2\2\2\u0180\u0181" + + "\7/\2\2\u0181\u0182\7@\2\2\u0182q\3\2\2\2\u0183\u0184\7?\2\2\u0184\u0185" + + "\7\u0080\2\2\u0185s\3\2\2\2\u0186\u0187\7?\2\2\u0187\u0188\7?\2\2\u0188" + + "\u0189\7\u0080\2\2\u0189u\3\2\2\2\u018a\u018b\7-\2\2\u018b\u018c\7-\2" + + "\2\u018cw\3\2\2\2\u018d\u018e\7/\2\2\u018e\u018f\7/\2\2\u018fy\3\2\2\2" + + "\u0190\u0191\7?\2\2\u0191{\3\2\2\2\u0192\u0193\7-\2\2\u0193\u0194\7?\2" + + "\2\u0194}\3\2\2\2\u0195\u0196\7/\2\2\u0196\u0197\7?\2\2\u0197\177\3\2" + + "\2\2\u0198\u0199\7,\2\2\u0199\u019a\7?\2\2\u019a\u0081\3\2\2\2\u019b\u019c" + + "\7\61\2\2\u019c\u019d\7?\2\2\u019d\u0083\3\2\2\2\u019e\u019f\7\'\2\2\u019f" + + "\u01a0\7?\2\2\u01a0\u0085\3\2\2\2\u01a1\u01a2\7(\2\2\u01a2\u01a3\7?\2" + + "\2\u01a3\u0087\3\2\2\2\u01a4\u01a5\7`\2\2\u01a5\u01a6\7?\2\2\u01a6\u0089" + + "\3\2\2\2\u01a7\u01a8\7~\2\2\u01a8\u01a9\7?\2\2\u01a9\u008b\3\2\2\2\u01aa" + + "\u01ab\7>\2\2\u01ab\u01ac\7>\2\2\u01ac\u01ad\7?\2\2\u01ad\u008d\3\2\2" + + "\2\u01ae\u01af\7@\2\2\u01af\u01b0\7@\2\2\u01b0\u01b1\7?\2\2\u01b1\u008f" + + "\3\2\2\2\u01b2\u01b3\7@\2\2\u01b3\u01b4\7@\2\2\u01b4\u01b5\7@\2\2\u01b5" + + "\u01b6\7?\2\2\u01b6\u0091\3\2\2\2\u01b7\u01b9\7\62\2\2\u01b8\u01ba\t\4" + + "\2\2\u01b9\u01b8\3\2\2\2\u01ba\u01bb\3\2\2\2\u01bb\u01b9\3\2\2\2\u01bb" + + "\u01bc\3\2\2\2\u01bc\u01be\3\2\2\2\u01bd\u01bf\t\5\2\2\u01be\u01bd\3\2" + + "\2\2\u01be\u01bf\3\2\2\2\u01bf\u0093\3\2\2\2\u01c0\u01c1\7\62\2\2\u01c1" + + "\u01c3\t\6\2\2\u01c2\u01c4\t\7\2\2\u01c3\u01c2\3\2\2\2\u01c4\u01c5\3\2" + + "\2\2\u01c5\u01c3\3\2\2\2\u01c5\u01c6\3\2\2\2\u01c6\u01c8\3\2\2\2\u01c7" + + "\u01c9\t\5\2\2\u01c8\u01c7\3\2\2\2\u01c8\u01c9\3\2\2\2\u01c9\u0095\3\2" + + "\2\2\u01ca\u01d3\7\62\2\2\u01cb\u01cf\t\b\2\2\u01cc\u01ce\t\t\2\2\u01cd" + + "\u01cc\3\2\2\2\u01ce\u01d1\3\2\2\2\u01cf\u01cd\3\2\2\2\u01cf\u01d0\3\2" + + "\2\2\u01d0\u01d3\3\2\2\2\u01d1\u01cf\3\2\2\2\u01d2\u01ca\3\2\2\2\u01d2" + + "\u01cb\3\2\2\2\u01d3\u01d5\3\2\2\2\u01d4\u01d6\t\n\2\2\u01d5\u01d4\3\2" + + "\2\2\u01d5\u01d6\3\2\2\2\u01d6\u0097\3\2\2\2\u01d7\u01e0\7\62\2\2\u01d8" + + "\u01dc\t\b\2\2\u01d9\u01db\t\t\2\2\u01da\u01d9\3\2\2\2\u01db\u01de\3\2" + + "\2\2\u01dc\u01da\3\2\2\2\u01dc\u01dd\3\2\2\2\u01dd\u01e0\3\2\2\2\u01de" + + "\u01dc\3\2\2\2\u01df\u01d7\3\2\2\2\u01df\u01d8\3\2\2\2\u01e0\u01e7\3\2" + + "\2\2\u01e1\u01e3\5\24\n\2\u01e2\u01e4\t\t\2\2\u01e3\u01e2\3\2\2\2\u01e4" + + "\u01e5\3\2\2\2\u01e5\u01e3\3\2\2\2\u01e5\u01e6\3\2\2\2\u01e6\u01e8\3\2" + + "\2\2\u01e7\u01e1\3\2\2\2\u01e7\u01e8\3\2\2\2\u01e8\u01f2\3\2\2\2\u01e9" + + "\u01eb\t\13\2\2\u01ea\u01ec\t\f\2\2\u01eb\u01ea\3\2\2\2\u01eb\u01ec\3" + + "\2\2\2\u01ec\u01ee\3\2\2\2\u01ed\u01ef\t\t\2\2\u01ee\u01ed\3\2\2\2\u01ef" + + "\u01f0\3\2\2\2\u01f0\u01ee\3\2\2\2\u01f0\u01f1\3\2\2\2\u01f1\u01f3\3\2" + + "\2\2\u01f2\u01e9\3\2\2\2\u01f2\u01f3\3\2\2\2\u01f3\u01f5\3\2\2\2\u01f4" + + "\u01f6\t\r\2\2\u01f5\u01f4\3\2\2\2\u01f5\u01f6\3\2\2\2\u01f6\u0099\3\2" + + "\2\2\u01f7\u01ff\7$\2\2\u01f8\u01f9\7^\2\2\u01f9\u01fe\7$\2\2\u01fa\u01fb" + + "\7^\2\2\u01fb\u01fe\7^\2\2\u01fc\u01fe\n\16\2\2\u01fd\u01f8\3\2\2\2\u01fd" + + "\u01fa\3\2\2\2\u01fd\u01fc\3\2\2\2\u01fe\u0201\3\2\2\2\u01ff\u0200\3\2" + + "\2\2\u01ff\u01fd\3\2\2\2\u0200\u0202\3\2\2\2\u0201\u01ff\3\2\2\2\u0202" + + "\u0210\7$\2\2\u0203\u020b\7)\2\2\u0204\u0205\7^\2\2\u0205\u020a\7)\2\2" + + "\u0206\u0207\7^\2\2\u0207\u020a\7^\2\2\u0208\u020a\n\17\2\2\u0209\u0204" + + "\3\2\2\2\u0209\u0206\3\2\2\2\u0209\u0208\3\2\2\2\u020a\u020d\3\2\2\2\u020b" + + "\u020c\3\2\2\2\u020b\u0209\3\2\2\2\u020c\u020e\3\2\2\2\u020d\u020b\3\2" + + "\2\2\u020e\u0210\7)\2\2\u020f\u01f7\3\2\2\2\u020f\u0203\3\2\2\2\u0210" + + "\u009b\3\2\2\2\u0211\u0215\7\61\2\2\u0212\u0213\7^\2\2\u0213\u0216\n\20" + + "\2\2\u0214\u0216\n\21\2\2\u0215\u0212\3\2\2\2\u0215\u0214\3\2\2\2\u0216" + + "\u0217\3\2\2\2\u0217\u0218\3\2\2\2\u0217\u0215\3\2\2\2\u0218\u0219\3\2" + + "\2\2\u0219\u021d\7\61\2\2\u021a\u021c\t\22\2\2\u021b\u021a\3\2\2\2\u021c" + + "\u021f\3\2\2\2\u021d\u021b\3\2\2\2\u021d\u021e\3\2\2\2\u021e\u0220\3\2" + + "\2\2\u021f\u021d\3\2\2\2\u0220\u0221\6N\3\2\u0221\u009d\3\2\2\2\u0222" + + "\u0223\7v\2\2\u0223\u0224\7t\2\2\u0224\u0225\7w\2\2\u0225\u0226\7g\2\2" + + "\u0226\u009f\3\2\2\2\u0227\u0228\7h\2\2\u0228\u0229\7c\2\2\u0229\u022a" + + "\7n\2\2\u022a\u022b\7u\2\2\u022b\u022c\7g\2\2\u022c\u00a1\3\2\2\2\u022d" + + "\u022e\7p\2\2\u022e\u022f\7w\2\2\u022f\u0230\7n\2\2\u0230\u0231\7n\2\2" + + "\u0231\u00a3\3\2\2\2\u0232\u0233\7d\2\2\u0233\u0234\7q\2\2\u0234\u0235" + + "\7q\2\2\u0235\u0236\7n\2\2\u0236\u0237\7g\2\2\u0237\u0238\7c\2\2\u0238" + + "\u0259\7p\2\2\u0239\u023a\7d\2\2\u023a\u023b\7{\2\2\u023b\u023c\7v\2\2" + + "\u023c\u0259\7g\2\2\u023d\u023e\7u\2\2\u023e\u023f\7j\2\2\u023f\u0240" + + "\7q\2\2\u0240\u0241\7t\2\2\u0241\u0259\7v\2\2\u0242\u0243\7e\2\2\u0243" + + "\u0244\7j\2\2\u0244\u0245\7c\2\2\u0245\u0259\7t\2\2\u0246\u0247\7k\2\2" + + "\u0247\u0248\7p\2\2\u0248\u0259\7v\2\2\u0249\u024a\7n\2\2\u024a\u024b" + + "\7q\2\2\u024b\u024c\7p\2\2\u024c\u0259\7i\2\2\u024d\u024e\7h\2\2\u024e" + + "\u024f\7n\2\2\u024f\u0250\7q\2\2\u0250\u0251\7c\2\2\u0251\u0259\7v\2\2" + + "\u0252\u0253\7f\2\2\u0253\u0254\7q\2\2\u0254\u0255\7w\2\2\u0255\u0256" + + "\7d\2\2\u0256\u0257\7n\2\2\u0257\u0259\7g\2\2\u0258\u0232\3\2\2\2\u0258" + + "\u0239\3\2\2\2\u0258\u023d\3\2\2\2\u0258\u0242\3\2\2\2\u0258\u0246\3\2" + + "\2\2\u0258\u0249\3\2\2\2\u0258\u024d\3\2\2\2\u0258\u0252\3\2\2\2\u0259" + + "\u00a5\3\2\2\2\u025a\u025b\7f\2\2\u025b\u025c\7g\2\2\u025c\u025d\7h\2" + + "\2\u025d\u00a7\3\2\2\2\u025e\u0262\t\23\2\2\u025f\u0261\t\24\2\2\u0260" + + "\u025f\3\2\2\2\u0261\u0264\3\2\2\2\u0262\u0260\3\2\2\2\u0262\u0263\3\2" + + "\2\2\u0263\u00a9\3\2\2\2\u0264\u0262\3\2\2\2\u0265\u026e\7\62\2\2\u0266" + + "\u026a\t\b\2\2\u0267\u0269\t\t\2\2\u0268\u0267\3\2\2\2\u0269\u026c\3\2" + + "\2\2\u026a\u0268\3\2\2\2\u026a\u026b\3\2\2\2\u026b\u026e\3\2\2\2\u026c" + + "\u026a\3\2\2\2\u026d\u0265\3\2\2\2\u026d\u0266\3\2\2\2\u026e\u026f\3\2" + + "\2\2\u026f\u0270\bU\4\2\u0270\u00ab\3\2\2\2\u0271\u0275\t\23\2\2\u0272" + + "\u0274\t\24\2\2\u0273\u0272\3\2\2\2\u0274\u0277\3\2\2\2\u0275\u0273\3" + + "\2\2\2\u0275\u0276\3\2\2\2\u0276\u0278\3\2\2\2\u0277\u0275\3\2\2\2\u0278" + + "\u0279\bV\4\2\u0279\u00ad\3\2\2\2$\2\3\u00b1\u00bb\u00c5\u00ca\u01bb\u01be" + + "\u01c5\u01c8\u01cf\u01d2\u01d5\u01dc\u01df\u01e5\u01e7\u01eb\u01f0\u01f2" + + "\u01f5\u01fd\u01ff\u0209\u020b\u020f\u0215\u0217\u021d\u0258\u0262\u026a" + + "\u026d\u0275\5\b\2\2\4\3\2\4\2\2"; + public static final ATN _ATN = new ATNDeserializer().deserialize(_serializedATN.toCharArray()); + static { + _decisionToDFA = new DFA[_ATN.getNumberOfDecisions()]; + for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) { + _decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i); + } } - } } diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/PainlessParser.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/PainlessParser.java index ef58500303fdc..a520e06cf7f58 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/PainlessParser.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/PainlessParser.java @@ -1,4389 +1,5510 @@ // ANTLR GENERATED CODE: DO NOT EDIT package org.elasticsearch.painless.antlr; + +import org.antlr.v4.runtime.*; import org.antlr.v4.runtime.atn.*; import org.antlr.v4.runtime.dfa.DFA; -import org.antlr.v4.runtime.*; import org.antlr.v4.runtime.misc.*; import org.antlr.v4.runtime.tree.*; + import java.util.List; -import java.util.Iterator; -import java.util.ArrayList; -@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"}) +@SuppressWarnings({ "all", "warnings", "unchecked", "unused", "cast" }) class PainlessParser extends Parser { - static { RuntimeMetaData.checkVersion("4.5.3", RuntimeMetaData.VERSION); } - - protected static final DFA[] _decisionToDFA; - protected static final PredictionContextCache _sharedContextCache = - new PredictionContextCache(); - public static final int - WS=1, COMMENT=2, LBRACK=3, RBRACK=4, LBRACE=5, RBRACE=6, LP=7, RP=8, DOT=9, - NSDOT=10, COMMA=11, SEMICOLON=12, IF=13, IN=14, ELSE=15, WHILE=16, DO=17, - FOR=18, CONTINUE=19, BREAK=20, RETURN=21, NEW=22, TRY=23, CATCH=24, THROW=25, - THIS=26, INSTANCEOF=27, BOOLNOT=28, BWNOT=29, MUL=30, DIV=31, REM=32, - ADD=33, SUB=34, LSH=35, RSH=36, USH=37, LT=38, LTE=39, GT=40, GTE=41, - EQ=42, EQR=43, NE=44, NER=45, BWAND=46, XOR=47, BWOR=48, BOOLAND=49, BOOLOR=50, - COND=51, COLON=52, ELVIS=53, REF=54, ARROW=55, FIND=56, MATCH=57, INCR=58, - DECR=59, ASSIGN=60, AADD=61, ASUB=62, AMUL=63, ADIV=64, AREM=65, AAND=66, - AXOR=67, AOR=68, ALSH=69, ARSH=70, AUSH=71, OCTAL=72, HEX=73, INTEGER=74, - DECIMAL=75, STRING=76, REGEX=77, TRUE=78, FALSE=79, NULL=80, PRIMITIVE=81, - DEF=82, ID=83, DOTINTEGER=84, DOTID=85; - public static final int - RULE_source = 0, RULE_function = 1, RULE_parameters = 2, RULE_statement = 3, - RULE_rstatement = 4, RULE_dstatement = 5, RULE_trailer = 6, RULE_block = 7, - RULE_empty = 8, RULE_initializer = 9, RULE_afterthought = 10, RULE_declaration = 11, - RULE_decltype = 12, RULE_type = 13, RULE_declvar = 14, RULE_trap = 15, - RULE_noncondexpression = 16, RULE_expression = 17, RULE_unary = 18, RULE_unarynotaddsub = 19, - RULE_castexpression = 20, RULE_primordefcasttype = 21, RULE_refcasttype = 22, - RULE_chain = 23, RULE_primary = 24, RULE_postfix = 25, RULE_postdot = 26, - RULE_callinvoke = 27, RULE_fieldaccess = 28, RULE_braceaccess = 29, RULE_arrayinitializer = 30, - RULE_listinitializer = 31, RULE_mapinitializer = 32, RULE_maptoken = 33, - RULE_arguments = 34, RULE_argument = 35, RULE_lambda = 36, RULE_lamtype = 37, - RULE_funcref = 38; - public static final String[] ruleNames = { - "source", "function", "parameters", "statement", "rstatement", "dstatement", - "trailer", "block", "empty", "initializer", "afterthought", "declaration", - "decltype", "type", "declvar", "trap", "noncondexpression", "expression", - "unary", "unarynotaddsub", "castexpression", "primordefcasttype", "refcasttype", - "chain", "primary", "postfix", "postdot", "callinvoke", "fieldaccess", - "braceaccess", "arrayinitializer", "listinitializer", "mapinitializer", - "maptoken", "arguments", "argument", "lambda", "lamtype", "funcref" - }; - - private static final String[] _LITERAL_NAMES = { - null, null, null, "'{'", "'}'", "'['", "']'", "'('", "')'", "'.'", "'?.'", - "','", "';'", "'if'", "'in'", "'else'", "'while'", "'do'", "'for'", "'continue'", - "'break'", "'return'", "'new'", "'try'", "'catch'", "'throw'", "'this'", - "'instanceof'", "'!'", "'~'", "'*'", "'/'", "'%'", "'+'", "'-'", "'<<'", - "'>>'", "'>>>'", "'<'", "'<='", "'>'", "'>='", "'=='", "'==='", "'!='", - "'!=='", "'&'", "'^'", "'|'", "'&&'", "'||'", "'?'", "':'", "'?:'", "'::'", - "'->'", "'=~'", "'==~'", "'++'", "'--'", "'='", "'+='", "'-='", "'*='", - "'/='", "'%='", "'&='", "'^='", "'|='", "'<<='", "'>>='", "'>>>='", null, - null, null, null, null, null, "'true'", "'false'", "'null'", null, "'def'" - }; - private static final String[] _SYMBOLIC_NAMES = { - null, "WS", "COMMENT", "LBRACK", "RBRACK", "LBRACE", "RBRACE", "LP", "RP", - "DOT", "NSDOT", "COMMA", "SEMICOLON", "IF", "IN", "ELSE", "WHILE", "DO", - "FOR", "CONTINUE", "BREAK", "RETURN", "NEW", "TRY", "CATCH", "THROW", - "THIS", "INSTANCEOF", "BOOLNOT", "BWNOT", "MUL", "DIV", "REM", "ADD", - "SUB", "LSH", "RSH", "USH", "LT", "LTE", "GT", "GTE", "EQ", "EQR", "NE", - "NER", "BWAND", "XOR", "BWOR", "BOOLAND", "BOOLOR", "COND", "COLON", "ELVIS", - "REF", "ARROW", "FIND", "MATCH", "INCR", "DECR", "ASSIGN", "AADD", "ASUB", - "AMUL", "ADIV", "AREM", "AAND", "AXOR", "AOR", "ALSH", "ARSH", "AUSH", - "OCTAL", "HEX", "INTEGER", "DECIMAL", "STRING", "REGEX", "TRUE", "FALSE", - "NULL", "PRIMITIVE", "DEF", "ID", "DOTINTEGER", "DOTID" - }; - public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); - - /** - * @deprecated Use {@link #VOCABULARY} instead. - */ - @Deprecated - public static final String[] tokenNames; - static { - tokenNames = new String[_SYMBOLIC_NAMES.length]; - for (int i = 0; i < tokenNames.length; i++) { - tokenNames[i] = VOCABULARY.getLiteralName(i); - if (tokenNames[i] == null) { - tokenNames[i] = VOCABULARY.getSymbolicName(i); - } - - if (tokenNames[i] == null) { - tokenNames[i] = ""; - } - } - } - - @Override - @Deprecated - public String[] getTokenNames() { - return tokenNames; - } - - @Override - - public Vocabulary getVocabulary() { - return VOCABULARY; - } - - @Override - public String getGrammarFileName() { return "PainlessParser.g4"; } - - @Override - public String[] getRuleNames() { return ruleNames; } - - @Override - public String getSerializedATN() { return _serializedATN; } - - @Override - public ATN getATN() { return _ATN; } - - public PainlessParser(TokenStream input) { - super(input); - _interp = new ParserATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache); - } - public static class SourceContext extends ParserRuleContext { - public TerminalNode EOF() { return getToken(PainlessParser.EOF, 0); } - public List function() { - return getRuleContexts(FunctionContext.class); - } - public FunctionContext function(int i) { - return getRuleContext(FunctionContext.class,i); - } - public List statement() { - return getRuleContexts(StatementContext.class); - } - public StatementContext statement(int i) { - return getRuleContext(StatementContext.class,i); - } - public SourceContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_source; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitSource(this); - else return visitor.visitChildren(this); - } - } - - public final SourceContext source() throws RecognitionException { - SourceContext _localctx = new SourceContext(_ctx, getState()); - enterRule(_localctx, 0, RULE_source); - int _la; - try { - int _alt; - enterOuterAlt(_localctx, 1); - { - setState(81); - _errHandler.sync(this); - _alt = getInterpreter().adaptivePredict(_input,0,_ctx); - while ( _alt!=2 && _alt!=org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER ) { - if ( _alt==1 ) { - { - { - setState(78); - function(); - } - } - } - setState(83); - _errHandler.sync(this); - _alt = getInterpreter().adaptivePredict(_input,0,_ctx); - } - setState(87); - _errHandler.sync(this); - _la = _input.LA(1); - while ((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << LBRACE) | (1L << LP) | (1L << IF) | (1L << WHILE) | (1L << DO) | (1L << FOR) | (1L << CONTINUE) | (1L << BREAK) | (1L << RETURN) | (1L << NEW) | (1L << TRY) | (1L << THROW) | (1L << BOOLNOT) | (1L << BWNOT) | (1L << ADD) | (1L << SUB) | (1L << INCR) | (1L << DECR))) != 0) || ((((_la - 72)) & ~0x3f) == 0 && ((1L << (_la - 72)) & ((1L << (OCTAL - 72)) | (1L << (HEX - 72)) | (1L << (INTEGER - 72)) | (1L << (DECIMAL - 72)) | (1L << (STRING - 72)) | (1L << (REGEX - 72)) | (1L << (TRUE - 72)) | (1L << (FALSE - 72)) | (1L << (NULL - 72)) | (1L << (PRIMITIVE - 72)) | (1L << (DEF - 72)) | (1L << (ID - 72)))) != 0)) { - { - { - setState(84); - statement(); - } - } - setState(89); - _errHandler.sync(this); - _la = _input.LA(1); - } - setState(90); - match(EOF); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class FunctionContext extends ParserRuleContext { - public DecltypeContext decltype() { - return getRuleContext(DecltypeContext.class,0); - } - public TerminalNode ID() { return getToken(PainlessParser.ID, 0); } - public ParametersContext parameters() { - return getRuleContext(ParametersContext.class,0); - } - public BlockContext block() { - return getRuleContext(BlockContext.class,0); - } - public FunctionContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_function; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitFunction(this); - else return visitor.visitChildren(this); - } - } - - public final FunctionContext function() throws RecognitionException { - FunctionContext _localctx = new FunctionContext(_ctx, getState()); - enterRule(_localctx, 2, RULE_function); - try { - enterOuterAlt(_localctx, 1); - { - setState(92); - decltype(); - setState(93); - match(ID); - setState(94); - parameters(); - setState(95); - block(); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class ParametersContext extends ParserRuleContext { - public TerminalNode LP() { return getToken(PainlessParser.LP, 0); } - public TerminalNode RP() { return getToken(PainlessParser.RP, 0); } - public List decltype() { - return getRuleContexts(DecltypeContext.class); - } - public DecltypeContext decltype(int i) { - return getRuleContext(DecltypeContext.class,i); - } - public List ID() { return getTokens(PainlessParser.ID); } - public TerminalNode ID(int i) { - return getToken(PainlessParser.ID, i); - } - public List COMMA() { return getTokens(PainlessParser.COMMA); } - public TerminalNode COMMA(int i) { - return getToken(PainlessParser.COMMA, i); - } - public ParametersContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_parameters; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitParameters(this); - else return visitor.visitChildren(this); - } - } - - public final ParametersContext parameters() throws RecognitionException { - ParametersContext _localctx = new ParametersContext(_ctx, getState()); - enterRule(_localctx, 4, RULE_parameters); - int _la; - try { - enterOuterAlt(_localctx, 1); - { - setState(97); - match(LP); - setState(109); - _la = _input.LA(1); - if (((((_la - 81)) & ~0x3f) == 0 && ((1L << (_la - 81)) & ((1L << (PRIMITIVE - 81)) | (1L << (DEF - 81)) | (1L << (ID - 81)))) != 0)) { - { - setState(98); - decltype(); - setState(99); - match(ID); - setState(106); - _errHandler.sync(this); - _la = _input.LA(1); - while (_la==COMMA) { - { - { - setState(100); - match(COMMA); - setState(101); - decltype(); - setState(102); - match(ID); - } - } - setState(108); - _errHandler.sync(this); - _la = _input.LA(1); - } - } - } - - setState(111); - match(RP); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class StatementContext extends ParserRuleContext { - public RstatementContext rstatement() { - return getRuleContext(RstatementContext.class,0); - } - public DstatementContext dstatement() { - return getRuleContext(DstatementContext.class,0); - } - public TerminalNode SEMICOLON() { return getToken(PainlessParser.SEMICOLON, 0); } - public TerminalNode EOF() { return getToken(PainlessParser.EOF, 0); } - public StatementContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_statement; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitStatement(this); - else return visitor.visitChildren(this); - } - } - - public final StatementContext statement() throws RecognitionException { - StatementContext _localctx = new StatementContext(_ctx, getState()); - enterRule(_localctx, 6, RULE_statement); - int _la; - try { - setState(117); - switch (_input.LA(1)) { - case IF: - case WHILE: - case FOR: - case TRY: - enterOuterAlt(_localctx, 1); - { - setState(113); - rstatement(); - } - break; - case LBRACE: - case LP: - case DO: - case CONTINUE: - case BREAK: - case RETURN: - case NEW: - case THROW: - case BOOLNOT: - case BWNOT: - case ADD: - case SUB: - case INCR: - case DECR: - case OCTAL: - case HEX: - case INTEGER: - case DECIMAL: - case STRING: - case REGEX: - case TRUE: - case FALSE: - case NULL: - case PRIMITIVE: - case DEF: - case ID: - enterOuterAlt(_localctx, 2); - { - setState(114); - dstatement(); - setState(115); - _la = _input.LA(1); - if ( !(_la==EOF || _la==SEMICOLON) ) { - _errHandler.recoverInline(this); - } else { - consume(); - } - } - break; - default: - throw new NoViableAltException(this); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class RstatementContext extends ParserRuleContext { - public RstatementContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_rstatement; } - - public RstatementContext() { } - public void copyFrom(RstatementContext ctx) { - super.copyFrom(ctx); - } - } - public static class ForContext extends RstatementContext { - public TerminalNode FOR() { return getToken(PainlessParser.FOR, 0); } - public TerminalNode LP() { return getToken(PainlessParser.LP, 0); } - public List SEMICOLON() { return getTokens(PainlessParser.SEMICOLON); } - public TerminalNode SEMICOLON(int i) { - return getToken(PainlessParser.SEMICOLON, i); - } - public TerminalNode RP() { return getToken(PainlessParser.RP, 0); } - public TrailerContext trailer() { - return getRuleContext(TrailerContext.class,0); - } - public EmptyContext empty() { - return getRuleContext(EmptyContext.class,0); - } - public InitializerContext initializer() { - return getRuleContext(InitializerContext.class,0); - } - public ExpressionContext expression() { - return getRuleContext(ExpressionContext.class,0); - } - public AfterthoughtContext afterthought() { - return getRuleContext(AfterthoughtContext.class,0); - } - public ForContext(RstatementContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitFor(this); - else return visitor.visitChildren(this); - } - } - public static class TryContext extends RstatementContext { - public TerminalNode TRY() { return getToken(PainlessParser.TRY, 0); } - public BlockContext block() { - return getRuleContext(BlockContext.class,0); - } - public List trap() { - return getRuleContexts(TrapContext.class); - } - public TrapContext trap(int i) { - return getRuleContext(TrapContext.class,i); - } - public TryContext(RstatementContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitTry(this); - else return visitor.visitChildren(this); - } - } - public static class WhileContext extends RstatementContext { - public TerminalNode WHILE() { return getToken(PainlessParser.WHILE, 0); } - public TerminalNode LP() { return getToken(PainlessParser.LP, 0); } - public ExpressionContext expression() { - return getRuleContext(ExpressionContext.class,0); - } - public TerminalNode RP() { return getToken(PainlessParser.RP, 0); } - public TrailerContext trailer() { - return getRuleContext(TrailerContext.class,0); + static { + RuntimeMetaData.checkVersion("4.5.3", RuntimeMetaData.VERSION); } - public EmptyContext empty() { - return getRuleContext(EmptyContext.class,0); - } - public WhileContext(RstatementContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitWhile(this); - else return visitor.visitChildren(this); - } - } - public static class IneachContext extends RstatementContext { - public TerminalNode FOR() { return getToken(PainlessParser.FOR, 0); } - public TerminalNode LP() { return getToken(PainlessParser.LP, 0); } - public TerminalNode ID() { return getToken(PainlessParser.ID, 0); } - public TerminalNode IN() { return getToken(PainlessParser.IN, 0); } - public ExpressionContext expression() { - return getRuleContext(ExpressionContext.class,0); - } - public TerminalNode RP() { return getToken(PainlessParser.RP, 0); } - public TrailerContext trailer() { - return getRuleContext(TrailerContext.class,0); - } - public IneachContext(RstatementContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitIneach(this); - else return visitor.visitChildren(this); - } - } - public static class IfContext extends RstatementContext { - public TerminalNode IF() { return getToken(PainlessParser.IF, 0); } - public TerminalNode LP() { return getToken(PainlessParser.LP, 0); } - public ExpressionContext expression() { - return getRuleContext(ExpressionContext.class,0); - } - public TerminalNode RP() { return getToken(PainlessParser.RP, 0); } - public List trailer() { - return getRuleContexts(TrailerContext.class); - } - public TrailerContext trailer(int i) { - return getRuleContext(TrailerContext.class,i); - } - public TerminalNode ELSE() { return getToken(PainlessParser.ELSE, 0); } - public IfContext(RstatementContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitIf(this); - else return visitor.visitChildren(this); - } - } - public static class EachContext extends RstatementContext { - public TerminalNode FOR() { return getToken(PainlessParser.FOR, 0); } - public TerminalNode LP() { return getToken(PainlessParser.LP, 0); } - public DecltypeContext decltype() { - return getRuleContext(DecltypeContext.class,0); - } - public TerminalNode ID() { return getToken(PainlessParser.ID, 0); } - public TerminalNode COLON() { return getToken(PainlessParser.COLON, 0); } - public ExpressionContext expression() { - return getRuleContext(ExpressionContext.class,0); - } - public TerminalNode RP() { return getToken(PainlessParser.RP, 0); } - public TrailerContext trailer() { - return getRuleContext(TrailerContext.class,0); - } - public EachContext(RstatementContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitEach(this); - else return visitor.visitChildren(this); - } - } - - public final RstatementContext rstatement() throws RecognitionException { - RstatementContext _localctx = new RstatementContext(_ctx, getState()); - enterRule(_localctx, 8, RULE_rstatement); - int _la; - try { - int _alt; - setState(179); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,12,_ctx) ) { - case 1: - _localctx = new IfContext(_localctx); - enterOuterAlt(_localctx, 1); - { - setState(119); - match(IF); - setState(120); - match(LP); - setState(121); - expression(); - setState(122); - match(RP); - setState(123); - trailer(); - setState(127); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,5,_ctx) ) { - case 1: - { - setState(124); - match(ELSE); - setState(125); - trailer(); - } - break; - case 2: - { - setState(126); - if (!( _input.LA(1) != ELSE )) throw new FailedPredicateException(this, " _input.LA(1) != ELSE "); - } - break; - } - } - break; - case 2: - _localctx = new WhileContext(_localctx); - enterOuterAlt(_localctx, 2); - { - setState(129); - match(WHILE); - setState(130); - match(LP); - setState(131); - expression(); - setState(132); - match(RP); - setState(135); - switch (_input.LA(1)) { - case LBRACK: - case LBRACE: - case LP: - case IF: - case WHILE: - case DO: - case FOR: - case CONTINUE: - case BREAK: - case RETURN: - case NEW: - case TRY: - case THROW: - case BOOLNOT: - case BWNOT: - case ADD: - case SUB: - case INCR: - case DECR: - case OCTAL: - case HEX: - case INTEGER: - case DECIMAL: - case STRING: - case REGEX: - case TRUE: - case FALSE: - case NULL: - case PRIMITIVE: - case DEF: - case ID: - { - setState(133); - trailer(); - } - break; - case SEMICOLON: - { - setState(134); - empty(); - } - break; - default: - throw new NoViableAltException(this); - } - } - break; - case 3: - _localctx = new ForContext(_localctx); - enterOuterAlt(_localctx, 3); - { - setState(137); - match(FOR); - setState(138); - match(LP); - setState(140); - _la = _input.LA(1); - if ((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << LBRACE) | (1L << LP) | (1L << NEW) | (1L << BOOLNOT) | (1L << BWNOT) | (1L << ADD) | (1L << SUB) | (1L << INCR) | (1L << DECR))) != 0) || ((((_la - 72)) & ~0x3f) == 0 && ((1L << (_la - 72)) & ((1L << (OCTAL - 72)) | (1L << (HEX - 72)) | (1L << (INTEGER - 72)) | (1L << (DECIMAL - 72)) | (1L << (STRING - 72)) | (1L << (REGEX - 72)) | (1L << (TRUE - 72)) | (1L << (FALSE - 72)) | (1L << (NULL - 72)) | (1L << (PRIMITIVE - 72)) | (1L << (DEF - 72)) | (1L << (ID - 72)))) != 0)) { - { - setState(139); - initializer(); - } - } - - setState(142); - match(SEMICOLON); - setState(144); - _la = _input.LA(1); - if ((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << LBRACE) | (1L << LP) | (1L << NEW) | (1L << BOOLNOT) | (1L << BWNOT) | (1L << ADD) | (1L << SUB) | (1L << INCR) | (1L << DECR))) != 0) || ((((_la - 72)) & ~0x3f) == 0 && ((1L << (_la - 72)) & ((1L << (OCTAL - 72)) | (1L << (HEX - 72)) | (1L << (INTEGER - 72)) | (1L << (DECIMAL - 72)) | (1L << (STRING - 72)) | (1L << (REGEX - 72)) | (1L << (TRUE - 72)) | (1L << (FALSE - 72)) | (1L << (NULL - 72)) | (1L << (ID - 72)))) != 0)) { - { - setState(143); - expression(); - } - } - - setState(146); - match(SEMICOLON); - setState(148); - _la = _input.LA(1); - if ((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << LBRACE) | (1L << LP) | (1L << NEW) | (1L << BOOLNOT) | (1L << BWNOT) | (1L << ADD) | (1L << SUB) | (1L << INCR) | (1L << DECR))) != 0) || ((((_la - 72)) & ~0x3f) == 0 && ((1L << (_la - 72)) & ((1L << (OCTAL - 72)) | (1L << (HEX - 72)) | (1L << (INTEGER - 72)) | (1L << (DECIMAL - 72)) | (1L << (STRING - 72)) | (1L << (REGEX - 72)) | (1L << (TRUE - 72)) | (1L << (FALSE - 72)) | (1L << (NULL - 72)) | (1L << (ID - 72)))) != 0)) { - { - setState(147); - afterthought(); - } - } - - setState(150); - match(RP); - setState(153); - switch (_input.LA(1)) { - case LBRACK: - case LBRACE: - case LP: - case IF: - case WHILE: - case DO: - case FOR: - case CONTINUE: - case BREAK: - case RETURN: - case NEW: - case TRY: - case THROW: - case BOOLNOT: - case BWNOT: - case ADD: - case SUB: - case INCR: - case DECR: - case OCTAL: - case HEX: - case INTEGER: - case DECIMAL: - case STRING: - case REGEX: - case TRUE: - case FALSE: - case NULL: - case PRIMITIVE: - case DEF: - case ID: - { - setState(151); - trailer(); - } - break; - case SEMICOLON: - { - setState(152); - empty(); - } - break; - default: - throw new NoViableAltException(this); - } - } - break; - case 4: - _localctx = new EachContext(_localctx); - enterOuterAlt(_localctx, 4); - { - setState(155); - match(FOR); - setState(156); - match(LP); - setState(157); - decltype(); - setState(158); - match(ID); - setState(159); - match(COLON); - setState(160); - expression(); - setState(161); - match(RP); - setState(162); - trailer(); - } - break; - case 5: - _localctx = new IneachContext(_localctx); - enterOuterAlt(_localctx, 5); - { - setState(164); - match(FOR); - setState(165); - match(LP); - setState(166); - match(ID); - setState(167); - match(IN); - setState(168); - expression(); - setState(169); - match(RP); - setState(170); - trailer(); - } - break; - case 6: - _localctx = new TryContext(_localctx); - enterOuterAlt(_localctx, 6); - { - setState(172); - match(TRY); - setState(173); - block(); - setState(175); - _errHandler.sync(this); - _alt = 1; - do { - switch (_alt) { - case 1: - { - { - setState(174); - trap(); + + protected static final DFA[] _decisionToDFA; + protected static final PredictionContextCache _sharedContextCache = new PredictionContextCache(); + public static final int WS = 1, COMMENT = 2, LBRACK = 3, RBRACK = 4, LBRACE = 5, RBRACE = 6, LP = 7, RP = 8, DOT = 9, NSDOT = 10, + COMMA = 11, SEMICOLON = 12, IF = 13, IN = 14, ELSE = 15, WHILE = 16, DO = 17, FOR = 18, CONTINUE = 19, BREAK = 20, RETURN = 21, + NEW = 22, TRY = 23, CATCH = 24, THROW = 25, THIS = 26, INSTANCEOF = 27, BOOLNOT = 28, BWNOT = 29, MUL = 30, DIV = 31, REM = 32, + ADD = 33, SUB = 34, LSH = 35, RSH = 36, USH = 37, LT = 38, LTE = 39, GT = 40, GTE = 41, EQ = 42, EQR = 43, NE = 44, NER = 45, + BWAND = 46, XOR = 47, BWOR = 48, BOOLAND = 49, BOOLOR = 50, COND = 51, COLON = 52, ELVIS = 53, REF = 54, ARROW = 55, FIND = 56, + MATCH = 57, INCR = 58, DECR = 59, ASSIGN = 60, AADD = 61, ASUB = 62, AMUL = 63, ADIV = 64, AREM = 65, AAND = 66, AXOR = 67, AOR = + 68, ALSH = 69, ARSH = 70, AUSH = 71, OCTAL = 72, HEX = 73, INTEGER = 74, DECIMAL = 75, STRING = 76, REGEX = 77, TRUE = 78, + FALSE = 79, NULL = 80, PRIMITIVE = 81, DEF = 82, ID = 83, DOTINTEGER = 84, DOTID = 85; + public static final int RULE_source = 0, RULE_function = 1, RULE_parameters = 2, RULE_statement = 3, RULE_rstatement = 4, + RULE_dstatement = 5, RULE_trailer = 6, RULE_block = 7, RULE_empty = 8, RULE_initializer = 9, RULE_afterthought = 10, + RULE_declaration = 11, RULE_decltype = 12, RULE_type = 13, RULE_declvar = 14, RULE_trap = 15, RULE_noncondexpression = 16, + RULE_expression = 17, RULE_unary = 18, RULE_unarynotaddsub = 19, RULE_castexpression = 20, RULE_primordefcasttype = 21, + RULE_refcasttype = 22, RULE_chain = 23, RULE_primary = 24, RULE_postfix = 25, RULE_postdot = 26, RULE_callinvoke = 27, + RULE_fieldaccess = 28, RULE_braceaccess = 29, RULE_arrayinitializer = 30, RULE_listinitializer = 31, RULE_mapinitializer = 32, + RULE_maptoken = 33, RULE_arguments = 34, RULE_argument = 35, RULE_lambda = 36, RULE_lamtype = 37, RULE_funcref = 38; + public static final String[] ruleNames = { + "source", + "function", + "parameters", + "statement", + "rstatement", + "dstatement", + "trailer", + "block", + "empty", + "initializer", + "afterthought", + "declaration", + "decltype", + "type", + "declvar", + "trap", + "noncondexpression", + "expression", + "unary", + "unarynotaddsub", + "castexpression", + "primordefcasttype", + "refcasttype", + "chain", + "primary", + "postfix", + "postdot", + "callinvoke", + "fieldaccess", + "braceaccess", + "arrayinitializer", + "listinitializer", + "mapinitializer", + "maptoken", + "arguments", + "argument", + "lambda", + "lamtype", + "funcref" }; + + private static final String[] _LITERAL_NAMES = { + null, + null, + null, + "'{'", + "'}'", + "'['", + "']'", + "'('", + "')'", + "'.'", + "'?.'", + "','", + "';'", + "'if'", + "'in'", + "'else'", + "'while'", + "'do'", + "'for'", + "'continue'", + "'break'", + "'return'", + "'new'", + "'try'", + "'catch'", + "'throw'", + "'this'", + "'instanceof'", + "'!'", + "'~'", + "'*'", + "'/'", + "'%'", + "'+'", + "'-'", + "'<<'", + "'>>'", + "'>>>'", + "'<'", + "'<='", + "'>'", + "'>='", + "'=='", + "'==='", + "'!='", + "'!=='", + "'&'", + "'^'", + "'|'", + "'&&'", + "'||'", + "'?'", + "':'", + "'?:'", + "'::'", + "'->'", + "'=~'", + "'==~'", + "'++'", + "'--'", + "'='", + "'+='", + "'-='", + "'*='", + "'/='", + "'%='", + "'&='", + "'^='", + "'|='", + "'<<='", + "'>>='", + "'>>>='", + null, + null, + null, + null, + null, + null, + "'true'", + "'false'", + "'null'", + null, + "'def'" }; + private static final String[] _SYMBOLIC_NAMES = { + null, + "WS", + "COMMENT", + "LBRACK", + "RBRACK", + "LBRACE", + "RBRACE", + "LP", + "RP", + "DOT", + "NSDOT", + "COMMA", + "SEMICOLON", + "IF", + "IN", + "ELSE", + "WHILE", + "DO", + "FOR", + "CONTINUE", + "BREAK", + "RETURN", + "NEW", + "TRY", + "CATCH", + "THROW", + "THIS", + "INSTANCEOF", + "BOOLNOT", + "BWNOT", + "MUL", + "DIV", + "REM", + "ADD", + "SUB", + "LSH", + "RSH", + "USH", + "LT", + "LTE", + "GT", + "GTE", + "EQ", + "EQR", + "NE", + "NER", + "BWAND", + "XOR", + "BWOR", + "BOOLAND", + "BOOLOR", + "COND", + "COLON", + "ELVIS", + "REF", + "ARROW", + "FIND", + "MATCH", + "INCR", + "DECR", + "ASSIGN", + "AADD", + "ASUB", + "AMUL", + "ADIV", + "AREM", + "AAND", + "AXOR", + "AOR", + "ALSH", + "ARSH", + "AUSH", + "OCTAL", + "HEX", + "INTEGER", + "DECIMAL", + "STRING", + "REGEX", + "TRUE", + "FALSE", + "NULL", + "PRIMITIVE", + "DEF", + "ID", + "DOTINTEGER", + "DOTID" }; + public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); + + /** + * @deprecated Use {@link #VOCABULARY} instead. + */ + @Deprecated + public static final String[] tokenNames; + static { + tokenNames = new String[_SYMBOLIC_NAMES.length]; + for (int i = 0; i < tokenNames.length; i++) { + tokenNames[i] = VOCABULARY.getLiteralName(i); + if (tokenNames[i] == null) { + tokenNames[i] = VOCABULARY.getSymbolicName(i); } + + if (tokenNames[i] == null) { + tokenNames[i] = ""; } - break; - default: - throw new NoViableAltException(this); - } - setState(177); - _errHandler.sync(this); - _alt = getInterpreter().adaptivePredict(_input,11,_ctx); - } while ( _alt!=2 && _alt!=org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER ); - } - break; - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class DstatementContext extends ParserRuleContext { - public DstatementContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_dstatement; } - - public DstatementContext() { } - public void copyFrom(DstatementContext ctx) { - super.copyFrom(ctx); - } - } - public static class DeclContext extends DstatementContext { - public DeclarationContext declaration() { - return getRuleContext(DeclarationContext.class,0); - } - public DeclContext(DstatementContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitDecl(this); - else return visitor.visitChildren(this); - } - } - public static class BreakContext extends DstatementContext { - public TerminalNode BREAK() { return getToken(PainlessParser.BREAK, 0); } - public BreakContext(DstatementContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitBreak(this); - else return visitor.visitChildren(this); - } - } - public static class ThrowContext extends DstatementContext { - public TerminalNode THROW() { return getToken(PainlessParser.THROW, 0); } - public ExpressionContext expression() { - return getRuleContext(ExpressionContext.class,0); + } } - public ThrowContext(DstatementContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitThrow(this); - else return visitor.visitChildren(this); - } - } - public static class ContinueContext extends DstatementContext { - public TerminalNode CONTINUE() { return getToken(PainlessParser.CONTINUE, 0); } - public ContinueContext(DstatementContext ctx) { copyFrom(ctx); } + @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitContinue(this); - else return visitor.visitChildren(this); - } - } - public static class ExprContext extends DstatementContext { - public ExpressionContext expression() { - return getRuleContext(ExpressionContext.class,0); + @Deprecated + public String[] getTokenNames() { + return tokenNames; } - public ExprContext(DstatementContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitExpr(this); - else return visitor.visitChildren(this); - } - } - public static class DoContext extends DstatementContext { - public TerminalNode DO() { return getToken(PainlessParser.DO, 0); } - public BlockContext block() { - return getRuleContext(BlockContext.class,0); - } - public TerminalNode WHILE() { return getToken(PainlessParser.WHILE, 0); } - public TerminalNode LP() { return getToken(PainlessParser.LP, 0); } - public ExpressionContext expression() { - return getRuleContext(ExpressionContext.class,0); - } - public TerminalNode RP() { return getToken(PainlessParser.RP, 0); } - public DoContext(DstatementContext ctx) { copyFrom(ctx); } + @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitDo(this); - else return visitor.visitChildren(this); - } - } - public static class ReturnContext extends DstatementContext { - public TerminalNode RETURN() { return getToken(PainlessParser.RETURN, 0); } - public ExpressionContext expression() { - return getRuleContext(ExpressionContext.class,0); + + public Vocabulary getVocabulary() { + return VOCABULARY; } - public ReturnContext(DstatementContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitReturn(this); - else return visitor.visitChildren(this); - } - } - - public final DstatementContext dstatement() throws RecognitionException { - DstatementContext _localctx = new DstatementContext(_ctx, getState()); - enterRule(_localctx, 10, RULE_dstatement); - int _la; - try { - setState(198); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,14,_ctx) ) { - case 1: - _localctx = new DoContext(_localctx); - enterOuterAlt(_localctx, 1); - { - setState(181); - match(DO); - setState(182); - block(); - setState(183); - match(WHILE); - setState(184); - match(LP); - setState(185); - expression(); - setState(186); - match(RP); - } - break; - case 2: - _localctx = new DeclContext(_localctx); - enterOuterAlt(_localctx, 2); - { - setState(188); - declaration(); - } - break; - case 3: - _localctx = new ContinueContext(_localctx); - enterOuterAlt(_localctx, 3); - { - setState(189); - match(CONTINUE); - } - break; - case 4: - _localctx = new BreakContext(_localctx); - enterOuterAlt(_localctx, 4); - { - setState(190); - match(BREAK); - } - break; - case 5: - _localctx = new ReturnContext(_localctx); - enterOuterAlt(_localctx, 5); - { - setState(191); - match(RETURN); - setState(193); - _la = _input.LA(1); - if ((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << LBRACE) | (1L << LP) | (1L << NEW) | (1L << BOOLNOT) | (1L << BWNOT) | (1L << ADD) | (1L << SUB) | (1L << INCR) | (1L << DECR))) != 0) || ((((_la - 72)) & ~0x3f) == 0 && ((1L << (_la - 72)) & ((1L << (OCTAL - 72)) | (1L << (HEX - 72)) | (1L << (INTEGER - 72)) | (1L << (DECIMAL - 72)) | (1L << (STRING - 72)) | (1L << (REGEX - 72)) | (1L << (TRUE - 72)) | (1L << (FALSE - 72)) | (1L << (NULL - 72)) | (1L << (ID - 72)))) != 0)) { - { - setState(192); - expression(); - } - } - - } - break; - case 6: - _localctx = new ThrowContext(_localctx); - enterOuterAlt(_localctx, 6); - { - setState(195); - match(THROW); - setState(196); - expression(); - } - break; - case 7: - _localctx = new ExprContext(_localctx); - enterOuterAlt(_localctx, 7); - { - setState(197); - expression(); - } - break; - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class TrailerContext extends ParserRuleContext { - public BlockContext block() { - return getRuleContext(BlockContext.class,0); - } - public StatementContext statement() { - return getRuleContext(StatementContext.class,0); - } - public TrailerContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_trailer; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitTrailer(this); - else return visitor.visitChildren(this); - } - } - - public final TrailerContext trailer() throws RecognitionException { - TrailerContext _localctx = new TrailerContext(_ctx, getState()); - enterRule(_localctx, 12, RULE_trailer); - try { - setState(202); - switch (_input.LA(1)) { - case LBRACK: - enterOuterAlt(_localctx, 1); - { - setState(200); - block(); - } - break; - case LBRACE: - case LP: - case IF: - case WHILE: - case DO: - case FOR: - case CONTINUE: - case BREAK: - case RETURN: - case NEW: - case TRY: - case THROW: - case BOOLNOT: - case BWNOT: - case ADD: - case SUB: - case INCR: - case DECR: - case OCTAL: - case HEX: - case INTEGER: - case DECIMAL: - case STRING: - case REGEX: - case TRUE: - case FALSE: - case NULL: - case PRIMITIVE: - case DEF: - case ID: - enterOuterAlt(_localctx, 2); - { - setState(201); - statement(); - } - break; - default: - throw new NoViableAltException(this); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class BlockContext extends ParserRuleContext { - public TerminalNode LBRACK() { return getToken(PainlessParser.LBRACK, 0); } - public TerminalNode RBRACK() { return getToken(PainlessParser.RBRACK, 0); } - public List statement() { - return getRuleContexts(StatementContext.class); - } - public StatementContext statement(int i) { - return getRuleContext(StatementContext.class,i); - } - public DstatementContext dstatement() { - return getRuleContext(DstatementContext.class,0); - } - public BlockContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_block; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitBlock(this); - else return visitor.visitChildren(this); - } - } - - public final BlockContext block() throws RecognitionException { - BlockContext _localctx = new BlockContext(_ctx, getState()); - enterRule(_localctx, 14, RULE_block); - int _la; - try { - int _alt; - enterOuterAlt(_localctx, 1); - { - setState(204); - match(LBRACK); - setState(208); - _errHandler.sync(this); - _alt = getInterpreter().adaptivePredict(_input,16,_ctx); - while ( _alt!=2 && _alt!=org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER ) { - if ( _alt==1 ) { - { - { - setState(205); - statement(); - } - } - } - setState(210); - _errHandler.sync(this); - _alt = getInterpreter().adaptivePredict(_input,16,_ctx); - } - setState(212); - _la = _input.LA(1); - if ((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << LBRACE) | (1L << LP) | (1L << DO) | (1L << CONTINUE) | (1L << BREAK) | (1L << RETURN) | (1L << NEW) | (1L << THROW) | (1L << BOOLNOT) | (1L << BWNOT) | (1L << ADD) | (1L << SUB) | (1L << INCR) | (1L << DECR))) != 0) || ((((_la - 72)) & ~0x3f) == 0 && ((1L << (_la - 72)) & ((1L << (OCTAL - 72)) | (1L << (HEX - 72)) | (1L << (INTEGER - 72)) | (1L << (DECIMAL - 72)) | (1L << (STRING - 72)) | (1L << (REGEX - 72)) | (1L << (TRUE - 72)) | (1L << (FALSE - 72)) | (1L << (NULL - 72)) | (1L << (PRIMITIVE - 72)) | (1L << (DEF - 72)) | (1L << (ID - 72)))) != 0)) { - { - setState(211); - dstatement(); - } - } - - setState(214); - match(RBRACK); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class EmptyContext extends ParserRuleContext { - public TerminalNode SEMICOLON() { return getToken(PainlessParser.SEMICOLON, 0); } - public EmptyContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_empty; } + @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitEmpty(this); - else return visitor.visitChildren(this); + public String getGrammarFileName() { + return "PainlessParser.g4"; } - } - public final EmptyContext empty() throws RecognitionException { - EmptyContext _localctx = new EmptyContext(_ctx, getState()); - enterRule(_localctx, 16, RULE_empty); - try { - enterOuterAlt(_localctx, 1); - { - setState(216); - match(SEMICOLON); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); + @Override + public String[] getRuleNames() { + return ruleNames; } - return _localctx; - } - public static class InitializerContext extends ParserRuleContext { - public DeclarationContext declaration() { - return getRuleContext(DeclarationContext.class,0); - } - public ExpressionContext expression() { - return getRuleContext(ExpressionContext.class,0); - } - public InitializerContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_initializer; } @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitInitializer(this); - else return visitor.visitChildren(this); - } - } - - public final InitializerContext initializer() throws RecognitionException { - InitializerContext _localctx = new InitializerContext(_ctx, getState()); - enterRule(_localctx, 18, RULE_initializer); - try { - setState(220); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,18,_ctx) ) { - case 1: - enterOuterAlt(_localctx, 1); - { - setState(218); - declaration(); - } - break; - case 2: - enterOuterAlt(_localctx, 2); - { - setState(219); - expression(); - } - break; - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class AfterthoughtContext extends ParserRuleContext { - public ExpressionContext expression() { - return getRuleContext(ExpressionContext.class,0); - } - public AfterthoughtContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_afterthought; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitAfterthought(this); - else return visitor.visitChildren(this); + public String getSerializedATN() { + return _serializedATN; } - } - public final AfterthoughtContext afterthought() throws RecognitionException { - AfterthoughtContext _localctx = new AfterthoughtContext(_ctx, getState()); - enterRule(_localctx, 20, RULE_afterthought); - try { - enterOuterAlt(_localctx, 1); - { - setState(222); - expression(); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); + @Override + public ATN getATN() { + return _ATN; } - return _localctx; - } - public static class DeclarationContext extends ParserRuleContext { - public DecltypeContext decltype() { - return getRuleContext(DecltypeContext.class,0); - } - public List declvar() { - return getRuleContexts(DeclvarContext.class); - } - public DeclvarContext declvar(int i) { - return getRuleContext(DeclvarContext.class,i); - } - public List COMMA() { return getTokens(PainlessParser.COMMA); } - public TerminalNode COMMA(int i) { - return getToken(PainlessParser.COMMA, i); - } - public DeclarationContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); + public PainlessParser(TokenStream input) { + super(input); + _interp = new ParserATNSimulator(this, _ATN, _decisionToDFA, _sharedContextCache); } - @Override public int getRuleIndex() { return RULE_declaration; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitDeclaration(this); - else return visitor.visitChildren(this); - } - } - - public final DeclarationContext declaration() throws RecognitionException { - DeclarationContext _localctx = new DeclarationContext(_ctx, getState()); - enterRule(_localctx, 22, RULE_declaration); - int _la; - try { - enterOuterAlt(_localctx, 1); - { - setState(224); - decltype(); - setState(225); - declvar(); - setState(230); - _errHandler.sync(this); - _la = _input.LA(1); - while (_la==COMMA) { - { - { - setState(226); - match(COMMA); - setState(227); - declvar(); - } - } - setState(232); - _errHandler.sync(this); - _la = _input.LA(1); - } - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class DecltypeContext extends ParserRuleContext { - public TypeContext type() { - return getRuleContext(TypeContext.class,0); - } - public List LBRACE() { return getTokens(PainlessParser.LBRACE); } - public TerminalNode LBRACE(int i) { - return getToken(PainlessParser.LBRACE, i); - } - public List RBRACE() { return getTokens(PainlessParser.RBRACE); } - public TerminalNode RBRACE(int i) { - return getToken(PainlessParser.RBRACE, i); - } - public DecltypeContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_decltype; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitDecltype(this); - else return visitor.visitChildren(this); - } - } - - public final DecltypeContext decltype() throws RecognitionException { - DecltypeContext _localctx = new DecltypeContext(_ctx, getState()); - enterRule(_localctx, 24, RULE_decltype); - try { - int _alt; - enterOuterAlt(_localctx, 1); - { - setState(233); - type(); - setState(238); - _errHandler.sync(this); - _alt = getInterpreter().adaptivePredict(_input,20,_ctx); - while ( _alt!=2 && _alt!=org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER ) { - if ( _alt==1 ) { - { - { - setState(234); - match(LBRACE); - setState(235); - match(RBRACE); - } - } - } - setState(240); - _errHandler.sync(this); - _alt = getInterpreter().adaptivePredict(_input,20,_ctx); - } - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class TypeContext extends ParserRuleContext { - public TerminalNode DEF() { return getToken(PainlessParser.DEF, 0); } - public TerminalNode PRIMITIVE() { return getToken(PainlessParser.PRIMITIVE, 0); } - public TerminalNode ID() { return getToken(PainlessParser.ID, 0); } - public List DOT() { return getTokens(PainlessParser.DOT); } - public TerminalNode DOT(int i) { - return getToken(PainlessParser.DOT, i); - } - public List DOTID() { return getTokens(PainlessParser.DOTID); } - public TerminalNode DOTID(int i) { - return getToken(PainlessParser.DOTID, i); - } - public TypeContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_type; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitType(this); - else return visitor.visitChildren(this); - } - } - - public final TypeContext type() throws RecognitionException { - TypeContext _localctx = new TypeContext(_ctx, getState()); - enterRule(_localctx, 26, RULE_type); - try { - int _alt; - setState(251); - switch (_input.LA(1)) { - case DEF: - enterOuterAlt(_localctx, 1); - { - setState(241); - match(DEF); - } - break; - case PRIMITIVE: - enterOuterAlt(_localctx, 2); - { - setState(242); - match(PRIMITIVE); - } - break; - case ID: - enterOuterAlt(_localctx, 3); - { - setState(243); - match(ID); - setState(248); - _errHandler.sync(this); - _alt = getInterpreter().adaptivePredict(_input,21,_ctx); - while ( _alt!=2 && _alt!=org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER ) { - if ( _alt==1 ) { - { - { - setState(244); - match(DOT); - setState(245); - match(DOTID); - } - } - } - setState(250); - _errHandler.sync(this); - _alt = getInterpreter().adaptivePredict(_input,21,_ctx); + + public static class SourceContext extends ParserRuleContext { + public TerminalNode EOF() { + return getToken(PainlessParser.EOF, 0); + } + + public List function() { + return getRuleContexts(FunctionContext.class); } + + public FunctionContext function(int i) { + return getRuleContext(FunctionContext.class, i); } - break; - default: - throw new NoViableAltException(this); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - public static class DeclvarContext extends ParserRuleContext { - public TerminalNode ID() { return getToken(PainlessParser.ID, 0); } - public TerminalNode ASSIGN() { return getToken(PainlessParser.ASSIGN, 0); } - public ExpressionContext expression() { - return getRuleContext(ExpressionContext.class,0); - } - public DeclvarContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_declvar; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitDeclvar(this); - else return visitor.visitChildren(this); - } - } - - public final DeclvarContext declvar() throws RecognitionException { - DeclvarContext _localctx = new DeclvarContext(_ctx, getState()); - enterRule(_localctx, 28, RULE_declvar); - int _la; - try { - enterOuterAlt(_localctx, 1); - { - setState(253); - match(ID); - setState(256); - _la = _input.LA(1); - if (_la==ASSIGN) { - { - setState(254); - match(ASSIGN); - setState(255); - expression(); - } - } - - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class TrapContext extends ParserRuleContext { - public TerminalNode CATCH() { return getToken(PainlessParser.CATCH, 0); } - public TerminalNode LP() { return getToken(PainlessParser.LP, 0); } - public TypeContext type() { - return getRuleContext(TypeContext.class,0); - } - public TerminalNode ID() { return getToken(PainlessParser.ID, 0); } - public TerminalNode RP() { return getToken(PainlessParser.RP, 0); } - public BlockContext block() { - return getRuleContext(BlockContext.class,0); - } - public TrapContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_trap; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitTrap(this); - else return visitor.visitChildren(this); - } - } - - public final TrapContext trap() throws RecognitionException { - TrapContext _localctx = new TrapContext(_ctx, getState()); - enterRule(_localctx, 30, RULE_trap); - try { - enterOuterAlt(_localctx, 1); - { - setState(258); - match(CATCH); - setState(259); - match(LP); - setState(260); - type(); - setState(261); - match(ID); - setState(262); - match(RP); - setState(263); - block(); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class NoncondexpressionContext extends ParserRuleContext { - public NoncondexpressionContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_noncondexpression; } - - public NoncondexpressionContext() { } - public void copyFrom(NoncondexpressionContext ctx) { - super.copyFrom(ctx); - } - } - public static class SingleContext extends NoncondexpressionContext { - public UnaryContext unary() { - return getRuleContext(UnaryContext.class,0); - } - public SingleContext(NoncondexpressionContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitSingle(this); - else return visitor.visitChildren(this); - } - } - public static class CompContext extends NoncondexpressionContext { - public List noncondexpression() { - return getRuleContexts(NoncondexpressionContext.class); - } - public NoncondexpressionContext noncondexpression(int i) { - return getRuleContext(NoncondexpressionContext.class,i); - } - public TerminalNode LT() { return getToken(PainlessParser.LT, 0); } - public TerminalNode LTE() { return getToken(PainlessParser.LTE, 0); } - public TerminalNode GT() { return getToken(PainlessParser.GT, 0); } - public TerminalNode GTE() { return getToken(PainlessParser.GTE, 0); } - public TerminalNode EQ() { return getToken(PainlessParser.EQ, 0); } - public TerminalNode EQR() { return getToken(PainlessParser.EQR, 0); } - public TerminalNode NE() { return getToken(PainlessParser.NE, 0); } - public TerminalNode NER() { return getToken(PainlessParser.NER, 0); } - public CompContext(NoncondexpressionContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitComp(this); - else return visitor.visitChildren(this); - } - } - public static class BoolContext extends NoncondexpressionContext { - public List noncondexpression() { - return getRuleContexts(NoncondexpressionContext.class); - } - public NoncondexpressionContext noncondexpression(int i) { - return getRuleContext(NoncondexpressionContext.class,i); - } - public TerminalNode BOOLAND() { return getToken(PainlessParser.BOOLAND, 0); } - public TerminalNode BOOLOR() { return getToken(PainlessParser.BOOLOR, 0); } - public BoolContext(NoncondexpressionContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitBool(this); - else return visitor.visitChildren(this); - } - } - public static class BinaryContext extends NoncondexpressionContext { - public List noncondexpression() { - return getRuleContexts(NoncondexpressionContext.class); - } - public NoncondexpressionContext noncondexpression(int i) { - return getRuleContext(NoncondexpressionContext.class,i); - } - public TerminalNode MUL() { return getToken(PainlessParser.MUL, 0); } - public TerminalNode DIV() { return getToken(PainlessParser.DIV, 0); } - public TerminalNode REM() { return getToken(PainlessParser.REM, 0); } - public TerminalNode ADD() { return getToken(PainlessParser.ADD, 0); } - public TerminalNode SUB() { return getToken(PainlessParser.SUB, 0); } - public TerminalNode FIND() { return getToken(PainlessParser.FIND, 0); } - public TerminalNode MATCH() { return getToken(PainlessParser.MATCH, 0); } - public TerminalNode LSH() { return getToken(PainlessParser.LSH, 0); } - public TerminalNode RSH() { return getToken(PainlessParser.RSH, 0); } - public TerminalNode USH() { return getToken(PainlessParser.USH, 0); } - public TerminalNode BWAND() { return getToken(PainlessParser.BWAND, 0); } - public TerminalNode XOR() { return getToken(PainlessParser.XOR, 0); } - public TerminalNode BWOR() { return getToken(PainlessParser.BWOR, 0); } - public BinaryContext(NoncondexpressionContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitBinary(this); - else return visitor.visitChildren(this); - } - } - public static class ElvisContext extends NoncondexpressionContext { - public List noncondexpression() { - return getRuleContexts(NoncondexpressionContext.class); - } - public NoncondexpressionContext noncondexpression(int i) { - return getRuleContext(NoncondexpressionContext.class,i); - } - public TerminalNode ELVIS() { return getToken(PainlessParser.ELVIS, 0); } - public ElvisContext(NoncondexpressionContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitElvis(this); - else return visitor.visitChildren(this); - } - } - public static class InstanceofContext extends NoncondexpressionContext { - public NoncondexpressionContext noncondexpression() { - return getRuleContext(NoncondexpressionContext.class,0); - } - public TerminalNode INSTANCEOF() { return getToken(PainlessParser.INSTANCEOF, 0); } - public DecltypeContext decltype() { - return getRuleContext(DecltypeContext.class,0); + public List statement() { + return getRuleContexts(StatementContext.class); + } + + public StatementContext statement(int i) { + return getRuleContext(StatementContext.class, i); + } + + public SourceContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_source; + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitSource(this); + else return visitor.visitChildren(this); + } } - public InstanceofContext(NoncondexpressionContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitInstanceof(this); - else return visitor.visitChildren(this); - } - } - - public final NoncondexpressionContext noncondexpression() throws RecognitionException { - return noncondexpression(0); - } - - private NoncondexpressionContext noncondexpression(int _p) throws RecognitionException { - ParserRuleContext _parentctx = _ctx; - int _parentState = getState(); - NoncondexpressionContext _localctx = new NoncondexpressionContext(_ctx, _parentState); - NoncondexpressionContext _prevctx = _localctx; - int _startState = 32; - enterRecursionRule(_localctx, 32, RULE_noncondexpression, _p); - int _la; - try { - int _alt; - enterOuterAlt(_localctx, 1); - { - { - _localctx = new SingleContext(_localctx); - _ctx = _localctx; - _prevctx = _localctx; - - setState(266); - unary(); - } - _ctx.stop = _input.LT(-1); - setState(309); - _errHandler.sync(this); - _alt = getInterpreter().adaptivePredict(_input,25,_ctx); - while ( _alt!=2 && _alt!=org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER ) { - if ( _alt==1 ) { - if ( _parseListeners!=null ) triggerExitRuleEvent(); - _prevctx = _localctx; - { - setState(307); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,24,_ctx) ) { - case 1: - { - _localctx = new BinaryContext(new NoncondexpressionContext(_parentctx, _parentState)); - pushNewRecursionContext(_localctx, _startState, RULE_noncondexpression); - setState(268); - if (!(precpred(_ctx, 13))) throw new FailedPredicateException(this, "precpred(_ctx, 13)"); - setState(269); - _la = _input.LA(1); - if ( !((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << MUL) | (1L << DIV) | (1L << REM))) != 0)) ) { - _errHandler.recoverInline(this); - } else { - consume(); - } - setState(270); - noncondexpression(14); - } - break; - case 2: - { - _localctx = new BinaryContext(new NoncondexpressionContext(_parentctx, _parentState)); - pushNewRecursionContext(_localctx, _startState, RULE_noncondexpression); - setState(271); - if (!(precpred(_ctx, 12))) throw new FailedPredicateException(this, "precpred(_ctx, 12)"); - setState(272); - _la = _input.LA(1); - if ( !(_la==ADD || _la==SUB) ) { - _errHandler.recoverInline(this); - } else { - consume(); - } - setState(273); - noncondexpression(13); - } - break; - case 3: - { - _localctx = new BinaryContext(new NoncondexpressionContext(_parentctx, _parentState)); - pushNewRecursionContext(_localctx, _startState, RULE_noncondexpression); - setState(274); - if (!(precpred(_ctx, 11))) throw new FailedPredicateException(this, "precpred(_ctx, 11)"); - setState(275); - _la = _input.LA(1); - if ( !(_la==FIND || _la==MATCH) ) { - _errHandler.recoverInline(this); - } else { - consume(); - } - setState(276); - noncondexpression(12); - } - break; - case 4: - { - _localctx = new BinaryContext(new NoncondexpressionContext(_parentctx, _parentState)); - pushNewRecursionContext(_localctx, _startState, RULE_noncondexpression); - setState(277); - if (!(precpred(_ctx, 10))) throw new FailedPredicateException(this, "precpred(_ctx, 10)"); - setState(278); - _la = _input.LA(1); - if ( !((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << LSH) | (1L << RSH) | (1L << USH))) != 0)) ) { - _errHandler.recoverInline(this); - } else { - consume(); - } - setState(279); - noncondexpression(11); - } - break; - case 5: - { - _localctx = new CompContext(new NoncondexpressionContext(_parentctx, _parentState)); - pushNewRecursionContext(_localctx, _startState, RULE_noncondexpression); - setState(280); - if (!(precpred(_ctx, 9))) throw new FailedPredicateException(this, "precpred(_ctx, 9)"); - setState(281); - _la = _input.LA(1); - if ( !((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << LT) | (1L << LTE) | (1L << GT) | (1L << GTE))) != 0)) ) { - _errHandler.recoverInline(this); - } else { - consume(); - } - setState(282); - noncondexpression(10); - } - break; - case 6: - { - _localctx = new CompContext(new NoncondexpressionContext(_parentctx, _parentState)); - pushNewRecursionContext(_localctx, _startState, RULE_noncondexpression); - setState(283); - if (!(precpred(_ctx, 7))) throw new FailedPredicateException(this, "precpred(_ctx, 7)"); - setState(284); - _la = _input.LA(1); - if ( !((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << EQ) | (1L << EQR) | (1L << NE) | (1L << NER))) != 0)) ) { - _errHandler.recoverInline(this); - } else { - consume(); - } - setState(285); - noncondexpression(8); - } - break; - case 7: - { - _localctx = new BinaryContext(new NoncondexpressionContext(_parentctx, _parentState)); - pushNewRecursionContext(_localctx, _startState, RULE_noncondexpression); - setState(286); - if (!(precpred(_ctx, 6))) throw new FailedPredicateException(this, "precpred(_ctx, 6)"); - setState(287); - match(BWAND); - setState(288); - noncondexpression(7); - } - break; - case 8: - { - _localctx = new BinaryContext(new NoncondexpressionContext(_parentctx, _parentState)); - pushNewRecursionContext(_localctx, _startState, RULE_noncondexpression); - setState(289); - if (!(precpred(_ctx, 5))) throw new FailedPredicateException(this, "precpred(_ctx, 5)"); - setState(290); - match(XOR); - setState(291); - noncondexpression(6); - } - break; - case 9: - { - _localctx = new BinaryContext(new NoncondexpressionContext(_parentctx, _parentState)); - pushNewRecursionContext(_localctx, _startState, RULE_noncondexpression); - setState(292); - if (!(precpred(_ctx, 4))) throw new FailedPredicateException(this, "precpred(_ctx, 4)"); - setState(293); - match(BWOR); - setState(294); - noncondexpression(5); - } - break; - case 10: - { - _localctx = new BoolContext(new NoncondexpressionContext(_parentctx, _parentState)); - pushNewRecursionContext(_localctx, _startState, RULE_noncondexpression); - setState(295); - if (!(precpred(_ctx, 3))) throw new FailedPredicateException(this, "precpred(_ctx, 3)"); - setState(296); - match(BOOLAND); - setState(297); - noncondexpression(4); - } - break; - case 11: - { - _localctx = new BoolContext(new NoncondexpressionContext(_parentctx, _parentState)); - pushNewRecursionContext(_localctx, _startState, RULE_noncondexpression); - setState(298); - if (!(precpred(_ctx, 2))) throw new FailedPredicateException(this, "precpred(_ctx, 2)"); - setState(299); - match(BOOLOR); - setState(300); - noncondexpression(3); - } - break; - case 12: - { - _localctx = new ElvisContext(new NoncondexpressionContext(_parentctx, _parentState)); - pushNewRecursionContext(_localctx, _startState, RULE_noncondexpression); - setState(301); - if (!(precpred(_ctx, 1))) throw new FailedPredicateException(this, "precpred(_ctx, 1)"); - setState(302); - match(ELVIS); - setState(303); - noncondexpression(1); - } - break; - case 13: + + public final SourceContext source() throws RecognitionException { + SourceContext _localctx = new SourceContext(_ctx, getState()); + enterRule(_localctx, 0, RULE_source); + int _la; + try { + int _alt; + enterOuterAlt(_localctx, 1); { - _localctx = new InstanceofContext(new NoncondexpressionContext(_parentctx, _parentState)); - pushNewRecursionContext(_localctx, _startState, RULE_noncondexpression); - setState(304); - if (!(precpred(_ctx, 8))) throw new FailedPredicateException(this, "precpred(_ctx, 8)"); - setState(305); - match(INSTANCEOF); - setState(306); - decltype(); + setState(81); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input, 0, _ctx); + while (_alt != 2 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER) { + if (_alt == 1) { + { + { + setState(78); + function(); + } + } + } + setState(83); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input, 0, _ctx); + } + setState(87); + _errHandler.sync(this); + _la = _input.LA(1); + while ((((_la) & ~0x3f) == 0 + && ((1L << _la) & ((1L << LBRACE) | (1L << LP) | (1L << IF) | (1L << WHILE) | (1L << DO) | (1L << FOR) | (1L + << CONTINUE) | (1L << BREAK) | (1L << RETURN) | (1L << NEW) | (1L << TRY) | (1L << THROW) | (1L << BOOLNOT) | (1L + << BWNOT) | (1L << ADD) | (1L << SUB) | (1L << INCR) | (1L << DECR))) != 0) + || ((((_la - 72)) & ~0x3f) == 0 + && ((1L << (_la - 72)) & ((1L << (OCTAL - 72)) | (1L << (HEX - 72)) | (1L << (INTEGER - 72)) | (1L << (DECIMAL + - 72)) | (1L << (STRING - 72)) | (1L << (REGEX - 72)) | (1L << (TRUE - 72)) | (1L << (FALSE - 72)) | (1L + << (NULL - 72)) | (1L << (PRIMITIVE - 72)) | (1L << (DEF - 72)) | (1L << (ID - 72)))) != 0)) { + { + { + setState(84); + statement(); + } + } + setState(89); + _errHandler.sync(this); + _la = _input.LA(1); + } + setState(90); + match(EOF); } - break; - } - } - } - setState(311); - _errHandler.sync(this); - _alt = getInterpreter().adaptivePredict(_input,25,_ctx); - } - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - unrollRecursionContexts(_parentctx); - } - return _localctx; - } - - public static class ExpressionContext extends ParserRuleContext { - public ExpressionContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_expression; } - - public ExpressionContext() { } - public void copyFrom(ExpressionContext ctx) { - super.copyFrom(ctx); - } - } - public static class ConditionalContext extends ExpressionContext { - public NoncondexpressionContext noncondexpression() { - return getRuleContext(NoncondexpressionContext.class,0); - } - public TerminalNode COND() { return getToken(PainlessParser.COND, 0); } - public List expression() { - return getRuleContexts(ExpressionContext.class); - } - public ExpressionContext expression(int i) { - return getRuleContext(ExpressionContext.class,i); - } - public TerminalNode COLON() { return getToken(PainlessParser.COLON, 0); } - public ConditionalContext(ExpressionContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitConditional(this); - else return visitor.visitChildren(this); - } - } - public static class AssignmentContext extends ExpressionContext { - public NoncondexpressionContext noncondexpression() { - return getRuleContext(NoncondexpressionContext.class,0); - } - public ExpressionContext expression() { - return getRuleContext(ExpressionContext.class,0); - } - public TerminalNode ASSIGN() { return getToken(PainlessParser.ASSIGN, 0); } - public TerminalNode AADD() { return getToken(PainlessParser.AADD, 0); } - public TerminalNode ASUB() { return getToken(PainlessParser.ASUB, 0); } - public TerminalNode AMUL() { return getToken(PainlessParser.AMUL, 0); } - public TerminalNode ADIV() { return getToken(PainlessParser.ADIV, 0); } - public TerminalNode AREM() { return getToken(PainlessParser.AREM, 0); } - public TerminalNode AAND() { return getToken(PainlessParser.AAND, 0); } - public TerminalNode AXOR() { return getToken(PainlessParser.AXOR, 0); } - public TerminalNode AOR() { return getToken(PainlessParser.AOR, 0); } - public TerminalNode ALSH() { return getToken(PainlessParser.ALSH, 0); } - public TerminalNode ARSH() { return getToken(PainlessParser.ARSH, 0); } - public TerminalNode AUSH() { return getToken(PainlessParser.AUSH, 0); } - public AssignmentContext(ExpressionContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitAssignment(this); - else return visitor.visitChildren(this); - } - } - public static class NonconditionalContext extends ExpressionContext { - public NoncondexpressionContext noncondexpression() { - return getRuleContext(NoncondexpressionContext.class,0); - } - public NonconditionalContext(ExpressionContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitNonconditional(this); - else return visitor.visitChildren(this); - } - } - - public final ExpressionContext expression() throws RecognitionException { - ExpressionContext _localctx = new ExpressionContext(_ctx, getState()); - enterRule(_localctx, 34, RULE_expression); - int _la; - try { - setState(323); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,26,_ctx) ) { - case 1: - _localctx = new NonconditionalContext(_localctx); - enterOuterAlt(_localctx, 1); - { - setState(312); - noncondexpression(0); - } - break; - case 2: - _localctx = new ConditionalContext(_localctx); - enterOuterAlt(_localctx, 2); - { - setState(313); - noncondexpression(0); - setState(314); - match(COND); - setState(315); - expression(); - setState(316); - match(COLON); - setState(317); - expression(); - } - break; - case 3: - _localctx = new AssignmentContext(_localctx); - enterOuterAlt(_localctx, 3); - { - setState(319); - noncondexpression(0); - setState(320); - _la = _input.LA(1); - if ( !(((((_la - 60)) & ~0x3f) == 0 && ((1L << (_la - 60)) & ((1L << (ASSIGN - 60)) | (1L << (AADD - 60)) | (1L << (ASUB - 60)) | (1L << (AMUL - 60)) | (1L << (ADIV - 60)) | (1L << (AREM - 60)) | (1L << (AAND - 60)) | (1L << (AXOR - 60)) | (1L << (AOR - 60)) | (1L << (ALSH - 60)) | (1L << (ARSH - 60)) | (1L << (AUSH - 60)))) != 0)) ) { - _errHandler.recoverInline(this); - } else { - consume(); - } - setState(321); - expression(); - } - break; - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class UnaryContext extends ParserRuleContext { - public UnaryContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_unary; } - - public UnaryContext() { } - public void copyFrom(UnaryContext ctx) { - super.copyFrom(ctx); - } - } - public static class NotaddsubContext extends UnaryContext { - public UnarynotaddsubContext unarynotaddsub() { - return getRuleContext(UnarynotaddsubContext.class,0); - } - public NotaddsubContext(UnaryContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitNotaddsub(this); - else return visitor.visitChildren(this); - } - } - public static class PreContext extends UnaryContext { - public ChainContext chain() { - return getRuleContext(ChainContext.class,0); - } - public TerminalNode INCR() { return getToken(PainlessParser.INCR, 0); } - public TerminalNode DECR() { return getToken(PainlessParser.DECR, 0); } - public PreContext(UnaryContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitPre(this); - else return visitor.visitChildren(this); - } - } - public static class AddsubContext extends UnaryContext { - public UnaryContext unary() { - return getRuleContext(UnaryContext.class,0); - } - public TerminalNode ADD() { return getToken(PainlessParser.ADD, 0); } - public TerminalNode SUB() { return getToken(PainlessParser.SUB, 0); } - public AddsubContext(UnaryContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitAddsub(this); - else return visitor.visitChildren(this); - } - } - - public final UnaryContext unary() throws RecognitionException { - UnaryContext _localctx = new UnaryContext(_ctx, getState()); - enterRule(_localctx, 36, RULE_unary); - int _la; - try { - setState(330); - switch (_input.LA(1)) { - case INCR: - case DECR: - _localctx = new PreContext(_localctx); - enterOuterAlt(_localctx, 1); - { - setState(325); - _la = _input.LA(1); - if ( !(_la==INCR || _la==DECR) ) { - _errHandler.recoverInline(this); - } else { - consume(); - } - setState(326); - chain(); - } - break; - case ADD: - case SUB: - _localctx = new AddsubContext(_localctx); - enterOuterAlt(_localctx, 2); - { - setState(327); - _la = _input.LA(1); - if ( !(_la==ADD || _la==SUB) ) { - _errHandler.recoverInline(this); - } else { - consume(); - } - setState(328); - unary(); - } - break; - case LBRACE: - case LP: - case NEW: - case BOOLNOT: - case BWNOT: - case OCTAL: - case HEX: - case INTEGER: - case DECIMAL: - case STRING: - case REGEX: - case TRUE: - case FALSE: - case NULL: - case ID: - _localctx = new NotaddsubContext(_localctx); - enterOuterAlt(_localctx, 3); - { - setState(329); - unarynotaddsub(); - } - break; - default: - throw new NoViableAltException(this); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class UnarynotaddsubContext extends ParserRuleContext { - public UnarynotaddsubContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_unarynotaddsub; } - - public UnarynotaddsubContext() { } - public void copyFrom(UnarynotaddsubContext ctx) { - super.copyFrom(ctx); - } - } - public static class CastContext extends UnarynotaddsubContext { - public CastexpressionContext castexpression() { - return getRuleContext(CastexpressionContext.class,0); - } - public CastContext(UnarynotaddsubContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitCast(this); - else return visitor.visitChildren(this); - } - } - public static class NotContext extends UnarynotaddsubContext { - public UnaryContext unary() { - return getRuleContext(UnaryContext.class,0); - } - public TerminalNode BOOLNOT() { return getToken(PainlessParser.BOOLNOT, 0); } - public TerminalNode BWNOT() { return getToken(PainlessParser.BWNOT, 0); } - public NotContext(UnarynotaddsubContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitNot(this); - else return visitor.visitChildren(this); - } - } - public static class ReadContext extends UnarynotaddsubContext { - public ChainContext chain() { - return getRuleContext(ChainContext.class,0); - } - public ReadContext(UnarynotaddsubContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitRead(this); - else return visitor.visitChildren(this); - } - } - public static class PostContext extends UnarynotaddsubContext { - public ChainContext chain() { - return getRuleContext(ChainContext.class,0); - } - public TerminalNode INCR() { return getToken(PainlessParser.INCR, 0); } - public TerminalNode DECR() { return getToken(PainlessParser.DECR, 0); } - public PostContext(UnarynotaddsubContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitPost(this); - else return visitor.visitChildren(this); - } - } - - public final UnarynotaddsubContext unarynotaddsub() throws RecognitionException { - UnarynotaddsubContext _localctx = new UnarynotaddsubContext(_ctx, getState()); - enterRule(_localctx, 38, RULE_unarynotaddsub); - int _la; - try { - setState(339); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,28,_ctx) ) { - case 1: - _localctx = new ReadContext(_localctx); - enterOuterAlt(_localctx, 1); - { - setState(332); - chain(); - } - break; - case 2: - _localctx = new PostContext(_localctx); - enterOuterAlt(_localctx, 2); - { - setState(333); - chain(); - setState(334); - _la = _input.LA(1); - if ( !(_la==INCR || _la==DECR) ) { - _errHandler.recoverInline(this); - } else { - consume(); - } - } - break; - case 3: - _localctx = new NotContext(_localctx); - enterOuterAlt(_localctx, 3); - { - setState(336); - _la = _input.LA(1); - if ( !(_la==BOOLNOT || _la==BWNOT) ) { - _errHandler.recoverInline(this); - } else { - consume(); - } - setState(337); - unary(); - } - break; - case 4: - _localctx = new CastContext(_localctx); - enterOuterAlt(_localctx, 4); - { - setState(338); - castexpression(); - } - break; - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class CastexpressionContext extends ParserRuleContext { - public CastexpressionContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_castexpression; } - - public CastexpressionContext() { } - public void copyFrom(CastexpressionContext ctx) { - super.copyFrom(ctx); - } - } - public static class RefcastContext extends CastexpressionContext { - public TerminalNode LP() { return getToken(PainlessParser.LP, 0); } - public RefcasttypeContext refcasttype() { - return getRuleContext(RefcasttypeContext.class,0); - } - public TerminalNode RP() { return getToken(PainlessParser.RP, 0); } - public UnarynotaddsubContext unarynotaddsub() { - return getRuleContext(UnarynotaddsubContext.class,0); - } - public RefcastContext(CastexpressionContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitRefcast(this); - else return visitor.visitChildren(this); - } - } - public static class PrimordefcastContext extends CastexpressionContext { - public TerminalNode LP() { return getToken(PainlessParser.LP, 0); } - public PrimordefcasttypeContext primordefcasttype() { - return getRuleContext(PrimordefcasttypeContext.class,0); - } - public TerminalNode RP() { return getToken(PainlessParser.RP, 0); } - public UnaryContext unary() { - return getRuleContext(UnaryContext.class,0); - } - public PrimordefcastContext(CastexpressionContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitPrimordefcast(this); - else return visitor.visitChildren(this); - } - } - - public final CastexpressionContext castexpression() throws RecognitionException { - CastexpressionContext _localctx = new CastexpressionContext(_ctx, getState()); - enterRule(_localctx, 40, RULE_castexpression); - try { - setState(351); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,29,_ctx) ) { - case 1: - _localctx = new PrimordefcastContext(_localctx); - enterOuterAlt(_localctx, 1); - { - setState(341); - match(LP); - setState(342); - primordefcasttype(); - setState(343); - match(RP); - setState(344); - unary(); - } - break; - case 2: - _localctx = new RefcastContext(_localctx); - enterOuterAlt(_localctx, 2); - { - setState(346); - match(LP); - setState(347); - refcasttype(); - setState(348); - match(RP); - setState(349); - unarynotaddsub(); - } - break; - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class PrimordefcasttypeContext extends ParserRuleContext { - public TerminalNode DEF() { return getToken(PainlessParser.DEF, 0); } - public TerminalNode PRIMITIVE() { return getToken(PainlessParser.PRIMITIVE, 0); } - public PrimordefcasttypeContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_primordefcasttype; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitPrimordefcasttype(this); - else return visitor.visitChildren(this); - } - } - - public final PrimordefcasttypeContext primordefcasttype() throws RecognitionException { - PrimordefcasttypeContext _localctx = new PrimordefcasttypeContext(_ctx, getState()); - enterRule(_localctx, 42, RULE_primordefcasttype); - int _la; - try { - enterOuterAlt(_localctx, 1); - { - setState(353); - _la = _input.LA(1); - if ( !(_la==PRIMITIVE || _la==DEF) ) { - _errHandler.recoverInline(this); - } else { - consume(); - } - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class RefcasttypeContext extends ParserRuleContext { - public TerminalNode DEF() { return getToken(PainlessParser.DEF, 0); } - public List LBRACE() { return getTokens(PainlessParser.LBRACE); } - public TerminalNode LBRACE(int i) { - return getToken(PainlessParser.LBRACE, i); - } - public List RBRACE() { return getTokens(PainlessParser.RBRACE); } - public TerminalNode RBRACE(int i) { - return getToken(PainlessParser.RBRACE, i); - } - public TerminalNode PRIMITIVE() { return getToken(PainlessParser.PRIMITIVE, 0); } - public TerminalNode ID() { return getToken(PainlessParser.ID, 0); } - public List DOT() { return getTokens(PainlessParser.DOT); } - public TerminalNode DOT(int i) { - return getToken(PainlessParser.DOT, i); - } - public List DOTID() { return getTokens(PainlessParser.DOTID); } - public TerminalNode DOTID(int i) { - return getToken(PainlessParser.DOTID, i); - } - public RefcasttypeContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_refcasttype; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitRefcasttype(this); - else return visitor.visitChildren(this); - } - } - - public final RefcasttypeContext refcasttype() throws RecognitionException { - RefcasttypeContext _localctx = new RefcasttypeContext(_ctx, getState()); - enterRule(_localctx, 44, RULE_refcasttype); - int _la; - try { - setState(384); - switch (_input.LA(1)) { - case DEF: - enterOuterAlt(_localctx, 1); - { - setState(355); - match(DEF); - setState(358); - _errHandler.sync(this); - _la = _input.LA(1); - do { - { - { - setState(356); - match(LBRACE); - setState(357); - match(RBRACE); - } - } - setState(360); - _errHandler.sync(this); - _la = _input.LA(1); - } while ( _la==LBRACE ); - } - break; - case PRIMITIVE: - enterOuterAlt(_localctx, 2); - { - setState(362); - match(PRIMITIVE); - setState(365); - _errHandler.sync(this); - _la = _input.LA(1); - do { - { - { - setState(363); - match(LBRACE); - setState(364); - match(RBRACE); - } - } - setState(367); - _errHandler.sync(this); - _la = _input.LA(1); - } while ( _la==LBRACE ); - } - break; - case ID: - enterOuterAlt(_localctx, 3); - { - setState(369); - match(ID); - setState(374); - _errHandler.sync(this); - _la = _input.LA(1); - while (_la==DOT) { - { - { - setState(370); - match(DOT); - setState(371); - match(DOTID); - } - } - setState(376); - _errHandler.sync(this); - _la = _input.LA(1); - } - setState(381); - _errHandler.sync(this); - _la = _input.LA(1); - while (_la==LBRACE) { - { - { - setState(377); - match(LBRACE); - setState(378); - match(RBRACE); - } - } - setState(383); - _errHandler.sync(this); - _la = _input.LA(1); - } - } - break; - default: - throw new NoViableAltException(this); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class ChainContext extends ParserRuleContext { - public ChainContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_chain; } - - public ChainContext() { } - public void copyFrom(ChainContext ctx) { - super.copyFrom(ctx); - } - } - public static class DynamicContext extends ChainContext { - public PrimaryContext primary() { - return getRuleContext(PrimaryContext.class,0); - } - public List postfix() { - return getRuleContexts(PostfixContext.class); - } - public PostfixContext postfix(int i) { - return getRuleContext(PostfixContext.class,i); - } - public DynamicContext(ChainContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitDynamic(this); - else return visitor.visitChildren(this); + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - } - public static class NewarrayContext extends ChainContext { - public ArrayinitializerContext arrayinitializer() { - return getRuleContext(ArrayinitializerContext.class,0); + + public static class FunctionContext extends ParserRuleContext { + public DecltypeContext decltype() { + return getRuleContext(DecltypeContext.class, 0); + } + + public TerminalNode ID() { + return getToken(PainlessParser.ID, 0); + } + + public ParametersContext parameters() { + return getRuleContext(ParametersContext.class, 0); + } + + public BlockContext block() { + return getRuleContext(BlockContext.class, 0); + } + + public FunctionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_function; + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitFunction(this); + else return visitor.visitChildren(this); + } } - public NewarrayContext(ChainContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitNewarray(this); - else return visitor.visitChildren(this); - } - } - - public final ChainContext chain() throws RecognitionException { - ChainContext _localctx = new ChainContext(_ctx, getState()); - enterRule(_localctx, 46, RULE_chain); - try { - int _alt; - setState(394); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,36,_ctx) ) { - case 1: - _localctx = new DynamicContext(_localctx); - enterOuterAlt(_localctx, 1); - { - setState(386); - primary(); - setState(390); - _errHandler.sync(this); - _alt = getInterpreter().adaptivePredict(_input,35,_ctx); - while ( _alt!=2 && _alt!=org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER ) { - if ( _alt==1 ) { - { + + public final FunctionContext function() throws RecognitionException { + FunctionContext _localctx = new FunctionContext(_ctx, getState()); + enterRule(_localctx, 2, RULE_function); + try { + enterOuterAlt(_localctx, 1); { - setState(387); - postfix(); + setState(92); + decltype(); + setState(93); + match(ID); + setState(94); + parameters(); + setState(95); + block(); } - } - } - setState(392); - _errHandler.sync(this); - _alt = getInterpreter().adaptivePredict(_input,35,_ctx); - } - } - break; - case 2: - _localctx = new NewarrayContext(_localctx); - enterOuterAlt(_localctx, 2); - { - setState(393); - arrayinitializer(); - } - break; - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class PrimaryContext extends ParserRuleContext { - public PrimaryContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_primary; } - - public PrimaryContext() { } - public void copyFrom(PrimaryContext ctx) { - super.copyFrom(ctx); - } - } - public static class ListinitContext extends PrimaryContext { - public ListinitializerContext listinitializer() { - return getRuleContext(ListinitializerContext.class,0); - } - public ListinitContext(PrimaryContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitListinit(this); - else return visitor.visitChildren(this); - } - } - public static class RegexContext extends PrimaryContext { - public TerminalNode REGEX() { return getToken(PainlessParser.REGEX, 0); } - public RegexContext(PrimaryContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitRegex(this); - else return visitor.visitChildren(this); - } - } - public static class NullContext extends PrimaryContext { - public TerminalNode NULL() { return getToken(PainlessParser.NULL, 0); } - public NullContext(PrimaryContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitNull(this); - else return visitor.visitChildren(this); - } - } - public static class StringContext extends PrimaryContext { - public TerminalNode STRING() { return getToken(PainlessParser.STRING, 0); } - public StringContext(PrimaryContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitString(this); - else return visitor.visitChildren(this); - } - } - public static class MapinitContext extends PrimaryContext { - public MapinitializerContext mapinitializer() { - return getRuleContext(MapinitializerContext.class,0); - } - public MapinitContext(PrimaryContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitMapinit(this); - else return visitor.visitChildren(this); - } - } - public static class CalllocalContext extends PrimaryContext { - public TerminalNode ID() { return getToken(PainlessParser.ID, 0); } - public ArgumentsContext arguments() { - return getRuleContext(ArgumentsContext.class,0); - } - public CalllocalContext(PrimaryContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitCalllocal(this); - else return visitor.visitChildren(this); - } - } - public static class TrueContext extends PrimaryContext { - public TerminalNode TRUE() { return getToken(PainlessParser.TRUE, 0); } - public TrueContext(PrimaryContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitTrue(this); - else return visitor.visitChildren(this); - } - } - public static class FalseContext extends PrimaryContext { - public TerminalNode FALSE() { return getToken(PainlessParser.FALSE, 0); } - public FalseContext(PrimaryContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitFalse(this); - else return visitor.visitChildren(this); - } - } - public static class VariableContext extends PrimaryContext { - public TerminalNode ID() { return getToken(PainlessParser.ID, 0); } - public VariableContext(PrimaryContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitVariable(this); - else return visitor.visitChildren(this); - } - } - public static class NumericContext extends PrimaryContext { - public TerminalNode OCTAL() { return getToken(PainlessParser.OCTAL, 0); } - public TerminalNode HEX() { return getToken(PainlessParser.HEX, 0); } - public TerminalNode INTEGER() { return getToken(PainlessParser.INTEGER, 0); } - public TerminalNode DECIMAL() { return getToken(PainlessParser.DECIMAL, 0); } - public NumericContext(PrimaryContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitNumeric(this); - else return visitor.visitChildren(this); - } - } - public static class NewobjectContext extends PrimaryContext { - public TerminalNode NEW() { return getToken(PainlessParser.NEW, 0); } - public TypeContext type() { - return getRuleContext(TypeContext.class,0); - } - public ArgumentsContext arguments() { - return getRuleContext(ArgumentsContext.class,0); - } - public NewobjectContext(PrimaryContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitNewobject(this); - else return visitor.visitChildren(this); - } - } - public static class PrecedenceContext extends PrimaryContext { - public TerminalNode LP() { return getToken(PainlessParser.LP, 0); } - public ExpressionContext expression() { - return getRuleContext(ExpressionContext.class,0); - } - public TerminalNode RP() { return getToken(PainlessParser.RP, 0); } - public PrecedenceContext(PrimaryContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitPrecedence(this); - else return visitor.visitChildren(this); - } - } - - public final PrimaryContext primary() throws RecognitionException { - PrimaryContext _localctx = new PrimaryContext(_ctx, getState()); - enterRule(_localctx, 48, RULE_primary); - int _la; - try { - setState(415); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,37,_ctx) ) { - case 1: - _localctx = new PrecedenceContext(_localctx); - enterOuterAlt(_localctx, 1); - { - setState(396); - match(LP); - setState(397); - expression(); - setState(398); - match(RP); - } - break; - case 2: - _localctx = new NumericContext(_localctx); - enterOuterAlt(_localctx, 2); - { - setState(400); - _la = _input.LA(1); - if ( !(((((_la - 72)) & ~0x3f) == 0 && ((1L << (_la - 72)) & ((1L << (OCTAL - 72)) | (1L << (HEX - 72)) | (1L << (INTEGER - 72)) | (1L << (DECIMAL - 72)))) != 0)) ) { - _errHandler.recoverInline(this); - } else { - consume(); - } - } - break; - case 3: - _localctx = new TrueContext(_localctx); - enterOuterAlt(_localctx, 3); - { - setState(401); - match(TRUE); - } - break; - case 4: - _localctx = new FalseContext(_localctx); - enterOuterAlt(_localctx, 4); - { - setState(402); - match(FALSE); - } - break; - case 5: - _localctx = new NullContext(_localctx); - enterOuterAlt(_localctx, 5); - { - setState(403); - match(NULL); - } - break; - case 6: - _localctx = new StringContext(_localctx); - enterOuterAlt(_localctx, 6); - { - setState(404); - match(STRING); - } - break; - case 7: - _localctx = new RegexContext(_localctx); - enterOuterAlt(_localctx, 7); - { - setState(405); - match(REGEX); - } - break; - case 8: - _localctx = new ListinitContext(_localctx); - enterOuterAlt(_localctx, 8); - { - setState(406); - listinitializer(); - } - break; - case 9: - _localctx = new MapinitContext(_localctx); - enterOuterAlt(_localctx, 9); - { - setState(407); - mapinitializer(); - } - break; - case 10: - _localctx = new VariableContext(_localctx); - enterOuterAlt(_localctx, 10); - { - setState(408); - match(ID); - } - break; - case 11: - _localctx = new CalllocalContext(_localctx); - enterOuterAlt(_localctx, 11); - { - setState(409); - match(ID); - setState(410); - arguments(); - } - break; - case 12: - _localctx = new NewobjectContext(_localctx); - enterOuterAlt(_localctx, 12); - { - setState(411); - match(NEW); - setState(412); - type(); - setState(413); - arguments(); - } - break; - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class PostfixContext extends ParserRuleContext { - public CallinvokeContext callinvoke() { - return getRuleContext(CallinvokeContext.class,0); - } - public FieldaccessContext fieldaccess() { - return getRuleContext(FieldaccessContext.class,0); - } - public BraceaccessContext braceaccess() { - return getRuleContext(BraceaccessContext.class,0); - } - public PostfixContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_postfix; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitPostfix(this); - else return visitor.visitChildren(this); - } - } - - public final PostfixContext postfix() throws RecognitionException { - PostfixContext _localctx = new PostfixContext(_ctx, getState()); - enterRule(_localctx, 50, RULE_postfix); - try { - setState(420); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,38,_ctx) ) { - case 1: - enterOuterAlt(_localctx, 1); - { - setState(417); - callinvoke(); - } - break; - case 2: - enterOuterAlt(_localctx, 2); - { - setState(418); - fieldaccess(); - } - break; - case 3: - enterOuterAlt(_localctx, 3); - { - setState(419); - braceaccess(); - } - break; - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class PostdotContext extends ParserRuleContext { - public CallinvokeContext callinvoke() { - return getRuleContext(CallinvokeContext.class,0); - } - public FieldaccessContext fieldaccess() { - return getRuleContext(FieldaccessContext.class,0); - } - public PostdotContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_postdot; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitPostdot(this); - else return visitor.visitChildren(this); - } - } - - public final PostdotContext postdot() throws RecognitionException { - PostdotContext _localctx = new PostdotContext(_ctx, getState()); - enterRule(_localctx, 52, RULE_postdot); - try { - setState(424); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,39,_ctx) ) { - case 1: - enterOuterAlt(_localctx, 1); - { - setState(422); - callinvoke(); - } - break; - case 2: - enterOuterAlt(_localctx, 2); - { - setState(423); - fieldaccess(); - } - break; - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class CallinvokeContext extends ParserRuleContext { - public TerminalNode DOTID() { return getToken(PainlessParser.DOTID, 0); } - public ArgumentsContext arguments() { - return getRuleContext(ArgumentsContext.class,0); - } - public TerminalNode DOT() { return getToken(PainlessParser.DOT, 0); } - public TerminalNode NSDOT() { return getToken(PainlessParser.NSDOT, 0); } - public CallinvokeContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_callinvoke; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitCallinvoke(this); - else return visitor.visitChildren(this); - } - } - - public final CallinvokeContext callinvoke() throws RecognitionException { - CallinvokeContext _localctx = new CallinvokeContext(_ctx, getState()); - enterRule(_localctx, 54, RULE_callinvoke); - int _la; - try { - enterOuterAlt(_localctx, 1); - { - setState(426); - _la = _input.LA(1); - if ( !(_la==DOT || _la==NSDOT) ) { - _errHandler.recoverInline(this); - } else { - consume(); - } - setState(427); - match(DOTID); - setState(428); - arguments(); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class FieldaccessContext extends ParserRuleContext { - public TerminalNode DOT() { return getToken(PainlessParser.DOT, 0); } - public TerminalNode NSDOT() { return getToken(PainlessParser.NSDOT, 0); } - public TerminalNode DOTID() { return getToken(PainlessParser.DOTID, 0); } - public TerminalNode DOTINTEGER() { return getToken(PainlessParser.DOTINTEGER, 0); } - public FieldaccessContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_fieldaccess; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitFieldaccess(this); - else return visitor.visitChildren(this); - } - } - - public final FieldaccessContext fieldaccess() throws RecognitionException { - FieldaccessContext _localctx = new FieldaccessContext(_ctx, getState()); - enterRule(_localctx, 56, RULE_fieldaccess); - int _la; - try { - enterOuterAlt(_localctx, 1); - { - setState(430); - _la = _input.LA(1); - if ( !(_la==DOT || _la==NSDOT) ) { - _errHandler.recoverInline(this); - } else { - consume(); - } - setState(431); - _la = _input.LA(1); - if ( !(_la==DOTINTEGER || _la==DOTID) ) { - _errHandler.recoverInline(this); - } else { - consume(); - } - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class BraceaccessContext extends ParserRuleContext { - public TerminalNode LBRACE() { return getToken(PainlessParser.LBRACE, 0); } - public ExpressionContext expression() { - return getRuleContext(ExpressionContext.class,0); - } - public TerminalNode RBRACE() { return getToken(PainlessParser.RBRACE, 0); } - public BraceaccessContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_braceaccess; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitBraceaccess(this); - else return visitor.visitChildren(this); - } - } - - public final BraceaccessContext braceaccess() throws RecognitionException { - BraceaccessContext _localctx = new BraceaccessContext(_ctx, getState()); - enterRule(_localctx, 58, RULE_braceaccess); - try { - enterOuterAlt(_localctx, 1); - { - setState(433); - match(LBRACE); - setState(434); - expression(); - setState(435); - match(RBRACE); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class ArrayinitializerContext extends ParserRuleContext { - public ArrayinitializerContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_arrayinitializer; } - - public ArrayinitializerContext() { } - public void copyFrom(ArrayinitializerContext ctx) { - super.copyFrom(ctx); - } - } - public static class NewstandardarrayContext extends ArrayinitializerContext { - public TerminalNode NEW() { return getToken(PainlessParser.NEW, 0); } - public TypeContext type() { - return getRuleContext(TypeContext.class,0); - } - public List LBRACE() { return getTokens(PainlessParser.LBRACE); } - public TerminalNode LBRACE(int i) { - return getToken(PainlessParser.LBRACE, i); - } - public List expression() { - return getRuleContexts(ExpressionContext.class); - } - public ExpressionContext expression(int i) { - return getRuleContext(ExpressionContext.class,i); - } - public List RBRACE() { return getTokens(PainlessParser.RBRACE); } - public TerminalNode RBRACE(int i) { - return getToken(PainlessParser.RBRACE, i); - } - public PostdotContext postdot() { - return getRuleContext(PostdotContext.class,0); - } - public List postfix() { - return getRuleContexts(PostfixContext.class); - } - public PostfixContext postfix(int i) { - return getRuleContext(PostfixContext.class,i); - } - public NewstandardarrayContext(ArrayinitializerContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitNewstandardarray(this); - else return visitor.visitChildren(this); - } - } - public static class NewinitializedarrayContext extends ArrayinitializerContext { - public TerminalNode NEW() { return getToken(PainlessParser.NEW, 0); } - public TypeContext type() { - return getRuleContext(TypeContext.class,0); - } - public TerminalNode LBRACE() { return getToken(PainlessParser.LBRACE, 0); } - public TerminalNode RBRACE() { return getToken(PainlessParser.RBRACE, 0); } - public TerminalNode LBRACK() { return getToken(PainlessParser.LBRACK, 0); } - public TerminalNode RBRACK() { return getToken(PainlessParser.RBRACK, 0); } - public List expression() { - return getRuleContexts(ExpressionContext.class); - } - public ExpressionContext expression(int i) { - return getRuleContext(ExpressionContext.class,i); - } - public List postfix() { - return getRuleContexts(PostfixContext.class); - } - public PostfixContext postfix(int i) { - return getRuleContext(PostfixContext.class,i); + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - public List COMMA() { return getTokens(PainlessParser.COMMA); } - public TerminalNode COMMA(int i) { - return getToken(PainlessParser.COMMA, i); + + public static class ParametersContext extends ParserRuleContext { + public TerminalNode LP() { + return getToken(PainlessParser.LP, 0); + } + + public TerminalNode RP() { + return getToken(PainlessParser.RP, 0); + } + + public List decltype() { + return getRuleContexts(DecltypeContext.class); + } + + public DecltypeContext decltype(int i) { + return getRuleContext(DecltypeContext.class, i); + } + + public List ID() { + return getTokens(PainlessParser.ID); + } + + public TerminalNode ID(int i) { + return getToken(PainlessParser.ID, i); + } + + public List COMMA() { + return getTokens(PainlessParser.COMMA); + } + + public TerminalNode COMMA(int i) { + return getToken(PainlessParser.COMMA, i); + } + + public ParametersContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_parameters; + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitParameters(this); + else return visitor.visitChildren(this); + } } - public NewinitializedarrayContext(ArrayinitializerContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitNewinitializedarray(this); - else return visitor.visitChildren(this); - } - } - - public final ArrayinitializerContext arrayinitializer() throws RecognitionException { - ArrayinitializerContext _localctx = new ArrayinitializerContext(_ctx, getState()); - enterRule(_localctx, 60, RULE_arrayinitializer); - int _la; - try { - int _alt; - setState(478); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,46,_ctx) ) { - case 1: - _localctx = new NewstandardarrayContext(_localctx); - enterOuterAlt(_localctx, 1); - { - setState(437); - match(NEW); - setState(438); - type(); - setState(443); - _errHandler.sync(this); - _alt = 1; - do { - switch (_alt) { - case 1: - { - { - setState(439); - match(LBRACE); - setState(440); - expression(); - setState(441); - match(RBRACE); - } - } - break; - default: - throw new NoViableAltException(this); - } - setState(445); - _errHandler.sync(this); - _alt = getInterpreter().adaptivePredict(_input,40,_ctx); - } while ( _alt!=2 && _alt!=org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER ); - setState(454); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,42,_ctx) ) { - case 1: - { - setState(447); - postdot(); - setState(451); - _errHandler.sync(this); - _alt = getInterpreter().adaptivePredict(_input,41,_ctx); - while ( _alt!=2 && _alt!=org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER ) { - if ( _alt==1 ) { - { - { - setState(448); - postfix(); - } - } - } - setState(453); - _errHandler.sync(this); - _alt = getInterpreter().adaptivePredict(_input,41,_ctx); - } - } - break; - } - } - break; - case 2: - _localctx = new NewinitializedarrayContext(_localctx); - enterOuterAlt(_localctx, 2); - { - setState(456); - match(NEW); - setState(457); - type(); - setState(458); - match(LBRACE); - setState(459); - match(RBRACE); - setState(460); - match(LBRACK); - setState(469); - _la = _input.LA(1); - if ((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << LBRACE) | (1L << LP) | (1L << NEW) | (1L << BOOLNOT) | (1L << BWNOT) | (1L << ADD) | (1L << SUB) | (1L << INCR) | (1L << DECR))) != 0) || ((((_la - 72)) & ~0x3f) == 0 && ((1L << (_la - 72)) & ((1L << (OCTAL - 72)) | (1L << (HEX - 72)) | (1L << (INTEGER - 72)) | (1L << (DECIMAL - 72)) | (1L << (STRING - 72)) | (1L << (REGEX - 72)) | (1L << (TRUE - 72)) | (1L << (FALSE - 72)) | (1L << (NULL - 72)) | (1L << (ID - 72)))) != 0)) { - { - setState(461); - expression(); - setState(466); - _errHandler.sync(this); - _la = _input.LA(1); - while (_la==COMMA) { - { - { - setState(462); - match(COMMA); - setState(463); - expression(); - } - } - setState(468); - _errHandler.sync(this); - _la = _input.LA(1); - } - } - } - - setState(471); - match(RBRACK); - setState(475); - _errHandler.sync(this); - _alt = getInterpreter().adaptivePredict(_input,45,_ctx); - while ( _alt!=2 && _alt!=org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER ) { - if ( _alt==1 ) { - { + + public final ParametersContext parameters() throws RecognitionException { + ParametersContext _localctx = new ParametersContext(_ctx, getState()); + enterRule(_localctx, 4, RULE_parameters); + int _la; + try { + enterOuterAlt(_localctx, 1); { - setState(472); - postfix(); + setState(97); + match(LP); + setState(109); + _la = _input.LA(1); + if (((((_la - 81)) & ~0x3f) == 0 + && ((1L << (_la - 81)) & ((1L << (PRIMITIVE - 81)) | (1L << (DEF - 81)) | (1L << (ID - 81)))) != 0)) { + { + setState(98); + decltype(); + setState(99); + match(ID); + setState(106); + _errHandler.sync(this); + _la = _input.LA(1); + while (_la == COMMA) { + { + { + setState(100); + match(COMMA); + setState(101); + decltype(); + setState(102); + match(ID); + } + } + setState(108); + _errHandler.sync(this); + _la = _input.LA(1); + } + } + } + + setState(111); + match(RP); } - } - } - setState(477); - _errHandler.sync(this); - _alt = getInterpreter().adaptivePredict(_input,45,_ctx); - } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); } - break; - } + return _localctx; } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - public static class ListinitializerContext extends ParserRuleContext { - public TerminalNode LBRACE() { return getToken(PainlessParser.LBRACE, 0); } - public List expression() { - return getRuleContexts(ExpressionContext.class); - } - public ExpressionContext expression(int i) { - return getRuleContext(ExpressionContext.class,i); - } - public TerminalNode RBRACE() { return getToken(PainlessParser.RBRACE, 0); } - public List COMMA() { return getTokens(PainlessParser.COMMA); } - public TerminalNode COMMA(int i) { - return getToken(PainlessParser.COMMA, i); + public static class StatementContext extends ParserRuleContext { + public RstatementContext rstatement() { + return getRuleContext(RstatementContext.class, 0); + } + + public DstatementContext dstatement() { + return getRuleContext(DstatementContext.class, 0); + } + + public TerminalNode SEMICOLON() { + return getToken(PainlessParser.SEMICOLON, 0); + } + + public TerminalNode EOF() { + return getToken(PainlessParser.EOF, 0); + } + + public StatementContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_statement; + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitStatement(this); + else return visitor.visitChildren(this); + } } - public ListinitializerContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); + + public final StatementContext statement() throws RecognitionException { + StatementContext _localctx = new StatementContext(_ctx, getState()); + enterRule(_localctx, 6, RULE_statement); + int _la; + try { + setState(117); + switch (_input.LA(1)) { + case IF: + case WHILE: + case FOR: + case TRY: + enterOuterAlt(_localctx, 1); { + setState(113); + rstatement(); + } + break; + case LBRACE: + case LP: + case DO: + case CONTINUE: + case BREAK: + case RETURN: + case NEW: + case THROW: + case BOOLNOT: + case BWNOT: + case ADD: + case SUB: + case INCR: + case DECR: + case OCTAL: + case HEX: + case INTEGER: + case DECIMAL: + case STRING: + case REGEX: + case TRUE: + case FALSE: + case NULL: + case PRIMITIVE: + case DEF: + case ID: + enterOuterAlt(_localctx, 2); { + setState(114); + dstatement(); + setState(115); + _la = _input.LA(1); + if (!(_la == EOF || _la == SEMICOLON)) { + _errHandler.recoverInline(this); + } else { + consume(); + } + } + break; + default: + throw new NoViableAltException(this); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override public int getRuleIndex() { return RULE_listinitializer; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitListinitializer(this); - else return visitor.visitChildren(this); - } - } - - public final ListinitializerContext listinitializer() throws RecognitionException { - ListinitializerContext _localctx = new ListinitializerContext(_ctx, getState()); - enterRule(_localctx, 62, RULE_listinitializer); - int _la; - try { - setState(493); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,48,_ctx) ) { - case 1: - enterOuterAlt(_localctx, 1); - { - setState(480); - match(LBRACE); - setState(481); - expression(); - setState(486); - _errHandler.sync(this); - _la = _input.LA(1); - while (_la==COMMA) { - { - { - setState(482); - match(COMMA); - setState(483); - expression(); - } - } - setState(488); - _errHandler.sync(this); - _la = _input.LA(1); - } - setState(489); - match(RBRACE); - } - break; - case 2: - enterOuterAlt(_localctx, 2); - { - setState(491); - match(LBRACE); - setState(492); - match(RBRACE); - } - break; - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class MapinitializerContext extends ParserRuleContext { - public TerminalNode LBRACE() { return getToken(PainlessParser.LBRACE, 0); } - public List maptoken() { - return getRuleContexts(MaptokenContext.class); - } - public MaptokenContext maptoken(int i) { - return getRuleContext(MaptokenContext.class,i); - } - public TerminalNode RBRACE() { return getToken(PainlessParser.RBRACE, 0); } - public List COMMA() { return getTokens(PainlessParser.COMMA); } - public TerminalNode COMMA(int i) { - return getToken(PainlessParser.COMMA, i); - } - public TerminalNode COLON() { return getToken(PainlessParser.COLON, 0); } - public MapinitializerContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_mapinitializer; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitMapinitializer(this); - else return visitor.visitChildren(this); - } - } - - public final MapinitializerContext mapinitializer() throws RecognitionException { - MapinitializerContext _localctx = new MapinitializerContext(_ctx, getState()); - enterRule(_localctx, 64, RULE_mapinitializer); - int _la; - try { - setState(509); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,50,_ctx) ) { - case 1: - enterOuterAlt(_localctx, 1); - { - setState(495); - match(LBRACE); - setState(496); - maptoken(); - setState(501); - _errHandler.sync(this); - _la = _input.LA(1); - while (_la==COMMA) { - { - { - setState(497); - match(COMMA); - setState(498); - maptoken(); - } - } - setState(503); - _errHandler.sync(this); - _la = _input.LA(1); - } - setState(504); - match(RBRACE); - } - break; - case 2: - enterOuterAlt(_localctx, 2); - { - setState(506); - match(LBRACE); - setState(507); - match(COLON); - setState(508); - match(RBRACE); - } - break; - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class MaptokenContext extends ParserRuleContext { - public List expression() { - return getRuleContexts(ExpressionContext.class); - } - public ExpressionContext expression(int i) { - return getRuleContext(ExpressionContext.class,i); - } - public TerminalNode COLON() { return getToken(PainlessParser.COLON, 0); } - public MaptokenContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_maptoken; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitMaptoken(this); - else return visitor.visitChildren(this); - } - } - - public final MaptokenContext maptoken() throws RecognitionException { - MaptokenContext _localctx = new MaptokenContext(_ctx, getState()); - enterRule(_localctx, 66, RULE_maptoken); - try { - enterOuterAlt(_localctx, 1); - { - setState(511); - expression(); - setState(512); - match(COLON); - setState(513); - expression(); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class ArgumentsContext extends ParserRuleContext { - public TerminalNode LP() { return getToken(PainlessParser.LP, 0); } - public TerminalNode RP() { return getToken(PainlessParser.RP, 0); } - public List argument() { - return getRuleContexts(ArgumentContext.class); - } - public ArgumentContext argument(int i) { - return getRuleContext(ArgumentContext.class,i); - } - public List COMMA() { return getTokens(PainlessParser.COMMA); } - public TerminalNode COMMA(int i) { - return getToken(PainlessParser.COMMA, i); - } - public ArgumentsContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_arguments; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitArguments(this); - else return visitor.visitChildren(this); - } - } - - public final ArgumentsContext arguments() throws RecognitionException { - ArgumentsContext _localctx = new ArgumentsContext(_ctx, getState()); - enterRule(_localctx, 68, RULE_arguments); - int _la; - try { - enterOuterAlt(_localctx, 1); - { - { - setState(515); - match(LP); - setState(524); - _la = _input.LA(1); - if ((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << LBRACE) | (1L << LP) | (1L << NEW) | (1L << THIS) | (1L << BOOLNOT) | (1L << BWNOT) | (1L << ADD) | (1L << SUB) | (1L << INCR) | (1L << DECR))) != 0) || ((((_la - 72)) & ~0x3f) == 0 && ((1L << (_la - 72)) & ((1L << (OCTAL - 72)) | (1L << (HEX - 72)) | (1L << (INTEGER - 72)) | (1L << (DECIMAL - 72)) | (1L << (STRING - 72)) | (1L << (REGEX - 72)) | (1L << (TRUE - 72)) | (1L << (FALSE - 72)) | (1L << (NULL - 72)) | (1L << (PRIMITIVE - 72)) | (1L << (DEF - 72)) | (1L << (ID - 72)))) != 0)) { - { - setState(516); - argument(); - setState(521); - _errHandler.sync(this); - _la = _input.LA(1); - while (_la==COMMA) { - { - { - setState(517); - match(COMMA); - setState(518); - argument(); - } - } - setState(523); - _errHandler.sync(this); - _la = _input.LA(1); - } - } - } - - setState(526); - match(RP); - } - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class ArgumentContext extends ParserRuleContext { - public ExpressionContext expression() { - return getRuleContext(ExpressionContext.class,0); - } - public LambdaContext lambda() { - return getRuleContext(LambdaContext.class,0); - } - public FuncrefContext funcref() { - return getRuleContext(FuncrefContext.class,0); - } - public ArgumentContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_argument; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitArgument(this); - else return visitor.visitChildren(this); - } - } - - public final ArgumentContext argument() throws RecognitionException { - ArgumentContext _localctx = new ArgumentContext(_ctx, getState()); - enterRule(_localctx, 70, RULE_argument); - try { - setState(531); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,53,_ctx) ) { - case 1: - enterOuterAlt(_localctx, 1); - { - setState(528); - expression(); - } - break; - case 2: - enterOuterAlt(_localctx, 2); - { - setState(529); - lambda(); - } - break; - case 3: - enterOuterAlt(_localctx, 3); - { - setState(530); - funcref(); - } - break; - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class LambdaContext extends ParserRuleContext { - public TerminalNode ARROW() { return getToken(PainlessParser.ARROW, 0); } - public List lamtype() { - return getRuleContexts(LamtypeContext.class); - } - public LamtypeContext lamtype(int i) { - return getRuleContext(LamtypeContext.class,i); - } - public TerminalNode LP() { return getToken(PainlessParser.LP, 0); } - public TerminalNode RP() { return getToken(PainlessParser.RP, 0); } - public BlockContext block() { - return getRuleContext(BlockContext.class,0); - } - public ExpressionContext expression() { - return getRuleContext(ExpressionContext.class,0); - } - public List COMMA() { return getTokens(PainlessParser.COMMA); } - public TerminalNode COMMA(int i) { - return getToken(PainlessParser.COMMA, i); - } - public LambdaContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_lambda; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitLambda(this); - else return visitor.visitChildren(this); - } - } - - public final LambdaContext lambda() throws RecognitionException { - LambdaContext _localctx = new LambdaContext(_ctx, getState()); - enterRule(_localctx, 72, RULE_lambda); - int _la; - try { - enterOuterAlt(_localctx, 1); - { - setState(546); - switch (_input.LA(1)) { - case PRIMITIVE: - case DEF: - case ID: - { - setState(533); - lamtype(); - } - break; - case LP: - { - setState(534); - match(LP); - setState(543); - _la = _input.LA(1); - if (((((_la - 81)) & ~0x3f) == 0 && ((1L << (_la - 81)) & ((1L << (PRIMITIVE - 81)) | (1L << (DEF - 81)) | (1L << (ID - 81)))) != 0)) { - { - setState(535); - lamtype(); - setState(540); - _errHandler.sync(this); - _la = _input.LA(1); - while (_la==COMMA) { + + public static class RstatementContext extends ParserRuleContext { + public RstatementContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_rstatement; + } + + public RstatementContext() {} + + public void copyFrom(RstatementContext ctx) { + super.copyFrom(ctx); + } + } + + public static class ForContext extends RstatementContext { + public TerminalNode FOR() { + return getToken(PainlessParser.FOR, 0); + } + + public TerminalNode LP() { + return getToken(PainlessParser.LP, 0); + } + + public List SEMICOLON() { + return getTokens(PainlessParser.SEMICOLON); + } + + public TerminalNode SEMICOLON(int i) { + return getToken(PainlessParser.SEMICOLON, i); + } + + public TerminalNode RP() { + return getToken(PainlessParser.RP, 0); + } + + public TrailerContext trailer() { + return getRuleContext(TrailerContext.class, 0); + } + + public EmptyContext empty() { + return getRuleContext(EmptyContext.class, 0); + } + + public InitializerContext initializer() { + return getRuleContext(InitializerContext.class, 0); + } + + public ExpressionContext expression() { + return getRuleContext(ExpressionContext.class, 0); + } + + public AfterthoughtContext afterthought() { + return getRuleContext(AfterthoughtContext.class, 0); + } + + public ForContext(RstatementContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitFor(this); + else return visitor.visitChildren(this); + } + } + + public static class TryContext extends RstatementContext { + public TerminalNode TRY() { + return getToken(PainlessParser.TRY, 0); + } + + public BlockContext block() { + return getRuleContext(BlockContext.class, 0); + } + + public List trap() { + return getRuleContexts(TrapContext.class); + } + + public TrapContext trap(int i) { + return getRuleContext(TrapContext.class, i); + } + + public TryContext(RstatementContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitTry(this); + else return visitor.visitChildren(this); + } + } + + public static class WhileContext extends RstatementContext { + public TerminalNode WHILE() { + return getToken(PainlessParser.WHILE, 0); + } + + public TerminalNode LP() { + return getToken(PainlessParser.LP, 0); + } + + public ExpressionContext expression() { + return getRuleContext(ExpressionContext.class, 0); + } + + public TerminalNode RP() { + return getToken(PainlessParser.RP, 0); + } + + public TrailerContext trailer() { + return getRuleContext(TrailerContext.class, 0); + } + + public EmptyContext empty() { + return getRuleContext(EmptyContext.class, 0); + } + + public WhileContext(RstatementContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitWhile(this); + else return visitor.visitChildren(this); + } + } + + public static class IneachContext extends RstatementContext { + public TerminalNode FOR() { + return getToken(PainlessParser.FOR, 0); + } + + public TerminalNode LP() { + return getToken(PainlessParser.LP, 0); + } + + public TerminalNode ID() { + return getToken(PainlessParser.ID, 0); + } + + public TerminalNode IN() { + return getToken(PainlessParser.IN, 0); + } + + public ExpressionContext expression() { + return getRuleContext(ExpressionContext.class, 0); + } + + public TerminalNode RP() { + return getToken(PainlessParser.RP, 0); + } + + public TrailerContext trailer() { + return getRuleContext(TrailerContext.class, 0); + } + + public IneachContext(RstatementContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitIneach(this); + else return visitor.visitChildren(this); + } + } + + public static class IfContext extends RstatementContext { + public TerminalNode IF() { + return getToken(PainlessParser.IF, 0); + } + + public TerminalNode LP() { + return getToken(PainlessParser.LP, 0); + } + + public ExpressionContext expression() { + return getRuleContext(ExpressionContext.class, 0); + } + + public TerminalNode RP() { + return getToken(PainlessParser.RP, 0); + } + + public List trailer() { + return getRuleContexts(TrailerContext.class); + } + + public TrailerContext trailer(int i) { + return getRuleContext(TrailerContext.class, i); + } + + public TerminalNode ELSE() { + return getToken(PainlessParser.ELSE, 0); + } + + public IfContext(RstatementContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitIf(this); + else return visitor.visitChildren(this); + } + } + + public static class EachContext extends RstatementContext { + public TerminalNode FOR() { + return getToken(PainlessParser.FOR, 0); + } + + public TerminalNode LP() { + return getToken(PainlessParser.LP, 0); + } + + public DecltypeContext decltype() { + return getRuleContext(DecltypeContext.class, 0); + } + + public TerminalNode ID() { + return getToken(PainlessParser.ID, 0); + } + + public TerminalNode COLON() { + return getToken(PainlessParser.COLON, 0); + } + + public ExpressionContext expression() { + return getRuleContext(ExpressionContext.class, 0); + } + + public TerminalNode RP() { + return getToken(PainlessParser.RP, 0); + } + + public TrailerContext trailer() { + return getRuleContext(TrailerContext.class, 0); + } + + public EachContext(RstatementContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitEach(this); + else return visitor.visitChildren(this); + } + } + + public final RstatementContext rstatement() throws RecognitionException { + RstatementContext _localctx = new RstatementContext(_ctx, getState()); + enterRule(_localctx, 8, RULE_rstatement); + int _la; + try { + int _alt; + setState(179); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 12, _ctx)) { + case 1: + _localctx = new IfContext(_localctx); + enterOuterAlt(_localctx, 1); { + setState(119); + match(IF); + setState(120); + match(LP); + setState(121); + expression(); + setState(122); + match(RP); + setState(123); + trailer(); + setState(127); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 5, _ctx)) { + case 1: { + setState(124); + match(ELSE); + setState(125); + trailer(); + } + break; + case 2: { + setState(126); + if (!(_input.LA(1) != ELSE)) throw new FailedPredicateException(this, " _input.LA(1) != ELSE "); + } + break; + } + } + break; + case 2: + _localctx = new WhileContext(_localctx); + enterOuterAlt(_localctx, 2); { + setState(129); + match(WHILE); + setState(130); + match(LP); + setState(131); + expression(); + setState(132); + match(RP); + setState(135); + switch (_input.LA(1)) { + case LBRACK: + case LBRACE: + case LP: + case IF: + case WHILE: + case DO: + case FOR: + case CONTINUE: + case BREAK: + case RETURN: + case NEW: + case TRY: + case THROW: + case BOOLNOT: + case BWNOT: + case ADD: + case SUB: + case INCR: + case DECR: + case OCTAL: + case HEX: + case INTEGER: + case DECIMAL: + case STRING: + case REGEX: + case TRUE: + case FALSE: + case NULL: + case PRIMITIVE: + case DEF: + case ID: { + setState(133); + trailer(); + } + break; + case SEMICOLON: { + setState(134); + empty(); + } + break; + default: + throw new NoViableAltException(this); + } + } + break; + case 3: + _localctx = new ForContext(_localctx); + enterOuterAlt(_localctx, 3); { + setState(137); + match(FOR); + setState(138); + match(LP); + setState(140); + _la = _input.LA(1); + if ((((_la) & ~0x3f) == 0 + && ((1L << _la) & ((1L << LBRACE) | (1L << LP) | (1L << NEW) | (1L << BOOLNOT) | (1L << BWNOT) | (1L << ADD) | (1L + << SUB) | (1L << INCR) | (1L << DECR))) != 0) + || ((((_la - 72)) & ~0x3f) == 0 + && ((1L << (_la - 72)) & ((1L << (OCTAL - 72)) | (1L << (HEX - 72)) | (1L << (INTEGER - 72)) | (1L << (DECIMAL + - 72)) | (1L << (STRING - 72)) | (1L << (REGEX - 72)) | (1L << (TRUE - 72)) | (1L << (FALSE - 72)) | (1L + << (NULL - 72)) | (1L << (PRIMITIVE - 72)) | (1L << (DEF - 72)) | (1L << (ID - 72)))) != 0)) { + { + setState(139); + initializer(); + } + } + + setState(142); + match(SEMICOLON); + setState(144); + _la = _input.LA(1); + if ((((_la) & ~0x3f) == 0 + && ((1L << _la) & ((1L << LBRACE) | (1L << LP) | (1L << NEW) | (1L << BOOLNOT) | (1L << BWNOT) | (1L << ADD) | (1L + << SUB) | (1L << INCR) | (1L << DECR))) != 0) + || ((((_la - 72)) & ~0x3f) == 0 + && ((1L << (_la - 72)) & ((1L << (OCTAL - 72)) | (1L << (HEX - 72)) | (1L << (INTEGER - 72)) | (1L << (DECIMAL + - 72)) | (1L << (STRING - 72)) | (1L << (REGEX - 72)) | (1L << (TRUE - 72)) | (1L << (FALSE - 72)) | (1L + << (NULL - 72)) | (1L << (ID - 72)))) != 0)) { + { + setState(143); + expression(); + } + } + + setState(146); + match(SEMICOLON); + setState(148); + _la = _input.LA(1); + if ((((_la) & ~0x3f) == 0 + && ((1L << _la) & ((1L << LBRACE) | (1L << LP) | (1L << NEW) | (1L << BOOLNOT) | (1L << BWNOT) | (1L << ADD) | (1L + << SUB) | (1L << INCR) | (1L << DECR))) != 0) + || ((((_la - 72)) & ~0x3f) == 0 + && ((1L << (_la - 72)) & ((1L << (OCTAL - 72)) | (1L << (HEX - 72)) | (1L << (INTEGER - 72)) | (1L << (DECIMAL + - 72)) | (1L << (STRING - 72)) | (1L << (REGEX - 72)) | (1L << (TRUE - 72)) | (1L << (FALSE - 72)) | (1L + << (NULL - 72)) | (1L << (ID - 72)))) != 0)) { + { + setState(147); + afterthought(); + } + } + + setState(150); + match(RP); + setState(153); + switch (_input.LA(1)) { + case LBRACK: + case LBRACE: + case LP: + case IF: + case WHILE: + case DO: + case FOR: + case CONTINUE: + case BREAK: + case RETURN: + case NEW: + case TRY: + case THROW: + case BOOLNOT: + case BWNOT: + case ADD: + case SUB: + case INCR: + case DECR: + case OCTAL: + case HEX: + case INTEGER: + case DECIMAL: + case STRING: + case REGEX: + case TRUE: + case FALSE: + case NULL: + case PRIMITIVE: + case DEF: + case ID: { + setState(151); + trailer(); + } + break; + case SEMICOLON: { + setState(152); + empty(); + } + break; + default: + throw new NoViableAltException(this); + } + } + break; + case 4: + _localctx = new EachContext(_localctx); + enterOuterAlt(_localctx, 4); { + setState(155); + match(FOR); + setState(156); + match(LP); + setState(157); + decltype(); + setState(158); + match(ID); + setState(159); + match(COLON); + setState(160); + expression(); + setState(161); + match(RP); + setState(162); + trailer(); + } + break; + case 5: + _localctx = new IneachContext(_localctx); + enterOuterAlt(_localctx, 5); { + setState(164); + match(FOR); + setState(165); + match(LP); + setState(166); + match(ID); + setState(167); + match(IN); + setState(168); + expression(); + setState(169); + match(RP); + setState(170); + trailer(); + } + break; + case 6: + _localctx = new TryContext(_localctx); + enterOuterAlt(_localctx, 6); { + setState(172); + match(TRY); + setState(173); + block(); + setState(175); + _errHandler.sync(this); + _alt = 1; + do { + switch (_alt) { + case 1: { + { + setState(174); + trap(); + } + } + break; + default: + throw new NoViableAltException(this); + } + setState(177); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input, 11, _ctx); + } while (_alt != 2 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER); + } + break; + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; + } + + public static class DstatementContext extends ParserRuleContext { + public DstatementContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_dstatement; + } + + public DstatementContext() {} + + public void copyFrom(DstatementContext ctx) { + super.copyFrom(ctx); + } + } + + public static class DeclContext extends DstatementContext { + public DeclarationContext declaration() { + return getRuleContext(DeclarationContext.class, 0); + } + + public DeclContext(DstatementContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitDecl(this); + else return visitor.visitChildren(this); + } + } + + public static class BreakContext extends DstatementContext { + public TerminalNode BREAK() { + return getToken(PainlessParser.BREAK, 0); + } + + public BreakContext(DstatementContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitBreak(this); + else return visitor.visitChildren(this); + } + } + + public static class ThrowContext extends DstatementContext { + public TerminalNode THROW() { + return getToken(PainlessParser.THROW, 0); + } + + public ExpressionContext expression() { + return getRuleContext(ExpressionContext.class, 0); + } + + public ThrowContext(DstatementContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitThrow(this); + else return visitor.visitChildren(this); + } + } + + public static class ContinueContext extends DstatementContext { + public TerminalNode CONTINUE() { + return getToken(PainlessParser.CONTINUE, 0); + } + + public ContinueContext(DstatementContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitContinue(this); + else return visitor.visitChildren(this); + } + } + + public static class ExprContext extends DstatementContext { + public ExpressionContext expression() { + return getRuleContext(ExpressionContext.class, 0); + } + + public ExprContext(DstatementContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitExpr(this); + else return visitor.visitChildren(this); + } + } + + public static class DoContext extends DstatementContext { + public TerminalNode DO() { + return getToken(PainlessParser.DO, 0); + } + + public BlockContext block() { + return getRuleContext(BlockContext.class, 0); + } + + public TerminalNode WHILE() { + return getToken(PainlessParser.WHILE, 0); + } + + public TerminalNode LP() { + return getToken(PainlessParser.LP, 0); + } + + public ExpressionContext expression() { + return getRuleContext(ExpressionContext.class, 0); + } + + public TerminalNode RP() { + return getToken(PainlessParser.RP, 0); + } + + public DoContext(DstatementContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitDo(this); + else return visitor.visitChildren(this); + } + } + + public static class ReturnContext extends DstatementContext { + public TerminalNode RETURN() { + return getToken(PainlessParser.RETURN, 0); + } + + public ExpressionContext expression() { + return getRuleContext(ExpressionContext.class, 0); + } + + public ReturnContext(DstatementContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitReturn(this); + else return visitor.visitChildren(this); + } + } + + public final DstatementContext dstatement() throws RecognitionException { + DstatementContext _localctx = new DstatementContext(_ctx, getState()); + enterRule(_localctx, 10, RULE_dstatement); + int _la; + try { + setState(198); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 14, _ctx)) { + case 1: + _localctx = new DoContext(_localctx); + enterOuterAlt(_localctx, 1); { + setState(181); + match(DO); + setState(182); + block(); + setState(183); + match(WHILE); + setState(184); + match(LP); + setState(185); + expression(); + setState(186); + match(RP); + } + break; + case 2: + _localctx = new DeclContext(_localctx); + enterOuterAlt(_localctx, 2); { + setState(188); + declaration(); + } + break; + case 3: + _localctx = new ContinueContext(_localctx); + enterOuterAlt(_localctx, 3); { + setState(189); + match(CONTINUE); + } + break; + case 4: + _localctx = new BreakContext(_localctx); + enterOuterAlt(_localctx, 4); { + setState(190); + match(BREAK); + } + break; + case 5: + _localctx = new ReturnContext(_localctx); + enterOuterAlt(_localctx, 5); { + setState(191); + match(RETURN); + setState(193); + _la = _input.LA(1); + if ((((_la) & ~0x3f) == 0 + && ((1L << _la) & ((1L << LBRACE) | (1L << LP) | (1L << NEW) | (1L << BOOLNOT) | (1L << BWNOT) | (1L << ADD) | (1L + << SUB) | (1L << INCR) | (1L << DECR))) != 0) + || ((((_la - 72)) & ~0x3f) == 0 + && ((1L << (_la - 72)) & ((1L << (OCTAL - 72)) | (1L << (HEX - 72)) | (1L << (INTEGER - 72)) | (1L << (DECIMAL + - 72)) | (1L << (STRING - 72)) | (1L << (REGEX - 72)) | (1L << (TRUE - 72)) | (1L << (FALSE - 72)) | (1L + << (NULL - 72)) | (1L << (ID - 72)))) != 0)) { + { + setState(192); + expression(); + } + } + + } + break; + case 6: + _localctx = new ThrowContext(_localctx); + enterOuterAlt(_localctx, 6); { + setState(195); + match(THROW); + setState(196); + expression(); + } + break; + case 7: + _localctx = new ExprContext(_localctx); + enterOuterAlt(_localctx, 7); { + setState(197); + expression(); + } + break; + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; + } + + public static class TrailerContext extends ParserRuleContext { + public BlockContext block() { + return getRuleContext(BlockContext.class, 0); + } + + public StatementContext statement() { + return getRuleContext(StatementContext.class, 0); + } + + public TrailerContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_trailer; + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitTrailer(this); + else return visitor.visitChildren(this); + } + } + + public final TrailerContext trailer() throws RecognitionException { + TrailerContext _localctx = new TrailerContext(_ctx, getState()); + enterRule(_localctx, 12, RULE_trailer); + try { + setState(202); + switch (_input.LA(1)) { + case LBRACK: + enterOuterAlt(_localctx, 1); { + setState(200); + block(); + } + break; + case LBRACE: + case LP: + case IF: + case WHILE: + case DO: + case FOR: + case CONTINUE: + case BREAK: + case RETURN: + case NEW: + case TRY: + case THROW: + case BOOLNOT: + case BWNOT: + case ADD: + case SUB: + case INCR: + case DECR: + case OCTAL: + case HEX: + case INTEGER: + case DECIMAL: + case STRING: + case REGEX: + case TRUE: + case FALSE: + case NULL: + case PRIMITIVE: + case DEF: + case ID: + enterOuterAlt(_localctx, 2); { + setState(201); + statement(); + } + break; + default: + throw new NoViableAltException(this); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; + } + + public static class BlockContext extends ParserRuleContext { + public TerminalNode LBRACK() { + return getToken(PainlessParser.LBRACK, 0); + } + + public TerminalNode RBRACK() { + return getToken(PainlessParser.RBRACK, 0); + } + + public List statement() { + return getRuleContexts(StatementContext.class); + } + + public StatementContext statement(int i) { + return getRuleContext(StatementContext.class, i); + } + + public DstatementContext dstatement() { + return getRuleContext(DstatementContext.class, 0); + } + + public BlockContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_block; + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitBlock(this); + else return visitor.visitChildren(this); + } + } + + public final BlockContext block() throws RecognitionException { + BlockContext _localctx = new BlockContext(_ctx, getState()); + enterRule(_localctx, 14, RULE_block); + int _la; + try { + int _alt; + enterOuterAlt(_localctx, 1); + { + setState(204); + match(LBRACK); + setState(208); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input, 16, _ctx); + while (_alt != 2 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER) { + if (_alt == 1) { + { + { + setState(205); + statement(); + } + } + } + setState(210); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input, 16, _ctx); + } + setState(212); + _la = _input.LA(1); + if ((((_la) & ~0x3f) == 0 + && ((1L << _la) & ((1L << LBRACE) | (1L << LP) | (1L << DO) | (1L << CONTINUE) | (1L << BREAK) | (1L << RETURN) | (1L + << NEW) | (1L << THROW) | (1L << BOOLNOT) | (1L << BWNOT) | (1L << ADD) | (1L << SUB) | (1L << INCR) | (1L + << DECR))) != 0) + || ((((_la - 72)) & ~0x3f) == 0 + && ((1L << (_la - 72)) & ((1L << (OCTAL - 72)) | (1L << (HEX - 72)) | (1L << (INTEGER - 72)) | (1L << (DECIMAL + - 72)) | (1L << (STRING - 72)) | (1L << (REGEX - 72)) | (1L << (TRUE - 72)) | (1L << (FALSE - 72)) | (1L + << (NULL - 72)) | (1L << (PRIMITIVE - 72)) | (1L << (DEF - 72)) | (1L << (ID - 72)))) != 0)) { + { + setState(211); + dstatement(); + } + } + + setState(214); + match(RBRACK); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; + } + + public static class EmptyContext extends ParserRuleContext { + public TerminalNode SEMICOLON() { + return getToken(PainlessParser.SEMICOLON, 0); + } + + public EmptyContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_empty; + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitEmpty(this); + else return visitor.visitChildren(this); + } + } + + public final EmptyContext empty() throws RecognitionException { + EmptyContext _localctx = new EmptyContext(_ctx, getState()); + enterRule(_localctx, 16, RULE_empty); + try { + enterOuterAlt(_localctx, 1); + { + setState(216); + match(SEMICOLON); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; + } + + public static class InitializerContext extends ParserRuleContext { + public DeclarationContext declaration() { + return getRuleContext(DeclarationContext.class, 0); + } + + public ExpressionContext expression() { + return getRuleContext(ExpressionContext.class, 0); + } + + public InitializerContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_initializer; + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitInitializer(this); + else return visitor.visitChildren(this); + } + } + + public final InitializerContext initializer() throws RecognitionException { + InitializerContext _localctx = new InitializerContext(_ctx, getState()); + enterRule(_localctx, 18, RULE_initializer); + try { + setState(220); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 18, _ctx)) { + case 1: + enterOuterAlt(_localctx, 1); { + setState(218); + declaration(); + } + break; + case 2: + enterOuterAlt(_localctx, 2); { + setState(219); + expression(); + } + break; + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; + } + + public static class AfterthoughtContext extends ParserRuleContext { + public ExpressionContext expression() { + return getRuleContext(ExpressionContext.class, 0); + } + + public AfterthoughtContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_afterthought; + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitAfterthought(this); + else return visitor.visitChildren(this); + } + } + + public final AfterthoughtContext afterthought() throws RecognitionException { + AfterthoughtContext _localctx = new AfterthoughtContext(_ctx, getState()); + enterRule(_localctx, 20, RULE_afterthought); + try { + enterOuterAlt(_localctx, 1); + { + setState(222); + expression(); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; + } + + public static class DeclarationContext extends ParserRuleContext { + public DecltypeContext decltype() { + return getRuleContext(DecltypeContext.class, 0); + } + + public List declvar() { + return getRuleContexts(DeclvarContext.class); + } + + public DeclvarContext declvar(int i) { + return getRuleContext(DeclvarContext.class, i); + } + + public List COMMA() { + return getTokens(PainlessParser.COMMA); + } + + public TerminalNode COMMA(int i) { + return getToken(PainlessParser.COMMA, i); + } + + public DeclarationContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_declaration; + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitDeclaration(this); + else return visitor.visitChildren(this); + } + } + + public final DeclarationContext declaration() throws RecognitionException { + DeclarationContext _localctx = new DeclarationContext(_ctx, getState()); + enterRule(_localctx, 22, RULE_declaration); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(224); + decltype(); + setState(225); + declvar(); + setState(230); + _errHandler.sync(this); + _la = _input.LA(1); + while (_la == COMMA) { + { + { + setState(226); + match(COMMA); + setState(227); + declvar(); + } + } + setState(232); + _errHandler.sync(this); + _la = _input.LA(1); + } + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; + } + + public static class DecltypeContext extends ParserRuleContext { + public TypeContext type() { + return getRuleContext(TypeContext.class, 0); + } + + public List LBRACE() { + return getTokens(PainlessParser.LBRACE); + } + + public TerminalNode LBRACE(int i) { + return getToken(PainlessParser.LBRACE, i); + } + + public List RBRACE() { + return getTokens(PainlessParser.RBRACE); + } + + public TerminalNode RBRACE(int i) { + return getToken(PainlessParser.RBRACE, i); + } + + public DecltypeContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_decltype; + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitDecltype(this); + else return visitor.visitChildren(this); + } + } + + public final DecltypeContext decltype() throws RecognitionException { + DecltypeContext _localctx = new DecltypeContext(_ctx, getState()); + enterRule(_localctx, 24, RULE_decltype); + try { + int _alt; + enterOuterAlt(_localctx, 1); + { + setState(233); + type(); + setState(238); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input, 20, _ctx); + while (_alt != 2 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER) { + if (_alt == 1) { + { + { + setState(234); + match(LBRACE); + setState(235); + match(RBRACE); + } + } + } + setState(240); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input, 20, _ctx); + } + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; + } + + public static class TypeContext extends ParserRuleContext { + public TerminalNode DEF() { + return getToken(PainlessParser.DEF, 0); + } + + public TerminalNode PRIMITIVE() { + return getToken(PainlessParser.PRIMITIVE, 0); + } + + public TerminalNode ID() { + return getToken(PainlessParser.ID, 0); + } + + public List DOT() { + return getTokens(PainlessParser.DOT); + } + + public TerminalNode DOT(int i) { + return getToken(PainlessParser.DOT, i); + } + + public List DOTID() { + return getTokens(PainlessParser.DOTID); + } + + public TerminalNode DOTID(int i) { + return getToken(PainlessParser.DOTID, i); + } + + public TypeContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_type; + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitType(this); + else return visitor.visitChildren(this); + } + } + + public final TypeContext type() throws RecognitionException { + TypeContext _localctx = new TypeContext(_ctx, getState()); + enterRule(_localctx, 26, RULE_type); + try { + int _alt; + setState(251); + switch (_input.LA(1)) { + case DEF: + enterOuterAlt(_localctx, 1); { + setState(241); + match(DEF); + } + break; + case PRIMITIVE: + enterOuterAlt(_localctx, 2); { + setState(242); + match(PRIMITIVE); + } + break; + case ID: + enterOuterAlt(_localctx, 3); { + setState(243); + match(ID); + setState(248); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input, 21, _ctx); + while (_alt != 2 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER) { + if (_alt == 1) { + { + { + setState(244); + match(DOT); + setState(245); + match(DOTID); + } + } + } + setState(250); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input, 21, _ctx); + } + } + break; + default: + throw new NoViableAltException(this); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; + } + + public static class DeclvarContext extends ParserRuleContext { + public TerminalNode ID() { + return getToken(PainlessParser.ID, 0); + } + + public TerminalNode ASSIGN() { + return getToken(PainlessParser.ASSIGN, 0); + } + + public ExpressionContext expression() { + return getRuleContext(ExpressionContext.class, 0); + } + + public DeclvarContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_declvar; + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitDeclvar(this); + else return visitor.visitChildren(this); + } + } + + public final DeclvarContext declvar() throws RecognitionException { + DeclvarContext _localctx = new DeclvarContext(_ctx, getState()); + enterRule(_localctx, 28, RULE_declvar); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(253); + match(ID); + setState(256); + _la = _input.LA(1); + if (_la == ASSIGN) { + { + setState(254); + match(ASSIGN); + setState(255); + expression(); + } + } + + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; + } + + public static class TrapContext extends ParserRuleContext { + public TerminalNode CATCH() { + return getToken(PainlessParser.CATCH, 0); + } + + public TerminalNode LP() { + return getToken(PainlessParser.LP, 0); + } + + public TypeContext type() { + return getRuleContext(TypeContext.class, 0); + } + + public TerminalNode ID() { + return getToken(PainlessParser.ID, 0); + } + + public TerminalNode RP() { + return getToken(PainlessParser.RP, 0); + } + + public BlockContext block() { + return getRuleContext(BlockContext.class, 0); + } + + public TrapContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_trap; + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitTrap(this); + else return visitor.visitChildren(this); + } + } + + public final TrapContext trap() throws RecognitionException { + TrapContext _localctx = new TrapContext(_ctx, getState()); + enterRule(_localctx, 30, RULE_trap); + try { + enterOuterAlt(_localctx, 1); + { + setState(258); + match(CATCH); + setState(259); + match(LP); + setState(260); + type(); + setState(261); + match(ID); + setState(262); + match(RP); + setState(263); + block(); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; + } + + public static class NoncondexpressionContext extends ParserRuleContext { + public NoncondexpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_noncondexpression; + } + + public NoncondexpressionContext() {} + + public void copyFrom(NoncondexpressionContext ctx) { + super.copyFrom(ctx); + } + } + + public static class SingleContext extends NoncondexpressionContext { + public UnaryContext unary() { + return getRuleContext(UnaryContext.class, 0); + } + + public SingleContext(NoncondexpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitSingle(this); + else return visitor.visitChildren(this); + } + } + + public static class CompContext extends NoncondexpressionContext { + public List noncondexpression() { + return getRuleContexts(NoncondexpressionContext.class); + } + + public NoncondexpressionContext noncondexpression(int i) { + return getRuleContext(NoncondexpressionContext.class, i); + } + + public TerminalNode LT() { + return getToken(PainlessParser.LT, 0); + } + + public TerminalNode LTE() { + return getToken(PainlessParser.LTE, 0); + } + + public TerminalNode GT() { + return getToken(PainlessParser.GT, 0); + } + + public TerminalNode GTE() { + return getToken(PainlessParser.GTE, 0); + } + + public TerminalNode EQ() { + return getToken(PainlessParser.EQ, 0); + } + + public TerminalNode EQR() { + return getToken(PainlessParser.EQR, 0); + } + + public TerminalNode NE() { + return getToken(PainlessParser.NE, 0); + } + + public TerminalNode NER() { + return getToken(PainlessParser.NER, 0); + } + + public CompContext(NoncondexpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitComp(this); + else return visitor.visitChildren(this); + } + } + + public static class BoolContext extends NoncondexpressionContext { + public List noncondexpression() { + return getRuleContexts(NoncondexpressionContext.class); + } + + public NoncondexpressionContext noncondexpression(int i) { + return getRuleContext(NoncondexpressionContext.class, i); + } + + public TerminalNode BOOLAND() { + return getToken(PainlessParser.BOOLAND, 0); + } + + public TerminalNode BOOLOR() { + return getToken(PainlessParser.BOOLOR, 0); + } + + public BoolContext(NoncondexpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitBool(this); + else return visitor.visitChildren(this); + } + } + + public static class BinaryContext extends NoncondexpressionContext { + public List noncondexpression() { + return getRuleContexts(NoncondexpressionContext.class); + } + + public NoncondexpressionContext noncondexpression(int i) { + return getRuleContext(NoncondexpressionContext.class, i); + } + + public TerminalNode MUL() { + return getToken(PainlessParser.MUL, 0); + } + + public TerminalNode DIV() { + return getToken(PainlessParser.DIV, 0); + } + + public TerminalNode REM() { + return getToken(PainlessParser.REM, 0); + } + + public TerminalNode ADD() { + return getToken(PainlessParser.ADD, 0); + } + + public TerminalNode SUB() { + return getToken(PainlessParser.SUB, 0); + } + + public TerminalNode FIND() { + return getToken(PainlessParser.FIND, 0); + } + + public TerminalNode MATCH() { + return getToken(PainlessParser.MATCH, 0); + } + + public TerminalNode LSH() { + return getToken(PainlessParser.LSH, 0); + } + + public TerminalNode RSH() { + return getToken(PainlessParser.RSH, 0); + } + + public TerminalNode USH() { + return getToken(PainlessParser.USH, 0); + } + + public TerminalNode BWAND() { + return getToken(PainlessParser.BWAND, 0); + } + + public TerminalNode XOR() { + return getToken(PainlessParser.XOR, 0); + } + + public TerminalNode BWOR() { + return getToken(PainlessParser.BWOR, 0); + } + + public BinaryContext(NoncondexpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitBinary(this); + else return visitor.visitChildren(this); + } + } + + public static class ElvisContext extends NoncondexpressionContext { + public List noncondexpression() { + return getRuleContexts(NoncondexpressionContext.class); + } + + public NoncondexpressionContext noncondexpression(int i) { + return getRuleContext(NoncondexpressionContext.class, i); + } + + public TerminalNode ELVIS() { + return getToken(PainlessParser.ELVIS, 0); + } + + public ElvisContext(NoncondexpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitElvis(this); + else return visitor.visitChildren(this); + } + } + + public static class InstanceofContext extends NoncondexpressionContext { + public NoncondexpressionContext noncondexpression() { + return getRuleContext(NoncondexpressionContext.class, 0); + } + + public TerminalNode INSTANCEOF() { + return getToken(PainlessParser.INSTANCEOF, 0); + } + + public DecltypeContext decltype() { + return getRuleContext(DecltypeContext.class, 0); + } + + public InstanceofContext(NoncondexpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitInstanceof(this); + else return visitor.visitChildren(this); + } + } + + public final NoncondexpressionContext noncondexpression() throws RecognitionException { + return noncondexpression(0); + } + + private NoncondexpressionContext noncondexpression(int _p) throws RecognitionException { + ParserRuleContext _parentctx = _ctx; + int _parentState = getState(); + NoncondexpressionContext _localctx = new NoncondexpressionContext(_ctx, _parentState); + NoncondexpressionContext _prevctx = _localctx; + int _startState = 32; + enterRecursionRule(_localctx, 32, RULE_noncondexpression, _p); + int _la; + try { + int _alt; + enterOuterAlt(_localctx, 1); + { + { + _localctx = new SingleContext(_localctx); + _ctx = _localctx; + _prevctx = _localctx; + + setState(266); + unary(); + } + _ctx.stop = _input.LT(-1); + setState(309); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input, 25, _ctx); + while (_alt != 2 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER) { + if (_alt == 1) { + if (_parseListeners != null) triggerExitRuleEvent(); + _prevctx = _localctx; + { + setState(307); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 24, _ctx)) { + case 1: { + _localctx = new BinaryContext(new NoncondexpressionContext(_parentctx, _parentState)); + pushNewRecursionContext(_localctx, _startState, RULE_noncondexpression); + setState(268); + if (!(precpred(_ctx, 13))) throw new FailedPredicateException(this, "precpred(_ctx, 13)"); + setState(269); + _la = _input.LA(1); + if (!((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << MUL) | (1L << DIV) | (1L << REM))) != 0))) { + _errHandler.recoverInline(this); + } else { + consume(); + } + setState(270); + noncondexpression(14); + } + break; + case 2: { + _localctx = new BinaryContext(new NoncondexpressionContext(_parentctx, _parentState)); + pushNewRecursionContext(_localctx, _startState, RULE_noncondexpression); + setState(271); + if (!(precpred(_ctx, 12))) throw new FailedPredicateException(this, "precpred(_ctx, 12)"); + setState(272); + _la = _input.LA(1); + if (!(_la == ADD || _la == SUB)) { + _errHandler.recoverInline(this); + } else { + consume(); + } + setState(273); + noncondexpression(13); + } + break; + case 3: { + _localctx = new BinaryContext(new NoncondexpressionContext(_parentctx, _parentState)); + pushNewRecursionContext(_localctx, _startState, RULE_noncondexpression); + setState(274); + if (!(precpred(_ctx, 11))) throw new FailedPredicateException(this, "precpred(_ctx, 11)"); + setState(275); + _la = _input.LA(1); + if (!(_la == FIND || _la == MATCH)) { + _errHandler.recoverInline(this); + } else { + consume(); + } + setState(276); + noncondexpression(12); + } + break; + case 4: { + _localctx = new BinaryContext(new NoncondexpressionContext(_parentctx, _parentState)); + pushNewRecursionContext(_localctx, _startState, RULE_noncondexpression); + setState(277); + if (!(precpred(_ctx, 10))) throw new FailedPredicateException(this, "precpred(_ctx, 10)"); + setState(278); + _la = _input.LA(1); + if (!((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << LSH) | (1L << RSH) | (1L << USH))) != 0))) { + _errHandler.recoverInline(this); + } else { + consume(); + } + setState(279); + noncondexpression(11); + } + break; + case 5: { + _localctx = new CompContext(new NoncondexpressionContext(_parentctx, _parentState)); + pushNewRecursionContext(_localctx, _startState, RULE_noncondexpression); + setState(280); + if (!(precpred(_ctx, 9))) throw new FailedPredicateException(this, "precpred(_ctx, 9)"); + setState(281); + _la = _input.LA(1); + if (!((((_la) & ~0x3f) == 0 + && ((1L << _la) & ((1L << LT) | (1L << LTE) | (1L << GT) | (1L << GTE))) != 0))) { + _errHandler.recoverInline(this); + } else { + consume(); + } + setState(282); + noncondexpression(10); + } + break; + case 6: { + _localctx = new CompContext(new NoncondexpressionContext(_parentctx, _parentState)); + pushNewRecursionContext(_localctx, _startState, RULE_noncondexpression); + setState(283); + if (!(precpred(_ctx, 7))) throw new FailedPredicateException(this, "precpred(_ctx, 7)"); + setState(284); + _la = _input.LA(1); + if (!((((_la) & ~0x3f) == 0 + && ((1L << _la) & ((1L << EQ) | (1L << EQR) | (1L << NE) | (1L << NER))) != 0))) { + _errHandler.recoverInline(this); + } else { + consume(); + } + setState(285); + noncondexpression(8); + } + break; + case 7: { + _localctx = new BinaryContext(new NoncondexpressionContext(_parentctx, _parentState)); + pushNewRecursionContext(_localctx, _startState, RULE_noncondexpression); + setState(286); + if (!(precpred(_ctx, 6))) throw new FailedPredicateException(this, "precpred(_ctx, 6)"); + setState(287); + match(BWAND); + setState(288); + noncondexpression(7); + } + break; + case 8: { + _localctx = new BinaryContext(new NoncondexpressionContext(_parentctx, _parentState)); + pushNewRecursionContext(_localctx, _startState, RULE_noncondexpression); + setState(289); + if (!(precpred(_ctx, 5))) throw new FailedPredicateException(this, "precpred(_ctx, 5)"); + setState(290); + match(XOR); + setState(291); + noncondexpression(6); + } + break; + case 9: { + _localctx = new BinaryContext(new NoncondexpressionContext(_parentctx, _parentState)); + pushNewRecursionContext(_localctx, _startState, RULE_noncondexpression); + setState(292); + if (!(precpred(_ctx, 4))) throw new FailedPredicateException(this, "precpred(_ctx, 4)"); + setState(293); + match(BWOR); + setState(294); + noncondexpression(5); + } + break; + case 10: { + _localctx = new BoolContext(new NoncondexpressionContext(_parentctx, _parentState)); + pushNewRecursionContext(_localctx, _startState, RULE_noncondexpression); + setState(295); + if (!(precpred(_ctx, 3))) throw new FailedPredicateException(this, "precpred(_ctx, 3)"); + setState(296); + match(BOOLAND); + setState(297); + noncondexpression(4); + } + break; + case 11: { + _localctx = new BoolContext(new NoncondexpressionContext(_parentctx, _parentState)); + pushNewRecursionContext(_localctx, _startState, RULE_noncondexpression); + setState(298); + if (!(precpred(_ctx, 2))) throw new FailedPredicateException(this, "precpred(_ctx, 2)"); + setState(299); + match(BOOLOR); + setState(300); + noncondexpression(3); + } + break; + case 12: { + _localctx = new ElvisContext(new NoncondexpressionContext(_parentctx, _parentState)); + pushNewRecursionContext(_localctx, _startState, RULE_noncondexpression); + setState(301); + if (!(precpred(_ctx, 1))) throw new FailedPredicateException(this, "precpred(_ctx, 1)"); + setState(302); + match(ELVIS); + setState(303); + noncondexpression(1); + } + break; + case 13: { + _localctx = new InstanceofContext(new NoncondexpressionContext(_parentctx, _parentState)); + pushNewRecursionContext(_localctx, _startState, RULE_noncondexpression); + setState(304); + if (!(precpred(_ctx, 8))) throw new FailedPredicateException(this, "precpred(_ctx, 8)"); + setState(305); + match(INSTANCEOF); + setState(306); + decltype(); + } + break; + } + } + } + setState(311); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input, 25, _ctx); + } + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + unrollRecursionContexts(_parentctx); + } + return _localctx; + } + + public static class ExpressionContext extends ParserRuleContext { + public ExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_expression; + } + + public ExpressionContext() {} + + public void copyFrom(ExpressionContext ctx) { + super.copyFrom(ctx); + } + } + + public static class ConditionalContext extends ExpressionContext { + public NoncondexpressionContext noncondexpression() { + return getRuleContext(NoncondexpressionContext.class, 0); + } + + public TerminalNode COND() { + return getToken(PainlessParser.COND, 0); + } + + public List expression() { + return getRuleContexts(ExpressionContext.class); + } + + public ExpressionContext expression(int i) { + return getRuleContext(ExpressionContext.class, i); + } + + public TerminalNode COLON() { + return getToken(PainlessParser.COLON, 0); + } + + public ConditionalContext(ExpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitConditional(this); + else return visitor.visitChildren(this); + } + } + + public static class AssignmentContext extends ExpressionContext { + public NoncondexpressionContext noncondexpression() { + return getRuleContext(NoncondexpressionContext.class, 0); + } + + public ExpressionContext expression() { + return getRuleContext(ExpressionContext.class, 0); + } + + public TerminalNode ASSIGN() { + return getToken(PainlessParser.ASSIGN, 0); + } + + public TerminalNode AADD() { + return getToken(PainlessParser.AADD, 0); + } + + public TerminalNode ASUB() { + return getToken(PainlessParser.ASUB, 0); + } + + public TerminalNode AMUL() { + return getToken(PainlessParser.AMUL, 0); + } + + public TerminalNode ADIV() { + return getToken(PainlessParser.ADIV, 0); + } + + public TerminalNode AREM() { + return getToken(PainlessParser.AREM, 0); + } + + public TerminalNode AAND() { + return getToken(PainlessParser.AAND, 0); + } + + public TerminalNode AXOR() { + return getToken(PainlessParser.AXOR, 0); + } + + public TerminalNode AOR() { + return getToken(PainlessParser.AOR, 0); + } + + public TerminalNode ALSH() { + return getToken(PainlessParser.ALSH, 0); + } + + public TerminalNode ARSH() { + return getToken(PainlessParser.ARSH, 0); + } + + public TerminalNode AUSH() { + return getToken(PainlessParser.AUSH, 0); + } + + public AssignmentContext(ExpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitAssignment(this); + else return visitor.visitChildren(this); + } + } + + public static class NonconditionalContext extends ExpressionContext { + public NoncondexpressionContext noncondexpression() { + return getRuleContext(NoncondexpressionContext.class, 0); + } + + public NonconditionalContext(ExpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitNonconditional(this); + else return visitor.visitChildren(this); + } + } + + public final ExpressionContext expression() throws RecognitionException { + ExpressionContext _localctx = new ExpressionContext(_ctx, getState()); + enterRule(_localctx, 34, RULE_expression); + int _la; + try { + setState(323); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 26, _ctx)) { + case 1: + _localctx = new NonconditionalContext(_localctx); + enterOuterAlt(_localctx, 1); { + setState(312); + noncondexpression(0); + } + break; + case 2: + _localctx = new ConditionalContext(_localctx); + enterOuterAlt(_localctx, 2); { + setState(313); + noncondexpression(0); + setState(314); + match(COND); + setState(315); + expression(); + setState(316); + match(COLON); + setState(317); + expression(); + } + break; + case 3: + _localctx = new AssignmentContext(_localctx); + enterOuterAlt(_localctx, 3); { + setState(319); + noncondexpression(0); + setState(320); + _la = _input.LA(1); + if (!(((((_la - 60)) & ~0x3f) == 0 + && ((1L << (_la - 60)) & ((1L << (ASSIGN - 60)) | (1L << (AADD - 60)) | (1L << (ASUB - 60)) | (1L << (AMUL - 60)) + | (1L << (ADIV - 60)) | (1L << (AREM - 60)) | (1L << (AAND - 60)) | (1L << (AXOR - 60)) | (1L << (AOR - 60)) + | (1L << (ALSH - 60)) | (1L << (ARSH - 60)) | (1L << (AUSH - 60)))) != 0))) { + _errHandler.recoverInline(this); + } else { + consume(); + } + setState(321); + expression(); + } + break; + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; + } + + public static class UnaryContext extends ParserRuleContext { + public UnaryContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_unary; + } + + public UnaryContext() {} + + public void copyFrom(UnaryContext ctx) { + super.copyFrom(ctx); + } + } + + public static class NotaddsubContext extends UnaryContext { + public UnarynotaddsubContext unarynotaddsub() { + return getRuleContext(UnarynotaddsubContext.class, 0); + } + + public NotaddsubContext(UnaryContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitNotaddsub(this); + else return visitor.visitChildren(this); + } + } + + public static class PreContext extends UnaryContext { + public ChainContext chain() { + return getRuleContext(ChainContext.class, 0); + } + + public TerminalNode INCR() { + return getToken(PainlessParser.INCR, 0); + } + + public TerminalNode DECR() { + return getToken(PainlessParser.DECR, 0); + } + + public PreContext(UnaryContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitPre(this); + else return visitor.visitChildren(this); + } + } + + public static class AddsubContext extends UnaryContext { + public UnaryContext unary() { + return getRuleContext(UnaryContext.class, 0); + } + + public TerminalNode ADD() { + return getToken(PainlessParser.ADD, 0); + } + + public TerminalNode SUB() { + return getToken(PainlessParser.SUB, 0); + } + + public AddsubContext(UnaryContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitAddsub(this); + else return visitor.visitChildren(this); + } + } + + public final UnaryContext unary() throws RecognitionException { + UnaryContext _localctx = new UnaryContext(_ctx, getState()); + enterRule(_localctx, 36, RULE_unary); + int _la; + try { + setState(330); + switch (_input.LA(1)) { + case INCR: + case DECR: + _localctx = new PreContext(_localctx); + enterOuterAlt(_localctx, 1); { + setState(325); + _la = _input.LA(1); + if (!(_la == INCR || _la == DECR)) { + _errHandler.recoverInline(this); + } else { + consume(); + } + setState(326); + chain(); + } + break; + case ADD: + case SUB: + _localctx = new AddsubContext(_localctx); + enterOuterAlt(_localctx, 2); { + setState(327); + _la = _input.LA(1); + if (!(_la == ADD || _la == SUB)) { + _errHandler.recoverInline(this); + } else { + consume(); + } + setState(328); + unary(); + } + break; + case LBRACE: + case LP: + case NEW: + case BOOLNOT: + case BWNOT: + case OCTAL: + case HEX: + case INTEGER: + case DECIMAL: + case STRING: + case REGEX: + case TRUE: + case FALSE: + case NULL: + case ID: + _localctx = new NotaddsubContext(_localctx); + enterOuterAlt(_localctx, 3); { + setState(329); + unarynotaddsub(); + } + break; + default: + throw new NoViableAltException(this); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; + } + + public static class UnarynotaddsubContext extends ParserRuleContext { + public UnarynotaddsubContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_unarynotaddsub; + } + + public UnarynotaddsubContext() {} + + public void copyFrom(UnarynotaddsubContext ctx) { + super.copyFrom(ctx); + } + } + + public static class CastContext extends UnarynotaddsubContext { + public CastexpressionContext castexpression() { + return getRuleContext(CastexpressionContext.class, 0); + } + + public CastContext(UnarynotaddsubContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitCast(this); + else return visitor.visitChildren(this); + } + } + + public static class NotContext extends UnarynotaddsubContext { + public UnaryContext unary() { + return getRuleContext(UnaryContext.class, 0); + } + + public TerminalNode BOOLNOT() { + return getToken(PainlessParser.BOOLNOT, 0); + } + + public TerminalNode BWNOT() { + return getToken(PainlessParser.BWNOT, 0); + } + + public NotContext(UnarynotaddsubContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitNot(this); + else return visitor.visitChildren(this); + } + } + + public static class ReadContext extends UnarynotaddsubContext { + public ChainContext chain() { + return getRuleContext(ChainContext.class, 0); + } + + public ReadContext(UnarynotaddsubContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitRead(this); + else return visitor.visitChildren(this); + } + } + + public static class PostContext extends UnarynotaddsubContext { + public ChainContext chain() { + return getRuleContext(ChainContext.class, 0); + } + + public TerminalNode INCR() { + return getToken(PainlessParser.INCR, 0); + } + + public TerminalNode DECR() { + return getToken(PainlessParser.DECR, 0); + } + + public PostContext(UnarynotaddsubContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitPost(this); + else return visitor.visitChildren(this); + } + } + + public final UnarynotaddsubContext unarynotaddsub() throws RecognitionException { + UnarynotaddsubContext _localctx = new UnarynotaddsubContext(_ctx, getState()); + enterRule(_localctx, 38, RULE_unarynotaddsub); + int _la; + try { + setState(339); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 28, _ctx)) { + case 1: + _localctx = new ReadContext(_localctx); + enterOuterAlt(_localctx, 1); { + setState(332); + chain(); + } + break; + case 2: + _localctx = new PostContext(_localctx); + enterOuterAlt(_localctx, 2); { + setState(333); + chain(); + setState(334); + _la = _input.LA(1); + if (!(_la == INCR || _la == DECR)) { + _errHandler.recoverInline(this); + } else { + consume(); + } + } + break; + case 3: + _localctx = new NotContext(_localctx); + enterOuterAlt(_localctx, 3); { + setState(336); + _la = _input.LA(1); + if (!(_la == BOOLNOT || _la == BWNOT)) { + _errHandler.recoverInline(this); + } else { + consume(); + } + setState(337); + unary(); + } + break; + case 4: + _localctx = new CastContext(_localctx); + enterOuterAlt(_localctx, 4); { + setState(338); + castexpression(); + } + break; + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; + } + + public static class CastexpressionContext extends ParserRuleContext { + public CastexpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_castexpression; + } + + public CastexpressionContext() {} + + public void copyFrom(CastexpressionContext ctx) { + super.copyFrom(ctx); + } + } + + public static class RefcastContext extends CastexpressionContext { + public TerminalNode LP() { + return getToken(PainlessParser.LP, 0); + } + + public RefcasttypeContext refcasttype() { + return getRuleContext(RefcasttypeContext.class, 0); + } + + public TerminalNode RP() { + return getToken(PainlessParser.RP, 0); + } + + public UnarynotaddsubContext unarynotaddsub() { + return getRuleContext(UnarynotaddsubContext.class, 0); + } + + public RefcastContext(CastexpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitRefcast(this); + else return visitor.visitChildren(this); + } + } + + public static class PrimordefcastContext extends CastexpressionContext { + public TerminalNode LP() { + return getToken(PainlessParser.LP, 0); + } + + public PrimordefcasttypeContext primordefcasttype() { + return getRuleContext(PrimordefcasttypeContext.class, 0); + } + + public TerminalNode RP() { + return getToken(PainlessParser.RP, 0); + } + + public UnaryContext unary() { + return getRuleContext(UnaryContext.class, 0); + } + + public PrimordefcastContext(CastexpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitPrimordefcast(this); + else return visitor.visitChildren(this); + } + } + + public final CastexpressionContext castexpression() throws RecognitionException { + CastexpressionContext _localctx = new CastexpressionContext(_ctx, getState()); + enterRule(_localctx, 40, RULE_castexpression); + try { + setState(351); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 29, _ctx)) { + case 1: + _localctx = new PrimordefcastContext(_localctx); + enterOuterAlt(_localctx, 1); { + setState(341); + match(LP); + setState(342); + primordefcasttype(); + setState(343); + match(RP); + setState(344); + unary(); + } + break; + case 2: + _localctx = new RefcastContext(_localctx); + enterOuterAlt(_localctx, 2); { + setState(346); + match(LP); + setState(347); + refcasttype(); + setState(348); + match(RP); + setState(349); + unarynotaddsub(); + } + break; + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; + } + + public static class PrimordefcasttypeContext extends ParserRuleContext { + public TerminalNode DEF() { + return getToken(PainlessParser.DEF, 0); + } + + public TerminalNode PRIMITIVE() { + return getToken(PainlessParser.PRIMITIVE, 0); + } + + public PrimordefcasttypeContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_primordefcasttype; + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitPrimordefcasttype( + this + ); + else return visitor.visitChildren(this); + } + } + + public final PrimordefcasttypeContext primordefcasttype() throws RecognitionException { + PrimordefcasttypeContext _localctx = new PrimordefcasttypeContext(_ctx, getState()); + enterRule(_localctx, 42, RULE_primordefcasttype); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(353); + _la = _input.LA(1); + if (!(_la == PRIMITIVE || _la == DEF)) { + _errHandler.recoverInline(this); + } else { + consume(); + } + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; + } + + public static class RefcasttypeContext extends ParserRuleContext { + public TerminalNode DEF() { + return getToken(PainlessParser.DEF, 0); + } + + public List LBRACE() { + return getTokens(PainlessParser.LBRACE); + } + + public TerminalNode LBRACE(int i) { + return getToken(PainlessParser.LBRACE, i); + } + + public List RBRACE() { + return getTokens(PainlessParser.RBRACE); + } + + public TerminalNode RBRACE(int i) { + return getToken(PainlessParser.RBRACE, i); + } + + public TerminalNode PRIMITIVE() { + return getToken(PainlessParser.PRIMITIVE, 0); + } + + public TerminalNode ID() { + return getToken(PainlessParser.ID, 0); + } + + public List DOT() { + return getTokens(PainlessParser.DOT); + } + + public TerminalNode DOT(int i) { + return getToken(PainlessParser.DOT, i); + } + + public List DOTID() { + return getTokens(PainlessParser.DOTID); + } + + public TerminalNode DOTID(int i) { + return getToken(PainlessParser.DOTID, i); + } + + public RefcasttypeContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_refcasttype; + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitRefcasttype(this); + else return visitor.visitChildren(this); + } + } + + public final RefcasttypeContext refcasttype() throws RecognitionException { + RefcasttypeContext _localctx = new RefcasttypeContext(_ctx, getState()); + enterRule(_localctx, 44, RULE_refcasttype); + int _la; + try { + setState(384); + switch (_input.LA(1)) { + case DEF: + enterOuterAlt(_localctx, 1); { + setState(355); + match(DEF); + setState(358); + _errHandler.sync(this); + _la = _input.LA(1); + do { + { + { + setState(356); + match(LBRACE); + setState(357); + match(RBRACE); + } + } + setState(360); + _errHandler.sync(this); + _la = _input.LA(1); + } while (_la == LBRACE); + } + break; + case PRIMITIVE: + enterOuterAlt(_localctx, 2); { + setState(362); + match(PRIMITIVE); + setState(365); + _errHandler.sync(this); + _la = _input.LA(1); + do { + { + { + setState(363); + match(LBRACE); + setState(364); + match(RBRACE); + } + } + setState(367); + _errHandler.sync(this); + _la = _input.LA(1); + } while (_la == LBRACE); + } + break; + case ID: + enterOuterAlt(_localctx, 3); { + setState(369); + match(ID); + setState(374); + _errHandler.sync(this); + _la = _input.LA(1); + while (_la == DOT) { + { + { + setState(370); + match(DOT); + setState(371); + match(DOTID); + } + } + setState(376); + _errHandler.sync(this); + _la = _input.LA(1); + } + setState(381); + _errHandler.sync(this); + _la = _input.LA(1); + while (_la == LBRACE) { + { + { + setState(377); + match(LBRACE); + setState(378); + match(RBRACE); + } + } + setState(383); + _errHandler.sync(this); + _la = _input.LA(1); + } + } + break; + default: + throw new NoViableAltException(this); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; + } + + public static class ChainContext extends ParserRuleContext { + public ChainContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_chain; + } + + public ChainContext() {} + + public void copyFrom(ChainContext ctx) { + super.copyFrom(ctx); + } + } + + public static class DynamicContext extends ChainContext { + public PrimaryContext primary() { + return getRuleContext(PrimaryContext.class, 0); + } + + public List postfix() { + return getRuleContexts(PostfixContext.class); + } + + public PostfixContext postfix(int i) { + return getRuleContext(PostfixContext.class, i); + } + + public DynamicContext(ChainContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitDynamic(this); + else return visitor.visitChildren(this); + } + } + + public static class NewarrayContext extends ChainContext { + public ArrayinitializerContext arrayinitializer() { + return getRuleContext(ArrayinitializerContext.class, 0); + } + + public NewarrayContext(ChainContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitNewarray(this); + else return visitor.visitChildren(this); + } + } + + public final ChainContext chain() throws RecognitionException { + ChainContext _localctx = new ChainContext(_ctx, getState()); + enterRule(_localctx, 46, RULE_chain); + try { + int _alt; + setState(394); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 36, _ctx)) { + case 1: + _localctx = new DynamicContext(_localctx); + enterOuterAlt(_localctx, 1); { + setState(386); + primary(); + setState(390); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input, 35, _ctx); + while (_alt != 2 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER) { + if (_alt == 1) { + { + { + setState(387); + postfix(); + } + } + } + setState(392); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input, 35, _ctx); + } + } + break; + case 2: + _localctx = new NewarrayContext(_localctx); + enterOuterAlt(_localctx, 2); { + setState(393); + arrayinitializer(); + } + break; + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; + } + + public static class PrimaryContext extends ParserRuleContext { + public PrimaryContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_primary; + } + + public PrimaryContext() {} + + public void copyFrom(PrimaryContext ctx) { + super.copyFrom(ctx); + } + } + + public static class ListinitContext extends PrimaryContext { + public ListinitializerContext listinitializer() { + return getRuleContext(ListinitializerContext.class, 0); + } + + public ListinitContext(PrimaryContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitListinit(this); + else return visitor.visitChildren(this); + } + } + + public static class RegexContext extends PrimaryContext { + public TerminalNode REGEX() { + return getToken(PainlessParser.REGEX, 0); + } + + public RegexContext(PrimaryContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitRegex(this); + else return visitor.visitChildren(this); + } + } + + public static class NullContext extends PrimaryContext { + public TerminalNode NULL() { + return getToken(PainlessParser.NULL, 0); + } + + public NullContext(PrimaryContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitNull(this); + else return visitor.visitChildren(this); + } + } + + public static class StringContext extends PrimaryContext { + public TerminalNode STRING() { + return getToken(PainlessParser.STRING, 0); + } + + public StringContext(PrimaryContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitString(this); + else return visitor.visitChildren(this); + } + } + + public static class MapinitContext extends PrimaryContext { + public MapinitializerContext mapinitializer() { + return getRuleContext(MapinitializerContext.class, 0); + } + + public MapinitContext(PrimaryContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitMapinit(this); + else return visitor.visitChildren(this); + } + } + + public static class CalllocalContext extends PrimaryContext { + public TerminalNode ID() { + return getToken(PainlessParser.ID, 0); + } + + public ArgumentsContext arguments() { + return getRuleContext(ArgumentsContext.class, 0); + } + + public CalllocalContext(PrimaryContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitCalllocal(this); + else return visitor.visitChildren(this); + } + } + + public static class TrueContext extends PrimaryContext { + public TerminalNode TRUE() { + return getToken(PainlessParser.TRUE, 0); + } + + public TrueContext(PrimaryContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitTrue(this); + else return visitor.visitChildren(this); + } + } + + public static class FalseContext extends PrimaryContext { + public TerminalNode FALSE() { + return getToken(PainlessParser.FALSE, 0); + } + + public FalseContext(PrimaryContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitFalse(this); + else return visitor.visitChildren(this); + } + } + + public static class VariableContext extends PrimaryContext { + public TerminalNode ID() { + return getToken(PainlessParser.ID, 0); + } + + public VariableContext(PrimaryContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitVariable(this); + else return visitor.visitChildren(this); + } + } + + public static class NumericContext extends PrimaryContext { + public TerminalNode OCTAL() { + return getToken(PainlessParser.OCTAL, 0); + } + + public TerminalNode HEX() { + return getToken(PainlessParser.HEX, 0); + } + + public TerminalNode INTEGER() { + return getToken(PainlessParser.INTEGER, 0); + } + + public TerminalNode DECIMAL() { + return getToken(PainlessParser.DECIMAL, 0); + } + + public NumericContext(PrimaryContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitNumeric(this); + else return visitor.visitChildren(this); + } + } + + public static class NewobjectContext extends PrimaryContext { + public TerminalNode NEW() { + return getToken(PainlessParser.NEW, 0); + } + + public TypeContext type() { + return getRuleContext(TypeContext.class, 0); + } + + public ArgumentsContext arguments() { + return getRuleContext(ArgumentsContext.class, 0); + } + + public NewobjectContext(PrimaryContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitNewobject(this); + else return visitor.visitChildren(this); + } + } + + public static class PrecedenceContext extends PrimaryContext { + public TerminalNode LP() { + return getToken(PainlessParser.LP, 0); + } + + public ExpressionContext expression() { + return getRuleContext(ExpressionContext.class, 0); + } + + public TerminalNode RP() { + return getToken(PainlessParser.RP, 0); + } + + public PrecedenceContext(PrimaryContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitPrecedence(this); + else return visitor.visitChildren(this); + } + } + + public final PrimaryContext primary() throws RecognitionException { + PrimaryContext _localctx = new PrimaryContext(_ctx, getState()); + enterRule(_localctx, 48, RULE_primary); + int _la; + try { + setState(415); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 37, _ctx)) { + case 1: + _localctx = new PrecedenceContext(_localctx); + enterOuterAlt(_localctx, 1); { + setState(396); + match(LP); + setState(397); + expression(); + setState(398); + match(RP); + } + break; + case 2: + _localctx = new NumericContext(_localctx); + enterOuterAlt(_localctx, 2); { + setState(400); + _la = _input.LA(1); + if (!(((((_la - 72)) & ~0x3f) == 0 + && ((1L << (_la - 72)) & ((1L << (OCTAL - 72)) | (1L << (HEX - 72)) | (1L << (INTEGER - 72)) | (1L << (DECIMAL + - 72)))) != 0))) { + _errHandler.recoverInline(this); + } else { + consume(); + } + } + break; + case 3: + _localctx = new TrueContext(_localctx); + enterOuterAlt(_localctx, 3); { + setState(401); + match(TRUE); + } + break; + case 4: + _localctx = new FalseContext(_localctx); + enterOuterAlt(_localctx, 4); { + setState(402); + match(FALSE); + } + break; + case 5: + _localctx = new NullContext(_localctx); + enterOuterAlt(_localctx, 5); { + setState(403); + match(NULL); + } + break; + case 6: + _localctx = new StringContext(_localctx); + enterOuterAlt(_localctx, 6); { + setState(404); + match(STRING); + } + break; + case 7: + _localctx = new RegexContext(_localctx); + enterOuterAlt(_localctx, 7); { + setState(405); + match(REGEX); + } + break; + case 8: + _localctx = new ListinitContext(_localctx); + enterOuterAlt(_localctx, 8); { + setState(406); + listinitializer(); + } + break; + case 9: + _localctx = new MapinitContext(_localctx); + enterOuterAlt(_localctx, 9); { + setState(407); + mapinitializer(); + } + break; + case 10: + _localctx = new VariableContext(_localctx); + enterOuterAlt(_localctx, 10); { + setState(408); + match(ID); + } + break; + case 11: + _localctx = new CalllocalContext(_localctx); + enterOuterAlt(_localctx, 11); { + setState(409); + match(ID); + setState(410); + arguments(); + } + break; + case 12: + _localctx = new NewobjectContext(_localctx); + enterOuterAlt(_localctx, 12); { + setState(411); + match(NEW); + setState(412); + type(); + setState(413); + arguments(); + } + break; + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; + } + + public static class PostfixContext extends ParserRuleContext { + public CallinvokeContext callinvoke() { + return getRuleContext(CallinvokeContext.class, 0); + } + + public FieldaccessContext fieldaccess() { + return getRuleContext(FieldaccessContext.class, 0); + } + + public BraceaccessContext braceaccess() { + return getRuleContext(BraceaccessContext.class, 0); + } + + public PostfixContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_postfix; + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitPostfix(this); + else return visitor.visitChildren(this); + } + } + + public final PostfixContext postfix() throws RecognitionException { + PostfixContext _localctx = new PostfixContext(_ctx, getState()); + enterRule(_localctx, 50, RULE_postfix); + try { + setState(420); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 38, _ctx)) { + case 1: + enterOuterAlt(_localctx, 1); { + setState(417); + callinvoke(); + } + break; + case 2: + enterOuterAlt(_localctx, 2); { + setState(418); + fieldaccess(); + } + break; + case 3: + enterOuterAlt(_localctx, 3); { + setState(419); + braceaccess(); + } + break; + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; + } + + public static class PostdotContext extends ParserRuleContext { + public CallinvokeContext callinvoke() { + return getRuleContext(CallinvokeContext.class, 0); + } + + public FieldaccessContext fieldaccess() { + return getRuleContext(FieldaccessContext.class, 0); + } + + public PostdotContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_postdot; + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitPostdot(this); + else return visitor.visitChildren(this); + } + } + + public final PostdotContext postdot() throws RecognitionException { + PostdotContext _localctx = new PostdotContext(_ctx, getState()); + enterRule(_localctx, 52, RULE_postdot); + try { + setState(424); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 39, _ctx)) { + case 1: + enterOuterAlt(_localctx, 1); { + setState(422); + callinvoke(); + } + break; + case 2: + enterOuterAlt(_localctx, 2); { + setState(423); + fieldaccess(); + } + break; + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; + } + + public static class CallinvokeContext extends ParserRuleContext { + public TerminalNode DOTID() { + return getToken(PainlessParser.DOTID, 0); + } + + public ArgumentsContext arguments() { + return getRuleContext(ArgumentsContext.class, 0); + } + + public TerminalNode DOT() { + return getToken(PainlessParser.DOT, 0); + } + + public TerminalNode NSDOT() { + return getToken(PainlessParser.NSDOT, 0); + } + + public CallinvokeContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_callinvoke; + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitCallinvoke(this); + else return visitor.visitChildren(this); + } + } + + public final CallinvokeContext callinvoke() throws RecognitionException { + CallinvokeContext _localctx = new CallinvokeContext(_ctx, getState()); + enterRule(_localctx, 54, RULE_callinvoke); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(426); + _la = _input.LA(1); + if (!(_la == DOT || _la == NSDOT)) { + _errHandler.recoverInline(this); + } else { + consume(); + } + setState(427); + match(DOTID); + setState(428); + arguments(); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; + } + + public static class FieldaccessContext extends ParserRuleContext { + public TerminalNode DOT() { + return getToken(PainlessParser.DOT, 0); + } + + public TerminalNode NSDOT() { + return getToken(PainlessParser.NSDOT, 0); + } + + public TerminalNode DOTID() { + return getToken(PainlessParser.DOTID, 0); + } + + public TerminalNode DOTINTEGER() { + return getToken(PainlessParser.DOTINTEGER, 0); + } + + public FieldaccessContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_fieldaccess; + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitFieldaccess(this); + else return visitor.visitChildren(this); + } + } + + public final FieldaccessContext fieldaccess() throws RecognitionException { + FieldaccessContext _localctx = new FieldaccessContext(_ctx, getState()); + enterRule(_localctx, 56, RULE_fieldaccess); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(430); + _la = _input.LA(1); + if (!(_la == DOT || _la == NSDOT)) { + _errHandler.recoverInline(this); + } else { + consume(); + } + setState(431); + _la = _input.LA(1); + if (!(_la == DOTINTEGER || _la == DOTID)) { + _errHandler.recoverInline(this); + } else { + consume(); + } + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; + } + + public static class BraceaccessContext extends ParserRuleContext { + public TerminalNode LBRACE() { + return getToken(PainlessParser.LBRACE, 0); + } + + public ExpressionContext expression() { + return getRuleContext(ExpressionContext.class, 0); + } + + public TerminalNode RBRACE() { + return getToken(PainlessParser.RBRACE, 0); + } + + public BraceaccessContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_braceaccess; + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitBraceaccess(this); + else return visitor.visitChildren(this); + } + } + + public final BraceaccessContext braceaccess() throws RecognitionException { + BraceaccessContext _localctx = new BraceaccessContext(_ctx, getState()); + enterRule(_localctx, 58, RULE_braceaccess); + try { + enterOuterAlt(_localctx, 1); + { + setState(433); + match(LBRACE); + setState(434); + expression(); + setState(435); + match(RBRACE); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; + } + + public static class ArrayinitializerContext extends ParserRuleContext { + public ArrayinitializerContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_arrayinitializer; + } + + public ArrayinitializerContext() {} + + public void copyFrom(ArrayinitializerContext ctx) { + super.copyFrom(ctx); + } + } + + public static class NewstandardarrayContext extends ArrayinitializerContext { + public TerminalNode NEW() { + return getToken(PainlessParser.NEW, 0); + } + + public TypeContext type() { + return getRuleContext(TypeContext.class, 0); + } + + public List LBRACE() { + return getTokens(PainlessParser.LBRACE); + } + + public TerminalNode LBRACE(int i) { + return getToken(PainlessParser.LBRACE, i); + } + + public List expression() { + return getRuleContexts(ExpressionContext.class); + } + + public ExpressionContext expression(int i) { + return getRuleContext(ExpressionContext.class, i); + } + + public List RBRACE() { + return getTokens(PainlessParser.RBRACE); + } + + public TerminalNode RBRACE(int i) { + return getToken(PainlessParser.RBRACE, i); + } + + public PostdotContext postdot() { + return getRuleContext(PostdotContext.class, 0); + } + + public List postfix() { + return getRuleContexts(PostfixContext.class); + } + + public PostfixContext postfix(int i) { + return getRuleContext(PostfixContext.class, i); + } + + public NewstandardarrayContext(ArrayinitializerContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitNewstandardarray(this); + else return visitor.visitChildren(this); + } + } + + public static class NewinitializedarrayContext extends ArrayinitializerContext { + public TerminalNode NEW() { + return getToken(PainlessParser.NEW, 0); + } + + public TypeContext type() { + return getRuleContext(TypeContext.class, 0); + } + + public TerminalNode LBRACE() { + return getToken(PainlessParser.LBRACE, 0); + } + + public TerminalNode RBRACE() { + return getToken(PainlessParser.RBRACE, 0); + } + + public TerminalNode LBRACK() { + return getToken(PainlessParser.LBRACK, 0); + } + + public TerminalNode RBRACK() { + return getToken(PainlessParser.RBRACK, 0); + } + + public List expression() { + return getRuleContexts(ExpressionContext.class); + } + + public ExpressionContext expression(int i) { + return getRuleContext(ExpressionContext.class, i); + } + + public List postfix() { + return getRuleContexts(PostfixContext.class); + } + + public PostfixContext postfix(int i) { + return getRuleContext(PostfixContext.class, i); + } + + public List COMMA() { + return getTokens(PainlessParser.COMMA); + } + + public TerminalNode COMMA(int i) { + return getToken(PainlessParser.COMMA, i); + } + + public NewinitializedarrayContext(ArrayinitializerContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitNewinitializedarray( + this + ); + else return visitor.visitChildren(this); + } + } + + public final ArrayinitializerContext arrayinitializer() throws RecognitionException { + ArrayinitializerContext _localctx = new ArrayinitializerContext(_ctx, getState()); + enterRule(_localctx, 60, RULE_arrayinitializer); + int _la; + try { + int _alt; + setState(478); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 46, _ctx)) { + case 1: + _localctx = new NewstandardarrayContext(_localctx); + enterOuterAlt(_localctx, 1); { + setState(437); + match(NEW); + setState(438); + type(); + setState(443); + _errHandler.sync(this); + _alt = 1; + do { + switch (_alt) { + case 1: { + { + setState(439); + match(LBRACE); + setState(440); + expression(); + setState(441); + match(RBRACE); + } + } + break; + default: + throw new NoViableAltException(this); + } + setState(445); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input, 40, _ctx); + } while (_alt != 2 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER); + setState(454); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 42, _ctx)) { + case 1: { + setState(447); + postdot(); + setState(451); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input, 41, _ctx); + while (_alt != 2 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER) { + if (_alt == 1) { + { + { + setState(448); + postfix(); + } + } + } + setState(453); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input, 41, _ctx); + } + } + break; + } + } + break; + case 2: + _localctx = new NewinitializedarrayContext(_localctx); + enterOuterAlt(_localctx, 2); { + setState(456); + match(NEW); + setState(457); + type(); + setState(458); + match(LBRACE); + setState(459); + match(RBRACE); + setState(460); + match(LBRACK); + setState(469); + _la = _input.LA(1); + if ((((_la) & ~0x3f) == 0 + && ((1L << _la) & ((1L << LBRACE) | (1L << LP) | (1L << NEW) | (1L << BOOLNOT) | (1L << BWNOT) | (1L << ADD) | (1L + << SUB) | (1L << INCR) | (1L << DECR))) != 0) + || ((((_la - 72)) & ~0x3f) == 0 + && ((1L << (_la - 72)) & ((1L << (OCTAL - 72)) | (1L << (HEX - 72)) | (1L << (INTEGER - 72)) | (1L << (DECIMAL + - 72)) | (1L << (STRING - 72)) | (1L << (REGEX - 72)) | (1L << (TRUE - 72)) | (1L << (FALSE - 72)) | (1L + << (NULL - 72)) | (1L << (ID - 72)))) != 0)) { + { + setState(461); + expression(); + setState(466); + _errHandler.sync(this); + _la = _input.LA(1); + while (_la == COMMA) { + { + { + setState(462); + match(COMMA); + setState(463); + expression(); + } + } + setState(468); + _errHandler.sync(this); + _la = _input.LA(1); + } + } + } + + setState(471); + match(RBRACK); + setState(475); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input, 45, _ctx); + while (_alt != 2 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER) { + if (_alt == 1) { + { + { + setState(472); + postfix(); + } + } + } + setState(477); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input, 45, _ctx); + } + } + break; + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; + } + + public static class ListinitializerContext extends ParserRuleContext { + public TerminalNode LBRACE() { + return getToken(PainlessParser.LBRACE, 0); + } + + public List expression() { + return getRuleContexts(ExpressionContext.class); + } + + public ExpressionContext expression(int i) { + return getRuleContext(ExpressionContext.class, i); + } + + public TerminalNode RBRACE() { + return getToken(PainlessParser.RBRACE, 0); + } + + public List COMMA() { + return getTokens(PainlessParser.COMMA); + } + + public TerminalNode COMMA(int i) { + return getToken(PainlessParser.COMMA, i); + } + + public ListinitializerContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_listinitializer; + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitListinitializer(this); + else return visitor.visitChildren(this); + } + } + + public final ListinitializerContext listinitializer() throws RecognitionException { + ListinitializerContext _localctx = new ListinitializerContext(_ctx, getState()); + enterRule(_localctx, 62, RULE_listinitializer); + int _la; + try { + setState(493); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 48, _ctx)) { + case 1: + enterOuterAlt(_localctx, 1); { + setState(480); + match(LBRACE); + setState(481); + expression(); + setState(486); + _errHandler.sync(this); + _la = _input.LA(1); + while (_la == COMMA) { + { + { + setState(482); + match(COMMA); + setState(483); + expression(); + } + } + setState(488); + _errHandler.sync(this); + _la = _input.LA(1); + } + setState(489); + match(RBRACE); + } + break; + case 2: + enterOuterAlt(_localctx, 2); { + setState(491); + match(LBRACE); + setState(492); + match(RBRACE); + } + break; + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; + } + + public static class MapinitializerContext extends ParserRuleContext { + public TerminalNode LBRACE() { + return getToken(PainlessParser.LBRACE, 0); + } + + public List maptoken() { + return getRuleContexts(MaptokenContext.class); + } + + public MaptokenContext maptoken(int i) { + return getRuleContext(MaptokenContext.class, i); + } + + public TerminalNode RBRACE() { + return getToken(PainlessParser.RBRACE, 0); + } + + public List COMMA() { + return getTokens(PainlessParser.COMMA); + } + + public TerminalNode COMMA(int i) { + return getToken(PainlessParser.COMMA, i); + } + + public TerminalNode COLON() { + return getToken(PainlessParser.COLON, 0); + } + + public MapinitializerContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_mapinitializer; + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitMapinitializer(this); + else return visitor.visitChildren(this); + } + } + + public final MapinitializerContext mapinitializer() throws RecognitionException { + MapinitializerContext _localctx = new MapinitializerContext(_ctx, getState()); + enterRule(_localctx, 64, RULE_mapinitializer); + int _la; + try { + setState(509); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 50, _ctx)) { + case 1: + enterOuterAlt(_localctx, 1); { + setState(495); + match(LBRACE); + setState(496); + maptoken(); + setState(501); + _errHandler.sync(this); + _la = _input.LA(1); + while (_la == COMMA) { + { + { + setState(497); + match(COMMA); + setState(498); + maptoken(); + } + } + setState(503); + _errHandler.sync(this); + _la = _input.LA(1); + } + setState(504); + match(RBRACE); + } + break; + case 2: + enterOuterAlt(_localctx, 2); { + setState(506); + match(LBRACE); + setState(507); + match(COLON); + setState(508); + match(RBRACE); + } + break; + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; + } + + public static class MaptokenContext extends ParserRuleContext { + public List expression() { + return getRuleContexts(ExpressionContext.class); + } + + public ExpressionContext expression(int i) { + return getRuleContext(ExpressionContext.class, i); + } + + public TerminalNode COLON() { + return getToken(PainlessParser.COLON, 0); + } + + public MaptokenContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_maptoken; + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitMaptoken(this); + else return visitor.visitChildren(this); + } + } + + public final MaptokenContext maptoken() throws RecognitionException { + MaptokenContext _localctx = new MaptokenContext(_ctx, getState()); + enterRule(_localctx, 66, RULE_maptoken); + try { + enterOuterAlt(_localctx, 1); + { + setState(511); + expression(); + setState(512); + match(COLON); + setState(513); + expression(); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; + } + + public static class ArgumentsContext extends ParserRuleContext { + public TerminalNode LP() { + return getToken(PainlessParser.LP, 0); + } + + public TerminalNode RP() { + return getToken(PainlessParser.RP, 0); + } + + public List argument() { + return getRuleContexts(ArgumentContext.class); + } + + public ArgumentContext argument(int i) { + return getRuleContext(ArgumentContext.class, i); + } + + public List COMMA() { + return getTokens(PainlessParser.COMMA); + } + + public TerminalNode COMMA(int i) { + return getToken(PainlessParser.COMMA, i); + } + + public ArgumentsContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_arguments; + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitArguments(this); + else return visitor.visitChildren(this); + } + } + + public final ArgumentsContext arguments() throws RecognitionException { + ArgumentsContext _localctx = new ArgumentsContext(_ctx, getState()); + enterRule(_localctx, 68, RULE_arguments); + int _la; + try { + enterOuterAlt(_localctx, 1); { + { + setState(515); + match(LP); + setState(524); + _la = _input.LA(1); + if ((((_la) & ~0x3f) == 0 + && ((1L << _la) & ((1L << LBRACE) | (1L << LP) | (1L << NEW) | (1L << THIS) | (1L << BOOLNOT) | (1L << BWNOT) | (1L + << ADD) | (1L << SUB) | (1L << INCR) | (1L << DECR))) != 0) + || ((((_la - 72)) & ~0x3f) == 0 + && ((1L << (_la - 72)) & ((1L << (OCTAL - 72)) | (1L << (HEX - 72)) | (1L << (INTEGER - 72)) | (1L << (DECIMAL + - 72)) | (1L << (STRING - 72)) | (1L << (REGEX - 72)) | (1L << (TRUE - 72)) | (1L << (FALSE - 72)) | (1L + << (NULL - 72)) | (1L << (PRIMITIVE - 72)) | (1L << (DEF - 72)) | (1L << (ID - 72)))) != 0)) { + { + setState(516); + argument(); + setState(521); + _errHandler.sync(this); + _la = _input.LA(1); + while (_la == COMMA) { + { + { + setState(517); + match(COMMA); + setState(518); + argument(); + } + } + setState(523); + _errHandler.sync(this); + _la = _input.LA(1); + } + } + } + + setState(526); + match(RP); + } + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; + } + + public static class ArgumentContext extends ParserRuleContext { + public ExpressionContext expression() { + return getRuleContext(ExpressionContext.class, 0); + } + + public LambdaContext lambda() { + return getRuleContext(LambdaContext.class, 0); + } + + public FuncrefContext funcref() { + return getRuleContext(FuncrefContext.class, 0); + } + + public ArgumentContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_argument; + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitArgument(this); + else return visitor.visitChildren(this); + } + } + + public final ArgumentContext argument() throws RecognitionException { + ArgumentContext _localctx = new ArgumentContext(_ctx, getState()); + enterRule(_localctx, 70, RULE_argument); + try { + setState(531); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 53, _ctx)) { + case 1: + enterOuterAlt(_localctx, 1); { + setState(528); + expression(); + } + break; + case 2: + enterOuterAlt(_localctx, 2); { + setState(529); + lambda(); + } + break; + case 3: + enterOuterAlt(_localctx, 3); { + setState(530); + funcref(); + } + break; + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; + } + + public static class LambdaContext extends ParserRuleContext { + public TerminalNode ARROW() { + return getToken(PainlessParser.ARROW, 0); + } + + public List lamtype() { + return getRuleContexts(LamtypeContext.class); + } + + public LamtypeContext lamtype(int i) { + return getRuleContext(LamtypeContext.class, i); + } + + public TerminalNode LP() { + return getToken(PainlessParser.LP, 0); + } + + public TerminalNode RP() { + return getToken(PainlessParser.RP, 0); + } + + public BlockContext block() { + return getRuleContext(BlockContext.class, 0); + } + + public ExpressionContext expression() { + return getRuleContext(ExpressionContext.class, 0); + } + + public List COMMA() { + return getTokens(PainlessParser.COMMA); + } + + public TerminalNode COMMA(int i) { + return getToken(PainlessParser.COMMA, i); + } + + public LambdaContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_lambda; + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitLambda(this); + else return visitor.visitChildren(this); + } + } + + public final LambdaContext lambda() throws RecognitionException { + LambdaContext _localctx = new LambdaContext(_ctx, getState()); + enterRule(_localctx, 72, RULE_lambda); + int _la; + try { + enterOuterAlt(_localctx, 1); { - setState(536); - match(COMMA); - setState(537); - lamtype(); + setState(546); + switch (_input.LA(1)) { + case PRIMITIVE: + case DEF: + case ID: { + setState(533); + lamtype(); + } + break; + case LP: { + setState(534); + match(LP); + setState(543); + _la = _input.LA(1); + if (((((_la - 81)) & ~0x3f) == 0 + && ((1L << (_la - 81)) & ((1L << (PRIMITIVE - 81)) | (1L << (DEF - 81)) | (1L << (ID - 81)))) != 0)) { + { + setState(535); + lamtype(); + setState(540); + _errHandler.sync(this); + _la = _input.LA(1); + while (_la == COMMA) { + { + { + setState(536); + match(COMMA); + setState(537); + lamtype(); + } + } + setState(542); + _errHandler.sync(this); + _la = _input.LA(1); + } + } + } + + setState(545); + match(RP); + } + break; + default: + throw new NoViableAltException(this); + } + setState(548); + match(ARROW); + setState(551); + switch (_input.LA(1)) { + case LBRACK: { + setState(549); + block(); + } + break; + case LBRACE: + case LP: + case NEW: + case BOOLNOT: + case BWNOT: + case ADD: + case SUB: + case INCR: + case DECR: + case OCTAL: + case HEX: + case INTEGER: + case DECIMAL: + case STRING: + case REGEX: + case TRUE: + case FALSE: + case NULL: + case ID: { + setState(550); + expression(); + } + break; + default: + throw new NoViableAltException(this); + } } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; + } + + public static class LamtypeContext extends ParserRuleContext { + public TerminalNode ID() { + return getToken(PainlessParser.ID, 0); + } + + public DecltypeContext decltype() { + return getRuleContext(DecltypeContext.class, 0); + } + + public LamtypeContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_lamtype; + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitLamtype(this); + else return visitor.visitChildren(this); + } + } + + public final LamtypeContext lamtype() throws RecognitionException { + LamtypeContext _localctx = new LamtypeContext(_ctx, getState()); + enterRule(_localctx, 74, RULE_lamtype); + try { + enterOuterAlt(_localctx, 1); + { + setState(554); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 58, _ctx)) { + case 1: { + setState(553); + decltype(); + } + break; + } + setState(556); + match(ID); } - setState(542); + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; + } + + public static class FuncrefContext extends ParserRuleContext { + public FuncrefContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_funcref; + } + + public FuncrefContext() {} + + public void copyFrom(FuncrefContext ctx) { + super.copyFrom(ctx); + } + } + + public static class ClassfuncrefContext extends FuncrefContext { + public DecltypeContext decltype() { + return getRuleContext(DecltypeContext.class, 0); + } + + public TerminalNode REF() { + return getToken(PainlessParser.REF, 0); + } + + public TerminalNode ID() { + return getToken(PainlessParser.ID, 0); + } + + public ClassfuncrefContext(FuncrefContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitClassfuncref(this); + else return visitor.visitChildren(this); + } + } + + public static class ConstructorfuncrefContext extends FuncrefContext { + public DecltypeContext decltype() { + return getRuleContext(DecltypeContext.class, 0); + } + + public TerminalNode REF() { + return getToken(PainlessParser.REF, 0); + } + + public TerminalNode NEW() { + return getToken(PainlessParser.NEW, 0); + } + + public ConstructorfuncrefContext(FuncrefContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitConstructorfuncref( + this + ); + else return visitor.visitChildren(this); + } + } + + public static class LocalfuncrefContext extends FuncrefContext { + public TerminalNode THIS() { + return getToken(PainlessParser.THIS, 0); + } + + public TerminalNode REF() { + return getToken(PainlessParser.REF, 0); + } + + public TerminalNode ID() { + return getToken(PainlessParser.ID, 0); + } + + public LocalfuncrefContext(FuncrefContext ctx) { + copyFrom(ctx); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof PainlessParserVisitor) return ((PainlessParserVisitor) visitor).visitLocalfuncref(this); + else return visitor.visitChildren(this); + } + } + + public final FuncrefContext funcref() throws RecognitionException { + FuncrefContext _localctx = new FuncrefContext(_ctx, getState()); + enterRule(_localctx, 76, RULE_funcref); + try { + setState(569); _errHandler.sync(this); - _la = _input.LA(1); - } - } - } - - setState(545); - match(RP); - } - break; - default: - throw new NoViableAltException(this); - } - setState(548); - match(ARROW); - setState(551); - switch (_input.LA(1)) { - case LBRACK: - { - setState(549); - block(); - } - break; - case LBRACE: - case LP: - case NEW: - case BOOLNOT: - case BWNOT: - case ADD: - case SUB: - case INCR: - case DECR: - case OCTAL: - case HEX: - case INTEGER: - case DECIMAL: - case STRING: - case REGEX: - case TRUE: - case FALSE: - case NULL: - case ID: - { - setState(550); - expression(); - } - break; - default: - throw new NoViableAltException(this); - } - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class LamtypeContext extends ParserRuleContext { - public TerminalNode ID() { return getToken(PainlessParser.ID, 0); } - public DecltypeContext decltype() { - return getRuleContext(DecltypeContext.class,0); - } - public LamtypeContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_lamtype; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitLamtype(this); - else return visitor.visitChildren(this); - } - } - - public final LamtypeContext lamtype() throws RecognitionException { - LamtypeContext _localctx = new LamtypeContext(_ctx, getState()); - enterRule(_localctx, 74, RULE_lamtype); - try { - enterOuterAlt(_localctx, 1); - { - setState(554); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,58,_ctx) ) { - case 1: - { - setState(553); - decltype(); - } - break; - } - setState(556); - match(ID); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class FuncrefContext extends ParserRuleContext { - public FuncrefContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_funcref; } - - public FuncrefContext() { } - public void copyFrom(FuncrefContext ctx) { - super.copyFrom(ctx); - } - } - public static class ClassfuncrefContext extends FuncrefContext { - public DecltypeContext decltype() { - return getRuleContext(DecltypeContext.class,0); - } - public TerminalNode REF() { return getToken(PainlessParser.REF, 0); } - public TerminalNode ID() { return getToken(PainlessParser.ID, 0); } - public ClassfuncrefContext(FuncrefContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitClassfuncref(this); - else return visitor.visitChildren(this); - } - } - public static class ConstructorfuncrefContext extends FuncrefContext { - public DecltypeContext decltype() { - return getRuleContext(DecltypeContext.class,0); - } - public TerminalNode REF() { return getToken(PainlessParser.REF, 0); } - public TerminalNode NEW() { return getToken(PainlessParser.NEW, 0); } - public ConstructorfuncrefContext(FuncrefContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitConstructorfuncref(this); - else return visitor.visitChildren(this); - } - } - public static class LocalfuncrefContext extends FuncrefContext { - public TerminalNode THIS() { return getToken(PainlessParser.THIS, 0); } - public TerminalNode REF() { return getToken(PainlessParser.REF, 0); } - public TerminalNode ID() { return getToken(PainlessParser.ID, 0); } - public LocalfuncrefContext(FuncrefContext ctx) { copyFrom(ctx); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof PainlessParserVisitor ) return ((PainlessParserVisitor)visitor).visitLocalfuncref(this); - else return visitor.visitChildren(this); - } - } - - public final FuncrefContext funcref() throws RecognitionException { - FuncrefContext _localctx = new FuncrefContext(_ctx, getState()); - enterRule(_localctx, 76, RULE_funcref); - try { - setState(569); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,59,_ctx) ) { - case 1: - _localctx = new ClassfuncrefContext(_localctx); - enterOuterAlt(_localctx, 1); - { - setState(558); - decltype(); - setState(559); - match(REF); - setState(560); - match(ID); - } - break; - case 2: - _localctx = new ConstructorfuncrefContext(_localctx); - enterOuterAlt(_localctx, 2); - { - setState(562); - decltype(); - setState(563); - match(REF); - setState(564); - match(NEW); - } - break; - case 3: - _localctx = new LocalfuncrefContext(_localctx); - enterOuterAlt(_localctx, 3); - { - setState(566); - match(THIS); - setState(567); - match(REF); - setState(568); - match(ID); - } - break; - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public boolean sempred(RuleContext _localctx, int ruleIndex, int predIndex) { - switch (ruleIndex) { - case 4: - return rstatement_sempred((RstatementContext)_localctx, predIndex); - case 16: - return noncondexpression_sempred((NoncondexpressionContext)_localctx, predIndex); - } - return true; - } - private boolean rstatement_sempred(RstatementContext _localctx, int predIndex) { - switch (predIndex) { - case 0: - return _input.LA(1) != ELSE ; - } - return true; - } - private boolean noncondexpression_sempred(NoncondexpressionContext _localctx, int predIndex) { - switch (predIndex) { - case 1: - return precpred(_ctx, 13); - case 2: - return precpred(_ctx, 12); - case 3: - return precpred(_ctx, 11); - case 4: - return precpred(_ctx, 10); - case 5: - return precpred(_ctx, 9); - case 6: - return precpred(_ctx, 7); - case 7: - return precpred(_ctx, 6); - case 8: - return precpred(_ctx, 5); - case 9: - return precpred(_ctx, 4); - case 10: - return precpred(_ctx, 3); - case 11: - return precpred(_ctx, 2); - case 12: - return precpred(_ctx, 1); - case 13: - return precpred(_ctx, 8); - } - return true; - } - - public static final String _serializedATN = - "\3\u0430\ud6d1\u8206\uad2d\u4417\uaef1\u8d80\uaadd\3W\u023e\4\2\t\2\4"+ - "\3\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b\t\b\4\t\t\t\4\n\t\n\4\13\t"+ - "\13\4\f\t\f\4\r\t\r\4\16\t\16\4\17\t\17\4\20\t\20\4\21\t\21\4\22\t\22"+ - "\4\23\t\23\4\24\t\24\4\25\t\25\4\26\t\26\4\27\t\27\4\30\t\30\4\31\t\31"+ - "\4\32\t\32\4\33\t\33\4\34\t\34\4\35\t\35\4\36\t\36\4\37\t\37\4 \t \4!"+ - "\t!\4\"\t\"\4#\t#\4$\t$\4%\t%\4&\t&\4\'\t\'\4(\t(\3\2\7\2R\n\2\f\2\16"+ - "\2U\13\2\3\2\7\2X\n\2\f\2\16\2[\13\2\3\2\3\2\3\3\3\3\3\3\3\3\3\3\3\4\3"+ - "\4\3\4\3\4\3\4\3\4\3\4\7\4k\n\4\f\4\16\4n\13\4\5\4p\n\4\3\4\3\4\3\5\3"+ - "\5\3\5\3\5\5\5x\n\5\3\6\3\6\3\6\3\6\3\6\3\6\3\6\3\6\5\6\u0082\n\6\3\6"+ - "\3\6\3\6\3\6\3\6\3\6\5\6\u008a\n\6\3\6\3\6\3\6\5\6\u008f\n\6\3\6\3\6\5"+ - "\6\u0093\n\6\3\6\3\6\5\6\u0097\n\6\3\6\3\6\3\6\5\6\u009c\n\6\3\6\3\6\3"+ - "\6\3\6\3\6\3\6\3\6\3\6\3\6\3\6\3\6\3\6\3\6\3\6\3\6\3\6\3\6\3\6\3\6\3\6"+ - "\6\6\u00b2\n\6\r\6\16\6\u00b3\5\6\u00b6\n\6\3\7\3\7\3\7\3\7\3\7\3\7\3"+ - "\7\3\7\3\7\3\7\3\7\3\7\5\7\u00c4\n\7\3\7\3\7\3\7\5\7\u00c9\n\7\3\b\3\b"+ - "\5\b\u00cd\n\b\3\t\3\t\7\t\u00d1\n\t\f\t\16\t\u00d4\13\t\3\t\5\t\u00d7"+ - "\n\t\3\t\3\t\3\n\3\n\3\13\3\13\5\13\u00df\n\13\3\f\3\f\3\r\3\r\3\r\3\r"+ - "\7\r\u00e7\n\r\f\r\16\r\u00ea\13\r\3\16\3\16\3\16\7\16\u00ef\n\16\f\16"+ - "\16\16\u00f2\13\16\3\17\3\17\3\17\3\17\3\17\7\17\u00f9\n\17\f\17\16\17"+ - "\u00fc\13\17\5\17\u00fe\n\17\3\20\3\20\3\20\5\20\u0103\n\20\3\21\3\21"+ - "\3\21\3\21\3\21\3\21\3\21\3\22\3\22\3\22\3\22\3\22\3\22\3\22\3\22\3\22"+ - "\3\22\3\22\3\22\3\22\3\22\3\22\3\22\3\22\3\22\3\22\3\22\3\22\3\22\3\22"+ - "\3\22\3\22\3\22\3\22\3\22\3\22\3\22\3\22\3\22\3\22\3\22\3\22\3\22\3\22"+ - "\3\22\3\22\3\22\3\22\3\22\7\22\u0136\n\22\f\22\16\22\u0139\13\22\3\23"+ - "\3\23\3\23\3\23\3\23\3\23\3\23\3\23\3\23\3\23\3\23\5\23\u0146\n\23\3\24"+ - "\3\24\3\24\3\24\3\24\5\24\u014d\n\24\3\25\3\25\3\25\3\25\3\25\3\25\3\25"+ - "\5\25\u0156\n\25\3\26\3\26\3\26\3\26\3\26\3\26\3\26\3\26\3\26\3\26\5\26"+ - "\u0162\n\26\3\27\3\27\3\30\3\30\3\30\6\30\u0169\n\30\r\30\16\30\u016a"+ - "\3\30\3\30\3\30\6\30\u0170\n\30\r\30\16\30\u0171\3\30\3\30\3\30\7\30\u0177"+ - "\n\30\f\30\16\30\u017a\13\30\3\30\3\30\7\30\u017e\n\30\f\30\16\30\u0181"+ - "\13\30\5\30\u0183\n\30\3\31\3\31\7\31\u0187\n\31\f\31\16\31\u018a\13\31"+ - "\3\31\5\31\u018d\n\31\3\32\3\32\3\32\3\32\3\32\3\32\3\32\3\32\3\32\3\32"+ - "\3\32\3\32\3\32\3\32\3\32\3\32\3\32\3\32\3\32\5\32\u01a2\n\32\3\33\3\33"+ - "\3\33\5\33\u01a7\n\33\3\34\3\34\5\34\u01ab\n\34\3\35\3\35\3\35\3\35\3"+ - "\36\3\36\3\36\3\37\3\37\3\37\3\37\3 \3 \3 \3 \3 \3 \6 \u01be\n \r \16"+ - " \u01bf\3 \3 \7 \u01c4\n \f \16 \u01c7\13 \5 \u01c9\n \3 \3 \3 \3 \3 "+ - "\3 \3 \3 \7 \u01d3\n \f \16 \u01d6\13 \5 \u01d8\n \3 \3 \7 \u01dc\n \f"+ - " \16 \u01df\13 \5 \u01e1\n \3!\3!\3!\3!\7!\u01e7\n!\f!\16!\u01ea\13!\3"+ - "!\3!\3!\3!\5!\u01f0\n!\3\"\3\"\3\"\3\"\7\"\u01f6\n\"\f\"\16\"\u01f9\13"+ - "\"\3\"\3\"\3\"\3\"\3\"\5\"\u0200\n\"\3#\3#\3#\3#\3$\3$\3$\3$\7$\u020a"+ - "\n$\f$\16$\u020d\13$\5$\u020f\n$\3$\3$\3%\3%\3%\5%\u0216\n%\3&\3&\3&\3"+ - "&\3&\7&\u021d\n&\f&\16&\u0220\13&\5&\u0222\n&\3&\5&\u0225\n&\3&\3&\3&"+ - "\5&\u022a\n&\3\'\5\'\u022d\n\'\3\'\3\'\3(\3(\3(\3(\3(\3(\3(\3(\3(\3(\3"+ - "(\5(\u023c\n(\3(\2\3\")\2\4\6\b\n\f\16\20\22\24\26\30\32\34\36 \"$&(*"+ - ",.\60\62\64\668:<>@BDFHJLN\2\20\3\3\16\16\3\2 \"\3\2#$\3\2:;\3\2%\'\3"+ - "\2(+\3\2,/\3\2>I\3\2<=\3\2\36\37\3\2ST\3\2JM\3\2\13\f\3\2VW\u0279\2S\3"+ - "\2\2\2\4^\3\2\2\2\6c\3\2\2\2\bw\3\2\2\2\n\u00b5\3\2\2\2\f\u00c8\3\2\2"+ - "\2\16\u00cc\3\2\2\2\20\u00ce\3\2\2\2\22\u00da\3\2\2\2\24\u00de\3\2\2\2"+ - "\26\u00e0\3\2\2\2\30\u00e2\3\2\2\2\32\u00eb\3\2\2\2\34\u00fd\3\2\2\2\36"+ - "\u00ff\3\2\2\2 \u0104\3\2\2\2\"\u010b\3\2\2\2$\u0145\3\2\2\2&\u014c\3"+ - "\2\2\2(\u0155\3\2\2\2*\u0161\3\2\2\2,\u0163\3\2\2\2.\u0182\3\2\2\2\60"+ - "\u018c\3\2\2\2\62\u01a1\3\2\2\2\64\u01a6\3\2\2\2\66\u01aa\3\2\2\28\u01ac"+ - "\3\2\2\2:\u01b0\3\2\2\2<\u01b3\3\2\2\2>\u01e0\3\2\2\2@\u01ef\3\2\2\2B"+ - "\u01ff\3\2\2\2D\u0201\3\2\2\2F\u0205\3\2\2\2H\u0215\3\2\2\2J\u0224\3\2"+ - "\2\2L\u022c\3\2\2\2N\u023b\3\2\2\2PR\5\4\3\2QP\3\2\2\2RU\3\2\2\2SQ\3\2"+ - "\2\2ST\3\2\2\2TY\3\2\2\2US\3\2\2\2VX\5\b\5\2WV\3\2\2\2X[\3\2\2\2YW\3\2"+ - "\2\2YZ\3\2\2\2Z\\\3\2\2\2[Y\3\2\2\2\\]\7\2\2\3]\3\3\2\2\2^_\5\32\16\2"+ - "_`\7U\2\2`a\5\6\4\2ab\5\20\t\2b\5\3\2\2\2co\7\t\2\2de\5\32\16\2el\7U\2"+ - "\2fg\7\r\2\2gh\5\32\16\2hi\7U\2\2ik\3\2\2\2jf\3\2\2\2kn\3\2\2\2lj\3\2"+ - "\2\2lm\3\2\2\2mp\3\2\2\2nl\3\2\2\2od\3\2\2\2op\3\2\2\2pq\3\2\2\2qr\7\n"+ - "\2\2r\7\3\2\2\2sx\5\n\6\2tu\5\f\7\2uv\t\2\2\2vx\3\2\2\2ws\3\2\2\2wt\3"+ - "\2\2\2x\t\3\2\2\2yz\7\17\2\2z{\7\t\2\2{|\5$\23\2|}\7\n\2\2}\u0081\5\16"+ - "\b\2~\177\7\21\2\2\177\u0082\5\16\b\2\u0080\u0082\6\6\2\2\u0081~\3\2\2"+ - "\2\u0081\u0080\3\2\2\2\u0082\u00b6\3\2\2\2\u0083\u0084\7\22\2\2\u0084"+ - "\u0085\7\t\2\2\u0085\u0086\5$\23\2\u0086\u0089\7\n\2\2\u0087\u008a\5\16"+ - "\b\2\u0088\u008a\5\22\n\2\u0089\u0087\3\2\2\2\u0089\u0088\3\2\2\2\u008a"+ - "\u00b6\3\2\2\2\u008b\u008c\7\24\2\2\u008c\u008e\7\t\2\2\u008d\u008f\5"+ - "\24\13\2\u008e\u008d\3\2\2\2\u008e\u008f\3\2\2\2\u008f\u0090\3\2\2\2\u0090"+ - "\u0092\7\16\2\2\u0091\u0093\5$\23\2\u0092\u0091\3\2\2\2\u0092\u0093\3"+ - "\2\2\2\u0093\u0094\3\2\2\2\u0094\u0096\7\16\2\2\u0095\u0097\5\26\f\2\u0096"+ - "\u0095\3\2\2\2\u0096\u0097\3\2\2\2\u0097\u0098\3\2\2\2\u0098\u009b\7\n"+ - "\2\2\u0099\u009c\5\16\b\2\u009a\u009c\5\22\n\2\u009b\u0099\3\2\2\2\u009b"+ - "\u009a\3\2\2\2\u009c\u00b6\3\2\2\2\u009d\u009e\7\24\2\2\u009e\u009f\7"+ - "\t\2\2\u009f\u00a0\5\32\16\2\u00a0\u00a1\7U\2\2\u00a1\u00a2\7\66\2\2\u00a2"+ - "\u00a3\5$\23\2\u00a3\u00a4\7\n\2\2\u00a4\u00a5\5\16\b\2\u00a5\u00b6\3"+ - "\2\2\2\u00a6\u00a7\7\24\2\2\u00a7\u00a8\7\t\2\2\u00a8\u00a9\7U\2\2\u00a9"+ - "\u00aa\7\20\2\2\u00aa\u00ab\5$\23\2\u00ab\u00ac\7\n\2\2\u00ac\u00ad\5"+ - "\16\b\2\u00ad\u00b6\3\2\2\2\u00ae\u00af\7\31\2\2\u00af\u00b1\5\20\t\2"+ - "\u00b0\u00b2\5 \21\2\u00b1\u00b0\3\2\2\2\u00b2\u00b3\3\2\2\2\u00b3\u00b1"+ - "\3\2\2\2\u00b3\u00b4\3\2\2\2\u00b4\u00b6\3\2\2\2\u00b5y\3\2\2\2\u00b5"+ - "\u0083\3\2\2\2\u00b5\u008b\3\2\2\2\u00b5\u009d\3\2\2\2\u00b5\u00a6\3\2"+ - "\2\2\u00b5\u00ae\3\2\2\2\u00b6\13\3\2\2\2\u00b7\u00b8\7\23\2\2\u00b8\u00b9"+ - "\5\20\t\2\u00b9\u00ba\7\22\2\2\u00ba\u00bb\7\t\2\2\u00bb\u00bc\5$\23\2"+ - "\u00bc\u00bd\7\n\2\2\u00bd\u00c9\3\2\2\2\u00be\u00c9\5\30\r\2\u00bf\u00c9"+ - "\7\25\2\2\u00c0\u00c9\7\26\2\2\u00c1\u00c3\7\27\2\2\u00c2\u00c4\5$\23"+ - "\2\u00c3\u00c2\3\2\2\2\u00c3\u00c4\3\2\2\2\u00c4\u00c9\3\2\2\2\u00c5\u00c6"+ - "\7\33\2\2\u00c6\u00c9\5$\23\2\u00c7\u00c9\5$\23\2\u00c8\u00b7\3\2\2\2"+ - "\u00c8\u00be\3\2\2\2\u00c8\u00bf\3\2\2\2\u00c8\u00c0\3\2\2\2\u00c8\u00c1"+ - "\3\2\2\2\u00c8\u00c5\3\2\2\2\u00c8\u00c7\3\2\2\2\u00c9\r\3\2\2\2\u00ca"+ - "\u00cd\5\20\t\2\u00cb\u00cd\5\b\5\2\u00cc\u00ca\3\2\2\2\u00cc\u00cb\3"+ - "\2\2\2\u00cd\17\3\2\2\2\u00ce\u00d2\7\5\2\2\u00cf\u00d1\5\b\5\2\u00d0"+ - "\u00cf\3\2\2\2\u00d1\u00d4\3\2\2\2\u00d2\u00d0\3\2\2\2\u00d2\u00d3\3\2"+ - "\2\2\u00d3\u00d6\3\2\2\2\u00d4\u00d2\3\2\2\2\u00d5\u00d7\5\f\7\2\u00d6"+ - "\u00d5\3\2\2\2\u00d6\u00d7\3\2\2\2\u00d7\u00d8\3\2\2\2\u00d8\u00d9\7\6"+ - "\2\2\u00d9\21\3\2\2\2\u00da\u00db\7\16\2\2\u00db\23\3\2\2\2\u00dc\u00df"+ - "\5\30\r\2\u00dd\u00df\5$\23\2\u00de\u00dc\3\2\2\2\u00de\u00dd\3\2\2\2"+ - "\u00df\25\3\2\2\2\u00e0\u00e1\5$\23\2\u00e1\27\3\2\2\2\u00e2\u00e3\5\32"+ - "\16\2\u00e3\u00e8\5\36\20\2\u00e4\u00e5\7\r\2\2\u00e5\u00e7\5\36\20\2"+ - "\u00e6\u00e4\3\2\2\2\u00e7\u00ea\3\2\2\2\u00e8\u00e6\3\2\2\2\u00e8\u00e9"+ - "\3\2\2\2\u00e9\31\3\2\2\2\u00ea\u00e8\3\2\2\2\u00eb\u00f0\5\34\17\2\u00ec"+ - "\u00ed\7\7\2\2\u00ed\u00ef\7\b\2\2\u00ee\u00ec\3\2\2\2\u00ef\u00f2\3\2"+ - "\2\2\u00f0\u00ee\3\2\2\2\u00f0\u00f1\3\2\2\2\u00f1\33\3\2\2\2\u00f2\u00f0"+ - "\3\2\2\2\u00f3\u00fe\7T\2\2\u00f4\u00fe\7S\2\2\u00f5\u00fa\7U\2\2\u00f6"+ - "\u00f7\7\13\2\2\u00f7\u00f9\7W\2\2\u00f8\u00f6\3\2\2\2\u00f9\u00fc\3\2"+ - "\2\2\u00fa\u00f8\3\2\2\2\u00fa\u00fb\3\2\2\2\u00fb\u00fe\3\2\2\2\u00fc"+ - "\u00fa\3\2\2\2\u00fd\u00f3\3\2\2\2\u00fd\u00f4\3\2\2\2\u00fd\u00f5\3\2"+ - "\2\2\u00fe\35\3\2\2\2\u00ff\u0102\7U\2\2\u0100\u0101\7>\2\2\u0101\u0103"+ - "\5$\23\2\u0102\u0100\3\2\2\2\u0102\u0103\3\2\2\2\u0103\37\3\2\2\2\u0104"+ - "\u0105\7\32\2\2\u0105\u0106\7\t\2\2\u0106\u0107\5\34\17\2\u0107\u0108"+ - "\7U\2\2\u0108\u0109\7\n\2\2\u0109\u010a\5\20\t\2\u010a!\3\2\2\2\u010b"+ - "\u010c\b\22\1\2\u010c\u010d\5&\24\2\u010d\u0137\3\2\2\2\u010e\u010f\f"+ - "\17\2\2\u010f\u0110\t\3\2\2\u0110\u0136\5\"\22\20\u0111\u0112\f\16\2\2"+ - "\u0112\u0113\t\4\2\2\u0113\u0136\5\"\22\17\u0114\u0115\f\r\2\2\u0115\u0116"+ - "\t\5\2\2\u0116\u0136\5\"\22\16\u0117\u0118\f\f\2\2\u0118\u0119\t\6\2\2"+ - "\u0119\u0136\5\"\22\r\u011a\u011b\f\13\2\2\u011b\u011c\t\7\2\2\u011c\u0136"+ - "\5\"\22\f\u011d\u011e\f\t\2\2\u011e\u011f\t\b\2\2\u011f\u0136\5\"\22\n"+ - "\u0120\u0121\f\b\2\2\u0121\u0122\7\60\2\2\u0122\u0136\5\"\22\t\u0123\u0124"+ - "\f\7\2\2\u0124\u0125\7\61\2\2\u0125\u0136\5\"\22\b\u0126\u0127\f\6\2\2"+ - "\u0127\u0128\7\62\2\2\u0128\u0136\5\"\22\7\u0129\u012a\f\5\2\2\u012a\u012b"+ - "\7\63\2\2\u012b\u0136\5\"\22\6\u012c\u012d\f\4\2\2\u012d\u012e\7\64\2"+ - "\2\u012e\u0136\5\"\22\5\u012f\u0130\f\3\2\2\u0130\u0131\7\67\2\2\u0131"+ - "\u0136\5\"\22\3\u0132\u0133\f\n\2\2\u0133\u0134\7\35\2\2\u0134\u0136\5"+ - "\32\16\2\u0135\u010e\3\2\2\2\u0135\u0111\3\2\2\2\u0135\u0114\3\2\2\2\u0135"+ - "\u0117\3\2\2\2\u0135\u011a\3\2\2\2\u0135\u011d\3\2\2\2\u0135\u0120\3\2"+ - "\2\2\u0135\u0123\3\2\2\2\u0135\u0126\3\2\2\2\u0135\u0129\3\2\2\2\u0135"+ - "\u012c\3\2\2\2\u0135\u012f\3\2\2\2\u0135\u0132\3\2\2\2\u0136\u0139\3\2"+ - "\2\2\u0137\u0135\3\2\2\2\u0137\u0138\3\2\2\2\u0138#\3\2\2\2\u0139\u0137"+ - "\3\2\2\2\u013a\u0146\5\"\22\2\u013b\u013c\5\"\22\2\u013c\u013d\7\65\2"+ - "\2\u013d\u013e\5$\23\2\u013e\u013f\7\66\2\2\u013f\u0140\5$\23\2\u0140"+ - "\u0146\3\2\2\2\u0141\u0142\5\"\22\2\u0142\u0143\t\t\2\2\u0143\u0144\5"+ - "$\23\2\u0144\u0146\3\2\2\2\u0145\u013a\3\2\2\2\u0145\u013b\3\2\2\2\u0145"+ - "\u0141\3\2\2\2\u0146%\3\2\2\2\u0147\u0148\t\n\2\2\u0148\u014d\5\60\31"+ - "\2\u0149\u014a\t\4\2\2\u014a\u014d\5&\24\2\u014b\u014d\5(\25\2\u014c\u0147"+ - "\3\2\2\2\u014c\u0149\3\2\2\2\u014c\u014b\3\2\2\2\u014d\'\3\2\2\2\u014e"+ - "\u0156\5\60\31\2\u014f\u0150\5\60\31\2\u0150\u0151\t\n\2\2\u0151\u0156"+ - "\3\2\2\2\u0152\u0153\t\13\2\2\u0153\u0156\5&\24\2\u0154\u0156\5*\26\2"+ - "\u0155\u014e\3\2\2\2\u0155\u014f\3\2\2\2\u0155\u0152\3\2\2\2\u0155\u0154"+ - "\3\2\2\2\u0156)\3\2\2\2\u0157\u0158\7\t\2\2\u0158\u0159\5,\27\2\u0159"+ - "\u015a\7\n\2\2\u015a\u015b\5&\24\2\u015b\u0162\3\2\2\2\u015c\u015d\7\t"+ - "\2\2\u015d\u015e\5.\30\2\u015e\u015f\7\n\2\2\u015f\u0160\5(\25\2\u0160"+ - "\u0162\3\2\2\2\u0161\u0157\3\2\2\2\u0161\u015c\3\2\2\2\u0162+\3\2\2\2"+ - "\u0163\u0164\t\f\2\2\u0164-\3\2\2\2\u0165\u0168\7T\2\2\u0166\u0167\7\7"+ - "\2\2\u0167\u0169\7\b\2\2\u0168\u0166\3\2\2\2\u0169\u016a\3\2\2\2\u016a"+ - "\u0168\3\2\2\2\u016a\u016b\3\2\2\2\u016b\u0183\3\2\2\2\u016c\u016f\7S"+ - "\2\2\u016d\u016e\7\7\2\2\u016e\u0170\7\b\2\2\u016f\u016d\3\2\2\2\u0170"+ - "\u0171\3\2\2\2\u0171\u016f\3\2\2\2\u0171\u0172\3\2\2\2\u0172\u0183\3\2"+ - "\2\2\u0173\u0178\7U\2\2\u0174\u0175\7\13\2\2\u0175\u0177\7W\2\2\u0176"+ - "\u0174\3\2\2\2\u0177\u017a\3\2\2\2\u0178\u0176\3\2\2\2\u0178\u0179\3\2"+ - "\2\2\u0179\u017f\3\2\2\2\u017a\u0178\3\2\2\2\u017b\u017c\7\7\2\2\u017c"+ - "\u017e\7\b\2\2\u017d\u017b\3\2\2\2\u017e\u0181\3\2\2\2\u017f\u017d\3\2"+ - "\2\2\u017f\u0180\3\2\2\2\u0180\u0183\3\2\2\2\u0181\u017f\3\2\2\2\u0182"+ - "\u0165\3\2\2\2\u0182\u016c\3\2\2\2\u0182\u0173\3\2\2\2\u0183/\3\2\2\2"+ - "\u0184\u0188\5\62\32\2\u0185\u0187\5\64\33\2\u0186\u0185\3\2\2\2\u0187"+ - "\u018a\3\2\2\2\u0188\u0186\3\2\2\2\u0188\u0189\3\2\2\2\u0189\u018d\3\2"+ - "\2\2\u018a\u0188\3\2\2\2\u018b\u018d\5> \2\u018c\u0184\3\2\2\2\u018c\u018b"+ - "\3\2\2\2\u018d\61\3\2\2\2\u018e\u018f\7\t\2\2\u018f\u0190\5$\23\2\u0190"+ - "\u0191\7\n\2\2\u0191\u01a2\3\2\2\2\u0192\u01a2\t\r\2\2\u0193\u01a2\7P"+ - "\2\2\u0194\u01a2\7Q\2\2\u0195\u01a2\7R\2\2\u0196\u01a2\7N\2\2\u0197\u01a2"+ - "\7O\2\2\u0198\u01a2\5@!\2\u0199\u01a2\5B\"\2\u019a\u01a2\7U\2\2\u019b"+ - "\u019c\7U\2\2\u019c\u01a2\5F$\2\u019d\u019e\7\30\2\2\u019e\u019f\5\34"+ - "\17\2\u019f\u01a0\5F$\2\u01a0\u01a2\3\2\2\2\u01a1\u018e\3\2\2\2\u01a1"+ - "\u0192\3\2\2\2\u01a1\u0193\3\2\2\2\u01a1\u0194\3\2\2\2\u01a1\u0195\3\2"+ - "\2\2\u01a1\u0196\3\2\2\2\u01a1\u0197\3\2\2\2\u01a1\u0198\3\2\2\2\u01a1"+ - "\u0199\3\2\2\2\u01a1\u019a\3\2\2\2\u01a1\u019b\3\2\2\2\u01a1\u019d\3\2"+ - "\2\2\u01a2\63\3\2\2\2\u01a3\u01a7\58\35\2\u01a4\u01a7\5:\36\2\u01a5\u01a7"+ - "\5<\37\2\u01a6\u01a3\3\2\2\2\u01a6\u01a4\3\2\2\2\u01a6\u01a5\3\2\2\2\u01a7"+ - "\65\3\2\2\2\u01a8\u01ab\58\35\2\u01a9\u01ab\5:\36\2\u01aa\u01a8\3\2\2"+ - "\2\u01aa\u01a9\3\2\2\2\u01ab\67\3\2\2\2\u01ac\u01ad\t\16\2\2\u01ad\u01ae"+ - "\7W\2\2\u01ae\u01af\5F$\2\u01af9\3\2\2\2\u01b0\u01b1\t\16\2\2\u01b1\u01b2"+ - "\t\17\2\2\u01b2;\3\2\2\2\u01b3\u01b4\7\7\2\2\u01b4\u01b5\5$\23\2\u01b5"+ - "\u01b6\7\b\2\2\u01b6=\3\2\2\2\u01b7\u01b8\7\30\2\2\u01b8\u01bd\5\34\17"+ - "\2\u01b9\u01ba\7\7\2\2\u01ba\u01bb\5$\23\2\u01bb\u01bc\7\b\2\2\u01bc\u01be"+ - "\3\2\2\2\u01bd\u01b9\3\2\2\2\u01be\u01bf\3\2\2\2\u01bf\u01bd\3\2\2\2\u01bf"+ - "\u01c0\3\2\2\2\u01c0\u01c8\3\2\2\2\u01c1\u01c5\5\66\34\2\u01c2\u01c4\5"+ - "\64\33\2\u01c3\u01c2\3\2\2\2\u01c4\u01c7\3\2\2\2\u01c5\u01c3\3\2\2\2\u01c5"+ - "\u01c6\3\2\2\2\u01c6\u01c9\3\2\2\2\u01c7\u01c5\3\2\2\2\u01c8\u01c1\3\2"+ - "\2\2\u01c8\u01c9\3\2\2\2\u01c9\u01e1\3\2\2\2\u01ca\u01cb\7\30\2\2\u01cb"+ - "\u01cc\5\34\17\2\u01cc\u01cd\7\7\2\2\u01cd\u01ce\7\b\2\2\u01ce\u01d7\7"+ - "\5\2\2\u01cf\u01d4\5$\23\2\u01d0\u01d1\7\r\2\2\u01d1\u01d3\5$\23\2\u01d2"+ - "\u01d0\3\2\2\2\u01d3\u01d6\3\2\2\2\u01d4\u01d2\3\2\2\2\u01d4\u01d5\3\2"+ - "\2\2\u01d5\u01d8\3\2\2\2\u01d6\u01d4\3\2\2\2\u01d7\u01cf\3\2\2\2\u01d7"+ - "\u01d8\3\2\2\2\u01d8\u01d9\3\2\2\2\u01d9\u01dd\7\6\2\2\u01da\u01dc\5\64"+ - "\33\2\u01db\u01da\3\2\2\2\u01dc\u01df\3\2\2\2\u01dd\u01db\3\2\2\2\u01dd"+ - "\u01de\3\2\2\2\u01de\u01e1\3\2\2\2\u01df\u01dd\3\2\2\2\u01e0\u01b7\3\2"+ - "\2\2\u01e0\u01ca\3\2\2\2\u01e1?\3\2\2\2\u01e2\u01e3\7\7\2\2\u01e3\u01e8"+ - "\5$\23\2\u01e4\u01e5\7\r\2\2\u01e5\u01e7\5$\23\2\u01e6\u01e4\3\2\2\2\u01e7"+ - "\u01ea\3\2\2\2\u01e8\u01e6\3\2\2\2\u01e8\u01e9\3\2\2\2\u01e9\u01eb\3\2"+ - "\2\2\u01ea\u01e8\3\2\2\2\u01eb\u01ec\7\b\2\2\u01ec\u01f0\3\2\2\2\u01ed"+ - "\u01ee\7\7\2\2\u01ee\u01f0\7\b\2\2\u01ef\u01e2\3\2\2\2\u01ef\u01ed\3\2"+ - "\2\2\u01f0A\3\2\2\2\u01f1\u01f2\7\7\2\2\u01f2\u01f7\5D#\2\u01f3\u01f4"+ - "\7\r\2\2\u01f4\u01f6\5D#\2\u01f5\u01f3\3\2\2\2\u01f6\u01f9\3\2\2\2\u01f7"+ - "\u01f5\3\2\2\2\u01f7\u01f8\3\2\2\2\u01f8\u01fa\3\2\2\2\u01f9\u01f7\3\2"+ - "\2\2\u01fa\u01fb\7\b\2\2\u01fb\u0200\3\2\2\2\u01fc\u01fd\7\7\2\2\u01fd"+ - "\u01fe\7\66\2\2\u01fe\u0200\7\b\2\2\u01ff\u01f1\3\2\2\2\u01ff\u01fc\3"+ - "\2\2\2\u0200C\3\2\2\2\u0201\u0202\5$\23\2\u0202\u0203\7\66\2\2\u0203\u0204"+ - "\5$\23\2\u0204E\3\2\2\2\u0205\u020e\7\t\2\2\u0206\u020b\5H%\2\u0207\u0208"+ - "\7\r\2\2\u0208\u020a\5H%\2\u0209\u0207\3\2\2\2\u020a\u020d\3\2\2\2\u020b"+ - "\u0209\3\2\2\2\u020b\u020c\3\2\2\2\u020c\u020f\3\2\2\2\u020d\u020b\3\2"+ - "\2\2\u020e\u0206\3\2\2\2\u020e\u020f\3\2\2\2\u020f\u0210\3\2\2\2\u0210"+ - "\u0211\7\n\2\2\u0211G\3\2\2\2\u0212\u0216\5$\23\2\u0213\u0216\5J&\2\u0214"+ - "\u0216\5N(\2\u0215\u0212\3\2\2\2\u0215\u0213\3\2\2\2\u0215\u0214\3\2\2"+ - "\2\u0216I\3\2\2\2\u0217\u0225\5L\'\2\u0218\u0221\7\t\2\2\u0219\u021e\5"+ - "L\'\2\u021a\u021b\7\r\2\2\u021b\u021d\5L\'\2\u021c\u021a\3\2\2\2\u021d"+ - "\u0220\3\2\2\2\u021e\u021c\3\2\2\2\u021e\u021f\3\2\2\2\u021f\u0222\3\2"+ - "\2\2\u0220\u021e\3\2\2\2\u0221\u0219\3\2\2\2\u0221\u0222\3\2\2\2\u0222"+ - "\u0223\3\2\2\2\u0223\u0225\7\n\2\2\u0224\u0217\3\2\2\2\u0224\u0218\3\2"+ - "\2\2\u0225\u0226\3\2\2\2\u0226\u0229\79\2\2\u0227\u022a\5\20\t\2\u0228"+ - "\u022a\5$\23\2\u0229\u0227\3\2\2\2\u0229\u0228\3\2\2\2\u022aK\3\2\2\2"+ - "\u022b\u022d\5\32\16\2\u022c\u022b\3\2\2\2\u022c\u022d\3\2\2\2\u022d\u022e"+ - "\3\2\2\2\u022e\u022f\7U\2\2\u022fM\3\2\2\2\u0230\u0231\5\32\16\2\u0231"+ - "\u0232\78\2\2\u0232\u0233\7U\2\2\u0233\u023c\3\2\2\2\u0234\u0235\5\32"+ - "\16\2\u0235\u0236\78\2\2\u0236\u0237\7\30\2\2\u0237\u023c\3\2\2\2\u0238"+ - "\u0239\7\34\2\2\u0239\u023a\78\2\2\u023a\u023c\7U\2\2\u023b\u0230\3\2"+ - "\2\2\u023b\u0234\3\2\2\2\u023b\u0238\3\2\2\2\u023cO\3\2\2\2>SYlow\u0081"+ - "\u0089\u008e\u0092\u0096\u009b\u00b3\u00b5\u00c3\u00c8\u00cc\u00d2\u00d6"+ - "\u00de\u00e8\u00f0\u00fa\u00fd\u0102\u0135\u0137\u0145\u014c\u0155\u0161"+ - "\u016a\u0171\u0178\u017f\u0182\u0188\u018c\u01a1\u01a6\u01aa\u01bf\u01c5"+ - "\u01c8\u01d4\u01d7\u01dd\u01e0\u01e8\u01ef\u01f7\u01ff\u020b\u020e\u0215"+ - "\u021e\u0221\u0224\u0229\u022c\u023b"; - public static final ATN _ATN = - new ATNDeserializer().deserialize(_serializedATN.toCharArray()); - static { - _decisionToDFA = new DFA[_ATN.getNumberOfDecisions()]; - for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) { - _decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i); - } - } + switch (getInterpreter().adaptivePredict(_input, 59, _ctx)) { + case 1: + _localctx = new ClassfuncrefContext(_localctx); + enterOuterAlt(_localctx, 1); { + setState(558); + decltype(); + setState(559); + match(REF); + setState(560); + match(ID); + } + break; + case 2: + _localctx = new ConstructorfuncrefContext(_localctx); + enterOuterAlt(_localctx, 2); { + setState(562); + decltype(); + setState(563); + match(REF); + setState(564); + match(NEW); + } + break; + case 3: + _localctx = new LocalfuncrefContext(_localctx); + enterOuterAlt(_localctx, 3); { + setState(566); + match(THIS); + setState(567); + match(REF); + setState(568); + match(ID); + } + break; + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; + } + + public boolean sempred(RuleContext _localctx, int ruleIndex, int predIndex) { + switch (ruleIndex) { + case 4: + return rstatement_sempred((RstatementContext) _localctx, predIndex); + case 16: + return noncondexpression_sempred((NoncondexpressionContext) _localctx, predIndex); + } + return true; + } + + private boolean rstatement_sempred(RstatementContext _localctx, int predIndex) { + switch (predIndex) { + case 0: + return _input.LA(1) != ELSE; + } + return true; + } + + private boolean noncondexpression_sempred(NoncondexpressionContext _localctx, int predIndex) { + switch (predIndex) { + case 1: + return precpred(_ctx, 13); + case 2: + return precpred(_ctx, 12); + case 3: + return precpred(_ctx, 11); + case 4: + return precpred(_ctx, 10); + case 5: + return precpred(_ctx, 9); + case 6: + return precpred(_ctx, 7); + case 7: + return precpred(_ctx, 6); + case 8: + return precpred(_ctx, 5); + case 9: + return precpred(_ctx, 4); + case 10: + return precpred(_ctx, 3); + case 11: + return precpred(_ctx, 2); + case 12: + return precpred(_ctx, 1); + case 13: + return precpred(_ctx, 8); + } + return true; + } + + public static final String _serializedATN = "\3\u0430\ud6d1\u8206\uad2d\u4417\uaef1\u8d80\uaadd\3W\u023e\4\2\t\2\4" + + "\3\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b\t\b\4\t\t\t\4\n\t\n\4\13\t" + + "\13\4\f\t\f\4\r\t\r\4\16\t\16\4\17\t\17\4\20\t\20\4\21\t\21\4\22\t\22" + + "\4\23\t\23\4\24\t\24\4\25\t\25\4\26\t\26\4\27\t\27\4\30\t\30\4\31\t\31" + + "\4\32\t\32\4\33\t\33\4\34\t\34\4\35\t\35\4\36\t\36\4\37\t\37\4 \t \4!" + + "\t!\4\"\t\"\4#\t#\4$\t$\4%\t%\4&\t&\4\'\t\'\4(\t(\3\2\7\2R\n\2\f\2\16" + + "\2U\13\2\3\2\7\2X\n\2\f\2\16\2[\13\2\3\2\3\2\3\3\3\3\3\3\3\3\3\3\3\4\3" + + "\4\3\4\3\4\3\4\3\4\3\4\7\4k\n\4\f\4\16\4n\13\4\5\4p\n\4\3\4\3\4\3\5\3" + + "\5\3\5\3\5\5\5x\n\5\3\6\3\6\3\6\3\6\3\6\3\6\3\6\3\6\5\6\u0082\n\6\3\6" + + "\3\6\3\6\3\6\3\6\3\6\5\6\u008a\n\6\3\6\3\6\3\6\5\6\u008f\n\6\3\6\3\6\5" + + "\6\u0093\n\6\3\6\3\6\5\6\u0097\n\6\3\6\3\6\3\6\5\6\u009c\n\6\3\6\3\6\3" + + "\6\3\6\3\6\3\6\3\6\3\6\3\6\3\6\3\6\3\6\3\6\3\6\3\6\3\6\3\6\3\6\3\6\3\6" + + "\6\6\u00b2\n\6\r\6\16\6\u00b3\5\6\u00b6\n\6\3\7\3\7\3\7\3\7\3\7\3\7\3" + + "\7\3\7\3\7\3\7\3\7\3\7\5\7\u00c4\n\7\3\7\3\7\3\7\5\7\u00c9\n\7\3\b\3\b" + + "\5\b\u00cd\n\b\3\t\3\t\7\t\u00d1\n\t\f\t\16\t\u00d4\13\t\3\t\5\t\u00d7" + + "\n\t\3\t\3\t\3\n\3\n\3\13\3\13\5\13\u00df\n\13\3\f\3\f\3\r\3\r\3\r\3\r" + + "\7\r\u00e7\n\r\f\r\16\r\u00ea\13\r\3\16\3\16\3\16\7\16\u00ef\n\16\f\16" + + "\16\16\u00f2\13\16\3\17\3\17\3\17\3\17\3\17\7\17\u00f9\n\17\f\17\16\17" + + "\u00fc\13\17\5\17\u00fe\n\17\3\20\3\20\3\20\5\20\u0103\n\20\3\21\3\21" + + "\3\21\3\21\3\21\3\21\3\21\3\22\3\22\3\22\3\22\3\22\3\22\3\22\3\22\3\22" + + "\3\22\3\22\3\22\3\22\3\22\3\22\3\22\3\22\3\22\3\22\3\22\3\22\3\22\3\22" + + "\3\22\3\22\3\22\3\22\3\22\3\22\3\22\3\22\3\22\3\22\3\22\3\22\3\22\3\22" + + "\3\22\3\22\3\22\3\22\3\22\7\22\u0136\n\22\f\22\16\22\u0139\13\22\3\23" + + "\3\23\3\23\3\23\3\23\3\23\3\23\3\23\3\23\3\23\3\23\5\23\u0146\n\23\3\24" + + "\3\24\3\24\3\24\3\24\5\24\u014d\n\24\3\25\3\25\3\25\3\25\3\25\3\25\3\25" + + "\5\25\u0156\n\25\3\26\3\26\3\26\3\26\3\26\3\26\3\26\3\26\3\26\3\26\5\26" + + "\u0162\n\26\3\27\3\27\3\30\3\30\3\30\6\30\u0169\n\30\r\30\16\30\u016a" + + "\3\30\3\30\3\30\6\30\u0170\n\30\r\30\16\30\u0171\3\30\3\30\3\30\7\30\u0177" + + "\n\30\f\30\16\30\u017a\13\30\3\30\3\30\7\30\u017e\n\30\f\30\16\30\u0181" + + "\13\30\5\30\u0183\n\30\3\31\3\31\7\31\u0187\n\31\f\31\16\31\u018a\13\31" + + "\3\31\5\31\u018d\n\31\3\32\3\32\3\32\3\32\3\32\3\32\3\32\3\32\3\32\3\32" + + "\3\32\3\32\3\32\3\32\3\32\3\32\3\32\3\32\3\32\5\32\u01a2\n\32\3\33\3\33" + + "\3\33\5\33\u01a7\n\33\3\34\3\34\5\34\u01ab\n\34\3\35\3\35\3\35\3\35\3" + + "\36\3\36\3\36\3\37\3\37\3\37\3\37\3 \3 \3 \3 \3 \3 \6 \u01be\n \r \16" + + " \u01bf\3 \3 \7 \u01c4\n \f \16 \u01c7\13 \5 \u01c9\n \3 \3 \3 \3 \3 " + + "\3 \3 \3 \7 \u01d3\n \f \16 \u01d6\13 \5 \u01d8\n \3 \3 \7 \u01dc\n \f" + + " \16 \u01df\13 \5 \u01e1\n \3!\3!\3!\3!\7!\u01e7\n!\f!\16!\u01ea\13!\3" + + "!\3!\3!\3!\5!\u01f0\n!\3\"\3\"\3\"\3\"\7\"\u01f6\n\"\f\"\16\"\u01f9\13" + + "\"\3\"\3\"\3\"\3\"\3\"\5\"\u0200\n\"\3#\3#\3#\3#\3$\3$\3$\3$\7$\u020a" + + "\n$\f$\16$\u020d\13$\5$\u020f\n$\3$\3$\3%\3%\3%\5%\u0216\n%\3&\3&\3&\3" + + "&\3&\7&\u021d\n&\f&\16&\u0220\13&\5&\u0222\n&\3&\5&\u0225\n&\3&\3&\3&" + + "\5&\u022a\n&\3\'\5\'\u022d\n\'\3\'\3\'\3(\3(\3(\3(\3(\3(\3(\3(\3(\3(\3" + + "(\5(\u023c\n(\3(\2\3\")\2\4\6\b\n\f\16\20\22\24\26\30\32\34\36 \"$&(*" + + ",.\60\62\64\668:<>@BDFHJLN\2\20\3\3\16\16\3\2 \"\3\2#$\3\2:;\3\2%\'\3" + + "\2(+\3\2,/\3\2>I\3\2<=\3\2\36\37\3\2ST\3\2JM\3\2\13\f\3\2VW\u0279\2S\3" + + "\2\2\2\4^\3\2\2\2\6c\3\2\2\2\bw\3\2\2\2\n\u00b5\3\2\2\2\f\u00c8\3\2\2" + + "\2\16\u00cc\3\2\2\2\20\u00ce\3\2\2\2\22\u00da\3\2\2\2\24\u00de\3\2\2\2" + + "\26\u00e0\3\2\2\2\30\u00e2\3\2\2\2\32\u00eb\3\2\2\2\34\u00fd\3\2\2\2\36" + + "\u00ff\3\2\2\2 \u0104\3\2\2\2\"\u010b\3\2\2\2$\u0145\3\2\2\2&\u014c\3" + + "\2\2\2(\u0155\3\2\2\2*\u0161\3\2\2\2,\u0163\3\2\2\2.\u0182\3\2\2\2\60" + + "\u018c\3\2\2\2\62\u01a1\3\2\2\2\64\u01a6\3\2\2\2\66\u01aa\3\2\2\28\u01ac" + + "\3\2\2\2:\u01b0\3\2\2\2<\u01b3\3\2\2\2>\u01e0\3\2\2\2@\u01ef\3\2\2\2B" + + "\u01ff\3\2\2\2D\u0201\3\2\2\2F\u0205\3\2\2\2H\u0215\3\2\2\2J\u0224\3\2" + + "\2\2L\u022c\3\2\2\2N\u023b\3\2\2\2PR\5\4\3\2QP\3\2\2\2RU\3\2\2\2SQ\3\2" + + "\2\2ST\3\2\2\2TY\3\2\2\2US\3\2\2\2VX\5\b\5\2WV\3\2\2\2X[\3\2\2\2YW\3\2" + + "\2\2YZ\3\2\2\2Z\\\3\2\2\2[Y\3\2\2\2\\]\7\2\2\3]\3\3\2\2\2^_\5\32\16\2" + + "_`\7U\2\2`a\5\6\4\2ab\5\20\t\2b\5\3\2\2\2co\7\t\2\2de\5\32\16\2el\7U\2" + + "\2fg\7\r\2\2gh\5\32\16\2hi\7U\2\2ik\3\2\2\2jf\3\2\2\2kn\3\2\2\2lj\3\2" + + "\2\2lm\3\2\2\2mp\3\2\2\2nl\3\2\2\2od\3\2\2\2op\3\2\2\2pq\3\2\2\2qr\7\n" + + "\2\2r\7\3\2\2\2sx\5\n\6\2tu\5\f\7\2uv\t\2\2\2vx\3\2\2\2ws\3\2\2\2wt\3" + + "\2\2\2x\t\3\2\2\2yz\7\17\2\2z{\7\t\2\2{|\5$\23\2|}\7\n\2\2}\u0081\5\16" + + "\b\2~\177\7\21\2\2\177\u0082\5\16\b\2\u0080\u0082\6\6\2\2\u0081~\3\2\2" + + "\2\u0081\u0080\3\2\2\2\u0082\u00b6\3\2\2\2\u0083\u0084\7\22\2\2\u0084" + + "\u0085\7\t\2\2\u0085\u0086\5$\23\2\u0086\u0089\7\n\2\2\u0087\u008a\5\16" + + "\b\2\u0088\u008a\5\22\n\2\u0089\u0087\3\2\2\2\u0089\u0088\3\2\2\2\u008a" + + "\u00b6\3\2\2\2\u008b\u008c\7\24\2\2\u008c\u008e\7\t\2\2\u008d\u008f\5" + + "\24\13\2\u008e\u008d\3\2\2\2\u008e\u008f\3\2\2\2\u008f\u0090\3\2\2\2\u0090" + + "\u0092\7\16\2\2\u0091\u0093\5$\23\2\u0092\u0091\3\2\2\2\u0092\u0093\3" + + "\2\2\2\u0093\u0094\3\2\2\2\u0094\u0096\7\16\2\2\u0095\u0097\5\26\f\2\u0096" + + "\u0095\3\2\2\2\u0096\u0097\3\2\2\2\u0097\u0098\3\2\2\2\u0098\u009b\7\n" + + "\2\2\u0099\u009c\5\16\b\2\u009a\u009c\5\22\n\2\u009b\u0099\3\2\2\2\u009b" + + "\u009a\3\2\2\2\u009c\u00b6\3\2\2\2\u009d\u009e\7\24\2\2\u009e\u009f\7" + + "\t\2\2\u009f\u00a0\5\32\16\2\u00a0\u00a1\7U\2\2\u00a1\u00a2\7\66\2\2\u00a2" + + "\u00a3\5$\23\2\u00a3\u00a4\7\n\2\2\u00a4\u00a5\5\16\b\2\u00a5\u00b6\3" + + "\2\2\2\u00a6\u00a7\7\24\2\2\u00a7\u00a8\7\t\2\2\u00a8\u00a9\7U\2\2\u00a9" + + "\u00aa\7\20\2\2\u00aa\u00ab\5$\23\2\u00ab\u00ac\7\n\2\2\u00ac\u00ad\5" + + "\16\b\2\u00ad\u00b6\3\2\2\2\u00ae\u00af\7\31\2\2\u00af\u00b1\5\20\t\2" + + "\u00b0\u00b2\5 \21\2\u00b1\u00b0\3\2\2\2\u00b2\u00b3\3\2\2\2\u00b3\u00b1" + + "\3\2\2\2\u00b3\u00b4\3\2\2\2\u00b4\u00b6\3\2\2\2\u00b5y\3\2\2\2\u00b5" + + "\u0083\3\2\2\2\u00b5\u008b\3\2\2\2\u00b5\u009d\3\2\2\2\u00b5\u00a6\3\2" + + "\2\2\u00b5\u00ae\3\2\2\2\u00b6\13\3\2\2\2\u00b7\u00b8\7\23\2\2\u00b8\u00b9" + + "\5\20\t\2\u00b9\u00ba\7\22\2\2\u00ba\u00bb\7\t\2\2\u00bb\u00bc\5$\23\2" + + "\u00bc\u00bd\7\n\2\2\u00bd\u00c9\3\2\2\2\u00be\u00c9\5\30\r\2\u00bf\u00c9" + + "\7\25\2\2\u00c0\u00c9\7\26\2\2\u00c1\u00c3\7\27\2\2\u00c2\u00c4\5$\23" + + "\2\u00c3\u00c2\3\2\2\2\u00c3\u00c4\3\2\2\2\u00c4\u00c9\3\2\2\2\u00c5\u00c6" + + "\7\33\2\2\u00c6\u00c9\5$\23\2\u00c7\u00c9\5$\23\2\u00c8\u00b7\3\2\2\2" + + "\u00c8\u00be\3\2\2\2\u00c8\u00bf\3\2\2\2\u00c8\u00c0\3\2\2\2\u00c8\u00c1" + + "\3\2\2\2\u00c8\u00c5\3\2\2\2\u00c8\u00c7\3\2\2\2\u00c9\r\3\2\2\2\u00ca" + + "\u00cd\5\20\t\2\u00cb\u00cd\5\b\5\2\u00cc\u00ca\3\2\2\2\u00cc\u00cb\3" + + "\2\2\2\u00cd\17\3\2\2\2\u00ce\u00d2\7\5\2\2\u00cf\u00d1\5\b\5\2\u00d0" + + "\u00cf\3\2\2\2\u00d1\u00d4\3\2\2\2\u00d2\u00d0\3\2\2\2\u00d2\u00d3\3\2" + + "\2\2\u00d3\u00d6\3\2\2\2\u00d4\u00d2\3\2\2\2\u00d5\u00d7\5\f\7\2\u00d6" + + "\u00d5\3\2\2\2\u00d6\u00d7\3\2\2\2\u00d7\u00d8\3\2\2\2\u00d8\u00d9\7\6" + + "\2\2\u00d9\21\3\2\2\2\u00da\u00db\7\16\2\2\u00db\23\3\2\2\2\u00dc\u00df" + + "\5\30\r\2\u00dd\u00df\5$\23\2\u00de\u00dc\3\2\2\2\u00de\u00dd\3\2\2\2" + + "\u00df\25\3\2\2\2\u00e0\u00e1\5$\23\2\u00e1\27\3\2\2\2\u00e2\u00e3\5\32" + + "\16\2\u00e3\u00e8\5\36\20\2\u00e4\u00e5\7\r\2\2\u00e5\u00e7\5\36\20\2" + + "\u00e6\u00e4\3\2\2\2\u00e7\u00ea\3\2\2\2\u00e8\u00e6\3\2\2\2\u00e8\u00e9" + + "\3\2\2\2\u00e9\31\3\2\2\2\u00ea\u00e8\3\2\2\2\u00eb\u00f0\5\34\17\2\u00ec" + + "\u00ed\7\7\2\2\u00ed\u00ef\7\b\2\2\u00ee\u00ec\3\2\2\2\u00ef\u00f2\3\2" + + "\2\2\u00f0\u00ee\3\2\2\2\u00f0\u00f1\3\2\2\2\u00f1\33\3\2\2\2\u00f2\u00f0" + + "\3\2\2\2\u00f3\u00fe\7T\2\2\u00f4\u00fe\7S\2\2\u00f5\u00fa\7U\2\2\u00f6" + + "\u00f7\7\13\2\2\u00f7\u00f9\7W\2\2\u00f8\u00f6\3\2\2\2\u00f9\u00fc\3\2" + + "\2\2\u00fa\u00f8\3\2\2\2\u00fa\u00fb\3\2\2\2\u00fb\u00fe\3\2\2\2\u00fc" + + "\u00fa\3\2\2\2\u00fd\u00f3\3\2\2\2\u00fd\u00f4\3\2\2\2\u00fd\u00f5\3\2" + + "\2\2\u00fe\35\3\2\2\2\u00ff\u0102\7U\2\2\u0100\u0101\7>\2\2\u0101\u0103" + + "\5$\23\2\u0102\u0100\3\2\2\2\u0102\u0103\3\2\2\2\u0103\37\3\2\2\2\u0104" + + "\u0105\7\32\2\2\u0105\u0106\7\t\2\2\u0106\u0107\5\34\17\2\u0107\u0108" + + "\7U\2\2\u0108\u0109\7\n\2\2\u0109\u010a\5\20\t\2\u010a!\3\2\2\2\u010b" + + "\u010c\b\22\1\2\u010c\u010d\5&\24\2\u010d\u0137\3\2\2\2\u010e\u010f\f" + + "\17\2\2\u010f\u0110\t\3\2\2\u0110\u0136\5\"\22\20\u0111\u0112\f\16\2\2" + + "\u0112\u0113\t\4\2\2\u0113\u0136\5\"\22\17\u0114\u0115\f\r\2\2\u0115\u0116" + + "\t\5\2\2\u0116\u0136\5\"\22\16\u0117\u0118\f\f\2\2\u0118\u0119\t\6\2\2" + + "\u0119\u0136\5\"\22\r\u011a\u011b\f\13\2\2\u011b\u011c\t\7\2\2\u011c\u0136" + + "\5\"\22\f\u011d\u011e\f\t\2\2\u011e\u011f\t\b\2\2\u011f\u0136\5\"\22\n" + + "\u0120\u0121\f\b\2\2\u0121\u0122\7\60\2\2\u0122\u0136\5\"\22\t\u0123\u0124" + + "\f\7\2\2\u0124\u0125\7\61\2\2\u0125\u0136\5\"\22\b\u0126\u0127\f\6\2\2" + + "\u0127\u0128\7\62\2\2\u0128\u0136\5\"\22\7\u0129\u012a\f\5\2\2\u012a\u012b" + + "\7\63\2\2\u012b\u0136\5\"\22\6\u012c\u012d\f\4\2\2\u012d\u012e\7\64\2" + + "\2\u012e\u0136\5\"\22\5\u012f\u0130\f\3\2\2\u0130\u0131\7\67\2\2\u0131" + + "\u0136\5\"\22\3\u0132\u0133\f\n\2\2\u0133\u0134\7\35\2\2\u0134\u0136\5" + + "\32\16\2\u0135\u010e\3\2\2\2\u0135\u0111\3\2\2\2\u0135\u0114\3\2\2\2\u0135" + + "\u0117\3\2\2\2\u0135\u011a\3\2\2\2\u0135\u011d\3\2\2\2\u0135\u0120\3\2" + + "\2\2\u0135\u0123\3\2\2\2\u0135\u0126\3\2\2\2\u0135\u0129\3\2\2\2\u0135" + + "\u012c\3\2\2\2\u0135\u012f\3\2\2\2\u0135\u0132\3\2\2\2\u0136\u0139\3\2" + + "\2\2\u0137\u0135\3\2\2\2\u0137\u0138\3\2\2\2\u0138#\3\2\2\2\u0139\u0137" + + "\3\2\2\2\u013a\u0146\5\"\22\2\u013b\u013c\5\"\22\2\u013c\u013d\7\65\2" + + "\2\u013d\u013e\5$\23\2\u013e\u013f\7\66\2\2\u013f\u0140\5$\23\2\u0140" + + "\u0146\3\2\2\2\u0141\u0142\5\"\22\2\u0142\u0143\t\t\2\2\u0143\u0144\5" + + "$\23\2\u0144\u0146\3\2\2\2\u0145\u013a\3\2\2\2\u0145\u013b\3\2\2\2\u0145" + + "\u0141\3\2\2\2\u0146%\3\2\2\2\u0147\u0148\t\n\2\2\u0148\u014d\5\60\31" + + "\2\u0149\u014a\t\4\2\2\u014a\u014d\5&\24\2\u014b\u014d\5(\25\2\u014c\u0147" + + "\3\2\2\2\u014c\u0149\3\2\2\2\u014c\u014b\3\2\2\2\u014d\'\3\2\2\2\u014e" + + "\u0156\5\60\31\2\u014f\u0150\5\60\31\2\u0150\u0151\t\n\2\2\u0151\u0156" + + "\3\2\2\2\u0152\u0153\t\13\2\2\u0153\u0156\5&\24\2\u0154\u0156\5*\26\2" + + "\u0155\u014e\3\2\2\2\u0155\u014f\3\2\2\2\u0155\u0152\3\2\2\2\u0155\u0154" + + "\3\2\2\2\u0156)\3\2\2\2\u0157\u0158\7\t\2\2\u0158\u0159\5,\27\2\u0159" + + "\u015a\7\n\2\2\u015a\u015b\5&\24\2\u015b\u0162\3\2\2\2\u015c\u015d\7\t" + + "\2\2\u015d\u015e\5.\30\2\u015e\u015f\7\n\2\2\u015f\u0160\5(\25\2\u0160" + + "\u0162\3\2\2\2\u0161\u0157\3\2\2\2\u0161\u015c\3\2\2\2\u0162+\3\2\2\2" + + "\u0163\u0164\t\f\2\2\u0164-\3\2\2\2\u0165\u0168\7T\2\2\u0166\u0167\7\7" + + "\2\2\u0167\u0169\7\b\2\2\u0168\u0166\3\2\2\2\u0169\u016a\3\2\2\2\u016a" + + "\u0168\3\2\2\2\u016a\u016b\3\2\2\2\u016b\u0183\3\2\2\2\u016c\u016f\7S" + + "\2\2\u016d\u016e\7\7\2\2\u016e\u0170\7\b\2\2\u016f\u016d\3\2\2\2\u0170" + + "\u0171\3\2\2\2\u0171\u016f\3\2\2\2\u0171\u0172\3\2\2\2\u0172\u0183\3\2" + + "\2\2\u0173\u0178\7U\2\2\u0174\u0175\7\13\2\2\u0175\u0177\7W\2\2\u0176" + + "\u0174\3\2\2\2\u0177\u017a\3\2\2\2\u0178\u0176\3\2\2\2\u0178\u0179\3\2" + + "\2\2\u0179\u017f\3\2\2\2\u017a\u0178\3\2\2\2\u017b\u017c\7\7\2\2\u017c" + + "\u017e\7\b\2\2\u017d\u017b\3\2\2\2\u017e\u0181\3\2\2\2\u017f\u017d\3\2" + + "\2\2\u017f\u0180\3\2\2\2\u0180\u0183\3\2\2\2\u0181\u017f\3\2\2\2\u0182" + + "\u0165\3\2\2\2\u0182\u016c\3\2\2\2\u0182\u0173\3\2\2\2\u0183/\3\2\2\2" + + "\u0184\u0188\5\62\32\2\u0185\u0187\5\64\33\2\u0186\u0185\3\2\2\2\u0187" + + "\u018a\3\2\2\2\u0188\u0186\3\2\2\2\u0188\u0189\3\2\2\2\u0189\u018d\3\2" + + "\2\2\u018a\u0188\3\2\2\2\u018b\u018d\5> \2\u018c\u0184\3\2\2\2\u018c\u018b" + + "\3\2\2\2\u018d\61\3\2\2\2\u018e\u018f\7\t\2\2\u018f\u0190\5$\23\2\u0190" + + "\u0191\7\n\2\2\u0191\u01a2\3\2\2\2\u0192\u01a2\t\r\2\2\u0193\u01a2\7P" + + "\2\2\u0194\u01a2\7Q\2\2\u0195\u01a2\7R\2\2\u0196\u01a2\7N\2\2\u0197\u01a2" + + "\7O\2\2\u0198\u01a2\5@!\2\u0199\u01a2\5B\"\2\u019a\u01a2\7U\2\2\u019b" + + "\u019c\7U\2\2\u019c\u01a2\5F$\2\u019d\u019e\7\30\2\2\u019e\u019f\5\34" + + "\17\2\u019f\u01a0\5F$\2\u01a0\u01a2\3\2\2\2\u01a1\u018e\3\2\2\2\u01a1" + + "\u0192\3\2\2\2\u01a1\u0193\3\2\2\2\u01a1\u0194\3\2\2\2\u01a1\u0195\3\2" + + "\2\2\u01a1\u0196\3\2\2\2\u01a1\u0197\3\2\2\2\u01a1\u0198\3\2\2\2\u01a1" + + "\u0199\3\2\2\2\u01a1\u019a\3\2\2\2\u01a1\u019b\3\2\2\2\u01a1\u019d\3\2" + + "\2\2\u01a2\63\3\2\2\2\u01a3\u01a7\58\35\2\u01a4\u01a7\5:\36\2\u01a5\u01a7" + + "\5<\37\2\u01a6\u01a3\3\2\2\2\u01a6\u01a4\3\2\2\2\u01a6\u01a5\3\2\2\2\u01a7" + + "\65\3\2\2\2\u01a8\u01ab\58\35\2\u01a9\u01ab\5:\36\2\u01aa\u01a8\3\2\2" + + "\2\u01aa\u01a9\3\2\2\2\u01ab\67\3\2\2\2\u01ac\u01ad\t\16\2\2\u01ad\u01ae" + + "\7W\2\2\u01ae\u01af\5F$\2\u01af9\3\2\2\2\u01b0\u01b1\t\16\2\2\u01b1\u01b2" + + "\t\17\2\2\u01b2;\3\2\2\2\u01b3\u01b4\7\7\2\2\u01b4\u01b5\5$\23\2\u01b5" + + "\u01b6\7\b\2\2\u01b6=\3\2\2\2\u01b7\u01b8\7\30\2\2\u01b8\u01bd\5\34\17" + + "\2\u01b9\u01ba\7\7\2\2\u01ba\u01bb\5$\23\2\u01bb\u01bc\7\b\2\2\u01bc\u01be" + + "\3\2\2\2\u01bd\u01b9\3\2\2\2\u01be\u01bf\3\2\2\2\u01bf\u01bd\3\2\2\2\u01bf" + + "\u01c0\3\2\2\2\u01c0\u01c8\3\2\2\2\u01c1\u01c5\5\66\34\2\u01c2\u01c4\5" + + "\64\33\2\u01c3\u01c2\3\2\2\2\u01c4\u01c7\3\2\2\2\u01c5\u01c3\3\2\2\2\u01c5" + + "\u01c6\3\2\2\2\u01c6\u01c9\3\2\2\2\u01c7\u01c5\3\2\2\2\u01c8\u01c1\3\2" + + "\2\2\u01c8\u01c9\3\2\2\2\u01c9\u01e1\3\2\2\2\u01ca\u01cb\7\30\2\2\u01cb" + + "\u01cc\5\34\17\2\u01cc\u01cd\7\7\2\2\u01cd\u01ce\7\b\2\2\u01ce\u01d7\7" + + "\5\2\2\u01cf\u01d4\5$\23\2\u01d0\u01d1\7\r\2\2\u01d1\u01d3\5$\23\2\u01d2" + + "\u01d0\3\2\2\2\u01d3\u01d6\3\2\2\2\u01d4\u01d2\3\2\2\2\u01d4\u01d5\3\2" + + "\2\2\u01d5\u01d8\3\2\2\2\u01d6\u01d4\3\2\2\2\u01d7\u01cf\3\2\2\2\u01d7" + + "\u01d8\3\2\2\2\u01d8\u01d9\3\2\2\2\u01d9\u01dd\7\6\2\2\u01da\u01dc\5\64" + + "\33\2\u01db\u01da\3\2\2\2\u01dc\u01df\3\2\2\2\u01dd\u01db\3\2\2\2\u01dd" + + "\u01de\3\2\2\2\u01de\u01e1\3\2\2\2\u01df\u01dd\3\2\2\2\u01e0\u01b7\3\2" + + "\2\2\u01e0\u01ca\3\2\2\2\u01e1?\3\2\2\2\u01e2\u01e3\7\7\2\2\u01e3\u01e8" + + "\5$\23\2\u01e4\u01e5\7\r\2\2\u01e5\u01e7\5$\23\2\u01e6\u01e4\3\2\2\2\u01e7" + + "\u01ea\3\2\2\2\u01e8\u01e6\3\2\2\2\u01e8\u01e9\3\2\2\2\u01e9\u01eb\3\2" + + "\2\2\u01ea\u01e8\3\2\2\2\u01eb\u01ec\7\b\2\2\u01ec\u01f0\3\2\2\2\u01ed" + + "\u01ee\7\7\2\2\u01ee\u01f0\7\b\2\2\u01ef\u01e2\3\2\2\2\u01ef\u01ed\3\2" + + "\2\2\u01f0A\3\2\2\2\u01f1\u01f2\7\7\2\2\u01f2\u01f7\5D#\2\u01f3\u01f4" + + "\7\r\2\2\u01f4\u01f6\5D#\2\u01f5\u01f3\3\2\2\2\u01f6\u01f9\3\2\2\2\u01f7" + + "\u01f5\3\2\2\2\u01f7\u01f8\3\2\2\2\u01f8\u01fa\3\2\2\2\u01f9\u01f7\3\2" + + "\2\2\u01fa\u01fb\7\b\2\2\u01fb\u0200\3\2\2\2\u01fc\u01fd\7\7\2\2\u01fd" + + "\u01fe\7\66\2\2\u01fe\u0200\7\b\2\2\u01ff\u01f1\3\2\2\2\u01ff\u01fc\3" + + "\2\2\2\u0200C\3\2\2\2\u0201\u0202\5$\23\2\u0202\u0203\7\66\2\2\u0203\u0204" + + "\5$\23\2\u0204E\3\2\2\2\u0205\u020e\7\t\2\2\u0206\u020b\5H%\2\u0207\u0208" + + "\7\r\2\2\u0208\u020a\5H%\2\u0209\u0207\3\2\2\2\u020a\u020d\3\2\2\2\u020b" + + "\u0209\3\2\2\2\u020b\u020c\3\2\2\2\u020c\u020f\3\2\2\2\u020d\u020b\3\2" + + "\2\2\u020e\u0206\3\2\2\2\u020e\u020f\3\2\2\2\u020f\u0210\3\2\2\2\u0210" + + "\u0211\7\n\2\2\u0211G\3\2\2\2\u0212\u0216\5$\23\2\u0213\u0216\5J&\2\u0214" + + "\u0216\5N(\2\u0215\u0212\3\2\2\2\u0215\u0213\3\2\2\2\u0215\u0214\3\2\2" + + "\2\u0216I\3\2\2\2\u0217\u0225\5L\'\2\u0218\u0221\7\t\2\2\u0219\u021e\5" + + "L\'\2\u021a\u021b\7\r\2\2\u021b\u021d\5L\'\2\u021c\u021a\3\2\2\2\u021d" + + "\u0220\3\2\2\2\u021e\u021c\3\2\2\2\u021e\u021f\3\2\2\2\u021f\u0222\3\2" + + "\2\2\u0220\u021e\3\2\2\2\u0221\u0219\3\2\2\2\u0221\u0222\3\2\2\2\u0222" + + "\u0223\3\2\2\2\u0223\u0225\7\n\2\2\u0224\u0217\3\2\2\2\u0224\u0218\3\2" + + "\2\2\u0225\u0226\3\2\2\2\u0226\u0229\79\2\2\u0227\u022a\5\20\t\2\u0228" + + "\u022a\5$\23\2\u0229\u0227\3\2\2\2\u0229\u0228\3\2\2\2\u022aK\3\2\2\2" + + "\u022b\u022d\5\32\16\2\u022c\u022b\3\2\2\2\u022c\u022d\3\2\2\2\u022d\u022e" + + "\3\2\2\2\u022e\u022f\7U\2\2\u022fM\3\2\2\2\u0230\u0231\5\32\16\2\u0231" + + "\u0232\78\2\2\u0232\u0233\7U\2\2\u0233\u023c\3\2\2\2\u0234\u0235\5\32" + + "\16\2\u0235\u0236\78\2\2\u0236\u0237\7\30\2\2\u0237\u023c\3\2\2\2\u0238" + + "\u0239\7\34\2\2\u0239\u023a\78\2\2\u023a\u023c\7U\2\2\u023b\u0230\3\2" + + "\2\2\u023b\u0234\3\2\2\2\u023b\u0238\3\2\2\2\u023cO\3\2\2\2>SYlow\u0081" + + "\u0089\u008e\u0092\u0096\u009b\u00b3\u00b5\u00c3\u00c8\u00cc\u00d2\u00d6" + + "\u00de\u00e8\u00f0\u00fa\u00fd\u0102\u0135\u0137\u0145\u014c\u0155\u0161" + + "\u016a\u0171\u0178\u017f\u0182\u0188\u018c\u01a1\u01a6\u01aa\u01bf\u01c5" + + "\u01c8\u01d4\u01d7\u01dd\u01e0\u01e8\u01ef\u01f7\u01ff\u020b\u020e\u0215" + + "\u021e\u0221\u0224\u0229\u022c\u023b"; + public static final ATN _ATN = new ATNDeserializer().deserialize(_serializedATN.toCharArray()); + static { + _decisionToDFA = new DFA[_ATN.getNumberOfDecisions()]; + for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) { + _decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i); + } + } } diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/PainlessParserBaseVisitor.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/PainlessParserBaseVisitor.java index 2c705c0040ea4..a7203c7101571 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/PainlessParserBaseVisitor.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/PainlessParserBaseVisitor.java @@ -1,5 +1,6 @@ // ANTLR GENERATED CODE: DO NOT EDIT package org.elasticsearch.painless.antlr; + import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor; /** @@ -11,550 +12,861 @@ * operations with no return type. */ class PainlessParserBaseVisitor extends AbstractParseTreeVisitor implements PainlessParserVisitor { - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitSource(PainlessParser.SourceContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitFunction(PainlessParser.FunctionContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitParameters(PainlessParser.ParametersContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitStatement(PainlessParser.StatementContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitIf(PainlessParser.IfContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitWhile(PainlessParser.WhileContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitFor(PainlessParser.ForContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitEach(PainlessParser.EachContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitIneach(PainlessParser.IneachContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitTry(PainlessParser.TryContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitDo(PainlessParser.DoContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitDecl(PainlessParser.DeclContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitContinue(PainlessParser.ContinueContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitBreak(PainlessParser.BreakContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitReturn(PainlessParser.ReturnContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitThrow(PainlessParser.ThrowContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitExpr(PainlessParser.ExprContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitTrailer(PainlessParser.TrailerContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitBlock(PainlessParser.BlockContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitEmpty(PainlessParser.EmptyContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitInitializer(PainlessParser.InitializerContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitAfterthought(PainlessParser.AfterthoughtContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitDeclaration(PainlessParser.DeclarationContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitDecltype(PainlessParser.DecltypeContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitType(PainlessParser.TypeContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitDeclvar(PainlessParser.DeclvarContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitTrap(PainlessParser.TrapContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitSingle(PainlessParser.SingleContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitComp(PainlessParser.CompContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitBool(PainlessParser.BoolContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitBinary(PainlessParser.BinaryContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitElvis(PainlessParser.ElvisContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitInstanceof(PainlessParser.InstanceofContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitNonconditional(PainlessParser.NonconditionalContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitConditional(PainlessParser.ConditionalContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitAssignment(PainlessParser.AssignmentContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitPre(PainlessParser.PreContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitAddsub(PainlessParser.AddsubContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitNotaddsub(PainlessParser.NotaddsubContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitRead(PainlessParser.ReadContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitPost(PainlessParser.PostContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitNot(PainlessParser.NotContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitCast(PainlessParser.CastContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitPrimordefcast(PainlessParser.PrimordefcastContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitRefcast(PainlessParser.RefcastContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitPrimordefcasttype(PainlessParser.PrimordefcasttypeContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitRefcasttype(PainlessParser.RefcasttypeContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitDynamic(PainlessParser.DynamicContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitNewarray(PainlessParser.NewarrayContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitPrecedence(PainlessParser.PrecedenceContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitNumeric(PainlessParser.NumericContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitTrue(PainlessParser.TrueContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitFalse(PainlessParser.FalseContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitNull(PainlessParser.NullContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitString(PainlessParser.StringContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitRegex(PainlessParser.RegexContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitListinit(PainlessParser.ListinitContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitMapinit(PainlessParser.MapinitContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitVariable(PainlessParser.VariableContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitCalllocal(PainlessParser.CalllocalContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitNewobject(PainlessParser.NewobjectContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitPostfix(PainlessParser.PostfixContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitPostdot(PainlessParser.PostdotContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitCallinvoke(PainlessParser.CallinvokeContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitFieldaccess(PainlessParser.FieldaccessContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitBraceaccess(PainlessParser.BraceaccessContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitNewstandardarray(PainlessParser.NewstandardarrayContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitNewinitializedarray(PainlessParser.NewinitializedarrayContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitListinitializer(PainlessParser.ListinitializerContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitMapinitializer(PainlessParser.MapinitializerContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitMaptoken(PainlessParser.MaptokenContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitArguments(PainlessParser.ArgumentsContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitArgument(PainlessParser.ArgumentContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitLambda(PainlessParser.LambdaContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitLamtype(PainlessParser.LamtypeContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitClassfuncref(PainlessParser.ClassfuncrefContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitConstructorfuncref(PainlessParser.ConstructorfuncrefContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitLocalfuncref(PainlessParser.LocalfuncrefContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitSource(PainlessParser.SourceContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitFunction(PainlessParser.FunctionContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitParameters(PainlessParser.ParametersContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitStatement(PainlessParser.StatementContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitIf(PainlessParser.IfContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitWhile(PainlessParser.WhileContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitFor(PainlessParser.ForContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitEach(PainlessParser.EachContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitIneach(PainlessParser.IneachContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitTry(PainlessParser.TryContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitDo(PainlessParser.DoContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitDecl(PainlessParser.DeclContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitContinue(PainlessParser.ContinueContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitBreak(PainlessParser.BreakContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitReturn(PainlessParser.ReturnContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitThrow(PainlessParser.ThrowContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitExpr(PainlessParser.ExprContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitTrailer(PainlessParser.TrailerContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitBlock(PainlessParser.BlockContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitEmpty(PainlessParser.EmptyContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitInitializer(PainlessParser.InitializerContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitAfterthought(PainlessParser.AfterthoughtContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitDeclaration(PainlessParser.DeclarationContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitDecltype(PainlessParser.DecltypeContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitType(PainlessParser.TypeContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitDeclvar(PainlessParser.DeclvarContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitTrap(PainlessParser.TrapContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitSingle(PainlessParser.SingleContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitComp(PainlessParser.CompContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitBool(PainlessParser.BoolContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitBinary(PainlessParser.BinaryContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitElvis(PainlessParser.ElvisContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitInstanceof(PainlessParser.InstanceofContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitNonconditional(PainlessParser.NonconditionalContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitConditional(PainlessParser.ConditionalContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitAssignment(PainlessParser.AssignmentContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitPre(PainlessParser.PreContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitAddsub(PainlessParser.AddsubContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitNotaddsub(PainlessParser.NotaddsubContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitRead(PainlessParser.ReadContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitPost(PainlessParser.PostContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitNot(PainlessParser.NotContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitCast(PainlessParser.CastContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitPrimordefcast(PainlessParser.PrimordefcastContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitRefcast(PainlessParser.RefcastContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitPrimordefcasttype(PainlessParser.PrimordefcasttypeContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitRefcasttype(PainlessParser.RefcasttypeContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitDynamic(PainlessParser.DynamicContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitNewarray(PainlessParser.NewarrayContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitPrecedence(PainlessParser.PrecedenceContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitNumeric(PainlessParser.NumericContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitTrue(PainlessParser.TrueContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitFalse(PainlessParser.FalseContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitNull(PainlessParser.NullContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitString(PainlessParser.StringContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitRegex(PainlessParser.RegexContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitListinit(PainlessParser.ListinitContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitMapinit(PainlessParser.MapinitContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitVariable(PainlessParser.VariableContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitCalllocal(PainlessParser.CalllocalContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitNewobject(PainlessParser.NewobjectContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitPostfix(PainlessParser.PostfixContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitPostdot(PainlessParser.PostdotContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitCallinvoke(PainlessParser.CallinvokeContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitFieldaccess(PainlessParser.FieldaccessContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitBraceaccess(PainlessParser.BraceaccessContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitNewstandardarray(PainlessParser.NewstandardarrayContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitNewinitializedarray(PainlessParser.NewinitializedarrayContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitListinitializer(PainlessParser.ListinitializerContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitMapinitializer(PainlessParser.MapinitializerContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitMaptoken(PainlessParser.MaptokenContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitArguments(PainlessParser.ArgumentsContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitArgument(PainlessParser.ArgumentContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitLambda(PainlessParser.LambdaContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitLamtype(PainlessParser.LamtypeContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitClassfuncref(PainlessParser.ClassfuncrefContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitConstructorfuncref(PainlessParser.ConstructorfuncrefContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitLocalfuncref(PainlessParser.LocalfuncrefContext ctx) { + return visitChildren(ctx); + } } diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/PainlessParserVisitor.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/PainlessParserVisitor.java index 421ca8ffc4f97..f9167091c7131 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/PainlessParserVisitor.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/PainlessParserVisitor.java @@ -1,5 +1,6 @@ // ANTLR GENERATED CODE: DO NOT EDIT package org.elasticsearch.painless.antlr; + import org.antlr.v4.runtime.tree.ParseTreeVisitor; /** @@ -10,522 +11,599 @@ * operations with no return type. */ interface PainlessParserVisitor extends ParseTreeVisitor { - /** - * Visit a parse tree produced by {@link PainlessParser#source}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitSource(PainlessParser.SourceContext ctx); - /** - * Visit a parse tree produced by {@link PainlessParser#function}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitFunction(PainlessParser.FunctionContext ctx); - /** - * Visit a parse tree produced by {@link PainlessParser#parameters}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitParameters(PainlessParser.ParametersContext ctx); - /** - * Visit a parse tree produced by {@link PainlessParser#statement}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitStatement(PainlessParser.StatementContext ctx); - /** - * Visit a parse tree produced by the {@code if} - * labeled alternative in {@link PainlessParser#rstatement}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitIf(PainlessParser.IfContext ctx); - /** - * Visit a parse tree produced by the {@code while} - * labeled alternative in {@link PainlessParser#rstatement}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitWhile(PainlessParser.WhileContext ctx); - /** - * Visit a parse tree produced by the {@code for} - * labeled alternative in {@link PainlessParser#rstatement}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitFor(PainlessParser.ForContext ctx); - /** - * Visit a parse tree produced by the {@code each} - * labeled alternative in {@link PainlessParser#rstatement}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitEach(PainlessParser.EachContext ctx); - /** - * Visit a parse tree produced by the {@code ineach} - * labeled alternative in {@link PainlessParser#rstatement}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitIneach(PainlessParser.IneachContext ctx); - /** - * Visit a parse tree produced by the {@code try} - * labeled alternative in {@link PainlessParser#rstatement}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitTry(PainlessParser.TryContext ctx); - /** - * Visit a parse tree produced by the {@code do} - * labeled alternative in {@link PainlessParser#dstatement}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitDo(PainlessParser.DoContext ctx); - /** - * Visit a parse tree produced by the {@code decl} - * labeled alternative in {@link PainlessParser#dstatement}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitDecl(PainlessParser.DeclContext ctx); - /** - * Visit a parse tree produced by the {@code continue} - * labeled alternative in {@link PainlessParser#dstatement}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitContinue(PainlessParser.ContinueContext ctx); - /** - * Visit a parse tree produced by the {@code break} - * labeled alternative in {@link PainlessParser#dstatement}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitBreak(PainlessParser.BreakContext ctx); - /** - * Visit a parse tree produced by the {@code return} - * labeled alternative in {@link PainlessParser#dstatement}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitReturn(PainlessParser.ReturnContext ctx); - /** - * Visit a parse tree produced by the {@code throw} - * labeled alternative in {@link PainlessParser#dstatement}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitThrow(PainlessParser.ThrowContext ctx); - /** - * Visit a parse tree produced by the {@code expr} - * labeled alternative in {@link PainlessParser#dstatement}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitExpr(PainlessParser.ExprContext ctx); - /** - * Visit a parse tree produced by {@link PainlessParser#trailer}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitTrailer(PainlessParser.TrailerContext ctx); - /** - * Visit a parse tree produced by {@link PainlessParser#block}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitBlock(PainlessParser.BlockContext ctx); - /** - * Visit a parse tree produced by {@link PainlessParser#empty}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitEmpty(PainlessParser.EmptyContext ctx); - /** - * Visit a parse tree produced by {@link PainlessParser#initializer}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitInitializer(PainlessParser.InitializerContext ctx); - /** - * Visit a parse tree produced by {@link PainlessParser#afterthought}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitAfterthought(PainlessParser.AfterthoughtContext ctx); - /** - * Visit a parse tree produced by {@link PainlessParser#declaration}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitDeclaration(PainlessParser.DeclarationContext ctx); - /** - * Visit a parse tree produced by {@link PainlessParser#decltype}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitDecltype(PainlessParser.DecltypeContext ctx); - /** - * Visit a parse tree produced by {@link PainlessParser#type}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitType(PainlessParser.TypeContext ctx); - /** - * Visit a parse tree produced by {@link PainlessParser#declvar}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitDeclvar(PainlessParser.DeclvarContext ctx); - /** - * Visit a parse tree produced by {@link PainlessParser#trap}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitTrap(PainlessParser.TrapContext ctx); - /** - * Visit a parse tree produced by the {@code single} - * labeled alternative in {@link PainlessParser#noncondexpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitSingle(PainlessParser.SingleContext ctx); - /** - * Visit a parse tree produced by the {@code comp} - * labeled alternative in {@link PainlessParser#noncondexpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitComp(PainlessParser.CompContext ctx); - /** - * Visit a parse tree produced by the {@code bool} - * labeled alternative in {@link PainlessParser#noncondexpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitBool(PainlessParser.BoolContext ctx); - /** - * Visit a parse tree produced by the {@code binary} - * labeled alternative in {@link PainlessParser#noncondexpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitBinary(PainlessParser.BinaryContext ctx); - /** - * Visit a parse tree produced by the {@code elvis} - * labeled alternative in {@link PainlessParser#noncondexpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitElvis(PainlessParser.ElvisContext ctx); - /** - * Visit a parse tree produced by the {@code instanceof} - * labeled alternative in {@link PainlessParser#noncondexpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitInstanceof(PainlessParser.InstanceofContext ctx); - /** - * Visit a parse tree produced by the {@code nonconditional} - * labeled alternative in {@link PainlessParser#expression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitNonconditional(PainlessParser.NonconditionalContext ctx); - /** - * Visit a parse tree produced by the {@code conditional} - * labeled alternative in {@link PainlessParser#expression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitConditional(PainlessParser.ConditionalContext ctx); - /** - * Visit a parse tree produced by the {@code assignment} - * labeled alternative in {@link PainlessParser#expression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitAssignment(PainlessParser.AssignmentContext ctx); - /** - * Visit a parse tree produced by the {@code pre} - * labeled alternative in {@link PainlessParser#unary}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitPre(PainlessParser.PreContext ctx); - /** - * Visit a parse tree produced by the {@code addsub} - * labeled alternative in {@link PainlessParser#unary}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitAddsub(PainlessParser.AddsubContext ctx); - /** - * Visit a parse tree produced by the {@code notaddsub} - * labeled alternative in {@link PainlessParser#unary}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitNotaddsub(PainlessParser.NotaddsubContext ctx); - /** - * Visit a parse tree produced by the {@code read} - * labeled alternative in {@link PainlessParser#unarynotaddsub}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitRead(PainlessParser.ReadContext ctx); - /** - * Visit a parse tree produced by the {@code post} - * labeled alternative in {@link PainlessParser#unarynotaddsub}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitPost(PainlessParser.PostContext ctx); - /** - * Visit a parse tree produced by the {@code not} - * labeled alternative in {@link PainlessParser#unarynotaddsub}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitNot(PainlessParser.NotContext ctx); - /** - * Visit a parse tree produced by the {@code cast} - * labeled alternative in {@link PainlessParser#unarynotaddsub}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitCast(PainlessParser.CastContext ctx); - /** - * Visit a parse tree produced by the {@code primordefcast} - * labeled alternative in {@link PainlessParser#castexpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitPrimordefcast(PainlessParser.PrimordefcastContext ctx); - /** - * Visit a parse tree produced by the {@code refcast} - * labeled alternative in {@link PainlessParser#castexpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitRefcast(PainlessParser.RefcastContext ctx); - /** - * Visit a parse tree produced by {@link PainlessParser#primordefcasttype}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitPrimordefcasttype(PainlessParser.PrimordefcasttypeContext ctx); - /** - * Visit a parse tree produced by {@link PainlessParser#refcasttype}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitRefcasttype(PainlessParser.RefcasttypeContext ctx); - /** - * Visit a parse tree produced by the {@code dynamic} - * labeled alternative in {@link PainlessParser#chain}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitDynamic(PainlessParser.DynamicContext ctx); - /** - * Visit a parse tree produced by the {@code newarray} - * labeled alternative in {@link PainlessParser#chain}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitNewarray(PainlessParser.NewarrayContext ctx); - /** - * Visit a parse tree produced by the {@code precedence} - * labeled alternative in {@link PainlessParser#primary}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitPrecedence(PainlessParser.PrecedenceContext ctx); - /** - * Visit a parse tree produced by the {@code numeric} - * labeled alternative in {@link PainlessParser#primary}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitNumeric(PainlessParser.NumericContext ctx); - /** - * Visit a parse tree produced by the {@code true} - * labeled alternative in {@link PainlessParser#primary}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitTrue(PainlessParser.TrueContext ctx); - /** - * Visit a parse tree produced by the {@code false} - * labeled alternative in {@link PainlessParser#primary}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitFalse(PainlessParser.FalseContext ctx); - /** - * Visit a parse tree produced by the {@code null} - * labeled alternative in {@link PainlessParser#primary}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitNull(PainlessParser.NullContext ctx); - /** - * Visit a parse tree produced by the {@code string} - * labeled alternative in {@link PainlessParser#primary}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitString(PainlessParser.StringContext ctx); - /** - * Visit a parse tree produced by the {@code regex} - * labeled alternative in {@link PainlessParser#primary}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitRegex(PainlessParser.RegexContext ctx); - /** - * Visit a parse tree produced by the {@code listinit} - * labeled alternative in {@link PainlessParser#primary}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitListinit(PainlessParser.ListinitContext ctx); - /** - * Visit a parse tree produced by the {@code mapinit} - * labeled alternative in {@link PainlessParser#primary}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitMapinit(PainlessParser.MapinitContext ctx); - /** - * Visit a parse tree produced by the {@code variable} - * labeled alternative in {@link PainlessParser#primary}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitVariable(PainlessParser.VariableContext ctx); - /** - * Visit a parse tree produced by the {@code calllocal} - * labeled alternative in {@link PainlessParser#primary}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitCalllocal(PainlessParser.CalllocalContext ctx); - /** - * Visit a parse tree produced by the {@code newobject} - * labeled alternative in {@link PainlessParser#primary}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitNewobject(PainlessParser.NewobjectContext ctx); - /** - * Visit a parse tree produced by {@link PainlessParser#postfix}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitPostfix(PainlessParser.PostfixContext ctx); - /** - * Visit a parse tree produced by {@link PainlessParser#postdot}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitPostdot(PainlessParser.PostdotContext ctx); - /** - * Visit a parse tree produced by {@link PainlessParser#callinvoke}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitCallinvoke(PainlessParser.CallinvokeContext ctx); - /** - * Visit a parse tree produced by {@link PainlessParser#fieldaccess}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitFieldaccess(PainlessParser.FieldaccessContext ctx); - /** - * Visit a parse tree produced by {@link PainlessParser#braceaccess}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitBraceaccess(PainlessParser.BraceaccessContext ctx); - /** - * Visit a parse tree produced by the {@code newstandardarray} - * labeled alternative in {@link PainlessParser#arrayinitializer}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitNewstandardarray(PainlessParser.NewstandardarrayContext ctx); - /** - * Visit a parse tree produced by the {@code newinitializedarray} - * labeled alternative in {@link PainlessParser#arrayinitializer}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitNewinitializedarray(PainlessParser.NewinitializedarrayContext ctx); - /** - * Visit a parse tree produced by {@link PainlessParser#listinitializer}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitListinitializer(PainlessParser.ListinitializerContext ctx); - /** - * Visit a parse tree produced by {@link PainlessParser#mapinitializer}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitMapinitializer(PainlessParser.MapinitializerContext ctx); - /** - * Visit a parse tree produced by {@link PainlessParser#maptoken}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitMaptoken(PainlessParser.MaptokenContext ctx); - /** - * Visit a parse tree produced by {@link PainlessParser#arguments}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitArguments(PainlessParser.ArgumentsContext ctx); - /** - * Visit a parse tree produced by {@link PainlessParser#argument}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitArgument(PainlessParser.ArgumentContext ctx); - /** - * Visit a parse tree produced by {@link PainlessParser#lambda}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitLambda(PainlessParser.LambdaContext ctx); - /** - * Visit a parse tree produced by {@link PainlessParser#lamtype}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitLamtype(PainlessParser.LamtypeContext ctx); - /** - * Visit a parse tree produced by the {@code classfuncref} - * labeled alternative in {@link PainlessParser#funcref}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitClassfuncref(PainlessParser.ClassfuncrefContext ctx); - /** - * Visit a parse tree produced by the {@code constructorfuncref} - * labeled alternative in {@link PainlessParser#funcref}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitConstructorfuncref(PainlessParser.ConstructorfuncrefContext ctx); - /** - * Visit a parse tree produced by the {@code localfuncref} - * labeled alternative in {@link PainlessParser#funcref}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitLocalfuncref(PainlessParser.LocalfuncrefContext ctx); + /** + * Visit a parse tree produced by {@link PainlessParser#source}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitSource(PainlessParser.SourceContext ctx); + + /** + * Visit a parse tree produced by {@link PainlessParser#function}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitFunction(PainlessParser.FunctionContext ctx); + + /** + * Visit a parse tree produced by {@link PainlessParser#parameters}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitParameters(PainlessParser.ParametersContext ctx); + + /** + * Visit a parse tree produced by {@link PainlessParser#statement}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitStatement(PainlessParser.StatementContext ctx); + + /** + * Visit a parse tree produced by the {@code if} + * labeled alternative in {@link PainlessParser#rstatement}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitIf(PainlessParser.IfContext ctx); + + /** + * Visit a parse tree produced by the {@code while} + * labeled alternative in {@link PainlessParser#rstatement}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitWhile(PainlessParser.WhileContext ctx); + + /** + * Visit a parse tree produced by the {@code for} + * labeled alternative in {@link PainlessParser#rstatement}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitFor(PainlessParser.ForContext ctx); + + /** + * Visit a parse tree produced by the {@code each} + * labeled alternative in {@link PainlessParser#rstatement}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitEach(PainlessParser.EachContext ctx); + + /** + * Visit a parse tree produced by the {@code ineach} + * labeled alternative in {@link PainlessParser#rstatement}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitIneach(PainlessParser.IneachContext ctx); + + /** + * Visit a parse tree produced by the {@code try} + * labeled alternative in {@link PainlessParser#rstatement}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitTry(PainlessParser.TryContext ctx); + + /** + * Visit a parse tree produced by the {@code do} + * labeled alternative in {@link PainlessParser#dstatement}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitDo(PainlessParser.DoContext ctx); + + /** + * Visit a parse tree produced by the {@code decl} + * labeled alternative in {@link PainlessParser#dstatement}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitDecl(PainlessParser.DeclContext ctx); + + /** + * Visit a parse tree produced by the {@code continue} + * labeled alternative in {@link PainlessParser#dstatement}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitContinue(PainlessParser.ContinueContext ctx); + + /** + * Visit a parse tree produced by the {@code break} + * labeled alternative in {@link PainlessParser#dstatement}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitBreak(PainlessParser.BreakContext ctx); + + /** + * Visit a parse tree produced by the {@code return} + * labeled alternative in {@link PainlessParser#dstatement}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitReturn(PainlessParser.ReturnContext ctx); + + /** + * Visit a parse tree produced by the {@code throw} + * labeled alternative in {@link PainlessParser#dstatement}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitThrow(PainlessParser.ThrowContext ctx); + + /** + * Visit a parse tree produced by the {@code expr} + * labeled alternative in {@link PainlessParser#dstatement}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitExpr(PainlessParser.ExprContext ctx); + + /** + * Visit a parse tree produced by {@link PainlessParser#trailer}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitTrailer(PainlessParser.TrailerContext ctx); + + /** + * Visit a parse tree produced by {@link PainlessParser#block}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitBlock(PainlessParser.BlockContext ctx); + + /** + * Visit a parse tree produced by {@link PainlessParser#empty}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitEmpty(PainlessParser.EmptyContext ctx); + + /** + * Visit a parse tree produced by {@link PainlessParser#initializer}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitInitializer(PainlessParser.InitializerContext ctx); + + /** + * Visit a parse tree produced by {@link PainlessParser#afterthought}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitAfterthought(PainlessParser.AfterthoughtContext ctx); + + /** + * Visit a parse tree produced by {@link PainlessParser#declaration}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitDeclaration(PainlessParser.DeclarationContext ctx); + + /** + * Visit a parse tree produced by {@link PainlessParser#decltype}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitDecltype(PainlessParser.DecltypeContext ctx); + + /** + * Visit a parse tree produced by {@link PainlessParser#type}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitType(PainlessParser.TypeContext ctx); + + /** + * Visit a parse tree produced by {@link PainlessParser#declvar}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitDeclvar(PainlessParser.DeclvarContext ctx); + + /** + * Visit a parse tree produced by {@link PainlessParser#trap}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitTrap(PainlessParser.TrapContext ctx); + + /** + * Visit a parse tree produced by the {@code single} + * labeled alternative in {@link PainlessParser#noncondexpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitSingle(PainlessParser.SingleContext ctx); + + /** + * Visit a parse tree produced by the {@code comp} + * labeled alternative in {@link PainlessParser#noncondexpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitComp(PainlessParser.CompContext ctx); + + /** + * Visit a parse tree produced by the {@code bool} + * labeled alternative in {@link PainlessParser#noncondexpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitBool(PainlessParser.BoolContext ctx); + + /** + * Visit a parse tree produced by the {@code binary} + * labeled alternative in {@link PainlessParser#noncondexpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitBinary(PainlessParser.BinaryContext ctx); + + /** + * Visit a parse tree produced by the {@code elvis} + * labeled alternative in {@link PainlessParser#noncondexpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitElvis(PainlessParser.ElvisContext ctx); + + /** + * Visit a parse tree produced by the {@code instanceof} + * labeled alternative in {@link PainlessParser#noncondexpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitInstanceof(PainlessParser.InstanceofContext ctx); + + /** + * Visit a parse tree produced by the {@code nonconditional} + * labeled alternative in {@link PainlessParser#expression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitNonconditional(PainlessParser.NonconditionalContext ctx); + + /** + * Visit a parse tree produced by the {@code conditional} + * labeled alternative in {@link PainlessParser#expression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitConditional(PainlessParser.ConditionalContext ctx); + + /** + * Visit a parse tree produced by the {@code assignment} + * labeled alternative in {@link PainlessParser#expression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitAssignment(PainlessParser.AssignmentContext ctx); + + /** + * Visit a parse tree produced by the {@code pre} + * labeled alternative in {@link PainlessParser#unary}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitPre(PainlessParser.PreContext ctx); + + /** + * Visit a parse tree produced by the {@code addsub} + * labeled alternative in {@link PainlessParser#unary}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitAddsub(PainlessParser.AddsubContext ctx); + + /** + * Visit a parse tree produced by the {@code notaddsub} + * labeled alternative in {@link PainlessParser#unary}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitNotaddsub(PainlessParser.NotaddsubContext ctx); + + /** + * Visit a parse tree produced by the {@code read} + * labeled alternative in {@link PainlessParser#unarynotaddsub}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitRead(PainlessParser.ReadContext ctx); + + /** + * Visit a parse tree produced by the {@code post} + * labeled alternative in {@link PainlessParser#unarynotaddsub}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitPost(PainlessParser.PostContext ctx); + + /** + * Visit a parse tree produced by the {@code not} + * labeled alternative in {@link PainlessParser#unarynotaddsub}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitNot(PainlessParser.NotContext ctx); + + /** + * Visit a parse tree produced by the {@code cast} + * labeled alternative in {@link PainlessParser#unarynotaddsub}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitCast(PainlessParser.CastContext ctx); + + /** + * Visit a parse tree produced by the {@code primordefcast} + * labeled alternative in {@link PainlessParser#castexpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitPrimordefcast(PainlessParser.PrimordefcastContext ctx); + + /** + * Visit a parse tree produced by the {@code refcast} + * labeled alternative in {@link PainlessParser#castexpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitRefcast(PainlessParser.RefcastContext ctx); + + /** + * Visit a parse tree produced by {@link PainlessParser#primordefcasttype}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitPrimordefcasttype(PainlessParser.PrimordefcasttypeContext ctx); + + /** + * Visit a parse tree produced by {@link PainlessParser#refcasttype}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitRefcasttype(PainlessParser.RefcasttypeContext ctx); + + /** + * Visit a parse tree produced by the {@code dynamic} + * labeled alternative in {@link PainlessParser#chain}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitDynamic(PainlessParser.DynamicContext ctx); + + /** + * Visit a parse tree produced by the {@code newarray} + * labeled alternative in {@link PainlessParser#chain}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitNewarray(PainlessParser.NewarrayContext ctx); + + /** + * Visit a parse tree produced by the {@code precedence} + * labeled alternative in {@link PainlessParser#primary}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitPrecedence(PainlessParser.PrecedenceContext ctx); + + /** + * Visit a parse tree produced by the {@code numeric} + * labeled alternative in {@link PainlessParser#primary}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitNumeric(PainlessParser.NumericContext ctx); + + /** + * Visit a parse tree produced by the {@code true} + * labeled alternative in {@link PainlessParser#primary}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitTrue(PainlessParser.TrueContext ctx); + + /** + * Visit a parse tree produced by the {@code false} + * labeled alternative in {@link PainlessParser#primary}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitFalse(PainlessParser.FalseContext ctx); + + /** + * Visit a parse tree produced by the {@code null} + * labeled alternative in {@link PainlessParser#primary}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitNull(PainlessParser.NullContext ctx); + + /** + * Visit a parse tree produced by the {@code string} + * labeled alternative in {@link PainlessParser#primary}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitString(PainlessParser.StringContext ctx); + + /** + * Visit a parse tree produced by the {@code regex} + * labeled alternative in {@link PainlessParser#primary}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitRegex(PainlessParser.RegexContext ctx); + + /** + * Visit a parse tree produced by the {@code listinit} + * labeled alternative in {@link PainlessParser#primary}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitListinit(PainlessParser.ListinitContext ctx); + + /** + * Visit a parse tree produced by the {@code mapinit} + * labeled alternative in {@link PainlessParser#primary}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitMapinit(PainlessParser.MapinitContext ctx); + + /** + * Visit a parse tree produced by the {@code variable} + * labeled alternative in {@link PainlessParser#primary}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitVariable(PainlessParser.VariableContext ctx); + + /** + * Visit a parse tree produced by the {@code calllocal} + * labeled alternative in {@link PainlessParser#primary}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitCalllocal(PainlessParser.CalllocalContext ctx); + + /** + * Visit a parse tree produced by the {@code newobject} + * labeled alternative in {@link PainlessParser#primary}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitNewobject(PainlessParser.NewobjectContext ctx); + + /** + * Visit a parse tree produced by {@link PainlessParser#postfix}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitPostfix(PainlessParser.PostfixContext ctx); + + /** + * Visit a parse tree produced by {@link PainlessParser#postdot}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitPostdot(PainlessParser.PostdotContext ctx); + + /** + * Visit a parse tree produced by {@link PainlessParser#callinvoke}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitCallinvoke(PainlessParser.CallinvokeContext ctx); + + /** + * Visit a parse tree produced by {@link PainlessParser#fieldaccess}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitFieldaccess(PainlessParser.FieldaccessContext ctx); + + /** + * Visit a parse tree produced by {@link PainlessParser#braceaccess}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitBraceaccess(PainlessParser.BraceaccessContext ctx); + + /** + * Visit a parse tree produced by the {@code newstandardarray} + * labeled alternative in {@link PainlessParser#arrayinitializer}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitNewstandardarray(PainlessParser.NewstandardarrayContext ctx); + + /** + * Visit a parse tree produced by the {@code newinitializedarray} + * labeled alternative in {@link PainlessParser#arrayinitializer}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitNewinitializedarray(PainlessParser.NewinitializedarrayContext ctx); + + /** + * Visit a parse tree produced by {@link PainlessParser#listinitializer}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitListinitializer(PainlessParser.ListinitializerContext ctx); + + /** + * Visit a parse tree produced by {@link PainlessParser#mapinitializer}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitMapinitializer(PainlessParser.MapinitializerContext ctx); + + /** + * Visit a parse tree produced by {@link PainlessParser#maptoken}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitMaptoken(PainlessParser.MaptokenContext ctx); + + /** + * Visit a parse tree produced by {@link PainlessParser#arguments}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitArguments(PainlessParser.ArgumentsContext ctx); + + /** + * Visit a parse tree produced by {@link PainlessParser#argument}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitArgument(PainlessParser.ArgumentContext ctx); + + /** + * Visit a parse tree produced by {@link PainlessParser#lambda}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitLambda(PainlessParser.LambdaContext ctx); + + /** + * Visit a parse tree produced by {@link PainlessParser#lamtype}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitLamtype(PainlessParser.LamtypeContext ctx); + + /** + * Visit a parse tree produced by the {@code classfuncref} + * labeled alternative in {@link PainlessParser#funcref}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitClassfuncref(PainlessParser.ClassfuncrefContext ctx); + + /** + * Visit a parse tree produced by the {@code constructorfuncref} + * labeled alternative in {@link PainlessParser#funcref}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitConstructorfuncref(PainlessParser.ConstructorfuncrefContext ctx); + + /** + * Visit a parse tree produced by the {@code localfuncref} + * labeled alternative in {@link PainlessParser#funcref}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitLocalfuncref(PainlessParser.LocalfuncrefContext ctx); } diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/ParserErrorStrategy.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/ParserErrorStrategy.java index 96aeb9d651e17..4ab2f8296d459 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/ParserErrorStrategy.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/ParserErrorStrategy.java @@ -34,8 +34,12 @@ public void recover(final Parser recognizer, final RecognitionException re) { if (token == null) { message = "no parse token found."; } else if (re instanceof InputMismatchException) { - message = "unexpected token [" + getTokenErrorDisplay(token) + "]" + - " was expecting one of [" + re.getExpectedTokens().toString(recognizer.getVocabulary()) + "]."; + message = "unexpected token [" + + getTokenErrorDisplay(token) + + "]" + + " was expecting one of [" + + re.getExpectedTokens().toString(recognizer.getVocabulary()) + + "]."; } else if (re instanceof NoViableAltException) { if (token.getType() == PainlessParser.EOF) { message = "unexpected end of script."; @@ -43,7 +47,7 @@ public void recover(final Parser recognizer, final RecognitionException re) { message = "invalid sequence of tokens near [" + getTokenErrorDisplay(token) + "]."; } } else { - message = "unexpected token near [" + getTokenErrorDisplay(token) + "]."; + message = "unexpected token near [" + getTokenErrorDisplay(token) + "]."; } Location location = new Location(sourceName, token == null ? -1 : token.getStartIndex()); @@ -53,14 +57,17 @@ public void recover(final Parser recognizer, final RecognitionException re) { @Override public Token recoverInline(final Parser recognizer) throws RecognitionException { final Token token = recognizer.getCurrentToken(); - final String message = "unexpected token [" + getTokenErrorDisplay(token) + "]" + - " was expecting one of [" + recognizer.getExpectedTokens().toString(recognizer.getVocabulary()) + "]."; + final String message = "unexpected token [" + + getTokenErrorDisplay(token) + + "]" + + " was expecting one of [" + + recognizer.getExpectedTokens().toString(recognizer.getVocabulary()) + + "]."; Location location = new Location(sourceName, token.getStartIndex()); throw location.createError(new IllegalArgumentException(message)); } @Override - public void sync(final Parser recognizer) { - } + public void sync(final Parser recognizer) {} } diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/SuggestLexer.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/SuggestLexer.java index 90c410c46c8a0..0bad35d925f25 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/SuggestLexer.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/SuggestLexer.java @@ -1,389 +1,615 @@ // ANTLR GENERATED CODE: DO NOT EDIT package org.elasticsearch.painless.antlr; -import org.antlr.v4.runtime.Lexer; -import org.antlr.v4.runtime.CharStream; -import org.antlr.v4.runtime.Token; -import org.antlr.v4.runtime.TokenStream; + import org.antlr.v4.runtime.*; +import org.antlr.v4.runtime.CharStream; +import org.antlr.v4.runtime.Lexer; import org.antlr.v4.runtime.atn.*; import org.antlr.v4.runtime.dfa.DFA; import org.antlr.v4.runtime.misc.*; -@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"}) +@SuppressWarnings({ "all", "warnings", "unchecked", "unused", "cast" }) public abstract class SuggestLexer extends Lexer { - static { RuntimeMetaData.checkVersion("4.5.3", RuntimeMetaData.VERSION); } + static { + RuntimeMetaData.checkVersion("4.5.3", RuntimeMetaData.VERSION); + } - protected static final DFA[] _decisionToDFA; - protected static final PredictionContextCache _sharedContextCache = - new PredictionContextCache(); - public static final int - WS=1, COMMENT=2, LBRACK=3, RBRACK=4, LBRACE=5, RBRACE=6, LP=7, RP=8, DOT=9, - NSDOT=10, COMMA=11, SEMICOLON=12, IF=13, IN=14, ELSE=15, WHILE=16, DO=17, - FOR=18, CONTINUE=19, BREAK=20, RETURN=21, NEW=22, TRY=23, CATCH=24, THROW=25, - THIS=26, INSTANCEOF=27, BOOLNOT=28, BWNOT=29, MUL=30, DIV=31, REM=32, - ADD=33, SUB=34, LSH=35, RSH=36, USH=37, LT=38, LTE=39, GT=40, GTE=41, - EQ=42, EQR=43, NE=44, NER=45, BWAND=46, XOR=47, BWOR=48, BOOLAND=49, BOOLOR=50, - COND=51, COLON=52, ELVIS=53, REF=54, ARROW=55, FIND=56, MATCH=57, INCR=58, - DECR=59, ASSIGN=60, AADD=61, ASUB=62, AMUL=63, ADIV=64, AREM=65, AAND=66, - AXOR=67, AOR=68, ALSH=69, ARSH=70, AUSH=71, OCTAL=72, HEX=73, INTEGER=74, - DECIMAL=75, STRING=76, REGEX=77, TRUE=78, FALSE=79, NULL=80, ATYPE=81, - TYPE=82, ID=83, UNKNOWN=84, DOTINTEGER=85, DOTID=86; - public static final int AFTER_DOT = 1; - public static String[] modeNames = { - "DEFAULT_MODE", "AFTER_DOT" - }; + protected static final DFA[] _decisionToDFA; + protected static final PredictionContextCache _sharedContextCache = new PredictionContextCache(); + public static final int WS = 1, COMMENT = 2, LBRACK = 3, RBRACK = 4, LBRACE = 5, RBRACE = 6, LP = 7, RP = 8, DOT = 9, NSDOT = 10, + COMMA = 11, SEMICOLON = 12, IF = 13, IN = 14, ELSE = 15, WHILE = 16, DO = 17, FOR = 18, CONTINUE = 19, BREAK = 20, RETURN = 21, + NEW = 22, TRY = 23, CATCH = 24, THROW = 25, THIS = 26, INSTANCEOF = 27, BOOLNOT = 28, BWNOT = 29, MUL = 30, DIV = 31, REM = 32, + ADD = 33, SUB = 34, LSH = 35, RSH = 36, USH = 37, LT = 38, LTE = 39, GT = 40, GTE = 41, EQ = 42, EQR = 43, NE = 44, NER = 45, + BWAND = 46, XOR = 47, BWOR = 48, BOOLAND = 49, BOOLOR = 50, COND = 51, COLON = 52, ELVIS = 53, REF = 54, ARROW = 55, FIND = 56, + MATCH = 57, INCR = 58, DECR = 59, ASSIGN = 60, AADD = 61, ASUB = 62, AMUL = 63, ADIV = 64, AREM = 65, AAND = 66, AXOR = 67, AOR = + 68, ALSH = 69, ARSH = 70, AUSH = 71, OCTAL = 72, HEX = 73, INTEGER = 74, DECIMAL = 75, STRING = 76, REGEX = 77, TRUE = 78, + FALSE = 79, NULL = 80, ATYPE = 81, TYPE = 82, ID = 83, UNKNOWN = 84, DOTINTEGER = 85, DOTID = 86; + public static final int AFTER_DOT = 1; + public static String[] modeNames = { "DEFAULT_MODE", "AFTER_DOT" }; - public static final String[] ruleNames = { - "WS", "COMMENT", "LBRACK", "RBRACK", "LBRACE", "RBRACE", "LP", "RP", "DOT", - "NSDOT", "COMMA", "SEMICOLON", "IF", "IN", "ELSE", "WHILE", "DO", "FOR", - "CONTINUE", "BREAK", "RETURN", "NEW", "TRY", "CATCH", "THROW", "THIS", - "INSTANCEOF", "BOOLNOT", "BWNOT", "MUL", "DIV", "REM", "ADD", "SUB", "LSH", - "RSH", "USH", "LT", "LTE", "GT", "GTE", "EQ", "EQR", "NE", "NER", "BWAND", - "XOR", "BWOR", "BOOLAND", "BOOLOR", "COND", "COLON", "ELVIS", "REF", "ARROW", - "FIND", "MATCH", "INCR", "DECR", "ASSIGN", "AADD", "ASUB", "AMUL", "ADIV", - "AREM", "AAND", "AXOR", "AOR", "ALSH", "ARSH", "AUSH", "OCTAL", "HEX", - "INTEGER", "DECIMAL", "STRING", "REGEX", "TRUE", "FALSE", "NULL", "ATYPE", - "TYPE", "ID", "UNKNOWN", "DOTINTEGER", "DOTID" - }; + public static final String[] ruleNames = { + "WS", + "COMMENT", + "LBRACK", + "RBRACK", + "LBRACE", + "RBRACE", + "LP", + "RP", + "DOT", + "NSDOT", + "COMMA", + "SEMICOLON", + "IF", + "IN", + "ELSE", + "WHILE", + "DO", + "FOR", + "CONTINUE", + "BREAK", + "RETURN", + "NEW", + "TRY", + "CATCH", + "THROW", + "THIS", + "INSTANCEOF", + "BOOLNOT", + "BWNOT", + "MUL", + "DIV", + "REM", + "ADD", + "SUB", + "LSH", + "RSH", + "USH", + "LT", + "LTE", + "GT", + "GTE", + "EQ", + "EQR", + "NE", + "NER", + "BWAND", + "XOR", + "BWOR", + "BOOLAND", + "BOOLOR", + "COND", + "COLON", + "ELVIS", + "REF", + "ARROW", + "FIND", + "MATCH", + "INCR", + "DECR", + "ASSIGN", + "AADD", + "ASUB", + "AMUL", + "ADIV", + "AREM", + "AAND", + "AXOR", + "AOR", + "ALSH", + "ARSH", + "AUSH", + "OCTAL", + "HEX", + "INTEGER", + "DECIMAL", + "STRING", + "REGEX", + "TRUE", + "FALSE", + "NULL", + "ATYPE", + "TYPE", + "ID", + "UNKNOWN", + "DOTINTEGER", + "DOTID" }; - private static final String[] _LITERAL_NAMES = { - null, null, null, "'{'", "'}'", "'['", "']'", "'('", "')'", "'.'", "'?.'", - "','", "';'", "'if'", "'in'", "'else'", "'while'", "'do'", "'for'", "'continue'", - "'break'", "'return'", "'new'", "'try'", "'catch'", "'throw'", "'this'", - "'instanceof'", "'!'", "'~'", "'*'", "'/'", "'%'", "'+'", "'-'", "'<<'", - "'>>'", "'>>>'", "'<'", "'<='", "'>'", "'>='", "'=='", "'==='", "'!='", - "'!=='", "'&'", "'^'", "'|'", "'&&'", "'||'", "'?'", "':'", "'?:'", "'::'", - "'->'", "'=~'", "'==~'", "'++'", "'--'", "'='", "'+='", "'-='", "'*='", - "'/='", "'%='", "'&='", "'^='", "'|='", "'<<='", "'>>='", "'>>>='", null, - null, null, null, null, null, "'true'", "'false'", "'null'" - }; - private static final String[] _SYMBOLIC_NAMES = { - null, "WS", "COMMENT", "LBRACK", "RBRACK", "LBRACE", "RBRACE", "LP", "RP", - "DOT", "NSDOT", "COMMA", "SEMICOLON", "IF", "IN", "ELSE", "WHILE", "DO", - "FOR", "CONTINUE", "BREAK", "RETURN", "NEW", "TRY", "CATCH", "THROW", - "THIS", "INSTANCEOF", "BOOLNOT", "BWNOT", "MUL", "DIV", "REM", "ADD", - "SUB", "LSH", "RSH", "USH", "LT", "LTE", "GT", "GTE", "EQ", "EQR", "NE", - "NER", "BWAND", "XOR", "BWOR", "BOOLAND", "BOOLOR", "COND", "COLON", "ELVIS", - "REF", "ARROW", "FIND", "MATCH", "INCR", "DECR", "ASSIGN", "AADD", "ASUB", - "AMUL", "ADIV", "AREM", "AAND", "AXOR", "AOR", "ALSH", "ARSH", "AUSH", - "OCTAL", "HEX", "INTEGER", "DECIMAL", "STRING", "REGEX", "TRUE", "FALSE", - "NULL", "ATYPE", "TYPE", "ID", "UNKNOWN", "DOTINTEGER", "DOTID" - }; - public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); + private static final String[] _LITERAL_NAMES = { + null, + null, + null, + "'{'", + "'}'", + "'['", + "']'", + "'('", + "')'", + "'.'", + "'?.'", + "','", + "';'", + "'if'", + "'in'", + "'else'", + "'while'", + "'do'", + "'for'", + "'continue'", + "'break'", + "'return'", + "'new'", + "'try'", + "'catch'", + "'throw'", + "'this'", + "'instanceof'", + "'!'", + "'~'", + "'*'", + "'/'", + "'%'", + "'+'", + "'-'", + "'<<'", + "'>>'", + "'>>>'", + "'<'", + "'<='", + "'>'", + "'>='", + "'=='", + "'==='", + "'!='", + "'!=='", + "'&'", + "'^'", + "'|'", + "'&&'", + "'||'", + "'?'", + "':'", + "'?:'", + "'::'", + "'->'", + "'=~'", + "'==~'", + "'++'", + "'--'", + "'='", + "'+='", + "'-='", + "'*='", + "'/='", + "'%='", + "'&='", + "'^='", + "'|='", + "'<<='", + "'>>='", + "'>>>='", + null, + null, + null, + null, + null, + null, + "'true'", + "'false'", + "'null'" }; + private static final String[] _SYMBOLIC_NAMES = { + null, + "WS", + "COMMENT", + "LBRACK", + "RBRACK", + "LBRACE", + "RBRACE", + "LP", + "RP", + "DOT", + "NSDOT", + "COMMA", + "SEMICOLON", + "IF", + "IN", + "ELSE", + "WHILE", + "DO", + "FOR", + "CONTINUE", + "BREAK", + "RETURN", + "NEW", + "TRY", + "CATCH", + "THROW", + "THIS", + "INSTANCEOF", + "BOOLNOT", + "BWNOT", + "MUL", + "DIV", + "REM", + "ADD", + "SUB", + "LSH", + "RSH", + "USH", + "LT", + "LTE", + "GT", + "GTE", + "EQ", + "EQR", + "NE", + "NER", + "BWAND", + "XOR", + "BWOR", + "BOOLAND", + "BOOLOR", + "COND", + "COLON", + "ELVIS", + "REF", + "ARROW", + "FIND", + "MATCH", + "INCR", + "DECR", + "ASSIGN", + "AADD", + "ASUB", + "AMUL", + "ADIV", + "AREM", + "AAND", + "AXOR", + "AOR", + "ALSH", + "ARSH", + "AUSH", + "OCTAL", + "HEX", + "INTEGER", + "DECIMAL", + "STRING", + "REGEX", + "TRUE", + "FALSE", + "NULL", + "ATYPE", + "TYPE", + "ID", + "UNKNOWN", + "DOTINTEGER", + "DOTID" }; + public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); - /** - * @deprecated Use {@link #VOCABULARY} instead. - */ - @Deprecated - public static final String[] tokenNames; - static { - tokenNames = new String[_SYMBOLIC_NAMES.length]; - for (int i = 0; i < tokenNames.length; i++) { - tokenNames[i] = VOCABULARY.getLiteralName(i); - if (tokenNames[i] == null) { - tokenNames[i] = VOCABULARY.getSymbolicName(i); - } + /** + * @deprecated Use {@link #VOCABULARY} instead. + */ + @Deprecated + public static final String[] tokenNames; + static { + tokenNames = new String[_SYMBOLIC_NAMES.length]; + for (int i = 0; i < tokenNames.length; i++) { + tokenNames[i] = VOCABULARY.getLiteralName(i); + if (tokenNames[i] == null) { + tokenNames[i] = VOCABULARY.getSymbolicName(i); + } - if (tokenNames[i] == null) { - tokenNames[i] = ""; - } + if (tokenNames[i] == null) { + tokenNames[i] = ""; + } + } } - } - - @Override - @Deprecated - public String[] getTokenNames() { - return tokenNames; - } - @Override + @Override + @Deprecated + public String[] getTokenNames() { + return tokenNames; + } - public Vocabulary getVocabulary() { - return VOCABULARY; - } + @Override + public Vocabulary getVocabulary() { + return VOCABULARY; + } - /** Is the preceding {@code /} a the beginning of a regex (true) or a division (false). */ - protected abstract boolean isSlashRegex(); - protected abstract boolean isType(String text); + /** Is the preceding {@code /} a the beginning of a regex (true) or a division (false). */ + protected abstract boolean isSlashRegex(); + protected abstract boolean isType(String text); - public SuggestLexer(CharStream input) { - super(input); - _interp = new LexerATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache); - } + public SuggestLexer(CharStream input) { + super(input); + _interp = new LexerATNSimulator(this, _ATN, _decisionToDFA, _sharedContextCache); + } - @Override - public String getGrammarFileName() { return "SuggestLexer.g4"; } + @Override + public String getGrammarFileName() { + return "SuggestLexer.g4"; + } - @Override - public String[] getRuleNames() { return ruleNames; } + @Override + public String[] getRuleNames() { + return ruleNames; + } - @Override - public String getSerializedATN() { return _serializedATN; } + @Override + public String getSerializedATN() { + return _serializedATN; + } - @Override - public String[] getModeNames() { return modeNames; } + @Override + public String[] getModeNames() { + return modeNames; + } - @Override - public ATN getATN() { return _ATN; } + @Override + public ATN getATN() { + return _ATN; + } - @Override - public boolean sempred(RuleContext _localctx, int ruleIndex, int predIndex) { - switch (ruleIndex) { - case 30: - return DIV_sempred((RuleContext)_localctx, predIndex); - case 76: - return REGEX_sempred((RuleContext)_localctx, predIndex); - case 81: - return TYPE_sempred((RuleContext)_localctx, predIndex); + @Override + public boolean sempred(RuleContext _localctx, int ruleIndex, int predIndex) { + switch (ruleIndex) { + case 30: + return DIV_sempred((RuleContext) _localctx, predIndex); + case 76: + return REGEX_sempred((RuleContext) _localctx, predIndex); + case 81: + return TYPE_sempred((RuleContext) _localctx, predIndex); + } + return true; } - return true; - } - private boolean DIV_sempred(RuleContext _localctx, int predIndex) { - switch (predIndex) { - case 0: - return isSlashRegex() == false ; + + private boolean DIV_sempred(RuleContext _localctx, int predIndex) { + switch (predIndex) { + case 0: + return isSlashRegex() == false; + } + return true; } - return true; - } - private boolean REGEX_sempred(RuleContext _localctx, int predIndex) { - switch (predIndex) { - case 1: - return isSlashRegex() ; + + private boolean REGEX_sempred(RuleContext _localctx, int predIndex) { + switch (predIndex) { + case 1: + return isSlashRegex(); + } + return true; } - return true; - } - private boolean TYPE_sempred(RuleContext _localctx, int predIndex) { - switch (predIndex) { - case 2: - return isType(getText()) ; + + private boolean TYPE_sempred(RuleContext _localctx, int predIndex) { + switch (predIndex) { + case 2: + return isType(getText()); + } + return true; } - return true; - } - public static final String _serializedATN = - "\3\u0430\ud6d1\u8206\uad2d\u4417\uaef1\u8d80\uaadd\2X\u0267\b\1\b\1\4"+ - "\2\t\2\4\3\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b\t\b\4\t\t\t\4\n\t\n"+ - "\4\13\t\13\4\f\t\f\4\r\t\r\4\16\t\16\4\17\t\17\4\20\t\20\4\21\t\21\4\22"+ - "\t\22\4\23\t\23\4\24\t\24\4\25\t\25\4\26\t\26\4\27\t\27\4\30\t\30\4\31"+ - "\t\31\4\32\t\32\4\33\t\33\4\34\t\34\4\35\t\35\4\36\t\36\4\37\t\37\4 \t"+ - " \4!\t!\4\"\t\"\4#\t#\4$\t$\4%\t%\4&\t&\4\'\t\'\4(\t(\4)\t)\4*\t*\4+\t"+ - "+\4,\t,\4-\t-\4.\t.\4/\t/\4\60\t\60\4\61\t\61\4\62\t\62\4\63\t\63\4\64"+ - "\t\64\4\65\t\65\4\66\t\66\4\67\t\67\48\t8\49\t9\4:\t:\4;\t;\4<\t<\4=\t"+ - "=\4>\t>\4?\t?\4@\t@\4A\tA\4B\tB\4C\tC\4D\tD\4E\tE\4F\tF\4G\tG\4H\tH\4"+ - "I\tI\4J\tJ\4K\tK\4L\tL\4M\tM\4N\tN\4O\tO\4P\tP\4Q\tQ\4R\tR\4S\tS\4T\t"+ - "T\4U\tU\4V\tV\4W\tW\3\2\6\2\u00b2\n\2\r\2\16\2\u00b3\3\2\3\2\3\3\3\3\3"+ - "\3\3\3\7\3\u00bc\n\3\f\3\16\3\u00bf\13\3\3\3\3\3\3\3\3\3\3\3\7\3\u00c6"+ - "\n\3\f\3\16\3\u00c9\13\3\3\3\3\3\5\3\u00cd\n\3\3\3\3\3\3\4\3\4\3\5\3\5"+ - "\3\6\3\6\3\7\3\7\3\b\3\b\3\t\3\t\3\n\3\n\3\n\3\n\3\13\3\13\3\13\3\13\3"+ - "\13\3\f\3\f\3\r\3\r\3\16\3\16\3\16\3\17\3\17\3\17\3\20\3\20\3\20\3\20"+ - "\3\20\3\21\3\21\3\21\3\21\3\21\3\21\3\22\3\22\3\22\3\23\3\23\3\23\3\23"+ - "\3\24\3\24\3\24\3\24\3\24\3\24\3\24\3\24\3\24\3\25\3\25\3\25\3\25\3\25"+ - "\3\25\3\26\3\26\3\26\3\26\3\26\3\26\3\26\3\27\3\27\3\27\3\27\3\30\3\30"+ - "\3\30\3\30\3\31\3\31\3\31\3\31\3\31\3\31\3\32\3\32\3\32\3\32\3\32\3\32"+ - "\3\33\3\33\3\33\3\33\3\33\3\34\3\34\3\34\3\34\3\34\3\34\3\34\3\34\3\34"+ - "\3\34\3\34\3\35\3\35\3\36\3\36\3\37\3\37\3 \3 \3 \3!\3!\3\"\3\"\3#\3#"+ - "\3$\3$\3$\3%\3%\3%\3&\3&\3&\3&\3\'\3\'\3(\3(\3(\3)\3)\3*\3*\3*\3+\3+\3"+ - "+\3,\3,\3,\3,\3-\3-\3-\3.\3.\3.\3.\3/\3/\3\60\3\60\3\61\3\61\3\62\3\62"+ - "\3\62\3\63\3\63\3\63\3\64\3\64\3\65\3\65\3\66\3\66\3\66\3\67\3\67\3\67"+ - "\38\38\38\39\39\39\3:\3:\3:\3:\3;\3;\3;\3<\3<\3<\3=\3=\3>\3>\3>\3?\3?"+ - "\3?\3@\3@\3@\3A\3A\3A\3B\3B\3B\3C\3C\3C\3D\3D\3D\3E\3E\3E\3F\3F\3F\3F"+ - "\3G\3G\3G\3G\3H\3H\3H\3H\3H\3I\3I\6I\u01bc\nI\rI\16I\u01bd\3I\5I\u01c1"+ - "\nI\3J\3J\3J\6J\u01c6\nJ\rJ\16J\u01c7\3J\5J\u01cb\nJ\3K\3K\3K\7K\u01d0"+ - "\nK\fK\16K\u01d3\13K\5K\u01d5\nK\3K\5K\u01d8\nK\3L\3L\3L\7L\u01dd\nL\f"+ - "L\16L\u01e0\13L\5L\u01e2\nL\3L\3L\6L\u01e6\nL\rL\16L\u01e7\5L\u01ea\n"+ - "L\3L\3L\5L\u01ee\nL\3L\6L\u01f1\nL\rL\16L\u01f2\5L\u01f5\nL\3L\5L\u01f8"+ - "\nL\3M\3M\3M\3M\3M\3M\7M\u0200\nM\fM\16M\u0203\13M\3M\3M\3M\3M\3M\3M\3"+ - "M\7M\u020c\nM\fM\16M\u020f\13M\3M\5M\u0212\nM\3N\3N\3N\3N\6N\u0218\nN"+ - "\rN\16N\u0219\3N\3N\7N\u021e\nN\fN\16N\u0221\13N\3N\3N\3O\3O\3O\3O\3O"+ - "\3P\3P\3P\3P\3P\3P\3Q\3Q\3Q\3Q\3Q\3R\3R\3R\3R\6R\u0239\nR\rR\16R\u023a"+ - "\3S\3S\3S\3S\7S\u0241\nS\fS\16S\u0244\13S\3S\3S\3T\3T\7T\u024a\nT\fT\16"+ - "T\u024d\13T\3U\3U\3U\3U\3V\3V\3V\7V\u0256\nV\fV\16V\u0259\13V\5V\u025b"+ - "\nV\3V\3V\3W\3W\7W\u0261\nW\fW\16W\u0264\13W\3W\3W\7\u00bd\u00c7\u0201"+ - "\u020d\u0219\2X\4\3\6\4\b\5\n\6\f\7\16\b\20\t\22\n\24\13\26\f\30\r\32"+ - "\16\34\17\36\20 \21\"\22$\23&\24(\25*\26,\27.\30\60\31\62\32\64\33\66"+ - "\348\35:\36<\37> @!B\"D#F$H%J&L\'N(P)R*T+V,X-Z.\\/^\60`\61b\62d\63f\64"+ - "h\65j\66l\67n8p9r:t;v|?~@\u0080A\u0082B\u0084C\u0086D\u0088E\u008a"+ - "F\u008cG\u008eH\u0090I\u0092J\u0094K\u0096L\u0098M\u009aN\u009cO\u009e"+ - "P\u00a0Q\u00a2R\u00a4S\u00a6T\u00a8U\u00aaV\u00acW\u00aeX\4\2\3\25\5\2"+ - "\13\f\17\17\"\"\4\2\f\f\17\17\3\2\629\4\2NNnn\4\2ZZzz\5\2\62;CHch\3\2"+ - "\63;\3\2\62;\b\2FFHHNNffhhnn\4\2GGgg\4\2--//\6\2FFHHffhh\4\2$$^^\4\2)"+ - ")^^\3\2\f\f\4\2\f\f\61\61\t\2WWeekknouuwwzz\5\2C\\aac|\6\2\62;C\\aac|"+ - "\u0288\2\4\3\2\2\2\2\6\3\2\2\2\2\b\3\2\2\2\2\n\3\2\2\2\2\f\3\2\2\2\2\16"+ - "\3\2\2\2\2\20\3\2\2\2\2\22\3\2\2\2\2\24\3\2\2\2\2\26\3\2\2\2\2\30\3\2"+ - "\2\2\2\32\3\2\2\2\2\34\3\2\2\2\2\36\3\2\2\2\2 \3\2\2\2\2\"\3\2\2\2\2$"+ - "\3\2\2\2\2&\3\2\2\2\2(\3\2\2\2\2*\3\2\2\2\2,\3\2\2\2\2.\3\2\2\2\2\60\3"+ - "\2\2\2\2\62\3\2\2\2\2\64\3\2\2\2\2\66\3\2\2\2\28\3\2\2\2\2:\3\2\2\2\2"+ - "<\3\2\2\2\2>\3\2\2\2\2@\3\2\2\2\2B\3\2\2\2\2D\3\2\2\2\2F\3\2\2\2\2H\3"+ - "\2\2\2\2J\3\2\2\2\2L\3\2\2\2\2N\3\2\2\2\2P\3\2\2\2\2R\3\2\2\2\2T\3\2\2"+ - "\2\2V\3\2\2\2\2X\3\2\2\2\2Z\3\2\2\2\2\\\3\2\2\2\2^\3\2\2\2\2`\3\2\2\2"+ - "\2b\3\2\2\2\2d\3\2\2\2\2f\3\2\2\2\2h\3\2\2\2\2j\3\2\2\2\2l\3\2\2\2\2n"+ - "\3\2\2\2\2p\3\2\2\2\2r\3\2\2\2\2t\3\2\2\2\2v\3\2\2\2\2x\3\2\2\2\2z\3\2"+ - "\2\2\2|\3\2\2\2\2~\3\2\2\2\2\u0080\3\2\2\2\2\u0082\3\2\2\2\2\u0084\3\2"+ - "\2\2\2\u0086\3\2\2\2\2\u0088\3\2\2\2\2\u008a\3\2\2\2\2\u008c\3\2\2\2\2"+ - "\u008e\3\2\2\2\2\u0090\3\2\2\2\2\u0092\3\2\2\2\2\u0094\3\2\2\2\2\u0096"+ - "\3\2\2\2\2\u0098\3\2\2\2\2\u009a\3\2\2\2\2\u009c\3\2\2\2\2\u009e\3\2\2"+ - "\2\2\u00a0\3\2\2\2\2\u00a2\3\2\2\2\2\u00a4\3\2\2\2\2\u00a6\3\2\2\2\2\u00a8"+ - "\3\2\2\2\2\u00aa\3\2\2\2\3\u00ac\3\2\2\2\3\u00ae\3\2\2\2\4\u00b1\3\2\2"+ - "\2\6\u00cc\3\2\2\2\b\u00d0\3\2\2\2\n\u00d2\3\2\2\2\f\u00d4\3\2\2\2\16"+ - "\u00d6\3\2\2\2\20\u00d8\3\2\2\2\22\u00da\3\2\2\2\24\u00dc\3\2\2\2\26\u00e0"+ - "\3\2\2\2\30\u00e5\3\2\2\2\32\u00e7\3\2\2\2\34\u00e9\3\2\2\2\36\u00ec\3"+ - "\2\2\2 \u00ef\3\2\2\2\"\u00f4\3\2\2\2$\u00fa\3\2\2\2&\u00fd\3\2\2\2(\u0101"+ - "\3\2\2\2*\u010a\3\2\2\2,\u0110\3\2\2\2.\u0117\3\2\2\2\60\u011b\3\2\2\2"+ - "\62\u011f\3\2\2\2\64\u0125\3\2\2\2\66\u012b\3\2\2\28\u0130\3\2\2\2:\u013b"+ - "\3\2\2\2<\u013d\3\2\2\2>\u013f\3\2\2\2@\u0141\3\2\2\2B\u0144\3\2\2\2D"+ - "\u0146\3\2\2\2F\u0148\3\2\2\2H\u014a\3\2\2\2J\u014d\3\2\2\2L\u0150\3\2"+ - "\2\2N\u0154\3\2\2\2P\u0156\3\2\2\2R\u0159\3\2\2\2T\u015b\3\2\2\2V\u015e"+ - "\3\2\2\2X\u0161\3\2\2\2Z\u0165\3\2\2\2\\\u0168\3\2\2\2^\u016c\3\2\2\2"+ - "`\u016e\3\2\2\2b\u0170\3\2\2\2d\u0172\3\2\2\2f\u0175\3\2\2\2h\u0178\3"+ - "\2\2\2j\u017a\3\2\2\2l\u017c\3\2\2\2n\u017f\3\2\2\2p\u0182\3\2\2\2r\u0185"+ - "\3\2\2\2t\u0188\3\2\2\2v\u018c\3\2\2\2x\u018f\3\2\2\2z\u0192\3\2\2\2|"+ - "\u0194\3\2\2\2~\u0197\3\2\2\2\u0080\u019a\3\2\2\2\u0082\u019d\3\2\2\2"+ - "\u0084\u01a0\3\2\2\2\u0086\u01a3\3\2\2\2\u0088\u01a6\3\2\2\2\u008a\u01a9"+ - "\3\2\2\2\u008c\u01ac\3\2\2\2\u008e\u01b0\3\2\2\2\u0090\u01b4\3\2\2\2\u0092"+ - "\u01b9\3\2\2\2\u0094\u01c2\3\2\2\2\u0096\u01d4\3\2\2\2\u0098\u01e1\3\2"+ - "\2\2\u009a\u0211\3\2\2\2\u009c\u0213\3\2\2\2\u009e\u0224\3\2\2\2\u00a0"+ - "\u0229\3\2\2\2\u00a2\u022f\3\2\2\2\u00a4\u0234\3\2\2\2\u00a6\u023c\3\2"+ - "\2\2\u00a8\u0247\3\2\2\2\u00aa\u024e\3\2\2\2\u00ac\u025a\3\2\2\2\u00ae"+ - "\u025e\3\2\2\2\u00b0\u00b2\t\2\2\2\u00b1\u00b0\3\2\2\2\u00b2\u00b3\3\2"+ - "\2\2\u00b3\u00b1\3\2\2\2\u00b3\u00b4\3\2\2\2\u00b4\u00b5\3\2\2\2\u00b5"+ - "\u00b6\b\2\2\2\u00b6\5\3\2\2\2\u00b7\u00b8\7\61\2\2\u00b8\u00b9\7\61\2"+ - "\2\u00b9\u00bd\3\2\2\2\u00ba\u00bc\13\2\2\2\u00bb\u00ba\3\2\2\2\u00bc"+ - "\u00bf\3\2\2\2\u00bd\u00be\3\2\2\2\u00bd\u00bb\3\2\2\2\u00be\u00c0\3\2"+ - "\2\2\u00bf\u00bd\3\2\2\2\u00c0\u00cd\t\3\2\2\u00c1\u00c2\7\61\2\2\u00c2"+ - "\u00c3\7,\2\2\u00c3\u00c7\3\2\2\2\u00c4\u00c6\13\2\2\2\u00c5\u00c4\3\2"+ - "\2\2\u00c6\u00c9\3\2\2\2\u00c7\u00c8\3\2\2\2\u00c7\u00c5\3\2\2\2\u00c8"+ - "\u00ca\3\2\2\2\u00c9\u00c7\3\2\2\2\u00ca\u00cb\7,\2\2\u00cb\u00cd\7\61"+ - "\2\2\u00cc\u00b7\3\2\2\2\u00cc\u00c1\3\2\2\2\u00cd\u00ce\3\2\2\2\u00ce"+ - "\u00cf\b\3\2\2\u00cf\7\3\2\2\2\u00d0\u00d1\7}\2\2\u00d1\t\3\2\2\2\u00d2"+ - "\u00d3\7\177\2\2\u00d3\13\3\2\2\2\u00d4\u00d5\7]\2\2\u00d5\r\3\2\2\2\u00d6"+ - "\u00d7\7_\2\2\u00d7\17\3\2\2\2\u00d8\u00d9\7*\2\2\u00d9\21\3\2\2\2\u00da"+ - "\u00db\7+\2\2\u00db\23\3\2\2\2\u00dc\u00dd\7\60\2\2\u00dd\u00de\3\2\2"+ - "\2\u00de\u00df\b\n\3\2\u00df\25\3\2\2\2\u00e0\u00e1\7A\2\2\u00e1\u00e2"+ - "\7\60\2\2\u00e2\u00e3\3\2\2\2\u00e3\u00e4\b\13\3\2\u00e4\27\3\2\2\2\u00e5"+ - "\u00e6\7.\2\2\u00e6\31\3\2\2\2\u00e7\u00e8\7=\2\2\u00e8\33\3\2\2\2\u00e9"+ - "\u00ea\7k\2\2\u00ea\u00eb\7h\2\2\u00eb\35\3\2\2\2\u00ec\u00ed\7k\2\2\u00ed"+ - "\u00ee\7p\2\2\u00ee\37\3\2\2\2\u00ef\u00f0\7g\2\2\u00f0\u00f1\7n\2\2\u00f1"+ - "\u00f2\7u\2\2\u00f2\u00f3\7g\2\2\u00f3!\3\2\2\2\u00f4\u00f5\7y\2\2\u00f5"+ - "\u00f6\7j\2\2\u00f6\u00f7\7k\2\2\u00f7\u00f8\7n\2\2\u00f8\u00f9\7g\2\2"+ - "\u00f9#\3\2\2\2\u00fa\u00fb\7f\2\2\u00fb\u00fc\7q\2\2\u00fc%\3\2\2\2\u00fd"+ - "\u00fe\7h\2\2\u00fe\u00ff\7q\2\2\u00ff\u0100\7t\2\2\u0100\'\3\2\2\2\u0101"+ - "\u0102\7e\2\2\u0102\u0103\7q\2\2\u0103\u0104\7p\2\2\u0104\u0105\7v\2\2"+ - "\u0105\u0106\7k\2\2\u0106\u0107\7p\2\2\u0107\u0108\7w\2\2\u0108\u0109"+ - "\7g\2\2\u0109)\3\2\2\2\u010a\u010b\7d\2\2\u010b\u010c\7t\2\2\u010c\u010d"+ - "\7g\2\2\u010d\u010e\7c\2\2\u010e\u010f\7m\2\2\u010f+\3\2\2\2\u0110\u0111"+ - "\7t\2\2\u0111\u0112\7g\2\2\u0112\u0113\7v\2\2\u0113\u0114\7w\2\2\u0114"+ - "\u0115\7t\2\2\u0115\u0116\7p\2\2\u0116-\3\2\2\2\u0117\u0118\7p\2\2\u0118"+ - "\u0119\7g\2\2\u0119\u011a\7y\2\2\u011a/\3\2\2\2\u011b\u011c\7v\2\2\u011c"+ - "\u011d\7t\2\2\u011d\u011e\7{\2\2\u011e\61\3\2\2\2\u011f\u0120\7e\2\2\u0120"+ - "\u0121\7c\2\2\u0121\u0122\7v\2\2\u0122\u0123\7e\2\2\u0123\u0124\7j\2\2"+ - "\u0124\63\3\2\2\2\u0125\u0126\7v\2\2\u0126\u0127\7j\2\2\u0127\u0128\7"+ - "t\2\2\u0128\u0129\7q\2\2\u0129\u012a\7y\2\2\u012a\65\3\2\2\2\u012b\u012c"+ - "\7v\2\2\u012c\u012d\7j\2\2\u012d\u012e\7k\2\2\u012e\u012f\7u\2\2\u012f"+ - "\67\3\2\2\2\u0130\u0131\7k\2\2\u0131\u0132\7p\2\2\u0132\u0133\7u\2\2\u0133"+ - "\u0134\7v\2\2\u0134\u0135\7c\2\2\u0135\u0136\7p\2\2\u0136\u0137\7e\2\2"+ - "\u0137\u0138\7g\2\2\u0138\u0139\7q\2\2\u0139\u013a\7h\2\2\u013a9\3\2\2"+ - "\2\u013b\u013c\7#\2\2\u013c;\3\2\2\2\u013d\u013e\7\u0080\2\2\u013e=\3"+ - "\2\2\2\u013f\u0140\7,\2\2\u0140?\3\2\2\2\u0141\u0142\7\61\2\2\u0142\u0143"+ - "\6 \2\2\u0143A\3\2\2\2\u0144\u0145\7\'\2\2\u0145C\3\2\2\2\u0146\u0147"+ - "\7-\2\2\u0147E\3\2\2\2\u0148\u0149\7/\2\2\u0149G\3\2\2\2\u014a\u014b\7"+ - ">\2\2\u014b\u014c\7>\2\2\u014cI\3\2\2\2\u014d\u014e\7@\2\2\u014e\u014f"+ - "\7@\2\2\u014fK\3\2\2\2\u0150\u0151\7@\2\2\u0151\u0152\7@\2\2\u0152\u0153"+ - "\7@\2\2\u0153M\3\2\2\2\u0154\u0155\7>\2\2\u0155O\3\2\2\2\u0156\u0157\7"+ - ">\2\2\u0157\u0158\7?\2\2\u0158Q\3\2\2\2\u0159\u015a\7@\2\2\u015aS\3\2"+ - "\2\2\u015b\u015c\7@\2\2\u015c\u015d\7?\2\2\u015dU\3\2\2\2\u015e\u015f"+ - "\7?\2\2\u015f\u0160\7?\2\2\u0160W\3\2\2\2\u0161\u0162\7?\2\2\u0162\u0163"+ - "\7?\2\2\u0163\u0164\7?\2\2\u0164Y\3\2\2\2\u0165\u0166\7#\2\2\u0166\u0167"+ - "\7?\2\2\u0167[\3\2\2\2\u0168\u0169\7#\2\2\u0169\u016a\7?\2\2\u016a\u016b"+ - "\7?\2\2\u016b]\3\2\2\2\u016c\u016d\7(\2\2\u016d_\3\2\2\2\u016e\u016f\7"+ - "`\2\2\u016fa\3\2\2\2\u0170\u0171\7~\2\2\u0171c\3\2\2\2\u0172\u0173\7("+ - "\2\2\u0173\u0174\7(\2\2\u0174e\3\2\2\2\u0175\u0176\7~\2\2\u0176\u0177"+ - "\7~\2\2\u0177g\3\2\2\2\u0178\u0179\7A\2\2\u0179i\3\2\2\2\u017a\u017b\7"+ - "<\2\2\u017bk\3\2\2\2\u017c\u017d\7A\2\2\u017d\u017e\7<\2\2\u017em\3\2"+ - "\2\2\u017f\u0180\7<\2\2\u0180\u0181\7<\2\2\u0181o\3\2\2\2\u0182\u0183"+ - "\7/\2\2\u0183\u0184\7@\2\2\u0184q\3\2\2\2\u0185\u0186\7?\2\2\u0186\u0187"+ - "\7\u0080\2\2\u0187s\3\2\2\2\u0188\u0189\7?\2\2\u0189\u018a\7?\2\2\u018a"+ - "\u018b\7\u0080\2\2\u018bu\3\2\2\2\u018c\u018d\7-\2\2\u018d\u018e\7-\2"+ - "\2\u018ew\3\2\2\2\u018f\u0190\7/\2\2\u0190\u0191\7/\2\2\u0191y\3\2\2\2"+ - "\u0192\u0193\7?\2\2\u0193{\3\2\2\2\u0194\u0195\7-\2\2\u0195\u0196\7?\2"+ - "\2\u0196}\3\2\2\2\u0197\u0198\7/\2\2\u0198\u0199\7?\2\2\u0199\177\3\2"+ - "\2\2\u019a\u019b\7,\2\2\u019b\u019c\7?\2\2\u019c\u0081\3\2\2\2\u019d\u019e"+ - "\7\61\2\2\u019e\u019f\7?\2\2\u019f\u0083\3\2\2\2\u01a0\u01a1\7\'\2\2\u01a1"+ - "\u01a2\7?\2\2\u01a2\u0085\3\2\2\2\u01a3\u01a4\7(\2\2\u01a4\u01a5\7?\2"+ - "\2\u01a5\u0087\3\2\2\2\u01a6\u01a7\7`\2\2\u01a7\u01a8\7?\2\2\u01a8\u0089"+ - "\3\2\2\2\u01a9\u01aa\7~\2\2\u01aa\u01ab\7?\2\2\u01ab\u008b\3\2\2\2\u01ac"+ - "\u01ad\7>\2\2\u01ad\u01ae\7>\2\2\u01ae\u01af\7?\2\2\u01af\u008d\3\2\2"+ - "\2\u01b0\u01b1\7@\2\2\u01b1\u01b2\7@\2\2\u01b2\u01b3\7?\2\2\u01b3\u008f"+ - "\3\2\2\2\u01b4\u01b5\7@\2\2\u01b5\u01b6\7@\2\2\u01b6\u01b7\7@\2\2\u01b7"+ - "\u01b8\7?\2\2\u01b8\u0091\3\2\2\2\u01b9\u01bb\7\62\2\2\u01ba\u01bc\t\4"+ - "\2\2\u01bb\u01ba\3\2\2\2\u01bc\u01bd\3\2\2\2\u01bd\u01bb\3\2\2\2\u01bd"+ - "\u01be\3\2\2\2\u01be\u01c0\3\2\2\2\u01bf\u01c1\t\5\2\2\u01c0\u01bf\3\2"+ - "\2\2\u01c0\u01c1\3\2\2\2\u01c1\u0093\3\2\2\2\u01c2\u01c3\7\62\2\2\u01c3"+ - "\u01c5\t\6\2\2\u01c4\u01c6\t\7\2\2\u01c5\u01c4\3\2\2\2\u01c6\u01c7\3\2"+ - "\2\2\u01c7\u01c5\3\2\2\2\u01c7\u01c8\3\2\2\2\u01c8\u01ca\3\2\2\2\u01c9"+ - "\u01cb\t\5\2\2\u01ca\u01c9\3\2\2\2\u01ca\u01cb\3\2\2\2\u01cb\u0095\3\2"+ - "\2\2\u01cc\u01d5\7\62\2\2\u01cd\u01d1\t\b\2\2\u01ce\u01d0\t\t\2\2\u01cf"+ - "\u01ce\3\2\2\2\u01d0\u01d3\3\2\2\2\u01d1\u01cf\3\2\2\2\u01d1\u01d2\3\2"+ - "\2\2\u01d2\u01d5\3\2\2\2\u01d3\u01d1\3\2\2\2\u01d4\u01cc\3\2\2\2\u01d4"+ - "\u01cd\3\2\2\2\u01d5\u01d7\3\2\2\2\u01d6\u01d8\t\n\2\2\u01d7\u01d6\3\2"+ - "\2\2\u01d7\u01d8\3\2\2\2\u01d8\u0097\3\2\2\2\u01d9\u01e2\7\62\2\2\u01da"+ - "\u01de\t\b\2\2\u01db\u01dd\t\t\2\2\u01dc\u01db\3\2\2\2\u01dd\u01e0\3\2"+ - "\2\2\u01de\u01dc\3\2\2\2\u01de\u01df\3\2\2\2\u01df\u01e2\3\2\2\2\u01e0"+ - "\u01de\3\2\2\2\u01e1\u01d9\3\2\2\2\u01e1\u01da\3\2\2\2\u01e2\u01e9\3\2"+ - "\2\2\u01e3\u01e5\5\24\n\2\u01e4\u01e6\t\t\2\2\u01e5\u01e4\3\2\2\2\u01e6"+ - "\u01e7\3\2\2\2\u01e7\u01e5\3\2\2\2\u01e7\u01e8\3\2\2\2\u01e8\u01ea\3\2"+ - "\2\2\u01e9\u01e3\3\2\2\2\u01e9\u01ea\3\2\2\2\u01ea\u01f4\3\2\2\2\u01eb"+ - "\u01ed\t\13\2\2\u01ec\u01ee\t\f\2\2\u01ed\u01ec\3\2\2\2\u01ed\u01ee\3"+ - "\2\2\2\u01ee\u01f0\3\2\2\2\u01ef\u01f1\t\t\2\2\u01f0\u01ef\3\2\2\2\u01f1"+ - "\u01f2\3\2\2\2\u01f2\u01f0\3\2\2\2\u01f2\u01f3\3\2\2\2\u01f3\u01f5\3\2"+ - "\2\2\u01f4\u01eb\3\2\2\2\u01f4\u01f5\3\2\2\2\u01f5\u01f7\3\2\2\2\u01f6"+ - "\u01f8\t\r\2\2\u01f7\u01f6\3\2\2\2\u01f7\u01f8\3\2\2\2\u01f8\u0099\3\2"+ - "\2\2\u01f9\u0201\7$\2\2\u01fa\u01fb\7^\2\2\u01fb\u0200\7$\2\2\u01fc\u01fd"+ - "\7^\2\2\u01fd\u0200\7^\2\2\u01fe\u0200\n\16\2\2\u01ff\u01fa\3\2\2\2\u01ff"+ - "\u01fc\3\2\2\2\u01ff\u01fe\3\2\2\2\u0200\u0203\3\2\2\2\u0201\u0202\3\2"+ - "\2\2\u0201\u01ff\3\2\2\2\u0202\u0204\3\2\2\2\u0203\u0201\3\2\2\2\u0204"+ - "\u0212\7$\2\2\u0205\u020d\7)\2\2\u0206\u0207\7^\2\2\u0207\u020c\7)\2\2"+ - "\u0208\u0209\7^\2\2\u0209\u020c\7^\2\2\u020a\u020c\n\17\2\2\u020b\u0206"+ - "\3\2\2\2\u020b\u0208\3\2\2\2\u020b\u020a\3\2\2\2\u020c\u020f\3\2\2\2\u020d"+ - "\u020e\3\2\2\2\u020d\u020b\3\2\2\2\u020e\u0210\3\2\2\2\u020f\u020d\3\2"+ - "\2\2\u0210\u0212\7)\2\2\u0211\u01f9\3\2\2\2\u0211\u0205\3\2\2\2\u0212"+ - "\u009b\3\2\2\2\u0213\u0217\7\61\2\2\u0214\u0215\7^\2\2\u0215\u0218\n\20"+ - "\2\2\u0216\u0218\n\21\2\2\u0217\u0214\3\2\2\2\u0217\u0216\3\2\2\2\u0218"+ - "\u0219\3\2\2\2\u0219\u021a\3\2\2\2\u0219\u0217\3\2\2\2\u021a\u021b\3\2"+ - "\2\2\u021b\u021f\7\61\2\2\u021c\u021e\t\22\2\2\u021d\u021c\3\2\2\2\u021e"+ - "\u0221\3\2\2\2\u021f\u021d\3\2\2\2\u021f\u0220\3\2\2\2\u0220\u0222\3\2"+ - "\2\2\u0221\u021f\3\2\2\2\u0222\u0223\6N\3\2\u0223\u009d\3\2\2\2\u0224"+ - "\u0225\7v\2\2\u0225\u0226\7t\2\2\u0226\u0227\7w\2\2\u0227\u0228\7g\2\2"+ - "\u0228\u009f\3\2\2\2\u0229\u022a\7h\2\2\u022a\u022b\7c\2\2\u022b\u022c"+ - "\7n\2\2\u022c\u022d\7u\2\2\u022d\u022e\7g\2\2\u022e\u00a1\3\2\2\2\u022f"+ - "\u0230\7p\2\2\u0230\u0231\7w\2\2\u0231\u0232\7n\2\2\u0232\u0233\7n\2\2"+ - "\u0233\u00a3\3\2\2\2\u0234\u0238\5\u00a6S\2\u0235\u0236\5\f\6\2\u0236"+ - "\u0237\5\16\7\2\u0237\u0239\3\2\2\2\u0238\u0235\3\2\2\2\u0239\u023a\3"+ - "\2\2\2\u023a\u0238\3\2\2\2\u023a\u023b\3\2\2\2\u023b\u00a5\3\2\2\2\u023c"+ - "\u0242\5\u00a8T\2\u023d\u023e\5\24\n\2\u023e\u023f\5\u00a8T\2\u023f\u0241"+ - "\3\2\2\2\u0240\u023d\3\2\2\2\u0241\u0244\3\2\2\2\u0242\u0240\3\2\2\2\u0242"+ - "\u0243\3\2\2\2\u0243\u0245\3\2\2\2\u0244\u0242\3\2\2\2\u0245\u0246\6S"+ - "\4\2\u0246\u00a7\3\2\2\2\u0247\u024b\t\23\2\2\u0248\u024a\t\24\2\2\u0249"+ - "\u0248\3\2\2\2\u024a\u024d\3\2\2\2\u024b\u0249\3\2\2\2\u024b\u024c\3\2"+ - "\2\2\u024c\u00a9\3\2\2\2\u024d\u024b\3\2\2\2\u024e\u024f\13\2\2\2\u024f"+ - "\u0250\3\2\2\2\u0250\u0251\bU\2\2\u0251\u00ab\3\2\2\2\u0252\u025b\7\62"+ - "\2\2\u0253\u0257\t\b\2\2\u0254\u0256\t\t\2\2\u0255\u0254\3\2\2\2\u0256"+ - "\u0259\3\2\2\2\u0257\u0255\3\2\2\2\u0257\u0258\3\2\2\2\u0258\u025b\3\2"+ - "\2\2\u0259\u0257\3\2\2\2\u025a\u0252\3\2\2\2\u025a\u0253\3\2\2\2\u025b"+ - "\u025c\3\2\2\2\u025c\u025d\bV\4\2\u025d\u00ad\3\2\2\2\u025e\u0262\t\23"+ - "\2\2\u025f\u0261\t\24\2\2\u0260\u025f\3\2\2\2\u0261\u0264\3\2\2\2\u0262"+ - "\u0260\3\2\2\2\u0262\u0263\3\2\2\2\u0263\u0265\3\2\2\2\u0264\u0262\3\2"+ - "\2\2\u0265\u0266\bW\4\2\u0266\u00af\3\2\2\2%\2\3\u00b3\u00bd\u00c7\u00cc"+ - "\u01bd\u01c0\u01c7\u01ca\u01d1\u01d4\u01d7\u01de\u01e1\u01e7\u01e9\u01ed"+ - "\u01f2\u01f4\u01f7\u01ff\u0201\u020b\u020d\u0211\u0217\u0219\u021f\u023a"+ - "\u0242\u024b\u0257\u025a\u0262\5\b\2\2\4\3\2\4\2\2"; - public static final ATN _ATN = - new ATNDeserializer().deserialize(_serializedATN.toCharArray()); - static { - _decisionToDFA = new DFA[_ATN.getNumberOfDecisions()]; - for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) { - _decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i); + public static final String _serializedATN = "\3\u0430\ud6d1\u8206\uad2d\u4417\uaef1\u8d80\uaadd\2X\u0267\b\1\b\1\4" + + "\2\t\2\4\3\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b\t\b\4\t\t\t\4\n\t\n" + + "\4\13\t\13\4\f\t\f\4\r\t\r\4\16\t\16\4\17\t\17\4\20\t\20\4\21\t\21\4\22" + + "\t\22\4\23\t\23\4\24\t\24\4\25\t\25\4\26\t\26\4\27\t\27\4\30\t\30\4\31" + + "\t\31\4\32\t\32\4\33\t\33\4\34\t\34\4\35\t\35\4\36\t\36\4\37\t\37\4 \t" + + " \4!\t!\4\"\t\"\4#\t#\4$\t$\4%\t%\4&\t&\4\'\t\'\4(\t(\4)\t)\4*\t*\4+\t" + + "+\4,\t,\4-\t-\4.\t.\4/\t/\4\60\t\60\4\61\t\61\4\62\t\62\4\63\t\63\4\64" + + "\t\64\4\65\t\65\4\66\t\66\4\67\t\67\48\t8\49\t9\4:\t:\4;\t;\4<\t<\4=\t" + + "=\4>\t>\4?\t?\4@\t@\4A\tA\4B\tB\4C\tC\4D\tD\4E\tE\4F\tF\4G\tG\4H\tH\4" + + "I\tI\4J\tJ\4K\tK\4L\tL\4M\tM\4N\tN\4O\tO\4P\tP\4Q\tQ\4R\tR\4S\tS\4T\t" + + "T\4U\tU\4V\tV\4W\tW\3\2\6\2\u00b2\n\2\r\2\16\2\u00b3\3\2\3\2\3\3\3\3\3" + + "\3\3\3\7\3\u00bc\n\3\f\3\16\3\u00bf\13\3\3\3\3\3\3\3\3\3\3\3\7\3\u00c6" + + "\n\3\f\3\16\3\u00c9\13\3\3\3\3\3\5\3\u00cd\n\3\3\3\3\3\3\4\3\4\3\5\3\5" + + "\3\6\3\6\3\7\3\7\3\b\3\b\3\t\3\t\3\n\3\n\3\n\3\n\3\13\3\13\3\13\3\13\3" + + "\13\3\f\3\f\3\r\3\r\3\16\3\16\3\16\3\17\3\17\3\17\3\20\3\20\3\20\3\20" + + "\3\20\3\21\3\21\3\21\3\21\3\21\3\21\3\22\3\22\3\22\3\23\3\23\3\23\3\23" + + "\3\24\3\24\3\24\3\24\3\24\3\24\3\24\3\24\3\24\3\25\3\25\3\25\3\25\3\25" + + "\3\25\3\26\3\26\3\26\3\26\3\26\3\26\3\26\3\27\3\27\3\27\3\27\3\30\3\30" + + "\3\30\3\30\3\31\3\31\3\31\3\31\3\31\3\31\3\32\3\32\3\32\3\32\3\32\3\32" + + "\3\33\3\33\3\33\3\33\3\33\3\34\3\34\3\34\3\34\3\34\3\34\3\34\3\34\3\34" + + "\3\34\3\34\3\35\3\35\3\36\3\36\3\37\3\37\3 \3 \3 \3!\3!\3\"\3\"\3#\3#" + + "\3$\3$\3$\3%\3%\3%\3&\3&\3&\3&\3\'\3\'\3(\3(\3(\3)\3)\3*\3*\3*\3+\3+\3" + + "+\3,\3,\3,\3,\3-\3-\3-\3.\3.\3.\3.\3/\3/\3\60\3\60\3\61\3\61\3\62\3\62" + + "\3\62\3\63\3\63\3\63\3\64\3\64\3\65\3\65\3\66\3\66\3\66\3\67\3\67\3\67" + + "\38\38\38\39\39\39\3:\3:\3:\3:\3;\3;\3;\3<\3<\3<\3=\3=\3>\3>\3>\3?\3?" + + "\3?\3@\3@\3@\3A\3A\3A\3B\3B\3B\3C\3C\3C\3D\3D\3D\3E\3E\3E\3F\3F\3F\3F" + + "\3G\3G\3G\3G\3H\3H\3H\3H\3H\3I\3I\6I\u01bc\nI\rI\16I\u01bd\3I\5I\u01c1" + + "\nI\3J\3J\3J\6J\u01c6\nJ\rJ\16J\u01c7\3J\5J\u01cb\nJ\3K\3K\3K\7K\u01d0" + + "\nK\fK\16K\u01d3\13K\5K\u01d5\nK\3K\5K\u01d8\nK\3L\3L\3L\7L\u01dd\nL\f" + + "L\16L\u01e0\13L\5L\u01e2\nL\3L\3L\6L\u01e6\nL\rL\16L\u01e7\5L\u01ea\n" + + "L\3L\3L\5L\u01ee\nL\3L\6L\u01f1\nL\rL\16L\u01f2\5L\u01f5\nL\3L\5L\u01f8" + + "\nL\3M\3M\3M\3M\3M\3M\7M\u0200\nM\fM\16M\u0203\13M\3M\3M\3M\3M\3M\3M\3" + + "M\7M\u020c\nM\fM\16M\u020f\13M\3M\5M\u0212\nM\3N\3N\3N\3N\6N\u0218\nN" + + "\rN\16N\u0219\3N\3N\7N\u021e\nN\fN\16N\u0221\13N\3N\3N\3O\3O\3O\3O\3O" + + "\3P\3P\3P\3P\3P\3P\3Q\3Q\3Q\3Q\3Q\3R\3R\3R\3R\6R\u0239\nR\rR\16R\u023a" + + "\3S\3S\3S\3S\7S\u0241\nS\fS\16S\u0244\13S\3S\3S\3T\3T\7T\u024a\nT\fT\16" + + "T\u024d\13T\3U\3U\3U\3U\3V\3V\3V\7V\u0256\nV\fV\16V\u0259\13V\5V\u025b" + + "\nV\3V\3V\3W\3W\7W\u0261\nW\fW\16W\u0264\13W\3W\3W\7\u00bd\u00c7\u0201" + + "\u020d\u0219\2X\4\3\6\4\b\5\n\6\f\7\16\b\20\t\22\n\24\13\26\f\30\r\32" + + "\16\34\17\36\20 \21\"\22$\23&\24(\25*\26,\27.\30\60\31\62\32\64\33\66" + + "\348\35:\36<\37> @!B\"D#F$H%J&L\'N(P)R*T+V,X-Z.\\/^\60`\61b\62d\63f\64" + + "h\65j\66l\67n8p9r:t;v|?~@\u0080A\u0082B\u0084C\u0086D\u0088E\u008a" + + "F\u008cG\u008eH\u0090I\u0092J\u0094K\u0096L\u0098M\u009aN\u009cO\u009e" + + "P\u00a0Q\u00a2R\u00a4S\u00a6T\u00a8U\u00aaV\u00acW\u00aeX\4\2\3\25\5\2" + + "\13\f\17\17\"\"\4\2\f\f\17\17\3\2\629\4\2NNnn\4\2ZZzz\5\2\62;CHch\3\2" + + "\63;\3\2\62;\b\2FFHHNNffhhnn\4\2GGgg\4\2--//\6\2FFHHffhh\4\2$$^^\4\2)" + + ")^^\3\2\f\f\4\2\f\f\61\61\t\2WWeekknouuwwzz\5\2C\\aac|\6\2\62;C\\aac|" + + "\u0288\2\4\3\2\2\2\2\6\3\2\2\2\2\b\3\2\2\2\2\n\3\2\2\2\2\f\3\2\2\2\2\16" + + "\3\2\2\2\2\20\3\2\2\2\2\22\3\2\2\2\2\24\3\2\2\2\2\26\3\2\2\2\2\30\3\2" + + "\2\2\2\32\3\2\2\2\2\34\3\2\2\2\2\36\3\2\2\2\2 \3\2\2\2\2\"\3\2\2\2\2$" + + "\3\2\2\2\2&\3\2\2\2\2(\3\2\2\2\2*\3\2\2\2\2,\3\2\2\2\2.\3\2\2\2\2\60\3" + + "\2\2\2\2\62\3\2\2\2\2\64\3\2\2\2\2\66\3\2\2\2\28\3\2\2\2\2:\3\2\2\2\2" + + "<\3\2\2\2\2>\3\2\2\2\2@\3\2\2\2\2B\3\2\2\2\2D\3\2\2\2\2F\3\2\2\2\2H\3" + + "\2\2\2\2J\3\2\2\2\2L\3\2\2\2\2N\3\2\2\2\2P\3\2\2\2\2R\3\2\2\2\2T\3\2\2" + + "\2\2V\3\2\2\2\2X\3\2\2\2\2Z\3\2\2\2\2\\\3\2\2\2\2^\3\2\2\2\2`\3\2\2\2" + + "\2b\3\2\2\2\2d\3\2\2\2\2f\3\2\2\2\2h\3\2\2\2\2j\3\2\2\2\2l\3\2\2\2\2n" + + "\3\2\2\2\2p\3\2\2\2\2r\3\2\2\2\2t\3\2\2\2\2v\3\2\2\2\2x\3\2\2\2\2z\3\2" + + "\2\2\2|\3\2\2\2\2~\3\2\2\2\2\u0080\3\2\2\2\2\u0082\3\2\2\2\2\u0084\3\2" + + "\2\2\2\u0086\3\2\2\2\2\u0088\3\2\2\2\2\u008a\3\2\2\2\2\u008c\3\2\2\2\2" + + "\u008e\3\2\2\2\2\u0090\3\2\2\2\2\u0092\3\2\2\2\2\u0094\3\2\2\2\2\u0096" + + "\3\2\2\2\2\u0098\3\2\2\2\2\u009a\3\2\2\2\2\u009c\3\2\2\2\2\u009e\3\2\2" + + "\2\2\u00a0\3\2\2\2\2\u00a2\3\2\2\2\2\u00a4\3\2\2\2\2\u00a6\3\2\2\2\2\u00a8" + + "\3\2\2\2\2\u00aa\3\2\2\2\3\u00ac\3\2\2\2\3\u00ae\3\2\2\2\4\u00b1\3\2\2" + + "\2\6\u00cc\3\2\2\2\b\u00d0\3\2\2\2\n\u00d2\3\2\2\2\f\u00d4\3\2\2\2\16" + + "\u00d6\3\2\2\2\20\u00d8\3\2\2\2\22\u00da\3\2\2\2\24\u00dc\3\2\2\2\26\u00e0" + + "\3\2\2\2\30\u00e5\3\2\2\2\32\u00e7\3\2\2\2\34\u00e9\3\2\2\2\36\u00ec\3" + + "\2\2\2 \u00ef\3\2\2\2\"\u00f4\3\2\2\2$\u00fa\3\2\2\2&\u00fd\3\2\2\2(\u0101" + + "\3\2\2\2*\u010a\3\2\2\2,\u0110\3\2\2\2.\u0117\3\2\2\2\60\u011b\3\2\2\2" + + "\62\u011f\3\2\2\2\64\u0125\3\2\2\2\66\u012b\3\2\2\28\u0130\3\2\2\2:\u013b" + + "\3\2\2\2<\u013d\3\2\2\2>\u013f\3\2\2\2@\u0141\3\2\2\2B\u0144\3\2\2\2D" + + "\u0146\3\2\2\2F\u0148\3\2\2\2H\u014a\3\2\2\2J\u014d\3\2\2\2L\u0150\3\2" + + "\2\2N\u0154\3\2\2\2P\u0156\3\2\2\2R\u0159\3\2\2\2T\u015b\3\2\2\2V\u015e" + + "\3\2\2\2X\u0161\3\2\2\2Z\u0165\3\2\2\2\\\u0168\3\2\2\2^\u016c\3\2\2\2" + + "`\u016e\3\2\2\2b\u0170\3\2\2\2d\u0172\3\2\2\2f\u0175\3\2\2\2h\u0178\3" + + "\2\2\2j\u017a\3\2\2\2l\u017c\3\2\2\2n\u017f\3\2\2\2p\u0182\3\2\2\2r\u0185" + + "\3\2\2\2t\u0188\3\2\2\2v\u018c\3\2\2\2x\u018f\3\2\2\2z\u0192\3\2\2\2|" + + "\u0194\3\2\2\2~\u0197\3\2\2\2\u0080\u019a\3\2\2\2\u0082\u019d\3\2\2\2" + + "\u0084\u01a0\3\2\2\2\u0086\u01a3\3\2\2\2\u0088\u01a6\3\2\2\2\u008a\u01a9" + + "\3\2\2\2\u008c\u01ac\3\2\2\2\u008e\u01b0\3\2\2\2\u0090\u01b4\3\2\2\2\u0092" + + "\u01b9\3\2\2\2\u0094\u01c2\3\2\2\2\u0096\u01d4\3\2\2\2\u0098\u01e1\3\2" + + "\2\2\u009a\u0211\3\2\2\2\u009c\u0213\3\2\2\2\u009e\u0224\3\2\2\2\u00a0" + + "\u0229\3\2\2\2\u00a2\u022f\3\2\2\2\u00a4\u0234\3\2\2\2\u00a6\u023c\3\2" + + "\2\2\u00a8\u0247\3\2\2\2\u00aa\u024e\3\2\2\2\u00ac\u025a\3\2\2\2\u00ae" + + "\u025e\3\2\2\2\u00b0\u00b2\t\2\2\2\u00b1\u00b0\3\2\2\2\u00b2\u00b3\3\2" + + "\2\2\u00b3\u00b1\3\2\2\2\u00b3\u00b4\3\2\2\2\u00b4\u00b5\3\2\2\2\u00b5" + + "\u00b6\b\2\2\2\u00b6\5\3\2\2\2\u00b7\u00b8\7\61\2\2\u00b8\u00b9\7\61\2" + + "\2\u00b9\u00bd\3\2\2\2\u00ba\u00bc\13\2\2\2\u00bb\u00ba\3\2\2\2\u00bc" + + "\u00bf\3\2\2\2\u00bd\u00be\3\2\2\2\u00bd\u00bb\3\2\2\2\u00be\u00c0\3\2" + + "\2\2\u00bf\u00bd\3\2\2\2\u00c0\u00cd\t\3\2\2\u00c1\u00c2\7\61\2\2\u00c2" + + "\u00c3\7,\2\2\u00c3\u00c7\3\2\2\2\u00c4\u00c6\13\2\2\2\u00c5\u00c4\3\2" + + "\2\2\u00c6\u00c9\3\2\2\2\u00c7\u00c8\3\2\2\2\u00c7\u00c5\3\2\2\2\u00c8" + + "\u00ca\3\2\2\2\u00c9\u00c7\3\2\2\2\u00ca\u00cb\7,\2\2\u00cb\u00cd\7\61" + + "\2\2\u00cc\u00b7\3\2\2\2\u00cc\u00c1\3\2\2\2\u00cd\u00ce\3\2\2\2\u00ce" + + "\u00cf\b\3\2\2\u00cf\7\3\2\2\2\u00d0\u00d1\7}\2\2\u00d1\t\3\2\2\2\u00d2" + + "\u00d3\7\177\2\2\u00d3\13\3\2\2\2\u00d4\u00d5\7]\2\2\u00d5\r\3\2\2\2\u00d6" + + "\u00d7\7_\2\2\u00d7\17\3\2\2\2\u00d8\u00d9\7*\2\2\u00d9\21\3\2\2\2\u00da" + + "\u00db\7+\2\2\u00db\23\3\2\2\2\u00dc\u00dd\7\60\2\2\u00dd\u00de\3\2\2" + + "\2\u00de\u00df\b\n\3\2\u00df\25\3\2\2\2\u00e0\u00e1\7A\2\2\u00e1\u00e2" + + "\7\60\2\2\u00e2\u00e3\3\2\2\2\u00e3\u00e4\b\13\3\2\u00e4\27\3\2\2\2\u00e5" + + "\u00e6\7.\2\2\u00e6\31\3\2\2\2\u00e7\u00e8\7=\2\2\u00e8\33\3\2\2\2\u00e9" + + "\u00ea\7k\2\2\u00ea\u00eb\7h\2\2\u00eb\35\3\2\2\2\u00ec\u00ed\7k\2\2\u00ed" + + "\u00ee\7p\2\2\u00ee\37\3\2\2\2\u00ef\u00f0\7g\2\2\u00f0\u00f1\7n\2\2\u00f1" + + "\u00f2\7u\2\2\u00f2\u00f3\7g\2\2\u00f3!\3\2\2\2\u00f4\u00f5\7y\2\2\u00f5" + + "\u00f6\7j\2\2\u00f6\u00f7\7k\2\2\u00f7\u00f8\7n\2\2\u00f8\u00f9\7g\2\2" + + "\u00f9#\3\2\2\2\u00fa\u00fb\7f\2\2\u00fb\u00fc\7q\2\2\u00fc%\3\2\2\2\u00fd" + + "\u00fe\7h\2\2\u00fe\u00ff\7q\2\2\u00ff\u0100\7t\2\2\u0100\'\3\2\2\2\u0101" + + "\u0102\7e\2\2\u0102\u0103\7q\2\2\u0103\u0104\7p\2\2\u0104\u0105\7v\2\2" + + "\u0105\u0106\7k\2\2\u0106\u0107\7p\2\2\u0107\u0108\7w\2\2\u0108\u0109" + + "\7g\2\2\u0109)\3\2\2\2\u010a\u010b\7d\2\2\u010b\u010c\7t\2\2\u010c\u010d" + + "\7g\2\2\u010d\u010e\7c\2\2\u010e\u010f\7m\2\2\u010f+\3\2\2\2\u0110\u0111" + + "\7t\2\2\u0111\u0112\7g\2\2\u0112\u0113\7v\2\2\u0113\u0114\7w\2\2\u0114" + + "\u0115\7t\2\2\u0115\u0116\7p\2\2\u0116-\3\2\2\2\u0117\u0118\7p\2\2\u0118" + + "\u0119\7g\2\2\u0119\u011a\7y\2\2\u011a/\3\2\2\2\u011b\u011c\7v\2\2\u011c" + + "\u011d\7t\2\2\u011d\u011e\7{\2\2\u011e\61\3\2\2\2\u011f\u0120\7e\2\2\u0120" + + "\u0121\7c\2\2\u0121\u0122\7v\2\2\u0122\u0123\7e\2\2\u0123\u0124\7j\2\2" + + "\u0124\63\3\2\2\2\u0125\u0126\7v\2\2\u0126\u0127\7j\2\2\u0127\u0128\7" + + "t\2\2\u0128\u0129\7q\2\2\u0129\u012a\7y\2\2\u012a\65\3\2\2\2\u012b\u012c" + + "\7v\2\2\u012c\u012d\7j\2\2\u012d\u012e\7k\2\2\u012e\u012f\7u\2\2\u012f" + + "\67\3\2\2\2\u0130\u0131\7k\2\2\u0131\u0132\7p\2\2\u0132\u0133\7u\2\2\u0133" + + "\u0134\7v\2\2\u0134\u0135\7c\2\2\u0135\u0136\7p\2\2\u0136\u0137\7e\2\2" + + "\u0137\u0138\7g\2\2\u0138\u0139\7q\2\2\u0139\u013a\7h\2\2\u013a9\3\2\2" + + "\2\u013b\u013c\7#\2\2\u013c;\3\2\2\2\u013d\u013e\7\u0080\2\2\u013e=\3" + + "\2\2\2\u013f\u0140\7,\2\2\u0140?\3\2\2\2\u0141\u0142\7\61\2\2\u0142\u0143" + + "\6 \2\2\u0143A\3\2\2\2\u0144\u0145\7\'\2\2\u0145C\3\2\2\2\u0146\u0147" + + "\7-\2\2\u0147E\3\2\2\2\u0148\u0149\7/\2\2\u0149G\3\2\2\2\u014a\u014b\7" + + ">\2\2\u014b\u014c\7>\2\2\u014cI\3\2\2\2\u014d\u014e\7@\2\2\u014e\u014f" + + "\7@\2\2\u014fK\3\2\2\2\u0150\u0151\7@\2\2\u0151\u0152\7@\2\2\u0152\u0153" + + "\7@\2\2\u0153M\3\2\2\2\u0154\u0155\7>\2\2\u0155O\3\2\2\2\u0156\u0157\7" + + ">\2\2\u0157\u0158\7?\2\2\u0158Q\3\2\2\2\u0159\u015a\7@\2\2\u015aS\3\2" + + "\2\2\u015b\u015c\7@\2\2\u015c\u015d\7?\2\2\u015dU\3\2\2\2\u015e\u015f" + + "\7?\2\2\u015f\u0160\7?\2\2\u0160W\3\2\2\2\u0161\u0162\7?\2\2\u0162\u0163" + + "\7?\2\2\u0163\u0164\7?\2\2\u0164Y\3\2\2\2\u0165\u0166\7#\2\2\u0166\u0167" + + "\7?\2\2\u0167[\3\2\2\2\u0168\u0169\7#\2\2\u0169\u016a\7?\2\2\u016a\u016b" + + "\7?\2\2\u016b]\3\2\2\2\u016c\u016d\7(\2\2\u016d_\3\2\2\2\u016e\u016f\7" + + "`\2\2\u016fa\3\2\2\2\u0170\u0171\7~\2\2\u0171c\3\2\2\2\u0172\u0173\7(" + + "\2\2\u0173\u0174\7(\2\2\u0174e\3\2\2\2\u0175\u0176\7~\2\2\u0176\u0177" + + "\7~\2\2\u0177g\3\2\2\2\u0178\u0179\7A\2\2\u0179i\3\2\2\2\u017a\u017b\7" + + "<\2\2\u017bk\3\2\2\2\u017c\u017d\7A\2\2\u017d\u017e\7<\2\2\u017em\3\2" + + "\2\2\u017f\u0180\7<\2\2\u0180\u0181\7<\2\2\u0181o\3\2\2\2\u0182\u0183" + + "\7/\2\2\u0183\u0184\7@\2\2\u0184q\3\2\2\2\u0185\u0186\7?\2\2\u0186\u0187" + + "\7\u0080\2\2\u0187s\3\2\2\2\u0188\u0189\7?\2\2\u0189\u018a\7?\2\2\u018a" + + "\u018b\7\u0080\2\2\u018bu\3\2\2\2\u018c\u018d\7-\2\2\u018d\u018e\7-\2" + + "\2\u018ew\3\2\2\2\u018f\u0190\7/\2\2\u0190\u0191\7/\2\2\u0191y\3\2\2\2" + + "\u0192\u0193\7?\2\2\u0193{\3\2\2\2\u0194\u0195\7-\2\2\u0195\u0196\7?\2" + + "\2\u0196}\3\2\2\2\u0197\u0198\7/\2\2\u0198\u0199\7?\2\2\u0199\177\3\2" + + "\2\2\u019a\u019b\7,\2\2\u019b\u019c\7?\2\2\u019c\u0081\3\2\2\2\u019d\u019e" + + "\7\61\2\2\u019e\u019f\7?\2\2\u019f\u0083\3\2\2\2\u01a0\u01a1\7\'\2\2\u01a1" + + "\u01a2\7?\2\2\u01a2\u0085\3\2\2\2\u01a3\u01a4\7(\2\2\u01a4\u01a5\7?\2" + + "\2\u01a5\u0087\3\2\2\2\u01a6\u01a7\7`\2\2\u01a7\u01a8\7?\2\2\u01a8\u0089" + + "\3\2\2\2\u01a9\u01aa\7~\2\2\u01aa\u01ab\7?\2\2\u01ab\u008b\3\2\2\2\u01ac" + + "\u01ad\7>\2\2\u01ad\u01ae\7>\2\2\u01ae\u01af\7?\2\2\u01af\u008d\3\2\2" + + "\2\u01b0\u01b1\7@\2\2\u01b1\u01b2\7@\2\2\u01b2\u01b3\7?\2\2\u01b3\u008f" + + "\3\2\2\2\u01b4\u01b5\7@\2\2\u01b5\u01b6\7@\2\2\u01b6\u01b7\7@\2\2\u01b7" + + "\u01b8\7?\2\2\u01b8\u0091\3\2\2\2\u01b9\u01bb\7\62\2\2\u01ba\u01bc\t\4" + + "\2\2\u01bb\u01ba\3\2\2\2\u01bc\u01bd\3\2\2\2\u01bd\u01bb\3\2\2\2\u01bd" + + "\u01be\3\2\2\2\u01be\u01c0\3\2\2\2\u01bf\u01c1\t\5\2\2\u01c0\u01bf\3\2" + + "\2\2\u01c0\u01c1\3\2\2\2\u01c1\u0093\3\2\2\2\u01c2\u01c3\7\62\2\2\u01c3" + + "\u01c5\t\6\2\2\u01c4\u01c6\t\7\2\2\u01c5\u01c4\3\2\2\2\u01c6\u01c7\3\2" + + "\2\2\u01c7\u01c5\3\2\2\2\u01c7\u01c8\3\2\2\2\u01c8\u01ca\3\2\2\2\u01c9" + + "\u01cb\t\5\2\2\u01ca\u01c9\3\2\2\2\u01ca\u01cb\3\2\2\2\u01cb\u0095\3\2" + + "\2\2\u01cc\u01d5\7\62\2\2\u01cd\u01d1\t\b\2\2\u01ce\u01d0\t\t\2\2\u01cf" + + "\u01ce\3\2\2\2\u01d0\u01d3\3\2\2\2\u01d1\u01cf\3\2\2\2\u01d1\u01d2\3\2" + + "\2\2\u01d2\u01d5\3\2\2\2\u01d3\u01d1\3\2\2\2\u01d4\u01cc\3\2\2\2\u01d4" + + "\u01cd\3\2\2\2\u01d5\u01d7\3\2\2\2\u01d6\u01d8\t\n\2\2\u01d7\u01d6\3\2" + + "\2\2\u01d7\u01d8\3\2\2\2\u01d8\u0097\3\2\2\2\u01d9\u01e2\7\62\2\2\u01da" + + "\u01de\t\b\2\2\u01db\u01dd\t\t\2\2\u01dc\u01db\3\2\2\2\u01dd\u01e0\3\2" + + "\2\2\u01de\u01dc\3\2\2\2\u01de\u01df\3\2\2\2\u01df\u01e2\3\2\2\2\u01e0" + + "\u01de\3\2\2\2\u01e1\u01d9\3\2\2\2\u01e1\u01da\3\2\2\2\u01e2\u01e9\3\2" + + "\2\2\u01e3\u01e5\5\24\n\2\u01e4\u01e6\t\t\2\2\u01e5\u01e4\3\2\2\2\u01e6" + + "\u01e7\3\2\2\2\u01e7\u01e5\3\2\2\2\u01e7\u01e8\3\2\2\2\u01e8\u01ea\3\2" + + "\2\2\u01e9\u01e3\3\2\2\2\u01e9\u01ea\3\2\2\2\u01ea\u01f4\3\2\2\2\u01eb" + + "\u01ed\t\13\2\2\u01ec\u01ee\t\f\2\2\u01ed\u01ec\3\2\2\2\u01ed\u01ee\3" + + "\2\2\2\u01ee\u01f0\3\2\2\2\u01ef\u01f1\t\t\2\2\u01f0\u01ef\3\2\2\2\u01f1" + + "\u01f2\3\2\2\2\u01f2\u01f0\3\2\2\2\u01f2\u01f3\3\2\2\2\u01f3\u01f5\3\2" + + "\2\2\u01f4\u01eb\3\2\2\2\u01f4\u01f5\3\2\2\2\u01f5\u01f7\3\2\2\2\u01f6" + + "\u01f8\t\r\2\2\u01f7\u01f6\3\2\2\2\u01f7\u01f8\3\2\2\2\u01f8\u0099\3\2" + + "\2\2\u01f9\u0201\7$\2\2\u01fa\u01fb\7^\2\2\u01fb\u0200\7$\2\2\u01fc\u01fd" + + "\7^\2\2\u01fd\u0200\7^\2\2\u01fe\u0200\n\16\2\2\u01ff\u01fa\3\2\2\2\u01ff" + + "\u01fc\3\2\2\2\u01ff\u01fe\3\2\2\2\u0200\u0203\3\2\2\2\u0201\u0202\3\2" + + "\2\2\u0201\u01ff\3\2\2\2\u0202\u0204\3\2\2\2\u0203\u0201\3\2\2\2\u0204" + + "\u0212\7$\2\2\u0205\u020d\7)\2\2\u0206\u0207\7^\2\2\u0207\u020c\7)\2\2" + + "\u0208\u0209\7^\2\2\u0209\u020c\7^\2\2\u020a\u020c\n\17\2\2\u020b\u0206" + + "\3\2\2\2\u020b\u0208\3\2\2\2\u020b\u020a\3\2\2\2\u020c\u020f\3\2\2\2\u020d" + + "\u020e\3\2\2\2\u020d\u020b\3\2\2\2\u020e\u0210\3\2\2\2\u020f\u020d\3\2" + + "\2\2\u0210\u0212\7)\2\2\u0211\u01f9\3\2\2\2\u0211\u0205\3\2\2\2\u0212" + + "\u009b\3\2\2\2\u0213\u0217\7\61\2\2\u0214\u0215\7^\2\2\u0215\u0218\n\20" + + "\2\2\u0216\u0218\n\21\2\2\u0217\u0214\3\2\2\2\u0217\u0216\3\2\2\2\u0218" + + "\u0219\3\2\2\2\u0219\u021a\3\2\2\2\u0219\u0217\3\2\2\2\u021a\u021b\3\2" + + "\2\2\u021b\u021f\7\61\2\2\u021c\u021e\t\22\2\2\u021d\u021c\3\2\2\2\u021e" + + "\u0221\3\2\2\2\u021f\u021d\3\2\2\2\u021f\u0220\3\2\2\2\u0220\u0222\3\2" + + "\2\2\u0221\u021f\3\2\2\2\u0222\u0223\6N\3\2\u0223\u009d\3\2\2\2\u0224" + + "\u0225\7v\2\2\u0225\u0226\7t\2\2\u0226\u0227\7w\2\2\u0227\u0228\7g\2\2" + + "\u0228\u009f\3\2\2\2\u0229\u022a\7h\2\2\u022a\u022b\7c\2\2\u022b\u022c" + + "\7n\2\2\u022c\u022d\7u\2\2\u022d\u022e\7g\2\2\u022e\u00a1\3\2\2\2\u022f" + + "\u0230\7p\2\2\u0230\u0231\7w\2\2\u0231\u0232\7n\2\2\u0232\u0233\7n\2\2" + + "\u0233\u00a3\3\2\2\2\u0234\u0238\5\u00a6S\2\u0235\u0236\5\f\6\2\u0236" + + "\u0237\5\16\7\2\u0237\u0239\3\2\2\2\u0238\u0235\3\2\2\2\u0239\u023a\3" + + "\2\2\2\u023a\u0238\3\2\2\2\u023a\u023b\3\2\2\2\u023b\u00a5\3\2\2\2\u023c" + + "\u0242\5\u00a8T\2\u023d\u023e\5\24\n\2\u023e\u023f\5\u00a8T\2\u023f\u0241" + + "\3\2\2\2\u0240\u023d\3\2\2\2\u0241\u0244\3\2\2\2\u0242\u0240\3\2\2\2\u0242" + + "\u0243\3\2\2\2\u0243\u0245\3\2\2\2\u0244\u0242\3\2\2\2\u0245\u0246\6S" + + "\4\2\u0246\u00a7\3\2\2\2\u0247\u024b\t\23\2\2\u0248\u024a\t\24\2\2\u0249" + + "\u0248\3\2\2\2\u024a\u024d\3\2\2\2\u024b\u0249\3\2\2\2\u024b\u024c\3\2" + + "\2\2\u024c\u00a9\3\2\2\2\u024d\u024b\3\2\2\2\u024e\u024f\13\2\2\2\u024f" + + "\u0250\3\2\2\2\u0250\u0251\bU\2\2\u0251\u00ab\3\2\2\2\u0252\u025b\7\62" + + "\2\2\u0253\u0257\t\b\2\2\u0254\u0256\t\t\2\2\u0255\u0254\3\2\2\2\u0256" + + "\u0259\3\2\2\2\u0257\u0255\3\2\2\2\u0257\u0258\3\2\2\2\u0258\u025b\3\2" + + "\2\2\u0259\u0257\3\2\2\2\u025a\u0252\3\2\2\2\u025a\u0253\3\2\2\2\u025b" + + "\u025c\3\2\2\2\u025c\u025d\bV\4\2\u025d\u00ad\3\2\2\2\u025e\u0262\t\23" + + "\2\2\u025f\u0261\t\24\2\2\u0260\u025f\3\2\2\2\u0261\u0264\3\2\2\2\u0262" + + "\u0260\3\2\2\2\u0262\u0263\3\2\2\2\u0263\u0265\3\2\2\2\u0264\u0262\3\2" + + "\2\2\u0265\u0266\bW\4\2\u0266\u00af\3\2\2\2%\2\3\u00b3\u00bd\u00c7\u00cc" + + "\u01bd\u01c0\u01c7\u01ca\u01d1\u01d4\u01d7\u01de\u01e1\u01e7\u01e9\u01ed" + + "\u01f2\u01f4\u01f7\u01ff\u0201\u020b\u020d\u0211\u0217\u0219\u021f\u023a" + + "\u0242\u024b\u0257\u025a\u0262\5\b\2\2\4\3\2\4\2\2"; + public static final ATN _ATN = new ATNDeserializer().deserialize(_serializedATN.toCharArray()); + static { + _decisionToDFA = new DFA[_ATN.getNumberOfDecisions()]; + for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) { + _decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i); + } } - } } diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/Walker.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/Walker.java index 608ad449be3bb..2e63023a2f556 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/Walker.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/Walker.java @@ -172,7 +172,7 @@ private Walker(String sourceName, String sourceText, CompilerSettings settings) this.identifier = 0; - this.source = (SClass)visit(buildAntlrTree(sourceText)); + this.source = (SClass) visit(buildAntlrTree(sourceText)); } private int nextIdentifier() { @@ -203,10 +203,15 @@ private void setupPicky(PainlessParser parser) { // a second listener to fail the test when the above happens. parser.addErrorListener(new BaseErrorListener() { @Override - public void syntaxError(final Recognizer recognizer, final Object offendingSymbol, final int line, - final int charPositionInLine, final String msg, final RecognitionException e) { - throw new AssertionError("line: " + line + ", offset: " + charPositionInLine + - ", symbol:" + offendingSymbol + " " + msg); + public void syntaxError( + final Recognizer recognizer, + final Object offendingSymbol, + final int line, + final int charPositionInLine, + final String msg, + final RecognitionException e + ) { + throw new AssertionError("line: " + line + ", offset: " + charPositionInLine + ", symbol:" + offendingSymbol + " " + msg); } }); @@ -228,7 +233,7 @@ public ANode visitSource(SourceContext ctx) { List functions = new ArrayList<>(); for (FunctionContext function : ctx.function()) { - functions.add((SFunction)visit(function)); + functions.add((SFunction) visit(function)); } // handle the code to generate the execute method here @@ -237,12 +242,23 @@ public ANode visitSource(SourceContext ctx) { List statements = new ArrayList<>(); for (StatementContext statement : ctx.statement()) { - statements.add((AStatement)visit(statement)); + statements.add((AStatement) visit(statement)); } // generate the execute method from the collected statements and parameters - SFunction execute = new SFunction(nextIdentifier(), location(ctx), "", "execute", emptyList(), emptyList(), - new SBlock(nextIdentifier(), location(ctx), statements), false, false, false, false); + SFunction execute = new SFunction( + nextIdentifier(), + location(ctx), + "", + "execute", + emptyList(), + emptyList(), + new SBlock(nextIdentifier(), location(ctx), statements), + false, + false, + false, + false + ); functions.add(execute); return new SClass(nextIdentifier(), location(ctx), functions); @@ -265,15 +281,26 @@ public ANode visitFunction(FunctionContext ctx) { } for (StatementContext statement : ctx.block().statement()) { - statements.add((AStatement)visit(statement)); + statements.add((AStatement) visit(statement)); } if (ctx.block().dstatement() != null) { - statements.add((AStatement)visit(ctx.block().dstatement())); + statements.add((AStatement) visit(ctx.block().dstatement())); } - return new SFunction(nextIdentifier(), location(ctx), - rtnType, name, paramTypes, paramNames, new SBlock(nextIdentifier(), location(ctx), statements), false, false, false, false); + return new SFunction( + nextIdentifier(), + location(ctx), + rtnType, + name, + paramTypes, + paramNames, + new SBlock(nextIdentifier(), location(ctx), statements), + false, + false, + false, + false + ); } @Override @@ -294,11 +321,11 @@ public ANode visitStatement(StatementContext ctx) { @Override public ANode visitIf(IfContext ctx) { - AExpression expression = (AExpression)visit(ctx.expression()); - SBlock ifblock = (SBlock)visit(ctx.trailer(0)); + AExpression expression = (AExpression) visit(ctx.expression()); + SBlock ifblock = (SBlock) visit(ctx.trailer(0)); if (ctx.trailer().size() > 1) { - SBlock elseblock = (SBlock)visit(ctx.trailer(1)); + SBlock elseblock = (SBlock) visit(ctx.trailer(1)); return new SIfElse(nextIdentifier(), location(ctx), expression, ifblock, elseblock); } else { @@ -308,10 +335,10 @@ public ANode visitIf(IfContext ctx) { @Override public ANode visitWhile(WhileContext ctx) { - AExpression expression = (AExpression)visit(ctx.expression()); + AExpression expression = (AExpression) visit(ctx.expression()); if (ctx.trailer() != null) { - SBlock block = (SBlock)visit(ctx.trailer()); + SBlock block = (SBlock) visit(ctx.trailer()); return new SWhile(nextIdentifier(), location(ctx), expression, block); } else if (ctx.empty() != null) { @@ -323,8 +350,8 @@ public ANode visitWhile(WhileContext ctx) { @Override public ANode visitDo(DoContext ctx) { - AExpression expression = (AExpression)visit(ctx.expression()); - SBlock block = (SBlock)visit(ctx.block()); + AExpression expression = (AExpression) visit(ctx.expression()); + SBlock block = (SBlock) visit(ctx.block()); return new SDo(nextIdentifier(), location(ctx), expression, block); } @@ -332,11 +359,11 @@ public ANode visitDo(DoContext ctx) { @Override public ANode visitFor(ForContext ctx) { ANode initializer = ctx.initializer() == null ? null : visit(ctx.initializer()); - AExpression expression = ctx.expression() == null ? null : (AExpression)visit(ctx.expression()); - AExpression afterthought = ctx.afterthought() == null ? null : (AExpression)visit(ctx.afterthought()); + AExpression expression = ctx.expression() == null ? null : (AExpression) visit(ctx.expression()); + AExpression afterthought = ctx.afterthought() == null ? null : (AExpression) visit(ctx.afterthought()); if (ctx.trailer() != null) { - SBlock block = (SBlock)visit(ctx.trailer()); + SBlock block = (SBlock) visit(ctx.trailer()); return new SFor(nextIdentifier(), location(ctx), initializer, expression, afterthought, block); } else if (ctx.empty() != null) { @@ -350,8 +377,8 @@ public ANode visitFor(ForContext ctx) { public ANode visitEach(EachContext ctx) { String type = ctx.decltype().getText(); String name = ctx.ID().getText(); - AExpression expression = (AExpression)visit(ctx.expression()); - SBlock block = (SBlock)visit(ctx.trailer()); + AExpression expression = (AExpression) visit(ctx.expression()); + SBlock block = (SBlock) visit(ctx.trailer()); return new SEach(nextIdentifier(), location(ctx), type, name, expression, block); } @@ -359,8 +386,8 @@ public ANode visitEach(EachContext ctx) { @Override public ANode visitIneach(IneachContext ctx) { String name = ctx.ID().getText(); - AExpression expression = (AExpression)visit(ctx.expression()); - SBlock block = (SBlock)visit(ctx.trailer()); + AExpression expression = (AExpression) visit(ctx.expression()); + SBlock block = (SBlock) visit(ctx.trailer()); return new SEach(nextIdentifier(), location(ctx), "def", name, expression, block); } @@ -393,11 +420,11 @@ public ANode visitReturn(ReturnContext ctx) { @Override public ANode visitTry(TryContext ctx) { - SBlock block = (SBlock)visit(ctx.block()); + SBlock block = (SBlock) visit(ctx.block()); List catches = new ArrayList<>(); for (TrapContext trap : ctx.trap()) { - catches.add((SCatch)visit(trap)); + catches.add((SCatch) visit(trap)); } return new STry(nextIdentifier(), location(ctx), block, catches); @@ -405,14 +432,14 @@ public ANode visitTry(TryContext ctx) { @Override public ANode visitThrow(ThrowContext ctx) { - AExpression expression = (AExpression)visit(ctx.expression()); + AExpression expression = (AExpression) visit(ctx.expression()); return new SThrow(nextIdentifier(), location(ctx), expression); } @Override public ANode visitExpr(ExprContext ctx) { - AExpression expression = (AExpression)visit(ctx.expression()); + AExpression expression = (AExpression) visit(ctx.expression()); return new SExpression(nextIdentifier(), location(ctx), expression); } @@ -423,7 +450,7 @@ public ANode visitTrailer(TrailerContext ctx) { return visit(ctx.block()); } else if (ctx.statement() != null) { List statements = new ArrayList<>(); - statements.add((AStatement)visit(ctx.statement())); + statements.add((AStatement) visit(ctx.statement())); return new SBlock(nextIdentifier(), location(ctx), statements); } else { @@ -439,11 +466,11 @@ public ANode visitBlock(BlockContext ctx) { List statements = new ArrayList<>(); for (StatementContext statement : ctx.statement()) { - statements.add((AStatement)visit(statement)); + statements.add((AStatement) visit(statement)); } if (ctx.dstatement() != null) { - statements.add((AStatement)visit(ctx.dstatement())); + statements.add((AStatement) visit(ctx.dstatement())); } return new SBlock(nextIdentifier(), location(ctx), statements); @@ -478,7 +505,7 @@ public ANode visitDeclaration(DeclarationContext ctx) { for (DeclvarContext declvar : ctx.declvar()) { String name = declvar.ID().getText(); - AExpression expression = declvar.expression() == null ? null : (AExpression)visit(declvar.expression()); + AExpression expression = declvar.expression() == null ? null : (AExpression) visit(declvar.expression()); declarations.add(new SDeclaration(nextIdentifier(), location(declvar), type, name, expression)); } @@ -504,7 +531,7 @@ public ANode visitDeclvar(DeclvarContext ctx) { public ANode visitTrap(TrapContext ctx) { String type = ctx.type().getText(); String name = ctx.ID().getText(); - SBlock block = (SBlock)visit(ctx.block()); + SBlock block = (SBlock) visit(ctx.block()); return new SCatch(nextIdentifier(), location(ctx), Exception.class, type, name, block); } @@ -516,8 +543,8 @@ public ANode visitSingle(SingleContext ctx) { @Override public ANode visitBinary(BinaryContext ctx) { - AExpression left = (AExpression)visit(ctx.noncondexpression(0)); - AExpression right = (AExpression)visit(ctx.noncondexpression(1)); + AExpression left = (AExpression) visit(ctx.noncondexpression(0)); + AExpression right = (AExpression) visit(ctx.noncondexpression(1)); final Operation operation; if (ctx.MUL() != null) { @@ -555,8 +582,8 @@ public ANode visitBinary(BinaryContext ctx) { @Override public ANode visitComp(CompContext ctx) { - AExpression left = (AExpression)visit(ctx.noncondexpression(0)); - AExpression right = (AExpression)visit(ctx.noncondexpression(1)); + AExpression left = (AExpression) visit(ctx.noncondexpression(0)); + AExpression right = (AExpression) visit(ctx.noncondexpression(1)); final Operation operation; if (ctx.LT() != null) { @@ -584,7 +611,7 @@ public ANode visitComp(CompContext ctx) { @Override public ANode visitInstanceof(InstanceofContext ctx) { - AExpression expr = (AExpression)visit(ctx.noncondexpression()); + AExpression expr = (AExpression) visit(ctx.noncondexpression()); String type = ctx.decltype().getText(); return new EInstanceof(nextIdentifier(), location(ctx), expr, type); @@ -592,8 +619,8 @@ public ANode visitInstanceof(InstanceofContext ctx) { @Override public ANode visitBool(BoolContext ctx) { - AExpression left = (AExpression)visit(ctx.noncondexpression(0)); - AExpression right = (AExpression)visit(ctx.noncondexpression(1)); + AExpression left = (AExpression) visit(ctx.noncondexpression(0)); + AExpression right = (AExpression) visit(ctx.noncondexpression(1)); final Operation operation; if (ctx.BOOLAND() != null) { @@ -609,8 +636,8 @@ public ANode visitBool(BoolContext ctx) { @Override public ANode visitElvis(ElvisContext ctx) { - AExpression left = (AExpression)visit(ctx.noncondexpression(0)); - AExpression right = (AExpression)visit(ctx.noncondexpression(1)); + AExpression left = (AExpression) visit(ctx.noncondexpression(0)); + AExpression right = (AExpression) visit(ctx.noncondexpression(1)); return new EElvis(nextIdentifier(), location(ctx), left, right); } @@ -622,17 +649,17 @@ public ANode visitNonconditional(NonconditionalContext ctx) { @Override public ANode visitConditional(ConditionalContext ctx) { - AExpression condition = (AExpression)visit(ctx.noncondexpression()); - AExpression left = (AExpression)visit(ctx.expression(0)); - AExpression right = (AExpression)visit(ctx.expression(1)); + AExpression condition = (AExpression) visit(ctx.noncondexpression()); + AExpression left = (AExpression) visit(ctx.expression(0)); + AExpression right = (AExpression) visit(ctx.expression(1)); return new EConditional(nextIdentifier(), location(ctx), condition, left, right); } @Override public ANode visitAssignment(AssignmentContext ctx) { - AExpression lhs = (AExpression)visit(ctx.noncondexpression()); - AExpression rhs = (AExpression)visit(ctx.expression()); + AExpression lhs = (AExpression) visit(ctx.noncondexpression()); + AExpression rhs = (AExpression) visit(ctx.expression()); final Operation operation; @@ -669,7 +696,7 @@ public ANode visitAssignment(AssignmentContext ctx) { @Override public ANode visitPre(PreContext ctx) { - AExpression expression = (AExpression)visit(ctx.chain()); + AExpression expression = (AExpression) visit(ctx.chain()); final Operation operation; @@ -681,13 +708,19 @@ public ANode visitPre(PreContext ctx) { throw location(ctx).createError(new IllegalStateException("illegal tree structure")); } - return new EAssignment(nextIdentifier(), location(ctx), expression, - new ENumeric(nextIdentifier(), location(ctx), "1", 10), false, operation); + return new EAssignment( + nextIdentifier(), + location(ctx), + expression, + new ENumeric(nextIdentifier(), location(ctx), "1", 10), + false, + operation + ); } @Override public ANode visitAddsub(AddsubContext ctx) { - AExpression expression = (AExpression)visit(ctx.unary()); + AExpression expression = (AExpression) visit(ctx.unary()); final Operation operation; @@ -714,7 +747,7 @@ public ANode visitRead(ReadContext ctx) { @Override public ANode visitPost(PostContext ctx) { - AExpression expression = (AExpression)visit(ctx.chain()); + AExpression expression = (AExpression) visit(ctx.chain()); final Operation operation; @@ -726,13 +759,19 @@ public ANode visitPost(PostContext ctx) { throw location(ctx).createError(new IllegalStateException("illegal tree structure")); } - return new EAssignment(nextIdentifier(), location(ctx), expression, - new ENumeric(nextIdentifier(), location(ctx), "1", 10), true, operation); + return new EAssignment( + nextIdentifier(), + location(ctx), + expression, + new ENumeric(nextIdentifier(), location(ctx), "1", 10), + true, + operation + ); } @Override public ANode visitNot(NotContext ctx) { - AExpression expression = (AExpression)visit(ctx.unary()); + AExpression expression = (AExpression) visit(ctx.unary()); final Operation operation; @@ -755,7 +794,7 @@ public ANode visitCast(CastContext ctx) { @Override public ANode visitPrimordefcast(PainlessParser.PrimordefcastContext ctx) { String type = ctx.primordefcasttype().getText(); - AExpression child = (AExpression)visit(ctx.unary()); + AExpression child = (AExpression) visit(ctx.unary()); return new EExplicit(nextIdentifier(), location(ctx), type, child); } @@ -763,7 +802,7 @@ public ANode visitPrimordefcast(PainlessParser.PrimordefcastContext ctx) { @Override public ANode visitRefcast(PainlessParser.RefcastContext ctx) { String type = ctx.refcasttype().getText(); - AExpression child = (AExpression)visit(ctx.unarynotaddsub()); + AExpression child = (AExpression) visit(ctx.unarynotaddsub()); return new EExplicit(nextIdentifier(), location(ctx), type, child); } @@ -780,7 +819,7 @@ public ANode visitRefcasttype(PainlessParser.RefcasttypeContext ctx) { @Override public ANode visitDynamic(DynamicContext ctx) { - AExpression primary = (AExpression)visit(ctx.primary()); + AExpression primary = (AExpression) visit(ctx.primary()); return buildPostfixChain(primary, null, ctx.postfix()); } @@ -976,7 +1015,7 @@ public ANode visitBraceaccess(BraceaccessContext ctx) { } public AExpression visitBraceaccess(BraceaccessContext ctx, AExpression prefix) { - AExpression expression = (AExpression)visit(ctx.expression()); + AExpression expression = (AExpression) visit(ctx.expression()); return new EBrace(nextIdentifier(), location(ctx), prefix, expression); } @@ -988,11 +1027,14 @@ public ANode visitNewstandardarray(NewstandardarrayContext ctx) { for (ExpressionContext expression : ctx.expression()) { type.append("[]"); - expressions.add((AExpression)visit(expression)); + expressions.add((AExpression) visit(expression)); } return buildPostfixChain( - new ENewArray(nextIdentifier(), location(ctx), type.toString(), expressions, false), ctx.postdot(), ctx.postfix()); + new ENewArray(nextIdentifier(), location(ctx), type.toString(), expressions, false), + ctx.postdot(), + ctx.postfix() + ); } @Override @@ -1001,7 +1043,7 @@ public ANode visitNewinitializedarray(NewinitializedarrayContext ctx) { List expressions = new ArrayList<>(); for (ExpressionContext expression : ctx.expression()) { - expressions.add((AExpression)visit(expression)); + expressions.add((AExpression) visit(expression)); } return buildPostfixChain(new ENewArray(nextIdentifier(), location(ctx), type, expressions, true), null, ctx.postfix()); @@ -1012,7 +1054,7 @@ public ANode visitListinitializer(ListinitializerContext ctx) { List values = new ArrayList<>(); for (ExpressionContext expression : ctx.expression()) { - values.add((AExpression)visit(expression)); + values.add((AExpression) visit(expression)); } return new EListInit(nextIdentifier(), location(ctx), values); @@ -1024,8 +1066,8 @@ public ANode visitMapinitializer(MapinitializerContext ctx) { List values = new ArrayList<>(); for (MaptokenContext maptoken : ctx.maptoken()) { - keys.add((AExpression)visit(maptoken.expression(0))); - values.add((AExpression)visit(maptoken.expression(1))); + keys.add((AExpression) visit(maptoken.expression(0))); + values.add((AExpression) visit(maptoken.expression(1))); } return new EMapInit(nextIdentifier(), location(ctx), keys, values); @@ -1045,7 +1087,7 @@ private List collectArguments(ArgumentsContext ctx) { List arguments = new ArrayList<>(); for (ArgumentContext argument : ctx.argument()) { - arguments.add((AExpression)visit(argument)); + arguments.add((AExpression) visit(argument)); } return arguments; @@ -1082,11 +1124,14 @@ public ANode visitLambda(LambdaContext ctx) { if (ctx.expression() != null) { // single expression - AExpression expression = (AExpression)visit(ctx.expression()); - block = new SBlock(nextIdentifier(), location(ctx), - Collections.singletonList(new SReturn(nextIdentifier(), location(ctx), expression))); + AExpression expression = (AExpression) visit(ctx.expression()); + block = new SBlock( + nextIdentifier(), + location(ctx), + Collections.singletonList(new SReturn(nextIdentifier(), location(ctx), expression)) + ); } else { - block = (SBlock)visit(ctx.block()); + block = (SBlock) visit(ctx.block()); } return new ELambda(nextIdentifier(), location(ctx), paramTypes, paramNames, block); @@ -1104,9 +1149,9 @@ public ANode visitClassfuncref(ClassfuncrefContext ctx) { @Override public ANode visitConstructorfuncref(ConstructorfuncrefContext ctx) { - return ctx.decltype().LBRACE().isEmpty() ? - new EFunctionRef(nextIdentifier(), location(ctx), ctx.decltype().getText(), ctx.NEW().getText()) : - new ENewArrayFunctionRef(nextIdentifier(), location(ctx), ctx.decltype().getText()); + return ctx.decltype().LBRACE().isEmpty() + ? new EFunctionRef(nextIdentifier(), location(ctx), ctx.decltype().getText(), ctx.NEW().getText()) + : new ENewArrayFunctionRef(nextIdentifier(), location(ctx), ctx.decltype().getText()); } @Override diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/api/Augmentation.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/api/Augmentation.java index 03657f2200a59..2c056d50539ca 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/api/Augmentation.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/api/Augmentation.java @@ -69,7 +69,7 @@ public static boolean any(Iterable receiver, Predicate predicate) { /** Converts this Iterable to a Collection. Returns the original Iterable if it is already a Collection. */ public static Collection asCollection(Iterable receiver) { if (receiver instanceof Collection) { - return (Collection)receiver; + return (Collection) receiver; } List list = new ArrayList<>(); for (T t : receiver) { @@ -81,7 +81,7 @@ public static Collection asCollection(Iterable receiver) { /** Converts this Iterable to a List. Returns the original Iterable if it is already a List. */ public static List asList(Iterable receiver) { if (receiver instanceof List) { - return (List)receiver; + return (List) receiver; } List list = new ArrayList<>(); for (T t : receiver) { @@ -138,13 +138,13 @@ public static boolean every(Iterable receiver, Predicate predicate) { * Iterates through the Iterable transforming items using the supplied function and * collecting any non-null results. */ - public static List findResults(Iterable receiver, Function filter) { + public static List findResults(Iterable receiver, Function filter) { List list = new ArrayList<>(); - for (T t: receiver) { - U result = filter.apply(t); - if (result != null) { - list.add(result); - } + for (T t : receiver) { + U result = filter.apply(t); + if (result != null) { + list.add(result); + } } return list; } @@ -152,8 +152,8 @@ public static List findResults(Iterable receiver, Function filt /** * Sorts all Iterable members into groups determined by the supplied mapping function. */ - public static Map> groupBy(Iterable receiver, Function mapper) { - Map> map = new LinkedHashMap<>(); + public static Map> groupBy(Iterable receiver, Function mapper) { + Map> map = new LinkedHashMap<>(); for (T t : receiver) { U mapped = mapper.apply(t); List results = map.get(mapped); @@ -175,7 +175,7 @@ public static String join(Iterable receiver, String separator) { boolean firstToken = true; for (T t : receiver) { if (firstToken) { - firstToken=false; + firstToken = false; } else { sb.append(separator); } @@ -213,7 +213,7 @@ public static double sum(Iterable receiver, ToDoubleFunction function) * Iterates through this collection transforming each entry into a new value using * the function, returning a list of transformed values. */ - public static List collect(Collection receiver, Function function) { + public static List collect(Collection receiver, Function function) { List list = new ArrayList<>(); for (T t : receiver) { list.add(function.apply(t)); @@ -225,7 +225,7 @@ public static List collect(Collection receiver, Function functi * Iterates through this collection transforming each entry into a new value using * the function, adding the values to the specified collection. */ - public static Object collect(Collection receiver, Collection collection, Function function) { + public static Object collect(Collection receiver, Collection collection, Function function) { for (T t : receiver) { collection.add(function.apply(t)); } @@ -262,7 +262,7 @@ public static List findAll(Collection receiver, Predicate predicate * but stopping once the first non-null result is found and returning that result. * If all results are null, null is returned. */ - public static Object findResult(Collection receiver, Function function) { + public static Object findResult(Collection receiver, Function function) { return findResult(receiver, null, function); } @@ -271,7 +271,7 @@ public static Object findResult(Collection receiver, Function func * but stopping once the first non-null result is found and returning that result. * If all results are null, defaultResult is returned. */ - public static Object findResult(Collection receiver, Object defaultResult, Function function) { + public static Object findResult(Collection receiver, Object defaultResult, Function function) { for (T t : receiver) { U value = function.apply(t); if (value != null) { @@ -308,9 +308,9 @@ public static List> split(Collection receiver, Predicate predi * Iterates through this map transforming each entry into a new value using * the function, returning a list of transformed values. */ - public static List collect(Map receiver, BiFunction function) { + public static List collect(Map receiver, BiFunction function) { List list = new ArrayList<>(); - for (Map.Entry kvPair : receiver.entrySet()) { + for (Map.Entry kvPair : receiver.entrySet()) { list.add(function.apply(kvPair.getKey(), kvPair.getValue())); } return list; @@ -320,17 +320,17 @@ public static List collect(Map receiver, BiFunction funct * Iterates through this map transforming each entry into a new value using * the function, adding the values to the specified collection. */ - public static Object collect(Map receiver, Collection collection, BiFunction function) { - for (Map.Entry kvPair : receiver.entrySet()) { + public static Object collect(Map receiver, Collection collection, BiFunction function) { + for (Map.Entry kvPair : receiver.entrySet()) { collection.add(function.apply(kvPair.getKey(), kvPair.getValue())); } return collection; } /** Counts the number of occurrences which satisfy the given predicate from inside this Map */ - public static int count(Map receiver, BiPredicate predicate) { + public static int count(Map receiver, BiPredicate predicate) { int count = 0; - for (Map.Entry kvPair : receiver.entrySet()) { + for (Map.Entry kvPair : receiver.entrySet()) { if (predicate.test(kvPair.getKey(), kvPair.getValue())) { count++; } @@ -339,7 +339,7 @@ public static int count(Map receiver, BiPredicate predicate) { } /** Iterates through a Map, passing each item to the given consumer. */ - public static Object each(Map receiver, BiConsumer consumer) { + public static Object each(Map receiver, BiConsumer consumer) { receiver.forEach(consumer); return receiver; } @@ -347,8 +347,8 @@ public static Object each(Map receiver, BiConsumer consumer) { /** * Used to determine if the given predicate is valid (i.e. returns true for all items in this map). */ - public static boolean every(Map receiver, BiPredicate predicate) { - for (Map.Entry kvPair : receiver.entrySet()) { + public static boolean every(Map receiver, BiPredicate predicate) { + for (Map.Entry kvPair : receiver.entrySet()) { if (predicate.test(kvPair.getKey(), kvPair.getValue()) == false) { return false; } @@ -359,8 +359,8 @@ public static boolean every(Map receiver, BiPredicate predicate) /** * Finds the first entry matching the predicate, or returns null. */ - public static Map.Entry find(Map receiver, BiPredicate predicate) { - for (Map.Entry kvPair : receiver.entrySet()) { + public static Map.Entry find(Map receiver, BiPredicate predicate) { + for (Map.Entry kvPair : receiver.entrySet()) { if (predicate.test(kvPair.getKey(), kvPair.getValue())) { return kvPair; } @@ -371,15 +371,15 @@ public static Map.Entry find(Map receiver, BiPredicate pred /** * Finds all values matching the predicate, returns as a map. */ - public static Map findAll(Map receiver, BiPredicate predicate) { + public static Map findAll(Map receiver, BiPredicate predicate) { // try to preserve some properties of the receiver (see the groovy javadocs) - final Map map; + final Map map; if (receiver instanceof TreeMap) { map = new TreeMap<>(); } else { map = new LinkedHashMap<>(); } - for (Map.Entry kvPair : receiver.entrySet()) { + for (Map.Entry kvPair : receiver.entrySet()) { if (predicate.test(kvPair.getKey(), kvPair.getValue())) { map.put(kvPair.getKey(), kvPair.getValue()); } @@ -392,7 +392,7 @@ public static Map findAll(Map receiver, BiPredicate predica * but stopping once the first non-null result is found and returning that result. * If all results are null, null is returned. */ - public static Object findResult(Map receiver, BiFunction function) { + public static Object findResult(Map receiver, BiFunction function) { return findResult(receiver, null, function); } @@ -401,8 +401,8 @@ public static Object findResult(Map receiver, BiFunction fun * but stopping once the first non-null result is found and returning that result. * If all results are null, defaultResult is returned. */ - public static Object findResult(Map receiver, Object defaultResult, BiFunction function) { - for (Map.Entry kvPair : receiver.entrySet()) { + public static Object findResult(Map receiver, Object defaultResult, BiFunction function) { + for (Map.Entry kvPair : receiver.entrySet()) { T value = function.apply(kvPair.getKey(), kvPair.getValue()); if (value != null) { return value; @@ -415,13 +415,13 @@ public static Object findResult(Map receiver, Object defaultResult, * Iterates through the map transforming items using the supplied function and * collecting any non-null results. */ - public static List findResults(Map receiver, BiFunction filter) { + public static List findResults(Map receiver, BiFunction filter) { List list = new ArrayList<>(); - for (Map.Entry kvPair : receiver.entrySet()) { - T result = filter.apply(kvPair.getKey(), kvPair.getValue()); - if (result != null) { - list.add(result); - } + for (Map.Entry kvPair : receiver.entrySet()) { + T result = filter.apply(kvPair.getKey(), kvPair.getValue()); + if (result != null) { + list.add(result); + } } return list; } @@ -429,11 +429,11 @@ public static List findResults(Map receiver, BiFunction f /** * Sorts all Map members into groups determined by the supplied mapping function. */ - public static Map> groupBy(Map receiver, BiFunction mapper) { - Map> map = new LinkedHashMap<>(); - for (Map.Entry kvPair : receiver.entrySet()) { + public static Map> groupBy(Map receiver, BiFunction mapper) { + Map> map = new LinkedHashMap<>(); + for (Map.Entry kvPair : receiver.entrySet()) { T mapped = mapper.apply(kvPair.getKey(), kvPair.getValue()); - Map results = map.get(mapped); + Map results = map.get(mapped); if (results == null) { // try to preserve some properties of the receiver (see the groovy javadocs) if (receiver instanceof TreeMap) { @@ -532,7 +532,7 @@ public static String[] splitOnToken(String receiver, String token, int limit) { // Loop until we hit the limit or forever if we are passed in less than one (signifying no limit) // If Integer.MIN_VALUE is passed in, it will still continue to loop down to 1 from MAX_VALUE // This edge case should be fine as we are limited by receiver length (Integer.MAX_VALUE) even if we split at every char - for(;limit != 1; limit--) { + for (; limit != 1; limit--) { // Find the next occurrence of token after current pos int idx = receiver.indexOf(token, pos); @@ -573,7 +573,7 @@ public static Object getByPath(List receiver, String path) { /** * Same as {@link #getByPath(List, String)}, but for Map. */ - public static Object getByPath(Map receiver, String path) { + public static Object getByPath(Map receiver, String path) { return getByPathDispatch(receiver, splitPath(path), 0, throwCantFindValue(path)); } @@ -588,7 +588,7 @@ public static Object getByPath(List receiver, String path, Object default /** * Same as {@link #getByPath(List, String, Object)}, but for Map. */ - public static Object getByPath(Map receiver, String path, Object defaultValue) { + public static Object getByPath(Map receiver, String path, Object defaultValue) { return getByPathDispatch(receiver, splitPath(path), 0, () -> defaultValue); } @@ -597,11 +597,11 @@ public static Object getByPath(Map receiver, String path, Object defa private static Object getByPathDispatch(Object obj, String[] elements, int i, Supplier defaultSupplier) { if (i > elements.length - 1) { return obj; - } else if (elements[i].length() == 0 ) { + } else if (elements[i].length() == 0) { String format = "Extra '.' in path [%s] at index [%d]"; throw new IllegalArgumentException(String.format(Locale.ROOT, format, String.join(".", elements), i)); - } else if (obj instanceof Map) { - return getByPathMap((Map) obj, elements, i, defaultSupplier); + } else if (obj instanceof Map) { + return getByPathMap((Map) obj, elements, i, defaultSupplier); } else if (obj instanceof List) { return getByPathList((List) obj, elements, i, defaultSupplier); } @@ -609,7 +609,7 @@ private static Object getByPathDispatch(Object obj, String[] elements, int i, Su } // lookup existing key in map, call back to dispatch. - private static Object getByPathMap(Map map, String[] elements, int i, Supplier defaultSupplier) { + private static Object getByPathMap(Map map, String[] elements, int i, Supplier defaultSupplier) { String element = elements[i]; if (map.containsKey(element)) { return getByPathDispatch(map.get(element), elements, i + 1, defaultSupplier); @@ -617,7 +617,7 @@ private static Object getByPathMap(Map map, String[] elements, int i, return handleMissing(map, elements, i, defaultSupplier); } - // lookup existing index in list, call back to dispatch. Throws IllegalArgumentException with NumberFormatException + // lookup existing index in list, call back to dispatch. Throws IllegalArgumentException with NumberFormatException // if index can't be parsed as an int. private static Object getByPathList(List list, String[] elements, int i, Supplier defaultSupplier) { String element = elements[i]; @@ -647,9 +647,7 @@ private static String[] splitPath(String path) { // A supplier that throws IllegalArgumentException private static Supplier throwCantFindValue(String path) { - return () -> { - throw new IllegalArgumentException(String.format(Locale.ROOT, "Could not find value at path [%s]", path)); - }; + return () -> { throw new IllegalArgumentException(String.format(Locale.ROOT, "Could not find value at path [%s]", path)); }; } // Use defaultSupplier if at last path element, otherwise throw IllegalArgumentException @@ -663,19 +661,16 @@ private static Object handleMissing(Object obj, String[] elements, int i, Suppli } String format = "Non-container [%s] at [%s], index [%d] in path [%s]"; throw new IllegalArgumentException( - String.format(Locale.ROOT, format, obj.getClass().getName(), elements[i], i, String.join(".", elements))); + String.format(Locale.ROOT, format, obj.getClass().getName(), elements[i], i, String.join(".", elements)) + ); } public static String sha1(String source) { - return MessageDigests.toHexString( - MessageDigests.sha1().digest(source.getBytes(StandardCharsets.UTF_8)) - ); + return MessageDigests.toHexString(MessageDigests.sha1().digest(source.getBytes(StandardCharsets.UTF_8))); } public static String sha256(String source) { - return MessageDigests.toHexString( - MessageDigests.sha256().digest(source.getBytes(StandardCharsets.UTF_8)) - ); + return MessageDigests.toHexString(MessageDigests.sha256().digest(source.getBytes(StandardCharsets.UTF_8))); } public static final int UNLIMITED_PATTERN_FACTOR = 0; diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/api/CIDR.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/api/CIDR.java index 6d9fdc3da2731..a9421db5b6fe4 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/api/CIDR.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/api/CIDR.java @@ -8,8 +8,8 @@ package org.elasticsearch.painless.api; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.network.InetAddresses; +import org.elasticsearch.core.Tuple; import java.net.InetAddress; import java.util.Arrays; @@ -56,8 +56,9 @@ private static Tuple getLowerUpper(Tuple c final Integer prefixLength = cidr.v2(); if (prefixLength < 0 || prefixLength > 8 * value.getAddress().length) { - throw new IllegalArgumentException("illegal prefixLength '" + prefixLength + - "'. Must be 0-32 for IPv4 ranges, 0-128 for IPv6 ranges"); + throw new IllegalArgumentException( + "illegal prefixLength '" + prefixLength + "'. Must be 0-32 for IPv4 ranges, 0-128 for IPv6 ranges" + ); } byte[] lower = value.getAddress(); @@ -77,14 +78,13 @@ private static boolean isBetween(byte[] addr, byte[] lower, byte[] upper) { lower = encode(lower); upper = encode(upper); } - return Arrays.compareUnsigned(lower, addr) <= 0 && - Arrays.compareUnsigned(upper, addr) >= 0; + return Arrays.compareUnsigned(lower, addr) <= 0 && Arrays.compareUnsigned(upper, addr) >= 0; } // Borrowed from Lucene to make this consistent IP fields matching for the mix of IPv4 and IPv6 values // Modified signature to avoid extra conversions private static byte[] encode(byte[] address) { - final byte[] IPV4_PREFIX = new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1}; + final byte[] IPV4_PREFIX = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1 }; if (address.length == 4) { byte[] mapped = new byte[16]; System.arraycopy(IPV4_PREFIX, 0, mapped, 0, IPV4_PREFIX.length); diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/api/Json.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/api/Json.java index a1dd1d5896801..bed5b1fda43af 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/api/Json.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/api/Json.java @@ -20,11 +20,12 @@ public class Json { /** * Load a string as the Java version of a JSON type, either List (JSON array), Map (JSON object), Number, Boolean or String */ - public static Object load(String json) throws IOException{ + public static Object load(String json) throws IOException { XContentParser parser = JsonXContent.jsonXContent.createParser( NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, - json); + json + ); switch (parser.nextToken()) { case START_ARRAY: @@ -46,7 +47,7 @@ public static Object load(String json) throws IOException{ * Write a JSON representable type as a string */ public static String dump(Object data) throws IOException { - return dump(data, false); + return dump(data, false); } /** diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/api/LimitedCharSequence.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/api/LimitedCharSequence.java index c4b42a377f4f0..bb589a8dd6ccb 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/api/LimitedCharSequence.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/api/LimitedCharSequence.java @@ -40,11 +40,19 @@ public LimitedCharSequence(CharSequence wrap, Pattern pattern, int limitFactor) } public String details() { - return (pattern != null ? "pattern: [" + pattern.pattern() + "], " : "") + - "limit factor: [" + limitFactor + "], " + - "char limit: [" + counter.charAtLimit + "], " + - "count: [" + counter.count + "], " + - "wrapped: [" + snippet(MAX_STR_LENGTH) + "]"; + return (pattern != null ? "pattern: [" + pattern.pattern() + "], " : "") + + "limit factor: [" + + limitFactor + + "], " + + "char limit: [" + + counter.charAtLimit + + "], " + + "count: [" + + counter.count + + "], " + + "wrapped: [" + + snippet(MAX_STR_LENGTH) + + "]"; } /** @@ -52,15 +60,21 @@ public String details() { */ String snippet(int maxStrLength) { if (maxStrLength < SNIPPET.length() * 6) { - throw new IllegalArgumentException("max str length must be large enough to include three snippets and three context chars, " + - "at least [" + SNIPPET.length() * 6 +"], not [" + maxStrLength + "]"); + throw new IllegalArgumentException( + "max str length must be large enough to include three snippets and three context chars, " + + "at least [" + + SNIPPET.length() * 6 + + "], not [" + + maxStrLength + + "]" + ); } if (wrapped.length() <= maxStrLength) { return wrapped.toString(); } - return wrapped.subSequence(0, maxStrLength - SNIPPET.length()) + "..." ; + return wrapped.subSequence(0, maxStrLength - SNIPPET.length()) + "..."; } @Override @@ -72,9 +86,14 @@ public int length() { public char charAt(int index) { counter.count++; if (counter.hitLimit()) { - throw new CircuitBreakingException("[scripting] Regular expression considered too many characters, " + details() + - ", this limit can be changed by changed by the [" + CompilerSettings.REGEX_LIMIT_FACTOR.getKey() + "] setting", - CircuitBreaker.Durability.TRANSIENT); + throw new CircuitBreakingException( + "[scripting] Regular expression considered too many characters, " + + details() + + ", this limit can be changed by changed by the [" + + CompilerSettings.REGEX_LIMIT_FACTOR.getKey() + + "] setting", + CircuitBreaker.Durability.TRANSIENT + ); } return wrapped.charAt(index); } diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/ir/IRNode.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/ir/IRNode.java index 97864d4c7a254..70417f2a8fd01 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/ir/IRNode.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/ir/IRNode.java @@ -43,7 +43,7 @@ public String toString() { @SuppressWarnings("unchecked") public V attachDecoration(IRDecoration decoration) { - IRDecoration previous = (IRDecoration)decorations.put((Class>)decoration.getClass(), decoration); + IRDecoration previous = (IRDecoration) decorations.put((Class>) decoration.getClass(), decoration); return previous == null ? null : previous.getValue(); } @@ -100,6 +100,7 @@ public Location getLocation() { /* ---- end node data, begin visitor ---- */ public abstract void visit(IRTreeVisitor irTreeVisitor, Scope scope); + public abstract void visitChildren(IRTreeVisitor irTreeVisitor, Scope scope); /* ---- end visitor ---- */ diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/lookup/PainlessCast.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/lookup/PainlessCast.java index dbeecccd0e02f..0a5cf4f997106 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/lookup/PainlessCast.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/lookup/PainlessCast.java @@ -22,7 +22,11 @@ public static PainlessCast originalTypetoTargetType(Class originalType, Class /** Create a cast where the original type will be unboxed, and then the cast will be performed. */ public static PainlessCast unboxOriginalType( - Class originalType, Class targetType, boolean explicitCast, Class unboxOriginalType) { + Class originalType, + Class targetType, + boolean explicitCast, + Class unboxOriginalType + ) { Objects.requireNonNull(originalType); Objects.requireNonNull(targetType); @@ -32,8 +36,7 @@ public static PainlessCast unboxOriginalType( } /** Create a cast where the target type will be unboxed, and then the cast will be performed. */ - public static PainlessCast unboxTargetType( - Class originalType, Class targetType, boolean explicitCast, Class unboxTargetType) { + public static PainlessCast unboxTargetType(Class originalType, Class targetType, boolean explicitCast, Class unboxTargetType) { Objects.requireNonNull(originalType); Objects.requireNonNull(targetType); @@ -43,8 +46,7 @@ public static PainlessCast unboxTargetType( } /** Create a cast where the original type will be boxed, and then the cast will be performed. */ - public static PainlessCast boxOriginalType( - Class originalType, Class targetType, boolean explicitCast, Class boxOriginalType) { + public static PainlessCast boxOriginalType(Class originalType, Class targetType, boolean explicitCast, Class boxOriginalType) { Objects.requireNonNull(originalType); Objects.requireNonNull(targetType); @@ -54,8 +56,7 @@ public static PainlessCast boxOriginalType( } /** Create a cast where the target type will be boxed, and then the cast will be performed. */ - public static PainlessCast boxTargetType( - Class originalType, Class targetType, boolean explicitCast, Class boxTargetType) { + public static PainlessCast boxTargetType(Class originalType, Class targetType, boolean explicitCast, Class boxTargetType) { Objects.requireNonNull(originalType); Objects.requireNonNull(targetType); @@ -81,8 +82,15 @@ public static PainlessCast unboxOriginalTypeToBoxTargetType(boolean explicitCast public final Class boxOriginalType; public final Class boxTargetType; - private PainlessCast(Class originalType, Class targetType, boolean explicitCast, - Class unboxOriginalType, Class unboxTargetType, Class boxOriginalType, Class boxTargetType) { + private PainlessCast( + Class originalType, + Class targetType, + boolean explicitCast, + Class unboxOriginalType, + Class unboxTargetType, + Class boxOriginalType, + Class boxTargetType + ) { this.originalType = originalType; this.targetType = targetType; @@ -103,15 +111,15 @@ public boolean equals(Object object) { return false; } - PainlessCast that = (PainlessCast)object; + PainlessCast that = (PainlessCast) object; - return explicitCast == that.explicitCast && - Objects.equals(originalType, that.originalType) && - Objects.equals(targetType, that.targetType) && - Objects.equals(unboxOriginalType, that.unboxOriginalType) && - Objects.equals(unboxTargetType, that.unboxTargetType) && - Objects.equals(boxOriginalType, that.boxOriginalType) && - Objects.equals(boxTargetType, that.boxTargetType); + return explicitCast == that.explicitCast + && Objects.equals(originalType, that.originalType) + && Objects.equals(targetType, that.targetType) + && Objects.equals(unboxOriginalType, that.unboxOriginalType) + && Objects.equals(unboxTargetType, that.unboxTargetType) + && Objects.equals(boxOriginalType, that.boxOriginalType) + && Objects.equals(boxTargetType, that.boxTargetType); } @Override diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/lookup/PainlessClass.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/lookup/PainlessClass.java index 4a8a7b7f15901..3fc572d8446bc 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/lookup/PainlessClass.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/lookup/PainlessClass.java @@ -26,13 +26,18 @@ public final class PainlessClass { public final Map getterMethodHandles; public final Map setterMethodHandles; - PainlessClass(Map constructors, - Map staticMethods, Map methods, - Map staticFields, Map fields, - PainlessMethod functionalInterfaceMethod, - Map, Object> annotations, - Map runtimeMethods, - Map getterMethodHandles, Map setterMethodHandles) { + PainlessClass( + Map constructors, + Map staticMethods, + Map methods, + Map staticFields, + Map fields, + PainlessMethod functionalInterfaceMethod, + Map, Object> annotations, + Map runtimeMethods, + Map getterMethodHandles, + Map setterMethodHandles + ) { this.constructors = Map.copyOf(constructors); this.staticMethods = Map.copyOf(staticMethods); @@ -57,15 +62,15 @@ public boolean equals(Object object) { return false; } - PainlessClass that = (PainlessClass)object; + PainlessClass that = (PainlessClass) object; - return Objects.equals(constructors, that.constructors) && - Objects.equals(staticMethods, that.staticMethods) && - Objects.equals(methods, that.methods) && - Objects.equals(staticFields, that.staticFields) && - Objects.equals(fields, that.fields) && - Objects.equals(functionalInterfaceMethod, that.functionalInterfaceMethod) && - Objects.equals(annotations, that.annotations); + return Objects.equals(constructors, that.constructors) + && Objects.equals(staticMethods, that.staticMethods) + && Objects.equals(methods, that.methods) + && Objects.equals(staticFields, that.staticFields) + && Objects.equals(fields, that.fields) + && Objects.equals(functionalInterfaceMethod, that.functionalInterfaceMethod) + && Objects.equals(annotations, that.annotations); } @Override diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/lookup/PainlessClassBinding.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/lookup/PainlessClassBinding.java index 309dce2d0128c..185b8f573d2b9 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/lookup/PainlessClassBinding.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/lookup/PainlessClassBinding.java @@ -23,8 +23,13 @@ public class PainlessClassBinding { public final List> typeParameters; public final Map, Object> annotations; - PainlessClassBinding(Constructor javaConstructor, Method javaMethod, Class returnType, List> typeParameters, - Map, Object> annotations) { + PainlessClassBinding( + Constructor javaConstructor, + Method javaMethod, + Class returnType, + List> typeParameters, + Map, Object> annotations + ) { this.javaConstructor = javaConstructor; this.javaMethod = javaMethod; @@ -43,12 +48,12 @@ public boolean equals(Object object) { return false; } - PainlessClassBinding that = (PainlessClassBinding)object; + PainlessClassBinding that = (PainlessClassBinding) object; - return Objects.equals(javaConstructor, that.javaConstructor) && - Objects.equals(javaMethod, that.javaMethod) && - Objects.equals(returnType, that.returnType) && - Objects.equals(typeParameters, that.typeParameters); + return Objects.equals(javaConstructor, that.javaConstructor) + && Objects.equals(javaMethod, that.javaMethod) + && Objects.equals(returnType, that.returnType) + && Objects.equals(typeParameters, that.typeParameters); } @Override diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/lookup/PainlessClassBuilder.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/lookup/PainlessClassBuilder.java index 6f614ce12dca2..65f1293d5772b 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/lookup/PainlessClassBuilder.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/lookup/PainlessClassBuilder.java @@ -42,8 +42,18 @@ final class PainlessClassBuilder { } PainlessClass build() { - return new PainlessClass(constructors, staticMethods, methods, staticFields, fields, functionalInterfaceMethod, annotations, - runtimeMethods, getterMethodHandles, setterMethodHandles); + return new PainlessClass( + constructors, + staticMethods, + methods, + staticFields, + fields, + functionalInterfaceMethod, + annotations, + runtimeMethods, + getterMethodHandles, + setterMethodHandles + ); } @Override @@ -56,15 +66,15 @@ public boolean equals(Object object) { return false; } - PainlessClassBuilder that = (PainlessClassBuilder)object; + PainlessClassBuilder that = (PainlessClassBuilder) object; - return Objects.equals(constructors, that.constructors) && - Objects.equals(staticMethods, that.staticMethods) && - Objects.equals(methods, that.methods) && - Objects.equals(staticFields, that.staticFields) && - Objects.equals(fields, that.fields) && - Objects.equals(functionalInterfaceMethod, that.functionalInterfaceMethod) && - Objects.equals(annotations, that.annotations); + return Objects.equals(constructors, that.constructors) + && Objects.equals(staticMethods, that.staticMethods) + && Objects.equals(methods, that.methods) + && Objects.equals(staticFields, that.staticFields) + && Objects.equals(fields, that.fields) + && Objects.equals(functionalInterfaceMethod, that.functionalInterfaceMethod) + && Objects.equals(annotations, that.annotations); } @Override diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/lookup/PainlessConstructor.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/lookup/PainlessConstructor.java index ea1d114934165..2e272d9c124a8 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/lookup/PainlessConstructor.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/lookup/PainlessConstructor.java @@ -23,8 +23,13 @@ public class PainlessConstructor { public final MethodType methodType; public final Map, Object> annotations; - PainlessConstructor(Constructor javaConstructor, List> typeParameters, MethodHandle methodHandle, MethodType methodType, - Map, Object> annotations) { + PainlessConstructor( + Constructor javaConstructor, + List> typeParameters, + MethodHandle methodHandle, + MethodType methodType, + Map, Object> annotations + ) { this.javaConstructor = javaConstructor; this.typeParameters = typeParameters; this.methodHandle = methodHandle; @@ -42,12 +47,12 @@ public boolean equals(Object object) { return false; } - PainlessConstructor that = (PainlessConstructor)object; + PainlessConstructor that = (PainlessConstructor) object; - return Objects.equals(javaConstructor, that.javaConstructor) && - Objects.equals(typeParameters, that.typeParameters) && - Objects.equals(methodType, that.methodType) && - Objects.equals(annotations, that.annotations); + return Objects.equals(javaConstructor, that.javaConstructor) + && Objects.equals(typeParameters, that.typeParameters) + && Objects.equals(methodType, that.methodType) + && Objects.equals(annotations, that.annotations); } @Override diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/lookup/PainlessField.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/lookup/PainlessField.java index e0bfabbb4b066..248f8fdd4ac07 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/lookup/PainlessField.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/lookup/PainlessField.java @@ -22,8 +22,13 @@ public final class PainlessField { public final MethodHandle getterMethodHandle; public final MethodHandle setterMethodHandle; - PainlessField(Field javaField, Class typeParameter, Map, Object> annotations, - MethodHandle getterMethodHandle, MethodHandle setterMethodHandle) { + PainlessField( + Field javaField, + Class typeParameter, + Map, Object> annotations, + MethodHandle getterMethodHandle, + MethodHandle setterMethodHandle + ) { this.javaField = javaField; this.typeParameter = typeParameter; @@ -43,11 +48,11 @@ public boolean equals(Object object) { return false; } - PainlessField that = (PainlessField)object; + PainlessField that = (PainlessField) object; - return Objects.equals(javaField, that.javaField) && - Objects.equals(typeParameter, that.typeParameter) && - Objects.equals(annotations, that.annotations); + return Objects.equals(javaField, that.javaField) + && Objects.equals(typeParameter, that.typeParameter) + && Objects.equals(annotations, that.annotations); } @Override diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/lookup/PainlessInstanceBinding.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/lookup/PainlessInstanceBinding.java index 7e12d8e91f0a1..da1305ebb3547 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/lookup/PainlessInstanceBinding.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/lookup/PainlessInstanceBinding.java @@ -47,13 +47,13 @@ public boolean equals(Object object) { return false; } - PainlessInstanceBinding that = (PainlessInstanceBinding)object; + PainlessInstanceBinding that = (PainlessInstanceBinding) object; - return targetInstance == that.targetInstance && - Objects.equals(javaMethod, that.javaMethod) && - Objects.equals(returnType, that.returnType) && - Objects.equals(typeParameters, that.typeParameters) && - Objects.equals(annotations, that.annotations); + return targetInstance == that.targetInstance + && Objects.equals(javaMethod, that.javaMethod) + && Objects.equals(returnType, that.returnType) + && Objects.equals(typeParameters, that.typeParameters) + && Objects.equals(annotations, that.annotations); } @Override diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/lookup/PainlessLookup.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/lookup/PainlessLookup.java index bb166eabd662b..50e743d059c77 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/lookup/PainlessLookup.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/lookup/PainlessLookup.java @@ -36,13 +36,14 @@ public final class PainlessLookup { private final Map painlessMethodKeysToPainlessInstanceBindings; PainlessLookup( - Map> javaClassNamesToClasses, - Map> canonicalClassNamesToClasses, - Map, PainlessClass> classesToPainlessClasses, - Map, Set>> classesToDirectSubClasses, - Map painlessMethodKeysToImportedPainlessMethods, - Map painlessMethodKeysToPainlessClassBindings, - Map painlessMethodKeysToPainlessInstanceBindings) { + Map> javaClassNamesToClasses, + Map> canonicalClassNamesToClasses, + Map, PainlessClass> classesToPainlessClasses, + Map, Set>> classesToDirectSubClasses, + Map painlessMethodKeysToImportedPainlessMethods, + Map painlessMethodKeysToPainlessClassBindings, + Map painlessMethodKeysToPainlessInstanceBindings + ) { Objects.requireNonNull(javaClassNamesToClasses); Objects.requireNonNull(canonicalClassNamesToClasses); @@ -163,9 +164,9 @@ public PainlessMethod lookupPainlessMethod(Class targetClass, boolean isStati } String painlessMethodKey = buildPainlessMethodKey(methodName, methodArity); - Function objectLookup = isStatic ? - targetPainlessClass -> targetPainlessClass.staticMethods.get(painlessMethodKey) : - targetPainlessClass -> targetPainlessClass.methods.get(painlessMethodKey); + Function objectLookup = isStatic + ? targetPainlessClass -> targetPainlessClass.staticMethods.get(painlessMethodKey) + : targetPainlessClass -> targetPainlessClass.methods.get(painlessMethodKey); return lookupPainlessObject(targetClass, objectLookup); } @@ -246,9 +247,9 @@ public PainlessField lookupPainlessField(Class targetClass, boolean isStatic, } String painlessFieldKey = buildPainlessFieldKey(fieldName); - Function objectLookup = isStatic ? - targetPainlessClass -> targetPainlessClass.staticFields.get(painlessFieldKey) : - targetPainlessClass -> targetPainlessClass.fields.get(painlessFieldKey); + Function objectLookup = isStatic + ? targetPainlessClass -> targetPainlessClass.staticFields.get(painlessFieldKey) + : targetPainlessClass -> targetPainlessClass.fields.get(painlessFieldKey); return lookupPainlessObject(targetClass, objectLookup); } @@ -292,8 +293,9 @@ public PainlessMethod lookupRuntimePainlessMethod(Class originalTargetClass, Objects.requireNonNull(methodName); String painlessMethodKey = buildPainlessMethodKey(methodName, methodArity); - Function objectLookup = - targetPainlessClass -> targetPainlessClass.runtimeMethods.get(painlessMethodKey); + Function objectLookup = targetPainlessClass -> targetPainlessClass.runtimeMethods.get( + painlessMethodKey + ); return lookupPainlessObject(originalTargetClass, objectLookup); } diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/lookup/PainlessLookupBuilder.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/lookup/PainlessLookupBuilder.java index 02cac53146262..19cba48fceac1 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/lookup/PainlessLookupBuilder.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/lookup/PainlessLookupBuilder.java @@ -89,20 +89,20 @@ Class defineBridge(String name, byte[] bytes) { private static final CodeSource CODESOURCE; - private static final Map painlessConstructorCache = new HashMap<>(); - private static final Map painlessMethodCache = new HashMap<>(); - private static final Map painlessFieldCache = new HashMap<>(); - private static final Map painlessClassBindingCache = new HashMap<>(); + private static final Map painlessConstructorCache = new HashMap<>(); + private static final Map painlessMethodCache = new HashMap<>(); + private static final Map painlessFieldCache = new HashMap<>(); + private static final Map painlessClassBindingCache = new HashMap<>(); private static final Map painlessInstanceBindingCache = new HashMap<>(); - private static final Map painlessBridgeCache = new HashMap<>(); + private static final Map painlessBridgeCache = new HashMap<>(); - private static final Pattern CLASS_NAME_PATTERN = Pattern.compile("^[_a-zA-Z][._a-zA-Z0-9]*$"); + private static final Pattern CLASS_NAME_PATTERN = Pattern.compile("^[_a-zA-Z][._a-zA-Z0-9]*$"); private static final Pattern METHOD_NAME_PATTERN = Pattern.compile("^[_a-zA-Z][_a-zA-Z0-9]*$"); - private static final Pattern FIELD_NAME_PATTERN = Pattern.compile("^[_a-zA-Z][_a-zA-Z0-9]*$"); + private static final Pattern FIELD_NAME_PATTERN = Pattern.compile("^[_a-zA-Z][_a-zA-Z0-9]*$"); static { try { - CODESOURCE = new CodeSource(new URL("file:" + BootstrapInfo.UNTRUSTED_CODEBASE), (Certificate[])null); + CODESOURCE = new CodeSource(new URL("file:" + BootstrapInfo.UNTRUSTED_CODEBASE), (Certificate[]) null); } catch (MalformedURLException mue) { throw new RuntimeException(mue); } @@ -117,8 +117,10 @@ public static PainlessLookup buildFromWhitelists(List whitelists) { for (WhitelistClass whitelistClass : whitelist.whitelistClasses) { origin = whitelistClass.origin; painlessLookupBuilder.addPainlessClass( - whitelist.classLoader, whitelistClass.javaClassName, - whitelistClass.painlessAnnotations); + whitelist.classLoader, + whitelistClass.javaClassName, + whitelistClass.painlessAnnotations + ); } } @@ -129,49 +131,70 @@ public static PainlessLookup buildFromWhitelists(List whitelists) { for (WhitelistConstructor whitelistConstructor : whitelistClass.whitelistConstructors) { origin = whitelistConstructor.origin; painlessLookupBuilder.addPainlessConstructor( - targetCanonicalClassName, whitelistConstructor.canonicalTypeNameParameters, - whitelistConstructor.painlessAnnotations); + targetCanonicalClassName, + whitelistConstructor.canonicalTypeNameParameters, + whitelistConstructor.painlessAnnotations + ); } for (WhitelistMethod whitelistMethod : whitelistClass.whitelistMethods) { origin = whitelistMethod.origin; painlessLookupBuilder.addPainlessMethod( - whitelist.classLoader, targetCanonicalClassName, whitelistMethod.augmentedCanonicalClassName, - whitelistMethod.methodName, whitelistMethod.returnCanonicalTypeName, - whitelistMethod.canonicalTypeNameParameters, whitelistMethod.painlessAnnotations); + whitelist.classLoader, + targetCanonicalClassName, + whitelistMethod.augmentedCanonicalClassName, + whitelistMethod.methodName, + whitelistMethod.returnCanonicalTypeName, + whitelistMethod.canonicalTypeNameParameters, + whitelistMethod.painlessAnnotations + ); } for (WhitelistField whitelistField : whitelistClass.whitelistFields) { origin = whitelistField.origin; painlessLookupBuilder.addPainlessField( - whitelist.classLoader, targetCanonicalClassName, - whitelistField.fieldName, whitelistField.canonicalTypeNameParameter, whitelistField.painlessAnnotations); + whitelist.classLoader, + targetCanonicalClassName, + whitelistField.fieldName, + whitelistField.canonicalTypeNameParameter, + whitelistField.painlessAnnotations + ); } } for (WhitelistMethod whitelistStatic : whitelist.whitelistImportedMethods) { origin = whitelistStatic.origin; painlessLookupBuilder.addImportedPainlessMethod( - whitelist.classLoader, whitelistStatic.augmentedCanonicalClassName, - whitelistStatic.methodName, whitelistStatic.returnCanonicalTypeName, - whitelistStatic.canonicalTypeNameParameters, - whitelistStatic.painlessAnnotations); + whitelist.classLoader, + whitelistStatic.augmentedCanonicalClassName, + whitelistStatic.methodName, + whitelistStatic.returnCanonicalTypeName, + whitelistStatic.canonicalTypeNameParameters, + whitelistStatic.painlessAnnotations + ); } for (WhitelistClassBinding whitelistClassBinding : whitelist.whitelistClassBindings) { origin = whitelistClassBinding.origin; painlessLookupBuilder.addPainlessClassBinding( - whitelist.classLoader, whitelistClassBinding.targetJavaClassName, whitelistClassBinding.methodName, - whitelistClassBinding.returnCanonicalTypeName, whitelistClassBinding.canonicalTypeNameParameters, - whitelistClassBinding.painlessAnnotations); + whitelist.classLoader, + whitelistClassBinding.targetJavaClassName, + whitelistClassBinding.methodName, + whitelistClassBinding.returnCanonicalTypeName, + whitelistClassBinding.canonicalTypeNameParameters, + whitelistClassBinding.painlessAnnotations + ); } for (WhitelistInstanceBinding whitelistInstanceBinding : whitelist.whitelistInstanceBindings) { origin = whitelistInstanceBinding.origin; painlessLookupBuilder.addPainlessInstanceBinding( - whitelistInstanceBinding.targetInstance, whitelistInstanceBinding.methodName, - whitelistInstanceBinding.returnCanonicalTypeName, whitelistInstanceBinding.canonicalTypeNameParameters, - whitelistInstanceBinding.painlessAnnotations); + whitelistInstanceBinding.targetInstance, + whitelistInstanceBinding.methodName, + whitelistInstanceBinding.returnCanonicalTypeName, + whitelistInstanceBinding.canonicalTypeNameParameters, + whitelistInstanceBinding.painlessAnnotations + ); } } } catch (Exception exception) { @@ -243,15 +266,15 @@ public void addPainlessClass(ClassLoader classLoader, String javaClassName, Map< Class clazz; - if ("void".equals(javaClassName)) clazz = void.class; + if ("void".equals(javaClassName)) clazz = void.class; else if ("boolean".equals(javaClassName)) clazz = boolean.class; - else if ("byte".equals(javaClassName)) clazz = byte.class; - else if ("short".equals(javaClassName)) clazz = short.class; - else if ("char".equals(javaClassName)) clazz = char.class; - else if ("int".equals(javaClassName)) clazz = int.class; - else if ("long".equals(javaClassName)) clazz = long.class; - else if ("float".equals(javaClassName)) clazz = float.class; - else if ("double".equals(javaClassName)) clazz = double.class; + else if ("byte".equals(javaClassName)) clazz = byte.class; + else if ("short".equals(javaClassName)) clazz = short.class; + else if ("char".equals(javaClassName)) clazz = char.class; + else if ("int".equals(javaClassName)) clazz = int.class; + else if ("long".equals(javaClassName)) clazz = long.class; + else if ("float".equals(javaClassName)) clazz = float.class; + else if ("double".equals(javaClassName)) clazz = double.class; else { clazz = loadClass(classLoader, javaClassName, () -> "class [" + javaClassName + "] not found"); } @@ -282,15 +305,23 @@ public void addPainlessClass(Class clazz, Map, Object> annotations) if (existingClass == null) { javaClassNamesToClasses.put(clazz.getName().intern(), clazz); } else if (existingClass != clazz) { - throw new IllegalArgumentException("class [" + canonicalClassName + "] " + - "cannot represent multiple java classes with the same name from different class loaders"); + throw new IllegalArgumentException( + "class [" + + canonicalClassName + + "] " + + "cannot represent multiple java classes with the same name from different class loaders" + ); } existingClass = canonicalClassNamesToClasses.get(canonicalClassName); if (existingClass != null && existingClass != clazz) { - throw new IllegalArgumentException("class [" + canonicalClassName + "] " + - "cannot represent multiple java classes with the same name from different class loaders"); + throw new IllegalArgumentException( + "class [" + + canonicalClassName + + "] " + + "cannot represent multiple java classes with the same name from different class loaders" + ); } PainlessClassBuilder existingPainlessClassBuilder = classesToPainlessClassBuilders.get(clazz); @@ -317,31 +348,49 @@ public void addPainlessClass(Class clazz, Map, Object> annotations) if (importedClass == null) { if (importClassName) { if (existingPainlessClassBuilder != null) { - throw new IllegalArgumentException( - "inconsistent no_import parameter found for class [" + canonicalClassName + "]"); + throw new IllegalArgumentException("inconsistent no_import parameter found for class [" + canonicalClassName + "]"); } canonicalClassNamesToClasses.put(importedCanonicalClassName.intern(), clazz); } } else if (importedClass != clazz) { - throw new IllegalArgumentException("imported class [" + importedCanonicalClassName + "] cannot represent multiple " + - "classes [" + canonicalClassName + "] and [" + typeToCanonicalTypeName(importedClass) + "]"); + throw new IllegalArgumentException( + "imported class [" + + importedCanonicalClassName + + "] cannot represent multiple " + + "classes [" + + canonicalClassName + + "] and [" + + typeToCanonicalTypeName(importedClass) + + "]" + ); } else if (importClassName == false) { throw new IllegalArgumentException("inconsistent no_import parameter found for class [" + canonicalClassName + "]"); } } } - public void addPainlessConstructor(String targetCanonicalClassName, List canonicalTypeNameParameters, - Map, Object> annotations) { + public void addPainlessConstructor( + String targetCanonicalClassName, + List canonicalTypeNameParameters, + Map, Object> annotations + ) { Objects.requireNonNull(targetCanonicalClassName); Objects.requireNonNull(canonicalTypeNameParameters); Class targetClass = canonicalClassNamesToClasses.get(targetCanonicalClassName); if (targetClass == null) { - throw new IllegalArgumentException("target class [" + targetCanonicalClassName + "] not found" + - "for constructor [[" + targetCanonicalClassName + "], " + canonicalTypeNameParameters + "]"); + throw new IllegalArgumentException( + "target class [" + + targetCanonicalClassName + + "] not found" + + "for constructor [[" + + targetCanonicalClassName + + "], " + + canonicalTypeNameParameters + + "]" + ); } List> typeParameters = new ArrayList<>(canonicalTypeNameParameters.size()); @@ -350,8 +399,16 @@ public void addPainlessConstructor(String targetCanonicalClassName, List Class typeParameter = canonicalTypeNameToType(canonicalTypeNameParameter); if (typeParameter == null) { - throw new IllegalArgumentException("type parameter [" + canonicalTypeNameParameter + "] not found " + - "for constructor [[" + targetCanonicalClassName + "], " + canonicalTypeNameParameters + "]"); + throw new IllegalArgumentException( + "type parameter [" + + canonicalTypeNameParameter + + "] not found " + + "for constructor [[" + + targetCanonicalClassName + + "], " + + canonicalTypeNameParameters + + "]" + ); } typeParameters.add(typeParameter); @@ -372,8 +429,16 @@ public void addPainlessConstructor(Class targetClass, List> typePara PainlessClassBuilder painlessClassBuilder = classesToPainlessClassBuilders.get(targetClass); if (painlessClassBuilder == null) { - throw new IllegalArgumentException("target class [" + targetCanonicalClassName + "] not found" + - "for constructor [[" + targetCanonicalClassName + "], " + typesToCanonicalTypeNames(typeParameters) + "]"); + throw new IllegalArgumentException( + "target class [" + + targetCanonicalClassName + + "] not found" + + "for constructor [[" + + targetCanonicalClassName + + "], " + + typesToCanonicalTypeNames(typeParameters) + + "]" + ); } int typeParametersSize = typeParameters.size(); @@ -381,8 +446,16 @@ public void addPainlessConstructor(Class targetClass, List> typePara for (Class typeParameter : typeParameters) { if (isValidType(typeParameter) == false) { - throw new IllegalArgumentException("type parameter [" + typeToCanonicalTypeName(typeParameter) + "] not found " + - "for constructor [[" + targetCanonicalClassName + "], " + typesToCanonicalTypeNames(typeParameters) + "]"); + throw new IllegalArgumentException( + "type parameter [" + + typeToCanonicalTypeName(typeParameter) + + "] not found " + + "for constructor [[" + + targetCanonicalClassName + + "], " + + typesToCanonicalTypeNames(typeParameters) + + "]" + ); } javaTypeParameters.add(typeToJavaType(typeParameter)); @@ -393,8 +466,15 @@ public void addPainlessConstructor(Class targetClass, List> typePara try { javaConstructor = targetClass.getConstructor(javaTypeParameters.toArray(new Class[typeParametersSize])); } catch (NoSuchMethodException nsme) { - throw new IllegalArgumentException("reflection object not found for constructor " + - "[[" + targetCanonicalClassName + "], " + typesToCanonicalTypeNames(typeParameters) + "]", nsme); + throw new IllegalArgumentException( + "reflection object not found for constructor " + + "[[" + + targetCanonicalClassName + + "], " + + typesToCanonicalTypeNames(typeParameters) + + "]", + nsme + ); } MethodHandle methodHandle; @@ -402,8 +482,15 @@ public void addPainlessConstructor(Class targetClass, List> typePara try { methodHandle = MethodHandles.publicLookup().in(targetClass).unreflectConstructor(javaConstructor); } catch (IllegalAccessException iae) { - throw new IllegalArgumentException("method handle not found for constructor " + - "[[" + targetCanonicalClassName + "], " + typesToCanonicalTypeNames(typeParameters) + "]", iae); + throw new IllegalArgumentException( + "method handle not found for constructor " + + "[[" + + targetCanonicalClassName + + "], " + + typesToCanonicalTypeNames(typeParameters) + + "]", + iae + ); } if (annotations.containsKey(CompileTimeOnlyAnnotation.class)) { @@ -414,22 +501,43 @@ public void addPainlessConstructor(Class targetClass, List> typePara String painlessConstructorKey = buildPainlessConstructorKey(typeParametersSize); PainlessConstructor existingPainlessConstructor = painlessClassBuilder.constructors.get(painlessConstructorKey); - PainlessConstructor newPainlessConstructor = new PainlessConstructor(javaConstructor, typeParameters, methodHandle, methodType, - annotations); + PainlessConstructor newPainlessConstructor = new PainlessConstructor( + javaConstructor, + typeParameters, + methodHandle, + methodType, + annotations + ); if (existingPainlessConstructor == null) { newPainlessConstructor = painlessConstructorCache.computeIfAbsent(newPainlessConstructor, key -> key); painlessClassBuilder.constructors.put(painlessConstructorKey.intern(), newPainlessConstructor); - } else if (newPainlessConstructor.equals(existingPainlessConstructor) == false){ - throw new IllegalArgumentException("cannot add constructors with the same arity but are not equivalent for constructors " + - "[[" + targetCanonicalClassName + "], " + typesToCanonicalTypeNames(typeParameters) + "] and " + - "[[" + targetCanonicalClassName + "], " + typesToCanonicalTypeNames(existingPainlessConstructor.typeParameters) + "]"); + } else if (newPainlessConstructor.equals(existingPainlessConstructor) == false) { + throw new IllegalArgumentException( + "cannot add constructors with the same arity but are not equivalent for constructors " + + "[[" + + targetCanonicalClassName + + "], " + + typesToCanonicalTypeNames(typeParameters) + + "] and " + + "[[" + + targetCanonicalClassName + + "], " + + typesToCanonicalTypeNames(existingPainlessConstructor.typeParameters) + + "]" + ); } } - public void addPainlessMethod(ClassLoader classLoader, String targetCanonicalClassName, String augmentedCanonicalClassName, - String methodName, String returnCanonicalTypeName, List canonicalTypeNameParameters, - Map, Object> annotations) { + public void addPainlessMethod( + ClassLoader classLoader, + String targetCanonicalClassName, + String augmentedCanonicalClassName, + String methodName, + String returnCanonicalTypeName, + List canonicalTypeNameParameters, + Map, Object> annotations + ) { Objects.requireNonNull(classLoader); Objects.requireNonNull(targetCanonicalClassName); @@ -441,16 +549,37 @@ public void addPainlessMethod(ClassLoader classLoader, String targetCanonicalCla Class targetClass = canonicalClassNamesToClasses.get(targetCanonicalClassName); if (targetClass == null) { - throw new IllegalArgumentException("target class [" + targetCanonicalClassName + "] not found for method " + - "[[" + targetCanonicalClassName + "], [" + methodName + "], " + canonicalTypeNameParameters + "]"); + throw new IllegalArgumentException( + "target class [" + + targetCanonicalClassName + + "] not found for method " + + "[[" + + targetCanonicalClassName + + "], [" + + methodName + + "], " + + canonicalTypeNameParameters + + "]" + ); } Class augmentedClass = null; if (augmentedCanonicalClassName != null) { - augmentedClass = loadClass(classLoader, augmentedCanonicalClassName, - () -> "augmented class [" + augmentedCanonicalClassName + "] not found for method " + - "[[" + targetCanonicalClassName + "], [" + methodName + "], " + canonicalTypeNameParameters + "]"); + augmentedClass = loadClass( + classLoader, + augmentedCanonicalClassName, + () -> "augmented class [" + + augmentedCanonicalClassName + + "] not found for method " + + "[[" + + targetCanonicalClassName + + "], [" + + methodName + + "], " + + canonicalTypeNameParameters + + "]" + ); } List> typeParameters = new ArrayList<>(canonicalTypeNameParameters.size()); @@ -459,8 +588,18 @@ public void addPainlessMethod(ClassLoader classLoader, String targetCanonicalCla Class typeParameter = canonicalTypeNameToType(canonicalTypeNameParameter); if (typeParameter == null) { - throw new IllegalArgumentException("type parameter [" + canonicalTypeNameParameter + "] not found for method " + - "[[" + targetCanonicalClassName + "], [" + methodName + "], " + canonicalTypeNameParameters + "]"); + throw new IllegalArgumentException( + "type parameter [" + + canonicalTypeNameParameter + + "] not found for method " + + "[[" + + targetCanonicalClassName + + "], [" + + methodName + + "], " + + canonicalTypeNameParameters + + "]" + ); } typeParameters.add(typeParameter); @@ -469,15 +608,31 @@ public void addPainlessMethod(ClassLoader classLoader, String targetCanonicalCla Class returnType = canonicalTypeNameToType(returnCanonicalTypeName); if (returnType == null) { - throw new IllegalArgumentException("return type [" + returnCanonicalTypeName + "] not found for method " + - "[[" + targetCanonicalClassName + "], [" + methodName + "], " + canonicalTypeNameParameters + "]"); + throw new IllegalArgumentException( + "return type [" + + returnCanonicalTypeName + + "] not found for method " + + "[[" + + targetCanonicalClassName + + "], [" + + methodName + + "], " + + canonicalTypeNameParameters + + "]" + ); } addPainlessMethod(targetClass, augmentedClass, methodName, returnType, typeParameters, annotations); } - public void addPainlessMethod(Class targetClass, Class augmentedClass, - String methodName, Class returnType, List> typeParameters, Map, Object> annotations) { + public void addPainlessMethod( + Class targetClass, + Class augmentedClass, + String methodName, + Class returnType, + List> typeParameters, + Map, Object> annotations + ) { Objects.requireNonNull(targetClass); Objects.requireNonNull(methodName); @@ -493,14 +648,25 @@ public void addPainlessMethod(Class targetClass, Class augmentedClass, if (METHOD_NAME_PATTERN.matcher(methodName).matches() == false) { throw new IllegalArgumentException( - "invalid method name [" + methodName + "] for target class [" + targetCanonicalClassName + "]."); + "invalid method name [" + methodName + "] for target class [" + targetCanonicalClassName + "]." + ); } PainlessClassBuilder painlessClassBuilder = classesToPainlessClassBuilders.get(targetClass); if (painlessClassBuilder == null) { - throw new IllegalArgumentException("target class [" + targetCanonicalClassName + "] not found for method " + - "[[" + targetCanonicalClassName + "], [" + methodName + "], " + typesToCanonicalTypeNames(typeParameters) + "]"); + throw new IllegalArgumentException( + "target class [" + + targetCanonicalClassName + + "] not found for method " + + "[[" + + targetCanonicalClassName + + "], [" + + methodName + + "], " + + typesToCanonicalTypeNames(typeParameters) + + "]" + ); } int typeParametersSize = typeParameters.size(); @@ -513,17 +679,36 @@ public void addPainlessMethod(Class targetClass, Class augmentedClass, for (Class typeParameter : typeParameters) { if (isValidType(typeParameter) == false) { - throw new IllegalArgumentException("type parameter [" + typeToCanonicalTypeName(typeParameter) + "] " + - "not found for method [[" + targetCanonicalClassName + "], [" + methodName + "], " + - typesToCanonicalTypeNames(typeParameters) + "]"); + throw new IllegalArgumentException( + "type parameter [" + + typeToCanonicalTypeName(typeParameter) + + "] " + + "not found for method [[" + + targetCanonicalClassName + + "], [" + + methodName + + "], " + + typesToCanonicalTypeNames(typeParameters) + + "]" + ); } javaTypeParameters.add(typeToJavaType(typeParameter)); } if (isValidType(returnType) == false) { - throw new IllegalArgumentException("return type [" + typeToCanonicalTypeName(returnType) + "] not found for method " + - "[[" + targetCanonicalClassName + "], [" + methodName + "], " + typesToCanonicalTypeNames(typeParameters) + "]"); + throw new IllegalArgumentException( + "return type [" + + typeToCanonicalTypeName(returnType) + + "] not found for method " + + "[[" + + targetCanonicalClassName + + "], [" + + methodName + + "], " + + typesToCanonicalTypeNames(typeParameters) + + "]" + ); } Method javaMethod; @@ -532,28 +717,57 @@ public void addPainlessMethod(Class targetClass, Class augmentedClass, try { javaMethod = targetClass.getMethod(methodName, javaTypeParameters.toArray(new Class[typeParametersSize])); } catch (NoSuchMethodException nsme) { - throw new IllegalArgumentException("reflection object not found for method [[" + targetCanonicalClassName + "], " + - "[" + methodName + "], " + typesToCanonicalTypeNames(typeParameters) + "]", nsme); + throw new IllegalArgumentException( + "reflection object not found for method [[" + + targetCanonicalClassName + + "], " + + "[" + + methodName + + "], " + + typesToCanonicalTypeNames(typeParameters) + + "]", + nsme + ); } } else { try { javaMethod = augmentedClass.getMethod(methodName, javaTypeParameters.toArray(new Class[typeParametersSize])); if (Modifier.isStatic(javaMethod.getModifiers()) == false) { - throw new IllegalArgumentException("method [[" + targetCanonicalClassName + "], [" + methodName + "], " + - typesToCanonicalTypeNames(typeParameters) + "] with augmented class " + - "[" + typeToCanonicalTypeName(augmentedClass) + "] must be static"); + throw new IllegalArgumentException( + "method [[" + + targetCanonicalClassName + + "], [" + + methodName + + "], " + + typesToCanonicalTypeNames(typeParameters) + + "] with augmented class " + + "[" + + typeToCanonicalTypeName(augmentedClass) + + "] must be static" + ); } } catch (NoSuchMethodException nsme) { - throw new IllegalArgumentException("reflection object not found for method " + - "[[" + targetCanonicalClassName + "], [" + methodName + "], " + typesToCanonicalTypeNames(typeParameters) + "] " + - "with augmented class [" + typeToCanonicalTypeName(augmentedClass) + "]", nsme); + throw new IllegalArgumentException( + "reflection object not found for method " + + "[[" + + targetCanonicalClassName + + "], [" + + methodName + + "], " + + typesToCanonicalTypeNames(typeParameters) + + "] " + + "with augmented class [" + + typeToCanonicalTypeName(augmentedClass) + + "]", + nsme + ); } } // injections alter the type parameters required for the user to call this method, since some are injected by compiler if (annotations.containsKey(InjectConstantAnnotation.class)) { - int numInjections = ((InjectConstantAnnotation)annotations.get(InjectConstantAnnotation.class)).injects.size(); + int numInjections = ((InjectConstantAnnotation) annotations.get(InjectConstantAnnotation.class)).injects.size(); if (numInjections > 0) { typeParameters.subList(0, numInjections).clear(); @@ -563,10 +777,21 @@ public void addPainlessMethod(Class targetClass, Class augmentedClass, } if (javaMethod.getReturnType() != typeToJavaType(returnType)) { - throw new IllegalArgumentException("return type [" + typeToCanonicalTypeName(javaMethod.getReturnType()) + "] " + - "does not match the specified returned type [" + typeToCanonicalTypeName(returnType) + "] " + - "for method [[" + targetClass.getCanonicalName() + "], [" + methodName + "], " + - typesToCanonicalTypeNames(typeParameters) + "]"); + throw new IllegalArgumentException( + "return type [" + + typeToCanonicalTypeName(javaMethod.getReturnType()) + + "] " + + "does not match the specified returned type [" + + typeToCanonicalTypeName(returnType) + + "] " + + "for method [[" + + targetClass.getCanonicalName() + + "], [" + + methodName + + "], " + + typesToCanonicalTypeNames(typeParameters) + + "]" + ); } MethodHandle methodHandle; @@ -575,18 +800,36 @@ public void addPainlessMethod(Class targetClass, Class augmentedClass, try { methodHandle = MethodHandles.publicLookup().in(targetClass).unreflect(javaMethod); } catch (IllegalAccessException iae) { - throw new IllegalArgumentException("method handle not found for method " + - "[[" + targetClass.getCanonicalName() + "], [" + methodName + "], " + - typesToCanonicalTypeNames(typeParameters) + "]", iae); + throw new IllegalArgumentException( + "method handle not found for method " + + "[[" + + targetClass.getCanonicalName() + + "], [" + + methodName + + "], " + + typesToCanonicalTypeNames(typeParameters) + + "]", + iae + ); } } else { try { methodHandle = MethodHandles.publicLookup().in(augmentedClass).unreflect(javaMethod); } catch (IllegalAccessException iae) { - throw new IllegalArgumentException("method handle not found for method " + - "[[" + targetClass.getCanonicalName() + "], [" + methodName + "], " + - typesToCanonicalTypeNames(typeParameters) + "]" + - "with augmented class [" + typeToCanonicalTypeName(augmentedClass) + "]", iae); + throw new IllegalArgumentException( + "method handle not found for method " + + "[[" + + targetClass.getCanonicalName() + + "], [" + + methodName + + "], " + + typesToCanonicalTypeNames(typeParameters) + + "]" + + "with augmented class [" + + typeToCanonicalTypeName(augmentedClass) + + "]", + iae + ); } } @@ -597,11 +840,18 @@ public void addPainlessMethod(Class targetClass, Class augmentedClass, MethodType methodType = methodHandle.type(); boolean isStatic = augmentedClass == null && Modifier.isStatic(javaMethod.getModifiers()); String painlessMethodKey = buildPainlessMethodKey(methodName, typeParametersSize); - PainlessMethod existingPainlessMethod = isStatic ? - painlessClassBuilder.staticMethods.get(painlessMethodKey) : - painlessClassBuilder.methods.get(painlessMethodKey); - PainlessMethod newPainlessMethod = - new PainlessMethod(javaMethod, targetClass, returnType, typeParameters, methodHandle, methodType, annotations); + PainlessMethod existingPainlessMethod = isStatic + ? painlessClassBuilder.staticMethods.get(painlessMethodKey) + : painlessClassBuilder.methods.get(painlessMethodKey); + PainlessMethod newPainlessMethod = new PainlessMethod( + javaMethod, + targetClass, + returnType, + typeParameters, + methodHandle, + methodType, + annotations + ); if (existingPainlessMethod == null) { newPainlessMethod = painlessMethodCache.computeIfAbsent(newPainlessMethod, key -> key); @@ -612,18 +862,39 @@ public void addPainlessMethod(Class targetClass, Class augmentedClass, painlessClassBuilder.methods.put(painlessMethodKey.intern(), newPainlessMethod); } } else if (newPainlessMethod.equals(existingPainlessMethod) == false) { - throw new IllegalArgumentException("cannot add methods with the same name and arity but are not equivalent for methods " + - "[[" + targetCanonicalClassName + "], [" + methodName + "], " + - "[" + typeToCanonicalTypeName(returnType) + "], " + - typesToCanonicalTypeNames(typeParameters) + "] and " + - "[[" + targetCanonicalClassName + "], [" + methodName + "], " + - "[" + typeToCanonicalTypeName(existingPainlessMethod.returnType) + "], " + - typesToCanonicalTypeNames(existingPainlessMethod.typeParameters) + "]"); + throw new IllegalArgumentException( + "cannot add methods with the same name and arity but are not equivalent for methods " + + "[[" + + targetCanonicalClassName + + "], [" + + methodName + + "], " + + "[" + + typeToCanonicalTypeName(returnType) + + "], " + + typesToCanonicalTypeNames(typeParameters) + + "] and " + + "[[" + + targetCanonicalClassName + + "], [" + + methodName + + "], " + + "[" + + typeToCanonicalTypeName(existingPainlessMethod.returnType) + + "], " + + typesToCanonicalTypeNames(existingPainlessMethod.typeParameters) + + "]" + ); } } - public void addPainlessField(ClassLoader classLoader, String targetCanonicalClassName, - String fieldName, String canonicalTypeNameParameter, Map, Object> annotations) { + public void addPainlessField( + ClassLoader classLoader, + String targetCanonicalClassName, + String fieldName, + String canonicalTypeNameParameter, + Map, Object> annotations + ) { Objects.requireNonNull(classLoader); Objects.requireNonNull(targetCanonicalClassName); @@ -634,34 +905,66 @@ public void addPainlessField(ClassLoader classLoader, String targetCanonicalClas Class targetClass = canonicalClassNamesToClasses.get(targetCanonicalClassName); if (targetClass == null) { - throw new IllegalArgumentException("target class [" + targetCanonicalClassName + "] not found for field " + - "[[" + targetCanonicalClassName + "], [" + fieldName + "], [" + canonicalTypeNameParameter + "]]"); - } - - String augmentedCanonicalClassName = annotations.containsKey(AugmentedAnnotation.class) ? - ((AugmentedAnnotation)annotations.get(AugmentedAnnotation.class)).getAugmentedCanonicalClassName() : null; + throw new IllegalArgumentException( + "target class [" + + targetCanonicalClassName + + "] not found for field " + + "[[" + + targetCanonicalClassName + + "], [" + + fieldName + + "], [" + + canonicalTypeNameParameter + + "]]" + ); + } + + String augmentedCanonicalClassName = annotations.containsKey(AugmentedAnnotation.class) + ? ((AugmentedAnnotation) annotations.get(AugmentedAnnotation.class)).getAugmentedCanonicalClassName() + : null; Class augmentedClass = null; if (augmentedCanonicalClassName != null) { - augmentedClass = loadClass(classLoader, augmentedCanonicalClassName, - () -> "augmented class [" + augmentedCanonicalClassName + "] not found for field " + - "[[" + targetCanonicalClassName + "], [" + fieldName + "]"); + augmentedClass = loadClass( + classLoader, + augmentedCanonicalClassName, + () -> "augmented class [" + + augmentedCanonicalClassName + + "] not found for field " + + "[[" + + targetCanonicalClassName + + "], [" + + fieldName + + "]" + ); } Class typeParameter = canonicalTypeNameToType(canonicalTypeNameParameter); if (typeParameter == null) { - throw new IllegalArgumentException("type parameter [" + canonicalTypeNameParameter + "] not found " + - "for field [[" + targetCanonicalClassName + "], [" + fieldName + "]"); + throw new IllegalArgumentException( + "type parameter [" + + canonicalTypeNameParameter + + "] not found " + + "for field [[" + + targetCanonicalClassName + + "], [" + + fieldName + + "]" + ); } - addPainlessField(targetClass, augmentedClass, fieldName, typeParameter, annotations); } - public void addPainlessField(Class targetClass, Class augmentedClass, - String fieldName, Class typeParameter, Map, Object> annotations) { + public void addPainlessField( + Class targetClass, + Class augmentedClass, + String fieldName, + Class typeParameter, + Map, Object> annotations + ) { Objects.requireNonNull(targetClass); Objects.requireNonNull(fieldName); @@ -676,20 +979,40 @@ public void addPainlessField(Class targetClass, Class augmentedClass, if (FIELD_NAME_PATTERN.matcher(fieldName).matches() == false) { throw new IllegalArgumentException( - "invalid field name [" + fieldName + "] for target class [" + targetCanonicalClassName + "]."); + "invalid field name [" + fieldName + "] for target class [" + targetCanonicalClassName + "]." + ); } - PainlessClassBuilder painlessClassBuilder = classesToPainlessClassBuilders.get(targetClass); if (painlessClassBuilder == null) { - throw new IllegalArgumentException("target class [" + targetCanonicalClassName + "] not found for field " + - "[[" + targetCanonicalClassName + "], [" + fieldName + "], [" + typeToCanonicalTypeName(typeParameter) + "]]"); + throw new IllegalArgumentException( + "target class [" + + targetCanonicalClassName + + "] not found for field " + + "[[" + + targetCanonicalClassName + + "], [" + + fieldName + + "], [" + + typeToCanonicalTypeName(typeParameter) + + "]]" + ); } if (isValidType(typeParameter) == false) { - throw new IllegalArgumentException("type parameter [" + typeToCanonicalTypeName(typeParameter) + "] not found for field " + - "[[" + targetCanonicalClassName + "], [" + fieldName + "], [" + typeToCanonicalTypeName(typeParameter) + "]]"); + throw new IllegalArgumentException( + "type parameter [" + + typeToCanonicalTypeName(typeParameter) + + "] not found for field " + + "[[" + + targetCanonicalClassName + + "], [" + + fieldName + + "], [" + + typeToCanonicalTypeName(typeParameter) + + "]]" + ); } Field javaField; @@ -698,29 +1021,66 @@ public void addPainlessField(Class targetClass, Class augmentedClass, try { javaField = targetClass.getField(fieldName); } catch (NoSuchFieldException nsfe) { - throw new IllegalArgumentException("reflection object not found for field " + - "[[" + targetCanonicalClassName + "], [" + fieldName + "], [" + typeToCanonicalTypeName(typeParameter) + "]]", - nsfe); + throw new IllegalArgumentException( + "reflection object not found for field " + + "[[" + + targetCanonicalClassName + + "], [" + + fieldName + + "], [" + + typeToCanonicalTypeName(typeParameter) + + "]]", + nsfe + ); } } else { try { javaField = augmentedClass.getField(fieldName); if (Modifier.isStatic(javaField.getModifiers()) == false || Modifier.isFinal(javaField.getModifiers()) == false) { - throw new IllegalArgumentException("field [[" + targetCanonicalClassName + "], [" + fieldName + "] " + - "with augmented class [" + typeToCanonicalTypeName(augmentedClass) + "] must be static and final"); + throw new IllegalArgumentException( + "field [[" + + targetCanonicalClassName + + "], [" + + fieldName + + "] " + + "with augmented class [" + + typeToCanonicalTypeName(augmentedClass) + + "] must be static and final" + ); } } catch (NoSuchFieldException nsfe) { - throw new IllegalArgumentException("reflection object not found for field " + - "[[" + targetCanonicalClassName + "], [" + fieldName + "], [" + typeToCanonicalTypeName(typeParameter) + "]]" + - "with augmented class [" + typeToCanonicalTypeName(augmentedClass) + "]", nsfe); + throw new IllegalArgumentException( + "reflection object not found for field " + + "[[" + + targetCanonicalClassName + + "], [" + + fieldName + + "], [" + + typeToCanonicalTypeName(typeParameter) + + "]]" + + "with augmented class [" + + typeToCanonicalTypeName(augmentedClass) + + "]", + nsfe + ); } } if (javaField.getType() != typeToJavaType(typeParameter)) { - throw new IllegalArgumentException("type parameter [" + typeToCanonicalTypeName(javaField.getType()) + "] " + - "does not match the specified type parameter [" + typeToCanonicalTypeName(typeParameter) + "] " + - "for field [[" + targetCanonicalClassName + "], [" + fieldName + "]"); + throw new IllegalArgumentException( + "type parameter [" + + typeToCanonicalTypeName(javaField.getType()) + + "] " + + "does not match the specified type parameter [" + + typeToCanonicalTypeName(typeParameter) + + "] " + + "for field [[" + + targetCanonicalClassName + + "], [" + + fieldName + + "]" + ); } MethodHandle methodHandleGetter; @@ -729,7 +1089,8 @@ public void addPainlessField(Class targetClass, Class augmentedClass, methodHandleGetter = MethodHandles.publicLookup().unreflectGetter(javaField); } catch (IllegalAccessException iae) { throw new IllegalArgumentException( - "getter method handle not found for field [[" + targetCanonicalClassName + "], [" + fieldName + "]]"); + "getter method handle not found for field [[" + targetCanonicalClassName + "], [" + fieldName + "]]" + ); } String painlessFieldKey = buildPainlessFieldKey(fieldName); @@ -746,12 +1107,24 @@ public void addPainlessField(Class targetClass, Class augmentedClass, newPainlessField = painlessFieldCache.computeIfAbsent(newPainlessField, key -> key); painlessClassBuilder.staticFields.put(painlessFieldKey.intern(), newPainlessField); } else if (newPainlessField.equals(existingPainlessField) == false) { - throw new IllegalArgumentException("cannot add fields with the same name but are not equivalent for fields " + - "[[" + targetCanonicalClassName + "], [" + fieldName + "], [" + - typeToCanonicalTypeName(typeParameter) + "] and " + - "[[" + targetCanonicalClassName + "], [" + existingPainlessField.javaField.getName() + "], " + - typeToCanonicalTypeName(existingPainlessField.typeParameter) + "] " + - "with the same name and different type parameters"); + throw new IllegalArgumentException( + "cannot add fields with the same name but are not equivalent for fields " + + "[[" + + targetCanonicalClassName + + "], [" + + fieldName + + "], [" + + typeToCanonicalTypeName(typeParameter) + + "] and " + + "[[" + + targetCanonicalClassName + + "], [" + + existingPainlessField.javaField.getName() + + "], " + + typeToCanonicalTypeName(existingPainlessField.typeParameter) + + "] " + + "with the same name and different type parameters" + ); } } else { MethodHandle methodHandleSetter; @@ -760,30 +1133,53 @@ public void addPainlessField(Class targetClass, Class augmentedClass, methodHandleSetter = MethodHandles.publicLookup().unreflectSetter(javaField); } catch (IllegalAccessException iae) { throw new IllegalArgumentException( - "setter method handle not found for field [[" + targetCanonicalClassName + "], [" + fieldName + "]]"); + "setter method handle not found for field [[" + targetCanonicalClassName + "], [" + fieldName + "]]" + ); } PainlessField existingPainlessField = painlessClassBuilder.fields.get(painlessFieldKey); - PainlessField newPainlessField = - new PainlessField(javaField, typeParameter, annotations, methodHandleGetter, methodHandleSetter); + PainlessField newPainlessField = new PainlessField( + javaField, + typeParameter, + annotations, + methodHandleGetter, + methodHandleSetter + ); if (existingPainlessField == null) { newPainlessField = painlessFieldCache.computeIfAbsent(newPainlessField, key -> key); painlessClassBuilder.fields.put(painlessFieldKey.intern(), newPainlessField); } else if (newPainlessField.equals(existingPainlessField) == false) { - throw new IllegalArgumentException("cannot add fields with the same name but are not equivalent for fields " + - "[[" + targetCanonicalClassName + "], [" + fieldName + "], [" + - typeToCanonicalTypeName(typeParameter) + "] and " + - "[[" + targetCanonicalClassName + "], [" + existingPainlessField.javaField.getName() + "], " + - typeToCanonicalTypeName(existingPainlessField.typeParameter) + "] " + - "with the same name and different type parameters"); + throw new IllegalArgumentException( + "cannot add fields with the same name but are not equivalent for fields " + + "[[" + + targetCanonicalClassName + + "], [" + + fieldName + + "], [" + + typeToCanonicalTypeName(typeParameter) + + "] and " + + "[[" + + targetCanonicalClassName + + "], [" + + existingPainlessField.javaField.getName() + + "], " + + typeToCanonicalTypeName(existingPainlessField.typeParameter) + + "] " + + "with the same name and different type parameters" + ); } } } - public void addImportedPainlessMethod(ClassLoader classLoader, String targetJavaClassName, - String methodName, String returnCanonicalTypeName, List canonicalTypeNameParameters, - Map, Object> annotations) { + public void addImportedPainlessMethod( + ClassLoader classLoader, + String targetJavaClassName, + String methodName, + String returnCanonicalTypeName, + List canonicalTypeNameParameters, + Map, Object> annotations + ) { Objects.requireNonNull(classLoader); Objects.requireNonNull(targetJavaClassName); @@ -795,8 +1191,18 @@ public void addImportedPainlessMethod(ClassLoader classLoader, String targetJava String targetCanonicalClassName = typeToCanonicalTypeName(targetClass); if (targetClass == null) { - throw new IllegalArgumentException("target class [" + targetCanonicalClassName + "] not found for imported method " + - "[[" + targetCanonicalClassName + "], [" + methodName + "], " + canonicalTypeNameParameters + "]"); + throw new IllegalArgumentException( + "target class [" + + targetCanonicalClassName + + "] not found for imported method " + + "[[" + + targetCanonicalClassName + + "], [" + + methodName + + "], " + + canonicalTypeNameParameters + + "]" + ); } List> typeParameters = new ArrayList<>(canonicalTypeNameParameters.size()); @@ -805,8 +1211,18 @@ public void addImportedPainlessMethod(ClassLoader classLoader, String targetJava Class typeParameter = canonicalTypeNameToType(canonicalTypeNameParameter); if (typeParameter == null) { - throw new IllegalArgumentException("type parameter [" + canonicalTypeNameParameter + "] not found for imported method " + - "[[" + targetCanonicalClassName + "], [" + methodName + "], " + canonicalTypeNameParameters + "]"); + throw new IllegalArgumentException( + "type parameter [" + + canonicalTypeNameParameter + + "] not found for imported method " + + "[[" + + targetCanonicalClassName + + "], [" + + methodName + + "], " + + canonicalTypeNameParameters + + "]" + ); } typeParameters.add(typeParameter); @@ -815,15 +1231,30 @@ public void addImportedPainlessMethod(ClassLoader classLoader, String targetJava Class returnType = canonicalTypeNameToType(returnCanonicalTypeName); if (returnType == null) { - throw new IllegalArgumentException("return type [" + returnCanonicalTypeName + "] not found for imported method " + - "[[" + targetCanonicalClassName + "], [" + methodName + "], " + canonicalTypeNameParameters + "]"); + throw new IllegalArgumentException( + "return type [" + + returnCanonicalTypeName + + "] not found for imported method " + + "[[" + + targetCanonicalClassName + + "], [" + + methodName + + "], " + + canonicalTypeNameParameters + + "]" + ); } addImportedPainlessMethod(targetClass, methodName, returnType, typeParameters, annotations); } - public void addImportedPainlessMethod(Class targetClass, String methodName, Class returnType, List> typeParameters, - Map, Object> annotations) { + public void addImportedPainlessMethod( + Class targetClass, + String methodName, + Class returnType, + List> typeParameters, + Map, Object> annotations + ) { Objects.requireNonNull(targetClass); Objects.requireNonNull(methodName); Objects.requireNonNull(returnType); @@ -839,13 +1270,18 @@ public void addImportedPainlessMethod(Class targetClass, String methodName, C if (existingTargetClass == null) { javaClassNamesToClasses.put(targetClass.getName().intern(), targetClass); } else if (existingTargetClass != targetClass) { - throw new IllegalArgumentException("class [" + targetCanonicalClassName + "] " + - "cannot represent multiple java classes with the same name from different class loaders"); + throw new IllegalArgumentException( + "class [" + + targetCanonicalClassName + + "] " + + "cannot represent multiple java classes with the same name from different class loaders" + ); } if (METHOD_NAME_PATTERN.matcher(methodName).matches() == false) { throw new IllegalArgumentException( - "invalid imported method name [" + methodName + "] for target class [" + targetCanonicalClassName + "]."); + "invalid imported method name [" + methodName + "] for target class [" + targetCanonicalClassName + "]." + ); } int typeParametersSize = typeParameters.size(); @@ -853,17 +1289,36 @@ public void addImportedPainlessMethod(Class targetClass, String methodName, C for (Class typeParameter : typeParameters) { if (isValidType(typeParameter) == false) { - throw new IllegalArgumentException("type parameter [" + typeToCanonicalTypeName(typeParameter) + "] " + - "not found for imported method [[" + targetCanonicalClassName + "], [" + methodName + "], " + - typesToCanonicalTypeNames(typeParameters) + "]"); + throw new IllegalArgumentException( + "type parameter [" + + typeToCanonicalTypeName(typeParameter) + + "] " + + "not found for imported method [[" + + targetCanonicalClassName + + "], [" + + methodName + + "], " + + typesToCanonicalTypeNames(typeParameters) + + "]" + ); } javaTypeParameters.add(typeToJavaType(typeParameter)); } if (isValidType(returnType) == false) { - throw new IllegalArgumentException("return type [" + typeToCanonicalTypeName(returnType) + "] not found for imported method " + - "[[" + targetCanonicalClassName + "], [" + methodName + "], " + typesToCanonicalTypeNames(typeParameters) + "]"); + throw new IllegalArgumentException( + "return type [" + + typeToCanonicalTypeName(returnType) + + "] not found for imported method " + + "[[" + + targetCanonicalClassName + + "], [" + + methodName + + "], " + + typesToCanonicalTypeNames(typeParameters) + + "]" + ); } Method javaMethod; @@ -871,20 +1326,47 @@ public void addImportedPainlessMethod(Class targetClass, String methodName, C try { javaMethod = targetClass.getMethod(methodName, javaTypeParameters.toArray(new Class[typeParametersSize])); } catch (NoSuchMethodException nsme) { - throw new IllegalArgumentException("imported method reflection object [[" + targetCanonicalClassName + "], " + - "[" + methodName + "], " + typesToCanonicalTypeNames(typeParameters) + "] not found", nsme); + throw new IllegalArgumentException( + "imported method reflection object [[" + + targetCanonicalClassName + + "], " + + "[" + + methodName + + "], " + + typesToCanonicalTypeNames(typeParameters) + + "] not found", + nsme + ); } if (javaMethod.getReturnType() != typeToJavaType(returnType)) { - throw new IllegalArgumentException("return type [" + typeToCanonicalTypeName(javaMethod.getReturnType()) + "] " + - "does not match the specified returned type [" + typeToCanonicalTypeName(returnType) + "] " + - "for imported method [[" + targetClass.getCanonicalName() + "], [" + methodName + "], " + - typesToCanonicalTypeNames(typeParameters) + "]"); + throw new IllegalArgumentException( + "return type [" + + typeToCanonicalTypeName(javaMethod.getReturnType()) + + "] " + + "does not match the specified returned type [" + + typeToCanonicalTypeName(returnType) + + "] " + + "for imported method [[" + + targetClass.getCanonicalName() + + "], [" + + methodName + + "], " + + typesToCanonicalTypeNames(typeParameters) + + "]" + ); } if (Modifier.isStatic(javaMethod.getModifiers()) == false) { - throw new IllegalArgumentException("imported method [[" + targetClass.getCanonicalName() + "], [" + methodName + "], " + - typesToCanonicalTypeNames(typeParameters) + "] must be static"); + throw new IllegalArgumentException( + "imported method [[" + + targetClass.getCanonicalName() + + "], [" + + methodName + + "], " + + typesToCanonicalTypeNames(typeParameters) + + "] must be static" + ); } String painlessMethodKey = buildPainlessMethodKey(methodName, typeParametersSize); @@ -902,34 +1384,71 @@ public void addImportedPainlessMethod(Class targetClass, String methodName, C try { methodHandle = MethodHandles.publicLookup().in(targetClass).unreflect(javaMethod); } catch (IllegalAccessException iae) { - throw new IllegalArgumentException("imported method handle [[" + targetClass.getCanonicalName() + "], " + - "[" + methodName + "], " + typesToCanonicalTypeNames(typeParameters) + "] not found", iae); + throw new IllegalArgumentException( + "imported method handle [[" + + targetClass.getCanonicalName() + + "], " + + "[" + + methodName + + "], " + + typesToCanonicalTypeNames(typeParameters) + + "] not found", + iae + ); } MethodType methodType = methodHandle.type(); PainlessMethod existingImportedPainlessMethod = painlessMethodKeysToImportedPainlessMethods.get(painlessMethodKey); - PainlessMethod newImportedPainlessMethod = - new PainlessMethod(javaMethod, targetClass, returnType, typeParameters, methodHandle, methodType, annotations); + PainlessMethod newImportedPainlessMethod = new PainlessMethod( + javaMethod, + targetClass, + returnType, + typeParameters, + methodHandle, + methodType, + annotations + ); if (existingImportedPainlessMethod == null) { newImportedPainlessMethod = painlessMethodCache.computeIfAbsent(newImportedPainlessMethod, key -> key); painlessMethodKeysToImportedPainlessMethods.put(painlessMethodKey.intern(), newImportedPainlessMethod); } else if (newImportedPainlessMethod.equals(existingImportedPainlessMethod) == false) { - throw new IllegalArgumentException("cannot add imported methods with the same name and arity " + - "but do not have equivalent methods " + - "[[" + targetCanonicalClassName + "], [" + methodName + "], " + - "[" + typeToCanonicalTypeName(returnType) + "], " + - typesToCanonicalTypeNames(typeParameters) + "] and " + - "[[" + targetCanonicalClassName + "], [" + methodName + "], " + - "[" + typeToCanonicalTypeName(existingImportedPainlessMethod.returnType) + "], " + - typesToCanonicalTypeNames(existingImportedPainlessMethod.typeParameters) + "]"); + throw new IllegalArgumentException( + "cannot add imported methods with the same name and arity " + + "but do not have equivalent methods " + + "[[" + + targetCanonicalClassName + + "], [" + + methodName + + "], " + + "[" + + typeToCanonicalTypeName(returnType) + + "], " + + typesToCanonicalTypeNames(typeParameters) + + "] and " + + "[[" + + targetCanonicalClassName + + "], [" + + methodName + + "], " + + "[" + + typeToCanonicalTypeName(existingImportedPainlessMethod.returnType) + + "], " + + typesToCanonicalTypeNames(existingImportedPainlessMethod.typeParameters) + + "]" + ); } } - public void addPainlessClassBinding(ClassLoader classLoader, String targetJavaClassName, - String methodName, String returnCanonicalTypeName, List canonicalTypeNameParameters, - Map, Object> annotations) { + public void addPainlessClassBinding( + ClassLoader classLoader, + String targetJavaClassName, + String methodName, + String returnCanonicalTypeName, + List canonicalTypeNameParameters, + Map, Object> annotations + ) { Objects.requireNonNull(classLoader); Objects.requireNonNull(targetJavaClassName); @@ -945,8 +1464,18 @@ public void addPainlessClassBinding(ClassLoader classLoader, String targetJavaCl Class typeParameter = canonicalTypeNameToType(canonicalTypeNameParameter); if (typeParameter == null) { - throw new IllegalArgumentException("type parameter [" + canonicalTypeNameParameter + "] not found for class binding " + - "[[" + targetCanonicalClassName + "], [" + methodName + "], " + canonicalTypeNameParameters + "]"); + throw new IllegalArgumentException( + "type parameter [" + + canonicalTypeNameParameter + + "] not found for class binding " + + "[[" + + targetCanonicalClassName + + "], [" + + methodName + + "], " + + canonicalTypeNameParameters + + "]" + ); } typeParameters.add(typeParameter); @@ -955,15 +1484,30 @@ public void addPainlessClassBinding(ClassLoader classLoader, String targetJavaCl Class returnType = canonicalTypeNameToType(returnCanonicalTypeName); if (returnType == null) { - throw new IllegalArgumentException("return type [" + returnCanonicalTypeName + "] not found for class binding " + - "[[" + targetCanonicalClassName + "], [" + methodName + "], " + canonicalTypeNameParameters + "]"); + throw new IllegalArgumentException( + "return type [" + + returnCanonicalTypeName + + "] not found for class binding " + + "[[" + + targetCanonicalClassName + + "], [" + + methodName + + "], " + + canonicalTypeNameParameters + + "]" + ); } addPainlessClassBinding(targetClass, methodName, returnType, typeParameters, annotations); } - public void addPainlessClassBinding(Class targetClass, String methodName, Class returnType, List> typeParameters, - Map, Object> annotations) { + public void addPainlessClassBinding( + Class targetClass, + String methodName, + Class returnType, + List> typeParameters, + Map, Object> annotations + ) { Objects.requireNonNull(targetClass); Objects.requireNonNull(methodName); Objects.requireNonNull(returnType); @@ -979,8 +1523,12 @@ public void addPainlessClassBinding(Class targetClass, String methodName, Cla if (existingTargetClass == null) { javaClassNamesToClasses.put(targetClass.getName().intern(), targetClass); } else if (existingTargetClass != targetClass) { - throw new IllegalArgumentException("class [" + targetCanonicalClassName + "] " + - "cannot represent multiple java classes with the same name from different class loaders"); + throw new IllegalArgumentException( + "class [" + + targetCanonicalClassName + + "] " + + "cannot represent multiple java classes with the same name from different class loaders" + ); } Constructor[] javaConstructors = targetClass.getConstructors(); @@ -990,7 +1538,8 @@ public void addPainlessClassBinding(Class targetClass, String methodName, Cla if (eachJavaConstructor.getDeclaringClass() == targetClass) { if (javaConstructor != null) { throw new IllegalArgumentException( - "class binding [" + targetCanonicalClassName + "] cannot have multiple constructors"); + "class binding [" + targetCanonicalClassName + "] cannot have multiple constructors" + ); } javaConstructor = eachJavaConstructor; @@ -1007,27 +1556,54 @@ public void addPainlessClassBinding(Class targetClass, String methodName, Cla Class typeParameter = typeParameters.get(typeParameterIndex); if (isValidType(typeParameter) == false) { - throw new IllegalArgumentException("type parameter [" + typeToCanonicalTypeName(typeParameter) + "] not found " + - "for class binding [[" + targetCanonicalClassName + "], " + typesToCanonicalTypeNames(typeParameters) + "]"); + throw new IllegalArgumentException( + "type parameter [" + + typeToCanonicalTypeName(typeParameter) + + "] not found " + + "for class binding [[" + + targetCanonicalClassName + + "], " + + typesToCanonicalTypeNames(typeParameters) + + "]" + ); } Class javaTypeParameter = javaConstructor.getParameterTypes()[typeParameterIndex]; if (isValidType(javaTypeParameter) == false) { - throw new IllegalArgumentException("type parameter [" + typeToCanonicalTypeName(typeParameter) + "] not found " + - "for class binding [[" + targetCanonicalClassName + "], " + typesToCanonicalTypeNames(typeParameters) + "]"); + throw new IllegalArgumentException( + "type parameter [" + + typeToCanonicalTypeName(typeParameter) + + "] not found " + + "for class binding [[" + + targetCanonicalClassName + + "], " + + typesToCanonicalTypeNames(typeParameters) + + "]" + ); } if (javaTypeParameter != typeToJavaType(typeParameter)) { - throw new IllegalArgumentException("type parameter [" + typeToCanonicalTypeName(javaTypeParameter) + "] " + - "does not match the specified type parameter [" + typeToCanonicalTypeName(typeParameter) + "] " + - "for class binding [[" + targetClass.getCanonicalName() + "], " + typesToCanonicalTypeNames(typeParameters) + "]"); + throw new IllegalArgumentException( + "type parameter [" + + typeToCanonicalTypeName(javaTypeParameter) + + "] " + + "does not match the specified type parameter [" + + typeToCanonicalTypeName(typeParameter) + + "] " + + "for class binding [[" + + targetClass.getCanonicalName() + + "], " + + typesToCanonicalTypeNames(typeParameters) + + "]" + ); } } if (METHOD_NAME_PATTERN.matcher(methodName).matches() == false) { throw new IllegalArgumentException( - "invalid method name [" + methodName + "] for class binding [" + targetCanonicalClassName + "]."); + "invalid method name [" + methodName + "] for class binding [" + targetCanonicalClassName + "]." + ); } if (annotations.containsKey(CompileTimeOnlyAnnotation.class)) { @@ -1057,34 +1633,81 @@ public void addPainlessClassBinding(Class targetClass, String methodName, Cla Class typeParameter = typeParameters.get(constructorTypeParametersSize + typeParameterIndex); if (isValidType(typeParameter) == false) { - throw new IllegalArgumentException("type parameter [" + typeToCanonicalTypeName(typeParameter) + "] not found " + - "for class binding [[" + targetCanonicalClassName + "], " + typesToCanonicalTypeNames(typeParameters) + "]"); + throw new IllegalArgumentException( + "type parameter [" + + typeToCanonicalTypeName(typeParameter) + + "] not found " + + "for class binding [[" + + targetCanonicalClassName + + "], " + + typesToCanonicalTypeNames(typeParameters) + + "]" + ); } Class javaTypeParameter = javaMethod.getParameterTypes()[typeParameterIndex]; if (isValidType(javaTypeParameter) == false) { - throw new IllegalArgumentException("type parameter [" + typeToCanonicalTypeName(typeParameter) + "] not found " + - "for class binding [[" + targetCanonicalClassName + "], " + typesToCanonicalTypeNames(typeParameters) + "]"); + throw new IllegalArgumentException( + "type parameter [" + + typeToCanonicalTypeName(typeParameter) + + "] not found " + + "for class binding [[" + + targetCanonicalClassName + + "], " + + typesToCanonicalTypeNames(typeParameters) + + "]" + ); } if (javaTypeParameter != typeToJavaType(typeParameter)) { - throw new IllegalArgumentException("type parameter [" + typeToCanonicalTypeName(javaTypeParameter) + "] " + - "does not match the specified type parameter [" + typeToCanonicalTypeName(typeParameter) + "] " + - "for class binding [[" + targetClass.getCanonicalName() + "], " + typesToCanonicalTypeNames(typeParameters) + "]"); + throw new IllegalArgumentException( + "type parameter [" + + typeToCanonicalTypeName(javaTypeParameter) + + "] " + + "does not match the specified type parameter [" + + typeToCanonicalTypeName(typeParameter) + + "] " + + "for class binding [[" + + targetClass.getCanonicalName() + + "], " + + typesToCanonicalTypeNames(typeParameters) + + "]" + ); } } if (isValidType(returnType) == false) { - throw new IllegalArgumentException("return type [" + typeToCanonicalTypeName(returnType) + "] not found for class binding " + - "[[" + targetCanonicalClassName + "], [" + methodName + "], " + typesToCanonicalTypeNames(typeParameters) + "]"); + throw new IllegalArgumentException( + "return type [" + + typeToCanonicalTypeName(returnType) + + "] not found for class binding " + + "[[" + + targetCanonicalClassName + + "], [" + + methodName + + "], " + + typesToCanonicalTypeNames(typeParameters) + + "]" + ); } if (javaMethod.getReturnType() != typeToJavaType(returnType)) { - throw new IllegalArgumentException("return type [" + typeToCanonicalTypeName(javaMethod.getReturnType()) + "] " + - "does not match the specified returned type [" + typeToCanonicalTypeName(returnType) + "] " + - "for class binding [[" + targetClass.getCanonicalName() + "], [" + methodName + "], " + - typesToCanonicalTypeNames(typeParameters) + "]"); + throw new IllegalArgumentException( + "return type [" + + typeToCanonicalTypeName(javaMethod.getReturnType()) + + "] " + + "does not match the specified returned type [" + + typeToCanonicalTypeName(returnType) + + "] " + + "for class binding [[" + + targetClass.getCanonicalName() + + "], [" + + methodName + + "], " + + typesToCanonicalTypeNames(typeParameters) + + "]" + ); } String painlessMethodKey = buildPainlessMethodKey(methodName, constructorTypeParametersSize + methodTypeParametersSize); @@ -1098,34 +1721,66 @@ public void addPainlessClassBinding(Class targetClass, String methodName, Cla } if (Modifier.isStatic(javaMethod.getModifiers())) { - throw new IllegalArgumentException("class binding [[" + targetClass.getCanonicalName() + "], [" + methodName + "], " + - typesToCanonicalTypeNames(typeParameters) + "] cannot be static"); + throw new IllegalArgumentException( + "class binding [[" + + targetClass.getCanonicalName() + + "], [" + + methodName + + "], " + + typesToCanonicalTypeNames(typeParameters) + + "] cannot be static" + ); } PainlessClassBinding existingPainlessClassBinding = painlessMethodKeysToPainlessClassBindings.get(painlessMethodKey); - PainlessClassBinding newPainlessClassBinding = - new PainlessClassBinding(javaConstructor, javaMethod, returnType, typeParameters, annotations); + PainlessClassBinding newPainlessClassBinding = new PainlessClassBinding( + javaConstructor, + javaMethod, + returnType, + typeParameters, + annotations + ); if (existingPainlessClassBinding == null) { newPainlessClassBinding = painlessClassBindingCache.computeIfAbsent(newPainlessClassBinding, key -> key); painlessMethodKeysToPainlessClassBindings.put(painlessMethodKey.intern(), newPainlessClassBinding); } else if (newPainlessClassBinding.equals(existingPainlessClassBinding) == false) { - throw new IllegalArgumentException("cannot add class bindings with the same name and arity " + - "but do not have equivalent methods " + - "[[" + targetCanonicalClassName + "], " + - "[" + methodName + "], " + - "[" + typeToCanonicalTypeName(returnType) + "], " + - typesToCanonicalTypeNames(typeParameters) + "] and " + - "[[" + targetCanonicalClassName + "], " + - "[" + methodName + "], " + - "[" + typeToCanonicalTypeName(existingPainlessClassBinding.returnType) + "], " + - typesToCanonicalTypeNames(existingPainlessClassBinding.typeParameters) + "]"); + throw new IllegalArgumentException( + "cannot add class bindings with the same name and arity " + + "but do not have equivalent methods " + + "[[" + + targetCanonicalClassName + + "], " + + "[" + + methodName + + "], " + + "[" + + typeToCanonicalTypeName(returnType) + + "], " + + typesToCanonicalTypeNames(typeParameters) + + "] and " + + "[[" + + targetCanonicalClassName + + "], " + + "[" + + methodName + + "], " + + "[" + + typeToCanonicalTypeName(existingPainlessClassBinding.returnType) + + "], " + + typesToCanonicalTypeNames(existingPainlessClassBinding.typeParameters) + + "]" + ); } } - public void addPainlessInstanceBinding(Object targetInstance, - String methodName, String returnCanonicalTypeName, List canonicalTypeNameParameters, - Map, Object> painlessAnnotations) { + public void addPainlessInstanceBinding( + Object targetInstance, + String methodName, + String returnCanonicalTypeName, + List canonicalTypeNameParameters, + Map, Object> painlessAnnotations + ) { Objects.requireNonNull(targetInstance); Objects.requireNonNull(methodName); @@ -1140,8 +1795,18 @@ public void addPainlessInstanceBinding(Object targetInstance, Class typeParameter = canonicalTypeNameToType(canonicalTypeNameParameter); if (typeParameter == null) { - throw new IllegalArgumentException("type parameter [" + canonicalTypeNameParameter + "] not found for instance binding " + - "[[" + targetCanonicalClassName + "], [" + methodName + "], " + canonicalTypeNameParameters + "]"); + throw new IllegalArgumentException( + "type parameter [" + + canonicalTypeNameParameter + + "] not found for instance binding " + + "[[" + + targetCanonicalClassName + + "], [" + + methodName + + "], " + + canonicalTypeNameParameters + + "]" + ); } typeParameters.add(typeParameter); @@ -1150,8 +1815,18 @@ public void addPainlessInstanceBinding(Object targetInstance, Class returnType = canonicalTypeNameToType(returnCanonicalTypeName); if (returnType == null) { - throw new IllegalArgumentException("return type [" + returnCanonicalTypeName + "] not found for class binding " + - "[[" + targetCanonicalClassName + "], [" + methodName + "], " + canonicalTypeNameParameters + "]"); + throw new IllegalArgumentException( + "return type [" + + returnCanonicalTypeName + + "] not found for class binding " + + "[[" + + targetCanonicalClassName + + "], [" + + methodName + + "], " + + canonicalTypeNameParameters + + "]" + ); } addPainlessInstanceBinding(targetInstance, methodName, returnType, typeParameters, painlessAnnotations); @@ -1181,13 +1856,18 @@ public void addPainlessInstanceBinding( if (existingTargetClass == null) { javaClassNamesToClasses.put(targetClass.getName().intern(), targetClass); } else if (existingTargetClass != targetClass) { - throw new IllegalArgumentException("class [" + targetCanonicalClassName + "] " + - "cannot represent multiple java classes with the same name from different class loaders"); + throw new IllegalArgumentException( + "class [" + + targetCanonicalClassName + + "] " + + "cannot represent multiple java classes with the same name from different class loaders" + ); } if (METHOD_NAME_PATTERN.matcher(methodName).matches() == false) { throw new IllegalArgumentException( - "invalid method name [" + methodName + "] for instance binding [" + targetCanonicalClassName + "]."); + "invalid method name [" + methodName + "] for instance binding [" + targetCanonicalClassName + "]." + ); } int typeParametersSize = typeParameters.size(); @@ -1195,17 +1875,36 @@ public void addPainlessInstanceBinding( for (Class typeParameter : typeParameters) { if (isValidType(typeParameter) == false) { - throw new IllegalArgumentException("type parameter [" + typeToCanonicalTypeName(typeParameter) + "] " + - "not found for instance binding [[" + targetCanonicalClassName + "], [" + methodName + "], " + - typesToCanonicalTypeNames(typeParameters) + "]"); + throw new IllegalArgumentException( + "type parameter [" + + typeToCanonicalTypeName(typeParameter) + + "] " + + "not found for instance binding [[" + + targetCanonicalClassName + + "], [" + + methodName + + "], " + + typesToCanonicalTypeNames(typeParameters) + + "]" + ); } javaTypeParameters.add(typeToJavaType(typeParameter)); } if (isValidType(returnType) == false) { - throw new IllegalArgumentException("return type [" + typeToCanonicalTypeName(returnType) + "] not found for imported method " + - "[[" + targetCanonicalClassName + "], [" + methodName + "], " + typesToCanonicalTypeNames(typeParameters) + "]"); + throw new IllegalArgumentException( + "return type [" + + typeToCanonicalTypeName(returnType) + + "] not found for imported method " + + "[[" + + targetCanonicalClassName + + "], [" + + methodName + + "], " + + typesToCanonicalTypeNames(typeParameters) + + "]" + ); } Method javaMethod; @@ -1213,20 +1912,47 @@ public void addPainlessInstanceBinding( try { javaMethod = targetClass.getMethod(methodName, javaTypeParameters.toArray(new Class[typeParametersSize])); } catch (NoSuchMethodException nsme) { - throw new IllegalArgumentException("instance binding reflection object [[" + targetCanonicalClassName + "], " + - "[" + methodName + "], " + typesToCanonicalTypeNames(typeParameters) + "] not found", nsme); + throw new IllegalArgumentException( + "instance binding reflection object [[" + + targetCanonicalClassName + + "], " + + "[" + + methodName + + "], " + + typesToCanonicalTypeNames(typeParameters) + + "] not found", + nsme + ); } if (javaMethod.getReturnType() != typeToJavaType(returnType)) { - throw new IllegalArgumentException("return type [" + typeToCanonicalTypeName(javaMethod.getReturnType()) + "] " + - "does not match the specified returned type [" + typeToCanonicalTypeName(returnType) + "] " + - "for instance binding [[" + targetClass.getCanonicalName() + "], [" + methodName + "], " + - typesToCanonicalTypeNames(typeParameters) + "]"); + throw new IllegalArgumentException( + "return type [" + + typeToCanonicalTypeName(javaMethod.getReturnType()) + + "] " + + "does not match the specified returned type [" + + typeToCanonicalTypeName(returnType) + + "] " + + "for instance binding [[" + + targetClass.getCanonicalName() + + "], [" + + methodName + + "], " + + typesToCanonicalTypeNames(typeParameters) + + "]" + ); } if (Modifier.isStatic(javaMethod.getModifiers())) { - throw new IllegalArgumentException("instance binding [[" + targetClass.getCanonicalName() + "], [" + methodName + "], " + - typesToCanonicalTypeNames(typeParameters) + "] cannot be static"); + throw new IllegalArgumentException( + "instance binding [[" + + targetClass.getCanonicalName() + + "], [" + + methodName + + "], " + + typesToCanonicalTypeNames(typeParameters) + + "] cannot be static" + ); } String painlessMethodKey = buildPainlessMethodKey(methodName, typeParametersSize); @@ -1240,25 +1966,47 @@ public void addPainlessInstanceBinding( } PainlessInstanceBinding existingPainlessInstanceBinding = painlessMethodKeysToPainlessInstanceBindings.get(painlessMethodKey); - PainlessInstanceBinding newPainlessInstanceBinding = - new PainlessInstanceBinding(targetInstance, javaMethod, returnType, typeParameters, painlessAnnotations); + PainlessInstanceBinding newPainlessInstanceBinding = new PainlessInstanceBinding( + targetInstance, + javaMethod, + returnType, + typeParameters, + painlessAnnotations + ); if (existingPainlessInstanceBinding == null) { newPainlessInstanceBinding = painlessInstanceBindingCache.computeIfAbsent(newPainlessInstanceBinding, key -> key); painlessMethodKeysToPainlessInstanceBindings.put(painlessMethodKey.intern(), newPainlessInstanceBinding); } else if (newPainlessInstanceBinding.equals(existingPainlessInstanceBinding) == false) { - throw new IllegalArgumentException("cannot add instances bindings with the same name and arity " + - "but do not have equivalent methods " + - "[[" + targetCanonicalClassName + "], " + - "[" + methodName + "], " + - "[" + typeToCanonicalTypeName(returnType) + "], " + - typesToCanonicalTypeNames(typeParameters) + "], " + - painlessAnnotations + " and " + - "[[" + targetCanonicalClassName + "], " + - "[" + methodName + "], " + - "[" + typeToCanonicalTypeName(existingPainlessInstanceBinding.returnType) + "], " + - typesToCanonicalTypeNames(existingPainlessInstanceBinding.typeParameters) + "], " + - existingPainlessInstanceBinding.annotations); + throw new IllegalArgumentException( + "cannot add instances bindings with the same name and arity " + + "but do not have equivalent methods " + + "[[" + + targetCanonicalClassName + + "], " + + "[" + + methodName + + "], " + + "[" + + typeToCanonicalTypeName(returnType) + + "], " + + typesToCanonicalTypeNames(typeParameters) + + "], " + + painlessAnnotations + + " and " + + "[[" + + targetCanonicalClassName + + "], " + + "[" + + methodName + + "], " + + "[" + + typeToCanonicalTypeName(existingPainlessInstanceBinding.returnType) + + "], " + + typesToCanonicalTypeNames(existingPainlessInstanceBinding.typeParameters) + + "], " + + existingPainlessInstanceBinding.annotations + ); } } @@ -1275,29 +2023,33 @@ public PainlessLookup build() { } if (javaClassNamesToClasses.values().containsAll(canonicalClassNamesToClasses.values()) == false) { - throw new IllegalArgumentException("the values of java class names to classes " + - "must be a superset of the values of canonical class names to classes"); + throw new IllegalArgumentException( + "the values of java class names to classes " + "must be a superset of the values of canonical class names to classes" + ); } if (javaClassNamesToClasses.values().containsAll(classesToPainlessClasses.keySet()) == false) { - throw new IllegalArgumentException("the values of java class names to classes " + - "must be a superset of the keys of classes to painless classes"); + throw new IllegalArgumentException( + "the values of java class names to classes " + "must be a superset of the keys of classes to painless classes" + ); } - if (canonicalClassNamesToClasses.values().containsAll(classesToPainlessClasses.keySet()) == false || - classesToPainlessClasses.keySet().containsAll(canonicalClassNamesToClasses.values()) == false) { - throw new IllegalArgumentException("the values of canonical class names to classes " + - "must have the same classes as the keys of classes to painless classes"); + if (canonicalClassNamesToClasses.values().containsAll(classesToPainlessClasses.keySet()) == false + || classesToPainlessClasses.keySet().containsAll(canonicalClassNamesToClasses.values()) == false) { + throw new IllegalArgumentException( + "the values of canonical class names to classes " + "must have the same classes as the keys of classes to painless classes" + ); } return new PainlessLookup( - javaClassNamesToClasses, - canonicalClassNamesToClasses, - classesToPainlessClasses, - classesToDirectSubClasses, - painlessMethodKeysToImportedPainlessMethods, - painlessMethodKeysToPainlessClassBindings, - painlessMethodKeysToPainlessInstanceBindings); + javaClassNamesToClasses, + canonicalClassNamesToClasses, + classesToPainlessClasses, + classesToDirectSubClasses, + painlessMethodKeysToImportedPainlessMethods, + painlessMethodKeysToPainlessClassBindings, + painlessMethodKeysToPainlessInstanceBindings + ); } private void buildPainlessClassHierarchy() { @@ -1318,7 +2070,7 @@ private void buildPainlessClassHierarchy() { // this finds the nearest super class for a given sub class // because the allow list may have gaps between classes // example: - // class A {} // allowed + // class A {} // allowed // class B extends A // not allowed // class C extends B // allowed // in this case C is considered a direct sub class of A @@ -1379,8 +2131,13 @@ private void setFunctionalInterfaceMethod(Class targetClass, PainlessClassBui } if (javaMethods.size() != 1 && targetClass.isAnnotationPresent(FunctionalInterface.class)) { - throw new IllegalArgumentException("class [" + typeToCanonicalTypeName(targetClass) + "] " + - "is illegally marked as a FunctionalInterface with java methods " + javaMethods); + throw new IllegalArgumentException( + "class [" + + typeToCanonicalTypeName(targetClass) + + "] " + + "is illegally marked as a FunctionalInterface with java methods " + + javaMethods + ); } else if (javaMethods.size() == 1) { java.lang.reflect.Method javaMethod = javaMethods.get(0); String painlessMethodKey = buildPainlessMethodKey(javaMethod.getName(), javaMethod.getParameterCount()); @@ -1406,15 +2163,13 @@ private void generateRuntimeMethods() { for (PainlessMethod painlessMethod : painlessClassBuilder.runtimeMethods.values()) { for (Class typeParameter : painlessMethod.typeParameters) { - if ( - typeParameter == Byte.class || - typeParameter == Short.class || - typeParameter == Character.class || - typeParameter == Integer.class || - typeParameter == Long.class || - typeParameter == Float.class || - typeParameter == Double.class - ) { + if (typeParameter == Byte.class + || typeParameter == Short.class + || typeParameter == Character.class + || typeParameter == Integer.class + || typeParameter == Long.class + || typeParameter == Float.class + || typeParameter == Double.class) { generateBridgeMethod(painlessClassBuilder, painlessMethod); } } @@ -1432,17 +2187,35 @@ private void generateBridgeMethod(PainlessClassBuilder painlessClassBuilder, Pai int bridgeClassFrames = ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS; int bridgeClassAccess = Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER | Opcodes.ACC_FINAL; - String bridgeClassName = - "org/elasticsearch/painless/Bridge$" + javaMethod.getDeclaringClass().getSimpleName() + "$" + javaMethod.getName(); + String bridgeClassName = "org/elasticsearch/painless/Bridge$" + + javaMethod.getDeclaringClass().getSimpleName() + + "$" + + javaMethod.getName(); ClassWriter bridgeClassWriter = new ClassWriter(bridgeClassFrames); bridgeClassWriter.visit( - WriterConstants.CLASS_VERSION, bridgeClassAccess, bridgeClassName, null, OBJECT_TYPE.getInternalName(), null); - - org.objectweb.asm.commons.Method bridgeConstructorType = - new org.objectweb.asm.commons.Method("", MethodType.methodType(void.class).toMethodDescriptorString()); - GeneratorAdapter bridgeConstructorWriter = - new GeneratorAdapter(Opcodes.ASM5, bridgeConstructorType, bridgeClassWriter.visitMethod( - Opcodes.ACC_PRIVATE, bridgeConstructorType.getName(), bridgeConstructorType.getDescriptor(), null, null)); + WriterConstants.CLASS_VERSION, + bridgeClassAccess, + bridgeClassName, + null, + OBJECT_TYPE.getInternalName(), + null + ); + + org.objectweb.asm.commons.Method bridgeConstructorType = new org.objectweb.asm.commons.Method( + "", + MethodType.methodType(void.class).toMethodDescriptorString() + ); + GeneratorAdapter bridgeConstructorWriter = new GeneratorAdapter( + Opcodes.ASM5, + bridgeConstructorType, + bridgeClassWriter.visitMethod( + Opcodes.ACC_PRIVATE, + bridgeConstructorType.getName(), + bridgeConstructorType.getDescriptor(), + null, + null + ) + ); bridgeConstructorWriter.visitCode(); bridgeConstructorWriter.loadThis(); bridgeConstructorWriter.invokeConstructor(OBJECT_TYPE, bridgeConstructorType); @@ -1457,15 +2230,13 @@ private void generateBridgeMethod(PainlessClassBuilder painlessClassBuilder, Pai } for (Class typeParameter : javaMethod.getParameterTypes()) { - if ( - typeParameter == Byte.class || - typeParameter == Short.class || - typeParameter == Character.class || - typeParameter == Integer.class || - typeParameter == Long.class || - typeParameter == Float.class || - typeParameter == Double.class - ) { + if (typeParameter == Byte.class + || typeParameter == Short.class + || typeParameter == Character.class + || typeParameter == Integer.class + || typeParameter == Long.class + || typeParameter == Float.class + || typeParameter == Double.class) { bridgeTypeParameters.add(Object.class); } else { bridgeTypeParameters.add(typeParameter); @@ -1473,11 +2244,13 @@ private void generateBridgeMethod(PainlessClassBuilder painlessClassBuilder, Pai } MethodType bridgeMethodType = MethodType.methodType(painlessMethod.returnType, bridgeTypeParameters); - MethodWriter bridgeMethodWriter = - new MethodWriter(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, - new org.objectweb.asm.commons.Method( - painlessMethod.javaMethod.getName(), bridgeMethodType.toMethodDescriptorString()), - bridgeClassWriter, null, null); + MethodWriter bridgeMethodWriter = new MethodWriter( + Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, + new org.objectweb.asm.commons.Method(painlessMethod.javaMethod.getName(), bridgeMethodType.toMethodDescriptorString()), + bridgeClassWriter, + null, + null + ); bridgeMethodWriter.visitCode(); if (isStatic == false) { @@ -1488,13 +2261,13 @@ private void generateBridgeMethod(PainlessClassBuilder painlessClassBuilder, Pai bridgeMethodWriter.loadArg(typeParameterCount + bridgeTypeParameterOffset); Class typeParameter = javaMethod.getParameterTypes()[typeParameterCount]; - if (typeParameter == Byte.class) bridgeMethodWriter.invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_BYTE_IMPLICIT); - else if (typeParameter == Short.class) bridgeMethodWriter.invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_SHORT_IMPLICIT); + if (typeParameter == Byte.class) bridgeMethodWriter.invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_BYTE_IMPLICIT); + else if (typeParameter == Short.class) bridgeMethodWriter.invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_SHORT_IMPLICIT); else if (typeParameter == Character.class) bridgeMethodWriter.invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_CHARACTER_IMPLICIT); - else if (typeParameter == Integer.class) bridgeMethodWriter.invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_INTEGER_IMPLICIT); - else if (typeParameter == Long.class) bridgeMethodWriter.invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_LONG_IMPLICIT); - else if (typeParameter == Float.class) bridgeMethodWriter.invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_FLOAT_IMPLICIT); - else if (typeParameter == Double.class) bridgeMethodWriter.invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_DOUBLE_IMPLICIT); + else if (typeParameter == Integer.class) bridgeMethodWriter.invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_INTEGER_IMPLICIT); + else if (typeParameter == Long.class) bridgeMethodWriter.invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_LONG_IMPLICIT); + else if (typeParameter == Float.class) bridgeMethodWriter.invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_FLOAT_IMPLICIT); + else if (typeParameter == Double.class) bridgeMethodWriter.invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_DOUBLE_IMPLICIT); } bridgeMethodWriter.invokeMethodCall(painlessMethod); @@ -1513,15 +2286,26 @@ public BridgeLoader run() { Class bridgeClass = bridgeLoader.defineBridge(bridgeClassName.replace('/', '.'), bridgeClassWriter.toByteArray()); Method bridgeMethod = bridgeClass.getMethod( - painlessMethod.javaMethod.getName(), bridgeTypeParameters.toArray(new Class[0])); + painlessMethod.javaMethod.getName(), + bridgeTypeParameters.toArray(new Class[0]) + ); MethodHandle bridgeHandle = MethodHandles.publicLookup().in(bridgeClass).unreflect(bridgeClass.getMethods()[0]); - bridgePainlessMethod = new PainlessMethod(bridgeMethod, bridgeClass, - painlessMethod.returnType, bridgeTypeParameters, bridgeHandle, bridgeMethodType, Collections.emptyMap()); + bridgePainlessMethod = new PainlessMethod( + bridgeMethod, + bridgeClass, + painlessMethod.returnType, + bridgeTypeParameters, + bridgeHandle, + bridgeMethodType, + Collections.emptyMap() + ); painlessClassBuilder.runtimeMethods.put(painlessMethodKey.intern(), bridgePainlessMethod); painlessBridgeCache.put(painlessMethod, bridgePainlessMethod); } catch (Exception exception) { throw new IllegalStateException( - "internal error occurred attempting to generate a bridge method [" + bridgeClassName + "]", exception); + "internal error occurred attempting to generate a bridge method [" + bridgeClassName + "]", + exception + ); } } else { painlessClassBuilder.runtimeMethods.put(painlessMethodKey.intern(), bridgePainlessMethod); @@ -1542,19 +2326,31 @@ private void cacheRuntimeHandles(PainlessClassBuilder painlessClassBuilder) { String methodName = painlessMethod.javaMethod.getName(); int typeParametersSize = painlessMethod.typeParameters.size(); - if (typeParametersSize == 0 && methodName.startsWith("get") && methodName.length() > 3 && - Character.isUpperCase(methodName.charAt(3))) { + if (typeParametersSize == 0 + && methodName.startsWith("get") + && methodName.length() > 3 + && Character.isUpperCase(methodName.charAt(3))) { painlessClassBuilder.getterMethodHandles.putIfAbsent( - Character.toLowerCase(methodName.charAt(3)) + methodName.substring(4), bridgePainlessMethod.methodHandle); - } else if (typeParametersSize == 0 && methodName.startsWith("is") && methodName.length() > 2 && - Character.isUpperCase(methodName.charAt(2))) { - painlessClassBuilder.getterMethodHandles.putIfAbsent( - Character.toLowerCase(methodName.charAt(2)) + methodName.substring(3), bridgePainlessMethod.methodHandle); - } else if (typeParametersSize == 1 && methodName.startsWith("set") && methodName.length() > 3 && - Character.isUpperCase(methodName.charAt(3))) { - painlessClassBuilder.setterMethodHandles.putIfAbsent( - Character.toLowerCase(methodName.charAt(3)) + methodName.substring(4), bridgePainlessMethod.methodHandle); - } + Character.toLowerCase(methodName.charAt(3)) + methodName.substring(4), + bridgePainlessMethod.methodHandle + ); + } else if (typeParametersSize == 0 + && methodName.startsWith("is") + && methodName.length() > 2 + && Character.isUpperCase(methodName.charAt(2))) { + painlessClassBuilder.getterMethodHandles.putIfAbsent( + Character.toLowerCase(methodName.charAt(2)) + methodName.substring(3), + bridgePainlessMethod.methodHandle + ); + } else if (typeParametersSize == 1 + && methodName.startsWith("set") + && methodName.length() > 3 + && Character.isUpperCase(methodName.charAt(3))) { + painlessClassBuilder.setterMethodHandles.putIfAbsent( + Character.toLowerCase(methodName.charAt(3)) + methodName.substring(4), + bridgePainlessMethod.methodHandle + ); + } } for (PainlessField painlessField : painlessClassBuilder.fields.values()) { diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/lookup/PainlessLookupUtility.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/lookup/PainlessLookupUtility.java index 3b593f91a5915..391df4f227f3f 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/lookup/PainlessLookupUtility.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/lookup/PainlessLookupUtility.java @@ -101,9 +101,9 @@ public static Class canonicalTypeNameToType(String canonicalTypeName, Map typeToUnboxedType(Class type) { * where {@code true} is returned if the type is a constant type and {@code false} otherwise. */ public static boolean isConstantType(Class type) { - return type == boolean.class || - type == byte.class || - type == short.class || - type == char.class || - type == int.class || - type == long.class || - type == float.class || - type == double.class || - type == String.class; + return type == boolean.class + || type == byte.class + || type == short.class + || type == char.class + || type == int.class + || type == long.class + || type == float.class + || type == double.class + || type == String.class; } /** @@ -360,7 +360,7 @@ public static Object[] buildInjections(PainlessMethod painlessMethod, Map names = ((InjectConstantAnnotation)painlessMethod.annotations.get(InjectConstantAnnotation.class)).injects; + List names = ((InjectConstantAnnotation) painlessMethod.annotations.get(InjectConstantAnnotation.class)).injects; Object[] injections = new Object[names.size()]; for (int i = 0; i < names.size(); i++) { @@ -368,8 +368,14 @@ public static Object[] buildInjections(PainlessMethod painlessMethod, Map, Object> annotations; - public PainlessMethod(Method javaMethod, Class targetClass, Class returnType, List> typeParameters, - MethodHandle methodHandle, MethodType methodType, Map, Object> annotations) { + public PainlessMethod( + Method javaMethod, + Class targetClass, + Class returnType, + List> typeParameters, + MethodHandle methodHandle, + MethodType methodType, + Map, Object> annotations + ) { this.javaMethod = javaMethod; this.targetClass = targetClass; @@ -47,14 +54,14 @@ public boolean equals(Object object) { return false; } - PainlessMethod that = (PainlessMethod)object; + PainlessMethod that = (PainlessMethod) object; - return Objects.equals(javaMethod, that.javaMethod) && - Objects.equals(targetClass, that.targetClass) && - Objects.equals(returnType, that.returnType) && - Objects.equals(typeParameters, that.typeParameters) && - Objects.equals(methodType, that.methodType) && - Objects.equals(annotations, that.annotations); + return Objects.equals(javaMethod, that.javaMethod) + && Objects.equals(targetClass, that.targetClass) + && Objects.equals(returnType, that.returnType) + && Objects.equals(typeParameters, that.typeParameters) + && Objects.equals(methodType, that.methodType) + && Objects.equals(annotations, that.annotations); } @Override diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/node/EAssignment.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/node/EAssignment.java index 72566cdc8259b..f0e76edd469f0 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/node/EAssignment.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/node/EAssignment.java @@ -24,8 +24,14 @@ public class EAssignment extends AExpression { private final boolean postIfRead; private final Operation operation; - public EAssignment(int identifier, Location location, - AExpression leftNode, AExpression rightNode, boolean postIfRead, Operation operation) { + public EAssignment( + int identifier, + Location location, + AExpression leftNode, + AExpression rightNode, + boolean postIfRead, + Operation operation + ) { super(identifier, location); diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/node/ECall.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/node/ECall.java index 181d5495eaa93..dc9891e9e1ff0 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/node/ECall.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/node/ECall.java @@ -25,8 +25,14 @@ public class ECall extends AExpression { private final List argumentNodes; private final boolean isNullSafe; - public ECall(int identifier, Location location, - AExpression prefixNode, String methodName, List argumentNodes, boolean isNullSafe) { + public ECall( + int identifier, + Location location, + AExpression prefixNode, + String methodName, + List argumentNodes, + boolean isNullSafe + ) { super(identifier, location); diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/node/ELambda.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/node/ELambda.java index fb895a8dbde34..ad8c521314d11 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/node/ELambda.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/node/ELambda.java @@ -44,8 +44,13 @@ public class ELambda extends AExpression { private final List parameterNames; private final SBlock blockNode; - public ELambda(int identifier, Location location, - List canonicalTypeNameParameters, List parameterNames, SBlock blockNode) { + public ELambda( + int identifier, + Location location, + List canonicalTypeNameParameters, + List parameterNames, + SBlock blockNode + ) { super(identifier, location); diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/node/SBlock.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/node/SBlock.java index 4e6550ce4e389..567db6d378716 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/node/SBlock.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/node/SBlock.java @@ -39,7 +39,7 @@ public void visit(UserTreeVisitor userTreeVisitor, Scope scope) { @Override public void visitChildren(UserTreeVisitor userTreeVisitor, Scope scope) { - for (AStatement statementNode: statementNodes) { + for (AStatement statementNode : statementNodes) { statementNode.visit(userTreeVisitor, scope); } } diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/node/SFor.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/node/SFor.java index 295657f2e7d4b..df3f63d748bea 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/node/SFor.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/node/SFor.java @@ -21,8 +21,14 @@ public class SFor extends AStatement { private final AExpression afterthoughtNode; private final SBlock blockNode; - public SFor(int identifier, Location location, - ANode initializerNode, AExpression conditionNode, AExpression afterthoughtNode, SBlock blockNode) { + public SFor( + int identifier, + Location location, + ANode initializerNode, + AExpression conditionNode, + AExpression afterthoughtNode, + SBlock blockNode + ) { super(identifier, location); diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/node/SFunction.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/node/SFunction.java index 1714f82d0b7ef..98168ea8d3dec 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/node/SFunction.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/node/SFunction.java @@ -30,10 +30,19 @@ public class SFunction extends ANode { private final boolean isSynthetic; private final boolean isAutoReturnEnabled; - public SFunction(int identifier, Location location, - String returnCanonicalTypeName, String name, List canonicalTypeNameParameters, List parameterNames, - SBlock blockNode, - boolean isInternal, boolean isStatic, boolean isSynthetic, boolean isAutoReturnEnabled) { + public SFunction( + int identifier, + Location location, + String returnCanonicalTypeName, + String name, + List canonicalTypeNameParameters, + List parameterNames, + SBlock blockNode, + boolean isInternal, + boolean isStatic, + boolean isSynthetic, + boolean isAutoReturnEnabled + ) { super(identifier, location); diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/DefaultConstantFoldingOptimizationPhase.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/DefaultConstantFoldingOptimizationPhase.java index 23213a917ccfa..6dc1a980be237 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/DefaultConstantFoldingOptimizationPhase.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/DefaultConstantFoldingOptimizationPhase.java @@ -179,38 +179,60 @@ public void visitUnaryMath(UnaryMathNode irUnaryMathNode, Consumer type = irUnaryMathNode.getDecorationValue(IRDExpressionType.class); if (operation == Operation.SUB) { if (type == int.class) { - irConstantNode.attachDecoration(new IRDConstant(-(int)constantValue)); + irConstantNode.attachDecoration(new IRDConstant(-(int) constantValue)); } else if (type == long.class) { - irConstantNode.attachDecoration(new IRDConstant(-(long)constantValue)); + irConstantNode.attachDecoration(new IRDConstant(-(long) constantValue)); } else if (type == float.class) { - irConstantNode.attachDecoration(new IRDConstant(-(float)constantValue)); + irConstantNode.attachDecoration(new IRDConstant(-(float) constantValue)); } else if (type == double.class) { - irConstantNode.attachDecoration(new IRDConstant(-(double)constantValue)); + irConstantNode.attachDecoration(new IRDConstant(-(double) constantValue)); } else { - throw irUnaryMathNode.getLocation().createError(new IllegalStateException("constant folding error: " + - "unexpected type [" + PainlessLookupUtility.typeToCanonicalTypeName(type) + "] for " + - "unary operation [" + operation.symbol + "] on " + - "constant [" + irConstantNode.getDecorationString(IRDConstant.class) + "]")); + throw irUnaryMathNode.getLocation() + .createError( + new IllegalStateException( + "constant folding error: " + + "unexpected type [" + + PainlessLookupUtility.typeToCanonicalTypeName(type) + + "] for " + + "unary operation [" + + operation.symbol + + "] on " + + "constant [" + + irConstantNode.getDecorationString(IRDConstant.class) + + "]" + ) + ); } scope.accept(irConstantNode); } else if (operation == Operation.BWNOT) { if (type == int.class) { - irConstantNode.attachDecoration(new IRDConstant(~(int)constantValue)); + irConstantNode.attachDecoration(new IRDConstant(~(int) constantValue)); } else if (type == long.class) { - irConstantNode.attachDecoration(new IRDConstant(~(long)constantValue)); + irConstantNode.attachDecoration(new IRDConstant(~(long) constantValue)); } else { - throw irUnaryMathNode.getLocation().createError(new IllegalStateException("constant folding error: " + - "unexpected type [" + PainlessLookupUtility.typeToCanonicalTypeName(type) + "] for " + - "unary operation [" + operation.symbol + "] on " + - "constant [" + irConstantNode.getDecorationString(IRDConstant.class) + "]")); + throw irUnaryMathNode.getLocation() + .createError( + new IllegalStateException( + "constant folding error: " + + "unexpected type [" + + PainlessLookupUtility.typeToCanonicalTypeName(type) + + "] for " + + "unary operation [" + + operation.symbol + + "] on " + + "constant [" + + irConstantNode.getDecorationString(IRDConstant.class) + + "]" + ) + ); } scope.accept(irConstantNode); @@ -218,10 +240,21 @@ public void visitUnaryMath(UnaryMathNode irUnaryMathNode, Consumer> (int)rightConstantValue)); + irLeftConstantNode.attachDecoration(new IRDConstant((int) leftConstantValue >> (int) rightConstantValue)); } else if (type == long.class) { - irLeftConstantNode.attachDecoration(new IRDConstant((long)leftConstantValue >> (int)rightConstantValue)); + irLeftConstantNode.attachDecoration(new IRDConstant((long) leftConstantValue >> (int) rightConstantValue)); } else { - throw irBinaryMathNode.getLocation().createError(new IllegalStateException("constant folding error: " + - "unexpected type [" + PainlessLookupUtility.typeToCanonicalTypeName(type) + "] for " + - "binary operation [" + operation.symbol + "] on " + - "constants [" + irLeftConstantNode.getDecorationString(IRDConstant.class) + "] " + - "and [" + irRightConstantNode.getDecorationString(IRDConstant.class) + "]")); + throw irBinaryMathNode.getLocation() + .createError( + new IllegalStateException( + "constant folding error: " + + "unexpected type [" + + PainlessLookupUtility.typeToCanonicalTypeName(type) + + "] for " + + "binary operation [" + + operation.symbol + + "] on " + + "constants [" + + irLeftConstantNode.getDecorationString(IRDConstant.class) + + "] " + + "and [" + + irRightConstantNode.getDecorationString(IRDConstant.class) + + "]" + ) + ); } scope.accept(irLeftConstantNode); } else if (operation == Operation.USH) { if (type == int.class) { - irLeftConstantNode.attachDecoration(new IRDConstant((int)leftConstantValue >>> (int)rightConstantValue)); + irLeftConstantNode.attachDecoration(new IRDConstant((int) leftConstantValue >>> (int) rightConstantValue)); } else if (type == long.class) { - irLeftConstantNode.attachDecoration(new IRDConstant((long)leftConstantValue >>> (int)rightConstantValue)); + irLeftConstantNode.attachDecoration(new IRDConstant((long) leftConstantValue >>> (int) rightConstantValue)); } else { - throw irBinaryMathNode.getLocation().createError(new IllegalStateException("constant folding error: " + - "unexpected type [" + PainlessLookupUtility.typeToCanonicalTypeName(type) + "] for " + - "binary operation [" + operation.symbol + "] on " + - "constants [" + irLeftConstantNode.getDecorationString(IRDConstant.class) + "] and " + - "[" + irRightConstantNode.getDecorationString(IRDConstant.class) + "]")); + throw irBinaryMathNode.getLocation() + .createError( + new IllegalStateException( + "constant folding error: " + + "unexpected type [" + + PainlessLookupUtility.typeToCanonicalTypeName(type) + + "] for " + + "binary operation [" + + operation.symbol + + "] on " + + "constants [" + + irLeftConstantNode.getDecorationString(IRDConstant.class) + + "] and " + + "[" + + irRightConstantNode.getDecorationString(IRDConstant.class) + + "]" + ) + ); } scope.accept(irLeftConstantNode); } else if (operation == Operation.BWAND) { if (type == int.class) { - irLeftConstantNode.attachDecoration(new IRDConstant((int)leftConstantValue & (int)rightConstantValue)); + irLeftConstantNode.attachDecoration(new IRDConstant((int) leftConstantValue & (int) rightConstantValue)); } else if (type == long.class) { - irLeftConstantNode.attachDecoration(new IRDConstant((long)leftConstantValue & (long)rightConstantValue)); + irLeftConstantNode.attachDecoration(new IRDConstant((long) leftConstantValue & (long) rightConstantValue)); } else { - throw irBinaryMathNode.getLocation().createError(new IllegalStateException("constant folding error: " + - "unexpected type [" + PainlessLookupUtility.typeToCanonicalTypeName(type) + "] for " + - "binary operation [" + operation.symbol + "] on " + - "constants [" + irLeftConstantNode.getDecorationString(IRDConstant.class) + "] " + - "and [" + irRightConstantNode.getDecorationString(IRDConstant.class) + "]")); + throw irBinaryMathNode.getLocation() + .createError( + new IllegalStateException( + "constant folding error: " + + "unexpected type [" + + PainlessLookupUtility.typeToCanonicalTypeName(type) + + "] for " + + "binary operation [" + + operation.symbol + + "] on " + + "constants [" + + irLeftConstantNode.getDecorationString(IRDConstant.class) + + "] " + + "and [" + + irRightConstantNode.getDecorationString(IRDConstant.class) + + "]" + ) + ); } scope.accept(irLeftConstantNode); } else if (operation == Operation.XOR) { if (type == boolean.class) { - irLeftConstantNode.attachDecoration(new IRDConstant((boolean)leftConstantValue ^ (boolean)rightConstantValue)); + irLeftConstantNode.attachDecoration(new IRDConstant((boolean) leftConstantValue ^ (boolean) rightConstantValue)); } else if (type == int.class) { - irLeftConstantNode.attachDecoration(new IRDConstant((int)leftConstantValue ^ (int)rightConstantValue)); + irLeftConstantNode.attachDecoration(new IRDConstant((int) leftConstantValue ^ (int) rightConstantValue)); } else if (type == long.class) { - irLeftConstantNode.attachDecoration(new IRDConstant((long)leftConstantValue ^ (long)rightConstantValue)); + irLeftConstantNode.attachDecoration(new IRDConstant((long) leftConstantValue ^ (long) rightConstantValue)); } else { - throw irBinaryMathNode.getLocation().createError(new IllegalStateException("constant folding error: " + - "unexpected type [" + PainlessLookupUtility.typeToCanonicalTypeName(type) + "] for " + - "binary operation [" + operation.symbol + "] on " + - "constants [" + irLeftConstantNode.getDecorationString(IRDConstant.class) + "] and " + - "[" + irRightConstantNode.getDecorationString(IRDConstant.class) + "]")); + throw irBinaryMathNode.getLocation() + .createError( + new IllegalStateException( + "constant folding error: " + + "unexpected type [" + + PainlessLookupUtility.typeToCanonicalTypeName(type) + + "] for " + + "binary operation [" + + operation.symbol + + "] on " + + "constants [" + + irLeftConstantNode.getDecorationString(IRDConstant.class) + + "] and " + + "[" + + irRightConstantNode.getDecorationString(IRDConstant.class) + + "]" + ) + ); } scope.accept(irLeftConstantNode); } else if (operation == Operation.BWOR) { if (type == int.class) { - irLeftConstantNode.attachDecoration(new IRDConstant((int)leftConstantValue | (int)rightConstantValue)); + irLeftConstantNode.attachDecoration(new IRDConstant((int) leftConstantValue | (int) rightConstantValue)); } else if (type == long.class) { - irLeftConstantNode.attachDecoration(new IRDConstant((long)leftConstantValue | (long)rightConstantValue)); + irLeftConstantNode.attachDecoration(new IRDConstant((long) leftConstantValue | (long) rightConstantValue)); } else { - throw irBinaryMathNode.getLocation().createError(new IllegalStateException("constant folding error: " + - "unexpected type [" + PainlessLookupUtility.typeToCanonicalTypeName(type) + "] for " + - "binary operation [" + operation.symbol + "] on " + - "constants [" + irLeftConstantNode.getDecorationString(IRDConstant.class) + "] " + - "and [" + irRightConstantNode.getDecorationString(IRDConstant.class) + "]")); + throw irBinaryMathNode.getLocation() + .createError( + new IllegalStateException( + "constant folding error: " + + "unexpected type [" + + PainlessLookupUtility.typeToCanonicalTypeName(type) + + "] for " + + "binary operation [" + + operation.symbol + + "] on " + + "constants [" + + irLeftConstantNode.getDecorationString(IRDConstant.class) + + "] " + + "and [" + + irRightConstantNode.getDecorationString(IRDConstant.class) + + "]" + ) + ); } scope.accept(irLeftConstantNode); @@ -446,18 +622,21 @@ public void visitStringConcatenation(StringConcatenationNode irStringConcatenati irRightNode.visit(this, (e) -> irStringConcatenationNode.getArgumentNodes().set(j + 1, e)); if (irLeftNode instanceof ConstantNode && irRightNode instanceof ConstantNode) { - ConstantNode irConstantNode = (ConstantNode)irLeftNode; - irConstantNode.attachDecoration(new IRDConstant( - "" + irConstantNode.getDecorationValue(IRDConstant.class) + irRightNode.getDecorationValue(IRDConstant.class))); + ConstantNode irConstantNode = (ConstantNode) irLeftNode; + irConstantNode.attachDecoration( + new IRDConstant( + "" + irConstantNode.getDecorationValue(IRDConstant.class) + irRightNode.getDecorationValue(IRDConstant.class) + ) + ); irConstantNode.attachDecoration(new IRDExpressionType(String.class)); irStringConcatenationNode.getArgumentNodes().remove(i + 1); } else if (irLeftNode instanceof NullNode && irRightNode instanceof ConstantNode) { - ConstantNode irConstantNode = (ConstantNode)irRightNode; + ConstantNode irConstantNode = (ConstantNode) irRightNode; irConstantNode.attachDecoration(new IRDConstant("" + null + irRightNode.getDecorationValue(IRDConstant.class))); irConstantNode.attachDecoration(new IRDExpressionType(String.class)); irStringConcatenationNode.getArgumentNodes().remove(i); } else if (irLeftNode instanceof ConstantNode && irRightNode instanceof NullNode) { - ConstantNode irConstantNode = (ConstantNode)irLeftNode; + ConstantNode irConstantNode = (ConstantNode) irLeftNode; irConstantNode.attachDecoration(new IRDConstant("" + irLeftNode.getDecorationValue(IRDConstant.class) + null)); irConstantNode.attachDecoration(new IRDExpressionType(String.class)); irStringConcatenationNode.getArgumentNodes().remove(i + 1); @@ -487,36 +666,68 @@ public void visitBoolean(BooleanNode irBooleanNode, Consumer sco irBooleanNode.getRightNode().visit(this, irBooleanNode::setRightNode); if (irBooleanNode.getLeftNode() instanceof ConstantNode && irBooleanNode.getRightNode() instanceof ConstantNode) { - ConstantNode irLeftConstantNode = (ConstantNode)irBooleanNode.getLeftNode(); - ConstantNode irRightConstantNode = (ConstantNode)irBooleanNode.getRightNode(); + ConstantNode irLeftConstantNode = (ConstantNode) irBooleanNode.getLeftNode(); + ConstantNode irRightConstantNode = (ConstantNode) irBooleanNode.getRightNode(); Operation operation = irBooleanNode.getDecorationValue(IRDOperation.class); Class type = irBooleanNode.getDecorationValue(IRDExpressionType.class); if (operation == Operation.AND) { if (type == boolean.class) { - irLeftConstantNode.attachDecoration(new IRDConstant( - (boolean)irLeftConstantNode.getDecorationValue(IRDConstant.class) && - (boolean)irRightConstantNode.getDecorationValue(IRDConstant.class))); + irLeftConstantNode.attachDecoration( + new IRDConstant( + (boolean) irLeftConstantNode.getDecorationValue(IRDConstant.class) + && (boolean) irRightConstantNode.getDecorationValue(IRDConstant.class) + ) + ); } else { - throw irBooleanNode.getLocation().createError(new IllegalStateException("constant folding error: " + - "unexpected type [" + PainlessLookupUtility.typeToCanonicalTypeName(type) + "] for " + - "binary operation [" + operation.symbol + "] on " + - "constants [" + irLeftConstantNode.getDecorationString(IRDConstant.class) + "] " + - "and [" + irRightConstantNode.getDecorationString(IRDConstant.class) + "]")); + throw irBooleanNode.getLocation() + .createError( + new IllegalStateException( + "constant folding error: " + + "unexpected type [" + + PainlessLookupUtility.typeToCanonicalTypeName(type) + + "] for " + + "binary operation [" + + operation.symbol + + "] on " + + "constants [" + + irLeftConstantNode.getDecorationString(IRDConstant.class) + + "] " + + "and [" + + irRightConstantNode.getDecorationString(IRDConstant.class) + + "]" + ) + ); } scope.accept(irLeftConstantNode); } else if (operation == Operation.OR) { if (type == boolean.class) { - irLeftConstantNode.attachDecoration(new IRDConstant( - (boolean)irLeftConstantNode.getDecorationValue(IRDConstant.class) || - (boolean)irRightConstantNode.getDecorationValue(IRDConstant.class))); + irLeftConstantNode.attachDecoration( + new IRDConstant( + (boolean) irLeftConstantNode.getDecorationValue(IRDConstant.class) + || (boolean) irRightConstantNode.getDecorationValue(IRDConstant.class) + ) + ); } else { - throw irBooleanNode.getLocation().createError(new IllegalStateException("constant folding error: " + - "unexpected type [" + PainlessLookupUtility.typeToCanonicalTypeName(type) + "] for " + - "boolean operation [" + operation.symbol + "] on " + - "constants [" + irLeftConstantNode.getDecorationString(IRDConstant.class) + "] " + - "and [" + irRightConstantNode.getDecorationString(IRDConstant.class) + "]")); + throw irBooleanNode.getLocation() + .createError( + new IllegalStateException( + "constant folding error: " + + "unexpected type [" + + PainlessLookupUtility.typeToCanonicalTypeName(type) + + "] for " + + "boolean operation [" + + operation.symbol + + "] on " + + "constants [" + + irLeftConstantNode.getDecorationString(IRDConstant.class) + + "] " + + "and [" + + irRightConstantNode.getDecorationString(IRDConstant.class) + + "]" + ) + ); } scope.accept(irLeftConstantNode); @@ -530,12 +741,14 @@ public void visitComparison(ComparisonNode irComparisonNode, Consumer (int)rightConstantValue)); + irLeftConstantNode.attachDecoration(new IRDConstant((int) leftConstantValue > (int) rightConstantValue)); } else if (type == long.class) { - irLeftConstantNode.attachDecoration(new IRDConstant((long)leftConstantValue > (long)rightConstantValue)); + irLeftConstantNode.attachDecoration(new IRDConstant((long) leftConstantValue > (long) rightConstantValue)); } else if (type == float.class) { - irLeftConstantNode.attachDecoration(new IRDConstant((float)leftConstantValue > (float)rightConstantValue)); + irLeftConstantNode.attachDecoration(new IRDConstant((float) leftConstantValue > (float) rightConstantValue)); } else if (type == double.class) { - irLeftConstantNode.attachDecoration(new IRDConstant((double)leftConstantValue > (double)rightConstantValue)); + irLeftConstantNode.attachDecoration(new IRDConstant((double) leftConstantValue > (double) rightConstantValue)); } else { - throw irComparisonNode.getLocation().createError(new IllegalStateException("constant folding error: " + - "unexpected type [" + PainlessLookupUtility.typeToCanonicalTypeName(type) + "] for " + - "comparison operation [" + operation.symbol + "] on " + - "constants [" + irLeftConstantNode.getDecorationString(IRDConstant.class) + "] " + - "and [" + irRightConstantNode.getDecorationString(IRDConstant.class) + "]")); + throw irComparisonNode.getLocation() + .createError( + new IllegalStateException( + "constant folding error: " + + "unexpected type [" + + PainlessLookupUtility.typeToCanonicalTypeName(type) + + "] for " + + "comparison operation [" + + operation.symbol + + "] on " + + "constants [" + + irLeftConstantNode.getDecorationString(IRDConstant.class) + + "] " + + "and [" + + irRightConstantNode.getDecorationString(IRDConstant.class) + + "]" + ) + ); } irLeftConstantNode.attachDecoration(new IRDExpressionType(boolean.class)); scope.accept(irLeftConstantNode); } else if (operation == Operation.GTE) { if (type == int.class) { - irLeftConstantNode.attachDecoration(new IRDConstant((int)leftConstantValue >= (int)rightConstantValue)); + irLeftConstantNode.attachDecoration(new IRDConstant((int) leftConstantValue >= (int) rightConstantValue)); } else if (type == long.class) { - irLeftConstantNode.attachDecoration(new IRDConstant((long)leftConstantValue >= (long)rightConstantValue)); + irLeftConstantNode.attachDecoration(new IRDConstant((long) leftConstantValue >= (long) rightConstantValue)); } else if (type == float.class) { - irLeftConstantNode.attachDecoration(new IRDConstant((float)leftConstantValue >= (float)rightConstantValue)); + irLeftConstantNode.attachDecoration(new IRDConstant((float) leftConstantValue >= (float) rightConstantValue)); } else if (type == double.class) { - irLeftConstantNode.attachDecoration(new IRDConstant((double)leftConstantValue >= (double)rightConstantValue)); + irLeftConstantNode.attachDecoration(new IRDConstant((double) leftConstantValue >= (double) rightConstantValue)); } else { - throw irComparisonNode.getLocation().createError(new IllegalStateException("constant folding error: " + - "unexpected type [" + PainlessLookupUtility.typeToCanonicalTypeName(type) + "] for " + - "comparison operation [" + operation.symbol + "] on " + - "constants [" + irLeftConstantNode.getDecorationString(IRDConstant.class) + "] " + - "and [" + irRightConstantNode.getDecorationString(IRDConstant.class) + "]")); + throw irComparisonNode.getLocation() + .createError( + new IllegalStateException( + "constant folding error: " + + "unexpected type [" + + PainlessLookupUtility.typeToCanonicalTypeName(type) + + "] for " + + "comparison operation [" + + operation.symbol + + "] on " + + "constants [" + + irLeftConstantNode.getDecorationString(IRDConstant.class) + + "] " + + "and [" + + irRightConstantNode.getDecorationString(IRDConstant.class) + + "]" + ) + ); } irLeftConstantNode.attachDecoration(new IRDExpressionType(boolean.class)); scope.accept(irLeftConstantNode); } else if (operation == Operation.LT) { if (type == int.class) { - irLeftConstantNode.attachDecoration(new IRDConstant((int)leftConstantValue < (int)rightConstantValue)); + irLeftConstantNode.attachDecoration(new IRDConstant((int) leftConstantValue < (int) rightConstantValue)); } else if (type == long.class) { - irLeftConstantNode.attachDecoration(new IRDConstant((long)leftConstantValue < (long)rightConstantValue)); + irLeftConstantNode.attachDecoration(new IRDConstant((long) leftConstantValue < (long) rightConstantValue)); } else if (type == float.class) { - irLeftConstantNode.attachDecoration(new IRDConstant((float)leftConstantValue < (float)rightConstantValue)); + irLeftConstantNode.attachDecoration(new IRDConstant((float) leftConstantValue < (float) rightConstantValue)); } else if (type == double.class) { - irLeftConstantNode.attachDecoration(new IRDConstant((double)leftConstantValue < (double)rightConstantValue)); + irLeftConstantNode.attachDecoration(new IRDConstant((double) leftConstantValue < (double) rightConstantValue)); } else { - throw irComparisonNode.getLocation().createError(new IllegalStateException("constant folding error: " + - "unexpected type [" + PainlessLookupUtility.typeToCanonicalTypeName(type) + "] for " + - "comparison operation [" + operation.symbol + "] on " + - "constants [" + irLeftConstantNode.getDecorationString(IRDConstant.class) + "] " + - "and [" + irRightConstantNode.getDecorationString(IRDConstant.class) + "]")); + throw irComparisonNode.getLocation() + .createError( + new IllegalStateException( + "constant folding error: " + + "unexpected type [" + + PainlessLookupUtility.typeToCanonicalTypeName(type) + + "] for " + + "comparison operation [" + + operation.symbol + + "] on " + + "constants [" + + irLeftConstantNode.getDecorationString(IRDConstant.class) + + "] " + + "and [" + + irRightConstantNode.getDecorationString(IRDConstant.class) + + "]" + ) + ); } irLeftConstantNode.attachDecoration(new IRDExpressionType(boolean.class)); scope.accept(irLeftConstantNode); } else if (operation == Operation.LTE) { if (type == int.class) { - irLeftConstantNode.attachDecoration(new IRDConstant((int)leftConstantValue <= (int)rightConstantValue)); + irLeftConstantNode.attachDecoration(new IRDConstant((int) leftConstantValue <= (int) rightConstantValue)); } else if (type == long.class) { - irLeftConstantNode.attachDecoration(new IRDConstant((long)leftConstantValue <= (long)rightConstantValue)); + irLeftConstantNode.attachDecoration(new IRDConstant((long) leftConstantValue <= (long) rightConstantValue)); } else if (type == float.class) { - irLeftConstantNode.attachDecoration(new IRDConstant((float)leftConstantValue <= (float)rightConstantValue)); + irLeftConstantNode.attachDecoration(new IRDConstant((float) leftConstantValue <= (float) rightConstantValue)); } else if (type == double.class) { - irLeftConstantNode.attachDecoration(new IRDConstant((double)leftConstantValue <= (double)rightConstantValue)); + irLeftConstantNode.attachDecoration(new IRDConstant((double) leftConstantValue <= (double) rightConstantValue)); } else { - throw irComparisonNode.getLocation().createError(new IllegalStateException("constant folding error: " + - "unexpected type [" + PainlessLookupUtility.typeToCanonicalTypeName(type) + "] for " + - "comparison operation [" + operation.symbol + "] on " + - "constants [" + irLeftConstantNode.getDecorationString(IRDConstant.class) + "] " + - "and [" + irRightConstantNode.getDecorationString(IRDConstant.class) + "]")); + throw irComparisonNode.getLocation() + .createError( + new IllegalStateException( + "constant folding error: " + + "unexpected type [" + + PainlessLookupUtility.typeToCanonicalTypeName(type) + + "] for " + + "comparison operation [" + + operation.symbol + + "] on " + + "constants [" + + irLeftConstantNode.getDecorationString(IRDConstant.class) + + "] " + + "and [" + + irRightConstantNode.getDecorationString(IRDConstant.class) + + "]" + ) + ); } irLeftConstantNode.attachDecoration(new IRDExpressionType(boolean.class)); @@ -682,9 +946,9 @@ public void visitComparison(ComparisonNode irComparisonNode, Consumer scope) { irCastNode.getChildNode().visit(this, irCastNode::setChildNode); - if (irCastNode.getChildNode() instanceof ConstantNode && - PainlessLookupUtility.isConstantType(irCastNode.getDecorationValue(IRDExpressionType.class))) { - ConstantNode irConstantNode = (ConstantNode)irCastNode.getChildNode(); + if (irCastNode.getChildNode() instanceof ConstantNode + && PainlessLookupUtility.isConstantType(irCastNode.getDecorationValue(IRDExpressionType.class))) { + ConstantNode irConstantNode = (ConstantNode) irCastNode.getChildNode(); Object constantValue = irConstantNode.getDecorationValue(IRDConstant.class); constantValue = AnalyzerCaster.constCast(irCastNode.getLocation(), constantValue, irCastNode.getDecorationValue(IRDCast.class)); irConstantNode.attachDecoration(new IRDConstant(constantValue)); diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/DefaultIRTreeToASMBytesPhase.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/DefaultIRTreeToASMBytesPhase.java index d265a514a453c..c9c242f7ba1fe 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/DefaultIRTreeToASMBytesPhase.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/DefaultIRTreeToASMBytesPhase.java @@ -202,8 +202,16 @@ public void visitClass(ClassNode irClassNode, WriteScope writeScope) { String className = CLASS_TYPE.getInternalName(); String[] classInterfaces = new String[] { interfaceBase }; - ClassWriter classWriter = new ClassWriter(scriptScope.getCompilerSettings(), statements, debugStream, - scriptClassInfo.getBaseClass(), classFrames, classAccess, className, classInterfaces); + ClassWriter classWriter = new ClassWriter( + scriptScope.getCompilerSettings(), + statements, + debugStream, + scriptClassInfo.getBaseClass(), + classFrames, + classAccess, + className, + classInterfaces + ); ClassVisitor classVisitor = classWriter.getClassVisitor(); classVisitor.visitSource(Location.computeSourceName(scriptScope.getScriptName()), null); writeScope = writeScope.newClassScope(classWriter); @@ -213,8 +221,11 @@ public void visitClass(ClassNode irClassNode, WriteScope writeScope) { if (scriptClassInfo.getBaseClass().getConstructors().length == 0) { init = new Method("", MethodType.methodType(void.class).toMethodDescriptorString()); } else { - init = new Method("", MethodType.methodType(void.class, - scriptClassInfo.getBaseClass().getConstructors()[0].getParameterTypes()).toMethodDescriptorString()); + init = new Method( + "", + MethodType.methodType(void.class, scriptClassInfo.getBaseClass().getConstructors()[0].getParameterTypes()) + .toMethodDescriptorString() + ); } // Write the constructor: @@ -230,8 +241,9 @@ public void visitClass(ClassNode irClassNode, WriteScope writeScope) { if (irClinitBlockNode.getStatementsNodes().isEmpty() == false) { MethodWriter methodWriter = classWriter.newMethodWriter( - Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, - new Method("", Type.getType(void.class), new Type[0])); + Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, + new Method("", Type.getType(void.class), new Type[0]) + ); visit(irClinitBlockNode, writeScope.newMethodScope(methodWriter).newBlockScope()); methodWriter.returnValue(); methodWriter.endMethod(); @@ -445,7 +457,7 @@ public void visitForLoop(ForLoopNode irForLoopNode, WriteScope writeScope) { if (irInitializerNode instanceof DeclarationBlockNode) { visit(irInitializerNode, writeScope); } else if (irInitializerNode instanceof ExpressionNode) { - ExpressionNode irExpressionNode = (ExpressionNode)irInitializerNode; + ExpressionNode irExpressionNode = (ExpressionNode) irInitializerNode; visit(irExpressionNode, writeScope); methodWriter.writePop(MethodWriter.getType(irExpressionNode.getDecorationValue(IRDExpressionType.class)).getSize()); @@ -495,14 +507,17 @@ public void visitForEachSubArrayLoop(ForEachSubArrayNode irForEachSubArrayNode, methodWriter.writeStatementOffset(irForEachSubArrayNode.getLocation()); Variable variable = writeScope.defineVariable( - irForEachSubArrayNode.getDecorationValue(IRDVariableType.class), - irForEachSubArrayNode.getDecorationValue(IRDVariableName.class)); + irForEachSubArrayNode.getDecorationValue(IRDVariableType.class), + irForEachSubArrayNode.getDecorationValue(IRDVariableName.class) + ); Variable array = writeScope.defineInternalVariable( - irForEachSubArrayNode.getDecorationValue(IRDArrayType.class), - irForEachSubArrayNode.getDecorationValue(IRDArrayName.class)); + irForEachSubArrayNode.getDecorationValue(IRDArrayType.class), + irForEachSubArrayNode.getDecorationValue(IRDArrayName.class) + ); Variable index = writeScope.defineInternalVariable( - irForEachSubArrayNode.getDecorationValue(IRDIndexType.class), - irForEachSubArrayNode.getDecorationValue(IRDIndexName.class)); + irForEachSubArrayNode.getDecorationValue(IRDIndexType.class), + irForEachSubArrayNode.getDecorationValue(IRDIndexName.class) + ); visit(irForEachSubArrayNode.getConditionNode(), writeScope); methodWriter.visitVarInsn(array.getAsmType().getOpcode(Opcodes.ISTORE), array.getSlot()); @@ -538,11 +553,13 @@ public void visitForEachSubIterableLoop(ForEachSubIterableNode irForEachSubItera methodWriter.writeStatementOffset(irForEachSubIterableNode.getLocation()); Variable variable = writeScope.defineVariable( - irForEachSubIterableNode.getDecorationValue(IRDVariableType.class), - irForEachSubIterableNode.getDecorationValue(IRDVariableName.class)); + irForEachSubIterableNode.getDecorationValue(IRDVariableType.class), + irForEachSubIterableNode.getDecorationValue(IRDVariableName.class) + ); Variable iterator = writeScope.defineInternalVariable( - irForEachSubIterableNode.getDecorationValue(IRDIterableType.class), - irForEachSubIterableNode.getDecorationValue(IRDIterableName.class)); + irForEachSubIterableNode.getDecorationValue(IRDIterableType.class), + irForEachSubIterableNode.getDecorationValue(IRDIterableName.class) + ); visit(irForEachSubIterableNode.getConditionNode(), writeScope); @@ -595,8 +612,12 @@ public void visitDeclaration(DeclarationNode irDeclarationNode, WriteScope write if (irDeclarationNode.getExpressionNode() == null) { Class sort = variable.getType(); - if (sort == void.class || sort == boolean.class || sort == byte.class || - sort == short.class || sort == char.class || sort == int.class) { + if (sort == void.class + || sort == boolean.class + || sort == byte.class + || sort == short.class + || sort == char.class + || sort == int.class) { methodWriter.push(0); } else if (sort == long.class) { methodWriter.push(0L); @@ -691,7 +712,11 @@ public void visitCatch(CatchNode irCatchNode, WriteScope writeScope) { } methodWriter.visitTryCatchBlock( - writeScope.getTryBeginLabel(), writeScope.getTryEndLabel(), jump, variable.getAsmType().getInternalName()); + writeScope.getTryBeginLabel(), + writeScope.getTryEndLabel(), + jump, + variable.getAsmType().getInternalName() + ); if (writeScope.getCatchesEndLabel() != null && (irBlockNode == null || irBlockNode.hasCondition(IRCAllEscape.class) == false)) { methodWriter.goTo(writeScope.getCatchesEndLabel()); @@ -763,8 +788,14 @@ public void visitUnaryMath(UnaryMathNode irUnaryMathNode, WriteScope writeScope) } else if (unaryType == long.class) { methodWriter.push(-1L); } else { - throw new IllegalStateException("unexpected unary math operation [" + operation + "] " + - "for type [" + irUnaryMathNode.getDecorationString(IRDExpressionType.class) + "]"); + throw new IllegalStateException( + "unexpected unary math operation [" + + operation + + "] " + + "for type [" + + irUnaryMathNode.getDecorationString(IRDExpressionType.class) + + "]" + ); } methodWriter.math(MethodWriter.XOR, actualType); @@ -782,8 +813,14 @@ public void visitUnaryMath(UnaryMathNode irUnaryMathNode, WriteScope writeScope) methodWriter.invokeDefCall("plus", descriptor, DefBootstrap.UNARY_OPERATOR, flags); } } else { - throw new IllegalStateException("unexpected unary math operation [" + operation + "] " + - "for type [" + irUnaryMathNode.getDecorationString(IRDExpressionType.class) + "]"); + throw new IllegalStateException( + "unexpected unary math operation [" + + operation + + "] " + + "for type [" + + irUnaryMathNode.getDecorationString(IRDExpressionType.class) + + "]" + ); } } } @@ -808,8 +845,14 @@ public void visitBinaryMath(BinaryMathNode irBinaryMathNode, WriteScope writeSco } else if (operation == Operation.MATCH) { methodWriter.invokeVirtual(Type.getType(Matcher.class), WriterConstants.MATCHER_MATCHES); } else { - throw new IllegalStateException("unexpected binary math operation [" + operation + "] " + - "for type [" + irBinaryMathNode.getDecorationString(IRDExpressionType.class) + "]"); + throw new IllegalStateException( + "unexpected binary math operation [" + + operation + + "] " + + "for type [" + + irBinaryMathNode.getDecorationString(IRDExpressionType.class) + + "]" + ); } } else { visit(irLeftNode, writeScope); @@ -817,16 +860,17 @@ public void visitBinaryMath(BinaryMathNode irBinaryMathNode, WriteScope writeSco Class expressionType = irBinaryMathNode.getDecorationValue(IRDExpressionType.class); - if (irBinaryMathNode.getDecorationValue(IRDBinaryType.class) == def.class || - (irBinaryMathNode.getDecoration(IRDShiftType.class) != null && - irBinaryMathNode.getDecorationValue(IRDShiftType.class) == def.class)) { + if (irBinaryMathNode.getDecorationValue(IRDBinaryType.class) == def.class + || (irBinaryMathNode.getDecoration(IRDShiftType.class) != null + && irBinaryMathNode.getDecorationValue(IRDShiftType.class) == def.class)) { methodWriter.writeDynamicBinaryInstruction( - irBinaryMathNode.getLocation(), - expressionType, - irLeftNode.getDecorationValue(IRDExpressionType.class), - irRightNode.getDecorationValue(IRDExpressionType.class), - operation, - irBinaryMathNode.getDecorationValueOrDefault(IRDFlags.class, 0)); + irBinaryMathNode.getLocation(), + expressionType, + irLeftNode.getDecorationValue(IRDExpressionType.class), + irRightNode.getDecorationValue(IRDExpressionType.class), + operation, + irBinaryMathNode.getDecorationValueOrDefault(IRDFlags.class, 0) + ); } else { methodWriter.writeBinaryInstruction(irBinaryMathNode.getLocation(), expressionType, operation); } @@ -887,8 +931,14 @@ public void visitBoolean(BooleanNode irBooleanNode, WriteScope writeScope) { methodWriter.push(false); methodWriter.mark(end); } else { - throw new IllegalStateException("unexpected boolean operation [" + operation + "] " + - "for type [" + irBooleanNode.getDecorationString(IRDExpressionType.class) + "]"); + throw new IllegalStateException( + "unexpected boolean operation [" + + operation + + "] " + + "for type [" + + irBooleanNode.getDecorationString(IRDExpressionType.class) + + "]" + ); } } @@ -912,9 +962,9 @@ public void visitComparison(ComparisonNode irComparisonNode, WriteScope writeSco boolean eq = (operation == Operation.EQ || operation == Operation.EQR); boolean ne = (operation == Operation.NE || operation == Operation.NER); - boolean lt = operation == Operation.LT; + boolean lt = operation == Operation.LT; boolean lte = operation == Operation.LTE; - boolean gt = operation == Operation.GT; + boolean gt = operation == Operation.GT; boolean gte = operation == Operation.GTE; boolean writejump = true; @@ -922,94 +972,127 @@ public void visitComparison(ComparisonNode irComparisonNode, WriteScope writeSco Class comparisonType = irComparisonNode.getDecorationValue(IRDComparisonType.class); Type type = MethodWriter.getType(comparisonType); - if (comparisonType == void.class || comparisonType == byte.class - || comparisonType == short.class || comparisonType == char.class) { - throw new IllegalStateException("unexpected comparison operation [" + operation + "] " + - "for type [" + irComparisonNode.getDecorationString(IRDExpressionType.class) + "]"); + if (comparisonType == void.class || comparisonType == byte.class || comparisonType == short.class || comparisonType == char.class) { + throw new IllegalStateException( + "unexpected comparison operation [" + + operation + + "] " + + "for type [" + + irComparisonNode.getDecorationString(IRDExpressionType.class) + + "]" + ); } else if (comparisonType == boolean.class) { if (eq) methodWriter.ifCmp(type, MethodWriter.EQ, jump); else if (ne) methodWriter.ifCmp(type, MethodWriter.NE, jump); else { - throw new IllegalStateException("unexpected comparison operation [" + operation + "] " + - "for type [" + irComparisonNode.getDecorationString(IRDExpressionType.class) + "]"); - } - } else if (comparisonType == int.class || comparisonType == long.class - || comparisonType == float.class || comparisonType == double.class) { - if (eq) methodWriter.ifCmp(type, MethodWriter.EQ, jump); - else if (ne) methodWriter.ifCmp(type, MethodWriter.NE, jump); - else if (lt) methodWriter.ifCmp(type, MethodWriter.LT, jump); - else if (lte) methodWriter.ifCmp(type, MethodWriter.LE, jump); - else if (gt) methodWriter.ifCmp(type, MethodWriter.GT, jump); - else if (gte) methodWriter.ifCmp(type, MethodWriter.GE, jump); - else { - throw new IllegalStateException("unexpected comparison operation [" + operation + "] " + - "for type [" + irComparisonNode.getDecorationString(IRDExpressionType.class) + "]"); + throw new IllegalStateException( + "unexpected comparison operation [" + + operation + + "] " + + "for type [" + + irComparisonNode.getDecorationString(IRDExpressionType.class) + + "]" + ); } + } else if (comparisonType == int.class + || comparisonType == long.class + || comparisonType == float.class + || comparisonType == double.class) { + if (eq) methodWriter.ifCmp(type, MethodWriter.EQ, jump); + else if (ne) methodWriter.ifCmp(type, MethodWriter.NE, jump); + else if (lt) methodWriter.ifCmp(type, MethodWriter.LT, jump); + else if (lte) methodWriter.ifCmp(type, MethodWriter.LE, jump); + else if (gt) methodWriter.ifCmp(type, MethodWriter.GT, jump); + else if (gte) methodWriter.ifCmp(type, MethodWriter.GE, jump); + else { + throw new IllegalStateException( + "unexpected comparison operation [" + + operation + + "] " + + "for type [" + + irComparisonNode.getDecorationString(IRDExpressionType.class) + + "]" + ); + } - } else if (comparisonType == def.class) { - Type booleanType = Type.getType(boolean.class); - Type descriptor = Type.getMethodType(booleanType, + } else if (comparisonType == def.class) { + Type booleanType = Type.getType(boolean.class); + Type descriptor = Type.getMethodType( + booleanType, MethodWriter.getType(irLeftNode.getDecorationValue(IRDExpressionType.class)), - MethodWriter.getType(irRightNode.getDecorationValue(IRDExpressionType.class))); - - if (eq) { - if (irRightNode instanceof NullNode) { - methodWriter.ifNull(jump); - } else if (irLeftNode instanceof NullNode == false && operation == Operation.EQ) { - methodWriter.invokeDefCall("eq", descriptor, DefBootstrap.BINARY_OPERATOR, DefBootstrap.OPERATOR_ALLOWS_NULL); + MethodWriter.getType(irRightNode.getDecorationValue(IRDExpressionType.class)) + ); + + if (eq) { + if (irRightNode instanceof NullNode) { + methodWriter.ifNull(jump); + } else if (irLeftNode instanceof NullNode == false && operation == Operation.EQ) { + methodWriter.invokeDefCall("eq", descriptor, DefBootstrap.BINARY_OPERATOR, DefBootstrap.OPERATOR_ALLOWS_NULL); + writejump = false; + } else { + methodWriter.ifCmp(type, MethodWriter.EQ, jump); + } + } else if (ne) { + if (irRightNode instanceof NullNode) { + methodWriter.ifNonNull(jump); + } else if (irLeftNode instanceof NullNode == false && operation == Operation.NE) { + methodWriter.invokeDefCall("eq", descriptor, DefBootstrap.BINARY_OPERATOR, DefBootstrap.OPERATOR_ALLOWS_NULL); + methodWriter.ifZCmp(MethodWriter.EQ, jump); + } else { + methodWriter.ifCmp(type, MethodWriter.NE, jump); + } + } else if (lt) { + methodWriter.invokeDefCall("lt", descriptor, DefBootstrap.BINARY_OPERATOR, 0); writejump = false; - } else { - methodWriter.ifCmp(type, MethodWriter.EQ, jump); - } - } else if (ne) { - if (irRightNode instanceof NullNode) { - methodWriter.ifNonNull(jump); - } else if (irLeftNode instanceof NullNode == false && operation == Operation.NE) { - methodWriter.invokeDefCall("eq", descriptor, DefBootstrap.BINARY_OPERATOR, DefBootstrap.OPERATOR_ALLOWS_NULL); - methodWriter.ifZCmp(MethodWriter.EQ, jump); - } else { - methodWriter.ifCmp(type, MethodWriter.NE, jump); - } - } else if (lt) { - methodWriter.invokeDefCall("lt", descriptor, DefBootstrap.BINARY_OPERATOR, 0); - writejump = false; - } else if (lte) { - methodWriter.invokeDefCall("lte", descriptor, DefBootstrap.BINARY_OPERATOR, 0); - writejump = false; - } else if (gt) { - methodWriter.invokeDefCall("gt", descriptor, DefBootstrap.BINARY_OPERATOR, 0); - writejump = false; - } else if (gte) { - methodWriter.invokeDefCall("gte", descriptor, DefBootstrap.BINARY_OPERATOR, 0); - writejump = false; - } else { - throw new IllegalStateException("unexpected comparison operation [" + operation + "] " + - "for type [" + irComparisonNode.getDecorationString(IRDExpressionType.class) + "]"); - } - } else { - if (eq) { - if (irRightNode instanceof NullNode) { - methodWriter.ifNull(jump); - } else if (operation == Operation.EQ) { - methodWriter.invokeStatic(OBJECTS_TYPE, EQUALS); + } else if (lte) { + methodWriter.invokeDefCall("lte", descriptor, DefBootstrap.BINARY_OPERATOR, 0); + writejump = false; + } else if (gt) { + methodWriter.invokeDefCall("gt", descriptor, DefBootstrap.BINARY_OPERATOR, 0); + writejump = false; + } else if (gte) { + methodWriter.invokeDefCall("gte", descriptor, DefBootstrap.BINARY_OPERATOR, 0); writejump = false; } else { - methodWriter.ifCmp(type, MethodWriter.EQ, jump); + throw new IllegalStateException( + "unexpected comparison operation [" + + operation + + "] " + + "for type [" + + irComparisonNode.getDecorationString(IRDExpressionType.class) + + "]" + ); } - } else if (ne) { - if (irRightNode instanceof NullNode) { - methodWriter.ifNonNull(jump); - } else if (operation == Operation.NE) { - methodWriter.invokeStatic(OBJECTS_TYPE, EQUALS); - methodWriter.ifZCmp(MethodWriter.EQ, jump); + } else { + if (eq) { + if (irRightNode instanceof NullNode) { + methodWriter.ifNull(jump); + } else if (operation == Operation.EQ) { + methodWriter.invokeStatic(OBJECTS_TYPE, EQUALS); + writejump = false; + } else { + methodWriter.ifCmp(type, MethodWriter.EQ, jump); + } + } else if (ne) { + if (irRightNode instanceof NullNode) { + methodWriter.ifNonNull(jump); + } else if (operation == Operation.NE) { + methodWriter.invokeStatic(OBJECTS_TYPE, EQUALS); + methodWriter.ifZCmp(MethodWriter.EQ, jump); + } else { + methodWriter.ifCmp(type, MethodWriter.NE, jump); + } } else { - methodWriter.ifCmp(type, MethodWriter.NE, jump); + throw new IllegalStateException( + "unexpected comparison operation [" + + operation + + "] " + + "for type [" + + irComparisonNode.getDecorationString(IRDExpressionType.class) + + "]" + ); } - } else { - throw new IllegalStateException("unexpected comparison operation [" + operation + "] " + - "for type [" + irComparisonNode.getDecorationString(IRDExpressionType.class) + "]"); } - } if (writejump) { methodWriter.push(false); @@ -1097,8 +1180,9 @@ public void visitListInitialization(ListInitializationNode irListInitializationN methodWriter.newInstance(MethodWriter.getType(irListInitializationNode.getDecorationValue(IRDExpressionType.class))); methodWriter.dup(); methodWriter.invokeConstructor( - Type.getType(painlessConstructor.javaConstructor.getDeclaringClass()), - Method.getMethod(painlessConstructor.javaConstructor)); + Type.getType(painlessConstructor.javaConstructor.getDeclaringClass()), + Method.getMethod(painlessConstructor.javaConstructor) + ); for (ExpressionNode irArgumentNode : irListInitializationNode.getArgumentNodes()) { methodWriter.dup(); @@ -1117,8 +1201,9 @@ public void visitMapInitialization(MapInitializationNode irMapInitializationNode methodWriter.newInstance(MethodWriter.getType(irMapInitializationNode.getDecorationValue(IRDExpressionType.class))); methodWriter.dup(); methodWriter.invokeConstructor( - Type.getType(painlessConstructor.javaConstructor.getDeclaringClass()), - Method.getMethod(painlessConstructor.javaConstructor)); + Type.getType(painlessConstructor.javaConstructor.getDeclaringClass()), + Method.getMethod(painlessConstructor.javaConstructor) + ); for (int index = 0; index < irMapInitializationNode.getArgumentsSize(); ++index) { methodWriter.dup(); @@ -1155,9 +1240,7 @@ public void visitNewArray(NewArrayNode irNewArrayNode, WriteScope writeScope) { } if (irArgumentNodes.size() > 1) { - methodWriter.visitMultiANewArrayInsn( - MethodWriter.getType(expressionType).getDescriptor(), - irArgumentNodes.size()); + methodWriter.visitMultiANewArrayInsn(MethodWriter.getType(expressionType).getDescriptor(), irArgumentNodes.size()); } else { methodWriter.newArray(MethodWriter.getType(expressionType.getComponentType())); } @@ -1180,8 +1263,9 @@ public void visitNewObject(NewObjectNode irNewObjectNode, WriteScope writeScope) PainlessConstructor painlessConstructor = irNewObjectNode.getDecorationValue(IRDConstructor.class); methodWriter.invokeConstructor( - Type.getType(painlessConstructor.javaConstructor.getDeclaringClass()), - Method.getMethod(painlessConstructor.javaConstructor)); + Type.getType(painlessConstructor.javaConstructor.getDeclaringClass()), + Method.getMethod(painlessConstructor.javaConstructor) + ); } @Override @@ -1189,15 +1273,15 @@ public void visitConstant(ConstantNode irConstantNode, WriteScope writeScope) { MethodWriter methodWriter = writeScope.getMethodWriter(); Object constant = irConstantNode.getDecorationValue(IRDConstant.class); - if (constant instanceof String) methodWriter.push((String)constant); - else if (constant instanceof Double) methodWriter.push((double)constant); - else if (constant instanceof Float) methodWriter.push((float)constant); - else if (constant instanceof Long) methodWriter.push((long)constant); - else if (constant instanceof Integer) methodWriter.push((int)constant); - else if (constant instanceof Character) methodWriter.push((char)constant); - else if (constant instanceof Short) methodWriter.push((short)constant); - else if (constant instanceof Byte) methodWriter.push((byte)constant); - else if (constant instanceof Boolean) methodWriter.push((boolean)constant); + if (constant instanceof String) methodWriter.push((String) constant); + else if (constant instanceof Double) methodWriter.push((double) constant); + else if (constant instanceof Float) methodWriter.push((float) constant); + else if (constant instanceof Long) methodWriter.push((long) constant); + else if (constant instanceof Integer) methodWriter.push((int) constant); + else if (constant instanceof Character) methodWriter.push((char) constant); + else if (constant instanceof Short) methodWriter.push((short) constant); + else if (constant instanceof Byte) methodWriter.push((byte) constant); + else if (constant instanceof Boolean) methodWriter.push((boolean) constant); else { /* * The constant doesn't properly fit into the constant pool so @@ -1226,7 +1310,7 @@ public void visitDefInterfaceReference(DefInterfaceReferenceNode irDefInterfaceR // place holder for functional interface receiver // which is resolved and replace at runtime - methodWriter.push((String)null); + methodWriter.push((String) null); if (irDefInterfaceReferenceNode.hasCondition(IRCInstanceCapture.class)) { Variable capturedThis = writeScope.getInternalVariable("this"); @@ -1333,8 +1417,9 @@ public void visitLoadDotDef(LoadDotDefNode irLoadDotDefNode, WriteScope writeSco MethodWriter methodWriter = writeScope.getMethodWriter(); methodWriter.writeDebugInfo(irLoadDotDefNode.getLocation()); Type methodType = Type.getMethodType( - MethodWriter.getType(irLoadDotDefNode.getDecorationValue(IRDExpressionType.class)), - MethodWriter.getType(def.class)); + MethodWriter.getType(irLoadDotDefNode.getDecorationValue(IRDExpressionType.class)), + MethodWriter.getType(def.class) + ); methodWriter.invokeDefCall(irLoadDotDefNode.getDecorationValue(IRDValue.class), methodType, DefBootstrap.LOAD); } @@ -1417,9 +1502,10 @@ public void visitLoadBraceDef(LoadBraceDefNode irLoadBraceDefNode, WriteScope wr MethodWriter methodWriter = writeScope.getMethodWriter(); methodWriter.writeDebugInfo(irLoadBraceDefNode.getLocation()); Type methodType = Type.getMethodType( - MethodWriter.getType(irLoadBraceDefNode.getDecorationValue(IRDExpressionType.class)), - MethodWriter.getType(def.class), - MethodWriter.getType(irLoadBraceDefNode.getDecorationValue(IRDIndexType.class))); + MethodWriter.getType(irLoadBraceDefNode.getDecorationValue(IRDExpressionType.class)), + MethodWriter.getType(def.class), + MethodWriter.getType(irLoadBraceDefNode.getDecorationValue(IRDIndexType.class)) + ); methodWriter.invokeDefCall("arrayLoad", methodType, DefBootstrap.ARRAY_LOAD); } @@ -1448,9 +1534,10 @@ public void visitStoreDotDef(StoreDotDefNode irStoreDotDefNode, WriteScope write methodWriter.writeDebugInfo(irStoreDotDefNode.getLocation()); Type methodType = Type.getMethodType( - MethodWriter.getType(void.class), - MethodWriter.getType(def.class), - MethodWriter.getType(irStoreDotDefNode.getDecorationValue(IRDStoreType.class))); + MethodWriter.getType(void.class), + MethodWriter.getType(def.class), + MethodWriter.getType(irStoreDotDefNode.getDecorationValue(IRDStoreType.class)) + ); methodWriter.invokeDefCall(irStoreDotDefNode.getDecorationValue(IRDValue.class), methodType, DefBootstrap.STORE); } @@ -1540,10 +1627,11 @@ public void visitStoreBraceDef(StoreBraceDefNode irStoreBraceDefNode, WriteScope methodWriter.writeDebugInfo(irStoreBraceDefNode.getLocation()); Type methodType = Type.getMethodType( - MethodWriter.getType(void.class), - MethodWriter.getType(def.class), - MethodWriter.getType(irStoreBraceDefNode.getDecorationValue(IRDIndexType.class)), - MethodWriter.getType(irStoreBraceDefNode.getDecorationValue(IRDStoreType.class))); + MethodWriter.getType(void.class), + MethodWriter.getType(def.class), + MethodWriter.getType(irStoreBraceDefNode.getDecorationValue(IRDIndexType.class)), + MethodWriter.getType(irStoreBraceDefNode.getDecorationValue(IRDStoreType.class)) + ); methodWriter.invokeDefCall("arrayStore", methodType, DefBootstrap.ARRAY_STORE); } @@ -1585,9 +1673,11 @@ public void visitInvokeCallDef(InvokeCallDefNode irInvokeCallDefNode, WriteScope // to hint at which values are the call's arguments // versus which values are captures if (irArgumentNode instanceof DefInterfaceReferenceNode) { - DefInterfaceReferenceNode defInterfaceReferenceNode = (DefInterfaceReferenceNode)irArgumentNode; - List captureNames = - defInterfaceReferenceNode.getDecorationValueOrDefault(IRDCaptureNames.class, Collections.emptyList()); + DefInterfaceReferenceNode defInterfaceReferenceNode = (DefInterfaceReferenceNode) irArgumentNode; + List captureNames = defInterfaceReferenceNode.getDecorationValueOrDefault( + IRDCaptureNames.class, + Collections.emptyList() + ); boostrapArguments.add(defInterfaceReferenceNode.getDecorationValue(IRDDefReferenceEncoding.class).toString()); if (defInterfaceReferenceNode.hasCondition(IRCInstanceCapture.class)) { @@ -1599,7 +1689,7 @@ public void visitInvokeCallDef(InvokeCallDefNode irInvokeCallDefNode, WriteScope // where the value is the number of current arguments plus the // total number of captures for easier capture count tracking // when resolved at runtime - char encoding = (char)(i + capturedCount); + char encoding = (char) (i + capturedCount); defCallRecipe.append(encoding); capturedCount += captureNames.size(); @@ -1622,8 +1712,10 @@ public void visitInvokeCallDef(InvokeCallDefNode irInvokeCallDefNode, WriteScope } String methodName = irInvokeCallDefNode.getDecorationValue(IRDName.class); - Type methodType = Type.getMethodType(MethodWriter.getType( - irInvokeCallDefNode.getDecorationValue(IRDExpressionType.class)), asmParameterTypes); + Type methodType = Type.getMethodType( + MethodWriter.getType(irInvokeCallDefNode.getDecorationValue(IRDExpressionType.class)), + asmParameterTypes + ); boostrapArguments.add(0, defCallRecipe.toString()); methodWriter.invokeDefCall(methodName, methodType, DefBootstrap.METHOD_CALL, boostrapArguments.toArray()); @@ -1678,8 +1770,10 @@ public void visitInvokeCallMember(InvokeCallMemberNode irInvokeCallMemberNode, W visit(irArgumentNode, writeScope); } - Method asmMethod = new Method(thisMethod.javaMethod.getName(), - thisMethod.methodType.dropParameterTypes(0, 1).toMethodDescriptorString()); + Method asmMethod = new Method( + thisMethod.javaMethod.getName(), + thisMethod.methodType.dropParameterTypes(0, 1).toMethodDescriptorString() + ); methodWriter.invokeVirtual(CLASS_TYPE, asmMethod); } else if (importedMethod != null) { for (ExpressionNode irArgumentNode : irArgumentNodes) { diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/DefaultSemanticAnalysisPhase.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/DefaultSemanticAnalysisPhase.java index bb17fccc36cd6..eee65f0e40b66 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/DefaultSemanticAnalysisPhase.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/DefaultSemanticAnalysisPhase.java @@ -193,14 +193,24 @@ public void checkedVisit(AExpression userExpressionNode, SemanticScope semanticS userExpressionNode.visit(this, semanticScope); if (semanticScope.hasDecoration(userExpressionNode, PartialCanonicalTypeName.class)) { - throw userExpressionNode.createError(new IllegalArgumentException("cannot resolve symbol [" + - semanticScope.getDecoration(userExpressionNode, PartialCanonicalTypeName.class).getPartialCanonicalTypeName() + - "]")); + throw userExpressionNode.createError( + new IllegalArgumentException( + "cannot resolve symbol [" + + semanticScope.getDecoration(userExpressionNode, PartialCanonicalTypeName.class).getPartialCanonicalTypeName() + + "]" + ) + ); } if (semanticScope.hasDecoration(userExpressionNode, StaticType.class)) { - throw userExpressionNode.createError(new IllegalArgumentException("value required: instead found unexpected type " + - "[" + semanticScope.getDecoration(userExpressionNode, StaticType.class).getStaticCanonicalTypeName() + "]")); + throw userExpressionNode.createError( + new IllegalArgumentException( + "value required: instead found unexpected type " + + "[" + + semanticScope.getDecoration(userExpressionNode, StaticType.class).getStaticCanonicalTypeName() + + "]" + ) + ); } if (semanticScope.hasDecoration(userExpressionNode, ValueType.class) == false) { @@ -224,8 +234,8 @@ public void visitClass(SClass userClassNode, ScriptScope scriptScope) { */ public void visitFunction(SFunction userFunctionNode, ScriptScope scriptScope) { String functionName = userFunctionNode.getFunctionName(); - LocalFunction localFunction = - scriptScope.getFunctionTable().getFunction(functionName, userFunctionNode.getCanonicalTypeNameParameters().size()); + LocalFunction localFunction = scriptScope.getFunctionTable() + .getFunction(functionName, userFunctionNode.getCanonicalTypeNameParameters().size()); Class returnType = localFunction.getReturnType(); List> typeParameters = localFunction.getTypeParameters(); FunctionScope functionScope = newFunctionScope(scriptScope, localFunction.getReturnType()); @@ -239,9 +249,17 @@ public void visitFunction(SFunction userFunctionNode, ScriptScope scriptScope) { SBlock userBlockNode = userFunctionNode.getBlockNode(); if (userBlockNode.getStatementNodes().isEmpty()) { - throw userFunctionNode.createError(new IllegalArgumentException("invalid function definition: " + - "found no statements for function " + - "[" + functionName + "] with [" + typeParameters.size() + "] parameters")); + throw userFunctionNode.createError( + new IllegalArgumentException( + "invalid function definition: " + + "found no statements for function " + + "[" + + functionName + + "] with [" + + typeParameters.size() + + "] parameters" + ) + ); } functionScope.setCondition(userBlockNode, LastSource.class); @@ -250,9 +268,17 @@ public void visitFunction(SFunction userFunctionNode, ScriptScope scriptScope) { boolean isAutoReturnEnabled = userFunctionNode.isAutoReturnEnabled(); if (methodEscape == false && isAutoReturnEnabled == false && returnType != void.class) { - throw userFunctionNode.createError(new IllegalArgumentException("invalid function definition: " + - "not all paths provide a return value for function " + - "[" + functionName + "] with [" + typeParameters.size() + "] parameters")); + throw userFunctionNode.createError( + new IllegalArgumentException( + "invalid function definition: " + + "not all paths provide a return value for function " + + "[" + + functionName + + "] with [" + + typeParameters.size() + + "] parameters" + ) + ); } if (methodEscape) { @@ -387,28 +413,27 @@ public void visitIfElse(SIfElse userIfElseNode, SemanticScope semanticScope) { semanticScope.replicateCondition(userIfElseNode, userElseBlockNode, LastLoop.class); visit(userElseBlockNode, semanticScope.newLocalScope()); - if (semanticScope.getCondition(userIfBlockNode, MethodEscape.class) && - semanticScope.getCondition(userElseBlockNode, MethodEscape.class)) { + if (semanticScope.getCondition(userIfBlockNode, MethodEscape.class) + && semanticScope.getCondition(userElseBlockNode, MethodEscape.class)) { semanticScope.setCondition(userIfElseNode, MethodEscape.class); } - if (semanticScope.getCondition(userIfBlockNode, LoopEscape.class) && - semanticScope.getCondition(userElseBlockNode, LoopEscape.class)) { + if (semanticScope.getCondition(userIfBlockNode, LoopEscape.class) + && semanticScope.getCondition(userElseBlockNode, LoopEscape.class)) { semanticScope.setCondition(userIfElseNode, LoopEscape.class); } - if (semanticScope.getCondition(userIfBlockNode, AllEscape.class) && - semanticScope.getCondition(userElseBlockNode, AllEscape.class)) { + if (semanticScope.getCondition(userIfBlockNode, AllEscape.class) + && semanticScope.getCondition(userElseBlockNode, AllEscape.class)) { semanticScope.setCondition(userIfElseNode, AllEscape.class); } - if (semanticScope.getCondition(userIfBlockNode, AnyContinue.class) || - semanticScope.getCondition(userElseBlockNode, AnyContinue.class)) { + if (semanticScope.getCondition(userIfBlockNode, AnyContinue.class) + || semanticScope.getCondition(userElseBlockNode, AnyContinue.class)) { semanticScope.setCondition(userIfElseNode, AnyContinue.class); } - if ( semanticScope.getCondition(userIfBlockNode, AnyBreak.class) || - semanticScope.getCondition(userElseBlockNode, AnyBreak.class)) { + if (semanticScope.getCondition(userIfBlockNode, AnyBreak.class) || semanticScope.getCondition(userElseBlockNode, AnyBreak.class)) { semanticScope.setCondition(userIfElseNode, AnyBreak.class); } } @@ -431,7 +456,7 @@ public void visitWhile(SWhile userWhileNode, SemanticScope semanticScope) { boolean continuous = false; if (userConditionNode instanceof EBooleanConstant) { - continuous = ((EBooleanConstant)userConditionNode).getBool(); + continuous = ((EBooleanConstant) userConditionNode).getBool(); if (continuous == false) { throw userWhileNode.createError(new IllegalArgumentException("extraneous while loop")); @@ -449,8 +474,8 @@ public void visitWhile(SWhile userWhileNode, SemanticScope semanticScope) { semanticScope.setCondition(userBlockNode, InLoop.class); visit(userBlockNode, semanticScope); - if (semanticScope.getCondition(userBlockNode, LoopEscape.class) && - semanticScope.getCondition(userBlockNode, AnyContinue.class) == false) { + if (semanticScope.getCondition(userBlockNode, LoopEscape.class) + && semanticScope.getCondition(userBlockNode, AnyContinue.class) == false) { throw userWhileNode.createError(new IllegalArgumentException("extraneous while loop")); } @@ -479,8 +504,8 @@ public void visitDo(SDo userDoNode, SemanticScope semanticScope) { semanticScope.setCondition(userBlockNode, InLoop.class); visit(userBlockNode, semanticScope); - if (semanticScope.getCondition(userBlockNode, LoopEscape.class) && - semanticScope.getCondition(userBlockNode, AnyContinue.class) == false) { + if (semanticScope.getCondition(userBlockNode, LoopEscape.class) + && semanticScope.getCondition(userBlockNode, AnyContinue.class) == false) { throw userDoNode.createError(new IllegalArgumentException("extraneous do-while loop")); } @@ -494,7 +519,7 @@ public void visitDo(SDo userDoNode, SemanticScope semanticScope) { boolean continuous; if (userConditionNode instanceof EBooleanConstant) { - continuous = ((EBooleanConstant)userConditionNode).getBool(); + continuous = ((EBooleanConstant) userConditionNode).getBool(); if (continuous == false) { throw userDoNode.createError(new IllegalArgumentException("extraneous do-while loop")); @@ -523,7 +548,7 @@ public void visitFor(SFor userForNode, SemanticScope semanticScope) { if (userInitializerNode instanceof SDeclBlock) { visit(userInitializerNode, semanticScope); } else if (userInitializerNode instanceof AExpression) { - checkedVisit((AExpression)userInitializerNode, semanticScope); + checkedVisit((AExpression) userInitializerNode, semanticScope); } else { throw userForNode.createError(new IllegalStateException("illegal tree structure")); } @@ -540,7 +565,7 @@ public void visitFor(SFor userForNode, SemanticScope semanticScope) { decorateWithCast(userConditionNode, semanticScope); if (userConditionNode instanceof EBooleanConstant) { - continuous = ((EBooleanConstant)userConditionNode).getBool(); + continuous = ((EBooleanConstant) userConditionNode).getBool(); if (continuous == false) { throw userForNode.createError(new IllegalArgumentException("extraneous for loop")); @@ -565,8 +590,8 @@ public void visitFor(SFor userForNode, SemanticScope semanticScope) { semanticScope.setCondition(userBlockNode, InLoop.class); visit(userBlockNode, semanticScope); - if (semanticScope.getCondition(userBlockNode, LoopEscape.class) && - semanticScope.getCondition(userBlockNode, AnyContinue.class) == false) { + if (semanticScope.getCondition(userBlockNode, LoopEscape.class) + && semanticScope.getCondition(userBlockNode, AnyContinue.class) == false) { throw userForNode.createError(new IllegalArgumentException("extraneous for loop")); } @@ -591,8 +616,9 @@ public void visitEach(SEach userEachNode, SemanticScope semanticScope) { Class type = semanticScope.getScriptScope().getPainlessLookup().canonicalTypeNameToType(canonicalTypeName); if (type == null) { - throw userEachNode.createError(new IllegalArgumentException( - "invalid foreach loop: type [" + canonicalTypeName + "] not found")); + throw userEachNode.createError( + new IllegalArgumentException("invalid foreach loop: type [" + canonicalTypeName + "] not found") + ); } semanticScope = semanticScope.newLocalScope(); @@ -612,28 +638,37 @@ public void visitEach(SEach userEachNode, SemanticScope semanticScope) { semanticScope.setCondition(userBlockNode, InLoop.class); visit(userBlockNode, semanticScope); - if (semanticScope.getCondition(userBlockNode, LoopEscape.class) && - semanticScope.getCondition(userBlockNode, AnyContinue.class) == false) { + if (semanticScope.getCondition(userBlockNode, LoopEscape.class) + && semanticScope.getCondition(userBlockNode, AnyContinue.class) == false) { throw userEachNode.createError(new IllegalArgumentException("extraneous foreach loop")); } Class iterableValueType = semanticScope.getDecoration(userIterableNode, ValueType.class).getValueType(); if (iterableValueType.isArray()) { - PainlessCast painlessCast = - AnalyzerCaster.getLegalCast(location, iterableValueType.getComponentType(), variable.getType(), true, true); + PainlessCast painlessCast = AnalyzerCaster.getLegalCast( + location, + iterableValueType.getComponentType(), + variable.getType(), + true, + true + ); if (painlessCast != null) { semanticScope.putDecoration(userEachNode, new ExpressionPainlessCast(painlessCast)); } } else if (iterableValueType == def.class || Iterable.class.isAssignableFrom(iterableValueType)) { if (iterableValueType != def.class) { - PainlessMethod method = semanticScope.getScriptScope().getPainlessLookup(). - lookupPainlessMethod(iterableValueType, false, "iterator", 0); + PainlessMethod method = semanticScope.getScriptScope() + .getPainlessLookup() + .lookupPainlessMethod(iterableValueType, false, "iterator", 0); if (method == null) { - throw userEachNode.createError(new IllegalArgumentException("invalid foreach loop: " + - "method [" + typeToCanonicalTypeName(iterableValueType) + ", iterator/0] not found")); + throw userEachNode.createError( + new IllegalArgumentException( + "invalid foreach loop: " + "method [" + typeToCanonicalTypeName(iterableValueType) + ", iterator/0] not found" + ) + ); } semanticScope.putDecoration(userEachNode, new IterablePainlessMethod(method)); @@ -645,8 +680,14 @@ public void visitEach(SEach userEachNode, SemanticScope semanticScope) { semanticScope.putDecoration(userEachNode, new ExpressionPainlessCast(painlessCast)); } } else { - throw userEachNode.createError(new IllegalArgumentException("invalid foreach loop: " + - "cannot iterate over type [" + PainlessLookupUtility.typeToCanonicalTypeName(iterableValueType) + "].")); + throw userEachNode.createError( + new IllegalArgumentException( + "invalid foreach loop: " + + "cannot iterate over type [" + + PainlessLookupUtility.typeToCanonicalTypeName(iterableValueType) + + "]." + ) + ); } } @@ -670,16 +711,18 @@ public void visitDeclaration(SDeclaration userDeclarationNode, SemanticScope sem String symbol = userDeclarationNode.getSymbol(); if (scriptScope.getPainlessLookup().isValidCanonicalClassName(symbol)) { - throw userDeclarationNode.createError(new IllegalArgumentException( - "invalid declaration: type [" + symbol + "] cannot be a name")); + throw userDeclarationNode.createError( + new IllegalArgumentException("invalid declaration: type [" + symbol + "] cannot be a name") + ); } String canonicalTypeName = userDeclarationNode.getCanonicalTypeName(); Class type = scriptScope.getPainlessLookup().canonicalTypeNameToType(canonicalTypeName); if (type == null) { - throw userDeclarationNode.createError(new IllegalArgumentException( - "invalid declaration: cannot resolve type [" + canonicalTypeName + "]")); + throw userDeclarationNode.createError( + new IllegalArgumentException("invalid declaration: cannot resolve type [" + canonicalTypeName + "]") + ); } AExpression userValueNode = userDeclarationNode.getValueNode(); @@ -706,9 +749,17 @@ public void visitReturn(SReturn userReturnNode, SemanticScope semanticScope) { if (userValueNode == null) { if (semanticScope.getReturnType() != void.class) { - throw userReturnNode.createError(new ClassCastException("cannot cast from " + - "[" + semanticScope.getReturnCanonicalTypeName() + "] to " + - "[" + PainlessLookupUtility.typeToCanonicalTypeName(void.class) + "]")); + throw userReturnNode.createError( + new ClassCastException( + "cannot cast from " + + "[" + + semanticScope.getReturnCanonicalTypeName() + + "] to " + + "[" + + PainlessLookupUtility.typeToCanonicalTypeName(void.class) + + "]" + ) + ); } } else { semanticScope.setCondition(userValueNode, Read.class); @@ -820,16 +871,18 @@ public void visitCatch(SCatch userCatchNode, SemanticScope semanticScope) { String symbol = userCatchNode.getSymbol(); if (scriptScope.getPainlessLookup().isValidCanonicalClassName(symbol)) { - throw userCatchNode.createError(new IllegalArgumentException( - "invalid catch declaration: type [" + symbol + "] cannot be a name")); + throw userCatchNode.createError( + new IllegalArgumentException("invalid catch declaration: type [" + symbol + "] cannot be a name") + ); } String canonicalTypeName = userCatchNode.getCanonicalTypeName(); Class type = scriptScope.getPainlessLookup().canonicalTypeNameToType(canonicalTypeName); if (type == null) { - throw userCatchNode.createError(new IllegalArgumentException( - "invalid catch declaration: cannot resolve type [" + canonicalTypeName + "]")); + throw userCatchNode.createError( + new IllegalArgumentException("invalid catch declaration: cannot resolve type [" + canonicalTypeName + "]") + ); } Location location = userCatchNode.getLocation(); @@ -838,9 +891,16 @@ public void visitCatch(SCatch userCatchNode, SemanticScope semanticScope) { Class baseException = userCatchNode.getBaseException(); if (userCatchNode.getBaseException().isAssignableFrom(type) == false) { - throw userCatchNode.createError(new ClassCastException( - "cannot cast from [" + PainlessLookupUtility.typeToCanonicalTypeName(type) + "] " + - "to [" + PainlessLookupUtility.typeToCanonicalTypeName(baseException) + "]")); + throw userCatchNode.createError( + new ClassCastException( + "cannot cast from [" + + PainlessLookupUtility.typeToCanonicalTypeName(type) + + "] " + + "to [" + + PainlessLookupUtility.typeToCanonicalTypeName(baseException) + + "]" + ) + ); } SBlock userBlockNode = userCatchNode.getBlockNode(); @@ -970,8 +1030,18 @@ public void visitAssignment(EAssignment userAssignmentNode, SemanticScope semant } if (compoundType == null || (isShift && shiftType == null)) { - throw userAssignmentNode.createError(new ClassCastException("invalid compound assignment: " + - "cannot apply [" + operation.symbol + "=] to types [" + leftValueType + "] and [" + rightValueType + "]")); + throw userAssignmentNode.createError( + new ClassCastException( + "invalid compound assignment: " + + "cannot apply [" + + operation.symbol + + "=] to types [" + + leftValueType + + "] and [" + + rightValueType + + "]" + ) + ); } if (isConcatenation) { @@ -1005,14 +1075,17 @@ public void visitAssignment(EAssignment userAssignmentNode, SemanticScope semant if (downcast != null) { semanticScope.putDecoration(userAssignmentNode, new DowncastPainlessCast(downcast)); } - // if the lhs node is a def optimized node we update the actual type to remove the need for a cast + // if the lhs node is a def optimized node we update the actual type to remove the need for a cast } else if (semanticScope.getCondition(userLeftNode, DefOptimized.class)) { checkedVisit(userRightNode, semanticScope); Class rightValueType = semanticScope.getDecoration(userRightNode, ValueType.class).getValueType(); if (rightValueType == void.class) { - throw userAssignmentNode.createError(new IllegalArgumentException( - "invalid assignment: cannot assign type [" + PainlessLookupUtility.typeToCanonicalTypeName(void.class) + "]")); + throw userAssignmentNode.createError( + new IllegalArgumentException( + "invalid assignment: cannot assign type [" + PainlessLookupUtility.typeToCanonicalTypeName(void.class) + "]" + ) + ); } semanticScope.putDecoration(userLeftNode, new ValueType(rightValueType)); @@ -1024,8 +1097,10 @@ public void visitAssignment(EAssignment userAssignmentNode, SemanticScope semant decorateWithCast(userRightNode, semanticScope); } - semanticScope.putDecoration(userAssignmentNode, - new ValueType(semanticScope.getCondition(userAssignmentNode, Read.class) ? leftValueType : void.class)); + semanticScope.putDecoration( + userAssignmentNode, + new ValueType(semanticScope.getCondition(userAssignmentNode, Read.class) ? leftValueType : void.class) + ); } /** @@ -1038,13 +1113,19 @@ public void visitUnary(EUnary userUnaryNode, SemanticScope semanticScope) { Operation operation = userUnaryNode.getOperation(); if (semanticScope.getCondition(userUnaryNode, Write.class)) { - throw userUnaryNode.createError(new IllegalArgumentException( - "invalid assignment: cannot assign a value to " + operation.name + " operation " + "[" + operation.symbol + "]")); + throw userUnaryNode.createError( + new IllegalArgumentException( + "invalid assignment: cannot assign a value to " + operation.name + " operation " + "[" + operation.symbol + "]" + ) + ); } if (semanticScope.getCondition(userUnaryNode, Read.class) == false) { - throw userUnaryNode.createError(new IllegalArgumentException( - "not a statement: result not used from " + operation.name + " operation " + "[" + operation.symbol + "]")); + throw userUnaryNode.createError( + new IllegalArgumentException( + "not a statement: result not used from " + operation.name + " operation " + "[" + operation.symbol + "]" + ) + ); } AExpression userChildNode = userUnaryNode.getChildNode(); @@ -1080,9 +1161,19 @@ public void visitUnary(EUnary userUnaryNode, SemanticScope semanticScope) { unaryType = AnalyzerCaster.promoteNumeric(childValueType, operation != Operation.BWNOT); if (unaryType == null) { - throw userUnaryNode.createError(new ClassCastException("cannot apply the " + operation.name + " operator " + - "[" + operation.symbol + "] to the type " + - "[" + PainlessLookupUtility.typeToCanonicalTypeName(childValueType) + "]")); + throw userUnaryNode.createError( + new ClassCastException( + "cannot apply the " + + operation.name + + " operator " + + "[" + + operation.symbol + + "] to the type " + + "[" + + PainlessLookupUtility.typeToCanonicalTypeName(childValueType) + + "]" + ) + ); } semanticScope.putDecoration(userChildNode, new TargetType(unaryType)); @@ -1116,13 +1207,19 @@ public void visitBinary(EBinary userBinaryNode, SemanticScope semanticScope) { Operation operation = userBinaryNode.getOperation(); if (semanticScope.getCondition(userBinaryNode, Write.class)) { - throw userBinaryNode.createError(new IllegalArgumentException( - "invalid assignment: cannot assign a value to " + operation.name + " operation " + "[" + operation.symbol + "]")); + throw userBinaryNode.createError( + new IllegalArgumentException( + "invalid assignment: cannot assign a value to " + operation.name + " operation " + "[" + operation.symbol + "]" + ) + ); } if (semanticScope.getCondition(userBinaryNode, Read.class) == false) { - throw userBinaryNode.createError(new IllegalArgumentException( - "not a statement: result not used from " + operation.name + " operation " + "[" + operation.symbol + "]")); + throw userBinaryNode.createError( + new IllegalArgumentException( + "not a statement: result not used from " + operation.name + " operation " + "[" + operation.symbol + "]" + ) + ); } AExpression userLeftNode = userBinaryNode.getLeftNode(); @@ -1169,10 +1266,22 @@ public void visitBinary(EBinary userBinaryNode, SemanticScope semanticScope) { } if (binaryType == null) { - throw userBinaryNode.createError(new ClassCastException("cannot apply the " + operation.name + " operator " + - "[" + operation.symbol + "] to the types " + - "[" + PainlessLookupUtility.typeToCanonicalTypeName(leftValueType) + "] and " + - "[" + PainlessLookupUtility.typeToCanonicalTypeName(rightValueType) + "]")); + throw userBinaryNode.createError( + new ClassCastException( + "cannot apply the " + + operation.name + + " operator " + + "[" + + operation.symbol + + "] to the types " + + "[" + + PainlessLookupUtility.typeToCanonicalTypeName(leftValueType) + + "] and " + + "[" + + PainlessLookupUtility.typeToCanonicalTypeName(rightValueType) + + "]" + ) + ); } valueType = binaryType; @@ -1219,13 +1328,19 @@ public void visitBooleanComp(EBooleanComp userBooleanCompNode, SemanticScope sem Operation operation = userBooleanCompNode.getOperation(); if (semanticScope.getCondition(userBooleanCompNode, Write.class)) { - throw userBooleanCompNode.createError(new IllegalArgumentException( - "invalid assignment: cannot assign a value to " + operation.name + " operation " + "[" + operation.symbol + "]")); + throw userBooleanCompNode.createError( + new IllegalArgumentException( + "invalid assignment: cannot assign a value to " + operation.name + " operation " + "[" + operation.symbol + "]" + ) + ); } if (semanticScope.getCondition(userBooleanCompNode, Read.class) == false) { - throw userBooleanCompNode.createError(new IllegalArgumentException( - "not a statement: result not used from " + operation.name + " operation " + "[" + operation.symbol + "]")); + throw userBooleanCompNode.createError( + new IllegalArgumentException( + "not a statement: result not used from " + operation.name + " operation " + "[" + operation.symbol + "]" + ) + ); } AExpression userLeftNode = userBooleanCompNode.getLeftNode(); @@ -1252,13 +1367,19 @@ public void visitComp(EComp userCompNode, SemanticScope semanticScope) { Operation operation = userCompNode.getOperation(); if (semanticScope.getCondition(userCompNode, Write.class)) { - throw userCompNode.createError(new IllegalArgumentException( - "invalid assignment: cannot assign a value to " + operation.name + " operation " + "[" + operation.symbol + "]")); + throw userCompNode.createError( + new IllegalArgumentException( + "invalid assignment: cannot assign a value to " + operation.name + " operation " + "[" + operation.symbol + "]" + ) + ); } if (semanticScope.getCondition(userCompNode, Read.class) == false) { - throw userCompNode.createError(new IllegalArgumentException( - "not a statement: result not used from " + operation.name + " operation " + "[" + operation.symbol + "]")); + throw userCompNode.createError( + new IllegalArgumentException( + "not a statement: result not used from " + operation.name + " operation " + "[" + operation.symbol + "]" + ) + ); } AExpression userLeftNode = userCompNode.getLeftNode(); @@ -1282,14 +1403,27 @@ public void visitComp(EComp userCompNode, SemanticScope semanticScope) { } if (promotedType == null) { - throw userCompNode.createError(new ClassCastException("cannot apply the " + operation.name + " operator " + - "[" + operation.symbol + "] to the types " + - "[" + PainlessLookupUtility.typeToCanonicalTypeName(leftValueType) + "] and " + - "[" + PainlessLookupUtility.typeToCanonicalTypeName(rightValueType) + "]")); + throw userCompNode.createError( + new ClassCastException( + "cannot apply the " + + operation.name + + " operator " + + "[" + + operation.symbol + + "] to the types " + + "[" + + PainlessLookupUtility.typeToCanonicalTypeName(leftValueType) + + "] and " + + "[" + + PainlessLookupUtility.typeToCanonicalTypeName(rightValueType) + + "]" + ) + ); } if ((operation == Operation.EQ || operation == Operation.EQR || operation == Operation.NE || operation == Operation.NER) - && userLeftNode instanceof ENull && userRightNode instanceof ENull) { + && userLeftNode instanceof ENull + && userRightNode instanceof ENull) { throw userCompNode.createError(new IllegalArgumentException("extraneous comparison of [null] constants")); } @@ -1313,13 +1447,19 @@ public void visitExplicit(EExplicit userExplicitNode, SemanticScope semanticScop String canonicalTypeName = userExplicitNode.getCanonicalTypeName(); if (semanticScope.getCondition(userExplicitNode, Write.class)) { - throw userExplicitNode.createError(new IllegalArgumentException( - "invalid assignment: cannot assign a value to an explicit cast with target type [" + canonicalTypeName + "]")); + throw userExplicitNode.createError( + new IllegalArgumentException( + "invalid assignment: cannot assign a value to an explicit cast with target type [" + canonicalTypeName + "]" + ) + ); } if (semanticScope.getCondition(userExplicitNode, Read.class) == false) { - throw userExplicitNode.createError(new IllegalArgumentException( - "not a statement: result not used from explicit cast with target type [" + canonicalTypeName + "]")); + throw userExplicitNode.createError( + new IllegalArgumentException( + "not a statement: result not used from explicit cast with target type [" + canonicalTypeName + "]" + ) + ); } Class valueType = semanticScope.getScriptScope().getPainlessLookup().canonicalTypeNameToType(canonicalTypeName); @@ -1347,13 +1487,19 @@ public void visitInstanceof(EInstanceof userInstanceofNode, SemanticScope semant String canonicalTypeName = userInstanceofNode.getCanonicalTypeName(); if (semanticScope.getCondition(userInstanceofNode, Write.class)) { - throw userInstanceofNode.createError(new IllegalArgumentException( - "invalid assignment: cannot assign a value to instanceof with target type [" + canonicalTypeName + "]")); + throw userInstanceofNode.createError( + new IllegalArgumentException( + "invalid assignment: cannot assign a value to instanceof with target type [" + canonicalTypeName + "]" + ) + ); } if (semanticScope.getCondition(userInstanceofNode, Read.class) == false) { - throw userInstanceofNode.createError(new IllegalArgumentException( - "not a statement: result not used from instanceof with target type [" + canonicalTypeName + "]")); + throw userInstanceofNode.createError( + new IllegalArgumentException( + "not a statement: result not used from instanceof with target type [" + canonicalTypeName + "]" + ) + ); } Class instanceType = semanticScope.getScriptScope().getPainlessLookup().canonicalTypeNameToType(canonicalTypeName); @@ -1377,13 +1523,15 @@ public void visitInstanceof(EInstanceof userInstanceofNode, SemanticScope semant @Override public void visitConditional(EConditional userConditionalNode, SemanticScope semanticScope) { if (semanticScope.getCondition(userConditionalNode, Write.class)) { - throw userConditionalNode.createError(new IllegalArgumentException( - "invalid assignment: cannot assign a value to conditional operation [?:]")); + throw userConditionalNode.createError( + new IllegalArgumentException("invalid assignment: cannot assign a value to conditional operation [?:]") + ); } if (semanticScope.getCondition(userConditionalNode, Read.class) == false) { - throw userConditionalNode.createError(new IllegalArgumentException( - "not a statement: result not used from conditional operation [?:]")); + throw userConditionalNode.createError( + new IllegalArgumentException("not a statement: result not used from conditional operation [?:]") + ); } AExpression userConditionNode = userConditionalNode.getConditionNode(); @@ -1415,9 +1563,17 @@ public void visitConditional(EConditional userConditionalNode, SemanticScope sem Class promote = AnalyzerCaster.promoteConditional(leftValueType, rightValueType); if (promote == null) { - throw userConditionalNode.createError(new ClassCastException("cannot apply the conditional operator [?:] to the types " + - "[" + PainlessLookupUtility.typeToCanonicalTypeName(leftValueType) + "] and " + - "[" + PainlessLookupUtility.typeToCanonicalTypeName(rightValueType) + "]")); + throw userConditionalNode.createError( + new ClassCastException( + "cannot apply the conditional operator [?:] to the types " + + "[" + + PainlessLookupUtility.typeToCanonicalTypeName(leftValueType) + + "] and " + + "[" + + PainlessLookupUtility.typeToCanonicalTypeName(rightValueType) + + "]" + ) + ); } semanticScope.putDecoration(userTrueNode, new TargetType(promote)); @@ -1440,8 +1596,9 @@ public void visitConditional(EConditional userConditionalNode, SemanticScope sem @Override public void visitElvis(EElvis userElvisNode, SemanticScope semanticScope) { if (semanticScope.getCondition(userElvisNode, Write.class)) { - throw userElvisNode.createError(new IllegalArgumentException( - "invalid assignment: cannot assign a value to elvis operation [?:]")); + throw userElvisNode.createError( + new IllegalArgumentException("invalid assignment: cannot assign a value to elvis operation [?:]") + ); } if (semanticScope.getCondition(userElvisNode, Read.class) == false) { @@ -1473,11 +1630,10 @@ public void visitElvis(EElvis userElvisNode, SemanticScope semanticScope) { if (userLeftNode instanceof ENull) { throw userElvisNode.createError(new IllegalArgumentException("Extraneous elvis operator. LHS is null.")); } - if ( userLeftNode instanceof EBooleanConstant || - userLeftNode instanceof ENumeric || - userLeftNode instanceof EDecimal || - userLeftNode instanceof EString - ) { + if (userLeftNode instanceof EBooleanConstant + || userLeftNode instanceof ENumeric + || userLeftNode instanceof EDecimal + || userLeftNode instanceof EString) { throw userElvisNode.createError(new IllegalArgumentException("Extraneous elvis operator. LHS is a constant.")); } if (leftValueType.isPrimitive()) { @@ -1512,8 +1668,9 @@ public void visitElvis(EElvis userElvisNode, SemanticScope semanticScope) { @Override public void visitListInit(EListInit userListInitNode, SemanticScope semanticScope) { if (semanticScope.getCondition(userListInitNode, Write.class)) { - throw userListInitNode.createError(new IllegalArgumentException( - "invalid assignment: cannot assign a value to list initializer")); + throw userListInitNode.createError( + new IllegalArgumentException("invalid assignment: cannot assign a value to list initializer") + ); } if (semanticScope.getCondition(userListInitNode, Read.class) == false) { @@ -1525,8 +1682,9 @@ public void visitListInit(EListInit userListInitNode, SemanticScope semanticScop PainlessConstructor constructor = semanticScope.getScriptScope().getPainlessLookup().lookupPainlessConstructor(valueType, 0); if (constructor == null) { - throw userListInitNode.createError(new IllegalArgumentException( - "constructor [" + typeToCanonicalTypeName(valueType) + ", /0] not found")); + throw userListInitNode.createError( + new IllegalArgumentException("constructor [" + typeToCanonicalTypeName(valueType) + ", /0] not found") + ); } semanticScope.putDecoration(userListInitNode, new StandardPainlessConstructor(constructor)); @@ -1534,8 +1692,9 @@ public void visitListInit(EListInit userListInitNode, SemanticScope semanticScop PainlessMethod method = semanticScope.getScriptScope().getPainlessLookup().lookupPainlessMethod(valueType, false, "add", 1); if (method == null) { - throw userListInitNode.createError(new IllegalArgumentException( - "method [" + typeToCanonicalTypeName(valueType) + ", add/1] not found")); + throw userListInitNode.createError( + new IllegalArgumentException("method [" + typeToCanonicalTypeName(valueType) + ", add/1] not found") + ); } semanticScope.putDecoration(userListInitNode, new StandardPainlessMethod(method)); @@ -1558,8 +1717,7 @@ public void visitListInit(EListInit userListInitNode, SemanticScope semanticScop @Override public void visitMapInit(EMapInit userMapInitNode, SemanticScope semanticScope) { if (semanticScope.getCondition(userMapInitNode, Write.class)) { - throw userMapInitNode.createError(new IllegalArgumentException( - "invalid assignment: cannot assign a value to map initializer")); + throw userMapInitNode.createError(new IllegalArgumentException("invalid assignment: cannot assign a value to map initializer")); } if (semanticScope.getCondition(userMapInitNode, Read.class) == false) { @@ -1571,8 +1729,9 @@ public void visitMapInit(EMapInit userMapInitNode, SemanticScope semanticScope) PainlessConstructor constructor = semanticScope.getScriptScope().getPainlessLookup().lookupPainlessConstructor(valueType, 0); if (constructor == null) { - throw userMapInitNode.createError(new IllegalArgumentException( - "constructor [" + typeToCanonicalTypeName(valueType) + ", /0] not found")); + throw userMapInitNode.createError( + new IllegalArgumentException("constructor [" + typeToCanonicalTypeName(valueType) + ", /0] not found") + ); } semanticScope.putDecoration(userMapInitNode, new StandardPainlessConstructor(constructor)); @@ -1580,8 +1739,9 @@ public void visitMapInit(EMapInit userMapInitNode, SemanticScope semanticScope) PainlessMethod method = semanticScope.getScriptScope().getPainlessLookup().lookupPainlessMethod(valueType, false, "put", 2); if (method == null) { - throw userMapInitNode.createError(new IllegalArgumentException( - "method [" + typeToCanonicalTypeName(valueType) + ", put/2] not found")); + throw userMapInitNode.createError( + new IllegalArgumentException("method [" + typeToCanonicalTypeName(valueType) + ", put/2] not found") + ); } semanticScope.putDecoration(userMapInitNode, new StandardPainlessMethod(method)); @@ -1636,8 +1796,10 @@ public void visitNewArray(ENewArray userNewArrayNode, SemanticScope semanticScop for (AExpression userValueNode : userNewArrayNode.getValueNodes()) { semanticScope.setCondition(userValueNode, Read.class); - semanticScope.putDecoration(userValueNode, - new TargetType(userNewArrayNode.isInitializer() ? valueType.getComponentType() : int.class)); + semanticScope.putDecoration( + userValueNode, + new TargetType(userNewArrayNode.isInitializer() ? valueType.getComponentType() : int.class) + ); semanticScope.setCondition(userValueNode, Internal.class); checkedVisit(userValueNode, semanticScope); decorateWithCast(userValueNode, semanticScope); @@ -1652,14 +1814,21 @@ public void visitNewArray(ENewArray userNewArrayNode, SemanticScope semanticScop */ @Override public void visitNewObj(ENewObj userNewObjNode, SemanticScope semanticScope) { - String canonicalTypeName = userNewObjNode.getCanonicalTypeName(); + String canonicalTypeName = userNewObjNode.getCanonicalTypeName(); List userArgumentNodes = userNewObjNode.getArgumentNodes(); int userArgumentsSize = userArgumentNodes.size(); if (semanticScope.getCondition(userNewObjNode, Write.class)) { - throw userNewObjNode.createError(new IllegalArgumentException( - "invalid assignment cannot assign a value to new object with constructor " + - "[" + canonicalTypeName + "/" + userArgumentsSize + "]")); + throw userNewObjNode.createError( + new IllegalArgumentException( + "invalid assignment cannot assign a value to new object with constructor " + + "[" + + canonicalTypeName + + "/" + + userArgumentsSize + + "]" + ) + ); } ScriptScope scriptScope = semanticScope.getScriptScope(); @@ -1672,8 +1841,11 @@ public void visitNewObj(ENewObj userNewObjNode, SemanticScope semanticScope) { PainlessConstructor constructor = scriptScope.getPainlessLookup().lookupPainlessConstructor(valueType, userArgumentsSize); if (constructor == null) { - throw userNewObjNode.createError(new IllegalArgumentException( - "constructor [" + typeToCanonicalTypeName(valueType) + ", /" + userArgumentsSize + "] not found")); + throw userNewObjNode.createError( + new IllegalArgumentException( + "constructor [" + typeToCanonicalTypeName(valueType) + ", /" + userArgumentsSize + "] not found" + ) + ); } scriptScope.putDecoration(userNewObjNode, new StandardPainlessConstructor(constructor)); @@ -1683,9 +1855,18 @@ public void visitNewObj(ENewObj userNewObjNode, SemanticScope semanticScope) { constructor.typeParameters.toArray(types); if (constructor.typeParameters.size() != userArgumentsSize) { - throw userNewObjNode.createError(new IllegalArgumentException( - "When calling constructor on type [" + PainlessLookupUtility.typeToCanonicalTypeName(valueType) + "] " + - "expected [" + constructor.typeParameters.size() + "] arguments, but found [" + userArgumentsSize + "].")); + throw userNewObjNode.createError( + new IllegalArgumentException( + "When calling constructor on type [" + + PainlessLookupUtility.typeToCanonicalTypeName(valueType) + + "] " + + "expected [" + + constructor.typeParameters.size() + + "] arguments, but found [" + + userArgumentsSize + + "]." + ) + ); } for (int i = 0; i < userArgumentsSize; ++i) { @@ -1712,8 +1893,11 @@ public void visitCallLocal(ECallLocal userCallLocalNode, SemanticScope semanticS int userArgumentsSize = userArgumentNodes.size(); if (semanticScope.getCondition(userCallLocalNode, Write.class)) { - throw userCallLocalNode.createError(new IllegalArgumentException( - "invalid assignment: cannot assign a value to function call [" + methodName + "/" + userArgumentsSize + "]")); + throw userCallLocalNode.createError( + new IllegalArgumentException( + "invalid assignment: cannot assign a value to function call [" + methodName + "/" + userArgumentsSize + "]" + ) + ); } ScriptScope scriptScope = semanticScope.getScriptScope(); @@ -1735,8 +1919,8 @@ public void visitCallLocal(ECallLocal userCallLocalNode, SemanticScope semanticS } if (localFunction == null) { - thisMethod = scriptScope.getPainlessLookup().lookupPainlessMethod( - scriptScope.getScriptClassInfo().getBaseClass(), false, methodName, userArgumentsSize); + thisMethod = scriptScope.getPainlessLookup() + .lookupPainlessMethod(scriptScope.getScriptClassInfo().getBaseClass(), false, methodName, userArgumentsSize); if (thisMethod == null) { importedMethod = scriptScope.getPainlessLookup().lookupImportedPainlessMethod(methodName, userArgumentsSize); @@ -1745,23 +1929,24 @@ public void visitCallLocal(ECallLocal userCallLocalNode, SemanticScope semanticS classBinding = scriptScope.getPainlessLookup().lookupPainlessClassBinding(methodName, userArgumentsSize); // check to see if this class binding requires an implicit this reference - if (classBinding != null && classBinding.typeParameters.isEmpty() == false && - classBinding.typeParameters.get(0) == scriptScope.getScriptClassInfo().getBaseClass()) { + if (classBinding != null + && classBinding.typeParameters.isEmpty() == false + && classBinding.typeParameters.get(0) == scriptScope.getScriptClassInfo().getBaseClass()) { classBinding = null; } if (classBinding == null) { // This extra check looks for a possible match where the class binding requires an implicit this - // reference. This is a temporary solution to allow the class binding access to data from the - // base script class without need for a user to add additional arguments. A long term solution + // reference. This is a temporary solution to allow the class binding access to data from the + // base script class without need for a user to add additional arguments. A long term solution // will likely involve adding a class instance binding where any instance can have a class binding - // as part of its API. However, the situation at run-time is difficult and will modifications that + // as part of its API. However, the situation at run-time is difficult and will modifications that // are a substantial change if even possible to do. classBinding = scriptScope.getPainlessLookup().lookupPainlessClassBinding(methodName, userArgumentsSize + 1); if (classBinding != null) { - if (classBinding.typeParameters.isEmpty() == false && - classBinding.typeParameters.get(0) == scriptScope.getScriptClassInfo().getBaseClass()) { + if (classBinding.typeParameters.isEmpty() == false + && classBinding.typeParameters.get(0) == scriptScope.getScriptClassInfo().getBaseClass()) { classBindingOffset = 1; } else { classBinding = null; @@ -1772,8 +1957,11 @@ public void visitCallLocal(ECallLocal userCallLocalNode, SemanticScope semanticS instanceBinding = scriptScope.getPainlessLookup().lookupPainlessInstanceBinding(methodName, userArgumentsSize); if (instanceBinding == null) { - throw userCallLocalNode.createError(new IllegalArgumentException( - "Unknown call [" + methodName + "] with [" + userArgumentsSize + "] arguments.")); + throw userCallLocalNode.createError( + new IllegalArgumentException( + "Unknown call [" + methodName + "] with [" + userArgumentsSize + "] arguments." + ) + ); } } } @@ -1842,13 +2030,15 @@ public void visitBooleanConstant(EBooleanConstant userBooleanConstantNode, Seman boolean bool = userBooleanConstantNode.getBool(); if (semanticScope.getCondition(userBooleanConstantNode, Write.class)) { - throw userBooleanConstantNode.createError(new IllegalArgumentException( - "invalid assignment: cannot assign a value to boolean constant [" + bool + "]")); + throw userBooleanConstantNode.createError( + new IllegalArgumentException("invalid assignment: cannot assign a value to boolean constant [" + bool + "]") + ); } if (semanticScope.getCondition(userBooleanConstantNode, Read.class) == false) { throw userBooleanConstantNode.createError( - new IllegalArgumentException("not a statement: boolean constant [" + bool + "] not used")); + new IllegalArgumentException("not a statement: boolean constant [" + bool + "] not used") + ); } semanticScope.putDecoration(userBooleanConstantNode, new ValueType(boolean.class)); @@ -1868,13 +2058,13 @@ public void visitNumeric(ENumeric userNumericNode, SemanticScope semanticScope) } if (semanticScope.getCondition(userNumericNode, Write.class)) { - throw userNumericNode.createError(new IllegalArgumentException( - "invalid assignment: cannot assign a value to numeric constant [" + numeric + "]")); + throw userNumericNode.createError( + new IllegalArgumentException("invalid assignment: cannot assign a value to numeric constant [" + numeric + "]") + ); } if (semanticScope.getCondition(userNumericNode, Read.class) == false) { - throw userNumericNode.createError(new IllegalArgumentException( - "not a statement: numeric constant [" + numeric + "] not used")); + throw userNumericNode.createError(new IllegalArgumentException("not a statement: numeric constant [" + numeric + "] not used")); } int radix = userNumericNode.getRadix(); @@ -1917,13 +2107,13 @@ public void visitNumeric(ENumeric userNumericNode, SemanticScope semanticScope) int integer = Integer.parseInt(numeric, radix); if (sort == byte.class && integer >= Byte.MIN_VALUE && integer <= Byte.MAX_VALUE) { - constant = (byte)integer; + constant = (byte) integer; valueType = byte.class; } else if (sort == char.class && integer >= Character.MIN_VALUE && integer <= Character.MAX_VALUE) { - constant = (char)integer; + constant = (char) integer; valueType = char.class; } else if (sort == short.class && integer >= Short.MIN_VALUE && integer <= Short.MAX_VALUE) { - constant = (short)integer; + constant = (short) integer; valueType = short.class; } else { constant = integer; @@ -1933,8 +2123,11 @@ public void visitNumeric(ENumeric userNumericNode, SemanticScope semanticScope) try { // Check if we can parse as a long. If so then hint that the user might prefer that. Long.parseLong(numeric, radix); - throw userNumericNode.createError(new IllegalArgumentException( - "Invalid int constant [" + numeric + "]. If you want a long constant then change it to [" + numeric + "L].")); + throw userNumericNode.createError( + new IllegalArgumentException( + "Invalid int constant [" + numeric + "]. If you want a long constant then change it to [" + numeric + "L]." + ) + ); } catch (NumberFormatException longNoGood) { // Ignored } @@ -1959,8 +2152,9 @@ public void visitDecimal(EDecimal userDecimalNode, SemanticScope semanticScope) } if (semanticScope.getCondition(userDecimalNode, Write.class)) { - throw userDecimalNode.createError(new IllegalArgumentException( - "invalid assignment: cannot assign a value to decimal constant [" + decimal + "]")); + throw userDecimalNode.createError( + new IllegalArgumentException("invalid assignment: cannot assign a value to decimal constant [" + decimal + "]") + ); } if (semanticScope.getCondition(userDecimalNode, Read.class) == false) { @@ -2003,8 +2197,9 @@ public void visitString(EString userStringNode, SemanticScope semanticScope) { String string = userStringNode.getString(); if (semanticScope.getCondition(userStringNode, Write.class)) { - throw userStringNode.createError(new IllegalArgumentException( - "invalid assignment: cannot assign a value to string constant [" + string + "]")); + throw userStringNode.createError( + new IllegalArgumentException("invalid assignment: cannot assign a value to string constant [" + string + "]") + ); } if (semanticScope.getCondition(userStringNode, Read.class) == false) { @@ -2034,8 +2229,9 @@ public void visitNull(ENull userNullNode, SemanticScope semanticScope) { if (targetType != null) { if (targetType.getTargetType().isPrimitive()) { - throw userNullNode.createError(new IllegalArgumentException( - "Cannot cast null to a primitive type [" + targetType.getTargetCanonicalTypeName() + "].")); + throw userNullNode.createError( + new IllegalArgumentException("Cannot cast null to a primitive type [" + targetType.getTargetCanonicalTypeName() + "].") + ); } valueType = targetType.getTargetType(); @@ -2056,19 +2252,27 @@ public void visitRegex(ERegex userRegexNode, SemanticScope semanticScope) { String flags = userRegexNode.getFlags(); if (semanticScope.getCondition(userRegexNode, Write.class)) { - throw userRegexNode.createError(new IllegalArgumentException( - "invalid assignment: cannot assign a value to regex constant [" + pattern + "] with flags [" + flags + "]")); + throw userRegexNode.createError( + new IllegalArgumentException( + "invalid assignment: cannot assign a value to regex constant [" + pattern + "] with flags [" + flags + "]" + ) + ); } if (semanticScope.getCondition(userRegexNode, Read.class) == false) { - throw userRegexNode.createError(new IllegalArgumentException( - "not a statement: regex constant [" + pattern + "] with flags [" + flags + "] not used")); + throw userRegexNode.createError( + new IllegalArgumentException("not a statement: regex constant [" + pattern + "] with flags [" + flags + "] not used") + ); } if (semanticScope.getScriptScope().getCompilerSettings().areRegexesEnabled() == CompilerSettings.RegexEnabled.FALSE) { - throw userRegexNode.createError(new IllegalStateException("Regexes are disabled. Set [script.painless.regex.enabled] to [true] " - + "in elasticsearch.yaml to allow them. Be careful though, regexes break out of Painless's protection against deep " - + "recursion and long loops.")); + throw userRegexNode.createError( + new IllegalStateException( + "Regexes are disabled. Set [script.painless.regex.enabled] to [true] " + + "in elasticsearch.yaml to allow them. Be careful though, regexes break out of Painless's protection against deep " + + "recursion and long loops." + ) + ); } Location location = userRegexNode.getLocation(); @@ -2113,8 +2317,17 @@ public void visitRegex(ERegex userRegexNode, SemanticScope semanticScope) { compiled = Pattern.compile(pattern, regexFlags); } catch (PatternSyntaxException pse) { throw new Location(location.getSourceName(), location.getOffset() + 1 + pse.getIndex()).createError( - new IllegalArgumentException("invalid regular expression: " + - "could not compile regex constant [" + pattern + "] with flags [" + flags + "]: " + pse.getDescription(), pse)); + new IllegalArgumentException( + "invalid regular expression: " + + "could not compile regex constant [" + + pattern + + "] with flags [" + + flags + + "]: " + + pse.getDescription(), + pse + ) + ); } semanticScope.putDecoration(userRegexNode, new ValueType(Pattern.class)); @@ -2166,13 +2379,20 @@ public void visitLambda(ELambda userLambdaNode, SemanticScope semanticScope) { // we know the method statically, infer return type and any unknown/def types interfaceMethod = scriptScope.getPainlessLookup().lookupFunctionalInterfacePainlessMethod(targetType.getTargetType()); if (interfaceMethod == null) { - throw userLambdaNode.createError(new IllegalArgumentException("Cannot pass lambda to " + - "[" + targetType.getTargetCanonicalTypeName() + "], not a functional interface")); + throw userLambdaNode.createError( + new IllegalArgumentException( + "Cannot pass lambda to " + "[" + targetType.getTargetCanonicalTypeName() + "], not a functional interface" + ) + ); } // check arity before we manipulate parameters - if (interfaceMethod.typeParameters.size() != canonicalTypeNameParameters.size()) - throw new IllegalArgumentException("Incorrect number of parameters for [" + interfaceMethod.javaMethod.getName() + - "] in [" + targetType.getTargetCanonicalTypeName() + "]"); + if (interfaceMethod.typeParameters.size() != canonicalTypeNameParameters.size()) throw new IllegalArgumentException( + "Incorrect number of parameters for [" + + interfaceMethod.javaMethod.getName() + + "] in [" + + targetType.getTargetCanonicalTypeName() + + "]" + ); // for method invocation, its allowed to ignore the return value if (interfaceMethod.returnType == void.class) { returnType = def.class; @@ -2246,12 +2466,22 @@ public void visitLambda(ELambda userLambdaNode, SemanticScope semanticScope) { // setup method reference to synthetic method if (targetType == null) { valueType = String.class; - semanticScope.putDecoration(userLambdaNode, - new EncodingDecoration(true, lambdaScope.usesInstanceMethod(), "this", name, capturedVariables.size())); + semanticScope.putDecoration( + userLambdaNode, + new EncodingDecoration(true, lambdaScope.usesInstanceMethod(), "this", name, capturedVariables.size()) + ); } else { - FunctionRef ref = FunctionRef.create(scriptScope.getPainlessLookup(), scriptScope.getFunctionTable(), - location, targetType.getTargetType(), "this", name, capturedVariables.size(), - scriptScope.getCompilerSettings().asMap(), lambdaScope.usesInstanceMethod()); + FunctionRef ref = FunctionRef.create( + scriptScope.getPainlessLookup(), + scriptScope.getFunctionTable(), + location, + targetType.getTargetType(), + "this", + name, + capturedVariables.size(), + scriptScope.getCompilerSettings().asMap(), + lambdaScope.usesInstanceMethod() + ); valueType = targetType.getTargetType(); semanticScope.putDecoration(userLambdaNode, new ReferenceDecoration(ref)); } @@ -2283,15 +2513,19 @@ public void visitFunctionRef(EFunctionRef userFunctionRefNode, SemanticScope sem Class valueType; boolean isInstanceReference = "this".equals(symbol); - if (isInstanceReference || type != null) { + if (isInstanceReference || type != null) { if (semanticScope.getCondition(userFunctionRefNode, Write.class)) { - throw userFunctionRefNode.createError(new IllegalArgumentException( - "invalid assignment: cannot assign a value to function reference [" + symbol + ":" + methodName + "]")); + throw userFunctionRefNode.createError( + new IllegalArgumentException( + "invalid assignment: cannot assign a value to function reference [" + symbol + ":" + methodName + "]" + ) + ); } if (read == false) { - throw userFunctionRefNode.createError(new IllegalArgumentException( - "not a statement: function reference [" + symbol + ":" + methodName + "] not used")); + throw userFunctionRefNode.createError( + new IllegalArgumentException("not a statement: function reference [" + symbol + ":" + methodName + "] not used") + ); } if (isInstanceReference) { @@ -2301,21 +2535,35 @@ public void visitFunctionRef(EFunctionRef userFunctionRefNode, SemanticScope sem valueType = String.class; semanticScope.putDecoration(userFunctionRefNode, new EncodingDecoration(true, isInstanceReference, symbol, methodName, 0)); } else { - FunctionRef ref = FunctionRef.create(scriptScope.getPainlessLookup(), scriptScope.getFunctionTable(), - location, targetType.getTargetType(), symbol, methodName, 0, - scriptScope.getCompilerSettings().asMap(), isInstanceReference); + FunctionRef ref = FunctionRef.create( + scriptScope.getPainlessLookup(), + scriptScope.getFunctionTable(), + location, + targetType.getTargetType(), + symbol, + methodName, + 0, + scriptScope.getCompilerSettings().asMap(), + isInstanceReference + ); valueType = targetType.getTargetType(); semanticScope.putDecoration(userFunctionRefNode, new ReferenceDecoration(ref)); } } else { if (semanticScope.getCondition(userFunctionRefNode, Write.class)) { - throw userFunctionRefNode.createError(new IllegalArgumentException( - "invalid assignment: cannot assign a value to capturing function reference [" + symbol + ":" + methodName + "]")); + throw userFunctionRefNode.createError( + new IllegalArgumentException( + "invalid assignment: cannot assign a value to capturing function reference [" + symbol + ":" + methodName + "]" + ) + ); } if (read == false) { - throw userFunctionRefNode.createError(new IllegalArgumentException( - "not a statement: capturing function reference [" + symbol + ":" + methodName + "] not used")); + throw userFunctionRefNode.createError( + new IllegalArgumentException( + "not a statement: capturing function reference [" + symbol + ":" + methodName + "] not used" + ) + ); } SemanticScope.Variable captured = semanticScope.getVariable(location, symbol); @@ -2340,9 +2588,17 @@ public void visitFunctionRef(EFunctionRef userFunctionRefNode, SemanticScope sem valueType = targetType.getTargetType(); // static case if (captured.getType() != def.class) { - FunctionRef ref = FunctionRef.create(scriptScope.getPainlessLookup(), scriptScope.getFunctionTable(), location, - targetType.getTargetType(), captured.getCanonicalTypeName(), methodName, 1, - scriptScope.getCompilerSettings().asMap(), false); + FunctionRef ref = FunctionRef.create( + scriptScope.getPainlessLookup(), + scriptScope.getFunctionTable(), + location, + targetType.getTargetType(), + captured.getCanonicalTypeName(), + methodName, + 1, + scriptScope.getCompilerSettings().asMap(), + false + ); semanticScope.putDecoration(userFunctionRefNode, new ReferenceDecoration(ref)); } } @@ -2361,13 +2617,19 @@ public void visitNewArrayFunctionRef(ENewArrayFunctionRef userNewArrayFunctionRe String canonicalTypeName = userNewArrayFunctionRefNode.getCanonicalTypeName(); if (semanticScope.getCondition(userNewArrayFunctionRefNode, Write.class)) { - throw userNewArrayFunctionRefNode.createError(new IllegalArgumentException( - "cannot assign a value to new array function reference with target type [ + " + canonicalTypeName + "]")); + throw userNewArrayFunctionRefNode.createError( + new IllegalArgumentException( + "cannot assign a value to new array function reference with target type [ + " + canonicalTypeName + "]" + ) + ); } if (semanticScope.getCondition(userNewArrayFunctionRefNode, Read.class) == false) { - throw userNewArrayFunctionRefNode.createError(new IllegalArgumentException( - "not a statement: new array function reference with target type [" + canonicalTypeName + "] not used")); + throw userNewArrayFunctionRefNode.createError( + new IllegalArgumentException( + "not a statement: new array function reference with target type [" + canonicalTypeName + "] not used" + ) + ); } ScriptScope scriptScope = semanticScope.getScriptScope(); @@ -2389,9 +2651,17 @@ public void visitNewArrayFunctionRef(ENewArrayFunctionRef userNewArrayFunctionRe valueType = String.class; scriptScope.putDecoration(userNewArrayFunctionRefNode, new EncodingDecoration(true, false, "this", name, 0)); } else { - FunctionRef ref = FunctionRef.create(scriptScope.getPainlessLookup(), scriptScope.getFunctionTable(), - userNewArrayFunctionRefNode.getLocation(), targetType.getTargetType(), "this", name, 0, - scriptScope.getCompilerSettings().asMap(), false); + FunctionRef ref = FunctionRef.create( + scriptScope.getPainlessLookup(), + scriptScope.getFunctionTable(), + userNewArrayFunctionRefNode.getLocation(), + targetType.getTargetType(), + "this", + name, + 0, + scriptScope.getCompilerSettings().asMap(), + false + ); valueType = targetType.getTargetType(); semanticScope.putDecoration(userNewArrayFunctionRefNode, new ReferenceDecoration(ref)); } @@ -2411,15 +2681,24 @@ public void visitSymbol(ESymbol userSymbolNode, SemanticScope semanticScope) { String symbol = userSymbolNode.getSymbol(); Class staticType = semanticScope.getScriptScope().getPainlessLookup().canonicalTypeNameToType(symbol); - if (staticType != null) { + if (staticType != null) { if (write) { - throw userSymbolNode.createError(new IllegalArgumentException("invalid assignment: " + - "cannot write a value to a static type [" + PainlessLookupUtility.typeToCanonicalTypeName(staticType) + "]")); + throw userSymbolNode.createError( + new IllegalArgumentException( + "invalid assignment: " + + "cannot write a value to a static type [" + + PainlessLookupUtility.typeToCanonicalTypeName(staticType) + + "]" + ) + ); } if (read == false) { - throw userSymbolNode.createError(new IllegalArgumentException("not a statement: " + - "static type [" + PainlessLookupUtility.typeToCanonicalTypeName(staticType) + "] not used")); + throw userSymbolNode.createError( + new IllegalArgumentException( + "not a statement: " + "static type [" + PainlessLookupUtility.typeToCanonicalTypeName(staticType) + "] not used" + ) + ); } semanticScope.putDecoration(userSymbolNode, new StaticType(staticType)); @@ -2467,32 +2746,54 @@ public void visitDot(EDot userDotNode, SemanticScope semanticScope) { StaticType prefixStaticType = semanticScope.getDecoration(userPrefixNode, StaticType.class); if (prefixValueType != null && prefixStaticType != null) { - throw userDotNode.createError(new IllegalStateException("cannot have both " + - "value [" + prefixValueType.getValueCanonicalTypeName() + "] " + - "and type [" + prefixStaticType.getStaticCanonicalTypeName() + "]")); + throw userDotNode.createError( + new IllegalStateException( + "cannot have both " + + "value [" + + prefixValueType.getValueCanonicalTypeName() + + "] " + + "and type [" + + prefixStaticType.getStaticCanonicalTypeName() + + "]" + ) + ); } if (semanticScope.hasDecoration(userPrefixNode, PartialCanonicalTypeName.class)) { if (prefixValueType != null) { - throw userDotNode.createError(new IllegalArgumentException("value required: instead found unexpected type " + - "[" + prefixValueType.getValueCanonicalTypeName() + "]")); + throw userDotNode.createError( + new IllegalArgumentException( + "value required: instead found unexpected type " + "[" + prefixValueType.getValueCanonicalTypeName() + "]" + ) + ); } if (prefixStaticType != null) { - throw userDotNode.createError(new IllegalArgumentException("value required: instead found unexpected type " + - "[" + prefixStaticType.getStaticType() + "]")); + throw userDotNode.createError( + new IllegalArgumentException( + "value required: instead found unexpected type " + "[" + prefixStaticType.getStaticType() + "]" + ) + ); } - String canonicalTypeName = - semanticScope.getDecoration(userPrefixNode, PartialCanonicalTypeName.class).getPartialCanonicalTypeName() + "." + index; + String canonicalTypeName = semanticScope.getDecoration(userPrefixNode, PartialCanonicalTypeName.class) + .getPartialCanonicalTypeName() + + "." + + index; Class staticType = scriptScope.getPainlessLookup().canonicalTypeNameToType(canonicalTypeName); if (staticType == null) { semanticScope.putDecoration(userDotNode, new PartialCanonicalTypeName(canonicalTypeName)); } else { if (write) { - throw userDotNode.createError(new IllegalArgumentException("invalid assignment: " + - "cannot write a value to a static type [" + PainlessLookupUtility.typeToCanonicalTypeName(staticType) + "]")); + throw userDotNode.createError( + new IllegalArgumentException( + "invalid assignment: " + + "cannot write a value to a static type [" + + PainlessLookupUtility.typeToCanonicalTypeName(staticType) + + "]" + ) + ); } semanticScope.putDecoration(userDotNode, new StaticType(staticType)); @@ -2507,8 +2808,14 @@ public void visitDot(EDot userDotNode, SemanticScope semanticScope) { if (staticType != null) { if (write) { - throw userDotNode.createError(new IllegalArgumentException("invalid assignment: " + - "cannot write a value to a static type [" + PainlessLookupUtility.typeToCanonicalTypeName(staticType) + "]")); + throw userDotNode.createError( + new IllegalArgumentException( + "invalid assignment: " + + "cannot write a value to a static type [" + + PainlessLookupUtility.typeToCanonicalTypeName(staticType) + + "]" + ) + ); } semanticScope.putDecoration(userDotNode, new StaticType(staticType)); @@ -2518,19 +2825,26 @@ public void visitDot(EDot userDotNode, SemanticScope semanticScope) { if (prefixValueType != null && prefixValueType.getValueType().isArray()) { if ("length".equals(index)) { if (write) { - throw userDotNode.createError(new IllegalArgumentException( - "invalid assignment: cannot assign a value write to read-only field [length] for an array.")); + throw userDotNode.createError( + new IllegalArgumentException( + "invalid assignment: cannot assign a value write to read-only field [length] for an array." + ) + ); } valueType = int.class; } else { - throw userDotNode.createError(new IllegalArgumentException( - "Field [" + index + "] does not exist for type [" + prefixValueType.getValueCanonicalTypeName() + "].")); + throw userDotNode.createError( + new IllegalArgumentException( + "Field [" + index + "] does not exist for type [" + prefixValueType.getValueCanonicalTypeName() + "]." + ) + ); } } else if (prefixValueType != null && prefixValueType.getValueType() == def.class) { TargetType targetType = userDotNode.isNullSafe() ? null : semanticScope.getDecoration(userDotNode, TargetType.class); - valueType = targetType == null || semanticScope.getCondition(userDotNode, Explicit.class) ? - def.class : targetType.getTargetType(); + valueType = targetType == null || semanticScope.getCondition(userDotNode, Explicit.class) + ? def.class + : targetType.getTargetType(); if (write) { semanticScope.setCondition(userDotNode, DefOptimized.class); @@ -2552,33 +2866,55 @@ public void visitDot(EDot userDotNode, SemanticScope semanticScope) { throw userDotNode.createError(new IllegalStateException("value required: instead found no value")); } - PainlessField field = - semanticScope.getScriptScope().getPainlessLookup().lookupPainlessField(prefixType, isStatic, index); + PainlessField field = semanticScope.getScriptScope() + .getPainlessLookup() + .lookupPainlessField(prefixType, isStatic, index); if (field == null) { PainlessMethod getter; PainlessMethod setter; - getter = scriptScope.getPainlessLookup().lookupPainlessMethod(prefixType, isStatic, - "get" + Character.toUpperCase(index.charAt(0)) + index.substring(1), 0); + getter = scriptScope.getPainlessLookup() + .lookupPainlessMethod( + prefixType, + isStatic, + "get" + Character.toUpperCase(index.charAt(0)) + index.substring(1), + 0 + ); if (getter == null) { - getter = scriptScope.getPainlessLookup().lookupPainlessMethod(prefixType, isStatic, - "is" + Character.toUpperCase(index.charAt(0)) + index.substring(1), 0); + getter = scriptScope.getPainlessLookup() + .lookupPainlessMethod( + prefixType, + isStatic, + "is" + Character.toUpperCase(index.charAt(0)) + index.substring(1), + 0 + ); } - setter = scriptScope.getPainlessLookup().lookupPainlessMethod(prefixType, isStatic, - "set" + Character.toUpperCase(index.charAt(0)) + index.substring(1), 0); + setter = scriptScope.getPainlessLookup() + .lookupPainlessMethod( + prefixType, + isStatic, + "set" + Character.toUpperCase(index.charAt(0)) + index.substring(1), + 0 + ); if (getter != null || setter != null) { if (getter != null && (getter.returnType == void.class || getter.typeParameters.isEmpty() == false)) { - throw userDotNode.createError(new IllegalArgumentException( - "Illegal get shortcut on field [" + index + "] for type [" + prefixCanonicalTypeName + "].")); + throw userDotNode.createError( + new IllegalArgumentException( + "Illegal get shortcut on field [" + index + "] for type [" + prefixCanonicalTypeName + "]." + ) + ); } if (setter != null && (setter.returnType != void.class || setter.typeParameters.size() != 1)) { - throw userDotNode.createError(new IllegalArgumentException( - "Illegal set shortcut on field [" + index + "] for type [" + prefixCanonicalTypeName + "].")); + throw userDotNode.createError( + new IllegalArgumentException( + "Illegal set shortcut on field [" + index + "] for type [" + prefixCanonicalTypeName + "]." + ) + ); } if (getter != null && setter != null && setter.typeParameters.get(0) != getter.returnType) { @@ -2588,8 +2924,11 @@ public void visitDot(EDot userDotNode, SemanticScope semanticScope) { if ((read == false || getter != null) && (write == false || setter != null)) { valueType = setter != null ? setter.typeParameters.get(0) : getter.returnType; } else { - throw userDotNode.createError(new IllegalArgumentException( - "Illegal shortcut on field [" + index + "] for type [" + prefixCanonicalTypeName + "].")); + throw userDotNode.createError( + new IllegalArgumentException( + "Illegal shortcut on field [" + index + "] for type [" + prefixCanonicalTypeName + "]." + ) + ); } if (getter != null) { @@ -2607,26 +2946,30 @@ public void visitDot(EDot userDotNode, SemanticScope semanticScope) { setter = scriptScope.getPainlessLookup().lookupPainlessMethod(prefixType, false, "put", 2); if (getter != null && (getter.returnType == void.class || getter.typeParameters.size() != 1)) { - throw userDotNode.createError(new IllegalArgumentException( - "Illegal map get shortcut for type [" + prefixCanonicalTypeName + "].")); + throw userDotNode.createError( + new IllegalArgumentException("Illegal map get shortcut for type [" + prefixCanonicalTypeName + "].") + ); } if (setter != null && setter.typeParameters.size() != 2) { - throw userDotNode.createError(new IllegalArgumentException( - "Illegal map set shortcut for type [" + prefixCanonicalTypeName + "].")); + throw userDotNode.createError( + new IllegalArgumentException("Illegal map set shortcut for type [" + prefixCanonicalTypeName + "].") + ); } - if (getter != null && setter != null && - (getter.typeParameters.get(0).equals(setter.typeParameters.get(0)) == false || - getter.returnType.equals(setter.typeParameters.get(1)) == false)) { + if (getter != null + && setter != null + && (getter.typeParameters.get(0).equals(setter.typeParameters.get(0)) == false + || getter.returnType.equals(setter.typeParameters.get(1)) == false)) { throw userDotNode.createError(new IllegalArgumentException("Shortcut argument types must match.")); } if ((read == false || getter != null) && (write == false || setter != null)) { valueType = setter != null ? setter.typeParameters.get(1) : getter.returnType; } else { - throw userDotNode.createError(new IllegalArgumentException( - "Illegal map shortcut for type [" + prefixCanonicalTypeName + "].")); + throw userDotNode.createError( + new IllegalArgumentException("Illegal map shortcut for type [" + prefixCanonicalTypeName + "].") + ); } if (getter != null) { @@ -2650,28 +2993,38 @@ public void visitDot(EDot userDotNode, SemanticScope semanticScope) { getter = scriptScope.getPainlessLookup().lookupPainlessMethod(prefixType, false, "get", 1); setter = scriptScope.getPainlessLookup().lookupPainlessMethod(prefixType, false, "set", 2); - if (getter != null && (getter.returnType == void.class || getter.typeParameters.size() != 1 || - getter.typeParameters.get(0) != int.class)) { - throw userDotNode.createError(new IllegalArgumentException( - "Illegal list get shortcut for type [" + prefixCanonicalTypeName + "].")); + if (getter != null + && (getter.returnType == void.class + || getter.typeParameters.size() != 1 + || getter.typeParameters.get(0) != int.class)) { + throw userDotNode.createError( + new IllegalArgumentException( + "Illegal list get shortcut for type [" + prefixCanonicalTypeName + "]." + ) + ); } if (setter != null && (setter.typeParameters.size() != 2 || setter.typeParameters.get(0) != int.class)) { - throw userDotNode.createError(new IllegalArgumentException( - "Illegal list set shortcut for type [" + prefixCanonicalTypeName + "].")); + throw userDotNode.createError( + new IllegalArgumentException( + "Illegal list set shortcut for type [" + prefixCanonicalTypeName + "]." + ) + ); } - if (getter != null && setter != null && - (getter.typeParameters.get(0).equals(setter.typeParameters.get(0)) == false - || getter.returnType.equals(setter.typeParameters.get(1)) == false)) { + if (getter != null + && setter != null + && (getter.typeParameters.get(0).equals(setter.typeParameters.get(0)) == false + || getter.returnType.equals(setter.typeParameters.get(1)) == false)) { throw userDotNode.createError(new IllegalArgumentException("Shortcut argument types must match.")); } if ((read == false || getter != null) && (write == false || setter != null)) { valueType = setter != null ? setter.typeParameters.get(1) : getter.returnType; } else { - throw userDotNode.createError(new IllegalArgumentException( - "Illegal list shortcut for type [" + prefixCanonicalTypeName + "].")); + throw userDotNode.createError( + new IllegalArgumentException("Illegal list shortcut for type [" + prefixCanonicalTypeName + "].") + ); } if (getter != null) { @@ -2688,17 +3041,26 @@ public void visitDot(EDot userDotNode, SemanticScope semanticScope) { if (valueType == null) { if (prefixValueType != null) { - throw userDotNode.createError(new IllegalArgumentException( - "field [" + prefixValueType.getValueCanonicalTypeName() + ", " + index + "] not found")); + throw userDotNode.createError( + new IllegalArgumentException( + "field [" + prefixValueType.getValueCanonicalTypeName() + ", " + index + "] not found" + ) + ); } else { - throw userDotNode.createError(new IllegalArgumentException( - "field [" + prefixStaticType.getStaticCanonicalTypeName() + ", " + index + "] not found")); + throw userDotNode.createError( + new IllegalArgumentException( + "field [" + prefixStaticType.getStaticCanonicalTypeName() + ", " + index + "] not found" + ) + ); } } } else { if (write && Modifier.isFinal(field.javaField.getModifiers())) { - throw userDotNode.createError(new IllegalArgumentException( - "invalid assignment: cannot assign a value to read-only field [" + field.javaField.getName() + "]")); + throw userDotNode.createError( + new IllegalArgumentException( + "invalid assignment: cannot assign a value to read-only field [" + field.javaField.getName() + "]" + ) + ); } semanticScope.putDecoration(userDotNode, new StandardPainlessField(field)); @@ -2710,8 +3072,9 @@ public void visitDot(EDot userDotNode, SemanticScope semanticScope) { if (userDotNode.isNullSafe()) { if (write) { - throw userDotNode.createError(new IllegalArgumentException( - "invalid assignment: cannot assign a value to a null safe operation [?.]")); + throw userDotNode.createError( + new IllegalArgumentException("invalid assignment: cannot assign a value to a null safe operation [?.]") + ); } if (valueType.isPrimitive()) { @@ -2755,8 +3118,9 @@ public void visitBrace(EBrace userBraceNode, SemanticScope semanticScope) { checkedVisit(userIndexNode, semanticScope); TargetType targetType = semanticScope.getDecoration(userBraceNode, TargetType.class); // TODO: remove ZonedDateTime exception when JodaCompatibleDateTime is removed - valueType = targetType == null || targetType.getTargetType() == ZonedDateTime.class || - semanticScope.getCondition(userBraceNode, Explicit.class) ? def.class : targetType.getTargetType(); + valueType = targetType == null + || targetType.getTargetType() == ZonedDateTime.class + || semanticScope.getCondition(userBraceNode, Explicit.class) ? def.class : targetType.getTargetType(); if (write) { semanticScope.setCondition(userBraceNode, DefOptimized.class); @@ -2764,30 +3128,38 @@ public void visitBrace(EBrace userBraceNode, SemanticScope semanticScope) { } else if (Map.class.isAssignableFrom(prefixValueType)) { String canonicalClassName = PainlessLookupUtility.typeToCanonicalTypeName(prefixValueType); - PainlessMethod getter = - semanticScope.getScriptScope().getPainlessLookup().lookupPainlessMethod(prefixValueType, false, "get", 1); - PainlessMethod setter = - semanticScope.getScriptScope().getPainlessLookup().lookupPainlessMethod(prefixValueType, false, "put", 2); + PainlessMethod getter = semanticScope.getScriptScope() + .getPainlessLookup() + .lookupPainlessMethod(prefixValueType, false, "get", 1); + PainlessMethod setter = semanticScope.getScriptScope() + .getPainlessLookup() + .lookupPainlessMethod(prefixValueType, false, "put", 2); if (getter != null && (getter.returnType == void.class || getter.typeParameters.size() != 1)) { - throw userBraceNode.createError(new IllegalArgumentException( - "Illegal map get shortcut for type [" + canonicalClassName + "].")); + throw userBraceNode.createError( + new IllegalArgumentException("Illegal map get shortcut for type [" + canonicalClassName + "].") + ); } if (setter != null && setter.typeParameters.size() != 2) { - throw userBraceNode.createError(new IllegalArgumentException( - "Illegal map set shortcut for type [" + canonicalClassName + "].")); + throw userBraceNode.createError( + new IllegalArgumentException("Illegal map set shortcut for type [" + canonicalClassName + "].") + ); } - if (getter != null && setter != null && (getter.typeParameters.get(0).equals(setter.typeParameters.get(0)) == false || - getter.returnType.equals(setter.typeParameters.get(1)) == false)) { + if (getter != null + && setter != null + && (getter.typeParameters.get(0).equals(setter.typeParameters.get(0)) == false + || getter.returnType.equals(setter.typeParameters.get(1)) == false)) { throw userBraceNode.createError(new IllegalArgumentException("Shortcut argument types must match.")); } if ((read == false || getter != null) && (write == false || setter != null)) { semanticScope.setCondition(userIndexNode, Read.class); - semanticScope.putDecoration(userIndexNode, - new TargetType(setter != null ? setter.typeParameters.get(0) : getter.typeParameters.get(0))); + semanticScope.putDecoration( + userIndexNode, + new TargetType(setter != null ? setter.typeParameters.get(0) : getter.typeParameters.get(0)) + ); checkedVisit(userIndexNode, semanticScope); decorateWithCast(userIndexNode, semanticScope); @@ -2801,31 +3173,38 @@ public void visitBrace(EBrace userBraceNode, SemanticScope semanticScope) { semanticScope.putDecoration(userBraceNode, new SetterPainlessMethod(setter)); } } else { - throw userBraceNode.createError(new IllegalArgumentException( - "Illegal map shortcut for type [" + canonicalClassName + "].")); + throw userBraceNode.createError( + new IllegalArgumentException("Illegal map shortcut for type [" + canonicalClassName + "].") + ); } semanticScope.setCondition(userBraceNode, MapShortcut.class); } else if (List.class.isAssignableFrom(prefixValueType)) { String canonicalClassName = PainlessLookupUtility.typeToCanonicalTypeName(prefixValueType); - PainlessMethod getter = - semanticScope.getScriptScope().getPainlessLookup().lookupPainlessMethod(prefixValueType, false, "get", 1); - PainlessMethod setter = - semanticScope.getScriptScope().getPainlessLookup().lookupPainlessMethod(prefixValueType, false, "set", 2); + PainlessMethod getter = semanticScope.getScriptScope() + .getPainlessLookup() + .lookupPainlessMethod(prefixValueType, false, "get", 1); + PainlessMethod setter = semanticScope.getScriptScope() + .getPainlessLookup() + .lookupPainlessMethod(prefixValueType, false, "set", 2); - if (getter != null && (getter.returnType == void.class || getter.typeParameters.size() != 1 || - getter.typeParameters.get(0) != int.class)) { - throw userBraceNode.createError(new IllegalArgumentException( - "Illegal list get shortcut for type [" + canonicalClassName + "].")); + if (getter != null + && (getter.returnType == void.class || getter.typeParameters.size() != 1 || getter.typeParameters.get(0) != int.class)) { + throw userBraceNode.createError( + new IllegalArgumentException("Illegal list get shortcut for type [" + canonicalClassName + "].") + ); } if (setter != null && (setter.typeParameters.size() != 2 || setter.typeParameters.get(0) != int.class)) { - throw userBraceNode.createError(new IllegalArgumentException( - "Illegal list set shortcut for type [" + canonicalClassName + "].")); + throw userBraceNode.createError( + new IllegalArgumentException("Illegal list set shortcut for type [" + canonicalClassName + "].") + ); } - if (getter != null && setter != null && (getter.typeParameters.get(0).equals(setter.typeParameters.get(0)) == false + if (getter != null + && setter != null + && (getter.typeParameters.get(0).equals(setter.typeParameters.get(0)) == false || getter.returnType.equals(setter.typeParameters.get(1)) == false)) { throw userBraceNode.createError(new IllegalArgumentException("Shortcut argument types must match.")); } @@ -2846,14 +3225,18 @@ public void visitBrace(EBrace userBraceNode, SemanticScope semanticScope) { semanticScope.putDecoration(userBraceNode, new SetterPainlessMethod(setter)); } } else { - throw userBraceNode.createError(new IllegalArgumentException( - "Illegal list shortcut for type [" + canonicalClassName + "].")); + throw userBraceNode.createError( + new IllegalArgumentException("Illegal list shortcut for type [" + canonicalClassName + "].") + ); } semanticScope.setCondition(userBraceNode, ListShortcut.class); } else { - throw userBraceNode.createError(new IllegalArgumentException("Illegal array access on type " + - "[" + PainlessLookupUtility.typeToCanonicalTypeName(prefixValueType) + "].")); + throw userBraceNode.createError( + new IllegalArgumentException( + "Illegal array access on type " + "[" + PainlessLookupUtility.typeToCanonicalTypeName(prefixValueType) + "]." + ) + ); } semanticScope.putDecoration(userBraceNode, new ValueType(valueType)); @@ -2870,8 +3253,11 @@ public void visitCall(ECall userCallNode, SemanticScope semanticScope) { int userArgumentsSize = userArgumentNodes.size(); if (semanticScope.getCondition(userCallNode, Write.class)) { - throw userCallNode.createError(new IllegalArgumentException( - "invalid assignment: cannot assign a value to method call [" + methodName + "/" + userArgumentsSize + "]")); + throw userCallNode.createError( + new IllegalArgumentException( + "invalid assignment: cannot assign a value to method call [" + methodName + "/" + userArgumentsSize + "]" + ) + ); } AExpression userPrefixNode = userCallNode.getPrefixNode(); @@ -2881,14 +3267,28 @@ public void visitCall(ECall userCallNode, SemanticScope semanticScope) { StaticType prefixStaticType = semanticScope.getDecoration(userPrefixNode, StaticType.class); if (prefixValueType != null && prefixStaticType != null) { - throw userCallNode.createError(new IllegalStateException("cannot have both " + - "value [" + prefixValueType.getValueCanonicalTypeName() + "] " + - "and type [" + prefixStaticType.getStaticCanonicalTypeName() + "]")); + throw userCallNode.createError( + new IllegalStateException( + "cannot have both " + + "value [" + + prefixValueType.getValueCanonicalTypeName() + + "] " + + "and type [" + + prefixStaticType.getStaticCanonicalTypeName() + + "]" + ) + ); } if (semanticScope.hasDecoration(userPrefixNode, PartialCanonicalTypeName.class)) { - throw userCallNode.createError(new IllegalArgumentException("cannot resolve symbol " + - "[" + semanticScope.getDecoration(userPrefixNode, PartialCanonicalTypeName.class).getPartialCanonicalTypeName() + "]")); + throw userCallNode.createError( + new IllegalArgumentException( + "cannot resolve symbol " + + "[" + + semanticScope.getDecoration(userPrefixNode, PartialCanonicalTypeName.class).getPartialCanonicalTypeName() + + "]" + ) + ); } boolean dynamic = false; @@ -2904,24 +3304,45 @@ public void visitCall(ECall userCallNode, SemanticScope semanticScope) { method = lookup.lookupPainlessMethod(type, false, methodName, userArgumentsSize); if (method == null) { - dynamic = lookup.lookupPainlessClass(type).annotations.containsKey(DynamicTypeAnnotation.class) && - lookup.lookupPainlessSubClassesMethod(type, methodName, userArgumentsSize) != null; + dynamic = lookup.lookupPainlessClass(type).annotations.containsKey(DynamicTypeAnnotation.class) + && lookup.lookupPainlessSubClassesMethod(type, methodName, userArgumentsSize) != null; if (dynamic == false) { - throw userCallNode.createError(new IllegalArgumentException("member method " + - "[" + prefixValueType.getValueCanonicalTypeName() + ", " + methodName + "/" + userArgumentsSize + "] " + - "not found")); + throw userCallNode.createError( + new IllegalArgumentException( + "member method " + + "[" + + prefixValueType.getValueCanonicalTypeName() + + ", " + + methodName + + "/" + + userArgumentsSize + + "] " + + "not found" + ) + ); } } } } else if (prefixStaticType != null) { - method = semanticScope.getScriptScope().getPainlessLookup().lookupPainlessMethod( - prefixStaticType.getStaticType(), true, methodName, userArgumentsSize); + method = semanticScope.getScriptScope() + .getPainlessLookup() + .lookupPainlessMethod(prefixStaticType.getStaticType(), true, methodName, userArgumentsSize); if (method == null) { - throw userCallNode.createError(new IllegalArgumentException("static method " + - "[" + prefixStaticType.getStaticCanonicalTypeName() + ", " + methodName + "/" + userArgumentsSize + "] " + - "not found")); + throw userCallNode.createError( + new IllegalArgumentException( + "static method " + + "[" + + prefixStaticType.getStaticCanonicalTypeName() + + ", " + + methodName + + "/" + + userArgumentsSize + + "] " + + "not found" + ) + ); } } else { throw userCallNode.createError(new IllegalStateException("value required: instead found no value")); @@ -2937,14 +3358,16 @@ public void visitCall(ECall userCallNode, SemanticScope semanticScope) { Class argumentValueType = semanticScope.getDecoration(userArgumentNode, ValueType.class).getValueType(); if (argumentValueType == void.class) { - throw userCallNode.createError(new IllegalArgumentException( - "Argument(s) cannot be of [void] type when calling method [" + methodName + "].")); + throw userCallNode.createError( + new IllegalArgumentException("Argument(s) cannot be of [void] type when calling method [" + methodName + "].") + ); } } TargetType targetType = userCallNode.isNullSafe() ? null : semanticScope.getDecoration(userCallNode, TargetType.class); - valueType = targetType == null || semanticScope.getCondition(userCallNode, Explicit.class) ? - def.class : targetType.getTargetType(); + valueType = targetType == null || semanticScope.getCondition(userCallNode, Explicit.class) + ? def.class + : targetType.getTargetType(); semanticScope.setCondition(userCallNode, DynamicInvocation.class); } else { diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/DefaultSemanticHeaderPhase.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/DefaultSemanticHeaderPhase.java index 337af7f42d45c..24d5508c5de79 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/DefaultSemanticHeaderPhase.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/DefaultSemanticHeaderPhase.java @@ -34,17 +34,28 @@ public void visitFunction(SFunction userFunctionNode, ScriptScope scriptScope) { int parameterCount = canonicalTypeNameParameters.size(); if (parameterCount != parameterNames.size()) { - throw userFunctionNode.createError(new IllegalStateException("invalid function definition: " + - "parameter types size [" + canonicalTypeNameParameters.size() + "] is not equal to " + - "parameter names size [" + parameterNames.size() + "] for function [" + functionName +"]")); + throw userFunctionNode.createError( + new IllegalStateException( + "invalid function definition: " + + "parameter types size [" + + canonicalTypeNameParameters.size() + + "] is not equal to " + + "parameter names size [" + + parameterNames.size() + + "] for function [" + + functionName + + "]" + ) + ); } FunctionTable functionTable = scriptScope.getFunctionTable(); String functionKey = FunctionTable.buildLocalFunctionKey(functionName, canonicalTypeNameParameters.size()); if (functionTable.getFunction(functionKey) != null) { - throw userFunctionNode.createError(new IllegalArgumentException("invalid function definition: " + - "found duplicate function [" + functionKey + "].")); + throw userFunctionNode.createError( + new IllegalArgumentException("invalid function definition: " + "found duplicate function [" + functionKey + "].") + ); } PainlessLookup painlessLookup = scriptScope.getPainlessLookup(); @@ -52,8 +63,16 @@ public void visitFunction(SFunction userFunctionNode, ScriptScope scriptScope) { Class returnType = painlessLookup.canonicalTypeNameToType(returnCanonicalTypeName); if (returnType == null) { - throw userFunctionNode.createError(new IllegalArgumentException("invalid function definition: " + - "return type [" + returnCanonicalTypeName + "] not found for function [" + functionKey + "]")); + throw userFunctionNode.createError( + new IllegalArgumentException( + "invalid function definition: " + + "return type [" + + returnCanonicalTypeName + + "] not found for function [" + + functionKey + + "]" + ) + ); } List> typeParameters = new ArrayList<>(); @@ -62,14 +81,27 @@ public void visitFunction(SFunction userFunctionNode, ScriptScope scriptScope) { Class paramType = painlessLookup.canonicalTypeNameToType(typeParameter); if (paramType == null) { - throw userFunctionNode.createError(new IllegalArgumentException("invalid function definition: " + - "parameter type [" + typeParameter + "] not found for function [" + functionKey + "]")); + throw userFunctionNode.createError( + new IllegalArgumentException( + "invalid function definition: " + + "parameter type [" + + typeParameter + + "] not found for function [" + + functionKey + + "]" + ) + ); } typeParameters.add(paramType); } - functionTable.addMangledFunction(functionName, returnType, typeParameters, userFunctionNode.isInternal(), - userFunctionNode.isStatic()); + functionTable.addMangledFunction( + functionName, + returnType, + typeParameters, + userFunctionNode.isInternal(), + userFunctionNode.isStatic() + ); } } diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/DefaultStringConcatenationOptimizationPhase.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/DefaultStringConcatenationOptimizationPhase.java index a25909eae723d..2c6f389955f7f 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/DefaultStringConcatenationOptimizationPhase.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/DefaultStringConcatenationOptimizationPhase.java @@ -15,14 +15,14 @@ public class DefaultStringConcatenationOptimizationPhase extends IRTreeBaseVisit @Override public void visitStringConcatenation(StringConcatenationNode irStringConcatenationNode, Void scope) { - int i = 0; + int i = 0; while (i < irStringConcatenationNode.getArgumentNodes().size()) { ExpressionNode irArgumentNode = irStringConcatenationNode.getArgumentNodes().get(i); if (irArgumentNode instanceof StringConcatenationNode) { irStringConcatenationNode.getArgumentNodes().remove(i); - irStringConcatenationNode.getArgumentNodes().addAll(i, ((StringConcatenationNode)irArgumentNode).getArgumentNodes()); + irStringConcatenationNode.getArgumentNodes().addAll(i, ((StringConcatenationNode) irArgumentNode).getArgumentNodes()); } else { i++; } diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/DefaultUserTreeToIRTreePhase.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/DefaultUserTreeToIRTreePhase.java index f0ea582e4643b..bb2f21baa2e6a 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/DefaultUserTreeToIRTreePhase.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/DefaultUserTreeToIRTreePhase.java @@ -160,8 +160,8 @@ import org.elasticsearch.painless.symbol.Decorations.ExpressionPainlessCast; import org.elasticsearch.painless.symbol.Decorations.GetterPainlessMethod; import org.elasticsearch.painless.symbol.Decorations.IRNodeDecoration; -import org.elasticsearch.painless.symbol.Decorations.InstanceCapturingLambda; import org.elasticsearch.painless.symbol.Decorations.InstanceCapturingFunctionRef; +import org.elasticsearch.painless.symbol.Decorations.InstanceCapturingLambda; import org.elasticsearch.painless.symbol.Decorations.InstanceType; import org.elasticsearch.painless.symbol.Decorations.IterablePainlessMethod; import org.elasticsearch.painless.symbol.Decorations.ListShortcut; @@ -306,10 +306,12 @@ protected void injectBootstrapMethod(ScriptScope scriptScope) { FunctionNode irFunctionNode = new FunctionNode(internalLocation); irFunctionNode.attachDecoration(new IRDName("$bootstrapDef")); irFunctionNode.attachDecoration(new IRDReturnType(CallSite.class)); - irFunctionNode.attachDecoration(new IRDTypeParameters( - Arrays.asList(Lookup.class, String.class, MethodType.class, int.class, int.class, Object[].class))); - irFunctionNode.attachDecoration(new IRDParameterNames( - Arrays.asList("methodHandlesLookup", "name", "type", "initialDepth", "flavor", "args"))); + irFunctionNode.attachDecoration( + new IRDTypeParameters(Arrays.asList(Lookup.class, String.class, MethodType.class, int.class, int.class, Object[].class)) + ); + irFunctionNode.attachDecoration( + new IRDParameterNames(Arrays.asList("methodHandlesLookup", "name", "type", "initialDepth", "flavor", "args")) + ); irFunctionNode.attachCondition(IRCStatic.class); irFunctionNode.attachCondition(IRCVarArgs.class); irFunctionNode.attachCondition(IRCSynthetic.class); @@ -338,33 +340,37 @@ protected void injectBootstrapMethod(ScriptScope scriptScope) { InvokeCallNode invokeCallNode = new InvokeCallNode(internalLocation); invokeCallNode.attachDecoration(new IRDExpressionType(CallSite.class)); - invokeCallNode.setMethod(new PainlessMethod( - DefBootstrap.class.getMethod("bootstrap", - PainlessLookup.class, - FunctionTable.class, - Map.class, - Lookup.class, - String.class, - MethodType.class, - int.class, - int.class, - Object[].class), - DefBootstrap.class, - CallSite.class, - Arrays.asList( - PainlessLookup.class, - FunctionTable.class, - Map.class, - Lookup.class, - String.class, - MethodType.class, - int.class, - int.class, - Object[].class), - null, - null, - null - ) + invokeCallNode.setMethod( + new PainlessMethod( + DefBootstrap.class.getMethod( + "bootstrap", + PainlessLookup.class, + FunctionTable.class, + Map.class, + Lookup.class, + String.class, + MethodType.class, + int.class, + int.class, + Object[].class + ), + DefBootstrap.class, + CallSite.class, + Arrays.asList( + PainlessLookup.class, + FunctionTable.class, + Map.class, + Lookup.class, + String.class, + MethodType.class, + int.class, + int.class, + Object[].class + ), + null, + null, + null + ) ); invokeCallNode.setBox(DefBootstrap.class); @@ -432,7 +438,7 @@ protected void injectBootstrapMethod(ScriptScope scriptScope) { } protected ExpressionNode injectCast(AExpression userExpressionNode, ScriptScope scriptScope) { - ExpressionNode irExpressionNode = (ExpressionNode)visit(userExpressionNode, scriptScope); + ExpressionNode irExpressionNode = (ExpressionNode) visit(userExpressionNode, scriptScope); if (irExpressionNode == null) { return null; @@ -474,8 +480,15 @@ protected ExpressionNode injectCast(AExpression userExpressionNode, ScriptScope * @param irStoreNode The store node if this is a write. * @return The root node for this assignment. */ - protected ExpressionNode buildLoadStore(int accessDepth, Location location, boolean isNullSafe, - ExpressionNode irPrefixNode, ExpressionNode irIndexNode, ExpressionNode irLoadNode, UnaryNode irStoreNode) { + protected ExpressionNode buildLoadStore( + int accessDepth, + Location location, + boolean isNullSafe, + ExpressionNode irPrefixNode, + ExpressionNode irIndexNode, + ExpressionNode irLoadNode, + UnaryNode irStoreNode + ) { // build out the load structure for load/compound assignment or the store structure for just store ExpressionNode irExpressionNode = irLoadNode != null ? irLoadNode : irStoreNode; @@ -554,7 +567,7 @@ public void visitClass(SClass userClassNode, ScriptScope scriptScope) { irClassNode = new ClassNode(userClassNode.getLocation()); for (SFunction userFunctionNode : userClassNode.getFunctionNodes()) { - irClassNode.addFunctionNode((FunctionNode)visit(userFunctionNode, scriptScope)); + irClassNode.addFunctionNode((FunctionNode) visit(userFunctionNode, scriptScope)); } irClassNode.setScriptScope(scriptScope); @@ -571,7 +584,7 @@ public void visitFunction(SFunction userFunctionNode, ScriptScope scriptScope) { Class returnType = localFunction.getReturnType(); boolean methodEscape = scriptScope.getCondition(userFunctionNode, MethodEscape.class); - BlockNode irBlockNode = (BlockNode)visit(userFunctionNode.getBlockNode(), scriptScope); + BlockNode irBlockNode = (BlockNode) visit(userFunctionNode.getBlockNode(), scriptScope); if (methodEscape == false) { ExpressionNode irExpressionNode; @@ -586,19 +599,19 @@ public void visitFunction(SFunction userFunctionNode, ScriptScope scriptScope) { if (returnType == boolean.class) { irConstantNode.attachDecoration(new IRDConstant(false)); } else if (returnType == byte.class - || returnType == char.class - || returnType == short.class - || returnType == int.class) { - irConstantNode.attachDecoration(new IRDConstant(0)); - } else if (returnType == long.class) { - irConstantNode.attachDecoration(new IRDConstant(0L)); - } else if (returnType == float.class) { - irConstantNode.attachDecoration(new IRDConstant(0f)); - } else if (returnType == double.class) { - irConstantNode.attachDecoration(new IRDConstant(0d)); - } else { - throw userFunctionNode.createError(new IllegalStateException("illegal tree structure")); - } + || returnType == char.class + || returnType == short.class + || returnType == int.class) { + irConstantNode.attachDecoration(new IRDConstant(0)); + } else if (returnType == long.class) { + irConstantNode.attachDecoration(new IRDConstant(0L)); + } else if (returnType == float.class) { + irConstantNode.attachDecoration(new IRDConstant(0f)); + } else if (returnType == double.class) { + irConstantNode.attachDecoration(new IRDConstant(0d)); + } else { + throw userFunctionNode.createError(new IllegalStateException("illegal tree structure")); + } irExpressionNode = irConstantNode; } else { @@ -617,10 +630,9 @@ public void visitFunction(SFunction userFunctionNode, ScriptScope scriptScope) { FunctionNode irFunctionNode = new FunctionNode(userFunctionNode.getLocation()); irFunctionNode.setBlockNode(irBlockNode); - String mangledName = scriptScope.getFunctionTable().getFunction( - userFunctionNode.getFunctionName(), - userFunctionNode.getCanonicalTypeNameParameters().size() - ).getMangledName(); + String mangledName = scriptScope.getFunctionTable() + .getFunction(userFunctionNode.getFunctionName(), userFunctionNode.getCanonicalTypeNameParameters().size()) + .getMangledName(); irFunctionNode.attachDecoration(new IRDName(mangledName)); irFunctionNode.attachDecoration(new IRDReturnType(returnType)); irFunctionNode.attachDecoration(new IRDTypeParameters(new ArrayList<>(localFunction.getTypeParameters()))); @@ -644,7 +656,7 @@ public void visitBlock(SBlock userBlockNode, ScriptScope scriptScope) { BlockNode irBlockNode = new BlockNode(userBlockNode.getLocation()); for (AStatement userStatementNode : userBlockNode.getStatementNodes()) { - irBlockNode.addStatementNode((StatementNode)visit(userStatementNode, scriptScope)); + irBlockNode.addStatementNode((StatementNode) visit(userStatementNode, scriptScope)); } if (scriptScope.getCondition(userBlockNode, AllEscape.class)) { @@ -658,7 +670,7 @@ public void visitBlock(SBlock userBlockNode, ScriptScope scriptScope) { public void visitIf(SIf userIfNode, ScriptScope scriptScope) { IfNode irIfNode = new IfNode(userIfNode.getLocation()); irIfNode.setConditionNode(injectCast(userIfNode.getConditionNode(), scriptScope)); - irIfNode.setBlockNode((BlockNode)visit(userIfNode.getIfBlockNode(), scriptScope)); + irIfNode.setBlockNode((BlockNode) visit(userIfNode.getIfBlockNode(), scriptScope)); scriptScope.putDecoration(userIfNode, new IRNodeDecoration(irIfNode)); } @@ -667,8 +679,8 @@ public void visitIf(SIf userIfNode, ScriptScope scriptScope) { public void visitIfElse(SIfElse userIfElseNode, ScriptScope scriptScope) { IfElseNode irIfElseNode = new IfElseNode(userIfElseNode.getLocation()); irIfElseNode.setConditionNode(injectCast(userIfElseNode.getConditionNode(), scriptScope)); - irIfElseNode.setBlockNode((BlockNode)visit(userIfElseNode.getIfBlockNode(), scriptScope)); - irIfElseNode.setElseBlockNode((BlockNode)visit(userIfElseNode.getElseBlockNode(), scriptScope)); + irIfElseNode.setBlockNode((BlockNode) visit(userIfElseNode.getIfBlockNode(), scriptScope)); + irIfElseNode.setElseBlockNode((BlockNode) visit(userIfElseNode.getElseBlockNode(), scriptScope)); scriptScope.putDecoration(userIfElseNode, new IRNodeDecoration(irIfElseNode)); } @@ -677,7 +689,7 @@ public void visitIfElse(SIfElse userIfElseNode, ScriptScope scriptScope) { public void visitWhile(SWhile userWhileNode, ScriptScope scriptScope) { WhileLoopNode irWhileLoopNode = new WhileLoopNode(userWhileNode.getLocation()); irWhileLoopNode.setConditionNode(injectCast(userWhileNode.getConditionNode(), scriptScope)); - irWhileLoopNode.setBlockNode((BlockNode)visit(userWhileNode.getBlockNode(), scriptScope)); + irWhileLoopNode.setBlockNode((BlockNode) visit(userWhileNode.getBlockNode(), scriptScope)); if (scriptScope.getCondition(userWhileNode, ContinuousLoop.class)) { irWhileLoopNode.attachCondition(IRCContinuous.class); @@ -690,7 +702,7 @@ public void visitWhile(SWhile userWhileNode, ScriptScope scriptScope) { public void visitDo(SDo userDoNode, ScriptScope scriptScope) { DoWhileLoopNode irDoWhileLoopNode = new DoWhileLoopNode(userDoNode.getLocation()); irDoWhileLoopNode.setConditionNode(injectCast(userDoNode.getConditionNode(), scriptScope)); - irDoWhileLoopNode.setBlockNode((BlockNode)visit(userDoNode.getBlockNode(), scriptScope)); + irDoWhileLoopNode.setBlockNode((BlockNode) visit(userDoNode.getBlockNode(), scriptScope)); if (scriptScope.getCondition(userDoNode, ContinuousLoop.class)) { irDoWhileLoopNode.attachCondition(IRCContinuous.class); @@ -704,8 +716,8 @@ public void visitFor(SFor userForNode, ScriptScope scriptScope) { ForLoopNode irForLoopNode = new ForLoopNode(userForNode.getLocation()); irForLoopNode.setInitialzerNode(visit(userForNode.getInitializerNode(), scriptScope)); irForLoopNode.setConditionNode(injectCast(userForNode.getConditionNode(), scriptScope)); - irForLoopNode.setAfterthoughtNode((ExpressionNode)visit(userForNode.getAfterthoughtNode(), scriptScope)); - irForLoopNode.setBlockNode((BlockNode)visit(userForNode.getBlockNode(), scriptScope)); + irForLoopNode.setAfterthoughtNode((ExpressionNode) visit(userForNode.getAfterthoughtNode(), scriptScope)); + irForLoopNode.setBlockNode((BlockNode) visit(userForNode.getBlockNode(), scriptScope)); if (scriptScope.getCondition(userForNode, ContinuousLoop.class)) { irForLoopNode.attachCondition(IRCContinuous.class); @@ -717,11 +729,12 @@ public void visitFor(SFor userForNode, ScriptScope scriptScope) { @Override public void visitEach(SEach userEachNode, ScriptScope scriptScope) { Variable variable = scriptScope.getDecoration(userEachNode, SemanticVariable.class).getSemanticVariable(); - PainlessCast painlessCast = scriptScope.hasDecoration(userEachNode, ExpressionPainlessCast.class) ? - scriptScope.getDecoration(userEachNode, ExpressionPainlessCast.class).getExpressionPainlessCast() : null; - ExpressionNode irIterableNode = (ExpressionNode)visit(userEachNode.getIterableNode(), scriptScope); + PainlessCast painlessCast = scriptScope.hasDecoration(userEachNode, ExpressionPainlessCast.class) + ? scriptScope.getDecoration(userEachNode, ExpressionPainlessCast.class).getExpressionPainlessCast() + : null; + ExpressionNode irIterableNode = (ExpressionNode) visit(userEachNode.getIterableNode(), scriptScope); Class iterableValueType = scriptScope.getDecoration(userEachNode.getIterableNode(), ValueType.class).getValueType(); - BlockNode irBlockNode = (BlockNode)visit(userEachNode.getBlockNode(), scriptScope); + BlockNode irBlockNode = (BlockNode) visit(userEachNode.getBlockNode(), scriptScope); ConditionNode irConditionNode; @@ -752,8 +765,9 @@ public void visitEach(SEach userEachNode, ScriptScope scriptScope) { irForEachSubIterableNode.attachDecoration(new IRDIterableName("#itr" + userEachNode.getLocation().getOffset())); if (iterableValueType != def.class) { - irForEachSubIterableNode.attachDecoration(new IRDMethod( - scriptScope.getDecoration(userEachNode, IterablePainlessMethod.class).getIterablePainlessMethod())); + irForEachSubIterableNode.attachDecoration( + new IRDMethod(scriptScope.getDecoration(userEachNode, IterablePainlessMethod.class).getIterablePainlessMethod()) + ); } if (painlessCast != null) { @@ -776,7 +790,7 @@ public void visitDeclBlock(SDeclBlock userDeclBlockNode, ScriptScope scriptScope DeclarationBlockNode irDeclarationBlockNode = new DeclarationBlockNode(userDeclBlockNode.getLocation()); for (SDeclaration userDeclarationNode : userDeclBlockNode.getDeclarationNodes()) { - irDeclarationBlockNode.addDeclarationNode((DeclarationNode)visit(userDeclarationNode, scriptScope)); + irDeclarationBlockNode.addDeclarationNode((DeclarationNode) visit(userDeclarationNode, scriptScope)); } scriptScope.putDecoration(userDeclBlockNode, new IRNodeDecoration(irDeclarationBlockNode)); @@ -825,10 +839,10 @@ public void visitTry(STry userTryNode, ScriptScope scriptScope) { TryNode irTryNode = new TryNode(userTryNode.getLocation()); for (SCatch userCatchNode : userTryNode.getCatchNodes()) { - irTryNode.addCatchNode((CatchNode)visit(userCatchNode, scriptScope)); + irTryNode.addCatchNode((CatchNode) visit(userCatchNode, scriptScope)); } - irTryNode.setBlockNode((BlockNode)visit(userTryNode.getBlockNode(), scriptScope)); + irTryNode.setBlockNode((BlockNode) visit(userTryNode.getBlockNode(), scriptScope)); scriptScope.putDecoration(userTryNode, new IRNodeDecoration(irTryNode)); } @@ -840,7 +854,7 @@ public void visitCatch(SCatch userCatchNode, ScriptScope scriptScope) { CatchNode irCatchNode = new CatchNode(userCatchNode.getLocation()); irCatchNode.attachDecoration(new IRDExceptionType(variable.getType())); irCatchNode.attachDecoration(new IRDSymbol(variable.getName())); - irCatchNode.setBlockNode((BlockNode)visit(userCatchNode.getBlockNode(), scriptScope)); + irCatchNode.setBlockNode((BlockNode) visit(userCatchNode.getBlockNode(), scriptScope)); scriptScope.putDecoration(userCatchNode, new IRNodeDecoration(irCatchNode)); } @@ -870,8 +884,9 @@ public void visitBreak(SBreak userBreakNode, ScriptScope scriptScope) { @Override public void visitAssignment(EAssignment userAssignmentNode, ScriptScope scriptScope) { boolean read = scriptScope.getCondition(userAssignmentNode, Read.class); - Class compoundType = scriptScope.hasDecoration(userAssignmentNode, CompoundType.class) ? - scriptScope.getDecoration(userAssignmentNode, CompoundType.class).getCompoundType() : null; + Class compoundType = scriptScope.hasDecoration(userAssignmentNode, CompoundType.class) + ? scriptScope.getDecoration(userAssignmentNode, CompoundType.class).getCompoundType() + : null; ExpressionNode irAssignmentNode; // add a cast node if necessary for the value node for the assignment @@ -881,7 +896,7 @@ public void visitAssignment(EAssignment userAssignmentNode, ScriptScope scriptSc if (compoundType != null) { boolean concatenate = userAssignmentNode.getOperation() == Operation.ADD && compoundType == String.class; scriptScope.setCondition(userAssignmentNode.getLeftNode(), Compound.class); - UnaryNode irStoreNode = (UnaryNode)visit(userAssignmentNode.getLeftNode(), scriptScope); + UnaryNode irStoreNode = (UnaryNode) visit(userAssignmentNode.getLeftNode(), scriptScope); ExpressionNode irLoadNode = irStoreNode.getChildNode(); ExpressionNode irCompoundNode; @@ -893,9 +908,9 @@ public void visitAssignment(EAssignment userAssignmentNode, ScriptScope scriptSc // must handle the StringBuilder case for java version <= 8 if (irLoadNode instanceof BinaryImplNode && WriterConstants.INDY_STRING_CONCAT_BOOTSTRAP_HANDLE == null) { - ((DupNode)((BinaryImplNode)irLoadNode).getLeftNode()).attachDecoration(new IRDDepth(1)); + ((DupNode) ((BinaryImplNode) irLoadNode).getLeftNode()).attachDecoration(new IRDDepth(1)); } - // handles when the operation is mathematical + // handles when the operation is mathematical } else { BinaryMathNode irBinaryMathNode = new BinaryMathNode(irStoreNode.getLocation()); irBinaryMathNode.setLeftNode(irLoadNode); @@ -907,14 +922,15 @@ public void visitAssignment(EAssignment userAssignmentNode, ScriptScope scriptSc irCompoundNode = irBinaryMathNode; } - PainlessCast downcast = scriptScope.hasDecoration(userAssignmentNode, DowncastPainlessCast.class) ? - scriptScope.getDecoration(userAssignmentNode, DowncastPainlessCast.class).getDowncastPainlessCast() : null; + PainlessCast downcast = scriptScope.hasDecoration(userAssignmentNode, DowncastPainlessCast.class) + ? scriptScope.getDecoration(userAssignmentNode, DowncastPainlessCast.class).getDowncastPainlessCast() + : null; // no need to downcast so the binary math node is the value for the store node if (downcast == null) { irCompoundNode.attachDecoration(new IRDExpressionType(irStoreNode.getDecorationValue(IRDStoreType.class))); irStoreNode.setChildNode(irCompoundNode); - // add a cast node to do a downcast as the value for the store node + // add a cast node to do a downcast as the value for the store node } else { CastNode irCastNode = new CastNode(irCompoundNode.getLocation()); irCastNode.attachDecoration(new IRDExpressionType(downcast.targetType)); @@ -937,7 +953,7 @@ public void visitAssignment(EAssignment userAssignmentNode, ScriptScope scriptSc irDupNode.attachDecoration(new IRDDepth(accessDepth)); irDupNode.setChildNode(irLoadNode); irLoadNode = irDupNode; - // the value is read from after the assignment (pre-increment/compound) + // the value is read from after the assignment (pre-increment/compound) } else { int size = MethodWriter.getType(irStoreNode.getDecorationValue(IRDExpressionType.class)).getSize(); irDupNode = new DupNode(irStoreNode.getLocation()); @@ -949,8 +965,9 @@ public void visitAssignment(EAssignment userAssignmentNode, ScriptScope scriptSc } } - PainlessCast upcast = scriptScope.hasDecoration(userAssignmentNode, UpcastPainlessCast.class) ? - scriptScope.getDecoration(userAssignmentNode, UpcastPainlessCast.class).getUpcastPainlessCast() : null; + PainlessCast upcast = scriptScope.hasDecoration(userAssignmentNode, UpcastPainlessCast.class) + ? scriptScope.getDecoration(userAssignmentNode, UpcastPainlessCast.class).getUpcastPainlessCast() + : null; // upcast the stored value if necessary if (upcast != null) { @@ -962,19 +979,19 @@ public void visitAssignment(EAssignment userAssignmentNode, ScriptScope scriptSc } if (concatenate) { - StringConcatenationNode irStringConcatenationNode = (StringConcatenationNode)irCompoundNode; + StringConcatenationNode irStringConcatenationNode = (StringConcatenationNode) irCompoundNode; irStringConcatenationNode.addArgumentNode(irLoadNode); irStringConcatenationNode.addArgumentNode(irValueNode); } else { - BinaryMathNode irBinaryMathNode = (BinaryMathNode)irCompoundNode; + BinaryMathNode irBinaryMathNode = (BinaryMathNode) irCompoundNode; irBinaryMathNode.setLeftNode(irLoadNode); irBinaryMathNode.setRightNode(irValueNode); } irAssignmentNode = irStoreNode; - // handles a standard assignment + // handles a standard assignment } else { - irAssignmentNode = (ExpressionNode)visit(userAssignmentNode.getLeftNode(), scriptScope); + irAssignmentNode = (ExpressionNode) visit(userAssignmentNode.getLeftNode(), scriptScope); // the value is read from after the assignment if (read) { @@ -990,9 +1007,9 @@ public void visitAssignment(EAssignment userAssignmentNode, ScriptScope scriptSc } if (irAssignmentNode instanceof BinaryImplNode) { - ((UnaryNode)((BinaryImplNode)irAssignmentNode).getRightNode()).setChildNode(irValueNode); + ((UnaryNode) ((BinaryImplNode) irAssignmentNode).getRightNode()).setChildNode(irValueNode); } else { - ((UnaryNode)irAssignmentNode).setChildNode(irValueNode); + ((UnaryNode) irAssignmentNode).setChildNode(irValueNode); } } @@ -1001,8 +1018,9 @@ public void visitAssignment(EAssignment userAssignmentNode, ScriptScope scriptSc @Override public void visitUnary(EUnary userUnaryNode, ScriptScope scriptScope) { - Class unaryType = scriptScope.hasDecoration(userUnaryNode, UnaryType.class) ? - scriptScope.getDecoration(userUnaryNode, UnaryType.class).getUnaryType() : null; + Class unaryType = scriptScope.hasDecoration(userUnaryNode, UnaryType.class) + ? scriptScope.getDecoration(userUnaryNode, UnaryType.class).getUnaryType() + : null; IRNode irNode; @@ -1011,7 +1029,8 @@ public void visitUnary(EUnary userUnaryNode, ScriptScope scriptScope) { } else { UnaryMathNode irUnaryMathNode = new UnaryMathNode(userUnaryNode.getLocation()); irUnaryMathNode.attachDecoration( - new IRDExpressionType(scriptScope.getDecoration(userUnaryNode, ValueType.class).getValueType())); + new IRDExpressionType(scriptScope.getDecoration(userUnaryNode, ValueType.class).getValueType()) + ); if (unaryType != null) { irUnaryMathNode.attachDecoration(new IRDUnaryType(unaryType)); @@ -1039,13 +1058,14 @@ public void visitBinary(EBinary userBinaryNode, ScriptScope scriptScope) { if (operation == Operation.ADD && valueType == String.class) { StringConcatenationNode stringConcatenationNode = new StringConcatenationNode(userBinaryNode.getLocation()); - stringConcatenationNode.addArgumentNode((ExpressionNode)visit(userBinaryNode.getLeftNode(), scriptScope)); - stringConcatenationNode.addArgumentNode((ExpressionNode)visit(userBinaryNode.getRightNode(), scriptScope)); + stringConcatenationNode.addArgumentNode((ExpressionNode) visit(userBinaryNode.getLeftNode(), scriptScope)); + stringConcatenationNode.addArgumentNode((ExpressionNode) visit(userBinaryNode.getRightNode(), scriptScope)); irExpressionNode = stringConcatenationNode; } else { Class binaryType = scriptScope.getDecoration(userBinaryNode, BinaryType.class).getBinaryType(); - Class shiftType = scriptScope.hasDecoration(userBinaryNode, ShiftType.class) ? - scriptScope.getDecoration(userBinaryNode, ShiftType.class).getShiftType() : null; + Class shiftType = scriptScope.hasDecoration(userBinaryNode, ShiftType.class) + ? scriptScope.getDecoration(userBinaryNode, ShiftType.class).getShiftType() + : null; BinaryMathNode irBinaryMathNode = new BinaryMathNode(userBinaryNode.getLocation()); @@ -1092,7 +1112,8 @@ public void visitComp(EComp userCompNode, ScriptScope scriptScope) { ComparisonNode irComparisonNode = new ComparisonNode(userCompNode.getLocation()); irComparisonNode.attachDecoration(new IRDExpressionType(scriptScope.getDecoration(userCompNode, ValueType.class).getValueType())); irComparisonNode.attachDecoration( - new IRDComparisonType(scriptScope.getDecoration(userCompNode, ComparisonType.class).getComparisonType())); + new IRDComparisonType(scriptScope.getDecoration(userCompNode, ComparisonType.class).getComparisonType()) + ); irComparisonNode.attachDecoration(new IRDOperation(userCompNode.getOperation())); irComparisonNode.setLeftNode(injectCast(userCompNode.getLeftNode(), scriptScope)); irComparisonNode.setRightNode(injectCast(userCompNode.getRightNode(), scriptScope)); @@ -1113,7 +1134,7 @@ public void visitInstanceof(EInstanceof userInstanceofNode, ScriptScope scriptSc InstanceofNode irInstanceofNode = new InstanceofNode(userInstanceofNode.getLocation()); irInstanceofNode.attachDecoration(new IRDExpressionType(valuetype)); irInstanceofNode.attachDecoration(new IRDInstanceType(instanceType)); - irInstanceofNode.setChildNode((ExpressionNode)visit(userInstanceofNode.getExpressionNode(), scriptScope)); + irInstanceofNode.setChildNode((ExpressionNode) visit(userInstanceofNode.getExpressionNode(), scriptScope)); scriptScope.putDecoration(userInstanceofNode, new IRNodeDecoration(irInstanceofNode)); } @@ -1122,7 +1143,8 @@ public void visitInstanceof(EInstanceof userInstanceofNode, ScriptScope scriptSc public void visitConditional(EConditional userConditionalNode, ScriptScope scriptScope) { ConditionalNode irConditionalNode = new ConditionalNode(userConditionalNode.getLocation()); irConditionalNode.attachDecoration( - new IRDExpressionType(scriptScope.getDecoration(userConditionalNode, ValueType.class).getValueType())); + new IRDExpressionType(scriptScope.getDecoration(userConditionalNode, ValueType.class).getValueType()) + ); irConditionalNode.setConditionNode(injectCast(userConditionalNode.getConditionNode(), scriptScope)); irConditionalNode.setLeftNode(injectCast(userConditionalNode.getTrueNode(), scriptScope)); irConditionalNode.setRightNode(injectCast(userConditionalNode.getFalseNode(), scriptScope)); @@ -1145,11 +1167,16 @@ public void visitListInit(EListInit userListInitNode, ScriptScope scriptScope) { ListInitializationNode irListInitializationNode = new ListInitializationNode(userListInitNode.getLocation()); irListInitializationNode.attachDecoration( - new IRDExpressionType(scriptScope.getDecoration(userListInitNode, ValueType.class).getValueType())); - irListInitializationNode.attachDecoration(new IRDConstructor( - scriptScope.getDecoration(userListInitNode, StandardPainlessConstructor.class).getStandardPainlessConstructor())); - irListInitializationNode.attachDecoration(new IRDMethod( - scriptScope.getDecoration(userListInitNode, StandardPainlessMethod.class).getStandardPainlessMethod())); + new IRDExpressionType(scriptScope.getDecoration(userListInitNode, ValueType.class).getValueType()) + ); + irListInitializationNode.attachDecoration( + new IRDConstructor( + scriptScope.getDecoration(userListInitNode, StandardPainlessConstructor.class).getStandardPainlessConstructor() + ) + ); + irListInitializationNode.attachDecoration( + new IRDMethod(scriptScope.getDecoration(userListInitNode, StandardPainlessMethod.class).getStandardPainlessMethod()) + ); for (AExpression userValueNode : userListInitNode.getValueNodes()) { irListInitializationNode.addArgumentNode(injectCast(userValueNode, scriptScope)); @@ -1163,17 +1190,22 @@ public void visitMapInit(EMapInit userMapInitNode, ScriptScope scriptScope) { MapInitializationNode irMapInitializationNode = new MapInitializationNode(userMapInitNode.getLocation()); irMapInitializationNode.attachDecoration( - new IRDExpressionType(scriptScope.getDecoration(userMapInitNode, ValueType.class).getValueType())); - irMapInitializationNode.attachDecoration(new IRDConstructor( - scriptScope.getDecoration(userMapInitNode, StandardPainlessConstructor.class).getStandardPainlessConstructor())); - irMapInitializationNode.attachDecoration(new IRDMethod( - scriptScope.getDecoration(userMapInitNode, StandardPainlessMethod.class).getStandardPainlessMethod())); - + new IRDExpressionType(scriptScope.getDecoration(userMapInitNode, ValueType.class).getValueType()) + ); + irMapInitializationNode.attachDecoration( + new IRDConstructor( + scriptScope.getDecoration(userMapInitNode, StandardPainlessConstructor.class).getStandardPainlessConstructor() + ) + ); + irMapInitializationNode.attachDecoration( + new IRDMethod(scriptScope.getDecoration(userMapInitNode, StandardPainlessMethod.class).getStandardPainlessMethod()) + ); for (int i = 0; i < userMapInitNode.getKeyNodes().size(); ++i) { irMapInitializationNode.addArgumentNode( - injectCast(userMapInitNode.getKeyNodes().get(i), scriptScope), - injectCast(userMapInitNode.getValueNodes().get(i), scriptScope)); + injectCast(userMapInitNode.getKeyNodes().get(i), scriptScope), + injectCast(userMapInitNode.getValueNodes().get(i), scriptScope) + ); } scriptScope.putDecoration(userMapInitNode, new IRNodeDecoration(irMapInitializationNode)); @@ -1186,7 +1218,7 @@ public void visitNewArray(ENewArray userNewArrayNode, ScriptScope scriptScope) { irNewArrayNode.attachDecoration(new IRDExpressionType(scriptScope.getDecoration(userNewArrayNode, ValueType.class).getValueType())); if (userNewArrayNode.isInitializer()) { - irNewArrayNode.attachCondition(IRCInitialize.class); + irNewArrayNode.attachCondition(IRCInitialize.class); } for (AExpression userArgumentNode : userNewArrayNode.getValueNodes()) { @@ -1199,8 +1231,8 @@ public void visitNewArray(ENewArray userNewArrayNode, ScriptScope scriptScope) { @Override public void visitNewObj(ENewObj userNewObjectNode, ScriptScope scriptScope) { Class valueType = scriptScope.getDecoration(userNewObjectNode, ValueType.class).getValueType(); - PainlessConstructor painlessConstructor = - scriptScope.getDecoration(userNewObjectNode, StandardPainlessConstructor.class).getStandardPainlessConstructor(); + PainlessConstructor painlessConstructor = scriptScope.getDecoration(userNewObjectNode, StandardPainlessConstructor.class) + .getStandardPainlessConstructor(); NewObjectNode irNewObjectNode = new NewObjectNode(userNewObjectNode.getLocation()); irNewObjectNode.attachDecoration(new IRDExpressionType(valueType)); @@ -1225,16 +1257,15 @@ public void visitCallLocal(ECallLocal callLocalNode, ScriptScope scriptScope) { LocalFunction localFunction = scriptScope.getDecoration(callLocalNode, StandardLocalFunction.class).getLocalFunction(); irInvokeCallMemberNode.attachDecoration(new IRDFunction(localFunction)); } else if (scriptScope.hasDecoration(callLocalNode, ThisPainlessMethod.class)) { - PainlessMethod thisMethod = - scriptScope.getDecoration(callLocalNode, ThisPainlessMethod.class).getThisPainlessMethod(); + PainlessMethod thisMethod = scriptScope.getDecoration(callLocalNode, ThisPainlessMethod.class).getThisPainlessMethod(); irInvokeCallMemberNode.attachDecoration(new IRDThisMethod(thisMethod)); } else if (scriptScope.hasDecoration(callLocalNode, StandardPainlessMethod.class)) { - PainlessMethod importedMethod = - scriptScope.getDecoration(callLocalNode, StandardPainlessMethod.class).getStandardPainlessMethod(); + PainlessMethod importedMethod = scriptScope.getDecoration(callLocalNode, StandardPainlessMethod.class) + .getStandardPainlessMethod(); irInvokeCallMemberNode.attachDecoration(new IRDMethod(importedMethod)); } else if (scriptScope.hasDecoration(callLocalNode, StandardPainlessClassBinding.class)) { - PainlessClassBinding painlessClassBinding = - scriptScope.getDecoration(callLocalNode, StandardPainlessClassBinding.class).getPainlessClassBinding(); + PainlessClassBinding painlessClassBinding = scriptScope.getDecoration(callLocalNode, StandardPainlessClassBinding.class) + .getPainlessClassBinding(); String bindingName = scriptScope.getNextSyntheticName("class_binding"); FieldNode irFieldNode = new FieldNode(callLocalNode.getLocation()); @@ -1245,14 +1276,16 @@ public void visitCallLocal(ECallLocal callLocalNode, ScriptScope scriptScope) { irInvokeCallMemberNode.attachDecoration(new IRDClassBinding(painlessClassBinding)); - if ((int)scriptScope.getDecoration(callLocalNode, StandardConstant.class).getStandardConstant() == 0) { + if ((int) scriptScope.getDecoration(callLocalNode, StandardConstant.class).getStandardConstant() == 0) { irInvokeCallMemberNode.attachCondition(IRCStatic.class); } irInvokeCallMemberNode.attachDecoration(new IRDName(bindingName)); } else if (scriptScope.hasDecoration(callLocalNode, StandardPainlessInstanceBinding.class)) { - PainlessInstanceBinding painlessInstanceBinding = - scriptScope.getDecoration(callLocalNode, StandardPainlessInstanceBinding.class).getPainlessInstanceBinding(); + PainlessInstanceBinding painlessInstanceBinding = scriptScope.getDecoration( + callLocalNode, + StandardPainlessInstanceBinding.class + ).getPainlessInstanceBinding(); String bindingName = scriptScope.getNextSyntheticName("instance_binding"); FieldNode irFieldNode = new FieldNode(callLocalNode.getLocation()); @@ -1349,24 +1382,28 @@ public void visitLambda(ELambda userLambdaNode, ScriptScope scriptScope) { if (scriptScope.hasDecoration(userLambdaNode, TargetType.class)) { TypedInterfaceReferenceNode typedInterfaceReferenceNode = new TypedInterfaceReferenceNode(userLambdaNode.getLocation()); - typedInterfaceReferenceNode.attachDecoration(new IRDReference( - scriptScope.getDecoration(userLambdaNode, ReferenceDecoration.class).getReference())); + typedInterfaceReferenceNode.attachDecoration( + new IRDReference(scriptScope.getDecoration(userLambdaNode, ReferenceDecoration.class).getReference()) + ); irExpressionNode = typedInterfaceReferenceNode; } else { DefInterfaceReferenceNode defInterfaceReferenceNode = new DefInterfaceReferenceNode(userLambdaNode.getLocation()); - defInterfaceReferenceNode.attachDecoration(new IRDDefReferenceEncoding( - scriptScope.getDecoration(userLambdaNode, EncodingDecoration.class).getEncoding())); + defInterfaceReferenceNode.attachDecoration( + new IRDDefReferenceEncoding(scriptScope.getDecoration(userLambdaNode, EncodingDecoration.class).getEncoding()) + ); irExpressionNode = defInterfaceReferenceNode; } FunctionNode irFunctionNode = new FunctionNode(userLambdaNode.getLocation()); - irFunctionNode.setBlockNode((BlockNode)visit(userLambdaNode.getBlockNode(), scriptScope)); + irFunctionNode.setBlockNode((BlockNode) visit(userLambdaNode.getBlockNode(), scriptScope)); irFunctionNode.attachDecoration(new IRDName(scriptScope.getDecoration(userLambdaNode, MethodNameDecoration.class).getMethodName())); irFunctionNode.attachDecoration(new IRDReturnType(scriptScope.getDecoration(userLambdaNode, ReturnType.class).getReturnType())); - irFunctionNode.attachDecoration(new IRDTypeParameters( - new ArrayList<>(scriptScope.getDecoration(userLambdaNode, TypeParameters.class).getTypeParameters()))); - irFunctionNode.attachDecoration(new IRDParameterNames( - new ArrayList<>(scriptScope.getDecoration(userLambdaNode, ParameterNames.class).getParameterNames()))); + irFunctionNode.attachDecoration( + new IRDTypeParameters(new ArrayList<>(scriptScope.getDecoration(userLambdaNode, TypeParameters.class).getTypeParameters())) + ); + irFunctionNode.attachDecoration( + new IRDParameterNames(new ArrayList<>(scriptScope.getDecoration(userLambdaNode, ParameterNames.class).getParameterNames())) + ); if (scriptScope.getCondition(userLambdaNode, InstanceCapturingLambda.class)) { irFunctionNode.attachCondition(IRCInstanceCapture.class); irExpressionNode.attachCondition(IRCInstanceCapture.class); @@ -1424,11 +1461,13 @@ public void visitFunctionRef(EFunctionRef userFunctionRefNode, ScriptScope scrip } irReferenceNode.attachDecoration( - new IRDExpressionType(scriptScope.getDecoration(userFunctionRefNode, ValueType.class).getValueType())); + new IRDExpressionType(scriptScope.getDecoration(userFunctionRefNode, ValueType.class).getValueType()) + ); if (capturesDecoration != null) { - irReferenceNode.attachDecoration(new IRDCaptureNames( - Collections.singletonList(capturesDecoration.getCaptures().get(0).getName()))); + irReferenceNode.attachDecoration( + new IRDCaptureNames(Collections.singletonList(capturesDecoration.getCaptures().get(0).getName())) + ); if (scriptScope.getCondition(userFunctionRefNode, CaptureBox.class)) { irReferenceNode.attachCondition(IRCCaptureBox.class); @@ -1443,8 +1482,9 @@ public void visitNewArrayFunctionRef(ENewArrayFunctionRef userNewArrayFunctionRe ExpressionNode irReferenceNode; if (scriptScope.hasDecoration(userNewArrayFunctionRefNode, TargetType.class)) { - TypedInterfaceReferenceNode typedInterfaceReferenceNode = - new TypedInterfaceReferenceNode(userNewArrayFunctionRefNode.getLocation()); + TypedInterfaceReferenceNode typedInterfaceReferenceNode = new TypedInterfaceReferenceNode( + userNewArrayFunctionRefNode.getLocation() + ); FunctionRef reference = scriptScope.getDecoration(userNewArrayFunctionRefNode, ReferenceDecoration.class).getReference(); typedInterfaceReferenceNode.attachDecoration(new IRDReference(reference)); irReferenceNode = typedInterfaceReferenceNode; @@ -1473,8 +1513,9 @@ public void visitNewArrayFunctionRef(ENewArrayFunctionRef userNewArrayFunctionRe irBlockNode.addStatementNode(irReturnNode); FunctionNode irFunctionNode = new FunctionNode(userNewArrayFunctionRefNode.getLocation()); - irFunctionNode.attachDecoration(new IRDName( - scriptScope.getDecoration(userNewArrayFunctionRefNode, MethodNameDecoration.class).getMethodName())); + irFunctionNode.attachDecoration( + new IRDName(scriptScope.getDecoration(userNewArrayFunctionRefNode, MethodNameDecoration.class).getMethodName()) + ); irFunctionNode.attachDecoration(new IRDReturnType(returnType)); irFunctionNode.attachDecoration(new IRDTypeParameters(Collections.singletonList(int.class))); irFunctionNode.attachDecoration(new IRDParameterNames(Collections.singletonList("size"))); @@ -1486,7 +1527,8 @@ public void visitNewArrayFunctionRef(ENewArrayFunctionRef userNewArrayFunctionRe irClassNode.addFunctionNode(irFunctionNode); irReferenceNode.attachDecoration( - new IRDExpressionType(scriptScope.getDecoration(userNewArrayFunctionRefNode, ValueType.class).getValueType())); + new IRDExpressionType(scriptScope.getDecoration(userNewArrayFunctionRefNode, ValueType.class).getValueType()) + ); scriptScope.putDecoration(userNewArrayFunctionRefNode, new IRNodeDecoration(irReferenceNode)); } @@ -1561,7 +1603,7 @@ public void visitDot(EDot userDotNode, ScriptScope scriptScope) { Class valueType = scriptScope.getDecoration(userDotNode, ValueType.class).getValueType(); ValueType prefixValueType = scriptScope.getDecoration(userDotNode.getPrefixNode(), ValueType.class); - ExpressionNode irPrefixNode = (ExpressionNode)visit(userDotNode.getPrefixNode(), scriptScope); + ExpressionNode irPrefixNode = (ExpressionNode) visit(userDotNode.getPrefixNode(), scriptScope); ExpressionNode irIndexNode = null; UnaryNode irStoreNode = null; ExpressionNode irLoadNode = null; @@ -1591,8 +1633,8 @@ public void visitDot(EDot userDotNode, ScriptScope scriptScope) { accessDepth = 1; } else if (scriptScope.hasDecoration(userDotNode, StandardPainlessField.class)) { - PainlessField painlessField = - scriptScope.getDecoration(userDotNode, StandardPainlessField.class).getStandardPainlessField(); + PainlessField painlessField = scriptScope.getDecoration(userDotNode, StandardPainlessField.class) + .getStandardPainlessField(); if (write || compound) { StoreDotNode irStoreDotNode = new StoreDotNode(location); @@ -1615,16 +1657,18 @@ public void visitDot(EDot userDotNode, ScriptScope scriptScope) { StoreDotShortcutNode irStoreDotShortcutNode = new StoreDotShortcutNode(location); irStoreDotShortcutNode.attachDecoration(new IRDExpressionType(read ? valueType : void.class)); irStoreDotShortcutNode.attachDecoration(new IRDStoreType(valueType)); - irStoreDotShortcutNode.attachDecoration(new IRDMethod( - scriptScope.getDecoration(userDotNode, SetterPainlessMethod.class).getSetterPainlessMethod())); + irStoreDotShortcutNode.attachDecoration( + new IRDMethod(scriptScope.getDecoration(userDotNode, SetterPainlessMethod.class).getSetterPainlessMethod()) + ); irStoreNode = irStoreDotShortcutNode; } if (write == false || compound) { LoadDotShortcutNode irLoadDotShortcutNode = new LoadDotShortcutNode(location); irLoadDotShortcutNode.attachDecoration(new IRDExpressionType(valueType)); - irLoadDotShortcutNode.attachDecoration(new IRDMethod( - scriptScope.getDecoration(userDotNode, GetterPainlessMethod.class).getGetterPainlessMethod())); + irLoadDotShortcutNode.attachDecoration( + new IRDMethod(scriptScope.getDecoration(userDotNode, GetterPainlessMethod.class).getGetterPainlessMethod()) + ); irLoadNode = irLoadDotShortcutNode; } @@ -1639,16 +1683,18 @@ public void visitDot(EDot userDotNode, ScriptScope scriptScope) { StoreMapShortcutNode irStoreMapShortcutNode = new StoreMapShortcutNode(location); irStoreMapShortcutNode.attachDecoration(new IRDExpressionType(read ? valueType : void.class)); irStoreMapShortcutNode.attachDecoration(new IRDStoreType(valueType)); - irStoreMapShortcutNode.attachDecoration(new IRDMethod( - scriptScope.getDecoration(userDotNode, SetterPainlessMethod.class).getSetterPainlessMethod())); + irStoreMapShortcutNode.attachDecoration( + new IRDMethod(scriptScope.getDecoration(userDotNode, SetterPainlessMethod.class).getSetterPainlessMethod()) + ); irStoreNode = irStoreMapShortcutNode; } if (write == false || compound) { LoadMapShortcutNode irLoadMapShortcutNode = new LoadMapShortcutNode(location); irLoadMapShortcutNode.attachDecoration(new IRDExpressionType(valueType)); - irLoadMapShortcutNode.attachDecoration(new IRDMethod( - scriptScope.getDecoration(userDotNode, GetterPainlessMethod.class).getGetterPainlessMethod())); + irLoadMapShortcutNode.attachDecoration( + new IRDMethod(scriptScope.getDecoration(userDotNode, GetterPainlessMethod.class).getGetterPainlessMethod()) + ); irLoadNode = irLoadMapShortcutNode; } @@ -1657,23 +1703,26 @@ public void visitDot(EDot userDotNode, ScriptScope scriptScope) { ConstantNode irConstantNode = new ConstantNode(location); irConstantNode.attachDecoration(new IRDExpressionType(int.class)); irConstantNode.attachDecoration( - new IRDConstant(scriptScope.getDecoration(userDotNode, StandardConstant.class).getStandardConstant())); + new IRDConstant(scriptScope.getDecoration(userDotNode, StandardConstant.class).getStandardConstant()) + ); irIndexNode = irConstantNode; if (write || compound) { StoreListShortcutNode irStoreListShortcutNode = new StoreListShortcutNode(location); irStoreListShortcutNode.attachDecoration(new IRDExpressionType(read ? valueType : void.class)); irStoreListShortcutNode.attachDecoration(new IRDStoreType(valueType)); - irStoreListShortcutNode.attachDecoration(new IRDMethod( - scriptScope.getDecoration(userDotNode, SetterPainlessMethod.class).getSetterPainlessMethod())); + irStoreListShortcutNode.attachDecoration( + new IRDMethod(scriptScope.getDecoration(userDotNode, SetterPainlessMethod.class).getSetterPainlessMethod()) + ); irStoreNode = irStoreListShortcutNode; } if (write == false || compound) { LoadListShortcutNode irLoadListShortcutNode = new LoadListShortcutNode(location); irLoadListShortcutNode.attachDecoration(new IRDExpressionType(valueType)); - irLoadListShortcutNode.attachDecoration(new IRDMethod( - scriptScope.getDecoration(userDotNode, GetterPainlessMethod.class).getGetterPainlessMethod())); + irLoadListShortcutNode.attachDecoration( + new IRDMethod(scriptScope.getDecoration(userDotNode, GetterPainlessMethod.class).getGetterPainlessMethod()) + ); irLoadNode = irLoadListShortcutNode; } @@ -1684,7 +1733,14 @@ public void visitDot(EDot userDotNode, ScriptScope scriptScope) { scriptScope.putDecoration(userDotNode, new AccessDepth(accessDepth)); irExpressionNode = buildLoadStore( - accessDepth, location, userDotNode.isNullSafe(), irPrefixNode, irIndexNode, irLoadNode, irStoreNode); + accessDepth, + location, + userDotNode.isNullSafe(), + irPrefixNode, + irIndexNode, + irLoadNode, + irStoreNode + ); } scriptScope.putDecoration(userDotNode, new IRNodeDecoration(irExpressionNode)); @@ -1703,7 +1759,7 @@ public void visitBrace(EBrace userBraceNode, ScriptScope scriptScope) { Class valueType = scriptScope.getDecoration(userBraceNode, ValueType.class).getValueType(); Class prefixValueType = scriptScope.getDecoration(userBraceNode.getPrefixNode(), ValueType.class).getValueType(); - ExpressionNode irPrefixNode = (ExpressionNode)visit(userBraceNode.getPrefixNode(), scriptScope); + ExpressionNode irPrefixNode = (ExpressionNode) visit(userBraceNode.getPrefixNode(), scriptScope); ExpressionNode irIndexNode = injectCast(userBraceNode.getIndexNode(), scriptScope); UnaryNode irStoreNode = null; ExpressionNode irLoadNode = null; @@ -1792,8 +1848,10 @@ public void visitBrace(EBrace userBraceNode, ScriptScope scriptScope) { scriptScope.putDecoration(userBraceNode, new AccessDepth(2)); - scriptScope.putDecoration(userBraceNode, new IRNodeDecoration( - buildLoadStore(2, location, false, irPrefixNode, irIndexNode, irLoadNode, irStoreNode))); + scriptScope.putDecoration( + userBraceNode, + new IRNodeDecoration(buildLoadStore(2, location, false, irPrefixNode, irIndexNode, irLoadNode, irStoreNode)) + ); } @Override @@ -1807,7 +1865,7 @@ public void visitCall(ECall userCallNode, ScriptScope scriptScope) { InvokeCallDefNode irCallSubDefNode = new InvokeCallDefNode(userCallNode.getLocation()); for (AExpression userArgumentNode : userCallNode.getArgumentNodes()) { - irCallSubDefNode.addArgumentNode((ExpressionNode)visit(userArgumentNode, scriptScope)); + irCallSubDefNode.addArgumentNode((ExpressionNode) visit(userArgumentNode, scriptScope)); } irCallSubDefNode.attachDecoration(new IRDExpressionType(valueType)); @@ -1860,7 +1918,7 @@ public void visitCall(ECall userCallNode, ScriptScope scriptScope) { } BinaryImplNode irBinaryImplNode = new BinaryImplNode(irExpressionNode.getLocation()); - irBinaryImplNode.setLeftNode((ExpressionNode)visit(userCallNode.getPrefixNode(), scriptScope)); + irBinaryImplNode.setLeftNode((ExpressionNode) visit(userCallNode.getPrefixNode(), scriptScope)); irBinaryImplNode.setRightNode(irExpressionNode); irBinaryImplNode.attachDecoration(irExpressionNode.getDecoration(IRDExpressionType.class)); diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/IRTreeVisitor.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/IRTreeVisitor.java index 1cbe70149a252..4ab3b3cc83969 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/IRTreeVisitor.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/IRTreeVisitor.java @@ -80,73 +80,138 @@ public interface IRTreeVisitor { void visitClass(ClassNode irClassNode, Scope scope); + void visitFunction(FunctionNode irFunctionNode, Scope scope); + void visitField(FieldNode irFieldNode, Scope scope); void visitBlock(BlockNode irBlockNode, Scope scope); + void visitIf(IfNode irIfNode, Scope scope); + void visitIfElse(IfElseNode irIfElseNode, Scope scope); + void visitWhileLoop(WhileLoopNode irWhileLoopNode, Scope scope); + void visitDoWhileLoop(DoWhileLoopNode irDoWhileLoopNode, Scope scope); + void visitForLoop(ForLoopNode irForLoopNode, Scope scope); + void visitForEachLoop(ForEachLoopNode irForEachLoopNode, Scope scope); + void visitForEachSubArrayLoop(ForEachSubArrayNode irForEachSubArrayNode, Scope scope); + void visitForEachSubIterableLoop(ForEachSubIterableNode irForEachSubIterableNode, Scope scope); + void visitDeclarationBlock(DeclarationBlockNode irDeclarationBlockNode, Scope scope); + void visitDeclaration(DeclarationNode irDeclarationNode, Scope scope); + void visitReturn(ReturnNode irReturnNode, Scope scope); + void visitStatementExpression(StatementExpressionNode irStatementExpressionNode, Scope scope); + void visitTry(TryNode irTryNode, Scope scope); + void visitCatch(CatchNode irCatchNode, Scope scope); + void visitThrow(ThrowNode irThrowNode, Scope scope); + void visitContinue(ContinueNode irContinueNode, Scope scope); + void visitBreak(BreakNode irBreakNode, Scope scope); void visitBinaryImpl(BinaryImplNode irBinaryImplNode, Scope scope); + void visitUnaryMath(UnaryMathNode irUnaryMathNode, Scope scope); + void visitBinaryMath(BinaryMathNode irBinaryMathNode, Scope scope); + void visitStringConcatenation(StringConcatenationNode irStringConcatenationNode, Scope scope); + void visitBoolean(BooleanNode irBooleanNode, Scope scope); + void visitComparison(ComparisonNode irComparisonNode, Scope scope); + void visitCast(CastNode irCastNode, Scope scope); + void visitInstanceof(InstanceofNode irInstanceofNode, Scope scope); + void visitConditional(ConditionalNode irConditionalNode, Scope scope); + void visitElvis(ElvisNode irElvisNode, Scope scope); + void visitListInitialization(ListInitializationNode irListInitializationNode, Scope scope); + void visitMapInitialization(MapInitializationNode irMapInitializationNode, Scope scope); + void visitNewArray(NewArrayNode irNewArrayNode, Scope scope); + void visitNewObject(NewObjectNode irNewObjectNode, Scope scope); + void visitConstant(ConstantNode irConstantNode, Scope scope); + void visitNull(NullNode irNullNode, Scope scope); + void visitDefInterfaceReference(DefInterfaceReferenceNode irDefInterfaceReferenceNode, Scope scope); + void visitTypedInterfaceReference(TypedInterfaceReferenceNode irTypedInterfaceReferenceNode, Scope scope); + void visitTypedCaptureReference(TypedCaptureReferenceNode irTypedCaptureReferenceNode, Scope scope); + void visitStatic(StaticNode irStaticNode, Scope scope); + void visitLoadVariable(LoadVariableNode irLoadVariableNode, Scope scope); + void visitNullSafeSub(NullSafeSubNode irNullSafeSubNode, Scope scope); + void visitLoadDotArrayLengthNode(LoadDotArrayLengthNode irLoadDotArrayLengthNode, Scope scope); + void visitLoadDotDef(LoadDotDefNode irLoadDotDefNode, Scope scope); + void visitLoadDot(LoadDotNode irLoadDotNode, Scope scope); + void visitLoadDotShortcut(LoadDotShortcutNode irDotSubShortcutNode, Scope scope); + void visitLoadListShortcut(LoadListShortcutNode irLoadListShortcutNode, Scope scope); + void visitLoadMapShortcut(LoadMapShortcutNode irLoadMapShortcutNode, Scope scope); + void visitLoadFieldMember(LoadFieldMemberNode irLoadFieldMemberNode, Scope scope); + void visitLoadBraceDef(LoadBraceDefNode irLoadBraceDefNode, Scope scope); + void visitLoadBrace(LoadBraceNode irLoadBraceNode, Scope scope); + void visitStoreVariable(StoreVariableNode irStoreVariableNode, Scope scope); + void visitStoreDotDef(StoreDotDefNode irStoreDotDefNode, Scope scope); + void visitStoreDot(StoreDotNode irStoreDotNode, Scope scope); + void visitStoreDotShortcut(StoreDotShortcutNode irDotSubShortcutNode, Scope scope); + void visitStoreListShortcut(StoreListShortcutNode irStoreListShortcutNode, Scope scope); + void visitStoreMapShortcut(StoreMapShortcutNode irStoreMapShortcutNode, Scope scope); + void visitStoreFieldMember(StoreFieldMemberNode irStoreFieldMemberNode, Scope scope); + void visitStoreBraceDef(StoreBraceDefNode irStoreBraceDefNode, Scope scope); + void visitStoreBrace(StoreBraceNode irStoreBraceNode, Scope scope); + void visitInvokeCallDef(InvokeCallDefNode irInvokeCallDefNode, Scope scope); + void visitInvokeCall(InvokeCallNode irInvokeCallNode, Scope scope); + void visitInvokeCallMember(InvokeCallMemberNode irInvokeCallMemberNode, Scope scope); + void visitFlipArrayIndex(FlipArrayIndexNode irFlipArrayIndexNode, Scope scope); + void visitFlipCollectionIndex(FlipCollectionIndexNode irFlipCollectionIndexNode, Scope scope); + void visitFlipDefIndex(FlipDefIndexNode irFlipDefIndexNode, Scope scope); + void visitDup(DupNode irDupNode, Scope scope); } diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/PainlessSemanticAnalysisPhase.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/PainlessSemanticAnalysisPhase.java index b996e4f011495..e4f9cee678e8c 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/PainlessSemanticAnalysisPhase.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/PainlessSemanticAnalysisPhase.java @@ -49,8 +49,8 @@ public void visitFunction(SFunction userFunctionNode, ScriptScope scriptScope) { if ("execute".equals(functionName)) { ScriptClassInfo scriptClassInfo = scriptScope.getScriptClassInfo(); - LocalFunction localFunction = - scriptScope.getFunctionTable().getFunction(functionName, scriptClassInfo.getExecuteArguments().size()); + LocalFunction localFunction = scriptScope.getFunctionTable() + .getFunction(functionName, scriptClassInfo.getExecuteArguments().size()); List> typeParameters = localFunction.getTypeParameters(); FunctionScope functionScope = newFunctionScope(scriptScope, localFunction.getReturnType()); @@ -71,9 +71,17 @@ public void visitFunction(SFunction userFunctionNode, ScriptScope scriptScope) { SBlock userBlockNode = userFunctionNode.getBlockNode(); if (userBlockNode.getStatementNodes().isEmpty()) { - throw userFunctionNode.createError(new IllegalArgumentException("invalid function definition: " + - "found no statements for function " + - "[" + functionName + "] with [" + typeParameters.size() + "] parameters")); + throw userFunctionNode.createError( + new IllegalArgumentException( + "invalid function definition: " + + "found no statements for function " + + "[" + + functionName + + "] with [" + + typeParameters.size() + + "] parameters" + ) + ); } functionScope.setCondition(userBlockNode, LastSource.class); @@ -118,8 +126,12 @@ public void visitExpression(SExpression userExpressionNode, SemanticScope semant semanticScope.putDecoration(userStatementNode, new TargetType(rtnType)); semanticScope.setCondition(userStatementNode, Internal.class); if ("execute".equals(functionName)) { - decorateWithCastForReturn(userStatementNode, userExpressionNode, semanticScope, - semanticScope.getScriptScope().getScriptClassInfo()); + decorateWithCastForReturn( + userStatementNode, + userExpressionNode, + semanticScope, + semanticScope.getScriptScope().getScriptClassInfo() + ); } else { decorateWithCast(userStatementNode, semanticScope); } @@ -143,9 +155,17 @@ public void visitReturn(SReturn userReturnNode, SemanticScope semanticScope) { if (userValueNode == null) { if (semanticScope.getReturnType() != void.class) { - throw userReturnNode.createError(new ClassCastException("cannot cast from " + - "[" + semanticScope.getReturnCanonicalTypeName() + "] to " + - "[" + PainlessLookupUtility.typeToCanonicalTypeName(void.class) + "]")); + throw userReturnNode.createError( + new ClassCastException( + "cannot cast from " + + "[" + + semanticScope.getReturnCanonicalTypeName() + + "] to " + + "[" + + PainlessLookupUtility.typeToCanonicalTypeName(void.class) + + "]" + ) + ); } } else { semanticScope.setCondition(userValueNode, Read.class); @@ -153,8 +173,12 @@ public void visitReturn(SReturn userReturnNode, SemanticScope semanticScope) { semanticScope.setCondition(userValueNode, Internal.class); checkedVisit(userValueNode, semanticScope); if ("execute".equals(functionName)) { - decorateWithCastForReturn(userValueNode, userReturnNode, semanticScope, - semanticScope.getScriptScope().getScriptClassInfo()); + decorateWithCastForReturn( + userValueNode, + userReturnNode, + semanticScope, + semanticScope.getScriptScope().getScriptClassInfo() + ); } else { decorateWithCast(userValueNode, semanticScope); } diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/PainlessSemanticHeaderPhase.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/PainlessSemanticHeaderPhase.java index 47df17eb23f50..558e6ecc20530 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/PainlessSemanticHeaderPhase.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/PainlessSemanticHeaderPhase.java @@ -30,8 +30,9 @@ public void visitFunction(SFunction userFunctionNode, ScriptScope scriptScope) { String functionKey = FunctionTable.buildLocalFunctionKey(functionName, scriptClassInfo.getExecuteArguments().size()); if (functionTable.getFunction(functionKey) != null) { - throw userFunctionNode.createError(new IllegalArgumentException("invalid function definition: " + - "found duplicate function [" + functionKey + "].")); + throw userFunctionNode.createError( + new IllegalArgumentException("invalid function definition: " + "found duplicate function [" + functionKey + "].") + ); } Class returnType = scriptClassInfo.getExecuteMethodReturnType(); diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/PainlessUserTreeToIRTreePhase.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/PainlessUserTreeToIRTreePhase.java index ae6e2115d22b2..dbcf7cd5eca0b 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/PainlessUserTreeToIRTreePhase.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/PainlessUserTreeToIRTreePhase.java @@ -82,12 +82,12 @@ public void visitFunction(SFunction userFunctionNode, ScriptScope scriptScope) { // the main "execute" block with several exceptions. if ("execute".equals(functionName)) { ScriptClassInfo scriptClassInfo = scriptScope.getScriptClassInfo(); - LocalFunction localFunction = - scriptScope.getFunctionTable().getFunction(functionName, scriptClassInfo.getExecuteArguments().size()); + LocalFunction localFunction = scriptScope.getFunctionTable() + .getFunction(functionName, scriptClassInfo.getExecuteArguments().size()); Class returnType = localFunction.getReturnType(); boolean methodEscape = scriptScope.getCondition(userFunctionNode, MethodEscape.class); - BlockNode irBlockNode = (BlockNode)visit(userFunctionNode.getBlockNode(), scriptScope); + BlockNode irBlockNode = (BlockNode) visit(userFunctionNode.getBlockNode(), scriptScope); if (methodEscape == false) { ExpressionNode irExpressionNode; @@ -102,19 +102,19 @@ public void visitFunction(SFunction userFunctionNode, ScriptScope scriptScope) { if (returnType == boolean.class) { irConstantNode.attachDecoration(new IRDConstant(false)); } else if (returnType == byte.class - || returnType == char.class - || returnType == short.class - || returnType == int.class) { - irConstantNode.attachDecoration(new IRDConstant(0)); - } else if (returnType == long.class) { - irConstantNode.attachDecoration(new IRDConstant(0L)); - } else if (returnType == float.class) { - irConstantNode.attachDecoration(new IRDConstant(0f)); - } else if (returnType == double.class) { - irConstantNode.attachDecoration(new IRDConstant(0d)); - } else { - throw userFunctionNode.createError(new IllegalStateException("illegal tree structure")); - } + || returnType == char.class + || returnType == short.class + || returnType == int.class) { + irConstantNode.attachDecoration(new IRDConstant(0)); + } else if (returnType == long.class) { + irConstantNode.attachDecoration(new IRDConstant(0L)); + } else if (returnType == float.class) { + irConstantNode.attachDecoration(new IRDConstant(0f)); + } else if (returnType == double.class) { + irConstantNode.attachDecoration(new IRDConstant(0d)); + } else { + throw userFunctionNode.createError(new IllegalStateException("illegal tree structure")); + } irExpressionNode = irConstantNode; } else { @@ -281,8 +281,9 @@ protected void injectGetsDeclarations(BlockNode irBlockNode, ScriptScope scriptS InvokeCallMemberNode irInvokeCallMemberNode = new InvokeCallMemberNode(internalLocation); irInvokeCallMemberNode.attachDecoration(new IRDExpressionType(returnType)); - irInvokeCallMemberNode.attachDecoration(new IRDFunction(new LocalFunction( - getMethod.getName(), returnType, Collections.emptyList(), true, false))); + irInvokeCallMemberNode.attachDecoration( + new IRDFunction(new LocalFunction(getMethod.getName(), returnType, Collections.emptyList(), true, false)) + ); irDeclarationNode.setExpressionNode(irInvokeCallMemberNode); } } @@ -364,15 +365,17 @@ protected void injectSandboxExceptions(FunctionNode irFunctionNode) { InvokeCallMemberNode irInvokeCallMemberNode = new InvokeCallMemberNode(internalLocation); irInvokeCallMemberNode.attachDecoration(new IRDExpressionType(ScriptException.class)); - irInvokeCallMemberNode.attachDecoration(new IRDFunction( + irInvokeCallMemberNode.attachDecoration( + new IRDFunction( new LocalFunction( - "convertToScriptException", - ScriptException.class, - Arrays.asList(Throwable.class, Map.class), - true, - false + "convertToScriptException", + ScriptException.class, + Arrays.asList(Throwable.class, Map.class), + true, + false ) - )); + ) + ); irThrowNode.setExpressionNode(irInvokeCallMemberNode); @@ -397,17 +400,15 @@ protected void injectSandboxExceptions(FunctionNode irFunctionNode) { irInvokeCallNode.attachDecoration(new IRDExpressionType(Map.class)); irInvokeCallNode.setBox(PainlessExplainError.class); irInvokeCallNode.setMethod( - new PainlessMethod( - PainlessExplainError.class.getMethod( - "getHeaders", - PainlessLookup.class), - PainlessExplainError.class, - null, - Collections.emptyList(), - null, - null, - null - ) + new PainlessMethod( + PainlessExplainError.class.getMethod("getHeaders", PainlessLookup.class), + PainlessExplainError.class, + null, + Collections.emptyList(), + null, + null, + null + ) ); irBinaryImplNode.setRightNode(irInvokeCallNode); @@ -420,7 +421,11 @@ protected void injectSandboxExceptions(FunctionNode irFunctionNode) { irInvokeCallNode.addArgumentNode(irLoadFieldMemberNode); for (Class throwable : new Class[] { - PainlessError.class, LinkageError.class, OutOfMemoryError.class, StackOverflowError.class, Exception.class}) { + PainlessError.class, + LinkageError.class, + OutOfMemoryError.class, + StackOverflowError.class, + Exception.class }) { String name = throwable.getSimpleName(); name = "#" + Character.toLowerCase(name.charAt(0)) + name.substring(1); @@ -442,15 +447,17 @@ protected void injectSandboxExceptions(FunctionNode irFunctionNode) { irInvokeCallMemberNode = new InvokeCallMemberNode(internalLocation); irInvokeCallMemberNode.attachDecoration(new IRDExpressionType(ScriptException.class)); - irInvokeCallMemberNode.attachDecoration(new IRDFunction( + irInvokeCallMemberNode.attachDecoration( + new IRDFunction( new LocalFunction( - "convertToScriptException", - ScriptException.class, - Arrays.asList(Throwable.class, Map.class), - true, - false + "convertToScriptException", + ScriptException.class, + Arrays.asList(Throwable.class, Map.class), + true, + false ) - )); + ) + ); irThrowNode.setExpressionNode(irInvokeCallMemberNode); @@ -474,15 +481,15 @@ protected void injectSandboxExceptions(FunctionNode irFunctionNode) { irInvokeCallNode.attachDecoration(new IRDExpressionType(Map.class)); irInvokeCallNode.setBox(Collections.class); irInvokeCallNode.setMethod( - new PainlessMethod( - Collections.class.getMethod("emptyMap"), - Collections.class, - null, - Collections.emptyList(), - null, - null, - null - ) + new PainlessMethod( + Collections.class.getMethod("emptyMap"), + Collections.class, + null, + Collections.emptyList(), + null, + null, + null + ) ); irBinaryImplNode.setRightNode(irInvokeCallNode); diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/UserTreeVisitor.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/UserTreeVisitor.java index 172b252d3653d..82f58a2785312 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/UserTreeVisitor.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/UserTreeVisitor.java @@ -57,50 +57,92 @@ public interface UserTreeVisitor { void visitClass(SClass userClassNode, Scope scope); + void visitFunction(SFunction userFunctionNode, Scope scope); void visitBlock(SBlock userBlockNode, Scope scope); + void visitIf(SIf userIfNode, Scope scope); + void visitIfElse(SIfElse userIfElseNode, Scope scope); + void visitWhile(SWhile userWhileNode, Scope scope); + void visitDo(SDo userDoNode, Scope scope); + void visitFor(SFor userForNode, Scope scope); + void visitEach(SEach userEachNode, Scope scope); + void visitDeclBlock(SDeclBlock userDeclBlockNode, Scope scope); + void visitDeclaration(SDeclaration userDeclarationNode, Scope scope); + void visitReturn(SReturn userReturnNode, Scope scope); + void visitExpression(SExpression userExpressionNode, Scope scope); + void visitTry(STry userTryNode, Scope scope); + void visitCatch(SCatch userCatchNode, Scope scope); + void visitThrow(SThrow userThrowNode, Scope scope); + void visitContinue(SContinue userContinueNode, Scope scope); + void visitBreak(SBreak userBreakNode, Scope scope); void visitAssignment(EAssignment userAssignmentNode, Scope scope); + void visitUnary(EUnary userUnaryNode, Scope scope); + void visitBinary(EBinary userBinaryNode, Scope scope); + void visitBooleanComp(EBooleanComp userBooleanCompNode, Scope scope); + void visitComp(EComp userCompNode, Scope scope); + void visitExplicit(EExplicit userExplicitNode, Scope scope); + void visitInstanceof(EInstanceof userInstanceofNode, Scope scope); + void visitConditional(EConditional userConditionalNode, Scope scope); + void visitElvis(EElvis userElvisNode, Scope scope); + void visitListInit(EListInit userListInitNode, Scope scope); + void visitMapInit(EMapInit userMapInitNode, Scope scope); + void visitNewArray(ENewArray userNewArrayNode, Scope scope); + void visitNewObj(ENewObj userNewObjectNode, Scope scope); + void visitCallLocal(ECallLocal userCallLocalNode, Scope scope); + void visitBooleanConstant(EBooleanConstant userBooleanConstantNode, Scope scope); + void visitNumeric(ENumeric userNumericNode, Scope scope); + void visitDecimal(EDecimal userDecimalNode, Scope scope); + void visitString(EString userStringNode, Scope scope); + void visitNull(ENull userNullNode, Scope scope); + void visitRegex(ERegex userRegexNode, Scope scope); + void visitLambda(ELambda userLambdaNode, Scope scope); + void visitFunctionRef(EFunctionRef userFunctionRefNode, Scope scope); + void visitNewArrayFunctionRef(ENewArrayFunctionRef userNewArrayFunctionRefNode, Scope scope); + void visitSymbol(ESymbol userSymbolNode, Scope scope); + void visitDot(EDot userDotNode, Scope scope); + void visitBrace(EBrace userBraceNode, Scope scope); + void visitCall(ECall userCallNode, Scope scope); } diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/symbol/Decorations.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/symbol/Decorations.java index 38a9d7f2f8b95..320da296bdaed 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/symbol/Decorations.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/symbol/Decorations.java @@ -80,7 +80,7 @@ public interface Write extends Condition { } - public static class TargetType implements Decoration { + public static class TargetType implements Decoration { private final Class targetType; @@ -614,6 +614,7 @@ public IRNode getIRNode() { public static class Converter implements Decoration { private final LocalFunction converter; + public Converter(LocalFunction converter) { this.converter = converter; } diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/symbol/Decorator.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/symbol/Decorator.java index 5f9d9da1c4bf6..ed6ca0f68c406 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/symbol/Decorator.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/symbol/Decorator.java @@ -39,7 +39,7 @@ public Decorator(int nodeCount) { @SuppressWarnings("unchecked") public T put(int identifier, T decoration) { - return (T)decorations.get(identifier).put(decoration.getClass(), decoration); + return (T) decorations.get(identifier).put(decoration.getClass(), decoration); } public T remove(int identifier, Class type) { diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/symbol/FunctionTable.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/symbol/FunctionTable.java index baea496e72aaf..c4f2438e3becb 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/symbol/FunctionTable.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/symbol/FunctionTable.java @@ -40,12 +40,23 @@ public static class LocalFunction { protected final Method asmMethod; public LocalFunction( - String functionName, Class returnType, List> typeParameters, boolean isInternal, boolean isStatic) { + String functionName, + Class returnType, + List> typeParameters, + boolean isInternal, + boolean isStatic + ) { this(functionName, "", returnType, typeParameters, isInternal, isStatic); } - private LocalFunction(String functionName, String mangle, - Class returnType, List> typeParameters, boolean isInternal, boolean isStatic) { + private LocalFunction( + String functionName, + String mangle, + Class returnType, + List> typeParameters, + boolean isInternal, + boolean isStatic + ) { this.functionName = Objects.requireNonNull(functionName); this.mangledName = Objects.requireNonNull(mangle) + this.functionName; @@ -58,8 +69,10 @@ private LocalFunction(String functionName, String mangle, Class[] javaTypeParameters = typeParameters.stream().map(PainlessLookupUtility::typeToJavaType).toArray(Class[]::new); this.methodType = MethodType.methodType(javaReturnType, javaTypeParameters); - this.asmMethod = new org.objectweb.asm.commons.Method(mangledName, - MethodType.methodType(javaReturnType, javaTypeParameters).toMethodDescriptorString()); + this.asmMethod = new org.objectweb.asm.commons.Method( + mangledName, + MethodType.methodType(javaReturnType, javaTypeParameters).toMethodDescriptorString() + ); } public String getMangledName() { @@ -104,7 +117,12 @@ public static String buildLocalFunctionKey(String functionName, int functionArit protected Map localFunctions = new HashMap<>(); public LocalFunction addFunction( - String functionName, Class returnType, List> typeParameters, boolean isInternal, boolean isStatic) { + String functionName, + Class returnType, + List> typeParameters, + boolean isInternal, + boolean isStatic + ) { String functionKey = buildLocalFunctionKey(functionName, typeParameters.size()); LocalFunction function = new LocalFunction(functionName, returnType, typeParameters, isInternal, isStatic); @@ -112,11 +130,22 @@ public LocalFunction addFunction( return function; } - public LocalFunction addMangledFunction(String functionName, - Class returnType, List> typeParameters, boolean isInternal, boolean isStatic) { + public LocalFunction addMangledFunction( + String functionName, + Class returnType, + List> typeParameters, + boolean isInternal, + boolean isStatic + ) { String functionKey = buildLocalFunctionKey(functionName, typeParameters.size()); - LocalFunction function = - new LocalFunction(functionName, MANGLED_FUNCTION_NAME_PREFIX, returnType, typeParameters, isInternal, isStatic); + LocalFunction function = new LocalFunction( + functionName, + MANGLED_FUNCTION_NAME_PREFIX, + returnType, + typeParameters, + isInternal, + isStatic + ); localFunctions.put(functionKey, function); return function; } diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/symbol/ScriptScope.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/symbol/ScriptScope.java index 67eca4b0756e6..1a5465ebaa1c5 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/symbol/ScriptScope.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/symbol/ScriptScope.java @@ -37,8 +37,14 @@ public class ScriptScope extends Decorator { protected Set usedVariables = Collections.emptySet(); protected Map staticConstants = new HashMap<>(); - public ScriptScope(PainlessLookup painlessLookup, CompilerSettings compilerSettings, - ScriptClassInfo scriptClassInfo, String scriptName, String scriptSource, int nodeCount) { + public ScriptScope( + PainlessLookup painlessLookup, + CompilerSettings compilerSettings, + ScriptClassInfo scriptClassInfo, + String scriptName, + String scriptSource, + int nodeCount + ) { super(nodeCount); diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/symbol/SemanticScope.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/symbol/SemanticScope.java index ff29353ef761a..4f05606dffa9d 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/symbol/SemanticScope.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/symbol/SemanticScope.java @@ -338,6 +338,7 @@ public boolean replicateCondition(ANode originalNode, ANode targetNode, Class getReturnType(); + public abstract String getReturnCanonicalTypeName(); public Variable defineVariable(Location location, Class type, String name, boolean isReadOnly) { @@ -352,9 +353,10 @@ public Variable defineVariable(Location location, Class type, String name, bo } public abstract boolean isVariableDefined(String name); + public abstract Variable getVariable(Location location, String name); - // We only want to track instance method use inside of lambdas for "this" injection. It's a noop for other scopes. + // We only want to track instance method use inside of lambdas for "this" injection. It's a noop for other scopes. public void setUsesInstanceMethod() {} public boolean usesInstanceMethod() { diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/symbol/WriteScope.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/symbol/WriteScope.java index 27f7abc61594b..84776b049c5de 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/symbol/WriteScope.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/symbol/WriteScope.java @@ -137,7 +137,7 @@ protected WriteScope(WriteScope parent, boolean isTryBlock) { this.nextSlot = parent.nextSlot; } - /** Creates a script scope as the top-level scope with no labels and parameters. */ + /** Creates a script scope as the top-level scope with no labels and parameters. */ public static WriteScope newScriptScope() { return new WriteScope(); } diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/toxcontent/DecorationToXContent.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/toxcontent/DecorationToXContent.java index 069113f88b522..643818c75020e 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/toxcontent/DecorationToXContent.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/toxcontent/DecorationToXContent.java @@ -341,7 +341,7 @@ public static void ToXContent(Converter converter, XContentBuilderWrapper builde } public static void ToXContent(Decoration decoration, XContentBuilderWrapper builder) { - if (decoration instanceof TargetType) { + if (decoration instanceof TargetType) { ToXContent((TargetType) decoration, builder); } else if (decoration instanceof ValueType) { ToXContent((ValueType) decoration, builder); diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/toxcontent/UserTreeToXContent.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/toxcontent/UserTreeToXContent.java index 7ebf73028d71f..e77174e0f469e 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/toxcontent/UserTreeToXContent.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/toxcontent/UserTreeToXContent.java @@ -8,7 +8,6 @@ package org.elasticsearch.painless.toxcontent; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.painless.Operation; import org.elasticsearch.painless.node.AExpression; import org.elasticsearch.painless.node.ANode; @@ -61,6 +60,7 @@ import org.elasticsearch.painless.symbol.Decorator.Condition; import org.elasticsearch.painless.symbol.Decorator.Decoration; import org.elasticsearch.painless.symbol.ScriptScope; +import org.elasticsearch.xcontent.XContentBuilder; import java.util.Comparator; import java.util.List; @@ -670,9 +670,10 @@ private void decorations(ANode node, ScriptScope scope) { if (decorations.isEmpty() == false) { builder.startArray(Fields.DECORATIONS); - List> dkeys = decorations.keySet().stream() - .sorted(Comparator.comparing(Class::getName)) - .collect(Collectors.toList()); + List> dkeys = decorations.keySet() + .stream() + .sorted(Comparator.comparing(Class::getName)) + .collect(Collectors.toList()); for (Class dkey : dkeys) { DecorationToXContent.ToXContent(decorations.get(dkey), builder); diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/toxcontent/XContentBuilderWrapper.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/toxcontent/XContentBuilderWrapper.java index 7be645ef86cf6..e6759392452df 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/toxcontent/XContentBuilderWrapper.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/toxcontent/XContentBuilderWrapper.java @@ -41,7 +41,7 @@ public void startObject() { } } - public void startObject(String name) { + public void startObject(String name) { try { builder.startObject(name); } catch (IOException io) { @@ -81,7 +81,7 @@ public void endArray() { } } - public void field(String name) { + public void field(String name) { try { builder.field(name); } catch (IOException io) { @@ -89,7 +89,7 @@ public void field(String name) { } } - public void field(String name, Object value) { + public void field(String name, Object value) { try { if (value instanceof Character) { builder.field(name, ((Character) value).charValue()); @@ -104,7 +104,7 @@ public void field(String name, Object value) { } } - public void field(String name, String value) { + public void field(String name, String value) { try { builder.field(name, value); } catch (IOException io) { @@ -112,11 +112,11 @@ public void field(String name, String value) { } } - public void field(String name, Class value) { + public void field(String name, Class value) { field(name, value.getName()); } - public void field(String name, int value) { + public void field(String name, int value) { try { builder.field(name, value); } catch (IOException io) { @@ -124,7 +124,7 @@ public void field(String name, int value) { } } - public void field(String name, boolean value) { + public void field(String name, boolean value) { try { builder.field(name, value); } catch (IOException io) { @@ -132,7 +132,7 @@ public void field(String name, boolean value) { } } - public void field(String name, List values) { + public void field(String name, List values) { try { builder.field(name, values); } catch (IOException io) { diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/AdditionTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/AdditionTests.java index 2e47987455a87..fe907a1c8640d 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/AdditionTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/AdditionTests.java @@ -9,7 +9,7 @@ package org.elasticsearch.painless; /** Tests for addition operator across all types */ -//TODO: NaN/Inf/overflow/... +// TODO: NaN/Inf/overflow/... public class AdditionTests extends ScriptTestCase { public void testBasics() throws Exception { @@ -17,172 +17,172 @@ public void testBasics() throws Exception { } public void testInt() throws Exception { - assertEquals(1+1, exec("int x = 1; int y = 1; return x+y;")); - assertEquals(1+2, exec("int x = 1; int y = 2; return x+y;")); - assertEquals(5+10, exec("int x = 5; int y = 10; return x+y;")); - assertEquals(1+1+2, exec("int x = 1; int y = 1; int z = 2; return x+y+z;")); - assertEquals((1+1)+2, exec("int x = 1; int y = 1; int z = 2; return (x+y)+z;")); - assertEquals(1+(1+2), exec("int x = 1; int y = 1; int z = 2; return x+(y+z);")); - assertEquals(0+1, exec("int x = 0; int y = 1; return x+y;")); - assertEquals(1+0, exec("int x = 1; int y = 0; return x+y;")); - assertEquals(0+0, exec("int x = 0; int y = 0; return x+y;")); - assertEquals(0+0, exec("int x = 0; int y = 0; return x+y;")); + assertEquals(1 + 1, exec("int x = 1; int y = 1; return x+y;")); + assertEquals(1 + 2, exec("int x = 1; int y = 2; return x+y;")); + assertEquals(5 + 10, exec("int x = 5; int y = 10; return x+y;")); + assertEquals(1 + 1 + 2, exec("int x = 1; int y = 1; int z = 2; return x+y+z;")); + assertEquals((1 + 1) + 2, exec("int x = 1; int y = 1; int z = 2; return (x+y)+z;")); + assertEquals(1 + (1 + 2), exec("int x = 1; int y = 1; int z = 2; return x+(y+z);")); + assertEquals(0 + 1, exec("int x = 0; int y = 1; return x+y;")); + assertEquals(1 + 0, exec("int x = 1; int y = 0; return x+y;")); + assertEquals(0 + 0, exec("int x = 0; int y = 0; return x+y;")); + assertEquals(0 + 0, exec("int x = 0; int y = 0; return x+y;")); } public void testIntConst() throws Exception { - assertEquals(1+1, exec("return 1+1;")); - assertEquals(1+2, exec("return 1+2;")); - assertEquals(5+10, exec("return 5+10;")); - assertEquals(1+1+2, exec("return 1+1+2;")); - assertEquals((1+1)+2, exec("return (1+1)+2;")); - assertEquals(1+(1+2), exec("return 1+(1+2);")); - assertEquals(0+1, exec("return 0+1;")); - assertEquals(1+0, exec("return 1+0;")); - assertEquals(0+0, exec("return 0+0;")); + assertEquals(1 + 1, exec("return 1+1;")); + assertEquals(1 + 2, exec("return 1+2;")); + assertEquals(5 + 10, exec("return 5+10;")); + assertEquals(1 + 1 + 2, exec("return 1+1+2;")); + assertEquals((1 + 1) + 2, exec("return (1+1)+2;")); + assertEquals(1 + (1 + 2), exec("return 1+(1+2);")); + assertEquals(0 + 1, exec("return 0+1;")); + assertEquals(1 + 0, exec("return 1+0;")); + assertEquals(0 + 0, exec("return 0+0;")); } public void testByte() throws Exception { - assertEquals((byte)1+(byte)1, exec("byte x = 1; byte y = 1; return x+y;")); - assertEquals((byte)1+(byte)2, exec("byte x = 1; byte y = 2; return x+y;")); - assertEquals((byte)5+(byte)10, exec("byte x = 5; byte y = 10; return x+y;")); - assertEquals((byte)1+(byte)1+(byte)2, exec("byte x = 1; byte y = 1; byte z = 2; return x+y+z;")); - assertEquals(((byte)1+(byte)1)+(byte)2, exec("byte x = 1; byte y = 1; byte z = 2; return (x+y)+z;")); - assertEquals((byte)1+((byte)1+(byte)2), exec("byte x = 1; byte y = 1; byte z = 2; return x+(y+z);")); - assertEquals((byte)0+(byte)1, exec("byte x = 0; byte y = 1; return x+y;")); - assertEquals((byte)1+(byte)0, exec("byte x = 1; byte y = 0; return x+y;")); - assertEquals((byte)0+(byte)0, exec("byte x = 0; byte y = 0; return x+y;")); + assertEquals((byte) 1 + (byte) 1, exec("byte x = 1; byte y = 1; return x+y;")); + assertEquals((byte) 1 + (byte) 2, exec("byte x = 1; byte y = 2; return x+y;")); + assertEquals((byte) 5 + (byte) 10, exec("byte x = 5; byte y = 10; return x+y;")); + assertEquals((byte) 1 + (byte) 1 + (byte) 2, exec("byte x = 1; byte y = 1; byte z = 2; return x+y+z;")); + assertEquals(((byte) 1 + (byte) 1) + (byte) 2, exec("byte x = 1; byte y = 1; byte z = 2; return (x+y)+z;")); + assertEquals((byte) 1 + ((byte) 1 + (byte) 2), exec("byte x = 1; byte y = 1; byte z = 2; return x+(y+z);")); + assertEquals((byte) 0 + (byte) 1, exec("byte x = 0; byte y = 1; return x+y;")); + assertEquals((byte) 1 + (byte) 0, exec("byte x = 1; byte y = 0; return x+y;")); + assertEquals((byte) 0 + (byte) 0, exec("byte x = 0; byte y = 0; return x+y;")); } public void testByteConst() throws Exception { - assertEquals((byte)1+(byte)1, exec("return (byte)1+(byte)1;")); - assertEquals((byte)1+(byte)2, exec("return (byte)1+(byte)2;")); - assertEquals((byte)5+(byte)10, exec("return (byte)5+(byte)10;")); - assertEquals((byte)1+(byte)1+(byte)2, exec("return (byte)1+(byte)1+(byte)2;")); - assertEquals(((byte)1+(byte)1)+(byte)2, exec("return ((byte)1+(byte)1)+(byte)2;")); - assertEquals((byte)1+((byte)1+(byte)2), exec("return (byte)1+((byte)1+(byte)2);")); - assertEquals((byte)0+(byte)1, exec("return (byte)0+(byte)1;")); - assertEquals((byte)1+(byte)0, exec("return (byte)1+(byte)0;")); - assertEquals((byte)0+(byte)0, exec("return (byte)0+(byte)0;")); + assertEquals((byte) 1 + (byte) 1, exec("return (byte)1+(byte)1;")); + assertEquals((byte) 1 + (byte) 2, exec("return (byte)1+(byte)2;")); + assertEquals((byte) 5 + (byte) 10, exec("return (byte)5+(byte)10;")); + assertEquals((byte) 1 + (byte) 1 + (byte) 2, exec("return (byte)1+(byte)1+(byte)2;")); + assertEquals(((byte) 1 + (byte) 1) + (byte) 2, exec("return ((byte)1+(byte)1)+(byte)2;")); + assertEquals((byte) 1 + ((byte) 1 + (byte) 2), exec("return (byte)1+((byte)1+(byte)2);")); + assertEquals((byte) 0 + (byte) 1, exec("return (byte)0+(byte)1;")); + assertEquals((byte) 1 + (byte) 0, exec("return (byte)1+(byte)0;")); + assertEquals((byte) 0 + (byte) 0, exec("return (byte)0+(byte)0;")); } public void testChar() throws Exception { - assertEquals((char)1+(char)1, exec("char x = 1; char y = 1; return x+y;")); - assertEquals((char)1+(char)2, exec("char x = 1; char y = 2; return x+y;")); - assertEquals((char)5+(char)10, exec("char x = 5; char y = 10; return x+y;")); - assertEquals((char)1+(char)1+(char)2, exec("char x = 1; char y = 1; char z = 2; return x+y+z;")); - assertEquals(((char)1+(char)1)+(char)2, exec("char x = 1; char y = 1; char z = 2; return (x+y)+z;")); - assertEquals((char)1+((char)1+(char)2), exec("char x = 1; char y = 1; char z = 2; return x+(y+z);")); - assertEquals((char)0+(char)1, exec("char x = 0; char y = 1; return x+y;")); - assertEquals((char)1+(char)0, exec("char x = 1; char y = 0; return x+y;")); - assertEquals((char)0+(char)0, exec("char x = 0; char y = 0; return x+y;")); + assertEquals((char) 1 + (char) 1, exec("char x = 1; char y = 1; return x+y;")); + assertEquals((char) 1 + (char) 2, exec("char x = 1; char y = 2; return x+y;")); + assertEquals((char) 5 + (char) 10, exec("char x = 5; char y = 10; return x+y;")); + assertEquals((char) 1 + (char) 1 + (char) 2, exec("char x = 1; char y = 1; char z = 2; return x+y+z;")); + assertEquals(((char) 1 + (char) 1) + (char) 2, exec("char x = 1; char y = 1; char z = 2; return (x+y)+z;")); + assertEquals((char) 1 + ((char) 1 + (char) 2), exec("char x = 1; char y = 1; char z = 2; return x+(y+z);")); + assertEquals((char) 0 + (char) 1, exec("char x = 0; char y = 1; return x+y;")); + assertEquals((char) 1 + (char) 0, exec("char x = 1; char y = 0; return x+y;")); + assertEquals((char) 0 + (char) 0, exec("char x = 0; char y = 0; return x+y;")); } public void testCharConst() throws Exception { - assertEquals((char)1+(char)1, exec("return (char)1+(char)1;")); - assertEquals((char)1+(char)2, exec("return (char)1+(char)2;")); - assertEquals((char)5+(char)10, exec("return (char)5+(char)10;")); - assertEquals((char)1+(char)1+(char)2, exec("return (char)1+(char)1+(char)2;")); - assertEquals(((char)1+(char)1)+(char)2, exec("return ((char)1+(char)1)+(char)2;")); - assertEquals((char)1+((char)1+(char)2), exec("return (char)1+((char)1+(char)2);")); - assertEquals((char)0+(char)1, exec("return (char)0+(char)1;")); - assertEquals((char)1+(char)0, exec("return (char)1+(char)0;")); - assertEquals((char)0+(char)0, exec("return (char)0+(char)0;")); + assertEquals((char) 1 + (char) 1, exec("return (char)1+(char)1;")); + assertEquals((char) 1 + (char) 2, exec("return (char)1+(char)2;")); + assertEquals((char) 5 + (char) 10, exec("return (char)5+(char)10;")); + assertEquals((char) 1 + (char) 1 + (char) 2, exec("return (char)1+(char)1+(char)2;")); + assertEquals(((char) 1 + (char) 1) + (char) 2, exec("return ((char)1+(char)1)+(char)2;")); + assertEquals((char) 1 + ((char) 1 + (char) 2), exec("return (char)1+((char)1+(char)2);")); + assertEquals((char) 0 + (char) 1, exec("return (char)0+(char)1;")); + assertEquals((char) 1 + (char) 0, exec("return (char)1+(char)0;")); + assertEquals((char) 0 + (char) 0, exec("return (char)0+(char)0;")); } public void testShort() throws Exception { - assertEquals((short)1+(short)1, exec("short x = 1; short y = 1; return x+y;")); - assertEquals((short)1+(short)2, exec("short x = 1; short y = 2; return x+y;")); - assertEquals((short)5+(short)10, exec("short x = 5; short y = 10; return x+y;")); - assertEquals((short)1+(short)1+(short)2, exec("short x = 1; short y = 1; short z = 2; return x+y+z;")); - assertEquals(((short)1+(short)1)+(short)2, exec("short x = 1; short y = 1; short z = 2; return (x+y)+z;")); - assertEquals((short)1+((short)1+(short)2), exec("short x = 1; short y = 1; short z = 2; return x+(y+z);")); - assertEquals((short)0+(short)1, exec("short x = 0; short y = 1; return x+y;")); - assertEquals((short)1+(short)0, exec("short x = 1; short y = 0; return x+y;")); - assertEquals((short)0+(short)0, exec("short x = 0; short y = 0; return x+y;")); + assertEquals((short) 1 + (short) 1, exec("short x = 1; short y = 1; return x+y;")); + assertEquals((short) 1 + (short) 2, exec("short x = 1; short y = 2; return x+y;")); + assertEquals((short) 5 + (short) 10, exec("short x = 5; short y = 10; return x+y;")); + assertEquals((short) 1 + (short) 1 + (short) 2, exec("short x = 1; short y = 1; short z = 2; return x+y+z;")); + assertEquals(((short) 1 + (short) 1) + (short) 2, exec("short x = 1; short y = 1; short z = 2; return (x+y)+z;")); + assertEquals((short) 1 + ((short) 1 + (short) 2), exec("short x = 1; short y = 1; short z = 2; return x+(y+z);")); + assertEquals((short) 0 + (short) 1, exec("short x = 0; short y = 1; return x+y;")); + assertEquals((short) 1 + (short) 0, exec("short x = 1; short y = 0; return x+y;")); + assertEquals((short) 0 + (short) 0, exec("short x = 0; short y = 0; return x+y;")); } public void testShortConst() throws Exception { - assertEquals((short)1+(short)1, exec("return (short)1+(short)1;")); - assertEquals((short)1+(short)2, exec("return (short)1+(short)2;")); - assertEquals((short)5+(short)10, exec("return (short)5+(short)10;")); - assertEquals((short)1+(short)1+(short)2, exec("return (short)1+(short)1+(short)2;")); - assertEquals(((short)1+(short)1)+(short)2, exec("return ((short)1+(short)1)+(short)2;")); - assertEquals((short)1+((short)1+(short)2), exec("return (short)1+((short)1+(short)2);")); - assertEquals((short)0+(short)1, exec("return (short)0+(short)1;")); - assertEquals((short)1+(short)0, exec("return (short)1+(short)0;")); - assertEquals((short)0+(short)0, exec("return (short)0+(short)0;")); + assertEquals((short) 1 + (short) 1, exec("return (short)1+(short)1;")); + assertEquals((short) 1 + (short) 2, exec("return (short)1+(short)2;")); + assertEquals((short) 5 + (short) 10, exec("return (short)5+(short)10;")); + assertEquals((short) 1 + (short) 1 + (short) 2, exec("return (short)1+(short)1+(short)2;")); + assertEquals(((short) 1 + (short) 1) + (short) 2, exec("return ((short)1+(short)1)+(short)2;")); + assertEquals((short) 1 + ((short) 1 + (short) 2), exec("return (short)1+((short)1+(short)2);")); + assertEquals((short) 0 + (short) 1, exec("return (short)0+(short)1;")); + assertEquals((short) 1 + (short) 0, exec("return (short)1+(short)0;")); + assertEquals((short) 0 + (short) 0, exec("return (short)0+(short)0;")); } public void testLong() throws Exception { - assertEquals(1L+1L, exec("long x = 1; long y = 1; return x+y;")); - assertEquals(1L+2L, exec("long x = 1; long y = 2; return x+y;")); - assertEquals(5L+10L, exec("long x = 5; long y = 10; return x+y;")); - assertEquals(1L+1L+2L, exec("long x = 1; long y = 1; long z = 2; return x+y+z;")); - assertEquals((1L+1L)+2L, exec("long x = 1; long y = 1; long z = 2; return (x+y)+z;")); - assertEquals(1L+(1L+2L), exec("long x = 1; long y = 1; long z = 2; return x+(y+z);")); - assertEquals(0L+1L, exec("long x = 0; long y = 1; return x+y;")); - assertEquals(1L+0L, exec("long x = 1; long y = 0; return x+y;")); - assertEquals(0L+0L, exec("long x = 0; long y = 0; return x+y;")); + assertEquals(1L + 1L, exec("long x = 1; long y = 1; return x+y;")); + assertEquals(1L + 2L, exec("long x = 1; long y = 2; return x+y;")); + assertEquals(5L + 10L, exec("long x = 5; long y = 10; return x+y;")); + assertEquals(1L + 1L + 2L, exec("long x = 1; long y = 1; long z = 2; return x+y+z;")); + assertEquals((1L + 1L) + 2L, exec("long x = 1; long y = 1; long z = 2; return (x+y)+z;")); + assertEquals(1L + (1L + 2L), exec("long x = 1; long y = 1; long z = 2; return x+(y+z);")); + assertEquals(0L + 1L, exec("long x = 0; long y = 1; return x+y;")); + assertEquals(1L + 0L, exec("long x = 1; long y = 0; return x+y;")); + assertEquals(0L + 0L, exec("long x = 0; long y = 0; return x+y;")); } public void testLongConst() throws Exception { - assertEquals(1L+1L, exec("return 1L+1L;")); - assertEquals(1L+2L, exec("return 1L+2L;")); - assertEquals(5L+10L, exec("return 5L+10L;")); - assertEquals(1L+1L+2L, exec("return 1L+1L+2L;")); - assertEquals((1L+1L)+2L, exec("return (1L+1L)+2L;")); - assertEquals(1L+(1L+2L), exec("return 1L+(1L+2L);")); - assertEquals(0L+1L, exec("return 0L+1L;")); - assertEquals(1L+0L, exec("return 1L+0L;")); - assertEquals(0L+0L, exec("return 0L+0L;")); + assertEquals(1L + 1L, exec("return 1L+1L;")); + assertEquals(1L + 2L, exec("return 1L+2L;")); + assertEquals(5L + 10L, exec("return 5L+10L;")); + assertEquals(1L + 1L + 2L, exec("return 1L+1L+2L;")); + assertEquals((1L + 1L) + 2L, exec("return (1L+1L)+2L;")); + assertEquals(1L + (1L + 2L), exec("return 1L+(1L+2L);")); + assertEquals(0L + 1L, exec("return 0L+1L;")); + assertEquals(1L + 0L, exec("return 1L+0L;")); + assertEquals(0L + 0L, exec("return 0L+0L;")); } public void testFloat() throws Exception { - assertEquals(1F+1F, exec("float x = 1F; float y = 1F; return x+y;")); - assertEquals(1F+2F, exec("float x = 1F; float y = 2F; return x+y;")); - assertEquals(5F+10F, exec("float x = 5F; float y = 10F; return x+y;")); - assertEquals(1F+1F+2F, exec("float x = 1F; float y = 1F; float z = 2F; return x+y+z;")); - assertEquals((1F+1F)+2F, exec("float x = 1F; float y = 1F; float z = 2F; return (x+y)+z;")); - assertEquals((1F+1F)+2F, exec("float x = 1F; float y = 1F; float z = 2F; return x+(y+z);")); - assertEquals(0F+1F, exec("float x = 0F; float y = 1F; return x+y;")); - assertEquals(1F+0F, exec("float x = 1F; float y = 0F; return x+y;")); - assertEquals(0F+0F, exec("float x = 0F; float y = 0F; return x+y;")); + assertEquals(1F + 1F, exec("float x = 1F; float y = 1F; return x+y;")); + assertEquals(1F + 2F, exec("float x = 1F; float y = 2F; return x+y;")); + assertEquals(5F + 10F, exec("float x = 5F; float y = 10F; return x+y;")); + assertEquals(1F + 1F + 2F, exec("float x = 1F; float y = 1F; float z = 2F; return x+y+z;")); + assertEquals((1F + 1F) + 2F, exec("float x = 1F; float y = 1F; float z = 2F; return (x+y)+z;")); + assertEquals((1F + 1F) + 2F, exec("float x = 1F; float y = 1F; float z = 2F; return x+(y+z);")); + assertEquals(0F + 1F, exec("float x = 0F; float y = 1F; return x+y;")); + assertEquals(1F + 0F, exec("float x = 1F; float y = 0F; return x+y;")); + assertEquals(0F + 0F, exec("float x = 0F; float y = 0F; return x+y;")); } public void testFloatConst() throws Exception { - assertEquals(1F+1F, exec("return 1F+1F;")); - assertEquals(1F+2F, exec("return 1F+2F;")); - assertEquals(5F+10F, exec("return 5F+10F;")); - assertEquals(1F+1F+2F, exec("return 1F+1F+2F;")); - assertEquals((1F+1F)+2F, exec("return (1F+1F)+2F;")); - assertEquals(1F+(1F+2F), exec("return 1F+(1F+2F);")); - assertEquals(0F+1F, exec("return 0F+1F;")); - assertEquals(1F+0F, exec("return 1F+0F;")); - assertEquals(0F+0F, exec("return 0F+0F;")); + assertEquals(1F + 1F, exec("return 1F+1F;")); + assertEquals(1F + 2F, exec("return 1F+2F;")); + assertEquals(5F + 10F, exec("return 5F+10F;")); + assertEquals(1F + 1F + 2F, exec("return 1F+1F+2F;")); + assertEquals((1F + 1F) + 2F, exec("return (1F+1F)+2F;")); + assertEquals(1F + (1F + 2F), exec("return 1F+(1F+2F);")); + assertEquals(0F + 1F, exec("return 0F+1F;")); + assertEquals(1F + 0F, exec("return 1F+0F;")); + assertEquals(0F + 0F, exec("return 0F+0F;")); } public void testDouble() throws Exception { - assertEquals(1.0+1.0, exec("double x = 1.0; double y = 1.0; return x+y;")); - assertEquals(1.0+2.0, exec("double x = 1.0; double y = 2.0; return x+y;")); - assertEquals(5.0+10.0, exec("double x = 5.0; double y = 10.0; return x+y;")); - assertEquals(1.0+1.0+2.0, exec("double x = 1.0; double y = 1.0; double z = 2.0; return x+y+z;")); - assertEquals((1.0+1.0)+2.0, exec("double x = 1.0; double y = 1.0; double z = 2.0; return (x+y)+z;")); - assertEquals(1.0+(1.0+2.0), exec("double x = 1.0; double y = 1.0; double z = 2.0; return x+(y+z);")); - assertEquals(0.0+1.0, exec("double x = 0.0; double y = 1.0; return x+y;")); - assertEquals(1.0+0.0, exec("double x = 1.0; double y = 0.0; return x+y;")); - assertEquals(0.0+0.0, exec("double x = 0.0; double y = 0.0; return x+y;")); + assertEquals(1.0 + 1.0, exec("double x = 1.0; double y = 1.0; return x+y;")); + assertEquals(1.0 + 2.0, exec("double x = 1.0; double y = 2.0; return x+y;")); + assertEquals(5.0 + 10.0, exec("double x = 5.0; double y = 10.0; return x+y;")); + assertEquals(1.0 + 1.0 + 2.0, exec("double x = 1.0; double y = 1.0; double z = 2.0; return x+y+z;")); + assertEquals((1.0 + 1.0) + 2.0, exec("double x = 1.0; double y = 1.0; double z = 2.0; return (x+y)+z;")); + assertEquals(1.0 + (1.0 + 2.0), exec("double x = 1.0; double y = 1.0; double z = 2.0; return x+(y+z);")); + assertEquals(0.0 + 1.0, exec("double x = 0.0; double y = 1.0; return x+y;")); + assertEquals(1.0 + 0.0, exec("double x = 1.0; double y = 0.0; return x+y;")); + assertEquals(0.0 + 0.0, exec("double x = 0.0; double y = 0.0; return x+y;")); } public void testDoubleConst() throws Exception { - assertEquals(1.0+1.0, exec("return 1.0+1.0;")); - assertEquals(1.0+2.0, exec("return 1.0+2.0;")); - assertEquals(5.0+10.0, exec("return 5.0+10.0;")); - assertEquals(1.0+1.0+2.0, exec("return 1.0+1.0+2.0;")); - assertEquals((1.0+1.0)+2.0, exec("return (1.0+1.0)+2.0;")); - assertEquals(1.0+(1.0+2.0), exec("return 1.0+(1.0+2.0);")); - assertEquals(0.0+1.0, exec("return 0.0+1.0;")); - assertEquals(1.0+0.0, exec("return 1.0+0.0;")); - assertEquals(0.0+0.0, exec("return 0.0+0.0;")); + assertEquals(1.0 + 1.0, exec("return 1.0+1.0;")); + assertEquals(1.0 + 2.0, exec("return 1.0+2.0;")); + assertEquals(5.0 + 10.0, exec("return 5.0+10.0;")); + assertEquals(1.0 + 1.0 + 2.0, exec("return 1.0+1.0+2.0;")); + assertEquals((1.0 + 1.0) + 2.0, exec("return (1.0+1.0)+2.0;")); + assertEquals(1.0 + (1.0 + 2.0), exec("return 1.0+(1.0+2.0);")); + assertEquals(0.0 + 1.0, exec("return 0.0+1.0;")); + assertEquals(1.0 + 0.0, exec("return 1.0+0.0;")); + assertEquals(0.0 + 0.0, exec("return 0.0+0.0;")); } public void testDef() { @@ -360,15 +360,9 @@ public void testDefTypedRHS() { } public void testDefNulls() { - expectScriptThrows(NullPointerException.class, () -> { - exec("def x = null; int y = 1; return x + y"); - }); - expectScriptThrows(NullPointerException.class, () -> { - exec("int x = 1; def y = null; return x + y"); - }); - expectScriptThrows(NullPointerException.class, () -> { - exec("def x = null; def y = 1; return x + y"); - }); + expectScriptThrows(NullPointerException.class, () -> { exec("def x = null; int y = 1; return x + y"); }); + expectScriptThrows(NullPointerException.class, () -> { exec("int x = 1; def y = null; return x + y"); }); + expectScriptThrows(NullPointerException.class, () -> { exec("def x = null; def y = 1; return x + y"); }); } public void testCompoundAssignment() { diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/AnalyzerCasterTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/AnalyzerCasterTests.java index e29827044936a..10d09f08300ed 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/AnalyzerCasterTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/AnalyzerCasterTests.java @@ -28,8 +28,10 @@ private static void assertCast(Class actual, Class expected, boolean mustB assertEquals(expected, cast.targetType); if (mustBeExplicit) { - ClassCastException error = expectThrows(ClassCastException.class, - () -> AnalyzerCaster.getLegalCast(location, actual, expected, false, false)); + ClassCastException error = expectThrows( + ClassCastException.class, + () -> AnalyzerCaster.getLegalCast(location, actual, expected, false, false) + ); assertTrue(error.getMessage().startsWith("Cannot cast")); } else { cast = AnalyzerCaster.getLegalCast(location, actual, expected, false, false); diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/AndTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/AndTests.java index 9b79e162924c5..c80d82b6776cf 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/AndTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/AndTests.java @@ -43,21 +43,13 @@ public void testLongConst() throws Exception { } public void testIllegal() throws Exception { - expectScriptThrows(ClassCastException.class, () -> { - exec("float x = (float)4; int y = 1; return x & y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("double x = (double)4; int y = 1; return x & y"); - }); + expectScriptThrows(ClassCastException.class, () -> { exec("float x = (float)4; int y = 1; return x & y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("double x = (double)4; int y = 1; return x & y"); }); } public void testDef() { - expectScriptThrows(ClassCastException.class, () -> { - exec("def x = (float)4; def y = (byte)1; return x & y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("def x = (double)4; def y = (byte)1; return x & y"); - }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = (float)4; def y = (byte)1; return x & y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = (double)4; def y = (byte)1; return x & y"); }); assertEquals(0, exec("def x = (byte)4; def y = (byte)1; return x & y")); assertEquals(0, exec("def x = (short)4; def y = (byte)1; return x & y")); assertEquals(0, exec("def x = (char)4; def y = (byte)1; return x & y")); @@ -94,19 +86,15 @@ public void testDef() { assertEquals(0, exec("def x = (int)4; def y = (int)1; return x & y")); assertEquals(0L, exec("def x = (long)4; def y = (long)1; return x & y")); - assertEquals(true, exec("def x = true; def y = true; return x & y")); + assertEquals(true, exec("def x = true; def y = true; return x & y")); assertEquals(false, exec("def x = true; def y = false; return x & y")); assertEquals(false, exec("def x = false; def y = true; return x & y")); assertEquals(false, exec("def x = false; def y = false; return x & y")); } public void testDefTypedLHS() { - expectScriptThrows(ClassCastException.class, () -> { - exec("float x = (float)4; def y = (byte)1; return x & y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("double x = (double)4; def y = (byte)1; return x & y"); - }); + expectScriptThrows(ClassCastException.class, () -> { exec("float x = (float)4; def y = (byte)1; return x & y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("double x = (double)4; def y = (byte)1; return x & y"); }); assertEquals(0, exec("byte x = (byte)4; def y = (byte)1; return x & y")); assertEquals(0, exec("short x = (short)4; def y = (byte)1; return x & y")); assertEquals(0, exec("char x = (char)4; def y = (byte)1; return x & y")); @@ -143,19 +131,15 @@ public void testDefTypedLHS() { assertEquals(0, exec("int x = (int)4; def y = (int)1; return x & y")); assertEquals(0L, exec("long x = (long)4; def y = (long)1; return x & y")); - assertEquals(true, exec("boolean x = true; def y = true; return x & y")); + assertEquals(true, exec("boolean x = true; def y = true; return x & y")); assertEquals(false, exec("boolean x = true; def y = false; return x & y")); assertEquals(false, exec("boolean x = false; def y = true; return x & y")); assertEquals(false, exec("boolean x = false; def y = false; return x & y")); } public void testDefTypedRHS() { - expectScriptThrows(ClassCastException.class, () -> { - exec("def x = (float)4; byte y = (byte)1; return x & y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("def x = (double)4; byte y = (byte)1; return x & y"); - }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = (float)4; byte y = (byte)1; return x & y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = (double)4; byte y = (byte)1; return x & y"); }); assertEquals(0, exec("def x = (byte)4; byte y = (byte)1; return x & y")); assertEquals(0, exec("def x = (short)4; byte y = (byte)1; return x & y")); assertEquals(0, exec("def x = (char)4; byte y = (byte)1; return x & y")); @@ -192,7 +176,7 @@ public void testDefTypedRHS() { assertEquals(0, exec("def x = (int)4; int y = (int)1; return x & y")); assertEquals(0L, exec("def x = (long)4; long y = (long)1; return x & y")); - assertEquals(true, exec("def x = true; boolean y = true; return x & y")); + assertEquals(true, exec("def x = true; boolean y = true; return x & y")); assertEquals(false, exec("def x = true; boolean y = false; return x & y")); assertEquals(false, exec("def x = false; boolean y = true; return x & y")); assertEquals(false, exec("def x = false; boolean y = false; return x & y")); @@ -222,18 +206,10 @@ public void testCompoundAssignment() { } public void testBogusCompoundAssignment() { - expectScriptThrows(ClassCastException.class, () -> { - exec("float x = 4; int y = 1; x &= y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("double x = 4; int y = 1; x &= y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("int x = 4; float y = 1; x &= y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("int x = 4; double y = 1; x &= y"); - }); + expectScriptThrows(ClassCastException.class, () -> { exec("float x = 4; int y = 1; x &= y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("double x = 4; int y = 1; x &= y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("int x = 4; float y = 1; x &= y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("int x = 4; double y = 1; x &= y"); }); } public void testDefCompoundAssignment() { @@ -260,17 +236,9 @@ public void testDefCompoundAssignment() { } public void testDefBogusCompoundAssignment() { - expectScriptThrows(ClassCastException.class, () -> { - exec("def x = 4F; int y = 1; x &= y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("def x = 4D; int y = 1; x &= y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("int x = 4; def y = 1F; x &= y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("int x = 4; def y = 1D; x &= y"); - }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = 4F; int y = 1; x &= y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = 4D; int y = 1; x &= y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("int x = 4; def y = 1F; x &= y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("int x = 4; def y = 1D; x &= y"); }); } } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/ArrayLikeObjectTestCase.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/ArrayLikeObjectTestCase.java index c61684acbe7f4..6edc5cf1a5db1 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/ArrayLikeObjectTestCase.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/ArrayLikeObjectTestCase.java @@ -22,11 +22,13 @@ public abstract class ArrayLikeObjectTestCase extends ScriptTestCase { * lists. */ protected abstract String declType(String valueType); + /** * Build the string for calling the constructor for the array-like-object to test. So {@code new int[5]} for arrays and * {@code [0, 0, 0, 0, 0]} or {@code [null, null, null, null, null]} for lists. */ protected abstract String valueCtorCall(String valueType, int size); + /** * Matcher for the message of the out of bounds exceptions thrown for too negative or too positive offsets. */ @@ -41,14 +43,14 @@ private void arrayLoadStoreTestCase(boolean declareAsDef, String valueType, Obje assertEquals(val, exec(decl + "; x[ 0] = params.val; return x[-5];", singletonMap("val", val), true)); assertEquals(val, exec(decl + "; x[-5] = params.val; return x[-5];", singletonMap("val", val), true)); - expectOutOfBounds( 6, decl + "; return x[ 6]", val); + expectOutOfBounds(6, decl + "; return x[ 6]", val); expectOutOfBounds(-1, decl + "; return x[-6]", val); - expectOutOfBounds( 6, decl + "; x[ 6] = params.val; return 0", val); + expectOutOfBounds(6, decl + "; x[ 6] = params.val; return 0", val); expectOutOfBounds(-1, decl + "; x[-6] = params.val; return 0", val); if (valPlusOne != null) { - assertEquals(val, exec(decl + "; x[0] = params.val; x[ 0] = x[ 0]++; return x[0];", singletonMap("val", val), true)); - assertEquals(val, exec(decl + "; x[0] = params.val; x[ 0] = x[-5]++; return x[0];", singletonMap("val", val), true)); + assertEquals(val, exec(decl + "; x[0] = params.val; x[ 0] = x[ 0]++; return x[0];", singletonMap("val", val), true)); + assertEquals(val, exec(decl + "; x[0] = params.val; x[ 0] = x[-5]++; return x[0];", singletonMap("val", val), true)); assertEquals(valPlusOne, exec(decl + "; x[0] = params.val; x[ 0] = ++x[ 0]; return x[0];", singletonMap("val", val), true)); assertEquals(valPlusOne, exec(decl + "; x[0] = params.val; x[ 0] = ++x[-5]; return x[0];", singletonMap("val", val), true)); assertEquals(valPlusOne, exec(decl + "; x[0] = params.val; x[ 0]++ ; return x[0];", singletonMap("val", val), true)); @@ -56,18 +58,20 @@ private void arrayLoadStoreTestCase(boolean declareAsDef, String valueType, Obje assertEquals(valPlusOne, exec(decl + "; x[0] = params.val; x[ 0] += 1 ; return x[0];", singletonMap("val", val), true)); assertEquals(valPlusOne, exec(decl + "; x[0] = params.val; x[-5] += 1 ; return x[0];", singletonMap("val", val), true)); - expectOutOfBounds( 6, decl + "; return x[ 6]++", val); + expectOutOfBounds(6, decl + "; return x[ 6]++", val); expectOutOfBounds(-1, decl + "; return x[-6]++", val); - expectOutOfBounds( 6, decl + "; return ++x[ 6]", val); + expectOutOfBounds(6, decl + "; return ++x[ 6]", val); expectOutOfBounds(-1, decl + "; return ++x[-6]", val); - expectOutOfBounds( 6, decl + "; x[ 6] += 1; return 0", val); + expectOutOfBounds(6, decl + "; x[ 6] += 1; return 0", val); expectOutOfBounds(-1, decl + "; x[-6] += 1; return 0", val); } } private void expectOutOfBounds(int index, String script, Object val) { - IndexOutOfBoundsException e = expectScriptThrows(IndexOutOfBoundsException.class, () -> - exec(script, singletonMap("val", val), true)); + IndexOutOfBoundsException e = expectScriptThrows( + IndexOutOfBoundsException.class, + () -> exec(script, singletonMap("val", val), true) + ); try { /* If this fails you *might* be missing -XX:-OmitStackTraceInFastThrow in the test jvm * In Eclipse you can add this by default by going to Preference->Java->Installed JREs, @@ -82,19 +86,63 @@ private void expectOutOfBounds(int index, String script, Object val) { } } - public void testInts() { arrayLoadStoreTestCase(false, "int", 5, 6); } - public void testIntsInDef() { arrayLoadStoreTestCase(true, "int", 5, 6); } - public void testLongs() { arrayLoadStoreTestCase(false, "long", 5L, 6L); } - public void testLongsInDef() { arrayLoadStoreTestCase(true, "long", 5L, 6L); } - public void testShorts() { arrayLoadStoreTestCase(false, "short", (short) 5, (short) 6); } - public void testShortsInDef() { arrayLoadStoreTestCase(true, "short", (short) 5, (short) 6); } - public void testBytes() { arrayLoadStoreTestCase(false, "byte", (byte) 5, (byte) 6); } - public void testBytesInDef() { arrayLoadStoreTestCase(true, "byte", (byte) 5, (byte) 6); } - public void testFloats() { arrayLoadStoreTestCase(false, "float", 5.0f, 6.0f); } - public void testFloatsInDef() { arrayLoadStoreTestCase(true, "float", 5.0f, 6.0f); } - public void testDoubles() { arrayLoadStoreTestCase(false, "double", 5.0d, 6.0d); } - public void testDoublesInDef() { arrayLoadStoreTestCase(true, "double", 5.0d, 6.0d); } - public void testStrings() { arrayLoadStoreTestCase(false, "String", "cat", null); } - public void testStringsInDef() { arrayLoadStoreTestCase(true, "String", "cat", null); } - public void testDef() { arrayLoadStoreTestCase(true, "def", 5, null); } + public void testInts() { + arrayLoadStoreTestCase(false, "int", 5, 6); + } + + public void testIntsInDef() { + arrayLoadStoreTestCase(true, "int", 5, 6); + } + + public void testLongs() { + arrayLoadStoreTestCase(false, "long", 5L, 6L); + } + + public void testLongsInDef() { + arrayLoadStoreTestCase(true, "long", 5L, 6L); + } + + public void testShorts() { + arrayLoadStoreTestCase(false, "short", (short) 5, (short) 6); + } + + public void testShortsInDef() { + arrayLoadStoreTestCase(true, "short", (short) 5, (short) 6); + } + + public void testBytes() { + arrayLoadStoreTestCase(false, "byte", (byte) 5, (byte) 6); + } + + public void testBytesInDef() { + arrayLoadStoreTestCase(true, "byte", (byte) 5, (byte) 6); + } + + public void testFloats() { + arrayLoadStoreTestCase(false, "float", 5.0f, 6.0f); + } + + public void testFloatsInDef() { + arrayLoadStoreTestCase(true, "float", 5.0f, 6.0f); + } + + public void testDoubles() { + arrayLoadStoreTestCase(false, "double", 5.0d, 6.0d); + } + + public void testDoublesInDef() { + arrayLoadStoreTestCase(true, "double", 5.0d, 6.0d); + } + + public void testStrings() { + arrayLoadStoreTestCase(false, "String", "cat", null); + } + + public void testStringsInDef() { + arrayLoadStoreTestCase(true, "String", "cat", null); + } + + public void testDef() { + arrayLoadStoreTestCase(true, "def", 5, null); + } } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/ArrayTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/ArrayTests.java index 62596b426b490..004a0f0bd99b3 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/ArrayTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/ArrayTests.java @@ -33,7 +33,7 @@ protected String valueCtorCall(String valueType, int size) { protected Matcher outOfBoundsExceptionMessageMatcher(int index, int size) { if (JavaVersion.current().compareTo(JavaVersion.parse("11")) < 0) { return equalTo(Integer.toString(index)); - } else{ + } else { return equalTo("Index " + Integer.toString(index) + " out of bounds for length " + Integer.toString(size)); } } @@ -55,8 +55,7 @@ public void testArrayLengthHelper() throws Throwable { private void assertArrayLength(int length, Object array) throws Throwable { final MethodHandle mh = Def.arrayLengthGetter(array.getClass()); assertSame(array.getClass(), mh.type().parameterType(0)); - assertEquals(length, (int) mh.asType(MethodType.methodType(int.class, Object.class)) - .invokeExact(array)); + assertEquals(length, (int) mh.asType(MethodType.methodType(int.class, Object.class)).invokeExact(array)); } public void testJacksCrazyExpression1() { @@ -72,8 +71,13 @@ public void testArrayVariable() { } public void testForLoop() { - assertEquals(999*1000/2, exec("def a = new int[1000]; for (int x = 0; x < a.length; x++) { a[x] = x; } "+ - "int total = 0; for (int x = 0; x < a.length; x++) { total += a[x]; } return total;")); + assertEquals( + 999 * 1000 / 2, + exec( + "def a = new int[1000]; for (int x = 0; x < a.length; x++) { a[x] = x; } " + + "int total = 0; for (int x = 0; x < a.length; x++) { total += a[x]; } return total;" + ) + ); } /** diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/AugmentationTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/AugmentationTests.java index 18b6a014b9d05..b51f0f2657278 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/AugmentationTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/AugmentationTests.java @@ -34,13 +34,14 @@ protected Map, List> scriptContexts() { public abstract static class DigestTestScript { public static final String[] PARAMETERS = {}; + public abstract String execute(); + public interface Factory { DigestTestScript newInstance(); } - public static final ScriptContext CONTEXT = - new ScriptContext<>("test", DigestTestScript.Factory.class); + public static final ScriptContext CONTEXT = new ScriptContext<>("test", DigestTestScript.Factory.class); } public void testStatic() { @@ -59,42 +60,39 @@ public void testDef() { } public void testCapturingReference() { - assertEquals(1, exec("int foo(Supplier t) { return t.get() }" + - "ArrayList l = new ArrayList(); l.add(1);" + - "return foo(l::getLength);")); - assertEquals(1, exec("int foo(Supplier t) { return t.get() }" + - "List l = new ArrayList(); l.add(1);" + - "return foo(l::getLength);")); - assertEquals(1, exec("int foo(Supplier t) { return t.get() }" + - "def l = new ArrayList(); l.add(1);" + - "return foo(l::getLength);")); + assertEquals( + 1, + exec("int foo(Supplier t) { return t.get() }" + "ArrayList l = new ArrayList(); l.add(1);" + "return foo(l::getLength);") + ); + assertEquals( + 1, + exec("int foo(Supplier t) { return t.get() }" + "List l = new ArrayList(); l.add(1);" + "return foo(l::getLength);") + ); + assertEquals( + 1, + exec("int foo(Supplier t) { return t.get() }" + "def l = new ArrayList(); l.add(1);" + "return foo(l::getLength);") + ); } public void testIterable_Any() { - assertEquals(true, - exec("List l = new ArrayList(); l.add(1); l.any(x -> x == 1)")); + assertEquals(true, exec("List l = new ArrayList(); l.add(1); l.any(x -> x == 1)")); } public void testIterable_AsCollection() { - assertEquals(true, - exec("List l = new ArrayList(); return l.asCollection() === l")); + assertEquals(true, exec("List l = new ArrayList(); return l.asCollection() === l")); } public void testIterable_AsList() { - assertEquals(true, - exec("List l = new ArrayList(); return l.asList() === l")); - assertEquals(5, - exec("Set l = new HashSet(); l.add(5); return l.asList()[0]")); + assertEquals(true, exec("List l = new ArrayList(); return l.asList() === l")); + assertEquals(5, exec("Set l = new HashSet(); l.add(5); return l.asList()[0]")); } public void testIterable_Each() { - assertEquals(1, - exec("List l = new ArrayList(); l.add(1); List l2 = new ArrayList(); l.each(l2::add); return l2.size()")); + assertEquals(1, exec("List l = new ArrayList(); l.add(1); List l2 = new ArrayList(); l.each(l2::add); return l2.size()")); } public void testIterable_EachWithIndex() { - assertEquals(0, - exec("List l = new ArrayList(); l.add(2); Map m = new HashMap(); l.eachWithIndex(m::put); return m.get(2)")); + assertEquals(0, exec("List l = new ArrayList(); l.add(2); Map m = new HashMap(); l.eachWithIndex(m::put); return m.get(2)")); } public void testIterable_Every() { @@ -102,121 +100,142 @@ public void testIterable_Every() { } public void testIterable_FindResults() { - assertEquals(1, - exec("List l = new ArrayList(); l.add(1); l.add(2); l.findResults(x -> x == 1 ? x : null).size()")); + assertEquals(1, exec("List l = new ArrayList(); l.add(1); l.add(2); l.findResults(x -> x == 1 ? x : null).size()")); } public void testIterable_GroupBy() { - assertEquals(2, - exec("List l = new ArrayList(); l.add(1); l.add(-1); l.groupBy(x -> x < 0 ? 'negative' : 'positive').size()")); + assertEquals(2, exec("List l = new ArrayList(); l.add(1); l.add(-1); l.groupBy(x -> x < 0 ? 'negative' : 'positive').size()")); } public void testIterable_Join() { - assertEquals("test,ing", - exec("List l = new ArrayList(); l.add('test'); l.add('ing'); l.join(',')")); - assertEquals(";empty;start;;test", - exec("List l = new ArrayList(); l.add(''); l.add('empty'); l.add('start'); l.add(''); l.add('test'); l.join(';')")); + assertEquals("test,ing", exec("List l = new ArrayList(); l.add('test'); l.add('ing'); l.join(',')")); + assertEquals( + ";empty;start;;test", + exec("List l = new ArrayList(); l.add(''); l.add('empty'); l.add('start'); l.add(''); l.add('test'); l.join(';')") + ); } public void testIterable_Sum() { assertEquals(3.0D, exec("def l = [1,2]; return l.sum()")); - assertEquals(5.0D, - exec("List l = new ArrayList(); l.add(1); l.add(2); l.sum(x -> x + 1)")); + assertEquals(5.0D, exec("List l = new ArrayList(); l.add(1); l.add(2); l.sum(x -> x + 1)")); } public void testCollection_Collect() { - assertEquals(Arrays.asList(2, 3), - exec("List l = new ArrayList(); l.add(1); l.add(2); l.collect(x -> x + 1)")); - assertEquals(asSet(2, 3), - exec("List l = new ArrayList(); l.add(1); l.add(2); l.collect(new HashSet(), x -> x + 1)")); + assertEquals(Arrays.asList(2, 3), exec("List l = new ArrayList(); l.add(1); l.add(2); l.collect(x -> x + 1)")); + assertEquals(asSet(2, 3), exec("List l = new ArrayList(); l.add(1); l.add(2); l.collect(new HashSet(), x -> x + 1)")); } public void testCollection_Find() { - assertEquals(2, - exec("List l = new ArrayList(); l.add(1); l.add(2); return l.find(x -> x == 2)")); + assertEquals(2, exec("List l = new ArrayList(); l.add(1); l.add(2); return l.find(x -> x == 2)")); } public void testCollection_FindAll() { - assertEquals(Arrays.asList(2), - exec("List l = new ArrayList(); l.add(1); l.add(2); return l.findAll(x -> x == 2)")); + assertEquals(Arrays.asList(2), exec("List l = new ArrayList(); l.add(1); l.add(2); return l.findAll(x -> x == 2)")); } public void testCollection_FindResult() { - assertEquals("found", - exec("List l = new ArrayList(); l.add(1); l.add(2); return l.findResult(x -> x > 1 ? 'found' : null)")); - assertEquals("notfound", - exec("List l = new ArrayList(); l.add(1); l.add(2); return l.findResult('notfound', x -> x > 10 ? 'found' : null)")); + assertEquals("found", exec("List l = new ArrayList(); l.add(1); l.add(2); return l.findResult(x -> x > 1 ? 'found' : null)")); + assertEquals( + "notfound", + exec("List l = new ArrayList(); l.add(1); l.add(2); return l.findResult('notfound', x -> x > 10 ? 'found' : null)") + ); } public void testCollection_Split() { - assertEquals(Arrays.asList(Arrays.asList(2), Arrays.asList(1)), - exec("List l = new ArrayList(); l.add(1); l.add(2); return l.split(x -> x == 2)")); + assertEquals( + Arrays.asList(Arrays.asList(2), Arrays.asList(1)), + exec("List l = new ArrayList(); l.add(1); l.add(2); return l.split(x -> x == 2)") + ); } public void testMap_Collect() { - assertEquals(Arrays.asList("one1", "two2"), - exec("Map m = new TreeMap(); m.one = 1; m.two = 2; m.collect((key,value) -> key + value)")); - assertEquals(asSet("one1", "two2"), - exec("Map m = new TreeMap(); m.one = 1; m.two = 2; m.collect(new HashSet(), (key,value) -> key + value)")); + assertEquals( + Arrays.asList("one1", "two2"), + exec("Map m = new TreeMap(); m.one = 1; m.two = 2; m.collect((key,value) -> key + value)") + ); + assertEquals( + asSet("one1", "two2"), + exec("Map m = new TreeMap(); m.one = 1; m.two = 2; m.collect(new HashSet(), (key,value) -> key + value)") + ); } public void testMap_Count() { - assertEquals(1, - exec("Map m = new TreeMap(); m.one = 1; m.two = 2; m.count((key,value) -> value == 2)")); + assertEquals(1, exec("Map m = new TreeMap(); m.one = 1; m.two = 2; m.count((key,value) -> value == 2)")); } public void testMap_Each() { - assertEquals(2, - exec("Map m = new TreeMap(); m.one = 1; m.two = 2; Map m2 = new TreeMap(); m.each(m2::put); return m2.size()")); + assertEquals(2, exec("Map m = new TreeMap(); m.one = 1; m.two = 2; Map m2 = new TreeMap(); m.each(m2::put); return m2.size()")); } public void testMap_Every() { - assertEquals(false, - exec("Map m = new TreeMap(); m.one = 1; m.two = 2; m.every((key,value) -> value == 2)")); + assertEquals(false, exec("Map m = new TreeMap(); m.one = 1; m.two = 2; m.every((key,value) -> value == 2)")); } public void testMap_Find() { - assertEquals("two", - exec("Map m = new TreeMap(); m.one = 1; m.two = 2; return m.find((key,value) -> value == 2).key")); + assertEquals("two", exec("Map m = new TreeMap(); m.one = 1; m.two = 2; return m.find((key,value) -> value == 2).key")); } public void testMap_FindAll() { - assertEquals(Collections.singletonMap("two", 2), - exec("Map m = new TreeMap(); m.one = 1; m.two = 2; return m.findAll((key,value) -> value == 2)")); + assertEquals( + Collections.singletonMap("two", 2), + exec("Map m = new TreeMap(); m.one = 1; m.two = 2; return m.findAll((key,value) -> value == 2)") + ); } public void testMap_FindResult() { - assertEquals("found", - exec("Map m = new TreeMap(); m.one = 1; m.two = 2; return m.findResult((key,value) -> value == 2 ? 'found' : null)")); - assertEquals("notfound", - exec("Map m = new TreeMap(); m.one = 1; m.two = 2; " + - "return m.findResult('notfound', (key,value) -> value == 10 ? 'found' : null)")); + assertEquals( + "found", + exec("Map m = new TreeMap(); m.one = 1; m.two = 2; return m.findResult((key,value) -> value == 2 ? 'found' : null)") + ); + assertEquals( + "notfound", + exec( + "Map m = new TreeMap(); m.one = 1; m.two = 2; " + + "return m.findResult('notfound', (key,value) -> value == 10 ? 'found' : null)" + ) + ); } public void testMap_FindResults() { - assertEquals(Arrays.asList("negative", "positive"), - exec("Map m = new TreeMap(); m.a = -1; m.b = 1; " + - "return m.findResults((key,value) -> value < 0 ? 'negative' : 'positive')")); + assertEquals( + Arrays.asList("negative", "positive"), + exec("Map m = new TreeMap(); m.a = -1; m.b = 1; " + "return m.findResults((key,value) -> value < 0 ? 'negative' : 'positive')") + ); } public void testMap_GroupBy() { - Map> expected = new HashMap<>(); + Map> expected = new HashMap<>(); expected.put("negative", Collections.singletonMap("a", -1)); expected.put("positive", Collections.singletonMap("b", 1)); - assertEquals(expected, - exec("Map m = new TreeMap(); m.a = -1; m.b = 1; " + - "return m.groupBy((key,value) -> value < 0 ? 'negative' : 'positive')")); + assertEquals( + expected, + exec("Map m = new TreeMap(); m.a = -1; m.b = 1; " + "return m.groupBy((key,value) -> value < 0 ? 'negative' : 'positive')") + ); } public void testFeatureTest() { - assertEquals(5, exec("org.elasticsearch.painless.FeatureTestObject ft = new org.elasticsearch.painless.FeatureTestObject();" + - " ft.setX(3); ft.setY(2); return ft.getTotal()")); - assertEquals(5, exec("def ft = new org.elasticsearch.painless.FeatureTestObject();" + - " ft.setX(3); ft.setY(2); return ft.getTotal()")); - assertEquals(8, exec("org.elasticsearch.painless.FeatureTestObject ft = new org.elasticsearch.painless.FeatureTestObject();" + - " ft.setX(3); ft.setY(2); return ft.addToTotal(3)")); - assertEquals(8, exec("def ft = new org.elasticsearch.painless.FeatureTestObject();" + - " ft.setX(3); ft.setY(2); return ft.addToTotal(3)")); + assertEquals( + 5, + exec( + "org.elasticsearch.painless.FeatureTestObject ft = new org.elasticsearch.painless.FeatureTestObject();" + + " ft.setX(3); ft.setY(2); return ft.getTotal()" + ) + ); + assertEquals( + 5, + exec("def ft = new org.elasticsearch.painless.FeatureTestObject();" + " ft.setX(3); ft.setY(2); return ft.getTotal()") + ); + assertEquals( + 8, + exec( + "org.elasticsearch.painless.FeatureTestObject ft = new org.elasticsearch.painless.FeatureTestObject();" + + " ft.setX(3); ft.setY(2); return ft.addToTotal(3)" + ) + ); + assertEquals( + 8, + exec("def ft = new org.elasticsearch.painless.FeatureTestObject();" + " ft.setX(3); ft.setY(2); return ft.addToTotal(3)") + ); } private static class SplitCase { @@ -229,10 +248,12 @@ private static class SplitCase { this.token = token; this.count = count; } + SplitCase(String input, String token) { this(input, token, -1); } } + public void testString_SplitOnToken() { SplitCase[] cases = new SplitCase[] { new SplitCase("", ""), @@ -247,22 +268,17 @@ public void testString_SplitOnToken() { new SplitCase("aaaaaaa", "a", 2), new SplitCase("1.1.1.1.111", "1"), new SplitCase("1.1.1.1.111", "."), - new SplitCase("1\n1.1.\r\n1\r\n111", "\r\n"), - }; + new SplitCase("1\n1.1.\r\n1\r\n111", "\r\n"), }; for (SplitCase split : cases) { assertArrayEquals( split.input.split(Pattern.quote(split.token), split.count), - (String[])exec("return \""+split.input+"\".splitOnToken(\""+split.token+"\", "+split.count+");") + (String[]) exec("return \"" + split.input + "\".splitOnToken(\"" + split.token + "\", " + split.count + ");") ); } } public String execDigest(String script) { - return scriptEngine.compile( - "digest_test", - script, - DigestTestScript.CONTEXT, Collections.emptyMap() - ).newInstance().execute(); + return scriptEngine.compile("digest_test", script, DigestTestScript.CONTEXT, Collections.emptyMap()).newInstance().execute(); } public void testSha1() { @@ -286,6 +302,6 @@ public void testAugmentedField() { assertEquals(Integer.MAX_VALUE, exec("org.elasticsearch.painless.FeatureTestObject.MAX_VALUE")); assertEquals("test_string", exec("Integer.STRINGS[0]")); assertEquals("test_value", exec("Map.STRINGS['test_key']")); - assertTrue((boolean)exec("Integer.STRINGS[0].substring(0, 4) == Map.STRINGS['test_key'].substring(0, 4)")); + assertTrue((boolean) exec("Integer.STRINGS[0].substring(0, 4) == Map.STRINGS['test_key'].substring(0, 4)")); } } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/BaseClassTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/BaseClassTests.java index 6813b40d61007..685080c8d90f6 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/BaseClassTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/BaseClassTests.java @@ -71,32 +71,44 @@ public Gets(String testString, int testInt, Map testMap) { } public static final String[] PARAMETERS = new String[] {}; + public abstract Object execute(); public String getTestString() { return testString; } + public int getTestInt() { return Math.abs(testInt); } + public Map getTestMap() { return testMap == null ? new HashMap<>() : testMap; } } + public void testGets() throws Exception { Map map = new HashMap<>(); map.put("s", 1); assertEquals(1, scriptEngine.compile("testGets0", "testInt", Gets.CONTEXT, emptyMap()).newInstance("s", -1, null).execute()); - assertEquals(Collections.emptyMap(), - scriptEngine.compile("testGets1", "testMap", Gets.CONTEXT, emptyMap()).newInstance("s", -1, null).execute()); - assertEquals(Collections.singletonMap("1", "1"), - scriptEngine.compile("testGets2", "testMap", Gets.CONTEXT, emptyMap()) - .newInstance("s", -1, Collections.singletonMap("1", "1")).execute()); + assertEquals( + Collections.emptyMap(), + scriptEngine.compile("testGets1", "testMap", Gets.CONTEXT, emptyMap()).newInstance("s", -1, null).execute() + ); + assertEquals( + Collections.singletonMap("1", "1"), + scriptEngine.compile("testGets2", "testMap", Gets.CONTEXT, emptyMap()) + .newInstance("s", -1, Collections.singletonMap("1", "1")) + .execute() + ); assertEquals("s", scriptEngine.compile("testGets3", "testString", Gets.CONTEXT, emptyMap()).newInstance("s", -1, null).execute()); - assertEquals(map, - scriptEngine.compile("testGets4", "testMap.put(testString, testInt); testMap", Gets.CONTEXT, emptyMap()) - .newInstance("s", -1, null).execute()); + assertEquals( + map, + scriptEngine.compile("testGets4", "testMap.put(testString, testInt); testMap", Gets.CONTEXT, emptyMap()) + .newInstance("s", -1, null) + .execute() + ); } public abstract static class NoArgs { @@ -107,17 +119,23 @@ public interface Factory { public static final ScriptContext CONTEXT = new ScriptContext<>("noargs", Factory.class); public static final String[] PARAMETERS = new String[] {}; + public abstract Object execute(); } + public void testNoArgs() throws Exception { assertEquals(1, scriptEngine.compile("testNoArgs0", "1", NoArgs.CONTEXT, emptyMap()).newInstance().execute()); assertEquals("foo", scriptEngine.compile("testNoArgs1", "'foo'", NoArgs.CONTEXT, emptyMap()).newInstance().execute()); - Exception e = expectScriptThrows(IllegalArgumentException.class, () -> - scriptEngine.compile("testNoArgs2", "doc", NoArgs.CONTEXT, emptyMap())); + Exception e = expectScriptThrows( + IllegalArgumentException.class, + () -> scriptEngine.compile("testNoArgs2", "doc", NoArgs.CONTEXT, emptyMap()) + ); assertEquals("cannot resolve symbol [doc]", e.getMessage()); - e = expectScriptThrows(IllegalArgumentException.class, () -> - scriptEngine.compile("testNoArgs3", "_score", NoArgs.CONTEXT, emptyMap())); + e = expectScriptThrows( + IllegalArgumentException.class, + () -> scriptEngine.compile("testNoArgs3", "_score", NoArgs.CONTEXT, emptyMap()) + ); assertEquals("cannot resolve symbol [_score]", e.getMessage()); String debug = Debugger.toString(NoArgs.class, "int i = 0", new CompilerSettings(), PainlessPlugin.BASE_WHITELISTS); @@ -132,9 +150,11 @@ public interface Factory { public static final ScriptContext CONTEXT = new ScriptContext<>("onearg", Factory.class); - public static final String[] PARAMETERS = new String[] {"arg"}; + public static final String[] PARAMETERS = new String[] { "arg" }; + public abstract Object execute(Object arg); } + public void testOneArg() throws Exception { Object rando = randomInt(); assertEquals(rando, scriptEngine.compile("testOneArg0", "arg", OneArg.CONTEXT, emptyMap()).newInstance().execute(rando)); @@ -149,14 +169,19 @@ public interface Factory { public static final ScriptContext CONTEXT = new ScriptContext<>("arrayarg", Factory.class); - public static final String[] PARAMETERS = new String[] {"arg"}; + public static final String[] PARAMETERS = new String[] { "arg" }; + public abstract Object execute(String[] arg); } + public void testArrayArg() throws Exception { String rando = randomAlphaOfLength(5); - assertEquals(rando, - scriptEngine.compile("testArrayArg0", "arg[0]", ArrayArg.CONTEXT, emptyMap()) - .newInstance().execute(new String[] {rando, "foo"})); + assertEquals( + rando, + scriptEngine.compile("testArrayArg0", "arg[0]", ArrayArg.CONTEXT, emptyMap()) + .newInstance() + .execute(new String[] { rando, "foo" }) + ); } public abstract static class PrimitiveArrayArg { @@ -166,14 +191,19 @@ public interface Factory { public static final ScriptContext CONTEXT = new ScriptContext<>("primitivearrayarg", Factory.class); - public static final String[] PARAMETERS = new String[] {"arg"}; + public static final String[] PARAMETERS = new String[] { "arg" }; + public abstract Object execute(int[] arg); } + public void testPrimitiveArrayArg() throws Exception { int rando = randomInt(); - assertEquals(rando, - scriptEngine.compile("PrimitiveArrayArg0", "arg[0]", PrimitiveArrayArg.CONTEXT, emptyMap()) - .newInstance().execute(new int[] {rando, 10})); + assertEquals( + rando, + scriptEngine.compile("PrimitiveArrayArg0", "arg[0]", PrimitiveArrayArg.CONTEXT, emptyMap()) + .newInstance() + .execute(new int[] { rando, 10 }) + ); } public abstract static class DefArrayArg { @@ -183,21 +213,32 @@ public interface Factory { public static final ScriptContext CONTEXT = new ScriptContext<>("defarrayarg", Factory.class); - public static final String[] PARAMETERS = new String[] {"arg"}; + public static final String[] PARAMETERS = new String[] { "arg" }; + public abstract Object execute(Object[] arg); } - public void testDefArrayArg()throws Exception { + + public void testDefArrayArg() throws Exception { Object rando = randomInt(); - assertEquals(rando, - scriptEngine.compile("testDefArray0", "arg[0]", DefArrayArg.CONTEXT, emptyMap()) - .newInstance().execute(new Object[] {rando, 10})); + assertEquals( + rando, + scriptEngine.compile("testDefArray0", "arg[0]", DefArrayArg.CONTEXT, emptyMap()) + .newInstance() + .execute(new Object[] { rando, 10 }) + ); rando = randomAlphaOfLength(5); - assertEquals(rando, - scriptEngine.compile("testDefArray1", "arg[0]", DefArrayArg.CONTEXT, emptyMap()) - .newInstance().execute(new Object[] {rando, 10})); - assertEquals(5, scriptEngine.compile( - "testDefArray2", "arg[0].length()", DefArrayArg.CONTEXT, emptyMap()) - .newInstance().execute(new Object[] {rando, 10})); + assertEquals( + rando, + scriptEngine.compile("testDefArray1", "arg[0]", DefArrayArg.CONTEXT, emptyMap()) + .newInstance() + .execute(new Object[] { rando, 10 }) + ); + assertEquals( + 5, + scriptEngine.compile("testDefArray2", "arg[0].length()", DefArrayArg.CONTEXT, emptyMap()) + .newInstance() + .execute(new Object[] { rando, 10 }) + ); } public abstract static class ManyArgs { @@ -207,19 +248,26 @@ public interface Factory { public static final ScriptContext CONTEXT = new ScriptContext<>("manyargs", Factory.class); - public static final String[] PARAMETERS = new String[] {"a", "b", "c", "d"}; + public static final String[] PARAMETERS = new String[] { "a", "b", "c", "d" }; + public abstract Object execute(int a, int b, int c, int d); + public abstract boolean needsA(); + public abstract boolean needsB(); + public abstract boolean needsC(); + public abstract boolean needsD(); } + public void testManyArgs() throws Exception { int rando = randomInt(); - assertEquals(rando, - scriptEngine.compile("testManyArgs0", "a", ManyArgs.CONTEXT, emptyMap()).newInstance().execute(rando, 0, 0, 0)); - assertEquals(10, - scriptEngine.compile("testManyArgs1", "a + b + c + d", ManyArgs.CONTEXT, emptyMap()).newInstance().execute(1, 2, 3, 4)); + assertEquals(rando, scriptEngine.compile("testManyArgs0", "a", ManyArgs.CONTEXT, emptyMap()).newInstance().execute(rando, 0, 0, 0)); + assertEquals( + 10, + scriptEngine.compile("testManyArgs1", "a + b + c + d", ManyArgs.CONTEXT, emptyMap()).newInstance().execute(1, 2, 3, 4) + ); // While we're here we can verify that painless correctly finds used variables ManyArgs script = scriptEngine.compile("testManyArgs2", "a", ManyArgs.CONTEXT, emptyMap()).newInstance(); @@ -246,13 +294,18 @@ public interface Factory { public static final ScriptContext CONTEXT = new ScriptContext<>("varargs", Factory.class); - public static final String[] PARAMETERS = new String[] {"arg"}; + public static final String[] PARAMETERS = new String[] { "arg" }; + public abstract Object execute(String... arg); } + public void testVarArgs() throws Exception { - assertEquals("foo bar baz", - scriptEngine.compile("testVarArgs0", "String.join(' ', Arrays.asList(arg))", VarArgs.CONTEXT, emptyMap()) - .newInstance().execute("foo", "bar", "baz")); + assertEquals( + "foo bar baz", + scriptEngine.compile("testVarArgs0", "String.join(' ', Arrays.asList(arg))", VarArgs.CONTEXT, emptyMap()) + .newInstance() + .execute("foo", "bar", "baz") + ); } public abstract static class DefaultMethods { @@ -262,31 +315,47 @@ public interface Factory { public static final ScriptContext CONTEXT = new ScriptContext<>("defaultmethods", Factory.class); - public static final String[] PARAMETERS = new String[] {"a", "b", "c", "d"}; + public static final String[] PARAMETERS = new String[] { "a", "b", "c", "d" }; + public abstract Object execute(int a, int b, int c, int d); + public Object executeWithOne() { return execute(1, 1, 1, 1); } + public Object executeWithASingleOne(int a, int b, int c) { return execute(a, b, c, 1); } } + public void testDefaultMethods() throws Exception { int rando = randomInt(); - assertEquals(rando, - scriptEngine.compile("testDefaultMethods0", "a", DefaultMethods.CONTEXT, emptyMap()).newInstance().execute(rando, 0, 0, 0)); - assertEquals(rando, - scriptEngine.compile("testDefaultMethods1", "a", DefaultMethods.CONTEXT, emptyMap()) - .newInstance().executeWithASingleOne(rando, 0, 0)); - assertEquals(10, - scriptEngine.compile("testDefaultMethods2", "a + b + c + d", DefaultMethods.CONTEXT, emptyMap()) - .newInstance().execute(1, 2, 3, 4)); - assertEquals(4, - scriptEngine.compile("testDefaultMethods3", "a + b + c + d", DefaultMethods.CONTEXT, emptyMap()) - .newInstance().executeWithOne()); - assertEquals(7, - scriptEngine.compile("testDefaultMethods4", "a + b + c + d", DefaultMethods.CONTEXT, emptyMap()) - .newInstance().executeWithASingleOne(1, 2, 3)); + assertEquals( + rando, + scriptEngine.compile("testDefaultMethods0", "a", DefaultMethods.CONTEXT, emptyMap()).newInstance().execute(rando, 0, 0, 0) + ); + assertEquals( + rando, + scriptEngine.compile("testDefaultMethods1", "a", DefaultMethods.CONTEXT, emptyMap()) + .newInstance() + .executeWithASingleOne(rando, 0, 0) + ); + assertEquals( + 10, + scriptEngine.compile("testDefaultMethods2", "a + b + c + d", DefaultMethods.CONTEXT, emptyMap()) + .newInstance() + .execute(1, 2, 3, 4) + ); + assertEquals( + 4, + scriptEngine.compile("testDefaultMethods3", "a + b + c + d", DefaultMethods.CONTEXT, emptyMap()).newInstance().executeWithOne() + ); + assertEquals( + 7, + scriptEngine.compile("testDefaultMethods4", "a + b + c + d", DefaultMethods.CONTEXT, emptyMap()) + .newInstance() + .executeWithASingleOne(1, 2, 3) + ); } public abstract static class ReturnsVoid { @@ -296,9 +365,11 @@ public interface Factory { public static final ScriptContext CONTEXT = new ScriptContext<>("returnsvoid", Factory.class); - public static final String[] PARAMETERS = new String[] {"map"}; + public static final String[] PARAMETERS = new String[] { "map" }; + public abstract void execute(Map map); } + public void testReturnsVoid() throws Exception { Map map = new HashMap<>(); scriptEngine.compile("testReturnsVoid0", "map.a = 'foo'", ReturnsVoid.CONTEXT, emptyMap()).newInstance().execute(map); @@ -321,62 +392,98 @@ public interface Factory { public static final ScriptContext CONTEXT = new ScriptContext<>("returnsprimitiveboolean", Factory.class); public static final String[] PARAMETERS = new String[] {}; + public abstract boolean execute(); } + public void testReturnsPrimitiveBoolean() throws Exception { assertTrue( - scriptEngine.compile("testReturnsPrimitiveBoolean0", "true", ReturnsPrimitiveBoolean.CONTEXT, emptyMap()) - .newInstance().execute()); + scriptEngine.compile("testReturnsPrimitiveBoolean0", "true", ReturnsPrimitiveBoolean.CONTEXT, emptyMap()) + .newInstance() + .execute() + ); assertFalse( - scriptEngine.compile("testReturnsPrimitiveBoolean1", "false", ReturnsPrimitiveBoolean.CONTEXT, emptyMap()) - .newInstance().execute()); + scriptEngine.compile("testReturnsPrimitiveBoolean1", "false", ReturnsPrimitiveBoolean.CONTEXT, emptyMap()) + .newInstance() + .execute() + ); assertTrue( - scriptEngine.compile("testReturnsPrimitiveBoolean2", "Boolean.TRUE", ReturnsPrimitiveBoolean.CONTEXT, emptyMap()) - .newInstance().execute()); + scriptEngine.compile("testReturnsPrimitiveBoolean2", "Boolean.TRUE", ReturnsPrimitiveBoolean.CONTEXT, emptyMap()) + .newInstance() + .execute() + ); assertFalse( - scriptEngine.compile("testReturnsPrimitiveBoolean3", "Boolean.FALSE", ReturnsPrimitiveBoolean.CONTEXT, emptyMap()) - .newInstance().execute()); + scriptEngine.compile("testReturnsPrimitiveBoolean3", "Boolean.FALSE", ReturnsPrimitiveBoolean.CONTEXT, emptyMap()) + .newInstance() + .execute() + ); assertTrue( - scriptEngine.compile("testReturnsPrimitiveBoolean4", "def i = true; i", ReturnsPrimitiveBoolean.CONTEXT, emptyMap()) - .newInstance().execute()); + scriptEngine.compile("testReturnsPrimitiveBoolean4", "def i = true; i", ReturnsPrimitiveBoolean.CONTEXT, emptyMap()) + .newInstance() + .execute() + ); assertTrue( - scriptEngine.compile("testReturnsPrimitiveBoolean5", "def i = Boolean.TRUE; i", ReturnsPrimitiveBoolean.CONTEXT, emptyMap()) - .newInstance().execute()); + scriptEngine.compile("testReturnsPrimitiveBoolean5", "def i = Boolean.TRUE; i", ReturnsPrimitiveBoolean.CONTEXT, emptyMap()) + .newInstance() + .execute() + ); assertTrue( - scriptEngine.compile("testReturnsPrimitiveBoolean6", "true || false", ReturnsPrimitiveBoolean.CONTEXT, emptyMap()) - .newInstance().execute()); + scriptEngine.compile("testReturnsPrimitiveBoolean6", "true || false", ReturnsPrimitiveBoolean.CONTEXT, emptyMap()) + .newInstance() + .execute() + ); String debug = Debugger.toString(ReturnsPrimitiveBoolean.class, "false", new CompilerSettings(), PainlessPlugin.BASE_WHITELISTS); assertThat(debug, containsString("ICONST_0")); // The important thing here is that we have the bytecode for returning an integer instead of an object. booleans are integers. assertThat(debug, containsString("IRETURN")); - Exception e = expectScriptThrows(ClassCastException.class, () -> - scriptEngine.compile("testReturnsPrimitiveBoolean7", "1L",ReturnsPrimitiveBoolean.CONTEXT, emptyMap()) - .newInstance().execute()); + Exception e = expectScriptThrows( + ClassCastException.class, + () -> scriptEngine.compile("testReturnsPrimitiveBoolean7", "1L", ReturnsPrimitiveBoolean.CONTEXT, emptyMap()) + .newInstance() + .execute() + ); assertEquals("Cannot cast from [long] to [boolean].", e.getMessage()); - e = expectScriptThrows(ClassCastException.class, () -> - scriptEngine.compile("testReturnsPrimitiveBoolean8", "1.1f", ReturnsPrimitiveBoolean.CONTEXT, emptyMap()) - .newInstance().execute()); + e = expectScriptThrows( + ClassCastException.class, + () -> scriptEngine.compile("testReturnsPrimitiveBoolean8", "1.1f", ReturnsPrimitiveBoolean.CONTEXT, emptyMap()) + .newInstance() + .execute() + ); assertEquals("Cannot cast from [float] to [boolean].", e.getMessage()); - e = expectScriptThrows(ClassCastException.class, () -> - scriptEngine.compile("testReturnsPrimitiveBoolean9", "1.1d", ReturnsPrimitiveBoolean.CONTEXT, emptyMap()) - .newInstance().execute()); + e = expectScriptThrows( + ClassCastException.class, + () -> scriptEngine.compile("testReturnsPrimitiveBoolean9", "1.1d", ReturnsPrimitiveBoolean.CONTEXT, emptyMap()) + .newInstance() + .execute() + ); assertEquals("Cannot cast from [double] to [boolean].", e.getMessage()); - expectScriptThrows(ClassCastException.class, () -> - scriptEngine.compile("testReturnsPrimitiveBoolean10", "def i = 1L; i", ReturnsPrimitiveBoolean.CONTEXT, emptyMap()) - .newInstance().execute()); - expectScriptThrows(ClassCastException.class, () -> - scriptEngine.compile("testReturnsPrimitiveBoolean11", "def i = 1.1f; i", ReturnsPrimitiveBoolean.CONTEXT, emptyMap()) - .newInstance().execute()); - expectScriptThrows(ClassCastException.class, () -> - scriptEngine.compile("testReturnsPrimitiveBoolean12", "def i = 1.1d; i", ReturnsPrimitiveBoolean.CONTEXT, emptyMap()) - .newInstance().execute()); + expectScriptThrows( + ClassCastException.class, + () -> scriptEngine.compile("testReturnsPrimitiveBoolean10", "def i = 1L; i", ReturnsPrimitiveBoolean.CONTEXT, emptyMap()) + .newInstance() + .execute() + ); + expectScriptThrows( + ClassCastException.class, + () -> scriptEngine.compile("testReturnsPrimitiveBoolean11", "def i = 1.1f; i", ReturnsPrimitiveBoolean.CONTEXT, emptyMap()) + .newInstance() + .execute() + ); + expectScriptThrows( + ClassCastException.class, + () -> scriptEngine.compile("testReturnsPrimitiveBoolean12", "def i = 1.1d; i", ReturnsPrimitiveBoolean.CONTEXT, emptyMap()) + .newInstance() + .execute() + ); assertFalse( - scriptEngine.compile("testReturnsPrimitiveBoolean13", "int i = 0", ReturnsPrimitiveBoolean.CONTEXT, emptyMap()) - .newInstance().execute()); + scriptEngine.compile("testReturnsPrimitiveBoolean13", "int i = 0", ReturnsPrimitiveBoolean.CONTEXT, emptyMap()) + .newInstance() + .execute() + ); } public abstract static class ReturnsPrimitiveInt { @@ -387,60 +494,95 @@ public interface Factory { public static final ScriptContext CONTEXT = new ScriptContext<>("returnsprimitiveint", Factory.class); public static final String[] PARAMETERS = new String[] {}; + public abstract int execute(); } + public void testReturnsPrimitiveInt() throws Exception { - assertEquals(1, - scriptEngine.compile("testReturnsPrimitiveInt0", "1", ReturnsPrimitiveInt.CONTEXT, emptyMap()) - .newInstance().execute()); - assertEquals(1, - scriptEngine.compile("testReturnsPrimitiveInt1", "(int) 1L", ReturnsPrimitiveInt.CONTEXT, emptyMap()) - .newInstance().execute()); - assertEquals(1, scriptEngine.compile("testReturnsPrimitiveInt2", "(int) 1.1d", ReturnsPrimitiveInt.CONTEXT, emptyMap()) - .newInstance().execute()); - assertEquals(1, - scriptEngine.compile("testReturnsPrimitiveInt3", "(int) 1.1f", ReturnsPrimitiveInt.CONTEXT, emptyMap()) - .newInstance().execute()); - assertEquals(1, - scriptEngine.compile("testReturnsPrimitiveInt4", "Integer.valueOf(1)", ReturnsPrimitiveInt.CONTEXT, emptyMap()) - .newInstance().execute()); - - assertEquals(1, - scriptEngine.compile("testReturnsPrimitiveInt5", "def i = 1; i", ReturnsPrimitiveInt.CONTEXT, emptyMap()) - .newInstance().execute()); - assertEquals(1, - scriptEngine.compile("testReturnsPrimitiveInt6", "def i = Integer.valueOf(1); i", ReturnsPrimitiveInt.CONTEXT, emptyMap()) - .newInstance().execute()); - - assertEquals(2, - scriptEngine.compile("testReturnsPrimitiveInt7", "1 + 1", ReturnsPrimitiveInt.CONTEXT, emptyMap()).newInstance().execute()); + assertEquals( + 1, + scriptEngine.compile("testReturnsPrimitiveInt0", "1", ReturnsPrimitiveInt.CONTEXT, emptyMap()).newInstance().execute() + ); + assertEquals( + 1, + scriptEngine.compile("testReturnsPrimitiveInt1", "(int) 1L", ReturnsPrimitiveInt.CONTEXT, emptyMap()).newInstance().execute() + ); + assertEquals( + 1, + scriptEngine.compile("testReturnsPrimitiveInt2", "(int) 1.1d", ReturnsPrimitiveInt.CONTEXT, emptyMap()).newInstance().execute() + ); + assertEquals( + 1, + scriptEngine.compile("testReturnsPrimitiveInt3", "(int) 1.1f", ReturnsPrimitiveInt.CONTEXT, emptyMap()).newInstance().execute() + ); + assertEquals( + 1, + scriptEngine.compile("testReturnsPrimitiveInt4", "Integer.valueOf(1)", ReturnsPrimitiveInt.CONTEXT, emptyMap()) + .newInstance() + .execute() + ); + + assertEquals( + 1, + scriptEngine.compile("testReturnsPrimitiveInt5", "def i = 1; i", ReturnsPrimitiveInt.CONTEXT, emptyMap()) + .newInstance() + .execute() + ); + assertEquals( + 1, + scriptEngine.compile("testReturnsPrimitiveInt6", "def i = Integer.valueOf(1); i", ReturnsPrimitiveInt.CONTEXT, emptyMap()) + .newInstance() + .execute() + ); + + assertEquals( + 2, + scriptEngine.compile("testReturnsPrimitiveInt7", "1 + 1", ReturnsPrimitiveInt.CONTEXT, emptyMap()).newInstance().execute() + ); String debug = Debugger.toString(ReturnsPrimitiveInt.class, "1", new CompilerSettings(), PainlessPlugin.BASE_WHITELISTS); assertThat(debug, containsString("ICONST_1")); // The important thing here is that we have the bytecode for returning an integer instead of an object assertThat(debug, containsString("IRETURN")); - Exception e = expectScriptThrows(ClassCastException.class, () -> - scriptEngine.compile("testReturnsPrimitiveInt8", "1L", ReturnsPrimitiveInt.CONTEXT, emptyMap()).newInstance().execute()); + Exception e = expectScriptThrows( + ClassCastException.class, + () -> scriptEngine.compile("testReturnsPrimitiveInt8", "1L", ReturnsPrimitiveInt.CONTEXT, emptyMap()).newInstance().execute() + ); assertEquals("Cannot cast from [long] to [int].", e.getMessage()); - e = expectScriptThrows(ClassCastException.class, () -> - scriptEngine.compile("testReturnsPrimitiveInt9", "1.1f", ReturnsPrimitiveInt.CONTEXT, emptyMap()).newInstance().execute()); + e = expectScriptThrows( + ClassCastException.class, + () -> scriptEngine.compile("testReturnsPrimitiveInt9", "1.1f", ReturnsPrimitiveInt.CONTEXT, emptyMap()).newInstance().execute() + ); assertEquals("Cannot cast from [float] to [int].", e.getMessage()); - e = expectScriptThrows(ClassCastException.class, () -> - scriptEngine.compile("testReturnsPrimitiveInt10", "1.1d", ReturnsPrimitiveInt.CONTEXT, emptyMap()).newInstance().execute()); + e = expectScriptThrows( + ClassCastException.class, + () -> scriptEngine.compile("testReturnsPrimitiveInt10", "1.1d", ReturnsPrimitiveInt.CONTEXT, emptyMap()).newInstance().execute() + ); assertEquals("Cannot cast from [double] to [int].", e.getMessage()); - expectScriptThrows(ClassCastException.class, () -> - scriptEngine.compile("testReturnsPrimitiveInt11", "def i = 1L; i", ReturnsPrimitiveInt.CONTEXT, emptyMap()) - .newInstance().execute()); - expectScriptThrows(ClassCastException.class, () -> - scriptEngine.compile("testReturnsPrimitiveInt12", "def i = 1.1f; i", ReturnsPrimitiveInt.CONTEXT, emptyMap()) - .newInstance().execute()); - expectScriptThrows(ClassCastException.class, () -> - scriptEngine.compile("testReturnsPrimitiveInt13", "def i = 1.1d; i", ReturnsPrimitiveInt.CONTEXT, emptyMap()) - .newInstance().execute()); - - assertEquals(0, scriptEngine.compile("testReturnsPrimitiveInt14", "int i = 0", ReturnsPrimitiveInt.CONTEXT, emptyMap()) - .newInstance().execute()); + expectScriptThrows( + ClassCastException.class, + () -> scriptEngine.compile("testReturnsPrimitiveInt11", "def i = 1L; i", ReturnsPrimitiveInt.CONTEXT, emptyMap()) + .newInstance() + .execute() + ); + expectScriptThrows( + ClassCastException.class, + () -> scriptEngine.compile("testReturnsPrimitiveInt12", "def i = 1.1f; i", ReturnsPrimitiveInt.CONTEXT, emptyMap()) + .newInstance() + .execute() + ); + expectScriptThrows( + ClassCastException.class, + () -> scriptEngine.compile("testReturnsPrimitiveInt13", "def i = 1.1d; i", ReturnsPrimitiveInt.CONTEXT, emptyMap()) + .newInstance() + .execute() + ); + + assertEquals( + 0, + scriptEngine.compile("testReturnsPrimitiveInt14", "int i = 0", ReturnsPrimitiveInt.CONTEXT, emptyMap()).newInstance().execute() + ); } public abstract static class ReturnsPrimitiveFloat { @@ -451,107 +593,199 @@ public interface Factory { public static final ScriptContext CONTEXT = new ScriptContext<>("returnsprimitivefloat", Factory.class); public static final String[] PARAMETERS = new String[] {}; + public abstract float execute(); } + public void testReturnsPrimitiveFloat() throws Exception { - assertEquals(1.1f, - scriptEngine.compile("testReturnsPrimitiveFloat0", "1.1f", ReturnsPrimitiveFloat.CONTEXT, emptyMap()) - .newInstance().execute(), 0); - assertEquals(1.1f, - scriptEngine.compile("testReturnsPrimitiveFloat1", "(float) 1.1d", ReturnsPrimitiveFloat.CONTEXT, emptyMap()) - .newInstance().execute(), 0); - assertEquals(1.1f, - scriptEngine.compile("testReturnsPrimitiveFloat2", "def d = 1.1f; d", ReturnsPrimitiveFloat.CONTEXT, emptyMap()) - .newInstance().execute(), 0); - assertEquals(1.1f, scriptEngine.compile( - "testReturnsPrimitiveFloat3", "def d = Float.valueOf(1.1f); d", ReturnsPrimitiveFloat.CONTEXT, emptyMap()) - .newInstance().execute(), 0); - - assertEquals(1.1f + 6.7f, - scriptEngine.compile("testReturnsPrimitiveFloat4", "1.1f + 6.7f", ReturnsPrimitiveFloat.CONTEXT, emptyMap()) - .newInstance().execute(), 0); - - Exception e = expectScriptThrows(ClassCastException.class, () -> - scriptEngine.compile("testReturnsPrimitiveFloat5", "1.1d", ReturnsPrimitiveFloat.CONTEXT, emptyMap()) - .newInstance().execute()); + assertEquals( + 1.1f, + scriptEngine.compile("testReturnsPrimitiveFloat0", "1.1f", ReturnsPrimitiveFloat.CONTEXT, emptyMap()).newInstance().execute(), + 0 + ); + assertEquals( + 1.1f, + scriptEngine.compile("testReturnsPrimitiveFloat1", "(float) 1.1d", ReturnsPrimitiveFloat.CONTEXT, emptyMap()) + .newInstance() + .execute(), + 0 + ); + assertEquals( + 1.1f, + scriptEngine.compile("testReturnsPrimitiveFloat2", "def d = 1.1f; d", ReturnsPrimitiveFloat.CONTEXT, emptyMap()) + .newInstance() + .execute(), + 0 + ); + assertEquals( + 1.1f, + scriptEngine.compile("testReturnsPrimitiveFloat3", "def d = Float.valueOf(1.1f); d", ReturnsPrimitiveFloat.CONTEXT, emptyMap()) + .newInstance() + .execute(), + 0 + ); + + assertEquals( + 1.1f + 6.7f, + scriptEngine.compile("testReturnsPrimitiveFloat4", "1.1f + 6.7f", ReturnsPrimitiveFloat.CONTEXT, emptyMap()) + .newInstance() + .execute(), + 0 + ); + + Exception e = expectScriptThrows( + ClassCastException.class, + () -> scriptEngine.compile("testReturnsPrimitiveFloat5", "1.1d", ReturnsPrimitiveFloat.CONTEXT, emptyMap()) + .newInstance() + .execute() + ); assertEquals("Cannot cast from [double] to [float].", e.getMessage()); - e = expectScriptThrows(ClassCastException.class, () -> - scriptEngine.compile("testReturnsPrimitiveFloat6", "def d = 1.1d; d", ReturnsPrimitiveFloat.CONTEXT, emptyMap()) - .newInstance().execute()); - e = expectScriptThrows(ClassCastException.class, () -> scriptEngine.compile( - "testReturnsPrimitiveFloat7", "def d = Double.valueOf(1.1); d", ReturnsPrimitiveFloat.CONTEXT, emptyMap()) - .newInstance().execute()); + e = expectScriptThrows( + ClassCastException.class, + () -> scriptEngine.compile("testReturnsPrimitiveFloat6", "def d = 1.1d; d", ReturnsPrimitiveFloat.CONTEXT, emptyMap()) + .newInstance() + .execute() + ); + e = expectScriptThrows( + ClassCastException.class, + () -> scriptEngine.compile( + "testReturnsPrimitiveFloat7", + "def d = Double.valueOf(1.1); d", + ReturnsPrimitiveFloat.CONTEXT, + emptyMap() + ).newInstance().execute() + ); String debug = Debugger.toString(ReturnsPrimitiveFloat.class, "1f", new CompilerSettings(), PainlessPlugin.BASE_WHITELISTS); assertThat(debug, containsString("FCONST_1")); // The important thing here is that we have the bytecode for returning a float instead of an object assertThat(debug, containsString("FRETURN")); - assertEquals(0.0f, - scriptEngine.compile("testReturnsPrimitiveFloat8", "int i = 0", ReturnsPrimitiveFloat.CONTEXT, emptyMap()) - .newInstance().execute(), 0); + assertEquals( + 0.0f, + scriptEngine.compile("testReturnsPrimitiveFloat8", "int i = 0", ReturnsPrimitiveFloat.CONTEXT, emptyMap()) + .newInstance() + .execute(), + 0 + ); } - public abstract static class ReturnsPrimitiveDouble { - public interface Factory { - ReturnsPrimitiveDouble newInstance(); - } + public abstract static class ReturnsPrimitiveDouble { + public interface Factory { + ReturnsPrimitiveDouble newInstance(); + } - public static final ScriptContext CONTEXT = new ScriptContext<>("returnsprimitivedouble", Factory.class); + public static final ScriptContext CONTEXT = new ScriptContext<>("returnsprimitivedouble", Factory.class); public static final String[] PARAMETERS = new String[] {}; + public abstract double execute(); } + public void testReturnsPrimitiveDouble() throws Exception { - assertEquals(1.0, - scriptEngine.compile("testReturnsPrimitiveDouble0", "1", ReturnsPrimitiveDouble.CONTEXT, emptyMap()) - .newInstance().execute(), 0); - assertEquals(1.0, - scriptEngine.compile("testReturnsPrimitiveDouble1", "1L", ReturnsPrimitiveDouble.CONTEXT, emptyMap()) - .newInstance().execute(), 0); - assertEquals(1.1, - scriptEngine.compile("testReturnsPrimitiveDouble2", "1.1d", ReturnsPrimitiveDouble.CONTEXT, emptyMap()) - .newInstance().execute(), 0); - assertEquals((double) 1.1f, - scriptEngine.compile("testReturnsPrimitiveDouble3", "1.1f", ReturnsPrimitiveDouble.CONTEXT, emptyMap()) - .newInstance().execute(), 0); - assertEquals(1.1, scriptEngine.compile( - "testReturnsPrimitiveDouble4", "Double.valueOf(1.1)", ReturnsPrimitiveDouble.CONTEXT, emptyMap()) - .newInstance().execute(), 0); - assertEquals((double) 1.1f, scriptEngine.compile( - "testReturnsPrimitiveDouble5", "Float.valueOf(1.1f)", ReturnsPrimitiveDouble.CONTEXT, emptyMap()) - .newInstance().execute(), 0); - - assertEquals(1.0, - scriptEngine.compile("testReturnsPrimitiveDouble6", "def d = 1; d", ReturnsPrimitiveDouble.CONTEXT, emptyMap()) - .newInstance().execute(), 0); - assertEquals(1.0, - scriptEngine.compile("testReturnsPrimitiveDouble7", "def d = 1L; d", ReturnsPrimitiveDouble.CONTEXT, emptyMap()) - .newInstance().execute(), 0); - assertEquals(1.1, - scriptEngine.compile("testReturnsPrimitiveDouble8", "def d = 1.1d; d", ReturnsPrimitiveDouble.CONTEXT, emptyMap()). - newInstance().execute(), 0); - assertEquals((double) 1.1f, - scriptEngine.compile("testReturnsPrimitiveDouble9", "def d = 1.1f; d", ReturnsPrimitiveDouble.CONTEXT, emptyMap()) - .newInstance().execute(), 0); - assertEquals(1.1, scriptEngine.compile( - "testReturnsPrimitiveDouble10", "def d = Double.valueOf(1.1); d", ReturnsPrimitiveDouble.CONTEXT, emptyMap()) - .newInstance().execute(), 0); - assertEquals((double) 1.1f, scriptEngine.compile( - "testReturnsPrimitiveDouble11", "def d = Float.valueOf(1.1f); d", ReturnsPrimitiveDouble.CONTEXT, emptyMap()) - .newInstance().execute(), 0); - - assertEquals(1.1 + 6.7, - scriptEngine.compile("testReturnsPrimitiveDouble12", "1.1 + 6.7", ReturnsPrimitiveDouble.CONTEXT, emptyMap()) - .newInstance().execute(), 0); + assertEquals( + 1.0, + scriptEngine.compile("testReturnsPrimitiveDouble0", "1", ReturnsPrimitiveDouble.CONTEXT, emptyMap()).newInstance().execute(), + 0 + ); + assertEquals( + 1.0, + scriptEngine.compile("testReturnsPrimitiveDouble1", "1L", ReturnsPrimitiveDouble.CONTEXT, emptyMap()).newInstance().execute(), + 0 + ); + assertEquals( + 1.1, + scriptEngine.compile("testReturnsPrimitiveDouble2", "1.1d", ReturnsPrimitiveDouble.CONTEXT, emptyMap()).newInstance().execute(), + 0 + ); + assertEquals( + (double) 1.1f, + scriptEngine.compile("testReturnsPrimitiveDouble3", "1.1f", ReturnsPrimitiveDouble.CONTEXT, emptyMap()).newInstance().execute(), + 0 + ); + assertEquals( + 1.1, + scriptEngine.compile("testReturnsPrimitiveDouble4", "Double.valueOf(1.1)", ReturnsPrimitiveDouble.CONTEXT, emptyMap()) + .newInstance() + .execute(), + 0 + ); + assertEquals( + (double) 1.1f, + scriptEngine.compile("testReturnsPrimitiveDouble5", "Float.valueOf(1.1f)", ReturnsPrimitiveDouble.CONTEXT, emptyMap()) + .newInstance() + .execute(), + 0 + ); + + assertEquals( + 1.0, + scriptEngine.compile("testReturnsPrimitiveDouble6", "def d = 1; d", ReturnsPrimitiveDouble.CONTEXT, emptyMap()) + .newInstance() + .execute(), + 0 + ); + assertEquals( + 1.0, + scriptEngine.compile("testReturnsPrimitiveDouble7", "def d = 1L; d", ReturnsPrimitiveDouble.CONTEXT, emptyMap()) + .newInstance() + .execute(), + 0 + ); + assertEquals( + 1.1, + scriptEngine.compile("testReturnsPrimitiveDouble8", "def d = 1.1d; d", ReturnsPrimitiveDouble.CONTEXT, emptyMap()) + .newInstance() + .execute(), + 0 + ); + assertEquals( + (double) 1.1f, + scriptEngine.compile("testReturnsPrimitiveDouble9", "def d = 1.1f; d", ReturnsPrimitiveDouble.CONTEXT, emptyMap()) + .newInstance() + .execute(), + 0 + ); + assertEquals( + 1.1, + scriptEngine.compile( + "testReturnsPrimitiveDouble10", + "def d = Double.valueOf(1.1); d", + ReturnsPrimitiveDouble.CONTEXT, + emptyMap() + ).newInstance().execute(), + 0 + ); + assertEquals( + (double) 1.1f, + scriptEngine.compile( + "testReturnsPrimitiveDouble11", + "def d = Float.valueOf(1.1f); d", + ReturnsPrimitiveDouble.CONTEXT, + emptyMap() + ).newInstance().execute(), + 0 + ); + + assertEquals( + 1.1 + 6.7, + scriptEngine.compile("testReturnsPrimitiveDouble12", "1.1 + 6.7", ReturnsPrimitiveDouble.CONTEXT, emptyMap()) + .newInstance() + .execute(), + 0 + ); String debug = Debugger.toString(ReturnsPrimitiveDouble.class, "1", new CompilerSettings(), PainlessPlugin.BASE_WHITELISTS); // The important thing here is that we have the bytecode for returning a double instead of an object assertThat(debug, containsString("DRETURN")); - assertEquals(0.0, - scriptEngine.compile("testReturnsPrimitiveDouble13", "int i = 0", ReturnsPrimitiveDouble.CONTEXT, emptyMap()) - .newInstance().execute(), 0); + assertEquals( + 0.0, + scriptEngine.compile("testReturnsPrimitiveDouble13", "int i = 0", ReturnsPrimitiveDouble.CONTEXT, emptyMap()) + .newInstance() + .execute(), + 0 + ); } public abstract static class NoArgsConstant { @@ -563,12 +797,22 @@ public interface Factory { public abstract Object execute(String foo); } + public void testNoArgsConstant() { - Exception e = expectScriptThrows(IllegalArgumentException.class, false, () -> - scriptEngine.compile("testNoArgsConstant0", "1", NoArgsConstant.CONTEXT, emptyMap()).newInstance().execute("constant")); - assertThat(e.getMessage(), startsWith( + Exception e = expectScriptThrows( + IllegalArgumentException.class, + false, + () -> scriptEngine.compile("testNoArgsConstant0", "1", NoArgsConstant.CONTEXT, emptyMap()).newInstance().execute("constant") + ); + assertThat( + e.getMessage(), + startsWith( "Painless needs a constant [String[] PARAMETERS] on all interfaces it implements with the " - + "names of the method arguments but [" + NoArgsConstant.class.getName() + "] doesn't have one.")); + + "names of the method arguments but [" + + NoArgsConstant.class.getName() + + "] doesn't have one." + ) + ); } public abstract static class WrongArgsConstant { @@ -578,15 +822,26 @@ public interface Factory { public static final ScriptContext CONTEXT = new ScriptContext<>("wrongargscontext", Factory.class); - boolean[] PARAMETERS = new boolean[] {false}; + boolean[] PARAMETERS = new boolean[] { false }; + public abstract Object execute(String foo); } + public void testWrongArgsConstant() { - Exception e = expectScriptThrows(IllegalArgumentException.class, false, () -> - scriptEngine.compile("testWrongArgsConstant0", "1", WrongArgsConstant.CONTEXT, emptyMap())); - assertThat(e.getMessage(), startsWith( + Exception e = expectScriptThrows( + IllegalArgumentException.class, + false, + () -> scriptEngine.compile("testWrongArgsConstant0", "1", WrongArgsConstant.CONTEXT, emptyMap()) + ); + assertThat( + e.getMessage(), + startsWith( "Painless needs a constant [String[] PARAMETERS] on all interfaces it implements with the " - + "names of the method arguments but [" + WrongArgsConstant.class.getName() + "] doesn't have one.")); + + "names of the method arguments but [" + + WrongArgsConstant.class.getName() + + "] doesn't have one." + ) + ); } public abstract static class WrongLengthOfArgConstant { @@ -596,14 +851,27 @@ public interface Factory { public static final ScriptContext CONTEXT = new ScriptContext<>("wronglengthofargcontext", Factory.class); - public static final String[] PARAMETERS = new String[] {"foo", "bar"}; + public static final String[] PARAMETERS = new String[] { "foo", "bar" }; + public abstract Object execute(String foo); } + public void testWrongLengthOfArgConstant() { - Exception e = expectScriptThrows(IllegalArgumentException.class, false, () -> - scriptEngine.compile("testWrongLengthOfArgConstant", "1", WrongLengthOfArgConstant.CONTEXT, emptyMap())); - assertThat(e.getMessage(), startsWith("[" + WrongLengthOfArgConstant.class.getName() + "#ARGUMENTS] has length [2] but [" - + WrongLengthOfArgConstant.class.getName() + "#execute] takes [1] argument.")); + Exception e = expectScriptThrows( + IllegalArgumentException.class, + false, + () -> scriptEngine.compile("testWrongLengthOfArgConstant", "1", WrongLengthOfArgConstant.CONTEXT, emptyMap()) + ); + assertThat( + e.getMessage(), + startsWith( + "[" + + WrongLengthOfArgConstant.class.getName() + + "#ARGUMENTS] has length [2] but [" + + WrongLengthOfArgConstant.class.getName() + + "#execute] takes [1] argument." + ) + ); } public abstract static class UnknownArgType { @@ -613,14 +881,24 @@ public interface Factory { public static final ScriptContext CONTEXT = new ScriptContext<>("unknownargtype", Factory.class); - public static final String[] PARAMETERS = new String[] {"foo"}; + public static final String[] PARAMETERS = new String[] { "foo" }; + public abstract Object execute(UnknownArgType foo); } + public void testUnknownArgType() { - Exception e = expectScriptThrows(IllegalArgumentException.class, false, () -> - scriptEngine.compile("testUnknownArgType0", "1", UnknownArgType.CONTEXT, emptyMap())); - assertEquals("[foo] is of unknown type [" + UnknownArgType.class.getName() + ". Painless interfaces can only accept arguments " - + "that are of whitelisted types.", e.getMessage()); + Exception e = expectScriptThrows( + IllegalArgumentException.class, + false, + () -> scriptEngine.compile("testUnknownArgType0", "1", UnknownArgType.CONTEXT, emptyMap()) + ); + assertEquals( + "[foo] is of unknown type [" + + UnknownArgType.class.getName() + + ". Painless interfaces can only accept arguments " + + "that are of whitelisted types.", + e.getMessage() + ); } public abstract static class UnknownReturnType { @@ -630,14 +908,25 @@ public interface Factory { public static final ScriptContext CONTEXT = new ScriptContext<>("unknownreturntype", Factory.class); - public static final String[] PARAMETERS = new String[] {"foo"}; + public static final String[] PARAMETERS = new String[] { "foo" }; + public abstract UnknownReturnType execute(String foo); } + public void testUnknownReturnType() { - Exception e = expectScriptThrows(IllegalArgumentException.class, false, () -> - scriptEngine.compile("testUnknownReturnType0", "1", UnknownReturnType.CONTEXT, emptyMap())); - assertEquals("Painless can only implement execute methods returning a whitelisted type but [" + UnknownReturnType.class.getName() - + "#execute] returns [" + UnknownReturnType.class.getName() + "] which isn't whitelisted.", e.getMessage()); + Exception e = expectScriptThrows( + IllegalArgumentException.class, + false, + () -> scriptEngine.compile("testUnknownReturnType0", "1", UnknownReturnType.CONTEXT, emptyMap()) + ); + assertEquals( + "Painless can only implement execute methods returning a whitelisted type but [" + + UnknownReturnType.class.getName() + + "#execute] returns [" + + UnknownReturnType.class.getName() + + "] which isn't whitelisted.", + e.getMessage() + ); } public abstract static class UnknownArgTypeInArray { @@ -647,14 +936,24 @@ public interface Factory { public static final ScriptContext CONTEXT = new ScriptContext<>("unknownargtypeinarray", Factory.class); - public static final String[] PARAMETERS = new String[] {"foo"}; + public static final String[] PARAMETERS = new String[] { "foo" }; + public abstract Object execute(UnknownArgTypeInArray[] foo); } + public void testUnknownArgTypeInArray() { - Exception e = expectScriptThrows(IllegalArgumentException.class, false, () -> - scriptEngine.compile("testUnknownAryTypeInArray0", "1", UnknownArgTypeInArray.CONTEXT, emptyMap())); - assertEquals("[foo] is of unknown type [" + UnknownArgTypeInArray.class.getName() + ". Painless interfaces can only accept " - + "arguments that are of whitelisted types.", e.getMessage()); + Exception e = expectScriptThrows( + IllegalArgumentException.class, + false, + () -> scriptEngine.compile("testUnknownAryTypeInArray0", "1", UnknownArgTypeInArray.CONTEXT, emptyMap()) + ); + assertEquals( + "[foo] is of unknown type [" + + UnknownArgTypeInArray.class.getName() + + ". Painless interfaces can only accept " + + "arguments that are of whitelisted types.", + e.getMessage() + ); } public abstract static class TwoExecuteMethods { @@ -665,12 +964,21 @@ public interface Factory { public static final ScriptContext CONTEXT = new ScriptContext<>("twoexecutemethods", Factory.class); public abstract Object execute(); + public abstract Object execute(boolean foo); } + public void testTwoExecuteMethods() { - Exception e = expectScriptThrows(IllegalArgumentException.class, false, () -> - scriptEngine.compile("testTwoExecuteMethods0", "null", TwoExecuteMethods.CONTEXT, emptyMap())); - assertEquals("Painless can only implement interfaces that have a single method named [execute] but [" - + TwoExecuteMethods.class.getName() + "] has more than one.", e.getMessage()); + Exception e = expectScriptThrows( + IllegalArgumentException.class, + false, + () -> scriptEngine.compile("testTwoExecuteMethods0", "null", TwoExecuteMethods.CONTEXT, emptyMap()) + ); + assertEquals( + "Painless can only implement interfaces that have a single method named [execute] but [" + + TwoExecuteMethods.class.getName() + + "] has more than one.", + e.getMessage() + ); } } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/BasicAPITests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/BasicAPITests.java index 28bc3a2cc378b..7b3c501b2bced 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/BasicAPITests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/BasicAPITests.java @@ -17,28 +17,68 @@ public class BasicAPITests extends ScriptTestCase { public void testListIterator() { - assertEquals(3, exec("List x = new ArrayList(); x.add(2); x.add(3); x.add(-2); Iterator y = x.iterator(); " + - "int total = 0; while (y.hasNext()) total += y.next(); return total;")); - assertEquals("abc", exec("List x = new ArrayList(); x.add(\"a\"); x.add(\"b\"); x.add(\"c\"); " + - "Iterator y = x.iterator(); String total = \"\"; while (y.hasNext()) total += y.next(); return total;")); - assertEquals(3, exec("def x = new ArrayList(); x.add(2); x.add(3); x.add(-2); def y = x.iterator(); " + - "def total = 0; while (y.hasNext()) total += y.next(); return total;")); + assertEquals( + 3, + exec( + "List x = new ArrayList(); x.add(2); x.add(3); x.add(-2); Iterator y = x.iterator(); " + + "int total = 0; while (y.hasNext()) total += y.next(); return total;" + ) + ); + assertEquals( + "abc", + exec( + "List x = new ArrayList(); x.add(\"a\"); x.add(\"b\"); x.add(\"c\"); " + + "Iterator y = x.iterator(); String total = \"\"; while (y.hasNext()) total += y.next(); return total;" + ) + ); + assertEquals( + 3, + exec( + "def x = new ArrayList(); x.add(2); x.add(3); x.add(-2); def y = x.iterator(); " + + "def total = 0; while (y.hasNext()) total += y.next(); return total;" + ) + ); } public void testSetIterator() { - assertEquals(3, exec("Set x = new HashSet(); x.add(2); x.add(3); x.add(-2); Iterator y = x.iterator(); " + - "int total = 0; while (y.hasNext()) total += y.next(); return total;")); - assertEquals("abc", exec("Set x = new HashSet(); x.add(\"a\"); x.add(\"b\"); x.add(\"c\"); " + - "Iterator y = x.iterator(); String total = \"\"; while (y.hasNext()) total += y.next(); return total;")); - assertEquals(3, exec("def x = new HashSet(); x.add(2); x.add(3); x.add(-2); def y = x.iterator(); " + - "def total = 0; while (y.hasNext()) total += (int)y.next(); return total;")); + assertEquals( + 3, + exec( + "Set x = new HashSet(); x.add(2); x.add(3); x.add(-2); Iterator y = x.iterator(); " + + "int total = 0; while (y.hasNext()) total += y.next(); return total;" + ) + ); + assertEquals( + "abc", + exec( + "Set x = new HashSet(); x.add(\"a\"); x.add(\"b\"); x.add(\"c\"); " + + "Iterator y = x.iterator(); String total = \"\"; while (y.hasNext()) total += y.next(); return total;" + ) + ); + assertEquals( + 3, + exec( + "def x = new HashSet(); x.add(2); x.add(3); x.add(-2); def y = x.iterator(); " + + "def total = 0; while (y.hasNext()) total += (int)y.next(); return total;" + ) + ); } public void testMapIterator() { - assertEquals(3, exec("Map x = new HashMap(); x.put(2, 2); x.put(3, 3); x.put(-2, -2); Iterator y = x.keySet().iterator(); " + - "int total = 0; while (y.hasNext()) total += (int)y.next(); return total;")); - assertEquals(3, exec("Map x = new HashMap(); x.put(2, 2); x.put(3, 3); x.put(-2, -2); Iterator y = x.values().iterator(); " + - "int total = 0; while (y.hasNext()) total += (int)y.next(); return total;")); + assertEquals( + 3, + exec( + "Map x = new HashMap(); x.put(2, 2); x.put(3, 3); x.put(-2, -2); Iterator y = x.keySet().iterator(); " + + "int total = 0; while (y.hasNext()) total += (int)y.next(); return total;" + ) + ); + assertEquals( + 3, + exec( + "Map x = new HashMap(); x.put(2, 2); x.put(3, 3); x.put(-2, -2); Iterator y = x.values().iterator(); " + + "int total = 0; while (y.hasNext()) total += (int)y.next(); return total;" + ) + ); } /** Test loads and stores with a map */ @@ -59,8 +99,7 @@ public void testUpdateMapLoadStore() { ctx.put("_source", _source); params.put("ctx", ctx); - assertEquals("testvalue", exec("params.ctx._source['load'].5 = params.ctx._source['load'].remove('load5')", - params, true)); + assertEquals("testvalue", exec("params.ctx._source['load'].5 = params.ctx._source['load'].remove('load5')", params, true)); } /** Test loads and stores with a list */ @@ -119,8 +158,13 @@ public void testPrimitivesHaveMethods() { } public void testPublicMemberAccess() { - assertEquals(5, exec("org.elasticsearch.painless.FeatureTestObject ft = new org.elasticsearch.painless.FeatureTestObject();" + - "ft.z = 5; return ft.z;")); + assertEquals( + 5, + exec( + "org.elasticsearch.painless.FeatureTestObject ft = new org.elasticsearch.painless.FeatureTestObject();" + + "ft.z = 5; return ft.z;" + ) + ); } public void testNoSemicolon() { @@ -134,17 +178,19 @@ public void testStatic() { public void testRandomUUID() { assertTrue( - Pattern.compile("\\p{XDigit}{8}(-\\p{XDigit}{4}){3}-\\p{XDigit}{12}").matcher( - (String)exec( - "UUID a = UUID.randomUUID();" + - "String s = a.toString(); " + - "UUID b = UUID.fromString(s);" + - "if (a.equals(b) == false) {" + - " throw new RuntimeException('uuids did not match');" + - "}" + - "return s;" + Pattern.compile("\\p{XDigit}{8}(-\\p{XDigit}{4}){3}-\\p{XDigit}{12}") + .matcher( + (String) exec( + "UUID a = UUID.randomUUID();" + + "String s = a.toString(); " + + "UUID b = UUID.fromString(s);" + + "if (a.equals(b) == false) {" + + " throw new RuntimeException('uuids did not match');" + + "}" + + "return s;" ) - ).matches() + ) + .matches() ); } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/BasicExpressionTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/BasicExpressionTests.java index 172708d01d70b..bbda09b963686 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/BasicExpressionTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/BasicExpressionTests.java @@ -28,8 +28,8 @@ public void testReturnConstant() { assertEquals(33.0F, exec("return 33f")); assertEquals(34.0F, exec("return 34.0F")); assertEquals(35.0F, exec("return 35F")); - assertEquals((byte)255, exec("return (byte)255")); - assertEquals((short)5, exec("return (short)5")); + assertEquals((byte) 255, exec("return (byte)255")); + assertEquals((short) 5, exec("return (short)5")); assertEquals("string", exec("return \"string\"")); assertEquals("string", exec("return 'string'")); assertEquals(true, exec("return true")); @@ -53,12 +53,16 @@ public void testStringEscapes() { // `\"` is a `"` if surrounded by `"`s assertEquals("\"string", exec("\"\\\"string\"")); Exception e = expectScriptThrows(IllegalArgumentException.class, () -> exec("'\\\"string'", false)); - assertEquals("unexpected character ['\\\"]. The only valid escape sequences in strings starting with ['] are [\\\\] and [\\'].", - e.getMessage()); + assertEquals( + "unexpected character ['\\\"]. The only valid escape sequences in strings starting with ['] are [\\\\] and [\\'].", + e.getMessage() + ); // `\'` is a `'` if surrounded by `'`s e = expectScriptThrows(IllegalArgumentException.class, () -> exec("\"\\'string\"", false)); - assertEquals("unexpected character [\"\\']. The only valid escape sequences in strings starting with [\"] are [\\\\] and [\\\"].", - e.getMessage()); + assertEquals( + "unexpected character [\"\\']. The only valid escape sequences in strings starting with [\"] are [\\\\] and [\\\"].", + e.getMessage() + ); assertEquals("'string", exec("'\\'string'")); // We don't break native escapes like new line assertEquals("\nstring", exec("\"\nstring\"")); @@ -82,8 +86,8 @@ public void testDeclareVariable() { assertEquals(7L, exec("long l = 7; return l;")); assertEquals(7.0, exec("double d = 7; return d;")); assertEquals(32.0F, exec("float f = 32F; return f;")); - assertEquals((byte)255, exec("byte b = (byte)255; return b;")); - assertEquals((short)5, exec("short s = (short)5; return s;")); + assertEquals((byte) 255, exec("byte b = (byte)255; return b;")); + assertEquals((short) 5, exec("short s = (short)5; return s;")); assertEquals("string", exec("String s = \"string\"; return s;")); assertEquals(true, exec("boolean v = true; return v;")); assertEquals(false, exec("boolean v = false; return v;")); @@ -91,24 +95,16 @@ public void testDeclareVariable() { public void testCast() { assertEquals(1, exec("return (int)1.0;")); - assertEquals((byte)100, exec("double x = 100; return (byte)x;")); + assertEquals((byte) 100, exec("double x = 100; return (byte)x;")); - assertEquals(3, exec( - "Map x = new HashMap();\n" + - "Object y = x;\n" + - "((Map)y).put(2, 3);\n" + - "return x.get(2);\n")); + assertEquals(3, exec("Map x = new HashMap();\n" + "Object y = x;\n" + "((Map)y).put(2, 3);\n" + "return x.get(2);\n")); } public void testIllegalDefCast() { - Exception exception = expectScriptThrows(ClassCastException.class, () -> { - exec("def x = 1.0; int y = x; return y;"); - }); + Exception exception = expectScriptThrows(ClassCastException.class, () -> { exec("def x = 1.0; int y = x; return y;"); }); assertTrue(exception.getMessage().contains("cannot implicitly cast")); - exception = expectScriptThrows(ClassCastException.class, () -> { - exec("def x = (short)1; byte y = x; return y;"); - }); + exception = expectScriptThrows(ClassCastException.class, () -> { exec("def x = (short)1; byte y = x; return y;"); }); assertTrue(exception.getMessage().contains("cannot implicitly cast")); } @@ -116,11 +112,10 @@ public void testCat() { assertEquals("aaabbb", exec("return \"aaa\" + \"bbb\";")); assertEquals("aaabbb", exec("String aaa = \"aaa\", bbb = \"bbb\"; return aaa + bbb;")); - assertEquals("aaabbbbbbbbb", exec( - "String aaa = \"aaa\", bbb = \"bbb\"; int x;\n" + - "for (; x < 3; ++x) \n" + - " aaa += bbb;\n" + - "return aaa;")); + assertEquals( + "aaabbbbbbbbb", + exec("String aaa = \"aaa\", bbb = \"bbb\"; int x;\n" + "for (; x < 3; ++x) \n" + " aaa += bbb;\n" + "return aaa;") + ); } public void testComp() { @@ -169,88 +164,85 @@ public void testPrecedence() { public void testNullSafeDeref() { // Objects in general - // Call - assertNull( exec("String a = null; return a?.toString()")); + // Call + assertNull(exec("String a = null; return a?.toString()")); assertEquals("foo", exec("String a = 'foo'; return a?.toString()")); - assertNull( exec("def a = null; return a?.toString()")); + assertNull(exec("def a = null; return a?.toString()")); assertEquals("foo", exec("def a = 'foo'; return a?.toString()")); - // Call with primitive result - assertMustBeNullable( "String a = null; return a?.length()"); - assertMustBeNullable( "String a = 'foo'; return a?.length()"); - assertNull( exec("def a = null; return a?.length()")); - assertEquals(3, exec("def a = 'foo'; return a?.length()")); - // Read shortcut - assertMustBeNullable( "org.elasticsearch.painless.FeatureTestObject a = null; return a?.x"); + // Call with primitive result + assertMustBeNullable("String a = null; return a?.length()"); + assertMustBeNullable("String a = 'foo'; return a?.length()"); + assertNull(exec("def a = null; return a?.length()")); + assertEquals(3, exec("def a = 'foo'; return a?.length()")); + // Read shortcut + assertMustBeNullable("org.elasticsearch.painless.FeatureTestObject a = null; return a?.x"); assertMustBeNullable( - "org.elasticsearch.painless.FeatureTestObject a = new org.elasticsearch.painless.FeatureTestObject(); return a?.x"); - assertNull( exec("def a = null; return a?.x")); - assertEquals(0, exec("def a = new org.elasticsearch.painless.FeatureTestObject(); return a?.x")); + "org.elasticsearch.painless.FeatureTestObject a = new org.elasticsearch.painless.FeatureTestObject(); return a?.x" + ); + assertNull(exec("def a = null; return a?.x")); + assertEquals(0, exec("def a = new org.elasticsearch.painless.FeatureTestObject(); return a?.x")); // Maps - // Call - assertNull( exec("Map a = null; return a?.toString()")); + // Call + assertNull(exec("Map a = null; return a?.toString()")); assertEquals("{}", exec("Map a = [:]; return a?.toString()")); - assertNull( exec("def a = null; return a?.toString()")); + assertNull(exec("def a = null; return a?.toString()")); assertEquals("{}", exec("def a = [:]; return a?.toString()")); - // Call with primitive result - assertMustBeNullable( "Map a = [:]; return a?.size()"); - assertMustBeNullable( "Map a = null; return a?.size()"); - assertNull( exec("def a = null; return a?.size()")); - assertEquals(0, exec("def a = [:]; return a?.size()")); - // Read shortcut - assertNull( exec("Map a = null; return a?.other")); // Read shortcut - assertEquals(1, exec("Map a = ['other':1]; return a?.other")); // Read shortcut - assertNull( exec("def a = null; return a?.other")); // Read shortcut - assertEquals(1, exec("def a = ['other':1]; return a?.other")); // Read shortcut + // Call with primitive result + assertMustBeNullable("Map a = [:]; return a?.size()"); + assertMustBeNullable("Map a = null; return a?.size()"); + assertNull(exec("def a = null; return a?.size()")); + assertEquals(0, exec("def a = [:]; return a?.size()")); + // Read shortcut + assertNull(exec("Map a = null; return a?.other")); // Read shortcut + assertEquals(1, exec("Map a = ['other':1]; return a?.other")); // Read shortcut + assertNull(exec("def a = null; return a?.other")); // Read shortcut + assertEquals(1, exec("def a = ['other':1]; return a?.other")); // Read shortcut // Array // Since you can't invoke methods on arrays we skip the toString and hashCode tests assertMustBeNullable("int[] a = null; return a?.length"); assertMustBeNullable("int[] a = new int[] {2, 3}; return a?.length"); - assertNull( exec("def a = null; return a?.length")); + assertNull(exec("def a = null; return a?.length")); assertEquals(2, exec("def a = new int[] {2, 3}; return a?.length")); // Results from maps (should just work but let's test anyway) FeatureTestObject t = new FeatureTestObject(); - assertNull( exec("Map a = ['thing': params.t]; return a.other?.getX()", singletonMap("t", t), true)); - assertNull( exec("Map a = ['thing': params.t]; return a.other?.x", singletonMap("t", t), true)); - assertNull( exec("def a = ['thing': params.t]; return a.other?.getX()", singletonMap("t", t), true)); - assertNull( exec("def a = ['thing': params.t]; return a.other?.x", singletonMap("t", t), true)); + assertNull(exec("Map a = ['thing': params.t]; return a.other?.getX()", singletonMap("t", t), true)); + assertNull(exec("Map a = ['thing': params.t]; return a.other?.x", singletonMap("t", t), true)); + assertNull(exec("def a = ['thing': params.t]; return a.other?.getX()", singletonMap("t", t), true)); + assertNull(exec("def a = ['thing': params.t]; return a.other?.x", singletonMap("t", t), true)); assertEquals(0, exec("Map a = ['other': params.t]; return a.other?.getX()", singletonMap("t", t), true)); - assertEquals(0, exec("Map a = ['other': params.t]; return a.other?.x", singletonMap("t", t), true)); + assertEquals(0, exec("Map a = ['other': params.t]; return a.other?.x", singletonMap("t", t), true)); assertEquals(0, exec("def a = ['other': params.t]; return a.other?.getX()", singletonMap("t", t), true)); - assertEquals(0, exec("def a = ['other': params.t]; return a.other?.x", singletonMap("t", t), true)); + assertEquals(0, exec("def a = ['other': params.t]; return a.other?.x", singletonMap("t", t), true)); // Chains - assertNull( exec("Map a = ['thing': ['cat': params.t]]; return a.other?.cat?.getX()", singletonMap("t", t), true)); - assertNull( exec("Map a = ['thing': ['cat': params.t]]; return a.other?.cat?.x", singletonMap("t", t), true)); - assertNull( exec("def a = ['thing': ['cat': params.t]]; return a.other?.cat?.getX()", singletonMap("t", t), true)); - assertNull( exec("def a = ['thing': ['cat': params.t]]; return a.other?.cat?.x", singletonMap("t", t), true)); + assertNull(exec("Map a = ['thing': ['cat': params.t]]; return a.other?.cat?.getX()", singletonMap("t", t), true)); + assertNull(exec("Map a = ['thing': ['cat': params.t]]; return a.other?.cat?.x", singletonMap("t", t), true)); + assertNull(exec("def a = ['thing': ['cat': params.t]]; return a.other?.cat?.getX()", singletonMap("t", t), true)); + assertNull(exec("def a = ['thing': ['cat': params.t]]; return a.other?.cat?.x", singletonMap("t", t), true)); assertEquals(0, exec("Map a = ['other': ['cat': params.t]]; return a.other?.cat?.getX()", singletonMap("t", t), true)); - assertEquals(0, exec("Map a = ['other': ['cat': params.t]]; return a.other?.cat?.x", singletonMap("t", t), true)); + assertEquals(0, exec("Map a = ['other': ['cat': params.t]]; return a.other?.cat?.x", singletonMap("t", t), true)); assertEquals(0, exec("def a = ['other': ['cat': params.t]]; return a.other?.cat?.getX()", singletonMap("t", t), true)); - assertEquals(0, exec("def a = ['other': ['cat': params.t]]; return a.other?.cat?.x", singletonMap("t", t), true)); + assertEquals(0, exec("def a = ['other': ['cat': params.t]]; return a.other?.cat?.x", singletonMap("t", t), true)); // Assignments - assertNull(exec( - "def a = [:];\n" - + "a.missing_length = a.missing?.length();\n" - + "return a.missing_length", true)); - assertEquals(3, exec( - "def a = [:];\n" - + "a.missing = 'foo';\n" - + "a.missing_length = a.missing?.length();\n" - + "return a.missing_length", true)); + assertNull(exec("def a = [:];\n" + "a.missing_length = a.missing?.length();\n" + "return a.missing_length", true)); + assertEquals( + 3, + exec("def a = [:];\n" + "a.missing = 'foo';\n" + "a.missing_length = a.missing?.length();\n" + "return a.missing_length", true) + ); // Writes, all unsupported at this point -// assertEquals(null, exec("org.elasticsearch.painless.FeatureTestObject a = null; return a?.x")); // Read field -// assertEquals(null, exec("org.elasticsearch.painless.FeatureTestObject a = null; a?.x = 7; return a?.x")); // Write field -// assertEquals(null, exec("Map a = null; a?.other = 'wow'; return a?.other")); // Write shortcut -// assertEquals(null, exec("def a = null; a?.other = 'cat'; return a?.other")); // Write shortcut -// assertEquals(null, exec("Map a = ['thing': 'bar']; a.other?.cat = 'no'; return a.other?.cat")); -// assertEquals(null, exec("def a = ['thing': 'bar']; a.other?.cat = 'no'; return a.other?.cat")); -// assertEquals(null, exec("Map a = ['thing': 'bar']; a.other?.cat?.dog = 'wombat'; return a.other?.cat?.dog")); -// assertEquals(null, exec("def a = ['thing': 'bar']; a.other?.cat?.dog = 'wombat'; return a.other?.cat?.dog")); + // assertEquals(null, exec("org.elasticsearch.painless.FeatureTestObject a = null; return a?.x")); // Read field + // assertEquals(null, exec("org.elasticsearch.painless.FeatureTestObject a = null; a?.x = 7; return a?.x")); // Write field + // assertEquals(null, exec("Map a = null; a?.other = 'wow'; return a?.other")); // Write shortcut + // assertEquals(null, exec("def a = null; a?.other = 'cat'; return a?.other")); // Write shortcut + // assertEquals(null, exec("Map a = ['thing': 'bar']; a.other?.cat = 'no'; return a.other?.cat")); + // assertEquals(null, exec("def a = ['thing': 'bar']; a.other?.cat = 'no'; return a.other?.cat")); + // assertEquals(null, exec("Map a = ['thing': 'bar']; a.other?.cat?.dog = 'wombat'; return a.other?.cat?.dog")); + // assertEquals(null, exec("def a = ['thing': 'bar']; a.other?.cat?.dog = 'wombat'; return a.other?.cat?.dog")); } // test to ensure static interface methods are called correctly diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/BasicStatementTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/BasicStatementTests.java index c971163034523..26dcbd6533b47 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/BasicStatementTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/BasicStatementTests.java @@ -34,18 +34,22 @@ public void testIfStatement() { assertEquals(2, exec("int x = 4; if (x == 5) return 1; else if (x == 4) return 2; else return 0;")); assertEquals(1, exec("int x = 4; if (x == 5) return 1; else if (x == 4) return 1; else return 0;")); - assertEquals(3, exec( - "int x = 5;\n" + - "if (x == 5) {\n" + - " int y = 2;\n" + - " \n" + - " if (y == 2) {\n" + - " x = 3;\n" + - " }\n" + - " \n" + - "}\n" + - "\n" + - "return x;\n")); + assertEquals( + 3, + exec( + "int x = 5;\n" + + "if (x == 5) {\n" + + " int y = 2;\n" + + " \n" + + " if (y == 2) {\n" + + " x = 3;\n" + + " }\n" + + " \n" + + "}\n" + + "\n" + + "return x;\n" + ) + ); } public void testWhileStatement() { @@ -53,27 +57,28 @@ public void testWhileStatement() { assertEquals("aaaaaa", exec("String c = \"a\"; int x; while (x < 5) { c += \"a\"; ++x; } return c;")); Object value = exec( - " byte[][] b = new byte[5][5]; \n" + - " byte x = 0, y; \n" + - " \n" + - " while (x < 5) { \n" + - " y = 0; \n" + - " \n" + - " while (y < 5) { \n" + - " b[x][y] = (byte)(x*y); \n" + - " ++y; \n" + - " } \n" + - " \n" + - " ++x; \n" + - " } \n" + - " \n" + - " return b; \n"); - - byte[][] b = (byte[][])value; + " byte[][] b = new byte[5][5]; \n" + + " byte x = 0, y; \n" + + " \n" + + " while (x < 5) { \n" + + " y = 0; \n" + + " \n" + + " while (y < 5) { \n" + + " b[x][y] = (byte)(x*y); \n" + + " ++y; \n" + + " } \n" + + " \n" + + " ++x; \n" + + " } \n" + + " \n" + + " return b; \n" + ); + + byte[][] b = (byte[][]) value; for (byte x = 0; x < 5; ++x) { for (byte y = 0; y < 5; ++y) { - assertEquals(x*y, b[x][y]); + assertEquals(x * y, b[x][y]); } } } @@ -82,27 +87,28 @@ public void testDoWhileStatement() { assertEquals("aaaaaa", exec("String c = \"a\"; int x; do { c += \"a\"; ++x; } while (x < 5); return c;")); Object value = exec( - " int[][] b = new int[5][5]; \n" + - " int x = 0, y; \n" + - " \n" + - " do { \n" + - " y = 0; \n" + - " \n" + - " do { \n" + - " b[x][y] = x*y; \n" + - " ++y; \n" + - " } while (y < 5); \n" + - " \n" + - " ++x; \n" + - " } while (x < 5); \n" + - " \n" + - " return b; \n"); - - int[][] b = (int[][])value; + " int[][] b = new int[5][5]; \n" + + " int x = 0, y; \n" + + " \n" + + " do { \n" + + " y = 0; \n" + + " \n" + + " do { \n" + + " b[x][y] = x*y; \n" + + " ++y; \n" + + " } while (y < 5); \n" + + " \n" + + " ++x; \n" + + " } while (x < 5); \n" + + " \n" + + " return b; \n" + ); + + int[][] b = (int[][]) value; for (byte x = 0; x < 5; ++x) { for (byte y = 0; y < 5; ++y) { - assertEquals(x*y, b[x][y]); + assertEquals(x * y, b[x][y]); } } } @@ -111,96 +117,180 @@ public void testForStatement() { assertEquals(6, exec("int x, y; for (x = 0; x < 4; ++x) {y += x;} return y;")); assertEquals("aaaaaa", exec("String c = \"a\"; for (int x = 0; x < 5; ++x) c += \"a\"; return c;")); - assertEquals(6, exec("double test() { return 0.0; }" + - "int x, y; for (test(); x < 4; test()) {y += x; ++x;} return y;")); + assertEquals(6, exec("double test() { return 0.0; }" + "int x, y; for (test(); x < 4; test()) {y += x; ++x;} return y;")); Object value = exec( - " int[][] b = new int[5][5]; \n" + - " for (int x = 0; x < 5; ++x) { \n" + - " for (int y = 0; y < 5; ++y) { \n" + - " b[x][y] = x*y; \n" + - " } \n" + - " } \n" + - " \n" + - " return b; \n"); - - int[][] b = (int[][])value; + " int[][] b = new int[5][5]; \n" + + " for (int x = 0; x < 5; ++x) { \n" + + " for (int y = 0; y < 5; ++y) { \n" + + " b[x][y] = x*y; \n" + + " } \n" + + " } \n" + + " \n" + + " return b; \n" + ); + + int[][] b = (int[][]) value; for (byte x = 0; x < 5; ++x) { for (byte y = 0; y < 5; ++y) { - assertEquals(x*y, b[x][y]); + assertEquals(x * y, b[x][y]); } } } public void testIterableForEachStatement() { - assertEquals(6, exec("List l = new ArrayList(); l.add(1); l.add(2); l.add(3); int total = 0;" + - " for (int x : l) total += x; return total")); - assertEquals(6, exec("List l = new ArrayList(); l.add(1); l.add(2); l.add(3); int total = 0;" + - " for (x in l) total += x; return total")); - assertEquals("123", exec("List l = new ArrayList(); l.add('1'); l.add('2'); l.add('3'); String cat = '';" + - " for (String x : l) cat += x; return cat")); - assertEquals("123", exec("List l = new ArrayList(); l.add('1'); l.add('2'); l.add('3'); String cat = '';" + - " for (x in l) cat += x; return cat")); - assertEquals("1236", exec("Map m = new HashMap(); m.put('1', 1); m.put('2', 2); m.put('3', 3);" + - " String cat = ''; int total = 0;" + - " for (Map.Entry e : m.entrySet()) { cat += e.getKey(); total += e.getValue(); } return cat + total")); - assertEquals("1236", exec("Map m = new HashMap(); m.put('1', 1); m.put('2', 2); m.put('3', 3);" + - " String cat = ''; int total = 0;" + - " for (e in m.entrySet()) { cat += e.getKey(); total += e.getValue(); } return cat + total")); + assertEquals( + 6, + exec("List l = new ArrayList(); l.add(1); l.add(2); l.add(3); int total = 0;" + " for (int x : l) total += x; return total") + ); + assertEquals( + 6, + exec("List l = new ArrayList(); l.add(1); l.add(2); l.add(3); int total = 0;" + " for (x in l) total += x; return total") + ); + assertEquals( + "123", + exec( + "List l = new ArrayList(); l.add('1'); l.add('2'); l.add('3'); String cat = '';" + + " for (String x : l) cat += x; return cat" + ) + ); + assertEquals( + "123", + exec("List l = new ArrayList(); l.add('1'); l.add('2'); l.add('3'); String cat = '';" + " for (x in l) cat += x; return cat") + ); + assertEquals( + "1236", + exec( + "Map m = new HashMap(); m.put('1', 1); m.put('2', 2); m.put('3', 3);" + + " String cat = ''; int total = 0;" + + " for (Map.Entry e : m.entrySet()) { cat += e.getKey(); total += e.getValue(); } return cat + total" + ) + ); + assertEquals( + "1236", + exec( + "Map m = new HashMap(); m.put('1', 1); m.put('2', 2); m.put('3', 3);" + + " String cat = ''; int total = 0;" + + " for (e in m.entrySet()) { cat += e.getKey(); total += e.getValue(); } return cat + total" + ) + ); } public void testIterableForEachStatementDef() { - assertEquals(6, exec("def l = new ArrayList(); l.add(1); l.add(2); l.add(3); int total = 0;" + - " for (int x : l) total += x; return total")); - assertEquals(6, exec("def l = new ArrayList(); l.add(1); l.add(2); l.add(3); int total = 0;" + - " for (x in l) total += x; return total")); - assertEquals("123", exec("def l = new ArrayList(); l.add('1'); l.add('2'); l.add('3'); String cat = '';" + - " for (String x : l) cat += x; return cat")); - assertEquals("123", exec("def l = new ArrayList(); l.add('1'); l.add('2'); l.add('3'); String cat = '';" + - " for (x in l) cat += x; return cat")); - assertEquals("1236", exec("def m = new HashMap(); m.put('1', 1); m.put('2', 2); m.put('3', 3);" + - " String cat = ''; int total = 0;" + - " for (Map.Entry e : m.entrySet()) { cat += e.getKey(); total += e.getValue(); } return cat + total")); - assertEquals("1236", exec("def m = new HashMap(); m.put('1', 1); m.put('2', 2); m.put('3', 3);" + - " String cat = ''; int total = 0;" + - " for (e in m.entrySet()) { cat += e.getKey(); total += e.getValue(); } return cat + total")); + assertEquals( + 6, + exec("def l = new ArrayList(); l.add(1); l.add(2); l.add(3); int total = 0;" + " for (int x : l) total += x; return total") + ); + assertEquals( + 6, + exec("def l = new ArrayList(); l.add(1); l.add(2); l.add(3); int total = 0;" + " for (x in l) total += x; return total") + ); + assertEquals( + "123", + exec( + "def l = new ArrayList(); l.add('1'); l.add('2'); l.add('3'); String cat = '';" + " for (String x : l) cat += x; return cat" + ) + ); + assertEquals( + "123", + exec("def l = new ArrayList(); l.add('1'); l.add('2'); l.add('3'); String cat = '';" + " for (x in l) cat += x; return cat") + ); + assertEquals( + "1236", + exec( + "def m = new HashMap(); m.put('1', 1); m.put('2', 2); m.put('3', 3);" + + " String cat = ''; int total = 0;" + + " for (Map.Entry e : m.entrySet()) { cat += e.getKey(); total += e.getValue(); } return cat + total" + ) + ); + assertEquals( + "1236", + exec( + "def m = new HashMap(); m.put('1', 1); m.put('2', 2); m.put('3', 3);" + + " String cat = ''; int total = 0;" + + " for (e in m.entrySet()) { cat += e.getKey(); total += e.getValue(); } return cat + total" + ) + ); } public void testArrayForEachStatement() { - assertEquals(6, exec("int[] a = new int[3]; a[0] = 1; a[1] = 2; a[2] = 3; int total = 0;" + - " for (int x : a) total += x; return total")); - assertEquals(6, exec("int[] a = new int[3]; a[0] = 1; a[1] = 2; a[2] = 3; int total = 0;" + - " for (x in a) total += x; return total")); - assertEquals("123", exec("String[] a = new String[3]; a[0] = '1'; a[1] = '2'; a[2] = '3'; def total = '';" + - " for (String x : a) total += x; return total")); - assertEquals("123", exec("String[] a = new String[3]; a[0] = '1'; a[1] = '2'; a[2] = '3'; def total = '';" + - " for (x in a) total += x; return total")); - assertEquals(6, exec("int[][] i = new int[3][1]; i[0][0] = 1; i[1][0] = 2; i[2][0] = 3; int total = 0;" + - " for (int[] j : i) total += j[0]; return total")); - assertEquals(6, exec("int[][] i = new int[3][1]; i[0][0] = 1; i[1][0] = 2; i[2][0] = 3; int total = 0;" + - " for (j in i) total += j[0]; return total")); + assertEquals( + 6, + exec("int[] a = new int[3]; a[0] = 1; a[1] = 2; a[2] = 3; int total = 0;" + " for (int x : a) total += x; return total") + ); + assertEquals( + 6, + exec("int[] a = new int[3]; a[0] = 1; a[1] = 2; a[2] = 3; int total = 0;" + " for (x in a) total += x; return total") + ); + assertEquals( + "123", + exec( + "String[] a = new String[3]; a[0] = '1'; a[1] = '2'; a[2] = '3'; def total = '';" + + " for (String x : a) total += x; return total" + ) + ); + assertEquals( + "123", + exec( + "String[] a = new String[3]; a[0] = '1'; a[1] = '2'; a[2] = '3'; def total = '';" + " for (x in a) total += x; return total" + ) + ); + assertEquals( + 6, + exec( + "int[][] i = new int[3][1]; i[0][0] = 1; i[1][0] = 2; i[2][0] = 3; int total = 0;" + + " for (int[] j : i) total += j[0]; return total" + ) + ); + assertEquals( + 6, + exec( + "int[][] i = new int[3][1]; i[0][0] = 1; i[1][0] = 2; i[2][0] = 3; int total = 0;" + + " for (j in i) total += j[0]; return total" + ) + ); } public void testArrayForEachStatementDef() { - assertEquals(6, exec("def a = new int[3]; a[0] = 1; a[1] = 2; a[2] = 3; int total = 0;" + - " for (int x : a) total += x; return total")); - assertEquals(6, exec("def a = new int[3]; a[0] = 1; a[1] = 2; a[2] = 3; int total = 0;" + - " for (x in a) total += x; return total")); - assertEquals("123", exec("def a = new String[3]; a[0] = '1'; a[1] = '2'; a[2] = '3'; def total = '';" + - " for (String x : a) total += x; return total")); - assertEquals("123", exec("def a = new String[3]; a[0] = '1'; a[1] = '2'; a[2] = '3'; def total = '';" + - " for (x in a) total += x; return total")); - assertEquals(6, exec("def i = new int[3][1]; i[0][0] = 1; i[1][0] = 2; i[2][0] = 3; int total = 0;" + - " for (int[] j : i) total += j[0]; return total")); - assertEquals(6, exec("def i = new int[3][1]; i[0][0] = 1; i[1][0] = 2; i[2][0] = 3; int total = 0;" + - " for (j in i) total += j[0]; return total")); + assertEquals( + 6, + exec("def a = new int[3]; a[0] = 1; a[1] = 2; a[2] = 3; int total = 0;" + " for (int x : a) total += x; return total") + ); + assertEquals( + 6, + exec("def a = new int[3]; a[0] = 1; a[1] = 2; a[2] = 3; int total = 0;" + " for (x in a) total += x; return total") + ); + assertEquals( + "123", + exec( + "def a = new String[3]; a[0] = '1'; a[1] = '2'; a[2] = '3'; def total = '';" + + " for (String x : a) total += x; return total" + ) + ); + assertEquals( + "123", + exec("def a = new String[3]; a[0] = '1'; a[1] = '2'; a[2] = '3'; def total = '';" + " for (x in a) total += x; return total") + ); + assertEquals( + 6, + exec( + "def i = new int[3][1]; i[0][0] = 1; i[1][0] = 2; i[2][0] = 3; int total = 0;" + + " for (int[] j : i) total += j[0]; return total" + ) + ); + assertEquals( + 6, + exec( + "def i = new int[3][1]; i[0][0] = 1; i[1][0] = 2; i[2][0] = 3; int total = 0;" + " for (j in i) total += j[0]; return total" + ) + ); } public void testDeclarationStatement() { - assertEquals((byte)2, exec("byte a = 2; return a;")); - assertEquals((short)2, exec("short a = 2; return a;")); - assertEquals((char)2, exec("char a = 2; return a;")); + assertEquals((byte) 2, exec("byte a = 2; return a;")); + assertEquals((short) 2, exec("short a = 2; return a;")); + assertEquals((char) 2, exec("char a = 2; return a;")); assertEquals(2, exec("int a = 2; return a;")); assertEquals(2L, exec("long a = 2; return a;")); assertEquals(2F, exec("float a = 2; return a;")); @@ -245,8 +335,8 @@ public void testReturnStatement() { assertEquals(10, exec("return 10;")); assertEquals(5, exec("int x = 5; return x;")); assertEquals(4, exec("int[] x = new int[2]; x[1] = 4; return x[1];")); - assertEquals(5, ((short[])exec("short[] s = new short[3]; s[1] = 5; return s;"))[1]); - assertEquals(10, ((Map)exec("Map s = new HashMap(); s.put(\"x\", 10); return s;")).get("x")); + assertEquals(5, ((short[]) exec("short[] s = new short[3]; s[1] = 5; return s;"))[1]); + assertEquals(10, ((Map) exec("Map s = new HashMap(); s.put(\"x\", 10); return s;")).get("x")); } public abstract static class OneArg { @@ -256,33 +346,53 @@ public interface Factory { public static final ScriptContext CONTEXT = new ScriptContext<>("onearg", OneArg.Factory.class); - public static final String[] PARAMETERS = new String[] {"arg"}; + public static final String[] PARAMETERS = new String[] { "arg" }; + public abstract void execute(List arg); } + public void testVoidReturnStatement() { List expected = Collections.singletonList(1); - assertEquals(expected, exec("void test(List list) {if (list.isEmpty()) {list.add(1); return;} list.add(2);} " + - "List rtn = new ArrayList(); test(rtn); rtn")); - assertEquals(expected, exec("void test(List list) {if (list.isEmpty()) {list.add(1); return} list.add(2);} " + - "List rtn = new ArrayList(); test(rtn); rtn")); + assertEquals( + expected, + exec( + "void test(List list) {if (list.isEmpty()) {list.add(1); return;} list.add(2);} " + + "List rtn = new ArrayList(); test(rtn); rtn" + ) + ); + assertEquals( + expected, + exec( + "void test(List list) {if (list.isEmpty()) {list.add(1); return} list.add(2);} " + + "List rtn = new ArrayList(); test(rtn); rtn" + ) + ); expected = new ArrayList<>(); expected.add(0); expected.add(2); - assertEquals(expected, exec("void test(List list) {if (list.isEmpty()) {list.add(1); return} list.add(2);} " + - "List rtn = new ArrayList(); rtn.add(0); test(rtn); rtn")); + assertEquals( + expected, + exec( + "void test(List list) {if (list.isEmpty()) {list.add(1); return} list.add(2);} " + + "List rtn = new ArrayList(); rtn.add(0); test(rtn); rtn" + ) + ); ArrayList input = new ArrayList<>(); - scriptEngine.compile("testOneArg", "if (arg.isEmpty()) {arg.add(1); return;} arg.add(2);", - OneArg.CONTEXT, emptyMap()).newInstance().execute(input); + scriptEngine.compile("testOneArg", "if (arg.isEmpty()) {arg.add(1); return;} arg.add(2);", OneArg.CONTEXT, emptyMap()) + .newInstance() + .execute(input); assertEquals(Collections.singletonList(1), input); input = new ArrayList<>(); - scriptEngine.compile("testOneArg", "if (arg.isEmpty()) {arg.add(1); return} arg.add(2);", - OneArg.CONTEXT, emptyMap()).newInstance().execute(input); + scriptEngine.compile("testOneArg", "if (arg.isEmpty()) {arg.add(1); return} arg.add(2);", OneArg.CONTEXT, emptyMap()) + .newInstance() + .execute(input); assertEquals(Collections.singletonList(1), input); input = new ArrayList<>(); input.add(0); - scriptEngine.compile("testOneArg", "if (arg.isEmpty()) {arg.add(1); return} arg.add(2);", - OneArg.CONTEXT, emptyMap()).newInstance().execute(input); + scriptEngine.compile("testOneArg", "if (arg.isEmpty()) {arg.add(1); return} arg.add(2);", OneArg.CONTEXT, emptyMap()) + .newInstance() + .execute(input); assertEquals(expected, input); } @@ -296,218 +406,254 @@ public void testLastInBlockDoesntNeedSemi() { } public void testArrayLoopWithoutCounter() { - assertEquals(6L, exec("long sum = 0; long[] array = new long[] { 1, 2, 3 };" + - "for (int i = 0; i < array.length; i++) { sum += array[i] } return sum", - Collections.emptyMap(), - Collections.singletonMap(CompilerSettings.MAX_LOOP_COUNTER, "0"), - true - )); - assertEquals(6L, exec("long sum = 0; long[] array = new long[] { 1, 2, 3 };" + - "int i = 0; while (i < array.length) { sum += array[i++] } return sum", - Collections.emptyMap(), - Collections.singletonMap(CompilerSettings.MAX_LOOP_COUNTER, "0"), - true - )); - assertEquals(6L, exec("long sum = 0; long[] array = new long[] { 1, 2, 3 };" + - "int i = 0; do { sum += array[i++] } while (i < array.length); return sum", - Collections.emptyMap(), - Collections.singletonMap(CompilerSettings.MAX_LOOP_COUNTER, "0"), - true - )); + assertEquals( + 6L, + exec( + "long sum = 0; long[] array = new long[] { 1, 2, 3 };" + + "for (int i = 0; i < array.length; i++) { sum += array[i] } return sum", + Collections.emptyMap(), + Collections.singletonMap(CompilerSettings.MAX_LOOP_COUNTER, "0"), + true + ) + ); + assertEquals( + 6L, + exec( + "long sum = 0; long[] array = new long[] { 1, 2, 3 };" + + "int i = 0; while (i < array.length) { sum += array[i++] } return sum", + Collections.emptyMap(), + Collections.singletonMap(CompilerSettings.MAX_LOOP_COUNTER, "0"), + true + ) + ); + assertEquals( + 6L, + exec( + "long sum = 0; long[] array = new long[] { 1, 2, 3 };" + + "int i = 0; do { sum += array[i++] } while (i < array.length); return sum", + Collections.emptyMap(), + Collections.singletonMap(CompilerSettings.MAX_LOOP_COUNTER, "0"), + true + ) + ); } // tests both single break and multiple breaks used in a script public void testForWithBreak() { // single break test - assertEquals(1, exec( - "Map settings = ['test1' : '1'];" + - "int i = 0;" + - "List keys = ['test0', 'test1', 'test2'];" + - "for (; i < keys.size(); ++i) {" + - " if (settings.containsKey(keys[i])) {" + - " break;" + - " }" + - "}" + - "return i;" - )); + assertEquals( + 1, + exec( + "Map settings = ['test1' : '1'];" + + "int i = 0;" + + "List keys = ['test0', 'test1', 'test2'];" + + "for (; i < keys.size(); ++i) {" + + " if (settings.containsKey(keys[i])) {" + + " break;" + + " }" + + "}" + + "return i;" + ) + ); List expected = new ArrayList<>(); expected.add(1); expected.add(0); // multiple breaks test - assertEquals(expected, exec( - "Map outer = ['test1' : '1'];" + - "Map inner = ['test0' : '2'];" + - "boolean found = false;" + - "int i = 0, j = 0;" + - "List keys = ['test0', 'test1', 'test2'];" + - "for (; i < keys.size(); ++i) {" + - " if (outer.containsKey(keys[i])) {" + - " for (; j < keys.size(); ++j) {" + - " if (inner.containsKey(keys[j])) {" + - " found = true;" + - " break;" + - " }" + - " }" + - " if (found) {" + - " break;" + - " }" + - " }" + - "}" + - "[i, j];" - )); + assertEquals( + expected, + exec( + "Map outer = ['test1' : '1'];" + + "Map inner = ['test0' : '2'];" + + "boolean found = false;" + + "int i = 0, j = 0;" + + "List keys = ['test0', 'test1', 'test2'];" + + "for (; i < keys.size(); ++i) {" + + " if (outer.containsKey(keys[i])) {" + + " for (; j < keys.size(); ++j) {" + + " if (inner.containsKey(keys[j])) {" + + " found = true;" + + " break;" + + " }" + + " }" + + " if (found) {" + + " break;" + + " }" + + " }" + + "}" + + "[i, j];" + ) + ); expected.set(1, 3); // multiple breaks test, ignore inner break - assertEquals(expected, exec( - "Map outer = ['test1' : '1'];" + - "Map inner = ['test3' : '2'];" + - "int i = 0, j = 0;" + - "boolean found = false;" + - "List keys = ['test0', 'test1', 'test2'];" + - "for (; i < keys.size(); ++i) {" + - " if (outer.containsKey(keys[i])) {" + - " for (; j < keys.size(); ++j) {" + - " if (found) {" + - " break;" + - " }" + - " }" + - " found = true;" + - " if (found) {" + - " break;" + - " }" + - " }" + - "}" + - "[i, j];" - )); + assertEquals( + expected, + exec( + "Map outer = ['test1' : '1'];" + + "Map inner = ['test3' : '2'];" + + "int i = 0, j = 0;" + + "boolean found = false;" + + "List keys = ['test0', 'test1', 'test2'];" + + "for (; i < keys.size(); ++i) {" + + " if (outer.containsKey(keys[i])) {" + + " for (; j < keys.size(); ++j) {" + + " if (found) {" + + " break;" + + " }" + + " }" + + " found = true;" + + " if (found) {" + + " break;" + + " }" + + " }" + + "}" + + "[i, j];" + ) + ); expected.set(0, 3); expected.set(1, 1); // multiple breaks test, ignore outer break - assertEquals(expected, exec( - "Map outer = ['test3' : '1'];" + - "Map inner = ['test1' : '2'];" + - "int i = 0, j = 0;" + - "boolean found = false;" + - "List keys = ['test0', 'test1', 'test2'];" + - "for (; i < keys.size(); ++i) {" + - " if (outer.containsKey('test3')) {" + - " for (; j < keys.size(); ++j) {" + - " if (inner.containsKey(keys[j])) {" + - " break;" + - " }" + - " }" + - " if (found) {" + - " break;" + - " }" + - " }" + - "}" + - "[i, j];" - )); + assertEquals( + expected, + exec( + "Map outer = ['test3' : '1'];" + + "Map inner = ['test1' : '2'];" + + "int i = 0, j = 0;" + + "boolean found = false;" + + "List keys = ['test0', 'test1', 'test2'];" + + "for (; i < keys.size(); ++i) {" + + " if (outer.containsKey('test3')) {" + + " for (; j < keys.size(); ++j) {" + + " if (inner.containsKey(keys[j])) {" + + " break;" + + " }" + + " }" + + " if (found) {" + + " break;" + + " }" + + " }" + + "}" + + "[i, j];" + ) + ); } // tests both single break and multiple breaks used in a script public void testForEachWithBreak() { // single break test - assertEquals(1, exec( - "Map settings = ['test1' : '1'];" + - "int i = 0;" + - "List keys = ['test0', 'test1', 'test2'];" + - "for (String key : keys) {" + - " if (settings.containsKey(key)) {" + - " break;" + - " }" + - " ++i;" + - "}" + - "return i;" - )); + assertEquals( + 1, + exec( + "Map settings = ['test1' : '1'];" + + "int i = 0;" + + "List keys = ['test0', 'test1', 'test2'];" + + "for (String key : keys) {" + + " if (settings.containsKey(key)) {" + + " break;" + + " }" + + " ++i;" + + "}" + + "return i;" + ) + ); List expected = new ArrayList<>(); expected.add(1); expected.add(0); // multiple breaks test - assertEquals(expected, exec( - "Map outer = ['test1' : '1'];" + - "Map inner = ['test0' : '2'];" + - "int i = 0, j = 0;" + - "boolean found = false;" + - "List keys = ['test0', 'test1', 'test2'];" + - "for (String okey : keys) {" + - " if (outer.containsKey(okey)) {" + - " for (String ikey : keys) {" + - " if (inner.containsKey(ikey)) {" + - " found = true;" + - " break;" + - " }" + - " ++j;" + - " }" + - " if (found) {" + - " break;" + - " }" + - " }" + - " ++i;" + - "}" + - "[i, j];" - )); + assertEquals( + expected, + exec( + "Map outer = ['test1' : '1'];" + + "Map inner = ['test0' : '2'];" + + "int i = 0, j = 0;" + + "boolean found = false;" + + "List keys = ['test0', 'test1', 'test2'];" + + "for (String okey : keys) {" + + " if (outer.containsKey(okey)) {" + + " for (String ikey : keys) {" + + " if (inner.containsKey(ikey)) {" + + " found = true;" + + " break;" + + " }" + + " ++j;" + + " }" + + " if (found) {" + + " break;" + + " }" + + " }" + + " ++i;" + + "}" + + "[i, j];" + ) + ); expected.set(0, 3); expected.set(1, 1); // multiple breaks test, ignore outer break - assertEquals(expected, exec( - "Map outer = ['test1' : '1'];" + - "Map inner = ['test1' : '1'];" + - "int i = 0, j = 0;" + - "boolean found = false;" + - "List keys = ['test0', 'test1', 'test2'];" + - "for (String okey : keys) {" + - " if (outer.containsKey(okey)) {" + - " for (String ikey : keys) {" + - " if (inner.containsKey(ikey)) {" + - " break;" + - " }" + - " ++j;" + - " }" + - " if (found) {" + - " break;" + - " }" + - " }" + - " ++i;" + - "}" + - "[i, j];" - )); + assertEquals( + expected, + exec( + "Map outer = ['test1' : '1'];" + + "Map inner = ['test1' : '1'];" + + "int i = 0, j = 0;" + + "boolean found = false;" + + "List keys = ['test0', 'test1', 'test2'];" + + "for (String okey : keys) {" + + " if (outer.containsKey(okey)) {" + + " for (String ikey : keys) {" + + " if (inner.containsKey(ikey)) {" + + " break;" + + " }" + + " ++j;" + + " }" + + " if (found) {" + + " break;" + + " }" + + " }" + + " ++i;" + + "}" + + "[i, j];" + ) + ); expected.set(0, 1); expected.set(1, 3); // multiple breaks test, ignore inner break - assertEquals(expected, exec( - "Map outer = ['test1' : '1'];" + - "Map inner = ['test1' : '1'];" + - "int i = 0, j = 0;" + - "boolean found = false;" + - "List keys = ['test0', 'test1', 'test2'];" + - "for (String okey : keys) {" + - " if (outer.containsKey(okey)) {" + - " for (String ikey : keys) {" + - " if (found) {" + - " break;" + - " }" + - " ++j;" + - " }" + - " found = true;" + - " if (found) {" + - " break;" + - " }" + - " }" + - " ++i;" + - "}" + - "[i, j];" - )); + assertEquals( + expected, + exec( + "Map outer = ['test1' : '1'];" + + "Map inner = ['test1' : '1'];" + + "int i = 0, j = 0;" + + "boolean found = false;" + + "List keys = ['test0', 'test1', 'test2'];" + + "for (String okey : keys) {" + + " if (outer.containsKey(okey)) {" + + " for (String ikey : keys) {" + + " if (found) {" + + " break;" + + " }" + + " ++j;" + + " }" + + " found = true;" + + " if (found) {" + + " break;" + + " }" + + " }" + + " ++i;" + + "}" + + "[i, j];" + ) + ); } public void testNoLoopCounterInForEach() { @@ -522,12 +668,17 @@ public void testNoLoopCounterInForEach() { Arrays.fill(test, 2); Map params = new HashMap<>(); params.put("values", test); - int total = (int)exec("int total = 0; for (int value : params['values']) total += value; return total", params, false); + int total = (int) exec("int total = 0; for (int value : params['values']) total += value; return total", params, false); assertEquals(total, 20000000); - PainlessError pe = expectScriptThrows(PainlessError.class, () -> - exec("int total = 0; for (int value = 0; value < params['values'].length; ++value) total += value; return total", - params, false)); + PainlessError pe = expectScriptThrows( + PainlessError.class, + () -> exec( + "int total = 0; for (int value = 0; value < params['values'].length; ++value) total += value; return total", + params, + false + ) + ); assertEquals("The maximum number of statements that can be executed in a loop has been reached.", pe.getMessage()); } } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/BindingsTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/BindingsTests.java index 005e084cd8c9f..922153b03ff27 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/BindingsTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/BindingsTests.java @@ -45,7 +45,7 @@ public BindingTestClass(int state0, int state1) { } public int addWithState(int istateless, double dstateless) { - return istateless + state + (int)dstateless; + return istateless + state + (int) dstateless; } } @@ -59,7 +59,7 @@ public ThisBindingTestClass(BindingsTestScript bindingsTestScript, int state0, i } public int addThisWithState(int istateless, double dstateless) { - return istateless + state + (int)dstateless + bindingsTestScript.getTestValue(); + return istateless + state + (int) dstateless + bindingsTestScript.getTestValue(); } } @@ -97,11 +97,17 @@ public int instanceMul(int i, int j) { public abstract static class BindingsTestScript { public static final String[] PARAMETERS = { "test", "bound" }; - public int getTestValue() {return 7;} + + public int getTestValue() { + return 7; + } + public abstract int execute(int test, int bound); + public interface Factory { BindingsTestScript newInstance(); } + public static final ScriptContext CONTEXT = new ScriptContext<>("bindings_test", Factory.class); } @@ -112,18 +118,41 @@ protected Map, List> scriptContexts() { whitelists.add(WhitelistLoader.loadFromResourceFiles(PainlessPlugin.class, "org.elasticsearch.painless.test")); InstanceBindingTestClass instanceBindingTestClass = new InstanceBindingTestClass(1); - WhitelistInstanceBinding getter = new WhitelistInstanceBinding("test", instanceBindingTestClass, - "setInstanceBindingValue", "void", Collections.singletonList("int"), Collections.emptyList()); - WhitelistInstanceBinding setter = new WhitelistInstanceBinding("test", instanceBindingTestClass, - "getInstanceBindingValue", "int", Collections.emptyList(), Collections.emptyList()); - WhitelistInstanceBinding mul = new WhitelistInstanceBinding("test", instanceBindingTestClass, - "instanceMul", "int", List.of("int", "int"), List.of(CompileTimeOnlyAnnotation.INSTANCE)); + WhitelistInstanceBinding getter = new WhitelistInstanceBinding( + "test", + instanceBindingTestClass, + "setInstanceBindingValue", + "void", + Collections.singletonList("int"), + Collections.emptyList() + ); + WhitelistInstanceBinding setter = new WhitelistInstanceBinding( + "test", + instanceBindingTestClass, + "getInstanceBindingValue", + "int", + Collections.emptyList(), + Collections.emptyList() + ); + WhitelistInstanceBinding mul = new WhitelistInstanceBinding( + "test", + instanceBindingTestClass, + "instanceMul", + "int", + List.of("int", "int"), + List.of(CompileTimeOnlyAnnotation.INSTANCE) + ); List instanceBindingsList = new ArrayList<>(); instanceBindingsList.add(getter); instanceBindingsList.add(setter); instanceBindingsList.add(mul); - Whitelist instanceBindingsWhitelist = new Whitelist(instanceBindingTestClass.getClass().getClassLoader(), - Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), instanceBindingsList); + Whitelist instanceBindingsWhitelist = new Whitelist( + instanceBindingTestClass.getClass().getClassLoader(), + Collections.emptyList(), + Collections.emptyList(), + Collections.emptyList(), + instanceBindingsList + ); whitelists.add(instanceBindingsWhitelist); contexts.put(BindingsTestScript.CONTEXT, whitelists); diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/BoxedCastTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/BoxedCastTests.java index 9bd20bb60e2b8..9b1f02e8eeb9b 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/BoxedCastTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/BoxedCastTests.java @@ -13,8 +13,7 @@ public class BoxedCastTests extends ScriptTestCase { public void testMethodCallByteToBoxedCasts() { assertEquals(0, exec("byte u = 1; Byte b = Byte.valueOf((byte)1); b.compareTo(u);")); assertEquals(0, exec("byte u = 1; Short b = Short.valueOf((short)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("byte u = 1; Character b = Character.valueOf((char)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("byte u = 1; Character b = Character.valueOf((char)1); b.compareTo(u);")); assertEquals(0, exec("byte u = 1; Integer b = Integer.valueOf((int)1); b.compareTo(u);")); assertEquals(0, exec("byte u = 1; Long b = Long.valueOf((long)1); b.compareTo(u);")); assertEquals(0, exec("byte u = 1; Float b = Float.valueOf((float)1); b.compareTo(u);")); @@ -22,8 +21,10 @@ public void testMethodCallByteToBoxedCasts() { assertEquals(0, exec("Byte u = Byte.valueOf((byte)1); Byte b = Byte.valueOf((byte)1); b.compareTo(u);")); assertEquals(0, exec("Byte u = Byte.valueOf((byte)1); Short b = Short.valueOf((short)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Byte u = Byte.valueOf((byte)1); Character b = Character.valueOf((char)1); b.compareTo(u);")); + expectScriptThrows( + ClassCastException.class, + () -> exec("Byte u = Byte.valueOf((byte)1); Character b = Character.valueOf((char)1); b.compareTo(u);") + ); assertEquals(0, exec("Byte u = Byte.valueOf((byte)1); Integer b = Integer.valueOf((int)1); b.compareTo(u);")); assertEquals(0, exec("Byte u = Byte.valueOf((byte)1); Long b = Long.valueOf((long)1); b.compareTo(u);")); assertEquals(0, exec("Byte u = Byte.valueOf((byte)1); Float b = Float.valueOf((float)1); b.compareTo(u);")); @@ -31,8 +32,7 @@ public void testMethodCallByteToBoxedCasts() { assertEquals(0, exec("byte u = 1; def b = Byte.valueOf((byte)1); b.compareTo(u);")); assertEquals(0, exec("byte u = 1; def b = Short.valueOf((short)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("byte u = 1; def b = Character.valueOf((char)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("byte u = 1; def b = Character.valueOf((char)1); b.compareTo(u);")); assertEquals(0, exec("byte u = 1; def b = Integer.valueOf((int)1); b.compareTo(u);")); assertEquals(0, exec("byte u = 1; def b = Long.valueOf((long)1); b.compareTo(u);")); assertEquals(0, exec("byte u = 1; def b = Float.valueOf((float)1); b.compareTo(u);")); @@ -40,8 +40,10 @@ public void testMethodCallByteToBoxedCasts() { assertEquals(0, exec("Byte u = Byte.valueOf((byte)1); def b = Byte.valueOf((byte)1); b.compareTo(u);")); assertEquals(0, exec("Byte u = Byte.valueOf((byte)1); def b = Short.valueOf((short)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Byte u = Byte.valueOf((byte)1); def b = Character.valueOf((char)1); b.compareTo(u);")); + expectScriptThrows( + ClassCastException.class, + () -> exec("Byte u = Byte.valueOf((byte)1); def b = Character.valueOf((char)1); b.compareTo(u);") + ); assertEquals(0, exec("Byte u = Byte.valueOf((byte)1); def b = Integer.valueOf((int)1); b.compareTo(u);")); assertEquals(0, exec("Byte u = Byte.valueOf((byte)1); def b = Long.valueOf((long)1); b.compareTo(u);")); assertEquals(0, exec("Byte u = Byte.valueOf((byte)1); def b = Float.valueOf((float)1); b.compareTo(u);")); @@ -49,8 +51,10 @@ public void testMethodCallByteToBoxedCasts() { assertEquals(0, exec("def u = (byte)1; Byte b = Byte.valueOf((byte)1); b.compareTo(u);")); assertEquals(0, exec("def u = (byte)1; Short b = Short.valueOf((short)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (byte)1; Character b = Character.valueOf((char)1); b.compareTo(u);")); + expectScriptThrows( + ClassCastException.class, + () -> exec("def u = (byte)1; Character b = Character.valueOf((char)1); b.compareTo(u);") + ); assertEquals(0, exec("def u = (byte)1; Integer b = Integer.valueOf((int)1); b.compareTo(u);")); assertEquals(0, exec("def u = (byte)1; Long b = Long.valueOf((long)1); b.compareTo(u);")); assertEquals(0, exec("def u = (byte)1; Float b = Float.valueOf((float)1); b.compareTo(u);")); @@ -58,8 +62,7 @@ public void testMethodCallByteToBoxedCasts() { assertEquals(0, exec("def u = (byte)1; def b = Byte.valueOf((byte)1); b.compareTo(u);")); assertEquals(0, exec("def u = (byte)1; def b = Short.valueOf((short)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (byte)1; def b = Character.valueOf((char)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("def u = (byte)1; def b = Character.valueOf((char)1); b.compareTo(u);")); assertEquals(0, exec("def u = (byte)1; def b = Integer.valueOf((int)1); b.compareTo(u);")); assertEquals(0, exec("def u = (byte)1; def b = Long.valueOf((long)1); b.compareTo(u);")); assertEquals(0, exec("def u = (byte)1; def b = Float.valueOf((float)1); b.compareTo(u);")); @@ -67,61 +70,64 @@ public void testMethodCallByteToBoxedCasts() { } public void testMethodCallShortToBoxedCasts() { - expectScriptThrows(ClassCastException.class, - () -> exec("short u = 1; Byte b = Byte.valueOf((byte)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("short u = 1; Byte b = Byte.valueOf((byte)1); b.compareTo(u);")); assertEquals(0, exec("short u = 1; Short b = Short.valueOf((short)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("short u = 1; Character b = Character.valueOf((char)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("short u = 1; Character b = Character.valueOf((char)1); b.compareTo(u);")); assertEquals(0, exec("short u = 1; Integer b = Integer.valueOf((int)1); b.compareTo(u);")); assertEquals(0, exec("short u = 1; Long b = Long.valueOf((long)1); b.compareTo(u);")); assertEquals(0, exec("short u = 1; Float b = Float.valueOf((float)1); b.compareTo(u);")); assertEquals(0, exec("short u = 1; Double b = Double.valueOf((double)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Short u = Short.valueOf((short)1); Byte b = Byte.valueOf((byte)1); b.compareTo(u);")); + expectScriptThrows( + ClassCastException.class, + () -> exec("Short u = Short.valueOf((short)1); Byte b = Byte.valueOf((byte)1); b.compareTo(u);") + ); assertEquals(0, exec("Short u = Short.valueOf((short)1); Short b = Short.valueOf((short)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Short u = Short.valueOf((short)1); Character b = Character.valueOf((char)1); b.compareTo(u);")); + expectScriptThrows( + ClassCastException.class, + () -> exec("Short u = Short.valueOf((short)1); Character b = Character.valueOf((char)1); b.compareTo(u);") + ); assertEquals(0, exec("Short u = Short.valueOf((short)1); Integer b = Integer.valueOf((int)1); b.compareTo(u);")); assertEquals(0, exec("Short u = Short.valueOf((short)1); Long b = Long.valueOf((long)1); b.compareTo(u);")); assertEquals(0, exec("Short u = Short.valueOf((short)1); Float b = Float.valueOf((float)1); b.compareTo(u);")); assertEquals(0, exec("Short u = Short.valueOf((short)1); Double b = Double.valueOf((double)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("short u = 1; def b = Byte.valueOf((byte)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("short u = 1; def b = Byte.valueOf((byte)1); b.compareTo(u);")); assertEquals(0, exec("short u = 1; def b = Short.valueOf((short)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("short u = 1; def b = Character.valueOf((char)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("short u = 1; def b = Character.valueOf((char)1); b.compareTo(u);")); assertEquals(0, exec("short u = 1; def b = Integer.valueOf((int)1); b.compareTo(u);")); assertEquals(0, exec("short u = 1; def b = Long.valueOf((long)1); b.compareTo(u);")); assertEquals(0, exec("short u = 1; def b = Float.valueOf((float)1); b.compareTo(u);")); assertEquals(0, exec("short u = 1; def b = Double.valueOf((double)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Short u = Short.valueOf((short)1); def b = Byte.valueOf((byte)1); b.compareTo(u);")); + expectScriptThrows( + ClassCastException.class, + () -> exec("Short u = Short.valueOf((short)1); def b = Byte.valueOf((byte)1); b.compareTo(u);") + ); assertEquals(0, exec("Short u = Short.valueOf((short)1); def b = Short.valueOf((short)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Short u = Short.valueOf((short)1); def b = Character.valueOf((char)1); b.compareTo(u);")); + expectScriptThrows( + ClassCastException.class, + () -> exec("Short u = Short.valueOf((short)1); def b = Character.valueOf((char)1); b.compareTo(u);") + ); assertEquals(0, exec("Short u = Short.valueOf((short)1); def b = Integer.valueOf((int)1); b.compareTo(u);")); assertEquals(0, exec("Short u = Short.valueOf((short)1); def b = Long.valueOf((long)1); b.compareTo(u);")); assertEquals(0, exec("Short u = Short.valueOf((short)1); def b = Float.valueOf((float)1); b.compareTo(u);")); assertEquals(0, exec("Short u = Short.valueOf((short)1); def b = Double.valueOf((double)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (short)1; Byte b = Byte.valueOf((byte)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("def u = (short)1; Byte b = Byte.valueOf((byte)1); b.compareTo(u);")); assertEquals(0, exec("def u = (short)1; Short b = Short.valueOf((short)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (short)1; Character b = Character.valueOf((char)1); b.compareTo(u);")); + expectScriptThrows( + ClassCastException.class, + () -> exec("def u = (short)1; Character b = Character.valueOf((char)1); b.compareTo(u);") + ); assertEquals(0, exec("def u = (short)1; Integer b = Integer.valueOf((int)1); b.compareTo(u);")); assertEquals(0, exec("def u = (short)1; Long b = Long.valueOf((long)1); b.compareTo(u);")); assertEquals(0, exec("def u = (short)1; Float b = Float.valueOf((float)1); b.compareTo(u);")); assertEquals(0, exec("def u = (short)1; Double b = Double.valueOf((double)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (short)1; def b = Byte.valueOf((byte)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("def u = (short)1; def b = Byte.valueOf((byte)1); b.compareTo(u);")); assertEquals(0, exec("def u = (short)1; def b = Short.valueOf((short)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (short)1; def b = Character.valueOf((char)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("def u = (short)1; def b = Character.valueOf((char)1); b.compareTo(u);")); assertEquals(0, exec("def u = (short)1; def b = Integer.valueOf((int)1); b.compareTo(u);")); assertEquals(0, exec("def u = (short)1; def b = Long.valueOf((long)1); b.compareTo(u);")); assertEquals(0, exec("def u = (short)1; def b = Float.valueOf((float)1); b.compareTo(u);")); @@ -129,60 +135,60 @@ public void testMethodCallShortToBoxedCasts() { } public void testMethodCallCharacterToBoxedCasts() { - expectScriptThrows(ClassCastException.class, - () -> exec("char u = 1; Byte b = Byte.valueOf((byte)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("char u = 1; Short b = Short.valueOf((short)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("char u = 1; Byte b = Byte.valueOf((byte)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("char u = 1; Short b = Short.valueOf((short)1); b.compareTo(u);")); assertEquals(0, exec("char u = 1; Character b = Character.valueOf((char)1); b.compareTo(u);")); assertEquals(0, exec("char u = 1; Integer b = Integer.valueOf((int)1); b.compareTo(u);")); assertEquals(0, exec("char u = 1; Long b = Long.valueOf((long)1); b.compareTo(u);")); assertEquals(0, exec("char u = 1; Float b = Float.valueOf((float)1); b.compareTo(u);")); assertEquals(0, exec("char u = 1; Double b = Double.valueOf((double)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Character u = Character.valueOf((char)1); Byte b = Byte.valueOf((byte)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Character u = Character.valueOf((char)1); Short b = Short.valueOf((short)1); b.compareTo(u);")); + expectScriptThrows( + ClassCastException.class, + () -> exec("Character u = Character.valueOf((char)1); Byte b = Byte.valueOf((byte)1); b.compareTo(u);") + ); + expectScriptThrows( + ClassCastException.class, + () -> exec("Character u = Character.valueOf((char)1); Short b = Short.valueOf((short)1); b.compareTo(u);") + ); assertEquals(0, exec("Character u = Character.valueOf((char)1); Character b = Character.valueOf((char)1); b.compareTo(u);")); assertEquals(0, exec("Character u = Character.valueOf((char)1); Integer b = Integer.valueOf((int)1); b.compareTo(u);")); assertEquals(0, exec("Character u = Character.valueOf((char)1); Long b = Long.valueOf((long)1); b.compareTo(u);")); assertEquals(0, exec("Character u = Character.valueOf((char)1); Float b = Float.valueOf((float)1); b.compareTo(u);")); assertEquals(0, exec("Character u = Character.valueOf((char)1); Double b = Double.valueOf((double)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("char u = 1; def b = Byte.valueOf((byte)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("char u = 1; def b = Short.valueOf((short)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("char u = 1; def b = Byte.valueOf((byte)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("char u = 1; def b = Short.valueOf((short)1); b.compareTo(u);")); assertEquals(0, exec("char u = 1; def b = Character.valueOf((char)1); b.compareTo(u);")); assertEquals(0, exec("char u = 1; def b = Integer.valueOf((int)1); b.compareTo(u);")); assertEquals(0, exec("char u = 1; def b = Long.valueOf((long)1); b.compareTo(u);")); assertEquals(0, exec("char u = 1; def b = Float.valueOf((float)1); b.compareTo(u);")); assertEquals(0, exec("char u = 1; def b = Double.valueOf((double)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Character u = Character.valueOf((char)1); def b = Byte.valueOf((byte)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Character u = Character.valueOf((char)1); def b = Short.valueOf((short)1); b.compareTo(u);")); + expectScriptThrows( + ClassCastException.class, + () -> exec("Character u = Character.valueOf((char)1); def b = Byte.valueOf((byte)1); b.compareTo(u);") + ); + expectScriptThrows( + ClassCastException.class, + () -> exec("Character u = Character.valueOf((char)1); def b = Short.valueOf((short)1); b.compareTo(u);") + ); assertEquals(0, exec("Character u = Character.valueOf((char)1); def b = Character.valueOf((char)1); b.compareTo(u);")); assertEquals(0, exec("Character u = Character.valueOf((char)1); def b = Integer.valueOf((int)1); b.compareTo(u);")); assertEquals(0, exec("Character u = Character.valueOf((char)1); def b = Long.valueOf((long)1); b.compareTo(u);")); assertEquals(0, exec("Character u = Character.valueOf((char)1); def b = Float.valueOf((float)1); b.compareTo(u);")); assertEquals(0, exec("Character u = Character.valueOf((char)1); def b = Double.valueOf((double)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (char)1; Byte b = Byte.valueOf((byte)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (char)1; Short b = Short.valueOf((short)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("def u = (char)1; Byte b = Byte.valueOf((byte)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("def u = (char)1; Short b = Short.valueOf((short)1); b.compareTo(u);")); assertEquals(0, exec("def u = (char)1; Character b = Character.valueOf((char)1); b.compareTo(u);")); assertEquals(0, exec("def u = (char)1; Integer b = Integer.valueOf((int)1); b.compareTo(u);")); assertEquals(0, exec("def u = (char)1; Long b = Long.valueOf((long)1); b.compareTo(u);")); assertEquals(0, exec("def u = (char)1; Float b = Float.valueOf((float)1); b.compareTo(u);")); assertEquals(0, exec("def u = (char)1; Double b = Double.valueOf((double)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (char)1; def b = Byte.valueOf((byte)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (char)1; def b = Short.valueOf((short)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("def u = (char)1; def b = Byte.valueOf((byte)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("def u = (char)1; def b = Short.valueOf((short)1); b.compareTo(u);")); assertEquals(0, exec("def u = (char)1; def b = Character.valueOf((char)1); b.compareTo(u);")); assertEquals(0, exec("def u = (char)1; def b = Integer.valueOf((int)1); b.compareTo(u);")); assertEquals(0, exec("def u = (char)1; def b = Long.valueOf((long)1); b.compareTo(u);")); @@ -191,67 +197,70 @@ public void testMethodCallCharacterToBoxedCasts() { } public void testMethodCallIntegerToBoxedCasts() { - expectScriptThrows(ClassCastException.class, - () -> exec("int u = 1; Byte b = Byte.valueOf((byte)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("int u = 1; Short b = Short.valueOf((short)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("int u = 1; Character b = Character.valueOf((char)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("int u = 1; Byte b = Byte.valueOf((byte)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("int u = 1; Short b = Short.valueOf((short)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("int u = 1; Character b = Character.valueOf((char)1); b.compareTo(u);")); assertEquals(0, exec("int u = 1; Integer b = Integer.valueOf((int)1); b.compareTo(u);")); assertEquals(0, exec("int u = 1; Long b = Long.valueOf((long)1); b.compareTo(u);")); assertEquals(0, exec("int u = 1; Float b = Float.valueOf((float)1); b.compareTo(u);")); assertEquals(0, exec("int u = 1; Double b = Double.valueOf((double)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Integer u = Integer.valueOf((int)1); Byte b = Byte.valueOf((byte)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Integer u = Integer.valueOf((int)1); Short b = Short.valueOf((short)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Integer u = Integer.valueOf((int)1); Character b = Character.valueOf((char)1); b.compareTo(u);")); + expectScriptThrows( + ClassCastException.class, + () -> exec("Integer u = Integer.valueOf((int)1); Byte b = Byte.valueOf((byte)1); b.compareTo(u);") + ); + expectScriptThrows( + ClassCastException.class, + () -> exec("Integer u = Integer.valueOf((int)1); Short b = Short.valueOf((short)1); b.compareTo(u);") + ); + expectScriptThrows( + ClassCastException.class, + () -> exec("Integer u = Integer.valueOf((int)1); Character b = Character.valueOf((char)1); b.compareTo(u);") + ); assertEquals(0, exec("Integer u = Integer.valueOf((int)1); Integer b = Integer.valueOf((int)1); b.compareTo(u);")); assertEquals(0, exec("Integer u = Integer.valueOf((int)1); Long b = Long.valueOf((long)1); b.compareTo(u);")); assertEquals(0, exec("Integer u = Integer.valueOf((int)1); Float b = Float.valueOf((float)1); b.compareTo(u);")); assertEquals(0, exec("Integer u = Integer.valueOf((int)1); Double b = Double.valueOf((double)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("int u = 1; def b = Byte.valueOf((byte)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("int u = 1; def b = Short.valueOf((short)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("int u = 1; def b = Character.valueOf((char)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("int u = 1; def b = Byte.valueOf((byte)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("int u = 1; def b = Short.valueOf((short)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("int u = 1; def b = Character.valueOf((char)1); b.compareTo(u);")); assertEquals(0, exec("int u = 1; def b = Integer.valueOf((int)1); b.compareTo(u);")); assertEquals(0, exec("int u = 1; def b = Long.valueOf((long)1); b.compareTo(u);")); assertEquals(0, exec("int u = 1; def b = Float.valueOf((float)1); b.compareTo(u);")); assertEquals(0, exec("int u = 1; def b = Double.valueOf((double)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Integer u = Integer.valueOf((int)1); def b = Byte.valueOf((byte)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Integer u = Integer.valueOf((int)1); def b = Short.valueOf((short)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Integer u = Integer.valueOf((int)1); def b = Character.valueOf((char)1); b.compareTo(u);")); + expectScriptThrows( + ClassCastException.class, + () -> exec("Integer u = Integer.valueOf((int)1); def b = Byte.valueOf((byte)1); b.compareTo(u);") + ); + expectScriptThrows( + ClassCastException.class, + () -> exec("Integer u = Integer.valueOf((int)1); def b = Short.valueOf((short)1); b.compareTo(u);") + ); + expectScriptThrows( + ClassCastException.class, + () -> exec("Integer u = Integer.valueOf((int)1); def b = Character.valueOf((char)1); b.compareTo(u);") + ); assertEquals(0, exec("Integer u = Integer.valueOf((int)1); def b = Integer.valueOf((int)1); b.compareTo(u);")); assertEquals(0, exec("Integer u = Integer.valueOf((int)1); def b = Long.valueOf((long)1); b.compareTo(u);")); assertEquals(0, exec("Integer u = Integer.valueOf((int)1); def b = Float.valueOf((float)1); b.compareTo(u);")); assertEquals(0, exec("Integer u = Integer.valueOf((int)1); def b = Double.valueOf((double)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (int)1; Byte b = Byte.valueOf((byte)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (int)1; Short b = Short.valueOf((short)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (int)1; Character b = Character.valueOf((char)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("def u = (int)1; Byte b = Byte.valueOf((byte)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("def u = (int)1; Short b = Short.valueOf((short)1); b.compareTo(u);")); + expectScriptThrows( + ClassCastException.class, + () -> exec("def u = (int)1; Character b = Character.valueOf((char)1); b.compareTo(u);") + ); assertEquals(0, exec("def u = (int)1; Integer b = Integer.valueOf((int)1); b.compareTo(u);")); assertEquals(0, exec("def u = (int)1; Long b = Long.valueOf((long)1); b.compareTo(u);")); assertEquals(0, exec("def u = (int)1; Float b = Float.valueOf((float)1); b.compareTo(u);")); assertEquals(0, exec("def u = (int)1; Double b = Double.valueOf((double)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (int)1; def b = Byte.valueOf((byte)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (int)1; def b = Short.valueOf((short)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (int)1; def b = Character.valueOf((char)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("def u = (int)1; def b = Byte.valueOf((byte)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("def u = (int)1; def b = Short.valueOf((short)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("def u = (int)1; def b = Character.valueOf((char)1); b.compareTo(u);")); assertEquals(0, exec("def u = (int)1; def b = Integer.valueOf((int)1); b.compareTo(u);")); assertEquals(0, exec("def u = (int)1; def b = Long.valueOf((long)1); b.compareTo(u);")); assertEquals(0, exec("def u = (int)1; def b = Float.valueOf((float)1); b.compareTo(u);")); @@ -259,247 +268,256 @@ public void testMethodCallIntegerToBoxedCasts() { } public void testMethodCallLongToBoxedCasts() { - expectScriptThrows(ClassCastException.class, - () -> exec("long u = 1; Byte b = Byte.valueOf((byte)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("long u = 1; Short b = Short.valueOf((short)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("long u = 1; Character b = Character.valueOf((char)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("long u = 1; Integer b = Integer.valueOf((int)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("long u = 1; Byte b = Byte.valueOf((byte)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("long u = 1; Short b = Short.valueOf((short)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("long u = 1; Character b = Character.valueOf((char)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("long u = 1; Integer b = Integer.valueOf((int)1); b.compareTo(u);")); assertEquals(0, exec("long u = 1; Long b = Long.valueOf((long)1); b.compareTo(u);")); assertEquals(0, exec("long u = 1; Float b = Float.valueOf((float)1); b.compareTo(u);")); assertEquals(0, exec("long u = 1; Double b = Double.valueOf((double)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Long u = Long.valueOf((long)1); Byte b = Byte.valueOf((byte)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Long u = Long.valueOf((long)1); Short b = Short.valueOf((short)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Long u = Long.valueOf((long)1); Character b = Character.valueOf((char)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Long u = Long.valueOf((long)1); Integer b = Integer.valueOf((int)1); b.compareTo(u);")); + expectScriptThrows( + ClassCastException.class, + () -> exec("Long u = Long.valueOf((long)1); Byte b = Byte.valueOf((byte)1); b.compareTo(u);") + ); + expectScriptThrows( + ClassCastException.class, + () -> exec("Long u = Long.valueOf((long)1); Short b = Short.valueOf((short)1); b.compareTo(u);") + ); + expectScriptThrows( + ClassCastException.class, + () -> exec("Long u = Long.valueOf((long)1); Character b = Character.valueOf((char)1); b.compareTo(u);") + ); + expectScriptThrows( + ClassCastException.class, + () -> exec("Long u = Long.valueOf((long)1); Integer b = Integer.valueOf((int)1); b.compareTo(u);") + ); assertEquals(0, exec("Long u = Long.valueOf((long)1); Long b = Long.valueOf((long)1); b.compareTo(u);")); assertEquals(0, exec("Long u = Long.valueOf((long)1); Float b = Float.valueOf((float)1); b.compareTo(u);")); assertEquals(0, exec("Long u = Long.valueOf((long)1); Double b = Double.valueOf((double)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("long u = 1; def b = Byte.valueOf((byte)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("long u = 1; def b = Short.valueOf((short)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("long u = 1; def b = Character.valueOf((char)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("long u = 1; def b = Integer.valueOf((int)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("long u = 1; def b = Byte.valueOf((byte)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("long u = 1; def b = Short.valueOf((short)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("long u = 1; def b = Character.valueOf((char)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("long u = 1; def b = Integer.valueOf((int)1); b.compareTo(u);")); assertEquals(0, exec("long u = 1; def b = Long.valueOf((long)1); b.compareTo(u);")); assertEquals(0, exec("long u = 1; def b = Float.valueOf((float)1); b.compareTo(u);")); assertEquals(0, exec("long u = 1; def b = Double.valueOf((double)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Long u = Long.valueOf((long)1); def b = Byte.valueOf((byte)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Long u = Long.valueOf((long)1); def b = Short.valueOf((short)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Long u = Long.valueOf((long)1); def b = Character.valueOf((char)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Long u = Long.valueOf((long)1); def b = Integer.valueOf((int)1); b.compareTo(u);")); + expectScriptThrows( + ClassCastException.class, + () -> exec("Long u = Long.valueOf((long)1); def b = Byte.valueOf((byte)1); b.compareTo(u);") + ); + expectScriptThrows( + ClassCastException.class, + () -> exec("Long u = Long.valueOf((long)1); def b = Short.valueOf((short)1); b.compareTo(u);") + ); + expectScriptThrows( + ClassCastException.class, + () -> exec("Long u = Long.valueOf((long)1); def b = Character.valueOf((char)1); b.compareTo(u);") + ); + expectScriptThrows( + ClassCastException.class, + () -> exec("Long u = Long.valueOf((long)1); def b = Integer.valueOf((int)1); b.compareTo(u);") + ); assertEquals(0, exec("Long u = Long.valueOf((long)1); def b = Long.valueOf((long)1); b.compareTo(u);")); assertEquals(0, exec("Long u = Long.valueOf((long)1); def b = Float.valueOf((float)1); b.compareTo(u);")); assertEquals(0, exec("Long u = Long.valueOf((long)1); def b = Double.valueOf((double)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (long)1; Byte b = Byte.valueOf((byte)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (long)1; Short b = Short.valueOf((short)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (long)1; Character b = Character.valueOf((char)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (long)1; Integer b = Integer.valueOf((int)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("def u = (long)1; Byte b = Byte.valueOf((byte)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("def u = (long)1; Short b = Short.valueOf((short)1); b.compareTo(u);")); + expectScriptThrows( + ClassCastException.class, + () -> exec("def u = (long)1; Character b = Character.valueOf((char)1); b.compareTo(u);") + ); + expectScriptThrows(ClassCastException.class, () -> exec("def u = (long)1; Integer b = Integer.valueOf((int)1); b.compareTo(u);")); assertEquals(0, exec("def u = (long)1; Long b = Long.valueOf((long)1); b.compareTo(u);")); assertEquals(0, exec("def u = (long)1; Float b = Float.valueOf((float)1); b.compareTo(u);")); assertEquals(0, exec("def u = (long)1; Double b = Double.valueOf((double)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (long)1; def b = Byte.valueOf((byte)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (long)1; def b = Short.valueOf((short)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (long)1; def b = Character.valueOf((char)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (long)1; def b = Integer.valueOf((int)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("def u = (long)1; def b = Byte.valueOf((byte)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("def u = (long)1; def b = Short.valueOf((short)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("def u = (long)1; def b = Character.valueOf((char)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("def u = (long)1; def b = Integer.valueOf((int)1); b.compareTo(u);")); assertEquals(0, exec("def u = (long)1; def b = Long.valueOf((long)1); b.compareTo(u);")); assertEquals(0, exec("def u = (long)1; def b = Float.valueOf((float)1); b.compareTo(u);")); assertEquals(0, exec("def u = (long)1; def b = Double.valueOf((double)1); b.compareTo(u);")); } public void testMethodCallFloatToBoxedCasts() { - expectScriptThrows(ClassCastException.class, - () -> exec("float u = 1; Byte b = Byte.valueOf((byte)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("float u = 1; Short b = Short.valueOf((short)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("float u = 1; Character b = Character.valueOf((char)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("float u = 1; Integer b = Integer.valueOf((int)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("float u = 1; Long b = Long.valueOf((long)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("float u = 1; Byte b = Byte.valueOf((byte)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("float u = 1; Short b = Short.valueOf((short)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("float u = 1; Character b = Character.valueOf((char)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("float u = 1; Integer b = Integer.valueOf((int)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("float u = 1; Long b = Long.valueOf((long)1); b.compareTo(u);")); assertEquals(0, exec("float u = 1; Float b = Float.valueOf((float)1); b.compareTo(u);")); assertEquals(0, exec("float u = 1; Double b = Double.valueOf((double)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Float u = Float.valueOf((float)1); Byte b = Byte.valueOf((byte)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Float u = Float.valueOf((float)1); Short b = Short.valueOf((short)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Float u = Float.valueOf((float)1); Character b = Character.valueOf((char)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Float u = Float.valueOf((float)1); Integer b = Integer.valueOf((int)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Float u = Float.valueOf((float)1); Long b = Long.valueOf((long)1); b.compareTo(u);")); + expectScriptThrows( + ClassCastException.class, + () -> exec("Float u = Float.valueOf((float)1); Byte b = Byte.valueOf((byte)1); b.compareTo(u);") + ); + expectScriptThrows( + ClassCastException.class, + () -> exec("Float u = Float.valueOf((float)1); Short b = Short.valueOf((short)1); b.compareTo(u);") + ); + expectScriptThrows( + ClassCastException.class, + () -> exec("Float u = Float.valueOf((float)1); Character b = Character.valueOf((char)1); b.compareTo(u);") + ); + expectScriptThrows( + ClassCastException.class, + () -> exec("Float u = Float.valueOf((float)1); Integer b = Integer.valueOf((int)1); b.compareTo(u);") + ); + expectScriptThrows( + ClassCastException.class, + () -> exec("Float u = Float.valueOf((float)1); Long b = Long.valueOf((long)1); b.compareTo(u);") + ); assertEquals(0, exec("Float u = Float.valueOf((float)1); Float b = Float.valueOf((float)1); b.compareTo(u);")); assertEquals(0, exec("Float u = Float.valueOf((float)1); Double b = Double.valueOf((double)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("float u = 1; def b = Byte.valueOf((byte)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("float u = 1; def b = Short.valueOf((short)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("float u = 1; def b = Character.valueOf((char)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("float u = 1; def b = Integer.valueOf((int)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("float u = 1; def b = Long.valueOf((long)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("float u = 1; def b = Byte.valueOf((byte)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("float u = 1; def b = Short.valueOf((short)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("float u = 1; def b = Character.valueOf((char)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("float u = 1; def b = Integer.valueOf((int)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("float u = 1; def b = Long.valueOf((long)1); b.compareTo(u);")); assertEquals(0, exec("float u = 1; def b = Float.valueOf((float)1); b.compareTo(u);")); assertEquals(0, exec("float u = 1; def b = Double.valueOf((double)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Float u = Float.valueOf((float)1); def b = Byte.valueOf((byte)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Float u = Float.valueOf((float)1); def b = Short.valueOf((short)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Float u = Float.valueOf((float)1); def b = Character.valueOf((char)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Float u = Float.valueOf((float)1); def b = Integer.valueOf((int)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Float u = Float.valueOf((float)1); def b = Long.valueOf((long)1); b.compareTo(u);")); + expectScriptThrows( + ClassCastException.class, + () -> exec("Float u = Float.valueOf((float)1); def b = Byte.valueOf((byte)1); b.compareTo(u);") + ); + expectScriptThrows( + ClassCastException.class, + () -> exec("Float u = Float.valueOf((float)1); def b = Short.valueOf((short)1); b.compareTo(u);") + ); + expectScriptThrows( + ClassCastException.class, + () -> exec("Float u = Float.valueOf((float)1); def b = Character.valueOf((char)1); b.compareTo(u);") + ); + expectScriptThrows( + ClassCastException.class, + () -> exec("Float u = Float.valueOf((float)1); def b = Integer.valueOf((int)1); b.compareTo(u);") + ); + expectScriptThrows( + ClassCastException.class, + () -> exec("Float u = Float.valueOf((float)1); def b = Long.valueOf((long)1); b.compareTo(u);") + ); assertEquals(0, exec("Float u = Float.valueOf((float)1); def b = Float.valueOf((float)1); b.compareTo(u);")); assertEquals(0, exec("Float u = Float.valueOf((float)1); def b = Double.valueOf((double)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (float)1; Byte b = Byte.valueOf((byte)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (float)1; Short b = Short.valueOf((short)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (float)1; Character b = Character.valueOf((char)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (float)1; Integer b = Integer.valueOf((int)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (float)1; Long b = Long.valueOf((long)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("def u = (float)1; Byte b = Byte.valueOf((byte)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("def u = (float)1; Short b = Short.valueOf((short)1); b.compareTo(u);")); + expectScriptThrows( + ClassCastException.class, + () -> exec("def u = (float)1; Character b = Character.valueOf((char)1); b.compareTo(u);") + ); + expectScriptThrows(ClassCastException.class, () -> exec("def u = (float)1; Integer b = Integer.valueOf((int)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("def u = (float)1; Long b = Long.valueOf((long)1); b.compareTo(u);")); assertEquals(0, exec("def u = (float)1; Float b = Float.valueOf((float)1); b.compareTo(u);")); assertEquals(0, exec("def u = (float)1; Double b = Double.valueOf((double)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (float)1; def b = Byte.valueOf((byte)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (float)1; def b = Short.valueOf((short)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (float)1; def b = Character.valueOf((char)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (float)1; def b = Integer.valueOf((int)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (float)1; def b = Long.valueOf((long)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("def u = (float)1; def b = Byte.valueOf((byte)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("def u = (float)1; def b = Short.valueOf((short)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("def u = (float)1; def b = Character.valueOf((char)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("def u = (float)1; def b = Integer.valueOf((int)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("def u = (float)1; def b = Long.valueOf((long)1); b.compareTo(u);")); assertEquals(0, exec("def u = (float)1; def b = Float.valueOf((float)1); b.compareTo(u);")); assertEquals(0, exec("def u = (float)1; def b = Double.valueOf((double)1); b.compareTo(u);")); } public void testMethodCallDoubleToBoxedCasts() { - expectScriptThrows(ClassCastException.class, - () -> exec("double u = 1; Byte b = Byte.valueOf((byte)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("double u = 1; Short b = Short.valueOf((short)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("double u = 1; Character b = Character.valueOf((char)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("double u = 1; Integer b = Integer.valueOf((int)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("double u = 1; Long b = Long.valueOf((long)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("double u = 1; Float b = Float.valueOf((float)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("double u = 1; Byte b = Byte.valueOf((byte)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("double u = 1; Short b = Short.valueOf((short)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("double u = 1; Character b = Character.valueOf((char)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("double u = 1; Integer b = Integer.valueOf((int)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("double u = 1; Long b = Long.valueOf((long)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("double u = 1; Float b = Float.valueOf((float)1); b.compareTo(u);")); assertEquals(0, exec("double u = 1; Double b = Double.valueOf((double)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Double u = Double.valueOf((double)1); Byte b = Byte.valueOf((byte)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Double u = Double.valueOf((double)1); Short b = Short.valueOf((short)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Double u = Double.valueOf((double)1); Character b = Character.valueOf((char)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Double u = Double.valueOf((double)1); Integer b = Integer.valueOf((int)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Double u = Double.valueOf((double)1); Long b = Long.valueOf((long)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Double u = Double.valueOf((double)1); Float b = Float.valueOf((float)1); b.compareTo(u);")); + expectScriptThrows( + ClassCastException.class, + () -> exec("Double u = Double.valueOf((double)1); Byte b = Byte.valueOf((byte)1); b.compareTo(u);") + ); + expectScriptThrows( + ClassCastException.class, + () -> exec("Double u = Double.valueOf((double)1); Short b = Short.valueOf((short)1); b.compareTo(u);") + ); + expectScriptThrows( + ClassCastException.class, + () -> exec("Double u = Double.valueOf((double)1); Character b = Character.valueOf((char)1); b.compareTo(u);") + ); + expectScriptThrows( + ClassCastException.class, + () -> exec("Double u = Double.valueOf((double)1); Integer b = Integer.valueOf((int)1); b.compareTo(u);") + ); + expectScriptThrows( + ClassCastException.class, + () -> exec("Double u = Double.valueOf((double)1); Long b = Long.valueOf((long)1); b.compareTo(u);") + ); + expectScriptThrows( + ClassCastException.class, + () -> exec("Double u = Double.valueOf((double)1); Float b = Float.valueOf((float)1); b.compareTo(u);") + ); assertEquals(0, exec("Double u = Double.valueOf((double)1); Double b = Double.valueOf((double)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("double u = 1; def b = Byte.valueOf((byte)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("double u = 1; def b = Short.valueOf((short)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("double u = 1; def b = Character.valueOf((char)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("double u = 1; def b = Integer.valueOf((int)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("double u = 1; def b = Long.valueOf((long)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("double u = 1; def b = Float.valueOf((float)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("double u = 1; def b = Byte.valueOf((byte)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("double u = 1; def b = Short.valueOf((short)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("double u = 1; def b = Character.valueOf((char)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("double u = 1; def b = Integer.valueOf((int)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("double u = 1; def b = Long.valueOf((long)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("double u = 1; def b = Float.valueOf((float)1); b.compareTo(u);")); assertEquals(0, exec("double u = 1; def b = Double.valueOf((double)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Double u = Double.valueOf((double)1); def b = Byte.valueOf((byte)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Double u = Double.valueOf((double)1); def b = Short.valueOf((short)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Double u = Double.valueOf((double)1); def b = Character.valueOf((char)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Double u = Double.valueOf((double)1); def b = Integer.valueOf((int)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Double u = Double.valueOf((double)1); def b = Long.valueOf((long)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("Double u = Double.valueOf((double)1); def b = Float.valueOf((float)1); b.compareTo(u);")); + expectScriptThrows( + ClassCastException.class, + () -> exec("Double u = Double.valueOf((double)1); def b = Byte.valueOf((byte)1); b.compareTo(u);") + ); + expectScriptThrows( + ClassCastException.class, + () -> exec("Double u = Double.valueOf((double)1); def b = Short.valueOf((short)1); b.compareTo(u);") + ); + expectScriptThrows( + ClassCastException.class, + () -> exec("Double u = Double.valueOf((double)1); def b = Character.valueOf((char)1); b.compareTo(u);") + ); + expectScriptThrows( + ClassCastException.class, + () -> exec("Double u = Double.valueOf((double)1); def b = Integer.valueOf((int)1); b.compareTo(u);") + ); + expectScriptThrows( + ClassCastException.class, + () -> exec("Double u = Double.valueOf((double)1); def b = Long.valueOf((long)1); b.compareTo(u);") + ); + expectScriptThrows( + ClassCastException.class, + () -> exec("Double u = Double.valueOf((double)1); def b = Float.valueOf((float)1); b.compareTo(u);") + ); assertEquals(0, exec("Double u = Double.valueOf((double)1); def b = Double.valueOf((double)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (double)1; Byte b = Byte.valueOf((byte)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (double)1; Short b = Short.valueOf((short)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (double)1; Character b = Character.valueOf((char)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (double)1; Integer b = Integer.valueOf((int)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (double)1; Long b = Long.valueOf((long)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (double)1; Float b = Float.valueOf((float)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("def u = (double)1; Byte b = Byte.valueOf((byte)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("def u = (double)1; Short b = Short.valueOf((short)1); b.compareTo(u);")); + expectScriptThrows( + ClassCastException.class, + () -> exec("def u = (double)1; Character b = Character.valueOf((char)1); b.compareTo(u);") + ); + expectScriptThrows(ClassCastException.class, () -> exec("def u = (double)1; Integer b = Integer.valueOf((int)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("def u = (double)1; Long b = Long.valueOf((long)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("def u = (double)1; Float b = Float.valueOf((float)1); b.compareTo(u);")); assertEquals(0, exec("def u = (double)1; Double b = Double.valueOf((double)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (double)1; def b = Byte.valueOf((byte)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (double)1; def b = Short.valueOf((short)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (double)1; def b = Character.valueOf((char)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (double)1; def b = Integer.valueOf((int)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (double)1; def b = Long.valueOf((long)1); b.compareTo(u);")); - expectScriptThrows(ClassCastException.class, - () -> exec("def u = (double)1; def b = Float.valueOf((float)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("def u = (double)1; def b = Byte.valueOf((byte)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("def u = (double)1; def b = Short.valueOf((short)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("def u = (double)1; def b = Character.valueOf((char)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("def u = (double)1; def b = Integer.valueOf((int)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("def u = (double)1; def b = Long.valueOf((long)1); b.compareTo(u);")); + expectScriptThrows(ClassCastException.class, () -> exec("def u = (double)1; def b = Float.valueOf((float)1); b.compareTo(u);")); assertEquals(0, exec("def u = (double)1; def b = Double.valueOf((double)1); b.compareTo(u);")); } public void testReturnToByteBoxedCasts() { - assertEquals((byte)1, exec("Byte rtn() {return (byte)1} rtn()")); + assertEquals((byte) 1, exec("Byte rtn() {return (byte)1} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Byte rtn() {return (short)1} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Byte rtn() {return (char)1} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Byte rtn() {return (int)1} rtn()")); @@ -507,7 +525,7 @@ public void testReturnToByteBoxedCasts() { expectScriptThrows(ClassCastException.class, () -> exec("Byte rtn() {return (float)1} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Byte rtn() {return (double)1} rtn()")); - assertEquals((byte)1, exec("Byte rtn() {return Byte.valueOf((byte)1)} rtn()")); + assertEquals((byte) 1, exec("Byte rtn() {return Byte.valueOf((byte)1)} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Byte rtn() {return Short.valueOf((short)1)} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Byte rtn() {return Character.valueOf((char)1)} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Byte rtn() {return Integer.valueOf((int)1)} rtn()")); @@ -515,7 +533,7 @@ public void testReturnToByteBoxedCasts() { expectScriptThrows(ClassCastException.class, () -> exec("Byte rtn() {return Float.valueOf((float)1)} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Byte rtn() {return Double.valueOf((double)1)} rtn()")); - assertEquals((byte)1, exec("Byte rtn() {def d = (byte)1; return d} rtn()")); + assertEquals((byte) 1, exec("Byte rtn() {def d = (byte)1; return d} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Byte rtn() {def d = (short)1; return d} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Byte rtn() {def d = (char)1; return d} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Byte rtn() {def d = (int)1; return d} rtn()")); @@ -523,7 +541,7 @@ public void testReturnToByteBoxedCasts() { expectScriptThrows(ClassCastException.class, () -> exec("Byte rtn() {def d = (float)1; return d} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Byte rtn() {def d = (double)1; return d} rtn()")); - assertEquals((byte)1, exec("Byte rtn() {def d = Byte.valueOf((byte)1); return d} rtn()")); + assertEquals((byte) 1, exec("Byte rtn() {def d = Byte.valueOf((byte)1); return d} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Byte rtn() {def d = Short.valueOf((short)1); return d} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Byte rtn() {def d = Character.valueOf((char)1); return d} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Byte rtn() {def d = Integer.valueOf((int)1); return d} rtn()")); @@ -533,32 +551,32 @@ public void testReturnToByteBoxedCasts() { } public void testReturnToShortBoxedCasts() { - assertEquals((short)1, exec("Short rtn() {return (byte)1} rtn()")); - assertEquals((short)1, exec("Short rtn() {return (short)1} rtn()")); + assertEquals((short) 1, exec("Short rtn() {return (byte)1} rtn()")); + assertEquals((short) 1, exec("Short rtn() {return (short)1} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Short rtn() {return (char)1} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Short rtn() {return (int)1} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Short rtn() {return (long)1} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Short rtn() {return (float)1} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Short rtn() {return (double)1} rtn()")); - assertEquals((short)1, exec("Short rtn() {return Byte.valueOf((byte)1)} rtn()")); - assertEquals((short)1, exec("Short rtn() {return Short.valueOf((short)1)} rtn()")); + assertEquals((short) 1, exec("Short rtn() {return Byte.valueOf((byte)1)} rtn()")); + assertEquals((short) 1, exec("Short rtn() {return Short.valueOf((short)1)} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Short rtn() {return Character.valueOf((char)1)} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Short rtn() {return Integer.valueOf((int)1)} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Short rtn() {return Long.valueOf((long)1)} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Short rtn() {return Float.valueOf((float)1)} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Short rtn() {return Double.valueOf((double)1)} rtn()")); - assertEquals((short)1, exec("Short rtn() {def d = (byte)1; return d} rtn()")); - assertEquals((short)1, exec("Short rtn() {def d = (short)1; return d} rtn()")); + assertEquals((short) 1, exec("Short rtn() {def d = (byte)1; return d} rtn()")); + assertEquals((short) 1, exec("Short rtn() {def d = (short)1; return d} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Short rtn() {def d = (char)1; return d} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Short rtn() {def d = (int)1; return d} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Short rtn() {def d = (long)1; return d} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Short rtn() {def d = (float)1; return d} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Short rtn() {def d = (double)1; return d} rtn()")); - assertEquals((short)1, exec("Short rtn() {def d = Byte.valueOf((byte)1); return d} rtn()")); - assertEquals((short)1, exec("Short rtn() {def d = Short.valueOf((short)1); return d} rtn()")); + assertEquals((short) 1, exec("Short rtn() {def d = Byte.valueOf((byte)1); return d} rtn()")); + assertEquals((short) 1, exec("Short rtn() {def d = Short.valueOf((short)1); return d} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Short rtn() {def d = Character.valueOf((char)1); return d} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Short rtn() {def d = Integer.valueOf((int)1); return d} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Short rtn() {def d = Long.valueOf((long)1); return d} rtn()")); @@ -569,7 +587,7 @@ public void testReturnToShortBoxedCasts() { public void testReturnToCharacterBoxedCasts() { expectScriptThrows(ClassCastException.class, () -> exec("Character rtn() {return (byte)1} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Character rtn() {return (short)1} rtn()")); - assertEquals((char)1, exec("Character rtn() {return (char)1} rtn()")); + assertEquals((char) 1, exec("Character rtn() {return (char)1} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Character rtn() {return (int)1} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Character rtn() {return (long)1} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Character rtn() {return (float)1} rtn()")); @@ -577,7 +595,7 @@ public void testReturnToCharacterBoxedCasts() { expectScriptThrows(ClassCastException.class, () -> exec("Character rtn() {return Byte.valueOf((byte)1)} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Character rtn() {return Short.valueOf((short)1)} rtn()")); - assertEquals((char)1, exec("Character rtn() {return Character.valueOf((char)1)} rtn()")); + assertEquals((char) 1, exec("Character rtn() {return Character.valueOf((char)1)} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Character rtn() {return Integer.valueOf((int)1)} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Character rtn() {return Long.valueOf((long)1)} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Character rtn() {return Float.valueOf((float)1)} rtn()")); @@ -585,7 +603,7 @@ public void testReturnToCharacterBoxedCasts() { expectScriptThrows(ClassCastException.class, () -> exec("Character rtn() {def d = (byte)1; return d} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Character rtn() {def d = (short)1; return d} rtn()")); - assertEquals((char)1, exec("Character rtn() {def d = (char)1; return d} rtn()")); + assertEquals((char) 1, exec("Character rtn() {def d = (char)1; return d} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Character rtn() {def d = (int)1; return d} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Character rtn() {def d = (long)1; return d} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Character rtn() {def d = (float)1; return d} rtn()")); @@ -593,7 +611,7 @@ public void testReturnToCharacterBoxedCasts() { expectScriptThrows(ClassCastException.class, () -> exec("Character rtn() {def d = Byte.valueOf((byte)1); return d} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Character rtn() {def d = Short.valueOf((short)1); return d} rtn()")); - assertEquals((char)1, exec("Character rtn() {def d = Character.valueOf((char)1); return d} rtn()")); + assertEquals((char) 1, exec("Character rtn() {def d = Character.valueOf((char)1); return d} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Character rtn() {def d = Integer.valueOf((int)1); return d} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Character rtn() {def d = Long.valueOf((long)1); return d} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Character rtn() {def d = Float.valueOf((float)1); return d} rtn()")); @@ -635,104 +653,104 @@ public void testReturnToIntegerBoxedCasts() { } public void testReturnToLongBoxedCasts() { - assertEquals((long)1, exec("Long rtn() {return (byte)1} rtn()")); - assertEquals((long)1, exec("Long rtn() {return (short)1} rtn()")); - assertEquals((long)1, exec("Long rtn() {return (char)1} rtn()")); - assertEquals((long)1, exec("Long rtn() {return (int)1} rtn()")); - assertEquals((long)1, exec("Long rtn() {return (long)1} rtn()")); + assertEquals((long) 1, exec("Long rtn() {return (byte)1} rtn()")); + assertEquals((long) 1, exec("Long rtn() {return (short)1} rtn()")); + assertEquals((long) 1, exec("Long rtn() {return (char)1} rtn()")); + assertEquals((long) 1, exec("Long rtn() {return (int)1} rtn()")); + assertEquals((long) 1, exec("Long rtn() {return (long)1} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Long rtn() {return (float)1} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Long rtn() {return (double)1} rtn()")); - assertEquals((long)1, exec("Long rtn() {return Byte.valueOf((byte)1)} rtn()")); - assertEquals((long)1, exec("Long rtn() {return Short.valueOf((short)1)} rtn()")); - assertEquals((long)1, exec("Long rtn() {return Character.valueOf((char)1)} rtn()")); - assertEquals((long)1, exec("Long rtn() {return Integer.valueOf((int)1)} rtn()")); - assertEquals((long)1, exec("Long rtn() {return Long.valueOf((long)1)} rtn()")); + assertEquals((long) 1, exec("Long rtn() {return Byte.valueOf((byte)1)} rtn()")); + assertEquals((long) 1, exec("Long rtn() {return Short.valueOf((short)1)} rtn()")); + assertEquals((long) 1, exec("Long rtn() {return Character.valueOf((char)1)} rtn()")); + assertEquals((long) 1, exec("Long rtn() {return Integer.valueOf((int)1)} rtn()")); + assertEquals((long) 1, exec("Long rtn() {return Long.valueOf((long)1)} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Long rtn() {return Float.valueOf((float)1)} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Long rtn() {return Double.valueOf((double)1)} rtn()")); - assertEquals((long)1, exec("Long rtn() {def d = (byte)1; return d} rtn()")); - assertEquals((long)1, exec("Long rtn() {def d = (short)1; return d} rtn()")); - assertEquals((long)1, exec("Long rtn() {def d = (char)1; return d} rtn()")); - assertEquals((long)1, exec("Long rtn() {def d = (int)1; return d} rtn()")); - assertEquals((long)1, exec("Long rtn() {def d = (long)1; return d} rtn()")); + assertEquals((long) 1, exec("Long rtn() {def d = (byte)1; return d} rtn()")); + assertEquals((long) 1, exec("Long rtn() {def d = (short)1; return d} rtn()")); + assertEquals((long) 1, exec("Long rtn() {def d = (char)1; return d} rtn()")); + assertEquals((long) 1, exec("Long rtn() {def d = (int)1; return d} rtn()")); + assertEquals((long) 1, exec("Long rtn() {def d = (long)1; return d} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Long rtn() {def d = (float)1; return d} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Long rtn() {def d = (double)1; return d} rtn()")); - assertEquals((long)1, exec("Long rtn() {def d = Byte.valueOf((byte)1); return d} rtn()")); - assertEquals((long)1, exec("Long rtn() {def d = Short.valueOf((short)1); return d} rtn()")); - assertEquals((long)1, exec("Long rtn() {def d = Character.valueOf((char)1); return d} rtn()")); - assertEquals((long)1, exec("Long rtn() {def d = Integer.valueOf((int)1); return d} rtn()")); - assertEquals((long)1, exec("Long rtn() {def d = Long.valueOf((long)1); return d} rtn()")); + assertEquals((long) 1, exec("Long rtn() {def d = Byte.valueOf((byte)1); return d} rtn()")); + assertEquals((long) 1, exec("Long rtn() {def d = Short.valueOf((short)1); return d} rtn()")); + assertEquals((long) 1, exec("Long rtn() {def d = Character.valueOf((char)1); return d} rtn()")); + assertEquals((long) 1, exec("Long rtn() {def d = Integer.valueOf((int)1); return d} rtn()")); + assertEquals((long) 1, exec("Long rtn() {def d = Long.valueOf((long)1); return d} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Long rtn() {def d = Float.valueOf((float)1); return d} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Long rtn() {def d = Double.valueOf((double)1); return d} rtn()")); } public void testReturnToFloatBoxedCasts() { - assertEquals((float)1, exec("Float rtn() {return (byte)1} rtn()")); - assertEquals((float)1, exec("Float rtn() {return (short)1} rtn()")); - assertEquals((float)1, exec("Float rtn() {return (char)1} rtn()")); - assertEquals((float)1, exec("Float rtn() {return (int)1} rtn()")); - assertEquals((float)1, exec("Float rtn() {return (long)1} rtn()")); - assertEquals((float)1, exec("Float rtn() {return (float)1} rtn()")); + assertEquals((float) 1, exec("Float rtn() {return (byte)1} rtn()")); + assertEquals((float) 1, exec("Float rtn() {return (short)1} rtn()")); + assertEquals((float) 1, exec("Float rtn() {return (char)1} rtn()")); + assertEquals((float) 1, exec("Float rtn() {return (int)1} rtn()")); + assertEquals((float) 1, exec("Float rtn() {return (long)1} rtn()")); + assertEquals((float) 1, exec("Float rtn() {return (float)1} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Float rtn() {return (double)1} rtn()")); - assertEquals((float)1, exec("Float rtn() {return Byte.valueOf((byte)1)} rtn()")); - assertEquals((float)1, exec("Float rtn() {return Short.valueOf((short)1)} rtn()")); - assertEquals((float)1, exec("Float rtn() {return Character.valueOf((char)1)} rtn()")); - assertEquals((float)1, exec("Float rtn() {return Integer.valueOf((int)1)} rtn()")); - assertEquals((float)1, exec("Float rtn() {return Long.valueOf((long)1)} rtn()")); - assertEquals((float)1, exec("Float rtn() {return Float.valueOf((float)1)} rtn()")); + assertEquals((float) 1, exec("Float rtn() {return Byte.valueOf((byte)1)} rtn()")); + assertEquals((float) 1, exec("Float rtn() {return Short.valueOf((short)1)} rtn()")); + assertEquals((float) 1, exec("Float rtn() {return Character.valueOf((char)1)} rtn()")); + assertEquals((float) 1, exec("Float rtn() {return Integer.valueOf((int)1)} rtn()")); + assertEquals((float) 1, exec("Float rtn() {return Long.valueOf((long)1)} rtn()")); + assertEquals((float) 1, exec("Float rtn() {return Float.valueOf((float)1)} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Float rtn() {return Double.valueOf((double)1)} rtn()")); - assertEquals((float)1, exec("Float rtn() {def d = (byte)1; return d} rtn()")); - assertEquals((float)1, exec("Float rtn() {def d = (short)1; return d} rtn()")); - assertEquals((float)1, exec("Float rtn() {def d = (char)1; return d} rtn()")); - assertEquals((float)1, exec("Float rtn() {def d = (int)1; return d} rtn()")); - assertEquals((float)1, exec("Float rtn() {def d = (long)1; return d} rtn()")); - assertEquals((float)1, exec("Float rtn() {def d = (float)1; return d} rtn()")); + assertEquals((float) 1, exec("Float rtn() {def d = (byte)1; return d} rtn()")); + assertEquals((float) 1, exec("Float rtn() {def d = (short)1; return d} rtn()")); + assertEquals((float) 1, exec("Float rtn() {def d = (char)1; return d} rtn()")); + assertEquals((float) 1, exec("Float rtn() {def d = (int)1; return d} rtn()")); + assertEquals((float) 1, exec("Float rtn() {def d = (long)1; return d} rtn()")); + assertEquals((float) 1, exec("Float rtn() {def d = (float)1; return d} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Float rtn() {def d = (double)1; return d} rtn()")); - assertEquals((float)1, exec("Float rtn() {def d = Byte.valueOf((byte)1); return d} rtn()")); - assertEquals((float)1, exec("Float rtn() {def d = Short.valueOf((short)1); return d} rtn()")); - assertEquals((float)1, exec("Float rtn() {def d = Character.valueOf((char)1); return d} rtn()")); - assertEquals((float)1, exec("Float rtn() {def d = Integer.valueOf((int)1); return d} rtn()")); - assertEquals((float)1, exec("Float rtn() {def d = Long.valueOf((long)1); return d} rtn()")); - assertEquals((float)1, exec("Float rtn() {def d = Float.valueOf((float)1); return d} rtn()")); + assertEquals((float) 1, exec("Float rtn() {def d = Byte.valueOf((byte)1); return d} rtn()")); + assertEquals((float) 1, exec("Float rtn() {def d = Short.valueOf((short)1); return d} rtn()")); + assertEquals((float) 1, exec("Float rtn() {def d = Character.valueOf((char)1); return d} rtn()")); + assertEquals((float) 1, exec("Float rtn() {def d = Integer.valueOf((int)1); return d} rtn()")); + assertEquals((float) 1, exec("Float rtn() {def d = Long.valueOf((long)1); return d} rtn()")); + assertEquals((float) 1, exec("Float rtn() {def d = Float.valueOf((float)1); return d} rtn()")); expectScriptThrows(ClassCastException.class, () -> exec("Float rtn() {def d = Double.valueOf((double)1); return d} rtn()")); } public void testReturnToDoubleBoxedCasts() { - assertEquals((double)1, exec("Double rtn() {return (byte)1} rtn()")); - assertEquals((double)1, exec("Double rtn() {return (short)1} rtn()")); - assertEquals((double)1, exec("Double rtn() {return (char)1} rtn()")); - assertEquals((double)1, exec("Double rtn() {return (int)1} rtn()")); - assertEquals((double)1, exec("Double rtn() {return (long)1} rtn()")); - assertEquals((double)1, exec("Double rtn() {return (float)1} rtn()")); - assertEquals((double)1, exec("Double rtn() {return (double)1} rtn()")); - - assertEquals((double)1, exec("Double rtn() {return Byte.valueOf((byte)1)} rtn()")); - assertEquals((double)1, exec("Double rtn() {return Short.valueOf((short)1)} rtn()")); - assertEquals((double)1, exec("Double rtn() {return Character.valueOf((char)1)} rtn()")); - assertEquals((double)1, exec("Double rtn() {return Integer.valueOf((int)1)} rtn()")); - assertEquals((double)1, exec("Double rtn() {return Long.valueOf((long)1)} rtn()")); - assertEquals((double)1, exec("Double rtn() {return Float.valueOf((float)1)} rtn()")); - assertEquals((double)1, exec("Double rtn() {return Double.valueOf((double)1)} rtn()")); - - assertEquals((double)1, exec("Double rtn() {def d = (byte)1; return d} rtn()")); - assertEquals((double)1, exec("Double rtn() {def d = (short)1; return d} rtn()")); - assertEquals((double)1, exec("Double rtn() {def d = (char)1; return d} rtn()")); - assertEquals((double)1, exec("Double rtn() {def d = (int)1; return d} rtn()")); - assertEquals((double)1, exec("Double rtn() {def d = (long)1; return d} rtn()")); - assertEquals((double)1, exec("Double rtn() {def d = (float)1; return d} rtn()")); - assertEquals((double)1, exec("Double rtn() {def d = (double)1; return d} rtn()")); - - assertEquals((double)1, exec("Double rtn() {def d = Byte.valueOf((byte)1); return d} rtn()")); - assertEquals((double)1, exec("Double rtn() {def d = Short.valueOf((short)1); return d} rtn()")); - assertEquals((double)1, exec("Double rtn() {def d = Character.valueOf((char)1); return d} rtn()")); - assertEquals((double)1, exec("Double rtn() {def d = Integer.valueOf((int)1); return d} rtn()")); - assertEquals((double)1, exec("Double rtn() {def d = Long.valueOf((long)1); return d} rtn()")); - assertEquals((double)1, exec("Double rtn() {def d = Float.valueOf((float)1); return d} rtn()")); - assertEquals((double)1, exec("Double rtn() {def d = Double.valueOf((double)1); return d} rtn()")); + assertEquals((double) 1, exec("Double rtn() {return (byte)1} rtn()")); + assertEquals((double) 1, exec("Double rtn() {return (short)1} rtn()")); + assertEquals((double) 1, exec("Double rtn() {return (char)1} rtn()")); + assertEquals((double) 1, exec("Double rtn() {return (int)1} rtn()")); + assertEquals((double) 1, exec("Double rtn() {return (long)1} rtn()")); + assertEquals((double) 1, exec("Double rtn() {return (float)1} rtn()")); + assertEquals((double) 1, exec("Double rtn() {return (double)1} rtn()")); + + assertEquals((double) 1, exec("Double rtn() {return Byte.valueOf((byte)1)} rtn()")); + assertEquals((double) 1, exec("Double rtn() {return Short.valueOf((short)1)} rtn()")); + assertEquals((double) 1, exec("Double rtn() {return Character.valueOf((char)1)} rtn()")); + assertEquals((double) 1, exec("Double rtn() {return Integer.valueOf((int)1)} rtn()")); + assertEquals((double) 1, exec("Double rtn() {return Long.valueOf((long)1)} rtn()")); + assertEquals((double) 1, exec("Double rtn() {return Float.valueOf((float)1)} rtn()")); + assertEquals((double) 1, exec("Double rtn() {return Double.valueOf((double)1)} rtn()")); + + assertEquals((double) 1, exec("Double rtn() {def d = (byte)1; return d} rtn()")); + assertEquals((double) 1, exec("Double rtn() {def d = (short)1; return d} rtn()")); + assertEquals((double) 1, exec("Double rtn() {def d = (char)1; return d} rtn()")); + assertEquals((double) 1, exec("Double rtn() {def d = (int)1; return d} rtn()")); + assertEquals((double) 1, exec("Double rtn() {def d = (long)1; return d} rtn()")); + assertEquals((double) 1, exec("Double rtn() {def d = (float)1; return d} rtn()")); + assertEquals((double) 1, exec("Double rtn() {def d = (double)1; return d} rtn()")); + + assertEquals((double) 1, exec("Double rtn() {def d = Byte.valueOf((byte)1); return d} rtn()")); + assertEquals((double) 1, exec("Double rtn() {def d = Short.valueOf((short)1); return d} rtn()")); + assertEquals((double) 1, exec("Double rtn() {def d = Character.valueOf((char)1); return d} rtn()")); + assertEquals((double) 1, exec("Double rtn() {def d = Integer.valueOf((int)1); return d} rtn()")); + assertEquals((double) 1, exec("Double rtn() {def d = Long.valueOf((long)1); return d} rtn()")); + assertEquals((double) 1, exec("Double rtn() {def d = Float.valueOf((float)1); return d} rtn()")); + assertEquals((double) 1, exec("Double rtn() {def d = Double.valueOf((double)1); return d} rtn()")); } } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/BufferTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/BufferTests.java index 9a53b919170d5..d77e26120c2f7 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/BufferTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/BufferTests.java @@ -22,8 +22,8 @@ public class BufferTests extends ScriptTestCase { public void testByteBufferMethods() { - ByteBuffer bb = ByteBuffer.wrap(new byte[] {0, 1, 2, 3, 4, 5, 6, 7}); - Map params = Collections.singletonMap("bb", ByteBuffer.wrap(new byte[] {0, 1, 2, 3, 4, 5, 6, 7})); + ByteBuffer bb = ByteBuffer.wrap(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 }); + Map params = Collections.singletonMap("bb", ByteBuffer.wrap(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 })); assertEquals(bb.limit(), exec("def bb = params['bb']; bb.limit()", params, true)); assertEquals(bb.limit(), exec("ByteBuffer bb = params['bb']; bb.limit()", params, true)); @@ -31,10 +31,14 @@ public void testByteBufferMethods() { assertEquals(bb.order(), exec("def bb = params['bb']; bb.order()", params, true)); assertEquals(bb.order(), exec("ByteBuffer bb = params['bb']; bb.order()", params, true)); - assertEquals(bb.order(ByteOrder.LITTLE_ENDIAN).order(), - exec("def bb = params['bb']; bb.order(ByteOrder.LITTLE_ENDIAN).order()", params, true)); - assertEquals(bb.order(ByteOrder.LITTLE_ENDIAN).order(), - exec("ByteBuffer bb = params['bb']; bb.order(ByteOrder.LITTLE_ENDIAN).order()", params, true)); + assertEquals( + bb.order(ByteOrder.LITTLE_ENDIAN).order(), + exec("def bb = params['bb']; bb.order(ByteOrder.LITTLE_ENDIAN).order()", params, true) + ); + assertEquals( + bb.order(ByteOrder.LITTLE_ENDIAN).order(), + exec("ByteBuffer bb = params['bb']; bb.order(ByteOrder.LITTLE_ENDIAN).order()", params, true) + ); assertEquals(bb.get(0), exec("def bb = params['bb']; bb.get(0)", params, true)); assertEquals(bb.get(0), exec("ByteBuffer bb = params['bb']; bb.get(0)", params, true)); @@ -44,11 +48,11 @@ public void testByteBufferMethods() { assertEquals(bb.getChar(0), exec("def bb = params['bb']; bb.getChar(0)", params, true)); assertEquals(bb.getChar(0), exec("ByteBuffer bb = params['bb']; bb.getChar(0)", params, true)); - assertEquals(bb.getDouble(0), (double)exec("def bb = params['bb']; bb.getDouble(0)", params, true), 0.1); - assertEquals(bb.getDouble(0), (double)exec("ByteBuffer bb = params['bb']; bb.getDouble(0)", params, true), 0.1); + assertEquals(bb.getDouble(0), (double) exec("def bb = params['bb']; bb.getDouble(0)", params, true), 0.1); + assertEquals(bb.getDouble(0), (double) exec("ByteBuffer bb = params['bb']; bb.getDouble(0)", params, true), 0.1); - assertEquals(bb.getFloat(0), (float)exec("def bb = params['bb']; bb.getFloat(0)", params, true), 0.1); - assertEquals(bb.getFloat(0), (float)exec("ByteBuffer bb = params['bb']; bb.getFloat(0)", params, true), 0.1); + assertEquals(bb.getFloat(0), (float) exec("def bb = params['bb']; bb.getFloat(0)", params, true), 0.1); + assertEquals(bb.getFloat(0), (float) exec("ByteBuffer bb = params['bb']; bb.getFloat(0)", params, true), 0.1); assertEquals(bb.getInt(0), exec("def bb = params['bb']; bb.getInt(0)", params, true)); assertEquals(bb.getInt(0), exec("ByteBuffer bb = params['bb']; bb.getInt(0)", params, true)); @@ -77,12 +81,12 @@ public void testByteBufferMethods() { assertEquals(bb.asShortBuffer(), exec("def bb = params['bb']; bb.asShortBuffer()", params, true)); assertEquals(bb.asShortBuffer(), exec("ByteBuffer bb = params['bb']; bb.asShortBuffer()", params, true)); - assertEquals(ByteBuffer.wrap(new byte[] {1, 2, 3}), exec("ByteBuffer.wrap(new byte[] {1, 2, 3})")); - assertEquals(ByteBuffer.wrap(new byte[] {1, 2, 3}, 1, 2), exec("ByteBuffer.wrap(new byte[] {1, 2, 3}, 1, 2)")); + assertEquals(ByteBuffer.wrap(new byte[] { 1, 2, 3 }), exec("ByteBuffer.wrap(new byte[] {1, 2, 3})")); + assertEquals(ByteBuffer.wrap(new byte[] { 1, 2, 3 }, 1, 2), exec("ByteBuffer.wrap(new byte[] {1, 2, 3}, 1, 2)")); } public void testCharBufferMethods() { - CharBuffer bb = CharBuffer.wrap(new char[] {0, 1, 2, 3, 4, 5, 6, 7}); + CharBuffer bb = CharBuffer.wrap(new char[] { 0, 1, 2, 3, 4, 5, 6, 7 }); Map params = Collections.singletonMap("bb", bb); assertEquals(bb.limit(), exec("def bb = params['bb']; bb.limit()", params, true)); @@ -95,7 +99,7 @@ public void testCharBufferMethods() { } public void testDoubleBufferMethods() { - DoubleBuffer bb = DoubleBuffer.wrap(new double[] {0, 1, 2, 3, 4, 5, 6, 7}); + DoubleBuffer bb = DoubleBuffer.wrap(new double[] { 0, 1, 2, 3, 4, 5, 6, 7 }); Map params = Collections.singletonMap("bb", bb); assertEquals(bb.limit(), exec("def bb = params['bb']; bb.limit()", params, true)); @@ -108,7 +112,7 @@ public void testDoubleBufferMethods() { } public void testFloatBufferMethods() { - FloatBuffer bb = FloatBuffer.wrap(new float[] {0, 1, 2, 3, 4, 5, 6, 7}); + FloatBuffer bb = FloatBuffer.wrap(new float[] { 0, 1, 2, 3, 4, 5, 6, 7 }); Map params = Collections.singletonMap("bb", bb); assertEquals(bb.limit(), exec("def bb = params['bb']; bb.limit()", params, true)); @@ -121,7 +125,7 @@ public void testFloatBufferMethods() { } public void testIntBufferMethods() { - IntBuffer bb = IntBuffer.wrap(new int[] {0, 1, 2, 3, 4, 5, 6, 7}); + IntBuffer bb = IntBuffer.wrap(new int[] { 0, 1, 2, 3, 4, 5, 6, 7 }); Map params = Collections.singletonMap("bb", bb); assertEquals(bb.limit(), exec("def bb = params['bb']; bb.limit()", params, true)); @@ -134,7 +138,7 @@ public void testIntBufferMethods() { } public void testLongBufferMethods() { - LongBuffer bb = LongBuffer.wrap(new long[] {0, 1, 2, 3, 4, 5, 6, 7}); + LongBuffer bb = LongBuffer.wrap(new long[] { 0, 1, 2, 3, 4, 5, 6, 7 }); Map params = Collections.singletonMap("bb", bb); assertEquals(bb.limit(), exec("def bb = params['bb']; bb.limit()", params, true)); @@ -147,7 +151,7 @@ public void testLongBufferMethods() { } public void testShortBufferMethods() { - ShortBuffer bb = ShortBuffer.wrap(new short[] {0, 1, 2, 3, 4, 5, 6, 7}); + ShortBuffer bb = ShortBuffer.wrap(new short[] { 0, 1, 2, 3, 4, 5, 6, 7 }); Map params = Collections.singletonMap("bb", bb); assertEquals(bb.limit(), exec("def bb = params['bb']; bb.limit()", params, true)); diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/CidrTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/CidrTests.java index 5fbb603368976..75cd05c932b31 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/CidrTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/CidrTests.java @@ -13,7 +13,6 @@ public void testContains() { Object bool = exec("CIDR c = new CIDR('10.1.1.0/23'); c.contains('10.1.1.128') && c.contains('10.1.0.255')"); assertEquals(Boolean.TRUE, bool); - bool = exec("CIDR c = new CIDR('10.1.1.0/25'); c.contains('10.1.1.127')"); assertEquals(Boolean.TRUE, bool); @@ -44,7 +43,8 @@ public void testInvalidIPs() { e = expectScriptThrows(IllegalArgumentException.class, () -> exec("new CIDR('2001:0db8:85a3:0000:0000:8a2e:0370:733g')")); assertEquals("'2001:0db8:85a3:0000:0000:8a2e:0370:733g' is not an IP string literal.", e.getMessage()); - e = expectScriptThrows(IllegalArgumentException.class, + e = expectScriptThrows( + IllegalArgumentException.class, () -> exec("new CIDR('2001:0db8:85a3::/64').contains('2001:0db8:85a3:0000:0000:8a2g:0370:7334')") ); assertEquals("'2001:0db8:85a3:0000:0000:8a2g:0370:7334' is not an IP string literal.", e.getMessage()); diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/ComparisonTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/ComparisonTests.java index fefb6fc8f8b5a..89d1872e1ccc4 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/ComparisonTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/ComparisonTests.java @@ -136,10 +136,10 @@ public void testDefNe() { assertEquals(false, exec("def x = new HashMap(); x.put(3, 3); def y = new HashMap(); y.put(3, 3); return x != y")); assertEquals(false, exec("def x = new HashMap(); def y = x; x.put(3, 3); y.put(3, 3); return x != y")); - assertEquals(false, exec("def x = true; def y = true; return x != y")); - assertEquals(true, exec("def x = true; def y = false; return x != y")); - assertEquals(true, exec("def x = false; def y = true; return x != y")); - assertEquals(false, exec("def x = false; def y = false; return x != y")); + assertEquals(false, exec("def x = true; def y = true; return x != y")); + assertEquals(true, exec("def x = true; def y = false; return x != y")); + assertEquals(true, exec("def x = false; def y = true; return x != y")); + assertEquals(false, exec("def x = false; def y = false; return x != y")); } public void testDefNeTypedLHS() { @@ -164,10 +164,10 @@ public void testDefNeTypedLHS() { assertEquals(false, exec("Map x = new HashMap(); x.put(3, 3); def y = new HashMap(); y.put(3, 3); return x != y")); assertEquals(false, exec("Map x = new HashMap(); def y = x; x.put(3, 3); y.put(3, 3); return x != y")); - assertEquals(false, exec("boolean x = true; def y = true; return x != y")); - assertEquals(true, exec("boolean x = true; def y = false; return x != y")); - assertEquals(true, exec("boolean x = false; def y = true; return x != y")); - assertEquals(false, exec("boolean x = false; def y = false; return x != y")); + assertEquals(false, exec("boolean x = true; def y = true; return x != y")); + assertEquals(true, exec("boolean x = true; def y = false; return x != y")); + assertEquals(true, exec("boolean x = false; def y = true; return x != y")); + assertEquals(false, exec("boolean x = false; def y = false; return x != y")); } public void testDefNeTypedRHS() { @@ -192,10 +192,10 @@ public void testDefNeTypedRHS() { assertEquals(false, exec("def x = new HashMap(); x.put(3, 3); Map y = new HashMap(); y.put(3, 3); return x != y")); assertEquals(false, exec("def x = new HashMap(); Map y = x; x.put(3, 3); y.put(3, 3); return x != y")); - assertEquals(false, exec("def x = true; boolean y = true; return x != y")); - assertEquals(true, exec("def x = true; boolean y = false; return x != y")); - assertEquals(true, exec("def x = false; boolean y = true; return x != y")); - assertEquals(false, exec("def x = false; boolean y = false; return x != y")); + assertEquals(false, exec("def x = true; boolean y = true; return x != y")); + assertEquals(true, exec("def x = true; boolean y = false; return x != y")); + assertEquals(true, exec("def x = false; boolean y = true; return x != y")); + assertEquals(false, exec("def x = false; boolean y = false; return x != y")); } public void testDefNer() { diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/ConditionalTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/ConditionalTests.java index 5a158c539aaa4..0e995599eeeec 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/ConditionalTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/ConditionalTests.java @@ -45,50 +45,54 @@ public void testPrecedence() { public void testAssignment() { assertEquals(4D, exec("boolean x = false; double z = x ? 2 : 4.0F; return z;")); - assertEquals((byte)7, exec("boolean x = false; int y = 2; byte z = x ? (byte)y : 7; return z;")); - assertEquals((byte)7, exec("boolean x = false; int y = 2; byte z = (byte)(x ? y : 7); return z;")); + assertEquals((byte) 7, exec("boolean x = false; int y = 2; byte z = x ? (byte)y : 7; return z;")); + assertEquals((byte) 7, exec("boolean x = false; int y = 2; byte z = (byte)(x ? y : 7); return z;")); assertEquals(ArrayList.class, exec("boolean x = false; Object z = x ? new HashMap() : new ArrayList(); return z;").getClass()); } public void testNullArguments() { assertEquals(null, exec("boolean b = false, c = true; Object x; Map y; return b && c ? x : y;")); - assertEquals(HashMap.class, - exec("boolean b = false, c = true; Object x; Map y = new HashMap(); return b && c ? x : y;").getClass()); + assertEquals( + HashMap.class, + exec("boolean b = false, c = true; Object x; Map y = new HashMap(); return b && c ? x : y;").getClass() + ); } public void testPromotion() { assertEquals(false, exec("boolean x = false; boolean y = true; return (x ? 2 : 4.0F) == (y ? 2 : 4.0F);")); - assertEquals(false, exec("boolean x = false; boolean y = true; " + - "return (x ? new HashMap() : new ArrayList()) == (y ? new HashMap() : new ArrayList());")); + assertEquals( + false, + exec( + "boolean x = false; boolean y = true; " + + "return (x ? new HashMap() : new ArrayList()) == (y ? new HashMap() : new ArrayList());" + ) + ); } public void testIncompatibleAssignment() { - expectScriptThrows(ClassCastException.class, () -> { - exec("boolean x = false; byte z = x ? 2 : 4.0F; return z;"); - }); + expectScriptThrows(ClassCastException.class, () -> { exec("boolean x = false; byte z = x ? 2 : 4.0F; return z;"); }); - expectScriptThrows(ClassCastException.class, () -> { - exec("boolean x = false; Map z = x ? 4 : (byte)7; return z;"); - }); + expectScriptThrows(ClassCastException.class, () -> { exec("boolean x = false; Map z = x ? 4 : (byte)7; return z;"); }); - expectScriptThrows(ClassCastException.class, () -> { - exec("boolean x = false; Map z = x ? new HashMap() : new ArrayList(); return z;"); - }); + expectScriptThrows( + ClassCastException.class, + () -> { exec("boolean x = false; Map z = x ? new HashMap() : new ArrayList(); return z;"); } + ); - expectScriptThrows(ClassCastException.class, () -> { - exec("boolean x = false; int y = 2; byte z = x ? y : 7; return z;"); - }); + expectScriptThrows(ClassCastException.class, () -> { exec("boolean x = false; int y = 2; byte z = x ? y : 7; return z;"); }); } public void testNested() { for (int i = 0; i < 100; i++) { String scriptPart = IntStream.range(0, i).mapToObj(j -> "field == '" + j + "' ? '" + j + "' :").collect(joining("\n")); - assertEquals("z", exec("def field = params.a;\n" + - "\n" + - "return (\n" + - scriptPart + - "field == '' ? 'unknown' :\n" + - "field);", Map.of("a", "z"), true)); + assertEquals( + "z", + exec( + "def field = params.a;\n" + "\n" + "return (\n" + scriptPart + "field == '' ? 'unknown' :\n" + "field);", + Map.of("a", "z"), + true + ) + ); } } } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/ConstantFoldingTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/ConstantFoldingTests.java index 0f3e92a01e1ad..b2556368fb38b 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/ConstantFoldingTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/ConstantFoldingTests.java @@ -107,27 +107,27 @@ public void testCast() { assertBytecodeExists("4L<5F", "ICONST_1"); } - public void testStoreInMap() { + public void testStoreInMap() { assertBytecodeExists("Map m = [:]; m.a = 1 + 1; m.a", "ICONST_2"); } - public void testStoreInMapDef() { + public void testStoreInMapDef() { assertBytecodeExists("def m = [:]; m.a = 1 + 1; m.a", "ICONST_2"); } - public void testStoreInList() { + public void testStoreInList() { assertBytecodeExists("List l = [null]; l.0 = 1 + 1; l.0", "ICONST_2"); } - public void testStoreInListDef() { + public void testStoreInListDef() { assertBytecodeExists("def l = [null]; l.0 = 1 + 1; l.0", "ICONST_2"); } - public void testStoreInArray() { + public void testStoreInArray() { assertBytecodeExists("int[] a = new int[1]; a[0] = 1 + 1; a[0]", "ICONST_2"); } - public void testStoreInArrayDef() { + public void testStoreInArrayDef() { assertBytecodeExists("def a = new int[1]; a[0] = 1 + 1; a[0]", "ICONST_2"); } } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/ContextExampleTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/ContextExampleTests.java index 2798180fbb5f7..49d03220295b0 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/ContextExampleTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/ContextExampleTests.java @@ -1,5 +1,4 @@ - /* * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one * or more contributor license agreements. Licensed under the Elastic License @@ -247,32 +246,34 @@ public static void main(String args[]) throws IOException { */ public void testIngestProcessorScript() { - assertEquals(1535785200000L, - exec("def x = ['date': '2018-9-1', 'time': '3:00 PM'];" + - "String[] dateSplit = x.date.splitOnToken('-');" + - "String year = dateSplit[0].trim();" + - "String month = dateSplit[1].trim();" + - "if (month.length() == 1) {" + - " month = '0' + month;" + - "}" + - "String day = dateSplit[2].trim();" + - "if (day.length() == 1) {" + - " day = '0' + day;" + - "}" + - "boolean pm = x.time.substring(x.time.length() - 2).equals('PM');" + - "String[] timeSplit = x.time.substring(0, x.time.length() - 2).splitOnToken(':');" + - "int hours = Integer.parseInt(timeSplit[0].trim());" + - "int minutes = Integer.parseInt(timeSplit[1].trim());" + - "if (pm) {" + - " hours += 12;" + - "}" + - "String dts = year + '-' + month + '-' + day + 'T' +" + - " (hours < 10 ? '0' + hours : '' + hours) + ':' +" + - " (minutes < 10 ? '0' + minutes : '' + minutes) +" + - " ':00+08:00';" + - "ZonedDateTime dt = ZonedDateTime.parse(" + - " dts, DateTimeFormatter.ISO_OFFSET_DATE_TIME);" + - "return dt.getLong(ChronoField.INSTANT_SECONDS) * 1000L" + assertEquals( + 1535785200000L, + exec( + "def x = ['date': '2018-9-1', 'time': '3:00 PM'];" + + "String[] dateSplit = x.date.splitOnToken('-');" + + "String year = dateSplit[0].trim();" + + "String month = dateSplit[1].trim();" + + "if (month.length() == 1) {" + + " month = '0' + month;" + + "}" + + "String day = dateSplit[2].trim();" + + "if (day.length() == 1) {" + + " day = '0' + day;" + + "}" + + "boolean pm = x.time.substring(x.time.length() - 2).equals('PM');" + + "String[] timeSplit = x.time.substring(0, x.time.length() - 2).splitOnToken(':');" + + "int hours = Integer.parseInt(timeSplit[0].trim());" + + "int minutes = Integer.parseInt(timeSplit[1].trim());" + + "if (pm) {" + + " hours += 12;" + + "}" + + "String dts = year + '-' + month + '-' + day + 'T' +" + + " (hours < 10 ? '0' + hours : '' + hours) + ':' +" + + " (minutes < 10 ? '0' + minutes : '' + minutes) +" + + " ':00+08:00';" + + "ZonedDateTime dt = ZonedDateTime.parse(" + + " dts, DateTimeFormatter.ISO_OFFSET_DATE_TIME);" + + "return dt.getLong(ChronoField.INSTANT_SECONDS) * 1000L" ) ); } @@ -285,7 +286,6 @@ public void testIngestProcessorScript() { */ - // Use script_fields API to add two extra fields to the hits /* @@ -309,7 +309,6 @@ public void testIngestProcessorScript() { } */ - // Testing only params, as I am not sure how to test Script Doc Values in painless public void testScriptFieldsScript() { Map hit = new HashMap<>(); @@ -318,16 +317,20 @@ public void testScriptFieldsScript() { hit.put("fields", fields); Map source = new HashMap<>(); - String[] actors = {"James Holland", "Krissy Smith", "Joe Muir", "Ryan Earns"}; + String[] actors = { "James Holland", "Krissy Smith", "Joe Muir", "Ryan Earns" }; source.put("actors", actors); - assertEquals(hit, exec( - "Map fields = new HashMap();" + - "fields[\"number-of-actors\"] = params['_source']['actors'].length;" + - "Map rtn = new HashMap();" + - "rtn[\"fields\"] = fields;" + - "return rtn;", - singletonMap("_source", source), true) + assertEquals( + hit, + exec( + "Map fields = new HashMap();" + + "fields[\"number-of-actors\"] = params['_source']['actors'].length;" + + "Map rtn = new HashMap();" + + "rtn[\"fields\"] = fields;" + + "return rtn;", + singletonMap("_source", source), + true + ) ); } @@ -361,13 +364,10 @@ public void testFilterScript() { params.put("_source", source); params.put("cost", 18); - boolean result = (boolean) exec( - " params['_source']['sold'] == false && params['_source']['cost'] < params.cost;", - params, true); + boolean result = (boolean) exec(" params['_source']['sold'] == false && params['_source']['cost'] < params.cost;", params, true); assertTrue(result); } - // Use script_fields API to add two extra fields to the hits /* curl -X GET localhost:9200/seats/_search @@ -396,4 +396,3 @@ public void testMinShouldMatchScript() { assertEquals(2, result, 0); } } - diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/DateTimeTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/DateTimeTests.java index 70d22cf0f4036..fdb00e114c95a 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/DateTimeTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/DateTimeTests.java @@ -14,170 +14,215 @@ public class DateTimeTests extends ScriptTestCase { public void testLongToZonedDateTime() { - assertEquals(ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of("Z")), exec( - "long milliSinceEpoch = 434931330000L;" + - "Instant instant = Instant.ofEpochMilli(milliSinceEpoch);" + - "return ZonedDateTime.ofInstant(instant, ZoneId.of('Z'));" - )); + assertEquals( + ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of("Z")), + exec( + "long milliSinceEpoch = 434931330000L;" + + "Instant instant = Instant.ofEpochMilli(milliSinceEpoch);" + + "return ZonedDateTime.ofInstant(instant, ZoneId.of('Z'));" + ) + ); } public void testStringToZonedDateTime() { - assertEquals(ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of("Z")), exec( - "String milliSinceEpochString = '434931330000';" + - "long milliSinceEpoch = Long.parseLong(milliSinceEpochString);" + - "Instant instant = Instant.ofEpochMilli(milliSinceEpoch);" + - "return ZonedDateTime.ofInstant(instant, ZoneId.of('Z'));" - )); - - assertEquals(ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of("Z")), exec( - "String datetime = '1983-10-13T22:15:30Z';" + - "return ZonedDateTime.parse(datetime);" - )); - - assertEquals(ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of("Z")), exec( - "String datetime = 'Thu, 13 Oct 1983 22:15:30 GMT';" + - "return ZonedDateTime.parse(datetime, DateTimeFormatter.RFC_1123_DATE_TIME);" - )); - - assertEquals(ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of("Z")), exec( - "String datetime = 'custom y 1983 m 10 d 13 22:15:30 Z';" + - "DateTimeFormatter dtf = DateTimeFormatter.ofPattern(" + - "\"'custom' 'y' yyyy 'm' MM 'd' dd HH:mm:ss VV\");" + - "return ZonedDateTime.parse(datetime, dtf);" - )); + assertEquals( + ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of("Z")), + exec( + "String milliSinceEpochString = '434931330000';" + + "long milliSinceEpoch = Long.parseLong(milliSinceEpochString);" + + "Instant instant = Instant.ofEpochMilli(milliSinceEpoch);" + + "return ZonedDateTime.ofInstant(instant, ZoneId.of('Z'));" + ) + ); + + assertEquals( + ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of("Z")), + exec("String datetime = '1983-10-13T22:15:30Z';" + "return ZonedDateTime.parse(datetime);") + ); + + assertEquals( + ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of("Z")), + exec( + "String datetime = 'Thu, 13 Oct 1983 22:15:30 GMT';" + + "return ZonedDateTime.parse(datetime, DateTimeFormatter.RFC_1123_DATE_TIME);" + ) + ); + + assertEquals( + ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of("Z")), + exec( + "String datetime = 'custom y 1983 m 10 d 13 22:15:30 Z';" + + "DateTimeFormatter dtf = DateTimeFormatter.ofPattern(" + + "\"'custom' 'y' yyyy 'm' MM 'd' dd HH:mm:ss VV\");" + + "return ZonedDateTime.parse(datetime, dtf);" + ) + ); } public void testPiecesToZonedDateTime() { - assertEquals(ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of("Z")), exec( - "int year = 1983;" + - "int month = 10;" + - "int day = 13;" + - "int hour = 22;" + - "int minutes = 15;" + - "int seconds = 30;" + - "int nanos = 0;" + - "String tz = 'Z';" + - "return ZonedDateTime.of(year, month, day, hour, minutes, seconds, nanos, ZoneId.of(tz));" - )); + assertEquals( + ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of("Z")), + exec( + "int year = 1983;" + + "int month = 10;" + + "int day = 13;" + + "int hour = 22;" + + "int minutes = 15;" + + "int seconds = 30;" + + "int nanos = 0;" + + "String tz = 'Z';" + + "return ZonedDateTime.of(year, month, day, hour, minutes, seconds, nanos, ZoneId.of(tz));" + ) + ); } public void testZonedDatetimeToLong() { - assertEquals(434931330000L, exec( - "ZonedDateTime zdt = ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of('Z'));" + - "return zdt.toInstant().toEpochMilli();" - )); + assertEquals( + 434931330000L, + exec( + "ZonedDateTime zdt = ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of('Z'));" + + "return zdt.toInstant().toEpochMilli();" + ) + ); } public void testZonedDateTimeToString() { - assertEquals("1983-10-13T22:15:30Z", exec( - "ZonedDateTime zdt = ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of('Z'));" + - "return zdt.format(DateTimeFormatter.ISO_INSTANT);" - )); - - assertEquals("date: 1983/10/13 time: 22:15:30", exec( - "ZonedDateTime zdt = ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of('Z'));" + - "DateTimeFormatter dtf = DateTimeFormatter.ofPattern(" + - "\"'date:' yyyy/MM/dd 'time:' HH:mm:ss\");" + - "return zdt.format(dtf);" - )); + assertEquals( + "1983-10-13T22:15:30Z", + exec( + "ZonedDateTime zdt = ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of('Z'));" + + "return zdt.format(DateTimeFormatter.ISO_INSTANT);" + ) + ); + + assertEquals( + "date: 1983/10/13 time: 22:15:30", + exec( + "ZonedDateTime zdt = ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of('Z'));" + + "DateTimeFormatter dtf = DateTimeFormatter.ofPattern(" + + "\"'date:' yyyy/MM/dd 'time:' HH:mm:ss\");" + + "return zdt.format(dtf);" + ) + ); } public void testZonedDateTimeToPieces() { - assertArrayEquals(new int[] {1983, 10, 13, 22, 15, 30, 100}, (int[])exec( - "int[] pieces = new int[7];" + - "ZonedDateTime zdt = ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 100, ZoneId.of('Z'));" + - "pieces[0] = zdt.year;" + - "pieces[1] = zdt.monthValue;" + - "pieces[2] = zdt.dayOfMonth;" + - "pieces[3] = zdt.hour;" + - "pieces[4] = zdt.minute;" + - "pieces[5] = zdt.second;" + - "pieces[6] = zdt.nano;" + - "return pieces;" - )); + assertArrayEquals( + new int[] { 1983, 10, 13, 22, 15, 30, 100 }, + (int[]) exec( + "int[] pieces = new int[7];" + + "ZonedDateTime zdt = ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 100, ZoneId.of('Z'));" + + "pieces[0] = zdt.year;" + + "pieces[1] = zdt.monthValue;" + + "pieces[2] = zdt.dayOfMonth;" + + "pieces[3] = zdt.hour;" + + "pieces[4] = zdt.minute;" + + "pieces[5] = zdt.second;" + + "pieces[6] = zdt.nano;" + + "return pieces;" + ) + ); } public void testLongManipulation() { - assertEquals(ZonedDateTime.of(1983, 10, 13, 22, 15, 27, 0, ZoneId.of("Z")), exec( - "long milliSinceEpoch = 434931330000L;" + - "milliSinceEpoch = milliSinceEpoch - 1000L*3L;" + - "Instant instant = Instant.ofEpochMilli(milliSinceEpoch);" + - "return ZonedDateTime.ofInstant(instant, ZoneId.of('Z'))" - )); + assertEquals( + ZonedDateTime.of(1983, 10, 13, 22, 15, 27, 0, ZoneId.of("Z")), + exec( + "long milliSinceEpoch = 434931330000L;" + + "milliSinceEpoch = milliSinceEpoch - 1000L*3L;" + + "Instant instant = Instant.ofEpochMilli(milliSinceEpoch);" + + "return ZonedDateTime.ofInstant(instant, ZoneId.of('Z'))" + ) + ); } public void testZonedDateTimeManipulation() { - assertEquals(ZonedDateTime.of(1983, 10, 16, 22, 15, 30, 0, ZoneId.of("Z")), exec( - "ZonedDateTime zdt = ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of('Z'));" + - "return zdt.plusDays(3);" - )); - - assertEquals(ZonedDateTime.of(1983, 10, 13, 20, 10, 30, 0, ZoneId.of("Z")), exec( - "ZonedDateTime zdt = ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of('Z'));" + - "return zdt.minusMinutes(125);" - )); - - assertEquals(ZonedDateTime.of(1976, 10, 13, 22, 15, 30, 0, ZoneId.of("Z")), exec( - "ZonedDateTime zdt = ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of('Z'));" + - "return zdt.withYear(1976);" - )); + assertEquals( + ZonedDateTime.of(1983, 10, 16, 22, 15, 30, 0, ZoneId.of("Z")), + exec("ZonedDateTime zdt = ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of('Z'));" + "return zdt.plusDays(3);") + ); + + assertEquals( + ZonedDateTime.of(1983, 10, 13, 20, 10, 30, 0, ZoneId.of("Z")), + exec("ZonedDateTime zdt = ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of('Z'));" + "return zdt.minusMinutes(125);") + ); + + assertEquals( + ZonedDateTime.of(1976, 10, 13, 22, 15, 30, 0, ZoneId.of("Z")), + exec("ZonedDateTime zdt = ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of('Z'));" + "return zdt.withYear(1976);") + ); } public void testLongTimeDifference() { - assertEquals(3000L, exec( - "long startTimestamp = 434931327000L;" + - "long endTimestamp = 434931330000L;" + - "return endTimestamp - startTimestamp;" - )); + assertEquals( + 3000L, + exec("long startTimestamp = 434931327000L;" + "long endTimestamp = 434931330000L;" + "return endTimestamp - startTimestamp;") + ); } public void testZonedDateTimeDifference() { - assertEquals(4989L, exec( - "ZonedDateTime zdt1 = ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 11000000, ZoneId.of('Z'));" + - "ZonedDateTime zdt2 = ZonedDateTime.of(1983, 10, 13, 22, 15, 35, 0, ZoneId.of('Z'));" + - "return ChronoUnit.MILLIS.between(zdt1, zdt2);" - )); - - assertEquals(4L, exec( - "ZonedDateTime zdt1 = ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 11000000, ZoneId.of('Z'));" + - "ZonedDateTime zdt2 = ZonedDateTime.of(1983, 10, 17, 22, 15, 35, 0, ZoneId.of('Z'));" + - "return ChronoUnit.DAYS.between(zdt1, zdt2);" - )); + assertEquals( + 4989L, + exec( + "ZonedDateTime zdt1 = ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 11000000, ZoneId.of('Z'));" + + "ZonedDateTime zdt2 = ZonedDateTime.of(1983, 10, 13, 22, 15, 35, 0, ZoneId.of('Z'));" + + "return ChronoUnit.MILLIS.between(zdt1, zdt2);" + ) + ); + + assertEquals( + 4L, + exec( + "ZonedDateTime zdt1 = ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 11000000, ZoneId.of('Z'));" + + "ZonedDateTime zdt2 = ZonedDateTime.of(1983, 10, 17, 22, 15, 35, 0, ZoneId.of('Z'));" + + "return ChronoUnit.DAYS.between(zdt1, zdt2);" + ) + ); } public void compareLongs() { - assertEquals(false, exec( - "long ts1 = 434931327000L;" + - "long ts2 = 434931330000L;" + - "return ts1 > ts2;" - )); + assertEquals(false, exec("long ts1 = 434931327000L;" + "long ts2 = 434931330000L;" + "return ts1 > ts2;")); } public void compareZonedDateTimes() { - assertEquals(true, exec( - "ZonedDateTime zdt1 = ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of('Z'));" + - "ZonedDateTime zdt2 = ZonedDateTime.of(1983, 10, 17, 22, 15, 35, 0, ZoneId.of('Z'));" + - "return zdt1.isBefore(zdt2);" - )); - - assertEquals(false, exec( - "ZonedDateTime zdt1 = ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of('Z'));" + - "ZonedDateTime zdt2 = ZonedDateTime.of(1983, 10, 17, 22, 15, 35, 0, ZoneId.of('Z'));" + - "return zdt1.isAfter(zdt2);" - )); + assertEquals( + true, + exec( + "ZonedDateTime zdt1 = ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of('Z'));" + + "ZonedDateTime zdt2 = ZonedDateTime.of(1983, 10, 17, 22, 15, 35, 0, ZoneId.of('Z'));" + + "return zdt1.isBefore(zdt2);" + ) + ); + + assertEquals( + false, + exec( + "ZonedDateTime zdt1 = ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of('Z'));" + + "ZonedDateTime zdt2 = ZonedDateTime.of(1983, 10, 17, 22, 15, 35, 0, ZoneId.of('Z'));" + + "return zdt1.isAfter(zdt2);" + ) + ); } public void testTimeZone() { - assertEquals(ZonedDateTime.of(1983, 10, 13, 15, 15, 30, 0, ZoneId.of("America/Los_Angeles")), exec( - "ZonedDateTime utc = ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of('Z'));" + - "return utc.withZoneSameInstant(ZoneId.of('America/Los_Angeles'));")); - - assertEquals("Thu, 13 Oct 1983 15:15:30 -0700", exec( - "String gmtString = 'Thu, 13 Oct 1983 22:15:30 GMT';" + - "ZonedDateTime gmtZdt = ZonedDateTime.parse(gmtString," + - "DateTimeFormatter.RFC_1123_DATE_TIME);" + - "ZonedDateTime pstZdt =" + - "gmtZdt.withZoneSameInstant(ZoneId.of('America/Los_Angeles'));" + - "return pstZdt.format(DateTimeFormatter.RFC_1123_DATE_TIME);")); + assertEquals( + ZonedDateTime.of(1983, 10, 13, 15, 15, 30, 0, ZoneId.of("America/Los_Angeles")), + exec( + "ZonedDateTime utc = ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of('Z'));" + + "return utc.withZoneSameInstant(ZoneId.of('America/Los_Angeles'));" + ) + ); + + assertEquals( + "Thu, 13 Oct 1983 15:15:30 -0700", + exec( + "String gmtString = 'Thu, 13 Oct 1983 22:15:30 GMT';" + + "ZonedDateTime gmtZdt = ZonedDateTime.parse(gmtString," + + "DateTimeFormatter.RFC_1123_DATE_TIME);" + + "ZonedDateTime pstZdt =" + + "gmtZdt.withZoneSameInstant(ZoneId.of('America/Los_Angeles'));" + + "return pstZdt.format(DateTimeFormatter.RFC_1123_DATE_TIME);" + ) + ); } } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/DebugTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/DebugTests.java index 509b1a11dc359..c1516e9323e7d 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/DebugTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/DebugTests.java @@ -30,8 +30,10 @@ public class DebugTests extends ScriptTestCase { public void testExplain() { // Debug.explain can explain an object Object dummy = new Object(); - PainlessExplainError e = expectScriptThrows(PainlessExplainError.class, () -> exec( - "Debug.explain(params.a)", singletonMap("a", dummy), true)); + PainlessExplainError e = expectScriptThrows( + PainlessExplainError.class, + () -> exec("Debug.explain(params.a)", singletonMap("a", dummy), true) + ); assertSame(dummy, e.getObjectToExplain()); assertThat(e.getHeaders(painlessLookup), hasEntry("es.to_string", singletonList(dummy.toString()))); assertThat(e.getHeaders(painlessLookup), hasEntry("es.java_class", singletonList("java.lang.Object"))); @@ -45,12 +47,14 @@ public void testExplain() { assertThat(e.getHeaders(painlessLookup), not(hasKey("es.painless_class"))); // You can't catch the explain exception - e = expectScriptThrows(PainlessExplainError.class, () -> exec( - "try {\n" - + " Debug.explain(params.a)\n" - + "} catch (Exception e) {\n" - + " return 1\n" - + "}", singletonMap("a", dummy), true)); + e = expectScriptThrows( + PainlessExplainError.class, + () -> exec( + "try {\n" + " Debug.explain(params.a)\n" + "} catch (Exception e) {\n" + " return 1\n" + "}", + singletonMap("a", dummy), + true + ) + ); assertSame(dummy, e.getObjectToExplain()); } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/Debugger.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/Debugger.java index 16775df059cc9..d20ac3cf31bea 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/Debugger.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/Debugger.java @@ -35,8 +35,12 @@ static String toString(Class iface, String source, CompilerSettings settings, PrintWriter outputWriter = new PrintWriter(output); Textifier textifier = new Textifier(); try { - new Compiler(iface, null, null, PainlessLookupBuilder.buildFromWhitelists(whitelists)) - .compile("", source, settings, textifier); + new Compiler(iface, null, null, PainlessLookupBuilder.buildFromWhitelists(whitelists)).compile( + "", + source, + settings, + textifier + ); } catch (RuntimeException e) { textifier.print(outputWriter); e.addSuppressed(new Exception("current bytecode: \n" + output)); @@ -48,15 +52,28 @@ static String toString(Class iface, String source, CompilerSettings settings, } /** compiles to bytecode, and returns debugging output */ - private static String tree(Class iface, String source, CompilerSettings settings, List whitelists, - UserTreeVisitor semanticPhaseVisitor, UserTreeVisitor irPhaseVisitor, - IRTreeVisitor asmPhaseVisitor) { + private static String tree( + Class iface, + String source, + CompilerSettings settings, + List whitelists, + UserTreeVisitor semanticPhaseVisitor, + UserTreeVisitor irPhaseVisitor, + IRTreeVisitor asmPhaseVisitor + ) { StringWriter output = new StringWriter(); PrintWriter outputWriter = new PrintWriter(output); Textifier textifier = new Textifier(); try { - new Compiler(iface, null, null, PainlessLookupBuilder.buildFromWhitelists(whitelists)) - .compile("", source, settings, textifier, semanticPhaseVisitor, irPhaseVisitor, asmPhaseVisitor); + new Compiler(iface, null, null, PainlessLookupBuilder.buildFromWhitelists(whitelists)).compile( + "", + source, + settings, + textifier, + semanticPhaseVisitor, + irPhaseVisitor, + asmPhaseVisitor + ); } catch (RuntimeException e) { textifier.print(outputWriter); e.addSuppressed(new Exception("current bytecode: \n" + output)); @@ -67,9 +84,20 @@ private static String tree(Class iface, String source, CompilerSettings setti return output.toString(); } - static void phases(final String source, UserTreeVisitor semanticPhaseVisitor, UserTreeVisitor irPhaseVisitor, - IRTreeVisitor asmPhaseVisitor) { - tree(PainlessTestScript.class, source, new CompilerSettings(), PainlessPlugin.BASE_WHITELISTS, semanticPhaseVisitor, irPhaseVisitor, - asmPhaseVisitor); + static void phases( + final String source, + UserTreeVisitor semanticPhaseVisitor, + UserTreeVisitor irPhaseVisitor, + IRTreeVisitor asmPhaseVisitor + ) { + tree( + PainlessTestScript.class, + source, + new CompilerSettings(), + PainlessPlugin.BASE_WHITELISTS, + semanticPhaseVisitor, + irPhaseVisitor, + asmPhaseVisitor + ); } } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/DefBootstrapTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/DefBootstrapTests.java index bbb9f380d1b0c..5717aea33fd89 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/DefBootstrapTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/DefBootstrapTests.java @@ -26,174 +26,197 @@ public class DefBootstrapTests extends ESTestCase { /** calls toString() on integers, twice */ public void testOneType() throws Throwable { - CallSite site = DefBootstrap.bootstrap(painlessLookup, - new FunctionTable(), - Collections.emptyMap(), - MethodHandles.publicLookup(), - "toString", - MethodType.methodType(String.class, Object.class), - 0, - DefBootstrap.METHOD_CALL, - ""); + CallSite site = DefBootstrap.bootstrap( + painlessLookup, + new FunctionTable(), + Collections.emptyMap(), + MethodHandles.publicLookup(), + "toString", + MethodType.methodType(String.class, Object.class), + 0, + DefBootstrap.METHOD_CALL, + "" + ); MethodHandle handle = site.dynamicInvoker(); assertDepthEquals(site, 0); // invoke with integer, needs lookup - assertEquals("5", (String)handle.invokeExact((Object)5)); + assertEquals("5", (String) handle.invokeExact((Object) 5)); assertDepthEquals(site, 1); // invoked with integer again: should be cached - assertEquals("6", (String)handle.invokeExact((Object)6)); + assertEquals("6", (String) handle.invokeExact((Object) 6)); assertDepthEquals(site, 1); } public void testTwoTypes() throws Throwable { - CallSite site = DefBootstrap.bootstrap(painlessLookup, - new FunctionTable(), - Collections.emptyMap(), - MethodHandles.publicLookup(), - "toString", - MethodType.methodType(String.class, Object.class), - 0, - DefBootstrap.METHOD_CALL, - ""); + CallSite site = DefBootstrap.bootstrap( + painlessLookup, + new FunctionTable(), + Collections.emptyMap(), + MethodHandles.publicLookup(), + "toString", + MethodType.methodType(String.class, Object.class), + 0, + DefBootstrap.METHOD_CALL, + "" + ); MethodHandle handle = site.dynamicInvoker(); assertDepthEquals(site, 0); - assertEquals("5", (String)handle.invokeExact((Object)5)); + assertEquals("5", (String) handle.invokeExact((Object) 5)); assertDepthEquals(site, 1); - assertEquals("1.5", (String)handle.invokeExact((Object)1.5f)); + assertEquals("1.5", (String) handle.invokeExact((Object) 1.5f)); assertDepthEquals(site, 2); // both these should be cached - assertEquals("6", (String)handle.invokeExact((Object)6)); + assertEquals("6", (String) handle.invokeExact((Object) 6)); assertDepthEquals(site, 2); - assertEquals("2.5", (String)handle.invokeExact((Object)2.5f)); + assertEquals("2.5", (String) handle.invokeExact((Object) 2.5f)); assertDepthEquals(site, 2); } public void testTooManyTypes() throws Throwable { // if this changes, test must be rewritten assertEquals(5, DefBootstrap.PIC.MAX_DEPTH); - CallSite site = DefBootstrap.bootstrap(painlessLookup, - new FunctionTable(), - Collections.emptyMap(), - MethodHandles.publicLookup(), - "toString", - MethodType.methodType(String.class, Object.class), - 0, - DefBootstrap.METHOD_CALL, - ""); + CallSite site = DefBootstrap.bootstrap( + painlessLookup, + new FunctionTable(), + Collections.emptyMap(), + MethodHandles.publicLookup(), + "toString", + MethodType.methodType(String.class, Object.class), + 0, + DefBootstrap.METHOD_CALL, + "" + ); MethodHandle handle = site.dynamicInvoker(); assertDepthEquals(site, 0); - assertEquals("5", (String)handle.invokeExact((Object)5)); + assertEquals("5", (String) handle.invokeExact((Object) 5)); assertDepthEquals(site, 1); - assertEquals("1.5", (String)handle.invokeExact((Object)1.5f)); + assertEquals("1.5", (String) handle.invokeExact((Object) 1.5f)); assertDepthEquals(site, 2); - assertEquals("6", (String)handle.invokeExact((Object)6L)); + assertEquals("6", (String) handle.invokeExact((Object) 6L)); assertDepthEquals(site, 3); - assertEquals("3.2", (String)handle.invokeExact((Object)3.2d)); + assertEquals("3.2", (String) handle.invokeExact((Object) 3.2d)); assertDepthEquals(site, 4); - assertEquals("foo", (String)handle.invokeExact((Object)"foo")); + assertEquals("foo", (String) handle.invokeExact((Object) "foo")); assertDepthEquals(site, 5); - assertEquals("c", (String)handle.invokeExact((Object)'c')); + assertEquals("c", (String) handle.invokeExact((Object) 'c')); assertDepthEquals(site, 5); } /** test that we revert to the megamorphic classvalue cache and that it works as expected */ public void testMegamorphic() throws Throwable { - DefBootstrap.PIC site = (DefBootstrap.PIC) DefBootstrap.bootstrap(painlessLookup, - new FunctionTable(), - Collections.emptyMap(), - MethodHandles.publicLookup(), - "size", - MethodType.methodType(int.class, Object.class), - 0, - DefBootstrap.METHOD_CALL, - ""); + DefBootstrap.PIC site = (DefBootstrap.PIC) DefBootstrap.bootstrap( + painlessLookup, + new FunctionTable(), + Collections.emptyMap(), + MethodHandles.publicLookup(), + "size", + MethodType.methodType(int.class, Object.class), + 0, + DefBootstrap.METHOD_CALL, + "" + ); site.depth = DefBootstrap.PIC.MAX_DEPTH; // mark megamorphic MethodHandle handle = site.dynamicInvoker(); - assertEquals(2, (int)handle.invokeExact((Object) Arrays.asList("1", "2"))); - assertEquals(1, (int)handle.invokeExact((Object) Collections.singletonMap("a", "b"))); - assertEquals(3, (int)handle.invokeExact((Object) Arrays.asList("x", "y", "z"))); - assertEquals(2, (int)handle.invokeExact((Object) Arrays.asList("u", "v"))); + assertEquals(2, (int) handle.invokeExact((Object) Arrays.asList("1", "2"))); + assertEquals(1, (int) handle.invokeExact((Object) Collections.singletonMap("a", "b"))); + assertEquals(3, (int) handle.invokeExact((Object) Arrays.asList("x", "y", "z"))); + assertEquals(2, (int) handle.invokeExact((Object) Arrays.asList("u", "v"))); - final HashMap map = new HashMap<>(); + final HashMap map = new HashMap<>(); map.put("x", "y"); map.put("a", "b"); - assertEquals(2, (int)handle.invokeExact((Object) map)); + assertEquals(2, (int) handle.invokeExact((Object) map)); - final IllegalArgumentException iae = expectThrows(IllegalArgumentException.class, () -> { - Integer.toString((int)handle.invokeExact(new Object())); - }); + final IllegalArgumentException iae = expectThrows( + IllegalArgumentException.class, + () -> { Integer.toString((int) handle.invokeExact(new Object())); } + ); assertEquals("dynamic method [java.lang.Object, size/0] not found", iae.getMessage()); - assertTrue("Does not fail inside ClassValue.computeValue()", Arrays.stream(iae.getStackTrace()).anyMatch(e -> { - return e.getMethodName().equals("computeValue") && - e.getClassName().startsWith("org.elasticsearch.painless.DefBootstrap$PIC$"); - })); + assertTrue( + "Does not fail inside ClassValue.computeValue()", + Arrays.stream(iae.getStackTrace()) + .anyMatch( + e -> { + return e.getMethodName().equals("computeValue") + && e.getClassName().startsWith("org.elasticsearch.painless.DefBootstrap$PIC$"); + } + ) + ); } // test operators with null guards public void testNullGuardAdd() throws Throwable { - DefBootstrap.MIC site = (DefBootstrap.MIC) DefBootstrap.bootstrap(painlessLookup, - new FunctionTable(), - Collections.emptyMap(), - MethodHandles.publicLookup(), - "add", - MethodType.methodType(Object.class, Object.class, Object.class), - 0, - DefBootstrap.BINARY_OPERATOR, - DefBootstrap.OPERATOR_ALLOWS_NULL); + DefBootstrap.MIC site = (DefBootstrap.MIC) DefBootstrap.bootstrap( + painlessLookup, + new FunctionTable(), + Collections.emptyMap(), + MethodHandles.publicLookup(), + "add", + MethodType.methodType(Object.class, Object.class, Object.class), + 0, + DefBootstrap.BINARY_OPERATOR, + DefBootstrap.OPERATOR_ALLOWS_NULL + ); MethodHandle handle = site.dynamicInvoker(); - assertEquals("nulltest", (Object)handle.invokeExact((Object)null, (Object)"test")); + assertEquals("nulltest", (Object) handle.invokeExact((Object) null, (Object) "test")); } public void testNullGuardAddWhenCached() throws Throwable { - DefBootstrap.MIC site = (DefBootstrap.MIC) DefBootstrap.bootstrap(painlessLookup, - new FunctionTable(), - Collections.emptyMap(), - MethodHandles.publicLookup(), - "add", - MethodType.methodType(Object.class, Object.class, Object.class), - 0, - DefBootstrap.BINARY_OPERATOR, - DefBootstrap.OPERATOR_ALLOWS_NULL); + DefBootstrap.MIC site = (DefBootstrap.MIC) DefBootstrap.bootstrap( + painlessLookup, + new FunctionTable(), + Collections.emptyMap(), + MethodHandles.publicLookup(), + "add", + MethodType.methodType(Object.class, Object.class, Object.class), + 0, + DefBootstrap.BINARY_OPERATOR, + DefBootstrap.OPERATOR_ALLOWS_NULL + ); MethodHandle handle = site.dynamicInvoker(); - assertEquals(2, (Object)handle.invokeExact((Object)1, (Object)1)); - assertEquals("nulltest", (Object)handle.invokeExact((Object)null, (Object)"test")); + assertEquals(2, (Object) handle.invokeExact((Object) 1, (Object) 1)); + assertEquals("nulltest", (Object) handle.invokeExact((Object) null, (Object) "test")); } public void testNullGuardEq() throws Throwable { - DefBootstrap.MIC site = (DefBootstrap.MIC) DefBootstrap.bootstrap(painlessLookup, - new FunctionTable(), - Collections.emptyMap(), - MethodHandles.publicLookup(), - "eq", - MethodType.methodType(boolean.class, Object.class, Object.class), - 0, - DefBootstrap.BINARY_OPERATOR, - DefBootstrap.OPERATOR_ALLOWS_NULL); + DefBootstrap.MIC site = (DefBootstrap.MIC) DefBootstrap.bootstrap( + painlessLookup, + new FunctionTable(), + Collections.emptyMap(), + MethodHandles.publicLookup(), + "eq", + MethodType.methodType(boolean.class, Object.class, Object.class), + 0, + DefBootstrap.BINARY_OPERATOR, + DefBootstrap.OPERATOR_ALLOWS_NULL + ); MethodHandle handle = site.dynamicInvoker(); - assertFalse((boolean) handle.invokeExact((Object)null, (Object)"test")); - assertTrue((boolean) handle.invokeExact((Object)null, (Object)null)); + assertFalse((boolean) handle.invokeExact((Object) null, (Object) "test")); + assertTrue((boolean) handle.invokeExact((Object) null, (Object) null)); } public void testNullGuardEqWhenCached() throws Throwable { - DefBootstrap.MIC site = (DefBootstrap.MIC) DefBootstrap.bootstrap(painlessLookup, - new FunctionTable(), - Collections.emptyMap(), - MethodHandles.publicLookup(), - "eq", - MethodType.methodType(boolean.class, Object.class, Object.class), - 0, - DefBootstrap.BINARY_OPERATOR, - DefBootstrap.OPERATOR_ALLOWS_NULL); + DefBootstrap.MIC site = (DefBootstrap.MIC) DefBootstrap.bootstrap( + painlessLookup, + new FunctionTable(), + Collections.emptyMap(), + MethodHandles.publicLookup(), + "eq", + MethodType.methodType(boolean.class, Object.class, Object.class), + 0, + DefBootstrap.BINARY_OPERATOR, + DefBootstrap.OPERATOR_ALLOWS_NULL + ); MethodHandle handle = site.dynamicInvoker(); - assertTrue((boolean) handle.invokeExact((Object)1, (Object)1)); - assertFalse((boolean) handle.invokeExact((Object)null, (Object)"test")); - assertTrue((boolean) handle.invokeExact((Object)null, (Object)null)); + assertTrue((boolean) handle.invokeExact((Object) 1, (Object) 1)); + assertFalse((boolean) handle.invokeExact((Object) null, (Object) "test")); + assertTrue((boolean) handle.invokeExact((Object) null, (Object) null)); } // make sure these operators work without null guards too @@ -201,36 +224,36 @@ public void testNullGuardEqWhenCached() throws Throwable { // and can be disabled in some circumstances. public void testNoNullGuardAdd() throws Throwable { - DefBootstrap.MIC site = (DefBootstrap.MIC) DefBootstrap.bootstrap(painlessLookup, - new FunctionTable(), - Collections.emptyMap(), - MethodHandles.publicLookup(), - "add", - MethodType.methodType(Object.class, int.class, Object.class), - 0, - DefBootstrap.BINARY_OPERATOR, - 0); + DefBootstrap.MIC site = (DefBootstrap.MIC) DefBootstrap.bootstrap( + painlessLookup, + new FunctionTable(), + Collections.emptyMap(), + MethodHandles.publicLookup(), + "add", + MethodType.methodType(Object.class, int.class, Object.class), + 0, + DefBootstrap.BINARY_OPERATOR, + 0 + ); MethodHandle handle = site.dynamicInvoker(); - expectThrows(NullPointerException.class, () -> { - assertNotNull((Object)handle.invokeExact(5, (Object)null)); - }); + expectThrows(NullPointerException.class, () -> { assertNotNull((Object) handle.invokeExact(5, (Object) null)); }); } public void testNoNullGuardAddWhenCached() throws Throwable { - DefBootstrap.MIC site = (DefBootstrap.MIC) DefBootstrap.bootstrap(painlessLookup, - new FunctionTable(), - Collections.emptyMap(), - MethodHandles.publicLookup(), - "add", - MethodType.methodType(Object.class, int.class, Object.class), - 0, - DefBootstrap.BINARY_OPERATOR, - 0); + DefBootstrap.MIC site = (DefBootstrap.MIC) DefBootstrap.bootstrap( + painlessLookup, + new FunctionTable(), + Collections.emptyMap(), + MethodHandles.publicLookup(), + "add", + MethodType.methodType(Object.class, int.class, Object.class), + 0, + DefBootstrap.BINARY_OPERATOR, + 0 + ); MethodHandle handle = site.dynamicInvoker(); - assertEquals(2, (Object)handle.invokeExact(1, (Object)1)); - expectThrows(NullPointerException.class, () -> { - assertNotNull((Object)handle.invokeExact(5, (Object)null)); - }); + assertEquals(2, (Object) handle.invokeExact(1, (Object) 1)); + expectThrows(NullPointerException.class, () -> { assertNotNull((Object) handle.invokeExact(5, (Object) null)); }); } static void assertDepthEquals(CallSite site, int expected) { diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/DefCastTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/DefCastTests.java index d0dd246fd8689..1400ed95d0b32 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/DefCastTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/DefCastTests.java @@ -34,7 +34,7 @@ public void testdefTobooleanImplicit() { public void testdefTobyteImplicit() { expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; byte b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = true; byte b = d;")); - assertEquals((byte)0, exec("def d = (byte)0; byte b = d; b")); + assertEquals((byte) 0, exec("def d = (byte)0; byte b = d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = (short)0; byte b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = (char)0; byte b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = (int)0; byte b = d;")); @@ -42,7 +42,7 @@ public void testdefTobyteImplicit() { expectScriptThrows(ClassCastException.class, () -> exec("def d = (float)0; byte b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = (double)0; byte b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); byte b = d;")); - assertEquals((byte)0, exec("def d = Byte.valueOf(0); byte b = d; b")); + assertEquals((byte) 0, exec("def d = Byte.valueOf(0); byte b = d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Short.valueOf(0); byte b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Character.valueOf(0); byte b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Integer.valueOf(0); byte b = d;")); @@ -55,16 +55,16 @@ public void testdefTobyteImplicit() { public void testdefToshortImplicit() { expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; short b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = true; short b = d;")); - assertEquals((short)0, exec("def d = (byte)0; short b = d; b")); - assertEquals((short)0, exec("def d = (short)0; short b = d; b")); + assertEquals((short) 0, exec("def d = (byte)0; short b = d; b")); + assertEquals((short) 0, exec("def d = (short)0; short b = d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = (char)0; short b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = (int)0; short b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = (long)0; short b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = (float)0; short b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = (double)0; short b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); short b = d;")); - assertEquals((short)0, exec("def d = Byte.valueOf(0); short b = d; b")); - assertEquals((short)0, exec("def d = Short.valueOf(0); short b = d; b")); + assertEquals((short) 0, exec("def d = Byte.valueOf(0); short b = d; b")); + assertEquals((short) 0, exec("def d = Short.valueOf(0); short b = d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Character.valueOf(0); short b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Integer.valueOf(0); short b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Long.valueOf(0); short b = d;")); @@ -79,7 +79,7 @@ public void testdefTocharImplicit() { expectScriptThrows(ClassCastException.class, () -> exec("def d = true; char b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = (byte)0; char b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = (short)0; char b = d;")); - assertEquals((char)0, exec("def d = (char)0; char b = d; b")); + assertEquals((char) 0, exec("def d = (char)0; char b = d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = (int)0; char b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = (long)0; char b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = (float)0; char b = d;")); @@ -87,7 +87,7 @@ public void testdefTocharImplicit() { expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); char b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Byte.valueOf(0); char b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Short.valueOf(0); char b = d;")); - assertEquals((char)0, exec("def d = Character.valueOf(0); char b = d; b")); + assertEquals((char) 0, exec("def d = Character.valueOf(0); char b = d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Integer.valueOf(0); char b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Long.valueOf(0); char b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Float.valueOf(0); char b = d;")); @@ -119,19 +119,19 @@ public void testdefTointImplicit() { public void testdefTolongImplicit() { expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; long b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = true; long b = d;")); - assertEquals((long)0, exec("def d = (byte)0; long b = d; b")); - assertEquals((long)0, exec("def d = (short)0; long b = d; b")); - assertEquals((long)0, exec("def d = (char)0; long b = d; b")); - assertEquals((long)0, exec("def d = 0; long b = d; b")); - assertEquals((long)0, exec("def d = (long)0; long b = d; b")); + assertEquals((long) 0, exec("def d = (byte)0; long b = d; b")); + assertEquals((long) 0, exec("def d = (short)0; long b = d; b")); + assertEquals((long) 0, exec("def d = (char)0; long b = d; b")); + assertEquals((long) 0, exec("def d = 0; long b = d; b")); + assertEquals((long) 0, exec("def d = (long)0; long b = d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = (float)0; long b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = (double)0; long b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); long b = d;")); - assertEquals((long)0, exec("def d = Byte.valueOf(0); long b = d; b")); - assertEquals((long)0, exec("def d = Short.valueOf(0); long b = d; b")); - assertEquals((long)0, exec("def d = Character.valueOf(0); long b = d; b")); - assertEquals((long)0, exec("def d = Integer.valueOf(0); long b = d; b")); - assertEquals((long)0, exec("def d = Long.valueOf(0); long b = d; b")); + assertEquals((long) 0, exec("def d = Byte.valueOf(0); long b = d; b")); + assertEquals((long) 0, exec("def d = Short.valueOf(0); long b = d; b")); + assertEquals((long) 0, exec("def d = Character.valueOf(0); long b = d; b")); + assertEquals((long) 0, exec("def d = Integer.valueOf(0); long b = d; b")); + assertEquals((long) 0, exec("def d = Long.valueOf(0); long b = d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Float.valueOf(0); long b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Double.valueOf(0); long b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); long b = d;")); @@ -140,21 +140,21 @@ public void testdefTolongImplicit() { public void testdefTodoubleImplicit() { expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; double b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = true; double b = d;")); - assertEquals((double)0, exec("def d = (byte)0; double b = d; b")); - assertEquals((double)0, exec("def d = (short)0; double b = d; b")); - assertEquals((double)0, exec("def d = (char)0; double b = d; b")); - assertEquals((double)0, exec("def d = 0; double b = d; b")); - assertEquals((double)0, exec("def d = (long)0; double b = d; b")); - assertEquals((double)0, exec("def d = (float)0; double b = d; b")); - assertEquals((double)0, exec("def d = (double)0; double b = d; b")); + assertEquals((double) 0, exec("def d = (byte)0; double b = d; b")); + assertEquals((double) 0, exec("def d = (short)0; double b = d; b")); + assertEquals((double) 0, exec("def d = (char)0; double b = d; b")); + assertEquals((double) 0, exec("def d = 0; double b = d; b")); + assertEquals((double) 0, exec("def d = (long)0; double b = d; b")); + assertEquals((double) 0, exec("def d = (float)0; double b = d; b")); + assertEquals((double) 0, exec("def d = (double)0; double b = d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); double b = d;")); - assertEquals((double)0, exec("def d = Byte.valueOf(0); double b = d; b")); - assertEquals((double)0, exec("def d = Short.valueOf(0); double b = d; b")); - assertEquals((double)0, exec("def d = Character.valueOf(0); double b = d; b")); - assertEquals((double)0, exec("def d = Integer.valueOf(0); double b = d; b")); - assertEquals((double)0, exec("def d = Long.valueOf(0); double b = d; b")); - assertEquals((double)0, exec("def d = Float.valueOf(0); double b = d; b")); - assertEquals((double)0, exec("def d = Double.valueOf(0); double b = d; b")); + assertEquals((double) 0, exec("def d = Byte.valueOf(0); double b = d; b")); + assertEquals((double) 0, exec("def d = Short.valueOf(0); double b = d; b")); + assertEquals((double) 0, exec("def d = Character.valueOf(0); double b = d; b")); + assertEquals((double) 0, exec("def d = Integer.valueOf(0); double b = d; b")); + assertEquals((double) 0, exec("def d = Long.valueOf(0); double b = d; b")); + assertEquals((double) 0, exec("def d = Float.valueOf(0); double b = d; b")); + assertEquals((double) 0, exec("def d = Double.valueOf(0); double b = d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); double b = d;")); } @@ -182,42 +182,42 @@ public void testdefTobooleanExplicit() { public void testdefTobyteExplicit() { expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; byte b = (byte)d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = true; byte b = (byte)d;")); - assertEquals((byte)0, exec("def d = (byte)0; byte b = (byte)d; b")); - assertEquals((byte)0, exec("def d = (short)0; byte b = (byte)d; b")); - assertEquals((byte)0, exec("def d = (char)0; byte b = (byte)d; b")); - assertEquals((byte)0, exec("def d = 0; byte b = (byte)d; b")); - assertEquals((byte)0, exec("def d = (long)0; byte b = (byte)d; b")); - assertEquals((byte)0, exec("def d = (float)0; byte b = (byte)d; b")); - assertEquals((byte)0, exec("def d = (double)0; byte b = (byte)d; b")); + assertEquals((byte) 0, exec("def d = (byte)0; byte b = (byte)d; b")); + assertEquals((byte) 0, exec("def d = (short)0; byte b = (byte)d; b")); + assertEquals((byte) 0, exec("def d = (char)0; byte b = (byte)d; b")); + assertEquals((byte) 0, exec("def d = 0; byte b = (byte)d; b")); + assertEquals((byte) 0, exec("def d = (long)0; byte b = (byte)d; b")); + assertEquals((byte) 0, exec("def d = (float)0; byte b = (byte)d; b")); + assertEquals((byte) 0, exec("def d = (double)0; byte b = (byte)d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); byte b = d;")); - assertEquals((byte)0, exec("def d = Byte.valueOf(0); byte b = (byte)d; b")); - assertEquals((byte)0, exec("def d = Short.valueOf(0); byte b = (byte)d; b")); - assertEquals((byte)0, exec("def d = Character.valueOf(0); byte b = (byte)d; b")); - assertEquals((byte)0, exec("def d = Integer.valueOf(0); byte b = (byte)d; b")); - assertEquals((byte)0, exec("def d = Long.valueOf(0); byte b = (byte)d; b")); - assertEquals((byte)0, exec("def d = Float.valueOf(0); byte b = (byte)d; b")); - assertEquals((byte)0, exec("def d = Double.valueOf(0); byte b = (byte)d; b")); + assertEquals((byte) 0, exec("def d = Byte.valueOf(0); byte b = (byte)d; b")); + assertEquals((byte) 0, exec("def d = Short.valueOf(0); byte b = (byte)d; b")); + assertEquals((byte) 0, exec("def d = Character.valueOf(0); byte b = (byte)d; b")); + assertEquals((byte) 0, exec("def d = Integer.valueOf(0); byte b = (byte)d; b")); + assertEquals((byte) 0, exec("def d = Long.valueOf(0); byte b = (byte)d; b")); + assertEquals((byte) 0, exec("def d = Float.valueOf(0); byte b = (byte)d; b")); + assertEquals((byte) 0, exec("def d = Double.valueOf(0); byte b = (byte)d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); byte b = (byte)d;")); } public void testdefToshortExplicit() { expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; short b = (short)d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = true; short b = (short)d;")); - assertEquals((short)0, exec("def d = (byte)0; short b = (short)d; b")); - assertEquals((short)0, exec("def d = (short)0; short b = (short)d; b")); - assertEquals((short)0, exec("def d = (char)0; short b = (short)d; b")); - assertEquals((short)0, exec("def d = 0; short b = (short)d; b")); - assertEquals((short)0, exec("def d = (long)0; short b = (short)d; b")); - assertEquals((short)0, exec("def d = (float)0; short b = (short)d; b")); - assertEquals((short)0, exec("def d = (double)0; short b = (short)d; b")); + assertEquals((short) 0, exec("def d = (byte)0; short b = (short)d; b")); + assertEquals((short) 0, exec("def d = (short)0; short b = (short)d; b")); + assertEquals((short) 0, exec("def d = (char)0; short b = (short)d; b")); + assertEquals((short) 0, exec("def d = 0; short b = (short)d; b")); + assertEquals((short) 0, exec("def d = (long)0; short b = (short)d; b")); + assertEquals((short) 0, exec("def d = (float)0; short b = (short)d; b")); + assertEquals((short) 0, exec("def d = (double)0; short b = (short)d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); short b = d;")); - assertEquals((short)0, exec("def d = Byte.valueOf(0); short b = (short)d; b")); - assertEquals((short)0, exec("def d = Short.valueOf(0); short b = (short)d; b")); - assertEquals((short)0, exec("def d = Character.valueOf(0); short b = (short)d; b")); - assertEquals((short)0, exec("def d = Integer.valueOf(0); short b = (short)d; b")); - assertEquals((short)0, exec("def d = Long.valueOf(0); short b = (short)d; b")); - assertEquals((short)0, exec("def d = Float.valueOf(0); short b = (short)d; b")); - assertEquals((short)0, exec("def d = Double.valueOf(0); short b = (short)d; b")); + assertEquals((short) 0, exec("def d = Byte.valueOf(0); short b = (short)d; b")); + assertEquals((short) 0, exec("def d = Short.valueOf(0); short b = (short)d; b")); + assertEquals((short) 0, exec("def d = Character.valueOf(0); short b = (short)d; b")); + assertEquals((short) 0, exec("def d = Integer.valueOf(0); short b = (short)d; b")); + assertEquals((short) 0, exec("def d = Long.valueOf(0); short b = (short)d; b")); + assertEquals((short) 0, exec("def d = Float.valueOf(0); short b = (short)d; b")); + assertEquals((short) 0, exec("def d = Double.valueOf(0); short b = (short)d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); short b = (short)d;")); } @@ -225,21 +225,21 @@ public void testdefTocharExplicit() { assertEquals('s', exec("def d = 's'; char b = (char)d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; char b = (char)d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = true; char b = (char)d;")); - assertEquals((char)0, exec("def d = (byte)0; char b = (char)d; b")); - assertEquals((char)0, exec("def d = (short)0; char b = (char)d; b")); - assertEquals((char)0, exec("def d = (char)0; char b = (char)d; b")); - assertEquals((char)0, exec("def d = 0; char b = (char)d; b")); - assertEquals((char)0, exec("def d = (long)0; char b = (char)d; b")); - assertEquals((char)0, exec("def d = (float)0; char b = (char)d; b")); - assertEquals((char)0, exec("def d = (double)0; char b = (char)d; b")); + assertEquals((char) 0, exec("def d = (byte)0; char b = (char)d; b")); + assertEquals((char) 0, exec("def d = (short)0; char b = (char)d; b")); + assertEquals((char) 0, exec("def d = (char)0; char b = (char)d; b")); + assertEquals((char) 0, exec("def d = 0; char b = (char)d; b")); + assertEquals((char) 0, exec("def d = (long)0; char b = (char)d; b")); + assertEquals((char) 0, exec("def d = (float)0; char b = (char)d; b")); + assertEquals((char) 0, exec("def d = (double)0; char b = (char)d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); char b = d;")); - assertEquals((char)0, exec("def d = Byte.valueOf(0); char b = (char)d; b")); - assertEquals((char)0, exec("def d = Short.valueOf(0); char b = (char)d; b")); - assertEquals((char)0, exec("def d = Character.valueOf(0); char b = (char)d; b")); - assertEquals((char)0, exec("def d = Integer.valueOf(0); char b = (char)d; b")); - assertEquals((char)0, exec("def d = Long.valueOf(0); char b = (char)d; b")); - assertEquals((char)0, exec("def d = Float.valueOf(0); char b = (char)d; b")); - assertEquals((char)0, exec("def d = Double.valueOf(0); char b = (char)d; b")); + assertEquals((char) 0, exec("def d = Byte.valueOf(0); char b = (char)d; b")); + assertEquals((char) 0, exec("def d = Short.valueOf(0); char b = (char)d; b")); + assertEquals((char) 0, exec("def d = Character.valueOf(0); char b = (char)d; b")); + assertEquals((char) 0, exec("def d = Integer.valueOf(0); char b = (char)d; b")); + assertEquals((char) 0, exec("def d = Long.valueOf(0); char b = (char)d; b")); + assertEquals((char) 0, exec("def d = Float.valueOf(0); char b = (char)d; b")); + assertEquals((char) 0, exec("def d = Double.valueOf(0); char b = (char)d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); char b = (char)d;")); } @@ -267,63 +267,63 @@ public void testdefTointExplicit() { public void testdefTolongExplicit() { expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; long b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = true; long b = (long)d;")); - assertEquals((long)0, exec("def d = (byte)0; long b = (long)d; b")); - assertEquals((long)0, exec("def d = (short)0; long b = (long)d; b")); - assertEquals((long)0, exec("def d = (char)0; long b = (long)d; b")); - assertEquals((long)0, exec("def d = 0; long b = (long)d; b")); - assertEquals((long)0, exec("def d = (long)0; long b = (long)d; b")); - assertEquals((long)0, exec("def d = (float)0; long b = (long)d; b")); - assertEquals((long)0, exec("def d = (double)0; long b = (long)d; b")); + assertEquals((long) 0, exec("def d = (byte)0; long b = (long)d; b")); + assertEquals((long) 0, exec("def d = (short)0; long b = (long)d; b")); + assertEquals((long) 0, exec("def d = (char)0; long b = (long)d; b")); + assertEquals((long) 0, exec("def d = 0; long b = (long)d; b")); + assertEquals((long) 0, exec("def d = (long)0; long b = (long)d; b")); + assertEquals((long) 0, exec("def d = (float)0; long b = (long)d; b")); + assertEquals((long) 0, exec("def d = (double)0; long b = (long)d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); long b = d;")); - assertEquals((long)0, exec("def d = Byte.valueOf(0); long b = (long)d; b")); - assertEquals((long)0, exec("def d = Short.valueOf(0); long b = (long)d; b")); - assertEquals((long)0, exec("def d = Character.valueOf(0); long b = (long)d; b")); - assertEquals((long)0, exec("def d = Integer.valueOf(0); long b = (long)d; b")); - assertEquals((long)0, exec("def d = Long.valueOf(0); long b = (long)d; b")); - assertEquals((long)0, exec("def d = Float.valueOf(0); long b = (long)d; b")); - assertEquals((long)0, exec("def d = Double.valueOf(0); long b = (long)d; b")); + assertEquals((long) 0, exec("def d = Byte.valueOf(0); long b = (long)d; b")); + assertEquals((long) 0, exec("def d = Short.valueOf(0); long b = (long)d; b")); + assertEquals((long) 0, exec("def d = Character.valueOf(0); long b = (long)d; b")); + assertEquals((long) 0, exec("def d = Integer.valueOf(0); long b = (long)d; b")); + assertEquals((long) 0, exec("def d = Long.valueOf(0); long b = (long)d; b")); + assertEquals((long) 0, exec("def d = Float.valueOf(0); long b = (long)d; b")); + assertEquals((long) 0, exec("def d = Double.valueOf(0); long b = (long)d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); long b = (long)d;")); } public void testdefTofloatExplicit() { expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; float b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = true; float b = (float)d;")); - assertEquals((float)0, exec("def d = (byte)0; float b = (float)d; b")); - assertEquals((float)0, exec("def d = (short)0; float b = (float)d; b")); - assertEquals((float)0, exec("def d = (char)0; float b = (float)d; b")); - assertEquals((float)0, exec("def d = 0; float b = (float)d; b")); - assertEquals((float)0, exec("def d = (long)0; float b = (float)d; b")); - assertEquals((float)0, exec("def d = (float)0; float b = (float)d; b")); - assertEquals((float)0, exec("def d = (double)0; float b = (float)d; b")); + assertEquals((float) 0, exec("def d = (byte)0; float b = (float)d; b")); + assertEquals((float) 0, exec("def d = (short)0; float b = (float)d; b")); + assertEquals((float) 0, exec("def d = (char)0; float b = (float)d; b")); + assertEquals((float) 0, exec("def d = 0; float b = (float)d; b")); + assertEquals((float) 0, exec("def d = (long)0; float b = (float)d; b")); + assertEquals((float) 0, exec("def d = (float)0; float b = (float)d; b")); + assertEquals((float) 0, exec("def d = (double)0; float b = (float)d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); float b = d;")); - assertEquals((float)0, exec("def d = Byte.valueOf(0); float b = (float)d; b")); - assertEquals((float)0, exec("def d = Short.valueOf(0); float b = (float)d; b")); - assertEquals((float)0, exec("def d = Character.valueOf(0); float b = (float)d; b")); - assertEquals((float)0, exec("def d = Integer.valueOf(0); float b = (float)d; b")); - assertEquals((float)0, exec("def d = Long.valueOf(0); float b = (float)d; b")); - assertEquals((float)0, exec("def d = Float.valueOf(0); float b = (float)d; b")); - assertEquals((float)0, exec("def d = Double.valueOf(0); float b = (float)d; b")); + assertEquals((float) 0, exec("def d = Byte.valueOf(0); float b = (float)d; b")); + assertEquals((float) 0, exec("def d = Short.valueOf(0); float b = (float)d; b")); + assertEquals((float) 0, exec("def d = Character.valueOf(0); float b = (float)d; b")); + assertEquals((float) 0, exec("def d = Integer.valueOf(0); float b = (float)d; b")); + assertEquals((float) 0, exec("def d = Long.valueOf(0); float b = (float)d; b")); + assertEquals((float) 0, exec("def d = Float.valueOf(0); float b = (float)d; b")); + assertEquals((float) 0, exec("def d = Double.valueOf(0); float b = (float)d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); float b = (float)d;")); } public void testdefTodoubleExplicit() { expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; double b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = true; double b = (double)d;")); - assertEquals((double)0, exec("def d = (byte)0; double b = (double)d; b")); - assertEquals((double)0, exec("def d = (short)0; double b = (double)d; b")); - assertEquals((double)0, exec("def d = (char)0; double b = (double)d; b")); - assertEquals((double)0, exec("def d = 0; double b = (double)d; b")); - assertEquals((double)0, exec("def d = (long)0; double b = (double)d; b")); - assertEquals((double)0, exec("def d = (float)0; double b = (double)d; b")); - assertEquals((double)0, exec("def d = (double)0; double b = (double)d; b")); + assertEquals((double) 0, exec("def d = (byte)0; double b = (double)d; b")); + assertEquals((double) 0, exec("def d = (short)0; double b = (double)d; b")); + assertEquals((double) 0, exec("def d = (char)0; double b = (double)d; b")); + assertEquals((double) 0, exec("def d = 0; double b = (double)d; b")); + assertEquals((double) 0, exec("def d = (long)0; double b = (double)d; b")); + assertEquals((double) 0, exec("def d = (float)0; double b = (double)d; b")); + assertEquals((double) 0, exec("def d = (double)0; double b = (double)d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); double b = d;")); - assertEquals((double)0, exec("def d = Byte.valueOf(0); double b = (double)d; b")); - assertEquals((double)0, exec("def d = Short.valueOf(0); double b = (double)d; b")); - assertEquals((double)0, exec("def d = Character.valueOf(0); double b = (double)d; b")); - assertEquals((double)0, exec("def d = Integer.valueOf(0); double b = (double)d; b")); - assertEquals((double)0, exec("def d = Long.valueOf(0); double b = (double)d; b")); - assertEquals((double)0, exec("def d = Float.valueOf(0); double b = (double)d; b")); - assertEquals((double)0, exec("def d = Double.valueOf(0); double b = (double)d; b")); + assertEquals((double) 0, exec("def d = Byte.valueOf(0); double b = (double)d; b")); + assertEquals((double) 0, exec("def d = Short.valueOf(0); double b = (double)d; b")); + assertEquals((double) 0, exec("def d = Character.valueOf(0); double b = (double)d; b")); + assertEquals((double) 0, exec("def d = Integer.valueOf(0); double b = (double)d; b")); + assertEquals((double) 0, exec("def d = Long.valueOf(0); double b = (double)d; b")); + assertEquals((double) 0, exec("def d = Float.valueOf(0); double b = (double)d; b")); + assertEquals((double) 0, exec("def d = Double.valueOf(0); double b = (double)d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); double b = (double)d;")); } @@ -351,7 +351,7 @@ public void testdefToBooleanImplicit() { public void testdefToByteImplicit() { expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; Byte b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = true; Byte b = d;")); - assertEquals((byte)0, exec("def d = (byte)0; Byte b = d; b")); + assertEquals((byte) 0, exec("def d = (byte)0; Byte b = d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = (short)0; Byte b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = (char)0; Byte b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = (int)0; Byte b = d;")); @@ -359,7 +359,7 @@ public void testdefToByteImplicit() { expectScriptThrows(ClassCastException.class, () -> exec("def d = (float)0; Byte b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = (double)0; Byte b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); Byte b = d;")); - assertEquals((byte)0, exec("def d = Byte.valueOf(0); Byte b = d; b")); + assertEquals((byte) 0, exec("def d = Byte.valueOf(0); Byte b = d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Short.valueOf(0); Byte b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Character.valueOf(0); Byte b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Integer.valueOf(0); Byte b = d;")); @@ -372,16 +372,16 @@ public void testdefToByteImplicit() { public void testdefToShortImplicit() { expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; Short b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = true; Short b = d;")); - assertEquals((short)0, exec("def d = (byte)0; Short b = d; b")); - assertEquals((short)0, exec("def d = (short)0; Short b = d; b")); + assertEquals((short) 0, exec("def d = (byte)0; Short b = d; b")); + assertEquals((short) 0, exec("def d = (short)0; Short b = d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = (char)0; Short b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = (int)0; Short b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = (long)0; Short b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = (float)0; Short b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = (double)0; Short b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); Short b = d;")); - assertEquals((short)0, exec("def d = Byte.valueOf(0); Short b = d; b")); - assertEquals((short)0, exec("def d = Short.valueOf(0); Short b = d; b")); + assertEquals((short) 0, exec("def d = Byte.valueOf(0); Short b = d; b")); + assertEquals((short) 0, exec("def d = Short.valueOf(0); Short b = d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Character.valueOf(0); Short b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Integer.valueOf(0); Short b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Long.valueOf(0); Short b = d;")); @@ -396,7 +396,7 @@ public void testdefToCharacterImplicit() { expectScriptThrows(ClassCastException.class, () -> exec("def d = true; Character b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = (byte)0; Character b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = (short)0; Character b = d;")); - assertEquals((char)0, exec("def d = (char)0; Character b = d; b")); + assertEquals((char) 0, exec("def d = (char)0; Character b = d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = (int)0; Character b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = (long)0; Character b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = (float)0; Character b = d;")); @@ -404,7 +404,7 @@ public void testdefToCharacterImplicit() { expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); Character b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Byte.valueOf(0); Character b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Short.valueOf(0); Character b = d;")); - assertEquals((char)0, exec("def d = Character.valueOf(0); Character b = d; b")); + assertEquals((char) 0, exec("def d = Character.valueOf(0); Character b = d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Integer.valueOf(0); Character b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Long.valueOf(0); Character b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Float.valueOf(0); Character b = d;")); @@ -436,19 +436,19 @@ public void testdefToIntegerImplicit() { public void testdefToLongImplicit() { expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; Long b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = true; Long b = d;")); - assertEquals((long)0, exec("def d = (byte)0; Long b = d; b")); - assertEquals((long)0, exec("def d = (short)0; Long b = d; b")); - assertEquals((long)0, exec("def d = (char)0; Long b = d; b")); - assertEquals((long)0, exec("def d = 0; Long b = d; b")); - assertEquals((long)0, exec("def d = (long)0; Long b = d; b")); + assertEquals((long) 0, exec("def d = (byte)0; Long b = d; b")); + assertEquals((long) 0, exec("def d = (short)0; Long b = d; b")); + assertEquals((long) 0, exec("def d = (char)0; Long b = d; b")); + assertEquals((long) 0, exec("def d = 0; Long b = d; b")); + assertEquals((long) 0, exec("def d = (long)0; Long b = d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = (float)0; Long b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = (double)0; Long b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); Long b = d;")); - assertEquals((long)0, exec("def d = Byte.valueOf(0); Long b = d; b")); - assertEquals((long)0, exec("def d = Short.valueOf(0); Long b = d; b")); - assertEquals((long)0, exec("def d = Character.valueOf(0); Long b = d; b")); - assertEquals((long)0, exec("def d = Integer.valueOf(0); Long b = d; b")); - assertEquals((long)0, exec("def d = Long.valueOf(0); Long b = d; b")); + assertEquals((long) 0, exec("def d = Byte.valueOf(0); Long b = d; b")); + assertEquals((long) 0, exec("def d = Short.valueOf(0); Long b = d; b")); + assertEquals((long) 0, exec("def d = Character.valueOf(0); Long b = d; b")); + assertEquals((long) 0, exec("def d = Integer.valueOf(0); Long b = d; b")); + assertEquals((long) 0, exec("def d = Long.valueOf(0); Long b = d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Float.valueOf(0); Long b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Double.valueOf(0); Long b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); Long b = d;")); @@ -457,20 +457,20 @@ public void testdefToLongImplicit() { public void testdefToFloatImplicit() { expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; Float b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = true; Float b = d;")); - assertEquals((float)0, exec("def d = (byte)0; Float b = d; b")); - assertEquals((float)0, exec("def d = (short)0; Float b = d; b")); - assertEquals((float)0, exec("def d = (char)0; Float b = d; b")); - assertEquals((float)0, exec("def d = 0; Float b = d; b")); - assertEquals((float)0, exec("def d = (long)0; Float b = d; b")); - assertEquals((float)0, exec("def d = (float)0; Float b = d; b")); + assertEquals((float) 0, exec("def d = (byte)0; Float b = d; b")); + assertEquals((float) 0, exec("def d = (short)0; Float b = d; b")); + assertEquals((float) 0, exec("def d = (char)0; Float b = d; b")); + assertEquals((float) 0, exec("def d = 0; Float b = d; b")); + assertEquals((float) 0, exec("def d = (long)0; Float b = d; b")); + assertEquals((float) 0, exec("def d = (float)0; Float b = d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = (double)0; Float b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); Float b = d;")); - assertEquals((float)0, exec("def d = Byte.valueOf(0); Float b = d; b")); - assertEquals((float)0, exec("def d = Short.valueOf(0); Float b = d; b")); - assertEquals((float)0, exec("def d = Character.valueOf(0); Float b = d; b")); - assertEquals((float)0, exec("def d = Integer.valueOf(0); Float b = d; b")); - assertEquals((float)0, exec("def d = Long.valueOf(0); Float b = d; b")); - assertEquals((float)0, exec("def d = Float.valueOf(0); Float b = d; b")); + assertEquals((float) 0, exec("def d = Byte.valueOf(0); Float b = d; b")); + assertEquals((float) 0, exec("def d = Short.valueOf(0); Float b = d; b")); + assertEquals((float) 0, exec("def d = Character.valueOf(0); Float b = d; b")); + assertEquals((float) 0, exec("def d = Integer.valueOf(0); Float b = d; b")); + assertEquals((float) 0, exec("def d = Long.valueOf(0); Float b = d; b")); + assertEquals((float) 0, exec("def d = Float.valueOf(0); Float b = d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Double.valueOf(0); Float b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); Float b = d;")); } @@ -478,21 +478,21 @@ public void testdefToFloatImplicit() { public void testdefToDoubleImplicit() { expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; Double b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = true; Double b = d;")); - assertEquals((double)0, exec("def d = (byte)0; Double b = d; b")); - assertEquals((double)0, exec("def d = (short)0; Double b = d; b")); - assertEquals((double)0, exec("def d = (char)0; Double b = d; b")); - assertEquals((double)0, exec("def d = 0; Double b = d; b")); - assertEquals((double)0, exec("def d = (long)0; Double b = d; b")); - assertEquals((double)0, exec("def d = (float)0; Double b = d; b")); - assertEquals((double)0, exec("def d = (double)0; Double b = d; b")); + assertEquals((double) 0, exec("def d = (byte)0; Double b = d; b")); + assertEquals((double) 0, exec("def d = (short)0; Double b = d; b")); + assertEquals((double) 0, exec("def d = (char)0; Double b = d; b")); + assertEquals((double) 0, exec("def d = 0; Double b = d; b")); + assertEquals((double) 0, exec("def d = (long)0; Double b = d; b")); + assertEquals((double) 0, exec("def d = (float)0; Double b = d; b")); + assertEquals((double) 0, exec("def d = (double)0; Double b = d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); Double b = d;")); - assertEquals((double)0, exec("def d = Byte.valueOf(0); Double b = d; b")); - assertEquals((double)0, exec("def d = Short.valueOf(0); Double b = d; b")); - assertEquals((double)0, exec("def d = Character.valueOf(0); Double b = d; b")); - assertEquals((double)0, exec("def d = Integer.valueOf(0); Double b = d; b")); - assertEquals((double)0, exec("def d = Long.valueOf(0); Double b = d; b")); - assertEquals((double)0, exec("def d = Float.valueOf(0); Double b = d; b")); - assertEquals((double)0, exec("def d = Double.valueOf(0); Double b = d; b")); + assertEquals((double) 0, exec("def d = Byte.valueOf(0); Double b = d; b")); + assertEquals((double) 0, exec("def d = Short.valueOf(0); Double b = d; b")); + assertEquals((double) 0, exec("def d = Character.valueOf(0); Double b = d; b")); + assertEquals((double) 0, exec("def d = Integer.valueOf(0); Double b = d; b")); + assertEquals((double) 0, exec("def d = Long.valueOf(0); Double b = d; b")); + assertEquals((double) 0, exec("def d = Float.valueOf(0); Double b = d; b")); + assertEquals((double) 0, exec("def d = Double.valueOf(0); Double b = d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); Double b = d;")); } @@ -520,42 +520,42 @@ public void testdefToBooleanExplicit() { public void testdefToByteExplicit() { expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; Byte b = (Byte)d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = true; Byte b = (Byte)d;")); - assertEquals((byte)0, exec("def d = (byte)0; Byte b = (Byte)d; b")); - assertEquals((byte)0, exec("def d = (short)0; Byte b = (Byte)d; b")); - assertEquals((byte)0, exec("def d = (char)0; Byte b = (Byte)d; b")); - assertEquals((byte)0, exec("def d = 0; Byte b = (Byte)d; b")); - assertEquals((byte)0, exec("def d = (long)0; Byte b = (Byte)d; b")); - assertEquals((byte)0, exec("def d = (float)0; Byte b = (Byte)d; b")); - assertEquals((byte)0, exec("def d = (double)0; Byte b = (Byte)d; b")); + assertEquals((byte) 0, exec("def d = (byte)0; Byte b = (Byte)d; b")); + assertEquals((byte) 0, exec("def d = (short)0; Byte b = (Byte)d; b")); + assertEquals((byte) 0, exec("def d = (char)0; Byte b = (Byte)d; b")); + assertEquals((byte) 0, exec("def d = 0; Byte b = (Byte)d; b")); + assertEquals((byte) 0, exec("def d = (long)0; Byte b = (Byte)d; b")); + assertEquals((byte) 0, exec("def d = (float)0; Byte b = (Byte)d; b")); + assertEquals((byte) 0, exec("def d = (double)0; Byte b = (Byte)d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); Byte b = d;")); - assertEquals((byte)0, exec("def d = Byte.valueOf(0); Byte b = (Byte)d; b")); - assertEquals((byte)0, exec("def d = Short.valueOf(0); Byte b = (Byte)d; b")); - assertEquals((byte)0, exec("def d = Character.valueOf(0); Byte b = (Byte)d; b")); - assertEquals((byte)0, exec("def d = Integer.valueOf(0); Byte b = (Byte)d; b")); - assertEquals((byte)0, exec("def d = Long.valueOf(0); Byte b = (Byte)d; b")); - assertEquals((byte)0, exec("def d = Float.valueOf(0); Byte b = (Byte)d; b")); - assertEquals((byte)0, exec("def d = Double.valueOf(0); Byte b = (Byte)d; b")); + assertEquals((byte) 0, exec("def d = Byte.valueOf(0); Byte b = (Byte)d; b")); + assertEquals((byte) 0, exec("def d = Short.valueOf(0); Byte b = (Byte)d; b")); + assertEquals((byte) 0, exec("def d = Character.valueOf(0); Byte b = (Byte)d; b")); + assertEquals((byte) 0, exec("def d = Integer.valueOf(0); Byte b = (Byte)d; b")); + assertEquals((byte) 0, exec("def d = Long.valueOf(0); Byte b = (Byte)d; b")); + assertEquals((byte) 0, exec("def d = Float.valueOf(0); Byte b = (Byte)d; b")); + assertEquals((byte) 0, exec("def d = Double.valueOf(0); Byte b = (Byte)d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); Byte b = (Byte)d;")); } public void testdefToShortExplicit() { expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; Short b = (Short)d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = true; Short b = (Short)d;")); - assertEquals((short)0, exec("def d = (byte)0; Short b = (Short)d; b")); - assertEquals((short)0, exec("def d = (short)0; Short b = (Short)d; b")); - assertEquals((short)0, exec("def d = (char)0; Short b = (Short)d; b")); - assertEquals((short)0, exec("def d = 0; Short b = (Short)d; b")); - assertEquals((short)0, exec("def d = (long)0; Short b = (Short)d; b")); - assertEquals((short)0, exec("def d = (float)0; Short b = (Short)d; b")); - assertEquals((short)0, exec("def d = (double)0; Short b = (Short)d; b")); + assertEquals((short) 0, exec("def d = (byte)0; Short b = (Short)d; b")); + assertEquals((short) 0, exec("def d = (short)0; Short b = (Short)d; b")); + assertEquals((short) 0, exec("def d = (char)0; Short b = (Short)d; b")); + assertEquals((short) 0, exec("def d = 0; Short b = (Short)d; b")); + assertEquals((short) 0, exec("def d = (long)0; Short b = (Short)d; b")); + assertEquals((short) 0, exec("def d = (float)0; Short b = (Short)d; b")); + assertEquals((short) 0, exec("def d = (double)0; Short b = (Short)d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); Short b = d;")); - assertEquals((short)0, exec("def d = Byte.valueOf(0); Short b = (Short)d; b")); - assertEquals((short)0, exec("def d = Short.valueOf(0); Short b = (Short)d; b")); - assertEquals((short)0, exec("def d = Character.valueOf(0); Short b = (Short)d; b")); - assertEquals((short)0, exec("def d = Integer.valueOf(0); Short b = (Short)d; b")); - assertEquals((short)0, exec("def d = Long.valueOf(0); Short b = (Short)d; b")); - assertEquals((short)0, exec("def d = Float.valueOf(0); Short b = (Short)d; b")); - assertEquals((short)0, exec("def d = Double.valueOf(0); Short b = (Short)d; b")); + assertEquals((short) 0, exec("def d = Byte.valueOf(0); Short b = (Short)d; b")); + assertEquals((short) 0, exec("def d = Short.valueOf(0); Short b = (Short)d; b")); + assertEquals((short) 0, exec("def d = Character.valueOf(0); Short b = (Short)d; b")); + assertEquals((short) 0, exec("def d = Integer.valueOf(0); Short b = (Short)d; b")); + assertEquals((short) 0, exec("def d = Long.valueOf(0); Short b = (Short)d; b")); + assertEquals((short) 0, exec("def d = Float.valueOf(0); Short b = (Short)d; b")); + assertEquals((short) 0, exec("def d = Double.valueOf(0); Short b = (Short)d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); Short b = (Short)d;")); } @@ -563,21 +563,21 @@ public void testdefToCharacterExplicit() { assertEquals('s', exec("def d = 's'; Character b = (Character)d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; Character b = (Character)d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = true; Character b = (Character)d;")); - assertEquals((char)0, exec("def d = (byte)0; Character b = (Character)d; b")); - assertEquals((char)0, exec("def d = (short)0; Character b = (Character)d; b")); - assertEquals((char)0, exec("def d = (char)0; Character b = (Character)d; b")); - assertEquals((char)0, exec("def d = 0; Character b = (Character)d; b")); - assertEquals((char)0, exec("def d = (long)0; Character b = (Character)d; b")); - assertEquals((char)0, exec("def d = (float)0; Character b = (Character)d; b")); - assertEquals((char)0, exec("def d = (double)0; Character b = (Character)d; b")); + assertEquals((char) 0, exec("def d = (byte)0; Character b = (Character)d; b")); + assertEquals((char) 0, exec("def d = (short)0; Character b = (Character)d; b")); + assertEquals((char) 0, exec("def d = (char)0; Character b = (Character)d; b")); + assertEquals((char) 0, exec("def d = 0; Character b = (Character)d; b")); + assertEquals((char) 0, exec("def d = (long)0; Character b = (Character)d; b")); + assertEquals((char) 0, exec("def d = (float)0; Character b = (Character)d; b")); + assertEquals((char) 0, exec("def d = (double)0; Character b = (Character)d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); Character b = d;")); - assertEquals((char)0, exec("def d = Byte.valueOf(0); Character b = (Character)d; b")); - assertEquals((char)0, exec("def d = Short.valueOf(0); Character b = (Character)d; b")); - assertEquals((char)0, exec("def d = Character.valueOf(0); Character b = (Character)d; b")); - assertEquals((char)0, exec("def d = Integer.valueOf(0); Character b = (Character)d; b")); - assertEquals((char)0, exec("def d = Long.valueOf(0); Character b = (Character)d; b")); - assertEquals((char)0, exec("def d = Float.valueOf(0); Character b = (Character)d; b")); - assertEquals((char)0, exec("def d = Double.valueOf(0); Character b = (Character)d; b")); + assertEquals((char) 0, exec("def d = Byte.valueOf(0); Character b = (Character)d; b")); + assertEquals((char) 0, exec("def d = Short.valueOf(0); Character b = (Character)d; b")); + assertEquals((char) 0, exec("def d = Character.valueOf(0); Character b = (Character)d; b")); + assertEquals((char) 0, exec("def d = Integer.valueOf(0); Character b = (Character)d; b")); + assertEquals((char) 0, exec("def d = Long.valueOf(0); Character b = (Character)d; b")); + assertEquals((char) 0, exec("def d = Float.valueOf(0); Character b = (Character)d; b")); + assertEquals((char) 0, exec("def d = Double.valueOf(0); Character b = (Character)d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); Character b = (Character)d;")); } @@ -605,63 +605,63 @@ public void testdefToIntegerExplicit() { public void testdefToLongExplicit() { expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; Long b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = true; Long b = (Long)d;")); - assertEquals((long)0, exec("def d = (byte)0; Long b = (Long)d; b")); - assertEquals((long)0, exec("def d = (short)0; Long b = (Long)d; b")); - assertEquals((long)0, exec("def d = (char)0; Long b = (Long)d; b")); - assertEquals((long)0, exec("def d = 0; Long b = (Long)d; b")); - assertEquals((long)0, exec("def d = (long)0; Long b = (Long)d; b")); - assertEquals((long)0, exec("def d = (float)0; Long b = (Long)d; b")); - assertEquals((long)0, exec("def d = (double)0; Long b = (Long)d; b")); + assertEquals((long) 0, exec("def d = (byte)0; Long b = (Long)d; b")); + assertEquals((long) 0, exec("def d = (short)0; Long b = (Long)d; b")); + assertEquals((long) 0, exec("def d = (char)0; Long b = (Long)d; b")); + assertEquals((long) 0, exec("def d = 0; Long b = (Long)d; b")); + assertEquals((long) 0, exec("def d = (long)0; Long b = (Long)d; b")); + assertEquals((long) 0, exec("def d = (float)0; Long b = (Long)d; b")); + assertEquals((long) 0, exec("def d = (double)0; Long b = (Long)d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); Long b = d;")); - assertEquals((long)0, exec("def d = Byte.valueOf(0); Long b = (Long)d; b")); - assertEquals((long)0, exec("def d = Short.valueOf(0); Long b = (Long)d; b")); - assertEquals((long)0, exec("def d = Character.valueOf(0); Long b = (Long)d; b")); - assertEquals((long)0, exec("def d = Integer.valueOf(0); Long b = (Long)d; b")); - assertEquals((long)0, exec("def d = Long.valueOf(0); Long b = (Long)d; b")); - assertEquals((long)0, exec("def d = Float.valueOf(0); Long b = (Long)d; b")); - assertEquals((long)0, exec("def d = Double.valueOf(0); Long b = (Long)d; b")); + assertEquals((long) 0, exec("def d = Byte.valueOf(0); Long b = (Long)d; b")); + assertEquals((long) 0, exec("def d = Short.valueOf(0); Long b = (Long)d; b")); + assertEquals((long) 0, exec("def d = Character.valueOf(0); Long b = (Long)d; b")); + assertEquals((long) 0, exec("def d = Integer.valueOf(0); Long b = (Long)d; b")); + assertEquals((long) 0, exec("def d = Long.valueOf(0); Long b = (Long)d; b")); + assertEquals((long) 0, exec("def d = Float.valueOf(0); Long b = (Long)d; b")); + assertEquals((long) 0, exec("def d = Double.valueOf(0); Long b = (Long)d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); Long b = (Long)d;")); } public void testdefToFloatExplicit() { expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; Float b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = true; Float b = (Float)d;")); - assertEquals((float)0, exec("def d = (byte)0; Float b = (Float)d; b")); - assertEquals((float)0, exec("def d = (short)0; Float b = (Float)d; b")); - assertEquals((float)0, exec("def d = (char)0; Float b = (Float)d; b")); - assertEquals((float)0, exec("def d = 0; Float b = (Float)d; b")); - assertEquals((float)0, exec("def d = (long)0; Float b = (Float)d; b")); - assertEquals((float)0, exec("def d = (float)0; Float b = (Float)d; b")); - assertEquals((float)0, exec("def d = (double)0; Float b = (Float)d; b")); + assertEquals((float) 0, exec("def d = (byte)0; Float b = (Float)d; b")); + assertEquals((float) 0, exec("def d = (short)0; Float b = (Float)d; b")); + assertEquals((float) 0, exec("def d = (char)0; Float b = (Float)d; b")); + assertEquals((float) 0, exec("def d = 0; Float b = (Float)d; b")); + assertEquals((float) 0, exec("def d = (long)0; Float b = (Float)d; b")); + assertEquals((float) 0, exec("def d = (float)0; Float b = (Float)d; b")); + assertEquals((float) 0, exec("def d = (double)0; Float b = (Float)d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); Float b = d;")); - assertEquals((float)0, exec("def d = Byte.valueOf(0); Float b = (Float)d; b")); - assertEquals((float)0, exec("def d = Short.valueOf(0); Float b = (Float)d; b")); - assertEquals((float)0, exec("def d = Character.valueOf(0); Float b = (Float)d; b")); - assertEquals((float)0, exec("def d = Integer.valueOf(0); Float b = (Float)d; b")); - assertEquals((float)0, exec("def d = Long.valueOf(0); Float b = (Float)d; b")); - assertEquals((float)0, exec("def d = Float.valueOf(0); Float b = (Float)d; b")); - assertEquals((float)0, exec("def d = Double.valueOf(0); Float b = (Float)d; b")); + assertEquals((float) 0, exec("def d = Byte.valueOf(0); Float b = (Float)d; b")); + assertEquals((float) 0, exec("def d = Short.valueOf(0); Float b = (Float)d; b")); + assertEquals((float) 0, exec("def d = Character.valueOf(0); Float b = (Float)d; b")); + assertEquals((float) 0, exec("def d = Integer.valueOf(0); Float b = (Float)d; b")); + assertEquals((float) 0, exec("def d = Long.valueOf(0); Float b = (Float)d; b")); + assertEquals((float) 0, exec("def d = Float.valueOf(0); Float b = (Float)d; b")); + assertEquals((float) 0, exec("def d = Double.valueOf(0); Float b = (Float)d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); Float b = (Float)d;")); } public void testdefToDoubleExplicit() { expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; Double b = d;")); expectScriptThrows(ClassCastException.class, () -> exec("def d = true; Double b = (Double)d;")); - assertEquals((double)0, exec("def d = (byte)0; Double b = (Double)d; b")); - assertEquals((double)0, exec("def d = (short)0; Double b = (Double)d; b")); - assertEquals((double)0, exec("def d = (char)0; Double b = (Double)d; b")); - assertEquals((double)0, exec("def d = 0; Double b = (Double)d; b")); - assertEquals((double)0, exec("def d = (long)0; Double b = (Double)d; b")); - assertEquals((double)0, exec("def d = (float)0; Double b = (Double)d; b")); - assertEquals((double)0, exec("def d = (double)0; Double b = (Double)d; b")); + assertEquals((double) 0, exec("def d = (byte)0; Double b = (Double)d; b")); + assertEquals((double) 0, exec("def d = (short)0; Double b = (Double)d; b")); + assertEquals((double) 0, exec("def d = (char)0; Double b = (Double)d; b")); + assertEquals((double) 0, exec("def d = 0; Double b = (Double)d; b")); + assertEquals((double) 0, exec("def d = (long)0; Double b = (Double)d; b")); + assertEquals((double) 0, exec("def d = (float)0; Double b = (Double)d; b")); + assertEquals((double) 0, exec("def d = (double)0; Double b = (Double)d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); Double b = d;")); - assertEquals((double)0, exec("def d = Byte.valueOf(0); Double b = (Double)d; b")); - assertEquals((double)0, exec("def d = Short.valueOf(0); Double b = (Double)d; b")); - assertEquals((double)0, exec("def d = Character.valueOf(0); Double b = (Double)d; b")); - assertEquals((double)0, exec("def d = Integer.valueOf(0); Double b = (Double)d; b")); - assertEquals((double)0, exec("def d = Long.valueOf(0); Double b = (Double)d; b")); - assertEquals((double)0, exec("def d = Float.valueOf(0); Double b = (Double)d; b")); - assertEquals((double)0, exec("def d = Double.valueOf(0); Double b = (Double)d; b")); + assertEquals((double) 0, exec("def d = Byte.valueOf(0); Double b = (Double)d; b")); + assertEquals((double) 0, exec("def d = Short.valueOf(0); Double b = (Double)d; b")); + assertEquals((double) 0, exec("def d = Character.valueOf(0); Double b = (Double)d; b")); + assertEquals((double) 0, exec("def d = Integer.valueOf(0); Double b = (Double)d; b")); + assertEquals((double) 0, exec("def d = Long.valueOf(0); Double b = (Double)d; b")); + assertEquals((double) 0, exec("def d = Float.valueOf(0); Double b = (Double)d; b")); + assertEquals((double) 0, exec("def d = Double.valueOf(0); Double b = (Double)d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); Double b = (Double)d;")); } @@ -674,15 +674,15 @@ public void testdefToStringExplicit() { } public void testConstFoldingDefCast() { - assertFalse((boolean)exec("def chr = 10; return (chr == (char)'x');")); - assertFalse((boolean)exec("def chr = 10; return (chr >= (char)'x');")); - assertTrue((boolean)exec("def chr = (char)10; return (chr <= (char)'x');")); - assertTrue((boolean)exec("def chr = 10; return (chr < (char)'x');")); - assertFalse((boolean)exec("def chr = (char)10; return (chr > (char)'x');")); - assertFalse((boolean)exec("def chr = 10L; return (chr > (char)'x');")); - assertFalse((boolean)exec("def chr = 10F; return (chr > (char)'x');")); - assertFalse((boolean)exec("def chr = 10D; return (chr > (char)'x');")); - assertFalse((boolean)exec("def chr = (char)10L; return (chr > (byte)10);")); - assertFalse((boolean)exec("def chr = (char)10L; return (chr > (double)(byte)(char)10);")); + assertFalse((boolean) exec("def chr = 10; return (chr == (char)'x');")); + assertFalse((boolean) exec("def chr = 10; return (chr >= (char)'x');")); + assertTrue((boolean) exec("def chr = (char)10; return (chr <= (char)'x');")); + assertTrue((boolean) exec("def chr = 10; return (chr < (char)'x');")); + assertFalse((boolean) exec("def chr = (char)10; return (chr > (char)'x');")); + assertFalse((boolean) exec("def chr = 10L; return (chr > (char)'x');")); + assertFalse((boolean) exec("def chr = 10F; return (chr > (char)'x');")); + assertFalse((boolean) exec("def chr = 10D; return (chr > (char)'x');")); + assertFalse((boolean) exec("def chr = (char)10L; return (chr > (byte)10);")); + assertFalse((boolean) exec("def chr = (char)10L; return (chr > (double)(byte)(char)10);")); } } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/DefEncodingTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/DefEncodingTests.java index e7d9fca729d59..f8df4424d826a 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/DefEncodingTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/DefEncodingTests.java @@ -15,48 +15,55 @@ public class DefEncodingTests extends ESTestCase { public void testParse() { - assertEquals(new Def.Encoding(true, false, "java.util.Comparator", "thenComparing", 1), - new Def.Encoding("Sfjava.util.Comparator.thenComparing,1")); + assertEquals( + new Def.Encoding(true, false, "java.util.Comparator", "thenComparing", 1), + new Def.Encoding("Sfjava.util.Comparator.thenComparing,1") + ); - assertEquals(new Def.Encoding(false, false, "ft0", "augmentInjectMultiTimesX", 1), - new Def.Encoding("Dfft0.augmentInjectMultiTimesX,1")); + assertEquals( + new Def.Encoding(false, false, "ft0", "augmentInjectMultiTimesX", 1), + new Def.Encoding("Dfft0.augmentInjectMultiTimesX,1") + ); - assertEquals(new Def.Encoding(false, false, "x", "concat", 1), - new Def.Encoding("Dfx.concat,1")); + assertEquals(new Def.Encoding(false, false, "x", "concat", 1), new Def.Encoding("Dfx.concat,1")); - assertEquals(new Def.Encoding(true, false, "java.lang.StringBuilder", "setLength", 1), - new Def.Encoding("Sfjava.lang.StringBuilder.setLength,1")); + assertEquals( + new Def.Encoding(true, false, "java.lang.StringBuilder", "setLength", 1), + new Def.Encoding("Sfjava.lang.StringBuilder.setLength,1") + ); - assertEquals(new Def.Encoding(true, false, "org.elasticsearch.painless.FeatureTestObject", "overloadedStatic", 0), - new Def.Encoding("Sforg.elasticsearch.painless.FeatureTestObject.overloadedStatic,0")); + assertEquals( + new Def.Encoding(true, false, "org.elasticsearch.painless.FeatureTestObject", "overloadedStatic", 0), + new Def.Encoding("Sforg.elasticsearch.painless.FeatureTestObject.overloadedStatic,0") + ); - assertEquals(new Def.Encoding(true, false, "this", "lambda$synthetic$0", 1), - new Def.Encoding("Sfthis.lambda$synthetic$0,1")); + assertEquals(new Def.Encoding(true, false, "this", "lambda$synthetic$0", 1), new Def.Encoding("Sfthis.lambda$synthetic$0,1")); - assertEquals(new Def.Encoding(true, true, "this", "lambda$synthetic$0", 2), - new Def.Encoding("Stthis.lambda$synthetic$0,2")); + assertEquals(new Def.Encoding(true, true, "this", "lambda$synthetic$0", 2), new Def.Encoding("Stthis.lambda$synthetic$0,2")); - assertEquals(new Def.Encoding(true, true, "this", "mycompare", 0), - new Def.Encoding("Stthis.mycompare,0")); + assertEquals(new Def.Encoding(true, true, "this", "mycompare", 0), new Def.Encoding("Stthis.mycompare,0")); } public void testValidate() { - IllegalArgumentException expected = expectThrows(IllegalArgumentException.class, - () -> new Def.Encoding(false, false, "this", "myMethod", 0)); + IllegalArgumentException expected = expectThrows( + IllegalArgumentException.class, + () -> new Def.Encoding(false, false, "this", "myMethod", 0) + ); - assertThat(expected.getMessage(), - startsWith("Def.Encoding must be static if symbol is 'this', encoding [Dfthis.myMethod,0]")); + assertThat(expected.getMessage(), startsWith("Def.Encoding must be static if symbol is 'this', encoding [Dfthis.myMethod,0]")); - expected = expectThrows(IllegalArgumentException.class, - () -> new Def.Encoding(true, true, "org.elasticsearch.painless.FeatureTestObject", "overloadedStatic", 0)); + expected = expectThrows( + IllegalArgumentException.class, + () -> new Def.Encoding(true, true, "org.elasticsearch.painless.FeatureTestObject", "overloadedStatic", 0) + ); - assertThat(expected.getMessage(), - startsWith("Def.Encoding symbol must be 'this', not [org.elasticsearch.painless.FeatureTestObject] if needsInstance")); + assertThat( + expected.getMessage(), + startsWith("Def.Encoding symbol must be 'this', not [org.elasticsearch.painless.FeatureTestObject] if needsInstance") + ); - expected = expectThrows(IllegalArgumentException.class, - () -> new Def.Encoding(false, false, "x", "", 1)); + expected = expectThrows(IllegalArgumentException.class, () -> new Def.Encoding(false, false, "x", "", 1)); - assertThat(expected.getMessage(), - startsWith("methodName must be non-empty, encoding [Dfx.,1]")); + assertThat(expected.getMessage(), startsWith("methodName must be non-empty, encoding [Dfx.,1]")); } } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/DefOptimizationTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/DefOptimizationTests.java index 81d458007cc66..5973cf8b3728a 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/DefOptimizationTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/DefOptimizationTests.java @@ -163,297 +163,257 @@ public void testIllegalCast() { final String script = "int x;\ndef y = new HashMap();\ny['double'] = 1.0;\nx = y.get('double');\n"; assertBytecodeExists(script, "INVOKEDYNAMIC get(Ljava/lang/Object;Ljava/lang/String;)I"); - final Exception exception = expectScriptThrows(ClassCastException.class, () -> { - exec(script); - }); + final Exception exception = expectScriptThrows(ClassCastException.class, () -> { exec(script); }); assertTrue(exception.getMessage().contains("Cannot cast java.lang.Double to java.lang.Integer")); } public void testMulOptLHS() { - assertBytecodeExists("int x = 1; def y = 2; return x * y", - "INVOKEDYNAMIC mul(ILjava/lang/Object;)Ljava/lang/Object;"); + assertBytecodeExists("int x = 1; def y = 2; return x * y", "INVOKEDYNAMIC mul(ILjava/lang/Object;)Ljava/lang/Object;"); } public void testMulOptRHS() { - assertBytecodeExists("def x = 1; int y = 2; return x * y", - "INVOKEDYNAMIC mul(Ljava/lang/Object;I)Ljava/lang/Object;"); + assertBytecodeExists("def x = 1; int y = 2; return x * y", "INVOKEDYNAMIC mul(Ljava/lang/Object;I)Ljava/lang/Object;"); } public void testMulOptRet() { - assertBytecodeExists("def x = 1; def y = 2; double d = x * y", - "INVOKEDYNAMIC mul(Ljava/lang/Object;Ljava/lang/Object;)D"); + assertBytecodeExists("def x = 1; def y = 2; double d = x * y", "INVOKEDYNAMIC mul(Ljava/lang/Object;Ljava/lang/Object;)D"); } public void testDivOptLHS() { - assertBytecodeExists("int x = 1; def y = 2; return x / y", - "INVOKEDYNAMIC div(ILjava/lang/Object;)Ljava/lang/Object;"); + assertBytecodeExists("int x = 1; def y = 2; return x / y", "INVOKEDYNAMIC div(ILjava/lang/Object;)Ljava/lang/Object;"); } public void testDivOptRHS() { - assertBytecodeExists("def x = 1; int y = 2; return x / y", - "INVOKEDYNAMIC div(Ljava/lang/Object;I)Ljava/lang/Object;"); + assertBytecodeExists("def x = 1; int y = 2; return x / y", "INVOKEDYNAMIC div(Ljava/lang/Object;I)Ljava/lang/Object;"); } public void testDivOptRet() { - assertBytecodeExists("def x = 1; def y = 2; double d = x / y", - "INVOKEDYNAMIC div(Ljava/lang/Object;Ljava/lang/Object;)D"); + assertBytecodeExists("def x = 1; def y = 2; double d = x / y", "INVOKEDYNAMIC div(Ljava/lang/Object;Ljava/lang/Object;)D"); } public void testRemOptLHS() { - assertBytecodeExists("int x = 1; def y = 2; return x % y", - "INVOKEDYNAMIC rem(ILjava/lang/Object;)Ljava/lang/Object;"); + assertBytecodeExists("int x = 1; def y = 2; return x % y", "INVOKEDYNAMIC rem(ILjava/lang/Object;)Ljava/lang/Object;"); } public void testRemOptRHS() { - assertBytecodeExists("def x = 1; int y = 2; return x % y", - "INVOKEDYNAMIC rem(Ljava/lang/Object;I)Ljava/lang/Object;"); + assertBytecodeExists("def x = 1; int y = 2; return x % y", "INVOKEDYNAMIC rem(Ljava/lang/Object;I)Ljava/lang/Object;"); } public void testRemOptRet() { - assertBytecodeExists("def x = 1; def y = 2; double d = x % y", - "INVOKEDYNAMIC rem(Ljava/lang/Object;Ljava/lang/Object;)D"); + assertBytecodeExists("def x = 1; def y = 2; double d = x % y", "INVOKEDYNAMIC rem(Ljava/lang/Object;Ljava/lang/Object;)D"); } public void testAddOptLHS() { - assertBytecodeExists("int x = 1; def y = 2; return x + y", - "INVOKEDYNAMIC add(ILjava/lang/Object;)Ljava/lang/Object;"); + assertBytecodeExists("int x = 1; def y = 2; return x + y", "INVOKEDYNAMIC add(ILjava/lang/Object;)Ljava/lang/Object;"); } public void testAddOptRHS() { - assertBytecodeExists("def x = 1; int y = 2; return x + y", - "INVOKEDYNAMIC add(Ljava/lang/Object;I)Ljava/lang/Object;"); + assertBytecodeExists("def x = 1; int y = 2; return x + y", "INVOKEDYNAMIC add(Ljava/lang/Object;I)Ljava/lang/Object;"); } public void testAddOptRet() { - assertBytecodeExists("def x = 1; def y = 2; double d = x + y", - "INVOKEDYNAMIC add(Ljava/lang/Object;Ljava/lang/Object;)D"); + assertBytecodeExists("def x = 1; def y = 2; double d = x + y", "INVOKEDYNAMIC add(Ljava/lang/Object;Ljava/lang/Object;)D"); } // horrible, sorry public void testAddOptNullGuards() { // needs null guard - assertBytecodeHasPattern("def x = 1; def y = 2; return x + y", - "(?s).*INVOKEDYNAMIC add.*arguments:\\s+" + "\\d+" - + ",\\s+" + DefBootstrap.BINARY_OPERATOR - + ",\\s+" + DefBootstrap.OPERATOR_ALLOWS_NULL + ".*"); + assertBytecodeHasPattern( + "def x = 1; def y = 2; return x + y", + "(?s).*INVOKEDYNAMIC add.*arguments:\\s+" + + "\\d+" + + ",\\s+" + + DefBootstrap.BINARY_OPERATOR + + ",\\s+" + + DefBootstrap.OPERATOR_ALLOWS_NULL + + ".*" + ); // still needs null guard, NPE is the wrong thing! - assertBytecodeHasPattern("def x = 1; def y = 2; double z = x + y", - "(?s).*INVOKEDYNAMIC add.*arguments:\\s+" + "\\d+" - + ",\\s+" + DefBootstrap.BINARY_OPERATOR - + ",\\s+" + DefBootstrap.OPERATOR_ALLOWS_NULL + ".*"); + assertBytecodeHasPattern( + "def x = 1; def y = 2; double z = x + y", + "(?s).*INVOKEDYNAMIC add.*arguments:\\s+" + + "\\d+" + + ",\\s+" + + DefBootstrap.BINARY_OPERATOR + + ",\\s+" + + DefBootstrap.OPERATOR_ALLOWS_NULL + + ".*" + ); // a primitive argument is present: no null guard needed - assertBytecodeHasPattern("def x = 1; int y = 2; return x + y", - "(?s).*INVOKEDYNAMIC add.*arguments:\\s+" + "\\d+" - + ",\\s+" + DefBootstrap.BINARY_OPERATOR - + ",\\s+" + 0 + ".*"); - assertBytecodeHasPattern("int x = 1; def y = 2; return x + y", - "(?s).*INVOKEDYNAMIC add.*arguments:\\s+" + "\\d+" - + ",\\s+" + DefBootstrap.BINARY_OPERATOR - + ",\\s+" + 0 + ".*"); + assertBytecodeHasPattern( + "def x = 1; int y = 2; return x + y", + "(?s).*INVOKEDYNAMIC add.*arguments:\\s+" + "\\d+" + ",\\s+" + DefBootstrap.BINARY_OPERATOR + ",\\s+" + 0 + ".*" + ); + assertBytecodeHasPattern( + "int x = 1; def y = 2; return x + y", + "(?s).*INVOKEDYNAMIC add.*arguments:\\s+" + "\\d+" + ",\\s+" + DefBootstrap.BINARY_OPERATOR + ",\\s+" + 0 + ".*" + ); } public void testSubOptLHS() { - assertBytecodeExists("int x = 1; def y = 2; return x - y", - "INVOKEDYNAMIC sub(ILjava/lang/Object;)Ljava/lang/Object;"); + assertBytecodeExists("int x = 1; def y = 2; return x - y", "INVOKEDYNAMIC sub(ILjava/lang/Object;)Ljava/lang/Object;"); } public void testSubOptRHS() { - assertBytecodeExists("def x = 1; int y = 2; return x - y", - "INVOKEDYNAMIC sub(Ljava/lang/Object;I)Ljava/lang/Object;"); + assertBytecodeExists("def x = 1; int y = 2; return x - y", "INVOKEDYNAMIC sub(Ljava/lang/Object;I)Ljava/lang/Object;"); } public void testSubOptRet() { - assertBytecodeExists("def x = 1; def y = 2; double d = x - y", - "INVOKEDYNAMIC sub(Ljava/lang/Object;Ljava/lang/Object;)D"); + assertBytecodeExists("def x = 1; def y = 2; double d = x - y", "INVOKEDYNAMIC sub(Ljava/lang/Object;Ljava/lang/Object;)D"); } public void testLshOptLHS() { - assertBytecodeExists("int x = 1; def y = 2; return x << y", - "INVOKEDYNAMIC lsh(ILjava/lang/Object;)Ljava/lang/Object;"); + assertBytecodeExists("int x = 1; def y = 2; return x << y", "INVOKEDYNAMIC lsh(ILjava/lang/Object;)Ljava/lang/Object;"); } public void testLshOptRHS() { - assertBytecodeExists("def x = 1; int y = 2; return x << y", - "INVOKEDYNAMIC lsh(Ljava/lang/Object;I)Ljava/lang/Object;"); + assertBytecodeExists("def x = 1; int y = 2; return x << y", "INVOKEDYNAMIC lsh(Ljava/lang/Object;I)Ljava/lang/Object;"); } public void testLshOptRet() { - assertBytecodeExists("def x = 1; def y = 2; double d = x << y", - "INVOKEDYNAMIC lsh(Ljava/lang/Object;Ljava/lang/Object;)D"); + assertBytecodeExists("def x = 1; def y = 2; double d = x << y", "INVOKEDYNAMIC lsh(Ljava/lang/Object;Ljava/lang/Object;)D"); } public void testRshOptLHS() { - assertBytecodeExists("int x = 1; def y = 2; return x >> y", - "INVOKEDYNAMIC rsh(ILjava/lang/Object;)Ljava/lang/Object;"); + assertBytecodeExists("int x = 1; def y = 2; return x >> y", "INVOKEDYNAMIC rsh(ILjava/lang/Object;)Ljava/lang/Object;"); } public void testRshOptRHS() { - assertBytecodeExists("def x = 1; int y = 2; return x >> y", - "INVOKEDYNAMIC rsh(Ljava/lang/Object;I)Ljava/lang/Object;"); + assertBytecodeExists("def x = 1; int y = 2; return x >> y", "INVOKEDYNAMIC rsh(Ljava/lang/Object;I)Ljava/lang/Object;"); } public void testRshOptRet() { - assertBytecodeExists("def x = 1; def y = 2; double d = x >> y", - "INVOKEDYNAMIC rsh(Ljava/lang/Object;Ljava/lang/Object;)D"); + assertBytecodeExists("def x = 1; def y = 2; double d = x >> y", "INVOKEDYNAMIC rsh(Ljava/lang/Object;Ljava/lang/Object;)D"); } public void testUshOptLHS() { - assertBytecodeExists("int x = 1; def y = 2; return x >>> y", - "INVOKEDYNAMIC ush(ILjava/lang/Object;)Ljava/lang/Object;"); + assertBytecodeExists("int x = 1; def y = 2; return x >>> y", "INVOKEDYNAMIC ush(ILjava/lang/Object;)Ljava/lang/Object;"); } public void testUshOptRHS() { - assertBytecodeExists("def x = 1; int y = 2; return x >>> y", - "INVOKEDYNAMIC ush(Ljava/lang/Object;I)Ljava/lang/Object;"); + assertBytecodeExists("def x = 1; int y = 2; return x >>> y", "INVOKEDYNAMIC ush(Ljava/lang/Object;I)Ljava/lang/Object;"); } public void testUshOptRet() { - assertBytecodeExists("def x = 1; def y = 2; double d = x >>> y", - "INVOKEDYNAMIC ush(Ljava/lang/Object;Ljava/lang/Object;)D"); + assertBytecodeExists("def x = 1; def y = 2; double d = x >>> y", "INVOKEDYNAMIC ush(Ljava/lang/Object;Ljava/lang/Object;)D"); } public void testAndOptLHS() { - assertBytecodeExists("int x = 1; def y = 2; return x & y", - "INVOKEDYNAMIC and(ILjava/lang/Object;)Ljava/lang/Object;"); + assertBytecodeExists("int x = 1; def y = 2; return x & y", "INVOKEDYNAMIC and(ILjava/lang/Object;)Ljava/lang/Object;"); } public void testAndOptRHS() { - assertBytecodeExists("def x = 1; int y = 2; return x & y", - "INVOKEDYNAMIC and(Ljava/lang/Object;I)Ljava/lang/Object;"); + assertBytecodeExists("def x = 1; int y = 2; return x & y", "INVOKEDYNAMIC and(Ljava/lang/Object;I)Ljava/lang/Object;"); } public void testAndOptRet() { - assertBytecodeExists("def x = 1; def y = 2; double d = x & y", - "INVOKEDYNAMIC and(Ljava/lang/Object;Ljava/lang/Object;)D"); + assertBytecodeExists("def x = 1; def y = 2; double d = x & y", "INVOKEDYNAMIC and(Ljava/lang/Object;Ljava/lang/Object;)D"); } public void testOrOptLHS() { - assertBytecodeExists("int x = 1; def y = 2; return x | y", - "INVOKEDYNAMIC or(ILjava/lang/Object;)Ljava/lang/Object;"); + assertBytecodeExists("int x = 1; def y = 2; return x | y", "INVOKEDYNAMIC or(ILjava/lang/Object;)Ljava/lang/Object;"); } public void testOrOptRHS() { - assertBytecodeExists("def x = 1; int y = 2; return x | y", - "INVOKEDYNAMIC or(Ljava/lang/Object;I)Ljava/lang/Object;"); + assertBytecodeExists("def x = 1; int y = 2; return x | y", "INVOKEDYNAMIC or(Ljava/lang/Object;I)Ljava/lang/Object;"); } public void testOrOptRet() { - assertBytecodeExists("def x = 1; def y = 2; double d = x | y", - "INVOKEDYNAMIC or(Ljava/lang/Object;Ljava/lang/Object;)D"); + assertBytecodeExists("def x = 1; def y = 2; double d = x | y", "INVOKEDYNAMIC or(Ljava/lang/Object;Ljava/lang/Object;)D"); } public void testXorOptLHS() { - assertBytecodeExists("int x = 1; def y = 2; return x ^ y", - "INVOKEDYNAMIC xor(ILjava/lang/Object;)Ljava/lang/Object;"); + assertBytecodeExists("int x = 1; def y = 2; return x ^ y", "INVOKEDYNAMIC xor(ILjava/lang/Object;)Ljava/lang/Object;"); } public void testXorOptRHS() { - assertBytecodeExists("def x = 1; int y = 2; return x ^ y", - "INVOKEDYNAMIC xor(Ljava/lang/Object;I)Ljava/lang/Object;"); + assertBytecodeExists("def x = 1; int y = 2; return x ^ y", "INVOKEDYNAMIC xor(Ljava/lang/Object;I)Ljava/lang/Object;"); } public void testXorOptRet() { - assertBytecodeExists("def x = 1; def y = 2; double d = x ^ y", - "INVOKEDYNAMIC xor(Ljava/lang/Object;Ljava/lang/Object;)D"); + assertBytecodeExists("def x = 1; def y = 2; double d = x ^ y", "INVOKEDYNAMIC xor(Ljava/lang/Object;Ljava/lang/Object;)D"); } public void testBooleanXorOptLHS() { - assertBytecodeExists("boolean x = true; def y = true; return x ^ y", - "INVOKEDYNAMIC xor(ZLjava/lang/Object;)Ljava/lang/Object;"); + assertBytecodeExists("boolean x = true; def y = true; return x ^ y", "INVOKEDYNAMIC xor(ZLjava/lang/Object;)Ljava/lang/Object;"); } public void testBooleanXorOptRHS() { - assertBytecodeExists("def x = true; boolean y = true; return x ^ y", - "INVOKEDYNAMIC xor(Ljava/lang/Object;Z)Ljava/lang/Object;"); + assertBytecodeExists("def x = true; boolean y = true; return x ^ y", "INVOKEDYNAMIC xor(Ljava/lang/Object;Z)Ljava/lang/Object;"); } public void testBooleanXorOptRet() { - assertBytecodeExists("def x = true; def y = true; boolean v = x ^ y", - "INVOKEDYNAMIC xor(Ljava/lang/Object;Ljava/lang/Object;)Z"); + assertBytecodeExists("def x = true; def y = true; boolean v = x ^ y", "INVOKEDYNAMIC xor(Ljava/lang/Object;Ljava/lang/Object;)Z"); } public void testLtOptLHS() { - assertBytecodeExists("int x = 1; def y = 2; return x < y", - "INVOKEDYNAMIC lt(ILjava/lang/Object;)Z"); + assertBytecodeExists("int x = 1; def y = 2; return x < y", "INVOKEDYNAMIC lt(ILjava/lang/Object;)Z"); } public void testLtOptRHS() { - assertBytecodeExists("def x = 1; int y = 2; return x < y", - "INVOKEDYNAMIC lt(Ljava/lang/Object;I)Z"); + assertBytecodeExists("def x = 1; int y = 2; return x < y", "INVOKEDYNAMIC lt(Ljava/lang/Object;I)Z"); } public void testLteOptLHS() { - assertBytecodeExists("int x = 1; def y = 2; return x <= y", - "INVOKEDYNAMIC lte(ILjava/lang/Object;)Z"); + assertBytecodeExists("int x = 1; def y = 2; return x <= y", "INVOKEDYNAMIC lte(ILjava/lang/Object;)Z"); } public void testLteOptRHS() { - assertBytecodeExists("def x = 1; int y = 2; return x <= y", - "INVOKEDYNAMIC lte(Ljava/lang/Object;I)Z"); + assertBytecodeExists("def x = 1; int y = 2; return x <= y", "INVOKEDYNAMIC lte(Ljava/lang/Object;I)Z"); } public void testEqOptLHS() { - assertBytecodeExists("int x = 1; def y = 2; return x == y", - "INVOKEDYNAMIC eq(ILjava/lang/Object;)Z"); + assertBytecodeExists("int x = 1; def y = 2; return x == y", "INVOKEDYNAMIC eq(ILjava/lang/Object;)Z"); } public void testEqOptRHS() { - assertBytecodeExists("def x = 1; int y = 2; return x == y", - "INVOKEDYNAMIC eq(Ljava/lang/Object;I)Z"); + assertBytecodeExists("def x = 1; int y = 2; return x == y", "INVOKEDYNAMIC eq(Ljava/lang/Object;I)Z"); } public void testNeqOptLHS() { - assertBytecodeExists("int x = 1; def y = 2; return x != y", - "INVOKEDYNAMIC eq(ILjava/lang/Object;)Z"); + assertBytecodeExists("int x = 1; def y = 2; return x != y", "INVOKEDYNAMIC eq(ILjava/lang/Object;)Z"); } public void testNeqOptRHS() { - assertBytecodeExists("def x = 1; int y = 2; return x != y", - "INVOKEDYNAMIC eq(Ljava/lang/Object;I)Z"); + assertBytecodeExists("def x = 1; int y = 2; return x != y", "INVOKEDYNAMIC eq(Ljava/lang/Object;I)Z"); } public void testGteOptLHS() { - assertBytecodeExists("int x = 1; def y = 2; return x >= y", - "INVOKEDYNAMIC gte(ILjava/lang/Object;)Z"); + assertBytecodeExists("int x = 1; def y = 2; return x >= y", "INVOKEDYNAMIC gte(ILjava/lang/Object;)Z"); } public void testGteOptRHS() { - assertBytecodeExists("def x = 1; int y = 2; return x >= y", - "INVOKEDYNAMIC gte(Ljava/lang/Object;I)Z"); + assertBytecodeExists("def x = 1; int y = 2; return x >= y", "INVOKEDYNAMIC gte(Ljava/lang/Object;I)Z"); } public void testGtOptLHS() { - assertBytecodeExists("int x = 1; def y = 2; return x > y", - "INVOKEDYNAMIC gt(ILjava/lang/Object;)Z"); + assertBytecodeExists("int x = 1; def y = 2; return x > y", "INVOKEDYNAMIC gt(ILjava/lang/Object;)Z"); } public void testGtOptRHS() { - assertBytecodeExists("def x = 1; int y = 2; return x > y", - "INVOKEDYNAMIC gt(Ljava/lang/Object;I)Z"); + assertBytecodeExists("def x = 1; int y = 2; return x > y", "INVOKEDYNAMIC gt(Ljava/lang/Object;I)Z"); } public void testUnaryMinusOptRet() { - assertBytecodeExists("def x = 1; double y = -x; return y", - "INVOKEDYNAMIC neg(Ljava/lang/Object;)D"); + assertBytecodeExists("def x = 1; double y = -x; return y", "INVOKEDYNAMIC neg(Ljava/lang/Object;)D"); } public void testUnaryNotOptRet() { - assertBytecodeExists("def x = 1; double y = ~x; return y", - "INVOKEDYNAMIC not(Ljava/lang/Object;)D"); + assertBytecodeExists("def x = 1; double y = ~x; return y", "INVOKEDYNAMIC not(Ljava/lang/Object;)D"); } public void testUnaryPlusOptRet() { - assertBytecodeExists("def x = 1; double y = +x; return y", - "INVOKEDYNAMIC plus(Ljava/lang/Object;)D"); + assertBytecodeExists("def x = 1; double y = +x; return y", "INVOKEDYNAMIC plus(Ljava/lang/Object;)D"); } public void testLambdaReturnType() { - assertBytecodeExists("List l = new ArrayList(); l.removeIf(x -> x < 10)", - "synthetic lambda$synthetic$0(Ljava/lang/Object;)Z"); + assertBytecodeExists("List l = new ArrayList(); l.removeIf(x -> x < 10)", "synthetic lambda$synthetic$0(Ljava/lang/Object;)Z"); } public void testLambdaArguments() { - assertBytecodeExists("List l = new ArrayList(); l.stream().mapToDouble(Double::valueOf).map(x -> x + 1)", - "synthetic lambda$synthetic$0(D)D"); + assertBytecodeExists( + "List l = new ArrayList(); l.stream().mapToDouble(Double::valueOf).map(x -> x + 1)", + "synthetic lambda$synthetic$0(D)D" + ); } } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/DivisionTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/DivisionTests.java index af7cf241cd6f4..6859bec1b9a25 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/DivisionTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/DivisionTests.java @@ -9,7 +9,7 @@ package org.elasticsearch.painless; /** Tests for division operator across all types */ -//TODO: NaN/Inf/overflow/... +// TODO: NaN/Inf/overflow/... public class DivisionTests extends ScriptTestCase { // TODO: byte,short,char @@ -20,111 +20,103 @@ public void testBasics() throws Exception { } public void testInt() throws Exception { - assertEquals(1/1, exec("int x = 1; int y = 1; return x/y;")); - assertEquals(2/3, exec("int x = 2; int y = 3; return x/y;")); - assertEquals(5/10, exec("int x = 5; int y = 10; return x/y;")); - assertEquals(10/1/2, exec("int x = 10; int y = 1; int z = 2; return x/y/z;")); - assertEquals((10/1)/2, exec("int x = 10; int y = 1; int z = 2; return (x/y)/z;")); - assertEquals(10/(4/2), exec("int x = 10; int y = 4; int z = 2; return x/(y/z);")); - assertEquals(10/1, exec("int x = 10; int y = 1; return x/y;")); - assertEquals(0/1, exec("int x = 0; int y = 1; return x/y;")); + assertEquals(1 / 1, exec("int x = 1; int y = 1; return x/y;")); + assertEquals(2 / 3, exec("int x = 2; int y = 3; return x/y;")); + assertEquals(5 / 10, exec("int x = 5; int y = 10; return x/y;")); + assertEquals(10 / 1 / 2, exec("int x = 10; int y = 1; int z = 2; return x/y/z;")); + assertEquals((10 / 1) / 2, exec("int x = 10; int y = 1; int z = 2; return (x/y)/z;")); + assertEquals(10 / (4 / 2), exec("int x = 10; int y = 4; int z = 2; return x/(y/z);")); + assertEquals(10 / 1, exec("int x = 10; int y = 1; return x/y;")); + assertEquals(0 / 1, exec("int x = 0; int y = 1; return x/y;")); } public void testIntConst() throws Exception { - assertEquals(1/1, exec("return 1/1;")); - assertEquals(2/3, exec("return 2/3;")); - assertEquals(5/10, exec("return 5/10;")); - assertEquals(10/1/2, exec("return 10/1/2;")); - assertEquals((10/1)/2, exec("return (10/1)/2;")); - assertEquals(10/(4/2), exec("return 10/(4/2);")); - assertEquals(10/1, exec("return 10/1;")); - assertEquals(0/1, exec("return 0/1;")); + assertEquals(1 / 1, exec("return 1/1;")); + assertEquals(2 / 3, exec("return 2/3;")); + assertEquals(5 / 10, exec("return 5/10;")); + assertEquals(10 / 1 / 2, exec("return 10/1/2;")); + assertEquals((10 / 1) / 2, exec("return (10/1)/2;")); + assertEquals(10 / (4 / 2), exec("return 10/(4/2);")); + assertEquals(10 / 1, exec("return 10/1;")); + assertEquals(0 / 1, exec("return 0/1;")); } public void testLong() throws Exception { - assertEquals(1L/1L, exec("long x = 1; long y = 1; return x/y;")); - assertEquals(2L/3L, exec("long x = 2; long y = 3; return x/y;")); - assertEquals(5L/10L, exec("long x = 5; long y = 10; return x/y;")); - assertEquals(10L/1L/2L, exec("long x = 10; long y = 1; long z = 2; return x/y/z;")); - assertEquals((10L/1L)/2L, exec("long x = 10; long y = 1; long z = 2; return (x/y)/z;")); - assertEquals(10L/(4L/2L), exec("long x = 10; long y = 4; long z = 2; return x/(y/z);")); - assertEquals(10L/1L, exec("long x = 10; long y = 1; return x/y;")); - assertEquals(0L/1L, exec("long x = 0; long y = 1; return x/y;")); + assertEquals(1L / 1L, exec("long x = 1; long y = 1; return x/y;")); + assertEquals(2L / 3L, exec("long x = 2; long y = 3; return x/y;")); + assertEquals(5L / 10L, exec("long x = 5; long y = 10; return x/y;")); + assertEquals(10L / 1L / 2L, exec("long x = 10; long y = 1; long z = 2; return x/y/z;")); + assertEquals((10L / 1L) / 2L, exec("long x = 10; long y = 1; long z = 2; return (x/y)/z;")); + assertEquals(10L / (4L / 2L), exec("long x = 10; long y = 4; long z = 2; return x/(y/z);")); + assertEquals(10L / 1L, exec("long x = 10; long y = 1; return x/y;")); + assertEquals(0L / 1L, exec("long x = 0; long y = 1; return x/y;")); } public void testLongConst() throws Exception { - assertEquals(1L/1L, exec("return 1L/1L;")); - assertEquals(2L/3L, exec("return 2L/3L;")); - assertEquals(5L/10L, exec("return 5L/10L;")); - assertEquals(10L/1L/2L, exec("return 10L/1L/2L;")); - assertEquals((10L/1L)/2L, exec("return (10L/1L)/2L;")); - assertEquals(10L/(4L/2L), exec("return 10L/(4L/2L);")); - assertEquals(10L/1L, exec("return 10L/1L;")); - assertEquals(0L/1L, exec("return 0L/1L;")); + assertEquals(1L / 1L, exec("return 1L/1L;")); + assertEquals(2L / 3L, exec("return 2L/3L;")); + assertEquals(5L / 10L, exec("return 5L/10L;")); + assertEquals(10L / 1L / 2L, exec("return 10L/1L/2L;")); + assertEquals((10L / 1L) / 2L, exec("return (10L/1L)/2L;")); + assertEquals(10L / (4L / 2L), exec("return 10L/(4L/2L);")); + assertEquals(10L / 1L, exec("return 10L/1L;")); + assertEquals(0L / 1L, exec("return 0L/1L;")); } public void testFloat() throws Exception { - assertEquals(1F/1F, exec("float x = 1; float y = 1; return x/y;")); - assertEquals(2F/3F, exec("float x = 2; float y = 3; return x/y;")); - assertEquals(5F/10F, exec("float x = 5; float y = 10; return x/y;")); - assertEquals(10F/1F/2F, exec("float x = 10; float y = 1; float z = 2; return x/y/z;")); - assertEquals((10F/1F)/2F, exec("float x = 10; float y = 1; float z = 2; return (x/y)/z;")); - assertEquals(10F/(4F/2F), exec("float x = 10; float y = 4; float z = 2; return x/(y/z);")); - assertEquals(10F/1F, exec("float x = 10; float y = 1; return x/y;")); - assertEquals(0F/1F, exec("float x = 0; float y = 1; return x/y;")); + assertEquals(1F / 1F, exec("float x = 1; float y = 1; return x/y;")); + assertEquals(2F / 3F, exec("float x = 2; float y = 3; return x/y;")); + assertEquals(5F / 10F, exec("float x = 5; float y = 10; return x/y;")); + assertEquals(10F / 1F / 2F, exec("float x = 10; float y = 1; float z = 2; return x/y/z;")); + assertEquals((10F / 1F) / 2F, exec("float x = 10; float y = 1; float z = 2; return (x/y)/z;")); + assertEquals(10F / (4F / 2F), exec("float x = 10; float y = 4; float z = 2; return x/(y/z);")); + assertEquals(10F / 1F, exec("float x = 10; float y = 1; return x/y;")); + assertEquals(0F / 1F, exec("float x = 0; float y = 1; return x/y;")); } public void testFloatConst() throws Exception { - assertEquals(1F/1F, exec("return 1F/1F;")); - assertEquals(2F/3F, exec("return 2F/3F;")); - assertEquals(5F/10F, exec("return 5F/10F;")); - assertEquals(10F/1F/2F, exec("return 10F/1F/2F;")); - assertEquals((10F/1F)/2F, exec("return (10F/1F)/2F;")); - assertEquals(10F/(4F/2F), exec("return 10F/(4F/2F);")); - assertEquals(10F/1F, exec("return 10F/1F;")); - assertEquals(0F/1F, exec("return 0F/1F;")); + assertEquals(1F / 1F, exec("return 1F/1F;")); + assertEquals(2F / 3F, exec("return 2F/3F;")); + assertEquals(5F / 10F, exec("return 5F/10F;")); + assertEquals(10F / 1F / 2F, exec("return 10F/1F/2F;")); + assertEquals((10F / 1F) / 2F, exec("return (10F/1F)/2F;")); + assertEquals(10F / (4F / 2F), exec("return 10F/(4F/2F);")); + assertEquals(10F / 1F, exec("return 10F/1F;")); + assertEquals(0F / 1F, exec("return 0F/1F;")); } public void testDouble() throws Exception { - assertEquals(1.0/1.0, exec("double x = 1; double y = 1; return x/y;")); - assertEquals(2.0/3.0, exec("double x = 2; double y = 3; return x/y;")); - assertEquals(5.0/10.0, exec("double x = 5; double y = 10; return x/y;")); - assertEquals(10.0/1.0/2.0, exec("double x = 10; double y = 1; double z = 2; return x/y/z;")); - assertEquals((10.0/1.0)/2.0, exec("double x = 10; double y = 1; double z = 2; return (x/y)/z;")); - assertEquals(10.0/(4.0/2.0), exec("double x = 10; double y = 4; double z = 2; return x/(y/z);")); - assertEquals(10.0/1.0, exec("double x = 10; double y = 1; return x/y;")); - assertEquals(0.0/1.0, exec("double x = 0; double y = 1; return x/y;")); + assertEquals(1.0 / 1.0, exec("double x = 1; double y = 1; return x/y;")); + assertEquals(2.0 / 3.0, exec("double x = 2; double y = 3; return x/y;")); + assertEquals(5.0 / 10.0, exec("double x = 5; double y = 10; return x/y;")); + assertEquals(10.0 / 1.0 / 2.0, exec("double x = 10; double y = 1; double z = 2; return x/y/z;")); + assertEquals((10.0 / 1.0) / 2.0, exec("double x = 10; double y = 1; double z = 2; return (x/y)/z;")); + assertEquals(10.0 / (4.0 / 2.0), exec("double x = 10; double y = 4; double z = 2; return x/(y/z);")); + assertEquals(10.0 / 1.0, exec("double x = 10; double y = 1; return x/y;")); + assertEquals(0.0 / 1.0, exec("double x = 0; double y = 1; return x/y;")); } public void testDoubleConst() throws Exception { - assertEquals(1.0/1.0, exec("return 1.0/1.0;")); - assertEquals(2.0/3.0, exec("return 2.0/3.0;")); - assertEquals(5.0/10.0, exec("return 5.0/10.0;")); - assertEquals(10.0/1.0/2.0, exec("return 10.0/1.0/2.0;")); - assertEquals((10.0/1.0)/2.0, exec("return (10.0/1.0)/2.0;")); - assertEquals(10.0/(4.0/2.0), exec("return 10.0/(4.0/2.0);")); - assertEquals(10.0/1.0, exec("return 10.0/1.0;")); - assertEquals(0.0/1.0, exec("return 0.0/1.0;")); + assertEquals(1.0 / 1.0, exec("return 1.0/1.0;")); + assertEquals(2.0 / 3.0, exec("return 2.0/3.0;")); + assertEquals(5.0 / 10.0, exec("return 5.0/10.0;")); + assertEquals(10.0 / 1.0 / 2.0, exec("return 10.0/1.0/2.0;")); + assertEquals((10.0 / 1.0) / 2.0, exec("return (10.0/1.0)/2.0;")); + assertEquals(10.0 / (4.0 / 2.0), exec("return 10.0/(4.0/2.0);")); + assertEquals(10.0 / 1.0, exec("return 10.0/1.0;")); + assertEquals(0.0 / 1.0, exec("return 0.0/1.0;")); } public void testDivideByZero() throws Exception { - expectScriptThrows(ArithmeticException.class, () -> { - exec("int x = 1; int y = 0; return x / y;"); - }); + expectScriptThrows(ArithmeticException.class, () -> { exec("int x = 1; int y = 0; return x / y;"); }); - expectScriptThrows(ArithmeticException.class, () -> { - exec("long x = 1L; long y = 0L; return x / y;"); - }); + expectScriptThrows(ArithmeticException.class, () -> { exec("long x = 1L; long y = 0L; return x / y;"); }); } public void testDivideByZeroConst() throws Exception { - expectScriptThrows(ArithmeticException.class, () -> { - exec("return 1/0;"); - }); + expectScriptThrows(ArithmeticException.class, () -> { exec("return 1/0;"); }); - expectScriptThrows(ArithmeticException.class, () -> { - exec("return 1L/0L;"); - }); + expectScriptThrows(ArithmeticException.class, () -> { exec("return 1L/0L;"); }); } public void testDef() { @@ -373,33 +365,21 @@ public void testDefCompoundAssignment() { public void testCompoundAssignmentByZero() { // byte - expectScriptThrows(ArithmeticException.class, () -> { - exec("byte x = 1; x /= 0; return x;"); - }); + expectScriptThrows(ArithmeticException.class, () -> { exec("byte x = 1; x /= 0; return x;"); }); // short - expectScriptThrows(ArithmeticException.class, () -> { - exec("short x = 1; x /= 0; return x;"); - }); + expectScriptThrows(ArithmeticException.class, () -> { exec("short x = 1; x /= 0; return x;"); }); // char - expectScriptThrows(ArithmeticException.class, () -> { - exec("char x = 1; x /= 0; return x;"); - }); + expectScriptThrows(ArithmeticException.class, () -> { exec("char x = 1; x /= 0; return x;"); }); // int - expectScriptThrows(ArithmeticException.class, () -> { - exec("int x = 1; x /= 0; return x;"); - }); + expectScriptThrows(ArithmeticException.class, () -> { exec("int x = 1; x /= 0; return x;"); }); // long - expectScriptThrows(ArithmeticException.class, () -> { - exec("long x = 1; x /= 0; return x;"); - }); + expectScriptThrows(ArithmeticException.class, () -> { exec("long x = 1; x /= 0; return x;"); }); // def - expectScriptThrows(ArithmeticException.class, () -> { - exec("def x = 1; x /= 0; return x;"); - }); + expectScriptThrows(ArithmeticException.class, () -> { exec("def x = 1; x /= 0; return x;"); }); } } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/DynamicTypeTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/DynamicTypeTests.java index 6e46ebb3469d7..cdeea943782fe 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/DynamicTypeTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/DynamicTypeTests.java @@ -34,14 +34,11 @@ public interface DynI { } - public static class DynA { - } + public static class DynA {} - public static class DynB extends DynA implements DynI { - } + public static class DynB extends DynA implements DynI {} - public static class DynC extends DynB { - } + public static class DynC extends DynB {} public static class DynD extends DynB { public char letter() { @@ -55,8 +52,7 @@ public char letter() { } } - public static class DynF extends DynE { - } + public static class DynF extends DynE {} public static class DynG extends DynF { public char letter() { @@ -73,96 +69,144 @@ public void testDynamicTypeResolution() { assertEquals('E', exec("DynamicTypeTests.DynI i = new DynamicTypeTests.DynE(); return i.letter()")); assertEquals('E', exec("DynamicTypeTests.DynI i = new DynamicTypeTests.DynF(); return i.letter()")); assertEquals('G', exec("DynamicTypeTests.DynI i = new DynamicTypeTests.DynG(); return i.letter()")); - IllegalArgumentException iae = expectScriptThrows(IllegalArgumentException.class, - () -> exec("DynamicTypeTests.DynI i = new DynamicTypeTests.DynD(); return i.value()")); + IllegalArgumentException iae = expectScriptThrows( + IllegalArgumentException.class, + () -> exec("DynamicTypeTests.DynI i = new DynamicTypeTests.DynD(); return i.value()") + ); assertTrue(iae.getMessage().contains("dynamic method") && iae.getMessage().contains("not found")); - iae = expectScriptThrows(IllegalArgumentException.class, - () -> exec("DynamicTypeTests.DynI i = new DynamicTypeTests.DynE(); return i.value()")); + iae = expectScriptThrows( + IllegalArgumentException.class, + () -> exec("DynamicTypeTests.DynI i = new DynamicTypeTests.DynE(); return i.value()") + ); assertTrue(iae.getMessage().contains("dynamic method") && iae.getMessage().contains("not found")); - iae = expectScriptThrows(IllegalArgumentException.class, - () -> exec("DynamicTypeTests.DynI i = new DynamicTypeTests.DynF(); return i.value()")); + iae = expectScriptThrows( + IllegalArgumentException.class, + () -> exec("DynamicTypeTests.DynI i = new DynamicTypeTests.DynF(); return i.value()") + ); assertTrue(iae.getMessage().contains("dynamic method") && iae.getMessage().contains("not found")); assertEquals(1, exec("DynamicTypeTests.DynI i = new DynamicTypeTests.DynG(); return i.value()")); - iae = expectScriptThrows(IllegalArgumentException.class, - () -> exec("DynamicTypeTests.DynA a = new DynamicTypeTests.DynD(); return a.letter()")); + iae = expectScriptThrows( + IllegalArgumentException.class, + () -> exec("DynamicTypeTests.DynA a = new DynamicTypeTests.DynD(); return a.letter()") + ); assertTrue(iae.getMessage().contains("member method") && iae.getMessage().contains("not found")); - iae = expectScriptThrows(IllegalArgumentException.class, - () -> exec("DynamicTypeTests.DynA a = new DynamicTypeTests.DynE(); return a.letter()")); + iae = expectScriptThrows( + IllegalArgumentException.class, + () -> exec("DynamicTypeTests.DynA a = new DynamicTypeTests.DynE(); return a.letter()") + ); assertTrue(iae.getMessage().contains("member method") && iae.getMessage().contains("not found")); - iae = expectScriptThrows(IllegalArgumentException.class, - () -> exec("DynamicTypeTests.DynA a = new DynamicTypeTests.DynF(); return a.letter()")); + iae = expectScriptThrows( + IllegalArgumentException.class, + () -> exec("DynamicTypeTests.DynA a = new DynamicTypeTests.DynF(); return a.letter()") + ); assertTrue(iae.getMessage().contains("member method") && iae.getMessage().contains("not found")); - iae = expectScriptThrows(IllegalArgumentException.class, - () -> exec("DynamicTypeTests.DynA a = new DynamicTypeTests.DynG(); return a.letter()")); + iae = expectScriptThrows( + IllegalArgumentException.class, + () -> exec("DynamicTypeTests.DynA a = new DynamicTypeTests.DynG(); return a.letter()") + ); assertTrue(iae.getMessage().contains("member method") && iae.getMessage().contains("not found")); - iae = expectScriptThrows(IllegalArgumentException.class, - () -> exec("DynamicTypeTests.DynA a = new DynamicTypeTests.DynD(); return a.value()")); + iae = expectScriptThrows( + IllegalArgumentException.class, + () -> exec("DynamicTypeTests.DynA a = new DynamicTypeTests.DynD(); return a.value()") + ); assertTrue(iae.getMessage().contains("member method") && iae.getMessage().contains("not found")); - iae = expectScriptThrows(IllegalArgumentException.class, - () -> exec("DynamicTypeTests.DynA a = new DynamicTypeTests.DynE(); return a.value()")); + iae = expectScriptThrows( + IllegalArgumentException.class, + () -> exec("DynamicTypeTests.DynA a = new DynamicTypeTests.DynE(); return a.value()") + ); assertTrue(iae.getMessage().contains("member method") && iae.getMessage().contains("not found")); - iae = expectScriptThrows(IllegalArgumentException.class, - () -> exec("DynamicTypeTests.DynA a = new DynamicTypeTests.DynF(); return a.value()")); + iae = expectScriptThrows( + IllegalArgumentException.class, + () -> exec("DynamicTypeTests.DynA a = new DynamicTypeTests.DynF(); return a.value()") + ); assertTrue(iae.getMessage().contains("member method") && iae.getMessage().contains("not found")); - iae = expectScriptThrows(IllegalArgumentException.class, - () -> exec("DynamicTypeTests.DynA a = new DynamicTypeTests.DynG(); return a.value()")); + iae = expectScriptThrows( + IllegalArgumentException.class, + () -> exec("DynamicTypeTests.DynA a = new DynamicTypeTests.DynG(); return a.value()") + ); assertTrue(iae.getMessage().contains("member method") && iae.getMessage().contains("not found")); assertEquals('D', exec("DynamicTypeTests.DynB b = new DynamicTypeTests.DynD(); return b.letter()")); assertEquals('E', exec("DynamicTypeTests.DynB b = new DynamicTypeTests.DynE(); return b.letter()")); assertEquals('E', exec("DynamicTypeTests.DynB b = new DynamicTypeTests.DynF(); return b.letter()")); assertEquals('G', exec("DynamicTypeTests.DynB b = new DynamicTypeTests.DynG(); return b.letter()")); - iae = expectScriptThrows(IllegalArgumentException.class, - () -> exec("DynamicTypeTests.DynB b = new DynamicTypeTests.DynD(); return b.value()")); + iae = expectScriptThrows( + IllegalArgumentException.class, + () -> exec("DynamicTypeTests.DynB b = new DynamicTypeTests.DynD(); return b.value()") + ); assertTrue(iae.getMessage().contains("dynamic method") && iae.getMessage().contains("not found")); - iae = expectScriptThrows(IllegalArgumentException.class, - () -> exec("DynamicTypeTests.DynB b = new DynamicTypeTests.DynE(); return b.value()")); + iae = expectScriptThrows( + IllegalArgumentException.class, + () -> exec("DynamicTypeTests.DynB b = new DynamicTypeTests.DynE(); return b.value()") + ); assertTrue(iae.getMessage().contains("dynamic method") && iae.getMessage().contains("not found")); - iae = expectScriptThrows(IllegalArgumentException.class, - () -> exec("DynamicTypeTests.DynB b = new DynamicTypeTests.DynF(); return b.value()")); + iae = expectScriptThrows( + IllegalArgumentException.class, + () -> exec("DynamicTypeTests.DynB b = new DynamicTypeTests.DynF(); return b.value()") + ); assertTrue(iae.getMessage().contains("dynamic method") && iae.getMessage().contains("not found")); assertEquals(1, exec("DynamicTypeTests.DynB b = new DynamicTypeTests.DynG(); return b.value()")); - iae = expectScriptThrows(IllegalArgumentException.class, - () -> exec("DynamicTypeTests.DynC c = new DynamicTypeTests.DynE(); return c.letter()")); + iae = expectScriptThrows( + IllegalArgumentException.class, + () -> exec("DynamicTypeTests.DynC c = new DynamicTypeTests.DynE(); return c.letter()") + ); assertTrue(iae.getMessage().contains("member method") && iae.getMessage().contains("not found")); - iae = expectScriptThrows(IllegalArgumentException.class, - () -> exec("DynamicTypeTests.DynC c = new DynamicTypeTests.DynF(); return c.letter()")); + iae = expectScriptThrows( + IllegalArgumentException.class, + () -> exec("DynamicTypeTests.DynC c = new DynamicTypeTests.DynF(); return c.letter()") + ); assertTrue(iae.getMessage().contains("member method") && iae.getMessage().contains("not found")); - iae = expectScriptThrows(IllegalArgumentException.class, - () -> exec("DynamicTypeTests.DynC c = new DynamicTypeTests.DynG(); return c.letter()")); + iae = expectScriptThrows( + IllegalArgumentException.class, + () -> exec("DynamicTypeTests.DynC c = new DynamicTypeTests.DynG(); return c.letter()") + ); assertTrue(iae.getMessage().contains("member method") && iae.getMessage().contains("not found")); - iae = expectScriptThrows(IllegalArgumentException.class, - () -> exec("DynamicTypeTests.DynC c = new DynamicTypeTests.DynE(); return c.value()")); + iae = expectScriptThrows( + IllegalArgumentException.class, + () -> exec("DynamicTypeTests.DynC c = new DynamicTypeTests.DynE(); return c.value()") + ); assertTrue(iae.getMessage().contains("member method") && iae.getMessage().contains("not found")); - iae = expectScriptThrows(IllegalArgumentException.class, - () -> exec("DynamicTypeTests.DynC c = new DynamicTypeTests.DynF(); return c.value()")); + iae = expectScriptThrows( + IllegalArgumentException.class, + () -> exec("DynamicTypeTests.DynC c = new DynamicTypeTests.DynF(); return c.value()") + ); assertTrue(iae.getMessage().contains("member method") && iae.getMessage().contains("not found")); - iae = expectScriptThrows(IllegalArgumentException.class, - () -> exec("DynamicTypeTests.DynC c = new DynamicTypeTests.DynG(); return c.value()")); + iae = expectScriptThrows( + IllegalArgumentException.class, + () -> exec("DynamicTypeTests.DynC c = new DynamicTypeTests.DynG(); return c.value()") + ); assertTrue(iae.getMessage().contains("member method") && iae.getMessage().contains("not found")); assertEquals('D', exec("DynamicTypeTests.DynD d = new DynamicTypeTests.DynD(); return d.letter()")); - iae = expectScriptThrows(IllegalArgumentException.class, - () -> exec("DynamicTypeTests.DynD d = new DynamicTypeTests.DynD(); return d.value()")); + iae = expectScriptThrows( + IllegalArgumentException.class, + () -> exec("DynamicTypeTests.DynD d = new DynamicTypeTests.DynD(); return d.value()") + ); assertTrue(iae.getMessage().contains("member method") && iae.getMessage().contains("not found")); assertEquals('E', exec("DynamicTypeTests.DynE e = new DynamicTypeTests.DynE(); return e.letter()")); assertEquals('E', exec("DynamicTypeTests.DynE e = new DynamicTypeTests.DynF(); return e.letter()")); assertEquals('G', exec("DynamicTypeTests.DynE e = new DynamicTypeTests.DynG(); return e.letter()")); - iae = expectScriptThrows(IllegalArgumentException.class, - () -> exec("DynamicTypeTests.DynE e = new DynamicTypeTests.DynE(); return e.value()")); + iae = expectScriptThrows( + IllegalArgumentException.class, + () -> exec("DynamicTypeTests.DynE e = new DynamicTypeTests.DynE(); return e.value()") + ); assertTrue(iae.getMessage().contains("dynamic method") && iae.getMessage().contains("not found")); - iae = expectScriptThrows(IllegalArgumentException.class, - () -> exec("DynamicTypeTests.DynE e = new DynamicTypeTests.DynF(); return e.value()")); + iae = expectScriptThrows( + IllegalArgumentException.class, + () -> exec("DynamicTypeTests.DynE e = new DynamicTypeTests.DynF(); return e.value()") + ); assertTrue(iae.getMessage().contains("dynamic method") && iae.getMessage().contains("not found")); assertEquals(1, exec("DynamicTypeTests.DynE e = new DynamicTypeTests.DynG(); return e.value()")); assertEquals('E', exec("DynamicTypeTests.DynF f = new DynamicTypeTests.DynF(); return f.letter()")); assertEquals('G', exec("DynamicTypeTests.DynF f = new DynamicTypeTests.DynG(); return f.letter()")); - iae = expectScriptThrows(IllegalArgumentException.class, - () -> exec("DynamicTypeTests.DynF f = new DynamicTypeTests.DynF(); return f.value()")); + iae = expectScriptThrows( + IllegalArgumentException.class, + () -> exec("DynamicTypeTests.DynF f = new DynamicTypeTests.DynF(); return f.value()") + ); assertTrue(iae.getMessage().contains("dynamic method") && iae.getMessage().contains("not found")); assertEquals(1, exec("DynamicTypeTests.DynF f = new DynamicTypeTests.DynG(); return f.value()")); diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/ElvisTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/ElvisTests.java index ffd6e5961112c..a4d65d9d8c7af 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/ElvisTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/ElvisTests.java @@ -53,7 +53,7 @@ public void testBasics() { assertEquals(2, exec("return params.a + 1 ?: 2 + 2", singletonMap("a", 1), true)); // Yes, this is silly, but it should be valid // Weird casts - assertEquals(1, exec("int i = params.i; String s = params.s; return s ?: i", singletonMap("i", 1), true)); + assertEquals(1, exec("int i = params.i; String s = params.s; return s ?: i", singletonMap("i", 1), true)); assertEquals("str", exec("Integer i = params.i; String s = params.s; return s ?: i", singletonMap("s", "str"), true)); // Combining @@ -74,8 +74,10 @@ public void testWithNullSafeDereferences() { public void testLazy() { assertEquals(1, exec("def fail() {throw new RuntimeException('test')} return params.a ?: fail()", singletonMap("a", 1), true)); - Exception e = expectScriptThrows(RuntimeException.class, () -> - exec("def fail() {throw new RuntimeException('test')} return params.a ?: fail()")); + Exception e = expectScriptThrows( + RuntimeException.class, + () -> exec("def fail() {throw new RuntimeException('test')} return params.a ?: fail()") + ); assertEquals(e.getMessage(), "test"); } @@ -97,8 +99,10 @@ private void checkOneBranch(String code, boolean expectOneBranch) { assertThat(disassembled, firstLookup, greaterThan(-1)); int firstElvisDestinationLabelIndex = disassembled.indexOf("IFNONNULL L", firstLookup); assertThat(disassembled, firstElvisDestinationLabelIndex, greaterThan(-1)); - String firstElvisDestinationLabel = disassembled.substring(firstElvisDestinationLabelIndex + "IFNONNULL ".length(), - disassembled.indexOf('\n', firstElvisDestinationLabelIndex)); + String firstElvisDestinationLabel = disassembled.substring( + firstElvisDestinationLabelIndex + "IFNONNULL ".length(), + disassembled.indexOf('\n', firstElvisDestinationLabelIndex) + ); int firstElvisDestionation = disassembled.indexOf(" " + firstElvisDestinationLabel); assertThat(disassembled, firstElvisDestionation, greaterThan(-1)); int ifAfterFirstElvisDestination = disassembled.indexOf("IF", firstElvisDestionation); diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/EmitTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/EmitTests.java index 06f75d17ddca9..344d4aaa822dc 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/EmitTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/EmitTests.java @@ -38,12 +38,12 @@ public TestFieldScript exec(String script) { public void testEmit() { TestFieldScript script = exec("emit(1L)"); assertNotNull(script); - assertArrayEquals(new long[]{1L}, script.fetchValues()); + assertArrayEquals(new long[] { 1L }, script.fetchValues()); } public void testEmitFromUserFunction() { TestFieldScript script = exec("void doEmit(long l) { emit(l) } doEmit(1L); doEmit(100L)"); assertNotNull(script); - assertArrayEquals(new long[]{1L, 100L}, script.fetchValues()); + assertArrayEquals(new long[] { 1L, 100L }, script.fetchValues()); } } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/EqualsTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/EqualsTests.java index 56b303c0d03eb..f2fa22ddff522 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/EqualsTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/EqualsTests.java @@ -156,18 +156,18 @@ public void testNotEqualsDefAndPrimitive() { * we can never be sure that the JVM hasn't configured itself to cache that Integer. It is sneaky like that. */ int uncachedAutoboxedInt = randomValueOtherThanMany(i -> Integer.valueOf(i) == Integer.valueOf(i), ESTestCase::randomInt); assertEquals(false, exec("def x = params.i; int y = params.i; return x != y;", singletonMap("i", uncachedAutoboxedInt), true)); - assertEquals(true, exec("def x = params.i; int y = params.i; return x !== y;", singletonMap("i", uncachedAutoboxedInt), true)); + assertEquals(true, exec("def x = params.i; int y = params.i; return x !== y;", singletonMap("i", uncachedAutoboxedInt), true)); assertEquals(false, exec("def x = params.i; int y = params.i; return y != x;", singletonMap("i", uncachedAutoboxedInt), true)); - assertEquals(true, exec("def x = params.i; int y = params.i; return y !== x;", singletonMap("i", uncachedAutoboxedInt), true)); + assertEquals(true, exec("def x = params.i; int y = params.i; return y !== x;", singletonMap("i", uncachedAutoboxedInt), true)); /* Now check that we use valueOf with the boxing used for comparing primitives to def. For this we need an * integer that is cached by Integer.valueOf. The JLS says 0 should always be cached. */ int cachedAutoboxedInt = 0; assertSame(Integer.valueOf(cachedAutoboxedInt), Integer.valueOf(cachedAutoboxedInt)); assertEquals(false, exec("def x = params.i; int y = params.i; return x != y;", singletonMap("i", cachedAutoboxedInt), true)); - assertEquals(false, exec("def x = params.i; int y = params.i; return x !== y;", singletonMap("i", cachedAutoboxedInt), true)); + assertEquals(false, exec("def x = params.i; int y = params.i; return x !== y;", singletonMap("i", cachedAutoboxedInt), true)); assertEquals(false, exec("def x = params.i; int y = params.i; return y != x;", singletonMap("i", cachedAutoboxedInt), true)); - assertEquals(false, exec("def x = params.i; int y = params.i; return y !== x;", singletonMap("i", cachedAutoboxedInt), true)); + assertEquals(false, exec("def x = params.i; int y = params.i; return y !== x;", singletonMap("i", cachedAutoboxedInt), true)); } public void testRightHandNull() { diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/FactoryTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/FactoryTests.java index 40ab60ffd1045..eb1a665327258 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/FactoryTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/FactoryTests.java @@ -40,8 +40,8 @@ public abstract static class StatefulFactoryTestScript { private final int y; public StatefulFactoryTestScript(int x, int y, int a, int b) { - this.x = x*a; - this.y = y*b; + this.x = x * a; + this.y = y * b; } public int getX() { @@ -49,7 +49,7 @@ public int getX() { } public int getY() { - return y*2; + return y * 2; } public int getC() { @@ -60,22 +60,31 @@ public int getD() { return 2; } - public static final String[] PARAMETERS = new String[] {"test"}; + public static final String[] PARAMETERS = new String[] { "test" }; + public abstract Object execute(int test); public abstract boolean needsTest(); + public abstract boolean needsNothing(); + public abstract boolean needsX(); + public abstract boolean needsC(); + public abstract boolean needsD(); public interface StatefulFactory { StatefulFactoryTestScript newInstance(int a, int b); boolean needsTest(); + boolean needsNothing(); + boolean needsX(); + boolean needsC(); + boolean needsD(); } @@ -83,19 +92,29 @@ public interface Factory { StatefulFactory newFactory(int x, int y); boolean needsTest(); + boolean needsNothing(); + boolean needsX(); + boolean needsC(); + boolean needsD(); } - public static final ScriptContext CONTEXT = - new ScriptContext<>("test", StatefulFactoryTestScript.Factory.class); + public static final ScriptContext CONTEXT = new ScriptContext<>( + "test", + StatefulFactoryTestScript.Factory.class + ); } public void testStatefulFactory() { StatefulFactoryTestScript.Factory factory = scriptEngine.compile( - "stateful_factory_test", "test + x + y + d", StatefulFactoryTestScript.CONTEXT, Collections.emptyMap()); + "stateful_factory_test", + "test + x + y + d", + StatefulFactoryTestScript.CONTEXT, + Collections.emptyMap() + ); StatefulFactoryTestScript.StatefulFactory statefulFactory = factory.newFactory(1, 2); StatefulFactoryTestScript script = statefulFactory.newInstance(3, 4); assertEquals(24, script.execute(3)); @@ -129,18 +148,19 @@ public Map getParams() { return params; } - public static final String[] PARAMETERS = new String[] {"test"}; + public static final String[] PARAMETERS = new String[] { "test" }; + public abstract Object execute(int test); public interface Factory { FactoryTestScript newInstance(Map params); boolean needsTest(); + boolean needsNothing(); } - public static final ScriptContext CONTEXT = - new ScriptContext<>("test", FactoryTestScript.Factory.class); + public static final ScriptContext CONTEXT = new ScriptContext<>("test", FactoryTestScript.Factory.class); } public abstract static class DeterministicFactoryTestScript { @@ -154,23 +174,31 @@ public Map getParams() { return params; } - public static final String[] PARAMETERS = new String[] {"test"}; + public static final String[] PARAMETERS = new String[] { "test" }; + public abstract Object execute(int test); - public interface Factory extends ScriptFactory{ + public interface Factory extends ScriptFactory { FactoryTestScript newInstance(Map params); boolean needsTest(); + boolean needsNothing(); } - public static final ScriptContext CONTEXT = - new ScriptContext<>("test", DeterministicFactoryTestScript.Factory.class); + public static final ScriptContext CONTEXT = new ScriptContext<>( + "test", + DeterministicFactoryTestScript.Factory.class + ); } public void testFactory() { - FactoryTestScript.Factory factory = - scriptEngine.compile("factory_test", "test + params.get('test')", FactoryTestScript.CONTEXT, Collections.emptyMap()); + FactoryTestScript.Factory factory = scriptEngine.compile( + "factory_test", + "test + params.get('test')", + FactoryTestScript.CONTEXT, + Collections.emptyMap() + ); FactoryTestScript script = factory.newInstance(Collections.singletonMap("test", 2)); assertEquals(4, script.execute(2)); assertEquals(5, script.execute(3)); @@ -182,41 +210,50 @@ public void testFactory() { } public void testDeterministic() { - DeterministicFactoryTestScript.Factory factory = - scriptEngine.compile("deterministic_test", "Integer.parseInt('123')", - DeterministicFactoryTestScript.CONTEXT, Collections.emptyMap()); + DeterministicFactoryTestScript.Factory factory = scriptEngine.compile( + "deterministic_test", + "Integer.parseInt('123')", + DeterministicFactoryTestScript.CONTEXT, + Collections.emptyMap() + ); assertTrue(factory.isResultDeterministic()); assertEquals(123, factory.newInstance(Collections.emptyMap()).execute(0)); } public void testNotDeterministic() { - DeterministicFactoryTestScript.Factory factory = - scriptEngine.compile("not_deterministic_test", "Math.random()", - DeterministicFactoryTestScript.CONTEXT, Collections.emptyMap()); + DeterministicFactoryTestScript.Factory factory = scriptEngine.compile( + "not_deterministic_test", + "Math.random()", + DeterministicFactoryTestScript.CONTEXT, + Collections.emptyMap() + ); assertFalse(factory.isResultDeterministic()); - Double d = (Double)factory.newInstance(Collections.emptyMap()).execute(0); + Double d = (Double) factory.newInstance(Collections.emptyMap()).execute(0); assertTrue(d >= 0.0 && d <= 1.0); } public void testMixedDeterministicIsNotDeterministic() { - DeterministicFactoryTestScript.Factory factory = - scriptEngine.compile("not_deterministic_test", "Integer.parseInt('123') + Math.random()", - DeterministicFactoryTestScript.CONTEXT, Collections.emptyMap()); + DeterministicFactoryTestScript.Factory factory = scriptEngine.compile( + "not_deterministic_test", + "Integer.parseInt('123') + Math.random()", + DeterministicFactoryTestScript.CONTEXT, + Collections.emptyMap() + ); assertFalse(factory.isResultDeterministic()); - Double d = (Double)factory.newInstance(Collections.emptyMap()).execute(0); + Double d = (Double) factory.newInstance(Collections.emptyMap()).execute(0); assertTrue(d >= 123.0 && d <= 124.0); } public abstract static class EmptyTestScript { public static final String[] PARAMETERS = {}; + public abstract Object execute(); public interface Factory { EmptyTestScript newInstance(); } - public static final ScriptContext CONTEXT = - new ScriptContext<>("test", EmptyTestScript.Factory.class); + public static final ScriptContext CONTEXT = new ScriptContext<>("test", EmptyTestScript.Factory.class); } public void testEmpty() { @@ -230,8 +267,12 @@ public void testEmpty() { } public void testTemplate() { - TemplateScript.Factory factory = - scriptEngine.compile("template_test", "params['test']", TemplateScript.CONTEXT, Collections.emptyMap()); + TemplateScript.Factory factory = scriptEngine.compile( + "template_test", + "params['test']", + TemplateScript.CONTEXT, + Collections.emptyMap() + ); TemplateScript script = factory.newInstance(Collections.singletonMap("test", "abc")); assertEquals("abc", script.execute()); assertEquals("abc", script.execute()); @@ -241,33 +282,42 @@ public void testTemplate() { } public void testGetterInLambda() { - FactoryTestScript.Factory factory = - scriptEngine.compile("template_test", - "IntSupplier createLambda(IntSupplier s) { return s; } createLambda(() -> params['x'] + test).getAsInt()", - FactoryTestScript.CONTEXT, Collections.emptyMap()); + FactoryTestScript.Factory factory = scriptEngine.compile( + "template_test", + "IntSupplier createLambda(IntSupplier s) { return s; } createLambda(() -> params['x'] + test).getAsInt()", + FactoryTestScript.CONTEXT, + Collections.emptyMap() + ); FactoryTestScript script = factory.newInstance(Collections.singletonMap("x", 1)); assertEquals(2, script.execute(1)); } public abstract static class VoidReturnTestScript { - public static final String[] PARAMETERS = {"map"}; + public static final String[] PARAMETERS = { "map" }; + public abstract void execute(Map map); public interface Factory { VoidReturnTestScript newInstance(); } - public static final ScriptContext CONTEXT = - new ScriptContext<>("test", VoidReturnTestScript.Factory.class); + public static final ScriptContext CONTEXT = new ScriptContext<>( + "test", + VoidReturnTestScript.Factory.class + ); } public void testVoidReturn() { scriptEngine.compile("void_return_test", "int x = 1 + 1; return;", VoidReturnTestScript.CONTEXT, Collections.emptyMap()); - IllegalArgumentException iae = expectScriptThrows(IllegalArgumentException.class, () -> - scriptEngine.compile("void_return_test", "1 + 1", VoidReturnTestScript.CONTEXT, Collections.emptyMap())); + IllegalArgumentException iae = expectScriptThrows( + IllegalArgumentException.class, + () -> scriptEngine.compile("void_return_test", "1 + 1", VoidReturnTestScript.CONTEXT, Collections.emptyMap()) + ); assertEquals(iae.getMessage(), "not a statement: result not used from addition operation [+]"); - ClassCastException cce = expectScriptThrows(ClassCastException.class, () -> - scriptEngine.compile("void_return_test", "def x = 1; return x;", VoidReturnTestScript.CONTEXT, Collections.emptyMap())); + ClassCastException cce = expectScriptThrows( + ClassCastException.class, + () -> scriptEngine.compile("void_return_test", "def x = 1; return x;", VoidReturnTestScript.CONTEXT, Collections.emptyMap()) + ); assertEquals(cce.getMessage(), "Cannot cast from [def] to [void]."); } @@ -282,27 +332,30 @@ public Map getParams() { return params; } - public static final String[] PARAMETERS = new String[] {"test"}; + public static final String[] PARAMETERS = new String[] { "test" }; + public abstract long[] execute(int test); public interface Factory { FactoryTestConverterScript newInstance(Map params); } - public static final ScriptContext CONTEXT = - new ScriptContext<>("test", FactoryTestConverterScript.Factory.class); + public static final ScriptContext CONTEXT = new ScriptContext<>( + "test", + FactoryTestConverterScript.Factory.class + ); public static long[] convertFromInt(int i) { - return new long[]{i}; + return new long[] { i }; } public static long[] convertFromString(String s) { - return new long[]{Long.parseLong(s)}; + return new long[] { Long.parseLong(s) }; } public static long[] convertFromList(List l) { long[] converted = new long[l.size()]; - for (int i=0; i < l.size(); i++) { + for (int i = 0; i < l.size(); i++) { Object o = l.get(i); if (o instanceof Long) { converted[i] = (Long) o; @@ -317,7 +370,7 @@ public static long[] convertFromList(List l) { public static long[] convertFromDef(Object def) { if (def instanceof String) { - return convertFromString((String)def); + return convertFromString((String) def); } else if (def instanceof Integer) { return convertFromInt(((Integer) def).intValue()); } else if (def instanceof List) { @@ -325,120 +378,126 @@ public static long[] convertFromDef(Object def) { } else { return (long[]) def; } - //throw new ClassCastException("Cannot convert [" + def + "] to long[]"); + // throw new ClassCastException("Cannot convert [" + def + "] to long[]"); } } - public void testConverterFactory() { - FactoryTestConverterScript.Factory factory = - scriptEngine.compile("converter_test", - "return test;", - FactoryTestConverterScript.CONTEXT, Collections.emptyMap()); + FactoryTestConverterScript.Factory factory = scriptEngine.compile( + "converter_test", + "return test;", + FactoryTestConverterScript.CONTEXT, + Collections.emptyMap() + ); FactoryTestConverterScript script = factory.newInstance(Collections.singletonMap("test", 2)); - assertArrayEquals(new long[]{2}, script.execute(2)); + assertArrayEquals(new long[] { 2 }, script.execute(2)); script = factory.newInstance(Collections.singletonMap("test", 3)); - assertArrayEquals(new long[]{3}, script.execute(3)); + assertArrayEquals(new long[] { 3 }, script.execute(3)); - factory = scriptEngine.compile("converter_test", - "return test + 1;", - FactoryTestConverterScript.CONTEXT, Collections.emptyMap()); + factory = scriptEngine.compile("converter_test", "return test + 1;", FactoryTestConverterScript.CONTEXT, Collections.emptyMap()); script = factory.newInstance(Collections.singletonMap("test", 2)); - assertArrayEquals(new long[]{1001}, script.execute(1000)); + assertArrayEquals(new long[] { 1001 }, script.execute(1000)); - factory = scriptEngine.compile("converter_test", - "return '100';", - FactoryTestConverterScript.CONTEXT, Collections.emptyMap()); + factory = scriptEngine.compile("converter_test", "return '100';", FactoryTestConverterScript.CONTEXT, Collections.emptyMap()); script = factory.newInstance(Collections.singletonMap("test", 2)); - assertArrayEquals(new long[]{100}, script.execute(1000)); + assertArrayEquals(new long[] { 100 }, script.execute(1000)); - factory = scriptEngine.compile("converter_test", + factory = scriptEngine.compile( + "converter_test", "long[] a = new long[]{test, 123}; return a;", - FactoryTestConverterScript.CONTEXT, Collections.emptyMap()); + FactoryTestConverterScript.CONTEXT, + Collections.emptyMap() + ); script = factory.newInstance(Collections.singletonMap("test", 2)); - assertArrayEquals(new long[]{1000, 123}, script.execute(1000)); + assertArrayEquals(new long[] { 1000, 123 }, script.execute(1000)); - factory = scriptEngine.compile("converter_test", - "return [test, 123];", - FactoryTestConverterScript.CONTEXT, Collections.emptyMap()); + factory = scriptEngine.compile("converter_test", "return [test, 123];", FactoryTestConverterScript.CONTEXT, Collections.emptyMap()); script = factory.newInstance(Collections.singletonMap("test", 2)); - assertArrayEquals(new long[]{1000, 123}, script.execute(1000)); + assertArrayEquals(new long[] { 1000, 123 }, script.execute(1000)); - factory = scriptEngine.compile("converter_test", + factory = scriptEngine.compile( + "converter_test", "ArrayList a = new ArrayList(); a.add(test); a.add(456); a.add('789'); return a;", - FactoryTestConverterScript.CONTEXT, Collections.emptyMap()); + FactoryTestConverterScript.CONTEXT, + Collections.emptyMap() + ); script = factory.newInstance(Collections.singletonMap("test", 2)); - assertArrayEquals(new long[]{123, 456, 789}, script.execute(123)); + assertArrayEquals(new long[] { 123, 456, 789 }, script.execute(123)); // autoreturn, no converter - factory = scriptEngine.compile("converter_test", - "new long[]{test}", - FactoryTestConverterScript.CONTEXT, Collections.emptyMap()); + factory = scriptEngine.compile("converter_test", "new long[]{test}", FactoryTestConverterScript.CONTEXT, Collections.emptyMap()); script = factory.newInstance(Collections.singletonMap("test", 2)); - assertArrayEquals(new long[]{123}, script.execute(123)); + assertArrayEquals(new long[] { 123 }, script.execute(123)); // autoreturn, converter - factory = scriptEngine.compile("converter_test", - "test", - FactoryTestConverterScript.CONTEXT, Collections.emptyMap()); + factory = scriptEngine.compile("converter_test", "test", FactoryTestConverterScript.CONTEXT, Collections.emptyMap()); script = factory.newInstance(Collections.singletonMap("test", 2)); - assertArrayEquals(new long[]{456}, script.execute(456)); + assertArrayEquals(new long[] { 456 }, script.execute(456)); - factory = scriptEngine.compile("converter_test", - "'1001'", - FactoryTestConverterScript.CONTEXT, Collections.emptyMap()); + factory = scriptEngine.compile("converter_test", "'1001'", FactoryTestConverterScript.CONTEXT, Collections.emptyMap()); script = factory.newInstance(Collections.singletonMap("test", 2)); - assertArrayEquals(new long[]{1001}, script.execute(456)); + assertArrayEquals(new long[] { 1001 }, script.execute(456)); // def tests - factory = scriptEngine.compile("converter_test", + factory = scriptEngine.compile( + "converter_test", "def a = new long[]{test, 123}; return a;", - FactoryTestConverterScript.CONTEXT, Collections.emptyMap()); + FactoryTestConverterScript.CONTEXT, + Collections.emptyMap() + ); script = factory.newInstance(Collections.singletonMap("test", 2)); - assertArrayEquals(new long[]{1000, 123}, script.execute(1000)); + assertArrayEquals(new long[] { 1000, 123 }, script.execute(1000)); - factory = scriptEngine.compile("converter_test", + factory = scriptEngine.compile( + "converter_test", "def l = [test, 123]; l;", - FactoryTestConverterScript.CONTEXT, Collections.emptyMap()); + FactoryTestConverterScript.CONTEXT, + Collections.emptyMap() + ); script = factory.newInstance(Collections.singletonMap("test", 2)); - assertArrayEquals(new long[]{1000, 123}, script.execute(1000)); + assertArrayEquals(new long[] { 1000, 123 }, script.execute(1000)); - factory = scriptEngine.compile("converter_test", + factory = scriptEngine.compile( + "converter_test", "def a = new ArrayList(); a.add(test); a.add(456); a.add('789'); return a;", - FactoryTestConverterScript.CONTEXT, Collections.emptyMap()); + FactoryTestConverterScript.CONTEXT, + Collections.emptyMap() + ); script = factory.newInstance(Collections.singletonMap("test", 2)); - assertArrayEquals(new long[]{123, 456, 789}, script.execute(123)); + assertArrayEquals(new long[] { 123, 456, 789 }, script.execute(123)); // autoreturn, no converter - factory = scriptEngine.compile("converter_test", + factory = scriptEngine.compile( + "converter_test", "def a = new long[]{test}; a;", - FactoryTestConverterScript.CONTEXT, Collections.emptyMap()); + FactoryTestConverterScript.CONTEXT, + Collections.emptyMap() + ); script = factory.newInstance(Collections.singletonMap("test", 2)); - assertArrayEquals(new long[]{123}, script.execute(123)); + assertArrayEquals(new long[] { 123 }, script.execute(123)); // autoreturn, converter - factory = scriptEngine.compile("converter_test", - "def a = '1001'; a", - FactoryTestConverterScript.CONTEXT, Collections.emptyMap()); + factory = scriptEngine.compile("converter_test", "def a = '1001'; a", FactoryTestConverterScript.CONTEXT, Collections.emptyMap()); script = factory.newInstance(Collections.singletonMap("test", 2)); - assertArrayEquals(new long[]{1001}, script.execute(456)); + assertArrayEquals(new long[] { 1001 }, script.execute(456)); - factory = scriptEngine.compile("converter_test", - "int x = 1", - FactoryTestConverterScript.CONTEXT, Collections.emptyMap()); + factory = scriptEngine.compile("converter_test", "int x = 1", FactoryTestConverterScript.CONTEXT, Collections.emptyMap()); script = factory.newInstance(Collections.singletonMap("test", 2)); assertArrayEquals(null, script.execute(123)); - factory = scriptEngine.compile("converter_test", + factory = scriptEngine.compile( + "converter_test", "short x = 1; return x", - FactoryTestConverterScript.CONTEXT, Collections.emptyMap()); + FactoryTestConverterScript.CONTEXT, + Collections.emptyMap() + ); script = factory.newInstance(Collections.singletonMap("test", 2)); - assertArrayEquals(new long[]{1}, script.execute(123)); + assertArrayEquals(new long[] { 1 }, script.execute(123)); - ClassCastException cce = expectScriptThrows(ClassCastException.class, () -> - scriptEngine.compile("converter_test", - "return true;", - FactoryTestConverterScript.CONTEXT, Collections.emptyMap())); + ClassCastException cce = expectScriptThrows( + ClassCastException.class, + () -> scriptEngine.compile("converter_test", "return true;", FactoryTestConverterScript.CONTEXT, Collections.emptyMap()) + ); assertEquals(cce.getMessage(), "Cannot cast from [boolean] to [long[]]."); } @@ -453,27 +512,28 @@ public Map getParams() { return params; } - public static final String[] PARAMETERS = new String[] {"test"}; + public static final String[] PARAMETERS = new String[] { "test" }; + public abstract long[] execute(int test); public interface Factory { FactoryTestConverterScriptBadDef newInstance(Map params); } - public static final ScriptContext CONTEXT = - new ScriptContext<>("test", FactoryTestConverterScriptBadDef.Factory.class); + public static final ScriptContext CONTEXT = new ScriptContext<>( + "test", + FactoryTestConverterScriptBadDef.Factory.class + ); public static long[] convertFromDef(int def) { - return new long[]{def}; + return new long[] { def }; } } public void testConverterFactoryBadDef() { IllegalStateException ise = null; try { - scriptEngine.compile("converter_def", - "return test;", - FactoryTestConverterScriptBadDef.CONTEXT, Collections.emptyMap()); + scriptEngine.compile("converter_def", "return test;", FactoryTestConverterScriptBadDef.CONTEXT, Collections.emptyMap()); } catch (ScriptException e) { ise = (IllegalStateException) e.getCause(); } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/FeatureTestAugmentationObject.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/FeatureTestAugmentationObject.java index 304aa22cd2203..7d923218fdb1a 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/FeatureTestAugmentationObject.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/FeatureTestAugmentationObject.java @@ -29,16 +29,23 @@ public static int augmentTimesSupplier(FeatureTestObject ft, Function fn, short arg) { - return ft.getX()*fn.apply(arg)*injected; + return ft.getX() * fn.apply(arg) * injected; } public static int augmentInjectMultiTimesX(FeatureTestObject ft, int inject1, int inject2, short user) { return ft.getX() * (inject1 + inject2) * user; } - public static int augmentInjectMultiWithLambda(FeatureTestObject ft, - int inject1, int inject2, int inject3, int inject4, Function fn, short arg) { - return ft.getX()*fn.apply(arg)*(inject1 + inject2 + inject3 + inject4); + public static int augmentInjectMultiWithLambda( + FeatureTestObject ft, + int inject1, + int inject2, + int inject3, + int inject4, + Function fn, + short arg + ) { + return ft.getX() * fn.apply(arg) * (inject1 + inject2 + inject3 + inject4); } private FeatureTestAugmentationObject() {} diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/FeatureTestObject.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/FeatureTestObject.java index 76b1d825d023d..a71733b8ad1f4 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/FeatureTestObject.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/FeatureTestObject.java @@ -47,8 +47,7 @@ public static int staticNumberArgument(int injected, int userArgument) { private Integer i; /** empty ctor */ - public FeatureTestObject() { - } + public FeatureTestObject() {} /** ctor with params */ public FeatureTestObject(int x, int y) { @@ -95,7 +94,7 @@ public int timesSupplier(Function fn, short fnArg, int userArg) } public int injectWithLambda(int injected, Function fn, short arg) { - return this.x*fn.apply(arg)*injected; + return this.x * fn.apply(arg) * injected; } public int injectMultiTimesX(int inject1, int inject2, int inject3, short user) { @@ -103,15 +102,15 @@ public int injectMultiTimesX(int inject1, int inject2, int inject3, short user) } public int injectMultiWithLambda(int inject1, int inject2, int inject3, Function fn, short arg) { - return this.x*fn.apply(arg)*(inject1 + inject2 + inject3); + return this.x * fn.apply(arg) * (inject1 + inject2 + inject3); } public Double mixedAdd(int i, Byte b, char c, Float f) { - return (double)(i + b + c + f); + return (double) (i + b + c + f); } /** method taking two functions! */ - public Object twoFunctionsOfX(Function f, Function g) { + public Object twoFunctionsOfX(Function f, Function g) { return f.apply(g.apply(x)); } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/FeatureTestObject2.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/FeatureTestObject2.java index 4992c546904ad..0490e9edec003 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/FeatureTestObject2.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/FeatureTestObject2.java @@ -10,10 +10,14 @@ /** Currently just a dummy class for testing a few features not yet exposed by whitelist! */ public class FeatureTestObject2 { - public FeatureTestObject2() {super();} + public FeatureTestObject2() { + super(); + } + public static int staticNumberArgument(int injected, int userArgument) { return injected * userArgument; } + public static int staticNumberArgument2(int userArgument1, int userArgument2) { return userArgument1 * userArgument2; } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/FloatOverflowTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/FloatOverflowTests.java index 9b962c443ce3e..a2bf232ece2d9 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/FloatOverflowTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/FloatOverflowTests.java @@ -65,8 +65,10 @@ public void testAdditionConst() throws Exception { public void testSubtraction() throws Exception { assertEquals(Float.NEGATIVE_INFINITY, exec("float x = -3.4028234663852886E38f; float y = 3.4028234663852886E38f; return x - y;")); - assertEquals(Double.NEGATIVE_INFINITY, - exec("double x = -1.7976931348623157E308; double y = 1.7976931348623157E308; return x - y;")); + assertEquals( + Double.NEGATIVE_INFINITY, + exec("double x = -1.7976931348623157E308; double y = 1.7976931348623157E308; return x - y;") + ); } public void testSubtractionConst() throws Exception { diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/FunctionRefTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/FunctionRefTests.java index 63306b8036f85..8403f03a55ffc 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/FunctionRefTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/FunctionRefTests.java @@ -34,68 +34,102 @@ public void testVirtualMethodReferenceDef() { } public void testQualifiedStaticMethodReference() { - assertEquals(true, - exec("List l = [true]; l.stream().map(org.elasticsearch.painless.FeatureTestObject::overloadedStatic).findFirst().get()")); + assertEquals( + true, + exec("List l = [true]; l.stream().map(org.elasticsearch.painless.FeatureTestObject::overloadedStatic).findFirst().get()") + ); } public void testQualifiedStaticMethodReferenceDef() { - assertEquals(true, - exec("def l = [true]; l.stream().map(org.elasticsearch.painless.FeatureTestObject::overloadedStatic).findFirst().get()")); + assertEquals( + true, + exec("def l = [true]; l.stream().map(org.elasticsearch.painless.FeatureTestObject::overloadedStatic).findFirst().get()") + ); } public void testQualifiedVirtualMethodReference() { long instant = randomLong(); - assertEquals(instant, exec( + assertEquals( + instant, + exec( "List l = [params.d]; return l.stream().mapToLong(Instant::toEpochMilli).sum()", - singletonMap("d", Instant.ofEpochMilli(instant)), true)); + singletonMap("d", Instant.ofEpochMilli(instant)), + true + ) + ); } public void testQualifiedVirtualMethodReferenceDef() { long instant = randomLong(); - assertEquals(instant, exec( + assertEquals( + instant, + exec( "def l = [params.d]; return l.stream().mapToLong(Instant::toEpochMilli).sum()", - singletonMap("d", Instant.ofEpochMilli(instant)), true)); + singletonMap("d", Instant.ofEpochMilli(instant)), + true + ) + ); } public void testCtorMethodReference() { - assertEquals(3.0D, - exec("List l = new ArrayList(); l.add(1.0); l.add(2.0); " + - "DoubleStream doubleStream = l.stream().mapToDouble(Double::doubleValue);" + - "DoubleSummaryStatistics stats = doubleStream.collect(DoubleSummaryStatistics::new, " + - "DoubleSummaryStatistics::accept, " + - "DoubleSummaryStatistics::combine); " + - "return stats.getSum()")); + assertEquals( + 3.0D, + exec( + "List l = new ArrayList(); l.add(1.0); l.add(2.0); " + + "DoubleStream doubleStream = l.stream().mapToDouble(Double::doubleValue);" + + "DoubleSummaryStatistics stats = doubleStream.collect(DoubleSummaryStatistics::new, " + + "DoubleSummaryStatistics::accept, " + + "DoubleSummaryStatistics::combine); " + + "return stats.getSum()" + ) + ); } public void testCtorMethodReferenceDef() { - assertEquals(3.0D, - exec("def l = new ArrayList(); l.add(1.0); l.add(2.0); " + - "def doubleStream = l.stream().mapToDouble(Double::doubleValue);" + - "def stats = doubleStream.collect(DoubleSummaryStatistics::new, " + - "DoubleSummaryStatistics::accept, " + - "DoubleSummaryStatistics::combine); " + - "return stats.getSum()")); + assertEquals( + 3.0D, + exec( + "def l = new ArrayList(); l.add(1.0); l.add(2.0); " + + "def doubleStream = l.stream().mapToDouble(Double::doubleValue);" + + "def stats = doubleStream.collect(DoubleSummaryStatistics::new, " + + "DoubleSummaryStatistics::accept, " + + "DoubleSummaryStatistics::combine); " + + "return stats.getSum()" + ) + ); } public void testCtorWithParams() { - assertArrayEquals(new Object[] { "foo", "bar" }, - (Object[]) exec("List l = new ArrayList(); l.add('foo'); l.add('bar'); " + - "Stream stream = l.stream().map(StringBuilder::new);" + - "return stream.map(Object::toString).toArray()")); + assertArrayEquals( + new Object[] { "foo", "bar" }, + (Object[]) exec( + "List l = new ArrayList(); l.add('foo'); l.add('bar'); " + + "Stream stream = l.stream().map(StringBuilder::new);" + + "return stream.map(Object::toString).toArray()" + ) + ); } public void testArrayCtorMethodRef() { - assertEquals(1.0D, - exec("List l = new ArrayList(); l.add(1.0); l.add(2.0); " + - "def[] array = l.stream().toArray(Double[]::new);" + - "return array[0];")); + assertEquals( + 1.0D, + exec( + "List l = new ArrayList(); l.add(1.0); l.add(2.0); " + + "def[] array = l.stream().toArray(Double[]::new);" + + "return array[0];" + ) + ); } public void testArrayCtorMethodRefDef() { - assertEquals(1.0D, - exec("def l = new ArrayList(); l.add(1.0); l.add(2.0); " + - "def[] array = l.stream().toArray(Double[]::new);" + - "return array[0];")); + assertEquals( + 1.0D, + exec( + "def l = new ArrayList(); l.add(1.0); l.add(2.0); " + + "def[] array = l.stream().toArray(Double[]::new);" + + "return array[0];" + ) + ); } public void testCapturingMethodReference() { @@ -119,178 +153,232 @@ public void testCapturingMethodReferenceDefEverywhere() { } public void testCapturingMethodReferenceMultipleLambdas() { - assertEquals("testingcdefg", exec( - "String x = 'testing';" + - "String y = 'abcdefg';" + - "org.elasticsearch.painless.FeatureTestObject test = new org.elasticsearch.painless.FeatureTestObject(2,3);" + - "return test.twoFunctionsOfX(x::concat, y::substring);")); + assertEquals( + "testingcdefg", + exec( + "String x = 'testing';" + + "String y = 'abcdefg';" + + "org.elasticsearch.painless.FeatureTestObject test = new org.elasticsearch.painless.FeatureTestObject(2,3);" + + "return test.twoFunctionsOfX(x::concat, y::substring);" + ) + ); } public void testCapturingMethodReferenceMultipleLambdasDefImpls() { - assertEquals("testingcdefg", exec( - "def x = 'testing';" + - "def y = 'abcdefg';" + - "org.elasticsearch.painless.FeatureTestObject test = new org.elasticsearch.painless.FeatureTestObject(2,3);" + - "return test.twoFunctionsOfX(x::concat, y::substring);")); + assertEquals( + "testingcdefg", + exec( + "def x = 'testing';" + + "def y = 'abcdefg';" + + "org.elasticsearch.painless.FeatureTestObject test = new org.elasticsearch.painless.FeatureTestObject(2,3);" + + "return test.twoFunctionsOfX(x::concat, y::substring);" + ) + ); } public void testCapturingMethodReferenceMultipleLambdasDefInterface() { - assertEquals("testingcdefg", exec( - "String x = 'testing';" + - "String y = 'abcdefg';" + - "def test = new org.elasticsearch.painless.FeatureTestObject(2,3);" + - "return test.twoFunctionsOfX(x::concat, y::substring);")); + assertEquals( + "testingcdefg", + exec( + "String x = 'testing';" + + "String y = 'abcdefg';" + + "def test = new org.elasticsearch.painless.FeatureTestObject(2,3);" + + "return test.twoFunctionsOfX(x::concat, y::substring);" + ) + ); } public void testCapturingMethodReferenceMultipleLambdasDefEverywhere() { - assertEquals("testingcdefg", exec( - "def x = 'testing';" + - "def y = 'abcdefg';" + - "def test = new org.elasticsearch.painless.FeatureTestObject(2,3);" + - "return test.twoFunctionsOfX(x::concat, y::substring);")); + assertEquals( + "testingcdefg", + exec( + "def x = 'testing';" + + "def y = 'abcdefg';" + + "def test = new org.elasticsearch.painless.FeatureTestObject(2,3);" + + "return test.twoFunctionsOfX(x::concat, y::substring);" + ) + ); } public void testOwnMethodReference() { - assertEquals(2, exec("int mycompare(int i, int j) { j - i } " + - "List l = new ArrayList(); l.add(2); l.add(1); l.sort(this::mycompare); return l.get(0);")); + assertEquals( + 2, + exec( + "int mycompare(int i, int j) { j - i } " + + "List l = new ArrayList(); l.add(2); l.add(1); l.sort(this::mycompare); return l.get(0);" + ) + ); } public void testOwnMethodReferenceDef() { - assertEquals(2, exec("int mycompare(int i, int j) { j - i } " + - "def l = new ArrayList(); l.add(2); l.add(1); l.sort(this::mycompare); return l.get(0);")); + assertEquals( + 2, + exec( + "int mycompare(int i, int j) { j - i } " + + "def l = new ArrayList(); l.add(2); l.add(1); l.sort(this::mycompare); return l.get(0);" + ) + ); } public void testInterfaceDefaultMethod() { - assertEquals("bar", exec("String f(BiFunction function) { function.apply('foo', 'bar') }" + - "Map map = new HashMap(); f(map::getOrDefault)")); + assertEquals( + "bar", + exec("String f(BiFunction function) { function.apply('foo', 'bar') }" + "Map map = new HashMap(); f(map::getOrDefault)") + ); } public void testInterfaceDefaultMethodDef() { - assertEquals("bar", exec("String f(BiFunction function) { function.apply('foo', 'bar') }" + - "def map = new HashMap(); f(map::getOrDefault)")); + assertEquals( + "bar", + exec("String f(BiFunction function) { function.apply('foo', 'bar') }" + "def map = new HashMap(); f(map::getOrDefault)") + ); } public void testInterfaceStaticMethod() { - assertEquals(-1, exec("Supplier get(Supplier supplier) { return supplier }" + - "Supplier s = get(Comparator::naturalOrder); s.get().compare(1, 2)")); + assertEquals( + -1, + exec( + "Supplier get(Supplier supplier) { return supplier }" + "Supplier s = get(Comparator::naturalOrder); s.get().compare(1, 2)" + ) + ); } public void testMethodMissing() { - Exception e = expectScriptThrows(IllegalArgumentException.class, () -> { - exec("List l = [2, 1]; l.sort(Integer::bogus); return l.get(0);"); - }); + Exception e = expectScriptThrows( + IllegalArgumentException.class, + () -> { exec("List l = [2, 1]; l.sort(Integer::bogus); return l.get(0);"); } + ); assertThat(e.getMessage(), containsString("function reference [Integer::bogus/2] matching [java.util.Comparator")); } public void testQualifiedMethodMissing() { - Exception e = expectScriptThrows(IllegalArgumentException.class, () -> { - exec("List l = [2, 1]; l.sort(java.time.Instant::bogus); return l.get(0);", false); - }); - assertThat(e.getMessage(), - containsString("function reference [java.time.Instant::bogus/2] matching [java.util.Comparator, compare/2")); + Exception e = expectScriptThrows( + IllegalArgumentException.class, + () -> { exec("List l = [2, 1]; l.sort(java.time.Instant::bogus); return l.get(0);", false); } + ); + assertThat( + e.getMessage(), + containsString("function reference [java.time.Instant::bogus/2] matching [java.util.Comparator, compare/2") + ); } public void testClassMissing() { - Exception e = expectScriptThrows(IllegalArgumentException.class, () -> { - exec("List l = [2, 1]; l.sort(Bogus::bogus); return l.get(0);", false); - }); + Exception e = expectScriptThrows( + IllegalArgumentException.class, + () -> { exec("List l = [2, 1]; l.sort(Bogus::bogus); return l.get(0);", false); } + ); assertThat(e.getMessage(), endsWith("variable [Bogus] is not defined")); } public void testQualifiedClassMissing() { - Exception e = expectScriptThrows(IllegalArgumentException.class, () -> { - exec("List l = [2, 1]; l.sort(org.package.BogusClass::bogus); return l.get(0);", false); - }); + Exception e = expectScriptThrows( + IllegalArgumentException.class, + () -> { exec("List l = [2, 1]; l.sort(org.package.BogusClass::bogus); return l.get(0);", false); } + ); assertEquals("variable [org.package.BogusClass] is not defined", e.getMessage()); } public void testNotFunctionalInterface() { - IllegalArgumentException expected = expectScriptThrows(IllegalArgumentException.class, () -> { - exec("List l = new ArrayList(); l.add(2); l.add(1); l.add(Integer::bogus); return l.get(0);"); - }); - assertThat(expected.getMessage(), - containsString("cannot convert function reference [Integer::bogus] to a non-functional interface [def]")); + IllegalArgumentException expected = expectScriptThrows( + IllegalArgumentException.class, + () -> { exec("List l = new ArrayList(); l.add(2); l.add(1); l.add(Integer::bogus); return l.get(0);"); } + ); + assertThat( + expected.getMessage(), + containsString("cannot convert function reference [Integer::bogus] to a non-functional interface [def]") + ); } public void testIncompatible() { - expectScriptThrows(ClassCastException.class, () -> { - exec("List l = new ArrayList(); l.add(2); l.add(1); l.sort(String::startsWith); return l.get(0);"); - }); + expectScriptThrows( + ClassCastException.class, + () -> { exec("List l = new ArrayList(); l.add(2); l.add(1); l.sort(String::startsWith); return l.get(0);"); } + ); } public void testWrongArity() { - IllegalArgumentException expected = expectScriptThrows(IllegalArgumentException.class, () -> { - exec("Optional.empty().orElseGet(String::startsWith);"); - }); - assertThat(expected.getMessage(), - containsString("function reference [String::startsWith/0] matching [java.util.function.Supplier")); + IllegalArgumentException expected = expectScriptThrows( + IllegalArgumentException.class, + () -> { exec("Optional.empty().orElseGet(String::startsWith);"); } + ); + assertThat( + expected.getMessage(), + containsString("function reference [String::startsWith/0] matching [java.util.function.Supplier") + ); } public void testWrongArityNotEnough() { - IllegalArgumentException expected = expectScriptThrows(IllegalArgumentException.class, () -> { - exec("List l = new ArrayList(); l.add(2); l.add(1); l.sort(String::isEmpty);"); - }); - assertThat(expected.getMessage(), containsString( - "function reference [String::isEmpty/2] matching [java.util.Comparator")); + IllegalArgumentException expected = expectScriptThrows( + IllegalArgumentException.class, + () -> { exec("List l = new ArrayList(); l.add(2); l.add(1); l.sort(String::isEmpty);"); } + ); + assertThat(expected.getMessage(), containsString("function reference [String::isEmpty/2] matching [java.util.Comparator")); } public void testWrongArityDef() { - IllegalArgumentException expected = expectScriptThrows(IllegalArgumentException.class, () -> { - exec("def y = Optional.empty(); return y.orElseGet(String::startsWith);"); - }); - assertThat(expected.getMessage(), - containsString("function reference [String::startsWith/0] matching [java.util.function.Supplier")); + IllegalArgumentException expected = expectScriptThrows( + IllegalArgumentException.class, + () -> { exec("def y = Optional.empty(); return y.orElseGet(String::startsWith);"); } + ); + assertThat( + expected.getMessage(), + containsString("function reference [String::startsWith/0] matching [java.util.function.Supplier") + ); } public void testWrongArityNotEnoughDef() { - IllegalArgumentException expected = expectScriptThrows(IllegalArgumentException.class, () -> { - exec("def l = new ArrayList(); l.add(2); l.add(1); l.sort(String::isEmpty);"); - }); - assertThat(expected.getMessage(), - containsString("function reference [String::isEmpty/2] matching [java.util.Comparator")); + IllegalArgumentException expected = expectScriptThrows( + IllegalArgumentException.class, + () -> { exec("def l = new ArrayList(); l.add(2); l.add(1); l.sort(String::isEmpty);"); } + ); + assertThat(expected.getMessage(), containsString("function reference [String::isEmpty/2] matching [java.util.Comparator")); } public void testReturnVoid() { - Throwable expected = expectScriptThrows(ClassCastException.class, () -> { - exec("StringBuilder b = new StringBuilder(); List l = [1, 2]; l.stream().mapToLong(b::setLength).sum();"); - }); + Throwable expected = expectScriptThrows( + ClassCastException.class, + () -> { exec("StringBuilder b = new StringBuilder(); List l = [1, 2]; l.stream().mapToLong(b::setLength).sum();"); } + ); assertThat(expected.getMessage(), containsString("Cannot cast from [void] to [long].")); } public void testReturnVoidDef() { - Exception expected = expectScriptThrows(LambdaConversionException.class, () -> { - exec("StringBuilder b = new StringBuilder(); def l = [1, 2]; l.stream().mapToLong(b::setLength);"); - }); + Exception expected = expectScriptThrows( + LambdaConversionException.class, + () -> { exec("StringBuilder b = new StringBuilder(); def l = [1, 2]; l.stream().mapToLong(b::setLength);"); } + ); assertThat(expected.getMessage(), containsString("lambda expects return type [long], but found return type [void]")); - expected = expectScriptThrows(LambdaConversionException.class, () -> { - exec("def b = new StringBuilder(); def l = [1, 2]; l.stream().mapToLong(b::setLength);"); - }); + expected = expectScriptThrows( + LambdaConversionException.class, + () -> { exec("def b = new StringBuilder(); def l = [1, 2]; l.stream().mapToLong(b::setLength);"); } + ); assertThat(expected.getMessage(), containsString("lambda expects return type [long], but found return type [void]")); - expected = expectScriptThrows(LambdaConversionException.class, () -> { - exec("def b = new StringBuilder(); List l = [1, 2]; l.stream().mapToLong(b::setLength);"); - }); + expected = expectScriptThrows( + LambdaConversionException.class, + () -> { exec("def b = new StringBuilder(); List l = [1, 2]; l.stream().mapToLong(b::setLength);"); } + ); assertThat(expected.getMessage(), containsString("lambda expects return type [long], but found return type [void]")); } public void testPrimitiveMethodReferences() { assertEquals(true, exec("boolean test(Function s) {return s.apply(Boolean.valueOf(true));} return test(boolean::booleanValue);")); assertEquals(true, exec("boolean test(Supplier s) {return s.get();} boolean b = true; return test(b::booleanValue);")); - assertEquals((byte)1, exec("byte test(Function s) {return s.apply(Byte.valueOf(1));} return test(byte::byteValue);")); - assertEquals((byte)1, exec("byte test(Supplier s) {return s.get();} byte b = 1; return test(b::byteValue);")); - assertEquals((short)1, exec("short test(Function s) {return s.apply(Short.valueOf(1));} return test(short::shortValue);")); - assertEquals((short)1, exec("short test(Supplier s) {return s.get();} short s = 1; return test(s::shortValue);")); - assertEquals((char)1, exec("char test(Function s) {return s.apply(Character.valueOf(1));} return test(char::charValue);")); - assertEquals((char)1, exec("char test(Supplier s) {return s.get();} char c = 1; return test(c::charValue);")); + assertEquals((byte) 1, exec("byte test(Function s) {return s.apply(Byte.valueOf(1));} return test(byte::byteValue);")); + assertEquals((byte) 1, exec("byte test(Supplier s) {return s.get();} byte b = 1; return test(b::byteValue);")); + assertEquals((short) 1, exec("short test(Function s) {return s.apply(Short.valueOf(1));} return test(short::shortValue);")); + assertEquals((short) 1, exec("short test(Supplier s) {return s.get();} short s = 1; return test(s::shortValue);")); + assertEquals((char) 1, exec("char test(Function s) {return s.apply(Character.valueOf(1));} return test(char::charValue);")); + assertEquals((char) 1, exec("char test(Supplier s) {return s.get();} char c = 1; return test(c::charValue);")); assertEquals(1, exec("int test(Function s) {return s.apply(Integer.valueOf(1));} return test(int::intValue);")); assertEquals(1, exec("int test(Supplier s) {return s.get();} int i = 1; return test(i::intValue);")); - assertEquals((long)1, exec("long test(Function s) {return s.apply(Long.valueOf(1));} return test(long::longValue);")); - assertEquals((long)1, exec("long test(Supplier s) {return s.get();} long l = 1; return test(l::longValue);")); - assertEquals((float)1, exec("float test(Function s) {return s.apply(Short.valueOf(1));} return test(float::floatValue);")); - assertEquals((float)1, exec("float test(Supplier s) {return s.get();} float f = 1; return test(f::floatValue);")); - assertEquals((double)1, exec("double test(Function s) {return s.apply(Double.valueOf(1));} return test(double::doubleValue);")); - assertEquals((double)1, exec("double test(Supplier s) {return s.get();} double d = 1; return test(d::doubleValue);")); + assertEquals((long) 1, exec("long test(Function s) {return s.apply(Long.valueOf(1));} return test(long::longValue);")); + assertEquals((long) 1, exec("long test(Supplier s) {return s.get();} long l = 1; return test(l::longValue);")); + assertEquals((float) 1, exec("float test(Function s) {return s.apply(Short.valueOf(1));} return test(float::floatValue);")); + assertEquals((float) 1, exec("float test(Supplier s) {return s.get();} float f = 1; return test(f::floatValue);")); + assertEquals((double) 1, exec("double test(Function s) {return s.apply(Double.valueOf(1));} return test(double::doubleValue);")); + assertEquals((double) 1, exec("double test(Supplier s) {return s.get();} double d = 1; return test(d::doubleValue);")); } public void testObjectMethodOverride() { @@ -300,8 +388,9 @@ public void testObjectMethodOverride() { } public void testInvalidStaticCaptureMethodReference() { - IllegalArgumentException expected = expectScriptThrows(IllegalArgumentException.class, () -> - exec("int test(Function f, String s) {return f.apply(s);} Integer i = Integer.valueOf(1); test(i::parseInt, '1')") + IllegalArgumentException expected = expectScriptThrows( + IllegalArgumentException.class, + () -> exec("int test(Function f, String s) {return f.apply(s);} Integer i = Integer.valueOf(1); test(i::parseInt, '1')") ); assertThat(expected.getMessage(), containsString("cannot use a static method as a function reference")); } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/FunctionTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/FunctionTests.java index f079f0b2056a9..23af1adb93124 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/FunctionTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/FunctionTests.java @@ -38,11 +38,11 @@ public void testRecursion() { } public void testEmpty() { - Exception expected = expectScriptThrows(IllegalArgumentException.class, () -> { - exec("void test(int x) {} test()"); - }); - assertThat(expected.getMessage(), containsString( - "invalid function definition: found no statements for function [test] with [1] parameters")); + Exception expected = expectScriptThrows(IllegalArgumentException.class, () -> { exec("void test(int x) {} test()"); }); + assertThat( + expected.getMessage(), + containsString("invalid function definition: found no statements for function [test] with [1] parameters") + ); } public void testReturnsAreUnboxedIfNeeded() { @@ -58,9 +58,10 @@ public void testReturnsAreUnboxedIfNeeded() { } public void testDuplicates() { - Exception expected = expectScriptThrows(IllegalArgumentException.class, () -> { - exec("void test(int x) {x = 2;} void test(def y) {y = 3;} test()"); - }); + Exception expected = expectScriptThrows( + IllegalArgumentException.class, + () -> { exec("void test(int x) {x = 2;} void test(def y) {y = 3;} test()"); } + ); assertThat(expected.getMessage(), containsString("found duplicate function")); } @@ -74,27 +75,32 @@ public void testBadCastFromMethod() { } public void testInfiniteLoop() { - Error expected = expectScriptThrows(PainlessError.class, () -> { - exec("void test() {boolean x = true; while (x) {}} test()"); - }); - assertThat(expected.getMessage(), - containsString("The maximum number of statements that can be executed in a loop has been reached.")); + Error expected = expectScriptThrows(PainlessError.class, () -> { exec("void test() {boolean x = true; while (x) {}} test()"); }); + assertThat( + expected.getMessage(), + containsString("The maximum number of statements that can be executed in a loop has been reached.") + ); } public void testReturnVoid() { assertEquals(null, exec("void test(StringBuilder b, int i) {b.setLength(i)} test(new StringBuilder(), 1)")); - Exception expected = expectScriptThrows(IllegalArgumentException.class, () -> { - exec("int test(StringBuilder b, int i) {b.setLength(i)} test(new StringBuilder(), 1)"); - }); - assertEquals("invalid function definition: " + - "not all paths provide a return value for function [test] with [2] parameters", expected.getMessage()); - expected = expectScriptThrows(ClassCastException.class, () -> { - exec("int test(StringBuilder b, int i) {return b.setLength(i)} test(new StringBuilder(), 1)"); - }); + Exception expected = expectScriptThrows( + IllegalArgumentException.class, + () -> { exec("int test(StringBuilder b, int i) {b.setLength(i)} test(new StringBuilder(), 1)"); } + ); + assertEquals( + "invalid function definition: " + "not all paths provide a return value for function [test] with [2] parameters", + expected.getMessage() + ); + expected = expectScriptThrows( + ClassCastException.class, + () -> { exec("int test(StringBuilder b, int i) {return b.setLength(i)} test(new StringBuilder(), 1)"); } + ); assertEquals("Cannot cast from [void] to [int].", expected.getMessage()); - expected = expectScriptThrows(ClassCastException.class, () -> { - exec("def test(StringBuilder b, int i) {return b.setLength(i)} test(new StringBuilder(), 1)"); - }); + expected = expectScriptThrows( + ClassCastException.class, + () -> { exec("def test(StringBuilder b, int i) {return b.setLength(i)} test(new StringBuilder(), 1)"); } + ); assertEquals("Cannot cast from [void] to [def].", expected.getMessage()); } } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/GeneralCastTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/GeneralCastTests.java index 6f1d7dc106bfe..3cc1492bbebe2 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/GeneralCastTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/GeneralCastTests.java @@ -15,9 +15,9 @@ public class GeneralCastTests extends ScriptTestCase { * Unary operator with explicit cast */ public void testUnaryOperator() { - assertEquals((byte)5, exec("long x = 5L; return (byte) (+x);")); - assertEquals((short)5, exec("long x = 5L; return (short) (+x);")); - assertEquals((char)5, exec("long x = 5L; return (char) (+x);")); + assertEquals((byte) 5, exec("long x = 5L; return (byte) (+x);")); + assertEquals((short) 5, exec("long x = 5L; return (short) (+x);")); + assertEquals((char) 5, exec("long x = 5L; return (char) (+x);")); assertEquals(5, exec("long x = 5L; return (int) (+x);")); assertEquals(5F, exec("long x = 5L; return (float) (+x);")); assertEquals(5L, exec("long x = 5L; return (long) (+x);")); @@ -28,9 +28,9 @@ public void testUnaryOperator() { * Binary operators with explicit cast */ public void testBinaryOperator() { - assertEquals((byte)6, exec("long x = 5L; return (byte) (x + 1);")); - assertEquals((short)6, exec("long x = 5L; return (short) (x + 1);")); - assertEquals((char)6, exec("long x = 5L; return (char) (x + 1);")); + assertEquals((byte) 6, exec("long x = 5L; return (byte) (x + 1);")); + assertEquals((short) 6, exec("long x = 5L; return (short) (x + 1);")); + assertEquals((char) 6, exec("long x = 5L; return (char) (x + 1);")); assertEquals(6, exec("long x = 5L; return (int) (x + 1);")); assertEquals(6F, exec("long x = 5L; return (float) (x + 1);")); assertEquals(6L, exec("long x = 5L; return (long) (x + 1);")); @@ -41,9 +41,9 @@ public void testBinaryOperator() { * Binary compound assignment with explicit cast */ public void testBinaryCompoundAssignment() { - assertEquals((byte)6, exec("long x = 5L; return (byte) (x += 1);")); - assertEquals((short)6, exec("long x = 5L; return (short) (x += 1);")); - assertEquals((char)6, exec("long x = 5L; return (char) (x += 1);")); + assertEquals((byte) 6, exec("long x = 5L; return (byte) (x += 1);")); + assertEquals((short) 6, exec("long x = 5L; return (short) (x += 1);")); + assertEquals((char) 6, exec("long x = 5L; return (char) (x += 1);")); assertEquals(6, exec("long x = 5L; return (int) (x += 1);")); assertEquals(6F, exec("long x = 5L; return (float) (x += 1);")); assertEquals(6L, exec("long x = 5L; return (long) (x += 1);")); @@ -54,9 +54,9 @@ public void testBinaryCompoundAssignment() { * Binary compound prefix with explicit cast */ public void testBinaryPrefix() { - assertEquals((byte)6, exec("long x = 5L; return (byte) (++x);")); - assertEquals((short)6, exec("long x = 5L; return (short) (++x);")); - assertEquals((char)6, exec("long x = 5L; return (char) (++x);")); + assertEquals((byte) 6, exec("long x = 5L; return (byte) (++x);")); + assertEquals((short) 6, exec("long x = 5L; return (short) (++x);")); + assertEquals((char) 6, exec("long x = 5L; return (char) (++x);")); assertEquals(6, exec("long x = 5L; return (int) (++x);")); assertEquals(6F, exec("long x = 5L; return (float) (++x);")); assertEquals(6L, exec("long x = 5L; return (long) (++x);")); @@ -67,9 +67,9 @@ public void testBinaryPrefix() { * Binary compound postifx with explicit cast */ public void testBinaryPostfix() { - assertEquals((byte)5, exec("long x = 5L; return (byte) (x++);")); - assertEquals((short)5, exec("long x = 5L; return (short) (x++);")); - assertEquals((char)5, exec("long x = 5L; return (char) (x++);")); + assertEquals((byte) 5, exec("long x = 5L; return (byte) (x++);")); + assertEquals((short) 5, exec("long x = 5L; return (short) (x++);")); + assertEquals((char) 5, exec("long x = 5L; return (char) (x++);")); assertEquals(5, exec("long x = 5L; return (int) (x++);")); assertEquals(5F, exec("long x = 5L; return (float) (x++);")); assertEquals(5L, exec("long x = 5L; return (long) (x++);")); @@ -80,9 +80,9 @@ public void testBinaryPostfix() { * Shift operators with explicit cast */ public void testShiftOperator() { - assertEquals((byte)10, exec("long x = 5L; return (byte) (x << 1);")); - assertEquals((short)10, exec("long x = 5L; return (short) (x << 1);")); - assertEquals((char)10, exec("long x = 5L; return (char) (x << 1);")); + assertEquals((byte) 10, exec("long x = 5L; return (byte) (x << 1);")); + assertEquals((short) 10, exec("long x = 5L; return (short) (x << 1);")); + assertEquals((char) 10, exec("long x = 5L; return (char) (x << 1);")); assertEquals(10, exec("long x = 5L; return (int) (x << 1);")); assertEquals(10F, exec("long x = 5L; return (float) (x << 1);")); assertEquals(10L, exec("long x = 5L; return (long) (x << 1);")); @@ -93,9 +93,9 @@ public void testShiftOperator() { * Shift compound assignment with explicit cast */ public void testShiftCompoundAssignment() { - assertEquals((byte)10, exec("long x = 5L; return (byte) (x <<= 1);")); - assertEquals((short)10, exec("long x = 5L; return (short) (x <<= 1);")); - assertEquals((char)10, exec("long x = 5L; return (char) (x <<= 1);")); + assertEquals((byte) 10, exec("long x = 5L; return (byte) (x <<= 1);")); + assertEquals((short) 10, exec("long x = 5L; return (short) (x <<= 1);")); + assertEquals((char) 10, exec("long x = 5L; return (char) (x <<= 1);")); assertEquals(10, exec("long x = 5L; return (int) (x <<= 1);")); assertEquals(10F, exec("long x = 5L; return (float) (x <<= 1);")); assertEquals(10L, exec("long x = 5L; return (long) (x <<= 1);")); @@ -106,42 +106,22 @@ public void testShiftCompoundAssignment() { * Test that without a cast, we fail when conversions would narrow. */ public void testIllegalConversions() { - expectScriptThrows(ClassCastException.class, () -> { - exec("long x = 5L; int y = +x; return y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("long x = 5L; int y = (x + x); return y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("boolean x = true; int y = +x; return y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("boolean x = true; int y = (x ^ false); return y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("long x = 5L; boolean y = +x; return y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("long x = 5L; boolean y = (x + x); return y"); - }); + expectScriptThrows(ClassCastException.class, () -> { exec("long x = 5L; int y = +x; return y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("long x = 5L; int y = (x + x); return y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("boolean x = true; int y = +x; return y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("boolean x = true; int y = (x ^ false); return y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("long x = 5L; boolean y = +x; return y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("long x = 5L; boolean y = (x + x); return y"); }); } /** * Test that even with a cast, some things aren't allowed. */ public void testIllegalExplicitConversions() { - expectScriptThrows(ClassCastException.class, () -> { - exec("boolean x = true; int y = (int) +x; return y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("boolean x = true; int y = (int) (x ^ false); return y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("long x = 5L; boolean y = (boolean) +x; return y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("long x = 5L; boolean y = (boolean) (x + x); return y"); - }); + expectScriptThrows(ClassCastException.class, () -> { exec("boolean x = true; int y = (int) +x; return y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("boolean x = true; int y = (int) (x ^ false); return y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("long x = 5L; boolean y = (boolean) +x; return y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("long x = 5L; boolean y = (boolean) (x + x); return y"); }); } /** @@ -164,9 +144,9 @@ public void testArgumentsDef() { * Unary operators adopt the return value */ public void testUnaryOperatorDef() { - assertEquals((byte)5, exec("def x = 5L; return (byte) (+x);")); - assertEquals((short)5, exec("def x = 5L; return (short) (+x);")); - assertEquals((char)5, exec("def x = 5L; return (char) (+x);")); + assertEquals((byte) 5, exec("def x = 5L; return (byte) (+x);")); + assertEquals((short) 5, exec("def x = 5L; return (short) (+x);")); + assertEquals((char) 5, exec("def x = 5L; return (char) (+x);")); assertEquals(5, exec("def x = 5L; return (int) (+x);")); assertEquals(5F, exec("def x = 5L; return (float) (+x);")); assertEquals(5L, exec("def x = 5L; return (long) (+x);")); @@ -177,9 +157,9 @@ public void testUnaryOperatorDef() { * Binary operators adopt the return value */ public void testBinaryOperatorDef() { - assertEquals((byte)6, exec("def x = 5L; return (byte) (x + 1);")); - assertEquals((short)6, exec("def x = 5L; return (short) (x + 1);")); - assertEquals((char)6, exec("def x = 5L; return (char) (x + 1);")); + assertEquals((byte) 6, exec("def x = 5L; return (byte) (x + 1);")); + assertEquals((short) 6, exec("def x = 5L; return (short) (x + 1);")); + assertEquals((char) 6, exec("def x = 5L; return (char) (x + 1);")); assertEquals(6, exec("def x = 5L; return (int) (x + 1);")); assertEquals(6F, exec("def x = 5L; return (float) (x + 1);")); assertEquals(6L, exec("def x = 5L; return (long) (x + 1);")); @@ -190,9 +170,9 @@ public void testBinaryOperatorDef() { * Binary operators don't yet adopt the return value with compound assignment */ public void testBinaryCompoundAssignmentDef() { - assertEquals((byte)6, exec("def x = 5L; return (byte) (x += 1);")); - assertEquals((short)6, exec("def x = 5L; return (short) (x += 1);")); - assertEquals((char)6, exec("def x = 5L; return (char) (x += 1);")); + assertEquals((byte) 6, exec("def x = 5L; return (byte) (x += 1);")); + assertEquals((short) 6, exec("def x = 5L; return (short) (x += 1);")); + assertEquals((char) 6, exec("def x = 5L; return (char) (x += 1);")); assertEquals(6, exec("def x = 5L; return (int) (x += 1);")); assertEquals(6F, exec("def x = 5L; return (float) (x += 1);")); assertEquals(6L, exec("def x = 5L; return (long) (x += 1);")); @@ -203,9 +183,9 @@ public void testBinaryCompoundAssignmentDef() { * Binary operators don't yet adopt the return value with compound assignment */ public void testBinaryCompoundAssignmentPrefix() { - assertEquals((byte)6, exec("def x = 5L; return (byte) (++x);")); - assertEquals((short)6, exec("def x = 5L; return (short) (++x);")); - assertEquals((char)6, exec("def x = 5L; return (char) (++x);")); + assertEquals((byte) 6, exec("def x = 5L; return (byte) (++x);")); + assertEquals((short) 6, exec("def x = 5L; return (short) (++x);")); + assertEquals((char) 6, exec("def x = 5L; return (char) (++x);")); assertEquals(6, exec("def x = 5L; return (int) (++x);")); assertEquals(6F, exec("def x = 5L; return (float) (++x);")); assertEquals(6L, exec("def x = 5L; return (long) (++x);")); @@ -216,9 +196,9 @@ public void testBinaryCompoundAssignmentPrefix() { * Binary operators don't yet adopt the return value with compound assignment */ public void testBinaryCompoundAssignmentPostfix() { - assertEquals((byte)5, exec("def x = 5L; return (byte) (x++);")); - assertEquals((short)5, exec("def x = 5L; return (short) (x++);")); - assertEquals((char)5, exec("def x = 5L; return (char) (x++);")); + assertEquals((byte) 5, exec("def x = 5L; return (byte) (x++);")); + assertEquals((short) 5, exec("def x = 5L; return (short) (x++);")); + assertEquals((char) 5, exec("def x = 5L; return (char) (x++);")); assertEquals(5, exec("def x = 5L; return (int) (x++);")); assertEquals(5F, exec("def x = 5L; return (float) (x++);")); assertEquals(5L, exec("def x = 5L; return (long) (x++);")); @@ -229,9 +209,9 @@ public void testBinaryCompoundAssignmentPostfix() { * Shift operators adopt the return value */ public void testShiftOperatorDef() { - assertEquals((byte)10, exec("def x = 5L; return (byte) (x << 1);")); - assertEquals((short)10, exec("def x = 5L; return (short) (x << 1);")); - assertEquals((char)10, exec("def x = 5L; return (char) (x << 1);")); + assertEquals((byte) 10, exec("def x = 5L; return (byte) (x << 1);")); + assertEquals((short) 10, exec("def x = 5L; return (short) (x << 1);")); + assertEquals((char) 10, exec("def x = 5L; return (char) (x << 1);")); assertEquals(10, exec("def x = 5L; return (int) (x << 1);")); assertEquals(10F, exec("def x = 5L; return (float) (x << 1);")); assertEquals(10L, exec("def x = 5L; return (long) (x << 1);")); @@ -242,9 +222,9 @@ public void testShiftOperatorDef() { * Shift operators don't yet adopt the return value with compound assignment */ public void testShiftCompoundAssignmentDef() { - assertEquals((byte)10, exec("def x = 5L; return (byte) (x <<= 1);")); - assertEquals((short)10, exec("def x = 5L; return (short) (x <<= 1);")); - assertEquals((char)10, exec("def x = 5L; return (char) (x <<= 1);")); + assertEquals((byte) 10, exec("def x = 5L; return (byte) (x <<= 1);")); + assertEquals((short) 10, exec("def x = 5L; return (short) (x <<= 1);")); + assertEquals((char) 10, exec("def x = 5L; return (char) (x <<= 1);")); assertEquals(10, exec("def x = 5L; return (int) (x <<= 1);")); assertEquals(10F, exec("def x = 5L; return (float) (x <<= 1);")); assertEquals(10L, exec("def x = 5L; return (long) (x <<= 1);")); @@ -255,24 +235,12 @@ public void testShiftCompoundAssignmentDef() { * Test that without a cast, we fail when conversions would narrow. */ public void testIllegalConversionsDef() { - expectScriptThrows(ClassCastException.class, () -> { - exec("def x = 5L; int y = +x; return y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("def x = 5L; int y = (x + x); return y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("def x = true; int y = +x; return y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("def x = true; int y = (x ^ false); return y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("def x = 5L; boolean y = +x; return y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("def x = 5L; boolean y = (x + x); return y"); - }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = 5L; int y = +x; return y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = 5L; int y = (x + x); return y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = true; int y = +x; return y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = true; int y = (x ^ false); return y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = 5L; boolean y = +x; return y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = 5L; boolean y = (x + x); return y"); }); } public void testUnboxMethodParameters() { @@ -294,27 +262,21 @@ public void testIllegalCastInMethodArgument() { * (stuff that methodhandles explicitCastArguments would otherwise allow) */ public void testIllegalExplicitConversionsDef() { - expectScriptThrows(ClassCastException.class, () -> { - exec("def x = true; int y = (int) +x; return y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("def x = true; int y = (int) (x ^ false); return y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("def x = 5L; boolean y = (boolean) +x; return y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("def x = 5L; boolean y = (boolean) (x + x); return y"); - }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = true; int y = (int) +x; return y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = true; int y = (int) (x ^ false); return y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = 5L; boolean y = (boolean) +x; return y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = 5L; boolean y = (boolean) (x + x); return y"); }); } public void testIllegalVoidCasts() { - expectScriptThrows(IllegalArgumentException.class, () -> { - exec("def map = ['a': 1,'b': 2,'c': 3]; map.c = Collections.sort(new ArrayList(map.keySet()));"); - }); - expectScriptThrows(IllegalArgumentException.class, () -> { - exec("Map map = ['a': 1,'b': 2,'c': 3]; def x = new HashMap(); x.put(1, map.clear());"); - }); + expectScriptThrows( + IllegalArgumentException.class, + () -> { exec("def map = ['a': 1,'b': 2,'c': 3]; map.c = Collections.sort(new ArrayList(map.keySet()));"); } + ); + expectScriptThrows( + IllegalArgumentException.class, + () -> { exec("Map map = ['a': 1,'b': 2,'c': 3]; def x = new HashMap(); x.put(1, map.clear());"); } + ); } public void testBoxedDefCalls() { @@ -323,13 +285,16 @@ public void testBoxedDefCalls() { assertEquals(1, exec("int x = 1; def y = 2.0; y.compareTo(x);")); assertEquals(-1, exec("Integer x = Integer.valueOf(3); def y = 2.0; y.compareTo(x);")); assertEquals(2, exec("def f = new org.elasticsearch.painless.FeatureTestObject(); f.i = (byte)2; f.i")); - assertEquals(4.0, exec( - "def x = new org.elasticsearch.painless.FeatureTestObject(); " + - "Byte i = Byte.valueOf(3); " + - "byte j = 1;" + - "Short s = Short.valueOf(-2);" + - "x.mixedAdd(j, i, (char)2, s)" - )); + assertEquals( + 4.0, + exec( + "def x = new org.elasticsearch.painless.FeatureTestObject(); " + + "Byte i = Byte.valueOf(3); " + + "byte j = 1;" + + "Short s = Short.valueOf(-2);" + + "x.mixedAdd(j, i, (char)2, s)" + ) + ); assertNull(exec("def f = new org.elasticsearch.painless.FeatureTestObject(); f.i = null; f.i")); expectScriptThrows(ClassCastException.class, () -> exec("def x = 2.0; def y = 1; y.compareTo(x);")); expectScriptThrows(ClassCastException.class, () -> exec("float f = 1.0f; def y = 1; y.compareTo(f);")); diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/GetByPathAugmentationTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/GetByPathAugmentationTests.java index f624c88034b18..a83d86ddf4c10 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/GetByPathAugmentationTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/GetByPathAugmentationTests.java @@ -8,7 +8,6 @@ package org.elasticsearch.painless; - import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -19,7 +18,7 @@ public class GetByPathAugmentationTests extends ScriptTestCase { private final String k001Key = "k011"; private final String k001Value = "b"; - private final Map k001Obj = new HashMap<>(); + private final Map k001Obj = new HashMap<>(); private final String k001MapStr = "['" + k001Key + "': '" + k001Value + "']"; private final String mapMapList = "['k0': ['k01': [['k010': 'a'], " + k001MapStr + "]], 'k1': ['q']]"; @@ -72,10 +71,7 @@ private IllegalArgumentException assertPathError(String collection, String key, } private IllegalArgumentException assertPathError(String script, String message) { - IllegalArgumentException illegal = expectScriptThrows( - IllegalArgumentException.class, - () -> exec(script) - ); + IllegalArgumentException illegal = expectScriptThrows(IllegalArgumentException.class, () -> exec(script)); assertEquals(message, illegal.getMessage()); return illegal; } @@ -205,37 +201,24 @@ public void testTrailingDot() { public void testBiListDefaultBadIndex() { String path = "1.k0"; - IllegalArgumentException err = assertPathError( - "[['a','b'],['c','d']]", - path, - "'foo'", - numberFormat("k0", path, 1)); + IllegalArgumentException err = assertPathError("[['a','b'],['c','d']]", path, "'foo'", numberFormat("k0", path, 1)); assertEquals(err.getCause().getClass(), NumberFormatException.class); } public void testBiMapListDefaultBadIndex() { String path = "k0.k01.k012"; - IllegalArgumentException err = assertPathError( - mapMapList, - path, - "'foo'", - numberFormat("k012", path, 2)); + IllegalArgumentException err = assertPathError(mapMapList, path, "'foo'", numberFormat("k012", path, 2)); assertEquals(err.getCause().getClass(), NumberFormatException.class); } public void testListMapBiListObjectDefaultBadIndex() { String path = "2.m2.a8"; - IllegalArgumentException err = assertPathError( - listMapListList, - path, - "'foo'", - numberFormat("a8", path, 2)); + IllegalArgumentException err = assertPathError(listMapListList, path, "'foo'", numberFormat("a8", path, 2)); assertEquals(err.getCause().getClass(), NumberFormatException.class); } public void testNonContainerDefaultBadIndex() { - assertPathError(mapMap, "a.b.c", "'foo'", - "Non-container [java.lang.String] at [c], index [2] in path [a.b.c]"); + assertPathError(mapMap, "a.b.c", "'foo'", "Non-container [java.lang.String] at [c], index [2] in path [a.b.c]"); } public void testDoubleDotDefault() { diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/IncrementTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/IncrementTests.java index 27d8e21dde697..87887477ab87e 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/IncrementTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/IncrementTests.java @@ -13,25 +13,25 @@ public class IncrementTests extends ScriptTestCase { /** incrementing byte values */ public void testIncrementByte() { - assertEquals((byte)0, exec("byte x = (byte)0; return x++;")); - assertEquals((byte)0, exec("byte x = (byte)0; return x--;")); - assertEquals((byte)1, exec("byte x = (byte)0; return ++x;")); - assertEquals((byte)-1, exec("byte x = (byte)0; return --x;")); + assertEquals((byte) 0, exec("byte x = (byte)0; return x++;")); + assertEquals((byte) 0, exec("byte x = (byte)0; return x--;")); + assertEquals((byte) 1, exec("byte x = (byte)0; return ++x;")); + assertEquals((byte) -1, exec("byte x = (byte)0; return --x;")); } /** incrementing char values */ public void testIncrementChar() { - assertEquals((char)0, exec("char x = (char)0; return x++;")); - assertEquals((char)1, exec("char x = (char)1; return x--;")); - assertEquals((char)1, exec("char x = (char)0; return ++x;")); + assertEquals((char) 0, exec("char x = (char)0; return x++;")); + assertEquals((char) 1, exec("char x = (char)1; return x--;")); + assertEquals((char) 1, exec("char x = (char)0; return ++x;")); } /** incrementing short values */ public void testIncrementShort() { - assertEquals((short)0, exec("short x = (short)0; return x++;")); - assertEquals((short)0, exec("short x = (short)0; return x--;")); - assertEquals((short)1, exec("short x = (short)0; return ++x;")); - assertEquals((short)-1, exec("short x = (short)0; return --x;")); + assertEquals((short) 0, exec("short x = (short)0; return x++;")); + assertEquals((short) 0, exec("short x = (short)0; return x--;")); + assertEquals((short) 1, exec("short x = (short)0; return ++x;")); + assertEquals((short) -1, exec("short x = (short)0; return --x;")); } /** incrementing integer values */ @@ -68,17 +68,17 @@ public void testIncrementDouble() { /** incrementing def values */ public void testIncrementDef() { - assertEquals((byte)0, exec("def x = (byte)0; return x++;")); - assertEquals((byte)0, exec("def x = (byte)0; return x--;")); - assertEquals((byte)1, exec("def x = (byte)0; return ++x;")); - assertEquals((byte)-1, exec("def x = (byte)0; return --x;")); - assertEquals((char)0, exec("def x = (char)0; return x++;")); - assertEquals((char)1, exec("def x = (char)1; return x--;")); - assertEquals((char)1, exec("def x = (char)0; return ++x;")); - assertEquals((short)0, exec("def x = (short)0; return x++;")); - assertEquals((short)0, exec("def x = (short)0; return x--;")); - assertEquals((short)1, exec("def x = (short)0; return ++x;")); - assertEquals((short)-1, exec("def x = (short)0; return --x;")); + assertEquals((byte) 0, exec("def x = (byte)0; return x++;")); + assertEquals((byte) 0, exec("def x = (byte)0; return x--;")); + assertEquals((byte) 1, exec("def x = (byte)0; return ++x;")); + assertEquals((byte) -1, exec("def x = (byte)0; return --x;")); + assertEquals((char) 0, exec("def x = (char)0; return x++;")); + assertEquals((char) 1, exec("def x = (char)1; return x--;")); + assertEquals((char) 1, exec("def x = (char)0; return ++x;")); + assertEquals((short) 0, exec("def x = (short)0; return x++;")); + assertEquals((short) 0, exec("def x = (short)0; return x--;")); + assertEquals((short) 1, exec("def x = (short)0; return ++x;")); + assertEquals((short) -1, exec("def x = (short)0; return --x;")); assertEquals(0, exec("def x = 0; return x++;")); assertEquals(0, exec("def x = 0; return x--;")); assertEquals(1, exec("def x = 0; return ++x;")); diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/InitializerTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/InitializerTests.java index db2d7de575d8e..2e60138f6eb35 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/InitializerTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/InitializerTests.java @@ -15,13 +15,13 @@ public class InitializerTests extends ScriptTestCase { - @SuppressWarnings({"rawtypes"}) + @SuppressWarnings({ "rawtypes" }) public void testArrayInitializers() { - int[] ints = (int[])exec("new int[] {}"); + int[] ints = (int[]) exec("new int[] {}"); assertEquals(0, ints.length); - ints = (int[])exec("new int[] {5, 7, -1, 14}"); + ints = (int[]) exec("new int[] {5, 7, -1, 14}"); assertEquals(4, ints.length); assertEquals(5, ints[0]); @@ -29,7 +29,7 @@ public void testArrayInitializers() { assertEquals(-1, ints[2]); assertEquals(14, ints[3]); - ints = (int[])exec("int y = 2; int z = 3; int[] x = new int[] {y*z, y + z, y - z, y, z}; return x;"); + ints = (int[]) exec("int y = 2; int z = 3; int[] x = new int[] {y*z, y + z, y - z, y, z}; return x;"); assertEquals(5, ints.length); assertEquals(6, ints[0]); @@ -38,8 +38,9 @@ public void testArrayInitializers() { assertEquals(2, ints[3]); assertEquals(3, ints[4]); - Object[] objects = (Object[])exec("int y = 2; List z = new ArrayList(); String s = 'aaa';" + - "Object[] x = new Object[] {y, z, 1 + s, s + 'aaa'}; return x;"); + Object[] objects = (Object[]) exec( + "int y = 2; List z = new ArrayList(); String s = 'aaa';" + "Object[] x = new Object[] {y, z, 1 + s, s + 'aaa'}; return x;" + ); assertEquals(4, objects.length); assertEquals(Integer.valueOf(2), objects[0]); @@ -48,13 +49,13 @@ public void testArrayInitializers() { assertEquals("aaaaaa", objects[3]); } - @SuppressWarnings({"rawtypes"}) + @SuppressWarnings({ "rawtypes" }) public void testListInitializers() { - List list = (List)exec("[]"); + List list = (List) exec("[]"); assertEquals(0, list.size()); - list = (List)exec("[5, 7, -1, 14]"); + list = (List) exec("[5, 7, -1, 14]"); assertEquals(4, list.size()); assertEquals(5, list.get(0)); @@ -62,7 +63,7 @@ public void testListInitializers() { assertEquals(-1, list.get(2)); assertEquals(14, list.get(3)); - list = (List)exec("int y = 2; int z = 3; def x = [y*z, y + z, y - z, y, z]; return x;"); + list = (List) exec("int y = 2; int z = 3; def x = [y*z, y + z, y - z, y, z]; return x;"); assertEquals(5, list.size()); assertEquals(6, list.get(0)); @@ -71,45 +72,44 @@ public void testListInitializers() { assertEquals(2, list.get(3)); assertEquals(3, list.get(4)); - list = (List)exec("int y = 2; List z = new ArrayList(); String s = 'aaa'; List x = [y, z, 1 + s, s + 'aaa']; return x;"); + list = (List) exec("int y = 2; List z = new ArrayList(); String s = 'aaa'; List x = [y, z, 1 + s, s + 'aaa']; return x;"); assertEquals(4, list.size()); assertEquals(Integer.valueOf(2), list.get(0)); assertEquals(new ArrayList(), list.get(1)); - assertEquals("1aaa", list.get(2)); + assertEquals("1aaa", list.get(2)); assertEquals("aaaaaa", list.get(3)); } - @SuppressWarnings({"rawtypes"}) + @SuppressWarnings({ "rawtypes" }) public void testMapInitializers() { - Map map = (Map)exec("[:]"); + Map map = (Map) exec("[:]"); assertEquals(0, map.size()); - map = (Map)exec("[5 : 7, -1 : 14]"); + map = (Map) exec("[5 : 7, -1 : 14]"); assertEquals(2, map.size()); assertEquals(Integer.valueOf(7), map.get(5)); assertEquals(Integer.valueOf(14), map.get(-1)); - map = (Map)exec("int y = 2; int z = 3; Map x = [y*z : y + z, y - z : y, z : z]; return x;"); + map = (Map) exec("int y = 2; int z = 3; Map x = [y*z : y + z, y - z : y, z : z]; return x;"); assertEquals(3, map.size()); assertEquals(Integer.valueOf(5), map.get(6)); assertEquals(Integer.valueOf(2), map.get(-1)); assertEquals(Integer.valueOf(3), map.get(3)); - map = (Map)exec("int y = 2; List z = new ArrayList(); String s = 'aaa';" + - "def x = [y : z, 1 + s : s + 'aaa']; return x;"); + map = (Map) exec("int y = 2; List z = new ArrayList(); String s = 'aaa';" + "def x = [y : z, 1 + s : s + 'aaa']; return x;"); assertEquals(2, map.size()); assertEquals(new ArrayList(), map.get(2)); assertEquals("aaaaaa", map.get("1aaa")); } - @SuppressWarnings({"unchecked", "rawtypes"}) + @SuppressWarnings({ "unchecked", "rawtypes" }) public void testCrazyInitializer() { - Map map = (Map)exec("int y = 2; int z = 3; Map x = [y*z : y + z, 's' : [y, [y : [[z], [], [:]]]], z : [z, 9]]; return x;"); + Map map = (Map) exec("int y = 2; int z = 3; Map x = [y*z : y + z, 's' : [y, [y : [[z], [], [:]]]], z : [z, 9]]; return x;"); List list0 = new ArrayList(); list0.add(3); diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/InjectionTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/InjectionTests.java index e9ec323de496f..8c38ea49c26f6 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/InjectionTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/InjectionTests.java @@ -11,196 +11,268 @@ public class InjectionTests extends ScriptTestCase { public void testInjection() { - assertEquals(16, - exec("org.elasticsearch.painless.FeatureTestObject.staticNumberArgument(8);")); + assertEquals(16, exec("org.elasticsearch.painless.FeatureTestObject.staticNumberArgument(8);")); } public void testInstanceInjection() { - assertEquals(1000, - exec("org.elasticsearch.painless.FeatureTestObject f = new org.elasticsearch.painless.FeatureTestObject(100, 0); " + - "f.injectTimesX(5)")); + assertEquals( + 1000, + exec( + "org.elasticsearch.painless.FeatureTestObject f = new org.elasticsearch.painless.FeatureTestObject(100, 0); " + + "f.injectTimesX(5)" + ) + ); } public void testInstanceInjectWithLambda() { - assertEquals(2000, - exec("org.elasticsearch.painless.FeatureTestObject f = new org.elasticsearch.painless.FeatureTestObject(100, 0); " + - "f.injectWithLambda(x -> 2*x, 5)")); + assertEquals( + 2000, + exec( + "org.elasticsearch.painless.FeatureTestObject f = new org.elasticsearch.painless.FeatureTestObject(100, 0); " + + "f.injectWithLambda(x -> 2*x, 5)" + ) + ); } public void testInstanceInjectWithDefLambda() { - assertEquals(2000, - exec("def f = new org.elasticsearch.painless.FeatureTestObject(100, 0); f.injectWithLambda(x -> 2*x, (short)5)")); + assertEquals( + 2000, + exec("def f = new org.elasticsearch.painless.FeatureTestObject(100, 0); f.injectWithLambda(x -> 2*x, (short)5)") + ); } public void testInjectionOnDefNoInject() { - assertEquals(1000, - exec("def d = new org.elasticsearch.painless.FeatureTestObject(100, 0); d.injectTimesX((short)5)")); + assertEquals(1000, exec("def d = new org.elasticsearch.painless.FeatureTestObject(100, 0); d.injectTimesX((short)5)")); } public void testInjectionOnMethodReference() { - assertEquals(60, - exec( - "def ft0 = new org.elasticsearch.painless.FeatureTestObject(2, 0); " + - "org.elasticsearch.painless.FeatureTestObject ft1 = " + - " new org.elasticsearch.painless.FeatureTestObject(1000, 0); " + - "ft1.timesSupplier(ft0::injectTimesX, (short)3, 5)")); + assertEquals( + 60, + exec( + "def ft0 = new org.elasticsearch.painless.FeatureTestObject(2, 0); " + + "org.elasticsearch.painless.FeatureTestObject ft1 = " + + " new org.elasticsearch.painless.FeatureTestObject(1000, 0); " + + "ft1.timesSupplier(ft0::injectTimesX, (short)3, 5)" + ) + ); } public void testInjectionOnMethodReference2() { - assertEquals(60, - exec( - "org.elasticsearch.painless.FeatureTestObject ft0 = new org.elasticsearch.painless.FeatureTestObject(2, 0); " + - "def ft1 = new org.elasticsearch.painless.FeatureTestObject(1000, 0); " + - "ft1.timesSupplier(ft0::injectTimesX, (short)3, 5)")); + assertEquals( + 60, + exec( + "org.elasticsearch.painless.FeatureTestObject ft0 = new org.elasticsearch.painless.FeatureTestObject(2, 0); " + + "def ft1 = new org.elasticsearch.painless.FeatureTestObject(1000, 0); " + + "ft1.timesSupplier(ft0::injectTimesX, (short)3, 5)" + ) + ); } public void testInjectionOnMethodReference3() { - assertEquals(60, - exec( - "def ft0 = new org.elasticsearch.painless.FeatureTestObject(2, 0); " + - "def ft1 = new org.elasticsearch.painless.FeatureTestObject(1000, 0); " + - "ft1.timesSupplier(ft0::injectTimesX, (short)3, 5)")); + assertEquals( + 60, + exec( + "def ft0 = new org.elasticsearch.painless.FeatureTestObject(2, 0); " + + "def ft1 = new org.elasticsearch.painless.FeatureTestObject(1000, 0); " + + "ft1.timesSupplier(ft0::injectTimesX, (short)3, 5)" + ) + ); } public void testAugmentedInstanceInjection() { - assertEquals(1000, - exec("org.elasticsearch.painless.FeatureTestObject f = new org.elasticsearch.painless.FeatureTestObject(100, 0); " + - "f.augmentInjectTimesX(5)")); + assertEquals( + 1000, + exec( + "org.elasticsearch.painless.FeatureTestObject f = new org.elasticsearch.painless.FeatureTestObject(100, 0); " + + "f.augmentInjectTimesX(5)" + ) + ); } public void testAugmentedInstanceInjectWithLambda() { - assertEquals(2000, - exec("org.elasticsearch.painless.FeatureTestObject f = new org.elasticsearch.painless.FeatureTestObject(100, 0); " + - "f.augmentInjectWithLambda(x -> 2*x, 5)")); + assertEquals( + 2000, + exec( + "org.elasticsearch.painless.FeatureTestObject f = new org.elasticsearch.painless.FeatureTestObject(100, 0); " + + "f.augmentInjectWithLambda(x -> 2*x, 5)" + ) + ); } public void testAugmentedInstanceInjectWithDefLambda() { - assertEquals(2000, - exec("def f = new org.elasticsearch.painless.FeatureTestObject(100, 0); f.augmentInjectWithLambda(x -> 2*x, (short)5)")); + assertEquals( + 2000, + exec("def f = new org.elasticsearch.painless.FeatureTestObject(100, 0); f.augmentInjectWithLambda(x -> 2*x, (short)5)") + ); } public void testAugmentedInjectionOnDefNoInject() { - assertEquals(1000, - exec("def d = new org.elasticsearch.painless.FeatureTestObject(100, 0); d.augmentInjectTimesX((short)5)")); + assertEquals(1000, exec("def d = new org.elasticsearch.painless.FeatureTestObject(100, 0); d.augmentInjectTimesX((short)5)")); } public void testAugmentedInjectionOnMethodReference() { - assertEquals(60, - exec( - "def ft0 = new org.elasticsearch.painless.FeatureTestObject(2, 0); " + - "org.elasticsearch.painless.FeatureTestObject ft1 = " + - " new org.elasticsearch.painless.FeatureTestObject(1000, 0); " + - "ft1.augmentTimesSupplier(ft0::augmentInjectTimesX, (short)3, 5)")); + assertEquals( + 60, + exec( + "def ft0 = new org.elasticsearch.painless.FeatureTestObject(2, 0); " + + "org.elasticsearch.painless.FeatureTestObject ft1 = " + + " new org.elasticsearch.painless.FeatureTestObject(1000, 0); " + + "ft1.augmentTimesSupplier(ft0::augmentInjectTimesX, (short)3, 5)" + ) + ); } public void testAugmentedInjectionOnMethodReference2() { - assertEquals(60, - exec( - "org.elasticsearch.painless.FeatureTestObject ft0 = new org.elasticsearch.painless.FeatureTestObject(2, 0); " + - "def ft1 = new org.elasticsearch.painless.FeatureTestObject(1000, 0); " + - "ft1.augmentTimesSupplier(ft0::augmentInjectTimesX, (short)3, 5)")); + assertEquals( + 60, + exec( + "org.elasticsearch.painless.FeatureTestObject ft0 = new org.elasticsearch.painless.FeatureTestObject(2, 0); " + + "def ft1 = new org.elasticsearch.painless.FeatureTestObject(1000, 0); " + + "ft1.augmentTimesSupplier(ft0::augmentInjectTimesX, (short)3, 5)" + ) + ); } public void testAugmentedInjectionOnMethodReference3() { - assertEquals(60, - exec( - "def ft0 = new org.elasticsearch.painless.FeatureTestObject(2, 0); " + - "def ft1 = new org.elasticsearch.painless.FeatureTestObject(1000, 0); " + - "ft1.augmentTimesSupplier(ft0::augmentInjectTimesX, (short)3, 5)")); + assertEquals( + 60, + exec( + "def ft0 = new org.elasticsearch.painless.FeatureTestObject(2, 0); " + + "def ft1 = new org.elasticsearch.painless.FeatureTestObject(1000, 0); " + + "ft1.augmentTimesSupplier(ft0::augmentInjectTimesX, (short)3, 5)" + ) + ); } public void testInstanceMultiInjection() { - assertEquals(6000, - exec("org.elasticsearch.painless.FeatureTestObject f = new org.elasticsearch.painless.FeatureTestObject(100, 0); " + - "f.injectMultiTimesX(5)")); + assertEquals( + 6000, + exec( + "org.elasticsearch.painless.FeatureTestObject f = new org.elasticsearch.painless.FeatureTestObject(100, 0); " + + "f.injectMultiTimesX(5)" + ) + ); } public void testInstanceMultiInjectWithLambda() { - assertEquals(8000, - exec("org.elasticsearch.painless.FeatureTestObject f = new org.elasticsearch.painless.FeatureTestObject(100, 0); " + - "f.injectMultiWithLambda(x -> 2*x, 5)")); + assertEquals( + 8000, + exec( + "org.elasticsearch.painless.FeatureTestObject f = new org.elasticsearch.painless.FeatureTestObject(100, 0); " + + "f.injectMultiWithLambda(x -> 2*x, 5)" + ) + ); } public void testInstanceMultiInjectWithDefLambda() { - assertEquals(2000, - exec("def f = new org.elasticsearch.painless.FeatureTestObject(100, 0); f.injectWithLambda(x -> 2*x, (short)5)")); + assertEquals( + 2000, + exec("def f = new org.elasticsearch.painless.FeatureTestObject(100, 0); f.injectWithLambda(x -> 2*x, (short)5)") + ); } public void testMultiInjectionOnDefNoMultiInject() { - assertEquals(6000, - exec("def d = new org.elasticsearch.painless.FeatureTestObject(100, 0); d.injectMultiTimesX((short)5)")); + assertEquals(6000, exec("def d = new org.elasticsearch.painless.FeatureTestObject(100, 0); d.injectMultiTimesX((short)5)")); } public void testMultiInjectionOnMethodReference() { - assertEquals(60, - exec( - "def ft0 = new org.elasticsearch.painless.FeatureTestObject(2, 0); " + - "org.elasticsearch.painless.FeatureTestObject ft1 = " + - " new org.elasticsearch.painless.FeatureTestObject(1000, 0); " + - "ft1.timesSupplier(ft0::injectTimesX, (short)3, 5)")); + assertEquals( + 60, + exec( + "def ft0 = new org.elasticsearch.painless.FeatureTestObject(2, 0); " + + "org.elasticsearch.painless.FeatureTestObject ft1 = " + + " new org.elasticsearch.painless.FeatureTestObject(1000, 0); " + + "ft1.timesSupplier(ft0::injectTimesX, (short)3, 5)" + ) + ); } public void testMultiInjectionOnMethodReference2() { - assertEquals(60, - exec( - "org.elasticsearch.painless.FeatureTestObject ft0 = new org.elasticsearch.painless.FeatureTestObject(2, 0); " + - "def ft1 = new org.elasticsearch.painless.FeatureTestObject(1000, 0); " + - "ft1.timesSupplier(ft0::injectTimesX, (short)3, 5)")); + assertEquals( + 60, + exec( + "org.elasticsearch.painless.FeatureTestObject ft0 = new org.elasticsearch.painless.FeatureTestObject(2, 0); " + + "def ft1 = new org.elasticsearch.painless.FeatureTestObject(1000, 0); " + + "ft1.timesSupplier(ft0::injectTimesX, (short)3, 5)" + ) + ); } public void testMultiInjectionOnMethodReference3() { - assertEquals(60, - exec( - "def ft0 = new org.elasticsearch.painless.FeatureTestObject(2, 0); " + - "def ft1 = new org.elasticsearch.painless.FeatureTestObject(1000, 0); " + - "ft1.timesSupplier(ft0::injectTimesX, (short)3, 5)")); + assertEquals( + 60, + exec( + "def ft0 = new org.elasticsearch.painless.FeatureTestObject(2, 0); " + + "def ft1 = new org.elasticsearch.painless.FeatureTestObject(1000, 0); " + + "ft1.timesSupplier(ft0::injectTimesX, (short)3, 5)" + ) + ); } public void testAugmentedInstanceMultiInjection() { - assertEquals(5000, - exec("org.elasticsearch.painless.FeatureTestObject f = new org.elasticsearch.painless.FeatureTestObject(100, 0); " + - "f.augmentInjectMultiTimesX(5)")); + assertEquals( + 5000, + exec( + "org.elasticsearch.painless.FeatureTestObject f = new org.elasticsearch.painless.FeatureTestObject(100, 0); " + + "f.augmentInjectMultiTimesX(5)" + ) + ); } public void testAugmentedInstanceMultiInjectWithLambda() { - assertEquals(20000, - exec("org.elasticsearch.painless.FeatureTestObject f = new org.elasticsearch.painless.FeatureTestObject(100, 0); " + - "f.augmentInjectMultiWithLambda(x -> 2*x, 5)")); + assertEquals( + 20000, + exec( + "org.elasticsearch.painless.FeatureTestObject f = new org.elasticsearch.painless.FeatureTestObject(100, 0); " + + "f.augmentInjectMultiWithLambda(x -> 2*x, 5)" + ) + ); } public void testAugmentedInstanceMultiInjectWithDefLambda() { - assertEquals(20000, - exec("def f = new org.elasticsearch.painless.FeatureTestObject(100, 0); " + - "f.augmentInjectMultiWithLambda(x -> 2*x, (short)5)")); + assertEquals( + 20000, + exec( + "def f = new org.elasticsearch.painless.FeatureTestObject(100, 0); " + "f.augmentInjectMultiWithLambda(x -> 2*x, (short)5)" + ) + ); } public void testAugmentedMultiInjectionOnDefNoMultiInject() { - assertEquals(5000, - exec("def d = new org.elasticsearch.painless.FeatureTestObject(100, 0); d.augmentInjectMultiTimesX((short)5)")); + assertEquals(5000, exec("def d = new org.elasticsearch.painless.FeatureTestObject(100, 0); d.augmentInjectMultiTimesX((short)5)")); } public void testAugmentedMultiInjectionOnMethodReference() { - assertEquals(300, - exec( - "def ft0 = new org.elasticsearch.painless.FeatureTestObject(2, 0); " + - "org.elasticsearch.painless.FeatureTestObject ft1 = " + - " new org.elasticsearch.painless.FeatureTestObject(1000, 0); " + - "ft1.augmentTimesSupplier(ft0::augmentInjectMultiTimesX, (short)3, 5)")); + assertEquals( + 300, + exec( + "def ft0 = new org.elasticsearch.painless.FeatureTestObject(2, 0); " + + "org.elasticsearch.painless.FeatureTestObject ft1 = " + + " new org.elasticsearch.painless.FeatureTestObject(1000, 0); " + + "ft1.augmentTimesSupplier(ft0::augmentInjectMultiTimesX, (short)3, 5)" + ) + ); } public void testAugmentedMultiInjectionOnMethodReference2() { - assertEquals(300, - exec( - "org.elasticsearch.painless.FeatureTestObject ft0 = new org.elasticsearch.painless.FeatureTestObject(2, 0); " + - "def ft1 = new org.elasticsearch.painless.FeatureTestObject(1000, 0); " + - "ft1.augmentTimesSupplier(ft0::augmentInjectMultiTimesX, (short)3, 5)")); + assertEquals( + 300, + exec( + "org.elasticsearch.painless.FeatureTestObject ft0 = new org.elasticsearch.painless.FeatureTestObject(2, 0); " + + "def ft1 = new org.elasticsearch.painless.FeatureTestObject(1000, 0); " + + "ft1.augmentTimesSupplier(ft0::augmentInjectMultiTimesX, (short)3, 5)" + ) + ); } public void testAugmentedMultiInjectionOnMethodReference3() { - assertEquals(300, - exec( - "def ft0 = new org.elasticsearch.painless.FeatureTestObject(2, 0); " + - "def ft1 = new org.elasticsearch.painless.FeatureTestObject(1000, 0); " + - "ft1.augmentTimesSupplier(ft0::augmentInjectMultiTimesX, (short)3, 5)")); + assertEquals( + 300, + exec( + "def ft0 = new org.elasticsearch.painless.FeatureTestObject(2, 0); " + + "def ft1 = new org.elasticsearch.painless.FeatureTestObject(1000, 0); " + + "ft1.augmentTimesSupplier(ft0::augmentInjectMultiTimesX, (short)3, 5)" + ) + ); } } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/IntegerOverflowTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/IntegerOverflowTests.java index 18489b6a87924..04cd8f48688bd 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/IntegerOverflowTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/IntegerOverflowTests.java @@ -13,16 +13,16 @@ public class IntegerOverflowTests extends ScriptTestCase { public void testAssignmentAdditionOverflow() { // byte - assertEquals((byte)(0 + 128), exec("byte x = 0; x += 128; return x;")); - assertEquals((byte)(0 + -129), exec("byte x = 0; x += -129; return x;")); + assertEquals((byte) (0 + 128), exec("byte x = 0; x += 128; return x;")); + assertEquals((byte) (0 + -129), exec("byte x = 0; x += -129; return x;")); // short - assertEquals((short)(0 + 32768), exec("short x = 0; x += 32768; return x;")); - assertEquals((short)(0 + -32769), exec("short x = 0; x += -32769; return x;")); + assertEquals((short) (0 + 32768), exec("short x = 0; x += 32768; return x;")); + assertEquals((short) (0 + -32769), exec("short x = 0; x += -32769; return x;")); // char - assertEquals((char)(0 + 65536), exec("char x = 0; x += 65536; return x;")); - assertEquals((char)(0 + -65536), exec("char x = 0; x += -65536; return x;")); + assertEquals((char) (0 + 65536), exec("char x = 0; x += 65536; return x;")); + assertEquals((char) (0 + -65536), exec("char x = 0; x += -65536; return x;")); // int assertEquals(1 + 2147483647, exec("int x = 1; x += 2147483647; return x;")); @@ -35,16 +35,16 @@ public void testAssignmentAdditionOverflow() { public void testAssignmentSubtractionOverflow() { // byte - assertEquals((byte)(0 - -128), exec("byte x = 0; x -= -128; return x;")); - assertEquals((byte)(0 - 129), exec("byte x = 0; x -= 129; return x;")); + assertEquals((byte) (0 - -128), exec("byte x = 0; x -= -128; return x;")); + assertEquals((byte) (0 - 129), exec("byte x = 0; x -= 129; return x;")); // short - assertEquals((short)(0 - -32768), exec("short x = 0; x -= -32768; return x;")); - assertEquals((short)(0 - 32769), exec("short x = 0; x -= 32769; return x;")); + assertEquals((short) (0 - -32768), exec("short x = 0; x -= -32768; return x;")); + assertEquals((short) (0 - 32769), exec("short x = 0; x -= 32769; return x;")); // char - assertEquals((char)(0 - -65536), exec("char x = 0; x -= -65536; return x;")); - assertEquals((char)(0 - 65536), exec("char x = 0; x -= 65536; return x;")); + assertEquals((char) (0 - -65536), exec("char x = 0; x -= -65536; return x;")); + assertEquals((char) (0 - 65536), exec("char x = 0; x -= 65536; return x;")); // int assertEquals(1 - -2147483647, exec("int x = 1; x -= -2147483647; return x;")); @@ -123,8 +123,10 @@ public void testIncrementOverFlow() throws Exception { public void testAddition() throws Exception { assertEquals(2147483647 + 2147483647, exec("int x = 2147483647; int y = 2147483647; return x + y;")); - assertEquals(9223372036854775807L + 9223372036854775807L, - exec("long x = 9223372036854775807L; long y = 9223372036854775807L; return x + y;")); + assertEquals( + 9223372036854775807L + 9223372036854775807L, + exec("long x = 9223372036854775807L; long y = 9223372036854775807L; return x + y;") + ); } public void testAdditionConst() throws Exception { @@ -144,8 +146,10 @@ public void testSubtractionConst() throws Exception { public void testMultiplication() throws Exception { assertEquals(2147483647 * 2147483647, exec("int x = 2147483647; int y = 2147483647; return x * y;")); - assertEquals(9223372036854775807L * 9223372036854775807L, - exec("long x = 9223372036854775807L; long y = 9223372036854775807L; return x * y;")); + assertEquals( + 9223372036854775807L * 9223372036854775807L, + exec("long x = 9223372036854775807L; long y = 9223372036854775807L; return x * y;") + ); } public void testMultiplicationConst() throws Exception { diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/LambdaTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/LambdaTests.java index 1f519f17d8fb0..1700717ecf974 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/LambdaTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/LambdaTests.java @@ -25,40 +25,48 @@ public void testNoArgLambdaDef() { } public void testLambdaWithArgs() { - assertEquals("short", exec("List l = new ArrayList(); l.add('looooong'); l.add('short'); " - + "l.sort((a, b) -> a.length() - b.length()); return l.get(0)")); + assertEquals( + "short", + exec( + "List l = new ArrayList(); l.add('looooong'); l.add('short'); " + + "l.sort((a, b) -> a.length() - b.length()); return l.get(0)" + ) + ); } public void testLambdaWithTypedArgs() { - assertEquals("short", exec("List l = new ArrayList(); l.add('looooong'); l.add('short'); " - + "l.sort((String a, String b) -> a.length() - b.length()); return l.get(0)")); + assertEquals( + "short", + exec( + "List l = new ArrayList(); l.add('looooong'); l.add('short'); " + + "l.sort((String a, String b) -> a.length() - b.length()); return l.get(0)" + ) + ); } public void testPrimitiveLambdas() { - assertEquals(4, exec("List l = new ArrayList(); l.add(1); l.add(1); " - + "return l.stream().mapToInt(x -> x + 1).sum();")); + assertEquals(4, exec("List l = new ArrayList(); l.add(1); l.add(1); " + "return l.stream().mapToInt(x -> x + 1).sum();")); } public void testPrimitiveLambdasWithTypedArgs() { - assertEquals(4, exec("List l = new ArrayList(); l.add(1); l.add(1); " - + "return l.stream().mapToInt(int x -> x + 1).sum();")); + assertEquals(4, exec("List l = new ArrayList(); l.add(1); l.add(1); " + "return l.stream().mapToInt(int x -> x + 1).sum();")); } public void testPrimitiveLambdasDef() { - assertEquals(4, exec("def l = new ArrayList(); l.add(1); l.add(1); " - + "return l.stream().mapToInt(x -> x + 1).sum();")); + assertEquals(4, exec("def l = new ArrayList(); l.add(1); l.add(1); " + "return l.stream().mapToInt(x -> x + 1).sum();")); } public void testPrimitiveLambdasWithTypedArgsDef() { - assertEquals(4, exec("def l = new ArrayList(); l.add(1); l.add(1); " - + "return l.stream().mapToInt(int x -> x + 1).sum();")); + assertEquals(4, exec("def l = new ArrayList(); l.add(1); l.add(1); " + "return l.stream().mapToInt(int x -> x + 1).sum();")); } public void testPrimitiveLambdasConvertible() { - assertEquals(2, exec("List l = new ArrayList(); l.add((short)1); l.add(1); " - + "return l.stream().mapToInt(long x -> (int)1).sum();")); + assertEquals( + 2, + exec("List l = new ArrayList(); l.add((short)1); l.add(1); " + "return l.stream().mapToInt(long x -> (int)1).sum();") + ); } public void testPrimitiveArgs() { @@ -83,26 +91,40 @@ public void testUnneededCurlyStatements() { /** interface ignores return value */ public void testVoidReturn() { - assertEquals(2, exec("List list = new ArrayList(); " - + "list.add(2); " - + "List list2 = new ArrayList(); " - + "list.forEach(x -> list2.add(x));" - + "return list[0]")); + assertEquals( + 2, + exec( + "List list = new ArrayList(); " + + "list.add(2); " + + "List list2 = new ArrayList(); " + + "list.forEach(x -> list2.add(x));" + + "return list[0]" + ) + ); } /** interface ignores return value */ public void testVoidReturnDef() { - assertEquals(2, exec("def list = new ArrayList(); " - + "list.add(2); " - + "List list2 = new ArrayList(); " - + "list.forEach(x -> list2.add(x));" - + "return list[0]")); + assertEquals( + 2, + exec( + "def list = new ArrayList(); " + + "list.add(2); " + + "List list2 = new ArrayList(); " + + "list.forEach(x -> list2.add(x));" + + "return list[0]" + ) + ); } public void testTwoLambdas() { - assertEquals("testingcdefg", exec( - "org.elasticsearch.painless.FeatureTestObject test = new org.elasticsearch.painless.FeatureTestObject(2,3);" + - "return test.twoFunctionsOfX(x -> 'testing'.concat(x), y -> 'abcdefg'.substring(y))")); + assertEquals( + "testingcdefg", + exec( + "org.elasticsearch.painless.FeatureTestObject test = new org.elasticsearch.painless.FeatureTestObject(2,3);" + + "return test.twoFunctionsOfX(x -> 'testing'.concat(x), y -> 'abcdefg'.substring(y))" + ) + ); } public void testNestedLambdas() { @@ -110,11 +132,12 @@ public void testNestedLambdas() { } public void testLambdaInLoop() { - assertEquals(100, exec("int sum = 0; " + - "for (int i = 0; i < 100; i++) {" + - " sum += Optional.empty().orElseGet(() -> 1);" + - "}" + - "return sum;")); + assertEquals( + 100, + exec( + "int sum = 0; " + "for (int i = 0; i < 100; i++) {" + " sum += Optional.empty().orElseGet(() -> 1);" + "}" + "return sum;" + ) + ); } public void testCapture() { @@ -126,19 +149,28 @@ public void testTwoCaptures() { } public void testCapturesAreReadOnly() { - IllegalArgumentException expected = expectScriptThrows(IllegalArgumentException.class, () -> { - exec("List l = new ArrayList(); l.add(1); l.add(1); " - + "return l.stream().mapToInt(x -> { l = null; return x + 1 }).sum();"); - }); + IllegalArgumentException expected = expectScriptThrows( + IllegalArgumentException.class, + () -> { + exec( + "List l = new ArrayList(); l.add(1); l.add(1); " + "return l.stream().mapToInt(x -> { l = null; return x + 1 }).sum();" + ); + } + ); assertTrue(expected.getMessage().contains("is read-only")); } /** Lambda parameters shouldn't be able to mask a variable already in scope */ public void testNoParamMasking() { - IllegalArgumentException expected = expectScriptThrows(IllegalArgumentException.class, () -> { - exec("int x = 0; List l = new ArrayList(); l.add(1); l.add(1); " - + "return l.stream().mapToInt(x -> { x += 1; return x }).sum();"); - }); + IllegalArgumentException expected = expectScriptThrows( + IllegalArgumentException.class, + () -> { + exec( + "int x = 0; List l = new ArrayList(); l.add(1); l.add(1); " + + "return l.stream().mapToInt(x -> { x += 1; return x }).sum();" + ); + } + ); assertTrue(expected.getMessage().contains("already defined")); } @@ -147,42 +179,47 @@ public void testCaptureDef() { } public void testNestedCapture() { - assertEquals(1, exec("boolean x = false; int y = 1;" + - "return Optional.empty().orElseGet(() -> x ? 5 : Optional.empty().orElseGet(() -> y));")); + assertEquals( + 1, + exec("boolean x = false; int y = 1;" + "return Optional.empty().orElseGet(() -> x ? 5 : Optional.empty().orElseGet(() -> y));") + ); } public void testNestedCaptureParams() { - assertEquals(2, exec("int foo(Function f) { return f.apply(1) }" + - "return foo(x -> foo(y -> x + 1))")); + assertEquals(2, exec("int foo(Function f) { return f.apply(1) }" + "return foo(x -> foo(y -> x + 1))")); } public void testWrongArity() { - IllegalArgumentException expected = expectScriptThrows(IllegalArgumentException.class, false, () -> { - exec("Optional.empty().orElseGet(x -> x);"); - }); + IllegalArgumentException expected = expectScriptThrows( + IllegalArgumentException.class, + false, + () -> { exec("Optional.empty().orElseGet(x -> x);"); } + ); assertTrue(expected.getMessage().contains("Incorrect number of parameters")); } public void testWrongArityDef() { - IllegalArgumentException expected = expectScriptThrows(IllegalArgumentException.class, () -> { - exec("def y = Optional.empty(); return y.orElseGet(x -> x);"); - }); + IllegalArgumentException expected = expectScriptThrows( + IllegalArgumentException.class, + () -> { exec("def y = Optional.empty(); return y.orElseGet(x -> x);"); } + ); assertTrue(expected.getMessage(), expected.getMessage().contains("due to an incorrect number of arguments")); } public void testWrongArityNotEnough() { - IllegalArgumentException expected = expectScriptThrows(IllegalArgumentException.class, false, () -> { - exec("List l = new ArrayList(); l.add(1); l.add(1); " - + "return l.stream().mapToInt(() -> 5).sum();"); - }); + IllegalArgumentException expected = expectScriptThrows( + IllegalArgumentException.class, + false, + () -> { exec("List l = new ArrayList(); l.add(1); l.add(1); " + "return l.stream().mapToInt(() -> 5).sum();"); } + ); assertTrue(expected.getMessage().contains("Incorrect number of parameters")); } public void testWrongArityNotEnoughDef() { - IllegalArgumentException expected = expectScriptThrows(IllegalArgumentException.class, () -> { - exec("def l = new ArrayList(); l.add(1); l.add(1); " - + "return l.stream().mapToInt(() -> 5).sum();"); - }); + IllegalArgumentException expected = expectScriptThrows( + IllegalArgumentException.class, + () -> { exec("def l = new ArrayList(); l.add(1); l.add(1); " + "return l.stream().mapToInt(() -> 5).sum();"); } + ); assertTrue(expected.getMessage(), expected.getMessage().contains("due to an incorrect number of arguments")); } @@ -208,34 +245,68 @@ public void testReservedCapture() { assertEquals(true, exec(compare + "compare(() -> { return params['nokey'] }, null)", params, true)); assertEquals(true, exec(compare + "compare(() -> { return params['number'] }, 2)", params, true)); assertEquals(false, exec(compare + "compare(() -> { return params['number'] }, 'value')", params, true)); - assertEquals(false, exec(compare + "compare(() -> { if (params['number'] == 2) { return params['number'] }" + - "else { return params['key'] } }, 'value')", params, true)); - assertEquals(true, exec(compare + "compare(() -> { if (params['number'] == 2) { return params['number'] }" + - "else { return params['key'] } }, 2)", params, true)); - assertEquals(true, exec(compare + "compare(() -> { if (params['number'] == 1) { return params['number'] }" + - "else { return params['key'] } }, 'value')", params, true)); - assertEquals(false, exec(compare + "compare(() -> { if (params['number'] == 1) { return params['number'] }" + - "else { return params['key'] } }, 2)", params, true)); + assertEquals( + false, + exec( + compare + + "compare(() -> { if (params['number'] == 2) { return params['number'] }" + + "else { return params['key'] } }, 'value')", + params, + true + ) + ); + assertEquals( + true, + exec( + compare + "compare(() -> { if (params['number'] == 2) { return params['number'] }" + "else { return params['key'] } }, 2)", + params, + true + ) + ); + assertEquals( + true, + exec( + compare + + "compare(() -> { if (params['number'] == 1) { return params['number'] }" + + "else { return params['key'] } }, 'value')", + params, + true + ) + ); + assertEquals( + false, + exec( + compare + "compare(() -> { if (params['number'] == 1) { return params['number'] }" + "else { return params['key'] } }, 2)", + params, + true + ) + ); } public void testReturnVoid() { - Throwable expected = expectScriptThrows(ClassCastException.class, () -> { - exec("StringBuilder b = new StringBuilder(); List l = [1, 2]; l.stream().mapToLong(i -> b.setLength(i))"); - }); + Throwable expected = expectScriptThrows( + ClassCastException.class, + () -> { exec("StringBuilder b = new StringBuilder(); List l = [1, 2]; l.stream().mapToLong(i -> b.setLength(i))"); } + ); assertThat(expected.getMessage(), containsString("Cannot cast from [void] to [long].")); } public void testReturnVoidDef() { // If we can catch the error at compile time we do - Exception expected = expectScriptThrows(ClassCastException.class, () -> { - exec("StringBuilder b = new StringBuilder(); def l = [1, 2]; l.stream().mapToLong(i -> b.setLength(i))"); - }); + Exception expected = expectScriptThrows( + ClassCastException.class, + () -> { exec("StringBuilder b = new StringBuilder(); def l = [1, 2]; l.stream().mapToLong(i -> b.setLength(i))"); } + ); assertThat(expected.getMessage(), containsString("Cannot cast from [void] to [def].")); // Otherwise we convert the void into a null - assertEquals(Arrays.asList(null, null), - exec("def b = new StringBuilder(); def l = [1, 2]; l.stream().map(i -> b.setLength(i)).collect(Collectors.toList())")); - assertEquals(Arrays.asList(null, null), - exec("def b = new StringBuilder(); List l = [1, 2]; l.stream().map(i -> b.setLength(i)).collect(Collectors.toList())")); + assertEquals( + Arrays.asList(null, null), + exec("def b = new StringBuilder(); def l = [1, 2]; l.stream().map(i -> b.setLength(i)).collect(Collectors.toList())") + ); + assertEquals( + Arrays.asList(null, null), + exec("def b = new StringBuilder(); List l = [1, 2]; l.stream().map(i -> b.setLength(i)).collect(Collectors.toList())") + ); } } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/ListTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/ListTests.java index 45b22fe2c0616..96fa861c6e807 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/ListTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/ListTests.java @@ -32,13 +32,20 @@ protected String valueCtorCall(String valueType, int size) { private String fillValue(String valueType) { switch (valueType) { - case "int": return "0"; - case "long": return "0L"; - case "short": return "(short) 0"; - case "byte": return "(byte) 0"; - case "float": return "0.0f"; - case "double": return "0.0"; // Double is implicit for decimal constants - default: return null; + case "int": + return "0"; + case "long": + return "0L"; + case "short": + return "(short) 0"; + case "byte": + return "(byte) 0"; + case "float": + return "0.0f"; + case "double": + return "0.0"; // Double is implicit for decimal constants + default: + return null; } } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/LookupTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/LookupTests.java index 09dd970adfb6d..c04dc4cd2f893 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/LookupTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/LookupTests.java @@ -25,50 +25,93 @@ public class LookupTests extends ESTestCase { @Before public void setup() { - painlessLookup = PainlessLookupBuilder.buildFromWhitelists(Collections.singletonList( - WhitelistLoader.loadFromResourceFiles(PainlessPlugin.class, "org.elasticsearch.painless.lookup") - )); + painlessLookup = PainlessLookupBuilder.buildFromWhitelists( + Collections.singletonList(WhitelistLoader.loadFromResourceFiles(PainlessPlugin.class, "org.elasticsearch.painless.lookup")) + ); } - public static class A { } // in whitelist - public static class B extends A { } // not in whitelist + public static class A {} // in whitelist + + public static class B extends A {} // not in whitelist + public static class C extends B { // in whitelist - public String getString0() { return "C/0"; } // in whitelist + public String getString0() { + return "C/0"; + } // in whitelist } + public static class D extends B { // in whitelist - public String getString0() { return "D/0"; } // in whitelist - public String getString1(int param0) { return "D/1 (" + param0 + ")"; } // in whitelist + public String getString0() { + return "D/0"; + } // in whitelist + + public String getString1(int param0) { + return "D/1 (" + param0 + ")"; + } // in whitelist } - public interface Z { } // in whitelist - public interface Y { } // not in whitelist - public interface X extends Y, Z { } // not in whitelist - public interface V extends Y, Z { } // in whitelist + public interface Z {} // in whitelist + + public interface Y {} // not in whitelist + + public interface X extends Y, Z {} // not in whitelist + + public interface V extends Y, Z {} // in whitelist + public interface U extends X { // in whitelist String getString2(int x, int y); // in whitelist + String getString1(int param0); // in whitelist + String getString0(); // not in whitelist } + public interface T extends V { // in whitelist String getString1(int param0); // in whitelist + int getInt0(); // in whitelist } - public interface S extends U, X { } // in whitelist - public static class AA implements X { } // in whitelist + public interface S extends U, X {} // in whitelist + + public static class AA implements X {} // in whitelist + public static class AB extends AA implements S { // not in whitelist - public String getString2(int x, int y) { return "" + x + y; } // not in whitelist - public String getString1(int param0) { return "" + param0; } // not in whitelist - public String getString0() { return ""; } // not in whitelist + public String getString2(int x, int y) { + return "" + x + y; + } // not in whitelist + + public String getString1(int param0) { + return "" + param0; + } // not in whitelist + + public String getString0() { + return ""; + } // not in whitelist } + public static class AC extends AB implements V { // in whitelist - public String getString2(int x, int y) { return "" + x + y; } // in whitelist + public String getString2(int x, int y) { + return "" + x + y; + } // in whitelist } + public static class AD extends AA implements X, S, T { // in whitelist - public String getString2(int x, int y) { return "" + x + y; } // in whitelist - public String getString1(int param0) { return "" + param0; } // in whitelist - public String getString0() { return ""; } // not in whitelist - public int getInt0() { return 0; } // in whitelist + public String getString2(int x, int y) { + return "" + x + y; + } // in whitelist + + public String getString1(int param0) { + return "" + param0; + } // in whitelist + + public String getString0() { + return ""; + } // not in whitelist + + public int getInt0() { + return 0; + } // in whitelist } public void testDirectSubClasses() { diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/MapTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/MapTests.java index 899807b3dbf33..6e619401bf84e 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/MapTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/MapTests.java @@ -19,9 +19,9 @@ private void mapAccessesTestCase(String listType) { assertEquals(2, exec(decl + "; return x[0];", true)); assertEquals(1, exec(decl + "; return x['a'];", true)); assertEquals(12, exec(decl + "; return x[123.1];", true)); - assertEquals(val, exec(decl + "; x[ 0] = params.val; return x[ 0];", singletonMap("val", val), true)); + assertEquals(val, exec(decl + "; x[ 0] = params.val; return x[ 0];", singletonMap("val", val), true)); assertEquals("slot", exec(decl + "; x[ 0] = params.val; return x[-5];", singletonMap("val", val), true)); - assertEquals(val, exec(decl + "; x[-5] = params.val; return x[-5];", singletonMap("val", val), true)); + assertEquals(val, exec(decl + "; x[-5] = params.val; return x[-5];", singletonMap("val", val), true)); } public void testMapInDefAccesses() { diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/MultiplicationTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/MultiplicationTests.java index 22fcf47af57e4..9b3be7025b3f5 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/MultiplicationTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/MultiplicationTests.java @@ -9,7 +9,7 @@ package org.elasticsearch.painless; /** Tests for multiplication operator across all types */ -//TODO: NaN/Inf/overflow/... +// TODO: NaN/Inf/overflow/... public class MultiplicationTests extends ScriptTestCase { // TODO: short,byte,char @@ -19,102 +19,102 @@ public void testBasics() throws Exception { } public void testInt() throws Exception { - assertEquals(1*1, exec("int x = 1; int y = 1; return x*y;")); - assertEquals(2*3, exec("int x = 2; int y = 3; return x*y;")); - assertEquals(5*10, exec("int x = 5; int y = 10; return x*y;")); - assertEquals(1*1*2, exec("int x = 1; int y = 1; int z = 2; return x*y*z;")); - assertEquals((1*1)*2, exec("int x = 1; int y = 1; int z = 2; return (x*y)*z;")); - assertEquals(1*(1*2), exec("int x = 1; int y = 1; int z = 2; return x*(y*z);")); - assertEquals(10*0, exec("int x = 10; int y = 0; return x*y;")); - assertEquals(0*0, exec("int x = 0; int y = 0; return x*x;")); + assertEquals(1 * 1, exec("int x = 1; int y = 1; return x*y;")); + assertEquals(2 * 3, exec("int x = 2; int y = 3; return x*y;")); + assertEquals(5 * 10, exec("int x = 5; int y = 10; return x*y;")); + assertEquals(1 * 1 * 2, exec("int x = 1; int y = 1; int z = 2; return x*y*z;")); + assertEquals((1 * 1) * 2, exec("int x = 1; int y = 1; int z = 2; return (x*y)*z;")); + assertEquals(1 * (1 * 2), exec("int x = 1; int y = 1; int z = 2; return x*(y*z);")); + assertEquals(10 * 0, exec("int x = 10; int y = 0; return x*y;")); + assertEquals(0 * 0, exec("int x = 0; int y = 0; return x*x;")); } public void testIntConst() throws Exception { - assertEquals(1*1, exec("return 1*1;")); - assertEquals(2*3, exec("return 2*3;")); - assertEquals(5*10, exec("return 5*10;")); - assertEquals(1*1*2, exec("return 1*1*2;")); - assertEquals((1*1)*2, exec("return (1*1)*2;")); - assertEquals(1*(1*2), exec("return 1*(1*2);")); - assertEquals(10*0, exec("return 10*0;")); - assertEquals(0*0, exec("return 0*0;")); + assertEquals(1 * 1, exec("return 1*1;")); + assertEquals(2 * 3, exec("return 2*3;")); + assertEquals(5 * 10, exec("return 5*10;")); + assertEquals(1 * 1 * 2, exec("return 1*1*2;")); + assertEquals((1 * 1) * 2, exec("return (1*1)*2;")); + assertEquals(1 * (1 * 2), exec("return 1*(1*2);")); + assertEquals(10 * 0, exec("return 10*0;")); + assertEquals(0 * 0, exec("return 0*0;")); } public void testByte() throws Exception { - assertEquals((byte)1*(byte)1, exec("byte x = 1; byte y = 1; return x*y;")); - assertEquals((byte)2*(byte)3, exec("byte x = 2; byte y = 3; return x*y;")); - assertEquals((byte)5*(byte)10, exec("byte x = 5; byte y = 10; return x*y;")); - assertEquals((byte)1*(byte)1*(byte)2, exec("byte x = 1; byte y = 1; byte z = 2; return x*y*z;")); - assertEquals(((byte)1*(byte)1)*(byte)2, exec("byte x = 1; byte y = 1; byte z = 2; return (x*y)*z;")); - assertEquals((byte)1*((byte)1*(byte)2), exec("byte x = 1; byte y = 1; byte z = 2; return x*(y*z);")); - assertEquals((byte)10*(byte)0, exec("byte x = 10; byte y = 0; return x*y;")); - assertEquals((byte)0*(byte)0, exec("byte x = 0; byte y = 0; return x*x;")); + assertEquals((byte) 1 * (byte) 1, exec("byte x = 1; byte y = 1; return x*y;")); + assertEquals((byte) 2 * (byte) 3, exec("byte x = 2; byte y = 3; return x*y;")); + assertEquals((byte) 5 * (byte) 10, exec("byte x = 5; byte y = 10; return x*y;")); + assertEquals((byte) 1 * (byte) 1 * (byte) 2, exec("byte x = 1; byte y = 1; byte z = 2; return x*y*z;")); + assertEquals(((byte) 1 * (byte) 1) * (byte) 2, exec("byte x = 1; byte y = 1; byte z = 2; return (x*y)*z;")); + assertEquals((byte) 1 * ((byte) 1 * (byte) 2), exec("byte x = 1; byte y = 1; byte z = 2; return x*(y*z);")); + assertEquals((byte) 10 * (byte) 0, exec("byte x = 10; byte y = 0; return x*y;")); + assertEquals((byte) 0 * (byte) 0, exec("byte x = 0; byte y = 0; return x*x;")); } public void testLong() throws Exception { - assertEquals(1L*1L, exec("long x = 1; long y = 1; return x*y;")); - assertEquals(2L*3L, exec("long x = 2; long y = 3; return x*y;")); - assertEquals(5L*10L, exec("long x = 5; long y = 10; return x*y;")); - assertEquals(1L*1L*2L, exec("long x = 1; long y = 1; int z = 2; return x*y*z;")); - assertEquals((1L*1L)*2L, exec("long x = 1; long y = 1; int z = 2; return (x*y)*z;")); - assertEquals(1L*(1L*2L), exec("long x = 1; long y = 1; int z = 2; return x*(y*z);")); - assertEquals(10L*0L, exec("long x = 10; long y = 0; return x*y;")); - assertEquals(0L*0L, exec("long x = 0; long y = 0; return x*x;")); + assertEquals(1L * 1L, exec("long x = 1; long y = 1; return x*y;")); + assertEquals(2L * 3L, exec("long x = 2; long y = 3; return x*y;")); + assertEquals(5L * 10L, exec("long x = 5; long y = 10; return x*y;")); + assertEquals(1L * 1L * 2L, exec("long x = 1; long y = 1; int z = 2; return x*y*z;")); + assertEquals((1L * 1L) * 2L, exec("long x = 1; long y = 1; int z = 2; return (x*y)*z;")); + assertEquals(1L * (1L * 2L), exec("long x = 1; long y = 1; int z = 2; return x*(y*z);")); + assertEquals(10L * 0L, exec("long x = 10; long y = 0; return x*y;")); + assertEquals(0L * 0L, exec("long x = 0; long y = 0; return x*x;")); } public void testLongConst() throws Exception { - assertEquals(1L*1L, exec("return 1L*1L;")); - assertEquals(2L*3L, exec("return 2L*3L;")); - assertEquals(5L*10L, exec("return 5L*10L;")); - assertEquals(1L*1L*2L, exec("return 1L*1L*2L;")); - assertEquals((1L*1L)*2L, exec("return (1L*1L)*2L;")); - assertEquals(1L*(1L*2L), exec("return 1L*(1L*2L);")); - assertEquals(10L*0L, exec("return 10L*0L;")); - assertEquals(0L*0L, exec("return 0L*0L;")); + assertEquals(1L * 1L, exec("return 1L*1L;")); + assertEquals(2L * 3L, exec("return 2L*3L;")); + assertEquals(5L * 10L, exec("return 5L*10L;")); + assertEquals(1L * 1L * 2L, exec("return 1L*1L*2L;")); + assertEquals((1L * 1L) * 2L, exec("return (1L*1L)*2L;")); + assertEquals(1L * (1L * 2L), exec("return 1L*(1L*2L);")); + assertEquals(10L * 0L, exec("return 10L*0L;")); + assertEquals(0L * 0L, exec("return 0L*0L;")); } public void testFloat() throws Exception { - assertEquals(1F*1F, exec("float x = 1; float y = 1; return x*y;")); - assertEquals(2F*3F, exec("float x = 2; float y = 3; return x*y;")); - assertEquals(5F*10F, exec("float x = 5; float y = 10; return x*y;")); - assertEquals(1F*1F*2F, exec("float x = 1; float y = 1; float z = 2; return x*y*z;")); - assertEquals((1F*1F)*2F, exec("float x = 1; float y = 1; float z = 2; return (x*y)*z;")); - assertEquals(1F*(1F*2F), exec("float x = 1; float y = 1; float z = 2; return x*(y*z);")); - assertEquals(10F*0F, exec("float x = 10; float y = 0; return x*y;")); - assertEquals(0F*0F, exec("float x = 0; float y = 0; return x*x;")); + assertEquals(1F * 1F, exec("float x = 1; float y = 1; return x*y;")); + assertEquals(2F * 3F, exec("float x = 2; float y = 3; return x*y;")); + assertEquals(5F * 10F, exec("float x = 5; float y = 10; return x*y;")); + assertEquals(1F * 1F * 2F, exec("float x = 1; float y = 1; float z = 2; return x*y*z;")); + assertEquals((1F * 1F) * 2F, exec("float x = 1; float y = 1; float z = 2; return (x*y)*z;")); + assertEquals(1F * (1F * 2F), exec("float x = 1; float y = 1; float z = 2; return x*(y*z);")); + assertEquals(10F * 0F, exec("float x = 10; float y = 0; return x*y;")); + assertEquals(0F * 0F, exec("float x = 0; float y = 0; return x*x;")); } public void testFloatConst() throws Exception { - assertEquals(1F*1F, exec("return 1F*1F;")); - assertEquals(2F*3F, exec("return 2F*3F;")); - assertEquals(5F*10F, exec("return 5F*10F;")); - assertEquals(1F*1F*2F, exec("return 1F*1F*2F;")); - assertEquals((1F*1F)*2F, exec("return (1F*1F)*2F;")); - assertEquals(1F*(1F*2F), exec("return 1F*(1F*2F);")); - assertEquals(10F*0F, exec("return 10F*0F;")); - assertEquals(0F*0F, exec("return 0F*0F;")); + assertEquals(1F * 1F, exec("return 1F*1F;")); + assertEquals(2F * 3F, exec("return 2F*3F;")); + assertEquals(5F * 10F, exec("return 5F*10F;")); + assertEquals(1F * 1F * 2F, exec("return 1F*1F*2F;")); + assertEquals((1F * 1F) * 2F, exec("return (1F*1F)*2F;")); + assertEquals(1F * (1F * 2F), exec("return 1F*(1F*2F);")); + assertEquals(10F * 0F, exec("return 10F*0F;")); + assertEquals(0F * 0F, exec("return 0F*0F;")); } public void testDouble() throws Exception { - assertEquals(1D*1D, exec("double x = 1; double y = 1; return x*y;")); - assertEquals(2D*3D, exec("double x = 2; double y = 3; return x*y;")); - assertEquals(5D*10D, exec("double x = 5; double y = 10; return x*y;")); - assertEquals(1D*1D*2D, exec("double x = 1; double y = 1; double z = 2; return x*y*z;")); - assertEquals((1D*1D)*2D, exec("double x = 1; double y = 1; double z = 2; return (x*y)*z;")); - assertEquals(1D*(1D*2D), exec("double x = 1; double y = 1; double z = 2; return x*(y*z);")); - assertEquals(10D*0D, exec("double x = 10; float y = 0; return x*y;")); - assertEquals(0D*0D, exec("double x = 0; float y = 0; return x*x;")); + assertEquals(1D * 1D, exec("double x = 1; double y = 1; return x*y;")); + assertEquals(2D * 3D, exec("double x = 2; double y = 3; return x*y;")); + assertEquals(5D * 10D, exec("double x = 5; double y = 10; return x*y;")); + assertEquals(1D * 1D * 2D, exec("double x = 1; double y = 1; double z = 2; return x*y*z;")); + assertEquals((1D * 1D) * 2D, exec("double x = 1; double y = 1; double z = 2; return (x*y)*z;")); + assertEquals(1D * (1D * 2D), exec("double x = 1; double y = 1; double z = 2; return x*(y*z);")); + assertEquals(10D * 0D, exec("double x = 10; float y = 0; return x*y;")); + assertEquals(0D * 0D, exec("double x = 0; float y = 0; return x*x;")); } public void testDoubleConst() throws Exception { - assertEquals(1.0*1.0, exec("return 1.0*1.0;")); - assertEquals(2.0*3.0, exec("return 2.0*3.0;")); - assertEquals(5.0*10.0, exec("return 5.0*10.0;")); - assertEquals(1.0*1.0*2.0, exec("return 1.0*1.0*2.0;")); - assertEquals((1.0*1.0)*2.0, exec("return (1.0*1.0)*2.0;")); - assertEquals(1.0*(1.0*2.0), exec("return 1.0*(1.0*2.0);")); - assertEquals(10.0*0.0, exec("return 10.0*0.0;")); - assertEquals(0.0*0.0, exec("return 0.0*0.0;")); + assertEquals(1.0 * 1.0, exec("return 1.0*1.0;")); + assertEquals(2.0 * 3.0, exec("return 2.0*3.0;")); + assertEquals(5.0 * 10.0, exec("return 5.0*10.0;")); + assertEquals(1.0 * 1.0 * 2.0, exec("return 1.0*1.0*2.0;")); + assertEquals((1.0 * 1.0) * 2.0, exec("return (1.0*1.0)*2.0;")); + assertEquals(1.0 * (1.0 * 2.0), exec("return 1.0*(1.0*2.0);")); + assertEquals(10.0 * 0.0, exec("return 10.0*0.0;")); + assertEquals(0.0 * 0.0, exec("return 0.0*0.0;")); } public void testDef() { diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/NoSemiColonTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/NoSemiColonTests.java index 3fefb940554d6..4e8b3da943f40 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/NoSemiColonTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/NoSemiColonTests.java @@ -15,9 +15,9 @@ public class NoSemiColonTests extends ScriptTestCase { public void testDeclarationStatement() { - assertEquals((byte)2, exec("byte a = 2; return a")); - assertEquals((short)2, exec("short a = 2; return a")); - assertEquals((char)2, exec("char a = 2; return a")); + assertEquals((byte) 2, exec("byte a = 2; return a")); + assertEquals((short) 2, exec("short a = 2; return a")); + assertEquals((char) 2, exec("char a = 2; return a")); assertEquals(2, exec("int a = 2; return a")); assertEquals(2L, exec("long a = 2; return a")); assertEquals(2F, exec("float a = 2; return a")); @@ -61,7 +61,7 @@ public void testReturnStatement() { assertEquals(10, exec("return 10")); assertEquals(5, exec("int x = 5; return x")); assertEquals(4, exec("int[] x = new int[2]; x[1] = 4; return x[1]")); - assertEquals(5, ((short[])exec("short[] s = new short[3]; s[1] = 5; return s"))[1]); - assertEquals(10, ((Map)exec("Map s = new HashMap(); s.put(\"x\", 10); return s")).get("x")); + assertEquals(5, ((short[]) exec("short[] s = new short[3]; s[1] = 5; return s"))[1]); + assertEquals(10, ((Map) exec("Map s = new HashMap(); s.put(\"x\", 10); return s")).get("x")); } } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/OrTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/OrTests.java index 69ab0a1a76dc3..7d0390d292d14 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/OrTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/OrTests.java @@ -43,21 +43,13 @@ public void testLongConst() throws Exception { } public void testIllegal() throws Exception { - expectScriptThrows(ClassCastException.class, () -> { - exec("float x = (float)4; int y = 1; return x | y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("double x = (double)4; int y = 1; return x | y"); - }); + expectScriptThrows(ClassCastException.class, () -> { exec("float x = (float)4; int y = 1; return x | y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("double x = (double)4; int y = 1; return x | y"); }); } public void testDef() { - expectScriptThrows(ClassCastException.class, () -> { - exec("def x = (float)4; def y = (byte)1; return x | y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("def x = (double)4; def y = (byte)1; return x | y"); - }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = (float)4; def y = (byte)1; return x | y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = (double)4; def y = (byte)1; return x | y"); }); assertEquals(5, exec("def x = (byte)4; def y = (byte)1; return x | y")); assertEquals(5, exec("def x = (short)4; def y = (byte)1; return x | y")); assertEquals(5, exec("def x = (char)4; def y = (byte)1; return x | y")); @@ -94,19 +86,15 @@ public void testDef() { assertEquals(5, exec("def x = (int)4; def y = (int)1; return x | y")); assertEquals(5L, exec("def x = (long)4; def y = (long)1; return x | y")); - assertEquals(true, exec("def x = true; def y = true; return x | y")); - assertEquals(true, exec("def x = true; def y = false; return x | y")); - assertEquals(true, exec("def x = false; def y = true; return x | y")); + assertEquals(true, exec("def x = true; def y = true; return x | y")); + assertEquals(true, exec("def x = true; def y = false; return x | y")); + assertEquals(true, exec("def x = false; def y = true; return x | y")); assertEquals(false, exec("def x = false; def y = false; return x | y")); } public void testDefTypedLHS() { - expectScriptThrows(ClassCastException.class, () -> { - exec("float x = (float)4; def y = (byte)1; return x | y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("double x = (double)4; def y = (byte)1; return x | y"); - }); + expectScriptThrows(ClassCastException.class, () -> { exec("float x = (float)4; def y = (byte)1; return x | y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("double x = (double)4; def y = (byte)1; return x | y"); }); assertEquals(5, exec("byte x = (byte)4; def y = (byte)1; return x | y")); assertEquals(5, exec("short x = (short)4; def y = (byte)1; return x | y")); assertEquals(5, exec("char x = (char)4; def y = (byte)1; return x | y")); @@ -143,19 +131,15 @@ public void testDefTypedLHS() { assertEquals(5, exec("int x = (int)4; def y = (int)1; return x | y")); assertEquals(5L, exec("long x = (long)4; def y = (long)1; return x | y")); - assertEquals(true, exec("boolean x = true; def y = true; return x | y")); - assertEquals(true, exec("boolean x = true; def y = false; return x | y")); - assertEquals(true, exec("boolean x = false; def y = true; return x | y")); + assertEquals(true, exec("boolean x = true; def y = true; return x | y")); + assertEquals(true, exec("boolean x = true; def y = false; return x | y")); + assertEquals(true, exec("boolean x = false; def y = true; return x | y")); assertEquals(false, exec("boolean x = false; def y = false; return x | y")); } public void testDefTypedRHS() { - expectScriptThrows(ClassCastException.class, () -> { - exec("def x = (float)4; byte y = (byte)1; return x | y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("def x = (double)4; byte y = (byte)1; return x | y"); - }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = (float)4; byte y = (byte)1; return x | y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = (double)4; byte y = (byte)1; return x | y"); }); assertEquals(5, exec("def x = (byte)4; byte y = (byte)1; return x | y")); assertEquals(5, exec("def x = (short)4; byte y = (byte)1; return x | y")); assertEquals(5, exec("def x = (char)4; byte y = (byte)1; return x | y")); @@ -192,9 +176,9 @@ public void testDefTypedRHS() { assertEquals(5, exec("def x = (int)4; int y = (int)1; return x | y")); assertEquals(5L, exec("def x = (long)4; long y = (long)1; return x | y")); - assertEquals(true, exec("def x = true; boolean y = true; return x | y")); - assertEquals(true, exec("def x = true; boolean y = false; return x | y")); - assertEquals(true, exec("def x = false; boolean y = true; return x | y")); + assertEquals(true, exec("def x = true; boolean y = true; return x | y")); + assertEquals(true, exec("def x = true; boolean y = false; return x | y")); + assertEquals(true, exec("def x = false; boolean y = true; return x | y")); assertEquals(false, exec("def x = false; boolean y = false; return x | y")); } @@ -222,18 +206,10 @@ public void testCompoundAssignment() { } public void testBogusCompoundAssignment() { - expectScriptThrows(ClassCastException.class, () -> { - exec("float x = 4; int y = 1; x |= y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("double x = 4; int y = 1; x |= y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("int x = 4; float y = 1; x |= y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("int x = 4; double y = 1; x |= y"); - }); + expectScriptThrows(ClassCastException.class, () -> { exec("float x = 4; int y = 1; x |= y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("double x = 4; int y = 1; x |= y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("int x = 4; float y = 1; x |= y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("int x = 4; double y = 1; x |= y"); }); } public void testDefCompoundAssignment() { @@ -260,17 +236,9 @@ public void testDefCompoundAssignment() { } public void testDefBogusCompoundAssignment() { - expectScriptThrows(ClassCastException.class, () -> { - exec("def x = 4F; int y = 1; x |= y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("def x = 4D; int y = 1; x |= y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("def x = 4; float y = 1; x |= y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("def x = 4; double y = 1; x |= y"); - }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = 4F; int y = 1; x |= y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = 4D; int y = 1; x |= y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = 4; float y = 1; x |= y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = 4; double y = 1; x |= y"); }); } } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/OverloadTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/OverloadTests.java index e709d69f2a2d8..b9d45a9af7328 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/OverloadTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/OverloadTests.java @@ -12,28 +12,40 @@ public class OverloadTests extends ScriptTestCase { public void testMethod() { - //assertEquals(2, exec("return 'abc123abc'.indexOf('c');")); - //assertEquals(8, exec("return 'abc123abc'.indexOf('c', 3);")); - IllegalArgumentException expected = expectScriptThrows(IllegalArgumentException.class, () -> { - exec("return 'abc123abc'.indexOf('c', 3, 'bogus');"); - }); + // assertEquals(2, exec("return 'abc123abc'.indexOf('c');")); + // assertEquals(8, exec("return 'abc123abc'.indexOf('c', 3);")); + IllegalArgumentException expected = expectScriptThrows( + IllegalArgumentException.class, + () -> { exec("return 'abc123abc'.indexOf('c', 3, 'bogus');"); } + ); assertTrue(expected.getMessage().contains("[java.lang.String, indexOf/3]")); } public void testMethodDynamic() { assertEquals(2, exec("def x = 'abc123abc'; return x.indexOf('c');")); assertEquals(8, exec("def x = 'abc123abc'; return x.indexOf('c', 3);")); - IllegalArgumentException expected = expectScriptThrows(IllegalArgumentException.class, () -> { - exec("def x = 'abc123abc'; return x.indexOf('c', 3, 'bogus');"); - }); + IllegalArgumentException expected = expectScriptThrows( + IllegalArgumentException.class, + () -> { exec("def x = 'abc123abc'; return x.indexOf('c', 3, 'bogus');"); } + ); assertTrue(expected.getMessage().contains("dynamic method [java.lang.String, indexOf/3] not found")); } public void testConstructor() { - assertEquals(true, exec("org.elasticsearch.painless.FeatureTestObject f = new org.elasticsearch.painless.FeatureTestObject();" + - "return f.x == 0 && f.y == 0;")); - assertEquals(true, exec("org.elasticsearch.painless.FeatureTestObject f = new org.elasticsearch.painless.FeatureTestObject(1, 2);" + - "return f.x == 1 && f.y == 2;")); + assertEquals( + true, + exec( + "org.elasticsearch.painless.FeatureTestObject f = new org.elasticsearch.painless.FeatureTestObject();" + + "return f.x == 0 && f.y == 0;" + ) + ); + assertEquals( + true, + exec( + "org.elasticsearch.painless.FeatureTestObject f = new org.elasticsearch.painless.FeatureTestObject(1, 2);" + + "return f.x == 1 && f.y == 2;" + ) + ); } public void testStatic() { diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/PostfixTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/PostfixTests.java index 5b3da4998cd9a..b34618eebeb35 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/PostfixTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/PostfixTests.java @@ -19,11 +19,14 @@ public void testConstantPostfixes() { public void testConditionalPostfixes() { assertEquals("5", exec("boolean b = false; (b ? 4 : 5).toString()")); - assertEquals(3, exec( - "Map x = new HashMap(); x['test'] = 3;" + - "Map y = new HashMap(); y['test'] = 4;" + - "boolean b = true;" + - "return (int)(b ? x : y).get('test')") + assertEquals( + 3, + exec( + "Map x = new HashMap(); x['test'] = 3;" + + "Map y = new HashMap(); y['test'] = 4;" + + "boolean b = true;" + + "return (int)(b ? x : y).get('test')" + ) ); } @@ -36,11 +39,14 @@ public void testAssignmentPostfixes() { public void testDefConditionalPostfixes() { assertEquals("5", exec("def b = false; (b ? 4 : 5).toString()")); - assertEquals(3, exec( - "def x = new HashMap(); x['test'] = 3;" + - "def y = new HashMap(); y['test'] = 4;" + - "boolean b = true;" + - "return (b ? x : y).get('test')") + assertEquals( + 3, + exec( + "def x = new HashMap(); x['test'] = 3;" + + "def y = new HashMap(); y['test'] = 4;" + + "boolean b = true;" + + "return (b ? x : y).get('test')" + ) ); } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/PromotionTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/PromotionTests.java index 13528cf998d82..abec712e7a7f9 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/PromotionTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/PromotionTests.java @@ -11,56 +11,56 @@ public class PromotionTests extends ScriptTestCase { public void testBinaryPromotion() throws Exception { // byte/byte - assertEquals((byte)1 + (byte)1, exec("byte x = 1; byte y = 1; return x+y;")); + assertEquals((byte) 1 + (byte) 1, exec("byte x = 1; byte y = 1; return x+y;")); // byte/char - assertEquals((byte)1 + (char)1, exec("byte x = 1; char y = 1; return x+y;")); + assertEquals((byte) 1 + (char) 1, exec("byte x = 1; char y = 1; return x+y;")); // byte/short - assertEquals((byte)1 + (short)1, exec("byte x = 1; short y = 1; return x+y;")); + assertEquals((byte) 1 + (short) 1, exec("byte x = 1; short y = 1; return x+y;")); // byte/int - assertEquals((byte)1 + 1, exec("byte x = 1; int y = 1; return x+y;")); + assertEquals((byte) 1 + 1, exec("byte x = 1; int y = 1; return x+y;")); // byte/long - assertEquals((byte)1 + 1L, exec("byte x = 1; long y = 1; return x+y;")); + assertEquals((byte) 1 + 1L, exec("byte x = 1; long y = 1; return x+y;")); // byte/float - assertEquals((byte)1 + 1F, exec("byte x = 1; float y = 1; return x+y;")); + assertEquals((byte) 1 + 1F, exec("byte x = 1; float y = 1; return x+y;")); // byte/double - assertEquals((byte)1 + 1.0, exec("byte x = 1; double y = 1; return x+y;")); + assertEquals((byte) 1 + 1.0, exec("byte x = 1; double y = 1; return x+y;")); // char/byte - assertEquals((char)1 + (byte)1, exec("char x = 1; byte y = 1; return x+y;")); + assertEquals((char) 1 + (byte) 1, exec("char x = 1; byte y = 1; return x+y;")); // char/char - assertEquals((char)1 + (char)1, exec("char x = 1; char y = 1; return x+y;")); + assertEquals((char) 1 + (char) 1, exec("char x = 1; char y = 1; return x+y;")); // char/short - assertEquals((char)1 + (short)1, exec("char x = 1; short y = 1; return x+y;")); + assertEquals((char) 1 + (short) 1, exec("char x = 1; short y = 1; return x+y;")); // char/int - assertEquals((char)1 + 1, exec("char x = 1; int y = 1; return x+y;")); + assertEquals((char) 1 + 1, exec("char x = 1; int y = 1; return x+y;")); // char/long - assertEquals((char)1 + 1L, exec("char x = 1; long y = 1; return x+y;")); + assertEquals((char) 1 + 1L, exec("char x = 1; long y = 1; return x+y;")); // char/float - assertEquals((char)1 + 1F, exec("char x = 1; float y = 1; return x+y;")); + assertEquals((char) 1 + 1F, exec("char x = 1; float y = 1; return x+y;")); // char/double - assertEquals((char)1 + 1.0, exec("char x = 1; double y = 1; return x+y;")); + assertEquals((char) 1 + 1.0, exec("char x = 1; double y = 1; return x+y;")); // short/byte - assertEquals((short)1 + (byte)1, exec("short x = 1; byte y = 1; return x+y;")); + assertEquals((short) 1 + (byte) 1, exec("short x = 1; byte y = 1; return x+y;")); // short/char - assertEquals((short)1 + (char)1, exec("short x = 1; char y = 1; return x+y;")); + assertEquals((short) 1 + (char) 1, exec("short x = 1; char y = 1; return x+y;")); // short/short - assertEquals((short)1 + (short)1, exec("short x = 1; short y = 1; return x+y;")); + assertEquals((short) 1 + (short) 1, exec("short x = 1; short y = 1; return x+y;")); // short/int - assertEquals((short)1 + 1, exec("short x = 1; int y = 1; return x+y;")); + assertEquals((short) 1 + 1, exec("short x = 1; int y = 1; return x+y;")); // short/long - assertEquals((short)1 + 1L, exec("short x = 1; long y = 1; return x+y;")); + assertEquals((short) 1 + 1L, exec("short x = 1; long y = 1; return x+y;")); // short/float - assertEquals((short)1 + 1F, exec("short x = 1; float y = 1; return x+y;")); + assertEquals((short) 1 + 1F, exec("short x = 1; float y = 1; return x+y;")); // short/double - assertEquals((short)1 + 1.0, exec("short x = 1; double y = 1; return x+y;")); + assertEquals((short) 1 + 1.0, exec("short x = 1; double y = 1; return x+y;")); // int/byte - assertEquals(1 + (byte)1, exec("int x = 1; byte y = 1; return x+y;")); + assertEquals(1 + (byte) 1, exec("int x = 1; byte y = 1; return x+y;")); // int/char - assertEquals(1 + (char)1, exec("int x = 1; char y = 1; return x+y;")); + assertEquals(1 + (char) 1, exec("int x = 1; char y = 1; return x+y;")); // int/short - assertEquals(1 + (short)1, exec("int x = 1; short y = 1; return x+y;")); + assertEquals(1 + (short) 1, exec("int x = 1; short y = 1; return x+y;")); // int/int assertEquals(1 + 1, exec("int x = 1; int y = 1; return x+y;")); // int/long @@ -71,11 +71,11 @@ public void testBinaryPromotion() throws Exception { assertEquals(1 + 1.0, exec("int x = 1; double y = 1; return x+y;")); // long/byte - assertEquals(1L + (byte)1, exec("long x = 1; byte y = 1; return x+y;")); + assertEquals(1L + (byte) 1, exec("long x = 1; byte y = 1; return x+y;")); // long/char - assertEquals(1L + (char)1, exec("long x = 1; char y = 1; return x+y;")); + assertEquals(1L + (char) 1, exec("long x = 1; char y = 1; return x+y;")); // long/short - assertEquals(1L + (short)1, exec("long x = 1; short y = 1; return x+y;")); + assertEquals(1L + (short) 1, exec("long x = 1; short y = 1; return x+y;")); // long/int assertEquals(1L + 1, exec("long x = 1; int y = 1; return x+y;")); // long/long @@ -86,11 +86,11 @@ public void testBinaryPromotion() throws Exception { assertEquals(1L + 1.0, exec("long x = 1; double y = 1; return x+y;")); // float/byte - assertEquals(1F + (byte)1, exec("float x = 1; byte y = 1; return x+y;")); + assertEquals(1F + (byte) 1, exec("float x = 1; byte y = 1; return x+y;")); // float/char - assertEquals(1F + (char)1, exec("float x = 1; char y = 1; return x+y;")); + assertEquals(1F + (char) 1, exec("float x = 1; char y = 1; return x+y;")); // float/short - assertEquals(1F + (short)1, exec("float x = 1; short y = 1; return x+y;")); + assertEquals(1F + (short) 1, exec("float x = 1; short y = 1; return x+y;")); // float/int assertEquals(1F + 1, exec("float x = 1; int y = 1; return x+y;")); // float/long @@ -101,11 +101,11 @@ public void testBinaryPromotion() throws Exception { assertEquals(1F + 1.0, exec("float x = 1; double y = 1; return x+y;")); // double/byte - assertEquals(1.0 + (byte)1, exec("double x = 1; byte y = 1; return x+y;")); + assertEquals(1.0 + (byte) 1, exec("double x = 1; byte y = 1; return x+y;")); // double/char - assertEquals(1.0 + (char)1, exec("double x = 1; char y = 1; return x+y;")); + assertEquals(1.0 + (char) 1, exec("double x = 1; char y = 1; return x+y;")); // double/short - assertEquals(1.0 + (short)1, exec("double x = 1; short y = 1; return x+y;")); + assertEquals(1.0 + (short) 1, exec("double x = 1; short y = 1; return x+y;")); // double/int assertEquals(1.0 + 1, exec("double x = 1; int y = 1; return x+y;")); // double/long @@ -118,56 +118,56 @@ public void testBinaryPromotion() throws Exception { public void testBinaryPromotionConst() throws Exception { // byte/byte - assertEquals((byte)1 + (byte)1, exec("return (byte)1 + (byte)1;")); + assertEquals((byte) 1 + (byte) 1, exec("return (byte)1 + (byte)1;")); // byte/char - assertEquals((byte)1 + (char)1, exec("return (byte)1 + (char)1;")); + assertEquals((byte) 1 + (char) 1, exec("return (byte)1 + (char)1;")); // byte/short - assertEquals((byte)1 + (short)1, exec("return (byte)1 + (short)1;")); + assertEquals((byte) 1 + (short) 1, exec("return (byte)1 + (short)1;")); // byte/int - assertEquals((byte)1 + 1, exec("return (byte)1 + 1;")); + assertEquals((byte) 1 + 1, exec("return (byte)1 + 1;")); // byte/long - assertEquals((byte)1 + 1L, exec("return (byte)1 + 1L;")); + assertEquals((byte) 1 + 1L, exec("return (byte)1 + 1L;")); // byte/float - assertEquals((byte)1 + 1F, exec("return (byte)1 + 1F;")); + assertEquals((byte) 1 + 1F, exec("return (byte)1 + 1F;")); // byte/double - assertEquals((byte)1 + 1.0, exec("return (byte)1 + 1.0;")); + assertEquals((byte) 1 + 1.0, exec("return (byte)1 + 1.0;")); // char/byte - assertEquals((char)1 + (byte)1, exec("return (char)1 + (byte)1;")); + assertEquals((char) 1 + (byte) 1, exec("return (char)1 + (byte)1;")); // char/char - assertEquals((char)1 + (char)1, exec("return (char)1 + (char)1;")); + assertEquals((char) 1 + (char) 1, exec("return (char)1 + (char)1;")); // char/short - assertEquals((char)1 + (short)1, exec("return (char)1 + (short)1;")); + assertEquals((char) 1 + (short) 1, exec("return (char)1 + (short)1;")); // char/int - assertEquals((char)1 + 1, exec("return (char)1 + 1;")); + assertEquals((char) 1 + 1, exec("return (char)1 + 1;")); // char/long - assertEquals((char)1 + 1L, exec("return (char)1 + 1L;")); + assertEquals((char) 1 + 1L, exec("return (char)1 + 1L;")); // char/float - assertEquals((char)1 + 1F, exec("return (char)1 + 1F;")); + assertEquals((char) 1 + 1F, exec("return (char)1 + 1F;")); // char/double - assertEquals((char)1 + 1.0, exec("return (char)1 + 1.0;")); + assertEquals((char) 1 + 1.0, exec("return (char)1 + 1.0;")); // short/byte - assertEquals((short)1 + (byte)1, exec("return (short)1 + (byte)1;")); + assertEquals((short) 1 + (byte) 1, exec("return (short)1 + (byte)1;")); // short/char - assertEquals((short)1 + (char)1, exec("return (short)1 + (char)1;")); + assertEquals((short) 1 + (char) 1, exec("return (short)1 + (char)1;")); // short/short - assertEquals((short)1 + (short)1, exec("return (short)1 + (short)1;")); + assertEquals((short) 1 + (short) 1, exec("return (short)1 + (short)1;")); // short/int - assertEquals((short)1 + 1, exec("return (short)1 + 1;")); + assertEquals((short) 1 + 1, exec("return (short)1 + 1;")); // short/long - assertEquals((short)1 + 1L, exec("return (short)1 + 1L;")); + assertEquals((short) 1 + 1L, exec("return (short)1 + 1L;")); // short/float - assertEquals((short)1 + 1F, exec("return (short)1 + 1F;")); + assertEquals((short) 1 + 1F, exec("return (short)1 + 1F;")); // short/double - assertEquals((short)1 + 1.0, exec("return (short)1 + 1.0;")); + assertEquals((short) 1 + 1.0, exec("return (short)1 + 1.0;")); // int/byte - assertEquals(1 + (byte)1, exec("return 1 + (byte)1;")); + assertEquals(1 + (byte) 1, exec("return 1 + (byte)1;")); // int/char - assertEquals(1 + (char)1, exec("return 1 + (char)1;")); + assertEquals(1 + (char) 1, exec("return 1 + (char)1;")); // int/short - assertEquals(1 + (short)1, exec("return 1 + (short)1;")); + assertEquals(1 + (short) 1, exec("return 1 + (short)1;")); // int/int assertEquals(1 + 1, exec("return 1 + 1;")); // int/long @@ -178,11 +178,11 @@ public void testBinaryPromotionConst() throws Exception { assertEquals(1 + 1.0, exec("return 1 + 1.0;")); // long/byte - assertEquals(1L + (byte)1, exec("return 1L + (byte)1;")); + assertEquals(1L + (byte) 1, exec("return 1L + (byte)1;")); // long/char - assertEquals(1L + (char)1, exec("return 1L + (char)1;")); + assertEquals(1L + (char) 1, exec("return 1L + (char)1;")); // long/short - assertEquals(1L + (short)1, exec("return 1L + (short)1;")); + assertEquals(1L + (short) 1, exec("return 1L + (short)1;")); // long/int assertEquals(1L + 1, exec("return 1L + 1;")); // long/long @@ -193,11 +193,11 @@ public void testBinaryPromotionConst() throws Exception { assertEquals(1L + 1.0, exec("return 1L + 1.0;")); // float/byte - assertEquals(1F + (byte)1, exec("return 1F + (byte)1;")); + assertEquals(1F + (byte) 1, exec("return 1F + (byte)1;")); // float/char - assertEquals(1F + (char)1, exec("return 1F + (char)1;")); + assertEquals(1F + (char) 1, exec("return 1F + (char)1;")); // float/short - assertEquals(1F + (short)1, exec("return 1F + (short)1;")); + assertEquals(1F + (short) 1, exec("return 1F + (short)1;")); // float/int assertEquals(1F + 1, exec("return 1F + 1;")); // float/long @@ -208,11 +208,11 @@ public void testBinaryPromotionConst() throws Exception { assertEquals(1F + 1.0, exec("return 1F + 1.0;")); // double/byte - assertEquals(1.0 + (byte)1, exec("return 1.0 + (byte)1;")); + assertEquals(1.0 + (byte) 1, exec("return 1.0 + (byte)1;")); // double/char - assertEquals(1.0 + (char)1, exec("return 1.0 + (char)1;")); + assertEquals(1.0 + (char) 1, exec("return 1.0 + (char)1;")); // double/short - assertEquals(1.0 + (short)1, exec("return 1.0 + (short)1;")); + assertEquals(1.0 + (short) 1, exec("return 1.0 + (short)1;")); // double/int assertEquals(1.0 + 1, exec("return 1.0 + 1;")); // double/long diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/RegexLimitTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/RegexLimitTests.java index 537b1671575bd..00494e3436277 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/RegexLimitTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/RegexLimitTests.java @@ -19,8 +19,9 @@ public class RegexLimitTests extends ScriptTestCase { private final String regexCircuitMessage = "[scripting] Regular expression considered too many characters"; public void testRegexInject_Matcher() { - String[] scripts = new String[]{pattern + ".matcher(" + charSequence + ").matches()", - "Matcher m = " + pattern + ".matcher(" + charSequence + "); m.matches()"}; + String[] scripts = new String[] { + pattern + ".matcher(" + charSequence + ").matches()", + "Matcher m = " + pattern + ".matcher(" + charSequence + "); m.matches()" }; for (String script : scripts) { setRegexLimitFactor(2); assertEquals(Boolean.TRUE, exec(script)); @@ -33,8 +34,9 @@ public void testRegexInject_Matcher() { } public void testRegexInjectUnlimited_Matcher() { - String[] scripts = new String[]{pattern + ".matcher(" + charSequence + ").matches()", - "Matcher m = " + pattern + ".matcher(" + charSequence + "); m.matches()"}; + String[] scripts = new String[] { + pattern + ".matcher(" + charSequence + ").matches()", + "Matcher m = " + pattern + ".matcher(" + charSequence + "); m.matches()" }; for (String script : scripts) { setRegexEnabled(); assertEquals(Boolean.TRUE, exec(script)); @@ -42,8 +44,9 @@ public void testRegexInjectUnlimited_Matcher() { } public void testRegexInject_Def_Matcher() { - String[] scripts = new String[]{"def p = " + pattern + "; p.matcher(" + charSequence + ").matches()", - "def p = " + pattern + "; def m = p.matcher(" + charSequence + "); m.matches()"}; + String[] scripts = new String[] { + "def p = " + pattern + "; p.matcher(" + charSequence + ").matches()", + "def p = " + pattern + "; def m = p.matcher(" + charSequence + "); m.matches()" }; for (String script : scripts) { setRegexLimitFactor(2); assertEquals(Boolean.TRUE, exec(script)); @@ -55,10 +58,13 @@ public void testRegexInject_Def_Matcher() { } public void testMethodRegexInject_Ref_Matcher() { - String script = - "boolean isMatch(Function func) { func.apply(" + charSequence +").matches(); } " + - "Pattern pattern = " + pattern + ";" + - "isMatch(pattern::matcher)"; + String script = "boolean isMatch(Function func) { func.apply(" + + charSequence + + ").matches(); } " + + "Pattern pattern = " + + pattern + + ";" + + "isMatch(pattern::matcher)"; setRegexLimitFactor(2); assertEquals(Boolean.TRUE, exec(script)); @@ -68,10 +74,13 @@ public void testMethodRegexInject_Ref_Matcher() { } public void testRegexInject_DefMethodRef_Matcher() { - String script = - "boolean isMatch(Function func) { func.apply(" + charSequence +").matches(); } " + - "def pattern = " + pattern + ";" + - "isMatch(pattern::matcher)"; + String script = "boolean isMatch(Function func) { func.apply(" + + charSequence + + ").matches(); } " + + "def pattern = " + + pattern + + ";" + + "isMatch(pattern::matcher)"; setRegexLimitFactor(2); assertEquals(Boolean.TRUE, exec(script)); @@ -81,11 +90,12 @@ public void testRegexInject_DefMethodRef_Matcher() { } public void testRegexInject_SplitLimit() { - String[] scripts = new String[]{pattern + ".split(" + splitCharSequence + ", 2)", - "Pattern p = " + pattern + "; p.split(" + splitCharSequence + ", 2)"}; + String[] scripts = new String[] { + pattern + ".split(" + splitCharSequence + ", 2)", + "Pattern p = " + pattern + "; p.split(" + splitCharSequence + ", 2)" }; for (String script : scripts) { setRegexLimitFactor(2); - assertArrayEquals(new String[]{"0-", "-X-abc-2-def-Y-abc-3-def-Z-abc"}, (String[])exec(script)); + assertArrayEquals(new String[] { "0-", "-X-abc-2-def-Y-abc-3-def-Z-abc" }, (String[]) exec(script)); setRegexLimitFactor(1); CircuitBreakingException cbe = expectScriptThrows(CircuitBreakingException.class, () -> exec(script)); @@ -94,18 +104,19 @@ public void testRegexInject_SplitLimit() { } public void testRegexInjectUnlimited_SplitLimit() { - String[] scripts = new String[]{pattern + ".split(" + splitCharSequence + ", 2)", - "Pattern p = " + pattern + "; p.split(" + splitCharSequence + ", 2)"}; + String[] scripts = new String[] { + pattern + ".split(" + splitCharSequence + ", 2)", + "Pattern p = " + pattern + "; p.split(" + splitCharSequence + ", 2)" }; for (String script : scripts) { setRegexEnabled(); - assertArrayEquals(new String[]{"0-", "-X-abc-2-def-Y-abc-3-def-Z-abc"}, (String[])exec(script)); + assertArrayEquals(new String[] { "0-", "-X-abc-2-def-Y-abc-3-def-Z-abc" }, (String[]) exec(script)); } } public void testRegexInject_Def_SplitLimit() { String script = "def p = " + pattern + "; p.split(" + splitCharSequence + ", 2)"; setRegexLimitFactor(2); - assertArrayEquals(new String[]{"0-", "-X-abc-2-def-Y-abc-3-def-Z-abc"}, (String[])exec(script)); + assertArrayEquals(new String[] { "0-", "-X-abc-2-def-Y-abc-3-def-Z-abc" }, (String[]) exec(script)); setRegexLimitFactor(1); CircuitBreakingException cbe = expectScriptThrows(CircuitBreakingException.class, () -> exec(script)); @@ -113,12 +124,15 @@ public void testRegexInject_Def_SplitLimit() { } public void testRegexInject_Ref_SplitLimit() { - String script = - "String[] splitLimit(BiFunction func) { func.apply(" + splitCharSequence + ", 2); } " + - "Pattern pattern = " + pattern + ";" + - "splitLimit(pattern::split)"; + String script = "String[] splitLimit(BiFunction func) { func.apply(" + + splitCharSequence + + ", 2); } " + + "Pattern pattern = " + + pattern + + ";" + + "splitLimit(pattern::split)"; setRegexLimitFactor(2); - assertArrayEquals(new String[]{"0-", "-X-abc-2-def-Y-abc-3-def-Z-abc"}, (String[])exec(script)); + assertArrayEquals(new String[] { "0-", "-X-abc-2-def-Y-abc-3-def-Z-abc" }, (String[]) exec(script)); setRegexLimitFactor(1); CircuitBreakingException cbe = expectScriptThrows(CircuitBreakingException.class, () -> exec(script)); @@ -126,12 +140,15 @@ public void testRegexInject_Ref_SplitLimit() { } public void testRegexInject_DefMethodRef_SplitLimit() { - String script = - "String[] splitLimit(BiFunction func) { func.apply(" + splitCharSequence + ", 2); } " + - "def pattern = " + pattern + ";" + - "splitLimit(pattern::split)"; + String script = "String[] splitLimit(BiFunction func) { func.apply(" + + splitCharSequence + + ", 2); } " + + "def pattern = " + + pattern + + ";" + + "splitLimit(pattern::split)"; setRegexLimitFactor(2); - assertArrayEquals(new String[]{"0-", "-X-abc-2-def-Y-abc-3-def-Z-abc"}, (String[])exec(script)); + assertArrayEquals(new String[] { "0-", "-X-abc-2-def-Y-abc-3-def-Z-abc" }, (String[]) exec(script)); setRegexLimitFactor(1); CircuitBreakingException cbe = expectScriptThrows(CircuitBreakingException.class, () -> exec(script)); @@ -139,11 +156,12 @@ public void testRegexInject_DefMethodRef_SplitLimit() { } public void testRegexInject_Split() { - String[] scripts = new String[]{pattern + ".split(" + splitCharSequence + ")", - "Pattern p = " + pattern + "; p.split(" + splitCharSequence + ")"}; + String[] scripts = new String[] { + pattern + ".split(" + splitCharSequence + ")", + "Pattern p = " + pattern + "; p.split(" + splitCharSequence + ")" }; for (String script : scripts) { setRegexLimitFactor(2); - assertArrayEquals(new String[]{"0-", "-X-", "-Y-", "-Z-abc"}, (String[])exec(script)); + assertArrayEquals(new String[] { "0-", "-X-", "-Y-", "-Z-abc" }, (String[]) exec(script)); setRegexLimitFactor(1); CircuitBreakingException cbe = expectScriptThrows(CircuitBreakingException.class, () -> exec(script)); @@ -152,18 +170,19 @@ public void testRegexInject_Split() { } public void testRegexInjectUnlimited_Split() { - String[] scripts = new String[]{pattern + ".split(" + splitCharSequence + ")", - "Pattern p = " + pattern + "; p.split(" + splitCharSequence + ")"}; + String[] scripts = new String[] { + pattern + ".split(" + splitCharSequence + ")", + "Pattern p = " + pattern + "; p.split(" + splitCharSequence + ")" }; for (String script : scripts) { setRegexEnabled(); - assertArrayEquals(new String[]{"0-", "-X-", "-Y-", "-Z-abc"}, (String[])exec(script)); + assertArrayEquals(new String[] { "0-", "-X-", "-Y-", "-Z-abc" }, (String[]) exec(script)); } } public void testRegexInject_Def_Split() { String script = "def p = " + pattern + "; p.split(" + splitCharSequence + ")"; setRegexLimitFactor(2); - assertArrayEquals(new String[]{"0-", "-X-", "-Y-", "-Z-abc"}, (String[])exec(script)); + assertArrayEquals(new String[] { "0-", "-X-", "-Y-", "-Z-abc" }, (String[]) exec(script)); setRegexLimitFactor(1); CircuitBreakingException cbe = expectScriptThrows(CircuitBreakingException.class, () -> exec(script)); @@ -171,12 +190,15 @@ public void testRegexInject_Def_Split() { } public void testRegexInject_Ref_Split() { - String script = - "String[] split(Function func) { func.apply(" + splitCharSequence + "); } " + - "Pattern pattern = " + pattern + ";" + - "split(pattern::split)"; + String script = "String[] split(Function func) { func.apply(" + + splitCharSequence + + "); } " + + "Pattern pattern = " + + pattern + + ";" + + "split(pattern::split)"; setRegexLimitFactor(2); - assertArrayEquals(new String[]{"0-", "-X-", "-Y-", "-Z-abc"}, (String[])exec(script)); + assertArrayEquals(new String[] { "0-", "-X-", "-Y-", "-Z-abc" }, (String[]) exec(script)); setRegexLimitFactor(1); CircuitBreakingException cbe = expectScriptThrows(CircuitBreakingException.class, () -> exec(script)); @@ -184,12 +206,15 @@ public void testRegexInject_Ref_Split() { } public void testRegexInject_DefMethodRef_Split() { - String script = - "String[] split(Function func) { func.apply(" + splitCharSequence +"); } " + - "def pattern = " + pattern + ";" + - "split(pattern::split)"; + String script = "String[] split(Function func) { func.apply(" + + splitCharSequence + + "); } " + + "def pattern = " + + pattern + + ";" + + "split(pattern::split)"; setRegexLimitFactor(2); - assertArrayEquals(new String[]{"0-", "-X-", "-Y-", "-Z-abc"}, (String[])exec(script)); + assertArrayEquals(new String[] { "0-", "-X-", "-Y-", "-Z-abc" }, (String[]) exec(script)); setRegexLimitFactor(1); CircuitBreakingException cbe = expectScriptThrows(CircuitBreakingException.class, () -> exec(script)); @@ -197,11 +222,12 @@ public void testRegexInject_DefMethodRef_Split() { } public void testRegexInject_SplitAsStream() { - String[] scripts = new String[]{pattern + ".splitAsStream(" + splitCharSequence + ").toArray(String[]::new)", - "Pattern p = " + pattern + "; p.splitAsStream(" + splitCharSequence + ").toArray(String[]::new)"}; + String[] scripts = new String[] { + pattern + ".splitAsStream(" + splitCharSequence + ").toArray(String[]::new)", + "Pattern p = " + pattern + "; p.splitAsStream(" + splitCharSequence + ").toArray(String[]::new)" }; for (String script : scripts) { setRegexLimitFactor(2); - assertArrayEquals(new String[]{"0-", "-X-", "-Y-", "-Z-abc"}, (String[]) exec(script)); + assertArrayEquals(new String[] { "0-", "-X-", "-Y-", "-Z-abc" }, (String[]) exec(script)); setRegexLimitFactor(1); CircuitBreakingException cbe = expectScriptThrows(CircuitBreakingException.class, () -> exec(script)); @@ -210,18 +236,19 @@ public void testRegexInject_SplitAsStream() { } public void testRegexInjectUnlimited_SplitAsStream() { - String[] scripts = new String[]{pattern + ".splitAsStream(" + splitCharSequence + ").toArray(String[]::new)", - "Pattern p = " + pattern + "; p.splitAsStream(" + splitCharSequence + ").toArray(String[]::new)"}; + String[] scripts = new String[] { + pattern + ".splitAsStream(" + splitCharSequence + ").toArray(String[]::new)", + "Pattern p = " + pattern + "; p.splitAsStream(" + splitCharSequence + ").toArray(String[]::new)" }; for (String script : scripts) { setRegexEnabled(); - assertArrayEquals(new String[]{"0-", "-X-", "-Y-", "-Z-abc"}, (String[]) exec(script)); + assertArrayEquals(new String[] { "0-", "-X-", "-Y-", "-Z-abc" }, (String[]) exec(script)); } } public void testRegexInject_Def_SplitAsStream() { String script = "def p = " + pattern + "; p.splitAsStream(" + splitCharSequence + ").toArray(String[]::new)"; setRegexLimitFactor(2); - assertArrayEquals(new String[]{"0-", "-X-", "-Y-", "-Z-abc"}, (String[]) exec(script)); + assertArrayEquals(new String[] { "0-", "-X-", "-Y-", "-Z-abc" }, (String[]) exec(script)); setRegexLimitFactor(1); CircuitBreakingException cbe = expectScriptThrows(CircuitBreakingException.class, () -> exec(script)); @@ -229,12 +256,15 @@ public void testRegexInject_Def_SplitAsStream() { } public void testRegexInject_Ref_SplitAsStream() { - String script = - "Stream splitStream(Function func) { func.apply(" + splitCharSequence +"); } " + - "Pattern pattern = " + pattern + ";" + - "splitStream(pattern::splitAsStream).toArray(String[]::new)"; + String script = "Stream splitStream(Function func) { func.apply(" + + splitCharSequence + + "); } " + + "Pattern pattern = " + + pattern + + ";" + + "splitStream(pattern::splitAsStream).toArray(String[]::new)"; setRegexLimitFactor(2); - assertArrayEquals(new String[]{"0-", "-X-", "-Y-", "-Z-abc"}, (String[]) exec(script)); + assertArrayEquals(new String[] { "0-", "-X-", "-Y-", "-Z-abc" }, (String[]) exec(script)); setRegexLimitFactor(1); CircuitBreakingException cbe = expectScriptThrows(CircuitBreakingException.class, () -> exec(script)); @@ -242,12 +272,15 @@ public void testRegexInject_Ref_SplitAsStream() { } public void testRegexInject_DefMethodRef_SplitAsStream() { - String script = - "Stream splitStream(Function func) { func.apply(" + splitCharSequence +"); } " + - "def pattern = " + pattern + ";" + - "splitStream(pattern::splitAsStream).toArray(String[]::new)"; + String script = "Stream splitStream(Function func) { func.apply(" + + splitCharSequence + + "); } " + + "def pattern = " + + pattern + + ";" + + "splitStream(pattern::splitAsStream).toArray(String[]::new)"; setRegexLimitFactor(2); - assertArrayEquals(new String[]{"0-", "-X-", "-Y-", "-Z-abc"}, (String[]) exec(script)); + assertArrayEquals(new String[] { "0-", "-X-", "-Y-", "-Z-abc" }, (String[]) exec(script)); setRegexLimitFactor(1); CircuitBreakingException cbe = expectScriptThrows(CircuitBreakingException.class, () -> exec(script)); diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/RegexTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/RegexTests.java index d3150562c8619..3e4e057b73365 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/RegexTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/RegexTests.java @@ -57,19 +57,19 @@ public void testPatternAfterUnaryNotBoolean() { assertEquals(true, exec("return !/foo/.matcher('bar').matches()")); } - public void testInTernaryCondition() { + public void testInTernaryCondition() { assertEquals(true, exec("return /foo/.matcher('foo').matches() ? true : false")); assertEquals(1, exec("def i = 0; i += /foo/.matcher('foo').matches() ? 1 : 0; return i")); assertEquals(true, exec("return 'foo' ==~ /foo/ ? true : false")); assertEquals(1, exec("def i = 0; i += 'foo' ==~ /foo/ ? 1 : 0; return i")); } - public void testInTernaryTrueArm() { + public void testInTernaryTrueArm() { assertEquals(true, exec("def i = true; return i ? /foo/.matcher('foo').matches() : false")); assertEquals(true, exec("def i = true; return i ? 'foo' ==~ /foo/ : false")); } - public void testInTernaryFalseArm() { + public void testInTernaryFalseArm() { assertEquals(true, exec("def i = false; return i ? false : 'foo' ==~ /foo/")); } @@ -129,11 +129,11 @@ public void testNamedGroup() { // Make sure some methods on Pattern are whitelisted public void testSplit() { - assertArrayEquals(new String[] {"cat", "dog"}, (String[]) exec("/,/.split('cat,dog')")); + assertArrayEquals(new String[] { "cat", "dog" }, (String[]) exec("/,/.split('cat,dog')")); } public void testSplitWithLimit() { - assertArrayEquals(new String[] {"cat", "dog,pig"}, (String[]) exec("/,/.split('cat,dog,pig', 2)")); + assertArrayEquals(new String[] { "cat", "dog,pig" }, (String[]) exec("/,/.split('cat,dog,pig', 2)")); } public void testSplitAsStream() { @@ -183,8 +183,10 @@ public void testReplaceAllMatchesString() { public void testReplaceAllMatchesCharSequence() { CharSequence charSequence = CharBuffer.wrap("the quick brown fox"); - assertEquals("thE qUIck brOwn fOx", - exec("params.a.replaceAll(/[aeiou]/, m -> m.group().toUpperCase(Locale.ROOT))", singletonMap("a", charSequence), true)); + assertEquals( + "thE qUIck brOwn fOx", + exec("params.a.replaceAll(/[aeiou]/, m -> m.group().toUpperCase(Locale.ROOT))", singletonMap("a", charSequence), true) + ); } public void testReplaceAllNoMatchString() { @@ -193,26 +195,33 @@ public void testReplaceAllNoMatchString() { public void testReplaceAllNoMatchCharSequence() { CharSequence charSequence = CharBuffer.wrap("i am cat"); - assertEquals("i am cat", - exec("params.a.replaceAll(/dolphin/, m -> m.group().toUpperCase(Locale.ROOT))", singletonMap("a", charSequence), true)); + assertEquals( + "i am cat", + exec("params.a.replaceAll(/dolphin/, m -> m.group().toUpperCase(Locale.ROOT))", singletonMap("a", charSequence), true) + ); } public void testReplaceAllQuoteReplacement() { - assertEquals("th/E q/U/Ick br/Own f/Ox", - exec("'the quick brown fox'.replaceAll(/[aeiou]/, m -> '/' + m.group().toUpperCase(Locale.ROOT))")); - assertEquals("th$E q$U$Ick br$Own f$Ox", - exec("'the quick brown fox'.replaceAll(/[aeiou]/, m -> '$' + m.group().toUpperCase(Locale.ROOT))")); + assertEquals( + "th/E q/U/Ick br/Own f/Ox", + exec("'the quick brown fox'.replaceAll(/[aeiou]/, m -> '/' + m.group().toUpperCase(Locale.ROOT))") + ); + assertEquals( + "th$E q$U$Ick br$Own f$Ox", + exec("'the quick brown fox'.replaceAll(/[aeiou]/, m -> '$' + m.group().toUpperCase(Locale.ROOT))") + ); } public void testReplaceFirstMatchesString() { - assertEquals("thE quick brown fox", - exec("'the quick brown fox'.replaceFirst(/[aeiou]/, m -> m.group().toUpperCase(Locale.ROOT))")); + assertEquals("thE quick brown fox", exec("'the quick brown fox'.replaceFirst(/[aeiou]/, m -> m.group().toUpperCase(Locale.ROOT))")); } public void testReplaceFirstMatchesCharSequence() { CharSequence charSequence = CharBuffer.wrap("the quick brown fox"); - assertEquals("thE quick brown fox", - exec("params.a.replaceFirst(/[aeiou]/, m -> m.group().toUpperCase(Locale.ROOT))", singletonMap("a", charSequence), true)); + assertEquals( + "thE quick brown fox", + exec("params.a.replaceFirst(/[aeiou]/, m -> m.group().toUpperCase(Locale.ROOT))", singletonMap("a", charSequence), true) + ); } public void testReplaceFirstNoMatchString() { @@ -221,45 +230,49 @@ public void testReplaceFirstNoMatchString() { public void testReplaceFirstNoMatchCharSequence() { CharSequence charSequence = CharBuffer.wrap("i am cat"); - assertEquals("i am cat", - exec("params.a.replaceFirst(/dolphin/, m -> m.group().toUpperCase(Locale.ROOT))", singletonMap("a", charSequence), true)); + assertEquals( + "i am cat", + exec("params.a.replaceFirst(/dolphin/, m -> m.group().toUpperCase(Locale.ROOT))", singletonMap("a", charSequence), true) + ); } public void testReplaceFirstQuoteReplacement() { - assertEquals("th/E quick brown fox", - exec("'the quick brown fox'.replaceFirst(/[aeiou]/, m -> '/' + m.group().toUpperCase(Locale.ROOT))")); - assertEquals("th$E quick brown fox", - exec("'the quick brown fox'.replaceFirst(/[aeiou]/, m -> '$' + m.group().toUpperCase(Locale.ROOT))")); + assertEquals( + "th/E quick brown fox", + exec("'the quick brown fox'.replaceFirst(/[aeiou]/, m -> '/' + m.group().toUpperCase(Locale.ROOT))") + ); + assertEquals( + "th$E quick brown fox", + exec("'the quick brown fox'.replaceFirst(/[aeiou]/, m -> '$' + m.group().toUpperCase(Locale.ROOT))") + ); } - public void testStoreInMap() { + public void testStoreInMap() { assertEquals(true, exec("Map m = [:]; m.a = /foo/; m.a.matcher('foo').matches()")); } - public void testStoreInMapDef() { + public void testStoreInMapDef() { assertEquals(true, exec("def m = [:]; m.a = /foo/; m.a.matcher('foo').matches()")); } - public void testStoreInList() { + public void testStoreInList() { assertEquals(true, exec("List l = [null]; l.0 = /foo/; l.0.matcher('foo').matches()")); } - public void testStoreInListDef() { + public void testStoreInListDef() { assertEquals(true, exec("def l = [null]; l.0 = /foo/; l.0.matcher('foo').matches()")); } - public void testStoreInArray() { + public void testStoreInArray() { assertEquals(true, exec("Pattern[] a = new Pattern[1]; a[0] = /foo/; a[0].matcher('foo').matches()")); } - public void testStoreInArrayDef() { + public void testStoreInArrayDef() { assertEquals(true, exec("def a = new Pattern[1]; a[0] = /foo/; a[0].matcher('foo').matches()")); } public void testCantUsePatternCompile() { - IllegalArgumentException e = expectScriptThrows(IllegalArgumentException.class, () -> { - exec("Pattern.compile('aa')"); - }); + IllegalArgumentException e = expectScriptThrows(IllegalArgumentException.class, () -> { exec("Pattern.compile('aa')"); }); assertTrue(e.getMessage().contains("[java.util.regex.Pattern, compile/1]")); } @@ -273,9 +286,7 @@ public void testBadRegexPattern() { ); // And make sure the location of the error points to the offset inside the pattern - assertScriptStack(e, - "/\\ujjjj/", - " ^---- HERE"); + assertScriptStack(e, "/\\ujjjj/", " ^---- HERE"); e = expectThrows(ScriptException.class, () -> { exec("/(?< >.+)/"); // Invalid capture name @@ -287,15 +298,11 @@ public void testBadRegexPattern() { ); // And make sure the location of the error points to the offset inside the pattern - assertScriptStack(e, - "/(?< >.+)/", - " ^---- HERE"); + assertScriptStack(e, "/(?< >.+)/", " ^---- HERE"); } public void testRegexAgainstNumber() { - ClassCastException e = expectScriptThrows(ClassCastException.class, () -> { - exec("12 ==~ /cat/"); - }); + ClassCastException e = expectScriptThrows(ClassCastException.class, () -> { exec("12 ==~ /cat/"); }); assertEquals("Cannot cast from [int] to [java.lang.String].", e.getMessage()); } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/RemainderTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/RemainderTests.java index c3528a3148c7c..080b99b6ddad4 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/RemainderTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/RemainderTests.java @@ -9,7 +9,7 @@ package org.elasticsearch.painless; /** Tests for division operator across all types */ -//TODO: NaN/Inf/overflow/... +// TODO: NaN/Inf/overflow/... public class RemainderTests extends ScriptTestCase { // TODO: byte,short,char @@ -20,111 +20,103 @@ public void testBasics() throws Exception { } public void testInt() throws Exception { - assertEquals(1%1, exec("int x = 1; int y = 1; return x%y;")); - assertEquals(2%3, exec("int x = 2; int y = 3; return x%y;")); - assertEquals(5%10, exec("int x = 5; int y = 10; return x%y;")); - assertEquals(10%1%2, exec("int x = 10; int y = 1; int z = 2; return x%y%z;")); - assertEquals((10%1)%2, exec("int x = 10; int y = 1; int z = 2; return (x%y)%z;")); - assertEquals(10%(4%3), exec("int x = 10; int y = 4; int z = 3; return x%(y%z);")); - assertEquals(10%1, exec("int x = 10; int y = 1; return x%y;")); - assertEquals(0%1, exec("int x = 0; int y = 1; return x%y;")); + assertEquals(1 % 1, exec("int x = 1; int y = 1; return x%y;")); + assertEquals(2 % 3, exec("int x = 2; int y = 3; return x%y;")); + assertEquals(5 % 10, exec("int x = 5; int y = 10; return x%y;")); + assertEquals(10 % 1 % 2, exec("int x = 10; int y = 1; int z = 2; return x%y%z;")); + assertEquals((10 % 1) % 2, exec("int x = 10; int y = 1; int z = 2; return (x%y)%z;")); + assertEquals(10 % (4 % 3), exec("int x = 10; int y = 4; int z = 3; return x%(y%z);")); + assertEquals(10 % 1, exec("int x = 10; int y = 1; return x%y;")); + assertEquals(0 % 1, exec("int x = 0; int y = 1; return x%y;")); } public void testIntConst() throws Exception { - assertEquals(1%1, exec("return 1%1;")); - assertEquals(2%3, exec("return 2%3;")); - assertEquals(5%10, exec("return 5%10;")); - assertEquals(10%1%2, exec("return 10%1%2;")); - assertEquals((10%1)%2, exec("return (10%1)%2;")); - assertEquals(10%(4%3), exec("return 10%(4%3);")); - assertEquals(10%1, exec("return 10%1;")); - assertEquals(0%1, exec("return 0%1;")); + assertEquals(1 % 1, exec("return 1%1;")); + assertEquals(2 % 3, exec("return 2%3;")); + assertEquals(5 % 10, exec("return 5%10;")); + assertEquals(10 % 1 % 2, exec("return 10%1%2;")); + assertEquals((10 % 1) % 2, exec("return (10%1)%2;")); + assertEquals(10 % (4 % 3), exec("return 10%(4%3);")); + assertEquals(10 % 1, exec("return 10%1;")); + assertEquals(0 % 1, exec("return 0%1;")); } public void testLong() throws Exception { - assertEquals(1L%1L, exec("long x = 1; long y = 1; return x%y;")); - assertEquals(2L%3L, exec("long x = 2; long y = 3; return x%y;")); - assertEquals(5L%10L, exec("long x = 5; long y = 10; return x%y;")); - assertEquals(10L%1L%2L, exec("long x = 10; long y = 1; long z = 2; return x%y%z;")); - assertEquals((10L%1L)%2L, exec("long x = 10; long y = 1; long z = 2; return (x%y)%z;")); - assertEquals(10L%(4L%3L), exec("long x = 10; long y = 4; long z = 3; return x%(y%z);")); - assertEquals(10L%1L, exec("long x = 10; long y = 1; return x%y;")); - assertEquals(0L%1L, exec("long x = 0; long y = 1; return x%y;")); + assertEquals(1L % 1L, exec("long x = 1; long y = 1; return x%y;")); + assertEquals(2L % 3L, exec("long x = 2; long y = 3; return x%y;")); + assertEquals(5L % 10L, exec("long x = 5; long y = 10; return x%y;")); + assertEquals(10L % 1L % 2L, exec("long x = 10; long y = 1; long z = 2; return x%y%z;")); + assertEquals((10L % 1L) % 2L, exec("long x = 10; long y = 1; long z = 2; return (x%y)%z;")); + assertEquals(10L % (4L % 3L), exec("long x = 10; long y = 4; long z = 3; return x%(y%z);")); + assertEquals(10L % 1L, exec("long x = 10; long y = 1; return x%y;")); + assertEquals(0L % 1L, exec("long x = 0; long y = 1; return x%y;")); } public void testLongConst() throws Exception { - assertEquals(1L%1L, exec("return 1L%1L;")); - assertEquals(2L%3L, exec("return 2L%3L;")); - assertEquals(5L%10L, exec("return 5L%10L;")); - assertEquals(10L%1L%2L, exec("return 10L%1L%2L;")); - assertEquals((10L%1L)%2L, exec("return (10L%1L)%2L;")); - assertEquals(10L%(4L%3L), exec("return 10L%(4L%3L);")); - assertEquals(10L%1L, exec("return 10L%1L;")); - assertEquals(0L%1L, exec("return 0L%1L;")); + assertEquals(1L % 1L, exec("return 1L%1L;")); + assertEquals(2L % 3L, exec("return 2L%3L;")); + assertEquals(5L % 10L, exec("return 5L%10L;")); + assertEquals(10L % 1L % 2L, exec("return 10L%1L%2L;")); + assertEquals((10L % 1L) % 2L, exec("return (10L%1L)%2L;")); + assertEquals(10L % (4L % 3L), exec("return 10L%(4L%3L);")); + assertEquals(10L % 1L, exec("return 10L%1L;")); + assertEquals(0L % 1L, exec("return 0L%1L;")); } public void testFloat() throws Exception { - assertEquals(1F%1F, exec("float x = 1; float y = 1; return x%y;")); - assertEquals(2F%3F, exec("float x = 2; float y = 3; return x%y;")); - assertEquals(5F%10F, exec("float x = 5; float y = 10; return x%y;")); - assertEquals(10F%1F%2F, exec("float x = 10; float y = 1; float z = 2; return x%y%z;")); - assertEquals((10F%1F)%2F, exec("float x = 10; float y = 1; float z = 2; return (x%y)%z;")); - assertEquals(10F%(4F%3F), exec("float x = 10; float y = 4; float z = 3; return x%(y%z);")); - assertEquals(10F%1F, exec("float x = 10; float y = 1; return x%y;")); - assertEquals(0F%1F, exec("float x = 0; float y = 1; return x%y;")); + assertEquals(1F % 1F, exec("float x = 1; float y = 1; return x%y;")); + assertEquals(2F % 3F, exec("float x = 2; float y = 3; return x%y;")); + assertEquals(5F % 10F, exec("float x = 5; float y = 10; return x%y;")); + assertEquals(10F % 1F % 2F, exec("float x = 10; float y = 1; float z = 2; return x%y%z;")); + assertEquals((10F % 1F) % 2F, exec("float x = 10; float y = 1; float z = 2; return (x%y)%z;")); + assertEquals(10F % (4F % 3F), exec("float x = 10; float y = 4; float z = 3; return x%(y%z);")); + assertEquals(10F % 1F, exec("float x = 10; float y = 1; return x%y;")); + assertEquals(0F % 1F, exec("float x = 0; float y = 1; return x%y;")); } public void testFloatConst() throws Exception { - assertEquals(1F%1F, exec("return 1F%1F;")); - assertEquals(2F%3F, exec("return 2F%3F;")); - assertEquals(5F%10F, exec("return 5F%10F;")); - assertEquals(10F%1F%2F, exec("return 10F%1F%2F;")); - assertEquals((10F%1F)%2F, exec("return (10F%1F)%2F;")); - assertEquals(10F%(4F%3F), exec("return 10F%(4F%3F);")); - assertEquals(10F%1F, exec("return 10F%1F;")); - assertEquals(0F%1F, exec("return 0F%1F;")); + assertEquals(1F % 1F, exec("return 1F%1F;")); + assertEquals(2F % 3F, exec("return 2F%3F;")); + assertEquals(5F % 10F, exec("return 5F%10F;")); + assertEquals(10F % 1F % 2F, exec("return 10F%1F%2F;")); + assertEquals((10F % 1F) % 2F, exec("return (10F%1F)%2F;")); + assertEquals(10F % (4F % 3F), exec("return 10F%(4F%3F);")); + assertEquals(10F % 1F, exec("return 10F%1F;")); + assertEquals(0F % 1F, exec("return 0F%1F;")); } public void testDouble() throws Exception { - assertEquals(1.0%1.0, exec("double x = 1; double y = 1; return x%y;")); - assertEquals(2.0%3.0, exec("double x = 2; double y = 3; return x%y;")); - assertEquals(5.0%10.0, exec("double x = 5; double y = 10; return x%y;")); - assertEquals(10.0%1.0%2.0, exec("double x = 10; double y = 1; double z = 2; return x%y%z;")); - assertEquals((10.0%1.0)%2.0, exec("double x = 10; double y = 1; double z = 2; return (x%y)%z;")); - assertEquals(10.0%(4.0%3.0), exec("double x = 10; double y = 4; double z = 3; return x%(y%z);")); - assertEquals(10.0%1.0, exec("double x = 10; double y = 1; return x%y;")); - assertEquals(0.0%1.0, exec("double x = 0; double y = 1; return x%y;")); + assertEquals(1.0 % 1.0, exec("double x = 1; double y = 1; return x%y;")); + assertEquals(2.0 % 3.0, exec("double x = 2; double y = 3; return x%y;")); + assertEquals(5.0 % 10.0, exec("double x = 5; double y = 10; return x%y;")); + assertEquals(10.0 % 1.0 % 2.0, exec("double x = 10; double y = 1; double z = 2; return x%y%z;")); + assertEquals((10.0 % 1.0) % 2.0, exec("double x = 10; double y = 1; double z = 2; return (x%y)%z;")); + assertEquals(10.0 % (4.0 % 3.0), exec("double x = 10; double y = 4; double z = 3; return x%(y%z);")); + assertEquals(10.0 % 1.0, exec("double x = 10; double y = 1; return x%y;")); + assertEquals(0.0 % 1.0, exec("double x = 0; double y = 1; return x%y;")); } public void testDoubleConst() throws Exception { - assertEquals(1.0%1.0, exec("return 1.0%1.0;")); - assertEquals(2.0%3.0, exec("return 2.0%3.0;")); - assertEquals(5.0%10.0, exec("return 5.0%10.0;")); - assertEquals(10.0%1.0%2.0, exec("return 10.0%1.0%2.0;")); - assertEquals((10.0%1.0)%2.0, exec("return (10.0%1.0)%2.0;")); - assertEquals(10.0%(4.0%3.0), exec("return 10.0%(4.0%3.0);")); - assertEquals(10.0%1.0, exec("return 10.0%1.0;")); - assertEquals(0.0%1.0, exec("return 0.0%1.0;")); + assertEquals(1.0 % 1.0, exec("return 1.0%1.0;")); + assertEquals(2.0 % 3.0, exec("return 2.0%3.0;")); + assertEquals(5.0 % 10.0, exec("return 5.0%10.0;")); + assertEquals(10.0 % 1.0 % 2.0, exec("return 10.0%1.0%2.0;")); + assertEquals((10.0 % 1.0) % 2.0, exec("return (10.0%1.0)%2.0;")); + assertEquals(10.0 % (4.0 % 3.0), exec("return 10.0%(4.0%3.0);")); + assertEquals(10.0 % 1.0, exec("return 10.0%1.0;")); + assertEquals(0.0 % 1.0, exec("return 0.0%1.0;")); } public void testDivideByZero() throws Exception { - expectScriptThrows(ArithmeticException.class, () -> { - exec("int x = 1; int y = 0; return x % y;"); - }); + expectScriptThrows(ArithmeticException.class, () -> { exec("int x = 1; int y = 0; return x % y;"); }); - expectScriptThrows(ArithmeticException.class, () -> { - exec("long x = 1L; long y = 0L; return x % y;"); - }); + expectScriptThrows(ArithmeticException.class, () -> { exec("long x = 1L; long y = 0L; return x % y;"); }); } public void testDivideByZeroConst() throws Exception { - expectScriptThrows(ArithmeticException.class, () -> { - exec("return 1%0;"); - }); + expectScriptThrows(ArithmeticException.class, () -> { exec("return 1%0;"); }); - expectScriptThrows(ArithmeticException.class, () -> { - exec("return 1L%0L;"); - }); + expectScriptThrows(ArithmeticException.class, () -> { exec("return 1L%0L;"); }); } public void testDef() { diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/ScriptEngineTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/ScriptEngineTests.java index ebf20a191f5fb..7e0379616d7bb 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/ScriptEngineTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/ScriptEngineTests.java @@ -16,7 +16,7 @@ public class ScriptEngineTests extends ScriptTestCase { public void testSimpleEquation() { final Object value = exec("return 1 + 2;"); - assertEquals(3, ((Number)value).intValue()); + assertEquals(3, ((Number) value).intValue()); } @SuppressWarnings("unchecked") // We know its Map because we put them there in the test @@ -31,7 +31,7 @@ public void testMapAccess() { vars.put("obj1", obj1); Object value = exec("return params['obj1'];", vars, true); - obj1 = (Map)value; + obj1 = (Map) value; assertEquals("value1", obj1.get("prop1")); assertEquals("value2", ((Map) obj1.get("obj2")).get("prop2")); @@ -53,9 +53,9 @@ public void testAccessListInScript() { assertEquals("1", exec("return params.l.0;", vars, true)); Object value = exec("return params.l.3;", vars, true); - obj1 = (Map)value; + obj1 = (Map) value; assertEquals("value1", obj1.get("prop1")); - assertEquals("value2", ((Map)obj1.get("obj2")).get("prop2")); + assertEquals("value2", ((Map) obj1.get("obj2")).get("prop2")); assertEquals("value1", exec("return params.l.3.prop1;", vars, true)); } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/ScriptTestCase.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/ScriptTestCase.java index 4b36706388751..0cab58ae95504 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/ScriptTestCase.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/ScriptTestCase.java @@ -9,6 +9,7 @@ package org.elasticsearch.painless; import junit.framework.AssertionFailedError; + import org.elasticsearch.common.settings.Settings; import org.elasticsearch.painless.antlr.Walker; import org.elasticsearch.painless.spi.Whitelist; @@ -70,13 +71,13 @@ public Object exec(String script, boolean picky) { /** Compiles and returns the result of {@code script} with access to {@code vars} */ public Object exec(String script, Map vars, boolean picky) { - Map compilerSettings = new HashMap<>(); + Map compilerSettings = new HashMap<>(); compilerSettings.put(CompilerSettings.INITIAL_CALL_SITE_DEPTH, random().nextBoolean() ? "0" : "10"); return exec(script, vars, compilerSettings, picky); } /** Compiles and returns the result of {@code script} with access to {@code vars} and compile-time parameters */ - public Object exec(String script, Map vars, Map compileParams, boolean picky) { + public Object exec(String script, Map vars, Map compileParams, boolean picky) { // test for ambiguity errors before running the actual script if picky is true if (picky) { CompilerSettings pickySettings = new CompilerSettings(); @@ -96,7 +97,7 @@ public Object exec(String script, Map vars, Map c */ public void assertBytecodeExists(String script, String bytecode) { final String asm = Debugger.toString(script); - assertTrue("bytecode not found, got: \n" + asm , asm.contains(bytecode)); + assertTrue("bytecode not found, got: \n" + asm, asm.contains(bytecode)); } /** @@ -105,7 +106,7 @@ public void assertBytecodeExists(String script, String bytecode) { */ public void assertBytecodeHasPattern(String script, String pattern) { final String asm = Debugger.toString(script); - assertTrue("bytecode not found, got: \n" + asm , asm.matches(pattern)); + assertTrue("bytecode not found, got: \n" + asm, asm.matches(pattern)); } /** Checks a specific exception class is thrown (boxed inside ScriptException) and returns it. */ @@ -114,8 +115,11 @@ public static T expectScriptThrows(Class expectedType, } /** Checks a specific exception class is thrown (boxed inside ScriptException) and returns it. */ - public static T expectScriptThrows(Class expectedType, boolean shouldHaveScriptStack, - ThrowingRunnable runnable) { + public static T expectScriptThrows( + Class expectedType, + boolean shouldHaveScriptStack, + ThrowingRunnable runnable + ) { try { runnable.run(); } catch (Throwable e) { @@ -143,8 +147,9 @@ public static T expectScriptThrows(Class expectedType, assertion.initCause(e); throw assertion; } - AssertionFailedError assertion = new AssertionFailedError("Unexpected exception type, expected " - + expectedType.getSimpleName()); + AssertionFailedError assertion = new AssertionFailedError( + "Unexpected exception type, expected " + expectedType.getSimpleName() + ); assertion.initCause(e); throw assertion; } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/ScriptedMetricAggContextsTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/ScriptedMetricAggContextsTests.java index 27f7884123147..5177da3f6dd04 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/ScriptedMetricAggContextsTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/ScriptedMetricAggContextsTests.java @@ -40,8 +40,12 @@ protected Map, List> scriptContexts() { } public void testInitBasic() { - ScriptedMetricAggContexts.InitScript.Factory factory = scriptEngine.compile("test", - "state.testField = params.initialVal", ScriptedMetricAggContexts.InitScript.CONTEXT, Collections.emptyMap()); + ScriptedMetricAggContexts.InitScript.Factory factory = scriptEngine.compile( + "test", + "state.testField = params.initialVal", + ScriptedMetricAggContexts.InitScript.CONTEXT, + Collections.emptyMap() + ); Map params = new HashMap<>(); Map state = new HashMap<>(); @@ -51,23 +55,31 @@ public void testInitBasic() { ScriptedMetricAggContexts.InitScript script = factory.newInstance(params, state); script.execute(); - assert(state.containsKey("testField")); + assert (state.containsKey("testField")); assertEquals(10, state.get("testField")); } public void testMapBasic() throws IOException { - ScriptedMetricAggContexts.MapScript.Factory factory = scriptEngine.compile("test", - "state.testField = 2*_score", ScriptedMetricAggContexts.MapScript.CONTEXT, Collections.emptyMap()); + ScriptedMetricAggContexts.MapScript.Factory factory = scriptEngine.compile( + "test", + "state.testField = 2*_score", + ScriptedMetricAggContexts.MapScript.CONTEXT, + Collections.emptyMap() + ); Map params = new HashMap<>(); Map state = new HashMap<>(); Scorable scorer = new Scorable() { @Override - public int docID() { return 0; } + public int docID() { + return 0; + } @Override - public float score() { return 0.5f; } + public float score() { + return 0.5f; + } }; ScriptedMetricAggContexts.MapScript.LeafFactory leafFactory = factory.newFactory(params, state, null); @@ -76,13 +88,17 @@ public void testMapBasic() throws IOException { script.setScorer(scorer); script.execute(); - assert(state.containsKey("testField")); + assert (state.containsKey("testField")); assertEquals(1.0, state.get("testField")); } public void testReturnSource() throws IOException { - ScriptedMetricAggContexts.MapScript.Factory factory = scriptEngine.compile("test", - "state._source = params._source", ScriptedMetricAggContexts.MapScript.CONTEXT, Collections.emptyMap()); + ScriptedMetricAggContexts.MapScript.Factory factory = scriptEngine.compile( + "test", + "state._source = params._source", + ScriptedMetricAggContexts.MapScript.CONTEXT, + Collections.emptyMap() + ); Map params = new HashMap<>(); Map state = new HashMap<>(); @@ -103,13 +119,17 @@ public void testReturnSource() throws IOException { script.execute(); assertTrue(state.containsKey("_source")); - assertTrue(state.get("_source") instanceof Map && ((Map)state.get("_source")).containsKey("test")); - assertEquals(1, ((Map)state.get("_source")).get("test")); + assertTrue(state.get("_source") instanceof Map && ((Map) state.get("_source")).containsKey("test")); + assertEquals(1, ((Map) state.get("_source")).get("test")); } public void testMapSourceAccess() throws IOException { - ScriptedMetricAggContexts.MapScript.Factory factory = scriptEngine.compile("test", - "state.testField = params._source.three", ScriptedMetricAggContexts.MapScript.CONTEXT, Collections.emptyMap()); + ScriptedMetricAggContexts.MapScript.Factory factory = scriptEngine.compile( + "test", + "state.testField = params._source.three", + ScriptedMetricAggContexts.MapScript.CONTEXT, + Collections.emptyMap() + ); Map params = new HashMap<>(); Map state = new HashMap<>(); @@ -134,9 +154,12 @@ public void testMapSourceAccess() throws IOException { } public void testCombineBasic() { - ScriptedMetricAggContexts.CombineScript.Factory factory = scriptEngine.compile("test", - "state.testField = params.initialVal; return state.testField + params.inc", ScriptedMetricAggContexts.CombineScript.CONTEXT, - Collections.emptyMap()); + ScriptedMetricAggContexts.CombineScript.Factory factory = scriptEngine.compile( + "test", + "state.testField = params.initialVal; return state.testField + params.inc", + ScriptedMetricAggContexts.CombineScript.CONTEXT, + Collections.emptyMap() + ); Map params = new HashMap<>(); Map state = new HashMap<>(); @@ -147,14 +170,18 @@ public void testCombineBasic() { ScriptedMetricAggContexts.CombineScript script = factory.newInstance(params, state); Object res = script.execute(); - assert(state.containsKey("testField")); + assert (state.containsKey("testField")); assertEquals(10, state.get("testField")); assertEquals(12, res); } public void testReduceBasic() { - ScriptedMetricAggContexts.ReduceScript.Factory factory = scriptEngine.compile("test", - "states[0].testField + states[1].testField", ScriptedMetricAggContexts.ReduceScript.CONTEXT, Collections.emptyMap()); + ScriptedMetricAggContexts.ReduceScript.Factory factory = scriptEngine.compile( + "test", + "states[0].testField + states[1].testField", + ScriptedMetricAggContexts.ReduceScript.CONTEXT, + Collections.emptyMap() + ); Map params = new HashMap<>(); List states = new ArrayList<>(); diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/ShiftTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/ShiftTests.java index 1b0ab61d00eb5..0567200412262 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/ShiftTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/ShiftTests.java @@ -39,33 +39,17 @@ public void testLongShiftsConst() { } public void testBogusShifts() { - expectScriptThrows(ClassCastException.class, ()-> { - exec("long x = 1L; float y = 2; return x << y;"); - }); - expectScriptThrows(ClassCastException.class, ()-> { - exec("int x = 1; double y = 2L; return x << y;"); - }); - expectScriptThrows(ClassCastException.class, ()-> { - exec("float x = 1F; int y = 2; return x << y;"); - }); - expectScriptThrows(ClassCastException.class, ()-> { - exec("double x = 1D; int y = 2L; return x << y;"); - }); + expectScriptThrows(ClassCastException.class, () -> { exec("long x = 1L; float y = 2; return x << y;"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("int x = 1; double y = 2L; return x << y;"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("float x = 1F; int y = 2; return x << y;"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("double x = 1D; int y = 2L; return x << y;"); }); } public void testBogusShiftsConst() { - expectScriptThrows(ClassCastException.class, ()-> { - exec("return 1L << 2F;"); - }); - expectScriptThrows(ClassCastException.class, ()-> { - exec("return 1L << 2.0;"); - }); - expectScriptThrows(ClassCastException.class, ()-> { - exec("return 1F << 2;"); - }); - expectScriptThrows(ClassCastException.class, ()-> { - exec("return 1D << 2L"); - }); + expectScriptThrows(ClassCastException.class, () -> { exec("return 1L << 2F;"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("return 1L << 2.0;"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("return 1F << 2;"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("return 1D << 2L"); }); } public void testLshDef() { @@ -411,126 +395,54 @@ public void testUshDefTypedRHS() { } public void testBogusDefShifts() { - expectScriptThrows(ClassCastException.class, ()-> { - exec("def x = 1L; def y = 2F; return x << y;"); - }); - expectScriptThrows(ClassCastException.class, ()-> { - exec("def x = 1; def y = 2D; return x << y;"); - }); - expectScriptThrows(ClassCastException.class, ()-> { - exec("def x = 1F; def y = 2; return x << y;"); - }); - expectScriptThrows(ClassCastException.class, ()-> { - exec("def x = 1D; def y = 2L; return x << y;"); - }); - - expectScriptThrows(ClassCastException.class, ()-> { - exec("def x = 1L; def y = 2F; return x >> y;"); - }); - expectScriptThrows(ClassCastException.class, ()-> { - exec("def x = 1; def y = 2D; return x >> y;"); - }); - expectScriptThrows(ClassCastException.class, ()-> { - exec("def x = 1F; def y = 2; return x >> y;"); - }); - expectScriptThrows(ClassCastException.class, ()-> { - exec("def x = 1D; def y = 2L; return x >> y;"); - }); - - expectScriptThrows(ClassCastException.class, ()-> { - exec("def x = 1L; def y = 2F; return x >>> y;"); - }); - expectScriptThrows(ClassCastException.class, ()-> { - exec("def x = 1; def y = 2D; return x >>> y;"); - }); - expectScriptThrows(ClassCastException.class, ()-> { - exec("def x = 1F; def y = 2; return x >>> y;"); - }); - expectScriptThrows(ClassCastException.class, ()-> { - exec("def x = 1D; def y = 2L; return x >>> y;"); - }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = 1L; def y = 2F; return x << y;"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = 1; def y = 2D; return x << y;"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = 1F; def y = 2; return x << y;"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = 1D; def y = 2L; return x << y;"); }); + + expectScriptThrows(ClassCastException.class, () -> { exec("def x = 1L; def y = 2F; return x >> y;"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = 1; def y = 2D; return x >> y;"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = 1F; def y = 2; return x >> y;"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = 1D; def y = 2L; return x >> y;"); }); + + expectScriptThrows(ClassCastException.class, () -> { exec("def x = 1L; def y = 2F; return x >>> y;"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = 1; def y = 2D; return x >>> y;"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = 1F; def y = 2; return x >>> y;"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = 1D; def y = 2L; return x >>> y;"); }); } public void testBogusDefShiftsTypedLHS() { - expectScriptThrows(ClassCastException.class, ()-> { - exec("long x = 1L; def y = 2F; return x << y;"); - }); - expectScriptThrows(ClassCastException.class, ()-> { - exec("int x = 1; def y = 2D; return x << y;"); - }); - expectScriptThrows(ClassCastException.class, ()-> { - exec("float x = 1F; def y = 2; return x << y;"); - }); - expectScriptThrows(ClassCastException.class, ()-> { - exec("double x = 1D; def y = 2L; return x << y;"); - }); - - expectScriptThrows(ClassCastException.class, ()-> { - exec("long x = 1L; def y = 2F; return x >> y;"); - }); - expectScriptThrows(ClassCastException.class, ()-> { - exec("int x = 1; def y = 2D; return x >> y;"); - }); - expectScriptThrows(ClassCastException.class, ()-> { - exec("float x = 1F; def y = 2; return x >> y;"); - }); - expectScriptThrows(ClassCastException.class, ()-> { - exec("double x = 1D; def y = 2L; return x >> y;"); - }); - - expectScriptThrows(ClassCastException.class, ()-> { - exec("long x = 1L; def y = 2F; return x >>> y;"); - }); - expectScriptThrows(ClassCastException.class, ()-> { - exec("int x = 1; def y = 2D; return x >>> y;"); - }); - expectScriptThrows(ClassCastException.class, ()-> { - exec("float x = 1F; def y = 2; return x >>> y;"); - }); - expectScriptThrows(ClassCastException.class, ()-> { - exec("double x = 1D; def y = 2L; return x >>> y;"); - }); + expectScriptThrows(ClassCastException.class, () -> { exec("long x = 1L; def y = 2F; return x << y;"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("int x = 1; def y = 2D; return x << y;"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("float x = 1F; def y = 2; return x << y;"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("double x = 1D; def y = 2L; return x << y;"); }); + + expectScriptThrows(ClassCastException.class, () -> { exec("long x = 1L; def y = 2F; return x >> y;"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("int x = 1; def y = 2D; return x >> y;"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("float x = 1F; def y = 2; return x >> y;"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("double x = 1D; def y = 2L; return x >> y;"); }); + + expectScriptThrows(ClassCastException.class, () -> { exec("long x = 1L; def y = 2F; return x >>> y;"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("int x = 1; def y = 2D; return x >>> y;"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("float x = 1F; def y = 2; return x >>> y;"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("double x = 1D; def y = 2L; return x >>> y;"); }); } public void testBogusDefShiftsTypedRHS() { - expectScriptThrows(ClassCastException.class, ()-> { - exec("def x = 1L; float y = 2F; return x << y;"); - }); - expectScriptThrows(ClassCastException.class, ()-> { - exec("def x = 1; double y = 2D; return x << y;"); - }); - expectScriptThrows(ClassCastException.class, ()-> { - exec("def x = 1F; int y = 2; return x << y;"); - }); - expectScriptThrows(ClassCastException.class, ()-> { - exec("def x = 1D; long y = 2L; return x << y;"); - }); - - expectScriptThrows(ClassCastException.class, ()-> { - exec("def x = 1L; float y = 2F; return x >> y;"); - }); - expectScriptThrows(ClassCastException.class, ()-> { - exec("def x = 1; double y = 2D; return x >> y;"); - }); - expectScriptThrows(ClassCastException.class, ()-> { - exec("def x = 1F; int y = 2; return x >> y;"); - }); - expectScriptThrows(ClassCastException.class, ()-> { - exec("def x = 1D; long y = 2L; return x >> y;"); - }); - - expectScriptThrows(ClassCastException.class, ()-> { - exec("def x = 1L; float y = 2F; return x >>> y;"); - }); - expectScriptThrows(ClassCastException.class, ()-> { - exec("def x = 1; double y = 2D; return x >>> y;"); - }); - expectScriptThrows(ClassCastException.class, ()-> { - exec("def x = 1F; int y = 2; return x >>> y;"); - }); - expectScriptThrows(ClassCastException.class, ()-> { - exec("def x = 1D; long y = 2L; return x >>> y;"); - }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = 1L; float y = 2F; return x << y;"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = 1; double y = 2D; return x << y;"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = 1F; int y = 2; return x << y;"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = 1D; long y = 2L; return x << y;"); }); + + expectScriptThrows(ClassCastException.class, () -> { exec("def x = 1L; float y = 2F; return x >> y;"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = 1; double y = 2D; return x >> y;"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = 1F; int y = 2; return x >> y;"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = 1D; long y = 2L; return x >> y;"); }); + + expectScriptThrows(ClassCastException.class, () -> { exec("def x = 1L; float y = 2F; return x >>> y;"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = 1; double y = 2D; return x >>> y;"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = 1F; int y = 2; return x >>> y;"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = 1D; long y = 2L; return x >>> y;"); }); } public void testLshCompoundAssignment() { @@ -594,47 +506,23 @@ public void testUshCompoundAssignment() { } public void testBogusCompoundAssignment() { - expectScriptThrows(ClassCastException.class, ()-> { - exec("long x = 1L; float y = 2; x <<= y;"); - }); - expectScriptThrows(ClassCastException.class, ()-> { - exec("int x = 1; double y = 2L; x <<= y;"); - }); - expectScriptThrows(ClassCastException.class, ()-> { - exec("float x = 1F; int y = 2; x <<= y;"); - }); - expectScriptThrows(ClassCastException.class, ()-> { - exec("double x = 1D; int y = 2L; x <<= y;"); - }); + expectScriptThrows(ClassCastException.class, () -> { exec("long x = 1L; float y = 2; x <<= y;"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("int x = 1; double y = 2L; x <<= y;"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("float x = 1F; int y = 2; x <<= y;"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("double x = 1D; int y = 2L; x <<= y;"); }); } public void testBogusCompoundAssignmentConst() { - expectScriptThrows(ClassCastException.class, ()-> { - exec("int x = 1L; x <<= 2F;"); - }); - expectScriptThrows(ClassCastException.class, ()-> { - exec("int x = 1L; x <<= 2.0;"); - }); - expectScriptThrows(ClassCastException.class, ()-> { - exec("float x = 1F; x <<= 2;"); - }); - expectScriptThrows(ClassCastException.class, ()-> { - exec("double x = 1D; x <<= 2L;"); - }); + expectScriptThrows(ClassCastException.class, () -> { exec("int x = 1L; x <<= 2F;"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("int x = 1L; x <<= 2.0;"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("float x = 1F; x <<= 2;"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("double x = 1D; x <<= 2L;"); }); } public void testBogusCompoundAssignmentDef() { - expectScriptThrows(ClassCastException.class, ()-> { - exec("def x = 1L; float y = 2; x <<= y;"); - }); - expectScriptThrows(ClassCastException.class, ()-> { - exec("def x = 1; double y = 2L; x <<= y;"); - }); - expectScriptThrows(ClassCastException.class, ()-> { - exec("float x = 1F; def y = 2; x <<= y;"); - }); - expectScriptThrows(ClassCastException.class, ()-> { - exec("double x = 1D; def y = 2L; x <<= y;"); - }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = 1L; float y = 2; x <<= y;"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = 1; double y = 2L; x <<= y;"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("float x = 1F; def y = 2; x <<= y;"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("double x = 1D; def y = 2L; x <<= y;"); }); } } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/SimilarityScriptTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/SimilarityScriptTests.java index c8b6fc9232d99..e0d9fae3ac00c 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/SimilarityScriptTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/SimilarityScriptTests.java @@ -49,7 +49,11 @@ protected Map, List> scriptContexts() { public void testBasics() throws IOException { SimilarityScript.Factory factory = scriptEngine.compile( - "foobar", "return query.boost * doc.freq / doc.length", SimilarityScript.CONTEXT, Collections.emptyMap()); + "foobar", + "return query.boost * doc.freq / doc.length", + SimilarityScript.CONTEXT, + Collections.emptyMap() + ); ScriptedSimilarity sim = new ScriptedSimilarity("foobar", null, "foobaz", factory::newInstance, true); Directory dir = new ByteBuffersDirectory(); IndexWriter w = new IndexWriter(dir, newIndexWriterConfig().setSimilarity(sim)); @@ -73,10 +77,12 @@ public void testBasics() throws IOException { w.close(); IndexSearcher searcher = new IndexSearcher(r); searcher.setSimilarity(sim); - Query query = new BoostQuery(new BooleanQuery.Builder() - .add(new TermQuery(new Term("f", "foo")), Occur.SHOULD) + Query query = new BoostQuery( + new BooleanQuery.Builder().add(new TermQuery(new Term("f", "foo")), Occur.SHOULD) .add(new TermQuery(new Term("match", "yes")), Occur.FILTER) - .build(), 3.2f); + .build(), + 3.2f + ); TopDocs topDocs = searcher.search(query, 1); assertEquals(1, topDocs.totalHits.value); assertEquals((float) (3.2 * 2 / 3), topDocs.scoreDocs[0].score, 0); @@ -86,9 +92,17 @@ public void testBasics() throws IOException { public void testWeightScript() throws IOException { SimilarityWeightScript.Factory weightFactory = scriptEngine.compile( - "foobar", "return query.boost", SimilarityWeightScript.CONTEXT, Collections.emptyMap()); + "foobar", + "return query.boost", + SimilarityWeightScript.CONTEXT, + Collections.emptyMap() + ); SimilarityScript.Factory factory = scriptEngine.compile( - "foobar", "return weight * doc.freq / doc.length", SimilarityScript.CONTEXT, Collections.emptyMap()); + "foobar", + "return weight * doc.freq / doc.length", + SimilarityScript.CONTEXT, + Collections.emptyMap() + ); ScriptedSimilarity sim = new ScriptedSimilarity("foobar", weightFactory::newInstance, "foobaz", factory::newInstance, true); Directory dir = new ByteBuffersDirectory(); IndexWriter w = new IndexWriter(dir, newIndexWriterConfig().setSimilarity(sim)); @@ -112,10 +126,12 @@ public void testWeightScript() throws IOException { w.close(); IndexSearcher searcher = new IndexSearcher(r); searcher.setSimilarity(sim); - Query query = new BoostQuery(new BooleanQuery.Builder() - .add(new TermQuery(new Term("f", "foo")), Occur.SHOULD) + Query query = new BoostQuery( + new BooleanQuery.Builder().add(new TermQuery(new Term("f", "foo")), Occur.SHOULD) .add(new TermQuery(new Term("match", "yes")), Occur.FILTER) - .build(), 3.2f); + .build(), + 3.2f + ); TopDocs topDocs = searcher.search(query, 1); assertEquals(1, topDocs.totalHits.value); assertEquals((float) (3.2 * 2 / 3), topDocs.scoreDocs[0].score, 0); diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/StringTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/StringTests.java index 821d586d20ac6..5bf140cb5d05a 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/StringTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/StringTests.java @@ -23,12 +23,12 @@ public void testAppend() { // boolean assertEquals("cat" + true, exec("String s = \"cat\"; return s + true;")); // byte - assertEquals("cat" + (byte)3, exec("String s = \"cat\"; return s + (byte)3;")); + assertEquals("cat" + (byte) 3, exec("String s = \"cat\"; return s + (byte)3;")); // short - assertEquals("cat" + (short)3, exec("String s = \"cat\"; return s + (short)3;")); + assertEquals("cat" + (short) 3, exec("String s = \"cat\"; return s + (short)3;")); // char assertEquals("cat" + 't', exec("String s = \"cat\"; return s + 't';")); - assertEquals("cat" + (char)40, exec("String s = \"cat\"; return s + (char)40;")); + assertEquals("cat" + (char) 40, exec("String s = \"cat\"; return s + (char)40;")); // int assertEquals("cat" + 2, exec("String s = \"cat\"; return s + 2;")); // long @@ -43,12 +43,12 @@ public void testAppend() { // boolean assertEquals("cat" + true, exec("String s = 'cat'; return s + true;")); // byte - assertEquals("cat" + (byte)3, exec("String s = 'cat'; return s + (byte)3;")); + assertEquals("cat" + (byte) 3, exec("String s = 'cat'; return s + (byte)3;")); // short - assertEquals("cat" + (short)3, exec("String s = 'cat'; return s + (short)3;")); + assertEquals("cat" + (short) 3, exec("String s = 'cat'; return s + (short)3;")); // char assertEquals("cat" + 't', exec("String s = 'cat'; return s + 't';")); - assertEquals("cat" + (char)40, exec("String s = 'cat'; return s + (char)40;")); + assertEquals("cat" + (char) 40, exec("String s = 'cat'; return s + (char)40;")); // int assertEquals("cat" + 2, exec("String s = 'cat'; return s + 2;")); // long @@ -80,8 +80,10 @@ private void doTestAppendMany(int count) { result.append(s); } final String s = script.toString(); - assertTrue("every string part should be separately pushed to stack.", - Debugger.toString(s).contains(String.format(Locale.ROOT, "LDC \"%03d\"", count/2))); + assertTrue( + "every string part should be separately pushed to stack.", + Debugger.toString(s).contains(String.format(Locale.ROOT, "LDC \"%03d\"", count / 2)) + ); assertEquals(result.toString(), exec(s)); } @@ -102,7 +104,7 @@ public void testStringAPI() { assertEquals("cdcde", exec("String t = \"abcde\"; return t.replace(\"ab\", \"cd\");")); assertEquals(false, exec("String s = \"xy\"; return s.startsWith(\"y\");")); assertEquals("e", exec("String t = \"abcde\"; return t.substring(4, 5);")); - assertEquals(97, ((char[])exec("String s = \"a\"; return s.toCharArray();"))[0]); + assertEquals(97, ((char[]) exec("String s = \"a\"; return s.toCharArray();"))[0]); assertEquals("a", exec("String s = \" a \"; return s.trim();")); assertEquals('x', exec("return \"x\".charAt(0);")); assertEquals(120, exec("return \"x\".codePointAt(0);")); @@ -115,7 +117,7 @@ public void testStringAPI() { assertEquals("cdcde", exec("return \"abcde\".replace(\"ab\", \"cd\");")); assertEquals(false, exec("return \"xy\".startsWith(\"y\");")); assertEquals("e", exec("return \"abcde\".substring(4, 5);")); - assertEquals(97, ((char[])exec("return \"a\".toCharArray();"))[0]); + assertEquals(97, ((char[]) exec("return \"a\".toCharArray();"))[0]); assertEquals("a", exec("return \" a \".trim();")); assertEquals("", exec("return new String();")); @@ -130,7 +132,7 @@ public void testStringAPI() { assertEquals("cdcde", exec("String t = 'abcde'; return t.replace('ab', 'cd');")); assertEquals(false, exec("String s = 'xy'; return s.startsWith('y');")); assertEquals("e", exec("String t = 'abcde'; return t.substring(4, 5);")); - assertEquals(97, ((char[])exec("String s = 'a'; return s.toCharArray();"))[0]); + assertEquals(97, ((char[]) exec("String s = 'a'; return s.toCharArray();"))[0]); assertEquals("a", exec("String s = ' a '; return s.trim();")); assertEquals('x', exec("return 'x'.charAt(0);")); assertEquals(120, exec("return 'x'.codePointAt(0);")); @@ -143,7 +145,7 @@ public void testStringAPI() { assertEquals("cdcde", exec("return 'abcde'.replace('ab', 'cd');")); assertEquals(false, exec("return 'xy'.startsWith('y');")); assertEquals("e", exec("return 'abcde'.substring(4, 5);")); - assertEquals(97, ((char[])exec("return 'a'.toCharArray();"))[0]); + assertEquals(97, ((char[]) exec("return 'a'.toCharArray();"))[0]); assertEquals("a", exec("return ' a '.trim();")); } @@ -156,54 +158,48 @@ public void testStringAndCharacter() { assertEquals('c', exec("String s = \"c\"; (char)s")); assertEquals('c', exec("String s = 'c'; (char)s")); - ClassCastException expected = expectScriptThrows(ClassCastException.class, false, () -> { - assertEquals("cc", exec("return (String)(char)\"cc\"")); - }); + ClassCastException expected = expectScriptThrows( + ClassCastException.class, + false, + () -> { assertEquals("cc", exec("return (String)(char)\"cc\"")); } + ); assertTrue(expected.getMessage().contains("cannot cast java.lang.String with length not equal to one to char")); - expected = expectScriptThrows(ClassCastException.class, false, () -> { - assertEquals("cc", exec("return (String)(char)'cc'")); - }); + expected = expectScriptThrows(ClassCastException.class, false, () -> { assertEquals("cc", exec("return (String)(char)'cc'")); }); assertTrue(expected.getMessage().contains("cannot cast java.lang.String with length not equal to one to char")); - expected = expectScriptThrows(ClassCastException.class, () -> { - assertEquals('c', exec("String s = \"cc\"; (char)s")); - }); + expected = expectScriptThrows(ClassCastException.class, () -> { assertEquals('c', exec("String s = \"cc\"; (char)s")); }); assertTrue(expected.getMessage().contains("cannot cast java.lang.String with length not equal to one to char")); - expected = expectScriptThrows(ClassCastException.class, () -> { - assertEquals('c', exec("String s = 'cc'; (char)s")); - }); + expected = expectScriptThrows(ClassCastException.class, () -> { assertEquals('c', exec("String s = 'cc'; (char)s")); }); assertTrue(expected.getMessage().contains("cannot cast java.lang.String with length not equal to one to char")); } public void testDefConcat() { - assertEquals("a" + (byte)2, exec("def x = 'a'; def y = (byte)2; return x + y")); - assertEquals("a" + (short)2, exec("def x = 'a'; def y = (short)2; return x + y")); - assertEquals("a" + (char)2, exec("def x = 'a'; def y = (char)2; return x + y")); + assertEquals("a" + (byte) 2, exec("def x = 'a'; def y = (byte)2; return x + y")); + assertEquals("a" + (short) 2, exec("def x = 'a'; def y = (short)2; return x + y")); + assertEquals("a" + (char) 2, exec("def x = 'a'; def y = (char)2; return x + y")); assertEquals("a" + 2, exec("def x = 'a'; def y = (int)2; return x + y")); assertEquals("a" + 2L, exec("def x = 'a'; def y = (long)2; return x + y")); assertEquals("a" + 2F, exec("def x = 'a'; def y = (float)2; return x + y")); assertEquals("a" + 2D, exec("def x = 'a'; def y = (double)2; return x + y")); assertEquals("ab", exec("def x = 'a'; def y = 'b'; return x + y")); - assertEquals((byte)2 + "a", exec("def x = 'a'; def y = (byte)2; return y + x")); - assertEquals((short)2 + "a", exec("def x = 'a'; def y = (short)2; return y + x")); - assertEquals((char)2 + "a", exec("def x = 'a'; def y = (char)2; return y + x")); + assertEquals((byte) 2 + "a", exec("def x = 'a'; def y = (byte)2; return y + x")); + assertEquals((short) 2 + "a", exec("def x = 'a'; def y = (short)2; return y + x")); + assertEquals((char) 2 + "a", exec("def x = 'a'; def y = (char)2; return y + x")); assertEquals(2 + "a", exec("def x = 'a'; def y = (int)2; return y + x")); assertEquals(2L + "a", exec("def x = 'a'; def y = (long)2; return y + x")); assertEquals(2F + "a", exec("def x = 'a'; def y = (float)2; return y + x")); assertEquals(2D + "a", exec("def x = 'a'; def y = (double)2; return y + x")); assertEquals("anull", exec("def x = 'a'; def y = null; return x + y")); assertEquals("nullb", exec("def x = null; def y = 'b'; return x + y")); - expectScriptThrows(NullPointerException.class, () -> { - exec("def x = null; def y = null; return x + y"); - }); + expectScriptThrows(NullPointerException.class, () -> { exec("def x = null; def y = null; return x + y"); }); } public void testDefCompoundAssignment() { - assertEquals("a" + (byte)2, exec("def x = 'a'; x += (byte)2; return x")); - assertEquals("a" + (short)2, exec("def x = 'a'; x += (short)2; return x")); - assertEquals("a" + (char)2, exec("def x = 'a'; x += (char)2; return x")); + assertEquals("a" + (byte) 2, exec("def x = 'a'; x += (byte)2; return x")); + assertEquals("a" + (short) 2, exec("def x = 'a'; x += (short)2; return x")); + assertEquals("a" + (char) 2, exec("def x = 'a'; x += (char)2; return x")); assertEquals("a" + 2, exec("def x = 'a'; x += (int)2; return x")); assertEquals("a" + 2L, exec("def x = 'a'; x += (long)2; return x")); assertEquals("a" + 2F, exec("def x = 'a'; x += (float)2; return x")); @@ -211,9 +207,7 @@ public void testDefCompoundAssignment() { assertEquals("ab", exec("def x = 'a'; def y = 'b'; x += y; return x")); assertEquals("anull", exec("def x = 'a'; x += null; return x")); assertEquals("nullb", exec("def x = null; x += 'b'; return x")); - expectScriptThrows(NullPointerException.class, () -> { - exec("def x = null; def y = null; x += y"); - }); + expectScriptThrows(NullPointerException.class, () -> { exec("def x = null; def y = null; x += y"); }); } public void testComplexCompoundAssignment() { @@ -244,15 +238,19 @@ public void testBase64Augmentations() { public void testJava9ConstantStringConcatBytecode() { assumeTrue("Needs Java 9 to test indified String concat", Constants.JRE_IS_MINIMUM_JAVA9); assertNotNull(WriterConstants.INDY_STRING_CONCAT_BOOTSTRAP_HANDLE); - assertBytecodeExists("String s = \"cat\"; return s + true + 'abc' + null;", - "INVOKEDYNAMIC concat(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"); + assertBytecodeExists( + "String s = \"cat\"; return s + true + 'abc' + null;", + "INVOKEDYNAMIC concat(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;" + ); } public void testJava9StringConcatBytecode() { assumeTrue("Needs Java 9 to test indified String concat", Constants.JRE_IS_MINIMUM_JAVA9); assertNotNull(WriterConstants.INDY_STRING_CONCAT_BOOTSTRAP_HANDLE); - assertBytecodeExists("String s = \"cat\"; boolean t = true; Object u = null; return s + t + 'abc' + u;", - "INVOKEDYNAMIC concat(Ljava/lang/String;ZLjava/lang/String;Ljava/lang/Object;)Ljava/lang/String;"); + assertBytecodeExists( + "String s = \"cat\"; boolean t = true; Object u = null; return s + t + 'abc' + u;", + "INVOKEDYNAMIC concat(Ljava/lang/String;ZLjava/lang/String;Ljava/lang/Object;)Ljava/lang/String;" + ); } public void testNullStringConcat() { diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/SubtractionTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/SubtractionTests.java index 000a70438bfe8..6682d18b0fa91 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/SubtractionTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/SubtractionTests.java @@ -9,7 +9,7 @@ package org.elasticsearch.painless; /** Tests for subtraction operator across all types */ -//TODO: NaN/Inf/overflow/... +// TODO: NaN/Inf/overflow/... public class SubtractionTests extends ScriptTestCase { public void testBasics() throws Exception { @@ -18,157 +18,157 @@ public void testBasics() throws Exception { } public void testInt() throws Exception { - assertEquals(1-1, exec("int x = 1; int y = 1; return x-y;")); - assertEquals(2-3, exec("int x = 2; int y = 3; return x-y;")); - assertEquals(5-10, exec("int x = 5; int y = 10; return x-y;")); - assertEquals(1-1-2, exec("int x = 1; int y = 1; int z = 2; return x-y-z;")); - assertEquals((1-1)-2, exec("int x = 1; int y = 1; int z = 2; return (x-y)-z;")); - assertEquals(1-(1-2), exec("int x = 1; int y = 1; int z = 2; return x-(y-z);")); - assertEquals(10-0, exec("int x = 10; int y = 0; return x-y;")); - assertEquals(0-0, exec("int x = 0; int y = 0; return x-x;")); + assertEquals(1 - 1, exec("int x = 1; int y = 1; return x-y;")); + assertEquals(2 - 3, exec("int x = 2; int y = 3; return x-y;")); + assertEquals(5 - 10, exec("int x = 5; int y = 10; return x-y;")); + assertEquals(1 - 1 - 2, exec("int x = 1; int y = 1; int z = 2; return x-y-z;")); + assertEquals((1 - 1) - 2, exec("int x = 1; int y = 1; int z = 2; return (x-y)-z;")); + assertEquals(1 - (1 - 2), exec("int x = 1; int y = 1; int z = 2; return x-(y-z);")); + assertEquals(10 - 0, exec("int x = 10; int y = 0; return x-y;")); + assertEquals(0 - 0, exec("int x = 0; int y = 0; return x-x;")); } public void testIntConst() throws Exception { - assertEquals(1-1, exec("return 1-1;")); - assertEquals(2-3, exec("return 2-3;")); - assertEquals(5-10, exec("return 5-10;")); - assertEquals(1-1-2, exec("return 1-1-2;")); - assertEquals((1-1)-2, exec("return (1-1)-2;")); - assertEquals(1-(1-2), exec("return 1-(1-2);")); - assertEquals(10-0, exec("return 10-0;")); - assertEquals(0-0, exec("return 0-0;")); + assertEquals(1 - 1, exec("return 1-1;")); + assertEquals(2 - 3, exec("return 2-3;")); + assertEquals(5 - 10, exec("return 5-10;")); + assertEquals(1 - 1 - 2, exec("return 1-1-2;")); + assertEquals((1 - 1) - 2, exec("return (1-1)-2;")); + assertEquals(1 - (1 - 2), exec("return 1-(1-2);")); + assertEquals(10 - 0, exec("return 10-0;")); + assertEquals(0 - 0, exec("return 0-0;")); } public void testByte() throws Exception { - assertEquals((byte)1-(byte)1, exec("byte x = 1; byte y = 1; return x-y;")); - assertEquals((byte)2-(byte)3, exec("byte x = 2; byte y = 3; return x-y;")); - assertEquals((byte)5-(byte)10, exec("byte x = 5; byte y = 10; return x-y;")); - assertEquals((byte)1-(byte)1-(byte)2, exec("byte x = 1; byte y = 1; byte z = 2; return x-y-z;")); - assertEquals(((byte)1-(byte)1)-(byte)2, exec("byte x = 1; byte y = 1; byte z = 2; return (x-y)-z;")); - assertEquals((byte)1-((byte)1-(byte)2), exec("byte x = 1; byte y = 1; byte z = 2; return x-(y-z);")); - assertEquals((byte)10-(byte)1, exec("byte x = 10; byte y = 1; return x-y;")); - assertEquals((byte)0-(byte)0, exec("byte x = 0; byte y = 0; return x-y;")); + assertEquals((byte) 1 - (byte) 1, exec("byte x = 1; byte y = 1; return x-y;")); + assertEquals((byte) 2 - (byte) 3, exec("byte x = 2; byte y = 3; return x-y;")); + assertEquals((byte) 5 - (byte) 10, exec("byte x = 5; byte y = 10; return x-y;")); + assertEquals((byte) 1 - (byte) 1 - (byte) 2, exec("byte x = 1; byte y = 1; byte z = 2; return x-y-z;")); + assertEquals(((byte) 1 - (byte) 1) - (byte) 2, exec("byte x = 1; byte y = 1; byte z = 2; return (x-y)-z;")); + assertEquals((byte) 1 - ((byte) 1 - (byte) 2), exec("byte x = 1; byte y = 1; byte z = 2; return x-(y-z);")); + assertEquals((byte) 10 - (byte) 1, exec("byte x = 10; byte y = 1; return x-y;")); + assertEquals((byte) 0 - (byte) 0, exec("byte x = 0; byte y = 0; return x-y;")); } public void testByteConst() throws Exception { - assertEquals((byte)1-(byte)1, exec("return (byte)1-(byte)1;")); - assertEquals((byte)2-(byte)3, exec("return (byte)2-(byte)3;")); - assertEquals((byte)5-(byte)10, exec("return (byte)5-(byte)10;")); - assertEquals((byte)1-(byte)1-(byte)2, exec("return (byte)1-(byte)1-(byte)2;")); - assertEquals(((byte)1-(byte)1)-(byte)2, exec("return ((byte)1-(byte)1)-(byte)2;")); - assertEquals((byte)1-((byte)1-(byte)2), exec("return (byte)1-((byte)1-(byte)2);")); - assertEquals((byte)10-(byte)1, exec("return (byte)10-(byte)1;")); - assertEquals((byte)0-(byte)0, exec("return (byte)0-(byte)0;")); + assertEquals((byte) 1 - (byte) 1, exec("return (byte)1-(byte)1;")); + assertEquals((byte) 2 - (byte) 3, exec("return (byte)2-(byte)3;")); + assertEquals((byte) 5 - (byte) 10, exec("return (byte)5-(byte)10;")); + assertEquals((byte) 1 - (byte) 1 - (byte) 2, exec("return (byte)1-(byte)1-(byte)2;")); + assertEquals(((byte) 1 - (byte) 1) - (byte) 2, exec("return ((byte)1-(byte)1)-(byte)2;")); + assertEquals((byte) 1 - ((byte) 1 - (byte) 2), exec("return (byte)1-((byte)1-(byte)2);")); + assertEquals((byte) 10 - (byte) 1, exec("return (byte)10-(byte)1;")); + assertEquals((byte) 0 - (byte) 0, exec("return (byte)0-(byte)0;")); } public void testChar() throws Exception { - assertEquals((char)1-(char)1, exec("char x = 1; char y = 1; return x-y;")); - assertEquals((char)2-(char)3, exec("char x = 2; char y = 3; return x-y;")); - assertEquals((char)5-(char)10, exec("char x = 5; char y = 10; return x-y;")); - assertEquals((char)1-(char)1-(char)2, exec("char x = 1; char y = 1; char z = 2; return x-y-z;")); - assertEquals(((char)1-(char)1)-(char)2, exec("char x = 1; char y = 1; char z = 2; return (x-y)-z;")); - assertEquals((char)1-((char)1-(char)2), exec("char x = 1; char y = 1; char z = 2; return x-(y-z);")); - assertEquals((char)10-(char)1, exec("char x = 10; char y = 1; return x-y;")); - assertEquals((char)0-(char)0, exec("char x = 0; char y = 0; return x-y;")); + assertEquals((char) 1 - (char) 1, exec("char x = 1; char y = 1; return x-y;")); + assertEquals((char) 2 - (char) 3, exec("char x = 2; char y = 3; return x-y;")); + assertEquals((char) 5 - (char) 10, exec("char x = 5; char y = 10; return x-y;")); + assertEquals((char) 1 - (char) 1 - (char) 2, exec("char x = 1; char y = 1; char z = 2; return x-y-z;")); + assertEquals(((char) 1 - (char) 1) - (char) 2, exec("char x = 1; char y = 1; char z = 2; return (x-y)-z;")); + assertEquals((char) 1 - ((char) 1 - (char) 2), exec("char x = 1; char y = 1; char z = 2; return x-(y-z);")); + assertEquals((char) 10 - (char) 1, exec("char x = 10; char y = 1; return x-y;")); + assertEquals((char) 0 - (char) 0, exec("char x = 0; char y = 0; return x-y;")); } public void testCharConst() throws Exception { - assertEquals((char)1-(char)1, exec("return (char)1-(char)1;")); - assertEquals((char)2-(char)3, exec("return (char)2-(char)3;")); - assertEquals((char)5-(char)10, exec("return (char)5-(char)10;")); - assertEquals((char)1-(char)1-(char)2, exec("return (char)1-(char)1-(char)2;")); - assertEquals(((char)1-(char)1)-(char)2, exec("return ((char)1-(char)1)-(char)2;")); - assertEquals((char)1-((char)1-(char)2), exec("return (char)1-((char)1-(char)2);")); - assertEquals((char)10-(char)1, exec("return (char)10-(char)1;")); - assertEquals((char)0-(char)0, exec("return (char)0-(char)0;")); + assertEquals((char) 1 - (char) 1, exec("return (char)1-(char)1;")); + assertEquals((char) 2 - (char) 3, exec("return (char)2-(char)3;")); + assertEquals((char) 5 - (char) 10, exec("return (char)5-(char)10;")); + assertEquals((char) 1 - (char) 1 - (char) 2, exec("return (char)1-(char)1-(char)2;")); + assertEquals(((char) 1 - (char) 1) - (char) 2, exec("return ((char)1-(char)1)-(char)2;")); + assertEquals((char) 1 - ((char) 1 - (char) 2), exec("return (char)1-((char)1-(char)2);")); + assertEquals((char) 10 - (char) 1, exec("return (char)10-(char)1;")); + assertEquals((char) 0 - (char) 0, exec("return (char)0-(char)0;")); } public void testShort() throws Exception { - assertEquals((short)1-(short)1, exec("short x = 1; short y = 1; return x-y;")); - assertEquals((short)2-(short)3, exec("short x = 2; short y = 3; return x-y;")); - assertEquals((short)5-(short)10, exec("short x = 5; short y = 10; return x-y;")); - assertEquals((short)1-(short)1-(short)2, exec("short x = 1; short y = 1; short z = 2; return x-y-z;")); - assertEquals(((short)1-(short)1)-(short)2, exec("short x = 1; short y = 1; short z = 2; return (x-y)-z;")); - assertEquals((short)1-((short)1-(short)2), exec("short x = 1; short y = 1; short z = 2; return x-(y-z);")); - assertEquals((short)10-(short)1, exec("short x = 10; short y = 1; return x-y;")); - assertEquals((short)0-(short)0, exec("short x = 0; short y = 0; return x-y;")); + assertEquals((short) 1 - (short) 1, exec("short x = 1; short y = 1; return x-y;")); + assertEquals((short) 2 - (short) 3, exec("short x = 2; short y = 3; return x-y;")); + assertEquals((short) 5 - (short) 10, exec("short x = 5; short y = 10; return x-y;")); + assertEquals((short) 1 - (short) 1 - (short) 2, exec("short x = 1; short y = 1; short z = 2; return x-y-z;")); + assertEquals(((short) 1 - (short) 1) - (short) 2, exec("short x = 1; short y = 1; short z = 2; return (x-y)-z;")); + assertEquals((short) 1 - ((short) 1 - (short) 2), exec("short x = 1; short y = 1; short z = 2; return x-(y-z);")); + assertEquals((short) 10 - (short) 1, exec("short x = 10; short y = 1; return x-y;")); + assertEquals((short) 0 - (short) 0, exec("short x = 0; short y = 0; return x-y;")); } public void testShortConst() throws Exception { - assertEquals((short)1-(short)1, exec("return (short)1-(short)1;")); - assertEquals((short)2-(short)3, exec("return (short)2-(short)3;")); - assertEquals((short)5-(short)10, exec("return (short)5-(short)10;")); - assertEquals((short)1-(short)1-(short)2, exec("return (short)1-(short)1-(short)2;")); - assertEquals(((short)1-(short)1)-(short)2, exec("return ((short)1-(short)1)-(short)2;")); - assertEquals((short)1-((short)1-(short)2), exec("return (short)1-((short)1-(short)2);")); - assertEquals((short)10-(short)1, exec("return (short)10-(short)1;")); - assertEquals((short)0-(short)0, exec("return (short)0-(short)0;")); + assertEquals((short) 1 - (short) 1, exec("return (short)1-(short)1;")); + assertEquals((short) 2 - (short) 3, exec("return (short)2-(short)3;")); + assertEquals((short) 5 - (short) 10, exec("return (short)5-(short)10;")); + assertEquals((short) 1 - (short) 1 - (short) 2, exec("return (short)1-(short)1-(short)2;")); + assertEquals(((short) 1 - (short) 1) - (short) 2, exec("return ((short)1-(short)1)-(short)2;")); + assertEquals((short) 1 - ((short) 1 - (short) 2), exec("return (short)1-((short)1-(short)2);")); + assertEquals((short) 10 - (short) 1, exec("return (short)10-(short)1;")); + assertEquals((short) 0 - (short) 0, exec("return (short)0-(short)0;")); } public void testLong() throws Exception { - assertEquals(1L-1L, exec("long x = 1; long y = 1; return x-y;")); - assertEquals(2L-3L, exec("long x = 2; long y = 3; return x-y;")); - assertEquals(5L-10L, exec("long x = 5; long y = 10; return x-y;")); - assertEquals(1L-1L-2L, exec("long x = 1; long y = 1; int z = 2; return x-y-z;")); - assertEquals((1L-1L)-2L, exec("long x = 1; long y = 1; int z = 2; return (x-y)-z;")); - assertEquals(1L-(1L-2L), exec("long x = 1; long y = 1; int z = 2; return x-(y-z);")); - assertEquals(10L-0L, exec("long x = 10; long y = 0; return x-y;")); - assertEquals(0L-0L, exec("long x = 0; long y = 0; return x-x;")); + assertEquals(1L - 1L, exec("long x = 1; long y = 1; return x-y;")); + assertEquals(2L - 3L, exec("long x = 2; long y = 3; return x-y;")); + assertEquals(5L - 10L, exec("long x = 5; long y = 10; return x-y;")); + assertEquals(1L - 1L - 2L, exec("long x = 1; long y = 1; int z = 2; return x-y-z;")); + assertEquals((1L - 1L) - 2L, exec("long x = 1; long y = 1; int z = 2; return (x-y)-z;")); + assertEquals(1L - (1L - 2L), exec("long x = 1; long y = 1; int z = 2; return x-(y-z);")); + assertEquals(10L - 0L, exec("long x = 10; long y = 0; return x-y;")); + assertEquals(0L - 0L, exec("long x = 0; long y = 0; return x-x;")); } public void testLongConst() throws Exception { - assertEquals(1L-1L, exec("return 1L-1L;")); - assertEquals(2L-3L, exec("return 2L-3L;")); - assertEquals(5L-10L, exec("return 5L-10L;")); - assertEquals(1L-1L-2L, exec("return 1L-1L-2L;")); - assertEquals((1L-1L)-2L, exec("return (1L-1L)-2L;")); - assertEquals(1L-(1L-2L), exec("return 1L-(1L-2L);")); - assertEquals(10L-0L, exec("return 10L-0L;")); - assertEquals(0L-0L, exec("return 0L-0L;")); + assertEquals(1L - 1L, exec("return 1L-1L;")); + assertEquals(2L - 3L, exec("return 2L-3L;")); + assertEquals(5L - 10L, exec("return 5L-10L;")); + assertEquals(1L - 1L - 2L, exec("return 1L-1L-2L;")); + assertEquals((1L - 1L) - 2L, exec("return (1L-1L)-2L;")); + assertEquals(1L - (1L - 2L), exec("return 1L-(1L-2L);")); + assertEquals(10L - 0L, exec("return 10L-0L;")); + assertEquals(0L - 0L, exec("return 0L-0L;")); } public void testFloat() throws Exception { - assertEquals(1F-1F, exec("float x = 1; float y = 1; return x-y;")); - assertEquals(2F-3F, exec("float x = 2; float y = 3; return x-y;")); - assertEquals(5F-10F, exec("float x = 5; float y = 10; return x-y;")); - assertEquals(1F-1F-2F, exec("float x = 1; float y = 1; float z = 2; return x-y-z;")); - assertEquals((1F-1F)-2F, exec("float x = 1; float y = 1; float z = 2; return (x-y)-z;")); - assertEquals(1F-(1F-2F), exec("float x = 1; float y = 1; float z = 2; return x-(y-z);")); - assertEquals(10F-0F, exec("float x = 10; float y = 0; return x-y;")); - assertEquals(0F-0F, exec("float x = 0; float y = 0; return x-x;")); + assertEquals(1F - 1F, exec("float x = 1; float y = 1; return x-y;")); + assertEquals(2F - 3F, exec("float x = 2; float y = 3; return x-y;")); + assertEquals(5F - 10F, exec("float x = 5; float y = 10; return x-y;")); + assertEquals(1F - 1F - 2F, exec("float x = 1; float y = 1; float z = 2; return x-y-z;")); + assertEquals((1F - 1F) - 2F, exec("float x = 1; float y = 1; float z = 2; return (x-y)-z;")); + assertEquals(1F - (1F - 2F), exec("float x = 1; float y = 1; float z = 2; return x-(y-z);")); + assertEquals(10F - 0F, exec("float x = 10; float y = 0; return x-y;")); + assertEquals(0F - 0F, exec("float x = 0; float y = 0; return x-x;")); } public void testFloatConst() throws Exception { - assertEquals(1F-1F, exec("return 1F-1F;")); - assertEquals(2F-3F, exec("return 2F-3F;")); - assertEquals(5F-10F, exec("return 5F-10F;")); - assertEquals(1F-1F-2F, exec("return 1F-1F-2F;")); - assertEquals((1F-1F)-2F, exec("return (1F-1F)-2F;")); - assertEquals(1F-(1F-2F), exec("return 1F-(1F-2F);")); - assertEquals(10F-0F, exec("return 10F-0F;")); - assertEquals(0F-0F, exec("return 0F-0F;")); + assertEquals(1F - 1F, exec("return 1F-1F;")); + assertEquals(2F - 3F, exec("return 2F-3F;")); + assertEquals(5F - 10F, exec("return 5F-10F;")); + assertEquals(1F - 1F - 2F, exec("return 1F-1F-2F;")); + assertEquals((1F - 1F) - 2F, exec("return (1F-1F)-2F;")); + assertEquals(1F - (1F - 2F), exec("return 1F-(1F-2F);")); + assertEquals(10F - 0F, exec("return 10F-0F;")); + assertEquals(0F - 0F, exec("return 0F-0F;")); } public void testDouble() throws Exception { - assertEquals(1D-1D, exec("double x = 1; double y = 1; return x-y;")); - assertEquals(2D-3D, exec("double x = 2; double y = 3; return x-y;")); - assertEquals(5D-10D, exec("double x = 5; double y = 10; return x-y;")); - assertEquals(1D-1D-2D, exec("double x = 1; double y = 1; double z = 2; return x-y-z;")); - assertEquals((1D-1D)-2D, exec("double x = 1; double y = 1; double z = 2; return (x-y)-z;")); - assertEquals(1D-(1D-2D), exec("double x = 1; double y = 1; double z = 2; return x-(y-z);")); - assertEquals(10D-0D, exec("double x = 10; float y = 0; return x-y;")); - assertEquals(0D-0D, exec("double x = 0; float y = 0; return x-x;")); + assertEquals(1D - 1D, exec("double x = 1; double y = 1; return x-y;")); + assertEquals(2D - 3D, exec("double x = 2; double y = 3; return x-y;")); + assertEquals(5D - 10D, exec("double x = 5; double y = 10; return x-y;")); + assertEquals(1D - 1D - 2D, exec("double x = 1; double y = 1; double z = 2; return x-y-z;")); + assertEquals((1D - 1D) - 2D, exec("double x = 1; double y = 1; double z = 2; return (x-y)-z;")); + assertEquals(1D - (1D - 2D), exec("double x = 1; double y = 1; double z = 2; return x-(y-z);")); + assertEquals(10D - 0D, exec("double x = 10; float y = 0; return x-y;")); + assertEquals(0D - 0D, exec("double x = 0; float y = 0; return x-x;")); } public void testyDoubleConst() throws Exception { - assertEquals(1.0-1.0, exec("return 1.0-1.0;")); - assertEquals(2.0-3.0, exec("return 2.0-3.0;")); - assertEquals(5.0-10.0, exec("return 5.0-10.0;")); - assertEquals(1.0-1.0-2.0, exec("return 1.0-1.0-2.0;")); - assertEquals((1.0-1.0)-2.0, exec("return (1.0-1.0)-2.0;")); - assertEquals(1.0-(1.0-2.0), exec("return 1.0-(1.0-2.0);")); - assertEquals(10.0-0.0, exec("return 10.0-0.0;")); - assertEquals(0.0-0.0, exec("return 0.0-0.0;")); + assertEquals(1.0 - 1.0, exec("return 1.0-1.0;")); + assertEquals(2.0 - 3.0, exec("return 2.0-3.0;")); + assertEquals(5.0 - 10.0, exec("return 5.0-10.0;")); + assertEquals(1.0 - 1.0 - 2.0, exec("return 1.0-1.0-2.0;")); + assertEquals((1.0 - 1.0) - 2.0, exec("return (1.0-1.0)-2.0;")); + assertEquals(1.0 - (1.0 - 2.0), exec("return 1.0-(1.0-2.0);")); + assertEquals(10.0 - 0.0, exec("return 10.0-0.0;")); + assertEquals(0.0 - 0.0, exec("return 0.0-0.0;")); } public void testDef() { diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/TestFieldScript.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/TestFieldScript.java index 9982bddf46e1d..b4fe2db59da85 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/TestFieldScript.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/TestFieldScript.java @@ -18,12 +18,15 @@ public abstract class TestFieldScript { @SuppressWarnings("unused") public static final String[] PARAMETERS = {}; + public interface Factory { TestFieldScript newInstance(); } - public static final ScriptContext CONTEXT = - new ScriptContext<>("painless_test_fieldscript", TestFieldScript.Factory.class); + public static final ScriptContext CONTEXT = new ScriptContext<>( + "painless_test_fieldscript", + TestFieldScript.Factory.class + ); public static class Emit { private final TestFieldScript script; @@ -44,6 +47,6 @@ public final void emit(long v) { } public long[] fetchValues() { - return values.stream().mapToLong(i->i).toArray(); + return values.stream().mapToLong(i -> i).toArray(); } } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/ThisTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/ThisTests.java index a92cb308e6af6..407b36caf1924 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/ThisTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/ThisTests.java @@ -70,8 +70,7 @@ public interface Factory { } public static final String[] PARAMETERS = {}; - public static final ScriptContext CONTEXT = - new ScriptContext<>("this_test", ThisScript.Factory.class); + public static final ScriptContext CONTEXT = new ScriptContext<>("this_test", ThisScript.Factory.class); } @Override @@ -96,11 +95,18 @@ public void testThisMethods() { List result = new ArrayList<>(); result.add("this"); result.add("base"); - assertEquals(result, exec("List result = []; " + - "thisString('this');" + - "setBaseString('base');" + - "result.add(thisString()); " + - "result.add(getBaseString());" + - "result;", "", "")); + assertEquals( + result, + exec( + "List result = []; " + + "thisString('this');" + + "setBaseString('base');" + + "result.add(thisString()); " + + "result.add(getBaseString());" + + "result;", + "", + "" + ) + ); } } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/ToXContentTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/ToXContentTests.java index 9df31d387b078..2ac349ba697cf 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/ToXContentTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/ToXContentTests.java @@ -9,12 +9,12 @@ package org.elasticsearch.painless; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.painless.phase.UserTreeVisitor; import org.elasticsearch.painless.symbol.ScriptScope; import org.elasticsearch.painless.toxcontent.UserTreeToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; import java.io.IOException; import java.util.Collections; @@ -23,9 +23,9 @@ public class ToXContentTests extends ScriptTestCase { public void testUserFunction() { - Map func = getFunction("def twofive(int i) { return 25 + i; } int j = 23; twofive(j)", "twofive"); - assertFalse((Boolean)func.get("isInternal")); - assertFalse((Boolean)func.get("isStatic")); + Map func = getFunction("def twofive(int i) { return 25 + i; } int j = 23; twofive(j)", "twofive"); + assertFalse((Boolean) func.get("isInternal")); + assertFalse((Boolean) func.get("isStatic")); assertEquals("SFunction", func.get("node")); assertEquals("def", func.get("returns")); assertEquals(List.of("int"), func.get("parameterTypes")); @@ -41,11 +41,11 @@ public void testBlock() { Map decl = getStatement(block, "SDeclBlock"); List decls = (List) decl.get("declarations"); assertEquals(1, decls.size()); - assertEquals("i", ((Map) decls.get(0)).get("symbol")); - assertEquals("int", ((Map) decls.get(0)).get("type")); + assertEquals("i", ((Map) decls.get(0)).get("symbol")); + assertEquals("int", ((Map) decls.get(0)).get("type")); Map ret = getStatement(block, "SReturn"); - Map symbol = (Map)((List) ret.get("value")).get(0); + Map symbol = (Map) ((List) ret.get("value")).get(0); assertEquals("ESymbol", symbol.get("node")); assertEquals("i", symbol.get("symbol")); } @@ -57,7 +57,7 @@ public void testFor() { Map ecomp = getNode(sfor, "condition", "EComp"); assertEquals("j", getNode(ecomp, "left", "ESymbol").get("symbol")); assertEquals("100", getNode(ecomp, "right", "ENumeric").get("numeric")); - assertEquals("less than", ((Map) ecomp.get("operation")).get("name")); + assertEquals("less than", ((Map) ecomp.get("operation")).get("name")); Map init = getNode(sfor, "initializer", "SDeclBlock"); Map decl = getNode(init, "declarations", "SDeclaration"); @@ -68,7 +68,7 @@ public void testFor() { Map after = getNode(sfor, "afterthought", "EAssignment"); assertEquals("j", getNode(after, "left", "ESymbol").get("symbol")); assertEquals("1", getNode(after, "right", "ENumeric").get("numeric")); - assertTrue((Boolean)after.get("postIfRead")); + assertTrue((Boolean) after.get("postIfRead")); } private Map getStatement(Map block, String node) { @@ -96,7 +96,7 @@ public void testFor() { private Map getFunction(XContentBuilder builder, String function) { Map map = XContentHelper.convertToMap(BytesReference.bytes(builder), false, builder.contentType()).v2(); - for (Object funcObj: ((List)map.get("functions"))) { + for (Object funcObj : ((List) map.get("functions"))) { if (funcObj instanceof Map) { if (function.equals(((Map) funcObj).get("name"))) { return (Map) funcObj; diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/TryCatchTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/TryCatchTests.java index f82a791d7b3b8..cc9e64da61004 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/TryCatchTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/TryCatchTests.java @@ -15,32 +15,44 @@ public class TryCatchTests extends ScriptTestCase { /** throws an exception */ public void testThrow() { - RuntimeException exception = expectScriptThrows(RuntimeException.class, () -> { - exec("throw new RuntimeException('test')"); - }); + RuntimeException exception = expectScriptThrows(RuntimeException.class, () -> { exec("throw new RuntimeException('test')"); }); assertEquals("test", exception.getMessage()); } /** catches the exact exception */ public void testCatch() { - assertEquals(1, exec("try { if (params.param == 'true') throw new RuntimeException('test'); } " + - "catch (RuntimeException e) { return 1; } return 2;", - Collections.singletonMap("param", "true"), true)); + assertEquals( + 1, + exec( + "try { if (params.param == 'true') throw new RuntimeException('test'); } " + + "catch (RuntimeException e) { return 1; } return 2;", + Collections.singletonMap("param", "true"), + true + ) + ); } /** catches superclass of the exception */ public void testCatchSuperclass() { - assertEquals(1, exec("try { if (params.param == 'true') throw new RuntimeException('test'); } " + - "catch (Exception e) { return 1; } return 2;", - Collections.singletonMap("param", "true"), true)); + assertEquals( + 1, + exec( + "try { if (params.param == 'true') throw new RuntimeException('test'); } " + "catch (Exception e) { return 1; } return 2;", + Collections.singletonMap("param", "true"), + true + ) + ); } /** tries to catch a different type of exception */ public void testNoCatch() { RuntimeException exception = expectScriptThrows(RuntimeException.class, () -> { - exec("try { if (params.param == 'true') throw new RuntimeException('test'); } " + - "catch (ArithmeticException e) { return 1; } return 2;", - Collections.singletonMap("param", "true"), true); + exec( + "try { if (params.param == 'true') throw new RuntimeException('test'); } " + + "catch (ArithmeticException e) { return 1; } return 2;", + Collections.singletonMap("param", "true"), + true + ); }); assertEquals("test", exception.getMessage()); } @@ -48,43 +60,62 @@ public void testNoCatch() { public void testNoCatchBlock() { assertEquals(0, exec("try { return Integer.parseInt('f') } catch (NumberFormatException nfe) {} return 0;")); - assertEquals(0, exec("try { return Integer.parseInt('f') } " + - "catch (NumberFormatException nfe) {}" + - "catch (Exception e) {}" + - " return 0;")); + assertEquals( + 0, + exec("try { return Integer.parseInt('f') } " + "catch (NumberFormatException nfe) {}" + "catch (Exception e) {}" + " return 0;") + ); - assertEquals(0, exec("try { throw new IllegalArgumentException('test') } " + - "catch (NumberFormatException nfe) {}" + - "catch (Exception e) {}" + - " return 0;")); + assertEquals( + 0, + exec( + "try { throw new IllegalArgumentException('test') } " + + "catch (NumberFormatException nfe) {}" + + "catch (Exception e) {}" + + " return 0;" + ) + ); - assertEquals(0, exec("try { throw new IllegalArgumentException('test') } " + - "catch (NumberFormatException nfe) {}" + - "catch (IllegalArgumentException iae) {}" + - "catch (Exception e) {}" + - " return 0;")); + assertEquals( + 0, + exec( + "try { throw new IllegalArgumentException('test') } " + + "catch (NumberFormatException nfe) {}" + + "catch (IllegalArgumentException iae) {}" + + "catch (Exception e) {}" + + " return 0;" + ) + ); } public void testMultiCatch() { - assertEquals(1, exec( - "try { return Integer.parseInt('f') } " + - "catch (NumberFormatException nfe) {return 1;} " + - "catch (ArrayIndexOutOfBoundsException aioobe) {return 2;} " + - "catch (Exception e) {return 3;}" - )); + assertEquals( + 1, + exec( + "try { return Integer.parseInt('f') } " + + "catch (NumberFormatException nfe) {return 1;} " + + "catch (ArrayIndexOutOfBoundsException aioobe) {return 2;} " + + "catch (Exception e) {return 3;}" + ) + ); - assertEquals(2, exec( - "try { return new int[] {}[0] } " + - "catch (NumberFormatException nfe) {return 1;} " + - "catch (ArrayIndexOutOfBoundsException aioobe) {return 2;} " + - "catch (Exception e) {return 3;}" - )); + assertEquals( + 2, + exec( + "try { return new int[] {}[0] } " + + "catch (NumberFormatException nfe) {return 1;} " + + "catch (ArrayIndexOutOfBoundsException aioobe) {return 2;} " + + "catch (Exception e) {return 3;}" + ) + ); - assertEquals(3, exec( - "try { throw new IllegalArgumentException('test'); } " + - "catch (NumberFormatException nfe) {return 1;} " + - "catch (ArrayIndexOutOfBoundsException aioobe) {return 2;} " + - "catch (Exception e) {return 3;}" - )); + assertEquals( + 3, + exec( + "try { throw new IllegalArgumentException('test'); } " + + "catch (NumberFormatException nfe) {return 1;} " + + "catch (ArrayIndexOutOfBoundsException aioobe) {return 2;} " + + "catch (Exception e) {return 3;}" + ) + ); } } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/UnaryTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/UnaryTests.java index c9a69df13f512..1eb69fce87964 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/UnaryTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/UnaryTests.java @@ -51,9 +51,9 @@ public void testDefNot() { } public void testDefNotTypedRet() { - assertEquals((double)~1, exec("def x = (byte)1; double y = ~x; return y;")); - assertEquals((float)~1, exec("def x = (short)1; float y = ~x; return y;")); - assertEquals((long)~1, exec("def x = (char)1; long y = ~x; return y;")); + assertEquals((double) ~1, exec("def x = (byte)1; double y = ~x; return y;")); + assertEquals((float) ~1, exec("def x = (short)1; float y = ~x; return y;")); + assertEquals((long) ~1, exec("def x = (char)1; long y = ~x; return y;")); assertEquals(~1, exec("def x = 1; int y = ~x; return y;")); } @@ -68,9 +68,9 @@ public void testDefNeg() { } public void testDefNegTypedRet() { - assertEquals((double)-1, exec("def x = (byte)1; double y = -x; return y;")); - assertEquals((float)-1, exec("def x = (short)1; float y = -x; return y;")); - assertEquals((long)-1, exec("def x = (char)1; long y = -x; return y;")); + assertEquals((double) -1, exec("def x = (byte)1; double y = -x; return y;")); + assertEquals((float) -1, exec("def x = (short)1; float y = -x; return y;")); + assertEquals((long) -1, exec("def x = (char)1; long y = -x; return y;")); assertEquals(-1, exec("def x = 1; int y = -x; return y;")); } @@ -85,9 +85,9 @@ public void testDefPlus() { } public void testDefPlusTypedRet() { - assertEquals((double)-1, exec("def x = (byte)-1; double y = +x; return y;")); - assertEquals((float)-1, exec("def x = (short)-1; float y = +x; return y;")); - assertEquals((long)65535, exec("def x = (char)-1; long y = +x; return y;")); + assertEquals((double) -1, exec("def x = (byte)-1; double y = +x; return y;")); + assertEquals((float) -1, exec("def x = (short)-1; float y = +x; return y;")); + assertEquals((long) 65535, exec("def x = (char)-1; long y = +x; return y;")); assertEquals(-1, exec("def x = -1; int y = +x; return y;")); } } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/UserFunctionTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/UserFunctionTests.java index 175fa03614d98..3ba379ab92b61 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/UserFunctionTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/UserFunctionTests.java @@ -18,16 +18,15 @@ public void testZeroArgumentUserFunction() { } public void testUserFunctionDefCallRef() { - String source = - "String getSource() { 'source'; }\n" + - "int myCompare(int a, int b) { getMulti() * Integer.compare(a, b) }\n" + - "int getMulti() { return -1 }\n" + - "def l = [1, 100, -100];\n" + - "if (myCompare(10, 50) > 0) { l.add(50 + getMulti()) }\n" + - "l.sort(this::myCompare);\n" + - "if (l[0] == 100) { l.remove(l.size() - 1) ; l.sort((a, b) -> -1 * myCompare(a, b)) } \n"+ - "if (getSource().startsWith('sour')) { l.add(255); }\n" + - "return l;"; + String source = "String getSource() { 'source'; }\n" + + "int myCompare(int a, int b) { getMulti() * Integer.compare(a, b) }\n" + + "int getMulti() { return -1 }\n" + + "def l = [1, 100, -100];\n" + + "if (myCompare(10, 50) > 0) { l.add(50 + getMulti()) }\n" + + "l.sort(this::myCompare);\n" + + "if (l[0] == 100) { l.remove(l.size() - 1) ; l.sort((a, b) -> -1 * myCompare(a, b)) } \n" + + "if (getSource().startsWith('sour')) { l.add(255); }\n" + + "return l;"; assertEquals(List.of(1, 49, 100, 255), exec(source)); assertBytecodeExists(source, "public &getSource()Ljava/lang/String"); assertBytecodeExists(source, "public &getMulti()I"); @@ -37,117 +36,110 @@ public void testUserFunctionDefCallRef() { } public void testChainedUserMethods() { - String source = "int myCompare(int a, int b) { getMulti() * (a - b) }\n" + - "int getMulti() { -1 }\n" + - "List l = [1, 100, -100];\n" + - "l.sort(this::myCompare);\n" + - "l;\n"; + String source = "int myCompare(int a, int b) { getMulti() * (a - b) }\n" + + "int getMulti() { -1 }\n" + + "List l = [1, 100, -100];\n" + + "l.sort(this::myCompare);\n" + + "l;\n"; assertEquals(List.of(100, 1, -100), exec(source, Map.of("a", 1), false)); } - public void testChainedUserMethodsLambda() { - String source = "int myCompare(int a, int b) { getMulti() * (a - b) }\n" + - "int getMulti() { -1 }\n" + - "List l = [1, 100, -100];\n" + - "l.sort((a, b) -> myCompare(a, b));\n" + - "l;\n"; + String source = "int myCompare(int a, int b) { getMulti() * (a - b) }\n" + + "int getMulti() { -1 }\n" + + "List l = [1, 100, -100];\n" + + "l.sort((a, b) -> myCompare(a, b));\n" + + "l;\n"; assertEquals(List.of(100, 1, -100), exec(source, Map.of("a", 1), false)); } public void testChainedUserMethodsDef() { - String source = "int myCompare(int a, int b) { getMulti() * (a - b) }\n" + - "int getMulti() { -1 }\n" + - "def l = [1, 100, -100];\n" + - "l.sort(this::myCompare);\n" + - "l;\n"; + String source = "int myCompare(int a, int b) { getMulti() * (a - b) }\n" + + "int getMulti() { -1 }\n" + + "def l = [1, 100, -100];\n" + + "l.sort(this::myCompare);\n" + + "l;\n"; assertEquals(List.of(100, 1, -100), exec(source, Map.of("a", 1), false)); } - public void testChainedUserMethodsLambdaDef() { - String source = "int myCompare(int a, int b) { getMulti() * (a - b) }\n" + - "int getMulti() { -1 }\n" + - "def l = [1, 100, -100];\n" + - "l.sort((a, b) -> myCompare(a, b));\n" + - "l;\n"; + String source = "int myCompare(int a, int b) { getMulti() * (a - b) }\n" + + "int getMulti() { -1 }\n" + + "def l = [1, 100, -100];\n" + + "l.sort((a, b) -> myCompare(a, b));\n" + + "l;\n"; assertEquals(List.of(100, 1, -100), exec(source, Map.of("a", 1), false)); } public void testChainedUserMethodsLambdaCaptureDef() { - String source = "int myCompare(int a, int b, int x, int m) { getMulti(m) * (a - b + x) }\n" + - "int getMulti(int m) { -1 * m }\n" + - "def l = [1, 100, -100];\n" + - "int cx = 100;\n" + - "int cm = 1;\n" + - "l.sort((a, b) -> myCompare(a, b, cx, cm));\n" + - "l;\n"; + String source = "int myCompare(int a, int b, int x, int m) { getMulti(m) * (a - b + x) }\n" + + "int getMulti(int m) { -1 * m }\n" + + "def l = [1, 100, -100];\n" + + "int cx = 100;\n" + + "int cm = 1;\n" + + "l.sort((a, b) -> myCompare(a, b, cx, cm));\n" + + "l;\n"; assertEquals(List.of(100, 1, -100), exec(source, Map.of("a", 1), false)); } public void testMethodReferenceInUserFunction() { - String source = "int myCompare(int a, int b, String s) { " + - " Map m = ['f': 5];" + - " a - b + m.computeIfAbsent(s, this::getLength) " + - "}\n" + - "int getLength(String s) { s.length() }\n" + - "def l = [1, 0, -2];\n" + - "String s = 'g';\n" + - "l.sort((a, b) -> myCompare(a, b, s));\n" + - "l;\n"; + String source = "int myCompare(int a, int b, String s) { " + + " Map m = ['f': 5];" + + " a - b + m.computeIfAbsent(s, this::getLength) " + + "}\n" + + "int getLength(String s) { s.length() }\n" + + "def l = [1, 0, -2];\n" + + "String s = 'g';\n" + + "l.sort((a, b) -> myCompare(a, b, s));\n" + + "l;\n"; assertEquals(List.of(-2, 1, 0), exec(source, Map.of("a", 1), false)); } public void testUserFunctionVirtual() { - String source = "int myCompare(int x, int y) { return -1 * (x - y) }\n" + - "return myCompare(100, 90);"; + String source = "int myCompare(int x, int y) { return -1 * (x - y) }\n" + "return myCompare(100, 90);"; assertEquals(-10, exec(source, Map.of("a", 1), false)); assertBytecodeExists(source, "INVOKEVIRTUAL org/elasticsearch/painless/PainlessScript$Script.&myCompare (II)I"); } public void testUserFunctionRef() { - String source = "int myCompare(int x, int y) { return -1 * x - y }\n" + - "List l = [1, 100, -100];\n" + - "l.sort(this::myCompare);\n" + - "return l;"; + String source = "int myCompare(int x, int y) { return -1 * x - y }\n" + + "List l = [1, 100, -100];\n" + + "l.sort(this::myCompare);\n" + + "return l;"; assertEquals(List.of(100, 1, -100), exec(source, Map.of("a", 1), false)); assertBytecodeExists(source, "public &myCompare(II)I"); } public void testUserFunctionRefEmpty() { - String source = "int myCompare(int x, int y) { return -1 * x - y }\n" + - "[].sort((a, b) -> myCompare(a, b));\n"; + String source = "int myCompare(int x, int y) { return -1 * x - y }\n" + "[].sort((a, b) -> myCompare(a, b));\n"; assertNull(exec(source, Map.of("a", 1), false)); assertBytecodeExists(source, "public &myCompare(II)I"); assertBytecodeExists(source, "INVOKEVIRTUAL org/elasticsearch/painless/PainlessScript$Script.&myCompare (II)I"); } public void testUserFunctionCallInLambda() { - String source = "int myCompare(int x, int y) { -1 * ( x - y ) }\n" + - "List l = [1, 100, -100];\n" + - "l.sort((a, b) -> myCompare(a, b));\n" + - "return l;"; + String source = "int myCompare(int x, int y) { -1 * ( x - y ) }\n" + + "List l = [1, 100, -100];\n" + + "l.sort((a, b) -> myCompare(a, b));\n" + + "return l;"; assertEquals(List.of(100, 1, -100), exec(source, Map.of("a", 1), false)); assertBytecodeExists(source, "public &myCompare(II)I"); assertBytecodeExists(source, "INVOKEVIRTUAL org/elasticsearch/painless/PainlessScript$Script.&myCompare (II)I"); } public void testUserFunctionLambdaCapture() { - String source = "int myCompare(Object o, int x, int y) { return o != null ? -1 * ( x - y ) : ( x - y ) }\n" + - "List l = [1, 100, -100];\n" + - "Object q = '';\n" + - "l.sort((a, b) -> myCompare(q, a, b));\n" + - "return l;"; + String source = "int myCompare(Object o, int x, int y) { return o != null ? -1 * ( x - y ) : ( x - y ) }\n" + + "List l = [1, 100, -100];\n" + + "Object q = '';\n" + + "l.sort((a, b) -> myCompare(q, a, b));\n" + + "return l;"; assertEquals(List.of(100, 1, -100), exec(source, Map.of("a", 1), false)); assertBytecodeExists(source, "public &myCompare(Ljava/lang/Object;II)I"); assertBytecodeExists(source, "INVOKEVIRTUAL org/elasticsearch/painless/PainlessScript$Script.&myCompare (Ljava/lang/Object;II)I"); } public void testLambdaCapture() { - String source = "List l = [1, 100, -100];\n" + - "int q = -1;\n" + - "l.sort((a, b) -> q * ( a - b ));\n" + - "return l;"; + String source = "List l = [1, 100, -100];\n" + "int q = -1;\n" + "l.sort((a, b) -> q * ( a - b ));\n" + "return l;"; assertEquals(List.of(100, 1, -100), exec(source, Map.of("a", 1), false)); assertBytecodeExists(source, "public static synthetic lambda$synthetic$0(ILjava/lang/Object;Ljava/lang/Object;)I"); } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/WhenThingsGoWrongTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/WhenThingsGoWrongTests.java index d0fb1d59431f8..e68e2f6ba15cd 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/WhenThingsGoWrongTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/WhenThingsGoWrongTests.java @@ -9,6 +9,7 @@ package org.elasticsearch.painless; import junit.framework.AssertionFailedError; + import org.apache.lucene.util.Constants; import org.elasticsearch.script.ScriptException; @@ -22,26 +23,22 @@ public class WhenThingsGoWrongTests extends ScriptTestCase { public void testNullPointer() { - expectScriptThrows(NullPointerException.class, () -> { - exec("int x = params['missing']; return x;"); - }); - expectScriptThrows(NullPointerException.class, () -> { - exec("Double.parseDouble(params['missing'])"); - }); + expectScriptThrows(NullPointerException.class, () -> { exec("int x = params['missing']; return x;"); }); + expectScriptThrows(NullPointerException.class, () -> { exec("Double.parseDouble(params['missing'])"); }); } public void testDefNullPointer() { - NullPointerException npe = expectScriptThrows(NullPointerException.class, () -> { - exec("def x = null; x.intValue(); return null;"); - }); + NullPointerException npe = expectScriptThrows( + NullPointerException.class, + () -> { exec("def x = null; x.intValue(); return null;"); } + ); assertEquals(npe.getMessage(), "cannot access method/field [intValue] from a null def reference"); - npe = expectScriptThrows(NullPointerException.class, () -> { - exec("def x = [1, null]; for (y in x) y.intValue(); return null;"); - }); + npe = expectScriptThrows(NullPointerException.class, () -> { exec("def x = [1, null]; for (y in x) y.intValue(); return null;"); }); assertEquals(npe.getMessage(), "cannot access method/field [intValue] from a null def reference"); - npe = expectScriptThrows(NullPointerException.class, () -> { - exec("def x = [1, 2L, 3.0, 'test', (byte)1, (short)1, (char)1, null]; for (y in x) y.toString(); return null;"); - }); + npe = expectScriptThrows( + NullPointerException.class, + () -> { exec("def x = [1, 2L, 3.0, 'test', (byte)1, (short)1, (char)1, null]; for (y in x) y.toString(); return null;"); } + ); assertEquals(npe.getMessage(), "cannot access method/field [toString] from a null def reference"); } @@ -50,58 +47,44 @@ public void testDefNullPointer() { * numbers are really 1 based character numbers. */ public void testScriptStack() { - for (String type : new String[] {"String", "def "}) { + for (String type : new String[] { "String", "def " }) { // trigger NPE at line 1 of the script - ScriptException exception = expectThrows(ScriptException.class, () -> { - exec(type + " x = null; boolean y = x.isEmpty();\n" + - "return y;"); - }); + ScriptException exception = expectThrows( + ScriptException.class, + () -> { exec(type + " x = null; boolean y = x.isEmpty();\n" + "return y;"); } + ); // null deref at x.isEmpty(), the '.' is offset 30 assertScriptElementColumn(30, exception); - assertScriptStack(exception, - "y = x.isEmpty();\n", - " ^---- HERE"); + assertScriptStack(exception, "y = x.isEmpty();\n", " ^---- HERE"); assertThat(exception.getCause(), instanceOf(NullPointerException.class)); // trigger NPE at line 2 of the script - exception = expectThrows(ScriptException.class, () -> { - exec(type + " x = null;\n" + - "return x.isEmpty();"); - }); + exception = expectThrows(ScriptException.class, () -> { exec(type + " x = null;\n" + "return x.isEmpty();"); }); // null deref at x.isEmpty(), the '.' is offset 25 assertScriptElementColumn(25, exception); - assertScriptStack(exception, - "return x.isEmpty();", - " ^---- HERE"); + assertScriptStack(exception, "return x.isEmpty();", " ^---- HERE"); assertThat(exception.getCause(), instanceOf(NullPointerException.class)); // trigger NPE at line 3 of the script - exception = expectThrows(ScriptException.class, () -> { - exec(type + " x = null;\n" + - type + " y = x;\n" + - "return y.isEmpty();"); - }); + exception = expectThrows( + ScriptException.class, + () -> { exec(type + " x = null;\n" + type + " y = x;\n" + "return y.isEmpty();"); } + ); // null deref at y.isEmpty(), the '.' is offset 39 assertScriptElementColumn(39, exception); - assertScriptStack(exception, - "return y.isEmpty();", - " ^---- HERE"); + assertScriptStack(exception, "return y.isEmpty();", " ^---- HERE"); assertThat(exception.getCause(), instanceOf(NullPointerException.class)); // trigger NPE at line 4 in script (inside conditional) - exception = expectThrows(ScriptException.class, () -> { - exec(type + " x = null;\n" + - "boolean y = false;\n" + - "if (!y) {\n" + - " y = x.isEmpty();\n" + - "}\n" + - "return y;"); - }); + exception = expectThrows( + ScriptException.class, + () -> { + exec(type + " x = null;\n" + "boolean y = false;\n" + "if (!y) {\n" + " y = x.isEmpty();\n" + "}\n" + "return y;"); + } + ); // null deref at x.isEmpty(), the '.' is offset 53 assertScriptElementColumn(53, exception); - assertScriptStack(exception, - "y = x.isEmpty();\n}\n", - " ^---- HERE"); + assertScriptStack(exception, "y = x.isEmpty();\n}\n", " ^---- HERE"); assertThat(exception.getCause(), instanceOf(NullPointerException.class)); } } @@ -111,8 +94,9 @@ private void assertScriptElementColumn(int expectedColumn, ScriptException excep for (int i = 0; i < stackTrace.length; i++) { if (WriterConstants.CLASS_NAME.equals(stackTrace[i].getClassName())) { if (expectedColumn + 1 != stackTrace[i].getLineNumber()) { - AssertionFailedError assertion = new AssertionFailedError("Expected column to be [" + expectedColumn + "] but was [" - + stackTrace[i].getLineNumber() + "]"); + AssertionFailedError assertion = new AssertionFailedError( + "Expected column to be [" + expectedColumn + "] but was [" + stackTrace[i].getLineNumber() + "]" + ); assertion.initCause(exception); throw assertion; } @@ -123,68 +107,52 @@ private void assertScriptElementColumn(int expectedColumn, ScriptException excep } public void testInvalidShift() { - expectScriptThrows(ClassCastException.class, () -> { - exec("float x = 15F; x <<= 2; return x;"); - }); + expectScriptThrows(ClassCastException.class, () -> { exec("float x = 15F; x <<= 2; return x;"); }); - expectScriptThrows(ClassCastException.class, () -> { - exec("double x = 15F; x <<= 2; return x;"); - }); + expectScriptThrows(ClassCastException.class, () -> { exec("double x = 15F; x <<= 2; return x;"); }); } public void testBogusParameter() { - IllegalArgumentException expected = expectThrows(IllegalArgumentException.class, () -> { - exec("return 5;", null, Collections.singletonMap("bogusParameterKey", "bogusParameterValue"), true); - }); + IllegalArgumentException expected = expectThrows( + IllegalArgumentException.class, + () -> { exec("return 5;", null, Collections.singletonMap("bogusParameterKey", "bogusParameterValue"), true); } + ); assertTrue(expected.getMessage().contains("Unrecognized compile-time parameter")); } public void testInfiniteLoops() { - PainlessError expected = expectScriptThrows(PainlessError.class, () -> { - exec("boolean x = true; while (x) {}"); - }); - assertTrue(expected.getMessage().contains( - "The maximum number of statements that can be executed in a loop has been reached.")); + PainlessError expected = expectScriptThrows(PainlessError.class, () -> { exec("boolean x = true; while (x) {}"); }); + assertTrue(expected.getMessage().contains("The maximum number of statements that can be executed in a loop has been reached.")); - expected = expectScriptThrows(PainlessError.class, () -> { - exec("while (true) {int y = 5;}"); - }); - assertTrue(expected.getMessage().contains( - "The maximum number of statements that can be executed in a loop has been reached.")); + expected = expectScriptThrows(PainlessError.class, () -> { exec("while (true) {int y = 5;}"); }); + assertTrue(expected.getMessage().contains("The maximum number of statements that can be executed in a loop has been reached.")); - expected = expectScriptThrows(PainlessError.class, () -> { - exec("while (true) { boolean x = true; while (x) {} }"); - }); - assertTrue(expected.getMessage().contains( - "The maximum number of statements that can be executed in a loop has been reached.")); + expected = expectScriptThrows(PainlessError.class, () -> { exec("while (true) { boolean x = true; while (x) {} }"); }); + assertTrue(expected.getMessage().contains("The maximum number of statements that can be executed in a loop has been reached.")); expected = expectScriptThrows(PainlessError.class, () -> { exec("while (true) { boolean x = false; while (x) {} }"); fail("should have hit PainlessError"); }); - assertTrue(expected.getMessage().contains( - "The maximum number of statements that can be executed in a loop has been reached.")); + assertTrue(expected.getMessage().contains("The maximum number of statements that can be executed in a loop has been reached.")); expected = expectScriptThrows(PainlessError.class, () -> { exec("boolean x = true; for (;x;) {}"); fail("should have hit PainlessError"); }); - assertTrue(expected.getMessage().contains( - "The maximum number of statements that can be executed in a loop has been reached.")); + assertTrue(expected.getMessage().contains("The maximum number of statements that can be executed in a loop has been reached.")); expected = expectScriptThrows(PainlessError.class, () -> { exec("for (;;) {int x = 5;}"); fail("should have hit PainlessError"); }); - assertTrue(expected.getMessage().contains( - "The maximum number of statements that can be executed in a loop has been reached.")); + assertTrue(expected.getMessage().contains("The maximum number of statements that can be executed in a loop has been reached.")); expected = expectScriptThrows(PainlessError.class, () -> { exec("def x = true; do {int y = 5;} while (x)"); fail("should have hit PainlessError"); }); - assertTrue(expected.getMessage().contains( - "The maximum number of statements that can be executed in a loop has been reached.")); + assertTrue(expected.getMessage().contains("The maximum number of statements that can be executed in a loop has been reached.")); RuntimeException parseException = expectScriptThrows(RuntimeException.class, () -> { exec("try { int x; } catch (PainlessError error) {}", false); @@ -197,42 +165,32 @@ public void testLoopLimits() { // right below limit: ok exec("for (int x = 0; x < 999999; ++x) {}"); - PainlessError expected = expectScriptThrows(PainlessError.class, () -> { - exec("for (int x = 0; x < 1000000; ++x) {}"); - }); - assertTrue(expected.getMessage().contains( - "The maximum number of statements that can be executed in a loop has been reached.")); + PainlessError expected = expectScriptThrows(PainlessError.class, () -> { exec("for (int x = 0; x < 1000000; ++x) {}"); }); + assertTrue(expected.getMessage().contains("The maximum number of statements that can be executed in a loop has been reached.")); } public void testIllegalDynamicMethod() { - IllegalArgumentException expected = expectScriptThrows(IllegalArgumentException.class, () -> { - exec("def x = 'test'; return x.getClass().toString()"); - }); + IllegalArgumentException expected = expectScriptThrows( + IllegalArgumentException.class, + () -> { exec("def x = 'test'; return x.getClass().toString()"); } + ); assertTrue(expected.getMessage().contains("dynamic method [java.lang.String, getClass/0] not found")); } public void testDynamicNPE() { - expectScriptThrows(NullPointerException.class, () -> { - exec("def x = null; return x.toString()"); - }); + expectScriptThrows(NullPointerException.class, () -> { exec("def x = null; return x.toString()"); }); } public void testDynamicWrongArgs() { - expectScriptThrows(WrongMethodTypeException.class, () -> { - exec("def x = new ArrayList(); return x.get('bogus');"); - }); + expectScriptThrows(WrongMethodTypeException.class, () -> { exec("def x = new ArrayList(); return x.get('bogus');"); }); } public void testDynamicArrayWrongIndex() { - expectScriptThrows(WrongMethodTypeException.class, () -> { - exec("def x = new long[1]; x[0]=1; return x['bogus'];"); - }); + expectScriptThrows(WrongMethodTypeException.class, () -> { exec("def x = new long[1]; x[0]=1; return x['bogus'];"); }); } public void testDynamicListWrongIndex() { - expectScriptThrows(WrongMethodTypeException.class, () -> { - exec("def x = new ArrayList(); x.add('foo'); return x['bogus'];"); - }); + expectScriptThrows(WrongMethodTypeException.class, () -> { exec("def x = new ArrayList(); x.add('foo'); return x['bogus'];"); }); } /** @@ -248,27 +206,23 @@ public void testRCurlyNotDelim() { } public void testBadBoxingCast() { - expectScriptThrows(ClassCastException.class, () -> { - exec("BitSet bs = new BitSet(); bs.and(2);"); - }); + expectScriptThrows(ClassCastException.class, () -> { exec("BitSet bs = new BitSet(); bs.and(2);"); }); } public void testOutOfMemoryError() { assumeTrue("test only happens to work for sure on oracle jre", Constants.JAVA_VENDOR.startsWith("Oracle")); - expectScriptThrows(OutOfMemoryError.class, () -> { - exec("int[] x = new int[Integer.MAX_VALUE - 1];"); - }); + expectScriptThrows(OutOfMemoryError.class, () -> { exec("int[] x = new int[Integer.MAX_VALUE - 1];"); }); } public void testStackOverflowError() { - expectScriptThrows(StackOverflowError.class, () -> { - exec("void recurse(int x, int y) {recurse(x, y)} recurse(1, 2);"); - }); + expectScriptThrows(StackOverflowError.class, () -> { exec("void recurse(int x, int y) {recurse(x, y)} recurse(1, 2);"); }); } public void testCanNotOverrideRegexEnabled() { - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> exec("", null, singletonMap(CompilerSettings.REGEX_ENABLED.getKey(), "true"), false)); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> exec("", null, singletonMap(CompilerSettings.REGEX_ENABLED.getKey(), "true"), false) + ); assertEquals("[painless.regex.enabled] can only be set on node startup.", e.getMessage()); } @@ -277,8 +231,10 @@ public void testInvalidIntConstantSuggestsLong() { assertEquals("Invalid int constant [864000000000]. If you want a long constant then change it to [864000000000L].", e.getMessage()); assertEquals(864000000000L, exec("return 864000000000L")); e = expectScriptThrows(IllegalArgumentException.class, () -> exec("return -864000000000")); - assertEquals("Invalid int constant [-864000000000]. If you want a long constant then change it to [-864000000000L].", - e.getMessage()); + assertEquals( + "Invalid int constant [-864000000000]. If you want a long constant then change it to [-864000000000L].", + e.getMessage() + ); assertEquals(-864000000000L, exec("return -864000000000L")); // If it isn't a valid long we don't give any suggestions @@ -295,11 +251,15 @@ public void testQuestionSpaceDotIsNotNullSafeDereference() { public void testBadStringEscape() { Exception e = expectScriptThrows(IllegalArgumentException.class, () -> exec("'\\a'", false)); - assertEquals("unexpected character ['\\a]. The only valid escape sequences in strings starting with ['] are [\\\\] and [\\'].", - e.getMessage()); + assertEquals( + "unexpected character ['\\a]. The only valid escape sequences in strings starting with ['] are [\\\\] and [\\'].", + e.getMessage() + ); e = expectScriptThrows(IllegalArgumentException.class, () -> exec("\"\\a\"", false)); - assertEquals("unexpected character [\"\\a]. The only valid escape sequences in strings starting with [\"] are [\\\\] and [\\\"].", - e.getMessage()); + assertEquals( + "unexpected character [\"\\a]. The only valid escape sequences in strings starting with [\"] are [\\\\] and [\\\"].", + e.getMessage() + ); } public void testRegularUnexpectedCharacter() { @@ -717,20 +677,26 @@ public void testInvalidFullyQualifiedStaticReferenceType() { // brace access iae = expectScriptThrows(IllegalArgumentException.class, () -> exec("java.util.List[0]")); assertEquals(iae.getMessage(), "value required: instead found unexpected type [java.util.List]"); - iae = expectScriptThrows(IllegalArgumentException.class, () -> - exec("java.util.List[] x = new java.util.List[1]; x[java.util.List]")); + iae = expectScriptThrows( + IllegalArgumentException.class, + () -> exec("java.util.List[] x = new java.util.List[1]; x[java.util.List]") + ); assertEquals(iae.getMessage(), "value required: instead found unexpected type [java.util.List]"); iae = expectScriptThrows(IllegalArgumentException.class, () -> exec("def x = new java.util.List[1]; x[java.util.List]")); assertEquals(iae.getMessage(), "value required: instead found unexpected type [java.util.List]"); iae = expectScriptThrows(IllegalArgumentException.class, () -> exec("Map x = new HashMap(); x[java.util.List]")); assertEquals(iae.getMessage(), "value required: instead found unexpected type [java.util.List]"); - iae = expectScriptThrows(IllegalArgumentException.class, () -> - exec("java.util.List x = new java.util.ArrayList(); x[java.util.List]")); + iae = expectScriptThrows( + IllegalArgumentException.class, + () -> exec("java.util.List x = new java.util.ArrayList(); x[java.util.List]") + ); assertEquals(iae.getMessage(), "value required: instead found unexpected type [java.util.List]"); // method call - iae = expectScriptThrows(IllegalArgumentException.class, () -> - exec("java.util.List x = new java.util.ArrayList(); x.add(java.util.List)")); + iae = expectScriptThrows( + IllegalArgumentException.class, + () -> exec("java.util.List x = new java.util.ArrayList(); x.add(java.util.List)") + ); assertEquals(iae.getMessage(), "value required: instead found unexpected type [java.util.List]"); iae = expectScriptThrows(IllegalArgumentException.class, () -> exec("def x = new java.util.ArrayList(); x.add(java.util.List)")); assertEquals(iae.getMessage(), "value required: instead found unexpected type [java.util.List]"); @@ -756,8 +722,10 @@ public void testInvalidFullyQualifiedStaticReferenceType() { // dot access iae = expectScriptThrows(IllegalArgumentException.class, () -> exec("java.util.List[0]")); assertEquals(iae.getMessage(), "value required: instead found unexpected type [java.util.List]"); - iae = expectScriptThrows(IllegalArgumentException.class, () -> - exec("java.util.List[] x = new java.util.List[1]; x[java.util.List]")); + iae = expectScriptThrows( + IllegalArgumentException.class, + () -> exec("java.util.List[] x = new java.util.List[1]; x[java.util.List]") + ); assertEquals(iae.getMessage(), "value required: instead found unexpected type [java.util.List]"); // elvis @@ -815,8 +783,10 @@ public void testInvalidFullyQualifiedStaticReferenceType() { assertEquals(iae.getMessage(), "value required: instead found unexpected type [java.util.List]"); // for - iae = expectScriptThrows(IllegalArgumentException.class, () -> - exec("for (java.util.List x = java.util.List;;) {java.util.List x = 1;}")); + iae = expectScriptThrows( + IllegalArgumentException.class, + () -> exec("for (java.util.List x = java.util.List;;) {java.util.List x = 1;}") + ); assertEquals(iae.getMessage(), "value required: instead found unexpected type [java.util.List]"); iae = expectScriptThrows(IllegalArgumentException.class, () -> exec("for (;java.util.List;) {java.util.List x = 1;}")); assertEquals(iae.getMessage(), "value required: instead found unexpected type [java.util.List]"); @@ -828,8 +798,10 @@ public void testInvalidFullyQualifiedStaticReferenceType() { assertEquals(iae.getMessage(), "value required: instead found unexpected type [java.util.List]"); // if/else - iae = expectScriptThrows(IllegalArgumentException.class, () -> - exec("if (java.util.List) {java.util.List x = 1;} else {java.util.List x = 2;}")); + iae = expectScriptThrows( + IllegalArgumentException.class, + () -> exec("if (java.util.List) {java.util.List x = 1;} else {java.util.List x = 2;}") + ); assertEquals(iae.getMessage(), "value required: instead found unexpected type [java.util.List]"); // return @@ -846,8 +818,10 @@ public void testInvalidFullyQualifiedStaticReferenceType() { } public void testInvalidNullSafeBehavior() { - expectScriptThrows(ClassCastException.class, () -> - exec("def test = ['hostname': 'somehostname']; test?.hostname && params.host.hostname != ''")); + expectScriptThrows( + ClassCastException.class, + () -> exec("def test = ['hostname': 'somehostname']; test?.hostname && params.host.hostname != ''") + ); expectScriptThrows(NullPointerException.class, () -> exec("params?.host?.hostname && params.host?.hostname != ''")); } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/XorTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/XorTests.java index a75bf990a359a..586fb08b171b0 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/XorTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/XorTests.java @@ -57,21 +57,13 @@ public void testBoolConst() throws Exception { } public void testIllegal() throws Exception { - expectScriptThrows(ClassCastException.class, () -> { - exec("float x = (float)4; int y = 1; return x ^ y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("double x = (double)4; int y = 1; return x ^ y"); - }); + expectScriptThrows(ClassCastException.class, () -> { exec("float x = (float)4; int y = 1; return x ^ y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("double x = (double)4; int y = 1; return x ^ y"); }); } public void testDef() { - expectScriptThrows(ClassCastException.class, () -> { - exec("def x = (float)4; def y = (byte)1; return x ^ y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("def x = (double)4; def y = (byte)1; return x ^ y"); - }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = (float)4; def y = (byte)1; return x ^ y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = (double)4; def y = (byte)1; return x ^ y"); }); assertEquals(5, exec("def x = (byte)4; def y = (byte)1; return x ^ y")); assertEquals(5, exec("def x = (short)4; def y = (byte)1; return x ^ y")); assertEquals(5, exec("def x = (char)4; def y = (byte)1; return x ^ y")); @@ -109,18 +101,14 @@ public void testDef() { assertEquals(5L, exec("def x = (long)4; def y = (long)1; return x ^ y")); assertEquals(false, exec("def x = true; def y = true; return x ^ y")); - assertEquals(true, exec("def x = true; def y = false; return x ^ y")); - assertEquals(true, exec("def x = false; def y = true; return x ^ y")); + assertEquals(true, exec("def x = true; def y = false; return x ^ y")); + assertEquals(true, exec("def x = false; def y = true; return x ^ y")); assertEquals(false, exec("def x = false; def y = false; return x ^ y")); } public void testDefTypedLHS() { - expectScriptThrows(ClassCastException.class, () -> { - exec("float x = (float)4; def y = (byte)1; return x ^ y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("double x = (double)4; def y = (byte)1; return x ^ y"); - }); + expectScriptThrows(ClassCastException.class, () -> { exec("float x = (float)4; def y = (byte)1; return x ^ y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("double x = (double)4; def y = (byte)1; return x ^ y"); }); assertEquals(5, exec("def x = (byte)4; def y = (byte)1; return x ^ y")); assertEquals(5, exec("def x = (short)4; def y = (byte)1; return x ^ y")); assertEquals(5, exec("def x = (char)4; def y = (byte)1; return x ^ y")); @@ -158,18 +146,14 @@ public void testDefTypedLHS() { assertEquals(5L, exec("def x = (long)4; def y = (long)1; return x ^ y")); assertEquals(false, exec("def x = true; def y = true; return x ^ y")); - assertEquals(true, exec("def x = true; def y = false; return x ^ y")); - assertEquals(true, exec("def x = false; def y = true; return x ^ y")); + assertEquals(true, exec("def x = true; def y = false; return x ^ y")); + assertEquals(true, exec("def x = false; def y = true; return x ^ y")); assertEquals(false, exec("def x = false; def y = false; return x ^ y")); } public void testDefTypedRHS() { - expectScriptThrows(ClassCastException.class, () -> { - exec("def x = (float)4; byte y = (byte)1; return x ^ y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("def x = (double)4; byte y = (byte)1; return x ^ y"); - }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = (float)4; byte y = (byte)1; return x ^ y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = (double)4; byte y = (byte)1; return x ^ y"); }); assertEquals(5, exec("def x = (byte)4; byte y = (byte)1; return x ^ y")); assertEquals(5, exec("def x = (short)4; byte y = (byte)1; return x ^ y")); assertEquals(5, exec("def x = (char)4; byte y = (byte)1; return x ^ y")); @@ -207,8 +191,8 @@ public void testDefTypedRHS() { assertEquals(5L, exec("def x = (long)4; long y = (long)1; return x ^ y")); assertEquals(false, exec("def x = true; boolean y = true; return x ^ y")); - assertEquals(true, exec("def x = true; boolean y = false; return x ^ y")); - assertEquals(true, exec("def x = false; boolean y = true; return x ^ y")); + assertEquals(true, exec("def x = true; boolean y = false; return x ^ y")); + assertEquals(true, exec("def x = false; boolean y = true; return x ^ y")); assertEquals(false, exec("def x = false; boolean y = false; return x ^ y")); } @@ -236,18 +220,10 @@ public void testCompoundAssignment() { } public void testBogusCompoundAssignment() { - expectScriptThrows(ClassCastException.class, () -> { - exec("float x = 4; int y = 1; x ^= y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("double x = 4; int y = 1; x ^= y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("int x = 4; float y = 1; x ^= y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("int x = 4; double y = 1; x ^= y"); - }); + expectScriptThrows(ClassCastException.class, () -> { exec("float x = 4; int y = 1; x ^= y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("double x = 4; int y = 1; x ^= y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("int x = 4; float y = 1; x ^= y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("int x = 4; double y = 1; x ^= y"); }); } public void testCompoundAssignmentDef() { @@ -274,17 +250,9 @@ public void testCompoundAssignmentDef() { } public void testDefBogusCompoundAssignment() { - expectScriptThrows(ClassCastException.class, () -> { - exec("def x = 4F; int y = 1; x ^= y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("def x = 4D; int y = 1; x ^= y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("int x = 4; def y = (float)1; x ^= y"); - }); - expectScriptThrows(ClassCastException.class, () -> { - exec("int x = 4; def y = (double)1; x ^= y"); - }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = 4F; int y = 1; x ^= y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("def x = 4D; int y = 1; x ^= y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("int x = 4; def y = (float)1; x ^= y"); }); + expectScriptThrows(ClassCastException.class, () -> { exec("int x = 4; def y = (double)1; x ^= y"); }); } } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/action/ContextInfoTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/action/ContextInfoTests.java index d06153ebfdd0b..0d11a39d075f6 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/action/ContextInfoTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/action/ContextInfoTests.java @@ -9,8 +9,8 @@ package org.elasticsearch.painless.action; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.util.ArrayList; import java.util.List; @@ -36,9 +36,7 @@ protected PainlessContextInfo createTestInstance() { for (int parameter = 0; parameter < parameterSize; ++parameter) { parameters.add(randomAlphaOfLengthBetween(1, 20)); } - constructors.add(new PainlessContextConstructorInfo( - randomAlphaOfLength(randomIntBetween(4, 10)), - parameters)); + constructors.add(new PainlessContextConstructorInfo(randomAlphaOfLength(randomIntBetween(4, 10)), parameters)); } ; @@ -50,11 +48,14 @@ protected PainlessContextInfo createTestInstance() { for (int parameter = 0; parameter < parameterSize; ++parameter) { parameters.add(randomAlphaOfLengthBetween(1, 20)); } - staticMethods.add(new PainlessContextMethodInfo( + staticMethods.add( + new PainlessContextMethodInfo( randomAlphaOfLength(randomIntBetween(4, 10)), randomAlphaOfLength(randomIntBetween(4, 10)), randomAlphaOfLength(randomIntBetween(4, 10)), - parameters)); + parameters + ) + ); } int methodsSize = randomInt(10); @@ -65,34 +66,51 @@ protected PainlessContextInfo createTestInstance() { for (int parameter = 0; parameter < parameterSize; ++parameter) { parameters.add(randomAlphaOfLengthBetween(1, 20)); } - methods.add(new PainlessContextMethodInfo( + methods.add( + new PainlessContextMethodInfo( randomAlphaOfLength(randomIntBetween(4, 10)), randomAlphaOfLength(randomIntBetween(4, 10)), randomAlphaOfLength(randomIntBetween(4, 10)), - parameters)); + parameters + ) + ); } int staticFieldsSize = randomInt(10); List staticFields = new ArrayList<>(); for (int staticField = 0; staticField < staticFieldsSize; ++staticField) { - staticFields.add(new PainlessContextFieldInfo( + staticFields.add( + new PainlessContextFieldInfo( randomAlphaOfLength(randomIntBetween(4, 10)), randomAlphaOfLength(randomIntBetween(4, 10)), - randomAlphaOfLength(randomIntBetween(4, 10)))); + randomAlphaOfLength(randomIntBetween(4, 10)) + ) + ); } int fieldsSize = randomInt(4); List fields = new ArrayList<>(); for (int field = 0; field < fieldsSize; ++field) { - fields.add(new PainlessContextFieldInfo( + fields.add( + new PainlessContextFieldInfo( randomAlphaOfLength(randomIntBetween(4, 10)), randomAlphaOfLength(randomIntBetween(4, 10)), - randomAlphaOfLength(randomIntBetween(4, 10)))); + randomAlphaOfLength(randomIntBetween(4, 10)) + ) + ); } - classes.add(new PainlessContextClassInfo( - randomAlphaOfLength(randomIntBetween(3, 200)), randomBoolean(), - constructors, staticMethods, methods, fields, staticFields)); + classes.add( + new PainlessContextClassInfo( + randomAlphaOfLength(randomIntBetween(3, 200)), + randomBoolean(), + constructors, + staticMethods, + methods, + fields, + staticFields + ) + ); } int importedMethodsSize = randomInt(4); @@ -103,11 +121,14 @@ protected PainlessContextInfo createTestInstance() { for (int parameter = 0; parameter < parameterSize; ++parameter) { parameters.add(randomAlphaOfLengthBetween(1, 20)); } - importedMethods.add(new PainlessContextMethodInfo( + importedMethods.add( + new PainlessContextMethodInfo( randomAlphaOfLength(randomIntBetween(4, 10)), randomAlphaOfLength(randomIntBetween(4, 10)), randomAlphaOfLength(randomIntBetween(4, 10)), - parameters)); + parameters + ) + ); } int classBindingsSize = randomInt(3); @@ -119,12 +140,15 @@ protected PainlessContextInfo createTestInstance() { for (int parameter = 0; parameter < parameterSize; ++parameter) { parameters.add(randomAlphaOfLengthBetween(1, 20)); } - classBindings.add(new PainlessContextClassBindingInfo( + classBindings.add( + new PainlessContextClassBindingInfo( randomAlphaOfLength(randomIntBetween(4, 10)), randomAlphaOfLength(randomIntBetween(4, 10)), randomAlphaOfLength(randomIntBetween(4, 10)), readOnly, - parameters)); + parameters + ) + ); } int instanceBindingsSize = randomInt(3); @@ -135,15 +159,17 @@ protected PainlessContextInfo createTestInstance() { for (int parameter = 0; parameter < parameterSize; ++parameter) { parameters.add(randomAlphaOfLengthBetween(1, 20)); } - instanceBindings.add(new PainlessContextInstanceBindingInfo( + instanceBindings.add( + new PainlessContextInstanceBindingInfo( randomAlphaOfLength(randomIntBetween(4, 10)), randomAlphaOfLength(randomIntBetween(4, 10)), randomAlphaOfLength(randomIntBetween(4, 10)), - parameters)); + parameters + ) + ); } - return new PainlessContextInfo(randomAlphaOfLength(20), - classes, importedMethods, classBindings, instanceBindings); + return new PainlessContextInfo(randomAlphaOfLength(20), classes, importedMethods, classBindings, instanceBindings); } @Override diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/action/PainlessExecuteApiTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/action/PainlessExecuteApiTests.java index 0e72ddf363b8d..011d495d08cbe 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/action/PainlessExecuteApiTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/action/PainlessExecuteApiTests.java @@ -9,7 +9,6 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.IndexService; import org.elasticsearch.index.query.MatchAllQueryBuilder; import org.elasticsearch.index.query.MatchQueryBuilder; @@ -22,6 +21,7 @@ import org.elasticsearch.script.ScriptService; import org.elasticsearch.script.ScriptType; import org.elasticsearch.test.ESSingleNodeTestCase; +import org.elasticsearch.xcontent.XContentType; import java.io.IOException; import java.util.Arrays; @@ -56,10 +56,12 @@ public void testDefaults() throws IOException { response = innerShardOperation(request, scriptService, null); assertThat(response.getResult(), equalTo("0.1")); - Exception e = expectThrows(ScriptException.class, - () -> { - Request r = new Request(new Script(ScriptType.INLINE, - "painless", "params.count / params.total + doc['constant']", params), null, null); + Exception e = expectThrows(ScriptException.class, () -> { + Request r = new Request( + new Script(ScriptType.INLINE, "painless", "params.count / params.total + doc['constant']", params), + null, + null + ); innerShardOperation(r, scriptService, null); }); assertThat(e.getCause().getMessage(), equalTo("cannot resolve symbol [doc]")); @@ -77,15 +79,21 @@ public void testFilterExecutionContext() throws IOException { contextSetup = new Request.ContextSetup("index", new BytesArray("{\"field\": 3}"), null); contextSetup.setXContentType(XContentType.JSON); - request = new Request(new Script(ScriptType.INLINE, "painless", "doc['field'].value >= params.max", - singletonMap("max", 3)), "filter", contextSetup); + request = new Request( + new Script(ScriptType.INLINE, "painless", "doc['field'].value >= params.max", singletonMap("max", 3)), + "filter", + contextSetup + ); response = innerShardOperation(request, scriptService, indexService); assertThat(response.getResult(), equalTo(true)); contextSetup = new Request.ContextSetup("index", new BytesArray("{\"field\": 2}"), null); contextSetup.setXContentType(XContentType.JSON); - request = new Request(new Script(ScriptType.INLINE, "painless", "doc['field'].value >= params.max", - singletonMap("max", 3)), "filter", contextSetup); + request = new Request( + new Script(ScriptType.INLINE, "painless", "doc['field'].value >= params.max", singletonMap("max", 3)), + "filter", + contextSetup + ); response = innerShardOperation(request, scriptService, indexService); assertThat(response.getResult(), equalTo(false)); } @@ -94,12 +102,22 @@ public void testScoreExecutionContext() throws IOException { ScriptService scriptService = getInstanceFromNode(ScriptService.class); IndexService indexService = createIndex("index", Settings.EMPTY, "doc", "rank", "type=long", "text", "type=text"); - Request.ContextSetup contextSetup = new Request.ContextSetup("index", - new BytesArray("{\"rank\": 4.0, \"text\": \"quick brown fox\"}"), new MatchQueryBuilder("text", "fox")); + Request.ContextSetup contextSetup = new Request.ContextSetup( + "index", + new BytesArray("{\"rank\": 4.0, \"text\": \"quick brown fox\"}"), + new MatchQueryBuilder("text", "fox") + ); contextSetup.setXContentType(XContentType.JSON); - Request request = new Request(new Script(ScriptType.INLINE, "painless", - "Math.round((_score + (doc['rank'].value / params.max_rank)) * 100.0) / 100.0", singletonMap("max_rank", 5.0)), "score", - contextSetup); + Request request = new Request( + new Script( + ScriptType.INLINE, + "painless", + "Math.round((_score + (doc['rank'].value / params.max_rank)) * 100.0) / 100.0", + singletonMap("max_rank", 5.0) + ), + "score", + contextSetup + ); Response response = innerShardOperation(request, scriptService, indexService); assertThat(response.getResult(), equalTo(0.93D)); } @@ -108,20 +126,27 @@ public void testBooleanFieldExecutionContext() throws IOException { ScriptService scriptService = getInstanceFromNode(ScriptService.class); IndexService indexService = createIndex("index", Settings.EMPTY, "doc", "rank", "type=long", "text", "type=text"); - Request.ContextSetup contextSetup = new Request.ContextSetup("index", - new BytesArray("{\"rank\": 4.0, \"text\": \"quick brown fox\"}"), new MatchQueryBuilder("text", "fox")); + Request.ContextSetup contextSetup = new Request.ContextSetup( + "index", + new BytesArray("{\"rank\": 4.0, \"text\": \"quick brown fox\"}"), + new MatchQueryBuilder("text", "fox") + ); contextSetup.setXContentType(XContentType.JSON); - Request request = new Request(new Script(ScriptType.INLINE, "painless", - "emit(doc['rank'].value < params.max_rank)", singletonMap("max_rank", 5.0)), "boolean_field", - contextSetup); + Request request = new Request( + new Script(ScriptType.INLINE, "painless", "emit(doc['rank'].value < params.max_rank)", singletonMap("max_rank", 5.0)), + "boolean_field", + contextSetup + ); Response response = innerShardOperation(request, scriptService, indexService); assertEquals(Collections.singletonList(true), response.getResult()); contextSetup = new Request.ContextSetup("index", new BytesArray("{}"), new MatchAllQueryBuilder()); contextSetup.setXContentType(XContentType.JSON); - request = new Request(new Script(ScriptType.INLINE, "painless", - "emit(false); emit(true); emit (false);", emptyMap()), "boolean_field", - contextSetup); + request = new Request( + new Script(ScriptType.INLINE, "painless", "emit(false); emit(true); emit (false);", emptyMap()), + "boolean_field", + contextSetup + ); response = innerShardOperation(request, scriptService, indexService); assertEquals(Arrays.asList(false, false, true), response.getResult()); } @@ -130,29 +155,39 @@ public void testDateFieldExecutionContext() throws IOException { ScriptService scriptService = getInstanceFromNode(ScriptService.class); IndexService indexService = createIndex("index", Settings.EMPTY, "doc", "test_date", "type=date"); - Request.ContextSetup contextSetup = new Request.ContextSetup("index", - new BytesArray("{\"test_date\":\"2015-01-01T12:10:30Z\"}"), new MatchAllQueryBuilder()); + Request.ContextSetup contextSetup = new Request.ContextSetup( + "index", + new BytesArray("{\"test_date\":\"2015-01-01T12:10:30Z\"}"), + new MatchAllQueryBuilder() + ); contextSetup.setXContentType(XContentType.JSON); - Request request = new Request(new Script(ScriptType.INLINE, "painless", - "emit(doc['test_date'].value.toInstant().toEpochMilli())", emptyMap()), "date_field", - contextSetup); + Request request = new Request( + new Script(ScriptType.INLINE, "painless", "emit(doc['test_date'].value.toInstant().toEpochMilli())", emptyMap()), + "date_field", + contextSetup + ); Response response = innerShardOperation(request, scriptService, indexService); assertEquals(Collections.singletonList("2015-01-01T12:10:30.000Z"), response.getResult()); contextSetup = new Request.ContextSetup("index", new BytesArray("{}"), new MatchAllQueryBuilder()); contextSetup.setXContentType(XContentType.JSON); - request = new Request(new Script(ScriptType.INLINE, "painless", - "emit(ZonedDateTime.parse(\"2021-01-01T00:00:00Z\").toInstant().toEpochMilli());\n" + - "emit(ZonedDateTime.parse(\"1942-05-31T15:16:17Z\").toInstant().toEpochMilli());\n" + - "emit(ZonedDateTime.parse(\"2035-10-13T10:54:19Z\").toInstant().toEpochMilli());", - emptyMap()), "date_field", contextSetup); + request = new Request( + new Script( + ScriptType.INLINE, + "painless", + "emit(ZonedDateTime.parse(\"2021-01-01T00:00:00Z\").toInstant().toEpochMilli());\n" + + "emit(ZonedDateTime.parse(\"1942-05-31T15:16:17Z\").toInstant().toEpochMilli());\n" + + "emit(ZonedDateTime.parse(\"2035-10-13T10:54:19Z\").toInstant().toEpochMilli());", + emptyMap() + ), + "date_field", + contextSetup + ); response = innerShardOperation(request, scriptService, indexService); assertEquals( - Arrays.asList( - "2021-01-01T00:00:00.000Z", - "1942-05-31T15:16:17.000Z", - "2035-10-13T10:54:19.000Z"), - response.getResult()); + Arrays.asList("2021-01-01T00:00:00.000Z", "1942-05-31T15:16:17.000Z", "2035-10-13T10:54:19.000Z"), + response.getResult() + ); } @SuppressWarnings("unchecked") @@ -160,24 +195,36 @@ public void testDoubleFieldExecutionContext() throws IOException { ScriptService scriptService = getInstanceFromNode(ScriptService.class); IndexService indexService = createIndex("index", Settings.EMPTY, "doc", "rank", "type=long", "text", "type=text"); - Request.ContextSetup contextSetup = new Request.ContextSetup("index", - new BytesArray("{\"rank\": 4.0, \"text\": \"quick brown fox\"}"), new MatchQueryBuilder("text", "fox")); + Request.ContextSetup contextSetup = new Request.ContextSetup( + "index", + new BytesArray("{\"rank\": 4.0, \"text\": \"quick brown fox\"}"), + new MatchQueryBuilder("text", "fox") + ); contextSetup.setXContentType(XContentType.JSON); - Request request = new Request(new Script(ScriptType.INLINE, "painless", - "emit(doc['rank'].value); emit(Math.log(doc['rank'].value))", emptyMap()), "double_field", - contextSetup); + Request request = new Request( + new Script(ScriptType.INLINE, "painless", "emit(doc['rank'].value); emit(Math.log(doc['rank'].value))", emptyMap()), + "double_field", + contextSetup + ); Response response = innerShardOperation(request, scriptService, indexService); - List doubles = (List)response.getResult(); + List doubles = (List) response.getResult(); assertEquals(4.0, doubles.get(0), 0.00001); assertEquals(Math.log(4.0), doubles.get(1), 0.00001); contextSetup = new Request.ContextSetup("index", new BytesArray("{}"), new MatchAllQueryBuilder()); contextSetup.setXContentType(XContentType.JSON); - request = new Request(new Script(ScriptType.INLINE, "painless", + request = new Request( + new Script( + ScriptType.INLINE, + "painless", "emit(3.1); emit(2.29); emit(-12.47); emit(-12.46); emit(Double.MAX_VALUE); emit(0.0);", - emptyMap()), "double_field", contextSetup); + emptyMap() + ), + "double_field", + contextSetup + ); response = innerShardOperation(request, scriptService, indexService); - doubles = (List)response.getResult(); + doubles = (List) response.getResult(); assertEquals(3.1, doubles.get(0), 0.00001); assertEquals(2.29, doubles.get(1), 0.00001); assertEquals(-12.47, doubles.get(2), 0.00001); @@ -191,30 +238,37 @@ public void testGeoPointFieldExecutionContext() throws IOException { ScriptService scriptService = getInstanceFromNode(ScriptService.class); IndexService indexService = createIndex("index", Settings.EMPTY, "doc", "test_point", "type=geo_point"); - Request.ContextSetup contextSetup = new Request.ContextSetup("index", - new BytesArray("{\"test_point\":\"30.0,40.0\"}"), new MatchAllQueryBuilder()); + Request.ContextSetup contextSetup = new Request.ContextSetup( + "index", + new BytesArray("{\"test_point\":\"30.0,40.0\"}"), + new MatchAllQueryBuilder() + ); contextSetup.setXContentType(XContentType.JSON); - Request request = new Request(new Script(ScriptType.INLINE, "painless", - "emit(doc['test_point'].value.lat, doc['test_point'].value.lon)", emptyMap()), - "geo_point_field", contextSetup); + Request request = new Request( + new Script(ScriptType.INLINE, "painless", "emit(doc['test_point'].value.lat, doc['test_point'].value.lon)", emptyMap()), + "geo_point_field", + contextSetup + ); Response response = innerShardOperation(request, scriptService, indexService); - List> points = (List>)response.getResult(); - assertEquals(40.0, (double)((List)points.get(0).get("coordinates")).get(0), 0.00001); - assertEquals(30.0, (double)((List)points.get(0).get("coordinates")).get(1), 0.00001); + List> points = (List>) response.getResult(); + assertEquals(40.0, (double) ((List) points.get(0).get("coordinates")).get(0), 0.00001); + assertEquals(30.0, (double) ((List) points.get(0).get("coordinates")).get(1), 0.00001); assertEquals("Point", points.get(0).get("type")); contextSetup = new Request.ContextSetup("index", new BytesArray("{}"), new MatchAllQueryBuilder()); contextSetup.setXContentType(XContentType.JSON); - request = new Request(new Script(ScriptType.INLINE, "painless", - "emit(78.96, 12.12); emit(13.45, 56.78);", - emptyMap()), "geo_point_field", contextSetup); + request = new Request( + new Script(ScriptType.INLINE, "painless", "emit(78.96, 12.12); emit(13.45, 56.78);", emptyMap()), + "geo_point_field", + contextSetup + ); response = innerShardOperation(request, scriptService, indexService); - points = (List>)response.getResult(); - assertEquals(12.12, (double)((List)points.get(0).get("coordinates")).get(0), 0.00001); - assertEquals(78.96, (double)((List)points.get(0).get("coordinates")).get(1), 0.00001); + points = (List>) response.getResult(); + assertEquals(12.12, (double) ((List) points.get(0).get("coordinates")).get(0), 0.00001); + assertEquals(78.96, (double) ((List) points.get(0).get("coordinates")).get(1), 0.00001); assertEquals("Point", points.get(0).get("type")); - assertEquals(56.78, (double)((List)points.get(1).get("coordinates")).get(0), 0.00001); - assertEquals(13.45, (double)((List)points.get(1).get("coordinates")).get(1), 0.00001); + assertEquals(56.78, (double) ((List) points.get(1).get("coordinates")).get(0), 0.00001); + assertEquals(13.45, (double) ((List) points.get(1).get("coordinates")).get(1), 0.00001); assertEquals("Point", points.get(1).get("type")); } @@ -222,45 +276,70 @@ public void testIpFieldExecutionContext() throws IOException { ScriptService scriptService = getInstanceFromNode(ScriptService.class); IndexService indexService = createIndex("index", Settings.EMPTY, "doc", "test_ip", "type=ip"); - Request.ContextSetup contextSetup = new Request.ContextSetup("index", - new BytesArray("{\"test_ip\":\"192.168.1.254\"}"), new MatchAllQueryBuilder()); + Request.ContextSetup contextSetup = new Request.ContextSetup( + "index", + new BytesArray("{\"test_ip\":\"192.168.1.254\"}"), + new MatchAllQueryBuilder() + ); contextSetup.setXContentType(XContentType.JSON); - Request request = new Request(new Script(ScriptType.INLINE, "painless", - "emit(doc['test_ip'].value);", emptyMap()), - "ip_field", contextSetup); + Request request = new Request( + new Script(ScriptType.INLINE, "painless", "emit(doc['test_ip'].value);", emptyMap()), + "ip_field", + contextSetup + ); Response response = innerShardOperation(request, scriptService, indexService); assertEquals(Collections.singletonList("192.168.1.254"), response.getResult()); contextSetup = new Request.ContextSetup("index", new BytesArray("{}"), new MatchAllQueryBuilder()); contextSetup.setXContentType(XContentType.JSON); - request = new Request(new Script(ScriptType.INLINE, "painless", - "emit(\"192.168.0.1\"); emit(\"2001:db8::8a2e:370:7334\"); emit(\"2001:0db8:0000:0000:0000:8a2e:0370:7333\"); " + - "emit(\"127.0.0.1\"); emit(\"255.255.255.255\"); emit(\"0.0.0.0\");", - emptyMap()), "ip_field", contextSetup); + request = new Request( + new Script( + ScriptType.INLINE, + "painless", + "emit(\"192.168.0.1\"); emit(\"2001:db8::8a2e:370:7334\"); emit(\"2001:0db8:0000:0000:0000:8a2e:0370:7333\"); " + + "emit(\"127.0.0.1\"); emit(\"255.255.255.255\"); emit(\"0.0.0.0\");", + emptyMap() + ), + "ip_field", + contextSetup + ); response = innerShardOperation(request, scriptService, indexService); - assertEquals(Arrays.asList( - "192.168.0.1", "2001:db8::8a2e:370:7334", "2001:db8::8a2e:370:7333", "127.0.0.1", "255.255.255.255", "0.0.0.0"), - response.getResult()); + assertEquals( + Arrays.asList("192.168.0.1", "2001:db8::8a2e:370:7334", "2001:db8::8a2e:370:7333", "127.0.0.1", "255.255.255.255", "0.0.0.0"), + response.getResult() + ); } public void testLongFieldExecutionContext() throws IOException { ScriptService scriptService = getInstanceFromNode(ScriptService.class); IndexService indexService = createIndex("index", Settings.EMPTY, "doc", "test_value", "type=long"); - Request.ContextSetup contextSetup = new Request.ContextSetup("index", - new BytesArray("{\"test_value\":\"42\"}"), new MatchAllQueryBuilder()); + Request.ContextSetup contextSetup = new Request.ContextSetup( + "index", + new BytesArray("{\"test_value\":\"42\"}"), + new MatchAllQueryBuilder() + ); contextSetup.setXContentType(XContentType.JSON); - Request request = new Request(new Script(ScriptType.INLINE, "painless", - "emit(doc['test_value'].value); emit(doc['test_value'].value - 2);", emptyMap()), - "long_field", contextSetup); + Request request = new Request( + new Script(ScriptType.INLINE, "painless", "emit(doc['test_value'].value); emit(doc['test_value'].value - 2);", emptyMap()), + "long_field", + contextSetup + ); Response response = innerShardOperation(request, scriptService, indexService); assertEquals(Arrays.asList(42L, 40L), response.getResult()); contextSetup = new Request.ContextSetup("index", new BytesArray("{}"), new MatchAllQueryBuilder()); contextSetup.setXContentType(XContentType.JSON); - request = new Request(new Script(ScriptType.INLINE, "painless", + request = new Request( + new Script( + ScriptType.INLINE, + "painless", "emit(3L); emit(1L); emit(20000000000L); emit(10L); emit(-1000L); emit(0L);", - emptyMap()), "long_field", contextSetup); + emptyMap() + ), + "long_field", + contextSetup + ); response = innerShardOperation(request, scriptService, indexService); assertEquals(Arrays.asList(3L, 1L, 20000000000L, 10L, -1000L, 0L), response.getResult()); } @@ -269,21 +348,33 @@ public void testKeywordFieldExecutionContext() throws IOException { ScriptService scriptService = getInstanceFromNode(ScriptService.class); IndexService indexService = createIndex("index", Settings.EMPTY, "doc", "rank", "type=long", "text", "type=keyword"); - Request.ContextSetup contextSetup = new Request.ContextSetup("index", - new BytesArray("{\"rank\": 4.0, \"text\": \"quick brown fox\"}"), new MatchQueryBuilder("text", "fox")); + Request.ContextSetup contextSetup = new Request.ContextSetup( + "index", + new BytesArray("{\"rank\": 4.0, \"text\": \"quick brown fox\"}"), + new MatchQueryBuilder("text", "fox") + ); contextSetup.setXContentType(XContentType.JSON); contextSetup.setXContentType(XContentType.JSON); - Request request = new Request(new Script(ScriptType.INLINE, "painless", - "emit(doc['rank'].value + doc['text'].value)", emptyMap()), - "keyword_field", contextSetup); + Request request = new Request( + new Script(ScriptType.INLINE, "painless", "emit(doc['rank'].value + doc['text'].value)", emptyMap()), + "keyword_field", + contextSetup + ); Response response = innerShardOperation(request, scriptService, indexService); assertEquals(Collections.singletonList("4quick brown fox"), response.getResult()); contextSetup = new Request.ContextSetup("index", new BytesArray("{}"), new MatchAllQueryBuilder()); contextSetup.setXContentType(XContentType.JSON); - request = new Request(new Script(ScriptType.INLINE, "painless", + request = new Request( + new Script( + ScriptType.INLINE, + "painless", "emit(\"test\"); emit(\"baz was not here\"); emit(\"Data\"); emit(\"-10\"); emit(\"20\"); emit(\"9\");", - emptyMap()), "keyword_field", contextSetup); + emptyMap() + ), + "keyword_field", + contextSetup + ); response = innerShardOperation(request, scriptService, indexService); assertEquals(Arrays.asList("test", "baz was not here", "Data", "-10", "20", "9"), response.getResult()); } @@ -294,12 +385,13 @@ public void testCompositeExecutionContext() throws IOException { Request.ContextSetup contextSetup = new Request.ContextSetup("index", new BytesArray("{}"), new MatchAllQueryBuilder()); contextSetup.setXContentType(XContentType.JSON); - Request request = new Request(new Script(ScriptType.INLINE, "painless", - "emit(\"foo\", \"bar\"); emit(\"foo2\", 2);", emptyMap()), "composite_field", contextSetup); + Request request = new Request( + new Script(ScriptType.INLINE, "painless", "emit(\"foo\", \"bar\"); emit(\"foo2\", 2);", emptyMap()), + "composite_field", + contextSetup + ); Response response = innerShardOperation(request, scriptService, indexService); - assertEquals(Map.of( - "composite_field.foo", List.of("bar"), - "composite_field.foo2", List.of(2)), response.getResult()); + assertEquals(Map.of("composite_field.foo", List.of("bar"), "composite_field.foo2", List.of(2)), response.getResult()); } public void testContextWhitelists() throws IOException { @@ -307,7 +399,7 @@ public void testContextWhitelists() throws IOException { // score Request request = new Request(new Script("sigmoid(1.0, 2.0, 3.0)"), null, null); Response response = innerShardOperation(request, scriptService, null); - double result = Double.parseDouble((String)response.getResult()); + double result = Double.parseDouble((String) response.getResult()); assertEquals(0.111, result, 0.001); // ingest @@ -318,12 +410,12 @@ public void testContextWhitelists() throws IOException { // movfn request = new Request(new Script("MovingFunctions.max(new double[]{1, 3, 2})"), null, null); response = innerShardOperation(request, scriptService, null); - assertEquals(3.0, Double.parseDouble((String)response.getResult()), .1); + assertEquals(3.0, Double.parseDouble((String) response.getResult()), .1); // json request = new Request(new Script("Json.load('{\"a\": 1, \"b\": 2}')['b']"), null, null); response = innerShardOperation(request, scriptService, null); - assertEquals(2, Integer.parseInt((String)response.getResult())); + assertEquals(2, Integer.parseInt((String) response.getResult())); } } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/action/PainlessExecuteRequestTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/action/PainlessExecuteRequestTests.java index 969f17a2fc875..6d82f55fbb4dd 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/action/PainlessExecuteRequestTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/action/PainlessExecuteRequestTests.java @@ -13,11 +13,6 @@ import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContent; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.query.MatchAllQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.painless.action.PainlessExecuteAction.Request.ContextSetup; @@ -26,6 +21,11 @@ import org.elasticsearch.script.ScriptType; import org.elasticsearch.search.SearchModule; import org.elasticsearch.test.AbstractWireSerializingTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; import java.io.IOException; import java.io.UncheckedIOException; @@ -62,8 +62,7 @@ public final void testFromXContent() throws Exception { @Override protected NamedWriteableRegistry getNamedWriteableRegistry() { - return new NamedWriteableRegistry(new SearchModule(Settings.EMPTY, Collections.emptyList() - ).getNamedWriteables()); + return new NamedWriteableRegistry(new SearchModule(Settings.EMPTY, Collections.emptyList()).getNamedWriteables()); } @Override @@ -92,7 +91,7 @@ public void testValidate() { assertEquals("Validation Failed: 1: only inline scripts are supported;", e.getMessage()); } - private static ContextSetup randomContextSetup() { + private static ContextSetup randomContextSetup() { String index = randomBoolean() ? randomAlphaOfLength(4) : null; QueryBuilder query = randomBoolean() ? new MatchAllQueryBuilder() : null; BytesReference doc = null; diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/action/PainlessExecuteResponseTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/action/PainlessExecuteResponseTests.java index 945f069943045..069fa5e56929b 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/action/PainlessExecuteResponseTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/action/PainlessExecuteResponseTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.painless.action; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/action/SuggestTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/action/SuggestTests.java index a9311c3e9885f..c930d38675a26 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/action/SuggestTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/action/SuggestTests.java @@ -38,116 +38,180 @@ private void compareTokens(List tokens, String... expected) { } public void testSuggestLexer() { - compareTokens( - getSuggestTokens("test"), - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ID), "test" - ); + compareTokens(getSuggestTokens("test"), SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ID), "test"); compareTokens( - getSuggestTokens("int test;"), - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.TYPE), "int", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ID), "test", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.SEMICOLON), ";" + getSuggestTokens("int test;"), + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.TYPE), + "int", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ID), + "test", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.SEMICOLON), + ";" ); compareTokens( - getSuggestTokens("ArrayList test;"), - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.TYPE), "ArrayList", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ID), "test", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.SEMICOLON), ";" + getSuggestTokens("ArrayList test;"), + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.TYPE), + "ArrayList", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ID), + "test", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.SEMICOLON), + ";" ); compareTokens( - getSuggestTokens("def test;"), - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.TYPE), "def", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ID), "test", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.SEMICOLON), ";" + getSuggestTokens("def test;"), + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.TYPE), + "def", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ID), + "test", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.SEMICOLON), + ";" ); compareTokens( - getSuggestTokens("int[] test;"), - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ATYPE), "int[]", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ID), "test", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.SEMICOLON), ";" + getSuggestTokens("int[] test;"), + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ATYPE), + "int[]", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ID), + "test", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.SEMICOLON), + ";" ); compareTokens( - getSuggestTokens("ArrayList[] test;"), - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ATYPE), "ArrayList[]", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ID), "test", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.SEMICOLON), ";" + getSuggestTokens("ArrayList[] test;"), + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ATYPE), + "ArrayList[]", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ID), + "test", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.SEMICOLON), + ";" ); compareTokens( - getSuggestTokens("def[] test;"), - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ATYPE), "def[]", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ID), "test", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.SEMICOLON), ";" + getSuggestTokens("def[] test;"), + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ATYPE), + "def[]", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ID), + "test", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.SEMICOLON), + ";" ); compareTokens( - getSuggestTokens("List test = new ArrayList(); test."), - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.TYPE), "List", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ID), "test", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ASSIGN), "=", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.NEW), "new", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.TYPE), "ArrayList", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.LP), "(", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.RP), ")", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.SEMICOLON), ";", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ID), "test", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.DOT), "." + getSuggestTokens("List test = new ArrayList(); test."), + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.TYPE), + "List", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ID), + "test", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ASSIGN), + "=", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.NEW), + "new", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.TYPE), + "ArrayList", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.LP), + "(", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.RP), + ")", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.SEMICOLON), + ";", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ID), + "test", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.DOT), + "." ); compareTokens( - getSuggestTokens("List test = new ArrayList(); test.add"), - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.TYPE), "List", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ID), "test", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ASSIGN), "=", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.NEW), "new", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.TYPE), "ArrayList", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.LP), "(", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.RP), ")", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.SEMICOLON), ";", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ID), "test", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.DOT), ".", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.DOTID), "add" + getSuggestTokens("List test = new ArrayList(); test.add"), + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.TYPE), + "List", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ID), + "test", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ASSIGN), + "=", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.NEW), + "new", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.TYPE), + "ArrayList", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.LP), + "(", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.RP), + ")", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.SEMICOLON), + ";", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ID), + "test", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.DOT), + ".", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.DOTID), + "add" ); compareTokens( - getSuggestTokens("List test = new ArrayList(); test.add("), - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.TYPE), "List", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ID), "test", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ASSIGN), "=", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.NEW), "new", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.TYPE), "ArrayList", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.LP), "(", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.RP), ")", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.SEMICOLON), ";", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ID), "test", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.DOT), ".", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.DOTID), "add", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.LP), "(" + getSuggestTokens("List test = new ArrayList(); test.add("), + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.TYPE), + "List", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ID), + "test", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ASSIGN), + "=", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.NEW), + "new", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.TYPE), + "ArrayList", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.LP), + "(", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.RP), + ")", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.SEMICOLON), + ";", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ID), + "test", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.DOT), + ".", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.DOTID), + "add", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.LP), + "(" ); compareTokens( - getSuggestTokens("def test(int param) {return param;} test(2);"), - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.TYPE), "def", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ID), "test", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.LP), "(", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.TYPE), "int", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ID), "param", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.RP), ")", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.LBRACK), "{", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.RETURN), "return", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ID), "param", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.SEMICOLON), ";", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.RBRACK), "}", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ID), "test", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.LP), "(", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.INTEGER), "2", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.RP), ")", - SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.SEMICOLON), ";" + getSuggestTokens("def test(int param) {return param;} test(2);"), + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.TYPE), + "def", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ID), + "test", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.LP), + "(", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.TYPE), + "int", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ID), + "param", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.RP), + ")", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.LBRACK), + "{", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.RETURN), + "return", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ID), + "param", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.SEMICOLON), + ";", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.RBRACK), + "}", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.ID), + "test", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.LP), + "(", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.INTEGER), + "2", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.RP), + ")", + SuggestLexer.VOCABULARY.getDisplayName(SuggestLexer.SEMICOLON), + ";" ); } } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/api/LimitedCharSequenceTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/api/LimitedCharSequenceTests.java index 6d2c7df41e358..b657ace7bd038 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/api/LimitedCharSequenceTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/api/LimitedCharSequenceTests.java @@ -15,14 +15,10 @@ public class LimitedCharSequenceTests extends ESTestCase { public void testBadFactor() { - IllegalArgumentException badArg = expectThrows(IllegalArgumentException.class, - () -> new LimitedCharSequence("abc", null, -1) - ); + IllegalArgumentException badArg = expectThrows(IllegalArgumentException.class, () -> new LimitedCharSequence("abc", null, -1)); assertEquals("limitFactor must be positive", badArg.getMessage()); - badArg = expectThrows(IllegalArgumentException.class, - () -> new LimitedCharSequence("abc", null, 0) - ); + badArg = expectThrows(IllegalArgumentException.class, () -> new LimitedCharSequence("abc", null, 0)); assertEquals("limitFactor must be positive", badArg.getMessage()); } @@ -33,9 +29,9 @@ public void testLength() { public void testCharAtEqualLimit() { String str = "abc"; - for (int limitFactor=1; limitFactor < 4; limitFactor++){ + for (int limitFactor = 1; limitFactor < 4; limitFactor++) { CharSequence seq = new LimitedCharSequence(str, null, limitFactor); - for (int i=0; i seq.charAt(0)); assertEquals( - "[scripting] Regular expression considered too many characters, " + - "pattern: [a.*bc], " + - "limit factor: [2], " + - "char limit: [6], " + - "count: [7], " + - "wrapped: [abc], " + - "this limit can be changed by changed by the [script.painless.regex.limit-factor] setting", - circuitBreakingException.getMessage()); + "[scripting] Regular expression considered too many characters, " + + "pattern: [a.*bc], " + + "limit factor: [2], " + + "char limit: [6], " + + "count: [7], " + + "wrapped: [abc], " + + "this limit can be changed by changed by the [script.painless.regex.limit-factor] setting", + circuitBreakingException.getMessage() + ); final CharSequence seqNullPattern = new LimitedCharSequence(str, null, 2); for (int i = 0; i < 6; i++) { @@ -66,13 +63,14 @@ public void testCharAtAboveLimit() { } circuitBreakingException = expectThrows(CircuitBreakingException.class, () -> seqNullPattern.charAt(0)); assertEquals( - "[scripting] Regular expression considered too many characters, " + - "limit factor: [2], " + - "char limit: [6], " + - "count: [7], " + - "wrapped: [abc], " + - "this limit can be changed by changed by the [script.painless.regex.limit-factor] setting", - circuitBreakingException.getMessage()); + "[scripting] Regular expression considered too many characters, " + + "limit factor: [2], " + + "char limit: [6], " + + "count: [7], " + + "wrapped: [abc], " + + "this limit can be changed by changed by the [script.painless.regex.limit-factor] setting", + circuitBreakingException.getMessage() + ); } public void testSubSequence() { diff --git a/modules/lang-painless/src/yamlRestTest/java/org/elasticsearch/painless/LangPainlessClientYamlTestSuiteIT.java b/modules/lang-painless/src/yamlRestTest/java/org/elasticsearch/painless/LangPainlessClientYamlTestSuiteIT.java index 7cc46c79ad22b..2c1e86d303d9b 100644 --- a/modules/lang-painless/src/yamlRestTest/java/org/elasticsearch/painless/LangPainlessClientYamlTestSuiteIT.java +++ b/modules/lang-painless/src/yamlRestTest/java/org/elasticsearch/painless/LangPainlessClientYamlTestSuiteIT.java @@ -10,6 +10,7 @@ import com.carrotsearch.randomizedtesting.annotations.Name; import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; + import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate; import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase; @@ -25,4 +26,3 @@ public static Iterable parameters() throws Exception { return ESClientYamlSuiteTestCase.createParameters(); } } - diff --git a/modules/mapper-extras/src/internalClusterTest/java/org/elasticsearch/index/mapper/MatchOnlyTextFieldMapperTests.java b/modules/mapper-extras/src/internalClusterTest/java/org/elasticsearch/index/mapper/MatchOnlyTextFieldMapperTests.java index e669b0d848dff..15078ba11634d 100644 --- a/modules/mapper-extras/src/internalClusterTest/java/org/elasticsearch/index/mapper/MatchOnlyTextFieldMapperTests.java +++ b/modules/mapper-extras/src/internalClusterTest/java/org/elasticsearch/index/mapper/MatchOnlyTextFieldMapperTests.java @@ -16,12 +16,12 @@ import org.apache.lucene.index.IndexableField; import org.apache.lucene.index.IndexableFieldType; import org.elasticsearch.common.Strings; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.index.mapper.extras.MapperExtrasPlugin; import org.elasticsearch.index.mapper.extras.MatchOnlyTextFieldMapper; import org.elasticsearch.index.query.SearchExecutionContext; import org.elasticsearch.plugins.Plugin; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; import org.hamcrest.Matchers; import java.io.IOException; @@ -53,9 +53,10 @@ public final void testExists() throws IOException { @Override protected void registerParameters(ParameterChecker checker) throws IOException { - checker.registerUpdateCheck(b -> { - b.field("meta", Collections.singletonMap("format", "mysql.access")); - }, m -> assertEquals(Collections.singletonMap("format", "mysql.access"), m.fieldType().meta())); + checker.registerUpdateCheck( + b -> { b.field("meta", Collections.singletonMap("format", "mysql.access")); }, + m -> assertEquals(Collections.singletonMap("format", "mysql.access"), m.fieldType().meta()) + ); } @Override @@ -106,12 +107,7 @@ public void testSimpleMerge() throws IOException { assertThat(mapperService.documentMapper().mappers().getMapper("field"), instanceOf(MatchOnlyTextFieldMapper.class)); XContentBuilder newField = mapping(b -> { - b.startObject("field") - .field("type", "match_only_text") - .startObject("meta") - .field("key", "value") - .endObject() - .endObject(); + b.startObject("field").field("type", "match_only_text").startObject("meta").field("key", "value").endObject().endObject(); b.startObject("other_field").field("type", "keyword").endObject(); }); merge(mapperService, newField); diff --git a/modules/mapper-extras/src/internalClusterTest/java/org/elasticsearch/index/mapper/TokenCountFieldMapperIntegrationIT.java b/modules/mapper-extras/src/internalClusterTest/java/org/elasticsearch/index/mapper/TokenCountFieldMapperIntegrationIT.java index b2419fef68313..b2a7560985165 100644 --- a/modules/mapper-extras/src/internalClusterTest/java/org/elasticsearch/index/mapper/TokenCountFieldMapperIntegrationIT.java +++ b/modules/mapper-extras/src/internalClusterTest/java/org/elasticsearch/index/mapper/TokenCountFieldMapperIntegrationIT.java @@ -10,6 +10,7 @@ import com.carrotsearch.randomizedtesting.annotations.Name; import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; + import org.elasticsearch.ElasticsearchException; import org.elasticsearch.action.DocWriteResponse; import org.elasticsearch.action.bulk.BulkResponse; @@ -52,8 +53,10 @@ public static Iterable buildParameters() { private final boolean storeCountedFields; private final boolean loadCountedFields; - public TokenCountFieldMapperIntegrationIT(@Name("storeCountedFields") boolean storeCountedFields, - @Name("loadCountedFields") boolean loadCountedFields) { + public TokenCountFieldMapperIntegrationIT( + @Name("storeCountedFields") boolean storeCountedFields, + @Name("loadCountedFields") boolean loadCountedFields + ) { this.storeCountedFields = storeCountedFields; this.loadCountedFields = loadCountedFields; } @@ -96,10 +99,8 @@ public void testSearchByTokenCount() throws IOException { public void testFacetByTokenCount() throws IOException { init(); - String facetField = randomFrom(Arrays.asList( - "foo.token_count", "foo.token_count_unstored", "foo.token_count_with_doc_values")); - SearchResponse result = searchByNumericRange(1, 10) - .addAggregation(AggregationBuilders.terms("facet").field(facetField)).get(); + String facetField = randomFrom(Arrays.asList("foo.token_count", "foo.token_count_unstored", "foo.token_count_with_doc_values")); + SearchResponse result = searchByNumericRange(1, 10).addAggregation(AggregationBuilders.terms("facet").field(facetField)).get(); assertSearchReturns(result, "single", "bulk1", "bulk2", "multi", "multibulk1", "multibulk2"); assertThat(result.getAggregations().asList().size(), equalTo(1)); Terms terms = (Terms) result.getAggregations().asList().get(0); @@ -111,52 +112,59 @@ private void init() throws IOException { settings.put(indexSettings()); settings.put("index.analysis.analyzer.mock_english.tokenizer", "standard"); settings.put("index.analysis.analyzer.mock_english.filter", "stop"); - prepareCreate("test") - .setSettings(settings) - .setMapping(jsonBuilder().startObject() - .startObject("_doc") + prepareCreate("test").setSettings(settings) + .setMapping( + jsonBuilder().startObject() + .startObject("_doc") .startObject("properties") - .startObject("foo") - .field("type", "text") - .field("store", storeCountedFields) - .field("analyzer", "simple") - .startObject("fields") - .startObject("token_count") - .field("type", "token_count") - .field("analyzer", "standard") - .field("store", true) - .endObject() - .startObject("token_count_unstored") - .field("type", "token_count") - .field("analyzer", "standard") - .endObject() - .startObject("token_count_with_doc_values") - .field("type", "token_count") - .field("analyzer", "standard") - .field("doc_values", true) - .endObject() - .startObject("token_count_without_position_increments") - .field("type", "token_count") - .field("analyzer", "mock_english") - .field("enable_position_increments", false) - .field("store", true) - .endObject() - .endObject() - .endObject() + .startObject("foo") + .field("type", "text") + .field("store", storeCountedFields) + .field("analyzer", "simple") + .startObject("fields") + .startObject("token_count") + .field("type", "token_count") + .field("analyzer", "standard") + .field("store", true) + .endObject() + .startObject("token_count_unstored") + .field("type", "token_count") + .field("analyzer", "standard") + .endObject() + .startObject("token_count_with_doc_values") + .field("type", "token_count") + .field("analyzer", "standard") + .field("doc_values", true) + .endObject() + .startObject("token_count_without_position_increments") + .field("type", "token_count") + .field("analyzer", "mock_english") + .field("enable_position_increments", false) + .field("store", true) + .endObject() + .endObject() + .endObject() + .endObject() + .endObject() .endObject() - .endObject().endObject()).get(); + ) + .get(); ensureGreen(); assertEquals(DocWriteResponse.Result.CREATED, prepareIndex("single", "I have four terms").get().getResult()); BulkResponse bulk = client().prepareBulk() - .add(prepareIndex("bulk1", "bulk three terms")) - .add(prepareIndex("bulk2", "this has five bulk terms")).get(); + .add(prepareIndex("bulk1", "bulk three terms")) + .add(prepareIndex("bulk2", "this has five bulk terms")) + .get(); assertFalse(bulk.buildFailureMessage(), bulk.hasFailures()); - assertEquals(DocWriteResponse.Result.CREATED, - prepareIndex("multi", "two terms", "wow now I have seven lucky terms").get().getResult()); + assertEquals( + DocWriteResponse.Result.CREATED, + prepareIndex("multi", "two terms", "wow now I have seven lucky terms").get().getResult() + ); bulk = client().prepareBulk() - .add(prepareIndex("multibulk1", "one", "oh wow now I have eight unlucky terms")) - .add(prepareIndex("multibulk2", "six is a bunch of terms", "ten! ten terms is just crazy! too many too count!")).get(); + .add(prepareIndex("multibulk1", "one", "oh wow now I have eight unlucky terms")) + .add(prepareIndex("multibulk2", "six is a bunch of terms", "ten! ten terms is just crazy! too many too count!")) + .get(); assertFalse(bulk.buildFailureMessage(), bulk.hasFailures()); assertThat(refresh().getFailedShards(), equalTo(0)); @@ -171,9 +179,11 @@ private SearchResponse searchById(String id) { } private SearchRequestBuilder searchByNumericRange(int low, int high) { - return prepareSearch().setQuery(QueryBuilders.rangeQuery(randomFrom( - Arrays.asList("foo.token_count", "foo.token_count_unstored", "foo.token_count_with_doc_values") - )).gte(low).lte(high)); + return prepareSearch().setQuery( + QueryBuilders.rangeQuery( + randomFrom(Arrays.asList("foo.token_count", "foo.token_count_unstored", "foo.token_count_with_doc_values")) + ).gte(low).lte(high) + ); } private SearchRequestBuilder prepareSearch() { @@ -197,17 +207,17 @@ private void assertSearchReturns(SearchResponse result, String... ids) { for (SearchHit hit : result.getHits()) { String id = hit.getId(); if (id.equals("single")) { - assertSearchHit(hit, new int[]{4}, new int[]{4}); + assertSearchHit(hit, new int[] { 4 }, new int[] { 4 }); } else if (id.equals("bulk1")) { - assertSearchHit(hit, new int[]{3}, new int[]{3}); + assertSearchHit(hit, new int[] { 3 }, new int[] { 3 }); } else if (id.equals("bulk2")) { - assertSearchHit(hit, new int[]{5}, new int[]{4}); + assertSearchHit(hit, new int[] { 5 }, new int[] { 4 }); } else if (id.equals("multi")) { - assertSearchHit(hit, new int[]{2, 7}, new int[]{2, 7}); + assertSearchHit(hit, new int[] { 2, 7 }, new int[] { 2, 7 }); } else if (id.equals("multibulk1")) { - assertSearchHit(hit, new int[]{1, 8}, new int[]{1, 8}); + assertSearchHit(hit, new int[] { 1, 8 }, new int[] { 1, 8 }); } else if (id.equals("multibulk2")) { - assertSearchHit(hit, new int[]{6, 10}, new int[]{3, 9}); + assertSearchHit(hit, new int[] { 6, 10 }, new int[] { 3, 9 }); } else { throw new ElasticsearchException("Unexpected response!"); } @@ -224,8 +234,7 @@ private void assertSearchHit(SearchHit hit, int[] standardTermCounts, int[] engl assertThat(hit.field("foo.token_count_without_position_increments"), not(nullValue())); assertThat(hit.field("foo.token_count_without_position_increments").getValues().size(), equalTo(englishTermCounts.length)); for (int i = 0; i < englishTermCounts.length; i++) { - assertThat(hit.field("foo.token_count_without_position_increments").getValues().get(i), - equalTo(englishTermCounts[i])); + assertThat(hit.field("foo.token_count_without_position_increments").getValues().get(i), equalTo(englishTermCounts[i])); } if (loadCountedFields && storeCountedFields) { diff --git a/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/MapperExtrasPlugin.java b/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/MapperExtrasPlugin.java index dbe22d474c502..b159177d2c8b0 100644 --- a/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/MapperExtrasPlugin.java +++ b/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/MapperExtrasPlugin.java @@ -41,8 +41,8 @@ public Map getMetadataMappers() { @Override public List> getQueries() { return Collections.singletonList( - new QuerySpec<>(RankFeatureQueryBuilder.NAME, RankFeatureQueryBuilder::new, - p -> RankFeatureQueryBuilder.PARSER.parse(p, null))); + new QuerySpec<>(RankFeatureQueryBuilder.NAME, RankFeatureQueryBuilder::new, p -> RankFeatureQueryBuilder.PARSER.parse(p, null)) + ); } } diff --git a/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/MatchOnlyTextFieldMapper.java b/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/MatchOnlyTextFieldMapper.java index cf39b9d4429bc..7e5f300f78814 100644 --- a/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/MatchOnlyTextFieldMapper.java +++ b/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/MatchOnlyTextFieldMapper.java @@ -94,9 +94,11 @@ public Builder(String name, IndexAnalyzers indexAnalyzers) { public Builder(String name, Version indexCreatedVersion, IndexAnalyzers indexAnalyzers) { super(name); this.indexCreatedVersion = indexCreatedVersion; - this.analyzers = new TextParams.Analyzers(indexAnalyzers, - m -> ((MatchOnlyTextFieldMapper) m).indexAnalyzer, - m -> ((MatchOnlyTextFieldMapper) m).positionIncrementGap); + this.analyzers = new TextParams.Analyzers( + indexAnalyzers, + m -> ((MatchOnlyTextFieldMapper) m).indexAnalyzer, + m -> ((MatchOnlyTextFieldMapper) m).positionIncrementGap + ); } @Override @@ -117,14 +119,7 @@ private MatchOnlyTextFieldType buildFieldType(MapperBuilderContext context) { public MatchOnlyTextFieldMapper build(MapperBuilderContext context) { MatchOnlyTextFieldType tft = buildFieldType(context); MultiFields multiFields = multiFieldsBuilder.build(this, context); - return new MatchOnlyTextFieldMapper( - name, - Defaults.FIELD_TYPE, - tft, - multiFields, - copyTo.build(), - this - ); + return new MatchOnlyTextFieldMapper(name, Defaults.FIELD_TYPE, tft, multiFields, copyTo.build(), this); } } @@ -165,8 +160,9 @@ public ValueFetcher valueFetcher(SearchExecutionContext context, String format) return SourceValueFetcher.toString(name(), context, format); } - private Function, IOException>> getValueFetcherProvider( - SearchExecutionContext searchExecutionContext) { + private Function, IOException>> getValueFetcherProvider( + SearchExecutionContext searchExecutionContext + ) { if (searchExecutionContext.isSourceEnabled() == false) { throw new IllegalArgumentException( "Field [" + name() + "] of type [" + CONTENT_TYPE + "] cannot run positional queries since [_source] is disabled." @@ -189,13 +185,15 @@ private Function, IOException> private Query toQuery(Query query, SearchExecutionContext searchExecutionContext) { return new ConstantScoreQuery( - new SourceConfirmedTextQuery(query, getValueFetcherProvider(searchExecutionContext), indexAnalyzer)); + new SourceConfirmedTextQuery(query, getValueFetcherProvider(searchExecutionContext), indexAnalyzer) + ); } private IntervalsSource toIntervalsSource( - IntervalsSource source, - Query approximation, - SearchExecutionContext searchExecutionContext) { + IntervalsSource source, + Query approximation, + SearchExecutionContext searchExecutionContext + ) { return new SourceIntervalsSource(source, approximation, getValueFetcherProvider(searchExecutionContext), indexAnalyzer); } @@ -229,10 +227,14 @@ public IntervalsSource prefixIntervals(BytesRef term, SearchExecutionContext con } @Override - public IntervalsSource fuzzyIntervals(String term, int maxDistance, int prefixLength, - boolean transpositions, SearchExecutionContext context) { - FuzzyQuery fuzzyQuery = new FuzzyQuery(new Term(name(), term), - maxDistance, prefixLength, 128, transpositions); + public IntervalsSource fuzzyIntervals( + String term, + int maxDistance, + int prefixLength, + boolean transpositions, + SearchExecutionContext context + ) { + FuzzyQuery fuzzyQuery = new FuzzyQuery(new Term(name(), term), maxDistance, prefixLength, 128, transpositions); fuzzyQuery.setRewriteMethod(MultiTermQuery.CONSTANT_SCORE_REWRITE); IntervalsSource fuzzyIntervals = Intervals.multiterm(fuzzyQuery.getAutomata(), term); return toIntervalsSource(fuzzyIntervals, fuzzyQuery, context); @@ -243,7 +245,8 @@ public IntervalsSource wildcardIntervals(BytesRef pattern, SearchExecutionContex return toIntervalsSource( Intervals.wildcard(pattern), new MatchAllDocsQuery(), // wildcard queries can be expensive, what should the approximation be? - context); + context + ); } @Override diff --git a/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/RankFeatureFieldMapper.java b/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/RankFeatureFieldMapper.java index 9661fbd2e6bf9..f86c4fdd6a9c2 100644 --- a/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/RankFeatureFieldMapper.java +++ b/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/RankFeatureFieldMapper.java @@ -15,7 +15,6 @@ import org.apache.lucene.search.Query; import org.apache.lucene.search.TermQuery; import org.elasticsearch.common.lucene.Lucene; -import org.elasticsearch.xcontent.XContentParser.Token; import org.elasticsearch.index.fielddata.IndexFieldData; import org.elasticsearch.index.mapper.DocumentParserContext; import org.elasticsearch.index.mapper.FieldMapper; @@ -26,6 +25,7 @@ import org.elasticsearch.index.mapper.ValueFetcher; import org.elasticsearch.index.query.SearchExecutionContext; import org.elasticsearch.search.lookup.SearchLookup; +import org.elasticsearch.xcontent.XContentParser.Token; import java.io.IOException; import java.util.Arrays; @@ -52,13 +52,17 @@ public static class Defaults { } private static RankFeatureFieldType ft(FieldMapper in) { - return ((RankFeatureFieldMapper)in).fieldType(); + return ((RankFeatureFieldMapper) in).fieldType(); } public static class Builder extends FieldMapper.Builder { - private final Parameter positiveScoreImpact - = Parameter.boolParam("positive_score_impact", false, m -> ft(m).positiveScoreImpact, true); + private final Parameter positiveScoreImpact = Parameter.boolParam( + "positive_score_impact", + false, + m -> ft(m).positiveScoreImpact, + true + ); private final Parameter> meta = Parameter.metaParam(); public Builder(String name) { @@ -72,9 +76,13 @@ protected List> getParameters() { @Override public RankFeatureFieldMapper build(MapperBuilderContext context) { - return new RankFeatureFieldMapper(name, + return new RankFeatureFieldMapper( + name, new RankFeatureFieldType(context.buildFullName(name), meta.getValue(), positiveScoreImpact.getValue()), - multiFieldsBuilder.build(this, context), copyTo.build(), positiveScoreImpact.getValue()); + multiFieldsBuilder.build(this, context), + copyTo.build(), + positiveScoreImpact.getValue() + ); } } @@ -129,8 +137,13 @@ public Query termQuery(Object value, SearchExecutionContext context) { private final boolean positiveScoreImpact; - private RankFeatureFieldMapper(String simpleName, MappedFieldType mappedFieldType, - MultiFields multiFields, CopyTo copyTo, boolean positiveScoreImpact) { + private RankFeatureFieldMapper( + String simpleName, + MappedFieldType mappedFieldType, + MultiFields multiFields, + CopyTo copyTo, + boolean positiveScoreImpact + ) { super(simpleName, mappedFieldType, Lucene.KEYWORD_ANALYZER, multiFields, copyTo); this.positiveScoreImpact = positiveScoreImpact; } @@ -151,8 +164,9 @@ protected void parseCreateField(DocumentParserContext context) throws IOExceptio } if (context.doc().getByKey(name()) != null) { - throw new IllegalArgumentException("[rank_feature] fields do not support indexing multiple values for the same field [" + - name() + "] in the same document"); + throw new IllegalArgumentException( + "[rank_feature] fields do not support indexing multiple values for the same field [" + name() + "] in the same document" + ); } if (positiveScoreImpact == false) { diff --git a/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/RankFeatureQueryBuilder.java b/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/RankFeatureQueryBuilder.java index bfab8a71ec9cf..43c955ce3cafa 100644 --- a/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/RankFeatureQueryBuilder.java +++ b/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/RankFeatureQueryBuilder.java @@ -11,17 +11,17 @@ import org.apache.lucene.document.FeatureField; import org.apache.lucene.search.MatchNoDocsQuery; import org.apache.lucene.search.Query; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.xcontent.ConstructingObjectParser; -import org.elasticsearch.xcontent.ObjectParser; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.extras.RankFeatureFieldMapper.RankFeatureFieldType; import org.elasticsearch.index.mapper.extras.RankFeaturesFieldMapper.RankFeaturesFieldType; import org.elasticsearch.index.query.AbstractQueryBuilder; import org.elasticsearch.index.query.SearchExecutionContext; +import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.Arrays; @@ -52,7 +52,9 @@ private ScoreFunction() {} // prevent extensions by users public static class Log extends ScoreFunction { private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "log", a -> new Log((Float) a[0])); + "log", + a -> new Log((Float) a[0]) + ); static { PARSER.declareFloat(ConstructingObjectParser.constructorArg(), new ParseField("scaling_factor")); } @@ -97,8 +99,10 @@ void doXContent(XContentBuilder builder) throws IOException { @Override Query toQuery(String field, String feature, boolean positiveScoreImpact) { if (positiveScoreImpact == false) { - throw new IllegalArgumentException("Cannot use the [log] function with a field that has a negative score impact as " + - "it would trigger negative scores"); + throw new IllegalArgumentException( + "Cannot use the [log] function with a field that has a negative score impact as " + + "it would trigger negative scores" + ); } return FeatureField.newLogQuery(field, feature, DEFAULT_BOOST, scalingFactor); } @@ -111,7 +115,9 @@ Query toQuery(String field, String feature, boolean positiveScoreImpact) { public static class Saturation extends ScoreFunction { private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "saturation", a -> new Saturation((Float) a[0])); + "saturation", + a -> new Saturation((Float) a[0]) + ); static { PARSER.declareFloat(ConstructingObjectParser.optionalConstructorArg(), new ParseField("pivot")); } @@ -182,7 +188,9 @@ Query toQuery(String field, String feature, boolean positiveScoreImpact) { public static class Sigmoid extends ScoreFunction { private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "sigmoid", a -> new Sigmoid((Float) a[0], ((Float) a[1]).floatValue())); + "sigmoid", + a -> new Sigmoid((Float) a[0], ((Float) a[1]).floatValue()) + ); static { PARSER.declareFloat(ConstructingObjectParser.constructorArg(), new ParseField("pivot")); PARSER.declareFloat(ConstructingObjectParser.constructorArg(), new ParseField("exponent")); @@ -206,8 +214,7 @@ public boolean equals(Object obj) { return false; } Sigmoid that = (Sigmoid) obj; - return pivot == that.pivot - && exp == that.exp; + return pivot == that.pivot && exp == that.exp; } @Override @@ -244,8 +251,7 @@ public static class Linear extends ScoreFunction { private static final ObjectParser PARSER = new ObjectParser<>("linear", Linear::new); - public Linear() { - } + public Linear() {} private Linear(StreamInput in) { this(); @@ -285,53 +291,49 @@ Query toQuery(String field, String feature, boolean positiveScoreImpact) { private static ScoreFunction readScoreFunction(StreamInput in) throws IOException { byte b = in.readByte(); switch (b) { - case 0: - return new ScoreFunction.Log(in); - case 1: - return new ScoreFunction.Saturation(in); - case 2: - return new ScoreFunction.Sigmoid(in); - case 3: - return new ScoreFunction.Linear(in); - default: - throw new IOException("Illegal score function id: " + b); + case 0: + return new ScoreFunction.Log(in); + case 1: + return new ScoreFunction.Saturation(in); + case 2: + return new ScoreFunction.Sigmoid(in); + case 3: + return new ScoreFunction.Linear(in); + default: + throw new IOException("Illegal score function id: " + b); } } - public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "feature", args -> { - final String field = (String) args[0]; - final float boost = args[1] == null ? DEFAULT_BOOST : (Float) args[1]; - final String queryName = (String) args[2]; - long numNonNulls = Arrays.stream(args, 3, args.length).filter(Objects::nonNull).count(); - final RankFeatureQueryBuilder query; - if (numNonNulls > 1) { - throw new IllegalArgumentException("Can only specify one of [log], [saturation], [sigmoid] and [linear]"); - } else if (numNonNulls == 0) { - query = new RankFeatureQueryBuilder(field, new ScoreFunction.Saturation()); - } else { - ScoreFunction scoreFunction = (ScoreFunction) Arrays.stream(args, 3, args.length) - .filter(Objects::nonNull) - .findAny() - .get(); - query = new RankFeatureQueryBuilder(field, scoreFunction); - } - query.boost(boost); - query.queryName(queryName); - return query; - }); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("feature", args -> { + final String field = (String) args[0]; + final float boost = args[1] == null ? DEFAULT_BOOST : (Float) args[1]; + final String queryName = (String) args[2]; + long numNonNulls = Arrays.stream(args, 3, args.length).filter(Objects::nonNull).count(); + final RankFeatureQueryBuilder query; + if (numNonNulls > 1) { + throw new IllegalArgumentException("Can only specify one of [log], [saturation], [sigmoid] and [linear]"); + } else if (numNonNulls == 0) { + query = new RankFeatureQueryBuilder(field, new ScoreFunction.Saturation()); + } else { + ScoreFunction scoreFunction = (ScoreFunction) Arrays.stream(args, 3, args.length).filter(Objects::nonNull).findAny().get(); + query = new RankFeatureQueryBuilder(field, scoreFunction); + } + query.boost(boost); + query.queryName(queryName); + return query; + }); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), new ParseField("field")); PARSER.declareFloat(ConstructingObjectParser.optionalConstructorArg(), BOOST_FIELD); PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), NAME_FIELD); - PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), - ScoreFunction.Log.PARSER, new ParseField("log")); - PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), - ScoreFunction.Saturation.PARSER, new ParseField("saturation")); - PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), - ScoreFunction.Sigmoid.PARSER, new ParseField("sigmoid")); - PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), - ScoreFunction.Linear.PARSER, new ParseField("linear")); + PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), ScoreFunction.Log.PARSER, new ParseField("log")); + PARSER.declareObject( + ConstructingObjectParser.optionalConstructorArg(), + ScoreFunction.Saturation.PARSER, + new ParseField("saturation") + ); + PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), ScoreFunction.Sigmoid.PARSER, new ParseField("sigmoid")); + PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), ScoreFunction.Linear.PARSER, new ParseField("linear")); } public static final String NAME = "rank_feature"; @@ -393,8 +395,12 @@ protected Query doToQuery(SearchExecutionContext context) throws IOException { } return new MatchNoDocsQuery(); // unmapped field } else { - throw new IllegalArgumentException("[rank_feature] query only works on [rank_feature] fields and " + - "features of [rank_features] fields, not [" + ft.typeName() + "]"); + throw new IllegalArgumentException( + "[rank_feature] query only works on [rank_feature] fields and " + + "features of [rank_features] fields, not [" + + ft.typeName() + + "]" + ); } } diff --git a/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/RankFeaturesFieldMapper.java b/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/RankFeaturesFieldMapper.java index 050d52faf407f..7ae4f0129f75e 100644 --- a/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/RankFeaturesFieldMapper.java +++ b/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/RankFeaturesFieldMapper.java @@ -11,7 +11,6 @@ import org.apache.lucene.document.FeatureField; import org.apache.lucene.search.Query; import org.elasticsearch.common.lucene.Lucene; -import org.elasticsearch.xcontent.XContentParser.Token; import org.elasticsearch.index.fielddata.IndexFieldData; import org.elasticsearch.index.mapper.DocumentParserContext; import org.elasticsearch.index.mapper.FieldMapper; @@ -22,6 +21,7 @@ import org.elasticsearch.index.mapper.ValueFetcher; import org.elasticsearch.index.query.SearchExecutionContext; import org.elasticsearch.search.lookup.SearchLookup; +import org.elasticsearch.xcontent.XContentParser.Token; import java.io.IOException; import java.util.List; @@ -37,13 +37,17 @@ public class RankFeaturesFieldMapper extends FieldMapper { public static final String CONTENT_TYPE = "rank_features"; private static RankFeaturesFieldType ft(FieldMapper in) { - return ((RankFeaturesFieldMapper)in).fieldType(); + return ((RankFeaturesFieldMapper) in).fieldType(); } public static class Builder extends FieldMapper.Builder { - private final Parameter positiveScoreImpact - = Parameter.boolParam("positive_score_impact", false, m -> ft(m).positiveScoreImpact, true); + private final Parameter positiveScoreImpact = Parameter.boolParam( + "positive_score_impact", + false, + m -> ft(m).positiveScoreImpact, + true + ); private final Parameter> meta = Parameter.metaParam(); public Builder(String name) { @@ -58,8 +62,12 @@ protected List> getParameters() { @Override public RankFeaturesFieldMapper build(MapperBuilderContext context) { return new RankFeaturesFieldMapper( - name, new RankFeaturesFieldType(context.buildFullName(name), meta.getValue(), positiveScoreImpact.getValue()), - multiFieldsBuilder.build(this, context), copyTo.build(), positiveScoreImpact.getValue()); + name, + new RankFeaturesFieldType(context.buildFullName(name), meta.getValue(), positiveScoreImpact.getValue()), + multiFieldsBuilder.build(this, context), + copyTo.build(), + positiveScoreImpact.getValue() + ); } } @@ -106,8 +114,13 @@ public Query termQuery(Object value, SearchExecutionContext context) { private final boolean positiveScoreImpact; - private RankFeaturesFieldMapper(String simpleName, MappedFieldType mappedFieldType, - MultiFields multiFields, CopyTo copyTo, boolean positiveScoreImpact) { + private RankFeaturesFieldMapper( + String simpleName, + MappedFieldType mappedFieldType, + MultiFields multiFields, + CopyTo copyTo, + boolean positiveScoreImpact + ) { super(simpleName, mappedFieldType, Lucene.KEYWORD_ANALYZER, multiFields, copyTo); this.positiveScoreImpact = positiveScoreImpact; } @@ -126,8 +139,9 @@ public RankFeaturesFieldType fieldType() { public void parse(DocumentParserContext context) throws IOException { if (context.parser().currentToken() != Token.START_OBJECT) { - throw new IllegalArgumentException("[rank_features] fields must be json objects, expected a START_OBJECT but got: " + - context.parser().currentToken()); + throw new IllegalArgumentException( + "[rank_features] fields must be json objects, expected a START_OBJECT but got: " + context.parser().currentToken() + ); } String feature = null; @@ -140,16 +154,23 @@ public void parse(DocumentParserContext context) throws IOException { final String key = name() + "." + feature; float value = context.parser().floatValue(true); if (context.doc().getByKey(key) != null) { - throw new IllegalArgumentException("[rank_features] fields do not support indexing multiple values for the same " + - "rank feature [" + key + "] in the same document"); + throw new IllegalArgumentException( + "[rank_features] fields do not support indexing multiple values for the same " + + "rank feature [" + + key + + "] in the same document" + ); } if (positiveScoreImpact == false) { value = 1 / value; } context.doc().addWithKey(key, new FeatureField(name(), feature, value)); } else { - throw new IllegalArgumentException("[rank_features] fields take hashes that map a feature to a strictly positive " + - "float, but got unexpected token " + token); + throw new IllegalArgumentException( + "[rank_features] fields take hashes that map a feature to a strictly positive " + + "float, but got unexpected token " + + token + ); } } } diff --git a/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/ScaledFloatFieldMapper.java b/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/ScaledFloatFieldMapper.java index dc7575728fa86..9e2a970d13e15 100644 --- a/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/ScaledFloatFieldMapper.java +++ b/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/ScaledFloatFieldMapper.java @@ -18,10 +18,6 @@ import org.elasticsearch.common.Explicit; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.script.field.DelegateDocValuesField; -import org.elasticsearch.script.field.DocValuesField; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentParser.Token; import org.elasticsearch.common.xcontent.support.XContentMapValues; import org.elasticsearch.index.fielddata.FieldData; import org.elasticsearch.index.fielddata.FormattedDocValues; @@ -43,9 +39,13 @@ import org.elasticsearch.index.mapper.TimeSeriesParams; import org.elasticsearch.index.mapper.ValueFetcher; import org.elasticsearch.index.query.SearchExecutionContext; +import org.elasticsearch.script.field.DelegateDocValuesField; +import org.elasticsearch.script.field.DocValuesField; import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.support.ValuesSourceType; import org.elasticsearch.search.lookup.SearchLookup; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentParser.Token; import java.io.IOException; import java.math.BigDecimal; @@ -79,18 +79,27 @@ public static class Builder extends FieldMapper.Builder { private final Parameter> ignoreMalformed; private final Parameter> coerce; - private final Parameter scalingFactor = new Parameter<>("scaling_factor", false, () -> null, - (n, c, o) -> XContentMapValues.nodeDoubleValue(o), m -> toType(m).scalingFactor) - .addValidator(v -> { - if (v == null) { - throw new IllegalArgumentException("Field [scaling_factor] is required"); - } - if (Double.isFinite(v) == false || v <= 0) { - throw new IllegalArgumentException("[scaling_factor] must be a positive number, got [" + v + "]"); - } - }); - private final Parameter nullValue = new Parameter<>("null_value", false, () -> null, - (n, c, o) -> o == null ? null : XContentMapValues.nodeDoubleValue(o), m -> toType(m).nullValue).acceptsNull(); + private final Parameter scalingFactor = new Parameter<>( + "scaling_factor", + false, + () -> null, + (n, c, o) -> XContentMapValues.nodeDoubleValue(o), + m -> toType(m).scalingFactor + ).addValidator(v -> { + if (v == null) { + throw new IllegalArgumentException("Field [scaling_factor] is required"); + } + if (Double.isFinite(v) == false || v <= 0) { + throw new IllegalArgumentException("[scaling_factor] must be a positive number, got [" + v + "]"); + } + }); + private final Parameter nullValue = new Parameter<>( + "null_value", + false, + () -> null, + (n, c, o) -> o == null ? null : XContentMapValues.nodeDoubleValue(o), + m -> toType(m).nullValue + ).acceptsNull(); private final Parameter> meta = Parameter.metaParam(); @@ -107,10 +116,13 @@ public Builder(String name, Settings settings) { public Builder(String name, boolean ignoreMalformedByDefault, boolean coerceByDefault) { super(name); - this.ignoreMalformed - = Parameter.explicitBoolParam("ignore_malformed", true, m -> toType(m).ignoreMalformed, ignoreMalformedByDefault); - this.coerce - = Parameter.explicitBoolParam("coerce", true, m -> toType(m).coerce, coerceByDefault); + this.ignoreMalformed = Parameter.explicitBoolParam( + "ignore_malformed", + true, + m -> toType(m).ignoreMalformed, + ignoreMalformedByDefault + ); + this.coerce = Parameter.explicitBoolParam("coerce", true, m -> toType(m).coerce, coerceByDefault); this.metric = TimeSeriesParams.metricParam( m -> toType(m).metricType, @@ -169,10 +181,16 @@ public static final class ScaledFloatFieldType extends SimpleMappedFieldType { private final Double nullValue; private final TimeSeriesParams.MetricType metricType; - - public ScaledFloatFieldType(String name, boolean indexed, boolean stored, boolean hasDocValues, - Map meta, double scalingFactor, Double nullValue, - TimeSeriesParams.MetricType metricType) { + public ScaledFloatFieldType( + String name, + boolean indexed, + boolean stored, + boolean hasDocValues, + Map meta, + double scalingFactor, + Double nullValue, + TimeSeriesParams.MetricType metricType + ) { super(name, indexed, stored, hasDocValues, TextSearchInfo.SIMPLE_MATCH_WITHOUT_TERMS, meta); this.scalingFactor = scalingFactor; this.nullValue = nullValue; @@ -211,9 +229,13 @@ public Query termsQuery(Collection values, SearchExecutionContext context) { } @Override - public Query rangeQuery(Object lowerTerm, Object upperTerm, - boolean includeLower, boolean includeUpper, - SearchExecutionContext context) { + public Query rangeQuery( + Object lowerTerm, + Object upperTerm, + boolean includeLower, + boolean includeUpper, + SearchExecutionContext context + ) { failIfNotIndexed(); Long lo = null; if (lowerTerm != null) { @@ -322,11 +344,12 @@ public TimeSeriesParams.MetricType getMetricType() { private final TimeSeriesParams.MetricType metricType; private ScaledFloatFieldMapper( - String simpleName, - ScaledFloatFieldType mappedFieldType, - MultiFields multiFields, - CopyTo copyTo, - Builder builder) { + String simpleName, + ScaledFloatFieldType mappedFieldType, + MultiFields multiFields, + CopyTo copyTo, + Builder builder + ) { super(simpleName, mappedFieldType, multiFields, copyTo); this.indexed = builder.indexed.getValue(); this.hasDocValues = builder.hasDocValues.getValue(); @@ -370,9 +393,7 @@ protected void parseCreateField(DocumentParserContext context) throws IOExceptio Number numericValue = null; if (parser.currentToken() == Token.VALUE_NULL) { value = null; - } else if (coerce.value() - && parser.currentToken() == Token.VALUE_STRING - && parser.textLength() == 0) { + } else if (coerce.value() && parser.currentToken() == Token.VALUE_STRING && parser.textLength() == 0) { value = null; } else { try { @@ -410,8 +431,7 @@ protected void parseCreateField(DocumentParserContext context) throws IOExceptio } long scaledValue = Math.round(doubleValue * scalingFactor); - List fields - = NumberFieldMapper.NumberType.LONG.createFields(fieldType().name(), scaledValue, indexed, hasDocValues, stored); + List fields = NumberFieldMapper.NumberType.LONG.createFields(fieldType().name(), scaledValue, indexed, hasDocValues, stored); context.doc().addAll(fields); if (hasDocValues == false && (indexed || stored)) { @@ -541,6 +561,7 @@ public SortedNumericDoubleValues getDoubleValues() { public boolean advanceExact(int doc) throws IOException { return singleValues.advanceExact(doc); } + @Override public double doubleValue() throws IOException { return singleValues.longValue() * scalingFactorInverse; diff --git a/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/SearchAsYouTypeFieldMapper.java b/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/SearchAsYouTypeFieldMapper.java index 3bca06abc2e59..1cea384e7fb08 100644 --- a/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/SearchAsYouTypeFieldMapper.java +++ b/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/SearchAsYouTypeFieldMapper.java @@ -92,11 +92,10 @@ public static class Defaults { public static final int MAX_SHINGLE_SIZE = 3; } - public static final TypeParser PARSER - = new TypeParser((n, c) -> new Builder(n, c.getIndexAnalyzers())); + public static final TypeParser PARSER = new TypeParser((n, c) -> new Builder(n, c.getIndexAnalyzers())); private static Builder builder(FieldMapper in) { - return ((SearchAsYouTypeFieldMapper)in).builder; + return ((SearchAsYouTypeFieldMapper) in).builder; } public static class Builder extends FieldMapper.Builder { @@ -107,23 +106,31 @@ public static class Builder extends FieldMapper.Builder { // This is only here because for some reason the initial impl of this always serialized // `doc_values=false`, even though it cannot be set; and so we need to continue // serializing it forever because of mapper assertions in mixed clusters. - private final Parameter docValues = Parameter.docValuesParam(m -> false, false) - .addValidator(v -> { - if (v) { - throw new MapperParsingException("Cannot set [doc_values] on field of type [search_as_you_type]"); - } - }) - .alwaysSerialize(); - - private final Parameter maxShingleSize = Parameter.intParam("max_shingle_size", false, - m -> builder(m).maxShingleSize.get(), Defaults.MAX_SHINGLE_SIZE) - .addValidator(v -> { - if (v < MAX_SHINGLE_SIZE_LOWER_BOUND || v > MAX_SHINGLE_SIZE_UPPER_BOUND) { - throw new MapperParsingException("[max_shingle_size] must be at least [" + MAX_SHINGLE_SIZE_LOWER_BOUND - + "] and at most " + "[" + MAX_SHINGLE_SIZE_UPPER_BOUND + "], got [" + v + "]"); - } - }) - .alwaysSerialize(); + private final Parameter docValues = Parameter.docValuesParam(m -> false, false).addValidator(v -> { + if (v) { + throw new MapperParsingException("Cannot set [doc_values] on field of type [search_as_you_type]"); + } + }).alwaysSerialize(); + + private final Parameter maxShingleSize = Parameter.intParam( + "max_shingle_size", + false, + m -> builder(m).maxShingleSize.get(), + Defaults.MAX_SHINGLE_SIZE + ).addValidator(v -> { + if (v < MAX_SHINGLE_SIZE_LOWER_BOUND || v > MAX_SHINGLE_SIZE_UPPER_BOUND) { + throw new MapperParsingException( + "[max_shingle_size] must be at least [" + + MAX_SHINGLE_SIZE_LOWER_BOUND + + "] and at most " + + "[" + + MAX_SHINGLE_SIZE_UPPER_BOUND + + "], got [" + + v + + "]" + ); + } + }).alwaysSerialize(); final TextParams.Analyzers analyzers; final Parameter similarity = TextParams.similarity(m -> builder(m).similarity.get()); @@ -137,17 +144,28 @@ public static class Builder extends FieldMapper.Builder { public Builder(String name, IndexAnalyzers indexAnalyzers) { super(name); this.analyzers = new TextParams.Analyzers( - indexAnalyzers, - m -> builder(m).analyzers.getIndexAnalyzer(), - m -> builder(m).analyzers.positionIncrementGap.getValue() + indexAnalyzers, + m -> builder(m).analyzers.getIndexAnalyzer(), + m -> builder(m).analyzers.positionIncrementGap.getValue() ); } @Override protected List> getParameters() { - return List.of(index, store, docValues, maxShingleSize, - analyzers.indexAnalyzer, analyzers.searchAnalyzer, analyzers.searchQuoteAnalyzer, similarity, - indexOptions, norms, termVectors, meta); + return List.of( + index, + store, + docValues, + maxShingleSize, + analyzers.indexAnalyzer, + analyzers.searchAnalyzer, + analyzers.searchQuoteAnalyzer, + similarity, + indexOptions, + norms, + termVectors, + meta + ); } @Override @@ -164,8 +182,14 @@ public SearchAsYouTypeFieldMapper build(MapperBuilderContext context) { NamedAnalyzer indexAnalyzer = analyzers.getIndexAnalyzer(); NamedAnalyzer searchAnalyzer = analyzers.getSearchAnalyzer(); - SearchAsYouTypeFieldType ft = new SearchAsYouTypeFieldType(context.buildFullName(name), fieldType, similarity.getValue(), - analyzers.getSearchAnalyzer(), analyzers.getSearchQuoteAnalyzer(), meta.getValue()); + SearchAsYouTypeFieldType ft = new SearchAsYouTypeFieldType( + context.buildFullName(name), + fieldType, + similarity.getValue(), + analyzers.getSearchAnalyzer(), + analyzers.getSearchQuoteAnalyzer(), + meta.getValue() + ); indexAnalyzers.put(ft.name(), indexAnalyzer); @@ -176,11 +200,16 @@ public SearchAsYouTypeFieldMapper build(MapperBuilderContext context) { prefixft.setStored(false); final String fullName = context.buildFullName(name); // wrap the root field's index analyzer with shingles and edge ngrams - final Analyzer prefixIndexWrapper = - SearchAsYouTypeAnalyzer.withShingleAndPrefix(indexAnalyzer.analyzer(), maxShingleSize.getValue()); + final Analyzer prefixIndexWrapper = SearchAsYouTypeAnalyzer.withShingleAndPrefix( + indexAnalyzer.analyzer(), + maxShingleSize.getValue() + ); // wrap the root field's search analyzer with only shingles - final NamedAnalyzer prefixSearchWrapper = new NamedAnalyzer(searchAnalyzer.name(), searchAnalyzer.scope(), - SearchAsYouTypeAnalyzer.withShingle(searchAnalyzer.analyzer(), maxShingleSize.getValue())); + final NamedAnalyzer prefixSearchWrapper = new NamedAnalyzer( + searchAnalyzer.name(), + searchAnalyzer.scope(), + SearchAsYouTypeAnalyzer.withShingle(searchAnalyzer.analyzer(), maxShingleSize.getValue()) + ); // don't wrap the root field's search quote analyzer as prefix field doesn't support phrase queries TextSearchInfo prefixSearchInfo = new TextSearchInfo(prefixft, similarity.getValue(), prefixSearchWrapper, searchAnalyzer); final PrefixFieldType prefixFieldType = new PrefixFieldType(fullName, prefixSearchInfo, Defaults.MIN_GRAM, Defaults.MAX_GRAM); @@ -197,14 +226,26 @@ public SearchAsYouTypeFieldMapper build(MapperBuilderContext context) { shingleft.setStored(false); String fieldName = getShingleFieldName(context.buildFullName(name), shingleSize); // wrap the root field's index, search, and search quote analyzers with shingles - final SearchAsYouTypeAnalyzer shingleIndexWrapper = - SearchAsYouTypeAnalyzer.withShingle(indexAnalyzer.analyzer(), shingleSize); - final NamedAnalyzer shingleSearchWrapper = new NamedAnalyzer(searchAnalyzer.name(), searchAnalyzer.scope(), - SearchAsYouTypeAnalyzer.withShingle(searchAnalyzer.analyzer(), shingleSize)); - final NamedAnalyzer shingleSearchQuoteWrapper = new NamedAnalyzer(searchAnalyzer.name(), searchAnalyzer.scope(), - SearchAsYouTypeAnalyzer.withShingle(searchAnalyzer.analyzer(), shingleSize)); - TextSearchInfo textSearchInfo - = new TextSearchInfo(shingleft, similarity.getValue(), shingleSearchWrapper, shingleSearchQuoteWrapper); + final SearchAsYouTypeAnalyzer shingleIndexWrapper = SearchAsYouTypeAnalyzer.withShingle( + indexAnalyzer.analyzer(), + shingleSize + ); + final NamedAnalyzer shingleSearchWrapper = new NamedAnalyzer( + searchAnalyzer.name(), + searchAnalyzer.scope(), + SearchAsYouTypeAnalyzer.withShingle(searchAnalyzer.analyzer(), shingleSize) + ); + final NamedAnalyzer shingleSearchQuoteWrapper = new NamedAnalyzer( + searchAnalyzer.name(), + searchAnalyzer.scope(), + SearchAsYouTypeAnalyzer.withShingle(searchAnalyzer.analyzer(), shingleSize) + ); + TextSearchInfo textSearchInfo = new TextSearchInfo( + shingleft, + similarity.getValue(), + shingleSearchWrapper, + shingleSearchQuoteWrapper + ); final ShingleFieldType shingleFieldType = new ShingleFieldType(fieldName, shingleSize, textSearchInfo); shingleFieldType.setPrefixFieldType(prefixFieldType); shingleFieldTypes[i] = shingleFieldType; @@ -242,10 +283,22 @@ static class SearchAsYouTypeFieldType extends StringFieldType { PrefixFieldType prefixField; ShingleFieldType[] shingleFields = new ShingleFieldType[0]; - SearchAsYouTypeFieldType(String name, FieldType fieldType, SimilarityProvider similarity, - NamedAnalyzer searchAnalyzer, NamedAnalyzer searchQuoteAnalyzer, Map meta) { - super(name, fieldType.indexOptions() != IndexOptions.NONE, fieldType.stored(), false, - new TextSearchInfo(fieldType, similarity, searchAnalyzer, searchQuoteAnalyzer), meta); + SearchAsYouTypeFieldType( + String name, + FieldType fieldType, + SimilarityProvider similarity, + NamedAnalyzer searchAnalyzer, + NamedAnalyzer searchQuoteAnalyzer, + Map meta + ) { + super( + name, + fieldType.indexOptions() != IndexOptions.NONE, + fieldType.stored(), + false, + new TextSearchInfo(fieldType, similarity, searchAnalyzer, searchQuoteAnalyzer), + meta + ); this.fieldType = fieldType; } @@ -273,8 +326,12 @@ public ValueFetcher valueFetcher(SearchExecutionContext context, String format) } @Override - public Query prefixQuery(String value, MultiTermQuery.RewriteMethod method, boolean caseInsensitive, - SearchExecutionContext context) { + public Query prefixQuery( + String value, + MultiTermQuery.RewriteMethod method, + boolean caseInsensitive, + SearchExecutionContext context + ) { if (prefixField == null || prefixField.termLengthWithinBounds(value.length()) == false) { return super.prefixQuery(value, method, caseInsensitive, context); } else { @@ -296,8 +353,8 @@ private void checkForPositions() { } @Override - public Query phraseQuery(TokenStream stream, int slop, boolean enablePositionIncrements, - SearchExecutionContext context) throws IOException { + public Query phraseQuery(TokenStream stream, int slop, boolean enablePositionIncrements, SearchExecutionContext context) + throws IOException { checkForPositions(); int numPos = countPosition(stream); if (shingleFields.length == 0 || slop > 0 || hasGaps(stream) || numPos <= 1) { @@ -309,8 +366,8 @@ public Query phraseQuery(TokenStream stream, int slop, boolean enablePositionInc } @Override - public Query multiPhraseQuery(TokenStream stream, int slop, boolean enablePositionIncrements, - SearchExecutionContext context) throws IOException { + public Query multiPhraseQuery(TokenStream stream, int slop, boolean enablePositionIncrements, SearchExecutionContext context) + throws IOException { checkForPositions(); int numPos = countPosition(stream); if (shingleFields.length == 0 || slop > 0 || hasGaps(stream) || numPos <= 1) { @@ -322,15 +379,13 @@ public Query multiPhraseQuery(TokenStream stream, int slop, boolean enablePositi } @Override - public Query phrasePrefixQuery(TokenStream stream, int slop, int maxExpansions, - SearchExecutionContext context) throws IOException { + public Query phrasePrefixQuery(TokenStream stream, int slop, int maxExpansions, SearchExecutionContext context) throws IOException { int numPos = countPosition(stream); if (numPos > 1) { checkForPositions(); } if (shingleFields.length == 0 || slop > 0 || hasGaps(stream) || numPos <= 1) { - return TextFieldMapper.createPhrasePrefixQuery(stream, name(), slop, maxExpansions, - null, null); + return TextFieldMapper.createPhrasePrefixQuery(stream, name(), slop, maxExpansions, null, null); } final ShingleFieldType shingleField = shingleFieldForPositions(numPos); stream = new FixedShingleFilter(stream, shingleField.shingleSize); @@ -342,8 +397,9 @@ public SpanQuery spanPrefixQuery(String value, SpanMultiTermQueryWrapper.SpanRew if (prefixField != null && prefixField.termLengthWithinBounds(value.length())) { return new FieldMaskingSpanQuery(new SpanTermQuery(new Term(prefixField.name(), indexedValueForSearch(value))), name()); } else { - SpanMultiTermQueryWrapper spanMulti = - new SpanMultiTermQueryWrapper<>(new PrefixQuery(new Term(name(), indexedValueForSearch(value)))); + SpanMultiTermQueryWrapper spanMulti = new SpanMultiTermQueryWrapper<>( + new PrefixQuery(new Term(name(), indexedValueForSearch(value))) + ); spanMulti.setRewriteMethod(method); return spanMulti; } @@ -372,10 +428,14 @@ boolean termLengthWithinBounds(int length) { } @Override - public Query prefixQuery(String value, MultiTermQuery.RewriteMethod method, boolean caseInsensitive, - SearchExecutionContext context) { + public Query prefixQuery( + String value, + MultiTermQuery.RewriteMethod method, + boolean caseInsensitive, + SearchExecutionContext context + ) { if (value.length() >= minChars) { - if(caseInsensitive) { + if (caseInsensitive) { return super.termQueryCaseInsensitive(value, context); } return super.termQuery(value, context); @@ -388,8 +448,7 @@ public Query prefixQuery(String value, MultiTermQuery.RewriteMethod method, bool Automaton automaton = Operations.concatenate(automata); AutomatonQuery query = new AutomatonQuery(new Term(name(), value + "*"), automaton); query.setRewriteMethod(method); - return new BooleanQuery.Builder() - .add(query, BooleanClause.Occur.SHOULD) + return new BooleanQuery.Builder().add(query, BooleanClause.Occur.SHOULD) .add(new TermQuery(new Term(parentField, value)), BooleanClause.Occur.SHOULD) .build(); } @@ -514,8 +573,12 @@ public String typeName() { } @Override - public Query prefixQuery(String value, MultiTermQuery.RewriteMethod method, boolean caseInsensitive, - SearchExecutionContext context) { + public Query prefixQuery( + String value, + MultiTermQuery.RewriteMethod method, + boolean caseInsensitive, + SearchExecutionContext context + ) { if (prefixFieldType == null || prefixFieldType.termLengthWithinBounds(value.length()) == false) { return super.prefixQuery(value, method, caseInsensitive, context); } else { @@ -531,25 +594,28 @@ public Query prefixQuery(String value, MultiTermQuery.RewriteMethod method, bool } @Override - public Query phraseQuery(TokenStream stream, int slop, boolean enablePositionIncrements, - SearchExecutionContext context) throws IOException { + public Query phraseQuery(TokenStream stream, int slop, boolean enablePositionIncrements, SearchExecutionContext context) + throws IOException { return TextFieldMapper.createPhraseQuery(stream, name(), slop, enablePositionIncrements); } @Override - public Query multiPhraseQuery(TokenStream stream, int slop, boolean enablePositionIncrements, - SearchExecutionContext context) throws IOException { + public Query multiPhraseQuery(TokenStream stream, int slop, boolean enablePositionIncrements, SearchExecutionContext context) + throws IOException { return TextFieldMapper.createPhraseQuery(stream, name(), slop, enablePositionIncrements); } @Override - public Query phrasePrefixQuery(TokenStream stream, int slop, int maxExpansions, - SearchExecutionContext context) throws IOException { - final String prefixFieldName = slop > 0 - ? null - : prefixFieldType.name(); - return TextFieldMapper.createPhrasePrefixQuery(stream, name(), slop, maxExpansions, - prefixFieldName, prefixFieldType::termLengthWithinBounds); + public Query phrasePrefixQuery(TokenStream stream, int slop, int maxExpansions, SearchExecutionContext context) throws IOException { + final String prefixFieldName = slop > 0 ? null : prefixFieldType.name(); + return TextFieldMapper.createPhrasePrefixQuery( + stream, + name(), + slop, + maxExpansions, + prefixFieldName, + prefixFieldType::termLengthWithinBounds + ); } @Override @@ -557,8 +623,9 @@ public SpanQuery spanPrefixQuery(String value, SpanMultiTermQueryWrapper.SpanRew if (prefixFieldType != null && prefixFieldType.termLengthWithinBounds(value.length())) { return new FieldMaskingSpanQuery(new SpanTermQuery(new Term(prefixFieldType.name(), indexedValueForSearch(value))), name()); } else { - SpanMultiTermQueryWrapper spanMulti = - new SpanMultiTermQueryWrapper<>(new PrefixQuery(new Term(name(), indexedValueForSearch(value)))); + SpanMultiTermQueryWrapper spanMulti = new SpanMultiTermQueryWrapper<>( + new PrefixQuery(new Term(name(), indexedValueForSearch(value))) + ); spanMulti.setRewriteMethod(method); return spanMulti; } @@ -570,13 +637,15 @@ public SpanQuery spanPrefixQuery(String value, SpanMultiTermQueryWrapper.SpanRew private final ShingleFieldMapper[] shingleFields; private final Builder builder; - public SearchAsYouTypeFieldMapper(String simpleName, - SearchAsYouTypeFieldType mappedFieldType, - CopyTo copyTo, - Map indexAnalyzers, - PrefixFieldMapper prefixField, - ShingleFieldMapper[] shingleFields, - Builder builder) { + public SearchAsYouTypeFieldMapper( + String simpleName, + SearchAsYouTypeFieldType mappedFieldType, + CopyTo copyTo, + Map indexAnalyzers, + PrefixFieldMapper prefixField, + ShingleFieldMapper[] shingleFields, + Builder builder + ) { super(simpleName, mappedFieldType, indexAnalyzers, MultiFields.empty(), copyTo, false, null); this.prefixField = prefixField; this.shingleFields = shingleFields; @@ -642,7 +711,8 @@ public Iterator iterator() { List subIterators = new ArrayList<>(); subIterators.add(prefixField); subIterators.addAll(Arrays.asList(shingleFields)); - @SuppressWarnings("unchecked") Iterator concat = Iterators.concat(super.iterator(), subIterators.iterator()); + @SuppressWarnings("unchecked") + Iterator concat = Iterators.concat(super.iterator(), subIterators.iterator()); return concat; } @@ -657,9 +727,7 @@ static class SearchAsYouTypeAnalyzer extends AnalyzerWrapper { private final int shingleSize; private final boolean indexPrefixes; - private SearchAsYouTypeAnalyzer(Analyzer delegate, - int shingleSize, - boolean indexPrefixes) { + private SearchAsYouTypeAnalyzer(Analyzer delegate, int shingleSize, boolean indexPrefixes) { super(delegate.getReuseStrategy()); this.delegate = Objects.requireNonNull(delegate); diff --git a/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/SourceIntervalsSource.java b/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/SourceIntervalsSource.java index 88adbcfe34570..7e46f998066bf 100644 --- a/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/SourceIntervalsSource.java +++ b/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/SourceIntervalsSource.java @@ -40,10 +40,12 @@ public final class SourceIntervalsSource extends IntervalsSource { private final Function, IOException>> valueFetcherProvider; private final Analyzer indexAnalyzer; - public SourceIntervalsSource(IntervalsSource in, - Query approximation, - Function, IOException>> valueFetcherProvider, - Analyzer indexAnalyzer) { + public SourceIntervalsSource( + IntervalsSource in, + Query approximation, + Function, IOException>> valueFetcherProvider, + Analyzer indexAnalyzer + ) { this.in = Objects.requireNonNull(in); this.approximation = Objects.requireNonNull(approximation); this.valueFetcherProvider = Objects.requireNonNull(valueFetcherProvider); diff --git a/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/TokenCountFieldMapper.java b/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/TokenCountFieldMapper.java index 6ec62adac0673..bfa230a926519 100644 --- a/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/TokenCountFieldMapper.java +++ b/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/TokenCountFieldMapper.java @@ -46,13 +46,20 @@ public static class Builder extends FieldMapper.Builder { private final Parameter hasDocValues = Parameter.docValuesParam(m -> toType(m).hasDocValues, true); private final Parameter store = Parameter.storeParam(m -> toType(m).store, false); - private final Parameter analyzer - = Parameter.analyzerParam("analyzer", true, m -> toType(m).analyzer, () -> null); + private final Parameter analyzer = Parameter.analyzerParam("analyzer", true, m -> toType(m).analyzer, () -> null); private final Parameter nullValue = new Parameter<>( - "null_value", false, () -> null, - (n, c, o) -> o == null ? null : nodeIntegerValue(o), m -> toType(m).nullValue).acceptsNull(); - private final Parameter enablePositionIncrements - = Parameter.boolParam("enable_position_increments", false, m -> toType(m).enablePositionIncrements, true); + "null_value", + false, + () -> null, + (n, c, o) -> o == null ? null : nodeIntegerValue(o), + m -> toType(m).nullValue + ).acceptsNull(); + private final Parameter enablePositionIncrements = Parameter.boolParam( + "enable_position_increments", + false, + m -> toType(m).enablePositionIncrements, + true + ); private final Parameter> meta = Parameter.metaParam(); @@ -76,7 +83,8 @@ public TokenCountFieldMapper build(MapperBuilderContext context) { store.getValue(), hasDocValues.getValue(), nullValue.getValue(), - meta.getValue()); + meta.getValue() + ); return new TokenCountFieldMapper(name, ft, multiFieldsBuilder.build(this, context), copyTo.build(), this); } } @@ -124,8 +132,13 @@ public ValueFetcher valueFetcher(SearchExecutionContext context, String format) private final boolean enablePositionIncrements; private final Integer nullValue; - protected TokenCountFieldMapper(String simpleName, MappedFieldType defaultFieldType, - MultiFields multiFields, CopyTo copyTo, Builder builder) { + protected TokenCountFieldMapper( + String simpleName, + MappedFieldType defaultFieldType, + MultiFields multiFields, + CopyTo copyTo, + Builder builder + ) { super(simpleName, defaultFieldType, multiFields, copyTo); this.analyzer = builder.analyzer.getValue(); this.enablePositionIncrements = builder.enablePositionIncrements.getValue(); @@ -150,9 +163,7 @@ protected void parseCreateField(DocumentParserContext context) throws IOExceptio tokenCount = countPositions(analyzer, name(), value, enablePositionIncrements); } - context.doc().addAll( - NumberFieldMapper.NumberType.INTEGER.createFields(fieldType().name(), tokenCount, index, hasDocValues, store) - ); + context.doc().addAll(NumberFieldMapper.NumberType.INTEGER.createFields(fieldType().name(), tokenCount, index, hasDocValues, store)); } /** diff --git a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/BWCTemplateTests.java b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/BWCTemplateTests.java index c3f0be862b1de..593f5e06bdd2a 100644 --- a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/BWCTemplateTests.java +++ b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/BWCTemplateTests.java @@ -8,9 +8,9 @@ package org.elasticsearch.index.mapper.extras; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.test.ESSingleNodeTestCase; +import org.elasticsearch.xcontent.XContentType; import java.util.Collection; diff --git a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/MatchOnlyTextFieldTypeTests.java b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/MatchOnlyTextFieldTypeTests.java index a5cd66bba5229..e0397f1577a5e 100644 --- a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/MatchOnlyTextFieldTypeTests.java +++ b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/MatchOnlyTextFieldTypeTests.java @@ -32,8 +32,6 @@ import org.elasticsearch.index.mapper.FieldTypeTestCase; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.extras.MatchOnlyTextFieldMapper.MatchOnlyTextFieldType; -import org.elasticsearch.index.mapper.extras.SourceConfirmedTextQuery; -import org.elasticsearch.index.mapper.extras.SourceIntervalsSource; import org.hamcrest.Matchers; import java.io.IOException; diff --git a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/RankFeatureFieldMapperTests.java b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/RankFeatureFieldMapperTests.java index 0f3cfe3186661..39e5c915bbd6a 100644 --- a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/RankFeatureFieldMapperTests.java +++ b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/RankFeatureFieldMapperTests.java @@ -15,15 +15,14 @@ import org.apache.lucene.search.Query; import org.apache.lucene.search.TermQuery; import org.elasticsearch.common.Strings; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.LuceneDocument; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.MapperParsingException; import org.elasticsearch.index.mapper.MapperTestCase; import org.elasticsearch.index.mapper.ParsedDocument; -import org.elasticsearch.index.mapper.extras.MapperExtrasPlugin; import org.elasticsearch.plugins.Plugin; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.Arrays; @@ -55,7 +54,7 @@ protected void assertExistsQuery(MappedFieldType fieldType, Query query, LuceneD @Override protected void assertSearchable(MappedFieldType fieldType) { - //always searchable even if it uses TextSearchInfo.NONE + // always searchable even if it uses TextSearchInfo.NONE assertTrue(fieldType.isSearchable()); } @@ -134,8 +133,10 @@ public void testRejectMultiValuedFields() throws MapperParsingException, IOExcep MapperParsingException.class, () -> mapper.parse(source(b -> b.field("field", Arrays.asList(10, 20)))) ); - assertEquals("[rank_feature] fields do not support indexing multiple values for the same field [field] in the same document", - e.getCause().getMessage()); + assertEquals( + "[rank_feature] fields do not support indexing multiple values for the same field [field] in the same document", + e.getCause().getMessage() + ); e = expectThrows(MapperParsingException.class, () -> mapper.parse(source(b -> { b.startArray("foo"); @@ -145,8 +146,10 @@ public void testRejectMultiValuedFields() throws MapperParsingException, IOExcep } b.endArray(); }))); - assertEquals("[rank_feature] fields do not support indexing multiple values for the same field [foo.field] in the same document", - e.getCause().getMessage()); + assertEquals( + "[rank_feature] fields do not support indexing multiple values for the same field [foo.field] in the same document", + e.getCause().getMessage() + ); } @Override diff --git a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/RankFeatureFieldTypeTests.java b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/RankFeatureFieldTypeTests.java index 1a8cf5e721339..57c931992f5db 100644 --- a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/RankFeatureFieldTypeTests.java +++ b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/RankFeatureFieldTypeTests.java @@ -11,7 +11,6 @@ import org.elasticsearch.index.mapper.FieldTypeTestCase; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.MapperBuilderContext; -import org.elasticsearch.index.mapper.extras.RankFeatureFieldMapper; import java.io.IOException; import java.util.Collections; diff --git a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/RankFeatureMetaFieldMapperTests.java b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/RankFeatureMetaFieldMapperTests.java index da5f1d7fa7899..c0b8def546cb4 100644 --- a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/RankFeatureMetaFieldMapperTests.java +++ b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/RankFeatureMetaFieldMapperTests.java @@ -33,9 +33,18 @@ protected Collection getPlugins() { } public void testBasics() throws Exception { - String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type") - .startObject("properties").startObject("field").field("type", "rank_feature").endObject().endObject() - .endObject().endObject()); + String mapping = Strings.toString( + XContentFactory.jsonBuilder() + .startObject() + .startObject("type") + .startObject("properties") + .startObject("field") + .field("type", "rank_feature") + .endObject() + .endObject() + .endObject() + .endObject() + ); Mapping parsedMapping = createMapperService(mapping).parseMapping("type", new CompressedXContent(mapping)); assertEquals(mapping, parsedMapping.toCompressedXContent().toString()); @@ -55,9 +64,13 @@ public void testDocumentParsingFailsOnMetaField() throws Exception { ); String rfMetaField = RankFeatureMetaFieldMapper.CONTENT_TYPE; BytesReference bytes = BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field(rfMetaField, 0).endObject()); - MapperParsingException e = expectThrows(MapperParsingException.class, () -> - mapper.parse(new SourceToParse("test", "1", bytes, XContentType.JSON))); - assertThat(e.getCause().getMessage(), - CoreMatchers.containsString("Field ["+ rfMetaField + "] is a metadata field and cannot be added inside a document.")); + MapperParsingException e = expectThrows( + MapperParsingException.class, + () -> mapper.parse(new SourceToParse("test", "1", bytes, XContentType.JSON)) + ); + assertThat( + e.getCause().getMessage(), + CoreMatchers.containsString("Field [" + rfMetaField + "] is a metadata field and cannot be added inside a document.") + ); } } diff --git a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/RankFeatureQueryBuilderTests.java b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/RankFeatureQueryBuilderTests.java index 02d5a619f93d9..3c65e0038c250 100644 --- a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/RankFeatureQueryBuilderTests.java +++ b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/RankFeatureQueryBuilderTests.java @@ -14,9 +14,7 @@ import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest; import org.elasticsearch.common.Strings; import org.elasticsearch.common.compress.CompressedXContent; -import org.elasticsearch.index.mapper.extras.MapperExtrasPlugin; import org.elasticsearch.index.mapper.MapperService; -import org.elasticsearch.index.mapper.extras.RankFeatureQueryBuilder; import org.elasticsearch.index.mapper.extras.RankFeatureQueryBuilder.ScoreFunction; import org.elasticsearch.index.query.SearchExecutionContext; import org.elasticsearch.plugins.Plugin; @@ -36,10 +34,22 @@ public class RankFeatureQueryBuilderTests extends AbstractQueryTestCase fields = new ArrayList<>(); fields.add("my_feature_field"); @@ -91,40 +101,39 @@ protected void doAssertLuceneQuery(RankFeatureQueryBuilder queryBuilder, Query q } public void testDefaultScoreFunction() throws IOException { - String query = "{\n" + - " \"rank_feature\" : {\n" + - " \"field\": \"my_feature_field\"\n" + - " }\n" + - "}"; + String query = "{\n" + " \"rank_feature\" : {\n" + " \"field\": \"my_feature_field\"\n" + " }\n" + "}"; Query parsedQuery = parseQuery(query).toQuery(createSearchExecutionContext()); assertEquals(FeatureField.newSaturationQuery("_feature", "my_feature_field"), parsedQuery); } public void testIllegalField() { - String query = "{\n" + - " \"rank_feature\" : {\n" + - " \"field\": \"" + TEXT_FIELD_NAME + "\"\n" + - " }\n" + - "}"; - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> parseQuery(query).toQuery(createSearchExecutionContext())); - assertEquals("[rank_feature] query only works on [rank_feature] fields and features of [rank_features] fields, not [text]", - e.getMessage()); + String query = "{\n" + " \"rank_feature\" : {\n" + " \"field\": \"" + TEXT_FIELD_NAME + "\"\n" + " }\n" + "}"; + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> parseQuery(query).toQuery(createSearchExecutionContext()) + ); + assertEquals( + "[rank_feature] query only works on [rank_feature] fields and features of [rank_features] fields, not [text]", + e.getMessage() + ); } public void testIllegalCombination() { - String query = "{\n" + - " \"rank_feature\" : {\n" + - " \"field\": \"my_negative_feature_field\",\n" + - " \"log\" : {\n" + - " \"scaling_factor\": 4.5\n" + - " }\n" + - " }\n" + - "}"; - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> parseQuery(query).toQuery(createSearchExecutionContext())); + String query = "{\n" + + " \"rank_feature\" : {\n" + + " \"field\": \"my_negative_feature_field\",\n" + + " \"log\" : {\n" + + " \"scaling_factor\": 4.5\n" + + " }\n" + + " }\n" + + "}"; + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> parseQuery(query).toQuery(createSearchExecutionContext()) + ); assertEquals( - "Cannot use the [log] function with a field that has a negative score impact as it would trigger negative scores", - e.getMessage()); + "Cannot use the [log] function with a field that has a negative score impact as it would trigger negative scores", + e.getMessage() + ); } } diff --git a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/RankFeaturesFieldMapperTests.java b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/RankFeaturesFieldMapperTests.java index 558c907093901..bb3e3141497e3 100644 --- a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/RankFeaturesFieldMapperTests.java +++ b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/RankFeaturesFieldMapperTests.java @@ -11,7 +11,6 @@ import org.apache.lucene.document.FeatureField; import org.apache.lucene.index.IndexableField; import org.elasticsearch.common.Strings; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.MapperParsingException; @@ -19,6 +18,7 @@ import org.elasticsearch.index.mapper.MapperTestCase; import org.elasticsearch.index.mapper.ParsedDocument; import org.elasticsearch.plugins.Plugin; +import org.elasticsearch.xcontent.XContentBuilder; import org.hamcrest.Matchers; import java.io.IOException; @@ -80,9 +80,9 @@ public void testDefaults() throws Exception { FeatureField featureField2 = null; for (IndexableField field : fields) { if (field.stringValue().equals("ten")) { - featureField1 = (FeatureField)field; + featureField1 = (FeatureField) field; } else if (field.stringValue().equals("twenty")) { - featureField2 = (FeatureField)field; + featureField2 = (FeatureField) field; } else { throw new UnsupportedOperationException(); } @@ -107,9 +107,9 @@ public void testNegativeScoreImpact() throws Exception { FeatureField featureField2 = null; for (IndexableField field : fields) { if (field.stringValue().equals("ten")) { - featureField1 = (FeatureField)field; + featureField1 = (FeatureField) field; } else if (field.stringValue().equals("twenty")) { - featureField2 = (FeatureField)field; + featureField2 = (FeatureField) field; } else { throw new UnsupportedOperationException(); } @@ -134,8 +134,10 @@ public void testRejectMultiValuedFields() throws MapperParsingException, IOExcep MapperParsingException.class, () -> mapper.parse(source(b -> b.startObject("field").field("foo", Arrays.asList(10, 20)).endObject())) ); - assertEquals("[rank_features] fields take hashes that map a feature to a strictly positive float, but got unexpected token " + - "START_ARRAY", e.getCause().getMessage()); + assertEquals( + "[rank_features] fields take hashes that map a feature to a strictly positive float, but got unexpected token " + "START_ARRAY", + e.getCause().getMessage() + ); e = expectThrows(MapperParsingException.class, () -> mapper.parse(source(b -> { b.startArray("foo"); @@ -145,8 +147,11 @@ public void testRejectMultiValuedFields() throws MapperParsingException, IOExcep } b.endArray(); }))); - assertEquals("[rank_features] fields do not support indexing multiple values for the same rank feature [foo.field.bar] in " + - "the same document", e.getCause().getMessage()); + assertEquals( + "[rank_features] fields do not support indexing multiple values for the same rank feature [foo.field.bar] in " + + "the same document", + e.getCause().getMessage() + ); } public void testCannotBeUsedInMultifields() { diff --git a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/RankFeaturesFieldTypeTests.java b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/RankFeaturesFieldTypeTests.java index 6ac2c1133a553..48a31691ac1bc 100644 --- a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/RankFeaturesFieldTypeTests.java +++ b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/RankFeaturesFieldTypeTests.java @@ -10,7 +10,6 @@ import org.elasticsearch.index.mapper.FieldTypeTestCase; import org.elasticsearch.index.mapper.MappedFieldType; -import org.elasticsearch.index.mapper.extras.RankFeaturesFieldMapper; import java.util.Collections; diff --git a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/ScaledFloatFieldMapperTests.java b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/ScaledFloatFieldMapperTests.java index eaca9edee1036..75042060940eb 100644 --- a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/ScaledFloatFieldMapperTests.java +++ b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/ScaledFloatFieldMapperTests.java @@ -12,9 +12,6 @@ import org.apache.lucene.index.IndexableField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.MapperParsingException; @@ -23,6 +20,9 @@ import org.elasticsearch.index.mapper.ParsedDocument; import org.elasticsearch.index.mapper.SourceToParse; import org.elasticsearch.plugins.Plugin; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentType; import java.io.IOException; import java.util.Arrays; @@ -51,21 +51,19 @@ protected void minimalMapping(XContentBuilder b) throws IOException { @Override protected void registerParameters(ParameterChecker checker) throws IOException { - checker.registerConflictCheck( - "scaling_factor", - fieldMapping(this::minimalMapping), - fieldMapping(b -> { - b.field("type", "scaled_float"); - b.field("scaling_factor", 5.0); - })); + checker.registerConflictCheck("scaling_factor", fieldMapping(this::minimalMapping), fieldMapping(b -> { + b.field("type", "scaled_float"); + b.field("scaling_factor", 5.0); + })); checker.registerConflictCheck("doc_values", b -> b.field("doc_values", false)); checker.registerConflictCheck("index", b -> b.field("index", false)); checker.registerConflictCheck("store", b -> b.field("store", true)); checker.registerConflictCheck("null_value", b -> b.field("null_value", 1)); - checker.registerUpdateCheck(b -> b.field("coerce", false), - m -> assertFalse(((ScaledFloatFieldMapper) m).coerce())); - checker.registerUpdateCheck(b -> b.field("ignore_malformed", true), - m -> assertTrue(((ScaledFloatFieldMapper) m).ignoreMalformed())); + checker.registerUpdateCheck(b -> b.field("coerce", false), m -> assertFalse(((ScaledFloatFieldMapper) m).coerce())); + checker.registerUpdateCheck( + b -> b.field("ignore_malformed", true), + m -> assertTrue(((ScaledFloatFieldMapper) m).ignoreMalformed()) + ); } public void testExistsQueryDocValuesDisabled() throws IOException { @@ -116,12 +114,14 @@ public void testNotIndexed() throws Exception { fieldMapping(b -> b.field("type", "scaled_float").field("index", false).field("scaling_factor", 10.0)) ); - ParsedDocument doc = mapper.parse(new SourceToParse("test", "1", BytesReference - .bytes(XContentFactory.jsonBuilder() - .startObject() - .field("field", 123) - .endObject()), - XContentType.JSON)); + ParsedDocument doc = mapper.parse( + new SourceToParse( + "test", + "1", + BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field("field", 123).endObject()), + XContentType.JSON + ) + ); IndexableField[] fields = doc.rootDoc().getFields("field"); assertEquals(1, fields.length); @@ -135,12 +135,14 @@ public void testNoDocValues() throws Exception { fieldMapping(b -> b.field("type", "scaled_float").field("doc_values", false).field("scaling_factor", 10.0)) ); - ParsedDocument doc = mapper.parse(new SourceToParse("test", "1", BytesReference - .bytes(XContentFactory.jsonBuilder() - .startObject() - .field("field", 123) - .endObject()), - XContentType.JSON)); + ParsedDocument doc = mapper.parse( + new SourceToParse( + "test", + "1", + BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field("field", 123).endObject()), + XContentType.JSON + ) + ); IndexableField[] fields = doc.rootDoc().getFields("field"); assertEquals(1, fields.length); @@ -154,12 +156,14 @@ public void testStore() throws Exception { fieldMapping(b -> b.field("type", "scaled_float").field("store", true).field("scaling_factor", 10.0)) ); - ParsedDocument doc = mapper.parse(new SourceToParse("test", "1", BytesReference - .bytes(XContentFactory.jsonBuilder() - .startObject() - .field("field", 123) - .endObject()), - XContentType.JSON)); + ParsedDocument doc = mapper.parse( + new SourceToParse( + "test", + "1", + BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field("field", 123).endObject()), + XContentType.JSON + ) + ); IndexableField[] fields = doc.rootDoc().getFields("field"); assertEquals(3, fields.length); @@ -175,12 +179,14 @@ public void testStore() throws Exception { public void testCoerce() throws Exception { DocumentMapper mapper = createDocumentMapper(fieldMapping(this::minimalMapping)); - ParsedDocument doc = mapper.parse(new SourceToParse("test", "1", BytesReference - .bytes(XContentFactory.jsonBuilder() - .startObject() - .field("field", "123") - .endObject()), - XContentType.JSON)); + ParsedDocument doc = mapper.parse( + new SourceToParse( + "test", + "1", + BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field("field", "123").endObject()), + XContentType.JSON + ) + ); IndexableField[] fields = doc.rootDoc().getFields("field"); assertEquals(2, fields.length); IndexableField pointField = fields[0]; @@ -192,12 +198,14 @@ public void testCoerce() throws Exception { DocumentMapper mapper2 = createDocumentMapper( fieldMapping(b -> b.field("type", "scaled_float").field("scaling_factor", 10.0).field("coerce", false)) ); - ThrowingRunnable runnable = () -> mapper2.parse(new SourceToParse("test", "1", BytesReference - .bytes(XContentFactory.jsonBuilder() - .startObject() - .field("field", "123") - .endObject()), - XContentType.JSON)); + ThrowingRunnable runnable = () -> mapper2.parse( + new SourceToParse( + "test", + "1", + BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field("field", "123").endObject()), + XContentType.JSON + ) + ); MapperParsingException e = expectThrows(MapperParsingException.class, runnable); assertThat(e.getCause().getMessage(), containsString("passed as String")); } @@ -213,24 +221,28 @@ public void testIgnoreMalformed() throws Exception { private void doTestIgnoreMalformed(String value, String exceptionMessageContains) throws Exception { DocumentMapper mapper = createDocumentMapper(fieldMapping(this::minimalMapping)); - ThrowingRunnable runnable = () -> mapper.parse(new SourceToParse("test", "1", BytesReference - .bytes(XContentFactory.jsonBuilder() - .startObject() - .field("field", value) - .endObject()), - XContentType.JSON)); + ThrowingRunnable runnable = () -> mapper.parse( + new SourceToParse( + "test", + "1", + BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field("field", value).endObject()), + XContentType.JSON + ) + ); MapperParsingException e = expectThrows(MapperParsingException.class, runnable); assertThat(e.getCause().getMessage(), containsString(exceptionMessageContains)); DocumentMapper mapper2 = createDocumentMapper( fieldMapping(b -> b.field("type", "scaled_float").field("scaling_factor", 10.0).field("ignore_malformed", true)) ); - ParsedDocument doc = mapper2.parse(new SourceToParse("test", "1", BytesReference - .bytes(XContentFactory.jsonBuilder() - .startObject() - .field("field", value) - .endObject()), - XContentType.JSON)); + ParsedDocument doc = mapper2.parse( + new SourceToParse( + "test", + "1", + BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field("field", value).endObject()), + XContentType.JSON + ) + ); IndexableField[] fields = doc.rootDoc().getFields("field"); assertEquals(0, fields.length); @@ -238,23 +250,27 @@ private void doTestIgnoreMalformed(String value, String exceptionMessageContains public void testNullValue() throws IOException { DocumentMapper mapper = createDocumentMapper(fieldMapping(this::minimalMapping)); - ParsedDocument doc = mapper.parse(new SourceToParse("test", "1", BytesReference - .bytes(XContentFactory.jsonBuilder() - .startObject() - .nullField("field") - .endObject()), - XContentType.JSON)); + ParsedDocument doc = mapper.parse( + new SourceToParse( + "test", + "1", + BytesReference.bytes(XContentFactory.jsonBuilder().startObject().nullField("field").endObject()), + XContentType.JSON + ) + ); assertArrayEquals(new IndexableField[0], doc.rootDoc().getFields("field")); - mapper = createDocumentMapper(fieldMapping(b -> b.field("type", "scaled_float") - .field("scaling_factor", 10.0) - .field("null_value", 2.5))); - doc = mapper.parse(new SourceToParse("test", "1", BytesReference - .bytes(XContentFactory.jsonBuilder() - .startObject() - .nullField("field") - .endObject()), - XContentType.JSON)); + mapper = createDocumentMapper( + fieldMapping(b -> b.field("type", "scaled_float").field("scaling_factor", 10.0).field("null_value", 2.5)) + ); + doc = mapper.parse( + new SourceToParse( + "test", + "1", + BytesReference.bytes(XContentFactory.jsonBuilder().startObject().nullField("field").endObject()), + XContentType.JSON + ) + ); IndexableField[] fields = doc.rootDoc().getFields("field"); assertEquals(2, fields.length); IndexableField pointField = fields[0]; @@ -274,8 +290,10 @@ public void testRejectIndexOptions() { MapperParsingException.class, () -> createMapperService(fieldMapping(b -> b.field("type", "scaled_float").field("index_options", randomIndexOptions()))) ); - assertThat(e.getMessage(), - containsString("Failed to parse mapping: unknown parameter [index_options] on mapper [field] of type [scaled_float]")); + assertThat( + e.getMessage(), + containsString("Failed to parse mapping: unknown parameter [index_options] on mapper [field] of type [scaled_float]") + ); } public void testMetricType() throws IOException { diff --git a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/ScaledFloatFieldTypeTests.java b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/ScaledFloatFieldTypeTests.java index 864b308c294f0..4ed8107bd72ec 100644 --- a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/ScaledFloatFieldTypeTests.java +++ b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/ScaledFloatFieldTypeTests.java @@ -26,7 +26,6 @@ import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.MapperBuilderContext; import org.elasticsearch.index.mapper.NumberFieldMapper; -import org.elasticsearch.index.mapper.extras.ScaledFloatFieldMapper; import java.io.IOException; import java.util.Arrays; @@ -36,23 +35,25 @@ public class ScaledFloatFieldTypeTests extends FieldTypeTestCase { public void testTermQuery() { - ScaledFloatFieldMapper.ScaledFloatFieldType ft - = new ScaledFloatFieldMapper.ScaledFloatFieldType("scaled_float", 0.1 + randomDouble() * 100); + ScaledFloatFieldMapper.ScaledFloatFieldType ft = new ScaledFloatFieldMapper.ScaledFloatFieldType( + "scaled_float", + 0.1 + randomDouble() * 100 + ); double value = (randomDouble() * 2 - 1) * 10000; long scaledValue = Math.round(value * ft.getScalingFactor()); assertEquals(LongPoint.newExactQuery("scaled_float", scaledValue), ft.termQuery(value, null)); } public void testTermsQuery() { - ScaledFloatFieldMapper.ScaledFloatFieldType ft - = new ScaledFloatFieldMapper.ScaledFloatFieldType("scaled_float", 0.1 + randomDouble() * 100); + ScaledFloatFieldMapper.ScaledFloatFieldType ft = new ScaledFloatFieldMapper.ScaledFloatFieldType( + "scaled_float", + 0.1 + randomDouble() * 100 + ); double value1 = (randomDouble() * 2 - 1) * 10000; long scaledValue1 = Math.round(value1 * ft.getScalingFactor()); double value2 = (randomDouble() * 2 - 1) * 10000; long scaledValue2 = Math.round(value2 * ft.getScalingFactor()); - assertEquals( - LongPoint.newSetQuery("scaled_float", scaledValue1, scaledValue2), - ft.termsQuery(Arrays.asList(value1, value2), null)); + assertEquals(LongPoint.newSetQuery("scaled_float", scaledValue1, scaledValue2), ft.termsQuery(Arrays.asList(value1, value2), null)); } public void testRangeQuery() throws IOException { @@ -98,8 +99,7 @@ public void testRangeQuery() throws IOException { } public void testRoundsUpperBoundCorrectly() { - ScaledFloatFieldMapper.ScaledFloatFieldType ft - = new ScaledFloatFieldMapper.ScaledFloatFieldType("scaled_float", 100); + ScaledFloatFieldMapper.ScaledFloatFieldType ft = new ScaledFloatFieldMapper.ScaledFloatFieldType("scaled_float", 100); Query scaledFloatQ = ft.rangeQuery(null, 0.1, true, false, MOCK_CONTEXT); assertEquals("scaled_float:[-9223372036854775808 TO 9]", scaledFloatQ.toString()); scaledFloatQ = ft.rangeQuery(null, 0.1, true, true, MOCK_CONTEXT); @@ -117,8 +117,7 @@ public void testRoundsUpperBoundCorrectly() { } public void testRoundsLowerBoundCorrectly() { - ScaledFloatFieldMapper.ScaledFloatFieldType ft - = new ScaledFloatFieldMapper.ScaledFloatFieldType("scaled_float", 100); + ScaledFloatFieldMapper.ScaledFloatFieldType ft = new ScaledFloatFieldMapper.ScaledFloatFieldType("scaled_float", 100); Query scaledFloatQ = ft.rangeQuery(-0.1, null, false, true, MOCK_CONTEXT); assertEquals("scaled_float:[-9 TO 9223372036854775807]", scaledFloatQ.toString()); scaledFloatQ = ft.rangeQuery(-0.1, null, true, true, MOCK_CONTEXT); @@ -134,10 +133,12 @@ public void testRoundsLowerBoundCorrectly() { } public void testValueForSearch() { - ScaledFloatFieldMapper.ScaledFloatFieldType ft - = new ScaledFloatFieldMapper.ScaledFloatFieldType("scaled_float", 0.1 + randomDouble() * 100); + ScaledFloatFieldMapper.ScaledFloatFieldType ft = new ScaledFloatFieldMapper.ScaledFloatFieldType( + "scaled_float", + 0.1 + randomDouble() * 100 + ); assertNull(ft.valueForDisplay(null)); - assertEquals(10/ft.getScalingFactor(), ft.valueForDisplay(10L)); + assertEquals(10 / ft.getScalingFactor(), ft.valueForDisplay(10L)); } public void testFieldData() throws IOException { @@ -151,45 +152,47 @@ public void testFieldData() throws IOException { w.addDocument(doc); try (DirectoryReader reader = DirectoryReader.open(w)) { // single-valued - ScaledFloatFieldMapper.ScaledFloatFieldType f1 - = new ScaledFloatFieldMapper.ScaledFloatFieldType("scaled_float1", scalingFactor); - IndexNumericFieldData fielddata = (IndexNumericFieldData) f1.fielddataBuilder("index", () -> { - throw new UnsupportedOperationException(); - }).build(null, null); + ScaledFloatFieldMapper.ScaledFloatFieldType f1 = new ScaledFloatFieldMapper.ScaledFloatFieldType( + "scaled_float1", + scalingFactor + ); + IndexNumericFieldData fielddata = (IndexNumericFieldData) f1.fielddataBuilder( + "index", + () -> { throw new UnsupportedOperationException(); } + ).build(null, null); assertEquals(fielddata.getNumericType(), IndexNumericFieldData.NumericType.DOUBLE); LeafNumericFieldData leafFieldData = fielddata.load(reader.leaves().get(0)); SortedNumericDoubleValues values = leafFieldData.getDoubleValues(); assertTrue(values.advanceExact(0)); assertEquals(1, values.docValueCount()); - assertEquals(10/f1.getScalingFactor(), values.nextValue(), 10e-5); + assertEquals(10 / f1.getScalingFactor(), values.nextValue(), 10e-5); // multi-valued - ScaledFloatFieldMapper.ScaledFloatFieldType f2 - = new ScaledFloatFieldMapper.ScaledFloatFieldType("scaled_float2", scalingFactor); - fielddata = (IndexNumericFieldData) f2.fielddataBuilder("index", () -> { - throw new UnsupportedOperationException(); - }).build(null, null); + ScaledFloatFieldMapper.ScaledFloatFieldType f2 = new ScaledFloatFieldMapper.ScaledFloatFieldType( + "scaled_float2", + scalingFactor + ); + fielddata = (IndexNumericFieldData) f2.fielddataBuilder("index", () -> { throw new UnsupportedOperationException(); }) + .build(null, null); leafFieldData = fielddata.load(reader.leaves().get(0)); values = leafFieldData.getDoubleValues(); assertTrue(values.advanceExact(0)); assertEquals(2, values.docValueCount()); - assertEquals(5/f2.getScalingFactor(), values.nextValue(), 10e-5); - assertEquals(12/f2.getScalingFactor(), values.nextValue(), 10e-5); + assertEquals(5 / f2.getScalingFactor(), values.nextValue(), 10e-5); + assertEquals(12 / f2.getScalingFactor(), values.nextValue(), 10e-5); } IOUtils.close(w, dir); } public void testFetchSourceValue() throws IOException { - MappedFieldType mapper = new ScaledFloatFieldMapper.Builder("field", false, false) - .scalingFactor(100) + MappedFieldType mapper = new ScaledFloatFieldMapper.Builder("field", false, false).scalingFactor(100) .build(MapperBuilderContext.ROOT) .fieldType(); assertEquals(List.of(3.14), fetchSourceValue(mapper, 3.1415926)); assertEquals(List.of(3.14), fetchSourceValue(mapper, "3.1415")); assertEquals(List.of(), fetchSourceValue(mapper, "")); - MappedFieldType nullValueMapper = new ScaledFloatFieldMapper.Builder("field", false, false) - .scalingFactor(100) + MappedFieldType nullValueMapper = new ScaledFloatFieldMapper.Builder("field", false, false).scalingFactor(100) .nullValue(2.71) .build(MapperBuilderContext.ROOT) .fieldType(); diff --git a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/SearchAsYouTypeAnalyzerTests.java b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/SearchAsYouTypeAnalyzerTests.java index 4b5833b8f7a80..1c1da8fc5f178 100644 --- a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/SearchAsYouTypeAnalyzerTests.java +++ b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/SearchAsYouTypeAnalyzerTests.java @@ -30,7 +30,7 @@ public class SearchAsYouTypeAnalyzerTests extends ESTestCase { private static final Analyzer SIMPLE = new SimpleAnalyzer(); - public static List analyze(SearchAsYouTypeAnalyzer analyzer, String text) throws IOException { + public static List analyze(SearchAsYouTypeAnalyzer analyzer, String text) throws IOException { final List tokens = new ArrayList<>(); try (TokenStream tokenStream = analyzer.tokenStream("field", text)) { final CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class); @@ -42,9 +42,11 @@ public static List analyze(SearchAsYouTypeAnalyzer analyzer, String text return tokens; } - private void testCase(String text, - Function analyzerFunction, - Function> expectedTokensFunction) throws IOException { + private void testCase( + String text, + Function analyzerFunction, + Function> expectedTokensFunction + ) throws IOException { for (int shingleSize = 2; shingleSize <= 4; shingleSize++) { final SearchAsYouTypeAnalyzer analyzer = analyzerFunction.apply(shingleSize); @@ -55,114 +57,178 @@ private void testCase(String text, } public void testSingleTermShingles() throws IOException { - testCase( - "quick", - shingleSize -> SearchAsYouTypeAnalyzer.withShingle(SIMPLE, shingleSize), - shingleSize -> emptyList() - ); + testCase("quick", shingleSize -> SearchAsYouTypeAnalyzer.withShingle(SIMPLE, shingleSize), shingleSize -> emptyList()); } public void testMultiTermShingles() throws IOException { - testCase( - "quick brown fox jump lazy", - shingleSize -> SearchAsYouTypeAnalyzer.withShingle(SIMPLE, shingleSize), - shingleSize -> { - if (shingleSize == 2) { - return asList("quick brown", "brown fox", "fox jump", "jump lazy"); - } else if (shingleSize == 3) { - return asList("quick brown fox", "brown fox jump", "fox jump lazy"); - } else if (shingleSize == 4) { - return asList("quick brown fox jump", "brown fox jump lazy"); - } - throw new IllegalArgumentException(); + testCase("quick brown fox jump lazy", shingleSize -> SearchAsYouTypeAnalyzer.withShingle(SIMPLE, shingleSize), shingleSize -> { + if (shingleSize == 2) { + return asList("quick brown", "brown fox", "fox jump", "jump lazy"); + } else if (shingleSize == 3) { + return asList("quick brown fox", "brown fox jump", "fox jump lazy"); + } else if (shingleSize == 4) { + return asList("quick brown fox jump", "brown fox jump lazy"); } - ); + throw new IllegalArgumentException(); + }); } public void testSingleTermPrefix() throws IOException { - testCase( - "quick", - shingleSize -> SearchAsYouTypeAnalyzer.withShingleAndPrefix(SIMPLE, shingleSize), - shingleSize -> { - final List tokens = new ArrayList<>(asList("q", "qu", "qui", "quic", "quick")); - tokens.addAll(tokenWithSpaces("quick", shingleSize)); - return tokens; - } - ); + testCase("quick", shingleSize -> SearchAsYouTypeAnalyzer.withShingleAndPrefix(SIMPLE, shingleSize), shingleSize -> { + final List tokens = new ArrayList<>(asList("q", "qu", "qui", "quic", "quick")); + tokens.addAll(tokenWithSpaces("quick", shingleSize)); + return tokens; + }); } public void testMultiTermPrefix() throws IOException { testCase( - //"quick red fox lazy brown", + // "quick red fox lazy brown", "quick brown fox jump lazy", shingleSize -> SearchAsYouTypeAnalyzer.withShingleAndPrefix(SIMPLE, shingleSize), shingleSize -> { if (shingleSize == 2) { final List tokens = new ArrayList<>(); - tokens.addAll(asList( - "q", "qu", "qui", "quic", "quick", "quick ", "quick b", "quick br", "quick bro", "quick brow", "quick brown" - )); - tokens.addAll(asList( - "b", "br", "bro", "brow", "brown", "brown ", "brown f", "brown fo", "brown fox" - )); - tokens.addAll(asList( - "f", "fo", "fox", "fox ", "fox j", "fox ju", "fox jum", "fox jump" - )); - tokens.addAll(asList( - "j", "ju", "jum", "jump", "jump ", "jump l", "jump la", "jump laz", "jump lazy" - )); - tokens.addAll(asList( - "l", "la", "laz", "lazy" - )); + tokens.addAll( + asList("q", "qu", "qui", "quic", "quick", "quick ", "quick b", "quick br", "quick bro", "quick brow", "quick brown") + ); + tokens.addAll(asList("b", "br", "bro", "brow", "brown", "brown ", "brown f", "brown fo", "brown fox")); + tokens.addAll(asList("f", "fo", "fox", "fox ", "fox j", "fox ju", "fox jum", "fox jump")); + tokens.addAll(asList("j", "ju", "jum", "jump", "jump ", "jump l", "jump la", "jump laz", "jump lazy")); + tokens.addAll(asList("l", "la", "laz", "lazy")); tokens.addAll(tokenWithSpaces("lazy", shingleSize)); return tokens; } else if (shingleSize == 3) { final List tokens = new ArrayList<>(); - tokens.addAll(asList( - "q", "qu", "qui", "quic", "quick", "quick ", "quick b", "quick br", "quick bro", "quick brow", "quick brown", - "quick brown ", "quick brown f", "quick brown fo", "quick brown fox" - )); - tokens.addAll(asList( - "b", "br", "bro", "brow", "brown", "brown ", "brown f", "brown fo", "brown fox", "brown fox ", "brown fox j", - "brown fox ju", "brown fox jum", "brown fox jump" - )); - tokens.addAll(asList( - "f", "fo", "fox", "fox ", "fox j", "fox ju", "fox jum", "fox jump", "fox jump ", "fox jump l", "fox jump la", - "fox jump laz", "fox jump lazy" - )); - tokens.addAll(asList( - "j", "ju", "jum", "jump", "jump ", "jump l", "jump la", "jump laz", "jump lazy" - )); + tokens.addAll( + asList( + "q", + "qu", + "qui", + "quic", + "quick", + "quick ", + "quick b", + "quick br", + "quick bro", + "quick brow", + "quick brown", + "quick brown ", + "quick brown f", + "quick brown fo", + "quick brown fox" + ) + ); + tokens.addAll( + asList( + "b", + "br", + "bro", + "brow", + "brown", + "brown ", + "brown f", + "brown fo", + "brown fox", + "brown fox ", + "brown fox j", + "brown fox ju", + "brown fox jum", + "brown fox jump" + ) + ); + tokens.addAll( + asList( + "f", + "fo", + "fox", + "fox ", + "fox j", + "fox ju", + "fox jum", + "fox jump", + "fox jump ", + "fox jump l", + "fox jump la", + "fox jump laz", + "fox jump lazy" + ) + ); + tokens.addAll(asList("j", "ju", "jum", "jump", "jump ", "jump l", "jump la", "jump laz", "jump lazy")); tokens.addAll(tokenWithSpaces("jump lazy", shingleSize - 1)); - tokens.addAll(asList( - "l", "la", "laz", "lazy" - )); + tokens.addAll(asList("l", "la", "laz", "lazy")); tokens.addAll(tokenWithSpaces("lazy", shingleSize)); return tokens; } else if (shingleSize == 4) { final List tokens = new ArrayList<>(); - tokens.addAll(asList( - "q", "qu", "qui", "quic", "quick", "quick ", "quick b", "quick br", "quick bro", "quick brow", "quick brown", - "quick brown ", "quick brown f", "quick brown fo", "quick brown fox", "quick brown fox ", "quick brown fox j", - "quick brown fox ju", "quick brown fox jum", "quick brown fox jump" - )); - tokens.addAll(asList( - "b", "br", "bro", "brow", "brown", "brown ", "brown f", "brown fo", "brown fox", "brown fox ", "brown fox j", - "brown fox ju", "brown fox jum", "brown fox jump", "brown fox jump ", "brown fox jump l", "brown fox jump la", - "brown fox jump laz", "brown fox jump lazy" - )); - tokens.addAll(asList( - "f", "fo", "fox", "fox ", "fox j", "fox ju", "fox jum", "fox jump", "fox jump ", "fox jump l", "fox jump la", - "fox jump laz", "fox jump lazy" - )); + tokens.addAll( + asList( + "q", + "qu", + "qui", + "quic", + "quick", + "quick ", + "quick b", + "quick br", + "quick bro", + "quick brow", + "quick brown", + "quick brown ", + "quick brown f", + "quick brown fo", + "quick brown fox", + "quick brown fox ", + "quick brown fox j", + "quick brown fox ju", + "quick brown fox jum", + "quick brown fox jump" + ) + ); + tokens.addAll( + asList( + "b", + "br", + "bro", + "brow", + "brown", + "brown ", + "brown f", + "brown fo", + "brown fox", + "brown fox ", + "brown fox j", + "brown fox ju", + "brown fox jum", + "brown fox jump", + "brown fox jump ", + "brown fox jump l", + "brown fox jump la", + "brown fox jump laz", + "brown fox jump lazy" + ) + ); + tokens.addAll( + asList( + "f", + "fo", + "fox", + "fox ", + "fox j", + "fox ju", + "fox jum", + "fox jump", + "fox jump ", + "fox jump l", + "fox jump la", + "fox jump laz", + "fox jump lazy" + ) + ); tokens.addAll(tokenWithSpaces("fox jump lazy", shingleSize - 2)); - tokens.addAll(asList( - "j", "ju", "jum", "jump", "jump ", "jump l", "jump la", "jump laz", "jump lazy" - )); + tokens.addAll(asList("j", "ju", "jum", "jump", "jump ", "jump l", "jump la", "jump laz", "jump lazy")); tokens.addAll(tokenWithSpaces("jump lazy", shingleSize - 1)); - tokens.addAll(asList( - "l", "la", "laz", "lazy" - )); + tokens.addAll(asList("l", "la", "laz", "lazy")); tokens.addAll(tokenWithSpaces("lazy", shingleSize)); return tokens; } diff --git a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/SearchAsYouTypeFieldMapperTests.java b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/SearchAsYouTypeFieldMapperTests.java index 98cfcb980e40d..8709f17121cb6 100644 --- a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/SearchAsYouTypeFieldMapperTests.java +++ b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/SearchAsYouTypeFieldMapperTests.java @@ -30,7 +30,6 @@ import org.apache.lucene.search.SynonymQuery; import org.apache.lucene.search.TermQuery; import org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.analysis.AnalyzerScope; import org.elasticsearch.index.analysis.IndexAnalyzers; @@ -42,8 +41,6 @@ import org.elasticsearch.index.mapper.MapperTestCase; import org.elasticsearch.index.mapper.ParsedDocument; import org.elasticsearch.index.mapper.TextFieldMapper; -import org.elasticsearch.index.mapper.extras.MapperExtrasPlugin; -import org.elasticsearch.index.mapper.extras.SearchAsYouTypeFieldMapper; import org.elasticsearch.index.mapper.extras.SearchAsYouTypeFieldMapper.PrefixFieldMapper; import org.elasticsearch.index.mapper.extras.SearchAsYouTypeFieldMapper.PrefixFieldType; import org.elasticsearch.index.mapper.extras.SearchAsYouTypeFieldMapper.SearchAsYouTypeAnalyzer; @@ -56,6 +53,7 @@ import org.elasticsearch.index.query.SearchExecutionContext; import org.elasticsearch.index.search.QueryStringQueryParser; import org.elasticsearch.plugins.Plugin; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.ArrayList; @@ -89,38 +87,30 @@ protected void registerParameters(ParameterChecker checker) throws IOException { checker.registerConflictCheck("term_vector", b -> b.field("term_vector", "yes")); // norms can be set from true to false, but not vice versa - checker.registerConflictCheck("norms", - fieldMapping(b -> { - b.field("type", "text"); - b.field("norms", false); - }), - fieldMapping(b -> { - b.field("type", "text"); - b.field("norms", true); - })); - checker.registerUpdateCheck( - b -> { - b.field("type", "search_as_you_type"); - b.field("norms", true); - }, - b -> { - b.field("type", "search_as_you_type"); - b.field("norms", false); - }, - m -> assertFalse(m.fieldType().getTextSearchInfo().hasNorms()) - ); + checker.registerConflictCheck("norms", fieldMapping(b -> { + b.field("type", "text"); + b.field("norms", false); + }), fieldMapping(b -> { + b.field("type", "text"); + b.field("norms", true); + })); + checker.registerUpdateCheck(b -> { + b.field("type", "search_as_you_type"); + b.field("norms", true); + }, b -> { + b.field("type", "search_as_you_type"); + b.field("norms", false); + }, m -> assertFalse(m.fieldType().getTextSearchInfo().hasNorms())); checker.registerUpdateCheck(b -> { - b.field("analyzer", "default"); - b.field("search_analyzer", "keyword"); - }, - m -> assertEquals("keyword", m.fieldType().getTextSearchInfo().getSearchAnalyzer().name())); + b.field("analyzer", "default"); + b.field("search_analyzer", "keyword"); + }, m -> assertEquals("keyword", m.fieldType().getTextSearchInfo().getSearchAnalyzer().name())); checker.registerUpdateCheck(b -> { - b.field("analyzer", "default"); - b.field("search_analyzer", "keyword"); - b.field("search_quote_analyzer", "keyword"); - }, - m -> assertEquals("keyword", m.fieldType().getTextSearchInfo().getSearchQuoteAnalyzer().name())); + b.field("analyzer", "default"); + b.field("search_analyzer", "keyword"); + b.field("search_quote_analyzer", "keyword"); + }, m -> assertEquals("keyword", m.fieldType().getTextSearchInfo().getSearchQuoteAnalyzer().name())); } @@ -167,14 +157,14 @@ protected void metaMapping(XContentBuilder b) throws IOException { public void testIndexing() throws IOException { DocumentMapper mapper = createDocumentMapper(fieldMapping(this::minimalMapping)); ParsedDocument doc = mapper.parse(source(b -> b.field("field", "new york city"))); - for (String field : new String[] { "field", "field._index_prefix", "field._2gram", "field._3gram"}) { + for (String field : new String[] { "field", "field._index_prefix", "field._2gram", "field._3gram" }) { IndexableField[] fields = doc.rootDoc().getFields(field); assertEquals(1, fields.length); assertEquals("new york city", fields[0].stringValue()); } } - public void testDefaultConfiguration() throws IOException { + public void testDefaultConfiguration() throws IOException { DocumentMapper defaultMapper = createDocumentMapper(fieldMapping(this::minimalMapping)); SearchAsYouTypeFieldMapper rootMapper = getRootFieldMapper(defaultMapper, "field"); assertRootFieldMapper(rootMapper, 3, "default"); @@ -182,11 +172,21 @@ public void testDefaultConfiguration() throws IOException { PrefixFieldMapper prefixFieldMapper = getPrefixFieldMapper(defaultMapper, "field._index_prefix"); assertPrefixFieldType(prefixFieldMapper, rootMapper.indexAnalyzers(), 3, "default"); - assertShingleFieldType(getShingleFieldMapper(defaultMapper, "field._2gram"), - rootMapper.indexAnalyzers(), 2, "default", prefixFieldMapper.fieldType()); - assertShingleFieldType(getShingleFieldMapper(defaultMapper, "field._3gram"), - rootMapper.indexAnalyzers(), 3, "default", prefixFieldMapper.fieldType()); - } + assertShingleFieldType( + getShingleFieldMapper(defaultMapper, "field._2gram"), + rootMapper.indexAnalyzers(), + 2, + "default", + prefixFieldMapper.fieldType() + ); + assertShingleFieldType( + getShingleFieldMapper(defaultMapper, "field._3gram"), + rootMapper.indexAnalyzers(), + 3, + "default", + prefixFieldMapper.fieldType() + ); + } public void testConfiguration() throws IOException { int maxShingleSize = 4; @@ -203,12 +203,27 @@ public void testConfiguration() throws IOException { PrefixFieldMapper prefixFieldMapper = getPrefixFieldMapper(defaultMapper, "field._index_prefix"); assertPrefixFieldType(prefixFieldMapper, rootMapper.indexAnalyzers(), maxShingleSize, analyzerName); - assertShingleFieldType(getShingleFieldMapper(defaultMapper, "field._2gram"), - rootMapper.indexAnalyzers(), 2, analyzerName, prefixFieldMapper.fieldType()); - assertShingleFieldType(getShingleFieldMapper(defaultMapper, "field._3gram"), - rootMapper.indexAnalyzers(), 3, analyzerName, prefixFieldMapper.fieldType()); - assertShingleFieldType(getShingleFieldMapper(defaultMapper, "field._4gram"), - rootMapper.indexAnalyzers(), 4, analyzerName, prefixFieldMapper.fieldType()); + assertShingleFieldType( + getShingleFieldMapper(defaultMapper, "field._2gram"), + rootMapper.indexAnalyzers(), + 2, + analyzerName, + prefixFieldMapper.fieldType() + ); + assertShingleFieldType( + getShingleFieldMapper(defaultMapper, "field._3gram"), + rootMapper.indexAnalyzers(), + 3, + analyzerName, + prefixFieldMapper.fieldType() + ); + assertShingleFieldType( + getShingleFieldMapper(defaultMapper, "field._4gram"), + rootMapper.indexAnalyzers(), + 4, + analyzerName, + prefixFieldMapper.fieldType() + ); } public void testSimpleMerge() throws IOException { @@ -283,14 +298,10 @@ public void testIndexOptions() throws IOException { ParsedDocument doc = mapper.parse(source(b -> b.field("field", "some text"))); - assertThat(fieldType(doc, "field").indexOptions(), - equalTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)); + assertThat(fieldType(doc, "field").indexOptions(), equalTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)); - Stream.of( - fieldType(doc, "field._index_prefix"), - fieldType(doc, "field._2gram"), - fieldType(doc, "field._3gram") - ).forEach(ft -> assertThat(ft.indexOptions(), equalTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS))); + Stream.of(fieldType(doc, "field._index_prefix"), fieldType(doc, "field._2gram"), fieldType(doc, "field._3gram")) + .forEach(ft -> assertThat(ft.indexOptions(), equalTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS))); } public void testStore() throws IOException { @@ -298,11 +309,8 @@ public void testStore() throws IOException { ParsedDocument doc = mapper.parse(source(b -> b.field("field", "some text"))); assertTrue(fieldType(doc, "field").stored()); - Stream.of( - fieldType(doc, "field._index_prefix"), - fieldType(doc, "field._2gram"), - fieldType(doc, "field._3gram") - ).forEach(ft -> assertFalse(ft.stored())); + Stream.of(fieldType(doc, "field._index_prefix"), fieldType(doc, "field._2gram"), fieldType(doc, "field._3gram")) + .forEach(ft -> assertFalse(ft.stored())); } public void testIndex() throws IOException { @@ -326,10 +334,16 @@ public void testStoredOnly() throws IOException { } public void testTermVectors() throws IOException { - for (String termVector : new String[] { "yes", "with_positions", "with_offsets", "with_positions_offsets", - "with_positions_payloads", "with_positions_offsets_payloads"}) { - DocumentMapper mapper = createDocumentMapper(fieldMapping(b -> b.field("type", "search_as_you_type") - .field("term_vector", termVector))); + for (String termVector : new String[] { + "yes", + "with_positions", + "with_offsets", + "with_positions_offsets", + "with_positions_payloads", + "with_positions_offsets_payloads" }) { + DocumentMapper mapper = createDocumentMapper( + fieldMapping(b -> b.field("type", "search_as_you_type").field("term_vector", termVector)) + ); ParsedDocument doc = mapper.parse(source(b -> b.field("field", "some text"))); IndexableFieldType rootField = fieldType(doc, "field"); @@ -346,10 +360,7 @@ public void testTermVectors() throws IOException { assertThat(rootField.storeTermVectorPayloads(), equalTo(termVector.contains("payloads"))); } - Stream.of( - fieldType(doc, "field._2gram"), - fieldType(doc, "field._3gram") - ).forEach(ft -> { + Stream.of(fieldType(doc, "field._2gram"), fieldType(doc, "field._3gram")).forEach(ft -> { assertTrue(ft.storeTermVectors()); if (termVector.contains("positions")) { assertThat(ft.storeTermVectorPositions(), equalTo(termVector.contains("positions"))); @@ -376,11 +387,8 @@ public void testNorms() throws IOException { DocumentMapper mapper = createDocumentMapper(fieldMapping(this::minimalMapping)); ParsedDocument doc = mapper.parse(source(b -> b.field("field", "some text"))); - Stream.of( - fieldType(doc, "field"), - fieldType(doc, "field._2gram"), - fieldType(doc, "field._3gram") - ).forEach(ft -> assertFalse(ft.omitNorms())); + Stream.of(fieldType(doc, "field"), fieldType(doc, "field._2gram"), fieldType(doc, "field._3gram")) + .forEach(ft -> assertFalse(ft.omitNorms())); PrefixFieldMapper prefixFieldMapper = getPrefixFieldMapper(mapper, "field._index_prefix"); assertTrue(prefixFieldMapper.fieldType.omitNorms()); @@ -393,11 +401,8 @@ public void testNorms() throws IOException { assertTrue(fieldType(doc, "field").omitNorms()); - Stream.of( - fieldType(doc, "field._index_prefix"), - fieldType(doc, "field._2gram"), - fieldType(doc, "field._3gram") - ).forEach(ft -> assertTrue(ft.omitNorms())); + Stream.of(fieldType(doc, "field._index_prefix"), fieldType(doc, "field._2gram"), fieldType(doc, "field._3gram")) + .forEach(ft -> assertTrue(ft.omitNorms())); } } @@ -411,7 +416,8 @@ public void testDocumentParsingMultipleValues() throws IOException { public void testMatchPhrasePrefix() throws IOException { SearchExecutionContext searchExecutionContext = createSearchExecutionContext( - createMapperService(fieldMapping(this::minimalMapping))); + createMapperService(fieldMapping(this::minimalMapping)) + ); { Query q = new MatchPhrasePrefixQueryBuilder("field", "two words").toQuery(searchExecutionContext); Query expected = new SynonymQuery.Builder("field._index_prefix").addTerm(new Term("field._index_prefix", "two words")).build(); @@ -436,10 +442,11 @@ public void testMatchPhrasePrefix() throws IOException { { Query q = new MatchPhrasePrefixQueryBuilder("field", "more than three words").toQuery(searchExecutionContext); - Query expected = new SpanNearQuery.Builder("field._3gram", true) - .addClause(new SpanTermQuery(new Term("field._3gram", "more than three"))) - .addClause(new FieldMaskingSpanQuery( - new SpanTermQuery(new Term("field._index_prefix", "than three words")), "field._3gram") + Query expected = new SpanNearQuery.Builder("field._3gram", true).addClause( + new SpanTermQuery(new Term("field._3gram", "more than three")) + ) + .addClause( + new FieldMaskingSpanQuery(new SpanTermQuery(new Term("field._index_prefix", "than three words")), "field._3gram") ) .build(); assertThat(q, equalTo(expected)); @@ -447,10 +454,11 @@ public void testMatchPhrasePrefix() throws IOException { { Query q = new MatchPhrasePrefixQueryBuilder("field._3gram", "more than three words").toQuery(searchExecutionContext); - Query expected = new SpanNearQuery.Builder("field._3gram", true) - .addClause(new SpanTermQuery(new Term("field._3gram", "more than three"))) - .addClause(new FieldMaskingSpanQuery( - new SpanTermQuery(new Term("field._index_prefix", "than three words")), "field._3gram") + Query expected = new SpanNearQuery.Builder("field._3gram", true).addClause( + new SpanTermQuery(new Term("field._3gram", "more than three")) + ) + .addClause( + new FieldMaskingSpanQuery(new SpanTermQuery(new Term("field._index_prefix", "than three words")), "field._3gram") ) .build(); assertThat(q, equalTo(expected)); @@ -463,9 +471,7 @@ public void testMatchPhrasePrefix() throws IOException { } { - Query actual = new MatchPhrasePrefixQueryBuilder("field._3gram", "one two three four") - .slop(1) - .toQuery(searchExecutionContext); + Query actual = new MatchPhrasePrefixQueryBuilder("field._3gram", "one two three four").slop(1).toQuery(searchExecutionContext); MultiPhrasePrefixQuery expected = new MultiPhrasePrefixQuery("field._3gram"); expected.setSlop(1); expected.add(new Term("field._3gram", "one two three")); @@ -477,91 +483,73 @@ public void testMatchPhrasePrefix() throws IOException { public void testMatchPhrase() throws IOException { SearchExecutionContext searchExecutionContext = createSearchExecutionContext( - createMapperService(fieldMapping(this::minimalMapping))); + createMapperService(fieldMapping(this::minimalMapping)) + ); { - Query actual = new MatchPhraseQueryBuilder("field", "one") - .toQuery(searchExecutionContext); + Query actual = new MatchPhraseQueryBuilder("field", "one").toQuery(searchExecutionContext); Query expected = new TermQuery(new Term("field", "one")); assertThat(actual, equalTo(expected)); } { - Query actual = new MatchPhraseQueryBuilder("field", "one two") - .toQuery(searchExecutionContext); - Query expected = new MultiPhraseQuery.Builder() - .add(new Term("field._2gram", "one two")) - .build(); + Query actual = new MatchPhraseQueryBuilder("field", "one two").toQuery(searchExecutionContext); + Query expected = new MultiPhraseQuery.Builder().add(new Term("field._2gram", "one two")).build(); assertThat(actual, equalTo(expected)); } { - Query actual = new MatchPhraseQueryBuilder("field", "one two three") - .toQuery(searchExecutionContext); - Query expected = new MultiPhraseQuery.Builder() - .add(new Term("field._3gram", "one two three")) - .build(); + Query actual = new MatchPhraseQueryBuilder("field", "one two three").toQuery(searchExecutionContext); + Query expected = new MultiPhraseQuery.Builder().add(new Term("field._3gram", "one two three")).build(); assertThat(actual, equalTo(expected)); } { - Query actual = new MatchPhraseQueryBuilder("field", "one two three four") - .toQuery(searchExecutionContext); - Query expected = new MultiPhraseQuery.Builder() - .add(new Term("field._3gram", "one two three")) + Query actual = new MatchPhraseQueryBuilder("field", "one two three four").toQuery(searchExecutionContext); + Query expected = new MultiPhraseQuery.Builder().add(new Term("field._3gram", "one two three")) .add(new Term("field._3gram", "two three four")) .build(); assertThat(actual, equalTo(expected)); } { - Query actual = new MatchPhraseQueryBuilder("field", "one two") - .slop(1) - .toQuery(searchExecutionContext); - Query expected = new MultiPhraseQuery.Builder() - .add(new Term("field", "one")) - .add(new Term("field", "two")) - .setSlop(1) - .build(); + Query actual = new MatchPhraseQueryBuilder("field", "one two").slop(1).toQuery(searchExecutionContext); + Query expected = new MultiPhraseQuery.Builder().add(new Term("field", "one")).add(new Term("field", "two")).setSlop(1).build(); assertThat(actual, equalTo(expected)); } { - Query actual = new MatchPhraseQueryBuilder("field._2gram", "one two") - .toQuery(searchExecutionContext); + Query actual = new MatchPhraseQueryBuilder("field._2gram", "one two").toQuery(searchExecutionContext); Query expected = new TermQuery(new Term("field._2gram", "one two")); assertThat(actual, equalTo(expected)); } { - Query actual = new MatchPhraseQueryBuilder("field._2gram", "one two three") - .toQuery(searchExecutionContext); - Query expected = new MultiPhraseQuery.Builder() - .add(new Term("field._2gram", "one two")) + Query actual = new MatchPhraseQueryBuilder("field._2gram", "one two three").toQuery(searchExecutionContext); + Query expected = new MultiPhraseQuery.Builder().add(new Term("field._2gram", "one two")) .add(new Term("field._2gram", "two three")) .build(); assertThat(actual, equalTo(expected)); } { - Query actual = new MatchPhraseQueryBuilder("field._3gram", "one two three") - .toQuery(searchExecutionContext); + Query actual = new MatchPhraseQueryBuilder("field._3gram", "one two three").toQuery(searchExecutionContext); Query expected = new TermQuery(new Term("field._3gram", "one two three")); assertThat(actual, equalTo(expected)); } { - Query actual = new MatchPhraseQueryBuilder("field._3gram", "one two three four") - .toQuery(searchExecutionContext); - Query expected = new MultiPhraseQuery.Builder() - .add(new Term("field._3gram", "one two three")) + Query actual = new MatchPhraseQueryBuilder("field._3gram", "one two three four").toQuery(searchExecutionContext); + Query expected = new MultiPhraseQuery.Builder().add(new Term("field._3gram", "one two three")) .add(new Term("field._3gram", "two three four")) .build(); assertThat(actual, equalTo(expected)); } { - expectThrows(IllegalArgumentException.class, - () -> new MatchPhraseQueryBuilder("field._index_prefix", "one two three four").toQuery(searchExecutionContext)); + expectThrows( + IllegalArgumentException.class, + () -> new MatchPhraseQueryBuilder("field._index_prefix", "one two three four").toQuery(searchExecutionContext) + ); } } @@ -585,12 +573,16 @@ public void testNestedExistsQuery() throws IOException, ParseException { SearchExecutionContext context = createSearchExecutionContext(ms); QueryStringQueryParser parser = new QueryStringQueryParser(context, "f"); Query q = parser.parse("foo:*"); - assertEquals(new ConstantScoreQuery(new BooleanQuery.Builder() - .add(new NormsFieldExistsQuery("foo.bar"), BooleanClause.Occur.SHOULD) - .add(new NormsFieldExistsQuery("foo.bar._3gram"), BooleanClause.Occur.SHOULD) - .add(new NormsFieldExistsQuery("foo.bar._2gram"), BooleanClause.Occur.SHOULD) - .add(new TermQuery(new Term("_field_names", "foo.bar._index_prefix")), BooleanClause.Occur.SHOULD) - .build()), q); + assertEquals( + new ConstantScoreQuery( + new BooleanQuery.Builder().add(new NormsFieldExistsQuery("foo.bar"), BooleanClause.Occur.SHOULD) + .add(new NormsFieldExistsQuery("foo.bar._3gram"), BooleanClause.Occur.SHOULD) + .add(new NormsFieldExistsQuery("foo.bar._2gram"), BooleanClause.Occur.SHOULD) + .add(new TermQuery(new Term("_field_names", "foo.bar._index_prefix")), BooleanClause.Occur.SHOULD) + .build() + ), + q + ); } private static BooleanQuery buildBoolPrefixQuery(String shingleFieldName, String prefixFieldName, List terms) { @@ -600,8 +592,9 @@ private static BooleanQuery buildBoolPrefixQuery(String shingleFieldName, String builder.add(new BooleanClause(new TermQuery(new Term(shingleFieldName, term)), BooleanClause.Occur.SHOULD)); } final String finalTerm = terms.get(terms.size() - 1); - builder.add(new BooleanClause( - new ConstantScoreQuery(new TermQuery(new Term(prefixFieldName, finalTerm))), BooleanClause.Occur.SHOULD)); + builder.add( + new BooleanClause(new ConstantScoreQuery(new TermQuery(new Term(prefixFieldName, finalTerm))), BooleanClause.Occur.SHOULD) + ); return builder.build(); } @@ -623,15 +616,27 @@ public void testMultiMatchBoolPrefix() throws IOException { assertThat(actual, instanceOf(DisjunctionMaxQuery.class)); final DisjunctionMaxQuery disMaxQuery = (DisjunctionMaxQuery) actual; assertThat(disMaxQuery.getDisjuncts(), hasSize(4)); - assertThat(disMaxQuery.getDisjuncts(), containsInAnyOrder( - buildBoolPrefixQuery( - "field", "field._index_prefix", asList("quick", "brown", "fox", "jump", "lazy", "dog")), - buildBoolPrefixQuery("field._2gram", "field._index_prefix", - asList("quick brown", "brown fox", "fox jump", "jump lazy", "lazy dog")), - buildBoolPrefixQuery("field._3gram", "field._index_prefix", - asList("quick brown fox", "brown fox jump", "fox jump lazy", "jump lazy dog")), - buildBoolPrefixQuery("field._4gram", "field._index_prefix", - asList("quick brown fox jump", "brown fox jump lazy", "fox jump lazy dog")))); + assertThat( + disMaxQuery.getDisjuncts(), + containsInAnyOrder( + buildBoolPrefixQuery("field", "field._index_prefix", asList("quick", "brown", "fox", "jump", "lazy", "dog")), + buildBoolPrefixQuery( + "field._2gram", + "field._index_prefix", + asList("quick brown", "brown fox", "fox jump", "jump lazy", "lazy dog") + ), + buildBoolPrefixQuery( + "field._3gram", + "field._index_prefix", + asList("quick brown fox", "brown fox jump", "fox jump lazy", "jump lazy dog") + ), + buildBoolPrefixQuery( + "field._4gram", + "field._index_prefix", + asList("quick brown fox jump", "brown fox jump lazy", "fox jump lazy dog") + ) + ) + ); } private void documentParsingTestCase(Collection values) throws IOException { @@ -648,15 +653,13 @@ private void documentParsingTestCase(Collection values) throws IOExcepti IndexableField[] prefixFields = parsedDocument.rootDoc().getFields("field._index_prefix"); IndexableField[] shingle2Fields = parsedDocument.rootDoc().getFields("field._2gram"); IndexableField[] shingle3Fields = parsedDocument.rootDoc().getFields("field._3gram"); - for (IndexableField[] fields : new IndexableField[][]{rootFields, prefixFields, shingle2Fields, shingle3Fields}) { + for (IndexableField[] fields : new IndexableField[][] { rootFields, prefixFields, shingle2Fields, shingle3Fields }) { Set expectedValues = Arrays.stream(fields).map(IndexableField::stringValue).collect(Collectors.toSet()); assertThat(values, equalTo(expectedValues)); } } - private static void assertRootFieldMapper(SearchAsYouTypeFieldMapper mapper, - int maxShingleSize, - String analyzerName) { + private static void assertRootFieldMapper(SearchAsYouTypeFieldMapper mapper, int maxShingleSize, String analyzerName) { assertThat(mapper.maxShingleSize(), equalTo(maxShingleSize)); assertThat(mapper.fieldType(), notNullValue()); @@ -666,23 +669,29 @@ private static void assertRootFieldMapper(SearchAsYouTypeFieldMapper mapper, assertThat(mapper.prefixField().fieldType().parentField, equalTo(mapper.name())); assertPrefixFieldType(mapper.prefixField(), mapper.indexAnalyzers(), maxShingleSize, analyzerName); - for (int shingleSize = 2; shingleSize <= maxShingleSize; shingleSize++) { final ShingleFieldMapper shingleFieldMapper = mapper.shingleFields()[shingleSize - 2]; assertThat(shingleFieldMapper, notNullValue()); - assertShingleFieldType(shingleFieldMapper, mapper.indexAnalyzers(), shingleSize, - analyzerName, mapper.prefixField().fieldType()); + assertShingleFieldType( + shingleFieldMapper, + mapper.indexAnalyzers(), + shingleSize, + analyzerName, + mapper.prefixField().fieldType() + ); } final int numberOfShingleSubfields = (maxShingleSize - 2) + 1; assertThat(mapper.shingleFields().length, equalTo(numberOfShingleSubfields)); } - private static void assertSearchAsYouTypeFieldType(SearchAsYouTypeFieldMapper mapper, - SearchAsYouTypeFieldType fieldType, - int maxShingleSize, - String analyzerName, - PrefixFieldType prefixFieldType) { + private static void assertSearchAsYouTypeFieldType( + SearchAsYouTypeFieldMapper mapper, + SearchAsYouTypeFieldType fieldType, + int maxShingleSize, + String analyzerName, + PrefixFieldType prefixFieldType + ) { assertThat(fieldType.shingleFields.length, equalTo(maxShingleSize - 1)); NamedAnalyzer indexAnalyzer = mapper.indexAnalyzers().get(fieldType.name()); @@ -697,11 +706,13 @@ private static void assertSearchAsYouTypeFieldType(SearchAsYouTypeFieldMapper ma assertThat(fieldType.prefixField, equalTo(prefixFieldType)); } - private static void assertShingleFieldType(ShingleFieldMapper mapper, - Map indexAnalyzers, - int shingleSize, - String analyzerName, - PrefixFieldType prefixFieldType) { + private static void assertShingleFieldType( + ShingleFieldMapper mapper, + Map indexAnalyzers, + int shingleSize, + String analyzerName, + PrefixFieldType prefixFieldType + ) { ShingleFieldType fieldType = mapper.fieldType(); assertThat(fieldType.shingleSize, equalTo(shingleSize)); @@ -719,8 +730,12 @@ private static void assertShingleFieldType(ShingleFieldMapper mapper, } - private static void assertPrefixFieldType(PrefixFieldMapper mapper, Map indexAnalyzers, - int shingleSize, String analyzerName) { + private static void assertPrefixFieldType( + PrefixFieldMapper mapper, + Map indexAnalyzers, + int shingleSize, + String analyzerName + ) { PrefixFieldType fieldType = mapper.fieldType(); NamedAnalyzer indexAnalyzer = indexAnalyzers.get(fieldType.name()); for (NamedAnalyzer analyzer : asList(indexAnalyzer, fieldType.getTextSearchInfo().getSearchAnalyzer())) { @@ -728,8 +743,9 @@ private static void assertPrefixFieldType(PrefixFieldMapper mapper, Map unsearchable.termQuery("foo", null)); assertThat(e.getMessage(), equalTo("Cannot search on field [" + NAME + "] since it is not indexed.")); } @@ -74,13 +83,23 @@ public void testTermQuery() { public void testTermsQuery() { final MappedFieldType fieldType = createFieldType(); - assertThat(fieldType.termsQuery(asList("foo", "bar"), null), - equalTo(new TermInSetQuery(NAME, asList(new BytesRef("foo"), new BytesRef("bar"))))); - - SearchAsYouTypeFieldType unsearchable = new SearchAsYouTypeFieldType(NAME, UNSEARCHABLE, null, - Lucene.STANDARD_ANALYZER, Lucene.STANDARD_ANALYZER, Collections.emptyMap()); - final IllegalArgumentException e = - expectThrows(IllegalArgumentException.class, () -> unsearchable.termsQuery(asList("foo", "bar"), null)); + assertThat( + fieldType.termsQuery(asList("foo", "bar"), null), + equalTo(new TermInSetQuery(NAME, asList(new BytesRef("foo"), new BytesRef("bar")))) + ); + + SearchAsYouTypeFieldType unsearchable = new SearchAsYouTypeFieldType( + NAME, + UNSEARCHABLE, + null, + Lucene.STANDARD_ANALYZER, + Lucene.STANDARD_ANALYZER, + Collections.emptyMap() + ); + final IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> unsearchable.termsQuery(asList("foo", "bar"), null) + ); assertThat(e.getMessage(), equalTo("Cannot search on field [" + NAME + "] since it is not indexed.")); } @@ -89,20 +108,29 @@ public void testPrefixQuery() { // this term should be a length that can be rewriteable to a term query on the prefix field final String withinBoundsTerm = "foo"; - assertThat(fieldType.prefixQuery(withinBoundsTerm, CONSTANT_SCORE_REWRITE, randomMockContext()), - equalTo(new ConstantScoreQuery(new TermQuery(new Term(NAME + "._index_prefix", withinBoundsTerm))))); + assertThat( + fieldType.prefixQuery(withinBoundsTerm, CONSTANT_SCORE_REWRITE, randomMockContext()), + equalTo(new ConstantScoreQuery(new TermQuery(new Term(NAME + "._index_prefix", withinBoundsTerm)))) + ); // our defaults don't allow a situation where a term can be too small // this term should be too long to be rewriteable to a term query on the prefix field final String longTerm = "toolongforourprefixfieldthistermis"; - assertThat(fieldType.prefixQuery(longTerm, CONSTANT_SCORE_REWRITE, MOCK_CONTEXT), - equalTo(new PrefixQuery(new Term(NAME, longTerm)))); - - ElasticsearchException ee = expectThrows(ElasticsearchException.class, - () -> fieldType.prefixQuery(longTerm, CONSTANT_SCORE_REWRITE, MOCK_CONTEXT_DISALLOW_EXPENSIVE)); - assertEquals("[prefix] queries cannot be executed when 'search.allow_expensive_queries' is set to false. " + - "For optimised prefix queries on text fields please enable [index_prefixes].", ee.getMessage()); + assertThat( + fieldType.prefixQuery(longTerm, CONSTANT_SCORE_REWRITE, MOCK_CONTEXT), + equalTo(new PrefixQuery(new Term(NAME, longTerm))) + ); + + ElasticsearchException ee = expectThrows( + ElasticsearchException.class, + () -> fieldType.prefixQuery(longTerm, CONSTANT_SCORE_REWRITE, MOCK_CONTEXT_DISALLOW_EXPENSIVE) + ); + assertEquals( + "[prefix] queries cannot be executed when 'search.allow_expensive_queries' is set to false. " + + "For optimised prefix queries on text fields please enable [index_prefixes].", + ee.getMessage() + ); } public void testFetchSourceValue() throws IOException { @@ -113,13 +141,20 @@ public void testFetchSourceValue() throws IOException { assertEquals(List.of("true"), fetchSourceValue(fieldType, true)); SearchAsYouTypeFieldMapper.PrefixFieldType prefixFieldType = new SearchAsYouTypeFieldMapper.PrefixFieldType( - fieldType.name(), fieldType.getTextSearchInfo(), 2, 10); + fieldType.name(), + fieldType.getTextSearchInfo(), + 2, + 10 + ); assertEquals(List.of("value"), fetchSourceValue(prefixFieldType, "value")); assertEquals(List.of("42"), fetchSourceValue(prefixFieldType, 42L)); assertEquals(List.of("true"), fetchSourceValue(prefixFieldType, true)); SearchAsYouTypeFieldMapper.ShingleFieldType shingleFieldType = new SearchAsYouTypeFieldMapper.ShingleFieldType( - fieldType.name(), 5, fieldType.getTextSearchInfo()); + fieldType.name(), + 5, + fieldType.getTextSearchInfo() + ); assertEquals(List.of("value"), fetchSourceValue(shingleFieldType, "value")); assertEquals(List.of("42"), fetchSourceValue(shingleFieldType, 42L)); assertEquals(List.of("true"), fetchSourceValue(shingleFieldType, true)); diff --git a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/SourceConfirmedTextQueryTests.java b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/SourceConfirmedTextQueryTests.java index 4d20ecc32b2ca..c0ce863b06969 100644 --- a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/SourceConfirmedTextQueryTests.java +++ b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/SourceConfirmedTextQueryTests.java @@ -34,7 +34,6 @@ import org.elasticsearch.common.CheckedIntFunction; import org.elasticsearch.common.lucene.Lucene; import org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery; -import org.elasticsearch.index.mapper.extras.SourceConfirmedTextQuery; import org.elasticsearch.test.ESTestCase; import java.io.IOException; diff --git a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/SourceIntervalsSourceTests.java b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/SourceIntervalsSourceTests.java index def6afe0c835d..e1d05e4aec412 100644 --- a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/SourceIntervalsSourceTests.java +++ b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/SourceIntervalsSourceTests.java @@ -30,7 +30,6 @@ import org.apache.lucene.util.BytesRef; import org.elasticsearch.common.CheckedIntFunction; import org.elasticsearch.common.lucene.Lucene; -import org.elasticsearch.index.mapper.extras.SourceIntervalsSource; import org.elasticsearch.test.ESTestCase; import java.io.IOException; @@ -48,9 +47,14 @@ public void testIntervals() throws IOException { final FieldType ft = new FieldType(TextField.TYPE_STORED); ft.setIndexOptions(IndexOptions.DOCS); ft.freeze(); - try (Directory dir = newDirectory(); IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(Lucene.STANDARD_ANALYZER) - .setMergePolicy(NoMergePolicy.INSTANCE) - .setMaxBufferedDocs(IndexWriterConfig.DISABLE_AUTO_FLUSH))) { + try ( + Directory dir = newDirectory(); + IndexWriter w = new IndexWriter( + dir, + newIndexWriterConfig(Lucene.STANDARD_ANALYZER).setMergePolicy(NoMergePolicy.INSTANCE) + .setMaxBufferedDocs(IndexWriterConfig.DISABLE_AUTO_FLUSH) + ) + ) { Document doc = new Document(); doc.add(new Field("body", "a b", ft)); @@ -76,7 +80,8 @@ public void testIntervals() throws IOException { Intervals.term(new BytesRef("d")), new TermQuery(new Term("body", "d")), SOURCE_FETCHER_PROVIDER, - Lucene.STANDARD_ANALYZER); + Lucene.STANDARD_ANALYZER + ); IntervalIterator intervals = source.intervals("body", reader.leaves().get(0)); @@ -108,7 +113,8 @@ public void testIntervals() throws IOException { Intervals.term(new BytesRef("d")), new MatchAllDocsQuery(), SOURCE_FETCHER_PROVIDER, - Lucene.STANDARD_ANALYZER); + Lucene.STANDARD_ANALYZER + ); intervals = source.intervals("body", reader.leaves().get(0)); diff --git a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/TokenCountFieldMapperTests.java b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/TokenCountFieldMapperTests.java index c88487287971a..57bdc13d760a4 100644 --- a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/TokenCountFieldMapperTests.java +++ b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/TokenCountFieldMapperTests.java @@ -15,7 +15,6 @@ import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.core.KeywordAnalyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.analysis.AnalyzerScope; import org.elasticsearch.index.analysis.IndexAnalyzers; @@ -25,9 +24,8 @@ import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.MapperTestCase; import org.elasticsearch.index.mapper.SourceToParse; -import org.elasticsearch.index.mapper.extras.MapperExtrasPlugin; -import org.elasticsearch.index.mapper.extras.TokenCountFieldMapper; import org.elasticsearch.plugins.Plugin; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.Arrays; @@ -69,29 +67,18 @@ protected void registerParameters(ParameterChecker checker) throws IOException { checker.registerConflictCheck("doc_values", b -> b.field("doc_values", false)); checker.registerConflictCheck("null_value", b -> b.field("null_value", 1)); checker.registerConflictCheck("enable_position_increments", b -> b.field("enable_position_increments", false)); - checker.registerUpdateCheck( - this::minimalMapping, - b -> b.field("type", "token_count").field("analyzer", "standard"), - m -> { - TokenCountFieldMapper tcfm = (TokenCountFieldMapper) m; - assertThat(tcfm.analyzer(), equalTo("standard")); - }); + checker.registerUpdateCheck(this::minimalMapping, b -> b.field("type", "token_count").field("analyzer", "standard"), m -> { + TokenCountFieldMapper tcfm = (TokenCountFieldMapper) m; + assertThat(tcfm.analyzer(), equalTo("standard")); + }); } @Override protected IndexAnalyzers createIndexAnalyzers(IndexSettings indexSettings) { - NamedAnalyzer dflt = new NamedAnalyzer( - "default", - AnalyzerScope.INDEX, - new StandardAnalyzer() - ); + NamedAnalyzer dflt = new NamedAnalyzer("default", AnalyzerScope.INDEX, new StandardAnalyzer()); NamedAnalyzer standard = new NamedAnalyzer("standard", AnalyzerScope.INDEX, new StandardAnalyzer()); NamedAnalyzer keyword = new NamedAnalyzer("keyword", AnalyzerScope.INDEX, new KeywordAnalyzer()); - return new IndexAnalyzers( - Map.of("default", dflt, "standard", standard, "keyword", keyword), - Map.of(), - Map.of() - ); + return new IndexAnalyzers(Map.of("default", dflt, "standard", standard, "keyword", keyword), Map.of(), Map.of()); } /** @@ -126,7 +113,7 @@ private Analyzer createMockAnalyzer() { Token t3 = new Token(); t2.setPositionIncrement(2); // Funny token with more than one increment int finalTokenIncrement = 4; // Final token increment - Token[] tokens = new Token[] {t1, t2, t3}; + Token[] tokens = new Token[] { t1, t2, t3 }; Collections.shuffle(Arrays.asList(tokens), random()); final TokenStream tokenStream = new CannedTokenStream(finalTokenIncrement, 0, tokens); // TODO: we have no CannedAnalyzer? @@ -181,8 +168,7 @@ private SourceToParse createDocument(String fieldValue) throws Exception { } private LuceneDocument parseDocument(DocumentMapper mapper, SourceToParse request) { - return mapper.parse(request) - .docs().stream().findFirst().orElseThrow(() -> new IllegalStateException("Test object not parsed")); + return mapper.parse(request).docs().stream().findFirst().orElseThrow(() -> new IllegalStateException("Test object not parsed")); } @Override diff --git a/modules/mapper-extras/src/yamlRestTest/java/org/elasticsearch/index/mapper/MapperExtrasClientYamlTestSuiteIT.java b/modules/mapper-extras/src/yamlRestTest/java/org/elasticsearch/index/mapper/MapperExtrasClientYamlTestSuiteIT.java index 06ec7cfe0222a..44a01edaa3ff2 100644 --- a/modules/mapper-extras/src/yamlRestTest/java/org/elasticsearch/index/mapper/MapperExtrasClientYamlTestSuiteIT.java +++ b/modules/mapper-extras/src/yamlRestTest/java/org/elasticsearch/index/mapper/MapperExtrasClientYamlTestSuiteIT.java @@ -26,4 +26,3 @@ public static Iterable parameters() throws Exception { return ESClientYamlSuiteTestCase.createParameters(); } } - diff --git a/modules/parent-join/src/internalClusterTest/java/org/elasticsearch/join/aggregations/AbstractParentChildTestCase.java b/modules/parent-join/src/internalClusterTest/java/org/elasticsearch/join/aggregations/AbstractParentChildTestCase.java index c2bef3ea5fe80..03c64174af2c5 100644 --- a/modules/parent-join/src/internalClusterTest/java/org/elasticsearch/join/aggregations/AbstractParentChildTestCase.java +++ b/modules/parent-join/src/internalClusterTest/java/org/elasticsearch/join/aggregations/AbstractParentChildTestCase.java @@ -31,10 +31,15 @@ public abstract class AbstractParentChildTestCase extends ParentChildTestCase { @Before public void setupCluster() throws Exception { assertAcked( - prepareCreate("test") - .setMapping( - addFieldMappings(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "article", "comment"), - "commenter", "keyword", "category", "keyword")) + prepareCreate("test").setMapping( + addFieldMappings( + buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "article", "comment"), + "commenter", + "keyword", + "category", + "keyword" + ) + ) ); List requests = new ArrayList<>(); @@ -49,7 +54,7 @@ public void setupCluster() throws Exception { String id = "article-" + i; // TODO: this array is always of length 1, and testChildrenAggs fails if this is changed - String[] categories = new String[randomIntBetween(1,1)]; + String[] categories = new String[randomIntBetween(1, 1)]; for (int j = 0; j < categories.length; j++) { String category = categories[j] = uniqueCategories[catIndex++ % uniqueCategories.length]; Control control = categoryToControl.computeIfAbsent(category, Control::new); @@ -79,17 +84,25 @@ public void setupCluster() throws Exception { articleToControl.get(articleId).commentIds.add(idValue); - IndexRequestBuilder indexRequest = createIndexRequest("test", "comment", idValue, - articleId, "commenter", commenter, "randomized", true); + IndexRequestBuilder indexRequest = createIndexRequest( + "test", + "comment", + idValue, + articleId, + "commenter", + commenter, + "randomized", + true + ); requests.add(indexRequest); } } } - requests.add(createIndexRequest("test", "article", "a", null, "category", new String[]{"a"}, "randomized", false)); - requests.add(createIndexRequest("test", "article", "b", null, "category", new String[]{"a", "b"}, "randomized", false)); - requests.add(createIndexRequest("test", "article", "c", null, "category", new String[]{"a", "b", "c"}, "randomized", false)); - requests.add(createIndexRequest("test", "article", "d", null, "category", new String[]{"c"}, "randomized", false)); + requests.add(createIndexRequest("test", "article", "a", null, "category", new String[] { "a" }, "randomized", false)); + requests.add(createIndexRequest("test", "article", "b", null, "category", new String[] { "a", "b" }, "randomized", false)); + requests.add(createIndexRequest("test", "article", "c", null, "category", new String[] { "a", "b", "c" }, "randomized", false)); + requests.add(createIndexRequest("test", "article", "d", null, "category", new String[] { "c" }, "randomized", false)); requests.add(createIndexRequest("test", "comment", "e", "a")); requests.add(createIndexRequest("test", "comment", "f", "c")); @@ -97,7 +110,6 @@ public void setupCluster() throws Exception { ensureSearchable("test"); } - protected static final class Control { final String category; diff --git a/modules/parent-join/src/internalClusterTest/java/org/elasticsearch/join/aggregations/ChildrenIT.java b/modules/parent-join/src/internalClusterTest/java/org/elasticsearch/join/aggregations/ChildrenIT.java index 62bc75cea65fe..5386d54a293a1 100644 --- a/modules/parent-join/src/internalClusterTest/java/org/elasticsearch/join/aggregations/ChildrenIT.java +++ b/modules/parent-join/src/internalClusterTest/java/org/elasticsearch/join/aggregations/ChildrenIT.java @@ -47,15 +47,17 @@ public class ChildrenIT extends AbstractParentChildTestCase { public void testChildrenAggs() throws Exception { SearchResponse searchResponse = client().prepareSearch("test") - .setQuery(matchQuery("randomized", true)) - .addAggregation( - terms("category").field("category").size(10000).subAggregation(children("to_comment", "comment") - .subAggregation( - terms("commenters").field("commenter").size(10000).subAggregation( - topHits("top_comments") - )) + .setQuery(matchQuery("randomized", true)) + .addAggregation( + terms("category").field("category") + .size(10000) + .subAggregation( + children("to_comment", "comment").subAggregation( + terms("commenters").field("commenter").size(10000).subAggregation(topHits("top_comments")) ) - ).get(); + ) + ) + .get(); assertSearchResponse(searchResponse); Terms categoryTerms = searchResponse.getAggregations().get("category"); @@ -68,11 +70,10 @@ public void testChildrenAggs() throws Exception { Children childrenBucket = categoryBucket.getAggregations().get("to_comment"); assertThat(childrenBucket.getName(), equalTo("to_comment")); assertThat(childrenBucket.getDocCount(), equalTo((long) entry1.getValue().commentIds.size())); - assertThat(((InternalAggregation)childrenBucket).getProperty("_count"), - equalTo((long) entry1.getValue().commentIds.size())); + assertThat(((InternalAggregation) childrenBucket).getProperty("_count"), equalTo((long) entry1.getValue().commentIds.size())); Terms commentersTerms = childrenBucket.getAggregations().get("commenters"); - assertThat(((InternalAggregation)childrenBucket).getProperty("commenters"), sameInstance(commentersTerms)); + assertThat(((InternalAggregation) childrenBucket).getProperty("commenters"), sameInstance(commentersTerms)); assertThat(commentersTerms.getBuckets().size(), equalTo(entry1.getValue().commenterToCommentId.size())); for (Map.Entry> entry2 : entry1.getValue().commenterToCommentId.entrySet()) { Terms.Bucket commentBucket = commentersTerms.getBucketByKey(entry2.getKey()); @@ -89,12 +90,13 @@ public void testChildrenAggs() throws Exception { public void testParentWithMultipleBuckets() throws Exception { SearchResponse searchResponse = client().prepareSearch("test") - .setQuery(matchQuery("randomized", false)) - .addAggregation( - terms("category").field("category").size(10000).subAggregation( - children("to_comment", "comment").subAggregation(topHits("top_comments").sort("id", SortOrder.ASC)) - ) - ).get(); + .setQuery(matchQuery("randomized", false)) + .addAggregation( + terms("category").field("category") + .size(10000) + .subAggregation(children("to_comment", "comment").subAggregation(topHits("top_comments").sort("id", SortOrder.ASC))) + ) + .get(); assertSearchResponse(searchResponse); Terms categoryTerms = searchResponse.getAggregations().get("category"); @@ -148,10 +150,9 @@ public void testParentWithMultipleBuckets() throws Exception { public void testWithDeletes() throws Exception { String indexName = "xyz"; assertAcked( - prepareCreate(indexName) - .setMapping( - addFieldMappings(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"), - "name", "keyword")) + prepareCreate(indexName).setMapping( + addFieldMappings(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"), "name", "keyword") + ) ); List requests = new ArrayList<>(); @@ -164,8 +165,8 @@ public void testWithDeletes() throws Exception { for (int i = 0; i < 10; i++) { SearchResponse searchResponse = client().prepareSearch(indexName) - .addAggregation(children("children", "child").subAggregation(sum("counts").field("count"))) - .get(); + .addAggregation(children("children", "child").subAggregation(sum("counts").field("count"))) + .get(); assertNoFailures(searchResponse); Children children = searchResponse.getAggregations().get("children"); @@ -182,20 +183,17 @@ public void testWithDeletes() throws Exception { */ UpdateResponse updateResponse; updateResponse = client().prepareUpdate(indexName, idToUpdate) - .setRouting("1") - .setDoc(Requests.INDEX_CONTENT_TYPE, "count", 1) - .setDetectNoop(false) - .get(); + .setRouting("1") + .setDoc(Requests.INDEX_CONTENT_TYPE, "count", 1) + .setDetectNoop(false) + .get(); assertThat(updateResponse.getVersion(), greaterThan(1L)); refresh(); } } public void testNonExistingChildType() throws Exception { - SearchResponse searchResponse = client().prepareSearch("test") - .addAggregation( - children("non-existing", "xyz") - ).get(); + SearchResponse searchResponse = client().prepareSearch("test").addAggregation(children("non-existing", "xyz")).get(); assertSearchResponse(searchResponse); Children children = searchResponse.getAggregations().get("non-existing"); @@ -208,18 +206,28 @@ public void testPostCollection() throws Exception { String masterType = "masterprod"; String childType = "variantsku"; assertAcked( - prepareCreate(indexName) - .setSettings(Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)) - .setMapping( - addFieldMappings(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, - masterType, childType), - "brand", "text", "name", "keyword", "material", "text", "color", "keyword", "size", "keyword")) + prepareCreate(indexName).setSettings( + Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + ) + .setMapping( + addFieldMappings( + buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, masterType, childType), + "brand", + "text", + "name", + "keyword", + "material", + "text", + "color", + "keyword", + "size", + "keyword" + ) + ) ); List requests = new ArrayList<>(); - requests.add(createIndexRequest(indexName, masterType, "1", null, "brand", "Levis", "name", - "Style 501", "material", "Denim")); + requests.add(createIndexRequest(indexName, masterType, "1", null, "brand", "Levis", "name", "Style 501", "material", "Denim")); requests.add(createIndexRequest(indexName, childType, "3", "1", "color", "blue", "size", "32")); requests.add(createIndexRequest(indexName, childType, "4", "1", "color", "blue", "size", "34")); requests.add(createIndexRequest(indexName, childType, "5", "1", "color", "blue", "size", "36")); @@ -227,8 +235,9 @@ public void testPostCollection() throws Exception { requests.add(createIndexRequest(indexName, childType, "7", "1", "color", "black", "size", "40")); requests.add(createIndexRequest(indexName, childType, "8", "1", "color", "gray", "size", "36")); - requests.add(createIndexRequest(indexName, masterType, "2", null, "brand", "Wrangler", "name", - "Regular Cut", "material", "Leather")); + requests.add( + createIndexRequest(indexName, masterType, "2", null, "brand", "Wrangler", "name", "Regular Cut", "material", "Leather") + ); requests.add(createIndexRequest(indexName, childType, "9", "2", "color", "blue", "size", "32")); requests.add(createIndexRequest(indexName, childType, "10", "2", "color", "blue", "size", "34")); requests.add(createIndexRequest(indexName, childType, "12", "2", "color", "black", "size", "36")); @@ -239,11 +248,12 @@ public void testPostCollection() throws Exception { indexRandom(true, requests); SearchResponse response = client().prepareSearch(indexName) - .setQuery(hasChildQuery(childType, termQuery("color", "orange"), ScoreMode.None)) - .addAggregation(children("my-refinements", childType) - .subAggregation(terms("my-colors").field("color")) - .subAggregation(terms("my-sizes").field("size")) - ).get(); + .setQuery(hasChildQuery(childType, termQuery("color", "orange"), ScoreMode.None)) + .addAggregation( + children("my-refinements", childType).subAggregation(terms("my-colors").field("color")) + .subAggregation(terms("my-sizes").field("size")) + ) + .get(); assertNoFailures(response); assertHitCount(response, 1); @@ -273,11 +283,13 @@ public void testHierarchicalChildrenAggs() { String parentType = "country"; String childType = "city"; assertAcked( - prepareCreate(indexName) - .setMapping( - addFieldMappings(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, - grandParentType, parentType, parentType, childType), - "name", "keyword")) + prepareCreate(indexName).setMapping( + addFieldMappings( + buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, grandParentType, parentType, parentType, childType), + "name", + "keyword" + ) + ) ); createIndexRequest(indexName, grandParentType, "1", null, "name", "europe").get(); @@ -286,14 +298,11 @@ public void testHierarchicalChildrenAggs() { refresh(); SearchResponse response = client().prepareSearch(indexName) - .setQuery(matchQuery("name", "europe")) - .addAggregation( - children(parentType, parentType).subAggregation(children(childType, childType).subAggregation( - terms("name").field("name") - ) - ) - ) - .get(); + .setQuery(matchQuery("name", "europe")) + .addAggregation( + children(parentType, parentType).subAggregation(children(childType, childType).subAggregation(terms("name").field("name"))) + ) + .get(); assertNoFailures(response); assertHitCount(response, 1); @@ -316,11 +325,17 @@ public void testPostCollectAllLeafReaders() throws Exception { // Before we only evaluated segments that yielded matches in 'towns' and 'parent_names' aggs, which caused // us to miss to evaluate child docs in segments we didn't have parent matches for. assertAcked( - prepareCreate("index") - .setMapping( - addFieldMappings(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, - "parentType", "childType"), - "name", "keyword", "town", "keyword", "age", "integer")) + prepareCreate("index").setMapping( + addFieldMappings( + buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parentType", "childType"), + "name", + "keyword", + "town", + "keyword", + "age", + "integer" + ) + ) ); List requests = new ArrayList<>(); requests.add(createIndexRequest("index", "parentType", "1", null, "name", "Bob", "town", "Memphis")); @@ -335,11 +350,14 @@ public void testPostCollectAllLeafReaders() throws Exception { SearchResponse response = client().prepareSearch("index") .setSize(0) - .addAggregation(AggregationBuilders.terms("towns").field("town") - .subAggregation(AggregationBuilders.terms("parent_names").field("name") - .subAggregation(children("child_docs", "childType")) - ) - ).get(); + .addAggregation( + AggregationBuilders.terms("towns") + .field("town") + .subAggregation( + AggregationBuilders.terms("parent_names").field("name").subAggregation(children("child_docs", "childType")) + ) + ) + .get(); Terms towns = response.getAggregations().get("towns"); assertThat(towns.getBuckets().size(), equalTo(2)); diff --git a/modules/parent-join/src/internalClusterTest/java/org/elasticsearch/join/aggregations/ParentIT.java b/modules/parent-join/src/internalClusterTest/java/org/elasticsearch/join/aggregations/ParentIT.java index ee628e41998ba..5c409179f4e18 100644 --- a/modules/parent-join/src/internalClusterTest/java/org/elasticsearch/join/aggregations/ParentIT.java +++ b/modules/parent-join/src/internalClusterTest/java/org/elasticsearch/join/aggregations/ParentIT.java @@ -36,49 +36,54 @@ public void testSimpleParentAgg() throws Exception { final SearchRequestBuilder searchRequest = client().prepareSearch("test") .setSize(10000) .setQuery(matchQuery("randomized", true)) - .addAggregation( - parent("to_article", "comment") - .subAggregation( - terms("category").field("category").size(10000))); + .addAggregation(parent("to_article", "comment").subAggregation(terms("category").field("category").size(10000))); SearchResponse searchResponse = searchRequest.get(); assertSearchResponse(searchResponse); - long articlesWithComment = articleToControl.values().stream().filter( - parentControl -> parentControl.commentIds.isEmpty() == false - ).count(); + long articlesWithComment = articleToControl.values() + .stream() + .filter(parentControl -> parentControl.commentIds.isEmpty() == false) + .count(); Parent parentAgg = searchResponse.getAggregations().get("to_article"); - assertThat("Request: " + searchRequest + "\nResponse: " + searchResponse + "\n", - parentAgg.getDocCount(), equalTo(articlesWithComment)); + assertThat( + "Request: " + searchRequest + "\nResponse: " + searchResponse + "\n", + parentAgg.getDocCount(), + equalTo(articlesWithComment) + ); Terms categoryTerms = parentAgg.getAggregations().get("category"); - long categoriesWithComments = categoryToControl.values().stream().filter( - control -> control.commentIds.isEmpty() == false).count(); - assertThat("Buckets: " + categoryTerms.getBuckets().stream().map( - (Function) MultiBucketsAggregation.Bucket::getKeyAsString).collect(Collectors.toList()) + - "\nCategories: " + categoryToControl.keySet(), - (long)categoryTerms.getBuckets().size(), equalTo(categoriesWithComments)); + long categoriesWithComments = categoryToControl.values().stream().filter(control -> control.commentIds.isEmpty() == false).count(); + assertThat( + "Buckets: " + + categoryTerms.getBuckets() + .stream() + .map((Function) MultiBucketsAggregation.Bucket::getKeyAsString) + .collect(Collectors.toList()) + + "\nCategories: " + + categoryToControl.keySet(), + (long) categoryTerms.getBuckets().size(), + equalTo(categoriesWithComments) + ); for (Map.Entry entry : categoryToControl.entrySet()) { // no children for this category -> no entry in the child to parent-aggregation - if(entry.getValue().commentIds.isEmpty()) { + if (entry.getValue().commentIds.isEmpty()) { assertNull(categoryTerms.getBucketByKey(entry.getKey())); continue; } final Terms.Bucket categoryBucket = categoryTerms.getBucketByKey(entry.getKey()); - assertNotNull("Failed for category " + entry.getKey(), - categoryBucket); - assertThat("Failed for category " + entry.getKey(), - categoryBucket.getKeyAsString(), equalTo(entry.getKey())); + assertNotNull("Failed for category " + entry.getKey(), categoryBucket); + assertThat("Failed for category " + entry.getKey(), categoryBucket.getKeyAsString(), equalTo(entry.getKey())); // count all articles in this category which have at least one comment - long articlesForCategory = articleToControl.values().stream() + long articlesForCategory = articleToControl.values() + .stream() // only articles with this category .filter(parentControl -> parentControl.category.equals(entry.getKey())) // only articles which have comments .filter(parentControl -> parentControl.commentIds.isEmpty() == false) .count(); - assertThat("Failed for category " + entry.getKey(), - categoryBucket.getDocCount(), equalTo(articlesForCategory)); + assertThat("Failed for category " + entry.getKey(), categoryBucket.getDocCount(), equalTo(articlesForCategory)); } } @@ -87,12 +92,13 @@ public void testParentAggs() throws Exception { .setSize(10000) .setQuery(matchQuery("randomized", true)) .addAggregation( - terms("to_commenter").field("commenter").size(10000).subAggregation( - parent("to_article", "comment").subAggregation( - terms("to_category").field("category").size(10000).subAggregation( - topHits("top_category") - )) - ) + terms("to_commenter").field("commenter") + .size(10000) + .subAggregation( + parent("to_article", "comment").subAggregation( + terms("to_category").field("category").size(10000).subAggregation(topHits("top_category")) + ) + ) ); SearchResponse searchResponse = searchRequest.get(); assertSearchResponse(searchResponse); @@ -101,32 +107,40 @@ public void testParentAggs() throws Exception { final Map> commenterToComments = getCommenterToComments(); Terms categoryTerms = searchResponse.getAggregations().get("to_commenter"); - assertThat("Request: " + searchRequest + "\nResponse: " + searchResponse + "\n", - categoryTerms.getBuckets().size(), equalTo(commenters.size())); + assertThat( + "Request: " + searchRequest + "\nResponse: " + searchResponse + "\n", + categoryTerms.getBuckets().size(), + equalTo(commenters.size()) + ); for (Terms.Bucket commenterBucket : categoryTerms.getBuckets()) { Set comments = commenterToComments.get(commenterBucket.getKeyAsString()); assertNotNull(comments); - assertThat("Failed for commenter " + commenterBucket.getKeyAsString(), - commenterBucket.getDocCount(), equalTo((long)comments.size())); + assertThat( + "Failed for commenter " + commenterBucket.getKeyAsString(), + commenterBucket.getDocCount(), + equalTo((long) comments.size()) + ); Parent articleAgg = commenterBucket.getAggregations().get("to_article"); assertThat(articleAgg.getName(), equalTo("to_article")); // find all articles for the comments for the current commenter - Set articles = articleToControl.values().stream().flatMap( - (Function>) parentControl -> parentControl.commentIds.stream(). - filter(comments::contains) - ).collect(Collectors.toSet()); + Set articles = articleToControl.values() + .stream() + .flatMap( + (Function>) parentControl -> parentControl.commentIds.stream().filter(comments::contains) + ) + .collect(Collectors.toSet()); - assertThat(articleAgg.getDocCount(), equalTo((long)articles.size())); + assertThat(articleAgg.getDocCount(), equalTo((long) articles.size())); Terms categoryAgg = articleAgg.getAggregations().get("to_category"); assertNotNull(categoryAgg); - List categories = categoryToControl.entrySet(). - stream(). - filter(entry -> entry.getValue().commenterToCommentId.containsKey(commenterBucket.getKeyAsString())). - map(Map.Entry::getKey). - collect(Collectors.toList()); + List categories = categoryToControl.entrySet() + .stream() + .filter(entry -> entry.getValue().commenterToCommentId.containsKey(commenterBucket.getKeyAsString())) + .map(Map.Entry::getKey) + .collect(Collectors.toList()); for (String category : categories) { Terms.Bucket categoryBucket = categoryAgg.getBucketByKey(category); @@ -148,9 +162,10 @@ public void testParentAggs() throws Exception { } private Set getCommenters() { - return categoryToControl.values().stream().flatMap( - (Function>) control -> control.commenterToCommentId.keySet().stream()). - collect(Collectors.toSet()); + return categoryToControl.values() + .stream() + .flatMap((Function>) control -> control.commenterToCommentId.keySet().stream()) + .collect(Collectors.toSet()); } private Map> getCommenterToComments() { @@ -165,10 +180,7 @@ private Map> getCommenterToComments() { } public void testNonExistingParentType() throws Exception { - SearchResponse searchResponse = client().prepareSearch("test") - .addAggregation( - parent("non-existing", "xyz") - ).get(); + SearchResponse searchResponse = client().prepareSearch("test").addAggregation(parent("non-existing", "xyz")).get(); assertSearchResponse(searchResponse); Parent parent = searchResponse.getAggregations().get("non-existing"); @@ -181,9 +193,10 @@ public void testTermsParentAggTerms() throws Exception { .setSize(10000) .setQuery(matchQuery("randomized", true)) .addAggregation( - terms("to_commenter").field("commenter").size(10000).subAggregation( - parent("to_article", "comment").subAggregation( - terms("to_category").field("category").size(10000)))); + terms("to_commenter").field("commenter") + .size(10000) + .subAggregation(parent("to_article", "comment").subAggregation(terms("to_category").field("category").size(10000))) + ); SearchResponse searchResponse = searchRequest.get(); assertSearchResponse(searchResponse); @@ -191,32 +204,40 @@ public void testTermsParentAggTerms() throws Exception { final Map> commenterToComments = getCommenterToComments(); Terms commentersAgg = searchResponse.getAggregations().get("to_commenter"); - assertThat("Request: " + searchRequest + "\nResponse: " + searchResponse + "\n", - commentersAgg.getBuckets().size(), equalTo(commenters.size())); + assertThat( + "Request: " + searchRequest + "\nResponse: " + searchResponse + "\n", + commentersAgg.getBuckets().size(), + equalTo(commenters.size()) + ); for (Terms.Bucket commenterBucket : commentersAgg.getBuckets()) { Set comments = commenterToComments.get(commenterBucket.getKeyAsString()); assertNotNull(comments); - assertThat("Failed for commenter " + commenterBucket.getKeyAsString(), - commenterBucket.getDocCount(), equalTo((long)comments.size())); + assertThat( + "Failed for commenter " + commenterBucket.getKeyAsString(), + commenterBucket.getDocCount(), + equalTo((long) comments.size()) + ); Parent articleAgg = commenterBucket.getAggregations().get("to_article"); assertThat(articleAgg.getName(), equalTo("to_article")); // find all articles for the comments for the current commenter - Set articles = articleToControl.values().stream().flatMap( - (Function>) parentControl -> parentControl.commentIds.stream(). - filter(comments::contains) - ).collect(Collectors.toSet()); + Set articles = articleToControl.values() + .stream() + .flatMap( + (Function>) parentControl -> parentControl.commentIds.stream().filter(comments::contains) + ) + .collect(Collectors.toSet()); - assertThat(articleAgg.getDocCount(), equalTo((long)articles.size())); + assertThat(articleAgg.getDocCount(), equalTo((long) articles.size())); Terms categoryAgg = articleAgg.getAggregations().get("to_category"); assertNotNull(categoryAgg); - List categories = categoryToControl.entrySet(). - stream(). - filter(entry -> entry.getValue().commenterToCommentId.containsKey(commenterBucket.getKeyAsString())). - map(Map.Entry::getKey). - collect(Collectors.toList()); + List categories = categoryToControl.entrySet() + .stream() + .filter(entry -> entry.getValue().commenterToCommentId.containsKey(commenterBucket.getKeyAsString())) + .map(Map.Entry::getKey) + .collect(Collectors.toList()); for (String category : categories) { Terms.Bucket categoryBucket = categoryAgg.getBucketByKey(category); diff --git a/modules/parent-join/src/internalClusterTest/java/org/elasticsearch/join/query/ChildQuerySearchIT.java b/modules/parent-join/src/internalClusterTest/java/org/elasticsearch/join/query/ChildQuerySearchIT.java index 2a0ecbd9d1a0c..6ef0ca4c5b7a6 100644 --- a/modules/parent-join/src/internalClusterTest/java/org/elasticsearch/join/query/ChildQuerySearchIT.java +++ b/modules/parent-join/src/internalClusterTest/java/org/elasticsearch/join/query/ChildQuerySearchIT.java @@ -48,7 +48,6 @@ import java.util.Map; import java.util.Set; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.common.xcontent.support.XContentMapValues.extractValue; import static org.elasticsearch.index.query.QueryBuilders.boolQuery; import static org.elasticsearch.index.query.QueryBuilders.constantScoreQuery; @@ -69,6 +68,7 @@ import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHit; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.hasId; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.Matchers.anyOf; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; @@ -77,9 +77,11 @@ public class ChildQuerySearchIT extends ParentChildTestCase { public void testMultiLevelChild() throws Exception { - assertAcked(prepareCreate("test") - .setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, - "parent", "child", "child", "grandchild"))); + assertAcked( + prepareCreate("test").setMapping( + buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child", "child", "grandchild") + ) + ); ensureGreen(); createIndexRequest("test", "parent", "p1", null, "p_field", "p_value1").get(); @@ -87,46 +89,51 @@ public void testMultiLevelChild() throws Exception { createIndexRequest("test", "grandchild", "gc1", "c1", "gc_field", "gc_value1").setRouting("p1").get(); refresh(); - SearchResponse searchResponse = client() - .prepareSearch("test") - .setQuery( - boolQuery() - .must(matchAllQuery()) - .filter(hasChildQuery( - "child", - boolQuery().must(termQuery("c_field", "c_value1")) - .filter(hasChildQuery("grandchild", termQuery("gc_field", "gc_value1"), ScoreMode.None)) - , ScoreMode.None))).get(); + SearchResponse searchResponse = client().prepareSearch("test") + .setQuery( + boolQuery().must(matchAllQuery()) + .filter( + hasChildQuery( + "child", + boolQuery().must(termQuery("c_field", "c_value1")) + .filter(hasChildQuery("grandchild", termQuery("gc_field", "gc_value1"), ScoreMode.None)), + ScoreMode.None + ) + ) + ) + .get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("p1")); searchResponse = client().prepareSearch("test") - .setQuery(boolQuery().must(matchAllQuery()) - .filter(hasParentQuery("parent", termQuery("p_field", "p_value1"), false))).execute() - .actionGet(); + .setQuery(boolQuery().must(matchAllQuery()).filter(hasParentQuery("parent", termQuery("p_field", "p_value1"), false))) + .execute() + .actionGet(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("c1")); searchResponse = client().prepareSearch("test") - .setQuery(boolQuery().must(matchAllQuery()) - .filter(hasParentQuery("child", termQuery("c_field", "c_value1"), false))).execute() - .actionGet(); + .setQuery(boolQuery().must(matchAllQuery()).filter(hasParentQuery("child", termQuery("c_field", "c_value1"), false))) + .execute() + .actionGet(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("gc1")); searchResponse = client().prepareSearch("test") - .setQuery(hasParentQuery("parent", termQuery("p_field", "p_value1"), false)).execute() - .actionGet(); + .setQuery(hasParentQuery("parent", termQuery("p_field", "p_value1"), false)) + .execute() + .actionGet(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("c1")); searchResponse = client().prepareSearch("test") - .setQuery(hasParentQuery("child", termQuery("c_field", "c_value1"), false)).execute() - .actionGet(); + .setQuery(hasParentQuery("child", termQuery("c_field", "c_value1"), false)) + .execute() + .actionGet(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("gc1")); @@ -134,17 +141,16 @@ public void testMultiLevelChild() throws Exception { // see #2744 public void test2744() throws IOException { - assertAcked(prepareCreate("test") - .setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "foo", "test"))); + assertAcked(prepareCreate("test").setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "foo", "test"))); ensureGreen(); // index simple data createIndexRequest("test", "foo", "1", null, "foo", 1).get(); createIndexRequest("test", "test", "2", "1", "foo", 1).get(); refresh(); - SearchResponse searchResponse = client().prepareSearch("test"). - setQuery(hasChildQuery("test", matchQuery("foo", 1), ScoreMode.None)) - .get(); + SearchResponse searchResponse = client().prepareSearch("test") + .setQuery(hasChildQuery("test", matchQuery("foo", 1), ScoreMode.None)) + .get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("1")); @@ -152,8 +158,7 @@ public void test2744() throws IOException { } public void testSimpleChildQuery() throws Exception { - assertAcked(prepareCreate("test") - .setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); + assertAcked(prepareCreate("test").setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); ensureGreen(); // index simple data @@ -167,8 +172,7 @@ public void testSimpleChildQuery() throws Exception { // TEST FETCHING _parent from child SearchResponse searchResponse; - searchResponse = client().prepareSearch("test") - .setQuery(idsQuery().addIds("c1")).get(); + searchResponse = client().prepareSearch("test").setQuery(idsQuery().addIds("c1")).get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("c1")); @@ -177,8 +181,8 @@ public void testSimpleChildQuery() throws Exception { // TEST matching on parent searchResponse = client().prepareSearch("test") - .setQuery(boolQuery().filter(termQuery("join_field#parent", "p1")).filter(termQuery("join_field", "child"))) - .get(); + .setQuery(boolQuery().filter(termQuery("join_field#parent", "p1")).filter(termQuery("join_field", "child"))) + .get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(2L)); assertThat(searchResponse.getHits().getAt(0).getId(), anyOf(equalTo("c1"), equalTo("c2"))); @@ -189,14 +193,12 @@ public void testSimpleChildQuery() throws Exception { assertThat(extractValue("join_field.parent", searchResponse.getHits().getAt(1).getSourceAsMap()), equalTo("p1")); // HAS CHILD - searchResponse = client().prepareSearch("test").setQuery(randomHasChild("child", "c_field", "yellow")) - .get(); + searchResponse = client().prepareSearch("test").setQuery(randomHasChild("child", "c_field", "yellow")).get(); assertHitCount(searchResponse, 1L); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("p1")); - searchResponse = client().prepareSearch("test").setQuery(randomHasChild("child", "c_field", "blue")).execute() - .actionGet(); + searchResponse = client().prepareSearch("test").setQuery(randomHasChild("child", "c_field", "blue")).execute().actionGet(); assertHitCount(searchResponse, 1L); assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("p2")); @@ -206,15 +208,13 @@ public void testSimpleChildQuery() throws Exception { assertThat(searchResponse.getHits().getAt(1).getId(), anyOf(equalTo("p2"), equalTo("p1"))); // HAS PARENT - searchResponse = client().prepareSearch("test") - .setQuery(randomHasParent("parent", "p_field", "p_value2")).get(); + searchResponse = client().prepareSearch("test").setQuery(randomHasParent("parent", "p_field", "p_value2")).get(); assertNoFailures(searchResponse); assertHitCount(searchResponse, 2L); assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("c3")); assertThat(searchResponse.getHits().getAt(1).getId(), equalTo("c4")); - searchResponse = client().prepareSearch("test") - .setQuery(randomHasParent("parent", "p_field", "p_value1")).get(); + searchResponse = client().prepareSearch("test").setQuery(randomHasParent("parent", "p_field", "p_value1")).get(); assertHitCount(searchResponse, 2L); assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("c1")); assertThat(searchResponse.getHits().getAt(1).getId(), equalTo("c2")); @@ -222,8 +222,7 @@ public void testSimpleChildQuery() throws Exception { // Issue #3290 public void testCachingBugWithFqueryFilter() throws Exception { - assertAcked(prepareCreate("test") - .setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); + assertAcked(prepareCreate("test").setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); ensureGreen(); List builders = new ArrayList<>(); // index simple data @@ -249,19 +248,18 @@ public void testCachingBugWithFqueryFilter() throws Exception { for (int i = 1; i <= 10; i++) { logger.info("Round {}", i); SearchResponse searchResponse = client().prepareSearch("test") - .setQuery(constantScoreQuery(hasChildQuery("child", matchAllQuery(), ScoreMode.Max))) - .get(); + .setQuery(constantScoreQuery(hasChildQuery("child", matchAllQuery(), ScoreMode.Max))) + .get(); assertNoFailures(searchResponse); searchResponse = client().prepareSearch("test") - .setQuery(constantScoreQuery(hasParentQuery("parent", matchAllQuery(), true))) - .get(); + .setQuery(constantScoreQuery(hasParentQuery("parent", matchAllQuery(), true))) + .get(); assertNoFailures(searchResponse); } } public void testHasParentFilter() throws Exception { - assertAcked(prepareCreate("test") - .setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); + assertAcked(prepareCreate("test").setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); ensureGreen(); Map> parentToChildren = new HashMap<>(); // Childless parent @@ -293,8 +291,9 @@ public void testHasParentFilter() throws Exception { assertThat(parentToChildren.isEmpty(), equalTo(false)); for (Map.Entry> parentToChildrenEntry : parentToChildren.entrySet()) { SearchResponse searchResponse = client().prepareSearch("test") - .setQuery(constantScoreQuery(hasParentQuery("parent", termQuery("p_field", parentToChildrenEntry.getKey()), false))) - .setSize(numChildDocsPerParent).get(); + .setQuery(constantScoreQuery(hasParentQuery("parent", termQuery("p_field", parentToChildrenEntry.getKey()), false))) + .setSize(numChildDocsPerParent) + .get(); assertNoFailures(searchResponse); Set childIds = parentToChildrenEntry.getValue(); @@ -308,8 +307,7 @@ public void testHasParentFilter() throws Exception { } public void testSimpleChildQueryWithFlush() throws Exception { - assertAcked(prepareCreate("test") - .setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); + assertAcked(prepareCreate("test").setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); ensureGreen(); // index simple data with flushes, so we have many segments @@ -330,22 +328,20 @@ public void testSimpleChildQueryWithFlush() throws Exception { // HAS CHILD QUERY SearchResponse searchResponse = client().prepareSearch("test") - .setQuery(hasChildQuery("child", termQuery("c_field", "yellow"), ScoreMode.None)) - .get(); + .setQuery(hasChildQuery("child", termQuery("c_field", "yellow"), ScoreMode.None)) + .get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("p1")); searchResponse = client().prepareSearch("test") - .setQuery(hasChildQuery("child", termQuery("c_field", "blue"), ScoreMode.None)) - .get(); + .setQuery(hasChildQuery("child", termQuery("c_field", "blue"), ScoreMode.None)) + .get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("p2")); - searchResponse = client().prepareSearch("test") - .setQuery(hasChildQuery("child", termQuery("c_field", "red"), ScoreMode.None)) - .get(); + searchResponse = client().prepareSearch("test").setQuery(hasChildQuery("child", termQuery("c_field", "red"), ScoreMode.None)).get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(2L)); assertThat(searchResponse.getHits().getAt(0).getId(), anyOf(equalTo("p2"), equalTo("p1"))); @@ -353,22 +349,22 @@ public void testSimpleChildQueryWithFlush() throws Exception { // HAS CHILD FILTER searchResponse = client().prepareSearch("test") - .setQuery(constantScoreQuery(hasChildQuery("child", termQuery("c_field", "yellow"), ScoreMode.None))) - .get(); + .setQuery(constantScoreQuery(hasChildQuery("child", termQuery("c_field", "yellow"), ScoreMode.None))) + .get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("p1")); searchResponse = client().prepareSearch("test") - .setQuery(constantScoreQuery(hasChildQuery("child", termQuery("c_field", "blue"), ScoreMode.None))) - .get(); + .setQuery(constantScoreQuery(hasChildQuery("child", termQuery("c_field", "blue"), ScoreMode.None))) + .get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("p2")); searchResponse = client().prepareSearch("test") - .setQuery(constantScoreQuery(hasChildQuery("child", termQuery("c_field", "red"), ScoreMode.None))) - .get(); + .setQuery(constantScoreQuery(hasChildQuery("child", termQuery("c_field", "red"), ScoreMode.None))) + .get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(2L)); assertThat(searchResponse.getHits().getAt(0).getId(), anyOf(equalTo("p2"), equalTo("p1"))); @@ -376,10 +372,11 @@ public void testSimpleChildQueryWithFlush() throws Exception { } public void testScopedFacet() throws Exception { - assertAcked(prepareCreate("test") - .setMapping( - addFieldMappings(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"), - "c_field", "keyword"))); + assertAcked( + prepareCreate("test").setMapping( + addFieldMappings(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"), "c_field", "keyword") + ) + ); ensureGreen(); // index simple data @@ -392,14 +389,24 @@ public void testScopedFacet() throws Exception { refresh(); - SearchResponse searchResponse = client() - .prepareSearch("test") - .setQuery(hasChildQuery("child", - boolQuery().should(termQuery("c_field", "red")).should(termQuery("c_field", "yellow")), ScoreMode.None)) - .addAggregation(AggregationBuilders.global("global").subAggregation( - AggregationBuilders.filter("filter", - boolQuery().should(termQuery("c_field", "red")).should(termQuery("c_field", "yellow"))).subAggregation( - AggregationBuilders.terms("facet1").field("c_field")))).get(); + SearchResponse searchResponse = client().prepareSearch("test") + .setQuery( + hasChildQuery( + "child", + boolQuery().should(termQuery("c_field", "red")).should(termQuery("c_field", "yellow")), + ScoreMode.None + ) + ) + .addAggregation( + AggregationBuilders.global("global") + .subAggregation( + AggregationBuilders.filter( + "filter", + boolQuery().should(termQuery("c_field", "red")).should(termQuery("c_field", "yellow")) + ).subAggregation(AggregationBuilders.terms("facet1").field("c_field")) + ) + ) + .get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(2L)); assertThat(searchResponse.getHits().getAt(0).getId(), anyOf(equalTo("p2"), equalTo("p1"))); @@ -416,8 +423,7 @@ public void testScopedFacet() throws Exception { } public void testDeletedParent() throws Exception { - assertAcked(prepareCreate("test") - .setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); + assertAcked(prepareCreate("test").setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); ensureGreen(); // index simple data createIndexRequest("test", "parent", "p1", null, "p_field", "p_value1").get(); @@ -430,7 +436,8 @@ public void testDeletedParent() throws Exception { refresh(); SearchResponse searchResponse = client().prepareSearch("test") - .setQuery(constantScoreQuery(hasChildQuery("child", termQuery("c_field", "yellow"), ScoreMode.None))).get(); + .setQuery(constantScoreQuery(hasChildQuery("child", termQuery("c_field", "yellow"), ScoreMode.None))) + .get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("p1")); @@ -442,7 +449,8 @@ public void testDeletedParent() throws Exception { client().admin().indices().prepareRefresh().get(); searchResponse = client().prepareSearch("test") - .setQuery(constantScoreQuery(hasChildQuery("child", termQuery("c_field", "yellow"), ScoreMode.None))).get(); + .setQuery(constantScoreQuery(hasChildQuery("child", termQuery("c_field", "yellow"), ScoreMode.None))) + .get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("p1")); @@ -450,8 +458,7 @@ public void testDeletedParent() throws Exception { } public void testDfsSearchType() throws Exception { - assertAcked(prepareCreate("test") - .setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); + assertAcked(prepareCreate("test").setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); ensureGreen(); // index simple data @@ -464,21 +471,22 @@ public void testDfsSearchType() throws Exception { refresh(); - SearchResponse searchResponse = client().prepareSearch("test").setSearchType(SearchType.DFS_QUERY_THEN_FETCH) - .setQuery(boolQuery().mustNot(hasChildQuery("child", boolQuery().should(queryStringQuery("c_field:*")), ScoreMode.None))) - .get(); + SearchResponse searchResponse = client().prepareSearch("test") + .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) + .setQuery(boolQuery().mustNot(hasChildQuery("child", boolQuery().should(queryStringQuery("c_field:*")), ScoreMode.None))) + .get(); assertNoFailures(searchResponse); - searchResponse = client().prepareSearch("test").setSearchType(SearchType.DFS_QUERY_THEN_FETCH) - .setQuery(boolQuery().mustNot(hasParentQuery("parent", - boolQuery().should(queryStringQuery("p_field:*")), false))).execute() - .actionGet(); + searchResponse = client().prepareSearch("test") + .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) + .setQuery(boolQuery().mustNot(hasParentQuery("parent", boolQuery().should(queryStringQuery("p_field:*")), false))) + .execute() + .actionGet(); assertNoFailures(searchResponse); } public void testHasChildAndHasParentFailWhenSomeSegmentsDontContainAnyParentOrChildDocs() throws Exception { - assertAcked(prepareCreate("test") - .setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); + assertAcked(prepareCreate("test").setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); ensureGreen(); createIndexRequest("test", "parent", "1", null, "p_field", 1).get(); @@ -488,19 +496,20 @@ public void testHasChildAndHasParentFailWhenSomeSegmentsDontContainAnyParentOrCh refresh(); SearchResponse searchResponse = client().prepareSearch("test") - .setQuery(boolQuery().must(matchAllQuery()).filter(hasChildQuery("child", matchAllQuery(), ScoreMode.None))).get(); + .setQuery(boolQuery().must(matchAllQuery()).filter(hasChildQuery("child", matchAllQuery(), ScoreMode.None))) + .get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); searchResponse = client().prepareSearch("test") - .setQuery(boolQuery().must(matchAllQuery()).filter(hasParentQuery("parent", matchAllQuery(), false))).get(); + .setQuery(boolQuery().must(matchAllQuery()).filter(hasParentQuery("parent", matchAllQuery(), false))) + .get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); } public void testCountApiUsage() throws Exception { - assertAcked(prepareCreate("test") - .setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); + assertAcked(prepareCreate("test").setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); ensureGreen(); String parentId = "p1"; @@ -508,30 +517,30 @@ public void testCountApiUsage() throws Exception { createIndexRequest("test", "child", "c1", parentId, "c_field", "1").get(); refresh(); - SearchResponse countResponse = client().prepareSearch("test").setSize(0) - .setQuery(hasChildQuery("child", termQuery("c_field", "1"), ScoreMode.Max)) - .get(); + SearchResponse countResponse = client().prepareSearch("test") + .setSize(0) + .setQuery(hasChildQuery("child", termQuery("c_field", "1"), ScoreMode.Max)) + .get(); assertHitCount(countResponse, 1L); - countResponse = client().prepareSearch("test").setSize(0) - .setQuery(hasParentQuery("parent", termQuery("p_field", "1"), true)) - .get(); + countResponse = client().prepareSearch("test").setSize(0).setQuery(hasParentQuery("parent", termQuery("p_field", "1"), true)).get(); assertHitCount(countResponse, 1L); - countResponse = client().prepareSearch("test").setSize(0) - .setQuery(constantScoreQuery(hasChildQuery("child", termQuery("c_field", "1"), ScoreMode.None))) - .get(); + countResponse = client().prepareSearch("test") + .setSize(0) + .setQuery(constantScoreQuery(hasChildQuery("child", termQuery("c_field", "1"), ScoreMode.None))) + .get(); assertHitCount(countResponse, 1L); - countResponse = client().prepareSearch("test").setSize(0) + countResponse = client().prepareSearch("test") + .setSize(0) .setQuery(constantScoreQuery(hasParentQuery("parent", termQuery("p_field", "1"), false))) - .get(); + .get(); assertHitCount(countResponse, 1L); } public void testExplainUsage() throws Exception { - assertAcked(prepareCreate("test") - .setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); + assertAcked(prepareCreate("test").setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); ensureGreen(); String parentId = "p1"; @@ -540,22 +549,22 @@ public void testExplainUsage() throws Exception { refresh(); SearchResponse searchResponse = client().prepareSearch("test") - .setExplain(true) - .setQuery(hasChildQuery("child", termQuery("c_field", "1"), ScoreMode.Max)) - .get(); + .setExplain(true) + .setQuery(hasChildQuery("child", termQuery("c_field", "1"), ScoreMode.Max)) + .get(); assertHitCount(searchResponse, 1L); assertThat(searchResponse.getHits().getAt(0).getExplanation().getDescription(), containsString("join value p1")); searchResponse = client().prepareSearch("test") - .setExplain(true) - .setQuery(hasParentQuery("parent", termQuery("p_field", "1"), true)) - .get(); + .setExplain(true) + .setQuery(hasParentQuery("parent", termQuery("p_field", "1"), true)) + .get(); assertHitCount(searchResponse, 1L); assertThat(searchResponse.getHits().getAt(0).getExplanation().getDescription(), containsString("join value p1")); ExplainResponse explainResponse = client().prepareExplain("test", parentId) - .setQuery(hasChildQuery("child", termQuery("c_field", "1"), ScoreMode.Max)) - .get(); + .setQuery(hasChildQuery("child", termQuery("c_field", "1"), ScoreMode.Max)) + .get(); assertThat(explainResponse.isExists(), equalTo(true)); assertThat(explainResponse.getExplanation().toString(), containsString("join value p1")); } @@ -594,27 +603,35 @@ List createDocBuilders() { } public void testScoreForParentChildQueriesWithFunctionScore() throws Exception { - assertAcked(prepareCreate("test") - .setMapping(jsonBuilder().startObject().startObject("_doc").startObject("properties") - .startObject("join_field") + assertAcked( + prepareCreate("test").setMapping( + jsonBuilder().startObject() + .startObject("_doc") + .startObject("properties") + .startObject("join_field") .field("type", "join") .startObject("relations") - .field("parent", new String[] {"child", "child1"}) + .field("parent", new String[] { "child", "child1" }) .endObject() - .endObject() - .endObject().endObject().endObject() - )); + .endObject() + .endObject() + .endObject() + .endObject() + ) + ); ensureGreen(); indexRandom(true, createDocBuilders().toArray(new IndexRequestBuilder[0])); - SearchResponse response = client() - .prepareSearch("test") - .setQuery( - hasChildQuery( - "child", - QueryBuilders.functionScoreQuery(matchQuery("c_field2", 0), - fieldValueFactorFunction("c_field1")) - .boostMode(CombineFunction.REPLACE), ScoreMode.Total)).get(); + SearchResponse response = client().prepareSearch("test") + .setQuery( + hasChildQuery( + "child", + QueryBuilders.functionScoreQuery(matchQuery("c_field2", 0), fieldValueFactorFunction("c_field1")) + .boostMode(CombineFunction.REPLACE), + ScoreMode.Total + ) + ) + .get(); assertThat(response.getHits().getTotalHits().value, equalTo(3L)); assertThat(response.getHits().getHits()[0].getId(), equalTo("1")); @@ -624,14 +641,16 @@ public void testScoreForParentChildQueriesWithFunctionScore() throws Exception { assertThat(response.getHits().getHits()[2].getId(), equalTo("2")); assertThat(response.getHits().getHits()[2].getScore(), equalTo(3f)); - response = client() - .prepareSearch("test") - .setQuery( - hasChildQuery( - "child", - QueryBuilders.functionScoreQuery(matchQuery("c_field2", 0), - fieldValueFactorFunction("c_field1")) - .boostMode(CombineFunction.REPLACE), ScoreMode.Max)).get(); + response = client().prepareSearch("test") + .setQuery( + hasChildQuery( + "child", + QueryBuilders.functionScoreQuery(matchQuery("c_field2", 0), fieldValueFactorFunction("c_field1")) + .boostMode(CombineFunction.REPLACE), + ScoreMode.Max + ) + ) + .get(); assertThat(response.getHits().getTotalHits().value, equalTo(3L)); assertThat(response.getHits().getHits()[0].getId(), equalTo("3")); @@ -641,14 +660,16 @@ public void testScoreForParentChildQueriesWithFunctionScore() throws Exception { assertThat(response.getHits().getHits()[2].getId(), equalTo("1")); assertThat(response.getHits().getHits()[2].getScore(), equalTo(2f)); - response = client() - .prepareSearch("test") - .setQuery( - hasChildQuery( - "child", - QueryBuilders.functionScoreQuery(matchQuery("c_field2", 0), - fieldValueFactorFunction("c_field1")) - .boostMode(CombineFunction.REPLACE), ScoreMode.Avg)).get(); + response = client().prepareSearch("test") + .setQuery( + hasChildQuery( + "child", + QueryBuilders.functionScoreQuery(matchQuery("c_field2", 0), fieldValueFactorFunction("c_field1")) + .boostMode(CombineFunction.REPLACE), + ScoreMode.Avg + ) + ) + .get(); assertThat(response.getHits().getTotalHits().value, equalTo(3L)); assertThat(response.getHits().getHits()[0].getId(), equalTo("3")); @@ -658,15 +679,18 @@ public void testScoreForParentChildQueriesWithFunctionScore() throws Exception { assertThat(response.getHits().getHits()[2].getId(), equalTo("1")); assertThat(response.getHits().getHits()[2].getScore(), equalTo(1.5f)); - response = client() - .prepareSearch("test") - .setQuery( - hasParentQuery( - "parent", - QueryBuilders.functionScoreQuery(matchQuery("p_field1", "p_value3"), - fieldValueFactorFunction("p_field2")) - .boostMode(CombineFunction.REPLACE), true)) - .addSort(SortBuilders.fieldSort("c_field3")).addSort(SortBuilders.scoreSort()).get(); + response = client().prepareSearch("test") + .setQuery( + hasParentQuery( + "parent", + QueryBuilders.functionScoreQuery(matchQuery("p_field1", "p_value3"), fieldValueFactorFunction("p_field2")) + .boostMode(CombineFunction.REPLACE), + true + ) + ) + .addSort(SortBuilders.fieldSort("c_field3")) + .addSort(SortBuilders.scoreSort()) + .get(); assertThat(response.getHits().getTotalHits().value, equalTo(7L)); assertThat(response.getHits().getHits()[0].getId(), equalTo("16")); @@ -687,42 +711,39 @@ public void testScoreForParentChildQueriesWithFunctionScore() throws Exception { // Issue #2536 public void testParentChildQueriesCanHandleNoRelevantTypesInIndex() throws Exception { - assertAcked(prepareCreate("test") - .setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); + assertAcked(prepareCreate("test").setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); ensureGreen(); SearchResponse response = client().prepareSearch("test") - .setQuery(hasChildQuery("child", matchQuery("text", "value"), ScoreMode.None)).get(); + .setQuery(hasChildQuery("child", matchQuery("text", "value"), ScoreMode.None)) + .get(); assertNoFailures(response); assertThat(response.getHits().getTotalHits().value, equalTo(0L)); - client().prepareIndex("test").setSource(jsonBuilder().startObject().field("text", "value").endObject()) - .setRefreshPolicy(RefreshPolicy.IMMEDIATE).get(); + client().prepareIndex("test") + .setSource(jsonBuilder().startObject().field("text", "value").endObject()) + .setRefreshPolicy(RefreshPolicy.IMMEDIATE) + .get(); - response = client().prepareSearch("test") - .setQuery(hasChildQuery("child", matchQuery("text", "value"), ScoreMode.None)).get(); + response = client().prepareSearch("test").setQuery(hasChildQuery("child", matchQuery("text", "value"), ScoreMode.None)).get(); assertNoFailures(response); assertThat(response.getHits().getTotalHits().value, equalTo(0L)); - response = client().prepareSearch("test").setQuery(hasChildQuery("child", matchQuery("text", "value"), ScoreMode.Max)) - .get(); + response = client().prepareSearch("test").setQuery(hasChildQuery("child", matchQuery("text", "value"), ScoreMode.Max)).get(); assertNoFailures(response); assertThat(response.getHits().getTotalHits().value, equalTo(0L)); - response = client().prepareSearch("test") - .setQuery(hasParentQuery("parent", matchQuery("text", "value"), false)).get(); + response = client().prepareSearch("test").setQuery(hasParentQuery("parent", matchQuery("text", "value"), false)).get(); assertNoFailures(response); assertThat(response.getHits().getTotalHits().value, equalTo(0L)); - response = client().prepareSearch("test").setQuery(hasParentQuery("parent", matchQuery("text", "value"), true)) - .get(); + response = client().prepareSearch("test").setQuery(hasParentQuery("parent", matchQuery("text", "value"), true)).get(); assertNoFailures(response); assertThat(response.getHits().getTotalHits().value, equalTo(0L)); } public void testHasChildAndHasParentFilter_withFilter() throws Exception { - assertAcked(prepareCreate("test") - .setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); + assertAcked(prepareCreate("test").setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); ensureGreen(); createIndexRequest("test", "parent", "1", null, "p_field", 1).get(); @@ -733,35 +754,37 @@ public void testHasChildAndHasParentFilter_withFilter() throws Exception { refresh(); SearchResponse searchResponse = client().prepareSearch("test") - .setQuery(boolQuery().must(matchAllQuery()).filter(hasChildQuery("child", termQuery("c_field", 1), ScoreMode.None))) - .get(); + .setQuery(boolQuery().must(matchAllQuery()).filter(hasChildQuery("child", termQuery("c_field", 1), ScoreMode.None))) + .get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); assertThat(searchResponse.getHits().getHits()[0].getId(), equalTo("1")); searchResponse = client().prepareSearch("test") - .setQuery(boolQuery().must(matchAllQuery()) - .filter(hasParentQuery("parent", termQuery("p_field", 1), false))).get(); + .setQuery(boolQuery().must(matchAllQuery()).filter(hasParentQuery("parent", termQuery("p_field", 1), false))) + .get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); assertThat(searchResponse.getHits().getHits()[0].getId(), equalTo("2")); } public void testHasChildInnerHitsHighlighting() throws Exception { - assertAcked(prepareCreate("test") - .setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); + assertAcked(prepareCreate("test").setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); ensureGreen(); createIndexRequest("test", "parent", "1", null, "p_field", 1).get(); createIndexRequest("test", "child", "2", "1", "c_field", "foo bar").get(); refresh(); - SearchResponse searchResponse = client().prepareSearch("test").setQuery( - hasChildQuery("child", matchQuery("c_field", "foo"), ScoreMode.None) - .innerHit(new InnerHitBuilder().setHighlightBuilder( - new HighlightBuilder().field(new Field("c_field") - .highlightQuery(QueryBuilders.matchQuery("c_field", "bar")))))) - .get(); + SearchResponse searchResponse = client().prepareSearch("test") + .setQuery( + hasChildQuery("child", matchQuery("c_field", "foo"), ScoreMode.None).innerHit( + new InnerHitBuilder().setHighlightBuilder( + new HighlightBuilder().field(new Field("c_field").highlightQuery(QueryBuilders.matchQuery("c_field", "bar"))) + ) + ) + ) + .get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); assertThat(searchResponse.getHits().getHits()[0].getId(), equalTo("1")); @@ -772,8 +795,7 @@ public void testHasChildInnerHitsHighlighting() throws Exception { } public void testHasChildAndHasParentWrappedInAQueryFilter() throws Exception { - assertAcked(prepareCreate("test") - .setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); + assertAcked(prepareCreate("test").setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); ensureGreen(); // query filter in case for p/c shouldn't execute per segment, but rather @@ -783,30 +805,40 @@ public void testHasChildAndHasParentWrappedInAQueryFilter() throws Exception { refresh(); SearchResponse searchResponse = client().prepareSearch("test") - .setQuery(boolQuery().must(matchAllQuery()).filter(hasChildQuery("child", matchQuery("c_field", 1), ScoreMode.None))) - .get(); + .setQuery(boolQuery().must(matchAllQuery()).filter(hasChildQuery("child", matchQuery("c_field", 1), ScoreMode.None))) + .get(); assertSearchHit(searchResponse, 1, hasId("1")); searchResponse = client().prepareSearch("test") - .setQuery(boolQuery().must(matchAllQuery()).filter(hasParentQuery("parent", matchQuery("p_field", 1), false))).get(); + .setQuery(boolQuery().must(matchAllQuery()).filter(hasParentQuery("parent", matchQuery("p_field", 1), false))) + .get(); assertSearchHit(searchResponse, 1, hasId("2")); searchResponse = client().prepareSearch("test") - .setQuery(boolQuery().must(matchAllQuery()) - .filter(boolQuery().must(hasChildQuery("child", matchQuery("c_field", 1), ScoreMode.None)))) - .get(); + .setQuery( + boolQuery().must(matchAllQuery()).filter(boolQuery().must(hasChildQuery("child", matchQuery("c_field", 1), ScoreMode.None))) + ) + .get(); assertSearchHit(searchResponse, 1, hasId("1")); searchResponse = client().prepareSearch("test") - .setQuery(boolQuery().must(matchAllQuery()) - .filter(boolQuery().must(hasParentQuery("parent", matchQuery("p_field", 1), false)))).get(); + .setQuery(boolQuery().must(matchAllQuery()).filter(boolQuery().must(hasParentQuery("parent", matchQuery("p_field", 1), false)))) + .get(); assertSearchHit(searchResponse, 1, hasId("2")); } public void testSimpleQueryRewrite() throws Exception { - assertAcked(prepareCreate("test") - .setMapping(addFieldMappings(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"), - "c_field", "keyword", "p_field", "keyword"))); + assertAcked( + prepareCreate("test").setMapping( + addFieldMappings( + buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"), + "c_field", + "keyword", + "p_field", + "keyword" + ) + ) + ); ensureGreen(); // index simple data @@ -823,12 +855,14 @@ public void testSimpleQueryRewrite() throws Exception { } refresh(); - SearchType[] searchTypes = new SearchType[]{SearchType.QUERY_THEN_FETCH, SearchType.DFS_QUERY_THEN_FETCH}; + SearchType[] searchTypes = new SearchType[] { SearchType.QUERY_THEN_FETCH, SearchType.DFS_QUERY_THEN_FETCH }; for (SearchType searchType : searchTypes) { - SearchResponse searchResponse = client().prepareSearch("test").setSearchType(searchType) - .setQuery(hasChildQuery("child", prefixQuery("c_field", "c"), ScoreMode.Max)) - .addSort("p_field", SortOrder.ASC) - .setSize(5).get(); + SearchResponse searchResponse = client().prepareSearch("test") + .setSearchType(searchType) + .setQuery(hasChildQuery("child", prefixQuery("c_field", "c"), ScoreMode.Max)) + .addSort("p_field", SortOrder.ASC) + .setSize(5) + .get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(10L)); assertThat(searchResponse.getHits().getHits()[0].getId(), equalTo("p000")); @@ -837,9 +871,12 @@ public void testSimpleQueryRewrite() throws Exception { assertThat(searchResponse.getHits().getHits()[3].getId(), equalTo("p003")); assertThat(searchResponse.getHits().getHits()[4].getId(), equalTo("p004")); - searchResponse = client().prepareSearch("test").setSearchType(searchType) - .setQuery(hasParentQuery("parent", prefixQuery("p_field", "p"), true)).addSort("c_field", SortOrder.ASC) - .setSize(5).get(); + searchResponse = client().prepareSearch("test") + .setSearchType(searchType) + .setQuery(hasParentQuery("parent", prefixQuery("p_field", "p"), true)) + .addSort("c_field", SortOrder.ASC) + .setSize(5) + .get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(500L)); assertThat(searchResponse.getHits().getHits()[0].getId(), equalTo("c000")); @@ -852,8 +889,7 @@ public void testSimpleQueryRewrite() throws Exception { // Issue #3144 public void testReIndexingParentAndChildDocuments() throws Exception { - assertAcked(prepareCreate("test") - .setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); + assertAcked(prepareCreate("test").setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); ensureGreen(); // index simple data @@ -867,17 +903,16 @@ public void testReIndexingParentAndChildDocuments() throws Exception { refresh(); SearchResponse searchResponse = client().prepareSearch("test") - .setQuery(hasChildQuery("child", termQuery("c_field", "yellow"), ScoreMode.Total)).get(); + .setQuery(hasChildQuery("child", termQuery("c_field", "yellow"), ScoreMode.Total)) + .get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("p1")); assertThat(searchResponse.getHits().getAt(0).getSourceAsString(), containsString("\"p_value1\"")); - searchResponse = client() - .prepareSearch("test") - .setQuery( - boolQuery().must(matchQuery("c_field", "x")).must( - hasParentQuery("parent", termQuery("p_field", "p_value2"), true))).get(); + searchResponse = client().prepareSearch("test") + .setQuery(boolQuery().must(matchQuery("c_field", "x")).must(hasParentQuery("parent", termQuery("p_field", "p_value2"), true))) + .get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(2L)); assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("c3")); @@ -894,17 +929,15 @@ public void testReIndexingParentAndChildDocuments() throws Exception { searchResponse = client().prepareSearch("test") .setQuery(hasChildQuery("child", termQuery("c_field", "yellow"), ScoreMode.Total)) - .get(); + .get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("p1")); assertThat(searchResponse.getHits().getAt(0).getSourceAsString(), containsString("\"p_value1\"")); - searchResponse = client() - .prepareSearch("test") - .setQuery( - boolQuery().must(matchQuery("c_field", "x")).must( - hasParentQuery("parent", termQuery("p_field", "p_value2"), true))).get(); + searchResponse = client().prepareSearch("test") + .setQuery(boolQuery().must(matchQuery("c_field", "x")).must(hasParentQuery("parent", termQuery("p_field", "p_value2"), true))) + .get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(2L)); assertThat(searchResponse.getHits().getAt(0).getId(), Matchers.anyOf(equalTo("c3"), equalTo("c4"))); @@ -913,8 +946,7 @@ public void testReIndexingParentAndChildDocuments() throws Exception { // Issue #3203 public void testHasChildQueryWithMinimumScore() throws Exception { - assertAcked(prepareCreate("test") - .setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); + assertAcked(prepareCreate("test").setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); ensureGreen(); // index simple data @@ -926,10 +958,10 @@ public void testHasChildQueryWithMinimumScore() throws Exception { createIndexRequest("test", "child", "c5", "p2", "c_field", "x").get(); refresh(); - SearchResponse searchResponse = client() - .prepareSearch("test").setQuery(hasChildQuery("child", matchAllQuery(), ScoreMode.Total)) - .setMinScore(3) // Score needs to be 3 or above! - .get(); + SearchResponse searchResponse = client().prepareSearch("test") + .setQuery(hasChildQuery("child", matchAllQuery(), ScoreMode.Total)) + .setMinScore(3) // Score needs to be 3 or above! + .get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("p2")); @@ -937,9 +969,10 @@ public void testHasChildQueryWithMinimumScore() throws Exception { } public void testParentFieldQuery() throws Exception { - assertAcked(prepareCreate("test") - .setSettings(Settings.builder().put("index.refresh_interval", -1)) - .setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); + assertAcked( + prepareCreate("test").setSettings(Settings.builder().put("index.refresh_interval", -1)) + .setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child")) + ); ensureGreen(); SearchResponse response = client().prepareSearch("test") @@ -959,20 +992,19 @@ public void testParentFieldQuery() throws Exception { refresh(); response = client().prepareSearch("test") - .setQuery(boolQuery() - .should(boolQuery().filter(termQuery("join_field#parent", "p1")).filter(termQuery("join_field", "child"))) - .should(boolQuery().filter(termQuery("join_field#parent", "p2")).filter(termQuery("join_field", "child"))) - ).get(); + .setQuery( + boolQuery().should(boolQuery().filter(termQuery("join_field#parent", "p1")).filter(termQuery("join_field", "child"))) + .should(boolQuery().filter(termQuery("join_field#parent", "p2")).filter(termQuery("join_field", "child"))) + ) + .get(); assertHitCount(response, 2L); } public void testParentIdQuery() throws Exception { - assertAcked(prepareCreate("test") - .setSettings(Settings.builder() - .put(indexSettings()) - .put("index.refresh_interval", -1) - ) - .setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); + assertAcked( + prepareCreate("test").setSettings(Settings.builder().put(indexSettings()).put("index.refresh_interval", -1)) + .setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child")) + ); ensureGreen(); createIndexRequest("test", "child", "c1", "p1").get(); @@ -985,16 +1017,13 @@ public void testParentIdQuery() throws Exception { refresh(); response = client().prepareSearch("test") - .setQuery(boolQuery() - .should(parentId("child", "p1")) - .should(parentId("child", "p2")) - ).get(); + .setQuery(boolQuery().should(parentId("child", "p1")).should(parentId("child", "p2"))) + .get(); assertHitCount(response, 2L); } public void testHasChildNotBeingCached() throws IOException { - assertAcked(prepareCreate("test") - .setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); + assertAcked(prepareCreate("test").setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); ensureGreen(); // index simple data @@ -1013,8 +1042,8 @@ public void testHasChildNotBeingCached() throws IOException { client().admin().indices().prepareRefresh("test").get(); SearchResponse searchResponse = client().prepareSearch("test") - .setQuery(constantScoreQuery(hasChildQuery("child", termQuery("c_field", "blue"), ScoreMode.None))) - .get(); + .setQuery(constantScoreQuery(hasChildQuery("child", termQuery("c_field", "blue"), ScoreMode.None))) + .get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); @@ -1022,8 +1051,8 @@ public void testHasChildNotBeingCached() throws IOException { client().admin().indices().prepareRefresh("test").get(); searchResponse = client().prepareSearch("test") - .setQuery(constantScoreQuery(hasChildQuery("child", termQuery("c_field", "blue"), ScoreMode.None))) - .get(); + .setQuery(constantScoreQuery(hasChildQuery("child", termQuery("c_field", "blue"), ScoreMode.None))) + .get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(2L)); } @@ -1054,17 +1083,23 @@ private QueryBuilder randomHasParent(String type, String field, String value) { // Issue #3818 public void testHasChildQueryOnlyReturnsSingleChildType() throws Exception { - assertAcked(prepareCreate("grandissue") - .setMapping(jsonBuilder().startObject().startObject("_doc").startObject("properties") - .startObject("join_field") + assertAcked( + prepareCreate("grandissue").setMapping( + jsonBuilder().startObject() + .startObject("_doc") + .startObject("properties") + .startObject("join_field") .field("type", "join") .startObject("relations") - .field("grandparent", "parent") - .field("parent", new String[] {"child_type_one", "child_type_two"}) + .field("grandparent", "parent") + .field("parent", new String[] { "child_type_one", "child_type_two" }) .endObject() - .endObject() - .endObject().endObject().endObject() - )); + .endObject() + .endObject() + .endObject() + .endObject() + ) + ); createIndexRequest("grandissue", "grandparent", "1", null, "name", "Grandpa").get(); createIndexRequest("grandissue", "parent", "2", "1", "name", "Dana").get(); @@ -1072,62 +1107,91 @@ public void testHasChildQueryOnlyReturnsSingleChildType() throws Exception { createIndexRequest("grandissue", "child_type_two", "4", "2", "name", "Kate").setRouting("1").get(); refresh(); - SearchResponse searchResponse = client().prepareSearch("grandissue").setQuery( + SearchResponse searchResponse = client().prepareSearch("grandissue") + .setQuery( boolQuery().must( - hasChildQuery( - "parent", - boolQuery().must( - hasChildQuery( - "child_type_one", - boolQuery().must( - queryStringQuery("name:William*") - ), - ScoreMode.None) - ), - ScoreMode.None) + hasChildQuery( + "parent", + boolQuery().must( + hasChildQuery("child_type_one", boolQuery().must(queryStringQuery("name:William*")), ScoreMode.None) + ), + ScoreMode.None + ) ) - ).get(); + ) + .get(); assertHitCount(searchResponse, 1L); - searchResponse = client().prepareSearch("grandissue").setQuery( + searchResponse = client().prepareSearch("grandissue") + .setQuery( boolQuery().must( - hasChildQuery( - "parent", - boolQuery().must( - hasChildQuery( - "child_type_two", - boolQuery().must( - queryStringQuery("name:William*") - ), - ScoreMode.None) - ), - ScoreMode.None) + hasChildQuery( + "parent", + boolQuery().must( + hasChildQuery("child_type_two", boolQuery().must(queryStringQuery("name:William*")), ScoreMode.None) + ), + ScoreMode.None + ) ) - ).get(); + ) + .get(); assertHitCount(searchResponse, 0L); } public void testHasChildQueryWithNestedInnerObjects() throws Exception { - assertAcked(prepareCreate("test") - .setMapping( - addFieldMappings(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"), - "objects", "nested"))); + assertAcked( + prepareCreate("test").setMapping( + addFieldMappings(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"), "objects", "nested") + ) + ); ensureGreen(); - createIndexRequest("test", "parent", "p1", null, jsonBuilder().startObject().field("p_field", "1").startArray("objects") - .startObject().field("i_field", "1").endObject() - .startObject().field("i_field", "2").endObject() - .startObject().field("i_field", "3").endObject() - .startObject().field("i_field", "4").endObject() - .startObject().field("i_field", "5").endObject() - .startObject().field("i_field", "6").endObject() - .endArray().endObject()) - .get(); - createIndexRequest("test", "parent", "p2", null, jsonBuilder().startObject().field("p_field", "2").startArray("objects") - .startObject().field("i_field", "1").endObject() - .startObject().field("i_field", "2").endObject() - .endArray().endObject()) - .get(); + createIndexRequest( + "test", + "parent", + "p1", + null, + jsonBuilder().startObject() + .field("p_field", "1") + .startArray("objects") + .startObject() + .field("i_field", "1") + .endObject() + .startObject() + .field("i_field", "2") + .endObject() + .startObject() + .field("i_field", "3") + .endObject() + .startObject() + .field("i_field", "4") + .endObject() + .startObject() + .field("i_field", "5") + .endObject() + .startObject() + .field("i_field", "6") + .endObject() + .endArray() + .endObject() + ).get(); + createIndexRequest( + "test", + "parent", + "p2", + null, + jsonBuilder().startObject() + .field("p_field", "2") + .startArray("objects") + .startObject() + .field("i_field", "1") + .endObject() + .startObject() + .field("i_field", "2") + .endObject() + .endArray() + .endObject() + ).get(); createIndexRequest("test", "child", "c1", "p1", "c_field", "blue").get(); createIndexRequest("test", "child", "c2", "p1", "c_field", "red").get(); createIndexRequest("test", "child", "c3", "p2", "c_field", "red").get(); @@ -1135,23 +1199,26 @@ public void testHasChildQueryWithNestedInnerObjects() throws Exception { ScoreMode scoreMode = randomFrom(ScoreMode.values()); SearchResponse searchResponse = client().prepareSearch("test") - .setQuery(boolQuery().must(hasChildQuery("child", termQuery("c_field", "blue"), scoreMode)) - .filter(boolQuery().mustNot(termQuery("p_field", "3")))) - .get(); + .setQuery( + boolQuery().must(hasChildQuery("child", termQuery("c_field", "blue"), scoreMode)) + .filter(boolQuery().mustNot(termQuery("p_field", "3"))) + ) + .get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); searchResponse = client().prepareSearch("test") - .setQuery(boolQuery().must(hasChildQuery("child", termQuery("c_field", "red"), scoreMode)) - .filter(boolQuery().mustNot(termQuery("p_field", "3")))) - .get(); + .setQuery( + boolQuery().must(hasChildQuery("child", termQuery("c_field", "red"), scoreMode)) + .filter(boolQuery().mustNot(termQuery("p_field", "3"))) + ) + .get(); assertNoFailures(searchResponse); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(2L)); } public void testNamedFilters() throws Exception { - assertAcked(prepareCreate("test") - .setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); + assertAcked(prepareCreate("test").setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); ensureGreen(); String parentId = "p1"; @@ -1159,40 +1226,37 @@ public void testNamedFilters() throws Exception { createIndexRequest("test", "child", "c1", parentId, "c_field", "1").get(); refresh(); - SearchResponse searchResponse = client().prepareSearch("test").setQuery(hasChildQuery("child", - termQuery("c_field", "1"), ScoreMode.Max).queryName("test")) - .get(); + SearchResponse searchResponse = client().prepareSearch("test") + .setQuery(hasChildQuery("child", termQuery("c_field", "1"), ScoreMode.Max).queryName("test")) + .get(); assertHitCount(searchResponse, 1L); assertThat(searchResponse.getHits().getAt(0).getMatchedQueries().length, equalTo(1)); assertThat(searchResponse.getHits().getAt(0).getMatchedQueries()[0], equalTo("test")); - searchResponse = client().prepareSearch("test").setQuery(hasParentQuery("parent", - termQuery("p_field", "1"), true).queryName("test")) - .get(); + searchResponse = client().prepareSearch("test") + .setQuery(hasParentQuery("parent", termQuery("p_field", "1"), true).queryName("test")) + .get(); assertHitCount(searchResponse, 1L); assertThat(searchResponse.getHits().getAt(0).getMatchedQueries().length, equalTo(1)); assertThat(searchResponse.getHits().getAt(0).getMatchedQueries()[0], equalTo("test")); - searchResponse = client().prepareSearch("test").setQuery(constantScoreQuery(hasChildQuery("child", - termQuery("c_field", "1"), ScoreMode.None).queryName("test"))) - .get(); + searchResponse = client().prepareSearch("test") + .setQuery(constantScoreQuery(hasChildQuery("child", termQuery("c_field", "1"), ScoreMode.None).queryName("test"))) + .get(); assertHitCount(searchResponse, 1L); assertThat(searchResponse.getHits().getAt(0).getMatchedQueries().length, equalTo(1)); assertThat(searchResponse.getHits().getAt(0).getMatchedQueries()[0], equalTo("test")); - searchResponse = client().prepareSearch("test").setQuery(constantScoreQuery(hasParentQuery("parent", - termQuery("p_field", "1"), false).queryName("test"))) - .get(); + searchResponse = client().prepareSearch("test") + .setQuery(constantScoreQuery(hasParentQuery("parent", termQuery("p_field", "1"), false).queryName("test"))) + .get(); assertHitCount(searchResponse, 1L); assertThat(searchResponse.getHits().getAt(0).getMatchedQueries().length, equalTo(1)); assertThat(searchResponse.getHits().getAt(0).getMatchedQueries()[0], equalTo("test")); } public void testParentChildQueriesNoParentType() throws Exception { - assertAcked(prepareCreate("test") - .setSettings(Settings.builder() - .put(indexSettings()) - .put("index.refresh_interval", -1))); + assertAcked(prepareCreate("test").setSettings(Settings.builder().put(indexSettings()).put("index.refresh_interval", -1))); ensureGreen(); String parentId = "p1"; @@ -1200,45 +1264,35 @@ public void testParentChildQueriesNoParentType() throws Exception { refresh(); try { - client().prepareSearch("test") - .setQuery(hasChildQuery("child", termQuery("c_field", "1"), ScoreMode.None)) - .get(); + client().prepareSearch("test").setQuery(hasChildQuery("child", termQuery("c_field", "1"), ScoreMode.None)).get(); fail(); } catch (SearchPhaseExecutionException e) { assertThat(e.status(), equalTo(RestStatus.BAD_REQUEST)); } try { - client().prepareSearch("test") - .setQuery(hasChildQuery("child", termQuery("c_field", "1"), ScoreMode.Max)) - .get(); + client().prepareSearch("test").setQuery(hasChildQuery("child", termQuery("c_field", "1"), ScoreMode.Max)).get(); fail(); } catch (SearchPhaseExecutionException e) { assertThat(e.status(), equalTo(RestStatus.BAD_REQUEST)); } try { - client().prepareSearch("test") - .setPostFilter(hasChildQuery("child", termQuery("c_field", "1"), ScoreMode.None)) - .get(); + client().prepareSearch("test").setPostFilter(hasChildQuery("child", termQuery("c_field", "1"), ScoreMode.None)).get(); fail(); } catch (SearchPhaseExecutionException e) { assertThat(e.status(), equalTo(RestStatus.BAD_REQUEST)); } try { - client().prepareSearch("test") - .setQuery(hasParentQuery("parent", termQuery("p_field", "1"), true)) - .get(); + client().prepareSearch("test").setQuery(hasParentQuery("parent", termQuery("p_field", "1"), true)).get(); fail(); } catch (SearchPhaseExecutionException e) { assertThat(e.status(), equalTo(RestStatus.BAD_REQUEST)); } try { - client().prepareSearch("test") - .setPostFilter(hasParentQuery("parent", termQuery("p_field", "1"), false)) - .get(); + client().prepareSearch("test").setPostFilter(hasParentQuery("parent", termQuery("p_field", "1"), false)).get(); fail(); } catch (SearchPhaseExecutionException e) { assertThat(e.status(), equalTo(RestStatus.BAD_REQUEST)); @@ -1246,9 +1300,10 @@ public void testParentChildQueriesNoParentType() throws Exception { } public void testParentChildCaching() throws Exception { - assertAcked(prepareCreate("test") - .setSettings(Settings.builder().put("index.refresh_interval", -1)) - .setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); + assertAcked( + prepareCreate("test").setSettings(Settings.builder().put("index.refresh_interval", -1)) + .setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child")) + ); ensureGreen(); // index simple data @@ -1268,29 +1323,31 @@ public void testParentChildCaching() throws Exception { for (int i = 0; i < 2; i++) { SearchResponse searchResponse = client().prepareSearch() - .setQuery(boolQuery().must(matchAllQuery()).filter(boolQuery() - .must(hasChildQuery("child", matchQuery("c_field", "red"), ScoreMode.None)) - .must(matchAllQuery()))) - .get(); + .setQuery( + boolQuery().must(matchAllQuery()) + .filter( + boolQuery().must(hasChildQuery("child", matchQuery("c_field", "red"), ScoreMode.None)).must(matchAllQuery()) + ) + ) + .get(); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(2L)); } - - createIndexRequest("test", "child", "c3", "p2", "c_field", "blue").get(); + createIndexRequest("test", "child", "c3", "p2", "c_field", "blue").get(); client().admin().indices().prepareRefresh("test").get(); SearchResponse searchResponse = client().prepareSearch() - .setQuery(boolQuery().must(matchAllQuery()).filter(boolQuery() - .must(hasChildQuery("child", matchQuery("c_field", "red"), ScoreMode.None)) - .must(matchAllQuery()))) - .get(); + .setQuery( + boolQuery().must(matchAllQuery()) + .filter(boolQuery().must(hasChildQuery("child", matchQuery("c_field", "red"), ScoreMode.None)).must(matchAllQuery())) + ) + .get(); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); } public void testParentChildQueriesViaScrollApi() throws Exception { - assertAcked(prepareCreate("test") - .setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); + assertAcked(prepareCreate("test").setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); ensureGreen(); for (int i = 0; i < 10; i++) { createIndexRequest("test", "parent", "p" + i, null).get(); @@ -1299,21 +1356,20 @@ public void testParentChildQueriesViaScrollApi() throws Exception { refresh(); - QueryBuilder[] queries = new QueryBuilder[]{ - hasChildQuery("child", matchAllQuery(), ScoreMode.None), - boolQuery().must(matchAllQuery()).filter(hasChildQuery("child", matchAllQuery(), ScoreMode.None)), - hasParentQuery("parent", matchAllQuery(), false), - boolQuery().must(matchAllQuery()).filter(hasParentQuery("parent", matchAllQuery(), false)) - }; + QueryBuilder[] queries = new QueryBuilder[] { + hasChildQuery("child", matchAllQuery(), ScoreMode.None), + boolQuery().must(matchAllQuery()).filter(hasChildQuery("child", matchAllQuery(), ScoreMode.None)), + hasParentQuery("parent", matchAllQuery(), false), + boolQuery().must(matchAllQuery()).filter(hasParentQuery("parent", matchAllQuery(), false)) }; for (QueryBuilder query : queries) { SearchResponse scrollResponse = client().prepareSearch("test") - .setScroll(TimeValue.timeValueSeconds(30)) - .setSize(1) - .addStoredField("_id") - .setQuery(query) - .execute() - .actionGet(); + .setScroll(TimeValue.timeValueSeconds(30)) + .setSize(1) + .addStoredField("_id") + .setQuery(query) + .execute() + .actionGet(); assertNoFailures(scrollResponse); assertThat(scrollResponse.getHits().getTotalHits().value, equalTo(10L)); @@ -1321,9 +1377,7 @@ public void testParentChildQueriesViaScrollApi() throws Exception { do { assertThat(scrollResponse.getHits().getTotalHits().value, equalTo(10L)); scannedDocs += scrollResponse.getHits().getHits().length; - scrollResponse = client() - .prepareSearchScroll(scrollResponse.getScrollId()) - .setScroll(TimeValue.timeValueSeconds(30)).get(); + scrollResponse = client().prepareSearchScroll(scrollResponse.getScrollId()).setScroll(TimeValue.timeValueSeconds(30)).get(); } while (scrollResponse.getHits().getHits().length > 0); clearScroll(scrollResponse.getScrollId()); assertThat(scannedDocs, equalTo(10)); @@ -1333,16 +1387,16 @@ public void testParentChildQueriesViaScrollApi() throws Exception { private List createMinMaxDocBuilders() { List indexBuilders = new ArrayList<>(); // Parent 1 and its children - indexBuilders.add(createIndexRequest("test", "parent", "1", null, "id",1)); + indexBuilders.add(createIndexRequest("test", "parent", "1", null, "id", 1)); indexBuilders.add(createIndexRequest("test", "child", "10", "1", "foo", "one")); // Parent 2 and its children - indexBuilders.add(createIndexRequest("test", "parent", "2", null, "id",2)); + indexBuilders.add(createIndexRequest("test", "parent", "2", null, "id", 2)); indexBuilders.add(createIndexRequest("test", "child", "11", "2", "foo", "one")); indexBuilders.add(createIndexRequest("test", "child", "12", "2", "foo", "one two")); // Parent 3 and its children - indexBuilders.add(createIndexRequest("test", "parent", "3", null, "id",3)); + indexBuilders.add(createIndexRequest("test", "parent", "3", null, "id", 3)); indexBuilders.add(createIndexRequest("test", "child", "13", "3", "foo", "one")); indexBuilders.add(createIndexRequest("test", "child", "14", "3", "foo", "one two")); indexBuilders.add(createIndexRequest("test", "child", "15", "3", "foo", "one two three")); @@ -1359,26 +1413,22 @@ private List createMinMaxDocBuilders() { private SearchResponse minMaxQuery(ScoreMode scoreMode, int minChildren, Integer maxChildren) throws SearchPhaseExecutionException { HasChildQueryBuilder hasChildQuery = hasChildQuery( - "child", - QueryBuilders.functionScoreQuery(constantScoreQuery(QueryBuilders.termQuery("foo", "two")), - new FunctionScoreQueryBuilder.FilterFunctionBuilder[]{ - new FunctionScoreQueryBuilder.FilterFunctionBuilder(weightFactorFunction(1)), - new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.termQuery("foo", "three"), - weightFactorFunction(1)), - new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.termQuery("foo", "four"), - weightFactorFunction(1)) - }).boostMode(CombineFunction.REPLACE).scoreMode(FunctionScoreQuery.ScoreMode.SUM), scoreMode) - .minMaxChildren(minChildren, maxChildren != null ? maxChildren : HasChildQueryBuilder.DEFAULT_MAX_CHILDREN); - - return client() - .prepareSearch("test") - .setQuery(hasChildQuery) - .addSort("_score", SortOrder.DESC).addSort("id", SortOrder.ASC).get(); + "child", + QueryBuilders.functionScoreQuery( + constantScoreQuery(QueryBuilders.termQuery("foo", "two")), + new FunctionScoreQueryBuilder.FilterFunctionBuilder[] { + new FunctionScoreQueryBuilder.FilterFunctionBuilder(weightFactorFunction(1)), + new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.termQuery("foo", "three"), weightFactorFunction(1)), + new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.termQuery("foo", "four"), weightFactorFunction(1)) } + ).boostMode(CombineFunction.REPLACE).scoreMode(FunctionScoreQuery.ScoreMode.SUM), + scoreMode + ).minMaxChildren(minChildren, maxChildren != null ? maxChildren : HasChildQueryBuilder.DEFAULT_MAX_CHILDREN); + + return client().prepareSearch("test").setQuery(hasChildQuery).addSort("_score", SortOrder.DESC).addSort("id", SortOrder.ASC).get(); } public void testMinMaxChildren() throws Exception { - assertAcked(prepareCreate("test") - .setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); + assertAcked(prepareCreate("test").setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); ensureGreen(); indexRandom(true, createMinMaxDocBuilders().toArray(new IndexRequestBuilder[0])); @@ -1650,64 +1700,71 @@ public void testMinMaxChildren() throws Exception { } public void testHasParentInnerQueryType() { - assertAcked(prepareCreate("test") - .setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent-type", "child-type"))); + assertAcked( + prepareCreate("test").setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent-type", "child-type")) + ); createIndexRequest("test", "child-type", "child-id", "parent-id").get(); createIndexRequest("test", "parent-type", "parent-id", null).get(); refresh(); - //make sure that when we explicitly set a type, the inner query is executed in the context of the child type instead - SearchResponse searchResponse = client().prepareSearch("test").setQuery( - hasChildQuery("child-type", new IdsQueryBuilder().addIds("child-id"), ScoreMode.None)).get(); + // make sure that when we explicitly set a type, the inner query is executed in the context of the child type instead + SearchResponse searchResponse = client().prepareSearch("test") + .setQuery(hasChildQuery("child-type", new IdsQueryBuilder().addIds("child-id"), ScoreMode.None)) + .get(); assertSearchHits(searchResponse, "parent-id"); - //make sure that when we explicitly set a type, the inner query is executed in the context of the parent type instead - searchResponse = client().prepareSearch("test").setQuery( - hasParentQuery("parent-type", new IdsQueryBuilder().addIds("parent-id"), false)).get(); + // make sure that when we explicitly set a type, the inner query is executed in the context of the parent type instead + searchResponse = client().prepareSearch("test") + .setQuery(hasParentQuery("parent-type", new IdsQueryBuilder().addIds("parent-id"), false)) + .get(); assertSearchHits(searchResponse, "child-id"); } public void testHighlightersIgnoreParentChild() throws IOException { - assertAcked(prepareCreate("test") - .setMapping(jsonBuilder().startObject().startObject("properties") - .startObject("join_field") + assertAcked( + prepareCreate("test").setMapping( + jsonBuilder().startObject() + .startObject("properties") + .startObject("join_field") .field("type", "join") .startObject("relations") - .field("parent-type", "child-type") + .field("parent-type", "child-type") .endObject() - .endObject() - .startObject("searchText") + .endObject() + .startObject("searchText") .field("type", "text") .field("term_vector", "with_positions_offsets") .field("index_options", "offsets") - .endObject() - .endObject().endObject() - )); + .endObject() + .endObject() + .endObject() + ) + ); createIndexRequest("test", "parent-type", "parent-id", null, "searchText", "quick brown fox").get(); createIndexRequest("test", "child-type", "child-id", "parent-id", "searchText", "quick brown fox").get(); refresh(); - String[] highlightTypes = new String[] {"plain", "fvh", "unified"}; + String[] highlightTypes = new String[] { "plain", "fvh", "unified" }; for (String highlightType : highlightTypes) { logger.info("Testing with highlight type [{}]", highlightType); SearchResponse searchResponse = client().prepareSearch("test") - .setQuery(new BoolQueryBuilder() - .must(new MatchQueryBuilder("searchText", "fox")) - .must(new HasChildQueryBuilder("child-type", new MatchAllQueryBuilder(), ScoreMode.None)) - ) - .highlighter(new HighlightBuilder().field(new HighlightBuilder.Field("searchText").highlighterType(highlightType))) - .get(); + .setQuery( + new BoolQueryBuilder().must(new MatchQueryBuilder("searchText", "fox")) + .must(new HasChildQueryBuilder("child-type", new MatchAllQueryBuilder(), ScoreMode.None)) + ) + .highlighter(new HighlightBuilder().field(new HighlightBuilder.Field("searchText").highlighterType(highlightType))) + .get(); assertHitCount(searchResponse, 1); assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("parent-id")); HighlightField highlightField = searchResponse.getHits().getAt(0).getHighlightFields().get("searchText"); assertThat(highlightField.getFragments()[0].string(), equalTo("quick brown fox")); searchResponse = client().prepareSearch("test") - .setQuery(new BoolQueryBuilder() - .must(new MatchQueryBuilder("searchText", "fox")) - .must(new HasParentQueryBuilder("parent-type", new MatchAllQueryBuilder(), false)) - ) - .highlighter(new HighlightBuilder().field(new HighlightBuilder.Field("searchText").highlighterType(highlightType))) - .get(); + .setQuery( + new BoolQueryBuilder().must(new MatchQueryBuilder("searchText", "fox")) + .must(new HasParentQueryBuilder("parent-type", new MatchAllQueryBuilder(), false)) + ) + .highlighter(new HighlightBuilder().field(new HighlightBuilder.Field("searchText").highlighterType(highlightType))) + .get(); assertHitCount(searchResponse, 1); assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("child-id")); highlightField = searchResponse.getHits().getAt(0).getHighlightFields().get("searchText"); @@ -1716,16 +1773,17 @@ public void testHighlightersIgnoreParentChild() throws IOException { } public void testAliasesFilterWithHasChildQuery() throws Exception { - assertAcked(prepareCreate("my-index") - .setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); + assertAcked( + prepareCreate("my-index").setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child")) + ); createIndexRequest("my-index", "parent", "1", null).get(); createIndexRequest("my-index", "child", "2", "1").get(); refresh(); - assertAcked(admin().indices().prepareAliases().addAlias("my-index", "filter1", - hasChildQuery("child", matchAllQuery(), ScoreMode.None))); - assertAcked(admin().indices().prepareAliases().addAlias("my-index", "filter2", - hasParentQuery("parent", matchAllQuery(), false))); + assertAcked( + admin().indices().prepareAliases().addAlias("my-index", "filter1", hasChildQuery("child", matchAllQuery(), ScoreMode.None)) + ); + assertAcked(admin().indices().prepareAliases().addAlias("my-index", "filter2", hasParentQuery("parent", matchAllQuery(), false))); SearchResponse response = client().prepareSearch("filter1").get(); assertHitCount(response, 1); diff --git a/modules/parent-join/src/internalClusterTest/java/org/elasticsearch/join/query/InnerHitsIT.java b/modules/parent-join/src/internalClusterTest/java/org/elasticsearch/join/query/InnerHitsIT.java index a4cce3ca34782..33b3268a86fc7 100644 --- a/modules/parent-join/src/internalClusterTest/java/org/elasticsearch/join/query/InnerHitsIT.java +++ b/modules/parent-join/src/internalClusterTest/java/org/elasticsearch/join/query/InnerHitsIT.java @@ -38,7 +38,6 @@ import java.util.Map; import java.util.function.Function; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.common.xcontent.support.XContentMapValues.extractValue; import static org.elasticsearch.index.query.QueryBuilders.boolQuery; import static org.elasticsearch.index.query.QueryBuilders.constantScoreQuery; @@ -56,6 +55,7 @@ import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHit; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.hasId; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.greaterThanOrEqualTo; @@ -77,23 +77,29 @@ protected Map, Object>> pluginScripts() { } public void testSimpleParentChild() throws Exception { - assertAcked(prepareCreate("articles") - .setMapping(jsonBuilder().startObject().startObject("_doc").startObject("properties") - .startObject("join_field") + assertAcked( + prepareCreate("articles").setMapping( + jsonBuilder().startObject() + .startObject("_doc") + .startObject("properties") + .startObject("join_field") .field("type", "join") .startObject("relations") - .field("article", "comment") + .field("article", "comment") + .endObject() .endObject() - .endObject() - .startObject("title") + .startObject("title") .field("type", "text") - .endObject() - .startObject("message") + .endObject() + .startObject("message") .field("type", "text") .field("fielddata", true) - .endObject() - .endObject().endObject().endObject() - )); + .endObject() + .endObject() + .endObject() + .endObject() + ) + ); List requests = new ArrayList<>(); requests.add(createIndexRequest("articles", "article", "p1", null, "title", "quick brown fox")); @@ -107,8 +113,7 @@ public void testSimpleParentChild() throws Exception { indexRandom(true, requests); SearchResponse response = client().prepareSearch("articles") - .setQuery(hasChildQuery("comment", matchQuery("message", "fox"), ScoreMode.None) - .innerHit(new InnerHitBuilder())) + .setQuery(hasChildQuery("comment", matchQuery("message", "fox"), ScoreMode.None).innerHit(new InnerHitBuilder())) .get(); assertNoFailures(response); assertHitCount(response, 1); @@ -124,8 +129,11 @@ public void testSimpleParentChild() throws Exception { final boolean seqNoAndTerm = randomBoolean(); response = client().prepareSearch("articles") - .setQuery(hasChildQuery("comment", matchQuery("message", "elephant"), ScoreMode.None) - .innerHit(new InnerHitBuilder().setSeqNoAndPrimaryTerm(seqNoAndTerm))) + .setQuery( + hasChildQuery("comment", matchQuery("message", "elephant"), ScoreMode.None).innerHit( + new InnerHitBuilder().setSeqNoAndPrimaryTerm(seqNoAndTerm) + ) + ) .get(); assertNoFailures(response); assertHitCount(response, 1); @@ -158,13 +166,14 @@ public void testSimpleParentChild() throws Exception { response = client().prepareSearch("articles") .setQuery( hasChildQuery("comment", matchQuery("message", "fox"), ScoreMode.None).innerHit( - new InnerHitBuilder() - .addFetchField("message") + new InnerHitBuilder().addFetchField("message") .setHighlightBuilder(new HighlightBuilder().field("message")) - .setExplain(true).setSize(1) - .addScriptField("script", new Script(ScriptType.INLINE, MockScriptEngine.NAME, "5", - Collections.emptyMap()))) - ).get(); + .setExplain(true) + .setSize(1) + .addScriptField("script", new Script(ScriptType.INLINE, MockScriptEngine.NAME, "5", Collections.emptyMap())) + ) + ) + .get(); assertNoFailures(response); innerHits = response.getHits().getAt(0).getInnerHits().get("comment"); assertThat(innerHits.getHits().length, equalTo(1)); @@ -177,7 +186,9 @@ public void testSimpleParentChild() throws Exception { .setQuery( hasChildQuery("comment", matchQuery("message", "fox"), ScoreMode.None).innerHit( new InnerHitBuilder().addDocValueField("message").setSize(1) - )).get(); + ) + ) + .get(); assertNoFailures(response); innerHits = response.getHits().getAt(0).getInnerHits().get("comment"); assertThat(innerHits.getHits().length, equalTo(1)); @@ -185,19 +196,25 @@ public void testSimpleParentChild() throws Exception { } public void testRandomParentChild() throws Exception { - assertAcked(prepareCreate("idx") - .setMapping(jsonBuilder().startObject().startObject("_doc").startObject("properties") - .startObject("id") + assertAcked( + prepareCreate("idx").setMapping( + jsonBuilder().startObject() + .startObject("_doc") + .startObject("properties") + .startObject("id") .field("type", "keyword") - .endObject() - .startObject("join_field") + .endObject() + .startObject("join_field") .field("type", "join") .startObject("relations") - .field("parent", new String[] {"child1", "child2"}) + .field("parent", new String[] { "child1", "child2" }) + .endObject() + .endObject() .endObject() - .endObject() - .endObject().endObject().endObject() - )); + .endObject() + .endObject() + ) + ); int numDocs = scaledRandomIntBetween(5, 50); List requestBuilders = new ArrayList<>(); @@ -224,12 +241,20 @@ public void testRandomParentChild() throws Exception { int size = randomIntBetween(0, numDocs); BoolQueryBuilder boolQuery = new BoolQueryBuilder(); - boolQuery.should(constantScoreQuery(hasChildQuery("child1", matchAllQuery(), ScoreMode.None) - .innerHit(new InnerHitBuilder().setName("a") - .addSort(new FieldSortBuilder("id").order(SortOrder.ASC)).setSize(size)))); - boolQuery.should(constantScoreQuery(hasChildQuery("child2", matchAllQuery(), ScoreMode.None) - .innerHit(new InnerHitBuilder().setName("b") - .addSort(new FieldSortBuilder("id").order(SortOrder.ASC)).setSize(size)))); + boolQuery.should( + constantScoreQuery( + hasChildQuery("child1", matchAllQuery(), ScoreMode.None).innerHit( + new InnerHitBuilder().setName("a").addSort(new FieldSortBuilder("id").order(SortOrder.ASC)).setSize(size) + ) + ) + ); + boolQuery.should( + constantScoreQuery( + hasChildQuery("child2", matchAllQuery(), ScoreMode.None).innerHit( + new InnerHitBuilder().setName("b").addSort(new FieldSortBuilder("id").order(SortOrder.ASC)).setSize(size) + ) + ) + ); SearchResponse searchResponse = client().prepareSearch("idx") .setSize(numDocs) .addSort("id", SortOrder.ASC) @@ -250,7 +275,7 @@ public void testRandomParentChild() throws Exception { SearchHits inner = searchHit.getInnerHits().get("a"); assertThat(inner.getTotalHits().value, equalTo((long) child1InnerObjects[parent])); for (int child = 0; child < child1InnerObjects[parent] && child < size; child++) { - SearchHit innerHit = inner.getAt(child); + SearchHit innerHit = inner.getAt(child); String childId = String.format(Locale.ENGLISH, "c1_%04d", offset1 + child); assertThat(innerHit.getId(), equalTo(childId)); assertThat(innerHit.getNestedIdentity(), nullValue()); @@ -270,26 +295,46 @@ public void testRandomParentChild() throws Exception { } public void testInnerHitsOnHasParent() throws Exception { - assertAcked(prepareCreate("stack") - .setMapping(addFieldMappings(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "question", "answer"), - "body", "text"))); + assertAcked( + prepareCreate("stack").setMapping( + addFieldMappings(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "question", "answer"), "body", "text") + ) + ); List requests = new ArrayList<>(); - requests.add(createIndexRequest("stack", "question", "1", null, "body", "I'm using HTTPS + Basic authentication " - + "to protect a resource. How can I throttle authentication attempts to protect against brute force attacks?")); + requests.add( + createIndexRequest( + "stack", + "question", + "1", + null, + "body", + "I'm using HTTPS + Basic authentication " + + "to protect a resource. How can I throttle authentication attempts to protect against brute force attacks?" + ) + ); requests.add(createIndexRequest("stack", "answer", "3", "1", "body", "install fail2ban and enable rules for apache")); - requests.add(createIndexRequest("stack", "question", "2", null, "body", - "I have firewall rules set up and also denyhosts installed.\\ndo I also need to install fail2ban?")); - requests.add(createIndexRequest("stack", "answer", "4", "2", "body", - "Denyhosts protects only ssh; Fail2Ban protects all daemons.")); + requests.add( + createIndexRequest( + "stack", + "question", + "2", + null, + "body", + "I have firewall rules set up and also denyhosts installed.\\ndo I also need to install fail2ban?" + ) + ); + requests.add( + createIndexRequest("stack", "answer", "4", "2", "body", "Denyhosts protects only ssh; Fail2Ban protects all daemons.") + ); indexRandom(true, requests); SearchResponse response = client().prepareSearch("stack") .addSort("id", SortOrder.ASC) .setQuery( - boolQuery() - .must(matchQuery("body", "fail2ban")) + boolQuery().must(matchQuery("body", "fail2ban")) .must(hasParentQuery("question", matchAllQuery(), false).innerHit(new InnerHitBuilder())) - ).get(); + ) + .get(); assertNoFailures(response); assertHitCount(response, 2); @@ -305,10 +350,17 @@ public void testInnerHitsOnHasParent() throws Exception { } public void testParentChildMultipleLayers() throws Exception { - assertAcked(prepareCreate("articles") - .setMapping( - addFieldMappings(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, - "article", "comment", "comment", "remark"), "title", "text", "message", "text"))); + assertAcked( + prepareCreate("articles").setMapping( + addFieldMappings( + buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "article", "comment", "comment", "remark"), + "title", + "text", + "message", + "text" + ) + ) + ); List requests = new ArrayList<>(); requests.add(createIndexRequest("articles", "article", "1", null, "title", "quick brown fox")); @@ -320,9 +372,13 @@ public void testParentChildMultipleLayers() throws Exception { indexRandom(true, requests); SearchResponse response = client().prepareSearch("articles") - .setQuery(hasChildQuery("comment", - hasChildQuery("remark", matchQuery("message", "good"), ScoreMode.None).innerHit(new InnerHitBuilder()), - ScoreMode.None).innerHit(new InnerHitBuilder())) + .setQuery( + hasChildQuery( + "comment", + hasChildQuery("remark", matchQuery("message", "good"), ScoreMode.None).innerHit(new InnerHitBuilder()), + ScoreMode.None + ).innerHit(new InnerHitBuilder()) + ) .get(); assertNoFailures(response); @@ -339,9 +395,13 @@ public void testParentChildMultipleLayers() throws Exception { assertThat(innerHits.getAt(0).getId(), equalTo("5")); response = client().prepareSearch("articles") - .setQuery(hasChildQuery("comment", - hasChildQuery("remark", matchQuery("message", "bad"), ScoreMode.None).innerHit(new InnerHitBuilder()), - ScoreMode.None).innerHit(new InnerHitBuilder())) + .setQuery( + hasChildQuery( + "comment", + hasChildQuery("remark", matchQuery("message", "bad"), ScoreMode.None).innerHit(new InnerHitBuilder()), + ScoreMode.None + ).innerHit(new InnerHitBuilder()) + ) .get(); assertNoFailures(response); @@ -359,9 +419,22 @@ public void testParentChildMultipleLayers() throws Exception { } public void testRoyals() throws Exception { - assertAcked(prepareCreate("royals") - .setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, - "king", "prince", "prince", "duke", "duke", "earl", "earl", "baron"))); + assertAcked( + prepareCreate("royals").setMapping( + buildParentJoinFieldMappingFromSimplifiedDef( + "join_field", + true, + "king", + "prince", + "prince", + "duke", + "duke", + "earl", + "earl", + "baron" + ) + ) + ); List requests = new ArrayList<>(); requests.add(createIndexRequest("royals", "king", "king", null)); @@ -378,20 +451,25 @@ public void testRoyals() throws Exception { indexRandom(true, requests); SearchResponse response = client().prepareSearch("royals") - .setQuery(boolQuery() - .filter(hasParentQuery("prince", - hasParentQuery("king", matchAllQuery(), false).innerHit(new InnerHitBuilder().setName("kings")), - false).innerHit(new InnerHitBuilder().setName("princes")) - ) - .filter(hasChildQuery("earl", - hasChildQuery("baron", matchAllQuery(), ScoreMode.None) - .innerHit(new InnerHitBuilder().setName("barons")), - ScoreMode.None).innerHit(new InnerHitBuilder() - .addSort(SortBuilders.fieldSort("id").order(SortOrder.ASC)) - .setName("earls") - .setSize(4)) + .setQuery( + boolQuery().filter( + hasParentQuery( + "prince", + hasParentQuery("king", matchAllQuery(), false).innerHit(new InnerHitBuilder().setName("kings")), + false + ).innerHit(new InnerHitBuilder().setName("princes")) ) - ).get(); + .filter( + hasChildQuery( + "earl", + hasChildQuery("baron", matchAllQuery(), ScoreMode.None).innerHit(new InnerHitBuilder().setName("barons")), + ScoreMode.None + ).innerHit( + new InnerHitBuilder().addSort(SortBuilders.fieldSort("id").order(SortOrder.ASC)).setName("earls").setSize(4) + ) + ) + ) + .get(); assertHitCount(response, 1); assertThat(response.getHits().getAt(0).getId(), equalTo("duke")); @@ -428,8 +506,7 @@ public void testRoyals() throws Exception { } public void testMatchesQueriesParentChildInnerHits() throws Exception { - assertAcked(prepareCreate("index") - .setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); + assertAcked(prepareCreate("index").setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); List requests = new ArrayList<>(); requests.add(createIndexRequest("index", "parent", "1", null)); requests.add(createIndexRequest("index", "child", "3", "1", "field", "value1")); @@ -439,8 +516,9 @@ public void testMatchesQueriesParentChildInnerHits() throws Exception { indexRandom(true, requests); SearchResponse response = client().prepareSearch("index") - .setQuery(hasChildQuery("child", matchQuery("field", "value1").queryName("_name1"), ScoreMode.None) - .innerHit(new InnerHitBuilder())) + .setQuery( + hasChildQuery("child", matchQuery("field", "value1").queryName("_name1"), ScoreMode.None).innerHit(new InnerHitBuilder()) + ) .addSort("id", SortOrder.ASC) .get(); assertHitCount(response, 2); @@ -454,12 +532,10 @@ public void testMatchesQueriesParentChildInnerHits() throws Exception { assertThat(response.getHits().getAt(1).getInnerHits().get("child").getAt(0).getMatchedQueries().length, equalTo(1)); assertThat(response.getHits().getAt(1).getInnerHits().get("child").getAt(0).getMatchedQueries()[0], equalTo("_name1")); - QueryBuilder query = hasChildQuery("child", matchQuery("field", "value2").queryName("_name2"), ScoreMode.None) - .innerHit(new InnerHitBuilder()); - response = client().prepareSearch("index") - .setQuery(query) - .addSort("id", SortOrder.ASC) - .get(); + QueryBuilder query = hasChildQuery("child", matchQuery("field", "value2").queryName("_name2"), ScoreMode.None).innerHit( + new InnerHitBuilder() + ); + response = client().prepareSearch("index").setQuery(query).addSort("id", SortOrder.ASC).get(); assertHitCount(response, 1); assertThat(response.getHits().getAt(0).getId(), equalTo("1")); assertThat(response.getHits().getAt(0).getInnerHits().get("child").getTotalHits().value, equalTo(1L)); @@ -468,9 +544,12 @@ public void testMatchesQueriesParentChildInnerHits() throws Exception { } public void testUseMaxDocInsteadOfSize() throws Exception { - assertAcked(prepareCreate("index1") - .setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child"))); - client().admin().indices().prepareUpdateSettings("index1") + assertAcked( + prepareCreate("index1").setMapping(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child")) + ); + client().admin() + .indices() + .prepareUpdateSettings("index1") .setSettings(Collections.singletonMap(IndexSettings.MAX_INNER_RESULT_WINDOW_SETTING.getKey(), ArrayUtil.MAX_ARRAY_LENGTH)) .get(); List requests = new ArrayList<>(); @@ -478,26 +557,38 @@ public void testUseMaxDocInsteadOfSize() throws Exception { requests.add(createIndexRequest("index1", "child", "2", "1", "field", "value1")); indexRandom(true, requests); - QueryBuilder query = hasChildQuery("child", matchQuery("field", "value1"), ScoreMode.None) - .innerHit(new InnerHitBuilder().setSize(ArrayUtil.MAX_ARRAY_LENGTH - 1)); - SearchResponse response = client().prepareSearch("index1") - .setQuery(query) - .get(); + QueryBuilder query = hasChildQuery("child", matchQuery("field", "value1"), ScoreMode.None).innerHit( + new InnerHitBuilder().setSize(ArrayUtil.MAX_ARRAY_LENGTH - 1) + ); + SearchResponse response = client().prepareSearch("index1").setQuery(query).get(); assertNoFailures(response); assertHitCount(response, 1); } public void testNestedInnerHitWrappedInParentChildInnerhit() { - assertAcked(prepareCreate("test") - .setMapping(addFieldMappings(buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, - "parent_type", "child_type"), "nested_type", "nested"))); + assertAcked( + prepareCreate("test").setMapping( + addFieldMappings( + buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent_type", "child_type"), + "nested_type", + "nested" + ) + ) + ); createIndexRequest("test", "parent_type", "1", null, "key", "value").get(); createIndexRequest("test", "child_type", "2", "1", "nested_type", Collections.singletonMap("key", "value")).get(); refresh(); SearchResponse response = client().prepareSearch("test") - .setQuery(boolQuery().must(matchQuery("key", "value")) - .should(hasChildQuery("child_type", nestedQuery("nested_type", matchAllQuery(), ScoreMode.None) - .innerHit(new InnerHitBuilder()), ScoreMode.None).innerHit(new InnerHitBuilder()))) + .setQuery( + boolQuery().must(matchQuery("key", "value")) + .should( + hasChildQuery( + "child_type", + nestedQuery("nested_type", matchAllQuery(), ScoreMode.None).innerHit(new InnerHitBuilder()), + ScoreMode.None + ).innerHit(new InnerHitBuilder()) + ) + ) .get(); assertHitCount(response, 1); SearchHit hit = response.getHits().getAt(0); @@ -507,10 +598,14 @@ public void testNestedInnerHitWrappedInParentChildInnerhit() { } public void testInnerHitsWithIgnoreUnmapped() { - assertAcked(prepareCreate("index1") - .setMapping(addFieldMappings( - buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent_type", "child_type"), - "nested_type", "nested")) + assertAcked( + prepareCreate("index1").setMapping( + addFieldMappings( + buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent_type", "child_type"), + "nested_type", + "nested" + ) + ) ); assertAcked(prepareCreate("index2")); createIndexRequest("index1", "parent_type", "1", null, "nested_type", Collections.singletonMap("key", "value")).get(); @@ -519,10 +614,11 @@ public void testInnerHitsWithIgnoreUnmapped() { refresh(); SearchResponse response = client().prepareSearch("index1", "index2") - .setQuery(boolQuery() - .should(hasChildQuery("child_type", matchAllQuery(), ScoreMode.None).ignoreUnmapped(true) - .innerHit(new InnerHitBuilder().setIgnoreUnmapped(true))) - .should(termQuery("key", "value")) + .setQuery( + boolQuery().should( + hasChildQuery("child_type", matchAllQuery(), ScoreMode.None).ignoreUnmapped(true) + .innerHit(new InnerHitBuilder().setIgnoreUnmapped(true)) + ).should(termQuery("key", "value")) ) .get(); assertNoFailures(response); @@ -531,46 +627,72 @@ public void testInnerHitsWithIgnoreUnmapped() { } public void testTooHighResultWindow() { - assertAcked(prepareCreate("index1") - .setMapping(addFieldMappings( - buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent_type", "child_type"), - "nested_type", "nested")) + assertAcked( + prepareCreate("index1").setMapping( + addFieldMappings( + buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent_type", "child_type"), + "nested_type", + "nested" + ) + ) ); createIndexRequest("index1", "parent_type", "1", null, "nested_type", Collections.singletonMap("key", "value")).get(); createIndexRequest("index1", "child_type", "2", "1").get(); refresh(); SearchResponse response = client().prepareSearch("index1") - .setQuery(hasChildQuery("child_type", matchAllQuery(), ScoreMode.None).ignoreUnmapped(true) - .innerHit(new InnerHitBuilder().setFrom(50).setSize(10).setName("_name"))) + .setQuery( + hasChildQuery("child_type", matchAllQuery(), ScoreMode.None).ignoreUnmapped(true) + .innerHit(new InnerHitBuilder().setFrom(50).setSize(10).setName("_name")) + ) .get(); assertNoFailures(response); assertHitCount(response, 1); - Exception e = expectThrows(SearchPhaseExecutionException.class, () -> client().prepareSearch("index1") - .setQuery(hasChildQuery("child_type", matchAllQuery(), ScoreMode.None).ignoreUnmapped(true) - .innerHit(new InnerHitBuilder().setFrom(100).setSize(10).setName("_name"))) - .get()); - assertThat(e.getCause().getMessage(), - containsString("the inner hit definition's [_name]'s from + size must be less than or equal to: [100] but was [110]")); - e = expectThrows(SearchPhaseExecutionException.class, () -> client().prepareSearch("index1") - .setQuery(hasChildQuery("child_type", matchAllQuery(), ScoreMode.None).ignoreUnmapped(true) - .innerHit(new InnerHitBuilder().setFrom(10).setSize(100).setName("_name"))) - .get()); - assertThat(e.getCause().getMessage(), - containsString("the inner hit definition's [_name]'s from + size must be less than or equal to: [100] but was [110]")); - - client().admin().indices().prepareUpdateSettings("index1") + Exception e = expectThrows( + SearchPhaseExecutionException.class, + () -> client().prepareSearch("index1") + .setQuery( + hasChildQuery("child_type", matchAllQuery(), ScoreMode.None).ignoreUnmapped(true) + .innerHit(new InnerHitBuilder().setFrom(100).setSize(10).setName("_name")) + ) + .get() + ); + assertThat( + e.getCause().getMessage(), + containsString("the inner hit definition's [_name]'s from + size must be less than or equal to: [100] but was [110]") + ); + e = expectThrows( + SearchPhaseExecutionException.class, + () -> client().prepareSearch("index1") + .setQuery( + hasChildQuery("child_type", matchAllQuery(), ScoreMode.None).ignoreUnmapped(true) + .innerHit(new InnerHitBuilder().setFrom(10).setSize(100).setName("_name")) + ) + .get() + ); + assertThat( + e.getCause().getMessage(), + containsString("the inner hit definition's [_name]'s from + size must be less than or equal to: [100] but was [110]") + ); + + client().admin() + .indices() + .prepareUpdateSettings("index1") .setSettings(Collections.singletonMap(IndexSettings.MAX_INNER_RESULT_WINDOW_SETTING.getKey(), 110)) .get(); response = client().prepareSearch("index1") - .setQuery(hasChildQuery("child_type", matchAllQuery(), ScoreMode.None).ignoreUnmapped(true) - .innerHit(new InnerHitBuilder().setFrom(100).setSize(10).setName("_name"))) + .setQuery( + hasChildQuery("child_type", matchAllQuery(), ScoreMode.None).ignoreUnmapped(true) + .innerHit(new InnerHitBuilder().setFrom(100).setSize(10).setName("_name")) + ) .get(); assertNoFailures(response); response = client().prepareSearch("index1") - .setQuery(hasChildQuery("child_type", matchAllQuery(), ScoreMode.None).ignoreUnmapped(true) - .innerHit(new InnerHitBuilder().setFrom(10).setSize(100).setName("_name"))) + .setQuery( + hasChildQuery("child_type", matchAllQuery(), ScoreMode.None).ignoreUnmapped(true) + .innerHit(new InnerHitBuilder().setFrom(10).setSize(100).setName("_name")) + ) .get(); assertNoFailures(response); } diff --git a/modules/parent-join/src/internalClusterTest/java/org/elasticsearch/join/query/ParentChildTestCase.java b/modules/parent-join/src/internalClusterTest/java/org/elasticsearch/join/query/ParentChildTestCase.java index 84b7ec8d96b10..b4846a1c003a6 100644 --- a/modules/parent-join/src/internalClusterTest/java/org/elasticsearch/join/query/ParentChildTestCase.java +++ b/modules/parent-join/src/internalClusterTest/java/org/elasticsearch/join/query/ParentChildTestCase.java @@ -10,14 +10,14 @@ import org.elasticsearch.action.index.IndexRequestBuilder; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.index.IndexModule; import org.elasticsearch.join.ParentJoinPlugin; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.test.ESIntegTestCase; import org.elasticsearch.test.InternalSettingsPlugin; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.json.JsonXContent; import java.io.IOException; import java.util.Arrays; @@ -41,7 +41,8 @@ protected Collection> nodePlugins() { @Override public Settings indexSettings() { - Settings.Builder builder = Settings.builder().put(super.indexSettings()) + Settings.Builder builder = Settings.builder() + .put(super.indexSettings()) // aggressive filter caching so that we can assert on the filter cache size .put(IndexModule.INDEX_QUERY_CACHE_ENABLED_SETTING.getKey(), true) .put(IndexModule.INDEX_QUERY_CACHE_EVERYTHING_SETTING.getKey(), true); @@ -58,23 +59,25 @@ protected IndexRequestBuilder createIndexRequest(String index, String type, Stri return createIndexRequest(index, type, id, parentId, source); } - protected IndexRequestBuilder createIndexRequest(String index, String type, String id, String parentId, - XContentBuilder builder) throws IOException { + protected IndexRequestBuilder createIndexRequest(String index, String type, String id, String parentId, XContentBuilder builder) + throws IOException { Map source = XContentHelper.convertToMap(JsonXContent.jsonXContent, Strings.toString(builder), false); return createIndexRequest(index, type, id, parentId, source); } - public static Map buildParentJoinFieldMappingFromSimplifiedDef(String joinFieldName, - boolean eagerGlobalOrdinals, - String... relations) { + public static Map buildParentJoinFieldMappingFromSimplifiedDef( + String joinFieldName, + boolean eagerGlobalOrdinals, + String... relations + ) { Map fields = new HashMap<>(); Map joinField = new HashMap<>(); joinField.put("type", "join"); joinField.put("eager_global_ordinals", eagerGlobalOrdinals); Map relationMap = new HashMap<>(); - for (int i = 0; i < relations.length; i+=2) { - String[] children = relations[i+1].split(","); + for (int i = 0; i < relations.length; i += 2) { + String[] children = relations[i + 1].split(","); if (children.length > 1) { relationMap.put(relations[i], children); } else { @@ -90,7 +93,7 @@ public static Map buildParentJoinFieldMappingFromSimplifiedDef(S @SuppressWarnings("unchecked") public static Map addFieldMappings(Map map, String... fields) { Map propsMap = (Map) map.get("properties"); - for (int i = 0; i < fields.length; i+=2) { + for (int i = 0; i < fields.length; i += 2) { String field = fields[i]; String type = fields[i + 1]; propsMap.put(field, Collections.singletonMap("type", type)); diff --git a/modules/parent-join/src/main/java/org/elasticsearch/join/ParentJoinPlugin.java b/modules/parent-join/src/main/java/org/elasticsearch/join/ParentJoinPlugin.java index 7c33b92aaae6e..675771c5f7541 100644 --- a/modules/parent-join/src/main/java/org/elasticsearch/join/ParentJoinPlugin.java +++ b/modules/parent-join/src/main/java/org/elasticsearch/join/ParentJoinPlugin.java @@ -28,8 +28,7 @@ public class ParentJoinPlugin extends Plugin implements SearchPlugin, MapperPlugin { - public ParentJoinPlugin() { - } + public ParentJoinPlugin() {} @Override public List> getQueries() { diff --git a/modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/Children.java b/modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/Children.java index 38a0b3d4e67d1..c994f80e9d333 100644 --- a/modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/Children.java +++ b/modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/Children.java @@ -13,5 +13,4 @@ /** * An single bucket aggregation that translates parent documents to their children documents. */ -public interface Children extends SingleBucketAggregation { -} +public interface Children extends SingleBucketAggregation {} diff --git a/modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/ChildrenAggregationBuilder.java b/modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/ChildrenAggregationBuilder.java index b5a7b40dc77e7..d314710f8d7ca 100644 --- a/modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/ChildrenAggregationBuilder.java +++ b/modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/ChildrenAggregationBuilder.java @@ -12,8 +12,6 @@ import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.join.mapper.Joiner; import org.elasticsearch.search.aggregations.AggregationBuilder; @@ -26,6 +24,8 @@ import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry; import org.elasticsearch.search.aggregations.support.ValuesSourceType; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.Map; @@ -53,8 +53,7 @@ public ChildrenAggregationBuilder(String name, String childType) { this.childType = childType; } - protected ChildrenAggregationBuilder(ChildrenAggregationBuilder clone, - Builder factoriesBuilder, Map metadata) { + protected ChildrenAggregationBuilder(ChildrenAggregationBuilder clone, Builder factoriesBuilder, Map metadata) { super(clone, factoriesBuilder, metadata); this.childType = clone.childType; this.childFilter = clone.childFilter; @@ -89,12 +88,13 @@ public BucketCardinality bucketCardinality() { return BucketCardinality.ONE; } - protected ValuesSourceAggregatorFactory innerBuild(AggregationContext context, - ValuesSourceConfig config, - AggregatorFactory parent, - Builder subFactoriesBuilder) throws IOException { - return new ChildrenAggregatorFactory(name, config, childFilter, parentFilter, context, parent, - subFactoriesBuilder, metadata); + protected ValuesSourceAggregatorFactory innerBuild( + AggregationContext context, + ValuesSourceConfig config, + AggregatorFactory parent, + Builder subFactoriesBuilder + ) throws IOException { + return new ChildrenAggregatorFactory(name, config, childFilter, parentFilter, context, parent, subFactoriesBuilder, metadata); } @Override @@ -133,8 +133,10 @@ public static ChildrenAggregationBuilder parse(String aggregationName, XContentP if ("type".equals(currentFieldName)) { childType = parser.text(); } else { - throw new ParsingException(parser.getTokenLocation(), - "Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "]."); + throw new ParsingException( + parser.getTokenLocation(), + "Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "]." + ); } } else { throw new ParsingException(parser.getTokenLocation(), "Unexpected token " + token + " in [" + aggregationName + "]."); @@ -142,8 +144,10 @@ public static ChildrenAggregationBuilder parse(String aggregationName, XContentP } if (childType == null) { - throw new ParsingException(parser.getTokenLocation(), - "Missing [child_type] field for children aggregation [" + aggregationName + "]"); + throw new ParsingException( + parser.getTokenLocation(), + "Missing [child_type] field for children aggregation [" + aggregationName + "]" + ); } return new ChildrenAggregationBuilder(aggregationName, childType); diff --git a/modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/ChildrenAggregatorFactory.java b/modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/ChildrenAggregatorFactory.java index aff07ad8ccfe4..83cc38636b101 100644 --- a/modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/ChildrenAggregatorFactory.java +++ b/modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/ChildrenAggregatorFactory.java @@ -32,14 +32,16 @@ public class ChildrenAggregatorFactory extends ValuesSourceAggregatorFactory { private final Query parentFilter; private final Query childFilter; - public ChildrenAggregatorFactory(String name, - ValuesSourceConfig config, - Query childFilter, - Query parentFilter, - AggregationContext context, - AggregatorFactory parent, - AggregatorFactories.Builder subFactoriesBuilder, - Map metadata) throws IOException { + public ChildrenAggregatorFactory( + String name, + ValuesSourceConfig config, + Query childFilter, + Query parentFilter, + AggregationContext context, + AggregatorFactory parent, + AggregatorFactories.Builder subFactoriesBuilder, + Map metadata + ) throws IOException { super(name, config, context, parent, subFactoriesBuilder, metadata); this.childFilter = childFilter; @@ -61,13 +63,24 @@ protected Aggregator doCreateInternal(Aggregator parent, CardinalityUpperBound c throws IOException { ValuesSource rawValuesSource = config.getValuesSource(); if (rawValuesSource instanceof WithOrdinals == false) { - throw new AggregationExecutionException("ValuesSource type " + rawValuesSource.toString() + - "is not supported for aggregation " + this.name()); + throw new AggregationExecutionException( + "ValuesSource type " + rawValuesSource.toString() + "is not supported for aggregation " + this.name() + ); } WithOrdinals valuesSource = (WithOrdinals) rawValuesSource; long maxOrd = valuesSource.globalMaxOrd(context.searcher()); - return new ParentToChildrenAggregator(name, factories, context, parent, childFilter, - parentFilter, valuesSource, maxOrd, cardinality, metadata); + return new ParentToChildrenAggregator( + name, + factories, + context, + parent, + childFilter, + parentFilter, + valuesSource, + maxOrd, + cardinality, + metadata + ); } @Override diff --git a/modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/ChildrenToParentAggregator.java b/modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/ChildrenToParentAggregator.java index 0731c9366a793..3f32807a4da66 100644 --- a/modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/ChildrenToParentAggregator.java +++ b/modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/ChildrenToParentAggregator.java @@ -8,7 +8,6 @@ package org.elasticsearch.join.aggregations; import org.apache.lucene.search.Query; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.search.aggregations.Aggregator; import org.elasticsearch.search.aggregations.AggregatorFactories; import org.elasticsearch.search.aggregations.CardinalityUpperBound; @@ -16,6 +15,7 @@ import org.elasticsearch.search.aggregations.bucket.BucketsAggregator; import org.elasticsearch.search.aggregations.support.AggregationContext; import org.elasticsearch.search.aggregations.support.ValuesSource; +import org.elasticsearch.xcontent.ParseField; import java.io.IOException; import java.util.Map; @@ -27,17 +27,32 @@ public class ChildrenToParentAggregator extends ParentJoinAggregator { static final ParseField TYPE_FIELD = new ParseField("type"); - public ChildrenToParentAggregator(String name, AggregatorFactories factories, - AggregationContext context, Aggregator parent, Query childFilter, - Query parentFilter, ValuesSource.Bytes.WithOrdinals valuesSource, - long maxOrd, CardinalityUpperBound cardinality, Map metadata) throws IOException { + public ChildrenToParentAggregator( + String name, + AggregatorFactories factories, + AggregationContext context, + Aggregator parent, + Query childFilter, + Query parentFilter, + ValuesSource.Bytes.WithOrdinals valuesSource, + long maxOrd, + CardinalityUpperBound cardinality, + Map metadata + ) throws IOException { super(name, factories, context, parent, childFilter, parentFilter, valuesSource, maxOrd, cardinality, metadata); } @Override public InternalAggregation[] buildAggregations(long[] owningBucketOrds) throws IOException { - return buildAggregationsForSingleBucket(owningBucketOrds, (owningBucketOrd, subAggregationResults) -> - new InternalParent(name, bucketDocCount(owningBucketOrd), subAggregationResults, metadata())); + return buildAggregationsForSingleBucket( + owningBucketOrds, + (owningBucketOrd, subAggregationResults) -> new InternalParent( + name, + bucketDocCount(owningBucketOrd), + subAggregationResults, + metadata() + ) + ); } @Override diff --git a/modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/Parent.java b/modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/Parent.java index 218f234d11ea3..d566c6422e934 100644 --- a/modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/Parent.java +++ b/modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/Parent.java @@ -13,5 +13,4 @@ /** * An single bucket aggregation that translates child documents to their parent documents. */ -public interface Parent extends SingleBucketAggregation { -} +public interface Parent extends SingleBucketAggregation {} diff --git a/modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/ParentAggregationBuilder.java b/modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/ParentAggregationBuilder.java index b0e270f4a59cf..9ea31e7eaa097 100644 --- a/modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/ParentAggregationBuilder.java +++ b/modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/ParentAggregationBuilder.java @@ -12,8 +12,6 @@ import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.join.mapper.Joiner; import org.elasticsearch.search.aggregations.AggregationBuilder; @@ -26,6 +24,8 @@ import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry; import org.elasticsearch.search.aggregations.support.ValuesSourceType; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.Map; @@ -53,8 +53,7 @@ public ParentAggregationBuilder(String name, String childType) { this.childType = childType; } - protected ParentAggregationBuilder(ParentAggregationBuilder clone, - Builder factoriesBuilder, Map metadata) { + protected ParentAggregationBuilder(ParentAggregationBuilder clone, Builder factoriesBuilder, Map metadata) { super(clone, factoriesBuilder, metadata); this.childType = clone.childType; this.childFilter = clone.childFilter; @@ -90,12 +89,13 @@ public BucketCardinality bucketCardinality() { } @Override - protected ValuesSourceAggregatorFactory innerBuild(AggregationContext context, - ValuesSourceConfig config, - AggregatorFactory parent, - Builder subFactoriesBuilder) throws IOException { - return new ParentAggregatorFactory(name, config, childFilter, parentFilter, context, parent, - subFactoriesBuilder, metadata); + protected ValuesSourceAggregatorFactory innerBuild( + AggregationContext context, + ValuesSourceConfig config, + AggregatorFactory parent, + Builder subFactoriesBuilder + ) throws IOException { + return new ParentAggregatorFactory(name, config, childFilter, parentFilter, context, parent, subFactoriesBuilder, metadata); } @Override @@ -132,8 +132,10 @@ public static ParentAggregationBuilder parse(String aggregationName, XContentPar if ("type".equals(currentFieldName)) { childType = parser.text(); } else { - throw new ParsingException(parser.getTokenLocation(), - "Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "]."); + throw new ParsingException( + parser.getTokenLocation(), + "Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "]." + ); } } else { throw new ParsingException(parser.getTokenLocation(), "Unexpected token " + token + " in [" + aggregationName + "]."); @@ -141,8 +143,10 @@ public static ParentAggregationBuilder parse(String aggregationName, XContentPar } if (childType == null) { - throw new ParsingException(parser.getTokenLocation(), - "Missing [child_type] field for parent aggregation [" + aggregationName + "]"); + throw new ParsingException( + parser.getTokenLocation(), + "Missing [child_type] field for parent aggregation [" + aggregationName + "]" + ); } return new ParentAggregationBuilder(aggregationName, childType); diff --git a/modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/ParentAggregatorFactory.java b/modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/ParentAggregatorFactory.java index 917ab2c0bd98d..5ff0088326229 100644 --- a/modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/ParentAggregatorFactory.java +++ b/modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/ParentAggregatorFactory.java @@ -32,14 +32,16 @@ public class ParentAggregatorFactory extends ValuesSourceAggregatorFactory { private final Query parentFilter; private final Query childFilter; - public ParentAggregatorFactory(String name, - ValuesSourceConfig config, - Query childFilter, - Query parentFilter, - AggregationContext context, - AggregatorFactory parent, - AggregatorFactories.Builder subFactoriesBuilder, - Map metadata) throws IOException { + public ParentAggregatorFactory( + String name, + ValuesSourceConfig config, + Query childFilter, + Query parentFilter, + AggregationContext context, + AggregatorFactory parent, + AggregatorFactories.Builder subFactoriesBuilder, + Map metadata + ) throws IOException { super(name, config, context, parent, subFactoriesBuilder, metadata); this.childFilter = childFilter; @@ -61,13 +63,24 @@ protected Aggregator doCreateInternal(Aggregator children, CardinalityUpperBound throws IOException { ValuesSource rawValuesSource = config.getValuesSource(); if (rawValuesSource instanceof WithOrdinals == false) { - throw new AggregationExecutionException("ValuesSource type " + rawValuesSource.toString() + - "is not supported for aggregation " + this.name()); + throw new AggregationExecutionException( + "ValuesSource type " + rawValuesSource.toString() + "is not supported for aggregation " + this.name() + ); } WithOrdinals valuesSource = (WithOrdinals) rawValuesSource; long maxOrd = valuesSource.globalMaxOrd(context.searcher()); - return new ChildrenToParentAggregator(name, factories, context, children, childFilter, - parentFilter, valuesSource, maxOrd, cardinality, metadata); + return new ChildrenToParentAggregator( + name, + factories, + context, + children, + childFilter, + parentFilter, + valuesSource, + maxOrd, + cardinality, + metadata + ); } @Override diff --git a/modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/ParentJoinAggregator.java b/modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/ParentJoinAggregator.java index 625b330017a41..5a69f87f3a95d 100644 --- a/modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/ParentJoinAggregator.java +++ b/modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/ParentJoinAggregator.java @@ -17,11 +17,11 @@ import org.apache.lucene.search.Scorer; import org.apache.lucene.search.Weight; import org.apache.lucene.util.Bits; -import org.elasticsearch.core.Releasable; -import org.elasticsearch.core.Releasables; import org.elasticsearch.common.lucene.Lucene; import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.common.util.BitArray; +import org.elasticsearch.core.Releasable; +import org.elasticsearch.core.Releasables; import org.elasticsearch.search.aggregations.Aggregator; import org.elasticsearch.search.aggregations.AggregatorFactories; import org.elasticsearch.search.aggregations.CardinalityUpperBound; @@ -50,16 +50,18 @@ public abstract class ParentJoinAggregator extends BucketsAggregator implements */ private final CollectionStrategy collectionStrategy; - public ParentJoinAggregator(String name, - AggregatorFactories factories, - AggregationContext context, - Aggregator parent, - Query inFilter, - Query outFilter, - ValuesSource.Bytes.WithOrdinals valuesSource, - long maxOrd, - CardinalityUpperBound cardinality, - Map metadata) throws IOException { + public ParentJoinAggregator( + String name, + AggregatorFactories factories, + AggregationContext context, + Aggregator parent, + Query inFilter, + Query outFilter, + ValuesSource.Bytes.WithOrdinals valuesSource, + long maxOrd, + CardinalityUpperBound cardinality, + Map metadata + ) throws IOException { /* * We have to use MANY to work around * https://github.com/elastic/elasticsearch/issues/59097 @@ -67,8 +69,9 @@ public ParentJoinAggregator(String name, super(name, factories, context, parent, CardinalityUpperBound.MANY, metadata); if (maxOrd > Integer.MAX_VALUE) { - throw new IllegalStateException("the number of parent [" + maxOrd + "] + is greater than the allowed limit " + - "for this aggregation: " + Integer.MAX_VALUE); + throw new IllegalStateException( + "the number of parent [" + maxOrd + "] + is greater than the allowed limit " + "for this aggregation: " + Integer.MAX_VALUE + ); } // these two filters are cached in the parser @@ -82,8 +85,7 @@ public ParentJoinAggregator(String name, } @Override - public final LeafBucketCollector getLeafCollector(LeafReaderContext ctx, - final LeafBucketCollector sub) throws IOException { + public final LeafBucketCollector getLeafCollector(LeafReaderContext ctx, final LeafBucketCollector sub) throws IOException { if (valuesSource == null) { return LeafBucketCollector.NO_OP_COLLECTOR; } @@ -149,7 +151,7 @@ public int docID() { * structure that maps a primitive long to a list of primitive * longs. */ - for (long owningBucketOrd: ordsToCollect) { + for (long owningBucketOrd : ordsToCollect) { if (collectionStrategy.exists(owningBucketOrd, globalOrdinal)) { collectBucket(sub, docId, owningBucketOrd); } @@ -172,6 +174,7 @@ protected void doClose() { */ protected interface CollectionStrategy extends Releasable { void add(long owningBucketOrd, int globalOrdinal); + boolean exists(long owningBucketOrd, int globalOrdinal); } diff --git a/modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/ParentToChildrenAggregator.java b/modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/ParentToChildrenAggregator.java index 92b3679fb47be..7496b19a5f053 100644 --- a/modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/ParentToChildrenAggregator.java +++ b/modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/ParentToChildrenAggregator.java @@ -8,13 +8,13 @@ package org.elasticsearch.join.aggregations; import org.apache.lucene.search.Query; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.search.aggregations.Aggregator; import org.elasticsearch.search.aggregations.AggregatorFactories; import org.elasticsearch.search.aggregations.CardinalityUpperBound; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.support.AggregationContext; import org.elasticsearch.search.aggregations.support.ValuesSource; +import org.elasticsearch.xcontent.ParseField; import java.io.IOException; import java.util.Map; @@ -23,17 +23,32 @@ public class ParentToChildrenAggregator extends ParentJoinAggregator { static final ParseField TYPE_FIELD = new ParseField("type"); - public ParentToChildrenAggregator(String name, AggregatorFactories factories, - AggregationContext context, Aggregator parent, Query childFilter, - Query parentFilter, ValuesSource.Bytes.WithOrdinals valuesSource, - long maxOrd, CardinalityUpperBound cardinality, Map metadata) throws IOException { + public ParentToChildrenAggregator( + String name, + AggregatorFactories factories, + AggregationContext context, + Aggregator parent, + Query childFilter, + Query parentFilter, + ValuesSource.Bytes.WithOrdinals valuesSource, + long maxOrd, + CardinalityUpperBound cardinality, + Map metadata + ) throws IOException { super(name, factories, context, parent, parentFilter, childFilter, valuesSource, maxOrd, cardinality, metadata); } @Override public InternalAggregation[] buildAggregations(long[] owningBucketOrds) throws IOException { - return buildAggregationsForSingleBucket(owningBucketOrds, (owningBucketOrd, subAggregationResults) -> - new InternalChildren(name, bucketDocCount(owningBucketOrd), subAggregationResults, metadata())); + return buildAggregationsForSingleBucket( + owningBucketOrds, + (owningBucketOrd, subAggregationResults) -> new InternalChildren( + name, + bucketDocCount(owningBucketOrd), + subAggregationResults, + metadata() + ) + ); } @Override diff --git a/modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/ParsedChildren.java b/modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/ParsedChildren.java index 9caca8f2fc85e..0ad39b1df59a9 100644 --- a/modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/ParsedChildren.java +++ b/modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/ParsedChildren.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.join.aggregations; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.aggregations.bucket.ParsedSingleBucketAggregation; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/ParsedParent.java b/modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/ParsedParent.java index 00725d4d48aa7..b61fa9c08453c 100644 --- a/modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/ParsedParent.java +++ b/modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/ParsedParent.java @@ -7,8 +7,8 @@ */ package org.elasticsearch.join.aggregations; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.aggregations.bucket.ParsedSingleBucketAggregation; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/modules/parent-join/src/main/java/org/elasticsearch/join/mapper/Joiner.java b/modules/parent-join/src/main/java/org/elasticsearch/join/mapper/Joiner.java index 20019d29742d5..59e12ce329f06 100644 --- a/modules/parent-join/src/main/java/org/elasticsearch/join/mapper/Joiner.java +++ b/modules/parent-join/src/main/java/org/elasticsearch/join/mapper/Joiner.java @@ -54,6 +54,7 @@ static Joiner getJoiner(Stream fieldTypes) { Optional joinType = fieldTypes.filter(ft -> ft instanceof JoinFieldType).map(ft -> (JoinFieldType) ft).findFirst(); return joinType.map(JoinFieldType::getJoiner).orElse(null); } + private final Map> parentsToChildren = new HashMap<>(); private final Map childrenToParents = new HashMap<>(); diff --git a/modules/parent-join/src/main/java/org/elasticsearch/join/mapper/ParentJoinFieldMapper.java b/modules/parent-join/src/main/java/org/elasticsearch/join/mapper/ParentJoinFieldMapper.java index 43b9c444d5797..90168739d86a0 100644 --- a/modules/parent-join/src/main/java/org/elasticsearch/join/mapper/ParentJoinFieldMapper.java +++ b/modules/parent-join/src/main/java/org/elasticsearch/join/mapper/ParentJoinFieldMapper.java @@ -14,8 +14,6 @@ import org.apache.lucene.index.IndexOptions; import org.apache.lucene.util.BytesRef; import org.elasticsearch.common.lucene.Lucene; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.fielddata.IndexFieldData; import org.elasticsearch.index.fielddata.plain.SortedSetOrdinalsIndexFieldData; @@ -32,6 +30,8 @@ import org.elasticsearch.index.query.SearchExecutionContext; import org.elasticsearch.search.aggregations.support.CoreValuesSourceType; import org.elasticsearch.search.lookup.SearchLookup; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.ArrayList; @@ -67,16 +67,16 @@ public static class Defaults { private static void checkIndexCompatibility(IndexSettings settings, String name) { if (settings.getIndexMetadata().isRoutingPartitionedIndex()) { - throw new IllegalStateException("cannot create join field [" + name + "] " + - "for the partitioned index " + "[" + settings.getIndex().getName() + "]"); + throw new IllegalStateException( + "cannot create join field [" + name + "] " + "for the partitioned index " + "[" + settings.getIndex().getName() + "]" + ); } } private static void checkObjectOrNested(MapperBuilderContext context, String name) { String fullName = context.buildFullName(name); if (fullName.equals(name) == false) { - throw new IllegalArgumentException("join field [" + fullName + "] " + - "cannot be added inside an object or in a multi-field"); + throw new IllegalArgumentException("join field [" + fullName + "] " + "cannot be added inside an object or in a multi-field"); } } @@ -86,11 +86,19 @@ private static ParentJoinFieldMapper toType(FieldMapper in) { public static class Builder extends FieldMapper.Builder { - final Parameter eagerGlobalOrdinals = Parameter.boolParam("eager_global_ordinals", true, - m -> toType(m).eagerGlobalOrdinals, true); - final Parameter> relations = new Parameter>("relations", true, - Collections::emptyList, (n, c, o) -> Relations.parse(o), m -> toType(m).relations) - .setMergeValidator(ParentJoinFieldMapper::checkRelationsConflicts); + final Parameter eagerGlobalOrdinals = Parameter.boolParam( + "eager_global_ordinals", + true, + m -> toType(m).eagerGlobalOrdinals, + true + ); + final Parameter> relations = new Parameter>( + "relations", + true, + Collections::emptyList, + (n, c, o) -> Relations.parse(o), + m -> toType(m).relations + ).setMergeValidator(ParentJoinFieldMapper::checkRelationsConflicts); final Parameter> meta = Parameter.metaParam(); @@ -112,12 +120,18 @@ protected List> getParameters() { public ParentJoinFieldMapper build(MapperBuilderContext context) { checkObjectOrNested(context, name); final Map parentIdFields = new HashMap<>(); - relations.get().stream() + relations.get() + .stream() .map(relation -> new ParentIdFieldMapper(name + "#" + relation.parent, eagerGlobalOrdinals.get())) .forEach(mapper -> parentIdFields.put(mapper.name(), mapper)); Joiner joiner = new Joiner(name(), relations.get()); - return new ParentJoinFieldMapper(name, new JoinFieldType(context.buildFullName(name), joiner, meta.get()), - Collections.unmodifiableMap(parentIdFields), eagerGlobalOrdinals.get(), relations.get()); + return new ParentJoinFieldMapper( + name, + new JoinFieldType(context.buildFullName(name), joiner, meta.get()), + Collections.unmodifiableMap(parentIdFields), + eagerGlobalOrdinals.get(), + relations.get() + ); } } @@ -177,10 +191,13 @@ private static boolean checkRelationsConflicts(List previous, List relations; - protected ParentJoinFieldMapper(String simpleName, - MappedFieldType mappedFieldType, - Map parentIdFields, - boolean eagerGlobalOrdinals, List relations) { + protected ParentJoinFieldMapper( + String simpleName, + MappedFieldType mappedFieldType, + Map parentIdFields, + boolean eagerGlobalOrdinals, + List relations + ) { super(simpleName, mappedFieldType, Lucene.KEYWORD_ANALYZER, MultiFields.empty(), CopyTo.empty()); this.parentIdFields = parentIdFields; this.eagerGlobalOrdinals = eagerGlobalOrdinals; @@ -239,7 +256,7 @@ public void parse(DocumentParserContext context) throws IOException { name = context.parser().text(); parent = null; } else { - throw new IllegalStateException("[" + name() + "] expected START_OBJECT or VALUE_STRING but was: " + token); + throw new IllegalStateException("[" + name() + "] expected START_OBJECT or VALUE_STRING but was: " + token); } if (name == null) { @@ -295,8 +312,12 @@ public FieldMapper.Builder getMergeBuilder() { @Override protected void doValidate(MappingLookup mappingLookup) { - List joinFields = mappingLookup.getMatchingFieldNames("*").stream().map(mappingLookup::getFieldType) - .filter(ft -> ft instanceof JoinFieldType).map(MappedFieldType::name).collect(Collectors.toList()); + List joinFields = mappingLookup.getMatchingFieldNames("*") + .stream() + .map(mappingLookup::getFieldType) + .filter(ft -> ft instanceof JoinFieldType) + .map(MappedFieldType::name) + .collect(Collectors.toList()); if (joinFields.size() > 1) { throw new IllegalArgumentException("Only one [parent-join] field can be defined per index, got " + joinFields); } diff --git a/modules/parent-join/src/main/java/org/elasticsearch/join/mapper/Relations.java b/modules/parent-join/src/main/java/org/elasticsearch/join/mapper/Relations.java index bf6ff81ee48e0..f77393603bd41 100644 --- a/modules/parent-join/src/main/java/org/elasticsearch/join/mapper/Relations.java +++ b/modules/parent-join/src/main/java/org/elasticsearch/join/mapper/Relations.java @@ -37,8 +37,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Relations relation = (Relations) o; - return Objects.equals(parent, relation.parent) && - Objects.equals(children, relation.children); + return Objects.equals(parent, relation.parent) && Objects.equals(children, relation.children); } @Override diff --git a/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasChildQueryBuilder.java b/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasChildQueryBuilder.java index 16513eb3b8767..4b8091924e121 100644 --- a/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasChildQueryBuilder.java +++ b/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasChildQueryBuilder.java @@ -18,13 +18,10 @@ import org.apache.lucene.search.join.ScoreMode; import org.apache.lucene.search.similarities.Similarity; import org.elasticsearch.ElasticsearchException; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.lucene.search.Queries; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.index.fielddata.IndexOrdinalsFieldData; import org.elasticsearch.index.fielddata.plain.SortedSetOrdinalsIndexFieldData; import org.elasticsearch.index.mapper.MappedFieldType; @@ -34,9 +31,12 @@ import org.elasticsearch.index.query.NestedQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryRewriteContext; -import org.elasticsearch.index.query.SearchExecutionContext; import org.elasticsearch.index.query.QueryShardException; +import org.elasticsearch.index.query.SearchExecutionContext; import org.elasticsearch.join.mapper.Joiner; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.HashMap; @@ -85,8 +85,14 @@ public HasChildQueryBuilder(String type, QueryBuilder query, ScoreMode scoreMode this(type, query, DEFAULT_MIN_CHILDREN, DEFAULT_MAX_CHILDREN, scoreMode, null); } - private HasChildQueryBuilder(String type, QueryBuilder query, int minChildren, int maxChildren, ScoreMode scoreMode, - InnerHitBuilder innerHitBuilder) { + private HasChildQueryBuilder( + String type, + QueryBuilder query, + int minChildren, + int maxChildren, + ScoreMode scoreMode, + InnerHitBuilder innerHitBuilder + ) { this.type = requireValue(type, "[" + NAME + "] requires 'type' field"); this.query = requireValue(query, "[" + NAME + "] requires 'query' field"); this.scoreMode = requireValue(scoreMode, "[" + NAME + "] requires 'score_mode' field"); @@ -185,7 +191,9 @@ public int minChildren() { * Returns the maximum number of children that are required to match for the parent to be considered a match. * The default is {@value #DEFAULT_MAX_CHILDREN} */ - public int maxChildren() { return maxChildren; } + public int maxChildren() { + return maxChildren; + } /** * Sets whether the query builder should ignore unmapped types (and run a @@ -194,7 +202,7 @@ public int minChildren() { */ public HasChildQueryBuilder ignoreUnmapped(boolean ignoreUnmapped) { this.ignoreUnmapped = ignoreUnmapped; - if (innerHitBuilder!= null ){ + if (innerHitBuilder != null) { innerHitBuilder.setIgnoreUnmapped(ignoreUnmapped); } return this; @@ -288,8 +296,9 @@ public String getWriteableName() { @Override protected Query doToQuery(SearchExecutionContext context) throws IOException { if (context.allowExpensiveQueries() == false) { - throw new ElasticsearchException("[joining] queries cannot be executed when '" + - ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false."); + throw new ElasticsearchException( + "[joining] queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false." + ); } Joiner joiner = Joiner.getJoiner(context); @@ -305,8 +314,10 @@ protected Query doToQuery(SearchExecutionContext context) throws IOException { if (ignoreUnmapped) { return new MatchNoDocsQuery(); } else { - throw new QueryShardException(context, "[" + NAME + "] join field [" + joiner.getJoinField() + - "] doesn't hold [" + type + "] as a child"); + throw new QueryShardException( + context, + "[" + NAME + "] join field [" + joiner.getJoinField() + "] doesn't hold [" + type + "] as a child" + ); } } @@ -323,8 +334,16 @@ protected Query doToQuery(SearchExecutionContext context) throws IOException { Query filteredQuery = Queries.filtered(query.toQuery(context), childFilter); MappedFieldType ft = context.getFieldType(parentJoinField); final SortedSetOrdinalsIndexFieldData fieldData = context.getForField(ft); - return new LateParsingQuery(parentFilter, filteredQuery, minChildren, maxChildren, - parentJoinField, scoreMode, fieldData, context.getSearchSimilarity()); + return new LateParsingQuery( + parentFilter, + filteredQuery, + minChildren, + maxChildren, + parentJoinField, + scoreMode, + fieldData, + context.getSearchSimilarity() + ); } /** @@ -348,9 +367,16 @@ public static final class LateParsingQuery extends Query { private final SortedSetOrdinalsIndexFieldData fieldDataJoin; private final Similarity similarity; - LateParsingQuery(Query toQuery, Query innerQuery, int minChildren, int maxChildren, - String joinField, ScoreMode scoreMode, - SortedSetOrdinalsIndexFieldData fieldData, Similarity similarity) { + LateParsingQuery( + Query toQuery, + Query innerQuery, + int minChildren, + int maxChildren, + String joinField, + ScoreMode scoreMode, + SortedSetOrdinalsIndexFieldData fieldData, + Similarity similarity + ) { this.toQuery = toQuery; this.innerQuery = innerQuery; this.minChildren = minChildren; @@ -373,8 +399,16 @@ public Query rewrite(IndexReader reader) throws IOException { indexSearcher.setSimilarity(similarity); IndexOrdinalsFieldData indexParentChildFieldData = fieldDataJoin.loadGlobal((DirectoryReader) reader); OrdinalMap ordinalMap = indexParentChildFieldData.getOrdinalMap(); - return JoinUtil.createJoinQuery(joinField, innerQuery, toQuery, indexSearcher, scoreMode, - ordinalMap, minChildren, maxChildren); + return JoinUtil.createJoinQuery( + joinField, + innerQuery, + toQuery, + indexSearcher, + scoreMode, + ordinalMap, + minChildren, + maxChildren + ); } else { if (reader.leaves().isEmpty() && reader.numDocs() == 0) { // asserting reader passes down a MultiReader during rewrite which makes this @@ -383,8 +417,9 @@ public Query rewrite(IndexReader reader) throws IOException { // and rewrite to match nothing return new MatchNoDocsQuery(); } - throw new IllegalStateException("can't load global ordinals for reader of type: " + - reader.getClass() + " must be a DirectoryReader"); + throw new IllegalStateException( + "can't load global ordinals for reader of type: " + reader.getClass() + " must be a DirectoryReader" + ); } } @@ -441,12 +476,12 @@ public Similarity getSimilarity() { @Override protected boolean doEquals(HasChildQueryBuilder that) { return Objects.equals(query, that.query) - && Objects.equals(type, that.type) - && Objects.equals(scoreMode, that.scoreMode) - && Objects.equals(minChildren, that.minChildren) - && Objects.equals(maxChildren, that.maxChildren) - && Objects.equals(innerHitBuilder, that.innerHitBuilder) - && Objects.equals(ignoreUnmapped, that.ignoreUnmapped); + && Objects.equals(type, that.type) + && Objects.equals(scoreMode, that.scoreMode) + && Objects.equals(minChildren, that.minChildren) + && Objects.equals(maxChildren, that.maxChildren) + && Objects.equals(innerHitBuilder, that.innerHitBuilder) + && Objects.equals(ignoreUnmapped, that.ignoreUnmapped); } @Override @@ -458,8 +493,14 @@ protected int doHashCode() { protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws IOException { QueryBuilder rewrittenQuery = query.rewrite(queryRewriteContext); if (rewrittenQuery != query) { - HasChildQueryBuilder hasChildQueryBuilder = - new HasChildQueryBuilder(type, rewrittenQuery, minChildren, maxChildren, scoreMode, innerHitBuilder); + HasChildQueryBuilder hasChildQueryBuilder = new HasChildQueryBuilder( + type, + rewrittenQuery, + minChildren, + maxChildren, + scoreMode, + innerHitBuilder + ); hasChildQueryBuilder.ignoreUnmapped(ignoreUnmapped); return hasChildQueryBuilder; } @@ -476,8 +517,13 @@ protected void extractInnerHitBuilders(Map inner Map children = new HashMap<>(); InnerHitContextBuilder.extractInnerHits(query, children); - InnerHitContextBuilder innerHitContextBuilder = - new ParentChildInnerHitContextBuilder(type, true, query, innerHitBuilder, children); + InnerHitContextBuilder innerHitContextBuilder = new ParentChildInnerHitContextBuilder( + type, + true, + query, + innerHitBuilder, + children + ); innerHits.put(name, innerHitContextBuilder); } } diff --git a/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasParentQueryBuilder.java b/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasParentQueryBuilder.java index 6750ef7444d64..65da28f96f1fa 100644 --- a/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasParentQueryBuilder.java +++ b/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasParentQueryBuilder.java @@ -11,13 +11,10 @@ import org.apache.lucene.search.Query; import org.apache.lucene.search.join.ScoreMode; import org.elasticsearch.ElasticsearchException; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.lucene.search.Queries; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.index.fielddata.plain.SortedSetOrdinalsIndexFieldData; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.query.AbstractQueryBuilder; @@ -25,9 +22,12 @@ import org.elasticsearch.index.query.InnerHitContextBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryRewriteContext; -import org.elasticsearch.index.query.SearchExecutionContext; import org.elasticsearch.index.query.QueryShardException; +import org.elasticsearch.index.query.SearchExecutionContext; import org.elasticsearch.join.mapper.Joiner; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.HashMap; @@ -64,7 +64,7 @@ public HasParentQueryBuilder(String type, QueryBuilder query, boolean score) { } private HasParentQueryBuilder(String type, QueryBuilder query, boolean score, InnerHitBuilder innerHitBuilder) { - this.parentType = requireValue(type, "[" + NAME + "] requires '" + PARENT_TYPE_FIELD.getPreferredName() + "' field"); + this.parentType = requireValue(type, "[" + NAME + "] requires '" + PARENT_TYPE_FIELD.getPreferredName() + "' field"); this.query = requireValue(query, "[" + NAME + "] requires '" + QUERY_FIELD.getPreferredName() + "' field"); this.score = score; this.innerHitBuilder = innerHitBuilder; @@ -150,8 +150,9 @@ public boolean ignoreUnmapped() { @Override protected Query doToQuery(SearchExecutionContext context) throws IOException { if (context.allowExpensiveQueries() == false) { - throw new ElasticsearchException("[joining] queries cannot be executed when '" + - ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false."); + throw new ElasticsearchException( + "[joining] queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false." + ); } Joiner joiner = Joiner.getJoiner(context); @@ -166,8 +167,10 @@ protected Query doToQuery(SearchExecutionContext context) throws IOException { if (ignoreUnmapped) { return new MatchNoDocsQuery(); } else { - throw new QueryShardException(context, "[" + NAME + "] join field [" + joiner.getJoinField() + - "] doesn't hold [" + parentType + "] as a parent"); + throw new QueryShardException( + context, + "[" + NAME + "] join field [" + joiner.getJoinField() + "] doesn't hold [" + parentType + "] as a parent" + ); } } @@ -176,9 +179,16 @@ protected Query doToQuery(SearchExecutionContext context) throws IOException { Query childFilter = joiner.childrenFilter(parentType); MappedFieldType fieldType = context.getFieldType(joiner.childJoinField(parentType)); final SortedSetOrdinalsIndexFieldData fieldData = context.getForField(fieldType); - return new HasChildQueryBuilder.LateParsingQuery(childFilter, innerQuery, - HasChildQueryBuilder.DEFAULT_MIN_CHILDREN, HasChildQueryBuilder.DEFAULT_MAX_CHILDREN, - fieldType.name(), score ? ScoreMode.Max : ScoreMode.None, fieldData, context.getSearchSimilarity()); + return new HasChildQueryBuilder.LateParsingQuery( + childFilter, + innerQuery, + HasChildQueryBuilder.DEFAULT_MIN_CHILDREN, + HasChildQueryBuilder.DEFAULT_MAX_CHILDREN, + fieldType.name(), + score ? ScoreMode.Max : ScoreMode.None, + fieldData, + context.getSearchSimilarity() + ); } @Override @@ -216,8 +226,7 @@ public static HasParentQueryBuilder fromXContent(XContentParser parser) throws I } else if (INNER_HITS_FIELD.match(currentFieldName, parser.getDeprecationHandler())) { innerHits = InnerHitBuilder.fromXContent(parser); } else { - throw new ParsingException(parser.getTokenLocation(), - "[has_parent] query does not support [" + currentFieldName + "]"); + throw new ParsingException(parser.getTokenLocation(), "[has_parent] query does not support [" + currentFieldName + "]"); } } else if (token.isValue()) { if (PARENT_TYPE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) { @@ -231,15 +240,13 @@ public static HasParentQueryBuilder fromXContent(XContentParser parser) throws I } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) { queryName = parser.text(); } else { - throw new ParsingException(parser.getTokenLocation(), - "[has_parent] query does not support [" + currentFieldName + "]"); + throw new ParsingException(parser.getTokenLocation(), "[has_parent] query does not support [" + currentFieldName + "]"); } } } - HasParentQueryBuilder queryBuilder = new HasParentQueryBuilder(parentType, iqb, score) - .ignoreUnmapped(ignoreUnmapped) - .queryName(queryName) - .boost(boost); + HasParentQueryBuilder queryBuilder = new HasParentQueryBuilder(parentType, iqb, score).ignoreUnmapped(ignoreUnmapped) + .queryName(queryName) + .boost(boost); if (innerHits != null) { queryBuilder.innerHit(innerHits); } @@ -254,10 +261,10 @@ public String getWriteableName() { @Override protected boolean doEquals(HasParentQueryBuilder that) { return Objects.equals(query, that.query) - && Objects.equals(parentType, that.parentType) - && Objects.equals(score, that.score) - && Objects.equals(innerHitBuilder, that.innerHitBuilder) - && Objects.equals(ignoreUnmapped, that.ignoreUnmapped); + && Objects.equals(parentType, that.parentType) + && Objects.equals(score, that.score) + && Objects.equals(innerHitBuilder, that.innerHitBuilder) + && Objects.equals(ignoreUnmapped, that.ignoreUnmapped); } @Override @@ -286,8 +293,13 @@ protected void extractInnerHitBuilders(Map inner Map children = new HashMap<>(); InnerHitContextBuilder.extractInnerHits(query, children); - InnerHitContextBuilder innerHitContextBuilder = - new ParentChildInnerHitContextBuilder(parentType, false, query, innerHitBuilder, children); + InnerHitContextBuilder innerHitContextBuilder = new ParentChildInnerHitContextBuilder( + parentType, + false, + query, + innerHitBuilder, + children + ); innerHits.put(name, innerHitContextBuilder); } } diff --git a/modules/parent-join/src/main/java/org/elasticsearch/join/query/ParentChildInnerHitContextBuilder.java b/modules/parent-join/src/main/java/org/elasticsearch/join/query/ParentChildInnerHitContextBuilder.java index 6f2e52e3d5638..6a644cdcdc5ca 100644 --- a/modules/parent-join/src/main/java/org/elasticsearch/join/query/ParentChildInnerHitContextBuilder.java +++ b/modules/parent-join/src/main/java/org/elasticsearch/join/query/ParentChildInnerHitContextBuilder.java @@ -49,8 +49,13 @@ class ParentChildInnerHitContextBuilder extends InnerHitContextBuilder { private final String typeName; private final boolean fetchChildInnerHits; - ParentChildInnerHitContextBuilder(String typeName, boolean fetchChildInnerHits, QueryBuilder query, - InnerHitBuilder innerHitBuilder, Map children) { + ParentChildInnerHitContextBuilder( + String typeName, + boolean fetchChildInnerHits, + QueryBuilder query, + InnerHitBuilder innerHitBuilder, + Map children + ) { super(query, innerHitBuilder, children); this.typeName = typeName; this.fetchChildInnerHits = fetchChildInnerHits; @@ -62,8 +67,13 @@ protected void doBuild(SearchContext context, InnerHitsContext innerHitsContext) Joiner joiner = Joiner.getJoiner(searchExecutionContext); if (joiner != null) { String name = innerHitBuilder.getName() != null ? innerHitBuilder.getName() : typeName; - JoinFieldInnerHitSubContext joinFieldInnerHits = new JoinFieldInnerHitSubContext(name, context, typeName, - fetchChildInnerHits, joiner); + JoinFieldInnerHitSubContext joinFieldInnerHits = new JoinFieldInnerHitSubContext( + name, + context, + typeName, + fetchChildInnerHits, + joiner + ); setupInnerHitsContext(searchExecutionContext, joinFieldInnerHits); innerHitsContext.addInnerHitDefinition(joinFieldInnerHits); } else { @@ -78,8 +88,7 @@ static final class JoinFieldInnerHitSubContext extends InnerHitsContext.InnerHit private final boolean fetchChildInnerHits; private final Joiner joiner; - JoinFieldInnerHitSubContext(String name, SearchContext context, String typeName, boolean fetchChildInnerHits, - Joiner joiner) { + JoinFieldInnerHitSubContext(String name, SearchContext context, String typeName, boolean fetchChildInnerHits, Joiner joiner) { super(name, context); this.typeName = typeName; this.fetchChildInnerHits = fetchChildInnerHits; @@ -119,10 +128,9 @@ public TopDocsAndMaxScore topDocs(SearchHit hit) throws IOException { intersect(weight, innerHitQueryWeight, totalHitCountCollector, ctx); } return new TopDocsAndMaxScore( - new TopDocs( - new TotalHits(totalHitCountCollector.getTotalHits(), TotalHits.Relation.EQUAL_TO), - Lucene.EMPTY_SCORE_DOCS - ), Float.NaN); + new TopDocs(new TotalHits(totalHitCountCollector.getTotalHits(), TotalHits.Relation.EQUAL_TO), Lucene.EMPTY_SCORE_DOCS), + Float.NaN + ); } else { int topN = Math.min(from() + size(), this.context.searcher().getIndexReader().maxDoc()); TopDocsCollector topDocsCollector; diff --git a/modules/parent-join/src/main/java/org/elasticsearch/join/query/ParentIdQueryBuilder.java b/modules/parent-join/src/main/java/org/elasticsearch/join/query/ParentIdQueryBuilder.java index 1ba4d64f9124c..c1a0f60e349b5 100644 --- a/modules/parent-join/src/main/java/org/elasticsearch/join/query/ParentIdQueryBuilder.java +++ b/modules/parent-join/src/main/java/org/elasticsearch/join/query/ParentIdQueryBuilder.java @@ -15,16 +15,16 @@ import org.apache.lucene.search.Query; import org.apache.lucene.search.TermQuery; import org.elasticsearch.ElasticsearchException; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.index.query.AbstractQueryBuilder; -import org.elasticsearch.index.query.SearchExecutionContext; import org.elasticsearch.index.query.QueryShardException; +import org.elasticsearch.index.query.SearchExecutionContext; import org.elasticsearch.join.mapper.Joiner; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.Objects; @@ -143,12 +143,12 @@ public static ParentIdQueryBuilder fromXContent(XContentParser parser) throws IO return queryBuilder; } - @Override protected Query doToQuery(SearchExecutionContext context) throws IOException { if (context.allowExpensiveQueries() == false) { - throw new ElasticsearchException("[joining] queries cannot be executed when '" + - ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false."); + throw new ElasticsearchException( + "[joining] queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false." + ); } Joiner joiner = Joiner.getJoiner(context); @@ -157,7 +157,7 @@ protected Query doToQuery(SearchExecutionContext context) throws IOException { return new MatchNoDocsQuery(); } else { final String indexName = context.getIndexSettings().getIndex().getName(); - throw new QueryShardException(context, "[" + NAME + "] no join field found for index [" + indexName + "]"); + throw new QueryShardException(context, "[" + NAME + "] no join field found for index [" + indexName + "]"); } } if (joiner.childTypeExists(type) == false) { @@ -167,8 +167,7 @@ protected Query doToQuery(SearchExecutionContext context) throws IOException { throw new QueryShardException(context, "[" + NAME + "] no relation found for child [" + type + "]"); } } - return new BooleanQuery.Builder() - .add(new TermQuery(new Term(joiner.parentJoinField(type), id)), BooleanClause.Occur.MUST) + return new BooleanQuery.Builder().add(new TermQuery(new Term(joiner.parentJoinField(type), id)), BooleanClause.Occur.MUST) // Need to take child type into account, otherwise a child doc of different type with the same id could match .add(new TermQuery(new Term(joiner.getJoinField(), type)), BooleanClause.Occur.FILTER) .build(); @@ -176,9 +175,7 @@ protected Query doToQuery(SearchExecutionContext context) throws IOException { @Override protected boolean doEquals(ParentIdQueryBuilder that) { - return Objects.equals(type, that.type) - && Objects.equals(id, that.id) - && Objects.equals(ignoreUnmapped, that.ignoreUnmapped); + return Objects.equals(type, that.type) && Objects.equals(id, that.id) && Objects.equals(ignoreUnmapped, that.ignoreUnmapped); } @Override diff --git a/modules/parent-join/src/main/java/org/elasticsearch/join/spi/ParentJoinNamedXContentProvider.java b/modules/parent-join/src/main/java/org/elasticsearch/join/spi/ParentJoinNamedXContentProvider.java index 1a9633f4c69c0..7d6392f9bc172 100644 --- a/modules/parent-join/src/main/java/org/elasticsearch/join/spi/ParentJoinNamedXContentProvider.java +++ b/modules/parent-join/src/main/java/org/elasticsearch/join/spi/ParentJoinNamedXContentProvider.java @@ -8,15 +8,15 @@ package org.elasticsearch.join.spi; -import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.xcontent.ContextParser; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.join.aggregations.ChildrenAggregationBuilder; import org.elasticsearch.join.aggregations.ParentAggregationBuilder; import org.elasticsearch.join.aggregations.ParsedChildren; import org.elasticsearch.join.aggregations.ParsedParent; import org.elasticsearch.plugins.spi.NamedXContentProvider; import org.elasticsearch.search.aggregations.Aggregation; +import org.elasticsearch.xcontent.ContextParser; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import java.util.Arrays; import java.util.List; diff --git a/modules/parent-join/src/test/java/org/elasticsearch/join/aggregations/ChildrenToParentAggregatorTests.java b/modules/parent-join/src/test/java/org/elasticsearch/join/aggregations/ChildrenToParentAggregatorTests.java index fd93aa2e06aaf..464eee585a2fe 100644 --- a/modules/parent-join/src/test/java/org/elasticsearch/join/aggregations/ChildrenToParentAggregatorTests.java +++ b/modules/parent-join/src/test/java/org/elasticsearch/join/aggregations/ChildrenToParentAggregatorTests.java @@ -85,8 +85,10 @@ public void testParentChild() throws IOException { final Map> expectedParentChildRelations = setupIndex(indexWriter); indexWriter.close(); - IndexReader indexReader = ElasticsearchDirectoryReader.wrap(DirectoryReader.open(directory), - new ShardId(new Index("foo", "_na_"), 1)); + IndexReader indexReader = ElasticsearchDirectoryReader.wrap( + DirectoryReader.open(directory), + new ShardId(new Index("foo", "_na_"), 1) + ); // TODO set "maybeWrap" to true for IndexSearcher once #23338 is resolved IndexSearcher indexSearcher = newSearcher(indexReader, false, true); @@ -109,13 +111,17 @@ public void testParentChild() throws IOException { // verify for each children for (String parent : expectedParentChildRelations.keySet()) { - testCase(new TermInSetQuery(IdFieldMapper.NAME, Uid.encodeId("child0_" + parent)), - indexSearcher, aggregation -> { - assertEquals("Expected one result for min-aggregation for parent: " + parent + - ", but had aggregation-results: " + aggregation, - 1, aggregation.getDocCount()); - assertEquals(expectedParentChildRelations.get(parent).v2(), - ((InternalMin) aggregation.getAggregations().get("in_parent")).getValue(), Double.MIN_VALUE); + testCase(new TermInSetQuery(IdFieldMapper.NAME, Uid.encodeId("child0_" + parent)), indexSearcher, aggregation -> { + assertEquals( + "Expected one result for min-aggregation for parent: " + parent + ", but had aggregation-results: " + aggregation, + 1, + aggregation.getDocCount() + ); + assertEquals( + expectedParentChildRelations.get(parent).v2(), + ((InternalMin) aggregation.getAggregations().get("in_parent")).getValue(), + Double.MIN_VALUE + ); }); } @@ -123,7 +129,6 @@ public void testParentChild() throws IOException { directory.close(); } - public void testParentChildTerms() throws IOException { Directory directory = newDirectory(); RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory); @@ -134,13 +139,13 @@ public void testParentChildTerms() throws IOException { SortedMap entries = new TreeMap<>(); for (Tuple value : expectedParentChildRelations.values()) { Long l = entries.computeIfAbsent(value.v2(), integer -> 0L); - entries.put(value.v2(), l+1); + entries.put(value.v2(), l + 1); } List> sortedValues = new ArrayList<>(entries.entrySet()); sortedValues.sort((o1, o2) -> { // sort larger values first int ret = o2.getValue().compareTo(o1.getValue()); - if(ret != 0) { + if (ret != 0) { return ret; } @@ -148,8 +153,10 @@ public void testParentChildTerms() throws IOException { return o1.getKey().compareTo(o2.getKey()); }); - IndexReader indexReader = ElasticsearchDirectoryReader.wrap(DirectoryReader.open(directory), - new ShardId(new Index("foo", "_na_"), 1)); + IndexReader indexReader = ElasticsearchDirectoryReader.wrap( + DirectoryReader.open(directory), + new ShardId(new Index("foo", "_na_"), 1) + ); // TODO set "maybeWrap" to true for IndexSearcher once #23338 is resolved IndexSearcher indexSearcher = newSearcher(indexReader, false, true); @@ -167,7 +174,7 @@ public void testParentChildTerms() throws IOException { for (Map.Entry entry : sortedValues) { LongTerms.Bucket bucket = valueTermsBuckets.get(i); assertEquals(entry.getKey().longValue(), bucket.getKeyAsNumber()); - assertEquals(entry.getValue(), (Long)bucket.getDocCount()); + assertEquals(entry.getValue(), (Long) bucket.getDocCount()); i++; } @@ -187,11 +194,13 @@ public void testTermsParentChildTerms() throws IOException { SortedMap sortedValues = new TreeMap<>(); for (Tuple value : expectedParentChildRelations.values()) { Long l = sortedValues.computeIfAbsent(value.v2(), integer -> 0L); - sortedValues.put(value.v2(), l+1); + sortedValues.put(value.v2(), l + 1); } - IndexReader indexReader = ElasticsearchDirectoryReader.wrap(DirectoryReader.open(directory), - new ShardId(new Index("foo", "_na_"), 1)); + IndexReader indexReader = ElasticsearchDirectoryReader.wrap( + DirectoryReader.open(directory), + new ShardId(new Index("foo", "_na_"), 1) + ); // TODO set "maybeWrap" to true for IndexSearcher once #23338 is resolved IndexSearcher indexSearcher = newSearcher(indexReader, false, true); @@ -233,18 +242,18 @@ private static Map> setupIndex(RandomIndexWriter private static List createParentDocument(String id, int value) { return Arrays.asList( - new StringField(IdFieldMapper.NAME, Uid.encodeId(id), Field.Store.NO), - new StringField("join_field", PARENT_TYPE, Field.Store.NO), - createJoinField(PARENT_TYPE, id), - new SortedNumericDocValuesField("number", value) + new StringField(IdFieldMapper.NAME, Uid.encodeId(id), Field.Store.NO), + new StringField("join_field", PARENT_TYPE, Field.Store.NO), + createJoinField(PARENT_TYPE, id), + new SortedNumericDocValuesField("number", value) ); } private static List createChildDocument(String childId, String parentId, int value) { return Arrays.asList( - new StringField(IdFieldMapper.NAME, Uid.encodeId(childId), Field.Store.NO), - new StringField("join_field", CHILD_TYPE, Field.Store.NO), - createJoinField(PARENT_TYPE, parentId), + new StringField(IdFieldMapper.NAME, Uid.encodeId(childId), Field.Store.NO), + new StringField("join_field", CHILD_TYPE, Field.Store.NO), + createJoinField(PARENT_TYPE, parentId), new SortedNumericDocValuesField("subNumber", value) ); } @@ -253,8 +262,7 @@ private static SortedDocValuesField createJoinField(String parentType, String id return new SortedDocValuesField("join_field#" + parentType, new BytesRef(id)); } - private void testCase(Query query, IndexSearcher indexSearcher, Consumer verify) - throws IOException { + private void testCase(Query query, IndexSearcher indexSearcher, Consumer verify) throws IOException { ParentAggregationBuilder aggregationBuilder = new ParentAggregationBuilder("_name", CHILD_TYPE); aggregationBuilder.subAggregation(new MinAggregationBuilder("in_parent").field("number")); @@ -264,8 +272,7 @@ private void testCase(Query query, IndexSearcher indexSearcher, Consumer verify) - throws IOException { + private void testCaseTerms(Query query, IndexSearcher indexSearcher, Consumer verify) throws IOException { ParentAggregationBuilder aggregationBuilder = new ParentAggregationBuilder("_name", CHILD_TYPE); aggregationBuilder.subAggregation(new TermsAggregationBuilder("value_terms").field("number")); @@ -276,12 +283,13 @@ private void testCaseTerms(Query query, IndexSearcher indexSearcher, Consumer verify) - throws IOException { - AggregationBuilder aggregationBuilder = - new TermsAggregationBuilder("subvalue_terms").field("subNumber"). - subAggregation(new ParentAggregationBuilder("to_parent", CHILD_TYPE). - subAggregation(new TermsAggregationBuilder("value_terms").field("number"))); + private void testCaseTermsParentTerms(Query query, IndexSearcher indexSearcher, Consumer verify) throws IOException { + AggregationBuilder aggregationBuilder = new TermsAggregationBuilder("subvalue_terms").field("subNumber") + .subAggregation( + new ParentAggregationBuilder("to_parent", CHILD_TYPE).subAggregation( + new TermsAggregationBuilder("value_terms").field("number") + ) + ); MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.LONG); MappedFieldType subFieldType = new NumberFieldMapper.NumberFieldType("subNumber", NumberFieldMapper.NumberType.LONG); @@ -294,7 +302,6 @@ protected List getSearchPlugins() { return Collections.singletonList(new ParentJoinPlugin()); } - static MappedFieldType[] withJoinFields(MappedFieldType... fieldTypes) { MappedFieldType[] result = new MappedFieldType[fieldTypes.length + 2]; System.arraycopy(fieldTypes, 0, result, 0, fieldTypes.length); diff --git a/modules/parent-join/src/test/java/org/elasticsearch/join/aggregations/InternalChildrenTests.java b/modules/parent-join/src/test/java/org/elasticsearch/join/aggregations/InternalChildrenTests.java index 8f24c5593292f..bb8c9fa168332 100644 --- a/modules/parent-join/src/test/java/org/elasticsearch/join/aggregations/InternalChildrenTests.java +++ b/modules/parent-join/src/test/java/org/elasticsearch/join/aggregations/InternalChildrenTests.java @@ -8,15 +8,15 @@ package org.elasticsearch.join.aggregations; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.util.CollectionUtils; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.join.ParentJoinPlugin; import org.elasticsearch.plugins.SearchPlugin; import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.InternalAggregations; import org.elasticsearch.search.aggregations.InternalSingleBucketAggregationTestCase; import org.elasticsearch.search.aggregations.bucket.ParsedSingleBucketAggregation; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import java.util.List; import java.util.Map; @@ -30,13 +30,23 @@ protected SearchPlugin registerPlugin() { @Override protected List getNamedXContents() { - return CollectionUtils.appendToCopy(super.getNamedXContents(), new NamedXContentRegistry.Entry( - Aggregation.class, new ParseField(ChildrenAggregationBuilder.NAME), (p, c) -> ParsedChildren.fromXContent(p, (String) c))); + return CollectionUtils.appendToCopy( + super.getNamedXContents(), + new NamedXContentRegistry.Entry( + Aggregation.class, + new ParseField(ChildrenAggregationBuilder.NAME), + (p, c) -> ParsedChildren.fromXContent(p, (String) c) + ) + ); } @Override - protected InternalChildren createTestInstance(String name, long docCount, InternalAggregations aggregations, - Map metadata) { + protected InternalChildren createTestInstance( + String name, + long docCount, + InternalAggregations aggregations, + Map metadata + ) { return new InternalChildren(name, docCount, aggregations, metadata); } diff --git a/modules/parent-join/src/test/java/org/elasticsearch/join/aggregations/InternalParentTests.java b/modules/parent-join/src/test/java/org/elasticsearch/join/aggregations/InternalParentTests.java index 77a1721cf2eca..25e8315756048 100644 --- a/modules/parent-join/src/test/java/org/elasticsearch/join/aggregations/InternalParentTests.java +++ b/modules/parent-join/src/test/java/org/elasticsearch/join/aggregations/InternalParentTests.java @@ -8,15 +8,15 @@ package org.elasticsearch.join.aggregations; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.util.CollectionUtils; -import org.elasticsearch.xcontent.NamedXContentRegistry.Entry; import org.elasticsearch.join.ParentJoinPlugin; import org.elasticsearch.plugins.SearchPlugin; import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.InternalAggregations; import org.elasticsearch.search.aggregations.InternalSingleBucketAggregationTestCase; import org.elasticsearch.search.aggregations.bucket.ParsedSingleBucketAggregation; +import org.elasticsearch.xcontent.NamedXContentRegistry.Entry; +import org.elasticsearch.xcontent.ParseField; import java.util.List; import java.util.Map; @@ -30,13 +30,19 @@ protected SearchPlugin registerPlugin() { @Override protected List getNamedXContents() { - return CollectionUtils.appendToCopy(super.getNamedXContents(), new Entry( - Aggregation.class, new ParseField(ParentAggregationBuilder.NAME), (p, c) -> ParsedParent.fromXContent(p, (String) c))); + return CollectionUtils.appendToCopy( + super.getNamedXContents(), + new Entry(Aggregation.class, new ParseField(ParentAggregationBuilder.NAME), (p, c) -> ParsedParent.fromXContent(p, (String) c)) + ); } @Override - protected InternalParent createTestInstance(String name, long docCount, InternalAggregations aggregations, - Map metadata) { + protected InternalParent createTestInstance( + String name, + long docCount, + InternalAggregations aggregations, + Map metadata + ) { return new InternalParent(name, docCount, aggregations, metadata); } diff --git a/modules/parent-join/src/test/java/org/elasticsearch/join/aggregations/ParentTests.java b/modules/parent-join/src/test/java/org/elasticsearch/join/aggregations/ParentTests.java index 359479623443d..c424dab398803 100644 --- a/modules/parent-join/src/test/java/org/elasticsearch/join/aggregations/ParentTests.java +++ b/modules/parent-join/src/test/java/org/elasticsearch/join/aggregations/ParentTests.java @@ -8,14 +8,14 @@ package org.elasticsearch.join.aggregations; -import java.util.Arrays; -import java.util.Collection; - import org.elasticsearch.join.ParentJoinPlugin; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.search.aggregations.BaseAggregationTestCase; import org.elasticsearch.test.TestGeoShapeFieldMapperPlugin; +import java.util.Arrays; +import java.util.Collection; + public class ParentTests extends BaseAggregationTestCase { @Override diff --git a/modules/parent-join/src/test/java/org/elasticsearch/join/aggregations/ParentToChildrenAggregatorTests.java b/modules/parent-join/src/test/java/org/elasticsearch/join/aggregations/ParentToChildrenAggregatorTests.java index b71aab3a156c0..bf61082a2fc61 100644 --- a/modules/parent-join/src/test/java/org/elasticsearch/join/aggregations/ParentToChildrenAggregatorTests.java +++ b/modules/parent-join/src/test/java/org/elasticsearch/join/aggregations/ParentToChildrenAggregatorTests.java @@ -22,8 +22,8 @@ import org.apache.lucene.search.TermInSetQuery; import org.apache.lucene.store.Directory; import org.apache.lucene.util.BytesRef; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.lucene.index.ElasticsearchDirectoryReader; +import org.elasticsearch.core.Tuple; import org.elasticsearch.index.Index; import org.elasticsearch.index.mapper.IdFieldMapper; import org.elasticsearch.index.mapper.KeywordFieldMapper; @@ -66,8 +66,11 @@ public void testNoDocs() throws IOException { testCase(new MatchAllDocsQuery(), newSearcher(indexReader, false, true), parentToChild -> { assertEquals(0, parentToChild.getDocCount()); - assertEquals(Double.POSITIVE_INFINITY, ((InternalMin) parentToChild.getAggregations().get("in_child")).getValue(), - Double.MIN_VALUE); + assertEquals( + Double.POSITIVE_INFINITY, + ((InternalMin) parentToChild.getAggregations().get("in_child")).getValue(), + Double.MIN_VALUE + ); }); indexReader.close(); directory.close(); @@ -80,8 +83,10 @@ public void testParentChild() throws IOException { final Map> expectedParentChildRelations = setupIndex(indexWriter); indexWriter.close(); - IndexReader indexReader = ElasticsearchDirectoryReader.wrap(DirectoryReader.open(directory), - new ShardId(new Index("foo", "_na_"), 1)); + IndexReader indexReader = ElasticsearchDirectoryReader.wrap( + DirectoryReader.open(directory), + new ShardId(new Index("foo", "_na_"), 1) + ); // TODO set "maybeWrap" to true for IndexSearcher once #23338 is resolved IndexSearcher indexSearcher = newSearcher(indexReader, false, true); @@ -100,8 +105,11 @@ public void testParentChild() throws IOException { for (String parent : expectedParentChildRelations.keySet()) { testCase(new TermInSetQuery(IdFieldMapper.NAME, Uid.encodeId(parent)), indexSearcher, child -> { assertEquals((long) expectedParentChildRelations.get(parent).v1(), child.getDocCount()); - assertEquals(expectedParentChildRelations.get(parent).v2(), - ((InternalMin) child.getAggregations().get("in_child")).getValue(), Double.MIN_VALUE); + assertEquals( + expectedParentChildRelations.get(parent).v2(), + ((InternalMin) child.getAggregations().get("in_child")).getValue(), + Double.MIN_VALUE + ); }); } indexReader.close(); @@ -190,20 +198,20 @@ private static Map> setupIndex(RandomIndexWriter private static List createParentDocument(String id, String kwd) { return Arrays.asList( - new StringField(IdFieldMapper.NAME, Uid.encodeId(id), Field.Store.NO), - new SortedSetDocValuesField("kwd", new BytesRef(kwd)), - new Field("kwd", new BytesRef(kwd), KeywordFieldMapper.Defaults.FIELD_TYPE), - new StringField("join_field", PARENT_TYPE, Field.Store.NO), - createJoinField(PARENT_TYPE, id) + new StringField(IdFieldMapper.NAME, Uid.encodeId(id), Field.Store.NO), + new SortedSetDocValuesField("kwd", new BytesRef(kwd)), + new Field("kwd", new BytesRef(kwd), KeywordFieldMapper.Defaults.FIELD_TYPE), + new StringField("join_field", PARENT_TYPE, Field.Store.NO), + createJoinField(PARENT_TYPE, id) ); } private static List createChildDocument(String childId, String parentId, int value) { return Arrays.asList( - new StringField(IdFieldMapper.NAME, Uid.encodeId(childId), Field.Store.NO), - new StringField("join_field", CHILD_TYPE, Field.Store.NO), - createJoinField(PARENT_TYPE, parentId), - new SortedNumericDocValuesField("number", value) + new StringField(IdFieldMapper.NAME, Uid.encodeId(childId), Field.Store.NO), + new StringField("join_field", CHILD_TYPE, Field.Store.NO), + createJoinField(PARENT_TYPE, parentId), + new SortedNumericDocValuesField("number", value) ); } @@ -211,8 +219,7 @@ private static SortedDocValuesField createJoinField(String parentType, String id return new SortedDocValuesField("join_field#" + parentType, new BytesRef(id)); } - private void testCase(Query query, IndexSearcher indexSearcher, Consumer verify) - throws IOException { + private void testCase(Query query, IndexSearcher indexSearcher, Consumer verify) throws IOException { ChildrenAggregationBuilder aggregationBuilder = new ChildrenAggregationBuilder("_name", CHILD_TYPE); aggregationBuilder.subAggregation(new MinAggregationBuilder("in_child").field("number")); diff --git a/modules/parent-join/src/test/java/org/elasticsearch/join/mapper/JoinFieldTypeTests.java b/modules/parent-join/src/test/java/org/elasticsearch/join/mapper/JoinFieldTypeTests.java index 6fc9f58f20888..8f6a65eab7b13 100644 --- a/modules/parent-join/src/test/java/org/elasticsearch/join/mapper/JoinFieldTypeTests.java +++ b/modules/parent-join/src/test/java/org/elasticsearch/join/mapper/JoinFieldTypeTests.java @@ -19,9 +19,7 @@ public class JoinFieldTypeTests extends FieldTypeTestCase { public void testFetchSourceValue() throws IOException { - MappedFieldType fieldType = new ParentJoinFieldMapper.Builder("field") - .build(MapperBuilderContext.ROOT) - .fieldType(); + MappedFieldType fieldType = new ParentJoinFieldMapper.Builder("field").build(MapperBuilderContext.ROOT).fieldType(); Map parentValue = Map.of("relation", "parent"); assertEquals(List.of(parentValue), fetchSourceValue(fieldType, parentValue)); @@ -29,8 +27,7 @@ public void testFetchSourceValue() throws IOException { Map childValue = Map.of("relation", "child", "parent", "1"); assertEquals(List.of(childValue), fetchSourceValue(fieldType, childValue)); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> fetchSourceValue(fieldType, parentValue, "format")); + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> fetchSourceValue(fieldType, parentValue, "format")); assertEquals("Field [field] of type [join] doesn't support formats.", e.getMessage()); } } diff --git a/modules/parent-join/src/test/java/org/elasticsearch/join/mapper/ParentJoinFieldMapperTests.java b/modules/parent-join/src/test/java/org/elasticsearch/join/mapper/ParentJoinFieldMapperTests.java index e263c83f3d5d4..a733179ab5e90 100644 --- a/modules/parent-join/src/test/java/org/elasticsearch/join/mapper/ParentJoinFieldMapperTests.java +++ b/modules/parent-join/src/test/java/org/elasticsearch/join/mapper/ParentJoinFieldMapperTests.java @@ -11,8 +11,6 @@ import org.apache.lucene.index.Term; import org.apache.lucene.search.TermQuery; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.mapper.Mapper; @@ -24,6 +22,8 @@ import org.elasticsearch.index.mapper.SourceToParse; import org.elasticsearch.join.ParentJoinPlugin; import org.elasticsearch.plugins.Plugin; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentType; import java.io.IOException; import java.util.Collection; @@ -46,8 +46,9 @@ public void testSingleLevel() throws Exception { b.endObject(); })); DocumentMapper docMapper = mapperService.documentMapper(); - Joiner joiner = Joiner.getJoiner(mapperService.mappingLookup().getMatchingFieldNames("*").stream() - .map(mapperService.mappingLookup()::getFieldType)); + Joiner joiner = Joiner.getJoiner( + mapperService.mappingLookup().getMatchingFieldNames("*").stream().map(mapperService.mappingLookup()::getFieldType) + ); assertNotNull(joiner); assertEquals("join_field", joiner.getJoinField()); @@ -66,8 +67,10 @@ public void testSingleLevel() throws Exception { assertEquals("child", doc.rootDoc().getBinaryValue("join_field").utf8ToString()); // Unknown join name - MapperException exc = expectThrows(MapperParsingException.class, - () -> docMapper.parse(source(b -> b.field("join_field", "unknown")))); + MapperException exc = expectThrows( + MapperParsingException.class, + () -> docMapper.parse(source(b -> b.field("join_field", "unknown"))) + ); assertThat(exc.getRootCause().getMessage(), containsString("unknown join name [unknown] for field [join_field]")); } @@ -98,8 +101,9 @@ public void testMultipleLevels() throws Exception { })); // Doc without join - ParsedDocument doc = docMapper.parse(new SourceToParse("test", "0", - BytesReference.bytes(XContentFactory.jsonBuilder().startObject().endObject()), XContentType.JSON)); + ParsedDocument doc = docMapper.parse( + new SourceToParse("test", "0", BytesReference.bytes(XContentFactory.jsonBuilder().startObject().endObject()), XContentType.JSON) + ); assertNull(doc.rootDoc().getBinaryValue("join_field")); // Doc parent @@ -135,11 +139,17 @@ public void testMultipleLevels() throws Exception { assertEquals("grand_child", doc.rootDoc().getBinaryValue("join_field").utf8ToString()); // Unknown join name - exc = expectThrows(MapperParsingException.class, - () -> docMapper.parse(new SourceToParse("test", "1", - BytesReference.bytes(XContentFactory.jsonBuilder().startObject() - .field("join_field", "unknown") - .endObject()), XContentType.JSON))); + exc = expectThrows( + MapperParsingException.class, + () -> docMapper.parse( + new SourceToParse( + "test", + "1", + BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field("join_field", "unknown").endObject()), + XContentType.JSON + ) + ) + ); assertThat(exc.getRootCause().getMessage(), containsString("unknown join name [unknown] for field [join_field]")); } @@ -233,8 +243,9 @@ public void testUpdateRelations() throws Exception { b.endObject(); })); - Joiner joiner = Joiner.getJoiner(mapperService.mappingLookup().getMatchingFieldNames("*").stream() - .map(mapperService.mappingLookup()::getFieldType)); + Joiner joiner = Joiner.getJoiner( + mapperService.mappingLookup().getMatchingFieldNames("*").stream().map(mapperService.mappingLookup()::getFieldType) + ); assertNotNull(joiner); assertEquals("join_field", joiner.getJoinField()); assertTrue(joiner.childTypeExists("child2")); @@ -260,8 +271,9 @@ public void testUpdateRelations() throws Exception { } b.endObject(); })); - joiner = Joiner.getJoiner(mapperService.mappingLookup().getMatchingFieldNames("*").stream() - .map(mapperService.mappingLookup()::getFieldType)); + joiner = Joiner.getJoiner( + mapperService.mappingLookup().getMatchingFieldNames("*").stream().map(mapperService.mappingLookup()::getFieldType) + ); assertNotNull(joiner); assertEquals("join_field", joiner.getJoinField()); assertTrue(joiner.childTypeExists("child2")); @@ -295,8 +307,10 @@ public void testInvalidJoinFieldInsideObject() throws Exception { } b.endObject(); }))); - assertThat(exc.getRootCause().getMessage(), - containsString("join field [object.join_field] cannot be added inside an object or in a multi-field")); + assertThat( + exc.getRootCause().getMessage(), + containsString("join field [object.join_field] cannot be added inside an object or in a multi-field") + ); } public void testInvalidJoinFieldInsideMultiFields() throws Exception { @@ -333,8 +347,10 @@ public void testMultipleJoinFields() throws Exception { b.field("type", "join"); b.startObject("relations").field("product", "item").endObject().endObject(); }))); - assertThat(exc.getMessage(), - equalTo("Only one [parent-join] field can be defined per index, got [join_field, another_join_field]")); + assertThat( + exc.getMessage(), + equalTo("Only one [parent-join] field can be defined per index, got [join_field, another_join_field]") + ); } { @@ -349,35 +365,44 @@ public void testMultipleJoinFields() throws Exception { IllegalArgumentException.class, () -> merge(mapperService, mapping(b -> b.startObject("another_join_field").field("type", "join").endObject())) ); - assertThat(exc.getMessage(), - equalTo("Only one [parent-join] field can be defined per index, got [join_field, another_join_field]")); + assertThat( + exc.getMessage(), + equalTo("Only one [parent-join] field can be defined per index, got [join_field, another_join_field]") + ); } } public void testEagerGlobalOrdinals() throws Exception { - MapperService mapperService = createMapperService(mapping(b -> b - .startObject("join_field") + MapperService mapperService = createMapperService( + mapping( + b -> b.startObject("join_field") .field("type", "join") .startObject("relations") - .field("parent", "child") - .field("child", "grand_child") + .field("parent", "child") + .field("child", "grand_child") + .endObject() .endObject() - .endObject())); + ) + ); assertFalse(mapperService.fieldType("join_field").eagerGlobalOrdinals()); assertNotNull(mapperService.fieldType("join_field#parent")); assertTrue(mapperService.fieldType("join_field#parent").eagerGlobalOrdinals()); assertNotNull(mapperService.fieldType("join_field#child")); assertTrue(mapperService.fieldType("join_field#child").eagerGlobalOrdinals()); - merge(mapperService, mapping(b -> b - .startObject("join_field") + merge( + mapperService, + mapping( + b -> b.startObject("join_field") .field("type", "join") .field("eager_global_ordinals", false) .startObject("relations") - .field("parent", "child") - .field("child", "grand_child") + .field("parent", "child") + .field("child", "grand_child") + .endObject() .endObject() - .endObject())); + ) + ); assertFalse(mapperService.fieldType("join_field").eagerGlobalOrdinals()); assertNotNull(mapperService.fieldType("join_field#parent")); assertFalse(mapperService.fieldType("join_field#parent").eagerGlobalOrdinals()); @@ -386,14 +411,17 @@ public void testEagerGlobalOrdinals() throws Exception { } public void testSubFields() throws IOException { - MapperService mapperService = createMapperService(mapping(b -> b - .startObject("join_field") - .field("type", "join") - .startObject("relations") + MapperService mapperService = createMapperService( + mapping( + b -> b.startObject("join_field") + .field("type", "join") + .startObject("relations") .field("parent", "child") .field("child", "grand_child") - .endObject() - .endObject())); + .endObject() + .endObject() + ) + ); ParentJoinFieldMapper mapper = (ParentJoinFieldMapper) mapperService.mappingLookup().getMapper("join_field"); assertTrue(mapper.fieldType().isSearchable()); assertTrue(mapper.fieldType().isAggregatable()); diff --git a/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasChildQueryBuilderTests.java b/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasChildQueryBuilderTests.java index a1d70ae4196ff..7a96875d4b2a6 100644 --- a/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasChildQueryBuilderTests.java +++ b/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasChildQueryBuilderTests.java @@ -9,6 +9,7 @@ package org.elasticsearch.join.query; import com.carrotsearch.randomizedtesting.generators.RandomPicks; + import org.apache.lucene.index.Term; import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.BooleanQuery; @@ -25,7 +26,6 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.compress.CompressedXContent; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.index.mapper.IdFieldMapper; import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.mapper.Uid; @@ -34,8 +34,8 @@ import org.elasticsearch.index.query.InnerHitContextBuilder; import org.elasticsearch.index.query.MatchAllQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; -import org.elasticsearch.index.query.SearchExecutionContext; import org.elasticsearch.index.query.QueryShardException; +import org.elasticsearch.index.query.SearchExecutionContext; import org.elasticsearch.index.query.TermQueryBuilder; import org.elasticsearch.index.query.WrapperQueryBuilder; import org.elasticsearch.index.similarity.SimilarityService; @@ -46,6 +46,7 @@ import org.elasticsearch.test.AbstractQueryTestCase; import org.elasticsearch.test.TestGeoShapeFieldMapperPlugin; import org.elasticsearch.test.VersionUtils; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.Arrays; @@ -54,9 +55,9 @@ import java.util.HashMap; import java.util.Map; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.join.query.HasChildQueryBuilder.LateParsingQuery; import static org.elasticsearch.join.query.JoinQueryBuilders.hasChildQuery; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.instanceOf; @@ -82,42 +83,45 @@ protected Collection> getPlugins() { @Override protected void initializeAdditionalMappings(MapperService mapperService) throws IOException { similarity = randomFrom("boolean", "BM25"); - XContentBuilder mapping = jsonBuilder().startObject().startObject("_doc").startObject("properties") + XContentBuilder mapping = jsonBuilder().startObject() + .startObject("_doc") + .startObject("properties") .startObject("join_field") - .field("type", "join") - .startObject("relations") - .field(PARENT_DOC, CHILD_DOC) - .endObject() + .field("type", "join") + .startObject("relations") + .field(PARENT_DOC, CHILD_DOC) + .endObject() .endObject() .startObject(TEXT_FIELD_NAME) - .field("type", "text") + .field("type", "text") .endObject() .startObject(KEYWORD_FIELD_NAME) - .field("type", "keyword") + .field("type", "keyword") .endObject() .startObject(INT_FIELD_NAME) - .field("type", "integer") + .field("type", "integer") .endObject() .startObject(DOUBLE_FIELD_NAME) - .field("type", "double") + .field("type", "double") .endObject() .startObject(BOOLEAN_FIELD_NAME) .field("type", "boolean") .endObject() .startObject(DATE_FIELD_NAME) - .field("type", "date") + .field("type", "date") .endObject() .startObject(OBJECT_FIELD_NAME) - .field("type", "object") + .field("type", "object") .endObject() .startObject("custom_string") - .field("type", "text") - .field("similarity", similarity) + .field("type", "text") + .field("similarity", similarity) + .endObject() + .endObject() .endObject() - .endObject().endObject().endObject(); + .endObject(); - mapperService.merge(TYPE, - new CompressedXContent(Strings.toString(mapping)), MapperService.MergeReason.MAPPING_UPDATE); + mapperService.merge(TYPE, new CompressedXContent(Strings.toString(mapping)), MapperService.MergeReason.MAPPING_UPDATE); } /** @@ -134,22 +138,25 @@ protected HasChildQueryBuilder doCreateTestQueryBuilder() { innerQueryBuilder = new WrapperQueryBuilder(innerQueryBuilder.toString()); } - HasChildQueryBuilder hqb = new HasChildQueryBuilder(CHILD_DOC, innerQueryBuilder, - RandomPicks.randomFrom(random(), ScoreMode.values())); + HasChildQueryBuilder hqb = new HasChildQueryBuilder( + CHILD_DOC, + innerQueryBuilder, + RandomPicks.randomFrom(random(), ScoreMode.values()) + ); hqb.minMaxChildren(min, max); hqb.ignoreUnmapped(randomBoolean()); if (randomBoolean()) { - hqb.innerHit(new InnerHitBuilder() - .setName(randomAlphaOfLengthBetween(1, 10)) + hqb.innerHit( + new InnerHitBuilder().setName(randomAlphaOfLengthBetween(1, 10)) .setSize(randomIntBetween(0, 100)) - .addSort(new FieldSortBuilder(KEYWORD_FIELD_NAME).order(SortOrder.ASC))); + .addSort(new FieldSortBuilder(KEYWORD_FIELD_NAME).order(SortOrder.ASC)) + ); } return hqb; } @Override - protected void doAssertLuceneQuery(HasChildQueryBuilder queryBuilder, Query query, - SearchExecutionContext context) throws IOException { + protected void doAssertLuceneQuery(HasChildQueryBuilder queryBuilder, Query query, SearchExecutionContext context) throws IOException { assertThat(query, instanceOf(LateParsingQuery.class)); LateParsingQuery lpq = (LateParsingQuery) query; assertEquals(queryBuilder.minChildren(), lpq.getMinChildren()); @@ -179,8 +186,7 @@ public void testSerializationBWC() throws IOException { public void testIllegalValues() { QueryBuilder query = new MatchAllQueryBuilder(); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> hasChildQuery(null, query, ScoreMode.None)); + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> hasChildQuery(null, query, ScoreMode.None)); assertEquals("[has_child] requires 'type' field", e.getMessage()); e = expectThrows(IllegalArgumentException.class, () -> hasChildQuery("foo", null, ScoreMode.None)); @@ -202,44 +208,43 @@ public void testIllegalValues() { } public void testFromJson() throws IOException { - String query = - "{\n" + - " \"has_child\" : {\n" + - " \"query\" : {\n" + - " \"range\" : {\n" + - " \"mapped_string\" : {\n" + - " \"from\" : \"agJhRET\",\n" + - " \"to\" : \"zvqIq\",\n" + - " \"include_lower\" : true,\n" + - " \"include_upper\" : true,\n" + - " \"boost\" : 1.0\n" + - " }\n" + - " }\n" + - " },\n" + - " \"type\" : \"child\",\n" + - " \"score_mode\" : \"avg\",\n" + - " \"min_children\" : 883170873,\n" + - " \"max_children\" : 1217235442,\n" + - " \"ignore_unmapped\" : false,\n" + - " \"boost\" : 2.0,\n" + - " \"_name\" : \"WNzYMJKRwePuRBh\",\n" + - " \"inner_hits\" : {\n" + - " \"name\" : \"inner_hits_name\",\n" + - " \"ignore_unmapped\" : false,\n" + - " \"from\" : 0,\n" + - " \"size\" : 100,\n" + - " \"version\" : false,\n" + - " \"seq_no_primary_term\" : false,\n" + - " \"explain\" : false,\n" + - " \"track_scores\" : false,\n" + - " \"sort\" : [ {\n" + - " \"mapped_string\" : {\n" + - " \"order\" : \"asc\"\n" + - " }\n" + - " } ]\n" + - " }\n" + - " }\n" + - "}"; + String query = "{\n" + + " \"has_child\" : {\n" + + " \"query\" : {\n" + + " \"range\" : {\n" + + " \"mapped_string\" : {\n" + + " \"from\" : \"agJhRET\",\n" + + " \"to\" : \"zvqIq\",\n" + + " \"include_lower\" : true,\n" + + " \"include_upper\" : true,\n" + + " \"boost\" : 1.0\n" + + " }\n" + + " }\n" + + " },\n" + + " \"type\" : \"child\",\n" + + " \"score_mode\" : \"avg\",\n" + + " \"min_children\" : 883170873,\n" + + " \"max_children\" : 1217235442,\n" + + " \"ignore_unmapped\" : false,\n" + + " \"boost\" : 2.0,\n" + + " \"_name\" : \"WNzYMJKRwePuRBh\",\n" + + " \"inner_hits\" : {\n" + + " \"name\" : \"inner_hits_name\",\n" + + " \"ignore_unmapped\" : false,\n" + + " \"from\" : 0,\n" + + " \"size\" : 100,\n" + + " \"version\" : false,\n" + + " \"seq_no_primary_term\" : false,\n" + + " \"explain\" : false,\n" + + " \"track_scores\" : false,\n" + + " \"sort\" : [ {\n" + + " \"mapped_string\" : {\n" + + " \"order\" : \"asc\"\n" + + " }\n" + + " } ]\n" + + " }\n" + + " }\n" + + "}"; HasChildQueryBuilder queryBuilder = (HasChildQueryBuilder) parseQuery(query); checkGeneratedJson(query, queryBuilder); assertEquals(query, queryBuilder.maxChildren(), 1217235442); @@ -249,10 +254,9 @@ public void testFromJson() throws IOException { assertEquals(query, queryBuilder.childType(), "child"); assertEquals(query, queryBuilder.scoreMode(), ScoreMode.Avg); assertNotNull(query, queryBuilder.innerHit()); - InnerHitBuilder expected = new InnerHitBuilder("child") - .setName("inner_hits_name") - .setSize(100) - .addSort(new FieldSortBuilder("mapped_string").order(SortOrder.ASC)); + InnerHitBuilder expected = new InnerHitBuilder("child").setName("inner_hits_name") + .setSize(100) + .addSort(new FieldSortBuilder("mapped_string").order(SortOrder.ASC)); assertEquals(query, queryBuilder.innerHit(), expected); } @@ -269,7 +273,7 @@ static void assertLateParsingQuery(Query query, String type, String id) throws I assertThat(lateParsingQuery.getInnerQuery(), instanceOf(BooleanQuery.class)); BooleanQuery booleanQuery = (BooleanQuery) lateParsingQuery.getInnerQuery(); assertThat(booleanQuery.clauses().size(), equalTo(2)); - //check the inner ids query, we have to call rewrite to get to check the type it's executed against + // check the inner ids query, we have to call rewrite to get to check the type it's executed against assertThat(booleanQuery.clauses().get(0).getOccur(), equalTo(BooleanClause.Occur.MUST)); assertThat(booleanQuery.clauses().get(0).getQuery(), instanceOf(TermInSetQuery.class)); TermInSetQuery termsQuery = (TermInSetQuery) booleanQuery.clauses().get(0).getQuery(); @@ -284,7 +288,7 @@ static void assertLateParsingQuery(Query query, String type, String id) throws I TermQuery termQuery = (TermQuery) booleanTermsQuery.clauses().get(0).getQuery(); assertThat(termQuery.getTerm().field(), equalTo(IdFieldMapper.NAME)); assertThat(termQuery.getTerm().bytes(), equalTo(Uid.encodeId(id))); - //check the type filter + // check the type filter assertThat(booleanQuery.clauses().get(1).getOccur(), equalTo(BooleanClause.Occur.FILTER)); assertEquals(new TermQuery(new Term("join_field", type)), booleanQuery.clauses().get(1).getQuery()); } @@ -302,11 +306,13 @@ public void testMustRewrite() throws IOException { public void testNonDefaultSimilarity() throws Exception { SearchExecutionContext searchExecutionContext = createSearchExecutionContext(); - HasChildQueryBuilder hasChildQueryBuilder = - hasChildQuery(CHILD_DOC, new TermQueryBuilder("custom_string", "value"), ScoreMode.None); + HasChildQueryBuilder hasChildQueryBuilder = hasChildQuery( + CHILD_DOC, + new TermQueryBuilder("custom_string", "value"), + ScoreMode.None + ); LateParsingQuery query = (LateParsingQuery) hasChildQueryBuilder.toQuery(searchExecutionContext); - Similarity expected = SimilarityService.BUILT_IN.get(similarity) - .apply(Settings.EMPTY, Version.CURRENT, null); + Similarity expected = SimilarityService.BUILT_IN.get(similarity).apply(Settings.EMPTY, Version.CURRENT, null); assertThat(((PerFieldSimilarityWrapper) query.getSimilarity()).get("custom_string"), instanceOf(expected.getClass())); } @@ -326,14 +332,19 @@ public void testIgnoreUnmapped() throws IOException { failingQueryBuilder.ignoreUnmapped(false); assertFalse(failingQueryBuilder.innerHit().isIgnoreUnmapped()); QueryShardException e = expectThrows(QueryShardException.class, () -> failingQueryBuilder.toQuery(createSearchExecutionContext())); - assertThat(e.getMessage(), containsString("[" + HasChildQueryBuilder.NAME + - "] join field [join_field] doesn't hold [unmapped] as a child")); + assertThat( + e.getMessage(), + containsString("[" + HasChildQueryBuilder.NAME + "] join field [join_field] doesn't hold [unmapped] as a child") + ); } public void testIgnoreUnmappedWithRewrite() throws IOException { // WrapperQueryBuilder makes sure we always rewrite - final HasChildQueryBuilder queryBuilder - = new HasChildQueryBuilder("unmapped", new WrapperQueryBuilder(new MatchAllQueryBuilder().toString()), ScoreMode.None); + final HasChildQueryBuilder queryBuilder = new HasChildQueryBuilder( + "unmapped", + new WrapperQueryBuilder(new MatchAllQueryBuilder().toString()), + ScoreMode.None + ); queryBuilder.ignoreUnmapped(true); SearchExecutionContext searchExecutionContext = createSearchExecutionContext(); Query query = queryBuilder.rewrite(searchExecutionContext).toQuery(searchExecutionContext); @@ -342,11 +353,16 @@ public void testIgnoreUnmappedWithRewrite() throws IOException { } public void testExtractInnerHitBuildersWithDuplicate() { - final HasChildQueryBuilder queryBuilder - = new HasChildQueryBuilder(CHILD_DOC, new WrapperQueryBuilder(new MatchAllQueryBuilder().toString()), ScoreMode.None); + final HasChildQueryBuilder queryBuilder = new HasChildQueryBuilder( + CHILD_DOC, + new WrapperQueryBuilder(new MatchAllQueryBuilder().toString()), + ScoreMode.None + ); queryBuilder.innerHit(new InnerHitBuilder("some_name")); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> InnerHitContextBuilder.extractInnerHits(queryBuilder, Collections.singletonMap("some_name", null))); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> InnerHitContextBuilder.extractInnerHits(queryBuilder, Collections.singletonMap("some_name", null)) + ); assertEquals("[inner_hits] already contains an entry for key [some_name]", e.getMessage()); } @@ -354,11 +370,8 @@ public void testDisallowExpensiveQueries() { SearchExecutionContext searchExecutionContext = mock(SearchExecutionContext.class); when(searchExecutionContext.allowExpensiveQueries()).thenReturn(false); - HasChildQueryBuilder queryBuilder = - hasChildQuery(CHILD_DOC, new TermQueryBuilder("custom_string", "value"), ScoreMode.None); - ElasticsearchException e = expectThrows(ElasticsearchException.class, - () -> queryBuilder.toQuery(searchExecutionContext)); - assertEquals("[joining] queries cannot be executed when 'search.allow_expensive_queries' is set to false.", - e.getMessage()); + HasChildQueryBuilder queryBuilder = hasChildQuery(CHILD_DOC, new TermQueryBuilder("custom_string", "value"), ScoreMode.None); + ElasticsearchException e = expectThrows(ElasticsearchException.class, () -> queryBuilder.toQuery(searchExecutionContext)); + assertEquals("[joining] queries cannot be executed when 'search.allow_expensive_queries' is set to false.", e.getMessage()); } } diff --git a/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasParentQueryBuilderTests.java b/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasParentQueryBuilderTests.java index 6d1478f2bf93c..057e45a8a3947 100644 --- a/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasParentQueryBuilderTests.java +++ b/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasParentQueryBuilderTests.java @@ -15,15 +15,14 @@ import org.elasticsearch.Version; import org.elasticsearch.common.Strings; import org.elasticsearch.common.compress.CompressedXContent; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.query.IdsQueryBuilder; import org.elasticsearch.index.query.InnerHitBuilder; import org.elasticsearch.index.query.InnerHitContextBuilder; import org.elasticsearch.index.query.MatchAllQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; -import org.elasticsearch.index.query.SearchExecutionContext; import org.elasticsearch.index.query.QueryShardException; +import org.elasticsearch.index.query.SearchExecutionContext; import org.elasticsearch.index.query.TermQueryBuilder; import org.elasticsearch.index.query.WrapperQueryBuilder; import org.elasticsearch.join.ParentJoinPlugin; @@ -34,6 +33,7 @@ import org.elasticsearch.test.AbstractQueryTestCase; import org.elasticsearch.test.TestGeoShapeFieldMapperPlugin; import org.elasticsearch.test.VersionUtils; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.Arrays; @@ -42,8 +42,8 @@ import java.util.HashMap; import java.util.Map; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.join.query.JoinQueryBuilders.hasParentQuery; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.instanceOf; @@ -65,38 +65,41 @@ protected Collection> getPlugins() { @Override protected void initializeAdditionalMappings(MapperService mapperService) throws IOException { - XContentBuilder mapping = jsonBuilder().startObject().startObject("_doc").startObject("properties") + XContentBuilder mapping = jsonBuilder().startObject() + .startObject("_doc") + .startObject("properties") .startObject("join_field") - .field("type", "join") - .startObject("relations") - .field(PARENT_DOC, CHILD_DOC) - .endObject() + .field("type", "join") + .startObject("relations") + .field(PARENT_DOC, CHILD_DOC) + .endObject() .endObject() .startObject(TEXT_FIELD_NAME) - .field("type", "text") + .field("type", "text") .endObject() - .startObject(KEYWORD_FIELD_NAME) + .startObject(KEYWORD_FIELD_NAME) .field("type", "keyword") .endObject() .startObject(INT_FIELD_NAME) - .field("type", "integer") + .field("type", "integer") .endObject() .startObject(DOUBLE_FIELD_NAME) - .field("type", "double") + .field("type", "double") .endObject() .startObject(BOOLEAN_FIELD_NAME) - .field("type", "boolean") + .field("type", "boolean") .endObject() .startObject(DATE_FIELD_NAME) - .field("type", "date") + .field("type", "date") .endObject() .startObject(OBJECT_FIELD_NAME) - .field("type", "object") + .field("type", "object") + .endObject() + .endObject() .endObject() - .endObject().endObject().endObject(); + .endObject(); - mapperService.merge(TYPE, - new CompressedXContent(Strings.toString(mapping)), MapperService.MergeReason.MAPPING_UPDATE); + mapperService.merge(TYPE, new CompressedXContent(Strings.toString(mapping)), MapperService.MergeReason.MAPPING_UPDATE); } /** @@ -112,17 +115,17 @@ protected HasParentQueryBuilder doCreateTestQueryBuilder() { HasParentQueryBuilder hqb = new HasParentQueryBuilder(PARENT_DOC, innerQueryBuilder, randomBoolean()); hqb.ignoreUnmapped(randomBoolean()); if (randomBoolean()) { - hqb.innerHit(new InnerHitBuilder() - .setName(randomAlphaOfLengthBetween(1, 10)) + hqb.innerHit( + new InnerHitBuilder().setName(randomAlphaOfLengthBetween(1, 10)) .setSize(randomIntBetween(0, 100)) - .addSort(new FieldSortBuilder(KEYWORD_FIELD_NAME).order(SortOrder.ASC))); + .addSort(new FieldSortBuilder(KEYWORD_FIELD_NAME).order(SortOrder.ASC)) + ); } return hqb; } @Override - protected void doAssertLuceneQuery(HasParentQueryBuilder queryBuilder, Query query, - SearchExecutionContext context) throws IOException { + protected void doAssertLuceneQuery(HasParentQueryBuilder queryBuilder, Query query, SearchExecutionContext context) throws IOException { assertThat(query, instanceOf(LateParsingQuery.class)); LateParsingQuery lpq = (LateParsingQuery) query; assertEquals(queryBuilder.score() ? ScoreMode.Max : ScoreMode.None, lpq.getScoreMode()); @@ -153,12 +156,10 @@ public void testSerializationBWC() throws IOException { public void testIllegalValues() throws IOException { QueryBuilder query = new MatchAllQueryBuilder(); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> hasParentQuery(null, query, false)); + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> hasParentQuery(null, query, false)); assertThat(e.getMessage(), equalTo("[has_parent] requires 'parent_type' field")); - e = expectThrows(IllegalArgumentException.class, - () -> hasParentQuery("foo", null, false)); + e = expectThrows(IllegalArgumentException.class, () -> hasParentQuery("foo", null, false)); assertThat(e.getMessage(), equalTo("[has_parent] requires 'query' field")); SearchExecutionContext context = createSearchExecutionContext(); @@ -169,8 +170,7 @@ public void testIllegalValues() throws IOException { public void testToQueryInnerQueryType() throws IOException { SearchExecutionContext searchExecutionContext = createSearchExecutionContext(); - HasParentQueryBuilder hasParentQueryBuilder = new HasParentQueryBuilder(PARENT_DOC, new IdsQueryBuilder().addIds("id"), - false); + HasParentQueryBuilder hasParentQueryBuilder = new HasParentQueryBuilder(PARENT_DOC, new IdsQueryBuilder().addIds("id"), false); Query query = hasParentQueryBuilder.toQuery(searchExecutionContext); HasChildQueryBuilderTests.assertLateParsingQuery(query, PARENT_DOC, "id"); } @@ -187,23 +187,22 @@ public void testMustRewrite() throws IOException { } public void testFromJson() throws IOException { - String json = - "{\n" + - " \"has_parent\" : {\n" + - " \"query\" : {\n" + - " \"term\" : {\n" + - " \"tag\" : {\n" + - " \"value\" : \"something\",\n" + - " \"boost\" : 1.0\n" + - " }\n" + - " }\n" + - " },\n" + - " \"parent_type\" : \"blog\",\n" + - " \"score\" : true,\n" + - " \"ignore_unmapped\" : false,\n" + - " \"boost\" : 1.0\n" + - " }\n" + - "}"; + String json = "{\n" + + " \"has_parent\" : {\n" + + " \"query\" : {\n" + + " \"term\" : {\n" + + " \"tag\" : {\n" + + " \"value\" : \"something\",\n" + + " \"boost\" : 1.0\n" + + " }\n" + + " }\n" + + " },\n" + + " \"parent_type\" : \"blog\",\n" + + " \"score\" : true,\n" + + " \"ignore_unmapped\" : false,\n" + + " \"boost\" : 1.0\n" + + " }\n" + + "}"; HasParentQueryBuilder parsed = (HasParentQueryBuilder) parseQuery(json); checkGeneratedJson(json, parsed); assertEquals(json, "blog", parsed.type()); @@ -226,14 +225,16 @@ public void testIgnoreUnmapped() throws IOException { failingQueryBuilder.ignoreUnmapped(false); assertFalse(failingQueryBuilder.innerHit().isIgnoreUnmapped()); QueryShardException e = expectThrows(QueryShardException.class, () -> failingQueryBuilder.toQuery(createSearchExecutionContext())); - assertThat(e.getMessage(), - containsString("[has_parent] join field [join_field] doesn't hold [unmapped] as a parent")); + assertThat(e.getMessage(), containsString("[has_parent] join field [join_field] doesn't hold [unmapped] as a parent")); } public void testIgnoreUnmappedWithRewrite() throws IOException { // WrapperQueryBuilder makes sure we always rewrite - final HasParentQueryBuilder queryBuilder = - new HasParentQueryBuilder("unmapped", new WrapperQueryBuilder(new MatchAllQueryBuilder().toString()), false); + final HasParentQueryBuilder queryBuilder = new HasParentQueryBuilder( + "unmapped", + new WrapperQueryBuilder(new MatchAllQueryBuilder().toString()), + false + ); queryBuilder.ignoreUnmapped(true); SearchExecutionContext searchExecutionContext = createSearchExecutionContext(); Query query = queryBuilder.rewrite(searchExecutionContext).toQuery(searchExecutionContext); @@ -242,11 +243,16 @@ public void testIgnoreUnmappedWithRewrite() throws IOException { } public void testExtractInnerHitBuildersWithDuplicate() { - final HasParentQueryBuilder queryBuilder - = new HasParentQueryBuilder(CHILD_DOC, new WrapperQueryBuilder(new MatchAllQueryBuilder().toString()), false); + final HasParentQueryBuilder queryBuilder = new HasParentQueryBuilder( + CHILD_DOC, + new WrapperQueryBuilder(new MatchAllQueryBuilder().toString()), + false + ); queryBuilder.innerHit(new InnerHitBuilder("some_name")); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> InnerHitContextBuilder.extractInnerHits(queryBuilder, Collections.singletonMap("some_name", null))); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> InnerHitContextBuilder.extractInnerHits(queryBuilder, Collections.singletonMap("some_name", null)) + ); assertEquals("[inner_hits] already contains an entry for key [some_name]", e.getMessage()); } @@ -255,10 +261,11 @@ public void testDisallowExpensiveQueries() { when(searchExecutionContext.allowExpensiveQueries()).thenReturn(false); HasParentQueryBuilder queryBuilder = new HasParentQueryBuilder( - CHILD_DOC, new WrapperQueryBuilder(new MatchAllQueryBuilder().toString()), false); - ElasticsearchException e = expectThrows(ElasticsearchException.class, - () -> queryBuilder.toQuery(searchExecutionContext)); - assertEquals("[joining] queries cannot be executed when 'search.allow_expensive_queries' is set to false.", - e.getMessage()); + CHILD_DOC, + new WrapperQueryBuilder(new MatchAllQueryBuilder().toString()), + false + ); + ElasticsearchException e = expectThrows(ElasticsearchException.class, () -> queryBuilder.toQuery(searchExecutionContext)); + assertEquals("[joining] queries cannot be executed when 'search.allow_expensive_queries' is set to false.", e.getMessage()); } } diff --git a/modules/parent-join/src/test/java/org/elasticsearch/join/query/ParentIdQueryBuilderTests.java b/modules/parent-join/src/test/java/org/elasticsearch/join/query/ParentIdQueryBuilderTests.java index 7ed888f8300e2..eb3245a816391 100644 --- a/modules/parent-join/src/test/java/org/elasticsearch/join/query/ParentIdQueryBuilderTests.java +++ b/modules/parent-join/src/test/java/org/elasticsearch/join/query/ParentIdQueryBuilderTests.java @@ -17,14 +17,14 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.compress.CompressedXContent; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.index.mapper.MapperService; -import org.elasticsearch.index.query.SearchExecutionContext; import org.elasticsearch.index.query.QueryShardException; +import org.elasticsearch.index.query.SearchExecutionContext; import org.elasticsearch.join.ParentJoinPlugin; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.test.AbstractQueryTestCase; import org.elasticsearch.test.TestGeoShapeFieldMapperPlugin; +import org.elasticsearch.xcontent.XContentBuilder; import org.hamcrest.Matchers; import java.io.IOException; @@ -53,38 +53,41 @@ protected Collection> getPlugins() { @Override protected void initializeAdditionalMappings(MapperService mapperService) throws IOException { - XContentBuilder mapping = jsonBuilder().startObject().startObject("_doc").startObject("properties") + XContentBuilder mapping = jsonBuilder().startObject() + .startObject("_doc") + .startObject("properties") .startObject("join_field") - .field("type", "join") - .startObject("relations") - .field("parent", "child") - .endObject() + .field("type", "join") + .startObject("relations") + .field("parent", "child") + .endObject() .endObject() .startObject(TEXT_FIELD_NAME) - .field("type", "text") + .field("type", "text") .endObject() .startObject(KEYWORD_FIELD_NAME) - .field("type", "keyword") + .field("type", "keyword") .endObject() .startObject(INT_FIELD_NAME) - .field("type", "integer") + .field("type", "integer") .endObject() .startObject(DOUBLE_FIELD_NAME) - .field("type", "double") + .field("type", "double") .endObject() .startObject(BOOLEAN_FIELD_NAME) - .field("type", "boolean") + .field("type", "boolean") .endObject() .startObject(DATE_FIELD_NAME) - .field("type", "date") + .field("type", "date") .endObject() .startObject(OBJECT_FIELD_NAME) - .field("type", "object") + .field("type", "object") + .endObject() + .endObject() .endObject() - .endObject().endObject().endObject(); + .endObject(); - mapperService.merge(TYPE, - new CompressedXContent(Strings.toString(mapping)), MapperService.MergeReason.MAPPING_UPDATE); + mapperService.merge(TYPE, new CompressedXContent(Strings.toString(mapping)), MapperService.MergeReason.MAPPING_UPDATE); } @Override @@ -97,24 +100,23 @@ protected void doAssertLuceneQuery(ParentIdQueryBuilder queryBuilder, Query quer assertThat(query, Matchers.instanceOf(BooleanQuery.class)); BooleanQuery booleanQuery = (BooleanQuery) query; assertThat(booleanQuery.clauses().size(), Matchers.equalTo(2)); - BooleanQuery expected = new BooleanQuery.Builder() - .add(new TermQuery(new Term(JOIN_FIELD_NAME + "#" + PARENT_NAME, queryBuilder.getId())), BooleanClause.Occur.MUST) - .add(new TermQuery(new Term(JOIN_FIELD_NAME, queryBuilder.getType())), BooleanClause.Occur.FILTER) - .build(); + BooleanQuery expected = new BooleanQuery.Builder().add( + new TermQuery(new Term(JOIN_FIELD_NAME + "#" + PARENT_NAME, queryBuilder.getId())), + BooleanClause.Occur.MUST + ).add(new TermQuery(new Term(JOIN_FIELD_NAME, queryBuilder.getType())), BooleanClause.Occur.FILTER).build(); assertThat(expected, equalTo(query)); } public void testFromJson() throws IOException { - String query = - "{\n" + - " \"parent_id\" : {\n" + - " \"type\" : \"child\",\n" + - " \"id\" : \"123\",\n" + - " \"ignore_unmapped\" : false,\n" + - " \"boost\" : 3.0,\n" + - " \"_name\" : \"name\"" + - " }\n" + - "}"; + String query = "{\n" + + " \"parent_id\" : {\n" + + " \"type\" : \"child\",\n" + + " \"id\" : \"123\",\n" + + " \"ignore_unmapped\" : false,\n" + + " \"boost\" : 3.0,\n" + + " \"_name\" : \"name\"" + + " }\n" + + "}"; ParentIdQueryBuilder queryBuilder = (ParentIdQueryBuilder) parseQuery(query); checkGeneratedJson(query, queryBuilder); assertThat(queryBuilder.getType(), Matchers.equalTo("child")); @@ -141,9 +143,7 @@ public void testDisallowExpensiveQueries() { when(searchExecutionContext.allowExpensiveQueries()).thenReturn(false); ParentIdQueryBuilder queryBuilder = doCreateTestQueryBuilder(); - ElasticsearchException e = expectThrows(ElasticsearchException.class, - () -> queryBuilder.toQuery(searchExecutionContext)); - assertEquals("[joining] queries cannot be executed when 'search.allow_expensive_queries' is set to false.", - e.getMessage()); + ElasticsearchException e = expectThrows(ElasticsearchException.class, () -> queryBuilder.toQuery(searchExecutionContext)); + assertEquals("[joining] queries cannot be executed when 'search.allow_expensive_queries' is set to false.", e.getMessage()); } } diff --git a/modules/percolator/src/internalClusterTest/java/org/elasticsearch/percolator/PercolatorQuerySearchIT.java b/modules/percolator/src/internalClusterTest/java/org/elasticsearch/percolator/PercolatorQuerySearchIT.java index c2150b7e96c75..6f64e697e907e 100644 --- a/modules/percolator/src/internalClusterTest/java/org/elasticsearch/percolator/PercolatorQuerySearchIT.java +++ b/modules/percolator/src/internalClusterTest/java/org/elasticsearch/percolator/PercolatorQuerySearchIT.java @@ -17,9 +17,6 @@ import org.elasticsearch.common.geo.GeoPoint; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.DistanceUnit; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.mapper.MapperParsingException; import org.elasticsearch.index.query.MatchPhraseQueryBuilder; import org.elasticsearch.index.query.MultiMatchQueryBuilder; @@ -30,14 +27,15 @@ import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.test.ESIntegTestCase; import org.elasticsearch.test.TestGeoShapeFieldMapperPlugin; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentType; import java.io.IOException; import java.util.Arrays; import java.util.Collection; import java.util.Collections; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; -import static org.elasticsearch.xcontent.XContentFactory.yamlBuilder; import static org.elasticsearch.index.query.QueryBuilders.boolQuery; import static org.elasticsearch.index.query.QueryBuilders.constantScoreQuery; import static org.elasticsearch.index.query.QueryBuilders.geoBoundingBoxQuery; @@ -54,6 +52,8 @@ import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; +import static org.elasticsearch.xcontent.XContentFactory.yamlBuilder; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.instanceOf; @@ -73,43 +73,44 @@ protected Collection> nodePlugins() { } public void testPercolatorQuery() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test") + assertAcked( + client().admin() + .indices() + .prepareCreate("test") .setMapping("id", "type=keyword", "field1", "type=keyword", "field2", "type=keyword", "query", "type=percolator") ); - client().prepareIndex("test").setId("1") - .setSource(jsonBuilder().startObject() - .field("id", "1") - .field("query", matchAllQuery()).endObject()) - .get(); - client().prepareIndex("test").setId("2") - .setSource(jsonBuilder().startObject() - .field("id", "2") - .field("query", matchQuery("field1", "value")).endObject()) - .get(); - client().prepareIndex("test").setId("3") - .setSource(jsonBuilder().startObject() + client().prepareIndex("test") + .setId("1") + .setSource(jsonBuilder().startObject().field("id", "1").field("query", matchAllQuery()).endObject()) + .get(); + client().prepareIndex("test") + .setId("2") + .setSource(jsonBuilder().startObject().field("id", "2").field("query", matchQuery("field1", "value")).endObject()) + .get(); + client().prepareIndex("test") + .setId("3") + .setSource( + jsonBuilder().startObject() .field("id", "3") - .field("query", boolQuery() - .must(matchQuery("field1", "value")) - .must(matchQuery("field2", "value")) - ).endObject()).get(); + .field("query", boolQuery().must(matchQuery("field1", "value")).must(matchQuery("field2", "value"))) + .endObject() + ) + .get(); client().admin().indices().prepareRefresh().get(); BytesReference source = BytesReference.bytes(jsonBuilder().startObject().endObject()); logger.info("percolating empty doc"); - SearchResponse response = client().prepareSearch() - .setQuery(new PercolateQueryBuilder("query", source, XContentType.JSON)) - .get(); + SearchResponse response = client().prepareSearch().setQuery(new PercolateQueryBuilder("query", source, XContentType.JSON)).get(); assertHitCount(response, 1); assertThat(response.getHits().getAt(0).getId(), equalTo("1")); source = BytesReference.bytes(jsonBuilder().startObject().field("field1", "value").endObject()); logger.info("percolating doc with 1 field"); response = client().prepareSearch() - .setQuery(new PercolateQueryBuilder("query", source, XContentType.JSON)) - .addSort("id", SortOrder.ASC) - .get(); + .setQuery(new PercolateQueryBuilder("query", source, XContentType.JSON)) + .addSort("id", SortOrder.ASC) + .get(); assertHitCount(response, 2); assertThat(response.getHits().getAt(0).getId(), equalTo("1")); assertThat(response.getHits().getAt(0).getFields().get("_percolator_document_slot").getValue(), equalTo(0)); @@ -119,9 +120,9 @@ public void testPercolatorQuery() throws Exception { source = BytesReference.bytes(jsonBuilder().startObject().field("field1", "value").field("field2", "value").endObject()); logger.info("percolating doc with 2 fields"); response = client().prepareSearch() - .setQuery(new PercolateQueryBuilder("query", source, XContentType.JSON)) - .addSort("id", SortOrder.ASC) - .get(); + .setQuery(new PercolateQueryBuilder("query", source, XContentType.JSON)) + .addSort("id", SortOrder.ASC) + .get(); assertHitCount(response, 3); assertThat(response.getHits().getAt(0).getId(), equalTo("1")); assertThat(response.getHits().getAt(0).getFields().get("_percolator_document_slot").getValue(), equalTo(0)); @@ -132,12 +133,18 @@ public void testPercolatorQuery() throws Exception { logger.info("percolating doc with 2 fields"); response = client().prepareSearch() - .setQuery(new PercolateQueryBuilder("query", Arrays.asList( + .setQuery( + new PercolateQueryBuilder( + "query", + Arrays.asList( BytesReference.bytes(jsonBuilder().startObject().field("field1", "value").endObject()), BytesReference.bytes(jsonBuilder().startObject().field("field1", "value").field("field2", "value").endObject()) - ), XContentType.JSON)) - .addSort("id", SortOrder.ASC) - .get(); + ), + XContentType.JSON + ) + ) + .addSort("id", SortOrder.ASC) + .get(); assertHitCount(response, 3); assertThat(response.getHits().getAt(0).getId(), equalTo("1")); assertThat(response.getHits().getAt(0).getFields().get("_percolator_document_slot").getValues(), equalTo(Arrays.asList(0, 1))); @@ -148,143 +155,181 @@ public void testPercolatorQuery() throws Exception { } public void testPercolatorRangeQueries() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test") - .setMapping("field1", "type=long", "field2", "type=double", "field3", "type=ip", "field4", "type=date", - "query", "type=percolator") + assertAcked( + client().admin() + .indices() + .prepareCreate("test") + .setMapping( + "field1", + "type=long", + "field2", + "type=double", + "field3", + "type=ip", + "field4", + "type=date", + "query", + "type=percolator" + ) ); - client().prepareIndex("test").setId("1") - .setSource(jsonBuilder().startObject().field("query", rangeQuery("field1").from(10).to(12)).endObject()) - .get(); - client().prepareIndex("test").setId("2") - .setSource(jsonBuilder().startObject().field("query", rangeQuery("field1").from(20).to(22)).endObject()) - .get(); - client().prepareIndex("test").setId("3") - .setSource(jsonBuilder().startObject().field("query", boolQuery() - .must(rangeQuery("field1").from(10).to(12)) - .must(rangeQuery("field1").from(12).to(14)) - ).endObject()).get(); + client().prepareIndex("test") + .setId("1") + .setSource(jsonBuilder().startObject().field("query", rangeQuery("field1").from(10).to(12)).endObject()) + .get(); + client().prepareIndex("test") + .setId("2") + .setSource(jsonBuilder().startObject().field("query", rangeQuery("field1").from(20).to(22)).endObject()) + .get(); + client().prepareIndex("test") + .setId("3") + .setSource( + jsonBuilder().startObject() + .field("query", boolQuery().must(rangeQuery("field1").from(10).to(12)).must(rangeQuery("field1").from(12).to(14))) + .endObject() + ) + .get(); client().admin().indices().prepareRefresh().get(); - client().prepareIndex("test").setId("4") - .setSource(jsonBuilder().startObject().field("query", rangeQuery("field2").from(10).to(12)).endObject()) - .get(); - client().prepareIndex("test").setId("5") - .setSource(jsonBuilder().startObject().field("query", rangeQuery("field2").from(20).to(22)).endObject()) - .get(); - client().prepareIndex("test").setId("6") - .setSource(jsonBuilder().startObject().field("query", boolQuery() - .must(rangeQuery("field2").from(10).to(12)) - .must(rangeQuery("field2").from(12).to(14)) - ).endObject()).get(); + client().prepareIndex("test") + .setId("4") + .setSource(jsonBuilder().startObject().field("query", rangeQuery("field2").from(10).to(12)).endObject()) + .get(); + client().prepareIndex("test") + .setId("5") + .setSource(jsonBuilder().startObject().field("query", rangeQuery("field2").from(20).to(22)).endObject()) + .get(); + client().prepareIndex("test") + .setId("6") + .setSource( + jsonBuilder().startObject() + .field("query", boolQuery().must(rangeQuery("field2").from(10).to(12)).must(rangeQuery("field2").from(12).to(14))) + .endObject() + ) + .get(); client().admin().indices().prepareRefresh().get(); - client().prepareIndex("test").setId("7") - .setSource(jsonBuilder().startObject() - .field("query", rangeQuery("field3").from("192.168.1.0").to("192.168.1.5")) - .endObject()) - .get(); - client().prepareIndex("test").setId("8") - .setSource(jsonBuilder().startObject() - .field("query", rangeQuery("field3").from("192.168.1.20").to("192.168.1.30")) - .endObject()) - .get(); - client().prepareIndex("test").setId("9") - .setSource(jsonBuilder().startObject().field("query", boolQuery() - .must(rangeQuery("field3").from("192.168.1.0").to("192.168.1.5")) - .must(rangeQuery("field3").from("192.168.1.5").to("192.168.1.10")) - ).endObject()).get(); - client().prepareIndex("test").setId("10") - .setSource(jsonBuilder().startObject().field("query", boolQuery() - .must(rangeQuery("field4").from("2010-01-01").to("2018-01-01")) - .must(rangeQuery("field4").from("2010-01-01").to("now")) - ).endObject()).get(); + client().prepareIndex("test") + .setId("7") + .setSource(jsonBuilder().startObject().field("query", rangeQuery("field3").from("192.168.1.0").to("192.168.1.5")).endObject()) + .get(); + client().prepareIndex("test") + .setId("8") + .setSource(jsonBuilder().startObject().field("query", rangeQuery("field3").from("192.168.1.20").to("192.168.1.30")).endObject()) + .get(); + client().prepareIndex("test") + .setId("9") + .setSource( + jsonBuilder().startObject() + .field( + "query", + boolQuery().must(rangeQuery("field3").from("192.168.1.0").to("192.168.1.5")) + .must(rangeQuery("field3").from("192.168.1.5").to("192.168.1.10")) + ) + .endObject() + ) + .get(); + client().prepareIndex("test") + .setId("10") + .setSource( + jsonBuilder().startObject() + .field( + "query", + boolQuery().must(rangeQuery("field4").from("2010-01-01").to("2018-01-01")) + .must(rangeQuery("field4").from("2010-01-01").to("now")) + ) + .endObject() + ) + .get(); client().admin().indices().prepareRefresh().get(); // Test long range: BytesReference source = BytesReference.bytes(jsonBuilder().startObject().field("field1", 12).endObject()); - SearchResponse response = client().prepareSearch() - .setQuery(new PercolateQueryBuilder("query", source, XContentType.JSON)) - .get(); + SearchResponse response = client().prepareSearch().setQuery(new PercolateQueryBuilder("query", source, XContentType.JSON)).get(); logger.info("response={}", response); assertHitCount(response, 2); assertThat(response.getHits().getAt(0).getId(), equalTo("3")); assertThat(response.getHits().getAt(1).getId(), equalTo("1")); source = BytesReference.bytes(jsonBuilder().startObject().field("field1", 11).endObject()); - response = client().prepareSearch() - .setQuery(new PercolateQueryBuilder("query", source, XContentType.JSON)) - .get(); + response = client().prepareSearch().setQuery(new PercolateQueryBuilder("query", source, XContentType.JSON)).get(); assertHitCount(response, 1); assertThat(response.getHits().getAt(0).getId(), equalTo("1")); // Test double range: source = BytesReference.bytes(jsonBuilder().startObject().field("field2", 12).endObject()); - response = client().prepareSearch() - .setQuery(new PercolateQueryBuilder("query", source, XContentType.JSON)) - .get(); + response = client().prepareSearch().setQuery(new PercolateQueryBuilder("query", source, XContentType.JSON)).get(); assertHitCount(response, 2); assertThat(response.getHits().getAt(0).getId(), equalTo("6")); assertThat(response.getHits().getAt(1).getId(), equalTo("4")); source = BytesReference.bytes(jsonBuilder().startObject().field("field2", 11).endObject()); - response = client().prepareSearch() - .setQuery(new PercolateQueryBuilder("query", source, XContentType.JSON)) - .get(); + response = client().prepareSearch().setQuery(new PercolateQueryBuilder("query", source, XContentType.JSON)).get(); assertHitCount(response, 1); assertThat(response.getHits().getAt(0).getId(), equalTo("4")); // Test IP range: source = BytesReference.bytes(jsonBuilder().startObject().field("field3", "192.168.1.5").endObject()); - response = client().prepareSearch() - .setQuery(new PercolateQueryBuilder("query", source, XContentType.JSON)) - .get(); + response = client().prepareSearch().setQuery(new PercolateQueryBuilder("query", source, XContentType.JSON)).get(); assertHitCount(response, 2); assertThat(response.getHits().getAt(0).getId(), equalTo("9")); assertThat(response.getHits().getAt(1).getId(), equalTo("7")); source = BytesReference.bytes(jsonBuilder().startObject().field("field3", "192.168.1.4").endObject()); - response = client().prepareSearch() - .setQuery(new PercolateQueryBuilder("query", source, XContentType.JSON)) - .get(); + response = client().prepareSearch().setQuery(new PercolateQueryBuilder("query", source, XContentType.JSON)).get(); assertHitCount(response, 1); assertThat(response.getHits().getAt(0).getId(), equalTo("7")); // Test date range: source = BytesReference.bytes(jsonBuilder().startObject().field("field4", "2016-05-15").endObject()); - response = client().prepareSearch() - .setQuery(new PercolateQueryBuilder("query", source, XContentType.JSON)) - .get(); + response = client().prepareSearch().setQuery(new PercolateQueryBuilder("query", source, XContentType.JSON)).get(); assertHitCount(response, 1); assertThat(response.getHits().getAt(0).getId(), equalTo("10")); } public void testPercolatorGeoQueries() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test") - .setMapping("id", "type=keyword", - "field1", "type=geo_point", "field2", "type=geo_shape", "query", "type=percolator")); - - client().prepareIndex("test").setId("1") - .setSource(jsonBuilder().startObject() - .field("query", geoDistanceQuery("field1").point(52.18, 4.38).distance(50, DistanceUnit.KILOMETERS)) - .field("id", "1") - .endObject()).get(); - - client().prepareIndex("test").setId("2") - .setSource(jsonBuilder().startObject() - .field("query", geoBoundingBoxQuery("field1").setCorners(52.3, 4.4, 52.1, 4.6)) - .field("id", "2") - .endObject()).get(); - - client().prepareIndex("test").setId("3") - .setSource(jsonBuilder().startObject() - .field("query", - geoPolygonQuery("field1", Arrays.asList(new GeoPoint(52.1, 4.4), new GeoPoint(52.3, 4.5), new GeoPoint(52.1, 4.6)))) - .field("id", "3") - .endObject()).get(); + assertAcked( + client().admin() + .indices() + .prepareCreate("test") + .setMapping("id", "type=keyword", "field1", "type=geo_point", "field2", "type=geo_shape", "query", "type=percolator") + ); + + client().prepareIndex("test") + .setId("1") + .setSource( + jsonBuilder().startObject() + .field("query", geoDistanceQuery("field1").point(52.18, 4.38).distance(50, DistanceUnit.KILOMETERS)) + .field("id", "1") + .endObject() + ) + .get(); + + client().prepareIndex("test") + .setId("2") + .setSource( + jsonBuilder().startObject() + .field("query", geoBoundingBoxQuery("field1").setCorners(52.3, 4.4, 52.1, 4.6)) + .field("id", "2") + .endObject() + ) + .get(); + + client().prepareIndex("test") + .setId("3") + .setSource( + jsonBuilder().startObject() + .field( + "query", + geoPolygonQuery("field1", Arrays.asList(new GeoPoint(52.1, 4.4), new GeoPoint(52.3, 4.5), new GeoPoint(52.1, 4.6))) + ) + .field("id", "3") + .endObject() + ) + .get(); refresh(); - BytesReference source = BytesReference.bytes(jsonBuilder().startObject() - .startObject("field1").field("lat", 52.20).field("lon", 4.51).endObject() - .endObject()); + BytesReference source = BytesReference.bytes( + jsonBuilder().startObject().startObject("field1").field("lat", 52.20).field("lon", 4.51).endObject().endObject() + ); SearchResponse response = client().prepareSearch() .setQuery(new PercolateQueryBuilder("query", source, XContentType.JSON)) .addSort("id", SortOrder.ASC) @@ -296,26 +341,30 @@ public void testPercolatorGeoQueries() throws Exception { } public void testPercolatorQueryExistingDocument() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test") + assertAcked( + client().admin() + .indices() + .prepareCreate("test") .setMapping("id", "type=keyword", "field1", "type=keyword", "field2", "type=keyword", "query", "type=percolator") ); - client().prepareIndex("test").setId("1") - .setSource(jsonBuilder().startObject() - .field("id", "1") - .field("query", matchAllQuery()).endObject()) - .get(); - client().prepareIndex("test").setId("2") - .setSource(jsonBuilder().startObject() - .field("id", "2") - .field("query", matchQuery("field1", "value")).endObject()) - .get(); - client().prepareIndex("test").setId("3") - .setSource(jsonBuilder().startObject() + client().prepareIndex("test") + .setId("1") + .setSource(jsonBuilder().startObject().field("id", "1").field("query", matchAllQuery()).endObject()) + .get(); + client().prepareIndex("test") + .setId("2") + .setSource(jsonBuilder().startObject().field("id", "2").field("query", matchQuery("field1", "value")).endObject()) + .get(); + client().prepareIndex("test") + .setId("3") + .setSource( + jsonBuilder().startObject() .field("id", "3") - .field("query", boolQuery() - .must(matchQuery("field1", "value")) - .must(matchQuery("field2", "value"))).endObject()).get(); + .field("query", boolQuery().must(matchQuery("field1", "value")).must(matchQuery("field2", "value"))) + .endObject() + ) + .get(); client().prepareIndex("test").setId("4").setSource("{\"id\": \"4\"}", XContentType.JSON).get(); client().prepareIndex("test").setId("5").setSource(XContentType.JSON, "id", "5", "field1", "value").get(); @@ -324,25 +373,25 @@ public void testPercolatorQueryExistingDocument() throws Exception { logger.info("percolating empty doc"); SearchResponse response = client().prepareSearch() - .setQuery(new PercolateQueryBuilder("query", "test", "1", null, null, null)) - .get(); + .setQuery(new PercolateQueryBuilder("query", "test", "1", null, null, null)) + .get(); assertHitCount(response, 1); assertThat(response.getHits().getAt(0).getId(), equalTo("1")); logger.info("percolating doc with 1 field"); response = client().prepareSearch() - .setQuery(new PercolateQueryBuilder("query", "test", "5", null, null, null)) - .addSort("id", SortOrder.ASC) - .get(); + .setQuery(new PercolateQueryBuilder("query", "test", "5", null, null, null)) + .addSort("id", SortOrder.ASC) + .get(); assertHitCount(response, 2); assertThat(response.getHits().getAt(0).getId(), equalTo("1")); assertThat(response.getHits().getAt(1).getId(), equalTo("2")); logger.info("percolating doc with 2 fields"); response = client().prepareSearch() - .setQuery(new PercolateQueryBuilder("query", "test", "6", null, null, null)) - .addSort("id", SortOrder.ASC) - .get(); + .setQuery(new PercolateQueryBuilder("query", "test", "6", null, null, null)) + .addSort("id", SortOrder.ASC) + .get(); assertHitCount(response, 3); assertThat(response.getHits().getAt(0).getId(), equalTo("1")); assertThat(response.getHits().getAt(1).getId(), equalTo("2")); @@ -350,91 +399,111 @@ public void testPercolatorQueryExistingDocument() throws Exception { } public void testPercolatorQueryExistingDocumentSourceDisabled() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test") - .setMapping("_source", "enabled=false", "field1", "type=keyword", "query", "type=percolator") + assertAcked( + client().admin() + .indices() + .prepareCreate("test") + .setMapping("_source", "enabled=false", "field1", "type=keyword", "query", "type=percolator") ); - client().prepareIndex("test").setId("1") - .setSource(jsonBuilder().startObject().field("query", matchAllQuery()).endObject()) - .get(); + client().prepareIndex("test").setId("1").setSource(jsonBuilder().startObject().field("query", matchAllQuery()).endObject()).get(); client().prepareIndex("test").setId("2").setSource("{}", XContentType.JSON).get(); client().admin().indices().prepareRefresh().get(); logger.info("percolating empty doc with source disabled"); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> { - client().prepareSearch() - .setQuery(new PercolateQueryBuilder("query", "test", "1", null, null, null)) - .get(); - }); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> { client().prepareSearch().setQuery(new PercolateQueryBuilder("query", "test", "1", null, null, null)).get(); } + ); assertThat(e.getMessage(), containsString("source disabled")); } - public void testPercolatorSpecificQueries() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test") + public void testPercolatorSpecificQueries() throws Exception { + assertAcked( + client().admin() + .indices() + .prepareCreate("test") .setMapping("id", "type=keyword", "field1", "type=text", "field2", "type=text", "query", "type=percolator") ); - client().prepareIndex("test").setId("1") - .setSource(jsonBuilder().startObject() + client().prepareIndex("test") + .setId("1") + .setSource( + jsonBuilder().startObject() .field("id", "1") - .field("query", multiMatchQuery("quick brown fox", "field1", "field2") - .type(MultiMatchQueryBuilder.Type.CROSS_FIELDS)).endObject()) - .get(); - client().prepareIndex("test").setId("2") - .setSource(jsonBuilder().startObject() + .field("query", multiMatchQuery("quick brown fox", "field1", "field2").type(MultiMatchQueryBuilder.Type.CROSS_FIELDS)) + .endObject() + ) + .get(); + client().prepareIndex("test") + .setId("2") + .setSource( + jsonBuilder().startObject() .field("id", "2") - .field("query", - spanNearQuery(spanTermQuery("field1", "quick"), 0) - .addClause(spanTermQuery("field1", "brown")) - .addClause(spanTermQuery("field1", "fox")) - .inOrder(true) - ).endObject()) - .get(); + .field( + "query", + spanNearQuery(spanTermQuery("field1", "quick"), 0).addClause(spanTermQuery("field1", "brown")) + .addClause(spanTermQuery("field1", "fox")) + .inOrder(true) + ) + .endObject() + ) + .get(); client().admin().indices().prepareRefresh().get(); - client().prepareIndex("test").setId("3") - .setSource(jsonBuilder().startObject() + client().prepareIndex("test") + .setId("3") + .setSource( + jsonBuilder().startObject() .field("id", "3") - .field("query", + .field( + "query", spanNotQuery( - spanNearQuery(spanTermQuery("field1", "quick"), 0) - .addClause(spanTermQuery("field1", "brown")) - .addClause(spanTermQuery("field1", "fox")) - .inOrder(true), - spanNearQuery(spanTermQuery("field1", "the"), 0) - .addClause(spanTermQuery("field1", "lazy")) - .addClause(spanTermQuery("field1", "dog")) - .inOrder(true)).dist(2) - ).endObject()) - .get(); + spanNearQuery(spanTermQuery("field1", "quick"), 0).addClause(spanTermQuery("field1", "brown")) + .addClause(spanTermQuery("field1", "fox")) + .inOrder(true), + spanNearQuery(spanTermQuery("field1", "the"), 0).addClause(spanTermQuery("field1", "lazy")) + .addClause(spanTermQuery("field1", "dog")) + .inOrder(true) + ).dist(2) + ) + .endObject() + ) + .get(); // doesn't match - client().prepareIndex("test").setId("4") - .setSource(jsonBuilder().startObject() + client().prepareIndex("test") + .setId("4") + .setSource( + jsonBuilder().startObject() .field("id", "4") - .field("query", + .field( + "query", spanNotQuery( - spanNearQuery(spanTermQuery("field1", "quick"), 0) - .addClause(spanTermQuery("field1", "brown")) - .addClause(spanTermQuery("field1", "fox")) - .inOrder(true), - spanNearQuery(spanTermQuery("field1", "the"), 0) - .addClause(spanTermQuery("field1", "lazy")) - .addClause(spanTermQuery("field1", "dog")) - .inOrder(true)).dist(3) - ).endObject()) - .get(); + spanNearQuery(spanTermQuery("field1", "quick"), 0).addClause(spanTermQuery("field1", "brown")) + .addClause(spanTermQuery("field1", "fox")) + .inOrder(true), + spanNearQuery(spanTermQuery("field1", "the"), 0).addClause(spanTermQuery("field1", "lazy")) + .addClause(spanTermQuery("field1", "dog")) + .inOrder(true) + ).dist(3) + ) + .endObject() + ) + .get(); client().admin().indices().prepareRefresh().get(); - BytesReference source = BytesReference.bytes(jsonBuilder().startObject() + BytesReference source = BytesReference.bytes( + jsonBuilder().startObject() .field("field1", "the quick brown fox jumps over the lazy dog") .field("field2", "the quick brown fox falls down into the well") - .endObject()); + .endObject() + ); SearchResponse response = client().prepareSearch() - .setQuery(new PercolateQueryBuilder("query", source, XContentType.JSON)) - .addSort("id", SortOrder.ASC) - .get(); + .setQuery(new PercolateQueryBuilder("query", source, XContentType.JSON)) + .addSort("id", SortOrder.ASC) + .get(); assertHitCount(response, 3); assertThat(response.getHits().getAt(0).getId(), equalTo("1")); assertThat(response.getHits().getAt(0).getScore(), equalTo(Float.NaN)); @@ -445,74 +514,84 @@ public void testPercolatorSpecificQueries() throws Exception { } public void testPercolatorQueryWithHighlighting() throws Exception { - StringBuilder fieldMapping = new StringBuilder("type=text") - .append(",store=").append(randomBoolean()); + StringBuilder fieldMapping = new StringBuilder("type=text").append(",store=").append(randomBoolean()); if (randomBoolean()) { fieldMapping.append(",term_vector=with_positions_offsets"); } else if (randomBoolean()) { fieldMapping.append(",index_options=offsets"); } - assertAcked(client().admin().indices().prepareCreate("test") + assertAcked( + client().admin() + .indices() + .prepareCreate("test") .setMapping("id", "type=keyword", "field1", fieldMapping.toString(), "query", "type=percolator") ); - client().prepareIndex("test").setId("1") - .setSource(jsonBuilder().startObject() - .field("id", "1") - .field("query", matchQuery("field1", "brown fox")).endObject()) - .execute().actionGet(); - client().prepareIndex("test").setId("2") - .setSource(jsonBuilder().startObject() - .field("id", "2") - .field("query", matchQuery("field1", "lazy dog")).endObject()) - .execute().actionGet(); - client().prepareIndex("test").setId("3") - .setSource(jsonBuilder().startObject() - .field("id", "3") - .field("query", termQuery("field1", "jumps")).endObject()) - .execute().actionGet(); - client().prepareIndex("test").setId("4") - .setSource(jsonBuilder().startObject() - .field("id", "4") - .field("query", termQuery("field1", "dog")).endObject()) - .execute().actionGet(); - client().prepareIndex("test").setId("5") - .setSource(jsonBuilder().startObject() - .field("id", "5") - .field("query", termQuery("field1", "fox")).endObject()) - .execute().actionGet(); + client().prepareIndex("test") + .setId("1") + .setSource(jsonBuilder().startObject().field("id", "1").field("query", matchQuery("field1", "brown fox")).endObject()) + .execute() + .actionGet(); + client().prepareIndex("test") + .setId("2") + .setSource(jsonBuilder().startObject().field("id", "2").field("query", matchQuery("field1", "lazy dog")).endObject()) + .execute() + .actionGet(); + client().prepareIndex("test") + .setId("3") + .setSource(jsonBuilder().startObject().field("id", "3").field("query", termQuery("field1", "jumps")).endObject()) + .execute() + .actionGet(); + client().prepareIndex("test") + .setId("4") + .setSource(jsonBuilder().startObject().field("id", "4").field("query", termQuery("field1", "dog")).endObject()) + .execute() + .actionGet(); + client().prepareIndex("test") + .setId("5") + .setSource(jsonBuilder().startObject().field("id", "5").field("query", termQuery("field1", "fox")).endObject()) + .execute() + .actionGet(); client().admin().indices().prepareRefresh().get(); - BytesReference document = BytesReference.bytes(jsonBuilder().startObject() - .field("field1", "The quick brown fox jumps over the lazy dog") - .endObject()); + BytesReference document = BytesReference.bytes( + jsonBuilder().startObject().field("field1", "The quick brown fox jumps over the lazy dog").endObject() + ); SearchResponse searchResponse = client().prepareSearch() - .setQuery(new PercolateQueryBuilder("query", document, XContentType.JSON)) - .highlighter(new HighlightBuilder().field("field1")) - .addSort("id", SortOrder.ASC) - .get(); + .setQuery(new PercolateQueryBuilder("query", document, XContentType.JSON)) + .highlighter(new HighlightBuilder().field("field1")) + .addSort("id", SortOrder.ASC) + .get(); assertHitCount(searchResponse, 5); - assertThat(searchResponse.getHits().getAt(0).getHighlightFields().get("field1").fragments()[0].string(), - equalTo("The quick brown fox jumps over the lazy dog")); - assertThat(searchResponse.getHits().getAt(1).getHighlightFields().get("field1").fragments()[0].string(), - equalTo("The quick brown fox jumps over the lazy dog")); - assertThat(searchResponse.getHits().getAt(2).getHighlightFields().get("field1").fragments()[0].string(), - equalTo("The quick brown fox jumps over the lazy dog")); - assertThat(searchResponse.getHits().getAt(3).getHighlightFields().get("field1").fragments()[0].string(), - equalTo("The quick brown fox jumps over the lazy dog")); - assertThat(searchResponse.getHits().getAt(4).getHighlightFields().get("field1").fragments()[0].string(), - equalTo("The quick brown fox jumps over the lazy dog")); - - BytesReference document1 = BytesReference.bytes(jsonBuilder().startObject() - .field("field1", "The quick brown fox jumps") - .endObject()); - BytesReference document2 = BytesReference.bytes(jsonBuilder().startObject() - .field("field1", "over the lazy dog") - .endObject()); + assertThat( + searchResponse.getHits().getAt(0).getHighlightFields().get("field1").fragments()[0].string(), + equalTo("The quick brown fox jumps over the lazy dog") + ); + assertThat( + searchResponse.getHits().getAt(1).getHighlightFields().get("field1").fragments()[0].string(), + equalTo("The quick brown fox jumps over the lazy dog") + ); + assertThat( + searchResponse.getHits().getAt(2).getHighlightFields().get("field1").fragments()[0].string(), + equalTo("The quick brown fox jumps over the lazy dog") + ); + assertThat( + searchResponse.getHits().getAt(3).getHighlightFields().get("field1").fragments()[0].string(), + equalTo("The quick brown fox jumps over the lazy dog") + ); + assertThat( + searchResponse.getHits().getAt(4).getHighlightFields().get("field1").fragments()[0].string(), + equalTo("The quick brown fox jumps over the lazy dog") + ); + + BytesReference document1 = BytesReference.bytes( + jsonBuilder().startObject().field("field1", "The quick brown fox jumps").endObject() + ); + BytesReference document2 = BytesReference.bytes(jsonBuilder().startObject().field("field1", "over the lazy dog").endObject()); searchResponse = client().prepareSearch() - .setQuery(boolQuery() - .should(new PercolateQueryBuilder("query", document1, XContentType.JSON).setName("query1")) - .should(new PercolateQueryBuilder("query", document2, XContentType.JSON).setName("query2")) + .setQuery( + boolQuery().should(new PercolateQueryBuilder("query", document1, XContentType.JSON).setName("query1")) + .should(new PercolateQueryBuilder("query", document2, XContentType.JSON).setName("query2")) ) .highlighter(new HighlightBuilder().field("field1")) .addSort("id", SortOrder.ASC) @@ -520,157 +599,245 @@ public void testPercolatorQueryWithHighlighting() throws Exception { logger.info("searchResponse={}", searchResponse); assertHitCount(searchResponse, 5); - assertThat(searchResponse.getHits().getAt(0).getHighlightFields().get("query1_field1").fragments()[0].string(), - equalTo("The quick brown fox jumps")); - assertThat(searchResponse.getHits().getAt(1).getHighlightFields().get("query2_field1").fragments()[0].string(), - equalTo("over the lazy dog")); - assertThat(searchResponse.getHits().getAt(2).getHighlightFields().get("query1_field1").fragments()[0].string(), - equalTo("The quick brown fox jumps")); - assertThat(searchResponse.getHits().getAt(3).getHighlightFields().get("query2_field1").fragments()[0].string(), - equalTo("over the lazy dog")); - assertThat(searchResponse.getHits().getAt(4).getHighlightFields().get("query1_field1").fragments()[0].string(), - equalTo("The quick brown fox jumps")); + assertThat( + searchResponse.getHits().getAt(0).getHighlightFields().get("query1_field1").fragments()[0].string(), + equalTo("The quick brown fox jumps") + ); + assertThat( + searchResponse.getHits().getAt(1).getHighlightFields().get("query2_field1").fragments()[0].string(), + equalTo("over the lazy dog") + ); + assertThat( + searchResponse.getHits().getAt(2).getHighlightFields().get("query1_field1").fragments()[0].string(), + equalTo("The quick brown fox jumps") + ); + assertThat( + searchResponse.getHits().getAt(3).getHighlightFields().get("query2_field1").fragments()[0].string(), + equalTo("over the lazy dog") + ); + assertThat( + searchResponse.getHits().getAt(4).getHighlightFields().get("query1_field1").fragments()[0].string(), + equalTo("The quick brown fox jumps") + ); searchResponse = client().prepareSearch() - .setQuery(new PercolateQueryBuilder("query", Arrays.asList( + .setQuery( + new PercolateQueryBuilder( + "query", + Arrays.asList( BytesReference.bytes(jsonBuilder().startObject().field("field1", "dog").endObject()), BytesReference.bytes(jsonBuilder().startObject().field("field1", "fox").endObject()), BytesReference.bytes(jsonBuilder().startObject().field("field1", "jumps").endObject()), BytesReference.bytes(jsonBuilder().startObject().field("field1", "brown fox").endObject()) - ), XContentType.JSON)) - .highlighter(new HighlightBuilder().field("field1")) - .addSort("id", SortOrder.ASC) - .get(); + ), + XContentType.JSON + ) + ) + .highlighter(new HighlightBuilder().field("field1")) + .addSort("id", SortOrder.ASC) + .get(); assertHitCount(searchResponse, 5); - assertThat(searchResponse.getHits().getAt(0).getFields().get("_percolator_document_slot").getValues(), - equalTo(Arrays.asList(1, 3))); - assertThat(searchResponse.getHits().getAt(0).getHighlightFields().get("1_field1").fragments()[0].string(), - equalTo("fox")); - assertThat(searchResponse.getHits().getAt(0).getHighlightFields().get("3_field1").fragments()[0].string(), - equalTo("brown fox")); - assertThat(searchResponse.getHits().getAt(1).getFields().get("_percolator_document_slot").getValues(), - equalTo(Collections.singletonList(0))); - assertThat(searchResponse.getHits().getAt(1).getHighlightFields().get("0_field1").fragments()[0].string(), - equalTo("dog")); - assertThat(searchResponse.getHits().getAt(2).getFields().get("_percolator_document_slot").getValues(), - equalTo(Collections.singletonList(2))); - assertThat(searchResponse.getHits().getAt(2).getHighlightFields().get("2_field1").fragments()[0].string(), - equalTo("jumps")); - assertThat(searchResponse.getHits().getAt(3).getFields().get("_percolator_document_slot").getValues(), - equalTo(Collections.singletonList(0))); - assertThat(searchResponse.getHits().getAt(3).getHighlightFields().get("0_field1").fragments()[0].string(), - equalTo("dog")); - assertThat(searchResponse.getHits().getAt(4).getFields().get("_percolator_document_slot").getValues(), - equalTo(Arrays.asList(1, 3))); - assertThat(searchResponse.getHits().getAt(4).getHighlightFields().get("1_field1").fragments()[0].string(), - equalTo("fox")); - assertThat(searchResponse.getHits().getAt(4).getHighlightFields().get("3_field1").fragments()[0].string(), - equalTo("brown fox")); + assertThat( + searchResponse.getHits().getAt(0).getFields().get("_percolator_document_slot").getValues(), + equalTo(Arrays.asList(1, 3)) + ); + assertThat(searchResponse.getHits().getAt(0).getHighlightFields().get("1_field1").fragments()[0].string(), equalTo("fox")); + assertThat( + searchResponse.getHits().getAt(0).getHighlightFields().get("3_field1").fragments()[0].string(), + equalTo("brown fox") + ); + assertThat( + searchResponse.getHits().getAt(1).getFields().get("_percolator_document_slot").getValues(), + equalTo(Collections.singletonList(0)) + ); + assertThat(searchResponse.getHits().getAt(1).getHighlightFields().get("0_field1").fragments()[0].string(), equalTo("dog")); + assertThat( + searchResponse.getHits().getAt(2).getFields().get("_percolator_document_slot").getValues(), + equalTo(Collections.singletonList(2)) + ); + assertThat( + searchResponse.getHits().getAt(2).getHighlightFields().get("2_field1").fragments()[0].string(), + equalTo("jumps") + ); + assertThat( + searchResponse.getHits().getAt(3).getFields().get("_percolator_document_slot").getValues(), + equalTo(Collections.singletonList(0)) + ); + assertThat(searchResponse.getHits().getAt(3).getHighlightFields().get("0_field1").fragments()[0].string(), equalTo("dog")); + assertThat( + searchResponse.getHits().getAt(4).getFields().get("_percolator_document_slot").getValues(), + equalTo(Arrays.asList(1, 3)) + ); + assertThat(searchResponse.getHits().getAt(4).getHighlightFields().get("1_field1").fragments()[0].string(), equalTo("fox")); + assertThat( + searchResponse.getHits().getAt(4).getHighlightFields().get("3_field1").fragments()[0].string(), + equalTo("brown fox") + ); searchResponse = client().prepareSearch() - .setQuery(boolQuery() - .should(new PercolateQueryBuilder("query", Arrays.asList( - BytesReference.bytes(jsonBuilder().startObject().field("field1", "dog").endObject()), - BytesReference.bytes(jsonBuilder().startObject().field("field1", "fox").endObject()) - ), XContentType.JSON).setName("query1")) - .should(new PercolateQueryBuilder("query", Arrays.asList( - BytesReference.bytes(jsonBuilder().startObject().field("field1", "jumps").endObject()), - BytesReference.bytes(jsonBuilder().startObject().field("field1", "brown fox").endObject()) - ), XContentType.JSON).setName("query2")) + .setQuery( + boolQuery().should( + new PercolateQueryBuilder( + "query", + Arrays.asList( + BytesReference.bytes(jsonBuilder().startObject().field("field1", "dog").endObject()), + BytesReference.bytes(jsonBuilder().startObject().field("field1", "fox").endObject()) + ), + XContentType.JSON + ).setName("query1") + ) + .should( + new PercolateQueryBuilder( + "query", + Arrays.asList( + BytesReference.bytes(jsonBuilder().startObject().field("field1", "jumps").endObject()), + BytesReference.bytes(jsonBuilder().startObject().field("field1", "brown fox").endObject()) + ), + XContentType.JSON + ).setName("query2") + ) ) .highlighter(new HighlightBuilder().field("field1")) .addSort("id", SortOrder.ASC) .get(); logger.info("searchResponse={}", searchResponse); assertHitCount(searchResponse, 5); - assertThat(searchResponse.getHits().getAt(0).getFields().get("_percolator_document_slot_query1").getValues(), - equalTo(Collections.singletonList(1))); - assertThat(searchResponse.getHits().getAt(0).getFields().get("_percolator_document_slot_query2").getValues(), - equalTo(Collections.singletonList(1))); - assertThat(searchResponse.getHits().getAt(0).getHighlightFields().get("query1_1_field1").fragments()[0].string(), - equalTo("fox")); - assertThat(searchResponse.getHits().getAt(0).getHighlightFields().get("query2_1_field1").fragments()[0].string(), - equalTo("brown fox")); - - assertThat(searchResponse.getHits().getAt(1).getFields().get("_percolator_document_slot_query1").getValues(), - equalTo(Collections.singletonList(0))); - assertThat(searchResponse.getHits().getAt(1).getHighlightFields().get("query1_0_field1").fragments()[0].string(), - equalTo("dog")); - - assertThat(searchResponse.getHits().getAt(2).getFields().get("_percolator_document_slot_query2").getValues(), - equalTo(Collections.singletonList(0))); - assertThat(searchResponse.getHits().getAt(2).getHighlightFields().get("query2_0_field1").fragments()[0].string(), - equalTo("jumps")); - - assertThat(searchResponse.getHits().getAt(3).getFields().get("_percolator_document_slot_query1").getValues(), - equalTo(Collections.singletonList(0))); - assertThat(searchResponse.getHits().getAt(3).getHighlightFields().get("query1_0_field1").fragments()[0].string(), - equalTo("dog")); - - assertThat(searchResponse.getHits().getAt(4).getFields().get("_percolator_document_slot_query1").getValues(), - equalTo(Collections.singletonList(1))); - assertThat(searchResponse.getHits().getAt(4).getFields().get("_percolator_document_slot_query2").getValues(), - equalTo(Collections.singletonList(1))); - assertThat(searchResponse.getHits().getAt(4).getHighlightFields().get("query1_1_field1").fragments()[0].string(), - equalTo("fox")); - assertThat(searchResponse.getHits().getAt(4).getHighlightFields().get("query2_1_field1").fragments()[0].string(), - equalTo("brown fox")); + assertThat( + searchResponse.getHits().getAt(0).getFields().get("_percolator_document_slot_query1").getValues(), + equalTo(Collections.singletonList(1)) + ); + assertThat( + searchResponse.getHits().getAt(0).getFields().get("_percolator_document_slot_query2").getValues(), + equalTo(Collections.singletonList(1)) + ); + assertThat( + searchResponse.getHits().getAt(0).getHighlightFields().get("query1_1_field1").fragments()[0].string(), + equalTo("fox") + ); + assertThat( + searchResponse.getHits().getAt(0).getHighlightFields().get("query2_1_field1").fragments()[0].string(), + equalTo("brown fox") + ); + + assertThat( + searchResponse.getHits().getAt(1).getFields().get("_percolator_document_slot_query1").getValues(), + equalTo(Collections.singletonList(0)) + ); + assertThat( + searchResponse.getHits().getAt(1).getHighlightFields().get("query1_0_field1").fragments()[0].string(), + equalTo("dog") + ); + + assertThat( + searchResponse.getHits().getAt(2).getFields().get("_percolator_document_slot_query2").getValues(), + equalTo(Collections.singletonList(0)) + ); + assertThat( + searchResponse.getHits().getAt(2).getHighlightFields().get("query2_0_field1").fragments()[0].string(), + equalTo("jumps") + ); + + assertThat( + searchResponse.getHits().getAt(3).getFields().get("_percolator_document_slot_query1").getValues(), + equalTo(Collections.singletonList(0)) + ); + assertThat( + searchResponse.getHits().getAt(3).getHighlightFields().get("query1_0_field1").fragments()[0].string(), + equalTo("dog") + ); + + assertThat( + searchResponse.getHits().getAt(4).getFields().get("_percolator_document_slot_query1").getValues(), + equalTo(Collections.singletonList(1)) + ); + assertThat( + searchResponse.getHits().getAt(4).getFields().get("_percolator_document_slot_query2").getValues(), + equalTo(Collections.singletonList(1)) + ); + assertThat( + searchResponse.getHits().getAt(4).getHighlightFields().get("query1_1_field1").fragments()[0].string(), + equalTo("fox") + ); + assertThat( + searchResponse.getHits().getAt(4).getHighlightFields().get("query2_1_field1").fragments()[0].string(), + equalTo("brown fox") + ); } public void testTakePositionOffsetGapIntoAccount() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test") + assertAcked( + client().admin() + .indices() + .prepareCreate("test") .setMapping("field", "type=text,position_increment_gap=5", "query", "type=percolator") ); - client().prepareIndex("test").setId("1") - .setSource(jsonBuilder().startObject().field("query", - new MatchPhraseQueryBuilder("field", "brown fox").slop(4)).endObject()) - .get(); - client().prepareIndex("test").setId("2") - .setSource(jsonBuilder().startObject().field("query", - new MatchPhraseQueryBuilder("field", "brown fox").slop(5)).endObject()) - .get(); + client().prepareIndex("test") + .setId("1") + .setSource(jsonBuilder().startObject().field("query", new MatchPhraseQueryBuilder("field", "brown fox").slop(4)).endObject()) + .get(); + client().prepareIndex("test") + .setId("2") + .setSource(jsonBuilder().startObject().field("query", new MatchPhraseQueryBuilder("field", "brown fox").slop(5)).endObject()) + .get(); client().admin().indices().prepareRefresh().get(); - SearchResponse response = client().prepareSearch().setQuery( - new PercolateQueryBuilder("query", new BytesArray("{\"field\" : [\"brown\", \"fox\"]}"), XContentType.JSON) - ).get(); + SearchResponse response = client().prepareSearch() + .setQuery(new PercolateQueryBuilder("query", new BytesArray("{\"field\" : [\"brown\", \"fox\"]}"), XContentType.JSON)) + .get(); assertHitCount(response, 1); assertThat(response.getHits().getAt(0).getId(), equalTo("2")); } - public void testManyPercolatorFields() throws Exception { String queryFieldName = randomAlphaOfLength(8); - assertAcked(client().admin().indices().prepareCreate("test1") - .setMapping(queryFieldName, "type=percolator", "field", "type=keyword") + assertAcked( + client().admin().indices().prepareCreate("test1").setMapping(queryFieldName, "type=percolator", "field", "type=keyword") ); - assertAcked(client().admin().indices().prepareCreate("test2") - .setMapping(queryFieldName, "type=percolator", "second_query_field", "type=percolator", "field", "type=keyword") + assertAcked( + client().admin() + .indices() + .prepareCreate("test2") + .setMapping(queryFieldName, "type=percolator", "second_query_field", "type=percolator", "field", "type=keyword") ); - assertAcked(client().admin().indices().prepareCreate("test3") - .setMapping(jsonBuilder().startObject().startObject("_doc").startObject("properties") - .startObject("field") - .field("type", "keyword") - .endObject() - .startObject("object_field") - .field("type", "object") - .startObject("properties") - .startObject(queryFieldName) - .field("type", "percolator") - .endObject() - .endObject() - .endObject() - .endObject() - .endObject().endObject()) + assertAcked( + client().admin() + .indices() + .prepareCreate("test3") + .setMapping( + jsonBuilder().startObject() + .startObject("_doc") + .startObject("properties") + .startObject("field") + .field("type", "keyword") + .endObject() + .startObject("object_field") + .field("type", "object") + .startObject("properties") + .startObject(queryFieldName) + .field("type", "percolator") + .endObject() + .endObject() + .endObject() + .endObject() + .endObject() + .endObject() + ) ); } public void testWithMultiplePercolatorFields() throws Exception { String queryFieldName = randomAlphaOfLength(8); - assertAcked(client().admin().indices().prepareCreate("test1") - .setMapping(queryFieldName, "type=percolator", "field", "type=keyword")); - assertAcked(client().admin().indices().prepareCreate("test2") - .setMapping(jsonBuilder().startObject().startObject("_doc").startObject("properties") + assertAcked( + client().admin().indices().prepareCreate("test1").setMapping(queryFieldName, "type=percolator", "field", "type=keyword") + ); + assertAcked( + client().admin() + .indices() + .prepareCreate("test2") + .setMapping( + jsonBuilder().startObject() + .startObject("_doc") + .startObject("properties") .startObject("field") .field("type", "keyword") .endObject() @@ -683,45 +850,62 @@ public void testWithMultiplePercolatorFields() throws Exception { .endObject() .endObject() .endObject() - .endObject().endObject()) + .endObject() + .endObject() + ) ); // Acceptable: - client().prepareIndex("test1").setId("1") - .setSource(jsonBuilder().startObject().field(queryFieldName, matchQuery("field", "value")).endObject()) - .get(); - client().prepareIndex("test2").setId("1") - .setSource(jsonBuilder().startObject().startObject("object_field") - .field(queryFieldName, matchQuery("field", "value")) - .endObject().endObject()) - .get(); + client().prepareIndex("test1") + .setId("1") + .setSource(jsonBuilder().startObject().field(queryFieldName, matchQuery("field", "value")).endObject()) + .get(); + client().prepareIndex("test2") + .setId("1") + .setSource( + jsonBuilder().startObject() + .startObject("object_field") + .field(queryFieldName, matchQuery("field", "value")) + .endObject() + .endObject() + ) + .get(); client().admin().indices().prepareRefresh().get(); BytesReference source = BytesReference.bytes(jsonBuilder().startObject().field("field", "value").endObject()); SearchResponse response = client().prepareSearch() - .setQuery(new PercolateQueryBuilder(queryFieldName, source, XContentType.JSON)) - .setIndices("test1") - .get(); + .setQuery(new PercolateQueryBuilder(queryFieldName, source, XContentType.JSON)) + .setIndices("test1") + .get(); assertHitCount(response, 1); assertThat(response.getHits().getAt(0).getId(), equalTo("1")); assertThat(response.getHits().getAt(0).getIndex(), equalTo("test1")); response = client().prepareSearch() - .setQuery(new PercolateQueryBuilder("object_field." + queryFieldName, source, XContentType.JSON)) - .setIndices("test2") - .get(); + .setQuery(new PercolateQueryBuilder("object_field." + queryFieldName, source, XContentType.JSON)) + .setIndices("test2") + .get(); assertHitCount(response, 1); assertThat(response.getHits().getAt(0).getId(), equalTo("1")); assertThat(response.getHits().getAt(0).getIndex(), equalTo("test2")); // Unacceptable: MapperParsingException e = expectThrows(MapperParsingException.class, () -> { - client().prepareIndex("test2").setId("1") - .setSource(jsonBuilder().startObject().startArray("object_field") - .startObject().field(queryFieldName, matchQuery("field", "value")).endObject() - .startObject().field(queryFieldName, matchQuery("field", "value")).endObject() - .endArray().endObject()) - .get(); + client().prepareIndex("test2") + .setId("1") + .setSource( + jsonBuilder().startObject() + .startArray("object_field") + .startObject() + .field(queryFieldName, matchQuery("field", "value")) + .endObject() + .startObject() + .field(queryFieldName, matchQuery("field", "value")) + .endObject() + .endArray() + .endObject() + ) + .get(); }); assertThat(e.getCause(), instanceOf(IllegalArgumentException.class)); assertThat(e.getCause().getMessage(), equalTo("a document can only contain one percolator query")); @@ -729,94 +913,176 @@ public void testWithMultiplePercolatorFields() throws Exception { public void testPercolateQueryWithNestedDocuments() throws Exception { XContentBuilder mapping = XContentFactory.jsonBuilder(); - mapping.startObject().startObject("properties").startObject("query").field("type", "percolator").endObject() - .startObject("id").field("type", "keyword").endObject() - .startObject("companyname").field("type", "text").endObject().startObject("employee").field("type", "nested") - .startObject("properties").startObject("name").field("type", "text").endObject().endObject().endObject().endObject() - .endObject(); - assertAcked(client().admin().indices().prepareCreate("test") - .setMapping(mapping) - ); - client().prepareIndex("test").setId("q1").setSource(jsonBuilder().startObject() - .field("id", "q1") - .field("query", QueryBuilders.nestedQuery("employee", - QueryBuilders.matchQuery("employee.name", "virginia potts").operator(Operator.AND), ScoreMode.Avg) - ).endObject()) - .get(); + mapping.startObject() + .startObject("properties") + .startObject("query") + .field("type", "percolator") + .endObject() + .startObject("id") + .field("type", "keyword") + .endObject() + .startObject("companyname") + .field("type", "text") + .endObject() + .startObject("employee") + .field("type", "nested") + .startObject("properties") + .startObject("name") + .field("type", "text") + .endObject() + .endObject() + .endObject() + .endObject() + .endObject(); + assertAcked(client().admin().indices().prepareCreate("test").setMapping(mapping)); + client().prepareIndex("test") + .setId("q1") + .setSource( + jsonBuilder().startObject() + .field("id", "q1") + .field( + "query", + QueryBuilders.nestedQuery( + "employee", + QueryBuilders.matchQuery("employee.name", "virginia potts").operator(Operator.AND), + ScoreMode.Avg + ) + ) + .endObject() + ) + .get(); // this query should never match as it doesn't use nested query: - client().prepareIndex("test").setId("q2").setSource(jsonBuilder().startObject() - .field("id", "q2") - .field("query", QueryBuilders.matchQuery("employee.name", "virginia")).endObject()) - .get(); + client().prepareIndex("test") + .setId("q2") + .setSource( + jsonBuilder().startObject() + .field("id", "q2") + .field("query", QueryBuilders.matchQuery("employee.name", "virginia")) + .endObject() + ) + .get(); client().admin().indices().prepareRefresh().get(); - client().prepareIndex("test").setId("q3").setSource(jsonBuilder().startObject() - .field("id", "q3") - .field("query", QueryBuilders.matchAllQuery()).endObject()) + client().prepareIndex("test") + .setId("q3") + .setSource(jsonBuilder().startObject().field("id", "q3").field("query", QueryBuilders.matchAllQuery()).endObject()) .get(); client().admin().indices().prepareRefresh().get(); SearchResponse response = client().prepareSearch() - .setQuery(new PercolateQueryBuilder("query", - BytesReference.bytes(XContentFactory.jsonBuilder() - .startObject().field("companyname", "stark") - .startArray("employee") - .startObject().field("name", "virginia potts").endObject() - .startObject().field("name", "tony stark").endObject() - .endArray() - .endObject()), XContentType.JSON)) - .addSort("id", SortOrder.ASC) - .get(); + .setQuery( + new PercolateQueryBuilder( + "query", + BytesReference.bytes( + XContentFactory.jsonBuilder() + .startObject() + .field("companyname", "stark") + .startArray("employee") + .startObject() + .field("name", "virginia potts") + .endObject() + .startObject() + .field("name", "tony stark") + .endObject() + .endArray() + .endObject() + ), + XContentType.JSON + ) + ) + .addSort("id", SortOrder.ASC) + .get(); assertHitCount(response, 2); assertThat(response.getHits().getAt(0).getId(), equalTo("q1")); assertThat(response.getHits().getAt(1).getId(), equalTo("q3")); response = client().prepareSearch() - .setQuery(new PercolateQueryBuilder("query", - BytesReference.bytes(XContentFactory.jsonBuilder() - .startObject().field("companyname", "notstark") - .startArray("employee") - .startObject().field("name", "virginia stark").endObject() - .startObject().field("name", "tony stark").endObject() - .endArray() - .endObject()), XContentType.JSON)) - .addSort("id", SortOrder.ASC) - .get(); + .setQuery( + new PercolateQueryBuilder( + "query", + BytesReference.bytes( + XContentFactory.jsonBuilder() + .startObject() + .field("companyname", "notstark") + .startArray("employee") + .startObject() + .field("name", "virginia stark") + .endObject() + .startObject() + .field("name", "tony stark") + .endObject() + .endArray() + .endObject() + ), + XContentType.JSON + ) + ) + .addSort("id", SortOrder.ASC) + .get(); assertHitCount(response, 1); assertThat(response.getHits().getAt(0).getId(), equalTo("q3")); response = client().prepareSearch() - .setQuery(new PercolateQueryBuilder("query", - BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field("companyname", "notstark").endObject()), - XContentType.JSON)) - .addSort("id", SortOrder.ASC) - .get(); + .setQuery( + new PercolateQueryBuilder( + "query", + BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field("companyname", "notstark").endObject()), + XContentType.JSON + ) + ) + .addSort("id", SortOrder.ASC) + .get(); assertHitCount(response, 1); assertThat(response.getHits().getAt(0).getId(), equalTo("q3")); response = client().prepareSearch() - .setQuery(new PercolateQueryBuilder("query", Arrays.asList( - BytesReference.bytes(XContentFactory.jsonBuilder() - .startObject().field("companyname", "stark") - .startArray("employee") - .startObject().field("name", "virginia potts").endObject() - .startObject().field("name", "tony stark").endObject() - .endArray() - .endObject()), - BytesReference.bytes(XContentFactory.jsonBuilder() - .startObject().field("companyname", "stark") - .startArray("employee") - .startObject().field("name", "peter parker").endObject() - .startObject().field("name", "virginia potts").endObject() - .endArray() - .endObject()), - BytesReference.bytes(XContentFactory.jsonBuilder() - .startObject().field("companyname", "stark") - .startArray("employee") - .startObject().field("name", "peter parker").endObject() - .endArray() - .endObject()) - ), XContentType.JSON)) + .setQuery( + new PercolateQueryBuilder( + "query", + Arrays.asList( + BytesReference.bytes( + XContentFactory.jsonBuilder() + .startObject() + .field("companyname", "stark") + .startArray("employee") + .startObject() + .field("name", "virginia potts") + .endObject() + .startObject() + .field("name", "tony stark") + .endObject() + .endArray() + .endObject() + ), + BytesReference.bytes( + XContentFactory.jsonBuilder() + .startObject() + .field("companyname", "stark") + .startArray("employee") + .startObject() + .field("name", "peter parker") + .endObject() + .startObject() + .field("name", "virginia potts") + .endObject() + .endArray() + .endObject() + ), + BytesReference.bytes( + XContentFactory.jsonBuilder() + .startObject() + .field("companyname", "stark") + .startArray("employee") + .startObject() + .field("name", "peter parker") + .endObject() + .endArray() + .endObject() + ) + ), + XContentType.JSON + ) + ) .addSort("id", SortOrder.ASC) .get(); assertHitCount(response, 2); @@ -827,47 +1093,85 @@ public void testPercolateQueryWithNestedDocuments() throws Exception { } public void testPercolatorQueryViaMultiSearch() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test") - .setMapping("field1", "type=text", "query", "type=percolator") - ); + assertAcked(client().admin().indices().prepareCreate("test").setMapping("field1", "type=text", "query", "type=percolator")); - client().prepareIndex("test").setId("1") + client().prepareIndex("test") + .setId("1") .setSource(jsonBuilder().startObject().field("query", matchQuery("field1", "b")).field("a", "b").endObject()) - .execute().actionGet(); - client().prepareIndex("test").setId("2") + .execute() + .actionGet(); + client().prepareIndex("test") + .setId("2") .setSource(jsonBuilder().startObject().field("query", matchQuery("field1", "c")).endObject()) - .execute().actionGet(); - client().prepareIndex("test").setId("3") - .setSource(jsonBuilder().startObject().field("query", boolQuery() - .must(matchQuery("field1", "b")) - .must(matchQuery("field1", "c")) - ).endObject()) - .execute().actionGet(); - client().prepareIndex("test").setId("4") + .execute() + .actionGet(); + client().prepareIndex("test") + .setId("3") + .setSource( + jsonBuilder().startObject() + .field("query", boolQuery().must(matchQuery("field1", "b")).must(matchQuery("field1", "c"))) + .endObject() + ) + .execute() + .actionGet(); + client().prepareIndex("test") + .setId("4") .setSource(jsonBuilder().startObject().field("query", matchAllQuery()).endObject()) - .execute().actionGet(); - client().prepareIndex("test").setId("5") + .execute() + .actionGet(); + client().prepareIndex("test") + .setId("5") .setSource(jsonBuilder().startObject().field("field1", "c").endObject()) - .execute().actionGet(); + .execute() + .actionGet(); client().admin().indices().prepareRefresh().get(); MultiSearchResponse response = client().prepareMultiSearch() - .add(client().prepareSearch("test") - .setQuery(new PercolateQueryBuilder("query", - BytesReference.bytes(jsonBuilder().startObject().field("field1", "b").endObject()), XContentType.JSON))) - .add(client().prepareSearch("test") - .setQuery(new PercolateQueryBuilder("query", - BytesReference.bytes(yamlBuilder().startObject().field("field1", "c").endObject()), XContentType.YAML))) - .add(client().prepareSearch("test") - .setQuery(new PercolateQueryBuilder("query", - BytesReference.bytes(jsonBuilder().startObject().field("field1", "b c").endObject()), XContentType.JSON))) - .add(client().prepareSearch("test") - .setQuery(new PercolateQueryBuilder("query", - BytesReference.bytes(jsonBuilder().startObject().field("field1", "d").endObject()), XContentType.JSON))) - .add(client().prepareSearch("test") - .setQuery(new PercolateQueryBuilder("query", "test", "5", null, null, null))) - .add(client().prepareSearch("test") // non existing doc, so error element - .setQuery(new PercolateQueryBuilder("query", "test", "6", null, null, null))) + .add( + client().prepareSearch("test") + .setQuery( + new PercolateQueryBuilder( + "query", + BytesReference.bytes(jsonBuilder().startObject().field("field1", "b").endObject()), + XContentType.JSON + ) + ) + ) + .add( + client().prepareSearch("test") + .setQuery( + new PercolateQueryBuilder( + "query", + BytesReference.bytes(yamlBuilder().startObject().field("field1", "c").endObject()), + XContentType.YAML + ) + ) + ) + .add( + client().prepareSearch("test") + .setQuery( + new PercolateQueryBuilder( + "query", + BytesReference.bytes(jsonBuilder().startObject().field("field1", "b c").endObject()), + XContentType.JSON + ) + ) + ) + .add( + client().prepareSearch("test") + .setQuery( + new PercolateQueryBuilder( + "query", + BytesReference.bytes(jsonBuilder().startObject().field("field1", "d").endObject()), + XContentType.JSON + ) + ) + ) + .add(client().prepareSearch("test").setQuery(new PercolateQueryBuilder("query", "test", "5", null, null, null))) + .add( + client().prepareSearch("test") // non existing doc, so error element + .setQuery(new PercolateQueryBuilder("query", "test", "6", null, null, null)) + ) .get(); MultiSearchResponse.Item item = response.getResponses()[0]; @@ -903,22 +1207,24 @@ public void testPercolatorQueryViaMultiSearch() throws Exception { public void testDisallowExpensiveQueries() throws IOException { try { - assertAcked(client().admin().indices().prepareCreate("test") + assertAcked( + client().admin() + .indices() + .prepareCreate("test") .setMapping("id", "type=keyword", "field1", "type=keyword", "query", "type=percolator") ); - client().prepareIndex("test").setId("1") - .setSource(jsonBuilder().startObject() - .field("id", "1") - .field("query", matchQuery("field1", "value")).endObject()) - .get(); + client().prepareIndex("test") + .setId("1") + .setSource(jsonBuilder().startObject().field("id", "1").field("query", matchQuery("field1", "value")).endObject()) + .get(); refresh(); // Execute with search.allow_expensive_queries = null => default value = false => success BytesReference source = BytesReference.bytes(jsonBuilder().startObject().field("field1", "value").endObject()); SearchResponse response = client().prepareSearch() - .setQuery(new PercolateQueryBuilder("query", source, XContentType.JSON)) - .get(); + .setQuery(new PercolateQueryBuilder("query", source, XContentType.JSON)) + .get(); assertHitCount(response, 1); assertThat(response.getHits().getAt(0).getId(), equalTo("1")); assertThat(response.getHits().getAt(0).getFields().get("_percolator_document_slot").getValue(), equalTo(0)); @@ -928,21 +1234,21 @@ public void testDisallowExpensiveQueries() throws IOException { updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", false)); assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); - ElasticsearchException e = expectThrows(ElasticsearchException.class, - () -> client().prepareSearch() - .setQuery(new PercolateQueryBuilder("query", source, XContentType.JSON)) - .get()); - assertEquals("[percolate] queries cannot be executed when 'search.allow_expensive_queries' is set to false.", - e.getCause().getMessage()); + ElasticsearchException e = expectThrows( + ElasticsearchException.class, + () -> client().prepareSearch().setQuery(new PercolateQueryBuilder("query", source, XContentType.JSON)).get() + ); + assertEquals( + "[percolate] queries cannot be executed when 'search.allow_expensive_queries' is set to false.", + e.getCause().getMessage() + ); // Set search.allow_expensive_queries setting to "true" ==> success updateSettingsRequest = new ClusterUpdateSettingsRequest(); updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", true)); assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); - response = client().prepareSearch() - .setQuery(new PercolateQueryBuilder("query", source, XContentType.JSON)) - .get(); + response = client().prepareSearch().setQuery(new PercolateQueryBuilder("query", source, XContentType.JSON)).get(); assertHitCount(response, 1); assertThat(response.getHits().getAt(0).getId(), equalTo("1")); assertThat(response.getHits().getAt(0).getFields().get("_percolator_document_slot").getValue(), equalTo(0)); @@ -955,37 +1261,56 @@ public void testDisallowExpensiveQueries() throws IOException { public void testWrappedWithConstantScore() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test") - .setMapping("d", "type=date", "q", "type=percolator") - ); + assertAcked(client().admin().indices().prepareCreate("test").setMapping("d", "type=date", "q", "type=percolator")); - client().prepareIndex("test").setId("1") - .setSource(jsonBuilder().startObject().field("q", - boolQuery().must(rangeQuery("d").gt("now")) - ).endObject()) - .execute().actionGet(); + client().prepareIndex("test") + .setId("1") + .setSource(jsonBuilder().startObject().field("q", boolQuery().must(rangeQuery("d").gt("now"))).endObject()) + .execute() + .actionGet(); - client().prepareIndex("test").setId("2") - .setSource(jsonBuilder().startObject().field("q", - boolQuery().must(rangeQuery("d").lt("now")) - ).endObject()) - .execute().actionGet(); + client().prepareIndex("test") + .setId("2") + .setSource(jsonBuilder().startObject().field("q", boolQuery().must(rangeQuery("d").lt("now"))).endObject()) + .execute() + .actionGet(); client().admin().indices().prepareRefresh().get(); - SearchResponse response = client().prepareSearch("test").setQuery(new PercolateQueryBuilder("q", - BytesReference.bytes(jsonBuilder().startObject().field("d", "2020-02-01T15:00:00.000+11:00").endObject()), - XContentType.JSON)).get(); + SearchResponse response = client().prepareSearch("test") + .setQuery( + new PercolateQueryBuilder( + "q", + BytesReference.bytes(jsonBuilder().startObject().field("d", "2020-02-01T15:00:00.000+11:00").endObject()), + XContentType.JSON + ) + ) + .get(); assertEquals(1, response.getHits().getTotalHits().value); - response = client().prepareSearch("test").setQuery(new PercolateQueryBuilder("q", - BytesReference.bytes(jsonBuilder().startObject().field("d", "2020-02-01T15:00:00.000+11:00").endObject()), - XContentType.JSON)).addSort("_doc", SortOrder.ASC).get(); + response = client().prepareSearch("test") + .setQuery( + new PercolateQueryBuilder( + "q", + BytesReference.bytes(jsonBuilder().startObject().field("d", "2020-02-01T15:00:00.000+11:00").endObject()), + XContentType.JSON + ) + ) + .addSort("_doc", SortOrder.ASC) + .get(); assertEquals(1, response.getHits().getTotalHits().value); - response = client().prepareSearch("test").setQuery(constantScoreQuery(new PercolateQueryBuilder("q", - BytesReference.bytes(jsonBuilder().startObject().field("d", "2020-02-01T15:00:00.000+11:00").endObject()), - XContentType.JSON))).get(); + response = client().prepareSearch("test") + .setQuery( + constantScoreQuery( + new PercolateQueryBuilder( + "q", + BytesReference.bytes(jsonBuilder().startObject().field("d", "2020-02-01T15:00:00.000+11:00").endObject()), + XContentType.JSON + ) + ) + ) + .get(); assertEquals(1, response.getHits().getTotalHits().value); } diff --git a/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQuery.java b/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQuery.java index 95667ff1240d9..e85c7b88f4a80 100644 --- a/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQuery.java +++ b/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQuery.java @@ -25,9 +25,9 @@ import org.apache.lucene.search.Weight; import org.apache.lucene.util.Accountable; import org.apache.lucene.util.Bits; -import org.elasticsearch.core.CheckedFunction; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.lucene.Lucene; +import org.elasticsearch.core.CheckedFunction; import java.io.IOException; import java.util.List; @@ -46,9 +46,15 @@ final class PercolateQuery extends Query implements Accountable { private final IndexSearcher percolatorIndexSearcher; private final Query nonNestedDocsFilter; - PercolateQuery(String name, QueryStore queryStore, List documents, - Query candidateMatchesQuery, IndexSearcher percolatorIndexSearcher, - Query nonNestedDocsFilter, Query verifiedMatchesQuery) { + PercolateQuery( + String name, + QueryStore queryStore, + List documents, + Query candidateMatchesQuery, + IndexSearcher percolatorIndexSearcher, + Query nonNestedDocsFilter, + Query verifiedMatchesQuery + ) { this.name = name; this.documents = Objects.requireNonNull(documents); this.candidateMatchesQuery = Objects.requireNonNull(candidateMatchesQuery); @@ -62,8 +68,15 @@ final class PercolateQuery extends Query implements Accountable { public Query rewrite(IndexReader reader) throws IOException { Query rewritten = candidateMatchesQuery.rewrite(reader); if (rewritten != candidateMatchesQuery) { - return new PercolateQuery(name, queryStore, documents, rewritten, percolatorIndexSearcher, - nonNestedDocsFilter, verifiedMatchesQuery); + return new PercolateQuery( + name, + queryStore, + documents, + rewritten, + percolatorIndexSearcher, + nonNestedDocsFilter, + verifiedMatchesQuery + ); } else { return this; } @@ -114,10 +127,9 @@ boolean matchDocId(int docId) throws IOException { Query query = percolatorQueries.apply(docId); if (query != null) { if (nonNestedDocsFilter != null) { - query = new BooleanQuery.Builder() - .add(query, Occur.MUST) - .add(nonNestedDocsFilter, Occur.FILTER) - .build(); + query = new BooleanQuery.Builder().add(query, Occur.MUST) + .add(nonNestedDocsFilter, Occur.FILTER) + .build(); } TopDocs topDocs = percolatorIndexSearcher.search(query, 1); if (topDocs.scoreDocs.length > 0) { @@ -160,10 +172,7 @@ boolean matchDocId(int docId) throws IOException { return false; } if (nonNestedDocsFilter != null) { - query = new BooleanQuery.Builder() - .add(query, Occur.MUST) - .add(nonNestedDocsFilter, Occur.FILTER) - .build(); + query = new BooleanQuery.Builder().add(query, Occur.MUST).add(nonNestedDocsFilter, Occur.FILTER).build(); } return Lucene.exists(percolatorIndexSearcher, query); } @@ -230,8 +239,7 @@ public String toString(String s) { sources.append(document.utf8ToString()); sources.append('\n'); } - return "PercolateQuery{document_sources={" + sources + "},inner={" + - candidateMatchesQuery.toString(s) + "}}"; + return "PercolateQuery{document_sources={" + sources + "},inner={" + candidateMatchesQuery.toString(s) + "}}"; } @Override @@ -239,7 +247,6 @@ public void visit(QueryVisitor visitor) { visitor.visitLeaf(this); } - @Override public long ramBytesUsed() { long ramUsed = 0L; diff --git a/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQueryBuilder.java b/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQueryBuilder.java index a0c5d346941ab..89f2379a9ea27 100644 --- a/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQueryBuilder.java +++ b/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQueryBuilder.java @@ -43,15 +43,8 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.logging.DeprecationLogger; -import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.index.fielddata.IndexFieldData; import org.elasticsearch.index.fielddata.IndexFieldDataCache; @@ -68,6 +61,13 @@ import org.elasticsearch.index.query.SearchExecutionContext; import org.elasticsearch.indices.breaker.CircuitBreakerService; import org.elasticsearch.indices.breaker.NoneCircuitBreakerService; +import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; import java.io.ByteArrayInputStream; import java.io.IOException; @@ -80,18 +80,17 @@ import java.util.function.BiConsumer; import java.util.function.Supplier; -import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg; -import static org.elasticsearch.xcontent.ConstructingObjectParser.optionalConstructorArg; import static org.elasticsearch.core.RestApiVersion.equalTo; import static org.elasticsearch.search.SearchService.ALLOW_EXPENSIVE_QUERIES; +import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg; +import static org.elasticsearch.xcontent.ConstructingObjectParser.optionalConstructorArg; public class PercolateQueryBuilder extends AbstractQueryBuilder { - private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(ParseField.class); - static final String DOCUMENT_TYPE_DEPRECATION_MESSAGE = "[types removal] Types are deprecated in [percolate] queries. " + - "The [document_type] should no longer be specified."; - static final String TYPE_DEPRECATION_MESSAGE = "[types removal] Types are deprecated in [percolate] queries. " + - "The [type] of the indexed document should no longer be specified."; - + private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(ParseField.class); + static final String DOCUMENT_TYPE_DEPRECATION_MESSAGE = "[types removal] Types are deprecated in [percolate] queries. " + + "The [document_type] should no longer be specified."; + static final String TYPE_DEPRECATION_MESSAGE = "[types removal] Types are deprecated in [percolate] queries. " + + "The [type] of the indexed document should no longer be specified."; public static final String NAME = "percolate"; @@ -165,9 +164,14 @@ public PercolateQueryBuilder(String field, List documents, XCont * @param indexedDocumentPreference The preference to use when fetching the document to percolate * @param indexedDocumentVersion The expected version of the document to percolate */ - public PercolateQueryBuilder(String field, String indexedDocumentIndex, - String indexedDocumentId, String indexedDocumentRouting, - String indexedDocumentPreference, Long indexedDocumentVersion) { + public PercolateQueryBuilder( + String field, + String indexedDocumentIndex, + String indexedDocumentId, + String indexedDocumentRouting, + String indexedDocumentPreference, + Long indexedDocumentVersion + ) { if (field == null) { throw new IllegalArgumentException("[field] is a required argument"); } @@ -289,8 +293,13 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep if (documents.isEmpty() == false) { builder.startArray(DOCUMENTS_FIELD.getPreferredName()); for (BytesReference document : documents) { - try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, - LoggingDeprecationHandler.INSTANCE, document)) { + try ( + XContentParser parser = XContentHelper.createParser( + NamedXContentRegistry.EMPTY, + LoggingDeprecationHandler.INSTANCE, + document + ) + ) { parser.nextToken(); builder.generator().copyCurrentStructure(parser); } @@ -351,14 +360,24 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep PARSER.declareString(PercolateQueryBuilder::setName, NAME_FIELD); PARSER.declareString(PercolateQueryBuilder::queryName, AbstractQueryBuilder.NAME_FIELD); PARSER.declareFloat(PercolateQueryBuilder::boost, BOOST_FIELD); - PARSER.declareRequiredFieldSet(DOCUMENT_FIELD.getPreferredName(), - DOCUMENTS_FIELD.getPreferredName(), INDEXED_DOCUMENT_FIELD_ID.getPreferredName()); - PARSER.declareExclusiveFieldSet(DOCUMENT_FIELD.getPreferredName(), - DOCUMENTS_FIELD.getPreferredName(), INDEXED_DOCUMENT_FIELD_ID.getPreferredName()); - PARSER.declareString(deprecateAndIgnoreType("percolate_with_type", TYPE_DEPRECATION_MESSAGE), - INDEXED_DOCUMENT_FIELD_TYPE.forRestApiVersion(equalTo(RestApiVersion.V_7))); - PARSER.declareString(deprecateAndIgnoreType("percolate_with_document_type", DOCUMENT_TYPE_DEPRECATION_MESSAGE), - DOCUMENT_TYPE_FIELD.forRestApiVersion(equalTo(RestApiVersion.V_7))); + PARSER.declareRequiredFieldSet( + DOCUMENT_FIELD.getPreferredName(), + DOCUMENTS_FIELD.getPreferredName(), + INDEXED_DOCUMENT_FIELD_ID.getPreferredName() + ); + PARSER.declareExclusiveFieldSet( + DOCUMENT_FIELD.getPreferredName(), + DOCUMENTS_FIELD.getPreferredName(), + INDEXED_DOCUMENT_FIELD_ID.getPreferredName() + ); + PARSER.declareString( + deprecateAndIgnoreType("percolate_with_type", TYPE_DEPRECATION_MESSAGE), + INDEXED_DOCUMENT_FIELD_TYPE.forRestApiVersion(equalTo(RestApiVersion.V_7)) + ); + PARSER.declareString( + deprecateAndIgnoreType("percolate_with_document_type", DOCUMENT_TYPE_DEPRECATION_MESSAGE), + DOCUMENT_TYPE_FIELD.forRestApiVersion(equalTo(RestApiVersion.V_7)) + ); } private static BiConsumer deprecateAndIgnoreType(String key, String message) { @@ -380,10 +399,10 @@ public static PercolateQueryBuilder fromXContent(XContentParser parser) throws I @Override protected boolean doEquals(PercolateQueryBuilder other) { return Objects.equals(field, other.field) - && Objects.equals(documents, other.documents) - && Objects.equals(indexedDocumentIndex, other.indexedDocumentIndex) - && Objects.equals(documentSupplier, other.documentSupplier) - && Objects.equals(indexedDocumentId, other.indexedDocumentId); + && Objects.equals(documents, other.documents) + && Objects.equals(indexedDocumentIndex, other.indexedDocumentIndex) + && Objects.equals(documentSupplier, other.documentSupplier) + && Objects.equals(indexedDocumentId, other.indexedDocumentId); } @@ -406,8 +425,11 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) { if (source == null) { return this; // not executed yet } else { - PercolateQueryBuilder rewritten = new PercolateQueryBuilder(field, - Collections.singletonList(source), XContentHelper.xContentType(source)); + PercolateQueryBuilder rewritten = new PercolateQueryBuilder( + field, + Collections.singletonList(source), + XContentHelper.xContentType(source) + ); if (name != null) { rewritten.setName(name); } @@ -426,10 +448,12 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) { client.get(getRequest, ActionListener.wrap(getResponse -> { if (getResponse.isExists() == false) { throw new ResourceNotFoundException( - "indexed document [{}/{}] couldn't be found", indexedDocumentIndex, indexedDocumentId + "indexed document [{}/{}] couldn't be found", + indexedDocumentIndex, + indexedDocumentId ); } - if(getResponse.isSourceEmpty()) { + if (getResponse.isSourceEmpty()) { throw new IllegalArgumentException( "indexed document [" + indexedDocumentIndex + "/" + indexedDocumentId + "] source disabled" ); @@ -449,8 +473,9 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) { @Override protected Query doToQuery(SearchExecutionContext context) throws IOException { if (context.allowExpensiveQueries() == false) { - throw new ElasticsearchException("[percolate] queries cannot be executed when '" + - ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false."); + throw new ElasticsearchException( + "[percolate] queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false." + ); } // Call nowInMillis() so that this query becomes un-cacheable since we @@ -470,8 +495,10 @@ protected Query doToQuery(SearchExecutionContext context) throws IOException { } if ((fieldType instanceof PercolatorFieldMapper.PercolatorFieldType) == false) { - throw new QueryShardException(context, "expected field [" + field + - "] to be of type [percolator], but is of type [" + fieldType.typeName() + "]"); + throw new QueryShardException( + context, + "expected field [" + field + "] to be of type [percolator], but is of type [" + fieldType.typeName() + "]" + ); } final List docs = new ArrayList<>(); @@ -492,10 +519,8 @@ protected Analyzer getWrappedAnalyzer(String fieldName) { if (docs.size() > 1 || docs.get(0).docs().size() > 1) { assert docs.size() != 1 || context.hasNested(); docSearcher = createMultiDocumentSearcher(analyzer, docs); - excludeNestedDocuments = context.hasNested() && docs.stream() - .map(ParsedDocument::docs) - .mapToInt(List::size) - .anyMatch(size -> size > 1); + excludeNestedDocuments = context.hasNested() + && docs.stream().map(ParsedDocument::docs).mapToInt(List::size).anyMatch(size -> size > 1); } else { MemoryIndex memoryIndex = MemoryIndex.fromDocument(docs.get(0).rootDoc(), analyzer, true, false); docSearcher = memoryIndex.createSearcher(); @@ -506,9 +531,9 @@ protected Analyzer getWrappedAnalyzer(String fieldName) { PercolatorFieldMapper.PercolatorFieldType pft = (PercolatorFieldMapper.PercolatorFieldType) fieldType; String name = this.name != null ? this.name : pft.name(); SearchExecutionContext percolateShardContext = wrap(context); - PercolatorFieldMapper.configureContext(percolateShardContext, pft.mapUnmappedFieldsAsText);; - PercolateQuery.QueryStore queryStore = createStore(pft.queryBuilderField, - percolateShardContext); + PercolatorFieldMapper.configureContext(percolateShardContext, pft.mapUnmappedFieldsAsText); + ; + PercolateQuery.QueryStore queryStore = createStore(pft.queryBuilderField, percolateShardContext); return pft.percolateQuery(name, queryStore, documents, docSearcher, excludeNestedDocuments, context.indexVersionCreated()); } @@ -521,7 +546,7 @@ public List getDocuments() { return documents; } - //pkg-private for testing + // pkg-private for testing XContentType getXContentType() { return documentXContentType; } @@ -534,10 +559,7 @@ static IndexSearcher createMultiDocumentSearcher(Analyzer analyzer, Collection

    iterable = () -> docs.stream() - .map(ParsedDocument::docs) - .flatMap(Collection::stream) - .iterator(); + Iterable iterable = () -> docs.stream().map(ParsedDocument::docs).flatMap(Collection::stream).iterator(); indexWriter.addDocuments(iterable); DirectoryReader directoryReader = DirectoryReader.open(indexWriter); @@ -550,8 +572,7 @@ static IndexSearcher createMultiDocumentSearcher(Analyzer analyzer, Collection

    { @@ -564,8 +585,12 @@ static PercolateQuery.QueryStore createStore(MappedFieldType queryBuilderFieldTy if (binaryDocValues.advanceExact(docId)) { BytesRef qbSource = binaryDocValues.binaryValue(); try (InputStream in = new ByteArrayInputStream(qbSource.bytes, qbSource.offset, qbSource.length)) { - try (StreamInput input = new NamedWriteableAwareStreamInput( - new InputStreamStreamInput(in, qbSource.length), registry)) { + try ( + StreamInput input = new NamedWriteableAwareStreamInput( + new InputStreamStreamInput(in, qbSource.length), + registry + ) + ) { input.setVersion(indexVersion); // Query builder's content is stored via BinaryFieldMapper, which has a custom encoding // to encode multiple binary values into a single binary doc values field. @@ -618,8 +643,7 @@ public BitSetProducer bitsetFilter(Query query) { @Override @SuppressWarnings("unchecked") public > IFD getForField(MappedFieldType fieldType) { - IndexFieldData.Builder builder = fieldType.fielddataBuilder(delegate.getFullyQualifiedIndex().getName(), - delegate::lookup); + IndexFieldData.Builder builder = fieldType.fielddataBuilder(delegate.getFullyQualifiedIndex().getName(), delegate::lookup); IndexFieldDataCache cache = new IndexFieldDataCache.None(); CircuitBreakerService circuitBreaker = new NoneCircuitBreakerService(); return (IFD) builder.build(cache, circuitBreaker); diff --git a/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolatorFieldMapper.java b/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolatorFieldMapper.java index ecf9844627f34..3a44bea25ae01 100644 --- a/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolatorFieldMapper.java +++ b/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolatorFieldMapper.java @@ -38,8 +38,6 @@ import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentLocation; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.core.Tuple; import org.elasticsearch.index.mapper.BinaryFieldMapper; import org.elasticsearch.index.mapper.DocumentParserContext; @@ -66,6 +64,8 @@ import org.elasticsearch.index.query.Rewriteable; import org.elasticsearch.index.query.SearchExecutionContext; import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder; +import org.elasticsearch.xcontent.XContentLocation; +import org.elasticsearch.xcontent.XContentParser; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -83,7 +83,10 @@ public class PercolatorFieldMapper extends FieldMapper { static final Setting INDEX_MAP_UNMAPPED_FIELDS_AS_TEXT_SETTING = Setting.boolSetting( - "index.percolator.map_unmapped_fields_as_text", false, Setting.Property.IndexScope); + "index.percolator.map_unmapped_fields_as_text", + false, + Setting.Property.IndexScope + ); static final String CONTENT_TYPE = "percolator"; static final byte FIELD_VALUE_SEPARATOR = 0; // nul code point @@ -140,10 +143,19 @@ public PercolatorFieldMapper build(MapperBuilderContext context) { fieldType.minimumShouldMatchField = minimumShouldMatchFieldMapper.fieldType(); fieldType.mapUnmappedFieldsAsText = mapUnmappedFieldsAsText; - return new PercolatorFieldMapper(name(), fieldType, - multiFields, copyTo.build(), searchExecutionContext, extractedTermsField, - extractionResultField, queryBuilderField, rangeFieldMapper, minimumShouldMatchFieldMapper, - mapUnmappedFieldsAsText); + return new PercolatorFieldMapper( + name(), + fieldType, + multiFields, + copyTo.build(), + searchExecutionContext, + extractedTermsField, + extractionResultField, + queryBuilderField, + rangeFieldMapper, + minimumShouldMatchFieldMapper, + mapUnmappedFieldsAsText + ); } static KeywordFieldMapper createExtractQueryFieldBuilder(String name, MapperBuilderContext context) { @@ -165,8 +177,10 @@ static RangeFieldMapper createExtractedRangeFieldBuilder(String name, RangeType } static NumberFieldMapper createMinimumShouldMatchField(MapperBuilderContext context) { - NumberFieldMapper.Builder builder = - NumberFieldMapper.Builder.docValuesOnly(MINIMUM_SHOULD_MATCH_FIELD_NAME, NumberFieldMapper.NumberType.INTEGER); + NumberFieldMapper.Builder builder = NumberFieldMapper.Builder.docValuesOnly( + MINIMUM_SHOULD_MATCH_FIELD_NAME, + NumberFieldMapper.NumberType.INTEGER + ); return builder.build(context); } @@ -213,8 +227,14 @@ public ValueFetcher valueFetcher(SearchExecutionContext context, String format) return SourceValueFetcher.identity(name(), context, format); } - Query percolateQuery(String name, PercolateQuery.QueryStore queryStore, List documents, - IndexSearcher searcher, boolean excludeNestedDocuments, Version indexVersion) throws IOException { + Query percolateQuery( + String name, + PercolateQuery.QueryStore queryStore, + List documents, + IndexSearcher searcher, + boolean excludeNestedDocuments, + Version indexVersion + ) throws IOException { IndexReader indexReader = searcher.getIndexReader(); Tuple t = createCandidateQuery(indexReader, indexVersion); Query candidateQuery = t.v1(); @@ -243,8 +263,8 @@ Tuple createCandidateQuery(IndexReader indexReader, Versi List extractedTerms = t.v1(); Map> encodedPointValuesByField = t.v2(); // `1 + ` is needed to take into account the EXTRACTION_FAILED should clause - boolean canUseMinimumShouldMatchField = 1 + extractedTerms.size() + encodedPointValuesByField.size() <= - BooleanQuery.getMaxClauseCount(); + boolean canUseMinimumShouldMatchField = 1 + extractedTerms.size() + encodedPointValuesByField.size() <= BooleanQuery + .getMaxClauseCount(); List subQueries = new ArrayList<>(); for (Map.Entry> entry : encodedPointValuesByField.entrySet()) { @@ -317,11 +337,19 @@ Tuple, Map>> extractTermsAndRanges(IndexRead private final RangeFieldMapper rangeFieldMapper; private final boolean mapUnmappedFieldsAsText; - PercolatorFieldMapper(String simpleName, MappedFieldType mappedFieldType, - MultiFields multiFields, CopyTo copyTo, Supplier searchExecutionContext, - KeywordFieldMapper queryTermsField, KeywordFieldMapper extractionResultField, - BinaryFieldMapper queryBuilderField, RangeFieldMapper rangeFieldMapper, - NumberFieldMapper minimumShouldMatchFieldMapper, boolean mapUnmappedFieldsAsText) { + PercolatorFieldMapper( + String simpleName, + MappedFieldType mappedFieldType, + MultiFields multiFields, + CopyTo copyTo, + Supplier searchExecutionContext, + KeywordFieldMapper queryTermsField, + KeywordFieldMapper extractionResultField, + BinaryFieldMapper queryBuilderField, + RangeFieldMapper rangeFieldMapper, + NumberFieldMapper minimumShouldMatchFieldMapper, + boolean mapUnmappedFieldsAsText + ) { super(simpleName, mappedFieldType, multiFields, copyTo); this.searchExecutionContext = searchExecutionContext; this.queryTermsField = queryTermsField; @@ -345,9 +373,7 @@ public void parse(DocumentParserContext context) throws IOException { configureContext(searchExecutionContext, isMapUnmappedFieldAsText()); XContentParser parser = context.parser(); - QueryBuilder queryBuilder = parseQueryBuilder( - parser, parser.getTokenLocation() - ); + QueryBuilder queryBuilder = parseQueryBuilder(parser, parser.getTokenLocation()); verifyQuery(queryBuilder); // Fetching of terms, shapes and indexed scripts happen during this rewrite: PlainActionFuture future = new PlainActionFuture<>(); @@ -362,10 +388,14 @@ public void parse(DocumentParserContext context) throws IOException { processQuery(query, context); } - static void createQueryBuilderField(Version indexVersion, BinaryFieldMapper qbField, - QueryBuilder queryBuilder, DocumentParserContext context) throws IOException { + static void createQueryBuilderField( + Version indexVersion, + BinaryFieldMapper qbField, + QueryBuilder queryBuilder, + DocumentParserContext context + ) throws IOException { try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) { - try (OutputStreamStreamOutput out = new OutputStreamStreamOutput(stream)) { + try (OutputStreamStreamOutput out = new OutputStreamStreamOutput(stream)) { out.setVersion(indexVersion); out.writeNamedWriteable(queryBuilder); qbField.indexValue(context, stream.toByteArray()); @@ -447,7 +477,11 @@ private static QueryBuilder parseQueryBuilder(XContentParser parser, XContentLoc @Override public Iterator iterator() { return Arrays.asList( - queryTermsField, extractionResultField, queryBuilderField, minimumShouldMatchFieldMapper, rangeFieldMapper + queryTermsField, + extractionResultField, + queryBuilderField, + minimumShouldMatchFieldMapper, + rangeFieldMapper ).iterator(); } diff --git a/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolatorHighlightSubFetchPhase.java b/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolatorHighlightSubFetchPhase.java index 21081e86a16a1..4a8e087074d4c 100644 --- a/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolatorHighlightSubFetchPhase.java +++ b/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolatorHighlightSubFetchPhase.java @@ -62,8 +62,9 @@ public void setNextReader(LeafReaderContext readerContext) { public void process(HitContext hit) throws IOException { boolean singlePercolateQuery = percolateQueries.size() == 1; for (PercolateQuery percolateQuery : percolateQueries) { - String fieldName = singlePercolateQuery ? PercolatorMatchedSlotSubFetchPhase.FIELD_NAME_PREFIX : - PercolatorMatchedSlotSubFetchPhase.FIELD_NAME_PREFIX + "_" + percolateQuery.getName(); + String fieldName = singlePercolateQuery + ? PercolatorMatchedSlotSubFetchPhase.FIELD_NAME_PREFIX + : PercolatorMatchedSlotSubFetchPhase.FIELD_NAME_PREFIX + "_" + percolateQuery.getName(); IndexSearcher percolatorIndexSearcher = percolateQuery.getPercolatorIndexSearcher(); PercolateQuery.QueryStore queryStore = percolateQuery.getQueryStore(); @@ -83,7 +84,8 @@ public void process(HitContext hit) throws IOException { HitContext subContext = new HitContext( new SearchHit(slot, "unknown", Collections.emptyMap(), Collections.emptyMap()), percolatorLeafReaderContext, - slot); + slot + ); subContext.sourceLookup().setSource(document); // force source because MemoryIndex does not store fields SearchHighlightContext highlight = new SearchHighlightContext(fetchContext.highlight().fields(), true); @@ -97,8 +99,9 @@ public void process(HitContext hit) throws IOException { } else { hlFieldName = percolateQuery.getName() + "_" + entry.getKey(); } - hit.hit().getHighlightFields().put(hlFieldName, - new HighlightField(hlFieldName, entry.getValue().fragments())); + hit.hit() + .getHighlightFields() + .put(hlFieldName, new HighlightField(hlFieldName, entry.getValue().fragments())); } else { // In case multiple documents are being percolated we need to identify to which document // a highlight belongs to. @@ -108,8 +111,9 @@ public void process(HitContext hit) throws IOException { } else { hlFieldName = percolateQuery.getName() + "_" + slot + "_" + entry.getKey(); } - hit.hit().getHighlightFields().put(hlFieldName, - new HighlightField(hlFieldName, entry.getValue().fragments())); + hit.hit() + .getHighlightFields() + .put(hlFieldName, new HighlightField(hlFieldName, entry.getValue().fragments())); } } } @@ -128,7 +132,7 @@ static List locatePercolatorQuery(Query query) { @Override public void visitLeaf(Query query) { if (query instanceof PercolateQuery) { - queries.add((PercolateQuery)query); + queries.add((PercolateQuery) query); } } }); diff --git a/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolatorMatchedSlotSubFetchPhase.java b/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolatorMatchedSlotSubFetchPhase.java index aef0185f9ffda..3944785e98ca9 100644 --- a/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolatorMatchedSlotSubFetchPhase.java +++ b/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolatorMatchedSlotSubFetchPhase.java @@ -122,8 +122,7 @@ String fieldName() { Query filterNestedDocs(Query in) { if (rootDocsBySlot != null) { // Ensures that we filter out nested documents - return new BooleanQuery.Builder() - .add(in, BooleanClause.Occur.MUST) + return new BooleanQuery.Builder().add(in, BooleanClause.Occur.MUST) .add(Queries.newNonNestedFilter(), BooleanClause.Occur.FILTER) .build(); } @@ -132,8 +131,7 @@ Query filterNestedDocs(Query in) { } static IntStream convertTopDocsToSlots(TopDocs topDocs, int[] rootDocsBySlot) { - IntStream stream = Arrays.stream(topDocs.scoreDocs) - .mapToInt(scoreDoc -> scoreDoc.doc); + IntStream stream = Arrays.stream(topDocs.scoreDocs).mapToInt(scoreDoc -> scoreDoc.doc); if (rootDocsBySlot != null) { stream = stream.map(docId -> Arrays.binarySearch(rootDocsBySlot, docId)); } diff --git a/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolatorPlugin.java b/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolatorPlugin.java index 04a84b7e646bd..a407414d5c8c6 100644 --- a/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolatorPlugin.java +++ b/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolatorPlugin.java @@ -30,10 +30,7 @@ public List> getQueries() { @Override public List getFetchSubPhases(FetchPhaseConstructionContext context) { - return Arrays.asList( - new PercolatorMatchedSlotSubFetchPhase(), - new PercolatorHighlightSubFetchPhase(context.getHighlighters()) - ); + return Arrays.asList(new PercolatorMatchedSlotSubFetchPhase(), new PercolatorHighlightSubFetchPhase(context.getHighlighters())); } @Override diff --git a/modules/percolator/src/main/java/org/elasticsearch/percolator/QueryAnalyzer.java b/modules/percolator/src/main/java/org/elasticsearch/percolator/QueryAnalyzer.java index 27ab455d5a0ff..f32dc225b8bba 100644 --- a/modules/percolator/src/main/java/org/elasticsearch/percolator/QueryAnalyzer.java +++ b/modules/percolator/src/main/java/org/elasticsearch/percolator/QueryAnalyzer.java @@ -43,8 +43,7 @@ final class QueryAnalyzer { - private QueryAnalyzer() { - } + private QueryAnalyzer() {} /** * Extracts terms and ranges from the provided query. These terms and ranges are stored with the percolator query and @@ -79,14 +78,21 @@ static Result analyze(Query query) { } private static final Set> verifiedQueries = Set.of( - TermQuery.class, TermInSetQuery.class, SynonymQuery.class, SpanTermQuery.class, SpanOrQuery.class, - BooleanQuery.class, DisjunctionMaxQuery.class, ConstantScoreQuery.class, BoostQuery.class, + TermQuery.class, + TermInSetQuery.class, + SynonymQuery.class, + SpanTermQuery.class, + SpanOrQuery.class, + BooleanQuery.class, + DisjunctionMaxQuery.class, + ConstantScoreQuery.class, + BoostQuery.class, BlendedTermQuery.class ); private static boolean isVerified(Query query) { if (query instanceof FunctionScoreQuery) { - return ((FunctionScoreQuery)query).getMinScore() == null; + return ((FunctionScoreQuery) query).getMinScore() == null; } for (Class cls : verifiedQueries) { if (cls.isAssignableFrom(query.getClass())) { @@ -172,14 +178,11 @@ public QueryVisitor getSubVisitor(Occur occur, Query parent) { public void visitLeaf(Query query) { if (query instanceof MatchAllDocsQuery) { terms.add(new Result(true, true)); - } - else if (query instanceof MatchNoDocsQuery) { + } else if (query instanceof MatchNoDocsQuery) { terms.add(Result.MATCH_NONE); - } - else if (query instanceof PointRangeQuery) { - terms.add(pointRangeQuery((PointRangeQuery)query)); - } - else { + } else if (query instanceof PointRangeQuery) { + terms.add(pointRangeQuery((PointRangeQuery) query)); + } else { terms.add(Result.UNKNOWN); } } @@ -227,8 +230,11 @@ private static Result pointRangeQuery(PointRangeQuery query) { byte[] interval = new byte[16]; NumericUtils.subtract(16, 0, prepad(upperPoint), prepad(lowerPoint), interval); - return new Result(false, Collections.singleton(new QueryExtraction( - new Range(query.getField(), lowerPoint, upperPoint, interval))), 1); + return new Result( + false, + Collections.singleton(new QueryExtraction(new Range(query.getField(), lowerPoint, upperPoint, interval))), + 1 + ); } private static byte[] prepad(byte[] original) { @@ -269,7 +275,7 @@ private static Result handleConjunction(List conjunctionsWithUnknowns) { // so that can lead to more false positives for percolator queries with range queries // than term based queries. // This is because the way number fields are extracted from the document to be - // percolated. Per field a single range is extracted and if a percolator query has two or + // percolated. Per field a single range is extracted and if a percolator query has two or // more range queries on the same field, then the minimum should match can be higher than clauses // in the CoveringQuery. Therefore right now the minimum should match is only incremented once per // number field when processing the percolator query at index time. @@ -281,8 +287,7 @@ private static Result handleConjunction(List conjunctionsWithUnknowns) { resultMsm = Math.max(0, resultMsm - 1); verified = false; } - } - else { + } else { // In case that there are duplicate term query extractions we need to be careful with // incrementing msm, because that could lead to valid matches not becoming candidate matches: // query: (field:val1 AND field:val2) AND (field:val2 AND field:val3) @@ -298,11 +303,7 @@ private static Result handleConjunction(List conjunctionsWithUnknowns) { // add range fields from this Result to the seenRangeFields set so that minimumShouldMatch is correctly // calculated for subsequent Results - result.extractions.stream() - .map(e -> e.range) - .filter(Objects::nonNull) - .map(e -> e.fieldName) - .forEach(seenRangeFields::add); + result.extractions.stream().map(e -> e.range).filter(Objects::nonNull).map(e -> e.fieldName).forEach(seenRangeFields::add); if (result.verified == false // If some inner extractions are optional, the result can't be verified @@ -349,12 +350,12 @@ private static Result handleDisjunction(List disjunctions, int requiredS for (int i = 0; i < disjunctions.size(); i++) { Result subResult = disjunctions.get(i); if (subResult.verified == false - // one of the sub queries requires more than one term to match, we can't - // verify it with a single top-level min_should_match - || subResult.minimumShouldMatch > 1 - // One of the inner clauses has multiple extractions, we won't be able to - // verify it with a single top-level min_should_match - || (subResult.extractions.size() > 1 && requiredShouldClauses > 1)) { + // one of the sub queries requires more than one term to match, we can't + // verify it with a single top-level min_should_match + || subResult.minimumShouldMatch > 1 + // One of the inner clauses has multiple extractions, we won't be able to + // verify it with a single top-level min_should_match + || (subResult.extractions.size() > 1 && requiredShouldClauses > 1)) { verified = false; } if (subResult.matchAllDocs) { @@ -380,10 +381,7 @@ private static Result handleDisjunction(List disjunctions, int requiredS if (hasRangeExtractions == false) { // Figure out what the combined msm is for this disjunction: // (sum the lowest required clauses, otherwise we're too strict and queries may not match) - clauses = clauses.stream() - .filter(val -> val > 0) - .sorted() - .collect(Collectors.toList()); + clauses = clauses.stream().filter(val -> val > 0).sorted().collect(Collectors.toList()); // When there are duplicated query extractions, percolator can no longer reliably determine msm across this disjunction if (hasDuplicateTerms) { @@ -421,8 +419,9 @@ static class Result { private Result(boolean matchAllDocs, boolean verified, Set extractions, int minimumShouldMatch) { if (minimumShouldMatch > extractions.size()) { - throw new IllegalArgumentException("minimumShouldMatch can't be greater than the number of extractions: " - + minimumShouldMatch + " > " + extractions.size()); + throw new IllegalArgumentException( + "minimumShouldMatch can't be greater than the number of extractions: " + minimumShouldMatch + " > " + extractions.size() + ); } this.matchAllDocs = matchAllDocs; this.extractions = extractions; @@ -459,7 +458,7 @@ boolean isMatchNoDocs() { return matchAllDocs == false && extractions.isEmpty(); } - static final Result UNKNOWN = new Result(false, false, Collections.emptySet(), 0){ + static final Result UNKNOWN = new Result(false, false, Collections.emptySet(), 0) { @Override boolean isUnknown() { return true; @@ -516,8 +515,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; QueryExtraction queryExtraction = (QueryExtraction) o; - return Objects.equals(term, queryExtraction.term) && - Objects.equals(range, queryExtraction.range); + return Objects.equals(term, queryExtraction.term) && Objects.equals(range, queryExtraction.range); } @Override @@ -527,10 +525,7 @@ public int hashCode() { @Override public String toString() { - return "QueryExtraction{" + - "term=" + term + - ",range=" + range + - '}'; + return "QueryExtraction{" + "term=" + term + ",range=" + range + '}'; } } @@ -554,9 +549,9 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Range range = (Range) o; - return Objects.equals(fieldName, range.fieldName) && - Arrays.equals(lowerPoint, range.lowerPoint) && - Arrays.equals(upperPoint, range.upperPoint); + return Objects.equals(fieldName, range.fieldName) + && Arrays.equals(lowerPoint, range.lowerPoint) + && Arrays.equals(upperPoint, range.upperPoint); } @Override @@ -570,10 +565,7 @@ public int hashCode() { @Override public String toString() { - return "Range{" + - ", fieldName='" + fieldName + '\'' + - ", interval=" + interval + - '}'; + return "Range{" + ", fieldName='" + fieldName + '\'' + ", interval=" + interval + '}'; } } diff --git a/modules/percolator/src/test/java/org/elasticsearch/percolator/CandidateQueryTests.java b/modules/percolator/src/test/java/org/elasticsearch/percolator/CandidateQueryTests.java index 5dea97db0cdb6..962f560d2b6ef 100644 --- a/modules/percolator/src/test/java/org/elasticsearch/percolator/CandidateQueryTests.java +++ b/modules/percolator/src/test/java/org/elasticsearch/percolator/CandidateQueryTests.java @@ -74,7 +74,6 @@ import org.elasticsearch.common.geo.ShapeRelation; import org.elasticsearch.common.lucene.search.function.FunctionScoreQuery; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.core.CheckedFunction; import org.elasticsearch.index.IndexService; import org.elasticsearch.index.mapper.DocumentParserContext; @@ -87,6 +86,7 @@ import org.elasticsearch.plugins.Plugin; import org.elasticsearch.test.ESSingleNodeTestCase; import org.elasticsearch.test.VersionUtils; +import org.elasticsearch.xcontent.XContentFactory; import org.junit.After; import org.junit.Before; @@ -136,22 +136,51 @@ public void init() throws Exception { indexService = createIndex(indexName, Settings.EMPTY); mapperService = indexService.mapperService(); - String mapper = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type") + String mapper = Strings.toString( + XContentFactory.jsonBuilder() + .startObject() + .startObject("type") .startObject("properties") - .startObject("int_field").field("type", "integer").endObject() - .startObject("long_field").field("type", "long").endObject() - .startObject("half_float_field").field("type", "half_float").endObject() - .startObject("float_field").field("type", "float").endObject() - .startObject("double_field").field("type", "double").endObject() - .startObject("ip_field").field("type", "ip").endObject() - .startObject("field").field("type", "keyword").endObject() - .endObject().endObject().endObject()); + .startObject("int_field") + .field("type", "integer") + .endObject() + .startObject("long_field") + .field("type", "long") + .endObject() + .startObject("half_float_field") + .field("type", "half_float") + .endObject() + .startObject("float_field") + .field("type", "float") + .endObject() + .startObject("double_field") + .field("type", "double") + .endObject() + .startObject("ip_field") + .field("type", "ip") + .endObject() + .startObject("field") + .field("type", "keyword") + .endObject() + .endObject() + .endObject() + .endObject() + ); mapperService.merge("type", new CompressedXContent(mapper), MapperService.MergeReason.MAPPING_UPDATE); String queryField = "query_field"; - String percolatorMapper = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type") - .startObject("properties").startObject(queryField).field("type", "percolator").endObject().endObject() - .endObject().endObject()); + String percolatorMapper = Strings.toString( + XContentFactory.jsonBuilder() + .startObject() + .startObject("type") + .startObject("properties") + .startObject(queryField) + .field("type", "percolator") + .endObject() + .endObject() + .endObject() + .endObject() + ); mapperService.merge("type", new CompressedXContent(percolatorMapper), MapperService.MergeReason.MAPPING_UPDATE); fieldMapper = (PercolatorFieldMapper) mapperService.documentMapper().mappers().getMapper(queryField); fieldType = (PercolatorFieldMapper.PercolatorFieldType) fieldMapper.fieldType(); @@ -199,12 +228,32 @@ public void testDuel() throws Exception { queryFunctions.add(() -> new TermQuery(new Term(field2, randomFrom(stringContent.get(field2))))); queryFunctions.add(() -> intFieldType.termQuery(randomFrom(intValues), context)); queryFunctions.add(() -> intFieldType.termsQuery(Arrays.asList(randomFrom(intValues), randomFrom(intValues)), context)); - queryFunctions.add(() -> intFieldType.rangeQuery(intValues.get(4), intValues.get(intValues.size() - 4), true, - true, ShapeRelation.WITHIN, null, null, context)); - queryFunctions.add(() -> new TermInSetQuery(field1, new BytesRef(randomFrom(stringContent.get(field1))), - new BytesRef(randomFrom(stringContent.get(field1))))); - queryFunctions.add(() -> new TermInSetQuery(field2, new BytesRef(randomFrom(stringContent.get(field1))), - new BytesRef(randomFrom(stringContent.get(field1))))); + queryFunctions.add( + () -> intFieldType.rangeQuery( + intValues.get(4), + intValues.get(intValues.size() - 4), + true, + true, + ShapeRelation.WITHIN, + null, + null, + context + ) + ); + queryFunctions.add( + () -> new TermInSetQuery( + field1, + new BytesRef(randomFrom(stringContent.get(field1))), + new BytesRef(randomFrom(stringContent.get(field1))) + ) + ); + queryFunctions.add( + () -> new TermInSetQuery( + field2, + new BytesRef(randomFrom(stringContent.get(field1))), + new BytesRef(randomFrom(stringContent.get(field1))) + ) + ); // many iterations with boolean queries, which are the most complex queries to deal with when nested int numRandomBoolQueries = 1000; for (int i = 0; i < numRandomBoolQueries; i++) { @@ -249,8 +298,7 @@ public void testDuel() throws Exception { document.add(new TextField(entry.getKey(), value, Field.Store.NO)); } for (Integer intValue : intValues) { - List numberFields = - NumberFieldMapper.NumberType.INTEGER.createFields("int_field", intValue, true, true, false); + List numberFields = NumberFieldMapper.NumberType.INTEGER.createFields("int_field", intValue, true, true, false); for (Field numberField : numberFields) { document.add(numberField); } @@ -259,8 +307,13 @@ public void testDuel() throws Exception { duelRun(queryStore, memoryIndex, shardSearcher); } - private BooleanQuery createRandomBooleanQuery(int depth, List fields, Map> content, - MappedFieldType intFieldType, List intValues) { + private BooleanQuery createRandomBooleanQuery( + int depth, + List fields, + Map> content, + MappedFieldType intFieldType, + List intValues + ) { BooleanQuery.Builder builder = new BooleanQuery.Builder(); int numClauses = randomIntBetween(1, 1 << randomIntBetween(2, 4)); // use low numbers of clauses more often int numShouldClauses = 0; @@ -321,9 +374,9 @@ public void testDuel2() throws Exception { MappedFieldType intFieldType = mapperService.fieldType("int_field"); List ranges = new ArrayList<>(); - ranges.add(new int[]{-5, 5}); - ranges.add(new int[]{0, 10}); - ranges.add(new int[]{15, 50}); + ranges.add(new int[] { -5, 5 }); + ranges.add(new int[] { 0, 10 }); + ranges.add(new int[] { 15, 50 }); SearchExecutionContext context = createSearchContext(indexService).getSearchExecutionContext(); List documents = new ArrayList<>(); @@ -368,8 +421,13 @@ public void testDuel2() throws Exception { } for (int[] range : ranges) { - List numberFields = - NumberFieldMapper.NumberType.INTEGER.createFields("int_field", between(range[0], range[1]), true, true, false); + List numberFields = NumberFieldMapper.NumberType.INTEGER.createFields( + "int_field", + between(range[0], range[1]), + true, + true, + false + ); for (Field numberField : numberFields) { document.add(numberField); } @@ -379,8 +437,13 @@ public void testDuel2() throws Exception { } } - private BooleanQuery randomBQ(int depth, List stringValues, List ranges, - MappedFieldType intFieldType, SearchExecutionContext context) { + private BooleanQuery randomBQ( + int depth, + List stringValues, + List ranges, + MappedFieldType intFieldType, + SearchExecutionContext context + ) { final int numClauses = randomIntBetween(1, 4); final boolean onlyShouldClauses = randomBoolean(); final BooleanQuery.Builder builder = new BooleanQuery.Builder(); @@ -522,26 +585,23 @@ public void testDuelIdBased() throws Exception { public void testDuelSpecificQueries() throws Exception { List documents = new ArrayList<>(); - BlendedTermQuery blendedTermQuery = BlendedTermQuery.dismaxBlendedQuery(new Term[]{new Term("field", "quick"), - new Term("field", "brown"), new Term("field", "fox")}, 1.0f); + BlendedTermQuery blendedTermQuery = BlendedTermQuery.dismaxBlendedQuery( + new Term[] { new Term("field", "quick"), new Term("field", "brown"), new Term("field", "fox") }, + 1.0f + ); addQuery(blendedTermQuery, documents); - SpanNearQuery spanNearQuery = new SpanNearQuery.Builder("field", true) - .addClause(new SpanTermQuery(new Term("field", "quick"))) - .addClause(new SpanTermQuery(new Term("field", "brown"))) - .addClause(new SpanTermQuery(new Term("field", "fox"))) - .build(); + SpanNearQuery spanNearQuery = new SpanNearQuery.Builder("field", true).addClause(new SpanTermQuery(new Term("field", "quick"))) + .addClause(new SpanTermQuery(new Term("field", "brown"))) + .addClause(new SpanTermQuery(new Term("field", "fox"))) + .build(); addQuery(spanNearQuery, documents); - SpanNearQuery spanNearQuery2 = new SpanNearQuery.Builder("field", true) - .addClause(new SpanTermQuery(new Term("field", "the"))) - .addClause(new SpanTermQuery(new Term("field", "lazy"))) - .addClause(new SpanTermQuery(new Term("field", "doc"))) - .build(); - SpanOrQuery spanOrQuery = new SpanOrQuery( - spanNearQuery, - spanNearQuery2 - ); + SpanNearQuery spanNearQuery2 = new SpanNearQuery.Builder("field", true).addClause(new SpanTermQuery(new Term("field", "the"))) + .addClause(new SpanTermQuery(new Term("field", "lazy"))) + .addClause(new SpanTermQuery(new Term("field", "doc"))) + .build(); + SpanOrQuery spanOrQuery = new SpanOrQuery(spanNearQuery, spanNearQuery2); addQuery(spanOrQuery, documents); SpanNotQuery spanNotQuery = new SpanNotQuery(spanNearQuery, spanNearQuery); @@ -583,8 +643,14 @@ public void testRangeQueries() throws Exception { Version v = VersionUtils.randomIndexCompatibleVersion(random()); MemoryIndex memoryIndex = MemoryIndex.fromDocument(Collections.singleton(new IntPoint("int_field", 3)), new WhitespaceAnalyzer()); IndexSearcher percolateSearcher = memoryIndex.createSearcher(); - Query query = fieldType.percolateQuery("_name", queryStore, Collections.singletonList(new BytesArray("{}")), - percolateSearcher, false, v); + Query query = fieldType.percolateQuery( + "_name", + queryStore, + Collections.singletonList(new BytesArray("{}")), + percolateSearcher, + false, + v + ); TopDocs topDocs = shardSearcher.search(query, 1); assertEquals(1L, topDocs.totalHits.value); assertEquals(1, topDocs.scoreDocs.length); @@ -598,8 +664,7 @@ public void testRangeQueries() throws Exception { assertEquals(1, topDocs.scoreDocs.length); assertEquals(1, topDocs.scoreDocs[0].doc); - memoryIndex = MemoryIndex.fromDocument(Collections.singleton(new HalfFloatPoint("half_float_field", 12)), - new WhitespaceAnalyzer()); + memoryIndex = MemoryIndex.fromDocument(Collections.singleton(new HalfFloatPoint("half_float_field", 12)), new WhitespaceAnalyzer()); percolateSearcher = memoryIndex.createSearcher(); query = fieldType.percolateQuery("_name", queryStore, Collections.singletonList(new BytesArray("{}")), percolateSearcher, false, v); topDocs = shardSearcher.search(query, 1); @@ -623,8 +688,10 @@ public void testRangeQueries() throws Exception { assertEquals(1, topDocs.scoreDocs.length); assertEquals(4, topDocs.scoreDocs[0].doc); - memoryIndex = MemoryIndex.fromDocument(Collections.singleton(new InetAddressPoint("ip_field", - forString("192.168.0.4"))), new WhitespaceAnalyzer()); + memoryIndex = MemoryIndex.fromDocument( + Collections.singleton(new InetAddressPoint("ip_field", forString("192.168.0.4"))), + new WhitespaceAnalyzer() + ); percolateSearcher = memoryIndex.createSearcher(); query = fieldType.percolateQuery("_name", queryStore, Collections.singletonList(new BytesArray("{}")), percolateSearcher, false, v); topDocs = shardSearcher.search(query, 1); @@ -658,8 +725,10 @@ public void testDuelRangeQueries() throws Exception { int lowerIpPart = randomIntBetween(0, 255); int upperIpPart = randomIntBetween(lowerIpPart, 255); - addQuery(InetAddressPoint.newRangeQuery("ip_field", forString("192.168.1." + lowerIpPart), - forString("192.168.1." + upperIpPart)), documents); + addQuery( + InetAddressPoint.newRangeQuery("ip_field", forString("192.168.1." + lowerIpPart), forString("192.168.1." + upperIpPart)), + documents + ); indexWriter.addDocuments(documents); indexWriter.close(); @@ -723,15 +792,13 @@ public void testDuelRangeQueries() throws Exception { memoryIndex = MemoryIndex.fromDocument(doc, new WhitespaceAnalyzer()); duelRun(queryStore, memoryIndex, shardSearcher); - doc = Collections.singleton(new InetAddressPoint("ip_field", - forString("192.168.1." + randomIntBetween(lowerIpPart, upperIpPart)))); + doc = Collections.singleton(new InetAddressPoint("ip_field", forString("192.168.1." + randomIntBetween(lowerIpPart, upperIpPart)))); memoryIndex = MemoryIndex.fromDocument(doc, new WhitespaceAnalyzer()); result = executeQuery(queryStore, memoryIndex, shardSearcher); assertThat(result.scoreDocs.length, equalTo(1)); assertThat(result.scoreDocs[0].doc, equalTo(5)); duelRun(queryStore, memoryIndex, shardSearcher); - doc = Collections.singleton(new InetAddressPoint("ip_field", - forString("192.168.1." + randomIntBetween(0, 255)))); + doc = Collections.singleton(new InetAddressPoint("ip_field", forString("192.168.1." + randomIntBetween(0, 255)))); memoryIndex = MemoryIndex.fromDocument(doc, new WhitespaceAnalyzer()); duelRun(queryStore, memoryIndex, shardSearcher); } @@ -765,8 +832,14 @@ public void testPercolateMatchAll() throws Exception { MemoryIndex memoryIndex = new MemoryIndex(); memoryIndex.addField("field", "value1", new WhitespaceAnalyzer()); IndexSearcher percolateSearcher = memoryIndex.createSearcher(); - PercolateQuery query = (PercolateQuery) fieldType.percolateQuery("_name", queryStore, - Collections.singletonList(new BytesArray("{}")), percolateSearcher, false, Version.CURRENT); + PercolateQuery query = (PercolateQuery) fieldType.percolateQuery( + "_name", + queryStore, + Collections.singletonList(new BytesArray("{}")), + percolateSearcher, + false, + Version.CURRENT + ); TopDocs topDocs = shardSearcher.search(query, 10, new Sort(SortField.FIELD_DOC)); assertEquals(3L, topDocs.totalHits.value); assertEquals(3, topDocs.scoreDocs.length); @@ -798,8 +871,14 @@ public void testFunctionScoreQuery() throws Exception { MemoryIndex memoryIndex = new MemoryIndex(); memoryIndex.addField("field", "value", new WhitespaceAnalyzer()); IndexSearcher percolateSearcher = memoryIndex.createSearcher(); - PercolateQuery query = (PercolateQuery) fieldType.percolateQuery("_name", queryStore, - Collections.singletonList(new BytesArray("{}")), percolateSearcher, false, Version.CURRENT); + PercolateQuery query = (PercolateQuery) fieldType.percolateQuery( + "_name", + queryStore, + Collections.singletonList(new BytesArray("{}")), + percolateSearcher, + false, + Version.CURRENT + ); TopDocs topDocs = shardSearcher.search(query, 10, new Sort(SortField.FIELD_DOC)); assertEquals(2L, topDocs.totalHits.value); assertEquals(2, topDocs.scoreDocs.length); @@ -846,11 +925,16 @@ public void testPercolateSmallAndLargeDocument() throws Exception { documents.add(document); iw.addDocuments(documents); // IW#addDocuments(...) ensures we end up with a single segment } - try (IndexReader ir = DirectoryReader.open(directory)){ + try (IndexReader ir = DirectoryReader.open(directory)) { IndexSearcher percolateSearcher = new IndexSearcher(ir); - PercolateQuery query = (PercolateQuery) - fieldType.percolateQuery("_name", queryStore, Collections.singletonList(new BytesArray("{}")), - percolateSearcher, false, v); + PercolateQuery query = (PercolateQuery) fieldType.percolateQuery( + "_name", + queryStore, + Collections.singletonList(new BytesArray("{}")), + percolateSearcher, + false, + v + ); BooleanQuery candidateQuery = (BooleanQuery) query.getCandidateMatchesQuery(); assertThat(candidateQuery.clauses().get(0).getQuery(), instanceOf(CoveringQuery.class)); TopDocs topDocs = shardSearcher.search(query, 10); @@ -880,11 +964,16 @@ public void testPercolateSmallAndLargeDocument() throws Exception { } iw.addDocument(document); } - try (IndexReader ir = DirectoryReader.open(directory)){ + try (IndexReader ir = DirectoryReader.open(directory)) { IndexSearcher percolateSearcher = new IndexSearcher(ir); - PercolateQuery query = (PercolateQuery) - fieldType.percolateQuery("_name", queryStore, Collections.singletonList(new BytesArray("{}")), - percolateSearcher, false, v); + PercolateQuery query = (PercolateQuery) fieldType.percolateQuery( + "_name", + queryStore, + Collections.singletonList(new BytesArray("{}")), + percolateSearcher, + false, + v + ); BooleanQuery candidateQuery = (BooleanQuery) query.getCandidateMatchesQuery(); assertThat(candidateQuery.clauses().get(0).getQuery(), instanceOf(TermInSetQuery.class)); @@ -919,8 +1008,7 @@ public void testDuplicatedClauses() throws Exception { builder.add(builder2.build(), Occur.MUST); addQuery(builder.build(), docs); - builder = new BooleanQuery.Builder() - .setMinimumNumberShouldMatch(2); + builder = new BooleanQuery.Builder().setMinimumNumberShouldMatch(2); builder1 = new BooleanQuery.Builder(); builder1.add(new TermQuery(new Term("field", "value1")), Occur.MUST); builder1.add(new TermQuery(new Term("field", "value2")), Occur.MUST); @@ -1041,8 +1129,14 @@ public void testMsmAndRanges_disjunction() throws Exception { private void duelRun(PercolateQuery.QueryStore queryStore, MemoryIndex memoryIndex, IndexSearcher shardSearcher) throws IOException { boolean requireScore = randomBoolean(); IndexSearcher percolateSearcher = memoryIndex.createSearcher(); - Query percolateQuery = fieldType.percolateQuery("_name", queryStore, - Collections.singletonList(new BytesArray("{}")), percolateSearcher, false, Version.CURRENT); + Query percolateQuery = fieldType.percolateQuery( + "_name", + queryStore, + Collections.singletonList(new BytesArray("{}")), + percolateSearcher, + false, + Version.CURRENT + ); Query query = requireScore ? percolateQuery : new ConstantScoreQuery(percolateQuery); TopDocs topDocs = shardSearcher.search(query, 100); @@ -1092,8 +1186,10 @@ private void duelRun(PercolateQuery.QueryStore queryStore, MemoryIndex memoryInd } logger.error("controlTopDocs.scoreDocs[{}].query_terms_field={}", i, builder.toString()); - NumericDocValues numericValues = - MultiDocValues.getNumericValues(shardSearcher.getIndexReader(), fieldType.minimumShouldMatchField.name()); + NumericDocValues numericValues = MultiDocValues.getNumericValues( + shardSearcher.getIndexReader(), + fieldType.minimumShouldMatchField.name() + ); boolean exact = numericValues.advanceExact(controlTopDocs.scoreDocs[i].doc); if (exact) { logger.error("controlTopDocs.scoreDocs[{}].minimum_should_match_field={}", i, numericValues.longValue()); @@ -1116,12 +1212,17 @@ private void addQuery(Query query, List docs) { queries.add(query); } - private TopDocs executeQuery(PercolateQuery.QueryStore queryStore, - MemoryIndex memoryIndex, - IndexSearcher shardSearcher) throws IOException { + private TopDocs executeQuery(PercolateQuery.QueryStore queryStore, MemoryIndex memoryIndex, IndexSearcher shardSearcher) + throws IOException { IndexSearcher percolateSearcher = memoryIndex.createSearcher(); - Query percolateQuery = fieldType.percolateQuery("_name", queryStore, - Collections.singletonList(new BytesArray("{}")), percolateSearcher, false, Version.CURRENT); + Query percolateQuery = fieldType.percolateQuery( + "_name", + queryStore, + Collections.singletonList(new BytesArray("{}")), + percolateSearcher, + false, + Version.CURRENT + ); return shardSearcher.search(percolateQuery, 10); } @@ -1193,7 +1294,7 @@ public String toString() { @Override public Scorer scorer(LeafReaderContext context) throws IOException { - float _score[] = new float[]{boost}; + float _score[] = new float[] { boost }; DocIdSetIterator allDocs = DocIdSetIterator.all(context.reader().maxDoc()); CheckedFunction leaf = queryStore.getQueries(context); FilteredDocIdSetIterator memoryIndexIterator = new FilteredDocIdSetIterator(allDocs) { diff --git a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolateQueryBuilderTests.java b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolateQueryBuilderTests.java index b61222ab3ded0..c7f14912c7d98 100644 --- a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolateQueryBuilderTests.java +++ b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolateQueryBuilderTests.java @@ -20,21 +20,21 @@ import org.elasticsearch.common.compress.CompressedXContent; import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.lucene.uid.Versions; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.index.get.GetResult; import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.query.QueryBuilder; -import org.elasticsearch.index.query.SearchExecutionContext; import org.elasticsearch.index.query.Rewriteable; +import org.elasticsearch.index.query.SearchExecutionContext; import org.elasticsearch.ingest.RandomDocumentPicks; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.test.AbstractQueryTestCase; import org.elasticsearch.test.TestGeoShapeFieldMapperPlugin; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.xcontent.json.JsonXContent; import org.hamcrest.Matchers; import java.io.IOException; @@ -60,8 +60,7 @@ public class PercolateQueryBuilderTests extends AbstractQueryTestCase pqb.toQuery(createSearchExecutionContext())); assertThat(e.getMessage(), equalTo("query builder must be rewritten first")); QueryBuilder rewrite = rewriteAndFetch(pqb, createSearchExecutionContext()); - PercolateQueryBuilder geoShapeQueryBuilder = - new PercolateQueryBuilder(pqb.getField(), documentSource, XContentType.JSON); + PercolateQueryBuilder geoShapeQueryBuilder = new PercolateQueryBuilder(pqb.getField(), documentSource, XContentType.JSON); assertEquals(geoShapeQueryBuilder, rewrite); } public void testIndexedDocumentDoesNotExist() throws IOException { indexedDocumentExists = false; PercolateQueryBuilder pqb = doCreateTestQueryBuilder(true); - ResourceNotFoundException e = expectThrows(ResourceNotFoundException.class, () -> rewriteAndFetch(pqb, - createSearchExecutionContext())); - String expectedString = "indexed document [" + indexedDocumentIndex + "/" + - indexedDocumentId + "] couldn't be found"; - assertThat(e.getMessage() , equalTo(expectedString)); + ResourceNotFoundException e = expectThrows( + ResourceNotFoundException.class, + () -> rewriteAndFetch(pqb, createSearchExecutionContext()) + ); + String expectedString = "indexed document [" + indexedDocumentIndex + "/" + indexedDocumentId + "] couldn't be found"; + assertThat(e.getMessage(), equalTo(expectedString)); } @Override protected Map getObjectsHoldingArbitraryContent() { - //document contains arbitrary content, no error expected when an object is added to it + // document contains arbitrary content, no error expected when an object is added to it final Map objects = new HashMap<>(); objects.put(PercolateQueryBuilder.DOCUMENT_FIELD.getPreferredName(), null); objects.put(PercolateQueryBuilder.DOCUMENTS_FIELD.getPreferredName(), null); @@ -198,28 +229,25 @@ protected Map getObjectsHoldingArbitraryContent() { } public void testRequiredParameters() { - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> { - new PercolateQueryBuilder(null, new BytesArray("{}"), XContentType.JSON); - }); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> { new PercolateQueryBuilder(null, new BytesArray("{}"), XContentType.JSON); } + ); assertThat(e.getMessage(), equalTo("[field] is a required argument")); - e = expectThrows(IllegalArgumentException.class, - () -> new PercolateQueryBuilder("_field", (List)null, XContentType.JSON)); + e = expectThrows( + IllegalArgumentException.class, + () -> new PercolateQueryBuilder("_field", (List) null, XContentType.JSON) + ); assertThat(e.getMessage(), equalTo("[document] is a required argument")); - e = expectThrows(IllegalArgumentException.class, () -> { - new PercolateQueryBuilder(null, "_index", "_id", null, null, null); - }); + e = expectThrows(IllegalArgumentException.class, () -> { new PercolateQueryBuilder(null, "_index", "_id", null, null, null); }); assertThat(e.getMessage(), equalTo("[field] is a required argument")); - e = expectThrows(IllegalArgumentException.class, () -> { - new PercolateQueryBuilder("_field", null, "_id", null, null, null); - }); + e = expectThrows(IllegalArgumentException.class, () -> { new PercolateQueryBuilder("_field", null, "_id", null, null, null); }); assertThat(e.getMessage(), equalTo("[index] is a required argument")); - e = expectThrows(IllegalArgumentException.class, () -> { - new PercolateQueryBuilder("_field", "_index", null, null, null, null); - }); + e = expectThrows(IllegalArgumentException.class, () -> { new PercolateQueryBuilder("_field", "_index", null, null, null, null); }); assertThat(e.getMessage(), equalTo("[id] is a required argument")); } @@ -236,14 +264,23 @@ public void testFromJsonNoType() throws IOException { documentSource = Collections.singletonList(randomSource(new HashSet<>())); SearchExecutionContext searchExecutionContext = createSearchExecutionContext(); - QueryBuilder queryBuilder = parseQuery("{\"percolate\" : { \"index\": \"" + indexedDocumentIndex + "\", \"id\": \"" + - indexedDocumentId + "\", \"field\":\"" + queryField + "\"}}"); + QueryBuilder queryBuilder = parseQuery( + "{\"percolate\" : { \"index\": \"" + + indexedDocumentIndex + + "\", \"id\": \"" + + indexedDocumentId + + "\", \"field\":\"" + + queryField + + "\"}}" + ); rewriteAndFetch(queryBuilder, searchExecutionContext).toQuery(searchExecutionContext); } public void testBothDocumentAndDocumentsSpecified() { - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> parseQuery("{\"percolate\" : { \"document\": {}, \"documents\": [{}, {}], \"field\":\"" + queryField + "\"}}")); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> parseQuery("{\"percolate\" : { \"document\": {}, \"documents\": [{}, {}], \"field\":\"" + queryField + "\"}}") + ); assertThat(e.getMessage(), containsString("The following fields are not allowed together: [document, documents]")); } @@ -307,9 +344,7 @@ public void testFieldAlias() throws IOException { QueryBuilder rewrittenBuilder = rewriteAndFetch(builder, searchExecutionContext); PercolateQuery query = (PercolateQuery) rewrittenBuilder.toQuery(searchExecutionContext); - PercolateQueryBuilder aliasBuilder = new PercolateQueryBuilder(aliasField, - builder.getDocuments(), - builder.getXContentType()); + PercolateQueryBuilder aliasBuilder = new PercolateQueryBuilder(aliasField, builder.getDocuments(), builder.getXContentType()); QueryBuilder rewrittenAliasBuilder = rewriteAndFetch(aliasBuilder, searchExecutionContext); PercolateQuery aliasQuery = (PercolateQuery) rewrittenAliasBuilder.toQuery(searchExecutionContext); @@ -347,16 +382,17 @@ public void testDisallowExpensiveQueries() { when(searchExecutionContext.allowExpensiveQueries()).thenReturn(false); PercolateQueryBuilder queryBuilder = doCreateTestQueryBuilder(true); - ElasticsearchException e = expectThrows(ElasticsearchException.class, - () -> queryBuilder.toQuery(searchExecutionContext)); - assertEquals("[percolate] queries cannot be executed when 'search.allow_expensive_queries' is set to false.", - e.getMessage()); + ElasticsearchException e = expectThrows(ElasticsearchException.class, () -> queryBuilder.toQuery(searchExecutionContext)); + assertEquals("[percolate] queries cannot be executed when 'search.allow_expensive_queries' is set to false.", e.getMessage()); } public void testFromJsonWithDocumentType() throws IOException { SearchExecutionContext searchExecutionContext = createSearchExecutionContext(); - String queryAsString = "{\"percolate\" : { \"document\": {}, \"document_type\":\"" + docType + "\", \"field\":\"" + - queryField + "\"}}"; + String queryAsString = "{\"percolate\" : { \"document\": {}, \"document_type\":\"" + + docType + + "\", \"field\":\"" + + queryField + + "\"}}"; XContentParser parser = createParserWithCompatibilityFor(JsonXContent.jsonXContent, queryAsString, RestApiVersion.V_7); QueryBuilder queryBuilder = parseQuery(parser); queryBuilder.toQuery(searchExecutionContext); @@ -370,10 +406,15 @@ public void testFromJsonWithType() throws IOException { documentSource = Collections.singletonList(randomSource(new HashSet<>())); SearchExecutionContext searchExecutionContext = createSearchExecutionContext(); - String queryAsString = "{\"percolate\" : { \"index\": \"" + indexedDocumentIndex + - "\", \"type\": \"_doc\", \"id\": \"" + indexedDocumentId + "\", \"field\":\"" + queryField + "\"}}"; + String queryAsString = "{\"percolate\" : { \"index\": \"" + + indexedDocumentIndex + + "\", \"type\": \"_doc\", \"id\": \"" + + indexedDocumentId + + "\", \"field\":\"" + + queryField + + "\"}}"; XContentParser parser = createParserWithCompatibilityFor(JsonXContent.jsonXContent, queryAsString, RestApiVersion.V_7); - QueryBuilder queryBuilder = parseQuery(parser); + QueryBuilder queryBuilder = parseQuery(parser); rewriteAndFetch(queryBuilder, searchExecutionContext).toQuery(searchExecutionContext); assertWarnings(PercolateQueryBuilder.TYPE_DEPRECATION_MESSAGE); } diff --git a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolateQueryTests.java b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolateQueryTests.java index a358e3d11e564..8ae24dfc47475 100644 --- a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolateQueryTests.java +++ b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolateQueryTests.java @@ -18,6 +18,8 @@ import org.apache.lucene.index.NoMergePolicy; import org.apache.lucene.index.Term; import org.apache.lucene.index.memory.MemoryIndex; +import org.apache.lucene.queries.spans.SpanNearQuery; +import org.apache.lucene.queries.spans.SpanTermQuery; import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.ConstantScoreQuery; @@ -29,8 +31,6 @@ import org.apache.lucene.search.Query; import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.TopDocs; -import org.apache.lucene.queries.spans.SpanNearQuery; -import org.apache.lucene.queries.spans.SpanTermQuery; import org.apache.lucene.store.Directory; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.test.ESTestCase; @@ -105,8 +105,17 @@ public void testPercolateQuery() throws Exception { memoryIndex.addField("field", "the quick brown fox jumps over the lazy dog", new WhitespaceAnalyzer()); IndexSearcher percolateSearcher = memoryIndex.createSearcher(); // no scoring, wrapping it in a constant score query: - Query query = new ConstantScoreQuery(new PercolateQuery("_name", queryStore, Collections.singletonList(new BytesArray("a")), - new TermQuery(new Term("select", "a")), percolateSearcher, null, new MatchNoDocsQuery(""))); + Query query = new ConstantScoreQuery( + new PercolateQuery( + "_name", + queryStore, + Collections.singletonList(new BytesArray("a")), + new TermQuery(new Term("select", "a")), + percolateSearcher, + null, + new MatchNoDocsQuery("") + ) + ); TopDocs topDocs = shardSearcher.search(query, 10); assertThat(topDocs.totalHits.value, equalTo(1L)); assertThat(topDocs.scoreDocs.length, equalTo(1)); @@ -115,8 +124,17 @@ public void testPercolateQuery() throws Exception { assertThat(explanation.isMatch(), is(true)); assertThat(explanation.getValue(), equalTo(topDocs.scoreDocs[0].score)); - query = new ConstantScoreQuery(new PercolateQuery("_name", queryStore, Collections.singletonList(new BytesArray("b")), - new TermQuery(new Term("select", "b")), percolateSearcher, null, new MatchNoDocsQuery(""))); + query = new ConstantScoreQuery( + new PercolateQuery( + "_name", + queryStore, + Collections.singletonList(new BytesArray("b")), + new TermQuery(new Term("select", "b")), + percolateSearcher, + null, + new MatchNoDocsQuery("") + ) + ); topDocs = shardSearcher.search(query, 10); assertThat(topDocs.totalHits.value, equalTo(3L)); assertThat(topDocs.scoreDocs.length, equalTo(3)); @@ -135,13 +153,29 @@ public void testPercolateQuery() throws Exception { assertThat(explanation.isMatch(), is(true)); assertThat(explanation.getValue(), equalTo(topDocs.scoreDocs[2].score)); - query = new ConstantScoreQuery(new PercolateQuery("_name", queryStore, Collections.singletonList(new BytesArray("c")), - new MatchAllDocsQuery(), percolateSearcher, null, new MatchAllDocsQuery())); + query = new ConstantScoreQuery( + new PercolateQuery( + "_name", + queryStore, + Collections.singletonList(new BytesArray("c")), + new MatchAllDocsQuery(), + percolateSearcher, + null, + new MatchAllDocsQuery() + ) + ); topDocs = shardSearcher.search(query, 10); assertThat(topDocs.totalHits.value, equalTo(4L)); - query = new PercolateQuery("_name", queryStore, Collections.singletonList(new BytesArray("{}")), - new TermQuery(new Term("select", "b")), percolateSearcher, null, new MatchNoDocsQuery("")); + query = new PercolateQuery( + "_name", + queryStore, + Collections.singletonList(new BytesArray("{}")), + new TermQuery(new Term("select", "b")), + percolateSearcher, + null, + new MatchNoDocsQuery("") + ); topDocs = shardSearcher.search(query, 10); assertThat(topDocs.totalHits.value, equalTo(3L)); assertThat(topDocs.scoreDocs.length, equalTo(3)); diff --git a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolateWithNestedQueryBuilderTests.java b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolateWithNestedQueryBuilderTests.java index 53a6830d7e24a..cbfab4a61aeb0 100644 --- a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolateWithNestedQueryBuilderTests.java +++ b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolateWithNestedQueryBuilderTests.java @@ -12,10 +12,10 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.compress.CompressedXContent; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.SearchExecutionContext; +import org.elasticsearch.xcontent.XContentType; import java.io.IOException; @@ -24,21 +24,26 @@ public class PercolateWithNestedQueryBuilderTests extends PercolateQueryBuilderT @Override protected void initializeAdditionalMappings(MapperService mapperService) throws IOException { super.initializeAdditionalMappings(mapperService); - mapperService.merge("_doc", new CompressedXContent(Strings.toString(PutMappingRequest.simpleMapping( - "some_nested_object", "type=nested"))), MapperService.MergeReason.MAPPING_UPDATE); + mapperService.merge( + "_doc", + new CompressedXContent(Strings.toString(PutMappingRequest.simpleMapping("some_nested_object", "type=nested"))), + MapperService.MergeReason.MAPPING_UPDATE + ); } public void testDetectsNestedDocuments() throws IOException { SearchExecutionContext searchExecutionContext = createSearchExecutionContext(); - PercolateQueryBuilder builder = new PercolateQueryBuilder(queryField, - new BytesArray("{ \"foo\": \"bar\" }"), XContentType.JSON); + PercolateQueryBuilder builder = new PercolateQueryBuilder(queryField, new BytesArray("{ \"foo\": \"bar\" }"), XContentType.JSON); QueryBuilder rewrittenBuilder = rewriteAndFetch(builder, searchExecutionContext); PercolateQuery query = (PercolateQuery) rewrittenBuilder.toQuery(searchExecutionContext); assertFalse(query.excludesNestedDocs()); - builder = new PercolateQueryBuilder(queryField, - new BytesArray("{ \"foo\": \"bar\", \"some_nested_object\": [ { \"baz\": 42 } ] }"), XContentType.JSON); + builder = new PercolateQueryBuilder( + queryField, + new BytesArray("{ \"foo\": \"bar\", \"some_nested_object\": [ { \"baz\": 42 } ] }"), + XContentType.JSON + ); rewrittenBuilder = rewriteAndFetch(builder, searchExecutionContext); query = (PercolateQuery) rewrittenBuilder.toQuery(searchExecutionContext); assertTrue(query.excludesNestedDocs()); diff --git a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorFieldMapperTests.java b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorFieldMapperTests.java index f4691193cd3a1..7fa9f802a3a87 100644 --- a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorFieldMapperTests.java +++ b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorFieldMapperTests.java @@ -42,9 +42,6 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.network.InetAddresses; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.core.Tuple; import org.elasticsearch.index.IndexService; import org.elasticsearch.index.mapper.DocumentMapper; @@ -78,6 +75,9 @@ import org.elasticsearch.script.Script; import org.elasticsearch.test.ESSingleNodeTestCase; import org.elasticsearch.test.InternalSettingsPlugin; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentType; import org.junit.Before; import java.io.ByteArrayInputStream; @@ -95,7 +95,6 @@ import java.util.stream.Collectors; import static java.util.Collections.emptyMap; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.index.query.QueryBuilders.boolQuery; import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery; import static org.elasticsearch.index.query.QueryBuilders.matchPhraseQuery; @@ -108,6 +107,7 @@ import static org.elasticsearch.percolator.PercolatorFieldMapper.EXTRACTION_COMPLETE; import static org.elasticsearch.percolator.PercolatorFieldMapper.EXTRACTION_FAILED; import static org.elasticsearch.percolator.PercolatorFieldMapper.EXTRACTION_PARTIAL; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.instanceOf; @@ -134,30 +134,71 @@ public void init() throws Exception { indexService = createIndex("test"); mapperService = indexService.mapperService(); - String mapper = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("doc") - .startObject("properties") - .startObject("field").field("type", "text").endObject() - .startObject("field1").field("type", "text").endObject() - .startObject("field2").field("type", "text").endObject() - .startObject("_field3").field("type", "text").endObject() - .startObject("field4").field("type", "text").endObject() - .startObject("number_field1").field("type", "integer").endObject() - .startObject("number_field2").field("type", "long").endObject() - .startObject("number_field3").field("type", "long").endObject() - .startObject("number_field4").field("type", "half_float").endObject() - .startObject("number_field5").field("type", "float").endObject() - .startObject("number_field6").field("type", "double").endObject() - .startObject("number_field7").field("type", "ip").endObject() - .startObject("date_field").field("type", "date").endObject() - .endObject().endObject().endObject()); + String mapper = Strings.toString( + XContentFactory.jsonBuilder() + .startObject() + .startObject("doc") + .startObject("properties") + .startObject("field") + .field("type", "text") + .endObject() + .startObject("field1") + .field("type", "text") + .endObject() + .startObject("field2") + .field("type", "text") + .endObject() + .startObject("_field3") + .field("type", "text") + .endObject() + .startObject("field4") + .field("type", "text") + .endObject() + .startObject("number_field1") + .field("type", "integer") + .endObject() + .startObject("number_field2") + .field("type", "long") + .endObject() + .startObject("number_field3") + .field("type", "long") + .endObject() + .startObject("number_field4") + .field("type", "half_float") + .endObject() + .startObject("number_field5") + .field("type", "float") + .endObject() + .startObject("number_field6") + .field("type", "double") + .endObject() + .startObject("number_field7") + .field("type", "ip") + .endObject() + .startObject("date_field") + .field("type", "date") + .endObject() + .endObject() + .endObject() + .endObject() + ); mapperService.merge("doc", new CompressedXContent(mapper), MapperService.MergeReason.MAPPING_UPDATE); } private void addQueryFieldMappings() throws Exception { fieldName = randomAlphaOfLength(4); - String percolatorMapper = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("doc") - .startObject("properties").startObject(fieldName).field("type", "percolator").endObject().endObject() - .endObject().endObject()); + String percolatorMapper = Strings.toString( + XContentFactory.jsonBuilder() + .startObject() + .startObject("doc") + .startObject("properties") + .startObject(fieldName) + .field("type", "percolator") + .endObject() + .endObject() + .endObject() + .endObject() + ); mapperService.merge("doc", new CompressedXContent(percolatorMapper), MapperService.MergeReason.MAPPING_UPDATE); fieldType = (PercolatorFieldMapper.PercolatorFieldType) mapperService.fieldType(fieldName); } @@ -213,11 +254,9 @@ public void testExtractRanges() throws Exception { SearchExecutionContext context = createSearchContext(indexService).getSearchExecutionContext(); addQueryFieldMappings(); BooleanQuery.Builder bq = new BooleanQuery.Builder(); - Query rangeQuery1 = mapperService.fieldType("number_field1") - .rangeQuery(10, 20, true, true, null, null, null, context); + Query rangeQuery1 = mapperService.fieldType("number_field1").rangeQuery(10, 20, true, true, null, null, null, context); bq.add(rangeQuery1, Occur.MUST); - Query rangeQuery2 = mapperService.fieldType("number_field1") - .rangeQuery(15, 20, true, true, null, null, null, context); + Query rangeQuery2 = mapperService.fieldType("number_field1").rangeQuery(15, 20, true, true, null, null, null, context); bq.add(rangeQuery2, Occur.MUST); DocumentMapper documentMapper = mapperService.documentMapper(); @@ -243,8 +282,7 @@ public void testExtractRanges() throws Exception { // Range queries on different fields: bq = new BooleanQuery.Builder(); bq.add(rangeQuery1, Occur.MUST); - rangeQuery2 = mapperService.fieldType("number_field2") - .rangeQuery(15, 20, true, true, null, null, null, context); + rangeQuery2 = mapperService.fieldType("number_field2").rangeQuery(15, 20, true, true, null, null, null, context); bq.add(rangeQuery2, Occur.MUST); documentParserContext = new TestDocumentParserContext(); @@ -335,7 +373,6 @@ public void testExtractTermsAndRanges() throws Exception { assertEquals("field4\u0000123", terms.get(13).utf8ToString()); } - public void testCreateCandidateQuery() throws Exception { int origMaxClauseCount = BooleanQuery.getMaxClauseCount(); try { @@ -433,67 +470,80 @@ public void testExtractTermsAndRanges_numberFields() throws Exception { public void testPercolatorFieldMapper() throws Exception { addQueryFieldMappings(); QueryBuilder queryBuilder = termQuery("field", "value"); - ParsedDocument doc = mapperService.documentMapper().parse(new SourceToParse("test", "1", - BytesReference.bytes(XContentFactory - .jsonBuilder() - .startObject() - .field(fieldName, queryBuilder) - .endObject()), - XContentType.JSON)); + ParsedDocument doc = mapperService.documentMapper() + .parse( + new SourceToParse( + "test", + "1", + BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field(fieldName, queryBuilder).endObject()), + XContentType.JSON + ) + ); assertThat(doc.rootDoc().getFields(fieldType.queryTermsField.name()).length, equalTo(1)); assertThat(doc.rootDoc().getFields(fieldType.queryTermsField.name())[0].binaryValue().utf8ToString(), equalTo("field\0value")); assertThat(doc.rootDoc().getFields(fieldType.queryBuilderField.name()).length, equalTo(1)); assertThat(doc.rootDoc().getFields(fieldType.extractionResultField.name()).length, equalTo(1)); - assertThat(doc.rootDoc().getFields(fieldType.extractionResultField.name())[0].stringValue(), - equalTo(EXTRACTION_COMPLETE)); + assertThat(doc.rootDoc().getFields(fieldType.extractionResultField.name())[0].stringValue(), equalTo(EXTRACTION_COMPLETE)); BytesRef qbSource = doc.rootDoc().getFields(fieldType.queryBuilderField.name())[0].binaryValue(); assertQueryBuilder(qbSource, queryBuilder); // add an query for which we don't extract terms from queryBuilder = rangeQuery("field").from("a").to("z"); - doc = mapperService.documentMapper().parse(new SourceToParse("test", "1", BytesReference.bytes(XContentFactory - .jsonBuilder() - .startObject() - .field(fieldName, queryBuilder) - .endObject()), - XContentType.JSON)); + doc = mapperService.documentMapper() + .parse( + new SourceToParse( + "test", + "1", + BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field(fieldName, queryBuilder).endObject()), + XContentType.JSON + ) + ); assertThat(doc.rootDoc().getFields(fieldType.extractionResultField.name()).length, equalTo(1)); - assertThat(doc.rootDoc().getFields(fieldType.extractionResultField.name())[0].stringValue(), - equalTo(EXTRACTION_FAILED)); + assertThat(doc.rootDoc().getFields(fieldType.extractionResultField.name())[0].stringValue(), equalTo(EXTRACTION_FAILED)); assertThat(doc.rootDoc().getFields(fieldType.queryTermsField.name()).length, equalTo(0)); assertThat(doc.rootDoc().getFields(fieldType.queryBuilderField.name()).length, equalTo(1)); qbSource = doc.rootDoc().getFields(fieldType.queryBuilderField.name())[0].binaryValue(); assertQueryBuilder(qbSource, queryBuilder); queryBuilder = rangeQuery("date_field").from("now"); - doc = mapperService.documentMapper().parse(new SourceToParse("test", "1", BytesReference.bytes(XContentFactory - .jsonBuilder() - .startObject() - .field(fieldName, queryBuilder) - .endObject()), - XContentType.JSON)); + doc = mapperService.documentMapper() + .parse( + new SourceToParse( + "test", + "1", + BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field(fieldName, queryBuilder).endObject()), + XContentType.JSON + ) + ); assertThat(doc.rootDoc().getFields(fieldType.extractionResultField.name()).length, equalTo(1)); - assertThat(doc.rootDoc().getFields(fieldType.extractionResultField.name())[0].stringValue(), - equalTo(EXTRACTION_FAILED)); + assertThat(doc.rootDoc().getFields(fieldType.extractionResultField.name())[0].stringValue(), equalTo(EXTRACTION_FAILED)); } public void testStoringQueries() throws Exception { addQueryFieldMappings(); - QueryBuilder[] queries = new QueryBuilder[]{ - termQuery("field", "value"), matchAllQuery(), matchQuery("field", "value"), matchPhraseQuery("field", "value"), - prefixQuery("field", "v"), wildcardQuery("field", "v*"), rangeQuery("number_field2").gte(0).lte(9), - rangeQuery("date_field").from("2015-01-01T00:00").to("2015-01-01T00:00") - }; + QueryBuilder[] queries = new QueryBuilder[] { + termQuery("field", "value"), + matchAllQuery(), + matchQuery("field", "value"), + matchPhraseQuery("field", "value"), + prefixQuery("field", "v"), + wildcardQuery("field", "v*"), + rangeQuery("number_field2").gte(0).lte(9), + rangeQuery("date_field").from("2015-01-01T00:00").to("2015-01-01T00:00") }; // note: it important that range queries never rewrite, otherwise it will cause results to be wrong. // (it can't use shard data for rewriting purposes, because percolator queries run on MemoryIndex) for (QueryBuilder query : queries) { - ParsedDocument doc = mapperService.documentMapper().parse(new SourceToParse("test", "1", - BytesReference.bytes(XContentFactory.jsonBuilder().startObject() - .field(fieldName, query) - .endObject()), - XContentType.JSON)); + ParsedDocument doc = mapperService.documentMapper() + .parse( + new SourceToParse( + "test", + "1", + BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field(fieldName, query).endObject()), + XContentType.JSON + ) + ); BytesRef qbSource = doc.rootDoc().getFields(fieldType.queryBuilderField.name())[0].binaryValue(); assertQueryBuilder(qbSource, query); } @@ -503,56 +553,71 @@ public void testQueryWithRewrite() throws Exception { addQueryFieldMappings(); client().prepareIndex("remote").setId("1").setSource("field", "value").get(); QueryBuilder queryBuilder = termsLookupQuery("field", new TermsLookup("remote", "1", "field")); - ParsedDocument doc = mapperService.documentMapper().parse(new SourceToParse("test", "1", - BytesReference.bytes(XContentFactory - .jsonBuilder() - .startObject() - .field(fieldName, queryBuilder) - .endObject()), - XContentType.JSON)); + ParsedDocument doc = mapperService.documentMapper() + .parse( + new SourceToParse( + "test", + "1", + BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field(fieldName, queryBuilder).endObject()), + XContentType.JSON + ) + ); BytesRef qbSource = doc.rootDoc().getFields(fieldType.queryBuilderField.name())[0].binaryValue(); SearchExecutionContext searchExecutionContext = indexService.newSearchExecutionContext( - randomInt(20), 0, null, () -> { - throw new UnsupportedOperationException(); - }, null, emptyMap()); + randomInt(20), + 0, + null, + () -> { throw new UnsupportedOperationException(); }, + null, + emptyMap() + ); PlainActionFuture future = new PlainActionFuture<>(); Rewriteable.rewriteAndFetch(queryBuilder, searchExecutionContext, future); assertQueryBuilder(qbSource, future.get()); } - public void testPercolatorFieldMapperUnMappedField() throws Exception { addQueryFieldMappings(); MapperParsingException exception = expectThrows(MapperParsingException.class, () -> { - mapperService.documentMapper().parse(new SourceToParse("test", "1", BytesReference.bytes(XContentFactory - .jsonBuilder() - .startObject() - .field(fieldName, termQuery("unmapped_field", "value")) - .endObject()), - XContentType.JSON)); + mapperService.documentMapper() + .parse( + new SourceToParse( + "test", + "1", + BytesReference.bytes( + XContentFactory.jsonBuilder().startObject().field(fieldName, termQuery("unmapped_field", "value")).endObject() + ), + XContentType.JSON + ) + ); }); assertThat(exception.getCause(), instanceOf(QueryShardException.class)); assertThat(exception.getCause().getMessage(), equalTo("No field mapping can be found for the field with name [unmapped_field]")); } - public void testPercolatorFieldMapper_noQuery() throws Exception { addQueryFieldMappings(); - ParsedDocument doc = mapperService.documentMapper().parse(new SourceToParse("test", "1", BytesReference - .bytes(XContentFactory - .jsonBuilder() - .startObject() - .endObject()), - XContentType.JSON)); + ParsedDocument doc = mapperService.documentMapper() + .parse( + new SourceToParse( + "test", + "1", + BytesReference.bytes(XContentFactory.jsonBuilder().startObject().endObject()), + XContentType.JSON + ) + ); assertThat(doc.rootDoc().getFields(fieldType.queryBuilderField.name()).length, equalTo(0)); try { - mapperService.documentMapper().parse(new SourceToParse("test", "1", BytesReference.bytes(XContentFactory - .jsonBuilder() - .startObject() - .nullField(fieldName) - .endObject()), - XContentType.JSON)); + mapperService.documentMapper() + .parse( + new SourceToParse( + "test", + "1", + BytesReference.bytes(XContentFactory.jsonBuilder().startObject().nullField(fieldName).endObject()), + XContentType.JSON + ) + ); } catch (MapperParsingException e) { assertThat(e.getDetailedMessage(), containsString("query malformed, must start with start_object")); } @@ -563,32 +628,58 @@ public void testAllowNoAdditionalSettings() throws Exception { IndexService indexService = createIndex("test1", Settings.EMPTY); MapperService mapperService = indexService.mapperService(); - String percolatorMapper = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("doc") - .startObject("properties").startObject(fieldName).field("type", "percolator").field("index", "no").endObject().endObject() - .endObject().endObject()); - MapperParsingException e = expectThrows(MapperParsingException.class, () -> - mapperService.merge("doc", new CompressedXContent(percolatorMapper), MapperService.MergeReason.MAPPING_UPDATE)); + String percolatorMapper = Strings.toString( + XContentFactory.jsonBuilder() + .startObject() + .startObject("doc") + .startObject("properties") + .startObject(fieldName) + .field("type", "percolator") + .field("index", "no") + .endObject() + .endObject() + .endObject() + .endObject() + ); + MapperParsingException e = expectThrows( + MapperParsingException.class, + () -> mapperService.merge("doc", new CompressedXContent(percolatorMapper), MapperService.MergeReason.MAPPING_UPDATE) + ); assertThat(e.getMessage(), containsString("Mapping definition for [" + fieldName + "] has unsupported parameters: [index : no]")); } // multiple percolator fields are allowed in the mapping, but only one field can be used at index time. public void testMultiplePercolatorFields() throws Exception { String typeName = "doc"; - String percolatorMapper = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject(typeName) + String percolatorMapper = Strings.toString( + XContentFactory.jsonBuilder() + .startObject() + .startObject(typeName) .startObject("properties") - .startObject("query_field1").field("type", "percolator").endObject() - .startObject("query_field2").field("type", "percolator").endObject() + .startObject("query_field1") + .field("type", "percolator") + .endObject() + .startObject("query_field2") + .field("type", "percolator") + .endObject() + .endObject() + .endObject() .endObject() - .endObject().endObject()); + ); mapperService.merge(typeName, new CompressedXContent(percolatorMapper), MapperService.MergeReason.MAPPING_UPDATE); QueryBuilder queryBuilder = matchQuery("field", "value"); - ParsedDocument doc = mapperService.documentMapper().parse(new SourceToParse("test", "1", - BytesReference.bytes(jsonBuilder().startObject() - .field("query_field1", queryBuilder) - .field("query_field2", queryBuilder) - .endObject()), - XContentType.JSON)); + ParsedDocument doc = mapperService.documentMapper() + .parse( + new SourceToParse( + "test", + "1", + BytesReference.bytes( + jsonBuilder().startObject().field("query_field1", queryBuilder).field("query_field2", queryBuilder).endObject() + ), + XContentType.JSON + ) + ); assertThat(doc.rootDoc().getFields().size(), equalTo(16)); // also includes all other meta fields BytesRef queryBuilderAsBytes = doc.rootDoc().getField("query_field1.query_builder_field").binaryValue(); assertQueryBuilder(queryBuilderAsBytes, queryBuilder); @@ -600,24 +691,37 @@ public void testMultiplePercolatorFields() throws Exception { // percolator field can be nested under an object field, but only one query can be specified per document public void testNestedPercolatorField() throws Exception { String typeName = "doc"; - String percolatorMapper = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject(typeName) + String percolatorMapper = Strings.toString( + XContentFactory.jsonBuilder() + .startObject() + .startObject(typeName) .startObject("properties") .startObject("object_field") - .field("type", "object") - .startObject("properties") - .startObject("query_field").field("type", "percolator").endObject() - .endObject() + .field("type", "object") + .startObject("properties") + .startObject("query_field") + .field("type", "percolator") + .endObject() + .endObject() .endObject() .endObject() - .endObject().endObject()); + .endObject() + .endObject() + ); mapperService.merge(typeName, new CompressedXContent(percolatorMapper), MapperService.MergeReason.MAPPING_UPDATE); QueryBuilder queryBuilder = matchQuery("field", "value"); - ParsedDocument doc = mapperService.documentMapper().parse(new SourceToParse("test", "1", - BytesReference.bytes(jsonBuilder().startObject().startObject("object_field") - .field("query_field", queryBuilder) - .endObject().endObject()), - XContentType.JSON)); + ParsedDocument doc = mapperService.documentMapper() + .parse( + new SourceToParse( + "test", + "1", + BytesReference.bytes( + jsonBuilder().startObject().startObject("object_field").field("query_field", queryBuilder).endObject().endObject() + ), + XContentType.JSON + ) + ); assertThat(doc.rootDoc().getFields().size(), equalTo(11)); // also includes all other meta fields IndexableField queryBuilderField = doc.rootDoc().getField("object_field.query_field.query_builder_field"); assertTrue(queryBuilderField.fieldType().omitNorms()); @@ -626,28 +730,49 @@ public void testNestedPercolatorField() throws Exception { BytesRef queryBuilderAsBytes = queryBuilderField.binaryValue(); assertQueryBuilder(queryBuilderAsBytes, queryBuilder); - doc = mapperService.documentMapper().parse(new SourceToParse("test", "1", - BytesReference.bytes(jsonBuilder().startObject() + doc = mapperService.documentMapper() + .parse( + new SourceToParse( + "test", + "1", + BytesReference.bytes( + jsonBuilder().startObject() .startArray("object_field") - .startObject().field("query_field", queryBuilder).endObject() + .startObject() + .field("query_field", queryBuilder) + .endObject() .endArray() - .endObject()), - XContentType.JSON)); + .endObject() + ), + XContentType.JSON + ) + ); assertThat(doc.rootDoc().getFields().size(), equalTo(11)); // also includes all other meta fields queryBuilderAsBytes = doc.rootDoc().getField("object_field.query_field.query_builder_field").binaryValue(); assertQueryBuilder(queryBuilderAsBytes, queryBuilder); MapperParsingException e = expectThrows(MapperParsingException.class, () -> { - mapperService.documentMapper().parse(new SourceToParse("test", "1", - BytesReference.bytes(jsonBuilder().startObject() - .startArray("object_field") - .startObject().field("query_field", queryBuilder).endObject() - .startObject().field("query_field", queryBuilder).endObject() - .endArray() - .endObject()), - XContentType.JSON)); - } - ); + mapperService.documentMapper() + .parse( + new SourceToParse( + "test", + "1", + BytesReference.bytes( + jsonBuilder().startObject() + .startArray("object_field") + .startObject() + .field("query_field", queryBuilder) + .endObject() + .startObject() + .field("query_field", queryBuilder) + .endObject() + .endArray() + .endObject() + ), + XContentType.JSON + ) + ); + }); assertThat(e.getCause(), instanceOf(IllegalArgumentException.class)); assertThat(e.getCause().getMessage(), equalTo("a document can only contain one percolator query")); } @@ -659,19 +784,20 @@ public void testUnsupportedQueries() { PercolatorFieldMapper.verifyQuery(rangeQuery2); HasChildQueryBuilder hasChildQuery = new HasChildQueryBuilder("parent", new MatchAllQueryBuilder(), ScoreMode.None); - expectThrows(IllegalArgumentException.class, () -> - PercolatorFieldMapper.verifyQuery(new BoolQueryBuilder().must(hasChildQuery))); - expectThrows(IllegalArgumentException.class, () -> - PercolatorFieldMapper.verifyQuery(new DisMaxQueryBuilder().add(hasChildQuery))); + expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new BoolQueryBuilder().must(hasChildQuery))); + expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new DisMaxQueryBuilder().add(hasChildQuery))); PercolatorFieldMapper.verifyQuery(new ConstantScoreQueryBuilder((rangeQuery1))); - expectThrows(IllegalArgumentException.class, () -> - PercolatorFieldMapper.verifyQuery(new ConstantScoreQueryBuilder(hasChildQuery))); + expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new ConstantScoreQueryBuilder(hasChildQuery))); PercolatorFieldMapper.verifyQuery(new BoostingQueryBuilder(rangeQuery1, new MatchAllQueryBuilder())); - expectThrows(IllegalArgumentException.class, () -> - PercolatorFieldMapper.verifyQuery(new BoostingQueryBuilder(hasChildQuery, new MatchAllQueryBuilder()))); + expectThrows( + IllegalArgumentException.class, + () -> PercolatorFieldMapper.verifyQuery(new BoostingQueryBuilder(hasChildQuery, new MatchAllQueryBuilder())) + ); PercolatorFieldMapper.verifyQuery(new FunctionScoreQueryBuilder(rangeQuery1, new RandomScoreFunctionBuilder())); - expectThrows(IllegalArgumentException.class, () -> - PercolatorFieldMapper.verifyQuery(new FunctionScoreQueryBuilder(hasChildQuery, new RandomScoreFunctionBuilder()))); + expectThrows( + IllegalArgumentException.class, + () -> PercolatorFieldMapper.verifyQuery(new FunctionScoreQueryBuilder(hasChildQuery, new RandomScoreFunctionBuilder())) + ); expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(hasChildQuery)); expectThrows(IllegalArgumentException.class, () -> PercolatorFieldMapper.verifyQuery(new BoolQueryBuilder().must(hasChildQuery))); @@ -697,10 +823,20 @@ private void assertQueryBuilder(BytesRef actual, QueryBuilder expected) throws I } public void testEmptyName() throws Exception { - String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type1") - .startObject("properties").startObject("").field("type", "percolator").endObject().endObject() - .endObject().endObject()); - MapperParsingException e = expectThrows(MapperParsingException.class, + String mapping = Strings.toString( + XContentFactory.jsonBuilder() + .startObject() + .startObject("type1") + .startObject("properties") + .startObject("") + .field("type", "percolator") + .endObject() + .endObject() + .endObject() + .endObject() + ); + MapperParsingException e = expectThrows( + MapperParsingException.class, () -> mapperService.parseMapping("type1", new CompressedXContent(mapping)) ); assertThat(e.getMessage(), containsString("name cannot be empty string")); @@ -721,11 +857,20 @@ public void testImplicitlySetDefaultScriptLang() throws Exception { query.endObject(); query.endObject(); - ParsedDocument doc = mapperService.documentMapper().parse(new SourceToParse("test", "1", - BytesReference.bytes(XContentFactory.jsonBuilder().startObject() - .rawField(fieldName, new BytesArray(Strings.toString(query)).streamInput(), query.contentType()) - .endObject()), - XContentType.JSON)); + ParsedDocument doc = mapperService.documentMapper() + .parse( + new SourceToParse( + "test", + "1", + BytesReference.bytes( + XContentFactory.jsonBuilder() + .startObject() + .rawField(fieldName, new BytesArray(Strings.toString(query)).streamInput(), query.contentType()) + .endObject() + ), + XContentType.JSON + ) + ); BytesRef querySource = doc.rootDoc().getFields(fieldType.queryBuilderField.name())[0].binaryValue(); try (InputStream in = new ByteArrayInputStream(querySource.bytes, querySource.offset, querySource.length)) { try (StreamInput input = new NamedWriteableAwareStreamInput(new InputStreamStreamInput(in), writableRegistry())) { @@ -759,19 +904,28 @@ public void testImplicitlySetDefaultScriptLang() throws Exception { query.endObject(); query.endObject(); - doc = mapperService.documentMapper().parse(new SourceToParse("test", "1", - BytesReference.bytes(XContentFactory.jsonBuilder().startObject() - .rawField(fieldName, new BytesArray(Strings.toString(query)).streamInput(), query.contentType()) - .endObject()), - XContentType.JSON)); + doc = mapperService.documentMapper() + .parse( + new SourceToParse( + "test", + "1", + BytesReference.bytes( + XContentFactory.jsonBuilder() + .startObject() + .rawField(fieldName, new BytesArray(Strings.toString(query)).streamInput(), query.contentType()) + .endObject() + ), + XContentType.JSON + ) + ); querySource = doc.rootDoc().getFields(fieldType.queryBuilderField.name())[0].binaryValue(); try (InputStream in = new ByteArrayInputStream(querySource.bytes, querySource.offset, querySource.length)) { try (StreamInput input = new NamedWriteableAwareStreamInput(new InputStreamStreamInput(in), writableRegistry())) { input.readVInt(); input.readVInt(); FunctionScoreQueryBuilder queryBuilder = (FunctionScoreQueryBuilder) input.readNamedWriteable(QueryBuilder.class); - ScriptScoreFunctionBuilder function = (ScriptScoreFunctionBuilder) - queryBuilder.filterFunctionBuilders()[0].getScoreFunction(); + ScriptScoreFunctionBuilder function = (ScriptScoreFunctionBuilder) queryBuilder.filterFunctionBuilders()[0] + .getScoreFunction(); assertEquals(Script.DEFAULT_SCRIPT_LANG, function.getScript().getLang()); } } @@ -842,19 +996,22 @@ public void testEncodeRange() { public void testDuplicatedClauses() throws Exception { addQueryFieldMappings(); - QueryBuilder qb = boolQuery() - .must(boolQuery().must(termQuery("field", "value1")).must(termQuery("field", "value2"))) - .must(boolQuery().must(termQuery("field", "value2")).must(termQuery("field", "value3"))); - ParsedDocument doc = mapperService.documentMapper().parse(new SourceToParse("test", "1", - BytesReference.bytes(XContentFactory.jsonBuilder().startObject() - .field(fieldName, qb) - .endObject()), - XContentType.JSON)); + QueryBuilder qb = boolQuery().must(boolQuery().must(termQuery("field", "value1")).must(termQuery("field", "value2"))) + .must(boolQuery().must(termQuery("field", "value2")).must(termQuery("field", "value3"))); + ParsedDocument doc = mapperService.documentMapper() + .parse( + new SourceToParse( + "test", + "1", + BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field(fieldName, qb).endObject()), + XContentType.JSON + ) + ); List values = Arrays.stream(doc.rootDoc().getFields(fieldType.queryTermsField.name())) - .map(f -> f.binaryValue().utf8ToString()) - .sorted() - .collect(Collectors.toList()); + .map(f -> f.binaryValue().utf8ToString()) + .sorted() + .collect(Collectors.toList()); assertThat(values.size(), equalTo(3)); assertThat(values.get(0), equalTo("field\0value1")); assertThat(values.get(1), equalTo("field\0value2")); @@ -862,21 +1019,24 @@ public void testDuplicatedClauses() throws Exception { int msm = doc.rootDoc().getFields(fieldType.minimumShouldMatchField.name())[0].numericValue().intValue(); assertThat(msm, equalTo(3)); - qb = boolQuery() - .must(boolQuery().must(termQuery("field", "value1")).must(termQuery("field", "value2"))) - .must(boolQuery().must(termQuery("field", "value2")).must(termQuery("field", "value3"))) - .must(boolQuery().must(termQuery("field", "value3")).must(termQuery("field", "value4"))) - .must(boolQuery().should(termQuery("field", "value4")).should(termQuery("field", "value5"))); - doc = mapperService.documentMapper().parse(new SourceToParse("test", "1", - BytesReference.bytes(XContentFactory.jsonBuilder().startObject() - .field(fieldName, qb) - .endObject()), - XContentType.JSON)); + qb = boolQuery().must(boolQuery().must(termQuery("field", "value1")).must(termQuery("field", "value2"))) + .must(boolQuery().must(termQuery("field", "value2")).must(termQuery("field", "value3"))) + .must(boolQuery().must(termQuery("field", "value3")).must(termQuery("field", "value4"))) + .must(boolQuery().should(termQuery("field", "value4")).should(termQuery("field", "value5"))); + doc = mapperService.documentMapper() + .parse( + new SourceToParse( + "test", + "1", + BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field(fieldName, qb).endObject()), + XContentType.JSON + ) + ); values = Arrays.stream(doc.rootDoc().getFields(fieldType.queryTermsField.name())) - .map(f -> f.binaryValue().utf8ToString()) - .sorted() - .collect(Collectors.toList()); + .map(f -> f.binaryValue().utf8ToString()) + .sorted() + .collect(Collectors.toList()); assertThat(values.size(), equalTo(5)); assertThat(values.get(0), equalTo("field\0value1")); assertThat(values.get(1), equalTo("field\0value2")); @@ -886,22 +1046,25 @@ public void testDuplicatedClauses() throws Exception { msm = doc.rootDoc().getFields(fieldType.minimumShouldMatchField.name())[0].numericValue().intValue(); assertThat(msm, equalTo(4)); - qb = boolQuery() - .minimumShouldMatch(3) - .should(boolQuery().should(termQuery("field", "value1")).should(termQuery("field", "value2"))) - .should(boolQuery().should(termQuery("field", "value2")).should(termQuery("field", "value3"))) - .should(boolQuery().should(termQuery("field", "value3")).should(termQuery("field", "value4"))) - .should(boolQuery().should(termQuery("field", "value4")).should(termQuery("field", "value5"))); - doc = mapperService.documentMapper().parse(new SourceToParse("test", "1", - BytesReference.bytes(XContentFactory.jsonBuilder().startObject() - .field(fieldName, qb) - .endObject()), - XContentType.JSON)); + qb = boolQuery().minimumShouldMatch(3) + .should(boolQuery().should(termQuery("field", "value1")).should(termQuery("field", "value2"))) + .should(boolQuery().should(termQuery("field", "value2")).should(termQuery("field", "value3"))) + .should(boolQuery().should(termQuery("field", "value3")).should(termQuery("field", "value4"))) + .should(boolQuery().should(termQuery("field", "value4")).should(termQuery("field", "value5"))); + doc = mapperService.documentMapper() + .parse( + new SourceToParse( + "test", + "1", + BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field(fieldName, qb).endObject()), + XContentType.JSON + ) + ); values = Arrays.stream(doc.rootDoc().getFields(fieldType.queryTermsField.name())) - .map(f -> f.binaryValue().utf8ToString()) - .sorted() - .collect(Collectors.toList()); + .map(f -> f.binaryValue().utf8ToString()) + .sorted() + .collect(Collectors.toList()); assertThat(values.size(), equalTo(5)); assertThat(values.get(0), equalTo("field\0value1")); assertThat(values.get(1), equalTo("field\0value2")); diff --git a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorHighlightSubFetchPhaseTests.java b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorHighlightSubFetchPhaseTests.java index c0c70d36c9f5f..fd5f07a718176 100644 --- a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorHighlightSubFetchPhaseTests.java +++ b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorHighlightSubFetchPhaseTests.java @@ -34,8 +34,15 @@ public class PercolatorHighlightSubFetchPhaseTests extends ESTestCase { public void testHitsExecutionNeeded() { - PercolateQuery percolateQuery = new PercolateQuery("_name", ctx -> null, Collections.singletonList(new BytesArray("{}")), - new MatchAllDocsQuery(), Mockito.mock(IndexSearcher.class), null, new MatchAllDocsQuery()); + PercolateQuery percolateQuery = new PercolateQuery( + "_name", + ctx -> null, + Collections.singletonList(new BytesArray("{}")), + new MatchAllDocsQuery(), + Mockito.mock(IndexSearcher.class), + null, + new MatchAllDocsQuery() + ); PercolatorHighlightSubFetchPhase subFetchPhase = new PercolatorHighlightSubFetchPhase(emptyMap()); FetchContext fetchContext = mock(FetchContext.class); Mockito.when(fetchContext.highlight()).thenReturn(new SearchHighlightContext(Collections.emptyList())); @@ -47,8 +54,15 @@ public void testHitsExecutionNeeded() { } public void testLocatePercolatorQuery() { - PercolateQuery percolateQuery = new PercolateQuery("_name", ctx -> null, Collections.singletonList(new BytesArray("{}")), - new MatchAllDocsQuery(), Mockito.mock(IndexSearcher.class), null, new MatchAllDocsQuery()); + PercolateQuery percolateQuery = new PercolateQuery( + "_name", + ctx -> null, + Collections.singletonList(new BytesArray("{}")), + new MatchAllDocsQuery(), + Mockito.mock(IndexSearcher.class), + null, + new MatchAllDocsQuery() + ); assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(new MatchAllDocsQuery()).size(), equalTo(0)); BooleanQuery.Builder bq = new BooleanQuery.Builder(); bq.add(new MatchAllDocsQuery(), BooleanClause.Occur.FILTER); @@ -81,16 +95,25 @@ public void testLocatePercolatorQuery() { assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(disjunctionMaxQuery).size(), equalTo(1)); assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(disjunctionMaxQuery).get(0), sameInstance(percolateQuery)); - PercolateQuery percolateQuery2 = new PercolateQuery("_name", ctx -> null, Collections.singletonList(new BytesArray("{}")), - new MatchAllDocsQuery(), Mockito.mock(IndexSearcher.class), null, new MatchAllDocsQuery()); + PercolateQuery percolateQuery2 = new PercolateQuery( + "_name", + ctx -> null, + Collections.singletonList(new BytesArray("{}")), + new MatchAllDocsQuery(), + Mockito.mock(IndexSearcher.class), + null, + new MatchAllDocsQuery() + ); bq = new BooleanQuery.Builder(); bq.add(new MatchAllDocsQuery(), BooleanClause.Occur.FILTER); assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(bq.build()).size(), equalTo(0)); bq.add(percolateQuery, BooleanClause.Occur.FILTER); bq.add(percolateQuery2, BooleanClause.Occur.FILTER); assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(bq.build()).size(), equalTo(2)); - assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(bq.build()), - containsInAnyOrder(sameInstance(percolateQuery), sameInstance(percolateQuery2))); + assertThat( + PercolatorHighlightSubFetchPhase.locatePercolatorQuery(bq.build()), + containsInAnyOrder(sameInstance(percolateQuery), sameInstance(percolateQuery2)) + ); assertNotNull(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(null)); assertThat(PercolatorHighlightSubFetchPhase.locatePercolatorQuery(null).size(), equalTo(0)); diff --git a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorMatchedSlotSubFetchPhaseTests.java b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorMatchedSlotSubFetchPhaseTests.java index 6c16c3d049c07..99afa2d936cbf 100644 --- a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorMatchedSlotSubFetchPhaseTests.java +++ b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorMatchedSlotSubFetchPhaseTests.java @@ -57,8 +57,15 @@ public void testHitsExecute() throws Exception { MemoryIndex memoryIndex = new MemoryIndex(); memoryIndex.addField("field", "value", new WhitespaceAnalyzer()); memoryIndex.addField(new NumericDocValuesField(SeqNoFieldMapper.PRIMARY_TERM_NAME, 0), null); - PercolateQuery percolateQuery = new PercolateQuery("_name", queryStore, Collections.emptyList(), - new MatchAllDocsQuery(), memoryIndex.createSearcher(), null, new MatchNoDocsQuery()); + PercolateQuery percolateQuery = new PercolateQuery( + "_name", + queryStore, + Collections.emptyList(), + new MatchAllDocsQuery(), + memoryIndex.createSearcher(), + null, + new MatchNoDocsQuery() + ); FetchContext sc = mock(FetchContext.class); when(sc.query()).thenReturn(percolateQuery); @@ -78,8 +85,15 @@ public void testHitsExecute() throws Exception { MemoryIndex memoryIndex = new MemoryIndex(); memoryIndex.addField("field", "value1", new WhitespaceAnalyzer()); memoryIndex.addField(new NumericDocValuesField(SeqNoFieldMapper.PRIMARY_TERM_NAME, 0), null); - PercolateQuery percolateQuery = new PercolateQuery("_name", queryStore, Collections.emptyList(), - new MatchAllDocsQuery(), memoryIndex.createSearcher(), null, new MatchNoDocsQuery()); + PercolateQuery percolateQuery = new PercolateQuery( + "_name", + queryStore, + Collections.emptyList(), + new MatchAllDocsQuery(), + memoryIndex.createSearcher(), + null, + new MatchNoDocsQuery() + ); FetchContext sc = mock(FetchContext.class); when(sc.query()).thenReturn(percolateQuery); @@ -98,8 +112,15 @@ public void testHitsExecute() throws Exception { MemoryIndex memoryIndex = new MemoryIndex(); memoryIndex.addField("field", "value", new WhitespaceAnalyzer()); memoryIndex.addField(new NumericDocValuesField(SeqNoFieldMapper.PRIMARY_TERM_NAME, 0), null); - PercolateQuery percolateQuery = new PercolateQuery("_name", queryStore, Collections.emptyList(), - new MatchAllDocsQuery(), memoryIndex.createSearcher(), null, new MatchNoDocsQuery()); + PercolateQuery percolateQuery = new PercolateQuery( + "_name", + queryStore, + Collections.emptyList(), + new MatchAllDocsQuery(), + memoryIndex.createSearcher(), + null, + new MatchNoDocsQuery() + ); FetchContext sc = mock(FetchContext.class); when(sc.query()).thenReturn(percolateQuery); diff --git a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorQuerySearchTests.java b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorQuerySearchTests.java index 4225c7cd59ea6..d429050b21a64 100644 --- a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorQuerySearchTests.java +++ b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorQuerySearchTests.java @@ -13,9 +13,6 @@ import org.elasticsearch.action.support.WriteRequest; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.IndexService; import org.elasticsearch.index.cache.bitset.BitsetFilterCache; import org.elasticsearch.index.engine.Engine; @@ -31,6 +28,9 @@ import org.elasticsearch.search.lookup.LeafDocLookup; import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.test.ESSingleNodeTestCase; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentType; import java.io.IOException; import java.util.Arrays; @@ -41,7 +41,6 @@ import java.util.function.Function; import static java.util.Collections.emptyMap; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.index.query.QueryBuilders.boolQuery; import static org.elasticsearch.index.query.QueryBuilders.matchQuery; import static org.elasticsearch.index.query.QueryBuilders.rangeQuery; @@ -49,6 +48,7 @@ import static org.elasticsearch.index.query.QueryBuilders.termQuery; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.Matchers.equalTo; public class PercolatorQuerySearchTests extends ESSingleNodeTestCase { @@ -74,14 +74,27 @@ protected Map, Object>> pluginScripts() { public void testPercolateScriptQuery() throws IOException { client().admin().indices().prepareCreate("index").setMapping("query", "type=percolator").get(); - client().prepareIndex("index").setId("1") - .setSource(jsonBuilder().startObject().field("query", QueryBuilders.scriptQuery( - new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "1==1", Collections.emptyMap()))).endObject()) + client().prepareIndex("index") + .setId("1") + .setSource( + jsonBuilder().startObject() + .field( + "query", + QueryBuilders.scriptQuery(new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "1==1", Collections.emptyMap())) + ) + .endObject() + ) .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .execute().actionGet(); + .execute() + .actionGet(); SearchResponse response = client().prepareSearch("index") - .setQuery(new PercolateQueryBuilder("query", BytesReference.bytes(jsonBuilder().startObject().field("field1", "b").endObject()), - XContentType.JSON)) + .setQuery( + new PercolateQueryBuilder( + "query", + BytesReference.bytes(jsonBuilder().startObject().field("field1", "b").endObject()), + XContentType.JSON + ) + ) .get(); assertHitCount(response, 1); assertSearchHits(response, "1"); @@ -89,33 +102,72 @@ public void testPercolateScriptQuery() throws IOException { public void testPercolateQueryWithNestedDocuments_doNotLeakBitsetCacheEntries() throws Exception { XContentBuilder mapping = XContentFactory.jsonBuilder(); - mapping.startObject().startObject("properties").startObject("companyname").field("type", "text").endObject() - .startObject("query").field("type", "percolator").endObject() - .startObject("employee").field("type", "nested").startObject("properties") - .startObject("name").field("type", "text").endObject().endObject().endObject().endObject() + mapping.startObject() + .startObject("properties") + .startObject("companyname") + .field("type", "text") + .endObject() + .startObject("query") + .field("type", "percolator") + .endObject() + .startObject("employee") + .field("type", "nested") + .startObject("properties") + .startObject("name") + .field("type", "text") + .endObject() + .endObject() + .endObject() + .endObject() .endObject(); - createIndex("test", client().admin().indices().prepareCreate("test") - // to avoid normal document from being cached by BitsetFilterCache - .setSettings(Settings.builder().put(BitsetFilterCache.INDEX_LOAD_RANDOM_ACCESS_FILTERS_EAGERLY_SETTING.getKey(), false)) - .setMapping(mapping) + createIndex( + "test", + client().admin() + .indices() + .prepareCreate("test") + // to avoid normal document from being cached by BitsetFilterCache + .setSettings(Settings.builder().put(BitsetFilterCache.INDEX_LOAD_RANDOM_ACCESS_FILTERS_EAGERLY_SETTING.getKey(), false)) + .setMapping(mapping) ); - client().prepareIndex("test").setId("q1").setSource(jsonBuilder().startObject() - .field("query", QueryBuilders.nestedQuery("employee", - matchQuery("employee.name", "virginia potts").operator(Operator.AND), ScoreMode.Avg) - ).endObject()) + client().prepareIndex("test") + .setId("q1") + .setSource( + jsonBuilder().startObject() + .field( + "query", + QueryBuilders.nestedQuery( + "employee", + matchQuery("employee.name", "virginia potts").operator(Operator.AND), + ScoreMode.Avg + ) + ) + .endObject() + ) .get(); client().admin().indices().prepareRefresh().get(); for (int i = 0; i < 32; i++) { SearchResponse response = client().prepareSearch() - .setQuery(new PercolateQueryBuilder("query", - BytesReference.bytes(XContentFactory.jsonBuilder() - .startObject().field("companyname", "stark") - .startArray("employee") - .startObject().field("name", "virginia potts").endObject() - .startObject().field("name", "tony stark").endObject() - .endArray() - .endObject()), XContentType.JSON)) + .setQuery( + new PercolateQueryBuilder( + "query", + BytesReference.bytes( + XContentFactory.jsonBuilder() + .startObject() + .field("companyname", "stark") + .startArray("employee") + .startObject() + .field("name", "virginia potts") + .endObject() + .startObject() + .field("name", "tony stark") + .endObject() + .endArray() + .endObject() + ), + XContentType.JSON + ) + ) .addSort("_doc", SortOrder.ASC) // size 0, because other wise load bitsets for normal document in FetchPhase#findRootDocumentIfNested(...) .setSize(0) @@ -126,8 +178,7 @@ public void testPercolateQueryWithNestedDocuments_doNotLeakBitsetCacheEntries() // We can't check via api... because BitsetCacheListener requires that it can extract shardId from index reader // and for percolator it can't do that, but that means we don't keep track of // memory for BitsetCache in case of percolator - long bitsetSize = client().admin().cluster().prepareClusterStats().get() - .getIndicesStats().getSegments().getBitsetMemoryInBytes(); + long bitsetSize = client().admin().cluster().prepareClusterStats().get().getIndicesStats().getSegments().getBitsetMemoryInBytes(); assertEquals("The percolator works with in-memory index and therefor shouldn't use bitset cache", 0L, bitsetSize); } @@ -164,14 +215,16 @@ public void testPercolateQueryWithNestedDocuments_doLeakFieldDataCacheEntries() mapping.endObject(); } mapping.endObject(); - createIndex("test", client().admin().indices().prepareCreate("test") - .setMapping(mapping) - ); + createIndex("test", client().admin().indices().prepareCreate("test").setMapping(mapping)); Script script = new Script(ScriptType.INLINE, MockScriptPlugin.NAME, "use_fielddata_please", Collections.emptyMap()); - client().prepareIndex("test").setId("q1").setSource(jsonBuilder().startObject() - .field("query", QueryBuilders.nestedQuery("employees", - QueryBuilders.scriptQuery(script), ScoreMode.Avg) - ).endObject()).get(); + client().prepareIndex("test") + .setId("q1") + .setSource( + jsonBuilder().startObject() + .field("query", QueryBuilders.nestedQuery("employees", QueryBuilders.scriptQuery(script), ScoreMode.Avg)) + .endObject() + ) + .get(); client().admin().indices().prepareRefresh().get(); XContentBuilder doc = jsonBuilder(); doc.startObject(); @@ -199,70 +252,92 @@ public void testPercolateQueryWithNestedDocuments_doLeakFieldDataCacheEntries() assertHitCount(response, 1); } - long fieldDataSize = client().admin().cluster().prepareClusterStats().get() - .getIndicesStats().getFieldData().getMemorySizeInBytes(); + long fieldDataSize = client().admin().cluster().prepareClusterStats().get().getIndicesStats().getFieldData().getMemorySizeInBytes(); assertEquals("The percolator works with in-memory index and therefor shouldn't use field-data cache", 0L, fieldDataSize); } public void testMapUnmappedFieldAsText() throws IOException { - Settings.Builder settings = Settings.builder() - .put("index.percolator.map_unmapped_fields_as_text", true); + Settings.Builder settings = Settings.builder().put("index.percolator.map_unmapped_fields_as_text", true); createIndex("test", settings.build(), "query", "query", "type=percolator"); - client().prepareIndex("test").setId("1") - .setSource(jsonBuilder().startObject().field("query", matchQuery("field1", "value")).endObject()).get(); + client().prepareIndex("test") + .setId("1") + .setSource(jsonBuilder().startObject().field("query", matchQuery("field1", "value")).endObject()) + .get(); client().admin().indices().prepareRefresh().get(); SearchResponse response = client().prepareSearch("test") - .setQuery(new PercolateQueryBuilder("query", - BytesReference.bytes(jsonBuilder().startObject().field("field1", "value").endObject()), - XContentType.JSON)) + .setQuery( + new PercolateQueryBuilder( + "query", + BytesReference.bytes(jsonBuilder().startObject().field("field1", "value").endObject()), + XContentType.JSON + ) + ) .get(); assertHitCount(response, 1); assertSearchHits(response, "1"); } public void testRangeQueriesWithNow() throws Exception { - IndexService indexService = createIndex("test", Settings.builder().put("index.number_of_shards", 1).build(), "_doc", - "field1", "type=keyword", "field2", "type=date", "query", "type=percolator"); + IndexService indexService = createIndex( + "test", + Settings.builder().put("index.number_of_shards", 1).build(), + "_doc", + "field1", + "type=keyword", + "field2", + "type=date", + "query", + "type=percolator" + ); - client().prepareIndex("test").setId("1") + client().prepareIndex("test") + .setId("1") .setSource(jsonBuilder().startObject().field("query", rangeQuery("field2").from("now-1h").to("now+1h")).endObject()) .get(); - client().prepareIndex("test").setId("2") - .setSource(jsonBuilder().startObject().field("query", boolQuery() - .filter(termQuery("field1", "value")) - .filter(rangeQuery("field2").from("now-1h").to("now+1h")) - ).endObject()) + client().prepareIndex("test") + .setId("2") + .setSource( + jsonBuilder().startObject() + .field( + "query", + boolQuery().filter(termQuery("field1", "value")).filter(rangeQuery("field2").from("now-1h").to("now+1h")) + ) + .endObject() + ) .get(); - Script script = new Script(ScriptType.INLINE, MockScriptPlugin.NAME, "1==1", Collections.emptyMap()); - client().prepareIndex("test").setId("3") - .setSource(jsonBuilder().startObject().field("query", boolQuery() - .filter(scriptQuery(script)) - .filter(rangeQuery("field2").from("now-1h").to("now+1h")) - ).endObject()) + client().prepareIndex("test") + .setId("3") + .setSource( + jsonBuilder().startObject() + .field("query", boolQuery().filter(scriptQuery(script)).filter(rangeQuery("field2").from("now-1h").to("now+1h"))) + .endObject() + ) .get(); client().admin().indices().prepareRefresh().get(); try (Engine.Searcher searcher = indexService.getShard(0).acquireSearcher("test")) { - long[] currentTime = new long[] {System.currentTimeMillis()}; - SearchExecutionContext searchExecutionContext = - indexService.newSearchExecutionContext(0, 0, searcher, () -> currentTime[0], null, emptyMap()); + long[] currentTime = new long[] { System.currentTimeMillis() }; + SearchExecutionContext searchExecutionContext = indexService.newSearchExecutionContext( + 0, + 0, + searcher, + () -> currentTime[0], + null, + emptyMap() + ); - BytesReference source = BytesReference.bytes(jsonBuilder().startObject() - .field("field1", "value") - .field("field2", currentTime[0]) - .endObject()); + BytesReference source = BytesReference.bytes( + jsonBuilder().startObject().field("field1", "value").field("field2", currentTime[0]).endObject() + ); QueryBuilder queryBuilder = new PercolateQueryBuilder("query", source, XContentType.JSON); Query query = queryBuilder.toQuery(searchExecutionContext); assertThat(searcher.count(query), equalTo(3)); currentTime[0] = currentTime[0] + 10800000; // + 3 hours - source = BytesReference.bytes(jsonBuilder().startObject() - .field("field1", "value") - .field("field2", currentTime[0]) - .endObject()); + source = BytesReference.bytes(jsonBuilder().startObject().field("field1", "value").field("field2", currentTime[0]).endObject()); queryBuilder = new PercolateQueryBuilder("query", source, XContentType.JSON); query = queryBuilder.toQuery(searchExecutionContext); assertThat(searcher.count(query), equalTo(3)); diff --git a/modules/percolator/src/test/java/org/elasticsearch/percolator/QueryAnalyzerTests.java b/modules/percolator/src/test/java/org/elasticsearch/percolator/QueryAnalyzerTests.java index acbd6d9df76bd..d30095466ad1e 100644 --- a/modules/percolator/src/test/java/org/elasticsearch/percolator/QueryAnalyzerTests.java +++ b/modules/percolator/src/test/java/org/elasticsearch/percolator/QueryAnalyzerTests.java @@ -109,11 +109,10 @@ public void testExtractQueryMetadata_phraseQuery() { } public void testExtractQueryMetadata_multiPhraseQuery() { - MultiPhraseQuery multiPhraseQuery = new MultiPhraseQuery.Builder() - .add(new Term("_field", "_term1")) - .add(new Term[] {new Term("_field", "_term2"), new Term("_field", "_term3")}) - .add(new Term[] {new Term("_field", "_term4"), new Term("_field", "_term5")}) - .add(new Term[] {new Term("_field", "_term6")}) + MultiPhraseQuery multiPhraseQuery = new MultiPhraseQuery.Builder().add(new Term("_field", "_term1")) + .add(new Term[] { new Term("_field", "_term2"), new Term("_field", "_term3") }) + .add(new Term[] { new Term("_field", "_term4"), new Term("_field", "_term5") }) + .add(new Term[] { new Term("_field", "_term6") }) .build(); Result result = analyze(multiPhraseQuery); assertThat(result.verified, is(false)); @@ -136,9 +135,8 @@ public void testExtractQueryMetadata_multiPhraseQuery() { } public void testExtractQueryMetadata_multiPhraseQuery_dups() { - MultiPhraseQuery multiPhraseQuery = new MultiPhraseQuery.Builder() - .add(new Term("_field", "_term1")) - .add(new Term[] {new Term("_field", "_term1"), new Term("_field", "_term2")}) + MultiPhraseQuery multiPhraseQuery = new MultiPhraseQuery.Builder().add(new Term("_field", "_term1")) + .add(new Term[] { new Term("_field", "_term1"), new Term("_field", "_term2") }) .build(); Result result = analyze(multiPhraseQuery); @@ -148,7 +146,6 @@ public void testExtractQueryMetadata_multiPhraseQuery_dups() { assertEquals(1, result.minimumShouldMatch); // because of the dup term } - public void testExtractQueryMetadata_booleanQuery() { BooleanQuery.Builder builder = new BooleanQuery.Builder(); TermQuery termQuery1 = new TermQuery(new Term("_field", "term0")); @@ -201,13 +198,10 @@ public void testExtractQueryMetadata_booleanQuery_msm() { assertThat(result.minimumShouldMatch, equalTo(2)); assertTermsEqual(result.extractions, term1, term2, term3); - builder = new BooleanQuery.Builder() - .add(new BooleanQuery.Builder() - .add(termQuery1, Occur.SHOULD) - .add(termQuery2, Occur.SHOULD) - .build(), Occur.SHOULD) - .add(termQuery3, Occur.SHOULD) - .setMinimumNumberShouldMatch(2); + builder = new BooleanQuery.Builder().add( + new BooleanQuery.Builder().add(termQuery1, Occur.SHOULD).add(termQuery2, Occur.SHOULD).build(), + Occur.SHOULD + ).add(termQuery3, Occur.SHOULD).setMinimumNumberShouldMatch(2); booleanQuery = builder.build(); result = analyze(booleanQuery); assertThat(result.verified, is(false)); @@ -216,15 +210,10 @@ public void testExtractQueryMetadata_booleanQuery_msm() { Term term4 = new Term("_field", "_term4"); TermQuery termQuery4 = new TermQuery(term4); - builder = new BooleanQuery.Builder() - .add(new BooleanQuery.Builder() - .add(termQuery1, Occur.MUST) - .add(termQuery2, Occur.FILTER) - .build(), Occur.SHOULD) - .add(new BooleanQuery.Builder() - .add(termQuery3, Occur.MUST) - .add(termQuery4, Occur.FILTER) - .build(), Occur.SHOULD); + builder = new BooleanQuery.Builder().add( + new BooleanQuery.Builder().add(termQuery1, Occur.MUST).add(termQuery2, Occur.FILTER).build(), + Occur.SHOULD + ).add(new BooleanQuery.Builder().add(termQuery3, Occur.MUST).add(termQuery4, Occur.FILTER).build(), Occur.SHOULD); booleanQuery = builder.build(); result = analyze(booleanQuery); assertThat(result.verified, is(false)); @@ -254,13 +243,10 @@ public void testExtractQueryMetadata_booleanQuery_msm() { assertThat(result.minimumShouldMatch, equalTo(5)); assertTermsEqual(result.extractions, term1, term2, term3, term4, term5); - builder = new BooleanQuery.Builder() - .add(new BooleanQuery.Builder() - .add(termQuery1, Occur.SHOULD) - .add(termQuery2, Occur.SHOULD) - .build(), Occur.SHOULD) - .add(new BooleanQuery.Builder().setMinimumNumberShouldMatch(1).build(), Occur.SHOULD) - .setMinimumNumberShouldMatch(2); + builder = new BooleanQuery.Builder().add( + new BooleanQuery.Builder().add(termQuery1, Occur.SHOULD).add(termQuery2, Occur.SHOULD).build(), + Occur.SHOULD + ).add(new BooleanQuery.Builder().setMinimumNumberShouldMatch(1).build(), Occur.SHOULD).setMinimumNumberShouldMatch(2); booleanQuery = builder.build(); result = analyze(booleanQuery); // ideally it would return no extractions, but the fact @@ -394,42 +380,34 @@ public void testExactMatch_booleanQuery() { assertThat("Prohibited clause, so candidate matches are not verified", result.verified, is(false)); assertThat(result.minimumShouldMatch, equalTo(1)); - builder = new BooleanQuery.Builder() - .add(new BooleanQuery.Builder() - .add(termQuery1, Occur.FILTER) - .add(termQuery2, Occur.FILTER) - .build(), Occur.SHOULD) - .add(termQuery3, Occur.SHOULD); + builder = new BooleanQuery.Builder().add( + new BooleanQuery.Builder().add(termQuery1, Occur.FILTER).add(termQuery2, Occur.FILTER).build(), + Occur.SHOULD + ).add(termQuery3, Occur.SHOULD); result = analyze(builder.build()); assertThat("Inner clause that is not a pure disjunction, so candidate matches are not verified", result.verified, is(false)); assertThat(result.minimumShouldMatch, equalTo(1)); - builder = new BooleanQuery.Builder() - .add(new BooleanQuery.Builder() - .add(termQuery1, Occur.SHOULD) - .add(termQuery2, Occur.SHOULD) - .build(), Occur.SHOULD) - .add(termQuery3, Occur.SHOULD); + builder = new BooleanQuery.Builder().add( + new BooleanQuery.Builder().add(termQuery1, Occur.SHOULD).add(termQuery2, Occur.SHOULD).build(), + Occur.SHOULD + ).add(termQuery3, Occur.SHOULD); result = analyze(builder.build()); assertThat("Inner clause that is a pure disjunction, so candidate matches are verified", result.verified, is(true)); assertThat(result.minimumShouldMatch, equalTo(1)); - builder = new BooleanQuery.Builder() - .add(new BooleanQuery.Builder() - .add(termQuery1, Occur.SHOULD) - .add(termQuery2, Occur.SHOULD) - .build(), Occur.MUST) - .add(termQuery3, Occur.FILTER); + builder = new BooleanQuery.Builder().add( + new BooleanQuery.Builder().add(termQuery1, Occur.SHOULD).add(termQuery2, Occur.SHOULD).build(), + Occur.MUST + ).add(termQuery3, Occur.FILTER); result = analyze(builder.build()); assertThat("Disjunctions of conjunctions can't be verified", result.verified, is(false)); assertThat(result.minimumShouldMatch, equalTo(2)); - builder = new BooleanQuery.Builder() - .add(new BooleanQuery.Builder() - .add(termQuery1, Occur.MUST) - .add(termQuery2, Occur.FILTER) - .build(), Occur.SHOULD) - .add(termQuery3, Occur.SHOULD); + builder = new BooleanQuery.Builder().add( + new BooleanQuery.Builder().add(termQuery1, Occur.MUST).add(termQuery2, Occur.FILTER).build(), + Occur.SHOULD + ).add(termQuery3, Occur.SHOULD); result = analyze(builder.build()); assertThat("Conjunctions of disjunctions can't be verified", result.verified, is(false)); assertThat(result.minimumShouldMatch, equalTo(1)); @@ -509,7 +487,7 @@ public void testExtractQueryMetadata_boostQuery() { } public void testExtractQueryMetadata_blendedTermQuery() { - Term[] termsArr = new Term[]{new Term("_field", "_term1"), new Term("_field", "_term2")}; + Term[] termsArr = new Term[] { new Term("_field", "_term1"), new Term("_field", "_term2") }; BlendedTermQuery blendedTermQuery = BlendedTermQuery.dismaxBlendedQuery(termsArr, 1.0f); Result result = analyze(blendedTermQuery); assertThat(result.verified, is(true)); @@ -545,8 +523,7 @@ public void testExtractQueryMetadata_spanTermQuery() { public void testExtractQueryMetadata_spanNearQuery() { SpanTermQuery spanTermQuery1 = new SpanTermQuery(new Term("_field", "_short_term")); SpanTermQuery spanTermQuery2 = new SpanTermQuery(new Term("_field", "_very_long_term")); - SpanNearQuery spanNearQuery = new SpanNearQuery.Builder("_field", true) - .addClause(spanTermQuery1).addClause(spanTermQuery2).build(); + SpanNearQuery spanNearQuery = new SpanNearQuery.Builder("_field", true).addClause(spanTermQuery1).addClause(spanTermQuery2).build(); Result result = analyze(spanNearQuery); assertThat(result.verified, is(false)); @@ -760,7 +737,8 @@ public void testExtractQueryMetadata_disjunctionMaxQuery() { TermQuery termQuery3 = new TermQuery(new Term("_field", "_term3")); TermQuery termQuery4 = new TermQuery(new Term("_field", "_term4")); DisjunctionMaxQuery disjunctionMaxQuery = new DisjunctionMaxQuery( - Arrays.asList(termQuery1, termQuery2, termQuery3, termQuery4), 0.1f + Arrays.asList(termQuery1, termQuery2, termQuery3, termQuery4), + 0.1f ); Result result = analyze(disjunctionMaxQuery); @@ -779,7 +757,8 @@ public void testExtractQueryMetadata_disjunctionMaxQuery() { assertThat(terms.get(3).bytes(), equalTo(termQuery4.getTerm().bytes())); disjunctionMaxQuery = new DisjunctionMaxQuery( - Arrays.asList(termQuery1, termQuery2, termQuery3, new PhraseQuery("_field", "_term4")), 0.1f + Arrays.asList(termQuery1, termQuery2, termQuery3, new PhraseQuery("_field", "_term4")), + 0.1f ); result = analyze(disjunctionMaxQuery); @@ -805,10 +784,7 @@ public void testSynonymQuery() { assertThat(result.minimumShouldMatch, equalTo(0)); assertThat(result.extractions.isEmpty(), is(true)); - query = new SynonymQuery.Builder("_field") - .addTerm(new Term("_field", "_value1")) - .addTerm(new Term("_field", "_value2")) - .build(); + query = new SynonymQuery.Builder("_field").addTerm(new Term("_field", "_value1")).addTerm(new Term("_field", "_value2")).build(); result = analyze(query); assertThat(result.verified, is(true)); assertThat(result.minimumShouldMatch, equalTo(1)); @@ -823,8 +799,7 @@ public void testFunctionScoreQuery() { assertThat(result.minimumShouldMatch, equalTo(1)); assertTermsEqual(result.extractions, new Term("_field", "_value")); - functionScoreQuery = new FunctionScoreQuery(termQuery, new RandomScoreFunction(0, 0, null), - CombineFunction.MULTIPLY, 1f, 10f); + functionScoreQuery = new FunctionScoreQuery(termQuery, new RandomScoreFunction(0, 0, null), CombineFunction.MULTIPLY, 1f, 10f); result = analyze(functionScoreQuery); assertThat(result.verified, is(false)); assertThat(result.minimumShouldMatch, equalTo(1)); @@ -840,8 +815,13 @@ public void testFunctionScoreQuery_withMatchAll() { assertThat(result.matchAllDocs, is(true)); assertThat(result.extractions.isEmpty(), is(true)); - FunctionScoreQuery functionScoreQuery2 = - new FunctionScoreQuery(innerQuery, new RandomScoreFunction(0, 0, null), CombineFunction.MULTIPLY, 1f, 10f); + FunctionScoreQuery functionScoreQuery2 = new FunctionScoreQuery( + innerQuery, + new RandomScoreFunction(0, 0, null), + CombineFunction.MULTIPLY, + 1f, + 10f + ); result = analyze(functionScoreQuery2); assertThat(result.verified, is(false)); assertThat(result.minimumShouldMatch, equalTo(0)); @@ -908,8 +888,7 @@ public void testPointRangeQuery() { assertDimension(ranges.get(0).range.lowerPoint, bytes -> DoublePoint.encodeDimension(10D, bytes, 0)); assertDimension(ranges.get(0).range.upperPoint, bytes -> DoublePoint.encodeDimension(20D, bytes, 0)); - query = InetAddressPoint.newRangeQuery("_field", InetAddresses.forString("192.168.1.0"), - InetAddresses.forString("192.168.1.255")); + query = InetAddressPoint.newRangeQuery("_field", InetAddresses.forString("192.168.1.0"), InetAddresses.forString("192.168.1.255")); result = analyze(query); assertThat(result.minimumShouldMatch, equalTo(1)); assertFalse(result.verified); @@ -926,7 +905,7 @@ public void testTooManyPointDimensions() { Query query1 = LatLonPoint.newBoxQuery("_field", 0, 1, 0, 1); assertEquals(Result.UNKNOWN, analyze(query1)); - Query query2 = LongPoint.newRangeQuery("_field", new long[]{0, 0, 0}, new long[]{1, 1, 1}); + Query query2 = LongPoint.newRangeQuery("_field", new long[] { 0, 0, 0 }, new long[] { 1, 1, 1 }); assertEquals(Result.UNKNOWN, analyze(query2)); } @@ -939,8 +918,10 @@ public void testPointRangeQuery_lowerUpperReversed() { } public void testIndexOrDocValuesQuery() { - Query query = new IndexOrDocValuesQuery(IntPoint.newRangeQuery("_field", 10, 20), - SortedNumericDocValuesField.newSlowRangeQuery("_field", 10, 20)); + Query query = new IndexOrDocValuesQuery( + IntPoint.newRangeQuery("_field", 10, 20), + SortedNumericDocValuesField.newSlowRangeQuery("_field", 10, 20) + ); Result result = analyze(query); assertFalse(result.verified); assertThat(result.minimumShouldMatch, equalTo(1)); @@ -1019,61 +1000,65 @@ public void testPointRangeQuerySelectRanges() { public void testExtractQueryMetadata_duplicatedClauses() { BooleanQuery.Builder builder = new BooleanQuery.Builder(); builder.add( - new BooleanQuery.Builder() - .add(new TermQuery(new Term("field", "value1")), BooleanClause.Occur.MUST) - .add(new TermQuery(new Term("field", "value2")), BooleanClause.Occur.MUST) - .build(), - BooleanClause.Occur.MUST + new BooleanQuery.Builder().add(new TermQuery(new Term("field", "value1")), BooleanClause.Occur.MUST) + .add(new TermQuery(new Term("field", "value2")), BooleanClause.Occur.MUST) + .build(), + BooleanClause.Occur.MUST ); builder.add( - new BooleanQuery.Builder() - .add(new TermQuery(new Term("field", "value2")), BooleanClause.Occur.MUST) - .add(new TermQuery(new Term("field", "value3")), BooleanClause.Occur.MUST) - .build(), - BooleanClause.Occur.MUST + new BooleanQuery.Builder().add(new TermQuery(new Term("field", "value2")), BooleanClause.Occur.MUST) + .add(new TermQuery(new Term("field", "value3")), BooleanClause.Occur.MUST) + .build(), + BooleanClause.Occur.MUST ); builder.add( - new BooleanQuery.Builder() - .add(new TermQuery(new Term("field", "value3")), BooleanClause.Occur.MUST) - .add(new TermQuery(new Term("field", "value4")), BooleanClause.Occur.MUST) - .build(), - BooleanClause.Occur.MUST + new BooleanQuery.Builder().add(new TermQuery(new Term("field", "value3")), BooleanClause.Occur.MUST) + .add(new TermQuery(new Term("field", "value4")), BooleanClause.Occur.MUST) + .build(), + BooleanClause.Occur.MUST ); Result result = analyze(builder.build()); assertThat(result.verified, is(false)); assertThat(result.matchAllDocs, is(false)); assertThat(result.minimumShouldMatch, equalTo(4)); - assertTermsEqual(result.extractions, new Term("field", "value1"), new Term("field", "value2"), - new Term("field", "value3"), new Term("field", "value4")); + assertTermsEqual( + result.extractions, + new Term("field", "value1"), + new Term("field", "value2"), + new Term("field", "value3"), + new Term("field", "value4") + ); builder = new BooleanQuery.Builder().setMinimumNumberShouldMatch(2); builder.add( - new BooleanQuery.Builder() - .add(new TermQuery(new Term("field", "value1")), BooleanClause.Occur.MUST) - .add(new TermQuery(new Term("field", "value2")), BooleanClause.Occur.MUST) - .build(), - BooleanClause.Occur.SHOULD + new BooleanQuery.Builder().add(new TermQuery(new Term("field", "value1")), BooleanClause.Occur.MUST) + .add(new TermQuery(new Term("field", "value2")), BooleanClause.Occur.MUST) + .build(), + BooleanClause.Occur.SHOULD ); builder.add( - new BooleanQuery.Builder() - .add(new TermQuery(new Term("field", "value2")), BooleanClause.Occur.MUST) - .add(new TermQuery(new Term("field", "value3")), BooleanClause.Occur.MUST) - .build(), - BooleanClause.Occur.SHOULD + new BooleanQuery.Builder().add(new TermQuery(new Term("field", "value2")), BooleanClause.Occur.MUST) + .add(new TermQuery(new Term("field", "value3")), BooleanClause.Occur.MUST) + .build(), + BooleanClause.Occur.SHOULD ); builder.add( - new BooleanQuery.Builder() - .add(new TermQuery(new Term("field", "value3")), BooleanClause.Occur.MUST) - .add(new TermQuery(new Term("field", "value4")), BooleanClause.Occur.MUST) - .build(), - BooleanClause.Occur.SHOULD + new BooleanQuery.Builder().add(new TermQuery(new Term("field", "value3")), BooleanClause.Occur.MUST) + .add(new TermQuery(new Term("field", "value4")), BooleanClause.Occur.MUST) + .build(), + BooleanClause.Occur.SHOULD ); result = analyze(builder.build()); assertThat(result.verified, is(false)); assertThat(result.matchAllDocs, is(false)); assertThat(result.minimumShouldMatch, equalTo(2)); - assertTermsEqual(result.extractions, new Term("field", "value1"), new Term("field", "value2"), - new Term("field", "value3"), new Term("field", "value4")); + assertTermsEqual( + result.extractions, + new Term("field", "value1"), + new Term("field", "value2"), + new Term("field", "value3"), + new Term("field", "value4") + ); } public void testEmptyQueries() { @@ -1103,14 +1088,22 @@ public void testIntervalQueries() { assertThat(result.minimumShouldMatch, equalTo(1)); assertTermsEqual(result.extractions, new Term("field", "term1"), new Term("field", "term2")); - source = Intervals.ordered(Intervals.term("term1"), Intervals.term("term2"), - Intervals.or(Intervals.term("term3"), Intervals.term("term4"))); + source = Intervals.ordered( + Intervals.term("term1"), + Intervals.term("term2"), + Intervals.or(Intervals.term("term3"), Intervals.term("term4")) + ); result = analyze(new IntervalQuery("field", source)); assertThat(result.verified, is(false)); assertThat(result.matchAllDocs, is(false)); assertThat(result.minimumShouldMatch, equalTo(3)); - assertTermsEqual(result.extractions, new Term("field", "term1"), new Term("field", "term2"), - new Term("field", "term3"), new Term("field", "term4")); + assertTermsEqual( + result.extractions, + new Term("field", "term1"), + new Term("field", "term2"), + new Term("field", "term3"), + new Term("field", "term4") + ); source = Intervals.ordered(Intervals.term("term1"), Intervals.wildcard(new BytesRef("a*"))); result = analyze(new IntervalQuery("field", source)); @@ -1201,11 +1194,8 @@ public void testIntervalQueries() { public void testRangeAndTermWithNestedMSM() { - Query q1 = new BooleanQuery.Builder() - .add(new TermQuery(new Term("f", "v3")), Occur.SHOULD) - .add(new BooleanQuery.Builder() - .add(new TermQuery(new Term("f", "n1")), Occur.SHOULD) - .build(), Occur.SHOULD) + Query q1 = new BooleanQuery.Builder().add(new TermQuery(new Term("f", "v3")), Occur.SHOULD) + .add(new BooleanQuery.Builder().add(new TermQuery(new Term("f", "n1")), Occur.SHOULD).build(), Occur.SHOULD) .add(new TermQuery(new Term("f", "v4")), Occur.SHOULD) .setMinimumNumberShouldMatch(2) .build(); @@ -1216,15 +1206,12 @@ public void testRangeAndTermWithNestedMSM() { assertFalse(r1.matchAllDocs); assertTrue(r1.verified); - Query q = new BooleanQuery.Builder() - .add(IntPoint.newRangeQuery("i", 0, 10), Occur.FILTER) + Query q = new BooleanQuery.Builder().add(IntPoint.newRangeQuery("i", 0, 10), Occur.FILTER) .add(new TermQuery(new Term("f", "v1")), Occur.MUST) .add(new TermQuery(new Term("f", "v2")), Occur.MUST) .add(IntPoint.newRangeQuery("i", 2, 20), Occur.FILTER) .add(new TermQuery(new Term("f", "v3")), Occur.SHOULD) - .add(new BooleanQuery.Builder() - .add(new TermQuery(new Term("f", "n1")), Occur.SHOULD) - .build(), Occur.SHOULD) + .add(new BooleanQuery.Builder().add(new TermQuery(new Term("f", "n1")), Occur.SHOULD).build(), Occur.SHOULD) .add(new TermQuery(new Term("f", "v4")), Occur.SHOULD) .setMinimumNumberShouldMatch(2) .build(); @@ -1238,8 +1225,7 @@ public void testRangeAndTermWithNestedMSM() { public void testCombinedRangeAndTermWithMinimumShouldMatch() { - Query disj = new BooleanQuery.Builder() - .add(IntPoint.newRangeQuery("i", 0, 10), Occur.SHOULD) + Query disj = new BooleanQuery.Builder().add(IntPoint.newRangeQuery("i", 0, 10), Occur.SHOULD) .add(new TermQuery(new Term("f", "v1")), Occur.SHOULD) .add(new TermQuery(new Term("f", "v1")), Occur.SHOULD) .setMinimumNumberShouldMatch(2) @@ -1251,8 +1237,7 @@ public void testCombinedRangeAndTermWithMinimumShouldMatch() { assertFalse(r.matchAllDocs); assertFalse(r.verified); - Query q = new BooleanQuery.Builder() - .add(IntPoint.newRangeQuery("i", 0, 10), Occur.SHOULD) + Query q = new BooleanQuery.Builder().add(IntPoint.newRangeQuery("i", 0, 10), Occur.SHOULD) .add(new TermQuery(new Term("f", "v1")), Occur.SHOULD) .add(new TermQuery(new Term("f", "v1")), Occur.SHOULD) .add(new TermQuery(new Term("f", "v1")), Occur.FILTER) @@ -1265,10 +1250,7 @@ public void testCombinedRangeAndTermWithMinimumShouldMatch() { assertFalse(result.verified); assertFalse(result.matchAllDocs); - q = new BooleanQuery.Builder() - .add(q, Occur.MUST) - .add(q, Occur.MUST) - .build(); + q = new BooleanQuery.Builder().add(q, Occur.MUST).add(q, Occur.MUST).build(); result = analyze(q); assertThat(result.minimumShouldMatch, equalTo(1)); @@ -1276,8 +1258,7 @@ public void testCombinedRangeAndTermWithMinimumShouldMatch() { assertFalse(result.verified); assertFalse(result.matchAllDocs); - Query q2 = new BooleanQuery.Builder() - .add(new TermQuery(new Term("f", "v1")), Occur.FILTER) + Query q2 = new BooleanQuery.Builder().add(new TermQuery(new Term("f", "v1")), Occur.FILTER) .add(IntPoint.newRangeQuery("i", 15, 20), Occur.SHOULD) .add(new TermQuery(new Term("f", "v2")), Occur.SHOULD) .add(new TermQuery(new Term("f", "v2")), Occur.MUST) @@ -1291,8 +1272,7 @@ public void testCombinedRangeAndTermWithMinimumShouldMatch() { assertFalse(result.matchAllDocs); // multiple range queries on different fields - Query q3 = new BooleanQuery.Builder() - .add(IntPoint.newRangeQuery("i", 15, 20), Occur.SHOULD) + Query q3 = new BooleanQuery.Builder().add(IntPoint.newRangeQuery("i", 15, 20), Occur.SHOULD) .add(IntPoint.newRangeQuery("i2", 15, 20), Occur.SHOULD) .add(new TermQuery(new Term("f", "v1")), Occur.SHOULD) .add(new TermQuery(new Term("f", "v2")), Occur.MUST) @@ -1305,8 +1285,7 @@ public void testCombinedRangeAndTermWithMinimumShouldMatch() { assertFalse(result.matchAllDocs); // multiple disjoint range queries on the same field - Query q4 = new BooleanQuery.Builder() - .add(IntPoint.newRangeQuery("i", 15, 20), Occur.SHOULD) + Query q4 = new BooleanQuery.Builder().add(IntPoint.newRangeQuery("i", 15, 20), Occur.SHOULD) .add(IntPoint.newRangeQuery("i", 25, 30), Occur.SHOULD) .add(IntPoint.newRangeQuery("i", 35, 40), Occur.SHOULD) .add(new TermQuery(new Term("f", "v1")), Occur.SHOULD) @@ -1320,14 +1299,12 @@ public void testCombinedRangeAndTermWithMinimumShouldMatch() { assertFalse(result.matchAllDocs); // multiple conjunction range queries on the same field - Query q5 = new BooleanQuery.Builder() - .add(new BooleanQuery.Builder() - .add(IntPoint.newRangeQuery("i", 15, 20), Occur.MUST) + Query q5 = new BooleanQuery.Builder().add( + new BooleanQuery.Builder().add(IntPoint.newRangeQuery("i", 15, 20), Occur.MUST) .add(IntPoint.newRangeQuery("i", 25, 30), Occur.MUST) - .build(), Occur.MUST) - .add(IntPoint.newRangeQuery("i", 35, 40), Occur.MUST) - .add(new TermQuery(new Term("f", "v2")), Occur.MUST) - .build(); + .build(), + Occur.MUST + ).add(IntPoint.newRangeQuery("i", 35, 40), Occur.MUST).add(new TermQuery(new Term("f", "v2")), Occur.MUST).build(); result = analyze(q5); assertThat(result.minimumShouldMatch, equalTo(2)); assertThat(result.extractions, hasSize(4)); @@ -1335,14 +1312,12 @@ public void testCombinedRangeAndTermWithMinimumShouldMatch() { assertFalse(result.matchAllDocs); // multiple conjunction range queries on different fields - Query q6 = new BooleanQuery.Builder() - .add(new BooleanQuery.Builder() - .add(IntPoint.newRangeQuery("i", 15, 20), Occur.MUST) + Query q6 = new BooleanQuery.Builder().add( + new BooleanQuery.Builder().add(IntPoint.newRangeQuery("i", 15, 20), Occur.MUST) .add(IntPoint.newRangeQuery("i2", 25, 30), Occur.MUST) - .build(), Occur.MUST) - .add(IntPoint.newRangeQuery("i", 35, 40), Occur.MUST) - .add(new TermQuery(new Term("f", "v2")), Occur.MUST) - .build(); + .build(), + Occur.MUST + ).add(IntPoint.newRangeQuery("i", 35, 40), Occur.MUST).add(new TermQuery(new Term("f", "v2")), Occur.MUST).build(); result = analyze(q6); assertThat(result.minimumShouldMatch, equalTo(3)); assertThat(result.extractions, hasSize(4)); @@ -1350,15 +1325,18 @@ public void testCombinedRangeAndTermWithMinimumShouldMatch() { assertFalse(result.matchAllDocs); // mixed term and range conjunctions - Query q7 = new BooleanQuery.Builder() - .add(new BooleanQuery.Builder() - .add(IntPoint.newRangeQuery("i", 1, 2), Occur.MUST) + Query q7 = new BooleanQuery.Builder().add( + new BooleanQuery.Builder().add(IntPoint.newRangeQuery("i", 1, 2), Occur.MUST) .add(new TermQuery(new Term("f", "1")), Occur.MUST) - .build(), Occur.MUST) - .add(new BooleanQuery.Builder() - .add(IntPoint.newRangeQuery("i", 1, 2), Occur.MUST) - .add(new TermQuery(new Term("f", "2")), Occur.MUST) - .build(), Occur.MUST) + .build(), + Occur.MUST + ) + .add( + new BooleanQuery.Builder().add(IntPoint.newRangeQuery("i", 1, 2), Occur.MUST) + .add(new TermQuery(new Term("f", "2")), Occur.MUST) + .build(), + Occur.MUST + ) .build(); result = analyze(q7); assertThat(result.minimumShouldMatch, equalTo(3)); diff --git a/modules/percolator/src/test/java/org/elasticsearch/percolator/QueryBuilderStoreTests.java b/modules/percolator/src/test/java/org/elasticsearch/percolator/QueryBuilderStoreTests.java index a74ea304273e3..bb58f919602ed 100644 --- a/modules/percolator/src/test/java/org/elasticsearch/percolator/QueryBuilderStoreTests.java +++ b/modules/percolator/src/test/java/org/elasticsearch/percolator/QueryBuilderStoreTests.java @@ -20,7 +20,6 @@ import org.elasticsearch.Version; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.core.CheckedFunction; import org.elasticsearch.index.fielddata.plain.BytesBinaryIndexFieldData; import org.elasticsearch.index.mapper.BinaryFieldMapper; @@ -30,10 +29,11 @@ import org.elasticsearch.index.mapper.TestDocumentParserContext; import org.elasticsearch.index.query.SearchExecutionContext; import org.elasticsearch.index.query.TermQueryBuilder; -import org.mockito.Mockito; import org.elasticsearch.search.SearchModule; import org.elasticsearch.search.aggregations.support.CoreValuesSourceType; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.mockito.Mockito; import java.io.IOException; import java.util.Collections; @@ -67,8 +67,7 @@ public void testStoringQueryBuilders() throws IOException { for (int i = 0; i < queryBuilders.length; i++) { queryBuilders[i] = new TermQueryBuilder(randomAlphaOfLength(4), randomAlphaOfLength(8)); DocumentParserContext documentParserContext = new TestDocumentParserContext(); - PercolatorFieldMapper.createQueryBuilderField(version, - fieldMapper, queryBuilders[i], documentParserContext); + PercolatorFieldMapper.createQueryBuilderField(version, fieldMapper, queryBuilders[i], documentParserContext); indexWriter.addDocument(documentParserContext.doc()); } } @@ -77,8 +76,9 @@ public void testStoringQueryBuilders() throws IOException { when(searchExecutionContext.indexVersionCreated()).thenReturn(version); when(searchExecutionContext.getWriteableRegistry()).thenReturn(writableRegistry()); when(searchExecutionContext.getXContentRegistry()).thenReturn(xContentRegistry()); - when(searchExecutionContext.getForField(fieldMapper.fieldType())) - .thenReturn(new BytesBinaryIndexFieldData(fieldMapper.name(), CoreValuesSourceType.KEYWORD)); + when(searchExecutionContext.getForField(fieldMapper.fieldType())).thenReturn( + new BytesBinaryIndexFieldData(fieldMapper.name(), CoreValuesSourceType.KEYWORD) + ); when(searchExecutionContext.getFieldType(Mockito.anyString())).thenAnswer(invocation -> { final String fieldName = (String) invocation.getArguments()[0]; return new KeywordFieldMapper.KeywordFieldType(fieldName); diff --git a/modules/rank-eval/src/internalClusterTest/java/org/elasticsearch/index/rankeval/RankEvalRequestIT.java b/modules/rank-eval/src/internalClusterTest/java/org/elasticsearch/index/rankeval/RankEvalRequestIT.java index 5ae58a682625a..8dfc592b6f4a5 100644 --- a/modules/rank-eval/src/internalClusterTest/java/org/elasticsearch/index/rankeval/RankEvalRequestIT.java +++ b/modules/rank-eval/src/internalClusterTest/java/org/elasticsearch/index/rankeval/RankEvalRequestIT.java @@ -49,22 +49,18 @@ public void setup() { createIndex(TEST_INDEX); ensureGreen(); - client().prepareIndex(TEST_INDEX).setId("1") - .setSource("id", 1, "text", "berlin", "title", "Berlin, Germany", "population", 3670622).get(); - client().prepareIndex(TEST_INDEX).setId("2").setSource("id", 2, "text", "amsterdam", "population", 851573) - .get(); - client().prepareIndex(TEST_INDEX).setId("3").setSource("id", 3, "text", "amsterdam", "population", 851573) - .get(); - client().prepareIndex(TEST_INDEX).setId("4").setSource("id", 4, "text", "amsterdam", "population", 851573) - .get(); - client().prepareIndex(TEST_INDEX).setId("5").setSource("id", 5, "text", "amsterdam", "population", 851573) - .get(); - client().prepareIndex(TEST_INDEX).setId("6").setSource("id", 6, "text", "amsterdam", "population", 851573) - .get(); + client().prepareIndex(TEST_INDEX) + .setId("1") + .setSource("id", 1, "text", "berlin", "title", "Berlin, Germany", "population", 3670622) + .get(); + client().prepareIndex(TEST_INDEX).setId("2").setSource("id", 2, "text", "amsterdam", "population", 851573).get(); + client().prepareIndex(TEST_INDEX).setId("3").setSource("id", 3, "text", "amsterdam", "population", 851573).get(); + client().prepareIndex(TEST_INDEX).setId("4").setSource("id", 4, "text", "amsterdam", "population", 851573).get(); + client().prepareIndex(TEST_INDEX).setId("5").setSource("id", 5, "text", "amsterdam", "population", 851573).get(); + client().prepareIndex(TEST_INDEX).setId("6").setSource("id", 6, "text", "amsterdam", "population", 851573).get(); // add another index for testing closed indices etc... - client().prepareIndex("test2").setId("7").setSource("id", 7, "text", "amsterdam", "population", 851573) - .get(); + client().prepareIndex("test2").setId("7").setSource("id", 7, "text", "amsterdam", "population", 851573).get(); refresh(); // set up an alias that can also be used in tests @@ -81,26 +77,22 @@ public void testPrecisionAtRequest() { SearchSourceBuilder testQuery = new SearchSourceBuilder(); testQuery.query(new MatchAllQueryBuilder()); testQuery.sort("id"); - RatedRequest amsterdamRequest = new RatedRequest("amsterdam_query", - createRelevant("2", "3", "4", "5"), testQuery); + RatedRequest amsterdamRequest = new RatedRequest("amsterdam_query", createRelevant("2", "3", "4", "5"), testQuery); amsterdamRequest.addSummaryFields(Arrays.asList(new String[] { "text", "title" })); specifications.add(amsterdamRequest); - RatedRequest berlinRequest = new RatedRequest("berlin_query", createRelevant("1"), - testQuery); + RatedRequest berlinRequest = new RatedRequest("berlin_query", createRelevant("1"), testQuery); berlinRequest.addSummaryFields(Arrays.asList(new String[] { "text", "title" })); specifications.add(berlinRequest); PrecisionAtK metric = new PrecisionAtK(1, false, 10); RankEvalSpec task = new RankEvalSpec(specifications, metric); - RankEvalRequestBuilder builder = new RankEvalRequestBuilder(client(), - RankEvalAction.INSTANCE, new RankEvalRequest()); + RankEvalRequestBuilder builder = new RankEvalRequestBuilder(client(), RankEvalAction.INSTANCE, new RankEvalRequest()); builder.setRankEvalSpec(task); String indexToUse = randomBoolean() ? TEST_INDEX : INDEX_ALIAS; - RankEvalResponse response = client().execute(RankEvalAction.INSTANCE, builder.request().indices(indexToUse)) - .actionGet(); + RankEvalResponse response = client().execute(RankEvalAction.INSTANCE, builder.request().indices(indexToUse)).actionGet(); // the expected Prec@ for the first query is 4/6 and the expected Prec@ for the // second is 1/6, divided by 2 to get the average double expectedPrecision = (1.0 / 6.0 + 4.0 / 6.0) / 2.0; @@ -161,19 +153,23 @@ public void testDCGRequest() { List specifications = new ArrayList<>(); List ratedDocs = Arrays.asList( - new RatedDocument(TEST_INDEX, "1", 3), - new RatedDocument(TEST_INDEX, "2", 2), - new RatedDocument(TEST_INDEX, "3", 3), - new RatedDocument(TEST_INDEX, "4", 0), - new RatedDocument(TEST_INDEX, "5", 1), - new RatedDocument(TEST_INDEX, "6", 2)); + new RatedDocument(TEST_INDEX, "1", 3), + new RatedDocument(TEST_INDEX, "2", 2), + new RatedDocument(TEST_INDEX, "3", 3), + new RatedDocument(TEST_INDEX, "4", 0), + new RatedDocument(TEST_INDEX, "5", 1), + new RatedDocument(TEST_INDEX, "6", 2) + ); specifications.add(new RatedRequest("amsterdam_query", ratedDocs, testQuery)); DiscountedCumulativeGain metric = new DiscountedCumulativeGain(false, null, 10); RankEvalSpec task = new RankEvalSpec(specifications, metric); - RankEvalRequestBuilder builder = new RankEvalRequestBuilder(client(), RankEvalAction.INSTANCE, - new RankEvalRequest(task, new String[] { TEST_INDEX })); + RankEvalRequestBuilder builder = new RankEvalRequestBuilder( + client(), + RankEvalAction.INSTANCE, + new RankEvalRequest(task, new String[] { TEST_INDEX }) + ); RankEvalResponse response = client().execute(RankEvalAction.INSTANCE, builder.request()).actionGet(); assertEquals(DiscountedCumulativeGainTests.EXPECTED_DCG, response.getMetricScore(), 10E-14); @@ -200,8 +196,11 @@ public void testMRRRequest() { MeanReciprocalRank metric = new MeanReciprocalRank(1, 10); RankEvalSpec task = new RankEvalSpec(specifications, metric); - RankEvalRequestBuilder builder = new RankEvalRequestBuilder(client(), RankEvalAction.INSTANCE, - new RankEvalRequest(task, new String[] { TEST_INDEX })); + RankEvalRequestBuilder builder = new RankEvalRequestBuilder( + client(), + RankEvalAction.INSTANCE, + new RankEvalRequest(task, new String[] { TEST_INDEX }) + ); RankEvalResponse response = client().execute(RankEvalAction.INSTANCE, builder.request()).actionGet(); // the expected reciprocal rank for the amsterdam_query is 1/5 @@ -232,20 +231,21 @@ public void testBadQuery() { List specifications = new ArrayList<>(); SearchSourceBuilder amsterdamQuery = new SearchSourceBuilder(); amsterdamQuery.query(new MatchAllQueryBuilder()); - RatedRequest amsterdamRequest = new RatedRequest("amsterdam_query", - createRelevant("2", "3", "4", "5"), amsterdamQuery); + RatedRequest amsterdamRequest = new RatedRequest("amsterdam_query", createRelevant("2", "3", "4", "5"), amsterdamQuery); specifications.add(amsterdamRequest); SearchSourceBuilder brokenQuery = new SearchSourceBuilder(); brokenQuery.query(QueryBuilders.termQuery("population", "noStringOnNumericFields")); - RatedRequest brokenRequest = new RatedRequest("broken_query", createRelevant("1"), - brokenQuery); + RatedRequest brokenRequest = new RatedRequest("broken_query", createRelevant("1"), brokenQuery); specifications.add(brokenRequest); RankEvalSpec task = new RankEvalSpec(specifications, new PrecisionAtK()); - RankEvalRequestBuilder builder = new RankEvalRequestBuilder(client(), RankEvalAction.INSTANCE, - new RankEvalRequest(task, new String[] { TEST_INDEX })); + RankEvalRequestBuilder builder = new RankEvalRequestBuilder( + client(), + RankEvalAction.INSTANCE, + new RankEvalRequest(task, new String[] { TEST_INDEX }) + ); builder.setRankEvalSpec(task); RankEvalResponse response = client().execute(RankEvalAction.INSTANCE, builder.request()).actionGet(); diff --git a/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/DiscountedCumulativeGain.java b/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/DiscountedCumulativeGain.java index cb472fd67e537..3b387fc347717 100644 --- a/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/DiscountedCumulativeGain.java +++ b/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/DiscountedCumulativeGain.java @@ -8,13 +8,13 @@ package org.elasticsearch.index.rankeval; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.search.SearchHit; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.search.SearchHit; import java.io.IOException; import java.util.ArrayList; @@ -25,9 +25,9 @@ import java.util.OptionalInt; import java.util.stream.Collectors; +import static org.elasticsearch.index.rankeval.EvaluationMetric.joinHitsWithRatings; import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg; import static org.elasticsearch.xcontent.ConstructingObjectParser.optionalConstructorArg; -import static org.elasticsearch.index.rankeval.EvaluationMetric.joinHitsWithRatings; /** * Metric implementing Discounted Cumulative Gain. @@ -106,15 +106,13 @@ public Integer getUnknownDocRating() { return this.unknownDocRating; } - @Override public OptionalInt forcedSearchSize() { return OptionalInt.of(k); } @Override - public EvalQueryQuality evaluate(String taskId, SearchHit[] hits, - List ratedDocs) { + public EvalQueryQuality evaluate(String taskId, SearchHit[] hits, List ratedDocs) { List ratedHits = joinHitsWithRatings(hits, ratedDocs); List ratingsInSearchHits = new ArrayList<>(ratedHits.size()); int unratedResults = 0; @@ -135,8 +133,7 @@ public EvalQueryQuality evaluate(String taskId, SearchHit[] hits, double idcg = 0; if (normalize) { - List allRatings = ratedDocs.stream().mapToInt(RatedDocument::getRating).boxed() - .collect(Collectors.toList()); + List allRatings = ratedDocs.stream().mapToInt(RatedDocument::getRating).boxed().collect(Collectors.toList()); Collections.sort(allRatings, Comparator.nullsLast(Collections.reverseOrder())); idcg = computeDCG(allRatings.subList(0, Math.min(ratingsInSearchHits.size(), allRatings.size()))); if (idcg != 0) { @@ -166,13 +163,19 @@ private static double computeDCG(List ratings) { private static final ParseField K_FIELD = new ParseField("k"); private static final ParseField NORMALIZE_FIELD = new ParseField("normalize"); private static final ParseField UNKNOWN_DOC_RATING_FIELD = new ParseField("unknown_doc_rating"); - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("dcg", false, - args -> { - Boolean normalized = (Boolean) args[0]; - Integer optK = (Integer) args[2]; - return new DiscountedCumulativeGain(normalized == null ? false : normalized, (Integer) args[1], - optK == null ? DEFAULT_K : optK); - }); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "dcg", + false, + args -> { + Boolean normalized = (Boolean) args[0]; + Integer optK = (Integer) args[2]; + return new DiscountedCumulativeGain( + normalized == null ? false : normalized, + (Integer) args[1], + optK == null ? DEFAULT_K : optK + ); + } + ); static { PARSER.declareBoolean(optionalConstructorArg(), NORMALIZE_FIELD); @@ -208,8 +211,8 @@ public final boolean equals(Object obj) { } DiscountedCumulativeGain other = (DiscountedCumulativeGain) obj; return Objects.equals(normalize, other.normalize) - && Objects.equals(unknownDocRating, other.unknownDocRating) - && Objects.equals(k, other.k); + && Objects.equals(unknownDocRating, other.unknownDocRating) + && Objects.equals(k, other.k); } @Override @@ -240,8 +243,7 @@ public static final class Detail implements MetricDetail { } @Override - public - String getMetricName() { + public String getMetricName() { return NAME; } @@ -256,9 +258,11 @@ public XContentBuilder innerToXContent(XContentBuilder builder, Params params) t return builder; } - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(NAME, true, args -> { - return new Detail((Double) args[0], (Double) args[1] != null ? (Double) args[1] : 0.0d, (Integer) args[2]); - }); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + true, + args -> { return new Detail((Double) args[0], (Double) args[1] != null ? (Double) args[1] : 0.0d, (Integer) args[2]); } + ); static { PARSER.declareDouble(constructorArg(), DCG_FIELD); @@ -319,9 +323,9 @@ public boolean equals(Object obj) { return false; } DiscountedCumulativeGain.Detail other = (DiscountedCumulativeGain.Detail) obj; - return Double.compare(this.dcg, other.dcg) == 0 && - Double.compare(this.idcg, other.idcg) == 0 && - this.unratedDocs == other.unratedDocs; + return Double.compare(this.dcg, other.dcg) == 0 + && Double.compare(this.idcg, other.idcg) == 0 + && this.unratedDocs == other.unratedDocs; } @Override @@ -330,4 +334,3 @@ public int hashCode() { } } } - diff --git a/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/EvalQueryQuality.java b/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/EvalQueryQuality.java index c794667a36be2..a7b9832fd81c3 100644 --- a/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/EvalQueryQuality.java +++ b/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/EvalQueryQuality.java @@ -8,16 +8,16 @@ package org.elasticsearch.index.rankeval; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.common.xcontent.XContentParserUtils; +import org.elasticsearch.index.rankeval.RatedDocument.DocumentKey; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentFragment; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.XContentParserUtils; -import org.elasticsearch.index.rankeval.RatedDocument.DocumentKey; import java.io.IOException; import java.util.ArrayList; @@ -115,8 +115,11 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws private static final ParseField UNRATED_DOCS_FIELD = new ParseField("unrated_docs"); private static final ParseField HITS_FIELD = new ParseField("hits"); private static final ParseField METRIC_DETAILS_FIELD = new ParseField("metric_details"); - private static final ObjectParser PARSER = new ObjectParser<>("eval_query_quality", - true, ParsedEvalQueryQuality::new); + private static final ObjectParser PARSER = new ObjectParser<>( + "eval_query_quality", + true, + ParsedEvalQueryQuality::new + ); private static class ParsedEvalQueryQuality { double evaluationResult; @@ -126,8 +129,7 @@ private static class ParsedEvalQueryQuality { static { PARSER.declareDouble((obj, value) -> obj.evaluationResult = value, METRIC_SCORE_FIELD); - PARSER.declareObject((obj, value) -> obj.optionalMetricDetails = value, (p, c) -> parseMetricDetail(p), - METRIC_DETAILS_FIELD); + PARSER.declareObject((obj, value) -> obj.optionalMetricDetails = value, (p, c) -> parseMetricDetail(p), METRIC_DETAILS_FIELD); PARSER.declareObjectArray((obj, list) -> obj.ratedHits = list, (p, c) -> RatedSearchHit.parse(p), HITS_FIELD); } @@ -152,10 +154,10 @@ public final boolean equals(Object obj) { return false; } EvalQueryQuality other = (EvalQueryQuality) obj; - return Objects.equals(queryId, other.queryId) && - Objects.equals(metricScore, other.metricScore) && - Objects.equals(ratedHits, other.ratedHits) && - Objects.equals(optionalMetricDetails, other.optionalMetricDetails); + return Objects.equals(queryId, other.queryId) + && Objects.equals(metricScore, other.metricScore) + && Objects.equals(ratedHits, other.ratedHits) + && Objects.equals(optionalMetricDetails, other.optionalMetricDetails); } @Override diff --git a/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/EvaluationMetric.java b/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/EvaluationMetric.java index 2c3d63dd84934..65340d225461e 100644 --- a/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/EvaluationMetric.java +++ b/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/EvaluationMetric.java @@ -9,10 +9,10 @@ package org.elasticsearch.index.rankeval; import org.elasticsearch.common.io.stream.NamedWriteable; -import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.index.rankeval.RatedDocument.DocumentKey; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; +import org.elasticsearch.xcontent.ToXContentObject; import java.util.ArrayList; import java.util.Collection; @@ -47,7 +47,7 @@ public interface EvaluationMetric extends ToXContentObject, NamedWriteable { */ static List joinHitsWithRatings(SearchHit[] hits, List ratedDocs) { Map ratedDocumentMap = ratedDocs.stream() - .collect(Collectors.toMap(RatedDocument::getKey, item -> item)); + .collect(Collectors.toMap(RatedDocument::getKey, item -> item)); List ratedSearchHits = new ArrayList<>(hits.length); for (SearchHit hit : hits) { DocumentKey key = new DocumentKey(hit.getIndex(), hit.getId()); @@ -65,8 +65,10 @@ static List joinHitsWithRatings(SearchHit[] hits, List filterUnratedDocuments(List ratedHits) { - return ratedHits.stream().filter(hit -> hit.getRating().isPresent() == false) - .map(hit -> new DocumentKey(hit.getSearchHit().getIndex(), hit.getSearchHit().getId())).collect(Collectors.toList()); + return ratedHits.stream() + .filter(hit -> hit.getRating().isPresent() == false) + .map(hit -> new DocumentKey(hit.getSearchHit().getIndex(), hit.getSearchHit().getId())) + .collect(Collectors.toList()); } /** diff --git a/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/ExpectedReciprocalRank.java b/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/ExpectedReciprocalRank.java index 5fd60be10d7e5..a62f3eb95bc2e 100644 --- a/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/ExpectedReciprocalRank.java +++ b/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/ExpectedReciprocalRank.java @@ -8,14 +8,14 @@ package org.elasticsearch.index.rankeval; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.search.SearchHit; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.search.SearchHit; import java.io.IOException; import java.util.ArrayList; @@ -23,9 +23,9 @@ import java.util.Objects; import java.util.OptionalInt; +import static org.elasticsearch.index.rankeval.EvaluationMetric.joinHitsWithRatings; import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg; import static org.elasticsearch.xcontent.ConstructingObjectParser.optionalConstructorArg; -import static org.elasticsearch.index.rankeval.EvaluationMetric.joinHitsWithRatings; /** * Implementation of the Expected Reciprocal Rank metric described in:

    @@ -113,7 +113,6 @@ public Integer getUnknownDocRating() { return this.unknownDocRating; } - @Override public OptionalInt forcedSearchSize() { return OptionalInt.of(k); @@ -165,14 +164,15 @@ public EvalQueryQuality evaluate(String taskId, SearchHit[] hits, List PARSER = new ConstructingObjectParser<>("dcg", false, - args -> { - int maxRelevance = (Integer) args[0]; - Integer optK = (Integer) args[2]; - return new ExpectedReciprocalRank(maxRelevance, (Integer) args[1], - optK == null ? DEFAULT_K : optK); - }); - + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "dcg", + false, + args -> { + int maxRelevance = (Integer) args[0]; + Integer optK = (Integer) args[2]; + return new ExpectedReciprocalRank(maxRelevance, (Integer) args[1], optK == null ? DEFAULT_K : optK); + } + ); static { PARSER.declareInt(constructorArg(), MAX_RELEVANCE_FIELD); @@ -207,9 +207,7 @@ public final boolean equals(Object obj) { return false; } ExpectedReciprocalRank other = (ExpectedReciprocalRank) obj; - return this.k == other.k && - this.maxRelevance == other.maxRelevance - && Objects.equals(unknownDocRating, other.unknownDocRating); + return this.k == other.k && this.maxRelevance == other.maxRelevance && Objects.equals(unknownDocRating, other.unknownDocRating); } @Override @@ -231,8 +229,7 @@ public static final class Detail implements MetricDetail { } @Override - public - String getMetricName() { + public String getMetricName() { return NAME; } @@ -241,9 +238,11 @@ public XContentBuilder innerToXContent(XContentBuilder builder, Params params) t return builder.field(UNRATED_FIELD.getPreferredName(), this.unratedDocs); } - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(NAME, true, args -> { - return new Detail((Integer) args[0]); - }); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + true, + args -> { return new Detail((Integer) args[0]); } + ); static { PARSER.declareInt(constructorArg(), UNRATED_FIELD); @@ -288,4 +287,3 @@ public int hashCode() { } } } - diff --git a/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/MeanReciprocalRank.java b/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/MeanReciprocalRank.java index 94f6ad4e87b35..43b4565e84820 100644 --- a/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/MeanReciprocalRank.java +++ b/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/MeanReciprocalRank.java @@ -8,22 +8,22 @@ package org.elasticsearch.index.rankeval; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.search.SearchHit; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.search.SearchHit; import java.io.IOException; import java.util.List; import java.util.Objects; import java.util.OptionalInt; +import static org.elasticsearch.index.rankeval.EvaluationMetric.joinHitsWithRatings; import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg; import static org.elasticsearch.xcontent.ConstructingObjectParser.optionalConstructorArg; -import static org.elasticsearch.index.rankeval.EvaluationMetric.joinHitsWithRatings; /** * Metric implementing Mean Reciprocal Rank (https://en.wikipedia.org/wiki/Mean_reciprocal_rank).
    @@ -123,13 +123,17 @@ public EvalQueryQuality evaluate(String taskId, SearchHit[] hits, List PARSER = new ConstructingObjectParser<>("reciprocal_rank", - args -> { - Integer optionalThreshold = (Integer) args[0]; - Integer optionalK = (Integer) args[1]; - return new MeanReciprocalRank(optionalThreshold == null ? DEFAULT_RATING_THRESHOLD : optionalThreshold, - optionalK == null ? DEFAULT_K : optionalK); - }); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "reciprocal_rank", + args -> { + Integer optionalThreshold = (Integer) args[0]; + Integer optionalK = (Integer) args[1]; + return new MeanReciprocalRank( + optionalThreshold == null ? DEFAULT_RATING_THRESHOLD : optionalThreshold, + optionalK == null ? DEFAULT_K : optionalK + ); + } + ); static { PARSER.declareInt(optionalConstructorArg(), RELEVANT_RATING_FIELD); @@ -160,8 +164,7 @@ public final boolean equals(Object obj) { return false; } MeanReciprocalRank other = (MeanReciprocalRank) obj; - return Objects.equals(relevantRatingThreshhold, other.relevantRatingThreshhold) - && Objects.equals(k, other.k); + return Objects.equals(relevantRatingThreshhold, other.relevantRatingThreshhold) && Objects.equals(k, other.k); } @Override @@ -183,20 +186,20 @@ public static final class Detail implements MetricDetail { } @Override - public - String getMetricName() { + public String getMetricName() { return NAME; } @Override - public XContentBuilder innerToXContent(XContentBuilder builder, Params params) - throws IOException { + public XContentBuilder innerToXContent(XContentBuilder builder, Params params) throws IOException { return builder.field(FIRST_RELEVANT_RANK_FIELD.getPreferredName(), firstRelevantRank); } - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(NAME, true, args -> { - return new Detail((Integer) args[0]); - }); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + true, + args -> { return new Detail((Integer) args[0]); } + ); static { PARSER.declareInt(constructorArg(), FIRST_RELEVANT_RANK_FIELD); diff --git a/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/PrecisionAtK.java b/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/PrecisionAtK.java index 54cfcaa9fdc40..4034dbda7c7ee 100644 --- a/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/PrecisionAtK.java +++ b/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/PrecisionAtK.java @@ -8,23 +8,24 @@ package org.elasticsearch.index.rankeval; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.search.SearchHit; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.search.SearchHit; -import javax.naming.directory.SearchResult; import java.io.IOException; import java.util.List; import java.util.Objects; import java.util.OptionalInt; +import javax.naming.directory.SearchResult; + +import static org.elasticsearch.index.rankeval.EvaluationMetric.joinHitsWithRatings; import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg; import static org.elasticsearch.xcontent.ConstructingObjectParser.optionalConstructorArg; -import static org.elasticsearch.index.rankeval.EvaluationMetric.joinHitsWithRatings; /** * Metric implementing Precision@K @@ -95,7 +96,8 @@ public PrecisionAtK() { return new PrecisionAtK( relevantRatingThreshold == null ? DEFAULT_RELEVANT_RATING_THRESHOLD : relevantRatingThreshold, ignoreUnlabeled == null ? DEFAULT_IGNORE_UNLABELED : ignoreUnlabeled, - k == null ? DEFAULT_K : k); + k == null ? DEFAULT_K : k + ); }); static { @@ -176,8 +178,7 @@ private boolean shouldCountUnlabeled() { * @return precision at k for above {@link SearchResult} list. **/ @Override - public EvalQueryQuality evaluate(String taskId, SearchHit[] hits, - List ratedDocs) { + public EvalQueryQuality evaluate(String taskId, SearchHit[] hits, List ratedDocs) { List ratedSearchHits = joinHitsWithRatings(hits, ratedDocs); @@ -243,8 +244,11 @@ public static final class Detail implements MetricDetail { this(in.readVInt(), in.readVInt()); } - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(NAME, true, args -> new Detail((Integer) args[0], (Integer) args[1])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + true, + args -> new Detail((Integer) args[0], (Integer) args[1]) + ); static { PARSER.declareInt(constructorArg(), RELEVANT_DOCS_RETRIEVED_FIELD); @@ -262,8 +266,7 @@ public void writeTo(StreamOutput out) throws IOException { } @Override - public XContentBuilder innerToXContent(XContentBuilder builder, Params params) - throws IOException { + public XContentBuilder innerToXContent(XContentBuilder builder, Params params) throws IOException { builder.field(RELEVANT_DOCS_RETRIEVED_FIELD.getPreferredName(), relevantRetrieved); builder.field(DOCS_RETRIEVED_FIELD.getPreferredName(), retrieved); return builder; @@ -291,8 +294,7 @@ public boolean equals(Object obj) { return false; } PrecisionAtK.Detail other = (PrecisionAtK.Detail) obj; - return Objects.equals(relevantRetrieved, other.relevantRetrieved) - && Objects.equals(retrieved, other.retrieved); + return Objects.equals(relevantRetrieved, other.relevantRetrieved) && Objects.equals(retrieved, other.retrieved); } @Override diff --git a/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RankEvalNamedXContentProvider.java b/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RankEvalNamedXContentProvider.java index ab86b606d2a2e..9239793066490 100644 --- a/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RankEvalNamedXContentProvider.java +++ b/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RankEvalNamedXContentProvider.java @@ -8,9 +8,9 @@ package org.elasticsearch.index.rankeval; -import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.plugins.spi.NamedXContentProvider; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import java.util.ArrayList; import java.util.List; @@ -20,27 +20,59 @@ public class RankEvalNamedXContentProvider implements NamedXContentProvider { @Override public List getNamedXContentParsers() { List namedXContent = new ArrayList<>(); - namedXContent.add(new NamedXContentRegistry.Entry(EvaluationMetric.class, new ParseField(PrecisionAtK.NAME), - PrecisionAtK::fromXContent)); - namedXContent.add(new NamedXContentRegistry.Entry(EvaluationMetric.class, new ParseField(RecallAtK.NAME), - RecallAtK::fromXContent)); - namedXContent.add(new NamedXContentRegistry.Entry(EvaluationMetric.class, new ParseField(MeanReciprocalRank.NAME), - MeanReciprocalRank::fromXContent)); - namedXContent.add(new NamedXContentRegistry.Entry(EvaluationMetric.class, new ParseField(DiscountedCumulativeGain.NAME), - DiscountedCumulativeGain::fromXContent)); - namedXContent.add(new NamedXContentRegistry.Entry(EvaluationMetric.class, new ParseField(ExpectedReciprocalRank.NAME), - ExpectedReciprocalRank::fromXContent)); + namedXContent.add( + new NamedXContentRegistry.Entry(EvaluationMetric.class, new ParseField(PrecisionAtK.NAME), PrecisionAtK::fromXContent) + ); + namedXContent.add(new NamedXContentRegistry.Entry(EvaluationMetric.class, new ParseField(RecallAtK.NAME), RecallAtK::fromXContent)); + namedXContent.add( + new NamedXContentRegistry.Entry( + EvaluationMetric.class, + new ParseField(MeanReciprocalRank.NAME), + MeanReciprocalRank::fromXContent + ) + ); + namedXContent.add( + new NamedXContentRegistry.Entry( + EvaluationMetric.class, + new ParseField(DiscountedCumulativeGain.NAME), + DiscountedCumulativeGain::fromXContent + ) + ); + namedXContent.add( + new NamedXContentRegistry.Entry( + EvaluationMetric.class, + new ParseField(ExpectedReciprocalRank.NAME), + ExpectedReciprocalRank::fromXContent + ) + ); - namedXContent.add(new NamedXContentRegistry.Entry(MetricDetail.class, new ParseField(PrecisionAtK.NAME), - PrecisionAtK.Detail::fromXContent)); - namedXContent.add(new NamedXContentRegistry.Entry(MetricDetail.class, new ParseField(RecallAtK.NAME), - RecallAtK.Detail::fromXContent)); - namedXContent.add(new NamedXContentRegistry.Entry(MetricDetail.class, new ParseField(MeanReciprocalRank.NAME), - MeanReciprocalRank.Detail::fromXContent)); - namedXContent.add(new NamedXContentRegistry.Entry(MetricDetail.class, new ParseField(DiscountedCumulativeGain.NAME), - DiscountedCumulativeGain.Detail::fromXContent)); - namedXContent.add(new NamedXContentRegistry.Entry(MetricDetail.class, new ParseField(ExpectedReciprocalRank.NAME), - ExpectedReciprocalRank.Detail::fromXContent)); + namedXContent.add( + new NamedXContentRegistry.Entry(MetricDetail.class, new ParseField(PrecisionAtK.NAME), PrecisionAtK.Detail::fromXContent) + ); + namedXContent.add( + new NamedXContentRegistry.Entry(MetricDetail.class, new ParseField(RecallAtK.NAME), RecallAtK.Detail::fromXContent) + ); + namedXContent.add( + new NamedXContentRegistry.Entry( + MetricDetail.class, + new ParseField(MeanReciprocalRank.NAME), + MeanReciprocalRank.Detail::fromXContent + ) + ); + namedXContent.add( + new NamedXContentRegistry.Entry( + MetricDetail.class, + new ParseField(DiscountedCumulativeGain.NAME), + DiscountedCumulativeGain.Detail::fromXContent + ) + ); + namedXContent.add( + new NamedXContentRegistry.Entry( + MetricDetail.class, + new ParseField(ExpectedReciprocalRank.NAME), + ExpectedReciprocalRank.Detail::fromXContent + ) + ); return namedXContent; } } diff --git a/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RankEvalPlugin.java b/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RankEvalPlugin.java index d22702321baac..03216a937d694 100644 --- a/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RankEvalPlugin.java +++ b/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RankEvalPlugin.java @@ -17,11 +17,11 @@ import org.elasticsearch.common.settings.IndexScopedSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsFilter; -import org.elasticsearch.xcontent.NamedXContentRegistry.Entry; import org.elasticsearch.plugins.ActionPlugin; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.rest.RestController; import org.elasticsearch.rest.RestHandler; +import org.elasticsearch.xcontent.NamedXContentRegistry.Entry; import java.util.ArrayList; import java.util.Arrays; @@ -37,9 +37,15 @@ public class RankEvalPlugin extends Plugin implements ActionPlugin { } @Override - public List getRestHandlers(Settings settings, RestController restController, ClusterSettings clusterSettings, - IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter, IndexNameExpressionResolver indexNameExpressionResolver, - Supplier nodesInCluster) { + public List getRestHandlers( + Settings settings, + RestController restController, + ClusterSettings clusterSettings, + IndexScopedSettings indexScopedSettings, + SettingsFilter settingsFilter, + IndexNameExpressionResolver indexNameExpressionResolver, + Supplier nodesInCluster + ) { return Collections.singletonList(new RestRankEvalAction()); } @@ -50,16 +56,20 @@ public List getNamedWriteables() { namedWriteables.add(new NamedWriteableRegistry.Entry(EvaluationMetric.class, RecallAtK.NAME, RecallAtK::new)); namedWriteables.add(new NamedWriteableRegistry.Entry(EvaluationMetric.class, MeanReciprocalRank.NAME, MeanReciprocalRank::new)); namedWriteables.add( - new NamedWriteableRegistry.Entry(EvaluationMetric.class, DiscountedCumulativeGain.NAME, DiscountedCumulativeGain::new)); + new NamedWriteableRegistry.Entry(EvaluationMetric.class, DiscountedCumulativeGain.NAME, DiscountedCumulativeGain::new) + ); namedWriteables.add( - new NamedWriteableRegistry.Entry(EvaluationMetric.class, ExpectedReciprocalRank.NAME, ExpectedReciprocalRank::new)); + new NamedWriteableRegistry.Entry(EvaluationMetric.class, ExpectedReciprocalRank.NAME, ExpectedReciprocalRank::new) + ); namedWriteables.add(new NamedWriteableRegistry.Entry(MetricDetail.class, PrecisionAtK.NAME, PrecisionAtK.Detail::new)); namedWriteables.add(new NamedWriteableRegistry.Entry(MetricDetail.class, RecallAtK.NAME, RecallAtK.Detail::new)); namedWriteables.add(new NamedWriteableRegistry.Entry(MetricDetail.class, MeanReciprocalRank.NAME, MeanReciprocalRank.Detail::new)); namedWriteables.add( - new NamedWriteableRegistry.Entry(MetricDetail.class, DiscountedCumulativeGain.NAME, DiscountedCumulativeGain.Detail::new)); + new NamedWriteableRegistry.Entry(MetricDetail.class, DiscountedCumulativeGain.NAME, DiscountedCumulativeGain.Detail::new) + ); namedWriteables.add( - new NamedWriteableRegistry.Entry(MetricDetail.class, ExpectedReciprocalRank.NAME, ExpectedReciprocalRank.Detail::new)); + new NamedWriteableRegistry.Entry(MetricDetail.class, ExpectedReciprocalRank.NAME, ExpectedReciprocalRank.Detail::new) + ); return namedWriteables; } diff --git a/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RankEvalRequest.java b/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RankEvalRequest.java index 776b1577f760b..bea4d4222fa58 100644 --- a/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RankEvalRequest.java +++ b/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RankEvalRequest.java @@ -30,7 +30,7 @@ public class RankEvalRequest extends ActionRequest implements IndicesRequest.Rep private RankEvalSpec rankingEvaluationSpec; - private IndicesOptions indicesOptions = SearchRequest.DEFAULT_INDICES_OPTIONS; + private IndicesOptions indicesOptions = SearchRequest.DEFAULT_INDICES_OPTIONS; private String[] indices = Strings.EMPTY_ARRAY; private SearchType searchType = SearchType.DEFAULT; @@ -50,8 +50,7 @@ public RankEvalRequest(RankEvalSpec rankingEvaluationSpec, String[] indices) { } } - RankEvalRequest() { - } + RankEvalRequest() {} @Override public ActionRequestValidationException validate() { @@ -141,10 +140,10 @@ public boolean equals(Object o) { return false; } RankEvalRequest that = (RankEvalRequest) o; - return Objects.equals(indicesOptions, that.indicesOptions) && - Arrays.equals(indices, that.indices) && - Objects.equals(rankingEvaluationSpec, that.rankingEvaluationSpec) && - Objects.equals(searchType, that.searchType); + return Objects.equals(indicesOptions, that.indicesOptions) + && Arrays.equals(indices, that.indices) + && Objects.equals(rankingEvaluationSpec, that.rankingEvaluationSpec) + && Objects.equals(searchType, that.searchType); } @Override diff --git a/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RankEvalRequestBuilder.java b/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RankEvalRequestBuilder.java index 4d745ebcd7dc7..1e9011e6112b3 100644 --- a/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RankEvalRequestBuilder.java +++ b/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RankEvalRequestBuilder.java @@ -8,14 +8,13 @@ package org.elasticsearch.index.rankeval; -import org.elasticsearch.action.ActionType; import org.elasticsearch.action.ActionRequestBuilder; +import org.elasticsearch.action.ActionType; import org.elasticsearch.client.ElasticsearchClient; public class RankEvalRequestBuilder extends ActionRequestBuilder { - public RankEvalRequestBuilder(ElasticsearchClient client, ActionType action, - RankEvalRequest request) { + public RankEvalRequestBuilder(ElasticsearchClient client, ActionType action, RankEvalRequest request) { super(client, action, request); } diff --git a/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RankEvalResponse.java b/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RankEvalResponse.java index 69921fdc8f7ab..9072d3a08eea3 100644 --- a/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RankEvalResponse.java +++ b/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RankEvalResponse.java @@ -10,16 +10,16 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.action.ActionResponse; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.common.xcontent.XContentParserUtils; +import org.elasticsearch.core.Tuple; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.XContentParserUtils; import java.io.IOException; import java.util.Collections; @@ -43,10 +43,9 @@ public class RankEvalResponse extends ActionResponse implements ToXContentObject /** exceptions for specific ranking evaluation queries, keyed by their id */ private Map failures; - public RankEvalResponse(double metricScore, Map partialResults, - Map failures) { + public RankEvalResponse(double metricScore, Map partialResults, Map failures) { this.metricScore = metricScore; - this.details = new HashMap<>(partialResults); + this.details = new HashMap<>(partialResults); this.failures = new HashMap<>(failures); } @@ -123,15 +122,22 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws private static final ParseField DETAILS_FIELD = new ParseField("details"); private static final ParseField FAILURES_FIELD = new ParseField("failures"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("rank_eval_response", - true, - a -> new RankEvalResponse((Double) a[0], - ((List) a[1]).stream().collect(Collectors.toMap(EvalQueryQuality::getId, Function.identity())), - ((List>) a[2]).stream().collect(Collectors.toMap(Tuple::v1, Tuple::v2)))); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "rank_eval_response", + true, + a -> new RankEvalResponse( + (Double) a[0], + ((List) a[1]).stream().collect(Collectors.toMap(EvalQueryQuality::getId, Function.identity())), + ((List>) a[2]).stream().collect(Collectors.toMap(Tuple::v1, Tuple::v2)) + ) + ); static { PARSER.declareDouble(ConstructingObjectParser.constructorArg(), EvalQueryQuality.METRIC_SCORE_FIELD); - PARSER.declareNamedObjects(ConstructingObjectParser.optionalConstructorArg(), (p, c, n) -> EvalQueryQuality.fromXContent(p, n), - DETAILS_FIELD); + PARSER.declareNamedObjects( + ConstructingObjectParser.optionalConstructorArg(), + (p, c, n) -> EvalQueryQuality.fromXContent(p, n), + DETAILS_FIELD + ); PARSER.declareNamedObjects(ConstructingObjectParser.optionalConstructorArg(), (p, c, n) -> { XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, p.nextToken(), p); XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, p.nextToken(), p); diff --git a/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RankEvalSpec.java b/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RankEvalSpec.java index 3d38d7a3d0e29..3f816e1479a66 100644 --- a/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RankEvalSpec.java +++ b/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RankEvalSpec.java @@ -8,18 +8,18 @@ package org.elasticsearch.index.rankeval; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.common.xcontent.XContentParserUtils; +import org.elasticsearch.script.Script; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.XContentParserUtils; -import org.elasticsearch.script.Script; import java.io.IOException; import java.util.ArrayList; @@ -52,14 +52,18 @@ public RankEvalSpec(List ratedRequests, EvaluationMetric metric, C this.metric = Objects.requireNonNull(metric, "Cannot evaluate ranking if no evaluation metric is provided."); if (ratedRequests == null || ratedRequests.isEmpty()) { throw new IllegalArgumentException( - "Cannot evaluate ranking if no search requests with rated results are provided. Seen: " + ratedRequests); + "Cannot evaluate ranking if no search requests with rated results are provided. Seen: " + ratedRequests + ); } this.ratedRequests = ratedRequests; if (templates == null || templates.isEmpty()) { for (RatedRequest request : ratedRequests) { if (request.getEvaluationRequest() == null) { - throw new IllegalStateException("Cannot evaluate ranking if neither template nor evaluation request is " - + "provided. Seen for request id: " + request.getId()); + throw new IllegalStateException( + "Cannot evaluate ranking if neither template nor evaluation request is " + + "provided. Seen for request id: " + + request.getId() + ); } } } @@ -135,14 +139,19 @@ public void setMaxConcurrentSearches(int maxConcurrentSearches) { private static final ParseField REQUESTS_FIELD = new ParseField("requests"); private static final ParseField MAX_CONCURRENT_SEARCHES_FIELD = new ParseField("max_concurrent_searches"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("rank_eval", - a -> new RankEvalSpec((List) a[0], (EvaluationMetric) a[1], (Collection) a[2])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "rank_eval", + a -> new RankEvalSpec((List) a[0], (EvaluationMetric) a[1], (Collection) a[2]) + ); static { PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), (p, c) -> RatedRequest.fromXContent(p), REQUESTS_FIELD); PARSER.declareObject(ConstructingObjectParser.constructorArg(), (p, c) -> parseMetric(p), METRIC_FIELD); - PARSER.declareObjectArray(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> ScriptWithId.fromXContent(p), - TEMPLATES_FIELD); + PARSER.declareObjectArray( + ConstructingObjectParser.optionalConstructorArg(), + (p, c) -> ScriptWithId.fromXContent(p), + TEMPLATES_FIELD + ); PARSER.declareInt(RankEvalSpec::setMaxConcurrentSearches, MAX_CONCURRENT_SEARCHES_FIELD); } @@ -170,9 +179,10 @@ static class ScriptWithId { this.script = script; } - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("script_with_id", - a -> new ScriptWithId((String) a[0], (Script) a[1])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "script_with_id", + a -> new ScriptWithId((String) a[0], (Script) a[1]) + ); public static ScriptWithId fromXContent(XContentParser parser) { return PARSER.apply(parser, null); @@ -228,10 +238,10 @@ public final boolean equals(Object obj) { } RankEvalSpec other = (RankEvalSpec) obj; - return Objects.equals(ratedRequests, other.ratedRequests) && - Objects.equals(metric, other.metric) && - Objects.equals(maxConcurrentSearches, other.maxConcurrentSearches) && - Objects.equals(templates, other.templates); + return Objects.equals(ratedRequests, other.ratedRequests) + && Objects.equals(metric, other.metric) + && Objects.equals(maxConcurrentSearches, other.maxConcurrentSearches) + && Objects.equals(templates, other.templates); } @Override diff --git a/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RatedDocument.java b/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RatedDocument.java index d7d0ad0b93991..f4e0ca946c16e 100644 --- a/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RatedDocument.java +++ b/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RatedDocument.java @@ -8,12 +8,12 @@ package org.elasticsearch.index.rankeval; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -41,8 +41,10 @@ public class RatedDocument implements Writeable, ToXContentObject { static final ParseField DOC_ID_FIELD = new ParseField("_id"); static final ParseField INDEX_FIELD = new ParseField("_index"); - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("rated_document", - a -> new RatedDocument((String) a[0], (String) a[1], (Integer) a[2])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "rated_document", + a -> new RatedDocument((String) a[0], (String) a[1], (Integer) a[2]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), INDEX_FIELD); diff --git a/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RatedRequest.java b/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RatedRequest.java index 21bf7d3845481..84b738aca204d 100644 --- a/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RatedRequest.java +++ b/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RatedRequest.java @@ -8,18 +8,18 @@ package org.elasticsearch.index.rankeval; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.index.rankeval.RatedDocument.DocumentKey; +import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.index.rankeval.RatedDocument.DocumentKey; -import org.elasticsearch.search.builder.SearchSourceBuilder; import java.io.IOException; import java.util.ArrayList; @@ -85,8 +85,7 @@ public class RatedRequest implements Writeable, ToXContentObject { * @param params template parameters * @param templateId a templare id */ - public RatedRequest(String id, List ratedDocs, Map params, - String templateId) { + public RatedRequest(String id, List ratedDocs, Map params, String templateId) { this(id, ratedDocs, null, params, templateId); } @@ -102,15 +101,22 @@ public RatedRequest(String id, List ratedDocs, SearchSourceBuilde this(id, ratedDocs, evaluatedQuery, new HashMap<>(), null); } - private RatedRequest(String id, List ratedDocs, SearchSourceBuilder evaluatedQuery, - Map params, String templateId) { + private RatedRequest( + String id, + List ratedDocs, + SearchSourceBuilder evaluatedQuery, + Map params, + String templateId + ) { if (params != null && (params.size() > 0 && evaluatedQuery != null)) { throw new IllegalArgumentException( - "Ambiguous rated request: Set both, verbatim test request and test request " + "template parameters."); + "Ambiguous rated request: Set both, verbatim test request and test request " + "template parameters." + ); } if (templateId != null && evaluatedQuery != null) { throw new IllegalArgumentException( - "Ambiguous rated request: Set both, verbatim test request and test request " + "template parameters."); + "Ambiguous rated request: Set both, verbatim test request and test request " + "template parameters." + ); } if ((params == null || params.size() < 1) && evaluatedQuery == null) { throw new IllegalArgumentException("Need to set at least test request or test request template parameters."); @@ -126,7 +132,8 @@ private RatedRequest(String id, List ratedDocs, SearchSourceBuild if (docKeys.add(doc.getKey()) == false) { String docKeyToString = doc.getKey().toString().replaceAll("\n", "").replaceAll(" ", " "); throw new IllegalArgumentException( - "Found duplicate rated document key [" + docKeyToString + "] in evaluation request [" + id + "]"); + "Found duplicate rated document key [" + docKeyToString + "] in evaluation request [" + id + "]" + ); } } @@ -239,17 +246,29 @@ public void addSummaryFields(List summaryFields) { private static final ParseField TEMPLATE_ID_FIELD = new ParseField("template_id"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("request", - a -> new RatedRequest((String) a[0], (List) a[1], (SearchSourceBuilder) a[2], (Map) a[3], - (String) a[4])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "request", + a -> new RatedRequest( + (String) a[0], + (List) a[1], + (SearchSourceBuilder) a[2], + (Map) a[3], + (String) a[4] + ) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), ID_FIELD); - PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), (p, c) -> { - return RatedDocument.fromXContent(p); - }, RATINGS_FIELD); - PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> - SearchSourceBuilder.fromXContent(p, false), REQUEST_FIELD); + PARSER.declareObjectArray( + ConstructingObjectParser.constructorArg(), + (p, c) -> { return RatedDocument.fromXContent(p); }, + RATINGS_FIELD + ); + PARSER.declareObject( + ConstructingObjectParser.optionalConstructorArg(), + (p, c) -> SearchSourceBuilder.fromXContent(p, false), + REQUEST_FIELD + ); PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> p.map(), PARAMS_FIELD); PARSER.declareStringArray(RatedRequest::addSummaryFields, FIELDS_FIELD); PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), TEMPLATE_ID_FIELD); @@ -311,16 +330,16 @@ public final boolean equals(Object obj) { RatedRequest other = (RatedRequest) obj; - return Objects.equals(id, other.id) && Objects.equals(evaluationRequest, other.evaluationRequest) - && Objects.equals(summaryFields, other.summaryFields) - && Objects.equals(ratedDocs, other.ratedDocs) - && Objects.equals(params, other.params) - && Objects.equals(templateId, other.templateId); + return Objects.equals(id, other.id) + && Objects.equals(evaluationRequest, other.evaluationRequest) + && Objects.equals(summaryFields, other.summaryFields) + && Objects.equals(ratedDocs, other.ratedDocs) + && Objects.equals(params, other.params) + && Objects.equals(templateId, other.templateId); } @Override public final int hashCode() { - return Objects.hash(id, evaluationRequest, summaryFields, ratedDocs, params, - templateId); + return Objects.hash(id, evaluationRequest, summaryFields, ratedDocs, params, templateId); } } diff --git a/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RatedSearchHit.java b/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RatedSearchHit.java index 571e7e8ddf9af..ffaa333dc74b7 100644 --- a/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RatedSearchHit.java +++ b/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RatedSearchHit.java @@ -8,17 +8,17 @@ package org.elasticsearch.index.rankeval; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.search.SearchHit; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.search.SearchHit; import java.io.IOException; import java.util.Objects; @@ -59,8 +59,7 @@ public OptionalInt getRating() { } @Override - public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) - throws IOException { + public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { builder.startObject(); builder.field("hit", (ToXContent) searchHit); builder.field("rating", rating.isPresent() ? rating.getAsInt() : null); @@ -70,14 +69,20 @@ public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params par private static final ParseField HIT_FIELD = new ParseField("hit"); private static final ParseField RATING_FIELD = new ParseField("rating"); - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("rated_hit", true, - a -> new RatedSearchHit((SearchHit) a[0], (OptionalInt) a[1])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "rated_hit", + true, + a -> new RatedSearchHit((SearchHit) a[0], (OptionalInt) a[1]) + ); static { PARSER.declareObject(ConstructingObjectParser.constructorArg(), (p, c) -> SearchHit.fromXContent(p), HIT_FIELD); - PARSER.declareField(ConstructingObjectParser.constructorArg(), - (p) -> p.currentToken() == XContentParser.Token.VALUE_NULL ? OptionalInt.empty() : OptionalInt.of(p.intValue()), - RATING_FIELD, ValueType.INT_OR_NULL); + PARSER.declareField( + ConstructingObjectParser.constructorArg(), + (p) -> p.currentToken() == XContentParser.Token.VALUE_NULL ? OptionalInt.empty() : OptionalInt.of(p.intValue()), + RATING_FIELD, + ValueType.INT_OR_NULL + ); } public static RatedSearchHit parse(XContentParser parser) throws IOException { @@ -93,8 +98,7 @@ public final boolean equals(Object obj) { return false; } RatedSearchHit other = (RatedSearchHit) obj; - return Objects.equals(rating, other.rating) - && Objects.equals(searchHit, other.searchHit); + return Objects.equals(rating, other.rating) && Objects.equals(searchHit, other.searchHit); } @Override diff --git a/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RecallAtK.java b/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RecallAtK.java index 64ec8dfa27b50..d3d0a3110ee58 100644 --- a/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RecallAtK.java +++ b/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RecallAtK.java @@ -8,13 +8,13 @@ package org.elasticsearch.index.rankeval; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.search.SearchHit; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.search.SearchHit; import java.io.IOException; import java.util.List; @@ -23,9 +23,9 @@ import javax.naming.directory.SearchResult; +import static org.elasticsearch.index.rankeval.EvaluationMetric.joinHitsWithRatings; import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg; import static org.elasticsearch.xcontent.ConstructingObjectParser.optionalConstructorArg; -import static org.elasticsearch.index.rankeval.EvaluationMetric.joinHitsWithRatings; /** * Metric implementing Recall@K @@ -79,7 +79,8 @@ public RecallAtK() { Integer k = (Integer) args[1]; return new RecallAtK( relevantRatingThreshold == null ? DEFAULT_RELEVANT_RATING_THRESHOLD : relevantRatingThreshold, - k == null ? DEFAULT_K : k); + k == null ? DEFAULT_K : k + ); }); static { @@ -143,8 +144,7 @@ private boolean isRelevant(int rating) { * @return recall at k for above {@link SearchResult} list. **/ @Override - public EvalQueryQuality evaluate(String taskId, SearchHit[] hits, - List ratedDocs) { + public EvalQueryQuality evaluate(String taskId, SearchHit[] hits, List ratedDocs) { List ratedSearchHits = joinHitsWithRatings(hits, ratedDocs); @@ -158,7 +158,7 @@ public EvalQueryQuality evaluate(String taskId, SearchHit[] hits, int relevant = 0; for (RatedDocument rd : ratedDocs) { - if(isRelevant(rd.getRating())) { + if (isRelevant(rd.getRating())) { relevant++; } } @@ -183,8 +183,7 @@ public final boolean equals(Object obj) { return false; } RecallAtK other = (RecallAtK) obj; - return Objects.equals(relevantRatingThreshold, other.relevantRatingThreshold) - && Objects.equals(k, other.k); + return Objects.equals(relevantRatingThreshold, other.relevantRatingThreshold) && Objects.equals(k, other.k); } @Override @@ -209,8 +208,11 @@ public static final class Detail implements MetricDetail { this.relevant = in.readVLong(); } - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(NAME, true, args -> new Detail((Integer) args[0], (Integer) args[1])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + true, + args -> new Detail((Integer) args[0], (Integer) args[1]) + ); static { PARSER.declareInt(constructorArg(), RELEVANT_DOCS_RETRIEVED_FIELD); @@ -228,8 +230,7 @@ public void writeTo(StreamOutput out) throws IOException { } @Override - public XContentBuilder innerToXContent(XContentBuilder builder, Params params) - throws IOException { + public XContentBuilder innerToXContent(XContentBuilder builder, Params params) throws IOException { builder.field(RELEVANT_DOCS_RETRIEVED_FIELD.getPreferredName(), relevantRetrieved); builder.field(RELEVANT_DOCS_FIELD.getPreferredName(), relevant); return builder; @@ -257,8 +258,7 @@ public boolean equals(Object obj) { return false; } RecallAtK.Detail other = (RecallAtK.Detail) obj; - return Objects.equals(relevantRetrieved, other.relevantRetrieved) - && Objects.equals(relevant, other.relevant); + return Objects.equals(relevantRetrieved, other.relevantRetrieved) && Objects.equals(relevant, other.relevant); } @Override diff --git a/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RestRankEvalAction.java b/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RestRankEvalAction.java index 51f5c40bceaa1..24db74263912d 100644 --- a/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RestRankEvalAction.java +++ b/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/RestRankEvalAction.java @@ -12,10 +12,10 @@ import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.common.Strings; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestToXContentListener; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.List; @@ -85,7 +85,8 @@ public List routes() { new Route(GET, "/" + ENDPOINT), new Route(POST, "/" + ENDPOINT), new Route(GET, "/{index}/" + ENDPOINT), - new Route(POST, "/{index}/" + ENDPOINT)); + new Route(POST, "/{index}/" + ENDPOINT) + ); } @Override @@ -94,8 +95,11 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli try (XContentParser parser = request.contentOrSourceParamParser()) { parseRankEvalRequest(rankEvalRequest, request, parser); } - return channel -> client.executeLocally(RankEvalAction.INSTANCE, rankEvalRequest, - new RestToXContentListener(channel)); + return channel -> client.executeLocally( + RankEvalAction.INSTANCE, + rankEvalRequest, + new RestToXContentListener(channel) + ); } private static void parseRankEvalRequest(RankEvalRequest rankEvalRequest, RestRequest request, XContentParser parser) { diff --git a/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/TransportRankEvalAction.java b/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/TransportRankEvalAction.java index 87b08196d058d..fc44f53273df1 100644 --- a/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/TransportRankEvalAction.java +++ b/modules/rank-eval/src/main/java/org/elasticsearch/index/rankeval/TransportRankEvalAction.java @@ -19,9 +19,6 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptService; import org.elasticsearch.script.TemplateScript; @@ -29,6 +26,9 @@ import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.tasks.Task; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; import java.io.IOException; import java.util.ArrayList; @@ -60,8 +60,13 @@ public class TransportRankEvalAction extends HandledTransportAction { @@ -131,8 +149,12 @@ static class RankEvalActionListener extends ActionListener.Delegating errors; private final EvaluationMetric metric; - RankEvalActionListener(ActionListener listener, EvaluationMetric metric, RatedRequest[] specifications, - Map errors) { + RankEvalActionListener( + ActionListener listener, + EvaluationMetric metric, + RatedRequest[] specifications, + Map errors + ) { super(listener); this.metric = metric; this.errors = errors; diff --git a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/DiscountedCumulativeGainTests.java b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/DiscountedCumulativeGainTests.java index 2b7a6df40a3af..9273b8b913094 100644 --- a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/DiscountedCumulativeGainTests.java +++ b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/DiscountedCumulativeGainTests.java @@ -11,6 +11,10 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; +import org.elasticsearch.index.shard.ShardId; +import org.elasticsearch.search.SearchHit; +import org.elasticsearch.search.SearchShardTarget; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; @@ -18,10 +22,6 @@ import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.index.shard.ShardId; -import org.elasticsearch.search.SearchHit; -import org.elasticsearch.search.SearchShardTarget; -import org.elasticsearch.test.ESTestCase; import java.io.IOException; import java.util.ArrayList; @@ -232,7 +232,7 @@ public void testParseFromXContent() throws IOException { } private void assertParsedCorrect(String xContent, Integer expectedUnknownDocRating, boolean expectedNormalize, int expectedK) - throws IOException { + throws IOException { try (XContentParser parser = createParser(JsonXContent.jsonXContent, xContent)) { DiscountedCumulativeGain dcgAt = DiscountedCumulativeGain.fromXContent(parser); assertEquals(expectedUnknownDocRating, dcgAt.getUnknownDocRating()); @@ -269,8 +269,10 @@ public void testXContentParsingIsNotLenient() throws IOException { try (XContentParser parser = createParser(xContentType.xContent(), withRandomFields)) { parser.nextToken(); parser.nextToken(); - XContentParseException exception = expectThrows(XContentParseException.class, - () -> DiscountedCumulativeGain.fromXContent(parser)); + XContentParseException exception = expectThrows( + XContentParseException.class, + () -> DiscountedCumulativeGain.fromXContent(parser) + ); assertThat(exception.getMessage(), containsString("[dcg] unknown field")); } } @@ -286,8 +288,18 @@ public void testMetricDetails() { assertEquals(expectedNdcg, detail.getNDCG(), 0.0); assertEquals(unratedDocs, detail.getUnratedDocs()); if (idcg != 0) { - assertEquals("{\"dcg\":{\"dcg\":" + dcg + ",\"ideal_dcg\":" + idcg + ",\"normalized_dcg\":" + expectedNdcg - + ",\"unrated_docs\":" + unratedDocs + "}}", Strings.toString(detail)); + assertEquals( + "{\"dcg\":{\"dcg\":" + + dcg + + ",\"ideal_dcg\":" + + idcg + + ",\"normalized_dcg\":" + + expectedNdcg + + ",\"unrated_docs\":" + + unratedDocs + + "}}", + Strings.toString(detail) + ); } else { assertEquals("{\"dcg\":{\"dcg\":" + dcg + ",\"unrated_docs\":" + unratedDocs + "}}", Strings.toString(detail)); } @@ -295,31 +307,42 @@ public void testMetricDetails() { public void testSerialization() throws IOException { DiscountedCumulativeGain original = createTestItem(); - DiscountedCumulativeGain deserialized = ESTestCase.copyWriteable(original, new NamedWriteableRegistry(Collections.emptyList()), - DiscountedCumulativeGain::new); + DiscountedCumulativeGain deserialized = ESTestCase.copyWriteable( + original, + new NamedWriteableRegistry(Collections.emptyList()), + DiscountedCumulativeGain::new + ); assertEquals(deserialized, original); assertEquals(deserialized.hashCode(), original.hashCode()); assertNotSame(deserialized, original); } public void testEqualsAndHash() throws IOException { - checkEqualsAndHashCode(createTestItem(), original -> { - return new DiscountedCumulativeGain(original.getNormalize(), original.getUnknownDocRating(), original.getK()); - }, DiscountedCumulativeGainTests::mutateTestItem); + checkEqualsAndHashCode( + createTestItem(), + original -> { return new DiscountedCumulativeGain(original.getNormalize(), original.getUnknownDocRating(), original.getK()); }, + DiscountedCumulativeGainTests::mutateTestItem + ); } private static DiscountedCumulativeGain mutateTestItem(DiscountedCumulativeGain original) { switch (randomIntBetween(0, 2)) { - case 0: - return new DiscountedCumulativeGain(original.getNormalize() == false, original.getUnknownDocRating(), original.getK()); - case 1: - return new DiscountedCumulativeGain(original.getNormalize(), - randomValueOtherThan(original.getUnknownDocRating(), () -> randomIntBetween(0, 10)), original.getK()); - case 2: - return new DiscountedCumulativeGain(original.getNormalize(), original.getUnknownDocRating(), - randomValueOtherThan(original.getK(), () -> randomIntBetween(1, 10))); - default: - throw new IllegalArgumentException("mutation variant not allowed"); + case 0: + return new DiscountedCumulativeGain(original.getNormalize() == false, original.getUnknownDocRating(), original.getK()); + case 1: + return new DiscountedCumulativeGain( + original.getNormalize(), + randomValueOtherThan(original.getUnknownDocRating(), () -> randomIntBetween(0, 10)), + original.getK() + ); + case 2: + return new DiscountedCumulativeGain( + original.getNormalize(), + original.getUnknownDocRating(), + randomValueOtherThan(original.getK(), () -> randomIntBetween(1, 10)) + ); + default: + throw new IllegalArgumentException("mutation variant not allowed"); } } } diff --git a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/EvalQueryQualityTests.java b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/EvalQueryQualityTests.java index adaa984b4a817..ef69287e36f9f 100644 --- a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/EvalQueryQualityTests.java +++ b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/EvalQueryQualityTests.java @@ -10,13 +10,13 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; +import org.elasticsearch.index.shard.ShardId; +import org.elasticsearch.search.SearchShardTarget; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.index.shard.ShardId; -import org.elasticsearch.search.SearchShardTarget; -import org.elasticsearch.test.ESTestCase; import java.io.IOException; import java.util.ArrayList; @@ -48,23 +48,27 @@ public static EvalQueryQuality randomEvalQueryQuality() { ratedSearchHit.getSearchHit().shard(new SearchShardTarget("_na_", new ShardId("index", "_na_", 0), null)); ratedHits.add(ratedSearchHit); } - EvalQueryQuality evalQueryQuality = new EvalQueryQuality(randomAlphaOfLength(10), - randomDoubleBetween(0.0, 1.0, true)); + EvalQueryQuality evalQueryQuality = new EvalQueryQuality(randomAlphaOfLength(10), randomDoubleBetween(0.0, 1.0, true)); if (randomBoolean()) { int metricDetail = randomIntBetween(0, 2); switch (metricDetail) { - case 0: - evalQueryQuality.setMetricDetails(new PrecisionAtK.Detail(randomIntBetween(0, 1000), randomIntBetween(0, 1000))); - break; - case 1: - evalQueryQuality.setMetricDetails(new MeanReciprocalRank.Detail(randomIntBetween(0, 1000))); - break; - case 2: - evalQueryQuality.setMetricDetails(new DiscountedCumulativeGain.Detail(randomDoubleBetween(0, 1, true), - randomBoolean() ? randomDoubleBetween(0, 1, true) : 0, randomInt())); - break; - default: - throw new IllegalArgumentException("illegal randomized value in test"); + case 0: + evalQueryQuality.setMetricDetails(new PrecisionAtK.Detail(randomIntBetween(0, 1000), randomIntBetween(0, 1000))); + break; + case 1: + evalQueryQuality.setMetricDetails(new MeanReciprocalRank.Detail(randomIntBetween(0, 1000))); + break; + case 2: + evalQueryQuality.setMetricDetails( + new DiscountedCumulativeGain.Detail( + randomDoubleBetween(0, 1, true), + randomBoolean() ? randomDoubleBetween(0, 1, true) : 0, + randomInt() + ) + ); + break; + default: + throw new IllegalArgumentException("illegal randomized value in test"); } } evalQueryQuality.addHitsAndRatings(ratedHits); @@ -122,24 +126,24 @@ private static EvalQueryQuality mutateTestItem(EvalQueryQuality original) { List ratedHits = new ArrayList<>(original.getHitsAndRatings()); MetricDetail metricDetails = original.getMetricDetails(); switch (randomIntBetween(0, 3)) { - case 0: - id = id + "_"; - break; - case 1: - metricScore = metricScore + 0.1; - break; - case 2: - if (metricDetails == null) { - metricDetails = new PrecisionAtK.Detail(1, 5); - } else { - metricDetails = null; - } - break; - case 3: - ratedHits.add(RatedSearchHitTests.randomRatedSearchHit()); - break; - default: - throw new IllegalStateException("The test should only allow four parameters mutated"); + case 0: + id = id + "_"; + break; + case 1: + metricScore = metricScore + 0.1; + break; + case 2: + if (metricDetails == null) { + metricDetails = new PrecisionAtK.Detail(1, 5); + } else { + metricDetails = null; + } + break; + case 3: + ratedHits.add(RatedSearchHitTests.randomRatedSearchHit()); + break; + default: + throw new IllegalStateException("The test should only allow four parameters mutated"); } EvalQueryQuality evalQueryQuality = new EvalQueryQuality(id, metricScore); evalQueryQuality.setMetricDetails(metricDetails); diff --git a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/ExpectedReciprocalRankTests.java b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/ExpectedReciprocalRankTests.java index 1447419366c8b..5a95d8813b16f 100644 --- a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/ExpectedReciprocalRankTests.java +++ b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/ExpectedReciprocalRankTests.java @@ -10,6 +10,10 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; +import org.elasticsearch.index.shard.ShardId; +import org.elasticsearch.search.SearchHit; +import org.elasticsearch.search.SearchShardTarget; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; @@ -17,10 +21,6 @@ import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.index.shard.ShardId; -import org.elasticsearch.search.SearchHit; -import org.elasticsearch.search.SearchShardTarget; -import org.elasticsearch.test.ESTestCase; import java.io.IOException; import java.util.ArrayList; @@ -38,11 +38,11 @@ public class ExpectedReciprocalRankTests extends ESTestCase { public void testProbabilityOfRelevance() { ExpectedReciprocalRank err = new ExpectedReciprocalRank(5); assertEquals(0.0, err.probabilityOfRelevance(0), 0.0); - assertEquals(1d/32d, err.probabilityOfRelevance(1), 0.0); - assertEquals(3d/32d, err.probabilityOfRelevance(2), 0.0); - assertEquals(7d/32d, err.probabilityOfRelevance(3), 0.0); - assertEquals(15d/32d, err.probabilityOfRelevance(4), 0.0); - assertEquals(31d/32d, err.probabilityOfRelevance(5), 0.0); + assertEquals(1d / 32d, err.probabilityOfRelevance(1), 0.0); + assertEquals(3d / 32d, err.probabilityOfRelevance(2), 0.0); + assertEquals(7d / 32d, err.probabilityOfRelevance(3), 0.0); + assertEquals(15d / 32d, err.probabilityOfRelevance(4), 0.0); + assertEquals(31d / 32d, err.probabilityOfRelevance(5), 0.0); } /** @@ -61,7 +61,7 @@ public void testProbabilityOfRelevance() { */ public void testERRAt() { List rated = new ArrayList<>(); - Integer[] relevanceRatings = new Integer[] { 3, 2, 0, 1}; + Integer[] relevanceRatings = new Integer[] { 3, 2, 0, 1 }; SearchHit[] hits = createSearchHits(rated, relevanceRatings); ExpectedReciprocalRank err = new ExpectedReciprocalRank(3, 0, 3); assertEquals(0.8984375, err.evaluate("id", hits, rated).metricScore(), DELTA); @@ -86,7 +86,7 @@ public void testERRAt() { */ public void testERRMissingRatings() { List rated = new ArrayList<>(); - Integer[] relevanceRatings = new Integer[] { 3, null, 0, 1}; + Integer[] relevanceRatings = new Integer[] { 3, null, 0, 1 }; SearchHit[] hits = createSearchHits(rated, relevanceRatings); ExpectedReciprocalRank err = new ExpectedReciprocalRank(3, null, 4); EvalQueryQuality evaluation = err.evaluate("id", hits, rated); @@ -124,7 +124,7 @@ public void testParseFromXContent() throws IOException { } private void assertParsedCorrect(String xContent, Integer expectedUnknownDocRating, int expectedMaxRelevance, int expectedK) - throws IOException { + throws IOException { try (XContentParser parser = createParser(JsonXContent.jsonXContent, xContent)) { ExpectedReciprocalRank errAt = ExpectedReciprocalRank.fromXContent(parser); assertEquals(expectedUnknownDocRating, errAt.getUnknownDocRating()); @@ -161,8 +161,10 @@ public void testXContentParsingIsNotLenient() throws IOException { try (XContentParser parser = createParser(xContentType.xContent(), withRandomFields)) { parser.nextToken(); parser.nextToken(); - XContentParseException exception = expectThrows(XContentParseException.class, - () -> DiscountedCumulativeGain.fromXContent(parser)); + XContentParseException exception = expectThrows( + XContentParseException.class, + () -> DiscountedCumulativeGain.fromXContent(parser) + ); assertThat(exception.getMessage(), containsString("[dcg] unknown field")); } } @@ -175,31 +177,42 @@ public void testMetricDetails() { public void testSerialization() throws IOException { ExpectedReciprocalRank original = createTestItem(); - ExpectedReciprocalRank deserialized = ESTestCase.copyWriteable(original, new NamedWriteableRegistry(Collections.emptyList()), - ExpectedReciprocalRank::new); + ExpectedReciprocalRank deserialized = ESTestCase.copyWriteable( + original, + new NamedWriteableRegistry(Collections.emptyList()), + ExpectedReciprocalRank::new + ); assertEquals(deserialized, original); assertEquals(deserialized.hashCode(), original.hashCode()); assertNotSame(deserialized, original); } public void testEqualsAndHash() throws IOException { - checkEqualsAndHashCode(createTestItem(), original -> { - return new ExpectedReciprocalRank(original.getMaxRelevance(), original.getUnknownDocRating(), original.getK()); - }, ExpectedReciprocalRankTests::mutateTestItem); + checkEqualsAndHashCode( + createTestItem(), + original -> { return new ExpectedReciprocalRank(original.getMaxRelevance(), original.getUnknownDocRating(), original.getK()); }, + ExpectedReciprocalRankTests::mutateTestItem + ); } private static ExpectedReciprocalRank mutateTestItem(ExpectedReciprocalRank original) { switch (randomIntBetween(0, 2)) { - case 0: - return new ExpectedReciprocalRank(original.getMaxRelevance() + 1, original.getUnknownDocRating(), original.getK()); - case 1: - return new ExpectedReciprocalRank(original.getMaxRelevance(), - randomValueOtherThan(original.getUnknownDocRating(), () -> randomIntBetween(0, 10)), original.getK()); - case 2: - return new ExpectedReciprocalRank(original.getMaxRelevance(), original.getUnknownDocRating(), - randomValueOtherThan(original.getK(), () -> randomIntBetween(1, 10))); - default: - throw new IllegalArgumentException("mutation variant not allowed"); + case 0: + return new ExpectedReciprocalRank(original.getMaxRelevance() + 1, original.getUnknownDocRating(), original.getK()); + case 1: + return new ExpectedReciprocalRank( + original.getMaxRelevance(), + randomValueOtherThan(original.getUnknownDocRating(), () -> randomIntBetween(0, 10)), + original.getK() + ); + case 2: + return new ExpectedReciprocalRank( + original.getMaxRelevance(), + original.getUnknownDocRating(), + randomValueOtherThan(original.getK(), () -> randomIntBetween(1, 10)) + ); + default: + throw new IllegalArgumentException("mutation variant not allowed"); } } } diff --git a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/MeanReciprocalRankTests.java b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/MeanReciprocalRankTests.java index 090041b80d56c..04adb1f66b830 100644 --- a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/MeanReciprocalRankTests.java +++ b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/MeanReciprocalRankTests.java @@ -10,6 +10,10 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; +import org.elasticsearch.index.shard.ShardId; +import org.elasticsearch.search.SearchHit; +import org.elasticsearch.search.SearchShardTarget; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; @@ -17,10 +21,6 @@ import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.index.shard.ShardId; -import org.elasticsearch.search.SearchHit; -import org.elasticsearch.search.SearchShardTarget; -import org.elasticsearch.test.ESTestCase; import java.io.IOException; import java.util.ArrayList; @@ -119,11 +119,11 @@ public void testEvaluationOneRelevantInResults() { */ public void testPrecisionAtFiveRelevanceThreshold() { List rated = new ArrayList<>(); - rated.add(new RatedDocument("test", "0", 0)); - rated.add(new RatedDocument("test", "1", 1)); - rated.add(new RatedDocument("test", "2", 2)); - rated.add(new RatedDocument("test", "3", 3)); - rated.add(new RatedDocument("test", "4", 4)); + rated.add(new RatedDocument("test", "0", 0)); + rated.add(new RatedDocument("test", "1", 1)); + rated.add(new RatedDocument("test", "2", 2)); + rated.add(new RatedDocument("test", "3", 3)); + rated.add(new RatedDocument("test", "4", 4)); SearchHit[] hits = createSearchHits(0, 5, "test"); MeanReciprocalRank reciprocalRank = new MeanReciprocalRank(2, 10); @@ -178,8 +178,7 @@ public void testXContentParsingIsNotLenient() throws IOException { try (XContentParser parser = createParser(xContentType.xContent(), withRandomFields)) { parser.nextToken(); parser.nextToken(); - XContentParseException exception = expectThrows(XContentParseException.class, - () -> MeanReciprocalRank.fromXContent(parser)); + XContentParseException exception = expectThrows(XContentParseException.class, () -> MeanReciprocalRank.fromXContent(parser)); assertThat(exception.getMessage(), containsString("[reciprocal_rank] unknown field")); } } @@ -203,8 +202,11 @@ static MeanReciprocalRank createTestItem() { public void testSerialization() throws IOException { MeanReciprocalRank original = createTestItem(); - MeanReciprocalRank deserialized = ESTestCase.copyWriteable(original, new NamedWriteableRegistry(Collections.emptyList()), - MeanReciprocalRank::new); + MeanReciprocalRank deserialized = ESTestCase.copyWriteable( + original, + new NamedWriteableRegistry(Collections.emptyList()), + MeanReciprocalRank::new + ); assertEquals(deserialized, original); assertEquals(deserialized.hashCode(), original.hashCode()); assertNotSame(deserialized, original); diff --git a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/PrecisionAtKTests.java b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/PrecisionAtKTests.java index d86b1b89d4400..b82690ea74211 100644 --- a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/PrecisionAtKTests.java +++ b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/PrecisionAtKTests.java @@ -10,6 +10,10 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; +import org.elasticsearch.index.shard.ShardId; +import org.elasticsearch.search.SearchHit; +import org.elasticsearch.search.SearchShardTarget; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; @@ -17,10 +21,6 @@ import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.index.shard.ShardId; -import org.elasticsearch.search.SearchHit; -import org.elasticsearch.search.SearchShardTarget; -import org.elasticsearch.test.ESTestCase; import java.io.IOException; import java.util.ArrayList; @@ -201,8 +201,11 @@ public void testXContentParsingIsNotLenient() throws IOException { public void testSerialization() throws IOException { PrecisionAtK original = createTestItem(); - PrecisionAtK deserialized = ESTestCase.copyWriteable(original, new NamedWriteableRegistry(Collections.emptyList()), - PrecisionAtK::new); + PrecisionAtK deserialized = ESTestCase.copyWriteable( + original, + new NamedWriteableRegistry(Collections.emptyList()), + PrecisionAtK::new + ); assertEquals(deserialized, original); assertEquals(deserialized.hashCode(), original.hashCode()); assertNotSame(deserialized, original); @@ -213,27 +216,39 @@ public void testEqualsAndHash() throws IOException { } private static PrecisionAtK copy(PrecisionAtK original) { - return new PrecisionAtK(original.getRelevantRatingThreshold(), original.getIgnoreUnlabeled(), - original.forcedSearchSize().getAsInt()); + return new PrecisionAtK( + original.getRelevantRatingThreshold(), + original.getIgnoreUnlabeled(), + original.forcedSearchSize().getAsInt() + ); } private static PrecisionAtK mutate(PrecisionAtK original) { PrecisionAtK pAtK; switch (randomIntBetween(0, 2)) { - case 0: - pAtK = new PrecisionAtK(original.getRelevantRatingThreshold(), original.getIgnoreUnlabeled() == false, - original.forcedSearchSize().getAsInt()); - break; - case 1: - pAtK = new PrecisionAtK(randomValueOtherThan(original.getRelevantRatingThreshold(), () -> randomIntBetween(0, 10)), - original.getIgnoreUnlabeled(), original.forcedSearchSize().getAsInt()); - break; - case 2: - pAtK = new PrecisionAtK(original.getRelevantRatingThreshold(), - original.getIgnoreUnlabeled(), original.forcedSearchSize().getAsInt() + 1); - break; - default: - throw new IllegalStateException("The test should only allow three parameters mutated"); + case 0: + pAtK = new PrecisionAtK( + original.getRelevantRatingThreshold(), + original.getIgnoreUnlabeled() == false, + original.forcedSearchSize().getAsInt() + ); + break; + case 1: + pAtK = new PrecisionAtK( + randomValueOtherThan(original.getRelevantRatingThreshold(), () -> randomIntBetween(0, 10)), + original.getIgnoreUnlabeled(), + original.forcedSearchSize().getAsInt() + ); + break; + case 2: + pAtK = new PrecisionAtK( + original.getRelevantRatingThreshold(), + original.getIgnoreUnlabeled(), + original.forcedSearchSize().getAsInt() + 1 + ); + break; + default: + throw new IllegalStateException("The test should only allow three parameters mutated"); } return pAtK; } diff --git a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RankEvalRequestTests.java b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RankEvalRequestTests.java index c2c6ebf685f2f..1429fff975cf8 100644 --- a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RankEvalRequestTests.java +++ b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RankEvalRequestTests.java @@ -13,8 +13,8 @@ import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.Writeable.Reader; import org.elasticsearch.common.util.ArrayUtils; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.test.AbstractWireSerializingTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.junit.AfterClass; import java.io.IOException; @@ -44,13 +44,20 @@ protected NamedWriteableRegistry getNamedWriteableRegistry() { protected RankEvalRequest createTestInstance() { int numberOfIndices = randomInt(3); String[] indices = new String[numberOfIndices]; - for (int i=0; i < numberOfIndices; i++) { + for (int i = 0; i < numberOfIndices; i++) { indices[i] = randomAlphaOfLengthBetween(5, 10); } RankEvalRequest rankEvalRequest = new RankEvalRequest(RankEvalSpecTests.createTestItem(), indices); IndicesOptions indicesOptions = IndicesOptions.fromOptions( - randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), - randomBoolean()); + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean() + ); rankEvalRequest.indicesOptions(indicesOptions); rankEvalRequest.searchType(randomFrom(SearchType.DFS_QUERY_THEN_FETCH, SearchType.QUERY_THEN_FETCH)); return rankEvalRequest; @@ -66,8 +73,14 @@ protected RankEvalRequest mutateInstance(RankEvalRequest instance) throws IOExce RankEvalRequest mutation = copyInstance(instance); List mutators = new ArrayList<>(); mutators.add(() -> mutation.indices(ArrayUtils.concat(instance.indices(), new String[] { randomAlphaOfLength(10) }))); - mutators.add(() -> mutation.indicesOptions(randomValueOtherThan(instance.indicesOptions(), - () -> IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean())))); + mutators.add( + () -> mutation.indicesOptions( + randomValueOtherThan( + instance.indicesOptions(), + () -> IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean()) + ) + ) + ); mutators.add(() -> { if (instance.searchType() == SearchType.DFS_QUERY_THEN_FETCH) { mutation.searchType(SearchType.QUERY_THEN_FETCH); diff --git a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RankEvalResponseTests.java b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RankEvalResponseTests.java index d8b61ad190ef6..474903dea5deb 100644 --- a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RankEvalResponseTests.java +++ b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RankEvalResponseTests.java @@ -19,17 +19,17 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.index.shard.ShardId; +import org.elasticsearch.search.SearchHit; +import org.elasticsearch.search.SearchParseException; +import org.elasticsearch.search.SearchShardTarget; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentLocation; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.index.shard.ShardId; -import org.elasticsearch.search.SearchHit; -import org.elasticsearch.search.SearchParseException; -import org.elasticsearch.search.SearchShardTarget; -import org.elasticsearch.test.ESTestCase; import java.io.IOException; import java.util.ArrayList; @@ -51,23 +51,30 @@ public class RankEvalResponseTests extends ESTestCase { private static final Exception[] RANDOM_EXCEPTIONS = new Exception[] { - new ClusterBlockException(singleton(NoMasterBlockService.NO_MASTER_BLOCK_WRITES)), - new CircuitBreakingException("Data too large", 123, 456, CircuitBreaker.Durability.PERMANENT), - new SearchParseException(SHARD_TARGET, "Parse failure", new XContentLocation(12, 98)), - new IllegalArgumentException("Closed resource", new RuntimeException("Resource")), - new SearchPhaseExecutionException("search", "all shards failed", - new ShardSearchFailure[] { new ShardSearchFailure(new ParsingException(1, 2, "foobar", null), - new SearchShardTarget("node_1", new ShardId("foo", "_na_", 1), null)) }), - new ElasticsearchException("Parsing failed", - new ParsingException(9, 42, "Wrong state", new NullPointerException("Unexpected null value"))) }; + new ClusterBlockException(singleton(NoMasterBlockService.NO_MASTER_BLOCK_WRITES)), + new CircuitBreakingException("Data too large", 123, 456, CircuitBreaker.Durability.PERMANENT), + new SearchParseException(SHARD_TARGET, "Parse failure", new XContentLocation(12, 98)), + new IllegalArgumentException("Closed resource", new RuntimeException("Resource")), + new SearchPhaseExecutionException( + "search", + "all shards failed", + new ShardSearchFailure[] { + new ShardSearchFailure( + new ParsingException(1, 2, "foobar", null), + new SearchShardTarget("node_1", new ShardId("foo", "_na_", 1), null) + ) } + ), + new ElasticsearchException( + "Parsing failed", + new ParsingException(9, 42, "Wrong state", new NullPointerException("Unexpected null value")) + ) }; private static RankEvalResponse createRandomResponse() { int numberOfRequests = randomIntBetween(0, 5); Map partials = new HashMap<>(numberOfRequests); for (int i = 0; i < numberOfRequests; i++) { String id = randomAlphaOfLengthBetween(3, 10); - EvalQueryQuality evalQuality = new EvalQueryQuality(id, - randomDoubleBetween(0.0, 1.0, true)); + EvalQueryQuality evalQuality = new EvalQueryQuality(id, randomDoubleBetween(0.0, 1.0, true)); int numberOfDocs = randomIntBetween(0, 5); List ratedHits = new ArrayList<>(numberOfDocs); for (int d = 0; d < numberOfDocs; d++) { @@ -117,13 +124,16 @@ public void testXContentParsing() throws IOException { } assertNotSame(testItem, parsedItem); // We cannot check equality of object here because some information (e.g. - // SearchHit#shard) cannot fully be parsed back. + // SearchHit#shard) cannot fully be parsed back. assertEquals(testItem.getMetricScore(), parsedItem.getMetricScore(), 0.0); assertEquals(testItem.getPartialResults().keySet(), parsedItem.getPartialResults().keySet()); for (EvalQueryQuality metricDetail : testItem.getPartialResults().values()) { EvalQueryQuality parsedEvalQueryQuality = parsedItem.getPartialResults().get(metricDetail.getId()); - assertToXContentEquivalent(toXContent(metricDetail, xContentType, humanReadable), - toXContent(parsedEvalQueryQuality, xContentType, humanReadable), xContentType); + assertToXContentEquivalent( + toXContent(metricDetail, xContentType, humanReadable), + toXContent(parsedEvalQueryQuality, xContentType, humanReadable), + xContentType + ); } // Also exceptions that are parsed back will be different since they are re-wrapped during parsing. // However, we can check that there is the expected number @@ -137,33 +147,39 @@ public void testXContentParsing() throws IOException { public void testToXContent() throws IOException { EvalQueryQuality coffeeQueryQuality = new EvalQueryQuality("coffee_query", 0.1); coffeeQueryQuality.addHitsAndRatings(Arrays.asList(searchHit("index", 123, 5), searchHit("index", 456, null))); - RankEvalResponse response = new RankEvalResponse(0.123, Collections.singletonMap("coffee_query", coffeeQueryQuality), - Collections.singletonMap("beer_query", new ParsingException(new XContentLocation(0, 0), "someMsg"))); + RankEvalResponse response = new RankEvalResponse( + 0.123, + Collections.singletonMap("coffee_query", coffeeQueryQuality), + Collections.singletonMap("beer_query", new ParsingException(new XContentLocation(0, 0), "someMsg")) + ); XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); String xContent = BytesReference.bytes(response.toXContent(builder, ToXContent.EMPTY_PARAMS)).utf8ToString(); - assertEquals(("{" + - " \"metric_score\": 0.123," + - " \"details\": {" + - " \"coffee_query\": {" + - " \"metric_score\": 0.1," + - " \"unrated_docs\": [{\"_index\":\"index\",\"_id\":\"456\"}]," + - " \"hits\":[{\"hit\":{\"_index\":\"index\",\"_id\":\"123\",\"_score\":1.0}," + - " \"rating\":5}," + - " {\"hit\":{\"_index\":\"index\",\"_id\":\"456\",\"_score\":1.0}," + - " \"rating\":null}" + - " ]" + - " }" + - " }," + - " \"failures\": {" + - " \"beer_query\": {" + - " \"error\" : {\"root_cause\": [{\"type\":\"parsing_exception\", \"reason\":\"someMsg\",\"line\":0,\"col\":0}]," + - " \"type\":\"parsing_exception\"," + - " \"reason\":\"someMsg\"," + - " \"line\":0,\"col\":0" + - " }" + - " }" + - " }" + - "}").replaceAll("\\s+", ""), xContent); + assertEquals( + ("{" + + " \"metric_score\": 0.123," + + " \"details\": {" + + " \"coffee_query\": {" + + " \"metric_score\": 0.1," + + " \"unrated_docs\": [{\"_index\":\"index\",\"_id\":\"456\"}]," + + " \"hits\":[{\"hit\":{\"_index\":\"index\",\"_id\":\"123\",\"_score\":1.0}," + + " \"rating\":5}," + + " {\"hit\":{\"_index\":\"index\",\"_id\":\"456\",\"_score\":1.0}," + + " \"rating\":null}" + + " ]" + + " }" + + " }," + + " \"failures\": {" + + " \"beer_query\": {" + + " \"error\" : {\"root_cause\": [{\"type\":\"parsing_exception\", \"reason\":\"someMsg\",\"line\":0,\"col\":0}]," + + " \"type\":\"parsing_exception\"," + + " \"reason\":\"someMsg\"," + + " \"line\":0,\"col\":0" + + " }" + + " }" + + " }" + + "}").replaceAll("\\s+", ""), + xContent + ); } private static RatedSearchHit searchHit(String index, int docId, Integer rating) { diff --git a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RankEvalSpecTests.java b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RankEvalSpecTests.java index c1cc545618627..6d973c45d5cab 100644 --- a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RankEvalSpecTests.java +++ b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RankEvalSpecTests.java @@ -11,13 +11,6 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.index.query.MatchAllQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.rankeval.RankEvalSpec.ScriptWithId; @@ -25,6 +18,13 @@ import org.elasticsearch.script.ScriptType; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.xcontent.json.JsonXContent; import java.io.IOException; import java.util.ArrayList; @@ -60,11 +60,14 @@ private static List randomList(Supplier randomSupplier) { } static RankEvalSpec createTestItem() { - Supplier metric = randomFrom(Arrays.asList( + Supplier metric = randomFrom( + Arrays.asList( () -> PrecisionAtKTests.createTestItem(), () -> RecallAtKTests.createTestItem(), () -> MeanReciprocalRankTests.createTestItem(), - () -> DiscountedCumulativeGainTests.createTestItem())); + () -> DiscountedCumulativeGainTests.createTestItem() + ) + ); List ratedRequests = null; Collection templates = null; @@ -87,12 +90,19 @@ static RankEvalSpec createTestItem() { Map templateParams = new HashMap<>(); templateParams.put("key", "value"); - RatedRequest ratedRequest = new RatedRequest("id", Arrays.asList(RatedDocumentTests.createRatedDocument()), templateParams, - "templateId"); + RatedRequest ratedRequest = new RatedRequest( + "id", + Arrays.asList(RatedDocumentTests.createRatedDocument()), + templateParams, + "templateId" + ); ratedRequests = Arrays.asList(ratedRequest); } else { - RatedRequest ratedRequest = new RatedRequest("id", Arrays.asList(RatedDocumentTests.createRatedDocument()), - new SearchSourceBuilder()); + RatedRequest ratedRequest = new RatedRequest( + "id", + Arrays.asList(RatedDocumentTests.createRatedDocument()), + new SearchSourceBuilder() + ); ratedRequests = Arrays.asList(ratedRequest); } RankEvalSpec spec = new RankEvalSpec(ratedRequests, metric.get(), templates); @@ -141,7 +151,8 @@ private static RankEvalSpec copy(RankEvalSpec original) throws IOException { namedWriteables.add(new NamedWriteableRegistry.Entry(EvaluationMetric.class, PrecisionAtK.NAME, PrecisionAtK::new)); namedWriteables.add(new NamedWriteableRegistry.Entry(EvaluationMetric.class, RecallAtK.NAME, RecallAtK::new)); namedWriteables.add( - new NamedWriteableRegistry.Entry(EvaluationMetric.class, DiscountedCumulativeGain.NAME, DiscountedCumulativeGain::new)); + new NamedWriteableRegistry.Entry(EvaluationMetric.class, DiscountedCumulativeGain.NAME, DiscountedCumulativeGain::new) + ); namedWriteables.add(new NamedWriteableRegistry.Entry(EvaluationMetric.class, MeanReciprocalRank.NAME, MeanReciprocalRank::new)); return ESTestCase.copyWriteable(original, new NamedWriteableRegistry(namedWriteables), RankEvalSpec::new); } @@ -157,22 +168,22 @@ static RankEvalSpec mutateTestItem(RankEvalSpec original) { int mutate = randomIntBetween(0, 2); switch (mutate) { - case 0: - RatedRequest request = RatedRequestsTests.createTestItem(true); - ratedRequests.add(request); - break; - case 1: - if (metric instanceof PrecisionAtK) { - metric = new DiscountedCumulativeGain(); - } else { - metric = new PrecisionAtK(); - } - break; - case 2: - templates.put("mutation", new Script(ScriptType.INLINE, "mustache", randomAlphaOfLength(10), new HashMap<>())); - break; - default: - throw new IllegalStateException("Requested to modify more than available parameters."); + case 0: + RatedRequest request = RatedRequestsTests.createTestItem(true); + ratedRequests.add(request); + break; + case 1: + if (metric instanceof PrecisionAtK) { + metric = new DiscountedCumulativeGain(); + } else { + metric = new PrecisionAtK(); + } + break; + case 2: + templates.put("mutation", new Script(ScriptType.INLINE, "mustache", randomAlphaOfLength(10), new HashMap<>())); + break; + default: + throw new IllegalStateException("Requested to modify more than available parameters."); } List scripts = new ArrayList<>(); diff --git a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RatedDocumentTests.java b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RatedDocumentTests.java index 47d1c79988377..c3e33d0e77e56 100644 --- a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RatedDocumentTests.java +++ b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RatedDocumentTests.java @@ -10,13 +10,13 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParseException; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; import java.io.IOException; import java.util.Collections; @@ -56,17 +56,22 @@ public void testXContentParsingIsNotLenient() throws IOException { public void testSerialization() throws IOException { RatedDocument original = createRatedDocument(); - RatedDocument deserialized = ESTestCase.copyWriteable(original, new NamedWriteableRegistry(Collections.emptyList()), - RatedDocument::new); + RatedDocument deserialized = ESTestCase.copyWriteable( + original, + new NamedWriteableRegistry(Collections.emptyList()), + RatedDocument::new + ); assertEquals(deserialized, original); assertEquals(deserialized.hashCode(), original.hashCode()); assertNotSame(deserialized, original); } public void testEqualsAndHash() throws IOException { - checkEqualsAndHashCode(createRatedDocument(), original -> { - return new RatedDocument(original.getIndex(), original.getDocID(), original.getRating()); - }, RatedDocumentTests::mutateTestItem); + checkEqualsAndHashCode( + createRatedDocument(), + original -> { return new RatedDocument(original.getIndex(), original.getDocID(), original.getRating()); }, + RatedDocumentTests::mutateTestItem + ); } private static RatedDocument mutateTestItem(RatedDocument original) { @@ -75,17 +80,17 @@ private static RatedDocument mutateTestItem(RatedDocument original) { String docId = original.getDocID(); switch (randomIntBetween(0, 2)) { - case 0: - rating = randomValueOtherThan(rating, () -> randomInt()); - break; - case 1: - index = randomValueOtherThan(index, () -> randomAlphaOfLength(10)); - break; - case 2: - docId = randomValueOtherThan(docId, () -> randomAlphaOfLength(10)); - break; - default: - throw new IllegalStateException("The test should only allow two parameters mutated"); + case 0: + rating = randomValueOtherThan(rating, () -> randomInt()); + break; + case 1: + index = randomValueOtherThan(index, () -> randomAlphaOfLength(10)); + break; + case 2: + docId = randomValueOtherThan(docId, () -> randomAlphaOfLength(10)); + break; + default: + throw new IllegalStateException("The test should only allow two parameters mutated"); } return new RatedDocument(index, docId, rating); } diff --git a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RatedRequestsTests.java b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RatedRequestsTests.java index 95c762a8b0724..63408f9503a5c 100644 --- a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RatedRequestsTests.java +++ b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RatedRequestsTests.java @@ -11,14 +11,6 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentParseException; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.index.query.MatchAllQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.search.SearchModule; @@ -28,6 +20,14 @@ import org.elasticsearch.search.suggest.SuggestBuilder; import org.elasticsearch.search.suggest.SuggestBuilders; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentParseException; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.xcontent.json.JsonXContent; import org.junit.AfterClass; import org.junit.BeforeClass; @@ -53,8 +53,10 @@ public class RatedRequestsTests extends ESTestCase { @BeforeClass public static void init() { xContentRegistry = new NamedXContentRegistry( - Stream.of(new SearchModule(Settings.EMPTY, emptyList()).getNamedXContents().stream()) - .flatMap(Function.identity()).collect(toList())); + Stream.of(new SearchModule(Settings.EMPTY, emptyList()).getNamedXContents().stream()) + .flatMap(Function.identity()) + .collect(toList()) + ); } @AfterClass @@ -163,34 +165,34 @@ private static RatedRequest mutateTestItem(RatedRequest original) { int mutate = randomIntBetween(0, 3); switch (mutate) { - case 0: - id = randomValueOtherThan(id, () -> randomAlphaOfLength(10)); - break; - case 1: - if (evaluationRequest != null) { - int size = randomValueOtherThan(evaluationRequest.size(), () -> randomInt(Integer.MAX_VALUE)); - evaluationRequest = new SearchSourceBuilder(); - evaluationRequest.size(size); - evaluationRequest.query(new MatchAllQueryBuilder()); - } else { - if (randomBoolean()) { - Map mutated = new HashMap<>(); - mutated.putAll(params); - mutated.put("one_more_key", "one_more_value"); - params = mutated; + case 0: + id = randomValueOtherThan(id, () -> randomAlphaOfLength(10)); + break; + case 1: + if (evaluationRequest != null) { + int size = randomValueOtherThan(evaluationRequest.size(), () -> randomInt(Integer.MAX_VALUE)); + evaluationRequest = new SearchSourceBuilder(); + evaluationRequest.size(size); + evaluationRequest.query(new MatchAllQueryBuilder()); } else { - templateId = randomValueOtherThan(templateId, () -> randomAlphaOfLength(5)); + if (randomBoolean()) { + Map mutated = new HashMap<>(); + mutated.putAll(params); + mutated.put("one_more_key", "one_more_value"); + params = mutated; + } else { + templateId = randomValueOtherThan(templateId, () -> randomAlphaOfLength(5)); + } } - } - break; - case 2: - ratedDocs = Arrays.asList(randomValueOtherThanMany(ratedDocs::contains, () -> RatedDocumentTests.createRatedDocument())); - break; - case 3: - summaryFields = Arrays.asList(randomValueOtherThanMany(summaryFields::contains, () -> randomAlphaOfLength(10))); - break; - default: - throw new IllegalStateException("Requested to modify more than available parameters."); + break; + case 2: + ratedDocs = Arrays.asList(randomValueOtherThanMany(ratedDocs::contains, () -> RatedDocumentTests.createRatedDocument())); + break; + case 3: + summaryFields = Arrays.asList(randomValueOtherThanMany(summaryFields::contains, () -> randomAlphaOfLength(10))); + break; + default: + throw new IllegalStateException("Requested to modify more than available parameters."); } RatedRequest ratedRequest; @@ -206,15 +208,21 @@ private static RatedRequest mutateTestItem(RatedRequest original) { public void testDuplicateRatedDocThrowsException() { List ratedDocs = Arrays.asList(new RatedDocument("index1", "id1", 1), new RatedDocument("index1", "id1", 5)); - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, - () -> new RatedRequest("test_query", ratedDocs, new SearchSourceBuilder())); - assertEquals("Found duplicate rated document key [{\"_index\":\"index1\",\"_id\":\"id1\"}] in evaluation request [test_query]", - ex.getMessage()); + IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> new RatedRequest("test_query", ratedDocs, new SearchSourceBuilder()) + ); + assertEquals( + "Found duplicate rated document key [{\"_index\":\"index1\",\"_id\":\"id1\"}] in evaluation request [test_query]", + ex.getMessage() + ); Map params = new HashMap<>(); params.put("key", "value"); ex = expectThrows(IllegalArgumentException.class, () -> new RatedRequest("test_query", ratedDocs, params, "templateId")); - assertEquals("Found duplicate rated document key [{\"_index\":\"index1\",\"_id\":\"id1\"}] in evaluation request [test_query]", - ex.getMessage()); + assertEquals( + "Found duplicate rated document key [{\"_index\":\"index1\",\"_id\":\"id1\"}] in evaluation request [test_query]", + ex.getMessage() + ); } public void testNullSummaryFieldsTreatment() { @@ -274,15 +282,19 @@ public void testHighlighterNotAllowed() { public void testExplainNotAllowed() { List ratedDocs = Arrays.asList(new RatedDocument("index1", "id1", 1)); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> new RatedRequest("id", ratedDocs, new SearchSourceBuilder().explain(true))); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> new RatedRequest("id", ratedDocs, new SearchSourceBuilder().explain(true)) + ); assertEquals("Query in rated requests should not use explain.", e.getMessage()); } public void testProfileNotAllowed() { List ratedDocs = Arrays.asList(new RatedDocument("index1", "id1", 1)); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> new RatedRequest("id", ratedDocs, new SearchSourceBuilder().profile(true))); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> new RatedRequest("id", ratedDocs, new SearchSourceBuilder().profile(true)) + ); assertEquals("Query in rated requests should not use profile.", e.getMessage()); } @@ -292,26 +304,26 @@ public void testProfileNotAllowed() { */ public void testParseFromXContent() throws IOException { String querySpecString = " {\n" - + " \"id\": \"my_qa_query\",\n" - + " \"request\": {\n" - + " \"query\": {\n" - + " \"bool\": {\n" - + " \"must\": [\n" - + " {\"match\": {\"beverage\": \"coffee\"}},\n" - + " {\"term\": {\"browser\": {\"value\": \"safari\"}}},\n" - + " {\"term\": {\"time_of_day\": " - + " {\"value\": \"morning\",\"boost\": 2}}},\n" - + " {\"term\": {\"ip_location\": " - + " {\"value\": \"ams\",\"boost\": 10}}}]}\n" - + " },\n" - + " \"size\": 10\n" - + " },\n" - + " \"summary_fields\" : [\"title\"],\n" - + " \"ratings\": [\n" - + " {\"_index\": \"test\" , \"_id\": \"1\", \"rating\" : 1 },\n" - + " {\"_index\": \"test\", \"rating\" : 0, \"_id\": \"2\"},\n" - + " {\"_id\": \"3\", \"_index\": \"test\", \"rating\" : 1} ]" - + "}\n"; + + " \"id\": \"my_qa_query\",\n" + + " \"request\": {\n" + + " \"query\": {\n" + + " \"bool\": {\n" + + " \"must\": [\n" + + " {\"match\": {\"beverage\": \"coffee\"}},\n" + + " {\"term\": {\"browser\": {\"value\": \"safari\"}}},\n" + + " {\"term\": {\"time_of_day\": " + + " {\"value\": \"morning\",\"boost\": 2}}},\n" + + " {\"term\": {\"ip_location\": " + + " {\"value\": \"ams\",\"boost\": 10}}}]}\n" + + " },\n" + + " \"size\": 10\n" + + " },\n" + + " \"summary_fields\" : [\"title\"],\n" + + " \"ratings\": [\n" + + " {\"_index\": \"test\" , \"_id\": \"1\", \"rating\" : 1 },\n" + + " {\"_index\": \"test\", \"rating\" : 0, \"_id\": \"2\"},\n" + + " {\"_id\": \"3\", \"_index\": \"test\", \"rating\" : 1} ]" + + "}\n"; try (XContentParser parser = createParser(JsonXContent.jsonXContent, querySpecString)) { RatedRequest specification = RatedRequest.fromXContent(parser); assertEquals("my_qa_query", specification.getId()); diff --git a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RatedSearchHitTests.java b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RatedSearchHitTests.java index 21efb0ce79b45..d303dea49b1f6 100644 --- a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RatedSearchHitTests.java +++ b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RatedSearchHitTests.java @@ -10,11 +10,11 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; +import org.elasticsearch.search.SearchHit; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.search.SearchHit; -import org.elasticsearch.test.ESTestCase; import java.io.IOException; import java.util.Collections; @@ -25,10 +25,13 @@ public class RatedSearchHitTests extends ESTestCase { public static RatedSearchHit randomRatedSearchHit() { - OptionalInt rating = randomBoolean() ? OptionalInt.empty() - : OptionalInt.of(randomIntBetween(0, 5)); - SearchHit searchHit = new SearchHit(randomIntBetween(0, 10), randomAlphaOfLength(10), Collections.emptyMap(), - Collections.emptyMap()); + OptionalInt rating = randomBoolean() ? OptionalInt.empty() : OptionalInt.of(randomIntBetween(0, 5)); + SearchHit searchHit = new SearchHit( + randomIntBetween(0, 10), + randomAlphaOfLength(10), + Collections.emptyMap(), + Collections.emptyMap() + ); RatedSearchHit ratedSearchHit = new RatedSearchHit(searchHit, rating); return ratedSearchHit; } @@ -37,15 +40,14 @@ private static RatedSearchHit mutateTestItem(RatedSearchHit original) { OptionalInt rating = original.getRating(); SearchHit hit = original.getSearchHit(); switch (randomIntBetween(0, 1)) { - case 0: - rating = rating.isPresent() ? OptionalInt.of(rating.getAsInt() + 1) : OptionalInt.of(randomInt(5)); - break; - case 1: - hit = new SearchHit(hit.docId(), hit.getId() + randomAlphaOfLength(10), Collections.emptyMap(), - Collections.emptyMap()); - break; - default: - throw new IllegalStateException("The test should only allow two parameters mutated"); + case 0: + rating = rating.isPresent() ? OptionalInt.of(rating.getAsInt() + 1) : OptionalInt.of(randomInt(5)); + break; + case 1: + hit = new SearchHit(hit.docId(), hit.getId() + randomAlphaOfLength(10), Collections.emptyMap(), Collections.emptyMap()); + break; + default: + throw new IllegalStateException("The test should only allow two parameters mutated"); } return new RatedSearchHit(hit, rating); } diff --git a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RecallAtKTests.java b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RecallAtKTests.java index a129002b7d450..ced3bb12ea1d6 100644 --- a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RecallAtKTests.java +++ b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RecallAtKTests.java @@ -10,6 +10,10 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; +import org.elasticsearch.index.shard.ShardId; +import org.elasticsearch.search.SearchHit; +import org.elasticsearch.search.SearchShardTarget; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; @@ -17,10 +21,6 @@ import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.index.shard.ShardId; -import org.elasticsearch.search.SearchHit; -import org.elasticsearch.search.SearchShardTarget; -import org.elasticsearch.test.ESTestCase; import java.io.IOException; import java.util.ArrayList; @@ -75,7 +75,7 @@ public void testRelevanceThreshold() { RecallAtK recallAtN = new RecallAtK(2, 5); - EvalQueryQuality evaluated = recallAtN.evaluate("id", toSearchHits(rated.subList(0,3), "test"), rated); + EvalQueryQuality evaluated = recallAtN.evaluate("id", toSearchHits(rated.subList(0, 3), "test"), rated); assertEquals((double) 1 / 3, evaluated.metricScore(), 0.00001); assertEquals(1, ((RecallAtK.Detail) evaluated.getMetricDetails()).getRelevantRetrieved()); assertEquals(3, ((RecallAtK.Detail) evaluated.getMetricDetails()).getRelevant()); @@ -187,8 +187,7 @@ public void testXContentParsingIsNotLenient() throws IOException { public void testSerialization() throws IOException { RecallAtK original = createTestItem(); - RecallAtK deserialized = ESTestCase.copyWriteable(original, new NamedWriteableRegistry(Collections.emptyList()), - RecallAtK::new); + RecallAtK deserialized = ESTestCase.copyWriteable(original, new NamedWriteableRegistry(Collections.emptyList()), RecallAtK::new); assertEquals(deserialized, original); assertEquals(deserialized.hashCode(), original.hashCode()); assertNotSame(deserialized, original); @@ -208,12 +207,11 @@ private static RecallAtK mutate(RecallAtK original) { case 0: recallAtK = new RecallAtK( randomValueOtherThan(original.getRelevantRatingThreshold(), () -> randomIntBetween(0, 10)), - original.forcedSearchSize().getAsInt()); + original.forcedSearchSize().getAsInt() + ); break; case 1: - recallAtK = new RecallAtK( - original.getRelevantRatingThreshold(), - original.forcedSearchSize().getAsInt() + 1); + recallAtK = new RecallAtK(original.getRelevantRatingThreshold(), original.forcedSearchSize().getAsInt() + 1); break; default: throw new IllegalStateException("The test should only allow two parameters mutated"); diff --git a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/TransportRankEvalActionTests.java b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/TransportRankEvalActionTests.java index 1f99392a054cc..19cf92d8f0de2 100644 --- a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/TransportRankEvalActionTests.java +++ b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/TransportRankEvalActionTests.java @@ -16,12 +16,12 @@ import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.env.Environment; import org.elasticsearch.script.ScriptService; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.NamedXContentRegistry; import java.util.ArrayList; import java.util.Arrays; @@ -31,8 +31,11 @@ public class TransportRankEvalActionTests extends ESTestCase { - private Settings settings = Settings.builder().put("path.home", createTempDir().toString()).put("node.name", "test-" + getTestName()) - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build(); + private Settings settings = Settings.builder() + .put("path.home", createTempDir().toString()) + .put("node.name", "test-" + getTestName()) + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) + .build(); /** * Test that request parameters like indicesOptions or searchType from ranking evaluation request are transfered to msearch request @@ -40,14 +43,25 @@ public class TransportRankEvalActionTests extends ESTestCase { public void testTransferRequestParameters() throws Exception { String indexName = "test_index"; List specifications = new ArrayList<>(); - specifications - .add(new RatedRequest("amsterdam_query", Arrays.asList(new RatedDocument(indexName, "1", 3)), new SearchSourceBuilder())); - RankEvalRequest rankEvalRequest = new RankEvalRequest(new RankEvalSpec(specifications, new DiscountedCumulativeGain()), - new String[] { indexName }); + specifications.add( + new RatedRequest("amsterdam_query", Arrays.asList(new RatedDocument(indexName, "1", 3)), new SearchSourceBuilder()) + ); + RankEvalRequest rankEvalRequest = new RankEvalRequest( + new RankEvalSpec(specifications, new DiscountedCumulativeGain()), + new String[] { indexName } + ); SearchType expectedSearchType = randomFrom(SearchType.CURRENTLY_SUPPORTED); rankEvalRequest.searchType(expectedSearchType); - IndicesOptions expectedIndicesOptions = IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), - randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean()); + IndicesOptions expectedIndicesOptions = IndicesOptions.fromOptions( + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean() + ); rankEvalRequest.indicesOptions(expectedIndicesOptions); NodeClient client = new NodeClient(settings, null) { @@ -55,13 +69,18 @@ public void testTransferRequestParameters() throws Exception { public void multiSearch(MultiSearchRequest request, ActionListener listener) { assertEquals(1, request.requests().size()); assertEquals(expectedSearchType, request.requests().get(0).searchType()); - assertArrayEquals(new String[]{indexName}, request.requests().get(0).indices()); + assertArrayEquals(new String[] { indexName }, request.requests().get(0).indices()); assertEquals(expectedIndicesOptions, request.requests().get(0).indicesOptions()); } }; - TransportRankEvalAction action = new TransportRankEvalAction(mock(ActionFilters.class), client, mock(TransportService.class), - mock(ScriptService.class), NamedXContentRegistry.EMPTY); + TransportRankEvalAction action = new TransportRankEvalAction( + mock(ActionFilters.class), + client, + mock(TransportService.class), + mock(ScriptService.class), + NamedXContentRegistry.EMPTY + ); action.doExecute(null, rankEvalRequest, null); } } diff --git a/modules/reindex/src/internalClusterTest/java/org/elasticsearch/client/documentation/ReindexDocumentationIT.java b/modules/reindex/src/internalClusterTest/java/org/elasticsearch/client/documentation/ReindexDocumentationIT.java index 59ec83193e3fa..b5a200afc78a3 100644 --- a/modules/reindex/src/internalClusterTest/java/org/elasticsearch/client/documentation/ReindexDocumentationIT.java +++ b/modules/reindex/src/internalClusterTest/java/org/elasticsearch/client/documentation/ReindexDocumentationIT.java @@ -18,19 +18,19 @@ import org.elasticsearch.index.reindex.AbstractBulkByScrollRequestBuilder; import org.elasticsearch.index.reindex.BulkByScrollResponse; import org.elasticsearch.index.reindex.BulkByScrollTask; -import org.elasticsearch.reindex.CancelTests; import org.elasticsearch.index.reindex.DeleteByQueryAction; import org.elasticsearch.index.reindex.DeleteByQueryRequestBuilder; import org.elasticsearch.index.reindex.ReindexAction; -import org.elasticsearch.reindex.ReindexPlugin; import org.elasticsearch.index.reindex.ReindexRequestBuilder; -import org.elasticsearch.reindex.RethrottleAction; -import org.elasticsearch.reindex.RethrottleRequestBuilder; import org.elasticsearch.index.reindex.UpdateByQueryAction; import org.elasticsearch.index.reindex.UpdateByQueryRequestBuilder; import org.elasticsearch.index.shard.IndexingOperationListener; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.plugins.Plugin; +import org.elasticsearch.reindex.CancelTests; +import org.elasticsearch.reindex.ReindexPlugin; +import org.elasticsearch.reindex.RethrottleAction; +import org.elasticsearch.reindex.RethrottleRequestBuilder; import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptType; import org.elasticsearch.search.sort.SortOrder; @@ -273,9 +273,14 @@ private ReindexRequestBuilder reindexAndPartiallyBlock() throws Exception { final int numDocs = randomIntBetween(10, 100); ALLOWED_OPERATIONS.release(numDocs); - indexRandom(true, false, true, IntStream.range(0, numDocs) - .mapToObj(i -> client().prepareIndex(INDEX_NAME).setId(Integer.toString(i)).setSource("n", Integer.toString(i))) - .collect(Collectors.toList())); + indexRandom( + true, + false, + true, + IntStream.range(0, numDocs) + .mapToObj(i -> client().prepareIndex(INDEX_NAME).setId(Integer.toString(i)).setSource("n", Integer.toString(i))) + .collect(Collectors.toList()) + ); // Checks that the all documents have been indexed and correctly counted assertHitCount(client().prepareSearch(INDEX_NAME).setSize(0).get(), numDocs); @@ -294,12 +299,10 @@ private ReindexRequestBuilder reindexAndPartiallyBlock() throws Exception { builder.execute(); // 10 seconds is usually fine but on heavily loaded machines this can take a while - assertBusy( - () -> { - assertTrue("Expected some queued threads", ALLOWED_OPERATIONS.hasQueuedThreads()); - assertEquals("Expected that no permits are available", 0, ALLOWED_OPERATIONS.availablePermits()); - }, - 1, TimeUnit.MINUTES); + assertBusy(() -> { + assertTrue("Expected some queued threads", ALLOWED_OPERATIONS.hasQueuedThreads()); + assertEquals("Expected that no permits are available", 0, ALLOWED_OPERATIONS.availablePermits()); + }, 1, TimeUnit.MINUTES); return builder; } diff --git a/modules/reindex/src/internalClusterTest/java/org/elasticsearch/index/reindex/BulkByScrollUsesAllScrollDocumentsAfterConflictsIntegTests.java b/modules/reindex/src/internalClusterTest/java/org/elasticsearch/index/reindex/BulkByScrollUsesAllScrollDocumentsAfterConflictsIntegTests.java index a06f9a0ac2b45..d09bdcb00f10d 100644 --- a/modules/reindex/src/internalClusterTest/java/org/elasticsearch/index/reindex/BulkByScrollUsesAllScrollDocumentsAfterConflictsIntegTests.java +++ b/modules/reindex/src/internalClusterTest/java/org/elasticsearch/index/reindex/BulkByScrollUsesAllScrollDocumentsAfterConflictsIntegTests.java @@ -22,9 +22,7 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.CollectionUtils; import org.elasticsearch.common.util.concurrent.EsThreadPoolExecutor; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.VersionType; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.plugins.Plugin; @@ -36,6 +34,8 @@ import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.test.ESIntegTestCase; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; import org.junit.Before; import java.util.ArrayList; @@ -52,9 +52,9 @@ import java.util.function.Function; import static org.elasticsearch.common.lucene.uid.Versions.MATCH_DELETED; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.index.mapper.MapperService.SINGLE_MAPPING_NAME; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.lessThanOrEqualTo; @@ -102,14 +102,14 @@ public void setUpCluster() { public void testUpdateByQuery() throws Exception { final String indexName = randomAlphaOfLength(10).toLowerCase(Locale.ROOT); final boolean scriptEnabled = randomBoolean(); - executeConcurrentUpdatesOnSubsetOfDocs(indexName, + executeConcurrentUpdatesOnSubsetOfDocs( + indexName, indexName, scriptEnabled, updateByQuery(), true, - (bulkByScrollResponse, updatedDocCount) -> { - assertThat(bulkByScrollResponse.getUpdated(), is((long) updatedDocCount)); - }); + (bulkByScrollResponse, updatedDocCount) -> { assertThat(bulkByScrollResponse.getUpdated(), is((long) updatedDocCount)); } + ); } public void testReindex() throws Exception { @@ -124,35 +124,40 @@ public void testReindex() throws Exception { reindexRequestBuilder.destination().setVersion(MATCH_DELETED); final boolean scriptEnabled = randomBoolean(); - executeConcurrentUpdatesOnSubsetOfDocs(sourceIndex, + executeConcurrentUpdatesOnSubsetOfDocs( + sourceIndex, targetIndex, scriptEnabled, reindexRequestBuilder, false, - (bulkByScrollResponse, reindexDocCount) -> { - assertThat(bulkByScrollResponse.getCreated(), is((long) reindexDocCount)); - }); + (bulkByScrollResponse, reindexDocCount) -> { assertThat(bulkByScrollResponse.getCreated(), is((long) reindexDocCount)); } + ); } public void testDeleteByQuery() throws Exception { final String indexName = randomAlphaOfLength(10).toLowerCase(Locale.ROOT); - executeConcurrentUpdatesOnSubsetOfDocs(indexName, + executeConcurrentUpdatesOnSubsetOfDocs( + indexName, indexName, false, deleteByQuery(), true, - (bulkByScrollResponse, deletedDocCount) -> { - assertThat(bulkByScrollResponse.getDeleted(), is((long) deletedDocCount)); - }); + (bulkByScrollResponse, deletedDocCount) -> { assertThat(bulkByScrollResponse.getDeleted(), is((long) deletedDocCount)); } + ); } - , - Self extends AbstractBulkByScrollRequestBuilder> void executeConcurrentUpdatesOnSubsetOfDocs(String sourceIndex, - String targetIndex, - boolean scriptEnabled, - AbstractBulkByScrollRequestBuilder requestBuilder, - boolean useOptimisticConcurrency, - BiConsumer resultConsumer) throws Exception { + < + R extends AbstractBulkByScrollRequest, + Self extends AbstractBulkByScrollRequestBuilder> + void + executeConcurrentUpdatesOnSubsetOfDocs( + String sourceIndex, + String targetIndex, + boolean scriptEnabled, + AbstractBulkByScrollRequestBuilder requestBuilder, + boolean useOptimisticConcurrency, + BiConsumer resultConsumer + ) throws Exception { createIndexWithSingleShard(sourceIndex); final int numDocs = 100; @@ -225,9 +230,7 @@ Self extends AbstractBulkByScrollRequestBuilder> void executeConcurrent // Ensure that the concurrent writes are enqueued before the update by query request is sent assertBusy(() -> assertThat(writeThreadPool.getQueue().size(), equalTo(1))); - requestBuilder.source(sourceIndex) - .maxDocs(maxDocs) - .abortOnVersionConflict(false); + requestBuilder.source(sourceIndex).maxDocs(maxDocs).abortOnVersionConflict(false); if (scriptEnabled) { final Script script = new Script(ScriptType.INLINE, SCRIPT_LANG, NOOP_GENERATOR, Collections.emptyMap()); @@ -291,11 +294,7 @@ private void createIndexWithSingleShard(String index) throws Exception { // Use explicit mappings so we don't have to create those on demands and the task ordering // can change to wait for mapping updates - assertAcked( - prepareCreate(index) - .setSettings(indexSettings) - .setMapping(mappings) - ); + assertAcked(prepareCreate(index).setSettings(indexSettings).setMapping(mappings)); } private IndexRequest createUpdatedIndexRequest(SearchHit searchHit, String targetIndex, boolean useOptimisticUpdate) { diff --git a/modules/reindex/src/internalClusterTest/java/org/elasticsearch/migration/FeatureMigrationIT.java b/modules/reindex/src/internalClusterTest/java/org/elasticsearch/migration/FeatureMigrationIT.java index 09765b472c770..007359da2fcf0 100644 --- a/modules/reindex/src/internalClusterTest/java/org/elasticsearch/migration/FeatureMigrationIT.java +++ b/modules/reindex/src/internalClusterTest/java/org/elasticsearch/migration/FeatureMigrationIT.java @@ -114,12 +114,9 @@ public void testMigrateInternalManagedSystemIndex() throws Exception { TestPlugin.postMigrationHook.set((clusterState, metadata) -> { assertThat(preUpgradeHookCalled.get(), is(true)); - assertThat( - metadata, - hasEntry("stringKey", "stringValue") - ); + assertThat(metadata, hasEntry("stringKey", "stringValue")); assertThat(metadata, hasEntry("intKey", 42)); - assertThat(metadata, hasEntry("listKey", Arrays.asList(1,2,3,4))); + assertThat(metadata, hasEntry("listKey", Arrays.asList(1, 2, 3, 4))); assertThat(metadata, hasKey("mapKey")); @SuppressWarnings("unchecked") Map innerMap = (Map) metadata.get("mapKey"); @@ -201,7 +198,8 @@ public void assertIndexHasCorrectProperties( int settingsFlagValue, boolean isManaged, boolean isInternal, - Collection aliasNames) { + Collection aliasNames + ) { IndexMetadata imd = metadata.index(indexName); assertThat(imd.getSettings().get(FlAG_SETTING_KEY), equalTo(Integer.toString(settingsFlagValue))); final Map mapping = imd.mapping().getSourceAsMap(); diff --git a/modules/reindex/src/internalClusterTest/java/org/elasticsearch/migration/MultiFeatureMigrationIT.java b/modules/reindex/src/internalClusterTest/java/org/elasticsearch/migration/MultiFeatureMigrationIT.java index 832990a973e53..077111ef3a2d2 100644 --- a/modules/reindex/src/internalClusterTest/java/org/elasticsearch/migration/MultiFeatureMigrationIT.java +++ b/modules/reindex/src/internalClusterTest/java/org/elasticsearch/migration/MultiFeatureMigrationIT.java @@ -118,10 +118,7 @@ public void testMultipleFeatureMigration() throws Exception { assertThat(secondPluginPreMigrationHookCalled.get(), nullValue()); assertThat(secondPluginPostMigrationHookCalled.get(), nullValue()); - assertThat( - metadata, - hasEntry("stringKey", "first plugin value") - ); + assertThat(metadata, hasEntry("stringKey", "first plugin value")); // We shouldn't have any results in the cluster state given no features have finished yet. FeatureMigrationResults currentResults = clusterState.metadata().custom(FeatureMigrationResults.TYPE); @@ -157,10 +154,7 @@ public void testMultipleFeatureMigration() throws Exception { assertThat(postMigrationHookCalled.get(), is(true)); assertThat(secondPluginPreMigrationHookCalled.get(), is(true)); - assertThat( - metadata, - hasEntry("stringKey", "second plugin value") - ); + assertThat(metadata, hasEntry("stringKey", "second plugin value")); // And here, the results should be the same, as we haven't updated the state with this feature's status yet. FeatureMigrationResults currentResults = clusterState.metadata().custom(FeatureMigrationResults.TYPE); @@ -276,25 +270,33 @@ public SecondPlugin() { } - @Override public String getFeatureName() { + @Override + public String getFeatureName() { return SECOND_FEATURE_NAME; } - @Override public String getFeatureDescription() { + @Override + public String getFeatureDescription() { return "a plugin for test system index migration with multiple features"; } - @Override public Collection getSystemIndexDescriptors(Settings settings) { + @Override + public Collection getSystemIndexDescriptors(Settings settings) { return Collections.singletonList(SECOND_FEATURE_IDX_DESCIPTOR); } - @Override public void prepareForIndicesMigration( - ClusterService clusterService, Client client, ActionListener> listener) { + @Override + public void prepareForIndicesMigration(ClusterService clusterService, Client client, ActionListener> listener) { listener.onResponse(preMigrationHook.get().apply(clusterService.state())); } - @Override public void indicesMigrationComplete( - Map preUpgradeMetadata, ClusterService clusterService, Client client, ActionListener listener) { + @Override + public void indicesMigrationComplete( + Map preUpgradeMetadata, + ClusterService clusterService, + Client client, + ActionListener listener + ) { postMigrationHook.get().accept(clusterService.state(), preUpgradeMetadata); listener.onResponse(true); } diff --git a/modules/reindex/src/javaRestTest/java/org/elasticsearch/index/reindex/ManyDocumentsIT.java b/modules/reindex/src/javaRestTest/java/org/elasticsearch/index/reindex/ManyDocumentsIT.java index 30e3f8400d937..f22b9236bce57 100644 --- a/modules/reindex/src/javaRestTest/java/org/elasticsearch/index/reindex/ManyDocumentsIT.java +++ b/modules/reindex/src/javaRestTest/java/org/elasticsearch/index/reindex/ManyDocumentsIT.java @@ -42,14 +42,15 @@ public void setupTestIndex() throws IOException { public void testReindex() throws IOException { Request request = new Request("POST", "/_reindex"); request.setJsonEntity( - "{\n" + - " \"source\":{\n" + - " \"index\":\"test\"\n" + - " },\n" + - " \"dest\":{\n" + - " \"index\":\"des\"\n" + - " }\n" + - "}"); + "{\n" + + " \"source\":{\n" + + " \"index\":\"test\"\n" + + " },\n" + + " \"dest\":{\n" + + " \"index\":\"des\"\n" + + " }\n" + + "}" + ); Map response = entityAsMap(client().performRequest(request)); assertThat(response, hasEntry("total", count)); assertThat(response, hasEntry("created", count)); @@ -60,43 +61,48 @@ public void testReindexFromRemote() throws IOException { nodesInfo = (Map) nodesInfo.get("nodes"); Map nodeInfo = (Map) nodesInfo.values().iterator().next(); Map http = (Map) nodeInfo.get("http"); - String remote = "http://"+ http.get("publish_address"); + String remote = "http://" + http.get("publish_address"); Request request = new Request("POST", "/_reindex"); if (randomBoolean()) { request.setJsonEntity( - "{\n" + - " \"source\":{\n" + - " \"index\":\"test\",\n" + - " \"remote\":{\n" + - " \"host\":\"" + remote + "\"\n" + - " }\n" + - " }\n," + - " \"dest\":{\n" + - " \"index\":\"des\"\n" + - " }\n" + - "}"); + "{\n" + + " \"source\":{\n" + + " \"index\":\"test\",\n" + + " \"remote\":{\n" + + " \"host\":\"" + + remote + + "\"\n" + + " }\n" + + " }\n," + + " \"dest\":{\n" + + " \"index\":\"des\"\n" + + " }\n" + + "}" + ); } else { // Test with external version_type request.setJsonEntity( - "{\n" + - " \"source\":{\n" + - " \"index\":\"test\",\n" + - " \"remote\":{\n" + - " \"host\":\"" + remote + "\"\n" + - " }\n" + - " }\n," + - " \"dest\":{\n" + - " \"index\":\"des\",\n" + - " \"version_type\": \"external\"\n" + - " }\n" + - "}"); + "{\n" + + " \"source\":{\n" + + " \"index\":\"test\",\n" + + " \"remote\":{\n" + + " \"host\":\"" + + remote + + "\"\n" + + " }\n" + + " }\n," + + " \"dest\":{\n" + + " \"index\":\"des\",\n" + + " \"version_type\": \"external\"\n" + + " }\n" + + "}" + ); } Map response = entityAsMap(client().performRequest(request)); assertThat(response, hasEntry("total", count)); assertThat(response, hasEntry("created", count)); } - public void testUpdateByQuery() throws IOException { Map response = entityAsMap(client().performRequest(new Request("POST", "/test/_update_by_query"))); assertThat(response, hasEntry("total", count)); @@ -105,12 +111,7 @@ public void testUpdateByQuery() throws IOException { public void testDeleteByQuery() throws IOException { Request request = new Request("POST", "/test/_delete_by_query"); - request.setJsonEntity( - "{\n" + - " \"query\":{\n" + - " \"match_all\": {}\n" + - " }\n" + - "}"); + request.setJsonEntity("{\n" + " \"query\":{\n" + " \"match_all\": {}\n" + " }\n" + "}"); Map response = entityAsMap(client().performRequest(request)); assertThat(response, hasEntry("total", count)); assertThat(response, hasEntry("deleted", count)); diff --git a/modules/reindex/src/javaRestTest/java/org/elasticsearch/index/reindex/ReindexWithoutContentIT.java b/modules/reindex/src/javaRestTest/java/org/elasticsearch/index/reindex/ReindexWithoutContentIT.java index c1f81eb9693ba..773a577e87407 100644 --- a/modules/reindex/src/javaRestTest/java/org/elasticsearch/index/reindex/ReindexWithoutContentIT.java +++ b/modules/reindex/src/javaRestTest/java/org/elasticsearch/index/reindex/ReindexWithoutContentIT.java @@ -19,8 +19,10 @@ public class ReindexWithoutContentIT extends ESRestTestCase { public void testReindexMissingBody() throws IOException { - ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest( - new Request("POST", "/_reindex"))); + ResponseException responseException = expectThrows( + ResponseException.class, + () -> client().performRequest(new Request("POST", "/_reindex")) + ); assertEquals(400, responseException.getResponse().getStatusLine().getStatusCode()); assertThat(responseException.getMessage(), containsString("request body is required")); } diff --git a/modules/reindex/src/javaRestTest/java/org/elasticsearch/index/reindex/remote/ReindexFromOldRemoteIT.java b/modules/reindex/src/javaRestTest/java/org/elasticsearch/index/reindex/remote/ReindexFromOldRemoteIT.java index 7a20ab9e7c18a..209ed34afcc4e 100644 --- a/modules/reindex/src/javaRestTest/java/org/elasticsearch/index/reindex/remote/ReindexFromOldRemoteIT.java +++ b/modules/reindex/src/javaRestTest/java/org/elasticsearch/index/reindex/remote/ReindexFromOldRemoteIT.java @@ -54,14 +54,17 @@ private void oldEsTestCase(String portPropertyName, String requestsPerSecond) th + " \"index\": \"test\",\n" + " \"size\": 1,\n" + " \"remote\": {\n" - + " \"host\": \"http://127.0.0.1:" + oldEsPort + "\"\n" + + " \"host\": \"http://127.0.0.1:" + + oldEsPort + + "\"\n" + " }\n" + " },\n" + " \"dest\": {\n" + " \"index\": \"test\",\n" + " \"version_type\": \"external\"\n" + " }\n" - + "}"); + + "}" + ); } else { // Reindex using the default internal version_type reindex.setJsonEntity( @@ -70,13 +73,16 @@ private void oldEsTestCase(String portPropertyName, String requestsPerSecond) th + " \"index\": \"test\",\n" + " \"size\": 1,\n" + " \"remote\": {\n" - + " \"host\": \"http://127.0.0.1:" + oldEsPort + "\"\n" + + " \"host\": \"http://127.0.0.1:" + + oldEsPort + + "\"\n" + " }\n" + " },\n" + " \"dest\": {\n" + " \"index\": \"test\"\n" + " }\n" - + "}"); + + "}" + ); } reindex.addParameter("refresh", "true"); reindex.addParameter("pretty", "true"); diff --git a/modules/reindex/src/main/java/org/elasticsearch/reindex/AbstractAsyncBulkByScrollAction.java b/modules/reindex/src/main/java/org/elasticsearch/reindex/AbstractAsyncBulkByScrollAction.java index aa9c821eefa8a..b277426a87968 100644 --- a/modules/reindex/src/main/java/org/elasticsearch/reindex/AbstractAsyncBulkByScrollAction.java +++ b/modules/reindex/src/main/java/org/elasticsearch/reindex/AbstractAsyncBulkByScrollAction.java @@ -25,10 +25,10 @@ import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.support.TransportAction; import org.elasticsearch.client.ParentTaskAssigningClient; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.unit.ByteSizeValue; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.AbstractRunnable; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.VersionType; import org.elasticsearch.index.mapper.IdFieldMapper; import org.elasticsearch.index.mapper.IndexFieldMapper; @@ -79,7 +79,8 @@ * Abstract base for scrolling across a search and executing bulk actions on all results. All package private methods are package private so * their tests can use them. Most methods run in the listener thread pool because they are meant to be fast and don't expect to block. */ -public abstract class AbstractAsyncBulkByScrollAction, +public abstract class AbstractAsyncBulkByScrollAction< + Request extends AbstractBulkByScrollRequest, Action extends TransportAction> { protected final Logger logger; @@ -120,18 +121,46 @@ public abstract class AbstractAsyncBulkByScrollAction listener, - @Nullable ScriptService scriptService, @Nullable ReindexSslConfig sslConfig) { - this(task, needsSourceDocumentVersions, needsSourceDocumentSeqNoAndPrimaryTerm, logger, client, client, threadPool, mainRequest, - listener, scriptService, sslConfig); - } - AbstractAsyncBulkByScrollAction(BulkByScrollTask task, boolean needsSourceDocumentVersions, - boolean needsSourceDocumentSeqNoAndPrimaryTerm, Logger logger, ParentTaskAssigningClient searchClient, - ParentTaskAssigningClient bulkClient, ThreadPool threadPool, Request mainRequest, - ActionListener listener, @Nullable ScriptService scriptService, - @Nullable ReindexSslConfig sslConfig) { + AbstractAsyncBulkByScrollAction( + BulkByScrollTask task, + boolean needsSourceDocumentVersions, + boolean needsSourceDocumentSeqNoAndPrimaryTerm, + Logger logger, + ParentTaskAssigningClient client, + ThreadPool threadPool, + Request mainRequest, + ActionListener listener, + @Nullable ScriptService scriptService, + @Nullable ReindexSslConfig sslConfig + ) { + this( + task, + needsSourceDocumentVersions, + needsSourceDocumentSeqNoAndPrimaryTerm, + logger, + client, + client, + threadPool, + mainRequest, + listener, + scriptService, + sslConfig + ); + } + + AbstractAsyncBulkByScrollAction( + BulkByScrollTask task, + boolean needsSourceDocumentVersions, + boolean needsSourceDocumentSeqNoAndPrimaryTerm, + Logger logger, + ParentTaskAssigningClient searchClient, + ParentTaskAssigningClient bulkClient, + ThreadPool threadPool, + Request mainRequest, + ActionListener listener, + @Nullable ScriptService scriptService, + @Nullable ReindexSslConfig sslConfig + ) { this.task = task; this.scriptService = scriptService; this.sslConfig = sslConfig; @@ -227,16 +256,27 @@ protected BulkRequest buildBulk(Iterable docs } protected ScrollableHitSource buildScrollableResultSource(BackoffPolicy backoffPolicy) { - return new ClientScrollableHitSource(logger, backoffPolicy, threadPool, worker::countSearchRetry, - this::onScrollResponse, this::finishHim, searchClient, - mainRequest.getSearchRequest()); + return new ClientScrollableHitSource( + logger, + backoffPolicy, + threadPool, + worker::countSearchRetry, + this::onScrollResponse, + this::finishHim, + searchClient, + mainRequest.getSearchRequest() + ); } /** * Build the response for reindex actions. */ - protected BulkByScrollResponse buildResponse(TimeValue took, List indexingFailures, - List searchFailures, boolean timedOut) { + protected BulkByScrollResponse buildResponse( + TimeValue took, + List indexingFailures, + List searchFailures, + boolean timedOut + ) { return new BulkByScrollResponse(took, task.getStatus(), indexingFailures, searchFailures, timedOut); } @@ -283,10 +323,9 @@ void onScrollResponse(long lastBatchStartTimeNS, int lastBatchSize, ScrollConsum return; } if ( // If any of the shards failed that should abort the request. - (response.getFailures().size() > 0) - // Timeouts aren't shard failures but we still need to pass them back to the user. - || response.isTimedOut() - ) { + (response.getFailures().size() > 0) + // Timeouts aren't shard failures but we still need to pass them back to the user. + || response.isTimedOut()) { refreshAndFinish(emptyList(), response.getFailures(), response.isTimedOut()); return; } @@ -336,8 +375,9 @@ void prepareBulkRequest(long thisBatchStartTimeNS, ScrollConsumableHitsResponse if (mainRequest.getMaxDocs() != MAX_DOCS_ALL_MATCHES) { // Truncate the hits if we have more than the request max docs long remainingDocsToProcess = max(0, mainRequest.getMaxDocs() - worker.getSuccessfullyProcessed()); - hits = remainingDocsToProcess < asyncResponse.remainingHits() ? asyncResponse.consumeHits((int) remainingDocsToProcess) - : asyncResponse.consumeRemainingHits(); + hits = remainingDocsToProcess < asyncResponse.remainingHits() + ? asyncResponse.consumeHits((int) remainingDocsToProcess) + : asyncResponse.consumeRemainingHits(); } else { hits = asyncResponse.consumeRemainingHits(); } @@ -361,8 +401,12 @@ void prepareBulkRequest(long thisBatchStartTimeNS, ScrollConsumableHitsResponse void sendBulkRequest(BulkRequest request, Runnable onSuccess) { final int requestSize = request.requests().size(); if (logger.isDebugEnabled()) { - logger.debug("[{}]: sending [{}] entry, [{}] bulk request", task.getId(), requestSize, - new ByteSizeValue(request.estimatedSizeInBytes())); + logger.debug( + "[{}]: sending [{}] entry, [{}] bulk request", + task.getId(), + requestSize, + new ByteSizeValue(request.estimatedSizeInBytes()) + ); } if (task.isCancelled()) { logger.debug("[{}]: finishing early because the task was cancelled", task.getId()); @@ -440,9 +484,7 @@ void onBulkResponse(BulkResponse response, Runnable onSuccess) { } } - void notifyDone(long thisBatchStartTimeNS, - ScrollConsumableHitsResponse asyncResponse, - int batchSize) { + void notifyDone(long thisBatchStartTimeNS, ScrollConsumableHitsResponse asyncResponse, int batchSize) { if (task.isCancelled()) { logger.debug("[{}]: finishing early because the task was cancelled", task.getId()); finishHim(null); @@ -512,14 +554,16 @@ protected void finishHim(Exception failure) { * @param searchFailures any search failures accumulated during the request * @param timedOut have any of the sub-requests timed out? */ - protected void finishHim(Exception failure, List indexingFailures, - List searchFailures, boolean timedOut) { + protected void finishHim(Exception failure, List indexingFailures, List searchFailures, boolean timedOut) { logger.debug("[{}]: finishing without any catastrophic failures", task.getId()); scrollSource.close(() -> { if (failure == null) { BulkByScrollResponse response = buildResponse( - timeValueNanos(System.nanoTime() - startTime.get()), - indexingFailures, searchFailures, timedOut); + timeValueNanos(System.nanoTime() - startTime.get()), + indexingFailures, + searchFailures, + timedOut + ); listener.onResponse(response); } else { listener.onFailure(failure); @@ -747,10 +791,12 @@ public abstract static class ScriptApplier implements BiFunction params; - public ScriptApplier(WorkerBulkByScrollTaskState taskWorker, - ScriptService scriptService, - Script script, - Map params) { + public ScriptApplier( + WorkerBulkByScrollTaskState taskWorker, + ScriptService scriptService, + Script script, + Map params + ) { this.taskWorker = taskWorker; this.scriptService = scriptService; this.script = script; @@ -825,17 +871,17 @@ public RequestWrapper apply(RequestWrapper request, ScrollableHitSource.Hi protected RequestWrapper scriptChangedOpType(RequestWrapper request, OpType oldOpType, OpType newOpType) { switch (newOpType) { - case NOOP: - taskWorker.countNoop(); - return null; - case DELETE: - RequestWrapper delete = wrap(new DeleteRequest(request.getIndex(), request.getId())); - delete.setVersion(request.getVersion()); - delete.setVersionType(VersionType.INTERNAL); - delete.setRouting(request.getRouting()); - return delete; - default: - throw new IllegalArgumentException("Unsupported operation type change from [" + oldOpType + "] to [" + newOpType + "]"); + case NOOP: + taskWorker.countNoop(); + return null; + case DELETE: + RequestWrapper delete = wrap(new DeleteRequest(request.getIndex(), request.getId())); + delete.setVersion(request.getVersion()); + delete.setVersionType(VersionType.INTERNAL); + delete.setRouting(request.getRouting()); + return delete; + default: + throw new IllegalArgumentException("Unsupported operation type change from [" + oldOpType + "] to [" + newOpType + "]"); } } @@ -871,8 +917,9 @@ public static OpType fromString(String opType) { case "delete": return OpType.DELETE; default: - throw new IllegalArgumentException("Operation type [" + lowerOpType + "] not allowed, only " + - Arrays.toString(values()) + " are allowed"); + throw new IllegalArgumentException( + "Operation type [" + lowerOpType + "] not allowed, only " + Arrays.toString(values()) + " are allowed" + ); } } diff --git a/modules/reindex/src/main/java/org/elasticsearch/reindex/AbstractBaseReindexRestHandler.java b/modules/reindex/src/main/java/org/elasticsearch/reindex/AbstractBaseReindexRestHandler.java index 7804ca8abcf69..dd9545f3dd418 100644 --- a/modules/reindex/src/main/java/org/elasticsearch/reindex/AbstractBaseReindexRestHandler.java +++ b/modules/reindex/src/main/java/org/elasticsearch/reindex/AbstractBaseReindexRestHandler.java @@ -13,7 +13,6 @@ import org.elasticsearch.action.support.ActiveShardCount; import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.index.reindex.AbstractBulkByScrollRequest; import org.elasticsearch.index.reindex.BulkByScrollResponse; import org.elasticsearch.index.reindex.BulkByScrollTask; @@ -23,15 +22,15 @@ import org.elasticsearch.rest.RestStatus; import org.elasticsearch.tasks.LoggingTaskListener; import org.elasticsearch.tasks.Task; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.HashMap; import java.util.Map; public abstract class AbstractBaseReindexRestHandler< - Request extends AbstractBulkByScrollRequest, - A extends ActionType - > extends BaseRestHandler { + Request extends AbstractBulkByScrollRequest, + A extends ActionType> extends BaseRestHandler { private final A action; @@ -39,8 +38,8 @@ protected AbstractBaseReindexRestHandler(A action) { this.action = action; } - protected RestChannelConsumer doPrepareRequest(RestRequest request, NodeClient client, - boolean includeCreated, boolean includeUpdated) throws IOException { + protected RestChannelConsumer doPrepareRequest(RestRequest request, NodeClient client, boolean includeCreated, boolean includeUpdated) + throws IOException { // Build the internal request Request internal = setCommonOptions(request, buildRequest(request, client.getNamedWriteableRegistry())); @@ -130,12 +129,15 @@ private static Integer parseSlices(RestRequest request) { slices = Integer.parseInt(slicesString); } catch (NumberFormatException e) { throw new IllegalArgumentException( - "[slices] must be a positive integer or the string \"auto\", but was [" + slicesString + "]", e); + "[slices] must be a positive integer or the string \"auto\", but was [" + slicesString + "]", + e + ); } if (slices < 1) { throw new IllegalArgumentException( - "[slices] must be a positive integer or the string \"auto\", but was [" + slicesString + "]"); + "[slices] must be a positive integer or the string \"auto\", but was [" + slicesString + "]" + ); } return slices; @@ -153,24 +155,23 @@ public static Float parseRequestsPerSecond(RestRequest request) { try { requestsPerSecond = Float.parseFloat(requestsPerSecondString); } catch (NumberFormatException e) { - throw new IllegalArgumentException( - "[requests_per_second] must be a float greater than 0. Use -1 to disable throttling.", e); + throw new IllegalArgumentException("[requests_per_second] must be a float greater than 0. Use -1 to disable throttling.", e); } if (requestsPerSecond == -1) { return Float.POSITIVE_INFINITY; } if (requestsPerSecond <= 0) { // We validate here and in the setters because the setters use "Float.POSITIVE_INFINITY" instead of -1 - throw new IllegalArgumentException( - "[requests_per_second] must be a float greater than 0. Use -1 to disable throttling."); + throw new IllegalArgumentException("[requests_per_second] must be a float greater than 0. Use -1 to disable throttling."); } return requestsPerSecond; } static void setMaxDocsValidateIdentical(AbstractBulkByScrollRequest request, int maxDocs) { if (request.getMaxDocs() != AbstractBulkByScrollRequest.MAX_DOCS_ALL_MATCHES && request.getMaxDocs() != maxDocs) { - throw new IllegalArgumentException("[max_docs] set to two different values [" + request.getMaxDocs() + "]" + - " and [" + maxDocs + "]"); + throw new IllegalArgumentException( + "[max_docs] set to two different values [" + request.getMaxDocs() + "]" + " and [" + maxDocs + "]" + ); } else { request.setMaxDocs(maxDocs); } diff --git a/modules/reindex/src/main/java/org/elasticsearch/reindex/AbstractBulkByQueryRestHandler.java b/modules/reindex/src/main/java/org/elasticsearch/reindex/AbstractBulkByQueryRestHandler.java index f40d78a4dfb89..2f7d3c5de5202 100644 --- a/modules/reindex/src/main/java/org/elasticsearch/reindex/AbstractBulkByQueryRestHandler.java +++ b/modules/reindex/src/main/java/org/elasticsearch/reindex/AbstractBulkByQueryRestHandler.java @@ -10,17 +10,17 @@ import org.elasticsearch.action.ActionType; import org.elasticsearch.action.search.SearchRequest; -import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.index.reindex.AbstractBulkByScrollRequest; import org.elasticsearch.index.reindex.BulkByScrollResponse; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.search.RestSearchAction; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.Map; @@ -31,24 +31,28 @@ * Rest handler for reindex actions that accepts a search request like Update-By-Query or Delete-By-Query */ public abstract class AbstractBulkByQueryRestHandler< - Request extends AbstractBulkByScrollRequest, - A extends ActionType> extends AbstractBaseReindexRestHandler { + Request extends AbstractBulkByScrollRequest, + A extends ActionType> extends AbstractBaseReindexRestHandler { protected AbstractBulkByQueryRestHandler(A action) { super(action); } - protected void parseInternalRequest(Request internal, RestRequest restRequest, NamedWriteableRegistry namedWriteableRegistry, - Map> bodyConsumers) throws IOException { + protected void parseInternalRequest( + Request internal, + RestRequest restRequest, + NamedWriteableRegistry namedWriteableRegistry, + Map> bodyConsumers + ) throws IOException { assert internal != null : "Request should not be null"; assert restRequest != null : "RestRequest should not be null"; SearchRequest searchRequest = internal.getSearchRequest(); try (XContentParser parser = extractRequestSpecificFields(restRequest, bodyConsumers)) { - IntConsumer sizeConsumer = restRequest.getRestApiVersion() == RestApiVersion.V_7 ? - size -> setMaxDocsFromSearchSize(internal, size) : - size -> failOnSizeSpecified(); + IntConsumer sizeConsumer = restRequest.getRestApiVersion() == RestApiVersion.V_7 + ? size -> setMaxDocsFromSearchSize(internal, size) + : size -> failOnSizeSpecified(); RestSearchAction.parseSearchRequest(searchRequest, restRequest, parser, namedWriteableRegistry, sizeConsumer); } @@ -72,13 +76,15 @@ protected void parseInternalRequest(Request internal, RestRequest restRequest, N * should get better when SearchRequest has full ObjectParser support * then we can delegate and stuff. */ - private XContentParser extractRequestSpecificFields(RestRequest restRequest, - Map> bodyConsumers) throws IOException { + private XContentParser extractRequestSpecificFields(RestRequest restRequest, Map> bodyConsumers) + throws IOException { if (restRequest.hasContentOrSourceParam() == false) { return null; // body is optional } - try (XContentParser parser = restRequest.contentOrSourceParamParser(); - XContentBuilder builder = XContentFactory.contentBuilder(parser.contentType())) { + try ( + XContentParser parser = restRequest.contentOrSourceParamParser(); + XContentBuilder builder = XContentFactory.contentBuilder(parser.contentType()) + ) { Map body = parser.map(); for (Map.Entry> consumer : bodyConsumers.entrySet()) { @@ -87,8 +93,13 @@ private XContentParser extractRequestSpecificFields(RestRequest restRequest, consumer.getValue().accept(value); } } - return parser.contentType().xContent().createParser(parser.getXContentRegistry(), - parser.getDeprecationHandler(), BytesReference.bytes(builder.map(body)).streamInput()); + return parser.contentType() + .xContent() + .createParser( + parser.getXContentRegistry(), + parser.getDeprecationHandler(), + BytesReference.bytes(builder.map(body)).streamInput() + ); } } diff --git a/modules/reindex/src/main/java/org/elasticsearch/reindex/AsyncDeleteByQueryAction.java b/modules/reindex/src/main/java/org/elasticsearch/reindex/AsyncDeleteByQueryAction.java index 2118f09557f0a..333c75d32f32a 100644 --- a/modules/reindex/src/main/java/org/elasticsearch/reindex/AsyncDeleteByQueryAction.java +++ b/modules/reindex/src/main/java/org/elasticsearch/reindex/AsyncDeleteByQueryAction.java @@ -24,9 +24,15 @@ */ public class AsyncDeleteByQueryAction extends AbstractAsyncBulkByScrollAction { - public AsyncDeleteByQueryAction(BulkByScrollTask task, Logger logger, ParentTaskAssigningClient client, - ThreadPool threadPool, DeleteByQueryRequest request, ScriptService scriptService, - ActionListener listener) { + public AsyncDeleteByQueryAction( + BulkByScrollTask task, + Logger logger, + ParentTaskAssigningClient client, + ThreadPool threadPool, + DeleteByQueryRequest request, + ScriptService scriptService, + ActionListener listener + ) { super(task, false, true, logger, client, threadPool, request, listener, scriptService, null); } diff --git a/modules/reindex/src/main/java/org/elasticsearch/reindex/BulkByScrollParallelizationHelper.java b/modules/reindex/src/main/java/org/elasticsearch/reindex/BulkByScrollParallelizationHelper.java index 19d772f338627..0a9e9d8972f66 100644 --- a/modules/reindex/src/main/java/org/elasticsearch/reindex/BulkByScrollParallelizationHelper.java +++ b/modules/reindex/src/main/java/org/elasticsearch/reindex/BulkByScrollParallelizationHelper.java @@ -8,8 +8,8 @@ package org.elasticsearch.reindex; -import org.elasticsearch.action.ActionType; import org.elasticsearch.action.ActionListener; +import org.elasticsearch.action.ActionType; import org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsRequest; import org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsResponse; import org.elasticsearch.action.search.SearchRequest; @@ -60,9 +60,14 @@ static > void startSlicedAc ActionListener listener, Client client, DiscoveryNode node, - Runnable workerAction) { - initTaskState(task, request, client, listener.delegateFailure( - (l, v) -> executeSlicedAction(task, request, action, l, client, node, workerAction))); + Runnable workerAction + ) { + initTaskState( + task, + request, + client, + listener.delegateFailure((l, v) -> executeSlicedAction(task, request, action, l, client, node, workerAction)) + ); } /** @@ -82,7 +87,8 @@ static > void executeSliced ActionListener listener, Client client, DiscoveryNode node, - Runnable workerAction) { + Runnable workerAction + ) { if (task.isLeader()) { sendSubRequests(client, action, node.getId(), task, request, listener); } else if (task.isWorker()) { @@ -104,7 +110,8 @@ static > void initTaskState BulkByScrollTask task, Request request, Client client, - ActionListener listener) { + ActionListener listener + ) { int configuredSlices = request.getSlices(); if (configuredSlices == AbstractBulkByScrollRequest.AUTO_SLICES) { ClusterSearchShardsRequest shardsRequest = new ClusterSearchShardsRequest(); @@ -122,7 +129,8 @@ static > void initTaskState private static > void setWorkerCount( Request request, BulkByScrollTask task, - int slices) { + int slices + ) { if (slices > 1) { task.setWorkerCount(slices); } else { @@ -133,23 +141,21 @@ private static > void setWo } private static int countSlicesBasedOnShards(ClusterSearchShardsResponse response) { - Map countsByIndex = Arrays.stream(response.getGroups()).collect(Collectors.toMap( - group -> group.getShardId().getIndex(), - group -> 1, - (sum, term) -> sum + term - )); + Map countsByIndex = Arrays.stream(response.getGroups()) + .collect(Collectors.toMap(group -> group.getShardId().getIndex(), group -> 1, (sum, term) -> sum + term)); Set counts = new HashSet<>(countsByIndex.values()); int leastShards = counts.isEmpty() ? 1 : Collections.min(counts); return Math.min(leastShards, AUTO_SLICE_CEILING); } private static > void sendSubRequests( - Client client, - ActionType action, - String localNodeId, - BulkByScrollTask task, - Request request, - ActionListener listener) { + Client client, + ActionType action, + String localNodeId, + BulkByScrollTask task, + Request request, + ActionListener listener + ) { LeaderBulkByScrollTaskState worker = task.getLeaderState(); int totalSlices = worker.getSlices(); @@ -158,8 +164,9 @@ private static > void sendS // TODO move the request to the correct node. maybe here or somehow do it as part of startup for reindex in general.... Request requestForSlice = request.forSlice(parentTaskId, slice, totalSlices); ActionListener sliceListener = ActionListener.wrap( - r -> worker.onSliceResponse(listener, slice.source().slice().getId(), r), - e -> worker.onSliceFailure(listener, slice.source().slice().getId(), e)); + r -> worker.onSliceResponse(listener, slice.source().slice().getId(), r), + e -> worker.onSliceFailure(listener, slice.source().slice().getId(), e) + ); client.execute(action, requestForSlice, sliceListener); } } diff --git a/modules/reindex/src/main/java/org/elasticsearch/reindex/BulkIndexByScrollResponseContentListener.java b/modules/reindex/src/main/java/org/elasticsearch/reindex/BulkIndexByScrollResponseContentListener.java index 363840cc50976..38792189efc3d 100644 --- a/modules/reindex/src/main/java/org/elasticsearch/reindex/BulkIndexByScrollResponseContentListener.java +++ b/modules/reindex/src/main/java/org/elasticsearch/reindex/BulkIndexByScrollResponseContentListener.java @@ -9,8 +9,6 @@ package org.elasticsearch.reindex; import org.elasticsearch.action.bulk.BulkItemResponse.Failure; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.index.reindex.BulkByScrollResponse; import org.elasticsearch.index.reindex.ScrollableHitSource.SearchFailure; import org.elasticsearch.rest.BytesRestResponse; @@ -18,6 +16,8 @@ import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; import java.util.Map; @@ -55,7 +55,7 @@ private RestStatus getStatus(BulkByScrollResponse response) { status = failure.getStatus(); } } - for (SearchFailure failure: response.getSearchFailures()) { + for (SearchFailure failure : response.getSearchFailures()) { RestStatus failureStatus = failure.getStatus(); if (failureStatus.getStatus() > status.getStatus()) { status = failureStatus; diff --git a/modules/reindex/src/main/java/org/elasticsearch/reindex/ReindexPlugin.java b/modules/reindex/src/main/java/org/elasticsearch/reindex/ReindexPlugin.java index 1228f36b805f4..752e653076c85 100644 --- a/modules/reindex/src/main/java/org/elasticsearch/reindex/ReindexPlugin.java +++ b/modules/reindex/src/main/java/org/elasticsearch/reindex/ReindexPlugin.java @@ -20,7 +20,6 @@ import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsFilter; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.env.Environment; import org.elasticsearch.env.NodeEnvironment; import org.elasticsearch.index.reindex.BulkByScrollTask; @@ -36,6 +35,7 @@ import org.elasticsearch.tasks.Task; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.watcher.ResourceWatcherService; +import org.elasticsearch.xcontent.NamedXContentRegistry; import java.util.ArrayList; import java.util.Arrays; @@ -51,36 +51,53 @@ public class ReindexPlugin extends Plugin implements ActionPlugin { @Override public List> getActions() { - return Arrays.asList(new ActionHandler<>(ReindexAction.INSTANCE, TransportReindexAction.class), - new ActionHandler<>(UpdateByQueryAction.INSTANCE, TransportUpdateByQueryAction.class), - new ActionHandler<>(DeleteByQueryAction.INSTANCE, TransportDeleteByQueryAction.class), - new ActionHandler<>(RethrottleAction.INSTANCE, TransportRethrottleAction.class)); + return Arrays.asList( + new ActionHandler<>(ReindexAction.INSTANCE, TransportReindexAction.class), + new ActionHandler<>(UpdateByQueryAction.INSTANCE, TransportUpdateByQueryAction.class), + new ActionHandler<>(DeleteByQueryAction.INSTANCE, TransportDeleteByQueryAction.class), + new ActionHandler<>(RethrottleAction.INSTANCE, TransportRethrottleAction.class) + ); } @Override public List getNamedWriteables() { return singletonList( - new NamedWriteableRegistry.Entry(Task.Status.class, BulkByScrollTask.Status.NAME, BulkByScrollTask.Status::new)); + new NamedWriteableRegistry.Entry(Task.Status.class, BulkByScrollTask.Status.NAME, BulkByScrollTask.Status::new) + ); } @Override - public List getRestHandlers(Settings settings, RestController restController, ClusterSettings clusterSettings, - IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter, IndexNameExpressionResolver indexNameExpressionResolver, - Supplier nodesInCluster) { + public List getRestHandlers( + Settings settings, + RestController restController, + ClusterSettings clusterSettings, + IndexScopedSettings indexScopedSettings, + SettingsFilter settingsFilter, + IndexNameExpressionResolver indexNameExpressionResolver, + Supplier nodesInCluster + ) { return Arrays.asList( - new RestReindexAction(), - new RestUpdateByQueryAction(), - new RestDeleteByQueryAction(), - new RestRethrottleAction(nodesInCluster)); + new RestReindexAction(), + new RestUpdateByQueryAction(), + new RestDeleteByQueryAction(), + new RestRethrottleAction(nodesInCluster) + ); } @Override - public Collection createComponents(Client client, ClusterService clusterService, ThreadPool threadPool, - ResourceWatcherService resourceWatcherService, ScriptService scriptService, - NamedXContentRegistry xContentRegistry, Environment environment, - NodeEnvironment nodeEnvironment, NamedWriteableRegistry namedWriteableRegistry, - IndexNameExpressionResolver expressionResolver, - Supplier repositoriesServiceSupplier) { + public Collection createComponents( + Client client, + ClusterService clusterService, + ThreadPool threadPool, + ResourceWatcherService resourceWatcherService, + ScriptService scriptService, + NamedXContentRegistry xContentRegistry, + Environment environment, + NodeEnvironment nodeEnvironment, + NamedWriteableRegistry namedWriteableRegistry, + IndexNameExpressionResolver expressionResolver, + Supplier repositoriesServiceSupplier + ) { return Collections.singletonList(new ReindexSslConfig(environment.settings(), environment, resourceWatcherService)); } diff --git a/modules/reindex/src/main/java/org/elasticsearch/reindex/ReindexSslConfig.java b/modules/reindex/src/main/java/org/elasticsearch/reindex/ReindexSslConfig.java index 4f4c6d0d3a703..0f597b4276d98 100644 --- a/modules/reindex/src/main/java/org/elasticsearch/reindex/ReindexSslConfig.java +++ b/modules/reindex/src/main/java/org/elasticsearch/reindex/ReindexSslConfig.java @@ -24,8 +24,6 @@ import org.elasticsearch.watcher.FileWatcher; import org.elasticsearch.watcher.ResourceWatcherService; -import javax.net.ssl.HostnameVerifier; -import javax.net.ssl.SSLContext; import java.io.IOException; import java.io.UncheckedIOException; import java.nio.file.Path; @@ -36,6 +34,9 @@ import java.util.Map; import java.util.function.Function; +import javax.net.ssl.HostnameVerifier; +import javax.net.ssl.SSLContext; + import static org.elasticsearch.common.settings.Setting.listSetting; import static org.elasticsearch.common.settings.Setting.simpleString; @@ -50,7 +51,9 @@ public class ReindexSslConfig { static { Setting.Property[] defaultProperties = new Setting.Property[] { Setting.Property.NodeScope, Setting.Property.Filtered }; - Setting.Property[] deprecatedProperties = new Setting.Property[] { Setting.Property.Deprecated, Setting.Property.NodeScope, + Setting.Property[] deprecatedProperties = new Setting.Property[] { + Setting.Property.Deprecated, + Setting.Property.NodeScope, Setting.Property.Filtered }; for (String key : SslConfigurationKeys.getStringKeys()) { String settingName = "reindex.ssl." + key; diff --git a/modules/reindex/src/main/java/org/elasticsearch/reindex/ReindexValidator.java b/modules/reindex/src/main/java/org/elasticsearch/reindex/ReindexValidator.java index f4d85e5d9c2d3..ae281c262bb9b 100644 --- a/modules/reindex/src/main/java/org/elasticsearch/reindex/ReindexValidator.java +++ b/modules/reindex/src/main/java/org/elasticsearch/reindex/ReindexValidator.java @@ -35,16 +35,20 @@ public class ReindexValidator { private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(ReindexValidator.class); - static final String SORT_DEPRECATED_MESSAGE = "The sort option in reindex is deprecated. " + - "Instead consider using query filtering to find the desired subset of data."; + static final String SORT_DEPRECATED_MESSAGE = "The sort option in reindex is deprecated. " + + "Instead consider using query filtering to find the desired subset of data."; private final CharacterRunAutomaton remoteWhitelist; private final ClusterService clusterService; private final IndexNameExpressionResolver resolver; private final AutoCreateIndex autoCreateIndex; - ReindexValidator(Settings settings, ClusterService clusterService, IndexNameExpressionResolver resolver, - AutoCreateIndex autoCreateIndex) { + ReindexValidator( + Settings settings, + ClusterService clusterService, + IndexNameExpressionResolver resolver, + AutoCreateIndex autoCreateIndex + ) { this.remoteWhitelist = buildRemoteWhitelist(TransportReindexAction.REMOTE_CLUSTER_WHITELIST.get(settings)); this.clusterService = clusterService; this.resolver = resolver; @@ -54,8 +58,14 @@ public class ReindexValidator { public void initialValidation(ReindexRequest request) { checkRemoteWhitelist(remoteWhitelist, request.getRemoteInfo()); ClusterState state = clusterService.state(); - validateAgainstAliases(request.getSearchRequest(), request.getDestination(), request.getRemoteInfo(), resolver, autoCreateIndex, - state); + validateAgainstAliases( + request.getSearchRequest(), + request.getDestination(), + request.getRemoteInfo(), + resolver, + autoCreateIndex, + state + ); SearchSourceBuilder searchSource = request.getSearchRequest().source(); if (searchSource != null && searchSource.sorts() != null && searchSource.sorts().isEmpty() == false) { deprecationLogger.critical(DeprecationCategory.API, "reindex_sort", SORT_DEPRECATED_MESSAGE); @@ -85,9 +95,13 @@ static CharacterRunAutomaton buildRemoteWhitelist(List whitelist) { Automaton automaton = Regex.simpleMatchToAutomaton(whitelist.toArray(Strings.EMPTY_ARRAY)); automaton = MinimizationOperations.minimize(automaton, Operations.DEFAULT_DETERMINIZE_WORK_LIMIT); if (Operations.isTotal(automaton)) { - throw new IllegalArgumentException("Refusing to start because whitelist " + whitelist + " accepts all addresses. " - + "This would allow users to reindex-from-remote any URL they like effectively having Elasticsearch make HTTP GETs " - + "for them."); + throw new IllegalArgumentException( + "Refusing to start because whitelist " + + whitelist + + " accepts all addresses. " + + "This would allow users to reindex-from-remote any URL they like effectively having Elasticsearch make HTTP GETs " + + "for them." + ); } return new CharacterRunAutomaton(automaton); } @@ -98,20 +112,23 @@ static CharacterRunAutomaton buildRemoteWhitelist(List whitelist) { * This cannot be done during request validation because the cluster state * isn't available then. Package private for testing. */ - static void validateAgainstAliases(SearchRequest source, IndexRequest destination, RemoteInfo remoteInfo, - IndexNameExpressionResolver indexNameExpressionResolver, AutoCreateIndex autoCreateIndex, - ClusterState clusterState) { + static void validateAgainstAliases( + SearchRequest source, + IndexRequest destination, + RemoteInfo remoteInfo, + IndexNameExpressionResolver indexNameExpressionResolver, + AutoCreateIndex autoCreateIndex, + ClusterState clusterState + ) { if (remoteInfo != null) { return; } String target = destination.index(); if (destination.isRequireAlias() && (false == clusterState.getMetadata().hasAlias(target))) { - throw new IndexNotFoundException("[" - + DocWriteRequest.REQUIRE_ALIAS - + "] request flag is [true] and [" - + target - + "] is not an alias", - target); + throw new IndexNotFoundException( + "[" + DocWriteRequest.REQUIRE_ALIAS + "] request flag is [true] and [" + target + "] is not an alias", + target + ); } if (false == autoCreateIndex.shouldAutoCreate(target, clusterState)) { /* diff --git a/modules/reindex/src/main/java/org/elasticsearch/reindex/Reindexer.java b/modules/reindex/src/main/java/org/elasticsearch/reindex/Reindexer.java index 10e9373853604..c5b326288b839 100644 --- a/modules/reindex/src/main/java/org/elasticsearch/reindex/Reindexer.java +++ b/modules/reindex/src/main/java/org/elasticsearch/reindex/Reindexer.java @@ -31,11 +31,6 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.lucene.uid.Versions; -import org.elasticsearch.xcontent.DeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.VersionType; import org.elasticsearch.index.mapper.VersionFieldMapper; import org.elasticsearch.index.reindex.BulkByScrollResponse; @@ -49,6 +44,11 @@ import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptService; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.DeprecationHandler; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; import java.io.IOException; import java.io.InputStream; @@ -75,8 +75,13 @@ public class Reindexer { private final ScriptService scriptService; private final ReindexSslConfig reindexSslConfig; - Reindexer(ClusterService clusterService, Client client, ThreadPool threadPool, ScriptService scriptService, - ReindexSslConfig reindexSslConfig) { + Reindexer( + ClusterService clusterService, + Client client, + ThreadPool threadPool, + ScriptService scriptService, + ReindexSslConfig reindexSslConfig + ) { this.clusterService = clusterService; this.client = client; this.threadPool = threadPool; @@ -89,16 +94,30 @@ public void initTask(BulkByScrollTask task, ReindexRequest request, ActionListen } public void execute(BulkByScrollTask task, ReindexRequest request, Client bulkClient, ActionListener listener) { - BulkByScrollParallelizationHelper.executeSlicedAction(task, request, ReindexAction.INSTANCE, listener, client, + BulkByScrollParallelizationHelper.executeSlicedAction( + task, + request, + ReindexAction.INSTANCE, + listener, + client, clusterService.localNode(), () -> { ParentTaskAssigningClient assigningClient = new ParentTaskAssigningClient(client, clusterService.localNode(), task); - ParentTaskAssigningClient assigningBulkClient = - new ParentTaskAssigningClient(bulkClient, clusterService.localNode(), task); - AsyncIndexBySearchAction searchAction = new AsyncIndexBySearchAction(task, logger, assigningClient, assigningBulkClient, - threadPool, scriptService, reindexSslConfig, request, listener); + ParentTaskAssigningClient assigningBulkClient = new ParentTaskAssigningClient(bulkClient, clusterService.localNode(), task); + AsyncIndexBySearchAction searchAction = new AsyncIndexBySearchAction( + task, + logger, + assigningClient, + assigningBulkClient, + threadPool, + scriptService, + reindexSslConfig, + request, + listener + ); searchAction.start(); - }); + } + ); } /** @@ -114,36 +133,33 @@ static RestClient buildRestClient(RemoteInfo remoteInfo, ReindexSslConfig sslCon for (Map.Entry header : remoteInfo.getHeaders().entrySet()) { clientHeaders[i++] = new BasicHeader(header.getKey(), header.getValue()); } - final RestClientBuilder builder = - RestClient.builder(new HttpHost(remoteInfo.getHost(), remoteInfo.getPort(), remoteInfo.getScheme())) - .setDefaultHeaders(clientHeaders) - .setRequestConfigCallback(c -> { - c.setConnectTimeout(Math.toIntExact(remoteInfo.getConnectTimeout().millis())); - c.setSocketTimeout(Math.toIntExact(remoteInfo.getSocketTimeout().millis())); - return c; - }) - .setHttpClientConfigCallback(c -> { - // Enable basic auth if it is configured - if (remoteInfo.getUsername() != null) { - UsernamePasswordCredentials creds = new UsernamePasswordCredentials(remoteInfo.getUsername(), - remoteInfo.getPassword()); - CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); - credentialsProvider.setCredentials(AuthScope.ANY, creds); - c.setDefaultCredentialsProvider(credentialsProvider); - } - // Stick the task id in the thread name so we can track down tasks from stack traces - AtomicInteger threads = new AtomicInteger(); - c.setThreadFactory(r -> { - String name = "es-client-" + taskId + "-" + threads.getAndIncrement(); - Thread t = new Thread(r, name); - threadCollector.add(t); - return t; - }); - // Limit ourselves to one reactor thread because for now the search process is single threaded. - c.setDefaultIOReactorConfig(IOReactorConfig.custom().setIoThreadCount(1).build()); - c.setSSLStrategy(sslConfig.getStrategy()); - return c; - }); + final RestClientBuilder builder = RestClient.builder( + new HttpHost(remoteInfo.getHost(), remoteInfo.getPort(), remoteInfo.getScheme()) + ).setDefaultHeaders(clientHeaders).setRequestConfigCallback(c -> { + c.setConnectTimeout(Math.toIntExact(remoteInfo.getConnectTimeout().millis())); + c.setSocketTimeout(Math.toIntExact(remoteInfo.getSocketTimeout().millis())); + return c; + }).setHttpClientConfigCallback(c -> { + // Enable basic auth if it is configured + if (remoteInfo.getUsername() != null) { + UsernamePasswordCredentials creds = new UsernamePasswordCredentials(remoteInfo.getUsername(), remoteInfo.getPassword()); + CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); + credentialsProvider.setCredentials(AuthScope.ANY, creds); + c.setDefaultCredentialsProvider(credentialsProvider); + } + // Stick the task id in the thread name so we can track down tasks from stack traces + AtomicInteger threads = new AtomicInteger(); + c.setThreadFactory(r -> { + String name = "es-client-" + taskId + "-" + threads.getAndIncrement(); + Thread t = new Thread(r, name); + threadCollector.add(t); + return t; + }); + // Limit ourselves to one reactor thread because for now the search process is single threaded. + c.setDefaultIOReactorConfig(IOReactorConfig.custom().setIoThreadCount(1).build()); + c.setSSLStrategy(sslConfig.getStrategy()); + return c; + }); if (Strings.hasLength(remoteInfo.getPathPrefix()) && "/".equals(remoteInfo.getPathPrefix()) == false) { builder.setPathPrefix(remoteInfo.getPathPrefix()); } @@ -166,16 +182,34 @@ static class AsyncIndexBySearchAction extends AbstractAsyncBulkByScrollAction createdThreads = emptyList(); - AsyncIndexBySearchAction(BulkByScrollTask task, Logger logger, ParentTaskAssigningClient searchClient, - ParentTaskAssigningClient bulkClient, ThreadPool threadPool, ScriptService scriptService, - ReindexSslConfig sslConfig, ReindexRequest request, ActionListener listener) { - super(task, + AsyncIndexBySearchAction( + BulkByScrollTask task, + Logger logger, + ParentTaskAssigningClient searchClient, + ParentTaskAssigningClient bulkClient, + ThreadPool threadPool, + ScriptService scriptService, + ReindexSslConfig sslConfig, + ReindexRequest request, + ActionListener listener + ) { + super( + task, /* * We only need the source version if we're going to use it when write and we only do that when the destination request uses * external versioning. */ request.getDestination().versionType() != VersionType.INTERNAL, - false, logger, searchClient, bulkClient, threadPool, request, listener, scriptService, sslConfig); + false, + logger, + searchClient, + bulkClient, + threadPool, + request, + listener, + scriptService, + sslConfig + ); } @Override @@ -185,21 +219,33 @@ protected ScrollableHitSource buildScrollableResultSource(BackoffPolicy backoffP createdThreads = synchronizedList(new ArrayList<>()); assert sslConfig != null : "Reindex ssl config must be set"; RestClient restClient = buildRestClient(remoteInfo, sslConfig, task.getId(), createdThreads); - return new RemoteScrollableHitSource(logger, backoffPolicy, threadPool, worker::countSearchRetry, - this::onScrollResponse, this::finishHim, - restClient, remoteInfo.getQuery(), mainRequest.getSearchRequest()); + return new RemoteScrollableHitSource( + logger, + backoffPolicy, + threadPool, + worker::countSearchRetry, + this::onScrollResponse, + this::finishHim, + restClient, + remoteInfo.getQuery(), + mainRequest.getSearchRequest() + ); } return super.buildScrollableResultSource(backoffPolicy); } @Override - protected void finishHim(Exception failure, List indexingFailures, - List searchFailures, boolean timedOut) { + protected void finishHim( + Exception failure, + List indexingFailures, + List searchFailures, + boolean timedOut + ) { super.finishHim(failure, indexingFailures, searchFailures, timedOut); // A little extra paranoia so we log something if we leave any threads running for (Thread thread : createdThreads) { if (thread.isAlive()) { - assert false: "Failed to properly stop client thread [" + thread.getName() + "]"; + assert false : "Failed to properly stop client thread [" + thread.getName() + "]"; logger.error("Failed to properly stop client thread [{}]", thread.getName()); } } @@ -242,16 +288,20 @@ protected RequestWrapper buildRequest(ScrollableHitSource.Hit doc) final XContentType mainRequestXContentType = mainRequest.getDestination().getContentType(); if (mainRequestXContentType != null && doc.getXContentType() != mainRequestXContentType) { // we need to convert - try (InputStream stream = doc.getSource().streamInput(); - XContentParser parser = sourceXContentType.xContent() - .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, stream); - XContentBuilder builder = XContentBuilder.builder(mainRequestXContentType.xContent())) { + try ( + InputStream stream = doc.getSource().streamInput(); + XContentParser parser = sourceXContentType.xContent() + .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, stream); + XContentBuilder builder = XContentBuilder.builder(mainRequestXContentType.xContent()) + ) { parser.nextToken(); builder.copyCurrentStructure(parser); index.source(BytesReference.bytes(builder), builder.contentType()); } catch (IOException e) { - throw new UncheckedIOException("failed to convert hit from " + sourceXContentType + " to " - + mainRequestXContentType, e); + throw new UncheckedIOException( + "failed to convert hit from " + sourceXContentType + " to " + mainRequestXContentType, + e + ); } } else { index.source(doc.getSource(), doc.getXContentType()); @@ -298,8 +348,12 @@ protected void copyRouting(RequestWrapper request, String routing) { class ReindexScriptApplier extends ScriptApplier { - ReindexScriptApplier(WorkerBulkByScrollTaskState taskWorker, ScriptService scriptService, Script script, - Map params) { + ReindexScriptApplier( + WorkerBulkByScrollTaskState taskWorker, + ScriptService scriptService, + Script script, + Map params + ) { super(taskWorker, scriptService, script, params); } diff --git a/modules/reindex/src/main/java/org/elasticsearch/reindex/RestDeleteByQueryAction.java b/modules/reindex/src/main/java/org/elasticsearch/reindex/RestDeleteByQueryAction.java index a3b7c90b8491a..eb82f8ed69a9f 100644 --- a/modules/reindex/src/main/java/org/elasticsearch/reindex/RestDeleteByQueryAction.java +++ b/modules/reindex/src/main/java/org/elasticsearch/reindex/RestDeleteByQueryAction.java @@ -9,8 +9,8 @@ package org.elasticsearch.reindex; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; +import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.index.reindex.DeleteByQueryAction; import org.elasticsearch.index.reindex.DeleteByQueryRequest; import org.elasticsearch.rest.RestRequest; @@ -32,7 +32,8 @@ public RestDeleteByQueryAction() { @Override public List routes() { - return List.of(new Route(POST, "/{index}/_delete_by_query"), + return List.of( + new Route(POST, "/{index}/_delete_by_query"), Route.builder(POST, "/{index}/{type}/_delete_by_query") .deprecated(RestSearchAction.TYPES_DEPRECATION_MESSAGE, RestApiVersion.V_7) .build() diff --git a/modules/reindex/src/main/java/org/elasticsearch/reindex/RestReindexAction.java b/modules/reindex/src/main/java/org/elasticsearch/reindex/RestReindexAction.java index 27cbb7c4a853d..c2ae165a9ee68 100644 --- a/modules/reindex/src/main/java/org/elasticsearch/reindex/RestReindexAction.java +++ b/modules/reindex/src/main/java/org/elasticsearch/reindex/RestReindexAction.java @@ -11,11 +11,11 @@ import org.elasticsearch.action.DocWriteRequest; import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.index.reindex.ReindexAction; import org.elasticsearch.index.reindex.ReindexRequest; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestRequestFilter; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.List; @@ -51,8 +51,9 @@ public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client @Override protected ReindexRequest buildRequest(RestRequest request, NamedWriteableRegistry namedWriteableRegistry) throws IOException { if (request.hasParam("pipeline")) { - throw new IllegalArgumentException("_reindex doesn't support [pipeline] as a query parameter. " - + "Specify it in the [dest] object instead."); + throw new IllegalArgumentException( + "_reindex doesn't support [pipeline] as a query parameter. " + "Specify it in the [dest] object instead." + ); } ReindexRequest internal; diff --git a/modules/reindex/src/main/java/org/elasticsearch/reindex/RestRethrottleAction.java b/modules/reindex/src/main/java/org/elasticsearch/reindex/RestRethrottleAction.java index fd255df4bb5b5..8605a210aec55 100644 --- a/modules/reindex/src/main/java/org/elasticsearch/reindex/RestRethrottleAction.java +++ b/modules/reindex/src/main/java/org/elasticsearch/reindex/RestRethrottleAction.java @@ -32,7 +32,8 @@ public List routes() { return List.of( new Route(POST, "/_update_by_query/{taskId}/_rethrottle"), new Route(POST, "/_delete_by_query/{taskId}/_rethrottle"), - new Route(POST, "/_reindex/{taskId}/_rethrottle")); + new Route(POST, "/_reindex/{taskId}/_rethrottle") + ); } @Override @@ -50,7 +51,10 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC } internalRequest.setRequestsPerSecond(requestsPerSecond); final String groupBy = request.param("group_by", "nodes"); - return channel -> - client.execute(RethrottleAction.INSTANCE, internalRequest, listTasksResponseListener(nodesInCluster, groupBy, channel)); + return channel -> client.execute( + RethrottleAction.INSTANCE, + internalRequest, + listTasksResponseListener(nodesInCluster, groupBy, channel) + ); } } diff --git a/modules/reindex/src/main/java/org/elasticsearch/reindex/RestUpdateByQueryAction.java b/modules/reindex/src/main/java/org/elasticsearch/reindex/RestUpdateByQueryAction.java index cd1a8279f895b..628158c1199a7 100644 --- a/modules/reindex/src/main/java/org/elasticsearch/reindex/RestUpdateByQueryAction.java +++ b/modules/reindex/src/main/java/org/elasticsearch/reindex/RestUpdateByQueryAction.java @@ -9,8 +9,8 @@ package org.elasticsearch.reindex; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; +import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.index.reindex.UpdateByQueryAction; import org.elasticsearch.index.reindex.UpdateByQueryRequest; import org.elasticsearch.rest.RestRequest; diff --git a/modules/reindex/src/main/java/org/elasticsearch/reindex/RethrottleRequest.java b/modules/reindex/src/main/java/org/elasticsearch/reindex/RethrottleRequest.java index bceec79c2f1bc..03f33fba7ef85 100644 --- a/modules/reindex/src/main/java/org/elasticsearch/reindex/RethrottleRequest.java +++ b/modules/reindex/src/main/java/org/elasticsearch/reindex/RethrottleRequest.java @@ -31,8 +31,7 @@ public class RethrottleRequest extends BaseTasksRequest { */ private Float requestsPerSecond; - public RethrottleRequest() { - } + public RethrottleRequest() {} public RethrottleRequest(StreamInput in) throws IOException { super(in); @@ -60,7 +59,8 @@ public float getRequestsPerSecond() { public RethrottleRequest setRequestsPerSecond(float requestsPerSecond) { if (requestsPerSecond <= 0) { throw new IllegalArgumentException( - "[requests_per_second] must be greater than 0. Use Float.POSITIVE_INFINITY to disable throttling."); + "[requests_per_second] must be greater than 0. Use Float.POSITIVE_INFINITY to disable throttling." + ); } this.requestsPerSecond = requestsPerSecond; return this; @@ -74,13 +74,15 @@ public ActionRequestValidationException validate() { } for (String action : getActions()) { switch (action) { - case ReindexAction.NAME: - case UpdateByQueryAction.NAME: - case DeleteByQueryAction.NAME: - continue; - default: - validationException = addValidationError( - "Can only change the throttling on reindex or update-by-query. Not on [" + action + "]", validationException); + case ReindexAction.NAME: + case UpdateByQueryAction.NAME: + case DeleteByQueryAction.NAME: + continue; + default: + validationException = addValidationError( + "Can only change the throttling on reindex or update-by-query. Not on [" + action + "]", + validationException + ); } } return validationException; diff --git a/modules/reindex/src/main/java/org/elasticsearch/reindex/RethrottleRequestBuilder.java b/modules/reindex/src/main/java/org/elasticsearch/reindex/RethrottleRequestBuilder.java index 87c2454a27883..61c8e58dc39d6 100644 --- a/modules/reindex/src/main/java/org/elasticsearch/reindex/RethrottleRequestBuilder.java +++ b/modules/reindex/src/main/java/org/elasticsearch/reindex/RethrottleRequestBuilder.java @@ -17,8 +17,7 @@ * Java API support for changing the throttle on reindex tasks while they are running. */ public class RethrottleRequestBuilder extends TasksRequestBuilder { - public RethrottleRequestBuilder(ElasticsearchClient client, - ActionType action) { + public RethrottleRequestBuilder(ElasticsearchClient client, ActionType action) { super(client, action, new RethrottleRequest()); } diff --git a/modules/reindex/src/main/java/org/elasticsearch/reindex/TransportDeleteByQueryAction.java b/modules/reindex/src/main/java/org/elasticsearch/reindex/TransportDeleteByQueryAction.java index 8f322e66fc5e1..320129cadfebb 100644 --- a/modules/reindex/src/main/java/org/elasticsearch/reindex/TransportDeleteByQueryAction.java +++ b/modules/reindex/src/main/java/org/elasticsearch/reindex/TransportDeleteByQueryAction.java @@ -25,7 +25,6 @@ import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; - public class TransportDeleteByQueryAction extends HandledTransportAction { private final ThreadPool threadPool; @@ -34,10 +33,20 @@ public class TransportDeleteByQueryAction extends HandledTransportAction) DeleteByQueryRequest::new); + public TransportDeleteByQueryAction( + ThreadPool threadPool, + ActionFilters actionFilters, + Client client, + TransportService transportService, + ScriptService scriptService, + ClusterService clusterService + ) { + super( + DeleteByQueryAction.NAME, + transportService, + actionFilters, + (Writeable.Reader) DeleteByQueryRequest::new + ); this.threadPool = threadPool; this.client = client; this.scriptService = scriptService; @@ -47,13 +56,21 @@ public TransportDeleteByQueryAction(ThreadPool threadPool, ActionFilters actionF @Override public void doExecute(Task task, DeleteByQueryRequest request, ActionListener listener) { BulkByScrollTask bulkByScrollTask = (BulkByScrollTask) task; - BulkByScrollParallelizationHelper.startSlicedAction(request, bulkByScrollTask, DeleteByQueryAction.INSTANCE, listener, client, + BulkByScrollParallelizationHelper.startSlicedAction( + request, + bulkByScrollTask, + DeleteByQueryAction.INSTANCE, + listener, + client, clusterService.localNode(), () -> { - ParentTaskAssigningClient assigningClient = new ParentTaskAssigningClient(client, clusterService.localNode(), - bulkByScrollTask); - new AsyncDeleteByQueryAction(bulkByScrollTask, logger, assigningClient, threadPool, request, scriptService, - listener).start(); + ParentTaskAssigningClient assigningClient = new ParentTaskAssigningClient( + client, + clusterService.localNode(), + bulkByScrollTask + ); + new AsyncDeleteByQueryAction(bulkByScrollTask, logger, assigningClient, threadPool, request, scriptService, listener) + .start(); } ); } diff --git a/modules/reindex/src/main/java/org/elasticsearch/reindex/TransportReindexAction.java b/modules/reindex/src/main/java/org/elasticsearch/reindex/TransportReindexAction.java index 23d89b1567723..3bc0019aa2d71 100644 --- a/modules/reindex/src/main/java/org/elasticsearch/reindex/TransportReindexAction.java +++ b/modules/reindex/src/main/java/org/elasticsearch/reindex/TransportReindexAction.java @@ -8,11 +8,6 @@ package org.elasticsearch.reindex; -import static java.util.Collections.emptyList; - -import java.util.List; -import java.util.function.Function; - import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.support.ActionFilters; import org.elasticsearch.action.support.AutoCreateIndex; @@ -33,9 +28,18 @@ import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; +import java.util.List; +import java.util.function.Function; + +import static java.util.Collections.emptyList; + public class TransportReindexAction extends HandledTransportAction { - public static final Setting> REMOTE_CLUSTER_WHITELIST = - Setting.listSetting("reindex.remote.whitelist", emptyList(), Function.identity(), Property.NodeScope); + public static final Setting> REMOTE_CLUSTER_WHITELIST = Setting.listSetting( + "reindex.remote.whitelist", + emptyList(), + Function.identity(), + Property.NodeScope + ); protected final ReindexValidator reindexValidator; private final Reindexer reindexer; @@ -43,17 +47,46 @@ public class TransportReindexAction extends HandledTransportAction listener) { validate(request); BulkByScrollTask bulkByScrollTask = (BulkByScrollTask) task; - reindexer.initTask(bulkByScrollTask, request, - listener.delegateFailure((l, v) -> reindexer.execute(bulkByScrollTask, request, getBulkClient(), l))); + reindexer.initTask( + bulkByScrollTask, + request, + listener.delegateFailure((l, v) -> reindexer.execute(bulkByScrollTask, request, getBulkClient(), l)) + ); } /** diff --git a/modules/reindex/src/main/java/org/elasticsearch/reindex/TransportRethrottleAction.java b/modules/reindex/src/main/java/org/elasticsearch/reindex/TransportRethrottleAction.java index cbce4513cf37d..d7ad28381cf63 100644 --- a/modules/reindex/src/main/java/org/elasticsearch/reindex/TransportRethrottleAction.java +++ b/modules/reindex/src/main/java/org/elasticsearch/reindex/TransportRethrottleAction.java @@ -31,10 +31,22 @@ public class TransportRethrottleAction extends TransportTasksAction listener) { + static void rethrottle( + Logger logger, + String localNodeId, + Client client, + BulkByScrollTask task, + float newRequestsPerSecond, + ActionListener listener + ) { if (task.isWorker()) { rethrottleChildTask(logger, localNodeId, task, newRequestsPerSecond, listener); @@ -56,12 +74,19 @@ static void rethrottle(Logger logger, String localNodeId, Client client, BulkByS return; } - throw new IllegalArgumentException("task [" + task.getId() + "] has not yet been initialized to the point where it knows how to " + - "rethrottle itself"); + throw new IllegalArgumentException( + "task [" + task.getId() + "] has not yet been initialized to the point where it knows how to " + "rethrottle itself" + ); } - private static void rethrottleParentTask(Logger logger, String localNodeId, Client client, BulkByScrollTask task, - float newRequestsPerSecond, ActionListener listener) { + private static void rethrottleParentTask( + Logger logger, + String localNodeId, + Client client, + BulkByScrollTask task, + float newRequestsPerSecond, + ActionListener listener + ) { final LeaderBulkByScrollTaskState leaderState = task.getLeaderState(); final int runningSubtasks = leaderState.runningSliceSubTasks(); @@ -69,30 +94,36 @@ private static void rethrottleParentTask(Logger logger, String localNodeId, Clie RethrottleRequest subRequest = new RethrottleRequest(); subRequest.setRequestsPerSecond(newRequestsPerSecond / runningSubtasks); subRequest.setParentTaskId(new TaskId(localNodeId, task.getId())); - logger.debug("rethrottling children of task [{}] to [{}] requests per second", task.getId(), - subRequest.getRequestsPerSecond()); - client.execute(RethrottleAction.INSTANCE, subRequest, ActionListener.wrap( - r -> { - r.rethrowFailures("Rethrottle"); - listener.onResponse(task.taskInfoGivenSubtaskInfo(localNodeId, r.getTasks())); - }, - listener::onFailure)); + logger.debug("rethrottling children of task [{}] to [{}] requests per second", task.getId(), subRequest.getRequestsPerSecond()); + client.execute(RethrottleAction.INSTANCE, subRequest, ActionListener.wrap(r -> { + r.rethrowFailures("Rethrottle"); + listener.onResponse(task.taskInfoGivenSubtaskInfo(localNodeId, r.getTasks())); + }, listener::onFailure)); } else { logger.debug("children of task [{}] are already finished, nothing to rethrottle", task.getId()); listener.onResponse(task.taskInfo(localNodeId, true)); } } - private static void rethrottleChildTask(Logger logger, String localNodeId, BulkByScrollTask task, float newRequestsPerSecond, - ActionListener listener) { + private static void rethrottleChildTask( + Logger logger, + String localNodeId, + BulkByScrollTask task, + float newRequestsPerSecond, + ActionListener listener + ) { logger.debug("rethrottling local task [{}] to [{}] requests per second", task.getId(), newRequestsPerSecond); task.getWorkerState().rethrottle(newRequestsPerSecond); listener.onResponse(task.taskInfo(localNodeId, true)); } @Override - protected ListTasksResponse newResponse(RethrottleRequest request, List tasks, - List taskOperationFailures, List failedNodeExceptions) { + protected ListTasksResponse newResponse( + RethrottleRequest request, + List tasks, + List taskOperationFailures, + List failedNodeExceptions + ) { return new ListTasksResponse(tasks, taskOperationFailures, failedNodeExceptions); } diff --git a/modules/reindex/src/main/java/org/elasticsearch/reindex/TransportUpdateByQueryAction.java b/modules/reindex/src/main/java/org/elasticsearch/reindex/TransportUpdateByQueryAction.java index 7e355fa47c39e..cd36d3b920a7b 100644 --- a/modules/reindex/src/main/java/org/elasticsearch/reindex/TransportUpdateByQueryAction.java +++ b/modules/reindex/src/main/java/org/elasticsearch/reindex/TransportUpdateByQueryAction.java @@ -45,10 +45,20 @@ public class TransportUpdateByQueryAction extends HandledTransportAction) UpdateByQueryRequest::new); + public TransportUpdateByQueryAction( + ThreadPool threadPool, + ActionFilters actionFilters, + Client client, + TransportService transportService, + ScriptService scriptService, + ClusterService clusterService + ) { + super( + UpdateByQueryAction.NAME, + transportService, + actionFilters, + (Writeable.Reader) UpdateByQueryRequest::new + ); this.threadPool = threadPool; this.client = client; this.scriptService = scriptService; @@ -58,14 +68,22 @@ public TransportUpdateByQueryAction(ThreadPool threadPool, ActionFilters actionF @Override protected void doExecute(Task task, UpdateByQueryRequest request, ActionListener listener) { BulkByScrollTask bulkByScrollTask = (BulkByScrollTask) task; - BulkByScrollParallelizationHelper.startSlicedAction(request, bulkByScrollTask, UpdateByQueryAction.INSTANCE, listener, client, + BulkByScrollParallelizationHelper.startSlicedAction( + request, + bulkByScrollTask, + UpdateByQueryAction.INSTANCE, + listener, + client, clusterService.localNode(), () -> { ClusterState state = clusterService.state(); - ParentTaskAssigningClient assigningClient = new ParentTaskAssigningClient(client, clusterService.localNode(), - bulkByScrollTask); - new AsyncIndexBySearchAction(bulkByScrollTask, logger, assigningClient, threadPool, scriptService, request, state, - listener).start(); + ParentTaskAssigningClient assigningClient = new ParentTaskAssigningClient( + client, + clusterService.localNode(), + bulkByScrollTask + ); + new AsyncIndexBySearchAction(bulkByScrollTask, logger, assigningClient, threadPool, scriptService, request, state, listener) + .start(); } ); } @@ -75,13 +93,29 @@ protected void doExecute(Task task, UpdateByQueryRequest request, ActionListener */ static class AsyncIndexBySearchAction extends AbstractAsyncBulkByScrollAction { - AsyncIndexBySearchAction(BulkByScrollTask task, Logger logger, ParentTaskAssigningClient client, - ThreadPool threadPool, ScriptService scriptService, UpdateByQueryRequest request, - ClusterState clusterState, ActionListener listener) { - super(task, + AsyncIndexBySearchAction( + BulkByScrollTask task, + Logger logger, + ParentTaskAssigningClient client, + ThreadPool threadPool, + ScriptService scriptService, + UpdateByQueryRequest request, + ClusterState clusterState, + ActionListener listener + ) { + super( + task, // use sequence number powered optimistic concurrency control - false, true, - logger, client, threadPool, request, listener, scriptService, null); + false, + true, + logger, + client, + threadPool, + request, + listener, + scriptService, + null + ); } @Override @@ -107,8 +141,12 @@ protected RequestWrapper buildRequest(ScrollableHitSource.Hit doc) class UpdateByQueryScriptApplier extends ScriptApplier { - UpdateByQueryScriptApplier(WorkerBulkByScrollTaskState taskWorker, ScriptService scriptService, Script script, - Map params) { + UpdateByQueryScriptApplier( + WorkerBulkByScrollTaskState taskWorker, + ScriptService scriptService, + Script script, + Map params + ) { super(taskWorker, scriptService, script, params); } diff --git a/modules/reindex/src/main/java/org/elasticsearch/reindex/remote/RemoteRequestBuilders.java b/modules/reindex/src/main/java/org/elasticsearch/reindex/remote/RemoteRequestBuilders.java index 9ef60325d5112..3b770aeff87d8 100644 --- a/modules/reindex/src/main/java/org/elasticsearch/reindex/remote/RemoteRequestBuilders.java +++ b/modules/reindex/src/main/java/org/elasticsearch/reindex/remote/RemoteRequestBuilders.java @@ -16,15 +16,15 @@ import org.elasticsearch.client.Request; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.core.TimeValue; +import org.elasticsearch.search.sort.FieldSortBuilder; +import org.elasticsearch.search.sort.SortBuilder; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.search.sort.FieldSortBuilder; -import org.elasticsearch.search.sort.SortBuilder; import java.io.IOException; import java.io.UnsupportedEncodingException; @@ -120,12 +120,14 @@ static Request initialSearch(SearchRequest searchRequest, BytesReference query, } // EMPTY is safe here because we're not calling namedObject - try (XContentBuilder entity = JsonXContent.contentBuilder(); - XContentParser queryParser = XContentHelper - .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, query)) { + try ( + XContentBuilder entity = JsonXContent.contentBuilder(); + XContentParser queryParser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, query) + ) { entity.startObject(); - entity.field("query"); { + entity.field("query"); + { /* We're intentionally a bit paranoid here - copying the query * as xcontent rather than writing a raw field. We don't want * poorly written queries to escape. Ever. */ @@ -133,7 +135,8 @@ static Request initialSearch(SearchRequest searchRequest, BytesReference query, XContentParser.Token shouldBeEof = queryParser.nextToken(); if (shouldBeEof != null) { throw new ElasticsearchException( - "query was more than a single object. This first token after the object is [" + shouldBeEof + "]"); + "query was more than a single object. This first token after the object is [" + shouldBeEof + "]" + ); } } @@ -197,9 +200,7 @@ static Request scroll(String scroll, TimeValue keepAlive, Version remoteVersion) } try (XContentBuilder entity = JsonXContent.contentBuilder()) { - entity.startObject() - .field("scroll_id", scroll) - .endObject(); + entity.startObject().field("scroll_id", scroll).endObject(); request.setJsonEntity(Strings.toString(entity)); } catch (IOException e) { throw new ElasticsearchException("failed to build scroll entity", e); @@ -216,9 +217,7 @@ static Request clearScroll(String scroll, Version remoteVersion) { return request; } try (XContentBuilder entity = JsonXContent.contentBuilder()) { - entity.startObject() - .array("scroll_id", scroll) - .endObject(); + entity.startObject().array("scroll_id", scroll).endObject(); request.setJsonEntity(Strings.toString(entity)); } catch (IOException e) { throw new ElasticsearchException("failed to build clear scroll entity", e); diff --git a/modules/reindex/src/main/java/org/elasticsearch/reindex/remote/RemoteResponseParsers.java b/modules/reindex/src/main/java/org/elasticsearch/reindex/remote/RemoteResponseParsers.java index 97ae04d8a922d..48694a52ba338 100644 --- a/modules/reindex/src/main/java/org/elasticsearch/reindex/remote/RemoteResponseParsers.java +++ b/modules/reindex/src/main/java/org/elasticsearch/reindex/remote/RemoteResponseParsers.java @@ -10,23 +10,23 @@ import org.apache.lucene.search.TotalHits; import org.elasticsearch.Version; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException; +import org.elasticsearch.core.Tuple; +import org.elasticsearch.index.reindex.ScrollableHitSource.BasicHit; +import org.elasticsearch.index.reindex.ScrollableHitSource.Hit; +import org.elasticsearch.index.reindex.ScrollableHitSource.Response; +import org.elasticsearch.index.reindex.ScrollableHitSource.SearchFailure; +import org.elasticsearch.search.SearchHits; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentLocation; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.index.reindex.ScrollableHitSource.BasicHit; -import org.elasticsearch.index.reindex.ScrollableHitSource.Hit; -import org.elasticsearch.index.reindex.ScrollableHitSource.Response; -import org.elasticsearch.index.reindex.ScrollableHitSource.SearchFailure; -import org.elasticsearch.search.SearchHits; import java.io.IOException; import java.util.List; @@ -47,14 +47,13 @@ private RemoteResponseParsers() {} /** * Parser for an individual {@code hit} element. */ - public static final ConstructingObjectParser HIT_PARSER = - new ConstructingObjectParser<>("hit", true, a -> { - int i = 0; - String index = (String) a[i++]; - String id = (String) a[i++]; - Long version = (Long) a[i++]; - return new BasicHit(index, id, version == null ? -1 : version); - }); + public static final ConstructingObjectParser HIT_PARSER = new ConstructingObjectParser<>("hit", true, a -> { + int i = 0; + String index = (String) a[i++]; + String id = (String) a[i++]; + Long version = (Long) a[i++]; + return new BasicHit(index, id, version == null ? -1 : version); + }); static { HIT_PARSER.declareString(constructorArg(), new ParseField("_index")); HIT_PARSER.declareString(constructorArg(), new ParseField("_id")); @@ -83,9 +82,7 @@ class Fields { String routing; } ObjectParser fieldsParser = new ObjectParser<>("fields", Fields::new); - HIT_PARSER.declareObject((hit, fields) -> { - hit.setRouting(fields.routing); - }, fieldsParser, new ParseField("fields")); + HIT_PARSER.declareObject((hit, fields) -> { hit.setRouting(fields.routing); }, fieldsParser, new ParseField("fields")); fieldsParser.declareString((fields, routing) -> fields.routing = routing, routingField); fieldsParser.declareLong((fields, ttl) -> {}, ttlField); // ignore ttls since they have been removed fieldsParser.declareString((fields, parent) -> {}, parentField); // ignore parents since they have been removed @@ -94,8 +91,7 @@ class Fields { /** * Parser for the {@code hits} element. Parsed to an array of {@code [total (Long), hits (List)]}. */ - public static final ConstructingObjectParser HITS_PARSER = - new ConstructingObjectParser<>("hits", true, a -> a); + public static final ConstructingObjectParser HITS_PARSER = new ConstructingObjectParser<>("hits", true, a -> a); static { HITS_PARSER.declareField(constructorArg(), (p, c) -> { if (p.currentToken() == XContentParser.Token.START_OBJECT) { @@ -113,22 +109,25 @@ class Fields { /** * Parser for {@code failed} shards in the {@code _shards} elements. */ - public static final ConstructingObjectParser SEARCH_FAILURE_PARSER = - new ConstructingObjectParser<>("failure", true, a -> { - int i = 0; - String index = (String) a[i++]; - Integer shardId = (Integer) a[i++]; - String nodeId = (String) a[i++]; - Object reason = a[i++]; + public static final ConstructingObjectParser SEARCH_FAILURE_PARSER = new ConstructingObjectParser<>( + "failure", + true, + a -> { + int i = 0; + String index = (String) a[i++]; + Integer shardId = (Integer) a[i++]; + String nodeId = (String) a[i++]; + Object reason = a[i++]; - Throwable reasonThrowable; - if (reason instanceof String) { - reasonThrowable = new RuntimeException("Unknown remote exception with reason=[" + (String) reason + "]"); - } else { - reasonThrowable = (Throwable) reason; - } - return new SearchFailure(reasonThrowable, index, shardId, nodeId); - }); + Throwable reasonThrowable; + if (reason instanceof String) { + reasonThrowable = new RuntimeException("Unknown remote exception with reason=[" + (String) reason + "]"); + } else { + reasonThrowable = (Throwable) reason; + } + return new SearchFailure(reasonThrowable, index, shardId, nodeId); + } + ); static { SEARCH_FAILURE_PARSER.declareStringOrNull(optionalConstructorArg(), new ParseField("index")); SEARCH_FAILURE_PARSER.declareInt(optionalConstructorArg(), new ParseField("shard")); @@ -146,44 +145,50 @@ class Fields { * Parser for the {@code _shards} element. Throws everything out except the errors array if there is one. If there isn't one then it * parses to an empty list. */ - public static final ConstructingObjectParser, Void> SHARDS_PARSER = - new ConstructingObjectParser<>("_shards", true, a -> { - @SuppressWarnings("unchecked") - List failures = (List) a[0]; - failures = failures == null ? emptyList() : failures; - return failures; - }); + public static final ConstructingObjectParser, Void> SHARDS_PARSER = new ConstructingObjectParser<>( + "_shards", + true, + a -> { + @SuppressWarnings("unchecked") + List failures = (List) a[0]; + failures = failures == null ? emptyList() : failures; + return failures; + } + ); static { SHARDS_PARSER.declareObjectArray(optionalConstructorArg(), SEARCH_FAILURE_PARSER, new ParseField("failures")); } - public static final ConstructingObjectParser RESPONSE_PARSER = - new ConstructingObjectParser<>("search_response", true, a -> { - int i = 0; - Throwable catastrophicFailure = (Throwable) a[i++]; - if (catastrophicFailure != null) { - return new Response(false, singletonList(new SearchFailure(catastrophicFailure)), 0, emptyList(), null); - } - boolean timedOut = (boolean) a[i++]; - String scroll = (String) a[i++]; - Object[] hitsElement = (Object[]) a[i++]; - @SuppressWarnings("unchecked") - List failures = (List) a[i++]; + public static final ConstructingObjectParser RESPONSE_PARSER = new ConstructingObjectParser<>( + "search_response", + true, + a -> { + int i = 0; + Throwable catastrophicFailure = (Throwable) a[i++]; + if (catastrophicFailure != null) { + return new Response(false, singletonList(new SearchFailure(catastrophicFailure)), 0, emptyList(), null); + } + boolean timedOut = (boolean) a[i++]; + String scroll = (String) a[i++]; + Object[] hitsElement = (Object[]) a[i++]; + @SuppressWarnings("unchecked") + List failures = (List) a[i++]; - long totalHits = 0; - List hits = emptyList(); + long totalHits = 0; + List hits = emptyList(); - // Pull apart the hits element if we got it - if (hitsElement != null) { - i = 0; - totalHits = (long) hitsElement[i++]; - @SuppressWarnings("unchecked") - List h = (List) hitsElement[i++]; - hits = h; - } + // Pull apart the hits element if we got it + if (hitsElement != null) { + i = 0; + totalHits = (long) hitsElement[i++]; + @SuppressWarnings("unchecked") + List h = (List) hitsElement[i++]; + hits = h; + } - return new Response(timedOut, failures, totalHits, hits, scroll); - }); + return new Response(timedOut, failures, totalHits, hits, scroll); + } + ); static { RESPONSE_PARSER.declareObject(optionalConstructorArg(), (p, c) -> ThrowableBuilder.PARSER.apply(p, null), new ParseField("error")); RESPONSE_PARSER.declareBoolean(optionalConstructorArg(), new ParseField("timed_out")); @@ -227,33 +232,37 @@ private Throwable buildWithoutCause() { requireNonNull(type, "[type] is required"); requireNonNull(reason, "[reason] is required"); switch (type) { - // Make some effort to use the right exceptions - case "es_rejected_execution_exception": - return new EsRejectedExecutionException(reason); - case "parsing_exception": - XContentLocation location = null; - if (line != null && column != null) { - location = new XContentLocation(line, column); - } - return new ParsingException(location, reason); - // But it isn't worth trying to get it perfect.... - default: - return new RuntimeException(type + ": " + reason); + // Make some effort to use the right exceptions + case "es_rejected_execution_exception": + return new EsRejectedExecutionException(reason); + case "parsing_exception": + XContentLocation location = null; + if (line != null && column != null) { + location = new XContentLocation(line, column); + } + return new ParsingException(location, reason); + // But it isn't worth trying to get it perfect.... + default: + return new RuntimeException(type + ": " + reason); } } public void setType(String type) { this.type = type; } + public void setReason(String reason) { this.reason = reason; } + public void setLine(Integer line) { this.line = line; } + public void setColumn(Integer column) { this.column = column; } + public void setCausedBy(Throwable causedBy) { this.causedBy = causedBy; } @@ -263,14 +272,16 @@ public void setCausedBy(Throwable causedBy) { * Parses the main action to return just the {@linkplain Version} that it returns. We throw everything else out. */ public static final ConstructingObjectParser MAIN_ACTION_PARSER = new ConstructingObjectParser<>( - "/", true, a -> (Version) a[0]); + "/", + true, + a -> (Version) a[0] + ); static { ConstructingObjectParser versionParser = new ConstructingObjectParser<>( - "version", true, a -> Version.fromString( - ((String) a[0]) - .replace("-SNAPSHOT", "") - .replaceFirst("-(alpha\\d+|beta\\d+|rc\\d+)", "") - )); + "version", + true, + a -> Version.fromString(((String) a[0]).replace("-SNAPSHOT", "").replaceFirst("-(alpha\\d+|beta\\d+|rc\\d+)", "")) + ); versionParser.declareString(constructorArg(), new ParseField("number")); MAIN_ACTION_PARSER.declareObject(constructorArg(), versionParser, new ParseField("version")); } diff --git a/modules/reindex/src/main/java/org/elasticsearch/reindex/remote/RemoteScrollableHitSource.java b/modules/reindex/src/main/java/org/elasticsearch/reindex/remote/RemoteScrollableHitSource.java index 19fc1bbd05fed..c6f6351098ae2 100644 --- a/modules/reindex/src/main/java/org/elasticsearch/reindex/remote/RemoteScrollableHitSource.java +++ b/modules/reindex/src/main/java/org/elasticsearch/reindex/remote/RemoteScrollableHitSource.java @@ -24,20 +24,20 @@ import org.elasticsearch.client.ResponseException; import org.elasticsearch.client.ResponseListener; import org.elasticsearch.client.RestClient; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParseException; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.reindex.RejectAwareActionListener; import org.elasticsearch.index.reindex.ScrollableHitSource; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentParseException; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; import java.io.IOException; import java.io.InputStream; @@ -55,9 +55,17 @@ public class RemoteScrollableHitSource extends ScrollableHitSource { private final SearchRequest searchRequest; Version remoteVersion; - public RemoteScrollableHitSource(Logger logger, BackoffPolicy backoffPolicy, ThreadPool threadPool, Runnable countSearchRetry, - Consumer onResponse, Consumer fail, - RestClient client, BytesReference query, SearchRequest searchRequest) { + public RemoteScrollableHitSource( + Logger logger, + BackoffPolicy backoffPolicy, + ThreadPool threadPool, + Runnable countSearchRetry, + Consumer onResponse, + Consumer fail, + RestClient client, + BytesReference query, + SearchRequest searchRequest + ) { super(logger, backoffPolicy, threadPool, countSearchRetry, onResponse, fail); this.query = query; this.searchRequest = searchRequest; @@ -68,8 +76,11 @@ public RemoteScrollableHitSource(Logger logger, BackoffPolicy backoffPolicy, Thr protected void doStart(RejectAwareActionListener searchListener) { lookupRemoteVersion(RejectAwareActionListener.withResponseHandler(searchListener, version -> { remoteVersion = version; - execute(RemoteRequestBuilders.initialSearch(searchRequest, query, remoteVersion), - RESPONSE_PARSER, RejectAwareActionListener.withResponseHandler(searchListener, r -> onStartResponse(searchListener, r))); + execute( + RemoteRequestBuilders.initialSearch(searchRequest, query, remoteVersion), + RESPONSE_PARSER, + RejectAwareActionListener.withResponseHandler(searchListener, r -> onStartResponse(searchListener, r)) + ); })); } @@ -110,11 +121,15 @@ public void onFailure(Exception e) { private void logFailure(Exception e) { if (e instanceof ResponseException) { ResponseException re = (ResponseException) e; - if (remoteVersion.before(Version.fromId(2000099)) - && re.getResponse().getStatusLine().getStatusCode() == 404) { - logger.debug((Supplier) () -> new ParameterizedMessage( + if (remoteVersion.before(Version.fromId(2000099)) && re.getResponse().getStatusLine().getStatusCode() == 404) { + logger.debug( + (Supplier) () -> new ParameterizedMessage( "Failed to clear scroll [{}] from pre-2.0 Elasticsearch. This is normal if the request terminated " - + "normally as the scroll has already been cleared automatically.", scrollId), e); + + "normally as the scroll has already been cleared automatically.", + scrollId + ), + e + ); return; } } @@ -140,8 +155,11 @@ protected void cleanup(Runnable onCompletion) { }); } - private void execute(Request request, - BiFunction parser, RejectAwareActionListener listener) { + private void execute( + Request request, + BiFunction parser, + RejectAwareActionListener listener + ) { // Preserve the thread context so headers survive after the call java.util.function.Supplier contextSupplier = threadPool.getThreadContext().newRestorableContext(true); try { @@ -163,7 +181,8 @@ public void onSuccess(org.elasticsearch.client.Response response) { if (xContentType == null) { try { throw new ElasticsearchException( - "Response didn't include Content-Type: " + bodyMessage(response.getEntity())); + "Response didn't include Content-Type: " + bodyMessage(response.getEntity()) + ); } catch (IOException e) { ElasticsearchException ee = new ElasticsearchException("Error extracting body from response"); ee.addSuppressed(e); @@ -171,18 +190,24 @@ public void onSuccess(org.elasticsearch.client.Response response) { } } // EMPTY is safe here because we don't call namedObject - try (XContentParser xContentParser = xContentType.xContent().createParser(NamedXContentRegistry.EMPTY, - LoggingDeprecationHandler.INSTANCE, content)) { + try ( + XContentParser xContentParser = xContentType.xContent() + .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, content) + ) { parsedResponse = parser.apply(xContentParser, xContentType); } catch (XContentParseException e) { /* Because we're streaming the response we can't get a copy of it here. The best we can do is hint that it * is totally wrong and we're probably not talking to Elasticsearch. */ throw new ElasticsearchException( - "Error parsing the response, remote is likely not an Elasticsearch instance", e); + "Error parsing the response, remote is likely not an Elasticsearch instance", + e + ); } } catch (IOException e) { throw new ElasticsearchException( - "Error deserializing response, remote is likely not an Elasticsearch instance", e); + "Error deserializing response, remote is likely not an Elasticsearch instance", + e + ); } listener.onResponse(parsedResponse); } @@ -195,15 +220,16 @@ public void onFailure(Exception e) { if (e instanceof ResponseException) { ResponseException re = (ResponseException) e; int statusCode = re.getResponse().getStatusLine().getStatusCode(); - e = wrapExceptionToPreserveStatus(statusCode, - re.getResponse().getEntity(), re); + e = wrapExceptionToPreserveStatus(statusCode, re.getResponse().getEntity(), re); if (RestStatus.TOO_MANY_REQUESTS.getStatus() == statusCode) { listener.onRejection(e); return; } } else if (e instanceof ContentTooLongException) { e = new IllegalArgumentException( - "Remote responded with a chunk that was too large. Use a smaller batch size.", e); + "Remote responded with a chunk that was too large. Use a smaller batch size.", + e + ); } listener.onFailure(e); } diff --git a/modules/reindex/src/test/java/org/elasticsearch/reindex/AbstractAsyncBulkByScrollActionMetadataTestCase.java b/modules/reindex/src/test/java/org/elasticsearch/reindex/AbstractAsyncBulkByScrollActionMetadataTestCase.java index e3089c6a9edb3..5db3e434b54e6 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/reindex/AbstractAsyncBulkByScrollActionMetadataTestCase.java +++ b/modules/reindex/src/test/java/org/elasticsearch/reindex/AbstractAsyncBulkByScrollActionMetadataTestCase.java @@ -12,12 +12,10 @@ import org.elasticsearch.index.reindex.AbstractBulkByScrollRequest; import org.elasticsearch.index.reindex.BulkByScrollResponse; import org.elasticsearch.index.reindex.ScrollableHitSource; -import org.elasticsearch.reindex.AbstractAsyncBulkByScrollAction; public abstract class AbstractAsyncBulkByScrollActionMetadataTestCase< - Request extends AbstractBulkByScrollRequest, - Response extends BulkByScrollResponse> - extends AbstractAsyncBulkByScrollActionTestCase { + Request extends AbstractBulkByScrollRequest, + Response extends BulkByScrollResponse> extends AbstractAsyncBulkByScrollActionTestCase { protected ScrollableHitSource.BasicHit doc() { return new ScrollableHitSource.BasicHit("index", "id", 0); diff --git a/modules/reindex/src/test/java/org/elasticsearch/reindex/AbstractAsyncBulkByScrollActionScriptTestCase.java b/modules/reindex/src/test/java/org/elasticsearch/reindex/AbstractAsyncBulkByScrollActionScriptTestCase.java index 728474b9642ee..5687754cc1ad6 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/reindex/AbstractAsyncBulkByScrollActionScriptTestCase.java +++ b/modules/reindex/src/test/java/org/elasticsearch/reindex/AbstractAsyncBulkByScrollActionScriptTestCase.java @@ -15,7 +15,6 @@ import org.elasticsearch.index.reindex.AbstractBulkIndexByScrollRequest; import org.elasticsearch.index.reindex.BulkByScrollResponse; import org.elasticsearch.index.reindex.ScrollableHitSource; -import org.elasticsearch.reindex.AbstractAsyncBulkByScrollAction; import org.elasticsearch.reindex.AbstractAsyncBulkByScrollAction.OpType; import org.elasticsearch.reindex.AbstractAsyncBulkByScrollAction.RequestWrapper; import org.elasticsearch.script.ScriptService; @@ -34,9 +33,8 @@ import static org.mockito.Mockito.when; public abstract class AbstractAsyncBulkByScrollActionScriptTestCase< - Request extends AbstractBulkIndexByScrollRequest, - Response extends BulkByScrollResponse> - extends AbstractAsyncBulkByScrollActionTestCase { + Request extends AbstractBulkIndexByScrollRequest, + Response extends BulkByScrollResponse> extends AbstractAsyncBulkByScrollActionTestCase { protected ScriptService scriptService; @@ -86,8 +84,10 @@ public void testSetOpTypeDelete() throws Exception { } public void testSetOpTypeUnknown() throws Exception { - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> applyScript((Map ctx) -> ctx.put("op", "unknown"))); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> applyScript((Map ctx) -> ctx.put("op", "unknown")) + ); assertThat(e.getMessage(), equalTo("Operation type [unknown] not allowed, only [noop, index, delete] are allowed")); } diff --git a/modules/reindex/src/test/java/org/elasticsearch/reindex/AsyncBulkByScrollActionTests.java b/modules/reindex/src/test/java/org/elasticsearch/reindex/AsyncBulkByScrollActionTests.java index c3df1ca343c94..aeba06c624b21 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/reindex/AsyncBulkByScrollActionTests.java +++ b/modules/reindex/src/test/java/org/elasticsearch/reindex/AsyncBulkByScrollActionTests.java @@ -46,13 +46,12 @@ import org.elasticsearch.client.FilterClient; import org.elasticsearch.client.ParentTaskAssigningClient; import org.elasticsearch.cluster.node.DiscoveryNode; -import org.elasticsearch.core.CheckedConsumer; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.AbstractRunnable; import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException; -import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.core.CheckedConsumer; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.Index; import org.elasticsearch.index.engine.VersionConflictEngineException; import org.elasticsearch.index.reindex.AbstractBulkByScrollRequest; @@ -75,6 +74,7 @@ import org.elasticsearch.test.client.NoOpClient; import org.elasticsearch.threadpool.TestThreadPool; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.XContentType; import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -206,10 +206,16 @@ public void testStartNextScrollRetriesOnRejectionAndSucceeds() throws Exception // this test primarily tests ClientScrollableHitSource but left it to test integration to status client.scrollsToReject = randomIntBetween(0, testRequest.getMaxRetries() - 1); // use fail() onResponse handler because mocked search never fires on listener. - ClientScrollableHitSource hitSource = new ClientScrollableHitSource(logger, buildTestBackoffPolicy(), + ClientScrollableHitSource hitSource = new ClientScrollableHitSource( + logger, + buildTestBackoffPolicy(), threadPool, - testTask.getWorkerState()::countSearchRetry, r -> fail(), ExceptionsHelper::reThrowIfNotNull, - new ParentTaskAssigningClient(client, localNode, testTask), testRequest.getSearchRequest()); + testTask.getWorkerState()::countSearchRetry, + r -> fail(), + ExceptionsHelper::reThrowIfNotNull, + new ParentTaskAssigningClient(client, localNode, testTask), + testRequest.getSearchRequest() + ); hitSource.setScroll(scrollId()); hitSource.startNextScroll(TimeValue.timeValueSeconds(0)); assertBusy(() -> assertEquals(client.scrollsToReject + 1, client.scrollAttempts.get())); @@ -224,21 +230,25 @@ public void testStartNextScrollRetriesOnRejectionAndSucceeds() throws Exception public void testStartNextScrollRetriesOnRejectionButFailsOnTooManyRejections() throws Exception { // this test primarily tests ClientScrollableHitSource but left it to test integration to status client.scrollsToReject = testRequest.getMaxRetries() + randomIntBetween(1, 100); - assertExactlyOnce( - onFail -> { - Consumer validingOnFail = e -> { - assertNotNull(ExceptionsHelper.unwrap(e, EsRejectedExecutionException.class)); - onFail.run(); - }; - ClientScrollableHitSource hitSource = new ClientScrollableHitSource(logger, buildTestBackoffPolicy(), - threadPool, - testTask.getWorkerState()::countSearchRetry, r -> fail(), validingOnFail, - new ParentTaskAssigningClient(client, localNode, testTask), testRequest.getSearchRequest()); - hitSource.setScroll(scrollId()); - hitSource.startNextScroll(TimeValue.timeValueSeconds(0)); - assertBusy(() -> assertEquals(testRequest.getMaxRetries() + 1, client.scrollAttempts.get())); - } - ); + assertExactlyOnce(onFail -> { + Consumer validingOnFail = e -> { + assertNotNull(ExceptionsHelper.unwrap(e, EsRejectedExecutionException.class)); + onFail.run(); + }; + ClientScrollableHitSource hitSource = new ClientScrollableHitSource( + logger, + buildTestBackoffPolicy(), + threadPool, + testTask.getWorkerState()::countSearchRetry, + r -> fail(), + validingOnFail, + new ParentTaskAssigningClient(client, localNode, testTask), + testRequest.getSearchRequest() + ); + hitSource.setScroll(scrollId()); + hitSource.startNextScroll(TimeValue.timeValueSeconds(0)); + assertBusy(() -> assertEquals(testRequest.getMaxRetries() + 1, client.scrollAttempts.get())); + }); assertNull("There shouldn't be a scroll attempt pending that we didn't reject", client.lastScroll.get()); assertEquals(testRequest.getMaxRetries(), testTask.getStatus().getSearchRetries()); } @@ -283,42 +293,40 @@ public void testBulkResponseSetsLotsOfStatus() throws Exception { ShardId shardId = new ShardId(new Index("name", "uid"), 0); if (rarely()) { versionConflicts++; - responses[i] = BulkItemResponse.failure(i, randomFrom(DocWriteRequest.OpType.values()), - new Failure(shardId.getIndexName(), "id" + i, - new VersionConflictEngineException(shardId, "id", "test"))); + responses[i] = BulkItemResponse.failure( + i, + randomFrom(DocWriteRequest.OpType.values()), + new Failure(shardId.getIndexName(), "id" + i, new VersionConflictEngineException(shardId, "id", "test")) + ); continue; } boolean createdResponse; DocWriteRequest.OpType opType; switch (randomIntBetween(0, 2)) { - case 0: - createdResponse = true; - opType = DocWriteRequest.OpType.CREATE; - created++; - break; - case 1: - createdResponse = false; - opType = randomFrom(DocWriteRequest.OpType.INDEX, DocWriteRequest.OpType.UPDATE); - updated++; - break; - case 2: - createdResponse = false; - opType = DocWriteRequest.OpType.DELETE; - deleted++; - break; - default: - throw new RuntimeException("Bad scenario"); + case 0: + createdResponse = true; + opType = DocWriteRequest.OpType.CREATE; + created++; + break; + case 1: + createdResponse = false; + opType = randomFrom(DocWriteRequest.OpType.INDEX, DocWriteRequest.OpType.UPDATE); + updated++; + break; + case 2: + createdResponse = false; + opType = DocWriteRequest.OpType.DELETE; + deleted++; + break; + default: + throw new RuntimeException("Bad scenario"); } final int seqNo = randomInt(20); final int primaryTerm = randomIntBetween(1, 16); - final IndexResponse response = - new IndexResponse(shardId, "id" + i, seqNo, primaryTerm, randomInt(), createdResponse); + final IndexResponse response = new IndexResponse(shardId, "id" + i, seqNo, primaryTerm, randomInt(), createdResponse); responses[i] = BulkItemResponse.success(i, opType, response); } - assertExactlyOnce(onSuccess -> - new DummyAsyncBulkByScrollAction().onBulkResponse(new BulkResponse(responses, 0), - onSuccess) - ); + assertExactlyOnce(onSuccess -> new DummyAsyncBulkByScrollAction().onBulkResponse(new BulkResponse(responses, 0), onSuccess)); assertEquals(versionConflicts, testTask.getStatus().getVersionConflicts()); assertEquals(updated, testTask.getStatus().getUpdated()); assertEquals(created, testTask.getStatus().getCreated()); @@ -364,8 +372,13 @@ public ScheduledCancellable schedule(Runnable command, TimeValue delay, String n */ public void testShardFailuresAbortRequest() throws Exception { SearchFailure shardFailure = new SearchFailure(new RuntimeException("test")); - ScrollableHitSource.Response scrollResponse = new ScrollableHitSource.Response(false, singletonList(shardFailure), 0, - emptyList(), null); + ScrollableHitSource.Response scrollResponse = new ScrollableHitSource.Response( + false, + singletonList(shardFailure), + 0, + emptyList(), + null + ); simulateScrollResponse(new DummyAsyncBulkByScrollAction(), System.nanoTime(), 0, scrollResponse); BulkByScrollResponse response = listener.get(); assertThat(response.getBulkFailures(), empty()); @@ -460,6 +473,7 @@ public ScheduledCancellable schedule(Runnable command, TimeValue delay, String n capturedCommand.set(command); return new ScheduledCancellable() { private boolean cancelled = false; + @Override public long getDelay(TimeUnit unit) { return unit.convert(delay.millis(), TimeUnit.MILLISECONDS); @@ -501,11 +515,22 @@ protected RequestWrapper buildRequest(Hit doc) { // create a simulated response. SearchHit hit = new SearchHit(0, "id", emptyMap(), emptyMap()).sourceRef(new BytesArray("{}")); - SearchHits hits = new SearchHits(IntStream.range(0, 100).mapToObj(i -> hit).toArray(SearchHit[]::new), - new TotalHits(0, TotalHits.Relation.EQUAL_TO),0); + SearchHits hits = new SearchHits( + IntStream.range(0, 100).mapToObj(i -> hit).toArray(SearchHit[]::new), + new TotalHits(0, TotalHits.Relation.EQUAL_TO), + 0 + ); InternalSearchResponse internalResponse = new InternalSearchResponse(hits, null, null, null, false, false, 1); - SearchResponse searchResponse = new SearchResponse(internalResponse, scrollId(), 5, 4, 0, randomLong(), null, - SearchResponse.Clusters.EMPTY); + SearchResponse searchResponse = new SearchResponse( + internalResponse, + scrollId(), + 5, + 4, + 0, + randomLong(), + null, + SearchResponse.Clusters.EMPTY + ); client.lastSearch.get().listener.onResponse(searchResponse); @@ -555,9 +580,7 @@ private void bulkRetryTestCase(boolean failWithRejection) throws Exception { assertThat(response.getSearchFailures(), empty()); assertNull(response.getReasonCancelled()); } else { - assertExactlyOnce(onSuccess -> - action.sendBulkRequest(request, onSuccess) - ); + assertExactlyOnce(onSuccess -> action.sendBulkRequest(request, onSuccess)); } } @@ -604,7 +627,7 @@ private void refreshTestCase(Boolean refresh, boolean addDestinationIndexes, boo } action.refreshAndFinish(emptyList(), emptyList(), false); if (shouldRefresh) { - assertArrayEquals(new String[] {"foo"}, client.lastRefreshRequest.get().indices()); + assertArrayEquals(new String[] { "foo" }, client.lastRefreshRequest.get().indices()); } else { assertNull("No refresh was attempted", client.lastRefreshRequest.get()); } @@ -615,18 +638,24 @@ public void testCancelBeforeInitialSearch() throws Exception { } public void testCancelBeforeScrollResponse() throws Exception { - cancelTaskCase((DummyAsyncBulkByScrollAction action) -> simulateScrollResponse(action, System.nanoTime(), 1, - new ScrollableHitSource.Response(false, emptyList(), between(1, 100000), emptyList(), null))); + cancelTaskCase( + (DummyAsyncBulkByScrollAction action) -> simulateScrollResponse( + action, + System.nanoTime(), + 1, + new ScrollableHitSource.Response(false, emptyList(), between(1, 100000), emptyList(), null) + ) + ); } public void testCancelBeforeSendBulkRequest() throws Exception { - cancelTaskCase((DummyAsyncBulkByScrollAction action) -> - action.sendBulkRequest(new BulkRequest(), Assert::fail)); + cancelTaskCase((DummyAsyncBulkByScrollAction action) -> action.sendBulkRequest(new BulkRequest(), Assert::fail)); } public void testCancelBeforeOnBulkResponse() throws Exception { - cancelTaskCase((DummyAsyncBulkByScrollAction action) -> - action.onBulkResponse(new BulkResponse(new BulkItemResponse[0], 0), Assert::fail)); + cancelTaskCase( + (DummyAsyncBulkByScrollAction action) -> action.onBulkResponse(new BulkResponse(new BulkItemResponse[0], 0), Assert::fail) + ); } public void testCancelBeforeStartNextScroll() throws Exception { @@ -711,8 +740,13 @@ public void testScrollConsumableHitsResponseCanBeConsumedInChunks() { for (int i = 0; i < numberOfHits; i++) { hits.add(new ScrollableHitSource.BasicHit("idx", "id-" + i, -1)); } - final ScrollableHitSource.Response scrollResponse = - new ScrollableHitSource.Response(false, emptyList(), hits.size(), hits, "scrollid"); + final ScrollableHitSource.Response scrollResponse = new ScrollableHitSource.Response( + false, + emptyList(), + hits.size(), + hits, + "scrollid" + ); final AbstractAsyncBulkByScrollAction.ScrollConsumableHitsResponse response = new AbstractAsyncBulkByScrollAction.ScrollConsumableHitsResponse(new ScrollableHitSource.AsyncResponse() { @Override @@ -721,8 +755,7 @@ public ScrollableHitSource.Response response() { } @Override - public void done(TimeValue extraKeepAlive) { - } + public void done(TimeValue extraKeepAlive) {} }); assertThat(response.remainingHits(), equalTo(numberOfHits)); @@ -757,8 +790,13 @@ public void testScrollConsumableHitsResponseErrorHandling() { hits.add(new ScrollableHitSource.BasicHit("idx", "id-" + i, -1)); } - final ScrollableHitSource.Response scrollResponse = - new ScrollableHitSource.Response(false, emptyList(), hits.size(), hits, "scrollid"); + final ScrollableHitSource.Response scrollResponse = new ScrollableHitSource.Response( + false, + emptyList(), + hits.size(), + hits, + "scrollid" + ); final AbstractAsyncBulkByScrollAction.ScrollConsumableHitsResponse response = new AbstractAsyncBulkByScrollAction.ScrollConsumableHitsResponse(new ScrollableHitSource.AsyncResponse() { @Override @@ -767,8 +805,7 @@ public ScrollableHitSource.Response response() { } @Override - public void done(TimeValue extraKeepAlive) { - } + public void done(TimeValue extraKeepAlive) {} }); assertThat(response.remainingHits(), equalTo(numberOfHits)); @@ -792,30 +829,46 @@ public void done(TimeValue extraKeepAlive) { /** * Simulate a scroll response by setting the scroll id and firing the onScrollResponse method. */ - private void simulateScrollResponse(DummyAsyncBulkByScrollAction action, long lastBatchTime, int lastBatchSize, - ScrollableHitSource.Response response) { + private void simulateScrollResponse( + DummyAsyncBulkByScrollAction action, + long lastBatchTime, + int lastBatchSize, + ScrollableHitSource.Response response + ) { action.setScroll(scrollId()); - action.onScrollResponse(lastBatchTime, lastBatchSize, - new AbstractAsyncBulkByScrollAction.ScrollConsumableHitsResponse( - new ScrollableHitSource.AsyncResponse() { - @Override - public ScrollableHitSource.Response response() { - return response; - } + action.onScrollResponse( + lastBatchTime, + lastBatchSize, + new AbstractAsyncBulkByScrollAction.ScrollConsumableHitsResponse(new ScrollableHitSource.AsyncResponse() { + @Override + public ScrollableHitSource.Response response() { + return response; + } - @Override - public void done(TimeValue extraKeepAlive) { - fail(); - } - }) + @Override + public void done(TimeValue extraKeepAlive) { + fail(); + } + }) ); } - private class DummyAsyncBulkByScrollAction - extends AbstractAsyncBulkByScrollAction { + private class DummyAsyncBulkByScrollAction extends AbstractAsyncBulkByScrollAction< + DummyAbstractBulkByScrollRequest, + DummyTransportAsyncBulkByScrollAction> { DummyAsyncBulkByScrollAction() { - super(testTask, randomBoolean(), randomBoolean(), AsyncBulkByScrollActionTests.this.logger, - new ParentTaskAssigningClient(client, localNode, testTask), client.threadPool(), testRequest, listener, null, null); + super( + testTask, + randomBoolean(), + randomBoolean(), + AsyncBulkByScrollActionTests.this.logger, + new ParentTaskAssigningClient(client, localNode, testTask), + client.threadPool(), + testRequest, + listener, + null, + null + ); } @Override @@ -839,9 +892,9 @@ private BackoffPolicy buildTestBackoffPolicy() { return constantBackoff(timeValueMillis(0), testRequest.getMaxRetries()); } - private static class DummyTransportAsyncBulkByScrollAction - extends TransportAction { - + private static class DummyTransportAsyncBulkByScrollAction extends TransportAction< + DummyAbstractBulkByScrollRequest, + BulkByScrollResponse> { protected DummyTransportAsyncBulkByScrollAction(String actionName, ActionFilters actionFilters, TaskManager taskManager) { super(actionName, actionFilters, taskManager); @@ -898,11 +951,15 @@ private class MyMockClient extends FilterClient { @Override @SuppressWarnings("unchecked") - protected - void doExecute(ActionType action, Request request, ActionListener listener) { + protected void doExecute( + ActionType action, + Request request, + ActionListener listener + ) { if (false == expectedHeaders.equals(threadPool().getThreadContext().getHeaders())) { listener.onFailure( - new RuntimeException("Expected " + expectedHeaders + " but got " + threadPool().getThreadContext().getHeaders())); + new RuntimeException("Expected " + expectedHeaders + " but got " + threadPool().getThreadContext().getHeaders()) + ); return; } @@ -956,34 +1013,43 @@ void doExecute(ActionType action, Request request, ActionListener randomSearchSourceBuilder( - () -> null, - () -> null, - () -> null, - () -> emptyList(), - () -> null, - () -> null)); + SearchRequest searchRequest = randomSearchRequest( + () -> randomSearchSourceBuilder(() -> null, () -> null, () -> null, () -> emptyList(), () -> null, () -> null) + ); if (searchRequest.source() != null) { // Clear the slice builder if there is one set. We can't call sliceIntoSubRequests if it is. searchRequest.source().slice(null); diff --git a/modules/reindex/src/test/java/org/elasticsearch/reindex/BulkIndexByScrollResponseMatcher.java b/modules/reindex/src/test/java/org/elasticsearch/reindex/BulkIndexByScrollResponseMatcher.java index b3fa72c8da78f..aa06039ff45ad 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/reindex/BulkIndexByScrollResponseMatcher.java +++ b/modules/reindex/src/test/java/org/elasticsearch/reindex/BulkIndexByScrollResponseMatcher.java @@ -121,14 +121,14 @@ public BulkIndexByScrollResponseMatcher slices(Matcher bulkFailures = frequently() ? emptyList() - : IntStream.range(0, between(1, 3)).mapToObj(j -> new BulkItemResponse.Failure("idx", "id", new Exception())) - .collect(Collectors.toList()); + BulkByScrollTask.Status status = new BulkByScrollTask.Status( + i, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + timeValueMillis(0), + 0f, + thisReasonCancelled, + timeValueMillis(0) + ); + List bulkFailures = frequently() + ? emptyList() + : IntStream.range(0, between(1, 3)) + .mapToObj(j -> new BulkItemResponse.Failure("idx", "id", new Exception())) + .collect(Collectors.toList()); allBulkFailures.addAll(bulkFailures); - List searchFailures = frequently() ? emptyList() - : IntStream.range(0, between(1, 3)).mapToObj(j -> new SearchFailure(new Exception())).collect(Collectors.toList()); + List searchFailures = frequently() + ? emptyList() + : IntStream.range(0, between(1, 3)).mapToObj(j -> new SearchFailure(new Exception())).collect(Collectors.toList()); allSearchFailures.addAll(searchFailures); boolean thisTimedOut = rarely(); timedOut |= thisTimedOut; diff --git a/modules/reindex/src/test/java/org/elasticsearch/reindex/CancelTests.java b/modules/reindex/src/test/java/org/elasticsearch/reindex/CancelTests.java index 1c8a2c3510ee8..be62bc0205b39 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/reindex/CancelTests.java +++ b/modules/reindex/src/test/java/org/elasticsearch/reindex/CancelTests.java @@ -17,7 +17,6 @@ import org.elasticsearch.action.ingest.DeletePipelineRequest; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.IndexModule; import org.elasticsearch.index.engine.Engine; import org.elasticsearch.index.engine.Engine.Operation.Origin; @@ -34,6 +33,7 @@ import org.elasticsearch.plugins.Plugin; import org.elasticsearch.tasks.TaskCancelledException; import org.elasticsearch.tasks.TaskInfo; +import org.elasticsearch.xcontent.XContentType; import org.hamcrest.Matcher; import org.junit.Before; @@ -79,8 +79,12 @@ public void clearAllowedOperations() { /** * Executes the cancellation test */ - private void testCancel(String action, AbstractBulkByScrollRequestBuilder builder, CancelAssertion assertion, - Matcher taskDescriptionMatcher) throws Exception { + private void testCancel( + String action, + AbstractBulkByScrollRequestBuilder builder, + CancelAssertion assertion, + Matcher taskDescriptionMatcher + ) throws Exception { createIndex(INDEX); // Total number of documents created for this test (~10 per primary shard per slice) @@ -88,9 +92,14 @@ private void testCancel(String action, AbstractBulkByScrollRequestBuilder ALLOWED_OPERATIONS.release(numDocs); logger.debug("setting up [{}] docs", numDocs); - indexRandom(true, false, true, IntStream.range(0, numDocs) + indexRandom( + true, + false, + true, + IntStream.range(0, numDocs) .mapToObj(i -> client().prepareIndex().setIndex(INDEX).setId(String.valueOf(i)).setSource("n", i)) - .collect(Collectors.toList())); + .collect(Collectors.toList()) + ); // Checks that the all documents have been indexed and correctly counted assertHitCount(client().prepareSearch(INDEX).setSize(0).get(), numDocs); @@ -116,7 +125,9 @@ private void testCancel(String action, AbstractBulkByScrollRequestBuilder logger.debug("waiting for updates to be blocked"); assertBusy( () -> assertTrue("updates blocked", ALLOWED_OPERATIONS.hasQueuedThreads() && ALLOWED_OPERATIONS.availablePermits() == 0), - 1, TimeUnit.MINUTES); // 10 seconds is usually fine but on heavily loaded machines this can take a while + 1, + TimeUnit.MINUTES + ); // 10 seconds is usually fine but on heavily loaded machines this can take a while // Status should show the task running TaskInfo mainTask = findTaskToCancel(action, builder.request().getSlices()); @@ -141,11 +152,15 @@ private void testCancel(String action, AbstractBulkByScrollRequestBuilder if (builder.request().getSlices() > 1) { boolean foundCancelled = false; - ListTasksResponse sliceList = client().admin().cluster().prepareListTasks().setParentTaskId(mainTask.getTaskId()) - .setDetailed(true).get(); + ListTasksResponse sliceList = client().admin() + .cluster() + .prepareListTasks() + .setParentTaskId(mainTask.getTaskId()) + .setDetailed(true) + .get(); sliceList.rethrowFailures("Fetch slice tasks"); logger.debug("finding at least one canceled child among {}", sliceList.getTasks()); - for (TaskInfo slice: sliceList.getTasks()) { + for (TaskInfo slice : sliceList.getTasks()) { BulkByScrollTask.Status sliceStatus = (BulkByScrollTask.Status) slice.getStatus(); if (sliceStatus.getReasonCancelled() == null) continue; assertEquals(CancelTasksRequest.DEFAULT_REASON, sliceStatus.getReasonCancelled()); @@ -175,8 +190,13 @@ private void testCancel(String action, AbstractBulkByScrollRequestBuilder if (ExceptionsHelper.unwrapCausesAndSuppressed(e, t -> t instanceof TaskCancelledException).isPresent()) { return; // the scroll request was cancelled } - String tasks = client().admin().cluster().prepareListTasks().setParentTaskId(mainTask.getTaskId()) - .setDetailed(true).get().toString(); + String tasks = client().admin() + .cluster() + .prepareListTasks() + .setParentTaskId(mainTask.getTaskId()) + .setDetailed(true) + .get() + .toString(); throw new RuntimeException("Exception while waiting for the response. Running tasks: " + tasks, e); } finally { if (builder.request().getSlices() >= 1) { @@ -218,12 +238,14 @@ public void testReindexCancel() throws Exception { } public void testUpdateByQueryCancel() throws Exception { - BytesReference pipeline = new BytesArray("{\n" + - " \"description\" : \"sets processed to true\",\n" + - " \"processors\" : [ {\n" + - " \"test\" : {}\n" + - " } ]\n" + - "}"); + BytesReference pipeline = new BytesArray( + "{\n" + + " \"description\" : \"sets processed to true\",\n" + + " \"processors\" : [ {\n" + + " \"test\" : {}\n" + + " } ]\n" + + "}" + ); assertAcked(client().admin().cluster().preparePutPipeline("set-processed", pipeline, XContentType.JSON).get()); testCancel(UpdateByQueryAction.NAME, updateByQuery().setPipeline("set-processed").source(INDEX), (response, total, modified) -> { @@ -235,48 +257,64 @@ public void testUpdateByQueryCancel() throws Exception { } public void testDeleteByQueryCancel() throws Exception { - testCancel(DeleteByQueryAction.NAME, deleteByQuery().source(INDEX).filter(QueryBuilders.matchAllQuery()), + testCancel( + DeleteByQueryAction.NAME, + deleteByQuery().source(INDEX).filter(QueryBuilders.matchAllQuery()), (response, total, modified) -> { assertThat(response, matcher().deleted(modified).reasonCancelled(equalTo("by user request"))); assertHitCount(client().prepareSearch(INDEX).setSize(0).get(), total - modified); - }, equalTo("delete-by-query [" + INDEX + "]")); + }, + equalTo("delete-by-query [" + INDEX + "]") + ); } public void testReindexCancelWithWorkers() throws Exception { - testCancel(ReindexAction.NAME, - reindex().source(INDEX).filter(QueryBuilders.matchAllQuery()).destination("dest").setSlices(5), - (response, total, modified) -> { - assertThat(response, matcher().created(modified).reasonCancelled(equalTo("by user request")).slices(hasSize(5))); - refresh("dest"); - assertHitCount(client().prepareSearch("dest").setSize(0).get(), modified); - }, - equalTo("reindex from [" + INDEX + "] to [dest]")); + testCancel( + ReindexAction.NAME, + reindex().source(INDEX).filter(QueryBuilders.matchAllQuery()).destination("dest").setSlices(5), + (response, total, modified) -> { + assertThat(response, matcher().created(modified).reasonCancelled(equalTo("by user request")).slices(hasSize(5))); + refresh("dest"); + assertHitCount(client().prepareSearch("dest").setSize(0).get(), modified); + }, + equalTo("reindex from [" + INDEX + "] to [dest]") + ); } public void testUpdateByQueryCancelWithWorkers() throws Exception { - BytesReference pipeline = new BytesArray("{\n" + - " \"description\" : \"sets processed to true\",\n" + - " \"processors\" : [ {\n" + - " \"test\" : {}\n" + - " } ]\n" + - "}"); + BytesReference pipeline = new BytesArray( + "{\n" + + " \"description\" : \"sets processed to true\",\n" + + " \"processors\" : [ {\n" + + " \"test\" : {}\n" + + " } ]\n" + + "}" + ); assertAcked(client().admin().cluster().preparePutPipeline("set-processed", pipeline, XContentType.JSON).get()); - testCancel(UpdateByQueryAction.NAME, updateByQuery().setPipeline("set-processed").source(INDEX).setSlices(5), - (response, total, modified) -> { - assertThat(response, matcher().updated(modified).reasonCancelled(equalTo("by user request")).slices(hasSize(5))); - assertHitCount(client().prepareSearch(INDEX).setSize(0).setQuery(termQuery("processed", true)).get(), modified); - }, equalTo("update-by-query [" + INDEX + "]")); + testCancel( + UpdateByQueryAction.NAME, + updateByQuery().setPipeline("set-processed").source(INDEX).setSlices(5), + (response, total, modified) -> { + assertThat(response, matcher().updated(modified).reasonCancelled(equalTo("by user request")).slices(hasSize(5))); + assertHitCount(client().prepareSearch(INDEX).setSize(0).setQuery(termQuery("processed", true)).get(), modified); + }, + equalTo("update-by-query [" + INDEX + "]") + ); assertAcked(client().admin().cluster().deletePipeline(new DeletePipelineRequest("set-processed")).get()); } public void testDeleteByQueryCancelWithWorkers() throws Exception { - testCancel(DeleteByQueryAction.NAME, deleteByQuery().source(INDEX).filter(QueryBuilders.matchAllQuery()).setSlices(5), + testCancel( + DeleteByQueryAction.NAME, + deleteByQuery().source(INDEX).filter(QueryBuilders.matchAllQuery()).setSlices(5), (response, total, modified) -> { assertThat(response, matcher().deleted(modified).reasonCancelled(equalTo("by user request")).slices(hasSize(5))); assertHitCount(client().prepareSearch(INDEX).setSize(0).get(), total - modified); - }, equalTo("delete-by-query [" + INDEX + "]")); + }, + equalTo("delete-by-query [" + INDEX + "]") + ); } /** diff --git a/modules/reindex/src/test/java/org/elasticsearch/reindex/ClientScrollableHitSourceTests.java b/modules/reindex/src/test/java/org/elasticsearch/reindex/ClientScrollableHitSourceTests.java index a641bfe55354d..1a5afecedb450 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/reindex/ClientScrollableHitSourceTests.java +++ b/modules/reindex/src/test/java/org/elasticsearch/reindex/ClientScrollableHitSourceTests.java @@ -23,8 +23,8 @@ import org.elasticsearch.client.support.AbstractClient; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.reindex.ClientScrollableHitSource; import org.elasticsearch.index.reindex.ScrollableHitSource; import org.elasticsearch.search.SearchHit; @@ -83,23 +83,30 @@ private static class ExpectedException extends RuntimeException { public void testRetryFail() { int retries = randomInt(10); - ExpectedException ex = expectThrows(ExpectedException.class, () -> { - dotestBasicsWithRetry(retries, retries+1, retries+1, e -> { throw new ExpectedException(e); }); - }); + ExpectedException ex = expectThrows( + ExpectedException.class, + () -> { dotestBasicsWithRetry(retries, retries + 1, retries + 1, e -> { throw new ExpectedException(e); }); } + ); assertThat(ex.getCause(), instanceOf(EsRejectedExecutionException.class)); } - private void dotestBasicsWithRetry(int retries, int minFailures, int maxFailures, - Consumer failureHandler) throws InterruptedException { + private void dotestBasicsWithRetry(int retries, int minFailures, int maxFailures, Consumer failureHandler) + throws InterruptedException { BlockingQueue responses = new ArrayBlockingQueue<>(100); MockClient client = new MockClient(threadPool); TaskId parentTask = new TaskId("thenode", randomInt()); AtomicInteger actualSearchRetries = new AtomicInteger(); int expectedSearchRetries = 0; - ClientScrollableHitSource hitSource = new ClientScrollableHitSource(logger, BackoffPolicy.constantBackoff(TimeValue.ZERO, retries), - threadPool, actualSearchRetries::incrementAndGet, responses::add, failureHandler, + ClientScrollableHitSource hitSource = new ClientScrollableHitSource( + logger, + BackoffPolicy.constantBackoff(TimeValue.ZERO, retries), + threadPool, + actualSearchRetries::incrementAndGet, + responses::add, + failureHandler, new ParentTaskAssigningClient(client, parentTask), - new SearchRequest().scroll("1m")); + new SearchRequest().scroll("1m") + ); hitSource.start(); for (int retry = 0; retry < randomIntBetween(minFailures, maxFailures); ++retry) { @@ -135,27 +142,41 @@ public void testScrollKeepAlive() { MockClient client = new MockClient(threadPool); TaskId parentTask = new TaskId("thenode", randomInt()); - ClientScrollableHitSource hitSource = new ClientScrollableHitSource(logger, BackoffPolicy.constantBackoff(TimeValue.ZERO, 0), - threadPool, () -> fail(), r -> fail(), e -> fail(), new ParentTaskAssigningClient(client, - parentTask), + ClientScrollableHitSource hitSource = new ClientScrollableHitSource( + logger, + BackoffPolicy.constantBackoff(TimeValue.ZERO, 0), + threadPool, + () -> fail(), + r -> fail(), + e -> fail(), + new ParentTaskAssigningClient(client, parentTask), // Set the base for the scroll to wait - this is added to the figure we calculate below - new SearchRequest().scroll(timeValueSeconds(10))); + new SearchRequest().scroll(timeValueSeconds(10)) + ); hitSource.startNextScroll(timeValueSeconds(100)); - client.validateRequest(SearchScrollAction.INSTANCE, - (SearchScrollRequest r) -> assertEquals(r.scroll().keepAlive().seconds(), 110)); + client.validateRequest(SearchScrollAction.INSTANCE, (SearchScrollRequest r) -> assertEquals(r.scroll().keepAlive().seconds(), 110)); } - - private SearchResponse createSearchResponse() { // create a simulated response. SearchHit hit = new SearchHit(0, "id", emptyMap(), emptyMap()).sourceRef(new BytesArray("{}")); - SearchHits hits = new SearchHits(IntStream.range(0, randomIntBetween(0, 20)).mapToObj(i -> hit).toArray(SearchHit[]::new), - new TotalHits(0, TotalHits.Relation.EQUAL_TO),0); + SearchHits hits = new SearchHits( + IntStream.range(0, randomIntBetween(0, 20)).mapToObj(i -> hit).toArray(SearchHit[]::new), + new TotalHits(0, TotalHits.Relation.EQUAL_TO), + 0 + ); InternalSearchResponse internalResponse = new InternalSearchResponse(hits, null, null, null, false, false, 1); - return new SearchResponse(internalResponse, randomSimpleString(random(), 1, 10), 5, 4, 0, randomLong(), null, - SearchResponse.Clusters.EMPTY); + return new SearchResponse( + internalResponse, + randomSimpleString(random(), 1, 10), + 5, + 4, + 0, + randomLong(), + null, + SearchResponse.Clusters.EMPTY + ); } private void assertSameHits(List actual, SearchHit[] expected) { @@ -199,24 +220,28 @@ public void validateRequest(ActionType action, Consumer executeRequest; + private ExecuteRequest executeRequest; MockClient(ThreadPool threadPool) { super(Settings.EMPTY, threadPool); } @Override - protected synchronized - void doExecute(ActionType action, - Request request, ActionListener listener) { + protected synchronized void doExecute( + ActionType action, + Request request, + ActionListener listener + ) { this.executeRequest = new ExecuteRequest<>(action, request, listener); this.notifyAll(); } @SuppressWarnings("unchecked") - public void respondx(ActionType action, - Function response) { + public void respondx( + ActionType action, + Function response + ) { ExecuteRequest executeRequest; synchronized (this) { executeRequest = this.executeRequest; @@ -225,8 +250,7 @@ public void res ((ExecuteRequest) executeRequest).respond(action, response); } - public void respond(ActionType action, - Response response) { + public void respond(ActionType action, Response response) { respondx(action, req -> response); } @@ -241,14 +265,15 @@ public void fail(ActionType action, } @SuppressWarnings("unchecked") - public void validateRequest(ActionType action, - Consumer validator) { + public void validateRequest( + ActionType action, + Consumer validator + ) { ((ExecuteRequest) executeRequest).validateRequest(action, validator); } @Override - public void close() { - } + public void close() {} public synchronized void awaitOperation() throws InterruptedException { if (executeRequest == null) { diff --git a/modules/reindex/src/test/java/org/elasticsearch/reindex/DeleteByQueryBasicTests.java b/modules/reindex/src/test/java/org/elasticsearch/reindex/DeleteByQueryBasicTests.java index 181e502f07821..1a981c032ae2c 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/reindex/DeleteByQueryBasicTests.java +++ b/modules/reindex/src/test/java/org/elasticsearch/reindex/DeleteByQueryBasicTests.java @@ -16,8 +16,8 @@ import org.elasticsearch.cluster.InternalClusterInfoService; import org.elasticsearch.cluster.routing.allocation.DiskThresholdSettings; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.CollectionUtils; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.IndexNotFoundException; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.reindex.AbstractBulkByScrollRequest; @@ -50,14 +50,15 @@ protected Collection> nodePlugins() { } public void testBasics() throws Exception { - indexRandom(true, - client().prepareIndex("test").setId("1").setSource("foo", "a"), - client().prepareIndex("test").setId("2").setSource("foo", "a"), - client().prepareIndex("test").setId("3").setSource("foo", "b"), - client().prepareIndex("test").setId("4").setSource("foo", "c"), - client().prepareIndex("test").setId("5").setSource("foo", "d"), - client().prepareIndex("test").setId("6").setSource("foo", "e"), - client().prepareIndex("test").setId("7").setSource("foo", "f") + indexRandom( + true, + client().prepareIndex("test").setId("1").setSource("foo", "a"), + client().prepareIndex("test").setId("2").setSource("foo", "a"), + client().prepareIndex("test").setId("3").setSource("foo", "b"), + client().prepareIndex("test").setId("4").setSource("foo", "c"), + client().prepareIndex("test").setId("5").setSource("foo", "d"), + client().prepareIndex("test").setId("6").setSource("foo", "e"), + client().prepareIndex("test").setId("7").setSource("foo", "f") ); assertHitCount(client().prepareSearch("test").setSize(0).get(), 7); @@ -117,8 +118,7 @@ public void testDeleteByQueryWithMultipleIndices() throws Exception { indexRandom(true, true, true, builders); // Deletes all the documents with candidate=true - assertThat(deleteByQuery().source("test-*").filter(termQuery("candidate", true)).refresh(true).get(), - matcher().deleted(deletions)); + assertThat(deleteByQuery().source("test-*").filter(termQuery("candidate", true)).refresh(true).get(), matcher().deleted(deletions)); for (int i = 0; i < indices; i++) { long remaining = docs - candidates[i]; @@ -176,9 +176,12 @@ public void testDeleteByMatchQuery() throws Exception { List builders = new ArrayList<>(); for (int i = 0; i < docs; i++) { - builders.add(client().prepareIndex("test").setId(Integer.toString(i)) + builders.add( + client().prepareIndex("test") + .setId(Integer.toString(i)) .setRouting(randomAlphaOfLengthBetween(1, 5)) - .setSource("foo", "bar")); + .setSource("foo", "bar") + ); } indexRandom(true, true, true, builders); @@ -213,8 +216,10 @@ public void testDeleteByQueryOnReadOnlyIndex() throws Exception { try { enableIndexBlock("test", SETTING_READ_ONLY); - assertThat(deleteByQuery().source("test").filter(QueryBuilders.matchAllQuery()).refresh(true).get(), - matcher().deleted(0).failures(docs)); + assertThat( + deleteByQuery().source("test").filter(QueryBuilders.matchAllQuery()).refresh(true).get(), + matcher().deleted(0).failures(docs) + ); } finally { disableIndexBlock("test", SETTING_READ_ONLY); } @@ -245,19 +250,27 @@ public void testDeleteByQueryOnReadOnlyAllowDeleteIndex() throws Exception { enableIndexBlock("test", SETTING_READ_ONLY_ALLOW_DELETE); if (diskAllocationDeciderEnabled) { // Fire off the delete-by-query first - final ActionFuture deleteByQueryResponse - = deleteByQuery().source("test").filter(QueryBuilders.matchAllQuery()).refresh(true).execute(); + final ActionFuture deleteByQueryResponse = deleteByQuery().source("test") + .filter(QueryBuilders.matchAllQuery()) + .refresh(true) + .execute(); // Then refresh the cluster info which checks the disk threshold and releases the block on the index - final InternalClusterInfoService clusterInfoService - = (InternalClusterInfoService) internalCluster().getCurrentMasterNodeInstance(ClusterInfoService.class); + final InternalClusterInfoService clusterInfoService = (InternalClusterInfoService) internalCluster() + .getCurrentMasterNodeInstance(ClusterInfoService.class); ClusterInfoServiceUtils.refresh(clusterInfoService); // The delete by query request will be executed successfully because it retries after the block is released assertThat(deleteByQueryResponse.actionGet(), matcher().deleted(docs)); } else { // The delete by query request will not be executed successfully because the block cannot be released - assertThat(deleteByQuery().source("test").filter(QueryBuilders.matchAllQuery()).refresh(true) - .setMaxRetries(2).setRetryBackoffInitialTime(TimeValue.timeValueMillis(50)).get(), - matcher().deleted(0).failures(docs)); + assertThat( + deleteByQuery().source("test") + .filter(QueryBuilders.matchAllQuery()) + .refresh(true) + .setMaxRetries(2) + .setRetryBackoffInitialTime(TimeValue.timeValueMillis(50)) + .get(), + matcher().deleted(0).failures(docs) + ); } } finally { disableIndexBlock("test", SETTING_READ_ONLY_ALLOW_DELETE); @@ -273,14 +286,15 @@ public void testDeleteByQueryOnReadOnlyAllowDeleteIndex() throws Exception { } public void testSlices() throws Exception { - indexRandom(true, - client().prepareIndex("test").setId("1").setSource("foo", "a"), - client().prepareIndex("test").setId("2").setSource("foo", "a"), - client().prepareIndex("test").setId("3").setSource("foo", "b"), - client().prepareIndex("test").setId("4").setSource("foo", "c"), - client().prepareIndex("test").setId("5").setSource("foo", "d"), - client().prepareIndex("test").setId("6").setSource("foo", "e"), - client().prepareIndex("test").setId("7").setSource("foo", "f") + indexRandom( + true, + client().prepareIndex("test").setId("1").setSource("foo", "a"), + client().prepareIndex("test").setId("2").setSource("foo", "a"), + client().prepareIndex("test").setId("3").setSource("foo", "b"), + client().prepareIndex("test").setId("4").setSource("foo", "c"), + client().prepareIndex("test").setId("5").setSource("foo", "d"), + client().prepareIndex("test").setId("6").setSource("foo", "e"), + client().prepareIndex("test").setId("7").setSource("foo", "f") ); assertHitCount(client().prepareSearch("test").setSize(0).get(), 7); @@ -289,26 +303,16 @@ public void testSlices() throws Exception { // Deletes the two docs that matches "foo:a" assertThat( - deleteByQuery() - .source("test") - .filter(termQuery("foo", "a")) - .refresh(true) - .setSlices(slices).get(), - matcher() - .deleted(2) - .slices(hasSize(expectedSlices))); + deleteByQuery().source("test").filter(termQuery("foo", "a")).refresh(true).setSlices(slices).get(), + matcher().deleted(2).slices(hasSize(expectedSlices)) + ); assertHitCount(client().prepareSearch("test").setSize(0).get(), 5); // Delete remaining docs assertThat( - deleteByQuery() - .source("test") - .filter(QueryBuilders.matchAllQuery()) - .refresh(true) - .setSlices(slices).get(), - matcher() - .deleted(5) - .slices(hasSize(expectedSlices))); + deleteByQuery().source("test").filter(QueryBuilders.matchAllQuery()).refresh(true).setSlices(slices).get(), + matcher().deleted(5).slices(hasSize(expectedSlices)) + ); assertHitCount(client().prepareSearch("test").setSize(0).get(), 0); } @@ -337,14 +341,9 @@ public void testMultipleSources() throws Exception { String[] sourceIndexNames = docs.keySet().toArray(new String[docs.size()]); assertThat( - deleteByQuery() - .source(sourceIndexNames) - .filter(QueryBuilders.matchAllQuery()) - .refresh(true) - .setSlices(slices).get(), - matcher() - .deleted(allDocs.size()) - .slices(hasSize(expectedSlices))); + deleteByQuery().source(sourceIndexNames).filter(QueryBuilders.matchAllQuery()).refresh(true).setSlices(slices).get(), + matcher().deleted(allDocs.size()).slices(hasSize(expectedSlices)) + ); for (String index : docs.keySet()) { assertHitCount(client().prepareSearch(index).setSize(0).get(), 0); @@ -353,8 +352,7 @@ public void testMultipleSources() throws Exception { } public void testMissingSources() { - BulkByScrollResponse response = updateByQuery() - .source("missing-index-*") + BulkByScrollResponse response = updateByQuery().source("missing-index-*") .refresh(true) .setSlices(AbstractBulkByScrollRequest.AUTO_SLICES) .get(); @@ -363,10 +361,11 @@ public void testMissingSources() { /** Enables or disables the cluster disk allocation decider **/ private void setDiskAllocationDeciderEnabled(boolean value) { - Settings settings = value ? Settings.builder().putNull( - DiskThresholdSettings.CLUSTER_ROUTING_ALLOCATION_DISK_THRESHOLD_ENABLED_SETTING.getKey()).build() : - Settings.builder().put( - DiskThresholdSettings.CLUSTER_ROUTING_ALLOCATION_DISK_THRESHOLD_ENABLED_SETTING.getKey(), value).build(); + Settings settings = value + ? Settings.builder().putNull(DiskThresholdSettings.CLUSTER_ROUTING_ALLOCATION_DISK_THRESHOLD_ENABLED_SETTING.getKey()).build() + : Settings.builder() + .put(DiskThresholdSettings.CLUSTER_ROUTING_ALLOCATION_DISK_THRESHOLD_ENABLED_SETTING.getKey(), value) + .build(); assertAcked(client().admin().cluster().prepareUpdateSettings().setPersistentSettings(settings).get()); } } diff --git a/modules/reindex/src/test/java/org/elasticsearch/reindex/DeleteByQueryConcurrentTests.java b/modules/reindex/src/test/java/org/elasticsearch/reindex/DeleteByQueryConcurrentTests.java index d14350ec0f732..4e7e1c659be02 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/reindex/DeleteByQueryConcurrentTests.java +++ b/modules/reindex/src/test/java/org/elasticsearch/reindex/DeleteByQueryConcurrentTests.java @@ -26,7 +26,7 @@ public class DeleteByQueryConcurrentTests extends ReindexTestCase { public void testConcurrentDeleteByQueriesOnDifferentDocs() throws Throwable { - final Thread[] threads = new Thread[scaledRandomIntBetween(2, 5)]; + final Thread[] threads = new Thread[scaledRandomIntBetween(2, 5)]; final long docs = randomIntBetween(1, 50); List builders = new ArrayList<>(); @@ -46,8 +46,10 @@ public void testConcurrentDeleteByQueriesOnDifferentDocs() throws Throwable { try { start.await(); - assertThat(deleteByQuery().source("_all").filter(termQuery("field", threadNum)).refresh(true).get(), - matcher().deleted(docs)); + assertThat( + deleteByQuery().source("_all").filter(termQuery("field", threadNum)).refresh(true).get(), + matcher().deleted(docs) + ); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } @@ -75,7 +77,7 @@ public void testConcurrentDeleteByQueriesOnSameDocs() throws Throwable { } indexRandom(true, true, true, builders); - final Thread[] threads = new Thread[scaledRandomIntBetween(2, 9)]; + final Thread[] threads = new Thread[scaledRandomIntBetween(2, 9)]; final CountDownLatch start = new CountDownLatch(1); final MatchQueryBuilder query = matchQuery("foo", "bar"); diff --git a/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexBasicTests.java b/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexBasicTests.java index 02c637a3f6fd2..9af4a746b9659 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexBasicTests.java +++ b/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexBasicTests.java @@ -28,10 +28,13 @@ public class ReindexBasicTests extends ReindexTestCase { public void testFiltering() throws Exception { - indexRandom(true, client().prepareIndex("source").setId("1").setSource("foo", "a"), - client().prepareIndex("source").setId("2").setSource("foo", "a"), - client().prepareIndex("source").setId("3").setSource("foo", "b"), - client().prepareIndex("source").setId("4").setSource("foo", "c")); + indexRandom( + true, + client().prepareIndex("source").setId("1").setSource("foo", "a"), + client().prepareIndex("source").setId("2").setSource("foo", "a"), + client().prepareIndex("source").setId("3").setSource("foo", "b"), + client().prepareIndex("source").setId("4").setSource("foo", "c") + ); assertHitCount(client().prepareSearch("source").setSize(0).get(), 4); // Copy all the docs @@ -138,11 +141,7 @@ public void testMultipleSources() throws Exception { int expectedSlices = expectedSliceStatuses(slices, docs.keySet()); String[] sourceIndexNames = docs.keySet().toArray(new String[docs.size()]); - ReindexRequestBuilder request = reindex() - .source(sourceIndexNames) - .destination("dest") - .refresh(true) - .setSlices(slices); + ReindexRequestBuilder request = reindex().source(sourceIndexNames).destination("dest").refresh(true).setSlices(slices); BulkByScrollResponse response = request.get(); assertThat(response, matcher().created(allDocs.size()).slices(hasSize(expectedSlices))); @@ -150,8 +149,7 @@ public void testMultipleSources() throws Exception { } public void testMissingSources() { - BulkByScrollResponse response = updateByQuery() - .source("missing-index-*") + BulkByScrollResponse response = updateByQuery().source("missing-index-*") .refresh(true) .setSlices(AbstractBulkByScrollRequest.AUTO_SLICES) .get(); diff --git a/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexFailureTests.java b/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexFailureTests.java index 7e1d0b0907d29..184256170dfe3 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexFailureTests.java +++ b/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexFailureTests.java @@ -35,8 +35,7 @@ public void testFailuresCauseAbortDefault() throws Exception { * Create the destination index such that the copy will cause a mapping * conflict on every request. */ - indexRandom(true, - client().prepareIndex("dest").setId("test").setSource("test", 10) /* Its a string in the source! */); + indexRandom(true, client().prepareIndex("dest").setId("test").setSource("test", 10) /* Its a string in the source! */); indexDocs(100); @@ -49,10 +48,8 @@ public void testFailuresCauseAbortDefault() throws Exception { copy.source().setSize(1); BulkByScrollResponse response = copy.get(); - assertThat(response, matcher() - .batches(1) - .failures(both(greaterThan(0)).and(lessThanOrEqualTo(maximumNumberOfShards())))); - for (Failure failure: response.getBulkFailures()) { + assertThat(response, matcher().batches(1).failures(both(greaterThan(0)).and(lessThanOrEqualTo(maximumNumberOfShards())))); + for (Failure failure : response.getBulkFailures()) { assertThat(failure.getCause().getCause(), instanceOf(IllegalArgumentException.class)); assertThat(failure.getCause().getCause().getMessage(), containsString("For input string: \"words words\"")); } @@ -60,8 +57,7 @@ public void testFailuresCauseAbortDefault() throws Exception { public void testAbortOnVersionConflict() throws Exception { // Just put something in the way of the copy. - indexRandom(true, - client().prepareIndex("dest").setId("1").setSource("test", "test")); + indexRandom(true, client().prepareIndex("dest").setId("1").setSource("test", "test")); indexDocs(100); @@ -71,7 +67,7 @@ public void testAbortOnVersionConflict() throws Exception { BulkByScrollResponse response = copy.get(); assertThat(response, matcher().batches(1).versionConflicts(1).failures(1).created(99)); - for (Failure failure: response.getBulkFailures()) { + for (Failure failure : response.getBulkFailures()) { assertThat(failure.getMessage(), containsString("VersionConflictEngineException: [")); } } @@ -108,9 +104,9 @@ public void testResponseOnSearchFailure() throws Exception { assertBusy(() -> assertFalse(indexExists("source"))); } catch (ExecutionException e) { logger.info("Triggered a reindex failure on the {} attempt: {}", attempt, e.getMessage()); - assertThat(e.getMessage(), - either(containsString("all shards failed")) - .or(containsString("No search context found")) + assertThat( + e.getMessage(), + either(containsString("all shards failed")).or(containsString("No search context found")) .or(containsString("no such index [source]")) .or(containsString("Partial shards failure")) ); diff --git a/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexFromRemoteBuildRestClientTests.java b/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexFromRemoteBuildRestClientTests.java index 6139e87bf1414..18c57fe342de3 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexFromRemoteBuildRestClientTests.java +++ b/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexFromRemoteBuildRestClientTests.java @@ -17,8 +17,6 @@ import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.index.query.MatchAllQueryBuilder; import org.elasticsearch.index.reindex.RemoteInfo; -import org.elasticsearch.reindex.ReindexSslConfig; -import org.elasticsearch.reindex.Reindexer; import org.elasticsearch.watcher.ResourceWatcherService; import java.util.ArrayList; @@ -36,9 +34,19 @@ public class ReindexFromRemoteBuildRestClientTests extends RestClientBuilderTest private final BytesReference matchAll = new BytesArray(new MatchAllQueryBuilder().toString()); public void testBuildRestClient() throws Exception { - for(final String path: new String[]{"", null, "/", "path"}) { - RemoteInfo remoteInfo = new RemoteInfo("https", "localhost", 9200, path, matchAll, null, null, emptyMap(), - RemoteInfo.DEFAULT_SOCKET_TIMEOUT, RemoteInfo.DEFAULT_CONNECT_TIMEOUT); + for (final String path : new String[] { "", null, "/", "path" }) { + RemoteInfo remoteInfo = new RemoteInfo( + "https", + "localhost", + 9200, + path, + matchAll, + null, + null, + emptyMap(), + RemoteInfo.DEFAULT_SOCKET_TIMEOUT, + RemoteInfo.DEFAULT_CONNECT_TIMEOUT + ); long taskId = randomLong(); List threads = synchronizedList(new ArrayList<>()); RestClient client = Reindexer.buildRestClient(remoteInfo, sslConfig(), taskId, threads); @@ -61,8 +69,18 @@ public void testHeaders() throws Exception { for (int i = 0; i < numHeaders; ++i) { headers.put("header" + i, Integer.toString(i)); } - RemoteInfo remoteInfo = new RemoteInfo("https", "localhost", 9200, null, matchAll, null, null, - headers, RemoteInfo.DEFAULT_SOCKET_TIMEOUT, RemoteInfo.DEFAULT_CONNECT_TIMEOUT); + RemoteInfo remoteInfo = new RemoteInfo( + "https", + "localhost", + 9200, + null, + matchAll, + null, + null, + headers, + RemoteInfo.DEFAULT_SOCKET_TIMEOUT, + RemoteInfo.DEFAULT_CONNECT_TIMEOUT + ); long taskId = randomLong(); List threads = synchronizedList(new ArrayList<>()); RestClient client = Reindexer.buildRestClient(remoteInfo, sslConfig(), taskId, threads); diff --git a/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexFromRemoteWhitelistTests.java b/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexFromRemoteWhitelistTests.java index 7595d7ed7d7fd..83adc2586c172 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexFromRemoteWhitelistTests.java +++ b/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexFromRemoteWhitelistTests.java @@ -43,8 +43,18 @@ public void testLocalRequestWithWhitelist() { * Build a {@link RemoteInfo}, defaulting values that we don't care about in this test to values that don't hurt anything. */ private RemoteInfo newRemoteInfo(String host, int port) { - return new RemoteInfo(randomAlphaOfLength(5), host, port, null, query, null, null, emptyMap(), - RemoteInfo.DEFAULT_SOCKET_TIMEOUT, RemoteInfo.DEFAULT_CONNECT_TIMEOUT); + return new RemoteInfo( + randomAlphaOfLength(5), + host, + port, + null, + query, + null, + null, + emptyMap(), + RemoteInfo.DEFAULT_SOCKET_TIMEOUT, + RemoteInfo.DEFAULT_CONNECT_TIMEOUT + ); } public void testWhitelistedRemote() { @@ -56,11 +66,25 @@ public void testWhitelistedRemote() { } public void testWhitelistedByPrefix() { - checkRemoteWhitelist(buildRemoteWhitelist(singletonList("*.example.com:9200")), - new RemoteInfo(randomAlphaOfLength(5), "es.example.com", 9200, null, query, null, null, emptyMap(), - RemoteInfo.DEFAULT_SOCKET_TIMEOUT, RemoteInfo.DEFAULT_CONNECT_TIMEOUT)); - checkRemoteWhitelist(buildRemoteWhitelist(singletonList("*.example.com:9200")), - newRemoteInfo("6e134134a1.us-east-1.aws.example.com", 9200)); + checkRemoteWhitelist( + buildRemoteWhitelist(singletonList("*.example.com:9200")), + new RemoteInfo( + randomAlphaOfLength(5), + "es.example.com", + 9200, + null, + query, + null, + null, + emptyMap(), + RemoteInfo.DEFAULT_SOCKET_TIMEOUT, + RemoteInfo.DEFAULT_CONNECT_TIMEOUT + ) + ); + checkRemoteWhitelist( + buildRemoteWhitelist(singletonList("*.example.com:9200")), + newRemoteInfo("6e134134a1.us-east-1.aws.example.com", 9200) + ); } public void testWhitelistedBySuffix() { @@ -80,8 +104,10 @@ public void testLoopbackInWhitelistRemote() throws UnknownHostException { public void testUnwhitelistedRemote() { int port = between(1, Integer.MAX_VALUE); List whitelist = randomBoolean() ? randomWhitelist() : emptyList(); - Exception e = expectThrows(IllegalArgumentException.class, - () -> checkRemoteWhitelist(buildRemoteWhitelist(whitelist), newRemoteInfo("not in list", port))); + Exception e = expectThrows( + IllegalArgumentException.class, + () -> checkRemoteWhitelist(buildRemoteWhitelist(whitelist), newRemoteInfo("not in list", port)) + ); assertEquals("[not in list:" + port + "] not whitelisted in reindex.remote.whitelist", e.getMessage()); } @@ -104,9 +130,14 @@ public void testIPv6Address() { private void assertMatchesTooMuch(List whitelist) { Exception e = expectThrows(IllegalArgumentException.class, () -> buildRemoteWhitelist(whitelist)); - assertEquals("Refusing to start because whitelist " + whitelist + " accepts all addresses. " + assertEquals( + "Refusing to start because whitelist " + + whitelist + + " accepts all addresses. " + "This would allow users to reindex-from-remote any URL they like effectively having Elasticsearch make HTTP GETs " - + "for them.", e.getMessage()); + + "for them.", + e.getMessage() + ); } private List randomWhitelist() { diff --git a/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexFromRemoteWithAuthTests.java b/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexFromRemoteWithAuthTests.java index 059095dafd66f..8ccac6ac223db 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexFromRemoteWithAuthTests.java +++ b/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexFromRemoteWithAuthTests.java @@ -28,7 +28,6 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.TransportAddress; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.env.Environment; import org.elasticsearch.env.NodeEnvironment; import org.elasticsearch.http.HttpInfo; @@ -46,6 +45,7 @@ import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.netty4.Netty4Plugin; import org.elasticsearch.watcher.ResourceWatcherService; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.junit.Before; import java.util.Arrays; @@ -66,10 +66,7 @@ public class ReindexFromRemoteWithAuthTests extends ESSingleNodeTestCase { @Override protected Collection> getPlugins() { - return Arrays.asList( - Netty4Plugin.class, - ReindexFromRemoteWithAuthTests.TestPlugin.class, - ReindexPlugin.class); + return Arrays.asList(Netty4Plugin.class, ReindexFromRemoteWithAuthTests.TestPlugin.class, ReindexPlugin.class); } @Override @@ -101,28 +98,40 @@ public void fetchTransportAddress() { * Build a {@link RemoteInfo}, defaulting values that we don't care about in this test to values that don't hurt anything. */ private RemoteInfo newRemoteInfo(String username, String password, Map headers) { - return new RemoteInfo("http", address.getAddress(), address.getPort(), null, - new BytesArray("{\"match_all\":{}}"), username, password, headers, - RemoteInfo.DEFAULT_SOCKET_TIMEOUT, RemoteInfo.DEFAULT_CONNECT_TIMEOUT); + return new RemoteInfo( + "http", + address.getAddress(), + address.getPort(), + null, + new BytesArray("{\"match_all\":{}}"), + username, + password, + headers, + RemoteInfo.DEFAULT_SOCKET_TIMEOUT, + RemoteInfo.DEFAULT_CONNECT_TIMEOUT + ); } public void testReindexFromRemoteWithAuthentication() throws Exception { - ReindexRequestBuilder request = new ReindexRequestBuilder(client(), ReindexAction.INSTANCE).source("source").destination("dest") - .setRemoteInfo(newRemoteInfo("Aladdin", "open sesame", emptyMap())); + ReindexRequestBuilder request = new ReindexRequestBuilder(client(), ReindexAction.INSTANCE).source("source") + .destination("dest") + .setRemoteInfo(newRemoteInfo("Aladdin", "open sesame", emptyMap())); assertThat(request.get(), matcher().created(1)); } public void testReindexSendsHeaders() throws Exception { - ReindexRequestBuilder request = new ReindexRequestBuilder(client(), ReindexAction.INSTANCE).source("source").destination("dest") - .setRemoteInfo(newRemoteInfo(null, null, singletonMap(TestFilter.EXAMPLE_HEADER, "doesn't matter"))); + ReindexRequestBuilder request = new ReindexRequestBuilder(client(), ReindexAction.INSTANCE).source("source") + .destination("dest") + .setRemoteInfo(newRemoteInfo(null, null, singletonMap(TestFilter.EXAMPLE_HEADER, "doesn't matter"))); ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, () -> request.get()); assertEquals(RestStatus.BAD_REQUEST, e.status()); assertThat(e.getMessage(), containsString("Hurray! Sent the header!")); } public void testReindexWithoutAuthenticationWhenRequired() throws Exception { - ReindexRequestBuilder request = new ReindexRequestBuilder(client(), ReindexAction.INSTANCE).source("source").destination("dest") - .setRemoteInfo(newRemoteInfo(null, null, emptyMap())); + ReindexRequestBuilder request = new ReindexRequestBuilder(client(), ReindexAction.INSTANCE).source("source") + .destination("dest") + .setRemoteInfo(newRemoteInfo(null, null, emptyMap())); ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, () -> request.get()); assertEquals(RestStatus.UNAUTHORIZED, e.status()); assertThat(e.getMessage(), containsString("\"reason\":\"Authentication required\"")); @@ -130,8 +139,9 @@ public void testReindexWithoutAuthenticationWhenRequired() throws Exception { } public void testReindexWithBadAuthentication() throws Exception { - ReindexRequestBuilder request = new ReindexRequestBuilder(client(), ReindexAction.INSTANCE).source("source").destination("dest") - .setRemoteInfo(newRemoteInfo("junk", "auth", emptyMap())); + ReindexRequestBuilder request = new ReindexRequestBuilder(client(), ReindexAction.INSTANCE).source("source") + .destination("dest") + .setRemoteInfo(newRemoteInfo("junk", "auth", emptyMap())); ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, () -> request.get()); assertThat(e.getMessage(), containsString("\"reason\":\"Bad Authorization\"")); } @@ -144,12 +154,19 @@ public static class TestPlugin extends Plugin implements ActionPlugin { private final SetOnce testFilter = new SetOnce<>(); @Override - public Collection createComponents(Client client, ClusterService clusterService, ThreadPool threadPool, - ResourceWatcherService resourceWatcherService, ScriptService scriptService, - NamedXContentRegistry xContentRegistry, Environment environment, - NodeEnvironment nodeEnvironment, NamedWriteableRegistry namedWriteableRegistry, - IndexNameExpressionResolver expressionResolver, - Supplier repositoriesServiceSupplier) { + public Collection createComponents( + Client client, + ClusterService clusterService, + ThreadPool threadPool, + ResourceWatcherService resourceWatcherService, + ScriptService scriptService, + NamedXContentRegistry xContentRegistry, + Environment environment, + NodeEnvironment nodeEnvironment, + NamedWriteableRegistry namedWriteableRegistry, + IndexNameExpressionResolver expressionResolver, + Supplier repositoriesServiceSupplier + ) { testFilter.set(new ReindexFromRemoteWithAuthTests.TestFilter(threadPool)); return Collections.emptyList(); } @@ -161,8 +178,10 @@ public List getActionFilters() { @Override public Collection getRestHeaders() { - return Arrays.asList(new RestHeaderDefinition(TestFilter.AUTHORIZATION_HEADER, false), - new RestHeaderDefinition(TestFilter.EXAMPLE_HEADER, false)); + return Arrays.asList( + new RestHeaderDefinition(TestFilter.AUTHORIZATION_HEADER, false), + new RestHeaderDefinition(TestFilter.EXAMPLE_HEADER, false) + ); } } @@ -189,8 +208,13 @@ public int order() { } @Override - public void apply(Task task, String action, - Request request, ActionListener listener, ActionFilterChain chain) { + public void apply( + Task task, + String action, + Request request, + ActionListener listener, + ActionFilterChain chain + ) { if (false == action.equals(SearchAction.NAME)) { chain.proceed(task, action, request, listener); return; @@ -200,8 +224,7 @@ public void app } String auth = context.getHeader(AUTHORIZATION_HEADER); if (auth == null) { - ElasticsearchSecurityException e = new ElasticsearchSecurityException("Authentication required", - RestStatus.UNAUTHORIZED); + ElasticsearchSecurityException e = new ElasticsearchSecurityException("Authentication required", RestStatus.UNAUTHORIZED); e.addHeader("WWW-Authenticate", "Basic realm=auth-realm"); throw e; } diff --git a/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexMetadataTests.java b/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexMetadataTests.java index 65f84088eb1e9..30c6e8c3ff474 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexMetadataTests.java +++ b/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexMetadataTests.java @@ -8,10 +8,10 @@ package org.elasticsearch.reindex; +import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.index.reindex.BulkByScrollResponse; import org.elasticsearch.index.reindex.ReindexRequest; import org.elasticsearch.index.reindex.ScrollableHitSource.Hit; -import org.elasticsearch.action.index.IndexRequest; /** * Index-by-search test for ttl, timestamp, and routing. @@ -67,8 +67,17 @@ protected ReindexRequest request() { private class TestAction extends Reindexer.AsyncIndexBySearchAction { TestAction() { - super(ReindexMetadataTests.this.task, ReindexMetadataTests.this.logger, null, null, ReindexMetadataTests.this.threadPool, - null, null, request(), listener()); + super( + ReindexMetadataTests.this.task, + ReindexMetadataTests.this.logger, + null, + null, + ReindexMetadataTests.this.threadPool, + null, + null, + request(), + listener() + ); } public ReindexRequest mainRequest() { @@ -76,8 +85,10 @@ public ReindexRequest mainRequest() { } @Override - public AbstractAsyncBulkByScrollAction.RequestWrapper copyMetadata(AbstractAsyncBulkByScrollAction.RequestWrapper request, - Hit doc) { + public AbstractAsyncBulkByScrollAction.RequestWrapper copyMetadata( + AbstractAsyncBulkByScrollAction.RequestWrapper request, + Hit doc + ) { return super.copyMetadata(request, doc); } } diff --git a/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexRestClientSslTests.java b/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexRestClientSslTests.java index d9cb47628e7e0..7a2e139586e80 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexRestClientSslTests.java +++ b/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexRestClientSslTests.java @@ -13,19 +13,19 @@ import com.sun.net.httpserver.HttpsParameters; import com.sun.net.httpserver.HttpsServer; -import org.elasticsearch.index.reindex.RemoteInfo; -import org.elasticsearch.jdk.JavaVersion; import org.elasticsearch.client.Request; import org.elasticsearch.client.Response; import org.elasticsearch.client.RestClient; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.common.bytes.BytesArray; -import org.elasticsearch.core.PathUtils; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.ssl.PemKeyConfig; import org.elasticsearch.common.ssl.PemTrustConfig; +import org.elasticsearch.core.PathUtils; +import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.env.Environment; import org.elasticsearch.env.TestEnvironment; +import org.elasticsearch.index.reindex.RemoteInfo; +import org.elasticsearch.jdk.JavaVersion; import org.elasticsearch.mocksocket.MockHttpServer; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.watcher.ResourceWatcherService; @@ -33,13 +33,6 @@ import org.junit.AfterClass; import org.junit.BeforeClass; -import javax.net.ssl.KeyManager; -import javax.net.ssl.SSLContext; -import javax.net.ssl.SSLHandshakeException; -import javax.net.ssl.SSLPeerUnverifiedException; -import javax.net.ssl.TrustManager; -import javax.net.ssl.X509ExtendedKeyManager; -import javax.net.ssl.X509ExtendedTrustManager; import java.io.IOException; import java.net.InetAddress; import java.net.InetSocketAddress; @@ -52,6 +45,14 @@ import java.util.concurrent.atomic.AtomicReference; import java.util.function.Consumer; +import javax.net.ssl.KeyManager; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLHandshakeException; +import javax.net.ssl.SSLPeerUnverifiedException; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509ExtendedKeyManager; +import javax.net.ssl.X509ExtendedTrustManager; + import static org.mockito.Mockito.mock; /** @@ -64,8 +65,7 @@ public class ReindexRestClientSslTests extends ESTestCase { private static HttpsServer server; - private static Consumer handler = ignore -> { - }; + private static Consumer handler = ignore -> {}; @BeforeClass public static void setupHttpServer() throws Exception { @@ -79,8 +79,8 @@ public static void setupHttpServer() throws Exception { HttpsExchange https = (HttpsExchange) http; handler.accept(https); // Always respond with 200 - // * If the reindex sees the 200, it means the SSL connection was established correctly. - // * We can check client certs in the handler. + // * If the reindex sees the 200, it means the SSL connection was established correctly. + // * We can check client certs in the handler. https.sendResponseHeaders(200, 0); https.close(); }); @@ -114,8 +114,7 @@ private static SSLContext buildServerSslContext() throws Exception { public void testClientFailsWithUntrustedCertificate() throws IOException { assumeFalse("https://github.com/elastic/elasticsearch/issues/49094", inFipsJvm()); final List threads = new ArrayList<>(); - final Settings.Builder builder = Settings.builder() - .put("path.home", createTempDir()); + final Settings.Builder builder = Settings.builder().put("path.home", createTempDir()); if (isHttpsServerBrokenWithTLSv13()) { builder.put("reindex.ssl.supported_protocols", "TLSv1.2"); } @@ -148,9 +147,7 @@ public void testClientSucceedsWithCertificateAuthorities() throws IOException { public void testClientSucceedsWithVerificationDisabled() throws IOException { assumeFalse("Cannot disable verification in FIPS JVM", inFipsJvm()); final List threads = new ArrayList<>(); - final Settings.Builder builder = Settings.builder() - .put("path.home", createTempDir()) - .put("reindex.ssl.verification_mode", "NONE"); + final Settings.Builder builder = Settings.builder().put("path.home", createTempDir()).put("reindex.ssl.verification_mode", "NONE"); if (isHttpsServerBrokenWithTLSv13()) { builder.put("reindex.ssl.supported_protocols", "TLSv1.2"); } @@ -203,9 +200,18 @@ public void testClientPassesClientCertificate() throws IOException { } private RemoteInfo getRemoteInfo() { - return new RemoteInfo("https", server.getAddress().getHostName(), server.getAddress().getPort(), "/", - new BytesArray("{\"match_all\":{}}"), "user", "password", Collections.emptyMap(), RemoteInfo.DEFAULT_SOCKET_TIMEOUT, - RemoteInfo.DEFAULT_CONNECT_TIMEOUT); + return new RemoteInfo( + "https", + server.getAddress().getHostName(), + server.getAddress().getPort(), + "/", + new BytesArray("{\"match_all\":{}}"), + "user", + "password", + Collections.emptyMap(), + RemoteInfo.DEFAULT_SOCKET_TIMEOUT, + RemoteInfo.DEFAULT_CONNECT_TIMEOUT + ); } @SuppressForbidden(reason = "use http server") diff --git a/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexScriptTests.java b/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexScriptTests.java index c5063a82a5bbd..ed83221aa14ab 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexScriptTests.java +++ b/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexScriptTests.java @@ -25,7 +25,7 @@ public class ReindexScriptTests extends AbstractAsyncBulkByScrollActionScriptTestCase { public void testSetIndex() throws Exception { - Object dest = randomFrom(new Object[] {234, 234L, "pancake"}); + Object dest = randomFrom(new Object[] { 234, 234L, "pancake" }); IndexRequest index = applyScript((Map ctx) -> ctx.put("_index", dest)); assertEquals(dest.toString(), index.index()); } @@ -39,7 +39,7 @@ public void testSettingIndexToNullIsError() throws Exception { } public void testSetId() throws Exception { - Object id = randomFrom(new Object[] {null, 234, 234L, "pancake"}); + Object id = randomFrom(new Object[] { null, 234, 234L, "pancake" }); IndexRequest index = applyScript((Map ctx) -> ctx.put("_id", id)); if (id == null) { assertNull(index.id()); @@ -49,7 +49,7 @@ public void testSetId() throws Exception { } public void testSetVersion() throws Exception { - Number version = randomFrom(new Number[] {null, 234, 234L}); + Number version = randomFrom(new Number[] { null, 234, 234L }); IndexRequest index = applyScript((Map ctx) -> ctx.put("_version", version)); if (version == null) { assertEquals(Versions.MATCH_ANY, index.version()); diff --git a/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexSingleNodeTests.java b/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexSingleNodeTests.java index 450e02d5cb7cb..f0d9285e9a140 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexSingleNodeTests.java +++ b/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexSingleNodeTests.java @@ -38,14 +38,15 @@ public void testDeprecatedSort() { // Copy a subset of the docs sorted int subsetSize = randomIntBetween(1, max - 1); - ReindexRequestBuilder copy = new ReindexRequestBuilder(client(), ReindexAction.INSTANCE) - .source("source").destination("dest").refresh(true); + ReindexRequestBuilder copy = new ReindexRequestBuilder(client(), ReindexAction.INSTANCE).source("source") + .destination("dest") + .refresh(true); copy.maxDocs(subsetSize); copy.request().addSortField("foo", SortOrder.DESC); assertThat(copy.get(), matcher().created(subsetSize)); assertHitCount(client().prepareSearch("dest").setSize(0).get(), subsetSize); - assertHitCount(client().prepareSearch("dest").setQuery(new RangeQueryBuilder("foo").gte(0).lt(max-subsetSize)).get(), 0); + assertHitCount(client().prepareSearch("dest").setQuery(new RangeQueryBuilder("foo").gte(0).lt(max - subsetSize)).get(), 0); assertWarnings(ReindexValidator.SORT_DEPRECATED_MESSAGE); } } diff --git a/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexSourceTargetValidationTests.java b/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexSourceTargetValidationTests.java index b5c52e3973d8f..7e861ccbc9c59 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexSourceTargetValidationTests.java +++ b/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexSourceTargetValidationTests.java @@ -19,15 +19,14 @@ import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; import org.elasticsearch.cluster.metadata.Metadata; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.ClusterSettings; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.Nullable; import org.elasticsearch.index.reindex.RemoteInfo; import org.elasticsearch.indices.EmptySystemIndices; import org.elasticsearch.indices.TestIndexNameExpressionResolver; -import org.elasticsearch.reindex.ReindexValidator; import org.elasticsearch.test.ESTestCase; import static java.util.Collections.emptyMap; @@ -40,7 +39,9 @@ * cluster.... */ public class ReindexSourceTargetValidationTests extends ESTestCase { - private static final ClusterState STATE = ClusterState.builder(new ClusterName("test")).metadata(Metadata.builder() + private static final ClusterState STATE = ClusterState.builder(new ClusterName("test")) + .metadata( + Metadata.builder() .put(index("target", "target_alias", "target_multi"), true) .put(index("target2", "target_multi"), true) .put(index("target_with_write_index", true, "target_multi_with_write_index"), true) @@ -50,10 +51,14 @@ public class ReindexSourceTargetValidationTests extends ESTestCase { .put(index("bar"), true) .put(index("baz"), true) .put(index("source", "source_multi"), true) - .put(index("source2", "source_multi"), true)).build(); + .put(index("source2", "source_multi"), true) + ) + .build(); private static final IndexNameExpressionResolver INDEX_NAME_EXPRESSION_RESOLVER = TestIndexNameExpressionResolver.newInstance(); - private static final AutoCreateIndex AUTO_CREATE_INDEX = new AutoCreateIndex(Settings.EMPTY, - new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), INDEX_NAME_EXPRESSION_RESOLVER, + private static final AutoCreateIndex AUTO_CREATE_INDEX = new AutoCreateIndex( + Settings.EMPTY, + new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), + INDEX_NAME_EXPRESSION_RESOLVER, EmptySystemIndices.INSTANCE ); @@ -82,9 +87,14 @@ public void testAliasesContainTarget() { public void testTargetIsAliasToMultipleIndicesWithoutWriteAlias() { Exception e = expectThrows(IllegalArgumentException.class, () -> succeeds("target_multi", "foo")); - assertThat(e.getMessage(), containsString("no write index is defined for alias [target_multi]. The write index may be explicitly " + - "disabled using is_write_index=false or the alias points to multiple indices without one being designated as a " + - "write index")); + assertThat( + e.getMessage(), + containsString( + "no write index is defined for alias [target_multi]. The write index may be explicitly " + + "disabled using is_write_index=false or the alias points to multiple indices without one being designated as a " + + "write index" + ) + ); } public void testTargetIsAliasWithWriteIndexDisabled() { @@ -109,11 +119,39 @@ public void testTargetIsWriteAlias() { public void testRemoteInfoSkipsValidation() { // The index doesn't have to exist - succeeds(new RemoteInfo(randomAlphaOfLength(5), "test", 9200, null, query, null, null, emptyMap(), - RemoteInfo.DEFAULT_SOCKET_TIMEOUT, RemoteInfo.DEFAULT_CONNECT_TIMEOUT), "does_not_exist", "target"); + succeeds( + new RemoteInfo( + randomAlphaOfLength(5), + "test", + 9200, + null, + query, + null, + null, + emptyMap(), + RemoteInfo.DEFAULT_SOCKET_TIMEOUT, + RemoteInfo.DEFAULT_CONNECT_TIMEOUT + ), + "does_not_exist", + "target" + ); // And it doesn't matter if they are the same index. They are considered to be different because the remote one is, well, remote. - succeeds(new RemoteInfo(randomAlphaOfLength(5), "test", 9200, null, query, null, null, emptyMap(), - RemoteInfo.DEFAULT_SOCKET_TIMEOUT, RemoteInfo.DEFAULT_CONNECT_TIMEOUT), "target", "target"); + succeeds( + new RemoteInfo( + randomAlphaOfLength(5), + "test", + 9200, + null, + query, + null, + null, + emptyMap(), + RemoteInfo.DEFAULT_SOCKET_TIMEOUT, + RemoteInfo.DEFAULT_CONNECT_TIMEOUT + ), + "target", + "target" + ); } private void fails(String target, String... sources) { @@ -126,8 +164,14 @@ private void succeeds(String target, String... sources) { } private void succeeds(RemoteInfo remoteInfo, String target, String... sources) { - ReindexValidator.validateAgainstAliases(new SearchRequest(sources), new IndexRequest(target), remoteInfo, - INDEX_NAME_EXPRESSION_RESOLVER, AUTO_CREATE_INDEX, STATE); + ReindexValidator.validateAgainstAliases( + new SearchRequest(sources), + new IndexRequest(target), + remoteInfo, + INDEX_NAME_EXPRESSION_RESOLVER, + AUTO_CREATE_INDEX, + STATE + ); } private static IndexMetadata index(String name, String... aliases) { @@ -135,11 +179,14 @@ private static IndexMetadata index(String name, String... aliases) { } private static IndexMetadata index(String name, @Nullable Boolean writeIndex, String... aliases) { - IndexMetadata.Builder builder = IndexMetadata.builder(name).settings(Settings.builder() - .put("index.version.created", Version.CURRENT.id) - .put("index.number_of_shards", 1) - .put("index.number_of_replicas", 1)); - for (String alias: aliases) { + IndexMetadata.Builder builder = IndexMetadata.builder(name) + .settings( + Settings.builder() + .put("index.version.created", Version.CURRENT.id) + .put("index.number_of_shards", 1) + .put("index.number_of_replicas", 1) + ); + for (String alias : aliases) { builder.putAlias(AliasMetadata.builder(alias).writeIndex(writeIndex).build()); } return builder.build(); diff --git a/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexTestCase.java b/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexTestCase.java index 18c4ef60a8d39..2bc30ba612f80 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexTestCase.java +++ b/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexTestCase.java @@ -74,9 +74,9 @@ static int randomSlices() { */ protected int expectedSlices(int requestSlices, Collection indices) { if (requestSlices == AbstractBulkByScrollRequest.AUTO_SLICES) { - int leastNumShards = Collections.min(indices.stream() - .map(sourceIndex -> getNumShards(sourceIndex).numPrimaries) - .collect(Collectors.toList())); + int leastNumShards = Collections.min( + indices.stream().map(sourceIndex -> getNumShards(sourceIndex).numPrimaries).collect(Collectors.toList()) + ); return Math.min(leastNumShards, BulkByScrollParallelizationHelper.AUTO_SLICE_CEILING); } else { return requestSlices; diff --git a/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexVersioningTests.java b/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexVersioningTests.java index 1bdea8f168055..96f0ff50027af 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexVersioningTests.java +++ b/modules/reindex/src/test/java/org/elasticsearch/reindex/ReindexVersioningTests.java @@ -16,7 +16,6 @@ import static org.elasticsearch.index.VersionType.EXTERNAL; import static org.elasticsearch.index.VersionType.INTERNAL; - public class ReindexVersioningTests extends ReindexTestCase { private static final int SOURCE_VERSION = 4; private static final int OLDER_VERSION = 1; @@ -80,7 +79,7 @@ public void testCreateVersionConflictsOnNewer() throws Exception { * Perform a reindex with EXTERNAL versioning which has "refresh" semantics. */ private BulkByScrollResponse reindexExternal() { - ReindexRequestBuilder reindex = reindex().source("source").destination("dest").abortOnVersionConflict(false); + ReindexRequestBuilder reindex = reindex().source("source").destination("dest").abortOnVersionConflict(false); reindex.destination().setVersionType(EXTERNAL); return reindex.get(); } @@ -89,7 +88,7 @@ private BulkByScrollResponse reindexExternal() { * Perform a reindex with INTERNAL versioning which has "overwrite" semantics. */ private BulkByScrollResponse reindexInternal() { - ReindexRequestBuilder reindex = reindex().source("source").destination("dest").abortOnVersionConflict(false); + ReindexRequestBuilder reindex = reindex().source("source").destination("dest").abortOnVersionConflict(false); reindex.destination().setVersionType(INTERNAL); return reindex.get(); } @@ -98,22 +97,26 @@ private BulkByScrollResponse reindexInternal() { * Perform a reindex with CREATE OpType which has "create" semantics. */ private BulkByScrollResponse reindexCreate() { - ReindexRequestBuilder reindex = reindex().source("source").destination("dest").abortOnVersionConflict(false); + ReindexRequestBuilder reindex = reindex().source("source").destination("dest").abortOnVersionConflict(false); reindex.destination().setOpType(CREATE); return reindex.get(); } private void setupSourceAbsent() throws Exception { - indexRandom(true, client().prepareIndex("source").setId("test").setVersionType(EXTERNAL) - .setVersion(SOURCE_VERSION).setSource("foo", "source")); + indexRandom( + true, + client().prepareIndex("source").setId("test").setVersionType(EXTERNAL).setVersion(SOURCE_VERSION).setSource("foo", "source") + ); assertEquals(SOURCE_VERSION, client().prepareGet("source", "test").get().getVersion()); } private void setupDest(int version) throws Exception { setupSourceAbsent(); - indexRandom(true, client().prepareIndex("dest").setId("test").setVersionType(EXTERNAL) - .setVersion(version).setSource("foo", "dest")); + indexRandom( + true, + client().prepareIndex("dest").setId("test").setVersionType(EXTERNAL).setVersion(version).setSource("foo", "dest") + ); assertEquals(version, client().prepareGet("dest", "test").get().getVersion()); } diff --git a/modules/reindex/src/test/java/org/elasticsearch/reindex/RestDeleteByQueryActionTests.java b/modules/reindex/src/test/java/org/elasticsearch/reindex/RestDeleteByQueryActionTests.java index 1991ca15295c2..a6c55bddb15df 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/reindex/RestDeleteByQueryActionTests.java +++ b/modules/reindex/src/test/java/org/elasticsearch/reindex/RestDeleteByQueryActionTests.java @@ -9,13 +9,12 @@ package org.elasticsearch.reindex; import org.elasticsearch.core.RestApiVersion; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.reindex.BulkByScrollResponse; -import org.elasticsearch.reindex.RestDeleteByQueryAction; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.search.RestSearchAction; import org.elasticsearch.test.rest.FakeRestRequest; import org.elasticsearch.test.rest.RestActionTestCase; +import org.elasticsearch.xcontent.XContentType; import org.junit.Before; import org.mockito.Mockito; @@ -36,17 +35,14 @@ public void setUpAction() { } public void testTypeInPath() throws IOException { - RestRequest request = new FakeRestRequest.Builder(xContentRegistry()) - .withHeaders(Map.of("Content-Type", contentTypeHeader, "Accept", contentTypeHeader)) - .withMethod(RestRequest.Method.POST) - .withPath("/some_index/some_type/_delete_by_query") - .build(); + RestRequest request = new FakeRestRequest.Builder(xContentRegistry()).withHeaders( + Map.of("Content-Type", contentTypeHeader, "Accept", contentTypeHeader) + ).withMethod(RestRequest.Method.POST).withPath("/some_index/some_type/_delete_by_query").build(); // checks the type in the URL is propagated correctly to the request object // only works after the request is dispatched, so its params are filled from url. dispatchRequest(request); - // RestDeleteByQueryAction itself doesn't check for a deprecated type usage // checking here for a deprecation from its internal search request assertWarnings(RestSearchAction.TYPES_DEPRECATION_MESSAGE); diff --git a/modules/reindex/src/test/java/org/elasticsearch/reindex/RestReindexActionTests.java b/modules/reindex/src/test/java/org/elasticsearch/reindex/RestReindexActionTests.java index ef851ae6c821e..0df013056dcdd 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/reindex/RestReindexActionTests.java +++ b/modules/reindex/src/test/java/org/elasticsearch/reindex/RestReindexActionTests.java @@ -11,13 +11,13 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.index.reindex.AbstractBulkByScrollRequest; import org.elasticsearch.index.reindex.ReindexRequest; import org.elasticsearch.test.rest.FakeRestRequest; import org.elasticsearch.test.rest.RestActionTestCase; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.xcontent.json.JsonXContent; import org.junit.Before; import java.io.IOException; @@ -38,12 +38,15 @@ public void setUpAction() { public void testPipelineQueryParameterIsError() throws IOException { FakeRestRequest.Builder request = new FakeRestRequest.Builder(xContentRegistry()); try (XContentBuilder body = JsonXContent.contentBuilder().prettyPrint()) { - body.startObject(); { - body.startObject("source"); { + body.startObject(); + { + body.startObject("source"); + { body.field("index", "source"); } body.endObject(); - body.startObject("dest"); { + body.startObject("dest"); + { body.field("index", "dest"); } body.endObject(); @@ -52,8 +55,10 @@ public void testPipelineQueryParameterIsError() throws IOException { request.withContent(BytesReference.bytes(body), body.contentType()); } request.withParams(singletonMap("pipeline", "doesn't matter")); - Exception e = expectThrows(IllegalArgumentException.class, () -> - action.buildRequest(request.build(), new NamedWriteableRegistry(Collections.emptyList()))); + Exception e = expectThrows( + IllegalArgumentException.class, + () -> action.buildRequest(request.build(), new NamedWriteableRegistry(Collections.emptyList())) + ); assertEquals("_reindex doesn't support [pipeline] as a query parameter. Specify it in the [dest] object instead.", e.getMessage()); } diff --git a/modules/reindex/src/test/java/org/elasticsearch/reindex/RestUpdateByQueryActionTests.java b/modules/reindex/src/test/java/org/elasticsearch/reindex/RestUpdateByQueryActionTests.java index 85180cd506bac..1621a948662e7 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/reindex/RestUpdateByQueryActionTests.java +++ b/modules/reindex/src/test/java/org/elasticsearch/reindex/RestUpdateByQueryActionTests.java @@ -9,13 +9,12 @@ package org.elasticsearch.reindex; import org.elasticsearch.core.RestApiVersion; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.reindex.BulkByScrollResponse; -import org.elasticsearch.reindex.RestUpdateByQueryAction; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.search.RestSearchAction; import org.elasticsearch.test.rest.FakeRestRequest; import org.elasticsearch.test.rest.RestActionTestCase; +import org.elasticsearch.xcontent.XContentType; import org.junit.Before; import org.mockito.Mockito; @@ -36,11 +35,9 @@ public void setUpAction() { } public void testTypeInPath() throws IOException { - RestRequest request = new FakeRestRequest.Builder(xContentRegistry()) - .withHeaders(Map.of("Content-Type", contentTypeHeader, "Accept", contentTypeHeader)) - .withMethod(RestRequest.Method.POST) - .withPath("/some_index/some_type/_update_by_query") - .build(); + RestRequest request = new FakeRestRequest.Builder(xContentRegistry()).withHeaders( + Map.of("Content-Type", contentTypeHeader, "Accept", contentTypeHeader) + ).withMethod(RestRequest.Method.POST).withPath("/some_index/some_type/_update_by_query").build(); // checks the type in the URL is propagated correctly to the request object // only works after the request is dispatched, so its params are filled from url. diff --git a/modules/reindex/src/test/java/org/elasticsearch/reindex/RethrottleTests.java b/modules/reindex/src/test/java/org/elasticsearch/reindex/RethrottleTests.java index 04c7e6937de65..b3be206e5c5c0 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/reindex/RethrottleTests.java +++ b/modules/reindex/src/test/java/org/elasticsearch/reindex/RethrottleTests.java @@ -97,12 +97,16 @@ private void testCase(AbstractBulkByScrollRequestBuilder request, String a assertThat(taskGroupToRethrottle.getChildTasks(), empty()); } else { // There should be a sane number of child tasks running - assertThat(taskGroupToRethrottle.getChildTasks(), - hasSize(allOf(greaterThanOrEqualTo(1), lessThanOrEqualTo(numSlices)))); + assertThat(taskGroupToRethrottle.getChildTasks(), hasSize(allOf(greaterThanOrEqualTo(1), lessThanOrEqualTo(numSlices)))); // Wait for all of the sub tasks to start (or finish, some might finish early, all that matters is that not all do) assertBusy(() -> { - BulkByScrollTask.Status parent = (BulkByScrollTask.Status) client().admin().cluster().prepareGetTask(taskToRethrottle).get() - .getTask().getTask().getStatus(); + BulkByScrollTask.Status parent = (BulkByScrollTask.Status) client().admin() + .cluster() + .prepareGetTask(taskToRethrottle) + .get() + .getTask() + .getTask() + .getStatus(); long finishedSubTasks = parent.getSliceStatuses().stream().filter(Objects::nonNull).count(); ListTasksResponse list = client().admin().cluster().prepareListTasks().setParentTaskId(taskToRethrottle).get(); list.rethrowFailures("subtasks"); @@ -123,14 +127,17 @@ private void testCase(AbstractBulkByScrollRequestBuilder request, String a } else { /* Check that at least one slice was rethrottled. We won't always rethrottle all of them because they might have completed. * With multiple slices these numbers might not add up perfectly, thus the 1.01F. */ - long unfinished = status.getSliceStatuses().stream() - .filter(Objects::nonNull) - .filter(slice -> slice.getStatus().getTotal() > slice.getStatus().getSuccessfullyProcessed()) - .count(); - float maxExpectedSliceRequestsPerSecond = newRequestsPerSecond == Float.POSITIVE_INFINITY ? - Float.POSITIVE_INFINITY : (newRequestsPerSecond / unfinished) * 1.01F; - float minExpectedSliceRequestsPerSecond = newRequestsPerSecond == Float.POSITIVE_INFINITY ? - Float.POSITIVE_INFINITY : (newRequestsPerSecond / numSlices) * 0.99F; + long unfinished = status.getSliceStatuses() + .stream() + .filter(Objects::nonNull) + .filter(slice -> slice.getStatus().getTotal() > slice.getStatus().getSuccessfullyProcessed()) + .count(); + float maxExpectedSliceRequestsPerSecond = newRequestsPerSecond == Float.POSITIVE_INFINITY + ? Float.POSITIVE_INFINITY + : (newRequestsPerSecond / unfinished) * 1.01F; + float minExpectedSliceRequestsPerSecond = newRequestsPerSecond == Float.POSITIVE_INFINITY + ? Float.POSITIVE_INFINITY + : (newRequestsPerSecond / numSlices) * 0.99F; boolean oneSliceRethrottled = false; float totalRequestsPerSecond = 0; for (BulkByScrollTask.StatusOrException statusOrException : status.getSliceStatuses()) { @@ -143,11 +150,15 @@ private void testCase(AbstractBulkByScrollRequestBuilder request, String a BulkByScrollTask.Status slice = statusOrException.getStatus(); if (slice.getTotal() > slice.getSuccessfullyProcessed()) { // This slice reports as not having completed so it should have been processed. - assertThat(slice.getRequestsPerSecond(), both(greaterThanOrEqualTo(minExpectedSliceRequestsPerSecond)) - .and(lessThanOrEqualTo(maxExpectedSliceRequestsPerSecond))); + assertThat( + slice.getRequestsPerSecond(), + both(greaterThanOrEqualTo(minExpectedSliceRequestsPerSecond)).and( + lessThanOrEqualTo(maxExpectedSliceRequestsPerSecond) + ) + ); } if (minExpectedSliceRequestsPerSecond <= slice.getRequestsPerSecond() - && slice.getRequestsPerSecond() <= maxExpectedSliceRequestsPerSecond) { + && slice.getRequestsPerSecond() <= maxExpectedSliceRequestsPerSecond) { oneSliceRethrottled = true; } totalRequestsPerSecond += slice.getRequestsPerSecond(); @@ -167,8 +178,11 @@ private void testCase(AbstractBulkByScrollRequestBuilder request, String a BulkByScrollResponse response = responseListener.get(); // It'd be bad if the entire require completed in a single batch. The test wouldn't be testing anything. - assertThat("Entire request completed in a single batch. This may invalidate the test as throttling is done between batches.", - response.getBatches(), greaterThanOrEqualTo(numSlices)); + assertThat( + "Entire request completed in a single batch. This may invalidate the test as throttling is done between batches.", + response.getBatches(), + greaterThanOrEqualTo(numSlices) + ); } private ListTasksResponse rethrottleTask(TaskId taskToRethrottle, float newRequestsPerSecond) throws Exception { @@ -178,8 +192,7 @@ private ListTasksResponse rethrottleTask(TaskId taskToRethrottle, float newReque assertBusy(() -> { try { - ListTasksResponse rethrottleResponse = rethrottle() - .setTaskId(taskToRethrottle) + ListTasksResponse rethrottleResponse = rethrottle().setTaskId(taskToRethrottle) .setRequestsPerSecond(newRequestsPerSecond) .get(); rethrottleResponse.rethrowFailures("Rethrottle"); @@ -191,8 +204,14 @@ private ListTasksResponse rethrottleTask(TaskId taskToRethrottle, float newReque throw e; } // We want to retry in this case so we throw an assertion error - assertThat(unwrapped.getMessage(), equalTo("task [" + taskToRethrottle.getId() - + "] has not yet been initialized to the point where it knows how to rethrottle itself")); + assertThat( + unwrapped.getMessage(), + equalTo( + "task [" + + taskToRethrottle.getId() + + "] has not yet been initialized to the point where it knows how to rethrottle itself" + ) + ); logger.info("caught unprepared task, retrying until prepared"); throw new AssertionError("Rethrottle request for task [" + taskToRethrottle.getId() + "] failed", e); } @@ -206,8 +225,7 @@ private TaskGroup findTaskToRethrottle(String actionName, int sliceCount) { do { ListTasksResponse tasks = client().admin().cluster().prepareListTasks().setActions(actionName).setDetailed(true).get(); tasks.rethrowFailures("Finding tasks to rethrottle"); - assertThat("tasks are left over from the last execution of this test", - tasks.getTaskGroups(), hasSize(lessThan(2))); + assertThat("tasks are left over from the last execution of this test", tasks.getTaskGroups(), hasSize(lessThan(2))); if (0 == tasks.getTaskGroups().size()) { // The parent task hasn't started yet continue; @@ -221,11 +239,14 @@ private TaskGroup findTaskToRethrottle(String actionName, int sliceCount) { * (maybe even empty!) that complete super fast so we have to * count them too. */ - long finishedChildStatuses = status.getSliceStatuses().stream() - .filter(n -> n != null) - .count(); - logger.info("Expected [{}] total children, [{}] are running and [{}] are finished\n{}", - sliceCount, taskGroup.getChildTasks().size(), finishedChildStatuses, status.getSliceStatuses()); + long finishedChildStatuses = status.getSliceStatuses().stream().filter(n -> n != null).count(); + logger.info( + "Expected [{}] total children, [{}] are running and [{}] are finished\n{}", + sliceCount, + taskGroup.getChildTasks().size(), + finishedChildStatuses, + status.getSliceStatuses() + ); if (sliceCount == finishedChildStatuses) { fail("all slices finished:\n" + status); } @@ -235,7 +256,8 @@ private TaskGroup findTaskToRethrottle(String actionName, int sliceCount) { } return taskGroup; } while (System.nanoTime() - start < TimeUnit.SECONDS.toNanos(10)); - throw new AssertionError("Couldn't find tasks to rethrottle. Here are the running tasks " + - client().admin().cluster().prepareListTasks().get()); + throw new AssertionError( + "Couldn't find tasks to rethrottle. Here are the running tasks " + client().admin().cluster().prepareListTasks().get() + ); } } diff --git a/modules/reindex/src/test/java/org/elasticsearch/reindex/RetryTests.java b/modules/reindex/src/test/java/org/elasticsearch/reindex/RetryTests.java index 14a9506218759..ec70cf571ed66 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/reindex/RetryTests.java +++ b/modules/reindex/src/test/java/org/elasticsearch/reindex/RetryTests.java @@ -63,16 +63,14 @@ public class RetryTests extends ESIntegTestCase { @After public void forceUnblockAllExecutors() { - for (CyclicBarrier barrier: blockedExecutors) { + for (CyclicBarrier barrier : blockedExecutors) { barrier.reset(); } } @Override protected Collection> nodePlugins() { - return Arrays.asList( - ReindexPlugin.class, - Netty4Plugin.class); + return Arrays.asList(ReindexPlugin.class, Netty4Plugin.class); } /** @@ -90,16 +88,17 @@ protected boolean addMockHttpTransport() { final Settings nodeSettings() { return Settings.builder() - // whitelist reindexing from the HTTP host we're going to use - .put(TransportReindexAction.REMOTE_CLUSTER_WHITELIST.getKey(), "127.0.0.1:*") - .build(); + // whitelist reindexing from the HTTP host we're going to use + .put(TransportReindexAction.REMOTE_CLUSTER_WHITELIST.getKey(), "127.0.0.1:*") + .build(); } public void testReindex() throws Exception { testCase( - ReindexAction.NAME, - client -> new ReindexRequestBuilder(client, ReindexAction.INSTANCE).source("source").destination("dest"), - matcher().created(DOC_COUNT)); + ReindexAction.NAME, + client -> new ReindexRequestBuilder(client, ReindexAction.INSTANCE).source("source").destination("dest"), + matcher().created(DOC_COUNT) + ); } public void testReindexFromRemote() throws Exception { @@ -117,51 +116,66 @@ public void testReindexFromRemote() throws Exception { assertNotNull(masterNode); TransportAddress address = masterNode.getInfo(HttpInfo.class).getAddress().publishAddress(); - RemoteInfo remote = - new RemoteInfo("http", address.getAddress(), address.getPort(), null, - new BytesArray("{\"match_all\":{}}"), null, null, emptyMap(), - RemoteInfo.DEFAULT_SOCKET_TIMEOUT, RemoteInfo.DEFAULT_CONNECT_TIMEOUT); - ReindexRequestBuilder request = new ReindexRequestBuilder(client, ReindexAction.INSTANCE).source("source").destination("dest") - .setRemoteInfo(remote); + RemoteInfo remote = new RemoteInfo( + "http", + address.getAddress(), + address.getPort(), + null, + new BytesArray("{\"match_all\":{}}"), + null, + null, + emptyMap(), + RemoteInfo.DEFAULT_SOCKET_TIMEOUT, + RemoteInfo.DEFAULT_CONNECT_TIMEOUT + ); + ReindexRequestBuilder request = new ReindexRequestBuilder(client, ReindexAction.INSTANCE).source("source") + .destination("dest") + .setRemoteInfo(remote); return request; }; testCase(ReindexAction.NAME, function, matcher().created(DOC_COUNT)); } public void testUpdateByQuery() throws Exception { - testCase(UpdateByQueryAction.NAME, client -> new UpdateByQueryRequestBuilder(client, UpdateByQueryAction.INSTANCE).source("source"), - matcher().updated(DOC_COUNT)); + testCase( + UpdateByQueryAction.NAME, + client -> new UpdateByQueryRequestBuilder(client, UpdateByQueryAction.INSTANCE).source("source"), + matcher().updated(DOC_COUNT) + ); } public void testDeleteByQuery() throws Exception { - testCase(DeleteByQueryAction.NAME, client -> new DeleteByQueryRequestBuilder(client, DeleteByQueryAction.INSTANCE).source("source") - .filter(QueryBuilders.matchAllQuery()), matcher().deleted(DOC_COUNT)); + testCase( + DeleteByQueryAction.NAME, + client -> new DeleteByQueryRequestBuilder(client, DeleteByQueryAction.INSTANCE).source("source") + .filter(QueryBuilders.matchAllQuery()), + matcher().deleted(DOC_COUNT) + ); } private void testCase( - String action, - Function> request, - BulkIndexByScrollResponseMatcher matcher) - throws Exception { + String action, + Function> request, + BulkIndexByScrollResponseMatcher matcher + ) throws Exception { /* * These test cases work by stuffing the bulk queue of a single node and * making sure that we read and write from that node. */ final Settings nodeSettings = Settings.builder() - // use pools of size 1 so we can block them - .put("thread_pool.write.size", 1) - // use queues of size 1 because size 0 is broken and because bulk requests need the queue to function - .put("thread_pool.write.queue_size", 1) - .put("node.attr.color", "blue") - .build(); + // use pools of size 1 so we can block them + .put("thread_pool.write.size", 1) + // use queues of size 1 because size 0 is broken and because bulk requests need the queue to function + .put("thread_pool.write.queue_size", 1) + .put("node.attr.color", "blue") + .build(); final String node = internalCluster().startDataOnlyNode(nodeSettings); - final Settings indexSettings = - Settings.builder() - .put("index.number_of_shards", 1) - .put("index.number_of_replicas", 0) - .put("index.routing.allocation.include.color", "blue") - .build(); + final Settings indexSettings = Settings.builder() + .put("index.number_of_shards", 1) + .put("index.number_of_replicas", 0) + .put("index.routing.allocation.include.color", "blue") + .build(); // Create the source index on the node with small thread pools so we can block them. client().admin().indices().prepareCreate("source").setSettings(indexSettings).execute().actionGet(); diff --git a/modules/reindex/src/test/java/org/elasticsearch/reindex/RoundTripTests.java b/modules/reindex/src/test/java/org/elasticsearch/reindex/RoundTripTests.java index 307243c0d2b69..073c0a68a5400 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/reindex/RoundTripTests.java +++ b/modules/reindex/src/test/java/org/elasticsearch/reindex/RoundTripTests.java @@ -59,8 +59,19 @@ public void testReindexRequest() throws IOException { TimeValue socketTimeout = parseTimeValue(randomPositiveTimeValue(), "socketTimeout"); TimeValue connectTimeout = parseTimeValue(randomPositiveTimeValue(), "connectTimeout"); reindex.setRemoteInfo( - new RemoteInfo(randomAlphaOfLength(5), randomAlphaOfLength(5), port, null, - query, username, password, headers, socketTimeout, connectTimeout)); + new RemoteInfo( + randomAlphaOfLength(5), + randomAlphaOfLength(5), + port, + null, + query, + username, + password, + headers, + socketTimeout, + connectTimeout + ) + ); } ReindexRequest tripped = new ReindexRequest(toInputByteStream(reindex)); assertRequestEquals(reindex, tripped); @@ -140,8 +151,7 @@ private void assertRequestEquals(ReindexRequest request, ReindexRequest tripped) } } - private void assertRequestEquals(AbstractBulkIndexByScrollRequest request, - AbstractBulkIndexByScrollRequest tripped) { + private void assertRequestEquals(AbstractBulkIndexByScrollRequest request, AbstractBulkIndexByScrollRequest tripped) { assertRequestEquals((AbstractBulkByScrollRequest) request, (AbstractBulkByScrollRequest) tripped); assertEquals(request.getScript(), tripped.getScript()); } diff --git a/modules/reindex/src/test/java/org/elasticsearch/reindex/TransportRethrottleActionTests.java b/modules/reindex/src/test/java/org/elasticsearch/reindex/TransportRethrottleActionTests.java index b85b81ecf06f9..1359dd394e51e 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/reindex/TransportRethrottleActionTests.java +++ b/modules/reindex/src/test/java/org/elasticsearch/reindex/TransportRethrottleActionTests.java @@ -15,9 +15,6 @@ import org.elasticsearch.client.Client; import org.elasticsearch.index.reindex.BulkByScrollResponse; import org.elasticsearch.index.reindex.BulkByScrollTask; -import org.elasticsearch.reindex.RethrottleAction; -import org.elasticsearch.reindex.RethrottleRequest; -import org.elasticsearch.reindex.TransportRethrottleAction; import org.elasticsearch.tasks.TaskId; import org.elasticsearch.tasks.TaskInfo; import org.elasticsearch.test.ESTestCase; @@ -58,8 +55,11 @@ public void createTask() { * @param simulator simulate a response from the sub-request to rethrottle the child requests * @param verifier verify the resulting response */ - private void rethrottleTestCase(int runningSlices, Consumer> simulator, - Consumer> verifier) { + private void rethrottleTestCase( + int runningSlices, + Consumer> simulator, + Consumer> verifier + ) { Client client = mock(Client.class); String localNodeId = randomAlphaOfLength(5); float newRequestsPerSecond = randomValueOtherThanMany(f -> f <= 0, () -> randomFloat()); @@ -84,7 +84,8 @@ private void rethrottleTestCase(int runningSlices, Consumer> expectSuccessfulRethrottleWithStatuses( - List sliceStatuses) { + List sliceStatuses + ) { return listener -> { TaskInfo taskInfo = captureResponse(TaskInfo.class, listener); assertEquals(sliceStatuses, ((BulkByScrollTask.Status) taskInfo.getStatus()).getSliceStatuses()); @@ -96,7 +97,8 @@ public void testRethrottleSuccessfulResponse() { List sliceStatuses = new ArrayList<>(slices); for (int i = 0; i < slices; i++) { BulkByScrollTask.Status status = believeableInProgressStatus(i); - tasks.add(new TaskInfo( + tasks.add( + new TaskInfo( new TaskId("test", 123), "test", "test", @@ -107,12 +109,16 @@ public void testRethrottleSuccessfulResponse() { true, false, new TaskId("test", task.getId()), - Collections.emptyMap())); + Collections.emptyMap() + ) + ); sliceStatuses.add(new BulkByScrollTask.StatusOrException(status)); } - rethrottleTestCase(slices, - listener -> listener.onResponse(new ListTasksResponse(tasks, emptyList(), emptyList())), - expectSuccessfulRethrottleWithStatuses(sliceStatuses)); + rethrottleTestCase( + slices, + listener -> listener.onResponse(new ListTasksResponse(tasks, emptyList(), emptyList())), + expectSuccessfulRethrottleWithStatuses(sliceStatuses) + ); } public void testRethrottleWithSomeSucceeded() { @@ -120,14 +126,15 @@ public void testRethrottleWithSomeSucceeded() { List sliceStatuses = new ArrayList<>(slices); for (int i = 0; i < succeeded; i++) { BulkByScrollTask.Status status = believeableCompletedStatus(i); - task.getLeaderState().onSliceResponse(neverCalled(), i, - new BulkByScrollResponse(timeValueMillis(10), status, emptyList(), emptyList(), false)); + task.getLeaderState() + .onSliceResponse(neverCalled(), i, new BulkByScrollResponse(timeValueMillis(10), status, emptyList(), emptyList(), false)); sliceStatuses.add(new BulkByScrollTask.StatusOrException(status)); } List tasks = new ArrayList<>(); for (int i = succeeded; i < slices; i++) { BulkByScrollTask.Status status = believeableInProgressStatus(i); - tasks.add(new TaskInfo( + tasks.add( + new TaskInfo( new TaskId("test", 123), "test", "test", @@ -138,12 +145,16 @@ public void testRethrottleWithSomeSucceeded() { true, false, new TaskId("test", task.getId()), - Collections.emptyMap())); + Collections.emptyMap() + ) + ); sliceStatuses.add(new BulkByScrollTask.StatusOrException(status)); } - rethrottleTestCase(slices - succeeded, - listener -> listener.onResponse(new ListTasksResponse(tasks, emptyList(), emptyList())), - expectSuccessfulRethrottleWithStatuses(sliceStatuses)); + rethrottleTestCase( + slices - succeeded, + listener -> listener.onResponse(new ListTasksResponse(tasks, emptyList(), emptyList())), + expectSuccessfulRethrottleWithStatuses(sliceStatuses) + ); } public void testRethrottleWithAllSucceeded() { @@ -152,17 +163,19 @@ public void testRethrottleWithAllSucceeded() { @SuppressWarnings("unchecked") ActionListener listener = i < slices - 1 ? neverCalled() : mock(ActionListener.class); BulkByScrollTask.Status status = believeableCompletedStatus(i); - task.getLeaderState().onSliceResponse(listener, i, new BulkByScrollResponse(timeValueMillis(10), status, emptyList(), - emptyList(), false)); + task.getLeaderState() + .onSliceResponse(listener, i, new BulkByScrollResponse(timeValueMillis(10), status, emptyList(), emptyList(), false)); if (i == slices - 1) { // The whole thing succeeded so we should have got the success captureResponse(BulkByScrollResponse.class, listener).getStatus(); } sliceStatuses.add(new BulkByScrollTask.StatusOrException(status)); } - rethrottleTestCase(0, - listener -> { /* There are no async tasks to simulate because the listener is called for us. */}, - expectSuccessfulRethrottleWithStatuses(sliceStatuses)); + rethrottleTestCase( + 0, + listener -> { /* There are no async tasks to simulate because the listener is called for us. */}, + expectSuccessfulRethrottleWithStatuses(sliceStatuses) + ); } private Consumer> expectException(Matcher exceptionMatcher) { @@ -181,16 +194,20 @@ public void testRethrottleCatastrophicFailures() { public void testRethrottleTaskOperationFailure() { Exception e = new Exception(); TaskOperationFailure failure = new TaskOperationFailure("test", 123, e); - rethrottleTestCase(slices, - listener -> listener.onResponse(new ListTasksResponse(emptyList(), singletonList(failure), emptyList())), - expectException(hasToString(containsString("Rethrottle of [test:123] failed")))); + rethrottleTestCase( + slices, + listener -> listener.onResponse(new ListTasksResponse(emptyList(), singletonList(failure), emptyList())), + expectException(hasToString(containsString("Rethrottle of [test:123] failed"))) + ); } public void testRethrottleNodeFailure() { FailedNodeException e = new FailedNodeException("test", "test", new Exception()); - rethrottleTestCase(slices, - listener -> listener.onResponse(new ListTasksResponse(emptyList(), emptyList(), singletonList(e))), - expectException(theInstance(e))); + rethrottleTestCase( + slices, + listener -> listener.onResponse(new ListTasksResponse(emptyList(), emptyList(), singletonList(e))), + expectException(theInstance(e)) + ); } private BulkByScrollTask.Status believeableInProgressStatus(Integer sliceId) { diff --git a/modules/reindex/src/test/java/org/elasticsearch/reindex/UpdateByQueryBasicTests.java b/modules/reindex/src/test/java/org/elasticsearch/reindex/UpdateByQueryBasicTests.java index 17d93516b260e..b54fc40cf719e 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/reindex/UpdateByQueryBasicTests.java +++ b/modules/reindex/src/test/java/org/elasticsearch/reindex/UpdateByQueryBasicTests.java @@ -27,10 +27,13 @@ public class UpdateByQueryBasicTests extends ReindexTestCase { public void testBasics() throws Exception { - indexRandom(true, client().prepareIndex("test").setId("1").setSource("foo", "a"), - client().prepareIndex("test").setId("2").setSource("foo", "a"), - client().prepareIndex("test").setId("3").setSource("foo", "b"), - client().prepareIndex("test").setId("4").setSource("foo", "c")); + indexRandom( + true, + client().prepareIndex("test").setId("1").setSource("foo", "a"), + client().prepareIndex("test").setId("2").setSource("foo", "a"), + client().prepareIndex("test").setId("3").setSource("foo", "b"), + client().prepareIndex("test").setId("4").setSource("foo", "c") + ); assertHitCount(client().prepareSearch("test").setSize(0).get(), 4); assertEquals(1, client().prepareGet("test", "1").get().getVersion()); assertEquals(1, client().prepareGet("test", "4").get().getVersion()); @@ -64,11 +67,13 @@ public void testBasics() throws Exception { } public void testSlices() throws Exception { - indexRandom(true, + indexRandom( + true, client().prepareIndex("test").setId("1").setSource("foo", "a"), client().prepareIndex("test").setId("2").setSource("foo", "a"), client().prepareIndex("test").setId("3").setSource("foo", "b"), - client().prepareIndex("test").setId("4").setSource("foo", "c")); + client().prepareIndex("test").setId("4").setSource("foo", "c") + ); assertHitCount(client().prepareSearch("test").setSize(0).get(), 4); assertEquals(1, client().prepareGet("test", "1").get().getVersion()); assertEquals(1, client().prepareGet("test", "4").get().getVersion()); @@ -78,39 +83,25 @@ public void testSlices() throws Exception { // Reindex all the docs assertThat( - updateByQuery() - .source("test") - .refresh(true) - .setSlices(slices).get(), - matcher() - .updated(4) - .slices(hasSize(expectedSlices))); + updateByQuery().source("test").refresh(true).setSlices(slices).get(), + matcher().updated(4).slices(hasSize(expectedSlices)) + ); assertEquals(2, client().prepareGet("test", "1").get().getVersion()); assertEquals(2, client().prepareGet("test", "4").get().getVersion()); // Now none of them assertThat( - updateByQuery() - .source("test") - .filter(termQuery("foo", "no_match")) - .setSlices(slices) - .refresh(true).get(), - matcher() - .updated(0) - .slices(hasSize(expectedSlices))); + updateByQuery().source("test").filter(termQuery("foo", "no_match")).setSlices(slices).refresh(true).get(), + matcher().updated(0).slices(hasSize(expectedSlices)) + ); assertEquals(2, client().prepareGet("test", "1").get().getVersion()); assertEquals(2, client().prepareGet("test", "4").get().getVersion()); // Now half of them assertThat( - updateByQuery() - .source("test") - .filter(termQuery("foo", "a")) - .refresh(true) - .setSlices(slices).get(), - matcher() - .updated(2) - .slices(hasSize(expectedSlices))); + updateByQuery().source("test").filter(termQuery("foo", "a")).refresh(true).setSlices(slices).get(), + matcher().updated(2).slices(hasSize(expectedSlices)) + ); assertEquals(3, client().prepareGet("test", "1").get().getVersion()); assertEquals(3, client().prepareGet("test", "2").get().getVersion()); assertEquals(2, client().prepareGet("test", "3").get().getVersion()); @@ -152,8 +143,7 @@ public void testMultipleSources() throws Exception { } public void testMissingSources() { - BulkByScrollResponse response = updateByQuery() - .source("missing-index-*") + BulkByScrollResponse response = updateByQuery().source("missing-index-*") .refresh(true) .setSlices(AbstractBulkByScrollRequest.AUTO_SLICES) .get(); diff --git a/modules/reindex/src/test/java/org/elasticsearch/reindex/UpdateByQueryMetadataTests.java b/modules/reindex/src/test/java/org/elasticsearch/reindex/UpdateByQueryMetadataTests.java index 10620a46ca516..cdad8316702d0 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/reindex/UpdateByQueryMetadataTests.java +++ b/modules/reindex/src/test/java/org/elasticsearch/reindex/UpdateByQueryMetadataTests.java @@ -14,8 +14,9 @@ import org.elasticsearch.index.reindex.ScrollableHitSource.Hit; import org.elasticsearch.index.reindex.UpdateByQueryRequest; -public class UpdateByQueryMetadataTests - extends AbstractAsyncBulkByScrollActionMetadataTestCase { +public class UpdateByQueryMetadataTests extends AbstractAsyncBulkByScrollActionMetadataTestCase< + UpdateByQueryRequest, + BulkByScrollResponse> { public void testRoutingIsCopied() { IndexRequest index = new IndexRequest(); @@ -35,13 +36,23 @@ protected UpdateByQueryRequest request() { private class TestAction extends TransportUpdateByQueryAction.AsyncIndexBySearchAction { TestAction() { - super(UpdateByQueryMetadataTests.this.task, UpdateByQueryMetadataTests.this.logger, null, - UpdateByQueryMetadataTests.this.threadPool, null, request(), ClusterState.EMPTY_STATE, listener()); + super( + UpdateByQueryMetadataTests.this.task, + UpdateByQueryMetadataTests.this.logger, + null, + UpdateByQueryMetadataTests.this.threadPool, + null, + request(), + ClusterState.EMPTY_STATE, + listener() + ); } @Override - public AbstractAsyncBulkByScrollAction.RequestWrapper copyMetadata(AbstractAsyncBulkByScrollAction.RequestWrapper request, - Hit doc) { + public AbstractAsyncBulkByScrollAction.RequestWrapper copyMetadata( + AbstractAsyncBulkByScrollAction.RequestWrapper request, + Hit doc + ) { return super.copyMetadata(request, doc); } } diff --git a/modules/reindex/src/test/java/org/elasticsearch/reindex/UpdateByQueryWhileModifyingTests.java b/modules/reindex/src/test/java/org/elasticsearch/reindex/UpdateByQueryWhileModifyingTests.java index c6fa330bc318d..b862b78f35f27 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/reindex/UpdateByQueryWhileModifyingTests.java +++ b/modules/reindex/src/test/java/org/elasticsearch/reindex/UpdateByQueryWhileModifyingTests.java @@ -40,8 +40,10 @@ public void testUpdateWhileReindexing() throws Exception { while (keepUpdating.get()) { try { BulkByScrollResponse response = updateByQuery().source("test").refresh(true).abortOnVersionConflict(false).get(); - assertThat(response, matcher().updated(either(equalTo(0L)).or(equalTo(1L))) - .versionConflicts(either(equalTo(0L)).or(equalTo(1L)))); + assertThat( + response, + matcher().updated(either(equalTo(0L)).or(equalTo(1L))).versionConflicts(either(equalTo(0L)).or(equalTo(1L))) + ); } catch (Exception e) { failure.set(e); } @@ -54,8 +56,10 @@ public void testUpdateWhileReindexing() throws Exception { GetResponse get = client().prepareGet("test", "test").get(); assertEquals(value.get(), get.getSource().get("test")); value.set(randomSimpleString(random())); - IndexRequestBuilder index = client().prepareIndex("test").setId("test").setSource("test", value.get()) - .setRefreshPolicy(IMMEDIATE); + IndexRequestBuilder index = client().prepareIndex("test") + .setId("test") + .setSource("test", value.get()) + .setRefreshPolicy(IMMEDIATE); /* * Update by query changes the document so concurrent * indexes might get version conflict exceptions so we just @@ -70,10 +74,16 @@ public void testUpdateWhileReindexing() throws Exception { } catch (VersionConflictEngineException e) { if (attempts >= MAX_ATTEMPTS) { throw new RuntimeException( - "Failed to index after [" + MAX_ATTEMPTS + "] attempts. Too many version conflicts!"); + "Failed to index after [" + MAX_ATTEMPTS + "] attempts. Too many version conflicts!" + ); } - logger.info("Caught expected version conflict trying to perform mutation number [{}] with version [{}] " - + "on attempt [{}]. Retrying.", i, get.getVersion(), attempts); + logger.info( + "Caught expected version conflict trying to perform mutation number [{}] with version [{}] " + + "on attempt [{}]. Retrying.", + i, + get.getVersion(), + attempts + ); get = client().prepareGet("test", "test").get(); } } diff --git a/modules/reindex/src/test/java/org/elasticsearch/reindex/UpdateByQueryWithScriptTests.java b/modules/reindex/src/test/java/org/elasticsearch/reindex/UpdateByQueryWithScriptTests.java index f4d04adfa21d7..1dbf9d978a1d5 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/reindex/UpdateByQueryWithScriptTests.java +++ b/modules/reindex/src/test/java/org/elasticsearch/reindex/UpdateByQueryWithScriptTests.java @@ -22,8 +22,9 @@ import static org.hamcrest.Matchers.containsString; import static org.mockito.Mockito.mock; -public class UpdateByQueryWithScriptTests - extends AbstractAsyncBulkByScrollActionScriptTestCase { +public class UpdateByQueryWithScriptTests extends AbstractAsyncBulkByScrollActionScriptTestCase< + UpdateByQueryRequest, + BulkByScrollResponse> { public void testModifyingCtxNotAllowed() { /* @@ -32,8 +33,8 @@ public void testModifyingCtxNotAllowed() { * more. The point of have many is that they should all present the same * error message to the user, not some ClassCastException. */ - Object[] options = new Object[] {"cat", new Object(), 123, new Date(), Math.PI}; - for (String ctxVar: new String[] {"_index", "_id", "_version", "_routing"}) { + Object[] options = new Object[] { "cat", new Object(), 123, new Date(), Math.PI }; + for (String ctxVar : new String[] { "_index", "_id", "_version", "_routing" }) { try { applyScript((Map ctx) -> ctx.put(ctxVar, randomFrom(options))); } catch (IllegalArgumentException e) { @@ -50,9 +51,23 @@ protected UpdateByQueryRequest request() { @Override protected TransportUpdateByQueryAction.AsyncIndexBySearchAction action(ScriptService scriptService, UpdateByQueryRequest request) { TransportService transportService = mock(TransportService.class); - TransportUpdateByQueryAction transportAction = new TransportUpdateByQueryAction(threadPool, - new ActionFilters(Collections.emptySet()), null, transportService, scriptService, null); - return new TransportUpdateByQueryAction.AsyncIndexBySearchAction(task, logger, null, threadPool, scriptService, request, - ClusterState.EMPTY_STATE, listener()); + TransportUpdateByQueryAction transportAction = new TransportUpdateByQueryAction( + threadPool, + new ActionFilters(Collections.emptySet()), + null, + transportService, + scriptService, + null + ); + return new TransportUpdateByQueryAction.AsyncIndexBySearchAction( + task, + logger, + null, + threadPool, + scriptService, + request, + ClusterState.EMPTY_STATE, + listener() + ); } } diff --git a/modules/reindex/src/test/java/org/elasticsearch/reindex/remote/RemoteInfoTests.java b/modules/reindex/src/test/java/org/elasticsearch/reindex/remote/RemoteInfoTests.java index e5d53591b96d6..b7eaff3d437b4 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/reindex/remote/RemoteInfoTests.java +++ b/modules/reindex/src/test/java/org/elasticsearch/reindex/remote/RemoteInfoTests.java @@ -16,20 +16,37 @@ public class RemoteInfoTests extends ESTestCase { private RemoteInfo newRemoteInfo(String scheme, String prefixPath, String username, String password) { - return new RemoteInfo(scheme, "testhost", 12344, prefixPath,new BytesArray("{ \"foo\" : \"bar\" }"), username, password, - emptyMap(), RemoteInfo.DEFAULT_SOCKET_TIMEOUT, RemoteInfo.DEFAULT_CONNECT_TIMEOUT); + return new RemoteInfo( + scheme, + "testhost", + 12344, + prefixPath, + new BytesArray("{ \"foo\" : \"bar\" }"), + username, + password, + emptyMap(), + RemoteInfo.DEFAULT_SOCKET_TIMEOUT, + RemoteInfo.DEFAULT_CONNECT_TIMEOUT + ); } public void testToString() { - assertEquals("host=testhost port=12344 query={ \"foo\" : \"bar\" }", - newRemoteInfo("http", null, null, null).toString()); - assertEquals("host=testhost port=12344 query={ \"foo\" : \"bar\" } username=testuser", - newRemoteInfo("http", null, "testuser", null).toString()); - assertEquals("host=testhost port=12344 query={ \"foo\" : \"bar\" } username=testuser password=<<>>", - newRemoteInfo("http", null, "testuser", "testpass").toString()); - assertEquals("scheme=https host=testhost port=12344 query={ \"foo\" : \"bar\" } username=testuser password=<<>>", - newRemoteInfo("https", null, "testuser", "testpass").toString()); - assertEquals("scheme=https host=testhost port=12344 pathPrefix=prxy query={ \"foo\" : \"bar\" } username=testuser password=<<>>", - newRemoteInfo("https", "prxy", "testuser", "testpass").toString()); + assertEquals("host=testhost port=12344 query={ \"foo\" : \"bar\" }", newRemoteInfo("http", null, null, null).toString()); + assertEquals( + "host=testhost port=12344 query={ \"foo\" : \"bar\" } username=testuser", + newRemoteInfo("http", null, "testuser", null).toString() + ); + assertEquals( + "host=testhost port=12344 query={ \"foo\" : \"bar\" } username=testuser password=<<>>", + newRemoteInfo("http", null, "testuser", "testpass").toString() + ); + assertEquals( + "scheme=https host=testhost port=12344 query={ \"foo\" : \"bar\" } username=testuser password=<<>>", + newRemoteInfo("https", null, "testuser", "testpass").toString() + ); + assertEquals( + "scheme=https host=testhost port=12344 pathPrefix=prxy query={ \"foo\" : \"bar\" } username=testuser password=<<>>", + newRemoteInfo("https", "prxy", "testuser", "testpass").toString() + ); } } diff --git a/modules/reindex/src/test/java/org/elasticsearch/reindex/remote/RemoteRequestBuildersTests.java b/modules/reindex/src/test/java/org/elasticsearch/reindex/remote/RemoteRequestBuildersTests.java index 5ef68da64d79d..0077e0555ff88 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/reindex/remote/RemoteRequestBuildersTests.java +++ b/modules/reindex/src/test/java/org/elasticsearch/reindex/remote/RemoteRequestBuildersTests.java @@ -17,7 +17,6 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.Streams; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.reindex.remote.RemoteRequestBuilders; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.test.ESTestCase; @@ -65,8 +64,10 @@ public void testIntialSearchPath() { assertEquals("/cat%2F,dog/_search", initialSearch(searchRequest, query, remoteVersion).getEndpoint()); // test a specific date math + all characters that need escaping. searchRequest.indices("", "<>/{}|+:,"); - assertEquals("/%3Ccat%7Bnow%2Fd%7D%3E,%3C%3E%2F%7B%7D%7C%2B%3A%2C/_search", - initialSearch(searchRequest, query, remoteVersion).getEndpoint()); + assertEquals( + "/%3Ccat%7Bnow%2Fd%7D%3E,%3C%3E%2F%7B%7D%7C%2B%3A%2C/_search", + initialSearch(searchRequest, query, remoteVersion).getEndpoint() + ); // re-escape already escaped (no special handling). searchRequest.indices("%2f", "%3a"); @@ -103,8 +104,10 @@ public void testInitialSearchParamsFields() { // Test request without any fields Version remoteVersion = Version.fromId(between(2000099, Version.CURRENT.id)); - assertThat(initialSearch(searchRequest, query, remoteVersion).getParameters(), - not(either(hasKey("stored_fields")).or(hasKey("fields")))); + assertThat( + initialSearch(searchRequest, query, remoteVersion).getParameters(), + not(either(hasKey("stored_fields")).or(hasKey("fields"))) + ); // Test stored_fields for versions that support it searchRequest = new SearchRequest().source(new SearchSourceBuilder()); @@ -125,15 +128,16 @@ public void testInitialSearchParamsFields() { searchRequest = new SearchRequest().source(new SearchSourceBuilder()); searchRequest.source().storedField("_source").storedField("_id"); remoteVersion = Version.fromId(between(0, 2000099 - 1)); - assertThat(initialSearch(searchRequest, query, remoteVersion).getParameters(), - hasEntry("fields", "_source,_id,_parent,_routing,_ttl")); + assertThat( + initialSearch(searchRequest, query, remoteVersion).getParameters(), + hasEntry("fields", "_source,_id,_parent,_routing,_ttl") + ); // But only versions before 1.0 force _source to be in the list searchRequest = new SearchRequest().source(new SearchSourceBuilder()); searchRequest.source().storedField("_id"); remoteVersion = Version.fromId(between(1000099, 2000099 - 1)); - assertThat(initialSearch(searchRequest, query, remoteVersion).getParameters(), - hasEntry("fields", "_id,_parent,_routing,_ttl")); + assertThat(initialSearch(searchRequest, query, remoteVersion).getParameters(), hasEntry("fields", "_id,_parent,_routing,_ttl")); } public void testInitialSearchParamsMisc() { @@ -163,8 +167,7 @@ public void testInitialSearchParamsMisc() { } assertThat(params, hasEntry("size", Integer.toString(size))); if (fetchVersion != null) { - assertThat(params, fetchVersion ? hasEntry("version", Boolean.TRUE.toString()) : - hasEntry("version", Boolean.FALSE.toString())); + assertThat(params, fetchVersion ? hasEntry("version", Boolean.TRUE.toString()) : hasEntry("version", Boolean.FALSE.toString())); } else { assertThat(params, hasEntry("version", Boolean.FALSE.toString())); } @@ -181,7 +184,7 @@ public void testInitialSearchDisallowPartialResults() { Map params = initialSearch(searchRequest, query, disallowVersion).getParameters(); assertEquals("false", params.get(allowPartialParamName)); - Version allowVersion = Version.fromId(between(0, v6_3-1)); + Version allowVersion = Version.fromId(between(0, v6_3 - 1)); params = initialSearch(searchRequest, query, allowVersion).getParameters(); assertThat(params.keySet(), not(contains(allowPartialParamName))); } @@ -209,23 +212,31 @@ public void testInitialSearchEntity() throws IOException { HttpEntity entity = initialSearch(searchRequest, new BytesArray(query), remoteVersion).getEntity(); assertEquals(ContentType.APPLICATION_JSON.toString(), entity.getContentType().getValue()); if (remoteVersion.onOrAfter(Version.fromId(1000099))) { - assertEquals("{\"query\":" + query + ",\"_source\":true}", - Streams.copyToString(new InputStreamReader(entity.getContent(), StandardCharsets.UTF_8))); + assertEquals( + "{\"query\":" + query + ",\"_source\":true}", + Streams.copyToString(new InputStreamReader(entity.getContent(), StandardCharsets.UTF_8)) + ); } else { - assertEquals("{\"query\":" + query + "}", - Streams.copyToString(new InputStreamReader(entity.getContent(), StandardCharsets.UTF_8))); + assertEquals( + "{\"query\":" + query + "}", + Streams.copyToString(new InputStreamReader(entity.getContent(), StandardCharsets.UTF_8)) + ); } // Source filtering is included if set up - searchRequest.source().fetchSource(new String[]{"in1", "in2"}, new String[]{"out"}); + searchRequest.source().fetchSource(new String[] { "in1", "in2" }, new String[] { "out" }); entity = initialSearch(searchRequest, new BytesArray(query), remoteVersion).getEntity(); assertEquals(ContentType.APPLICATION_JSON.toString(), entity.getContentType().getValue()); - assertEquals("{\"query\":" + query + ",\"_source\":{\"includes\":[\"in1\",\"in2\"],\"excludes\":[\"out\"]}}", - Streams.copyToString(new InputStreamReader(entity.getContent(), StandardCharsets.UTF_8))); + assertEquals( + "{\"query\":" + query + ",\"_source\":{\"includes\":[\"in1\",\"in2\"],\"excludes\":[\"out\"]}}", + Streams.copyToString(new InputStreamReader(entity.getContent(), StandardCharsets.UTF_8)) + ); // Invalid XContent fails - RuntimeException e = expectThrows(RuntimeException.class, - () -> initialSearch(searchRequest, new BytesArray("{}, \"trailing\": {}"), remoteVersion)); + RuntimeException e = expectThrows( + RuntimeException.class, + () -> initialSearch(searchRequest, new BytesArray("{}, \"trailing\": {}"), remoteVersion) + ); assertThat(e.getCause().getMessage(), containsString("Unexpected character (',' (code 44))")); e = expectThrows(RuntimeException.class, () -> initialSearch(searchRequest, new BytesArray("{"), remoteVersion)); assertThat(e.getCause().getMessage(), containsString("Unexpected end-of-input")); @@ -242,8 +253,10 @@ public void testScrollEntity() throws IOException { String scroll = randomAlphaOfLength(30); HttpEntity entity = scroll(scroll, timeValueMillis(between(1, 1000)), Version.fromString("5.0.0")).getEntity(); assertEquals(ContentType.APPLICATION_JSON.toString(), entity.getContentType().getValue()); - assertThat(Streams.copyToString(new InputStreamReader(entity.getContent(), StandardCharsets.UTF_8)), - containsString("\"" + scroll + "\"")); + assertThat( + Streams.copyToString(new InputStreamReader(entity.getContent(), StandardCharsets.UTF_8)), + containsString("\"" + scroll + "\"") + ); // Test with version < 2.0.0 entity = scroll(scroll, timeValueMillis(between(1, 1000)), Version.fromId(1070499)).getEntity(); @@ -255,8 +268,10 @@ public void testClearScroll() throws IOException { String scroll = randomAlphaOfLength(30); Request request = clearScroll(scroll, Version.fromString("5.0.0")); assertEquals(ContentType.APPLICATION_JSON.toString(), request.getEntity().getContentType().getValue()); - assertThat(Streams.copyToString(new InputStreamReader(request.getEntity().getContent(), StandardCharsets.UTF_8)), - containsString("\"" + scroll + "\"")); + assertThat( + Streams.copyToString(new InputStreamReader(request.getEntity().getContent(), StandardCharsets.UTF_8)), + containsString("\"" + scroll + "\"") + ); assertThat(request.getParameters().keySet(), empty()); // Test with version < 2.0.0 diff --git a/modules/reindex/src/test/java/org/elasticsearch/reindex/remote/RemoteResponseParsersTests.java b/modules/reindex/src/test/java/org/elasticsearch/reindex/remote/RemoteResponseParsersTests.java index 685ccba64369c..1c4dd381729aa 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/reindex/remote/RemoteResponseParsersTests.java +++ b/modules/reindex/src/test/java/org/elasticsearch/reindex/remote/RemoteResponseParsersTests.java @@ -10,11 +10,11 @@ import org.elasticsearch.action.search.ShardSearchFailure; import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException; +import org.elasticsearch.index.reindex.ScrollableHitSource; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.index.reindex.ScrollableHitSource; -import org.elasticsearch.test.ESTestCase; import org.hamcrest.Matchers; import java.io.IOException; diff --git a/modules/reindex/src/test/java/org/elasticsearch/reindex/remote/RemoteScrollableHitSourceTests.java b/modules/reindex/src/test/java/org/elasticsearch/reindex/remote/RemoteScrollableHitSourceTests.java index 925e91941604a..fd1927554ea6c 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/reindex/remote/RemoteScrollableHitSourceTests.java +++ b/modules/reindex/src/test/java/org/elasticsearch/reindex/remote/RemoteScrollableHitSourceTests.java @@ -38,9 +38,9 @@ import org.elasticsearch.common.io.Streams; import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.EsExecutors; import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.reindex.RejectAwareActionListener; import org.elasticsearch.index.reindex.ScrollableHitSource; import org.elasticsearch.index.reindex.ScrollableHitSource.Response; @@ -139,10 +139,9 @@ public void testLookupRemoteVersion() throws Exception { private void assertLookupRemoteVersion(Version expected, String s) throws Exception { AtomicBoolean called = new AtomicBoolean(); - sourceWithMockedRemoteCall(false, ContentType.APPLICATION_JSON, s) - .lookupRemoteVersion(wrapAsListener(v -> { - assertEquals(expected, v); - called.set(true); + sourceWithMockedRemoteCall(false, ContentType.APPLICATION_JSON, s).lookupRemoteVersion(wrapAsListener(v -> { + assertEquals(expected, v); + called.set(true); })); assertTrue(called.get()); } @@ -243,10 +242,13 @@ public void testParseRejection() throws Exception { assertEquals((Integer) 0, r.getFailures().get(0).getShardId()); assertEquals("87A7NvevQxSrEwMbtRCecg", r.getFailures().get(0).getNodeId()); assertThat(r.getFailures().get(0).getReason(), instanceOf(EsRejectedExecutionException.class)); - assertEquals("rejected execution of org.elasticsearch.transport.TransportService$5@52d06af2 on " + assertEquals( + "rejected execution of org.elasticsearch.transport.TransportService$5@52d06af2 on " + "EsThreadPoolExecutor[search, queue capacity = 1000, org.elasticsearch.common.util.concurrent." + "EsThreadPoolExecutor@778ea553[Running, pool size = 7, active threads = 7, queued tasks = 1000, " - + "completed tasks = 4182]]", r.getFailures().get(0).getReason().getMessage()); + + "completed tasks = 4182]]", + r.getFailures().get(0).getReason().getMessage() + ); assertThat(r.getHits(), hasSize(1)); assertEquals("test", r.getHits().get(0).getIndex()); assertEquals("AVToMiC250DjIiBO3yJ_", r.getHits().get(0).getId()); @@ -273,8 +275,10 @@ public void testParseFailureWithStatus() throws Exception { assertEquals(null, r.getFailures().get(0).getShardId()); assertEquals(null, r.getFailures().get(0).getNodeId()); assertThat(r.getFailures().get(0).getReason(), instanceOf(RuntimeException.class)); - assertEquals("Unknown remote exception with reason=[SearchContextMissingException[No search context found for id [82]]]", - r.getFailures().get(0).getReason().getMessage()); + assertEquals( + "Unknown remote exception with reason=[SearchContextMissingException[No search context found for id [82]]]", + r.getFailures().get(0).getReason().getMessage() + ); assertThat(r.getHits(), hasSize(1)); assertEquals("test", r.getHits().get(0).getIndex()); assertEquals("10000", r.getHits().get(0).getId()); @@ -284,8 +288,11 @@ public void testParseFailureWithStatus() throws Exception { sourceWithMockedRemoteCall("failure_with_status.json").doStart(wrapAsListener(checkResponse)); assertTrue(called.get()); called.set(false); - sourceWithMockedRemoteCall("failure_with_status.json").doStartNextScroll("scroll", timeValueMillis(0), - wrapAsListener(checkResponse)); + sourceWithMockedRemoteCall("failure_with_status.json").doStartNextScroll( + "scroll", + timeValueMillis(0), + wrapAsListener(checkResponse) + ); assertTrue(called.get()); } @@ -415,8 +422,14 @@ public void testWrapExceptionToPreserveStatus() throws IOException { public void testTooLargeResponse() throws Exception { ContentTooLongException tooLong = new ContentTooLongException("too long!"); CloseableHttpAsyncClient httpClient = mock(CloseableHttpAsyncClient.class); - when(httpClient.execute(any(HttpAsyncRequestProducer.class), any(HttpAsyncResponseConsumer.class), - any(HttpClientContext.class), any(FutureCallback.class))).then(new Answer>() { + when( + httpClient.execute( + any(HttpAsyncRequestProducer.class), + any(HttpAsyncResponseConsumer.class), + any(HttpClientContext.class), + any(FutureCallback.class) + ) + ).then(new Answer>() { @Override public Future answer(InvocationOnMock invocationOnMock) throws Throwable { HeapBufferedAsyncResponseConsumer consumer = (HeapBufferedAsyncResponseConsumer) invocationOnMock.getArguments()[1]; @@ -438,8 +451,11 @@ public Future answer(InvocationOnMock invocationOnMock) throws Thr } public void testNoContentTypeIsError() { - RuntimeException e = expectListenerFailure(RuntimeException.class, (RejectAwareActionListener listener) -> - sourceWithMockedRemoteCall(false, null, "main/0_20_5.json").lookupRemoteVersion(listener)); + RuntimeException e = expectListenerFailure( + RuntimeException.class, + (RejectAwareActionListener listener) -> sourceWithMockedRemoteCall(false, null, "main/0_20_5.json") + .lookupRemoteVersion(listener) + ); assertThat(e.getMessage(), containsString("Response didn't include Content-Type: body={")); } @@ -494,8 +510,14 @@ private RemoteScrollableHitSource sourceWithMockedRemoteCall(boolean mockRemoteV } CloseableHttpAsyncClient httpClient = mock(CloseableHttpAsyncClient.class); - when(httpClient.execute(any(HttpAsyncRequestProducer.class), any(HttpAsyncResponseConsumer.class), - any(HttpClientContext.class), any(FutureCallback.class))).thenAnswer(new Answer>() { + when( + httpClient.execute( + any(HttpAsyncRequestProducer.class), + any(HttpAsyncResponseConsumer.class), + any(HttpClientContext.class), + any(FutureCallback.class) + ) + ).thenAnswer(new Answer>() { int responseCount = 0; @@ -505,7 +527,7 @@ public Future answer(InvocationOnMock invocationOnMock) throws Thr threadPool.getThreadContext().stashContext(); HttpAsyncRequestProducer requestProducer = (HttpAsyncRequestProducer) invocationOnMock.getArguments()[0]; FutureCallback futureCallback = (FutureCallback) invocationOnMock.getArguments()[3]; - HttpEntityEnclosingRequest request = (HttpEntityEnclosingRequest)requestProducer.generateRequest(); + HttpEntityEnclosingRequest request = (HttpEntityEnclosingRequest) requestProducer.generateRequest(); URL resource = resources[responseCount]; String path = paths[responseCount++]; ProtocolVersion protocolVersion = new ProtocolVersion("http", 1, 1); @@ -535,7 +557,8 @@ private RemoteScrollableHitSource sourceWithMockedClient(boolean mockRemoteVersi when(clientBuilder.build()).thenReturn(httpClient); RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)) - .setHttpClientConfigCallback(httpClientBuilder -> clientBuilder).build(); + .setHttpClientConfigCallback(httpClientBuilder -> clientBuilder) + .build(); TestRemoteScrollableHitSource hitSource = new TestRemoteScrollableHitSource(restClient) { @Override @@ -563,30 +586,32 @@ private void countRetry() { private class TestRemoteScrollableHitSource extends RemoteScrollableHitSource { TestRemoteScrollableHitSource(RestClient client) { - super(RemoteScrollableHitSourceTests.this.logger, backoff(), RemoteScrollableHitSourceTests.this.threadPool, + super( + RemoteScrollableHitSourceTests.this.logger, + backoff(), + RemoteScrollableHitSourceTests.this.threadPool, RemoteScrollableHitSourceTests.this::countRetry, - responseQueue::add, failureQueue::add, - client, new BytesArray("{}"), RemoteScrollableHitSourceTests.this.searchRequest); + responseQueue::add, + failureQueue::add, + client, + new BytesArray("{}"), + RemoteScrollableHitSourceTests.this.searchRequest + ); } } private RejectAwareActionListener wrapAsListener(Consumer consumer) { - Consumer throwing = e -> { - throw new AssertionError(e); - }; + Consumer throwing = e -> { throw new AssertionError(e); }; return RejectAwareActionListener.wrap(consumer::accept, throwing, throwing); } @SuppressWarnings("unchecked") private T expectListenerFailure(Class expectedException, Consumer> subject) { AtomicReference exception = new AtomicReference<>(); - subject.accept(RejectAwareActionListener.wrap( - r -> fail(), - e -> { - assertThat(e, instanceOf(expectedException)); - assertTrue(exception.compareAndSet(null, (T) e)); - }, - e -> fail())); + subject.accept(RejectAwareActionListener.wrap(r -> fail(), e -> { + assertThat(e, instanceOf(expectedException)); + assertTrue(exception.compareAndSet(null, (T) e)); + }, e -> fail())); assertNotNull(exception.get()); return exception.get(); } diff --git a/modules/repository-url/src/internalClusterTest/java/org/elasticsearch/repositories/url/URLSnapshotRestoreIT.java b/modules/repository-url/src/internalClusterTest/java/org/elasticsearch/repositories/url/URLSnapshotRestoreIT.java index cc130ca913b42..daffed8dc6cb5 100644 --- a/modules/repository-url/src/internalClusterTest/java/org/elasticsearch/repositories/url/URLSnapshotRestoreIT.java +++ b/modules/repository-url/src/internalClusterTest/java/org/elasticsearch/repositories/url/URLSnapshotRestoreIT.java @@ -42,11 +42,18 @@ public void testUrlRepository() throws Exception { logger.info("--> creating repository"); Path repositoryLocation = randomRepoPath(); - assertAcked(client.admin().cluster().preparePutRepository("test-repo") - .setType(FsRepository.TYPE).setSettings(Settings.builder() - .put(FsRepository.LOCATION_SETTING.getKey(), repositoryLocation) - .put(FsRepository.COMPRESS_SETTING.getKey(), randomBoolean()) - .put(FsRepository.CHUNK_SIZE_SETTING.getKey(), randomIntBetween(100, 1000), ByteSizeUnit.BYTES))); + assertAcked( + client.admin() + .cluster() + .preparePutRepository("test-repo") + .setType(FsRepository.TYPE) + .setSettings( + Settings.builder() + .put(FsRepository.LOCATION_SETTING.getKey(), repositoryLocation) + .put(FsRepository.COMPRESS_SETTING.getKey(), randomBoolean()) + .put(FsRepository.CHUNK_SIZE_SETTING.getKey(), randomIntBetween(100, 1000), ByteSizeUnit.BYTES) + ) + ); createIndex("test-idx"); ensureGreen(); @@ -59,8 +66,7 @@ public void testUrlRepository() throws Exception { assertThat(client.prepareSearch("test-idx").setSize(0).get().getHits().getTotalHits().value, equalTo(100L)); logger.info("--> snapshot"); - CreateSnapshotResponse createSnapshotResponse = client - .admin() + CreateSnapshotResponse createSnapshotResponse = client.admin() .cluster() .prepareCreateSnapshot("test-repo", "test-snap") .setWaitForCompletion(true) @@ -70,8 +76,7 @@ public void testUrlRepository() throws Exception { int actualTotalShards = createSnapshotResponse.getSnapshotInfo().totalShards(); assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(actualTotalShards)); - SnapshotState state = client - .admin() + SnapshotState state = client.admin() .cluster() .prepareGetSnapshots("test-repo") .setSnapshots("test-snap") @@ -85,13 +90,19 @@ public void testUrlRepository() throws Exception { cluster().wipeIndices("test-idx"); logger.info("--> create read-only URL repository"); - assertAcked(client.admin().cluster().preparePutRepository("url-repo") - .setType(URLRepository.TYPE).setSettings(Settings.builder() - .put(URLRepository.URL_SETTING.getKey(), repositoryLocation.toUri().toURL().toString()) - .put("list_directories", randomBoolean()))); + assertAcked( + client.admin() + .cluster() + .preparePutRepository("url-repo") + .setType(URLRepository.TYPE) + .setSettings( + Settings.builder() + .put(URLRepository.URL_SETTING.getKey(), repositoryLocation.toUri().toURL().toString()) + .put("list_directories", randomBoolean()) + ) + ); logger.info("--> restore index after deletion"); - RestoreSnapshotResponse restoreSnapshotResponse = client - .admin() + RestoreSnapshotResponse restoreSnapshotResponse = client.admin() .cluster() .prepareRestoreSnapshot("url-repo", "test-snap") .setWaitForCompletion(true) diff --git a/modules/repository-url/src/main/java/org/elasticsearch/common/blobstore/url/URLBlobContainer.java b/modules/repository-url/src/main/java/org/elasticsearch/common/blobstore/url/URLBlobContainer.java index db0b92a894053..89fcc9aba2efa 100644 --- a/modules/repository-url/src/main/java/org/elasticsearch/common/blobstore/url/URLBlobContainer.java +++ b/modules/repository-url/src/main/java/org/elasticsearch/common/blobstore/url/URLBlobContainer.java @@ -8,14 +8,14 @@ package org.elasticsearch.common.blobstore.url; -import org.elasticsearch.core.CheckedConsumer; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.common.blobstore.BlobContainer; import org.elasticsearch.common.blobstore.BlobMetadata; import org.elasticsearch.common.blobstore.BlobPath; import org.elasticsearch.common.blobstore.DeleteResult; import org.elasticsearch.common.blobstore.support.AbstractBlobContainer; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.core.CheckedConsumer; +import org.elasticsearch.core.SuppressForbidden; import java.io.BufferedInputStream; import java.io.FileNotFoundException; @@ -124,10 +124,8 @@ public void writeBlob(String blobName, InputStream inputStream, long blobSize, b } @Override - public void writeBlob(String blobName, - boolean failIfAlreadyExists, - boolean atomic, - CheckedConsumer writer) throws IOException { + public void writeBlob(String blobName, boolean failIfAlreadyExists, boolean atomic, CheckedConsumer writer) + throws IOException { throw new UnsupportedOperationException("URL repository doesn't support this operation"); } diff --git a/modules/repository-url/src/main/java/org/elasticsearch/common/blobstore/url/URLBlobStore.java b/modules/repository-url/src/main/java/org/elasticsearch/common/blobstore/url/URLBlobStore.java index a9d39cecbbf50..5a9a770474812 100644 --- a/modules/repository-url/src/main/java/org/elasticsearch/common/blobstore/url/URLBlobStore.java +++ b/modules/repository-url/src/main/java/org/elasticsearch/common/blobstore/url/URLBlobStore.java @@ -8,7 +8,6 @@ package org.elasticsearch.common.blobstore.url; -import org.elasticsearch.core.CheckedFunction; import org.elasticsearch.common.blobstore.BlobContainer; import org.elasticsearch.common.blobstore.BlobPath; import org.elasticsearch.common.blobstore.BlobStore; @@ -20,6 +19,7 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.core.CheckedFunction; import java.net.MalformedURLException; import java.net.URL; @@ -36,7 +36,6 @@ public class URLBlobStore implements BlobStore { Setting.Property.NodeScope ); - private final URL path; private final int bufferSizeInBytes; @@ -60,8 +59,13 @@ public URLBlobStore(Settings settings, URL path, URLHttpClient httpClient, URLHt final String protocol = this.path.getProtocol(); if (protocol.equals("http") || protocol.equals("https")) { - this.blobContainerFactory = (blobPath) -> - new HttpURLBlobContainer(this, blobPath, buildPath(blobPath), httpClient, httpClientSettings); + this.blobContainerFactory = (blobPath) -> new HttpURLBlobContainer( + this, + blobPath, + buildPath(blobPath), + httpClient, + httpClientSettings + ); } else if (protocol.equals("file")) { this.blobContainerFactory = (blobPath) -> new FileURLBlobContainer(this, blobPath, buildPath(blobPath)); } else { diff --git a/modules/repository-url/src/main/java/org/elasticsearch/common/blobstore/url/http/HttpResponseInputStream.java b/modules/repository-url/src/main/java/org/elasticsearch/common/blobstore/url/http/HttpResponseInputStream.java index 9eb97b60c4537..1210a2615a142 100644 --- a/modules/repository-url/src/main/java/org/elasticsearch/common/blobstore/url/http/HttpResponseInputStream.java +++ b/modules/repository-url/src/main/java/org/elasticsearch/common/blobstore/url/http/HttpResponseInputStream.java @@ -38,8 +38,7 @@ public void close() throws IOException { private static class EmptyInputStream extends InputStream { public static final EmptyInputStream INSTANCE = new EmptyInputStream(); - private EmptyInputStream() { - } + private EmptyInputStream() {} @Override public int available() { @@ -47,12 +46,10 @@ public int available() { } @Override - public void close() { - } + public void close() {} @Override - public void mark(final int readLimit) { - } + public void mark(final int readLimit) {} @Override public boolean markSupported() { @@ -75,8 +72,7 @@ public int read(final byte[] buf, final int off, final int len) { } @Override - public void reset() { - } + public void reset() {} @Override public long skip(final long n) { diff --git a/modules/repository-url/src/main/java/org/elasticsearch/common/blobstore/url/http/HttpURLBlobContainer.java b/modules/repository-url/src/main/java/org/elasticsearch/common/blobstore/url/http/HttpURLBlobContainer.java index 12e5677dfe479..106e611151b06 100644 --- a/modules/repository-url/src/main/java/org/elasticsearch/common/blobstore/url/http/HttpURLBlobContainer.java +++ b/modules/repository-url/src/main/java/org/elasticsearch/common/blobstore/url/http/HttpURLBlobContainer.java @@ -22,11 +22,13 @@ public class HttpURLBlobContainer extends URLBlobContainer { private final URLHttpClient httpClient; private final URLHttpClientSettings httpClientSettings; - public HttpURLBlobContainer(URLBlobStore blobStore, - BlobPath blobPath, - URL path, - URLHttpClient httpClient, - URLHttpClientSettings httpClientSettings) { + public HttpURLBlobContainer( + URLBlobStore blobStore, + BlobPath blobPath, + URL path, + URLHttpClient httpClient, + URLHttpClientSettings httpClientSettings + ) { super(blobStore, blobPath, path); this.httpClient = httpClient; this.httpClientSettings = httpClientSettings; @@ -38,12 +40,14 @@ public InputStream readBlob(String name, long position, long length) throws IOEx return new ByteArrayInputStream(new byte[0]); } - return new RetryingHttpInputStream(name, + return new RetryingHttpInputStream( + name, getURIForBlob(name), position, Math.addExact(position, length) - 1, httpClient, - httpClientSettings.getMaxRetries()); + httpClientSettings.getMaxRetries() + ); } @Override diff --git a/modules/repository-url/src/main/java/org/elasticsearch/common/blobstore/url/http/RetryingHttpInputStream.java b/modules/repository-url/src/main/java/org/elasticsearch/common/blobstore/url/http/RetryingHttpInputStream.java index 3406e20b16e61..18912772d3939 100644 --- a/modules/repository-url/src/main/java/org/elasticsearch/common/blobstore/url/http/RetryingHttpInputStream.java +++ b/modules/repository-url/src/main/java/org/elasticsearch/common/blobstore/url/http/RetryingHttpInputStream.java @@ -147,13 +147,29 @@ private void ensureOpen() { private void maybeThrow(IOException e) throws IOException { if (retryCount >= maxRetries || e instanceof NoSuchFileException) { - logger.debug(new ParameterizedMessage("failed reading [{}] at offset [{}], retry [{}] of [{}], giving up", - blobURI, start + totalBytesRead, retryCount, maxRetries), e); + logger.debug( + new ParameterizedMessage( + "failed reading [{}] at offset [{}], retry [{}] of [{}], giving up", + blobURI, + start + totalBytesRead, + retryCount, + maxRetries + ), + e + ); throw addSuppressedFailures(e); } - logger.debug(new ParameterizedMessage("failed reading [{}] at offset [{}], retry [{}] of [{}], retrying", - blobURI, start + totalBytesRead, retryCount, maxRetries), e); + logger.debug( + new ParameterizedMessage( + "failed reading [{}] at offset [{}], retry [{}] of [{}], retrying", + blobURI, + start + totalBytesRead, + retryCount, + maxRetries + ), + e + ); retryCount += 1; accumulateFailure(e); @@ -216,8 +232,11 @@ private HttpResponseInputStream openInputStream() throws IOException { if (statusCode != RestStatus.OK.getStatus() && statusCode != RestStatus.PARTIAL_CONTENT.getStatus()) { String body = response.getBodyAsString(MAX_ERROR_MESSAGE_BODY_SIZE); IOUtils.closeWhileHandlingException(response); - throw new IOException(getErrorMessage("The server returned an invalid response:" + - " Status code: [" + statusCode + "] - Body: " + body)); + throw new IOException( + getErrorMessage( + "The server returned an invalid response:" + " Status code: [" + statusCode + "] - Body: " + body + ) + ); } currentStreamLastOffset = Math.addExact(Math.addExact(start, totalBytesRead), getStreamLength(response)); @@ -258,9 +277,13 @@ private long getStreamLength(URLHttpClient.HttpResponse httpResponse) { assert upperBound >= lowerBound : "Incorrect Content-Range: lower bound > upper bound " + lowerBound + "-" + upperBound; assert lowerBound == start + totalBytesRead : "Incorrect Content-Range: lower bound != specified lower bound"; - assert upperBound == end || upperBound <= MAX_RANGE_VAL : - "Incorrect Content-Range: the returned upper bound is incorrect, expected [" + end + "] " + - "got [" + upperBound + "]"; + assert upperBound == end || upperBound <= MAX_RANGE_VAL + : "Incorrect Content-Range: the returned upper bound is incorrect, expected [" + + end + + "] " + + "got [" + + upperBound + + "]"; return upperBound - lowerBound + 1; } diff --git a/modules/repository-url/src/main/java/org/elasticsearch/common/blobstore/url/http/URLHttpClient.java b/modules/repository-url/src/main/java/org/elasticsearch/common/blobstore/url/http/URLHttpClient.java index 4ccc6724cf342..52d4285f3edfd 100644 --- a/modules/repository-url/src/main/java/org/elasticsearch/common/blobstore/url/http/URLHttpClient.java +++ b/modules/repository-url/src/main/java/org/elasticsearch/common/blobstore/url/http/URLHttpClient.java @@ -21,8 +21,8 @@ import org.apache.http.ssl.SSLContexts; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.io.Streams; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.rest.RestStatus; @@ -197,8 +197,9 @@ private Charset getCharset(HttpEntity httpEntity) { private boolean isValidContentTypeToParseError(HttpEntity httpEntity) { Header contentType = httpEntity.getContentType(); - return contentType != null && httpEntity.getContentLength() > 0 && - (contentType.getValue().startsWith("text/") || contentType.getValue().startsWith("application/")); + return contentType != null + && httpEntity.getContentLength() > 0 + && (contentType.getValue().startsWith("text/") || contentType.getValue().startsWith("application/")); } private boolean isSuccessful(int statusCode) { diff --git a/modules/repository-url/src/main/java/org/elasticsearch/common/blobstore/url/http/URLHttpClientSettings.java b/modules/repository-url/src/main/java/org/elasticsearch/common/blobstore/url/http/URLHttpClientSettings.java index 4396d31b6ab0e..e9c95271b9d0b 100644 --- a/modules/repository-url/src/main/java/org/elasticsearch/common/blobstore/url/http/URLHttpClientSettings.java +++ b/modules/repository-url/src/main/java/org/elasticsearch/common/blobstore/url/http/URLHttpClientSettings.java @@ -24,13 +24,15 @@ public class URLHttpClientSettings { "http_socket_timeout", TimeValue.timeValueMillis(URLHttpClientSettings.DEFAULT_SOCKET_TIMEOUT_MILLIS), TimeValue.timeValueMillis(1), - TimeValue.timeValueMinutes(60)); + TimeValue.timeValueMinutes(60) + ); static final Setting HTTP_MAX_RETRIES_SETTING = Setting.intSetting( "http_max_retries", URLHttpClientSettings.DEFAULT_MAX_RETRIES, 0, - Integer.MAX_VALUE); + Integer.MAX_VALUE + ); private int maxRetries = DEFAULT_MAX_RETRIES; private int connectionPoolTimeoutMs = DEFAULT_CONNECTION_POOL_TIMEOUT_MILLIS; diff --git a/modules/repository-url/src/main/java/org/elasticsearch/plugin/repository/url/URLRepositoryPlugin.java b/modules/repository-url/src/main/java/org/elasticsearch/plugin/repository/url/URLRepositoryPlugin.java index 9657508c7f5f5..0da23cd6bd30d 100644 --- a/modules/repository-url/src/main/java/org/elasticsearch/plugin/repository/url/URLRepositoryPlugin.java +++ b/modules/repository-url/src/main/java/org/elasticsearch/plugin/repository/url/URLRepositoryPlugin.java @@ -16,7 +16,6 @@ import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.util.BigArrays; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.env.Environment; import org.elasticsearch.env.NodeEnvironment; @@ -29,6 +28,7 @@ import org.elasticsearch.script.ScriptService; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.watcher.ResourceWatcherService; +import org.elasticsearch.xcontent.NamedXContentRegistry; import java.io.IOException; import java.util.Arrays; @@ -51,34 +51,41 @@ public List> getSettings() { } @Override - public Map getRepositories(Environment env, NamedXContentRegistry namedXContentRegistry, - ClusterService clusterService, BigArrays bigArrays, - RecoverySettings recoverySettings) { - return Collections.singletonMap(URLRepository.TYPE, - metadata -> { + public Map getRepositories( + Environment env, + NamedXContentRegistry namedXContentRegistry, + ClusterService clusterService, + BigArrays bigArrays, + RecoverySettings recoverySettings + ) { + return Collections.singletonMap(URLRepository.TYPE, metadata -> { assert httpClientFactory.get() != null : "Expected to get a configured http client factory"; - return new URLRepository(metadata, + return new URLRepository( + metadata, env, namedXContentRegistry, clusterService, bigArrays, recoverySettings, - httpClientFactory.get()); + httpClientFactory.get() + ); }); } @Override - public Collection createComponents(Client client, - ClusterService clusterService, - ThreadPool threadPool, - ResourceWatcherService resourceWatcherService, - ScriptService scriptService, - NamedXContentRegistry xContentRegistry, - Environment environment, - NodeEnvironment nodeEnvironment, - NamedWriteableRegistry namedWriteableRegistry, - IndexNameExpressionResolver indexNameExpressionResolver, - Supplier repositoriesServiceSupplier) { + public Collection createComponents( + Client client, + ClusterService clusterService, + ThreadPool threadPool, + ResourceWatcherService resourceWatcherService, + ScriptService scriptService, + NamedXContentRegistry xContentRegistry, + Environment environment, + NodeEnvironment nodeEnvironment, + NamedWriteableRegistry namedWriteableRegistry, + IndexNameExpressionResolver indexNameExpressionResolver, + Supplier repositoriesServiceSupplier + ) { final URLHttpClient.Factory apacheURLHttpClientFactory = new URLHttpClient.Factory(); diff --git a/modules/repository-url/src/main/java/org/elasticsearch/repositories/url/URLRepository.java b/modules/repository-url/src/main/java/org/elasticsearch/repositories/url/URLRepository.java index 8ca8bccf29df5..290f5cde49b0f 100644 --- a/modules/repository-url/src/main/java/org/elasticsearch/repositories/url/URLRepository.java +++ b/modules/repository-url/src/main/java/org/elasticsearch/repositories/url/URLRepository.java @@ -15,19 +15,19 @@ import org.elasticsearch.common.blobstore.BlobContainer; import org.elasticsearch.common.blobstore.BlobPath; import org.elasticsearch.common.blobstore.BlobStore; -import org.elasticsearch.common.blobstore.url.http.URLHttpClientSettings; import org.elasticsearch.common.blobstore.url.URLBlobStore; import org.elasticsearch.common.blobstore.url.http.URLHttpClient; +import org.elasticsearch.common.blobstore.url.http.URLHttpClientSettings; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Setting.Property; import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.common.util.URIPattern; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.env.Environment; import org.elasticsearch.indices.recovery.RecoverySettings; import org.elasticsearch.repositories.RepositoryException; import org.elasticsearch.repositories.blobstore.BlobStoreRepository; +import org.elasticsearch.xcontent.NamedXContentRegistry; import java.net.MalformedURLException; import java.net.URISyntaxException; @@ -52,21 +52,31 @@ public class URLRepository extends BlobStoreRepository { public static final String TYPE = "url"; static { - //noinspection ConstantConditions + // noinspection ConstantConditions assert TYPE.equals(BlobStoreRepository.URL_REPOSITORY_TYPE); } - public static final Setting> SUPPORTED_PROTOCOLS_SETTING = - Setting.listSetting("repositories.url.supported_protocols", Arrays.asList("http", "https", "ftp", "file", "jar"), - Function.identity(), Property.NodeScope); + public static final Setting> SUPPORTED_PROTOCOLS_SETTING = Setting.listSetting( + "repositories.url.supported_protocols", + Arrays.asList("http", "https", "ftp", "file", "jar"), + Function.identity(), + Property.NodeScope + ); - public static final Setting> ALLOWED_URLS_SETTING = - Setting.listSetting("repositories.url.allowed_urls", Collections.emptyList(), URIPattern::new, Property.NodeScope); + public static final Setting> ALLOWED_URLS_SETTING = Setting.listSetting( + "repositories.url.allowed_urls", + Collections.emptyList(), + URIPattern::new, + Property.NodeScope + ); public static final Setting URL_SETTING = new Setting<>("url", "http:", URLRepository::parseURL, Property.NodeScope); - public static final Setting REPOSITORIES_URL_SETTING = - new Setting<>("repositories.url.url", (s) -> s.get("repositories.uri.url", "http:"), URLRepository::parseURL, - Property.NodeScope); + public static final Setting REPOSITORIES_URL_SETTING = new Setting<>( + "repositories.url.url", + (s) -> s.get("repositories.uri.url", "http:"), + URLRepository::parseURL, + Property.NodeScope + ); private final List supportedProtocols; @@ -83,19 +93,26 @@ public class URLRepository extends BlobStoreRepository { /** * Constructs a read-only URL-based repository */ - public URLRepository(RepositoryMetadata metadata, Environment environment, - NamedXContentRegistry namedXContentRegistry, ClusterService clusterService, BigArrays bigArrays, - RecoverySettings recoverySettings, URLHttpClient.Factory httpClientFactory) { + public URLRepository( + RepositoryMetadata metadata, + Environment environment, + NamedXContentRegistry namedXContentRegistry, + ClusterService clusterService, + BigArrays bigArrays, + RecoverySettings recoverySettings, + URLHttpClient.Factory httpClientFactory + ) { super(metadata, namedXContentRegistry, clusterService, bigArrays, recoverySettings, BlobPath.EMPTY); - if (URL_SETTING.exists(metadata.settings()) == false && REPOSITORIES_URL_SETTING.exists(environment.settings()) == false) { + if (URL_SETTING.exists(metadata.settings()) == false && REPOSITORIES_URL_SETTING.exists(environment.settings()) == false) { throw new RepositoryException(metadata.name(), "missing url"); } this.environment = environment; supportedProtocols = SUPPORTED_PROTOCOLS_SETTING.get(environment.settings()); - urlWhiteList = ALLOWED_URLS_SETTING.get(environment.settings()).toArray(new URIPattern[]{}); + urlWhiteList = ALLOWED_URLS_SETTING.get(environment.settings()).toArray(new URIPattern[] {}); url = URL_SETTING.exists(metadata.settings()) - ? URL_SETTING.get(metadata.settings()) : REPOSITORIES_URL_SETTING.get(environment.settings()); + ? URL_SETTING.get(metadata.settings()) + : REPOSITORIES_URL_SETTING.get(environment.settings()); this.httpClientSettings = URLHttpClientSettings.fromSettings(metadata.settings()); this.httpClient = httpClientFactory.create(httpClientSettings); @@ -141,10 +158,12 @@ private URL checkURL(URL url) { // We didn't match white list - try to resolve against path.repo URL normalizedUrl = environment.resolveRepoURL(url); if (normalizedUrl == null) { - String logMessage = "The specified url [{}] doesn't start with any repository paths specified by the " + - "path.repo setting or by {} setting: [{}] "; + String logMessage = "The specified url [{}] doesn't start with any repository paths specified by the " + + "path.repo setting or by {} setting: [{}] "; logger.warn(logMessage, url, ALLOWED_URLS_SETTING.getKey(), environment.repoFiles()); - String exceptionMessage = "file url [" + url + "] doesn't match any of the locations specified by path.repo or " + String exceptionMessage = "file url [" + + url + + "] doesn't match any of the locations specified by path.repo or " + ALLOWED_URLS_SETTING.getKey(); throw new RepositoryException(getMetadata().name(), exceptionMessage); } diff --git a/modules/repository-url/src/test/java/org/elasticsearch/common/blobstore/url/FileURLBlobStoreTests.java b/modules/repository-url/src/test/java/org/elasticsearch/common/blobstore/url/FileURLBlobStoreTests.java index 148898793f78b..3e5d46f08264f 100644 --- a/modules/repository-url/src/test/java/org/elasticsearch/common/blobstore/url/FileURLBlobStoreTests.java +++ b/modules/repository-url/src/test/java/org/elasticsearch/common/blobstore/url/FileURLBlobStoreTests.java @@ -34,8 +34,12 @@ public static void setUpData() throws Exception { file = createTempFile(); blobName = file.getFileName().toString(); Files.write(file, data); - blobStore = new URLBlobStore(Settings.EMPTY, file.getParent().toUri().toURL(), mock(URLHttpClient.class), - mock(URLHttpClientSettings.class)); + blobStore = new URLBlobStore( + Settings.EMPTY, + file.getParent().toUri().toURL(), + mock(URLHttpClient.class), + mock(URLHttpClientSettings.class) + ); } @Override diff --git a/modules/repository-url/src/test/java/org/elasticsearch/common/blobstore/url/HttpURLBlobStoreTests.java b/modules/repository-url/src/test/java/org/elasticsearch/common/blobstore/url/HttpURLBlobStoreTests.java index 00369aac8ae23..01bbc78a2bf4a 100644 --- a/modules/repository-url/src/test/java/org/elasticsearch/common/blobstore/url/HttpURLBlobStoreTests.java +++ b/modules/repository-url/src/test/java/org/elasticsearch/common/blobstore/url/HttpURLBlobStoreTests.java @@ -10,7 +10,7 @@ import com.sun.net.httpserver.Headers; import com.sun.net.httpserver.HttpServer; -import org.elasticsearch.core.SuppressForbidden; + import org.elasticsearch.common.blobstore.BlobContainer; import org.elasticsearch.common.blobstore.BlobPath; import org.elasticsearch.common.blobstore.url.http.URLHttpClient; @@ -19,6 +19,7 @@ import org.elasticsearch.common.io.Streams; import org.elasticsearch.common.network.InetAddresses; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.mocksocket.MockHttpServer; import org.elasticsearch.rest.RestStatus; import org.junit.AfterClass; diff --git a/modules/repository-url/src/test/java/org/elasticsearch/common/blobstore/url/URLBlobContainerRetriesTests.java b/modules/repository-url/src/test/java/org/elasticsearch/common/blobstore/url/URLBlobContainerRetriesTests.java index ad173b209b4f8..af91d807305fc 100644 --- a/modules/repository-url/src/test/java/org/elasticsearch/common/blobstore/url/URLBlobContainerRetriesTests.java +++ b/modules/repository-url/src/test/java/org/elasticsearch/common/blobstore/url/URLBlobContainerRetriesTests.java @@ -9,7 +9,6 @@ package org.elasticsearch.common.blobstore.url; import org.apache.http.ConnectionClosedException; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.common.blobstore.BlobContainer; import org.elasticsearch.common.blobstore.BlobPath; import org.elasticsearch.common.blobstore.url.http.URLHttpClient; @@ -18,6 +17,7 @@ import org.elasticsearch.common.network.InetAddresses; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.core.TimeValue; import org.elasticsearch.repositories.blobstore.AbstractBlobContainerRetriesTestCase; import org.hamcrest.Matcher; @@ -66,14 +66,17 @@ protected Matcher readTimeoutExceptionMatcher() { // If the timeout is too tight it's possible that an URLHttpClientIOException is thrown as that // exception is thrown before reading data from the response body. return either(instanceOf(SocketTimeoutException.class)).or(instanceOf(ConnectionClosedException.class)) - .or(instanceOf(RuntimeException.class)).or(instanceOf(URLHttpClientIOException.class)); + .or(instanceOf(RuntimeException.class)) + .or(instanceOf(URLHttpClientIOException.class)); } @Override - protected BlobContainer createBlobContainer(Integer maxRetries, - TimeValue readTimeout, - Boolean disableChunkedEncoding, - ByteSizeValue bufferSize) { + protected BlobContainer createBlobContainer( + Integer maxRetries, + TimeValue readTimeout, + Boolean disableChunkedEncoding, + ByteSizeValue bufferSize + ) { Settings.Builder settingsBuilder = Settings.builder(); if (maxRetries != null) { @@ -87,8 +90,12 @@ protected BlobContainer createBlobContainer(Integer maxRetries, try { final Settings settings = settingsBuilder.build(); final URLHttpClientSettings httpClientSettings = URLHttpClientSettings.fromSettings(settings); - URLBlobStore urlBlobStore = - new URLBlobStore(settings, new URL(getEndpointForServer()), factory.create(httpClientSettings), httpClientSettings); + URLBlobStore urlBlobStore = new URLBlobStore( + settings, + new URL(getEndpointForServer()), + factory.create(httpClientSettings), + httpClientSettings + ); return urlBlobStore.blobContainer(BlobPath.EMPTY); } catch (MalformedURLException e) { throw new RuntimeException("Unable to create URLBlobStore", e); diff --git a/modules/repository-url/src/test/java/org/elasticsearch/common/blobstore/url/http/RetryingHttpInputStreamTests.java b/modules/repository-url/src/test/java/org/elasticsearch/common/blobstore/url/http/RetryingHttpInputStreamTests.java index 853bcb5600427..67b315bf94a07 100644 --- a/modules/repository-url/src/test/java/org/elasticsearch/common/blobstore/url/http/RetryingHttpInputStreamTests.java +++ b/modules/repository-url/src/test/java/org/elasticsearch/common/blobstore/url/http/RetryingHttpInputStreamTests.java @@ -37,17 +37,15 @@ public void testUnderlyingInputStreamIsAbortedAfterAFailureAndRetries() throws E final int firstChunkSize = randomIntBetween(1, blobSize - 1); final HttpResponseInputStream firstHttpResponseInputStream = mock(HttpResponseInputStream.class); - when(firstHttpResponseInputStream.read(any(), anyInt(), anyInt())) - .thenReturn(firstChunkSize) - .thenThrow(new IOException()); + when(firstHttpResponseInputStream.read(any(), anyInt(), anyInt())).thenReturn(firstChunkSize).thenThrow(new IOException()); final Map firstResponseHeaders = Map.of("Content-Length", Integer.toString(blobSize)); final HttpResponseInputStream secondHttpResponseInputStream = mock(HttpResponseInputStream.class); - when(secondHttpResponseInputStream.read(any(), anyInt(), anyInt())) - .thenReturn(blobSize - firstChunkSize) - .thenReturn(-1); - final Map secondResponseHeaders = - Map.of("Content-Range", String.format(Locale.ROOT, "bytes %d-%d/%d", firstChunkSize, blobSize - 1, blobSize)); + when(secondHttpResponseInputStream.read(any(), anyInt(), anyInt())).thenReturn(blobSize - firstChunkSize).thenReturn(-1); + final Map secondResponseHeaders = Map.of( + "Content-Range", + String.format(Locale.ROOT, "bytes %d-%d/%d", firstChunkSize, blobSize - 1, blobSize) + ); final List responses = List.of( new MockHttpResponse(firstHttpResponseInputStream, RestStatus.OK.getStatus(), firstResponseHeaders), @@ -56,7 +54,8 @@ public void testUnderlyingInputStreamIsAbortedAfterAFailureAndRetries() throws E protected void assertExpectedRequestHeaders(Map requestHeaders) { assertThat("Expected a Range request but it wasn't", requestHeaders.containsKey("Range"), equalTo(true)); } - }); + } + ); final Iterator responsesIterator = responses.iterator(); @@ -120,8 +119,10 @@ public void close() throws IOException { } }; - final IOException exception = expectThrows(IOException.class, - () -> Streams.readFully(new RetryingHttpInputStream("blob", blobURI, urlHttpClient, 0))); + final IOException exception = expectThrows( + IOException.class, + () -> Streams.readFully(new RetryingHttpInputStream("blob", blobURI, urlHttpClient, 0)) + ); assertThat(closed.get(), equalTo(1)); verify(httpResponseInputStream, times(1)).close(); @@ -139,8 +140,10 @@ public void testRetriesTheRequestAfterAFailureUpToMaxRetries() throws Exception public HttpResponse get(URI uri, Map headers) throws IOException { attempts.incrementAndGet(); if (randomBoolean()) { - final Integer statusCode = - randomFrom(RestStatus.INTERNAL_SERVER_ERROR.getStatus(), RestStatus.SERVICE_UNAVAILABLE.getStatus()); + final Integer statusCode = randomFrom( + RestStatus.INTERNAL_SERVER_ERROR.getStatus(), + RestStatus.SERVICE_UNAVAILABLE.getStatus() + ); throw new URLHttpClientException(statusCode, "Server error"); } else { throw new URLHttpClientIOException("Unable to execute request", new IOException()); @@ -148,8 +151,7 @@ public HttpResponse get(URI uri, Map headers) throws IOException } }; - expectThrows(IOException.class, - () -> Streams.readFully(new RetryingHttpInputStream("blob", blobURI, urlHttpClient, maxRetries))); + expectThrows(IOException.class, () -> Streams.readFully(new RetryingHttpInputStream("blob", blobURI, urlHttpClient, maxRetries))); assertThat(attempts.get(), equalTo(maxRetries + 1)); } @@ -167,8 +169,7 @@ public HttpResponse get(URI uri, Map headers) { } }; - expectThrows(IOException.class, - () -> Streams.readFully(new RetryingHttpInputStream("blob", blobURI, urlHttpClient, maxRetries))); + expectThrows(IOException.class, () -> Streams.readFully(new RetryingHttpInputStream("blob", blobURI, urlHttpClient, maxRetries))); assertThat(attempts.get(), equalTo(1)); } @@ -200,8 +201,7 @@ public String getHeader(String headerName) { } @Override - public void close() { - } + public void close() {} @Override public String getBodyAsString(int maxSize) { diff --git a/modules/repository-url/src/test/java/org/elasticsearch/common/blobstore/url/http/URLHttpClientTests.java b/modules/repository-url/src/test/java/org/elasticsearch/common/blobstore/url/http/URLHttpClientTests.java index e7d18b5807329..740c77efea2b7 100644 --- a/modules/repository-url/src/test/java/org/elasticsearch/common/blobstore/url/http/URLHttpClientTests.java +++ b/modules/repository-url/src/test/java/org/elasticsearch/common/blobstore/url/http/URLHttpClientTests.java @@ -10,11 +10,12 @@ import com.sun.net.httpserver.Headers; import com.sun.net.httpserver.HttpServer; -import org.elasticsearch.core.SuppressForbidden; + import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.Streams; import org.elasticsearch.common.network.InetAddresses; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.mocksocket.MockHttpServer; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.test.ESTestCase; @@ -46,9 +47,7 @@ public static void setUpHttpServer() throws Exception { httpServer = MockHttpServer.createHttp(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0); httpServer.start(); httpClientFactory = new URLHttpClient.Factory(); - final Settings settings = Settings.builder() - .put("http_max_retries", 0) - .build(); + final Settings settings = Settings.builder().put("http_max_retries", 0).build(); httpClient = httpClientFactory.create(URLHttpClientSettings.fromSettings(settings)); } @@ -61,8 +60,7 @@ public static void tearDownHttpServer() throws Exception { public void testSuccessfulRequest() throws Exception { byte[] originalData = randomByteArrayOfLength(randomIntBetween(100, 1024)); - RestStatus statusCode = - randomFrom(RestStatus.OK, RestStatus.PARTIAL_CONTENT); + RestStatus statusCode = randomFrom(RestStatus.OK, RestStatus.PARTIAL_CONTENT); httpServer.createContext("/correct_data", exchange -> { try { @@ -84,8 +82,11 @@ public void testSuccessfulRequest() throws Exception { } public void testEmptyErrorMessageBody() { - final Integer errorCode = randomFrom(RestStatus.BAD_GATEWAY.getStatus(), - RestStatus.REQUEST_ENTITY_TOO_LARGE.getStatus(), RestStatus.INTERNAL_SERVER_ERROR.getStatus()); + final Integer errorCode = randomFrom( + RestStatus.BAD_GATEWAY.getStatus(), + RestStatus.REQUEST_ENTITY_TOO_LARGE.getStatus(), + RestStatus.INTERNAL_SERVER_ERROR.getStatus() + ); httpServer.createContext("/empty_error", exchange -> { assertThat(exchange.getRequestMethod(), equalTo("GET")); @@ -111,8 +112,10 @@ public void testEmptyErrorMessageBody() { } }); - final URLHttpClientException urlHttpClientException = - expectThrows(URLHttpClientException.class, () -> executeRequest("/empty_error")); + final URLHttpClientException urlHttpClientException = expectThrows( + URLHttpClientException.class, + () -> executeRequest("/empty_error") + ); assertThat(urlHttpClientException.getMessage(), is(createErrorMessage(errorCode, ""))); assertThat(urlHttpClientException.getStatusCode(), equalTo(errorCode)); @@ -129,8 +132,11 @@ public void testErrorMessageParsing() { charset = StandardCharsets.UTF_8; errorMessage = randomUnicodeOfLength(errorMessageSize); } - final Integer errorCode = randomFrom(RestStatus.BAD_GATEWAY.getStatus(), - RestStatus.REQUEST_ENTITY_TOO_LARGE.getStatus(), RestStatus.INTERNAL_SERVER_ERROR.getStatus()); + final Integer errorCode = randomFrom( + RestStatus.BAD_GATEWAY.getStatus(), + RestStatus.REQUEST_ENTITY_TOO_LARGE.getStatus(), + RestStatus.INTERNAL_SERVER_ERROR.getStatus() + ); httpServer.createContext("/error", exchange -> { assertThat(exchange.getRequestMethod(), equalTo("GET")); @@ -148,8 +154,7 @@ public void testErrorMessageParsing() { } }); - final URLHttpClientException urlHttpClientException = - expectThrows(URLHttpClientException.class, () -> executeRequest("/error")); + final URLHttpClientException urlHttpClientException = expectThrows(URLHttpClientException.class, () -> executeRequest("/error")); assertThat(urlHttpClientException.getMessage(), equalTo(createErrorMessage(errorCode, errorMessage))); assertThat(urlHttpClientException.getStatusCode(), equalTo(errorCode)); @@ -158,8 +163,10 @@ public void testErrorMessageParsing() { public void testLargeErrorMessageIsBounded() throws Exception { final Charset charset; final String errorMessage; - final int errorMessageSize = randomIntBetween(URLHttpClient.MAX_ERROR_MESSAGE_BODY_SIZE + 1, - URLHttpClient.MAX_ERROR_MESSAGE_BODY_SIZE * 2); + final int errorMessageSize = randomIntBetween( + URLHttpClient.MAX_ERROR_MESSAGE_BODY_SIZE + 1, + URLHttpClient.MAX_ERROR_MESSAGE_BODY_SIZE * 2 + ); if (randomBoolean()) { charset = Charset.forName("ISO-8859-4"); errorMessage = randomAlphaOfLength(errorMessageSize); @@ -167,8 +174,11 @@ public void testLargeErrorMessageIsBounded() throws Exception { charset = StandardCharsets.UTF_8; errorMessage = randomUnicodeOfCodepointLength(errorMessageSize); } - final Integer errorCode = randomFrom(RestStatus.BAD_GATEWAY.getStatus(), - RestStatus.REQUEST_ENTITY_TOO_LARGE.getStatus(), RestStatus.INTERNAL_SERVER_ERROR.getStatus()); + final Integer errorCode = randomFrom( + RestStatus.BAD_GATEWAY.getStatus(), + RestStatus.REQUEST_ENTITY_TOO_LARGE.getStatus(), + RestStatus.INTERNAL_SERVER_ERROR.getStatus() + ); httpServer.createContext("/large_error", exchange -> { assertThat(exchange.getRequestMethod(), equalTo("GET")); @@ -188,8 +198,10 @@ public void testLargeErrorMessageIsBounded() throws Exception { } }); - final URLHttpClientException urlHttpClientException = - expectThrows(URLHttpClientException.class, () -> executeRequest("/large_error")); + final URLHttpClientException urlHttpClientException = expectThrows( + URLHttpClientException.class, + () -> executeRequest("/large_error") + ); final byte[] bytes = errorMessage.getBytes(charset); final String strippedErrorMessage = new String(Arrays.copyOf(bytes, URLHttpClient.MAX_ERROR_MESSAGE_BODY_SIZE), charset); @@ -199,8 +211,11 @@ public void testLargeErrorMessageIsBounded() throws Exception { } public void testInvalidErrorMessageCharsetIsIgnored() { - final Integer errorCode = randomFrom(RestStatus.BAD_GATEWAY.getStatus(), - RestStatus.REQUEST_ENTITY_TOO_LARGE.getStatus(), RestStatus.INTERNAL_SERVER_ERROR.getStatus()); + final Integer errorCode = randomFrom( + RestStatus.BAD_GATEWAY.getStatus(), + RestStatus.REQUEST_ENTITY_TOO_LARGE.getStatus(), + RestStatus.INTERNAL_SERVER_ERROR.getStatus() + ); httpServer.createContext("/unknown_charset", exchange -> { assertThat(exchange.getRequestMethod(), equalTo("GET")); @@ -220,17 +235,19 @@ public void testInvalidErrorMessageCharsetIsIgnored() { } }); - final URLHttpClientException urlHttpClientException = - expectThrows(URLHttpClientException.class, () -> executeRequest("/unknown_charset")); + final URLHttpClientException urlHttpClientException = expectThrows( + URLHttpClientException.class, + () -> executeRequest("/unknown_charset") + ); assertThat(urlHttpClientException.getMessage(), is(createErrorMessage(errorCode, ""))); assertThat(urlHttpClientException.getStatusCode(), equalTo(errorCode)); } private URLHttpClient.HttpResponse executeRequest(String endpoint) throws Exception { - return AccessController.doPrivileged((PrivilegedExceptionAction) () -> { - return httpClient.get(getURIForEndpoint(endpoint), Map.of()); - }); + return AccessController.doPrivileged( + (PrivilegedExceptionAction) () -> { return httpClient.get(getURIForEndpoint(endpoint), Map.of()); } + ); } private URI getURIForEndpoint(String endpoint) throws Exception { diff --git a/modules/repository-url/src/test/java/org/elasticsearch/repositories/url/URLRepositoryTests.java b/modules/repository-url/src/test/java/org/elasticsearch/repositories/url/URLRepositoryTests.java index d4c580ea99519..00abf1e77fd57 100644 --- a/modules/repository-url/src/test/java/org/elasticsearch/repositories/url/URLRepositoryTests.java +++ b/modules/repository-url/src/test/java/org/elasticsearch/repositories/url/URLRepositoryTests.java @@ -13,13 +13,13 @@ import org.elasticsearch.common.settings.ClusterSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.MockBigArrays; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.env.Environment; import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.indices.recovery.RecoverySettings; import org.elasticsearch.repositories.RepositoryException; import org.elasticsearch.repositories.blobstore.BlobStoreTestUtil; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; import java.io.IOException; import java.nio.file.Path; @@ -33,11 +33,15 @@ public class URLRepositoryTests extends ESTestCase { private URLRepository createRepository(Settings baseSettings, RepositoryMetadata repositoryMetadata) { - return new URLRepository(repositoryMetadata, TestEnvironment.newEnvironment(baseSettings), - new NamedXContentRegistry(Collections.emptyList()), BlobStoreTestUtil.mockClusterService(), + return new URLRepository( + repositoryMetadata, + TestEnvironment.newEnvironment(baseSettings), + new NamedXContentRegistry(Collections.emptyList()), + BlobStoreTestUtil.mockClusterService(), MockBigArrays.NON_RECYCLING_INSTANCE, new RecoverySettings(baseSettings, new ClusterSettings(baseSettings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS)), - mock(URLHttpClient.Factory.class)) { + mock(URLHttpClient.Factory.class) + ) { @Override protected void assertSnapshotOrGenericThread() { // eliminate thread name check as we create repo manually on test/main threads @@ -74,7 +78,8 @@ public void testIfNotWhiteListedMustSetRepoURL() throws IOException { repository.blobContainer(); fail("RepositoryException should have been thrown."); } catch (RepositoryException e) { - String msg = "[url] file url [" + repoPath + String msg = "[url] file url [" + + repoPath + "] doesn't match any of the locations specified by path.repo or repositories.url.allowed_urls"; assertEquals(msg, e.getMessage()); } @@ -96,7 +101,7 @@ public void testMustBeSupportedProtocol() throws IOException { repository.blobContainer(); fail("RepositoryException should have been thrown."); } catch (RepositoryException e) { - assertEquals("[url] unsupported url protocol [file] from URL [" + repoPath +"]", e.getMessage()); + assertEquals("[url] unsupported url protocol [file] from URL [" + repoPath + "]", e.getMessage()); } } @@ -104,7 +109,7 @@ public void testNonNormalizedUrl() throws IOException { Settings baseSettings = Settings.builder() .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) .put(URLRepository.ALLOWED_URLS_SETTING.getKey(), "file:/tmp/") - .put(URLRepository.REPOSITORIES_URL_SETTING.getKey(), "file:/var/" ) + .put(URLRepository.REPOSITORIES_URL_SETTING.getKey(), "file:/var/") .build(); RepositoryMetadata repositoryMetadata = new RepositoryMetadata("url", URLRepository.TYPE, baseSettings); final URLRepository repository = createRepository(baseSettings, repositoryMetadata); @@ -113,9 +118,11 @@ public void testNonNormalizedUrl() throws IOException { repository.blobContainer(); fail("RepositoryException should have been thrown."); } catch (RepositoryException e) { - assertEquals("[url] file url [file:/var/] doesn't match any of the locations " - + "specified by path.repo or repositories.url.allowed_urls", - e.getMessage()); + assertEquals( + "[url] file url [file:/var/] doesn't match any of the locations " + + "specified by path.repo or repositories.url.allowed_urls", + e.getMessage() + ); } } diff --git a/modules/repository-url/src/yamlRestTest/java/org/elasticsearch/repositories/url/RepositoryURLClientYamlTestSuiteIT.java b/modules/repository-url/src/yamlRestTest/java/org/elasticsearch/repositories/url/RepositoryURLClientYamlTestSuiteIT.java index 61ad6cc9814af..0958276656a81 100644 --- a/modules/repository-url/src/yamlRestTest/java/org/elasticsearch/repositories/url/RepositoryURLClientYamlTestSuiteIT.java +++ b/modules/repository-url/src/yamlRestTest/java/org/elasticsearch/repositories/url/RepositoryURLClientYamlTestSuiteIT.java @@ -10,21 +10,22 @@ import com.carrotsearch.randomizedtesting.annotations.Name; import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; + import org.apache.http.HttpEntity; import org.apache.http.entity.ContentType; import org.apache.http.nio.entity.NStringEntity; import org.elasticsearch.client.Request; import org.elasticsearch.client.Response; import org.elasticsearch.common.Strings; -import org.elasticsearch.core.PathUtils; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.support.XContentMapValues; +import org.elasticsearch.core.PathUtils; import org.elasticsearch.repositories.fs.FsRepository; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate; import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; import org.junit.Before; import java.io.IOException; @@ -75,15 +76,17 @@ public void registerRepositories() throws IOException { // Create a FS repository using the path.repo location Request createFsRepositoryRequest = new Request("PUT", "/_snapshot/repository-fs"); - createFsRepositoryRequest.setEntity(buildRepositorySettings(FsRepository.TYPE, - Settings.builder().put("location", pathRepo).build())); + createFsRepositoryRequest.setEntity( + buildRepositorySettings(FsRepository.TYPE, Settings.builder().put("location", pathRepo).build()) + ); Response createFsRepositoryResponse = client().performRequest(createFsRepositoryRequest); assertThat(createFsRepositoryResponse.getStatusLine().getStatusCode(), equalTo(RestStatus.OK.getStatus())); // Create a URL repository using the file://{path.repo} URL Request createFileRepositoryRequest = new Request("PUT", "/_snapshot/repository-file"); - createFileRepositoryRequest.setEntity(buildRepositorySettings("url", - Settings.builder().put("url", pathRepoUri.toString()).build())); + createFileRepositoryRequest.setEntity( + buildRepositorySettings("url", Settings.builder().put("url", pathRepoUri.toString()).build()) + ); Response createFileRepositoryResponse = client().performRequest(createFileRepositoryRequest); assertThat(createFileRepositoryResponse.getStatusLine().getStatusCode(), equalTo(RestStatus.OK.getStatus())); @@ -95,8 +98,7 @@ public void registerRepositories() throws IOException { InetAddress inetAddress = InetAddress.getByName(new URL(allowedUrl).getHost()); if (inetAddress.isAnyLocalAddress() || inetAddress.isLoopbackAddress()) { Request createUrlRepositoryRequest = new Request("PUT", "/_snapshot/repository-url"); - createUrlRepositoryRequest.setEntity(buildRepositorySettings("url", - Settings.builder().put("url", allowedUrl).build())); + createUrlRepositoryRequest.setEntity(buildRepositorySettings("url", Settings.builder().put("url", allowedUrl).build())); Response createUrlRepositoryResponse = client().performRequest(createUrlRepositoryRequest); assertThat(createUrlRepositoryResponse.getStatusLine().getStatusCode(), equalTo(RestStatus.OK.getStatus())); break; diff --git a/modules/systemd/src/main/java/org/elasticsearch/systemd/SystemdPlugin.java b/modules/systemd/src/main/java/org/elasticsearch/systemd/SystemdPlugin.java index aa0b2e05ddcf9..96c2ca9c72e99 100644 --- a/modules/systemd/src/main/java/org/elasticsearch/systemd/SystemdPlugin.java +++ b/modules/systemd/src/main/java/org/elasticsearch/systemd/SystemdPlugin.java @@ -17,7 +17,6 @@ import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.env.Environment; import org.elasticsearch.env.NodeEnvironment; import org.elasticsearch.plugins.ClusterPlugin; @@ -27,6 +26,7 @@ import org.elasticsearch.threadpool.Scheduler; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.watcher.ResourceWatcherService; +import org.elasticsearch.xcontent.NamedXContentRegistry; import java.util.Collection; import java.util.List; @@ -87,7 +87,8 @@ public Collection createComponents( final NodeEnvironment nodeEnvironment, final NamedWriteableRegistry namedWriteableRegistry, final IndexNameExpressionResolver expressionResolver, - final Supplier repositoriesServiceSupplier) { + final Supplier repositoriesServiceSupplier + ) { if (enabled == false) { extender.set(null); return List.of(); @@ -99,15 +100,12 @@ public Collection createComponents( * Therefore, every fifteen seconds we send systemd a message via sd_notify to extend the timeout by thirty seconds. We will cancel * this scheduled task after we successfully notify systemd that we are ready. */ - extender.set(threadPool.scheduleWithFixedDelay( - () -> { - final int rc = sd_notify(0, "EXTEND_TIMEOUT_USEC=30000000"); - if (rc < 0) { - logger.warn("extending startup timeout via sd_notify failed with [{}]", rc); - } - }, - TimeValue.timeValueSeconds(15), - ThreadPool.Names.SAME)); + extender.set(threadPool.scheduleWithFixedDelay(() -> { + final int rc = sd_notify(0, "EXTEND_TIMEOUT_USEC=30000000"); + if (rc < 0) { + logger.warn("extending startup timeout via sd_notify failed with [{}]", rc); + } + }, TimeValue.timeValueSeconds(15), ThreadPool.Names.SAME)); return List.of(); } diff --git a/modules/systemd/src/test/java/org/elasticsearch/systemd/SystemdPluginTests.java b/modules/systemd/src/test/java/org/elasticsearch/systemd/SystemdPluginTests.java index 13f3cd4bda361..7c99f28d396d9 100644 --- a/modules/systemd/src/test/java/org/elasticsearch/systemd/SystemdPluginTests.java +++ b/modules/systemd/src/test/java/org/elasticsearch/systemd/SystemdPluginTests.java @@ -36,8 +36,10 @@ public class SystemdPluginTests extends ESTestCase { private Build.Type randomPackageBuildType = randomFrom(Build.Type.DEB, Build.Type.RPM); - private Build.Type randomNonPackageBuildType = - randomValueOtherThanMany(t -> t == Build.Type.DEB || t == Build.Type.RPM, () -> randomFrom(Build.Type.values())); + private Build.Type randomNonPackageBuildType = randomValueOtherThanMany( + t -> t == Build.Type.DEB || t == Build.Type.RPM, + () -> randomFrom(Build.Type.values()) + ); final Scheduler.Cancellable extender = mock(Scheduler.Cancellable.class); final ThreadPool threadPool = mock(ThreadPool.class); @@ -79,46 +81,38 @@ public void testIsExplicitlyNotEnabled() { public void testInvalid() { final String esSDNotify = randomValueOtherThanMany( s -> Boolean.TRUE.toString().equals(s) || Boolean.FALSE.toString().equals(s), - () -> randomAlphaOfLength(4)); - final RuntimeException e = expectThrows(RuntimeException.class, - () -> new SystemdPlugin(false, randomPackageBuildType, esSDNotify)); + () -> randomAlphaOfLength(4) + ); + final RuntimeException e = expectThrows(RuntimeException.class, () -> new SystemdPlugin(false, randomPackageBuildType, esSDNotify)); assertThat(e, hasToString(containsString("ES_SD_NOTIFY set to unexpected value [" + esSDNotify + "]"))); } public void testOnNodeStartedSuccess() { - runTestOnNodeStarted( - Boolean.TRUE.toString(), - randomIntBetween(0, Integer.MAX_VALUE), - (maybe, plugin) -> { - assertThat(maybe, OptionalMatchers.isEmpty()); - verify(plugin.extender()).cancel(); - }); + runTestOnNodeStarted(Boolean.TRUE.toString(), randomIntBetween(0, Integer.MAX_VALUE), (maybe, plugin) -> { + assertThat(maybe, OptionalMatchers.isEmpty()); + verify(plugin.extender()).cancel(); + }); } public void testOnNodeStartedFailure() { final int rc = randomIntBetween(Integer.MIN_VALUE, -1); - runTestOnNodeStarted( - Boolean.TRUE.toString(), - rc, - (maybe, plugin) -> { - assertThat(maybe, OptionalMatchers.isPresent()); - // noinspection OptionalGetWithoutIsPresent - assertThat(maybe.get(), instanceOf(RuntimeException.class)); - assertThat(maybe.get(), hasToString(containsString("sd_notify returned error [" + rc + "]"))); - }); + runTestOnNodeStarted(Boolean.TRUE.toString(), rc, (maybe, plugin) -> { + assertThat(maybe, OptionalMatchers.isPresent()); + // noinspection OptionalGetWithoutIsPresent + assertThat(maybe.get(), instanceOf(RuntimeException.class)); + assertThat(maybe.get(), hasToString(containsString("sd_notify returned error [" + rc + "]"))); + }); } public void testOnNodeStartedNotEnabled() { - runTestOnNodeStarted( - Boolean.FALSE.toString(), - randomInt(), - (maybe, plugin) -> assertThat(maybe, OptionalMatchers.isEmpty())); + runTestOnNodeStarted(Boolean.FALSE.toString(), randomInt(), (maybe, plugin) -> assertThat(maybe, OptionalMatchers.isEmpty())); } private void runTestOnNodeStarted( final String esSDNotify, final int rc, - final BiConsumer, SystemdPlugin> assertions) { + final BiConsumer, SystemdPlugin> assertions + ) { runTest(esSDNotify, rc, assertions, SystemdPlugin::onNodeStarted, "READY=1"); } @@ -126,27 +120,23 @@ public void testCloseSuccess() { runTestClose( Boolean.TRUE.toString(), randomIntBetween(1, Integer.MAX_VALUE), - (maybe, plugin) -> assertThat(maybe, OptionalMatchers.isEmpty())); + (maybe, plugin) -> assertThat(maybe, OptionalMatchers.isEmpty()) + ); } public void testCloseFailure() { runTestClose( Boolean.TRUE.toString(), randomIntBetween(Integer.MIN_VALUE, -1), - (maybe, plugin) -> assertThat(maybe, OptionalMatchers.isEmpty())); + (maybe, plugin) -> assertThat(maybe, OptionalMatchers.isEmpty()) + ); } public void testCloseNotEnabled() { - runTestClose( - Boolean.FALSE.toString(), - randomInt(), - (maybe, plugin) -> assertThat(maybe, OptionalMatchers.isEmpty())); + runTestClose(Boolean.FALSE.toString(), randomInt(), (maybe, plugin) -> assertThat(maybe, OptionalMatchers.isEmpty())); } - private void runTestClose( - final String esSDNotify, - final int rc, - final BiConsumer, SystemdPlugin> assertions) { + private void runTestClose(final String esSDNotify, final int rc, final BiConsumer, SystemdPlugin> assertions) { runTest(esSDNotify, rc, assertions, SystemdPlugin::close, "STOPPING=1"); } @@ -155,7 +145,8 @@ private void runTest( final int rc, final BiConsumer, SystemdPlugin> assertions, final CheckedConsumer invocation, - final String expectedState) { + final String expectedState + ) { final AtomicBoolean invoked = new AtomicBoolean(); final AtomicInteger invokedUnsetEnvironment = new AtomicInteger(); final AtomicReference invokedState = new AtomicReference<>(); diff --git a/modules/transport-netty4/src/internalClusterTest/java/org/elasticsearch/http/netty4/Netty4HttpRequestSizeLimitIT.java b/modules/transport-netty4/src/internalClusterTest/java/org/elasticsearch/http/netty4/Netty4HttpRequestSizeLimitIT.java index d463b9ce8b3b0..0dadc159c41c7 100644 --- a/modules/transport-netty4/src/internalClusterTest/java/org/elasticsearch/http/netty4/Netty4HttpRequestSizeLimitIT.java +++ b/modules/transport-netty4/src/internalClusterTest/java/org/elasticsearch/http/netty4/Netty4HttpRequestSizeLimitIT.java @@ -11,12 +11,13 @@ import io.netty.handler.codec.http.FullHttpResponse; import io.netty.handler.codec.http.HttpResponseStatus; import io.netty.util.ReferenceCounted; + import org.elasticsearch.ESNetty4IntegTestCase; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.TransportAddress; import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.core.Tuple; import org.elasticsearch.http.HttpServerTransport; import org.elasticsearch.indices.breaker.HierarchyCircuitBreakerService; import org.elasticsearch.test.ESIntegTestCase.ClusterScope; @@ -100,8 +101,7 @@ public void testDoesNotLimitExcludedRequests() throws Exception { List> requestUris = new ArrayList<>(); for (int i = 0; i < 1500; i++) { - requestUris.add(Tuple.tuple("/_cluster/settings", - "{ \"persistent\": {\"search.default_search_timeout\": \"40s\" } }")); + requestUris.add(Tuple.tuple("/_cluster/settings", "{ \"persistent\": {\"search.default_search_timeout\": \"40s\" } }")); } HttpServerTransport httpServerTransport = internalCluster().getInstance(HttpServerTransport.class); @@ -125,8 +125,11 @@ private void assertAtLeastOnceExpectedStatus(Collection respon private void assertAllInExpectedStatus(Collection responses, HttpResponseStatus expectedStatus) { long countUnexpectedStatus = responses.stream().filter(r -> r.status().equals(expectedStatus) == false).count(); - assertThat("Expected all requests with status [" + expectedStatus + "] but [" + countUnexpectedStatus + - "] requests had a different one", countUnexpectedStatus, equalTo(0L)); + assertThat( + "Expected all requests with status [" + expectedStatus + "] but [" + countUnexpectedStatus + "] requests had a different one", + countUnexpectedStatus, + equalTo(0L) + ); } } diff --git a/modules/transport-netty4/src/internalClusterTest/java/org/elasticsearch/http/netty4/Netty4PipeliningIT.java b/modules/transport-netty4/src/internalClusterTest/java/org/elasticsearch/http/netty4/Netty4PipeliningIT.java index 46681dae55d40..b826a2845fff6 100644 --- a/modules/transport-netty4/src/internalClusterTest/java/org/elasticsearch/http/netty4/Netty4PipeliningIT.java +++ b/modules/transport-netty4/src/internalClusterTest/java/org/elasticsearch/http/netty4/Netty4PipeliningIT.java @@ -10,6 +10,7 @@ import io.netty.handler.codec.http.FullHttpResponse; import io.netty.util.ReferenceCounted; + import org.elasticsearch.ESNetty4IntegTestCase; import org.elasticsearch.common.transport.TransportAddress; import org.elasticsearch.http.HttpServerTransport; @@ -31,7 +32,7 @@ protected boolean addMockHttpTransport() { } public void testThatNettyHttpServerSupportsPipelining() throws Exception { - String[] requests = new String[]{"/", "/_nodes/stats", "/", "/_cluster/state", "/"}; + String[] requests = new String[] { "/", "/_nodes/stats", "/", "/_cluster/state", "/" }; HttpServerTransport httpServerTransport = internalCluster().getInstance(HttpServerTransport.class); TransportAddress[] boundAddresses = httpServerTransport.boundAddress().boundAddresses(); diff --git a/modules/transport-netty4/src/internalClusterTest/java/org/elasticsearch/rest/discovery/Zen2RestApiIT.java b/modules/transport-netty4/src/internalClusterTest/java/org/elasticsearch/rest/discovery/Zen2RestApiIT.java index 8d0896f90b507..f50df6c73bdb5 100644 --- a/modules/transport-netty4/src/internalClusterTest/java/org/elasticsearch/rest/discovery/Zen2RestApiIT.java +++ b/modules/transport-netty4/src/internalClusterTest/java/org/elasticsearch/rest/discovery/Zen2RestApiIT.java @@ -48,16 +48,18 @@ protected boolean addMockHttpTransport() { public void testRollingRestartOfTwoNodeCluster() throws Exception { internalCluster().setBootstrapMasterNodeIndex(1); final List nodes = internalCluster().startNodes(2); - createIndex("test", + createIndex( + "test", Settings.builder() .put(UnassignedInfo.INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING.getKey(), TimeValue.ZERO) // assign shards .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 2) // causes rebalancing .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) - .build()); + .build() + ); ensureGreen("test"); final DiscoveryNodes discoveryNodes = client().admin().cluster().prepareState().clear().setNodes(true).get().getState().nodes(); - final Map nodeIdsByName = new HashMap<>(discoveryNodes.getSize()); + final Map nodeIdsByName = new HashMap<>(discoveryNodes.getSize()); discoveryNodes.forEach(n -> nodeIdsByName.put(n.getName(), n.getId())); RestClient restClient = getRestClient(); @@ -91,7 +93,9 @@ public Settings onNodeStopped(String nodeName) throws IOException { new Node( HttpHost.create( internalCluster().getInstance(HttpServerTransport.class, viaNode) - .boundAddress().publishAddress().toString() + .boundAddress() + .publishAddress() + .toString() ) ) ) @@ -99,7 +103,9 @@ public Settings onNodeStopped(String nodeName) throws IOException { Response deleteResponse = restClient.performRequest(new Request("DELETE", "/_cluster/voting_config_exclusions")); assertThat(deleteResponse.getStatusLine().getStatusCode(), is(200)); - ClusterHealthResponse clusterHealthResponse = client(viaNode).admin().cluster().prepareHealth() + ClusterHealthResponse clusterHealthResponse = client(viaNode).admin() + .cluster() + .prepareHealth() .setWaitForEvents(Priority.LANGUID) .setWaitForNodes(Integer.toString(1)) .setTimeout(TimeValue.timeValueSeconds(30L)) @@ -128,7 +134,8 @@ public void testClearVotingTombstonesNotWaitingForRemoval() throws Exception { assertThat(response.getStatusLine().getStatusCode(), is(200)); assertThat(response.getEntity().getContentLength(), is(0L)); Response deleteResponse = restClient.performRequest( - new Request("DELETE", "/_cluster/voting_config_exclusions/?wait_for_removal=false")); + new Request("DELETE", "/_cluster/voting_config_exclusions/?wait_for_removal=false") + ); assertThat(deleteResponse.getStatusLine().getStatusCode(), is(200)); assertThat(deleteResponse.getEntity().getContentLength(), is(0L)); } diff --git a/modules/transport-netty4/src/internalClusterTest/java/org/elasticsearch/transport/netty4/ESLoggingHandlerIT.java b/modules/transport-netty4/src/internalClusterTest/java/org/elasticsearch/transport/netty4/ESLoggingHandlerIT.java index 33cc4693e00cd..8ab50fa7b6a45 100644 --- a/modules/transport-netty4/src/internalClusterTest/java/org/elasticsearch/transport/netty4/ESLoggingHandlerIT.java +++ b/modules/transport-netty4/src/internalClusterTest/java/org/elasticsearch/transport/netty4/ESLoggingHandlerIT.java @@ -45,34 +45,43 @@ public void tearDown() throws Exception { } @TestLogging( - value = "org.elasticsearch.transport.netty4.ESLoggingHandler:trace,org.elasticsearch.transport.TransportLogger:trace", - reason = "to ensure we log network events on TRACE level") + value = "org.elasticsearch.transport.netty4.ESLoggingHandler:trace,org.elasticsearch.transport.TransportLogger:trace", + reason = "to ensure we log network events on TRACE level" + ) public void testLoggingHandler() { - final String writePattern = - ".*\\[length: \\d+" + - ", request id: \\d+" + - ", type: request" + - ", version: .*" + - ", action: cluster:monitor/nodes/hot_threads\\[n\\]\\]" + - " WRITE: \\d+B"; - final MockLogAppender.LoggingExpectation writeExpectation = - new MockLogAppender.PatternSeenEventExpectation( - "hot threads request", TransportLogger.class.getCanonicalName(), Level.TRACE, writePattern); + final String writePattern = ".*\\[length: \\d+" + + ", request id: \\d+" + + ", type: request" + + ", version: .*" + + ", action: cluster:monitor/nodes/hot_threads\\[n\\]\\]" + + " WRITE: \\d+B"; + final MockLogAppender.LoggingExpectation writeExpectation = new MockLogAppender.PatternSeenEventExpectation( + "hot threads request", + TransportLogger.class.getCanonicalName(), + Level.TRACE, + writePattern + ); - final MockLogAppender.LoggingExpectation flushExpectation = - new MockLogAppender.SeenEventExpectation("flush", ESLoggingHandler.class.getCanonicalName(), Level.TRACE, "*FLUSH*"); + final MockLogAppender.LoggingExpectation flushExpectation = new MockLogAppender.SeenEventExpectation( + "flush", + ESLoggingHandler.class.getCanonicalName(), + Level.TRACE, + "*FLUSH*" + ); - final String readPattern = - ".*\\[length: \\d+" + - ", request id: \\d+" + - ", type: request" + - ", version: .*" + - ", action: cluster:monitor/nodes/hot_threads\\[n\\]\\]" + - " READ: \\d+B"; + final String readPattern = ".*\\[length: \\d+" + + ", request id: \\d+" + + ", type: request" + + ", version: .*" + + ", action: cluster:monitor/nodes/hot_threads\\[n\\]\\]" + + " READ: \\d+B"; - final MockLogAppender.LoggingExpectation readExpectation = - new MockLogAppender.PatternSeenEventExpectation( - "hot threads request", TransportLogger.class.getCanonicalName(), Level.TRACE, readPattern); + final MockLogAppender.LoggingExpectation readExpectation = new MockLogAppender.PatternSeenEventExpectation( + "hot threads request", + TransportLogger.class.getCanonicalName(), + Level.TRACE, + readPattern + ); appender.addExpectation(writeExpectation); appender.addExpectation(flushExpectation); @@ -83,12 +92,22 @@ public void testLoggingHandler() { @TestLogging(value = "org.elasticsearch.transport.TcpTransport:DEBUG", reason = "to ensure we log connection events on DEBUG level") public void testConnectionLogging() throws IOException { - appender.addExpectation(new MockLogAppender.PatternSeenEventExpectation("open connection log", - TcpTransport.class.getCanonicalName(), Level.DEBUG, - ".*opened transport connection \\[[1-9][0-9]*\\] to .*")); - appender.addExpectation(new MockLogAppender.PatternSeenEventExpectation("close connection log", - TcpTransport.class.getCanonicalName(), Level.DEBUG, - ".*closed transport connection \\[[1-9][0-9]*\\] to .* with age \\[[0-9]+ms\\].*")); + appender.addExpectation( + new MockLogAppender.PatternSeenEventExpectation( + "open connection log", + TcpTransport.class.getCanonicalName(), + Level.DEBUG, + ".*opened transport connection \\[[1-9][0-9]*\\] to .*" + ) + ); + appender.addExpectation( + new MockLogAppender.PatternSeenEventExpectation( + "close connection log", + TcpTransport.class.getCanonicalName(), + Level.DEBUG, + ".*closed transport connection \\[[1-9][0-9]*\\] to .* with age \\[[0-9]+ms\\].*" + ) + ); final String nodeName = internalCluster().startNode(); internalCluster().stopRandomNode(InternalTestCluster.nameFilter(nodeName)); diff --git a/modules/transport-netty4/src/internalClusterTest/java/org/elasticsearch/transport/netty4/Netty4TransportMultiPortIntegrationIT.java b/modules/transport-netty4/src/internalClusterTest/java/org/elasticsearch/transport/netty4/Netty4TransportMultiPortIntegrationIT.java index 34d3fbc8284ee..4566d2a56d953 100644 --- a/modules/transport-netty4/src/internalClusterTest/java/org/elasticsearch/transport/netty4/Netty4TransportMultiPortIntegrationIT.java +++ b/modules/transport-netty4/src/internalClusterTest/java/org/elasticsearch/transport/netty4/Netty4TransportMultiPortIntegrationIT.java @@ -65,8 +65,10 @@ public void testThatInfosAreExposed() throws Exception { // bound addresses for (TransportAddress transportAddress : boundTransportAddress.boundAddresses()) { assertThat(transportAddress, instanceOf(TransportAddress.class)); - assertThat(transportAddress.address().getPort(), - is(allOf(greaterThanOrEqualTo(randomPort), lessThanOrEqualTo(randomPort + 10)))); + assertThat( + transportAddress.address().getPort(), + is(allOf(greaterThanOrEqualTo(randomPort), lessThanOrEqualTo(randomPort + 10))) + ); } // publish address diff --git a/modules/transport-netty4/src/javaRestTest/java/org/elasticsearch/rest/Netty4BadRequestIT.java b/modules/transport-netty4/src/javaRestTest/java/org/elasticsearch/rest/Netty4BadRequestIT.java index 1e82b79cc00c7..9f6403fc78b6c 100644 --- a/modules/transport-netty4/src/javaRestTest/java/org/elasticsearch/rest/Netty4BadRequestIT.java +++ b/modules/transport-netty4/src/javaRestTest/java/org/elasticsearch/rest/Netty4BadRequestIT.java @@ -39,13 +39,14 @@ public void testBadRequest() throws IOException { final Setting httpMaxInitialLineLength = HttpTransportSettings.SETTING_HTTP_MAX_INITIAL_LINE_LENGTH; final String key = httpMaxInitialLineLength.getKey().substring("http.".length()); for (Map.Entry entry : map.entrySet()) { - @SuppressWarnings("unchecked") final Map settings = - (Map)((Map)entry.getValue()).get("settings"); - final int maxIntialLineLength; + @SuppressWarnings("unchecked") + final Map settings = (Map) ((Map) entry.getValue()).get("settings"); + final int maxIntialLineLength; if (settings.containsKey("http")) { - @SuppressWarnings("unchecked") final Map httpSettings = (Map)settings.get("http"); + @SuppressWarnings("unchecked") + final Map httpSettings = (Map) settings.get("http"); if (httpSettings.containsKey(key)) { - maxIntialLineLength = ByteSizeValue.parseBytesSizeValue((String)httpSettings.get(key), key).bytesAsInt(); + maxIntialLineLength = ByteSizeValue.parseBytesSizeValue((String) httpSettings.get(key), key).bytesAsInt(); } else { maxIntialLineLength = httpMaxInitialLineLength.getDefault(Settings.EMPTY).bytesAsInt(); } @@ -56,10 +57,10 @@ public void testBadRequest() throws IOException { } final String path = "/" + new String(new byte[maxMaxInitialLineLength], Charset.forName("UTF-8")).replace('\0', 'a'); - final ResponseException e = - expectThrows( - ResponseException.class, - () -> client().performRequest(new Request(randomFrom("GET", "POST", "PUT"), path))); + final ResponseException e = expectThrows( + ResponseException.class, + () -> client().performRequest(new Request(randomFrom("GET", "POST", "PUT"), path)) + ); assertThat(e.getResponse().getStatusLine().getStatusCode(), equalTo(BAD_REQUEST.getStatus())); assertThat(e, hasToString(containsString("too_long_frame_exception"))); assertThat(e, hasToString(matches("An HTTP line is larger than \\d+ bytes"))); diff --git a/modules/transport-netty4/src/javaRestTest/java/org/elasticsearch/rest/Netty4HeadBodyIsEmptyIT.java b/modules/transport-netty4/src/javaRestTest/java/org/elasticsearch/rest/Netty4HeadBodyIsEmptyIT.java index 8899ba0a35c4e..4720a68ae0a00 100644 --- a/modules/transport-netty4/src/javaRestTest/java/org/elasticsearch/rest/Netty4HeadBodyIsEmptyIT.java +++ b/modules/transport-netty4/src/javaRestTest/java/org/elasticsearch/rest/Netty4HeadBodyIsEmptyIT.java @@ -11,9 +11,9 @@ import org.elasticsearch.client.Request; import org.elasticsearch.client.Response; import org.elasticsearch.common.Strings; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.rest.action.admin.indices.RestPutIndexTemplateAction; import org.elasticsearch.test.rest.ESRestTestCase; +import org.elasticsearch.xcontent.XContentBuilder; import org.hamcrest.Matcher; import java.io.IOException; @@ -21,9 +21,9 @@ import static java.util.Collections.emptyMap; import static java.util.Collections.singletonMap; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.rest.RestStatus.NOT_FOUND; import static org.elasticsearch.rest.RestStatus.OK; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.Matchers.greaterThan; public class Netty4HeadBodyIsEmptyIT extends ESRestTestCase { @@ -181,11 +181,12 @@ private void headTestCase(final String url, final Map params, fi } private void headTestCase( - final String url, - final Map params, - final int expectedStatusCode, - final Matcher matcher, - final String... expectedWarnings) throws IOException { + final String url, + final Map params, + final int expectedStatusCode, + final Matcher matcher, + final String... expectedWarnings + ) throws IOException { Request request = new Request("HEAD", url); for (Map.Entry param : params.entrySet()) { request.addParameter(param.getKey(), param.getValue()); diff --git a/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpChannel.java b/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpChannel.java index f52b2de33e318..23c8276e31cf0 100644 --- a/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpChannel.java +++ b/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpChannel.java @@ -9,6 +9,7 @@ package org.elasticsearch.http.netty4; import io.netty.channel.Channel; + import org.elasticsearch.action.ActionListener; import org.elasticsearch.core.CompletableContext; import org.elasticsearch.http.HttpChannel; @@ -72,9 +73,6 @@ public Channel getNettyChannel() { @Override public String toString() { - return "Netty4HttpChannel{" + - "localAddress=" + getLocalAddress() + - ", remoteAddress=" + getRemoteAddress() + - '}'; + return "Netty4HttpChannel{" + "localAddress=" + getLocalAddress() + ", remoteAddress=" + getRemoteAddress() + '}'; } } diff --git a/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpPipeliningHandler.java b/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpPipeliningHandler.java index 102ad4feeb6b7..8ec707157ca3f 100644 --- a/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpPipeliningHandler.java +++ b/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpPipeliningHandler.java @@ -11,6 +11,7 @@ import io.netty.channel.ChannelDuplexHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelPromise; + import org.apache.logging.log4j.Logger; import org.elasticsearch.core.Tuple; import org.elasticsearch.http.HttpPipelinedRequest; diff --git a/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpRequest.java b/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpRequest.java index dd8e7807a69cc..7a7c834f287ce 100644 --- a/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpRequest.java +++ b/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpRequest.java @@ -19,6 +19,7 @@ import io.netty.handler.codec.http.cookie.Cookie; import io.netty.handler.codec.http.cookie.ServerCookieDecoder; import io.netty.handler.codec.http.cookie.ServerCookieEncoder; + import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.http.HttpRequest; import org.elasticsearch.rest.RestRequest; @@ -44,22 +45,44 @@ public class Netty4HttpRequest implements HttpRequest { private final boolean pooled; Netty4HttpRequest(FullHttpRequest request) { - this(request, new HttpHeadersMap(request.headers()), new AtomicBoolean(false), true, - Netty4Utils.toBytesReference(request.content())); + this( + request, + new HttpHeadersMap(request.headers()), + new AtomicBoolean(false), + true, + Netty4Utils.toBytesReference(request.content()) + ); } Netty4HttpRequest(FullHttpRequest request, Exception inboundException) { - this(request, new HttpHeadersMap(request.headers()), new AtomicBoolean(false), true, - Netty4Utils.toBytesReference(request.content()), inboundException); + this( + request, + new HttpHeadersMap(request.headers()), + new AtomicBoolean(false), + true, + Netty4Utils.toBytesReference(request.content()), + inboundException + ); } - private Netty4HttpRequest(FullHttpRequest request, HttpHeadersMap headers, AtomicBoolean released, boolean pooled, - BytesReference content) { + private Netty4HttpRequest( + FullHttpRequest request, + HttpHeadersMap headers, + AtomicBoolean released, + boolean pooled, + BytesReference content + ) { this(request, headers, released, pooled, content, null); } - private Netty4HttpRequest(FullHttpRequest request, HttpHeadersMap headers, AtomicBoolean released, boolean pooled, - BytesReference content, Exception inboundException) { + private Netty4HttpRequest( + FullHttpRequest request, + HttpHeadersMap headers, + AtomicBoolean released, + boolean pooled, + BytesReference content, + Exception inboundException + ) { this.request = request; this.headers = headers; this.content = content; @@ -71,17 +94,13 @@ private Netty4HttpRequest(FullHttpRequest request, HttpHeadersMap headers, Atomi @Override public RestRequest.Method method() { HttpMethod httpMethod = request.method(); - if (httpMethod == HttpMethod.GET) - return RestRequest.Method.GET; + if (httpMethod == HttpMethod.GET) return RestRequest.Method.GET; - if (httpMethod == HttpMethod.POST) - return RestRequest.Method.POST; + if (httpMethod == HttpMethod.POST) return RestRequest.Method.POST; - if (httpMethod == HttpMethod.PUT) - return RestRequest.Method.PUT; + if (httpMethod == HttpMethod.PUT) return RestRequest.Method.PUT; - if (httpMethod == HttpMethod.DELETE) - return RestRequest.Method.DELETE; + if (httpMethod == HttpMethod.DELETE) return RestRequest.Method.DELETE; if (httpMethod == HttpMethod.HEAD) { return RestRequest.Method.HEAD; @@ -133,9 +152,19 @@ public HttpRequest releaseAndCopy() { try { final ByteBuf copiedContent = Unpooled.copiedBuffer(request.content()); return new Netty4HttpRequest( - new DefaultFullHttpRequest(request.protocolVersion(), request.method(), request.uri(), copiedContent, request.headers(), - request.trailingHeaders()), - headers, new AtomicBoolean(false), false, Netty4Utils.toBytesReference(copiedContent)); + new DefaultFullHttpRequest( + request.protocolVersion(), + request.method(), + request.uri(), + copiedContent, + request.headers(), + request.trailingHeaders() + ), + headers, + new AtomicBoolean(false), + false, + Netty4Utils.toBytesReference(copiedContent) + ); } finally { release(); } @@ -177,10 +206,15 @@ public HttpRequest removeHeader(String header) { HttpHeaders trailingHeaders = new DefaultHttpHeaders(); trailingHeaders.add(request.trailingHeaders()); trailingHeaders.remove(header); - FullHttpRequest requestWithoutHeader = new DefaultFullHttpRequest(request.protocolVersion(), request.method(), request.uri(), - request.content(), headersWithoutContentTypeHeader, trailingHeaders); - return new Netty4HttpRequest(requestWithoutHeader, new HttpHeadersMap(requestWithoutHeader.headers()), released, - pooled, content); + FullHttpRequest requestWithoutHeader = new DefaultFullHttpRequest( + request.protocolVersion(), + request.method(), + request.uri(), + request.content(), + headersWithoutContentTypeHeader, + trailingHeaders + ); + return new Netty4HttpRequest(requestWithoutHeader, new HttpHeadersMap(requestWithoutHeader.headers()), released, pooled, content); } @Override @@ -270,7 +304,9 @@ public Collection> values() { @Override public Set>> entrySet() { - return httpHeaders.names().stream().map(k -> new AbstractMap.SimpleImmutableEntry<>(k, httpHeaders.getAll(k))) + return httpHeaders.names() + .stream() + .map(k -> new AbstractMap.SimpleImmutableEntry<>(k, httpHeaders.getAll(k))) .collect(Collectors.toSet()); } } diff --git a/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpRequestCreator.java b/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpRequestCreator.java index ec09236541ba0..a38dee7576d69 100644 --- a/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpRequestCreator.java +++ b/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpRequestCreator.java @@ -12,6 +12,7 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToMessageDecoder; import io.netty.handler.codec.http.FullHttpRequest; + import org.elasticsearch.ExceptionsHelper; import java.util.List; diff --git a/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpRequestHandler.java b/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpRequestHandler.java index 822e6b974a597..9a592472d1b3a 100644 --- a/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpRequestHandler.java +++ b/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpRequestHandler.java @@ -11,6 +11,7 @@ import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; + import org.elasticsearch.ExceptionsHelper; import org.elasticsearch.http.HttpPipelinedRequest; diff --git a/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpResponse.java b/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpResponse.java index 40d6098b39411..965bc9ffc1dd4 100644 --- a/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpResponse.java +++ b/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpResponse.java @@ -12,6 +12,7 @@ import io.netty.handler.codec.http.HttpHeaders; import io.netty.handler.codec.http.HttpResponseStatus; import io.netty.handler.codec.http.HttpVersion; + import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.http.HttpResponse; import org.elasticsearch.rest.RestStatus; @@ -40,4 +41,3 @@ public HttpHeaders requestHeaders() { return requestHeaders; } } - diff --git a/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpResponseCreator.java b/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpResponseCreator.java index ef88ac95f90be..dce3cf2e6ca96 100644 --- a/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpResponseCreator.java +++ b/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpResponseCreator.java @@ -17,6 +17,7 @@ import io.netty.handler.codec.http.DefaultHttpResponse; import io.netty.handler.codec.http.DefaultLastHttpContent; import io.netty.handler.codec.http.HttpResponse; + import org.elasticsearch.core.Booleans; import org.elasticsearch.transport.netty4.NettyAllocator; diff --git a/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpServerChannel.java b/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpServerChannel.java index ac56bbadc3f21..e798d04f7f2ce 100644 --- a/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpServerChannel.java +++ b/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpServerChannel.java @@ -9,6 +9,7 @@ package org.elasticsearch.http.netty4; import io.netty.channel.Channel; + import org.elasticsearch.action.ActionListener; import org.elasticsearch.core.CompletableContext; import org.elasticsearch.http.HttpServerChannel; diff --git a/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpServerTransport.java b/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpServerTransport.java index aaf09590e474d..64417f7be8fc0 100644 --- a/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpServerTransport.java +++ b/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpServerTransport.java @@ -28,6 +28,7 @@ import io.netty.handler.timeout.ReadTimeoutException; import io.netty.handler.timeout.ReadTimeoutHandler; import io.netty.util.AttributeKey; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.ExceptionsHelper; @@ -40,7 +41,6 @@ import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.common.util.concurrent.EsExecutors; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.core.internal.net.NetUtils; import org.elasticsearch.http.AbstractHttpServerTransport; @@ -49,10 +49,11 @@ import org.elasticsearch.http.HttpReadTimeoutException; import org.elasticsearch.http.HttpServerChannel; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.transport.netty4.Netty4Utils; import org.elasticsearch.transport.netty4.NettyAllocator; import org.elasticsearch.transport.netty4.NettyByteBufSizer; import org.elasticsearch.transport.netty4.SharedGroupFactory; -import org.elasticsearch.transport.netty4.Netty4Utils; +import org.elasticsearch.xcontent.NamedXContentRegistry; import java.net.InetSocketAddress; import java.net.SocketOption; @@ -87,8 +88,9 @@ public class Netty4HttpServerTransport extends AbstractHttpServerTransport { private static final String SETTING_KEY_HTTP_NETTY_MAX_COMPOSITE_BUFFER_COMPONENTS = "http.netty.max_composite_buffer_components"; - public static Setting SETTING_HTTP_NETTY_MAX_COMPOSITE_BUFFER_COMPONENTS = - new Setting<>(SETTING_KEY_HTTP_NETTY_MAX_COMPOSITE_BUFFER_COMPONENTS, (s) -> { + public static Setting SETTING_HTTP_NETTY_MAX_COMPOSITE_BUFFER_COMPONENTS = new Setting<>( + SETTING_KEY_HTTP_NETTY_MAX_COMPOSITE_BUFFER_COMPONENTS, + (s) -> { ByteSizeValue maxContentLength = SETTING_HTTP_MAX_CONTENT_LENGTH.get(s); /* * Netty accumulates buffers containing data from all incoming network packets that make up one HTTP request in an instance of @@ -112,12 +114,18 @@ public class Netty4HttpServerTransport extends AbstractHttpServerTransport { long maxBufferComponents = Math.max(2, Math.min(maxBufferComponentsEstimate, Integer.MAX_VALUE)); return String.valueOf(maxBufferComponents); // Netty's CompositeByteBuf implementation does not allow less than two components. - }, s -> Setting.parseInt(s, 2, Integer.MAX_VALUE, SETTING_KEY_HTTP_NETTY_MAX_COMPOSITE_BUFFER_COMPONENTS), Property.NodeScope); + }, + s -> Setting.parseInt(s, 2, Integer.MAX_VALUE, SETTING_KEY_HTTP_NETTY_MAX_COMPOSITE_BUFFER_COMPONENTS), + Property.NodeScope + ); public static final Setting SETTING_HTTP_WORKER_COUNT = Setting.intSetting("http.netty.worker_count", 0, Property.NodeScope); - public static final Setting SETTING_HTTP_NETTY_RECEIVE_PREDICTOR_SIZE = - Setting.byteSizeSetting("http.netty.receive_predictor_size", new ByteSizeValue(64, ByteSizeUnit.KB), Property.NodeScope); + public static final Setting SETTING_HTTP_NETTY_RECEIVE_PREDICTOR_SIZE = Setting.byteSizeSetting( + "http.netty.receive_predictor_size", + new ByteSizeValue(64, ByteSizeUnit.KB), + Property.NodeScope + ); private final ByteSizeValue maxInitialLineLength; private final ByteSizeValue maxHeaderSize; @@ -134,9 +142,16 @@ public class Netty4HttpServerTransport extends AbstractHttpServerTransport { private volatile ServerBootstrap serverBootstrap; private volatile SharedGroupFactory.SharedGroup sharedGroup; - public Netty4HttpServerTransport(Settings settings, NetworkService networkService, BigArrays bigArrays, ThreadPool threadPool, - NamedXContentRegistry xContentRegistry, Dispatcher dispatcher, ClusterSettings clusterSettings, - SharedGroupFactory sharedGroupFactory) { + public Netty4HttpServerTransport( + Settings settings, + NetworkService networkService, + BigArrays bigArrays, + ThreadPool threadPool, + NamedXContentRegistry xContentRegistry, + Dispatcher dispatcher, + ClusterSettings clusterSettings, + SharedGroupFactory sharedGroupFactory + ) { super(settings, networkService, bigArrays, threadPool, xContentRegistry, dispatcher, clusterSettings); Netty4Utils.setAvailableProcessors(EsExecutors.NODE_PROCESSORS_SETTING.get(settings)); NettyAllocator.logAllocatorDescriptionIfNeeded(); @@ -154,10 +169,17 @@ public Netty4HttpServerTransport(Settings settings, NetworkService networkServic ByteSizeValue receivePredictor = SETTING_HTTP_NETTY_RECEIVE_PREDICTOR_SIZE.get(settings); recvByteBufAllocator = new FixedRecvByteBufAllocator(receivePredictor.bytesAsInt()); - logger.debug("using max_chunk_size[{}], max_header_size[{}], max_initial_line_length[{}], max_content_length[{}], " + - "receive_predictor[{}], max_composite_buffer_components[{}], pipelining_max_events[{}]", - maxChunkSize, maxHeaderSize, maxInitialLineLength, maxContentLength, receivePredictor, maxCompositeBufferComponents, - pipeliningMaxEvents); + logger.debug( + "using max_chunk_size[{}], max_header_size[{}], max_initial_line_length[{}], max_content_length[{}], " + + "receive_predictor[{}], max_composite_buffer_components[{}], pipelining_max_events[{}]", + maxChunkSize, + maxHeaderSize, + maxInitialLineLength, + maxContentLength, + receivePredictor, + maxCompositeBufferComponents, + pipeliningMaxEvents + ); } public Settings settings() { @@ -198,8 +220,10 @@ protected void doStart() { if (SETTING_HTTP_TCP_KEEP_INTERVAL.get(settings) >= 0) { final SocketOption keepIntervalOption = NetUtils.getTcpKeepIntervalSocketOptionOrNull(); if (keepIntervalOption != null) { - serverBootstrap.childOption(NioChannelOption.of(keepIntervalOption), - SETTING_HTTP_TCP_KEEP_INTERVAL.get(settings)); + serverBootstrap.childOption( + NioChannelOption.of(keepIntervalOption), + SETTING_HTTP_TCP_KEEP_INTERVAL.get(settings) + ); } } if (SETTING_HTTP_TCP_KEEP_COUNT.get(settings) >= 0) { @@ -281,7 +305,7 @@ protected static class HttpChannelHandler extends ChannelInitializer { protected HttpChannelHandler(final Netty4HttpServerTransport transport, final HttpHandlingSettings handlingSettings) { this.transport = transport; this.handlingSettings = handlingSettings; - this.requestCreator = new Netty4HttpRequestCreator(); + this.requestCreator = new Netty4HttpRequestCreator(); this.requestHandler = new Netty4HttpRequestHandler(transport); this.responseCreator = new Netty4HttpResponseCreator(); } @@ -295,7 +319,8 @@ protected void initChannel(Channel ch) throws Exception { final HttpRequestDecoder decoder = new HttpRequestDecoder( handlingSettings.getMaxInitialLineLength(), handlingSettings.getMaxHeaderSize(), - handlingSettings.getMaxChunkSize()); + handlingSettings.getMaxChunkSize() + ); decoder.setCumulator(ByteToMessageDecoder.COMPOSITE_CUMULATOR); ch.pipeline().addLast("decoder", decoder); ch.pipeline().addLast("decoder_compress", new HttpContentDecompressor()); diff --git a/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/CopyBytesServerSocketChannel.java b/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/CopyBytesServerSocketChannel.java index a7e019c401892..e0c2ca54d6883 100644 --- a/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/CopyBytesServerSocketChannel.java +++ b/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/CopyBytesServerSocketChannel.java @@ -24,6 +24,7 @@ import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.util.internal.SocketUtils; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; diff --git a/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/CopyBytesSocketChannel.java b/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/CopyBytesSocketChannel.java index 5b24a7839cc2d..768f652afac95 100644 --- a/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/CopyBytesSocketChannel.java +++ b/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/CopyBytesSocketChannel.java @@ -27,8 +27,9 @@ import io.netty.channel.ChannelOutboundBuffer; import io.netty.channel.RecvByteBufAllocator; import io.netty.channel.socket.nio.NioSocketChannel; -import org.elasticsearch.core.SuppressForbidden; + import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.core.SuppressForbidden; import java.io.IOException; import java.nio.ByteBuffer; @@ -36,7 +37,6 @@ import static io.netty.channel.internal.ChannelUtils.MAX_BYTES_PER_GATHERING_WRITE_ATTEMPTED_LOW_THRESHOLD; - /** * This class is adapted from {@link NioSocketChannel} class in the Netty project. It overrides the channel * read/write behavior to ensure that the bytes are always copied to a thread-local direct bytes buffer. This @@ -50,8 +50,9 @@ @SuppressForbidden(reason = "Channel#write") public class CopyBytesSocketChannel extends Netty4NioSocketChannel { - private static final int MAX_BYTES_PER_WRITE = StrictMath.toIntExact(ByteSizeValue.parseBytesSizeValue( - System.getProperty("es.transport.buffer.size", "1m"), "es.transport.buffer.size").getBytes()); + private static final int MAX_BYTES_PER_WRITE = StrictMath.toIntExact( + ByteSizeValue.parseBytesSizeValue(System.getProperty("es.transport.buffer.size", "1m"), "es.transport.buffer.size").getBytes() + ); private static final ThreadLocal ioBuffer = ThreadLocal.withInitial(() -> ByteBuffer.allocateDirect(MAX_BYTES_PER_WRITE)); private final WriteConfig writeConfig = new WriteConfig(); diff --git a/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/Netty4MessageChannelHandler.java b/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/Netty4MessageChannelHandler.java index 22385115782a3..f8ac4228d3d91 100644 --- a/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/Netty4MessageChannelHandler.java +++ b/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/Netty4MessageChannelHandler.java @@ -46,8 +46,15 @@ final class Netty4MessageChannelHandler extends ChannelDuplexHandler { this.transport = transport; final ThreadPool threadPool = transport.getThreadPool(); final Transport.RequestHandlers requestHandlers = transport.getRequestHandlers(); - this.pipeline = new InboundPipeline(transport.getVersion(), transport.getStatsTracker(), NettyAllocator.getRecycler(), - threadPool::relativeTimeInMillis, transport.getInflightBreaker(), requestHandlers::getHandler, transport::inboundMessage); + this.pipeline = new InboundPipeline( + transport.getVersion(), + transport.getStatsTracker(), + NettyAllocator.getRecycler(), + threadPool::relativeTimeInMillis, + transport.getInflightBreaker(), + requestHandlers::getHandler, + transport::inboundMessage + ); } @Override diff --git a/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/Netty4Plugin.java b/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/Netty4Plugin.java index af352a4791ab3..d544fd64928a1 100644 --- a/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/Netty4Plugin.java +++ b/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/Netty4Plugin.java @@ -18,7 +18,6 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.common.util.PageCacheRecycler; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.http.HttpServerTransport; import org.elasticsearch.http.netty4.Netty4HttpServerTransport; import org.elasticsearch.indices.breaker.CircuitBreakerService; @@ -26,6 +25,7 @@ import org.elasticsearch.plugins.Plugin; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.Transport; +import org.elasticsearch.xcontent.NamedXContentRegistry; import java.util.Arrays; import java.util.Collections; @@ -57,32 +57,62 @@ public List> getSettings() { @Override public Settings additionalSettings() { return Settings.builder() - // here we set the netty4 transport and http transport as the default. This is a set once setting - // ie. if another plugin does that as well the server will fail - only one default network can exist! - .put(NetworkModule.HTTP_DEFAULT_TYPE_SETTING.getKey(), NETTY_HTTP_TRANSPORT_NAME) - .put(NetworkModule.TRANSPORT_DEFAULT_TYPE_SETTING.getKey(), NETTY_TRANSPORT_NAME) - .build(); + // here we set the netty4 transport and http transport as the default. This is a set once setting + // ie. if another plugin does that as well the server will fail - only one default network can exist! + .put(NetworkModule.HTTP_DEFAULT_TYPE_SETTING.getKey(), NETTY_HTTP_TRANSPORT_NAME) + .put(NetworkModule.TRANSPORT_DEFAULT_TYPE_SETTING.getKey(), NETTY_TRANSPORT_NAME) + .build(); } @Override - public Map> getTransports(Settings settings, ThreadPool threadPool, PageCacheRecycler pageCacheRecycler, - CircuitBreakerService circuitBreakerService, - NamedWriteableRegistry namedWriteableRegistry, NetworkService networkService) { - return Collections.singletonMap(NETTY_TRANSPORT_NAME, () -> new Netty4Transport(settings, Version.CURRENT, threadPool, - networkService, pageCacheRecycler, namedWriteableRegistry, circuitBreakerService, getSharedGroupFactory(settings))); + public Map> getTransports( + Settings settings, + ThreadPool threadPool, + PageCacheRecycler pageCacheRecycler, + CircuitBreakerService circuitBreakerService, + NamedWriteableRegistry namedWriteableRegistry, + NetworkService networkService + ) { + return Collections.singletonMap( + NETTY_TRANSPORT_NAME, + () -> new Netty4Transport( + settings, + Version.CURRENT, + threadPool, + networkService, + pageCacheRecycler, + namedWriteableRegistry, + circuitBreakerService, + getSharedGroupFactory(settings) + ) + ); } @Override - public Map> getHttpTransports(Settings settings, ThreadPool threadPool, BigArrays bigArrays, - PageCacheRecycler pageCacheRecycler, - CircuitBreakerService circuitBreakerService, - NamedXContentRegistry xContentRegistry, - NetworkService networkService, - HttpServerTransport.Dispatcher dispatcher, - ClusterSettings clusterSettings) { - return Collections.singletonMap(NETTY_HTTP_TRANSPORT_NAME, - () -> new Netty4HttpServerTransport(settings, networkService, bigArrays, threadPool, xContentRegistry, dispatcher, - clusterSettings, getSharedGroupFactory(settings))); + public Map> getHttpTransports( + Settings settings, + ThreadPool threadPool, + BigArrays bigArrays, + PageCacheRecycler pageCacheRecycler, + CircuitBreakerService circuitBreakerService, + NamedXContentRegistry xContentRegistry, + NetworkService networkService, + HttpServerTransport.Dispatcher dispatcher, + ClusterSettings clusterSettings + ) { + return Collections.singletonMap( + NETTY_HTTP_TRANSPORT_NAME, + () -> new Netty4HttpServerTransport( + settings, + networkService, + bigArrays, + threadPool, + xContentRegistry, + dispatcher, + clusterSettings, + getSharedGroupFactory(settings) + ) + ); } private SharedGroupFactory getSharedGroupFactory(Settings settings) { diff --git a/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/Netty4TcpChannel.java b/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/Netty4TcpChannel.java index e8f853483daab..5ea02e7ccf64f 100644 --- a/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/Netty4TcpChannel.java +++ b/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/Netty4TcpChannel.java @@ -11,11 +11,12 @@ import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelPromise; + import org.elasticsearch.ExceptionsHelper; import org.elasticsearch.action.ActionListener; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.core.CompletableContext; +import org.elasticsearch.core.Nullable; import org.elasticsearch.transport.TcpChannel; import org.elasticsearch.transport.TransportException; @@ -145,10 +146,13 @@ public Channel getNettyChannel() { @Override public String toString() { - return "Netty4TcpChannel{" + - "localAddress=" + getLocalAddress() + - ", remoteAddress=" + channel.remoteAddress() + - ", profile=" + profile + - '}'; + return "Netty4TcpChannel{" + + "localAddress=" + + getLocalAddress() + + ", remoteAddress=" + + channel.remoteAddress() + + ", profile=" + + profile + + '}'; } } diff --git a/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/Netty4TcpServerChannel.java b/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/Netty4TcpServerChannel.java index bb25e031dbc35..429416dd1709a 100644 --- a/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/Netty4TcpServerChannel.java +++ b/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/Netty4TcpServerChannel.java @@ -9,6 +9,7 @@ package org.elasticsearch.transport.netty4; import io.netty.channel.Channel; + import org.elasticsearch.action.ActionListener; import org.elasticsearch.core.CompletableContext; import org.elasticsearch.transport.TcpServerChannel; diff --git a/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/Netty4Transport.java b/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/Netty4Transport.java index abb9c9a71bddd..88d4a03fcdf93 100644 --- a/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/Netty4Transport.java +++ b/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/Netty4Transport.java @@ -63,19 +63,29 @@ public class Netty4Transport extends TcpTransport { private static final Logger logger = LogManager.getLogger(Netty4Transport.class); - public static final Setting WORKER_COUNT = - new Setting<>("transport.netty.worker_count", - (s) -> Integer.toString(EsExecutors.allocatedProcessors(s)), - (s) -> Setting.parseInt(s, 1, "transport.netty.worker_count"), Property.NodeScope); + public static final Setting WORKER_COUNT = new Setting<>( + "transport.netty.worker_count", + (s) -> Integer.toString(EsExecutors.allocatedProcessors(s)), + (s) -> Setting.parseInt(s, 1, "transport.netty.worker_count"), + Property.NodeScope + ); public static final Setting NETTY_RECEIVE_PREDICTOR_SIZE = Setting.byteSizeSetting( - "transport.netty.receive_predictor_size", new ByteSizeValue(64, ByteSizeUnit.KB), Property.NodeScope); - public static final Setting NETTY_RECEIVE_PREDICTOR_MIN = - byteSizeSetting("transport.netty.receive_predictor_min", NETTY_RECEIVE_PREDICTOR_SIZE, Property.NodeScope); - public static final Setting NETTY_RECEIVE_PREDICTOR_MAX = - byteSizeSetting("transport.netty.receive_predictor_max", NETTY_RECEIVE_PREDICTOR_SIZE, Property.NodeScope); - public static final Setting NETTY_BOSS_COUNT = - intSetting("transport.netty.boss_count", 1, 1, Property.NodeScope); + "transport.netty.receive_predictor_size", + new ByteSizeValue(64, ByteSizeUnit.KB), + Property.NodeScope + ); + public static final Setting NETTY_RECEIVE_PREDICTOR_MIN = byteSizeSetting( + "transport.netty.receive_predictor_min", + NETTY_RECEIVE_PREDICTOR_SIZE, + Property.NodeScope + ); + public static final Setting NETTY_RECEIVE_PREDICTOR_MAX = byteSizeSetting( + "transport.netty.receive_predictor_max", + NETTY_RECEIVE_PREDICTOR_SIZE, + Property.NodeScope + ); + public static final Setting NETTY_BOSS_COUNT = intSetting("transport.netty.boss_count", 1, 1, Property.NodeScope); private final SharedGroupFactory sharedGroupFactory; private final RecvByteBufAllocator recvByteBufAllocator; @@ -85,9 +95,16 @@ public class Netty4Transport extends TcpTransport { private volatile Bootstrap clientBootstrap; private volatile SharedGroupFactory.SharedGroup sharedGroup; - public Netty4Transport(Settings settings, Version version, ThreadPool threadPool, NetworkService networkService, - PageCacheRecycler pageCacheRecycler, NamedWriteableRegistry namedWriteableRegistry, - CircuitBreakerService circuitBreakerService, SharedGroupFactory sharedGroupFactory) { + public Netty4Transport( + Settings settings, + Version version, + ThreadPool threadPool, + NetworkService networkService, + PageCacheRecycler pageCacheRecycler, + NamedWriteableRegistry namedWriteableRegistry, + CircuitBreakerService circuitBreakerService, + SharedGroupFactory sharedGroupFactory + ) { super(settings, version, threadPool, pageCacheRecycler, circuitBreakerService, namedWriteableRegistry, networkService); Netty4Utils.setAvailableProcessors(EsExecutors.NODE_PROCESSORS_SETTING.get(settings)); NettyAllocator.logAllocatorDescriptionIfNeeded(); @@ -99,8 +116,11 @@ public Netty4Transport(Settings settings, Version version, ThreadPool threadPool if (receivePredictorMax.getBytes() == receivePredictorMin.getBytes()) { recvByteBufAllocator = new FixedRecvByteBufAllocator((int) receivePredictorMax.getBytes()); } else { - recvByteBufAllocator = new AdaptiveRecvByteBufAllocator((int) receivePredictorMin.getBytes(), - (int) receivePredictorMin.getBytes(), (int) receivePredictorMax.getBytes()); + recvByteBufAllocator = new AdaptiveRecvByteBufAllocator( + (int) receivePredictorMin.getBytes(), + (int) receivePredictorMin.getBytes(), + (int) receivePredictorMax.getBytes() + ); } } @@ -178,9 +198,16 @@ private Bootstrap createClientBootstrap(SharedGroupFactory.SharedGroup sharedGro private void createServerBootstrap(ProfileSettings profileSettings, SharedGroupFactory.SharedGroup sharedGroup) { String name = profileSettings.profileName; if (logger.isDebugEnabled()) { - logger.debug("using profile[{}], worker_count[{}], port[{}], bind_host[{}], publish_host[{}], receive_predictor[{}->{}]", - name, sharedGroupFactory.getTransportWorkerCount(), profileSettings.portOrRange, profileSettings.bindHosts, - profileSettings.publishHosts, receivePredictorMin, receivePredictorMax); + logger.debug( + "using profile[{}], worker_count[{}], port[{}], bind_host[{}], publish_host[{}], receive_predictor[{}->{}]", + name, + sharedGroupFactory.getTransportWorkerCount(), + profileSettings.portOrRange, + profileSettings.bindHosts, + profileSettings.publishHosts, + receivePredictorMin, + receivePredictorMax + ); } final ServerBootstrap serverBootstrap = new ServerBootstrap(); diff --git a/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/Netty4Utils.java b/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/Netty4Utils.java index b5343eda99cbd..26579ed12b4d9 100644 --- a/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/Netty4Utils.java +++ b/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/Netty4Utils.java @@ -12,11 +12,12 @@ import io.netty.buffer.CompositeByteBuf; import io.netty.buffer.Unpooled; import io.netty.util.NettyRuntime; + import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.BytesRefIterator; -import org.elasticsearch.core.Booleans; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.core.Booleans; import java.io.IOException; import java.nio.ByteBuffer; @@ -54,10 +55,11 @@ public static void setAvailableProcessors(final int availableProcessors) { * in Netty and our previous value did not take, bail. */ final String message = String.format( - Locale.ROOT, - "available processors value [%d] did not match current value [%d]", - availableProcessors, - NettyRuntime.availableProcessors()); + Locale.ROOT, + "available processors value [%d] did not match current value [%d]", + availableProcessors, + NettyRuntime.availableProcessors() + ); throw new IllegalStateException(message); } } diff --git a/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/NettyAllocator.java b/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/NettyAllocator.java index 3133dd623c325..b5d96ba3ea60e 100644 --- a/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/NettyAllocator.java +++ b/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/NettyAllocator.java @@ -46,7 +46,8 @@ public class NettyAllocator { if (Booleans.parseBoolean(System.getProperty(USE_NETTY_DEFAULT), false)) { ALLOCATOR = ByteBufAllocator.DEFAULT; SUGGESTED_MAX_ALLOCATION_SIZE = 1024 * 1024; - DESCRIPTION = "[name=netty_default, suggested_max_allocation_size=" + new ByteSizeValue(SUGGESTED_MAX_ALLOCATION_SIZE) + DESCRIPTION = "[name=netty_default, suggested_max_allocation_size=" + + new ByteSizeValue(SUGGESTED_MAX_ALLOCATION_SIZE) + ", factors={es.unsafe.use_netty_default_allocator=true}]"; } else { final long heapSizeInBytes = JvmInfo.jvmInfo().getMem().getHeapMax().getBytes(); @@ -66,11 +67,17 @@ public class NettyAllocator { } else { SUGGESTED_MAX_ALLOCATION_SIZE = 1024 * 1024; } - DESCRIPTION = "[name=unpooled, suggested_max_allocation_size=" + new ByteSizeValue(SUGGESTED_MAX_ALLOCATION_SIZE) - + ", factors={es.unsafe.use_unpooled_allocator=" + System.getProperty(USE_UNPOOLED) - + ", g1gc_enabled=" + g1gcEnabled - + ", g1gc_region_size=" + g1gcRegionSize - + ", heap_size=" + heapSize + "}]"; + DESCRIPTION = "[name=unpooled, suggested_max_allocation_size=" + + new ByteSizeValue(SUGGESTED_MAX_ALLOCATION_SIZE) + + ", factors={es.unsafe.use_unpooled_allocator=" + + System.getProperty(USE_UNPOOLED) + + ", g1gc_enabled=" + + g1gcEnabled + + ", g1gc_region_size=" + + g1gcRegionSize + + ", heap_size=" + + heapSize + + "}]"; } else { int nHeapArena = PooledByteBufAllocator.defaultNumHeapArena(); int pageSize; @@ -95,16 +102,31 @@ public class NettyAllocator { int smallCacheSize = PooledByteBufAllocator.defaultSmallCacheSize(); int normalCacheSize = PooledByteBufAllocator.defaultNormalCacheSize(); boolean useCacheForAllThreads = PooledByteBufAllocator.defaultUseCacheForAllThreads(); - delegate = new PooledByteBufAllocator(false, nHeapArena, 0, pageSize, maxOrder, tinyCacheSize, - smallCacheSize, normalCacheSize, useCacheForAllThreads); + delegate = new PooledByteBufAllocator( + false, + nHeapArena, + 0, + pageSize, + maxOrder, + tinyCacheSize, + smallCacheSize, + normalCacheSize, + useCacheForAllThreads + ); int chunkSizeInBytes = pageSize << maxOrder; ByteSizeValue chunkSize = new ByteSizeValue(chunkSizeInBytes); SUGGESTED_MAX_ALLOCATION_SIZE = chunkSizeInBytes; - DESCRIPTION = "[name=elasticsearch_configured, chunk_size=" + chunkSize - + ", suggested_max_allocation_size=" + new ByteSizeValue(SUGGESTED_MAX_ALLOCATION_SIZE) - + ", factors={es.unsafe.use_netty_default_chunk_and_page_size=" + useDefaultChunkAndPageSize() - + ", g1gc_enabled=" + g1gcEnabled - + ", g1gc_region_size=" + g1gcRegionSize + "}]"; + DESCRIPTION = "[name=elasticsearch_configured, chunk_size=" + + chunkSize + + ", suggested_max_allocation_size=" + + new ByteSizeValue(SUGGESTED_MAX_ALLOCATION_SIZE) + + ", factors={es.unsafe.use_netty_default_chunk_and_page_size=" + + useDefaultChunkAndPageSize() + + ", g1gc_enabled=" + + g1gcEnabled + + ", g1gc_region_size=" + + g1gcRegionSize + + "}]"; } ALLOCATOR = new NoDirectBuffers(delegate); } diff --git a/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/SharedGroupFactory.java b/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/SharedGroupFactory.java index cfa012a707ac9..14c2c13ed7669 100644 --- a/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/SharedGroupFactory.java +++ b/modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/SharedGroupFactory.java @@ -11,6 +11,7 @@ import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.util.concurrent.Future; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.common.settings.Settings; @@ -65,8 +66,10 @@ public synchronized SharedGroup getHttpGroup() { return getGenericGroup(); } else { if (dedicatedHttpGroup == null) { - NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup(httpWorkerCount, - daemonThreadFactory(settings, HttpServerTransport.HTTP_SERVER_WORKER_THREAD_NAME_PREFIX)); + NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup( + httpWorkerCount, + daemonThreadFactory(settings, HttpServerTransport.HTTP_SERVER_WORKER_THREAD_NAME_PREFIX) + ); dedicatedHttpGroup = new SharedGroup(new RefCountedGroup(eventLoopGroup)); } return dedicatedHttpGroup; @@ -75,8 +78,10 @@ public synchronized SharedGroup getHttpGroup() { private SharedGroup getGenericGroup() { if (genericGroup == null) { - EventLoopGroup eventLoopGroup = new NioEventLoopGroup(workerCount, - EsExecutors.daemonThreadFactory(settings, TcpTransport.TRANSPORT_WORKER_THREAD_NAME_PREFIX)); + EventLoopGroup eventLoopGroup = new NioEventLoopGroup( + workerCount, + EsExecutors.daemonThreadFactory(settings, TcpTransport.TRANSPORT_WORKER_THREAD_NAME_PREFIX) + ); this.genericGroup = new RefCountedGroup(eventLoopGroup); } else { genericGroup.incRef(); diff --git a/modules/transport-netty4/src/test/java/org/elasticsearch/http/netty4/Netty4BadRequestTests.java b/modules/transport-netty4/src/test/java/org/elasticsearch/http/netty4/Netty4BadRequestTests.java index 7933243cf3696..bbb21b3f3d864 100644 --- a/modules/transport-netty4/src/test/java/org/elasticsearch/http/netty4/Netty4BadRequestTests.java +++ b/modules/transport-netty4/src/test/java/org/elasticsearch/http/netty4/Netty4BadRequestTests.java @@ -10,6 +10,7 @@ import io.netty.handler.codec.http.FullHttpResponse; import io.netty.util.ReferenceCounted; + import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.network.NetworkService; import org.elasticsearch.common.settings.ClusterSettings; @@ -78,15 +79,26 @@ public void dispatchBadRequest(RestChannel channel, ThreadContext threadContext, }; Settings settings = Settings.builder().put(HttpTransportSettings.SETTING_HTTP_PORT.getKey(), getPortRange()).build(); - try (HttpServerTransport httpServerTransport = new Netty4HttpServerTransport(settings, networkService, bigArrays, threadPool, - xContentRegistry(), dispatcher, new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), - new SharedGroupFactory(Settings.EMPTY))) { + try ( + HttpServerTransport httpServerTransport = new Netty4HttpServerTransport( + settings, + networkService, + bigArrays, + threadPool, + xContentRegistry(), + dispatcher, + new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), + new SharedGroupFactory(Settings.EMPTY) + ) + ) { httpServerTransport.start(); final TransportAddress transportAddress = randomFrom(httpServerTransport.boundAddress().boundAddresses()); try (Netty4HttpClient nettyHttpClient = new Netty4HttpClient()) { - final Collection responses = - nettyHttpClient.get(transportAddress.address(), "/_cluster/settings?pretty=%"); + final Collection responses = nettyHttpClient.get( + transportAddress.address(), + "/_cluster/settings?pretty=%" + ); try { assertThat(responses, hasSize(1)); assertThat(responses.iterator().next().status().code(), equalTo(400)); @@ -96,7 +108,9 @@ public void dispatchBadRequest(RestChannel channel, ThreadContext threadContext, assertThat( responseBodies.iterator().next(), containsString( - "\"reason\":\"java.lang.IllegalArgumentException: unterminated escape sequence at end of string: %\"")); + "\"reason\":\"java.lang.IllegalArgumentException: unterminated escape sequence at end of string: %\"" + ) + ); } finally { responses.forEach(ReferenceCounted::release); } diff --git a/modules/transport-netty4/src/test/java/org/elasticsearch/http/netty4/Netty4HttpClient.java b/modules/transport-netty4/src/test/java/org/elasticsearch/http/netty4/Netty4HttpClient.java index 7bc2f09f64a11..433b2fb8f3732 100644 --- a/modules/transport-netty4/src/test/java/org/elasticsearch/http/netty4/Netty4HttpClient.java +++ b/modules/transport-netty4/src/test/java/org/elasticsearch/http/netty4/Netty4HttpClient.java @@ -31,9 +31,10 @@ import io.netty.handler.codec.http.HttpResponse; import io.netty.handler.codec.http.HttpResponseDecoder; import io.netty.handler.codec.http.HttpVersion; -import org.elasticsearch.core.Tuple; + import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.core.Tuple; import org.elasticsearch.tasks.Task; import org.elasticsearch.transport.netty4.NettyAllocator; @@ -75,8 +76,7 @@ static Collection returnOpaqueIds(Collection responses private final Bootstrap clientBootstrap; Netty4HttpClient() { - clientBootstrap = new Bootstrap() - .channel(NettyAllocator.getChannelType()) + clientBootstrap = new Bootstrap().channel(NettyAllocator.getChannelType()) .option(ChannelOption.ALLOCATOR, NettyAllocator.getAllocator()) .group(new NioEventLoopGroup(1)); } @@ -109,8 +109,11 @@ public final Collection put(SocketAddress remoteAddress, List< return processRequestsWithBody(HttpMethod.PUT, remoteAddress, urisAndBodies); } - private List processRequestsWithBody(HttpMethod method, SocketAddress remoteAddress, List> urisAndBodies) throws InterruptedException { + private List processRequestsWithBody( + HttpMethod method, + SocketAddress remoteAddress, + List> urisAndBodies + ) throws InterruptedException { List requests = new ArrayList<>(urisAndBodies.size()); for (Tuple uriAndBody : urisAndBodies) { ByteBuf content = Unpooled.copiedBuffer(uriAndBody.v2(), StandardCharsets.UTF_8); @@ -123,9 +126,8 @@ private List processRequestsWithBody(HttpMethod method, Socket return sendRequests(remoteAddress, requests); } - private synchronized List sendRequests( - final SocketAddress remoteAddress, - final Collection requests) throws InterruptedException { + private synchronized List sendRequests(final SocketAddress remoteAddress, final Collection requests) + throws InterruptedException { final CountDownLatch latch = new CountDownLatch(requests.size()); final List content = Collections.synchronizedList(new ArrayList<>(requests.size())); diff --git a/modules/transport-netty4/src/test/java/org/elasticsearch/http/netty4/Netty4HttpPipeliningHandlerTests.java b/modules/transport-netty4/src/test/java/org/elasticsearch/http/netty4/Netty4HttpPipeliningHandlerTests.java index a5452e554953d..4deb396a5e185 100644 --- a/modules/transport-netty4/src/test/java/org/elasticsearch/http/netty4/Netty4HttpPipeliningHandlerTests.java +++ b/modules/transport-netty4/src/test/java/org/elasticsearch/http/netty4/Netty4HttpPipeliningHandlerTests.java @@ -17,6 +17,7 @@ import io.netty.handler.codec.http.FullHttpResponse; import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.QueryStringDecoder; + import org.elasticsearch.common.Randomness; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; @@ -76,8 +77,10 @@ private void shutdownExecutorService() throws InterruptedException { public void testThatPipeliningWorksWithFastSerializedRequests() throws InterruptedException { final int numberOfRequests = randomIntBetween(2, 128); - final EmbeddedChannel embeddedChannel = new EmbeddedChannel(new Netty4HttpPipeliningHandler(logger, numberOfRequests), - new WorkEmulatorHandler()); + final EmbeddedChannel embeddedChannel = new EmbeddedChannel( + new Netty4HttpPipeliningHandler(logger, numberOfRequests), + new WorkEmulatorHandler() + ); for (int i = 0; i < numberOfRequests; i++) { embeddedChannel.writeInbound(createHttpRequest("/" + String.valueOf(i))); @@ -103,8 +106,10 @@ public void testThatPipeliningWorksWithFastSerializedRequests() throws Interrupt public void testThatPipeliningWorksWhenSlowRequestsInDifferentOrder() throws InterruptedException { final int numberOfRequests = randomIntBetween(2, 128); - final EmbeddedChannel embeddedChannel = new EmbeddedChannel(new Netty4HttpPipeliningHandler(logger, numberOfRequests), - new WorkEmulatorHandler()); + final EmbeddedChannel embeddedChannel = new EmbeddedChannel( + new Netty4HttpPipeliningHandler(logger, numberOfRequests), + new WorkEmulatorHandler() + ); for (int i = 0; i < numberOfRequests; i++) { embeddedChannel.writeInbound(createHttpRequest("/" + String.valueOf(i))); @@ -133,8 +138,10 @@ public void testThatPipeliningWorksWhenSlowRequestsInDifferentOrder() throws Int public void testThatPipeliningClosesConnectionWithTooManyEvents() throws InterruptedException { final int numberOfRequests = randomIntBetween(2, 128); - final EmbeddedChannel embeddedChannel = new EmbeddedChannel(new Netty4HttpPipeliningHandler(logger, numberOfRequests), - new WorkEmulatorHandler()); + final EmbeddedChannel embeddedChannel = new EmbeddedChannel( + new Netty4HttpPipeliningHandler(logger, numberOfRequests), + new WorkEmulatorHandler() + ); for (int i = 0; i < 1 + numberOfRequests + 1; i++) { embeddedChannel.writeInbound(createHttpRequest("/" + Integer.toString(i))); @@ -161,8 +168,7 @@ public void testThatPipeliningClosesConnectionWithTooManyEvents() throws Interru public void testPipeliningRequestsAreReleased() throws InterruptedException { final int numberOfRequests = 10; - final EmbeddedChannel embeddedChannel = - new EmbeddedChannel(new Netty4HttpPipeliningHandler(logger, numberOfRequests + 1)); + final EmbeddedChannel embeddedChannel = new EmbeddedChannel(new Netty4HttpPipeliningHandler(logger, numberOfRequests + 1)); for (int i = 0; i < numberOfRequests; i++) { embeddedChannel.writeInbound(createHttpRequest("/" + i)); @@ -193,7 +199,6 @@ public void testPipeliningRequestsAreReleased() throws InterruptedException { } } - private void assertReadHttpMessageHasContent(EmbeddedChannel embeddedChannel, String expectedContent) { FullHttpResponse response = (FullHttpResponse) embeddedChannel.outboundMessages().poll(); assertNotNull("Expected response to exist, maybe you did not wait long enough?", response); diff --git a/modules/transport-netty4/src/test/java/org/elasticsearch/http/netty4/Netty4HttpServerPipeliningTests.java b/modules/transport-netty4/src/test/java/org/elasticsearch/http/netty4/Netty4HttpServerPipeliningTests.java index 29a3f6fe02c45..4902f5842ed1b 100644 --- a/modules/transport-netty4/src/test/java/org/elasticsearch/http/netty4/Netty4HttpServerPipeliningTests.java +++ b/modules/transport-netty4/src/test/java/org/elasticsearch/http/netty4/Netty4HttpServerPipeliningTests.java @@ -17,6 +17,7 @@ import io.netty.channel.SimpleChannelInboundHandler; import io.netty.handler.codec.http.FullHttpResponse; import io.netty.util.ReferenceCounted; + import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.network.NetworkService; import org.elasticsearch.common.settings.ClusterSettings; @@ -70,9 +71,7 @@ public void shutdown() throws Exception { } public void testThatHttpPipeliningWorks() throws Exception { - final Settings settings = Settings.builder() - .put("http.port", "0") - .build(); + final Settings settings = Settings.builder().put("http.port", "0").build(); try (HttpServerTransport httpServerTransport = new CustomNettyHttpServerTransport(settings)) { httpServerTransport.start(); final TransportAddress transportAddress = randomFrom(httpServerTransport.boundAddress().boundAddresses()); @@ -88,7 +87,7 @@ public void testThatHttpPipeliningWorks() throws Exception { } try (Netty4HttpClient nettyHttpClient = new Netty4HttpClient()) { - Collection responses = nettyHttpClient.get(transportAddress.address(), requests.toArray(new String[]{})); + Collection responses = nettyHttpClient.get(transportAddress.address(), requests.toArray(new String[] {})); try { Collection responseBodies = Netty4HttpClient.returnHttpResponseBodies(responses); assertThat(responseBodies, contains(requests.toArray())); @@ -104,12 +103,16 @@ class CustomNettyHttpServerTransport extends Netty4HttpServerTransport { private final ExecutorService executorService = Executors.newCachedThreadPool(); CustomNettyHttpServerTransport(final Settings settings) { - super(settings, + super( + settings, Netty4HttpServerPipeliningTests.this.networkService, Netty4HttpServerPipeliningTests.this.bigArrays, Netty4HttpServerPipeliningTests.this.threadPool, - xContentRegistry(), new NullDispatcher(), new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), - new SharedGroupFactory(settings)); + xContentRegistry(), + new NullDispatcher(), + new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), + new SharedGroupFactory(settings) + ); } @Override @@ -180,8 +183,10 @@ public void run() { final ByteBuf buffer = Unpooled.copiedBuffer(uri, StandardCharsets.UTF_8); - HttpResponse response = - pipelinedRequest.createResponse(RestStatus.OK, new BytesArray(uri.getBytes(StandardCharsets.UTF_8))); + HttpResponse response = pipelinedRequest.createResponse( + RestStatus.OK, + new BytesArray(uri.getBytes(StandardCharsets.UTF_8)) + ); response.addHeader("content-length", Integer.toString(buffer.readableBytes())); final boolean slow = uri.matches("/slow/\\d+"); diff --git a/modules/transport-netty4/src/test/java/org/elasticsearch/http/netty4/Netty4HttpServerTransportTests.java b/modules/transport-netty4/src/test/java/org/elasticsearch/http/netty4/Netty4HttpServerTransportTests.java index 218a7ea989175..c01a33f1749da 100644 --- a/modules/transport-netty4/src/test/java/org/elasticsearch/http/netty4/Netty4HttpServerTransportTests.java +++ b/modules/transport-netty4/src/test/java/org/elasticsearch/http/netty4/Netty4HttpServerTransportTests.java @@ -32,6 +32,7 @@ import io.netty.handler.codec.http.HttpResponseStatus; import io.netty.handler.codec.http.HttpUtil; import io.netty.handler.codec.http.HttpVersion; + import org.apache.logging.log4j.message.ParameterizedMessage; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.bytes.BytesArray; @@ -42,10 +43,10 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.TransportAddress; import org.elasticsearch.common.unit.ByteSizeValue; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.MockBigArrays; import org.elasticsearch.common.util.MockPageCacheRecycler; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.http.AbstractHttpServerTransportTestCase; import org.elasticsearch.http.BindHttpException; import org.elasticsearch.http.CorsHandler; @@ -59,8 +60,8 @@ import org.elasticsearch.test.rest.FakeRestRequest; import org.elasticsearch.threadpool.TestThreadPool; import org.elasticsearch.threadpool.ThreadPool; -import org.elasticsearch.transport.netty4.SharedGroupFactory; import org.elasticsearch.transport.netty4.NettyAllocator; +import org.elasticsearch.transport.netty4.SharedGroupFactory; import org.junit.After; import org.junit.Before; @@ -131,8 +132,7 @@ public void testExpectContinueHeaderContentLengthTooLong() throws InterruptedExc final int maxContentLength = randomIntBetween(1, 104857600); final Settings settings = createBuilderWithPort().put(key, maxContentLength + "b").build(); final int contentLength = randomIntBetween(maxContentLength + 1, Integer.MAX_VALUE); - runExpectHeaderTest( - settings, HttpHeaderValues.CONTINUE.toString(), contentLength, HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE); + runExpectHeaderTest(settings, HttpHeaderValues.CONTINUE.toString(), contentLength, HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE); } /** @@ -145,10 +145,11 @@ public void testExpectUnsupportedExpectation() throws InterruptedException { } private void runExpectHeaderTest( - final Settings settings, - final String expectation, - final int contentLength, - final HttpResponseStatus expectedStatus) throws InterruptedException { + final Settings settings, + final String expectation, + final int contentLength, + final HttpResponseStatus expectedStatus + ) throws InterruptedException { final HttpServerTransport.Dispatcher dispatcher = new HttpServerTransport.Dispatcher() { @Override public void dispatchRequest(RestRequest request, RestChannel channel, ThreadContext threadContext) { @@ -157,13 +158,25 @@ public void dispatchRequest(RestRequest request, RestChannel channel, ThreadCont @Override public void dispatchBadRequest(RestChannel channel, ThreadContext threadContext, Throwable cause) { - logger.error(new ParameterizedMessage("--> Unexpected bad request [{}]", - FakeRestRequest.requestToString(channel.request())), cause); + logger.error( + new ParameterizedMessage("--> Unexpected bad request [{}]", FakeRestRequest.requestToString(channel.request())), + cause + ); throw new AssertionError(); } }; - try (Netty4HttpServerTransport transport = new Netty4HttpServerTransport(settings, networkService, bigArrays, threadPool, - xContentRegistry(), dispatcher, clusterSettings, new SharedGroupFactory(settings))) { + try ( + Netty4HttpServerTransport transport = new Netty4HttpServerTransport( + settings, + networkService, + bigArrays, + threadPool, + xContentRegistry(), + dispatcher, + clusterSettings, + new SharedGroupFactory(settings) + ) + ) { transport.start(); final TransportAddress remoteAddress = randomFrom(transport.boundAddress().boundAddresses()); try (Netty4HttpClient client = new Netty4HttpClient()) { @@ -175,13 +188,18 @@ public void dispatchBadRequest(RestChannel channel, ThreadContext threadContext, try { assertThat(response.status(), equalTo(expectedStatus)); if (expectedStatus.equals(HttpResponseStatus.CONTINUE)) { - final FullHttpRequest continuationRequest = - new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/", Unpooled.EMPTY_BUFFER); + final FullHttpRequest continuationRequest = new DefaultFullHttpRequest( + HttpVersion.HTTP_1_1, + HttpMethod.POST, + "/", + Unpooled.EMPTY_BUFFER + ); final FullHttpResponse continuationResponse = client.send(remoteAddress.address(), continuationRequest); try { assertThat(continuationResponse.status(), is(HttpResponseStatus.OK)); assertThat( - new String(ByteBufUtil.getBytes(continuationResponse.content()), StandardCharsets.UTF_8), is("done") + new String(ByteBufUtil.getBytes(continuationResponse.content()), StandardCharsets.UTF_8), + is("done") ); } finally { continuationResponse.release(); @@ -196,21 +214,38 @@ public void dispatchBadRequest(RestChannel channel, ThreadContext threadContext, public void testBindUnavailableAddress() { Settings initialSettings = createSettings(); - try (Netty4HttpServerTransport transport = new Netty4HttpServerTransport(initialSettings, networkService, bigArrays, threadPool, - xContentRegistry(), new NullDispatcher(), clusterSettings, new SharedGroupFactory(Settings.EMPTY))) { + try ( + Netty4HttpServerTransport transport = new Netty4HttpServerTransport( + initialSettings, + networkService, + bigArrays, + threadPool, + xContentRegistry(), + new NullDispatcher(), + clusterSettings, + new SharedGroupFactory(Settings.EMPTY) + ) + ) { transport.start(); TransportAddress remoteAddress = randomFrom(transport.boundAddress().boundAddresses()); Settings settings = Settings.builder() .put("http.port", remoteAddress.getPort()) .put("network.host", remoteAddress.getAddress()) .build(); - try (Netty4HttpServerTransport otherTransport = new Netty4HttpServerTransport(settings, networkService, bigArrays, threadPool, - xContentRegistry(), new NullDispatcher(), clusterSettings, new SharedGroupFactory(settings))) { + try ( + Netty4HttpServerTransport otherTransport = new Netty4HttpServerTransport( + settings, + networkService, + bigArrays, + threadPool, + xContentRegistry(), + new NullDispatcher(), + clusterSettings, + new SharedGroupFactory(settings) + ) + ) { BindHttpException bindHttpException = expectThrows(BindHttpException.class, otherTransport::start); - assertEquals( - "Failed to bind to " + NetworkAddress.format(remoteAddress.address()), - bindHttpException.getMessage() - ); + assertEquals("Failed to bind to " + NetworkAddress.format(remoteAddress.address()), bindHttpException.getMessage()); } } } @@ -249,9 +284,18 @@ public void dispatchBadRequest(final RestChannel channel, final ThreadContext th settings = createBuilderWithPort().put(httpMaxInitialLineLengthSetting.getKey(), maxInitialLineLength + "b").build(); } - try (Netty4HttpServerTransport transport = new Netty4HttpServerTransport( - settings, networkService, bigArrays, threadPool, xContentRegistry(), dispatcher, clusterSettings, - new SharedGroupFactory(settings))) { + try ( + Netty4HttpServerTransport transport = new Netty4HttpServerTransport( + settings, + networkService, + bigArrays, + threadPool, + xContentRegistry(), + dispatcher, + clusterSettings, + new SharedGroupFactory(settings) + ) + ) { transport.start(); final TransportAddress remoteAddress = randomFrom(transport.boundAddress().boundAddresses()); @@ -264,7 +308,8 @@ settings, networkService, bigArrays, threadPool, xContentRegistry(), dispatcher, assertThat(response.status(), equalTo(HttpResponseStatus.BAD_REQUEST)); assertThat( new String(response.content().array(), Charset.forName("UTF-8")), - containsString("you sent a bad request and you should feel bad")); + containsString("you sent a bad request and you should feel bad") + ); } finally { response.release(); } @@ -292,16 +337,27 @@ public void dispatchRequest(final RestRequest request, final RestChannel channel @Override public void dispatchBadRequest(final RestChannel channel, final ThreadContext threadContext, final Throwable cause) { - logger.error(new ParameterizedMessage("--> Unexpected bad request [{}]", - FakeRestRequest.requestToString(channel.request())), cause); + logger.error( + new ParameterizedMessage("--> Unexpected bad request [{}]", FakeRestRequest.requestToString(channel.request())), + cause + ); throw new AssertionError(); } }; - try (Netty4HttpServerTransport transport = new Netty4HttpServerTransport( - Settings.EMPTY, networkService, bigArrays, threadPool, xContentRegistry(), dispatcher, clusterSettings, - new SharedGroupFactory(Settings.EMPTY))) { + try ( + Netty4HttpServerTransport transport = new Netty4HttpServerTransport( + Settings.EMPTY, + networkService, + bigArrays, + threadPool, + xContentRegistry(), + dispatcher, + clusterSettings, + new SharedGroupFactory(Settings.EMPTY) + ) + ) { transport.start(); final TransportAddress remoteAddress = randomFrom(transport.boundAddress().boundAddresses()); @@ -345,23 +401,32 @@ public void dispatchRequest(final RestRequest request, final RestChannel channel } @Override - public void dispatchBadRequest(final RestChannel channel, - final ThreadContext threadContext, - final Throwable cause) { - logger.error(new ParameterizedMessage("--> Unexpected bad request [{}]", - FakeRestRequest.requestToString(channel.request())), cause); + public void dispatchBadRequest(final RestChannel channel, final ThreadContext threadContext, final Throwable cause) { + logger.error( + new ParameterizedMessage("--> Unexpected bad request [{}]", FakeRestRequest.requestToString(channel.request())), + cause + ); throw new AssertionError(); } }; - final Settings settings = createBuilderWithPort() - .put(SETTING_CORS_ENABLED.getKey(), true) - .put(SETTING_CORS_ALLOW_ORIGIN.getKey(), "elastic.co").build(); + final Settings settings = createBuilderWithPort().put(SETTING_CORS_ENABLED.getKey(), true) + .put(SETTING_CORS_ALLOW_ORIGIN.getKey(), "elastic.co") + .build(); - try (Netty4HttpServerTransport transport = new Netty4HttpServerTransport(settings, networkService, bigArrays, threadPool, - xContentRegistry(), dispatcher, randomClusterSettings(), - new SharedGroupFactory(settings))) { + try ( + Netty4HttpServerTransport transport = new Netty4HttpServerTransport( + settings, + networkService, + bigArrays, + threadPool, + xContentRegistry(), + dispatcher, + randomClusterSettings(), + new SharedGroupFactory(settings) + ) + ) { transport.start(); final TransportAddress remoteAddress = randomFrom(transport.boundAddress().boundAddresses()); @@ -407,40 +472,51 @@ public void dispatchRequest(final RestRequest request, final RestChannel channel } @Override - public void dispatchBadRequest(final RestChannel channel, - final ThreadContext threadContext, - final Throwable cause) { - logger.error(new ParameterizedMessage("--> Unexpected bad request [{}]", - FakeRestRequest.requestToString(channel.request())), cause); + public void dispatchBadRequest(final RestChannel channel, final ThreadContext threadContext, final Throwable cause) { + logger.error( + new ParameterizedMessage("--> Unexpected bad request [{}]", FakeRestRequest.requestToString(channel.request())), + cause + ); throw new AssertionError("Should not have received a dispatched request"); } }; - Settings settings = createBuilderWithPort() - .put(HttpTransportSettings.SETTING_HTTP_READ_TIMEOUT.getKey(), new TimeValue(randomIntBetween(100, 300))) - .build(); + Settings settings = createBuilderWithPort().put( + HttpTransportSettings.SETTING_HTTP_READ_TIMEOUT.getKey(), + new TimeValue(randomIntBetween(100, 300)) + ).build(); NioEventLoopGroup group = new NioEventLoopGroup(); - try (Netty4HttpServerTransport transport = new Netty4HttpServerTransport(settings, networkService, bigArrays, threadPool, - xContentRegistry(), dispatcher, randomClusterSettings(), - new SharedGroupFactory(settings))) { + try ( + Netty4HttpServerTransport transport = new Netty4HttpServerTransport( + settings, + networkService, + bigArrays, + threadPool, + xContentRegistry(), + dispatcher, + randomClusterSettings(), + new SharedGroupFactory(settings) + ) + ) { transport.start(); final TransportAddress remoteAddress = randomFrom(transport.boundAddress().boundAddresses()); CountDownLatch channelClosedLatch = new CountDownLatch(1); - Bootstrap clientBootstrap = new Bootstrap() - .option(ChannelOption.ALLOCATOR, NettyAllocator.getAllocator()) + Bootstrap clientBootstrap = new Bootstrap().option(ChannelOption.ALLOCATOR, NettyAllocator.getAllocator()) .channel(NioSocketChannel.class) .handler(new ChannelInitializer() { - @Override - protected void initChannel(SocketChannel ch) { - ch.pipeline().addLast(new ChannelHandlerAdapter() {}); + @Override + protected void initChannel(SocketChannel ch) { + ch.pipeline().addLast(new ChannelHandlerAdapter() { + }); - } - }).group(group); + } + }) + .group(group); ChannelFuture connect = clientBootstrap.connect(remoteAddress.address()); connect.channel().closeFuture().addListener(future -> channelClosedLatch.countDown()); diff --git a/modules/transport-netty4/src/test/java/org/elasticsearch/transport/netty4/CopyBytesSocketChannelTests.java b/modules/transport-netty4/src/test/java/org/elasticsearch/transport/netty4/CopyBytesSocketChannelTests.java index acc50028cb48d..c2bcf33267527 100644 --- a/modules/transport-netty4/src/test/java/org/elasticsearch/transport/netty4/CopyBytesSocketChannelTests.java +++ b/modules/transport-netty4/src/test/java/org/elasticsearch/transport/netty4/CopyBytesSocketChannelTests.java @@ -19,10 +19,9 @@ import io.netty.channel.ChannelOption; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.channel.nio.NioEventLoopGroup; + import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.transport.netty4.CopyBytesServerSocketChannel; -import org.elasticsearch.transport.netty4.CopyBytesSocketChannel; import java.io.IOException; import java.net.InetAddress; diff --git a/modules/transport-netty4/src/test/java/org/elasticsearch/transport/netty4/Netty4SizeHeaderFrameDecoderTests.java b/modules/transport-netty4/src/test/java/org/elasticsearch/transport/netty4/Netty4SizeHeaderFrameDecoderTests.java index 9bad81f6caf35..2229e46e522a0 100644 --- a/modules/transport-netty4/src/test/java/org/elasticsearch/transport/netty4/Netty4SizeHeaderFrameDecoderTests.java +++ b/modules/transport-netty4/src/test/java/org/elasticsearch/transport/netty4/Netty4SizeHeaderFrameDecoderTests.java @@ -54,8 +54,16 @@ public void startThreadPool() { threadPool = new ThreadPool(settings); NetworkService networkService = new NetworkService(Collections.emptyList()); PageCacheRecycler recycler = new MockPageCacheRecycler(Settings.EMPTY); - nettyTransport = new Netty4Transport(settings, Version.CURRENT, threadPool, networkService, recycler, - new NamedWriteableRegistry(Collections.emptyList()), new NoneCircuitBreakerService(), new SharedGroupFactory(settings)); + nettyTransport = new Netty4Transport( + settings, + Version.CURRENT, + threadPool, + networkService, + recycler, + new NamedWriteableRegistry(Collections.emptyList()), + new NoneCircuitBreakerService(), + new SharedGroupFactory(settings) + ); nettyTransport.start(); TransportAddress[] boundAddresses = nettyTransport.boundAddress().boundAddresses(); diff --git a/modules/transport-netty4/src/test/java/org/elasticsearch/transport/netty4/Netty4UtilsTests.java b/modules/transport-netty4/src/test/java/org/elasticsearch/transport/netty4/Netty4UtilsTests.java index f828b4ef944bf..5edfda5cd1bac 100644 --- a/modules/transport-netty4/src/test/java/org/elasticsearch/transport/netty4/Netty4UtilsTests.java +++ b/modules/transport-netty4/src/test/java/org/elasticsearch/transport/netty4/Netty4UtilsTests.java @@ -11,6 +11,7 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.CompositeByteBuf; import io.netty.buffer.Unpooled; + import org.apache.lucene.util.BytesRef; import org.elasticsearch.common.breaker.CircuitBreaker; import org.elasticsearch.common.bytes.AbstractBytesReferenceTestCase; @@ -50,8 +51,10 @@ public void testToChannelBufferWithSliceAfter() throws IOException { int sliceLength = randomIntBetween(ref.length() - sliceOffset, ref.length() - sliceOffset); ByteBuf buffer = Netty4Utils.toByteBuf(ref); BytesReference bytesReference = Netty4Utils.toBytesReference(buffer); - assertArrayEquals(BytesReference.toBytes(ref.slice(sliceOffset, sliceLength)), - BytesReference.toBytes(bytesReference.slice(sliceOffset, sliceLength))); + assertArrayEquals( + BytesReference.toBytes(ref.slice(sliceOffset, sliceLength)), + BytesReference.toBytes(bytesReference.slice(sliceOffset, sliceLength)) + ); } public void testToChannelBuffer() throws IOException { @@ -77,8 +80,7 @@ private BytesReference getRandomizedBytesReference(int length) throws IOExceptio return new BytesArray(ref.toBytesRef()); } else if (randomBoolean()) { BytesRef bytesRef = ref.toBytesRef(); - return Netty4Utils.toBytesReference(Unpooled.wrappedBuffer(bytesRef.bytes, bytesRef.offset, - bytesRef.length)); + return Netty4Utils.toBytesReference(Unpooled.wrappedBuffer(bytesRef.bytes, bytesRef.offset, bytesRef.length)); } else { return ref; } diff --git a/modules/transport-netty4/src/test/java/org/elasticsearch/transport/netty4/NettyTransportMultiPortTests.java b/modules/transport-netty4/src/test/java/org/elasticsearch/transport/netty4/NettyTransportMultiPortTests.java index 9929294d8ebed..9648f19ac8c92 100644 --- a/modules/transport-netty4/src/test/java/org/elasticsearch/transport/netty4/NettyTransportMultiPortTests.java +++ b/modules/transport-netty4/src/test/java/org/elasticsearch/transport/netty4/NettyTransportMultiPortTests.java @@ -108,9 +108,16 @@ public void testThatDefaultProfilePortOverridesGeneralConfiguration() throws Exc private TcpTransport startTransport(Settings settings, ThreadPool threadPool) { PageCacheRecycler recycler = new MockPageCacheRecycler(Settings.EMPTY); - TcpTransport transport = new Netty4Transport(settings, Version.CURRENT, threadPool, new NetworkService(Collections.emptyList()), - recycler, new NamedWriteableRegistry(Collections.emptyList()), new NoneCircuitBreakerService(), - new SharedGroupFactory(settings)); + TcpTransport transport = new Netty4Transport( + settings, + Version.CURRENT, + threadPool, + new NetworkService(Collections.emptyList()), + recycler, + new NamedWriteableRegistry(Collections.emptyList()), + new NoneCircuitBreakerService(), + new SharedGroupFactory(settings) + ); transport.start(); assertThat(transport.lifecycleState(), is(Lifecycle.State.STARTED)); diff --git a/modules/transport-netty4/src/test/java/org/elasticsearch/transport/netty4/SharedGroupFactoryTests.java b/modules/transport-netty4/src/test/java/org/elasticsearch/transport/netty4/SharedGroupFactoryTests.java index 38d30a4690ecc..7cd34ad02d5a9 100644 --- a/modules/transport-netty4/src/test/java/org/elasticsearch/transport/netty4/SharedGroupFactoryTests.java +++ b/modules/transport-netty4/src/test/java/org/elasticsearch/transport/netty4/SharedGroupFactoryTests.java @@ -11,7 +11,6 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.http.netty4.Netty4HttpServerTransport; import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.transport.netty4.SharedGroupFactory; public final class SharedGroupFactoryTests extends ESTestCase { diff --git a/modules/transport-netty4/src/test/java/org/elasticsearch/transport/netty4/SimpleNetty4TransportTests.java b/modules/transport-netty4/src/test/java/org/elasticsearch/transport/netty4/SimpleNetty4TransportTests.java index baabccd66e744..23ab2f54543bd 100644 --- a/modules/transport-netty4/src/test/java/org/elasticsearch/transport/netty4/SimpleNetty4TransportTests.java +++ b/modules/transport-netty4/src/test/java/org/elasticsearch/transport/netty4/SimpleNetty4TransportTests.java @@ -10,7 +10,6 @@ import org.elasticsearch.Version; import org.elasticsearch.action.ActionListener; -import org.elasticsearch.jdk.JavaVersion; import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.network.NetworkService; @@ -21,6 +20,7 @@ import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.core.internal.net.NetUtils; import org.elasticsearch.indices.breaker.NoneCircuitBreakerService; +import org.elasticsearch.jdk.JavaVersion; import org.elasticsearch.test.transport.MockTransportService; import org.elasticsearch.test.transport.StubbableTransport; import org.elasticsearch.transport.AbstractSimpleTransportTestCase; @@ -49,13 +49,24 @@ public class SimpleNetty4TransportTests extends AbstractSimpleTransportTestCase @Override protected Transport build(Settings settings, final Version version, ClusterSettings clusterSettings, boolean doHandshake) { NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(Collections.emptyList()); - return new Netty4Transport(settings, version, threadPool, new NetworkService(Collections.emptyList()), - PageCacheRecycler.NON_RECYCLING_INSTANCE, namedWriteableRegistry, new NoneCircuitBreakerService(), - new SharedGroupFactory(settings)) { + return new Netty4Transport( + settings, + version, + threadPool, + new NetworkService(Collections.emptyList()), + PageCacheRecycler.NON_RECYCLING_INSTANCE, + namedWriteableRegistry, + new NoneCircuitBreakerService(), + new SharedGroupFactory(settings) + ) { @Override - public void executeHandshake(DiscoveryNode node, TcpChannel channel, ConnectionProfile profile, - ActionListener listener) { + public void executeHandshake( + DiscoveryNode node, + TcpChannel channel, + ConnectionProfile profile, + ActionListener listener + ) { if (doHandshake) { super.executeHandshake(node, channel, profile, listener); } else { @@ -67,8 +78,16 @@ public void executeHandshake(DiscoveryNode node, TcpChannel channel, ConnectionP public void testConnectException() throws UnknownHostException { try { - connectToNode(serviceA, new DiscoveryNode("C", new TransportAddress(InetAddress.getByName("localhost"), 9876), - emptyMap(), emptySet(),Version.CURRENT)); + connectToNode( + serviceA, + new DiscoveryNode( + "C", + new TransportAddress(InetAddress.getByName("localhost"), 9876), + emptyMap(), + emptySet(), + Version.CURRENT + ) + ); fail("Expected ConnectTransportException"); } catch (ConnectTransportException e) { assertThat(e.getMessage(), containsString("connect_exception")); @@ -77,11 +96,14 @@ public void testConnectException() throws UnknownHostException { } public void testDefaultKeepAliveSettings() throws IOException { - assumeTrue("setting default keepalive options not supported on this platform", - (IOUtils.LINUX || IOUtils.MAC_OS_X) && - JavaVersion.current().compareTo(JavaVersion.parse("11")) >= 0); - try (MockTransportService serviceC = buildService("TS_C", Version.CURRENT, Settings.EMPTY); - MockTransportService serviceD = buildService("TS_D", Version.CURRENT, Settings.EMPTY)) { + assumeTrue( + "setting default keepalive options not supported on this platform", + (IOUtils.LINUX || IOUtils.MAC_OS_X) && JavaVersion.current().compareTo(JavaVersion.parse("11")) >= 0 + ); + try ( + MockTransportService serviceC = buildService("TS_C", Version.CURRENT, Settings.EMPTY); + MockTransportService serviceD = buildService("TS_D", Version.CURRENT, Settings.EMPTY) + ) { try (Transport.Connection connection = openConnection(serviceC, serviceD.getLocalDiscoNode(), TestProfiles.LIGHT_PROFILE)) { assertThat(connection, instanceOf(StubbableTransport.WrappedConnection.class)); diff --git a/plugins/analysis-icu/src/internalClusterTest/java/org/elasticsearch/index/mapper/ICUCollationKeywordFieldMapperIT.java b/plugins/analysis-icu/src/internalClusterTest/java/org/elasticsearch/index/mapper/ICUCollationKeywordFieldMapperIT.java index 6c61c6cd20e20..e99cd86691f2f 100644 --- a/plugins/analysis-icu/src/internalClusterTest/java/org/elasticsearch/index/mapper/ICUCollationKeywordFieldMapperIT.java +++ b/plugins/analysis-icu/src/internalClusterTest/java/org/elasticsearch/index/mapper/ICUCollationKeywordFieldMapperIT.java @@ -13,8 +13,6 @@ import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.plugin.analysis.icu.AnalysisICUPlugin; import org.elasticsearch.plugins.Plugin; @@ -23,15 +21,17 @@ import org.elasticsearch.search.sort.SortMode; import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.test.ESIntegTestCase; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; import java.util.Collection; import java.util.Collections; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertOrderedSearchHits; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; public class ICUCollationKeywordFieldMapperIT extends ESIntegTestCase { @@ -49,10 +49,10 @@ protected Collection> nodePlugins() { public void testBasicUsage() throws Exception { String index = "foo"; - String[] equivalent = {"I WİLL USE TURKİSH CASING", "ı will use turkish casıng"}; + String[] equivalent = { "I WİLL USE TURKİSH CASING", "ı will use turkish casıng" }; - XContentBuilder builder = jsonBuilder() - .startObject().startObject("properties") + XContentBuilder builder = jsonBuilder().startObject() + .startObject("properties") .startObject("id") .field("type", "keyword") .endObject() @@ -61,26 +61,25 @@ public void testBasicUsage() throws Exception { .field("language", "tr") .field("strength", "primary") .endObject() - .endObject().endObject(); + .endObject() + .endObject(); assertAcked(client().admin().indices().prepareCreate(index).setMapping(builder)); // both values should collate to same value - indexRandom(true, - client().prepareIndex(index).setId("1") - .setSource("{\"id\":\"1\",\"collate\":\"" + equivalent[0] + "\"}", XContentType.JSON), - client().prepareIndex(index).setId("2") - .setSource("{\"id\":\"2\",\"collate\":\"" + equivalent[1] + "\"}", XContentType.JSON) + indexRandom( + true, + client().prepareIndex(index).setId("1").setSource("{\"id\":\"1\",\"collate\":\"" + equivalent[0] + "\"}", XContentType.JSON), + client().prepareIndex(index).setId("2").setSource("{\"id\":\"2\",\"collate\":\"" + equivalent[1] + "\"}", XContentType.JSON) ); // searching for either of the terms should return both results since they collate to the same value - SearchRequest request = new SearchRequest() - .indices(index) - .source(new SearchSourceBuilder() - .fetchSource(false) - .query(QueryBuilders.termQuery("collate", randomBoolean() ? equivalent[0] : equivalent[1])) - .sort("collate") - .sort("id", SortOrder.DESC) // secondary sort should kick in because both will collate to same value + SearchRequest request = new SearchRequest().indices(index) + .source( + new SearchSourceBuilder().fetchSource(false) + .query(QueryBuilders.termQuery("collate", randomBoolean() ? equivalent[0] : equivalent[1])) + .sort("collate") + .sort("id", SortOrder.DESC) // secondary sort should kick in because both will collate to same value ); SearchResponse response = client().search(request).actionGet(); @@ -92,10 +91,10 @@ public void testBasicUsage() throws Exception { public void testMultipleValues() throws Exception { String index = "foo"; - String[] equivalent = {"a", "C", "a", "B"}; + String[] equivalent = { "a", "C", "a", "B" }; - XContentBuilder builder = jsonBuilder() - .startObject().startObject("properties") + XContentBuilder builder = jsonBuilder().startObject() + .startObject("properties") .startObject("id") .field("type", "keyword") .endObject() @@ -103,27 +102,28 @@ public void testMultipleValues() throws Exception { .field("type", "icu_collation_keyword") .field("language", "en") .endObject() - .endObject().endObject(); + .endObject() + .endObject(); assertAcked(client().admin().indices().prepareCreate(index).setMapping(builder)); // everything should be indexed fine, no exceptions - indexRandom(true, - client().prepareIndex(index).setId("1") + indexRandom( + true, + client().prepareIndex(index) + .setId("1") .setSource("{\"id\":\"1\", \"collate\":[\"" + equivalent[0] + "\", \"" + equivalent[1] + "\"]}", XContentType.JSON), - client().prepareIndex(index).setId("2") - .setSource("{\"id\":\"2\",\"collate\":\"" + equivalent[2] + "\"}", XContentType.JSON) + client().prepareIndex(index).setId("2").setSource("{\"id\":\"2\",\"collate\":\"" + equivalent[2] + "\"}", XContentType.JSON) ); // using sort mode = max, values B and C will be used for the sort - SearchRequest request = new SearchRequest() - .indices(index) - .source(new SearchSourceBuilder() - .fetchSource(false) - .query(QueryBuilders.termQuery("collate", "a")) - // if mode max we use c and b as sort values, if max we use "a" for both - .sort(SortBuilders.fieldSort("collate").sortMode(SortMode.MAX).order(SortOrder.DESC)) - .sort("id", SortOrder.DESC) // will be ignored + SearchRequest request = new SearchRequest().indices(index) + .source( + new SearchSourceBuilder().fetchSource(false) + .query(QueryBuilders.termQuery("collate", "a")) + // if mode max we use c and b as sort values, if max we use "a" for both + .sort(SortBuilders.fieldSort("collate").sortMode(SortMode.MAX).order(SortOrder.DESC)) + .sort("id", SortOrder.DESC) // will be ignored ); SearchResponse response = client().search(request).actionGet(); @@ -132,14 +132,13 @@ public void testMultipleValues() throws Exception { assertOrderedSearchHits(response, "1", "2"); // same thing, using different sort mode that will use a for both docs - request = new SearchRequest() - .indices(index) - .source(new SearchSourceBuilder() - .fetchSource(false) - .query(QueryBuilders.termQuery("collate", "a")) - // if mode max we use c and b as sort values, if max we use "a" for both - .sort(SortBuilders.fieldSort("collate").sortMode(SortMode.MIN).order(SortOrder.DESC)) - .sort("id", SortOrder.DESC) // will NOT be ignored and will determine order + request = new SearchRequest().indices(index) + .source( + new SearchSourceBuilder().fetchSource(false) + .query(QueryBuilders.termQuery("collate", "a")) + // if mode max we use c and b as sort values, if max we use "a" for both + .sort(SortBuilders.fieldSort("collate").sortMode(SortMode.MIN).order(SortOrder.DESC)) + .sort("id", SortOrder.DESC) // will NOT be ignored and will determine order ); response = client().search(request).actionGet(); @@ -154,10 +153,10 @@ public void testMultipleValues() throws Exception { public void testNormalization() throws Exception { String index = "foo"; - String[] equivalent = {"I W\u0049\u0307LL USE TURKİSH CASING", "ı will use turkish casıng"}; + String[] equivalent = { "I W\u0049\u0307LL USE TURKİSH CASING", "ı will use turkish casıng" }; - XContentBuilder builder = jsonBuilder() - .startObject().startObject("properties") + XContentBuilder builder = jsonBuilder().startObject() + .startObject("properties") .startObject("id") .field("type", "keyword") .endObject() @@ -167,25 +166,24 @@ public void testNormalization() throws Exception { .field("strength", "primary") .field("decomposition", "canonical") .endObject() - .endObject().endObject(); + .endObject() + .endObject(); assertAcked(client().admin().indices().prepareCreate(index).setMapping(builder)); - indexRandom(true, - client().prepareIndex(index).setId("1") - .setSource("{\"id\":\"1\",\"collate\":\"" + equivalent[0] + "\"}", XContentType.JSON), - client().prepareIndex(index).setId("2") - .setSource("{\"id\":\"2\",\"collate\":\"" + equivalent[1] + "\"}", XContentType.JSON) + indexRandom( + true, + client().prepareIndex(index).setId("1").setSource("{\"id\":\"1\",\"collate\":\"" + equivalent[0] + "\"}", XContentType.JSON), + client().prepareIndex(index).setId("2").setSource("{\"id\":\"2\",\"collate\":\"" + equivalent[1] + "\"}", XContentType.JSON) ); // searching for either of the terms should return both results since they collate to the same value - SearchRequest request = new SearchRequest() - .indices(index) - .source(new SearchSourceBuilder() - .fetchSource(false) - .query(QueryBuilders.termQuery("collate", randomBoolean() ? equivalent[0] : equivalent[1])) - .sort("collate") - .sort("id", SortOrder.DESC) // secondary sort should kick in because both will collate to same value + SearchRequest request = new SearchRequest().indices(index) + .source( + new SearchSourceBuilder().fetchSource(false) + .query(QueryBuilders.termQuery("collate", randomBoolean() ? equivalent[0] : equivalent[1])) + .sort("collate") + .sort("id", SortOrder.DESC) // secondary sort should kick in because both will collate to same value ); SearchResponse response = client().search(request).actionGet(); @@ -200,10 +198,10 @@ public void testNormalization() throws Exception { public void testSecondaryStrength() throws Exception { String index = "foo"; - String[] equivalent = {"TESTING", "testing"}; + String[] equivalent = { "TESTING", "testing" }; - XContentBuilder builder = jsonBuilder() - .startObject().startObject("properties") + XContentBuilder builder = jsonBuilder().startObject() + .startObject("properties") .startObject("id") .field("type", "keyword") .endObject() @@ -213,24 +211,23 @@ public void testSecondaryStrength() throws Exception { .field("strength", "secondary") .field("decomposition", "no") .endObject() - .endObject().endObject(); + .endObject() + .endObject(); assertAcked(client().admin().indices().prepareCreate(index).setMapping(builder)); - indexRandom(true, - client().prepareIndex(index).setId("1") - .setSource("{\"id\":\"1\",\"collate\":\"" + equivalent[0] + "\"}", XContentType.JSON), - client().prepareIndex(index).setId("2") - .setSource("{\"id\":\"2\",\"collate\":\"" + equivalent[1] + "\"}", XContentType.JSON) + indexRandom( + true, + client().prepareIndex(index).setId("1").setSource("{\"id\":\"1\",\"collate\":\"" + equivalent[0] + "\"}", XContentType.JSON), + client().prepareIndex(index).setId("2").setSource("{\"id\":\"2\",\"collate\":\"" + equivalent[1] + "\"}", XContentType.JSON) ); - SearchRequest request = new SearchRequest() - .indices(index) - .source(new SearchSourceBuilder() - .fetchSource(false) - .query(QueryBuilders.termQuery("collate", randomBoolean() ? equivalent[0] : equivalent[1])) - .sort("collate") - .sort("id", SortOrder.DESC) // secondary sort should kick in because both will collate to same value + SearchRequest request = new SearchRequest().indices(index) + .source( + new SearchSourceBuilder().fetchSource(false) + .query(QueryBuilders.termQuery("collate", randomBoolean() ? equivalent[0] : equivalent[1])) + .sort("collate") + .sort("id", SortOrder.DESC) // secondary sort should kick in because both will collate to same value ); SearchResponse response = client().search(request).actionGet(); @@ -246,10 +243,10 @@ public void testSecondaryStrength() throws Exception { public void testIgnorePunctuation() throws Exception { String index = "foo"; - String[] equivalent = {"foo-bar", "foo bar"}; + String[] equivalent = { "foo-bar", "foo bar" }; - XContentBuilder builder = jsonBuilder() - .startObject().startObject("properties") + XContentBuilder builder = jsonBuilder().startObject() + .startObject("properties") .startObject("id") .field("type", "keyword") .endObject() @@ -259,22 +256,23 @@ public void testIgnorePunctuation() throws Exception { .field("strength", "primary") .field("alternate", "shifted") .endObject() - .endObject().endObject(); + .endObject() + .endObject(); assertAcked(client().admin().indices().prepareCreate(index).setMapping(builder)); - indexRandom(true, + indexRandom( + true, client().prepareIndex(index).setId("1").setSource("{\"id\":\"1\",\"collate\":\"" + equivalent[0] + "\"}", XContentType.JSON), client().prepareIndex(index).setId("2").setSource("{\"id\":\"2\",\"collate\":\"" + equivalent[1] + "\"}", XContentType.JSON) ); - SearchRequest request = new SearchRequest() - .indices(index) - .source(new SearchSourceBuilder() - .fetchSource(false) - .query(QueryBuilders.termQuery("collate", randomBoolean() ? equivalent[0] : equivalent[1])) - .sort("collate") - .sort("id", SortOrder.DESC) // secondary sort should kick in because both will collate to same value + SearchRequest request = new SearchRequest().indices(index) + .source( + new SearchSourceBuilder().fetchSource(false) + .query(QueryBuilders.termQuery("collate", randomBoolean() ? equivalent[0] : equivalent[1])) + .sort("collate") + .sort("id", SortOrder.DESC) // secondary sort should kick in because both will collate to same value ); SearchResponse response = client().search(request).actionGet(); @@ -290,8 +288,8 @@ public void testIgnorePunctuation() throws Exception { public void testIgnoreWhitespace() throws Exception { String index = "foo"; - XContentBuilder builder = jsonBuilder() - .startObject().startObject("properties") + XContentBuilder builder = jsonBuilder().startObject() + .startObject("properties") .startObject("id") .field("type", "keyword") .endObject() @@ -303,23 +301,24 @@ public void testIgnoreWhitespace() throws Exception { .field("variable_top", " ") .field("index", false) .endObject() - .endObject().endObject(); + .endObject() + .endObject(); assertAcked(client().admin().indices().prepareCreate(index).setMapping(builder)); - indexRandom(true, + indexRandom( + true, client().prepareIndex(index).setId("1").setSource("{\"id\":\"1\",\"collate\":\"foo bar\"}", XContentType.JSON), client().prepareIndex(index).setId("2").setSource("{\"id\":\"2\",\"collate\":\"foobar\"}", XContentType.JSON), client().prepareIndex(index).setId("3").setSource("{\"id\":\"3\",\"collate\":\"foo-bar\"}", XContentType.JSON) ); - SearchRequest request = new SearchRequest() - .indices(index) - .source(new SearchSourceBuilder() - .fetchSource(false) - .sort("collate", SortOrder.ASC) - // secondary sort should kick in on docs 1 and 3 because same value collate value - .sort("id", SortOrder.ASC) + SearchRequest request = new SearchRequest().indices(index) + .source( + new SearchSourceBuilder().fetchSource(false) + .sort("collate", SortOrder.ASC) + // secondary sort should kick in on docs 1 and 3 because same value collate value + .sort("id", SortOrder.ASC) ); SearchResponse response = client().search(request).actionGet(); @@ -335,29 +334,27 @@ public void testIgnoreWhitespace() throws Exception { public void testNumerics() throws Exception { String index = "foo"; - XContentBuilder builder = jsonBuilder() - .startObject().startObject("properties") + XContentBuilder builder = jsonBuilder().startObject() + .startObject("properties") .startObject("collate") .field("type", "icu_collation_keyword") .field("language", "en") .field("numeric", true) .field("index", false) .endObject() - .endObject().endObject(); + .endObject() + .endObject(); assertAcked(client().admin().indices().prepareCreate(index).setMapping(builder)); - indexRandom(true, + indexRandom( + true, client().prepareIndex(index).setId("1").setSource("{\"collate\":\"foobar-10\"}", XContentType.JSON), client().prepareIndex(index).setId("2").setSource("{\"collate\":\"foobar-9\"}", XContentType.JSON) ); - SearchRequest request = new SearchRequest() - .indices(index) - .source(new SearchSourceBuilder() - .fetchSource(false) - .sort("collate", SortOrder.ASC) - ); + SearchRequest request = new SearchRequest().indices(index) + .source(new SearchSourceBuilder().fetchSource(false).sort("collate", SortOrder.ASC)); SearchResponse response = client().search(request).actionGet(); assertNoFailures(response); @@ -372,8 +369,8 @@ public void testNumerics() throws Exception { public void testIgnoreAccentsButNotCase() throws Exception { String index = "foo"; - XContentBuilder builder = jsonBuilder() - .startObject().startObject("properties") + XContentBuilder builder = jsonBuilder().startObject() + .startObject("properties") .startObject("id") .field("type", "keyword") .endObject() @@ -384,24 +381,21 @@ public void testIgnoreAccentsButNotCase() throws Exception { .field("case_level", true) .field("index", false) .endObject() - .endObject().endObject(); + .endObject() + .endObject(); assertAcked(client().admin().indices().prepareCreate(index).setMapping(builder)); - indexRandom(true, + indexRandom( + true, client().prepareIndex(index).setId("1").setSource("{\"id\":\"1\",\"collate\":\"résumé\"}", XContentType.JSON), client().prepareIndex(index).setId("2").setSource("{\"id\":\"2\",\"collate\":\"Resume\"}", XContentType.JSON), client().prepareIndex(index).setId("3").setSource("{\"id\":\"3\",\"collate\":\"resume\"}", XContentType.JSON), client().prepareIndex(index).setId("4").setSource("{\"id\":\"4\",\"collate\":\"Résumé\"}", XContentType.JSON) ); - SearchRequest request = new SearchRequest() - .indices(index) - .source(new SearchSourceBuilder() - .fetchSource(false) - .sort("collate", SortOrder.ASC) - .sort("id", SortOrder.DESC) - ); + SearchRequest request = new SearchRequest().indices(index) + .source(new SearchSourceBuilder().fetchSource(false).sort("collate", SortOrder.ASC).sort("id", SortOrder.DESC)); SearchResponse response = client().search(request).actionGet(); assertNoFailures(response); @@ -416,8 +410,8 @@ public void testIgnoreAccentsButNotCase() throws Exception { public void testUpperCaseFirst() throws Exception { String index = "foo"; - XContentBuilder builder = jsonBuilder() - .startObject().startObject("properties") + XContentBuilder builder = jsonBuilder().startObject() + .startObject("properties") .startObject("collate") .field("type", "icu_collation_keyword") .field("language", "en") @@ -425,21 +419,19 @@ public void testUpperCaseFirst() throws Exception { .field("case_first", "upper") .field("index", false) .endObject() - .endObject().endObject(); + .endObject() + .endObject(); assertAcked(client().admin().indices().prepareCreate(index).setMapping(builder)); - indexRandom(true, + indexRandom( + true, client().prepareIndex(index).setId("1").setSource("{\"collate\":\"resume\"}", XContentType.JSON), client().prepareIndex(index).setId("2").setSource("{\"collate\":\"Resume\"}", XContentType.JSON) ); - SearchRequest request = new SearchRequest() - .indices(index) - .source(new SearchSourceBuilder() - .fetchSource(false) - .sort("collate", SortOrder.ASC) - ); + SearchRequest request = new SearchRequest().indices(index) + .source(new SearchSourceBuilder().fetchSource(false).sort("collate", SortOrder.ASC)); SearchResponse response = client().search(request).actionGet(); assertNoFailures(response); @@ -458,18 +450,15 @@ public void testCustomRules() throws Exception { String index = "foo"; RuleBasedCollator baseCollator = (RuleBasedCollator) Collator.getInstance(new ULocale("de_DE")); - String DIN5007_2_tailorings = - "& ae , a\u0308 & AE , A\u0308" + - "& oe , o\u0308 & OE , O\u0308" + - "& ue , u\u0308 & UE , u\u0308"; + String DIN5007_2_tailorings = "& ae , a\u0308 & AE , A\u0308" + "& oe , o\u0308 & OE , O\u0308" + "& ue , u\u0308 & UE , u\u0308"; RuleBasedCollator tailoredCollator = new RuleBasedCollator(baseCollator.getRules() + DIN5007_2_tailorings); String tailoredRules = tailoredCollator.getRules(); - String[] equivalent = {"Töne", "Toene"}; + String[] equivalent = { "Töne", "Toene" }; - XContentBuilder builder = jsonBuilder() - .startObject().startObject("properties") + XContentBuilder builder = jsonBuilder().startObject() + .startObject("properties") .startObject("id") .field("type", "keyword") .endObject() @@ -478,22 +467,23 @@ public void testCustomRules() throws Exception { .field("rules", tailoredRules) .field("strength", "primary") .endObject() - .endObject().endObject(); + .endObject() + .endObject(); assertAcked(client().admin().indices().prepareCreate(index).setMapping(builder)); - indexRandom(true, + indexRandom( + true, client().prepareIndex(index).setId("1").setSource("{\"id\":\"1\",\"collate\":\"" + equivalent[0] + "\"}", XContentType.JSON), client().prepareIndex(index).setId("2").setSource("{\"id\":\"2\",\"collate\":\"" + equivalent[1] + "\"}", XContentType.JSON) ); - SearchRequest request = new SearchRequest() - .indices(index) - .source(new SearchSourceBuilder() - .fetchSource(false) - .query(QueryBuilders.termQuery("collate", randomBoolean() ? equivalent[0] : equivalent[1])) - .sort("collate", SortOrder.ASC) - .sort("id", SortOrder.DESC) // secondary sort should kick in because both will collate to same value + SearchRequest request = new SearchRequest().indices(index) + .source( + new SearchSourceBuilder().fetchSource(false) + .query(QueryBuilders.termQuery("collate", randomBoolean() ? equivalent[0] : equivalent[1])) + .sort("collate", SortOrder.ASC) + .sort("id", SortOrder.DESC) // secondary sort should kick in because both will collate to same value ); SearchResponse response = client().search(request).actionGet(); diff --git a/plugins/analysis-icu/src/main/java/org/elasticsearch/plugin/analysis/icu/ICUCollationKeyFilter.java b/plugins/analysis-icu/src/main/java/org/elasticsearch/plugin/analysis/icu/ICUCollationKeyFilter.java index 1adc26e599a31..40da86f611bb0 100644 --- a/plugins/analysis-icu/src/main/java/org/elasticsearch/plugin/analysis/icu/ICUCollationKeyFilter.java +++ b/plugins/analysis-icu/src/main/java/org/elasticsearch/plugin/analysis/icu/ICUCollationKeyFilter.java @@ -19,10 +19,11 @@ import com.ibm.icu.text.Collator; import com.ibm.icu.text.RawCollationKey; + import org.apache.lucene.analysis.TokenFilter; import org.apache.lucene.analysis.TokenStream; -import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; import org.apache.lucene.analysis.icu.ICUCollationDocValuesField; +import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; import java.io.IOException; @@ -68,42 +69,40 @@ */ @Deprecated public final class ICUCollationKeyFilter extends TokenFilter { - private Collator collator = null; - private RawCollationKey reusableKey = new RawCollationKey(); - private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class); + private Collator collator = null; + private RawCollationKey reusableKey = new RawCollationKey(); + private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class); - /** - * - * @param input Source token stream - * @param collator CollationKey generator - */ - public ICUCollationKeyFilter(TokenStream input, Collator collator) { - super(input); - // clone the collator: see http://userguide.icu-project.org/collation/architecture - try { - this.collator = (Collator) collator.clone(); - } catch (CloneNotSupportedException e) { - throw new RuntimeException(e); + /** + * + * @param input Source token stream + * @param collator CollationKey generator + */ + public ICUCollationKeyFilter(TokenStream input, Collator collator) { + super(input); + // clone the collator: see http://userguide.icu-project.org/collation/architecture + try { + this.collator = (Collator) collator.clone(); + } catch (CloneNotSupportedException e) { + throw new RuntimeException(e); + } } - } - @Override - public boolean incrementToken() throws IOException { - if (input.incrementToken()) { - char[] termBuffer = termAtt.buffer(); - String termText = new String(termBuffer, 0, termAtt.length()); - collator.getRawCollationKey(termText, reusableKey); - int encodedLength = IndexableBinaryStringTools.getEncodedLength( - reusableKey.bytes, 0, reusableKey.size); - if (encodedLength > termBuffer.length) { - termAtt.resizeBuffer(encodedLength); - } - termAtt.setLength(encodedLength); - IndexableBinaryStringTools.encode(reusableKey.bytes, 0, reusableKey.size, - termAtt.buffer(), 0, encodedLength); - return true; - } else { - return false; + @Override + public boolean incrementToken() throws IOException { + if (input.incrementToken()) { + char[] termBuffer = termAtt.buffer(); + String termText = new String(termBuffer, 0, termAtt.length()); + collator.getRawCollationKey(termText, reusableKey); + int encodedLength = IndexableBinaryStringTools.getEncodedLength(reusableKey.bytes, 0, reusableKey.size); + if (encodedLength > termBuffer.length) { + termAtt.resizeBuffer(encodedLength); + } + termAtt.setLength(encodedLength); + IndexableBinaryStringTools.encode(reusableKey.bytes, 0, reusableKey.size, termAtt.buffer(), 0, encodedLength); + return true; + } else { + return false; + } } - } } diff --git a/plugins/analysis-icu/src/main/java/org/elasticsearch/plugin/analysis/icu/ICUCollationKeywordFieldMapper.java b/plugins/analysis-icu/src/main/java/org/elasticsearch/plugin/analysis/icu/ICUCollationKeywordFieldMapper.java index 833b95ef360db..ca8b694091e22 100644 --- a/plugins/analysis-icu/src/main/java/org/elasticsearch/plugin/analysis/icu/ICUCollationKeywordFieldMapper.java +++ b/plugins/analysis-icu/src/main/java/org/elasticsearch/plugin/analysis/icu/ICUCollationKeywordFieldMapper.java @@ -23,8 +23,9 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.lucene.Lucene; import org.elasticsearch.common.unit.Fuzziness; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.core.Nullable; +import org.elasticsearch.index.fielddata.IndexFieldData; +import org.elasticsearch.index.fielddata.plain.SortedSetOrdinalsIndexFieldData; import org.elasticsearch.index.mapper.DocumentParserContext; import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.mapper.MappedFieldType; @@ -34,12 +35,11 @@ import org.elasticsearch.index.mapper.TextParams; import org.elasticsearch.index.mapper.TextSearchInfo; import org.elasticsearch.index.mapper.ValueFetcher; -import org.elasticsearch.index.fielddata.IndexFieldData; -import org.elasticsearch.index.fielddata.plain.SortedSetOrdinalsIndexFieldData; import org.elasticsearch.index.query.SearchExecutionContext; import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.support.CoreValuesSourceType; import org.elasticsearch.search.lookup.SearchLookup; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.time.ZoneId; @@ -57,8 +57,16 @@ public static final class CollationFieldType extends StringFieldType { private final String nullValue; private final int ignoreAbove; - public CollationFieldType(String name, boolean isSearchable, boolean isStored, boolean hasDocValues, - Collator collator, String nullValue, int ignoreAbove, Map meta) { + public CollationFieldType( + String name, + boolean isSearchable, + boolean isStored, + boolean hasDocValues, + Collator collator, + String nullValue, + int ignoreAbove, + Map meta + ) { super(name, isSearchable, isStored, hasDocValues, TextSearchInfo.SIMPLE_MATCH_ONLY, meta); this.collator = collator; this.nullValue = nullValue; @@ -120,28 +128,46 @@ protected BytesRef indexedValueForSearch(Object value) { } @Override - public Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int maxExpansions, - boolean transpositions, SearchExecutionContext context) { + public Query fuzzyQuery( + Object value, + Fuzziness fuzziness, + int prefixLength, + int maxExpansions, + boolean transpositions, + SearchExecutionContext context + ) { throw new UnsupportedOperationException("[fuzzy] queries are not supported on [" + CONTENT_TYPE + "] fields."); } @Override - public Query prefixQuery(String value, MultiTermQuery.RewriteMethod method, - boolean caseInsensitive, SearchExecutionContext context) { + public Query prefixQuery( + String value, + MultiTermQuery.RewriteMethod method, + boolean caseInsensitive, + SearchExecutionContext context + ) { throw new UnsupportedOperationException("[prefix] queries are not supported on [" + CONTENT_TYPE + "] fields."); } @Override - public Query wildcardQuery(String value, - @Nullable MultiTermQuery.RewriteMethod method, - boolean caseInsensitive, - SearchExecutionContext context) { + public Query wildcardQuery( + String value, + @Nullable MultiTermQuery.RewriteMethod method, + boolean caseInsensitive, + SearchExecutionContext context + ) { throw new UnsupportedOperationException("[wildcard] queries are not supported on [" + CONTENT_TYPE + "] fields."); } @Override - public Query regexpQuery(String value, int syntaxFlags, int matchFlags, int maxDeterminizedStates, - MultiTermQuery.RewriteMethod method, SearchExecutionContext context) { + public Query regexpQuery( + String value, + int syntaxFlags, + int matchFlags, + int maxDeterminizedStates, + MultiTermQuery.RewriteMethod method, + SearchExecutionContext context + ) { throw new UnsupportedOperationException("[regexp] queries are not supported on [" + CONTENT_TYPE + "] fields."); } @@ -152,8 +178,7 @@ public String getWriteableName() { } @Override - public void writeTo(StreamOutput out) { - } + public void writeTo(StreamOutput out) {} @Override public String format(BytesRef value) { @@ -189,45 +214,44 @@ public static class Builder extends FieldMapper.Builder { final Parameter hasDocValues = Parameter.docValuesParam(m -> toType(m).hasDocValues, true); final Parameter stored = Parameter.storeParam(m -> toType(m).fieldType.stored(), false); - final Parameter indexOptions - = Parameter.restrictedStringParam("index_options", false, m -> toType(m).indexOptions, "docs", "freqs"); + final Parameter indexOptions = Parameter.restrictedStringParam( + "index_options", + false, + m -> toType(m).indexOptions, + "docs", + "freqs" + ); final Parameter hasNorms = TextParams.norms(false, m -> toType(m).fieldType.omitNorms() == false); final Parameter> meta = Parameter.metaParam(); - final Parameter rules - = Parameter.stringParam("rules", false, m -> toType(m).params.rules, null).acceptsNull(); - final Parameter language - = Parameter.stringParam("language", false, m -> toType(m).params.language, null).acceptsNull(); - final Parameter country - = Parameter.stringParam("country", false, m -> toType(m).params.country, null).acceptsNull(); - final Parameter variant - = Parameter.stringParam("variant", false, m -> toType(m).params.variant, null).acceptsNull(); - final Parameter strength - = Parameter.stringParam("strength", false, m -> toType(m).params.strength, null).acceptsNull(); - final Parameter decomposition - = Parameter.stringParam("decomposition", false, m -> toType(m).params.decomposition, null) + final Parameter rules = Parameter.stringParam("rules", false, m -> toType(m).params.rules, null).acceptsNull(); + final Parameter language = Parameter.stringParam("language", false, m -> toType(m).params.language, null).acceptsNull(); + final Parameter country = Parameter.stringParam("country", false, m -> toType(m).params.country, null).acceptsNull(); + final Parameter variant = Parameter.stringParam("variant", false, m -> toType(m).params.variant, null).acceptsNull(); + final Parameter strength = Parameter.stringParam("strength", false, m -> toType(m).params.strength, null).acceptsNull(); + final Parameter decomposition = Parameter.stringParam("decomposition", false, m -> toType(m).params.decomposition, null) .acceptsNull(); - final Parameter alternate - = Parameter.stringParam("alternate", false, m -> toType(m).params.alternate, null).acceptsNull(); + final Parameter alternate = Parameter.stringParam("alternate", false, m -> toType(m).params.alternate, null).acceptsNull(); final Parameter caseLevel = Parameter.boolParam("case_level", false, m -> toType(m).params.caseLevel, false); - final Parameter caseFirst - = Parameter.stringParam("case_first", false, m -> toType(m).params.caseFirst, null).acceptsNull(); + final Parameter caseFirst = Parameter.stringParam("case_first", false, m -> toType(m).params.caseFirst, null).acceptsNull(); final Parameter numeric = Parameter.boolParam("numeric", false, m -> toType(m).params.numeric, false); - final Parameter variableTop - = Parameter.stringParam("variable_top", false, m -> toType(m).params.variableTop, null).acceptsNull(); - final Parameter hiraganaQuaternaryMode - = Parameter.boolParam("hiragana_quaternary_mode", false, m -> toType(m).params.hiraganaQuaternaryMode, false).acceptsNull(); - - final Parameter ignoreAbove - = Parameter.intParam("ignore_above", true, m -> toType(m).ignoreAbove, Integer.MAX_VALUE) + final Parameter variableTop = Parameter.stringParam("variable_top", false, m -> toType(m).params.variableTop, null) + .acceptsNull(); + final Parameter hiraganaQuaternaryMode = Parameter.boolParam( + "hiragana_quaternary_mode", + false, + m -> toType(m).params.hiraganaQuaternaryMode, + false + ).acceptsNull(); + + final Parameter ignoreAbove = Parameter.intParam("ignore_above", true, m -> toType(m).ignoreAbove, Integer.MAX_VALUE) .addValidator(v -> { if (v < 0) { throw new IllegalArgumentException("[ignore_above] must be positive, got [" + v + "]"); } }); - final Parameter nullValue - = Parameter.stringParam("null_value", false, m -> toType(m).nullValue, null).acceptsNull(); + final Parameter nullValue = Parameter.stringParam("null_value", false, m -> toType(m).nullValue, null).acceptsNull(); public Builder(String name) { super(name); @@ -245,10 +269,28 @@ Builder ignoreAbove(int ignoreAbove) { @Override protected List> getParameters() { - return List.of(indexed, hasDocValues, stored, indexOptions, hasNorms, - rules, language, country, variant, strength, decomposition, alternate, - caseLevel, caseFirst, numeric, variableTop, hiraganaQuaternaryMode, - ignoreAbove, nullValue, meta); + return List.of( + indexed, + hasDocValues, + stored, + indexOptions, + hasNorms, + rules, + language, + country, + variant, + strength, + decomposition, + alternate, + caseLevel, + caseFirst, + numeric, + variableTop, + hiraganaQuaternaryMode, + ignoreAbove, + nullValue, + meta + ); } private CollatorParams collatorParams() { @@ -281,11 +323,25 @@ private FieldType buildFieldType() { public ICUCollationKeywordFieldMapper build(MapperBuilderContext context) { final CollatorParams params = collatorParams(); final Collator collator = params.buildCollator(); - CollationFieldType ft = new CollationFieldType(context.buildFullName(name), indexed.getValue(), - stored.getValue(), hasDocValues.getValue(), collator, nullValue.getValue(), ignoreAbove.getValue(), - meta.getValue()); - return new ICUCollationKeywordFieldMapper(name, buildFieldType(), ft, - multiFieldsBuilder.build(this, context), copyTo.build(), collator, this); + CollationFieldType ft = new CollationFieldType( + context.buildFullName(name), + indexed.getValue(), + stored.getValue(), + hasDocValues.getValue(), + collator, + nullValue.getValue(), + ignoreAbove.getValue(), + meta.getValue() + ); + return new ICUCollationKeywordFieldMapper( + name, + buildFieldType(), + ft, + multiFieldsBuilder.build(this, context), + copyTo.build(), + collator, + this + ); } } @@ -411,10 +467,15 @@ public Collator buildCollator() { private final boolean hasDocValues; private final String indexOptions; - protected ICUCollationKeywordFieldMapper(String simpleName, FieldType fieldType, - MappedFieldType mappedFieldType, - MultiFields multiFields, CopyTo copyTo, - Collator collator, Builder builder) { + protected ICUCollationKeywordFieldMapper( + String simpleName, + FieldType fieldType, + MappedFieldType mappedFieldType, + MultiFields multiFields, + CopyTo copyTo, + Collator collator, + Builder builder + ) { super(simpleName, mappedFieldType, Lucene.KEYWORD_ANALYZER, multiFields, copyTo); assert collator.isFrozen(); this.fieldType = fieldType; diff --git a/plugins/analysis-icu/src/main/java/org/elasticsearch/plugin/analysis/icu/IcuAnalyzerProvider.java b/plugins/analysis-icu/src/main/java/org/elasticsearch/plugin/analysis/icu/IcuAnalyzerProvider.java index 9635487a06178..17e54db020c5a 100644 --- a/plugins/analysis-icu/src/main/java/org/elasticsearch/plugin/analysis/icu/IcuAnalyzerProvider.java +++ b/plugins/analysis-icu/src/main/java/org/elasticsearch/plugin/analysis/icu/IcuAnalyzerProvider.java @@ -9,6 +9,7 @@ package org.elasticsearch.plugin.analysis.icu; import com.ibm.icu.text.Normalizer2; + import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.Tokenizer; import org.apache.lucene.analysis.icu.ICUFoldingFilter; @@ -30,11 +31,15 @@ public IcuAnalyzerProvider(IndexSettings indexSettings, Environment environment, String method = settings.get("method", "nfkc_cf"); String mode = settings.get("mode", "compose"); if ("compose".equals(mode) == false && "decompose".equals(mode) == false) { - throw new IllegalArgumentException("Unknown mode [" + mode + "] in analyzer [" + name + - "], expected one of [compose, decompose]"); + throw new IllegalArgumentException( + "Unknown mode [" + mode + "] in analyzer [" + name + "], expected one of [compose, decompose]" + ); } Normalizer2 normalizer = Normalizer2.getInstance( - null, method, "compose".equals(mode) ? Normalizer2.Mode.COMPOSE : Normalizer2.Mode.DECOMPOSE); + null, + method, + "compose".equals(mode) ? Normalizer2.Mode.COMPOSE : Normalizer2.Mode.DECOMPOSE + ); this.normalizer = IcuNormalizerTokenFilterFactory.wrapWithUnicodeSetFilter(indexSettings, normalizer, settings); } diff --git a/plugins/analysis-icu/src/main/java/org/elasticsearch/plugin/analysis/icu/IcuCollationTokenFilterFactory.java b/plugins/analysis-icu/src/main/java/org/elasticsearch/plugin/analysis/icu/IcuCollationTokenFilterFactory.java index 9cd9dc646652f..598dea7527d28 100644 --- a/plugins/analysis-icu/src/main/java/org/elasticsearch/plugin/analysis/icu/IcuCollationTokenFilterFactory.java +++ b/plugins/analysis-icu/src/main/java/org/elasticsearch/plugin/analysis/icu/IcuCollationTokenFilterFactory.java @@ -8,22 +8,22 @@ package org.elasticsearch.plugin.analysis.icu; -import java.io.IOException; -import java.nio.charset.Charset; -import java.nio.file.Files; -import java.nio.file.InvalidPathException; +import com.ibm.icu.text.Collator; +import com.ibm.icu.text.RuleBasedCollator; +import com.ibm.icu.util.ULocale; import org.apache.lucene.analysis.TokenStream; import org.elasticsearch.common.io.Streams; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.env.Environment; import org.elasticsearch.index.IndexSettings; - -import com.ibm.icu.text.Collator; -import com.ibm.icu.text.RuleBasedCollator; -import com.ibm.icu.util.ULocale; import org.elasticsearch.index.analysis.AbstractTokenFilterFactory; +import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.InvalidPathException; + /** * An ICU based collation token filter. There are two ways to configure collation: *

    The first is simply specifying the locale (defaults to the default diff --git a/plugins/analysis-icu/src/main/java/org/elasticsearch/plugin/analysis/icu/IcuFoldingTokenFilterFactory.java b/plugins/analysis-icu/src/main/java/org/elasticsearch/plugin/analysis/icu/IcuFoldingTokenFilterFactory.java index bf25fec1c5dc6..a00b76e47b8e1 100644 --- a/plugins/analysis-icu/src/main/java/org/elasticsearch/plugin/analysis/icu/IcuFoldingTokenFilterFactory.java +++ b/plugins/analysis-icu/src/main/java/org/elasticsearch/plugin/analysis/icu/IcuFoldingTokenFilterFactory.java @@ -18,7 +18,6 @@ import org.elasticsearch.index.analysis.AbstractTokenFilterFactory; import org.elasticsearch.index.analysis.NormalizingTokenFilterFactory; - /** * Uses the {@link org.apache.lucene.analysis.icu.ICUFoldingFilter}. * Applies foldings from UTR#30 Character Foldings. @@ -35,7 +34,10 @@ public class IcuFoldingTokenFilterFactory extends AbstractTokenFilterFactory implements NormalizingTokenFilterFactory { /** Store here the same Normalizer used by the lucene ICUFoldingFilter */ private static final Normalizer2 ICU_FOLDING_NORMALIZER = Normalizer2.getInstance( - ICUFoldingFilter.class.getResourceAsStream("utr30.nrm"), "utr30", Normalizer2.Mode.COMPOSE); + ICUFoldingFilter.class.getResourceAsStream("utr30.nrm"), + "utr30", + Normalizer2.Mode.COMPOSE + ); private final Normalizer2 normalizer; diff --git a/plugins/analysis-icu/src/main/java/org/elasticsearch/plugin/analysis/icu/IcuNormalizerCharFilterFactory.java b/plugins/analysis-icu/src/main/java/org/elasticsearch/plugin/analysis/icu/IcuNormalizerCharFilterFactory.java index 86b7126bafecf..1a5a1972f7dd4 100644 --- a/plugins/analysis-icu/src/main/java/org/elasticsearch/plugin/analysis/icu/IcuNormalizerCharFilterFactory.java +++ b/plugins/analysis-icu/src/main/java/org/elasticsearch/plugin/analysis/icu/IcuNormalizerCharFilterFactory.java @@ -8,7 +8,6 @@ package org.elasticsearch.plugin.analysis.icu; - import com.ibm.icu.text.Normalizer2; import org.apache.lucene.analysis.icu.ICUNormalizer2CharFilter; @@ -20,7 +19,6 @@ import java.io.Reader; - /** * Uses the {@link org.apache.lucene.analysis.icu.ICUNormalizer2CharFilter} to normalize character. *

    The {@code name} can be used to provide the type of normalization to perform.

    @@ -39,7 +37,10 @@ public IcuNormalizerCharFilterFactory(IndexSettings indexSettings, Environment e mode = "compose"; } Normalizer2 normalizer = Normalizer2.getInstance( - null, method, "compose".equals(mode) ? Normalizer2.Mode.COMPOSE : Normalizer2.Mode.DECOMPOSE); + null, + method, + "compose".equals(mode) ? Normalizer2.Mode.COMPOSE : Normalizer2.Mode.DECOMPOSE + ); this.normalizer = IcuNormalizerTokenFilterFactory.wrapWithUnicodeSetFilter(indexSettings, normalizer, settings); } diff --git a/plugins/analysis-icu/src/main/java/org/elasticsearch/plugin/analysis/icu/IcuNormalizerTokenFilterFactory.java b/plugins/analysis-icu/src/main/java/org/elasticsearch/plugin/analysis/icu/IcuNormalizerTokenFilterFactory.java index d05edf00e7682..1698df2180413 100644 --- a/plugins/analysis-icu/src/main/java/org/elasticsearch/plugin/analysis/icu/IcuNormalizerTokenFilterFactory.java +++ b/plugins/analysis-icu/src/main/java/org/elasticsearch/plugin/analysis/icu/IcuNormalizerTokenFilterFactory.java @@ -11,6 +11,7 @@ import com.ibm.icu.text.FilteredNormalizer2; import com.ibm.icu.text.Normalizer2; import com.ibm.icu.text.UnicodeSet; + import org.apache.lucene.analysis.TokenStream; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.env.Environment; @@ -18,7 +19,6 @@ import org.elasticsearch.index.analysis.AbstractTokenFilterFactory; import org.elasticsearch.index.analysis.NormalizingTokenFilterFactory; - /** * Uses the {@link org.apache.lucene.analysis.icu.ICUNormalizer2Filter} to normalize tokens. *

    The {@code name} can be used to provide the type of normalization to perform.

    @@ -40,9 +40,7 @@ public TokenStream create(TokenStream tokenStream) { return new org.apache.lucene.analysis.icu.ICUNormalizer2Filter(tokenStream, normalizer); } - static Normalizer2 wrapWithUnicodeSetFilter(final IndexSettings indexSettings, - final Normalizer2 normalizer, - final Settings settings) { + static Normalizer2 wrapWithUnicodeSetFilter(final IndexSettings indexSettings, final Normalizer2 normalizer, final Settings settings) { String unicodeSetFilter = settings.get("unicode_set_filter"); if (unicodeSetFilter != null) { UnicodeSet unicodeSet = new UnicodeSet(unicodeSetFilter); diff --git a/plugins/analysis-icu/src/main/java/org/elasticsearch/plugin/analysis/icu/IcuTokenizerFactory.java b/plugins/analysis-icu/src/main/java/org/elasticsearch/plugin/analysis/icu/IcuTokenizerFactory.java index e4b07a578a72b..8a5831a446276 100644 --- a/plugins/analysis-icu/src/main/java/org/elasticsearch/plugin/analysis/icu/IcuTokenizerFactory.java +++ b/plugins/analysis-icu/src/main/java/org/elasticsearch/plugin/analysis/icu/IcuTokenizerFactory.java @@ -13,6 +13,7 @@ import com.ibm.icu.lang.UScript; import com.ibm.icu.text.BreakIterator; import com.ibm.icu.text.RuleBasedBreakIterator; + import org.apache.lucene.analysis.Tokenizer; import org.apache.lucene.analysis.icu.segmentation.DefaultICUTokenizerConfig; import org.apache.lucene.analysis.icu.segmentation.ICUTokenizer; @@ -45,7 +46,7 @@ public IcuTokenizerFactory(IndexSettings indexSettings, Environment environment, public Tokenizer create() { if (config == null) { return new ICUTokenizer(); - }else{ + } else { return new ICUTokenizer(config); } } @@ -94,14 +95,11 @@ public RuleBasedBreakIterator getBreakIterator(int script) { } } - //parse a single RBBi rule file + // parse a single RBBi rule file private BreakIterator parseRules(String filename, Environment env) throws IOException { final Path path = env.configFile().resolve(filename); - String rules = Files.readAllLines(path) - .stream() - .filter((v) -> v.startsWith("#") == false) - .collect(Collectors.joining("\n")); + String rules = Files.readAllLines(path).stream().filter((v) -> v.startsWith("#") == false).collect(Collectors.joining("\n")); return new RuleBasedBreakIterator(rules.toString()); } diff --git a/plugins/analysis-icu/src/main/java/org/elasticsearch/plugin/analysis/icu/IcuTransformTokenFilterFactory.java b/plugins/analysis-icu/src/main/java/org/elasticsearch/plugin/analysis/icu/IcuTransformTokenFilterFactory.java index 5a00849fe7fb6..9222a7c811fd5 100644 --- a/plugins/analysis-icu/src/main/java/org/elasticsearch/plugin/analysis/icu/IcuTransformTokenFilterFactory.java +++ b/plugins/analysis-icu/src/main/java/org/elasticsearch/plugin/analysis/icu/IcuTransformTokenFilterFactory.java @@ -9,6 +9,7 @@ package org.elasticsearch.plugin.analysis.icu; import com.ibm.icu.text.Transliterator; + import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.icu.ICUTransformFilter; import org.elasticsearch.common.settings.Settings; diff --git a/plugins/analysis-icu/src/main/java/org/elasticsearch/plugin/analysis/icu/IndexableBinaryStringTools.java b/plugins/analysis-icu/src/main/java/org/elasticsearch/plugin/analysis/icu/IndexableBinaryStringTools.java index 41a213cc451a4..c7a93ddd40bb0 100644 --- a/plugins/analysis-icu/src/main/java/org/elasticsearch/plugin/analysis/icu/IndexableBinaryStringTools.java +++ b/plugins/analysis-icu/src/main/java/org/elasticsearch/plugin/analysis/icu/IndexableBinaryStringTools.java @@ -42,201 +42,193 @@ @Deprecated public final class IndexableBinaryStringTools { - private static final CodingCase[] CODING_CASES = { - // CodingCase(int initialShift, int finalShift) - new CodingCase( 7, 1 ), - // CodingCase(int initialShift, int middleShift, int finalShift) - new CodingCase(14, 6, 2), - new CodingCase(13, 5, 3), - new CodingCase(12, 4, 4), - new CodingCase(11, 3, 5), - new CodingCase(10, 2, 6), - new CodingCase( 9, 1, 7), - new CodingCase( 8, 0 ) - }; + private static final CodingCase[] CODING_CASES = { + // CodingCase(int initialShift, int finalShift) + new CodingCase(7, 1), + // CodingCase(int initialShift, int middleShift, int finalShift) + new CodingCase(14, 6, 2), + new CodingCase(13, 5, 3), + new CodingCase(12, 4, 4), + new CodingCase(11, 3, 5), + new CodingCase(10, 2, 6), + new CodingCase(9, 1, 7), + new CodingCase(8, 0) }; - // Export only static methods - private IndexableBinaryStringTools() {} + // Export only static methods + private IndexableBinaryStringTools() {} - /** - * Returns the number of chars required to encode the given bytes. - * - * @param inputArray byte sequence to be encoded - * @param inputOffset initial offset into inputArray - * @param inputLength number of bytes in inputArray - * @return The number of chars required to encode the number of bytes. - */ - public static int getEncodedLength(byte[] inputArray, int inputOffset, - int inputLength) { - // Use long for intermediaries to protect against overflow - return (int)((8L * inputLength + 14L) / 15L) + 1; - } - - /** - * Returns the number of bytes required to decode the given char sequence. - * - * @param encoded char sequence to be decoded - * @param offset initial offset - * @param length number of characters - * @return The number of bytes required to decode the given char sequence - */ - public static int getDecodedLength(char[] encoded, int offset, int length) { - final int numChars = length - 1; - if (numChars <= 0) { - return 0; - } else { - // Use long for intermediaries to protect against overflow - final long numFullBytesInFinalChar = encoded[offset + length - 1]; - final long numEncodedChars = numChars - 1; - return (int)((numEncodedChars * 15L + 7L) / 8L + numFullBytesInFinalChar); + /** + * Returns the number of chars required to encode the given bytes. + * + * @param inputArray byte sequence to be encoded + * @param inputOffset initial offset into inputArray + * @param inputLength number of bytes in inputArray + * @return The number of chars required to encode the number of bytes. + */ + public static int getEncodedLength(byte[] inputArray, int inputOffset, int inputLength) { + // Use long for intermediaries to protect against overflow + return (int) ((8L * inputLength + 14L) / 15L) + 1; } - } - /** - * Encodes the input byte sequence into the output char sequence. Before - * calling this method, ensure that the output array has sufficient - * capacity by calling {@link #getEncodedLength(byte[], int, int)}. - * - * @param inputArray byte sequence to be encoded - * @param inputOffset initial offset into inputArray - * @param inputLength number of bytes in inputArray - * @param outputArray char sequence to store encoded result - * @param outputOffset initial offset into outputArray - * @param outputLength length of output, must be getEncodedLength - */ - public static void encode(byte[] inputArray, int inputOffset, - int inputLength, char[] outputArray, int outputOffset, int outputLength) { - assert (outputLength == getEncodedLength(inputArray, inputOffset, - inputLength)); - if (inputLength > 0) { - int inputByteNum = inputOffset; - int caseNum = 0; - int outputCharNum = outputOffset; - CodingCase codingCase; - for (; inputByteNum + CODING_CASES[caseNum].numBytes <= inputLength; ++outputCharNum) { - codingCase = CODING_CASES[caseNum]; - if (2 == codingCase.numBytes) { - outputArray[outputCharNum] = (char) (((inputArray[inputByteNum] & 0xFF) << codingCase.initialShift) - + (((inputArray[inputByteNum + 1] & 0xFF) >>> codingCase.finalShift) & codingCase.finalMask) & (short) 0x7FFF); - } else { // numBytes is 3 - outputArray[outputCharNum] = (char) (((inputArray[inputByteNum] & 0xFF) << codingCase.initialShift) - + ((inputArray[inputByteNum + 1] & 0xFF) << codingCase.middleShift) - + (((inputArray[inputByteNum + 2] & 0xFF) >>> codingCase.finalShift) & codingCase.finalMask) & (short) 0x7FFF); - } - inputByteNum += codingCase.advanceBytes; - if (++caseNum == CODING_CASES.length) { - caseNum = 0; + /** + * Returns the number of bytes required to decode the given char sequence. + * + * @param encoded char sequence to be decoded + * @param offset initial offset + * @param length number of characters + * @return The number of bytes required to decode the given char sequence + */ + public static int getDecodedLength(char[] encoded, int offset, int length) { + final int numChars = length - 1; + if (numChars <= 0) { + return 0; + } else { + // Use long for intermediaries to protect against overflow + final long numFullBytesInFinalChar = encoded[offset + length - 1]; + final long numEncodedChars = numChars - 1; + return (int) ((numEncodedChars * 15L + 7L) / 8L + numFullBytesInFinalChar); } - } - // Produce final char (if any) and trailing count chars. - codingCase = CODING_CASES[caseNum]; + } - if (inputByteNum + 1 < inputLength) { // codingCase.numBytes must be 3 - outputArray[outputCharNum++] = (char) ( - ( ((inputArray[inputByteNum] & 0xFF) << codingCase.initialShift) - + ((inputArray[inputByteNum + 1] & 0xFF) << codingCase.middleShift) - ) & (short) 0x7FFF); - // Add trailing char containing the number of full bytes in final char - outputArray[outputCharNum++] = (char) 1; - } else if (inputByteNum < inputLength) { - outputArray[outputCharNum++] = (char) (((inputArray[inputByteNum] & 0xFF) << codingCase.initialShift) & (short) 0x7FFF); - // Add trailing char containing the number of full bytes in final char - outputArray[outputCharNum++] = caseNum == 0 ? (char) 1 : (char) 0; - } else { // No left over bits - last char is completely filled. - // Add trailing char containing the number of full bytes in final char - outputArray[outputCharNum++] = (char) 1; - } + /** + * Encodes the input byte sequence into the output char sequence. Before + * calling this method, ensure that the output array has sufficient + * capacity by calling {@link #getEncodedLength(byte[], int, int)}. + * + * @param inputArray byte sequence to be encoded + * @param inputOffset initial offset into inputArray + * @param inputLength number of bytes in inputArray + * @param outputArray char sequence to store encoded result + * @param outputOffset initial offset into outputArray + * @param outputLength length of output, must be getEncodedLength + */ + public static void encode(byte[] inputArray, int inputOffset, int inputLength, char[] outputArray, int outputOffset, int outputLength) { + assert (outputLength == getEncodedLength(inputArray, inputOffset, inputLength)); + if (inputLength > 0) { + int inputByteNum = inputOffset; + int caseNum = 0; + int outputCharNum = outputOffset; + CodingCase codingCase; + for (; inputByteNum + CODING_CASES[caseNum].numBytes <= inputLength; ++outputCharNum) { + codingCase = CODING_CASES[caseNum]; + if (2 == codingCase.numBytes) { + outputArray[outputCharNum] = (char) (((inputArray[inputByteNum] & 0xFF) << codingCase.initialShift) + + (((inputArray[inputByteNum + 1] & 0xFF) >>> codingCase.finalShift) & codingCase.finalMask) & (short) 0x7FFF); + } else { // numBytes is 3 + outputArray[outputCharNum] = (char) (((inputArray[inputByteNum] & 0xFF) << codingCase.initialShift) + + ((inputArray[inputByteNum + 1] & 0xFF) << codingCase.middleShift) + (((inputArray[inputByteNum + 2] & 0xFF) + >>> codingCase.finalShift) & codingCase.finalMask) & (short) 0x7FFF); + } + inputByteNum += codingCase.advanceBytes; + if (++caseNum == CODING_CASES.length) { + caseNum = 0; + } + } + // Produce final char (if any) and trailing count chars. + codingCase = CODING_CASES[caseNum]; + + if (inputByteNum + 1 < inputLength) { // codingCase.numBytes must be 3 + outputArray[outputCharNum++] = (char) ((((inputArray[inputByteNum] & 0xFF) << codingCase.initialShift) + + ((inputArray[inputByteNum + 1] & 0xFF) << codingCase.middleShift)) & (short) 0x7FFF); + // Add trailing char containing the number of full bytes in final char + outputArray[outputCharNum++] = (char) 1; + } else if (inputByteNum < inputLength) { + outputArray[outputCharNum++] = (char) (((inputArray[inputByteNum] & 0xFF) << codingCase.initialShift) & (short) 0x7FFF); + // Add trailing char containing the number of full bytes in final char + outputArray[outputCharNum++] = caseNum == 0 ? (char) 1 : (char) 0; + } else { // No left over bits - last char is completely filled. + // Add trailing char containing the number of full bytes in final char + outputArray[outputCharNum++] = (char) 1; + } + } } - } - /** - * Decodes the input char sequence into the output byte sequence. Before - * calling this method, ensure that the output array has sufficient capacity - * by calling {@link #getDecodedLength(char[], int, int)}. - * - * @param inputArray char sequence to be decoded - * @param inputOffset initial offset into inputArray - * @param inputLength number of chars in inputArray - * @param outputArray byte sequence to store encoded result - * @param outputOffset initial offset into outputArray - * @param outputLength length of output, must be - * getDecodedLength(inputArray, inputOffset, inputLength) - */ - public static void decode(char[] inputArray, int inputOffset, - int inputLength, byte[] outputArray, int outputOffset, int outputLength) { - assert (outputLength == getDecodedLength(inputArray, inputOffset, - inputLength)); - final int numInputChars = inputLength - 1; - final int numOutputBytes = outputLength; + /** + * Decodes the input char sequence into the output byte sequence. Before + * calling this method, ensure that the output array has sufficient capacity + * by calling {@link #getDecodedLength(char[], int, int)}. + * + * @param inputArray char sequence to be decoded + * @param inputOffset initial offset into inputArray + * @param inputLength number of chars in inputArray + * @param outputArray byte sequence to store encoded result + * @param outputOffset initial offset into outputArray + * @param outputLength length of output, must be + * getDecodedLength(inputArray, inputOffset, inputLength) + */ + public static void decode(char[] inputArray, int inputOffset, int inputLength, byte[] outputArray, int outputOffset, int outputLength) { + assert (outputLength == getDecodedLength(inputArray, inputOffset, inputLength)); + final int numInputChars = inputLength - 1; + final int numOutputBytes = outputLength; - if (numOutputBytes > 0) { - int caseNum = 0; - int outputByteNum = outputOffset; - int inputCharNum = inputOffset; - short inputChar; - CodingCase codingCase; - for (; inputCharNum < numInputChars - 1; ++inputCharNum) { - codingCase = CODING_CASES[caseNum]; - inputChar = (short) inputArray[inputCharNum]; - if (2 == codingCase.numBytes) { - if (0 == caseNum) { - outputArray[outputByteNum] = (byte) (inputChar >>> codingCase.initialShift); - } else { + if (numOutputBytes > 0) { + int caseNum = 0; + int outputByteNum = outputOffset; + int inputCharNum = inputOffset; + short inputChar; + CodingCase codingCase; + for (; inputCharNum < numInputChars - 1; ++inputCharNum) { + codingCase = CODING_CASES[caseNum]; + inputChar = (short) inputArray[inputCharNum]; + if (2 == codingCase.numBytes) { + if (0 == caseNum) { + outputArray[outputByteNum] = (byte) (inputChar >>> codingCase.initialShift); + } else { + outputArray[outputByteNum] += (byte) (inputChar >>> codingCase.initialShift); + } + outputArray[outputByteNum + 1] = (byte) ((inputChar & codingCase.finalMask) << codingCase.finalShift); + } else { // numBytes is 3 + outputArray[outputByteNum] += (byte) (inputChar >>> codingCase.initialShift); + outputArray[outputByteNum + 1] = (byte) ((inputChar & codingCase.middleMask) >>> codingCase.middleShift); + outputArray[outputByteNum + 2] = (byte) ((inputChar & codingCase.finalMask) << codingCase.finalShift); + } + outputByteNum += codingCase.advanceBytes; + if (++caseNum == CODING_CASES.length) { + caseNum = 0; + } + } + // Handle final char + inputChar = (short) inputArray[inputCharNum]; + codingCase = CODING_CASES[caseNum]; + if (0 == caseNum) { + outputArray[outputByteNum] = 0; + } outputArray[outputByteNum] += (byte) (inputChar >>> codingCase.initialShift); - } - outputArray[outputByteNum + 1] = (byte) ((inputChar & codingCase.finalMask) << codingCase.finalShift); - } else { // numBytes is 3 - outputArray[outputByteNum] += (byte) (inputChar >>> codingCase.initialShift); - outputArray[outputByteNum + 1] = (byte) ((inputChar & codingCase.middleMask) >>> codingCase.middleShift); - outputArray[outputByteNum + 2] = (byte) ((inputChar & codingCase.finalMask) << codingCase.finalShift); + final int bytesLeft = numOutputBytes - outputByteNum; + if (bytesLeft > 1) { + if (2 == codingCase.numBytes) { + outputArray[outputByteNum + 1] = (byte) ((inputChar & codingCase.finalMask) >>> codingCase.finalShift); + } else { // numBytes is 3 + outputArray[outputByteNum + 1] = (byte) ((inputChar & codingCase.middleMask) >>> codingCase.middleShift); + if (bytesLeft > 2) { + outputArray[outputByteNum + 2] = (byte) ((inputChar & codingCase.finalMask) << codingCase.finalShift); + } + } + } } - outputByteNum += codingCase.advanceBytes; - if (++caseNum == CODING_CASES.length) { - caseNum = 0; - } - } - // Handle final char - inputChar = (short) inputArray[inputCharNum]; - codingCase = CODING_CASES[caseNum]; - if (0 == caseNum) { - outputArray[outputByteNum] = 0; - } - outputArray[outputByteNum] += (byte) (inputChar >>> codingCase.initialShift); - final int bytesLeft = numOutputBytes - outputByteNum; - if (bytesLeft > 1) { - if (2 == codingCase.numBytes) { - outputArray[outputByteNum + 1] = (byte) ((inputChar & codingCase.finalMask) >>> codingCase.finalShift); - } else { // numBytes is 3 - outputArray[outputByteNum + 1] = (byte) ((inputChar & codingCase.middleMask) >>> codingCase.middleShift); - if (bytesLeft > 2) { - outputArray[outputByteNum + 2] = (byte) ((inputChar & codingCase.finalMask) << codingCase.finalShift); - } - } - } } - } - static class CodingCase { - int numBytes, initialShift, middleShift, finalShift, advanceBytes = 2; - short middleMask, finalMask; + static class CodingCase { + int numBytes, initialShift, middleShift, finalShift, advanceBytes = 2; + short middleMask, finalMask; - CodingCase(int initialShift, int middleShift, int finalShift) { - this.numBytes = 3; - this.initialShift = initialShift; - this.middleShift = middleShift; - this.finalShift = finalShift; - this.finalMask = (short)((short)0xFF >>> finalShift); - this.middleMask = (short)((short)0xFF << middleShift); - } + CodingCase(int initialShift, int middleShift, int finalShift) { + this.numBytes = 3; + this.initialShift = initialShift; + this.middleShift = middleShift; + this.finalShift = finalShift; + this.finalMask = (short) ((short) 0xFF >>> finalShift); + this.middleMask = (short) ((short) 0xFF << middleShift); + } - CodingCase(int initialShift, int finalShift) { - this.numBytes = 2; - this.initialShift = initialShift; - this.finalShift = finalShift; - this.finalMask = (short)((short)0xFF >>> finalShift); - if (finalShift != 0) { - advanceBytes = 1; - } + CodingCase(int initialShift, int finalShift) { + this.numBytes = 2; + this.initialShift = initialShift; + this.finalShift = finalShift; + this.finalMask = (short) ((short) 0xFF >>> finalShift); + if (finalShift != 0) { + advanceBytes = 1; + } + } } - } } diff --git a/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/AnalysisICUFactoryTests.java b/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/AnalysisICUFactoryTests.java index 2747e8d138885..4922fbc32045d 100644 --- a/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/AnalysisICUFactoryTests.java +++ b/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/AnalysisICUFactoryTests.java @@ -8,13 +8,7 @@ package org.elasticsearch.plugin.analysis.icu; -import org.elasticsearch.plugin.analysis.icu.IcuFoldingTokenFilterFactory; -import org.elasticsearch.plugin.analysis.icu.IcuNormalizerCharFilterFactory; -import org.elasticsearch.plugin.analysis.icu.IcuNormalizerTokenFilterFactory; -import org.elasticsearch.plugin.analysis.icu.IcuTokenizerFactory; -import org.elasticsearch.plugin.analysis.icu.IcuTransformTokenFilterFactory; import org.elasticsearch.indices.analysis.AnalysisFactoryTestCase; -import org.elasticsearch.plugin.analysis.icu.AnalysisICUPlugin; import java.util.HashMap; import java.util.Map; diff --git a/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/CollationFieldTypeTests.java b/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/CollationFieldTypeTests.java index 22c41b8635ef2..5a03632b0052e 100644 --- a/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/CollationFieldTypeTests.java +++ b/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/CollationFieldTypeTests.java @@ -11,6 +11,7 @@ import com.ibm.icu.text.Collator; import com.ibm.icu.text.RawCollationKey; import com.ibm.icu.util.ULocale; + import org.apache.lucene.index.Term; import org.apache.lucene.search.TermInSetQuery; import org.apache.lucene.search.TermQuery; @@ -20,8 +21,8 @@ import org.elasticsearch.common.unit.Fuzziness; import org.elasticsearch.index.mapper.FieldTypeTestCase; import org.elasticsearch.index.mapper.MappedFieldType; -import org.elasticsearch.plugin.analysis.icu.ICUCollationKeywordFieldMapper.CollationFieldType; import org.elasticsearch.index.mapper.MappedFieldType.Relation; +import org.elasticsearch.plugin.analysis.icu.ICUCollationKeywordFieldMapper.CollationFieldType; import java.io.IOException; import java.util.ArrayList; @@ -39,10 +40,19 @@ private static CollationFieldType createFieldType() { public void testIsFieldWithinQuery() throws IOException { CollationFieldType ft = createFieldType(); // current impl ignores args and shourd always return INTERSECTS - assertEquals(Relation.INTERSECTS, ft.isFieldWithinQuery(null, - RandomStrings.randomAsciiOfLengthBetween(random(), 0, 5), - RandomStrings.randomAsciiOfLengthBetween(random(), 0, 5), - randomBoolean(), randomBoolean(), null, null, null)); + assertEquals( + Relation.INTERSECTS, + ft.isFieldWithinQuery( + null, + RandomStrings.randomAsciiOfLengthBetween(random(), 0, 5), + RandomStrings.randomAsciiOfLengthBetween(random(), 0, 5), + randomBoolean(), + randomBoolean(), + null, + null, + null + ) + ); } public void testTermQuery() { @@ -57,8 +67,7 @@ public void testTermQuery() { assertEquals(new TermQuery(new Term("field", expected)), ft.termQuery("I WİLL USE TURKİSH CASING", null)); MappedFieldType unsearchable = new CollationFieldType("field", false, collator); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> unsearchable.termQuery("bar", null)); + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> unsearchable.termQuery("bar", null)); assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage()); } @@ -73,40 +82,49 @@ public void testTermsQuery() { terms.add(new BytesRef(fooKey.bytes, 0, fooKey.size)); terms.add(new BytesRef(barKey.bytes, 0, barKey.size)); - assertEquals(new TermInSetQuery("field", terms), - ft.termsQuery(Arrays.asList("foo", "bar"), null)); + assertEquals(new TermInSetQuery("field", terms), ft.termsQuery(Arrays.asList("foo", "bar"), null)); MappedFieldType unsearchable = new CollationFieldType("field", false, collator); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> unsearchable.termsQuery(Arrays.asList("foo", "bar"), null)); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> unsearchable.termsQuery(Arrays.asList("foo", "bar"), null) + ); assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage()); } public void testRegexpQuery() { MappedFieldType ft = createFieldType(); - UnsupportedOperationException e = expectThrows(UnsupportedOperationException.class, - () -> ft.regexpQuery("foo.*", 0, 0, 10, null, randomMockContext())); + UnsupportedOperationException e = expectThrows( + UnsupportedOperationException.class, + () -> ft.regexpQuery("foo.*", 0, 0, 10, null, randomMockContext()) + ); assertEquals("[regexp] queries are not supported on [icu_collation_keyword] fields.", e.getMessage()); } public void testFuzzyQuery() { MappedFieldType ft = createFieldType(); - UnsupportedOperationException e = expectThrows(UnsupportedOperationException.class, - () -> ft.fuzzyQuery("foo", Fuzziness.fromEdits(2), 1, 50, true, randomMockContext())); + UnsupportedOperationException e = expectThrows( + UnsupportedOperationException.class, + () -> ft.fuzzyQuery("foo", Fuzziness.fromEdits(2), 1, 50, true, randomMockContext()) + ); assertEquals("[fuzzy] queries are not supported on [icu_collation_keyword] fields.", e.getMessage()); } public void testPrefixQuery() { MappedFieldType ft = createFieldType(); - UnsupportedOperationException e = expectThrows(UnsupportedOperationException.class, - () -> ft.prefixQuery("prefix", null, randomMockContext())); + UnsupportedOperationException e = expectThrows( + UnsupportedOperationException.class, + () -> ft.prefixQuery("prefix", null, randomMockContext()) + ); assertEquals("[prefix] queries are not supported on [icu_collation_keyword] fields.", e.getMessage()); } public void testWildcardQuery() { MappedFieldType ft = createFieldType(); - UnsupportedOperationException e = expectThrows(UnsupportedOperationException.class, - () -> ft.wildcardQuery("foo*", null, randomMockContext())); + UnsupportedOperationException e = expectThrows( + UnsupportedOperationException.class, + () -> ft.wildcardQuery("foo*", null, randomMockContext()) + ); assertEquals("[wildcard] queries are not supported on [icu_collation_keyword] fields.", e.getMessage()); } @@ -115,19 +133,30 @@ public void testRangeQuery() { RawCollationKey aKey = DEFAULT_COLLATOR.getRawCollationKey("a", null); RawCollationKey bKey = DEFAULT_COLLATOR.getRawCollationKey("b", null); - TermRangeQuery expected = new TermRangeQuery("field", new BytesRef(aKey.bytes, 0, aKey.size), - new BytesRef(bKey.bytes, 0, bKey.size), false, false); + TermRangeQuery expected = new TermRangeQuery( + "field", + new BytesRef(aKey.bytes, 0, aKey.size), + new BytesRef(bKey.bytes, 0, bKey.size), + false, + false + ); assertEquals(expected, ft.rangeQuery("a", "b", false, false, null, null, null, MOCK_CONTEXT)); - ElasticsearchException ee = expectThrows(ElasticsearchException.class, - () -> ft.rangeQuery("a", "b", true, true, null, null, null, MOCK_CONTEXT_DISALLOW_EXPENSIVE)); - assertEquals("[range] queries on [text] or [keyword] fields cannot be executed when " + - "'search.allow_expensive_queries' is set to false.", ee.getMessage()); + ElasticsearchException ee = expectThrows( + ElasticsearchException.class, + () -> ft.rangeQuery("a", "b", true, true, null, null, null, MOCK_CONTEXT_DISALLOW_EXPENSIVE) + ); + assertEquals( + "[range] queries on [text] or [keyword] fields cannot be executed when " + "'search.allow_expensive_queries' is set to false.", + ee.getMessage() + ); MappedFieldType unsearchable = new CollationFieldType("field", false, DEFAULT_COLLATOR); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> unsearchable.rangeQuery("a", "b", false, false, null, null, null, MOCK_CONTEXT)); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> unsearchable.rangeQuery("a", "b", false, false, null, null, null, MOCK_CONTEXT) + ); assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage()); } } diff --git a/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/ICUCollationKeywordFieldMapperTests.java b/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/ICUCollationKeywordFieldMapperTests.java index 9cbdbb4504237..caac807e95c3b 100644 --- a/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/ICUCollationKeywordFieldMapperTests.java +++ b/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/ICUCollationKeywordFieldMapperTests.java @@ -18,9 +18,6 @@ import org.apache.lucene.util.BytesRef; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.MapperParsingException; @@ -28,8 +25,10 @@ import org.elasticsearch.index.mapper.MapperTestCase; import org.elasticsearch.index.mapper.ParsedDocument; import org.elasticsearch.index.mapper.SourceToParse; -import org.elasticsearch.plugin.analysis.icu.AnalysisICUPlugin; import org.elasticsearch.plugins.Plugin; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentType; import java.io.IOException; import java.util.Arrays; @@ -106,11 +105,9 @@ public void testNullValue() throws IOException { assertArrayEquals(new IndexableField[0], doc.rootDoc().getFields("field")); mapper = createDocumentMapper(fieldMapping(b -> b.field("type", FIELD_TYPE).field("null_value", "1234"))); - doc = mapper.parse(new SourceToParse("test", "1", BytesReference - .bytes(XContentFactory.jsonBuilder() - .startObject() - .endObject()), - XContentType.JSON)); + doc = mapper.parse( + new SourceToParse("test", "1", BytesReference.bytes(XContentFactory.jsonBuilder().startObject().endObject()), XContentType.JSON) + ); IndexableField[] fields = doc.rootDoc().getFields("field"); assertEquals(0, fields.length); @@ -208,8 +205,10 @@ public void testIndexOptions() throws IOException { assertEquals(IndexOptions.DOCS_AND_FREQS, fields[0].fieldType().indexOptions()); for (String indexOptions : Arrays.asList("positions", "offsets")) { - Exception e = expectThrows(MapperParsingException.class, - () -> createDocumentMapper(fieldMapping(b -> b.field("type", FIELD_TYPE).field("index_options", indexOptions)))); + Exception e = expectThrows( + MapperParsingException.class, + () -> createDocumentMapper(fieldMapping(b -> b.field("type", FIELD_TYPE).field("index_options", indexOptions))) + ); assertThat( e.getMessage(), containsString("Unknown value [" + indexOptions + "] for field [index_options] - accepted values are [docs, freqs]") @@ -268,7 +267,6 @@ public void testUpdateCollator() throws IOException { assertThat(e.getMessage(), containsString("Cannot update parameter [language] from [tr] to [en]")); } - public void testIgnoreAbove() throws IOException { DocumentMapper mapper = createDocumentMapper(fieldMapping(b -> b.field("type", FIELD_TYPE).field("ignore_above", 5))); ParsedDocument doc = mapper.parse(source(b -> b.field("field", "elk"))); diff --git a/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/ICUCollationKeywordFieldTypeTests.java b/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/ICUCollationKeywordFieldTypeTests.java index bf383666eb16d..6dfa74225a16a 100644 --- a/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/ICUCollationKeywordFieldTypeTests.java +++ b/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/ICUCollationKeywordFieldTypeTests.java @@ -10,7 +10,6 @@ import org.elasticsearch.index.mapper.FieldTypeTestCase; import org.elasticsearch.index.mapper.MapperBuilderContext; -import org.elasticsearch.plugin.analysis.icu.ICUCollationKeywordFieldMapper; import java.io.IOException; import java.util.List; @@ -19,20 +18,17 @@ public class ICUCollationKeywordFieldTypeTests extends FieldTypeTestCase { public void testFetchSourceValue() throws IOException { - ICUCollationKeywordFieldMapper mapper = new ICUCollationKeywordFieldMapper.Builder("field") - .build(MapperBuilderContext.ROOT); + ICUCollationKeywordFieldMapper mapper = new ICUCollationKeywordFieldMapper.Builder("field").build(MapperBuilderContext.ROOT); assertEquals(List.of("42"), fetchSourceValue(mapper.fieldType(), 42L)); assertEquals(List.of("true"), fetchSourceValue(mapper.fieldType(), true)); - ICUCollationKeywordFieldMapper ignoreAboveMapper = new ICUCollationKeywordFieldMapper.Builder("field") - .ignoreAbove(4) + ICUCollationKeywordFieldMapper ignoreAboveMapper = new ICUCollationKeywordFieldMapper.Builder("field").ignoreAbove(4) .build(MapperBuilderContext.ROOT); assertEquals(List.of(), fetchSourceValue(ignoreAboveMapper.fieldType(), "value")); assertEquals(List.of("42"), fetchSourceValue(ignoreAboveMapper.fieldType(), 42L)); assertEquals(List.of("true"), fetchSourceValue(ignoreAboveMapper.fieldType(), true)); - ICUCollationKeywordFieldMapper nullValueMapper = new ICUCollationKeywordFieldMapper.Builder("field") - .nullValue("NULL") + ICUCollationKeywordFieldMapper nullValueMapper = new ICUCollationKeywordFieldMapper.Builder("field").nullValue("NULL") .build(MapperBuilderContext.ROOT); assertEquals(List.of("NULL"), fetchSourceValue(nullValueMapper.fieldType(), null)); } diff --git a/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/IcuAnalyzerTests.java b/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/IcuAnalyzerTests.java index 9dd0de4334f1d..4d1e6eb14dc99 100644 --- a/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/IcuAnalyzerTests.java +++ b/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/IcuAnalyzerTests.java @@ -14,8 +14,6 @@ import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.IndexSettings; -import org.elasticsearch.plugin.analysis.icu.IcuAnalyzerProvider; -import org.elasticsearch.plugin.analysis.icu.AnalysisICUPlugin; import org.elasticsearch.test.IndexSettingsModule; import java.io.IOException; @@ -26,58 +24,51 @@ public class IcuAnalyzerTests extends BaseTokenStreamTestCase { public void testMixedAlphabetTokenization() throws IOException { - Settings settings = Settings.builder() - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .build(); + Settings settings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).build(); IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("index", settings); String input = "안녕은하철도999극장판2.1981년8월8일.일본개봉작1999년재더빙video판"; AnalysisICUPlugin plugin = new AnalysisICUPlugin(); Analyzer analyzer = plugin.getAnalyzers().get("icu_analyzer").get(idxSettings, null, "icu", settings).get(); - assertAnalyzesTo(analyzer, input, - new String[]{"안녕은하철도", "999", "극장판", "2.1981", "년", "8", "월", "8", "일", "일본개봉작", "1999", "년재더빙", "video", "판"}); + assertAnalyzesTo( + analyzer, + input, + new String[] { "안녕은하철도", "999", "극장판", "2.1981", "년", "8", "월", "8", "일", "일본개봉작", "1999", "년재더빙", "video", "판" } + ); } public void testMiddleDots() throws IOException { - Settings settings = Settings.builder() - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .build(); + Settings settings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).build(); IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("index", settings); String input = "경승지·산악·협곡·해협·곶·심연·폭포·호수·급류"; Analyzer analyzer = new IcuAnalyzerProvider(idxSettings, null, "icu", settings).get(); - assertAnalyzesTo(analyzer, input, - new String[]{"경승지", "산악", "협곡", "해협", "곶", "심연", "폭포", "호수", "급류"}); + assertAnalyzesTo(analyzer, input, new String[] { "경승지", "산악", "협곡", "해협", "곶", "심연", "폭포", "호수", "급류" }); } public void testUnicodeNumericCharacters() throws IOException { - Settings settings = Settings.builder() - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .build(); + Settings settings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).build(); IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("index", settings); String input = "① ② ③ ⑴ ⑵ ⑶ ¼ ⅓ ⅜ ¹ ² ³ ₁ ₂ ₃"; Analyzer analyzer = new IcuAnalyzerProvider(idxSettings, null, "icu", settings).get(); - assertAnalyzesTo(analyzer, input, - new String[]{"1", "2", "3", "1", "2", "3", "1/4", "1/3", "3/8", "1", "2", "3", "1", "2", "3"}); + assertAnalyzesTo(analyzer, input, new String[] { "1", "2", "3", "1", "2", "3", "1/4", "1/3", "3/8", "1", "2", "3", "1", "2", "3" }); } public void testBadSettings() { - Settings settings = Settings.builder() - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put("mode", "wrong") - .build(); + Settings settings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).put("mode", "wrong").build(); IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("index", settings); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> { - new IcuAnalyzerProvider(idxSettings, null, "icu", settings); - }); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> { new IcuAnalyzerProvider(idxSettings, null, "icu", settings); } + ); assertThat(e.getMessage(), containsString("Unknown mode [wrong] in analyzer [icu], expected one of [compose, decompose]")); diff --git a/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/IcuTokenizerFactoryTests.java b/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/IcuTokenizerFactoryTests.java index 818adfa127598..73dbcf97ab3f1 100644 --- a/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/IcuTokenizerFactoryTests.java +++ b/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/IcuTokenizerFactoryTests.java @@ -36,7 +36,7 @@ public void testSimpleIcuTokenizer() throws IOException { Reader reader = new StringReader("向日葵, one-two"); tokenizer.setReader(reader); - assertTokenStreamContents(tokenizer, new String[]{"向日葵", "one", "two"}); + assertTokenStreamContents(tokenizer, new String[] { "向日葵", "one", "two" }); } public void testIcuCustomizeRuleFile() throws IOException { @@ -45,13 +45,28 @@ public void testIcuCustomizeRuleFile() throws IOException { // test the tokenizer with single rule file TokenizerFactory tokenizerFactory = analysis.tokenizer.get("user_rule_tokenizer"); ICUTokenizer tokenizer = (ICUTokenizer) tokenizerFactory.create(); - Reader reader = new StringReader - ("One-two punch. Brang-, not brung-it. This one--not that one--is the right one, -ish."); + Reader reader = new StringReader("One-two punch. Brang-, not brung-it. This one--not that one--is the right one, -ish."); tokenizer.setReader(reader); - assertTokenStreamContents(tokenizer, - new String[]{"One-two", "punch", "Brang", "not", "brung-it", - "This", "one", "not", "that", "one", "is", "the", "right", "one", "ish"}); + assertTokenStreamContents( + tokenizer, + new String[] { + "One-two", + "punch", + "Brang", + "not", + "brung-it", + "This", + "one", + "not", + "that", + "one", + "is", + "the", + "right", + "one", + "ish" } + ); } public void testMultipleIcuCustomizeRuleFiles() throws IOException { @@ -60,17 +75,15 @@ public void testMultipleIcuCustomizeRuleFiles() throws IOException { // test the tokenizer with two rule files TokenizerFactory tokenizerFactory = analysis.tokenizer.get("multi_rule_tokenizer"); ICUTokenizer tokenizer = (ICUTokenizer) tokenizerFactory.create(); - StringReader reader = new StringReader - ("Some English. Немного русский. ข้อความภาษาไทยเล็ก ๆ น้อย ๆ More English."); + StringReader reader = new StringReader("Some English. Немного русский. ข้อความภาษาไทยเล็ก ๆ น้อย ๆ More English."); tokenizer.setReader(reader); - assertTokenStreamContents(tokenizer, new String[]{"Some", "English", - "Немного русский. ", - "ข้อความภาษาไทยเล็ก ๆ น้อย ๆ ", - "More", "English"}); + assertTokenStreamContents( + tokenizer, + new String[] { "Some", "English", "Немного русский. ", "ข้อความภาษาไทยเล็ก ๆ น้อย ๆ ", "More", "English" } + ); } - private static TestAnalysis createTestAnalysis() throws IOException { InputStream keywords = IcuTokenizerFactoryTests.class.getResourceAsStream("KeywordTokenizer.rbbi"); InputStream latin = IcuTokenizerFactoryTests.class.getResourceAsStream("Latin-dont-break-on-hyphens.rbbi"); diff --git a/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/IndexableBinaryStringToolsTests.java b/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/IndexableBinaryStringToolsTests.java index df4b521b61da6..bb3b6fe2e42cb 100644 --- a/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/IndexableBinaryStringToolsTests.java +++ b/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/IndexableBinaryStringToolsTests.java @@ -21,10 +21,10 @@ import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope; import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope.Scope; import com.carrotsearch.randomizedtesting.annotations.TimeoutSuite; + import org.apache.lucene.util.ArrayUtil; import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.TimeUnits; -import org.elasticsearch.plugin.analysis.icu.IndexableBinaryStringTools; import org.elasticsearch.test.junit.listeners.ReproduceInfoPrinter; import org.junit.BeforeClass; @@ -34,215 +34,223 @@ * @deprecated Remove when IndexableBinaryStringTools is removed. */ @Deprecated -@Listeners({ - ReproduceInfoPrinter.class -}) +@Listeners({ ReproduceInfoPrinter.class }) @ThreadLeakScope(Scope.NONE) @TimeoutSuite(millis = TimeUnits.HOUR) @LuceneTestCase.SuppressSysoutChecks(bugUrl = "we log a lot on purpose") public class IndexableBinaryStringToolsTests extends LuceneTestCase { - private static int NUM_RANDOM_TESTS; - private static int MAX_RANDOM_BINARY_LENGTH; - private static final String LINE_SEPARATOR = System.lineSeparator(); - - @BeforeClass - public static void beforeClass() throws Exception { - NUM_RANDOM_TESTS = atLeast(200); - MAX_RANDOM_BINARY_LENGTH = atLeast(300); - } - - public void testSingleBinaryRoundTrip() { - byte[] binary = new byte[] { (byte) 0x23, (byte) 0x98, (byte) 0x13, - (byte) 0xE4, (byte) 0x76, (byte) 0x41, (byte) 0xB2, (byte) 0xC9, - (byte) 0x7F, (byte) 0x0A, (byte) 0xA6, (byte) 0xD8 }; - - int encodedLen = IndexableBinaryStringTools.getEncodedLength(binary, 0, - binary.length); - char encoded[] = new char[encodedLen]; - IndexableBinaryStringTools.encode(binary, 0, binary.length, encoded, 0, - encoded.length); - - int decodedLen = IndexableBinaryStringTools.getDecodedLength(encoded, 0, - encoded.length); - byte decoded[] = new byte[decodedLen]; - IndexableBinaryStringTools.decode(encoded, 0, encoded.length, decoded, 0, - decoded.length); - - assertEquals("Round trip decode/decode returned different results:" - + LINE_SEPARATOR + "original: " - + binaryDump(binary, binary.length) - + LINE_SEPARATOR + " encoded: " - + charArrayDump(encoded, encoded.length) - + LINE_SEPARATOR + " decoded: " - + binaryDump(decoded, decoded.length), - binaryDump(binary, binary.length), binaryDump(decoded, decoded.length)); - } - - public void testEncodedSortability() { - byte[] originalArray1 = new byte[MAX_RANDOM_BINARY_LENGTH]; - char[] originalString1 = new char[MAX_RANDOM_BINARY_LENGTH]; - char[] encoded1 = new char[MAX_RANDOM_BINARY_LENGTH * 10]; - byte[] original2 = new byte[MAX_RANDOM_BINARY_LENGTH]; - char[] originalString2 = new char[MAX_RANDOM_BINARY_LENGTH]; - char[] encoded2 = new char[MAX_RANDOM_BINARY_LENGTH * 10]; - - for (int testNum = 0; testNum < NUM_RANDOM_TESTS; ++testNum) { - int numBytes1 = random().nextInt(MAX_RANDOM_BINARY_LENGTH - 1) + 1; // Min == 1 - - for (int byteNum = 0; byteNum < numBytes1; ++byteNum) { - int randomInt = random().nextInt(0x100); - originalArray1[byteNum] = (byte) randomInt; - originalString1[byteNum] = (char) randomInt; - } - - int numBytes2 = random().nextInt(MAX_RANDOM_BINARY_LENGTH - 1) + 1; // Min == 1 - - for (int byteNum = 0; byteNum < numBytes2; ++byteNum) { - int randomInt = random().nextInt(0x100); - original2[byteNum] = (byte) randomInt; - originalString2[byteNum] = (char) randomInt; - } - int originalComparison = new String(originalString1, 0, numBytes1) - .compareTo(new String(originalString2, 0, numBytes2)); - originalComparison = originalComparison < 0 ? -1 - : originalComparison > 0 ? 1 : 0; - - int encodedLen1 = IndexableBinaryStringTools.getEncodedLength( - originalArray1, 0, numBytes1); - if (encodedLen1 > encoded1.length) - encoded1 = new char[ArrayUtil.oversize(encodedLen1, Character.BYTES)]; - IndexableBinaryStringTools.encode(originalArray1, 0, numBytes1, encoded1, - 0, encodedLen1); - - int encodedLen2 = IndexableBinaryStringTools.getEncodedLength(original2, - 0, numBytes2); - if (encodedLen2 > encoded2.length) - encoded2 = new char[ArrayUtil.oversize(encodedLen2, Character.BYTES)]; - IndexableBinaryStringTools.encode(original2, 0, numBytes2, encoded2, 0, - encodedLen2); - - int encodedComparison = new String(encoded1, 0, encodedLen1) - .compareTo(new String(encoded2, 0, encodedLen2)); - encodedComparison = encodedComparison < 0 ? -1 - : encodedComparison > 0 ? 1 : 0; - - assertEquals("Test #" + (testNum + 1) - + ": Original bytes and encoded chars compare differently:" - + LINE_SEPARATOR + " binary 1: " - + binaryDump(originalArray1, numBytes1) - + LINE_SEPARATOR + " binary 2: " - + binaryDump(original2, numBytes2) - + LINE_SEPARATOR + "encoded 1: " - + charArrayDump(encoded1, encodedLen1) - + LINE_SEPARATOR + "encoded 2: " - + charArrayDump(encoded2, encodedLen2) - + LINE_SEPARATOR, originalComparison, - encodedComparison); + private static int NUM_RANDOM_TESTS; + private static int MAX_RANDOM_BINARY_LENGTH; + private static final String LINE_SEPARATOR = System.lineSeparator(); + + @BeforeClass + public static void beforeClass() throws Exception { + NUM_RANDOM_TESTS = atLeast(200); + MAX_RANDOM_BINARY_LENGTH = atLeast(300); + } + + public void testSingleBinaryRoundTrip() { + byte[] binary = new byte[] { + (byte) 0x23, + (byte) 0x98, + (byte) 0x13, + (byte) 0xE4, + (byte) 0x76, + (byte) 0x41, + (byte) 0xB2, + (byte) 0xC9, + (byte) 0x7F, + (byte) 0x0A, + (byte) 0xA6, + (byte) 0xD8 }; + + int encodedLen = IndexableBinaryStringTools.getEncodedLength(binary, 0, binary.length); + char encoded[] = new char[encodedLen]; + IndexableBinaryStringTools.encode(binary, 0, binary.length, encoded, 0, encoded.length); + + int decodedLen = IndexableBinaryStringTools.getDecodedLength(encoded, 0, encoded.length); + byte decoded[] = new byte[decodedLen]; + IndexableBinaryStringTools.decode(encoded, 0, encoded.length, decoded, 0, decoded.length); + + assertEquals( + "Round trip decode/decode returned different results:" + + LINE_SEPARATOR + + "original: " + + binaryDump(binary, binary.length) + + LINE_SEPARATOR + + " encoded: " + + charArrayDump(encoded, encoded.length) + + LINE_SEPARATOR + + " decoded: " + + binaryDump(decoded, decoded.length), + binaryDump(binary, binary.length), + binaryDump(decoded, decoded.length) + ); } - } - - public void testEmptyInput() { - byte[] binary = new byte[0]; - - int encodedLen = IndexableBinaryStringTools.getEncodedLength(binary, 0, - binary.length); - char[] encoded = new char[encodedLen]; - IndexableBinaryStringTools.encode(binary, 0, binary.length, encoded, 0, - encoded.length); - - int decodedLen = IndexableBinaryStringTools.getDecodedLength(encoded, 0, - encoded.length); - byte[] decoded = new byte[decodedLen]; - IndexableBinaryStringTools.decode(encoded, 0, encoded.length, decoded, 0, - decoded.length); - - assertEquals("decoded empty input was not empty", decoded.length, 0); - } - - public void testAllNullInput() { - byte[] binary = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; - - int encodedLen = IndexableBinaryStringTools.getEncodedLength(binary, 0, - binary.length); - char encoded[] = new char[encodedLen]; - IndexableBinaryStringTools.encode(binary, 0, binary.length, encoded, 0, - encoded.length); - - int decodedLen = IndexableBinaryStringTools.getDecodedLength(encoded, 0, - encoded.length); - byte[] decoded = new byte[decodedLen]; - IndexableBinaryStringTools.decode(encoded, 0, encoded.length, decoded, 0, - decoded.length); - - assertEquals("Round trip decode/decode returned different results:" - + LINE_SEPARATOR + " original: " - + binaryDump(binary, binary.length) - + LINE_SEPARATOR + "decodedBuf: " - + binaryDump(decoded, decoded.length), - binaryDump(binary, binary.length), binaryDump(decoded, decoded.length)); - } - - public void testRandomBinaryRoundTrip() { - byte[] binary = new byte[MAX_RANDOM_BINARY_LENGTH]; - char[] encoded = new char[MAX_RANDOM_BINARY_LENGTH * 10]; - byte[] decoded = new byte[MAX_RANDOM_BINARY_LENGTH]; - for (int testNum = 0; testNum < NUM_RANDOM_TESTS; ++testNum) { - int numBytes = random().nextInt(MAX_RANDOM_BINARY_LENGTH - 1) + 1; // Min == 1 - - for (int byteNum = 0; byteNum < numBytes; ++byteNum) { - binary[byteNum] = (byte) random().nextInt(0x100); - } - - int encodedLen = IndexableBinaryStringTools.getEncodedLength(binary, 0, - numBytes); - if (encoded.length < encodedLen) - encoded = new char[ArrayUtil.oversize(encodedLen, Character.BYTES)]; - IndexableBinaryStringTools.encode(binary, 0, numBytes, encoded, 0, - encodedLen); - - int decodedLen = IndexableBinaryStringTools.getDecodedLength(encoded, 0, - encodedLen); - IndexableBinaryStringTools.decode(encoded, 0, encodedLen, decoded, 0, - decodedLen); - - assertEquals("Test #" + (testNum + 1) - + ": Round trip decode/decode returned different results:" - + LINE_SEPARATOR + " original: " - + binaryDump(binary, numBytes) + LINE_SEPARATOR - + "encodedBuf: " + charArrayDump(encoded, encodedLen) - + LINE_SEPARATOR + "decodedBuf: " - + binaryDump(decoded, decodedLen), binaryDump(binary, numBytes), - binaryDump(decoded, decodedLen)); + + public void testEncodedSortability() { + byte[] originalArray1 = new byte[MAX_RANDOM_BINARY_LENGTH]; + char[] originalString1 = new char[MAX_RANDOM_BINARY_LENGTH]; + char[] encoded1 = new char[MAX_RANDOM_BINARY_LENGTH * 10]; + byte[] original2 = new byte[MAX_RANDOM_BINARY_LENGTH]; + char[] originalString2 = new char[MAX_RANDOM_BINARY_LENGTH]; + char[] encoded2 = new char[MAX_RANDOM_BINARY_LENGTH * 10]; + + for (int testNum = 0; testNum < NUM_RANDOM_TESTS; ++testNum) { + int numBytes1 = random().nextInt(MAX_RANDOM_BINARY_LENGTH - 1) + 1; // Min == 1 + + for (int byteNum = 0; byteNum < numBytes1; ++byteNum) { + int randomInt = random().nextInt(0x100); + originalArray1[byteNum] = (byte) randomInt; + originalString1[byteNum] = (char) randomInt; + } + + int numBytes2 = random().nextInt(MAX_RANDOM_BINARY_LENGTH - 1) + 1; // Min == 1 + + for (int byteNum = 0; byteNum < numBytes2; ++byteNum) { + int randomInt = random().nextInt(0x100); + original2[byteNum] = (byte) randomInt; + originalString2[byteNum] = (char) randomInt; + } + int originalComparison = new String(originalString1, 0, numBytes1).compareTo(new String(originalString2, 0, numBytes2)); + originalComparison = originalComparison < 0 ? -1 : originalComparison > 0 ? 1 : 0; + + int encodedLen1 = IndexableBinaryStringTools.getEncodedLength(originalArray1, 0, numBytes1); + if (encodedLen1 > encoded1.length) encoded1 = new char[ArrayUtil.oversize(encodedLen1, Character.BYTES)]; + IndexableBinaryStringTools.encode(originalArray1, 0, numBytes1, encoded1, 0, encodedLen1); + + int encodedLen2 = IndexableBinaryStringTools.getEncodedLength(original2, 0, numBytes2); + if (encodedLen2 > encoded2.length) encoded2 = new char[ArrayUtil.oversize(encodedLen2, Character.BYTES)]; + IndexableBinaryStringTools.encode(original2, 0, numBytes2, encoded2, 0, encodedLen2); + + int encodedComparison = new String(encoded1, 0, encodedLen1).compareTo(new String(encoded2, 0, encodedLen2)); + encodedComparison = encodedComparison < 0 ? -1 : encodedComparison > 0 ? 1 : 0; + + assertEquals( + "Test #" + + (testNum + 1) + + ": Original bytes and encoded chars compare differently:" + + LINE_SEPARATOR + + " binary 1: " + + binaryDump(originalArray1, numBytes1) + + LINE_SEPARATOR + + " binary 2: " + + binaryDump(original2, numBytes2) + + LINE_SEPARATOR + + "encoded 1: " + + charArrayDump(encoded1, encodedLen1) + + LINE_SEPARATOR + + "encoded 2: " + + charArrayDump(encoded2, encodedLen2) + + LINE_SEPARATOR, + originalComparison, + encodedComparison + ); + } } - } - - public String binaryDump(byte[] binary, int numBytes) { - StringBuilder buf = new StringBuilder(); - for (int byteNum = 0 ; byteNum < numBytes ; ++byteNum) { - String hex = Integer.toHexString(binary[byteNum] & 0xFF); - if (hex.length() == 1) { - buf.append('0'); - } - buf.append(hex.toUpperCase(Locale.ROOT)); - if (byteNum < numBytes - 1) { - buf.append(' '); - } + + public void testEmptyInput() { + byte[] binary = new byte[0]; + + int encodedLen = IndexableBinaryStringTools.getEncodedLength(binary, 0, binary.length); + char[] encoded = new char[encodedLen]; + IndexableBinaryStringTools.encode(binary, 0, binary.length, encoded, 0, encoded.length); + + int decodedLen = IndexableBinaryStringTools.getDecodedLength(encoded, 0, encoded.length); + byte[] decoded = new byte[decodedLen]; + IndexableBinaryStringTools.decode(encoded, 0, encoded.length, decoded, 0, decoded.length); + + assertEquals("decoded empty input was not empty", decoded.length, 0); } - return buf.toString(); - } - - public String charArrayDump(char[] charArray, int numBytes) { - StringBuilder buf = new StringBuilder(); - for (int charNum = 0 ; charNum < numBytes ; ++charNum) { - String hex = Integer.toHexString(charArray[charNum]); - for (int digit = 0 ; digit < 4 - hex.length() ; ++digit) { - buf.append('0'); - } - buf.append(hex.toUpperCase(Locale.ROOT)); - if (charNum < numBytes - 1) { - buf.append(' '); - } + + public void testAllNullInput() { + byte[] binary = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; + + int encodedLen = IndexableBinaryStringTools.getEncodedLength(binary, 0, binary.length); + char encoded[] = new char[encodedLen]; + IndexableBinaryStringTools.encode(binary, 0, binary.length, encoded, 0, encoded.length); + + int decodedLen = IndexableBinaryStringTools.getDecodedLength(encoded, 0, encoded.length); + byte[] decoded = new byte[decodedLen]; + IndexableBinaryStringTools.decode(encoded, 0, encoded.length, decoded, 0, decoded.length); + + assertEquals( + "Round trip decode/decode returned different results:" + + LINE_SEPARATOR + + " original: " + + binaryDump(binary, binary.length) + + LINE_SEPARATOR + + "decodedBuf: " + + binaryDump(decoded, decoded.length), + binaryDump(binary, binary.length), + binaryDump(decoded, decoded.length) + ); + } + + public void testRandomBinaryRoundTrip() { + byte[] binary = new byte[MAX_RANDOM_BINARY_LENGTH]; + char[] encoded = new char[MAX_RANDOM_BINARY_LENGTH * 10]; + byte[] decoded = new byte[MAX_RANDOM_BINARY_LENGTH]; + for (int testNum = 0; testNum < NUM_RANDOM_TESTS; ++testNum) { + int numBytes = random().nextInt(MAX_RANDOM_BINARY_LENGTH - 1) + 1; // Min == 1 + + for (int byteNum = 0; byteNum < numBytes; ++byteNum) { + binary[byteNum] = (byte) random().nextInt(0x100); + } + + int encodedLen = IndexableBinaryStringTools.getEncodedLength(binary, 0, numBytes); + if (encoded.length < encodedLen) encoded = new char[ArrayUtil.oversize(encodedLen, Character.BYTES)]; + IndexableBinaryStringTools.encode(binary, 0, numBytes, encoded, 0, encodedLen); + + int decodedLen = IndexableBinaryStringTools.getDecodedLength(encoded, 0, encodedLen); + IndexableBinaryStringTools.decode(encoded, 0, encodedLen, decoded, 0, decodedLen); + + assertEquals( + "Test #" + + (testNum + 1) + + ": Round trip decode/decode returned different results:" + + LINE_SEPARATOR + + " original: " + + binaryDump(binary, numBytes) + + LINE_SEPARATOR + + "encodedBuf: " + + charArrayDump(encoded, encodedLen) + + LINE_SEPARATOR + + "decodedBuf: " + + binaryDump(decoded, decodedLen), + binaryDump(binary, numBytes), + binaryDump(decoded, decodedLen) + ); + } + } + + public String binaryDump(byte[] binary, int numBytes) { + StringBuilder buf = new StringBuilder(); + for (int byteNum = 0; byteNum < numBytes; ++byteNum) { + String hex = Integer.toHexString(binary[byteNum] & 0xFF); + if (hex.length() == 1) { + buf.append('0'); + } + buf.append(hex.toUpperCase(Locale.ROOT)); + if (byteNum < numBytes - 1) { + buf.append(' '); + } + } + return buf.toString(); + } + + public String charArrayDump(char[] charArray, int numBytes) { + StringBuilder buf = new StringBuilder(); + for (int charNum = 0; charNum < numBytes; ++charNum) { + String hex = Integer.toHexString(charArray[charNum]); + for (int digit = 0; digit < 4 - hex.length(); ++digit) { + buf.append('0'); + } + buf.append(hex.toUpperCase(Locale.ROOT)); + if (charNum < numBytes - 1) { + buf.append(' '); + } + } + return buf.toString(); } - return buf.toString(); - } } diff --git a/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/SimpleIcuAnalysisTests.java b/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/SimpleIcuAnalysisTests.java index 1fa2696c46d95..af4e53b1e38aa 100644 --- a/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/SimpleIcuAnalysisTests.java +++ b/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/SimpleIcuAnalysisTests.java @@ -13,13 +13,6 @@ import org.elasticsearch.index.analysis.CharFilterFactory; import org.elasticsearch.index.analysis.TokenFilterFactory; import org.elasticsearch.index.analysis.TokenizerFactory; -import org.elasticsearch.plugin.analysis.icu.IcuCollationTokenFilterFactory; -import org.elasticsearch.plugin.analysis.icu.IcuFoldingTokenFilterFactory; -import org.elasticsearch.plugin.analysis.icu.IcuNormalizerCharFilterFactory; -import org.elasticsearch.plugin.analysis.icu.IcuNormalizerTokenFilterFactory; -import org.elasticsearch.plugin.analysis.icu.IcuTokenizerFactory; -import org.elasticsearch.plugin.analysis.icu.IcuTransformTokenFilterFactory; -import org.elasticsearch.plugin.analysis.icu.AnalysisICUPlugin; import org.elasticsearch.test.ESTestCase; import java.io.IOException; diff --git a/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/SimpleIcuCollationTokenFilterTests.java b/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/SimpleIcuCollationTokenFilterTests.java index b3d4587940fc6..9e88371a681f6 100644 --- a/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/SimpleIcuCollationTokenFilterTests.java +++ b/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/SimpleIcuCollationTokenFilterTests.java @@ -11,6 +11,7 @@ import com.ibm.icu.text.Collator; import com.ibm.icu.text.RuleBasedCollator; import com.ibm.icu.util.ULocale; + import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.Tokenizer; import org.apache.lucene.analysis.core.KeywordTokenizer; @@ -18,7 +19,6 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.Index; import org.elasticsearch.index.analysis.TokenFilterFactory; -import org.elasticsearch.plugin.analysis.icu.AnalysisICUPlugin; import org.elasticsearch.test.ESTestCase; import java.io.IOException; @@ -33,14 +33,15 @@ public class SimpleIcuCollationTokenFilterTests extends ESTestCase { */ public void testDefaultUsage() throws Exception { Settings settings = Settings.builder() - .put("index.analysis.filter.myCollator.type", "icu_collation") - .put("index.analysis.filter.myCollator.strength", "primary") - .build(); + .put("index.analysis.filter.myCollator.type", "icu_collation") + .put("index.analysis.filter.myCollator.strength", "primary") + .build(); TestAnalysis analysis = createTestAnalysis(new Index("test", "_na_"), settings, new AnalysisICUPlugin()); TokenFilterFactory filterFactory = analysis.tokenFilter.get("myCollator"); assertCollatesToSame(filterFactory, "FOO", "foo"); } + /* * Turkish has some funny casing. * This test shows how you can solve this kind of thing easily with collation. @@ -49,10 +50,10 @@ public void testDefaultUsage() throws Exception { */ public void testBasicUsage() throws Exception { Settings settings = Settings.builder() - .put("index.analysis.filter.myCollator.type", "icu_collation") - .put("index.analysis.filter.myCollator.language", "tr") - .put("index.analysis.filter.myCollator.strength", "primary") - .build(); + .put("index.analysis.filter.myCollator.type", "icu_collation") + .put("index.analysis.filter.myCollator.language", "tr") + .put("index.analysis.filter.myCollator.strength", "primary") + .build(); TestAnalysis analysis = createTestAnalysis(new Index("test", "_na_"), settings, new AnalysisICUPlugin()); TokenFilterFactory filterFactory = analysis.tokenFilter.get("myCollator"); @@ -64,11 +65,11 @@ public void testBasicUsage() throws Exception { */ public void testNormalization() throws IOException { Settings settings = Settings.builder() - .put("index.analysis.filter.myCollator.type", "icu_collation") - .put("index.analysis.filter.myCollator.language", "tr") - .put("index.analysis.filter.myCollator.strength", "primary") - .put("index.analysis.filter.myCollator.decomposition", "canonical") - .build(); + .put("index.analysis.filter.myCollator.type", "icu_collation") + .put("index.analysis.filter.myCollator.language", "tr") + .put("index.analysis.filter.myCollator.strength", "primary") + .put("index.analysis.filter.myCollator.decomposition", "canonical") + .build(); TestAnalysis analysis = createTestAnalysis(new Index("test", "_na_"), settings, new AnalysisICUPlugin()); TokenFilterFactory filterFactory = analysis.tokenFilter.get("myCollator"); @@ -80,11 +81,11 @@ public void testNormalization() throws IOException { */ public void testSecondaryStrength() throws IOException { Settings settings = Settings.builder() - .put("index.analysis.filter.myCollator.type", "icu_collation") - .put("index.analysis.filter.myCollator.language", "en") - .put("index.analysis.filter.myCollator.strength", "secondary") - .put("index.analysis.filter.myCollator.decomposition", "no") - .build(); + .put("index.analysis.filter.myCollator.type", "icu_collation") + .put("index.analysis.filter.myCollator.language", "en") + .put("index.analysis.filter.myCollator.strength", "secondary") + .put("index.analysis.filter.myCollator.decomposition", "no") + .build(); TestAnalysis analysis = createTestAnalysis(new Index("test", "_na_"), settings, new AnalysisICUPlugin()); TokenFilterFactory filterFactory = analysis.tokenFilter.get("myCollator"); @@ -97,11 +98,11 @@ public void testSecondaryStrength() throws IOException { */ public void testIgnorePunctuation() throws IOException { Settings settings = Settings.builder() - .put("index.analysis.filter.myCollator.type", "icu_collation") - .put("index.analysis.filter.myCollator.language", "en") - .put("index.analysis.filter.myCollator.strength", "primary") - .put("index.analysis.filter.myCollator.alternate", "shifted") - .build(); + .put("index.analysis.filter.myCollator.type", "icu_collation") + .put("index.analysis.filter.myCollator.language", "en") + .put("index.analysis.filter.myCollator.strength", "primary") + .put("index.analysis.filter.myCollator.alternate", "shifted") + .build(); TestAnalysis analysis = createTestAnalysis(new Index("test", "_na_"), settings, new AnalysisICUPlugin()); TokenFilterFactory filterFactory = analysis.tokenFilter.get("myCollator"); @@ -114,12 +115,12 @@ public void testIgnorePunctuation() throws IOException { */ public void testIgnoreWhitespace() throws IOException { Settings settings = Settings.builder() - .put("index.analysis.filter.myCollator.type", "icu_collation") - .put("index.analysis.filter.myCollator.language", "en") - .put("index.analysis.filter.myCollator.strength", "primary") - .put("index.analysis.filter.myCollator.alternate", "shifted") - .put("index.analysis.filter.myCollator.variableTop", " ") - .build(); + .put("index.analysis.filter.myCollator.type", "icu_collation") + .put("index.analysis.filter.myCollator.language", "en") + .put("index.analysis.filter.myCollator.strength", "primary") + .put("index.analysis.filter.myCollator.alternate", "shifted") + .put("index.analysis.filter.myCollator.variableTop", " ") + .build(); TestAnalysis analysis = createTestAnalysis(new Index("test", "_na_"), settings, new AnalysisICUPlugin()); TokenFilterFactory filterFactory = analysis.tokenFilter.get("myCollator"); @@ -134,10 +135,10 @@ public void testIgnoreWhitespace() throws IOException { */ public void testNumerics() throws IOException { Settings settings = Settings.builder() - .put("index.analysis.filter.myCollator.type", "icu_collation") - .put("index.analysis.filter.myCollator.language", "en") - .put("index.analysis.filter.myCollator.numeric", "true") - .build(); + .put("index.analysis.filter.myCollator.type", "icu_collation") + .put("index.analysis.filter.myCollator.language", "en") + .put("index.analysis.filter.myCollator.numeric", "true") + .build(); TestAnalysis analysis = createTestAnalysis(new Index("test", "_na_"), settings, new AnalysisICUPlugin()); TokenFilterFactory filterFactory = analysis.tokenFilter.get("myCollator"); @@ -150,11 +151,11 @@ public void testNumerics() throws IOException { */ public void testIgnoreAccentsButNotCase() throws IOException { Settings settings = Settings.builder() - .put("index.analysis.filter.myCollator.type", "icu_collation") - .put("index.analysis.filter.myCollator.language", "en") - .put("index.analysis.filter.myCollator.strength", "primary") - .put("index.analysis.filter.myCollator.caseLevel", "true") - .build(); + .put("index.analysis.filter.myCollator.type", "icu_collation") + .put("index.analysis.filter.myCollator.language", "en") + .put("index.analysis.filter.myCollator.strength", "primary") + .put("index.analysis.filter.myCollator.caseLevel", "true") + .build(); TestAnalysis analysis = createTestAnalysis(new Index("test", "_na_"), settings, new AnalysisICUPlugin()); TokenFilterFactory filterFactory = analysis.tokenFilter.get("myCollator"); @@ -170,11 +171,11 @@ public void testIgnoreAccentsButNotCase() throws IOException { */ public void testUpperCaseFirst() throws IOException { Settings settings = Settings.builder() - .put("index.analysis.filter.myCollator.type", "icu_collation") - .put("index.analysis.filter.myCollator.language", "en") - .put("index.analysis.filter.myCollator.strength", "tertiary") - .put("index.analysis.filter.myCollator.caseFirst", "upper") - .build(); + .put("index.analysis.filter.myCollator.type", "icu_collation") + .put("index.analysis.filter.myCollator.language", "en") + .put("index.analysis.filter.myCollator.strength", "tertiary") + .put("index.analysis.filter.myCollator.caseFirst", "upper") + .build(); TestAnalysis analysis = createTestAnalysis(new Index("test", "_na_"), settings, new AnalysisICUPlugin()); TokenFilterFactory filterFactory = analysis.tokenFilter.get("myCollator"); @@ -190,19 +191,16 @@ public void testUpperCaseFirst() throws IOException { */ public void testCustomRules() throws Exception { RuleBasedCollator baseCollator = (RuleBasedCollator) Collator.getInstance(new ULocale("de_DE")); - String DIN5007_2_tailorings = - "& ae , a\u0308 & AE , A\u0308"+ - "& oe , o\u0308 & OE , O\u0308"+ - "& ue , u\u0308 & UE , u\u0308"; + String DIN5007_2_tailorings = "& ae , a\u0308 & AE , A\u0308" + "& oe , o\u0308 & OE , O\u0308" + "& ue , u\u0308 & UE , u\u0308"; RuleBasedCollator tailoredCollator = new RuleBasedCollator(baseCollator.getRules() + DIN5007_2_tailorings); String tailoredRules = tailoredCollator.getRules(); Settings settings = Settings.builder() - .put("index.analysis.filter.myCollator.type", "icu_collation") - .put("index.analysis.filter.myCollator.rules", tailoredRules) - .put("index.analysis.filter.myCollator.strength", "primary") - .build(); + .put("index.analysis.filter.myCollator.type", "icu_collation") + .put("index.analysis.filter.myCollator.rules", tailoredRules) + .put("index.analysis.filter.myCollator.strength", "primary") + .build(); TestAnalysis analysis = createTestAnalysis(new Index("test", "_na_"), settings, new AnalysisICUPlugin()); TokenFilterFactory filterFactory = analysis.tokenFilter.get("myCollator"); @@ -216,16 +214,15 @@ public void testCustomRules() throws Exception { */ public void testBasicCustomRules() throws Exception { Settings settings = Settings.builder() - .put("index.analysis.filter.myCollator.type", "icu_collation") - .put("index.analysis.filter.myCollator.rules", "&a < g") - .build(); + .put("index.analysis.filter.myCollator.type", "icu_collation") + .put("index.analysis.filter.myCollator.rules", "&a < g") + .build(); TestAnalysis analysis = createTestAnalysis(new Index("test", "_na_"), settings, new AnalysisICUPlugin()); TokenFilterFactory filterFactory = analysis.tokenFilter.get("myCollator"); assertCollation(filterFactory, "green", "bird", -1); } - private void assertCollatesToSame(TokenFilterFactory factory, String string1, String string2) throws IOException { assertCollation(factory, string1, string2, 0); } diff --git a/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/SimpleIcuNormalizerCharFilterTests.java b/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/SimpleIcuNormalizerCharFilterTests.java index 0b5f5c00198e2..0064ee56e5f6f 100644 --- a/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/SimpleIcuNormalizerCharFilterTests.java +++ b/plugins/analysis-icu/src/test/java/org/elasticsearch/plugin/analysis/icu/SimpleIcuNormalizerCharFilterTests.java @@ -9,24 +9,21 @@ package org.elasticsearch.plugin.analysis.icu; import com.ibm.icu.text.Normalizer2; + import org.apache.lucene.analysis.CharFilter; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.Index; import org.elasticsearch.index.analysis.CharFilterFactory; -import org.elasticsearch.plugin.analysis.icu.AnalysisICUPlugin; import org.elasticsearch.test.ESTestCase; import java.io.StringReader; - /** * Test */ public class SimpleIcuNormalizerCharFilterTests extends ESTestCase { public void testDefaultSetting() throws Exception { - Settings settings = Settings.builder() - .put("index.analysis.char_filter.myNormalizerChar.type", "icu_normalizer") - .build(); + Settings settings = Settings.builder().put("index.analysis.char_filter.myNormalizerChar.type", "icu_normalizer").build(); TestAnalysis analysis = createTestAnalysis(new Index("test", "_na_"), settings, new AnalysisICUPlugin()); CharFilterFactory charFilterFactory = analysis.charFilter.get("myNormalizerChar"); diff --git a/plugins/analysis-icu/src/yamlRestTest/java/org/elasticsearch/index/analysis/IcuClientYamlTestSuiteIT.java b/plugins/analysis-icu/src/yamlRestTest/java/org/elasticsearch/index/analysis/IcuClientYamlTestSuiteIT.java index ed1ff95cd4651..3a4ad7e259480 100644 --- a/plugins/analysis-icu/src/yamlRestTest/java/org/elasticsearch/index/analysis/IcuClientYamlTestSuiteIT.java +++ b/plugins/analysis-icu/src/yamlRestTest/java/org/elasticsearch/index/analysis/IcuClientYamlTestSuiteIT.java @@ -25,4 +25,3 @@ public static Iterable parameters() throws Exception { return ESClientYamlSuiteTestCase.createParameters(); } } - diff --git a/plugins/analysis-kuromoji/src/main/java/org/elasticsearch/plugin/analysis/kuromoji/JapaneseStopTokenFilterFactory.java b/plugins/analysis-kuromoji/src/main/java/org/elasticsearch/plugin/analysis/kuromoji/JapaneseStopTokenFilterFactory.java index c2ff2047cff5d..fc368ea9f09e1 100644 --- a/plugins/analysis-kuromoji/src/main/java/org/elasticsearch/plugin/analysis/kuromoji/JapaneseStopTokenFilterFactory.java +++ b/plugins/analysis-kuromoji/src/main/java/org/elasticsearch/plugin/analysis/kuromoji/JapaneseStopTokenFilterFactory.java @@ -8,7 +8,6 @@ package org.elasticsearch.plugin.analysis.kuromoji; - import org.apache.lucene.analysis.CharArraySet; import org.apache.lucene.analysis.StopFilter; import org.apache.lucene.analysis.TokenStream; @@ -38,8 +37,14 @@ public JapaneseStopTokenFilterFactory(IndexSettings indexSettings, Environment e super(indexSettings, name, settings); this.ignoreCase = settings.getAsBoolean("ignore_case", false); this.removeTrailing = settings.getAsBoolean("remove_trailing", true); - this.stopWords = Analysis.parseWords(env, settings, "stopwords", - JapaneseAnalyzer.getDefaultStopSet(), NAMED_STOP_WORDS, ignoreCase); + this.stopWords = Analysis.parseWords( + env, + settings, + "stopwords", + JapaneseAnalyzer.getDefaultStopSet(), + NAMED_STOP_WORDS, + ignoreCase + ); } @Override diff --git a/plugins/analysis-kuromoji/src/main/java/org/elasticsearch/plugin/analysis/kuromoji/KuromojiAnalyzerProvider.java b/plugins/analysis-kuromoji/src/main/java/org/elasticsearch/plugin/analysis/kuromoji/KuromojiAnalyzerProvider.java index 83c5f8559df35..dcdf415e4570e 100644 --- a/plugins/analysis-kuromoji/src/main/java/org/elasticsearch/plugin/analysis/kuromoji/KuromojiAnalyzerProvider.java +++ b/plugins/analysis-kuromoji/src/main/java/org/elasticsearch/plugin/analysis/kuromoji/KuromojiAnalyzerProvider.java @@ -37,5 +37,4 @@ public JapaneseAnalyzer get() { return this.analyzer; } - } diff --git a/plugins/analysis-kuromoji/src/main/java/org/elasticsearch/plugin/analysis/kuromoji/KuromojiTokenizerFactory.java b/plugins/analysis-kuromoji/src/main/java/org/elasticsearch/plugin/analysis/kuromoji/KuromojiTokenizerFactory.java index 8425cdcabd635..12efa4abc111e 100644 --- a/plugins/analysis-kuromoji/src/main/java/org/elasticsearch/plugin/analysis/kuromoji/KuromojiTokenizerFactory.java +++ b/plugins/analysis-kuromoji/src/main/java/org/elasticsearch/plugin/analysis/kuromoji/KuromojiTokenizerFactory.java @@ -54,8 +54,9 @@ public KuromojiTokenizerFactory(IndexSettings indexSettings, Environment env, St public static UserDictionary getUserDictionary(Environment env, Settings settings) { if (settings.get(USER_DICT_PATH_OPTION) != null && settings.get(USER_DICT_RULES_OPTION) != null) { - throw new IllegalArgumentException("It is not allowed to use [" + USER_DICT_PATH_OPTION + "] in conjunction" + - " with [" + USER_DICT_RULES_OPTION + "]"); + throw new IllegalArgumentException( + "It is not allowed to use [" + USER_DICT_PATH_OPTION + "] in conjunction" + " with [" + USER_DICT_RULES_OPTION + "]" + ); } try { List ruleList = Analysis.getWordList(env, settings, USER_DICT_PATH_OPTION, USER_DICT_RULES_OPTION, false); @@ -69,11 +70,12 @@ public static UserDictionary getUserDictionary(Environment env, Settings setting if (line.startsWith("#") == false) { String[] values = CSVUtil.parse(line); if (dup.add(values[0]) == false) { - throw new IllegalArgumentException("Found duplicate term [" + values[0] + "] in user dictionary " + - "at line [" + lineNum + "]"); + throw new IllegalArgumentException( + "Found duplicate term [" + values[0] + "] in user dictionary " + "at line [" + lineNum + "]" + ); } } - ++ lineNum; + ++lineNum; } StringBuilder sb = new StringBuilder(); for (String line : ruleList) { diff --git a/plugins/analysis-kuromoji/src/test/java/org/elasticsearch/plugin/analysis/kuromoji/AnalysisKuromojiFactoryTests.java b/plugins/analysis-kuromoji/src/test/java/org/elasticsearch/plugin/analysis/kuromoji/AnalysisKuromojiFactoryTests.java index 63f6f94f79033..07dffe0207ba8 100644 --- a/plugins/analysis-kuromoji/src/test/java/org/elasticsearch/plugin/analysis/kuromoji/AnalysisKuromojiFactoryTests.java +++ b/plugins/analysis-kuromoji/src/test/java/org/elasticsearch/plugin/analysis/kuromoji/AnalysisKuromojiFactoryTests.java @@ -10,13 +10,6 @@ import org.apache.lucene.analysis.ja.JapaneseTokenizerFactory; import org.elasticsearch.indices.analysis.AnalysisFactoryTestCase; -import org.elasticsearch.plugin.analysis.kuromoji.AnalysisKuromojiPlugin; -import org.elasticsearch.plugin.analysis.kuromoji.KuromojiBaseFormFilterFactory; -import org.elasticsearch.plugin.analysis.kuromoji.KuromojiIterationMarkCharFilterFactory; -import org.elasticsearch.plugin.analysis.kuromoji.KuromojiKatakanaStemmerFactory; -import org.elasticsearch.plugin.analysis.kuromoji.KuromojiNumberFilterFactory; -import org.elasticsearch.plugin.analysis.kuromoji.KuromojiPartOfSpeechFilterFactory; -import org.elasticsearch.plugin.analysis.kuromoji.KuromojiReadingFormFilterFactory; import java.util.HashMap; import java.util.Map; diff --git a/plugins/analysis-kuromoji/src/test/java/org/elasticsearch/plugin/analysis/kuromoji/KuromojiAnalysisTests.java b/plugins/analysis-kuromoji/src/test/java/org/elasticsearch/plugin/analysis/kuromoji/KuromojiAnalysisTests.java index e85628b1e8d9a..1bf12510f136e 100644 --- a/plugins/analysis-kuromoji/src/test/java/org/elasticsearch/plugin/analysis/kuromoji/KuromojiAnalysisTests.java +++ b/plugins/analysis-kuromoji/src/test/java/org/elasticsearch/plugin/analysis/kuromoji/KuromojiAnalysisTests.java @@ -85,7 +85,7 @@ public void testBaseFormFilterFactory() throws IOException { TokenFilterFactory tokenFilter = analysis.tokenFilter.get("kuromoji_pos"); assertThat(tokenFilter, instanceOf(KuromojiPartOfSpeechFilterFactory.class)); String source = "私は制限スピードを超える。"; - String[] expected = new String[]{"私", "は", "制限", "スピード", "を"}; + String[] expected = new String[] { "私", "は", "制限", "スピード", "を" }; Tokenizer tokenizer = new JapaneseTokenizer(null, true, JapaneseTokenizer.Mode.SEARCH); tokenizer.setReader(new StringReader(source)); assertSimpleTSOutput(tokenFilter.create(tokenizer), expected); @@ -98,7 +98,7 @@ public void testPartOfSpeechFilter() throws IOException { assertThat(tokenFilter, instanceOf(KuromojiPartOfSpeechFilterFactory.class)); String source = "寿司がおいしいね"; - String[] expected_tokens = new String[]{"寿司", "おいしい"}; + String[] expected_tokens = new String[] { "寿司", "おいしい" }; Tokenizer tokenizer = new JapaneseTokenizer(null, true, JapaneseTokenizer.Mode.SEARCH); tokenizer.setReader(new StringReader(source)); @@ -111,7 +111,7 @@ public void testReadingFormFilterFactory() throws IOException { TokenFilterFactory tokenFilter = analysis.tokenFilter.get("kuromoji_rf"); assertThat(tokenFilter, instanceOf(KuromojiReadingFormFilterFactory.class)); String source = "今夜はロバート先生と話した"; - String[] expected_tokens_romaji = new String[]{"kon'ya", "ha", "robato", "sensei", "to", "hanashi", "ta"}; + String[] expected_tokens_romaji = new String[] { "kon'ya", "ha", "robato", "sensei", "to", "hanashi", "ta" }; Tokenizer tokenizer = new JapaneseTokenizer(null, true, JapaneseTokenizer.Mode.SEARCH); tokenizer.setReader(new StringReader(source)); @@ -120,7 +120,7 @@ public void testReadingFormFilterFactory() throws IOException { tokenizer = new JapaneseTokenizer(null, true, JapaneseTokenizer.Mode.SEARCH); tokenizer.setReader(new StringReader(source)); - String[] expected_tokens_katakana = new String[]{"コンヤ", "ハ", "ロバート", "センセイ", "ト", "ハナシ", "タ"}; + String[] expected_tokens_katakana = new String[] { "コンヤ", "ハ", "ロバート", "センセイ", "ト", "ハナシ", "タ" }; tokenFilter = analysis.tokenFilter.get("kuromoji_readingform"); assertThat(tokenFilter, instanceOf(KuromojiReadingFormFilterFactory.class)); assertSimpleTSOutput(tokenFilter.create(tokenizer), expected_tokens_katakana); @@ -138,7 +138,21 @@ public void testKatakanaStemFilter() throws IOException { // パーティー should be stemmed by default // (min len) コピー should not be stemmed String[] expected_tokens_katakana = new String[] { - "明後日", "パーティ", "に", "行く", "予定", "が", "ある", "図書館", "で", "資料", "を", "コピー", "し", "まし", "た"}; + "明後日", + "パーティ", + "に", + "行く", + "予定", + "が", + "ある", + "図書館", + "で", + "資料", + "を", + "コピー", + "し", + "まし", + "た" }; assertSimpleTSOutput(tokenFilter.create(tokenizer), expected_tokens_katakana); tokenFilter = analysis.tokenFilter.get("kuromoji_ks"); @@ -149,7 +163,21 @@ public void testKatakanaStemFilter() throws IOException { // パーティー should not be stemmed since min len == 6 // コピー should not be stemmed expected_tokens_katakana = new String[] { - "明後日", "パーティー", "に", "行く", "予定", "が", "ある", "図書館", "で", "資料", "を", "コピー", "し", "まし", "た"}; + "明後日", + "パーティー", + "に", + "行く", + "予定", + "が", + "ある", + "図書館", + "で", + "資料", + "を", + "コピー", + "し", + "まし", + "た" }; assertSimpleTSOutput(tokenFilter.create(tokenizer), expected_tokens_katakana); } @@ -191,7 +219,7 @@ public void testJapaneseStopFilterFactory() throws IOException { TokenFilterFactory tokenFilter = analysis.tokenFilter.get("ja_stop"); assertThat(tokenFilter, instanceOf(JapaneseStopTokenFilterFactory.class)); String source = "私は制限スピードを超える。"; - String[] expected = new String[]{"私", "制限", "超える"}; + String[] expected = new String[] { "私", "制限", "超える" }; Tokenizer tokenizer = new JapaneseTokenizer(null, true, JapaneseTokenizer.Mode.SEARCH); tokenizer.setReader(new StringReader(source)); assertSimpleTSOutput(tokenFilter.create(tokenizer), expected); @@ -215,8 +243,7 @@ private static TestAnalysis createTestAnalysis() throws IOException { return createTestAnalysis(new Index("test", "_na_"), nodeSettings, settings, new AnalysisKuromojiPlugin()); } - public static void assertSimpleTSOutput(TokenStream stream, - String[] expected) throws IOException { + public static void assertSimpleTSOutput(TokenStream stream, String[] expected) throws IOException { stream.reset(); CharTermAttribute termAttr = stream.getAttribute(CharTermAttribute.class); assertThat(termAttr, notNullValue()); @@ -228,8 +255,7 @@ public static void assertSimpleTSOutput(TokenStream stream, assertThat("not all tokens produced", i, equalTo(expected.length)); } - private void assertCharFilterEquals(Reader filtered, - String expected) throws IOException { + private void assertCharFilterEquals(Reader filtered, String expected) throws IOException { String actual = readFully(filtered); assertThat(actual, equalTo(expected)); } @@ -237,8 +263,8 @@ private void assertCharFilterEquals(Reader filtered, private String readFully(Reader reader) throws IOException { StringBuilder buffer = new StringBuilder(); int ch; - while((ch = reader.read()) != -1){ - buffer.append((char)ch); + while ((ch = reader.read()) != -1) { + buffer.append((char) ch); } return buffer.toString(); } @@ -247,7 +273,7 @@ public void testKuromojiUserDict() throws IOException { TestAnalysis analysis = createTestAnalysis(); TokenizerFactory tokenizerFactory = analysis.tokenizer.get("kuromoji_user_dict"); String source = "私は制限スピードを超える。"; - String[] expected = new String[]{"私", "は", "制限スピード", "を", "超える"}; + String[] expected = new String[] { "私", "は", "制限スピード", "を", "超える" }; Tokenizer tokenizer = tokenizerFactory.create(); tokenizer.setReader(new StringReader(source)); @@ -265,7 +291,7 @@ public void testNbestCost() throws IOException { TestAnalysis analysis = createTestAnalysis(); TokenizerFactory tokenizerFactory = analysis.tokenizer.get("kuromoji_nbest_cost"); String source = "鳩山積み"; - String[] expected = new String[] {"鳩", "鳩山", "山積み", "積み"}; + String[] expected = new String[] { "鳩", "鳩山", "山積み", "積み" }; Tokenizer tokenizer = tokenizerFactory.create(); tokenizer.setReader(new StringReader(source)); @@ -276,7 +302,7 @@ public void testNbestExample() throws IOException { TestAnalysis analysis = createTestAnalysis(); TokenizerFactory tokenizerFactory = analysis.tokenizer.get("kuromoji_nbest_examples"); String source = "鳩山積み"; - String[] expected = new String[] {"鳩", "鳩山", "山積み", "積み"}; + String[] expected = new String[] { "鳩", "鳩山", "山積み", "積み" }; Tokenizer tokenizer = tokenizerFactory.create(); tokenizer.setReader(new StringReader(source)); @@ -287,7 +313,7 @@ public void testNbestBothOptions() throws IOException { TestAnalysis analysis = createTestAnalysis(); TokenizerFactory tokenizerFactory = analysis.tokenizer.get("kuromoji_nbest_both"); String source = "鳩山積み"; - String[] expected = new String[] {"鳩", "鳩山", "山積み", "積み"}; + String[] expected = new String[] { "鳩", "鳩山", "山積み", "積み" }; Tokenizer tokenizer = tokenizerFactory.create(); tokenizer.setReader(new StringReader(source)); @@ -300,7 +326,7 @@ public void testNumberFilterFactory() throws Exception { TokenFilterFactory tokenFilter = analysis.tokenFilter.get("kuromoji_number"); assertThat(tokenFilter, instanceOf(KuromojiNumberFilterFactory.class)); String source = "本日十万二千五百円のワインを買った"; - String[] expected = new String[]{"本日", "102500", "円", "の", "ワイン", "を", "買っ", "た"}; + String[] expected = new String[] { "本日", "102500", "円", "の", "ワイン", "を", "買っ", "た" }; Tokenizer tokenizer = new JapaneseTokenizer(null, true, JapaneseTokenizer.Mode.SEARCH); tokenizer.setReader(new StringReader(source)); assertSimpleTSOutput(tokenFilter.create(tokenizer), expected); @@ -314,11 +340,11 @@ public void testKuromojiAnalyzerUserDict() throws Exception { TestAnalysis analysis = createTestAnalysis(settings); Analyzer analyzer = analysis.indexAnalyzers.get("my_analyzer"); try (TokenStream stream = analyzer.tokenStream("", "制限スピード")) { - assertTokenStreamContents(stream, new String[]{"制限スピード"}); + assertTokenStreamContents(stream, new String[] { "制限スピード" }); } try (TokenStream stream = analyzer.tokenStream("", "c++world")) { - assertTokenStreamContents(stream, new String[]{"c++", "world"}); + assertTokenStreamContents(stream, new String[] { "c++", "world" }); } } @@ -329,15 +355,22 @@ public void testKuromojiAnalyzerInvalidUserDictOption() throws Exception { .putList("index.analysis.analyzer.my_analyzer.user_dictionary_rules", "c++,c++,w,w") .build(); IllegalArgumentException exc = expectThrows(IllegalArgumentException.class, () -> createTestAnalysis(settings)); - assertThat(exc.getMessage(), containsString("It is not allowed to use [user_dictionary] in conjunction " + - "with [user_dictionary_rules]")); + assertThat( + exc.getMessage(), + containsString("It is not allowed to use [user_dictionary] in conjunction " + "with [user_dictionary_rules]") + ); } public void testKuromojiAnalyzerDuplicateUserDictRule() throws Exception { Settings settings = Settings.builder() .put("index.analysis.analyzer.my_analyzer.type", "kuromoji") - .putList("index.analysis.analyzer.my_analyzer.user_dictionary_rules", - "c++,c++,w,w", "#comment", "制限スピード,制限スピード,セイゲンスピード,テスト名詞", "制限スピード,制限スピード,セイゲンスピード,テスト名詞") + .putList( + "index.analysis.analyzer.my_analyzer.user_dictionary_rules", + "c++,c++,w,w", + "#comment", + "制限スピード,制限スピード,セイゲンスピード,テスト名詞", + "制限スピード,制限スピード,セイゲンスピード,テスト名詞" + ) .build(); IllegalArgumentException exc = expectThrows(IllegalArgumentException.class, () -> createTestAnalysis(settings)); assertThat(exc.getMessage(), containsString("[制限スピード] in user dictionary at line [3]")); @@ -347,7 +380,7 @@ public void testDiscardCompoundToken() throws Exception { TestAnalysis analysis = createTestAnalysis(); TokenizerFactory tokenizerFactory = analysis.tokenizer.get("kuromoji_discard_compound_token"); String source = "株式会社"; - String[] expected = new String[] {"株式", "会社"}; + String[] expected = new String[] { "株式", "会社" }; Tokenizer tokenizer = tokenizerFactory.create(); tokenizer.setReader(new StringReader(source)); diff --git a/plugins/analysis-kuromoji/src/yamlRestTest/java/org/elasticsearch/index/analysis/KuromojiClientYamlTestSuiteIT.java b/plugins/analysis-kuromoji/src/yamlRestTest/java/org/elasticsearch/index/analysis/KuromojiClientYamlTestSuiteIT.java index 0462e32f0bbbd..41e2e420abb05 100644 --- a/plugins/analysis-kuromoji/src/yamlRestTest/java/org/elasticsearch/index/analysis/KuromojiClientYamlTestSuiteIT.java +++ b/plugins/analysis-kuromoji/src/yamlRestTest/java/org/elasticsearch/index/analysis/KuromojiClientYamlTestSuiteIT.java @@ -25,4 +25,3 @@ public static Iterable parameters() throws Exception { return ESClientYamlSuiteTestCase.createParameters(); } } - diff --git a/plugins/analysis-nori/src/main/java/org/elasticsearch/plugin/analysis/nori/NoriAnalyzerProvider.java b/plugins/analysis-nori/src/main/java/org/elasticsearch/plugin/analysis/nori/NoriAnalyzerProvider.java index 14725c09da150..2b8a796e7610a 100644 --- a/plugins/analysis-nori/src/main/java/org/elasticsearch/plugin/analysis/nori/NoriAnalyzerProvider.java +++ b/plugins/analysis-nori/src/main/java/org/elasticsearch/plugin/analysis/nori/NoriAnalyzerProvider.java @@ -8,21 +8,21 @@ package org.elasticsearch.plugin.analysis.nori; -import java.util.List; -import java.util.Set; import org.apache.lucene.analysis.ko.KoreanAnalyzer; import org.apache.lucene.analysis.ko.KoreanPartOfSpeechStopFilter; import org.apache.lucene.analysis.ko.KoreanTokenizer; -import org.apache.lucene.analysis.ko.dict.UserDictionary; import org.apache.lucene.analysis.ko.POS; +import org.apache.lucene.analysis.ko.dict.UserDictionary; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.env.Environment; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.analysis.AbstractIndexAnalyzerProvider; import org.elasticsearch.index.analysis.Analysis; -import static org.elasticsearch.plugin.analysis.nori.NoriPartOfSpeechStopFilterFactory.resolvePOSList; +import java.util.List; +import java.util.Set; +import static org.elasticsearch.plugin.analysis.nori.NoriPartOfSpeechStopFilterFactory.resolvePOSList; public class NoriAnalyzerProvider extends AbstractIndexAnalyzerProvider { private final KoreanAnalyzer analyzer; @@ -41,5 +41,4 @@ public KoreanAnalyzer get() { return analyzer; } - } diff --git a/plugins/analysis-nori/src/main/java/org/elasticsearch/plugin/analysis/nori/NoriPartOfSpeechStopFilterFactory.java b/plugins/analysis-nori/src/main/java/org/elasticsearch/plugin/analysis/nori/NoriPartOfSpeechStopFilterFactory.java index 43fa8fdd3cab5..e55c8ed4dfb14 100644 --- a/plugins/analysis-nori/src/main/java/org/elasticsearch/plugin/analysis/nori/NoriPartOfSpeechStopFilterFactory.java +++ b/plugins/analysis-nori/src/main/java/org/elasticsearch/plugin/analysis/nori/NoriPartOfSpeechStopFilterFactory.java @@ -35,7 +35,6 @@ public TokenStream create(TokenStream tokenStream) { return new KoreanPartOfSpeechStopFilter(tokenStream, stopTags); } - static Set resolvePOSList(List tagList) { Set stopTags = new HashSet<>(); for (String tag : tagList) { diff --git a/plugins/analysis-nori/src/main/java/org/elasticsearch/plugin/analysis/nori/NoriTokenizerFactory.java b/plugins/analysis-nori/src/main/java/org/elasticsearch/plugin/analysis/nori/NoriTokenizerFactory.java index ee9ec8c8c692f..5c030e90d4482 100644 --- a/plugins/analysis-nori/src/main/java/org/elasticsearch/plugin/analysis/nori/NoriTokenizerFactory.java +++ b/plugins/analysis-nori/src/main/java/org/elasticsearch/plugin/analysis/nori/NoriTokenizerFactory.java @@ -41,8 +41,9 @@ public NoriTokenizerFactory(IndexSettings indexSettings, Environment env, String public static UserDictionary getUserDictionary(Environment env, Settings settings) { if (settings.get(USER_DICT_PATH_OPTION) != null && settings.get(USER_DICT_RULES_OPTION) != null) { - throw new IllegalArgumentException("It is not allowed to use [" + USER_DICT_PATH_OPTION + "] in conjunction" + - " with [" + USER_DICT_RULES_OPTION + "]"); + throw new IllegalArgumentException( + "It is not allowed to use [" + USER_DICT_PATH_OPTION + "] in conjunction" + " with [" + USER_DICT_RULES_OPTION + "]" + ); } List ruleList = Analysis.getWordList(env, settings, USER_DICT_PATH_OPTION, USER_DICT_RULES_OPTION, true); StringBuilder sb = new StringBuilder(); @@ -70,8 +71,13 @@ public static KoreanTokenizer.DecompoundMode getMode(Settings settings) { @Override public Tokenizer create() { - return new KoreanTokenizer(KoreanTokenizer.DEFAULT_TOKEN_ATTRIBUTE_FACTORY, userDictionary, decompoundMode, false, - discardPunctuation); + return new KoreanTokenizer( + KoreanTokenizer.DEFAULT_TOKEN_ATTRIBUTE_FACTORY, + userDictionary, + decompoundMode, + false, + discardPunctuation + ); } } diff --git a/plugins/analysis-nori/src/test/java/org/elasticsearch/plugin/analysis/nori/AnalysisNoriFactoryTests.java b/plugins/analysis-nori/src/test/java/org/elasticsearch/plugin/analysis/nori/AnalysisNoriFactoryTests.java index 85657a4d8a3a6..44ba5e4148115 100644 --- a/plugins/analysis-nori/src/test/java/org/elasticsearch/plugin/analysis/nori/AnalysisNoriFactoryTests.java +++ b/plugins/analysis-nori/src/test/java/org/elasticsearch/plugin/analysis/nori/AnalysisNoriFactoryTests.java @@ -10,10 +10,6 @@ import org.apache.lucene.analysis.ko.KoreanTokenizerFactory; import org.elasticsearch.indices.analysis.AnalysisFactoryTestCase; -import org.elasticsearch.plugin.analysis.nori.AnalysisNoriPlugin; -import org.elasticsearch.plugin.analysis.nori.NoriNumberFilterFactory; -import org.elasticsearch.plugin.analysis.nori.NoriPartOfSpeechStopFilterFactory; -import org.elasticsearch.plugin.analysis.nori.NoriReadingFormFilterFactory; import java.util.HashMap; import java.util.Map; diff --git a/plugins/analysis-nori/src/test/java/org/elasticsearch/plugin/analysis/nori/NoriAnalysisTests.java b/plugins/analysis-nori/src/test/java/org/elasticsearch/plugin/analysis/nori/NoriAnalysisTests.java index f89611f870ae4..994ebffb9af63 100644 --- a/plugins/analysis-nori/src/test/java/org/elasticsearch/plugin/analysis/nori/NoriAnalysisTests.java +++ b/plugins/analysis-nori/src/test/java/org/elasticsearch/plugin/analysis/nori/NoriAnalysisTests.java @@ -63,12 +63,12 @@ public void testNoriAnalyzer() throws Exception { .build(); TestAnalysis analysis = createTestAnalysis(settings); Analyzer analyzer = analysis.indexAnalyzers.get("my_analyzer"); - try (TokenStream stream = analyzer.tokenStream("", "여섯 용이" )) { - assertTokenStreamContents(stream, new String[] {"용", "이"}); + try (TokenStream stream = analyzer.tokenStream("", "여섯 용이")) { + assertTokenStreamContents(stream, new String[] { "용", "이" }); } try (TokenStream stream = analyzer.tokenStream("", "가늠표")) { - assertTokenStreamContents(stream, new String[] {"가늠표", "가늠", "표"}); + assertTokenStreamContents(stream, new String[] { "가늠표", "가늠", "표" }); } } @@ -80,11 +80,11 @@ public void testNoriAnalyzerUserDict() throws Exception { TestAnalysis analysis = createTestAnalysis(settings); Analyzer analyzer = analysis.indexAnalyzers.get("my_analyzer"); try (TokenStream stream = analyzer.tokenStream("", "세종시")) { - assertTokenStreamContents(stream, new String[]{"세종", "시"}); + assertTokenStreamContents(stream, new String[] { "세종", "시" }); } try (TokenStream stream = analyzer.tokenStream("", "c++world")) { - assertTokenStreamContents(stream, new String[]{"c++", "world"}); + assertTokenStreamContents(stream, new String[] { "c++", "world" }); } } @@ -95,12 +95,12 @@ public void testNoriAnalyzerUserDictPath() throws Exception { .build(); TestAnalysis analysis = createTestAnalysis(settings); Analyzer analyzer = analysis.indexAnalyzers.get("my_analyzer"); - try (TokenStream stream = analyzer.tokenStream("", "세종시" )) { - assertTokenStreamContents(stream, new String[] {"세종", "시"}); + try (TokenStream stream = analyzer.tokenStream("", "세종시")) { + assertTokenStreamContents(stream, new String[] { "세종", "시" }); } try (TokenStream stream = analyzer.tokenStream("", "c++world")) { - assertTokenStreamContents(stream, new String[] {"c++", "world"}); + assertTokenStreamContents(stream, new String[] { "c++", "world" }); } } @@ -111,8 +111,10 @@ public void testNoriAnalyzerInvalidUserDictOption() throws Exception { .putList("index.analysis.analyzer.my_analyzer.user_dictionary_rules", "c++", "C샤프", "세종", "세종시 세종 시") .build(); IllegalArgumentException exc = expectThrows(IllegalArgumentException.class, () -> createTestAnalysis(settings)); - assertThat(exc.getMessage(), containsString("It is not allowed to use [user_dictionary] in conjunction " + - "with [user_dictionary_rules]")); + assertThat( + exc.getMessage(), + containsString("It is not allowed to use [user_dictionary] in conjunction " + "with [user_dictionary_rules]") + ); } public void testNoriTokenizer() throws Exception { @@ -123,12 +125,12 @@ public void testNoriTokenizer() throws Exception { TestAnalysis analysis = createTestAnalysis(settings); Tokenizer tokenizer = analysis.tokenizer.get("my_tokenizer").create(); tokenizer.setReader(new StringReader("뿌리가 깊은 나무")); - assertTokenStreamContents(tokenizer, new String[] {"뿌리", "가", "깊", "은", "나무"}); + assertTokenStreamContents(tokenizer, new String[] { "뿌리", "가", "깊", "은", "나무" }); tokenizer.setReader(new StringReader("가늠표")); - assertTokenStreamContents(tokenizer, new String[] {"가늠표", "가늠", "표"}); + assertTokenStreamContents(tokenizer, new String[] { "가늠표", "가늠", "표" }); // discard_punctuation default(true) tokenizer.setReader(new StringReader("3.2개")); - assertTokenStreamContents(tokenizer, new String[] {"3", "2", "개"}); + assertTokenStreamContents(tokenizer, new String[] { "3", "2", "개" }); } public void testNoriTokenizerDiscardPunctuationOptionTrue() throws Exception { @@ -136,7 +138,7 @@ public void testNoriTokenizerDiscardPunctuationOptionTrue() throws Exception { TestAnalysis analysis = createTestAnalysis(settings); Tokenizer tokenizer = analysis.tokenizer.get("my_tokenizer").create(); tokenizer.setReader(new StringReader("3.2개")); - assertTokenStreamContents(tokenizer, new String[] {"3", "2", "개"}); + assertTokenStreamContents(tokenizer, new String[] { "3", "2", "개" }); } public void testNoriTokenizerDiscardPunctuationOptionFalse() throws Exception { @@ -144,15 +146,14 @@ public void testNoriTokenizerDiscardPunctuationOptionFalse() throws Exception { TestAnalysis analysis = createTestAnalysis(settings); Tokenizer tokenizer = analysis.tokenizer.get("my_tokenizer").create(); tokenizer.setReader(new StringReader("3.2개")); - assertTokenStreamContents(tokenizer, new String[] {"3", ".", "2", "개"}); + assertTokenStreamContents(tokenizer, new String[] { "3", ".", "2", "개" }); } public void testNoriTokenizerInvalidDiscardPunctuationOption() { String wrongOption = "wrong"; Settings settings = createDiscardPunctuationOption(wrongOption); IllegalArgumentException exc = expectThrows(IllegalArgumentException.class, () -> createTestAnalysis(settings)); - assertThat(exc.getMessage(), containsString("Failed to parse value [" + wrongOption - + "] as only [true] or [false] are allowed.")); + assertThat(exc.getMessage(), containsString("Failed to parse value [" + wrongOption + "] as only [true] or [false] are allowed.")); } public void testNoriPartOfSpeech() throws IOException { @@ -165,7 +166,7 @@ public void testNoriPartOfSpeech() throws IOException { Tokenizer tokenizer = new KoreanTokenizer(); tokenizer.setReader(new StringReader("여섯 용이")); TokenStream stream = factory.create(tokenizer); - assertTokenStreamContents(stream, new String[] {"용", "이"}); + assertTokenStreamContents(stream, new String[] { "용", "이" }); } public void testNoriReadingForm() throws IOException { @@ -179,7 +180,7 @@ public void testNoriReadingForm() throws IOException { Tokenizer tokenizer = new KoreanTokenizer(); tokenizer.setReader(new StringReader("鄕歌")); TokenStream stream = factory.create(tokenizer); - assertTokenStreamContents(stream, new String[] {"향가"}); + assertTokenStreamContents(stream, new String[] { "향가" }); } public void testNoriNumber() throws IOException { @@ -193,7 +194,7 @@ public void testNoriNumber() throws IOException { Tokenizer tokenizer = new KoreanTokenizer(); tokenizer.setReader(new StringReader("오늘 십만이천오백원짜리 와인 구입")); TokenStream stream = factory.create(tokenizer); - assertTokenStreamContents(stream, new String[] {"오늘", "102500", "원", "짜리", "와인", "구입"}); + assertTokenStreamContents(stream, new String[] { "오늘", "102500", "원", "짜리", "와인", "구입" }); } private Settings createDiscardPunctuationOption(String option) { diff --git a/plugins/analysis-nori/src/yamlRestTest/java/org/elasticsearch/index/analysis/NoriClientYamlTestSuiteIT.java b/plugins/analysis-nori/src/yamlRestTest/java/org/elasticsearch/index/analysis/NoriClientYamlTestSuiteIT.java index 470589b2dfc1a..4c7238ebc9e2e 100644 --- a/plugins/analysis-nori/src/yamlRestTest/java/org/elasticsearch/index/analysis/NoriClientYamlTestSuiteIT.java +++ b/plugins/analysis-nori/src/yamlRestTest/java/org/elasticsearch/index/analysis/NoriClientYamlTestSuiteIT.java @@ -25,4 +25,3 @@ public static Iterable parameters() throws Exception { return ESClientYamlSuiteTestCase.createParameters(); } } - diff --git a/plugins/analysis-phonetic/src/main/java/org/elasticsearch/plugin/analysis/phonetic/AnalysisPhoneticPlugin.java b/plugins/analysis-phonetic/src/main/java/org/elasticsearch/plugin/analysis/phonetic/AnalysisPhoneticPlugin.java index 1365e1cd40ee8..47562ea372b34 100644 --- a/plugins/analysis-phonetic/src/main/java/org/elasticsearch/plugin/analysis/phonetic/AnalysisPhoneticPlugin.java +++ b/plugins/analysis-phonetic/src/main/java/org/elasticsearch/plugin/analysis/phonetic/AnalysisPhoneticPlugin.java @@ -23,4 +23,3 @@ public Map> getTokenFilters() { return singletonMap("phonetic", PhoneticTokenFilterFactory::new); } } - diff --git a/plugins/analysis-phonetic/src/main/java/org/elasticsearch/plugin/analysis/phonetic/HaasePhonetik.java b/plugins/analysis-phonetic/src/main/java/org/elasticsearch/plugin/analysis/phonetic/HaasePhonetik.java index a9018454805c0..3132c6bed2b49 100644 --- a/plugins/analysis-phonetic/src/main/java/org/elasticsearch/plugin/analysis/phonetic/HaasePhonetik.java +++ b/plugins/analysis-phonetic/src/main/java/org/elasticsearch/plugin/analysis/phonetic/HaasePhonetik.java @@ -26,10 +26,20 @@ */ public class HaasePhonetik extends KoelnerPhonetik { - private static final String[] HAASE_VARIATIONS_PATTERNS = {"OWN", "RB", "WSK", "A$", "O$", "SCH", - "GLI", "EAU$", "^CH", "AUX", "EUX", "ILLE"}; - private static final String[] HAASE_VARIATIONS_REPLACEMENTS = {"AUN", "RW", "RSK", "AR", "OW", "CH", - "LI", "O", "SCH", "O", "O", "I"}; + private static final String[] HAASE_VARIATIONS_PATTERNS = { + "OWN", + "RB", + "WSK", + "A$", + "O$", + "SCH", + "GLI", + "EAU$", + "^CH", + "AUX", + "EUX", + "ILLE" }; + private static final String[] HAASE_VARIATIONS_REPLACEMENTS = { "AUN", "RW", "RSK", "AR", "OW", "CH", "LI", "O", "SCH", "O", "O", "I" }; @Override protected String[] getPatterns() { diff --git a/plugins/analysis-phonetic/src/main/java/org/elasticsearch/plugin/analysis/phonetic/KoelnerPhonetik.java b/plugins/analysis-phonetic/src/main/java/org/elasticsearch/plugin/analysis/phonetic/KoelnerPhonetik.java index 3d3860a867e8b..ee4ee232fd6b9 100644 --- a/plugins/analysis-phonetic/src/main/java/org/elasticsearch/plugin/analysis/phonetic/KoelnerPhonetik.java +++ b/plugins/analysis-phonetic/src/main/java/org/elasticsearch/plugin/analysis/phonetic/KoelnerPhonetik.java @@ -34,18 +34,14 @@ */ public class KoelnerPhonetik implements StringEncoder { - private static final String[] POSTEL_VARIATIONS_PATTERNS = {"AUN", "OWN", "RB", "RW", "WSK", "RSK"}; - private static final String[] POSTEL_VARIATIONS_REPLACEMENTS = {"OWN", "AUN", "RW", "RB", "RSK", "WSK"}; + private static final String[] POSTEL_VARIATIONS_PATTERNS = { "AUN", "OWN", "RB", "RW", "WSK", "RSK" }; + private static final String[] POSTEL_VARIATIONS_REPLACEMENTS = { "OWN", "AUN", "RW", "RB", "RSK", "WSK" }; private Pattern[] variationsPatterns; private boolean primary = false; - private final Set csz = new HashSet<>(Arrays.asList( - 'C', 'S', 'Z')); - private final Set ckq = new HashSet<>(Arrays.asList( - 'C', 'K', 'Q')); - private final Set aouhkxq = new HashSet<>(Arrays.asList( - 'A', 'O', 'U', 'H', 'K', 'X', 'Q')); - private final Set ahkloqrux = new HashSet<>(Arrays.asList( - 'A', 'H', 'K', 'L', 'O', 'Q', 'R', 'U', 'X')); + private final Set csz = new HashSet<>(Arrays.asList('C', 'S', 'Z')); + private final Set ckq = new HashSet<>(Arrays.asList('C', 'K', 'Q')); + private final Set aouhkxq = new HashSet<>(Arrays.asList('A', 'O', 'U', 'H', 'K', 'X', 'Q')); + private final Set ahkloqrux = new HashSet<>(Arrays.asList('A', 'H', 'K', 'L', 'O', 'Q', 'R', 'U', 'X')); /** * Constructor for Kölner Phonetik @@ -108,7 +104,6 @@ public String encode(String str) throws EncoderException { return sb.toString(); } - private void init() { this.variationsPatterns = new Pattern[getPatterns().length]; for (int i = 0; i < getPatterns().length; i++) { diff --git a/plugins/analysis-phonetic/src/main/java/org/elasticsearch/plugin/analysis/phonetic/PhoneticTokenFilterFactory.java b/plugins/analysis-phonetic/src/main/java/org/elasticsearch/plugin/analysis/phonetic/PhoneticTokenFilterFactory.java index fdfc6b7c51569..586ed4729fb70 100644 --- a/plugins/analysis-phonetic/src/main/java/org/elasticsearch/plugin/analysis/phonetic/PhoneticTokenFilterFactory.java +++ b/plugins/analysis-phonetic/src/main/java/org/elasticsearch/plugin/analysis/phonetic/PhoneticTokenFilterFactory.java @@ -72,38 +72,38 @@ public PhoneticTokenFilterFactory(IndexSettings indexSettings, Environment envir this.encoder = null; this.maxcodelength = settings.getAsInt("max_code_len", 4); } else if ("bm".equalsIgnoreCase(encodername) - || "beider_morse".equalsIgnoreCase(encodername) - || "beidermorse".equalsIgnoreCase(encodername)) { - this.encoder = null; - this.languageset = settings.getAsList("languageset"); - String ruleType = settings.get("rule_type", "approx"); - if ("approx".equalsIgnoreCase(ruleType)) { - ruletype = RuleType.APPROX; - } else if ("exact".equalsIgnoreCase(ruleType)) { - ruletype = RuleType.EXACT; + || "beider_morse".equalsIgnoreCase(encodername) + || "beidermorse".equalsIgnoreCase(encodername)) { + this.encoder = null; + this.languageset = settings.getAsList("languageset"); + String ruleType = settings.get("rule_type", "approx"); + if ("approx".equalsIgnoreCase(ruleType)) { + ruletype = RuleType.APPROX; + } else if ("exact".equalsIgnoreCase(ruleType)) { + ruletype = RuleType.EXACT; + } else { + throw new IllegalArgumentException("No matching rule type [" + ruleType + "] for beider morse encoder"); + } + String nameType = settings.get("name_type", "generic"); + if ("GENERIC".equalsIgnoreCase(nameType)) { + nametype = NameType.GENERIC; + } else if ("ASHKENAZI".equalsIgnoreCase(nameType)) { + nametype = NameType.ASHKENAZI; + } else if ("SEPHARDIC".equalsIgnoreCase(nameType)) { + nametype = NameType.SEPHARDIC; + } + } else if ("koelnerphonetik".equalsIgnoreCase(encodername)) { + this.encoder = new KoelnerPhonetik(); + } else if ("haasephonetik".equalsIgnoreCase(encodername)) { + this.encoder = new HaasePhonetik(); + } else if ("nysiis".equalsIgnoreCase(encodername)) { + this.encoder = new Nysiis(); + } else if ("daitch_mokotoff".equalsIgnoreCase(encodername)) { + this.encoder = null; + this.isDaitchMokotoff = true; } else { - throw new IllegalArgumentException("No matching rule type [" + ruleType + "] for beider morse encoder"); - } - String nameType = settings.get("name_type", "generic"); - if ("GENERIC".equalsIgnoreCase(nameType)) { - nametype = NameType.GENERIC; - } else if ("ASHKENAZI".equalsIgnoreCase(nameType)) { - nametype = NameType.ASHKENAZI; - } else if ("SEPHARDIC".equalsIgnoreCase(nameType)) { - nametype = NameType.SEPHARDIC; + throw new IllegalArgumentException("unknown encoder [" + encodername + "] for phonetic token filter"); } - } else if ("koelnerphonetik".equalsIgnoreCase(encodername)) { - this.encoder = new KoelnerPhonetik(); - } else if ("haasephonetik".equalsIgnoreCase(encodername)) { - this.encoder = new HaasePhonetik(); - } else if ("nysiis".equalsIgnoreCase(encodername)) { - this.encoder = new Nysiis(); - } else if ("daitch_mokotoff".equalsIgnoreCase(encodername)) { - this.encoder = null; - this.isDaitchMokotoff = true; - } else { - throw new IllegalArgumentException("unknown encoder [" + encodername + "] for phonetic token filter"); - } } @Override diff --git a/plugins/analysis-phonetic/src/test/java/org/elasticsearch/plugin/analysis/phonetic/AnalysisPhoneticFactoryTests.java b/plugins/analysis-phonetic/src/test/java/org/elasticsearch/plugin/analysis/phonetic/AnalysisPhoneticFactoryTests.java index 495a57d209835..2787e65f82754 100644 --- a/plugins/analysis-phonetic/src/test/java/org/elasticsearch/plugin/analysis/phonetic/AnalysisPhoneticFactoryTests.java +++ b/plugins/analysis-phonetic/src/test/java/org/elasticsearch/plugin/analysis/phonetic/AnalysisPhoneticFactoryTests.java @@ -14,8 +14,6 @@ import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.analysis.TokenFilterFactory; import org.elasticsearch.indices.analysis.AnalysisFactoryTestCase; -import org.elasticsearch.plugin.analysis.phonetic.AnalysisPhoneticPlugin; -import org.elasticsearch.plugin.analysis.phonetic.PhoneticTokenFilterFactory; import org.elasticsearch.test.IndexSettingsModule; import org.elasticsearch.test.VersionUtils; @@ -47,8 +45,7 @@ public void testDisallowedWithSynonyms() throws IOException { .build(); IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("index", settings); - TokenFilterFactory tff - = plugin.getTokenFilters().get("phonetic").get(idxSettings, null, "phonetic", settings); + TokenFilterFactory tff = plugin.getTokenFilters().get("phonetic").get(idxSettings, null, "phonetic", settings); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, tff::getSynonymFilter); assertEquals("Token filter [phonetic] cannot be used to parse synonyms", e.getMessage()); } diff --git a/plugins/analysis-phonetic/src/test/java/org/elasticsearch/plugin/analysis/phonetic/SimplePhoneticAnalysisTests.java b/plugins/analysis-phonetic/src/test/java/org/elasticsearch/plugin/analysis/phonetic/SimplePhoneticAnalysisTests.java index 5e201db05712e..d3997b86fe1bb 100644 --- a/plugins/analysis-phonetic/src/test/java/org/elasticsearch/plugin/analysis/phonetic/SimplePhoneticAnalysisTests.java +++ b/plugins/analysis-phonetic/src/test/java/org/elasticsearch/plugin/analysis/phonetic/SimplePhoneticAnalysisTests.java @@ -33,9 +33,10 @@ public class SimplePhoneticAnalysisTests extends ESTestCase { @Before public void setup() throws IOException { String yaml = "/org/elasticsearch/plugin/analysis/phonetic/phonetic-1.yml"; - Settings settings = Settings.builder().loadFromStream(yaml, getClass().getResourceAsStream(yaml), false) - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .build(); + Settings settings = Settings.builder() + .loadFromStream(yaml, getClass().getResourceAsStream(yaml), false) + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .build(); this.analysis = createTestAnalysis(new Index("test", "_na_"), settings, new AnalysisPhoneticPlugin()); } @@ -48,9 +49,35 @@ public void testPhoneticTokenFilterBeiderMorseNoLanguage() throws IOException { TokenFilterFactory filterFactory = analysis.tokenFilter.get("beidermorsefilter"); Tokenizer tokenizer = new WhitespaceTokenizer(); tokenizer.setReader(new StringReader("ABADIAS")); - String[] expected = new String[] { "abYdias", "abYdios", "abadia", "abadiaS", "abadias", "abadio", "abadioS", "abadios", "abodia", - "abodiaS", "abodias", "abodio", "abodioS", "abodios", "avadias", "avadios", "avodias", "avodios", "obadia", "obadiaS", - "obadias", "obadio", "obadioS", "obadios", "obodia", "obodiaS", "obodias", "obodioS" }; + String[] expected = new String[] { + "abYdias", + "abYdios", + "abadia", + "abadiaS", + "abadias", + "abadio", + "abadioS", + "abadios", + "abodia", + "abodiaS", + "abodias", + "abodio", + "abodioS", + "abodios", + "avadias", + "avadios", + "avodias", + "avodios", + "obadia", + "obadiaS", + "obadias", + "obadio", + "obadioS", + "obadios", + "obodia", + "obodiaS", + "obodias", + "obodioS" }; BaseTokenStreamTestCase.assertTokenStreamContents(filterFactory.create(tokenizer), expected); } @@ -58,8 +85,23 @@ public void testPhoneticTokenFilterBeiderMorseWithLanguage() throws IOException TokenFilterFactory filterFactory = analysis.tokenFilter.get("beidermorsefilterfrench"); Tokenizer tokenizer = new WhitespaceTokenizer(); tokenizer.setReader(new StringReader("Rimbault")); - String[] expected = new String[] { "rimbD", "rimbDlt", "rimba", "rimbalt", "rimbo", "rimbolt", "rimbu", "rimbult", "rmbD", "rmbDlt", - "rmba", "rmbalt", "rmbo", "rmbolt", "rmbu", "rmbult" }; + String[] expected = new String[] { + "rimbD", + "rimbDlt", + "rimba", + "rimbalt", + "rimbo", + "rimbolt", + "rimbu", + "rimbult", + "rmbD", + "rmbDlt", + "rmba", + "rmbalt", + "rmbo", + "rmbolt", + "rmbu", + "rmbult" }; BaseTokenStreamTestCase.assertTokenStreamContents(filterFactory.create(tokenizer), expected); } diff --git a/plugins/analysis-phonetic/src/yamlRestTest/java/org/elasticsearch/index/analysis/PhoneticClientYamlTestSuiteIT.java b/plugins/analysis-phonetic/src/yamlRestTest/java/org/elasticsearch/index/analysis/PhoneticClientYamlTestSuiteIT.java index 887f8384cdff2..ae9f25d4bd0da 100644 --- a/plugins/analysis-phonetic/src/yamlRestTest/java/org/elasticsearch/index/analysis/PhoneticClientYamlTestSuiteIT.java +++ b/plugins/analysis-phonetic/src/yamlRestTest/java/org/elasticsearch/index/analysis/PhoneticClientYamlTestSuiteIT.java @@ -25,4 +25,3 @@ public static Iterable parameters() throws Exception { return ESClientYamlSuiteTestCase.createParameters(); } } - diff --git a/plugins/analysis-smartcn/src/main/java/org/elasticsearch/plugin/analysis/smartcn/SmartChineseStopTokenFilterFactory.java b/plugins/analysis-smartcn/src/main/java/org/elasticsearch/plugin/analysis/smartcn/SmartChineseStopTokenFilterFactory.java index 6caea9d6830a9..d261465fa4c11 100644 --- a/plugins/analysis-smartcn/src/main/java/org/elasticsearch/plugin/analysis/smartcn/SmartChineseStopTokenFilterFactory.java +++ b/plugins/analysis-smartcn/src/main/java/org/elasticsearch/plugin/analysis/smartcn/SmartChineseStopTokenFilterFactory.java @@ -37,8 +37,14 @@ public SmartChineseStopTokenFilterFactory(IndexSettings indexSettings, Environme super(indexSettings, name, settings); this.ignoreCase = settings.getAsBoolean("ignore_case", false); this.removeTrailing = settings.getAsBoolean("remove_trailing", true); - this.stopWords = Analysis.parseWords(env, settings, "stopwords", - SmartChineseAnalyzer.getDefaultStopSet(), NAMED_STOP_WORDS, ignoreCase); + this.stopWords = Analysis.parseWords( + env, + settings, + "stopwords", + SmartChineseAnalyzer.getDefaultStopSet(), + NAMED_STOP_WORDS, + ignoreCase + ); } @Override diff --git a/plugins/analysis-smartcn/src/test/java/org/elasticsearch/plugin/analysis/smartcn/AnalysisSmartChineseFactoryTests.java b/plugins/analysis-smartcn/src/test/java/org/elasticsearch/plugin/analysis/smartcn/AnalysisSmartChineseFactoryTests.java index 4fff866b65cf9..23d0b1a21a204 100644 --- a/plugins/analysis-smartcn/src/test/java/org/elasticsearch/plugin/analysis/smartcn/AnalysisSmartChineseFactoryTests.java +++ b/plugins/analysis-smartcn/src/test/java/org/elasticsearch/plugin/analysis/smartcn/AnalysisSmartChineseFactoryTests.java @@ -9,8 +9,6 @@ package org.elasticsearch.plugin.analysis.smartcn; import org.elasticsearch.indices.analysis.AnalysisFactoryTestCase; -import org.elasticsearch.plugin.analysis.smartcn.AnalysisSmartChinesePlugin; -import org.elasticsearch.plugin.analysis.smartcn.SmartChineseTokenizerTokenizerFactory; import java.util.HashMap; import java.util.Map; @@ -19,6 +17,7 @@ public class AnalysisSmartChineseFactoryTests extends AnalysisFactoryTestCase { public AnalysisSmartChineseFactoryTests() { super(new AnalysisSmartChinesePlugin()); } + @Override protected Map> getTokenizers() { Map> tokenizers = new HashMap<>(super.getTokenizers()); diff --git a/plugins/analysis-smartcn/src/test/java/org/elasticsearch/plugin/analysis/smartcn/SimpleSmartChineseAnalysisTests.java b/plugins/analysis-smartcn/src/test/java/org/elasticsearch/plugin/analysis/smartcn/SimpleSmartChineseAnalysisTests.java index afc9f29c7e65f..6187f65e3cd30 100644 --- a/plugins/analysis-smartcn/src/test/java/org/elasticsearch/plugin/analysis/smartcn/SimpleSmartChineseAnalysisTests.java +++ b/plugins/analysis-smartcn/src/test/java/org/elasticsearch/plugin/analysis/smartcn/SimpleSmartChineseAnalysisTests.java @@ -11,8 +11,6 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.Index; import org.elasticsearch.index.analysis.TokenizerFactory; -import org.elasticsearch.plugin.analysis.smartcn.AnalysisSmartChinesePlugin; -import org.elasticsearch.plugin.analysis.smartcn.SmartChineseTokenizerTokenizerFactory; import org.elasticsearch.test.ESTestCase; import org.hamcrest.MatcherAssert; @@ -22,8 +20,7 @@ public class SimpleSmartChineseAnalysisTests extends ESTestCase { public void testDefaultsIcuAnalysis() throws IOException { - final TestAnalysis analysis = createTestAnalysis(new Index("test", "_na_"), Settings.EMPTY, - new AnalysisSmartChinesePlugin()); + final TestAnalysis analysis = createTestAnalysis(new Index("test", "_na_"), Settings.EMPTY, new AnalysisSmartChinesePlugin()); TokenizerFactory tokenizerFactory = analysis.tokenizer.get("smartcn_tokenizer"); MatcherAssert.assertThat(tokenizerFactory, instanceOf(SmartChineseTokenizerTokenizerFactory.class)); } diff --git a/plugins/analysis-smartcn/src/yamlRestTest/java/org/elasticsearch/index/analysis/SmartCNClientYamlTestSuiteIT.java b/plugins/analysis-smartcn/src/yamlRestTest/java/org/elasticsearch/index/analysis/SmartCNClientYamlTestSuiteIT.java index 56fe34591f53c..1d83ba8156916 100644 --- a/plugins/analysis-smartcn/src/yamlRestTest/java/org/elasticsearch/index/analysis/SmartCNClientYamlTestSuiteIT.java +++ b/plugins/analysis-smartcn/src/yamlRestTest/java/org/elasticsearch/index/analysis/SmartCNClientYamlTestSuiteIT.java @@ -25,4 +25,3 @@ public static Iterable parameters() throws Exception { return ESClientYamlSuiteTestCase.createParameters(); } } - diff --git a/plugins/analysis-stempel/src/main/java/org/elasticsearch/index/analysis/pl/PolishStemTokenFilterFactory.java b/plugins/analysis-stempel/src/main/java/org/elasticsearch/index/analysis/pl/PolishStemTokenFilterFactory.java index d7ea34b3ca897..b6f630bb78e6e 100644 --- a/plugins/analysis-stempel/src/main/java/org/elasticsearch/index/analysis/pl/PolishStemTokenFilterFactory.java +++ b/plugins/analysis-stempel/src/main/java/org/elasticsearch/index/analysis/pl/PolishStemTokenFilterFactory.java @@ -17,16 +17,14 @@ import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.analysis.AbstractTokenFilterFactory; - - - public class PolishStemTokenFilterFactory extends AbstractTokenFilterFactory { public PolishStemTokenFilterFactory(IndexSettings indexSettings, Environment environment, String name, Settings settings) { super(indexSettings, name, settings); } - @Override public TokenStream create(TokenStream tokenStream) { + @Override + public TokenStream create(TokenStream tokenStream) { return new StempelFilter(tokenStream, new StempelStemmer(PolishAnalyzer.getDefaultTable())); } } diff --git a/plugins/analysis-stempel/src/main/java/org/elasticsearch/index/analysis/pl/PolishStopTokenFilterFactory.java b/plugins/analysis-stempel/src/main/java/org/elasticsearch/index/analysis/pl/PolishStopTokenFilterFactory.java index a53b42b75e4cb..9b35bd4ed2b87 100644 --- a/plugins/analysis-stempel/src/main/java/org/elasticsearch/index/analysis/pl/PolishStopTokenFilterFactory.java +++ b/plugins/analysis-stempel/src/main/java/org/elasticsearch/index/analysis/pl/PolishStopTokenFilterFactory.java @@ -8,7 +8,6 @@ package org.elasticsearch.index.analysis.pl; - import org.apache.lucene.analysis.CharArraySet; import org.apache.lucene.analysis.StopFilter; import org.apache.lucene.analysis.TokenStream; @@ -38,8 +37,7 @@ public PolishStopTokenFilterFactory(IndexSettings indexSettings, Environment env super(indexSettings, name, settings); this.ignoreCase = settings.getAsBoolean("ignore_case", false); this.removeTrailing = settings.getAsBoolean("remove_trailing", true); - this.stopWords = Analysis.parseWords(env, settings, "stopwords", - PolishAnalyzer.getDefaultStopSet(), NAMED_STOP_WORDS, ignoreCase); + this.stopWords = Analysis.parseWords(env, settings, "stopwords", PolishAnalyzer.getDefaultStopSet(), NAMED_STOP_WORDS, ignoreCase); } @Override diff --git a/plugins/analysis-stempel/src/main/java/org/elasticsearch/plugin/analysis/stempel/AnalysisStempelPlugin.java b/plugins/analysis-stempel/src/main/java/org/elasticsearch/plugin/analysis/stempel/AnalysisStempelPlugin.java index 7125935ccf097..5d3b9e873b3e0 100644 --- a/plugins/analysis-stempel/src/main/java/org/elasticsearch/plugin/analysis/stempel/AnalysisStempelPlugin.java +++ b/plugins/analysis-stempel/src/main/java/org/elasticsearch/plugin/analysis/stempel/AnalysisStempelPlugin.java @@ -25,8 +25,7 @@ public class AnalysisStempelPlugin extends Plugin implements AnalysisPlugin { @Override public Map> getTokenFilters() { - return Map.of("polish_stem", PolishStemTokenFilterFactory::new, - "polish_stop", PolishStopTokenFilterFactory::new); + return Map.of("polish_stem", PolishStemTokenFilterFactory::new, "polish_stop", PolishStopTokenFilterFactory::new); } @Override diff --git a/plugins/analysis-stempel/src/test/java/org/elasticsearch/index/analysis/AnalysisPolishFactoryTests.java b/plugins/analysis-stempel/src/test/java/org/elasticsearch/index/analysis/AnalysisPolishFactoryTests.java index 45dadb1f3a1f4..df0c2274bbfae 100644 --- a/plugins/analysis-stempel/src/test/java/org/elasticsearch/index/analysis/AnalysisPolishFactoryTests.java +++ b/plugins/analysis-stempel/src/test/java/org/elasticsearch/index/analysis/AnalysisPolishFactoryTests.java @@ -40,7 +40,7 @@ protected Map> getTokenFilters() { } public void testThreadSafety() throws IOException { - // TODO: is this the right boilerplate? I forked this out of TransportAnalyzeAction.java: + // TODO: is this the right boilerplate? I forked this out of TransportAnalyzeAction.java: Settings settings = Settings.builder() // for _na_ .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) diff --git a/plugins/analysis-stempel/src/test/java/org/elasticsearch/index/analysis/PolishAnalysisTests.java b/plugins/analysis-stempel/src/test/java/org/elasticsearch/index/analysis/PolishAnalysisTests.java index 4b9ac61d77797..fafbcafb3189b 100644 --- a/plugins/analysis-stempel/src/test/java/org/elasticsearch/index/analysis/PolishAnalysisTests.java +++ b/plugins/analysis-stempel/src/test/java/org/elasticsearch/index/analysis/PolishAnalysisTests.java @@ -23,8 +23,7 @@ public class PolishAnalysisTests extends ESTestCase { public void testDefaultsPolishAnalysis() throws IOException { - final TestAnalysis analysis = createTestAnalysis(new Index("test", "_na_"), Settings.EMPTY, - new AnalysisStempelPlugin()); + final TestAnalysis analysis = createTestAnalysis(new Index("test", "_na_"), Settings.EMPTY, new AnalysisStempelPlugin()); TokenFilterFactory tokenizerFactory = analysis.tokenFilter.get("polish_stem"); MatcherAssert.assertThat(tokenizerFactory, instanceOf(PolishStemTokenFilterFactory.class)); diff --git a/plugins/analysis-stempel/src/test/java/org/elasticsearch/index/analysis/SimplePolishTokenFilterTests.java b/plugins/analysis-stempel/src/test/java/org/elasticsearch/index/analysis/SimplePolishTokenFilterTests.java index 0095b4baa06ac..671cb2e4f15cc 100644 --- a/plugins/analysis-stempel/src/test/java/org/elasticsearch/index/analysis/SimplePolishTokenFilterTests.java +++ b/plugins/analysis-stempel/src/test/java/org/elasticsearch/index/analysis/SimplePolishTokenFilterTests.java @@ -35,9 +35,7 @@ public void testBasicUsage() throws Exception { private void testToken(String source, String expected) throws IOException { Index index = new Index("test", "_na_"); - Settings settings = Settings.builder() - .put("index.analysis.filter.myStemmer.type", "polish_stem") - .build(); + Settings settings = Settings.builder().put("index.analysis.filter.myStemmer.type", "polish_stem").build(); TestAnalysis analysis = createTestAnalysis(index, settings, new AnalysisStempelPlugin()); TokenFilterFactory filterFactory = analysis.tokenFilter.get("myStemmer"); diff --git a/plugins/analysis-stempel/src/yamlRestTest/java/org/elasticsearch/index/analysis/StempelClientYamlTestSuiteIT.java b/plugins/analysis-stempel/src/yamlRestTest/java/org/elasticsearch/index/analysis/StempelClientYamlTestSuiteIT.java index 60c5665ebe8ed..faee6493920c8 100644 --- a/plugins/analysis-stempel/src/yamlRestTest/java/org/elasticsearch/index/analysis/StempelClientYamlTestSuiteIT.java +++ b/plugins/analysis-stempel/src/yamlRestTest/java/org/elasticsearch/index/analysis/StempelClientYamlTestSuiteIT.java @@ -25,4 +25,3 @@ public static Iterable parameters() throws Exception { return ESClientYamlSuiteTestCase.createParameters(); } } - diff --git a/plugins/analysis-ukrainian/src/main/java/org/elasticsearch/plugin/analysis/ukrainian/XUkrainianMorfologikAnalyzer.java b/plugins/analysis-ukrainian/src/main/java/org/elasticsearch/plugin/analysis/ukrainian/XUkrainianMorfologikAnalyzer.java index 9bce52de6f8b3..0359297c9896d 100644 --- a/plugins/analysis-ukrainian/src/main/java/org/elasticsearch/plugin/analysis/ukrainian/XUkrainianMorfologikAnalyzer.java +++ b/plugins/analysis-ukrainian/src/main/java/org/elasticsearch/plugin/analysis/ukrainian/XUkrainianMorfologikAnalyzer.java @@ -17,6 +17,7 @@ package org.elasticsearch.plugin.analysis.ukrainian; import morfologik.stemming.Dictionary; + import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.CharArraySet; import org.apache.lucene.analysis.LowerCaseFilter; @@ -62,17 +63,17 @@ public static CharArraySet getDefaultStopSet() { * Atomically loads the DEFAULT_STOP_SET and DICTIONARY in a lazy fashion once the outer class * accesses the static final set the first time.; */ - @SuppressForbidden(reason="Lucene uses IOUtils") + @SuppressForbidden(reason = "Lucene uses IOUtils") private static class DefaultSetHolder { static final CharArraySet DEFAULT_STOP_SET; static final Dictionary DICTIONARY; static { try { - DEFAULT_STOP_SET = WordlistLoader.getSnowballWordSet(IOUtils.getDecodingReader(UkrainianMorfologikAnalyzer.class, - DEFAULT_STOPWORD_FILE, StandardCharsets.UTF_8)); - DICTIONARY = Dictionary.read( - UkrainianMorfologikAnalyzer.class.getClassLoader().getResource("ua/net/nlp/ukrainian.dict")); + DEFAULT_STOP_SET = WordlistLoader.getSnowballWordSet( + IOUtils.getDecodingReader(UkrainianMorfologikAnalyzer.class, DEFAULT_STOPWORD_FILE, StandardCharsets.UTF_8) + ); + DICTIONARY = Dictionary.read(UkrainianMorfologikAnalyzer.class.getClassLoader().getResource("ua/net/nlp/ukrainian.dict")); } catch (IOException ex) { // default set should always be present as it is part of the // distribution (JAR) diff --git a/plugins/analysis-ukrainian/src/test/java/org/elasticsearch/plugin/analysis/ukrainian/SimpleUkrainianAnalyzerTests.java b/plugins/analysis-ukrainian/src/test/java/org/elasticsearch/plugin/analysis/ukrainian/SimpleUkrainianAnalyzerTests.java index e8b3e75b56938..c774405aa36d1 100644 --- a/plugins/analysis-ukrainian/src/test/java/org/elasticsearch/plugin/analysis/ukrainian/SimpleUkrainianAnalyzerTests.java +++ b/plugins/analysis-ukrainian/src/test/java/org/elasticsearch/plugin/analysis/ukrainian/SimpleUkrainianAnalyzerTests.java @@ -13,7 +13,6 @@ import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.Index; -import org.elasticsearch.plugin.analysis.ukrainian.AnalysisUkrainianPlugin; import org.elasticsearch.test.ESTestCase; import java.io.IOException; diff --git a/plugins/analysis-ukrainian/src/test/java/org/elasticsearch/plugin/analysis/ukrainian/UkrainianAnalysisTests.java b/plugins/analysis-ukrainian/src/test/java/org/elasticsearch/plugin/analysis/ukrainian/UkrainianAnalysisTests.java index 7c4b6a9783a96..cde14f34dcdd0 100644 --- a/plugins/analysis-ukrainian/src/test/java/org/elasticsearch/plugin/analysis/ukrainian/UkrainianAnalysisTests.java +++ b/plugins/analysis-ukrainian/src/test/java/org/elasticsearch/plugin/analysis/ukrainian/UkrainianAnalysisTests.java @@ -21,8 +21,7 @@ public class UkrainianAnalysisTests extends ESTestCase { public void testDefaultsUkranianAnalysis() throws IOException { - final TestAnalysis analysis = createTestAnalysis(new Index("test", "_na_"), Settings.EMPTY, - new AnalysisUkrainianPlugin()); + final TestAnalysis analysis = createTestAnalysis(new Index("test", "_na_"), Settings.EMPTY, new AnalysisUkrainianPlugin()); Analyzer analyzer = analysis.indexAnalyzers.get("ukrainian").analyzer(); MatcherAssert.assertThat(analyzer, instanceOf(XUkrainianMorfologikAnalyzer.class)); diff --git a/plugins/analysis-ukrainian/src/yamlRestTest/java/org/elasticsearch/index/analysis/UkrainianClientYamlTestSuiteIT.java b/plugins/analysis-ukrainian/src/yamlRestTest/java/org/elasticsearch/index/analysis/UkrainianClientYamlTestSuiteIT.java index 1456a03730b4d..e02036548c0ad 100644 --- a/plugins/analysis-ukrainian/src/yamlRestTest/java/org/elasticsearch/index/analysis/UkrainianClientYamlTestSuiteIT.java +++ b/plugins/analysis-ukrainian/src/yamlRestTest/java/org/elasticsearch/index/analysis/UkrainianClientYamlTestSuiteIT.java @@ -25,4 +25,3 @@ public static Iterable parameters() throws Exception { return ESClientYamlSuiteTestCase.createParameters(); } } - diff --git a/plugins/discovery-azure-classic/src/internalClusterTest/java/org/elasticsearch/cloud/azure/classic/AbstractAzureComputeServiceTestCase.java b/plugins/discovery-azure-classic/src/internalClusterTest/java/org/elasticsearch/cloud/azure/classic/AbstractAzureComputeServiceTestCase.java index 1ddf8d22d6d9b..d96b987878f33 100644 --- a/plugins/discovery-azure-classic/src/internalClusterTest/java/org/elasticsearch/cloud/azure/classic/AbstractAzureComputeServiceTestCase.java +++ b/plugins/discovery-azure-classic/src/internalClusterTest/java/org/elasticsearch/cloud/azure/classic/AbstractAzureComputeServiceTestCase.java @@ -14,6 +14,7 @@ import com.microsoft.windowsazure.management.compute.models.InstanceEndpoint; import com.microsoft.windowsazure.management.compute.models.RoleInstance; import com.microsoft.windowsazure.management.compute.models.RoleInstancePowerState; + import org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse; import org.elasticsearch.cloud.azure.classic.management.AzureComputeService; import org.elasticsearch.cloud.azure.classic.management.AzureComputeService.Discovery; @@ -145,10 +146,12 @@ protected AzureComputeService createComputeService() { * network addresses for Azure instances running on the same host but different ports. */ @Override - protected AzureSeedHostsProvider createSeedHostsProvider(final Settings settings, - final AzureComputeService azureComputeService, - final TransportService transportService, - final NetworkService networkService) { + protected AzureSeedHostsProvider createSeedHostsProvider( + final Settings settings, + final AzureComputeService azureComputeService, + final TransportService transportService, + final NetworkService networkService + ) { return new AzureSeedHostsProvider(settings, azureComputeService, transportService, networkService) { @Override protected String resolveInstanceAddress(final HostType hostType, final RoleInstance instance) { diff --git a/plugins/discovery-azure-classic/src/internalClusterTest/java/org/elasticsearch/discovery/azure/classic/AzureDiscoveryClusterFormationTests.java b/plugins/discovery-azure-classic/src/internalClusterTest/java/org/elasticsearch/discovery/azure/classic/AzureDiscoveryClusterFormationTests.java index 58e3aad855c59..2588b6f06bf1e 100644 --- a/plugins/discovery-azure-classic/src/internalClusterTest/java/org/elasticsearch/discovery/azure/classic/AzureDiscoveryClusterFormationTests.java +++ b/plugins/discovery-azure-classic/src/internalClusterTest/java/org/elasticsearch/discovery/azure/classic/AzureDiscoveryClusterFormationTests.java @@ -13,15 +13,16 @@ import com.sun.net.httpserver.Headers; import com.sun.net.httpserver.HttpsConfigurator; import com.sun.net.httpserver.HttpsServer; + import org.apache.logging.log4j.LogManager; -import org.elasticsearch.jdk.JavaVersion; import org.elasticsearch.cloud.azure.classic.management.AzureComputeService; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.common.io.FileSystemUtils; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.discovery.DiscoveryModule; import org.elasticsearch.env.Environment; +import org.elasticsearch.jdk.JavaVersion; import org.elasticsearch.mocksocket.MockHttpServer; import org.elasticsearch.node.Node; import org.elasticsearch.plugin.discovery.azure.classic.AzureDiscoveryPlugin; @@ -33,13 +34,6 @@ import org.junit.ClassRule; import org.junit.rules.ExternalResource; -import javax.net.ssl.KeyManagerFactory; -import javax.net.ssl.SSLContext; -import javax.net.ssl.TrustManagerFactory; -import javax.xml.XMLConstants; -import javax.xml.stream.XMLOutputFactory; -import javax.xml.stream.XMLStreamException; -import javax.xml.stream.XMLStreamWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -59,6 +53,14 @@ import java.util.UUID; import java.util.concurrent.ExecutionException; +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManagerFactory; +import javax.xml.XMLConstants; +import javax.xml.stream.XMLOutputFactory; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamWriter; + @ESIntegTestCase.ClusterScope(numDataNodes = 2, numClientNodes = 0) @SuppressForbidden(reason = "use http server") public class AzureDiscoveryClusterFormationTests extends ESIntegTestCase { @@ -106,13 +108,16 @@ protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { } catch (IOException e) { throw new RuntimeException(e); } - return Settings.builder().put(super.nodeSettings(nodeOrdinal, otherSettings)) + return Settings.builder() + .put(super.nodeSettings(nodeOrdinal, otherSettings)) .put(DiscoveryModule.DISCOVERY_SEED_PROVIDERS_SETTING.getKey(), AzureDiscoveryPlugin.AZURE) .put(Environment.PATH_LOGS_SETTING.getKey(), resolve) .put(TransportSettings.PORT.getKey(), 0) .put(Node.WRITE_PORTS_FILE_SETTING.getKey(), "true") - .put(AzureComputeService.Management.ENDPOINT_SETTING.getKey(), "https://" + InetAddress.getLoopbackAddress().getHostAddress() + - ":" + httpsServer.getAddress().getPort()) + .put( + AzureComputeService.Management.ENDPOINT_SETTING.getKey(), + "https://" + InetAddress.getLoopbackAddress().getHostAddress() + ":" + httpsServer.getAddress().getPort() + ) .put(AzureComputeService.Management.KEYSTORE_PATH_SETTING.getKey(), keyStoreFile.toAbsolutePath()) .put(AzureComputeService.Discovery.HOST_TYPE_SETTING.getKey(), AzureSeedHostsProvider.HostType.PUBLIC_IP.name()) .put(AzureComputeService.Management.KEYSTORE_PASSWORD_SETTING.getKey(), "keypass") @@ -266,9 +271,9 @@ private static String getProtocol() { if (JavaVersion.current().compareTo(JavaVersion.parse("12")) < 0) { return "TLSv1.2"; } else { - JavaVersion full = - AccessController.doPrivileged( - (PrivilegedAction) () -> JavaVersion.parse(System.getProperty("java.version"))); + JavaVersion full = AccessController.doPrivileged( + (PrivilegedAction) () -> JavaVersion.parse(System.getProperty("java.version")) + ); if (full.compareTo(JavaVersion.parse("12.0.1")) < 0) { return "TLSv1.2"; } diff --git a/plugins/discovery-azure-classic/src/internalClusterTest/java/org/elasticsearch/discovery/azure/classic/AzureSimpleTests.java b/plugins/discovery-azure-classic/src/internalClusterTest/java/org/elasticsearch/discovery/azure/classic/AzureSimpleTests.java index 6e4c00a37bf5f..00d8b5980374c 100644 --- a/plugins/discovery-azure-classic/src/internalClusterTest/java/org/elasticsearch/discovery/azure/classic/AzureSimpleTests.java +++ b/plugins/discovery-azure-classic/src/internalClusterTest/java/org/elasticsearch/discovery/azure/classic/AzureSimpleTests.java @@ -16,15 +16,13 @@ import static org.hamcrest.Matchers.containsString; -@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, - numDataNodes = 0, - numClientNodes = 0) +@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0, numClientNodes = 0) public class AzureSimpleTests extends AbstractAzureComputeServiceTestCase { public void testOneNodeShouldRunUsingPrivateIp() { Settings.Builder settings = Settings.builder() - .put(Management.SERVICE_NAME_SETTING.getKey(), "dummy") - .put(Discovery.HOST_TYPE_SETTING.getKey(), "private_ip"); + .put(Management.SERVICE_NAME_SETTING.getKey(), "dummy") + .put(Discovery.HOST_TYPE_SETTING.getKey(), "private_ip"); final String node1 = internalCluster().startNode(settings); registerAzureNode(node1); @@ -36,8 +34,8 @@ public void testOneNodeShouldRunUsingPrivateIp() { public void testOneNodeShouldRunUsingPublicIp() { Settings.Builder settings = Settings.builder() - .put(Management.SERVICE_NAME_SETTING.getKey(), "dummy") - .put(Discovery.HOST_TYPE_SETTING.getKey(), "public_ip"); + .put(Management.SERVICE_NAME_SETTING.getKey(), "dummy") + .put(Discovery.HOST_TYPE_SETTING.getKey(), "public_ip"); final String node1 = internalCluster().startNode(settings); registerAzureNode(node1); @@ -49,8 +47,8 @@ public void testOneNodeShouldRunUsingPublicIp() { public void testOneNodeShouldRunUsingWrongSettings() { Settings.Builder settings = Settings.builder() - .put(Management.SERVICE_NAME_SETTING.getKey(), "dummy") - .put(Discovery.HOST_TYPE_SETTING.getKey(), "do_not_exist"); + .put(Management.SERVICE_NAME_SETTING.getKey(), "dummy") + .put(Discovery.HOST_TYPE_SETTING.getKey(), "do_not_exist"); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> internalCluster().startNode(settings)); assertThat(e.getMessage(), containsString("invalid value for host type [do_not_exist]")); diff --git a/plugins/discovery-azure-classic/src/internalClusterTest/java/org/elasticsearch/discovery/azure/classic/AzureTwoStartedNodesTests.java b/plugins/discovery-azure-classic/src/internalClusterTest/java/org/elasticsearch/discovery/azure/classic/AzureTwoStartedNodesTests.java index 16499dd2f1ec4..dd85fa88b94db 100644 --- a/plugins/discovery-azure-classic/src/internalClusterTest/java/org/elasticsearch/discovery/azure/classic/AzureTwoStartedNodesTests.java +++ b/plugins/discovery-azure-classic/src/internalClusterTest/java/org/elasticsearch/discovery/azure/classic/AzureTwoStartedNodesTests.java @@ -14,9 +14,7 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.test.ESIntegTestCase; -@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, - numDataNodes = 0, - numClientNodes = 0) +@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0, numClientNodes = 0) public class AzureTwoStartedNodesTests extends AbstractAzureComputeServiceTestCase { public void testTwoNodesShouldRunUsingPrivateOrPublicIp() { diff --git a/plugins/discovery-azure-classic/src/main/java/org/elasticsearch/cloud/azure/classic/management/AzureComputeService.java b/plugins/discovery-azure-classic/src/main/java/org/elasticsearch/cloud/azure/classic/management/AzureComputeService.java index e1d5696138cce..6579efa1d8733 100644 --- a/plugins/discovery-azure-classic/src/main/java/org/elasticsearch/cloud/azure/classic/management/AzureComputeService.java +++ b/plugins/discovery-azure-classic/src/main/java/org/elasticsearch/cloud/azure/classic/management/AzureComputeService.java @@ -10,6 +10,7 @@ import com.microsoft.windowsazure.core.utils.KeyStoreType; import com.microsoft.windowsazure.management.compute.models.HostedServiceGetDetailedResponse; + import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Setting.Property; import org.elasticsearch.core.TimeValue; @@ -23,44 +24,78 @@ public interface AzureComputeService { final class Management { - public static final Setting SUBSCRIPTION_ID_SETTING = - Setting.simpleString("cloud.azure.management.subscription.id", Property.NodeScope, Property.Filtered); - public static final Setting SERVICE_NAME_SETTING = - Setting.simpleString("cloud.azure.management.cloud.service.name", Property.NodeScope); + public static final Setting SUBSCRIPTION_ID_SETTING = Setting.simpleString( + "cloud.azure.management.subscription.id", + Property.NodeScope, + Property.Filtered + ); + public static final Setting SERVICE_NAME_SETTING = Setting.simpleString( + "cloud.azure.management.cloud.service.name", + Property.NodeScope + ); // Keystore settings - public static final Setting KEYSTORE_PATH_SETTING = - Setting.simpleString("cloud.azure.management.keystore.path", Property.NodeScope, Property.Filtered); - public static final Setting KEYSTORE_PASSWORD_SETTING = - Setting.simpleString("cloud.azure.management.keystore.password", Property.NodeScope, - Property.Filtered); - public static final Setting KEYSTORE_TYPE_SETTING = - new Setting<>("cloud.azure.management.keystore.type", KeyStoreType.pkcs12.name(), KeyStoreType::fromString, - Property.NodeScope, Property.Filtered); + public static final Setting KEYSTORE_PATH_SETTING = Setting.simpleString( + "cloud.azure.management.keystore.path", + Property.NodeScope, + Property.Filtered + ); + public static final Setting KEYSTORE_PASSWORD_SETTING = Setting.simpleString( + "cloud.azure.management.keystore.password", + Property.NodeScope, + Property.Filtered + ); + public static final Setting KEYSTORE_TYPE_SETTING = new Setting<>( + "cloud.azure.management.keystore.type", + KeyStoreType.pkcs12.name(), + KeyStoreType::fromString, + Property.NodeScope, + Property.Filtered + ); // so that it can overridden for tests - public static final Setting ENDPOINT_SETTING = new Setting("cloud.azure.management.endpoint", - "https://management.core.windows.net/", s -> { + public static final Setting ENDPOINT_SETTING = new Setting( + "cloud.azure.management.endpoint", + "https://management.core.windows.net/", + s -> { try { return new URI(s); } catch (URISyntaxException e) { throw new IllegalArgumentException(e); } - }, Property.NodeScope); + }, + Property.NodeScope + ); } final class Discovery { - public static final Setting REFRESH_SETTING = - Setting.positiveTimeSetting("discovery.azure.refresh_interval", TimeValue.timeValueSeconds(0), Property.NodeScope); - public static final Setting HOST_TYPE_SETTING = - new Setting<>("discovery.azure.host.type", AzureSeedHostsProvider.HostType.PRIVATE_IP.name(), - AzureSeedHostsProvider.HostType::fromString, Property.NodeScope); - public static final Setting ENDPOINT_NAME_SETTING = new Setting<>("discovery.azure.endpoint.name", "elasticsearch", - Function.identity(), Property.NodeScope); - public static final Setting DEPLOYMENT_NAME_SETTING = Setting.simpleString("discovery.azure.deployment.name", - Property.NodeScope); - public static final Setting DEPLOYMENT_SLOT_SETTING = new Setting<>("discovery.azure.deployment.slot", - Deployment.PRODUCTION.name(), Deployment::fromString, Property.NodeScope); + public static final Setting REFRESH_SETTING = Setting.positiveTimeSetting( + "discovery.azure.refresh_interval", + TimeValue.timeValueSeconds(0), + Property.NodeScope + ); + public static final Setting HOST_TYPE_SETTING = new Setting<>( + "discovery.azure.host.type", + AzureSeedHostsProvider.HostType.PRIVATE_IP.name(), + AzureSeedHostsProvider.HostType::fromString, + Property.NodeScope + ); + public static final Setting ENDPOINT_NAME_SETTING = new Setting<>( + "discovery.azure.endpoint.name", + "elasticsearch", + Function.identity(), + Property.NodeScope + ); + public static final Setting DEPLOYMENT_NAME_SETTING = Setting.simpleString( + "discovery.azure.deployment.name", + Property.NodeScope + ); + public static final Setting DEPLOYMENT_SLOT_SETTING = new Setting<>( + "discovery.azure.deployment.slot", + Deployment.PRODUCTION.name(), + Deployment::fromString, + Property.NodeScope + ); } HostedServiceGetDetailedResponse getServiceDetails(); diff --git a/plugins/discovery-azure-classic/src/main/java/org/elasticsearch/cloud/azure/classic/management/AzureComputeServiceImpl.java b/plugins/discovery-azure-classic/src/main/java/org/elasticsearch/cloud/azure/classic/management/AzureComputeServiceImpl.java index cef2dbf3a7f13..995e197e92bed 100644 --- a/plugins/discovery-azure-classic/src/main/java/org/elasticsearch/cloud/azure/classic/management/AzureComputeServiceImpl.java +++ b/plugins/discovery-azure-classic/src/main/java/org/elasticsearch/cloud/azure/classic/management/AzureComputeServiceImpl.java @@ -8,12 +8,6 @@ package org.elasticsearch.cloud.azure.classic.management; -import java.io.IOException; -import java.security.AccessController; -import java.security.PrivilegedActionException; -import java.security.PrivilegedExceptionAction; -import java.util.ServiceLoader; - import com.microsoft.windowsazure.Configuration; import com.microsoft.windowsazure.core.Builder; import com.microsoft.windowsazure.core.DefaultBuilder; @@ -22,6 +16,7 @@ import com.microsoft.windowsazure.management.compute.ComputeManagementService; import com.microsoft.windowsazure.management.compute.models.HostedServiceGetDetailedResponse; import com.microsoft.windowsazure.management.configuration.ManagementConfiguration; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.ElasticsearchException; @@ -32,10 +27,14 @@ import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; -public class AzureComputeServiceImpl extends AbstractLifecycleComponent - implements AzureComputeService { - private static final Logger logger = LogManager.getLogger(AzureComputeServiceImpl.class); +import java.io.IOException; +import java.security.AccessController; +import java.security.PrivilegedActionException; +import java.security.PrivilegedExceptionAction; +import java.util.ServiceLoader; +public class AzureComputeServiceImpl extends AbstractLifecycleComponent implements AzureComputeService { + private static final Logger logger = LogManager.getLogger(AzureComputeServiceImpl.class); private final ComputeManagementClient client; private final String serviceName; @@ -65,8 +64,15 @@ public AzureComputeServiceImpl(Settings settings) { Configuration configuration = new Configuration(builder); configuration.setProperty(Configuration.PROPERTY_LOG_HTTP_REQUESTS, logger.isTraceEnabled()); - Configuration managementConfig = ManagementConfiguration.configure(null, configuration, - Management.ENDPOINT_SETTING.get(settings), subscriptionId, keystorePath, keystorePassword, keystoreType); + Configuration managementConfig = ManagementConfiguration.configure( + null, + configuration, + Management.ENDPOINT_SETTING.get(settings), + subscriptionId, + keystorePath, + keystorePassword, + keystoreType + ); logger.debug("creating new Azure client for [{}], [{}]", subscriptionId, serviceName); client = ComputeManagementService.create(managementConfig); @@ -87,20 +93,20 @@ private static String getRequiredSetting(Settings settings, Setting sett public HostedServiceGetDetailedResponse getServiceDetails() { SpecialPermission.check(); try { - return AccessController.doPrivileged((PrivilegedExceptionAction) - () -> client.getHostedServicesOperations().getDetailed(serviceName)); + return AccessController.doPrivileged( + (PrivilegedExceptionAction) () -> client.getHostedServicesOperations() + .getDetailed(serviceName) + ); } catch (PrivilegedActionException e) { throw new AzureServiceRemoteException("can not get list of azure nodes", e.getCause()); } } @Override - protected void doStart() throws ElasticsearchException { - } + protected void doStart() throws ElasticsearchException {} @Override - protected void doStop() throws ElasticsearchException { - } + protected void doStop() throws ElasticsearchException {} @Override protected void doClose() throws ElasticsearchException { diff --git a/plugins/discovery-azure-classic/src/main/java/org/elasticsearch/discovery/azure/classic/AzureSeedHostsProvider.java b/plugins/discovery-azure-classic/src/main/java/org/elasticsearch/discovery/azure/classic/AzureSeedHostsProvider.java index e86f8c2cc015a..8f29dafe72fc5 100644 --- a/plugins/discovery-azure-classic/src/main/java/org/elasticsearch/discovery/azure/classic/AzureSeedHostsProvider.java +++ b/plugins/discovery-azure-classic/src/main/java/org/elasticsearch/discovery/azure/classic/AzureSeedHostsProvider.java @@ -44,10 +44,10 @@ public enum HostType { PRIVATE_IP("private_ip"), PUBLIC_IP("public_ip"); - private String type ; + private String type; HostType(String type) { - this.type = type ; + this.type = type; } public String getType() { @@ -99,8 +99,12 @@ public static Deployment fromString(String string) { private final String deploymentName; private final DeploymentSlot deploymentSlot; - public AzureSeedHostsProvider(Settings settings, AzureComputeService azureComputeService, - TransportService transportService, NetworkService networkService) { + public AzureSeedHostsProvider( + Settings settings, + AzureComputeService azureComputeService, + TransportService transportService, + NetworkService networkService + ) { this.settings = settings; this.azureComputeService = azureComputeService; this.transportService = transportService; @@ -128,8 +132,8 @@ public AzureSeedHostsProvider(Settings settings, AzureComputeService azureComput @Override public List getSeedAddresses(HostsResolver hostsResolver) { if (refreshInterval.millis() != 0) { - if (dynamicHosts != null && - (refreshInterval.millis() < 0 || (System.currentTimeMillis() - lastRefresh) < refreshInterval.millis())) { + if (dynamicHosts != null + && (refreshInterval.millis() < 0 || (System.currentTimeMillis() - lastRefresh) < refreshInterval.millis())) { logger.trace("using cache to retrieve node list"); return dynamicHosts; } @@ -155,7 +159,8 @@ public List getSeedAddresses(HostsResolver hostsResolver) { InetAddress ipAddress = null; try { ipAddress = networkService.resolvePublishHostAddresses( - NetworkService.GLOBAL_NETWORK_PUBLISH_HOST_SETTING.get(settings).toArray(Strings.EMPTY_ARRAY)); + NetworkService.GLOBAL_NETWORK_PUBLISH_HOST_SETTING.get(settings).toArray(Strings.EMPTY_ARRAY) + ); logger.trace("ip of current node: [{}]", ipAddress); } catch (IOException e) { // We can't find the publish host address... Hmmm. Too bad :-( @@ -165,24 +170,26 @@ public List getSeedAddresses(HostsResolver hostsResolver) { for (HostedServiceGetDetailedResponse.Deployment deployment : detailed.getDeployments()) { // We check the deployment slot if (deployment.getDeploymentSlot() != deploymentSlot) { - logger.debug("current deployment slot [{}] for [{}] is different from [{}]. skipping...", - deployment.getDeploymentSlot(), deployment.getName(), deploymentSlot); + logger.debug( + "current deployment slot [{}] for [{}] is different from [{}]. skipping...", + deployment.getDeploymentSlot(), + deployment.getName(), + deploymentSlot + ); continue; } // If provided, we check the deployment name if (Strings.hasLength(deploymentName) && deploymentName.equals(deployment.getName()) == false) { - logger.debug("current deployment name [{}] different from [{}]. skipping...", - deployment.getName(), deploymentName); + logger.debug("current deployment name [{}] different from [{}]. skipping...", deployment.getName(), deploymentName); continue; } // We check current deployment status - if (deployment.getStatus() != DeploymentStatus.Starting && - deployment.getStatus() != DeploymentStatus.Deploying && - deployment.getStatus() != DeploymentStatus.Running) { - logger.debug("[{}] status is [{}]. skipping...", - deployment.getName(), deployment.getStatus()); + if (deployment.getStatus() != DeploymentStatus.Starting + && deployment.getStatus() != DeploymentStatus.Deploying + && deployment.getStatus() != DeploymentStatus.Running) { + logger.debug("[{}] status is [{}]. skipping...", deployment.getName(), deployment.getStatus()); continue; } diff --git a/plugins/discovery-azure-classic/src/main/java/org/elasticsearch/plugin/discovery/azure/classic/AzureDiscoveryPlugin.java b/plugins/discovery-azure-classic/src/main/java/org/elasticsearch/plugin/discovery/azure/classic/AzureDiscoveryPlugin.java index 5fb40703288b0..cb050b4394403 100644 --- a/plugins/discovery-azure-classic/src/main/java/org/elasticsearch/plugin/discovery/azure/classic/AzureDiscoveryPlugin.java +++ b/plugins/discovery-azure-classic/src/main/java/org/elasticsearch/plugin/discovery/azure/classic/AzureDiscoveryPlugin.java @@ -48,31 +48,36 @@ protected AzureComputeService createComputeService() { } @Override - public Map> getSeedHostProviders(TransportService transportService, - NetworkService networkService) { - return Collections.singletonMap(AZURE, - () -> createSeedHostsProvider(settings, createComputeService(), transportService, networkService)); + public Map> getSeedHostProviders(TransportService transportService, NetworkService networkService) { + return Collections.singletonMap( + AZURE, + () -> createSeedHostsProvider(settings, createComputeService(), transportService, networkService) + ); } // Used for testing - protected AzureSeedHostsProvider createSeedHostsProvider(final Settings settings, - final AzureComputeService azureComputeService, - final TransportService transportService, - final NetworkService networkService) { + protected AzureSeedHostsProvider createSeedHostsProvider( + final Settings settings, + final AzureComputeService azureComputeService, + final TransportService transportService, + final NetworkService networkService + ) { return new AzureSeedHostsProvider(settings, azureComputeService, transportService, networkService); } @Override public List> getSettings() { - return Arrays.asList(AzureComputeService.Discovery.REFRESH_SETTING, - AzureComputeService.Management.KEYSTORE_PASSWORD_SETTING, - AzureComputeService.Management.KEYSTORE_PATH_SETTING, - AzureComputeService.Management.KEYSTORE_TYPE_SETTING, - AzureComputeService.Management.SUBSCRIPTION_ID_SETTING, - AzureComputeService.Management.SERVICE_NAME_SETTING, - AzureComputeService.Discovery.HOST_TYPE_SETTING, - AzureComputeService.Discovery.DEPLOYMENT_NAME_SETTING, - AzureComputeService.Discovery.DEPLOYMENT_SLOT_SETTING, - AzureComputeService.Discovery.ENDPOINT_NAME_SETTING); + return Arrays.asList( + AzureComputeService.Discovery.REFRESH_SETTING, + AzureComputeService.Management.KEYSTORE_PASSWORD_SETTING, + AzureComputeService.Management.KEYSTORE_PATH_SETTING, + AzureComputeService.Management.KEYSTORE_TYPE_SETTING, + AzureComputeService.Management.SUBSCRIPTION_ID_SETTING, + AzureComputeService.Management.SERVICE_NAME_SETTING, + AzureComputeService.Discovery.HOST_TYPE_SETTING, + AzureComputeService.Discovery.DEPLOYMENT_NAME_SETTING, + AzureComputeService.Discovery.DEPLOYMENT_SLOT_SETTING, + AzureComputeService.Discovery.ENDPOINT_NAME_SETTING + ); } } diff --git a/plugins/discovery-azure-classic/src/yamlRestTest/java/org/elasticsearch/discovery/azure/classic/DiscoveryAzureClassicClientYamlTestSuiteIT.java b/plugins/discovery-azure-classic/src/yamlRestTest/java/org/elasticsearch/discovery/azure/classic/DiscoveryAzureClassicClientYamlTestSuiteIT.java index 2d4ec667f23d3..cc20fdbbf5fd3 100644 --- a/plugins/discovery-azure-classic/src/yamlRestTest/java/org/elasticsearch/discovery/azure/classic/DiscoveryAzureClassicClientYamlTestSuiteIT.java +++ b/plugins/discovery-azure-classic/src/yamlRestTest/java/org/elasticsearch/discovery/azure/classic/DiscoveryAzureClassicClientYamlTestSuiteIT.java @@ -25,4 +25,3 @@ public static Iterable parameters() throws Exception { return ESClientYamlSuiteTestCase.createParameters(); } } - diff --git a/plugins/discovery-ec2/qa/amazon-ec2/src/yamlRestTest/java/org/elasticsearch/discovery/ec2/AmazonEC2DiscoveryClientYamlTestSuiteIT.java b/plugins/discovery-ec2/qa/amazon-ec2/src/yamlRestTest/java/org/elasticsearch/discovery/ec2/AmazonEC2DiscoveryClientYamlTestSuiteIT.java index efa3840ceed4a..b5853b9fefc5a 100644 --- a/plugins/discovery-ec2/qa/amazon-ec2/src/yamlRestTest/java/org/elasticsearch/discovery/ec2/AmazonEC2DiscoveryClientYamlTestSuiteIT.java +++ b/plugins/discovery-ec2/qa/amazon-ec2/src/yamlRestTest/java/org/elasticsearch/discovery/ec2/AmazonEC2DiscoveryClientYamlTestSuiteIT.java @@ -10,6 +10,7 @@ import com.carrotsearch.randomizedtesting.annotations.Name; import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; + import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate; import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase; diff --git a/plugins/discovery-ec2/qa/amazon-ec2/src/yamlRestTest/java/org/elasticsearch/discovery/ec2/AmazonEC2Fixture.java b/plugins/discovery-ec2/qa/amazon-ec2/src/yamlRestTest/java/org/elasticsearch/discovery/ec2/AmazonEC2Fixture.java index 0bf74d1c1a6ea..bbe35747c12eb 100644 --- a/plugins/discovery-ec2/qa/amazon-ec2/src/yamlRestTest/java/org/elasticsearch/discovery/ec2/AmazonEC2Fixture.java +++ b/plugins/discovery-ec2/qa/amazon-ec2/src/yamlRestTest/java/org/elasticsearch/discovery/ec2/AmazonEC2Fixture.java @@ -8,6 +8,7 @@ package org.elasticsearch.discovery.ec2; import com.amazonaws.util.DateUtils; + import org.apache.http.NameValuePair; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; @@ -18,9 +19,6 @@ import org.elasticsearch.rest.RestStatus; import org.elasticsearch.test.fixture.AbstractHttpFixture; -import javax.xml.XMLConstants; -import javax.xml.stream.XMLOutputFactory; -import javax.xml.stream.XMLStreamWriter; import java.io.IOException; import java.io.StringWriter; import java.nio.file.Files; @@ -33,6 +31,10 @@ import java.util.UUID; import java.util.concurrent.TimeUnit; +import javax.xml.XMLConstants; +import javax.xml.stream.XMLOutputFactory; +import javax.xml.stream.XMLStreamWriter; + import static java.nio.charset.StandardCharsets.UTF_8; /** @@ -89,32 +91,41 @@ protected Response handle(final Request request) throws IOException { return new Response(RestStatus.OK.getStatus(), TEXT_PLAIN_CONTENT_TYPE, "127.0.0.1".getBytes(UTF_8)); } - if (instanceProfile && - "/latest/meta-data/iam/security-credentials/".equals(request.getPath()) && - HttpGet.METHOD_NAME.equals(request.getMethod())) { + if (instanceProfile + && "/latest/meta-data/iam/security-credentials/".equals(request.getPath()) + && HttpGet.METHOD_NAME.equals(request.getMethod())) { final Map headers = new HashMap<>(contentType("text/plain")); return new Response(RestStatus.OK.getStatus(), headers, "my_iam_profile".getBytes(UTF_8)); } - if (instanceProfile && "/latest/api/token".equals(request.getPath()) - && HttpPut.METHOD_NAME.equals(request.getMethod())) { + if (instanceProfile && "/latest/api/token".equals(request.getPath()) && HttpPut.METHOD_NAME.equals(request.getMethod())) { // TODO: Implement IMDSv2 behavior here. For now this just returns a 403 which makes the SDK fall back to IMDSv1 - // which is implemented in this fixture + // which is implemented in this fixture return new Response(RestStatus.FORBIDDEN.getStatus(), TEXT_PLAIN_CONTENT_TYPE, EMPTY_BYTE); } - if ((containerCredentials && - "/ecs_credentials_endpoint".equals(request.getPath()) && - HttpGet.METHOD_NAME.equals(request.getMethod())) || - ("/latest/meta-data/iam/security-credentials/my_iam_profile".equals(request.getPath()) && - HttpGet.METHOD_NAME.equals(request.getMethod()))) { + if ((containerCredentials + && "/ecs_credentials_endpoint".equals(request.getPath()) + && HttpGet.METHOD_NAME.equals(request.getMethod())) + || ("/latest/meta-data/iam/security-credentials/my_iam_profile".equals(request.getPath()) + && HttpGet.METHOD_NAME.equals(request.getMethod()))) { final Date expiration = new Date(new Date().getTime() + TimeUnit.DAYS.toMillis(1)); final String response = "{" - + "\"AccessKeyId\": \"" + "ec2_integration_test_access_key" + "\"," - + "\"Expiration\": \"" + DateUtils.formatISO8601Date(expiration) + "\"," - + "\"RoleArn\": \"" + "test" + "\"," - + "\"SecretAccessKey\": \"" + "ec2_integration_test_secret_key" + "\"," - + "\"Token\": \"" + "test" + "\"" + + "\"AccessKeyId\": \"" + + "ec2_integration_test_access_key" + + "\"," + + "\"Expiration\": \"" + + DateUtils.formatISO8601Date(expiration) + + "\"," + + "\"RoleArn\": \"" + + "test" + + "\"," + + "\"SecretAccessKey\": \"" + + "ec2_integration_test_secret_key" + + "\"," + + "\"Token\": \"" + + "test" + + "\"" + "}"; final Map headers = new HashMap<>(contentType("application/json")); diff --git a/plugins/discovery-ec2/src/internalClusterTest/java/org/elasticsearch/discovery/ec2/AbstractAwsTestCase.java b/plugins/discovery-ec2/src/internalClusterTest/java/org/elasticsearch/discovery/ec2/AbstractAwsTestCase.java index 93483d8be784b..0f946732f4350 100644 --- a/plugins/discovery-ec2/src/internalClusterTest/java/org/elasticsearch/discovery/ec2/AbstractAwsTestCase.java +++ b/plugins/discovery-ec2/src/internalClusterTest/java/org/elasticsearch/discovery/ec2/AbstractAwsTestCase.java @@ -9,9 +9,9 @@ package org.elasticsearch.discovery.ec2; import org.elasticsearch.common.Strings; -import org.elasticsearch.core.PathUtils; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsException; +import org.elasticsearch.core.PathUtils; import org.elasticsearch.env.Environment; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.test.ESIntegTestCase; @@ -32,9 +32,9 @@ public abstract class AbstractAwsTestCase extends ESIntegTestCase { @Override protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { - Settings.Builder settings = Settings.builder() - .put(super.nodeSettings(nodeOrdinal, otherSettings)) - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()); + Settings.Builder settings = Settings.builder() + .put(super.nodeSettings(nodeOrdinal, otherSettings)) + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()); // if explicit, just load it and don't load from env try { @@ -46,7 +46,8 @@ protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { } } else { throw new IllegalStateException( - "to run integration tests, you need to set -Dtests.thirdparty=true and -Dtests.config=/path/to/elasticsearch.yml"); + "to run integration tests, you need to set -Dtests.thirdparty=true and -Dtests.config=/path/to/elasticsearch.yml" + ); } } catch (SettingsException exception) { throw new IllegalStateException("your test configuration file is incorrect: " + System.getProperty("tests.config"), exception); diff --git a/plugins/discovery-ec2/src/internalClusterTest/java/org/elasticsearch/discovery/ec2/Ec2DiscoveryUpdateSettingsTests.java b/plugins/discovery-ec2/src/internalClusterTest/java/org/elasticsearch/discovery/ec2/Ec2DiscoveryUpdateSettingsTests.java index d3f03523eab59..c77d1e57e2c8a 100644 --- a/plugins/discovery-ec2/src/internalClusterTest/java/org/elasticsearch/discovery/ec2/Ec2DiscoveryUpdateSettingsTests.java +++ b/plugins/discovery-ec2/src/internalClusterTest/java/org/elasticsearch/discovery/ec2/Ec2DiscoveryUpdateSettingsTests.java @@ -8,7 +8,6 @@ package org.elasticsearch.discovery.ec2; - import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsResponse; import org.elasticsearch.common.UUIDs; import org.elasticsearch.common.settings.Settings; @@ -26,17 +25,17 @@ @ClusterScope(scope = Scope.TEST, numDataNodes = 0, numClientNodes = 0) public class Ec2DiscoveryUpdateSettingsTests extends AbstractAwsTestCase { public void testMinimumMasterNodesStart() { - Settings nodeSettings = Settings.builder() - .put(DiscoveryModule.DISCOVERY_SEED_PROVIDERS_SETTING.getKey(), "ec2") - .build(); + Settings nodeSettings = Settings.builder().put(DiscoveryModule.DISCOVERY_SEED_PROVIDERS_SETTING.getKey(), "ec2").build(); internalCluster().startNode(nodeSettings); // We try to update a setting now final String expectedValue = UUIDs.randomBase64UUID(random()); final String settingName = "cluster.routing.allocation.exclude.any_attribute"; - final ClusterUpdateSettingsResponse response = client().admin().cluster().prepareUpdateSettings() - .setPersistentSettings(Settings.builder().put(settingName, expectedValue)) - .get(); + final ClusterUpdateSettingsResponse response = client().admin() + .cluster() + .prepareUpdateSettings() + .setPersistentSettings(Settings.builder().put(settingName, expectedValue)) + .get(); final String value = response.getPersistentSettings().get(settingName); assertThat(value, is(expectedValue)); diff --git a/plugins/discovery-ec2/src/main/java/org/elasticsearch/discovery/ec2/AmazonEc2Reference.java b/plugins/discovery-ec2/src/main/java/org/elasticsearch/discovery/ec2/AmazonEc2Reference.java index 6000fd7b6731e..b4d40dd009888 100644 --- a/plugins/discovery-ec2/src/main/java/org/elasticsearch/discovery/ec2/AmazonEc2Reference.java +++ b/plugins/discovery-ec2/src/main/java/org/elasticsearch/discovery/ec2/AmazonEc2Reference.java @@ -10,8 +10,8 @@ import com.amazonaws.services.ec2.AmazonEC2; -import org.elasticsearch.core.Releasable; import org.elasticsearch.core.AbstractRefCounted; +import org.elasticsearch.core.Releasable; /** * Handles the shutdown of the wrapped {@link AmazonEC2} using reference diff --git a/plugins/discovery-ec2/src/main/java/org/elasticsearch/discovery/ec2/AwsEc2SeedHostsProvider.java b/plugins/discovery-ec2/src/main/java/org/elasticsearch/discovery/ec2/AwsEc2SeedHostsProvider.java index c9a6a4266ef72..25ae453d96ab6 100644 --- a/plugins/discovery-ec2/src/main/java/org/elasticsearch/discovery/ec2/AwsEc2SeedHostsProvider.java +++ b/plugins/discovery-ec2/src/main/java/org/elasticsearch/discovery/ec2/AwsEc2SeedHostsProvider.java @@ -23,8 +23,8 @@ import org.apache.logging.log4j.util.Supplier; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.TransportAddress; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.SingleObjectCache; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.discovery.SeedHostsProvider; import org.elasticsearch.transport.TransportService; @@ -78,8 +78,14 @@ class AwsEc2SeedHostsProvider implements SeedHostsProvider { availabilityZones.addAll(AwsEc2Service.AVAILABILITY_ZONES_SETTING.get(settings)); if (logger.isDebugEnabled()) { - logger.debug("using host_type [{}], tags [{}], groups [{}] with any_group [{}], availability_zones [{}]", hostType, tags, - groups, bindAnyGroup, availabilityZones); + logger.debug( + "using host_type [{}], tags [{}], groups [{}] with any_group [{}], availability_zones [{}]", + hostType, + tags, + groups, + bindAnyGroup, + availabilityZones + ); } } @@ -120,18 +126,25 @@ protected List fetchDynamicNodes() { } if (bindAnyGroup) { // We check if we can find at least one group name or one group id in groups. - if (disjoint(securityGroupNames, groups) - && disjoint(securityGroupIds, groups)) { - logger.trace("filtering out instance {} based on groups {}, not part of {}", instance.getInstanceId(), - instanceSecurityGroups, groups); + if (disjoint(securityGroupNames, groups) && disjoint(securityGroupIds, groups)) { + logger.trace( + "filtering out instance {} based on groups {}, not part of {}", + instance.getInstanceId(), + instanceSecurityGroups, + groups + ); // continue to the next instance continue; } } else { // We need tp match all group names or group ids, otherwise we ignore this instance if ((securityGroupNames.containsAll(groups) || securityGroupIds.containsAll(groups)) == false) { - logger.trace("filtering out instance {} based on groups {}, does not include all of {}", - instance.getInstanceId(), instanceSecurityGroups, groups); + logger.trace( + "filtering out instance {} based on groups {}, does not include all of {}", + instance.getInstanceId(), + instanceSecurityGroups, + groups + ); // continue to the next instance continue; } @@ -171,8 +184,13 @@ && disjoint(securityGroupIds, groups)) { } catch (final Exception e) { final String finalAddress = address; logger.warn( - (Supplier) - () -> new ParameterizedMessage("failed to add {}, address {}", instance.getInstanceId(), finalAddress), e); + (Supplier) () -> new ParameterizedMessage( + "failed to add {}, address {}", + instance.getInstanceId(), + finalAddress + ), + e + ); } } else { logger.trace("not adding {}, address is null, host_type {}", instance.getInstanceId(), hostType); @@ -186,23 +204,18 @@ && disjoint(securityGroupIds, groups)) { } private DescribeInstancesRequest buildDescribeInstancesRequest() { - final DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest() - .withFilters( - new Filter("instance-state-name").withValues("running", "pending") - ); + final DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest().withFilters( + new Filter("instance-state-name").withValues("running", "pending") + ); for (final Map.Entry> tagFilter : tags.entrySet()) { // for a given tag key, OR relationship for multiple different values - describeInstancesRequest.withFilters( - new Filter("tag:" + tagFilter.getKey()).withValues(tagFilter.getValue()) - ); + describeInstancesRequest.withFilters(new Filter("tag:" + tagFilter.getKey()).withValues(tagFilter.getValue())); } if (availabilityZones.isEmpty() == false) { // OR relationship amongst multiple values of the availability-zone filter - describeInstancesRequest.withFilters( - new Filter("availability-zone").withValues(availabilityZones) - ); + describeInstancesRequest.withFilters(new Filter("availability-zone").withValues(availabilityZones)); } return describeInstancesRequest; @@ -211,7 +224,7 @@ private DescribeInstancesRequest buildDescribeInstancesRequest() { private final class TransportAddressesCache extends SingleObjectCache> { protected TransportAddressesCache(TimeValue refreshInterval) { - super(refreshInterval, new ArrayList<>()); + super(refreshInterval, new ArrayList<>()); } @Override diff --git a/plugins/discovery-ec2/src/main/java/org/elasticsearch/discovery/ec2/AwsEc2Service.java b/plugins/discovery-ec2/src/main/java/org/elasticsearch/discovery/ec2/AwsEc2Service.java index a73e56fd819b1..92011e166c0c1 100644 --- a/plugins/discovery-ec2/src/main/java/org/elasticsearch/discovery/ec2/AwsEc2Service.java +++ b/plugins/discovery-ec2/src/main/java/org/elasticsearch/discovery/ec2/AwsEc2Service.java @@ -35,8 +35,12 @@ class HostType { * XXXX refers to a name of a tag configured for all EC2 instances. Instances which don't * have this tag set will be ignored by the discovery process. Defaults to private_ip. */ - Setting HOST_TYPE_SETTING = - new Setting<>("discovery.ec2.host_type", HostType.PRIVATE_IP, Function.identity(), Property.NodeScope); + Setting HOST_TYPE_SETTING = new Setting<>( + "discovery.ec2.host_type", + HostType.PRIVATE_IP, + Function.identity(), + Property.NodeScope + ); /** * discovery.ec2.any_group: If set to false, will require all security groups to be present for the instance to be used for the * discovery. Defaults to true. @@ -46,19 +50,30 @@ class HostType { * discovery.ec2.groups: Either a comma separated list or array based list of (security) groups. Only instances with the provided * security groups will be used in the cluster discovery. (NOTE: You could provide either group NAME or group ID.) */ - Setting> GROUPS_SETTING = Setting.listSetting("discovery.ec2.groups", new ArrayList<>(), s -> s.toString(), - Property.NodeScope); + Setting> GROUPS_SETTING = Setting.listSetting( + "discovery.ec2.groups", + new ArrayList<>(), + s -> s.toString(), + Property.NodeScope + ); /** * discovery.ec2.availability_zones: Either a comma separated list or array based list of availability zones. Only instances within * the provided availability zones will be used in the cluster discovery. */ - Setting> AVAILABILITY_ZONES_SETTING = Setting.listSetting("discovery.ec2.availability_zones", Collections.emptyList(), - s -> s.toString(), Property.NodeScope); + Setting> AVAILABILITY_ZONES_SETTING = Setting.listSetting( + "discovery.ec2.availability_zones", + Collections.emptyList(), + s -> s.toString(), + Property.NodeScope + ); /** * discovery.ec2.node_cache_time: How long the list of hosts is cached to prevent further requests to the AWS API. Defaults to 10s. */ - Setting NODE_CACHE_TIME_SETTING = Setting.timeSetting("discovery.ec2.node_cache_time", TimeValue.timeValueSeconds(10), - Property.NodeScope); + Setting NODE_CACHE_TIME_SETTING = Setting.timeSetting( + "discovery.ec2.node_cache_time", + TimeValue.timeValueSeconds(10), + Property.NodeScope + ); /** * discovery.ec2.tag.*: The ec2 discovery can filter machines to include in the cluster based on tags (and not just groups). @@ -66,8 +81,10 @@ class HostType { * instances with a tag key set to stage, and a value of dev. Several tags set will require all of those tags to be set for the * instance to be included. */ - Setting.AffixSetting> TAG_SETTING = Setting.prefixKeySetting("discovery.ec2.tag.", - key -> Setting.listSetting(key, Collections.emptyList(), Function.identity(), Property.NodeScope)); + Setting.AffixSetting> TAG_SETTING = Setting.prefixKeySetting( + "discovery.ec2.tag.", + key -> Setting.listSetting(key, Collections.emptyList(), Function.identity(), Property.NodeScope) + ); /** * Builds then caches an {@code AmazonEC2} client using the current client diff --git a/plugins/discovery-ec2/src/main/java/org/elasticsearch/discovery/ec2/AwsEc2ServiceImpl.java b/plugins/discovery-ec2/src/main/java/org/elasticsearch/discovery/ec2/AwsEc2ServiceImpl.java index 8a2c2715e136d..d174aeece609d 100644 --- a/plugins/discovery-ec2/src/main/java/org/elasticsearch/discovery/ec2/AwsEc2ServiceImpl.java +++ b/plugins/discovery-ec2/src/main/java/org/elasticsearch/discovery/ec2/AwsEc2ServiceImpl.java @@ -17,6 +17,7 @@ import com.amazonaws.http.IdleConnectionReaper; import com.amazonaws.services.ec2.AmazonEC2; import com.amazonaws.services.ec2.AmazonEC2ClientBuilder; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.ElasticsearchException; @@ -30,7 +31,7 @@ class AwsEc2ServiceImpl implements AwsEc2Service { private static final Logger logger = LogManager.getLogger(AwsEc2ServiceImpl.class); private final AtomicReference> lazyClientReference = - new AtomicReference<>(); + new AtomicReference<>(); private AmazonEC2 buildClient(Ec2ClientSettings clientSettings) { final AWSCredentialsProvider credentials = buildCredentials(logger, clientSettings); @@ -40,7 +41,8 @@ private AmazonEC2 buildClient(Ec2ClientSettings clientSettings) { // proxy for testing AmazonEC2 buildClient(AWSCredentialsProvider credentials, ClientConfiguration configuration, String endpoint) { - final AmazonEC2ClientBuilder builder = AmazonEC2ClientBuilder.standard().withCredentials(credentials) + final AmazonEC2ClientBuilder builder = AmazonEC2ClientBuilder.standard() + .withCredentials(credentials) .withClientConfiguration(configuration); if (Strings.hasText(endpoint)) { logger.debug("using explicit ec2 endpoint [{}]", endpoint); @@ -98,8 +100,10 @@ public AmazonEc2Reference client() { @Override public void refreshAndClearCache(Ec2ClientSettings clientSettings) { final LazyInitializable newClient = new LazyInitializable<>( - () -> new AmazonEc2Reference(buildClient(clientSettings)), clientReference -> clientReference.incRef(), - clientReference -> clientReference.decRef()); + () -> new AmazonEc2Reference(buildClient(clientSettings)), + clientReference -> clientReference.incRef(), + clientReference -> clientReference.decRef() + ); final LazyInitializable oldClient = this.lazyClientReference.getAndSet(newClient); if (oldClient != null) { oldClient.reset(); diff --git a/plugins/discovery-ec2/src/main/java/org/elasticsearch/discovery/ec2/Ec2ClientSettings.java b/plugins/discovery-ec2/src/main/java/org/elasticsearch/discovery/ec2/Ec2ClientSettings.java index 83949291760c5..1d13cbbd13ecb 100644 --- a/plugins/discovery-ec2/src/main/java/org/elasticsearch/discovery/ec2/Ec2ClientSettings.java +++ b/plugins/discovery-ec2/src/main/java/org/elasticsearch/discovery/ec2/Ec2ClientSettings.java @@ -13,6 +13,7 @@ import com.amazonaws.auth.AWSCredentials; import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.auth.BasicSessionCredentials; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.common.logging.DeprecationCategory; @@ -48,12 +49,20 @@ final class Ec2ClientSettings { static final Setting PROXY_PORT_SETTING = Setting.intSetting("discovery.ec2.proxy.port", 80, 0, 1 << 16, Property.NodeScope); /** An override for the ec2 endpoint to connect to. */ - static final Setting ENDPOINT_SETTING = new Setting<>("discovery.ec2.endpoint", "", s -> s.toLowerCase(Locale.ROOT), - Property.NodeScope); + static final Setting ENDPOINT_SETTING = new Setting<>( + "discovery.ec2.endpoint", + "", + s -> s.toLowerCase(Locale.ROOT), + Property.NodeScope + ); /** The protocol to use to connect to to ec2. */ - static final Setting PROTOCOL_SETTING = new Setting<>("discovery.ec2.protocol", "https", - s -> Protocol.valueOf(s.toUpperCase(Locale.ROOT)), Property.NodeScope); + static final Setting PROTOCOL_SETTING = new Setting<>( + "discovery.ec2.protocol", + "https", + s -> Protocol.valueOf(s.toUpperCase(Locale.ROOT)), + Property.NodeScope + ); /** The username of a proxy to connect to s3 through. */ static final Setting PROXY_USERNAME_SETTING = SecureSetting.secureString("discovery.ec2.proxy.username", null); @@ -62,8 +71,11 @@ final class Ec2ClientSettings { static final Setting PROXY_PASSWORD_SETTING = SecureSetting.secureString("discovery.ec2.proxy.password", null); /** The socket timeout for connecting to s3. */ - static final Setting READ_TIMEOUT_SETTING = Setting.timeSetting("discovery.ec2.read_timeout", - TimeValue.timeValueMillis(ClientConfiguration.DEFAULT_SOCKET_TIMEOUT), Property.NodeScope); + static final Setting READ_TIMEOUT_SETTING = Setting.timeSetting( + "discovery.ec2.read_timeout", + TimeValue.timeValueMillis(ClientConfiguration.DEFAULT_SOCKET_TIMEOUT), + Property.NodeScope + ); private static final Logger logger = LogManager.getLogger(Ec2ClientSettings.class); @@ -99,8 +111,16 @@ final class Ec2ClientSettings { /** The read timeout for the ec2 client. */ final int readTimeoutMillis; - protected Ec2ClientSettings(AWSCredentials credentials, String endpoint, Protocol protocol, String proxyHost, int proxyPort, - String proxyUsername, String proxyPassword, int readTimeoutMillis) { + protected Ec2ClientSettings( + AWSCredentials credentials, + String endpoint, + Protocol protocol, + String proxyHost, + int proxyPort, + String proxyUsername, + String proxyPassword, + int readTimeoutMillis + ) { this.credentials = credentials; this.endpoint = endpoint; this.protocol = protocol; @@ -112,27 +132,41 @@ protected Ec2ClientSettings(AWSCredentials credentials, String endpoint, Protoco } static AWSCredentials loadCredentials(Settings settings) { - try (SecureString key = ACCESS_KEY_SETTING.get(settings); - SecureString secret = SECRET_KEY_SETTING.get(settings); - SecureString sessionToken = SESSION_TOKEN_SETTING.get(settings)) { + try ( + SecureString key = ACCESS_KEY_SETTING.get(settings); + SecureString secret = SECRET_KEY_SETTING.get(settings); + SecureString sessionToken = SESSION_TOKEN_SETTING.get(settings) + ) { if (key.length() == 0 && secret.length() == 0) { if (sessionToken.length() > 0) { - throw new SettingsException("Setting [{}] is set but [{}] and [{}] are not", - SESSION_TOKEN_SETTING.getKey(), ACCESS_KEY_SETTING.getKey(), SECRET_KEY_SETTING.getKey()); + throw new SettingsException( + "Setting [{}] is set but [{}] and [{}] are not", + SESSION_TOKEN_SETTING.getKey(), + ACCESS_KEY_SETTING.getKey(), + SECRET_KEY_SETTING.getKey() + ); } logger.debug("Using either environment variables, system properties or instance profile credentials"); return null; } else { if (key.length() == 0) { - deprecationLogger.critical(DeprecationCategory.SETTINGS, "ec2_invalid_settings", + deprecationLogger.critical( + DeprecationCategory.SETTINGS, + "ec2_invalid_settings", "Setting [{}] is set but [{}] is not, which will be unsupported in future", - SECRET_KEY_SETTING.getKey(), ACCESS_KEY_SETTING.getKey()); + SECRET_KEY_SETTING.getKey(), + ACCESS_KEY_SETTING.getKey() + ); } if (secret.length() == 0) { - deprecationLogger.critical(DeprecationCategory.SETTINGS, "ec2_invalid_settings", + deprecationLogger.critical( + DeprecationCategory.SETTINGS, + "ec2_invalid_settings", "Setting [{}] is set but [{}] is not, which will be unsupported in future", - ACCESS_KEY_SETTING.getKey(), SECRET_KEY_SETTING.getKey()); + ACCESS_KEY_SETTING.getKey(), + SECRET_KEY_SETTING.getKey() + ); } final AWSCredentials credentials; @@ -152,8 +186,10 @@ static AWSCredentials loadCredentials(Settings settings) { /** Parse settings for a single client. */ static Ec2ClientSettings getClientSettings(Settings settings) { final AWSCredentials credentials = loadCredentials(settings); - try (SecureString proxyUsername = PROXY_USERNAME_SETTING.get(settings); - SecureString proxyPassword = PROXY_PASSWORD_SETTING.get(settings)) { + try ( + SecureString proxyUsername = PROXY_USERNAME_SETTING.get(settings); + SecureString proxyPassword = PROXY_PASSWORD_SETTING.get(settings) + ) { return new Ec2ClientSettings( credentials, ENDPOINT_SETTING.get(settings), @@ -162,7 +198,8 @@ static Ec2ClientSettings getClientSettings(Settings settings) { PROXY_PORT_SETTING.get(settings), proxyUsername.toString(), proxyPassword.toString(), - (int)READ_TIMEOUT_SETTING.get(settings).millis()); + (int) READ_TIMEOUT_SETTING.get(settings).millis() + ); } } diff --git a/plugins/discovery-ec2/src/main/java/org/elasticsearch/discovery/ec2/Ec2DiscoveryPlugin.java b/plugins/discovery-ec2/src/main/java/org/elasticsearch/discovery/ec2/Ec2DiscoveryPlugin.java index 2c839533fa0ba..91ab319b121eb 100644 --- a/plugins/discovery-ec2/src/main/java/org/elasticsearch/discovery/ec2/Ec2DiscoveryPlugin.java +++ b/plugins/discovery-ec2/src/main/java/org/elasticsearch/discovery/ec2/Ec2DiscoveryPlugin.java @@ -10,13 +10,14 @@ import com.amazonaws.util.EC2MetadataUtils; import com.amazonaws.util.json.Jackson; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.SpecialPermission; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.common.network.NetworkService; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.discovery.SeedHostsProvider; import org.elasticsearch.node.Node; import org.elasticsearch.plugins.DiscoveryPlugin; @@ -85,33 +86,33 @@ public NetworkService.CustomNameResolver getCustomNameResolver(Settings settings } @Override - public Map> getSeedHostProviders(TransportService transportService, - NetworkService networkService) { + public Map> getSeedHostProviders(TransportService transportService, NetworkService networkService) { return Collections.singletonMap(EC2, () -> new AwsEc2SeedHostsProvider(settings, transportService, ec2Service)); } @Override public List> getSettings() { return Arrays.asList( - // Register EC2 discovery settings: discovery.ec2 - Ec2ClientSettings.ACCESS_KEY_SETTING, - Ec2ClientSettings.SECRET_KEY_SETTING, - Ec2ClientSettings.SESSION_TOKEN_SETTING, - Ec2ClientSettings.ENDPOINT_SETTING, - Ec2ClientSettings.PROTOCOL_SETTING, - Ec2ClientSettings.PROXY_HOST_SETTING, - Ec2ClientSettings.PROXY_PORT_SETTING, - Ec2ClientSettings.PROXY_USERNAME_SETTING, - Ec2ClientSettings.PROXY_PASSWORD_SETTING, - Ec2ClientSettings.READ_TIMEOUT_SETTING, - AwsEc2Service.HOST_TYPE_SETTING, - AwsEc2Service.ANY_GROUP_SETTING, - AwsEc2Service.GROUPS_SETTING, - AwsEc2Service.AVAILABILITY_ZONES_SETTING, - AwsEc2Service.NODE_CACHE_TIME_SETTING, - AwsEc2Service.TAG_SETTING, - // Register cloud node settings: cloud.node - AwsEc2Service.AUTO_ATTRIBUTE_SETTING); + // Register EC2 discovery settings: discovery.ec2 + Ec2ClientSettings.ACCESS_KEY_SETTING, + Ec2ClientSettings.SECRET_KEY_SETTING, + Ec2ClientSettings.SESSION_TOKEN_SETTING, + Ec2ClientSettings.ENDPOINT_SETTING, + Ec2ClientSettings.PROTOCOL_SETTING, + Ec2ClientSettings.PROXY_HOST_SETTING, + Ec2ClientSettings.PROXY_PORT_SETTING, + Ec2ClientSettings.PROXY_USERNAME_SETTING, + Ec2ClientSettings.PROXY_PASSWORD_SETTING, + Ec2ClientSettings.READ_TIMEOUT_SETTING, + AwsEc2Service.HOST_TYPE_SETTING, + AwsEc2Service.ANY_GROUP_SETTING, + AwsEc2Service.GROUPS_SETTING, + AwsEc2Service.AVAILABILITY_ZONES_SETTING, + AwsEc2Service.NODE_CACHE_TIME_SETTING, + AwsEc2Service.TAG_SETTING, + // Register cloud node settings: cloud.node + AwsEc2Service.AUTO_ATTRIBUTE_SETTING + ); } @Override @@ -145,8 +146,10 @@ static Settings getAvailabilityZoneNodeAttributes(Settings settings, String azMe throw new UncheckedIOException(e); } - try (InputStream in = SocketAccess.doPrivilegedIOException(urlConnection::getInputStream); - BufferedReader urlReader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))) { + try ( + InputStream in = SocketAccess.doPrivilegedIOException(urlConnection::getInputStream); + BufferedReader urlReader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8)) + ) { final String metadataResult = urlReader.readLine(); if ((metadataResult == null) || (metadataResult.length() == 0)) { diff --git a/plugins/discovery-ec2/src/main/java/org/elasticsearch/discovery/ec2/Ec2NameResolver.java b/plugins/discovery-ec2/src/main/java/org/elasticsearch/discovery/ec2/Ec2NameResolver.java index c54156bbc3e75..7c98d76bdf527 100644 --- a/plugins/discovery-ec2/src/main/java/org/elasticsearch/discovery/ec2/Ec2NameResolver.java +++ b/plugins/discovery-ec2/src/main/java/org/elasticsearch/discovery/ec2/Ec2NameResolver.java @@ -12,8 +12,8 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.common.network.NetworkService.CustomNameResolver; +import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.core.internal.io.IOUtils; import java.io.BufferedReader; @@ -105,7 +105,7 @@ public InetAddress[] resolve(Ec2HostnameType type) throws IOException { @Override public InetAddress[] resolveDefault() { return null; // using this, one has to explicitly specify _ec2_ in network setting -// return resolve(Ec2HostnameType.DEFAULT, false); + // return resolve(Ec2HostnameType.DEFAULT, false); } @Override diff --git a/plugins/discovery-ec2/src/test/java/org/elasticsearch/discovery/ec2/AbstractEC2MockAPITestCase.java b/plugins/discovery-ec2/src/test/java/org/elasticsearch/discovery/ec2/AbstractEC2MockAPITestCase.java index da9afc545c507..1f08d7cada38c 100644 --- a/plugins/discovery-ec2/src/test/java/org/elasticsearch/discovery/ec2/AbstractEC2MockAPITestCase.java +++ b/plugins/discovery-ec2/src/test/java/org/elasticsearch/discovery/ec2/AbstractEC2MockAPITestCase.java @@ -10,11 +10,12 @@ import com.amazonaws.services.ec2.model.Instance; import com.amazonaws.services.ec2.model.Tag; import com.sun.net.httpserver.HttpServer; -import org.elasticsearch.core.SuppressForbidden; + import org.elasticsearch.common.network.InetAddresses; import org.elasticsearch.common.network.NetworkService; import org.elasticsearch.common.settings.MockSecureSettings; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.mocksocket.MockHttpServer; import org.elasticsearch.test.ESTestCase; @@ -24,10 +25,6 @@ import org.junit.After; import org.junit.Before; -import javax.xml.XMLConstants; -import javax.xml.stream.XMLOutputFactory; -import javax.xml.stream.XMLStreamWriter; - import java.io.StringWriter; import java.net.InetAddress; import java.net.InetSocketAddress; @@ -35,6 +32,10 @@ import java.util.List; import java.util.UUID; +import javax.xml.XMLConstants; +import javax.xml.stream.XMLOutputFactory; +import javax.xml.stream.XMLStreamWriter; + import static java.nio.charset.StandardCharsets.UTF_8; @SuppressForbidden(reason = "use a http server") diff --git a/plugins/discovery-ec2/src/test/java/org/elasticsearch/discovery/ec2/AwsEc2ServiceImplTests.java b/plugins/discovery-ec2/src/test/java/org/elasticsearch/discovery/ec2/AwsEc2ServiceImplTests.java index bf7c363d94ae7..18147f77b5809 100644 --- a/plugins/discovery-ec2/src/test/java/org/elasticsearch/discovery/ec2/AwsEc2ServiceImplTests.java +++ b/plugins/discovery-ec2/src/test/java/org/elasticsearch/discovery/ec2/AwsEc2ServiceImplTests.java @@ -28,8 +28,10 @@ public class AwsEc2ServiceImplTests extends ESTestCase { public void testAWSCredentialsWithSystemProviders() { - final AWSCredentialsProvider credentialsProvider = AwsEc2ServiceImpl.buildCredentials(logger, - Ec2ClientSettings.getClientSettings(Settings.EMPTY)); + final AWSCredentialsProvider credentialsProvider = AwsEc2ServiceImpl.buildCredentials( + logger, + Ec2ClientSettings.getClientSettings(Settings.EMPTY) + ); assertThat(credentialsProvider, instanceOf(DefaultAWSCredentialsProviderChain.class)); } @@ -37,8 +39,10 @@ public void testAWSCredentialsWithElasticsearchAwsSettings() { final MockSecureSettings secureSettings = new MockSecureSettings(); secureSettings.setString("discovery.ec2.access_key", "aws_key"); secureSettings.setString("discovery.ec2.secret_key", "aws_secret"); - final AWSCredentials credentials = AwsEc2ServiceImpl.buildCredentials(logger, - Ec2ClientSettings.getClientSettings(Settings.builder().setSecureSettings(secureSettings).build())).getCredentials(); + final AWSCredentials credentials = AwsEc2ServiceImpl.buildCredentials( + logger, + Ec2ClientSettings.getClientSettings(Settings.builder().setSecureSettings(secureSettings).build()) + ).getCredentials(); assertThat(credentials.getAWSAccessKeyId(), is("aws_key")); assertThat(credentials.getAWSSecretKey(), is("aws_secret")); } @@ -48,8 +52,10 @@ public void testAWSSessionCredentialsWithElasticsearchAwsSettings() { secureSettings.setString("discovery.ec2.access_key", "aws_key"); secureSettings.setString("discovery.ec2.secret_key", "aws_secret"); secureSettings.setString("discovery.ec2.session_token", "aws_session_token"); - final BasicSessionCredentials credentials = (BasicSessionCredentials) AwsEc2ServiceImpl.buildCredentials(logger, - Ec2ClientSettings.getClientSettings(Settings.builder().setSecureSettings(secureSettings).build())).getCredentials(); + final BasicSessionCredentials credentials = (BasicSessionCredentials) AwsEc2ServiceImpl.buildCredentials( + logger, + Ec2ClientSettings.getClientSettings(Settings.builder().setSecureSettings(secureSettings).build()) + ).getCredentials(); assertThat(credentials.getAWSAccessKeyId(), is("aws_key")); assertThat(credentials.getAWSSecretKey(), is("aws_secret")); assertThat(credentials.getSessionToken(), is("aws_session_token")); @@ -58,39 +64,57 @@ public void testAWSSessionCredentialsWithElasticsearchAwsSettings() { public void testDeprecationOfLoneAccessKey() { final MockSecureSettings secureSettings = new MockSecureSettings(); secureSettings.setString("discovery.ec2.access_key", "aws_key"); - final AWSCredentials credentials = AwsEc2ServiceImpl.buildCredentials(logger, - Ec2ClientSettings.getClientSettings(Settings.builder().setSecureSettings(secureSettings).build())).getCredentials(); + final AWSCredentials credentials = AwsEc2ServiceImpl.buildCredentials( + logger, + Ec2ClientSettings.getClientSettings(Settings.builder().setSecureSettings(secureSettings).build()) + ).getCredentials(); assertThat(credentials.getAWSAccessKeyId(), is("aws_key")); assertThat(credentials.getAWSSecretKey(), is("")); - assertSettingDeprecationsAndWarnings(new Setting[]{}, - new DeprecationWarning(DeprecationLogger.CRITICAL, "Setting [discovery.ec2.access_key] is set but " + - "[discovery.ec2.secret_key] is not, which will be unsupported in future")); + assertSettingDeprecationsAndWarnings( + new Setting[] {}, + new DeprecationWarning( + DeprecationLogger.CRITICAL, + "Setting [discovery.ec2.access_key] is set but " + "[discovery.ec2.secret_key] is not, which will be unsupported in future" + ) + ); } public void testDeprecationOfLoneSecretKey() { final MockSecureSettings secureSettings = new MockSecureSettings(); secureSettings.setString("discovery.ec2.secret_key", "aws_secret"); - final AWSCredentials credentials = AwsEc2ServiceImpl.buildCredentials(logger, - Ec2ClientSettings.getClientSettings(Settings.builder().setSecureSettings(secureSettings).build())).getCredentials(); + final AWSCredentials credentials = AwsEc2ServiceImpl.buildCredentials( + logger, + Ec2ClientSettings.getClientSettings(Settings.builder().setSecureSettings(secureSettings).build()) + ).getCredentials(); assertThat(credentials.getAWSAccessKeyId(), is("")); assertThat(credentials.getAWSSecretKey(), is("aws_secret")); - assertSettingDeprecationsAndWarnings(new Setting[]{}, - new DeprecationWarning(DeprecationLogger.CRITICAL, "Setting [discovery.ec2.secret_key] is set but " + - "[discovery.ec2.access_key] is not, which will be unsupported in future")); + assertSettingDeprecationsAndWarnings( + new Setting[] {}, + new DeprecationWarning( + DeprecationLogger.CRITICAL, + "Setting [discovery.ec2.secret_key] is set but " + "[discovery.ec2.access_key] is not, which will be unsupported in future" + ) + ); } public void testRejectionOfLoneSessionToken() { final MockSecureSettings secureSettings = new MockSecureSettings(); secureSettings.setString("discovery.ec2.session_token", "aws_session_token"); - SettingsException e = expectThrows(SettingsException.class, () -> AwsEc2ServiceImpl.buildCredentials(logger, - Ec2ClientSettings.getClientSettings(Settings.builder().setSecureSettings(secureSettings).build()))); - assertThat(e.getMessage(), is( - "Setting [discovery.ec2.session_token] is set but [discovery.ec2.access_key] and [discovery.ec2.secret_key] are not")); + SettingsException e = expectThrows( + SettingsException.class, + () -> AwsEc2ServiceImpl.buildCredentials( + logger, + Ec2ClientSettings.getClientSettings(Settings.builder().setSecureSettings(secureSettings).build()) + ) + ); + assertThat( + e.getMessage(), + is("Setting [discovery.ec2.session_token] is set but [discovery.ec2.access_key] and [discovery.ec2.secret_key] are not") + ); } public void testAWSDefaultConfiguration() { - launchAWSConfigurationTest(Settings.EMPTY, Protocol.HTTPS, null, -1, null, null, - ClientConfiguration.DEFAULT_SOCKET_TIMEOUT); + launchAWSConfigurationTest(Settings.EMPTY, Protocol.HTTPS, null, -1, null, null, ClientConfiguration.DEFAULT_SOCKET_TIMEOUT); } public void testAWSConfigurationWithAwsSettings() { @@ -107,15 +131,16 @@ public void testAWSConfigurationWithAwsSettings() { launchAWSConfigurationTest(settings, Protocol.HTTP, "aws_proxy_host", 8080, "aws_proxy_username", "aws_proxy_password", 10000); } - protected void launchAWSConfigurationTest(Settings settings, - Protocol expectedProtocol, - String expectedProxyHost, - int expectedProxyPort, - String expectedProxyUsername, - String expectedProxyPassword, - int expectedReadTimeout) { - final ClientConfiguration configuration = AwsEc2ServiceImpl.buildConfiguration( - Ec2ClientSettings.getClientSettings(settings)); + protected void launchAWSConfigurationTest( + Settings settings, + Protocol expectedProtocol, + String expectedProxyHost, + int expectedProxyPort, + String expectedProxyUsername, + String expectedProxyPassword, + int expectedReadTimeout + ) { + final ClientConfiguration configuration = AwsEc2ServiceImpl.buildConfiguration(Ec2ClientSettings.getClientSettings(settings)); assertThat(configuration.getResponseMetadataCacheSize(), is(0)); assertThat(configuration.getProtocol(), is(expectedProtocol)); diff --git a/plugins/discovery-ec2/src/test/java/org/elasticsearch/discovery/ec2/EC2RetriesTests.java b/plugins/discovery-ec2/src/test/java/org/elasticsearch/discovery/ec2/EC2RetriesTests.java index 60c133d809d3c..2a30a3c68979f 100644 --- a/plugins/discovery-ec2/src/test/java/org/elasticsearch/discovery/ec2/EC2RetriesTests.java +++ b/plugins/discovery-ec2/src/test/java/org/elasticsearch/discovery/ec2/EC2RetriesTests.java @@ -10,15 +10,16 @@ import com.amazonaws.http.HttpMethodName; import com.amazonaws.services.ec2.model.Instance; + import org.apache.http.HttpStatus; import org.apache.http.NameValuePair; import org.apache.http.client.utils.URLEncodedUtils; import org.elasticsearch.Version; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.TransportAddress; import org.elasticsearch.common.util.PageCacheRecycler; +import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.discovery.SeedHostsProvider; import org.elasticsearch.discovery.SeedHostsResolver; import org.elasticsearch.indices.breaker.NoneCircuitBreakerService; @@ -44,9 +45,21 @@ public class EC2RetriesTests extends AbstractEC2MockAPITestCase { @Override protected MockTransportService createTransportService() { - return new MockTransportService(Settings.EMPTY, new MockNioTransport(Settings.EMPTY, Version.CURRENT, threadPool, networkService, - PageCacheRecycler.NON_RECYCLING_INSTANCE, new NamedWriteableRegistry(Collections.emptyList()), - new NoneCircuitBreakerService()), threadPool, TransportService.NOOP_TRANSPORT_INTERCEPTOR, null); + return new MockTransportService( + Settings.EMPTY, + new MockNioTransport( + Settings.EMPTY, + Version.CURRENT, + threadPool, + networkService, + PageCacheRecycler.NON_RECYCLING_INSTANCE, + new NamedWriteableRegistry(Collections.emptyList()), + new NoneCircuitBreakerService() + ), + threadPool, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + null + ); } public void testEC2DiscoveryRetriesOnRateLimiting() throws IOException { @@ -64,8 +77,10 @@ public void testEC2DiscoveryRetriesOnRateLimiting() throws IOException { if (auth == null || auth.contains(accessKey) == false) { throw new IllegalArgumentException("wrong access key: " + auth); } - if (failedRequests.compute(exchange.getRequestHeaders().getFirst("Amz-sdk-invocation-id"), - (requestId, count) -> Objects.requireNonNullElse(count, 0) + 1) < maxRetries) { + if (failedRequests.compute( + exchange.getRequestHeaders().getFirst("Amz-sdk-invocation-id"), + (requestId, count) -> Objects.requireNonNullElse(count, 0) + 1 + ) < maxRetries) { exchange.sendResponseHeaders(HttpStatus.SC_SERVICE_UNAVAILABLE, -1); return; } @@ -73,8 +88,9 @@ public void testEC2DiscoveryRetriesOnRateLimiting() throws IOException { byte[] responseBody = null; for (NameValuePair parse : URLEncodedUtils.parse(request, UTF_8)) { if ("Action".equals(parse.getName())) { - responseBody = generateDescribeInstancesResponse(hosts.stream().map( - address -> new Instance().withPublicIpAddress(address)).collect(Collectors.toList())); + responseBody = generateDescribeInstancesResponse( + hosts.stream().map(address -> new Instance().withPublicIpAddress(address)).collect(Collectors.toList()) + ); break; } } diff --git a/plugins/discovery-ec2/src/test/java/org/elasticsearch/discovery/ec2/Ec2DiscoveryPluginTests.java b/plugins/discovery-ec2/src/test/java/org/elasticsearch/discovery/ec2/Ec2DiscoveryPluginTests.java index 974804ba4d682..a1562cc68a945 100644 --- a/plugins/discovery-ec2/src/test/java/org/elasticsearch/discovery/ec2/Ec2DiscoveryPluginTests.java +++ b/plugins/discovery-ec2/src/test/java/org/elasticsearch/discovery/ec2/Ec2DiscoveryPluginTests.java @@ -15,6 +15,7 @@ import com.amazonaws.auth.BasicSessionCredentials; import com.amazonaws.services.ec2.AbstractAmazonEC2; import com.amazonaws.services.ec2.AmazonEC2; + import org.elasticsearch.common.settings.MockSecureSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.node.Node; @@ -32,9 +33,7 @@ public class Ec2DiscoveryPluginTests extends ESTestCase { private Settings getNodeAttributes(Settings settings, String url) { - final Settings realSettings = Settings.builder() - .put(AwsEc2Service.AUTO_ATTRIBUTE_SETTING.getKey(), true) - .put(settings).build(); + final Settings realSettings = Settings.builder().put(AwsEc2Service.AUTO_ATTRIBUTE_SETTING.getKey(), true).put(settings).build(); return Ec2DiscoveryPlugin.getAvailabilityZoneNodeAttributes(realSettings, url); } @@ -48,8 +47,7 @@ private void assertNodeAttributes(Settings settings, String url, String expected } public void testNodeAttributesDisabled() { - final Settings settings = Settings.builder() - .put(AwsEc2Service.AUTO_ATTRIBUTE_SETTING.getKey(), false).build(); + final Settings settings = Settings.builder().put(AwsEc2Service.AUTO_ATTRIBUTE_SETTING.getKey(), false).build(); assertNodeAttributes(settings, "bogus", null); } @@ -60,9 +58,7 @@ public void testNodeAttributes() throws Exception { } public void testNodeAttributesBogusUrl() { - final UncheckedIOException e = expectThrows(UncheckedIOException.class, () -> - getNodeAttributes(Settings.EMPTY, "bogus") - ); + final UncheckedIOException e = expectThrows(UncheckedIOException.class, () -> getNodeAttributes(Settings.EMPTY, "bogus")); assertNotNull(e.getCause()); final String msg = e.getCause().getMessage(); assertTrue(msg, msg.contains("no protocol: bogus")); @@ -70,8 +66,9 @@ public void testNodeAttributesBogusUrl() { public void testNodeAttributesEmpty() throws Exception { final Path zoneUrl = createTempFile(); - final IllegalStateException e = expectThrows(IllegalStateException.class, () -> - getNodeAttributes(Settings.EMPTY, zoneUrl.toUri().toURL().toString()) + final IllegalStateException e = expectThrows( + IllegalStateException.class, + () -> getNodeAttributes(Settings.EMPTY, zoneUrl.toUri().toURL().toString()) ); assertTrue(e.getMessage(), e.getMessage().contains("no ec2 metadata returned")); } @@ -107,11 +104,11 @@ public void testClientSettingsReInit() throws IOException { mockSecure1.setString(Ec2ClientSettings.PROXY_USERNAME_SETTING.getKey(), "proxy_username_1"); mockSecure1.setString(Ec2ClientSettings.PROXY_PASSWORD_SETTING.getKey(), "proxy_password_1"); final Settings settings1 = Settings.builder() - .put(Ec2ClientSettings.PROXY_HOST_SETTING.getKey(), "proxy_host_1") - .put(Ec2ClientSettings.PROXY_PORT_SETTING.getKey(), 881) - .put(Ec2ClientSettings.ENDPOINT_SETTING.getKey(), "ec2_endpoint_1") - .setSecureSettings(mockSecure1) - .build(); + .put(Ec2ClientSettings.PROXY_HOST_SETTING.getKey(), "proxy_host_1") + .put(Ec2ClientSettings.PROXY_PORT_SETTING.getKey(), 881) + .put(Ec2ClientSettings.ENDPOINT_SETTING.getKey(), "ec2_endpoint_1") + .setSecureSettings(mockSecure1) + .build(); final MockSecureSettings mockSecure2 = new MockSecureSettings(); mockSecure2.setString(Ec2ClientSettings.ACCESS_KEY_SETTING.getKey(), "ec2_access_2"); mockSecure2.setString(Ec2ClientSettings.SECRET_KEY_SETTING.getKey(), "ec2_secret_key_2"); @@ -122,11 +119,11 @@ public void testClientSettingsReInit() throws IOException { mockSecure2.setString(Ec2ClientSettings.PROXY_USERNAME_SETTING.getKey(), "proxy_username_2"); mockSecure2.setString(Ec2ClientSettings.PROXY_PASSWORD_SETTING.getKey(), "proxy_password_2"); final Settings settings2 = Settings.builder() - .put(Ec2ClientSettings.PROXY_HOST_SETTING.getKey(), "proxy_host_2") - .put(Ec2ClientSettings.PROXY_PORT_SETTING.getKey(), 882) - .put(Ec2ClientSettings.ENDPOINT_SETTING.getKey(), "ec2_endpoint_2") - .setSecureSettings(mockSecure2) - .build(); + .put(Ec2ClientSettings.PROXY_HOST_SETTING.getKey(), "proxy_host_2") + .put(Ec2ClientSettings.PROXY_PORT_SETTING.getKey(), 882) + .put(Ec2ClientSettings.ENDPOINT_SETTING.getKey(), "ec2_endpoint_2") + .setSecureSettings(mockSecure2) + .build(); try (Ec2DiscoveryPluginMock plugin = new Ec2DiscoveryPluginMock(settings1)) { try (AmazonEc2Reference clientReference = plugin.ec2Service.client()) { { @@ -135,7 +132,7 @@ public void testClientSettingsReInit() throws IOException { assertThat(credentials.getAWSSecretKey(), is("ec2_secret_key_1")); if (mockSecure1HasSessionToken) { assertThat(credentials, instanceOf(BasicSessionCredentials.class)); - assertThat(((BasicSessionCredentials)credentials).getSessionToken(), is("ec2_session_token_1")); + assertThat(((BasicSessionCredentials) credentials).getSessionToken(), is("ec2_session_token_1")); } else { assertThat(credentials, instanceOf(BasicAWSCredentials.class)); } @@ -152,7 +149,7 @@ public void testClientSettingsReInit() throws IOException { final AWSCredentials credentials = ((AmazonEC2Mock) clientReference.client()).credentials.getCredentials(); if (mockSecure1HasSessionToken) { assertThat(credentials, instanceOf(BasicSessionCredentials.class)); - assertThat(((BasicSessionCredentials)credentials).getSessionToken(), is("ec2_session_token_1")); + assertThat(((BasicSessionCredentials) credentials).getSessionToken(), is("ec2_session_token_1")); } else { assertThat(credentials, instanceOf(BasicAWSCredentials.class)); } @@ -169,7 +166,7 @@ public void testClientSettingsReInit() throws IOException { assertThat(credentials.getAWSSecretKey(), is("ec2_secret_key_2")); if (mockSecure2HasSessionToken) { assertThat(credentials, instanceOf(BasicSessionCredentials.class)); - assertThat(((BasicSessionCredentials)credentials).getSessionToken(), is("ec2_session_token_2")); + assertThat(((BasicSessionCredentials) credentials).getSessionToken(), is("ec2_session_token_2")); } else { assertThat(credentials, instanceOf(BasicAWSCredentials.class)); } @@ -187,8 +184,7 @@ private static class Ec2DiscoveryPluginMock extends Ec2DiscoveryPlugin { Ec2DiscoveryPluginMock(Settings settings) { super(settings, new AwsEc2ServiceImpl() { @Override - AmazonEC2 buildClient(AWSCredentialsProvider credentials, ClientConfiguration configuration, - String endpoint) { + AmazonEC2 buildClient(AWSCredentialsProvider credentials, ClientConfiguration configuration, String endpoint) { return new AmazonEC2Mock(credentials, configuration, endpoint); } }); @@ -208,7 +204,6 @@ private static class AmazonEC2Mock extends AbstractAmazonEC2 { } @Override - public void shutdown() { - } + public void shutdown() {} } } diff --git a/plugins/discovery-ec2/src/test/java/org/elasticsearch/discovery/ec2/Ec2DiscoveryTests.java b/plugins/discovery-ec2/src/test/java/org/elasticsearch/discovery/ec2/Ec2DiscoveryTests.java index 8e5b51872c592..f5da55a0bc640 100644 --- a/plugins/discovery-ec2/src/test/java/org/elasticsearch/discovery/ec2/Ec2DiscoveryTests.java +++ b/plugins/discovery-ec2/src/test/java/org/elasticsearch/discovery/ec2/Ec2DiscoveryTests.java @@ -13,15 +13,16 @@ import com.amazonaws.services.ec2.model.InstanceState; import com.amazonaws.services.ec2.model.InstanceStateName; import com.amazonaws.services.ec2.model.Tag; + import org.apache.http.HttpStatus; import org.apache.http.NameValuePair; import org.apache.http.client.utils.URLEncodedUtils; import org.elasticsearch.Version; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.common.network.NetworkService; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.TransportAddress; import org.elasticsearch.common.util.PageCacheRecycler; +import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.indices.breaker.NoneCircuitBreakerService; import org.elasticsearch.test.transport.MockTransportService; import org.elasticsearch.transport.Transport; @@ -59,13 +60,19 @@ public class Ec2DiscoveryTests extends AbstractEC2MockAPITestCase { private Map poorMansDNS = new ConcurrentHashMap<>(); protected MockTransportService createTransportService() { - final Transport transport = new MockNioTransport(Settings.EMPTY, Version.CURRENT, threadPool, - new NetworkService(Collections.emptyList()), PageCacheRecycler.NON_RECYCLING_INSTANCE, writableRegistry(), - new NoneCircuitBreakerService()) { + final Transport transport = new MockNioTransport( + Settings.EMPTY, + Version.CURRENT, + threadPool, + new NetworkService(Collections.emptyList()), + PageCacheRecycler.NON_RECYCLING_INSTANCE, + writableRegistry(), + new NoneCircuitBreakerService() + ) { @Override public TransportAddress[] addressesFromString(String address) { // we just need to ensure we don't resolve DNS here - return new TransportAddress[] {poorMansDNS.getOrDefault(address, buildNewFakeTransportAddress())}; + return new TransportAddress[] { poorMansDNS.getOrDefault(address, buildNewFakeTransportAddress()) }; } }; return new MockTransportService(Settings.EMPTY, transport, threadPool, TransportService.NOOP_TRANSPORT_INTERCEPTOR, null); @@ -91,21 +98,20 @@ protected List buildDynamicHosts(Settings nodeSettings, int no // Simulate an EC2 DescribeInstancesResponse final Map> tagsIncluded = new HashMap<>(); final String[] params = request.split("&"); - Arrays.stream(params).filter(entry -> entry.startsWith("Filter.") && entry.contains("=tag%3A")) - .forEach(entry -> { - final int startIndex = "Filter.".length(); - final int filterId = Integer.parseInt(entry.substring(startIndex, entry.indexOf(".", startIndex))); - tagsIncluded.put(entry.substring(entry.indexOf("=tag%3A") + "=tag%3A".length()), - Arrays.stream(params) - .filter(param -> param.startsWith("Filter." + filterId + ".Value.")) - .map(param -> param.substring(param.indexOf("=") + 1)) - .collect(Collectors.toList())); - } + Arrays.stream(params).filter(entry -> entry.startsWith("Filter.") && entry.contains("=tag%3A")).forEach(entry -> { + final int startIndex = "Filter.".length(); + final int filterId = Integer.parseInt(entry.substring(startIndex, entry.indexOf(".", startIndex))); + tagsIncluded.put( + entry.substring(entry.indexOf("=tag%3A") + "=tag%3A".length()), + Arrays.stream(params) + .filter(param -> param.startsWith("Filter." + filterId + ".Value.")) + .map(param -> param.substring(param.indexOf("=") + 1)) + .collect(Collectors.toList()) ); + }); final List instances = IntStream.range(1, nodes + 1).mapToObj(node -> { final String instanceId = "node" + node; - final Instance instance = new Instance() - .withInstanceId(instanceId) + final Instance instance = new Instance().withInstanceId(instanceId) .withState(new InstanceState().withName(InstanceStateName.Running)) .withPrivateDnsName(PREFIX_PRIVATE_DNS + instanceId + SUFFIX_PRIVATE_DNS) .withPublicDnsName(PREFIX_PUBLIC_DNS + instanceId + SUFFIX_PUBLIC_DNS) @@ -115,12 +121,19 @@ protected List buildDynamicHosts(Settings nodeSettings, int no instance.setTags(tagsList.get(node - 1)); } return instance; - }).filter(instance -> - tagsIncluded.entrySet().stream().allMatch(entry -> instance.getTags().stream() - .filter(t -> t.getKey().equals(entry.getKey())) - .map(Tag::getValue) - .collect(Collectors.toList()) - .containsAll(entry.getValue()))) + }) + .filter( + instance -> tagsIncluded.entrySet() + .stream() + .allMatch( + entry -> instance.getTags() + .stream() + .filter(t -> t.getKey().equals(entry.getKey())) + .map(Tag::getValue) + .collect(Collectors.toList()) + .containsAll(entry.getValue()) + ) + ) .collect(Collectors.toList()); for (NameValuePair parse : URLEncodedUtils.parse(request, UTF_8)) { if ("Action".equals(parse.getName())) { @@ -146,8 +159,7 @@ protected List buildDynamicHosts(Settings nodeSettings, int no public void testDefaultSettings() throws InterruptedException { int nodes = randomInt(10); - Settings nodeSettings = Settings.builder() - .build(); + Settings nodeSettings = Settings.builder().build(); List discoveryNodes = buildDynamicHosts(nodeSettings, nodes); assertThat(discoveryNodes, hasSize(nodes)); } @@ -155,11 +167,9 @@ public void testDefaultSettings() throws InterruptedException { public void testPrivateIp() throws InterruptedException { int nodes = randomInt(10); for (int i = 0; i < nodes; i++) { - poorMansDNS.put(PREFIX_PRIVATE_IP + (i+1), buildNewFakeTransportAddress()); + poorMansDNS.put(PREFIX_PRIVATE_IP + (i + 1), buildNewFakeTransportAddress()); } - Settings nodeSettings = Settings.builder() - .put(AwsEc2Service.HOST_TYPE_SETTING.getKey(), "private_ip") - .build(); + Settings nodeSettings = Settings.builder().put(AwsEc2Service.HOST_TYPE_SETTING.getKey(), "private_ip").build(); List transportAddresses = buildDynamicHosts(nodeSettings, nodes); assertThat(transportAddresses, hasSize(nodes)); // We check that we are using here expected address @@ -173,11 +183,9 @@ public void testPrivateIp() throws InterruptedException { public void testPublicIp() throws InterruptedException { int nodes = randomInt(10); for (int i = 0; i < nodes; i++) { - poorMansDNS.put(PREFIX_PUBLIC_IP + (i+1), buildNewFakeTransportAddress()); + poorMansDNS.put(PREFIX_PUBLIC_IP + (i + 1), buildNewFakeTransportAddress()); } - Settings nodeSettings = Settings.builder() - .put(AwsEc2Service.HOST_TYPE_SETTING.getKey(), "public_ip") - .build(); + Settings nodeSettings = Settings.builder().put(AwsEc2Service.HOST_TYPE_SETTING.getKey(), "public_ip").build(); List dynamicHosts = buildDynamicHosts(nodeSettings, nodes); assertThat(dynamicHosts, hasSize(nodes)); // We check that we are using here expected address @@ -191,21 +199,17 @@ public void testPublicIp() throws InterruptedException { public void testPrivateDns() throws InterruptedException { int nodes = randomInt(10); for (int i = 0; i < nodes; i++) { - String instanceId = "node" + (i+1); - poorMansDNS.put(PREFIX_PRIVATE_DNS + instanceId + - SUFFIX_PRIVATE_DNS, buildNewFakeTransportAddress()); + String instanceId = "node" + (i + 1); + poorMansDNS.put(PREFIX_PRIVATE_DNS + instanceId + SUFFIX_PRIVATE_DNS, buildNewFakeTransportAddress()); } - Settings nodeSettings = Settings.builder() - .put(AwsEc2Service.HOST_TYPE_SETTING.getKey(), "private_dns") - .build(); + Settings nodeSettings = Settings.builder().put(AwsEc2Service.HOST_TYPE_SETTING.getKey(), "private_dns").build(); List dynamicHosts = buildDynamicHosts(nodeSettings, nodes); assertThat(dynamicHosts, hasSize(nodes)); // We check that we are using here expected address int node = 1; for (TransportAddress address : dynamicHosts) { String instanceId = "node" + node++; - TransportAddress expected = poorMansDNS.get( - PREFIX_PRIVATE_DNS + instanceId + SUFFIX_PRIVATE_DNS); + TransportAddress expected = poorMansDNS.get(PREFIX_PRIVATE_DNS + instanceId + SUFFIX_PRIVATE_DNS); assertEquals(address, expected); } } @@ -213,41 +217,31 @@ public void testPrivateDns() throws InterruptedException { public void testPublicDns() throws InterruptedException { int nodes = randomInt(10); for (int i = 0; i < nodes; i++) { - String instanceId = "node" + (i+1); - poorMansDNS.put(PREFIX_PUBLIC_DNS + instanceId - + SUFFIX_PUBLIC_DNS, buildNewFakeTransportAddress()); + String instanceId = "node" + (i + 1); + poorMansDNS.put(PREFIX_PUBLIC_DNS + instanceId + SUFFIX_PUBLIC_DNS, buildNewFakeTransportAddress()); } - Settings nodeSettings = Settings.builder() - .put(AwsEc2Service.HOST_TYPE_SETTING.getKey(), "public_dns") - .build(); + Settings nodeSettings = Settings.builder().put(AwsEc2Service.HOST_TYPE_SETTING.getKey(), "public_dns").build(); List dynamicHosts = buildDynamicHosts(nodeSettings, nodes); assertThat(dynamicHosts, hasSize(nodes)); // We check that we are using here expected address int node = 1; for (TransportAddress address : dynamicHosts) { String instanceId = "node" + node++; - TransportAddress expected = poorMansDNS.get( - PREFIX_PUBLIC_DNS + instanceId + SUFFIX_PUBLIC_DNS); + TransportAddress expected = poorMansDNS.get(PREFIX_PUBLIC_DNS + instanceId + SUFFIX_PUBLIC_DNS); assertEquals(address, expected); } } public void testInvalidHostType() throws InterruptedException { - Settings nodeSettings = Settings.builder() - .put(AwsEc2Service.HOST_TYPE_SETTING.getKey(), "does_not_exist") - .build(); + Settings nodeSettings = Settings.builder().put(AwsEc2Service.HOST_TYPE_SETTING.getKey(), "does_not_exist").build(); - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> { - buildDynamicHosts(nodeSettings, 1); - }); + IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> { buildDynamicHosts(nodeSettings, 1); }); assertThat(exception.getMessage(), containsString("does_not_exist is unknown for discovery.ec2.host_type")); } public void testFilterByTags() throws InterruptedException { int nodes = randomIntBetween(5, 10); - Settings nodeSettings = Settings.builder() - .put(AwsEc2Service.TAG_SETTING.getKey() + "stage", "prod") - .build(); + Settings nodeSettings = Settings.builder().put(AwsEc2Service.TAG_SETTING.getKey() + "stage", "prod").build(); int prodInstances = 0; List> tagsList = new ArrayList<>(); @@ -270,9 +264,7 @@ public void testFilterByTags() throws InterruptedException { public void testFilterByMultipleTags() throws InterruptedException { int nodes = randomIntBetween(5, 10); - Settings nodeSettings = Settings.builder() - .putList(AwsEc2Service.TAG_SETTING.getKey() + "stage", "prod", "preprod") - .build(); + Settings nodeSettings = Settings.builder().putList(AwsEc2Service.TAG_SETTING.getKey() + "stage", "prod", "preprod").build(); int prodInstances = 0; List> tagsList = new ArrayList<>(); @@ -309,9 +301,7 @@ public void testReadHostFromTag() throws UnknownHostException { poorMansDNS.put("node" + (node + 1), new TransportAddress(InetAddress.getByName(addresses[node]), 9300)); } - Settings nodeSettings = Settings.builder() - .put(AwsEc2Service.HOST_TYPE_SETTING.getKey(), "tag:foo") - .build(); + Settings nodeSettings = Settings.builder().put(AwsEc2Service.HOST_TYPE_SETTING.getKey(), "tag:foo").build(); List> tagsList = new ArrayList<>(); @@ -333,6 +323,7 @@ public void testReadHostFromTag() throws UnknownHostException { abstract static class DummyEc2SeedHostsProvider extends AwsEc2SeedHostsProvider { public int fetchCount = 0; + DummyEc2SeedHostsProvider(Settings settings, TransportService transportService, AwsEc2Service service) { super(settings, transportService, service); } diff --git a/plugins/discovery-ec2/src/test/java/org/elasticsearch/discovery/ec2/Ec2NetworkTests.java b/plugins/discovery-ec2/src/test/java/org/elasticsearch/discovery/ec2/Ec2NetworkTests.java index 9cfba760e279e..eed6ae76a4573 100644 --- a/plugins/discovery-ec2/src/test/java/org/elasticsearch/discovery/ec2/Ec2NetworkTests.java +++ b/plugins/discovery-ec2/src/test/java/org/elasticsearch/discovery/ec2/Ec2NetworkTests.java @@ -9,10 +9,11 @@ package org.elasticsearch.discovery.ec2; import com.sun.net.httpserver.HttpServer; + import org.elasticsearch.common.Strings; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.common.network.NetworkService; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.mocksocket.MockHttpServer; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.test.ESTestCase; @@ -50,7 +51,7 @@ public class Ec2NetworkTests extends ESTestCase { public static void startHttp() throws Exception { httpServer = MockHttpServer.createHttp(new InetSocketAddress(InetAddress.getLoopbackAddress().getHostAddress(), 0), 0); - BiConsumer registerContext = (path, v) ->{ + BiConsumer registerContext = (path, v) -> { final byte[] message = v.getBytes(UTF_8); httpServer.createContext(path, (s) -> { s.sendResponseHeaders(RestStatus.OK.getStatus(), message.length); @@ -59,10 +60,10 @@ public static void startHttp() throws Exception { responseBody.close(); }); }; - registerContext.accept("/latest/meta-data/local-ipv4","127.0.0.1"); - registerContext.accept("/latest/meta-data/public-ipv4","165.168.10.2"); - registerContext.accept("/latest/meta-data/public-hostname","165.168.10.3"); - registerContext.accept("/latest/meta-data/local-hostname","10.10.10.5"); + registerContext.accept("/latest/meta-data/local-ipv4", "127.0.0.1"); + registerContext.accept("/latest/meta-data/public-ipv4", "165.168.10.2"); + registerContext.accept("/latest/meta-data/public-hostname", "165.168.10.3"); + registerContext.accept("/latest/meta-data/local-hostname", "10.10.10.5"); httpServer.start(); } @@ -70,8 +71,12 @@ public static void startHttp() throws Exception { @Before public void setup() { // redirect EC2 metadata service to httpServer - AccessController.doPrivileged((PrivilegedAction) () -> System.setProperty(EC2_METADATA_SERVICE_OVERRIDE_SYSTEM_PROPERTY, - "http://" + httpServer.getAddress().getHostName() + ":" + httpServer.getAddress().getPort())); + AccessController.doPrivileged( + (PrivilegedAction) () -> System.setProperty( + EC2_METADATA_SERVICE_OVERRIDE_SYSTEM_PROPERTY, + "http://" + httpServer.getAddress().getHostName() + ":" + httpServer.getAddress().getPort() + ) + ); } @AfterClass @@ -92,14 +97,17 @@ public void testNetworkHostEc2() throws IOException { */ public void testNetworkHostUnableToResolveEc2() { // redirect EC2 metadata service to unknown location - AccessController.doPrivileged((PrivilegedAction) () -> System.setProperty(EC2_METADATA_SERVICE_OVERRIDE_SYSTEM_PROPERTY, - "http://127.0.0.1/")); + AccessController.doPrivileged( + (PrivilegedAction) () -> System.setProperty(EC2_METADATA_SERVICE_OVERRIDE_SYSTEM_PROPERTY, "http://127.0.0.1/") + ); try { resolveEc2("_ec2_", (InetAddress[]) null); } catch (IOException e) { - assertThat(e.getMessage(), - equalTo("IOException caught when fetching InetAddress from [http://127.0.0.1//latest/meta-data/local-ipv4]")); + assertThat( + e.getMessage(), + equalTo("IOException caught when fetching InetAddress from [http://127.0.0.1//latest/meta-data/local-ipv4]") + ); } } @@ -145,15 +153,14 @@ public void testNetworkHostEc2PublicDns() throws IOException { resolveEc2("_ec2:publicDns_", InetAddress.getByName("165.168.10.3")); } - private InetAddress[] resolveEc2(String host, InetAddress ... expected) throws IOException { - Settings nodeSettings = Settings.builder() - .put("network.host", host) - .build(); + private InetAddress[] resolveEc2(String host, InetAddress... expected) throws IOException { + Settings nodeSettings = Settings.builder().put("network.host", host).build(); NetworkService networkService = new NetworkService(Collections.singletonList(new Ec2NameResolver())); InetAddress[] addresses = networkService.resolveBindHostAddresses( - NetworkService.GLOBAL_NETWORK_BIND_HOST_SETTING.get(nodeSettings).toArray(Strings.EMPTY_ARRAY)); + NetworkService.GLOBAL_NETWORK_BIND_HOST_SETTING.get(nodeSettings).toArray(Strings.EMPTY_ARRAY) + ); if (expected == null) { fail("We should get an IOException, resolved addressed:" + Arrays.toString(addresses)); } diff --git a/plugins/discovery-ec2/src/yamlRestTest/java/org/elasticsearch/discovery/ec2/CloudAwsClientYamlTestSuiteIT.java b/plugins/discovery-ec2/src/yamlRestTest/java/org/elasticsearch/discovery/ec2/CloudAwsClientYamlTestSuiteIT.java index 7cd1880aa1931..2110baae5ec9d 100644 --- a/plugins/discovery-ec2/src/yamlRestTest/java/org/elasticsearch/discovery/ec2/CloudAwsClientYamlTestSuiteIT.java +++ b/plugins/discovery-ec2/src/yamlRestTest/java/org/elasticsearch/discovery/ec2/CloudAwsClientYamlTestSuiteIT.java @@ -25,4 +25,3 @@ public static Iterable parameters() throws Exception { return ESClientYamlSuiteTestCase.createParameters(); } } - diff --git a/plugins/discovery-gce/qa/gce/src/yamlRestTest/java/org/elasticsearch/cloud/gce/GCEDiscoveryClientYamlTestSuiteIT.java b/plugins/discovery-gce/qa/gce/src/yamlRestTest/java/org/elasticsearch/cloud/gce/GCEDiscoveryClientYamlTestSuiteIT.java index 66ca0dc9c5798..18fd0ff5acc2b 100644 --- a/plugins/discovery-gce/qa/gce/src/yamlRestTest/java/org/elasticsearch/cloud/gce/GCEDiscoveryClientYamlTestSuiteIT.java +++ b/plugins/discovery-gce/qa/gce/src/yamlRestTest/java/org/elasticsearch/cloud/gce/GCEDiscoveryClientYamlTestSuiteIT.java @@ -10,6 +10,7 @@ import com.carrotsearch.randomizedtesting.annotations.Name; import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; + import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate; import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase; diff --git a/plugins/discovery-gce/qa/gce/src/yamlRestTest/java/org/elasticsearch/cloud/gce/GCEFixture.java b/plugins/discovery-gce/qa/gce/src/yamlRestTest/java/org/elasticsearch/cloud/gce/GCEFixture.java index bba13eae37af6..5d284f8d64195 100644 --- a/plugins/discovery-gce/qa/gce/src/yamlRestTest/java/org/elasticsearch/cloud/gce/GCEFixture.java +++ b/plugins/discovery-gce/qa/gce/src/yamlRestTest/java/org/elasticsearch/cloud/gce/GCEFixture.java @@ -9,9 +9,9 @@ import org.apache.http.client.methods.HttpGet; import org.elasticsearch.common.Strings; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.common.collect.MapBuilder; import org.elasticsearch.common.path.PathTrie; +import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.RestUtils; import org.elasticsearch.test.fixture.AbstractHttpFixture; @@ -102,51 +102,66 @@ private PathTrie defaultHandlers() { }; // https://cloud.google.com/compute/docs/storing-retrieving-metadata - handlers.insert(nonAuthPath(HttpGet.METHOD_NAME, "/computeMetadata/v1/project/project-id"), - request -> simpleValue.apply(PROJECT_ID)); - handlers.insert(nonAuthPath(HttpGet.METHOD_NAME, "/computeMetadata/v1/project/attributes/google-compute-default-zone"), - request -> simpleValue.apply(ZONE)); + handlers.insert( + nonAuthPath(HttpGet.METHOD_NAME, "/computeMetadata/v1/project/project-id"), + request -> simpleValue.apply(PROJECT_ID) + ); + handlers.insert( + nonAuthPath(HttpGet.METHOD_NAME, "/computeMetadata/v1/project/attributes/google-compute-default-zone"), + request -> simpleValue.apply(ZONE) + ); // https://cloud.google.com/compute/docs/access/create-enable-service-accounts-for-instances - handlers.insert(nonAuthPath(HttpGet.METHOD_NAME, "/computeMetadata/v1/instance/service-accounts/default/token"), - request -> jsonValue.apply(Strings.toString(jsonBuilder() - .startObject() - .field("access_token", TOKEN) - .field("expires_in", TimeUnit.HOURS.toSeconds(1)) - .field("token_type", TOKEN_TYPE) - .endObject()))); + handlers.insert( + nonAuthPath(HttpGet.METHOD_NAME, "/computeMetadata/v1/instance/service-accounts/default/token"), + request -> jsonValue.apply( + Strings.toString( + jsonBuilder().startObject() + .field("access_token", TOKEN) + .field("expires_in", TimeUnit.HOURS.toSeconds(1)) + .field("token_type", TOKEN_TYPE) + .endObject() + ) + ) + ); // https://cloud.google.com/compute/docs/reference/rest/v1/instances - handlers.insert(authPath(HttpGet.METHOD_NAME, "/compute/v1/projects/{project}/zones/{zone}/instances"), - request -> { - final var items = new ArrayList>(); - int count = 0; - for (String address : Files.readAllLines(nodes)) { - count++; - items.add(Map.of( - "id", Long.toString(9309873766405L + count), - "description", "ES node" + count, - "name", "test" + count, - "kind", "compute#instance", - "machineType", "n1-standard-1", - "networkInterfaces", List.of(Map.of( - "accessConfigs", Collections.emptyList(), - "name", "nic0", - "network", "default", - "networkIP", address)), - "status", "RUNNING", - "zone", ZONE)); - } - - final String json = Strings.toString(jsonBuilder() - .startObject() - .field("id", "test-instances") - .field("items", items) - .endObject()); - - final byte[] responseAsBytes = json.getBytes(StandardCharsets.UTF_8); - final Map headers = new HashMap<>(JSON_CONTENT_TYPE); - commonHeaderConsumer.accept(headers); - return new Response(RestStatus.OK.getStatus(), headers, responseAsBytes); + handlers.insert(authPath(HttpGet.METHOD_NAME, "/compute/v1/projects/{project}/zones/{zone}/instances"), request -> { + final var items = new ArrayList>(); + int count = 0; + for (String address : Files.readAllLines(nodes)) { + count++; + items.add( + Map.of( + "id", + Long.toString(9309873766405L + count), + "description", + "ES node" + count, + "name", + "test" + count, + "kind", + "compute#instance", + "machineType", + "n1-standard-1", + "networkInterfaces", + List.of( + Map.of("accessConfigs", Collections.emptyList(), "name", "nic0", "network", "default", "networkIP", address) + ), + "status", + "RUNNING", + "zone", + ZONE + ) + ); + } + + final String json = Strings.toString( + jsonBuilder().startObject().field("id", "test-instances").field("items", items).endObject() + ); + + final byte[] responseAsBytes = json.getBytes(StandardCharsets.UTF_8); + final Map headers = new HashMap<>(JSON_CONTENT_TYPE); + commonHeaderConsumer.accept(headers); + return new Response(RestStatus.OK.getStatus(), headers, responseAsBytes); }); return handlers; } @@ -173,22 +188,29 @@ protected Response handle(final Request request) throws IOException { } private static Response newError(final RestStatus status, final String code, final String message) throws IOException { - final String response = Strings.toString(jsonBuilder() - .startObject() - .field("error", MapBuilder.newMapBuilder() - .put("errors", Collections.singletonList( + final String response = Strings.toString( + jsonBuilder().startObject() + .field( + "error", MapBuilder.newMapBuilder() - .put("domain", "global") - .put("reason", "required") - .put("message", message) - .put("locationType", "header") - .put("location", code) - .immutableMap() - )) - .put("code", status.getStatus()) - .put("message", message) - .immutableMap()) - .endObject()); + .put( + "errors", + Collections.singletonList( + MapBuilder.newMapBuilder() + .put("domain", "global") + .put("reason", "required") + .put("message", message) + .put("locationType", "header") + .put("location", code) + .immutableMap() + ) + ) + .put("code", status.getStatus()) + .put("message", message) + .immutableMap() + ) + .endObject() + ); return new Response(status.getStatus(), JSON_CONTENT_TYPE, response.getBytes(UTF_8)); } diff --git a/plugins/discovery-gce/src/internalClusterTest/java/org/elasticsearch/discovery/gce/GceDiscoverTests.java b/plugins/discovery-gce/src/internalClusterTest/java/org/elasticsearch/discovery/gce/GceDiscoverTests.java index fe6a39de4c626..b3eac7e48635f 100644 --- a/plugins/discovery-gce/src/internalClusterTest/java/org/elasticsearch/discovery/gce/GceDiscoverTests.java +++ b/plugins/discovery-gce/src/internalClusterTest/java/org/elasticsearch/discovery/gce/GceDiscoverTests.java @@ -10,6 +10,7 @@ import com.google.api.services.compute.model.Instance; import com.google.api.services.compute.model.NetworkInterface; + import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse; import org.elasticsearch.cloud.gce.GceInstancesService; import org.elasticsearch.cloud.gce.util.Access; @@ -51,10 +52,10 @@ protected Collection> nodePlugins() { @Override protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { return Settings.builder() - .put(super.nodeSettings(nodeOrdinal, otherSettings)) - .put(DISCOVERY_SEED_PROVIDERS_SETTING.getKey(), "gce") - .put("cloud.gce.project_id", "test") - .put("cloud.gce.zone", "test") + .put(super.nodeSettings(nodeOrdinal, otherSettings)) + .put(DISCOVERY_SEED_PROVIDERS_SETTING.getKey(), "gce") + .put("cloud.gce.project_id", "test") + .put("cloud.gce.zone", "test") .build(); } @@ -63,22 +64,26 @@ public void testJoin() { final String masterNode = internalCluster().startMasterOnlyNode(); registerGceNode(masterNode); - ClusterStateResponse clusterStateResponse = client(masterNode).admin().cluster().prepareState() - .setMasterNodeTimeout("1s") - .clear() - .setNodes(true) - .get(); + ClusterStateResponse clusterStateResponse = client(masterNode).admin() + .cluster() + .prepareState() + .setMasterNodeTimeout("1s") + .clear() + .setNodes(true) + .get(); assertNotNull(clusterStateResponse.getState().nodes().getMasterNodeId()); // start another node final String secondNode = internalCluster().startNode(); registerGceNode(secondNode); - clusterStateResponse = client(secondNode).admin().cluster().prepareState() - .setMasterNodeTimeout("1s") - .clear() - .setNodes(true) - .setLocal(true) - .get(); + clusterStateResponse = client(secondNode).admin() + .cluster() + .prepareState() + .setMasterNodeTimeout("1s") + .clear() + .setNodes(true) + .setLocal(true) + .get(); assertNotNull(clusterStateResponse.getState().nodes().getMasterNodeId()); // wait for the cluster to form @@ -161,8 +166,7 @@ public List zones() { } @Override - public void close() throws IOException { - } + public void close() throws IOException {} }; } } diff --git a/plugins/discovery-gce/src/main/java/org/elasticsearch/cloud/gce/GceInstancesService.java b/plugins/discovery-gce/src/main/java/org/elasticsearch/cloud/gce/GceInstancesService.java index 0a40a958569c2..c3086719895ef 100644 --- a/plugins/discovery-gce/src/main/java/org/elasticsearch/cloud/gce/GceInstancesService.java +++ b/plugins/discovery-gce/src/main/java/org/elasticsearch/cloud/gce/GceInstancesService.java @@ -9,6 +9,7 @@ package org.elasticsearch.cloud.gce; import com.google.api.services.compute.model.Instance; + import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Setting.Property; import org.elasticsearch.core.TimeValue; @@ -36,15 +37,22 @@ public interface GceInstancesService extends Closeable { /** * cloud.gce.zone: Google Compute Engine zones */ - Setting> ZONE_SETTING = - Setting.listSetting("cloud.gce.zone", Collections.emptyList(), Function.identity(), Property.NodeScope); + Setting> ZONE_SETTING = Setting.listSetting( + "cloud.gce.zone", + Collections.emptyList(), + Function.identity(), + Property.NodeScope + ); /** * cloud.gce.refresh_interval: How long the list of hosts is cached to prevent further requests to the AWS API. 0 disables caching. * A negative value will cause infinite caching. Defaults to 0s. */ - Setting REFRESH_SETTING = - Setting.timeSetting("cloud.gce.refresh_interval", TimeValue.timeValueSeconds(0), Property.NodeScope); + Setting REFRESH_SETTING = Setting.timeSetting( + "cloud.gce.refresh_interval", + TimeValue.timeValueSeconds(0), + Property.NodeScope + ); /** * cloud.gce.retry: Should we retry calling GCE API in case of error? Defaults to true. @@ -56,8 +64,7 @@ public interface GceInstancesService extends Closeable { * It's a total time since the initial call is made. * A negative value will retry indefinitely. Defaults to `-1s` (retry indefinitely). */ - Setting MAX_WAIT_SETTING = - Setting.timeSetting("cloud.gce.max_wait", TimeValue.timeValueSeconds(-1), Property.NodeScope); + Setting MAX_WAIT_SETTING = Setting.timeSetting("cloud.gce.max_wait", TimeValue.timeValueSeconds(-1), Property.NodeScope); /** * Return a collection of running instances within the same GCE project diff --git a/plugins/discovery-gce/src/main/java/org/elasticsearch/cloud/gce/GceInstancesServiceImpl.java b/plugins/discovery-gce/src/main/java/org/elasticsearch/cloud/gce/GceInstancesServiceImpl.java index d5477b62b78ce..55f0292285135 100644 --- a/plugins/discovery-gce/src/main/java/org/elasticsearch/cloud/gce/GceInstancesServiceImpl.java +++ b/plugins/discovery-gce/src/main/java/org/elasticsearch/cloud/gce/GceInstancesServiceImpl.java @@ -47,10 +47,17 @@ public class GceInstancesServiceImpl implements GceInstancesService { private static final Logger logger = LogManager.getLogger(GceInstancesServiceImpl.class); // all settings just used for testing - not registered by default - public static final Setting GCE_VALIDATE_CERTIFICATES = - Setting.boolSetting("cloud.gce.validate_certificates", true, Property.NodeScope); - public static final Setting GCE_ROOT_URL = - new Setting<>("cloud.gce.root_url", "https://www.googleapis.com", Function.identity(), Property.NodeScope); + public static final Setting GCE_VALIDATE_CERTIFICATES = Setting.boolSetting( + "cloud.gce.validate_certificates", + true, + Property.NodeScope + ); + public static final Setting GCE_ROOT_URL = new Setting<>( + "cloud.gce.root_url", + "https://www.googleapis.com", + Function.identity(), + Property.NodeScope + ); private final String project; private final List zones; @@ -67,8 +74,9 @@ public Collection instances() { return list.execute(); }); // assist type inference - return instanceList.isEmpty() || instanceList.getItems() == null ? - Collections.emptyList() : instanceList.getItems(); + return instanceList.isEmpty() || instanceList.getItems() == null + ? Collections.emptyList() + : instanceList.getItems(); } catch (IOException e) { logger.warn((Supplier) () -> new ParameterizedMessage("Problem fetching instance list for zone {}", zoneId), e); logger.debug("Full exception:", e); @@ -127,8 +135,9 @@ private List resolveZones() { } try { - final String defaultZone = - getAppEngineValueFromMetadataServer("/computeMetadata/v1/project/attributes/google-compute-default-zone"); + final String defaultZone = getAppEngineValueFromMetadataServer( + "/computeMetadata/v1/project/attributes/google-compute-default-zone" + ); return Collections.singletonList(defaultZone); } catch (Exception e) { logger.warn("unable to resolve default zone from metadata server for GCE discovery service", e); @@ -170,8 +179,7 @@ protected synchronized HttpTransport getGceHttpTransport() throws GeneralSecurit public synchronized Compute client() { if (refreshInterval != null && refreshInterval.millis() != 0) { - if (client != null && - (refreshInterval.millis() < 0 || (System.currentTimeMillis() - lastRefresh) < refreshInterval.millis())) { + if (client != null && (refreshInterval.millis() < 0 || (System.currentTimeMillis() - lastRefresh) < refreshInterval.millis())) { if (logger.isTraceEnabled()) logger.trace("using cache to retrieve client"); return client; } @@ -183,13 +191,13 @@ public synchronized Compute client() { logger.info("starting GCE discovery service"); // Forcing Google Token API URL as set in GCE SDK to - // http://metadata/computeMetadata/v1/instance/service-accounts/default/token + // http://metadata/computeMetadata/v1/instance/service-accounts/default/token // See https://developers.google.com/compute/docs/metadata#metadataserver - String tokenServerEncodedUrl = GceMetadataService.GCE_HOST.get(settings) + - "/computeMetadata/v1/instance/service-accounts/default/token"; - ComputeCredential credential = new ComputeCredential.Builder(getGceHttpTransport(), gceJsonFactory) - .setTokenServerEncodedUrl(tokenServerEncodedUrl) - .build(); + String tokenServerEncodedUrl = GceMetadataService.GCE_HOST.get(settings) + + "/computeMetadata/v1/instance/service-accounts/default/token"; + ComputeCredential credential = new ComputeCredential.Builder(getGceHttpTransport(), gceJsonFactory).setTokenServerEncodedUrl( + tokenServerEncodedUrl + ).build(); // hack around code messiness in GCE code // TODO: get this fixed @@ -200,7 +208,6 @@ public synchronized Compute client() { refreshInterval = TimeValue.timeValueSeconds(credential.getExpiresInSeconds() - 1); } - Compute.Builder builder = new Compute.Builder(getGceHttpTransport(), gceJsonFactory, null).setApplicationName(VERSION) .setRootUrl(GCE_ROOT_URL.get(settings)); diff --git a/plugins/discovery-gce/src/main/java/org/elasticsearch/cloud/gce/GceMetadataService.java b/plugins/discovery-gce/src/main/java/org/elasticsearch/cloud/gce/GceMetadataService.java index 2afc3a9bcce33..c3715d68244c0 100644 --- a/plugins/discovery-gce/src/main/java/org/elasticsearch/cloud/gce/GceMetadataService.java +++ b/plugins/discovery-gce/src/main/java/org/elasticsearch/cloud/gce/GceMetadataService.java @@ -8,17 +8,12 @@ package org.elasticsearch.cloud.gce; -import java.io.IOException; -import java.net.URI; -import java.net.URISyntaxException; -import java.security.GeneralSecurityException; -import java.util.function.Function; - import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.http.GenericUrl; import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpResponse; import com.google.api.client.http.HttpTransport; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.cloud.gce.util.Access; @@ -26,15 +21,25 @@ import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.security.GeneralSecurityException; +import java.util.function.Function; + public class GceMetadataService extends AbstractLifecycleComponent { private static final Logger logger = LogManager.getLogger(GceMetadataService.class); // Forcing Google Token API URL as set in GCE SDK to - // http://metadata/computeMetadata/v1/instance/service-accounts/default/token + // http://metadata/computeMetadata/v1/instance/service-accounts/default/token // See https://developers.google.com/compute/docs/metadata#metadataserver // all settings just used for testing - not registered by default - public static final Setting GCE_HOST = - new Setting<>("cloud.gce.host", "http://metadata.google.internal", Function.identity(), Setting.Property.NodeScope); + public static final Setting GCE_HOST = new Setting<>( + "cloud.gce.host", + "http://metadata.google.internal", + Function.identity(), + Setting.Property.NodeScope + ); private final Settings settings; @@ -54,7 +59,7 @@ protected synchronized HttpTransport getGceHttpTransport() throws GeneralSecurit public String metadata(String metadataPath) throws IOException, URISyntaxException { // Forcing Google Token API URL as set in GCE SDK to - // http://metadata/computeMetadata/v1/instance/service-accounts/default/token + // http://metadata/computeMetadata/v1/instance/service-accounts/default/token // See https://developers.google.com/compute/docs/metadata#metadataserver final URI urlMetadataNetwork = new URI(GCE_HOST.get(settings)).resolve("/computeMetadata/v1/instance/").resolve(metadataPath); logger.debug("get metadata from [{}]", urlMetadataNetwork); @@ -67,11 +72,9 @@ public String metadata(String metadataPath) throws IOException, URISyntaxExcepti // This is needed to query meta data: https://cloud.google.com/compute/docs/metadata headers.put("Metadata-Flavor", "Google"); - HttpResponse response = Access.doPrivilegedIOException(() -> - getGceHttpTransport().createRequestFactory() - .buildGetRequest(genericUrl) - .setHeaders(headers) - .execute()); + HttpResponse response = Access.doPrivilegedIOException( + () -> getGceHttpTransport().createRequestFactory().buildGetRequest(genericUrl).setHeaders(headers).execute() + ); String metadata = response.parseAsString(); logger.debug("metadata found [{}]", metadata); return metadata; diff --git a/plugins/discovery-gce/src/main/java/org/elasticsearch/cloud/gce/network/GceNameResolver.java b/plugins/discovery-gce/src/main/java/org/elasticsearch/cloud/gce/network/GceNameResolver.java index e3ff5882ffb95..6c3244f947c87 100644 --- a/plugins/discovery-gce/src/main/java/org/elasticsearch/cloud/gce/network/GceNameResolver.java +++ b/plugins/discovery-gce/src/main/java/org/elasticsearch/cloud/gce/network/GceNameResolver.java @@ -87,8 +87,12 @@ private InetAddress[] resolve(String value) throws IOException { // We replace network placeholder with network interface value gceMetadataPath = Strings.replace(GceAddressResolverType.PRIVATE_IP.gceName, "{{network}}", network); } else { - throw new IllegalArgumentException("[" + value + "] is not one of the supported GCE network.host setting. " + - "Expecting _gce_, _gce:privateIp:X_, _gce:hostname_"); + throw new IllegalArgumentException( + "[" + + value + + "] is not one of the supported GCE network.host setting. " + + "Expecting _gce_, _gce:privateIp:X_, _gce:hostname_" + ); } try { diff --git a/plugins/discovery-gce/src/main/java/org/elasticsearch/cloud/gce/util/Access.java b/plugins/discovery-gce/src/main/java/org/elasticsearch/cloud/gce/util/Access.java index f6aad05a7ad99..0f328baedadff 100644 --- a/plugins/discovery-gce/src/main/java/org/elasticsearch/cloud/gce/util/Access.java +++ b/plugins/discovery-gce/src/main/java/org/elasticsearch/cloud/gce/util/Access.java @@ -41,8 +41,7 @@ public static void doPrivilegedVoid(final Runnable action) { }); } - public static T doPrivilegedIOException(final PrivilegedExceptionAction operation) - throws IOException { + public static T doPrivilegedIOException(final PrivilegedExceptionAction operation) throws IOException { SpecialPermission.check(); try { return AccessController.doPrivileged(operation); diff --git a/plugins/discovery-gce/src/main/java/org/elasticsearch/discovery/gce/GceSeedHostsProvider.java b/plugins/discovery-gce/src/main/java/org/elasticsearch/discovery/gce/GceSeedHostsProvider.java index d1d8974e1deb0..29b4059b6053c 100644 --- a/plugins/discovery-gce/src/main/java/org/elasticsearch/discovery/gce/GceSeedHostsProvider.java +++ b/plugins/discovery-gce/src/main/java/org/elasticsearch/discovery/gce/GceSeedHostsProvider.java @@ -44,8 +44,12 @@ public class GceSeedHostsProvider implements SeedHostsProvider { /** * discovery.gce.tags: The gce discovery can filter machines to include in the cluster based on tags. */ - public static final Setting> TAGS_SETTING = - Setting.listSetting("discovery.gce.tags", emptyList(), Function.identity(), Property.NodeScope); + public static final Setting> TAGS_SETTING = Setting.listSetting( + "discovery.gce.tags", + emptyList(), + Function.identity(), + Property.NodeScope + ); static final class Status { private static final String TERMINATED = "TERMINATED"; @@ -64,9 +68,12 @@ static final class Status { private long lastRefresh; private List cachedDynamicHosts; - public GceSeedHostsProvider(Settings settings, GceInstancesService gceInstancesService, - TransportService transportService, - NetworkService networkService) { + public GceSeedHostsProvider( + Settings settings, + GceInstancesService gceInstancesService, + TransportService transportService, + NetworkService networkService + ) { this.settings = settings; this.gceInstancesService = gceInstancesService; this.transportService = transportService; @@ -90,14 +97,19 @@ public GceSeedHostsProvider(Settings settings, GceInstancesService gceInstancesS public List getSeedAddresses(HostsResolver hostsResolver) { // We check that needed properties have been set if (this.project == null || this.project.isEmpty() || this.zones == null || this.zones.isEmpty()) { - throw new IllegalArgumentException("one or more gce discovery settings are missing. " + - "Check elasticsearch.yml file. Should have [" + GceInstancesService.PROJECT_SETTING.getKey() + - "] and [" + GceInstancesService.ZONE_SETTING.getKey() + "]."); + throw new IllegalArgumentException( + "one or more gce discovery settings are missing. " + + "Check elasticsearch.yml file. Should have [" + + GceInstancesService.PROJECT_SETTING.getKey() + + "] and [" + + GceInstancesService.ZONE_SETTING.getKey() + + "]." + ); } if (refreshInterval.millis() != 0) { - if (cachedDynamicHosts != null && - (refreshInterval.millis() < 0 || (System.currentTimeMillis() - lastRefresh) < refreshInterval.millis())) { + if (cachedDynamicHosts != null + && (refreshInterval.millis() < 0 || (System.currentTimeMillis() - lastRefresh) < refreshInterval.millis())) { if (logger.isTraceEnabled()) logger.trace("using cache to retrieve node list"); return cachedDynamicHosts; } @@ -109,7 +121,8 @@ public List getSeedAddresses(HostsResolver hostsResolver) { String ipAddress = null; try { InetAddress inetAddress = networkService.resolvePublishHostAddresses( - NetworkService.GLOBAL_NETWORK_PUBLISH_HOST_SETTING.get(settings).toArray(Strings.EMPTY_ARRAY)); + NetworkService.GLOBAL_NETWORK_PUBLISH_HOST_SETTING.get(settings).toArray(Strings.EMPTY_ARRAY) + ); if (inetAddress != null) { ipAddress = NetworkAddress.format(inetAddress); } @@ -144,8 +157,10 @@ public List getSeedAddresses(HostsResolver hostsResolver) { boolean filterByTag = false; if (tags.isEmpty() == false) { logger.trace("start filtering instance {} with tags {}.", name, tags); - if (instance.getTags() == null || instance.getTags().isEmpty() - || instance.getTags().getItems() == null || instance.getTags().getItems().isEmpty()) { + if (instance.getTags() == null + || instance.getTags().isEmpty() + || instance.getTags().getItems() == null + || instance.getTags().getItems().isEmpty()) { // If this instance have no tag, we filter it logger.trace("no tags for this instance but we asked for tags. {} won't be part of the cluster.", name); filterByTag = true; @@ -168,8 +183,12 @@ public List getSeedAddresses(HostsResolver hostsResolver) { } } if (filterByTag) { - logger.trace("filtering out instance {} based tags {}, not part of {}", name, tags, - instance.getTags() == null || instance.getTags().getItems() == null ? "" : instance.getTags()); + logger.trace( + "filtering out instance {} based tags {}, not part of {}", + name, + tags, + instance.getTags() == null || instance.getTags().getItems() == null ? "" : instance.getTags() + ); continue; } else { logger.trace("instance {} with tags {} is added to discovery", name, tags); @@ -225,8 +244,14 @@ public List getSeedAddresses(HostsResolver hostsResolver) { TransportAddress[] addresses = transportService.addressesFromString(address); for (TransportAddress transportAddress : addresses) { - logger.trace("adding {}, type {}, address {}, transport_address {}, status {}", name, type, - ip_private, transportAddress, status); + logger.trace( + "adding {}, type {}, address {}, transport_address {}, status {}", + name, + type, + ip_private, + transportAddress, + status + ); cachedDynamicHosts.add(transportAddress); } } diff --git a/plugins/discovery-gce/src/main/java/org/elasticsearch/discovery/gce/RetryHttpInitializerWrapper.java b/plugins/discovery-gce/src/main/java/org/elasticsearch/discovery/gce/RetryHttpInitializerWrapper.java index d7723333b2d00..eab31b01e6e72 100644 --- a/plugins/discovery-gce/src/main/java/org/elasticsearch/discovery/gce/RetryHttpInitializerWrapper.java +++ b/plugins/discovery-gce/src/main/java/org/elasticsearch/discovery/gce/RetryHttpInitializerWrapper.java @@ -8,8 +8,6 @@ package org.elasticsearch.discovery.gce; -import org.apache.logging.log4j.Logger; -import org.apache.logging.log4j.LogManager; import com.google.api.client.auth.oauth2.Credential; import com.google.api.client.googleapis.testing.auth.oauth2.MockGoogleCredential; import com.google.api.client.http.HttpBackOffIOExceptionHandler; @@ -20,6 +18,9 @@ import com.google.api.client.http.HttpUnsuccessfulResponseHandler; import com.google.api.client.util.ExponentialBackOff; import com.google.api.client.util.Sleeper; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.elasticsearch.cloud.gce.util.Access; import org.elasticsearch.core.TimeValue; @@ -49,8 +50,7 @@ public RetryHttpInitializerWrapper(Credential wrappedCredential, TimeValue maxWa } // Use only for testing. - RetryHttpInitializerWrapper( - Credential wrappedCredential, Sleeper sleeper, TimeValue maxWait) { + RetryHttpInitializerWrapper(Credential wrappedCredential, Sleeper sleeper, TimeValue maxWait) { this.wrappedCredential = Objects.requireNonNull(wrappedCredential); this.sleeper = sleeper; this.maxWait = maxWait; @@ -64,45 +64,35 @@ static MockGoogleCredential.Builder newMockCredentialBuilder() { @Override public void initialize(HttpRequest httpRequest) { - final HttpUnsuccessfulResponseHandler backoffHandler = - new HttpBackOffUnsuccessfulResponseHandler( - new ExponentialBackOff.Builder() - .setMaxElapsedTimeMillis(((int) maxWait.getMillis())) - .build()) - .setSleeper(sleeper); + final HttpUnsuccessfulResponseHandler backoffHandler = new HttpBackOffUnsuccessfulResponseHandler( + new ExponentialBackOff.Builder().setMaxElapsedTimeMillis(((int) maxWait.getMillis())).build() + ).setSleeper(sleeper); httpRequest.setInterceptor(wrappedCredential); - httpRequest.setUnsuccessfulResponseHandler( - new HttpUnsuccessfulResponseHandler() { - int retry = 0; + httpRequest.setUnsuccessfulResponseHandler(new HttpUnsuccessfulResponseHandler() { + int retry = 0; - @Override - public boolean handleResponse(HttpRequest request, HttpResponse response, boolean supportsRetry) throws IOException { - if (wrappedCredential.handleResponse( - request, response, supportsRetry)) { - // If credential decides it can handle it, - // the return code or message indicated - // something specific to authentication, - // and no backoff is desired. - return true; - } else if (backoffHandler.handleResponse( - request, response, supportsRetry)) { - // Otherwise, we defer to the judgement of - // our internal backoff handler. - logger.debug("Retrying [{}] times : [{}]", retry, request.getUrl()); - return true; - } else { - return false; - } - } - }); + @Override + public boolean handleResponse(HttpRequest request, HttpResponse response, boolean supportsRetry) throws IOException { + if (wrappedCredential.handleResponse(request, response, supportsRetry)) { + // If credential decides it can handle it, + // the return code or message indicated + // something specific to authentication, + // and no backoff is desired. + return true; + } else if (backoffHandler.handleResponse(request, response, supportsRetry)) { + // Otherwise, we defer to the judgement of + // our internal backoff handler. + logger.debug("Retrying [{}] times : [{}]", retry, request.getUrl()); + return true; + } else { + return false; + } + } + }); httpRequest.setIOExceptionHandler( - new HttpBackOffIOExceptionHandler( - new ExponentialBackOff.Builder() - .setMaxElapsedTimeMillis(((int) maxWait.getMillis())) - .build()) - .setSleeper(sleeper) + new HttpBackOffIOExceptionHandler(new ExponentialBackOff.Builder().setMaxElapsedTimeMillis(((int) maxWait.getMillis())).build()) + .setSleeper(sleeper) ); } } - diff --git a/plugins/discovery-gce/src/main/java/org/elasticsearch/plugin/discovery/gce/GceDiscoveryPlugin.java b/plugins/discovery-gce/src/main/java/org/elasticsearch/plugin/discovery/gce/GceDiscoveryPlugin.java index 1c1506a5646a0..268fdde4ca5a4 100644 --- a/plugins/discovery-gce/src/main/java/org/elasticsearch/plugin/discovery/gce/GceDiscoveryPlugin.java +++ b/plugins/discovery-gce/src/main/java/org/elasticsearch/plugin/discovery/gce/GceDiscoveryPlugin.java @@ -10,6 +10,7 @@ import com.google.api.client.http.HttpHeaders; import com.google.api.client.util.ClassInfo; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.lucene.util.SetOnce; @@ -18,10 +19,10 @@ import org.elasticsearch.cloud.gce.GceMetadataService; import org.elasticsearch.cloud.gce.network.GceNameResolver; import org.elasticsearch.cloud.gce.util.Access; -import org.elasticsearch.core.Booleans; import org.elasticsearch.common.network.NetworkService; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.Booleans; import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.discovery.SeedHostsProvider; import org.elasticsearch.discovery.gce.GceSeedHostsProvider; @@ -41,8 +42,9 @@ public class GceDiscoveryPlugin extends Plugin implements DiscoveryPlugin, Closeable { /** Determines whether settings those reroutes GCE call should be allowed (for testing purposes only). */ - private static final boolean ALLOW_REROUTE_GCE_SETTINGS = - Booleans.parseBoolean(System.getProperty("es.allow_reroute_gce_settings", "false")); + private static final boolean ALLOW_REROUTE_GCE_SETTINGS = Booleans.parseBoolean( + System.getProperty("es.allow_reroute_gce_settings", "false") + ); public static final String GCE = "gce"; protected final Settings settings; @@ -59,7 +61,7 @@ public class GceDiscoveryPlugin extends Plugin implements DiscoveryPlugin, Close * our plugin permissions don't allow core to "reach through" plugins to * change the permission. Because that'd be silly. */ - Access.doPrivilegedVoid( () -> ClassInfo.of(HttpHeaders.class, true)); + Access.doPrivilegedVoid(() -> ClassInfo.of(HttpHeaders.class, true)); } public GceDiscoveryPlugin(Settings settings) { @@ -73,8 +75,7 @@ protected GceInstancesService createGceInstancesService() { } @Override - public Map> getSeedHostProviders(TransportService transportService, - NetworkService networkService) { + public Map> getSeedHostProviders(TransportService transportService, NetworkService networkService) { return Collections.singletonMap(GCE, () -> { gceInstancesService.set(createGceInstancesService()); return new GceSeedHostsProvider(settings, gceInstancesService.get(), transportService, networkService); @@ -97,7 +98,8 @@ public List> getSettings() { GceSeedHostsProvider.TAGS_SETTING, GceInstancesService.REFRESH_SETTING, GceInstancesService.RETRY_SETTING, - GceInstancesService.MAX_WAIT_SETTING) + GceInstancesService.MAX_WAIT_SETTING + ) ); if (ALLOW_REROUTE_GCE_SETTINGS) { @@ -107,8 +109,6 @@ public List> getSettings() { return Collections.unmodifiableList(settings); } - - @Override public void close() throws IOException { IOUtils.close(gceInstancesService.get()); diff --git a/plugins/discovery-gce/src/test/java/org/elasticsearch/cloud/gce/GceInstancesServiceImplTests.java b/plugins/discovery-gce/src/test/java/org/elasticsearch/cloud/gce/GceInstancesServiceImplTests.java index 1b70f12fa8098..cae2150cc4d29 100644 --- a/plugins/discovery-gce/src/test/java/org/elasticsearch/cloud/gce/GceInstancesServiceImplTests.java +++ b/plugins/discovery-gce/src/test/java/org/elasticsearch/cloud/gce/GceInstancesServiceImplTests.java @@ -14,6 +14,7 @@ import com.google.api.client.testing.http.MockHttpTransport; import com.google.api.client.testing.http.MockLowLevelHttpRequest; import com.google.api.client.testing.http.MockLowLevelHttpResponse; + import org.elasticsearch.common.settings.Settings; import org.elasticsearch.test.ESTestCase; diff --git a/plugins/discovery-gce/src/test/java/org/elasticsearch/discovery/gce/GceDiscoveryTests.java b/plugins/discovery-gce/src/test/java/org/elasticsearch/discovery/gce/GceDiscoveryTests.java index 650876b510f25..a32f54638f8d6 100644 --- a/plugins/discovery-gce/src/test/java/org/elasticsearch/discovery/gce/GceDiscoveryTests.java +++ b/plugins/discovery-gce/src/test/java/org/elasticsearch/discovery/gce/GceDiscoveryTests.java @@ -96,8 +96,12 @@ public void stopGceComputeService() throws IOException { } protected List buildDynamicNodes(GceInstancesServiceImpl gceInstancesService, Settings nodeSettings) { - GceSeedHostsProvider provider = new GceSeedHostsProvider(nodeSettings, gceInstancesService, - transportService, new NetworkService(Collections.emptyList())); + GceSeedHostsProvider provider = new GceSeedHostsProvider( + nodeSettings, + gceInstancesService, + transportService, + new NetworkService(Collections.emptyList()) + ); List dynamicHosts = provider.getSeedAddresses(null); logger.info("--> addresses found: {}", dynamicHosts); @@ -106,9 +110,9 @@ protected List buildDynamicNodes(GceInstancesServiceImpl gceIn public void testNodesWithDifferentTagsAndNoTagSet() { Settings nodeSettings = Settings.builder() - .put(GceInstancesServiceImpl.PROJECT_SETTING.getKey(), projectName) - .put(GceInstancesServiceImpl.ZONE_SETTING.getKey(), "europe-west1-b") - .build(); + .put(GceInstancesServiceImpl.PROJECT_SETTING.getKey(), projectName) + .put(GceInstancesServiceImpl.ZONE_SETTING.getKey(), "europe-west1-b") + .build(); mock = new GceInstancesServiceMock(nodeSettings); List dynamicHosts = buildDynamicNodes(mock, nodeSettings); assertThat(dynamicHosts, hasSize(2)); @@ -116,10 +120,10 @@ public void testNodesWithDifferentTagsAndNoTagSet() { public void testNodesWithDifferentTagsAndOneTagSet() { Settings nodeSettings = Settings.builder() - .put(GceInstancesServiceImpl.PROJECT_SETTING.getKey(), projectName) - .put(GceInstancesServiceImpl.ZONE_SETTING.getKey(), "europe-west1-b") - .putList(GceSeedHostsProvider.TAGS_SETTING.getKey(), "elasticsearch") - .build(); + .put(GceInstancesServiceImpl.PROJECT_SETTING.getKey(), projectName) + .put(GceInstancesServiceImpl.ZONE_SETTING.getKey(), "europe-west1-b") + .putList(GceSeedHostsProvider.TAGS_SETTING.getKey(), "elasticsearch") + .build(); mock = new GceInstancesServiceMock(nodeSettings); List dynamicHosts = buildDynamicNodes(mock, nodeSettings); assertThat(dynamicHosts, hasSize(1)); @@ -127,10 +131,10 @@ public void testNodesWithDifferentTagsAndOneTagSet() { public void testNodesWithDifferentTagsAndTwoTagSet() { Settings nodeSettings = Settings.builder() - .put(GceInstancesServiceImpl.PROJECT_SETTING.getKey(), projectName) - .put(GceInstancesServiceImpl.ZONE_SETTING.getKey(), "europe-west1-b") - .putList(GceSeedHostsProvider.TAGS_SETTING.getKey(), "elasticsearch", "dev") - .build(); + .put(GceInstancesServiceImpl.PROJECT_SETTING.getKey(), projectName) + .put(GceInstancesServiceImpl.ZONE_SETTING.getKey(), "europe-west1-b") + .putList(GceSeedHostsProvider.TAGS_SETTING.getKey(), "elasticsearch", "dev") + .build(); mock = new GceInstancesServiceMock(nodeSettings); List dynamicHosts = buildDynamicNodes(mock, nodeSettings); assertThat(dynamicHosts, hasSize(1)); @@ -138,9 +142,9 @@ public void testNodesWithDifferentTagsAndTwoTagSet() { public void testNodesWithSameTagsAndNoTagSet() { Settings nodeSettings = Settings.builder() - .put(GceInstancesServiceImpl.PROJECT_SETTING.getKey(), projectName) - .put(GceInstancesServiceImpl.ZONE_SETTING.getKey(), "europe-west1-b") - .build(); + .put(GceInstancesServiceImpl.PROJECT_SETTING.getKey(), projectName) + .put(GceInstancesServiceImpl.ZONE_SETTING.getKey(), "europe-west1-b") + .build(); mock = new GceInstancesServiceMock(nodeSettings); List dynamicHosts = buildDynamicNodes(mock, nodeSettings); assertThat(dynamicHosts, hasSize(2)); @@ -148,10 +152,10 @@ public void testNodesWithSameTagsAndNoTagSet() { public void testNodesWithSameTagsAndOneTagSet() { Settings nodeSettings = Settings.builder() - .put(GceInstancesServiceImpl.PROJECT_SETTING.getKey(), projectName) - .put(GceInstancesServiceImpl.ZONE_SETTING.getKey(), "europe-west1-b") - .putList(GceSeedHostsProvider.TAGS_SETTING.getKey(), "elasticsearch") - .build(); + .put(GceInstancesServiceImpl.PROJECT_SETTING.getKey(), projectName) + .put(GceInstancesServiceImpl.ZONE_SETTING.getKey(), "europe-west1-b") + .putList(GceSeedHostsProvider.TAGS_SETTING.getKey(), "elasticsearch") + .build(); mock = new GceInstancesServiceMock(nodeSettings); List dynamicHosts = buildDynamicNodes(mock, nodeSettings); assertThat(dynamicHosts, hasSize(2)); @@ -159,10 +163,10 @@ public void testNodesWithSameTagsAndOneTagSet() { public void testNodesWithSameTagsAndTwoTagsSet() { Settings nodeSettings = Settings.builder() - .put(GceInstancesServiceImpl.PROJECT_SETTING.getKey(), projectName) - .put(GceInstancesServiceImpl.ZONE_SETTING.getKey(), "europe-west1-b") - .putList(GceSeedHostsProvider.TAGS_SETTING.getKey(), "elasticsearch", "dev") - .build(); + .put(GceInstancesServiceImpl.PROJECT_SETTING.getKey(), projectName) + .put(GceInstancesServiceImpl.ZONE_SETTING.getKey(), "europe-west1-b") + .putList(GceSeedHostsProvider.TAGS_SETTING.getKey(), "elasticsearch", "dev") + .build(); mock = new GceInstancesServiceMock(nodeSettings); List dynamicHosts = buildDynamicNodes(mock, nodeSettings); assertThat(dynamicHosts, hasSize(2)); @@ -170,9 +174,9 @@ public void testNodesWithSameTagsAndTwoTagsSet() { public void testMultipleZonesAndTwoNodesInSameZone() { Settings nodeSettings = Settings.builder() - .put(GceInstancesServiceImpl.PROJECT_SETTING.getKey(), projectName) - .putList(GceInstancesServiceImpl.ZONE_SETTING.getKey(), "us-central1-a", "europe-west1-b") - .build(); + .put(GceInstancesServiceImpl.PROJECT_SETTING.getKey(), projectName) + .putList(GceInstancesServiceImpl.ZONE_SETTING.getKey(), "us-central1-a", "europe-west1-b") + .build(); mock = new GceInstancesServiceMock(nodeSettings); List dynamicHosts = buildDynamicNodes(mock, nodeSettings); assertThat(dynamicHosts, hasSize(2)); @@ -180,9 +184,9 @@ public void testMultipleZonesAndTwoNodesInSameZone() { public void testMultipleZonesAndTwoNodesInDifferentZones() { Settings nodeSettings = Settings.builder() - .put(GceInstancesServiceImpl.PROJECT_SETTING.getKey(), projectName) - .putList(GceInstancesServiceImpl.ZONE_SETTING.getKey(), "us-central1-a", "europe-west1-b") - .build(); + .put(GceInstancesServiceImpl.PROJECT_SETTING.getKey(), projectName) + .putList(GceInstancesServiceImpl.ZONE_SETTING.getKey(), "us-central1-a", "europe-west1-b") + .build(); mock = new GceInstancesServiceMock(nodeSettings); List dynamicHosts = buildDynamicNodes(mock, nodeSettings); assertThat(dynamicHosts, hasSize(2)); @@ -193,9 +197,9 @@ public void testMultipleZonesAndTwoNodesInDifferentZones() { */ public void testZeroNode43() { Settings nodeSettings = Settings.builder() - .put(GceInstancesServiceImpl.PROJECT_SETTING.getKey(), projectName) - .putList(GceInstancesServiceImpl.ZONE_SETTING.getKey(), "us-central1-a", "us-central1-b") - .build(); + .put(GceInstancesServiceImpl.PROJECT_SETTING.getKey(), projectName) + .putList(GceInstancesServiceImpl.ZONE_SETTING.getKey(), "us-central1-a", "us-central1-b") + .build(); mock = new GceInstancesServiceMock(nodeSettings); List dynamicHosts = buildDynamicNodes(mock, nodeSettings); assertThat(dynamicHosts, hasSize(0)); diff --git a/plugins/discovery-gce/src/test/java/org/elasticsearch/discovery/gce/GceInstancesServiceMock.java b/plugins/discovery-gce/src/test/java/org/elasticsearch/discovery/gce/GceInstancesServiceMock.java index 1a12434352528..ac74034a8ebfd 100644 --- a/plugins/discovery-gce/src/test/java/org/elasticsearch/discovery/gce/GceInstancesServiceMock.java +++ b/plugins/discovery-gce/src/test/java/org/elasticsearch/discovery/gce/GceInstancesServiceMock.java @@ -9,6 +9,7 @@ package org.elasticsearch.discovery.gce; import com.google.api.client.http.HttpTransport; + import org.elasticsearch.cloud.gce.GceInstancesServiceImpl; import org.elasticsearch.common.settings.Settings; diff --git a/plugins/discovery-gce/src/test/java/org/elasticsearch/discovery/gce/GceMetadataServiceMock.java b/plugins/discovery-gce/src/test/java/org/elasticsearch/discovery/gce/GceMetadataServiceMock.java index 1d772387a1296..bc5b355b2a87b 100644 --- a/plugins/discovery-gce/src/test/java/org/elasticsearch/discovery/gce/GceMetadataServiceMock.java +++ b/plugins/discovery-gce/src/test/java/org/elasticsearch/discovery/gce/GceMetadataServiceMock.java @@ -9,6 +9,7 @@ package org.elasticsearch.discovery.gce; import com.google.api.client.http.HttpTransport; + import org.elasticsearch.cloud.gce.GceMetadataService; import org.elasticsearch.common.settings.Settings; diff --git a/plugins/discovery-gce/src/test/java/org/elasticsearch/discovery/gce/GceMockUtils.java b/plugins/discovery-gce/src/test/java/org/elasticsearch/discovery/gce/GceMockUtils.java index 2663c2740a72d..889228ac838a6 100644 --- a/plugins/discovery-gce/src/test/java/org/elasticsearch/discovery/gce/GceMockUtils.java +++ b/plugins/discovery-gce/src/test/java/org/elasticsearch/discovery/gce/GceMockUtils.java @@ -15,6 +15,7 @@ import com.google.api.client.testing.http.MockHttpTransport; import com.google.api.client.testing.http.MockLowLevelHttpRequest; import com.google.api.client.testing.http.MockLowLevelHttpResponse; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.common.Strings; diff --git a/plugins/discovery-gce/src/test/java/org/elasticsearch/discovery/gce/GceNetworkTests.java b/plugins/discovery-gce/src/test/java/org/elasticsearch/discovery/gce/GceNetworkTests.java index 31971c86a857f..bdb6abd8d1bc7 100644 --- a/plugins/discovery-gce/src/test/java/org/elasticsearch/discovery/gce/GceNetworkTests.java +++ b/plugins/discovery-gce/src/test/java/org/elasticsearch/discovery/gce/GceNetworkTests.java @@ -70,8 +70,10 @@ public void testNetworkHostPrivateIpInterface() throws IOException { * network.host: _local_ */ public void networkHostCoreLocal() throws IOException { - resolveGce("_local_", new NetworkService(Collections.emptyList()) - .resolveBindHostAddresses(new String[] { NetworkService.DEFAULT_NETWORK_HOST })); + resolveGce( + "_local_", + new NetworkService(Collections.emptyList()).resolveBindHostAddresses(new String[] { NetworkService.DEFAULT_NETWORK_HOST }) + ); } /** @@ -81,7 +83,7 @@ public void networkHostCoreLocal() throws IOException { * @throws IOException Well... If something goes wrong :) */ private void resolveGce(String gceNetworkSetting, InetAddress expected) throws IOException { - resolveGce(gceNetworkSetting, expected == null ? null : new InetAddress [] { expected }); + resolveGce(gceNetworkSetting, expected == null ? null : new InetAddress[] { expected }); } /** @@ -91,15 +93,14 @@ private void resolveGce(String gceNetworkSetting, InetAddress expected) throws I * @throws IOException Well... If something goes wrong :) */ private void resolveGce(String gceNetworkSetting, InetAddress[] expected) throws IOException { - Settings nodeSettings = Settings.builder() - .put("network.host", gceNetworkSetting) - .build(); + Settings nodeSettings = Settings.builder().put("network.host", gceNetworkSetting).build(); GceMetadataServiceMock mock = new GceMetadataServiceMock(nodeSettings); NetworkService networkService = new NetworkService(Collections.singletonList(new GceNameResolver(mock))); try { InetAddress[] addresses = networkService.resolveBindHostAddresses( - NetworkService.GLOBAL_NETWORK_BIND_HOST_SETTING.get(nodeSettings).toArray(Strings.EMPTY_ARRAY)); + NetworkService.GLOBAL_NETWORK_BIND_HOST_SETTING.get(nodeSettings).toArray(Strings.EMPTY_ARRAY) + ); if (expected == null) { fail("We should get a IllegalArgumentException when setting network.host: _gce:doesnotexist_"); } diff --git a/plugins/discovery-gce/src/test/java/org/elasticsearch/discovery/gce/RetryHttpInitializerWrapperTests.java b/plugins/discovery-gce/src/test/java/org/elasticsearch/discovery/gce/RetryHttpInitializerWrapperTests.java index 2dc6ffee45a0b..2c29523ff4c91 100644 --- a/plugins/discovery-gce/src/test/java/org/elasticsearch/discovery/gce/RetryHttpInitializerWrapperTests.java +++ b/plugins/discovery-gce/src/test/java/org/elasticsearch/discovery/gce/RetryHttpInitializerWrapperTests.java @@ -23,6 +23,7 @@ import com.google.api.client.testing.http.MockLowLevelHttpResponse; import com.google.api.client.testing.util.MockSleeper; import com.google.api.services.compute.Compute; + import org.elasticsearch.core.TimeValue; import org.elasticsearch.test.ESTestCase; @@ -83,20 +84,20 @@ public LowLevelHttpRequest buildRequest(String method, String url) { } public void testSimpleRetry() throws Exception { - FailThenSuccessBackoffTransport fakeTransport = - new FailThenSuccessBackoffTransport(HttpStatusCodes.STATUS_CODE_SERVER_ERROR, 3); + FailThenSuccessBackoffTransport fakeTransport = new FailThenSuccessBackoffTransport(HttpStatusCodes.STATUS_CODE_SERVER_ERROR, 3); - MockGoogleCredential credential = RetryHttpInitializerWrapper.newMockCredentialBuilder() - .build(); + MockGoogleCredential credential = RetryHttpInitializerWrapper.newMockCredentialBuilder().build(); MockSleeper mockSleeper = new MockSleeper(); - RetryHttpInitializerWrapper retryHttpInitializerWrapper = new RetryHttpInitializerWrapper(credential, mockSleeper, - TimeValue.timeValueSeconds(5)); + RetryHttpInitializerWrapper retryHttpInitializerWrapper = new RetryHttpInitializerWrapper( + credential, + mockSleeper, + TimeValue.timeValueSeconds(5) + ); - Compute client = new Compute.Builder(fakeTransport, new JacksonFactory(), null) - .setHttpRequestInitializer(retryHttpInitializerWrapper) - .setApplicationName("test") - .build(); + Compute client = new Compute.Builder(fakeTransport, new JacksonFactory(), null).setHttpRequestInitializer( + retryHttpInitializerWrapper + ).setApplicationName("test").build(); HttpRequest request = client.getRequestFactory().buildRequest("Get", new GenericUrl("http://elasticsearch.com"), null); HttpResponse response = request.execute(); @@ -109,11 +110,12 @@ public void testRetryWaitTooLong() throws Exception { TimeValue maxWaitTime = TimeValue.timeValueMillis(10); int maxRetryTimes = 50; - FailThenSuccessBackoffTransport fakeTransport = - new FailThenSuccessBackoffTransport(HttpStatusCodes.STATUS_CODE_SERVER_ERROR, maxRetryTimes); + FailThenSuccessBackoffTransport fakeTransport = new FailThenSuccessBackoffTransport( + HttpStatusCodes.STATUS_CODE_SERVER_ERROR, + maxRetryTimes + ); JsonFactory jsonFactory = new JacksonFactory(); - MockGoogleCredential credential = RetryHttpInitializerWrapper.newMockCredentialBuilder() - .build(); + MockGoogleCredential credential = RetryHttpInitializerWrapper.newMockCredentialBuilder().build(); MockSleeper oneTimeSleeper = new MockSleeper() { @Override @@ -125,10 +127,9 @@ public void sleep(long millis) throws InterruptedException { RetryHttpInitializerWrapper retryHttpInitializerWrapper = new RetryHttpInitializerWrapper(credential, oneTimeSleeper, maxWaitTime); - Compute client = new Compute.Builder(fakeTransport, jsonFactory, null) - .setHttpRequestInitializer(retryHttpInitializerWrapper) - .setApplicationName("test") - .build(); + Compute client = new Compute.Builder(fakeTransport, jsonFactory, null).setHttpRequestInitializer(retryHttpInitializerWrapper) + .setApplicationName("test") + .build(); HttpRequest request1 = client.getRequestFactory().buildRequest("Get", new GenericUrl("http://elasticsearch.com"), null); try { @@ -142,19 +143,23 @@ public void sleep(long millis) throws InterruptedException { } public void testIOExceptionRetry() throws Exception { - FailThenSuccessBackoffTransport fakeTransport = - new FailThenSuccessBackoffTransport(HttpStatusCodes.STATUS_CODE_SERVER_ERROR, 1, true); + FailThenSuccessBackoffTransport fakeTransport = new FailThenSuccessBackoffTransport( + HttpStatusCodes.STATUS_CODE_SERVER_ERROR, + 1, + true + ); - MockGoogleCredential credential = RetryHttpInitializerWrapper.newMockCredentialBuilder() - .build(); + MockGoogleCredential credential = RetryHttpInitializerWrapper.newMockCredentialBuilder().build(); MockSleeper mockSleeper = new MockSleeper(); - RetryHttpInitializerWrapper retryHttpInitializerWrapper = new RetryHttpInitializerWrapper(credential, mockSleeper, - TimeValue.timeValueSeconds(30L)); - - Compute client = new Compute.Builder(fakeTransport, new JacksonFactory(), null) - .setHttpRequestInitializer(retryHttpInitializerWrapper) - .setApplicationName("test") - .build(); + RetryHttpInitializerWrapper retryHttpInitializerWrapper = new RetryHttpInitializerWrapper( + credential, + mockSleeper, + TimeValue.timeValueSeconds(30L) + ); + + Compute client = new Compute.Builder(fakeTransport, new JacksonFactory(), null).setHttpRequestInitializer( + retryHttpInitializerWrapper + ).setApplicationName("test").build(); HttpRequest request = client.getRequestFactory().buildRequest("Get", new GenericUrl("http://elasticsearch.com"), null); HttpResponse response = request.execute(); diff --git a/plugins/discovery-gce/src/yamlRestTest/java/org/elasticsearch/discovery/gce/DiscoveryGceClientYamlTestSuiteIT.java b/plugins/discovery-gce/src/yamlRestTest/java/org/elasticsearch/discovery/gce/DiscoveryGceClientYamlTestSuiteIT.java index 9815db39a61c5..af3fd118b5061 100644 --- a/plugins/discovery-gce/src/yamlRestTest/java/org/elasticsearch/discovery/gce/DiscoveryGceClientYamlTestSuiteIT.java +++ b/plugins/discovery-gce/src/yamlRestTest/java/org/elasticsearch/discovery/gce/DiscoveryGceClientYamlTestSuiteIT.java @@ -25,4 +25,3 @@ public static Iterable parameters() throws Exception { return ESClientYamlSuiteTestCase.createParameters(); } } - diff --git a/plugins/ingest-attachment/src/main/java/org/elasticsearch/ingest/attachment/AttachmentProcessor.java b/plugins/ingest-attachment/src/main/java/org/elasticsearch/ingest/attachment/AttachmentProcessor.java index 22857331f896c..4dfa3253275cf 100644 --- a/plugins/ingest-attachment/src/main/java/org/elasticsearch/ingest/attachment/AttachmentProcessor.java +++ b/plugins/ingest-attachment/src/main/java/org/elasticsearch/ingest/attachment/AttachmentProcessor.java @@ -48,9 +48,18 @@ public final class AttachmentProcessor extends AbstractProcessor { private final String indexedCharsField; private final String resourceName; - AttachmentProcessor(String tag, String description, String field, String targetField, Set properties, - int indexedChars, boolean ignoreMissing, String indexedCharsField, String resourceName, - boolean removeBinary) { + AttachmentProcessor( + String tag, + String description, + String field, + String targetField, + Set properties, + int indexedChars, + boolean ignoreMissing, + String indexedCharsField, + String resourceName, + boolean removeBinary + ) { super(tag, description); this.field = field; this.targetField = targetField; @@ -203,8 +212,12 @@ public static final class Factory implements Processor.Factory { static final Set DEFAULT_PROPERTIES = EnumSet.allOf(Property.class); @Override - public AttachmentProcessor create(Map registry, String processorTag, - String description, Map config) throws Exception { + public AttachmentProcessor create( + Map registry, + String processorTag, + String description, + Map config + ) throws Exception { String field = readStringProperty(TYPE, processorTag, config, "field"); String resourceName = readOptionalStringProperty(TYPE, processorTag, config, "resource_name"); String targetField = readStringProperty(TYPE, processorTag, config, "target_field", "attachment"); @@ -221,16 +234,30 @@ public AttachmentProcessor create(Map registry, Strin try { properties.add(Property.parse(fieldName)); } catch (Exception e) { - throw newConfigurationException(TYPE, processorTag, "properties", "illegal field option [" + - fieldName + "]. valid values are " + Arrays.toString(Property.values())); + throw newConfigurationException( + TYPE, + processorTag, + "properties", + "illegal field option [" + fieldName + "]. valid values are " + Arrays.toString(Property.values()) + ); } } } else { properties = DEFAULT_PROPERTIES; } - return new AttachmentProcessor(processorTag, description, field, targetField, properties, indexedChars, ignoreMissing, - indexedCharsField, resourceName, removeBinary); + return new AttachmentProcessor( + processorTag, + description, + field, + targetField, + properties, + indexedChars, + ignoreMissing, + indexedCharsField, + resourceName, + removeBinary + ); } } diff --git a/plugins/ingest-attachment/src/main/java/org/elasticsearch/ingest/attachment/IngestAttachmentPlugin.java b/plugins/ingest-attachment/src/main/java/org/elasticsearch/ingest/attachment/IngestAttachmentPlugin.java index 385cb5f664a97..420f2102b824a 100644 --- a/plugins/ingest-attachment/src/main/java/org/elasticsearch/ingest/attachment/IngestAttachmentPlugin.java +++ b/plugins/ingest-attachment/src/main/java/org/elasticsearch/ingest/attachment/IngestAttachmentPlugin.java @@ -8,13 +8,13 @@ package org.elasticsearch.ingest.attachment; -import java.util.Collections; -import java.util.Map; - import org.elasticsearch.ingest.Processor; import org.elasticsearch.plugins.IngestPlugin; import org.elasticsearch.plugins.Plugin; +import java.util.Collections; +import java.util.Map; + public class IngestAttachmentPlugin extends Plugin implements IngestPlugin { @Override diff --git a/plugins/ingest-attachment/src/main/java/org/elasticsearch/ingest/attachment/TikaImpl.java b/plugins/ingest-attachment/src/main/java/org/elasticsearch/ingest/attachment/TikaImpl.java index 62c3a7bc94de4..7e999b2577d4f 100644 --- a/plugins/ingest-attachment/src/main/java/org/elasticsearch/ingest/attachment/TikaImpl.java +++ b/plugins/ingest-attachment/src/main/java/org/elasticsearch/ingest/attachment/TikaImpl.java @@ -17,10 +17,10 @@ import org.apache.tika.parser.ParserDecorator; import org.elasticsearch.SpecialPermission; import org.elasticsearch.bootstrap.FilePermissionUtils; +import org.elasticsearch.core.PathUtils; +import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.jdk.JarHell; import org.elasticsearch.jdk.JavaVersion; -import org.elasticsearch.core.SuppressForbidden; -import org.elasticsearch.core.PathUtils; import java.io.ByteArrayInputStream; import java.io.IOException; @@ -53,15 +53,17 @@ final class TikaImpl { /** Exclude some formats */ - private static final Set EXCLUDES = new HashSet<>(Arrays.asList( - MediaType.application("vnd.ms-visio.drawing"), - MediaType.application("vnd.ms-visio.drawing.macroenabled.12"), - MediaType.application("vnd.ms-visio.stencil"), - MediaType.application("vnd.ms-visio.stencil.macroenabled.12"), - MediaType.application("vnd.ms-visio.template"), - MediaType.application("vnd.ms-visio.template.macroenabled.12"), - MediaType.application("vnd.ms-visio.drawing") - )); + private static final Set EXCLUDES = new HashSet<>( + Arrays.asList( + MediaType.application("vnd.ms-visio.drawing"), + MediaType.application("vnd.ms-visio.drawing.macroenabled.12"), + MediaType.application("vnd.ms-visio.stencil"), + MediaType.application("vnd.ms-visio.stencil.macroenabled.12"), + MediaType.application("vnd.ms-visio.template"), + MediaType.application("vnd.ms-visio.template.macroenabled.12"), + MediaType.application("vnd.ms-visio.drawing") + ) + ); /** subset of parsers for types we support */ private static final Parser PARSERS[] = new Parser[] { @@ -76,8 +78,7 @@ final class TikaImpl { new org.apache.tika.parser.odf.OpenDocumentParser(), new org.apache.tika.parser.iwork.IWorkPackageParser(), new org.apache.tika.parser.xml.DcXMLParser(), - new org.apache.tika.parser.epub.EpubParser(), - }; + new org.apache.tika.parser.epub.EpubParser(), }; /** autodetector based on this subset */ private static final AutoDetectParser PARSER_INSTANCE = new AutoDetectParser(PARSERS); @@ -93,8 +94,10 @@ static String parse(final byte content[], final Metadata metadata, final int lim SpecialPermission.check(); try { - return AccessController.doPrivileged((PrivilegedExceptionAction) - () -> TIKA_INSTANCE.parseToString(new ByteArrayInputStream(content), metadata, limit), RESTRICTED_CONTEXT); + return AccessController.doPrivileged( + (PrivilegedExceptionAction) () -> TIKA_INSTANCE.parseToString(new ByteArrayInputStream(content), metadata, limit), + RESTRICTED_CONTEXT + ); } catch (PrivilegedActionException e) { // checked exception from tika: unbox it Throwable cause = e.getCause(); @@ -111,9 +114,7 @@ static String parse(final byte content[], final Metadata metadata, final int lim // apply additional containment for parsers, this is intersected with the current permissions // its hairy, but worth it so we don't have some XML flaw reading random crap from the FS private static final AccessControlContext RESTRICTED_CONTEXT = new AccessControlContext( - new ProtectionDomain[] { - new ProtectionDomain(null, getRestrictedPermissions()) - } + new ProtectionDomain[] { new ProtectionDomain(null, getRestrictedPermissions()) } ); // compute some minimal permissions for parsers. they only get r/w access to the java temp directory, @@ -131,7 +132,7 @@ static PermissionCollection getRestrictedPermissions() { addReadPermissions(perms, JarHell.parseClassPath()); // plugin jars if (TikaImpl.class.getClassLoader() instanceof URLClassLoader) { - URL[] urls = ((URLClassLoader)TikaImpl.class.getClassLoader()).getURLs(); + URL[] urls = ((URLClassLoader) TikaImpl.class.getClassLoader()).getURLs(); Set set = new LinkedHashSet<>(Arrays.asList(urls)); if (set.size() != urls.length) { throw new AssertionError("duplicate jars: " + Arrays.toString(urls)); @@ -139,8 +140,13 @@ static PermissionCollection getRestrictedPermissions() { addReadPermissions(perms, set); } // jvm's java.io.tmpdir (needs read/write) - FilePermissionUtils.addDirectoryPath(perms, "java.io.tmpdir", PathUtils.get(System.getProperty("java.io.tmpdir")), - "read,readlink,write,delete", false); + FilePermissionUtils.addDirectoryPath( + perms, + "java.io.tmpdir", + PathUtils.get(System.getProperty("java.io.tmpdir")), + "read,readlink,write,delete", + false + ); } catch (IOException e) { throw new UncheckedIOException(e); } diff --git a/plugins/ingest-attachment/src/test/java/org/elasticsearch/ingest/attachment/AttachmentProcessorTests.java b/plugins/ingest-attachment/src/test/java/org/elasticsearch/ingest/attachment/AttachmentProcessorTests.java index 63197de7f7981..257eb24392a65 100644 --- a/plugins/ingest-attachment/src/test/java/org/elasticsearch/ingest/attachment/AttachmentProcessorTests.java +++ b/plugins/ingest-attachment/src/test/java/org/elasticsearch/ingest/attachment/AttachmentProcessorTests.java @@ -43,8 +43,18 @@ public class AttachmentProcessorTests extends ESTestCase { @Before public void createStandardProcessor() { - processor = new AttachmentProcessor(randomAlphaOfLength(10), null, "source_field", - "target_field", EnumSet.allOf(AttachmentProcessor.Property.class), 10000, false, null, null, false); + processor = new AttachmentProcessor( + randomAlphaOfLength(10), + null, + "source_field", + "target_field", + EnumSet.allOf(AttachmentProcessor.Property.class), + 10000, + false, + null, + null, + false + ); } public void testEnglishTextDocument() throws Exception { @@ -58,9 +68,10 @@ public void testEnglishTextDocument() throws Exception { } public void testHtmlDocumentWithRandomFields() throws Exception { - //date is not present in the html doc - ArrayList fieldsList = new ArrayList<>(EnumSet.complementOf(EnumSet.of - (AttachmentProcessor.Property.DATE))); + // date is not present in the html doc + ArrayList fieldsList = new ArrayList<>( + EnumSet.complementOf(EnumSet.of(AttachmentProcessor.Property.DATE)) + ); Set selectedProperties = new HashSet<>(); int numFields = randomIntBetween(1, fieldsList.size()); @@ -76,8 +87,18 @@ public void testHtmlDocumentWithRandomFields() throws Exception { if (randomBoolean()) { selectedProperties.add(AttachmentProcessor.Property.DATE); } - processor = new AttachmentProcessor(randomAlphaOfLength(10), null, "source_field", - "target_field", selectedProperties, 10000, false, null, null, false); + processor = new AttachmentProcessor( + randomAlphaOfLength(10), + null, + "source_field", + "target_field", + selectedProperties, + 10000, + false, + null, + null, + false + ); Map attachmentData = parseDocument("htmlWithEmptyDateMeta.html", processor); assertThat(attachmentData.keySet(), hasSize(selectedFieldNames.length)); @@ -107,49 +128,51 @@ public void testEmptyTextDocument() throws Exception { public void testWordDocument() throws Exception { Map attachmentData = parseDocument("issue-104.docx", processor); - assertThat(attachmentData.keySet(), containsInAnyOrder("content", "language", "date", "author", "content_type", - "content_length")); + assertThat(attachmentData.keySet(), containsInAnyOrder("content", "language", "date", "author", "content_type", "content_length")); assertThat(attachmentData.get("content"), is(notNullValue())); assertThat(attachmentData.get("language"), is("en")); assertThat(attachmentData.get("date"), is("2012-10-12T11:17:00Z")); assertThat(attachmentData.get("author"), is("Windows User")); assertThat(attachmentData.get("content_length"), is(notNullValue())); - assertThat(attachmentData.get("content_type").toString(), - is("application/vnd.openxmlformats-officedocument.wordprocessingml.document")); + assertThat( + attachmentData.get("content_type").toString(), + is("application/vnd.openxmlformats-officedocument.wordprocessingml.document") + ); } public void testWordDocumentWithVisioSchema() throws Exception { Map attachmentData = parseDocument("issue-22077.docx", processor); - assertThat(attachmentData.keySet(), containsInAnyOrder("content", "language", "date", "author", "content_type", - "content_length")); + assertThat(attachmentData.keySet(), containsInAnyOrder("content", "language", "date", "author", "content_type", "content_length")); assertThat(attachmentData.get("content").toString(), containsString("Table of Contents")); assertThat(attachmentData.get("language"), is("en")); assertThat(attachmentData.get("date"), is("2015-01-06T18:07:00Z")); assertThat(attachmentData.get("author"), is(notNullValue())); assertThat(attachmentData.get("content_length"), is(notNullValue())); - assertThat(attachmentData.get("content_type").toString(), - is("application/vnd.openxmlformats-officedocument.wordprocessingml.document")); + assertThat( + attachmentData.get("content_type").toString(), + is("application/vnd.openxmlformats-officedocument.wordprocessingml.document") + ); } public void testLegacyWordDocumentWithVisioSchema() throws Exception { Map attachmentData = parseDocument("issue-22077.doc", processor); - assertThat(attachmentData.keySet(), containsInAnyOrder("content", "language", "date", "author", "content_type", - "content_length")); + assertThat(attachmentData.keySet(), containsInAnyOrder("content", "language", "date", "author", "content_type", "content_length")); assertThat(attachmentData.get("content").toString(), containsString("Table of Contents")); assertThat(attachmentData.get("language"), is("en")); assertThat(attachmentData.get("date"), is("2016-12-16T15:04:00Z")); assertThat(attachmentData.get("author"), is(notNullValue())); assertThat(attachmentData.get("content_length"), is(notNullValue())); - assertThat(attachmentData.get("content_type").toString(), - is("application/msword")); + assertThat(attachmentData.get("content_type").toString(), is("application/msword")); } public void testPdf() throws Exception { Map attachmentData = parseDocument("test.pdf", processor); - assertThat(attachmentData.get("content"), - is("This is a test, with umlauts, from München\n\nAlso contains newlines for testing.\n\nAnd one more.")); + assertThat( + attachmentData.get("content"), + is("This is a test, with umlauts, from München\n\nAlso contains newlines for testing.\n\nAnd one more.") + ); assertThat(attachmentData.get("content_type").toString(), is("application/pdf")); assertThat(attachmentData.get("content_length"), is(notNullValue())); } @@ -169,8 +192,10 @@ public void testEncryptedPdf() throws Exception { public void testHtmlDocument() throws Exception { Map attachmentData = parseDocument("htmlWithEmptyDateMeta.html", processor); - assertThat(attachmentData.keySet(), containsInAnyOrder("language", "content", "author", "keywords", "title", "content_type", - "content_length")); + assertThat( + attachmentData.keySet(), + containsInAnyOrder("language", "content", "author", "keywords", "title", "content_type", "content_length") + ); assertThat(attachmentData.get("language"), is("en")); assertThat(attachmentData.get("content"), is(notNullValue())); assertThat(attachmentData.get("content_length"), is(notNullValue())); @@ -190,8 +215,10 @@ public void testXHtmlDocument() throws Exception { public void testEpubDocument() throws Exception { Map attachmentData = parseDocument("testEPUB.epub", processor); - assertThat(attachmentData.keySet(), containsInAnyOrder("language", "content", "author", "title", "content_type", "content_length", - "date", "keywords")); + assertThat( + attachmentData.keySet(), + containsInAnyOrder("language", "content", "author", "title", "content_type", "content_length", "date", "keywords") + ); assertThat(attachmentData.get("content_type").toString(), containsString("application/epub+zip")); } @@ -233,11 +260,23 @@ public void testParseAsBytesArray() throws Exception { } public void testNullValueWithIgnoreMissing() throws Exception { - IngestDocument originalIngestDocument = RandomDocumentPicks.randomIngestDocument(random(), - Collections.singletonMap("source_field", null)); + IngestDocument originalIngestDocument = RandomDocumentPicks.randomIngestDocument( + random(), + Collections.singletonMap("source_field", null) + ); IngestDocument ingestDocument = new IngestDocument(originalIngestDocument); - Processor processor = new AttachmentProcessor(randomAlphaOfLength(10), null, "source_field", - "randomTarget", null, 10, true, null, null, false); + Processor processor = new AttachmentProcessor( + randomAlphaOfLength(10), + null, + "source_field", + "randomTarget", + null, + 10, + true, + null, + null, + false + ); processor.execute(ingestDocument); assertIngestDocument(originalIngestDocument, ingestDocument); } @@ -245,18 +284,40 @@ public void testNullValueWithIgnoreMissing() throws Exception { public void testNonExistentWithIgnoreMissing() throws Exception { IngestDocument originalIngestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.emptyMap()); IngestDocument ingestDocument = new IngestDocument(originalIngestDocument); - Processor processor = new AttachmentProcessor(randomAlphaOfLength(10), null, "source_field", - "randomTarget", null, 10, true, null, null, false); + Processor processor = new AttachmentProcessor( + randomAlphaOfLength(10), + null, + "source_field", + "randomTarget", + null, + 10, + true, + null, + null, + false + ); processor.execute(ingestDocument); assertIngestDocument(originalIngestDocument, ingestDocument); } public void testNullWithoutIgnoreMissing() throws Exception { - IngestDocument originalIngestDocument = RandomDocumentPicks.randomIngestDocument(random(), - Collections.singletonMap("source_field", null)); + IngestDocument originalIngestDocument = RandomDocumentPicks.randomIngestDocument( + random(), + Collections.singletonMap("source_field", null) + ); IngestDocument ingestDocument = new IngestDocument(originalIngestDocument); - Processor processor = new AttachmentProcessor(randomAlphaOfLength(10), null, "source_field", - "randomTarget", null, 10, false, null, null, false); + Processor processor = new AttachmentProcessor( + randomAlphaOfLength(10), + null, + "source_field", + "randomTarget", + null, + 10, + false, + null, + null, + false + ); Exception exception = expectThrows(Exception.class, () -> processor.execute(ingestDocument)); assertThat(exception.getMessage(), equalTo("field [source_field] is null, cannot parse.")); } @@ -264,8 +325,18 @@ public void testNullWithoutIgnoreMissing() throws Exception { public void testNonExistentWithoutIgnoreMissing() throws Exception { IngestDocument originalIngestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.emptyMap()); IngestDocument ingestDocument = new IngestDocument(originalIngestDocument); - Processor processor = new AttachmentProcessor(randomAlphaOfLength(10), null, "source_field", - "randomTarget", null, 10, false, null, null, false); + Processor processor = new AttachmentProcessor( + randomAlphaOfLength(10), + null, + "source_field", + "randomTarget", + null, + 10, + false, + null, + null, + false + ); Exception exception = expectThrows(Exception.class, () -> processor.execute(ingestDocument)); assertThat(exception.getMessage(), equalTo("field [source_field] not present as part of path [source_field]")); } @@ -279,9 +350,12 @@ private Map parseDocument(String file, AttachmentProcessor proce return parseDocument(file, processor, optionalFields, false); } - private Map parseDocument(String file, AttachmentProcessor processor, Map optionalFields, - boolean includeResourceName) - throws Exception { + private Map parseDocument( + String file, + AttachmentProcessor processor, + Map optionalFields, + boolean includeResourceName + ) throws Exception { Map document = new HashMap<>(); document.put("source_field", getAsBinaryOrBase64(file)); if (includeResourceName) { @@ -298,8 +372,18 @@ private Map parseDocument(String file, AttachmentProcessor proce } public void testIndexedChars() throws Exception { - processor = new AttachmentProcessor(randomAlphaOfLength(10), null, "source_field", - "target_field", EnumSet.allOf(AttachmentProcessor.Property.class), 19, false, null, null, false); + processor = new AttachmentProcessor( + randomAlphaOfLength(10), + null, + "source_field", + "target_field", + EnumSet.allOf(AttachmentProcessor.Property.class), + 19, + false, + null, + null, + false + ); Map attachmentData = parseDocument("text-in-english.txt", processor); @@ -309,8 +393,18 @@ public void testIndexedChars() throws Exception { assertThat(attachmentData.get("content_type").toString(), containsString("text/plain")); assertThat(attachmentData.get("content_length"), is(19L)); - processor = new AttachmentProcessor(randomAlphaOfLength(10), null, "source_field", - "target_field", EnumSet.allOf(AttachmentProcessor.Property.class), 19, false, "max_length", null, false); + processor = new AttachmentProcessor( + randomAlphaOfLength(10), + null, + "source_field", + "target_field", + EnumSet.allOf(AttachmentProcessor.Property.class), + 19, + false, + "max_length", + null, + false + ); attachmentData = parseDocument("text-in-english.txt", processor); @@ -339,12 +433,25 @@ public void testIndexedChars() throws Exception { } public void testIndexedCharsWithResourceName() throws Exception { - processor = new AttachmentProcessor(randomAlphaOfLength(10), null, "source_field", - "target_field", EnumSet.allOf(AttachmentProcessor.Property.class), 100, - false, null, "resource_name", false); - - Map attachmentData = parseDocument("text-cjk-big5.txt", processor, Collections.singletonMap("max_length", 100), - true); + processor = new AttachmentProcessor( + randomAlphaOfLength(10), + null, + "source_field", + "target_field", + EnumSet.allOf(AttachmentProcessor.Property.class), + 100, + false, + null, + "resource_name", + false + ); + + Map attachmentData = parseDocument( + "text-cjk-big5.txt", + processor, + Collections.singletonMap("max_length", 100), + true + ); assertThat(attachmentData.keySet(), containsInAnyOrder("language", "content", "content_type", "content_length")); assertThat(attachmentData.get("content").toString(), containsString("碩鼠碩鼠,無食我黍!")); @@ -380,8 +487,18 @@ public void testRemoveBinary() throws Exception { } { // Remove the binary field. - processor = new AttachmentProcessor(randomAlphaOfLength(10), null, "source_field", - "target_field", EnumSet.allOf(AttachmentProcessor.Property.class), 10000, false, null, null, true); + processor = new AttachmentProcessor( + randomAlphaOfLength(10), + null, + "source_field", + "target_field", + EnumSet.allOf(AttachmentProcessor.Property.class), + 10000, + false, + null, + null, + true + ); Map document = new HashMap<>(); document.put("source_field", getAsBinaryOrBase64("text-in-english.txt")); IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document); diff --git a/plugins/ingest-attachment/src/test/java/org/elasticsearch/ingest/attachment/TikaDocTests.java b/plugins/ingest-attachment/src/test/java/org/elasticsearch/ingest/attachment/TikaDocTests.java index 5287cbcd3cfb4..4e4507d2f17df 100644 --- a/plugins/ingest-attachment/src/test/java/org/elasticsearch/ingest/attachment/TikaDocTests.java +++ b/plugins/ingest-attachment/src/test/java/org/elasticsearch/ingest/attachment/TikaDocTests.java @@ -41,8 +41,8 @@ public void testFiles() throws Exception { try (DirectoryStream stream = Files.newDirectoryStream(tmp)) { for (Path doc : stream) { - logger.debug("parsing: {}", doc); - assertParseable(doc); + logger.debug("parsing: {}", doc); + assertParseable(doc); } } } diff --git a/plugins/ingest-attachment/src/test/java/org/elasticsearch/ingest/attachment/TikaImplTests.java b/plugins/ingest-attachment/src/test/java/org/elasticsearch/ingest/attachment/TikaImplTests.java index ebdb4ad75c913..66ac34329213b 100644 --- a/plugins/ingest-attachment/src/test/java/org/elasticsearch/ingest/attachment/TikaImplTests.java +++ b/plugins/ingest-attachment/src/test/java/org/elasticsearch/ingest/attachment/TikaImplTests.java @@ -12,8 +12,8 @@ public class TikaImplTests extends ESTestCase { - public void testTikaLoads() throws Exception { - Class.forName("org.elasticsearch.ingest.attachment.TikaImpl"); - } + public void testTikaLoads() throws Exception { + Class.forName("org.elasticsearch.ingest.attachment.TikaImpl"); + } } diff --git a/plugins/ingest-attachment/src/yamlRestTest/java/org/elasticsearch/ingest/attachment/IngestAttachmentClientYamlTestSuiteIT.java b/plugins/ingest-attachment/src/yamlRestTest/java/org/elasticsearch/ingest/attachment/IngestAttachmentClientYamlTestSuiteIT.java index 9139c2bc9b085..5c43c969986e9 100644 --- a/plugins/ingest-attachment/src/yamlRestTest/java/org/elasticsearch/ingest/attachment/IngestAttachmentClientYamlTestSuiteIT.java +++ b/plugins/ingest-attachment/src/yamlRestTest/java/org/elasticsearch/ingest/attachment/IngestAttachmentClientYamlTestSuiteIT.java @@ -25,4 +25,3 @@ public static Iterable parameters() throws Exception { return ESClientYamlSuiteTestCase.createParameters(); } } - diff --git a/plugins/mapper-annotated-text/src/internalClusterTest/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextFieldMapperTests.java b/plugins/mapper-annotated-text/src/internalClusterTest/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextFieldMapperTests.java index 63d2ee0534c2c..e9b5c691a0682 100644 --- a/plugins/mapper-annotated-text/src/internalClusterTest/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextFieldMapperTests.java +++ b/plugins/mapper-annotated-text/src/internalClusterTest/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextFieldMapperTests.java @@ -24,9 +24,6 @@ import org.apache.lucene.index.TermsEnum; import org.apache.lucene.util.BytesRef; import org.elasticsearch.common.Strings; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.analysis.AnalyzerScope; import org.elasticsearch.index.analysis.CharFilterFactory; @@ -43,6 +40,9 @@ import org.elasticsearch.index.mapper.ParsedDocument; import org.elasticsearch.index.mapper.TextFieldMapper; import org.elasticsearch.plugins.Plugin; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; import java.io.IOException; import java.util.Arrays; @@ -77,16 +77,14 @@ protected Object getSampleValueForDocument() { protected void registerParameters(ParameterChecker checker) throws IOException { checker.registerUpdateCheck(b -> { - b.field("analyzer", "default"); - b.field("search_analyzer", "keyword"); - }, - m -> assertEquals("keyword", m.fieldType().getTextSearchInfo().getSearchAnalyzer().name())); + b.field("analyzer", "default"); + b.field("search_analyzer", "keyword"); + }, m -> assertEquals("keyword", m.fieldType().getTextSearchInfo().getSearchAnalyzer().name())); checker.registerUpdateCheck(b -> { - b.field("analyzer", "default"); - b.field("search_analyzer", "keyword"); - b.field("search_quote_analyzer", "keyword"); - }, - m -> assertEquals("keyword", m.fieldType().getTextSearchInfo().getSearchQuoteAnalyzer().name())); + b.field("analyzer", "default"); + b.field("search_analyzer", "keyword"); + b.field("search_quote_analyzer", "keyword"); + }, m -> assertEquals("keyword", m.fieldType().getTextSearchInfo().getSearchQuoteAnalyzer().name())); checker.registerConflictCheck("store", b -> b.field("store", true)); checker.registerConflictCheck("index_options", b -> b.field("index_options", "docs")); @@ -97,26 +95,20 @@ protected void registerParameters(ParameterChecker checker) throws IOException { checker.registerConflictCheck("position_increment_gap", b -> b.field("position_increment_gap", 10)); // norms can be set from true to false, but not vice versa - checker.registerConflictCheck("norms", - fieldMapping(b -> { - b.field("type", "annotated_text"); - b.field("norms", false); - }), - fieldMapping(b -> { - b.field("type", "annotated_text"); - b.field("norms", true); - })); - checker.registerUpdateCheck( - b -> { - b.field("type", "annotated_text"); - b.field("norms", true); - }, - b -> { - b.field("type", "annotated_text"); - b.field("norms", false); - }, - m -> assertFalse(m.fieldType().getTextSearchInfo().hasNorms()) - ); + checker.registerConflictCheck("norms", fieldMapping(b -> { + b.field("type", "annotated_text"); + b.field("norms", false); + }), fieldMapping(b -> { + b.field("type", "annotated_text"); + b.field("norms", true); + })); + checker.registerUpdateCheck(b -> { + b.field("type", "annotated_text"); + b.field("norms", true); + }, b -> { + b.field("type", "annotated_text"); + b.field("norms", false); + }, m -> assertFalse(m.fieldType().getTextSearchInfo().hasNorms())); } @Override @@ -189,7 +181,6 @@ public void testAnnotationInjection() throws IOException { assertEquals(0, postings.nextDoc()); assertEquals(2, postings.nextPosition()); - assertTrue(terms.seekExact(new BytesRef("hush"))); postings = terms.postings(null, PostingsEnum.POSITIONS); assertEquals(0, postings.nextDoc()); @@ -238,8 +229,7 @@ public void testIndexedTermVectors() throws IOException { })); String text = "the quick [brown](Color) fox jumped over the lazy dog"; - ParsedDocument doc - = mapperService.documentMapper().parse(source(b -> b.field("field", text))); + ParsedDocument doc = mapperService.documentMapper().parse(source(b -> b.field("field", text))); withLuceneIndex(mapperService, iw -> iw.addDocument(doc.rootDoc()), reader -> { LeafReader leaf = reader.leaves().get(0).reader(); @@ -250,7 +240,7 @@ public void testIndexedTermVectors() throws IOException { while ((term = iterator.next()) != null) { foundTerms.add(term.utf8ToString()); } - //Check we have both text and annotation tokens + // Check we have both text and annotation tokens assertTrue(foundTerms.contains("brown")); assertTrue(foundTerms.contains("Color")); assertTrue(foundTerms.contains("fox")); @@ -374,62 +364,92 @@ public void testPositionIncrementGap() throws IOException { } public void testSearchAnalyzerSerialization() throws IOException { - String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("_doc") + String mapping = Strings.toString( + XContentFactory.jsonBuilder() + .startObject() + .startObject("_doc") .startObject("properties") - .startObject("field") - .field("type", "annotated_text") - .field("analyzer", "standard") - .field("search_analyzer", "keyword") - .endObject() - .endObject().endObject().endObject()); + .startObject("field") + .field("type", "annotated_text") + .field("analyzer", "standard") + .field("search_analyzer", "keyword") + .endObject() + .endObject() + .endObject() + .endObject() + ); DocumentMapper mapper = createDocumentMapper(mapping); - assertEquals(mapping, mapper.mappingSource().toString()); + assertEquals(mapping, mapper.mappingSource().toString()); // special case: default index analyzer - mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("_doc") + mapping = Strings.toString( + XContentFactory.jsonBuilder() + .startObject() + .startObject("_doc") .startObject("properties") - .startObject("field") - .field("type", "annotated_text") - .field("analyzer", "default") - .field("search_analyzer", "keyword") - .endObject() - .endObject().endObject().endObject()); + .startObject("field") + .field("type", "annotated_text") + .field("analyzer", "default") + .field("search_analyzer", "keyword") + .endObject() + .endObject() + .endObject() + .endObject() + ); mapper = createDocumentMapper(mapping); - assertEquals(mapping, mapper.mappingSource().toString()); + assertEquals(mapping, mapper.mappingSource().toString()); - mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("_doc") - .startObject("properties") - .startObject("field") - .field("type", "annotated_text") - .field("analyzer", "keyword") - .endObject() - .endObject().endObject().endObject()); + mapping = Strings.toString( + XContentFactory.jsonBuilder() + .startObject() + .startObject("_doc") + .startObject("properties") + .startObject("field") + .field("type", "annotated_text") + .field("analyzer", "keyword") + .endObject() + .endObject() + .endObject() + .endObject() + ); mapper = createDocumentMapper(mapping); - assertEquals(mapping, mapper.mappingSource().toString()); + assertEquals(mapping, mapper.mappingSource().toString()); // special case: default search analyzer - mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("_doc") - .startObject("properties") - .startObject("field") - .field("type", "annotated_text") - .field("analyzer", "keyword") - .field("search_analyzer", "default") - .endObject() - .endObject().endObject().endObject()); + mapping = Strings.toString( + XContentFactory.jsonBuilder() + .startObject() + .startObject("_doc") + .startObject("properties") + .startObject("field") + .field("type", "annotated_text") + .field("analyzer", "keyword") + .field("search_analyzer", "default") + .endObject() + .endObject() + .endObject() + .endObject() + ); mapper = createDocumentMapper(mapping); - assertEquals(mapping, mapper.mappingSource().toString()); - - mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("_doc") - .startObject("properties") - .startObject("field") - .field("type", "annotated_text") - .field("analyzer", "keyword") - .endObject() - .endObject().endObject().endObject()); + assertEquals(mapping, mapper.mappingSource().toString()); + + mapping = Strings.toString( + XContentFactory.jsonBuilder() + .startObject() + .startObject("_doc") + .startObject("properties") + .startObject("field") + .field("type", "annotated_text") + .field("analyzer", "keyword") + .endObject() + .endObject() + .endObject() + .endObject() + ); mapper = createDocumentMapper(mapping); XContentBuilder builder = XContentFactory.jsonBuilder(); @@ -444,32 +464,44 @@ public void testSearchAnalyzerSerialization() throws IOException { } public void testSearchQuoteAnalyzerSerialization() throws IOException { - String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("_doc") + String mapping = Strings.toString( + XContentFactory.jsonBuilder() + .startObject() + .startObject("_doc") .startObject("properties") - .startObject("field") - .field("type","annotated_text") - .field("analyzer", "standard") - .field("search_analyzer", "standard") - .field("search_quote_analyzer", "keyword") - .endObject() - .endObject().endObject().endObject()); + .startObject("field") + .field("type", "annotated_text") + .field("analyzer", "standard") + .field("search_analyzer", "standard") + .field("search_quote_analyzer", "keyword") + .endObject() + .endObject() + .endObject() + .endObject() + ); DocumentMapper mapper = createDocumentMapper(mapping); - assertEquals(mapping, mapper.mappingSource().toString()); + assertEquals(mapping, mapper.mappingSource().toString()); // special case: default index/search analyzer - mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("_doc") + mapping = Strings.toString( + XContentFactory.jsonBuilder() + .startObject() + .startObject("_doc") .startObject("properties") - .startObject("field") - .field("type", "annotated_text") - .field("analyzer", "default") - .field("search_analyzer", "default") - .field("search_quote_analyzer", "keyword") - .endObject() - .endObject().endObject().endObject()); + .startObject("field") + .field("type", "annotated_text") + .field("analyzer", "default") + .field("search_analyzer", "default") + .field("search_quote_analyzer", "keyword") + .endObject() + .endObject() + .endObject() + .endObject() + ); mapper = createDocumentMapper(mapping); - assertEquals(mapping, mapper.mappingSource().toString()); + assertEquals(mapping, mapper.mappingSource().toString()); } public void testTermVectors() throws IOException { @@ -536,8 +568,7 @@ public void testNotIndexedField() { b.field("type", "annotated_text"); b.field("index", false); }))); - assertEquals("Failed to parse mapping: unknown parameter [index] on mapper [field] of type [annotated_text]", - e.getMessage()); + assertEquals("Failed to parse mapping: unknown parameter [index] on mapper [field] of type [annotated_text]", e.getMessage()); } public void testAnalyzedFieldPositionIncrementWithoutPositions() { @@ -547,8 +578,7 @@ public void testAnalyzedFieldPositionIncrementWithoutPositions() { b.field("index_options", indexOptions); b.field("position_increment_gap", 0); }))); - assertThat(e.getMessage(), - containsString("Cannot set position_increment_gap on field [field] without positions enabled")); + assertThat(e.getMessage(), containsString("Cannot set position_increment_gap on field [field] without positions enabled")); } } diff --git a/plugins/mapper-annotated-text/src/main/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedPassageFormatter.java b/plugins/mapper-annotated-text/src/main/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedPassageFormatter.java index 7df66db6a62db..4d45af756b4b5 100644 --- a/plugins/mapper-annotated-text/src/main/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedPassageFormatter.java +++ b/plugins/mapper-annotated-text/src/main/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedPassageFormatter.java @@ -11,9 +11,9 @@ import org.apache.lucene.search.highlight.Encoder; import org.apache.lucene.search.uhighlight.Passage; import org.apache.lucene.search.uhighlight.PassageFormatter; -import org.elasticsearch.lucene.search.uhighlight.Snippet; import org.elasticsearch.index.mapper.annotatedtext.AnnotatedTextFieldMapper.AnnotatedText; import org.elasticsearch.index.mapper.annotatedtext.AnnotatedTextFieldMapper.AnnotatedText.AnnotationToken; +import org.elasticsearch.lucene.search.uhighlight.Snippet; import org.elasticsearch.search.fetch.subphase.highlight.HighlightUtils; import java.io.UnsupportedEncodingException; @@ -29,7 +29,6 @@ */ public class AnnotatedPassageFormatter extends PassageFormatter { - public static final String SEARCH_HIT_TYPE = "_hit_term"; private final Encoder encoder; AnnotatedText[] annotations; @@ -49,25 +48,25 @@ static class MarkupPassage { public void addUnlessOverlapping(Markup newMarkup) { // Fast exit. - if(newMarkup.start > lastMarkupEnd) { + if (newMarkup.start > lastMarkupEnd) { markups.add(newMarkup); lastMarkupEnd = newMarkup.end; return; } // Check to see if this new markup overlaps with any prior - int index=0; - for (Markup existingMarkup: markups) { - if(existingMarkup.samePosition(newMarkup)) { + int index = 0; + for (Markup existingMarkup : markups) { + if (existingMarkup.samePosition(newMarkup)) { existingMarkup.merge(newMarkup); return; } - if(existingMarkup.overlaps(newMarkup)) { + if (existingMarkup.overlaps(newMarkup)) { // existing markup wins - we throw away the new markup that would span this position return; } // markup list is in start offset order so we can insert at this position then shift others right - if(existingMarkup.isAfter(newMarkup)) { + if (existingMarkup.isAfter(newMarkup)) { markups.add(index, newMarkup); return; } @@ -78,41 +77,48 @@ public void addUnlessOverlapping(Markup newMarkup) { } } + static class Markup { int start; int end; String metadata; + Markup(int start, int end, String metadata) { super(); this.start = start; this.end = end; this.metadata = metadata; } + boolean isAfter(Markup other) { return start > other.end; } + void merge(Markup newMarkup) { // metadata is key1=value&key2=value&.... syntax used for urls assert samePosition(newMarkup); metadata += "&" + newMarkup.metadata; } + boolean samePosition(Markup other) { return this.start == other.start && this.end == other.end; } + boolean overlaps(Markup other) { - return (start<=other.start && end >= other.start) - || (start <= other.end && end >=other.end) - || (start>=other.start && end<=other.end); + return (start <= other.start && end >= other.start) + || (start <= other.end && end >= other.end) + || (start >= other.start && end <= other.end); } + @Override public String toString() { return "Markup [start=" + start + ", end=" + end + ", metadata=" + metadata + "]"; } - } + // Merge original annotations and search hits into a single set of markups for each passage - static MarkupPassage mergeAnnotations(AnnotationToken [] annotations, Passage passage){ + static MarkupPassage mergeAnnotations(AnnotationToken[] annotations, Passage passage) { try { MarkupPassage markupPassage = new MarkupPassage(); @@ -121,15 +127,19 @@ static MarkupPassage mergeAnnotations(AnnotationToken [] annotations, Passage pa int start = passage.getMatchStarts()[i]; int end = passage.getMatchEnds()[i]; String searchTerm = passage.getMatchTerms()[i].utf8ToString(); - Markup markup = new Markup(start, end, SEARCH_HIT_TYPE+"="+URLEncoder.encode(searchTerm, StandardCharsets.UTF_8.name())); + Markup markup = new Markup( + start, + end, + SEARCH_HIT_TYPE + "=" + URLEncoder.encode(searchTerm, StandardCharsets.UTF_8.name()) + ); markupPassage.addUnlessOverlapping(markup); } // Now add original text's annotations - ignoring any that might conflict with the search hits markup. - for (AnnotationToken token: annotations) { + for (AnnotationToken token : annotations) { int start = token.offset; int end = token.endOffset; - if(start >= passage.getStartOffset() && end<=passage.getEndOffset()) { + if (start >= passage.getStartOffset() && end <= passage.getEndOffset()) { String escapedValue = URLEncoder.encode(token.value, StandardCharsets.UTF_8.name()); Markup markup = new Markup(start, end, escapedValue); markupPassage.addUnlessOverlapping(markup); @@ -143,7 +153,6 @@ static MarkupPassage mergeAnnotations(AnnotationToken [] annotations, Passage pa } } - @Override public Snippet[] format(Passage[] passages, String content) { Snippet[] snippets = new Snippet[passages.length]; @@ -151,13 +160,12 @@ public Snippet[] format(Passage[] passages, String content) { int pos; int j = 0; for (Passage passage : passages) { - AnnotationToken [] annotations = getIntersectingAnnotations(passage.getStartOffset(), - passage.getEndOffset()); + AnnotationToken[] annotations = getIntersectingAnnotations(passage.getStartOffset(), passage.getEndOffset()); MarkupPassage mergedMarkup = mergeAnnotations(annotations, passage); StringBuilder sb = new StringBuilder(); pos = passage.getStartOffset(); - for(Markup markup: mergedMarkup.markups) { + for (Markup markup : mergedMarkup.markups) { int start = markup.start; int end = markup.end; // its possible to have overlapping terms @@ -176,13 +184,13 @@ public Snippet[] format(Passage[] passages, String content) { } // its possible a "term" from the analyzer could span a sentence boundary. append(sb, content, pos, Math.max(pos, passage.getEndOffset())); - //we remove the paragraph separator if present at the end of the snippet (we used it as separator between values) + // we remove the paragraph separator if present at the end of the snippet (we used it as separator between values) if (sb.charAt(sb.length() - 1) == HighlightUtils.PARAGRAPH_SEPARATOR) { sb.deleteCharAt(sb.length() - 1); } else if (sb.charAt(sb.length() - 1) == HighlightUtils.NULL_SEPARATOR) { sb.deleteCharAt(sb.length() - 1); } - //and we trim the snippets too + // and we trim the snippets too snippets[j++] = new Snippet(sb.toString().trim(), passage.getScore(), passage.getNumMatches() > 0); } return snippets; @@ -190,21 +198,21 @@ public Snippet[] format(Passage[] passages, String content) { public AnnotationToken[] getIntersectingAnnotations(int start, int end) { List intersectingAnnotations = new ArrayList<>(); - int fieldValueOffset =0; + int fieldValueOffset = 0; for (AnnotatedText fieldValueAnnotations : this.annotations) { - //This is called from a highlighter where all of the field values are concatenated + // This is called from a highlighter where all of the field values are concatenated // so each annotation offset will need to be adjusted so that it takes into account // the previous values AND the MULTIVAL delimiter for (int i = 0; i < fieldValueAnnotations.numAnnotations(); i++) { AnnotationToken token = fieldValueAnnotations.getAnnotation(i); if (token.intersects(start - fieldValueOffset, end - fieldValueOffset)) { - intersectingAnnotations - .add(new AnnotationToken(token.offset + fieldValueOffset, token.endOffset + - fieldValueOffset, token.value)); + intersectingAnnotations.add( + new AnnotationToken(token.offset + fieldValueOffset, token.endOffset + fieldValueOffset, token.value) + ); } } - //add 1 for the fieldvalue separator character - fieldValueOffset +=fieldValueAnnotations.textMinusMarkup.length() +1; + // add 1 for the fieldvalue separator character + fieldValueOffset += fieldValueAnnotations.textMinusMarkup.length() + 1; } return intersectingAnnotations.toArray(new AnnotationToken[intersectingAnnotations.size()]); } diff --git a/plugins/mapper-annotated-text/src/main/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextFieldMapper.java b/plugins/mapper-annotated-text/src/main/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextFieldMapper.java index 482c28253c2b9..848240d937e44 100644 --- a/plugins/mapper-annotated-text/src/main/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextFieldMapper.java +++ b/plugins/mapper-annotated-text/src/main/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextFieldMapper.java @@ -62,12 +62,16 @@ public class AnnotatedTextFieldMapper extends FieldMapper { public static final String CONTENT_TYPE = "annotated_text"; private static Builder builder(FieldMapper in) { - return ((AnnotatedTextFieldMapper)in).builder; + return ((AnnotatedTextFieldMapper) in).builder; } private static NamedAnalyzer wrapAnalyzer(NamedAnalyzer in) { - return new NamedAnalyzer(in.name(), AnalyzerScope.INDEX, - new AnnotationAnalyzerWrapper(in.analyzer()), in.getPositionIncrementGap("")); + return new NamedAnalyzer( + in.name(), + AnalyzerScope.INDEX, + new AnnotationAnalyzerWrapper(in.analyzer()), + in.getPositionIncrementGap("") + ); } public static class Builder extends FieldMapper.Builder { @@ -75,8 +79,7 @@ public static class Builder extends FieldMapper.Builder { private final Parameter store = Parameter.storeParam(m -> builder(m).store.getValue(), false); final TextParams.Analyzers analyzers; - final Parameter similarity - = TextParams.similarity(m -> builder(m).similarity.getValue()); + final Parameter similarity = TextParams.similarity(m -> builder(m).similarity.getValue()); final Parameter indexOptions = TextParams.indexOptions(m -> builder(m).indexOptions.getValue()); final Parameter norms = TextParams.norms(true, m -> builder(m).norms.getValue()); @@ -86,17 +89,27 @@ public static class Builder extends FieldMapper.Builder { public Builder(String name, IndexAnalyzers indexAnalyzers) { super(name); - this.analyzers = new TextParams.Analyzers(indexAnalyzers, - m -> builder(m).analyzers.getIndexAnalyzer(), - m -> builder(m).analyzers.positionIncrementGap.getValue()); + this.analyzers = new TextParams.Analyzers( + indexAnalyzers, + m -> builder(m).analyzers.getIndexAnalyzer(), + m -> builder(m).analyzers.positionIncrementGap.getValue() + ); } @Override protected List> getParameters() { - return Arrays.asList(store, indexOptions, norms, termVectors, similarity, - analyzers.indexAnalyzer, analyzers.searchAnalyzer, analyzers.searchQuoteAnalyzer, + return Arrays.asList( + store, + indexOptions, + norms, + termVectors, + similarity, + analyzers.indexAnalyzer, + analyzers.searchAnalyzer, + analyzers.searchQuoteAnalyzer, analyzers.positionIncrementGap, - meta); + meta + ); } private AnnotatedTextFieldType buildFieldType(FieldType fieldType, MapperBuilderContext context) { @@ -104,29 +117,32 @@ private AnnotatedTextFieldType buildFieldType(FieldType fieldType, MapperBuilder fieldType, similarity.get(), wrapAnalyzer(analyzers.getSearchAnalyzer()), - wrapAnalyzer(analyzers.getSearchQuoteAnalyzer())); - return new AnnotatedTextFieldType( - context.buildFullName(name), - store.getValue(), - tsi, - meta.getValue()); + wrapAnalyzer(analyzers.getSearchQuoteAnalyzer()) + ); + return new AnnotatedTextFieldType(context.buildFullName(name), store.getValue(), tsi, meta.getValue()); } @Override public AnnotatedTextFieldMapper build(MapperBuilderContext context) { FieldType fieldType = TextParams.buildFieldType(() -> true, store, indexOptions, norms, termVectors); - if (fieldType.indexOptions() == IndexOptions.NONE ) { + if (fieldType.indexOptions() == IndexOptions.NONE) { throw new IllegalArgumentException("[" + CONTENT_TYPE + "] fields must be indexed"); } if (analyzers.positionIncrementGap.isConfigured()) { if (fieldType.indexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) < 0) { - throw new IllegalArgumentException("Cannot set position_increment_gap on field [" - + name + "] without positions enabled"); + throw new IllegalArgumentException( + "Cannot set position_increment_gap on field [" + name + "] without positions enabled" + ); } } return new AnnotatedTextFieldMapper( - name, fieldType, buildFieldType(fieldType, context), - multiFieldsBuilder.build(this, context), copyTo.build(), this); + name, + fieldType, + buildFieldType(fieldType, context), + multiFieldsBuilder.build(this, context), + copyTo.build(), + this + ); } } @@ -142,16 +158,16 @@ public static final class AnnotatedText { List annotations; // Format is markdown-like syntax for URLs eg: - // "New mayor is [John Smith](type=person&value=John%20Smith) " + // "New mayor is [John Smith](type=person&value=John%20Smith) " static Pattern markdownPattern = Pattern.compile("\\[([^]\\[]*)]\\(([^)(]*)\\)"); - public static AnnotatedText parse (String textPlusMarkup) { - List annotations =new ArrayList<>(); + public static AnnotatedText parse(String textPlusMarkup) { + List annotations = new ArrayList<>(); Matcher m = markdownPattern.matcher(textPlusMarkup); int lastPos = 0; StringBuilder sb = new StringBuilder(); - while(m.find()){ - if(m.start() > lastPos){ + while (m.find()) { + if (m.start() > lastPos) { sb.append(textPlusMarkup, lastPos, m.start()); } @@ -164,22 +180,22 @@ public static AnnotatedText parse (String textPlusMarkup) { String value = null; for (String pair : pairs) { String[] kv = pair.split("="); - if(kv.length == 2){ + if (kv.length == 2) { throw new ElasticsearchParseException("key=value pairs are not supported in annotations"); } - if(kv.length == 1) { - //Check "=" sign wasn't in the pair string - if(kv[0].length() == pair.length()) { - //untyped value + if (kv.length == 1) { + // Check "=" sign wasn't in the pair string + if (kv[0].length() == pair.length()) { + // untyped value value = URLDecoder.decode(kv[0], StandardCharsets.UTF_8); } } - if (value!=null && value.length() > 0) { + if (value != null && value.length() > 0) { annotations.add(new AnnotationToken(startOffset, endOffset, value)); } } } - if(lastPos < textPlusMarkup.length()){ + if (lastPos < textPlusMarkup.length()) { sb.append(textPlusMarkup.substring(lastPos)); } return new AnnotatedText(sb.toString(), textPlusMarkup, annotations); @@ -196,19 +212,22 @@ public static final class AnnotationToken { public final int endOffset; public final String value; + public AnnotationToken(int offset, int endOffset, String value) { this.offset = offset; this.endOffset = endOffset; this.value = value; } + @Override public String toString() { - return value +" ("+offset+" - "+endOffset+")"; + return value + " (" + offset + " - " + endOffset + ")"; } public boolean intersects(int start, int end) { - return (start <= offset && end >= offset) || (start <= endOffset && end >= endOffset) - || (start >= offset && end <= endOffset); + return (start <= offset && end >= offset) + || (start <= endOffset && end >= endOffset) + || (start >= offset && end <= endOffset); } @Override @@ -223,29 +242,27 @@ public int hashCode() { @Override public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; + if (this == obj) return true; + if (obj == null) return false; + if (getClass() != obj.getClass()) return false; AnnotationToken other = (AnnotationToken) obj; - return Objects.equals(endOffset, other.endOffset) && Objects.equals(offset, other.offset) - && Objects.equals(value, other.value); + return Objects.equals(endOffset, other.endOffset) + && Objects.equals(offset, other.offset) + && Objects.equals(value, other.value); } } @Override public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append(textMinusMarkup); - sb.append("\n"); - annotations.forEach(a -> { - sb.append(a); - sb.append("\n"); - }); - return sb.toString(); + StringBuilder sb = new StringBuilder(); + sb.append(textMinusMarkup); + sb.append("\n"); + annotations.forEach(a -> { + sb.append(a); + sb.append("\n"); + }); + return sb.toString(); } public int numAnnotations() { @@ -276,14 +293,14 @@ public static final class AnnotatedHighlighterAnalyzer extends AnalyzerWrapper { // which array element is currently being highlighted. int readerNum; - public AnnotatedHighlighterAnalyzer(Analyzer delegate){ + public AnnotatedHighlighterAnalyzer(Analyzer delegate) { super(delegate.getReuseStrategy()); this.delegate = delegate; } @Override public Analyzer getWrappedAnalyzer(String fieldName) { - return delegate; + return delegate; } // Called with each new doc being highlighted @@ -310,13 +327,13 @@ public static final class AnnotationAnalyzerWrapper extends AnalyzerWrapper { private final Analyzer delegate; public AnnotationAnalyzerWrapper(Analyzer delegate) { - super(delegate.getReuseStrategy()); - this.delegate = delegate; + super(delegate.getReuseStrategy()); + this.delegate = delegate; } @Override public Analyzer getWrappedAnalyzer(String fieldName) { - return delegate; + return delegate; } @Override @@ -349,7 +366,6 @@ static String readToString(Reader reader) { } } - public static final class AnnotationsInjector extends TokenFilter { private AnnotatedText annotatedText; @@ -366,17 +382,17 @@ public static final class AnnotationsInjector extends TokenFilter { private final TypeAttribute typeAtt = addAttribute(TypeAttribute.class); public AnnotationsInjector(TokenStream in) { - super(in); + super(in); } public void setAnnotations(AnnotatedText annotatedText) { - this.annotatedText = annotatedText; - currentAnnotationIndex = 0; - if(annotatedText!=null && annotatedText.numAnnotations()>0){ - nextAnnotationForInjection = annotatedText.getAnnotation(0); - } else { - nextAnnotationForInjection = null; - } + this.annotatedText = annotatedText; + currentAnnotationIndex = 0; + if (annotatedText != null && annotatedText.numAnnotations() > 0) { + nextAnnotationForInjection = annotatedText.getAnnotation(0); + } else { + nextAnnotationForInjection = null; + } } @Override @@ -389,17 +405,17 @@ public void reset() throws IOException { // Abstracts if we are pulling from some pre-cached buffer of // text tokens or directly from the wrapped TokenStream - private boolean internalNextToken() throws IOException{ - if (pendingStatePos < pendingStates.size()){ + private boolean internalNextToken() throws IOException { + if (pendingStatePos < pendingStates.size()) { restoreState(pendingStates.get(pendingStatePos)); - pendingStatePos ++; - if(pendingStatePos >=pendingStates.size()){ - pendingStatePos =0; + pendingStatePos++; + if (pendingStatePos >= pendingStates.size()) { + pendingStatePos = 0; pendingStates.clear(); } return true; } - if(inputExhausted) { + if (inputExhausted) { return false; } return input.incrementToken(); @@ -424,7 +440,7 @@ public boolean incrementToken() throws IOException { // Buffer up all the other tokens spanned by this annotation to determine length. if (input.incrementToken()) { if (textOffsetAtt.endOffset() <= nextAnnotationForInjection.endOffset - && textOffsetAtt.startOffset() < nextAnnotationForInjection.endOffset) { + && textOffsetAtt.startOffset() < nextAnnotationForInjection.endOffset) { annotationPosLen += posAtt.getPositionIncrement(); } pendingStates.add(captureState()); @@ -445,7 +461,7 @@ public boolean incrementToken() throws IOException { } private void setType() { - //Default annotation type - in future AnnotationTokens may contain custom type info + // Default annotation type - in future AnnotationTokens may contain custom type info typeAtt.setType("annotation"); } @@ -460,22 +476,20 @@ private void emitAnnotation(int firstSpannedTextPosInc, int annotationPosLen) th final AnnotatedText.AnnotationToken firstAnnotationAtThisPos = nextAnnotationForInjection; while (nextAnnotationForInjection != null && nextAnnotationForInjection.offset == annotationOffset) { - setType(); termAtt.resizeBuffer(nextAnnotationForInjection.value.length()); termAtt.copyBuffer(nextAnnotationForInjection.value.toCharArray(), 0, nextAnnotationForInjection.value.length()); if (nextAnnotationForInjection == firstAnnotationAtThisPos) { posAtt.setPositionIncrement(firstSpannedTextPosInc); - //Put at the head of the queue of tokens to be emitted + // Put at the head of the queue of tokens to be emitted pendingStates.add(0, captureState()); } else { posAtt.setPositionIncrement(0); - //Put after the head of the queue of tokens to be emitted + // Put after the head of the queue of tokens to be emitted pendingStates.add(1, captureState()); } - // Flag the inject annotation as null to prevent re-injection. currentAnnotationIndex++; if (currentAnnotationIndex < annotatedText.numAnnotations()) { @@ -488,7 +502,7 @@ private void emitAnnotation(int firstSpannedTextPosInc, int annotationPosLen) th internalNextToken(); } - } + } public static final class AnnotatedTextFieldType extends TextFieldMapper.TextFieldType { @@ -509,8 +523,14 @@ public String typeName() { private final FieldType fieldType; private final Builder builder; - protected AnnotatedTextFieldMapper(String simpleName, FieldType fieldType, AnnotatedTextFieldType mappedFieldType, - MultiFields multiFields, CopyTo copyTo, Builder builder) { + protected AnnotatedTextFieldMapper( + String simpleName, + FieldType fieldType, + AnnotatedTextFieldType mappedFieldType, + MultiFields multiFields, + CopyTo copyTo, + Builder builder + ) { super(simpleName, mappedFieldType, wrapAnalyzer(builder.analyzers.getIndexAnalyzer()), multiFields, copyTo); assert fieldType.tokenized(); this.fieldType = fieldType; diff --git a/plugins/mapper-annotated-text/src/main/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextHighlighter.java b/plugins/mapper-annotated-text/src/main/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextHighlighter.java index 5ab82a1310280..f0f3042f1e12f 100644 --- a/plugins/mapper-annotated-text/src/main/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextHighlighter.java +++ b/plugins/mapper-annotated-text/src/main/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextHighlighter.java @@ -10,12 +10,12 @@ import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.search.highlight.Encoder; -import org.elasticsearch.lucene.search.uhighlight.CustomUnifiedHighlighter; import org.apache.lucene.search.uhighlight.PassageFormatter; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.annotatedtext.AnnotatedTextFieldMapper.AnnotatedHighlighterAnalyzer; import org.elasticsearch.index.mapper.annotatedtext.AnnotatedTextFieldMapper.AnnotatedText; import org.elasticsearch.index.query.SearchExecutionContext; +import org.elasticsearch.lucene.search.uhighlight.CustomUnifiedHighlighter; import org.elasticsearch.search.fetch.FetchSubPhase.HitContext; import org.elasticsearch.search.fetch.subphase.highlight.SearchHighlightContext; import org.elasticsearch.search.fetch.subphase.highlight.UnifiedHighlighter; diff --git a/plugins/mapper-annotated-text/src/test/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextFieldTypeTests.java b/plugins/mapper-annotated-text/src/test/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextFieldTypeTests.java index af58f5d05a831..d9d28d34f88d5 100644 --- a/plugins/mapper-annotated-text/src/test/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextFieldTypeTests.java +++ b/plugins/mapper-annotated-text/src/test/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextFieldTypeTests.java @@ -28,9 +28,9 @@ public void testIntervals() throws IOException { } public void testFetchSourceValue() throws IOException { - MappedFieldType fieldType = new AnnotatedTextFieldMapper.Builder("field", createDefaultIndexAnalyzers()) - .build(MapperBuilderContext.ROOT) - .fieldType(); + MappedFieldType fieldType = new AnnotatedTextFieldMapper.Builder("field", createDefaultIndexAnalyzers()).build( + MapperBuilderContext.ROOT + ).fieldType(); assertEquals(List.of("value"), fetchSourceValue(fieldType, "value")); assertEquals(List.of("42"), fetchSourceValue(fieldType, 42L)); diff --git a/plugins/mapper-annotated-text/src/test/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextHighlighterTests.java b/plugins/mapper-annotated-text/src/test/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextHighlighterTests.java index 1185b53c2e9b8..478ae9251b505 100644 --- a/plugins/mapper-annotated-text/src/test/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextHighlighterTests.java +++ b/plugins/mapper-annotated-text/src/test/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextHighlighterTests.java @@ -8,14 +8,6 @@ package org.elasticsearch.index.mapper.annotatedtext; -import static org.elasticsearch.lucene.search.uhighlight.CustomUnifiedHighlighter.MULTIVAL_SEP_CHAR; -import static org.hamcrest.CoreMatchers.equalTo; - -import java.net.URLEncoder; -import java.text.BreakIterator; -import java.util.ArrayList; -import java.util.Locale; - import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; @@ -36,33 +28,62 @@ import org.apache.lucene.search.TopDocs; import org.apache.lucene.search.highlight.DefaultEncoder; import org.apache.lucene.search.uhighlight.CustomSeparatorBreakIterator; -import org.elasticsearch.lucene.search.uhighlight.CustomUnifiedHighlighter; -import org.elasticsearch.lucene.search.uhighlight.Snippet; import org.apache.lucene.search.uhighlight.SplittingBreakIterator; import org.apache.lucene.search.uhighlight.UnifiedHighlighter; import org.apache.lucene.store.Directory; import org.elasticsearch.common.Strings; -import org.elasticsearch.index.mapper.annotatedtext.AnnotatedPassageFormatter; import org.elasticsearch.index.mapper.annotatedtext.AnnotatedTextFieldMapper.AnnotatedHighlighterAnalyzer; import org.elasticsearch.index.mapper.annotatedtext.AnnotatedTextFieldMapper.AnnotatedText; import org.elasticsearch.index.mapper.annotatedtext.AnnotatedTextFieldMapper.AnnotationAnalyzerWrapper; +import org.elasticsearch.lucene.search.uhighlight.CustomUnifiedHighlighter; +import org.elasticsearch.lucene.search.uhighlight.Snippet; import org.elasticsearch.search.fetch.subphase.highlight.LimitTokenOffsetAnalyzer; import org.elasticsearch.test.ESTestCase; +import java.net.URLEncoder; +import java.text.BreakIterator; +import java.util.ArrayList; +import java.util.Locale; + +import static org.elasticsearch.lucene.search.uhighlight.CustomUnifiedHighlighter.MULTIVAL_SEP_CHAR; +import static org.hamcrest.CoreMatchers.equalTo; + public class AnnotatedTextHighlighterTests extends ESTestCase { - private void assertHighlightOneDoc(String fieldName, String[] markedUpInputs, - Query query, Locale locale, BreakIterator breakIterator, - int noMatchSize, String[] expectedPassages) throws Exception { + private void assertHighlightOneDoc( + String fieldName, + String[] markedUpInputs, + Query query, + Locale locale, + BreakIterator breakIterator, + int noMatchSize, + String[] expectedPassages + ) throws Exception { - assertHighlightOneDoc(fieldName, markedUpInputs, query, locale, breakIterator, noMatchSize, expectedPassages, - Integer.MAX_VALUE, null); + assertHighlightOneDoc( + fieldName, + markedUpInputs, + query, + locale, + breakIterator, + noMatchSize, + expectedPassages, + Integer.MAX_VALUE, + null + ); } - private void assertHighlightOneDoc(String fieldName, String []markedUpInputs, - Query query, Locale locale, BreakIterator breakIterator, - int noMatchSize, String[] expectedPassages, - int maxAnalyzedOffset, Integer queryMaxAnalyzedOffset) throws Exception { + private void assertHighlightOneDoc( + String fieldName, + String[] markedUpInputs, + Query query, + Locale locale, + BreakIterator breakIterator, + int noMatchSize, + String[] expectedPassages, + int maxAnalyzedOffset, + Integer queryMaxAnalyzedOffset + ) throws Exception { try (Directory dir = newDirectory()) { // Annotated fields wrap the usual analyzer with one that injects extra tokens @@ -109,20 +130,20 @@ private void assertHighlightOneDoc(String fieldName, String []markedUpInputs, assertThat(topDocs.totalHits.value, equalTo(1L)); String rawValue = Strings.collectionToDelimitedString(plainTextForHighlighter, String.valueOf(MULTIVAL_SEP_CHAR)); CustomUnifiedHighlighter highlighter = new CustomUnifiedHighlighter( - searcher, - hiliteAnalyzer, - UnifiedHighlighter.OffsetSource.ANALYSIS, - passageFormatter, - locale, - breakIterator, - "index", - "text", - query, - noMatchSize, - expectedPassages.length, - name -> "text".equals(name), - maxAnalyzedOffset, - queryMaxAnalyzedOffset + searcher, + hiliteAnalyzer, + UnifiedHighlighter.OffsetSource.ANALYSIS, + passageFormatter, + locale, + breakIterator, + "index", + "text", + query, + noMatchSize, + expectedPassages.length, + name -> "text".equals(name), + maxAnalyzedOffset, + queryMaxAnalyzedOffset ); highlighter.setFieldMatcher((name) -> "text".equals(name)); final Snippet[] snippets = highlighter.highlightField(getOnlyLeafReader(reader), topDocs.scoreDocs[0].doc, () -> rawValue); @@ -141,37 +162,49 @@ public void testAnnotatedTextStructuredMatch() throws Exception { String url = "https://en.wikipedia.org/wiki/Key_Word_in_Context"; String encodedUrl = URLEncoder.encode(url, "UTF-8"); String annotatedWord = "[highlighting](" + encodedUrl + ")"; - String highlightedAnnotatedWord = "[highlighting](" + AnnotatedPassageFormatter.SEARCH_HIT_TYPE + "=" + encodedUrl + "&" - + encodedUrl + ")"; - final String[] markedUpInputs = { "This is a test. Just a test1 " + annotatedWord + " from [annotated](bar) highlighter.", - "This is the second " + annotatedWord + " value to perform highlighting on a longer text that gets scored lower." }; + String highlightedAnnotatedWord = "[highlighting](" + + AnnotatedPassageFormatter.SEARCH_HIT_TYPE + + "=" + + encodedUrl + + "&" + + encodedUrl + + ")"; + final String[] markedUpInputs = { + "This is a test. Just a test1 " + annotatedWord + " from [annotated](bar) highlighter.", + "This is the second " + annotatedWord + " value to perform highlighting on a longer text that gets scored lower." }; String[] expectedPassages = { - "This is a test. Just a test1 " + highlightedAnnotatedWord + " from [annotated](bar) highlighter.", - "This is the second " + highlightedAnnotatedWord + " value to perform highlighting on a" - + " longer text that gets scored lower." }; + "This is a test. Just a test1 " + highlightedAnnotatedWord + " from [annotated](bar) highlighter.", + "This is the second " + + highlightedAnnotatedWord + + " value to perform highlighting on a" + + " longer text that gets scored lower." }; Query query = new TermQuery(new Term("text", url)); BreakIterator breakIterator = new CustomSeparatorBreakIterator(MULTIVAL_SEP_CHAR); assertHighlightOneDoc("text", markedUpInputs, query, Locale.ROOT, breakIterator, 0, expectedPassages); } public void testAnnotatedTextOverlapsWithUnstructuredSearchTerms() throws Exception { - final String[] markedUpInputs = { "[Donald Trump](Donald+Trump) visited Singapore", - "Donald duck is a [Disney](Disney+Inc) invention" }; + final String[] markedUpInputs = { + "[Donald Trump](Donald+Trump) visited Singapore", + "Donald duck is a [Disney](Disney+Inc) invention" }; - String[] expectedPassages = { "[Donald](_hit_term=donald) Trump visited Singapore", - "[Donald](_hit_term=donald) duck is a [Disney](Disney+Inc) invention" }; + String[] expectedPassages = { + "[Donald](_hit_term=donald) Trump visited Singapore", + "[Donald](_hit_term=donald) duck is a [Disney](Disney+Inc) invention" }; Query query = new TermQuery(new Term("text", "donald")); BreakIterator breakIterator = new CustomSeparatorBreakIterator(MULTIVAL_SEP_CHAR); assertHighlightOneDoc("text", markedUpInputs, query, Locale.ROOT, breakIterator, 0, expectedPassages); } public void testAnnotatedTextMultiFieldWithBreakIterator() throws Exception { - final String[] markedUpInputs = { "[Donald Trump](Donald+Trump) visited Singapore. Kim shook hands with Donald", - "Donald duck is a [Disney](Disney+Inc) invention" }; - String[] expectedPassages = { "[Donald](_hit_term=donald) Trump visited Singapore", - "Kim shook hands with [Donald](_hit_term=donald)", - "[Donald](_hit_term=donald) duck is a [Disney](Disney+Inc) invention" }; + final String[] markedUpInputs = { + "[Donald Trump](Donald+Trump) visited Singapore. Kim shook hands with Donald", + "Donald duck is a [Disney](Disney+Inc) invention" }; + String[] expectedPassages = { + "[Donald](_hit_term=donald) Trump visited Singapore", + "Kim shook hands with [Donald](_hit_term=donald)", + "[Donald](_hit_term=donald) duck is a [Disney](Disney+Inc) invention" }; Query query = new TermQuery(new Term("text", "donald")); BreakIterator breakIterator = new CustomSeparatorBreakIterator(MULTIVAL_SEP_CHAR); breakIterator = new SplittingBreakIterator(breakIterator, '.'); @@ -179,9 +212,10 @@ public void testAnnotatedTextMultiFieldWithBreakIterator() throws Exception { } public void testAnnotatedTextSingleFieldWithBreakIterator() throws Exception { - final String[] markedUpInputs = { "[Donald Trump](Donald+Trump) visited Singapore. Kim shook hands with Donald"}; - String[] expectedPassages = { "[Donald](_hit_term=donald) Trump visited Singapore", - "Kim shook hands with [Donald](_hit_term=donald)"}; + final String[] markedUpInputs = { "[Donald Trump](Donald+Trump) visited Singapore. Kim shook hands with Donald" }; + String[] expectedPassages = { + "[Donald](_hit_term=donald) Trump visited Singapore", + "Kim shook hands with [Donald](_hit_term=donald)" }; Query query = new TermQuery(new Term("text", "donald")); BreakIterator breakIterator = new CustomSeparatorBreakIterator(MULTIVAL_SEP_CHAR); breakIterator = new SplittingBreakIterator(breakIterator, '.'); @@ -189,17 +223,16 @@ public void testAnnotatedTextSingleFieldWithBreakIterator() throws Exception { } public void testAnnotatedTextSingleFieldWithPhraseQuery() throws Exception { - final String[] markedUpInputs = { "[Donald Trump](Donald+Trump) visited Singapore", - "Donald Jr was with Melania Trump"}; - String[] expectedPassages = { "[Donald](_hit_term=donald) [Trump](_hit_term=trump) visited Singapore"}; + final String[] markedUpInputs = { "[Donald Trump](Donald+Trump) visited Singapore", "Donald Jr was with Melania Trump" }; + String[] expectedPassages = { "[Donald](_hit_term=donald) [Trump](_hit_term=trump) visited Singapore" }; Query query = new PhraseQuery("text", "donald", "trump"); BreakIterator breakIterator = new CustomSeparatorBreakIterator(MULTIVAL_SEP_CHAR); assertHighlightOneDoc("text", markedUpInputs, query, Locale.ROOT, breakIterator, 0, expectedPassages); } public void testBadAnnotation() throws Exception { - final String[] markedUpInputs = { "Missing bracket for [Donald Trump](Donald+Trump visited Singapore"}; - String[] expectedPassages = { "Missing bracket for [Donald Trump](Donald+Trump visited [Singapore](_hit_term=singapore)"}; + final String[] markedUpInputs = { "Missing bracket for [Donald Trump](Donald+Trump visited Singapore" }; + String[] expectedPassages = { "Missing bracket for [Donald Trump](Donald+Trump visited [Singapore](_hit_term=singapore)" }; Query query = new TermQuery(new Term("text", "singapore")); BreakIterator breakIterator = new CustomSeparatorBreakIterator(MULTIVAL_SEP_CHAR); assertHighlightOneDoc("text", markedUpInputs, query, Locale.ROOT, breakIterator, 0, expectedPassages); @@ -208,8 +241,17 @@ public void testBadAnnotation() throws Exception { public void testExceedMaxAnalyzedOffset() throws Exception { TermQuery query = new TermQuery(new Term("text", "exceeds")); BreakIterator breakIterator = new CustomSeparatorBreakIterator(MULTIVAL_SEP_CHAR); - assertHighlightOneDoc("text", new String[] { "[Short Text](Short+Text)" }, query, Locale.ROOT, breakIterator, 0, new String[] {}, - 10, null); + assertHighlightOneDoc( + "text", + new String[] { "[Short Text](Short+Text)" }, + query, + Locale.ROOT, + breakIterator, + 0, + new String[] {}, + 10, + null + ); IllegalArgumentException e = expectThrows( IllegalArgumentException.class, @@ -234,18 +276,18 @@ public void testExceedMaxAnalyzedOffset() throws Exception { final Integer queryMaxOffset = randomIntBetween(21, 1000); e = expectThrows( - IllegalArgumentException.class, - () -> assertHighlightOneDoc( - "text", - new String[] { "[Long Text exceeds](Long+Text+exceeds) MAX analyzed offset)" }, - query, - Locale.ROOT, - breakIterator, - 0, - new String[] {}, - 20, - queryMaxOffset - ) + IllegalArgumentException.class, + () -> assertHighlightOneDoc( + "text", + new String[] { "[Long Text exceeds](Long+Text+exceeds) MAX analyzed offset)" }, + query, + Locale.ROOT, + breakIterator, + 0, + new String[] {}, + 20, + queryMaxOffset + ) ); assertEquals( "The length [38] of field [text] in doc[0]/index[index] exceeds the [index.highlight.max_analyzed_offset] limit [20]. " diff --git a/plugins/mapper-annotated-text/src/test/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextParsingTests.java b/plugins/mapper-annotated-text/src/test/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextParsingTests.java index 1ff84d2c1757f..c1a9aa9504219 100644 --- a/plugins/mapper-annotated-text/src/test/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextParsingTests.java +++ b/plugins/mapper-annotated-text/src/test/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextParsingTests.java @@ -30,33 +30,35 @@ private void checkParsing(String markup, String expectedPlainText, AnnotationTok } public void testSingleValueMarkup() { - checkParsing("foo [bar](Y)", "foo bar", new AnnotationToken(4,7,"Y")); + checkParsing("foo [bar](Y)", "foo bar", new AnnotationToken(4, 7, "Y")); } public void testMultiValueMarkup() { - checkParsing("foo [bar](Y&B)", "foo bar", new AnnotationToken(4,7,"Y"), - new AnnotationToken(4,7,"B")); + checkParsing("foo [bar](Y&B)", "foo bar", new AnnotationToken(4, 7, "Y"), new AnnotationToken(4, 7, "B")); } public void testBlankTextAnnotation() { - checkParsing("It sounded like this:[](theSoundOfOneHandClapping)", "It sounded like this:", - new AnnotationToken(21,21,"theSoundOfOneHandClapping")); + checkParsing( + "It sounded like this:[](theSoundOfOneHandClapping)", + "It sounded like this:", + new AnnotationToken(21, 21, "theSoundOfOneHandClapping") + ); } public void testMissingBracket() { - checkParsing("[foo](MissingEndBracket bar", - "[foo](MissingEndBracket bar", new AnnotationToken[0]); + checkParsing("[foo](MissingEndBracket bar", "[foo](MissingEndBracket bar", new AnnotationToken[0]); } public void testAnnotationWithType() { - Exception expectedException = expectThrows(ElasticsearchParseException.class, - () -> checkParsing("foo [bar](type=foo) baz", "foo bar baz", new AnnotationToken(4,7, "noType"))); - assertThat(expectedException.getMessage(), equalTo("key=value pairs are not supported in annotations")); + Exception expectedException = expectThrows( + ElasticsearchParseException.class, + () -> checkParsing("foo [bar](type=foo) baz", "foo bar baz", new AnnotationToken(4, 7, "noType")) + ); + assertThat(expectedException.getMessage(), equalTo("key=value pairs are not supported in annotations")); } public void testMissingValue() { checkParsing("[foo]() bar", "foo bar", new AnnotationToken[0]); } - } diff --git a/plugins/mapper-annotated-text/src/yamlRestTest/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextClientYamlTestSuiteIT.java b/plugins/mapper-annotated-text/src/yamlRestTest/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextClientYamlTestSuiteIT.java index b0bfc375c9992..b5c46f06fa54e 100644 --- a/plugins/mapper-annotated-text/src/yamlRestTest/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextClientYamlTestSuiteIT.java +++ b/plugins/mapper-annotated-text/src/yamlRestTest/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextClientYamlTestSuiteIT.java @@ -25,4 +25,3 @@ public static Iterable parameters() throws Exception { return createParameters(); } } - diff --git a/plugins/mapper-murmur3/src/main/java/org/elasticsearch/index/mapper/murmur3/Murmur3FieldMapper.java b/plugins/mapper-murmur3/src/main/java/org/elasticsearch/index/mapper/murmur3/Murmur3FieldMapper.java index 859cc1bfbe73d..59dc8d602ad02 100644 --- a/plugins/mapper-murmur3/src/main/java/org/elasticsearch/index/mapper/murmur3/Murmur3FieldMapper.java +++ b/plugins/mapper-murmur3/src/main/java/org/elasticsearch/index/mapper/murmur3/Murmur3FieldMapper.java @@ -69,7 +69,8 @@ public Murmur3FieldMapper build(MapperBuilderContext context) { name, new Murmur3FieldType(context.buildFullName(name), stored.getValue(), meta.getValue()), multiFieldsBuilder.build(this, context), - copyTo.build()); + copyTo.build() + ); } } @@ -103,10 +104,7 @@ public Query termQuery(Object value, SearchExecutionContext context) { } } - protected Murmur3FieldMapper(String simpleName, - MappedFieldType mappedFieldType, - MultiFields multiFields, - CopyTo copyTo) { + protected Murmur3FieldMapper(String simpleName, MappedFieldType mappedFieldType, MultiFields multiFields, CopyTo copyTo) { super(simpleName, mappedFieldType, multiFields, copyTo); } @@ -121,8 +119,7 @@ protected String contentType() { } @Override - protected void parseCreateField(DocumentParserContext context) - throws IOException { + protected void parseCreateField(DocumentParserContext context) throws IOException { final String value = context.parser().textOrNull(); if (value != null) { final BytesRef bytes = new BytesRef(value.toString()); diff --git a/plugins/mapper-murmur3/src/test/java/org/elasticsearch/index/mapper/murmur3/Murmur3FieldMapperTests.java b/plugins/mapper-murmur3/src/test/java/org/elasticsearch/index/mapper/murmur3/Murmur3FieldMapperTests.java index d180322060df5..b02ec454d529a 100644 --- a/plugins/mapper-murmur3/src/test/java/org/elasticsearch/index/mapper/murmur3/Murmur3FieldMapperTests.java +++ b/plugins/mapper-murmur3/src/test/java/org/elasticsearch/index/mapper/murmur3/Murmur3FieldMapperTests.java @@ -11,13 +11,13 @@ import org.apache.lucene.index.DocValuesType; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.IndexableField; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.MapperTestCase; import org.elasticsearch.index.mapper.ParsedDocument; import org.elasticsearch.plugin.mapper.MapperMurmur3Plugin; import org.elasticsearch.plugins.Plugin; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.Arrays; diff --git a/plugins/mapper-murmur3/src/yamlRestTest/java/org/elasticsearch/index/mapper/murmur3/MapperMurmur3ClientYamlTestSuiteIT.java b/plugins/mapper-murmur3/src/yamlRestTest/java/org/elasticsearch/index/mapper/murmur3/MapperMurmur3ClientYamlTestSuiteIT.java index 1e219380849b8..82da6ca3f9efa 100644 --- a/plugins/mapper-murmur3/src/yamlRestTest/java/org/elasticsearch/index/mapper/murmur3/MapperMurmur3ClientYamlTestSuiteIT.java +++ b/plugins/mapper-murmur3/src/yamlRestTest/java/org/elasticsearch/index/mapper/murmur3/MapperMurmur3ClientYamlTestSuiteIT.java @@ -25,4 +25,3 @@ public static Iterable parameters() throws Exception { return createParameters(); } } - diff --git a/plugins/mapper-size/src/internalClusterTest/java/org/elasticsearch/index/mapper/size/SizeMappingIT.java b/plugins/mapper-size/src/internalClusterTest/java/org/elasticsearch/index/mapper/size/SizeMappingIT.java index 39a0edb8119b8..8cb06e3c206c2 100644 --- a/plugins/mapper-size/src/internalClusterTest/java/org/elasticsearch/index/mapper/size/SizeMappingIT.java +++ b/plugins/mapper-size/src/internalClusterTest/java/org/elasticsearch/index/mapper/size/SizeMappingIT.java @@ -11,11 +11,11 @@ import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.support.master.AcknowledgedResponse; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.plugin.mapper.MapperSizePlugin; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.test.ESIntegTestCase; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; import java.io.IOException; import java.util.Arrays; @@ -23,8 +23,8 @@ import java.util.Locale; import java.util.Map; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.Matchers.hasKey; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; @@ -40,19 +40,21 @@ protected Collection> nodePlugins() { public void testThatUpdatingMappingShouldNotRemoveSizeMappingConfiguration() throws Exception { String index = "foo"; - XContentBuilder builder = - jsonBuilder().startObject().startObject("_size").field("enabled", true).endObject().endObject(); + XContentBuilder builder = jsonBuilder().startObject().startObject("_size").field("enabled", true).endObject().endObject(); assertAcked(client().admin().indices().prepareCreate(index).setMapping(builder)); // check mapping again assertSizeMappingEnabled(index, true); // update some field in the mapping - XContentBuilder updateMappingBuilder = - jsonBuilder().startObject().startObject("properties").startObject("otherField").field("type", "text") - .endObject().endObject().endObject(); - AcknowledgedResponse putMappingResponse = - client().admin().indices().preparePutMapping(index).setSource(updateMappingBuilder).get(); + XContentBuilder updateMappingBuilder = jsonBuilder().startObject() + .startObject("properties") + .startObject("otherField") + .field("type", "text") + .endObject() + .endObject() + .endObject(); + AcknowledgedResponse putMappingResponse = client().admin().indices().preparePutMapping(index).setSource(updateMappingBuilder).get(); assertAcked(putMappingResponse); // make sure size field is still in mapping @@ -62,18 +64,19 @@ public void testThatUpdatingMappingShouldNotRemoveSizeMappingConfiguration() thr public void testThatSizeCanBeSwitchedOnAndOff() throws Exception { String index = "foo"; - XContentBuilder builder = - jsonBuilder().startObject().startObject("_size").field("enabled", true).endObject().endObject(); + XContentBuilder builder = jsonBuilder().startObject().startObject("_size").field("enabled", true).endObject().endObject(); assertAcked(client().admin().indices().prepareCreate(index).setMapping(builder)); // check mapping again assertSizeMappingEnabled(index, true); // update some field in the mapping - XContentBuilder updateMappingBuilder = - jsonBuilder().startObject().startObject("_size").field("enabled", false).endObject().endObject(); - AcknowledgedResponse putMappingResponse = - client().admin().indices().preparePutMapping(index).setSource(updateMappingBuilder).get(); + XContentBuilder updateMappingBuilder = jsonBuilder().startObject() + .startObject("_size") + .field("enabled", false) + .endObject() + .endObject(); + AcknowledgedResponse putMappingResponse = client().admin().indices().preparePutMapping(index).setSource(updateMappingBuilder).get(); assertAcked(putMappingResponse); // make sure size field is still in mapping @@ -81,10 +84,12 @@ public void testThatSizeCanBeSwitchedOnAndOff() throws Exception { } private void assertSizeMappingEnabled(String index, boolean enabled) throws IOException { - String errMsg = String.format(Locale.ROOT, - "Expected size field mapping to be " + (enabled ? "enabled" : "disabled") + " for %s", index); - GetMappingsResponse getMappingsResponse = - client().admin().indices().prepareGetMappings(index).get(); + String errMsg = String.format( + Locale.ROOT, + "Expected size field mapping to be " + (enabled ? "enabled" : "disabled") + " for %s", + index + ); + GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings(index).get(); Map mappingSource = getMappingsResponse.getMappings().get(index).getSourceAsMap(); assertThat(errMsg, mappingSource, hasKey("_size")); String sizeAsString = mappingSource.get("_size").toString(); @@ -94,9 +99,8 @@ private void assertSizeMappingEnabled(String index, boolean enabled) throws IOEx public void testBasic() throws Exception { assertAcked(prepareCreate("test").setMapping("_size", "enabled=true")); - final String source = "{\"f\":\"" + randomAlphaOfLengthBetween(1, 100)+ "\"}"; - indexRandom(true, - client().prepareIndex("test").setId("1").setSource(source, XContentType.JSON)); + final String source = "{\"f\":\"" + randomAlphaOfLengthBetween(1, 100) + "\"}"; + indexRandom(true, client().prepareIndex("test").setId("1").setSource(source, XContentType.JSON)); GetResponse getResponse = client().prepareGet("test", "1").setStoredFields("_size").get(); assertNotNull(getResponse.getField("_size")); assertEquals(source.length(), (int) getResponse.getField("_size").getValue()); @@ -104,9 +108,8 @@ public void testBasic() throws Exception { public void testGetWithFields() throws Exception { assertAcked(prepareCreate("test").setMapping("_size", "enabled=true")); - final String source = "{\"f\":\"" + randomAlphaOfLengthBetween(1, 100)+ "\"}"; - indexRandom(true, - client().prepareIndex("test").setId("1").setSource(source, XContentType.JSON)); + final String source = "{\"f\":\"" + randomAlphaOfLengthBetween(1, 100) + "\"}"; + indexRandom(true, client().prepareIndex("test").setId("1").setSource(source, XContentType.JSON)); SearchResponse searchResponse = client().prepareSearch("test").addFetchField("_size").get(); assertEquals(source.length(), ((Long) searchResponse.getHits().getHits()[0].getFields().get("_size").getValue()).intValue()); diff --git a/plugins/mapper-size/src/main/java/org/elasticsearch/index/mapper/size/SizeFieldMapper.java b/plugins/mapper-size/src/main/java/org/elasticsearch/index/mapper/size/SizeFieldMapper.java index d2e3c6e8b0e0d..768178a3c0880 100644 --- a/plugins/mapper-size/src/main/java/org/elasticsearch/index/mapper/size/SizeFieldMapper.java +++ b/plugins/mapper-size/src/main/java/org/elasticsearch/index/mapper/size/SizeFieldMapper.java @@ -31,8 +31,7 @@ private static SizeFieldMapper toType(FieldMapper in) { public static class Builder extends MetadataFieldMapper.Builder { - private final Parameter> enabled - = updateableBoolParam("enabled", m -> toType(m).enabled, false); + private final Parameter> enabled = updateableBoolParam("enabled", m -> toType(m).enabled, false); private Builder() { super(NAME); diff --git a/plugins/mapper-size/src/test/java/org/elasticsearch/index/mapper/size/SizeMappingTests.java b/plugins/mapper-size/src/test/java/org/elasticsearch/index/mapper/size/SizeMappingTests.java index 98187dbdbf14b..4599b06115f98 100644 --- a/plugins/mapper-size/src/test/java/org/elasticsearch/index/mapper/size/SizeMappingTests.java +++ b/plugins/mapper-size/src/test/java/org/elasticsearch/index/mapper/size/SizeMappingTests.java @@ -9,13 +9,13 @@ package org.elasticsearch.index.mapper.size; import org.apache.lucene.index.IndexableField; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.mapper.MapperServiceTestCase; import org.elasticsearch.index.mapper.ParsedDocument; import org.elasticsearch.plugin.mapper.MapperSizePlugin; import org.elasticsearch.plugins.Plugin; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.Collection; diff --git a/plugins/mapper-size/src/yamlRestTest/java/org/elasticsearch/index/mapper/size/MapperSizeClientYamlTestSuiteIT.java b/plugins/mapper-size/src/yamlRestTest/java/org/elasticsearch/index/mapper/size/MapperSizeClientYamlTestSuiteIT.java index a27ce65bef980..a1bef57849f2c 100644 --- a/plugins/mapper-size/src/yamlRestTest/java/org/elasticsearch/index/mapper/size/MapperSizeClientYamlTestSuiteIT.java +++ b/plugins/mapper-size/src/yamlRestTest/java/org/elasticsearch/index/mapper/size/MapperSizeClientYamlTestSuiteIT.java @@ -25,4 +25,3 @@ public static Iterable parameters() throws Exception { return createParameters(); } } - diff --git a/plugins/repository-azure/src/internalClusterTest/java/org/elasticsearch/repositories/azure/AzureBlobStoreRepositoryTests.java b/plugins/repository-azure/src/internalClusterTest/java/org/elasticsearch/repositories/azure/AzureBlobStoreRepositoryTests.java index d293ad7c6c2d9..4ded5040eab48 100644 --- a/plugins/repository-azure/src/internalClusterTest/java/org/elasticsearch/repositories/azure/AzureBlobStoreRepositoryTests.java +++ b/plugins/repository-azure/src/internalClusterTest/java/org/elasticsearch/repositories/azure/AzureBlobStoreRepositoryTests.java @@ -59,10 +59,10 @@ protected String repositoryType() { @Override protected Settings repositorySettings(String repoName) { Settings.Builder settingsBuilder = Settings.builder() - .put(super.repositorySettings(repoName)) - .put(AzureRepository.Repository.MAX_SINGLE_PART_UPLOAD_SIZE_SETTING.getKey(), new ByteSizeValue(1, ByteSizeUnit.MB)) - .put(AzureRepository.Repository.CONTAINER_SETTING.getKey(), "container") - .put(AzureStorageSettings.ACCOUNT_SETTING.getKey(), "test"); + .put(super.repositorySettings(repoName)) + .put(AzureRepository.Repository.MAX_SINGLE_PART_UPLOAD_SIZE_SETTING.getKey(), new ByteSizeValue(1, ByteSizeUnit.MB)) + .put(AzureRepository.Repository.CONTAINER_SETTING.getKey(), "container") + .put(AzureStorageSettings.ACCOUNT_SETTING.getKey(), "test"); if (randomBoolean()) { settingsBuilder.put(AzureRepository.Repository.BASE_PATH_SETTING.getKey(), randomFrom("test", "test/1")); } @@ -76,8 +76,10 @@ protected Collection> nodePlugins() { @Override protected Map createHttpHandlers() { - return Collections.singletonMap("/" + DEFAULT_ACCOUNT_NAME, - new AzureHTTPStatsCollectorHandler(new AzureBlobStoreHttpHandler(DEFAULT_ACCOUNT_NAME, "container"))); + return Collections.singletonMap( + "/" + DEFAULT_ACCOUNT_NAME, + new AzureHTTPStatsCollectorHandler(new AzureBlobStoreHttpHandler(DEFAULT_ACCOUNT_NAME, "container")) + ); } @Override @@ -117,9 +119,14 @@ AzureStorageService createAzureStorageService(Settings settings, AzureClientProv return new AzureStorageService(settings, azureClientProvider) { @Override RequestRetryOptions getRetryOptions(LocationMode locationMode, AzureStorageSettings azureStorageSettings) { - return new RequestRetryOptions(RetryPolicyType.EXPONENTIAL, - azureStorageSettings.getMaxRetries() + 1, 60, - 50L, 100L, null); + return new RequestRetryOptions( + RetryPolicyType.EXPONENTIAL, + azureStorageSettings.getMaxRetries() + 1, + 60, + 50L, + 100L, + null + ); } @Override @@ -165,9 +172,7 @@ protected void handleAsError(final HttpExchange exchange) throws IOException { protected String requestUniqueId(final HttpExchange exchange) { final String requestId = exchange.getRequestHeaders().getFirst("X-ms-client-request-id"); final String range = exchange.getRequestHeaders().getFirst("Content-Range"); - return exchange.getRequestMethod() - + " " + requestId - + (range != null ? " " + range : ""); + return exchange.getRequestMethod() + " " + requestId + (range != null ? " " + range : ""); } } diff --git a/plugins/repository-azure/src/internalClusterTest/java/org/elasticsearch/repositories/azure/AzureStorageCleanupThirdPartyTests.java b/plugins/repository-azure/src/internalClusterTest/java/org/elasticsearch/repositories/azure/AzureStorageCleanupThirdPartyTests.java index 8b14e6b34905a..da1ff13f8d6a7 100644 --- a/plugins/repository-azure/src/internalClusterTest/java/org/elasticsearch/repositories/azure/AzureStorageCleanupThirdPartyTests.java +++ b/plugins/repository-azure/src/internalClusterTest/java/org/elasticsearch/repositories/azure/AzureStorageCleanupThirdPartyTests.java @@ -11,6 +11,7 @@ import com.azure.storage.blob.BlobContainerClient; import com.azure.storage.blob.BlobServiceClient; import com.azure.storage.blob.models.BlobStorageException; + import org.elasticsearch.action.ActionRunnable; import org.elasticsearch.action.support.PlainActionFuture; import org.elasticsearch.action.support.master.AcknowledgedResponse; @@ -45,10 +46,7 @@ protected Collection> getPlugins() { protected Settings nodeSettings() { final String endpoint = System.getProperty("test.azure.endpoint_suffix"); if (Strings.hasText(endpoint)) { - return Settings.builder() - .put(super.nodeSettings()) - .put("azure.client.default.endpoint_suffix", endpoint) - .build(); + return Settings.builder().put(super.nodeSettings()).put("azure.client.default.endpoint_suffix", endpoint).build(); } return super.nodeSettings(); } @@ -77,13 +75,17 @@ protected SecureSettings credentials() { @Override protected void createRepository(String repoName) { - AcknowledgedResponse putRepositoryResponse = client().admin().cluster().preparePutRepository(repoName) + AcknowledgedResponse putRepositoryResponse = client().admin() + .cluster() + .preparePutRepository(repoName) .setType("azure") - .setSettings(Settings.builder() - .put("container", System.getProperty("test.azure.container")) - .put("base_path", System.getProperty("test.azure.base")) - .put("max_single_part_upload_size", new ByteSizeValue(1, ByteSizeUnit.MB)) - ).get(); + .setSettings( + Settings.builder() + .put("container", System.getProperty("test.azure.container")) + .put("base_path", System.getProperty("test.azure.base")) + .put("max_single_part_upload_size", new ByteSizeValue(1, ByteSizeUnit.MB)) + ) + .get(); assertThat(putRepositoryResponse.isAcknowledged(), equalTo(true)); if (Strings.hasText(System.getProperty("test.azure.sas_token"))) { ensureSasTokenPermissions(); @@ -95,17 +97,19 @@ private void ensureSasTokenPermissions() { final PlainActionFuture future = PlainActionFuture.newFuture(); repository.threadPool().generic().execute(ActionRunnable.wrap(future, l -> { final AzureBlobStore blobStore = (AzureBlobStore) repository.blobStore(); - final AzureBlobServiceClient azureBlobServiceClient = - blobStore.getService().client("default", LocationMode.PRIMARY_ONLY); + final AzureBlobServiceClient azureBlobServiceClient = blobStore.getService().client("default", LocationMode.PRIMARY_ONLY); final BlobServiceClient client = azureBlobServiceClient.getSyncClient(); try { SocketAccess.doPrivilegedException(() -> { final BlobContainerClient blobContainer = client.getBlobContainerClient(blobStore.toString()); return blobContainer.exists(); }); - future.onFailure(new RuntimeException( - "The SAS token used in this test allowed for checking container existence. This test only supports tokens " + - "that grant only the documented permission requirements for the Azure repository plugin.")); + future.onFailure( + new RuntimeException( + "The SAS token used in this test allowed for checking container existence. This test only supports tokens " + + "that grant only the documented permission requirements for the Azure repository plugin." + ) + ); } catch (BlobStorageException e) { if (e.getStatusCode() == HttpURLConnection.HTTP_FORBIDDEN) { future.onResponse(null); @@ -124,8 +128,7 @@ public void testMultiBlockUpload() throws Exception { PlainActionFuture future = PlainActionFuture.newFuture(); repo.threadPool().generic().execute(ActionRunnable.run(future, () -> { final BlobContainer blobContainer = repo.blobStore().blobContainer(repo.basePath().add("large_write")); - blobContainer.writeBlob(UUIDs.base64UUID(), - new ByteArrayInputStream(randomByteArrayOfLength(blobSize)), blobSize, false); + blobContainer.writeBlob(UUIDs.base64UUID(), new ByteArrayInputStream(randomByteArrayOfLength(blobSize)), blobSize, false); blobContainer.delete(); })); future.get(); diff --git a/plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureBlobContainer.java b/plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureBlobContainer.java index 7bd91c2daef84..6154694b1d55d 100644 --- a/plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureBlobContainer.java +++ b/plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureBlobContainer.java @@ -9,17 +9,18 @@ package org.elasticsearch.repositories.azure; import com.azure.storage.blob.models.BlobStorageException; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.core.util.Throwables; -import org.elasticsearch.core.CheckedConsumer; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.blobstore.BlobContainer; import org.elasticsearch.common.blobstore.BlobMetadata; import org.elasticsearch.common.blobstore.BlobPath; import org.elasticsearch.common.blobstore.DeleteResult; import org.elasticsearch.common.blobstore.support.AbstractBlobContainer; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.core.CheckedConsumer; +import org.elasticsearch.core.Nullable; import java.io.IOException; import java.io.InputStream; @@ -53,7 +54,7 @@ private InputStream openInputStream(String blobName, long position, @Nullable Lo // On Azure, if the location path is a secondary location, and the blob does not // exist, instead of returning immediately from the getInputStream call below // with a 404 StorageException, Azure keeps trying and trying for a long timeout - // before throwing a storage exception. This can cause long delays in retrieving + // before throwing a storage exception. This can cause long delays in retrieving // snapshots, so we first check if the blob exists before trying to open an input // stream to it. throw new NoSuchFileException("Blob [" + blobKey + "] not found"); @@ -103,10 +104,8 @@ public void writeBlob(String blobName, BytesReference bytes, boolean failIfAlrea } @Override - public void writeBlob(String blobName, - boolean failIfAlreadyExists, - boolean atomic, - CheckedConsumer writer) throws IOException { + public void writeBlob(String blobName, boolean failIfAlreadyExists, boolean atomic, CheckedConsumer writer) + throws IOException { blobStore.writeBlob(buildKey(blobName), failIfAlreadyExists, writer); } diff --git a/plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureBlobServiceClient.java b/plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureBlobServiceClient.java index a71b82e225a49..7e5c230453e18 100644 --- a/plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureBlobServiceClient.java +++ b/plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureBlobServiceClient.java @@ -8,9 +8,10 @@ package org.elasticsearch.repositories.azure; +import io.netty.buffer.ByteBufAllocator; + import com.azure.storage.blob.BlobServiceAsyncClient; import com.azure.storage.blob.BlobServiceClient; -import io.netty.buffer.ByteBufAllocator; class AzureBlobServiceClient { private final BlobServiceClient blobServiceClient; @@ -18,10 +19,12 @@ class AzureBlobServiceClient { private final int maxRetries; private final ByteBufAllocator allocator; - AzureBlobServiceClient(BlobServiceClient blobServiceClient, - BlobServiceAsyncClient blobAsyncClient, - int maxRetries, - ByteBufAllocator allocator) { + AzureBlobServiceClient( + BlobServiceClient blobServiceClient, + BlobServiceAsyncClient blobAsyncClient, + int maxRetries, + ByteBufAllocator allocator + ) { this.blobServiceClient = blobServiceClient; this.blobAsyncClient = blobAsyncClient; this.maxRetries = maxRetries; diff --git a/plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureBlobStore.java b/plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureBlobStore.java index 6c43acccd7036..0152e7d4f2fda 100644 --- a/plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureBlobStore.java +++ b/plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureBlobStore.java @@ -8,6 +8,13 @@ package org.elasticsearch.repositories.azure; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; +import io.netty.util.ReferenceCountUtil; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; +import reactor.core.scheduler.Schedulers; + import com.azure.core.http.rest.ResponseBase; import com.azure.storage.blob.BlobAsyncClient; import com.azure.storage.blob.BlobClient; @@ -26,16 +33,11 @@ import com.azure.storage.blob.models.ListBlobsOptions; import com.azure.storage.blob.options.BlockBlobSimpleUploadOptions; import com.azure.storage.blob.specialized.BlockBlobAsyncClient; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufAllocator; -import io.netty.util.ReferenceCountUtil; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.message.ParameterizedMessage; import org.elasticsearch.cluster.metadata.RepositoryMetadata; -import org.elasticsearch.common.util.BigArrays; -import org.elasticsearch.core.CheckedConsumer; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.UUIDs; import org.elasticsearch.common.blobstore.BlobContainer; import org.elasticsearch.common.blobstore.BlobMetadata; @@ -44,14 +46,14 @@ import org.elasticsearch.common.blobstore.DeleteResult; import org.elasticsearch.common.blobstore.support.PlainBlobMetadata; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.common.util.BigArrays; +import org.elasticsearch.core.CheckedConsumer; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.core.Tuple; import org.elasticsearch.repositories.azure.AzureRepository.Repository; import org.elasticsearch.repositories.blobstore.ChunkedBlobOutputStream; -import reactor.core.publisher.Flux; -import reactor.core.publisher.Mono; -import reactor.core.scheduler.Schedulers; import java.io.FilterInputStream; import java.io.IOException; @@ -106,33 +108,21 @@ public AzureBlobStore(RepositoryMetadata metadata, AzureStorageService service, this.maxSinglePartUploadSize = Repository.MAX_SINGLE_PART_UPLOAD_SIZE_SETTING.get(metadata.settings()); List requestStatsCollectors = List.of( - RequestStatsCollector.create( - (httpMethod, url) -> httpMethod.equals("HEAD"), - stats.headOperations::incrementAndGet - ), + RequestStatsCollector.create((httpMethod, url) -> httpMethod.equals("HEAD"), stats.headOperations::incrementAndGet), RequestStatsCollector.create( (httpMethod, url) -> httpMethod.equals("GET") && isListRequest(httpMethod, url) == false, stats.getOperations::incrementAndGet ), - RequestStatsCollector.create( - this::isListRequest, - stats.listOperations::incrementAndGet - ), - RequestStatsCollector.create( - this::isPutBlockRequest, - stats.putBlockOperations::incrementAndGet - ), - RequestStatsCollector.create( - this::isPutBlockListRequest, - stats.putBlockListOperations::incrementAndGet - ), + RequestStatsCollector.create(this::isListRequest, stats.listOperations::incrementAndGet), + RequestStatsCollector.create(this::isPutBlockRequest, stats.putBlockOperations::incrementAndGet), + RequestStatsCollector.create(this::isPutBlockListRequest, stats.putBlockListOperations::incrementAndGet), RequestStatsCollector.create( // https://docs.microsoft.com/en-us/rest/api/storageservices/put-blob#uri-parameters // The only URI parameter allowed for put-blob operation is "timeout", but if a sas token is used, // it's possible that the URI parameters contain additional parameters unrelated to the upload type. - (httpMethod, url) -> httpMethod.equals("PUT") && - isPutBlockRequest(httpMethod, url) == false && - isPutBlockListRequest(httpMethod, url) == false, + (httpMethod, url) -> httpMethod.equals("PUT") + && isPutBlockRequest(httpMethod, url) == false + && isPutBlockListRequest(httpMethod, url) == false, stats.putOperations::incrementAndGet ) ); @@ -160,24 +150,19 @@ public AzureBlobStore(RepositoryMetadata metadata, AzureStorageService service, } private boolean isListRequest(String httpMethod, URL url) { - return httpMethod.equals("GET") && - url.getQuery() != null && - url.getQuery().contains("comp=list"); + return httpMethod.equals("GET") && url.getQuery() != null && url.getQuery().contains("comp=list"); } // https://docs.microsoft.com/en-us/rest/api/storageservices/put-block private boolean isPutBlockRequest(String httpMethod, URL url) { String queryParams = url.getQuery() == null ? "" : url.getQuery(); - return httpMethod.equals("PUT") && - queryParams.contains("comp=block") && - queryParams.contains("blockid="); + return httpMethod.equals("PUT") && queryParams.contains("comp=block") && queryParams.contains("blockid="); } // https://docs.microsoft.com/en-us/rest/api/storageservices/put-block-list private boolean isPutBlockListRequest(String httpMethod, URL url) { String queryParams = url.getQuery() == null ? "" : url.getQuery(); - return httpMethod.equals("PUT") && - queryParams.contains("comp=blocklist"); + return httpMethod.equals("PUT") && queryParams.contains("comp=blocklist"); } public long getReadChunkSize() { @@ -206,8 +191,7 @@ public BlobContainer blobContainer(BlobPath path) { } @Override - public void close() { - } + public void close() {} public boolean blobExists(String blob) throws IOException { final BlobServiceClient client = client(); @@ -233,9 +217,8 @@ public DeleteResult deleteBlobDirectory(String path) throws IOException { SocketAccess.doPrivilegedVoidException(() -> { final BlobContainerAsyncClient blobContainerAsyncClient = asyncClient().getBlobContainerAsyncClient(container); - final ListBlobsOptions options = new ListBlobsOptions() - .setPrefix(path) - .setDetails(new BlobListDetails().setRetrieveMetadata(true)); + final ListBlobsOptions options = new ListBlobsOptions().setPrefix(path) + .setDetails(new BlobListDetails().setRetrieveMetadata(true)); try { blobContainerAsyncClient.listBlobs(options, null).flatMap(blobItem -> { if (blobItem.isPrefix() != null && blobItem.isPrefix()) { @@ -282,8 +265,9 @@ void deleteBlobs(Iterator blobs) throws IOException { final BlobContainerAsyncClient blobContainerClient = asyncClient.getBlobContainerAsyncClient(container); try { Flux.fromStream(StreamSupport.stream(Spliterators.spliteratorUnknownSize(blobs, Spliterator.ORDERED), false)) - .flatMap(blob -> getDeleteTask(blob, blobContainerClient.getBlobAsyncClient(blob)), CONCURRENT_DELETES) - .then().block(); + .flatMap(blob -> getDeleteTask(blob, blobContainerClient.getBlobAsyncClient(blob)), CONCURRENT_DELETES) + .then() + .block(); } catch (Exception e) { filterDeleteExceptionsAndRethrow(e, new IOException("Unable to delete blobs")); } @@ -292,11 +276,13 @@ void deleteBlobs(Iterator blobs) throws IOException { private static Mono getDeleteTask(String blobName, BlobAsyncClient blobAsyncClient) { return blobAsyncClient.delete() - // Ignore not found blobs, as it's possible that due to network errors a request - // for an already deleted blob is retried, causing an error. - .onErrorResume(e -> - e instanceof BlobStorageException && ((BlobStorageException) e).getStatusCode() == 404, throwable -> Mono.empty()) - .onErrorMap(throwable -> new IOException("Error deleting blob " + blobName, throwable)); + // Ignore not found blobs, as it's possible that due to network errors a request + // for an already deleted blob is retried, causing an error. + .onErrorResume( + e -> e instanceof BlobStorageException && ((BlobStorageException) e).getStatusCode() == 404, + throwable -> Mono.empty() + ) + .onErrorMap(throwable -> new IOException("Error deleting blob " + blobName, throwable)); } public InputStream getInputStream(String blob, long position, final @Nullable Long length) throws IOException { @@ -305,7 +291,7 @@ public InputStream getInputStream(String blob, long position, final @Nullable Lo final BlobServiceClient syncClient = azureBlobServiceClient.getSyncClient(); final BlobServiceAsyncClient asyncClient = azureBlobServiceClient.getAsyncClient(); - return SocketAccess.doPrivilegedException(() ->{ + return SocketAccess.doPrivilegedException(() -> { final BlobContainerClient blobContainerClient = syncClient.getBlobContainerClient(container); final BlobClient blobClient = blobContainerClient.getBlobClient(blob); final long totalSize; @@ -316,22 +302,26 @@ public InputStream getInputStream(String blob, long position, final @Nullable Lo } BlobAsyncClient blobAsyncClient = asyncClient.getBlobContainerAsyncClient(container).getBlobAsyncClient(blob); int maxReadRetries = service.getMaxReadRetries(clientName); - return new AzureInputStream(blobAsyncClient, position, length == null ? totalSize : length , totalSize, maxReadRetries, - azureBlobServiceClient.getAllocator()); + return new AzureInputStream( + blobAsyncClient, + position, + length == null ? totalSize : length, + totalSize, + maxReadRetries, + azureBlobServiceClient.getAllocator() + ); }); } public Map listBlobsByPrefix(String keyPath, String prefix) throws IOException { final var blobsBuilder = new HashMap(); - logger.trace(() -> - new ParameterizedMessage("listing container [{}], keyPath [{}], prefix [{}]", container, keyPath, prefix)); + logger.trace(() -> new ParameterizedMessage("listing container [{}], keyPath [{}], prefix [{}]", container, keyPath, prefix)); try { final BlobServiceClient client = client(); SocketAccess.doPrivilegedVoidException(() -> { final BlobContainerClient containerClient = client.getBlobContainerClient(container); final BlobListDetails details = new BlobListDetails().setRetrieveMetadata(true); - final ListBlobsOptions listBlobsOptions = new ListBlobsOptions() - .setPrefix(keyPath + (prefix == null ? "" : prefix)) + final ListBlobsOptions listBlobsOptions = new ListBlobsOptions().setPrefix(keyPath + (prefix == null ? "" : prefix)) .setDetails(details); for (final BlobItem blobItem : containerClient.listBlobsByHierarchy("/", listBlobsOptions, null)) { @@ -342,8 +332,7 @@ public Map listBlobsByPrefix(String keyPath, String prefix } String blobName = blobItem.getName().substring(keyPath.length()); - blobsBuilder.put(blobName, - new PlainBlobMetadata(blobName, properties.getContentLength())); + blobsBuilder.put(blobName, new PlainBlobMetadata(blobName, properties.getContentLength())); } }); } catch (Exception e) { @@ -372,8 +361,7 @@ public Map children(BlobPath path) throws IOException { } // Remove trailing slash directoryName = directoryName.substring(0, directoryName.length() - 1); - childrenBuilder.put(directoryName, - new AzureBlobContainer(BlobPath.EMPTY.add(blobItem.getName()), this)); + childrenBuilder.put(directoryName, new AzureBlobContainer(BlobPath.EMPTY.add(blobItem.getName()), this)); } } }); @@ -385,16 +373,15 @@ public Map children(BlobPath path) throws IOException { } public void writeBlob(String blobName, BytesReference bytes, boolean failIfAlreadyExists) { - Flux byteBufferFlux = - Flux.fromArray(BytesReference.toByteBuffers(bytes)); + Flux byteBufferFlux = Flux.fromArray(BytesReference.toByteBuffers(bytes)); executeSingleUpload(blobName, byteBufferFlux, bytes.length(), failIfAlreadyExists); } - public void writeBlob(String blobName, - boolean failIfAlreadyExists, - CheckedConsumer writer) throws IOException { + public void writeBlob(String blobName, boolean failIfAlreadyExists, CheckedConsumer writer) + throws IOException { final BlockBlobAsyncClient blockBlobAsyncClient = asyncClient().getBlobContainerAsyncClient(container) - .getBlobAsyncClient(blobName).getBlockBlobAsyncClient(); + .getBlobAsyncClient(blobName) + .getBlockBlobAsyncClient(); try (ChunkedBlobOutputStream out = new ChunkedBlobOutputStream<>(bigArrays, getUploadBlockSize()) { @Override @@ -403,11 +390,13 @@ protected void flushBuffer() { return; } final String blockId = makeMultipartBlockId(); - SocketAccess.doPrivilegedVoidException(() -> blockBlobAsyncClient.stageBlock( + SocketAccess.doPrivilegedVoidException( + () -> blockBlobAsyncClient.stageBlock( blockId, Flux.fromArray(BytesReference.toByteBuffers(buffer.bytes())), buffer.size() - ).block()); + ).block() + ); finishPart(blockId); } @@ -418,7 +407,8 @@ protected void onCompletion() { } else { flushBuffer(); SocketAccess.doPrivilegedVoidException( - () -> blockBlobAsyncClient.commitBlockList(parts, failIfAlreadyExists == false).block()); + () -> blockBlobAsyncClient.commitBlockList(parts, failIfAlreadyExists == false).block() + ); } } @@ -439,15 +429,15 @@ public void writeBlob(String blobName, InputStream inputStream, long blobSize, b logger.trace(() -> new ParameterizedMessage("writeBlob({}, stream, {})", blobName, blobSize)); try { if (blobSize <= getLargeBlobThresholdInBytes()) { - final Flux byteBufferFlux = - convertStreamToByteBuffer(inputStream, blobSize, DEFAULT_UPLOAD_BUFFERS_SIZE); + final Flux byteBufferFlux = convertStreamToByteBuffer(inputStream, blobSize, DEFAULT_UPLOAD_BUFFERS_SIZE); executeSingleUpload(blobName, byteBufferFlux, blobSize, failIfAlreadyExists); } else { executeMultipartUpload(blobName, inputStream, blobSize, failIfAlreadyExists); } } catch (final BlobStorageException e) { - if (failIfAlreadyExists && e.getStatusCode() == HttpURLConnection.HTTP_CONFLICT && - BlobErrorCode.BLOB_ALREADY_EXISTS.equals(e.getErrorCode())) { + if (failIfAlreadyExists + && e.getStatusCode() == HttpURLConnection.HTTP_CONFLICT + && BlobErrorCode.BLOB_ALREADY_EXISTS.equals(e.getErrorCode())) { throw new FileAlreadyExistsException(blobName, null, e.getMessage()); } throw new IOException("Unable to write blob " + blobName, e); @@ -478,8 +468,7 @@ private void executeSingleUpload(String blobName, Flux byteBufferFlu private void executeMultipartUpload(String blobName, InputStream inputStream, long blobSize, boolean failIfAlreadyExists) { SocketAccess.doPrivilegedVoidException(() -> { final BlobServiceAsyncClient asyncClient = asyncClient(); - final BlobAsyncClient blobAsyncClient = asyncClient.getBlobContainerAsyncClient(container) - .getBlobAsyncClient(blobName); + final BlobAsyncClient blobAsyncClient = asyncClient.getBlobContainerAsyncClient(container).getBlobAsyncClient(blobName); final BlockBlobAsyncClient blockBlobAsyncClient = blobAsyncClient.getBlockBlobAsyncClient(); final long partSize = getUploadBlockSize(); @@ -551,37 +540,37 @@ public synchronized int read() throws IOException { // length is at most 100MB so it's safe to cast back to an integer in this case final int parts = (int) length / chunkSize; final long remaining = length % chunkSize; - return Flux.range(0, remaining == 0 ? parts : parts + 1) - .map(i -> i * chunkSize) - .concatMap(pos -> Mono.fromCallable(() -> { - long count = pos + chunkSize > length ? length - pos : chunkSize; - int numOfBytesRead = 0; - int offset = 0; - int len = (int) count; - final byte[] buffer = new byte[len]; - while (numOfBytesRead != -1 && offset < count) { - numOfBytesRead = inputStream.read(buffer, offset, len); - offset += numOfBytesRead; - len -= numOfBytesRead; - if (numOfBytesRead != -1) { - currentTotalLength.addAndGet(numOfBytesRead); - } - } - if (numOfBytesRead == -1 && currentTotalLength.get() < length) { - throw new IllegalStateException( - "InputStream provided" + currentTotalLength + " bytes, less than the expected" + length + " bytes" - ); + return Flux.range(0, remaining == 0 ? parts : parts + 1).map(i -> i * chunkSize).concatMap(pos -> Mono.fromCallable(() -> { + long count = pos + chunkSize > length ? length - pos : chunkSize; + int numOfBytesRead = 0; + int offset = 0; + int len = (int) count; + final byte[] buffer = new byte[len]; + while (numOfBytesRead != -1 && offset < count) { + numOfBytesRead = inputStream.read(buffer, offset, len); + offset += numOfBytesRead; + len -= numOfBytesRead; + if (numOfBytesRead != -1) { + currentTotalLength.addAndGet(numOfBytesRead); } - return ByteBuffer.wrap(buffer); - })) - .doOnComplete(() -> { - if (currentTotalLength.get() > length) { - throw new IllegalStateException( - "Read more data than was requested. Size of data read: " + currentTotalLength.get() + "." + - " Size of data requested: " + length - ); - } - }); + } + if (numOfBytesRead == -1 && currentTotalLength.get() < length) { + throw new IllegalStateException( + "InputStream provided" + currentTotalLength + " bytes, less than the expected" + length + " bytes" + ); + } + return ByteBuffer.wrap(buffer); + })).doOnComplete(() -> { + if (currentTotalLength.get() > length) { + throw new IllegalStateException( + "Read more data than was requested. Size of data read: " + + currentTotalLength.get() + + "." + + " Size of data requested: " + + length + ); + } + }); }).subscribeOn(Schedulers.elastic()); // We need to subscribe on a different scheduler to avoid blocking the io threads when // we read the input stream (i.e. when it's rate limited) } @@ -654,12 +643,20 @@ private static class Stats { private final AtomicLong putBlockListOperations = new AtomicLong(); private Map toMap() { - return Map.of("GetBlob", getOperations.get(), - "ListBlobs", listOperations.get(), - "GetBlobProperties", headOperations.get(), - "PutBlob", putOperations.get(), - "PutBlock", putBlockOperations.get(), - "PutBlockList", putBlockListOperations.get()); + return Map.of( + "GetBlob", + getOperations.get(), + "ListBlobs", + listOperations.get(), + "GetBlobProperties", + headOperations.get(), + "PutBlob", + putOperations.get(), + "PutBlock", + putBlockOperations.get(), + "PutBlockList", + putBlockListOperations.get() + ); } } @@ -669,33 +666,32 @@ private static class AzureInputStream extends InputStream { private boolean closed; private final ByteBufAllocator allocator; - private AzureInputStream(final BlobAsyncClient client, - long rangeOffset, - long rangeLength, - long contentLength, - int maxRetries, - ByteBufAllocator allocator) throws IOException { + private AzureInputStream( + final BlobAsyncClient client, + long rangeOffset, + long rangeLength, + long contentLength, + int maxRetries, + ByteBufAllocator allocator + ) throws IOException { rangeLength = Math.min(rangeLength, contentLength - rangeOffset); final BlobRange range = new BlobRange(rangeOffset, rangeLength); - DownloadRetryOptions downloadRetryOptions = new DownloadRetryOptions() - .setMaxRetryRequests(maxRetries); - Flux byteBufFlux = - client.downloadWithResponse(range, downloadRetryOptions, null, false) - .flux() - .concatMap(ResponseBase::getValue) // it's important to use concatMap, since flatMap doesn't provide ordering - // guarantees and that's not fun to debug :( - .filter(Objects::nonNull) - .map(this::copyBuffer); // Sadly we have to copy the buffers since the memory is released after the flux execution - // ends and we need that the byte buffer outlives that lifecycle. Since the SDK provides an - // ByteBuffer instead of a ByteBuf we cannot just increase the ref count and release the - // memory later on. + DownloadRetryOptions downloadRetryOptions = new DownloadRetryOptions().setMaxRetryRequests(maxRetries); + Flux byteBufFlux = client.downloadWithResponse(range, downloadRetryOptions, null, false) + .flux() + .concatMap(ResponseBase::getValue) // it's important to use concatMap, since flatMap doesn't provide ordering + // guarantees and that's not fun to debug :( + .filter(Objects::nonNull) + .map(this::copyBuffer); // Sadly we have to copy the buffers since the memory is released after the flux execution + // ends and we need that the byte buffer outlives that lifecycle. Since the SDK provides an + // ByteBuffer instead of a ByteBuf we cannot just increase the ref count and release the + // memory later on. this.allocator = allocator; // On the transport layer we read the recv buffer in 64kb chunks, but later on those buffers are // split into 8kb chunks (see HttpObjectDecoder), so we request upstream the equivalent to 64kb. (i.e. 8 elements per batch * // 8kb) - this.cancellableRateLimitedFluxIterator = - new CancellableRateLimitedFluxIterator<>(8, ReferenceCountUtil::safeRelease); + this.cancellableRateLimitedFluxIterator = new CancellableRateLimitedFluxIterator<>(8, ReferenceCountUtil::safeRelease); // Read eagerly the first chunk so we can throw early if the // blob doesn't exist byteBufFlux.subscribe(cancellableRateLimitedFluxIterator); @@ -782,14 +778,12 @@ private static class RequestStatsCollector { private final BiPredicate filter; private final Runnable onHttpRequest; - private RequestStatsCollector(BiPredicate filter, - Runnable onHttpRequest) { + private RequestStatsCollector(BiPredicate filter, Runnable onHttpRequest) { this.filter = filter; this.onHttpRequest = onHttpRequest; } - static RequestStatsCollector create(BiPredicate filter, - Runnable consumer) { + static RequestStatsCollector create(BiPredicate filter, Runnable consumer) { return new RequestStatsCollector(filter, consumer); } diff --git a/plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureClientProvider.java b/plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureClientProvider.java index f9caf98289f73..18601757efea9 100644 --- a/plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureClientProvider.java +++ b/plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureClientProvider.java @@ -8,6 +8,16 @@ package org.elasticsearch.repositories.azure; +import io.netty.buffer.ByteBufAllocator; +import io.netty.buffer.PooledByteBufAllocator; +import io.netty.channel.ChannelOption; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.nio.NioEventLoopGroup; +import reactor.core.publisher.Mono; +import reactor.core.scheduler.Scheduler; +import reactor.core.scheduler.Schedulers; +import reactor.netty.resources.ConnectionProvider; + import com.azure.core.http.HttpClient; import com.azure.core.http.HttpMethod; import com.azure.core.http.HttpPipelineCallContext; @@ -23,11 +33,7 @@ import com.azure.storage.blob.BlobServiceClientBuilder; import com.azure.storage.common.implementation.connectionstring.StorageConnectionString; import com.azure.storage.common.policy.RequestRetryOptions; -import io.netty.buffer.ByteBufAllocator; -import io.netty.buffer.PooledByteBufAllocator; -import io.netty.channel.ChannelOption; -import io.netty.channel.EventLoopGroup; -import io.netty.channel.nio.NioEventLoopGroup; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.common.component.AbstractLifecycleComponent; @@ -37,10 +43,6 @@ import org.elasticsearch.repositories.azure.executors.PrivilegedExecutor; import org.elasticsearch.repositories.azure.executors.ReactorScheduledExecutorService; import org.elasticsearch.threadpool.ThreadPool; -import reactor.core.publisher.Mono; -import reactor.core.scheduler.Scheduler; -import reactor.core.scheduler.Schedulers; -import reactor.netty.resources.ConnectionProvider; import java.io.IOException; import java.net.URL; @@ -64,23 +66,27 @@ class AzureClientProvider extends AbstractLifecycleComponent { "repository.azure.http_client.event_loop_executor_thread_count", DEFAULT_EVENT_LOOP_THREAD_COUNT, 1, - Setting.Property.NodeScope); + Setting.Property.NodeScope + ); static final Setting MAX_OPEN_CONNECTIONS = Setting.intSetting( "repository.azure.http_client.max_open_connections", DEFAULT_MAX_CONNECTIONS, 1, - Setting.Property.NodeScope); + Setting.Property.NodeScope + ); static final Setting OPEN_CONNECTION_TIMEOUT = Setting.timeSetting( "repository.azure.http_client.connection_timeout", DEFAULT_CONNECTION_TIMEOUT, - Setting.Property.NodeScope); + Setting.Property.NodeScope + ); static final Setting MAX_IDLE_TIME = Setting.timeSetting( "repository.azure.http_client.connection_max_idle_time", DEFAULT_MAX_CONNECTION_IDLE_TIME, - Setting.Property.NodeScope); + Setting.Property.NodeScope + ); private final ThreadPool threadPool; private final String reactorExecutorName; @@ -90,11 +96,13 @@ class AzureClientProvider extends AbstractLifecycleComponent { private final ClientLogger clientLogger = new ClientLogger(AzureClientProvider.class); private volatile boolean closed = false; - AzureClientProvider(ThreadPool threadPool, - String reactorExecutorName, - EventLoopGroup eventLoopGroup, - ConnectionProvider connectionProvider, - ByteBufAllocator byteBufAllocator) { + AzureClientProvider( + ThreadPool threadPool, + String reactorExecutorName, + EventLoopGroup eventLoopGroup, + ConnectionProvider connectionProvider, + ByteBufAllocator byteBufAllocator + ) { this.threadPool = threadPool; this.reactorExecutorName = reactorExecutorName; this.eventLoopGroup = eventLoopGroup; @@ -111,19 +119,20 @@ static AzureClientProvider create(ThreadPool threadPool, Settings settings) { // Most of the code that needs special permissions (i.e. jackson serializers generation) is executed // in the event loop executor. That's the reason why we should provide an executor that allows the // execution of privileged code - final EventLoopGroup eventLoopGroup = new NioEventLoopGroup(eventLoopThreadsFromSettings(settings), - new PrivilegedExecutor(eventLoopExecutor)); + final EventLoopGroup eventLoopGroup = new NioEventLoopGroup( + eventLoopThreadsFromSettings(settings), + new PrivilegedExecutor(eventLoopExecutor) + ); final TimeValue openConnectionTimeout = OPEN_CONNECTION_TIMEOUT.get(settings); final TimeValue maxIdleTime = MAX_IDLE_TIME.get(settings); - ConnectionProvider provider = - ConnectionProvider.builder("azure-sdk-connection-pool") - .maxConnections(MAX_OPEN_CONNECTIONS.get(settings)) - .pendingAcquireMaxCount(PENDING_CONNECTION_QUEUE_SIZE) // This determines the max outstanding queued requests - .pendingAcquireTimeout(Duration.ofMillis(openConnectionTimeout.millis())) - .maxIdleTime(Duration.ofMillis(maxIdleTime.millis())) - .build(); + ConnectionProvider provider = ConnectionProvider.builder("azure-sdk-connection-pool") + .maxConnections(MAX_OPEN_CONNECTIONS.get(settings)) + .pendingAcquireMaxCount(PENDING_CONNECTION_QUEUE_SIZE) // This determines the max outstanding queued requests + .pendingAcquireTimeout(Duration.ofMillis(openConnectionTimeout.millis())) + .maxIdleTime(Duration.ofMillis(maxIdleTime.millis())) + .build(); ByteBufAllocator pooledByteBufAllocator = createByteBufAllocator(); @@ -140,30 +149,22 @@ private static ByteBufAllocator createByteBufAllocator() { int smallCacheSize = PooledByteBufAllocator.defaultSmallCacheSize(); int normalCacheSize = PooledByteBufAllocator.defaultNormalCacheSize(); - return new PooledByteBufAllocator(false, - nHeapArena, - 0, - pageSize, - maxOrder, - tinyCacheSize, - smallCacheSize, - normalCacheSize, - false); + return new PooledByteBufAllocator(false, nHeapArena, 0, pageSize, maxOrder, tinyCacheSize, smallCacheSize, normalCacheSize, false); } - AzureBlobServiceClient createClient(AzureStorageSettings settings, - LocationMode locationMode, - RequestRetryOptions retryOptions, - ProxyOptions proxyOptions, - BiConsumer successfulRequestConsumer) { + AzureBlobServiceClient createClient( + AzureStorageSettings settings, + LocationMode locationMode, + RequestRetryOptions retryOptions, + ProxyOptions proxyOptions, + BiConsumer successfulRequestConsumer + ) { if (closed) { throw new IllegalStateException("AzureClientProvider is already closed"); } reactor.netty.http.client.HttpClient nettyHttpClient = reactor.netty.http.client.HttpClient.create(connectionProvider); - nettyHttpClient = nettyHttpClient - .port(80) - .wiretap(false); + nettyHttpClient = nettyHttpClient.port(80).wiretap(false); nettyHttpClient = nettyHttpClient.tcpConfiguration(tcpClient -> { tcpClient = tcpClient.runOn(eventLoopGroup); @@ -171,15 +172,11 @@ AzureBlobServiceClient createClient(AzureStorageSettings settings, return tcpClient; }); - final HttpClient httpClient = new NettyAsyncHttpClientBuilder(nettyHttpClient) - .disableBufferCopy(true) - .proxy(proxyOptions) - .build(); + final HttpClient httpClient = new NettyAsyncHttpClientBuilder(nettyHttpClient).disableBufferCopy(true).proxy(proxyOptions).build(); final String connectionString = settings.getConnectString(); - BlobServiceClientBuilder builder = new BlobServiceClientBuilder() - .connectionString(connectionString) + BlobServiceClientBuilder builder = new BlobServiceClientBuilder().connectionString(connectionString) .httpClient(httpClient) .retryOptions(retryOptions); @@ -192,8 +189,9 @@ AzureBlobServiceClient createClient(AzureStorageSettings settings, StorageConnectionString storageConnectionString = StorageConnectionString.create(connectionString, clientLogger); String secondaryUri = storageConnectionString.getBlobEndpoint().getSecondaryUri(); if (secondaryUri == null) { - throw new IllegalArgumentException("Unable to configure an AzureClient using a secondary location without a secondary " + - "endpoint"); + throw new IllegalArgumentException( + "Unable to configure an AzureClient using a secondary location without a secondary " + "endpoint" + ); } builder.endpoint(secondaryUri); @@ -264,8 +262,7 @@ private SuccessfulRequestTracker(BiConsumer onSuccessfulRequest) { @Override public Mono process(HttpPipelineCallContext context, HttpPipelineNextPolicy next) { - return next.process() - .doOnSuccess(httpResponse -> trackSuccessfulRequest(context.getHttpRequest(), httpResponse)); + return next.process().doOnSuccess(httpResponse -> trackSuccessfulRequest(context.getHttpRequest(), httpResponse)); } private void trackSuccessfulRequest(HttpRequest httpRequest, HttpResponse httpResponse) { diff --git a/plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureRepository.java b/plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureRepository.java index 13175864bf69a..c83da03dc6793 100644 --- a/plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureRepository.java +++ b/plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureRepository.java @@ -21,9 +21,9 @@ import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.common.util.BigArrays; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.indices.recovery.RecoverySettings; import org.elasticsearch.repositories.blobstore.MeteredBlobStoreRepository; +import org.elasticsearch.xcontent.NamedXContentRegistry; import java.util.Locale; import java.util.Map; @@ -50,21 +50,42 @@ public class AzureRepository extends MeteredBlobStoreRepository { public static final class Repository { @Deprecated // Replaced by client - public static final Setting ACCOUNT_SETTING = new Setting<>("account", "default", Function.identity(), - Property.NodeScope, Property.Deprecated); + public static final Setting ACCOUNT_SETTING = new Setting<>( + "account", + "default", + Function.identity(), + Property.NodeScope, + Property.Deprecated + ); public static final Setting CLIENT_NAME = new Setting<>("client", ACCOUNT_SETTING, Function.identity()); - public static final Setting CONTAINER_SETTING = - new Setting<>("container", "elasticsearch-snapshots", Function.identity(), Property.NodeScope); + public static final Setting CONTAINER_SETTING = new Setting<>( + "container", + "elasticsearch-snapshots", + Function.identity(), + Property.NodeScope + ); public static final Setting BASE_PATH_SETTING = Setting.simpleString("base_path", Property.NodeScope); - public static final Setting LOCATION_MODE_SETTING = new Setting<>("location_mode", - s -> LocationMode.PRIMARY_ONLY.toString(), s -> LocationMode.valueOf(s.toUpperCase(Locale.ROOT)), Property.NodeScope); - public static final Setting CHUNK_SIZE_SETTING = - Setting.byteSizeSetting("chunk_size", MAX_CHUNK_SIZE, MIN_CHUNK_SIZE, MAX_CHUNK_SIZE, Property.NodeScope); + public static final Setting LOCATION_MODE_SETTING = new Setting<>( + "location_mode", + s -> LocationMode.PRIMARY_ONLY.toString(), + s -> LocationMode.valueOf(s.toUpperCase(Locale.ROOT)), + Property.NodeScope + ); + public static final Setting CHUNK_SIZE_SETTING = Setting.byteSizeSetting( + "chunk_size", + MAX_CHUNK_SIZE, + MIN_CHUNK_SIZE, + MAX_CHUNK_SIZE, + Property.NodeScope + ); public static final Setting READONLY_SETTING = Setting.boolSetting(READONLY_SETTING_KEY, false, Property.NodeScope); // see ModelHelper.BLOB_DEFAULT_MAX_SINGLE_UPLOAD_SIZE private static final ByteSizeValue DEFAULT_MAX_SINGLE_UPLOAD_SIZE = new ByteSizeValue(256, ByteSizeUnit.MB); - public static final Setting MAX_SINGLE_PART_UPLOAD_SIZE_SETTING = - Setting.byteSizeSetting("max_single_part_upload_size", DEFAULT_MAX_SINGLE_UPLOAD_SIZE, Property.NodeScope); + public static final Setting MAX_SINGLE_PART_UPLOAD_SIZE_SETTING = Setting.byteSizeSetting( + "max_single_part_upload_size", + DEFAULT_MAX_SINGLE_UPLOAD_SIZE, + Property.NodeScope + ); } private final ByteSizeValue chunkSize; @@ -77,14 +98,17 @@ public AzureRepository( final AzureStorageService storageService, final ClusterService clusterService, final BigArrays bigArrays, - final RecoverySettings recoverySettings) { - super(metadata, + final RecoverySettings recoverySettings + ) { + super( + metadata, namedXContentRegistry, clusterService, bigArrays, recoverySettings, buildBasePath(metadata), - buildLocation(metadata)); + buildLocation(metadata) + ); this.chunkSize = Repository.CHUNK_SIZE_SETTING.get(metadata.settings()); this.storageService = storageService; @@ -103,7 +127,7 @@ private static BlobPath buildBasePath(RepositoryMetadata metadata) { if (Strings.hasLength(basePath)) { // Remove starting / if any BlobPath path = BlobPath.EMPTY; - for(final String elem : basePath.split("/")) { + for (final String elem : basePath.split("/")) { path = path.add(elem); } return path; @@ -113,8 +137,12 @@ private static BlobPath buildBasePath(RepositoryMetadata metadata) { } private static Map buildLocation(RepositoryMetadata metadata) { - return Map.of("base_path", Repository.BASE_PATH_SETTING.get(metadata.settings()), - "container", Repository.CONTAINER_SETTING.get(metadata.settings())); + return Map.of( + "base_path", + Repository.BASE_PATH_SETTING.get(metadata.settings()), + "container", + Repository.CONTAINER_SETTING.get(metadata.settings()) + ); } @Override @@ -126,9 +154,15 @@ protected BlobStore getBlobStore() { protected AzureBlobStore createBlobStore() { final AzureBlobStore blobStore = new AzureBlobStore(metadata, storageService, bigArrays); - logger.debug(() -> new ParameterizedMessage( - "using container [{}], chunk_size [{}], compress [{}], base_path [{}]", - blobStore, chunkSize, isCompress(), basePath())); + logger.debug( + () -> new ParameterizedMessage( + "using container [{}], chunk_size [{}], compress [{}], base_path [{}]", + blobStore, + chunkSize, + isCompress(), + basePath() + ) + ); return blobStore; } diff --git a/plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureRepositoryPlugin.java b/plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureRepositoryPlugin.java index 562e974a875d1..0634f02b69af6 100644 --- a/plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureRepositoryPlugin.java +++ b/plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureRepositoryPlugin.java @@ -67,9 +67,13 @@ public AzureRepositoryPlugin(Settings settings) { } @Override - public Map getRepositories(Environment env, NamedXContentRegistry namedXContentRegistry, - ClusterService clusterService, BigArrays bigArrays, - RecoverySettings recoverySettings) { + public Map getRepositories( + Environment env, + NamedXContentRegistry namedXContentRegistry, + ClusterService clusterService, + BigArrays bigArrays, + RecoverySettings recoverySettings + ) { return Collections.singletonMap(AzureRepository.TYPE, metadata -> { AzureStorageService storageService = azureStoreService.get(); assert storageService != null; @@ -78,19 +82,20 @@ public Map getRepositories(Environment env, NamedXCo } @Override - public Collection createComponents(Client client, - ClusterService clusterService, - ThreadPool threadPool, - ResourceWatcherService resourceWatcherService, - ScriptService scriptService, - NamedXContentRegistry xContentRegistry, - Environment environment, - NodeEnvironment nodeEnvironment, - NamedWriteableRegistry namedWriteableRegistry, - IndexNameExpressionResolver indexNameExpressionResolver, - Supplier repositoriesServiceSupplier) { - AzureClientProvider azureClientProvider = - AzureClientProvider.create(threadPool, settings); + public Collection createComponents( + Client client, + ClusterService clusterService, + ThreadPool threadPool, + ResourceWatcherService resourceWatcherService, + ScriptService scriptService, + NamedXContentRegistry xContentRegistry, + Environment environment, + NodeEnvironment nodeEnvironment, + NamedWriteableRegistry namedWriteableRegistry, + IndexNameExpressionResolver indexNameExpressionResolver, + Supplier repositoriesServiceSupplier + ) { + AzureClientProvider azureClientProvider = AzureClientProvider.create(threadPool, settings); azureStoreService.set(createAzureStorageService(settings, azureClientProvider)); return List.of(azureClientProvider); } diff --git a/plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureStorageService.java b/plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureStorageService.java index 2b9fee5aeb2e4..529d9f42a5e5b 100644 --- a/plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureStorageService.java +++ b/plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureStorageService.java @@ -13,6 +13,7 @@ import com.azure.storage.common.implementation.connectionstring.StorageConnectionString; import com.azure.storage.common.policy.RequestRetryOptions; import com.azure.storage.common.policy.RetryPolicyType; + import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsException; import org.elasticsearch.common.unit.ByteSizeUnit; @@ -50,10 +51,10 @@ public class AzureStorageService { private static final ByteSizeValue DEFAULT_BLOCK_SIZE = new ByteSizeValue( Math.max( ByteSizeUnit.MB.toBytes(5), // minimum value - Math.min( - MAX_BLOCK_SIZE.getBytes(), - JvmInfo.jvmInfo().getMem().getHeapMax().getBytes() / 20)), - ByteSizeUnit.BYTES); + Math.min(MAX_BLOCK_SIZE.getBytes(), JvmInfo.jvmInfo().getMem().getHeapMax().getBytes() / 20) + ), + ByteSizeUnit.BYTES + ); /** * The maximum size of a Block Blob. @@ -64,7 +65,7 @@ public class AzureStorageService { /** * Maximum allowed blob size in Azure blob store. */ - public static final ByteSizeValue MAX_CHUNK_SIZE = new ByteSizeValue(MAX_BLOB_SIZE , ByteSizeUnit.BYTES); + public static final ByteSizeValue MAX_CHUNK_SIZE = new ByteSizeValue(MAX_BLOB_SIZE, ByteSizeUnit.BYTES); private static final long DEFAULT_UPLOAD_BLOCK_SIZE = DEFAULT_BLOCK_SIZE.getBytes(); @@ -161,9 +162,14 @@ RequestRetryOptions getRetryOptions(LocationMode locationMode, AzureStorageSetti // to fix this issue. TimeValue configuredTimeout = azureStorageSettings.getTimeout(); int timeout = configuredTimeout.duration() == -1 ? Integer.MAX_VALUE : Math.max(1, Math.toIntExact(configuredTimeout.getSeconds())); - return new RequestRetryOptions(RetryPolicyType.EXPONENTIAL, - azureStorageSettings.getMaxRetries(), timeout, - null, null, secondaryHost); + return new RequestRetryOptions( + RetryPolicyType.EXPONENTIAL, + azureStorageSettings.getMaxRetries(), + timeout, + null, + null, + secondaryHost + ); } /** diff --git a/plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureStorageSettings.java b/plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureStorageSettings.java index e2a2e3bbdbe7e..7e1c171a4d092 100644 --- a/plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureStorageSettings.java +++ b/plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureStorageSettings.java @@ -8,7 +8,6 @@ package org.elasticsearch.repositories.azure; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.SecureSetting; import org.elasticsearch.common.settings.SecureString; @@ -17,6 +16,7 @@ import org.elasticsearch.common.settings.Setting.Property; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsException; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; import java.net.InetAddress; @@ -36,39 +36,71 @@ final class AzureStorageSettings { private static final String AZURE_CLIENT_PREFIX_KEY = "azure.client."; /** Azure account name */ - public static final AffixSetting ACCOUNT_SETTING = - Setting.affixKeySetting(AZURE_CLIENT_PREFIX_KEY, "account", key -> SecureSetting.secureString(key, null)); + public static final AffixSetting ACCOUNT_SETTING = Setting.affixKeySetting( + AZURE_CLIENT_PREFIX_KEY, + "account", + key -> SecureSetting.secureString(key, null) + ); /** Azure key */ - public static final AffixSetting KEY_SETTING = Setting.affixKeySetting(AZURE_CLIENT_PREFIX_KEY, "key", - key -> SecureSetting.secureString(key, null)); + public static final AffixSetting KEY_SETTING = Setting.affixKeySetting( + AZURE_CLIENT_PREFIX_KEY, + "key", + key -> SecureSetting.secureString(key, null) + ); /** Azure SAS token */ - public static final AffixSetting SAS_TOKEN_SETTING = Setting.affixKeySetting(AZURE_CLIENT_PREFIX_KEY, "sas_token", - key -> SecureSetting.secureString(key, null)); + public static final AffixSetting SAS_TOKEN_SETTING = Setting.affixKeySetting( + AZURE_CLIENT_PREFIX_KEY, + "sas_token", + key -> SecureSetting.secureString(key, null) + ); /** max_retries: Number of retries in case of Azure errors. Defaults to 3 (RequestRetryOptions). */ - public static final AffixSetting MAX_RETRIES_SETTING = - Setting.affixKeySetting(AZURE_CLIENT_PREFIX_KEY, "max_retries", - (key) -> Setting.intSetting(key, DEFAULT_MAX_RETRIES, Setting.Property.NodeScope), - () -> ACCOUNT_SETTING, () -> KEY_SETTING); + public static final AffixSetting MAX_RETRIES_SETTING = Setting.affixKeySetting( + AZURE_CLIENT_PREFIX_KEY, + "max_retries", + (key) -> Setting.intSetting(key, DEFAULT_MAX_RETRIES, Setting.Property.NodeScope), + () -> ACCOUNT_SETTING, + () -> KEY_SETTING + ); /** * Azure endpoint suffix. Default to core.windows.net (CloudStorageAccount.DEFAULT_DNS). */ - public static final AffixSetting ENDPOINT_SUFFIX_SETTING = Setting.affixKeySetting(AZURE_CLIENT_PREFIX_KEY, "endpoint_suffix", - key -> Setting.simpleString(key, Property.NodeScope), () -> ACCOUNT_SETTING, () -> KEY_SETTING); + public static final AffixSetting ENDPOINT_SUFFIX_SETTING = Setting.affixKeySetting( + AZURE_CLIENT_PREFIX_KEY, + "endpoint_suffix", + key -> Setting.simpleString(key, Property.NodeScope), + () -> ACCOUNT_SETTING, + () -> KEY_SETTING + ); - public static final AffixSetting TIMEOUT_SETTING = Setting.affixKeySetting(AZURE_CLIENT_PREFIX_KEY, "timeout", - (key) -> Setting.timeSetting(key, TimeValue.timeValueMinutes(-1), Property.NodeScope), () -> ACCOUNT_SETTING, () -> KEY_SETTING); + public static final AffixSetting TIMEOUT_SETTING = Setting.affixKeySetting( + AZURE_CLIENT_PREFIX_KEY, + "timeout", + (key) -> Setting.timeSetting(key, TimeValue.timeValueMinutes(-1), Property.NodeScope), + () -> ACCOUNT_SETTING, + () -> KEY_SETTING + ); /** The type of the proxy to connect to azure through. Can be direct (no proxy, default), http or socks */ - public static final AffixSetting PROXY_TYPE_SETTING = Setting.affixKeySetting(AZURE_CLIENT_PREFIX_KEY, "proxy.type", - (key) -> new Setting<>(key, "direct", s -> Proxy.Type.valueOf(s.toUpperCase(Locale.ROOT)), Property.NodeScope) - , () -> ACCOUNT_SETTING, () -> KEY_SETTING); + public static final AffixSetting PROXY_TYPE_SETTING = Setting.affixKeySetting( + AZURE_CLIENT_PREFIX_KEY, + "proxy.type", + (key) -> new Setting<>(key, "direct", s -> Proxy.Type.valueOf(s.toUpperCase(Locale.ROOT)), Property.NodeScope), + () -> ACCOUNT_SETTING, + () -> KEY_SETTING + ); /** The host name of a proxy to connect to azure through. */ - public static final AffixSetting PROXY_HOST_SETTING = Setting.affixKeySetting(AZURE_CLIENT_PREFIX_KEY, "proxy.host", - (key) -> Setting.simpleString(key, Property.NodeScope), () -> KEY_SETTING, () -> ACCOUNT_SETTING, () -> PROXY_TYPE_SETTING); + public static final AffixSetting PROXY_HOST_SETTING = Setting.affixKeySetting( + AZURE_CLIENT_PREFIX_KEY, + "proxy.host", + (key) -> Setting.simpleString(key, Property.NodeScope), + () -> KEY_SETTING, + () -> ACCOUNT_SETTING, + () -> PROXY_TYPE_SETTING + ); /** The port of a proxy to connect to azure through. */ public static final Setting PROXY_PORT_SETTING = Setting.affixKeySetting( @@ -78,7 +110,8 @@ final class AzureStorageSettings { () -> ACCOUNT_SETTING, () -> KEY_SETTING, () -> PROXY_TYPE_SETTING, - () -> PROXY_HOST_SETTING); + () -> PROXY_HOST_SETTING + ); private final String account; private final String connectString; @@ -87,8 +120,17 @@ final class AzureStorageSettings { private final int maxRetries; private final Proxy proxy; - private AzureStorageSettings(String account, String key, String sasToken, String endpointSuffix, TimeValue timeout, int maxRetries, - Proxy.Type proxyType, String proxyHost, Integer proxyPort) { + private AzureStorageSettings( + String account, + String key, + String sasToken, + String endpointSuffix, + TimeValue timeout, + int maxRetries, + Proxy.Type proxyType, + String proxyHost, + Integer proxyPort + ) { this.account = account; this.connectString = buildConnectString(account, key, sasToken, endpointSuffix); this.endpointSuffix = endpointSuffix; @@ -156,7 +198,6 @@ private static String buildConnectString(String account, @Nullable String key, @ return connectionStringBuilder.toString(); } - @Override public String toString() { final StringBuilder sb = new StringBuilder("AzureStorageSettings{"); @@ -193,21 +234,26 @@ public static Map load(Settings settings) { // pkg private for tests /** Parse settings for a single client. */ private static AzureStorageSettings getClientSettings(Settings settings, String clientName) { - try (SecureString account = getConfigValue(settings, clientName, ACCOUNT_SETTING); - SecureString key = getConfigValue(settings, clientName, KEY_SETTING); - SecureString sasToken = getConfigValue(settings, clientName, SAS_TOKEN_SETTING)) { - return new AzureStorageSettings(account.toString(), key.toString(), sasToken.toString(), + try ( + SecureString account = getConfigValue(settings, clientName, ACCOUNT_SETTING); + SecureString key = getConfigValue(settings, clientName, KEY_SETTING); + SecureString sasToken = getConfigValue(settings, clientName, SAS_TOKEN_SETTING) + ) { + return new AzureStorageSettings( + account.toString(), + key.toString(), + sasToken.toString(), getValue(settings, clientName, ENDPOINT_SUFFIX_SETTING), getValue(settings, clientName, TIMEOUT_SETTING), getValue(settings, clientName, MAX_RETRIES_SETTING), getValue(settings, clientName, PROXY_TYPE_SETTING), getValue(settings, clientName, PROXY_HOST_SETTING), - getValue(settings, clientName, PROXY_PORT_SETTING)); + getValue(settings, clientName, PROXY_PORT_SETTING) + ); } } - private static T getConfigValue(Settings settings, String clientName, - Setting.AffixSetting clientSetting) { + private static T getConfigValue(Settings settings, String clientName, Setting.AffixSetting clientSetting) { final Setting concreteSetting = clientSetting.getConcreteSettingForNamespace(clientName); return concreteSetting.get(settings); } diff --git a/plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/CancellableRateLimitedFluxIterator.java b/plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/CancellableRateLimitedFluxIterator.java index c511274545349..aab052a4acdb0 100644 --- a/plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/CancellableRateLimitedFluxIterator.java +++ b/plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/CancellableRateLimitedFluxIterator.java @@ -77,7 +77,7 @@ public boolean hasNext() { // This method acts as a barrier between producers and consumers // and it's possible that the consumer thread is blocked // waiting until the producer emits an element. - for (; ; ) { + for (;;) { boolean isDone = done; boolean isQueueEmpty = queue.isEmpty(); @@ -129,8 +129,7 @@ public T next() { if (totalEmittedElements == elementsPerBatch) { emittedElements = 0; subscription.get().request(totalEmittedElements); - } - else { + } else { emittedElements = totalEmittedElements; } diff --git a/plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/executors/ReactorScheduledExecutorService.java b/plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/executors/ReactorScheduledExecutorService.java index 61133fb4dddd3..e25d8491382c0 100644 --- a/plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/executors/ReactorScheduledExecutorService.java +++ b/plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/executors/ReactorScheduledExecutorService.java @@ -11,9 +11,9 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.message.ParameterizedMessage; -import org.elasticsearch.core.TimeValue; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException; +import org.elasticsearch.core.SuppressForbidden; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.threadpool.Scheduler; import org.elasticsearch.threadpool.ThreadPool; @@ -73,9 +73,14 @@ public ScheduledFuture scheduleAtFixedRate(Runnable command, long initialDela delegate.execute(decoratedCommand); } catch (EsRejectedExecutionException e) { if (e.isExecutorShutdown()) { - logger.debug(new ParameterizedMessage( - "could not schedule execution of [{}] on [{}] as executor is shut down", - decoratedCommand, executorName), e); + logger.debug( + new ParameterizedMessage( + "could not schedule execution of [{}] on [{}] as executor is shut down", + decoratedCommand, + executorName + ), + e + ); } else { throw e; } @@ -87,9 +92,7 @@ public ScheduledFuture scheduleAtFixedRate(Runnable command, long initialDela public ScheduledFuture scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) { Runnable decorateRunnable = decorateRunnable(command); - Scheduler.Cancellable cancellable = threadPool.scheduleWithFixedDelay(decorateRunnable, - new TimeValue(delay, unit), - executorName); + Scheduler.Cancellable cancellable = threadPool.scheduleWithFixedDelay(decorateRunnable, new TimeValue(delay, unit), executorName); return new ReactorFuture<>(cancellable); } diff --git a/plugins/repository-azure/src/test/java/org/elasticsearch/repositories/azure/AzureBlobContainerRetriesTests.java b/plugins/repository-azure/src/test/java/org/elasticsearch/repositories/azure/AzureBlobContainerRetriesTests.java index f1a50150b0282..e7b929a0cc372 100644 --- a/plugins/repository-azure/src/test/java/org/elasticsearch/repositories/azure/AzureBlobContainerRetriesTests.java +++ b/plugins/repository-azure/src/test/java/org/elasticsearch/repositories/azure/AzureBlobContainerRetriesTests.java @@ -7,21 +7,20 @@ */ package org.elasticsearch.repositories.azure; +import fixture.azure.AzureHttpHandler; + import com.azure.storage.common.policy.RequestRetryOptions; import com.azure.storage.common.policy.RetryPolicyType; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpServer; -import fixture.azure.AzureHttpHandler; + import org.elasticsearch.cluster.metadata.RepositoryMetadata; import org.elasticsearch.common.Strings; -import org.elasticsearch.common.util.BigArrays; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.common.UUIDs; import org.elasticsearch.common.blobstore.BlobContainer; import org.elasticsearch.common.blobstore.BlobPath; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.io.Streams; import org.elasticsearch.common.lucene.store.ByteArrayIndexInput; import org.elasticsearch.common.lucene.store.InputStreamIndexInput; @@ -30,8 +29,11 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; -import org.elasticsearch.core.TimeValue; +import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.common.util.concurrent.CountDown; +import org.elasticsearch.core.SuppressForbidden; +import org.elasticsearch.core.TimeValue; +import org.elasticsearch.core.Tuple; import org.elasticsearch.mocksocket.MockHttpServer; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.RestUtils; @@ -96,7 +98,8 @@ public class AzureBlobContainerRetriesTests extends ESTestCase { @Before public void setUp() throws Exception { - threadPool = new TestThreadPool(getTestClass().getName(), + threadPool = new TestThreadPool( + getTestClass().getName(), AzureRepositoryPlugin.executorBuilder(), AzureRepositoryPlugin.nettyEventLoopExecutorBuilder(Settings.EMPTY) ); @@ -126,8 +129,7 @@ private BlobContainer createBlobContainer(final int maxRetries, String secondary final Settings.Builder clientSettings = Settings.builder(); final String clientName = randomAlphaOfLength(5).toLowerCase(Locale.ROOT); - String endpoint = - "ignored;DefaultEndpointsProtocol=http;BlobEndpoint=" + getEndpointForServer(httpServer, "account"); + String endpoint = "ignored;DefaultEndpointsProtocol=http;BlobEndpoint=" + getEndpointForServer(httpServer, "account"); if (secondaryHost != null) { endpoint += ";BlobSecondaryEndpoint=" + getEndpointForServer(secondaryHttpServer, "account"); } @@ -144,7 +146,8 @@ private BlobContainer createBlobContainer(final int maxRetries, String secondary final AzureStorageService service = new AzureStorageService(clientSettings.build(), clientProvider) { @Override RequestRetryOptions getRetryOptions(LocationMode locationMode, AzureStorageSettings azureStorageSettings) { - return new RequestRetryOptions(RetryPolicyType.EXPONENTIAL, + return new RequestRetryOptions( + RetryPolicyType.EXPONENTIAL, maxRetries + 1, 60, 50L, @@ -152,7 +155,8 @@ RequestRetryOptions getRetryOptions(LocationMode locationMode, AzureStorageSetti // The SDK doesn't work well with ip endponts. Secondary host endpoints that contain // a path causes the sdk to rewrite the endpoint with an invalid path, that's the reason why we provide just the host + // port. - secondaryHost != null ? secondaryHost.replaceFirst("/account", "") : null); + secondaryHost != null ? secondaryHost.replaceFirst("/account", "") : null + ); } @Override @@ -166,29 +170,31 @@ int getMaxReadRetries(String clientName) { } }; - final RepositoryMetadata repositoryMetadata = new RepositoryMetadata("repository", AzureRepository.TYPE, + final RepositoryMetadata repositoryMetadata = new RepositoryMetadata( + "repository", + AzureRepository.TYPE, Settings.builder() .put(CONTAINER_SETTING.getKey(), "container") .put(ACCOUNT_SETTING.getKey(), clientName) .put(LOCATION_MODE_SETTING.getKey(), locationMode) .put(MAX_SINGLE_PART_UPLOAD_SIZE_SETTING.getKey(), new ByteSizeValue(1, ByteSizeUnit.MB)) - .build()); + .build() + ); return new AzureBlobContainer(BlobPath.EMPTY, new AzureBlobStore(repositoryMetadata, service, BigArrays.NON_RECYCLING_INSTANCE)); } public void testReadNonexistentBlobThrowsNoSuchFileException() { final BlobContainer blobContainer = createBlobContainer(between(1, 5)); - final Exception exception = expectThrows(NoSuchFileException.class, - () -> { - if (randomBoolean()) { - blobContainer.readBlob("read_nonexistent_blob"); - } else { - final long position = randomLongBetween(0, MAX_RANGE_VAL - 1L); - final long length = randomLongBetween(1, MAX_RANGE_VAL - position); - blobContainer.readBlob("read_nonexistent_blob", position, length); - } - }); + final Exception exception = expectThrows(NoSuchFileException.class, () -> { + if (randomBoolean()) { + blobContainer.readBlob("read_nonexistent_blob"); + } else { + final long position = randomLongBetween(0, MAX_RANGE_VAL - 1L); + final long length = randomLongBetween(1, MAX_RANGE_VAL - position); + blobContainer.readBlob("read_nonexistent_blob", position, length); + } + }); assertThat(exception.toString(), exception.getMessage().toLowerCase(Locale.ROOT), containsString("not found")); } @@ -259,8 +265,8 @@ public void testReadRangeBlobWithRetries() throws Exception { final int length = (rangeEnd.get() - rangeStart) + 1; assertThat(length, lessThanOrEqualTo(bytes.length - rangeStart)); exchange.getResponseHeaders().add("Content-Type", "application/octet-stream"); - exchange.getResponseHeaders().add("Content-Range", - "bytes " + rangeStart + "-" + (rangeStart + rangeEnd.get() + 1) + "/" + bytes.length); + exchange.getResponseHeaders() + .add("Content-Range", "bytes " + rangeStart + "-" + (rangeStart + rangeEnd.get() + 1) + "/" + bytes.length); exchange.getResponseHeaders().add("x-ms-blob-content-length", String.valueOf(length)); exchange.getResponseHeaders().add("Content-Length", String.valueOf(length)); exchange.getResponseHeaders().add("x-ms-blob-type", "blockblob"); @@ -433,9 +439,9 @@ public void testWriteLargeBlobStreaming() throws Exception { if ("blocklist".equals(complete) && (countDownComplete.countDown())) { final String blockList = Streams.copyToString(new InputStreamReader(exchange.getRequestBody(), UTF_8)); final List blockUids = Arrays.stream(blockList.split("")) - .filter(line -> line.contains("")) - .map(line -> line.substring(0, line.indexOf(""))) - .collect(Collectors.toList()); + .filter(line -> line.contains("")) + .map(line -> line.substring(0, line.indexOf(""))) + .collect(Collectors.toList()); final ByteArrayOutputStream blob = new ByteArrayOutputStream(); for (String blockUid : blockUids) { @@ -485,8 +491,8 @@ public void testRetryUntilFail() throws Exception { throw new AssertionError("Should not receive any data"); } } catch (IOException e) { - // Suppress the exception since it's expected that the - // connection is closed before anything can be read + // Suppress the exception since it's expected that the + // connection is closed before anything can be read } finally { exchange.close(); } @@ -505,11 +511,12 @@ public boolean markSupported() { } @Override - public void reset() { - } + public void reset() {} }) { - final IOException ioe = expectThrows(IOException.class, () -> - blobContainer.writeBlob("write_blob_max_retries", stream, randomIntBetween(1, 128), randomBoolean())); + final IOException ioe = expectThrows( + IOException.class, + () -> blobContainer.writeBlob("write_blob_max_retries", stream, randomIntBetween(1, 128), randomBoolean()) + ); assertThat(ioe.getMessage(), is("Unable to write blob write_blob_max_retries")); // The mock http server uses 1 thread to process the requests, it's possible that the // call to writeBlob throws before all the requests have been processed in the http server, diff --git a/plugins/repository-azure/src/test/java/org/elasticsearch/repositories/azure/AzureClientProviderTests.java b/plugins/repository-azure/src/test/java/org/elasticsearch/repositories/azure/AzureClientProviderTests.java index 06080e4572bfb..5c957511bbbe3 100644 --- a/plugins/repository-azure/src/test/java/org/elasticsearch/repositories/azure/AzureClientProviderTests.java +++ b/plugins/repository-azure/src/test/java/org/elasticsearch/repositories/azure/AzureClientProviderTests.java @@ -9,6 +9,7 @@ package org.elasticsearch.repositories.azure; import com.azure.storage.common.policy.RequestRetryOptions; + import org.elasticsearch.common.settings.MockSecureSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.test.ESTestCase; @@ -25,14 +26,15 @@ import java.util.function.BiConsumer; public class AzureClientProviderTests extends ESTestCase { - private static final BiConsumer EMPTY_CONSUMER = (method, url) -> { }; + private static final BiConsumer EMPTY_CONSUMER = (method, url) -> {}; private ThreadPool threadPool; private AzureClientProvider azureClientProvider; @Before public void setUpThreadPool() { - threadPool = new TestThreadPool(getTestName(), + threadPool = new TestThreadPool( + getTestName(), AzureRepositoryPlugin.executorBuilder(), AzureRepositoryPlugin.nettyEventLoopExecutorBuilder(Settings.EMPTY) ); @@ -52,8 +54,8 @@ public void testCanCreateAClientWithSecondaryLocation() { final String endpoint; if (randomBoolean()) { - endpoint = "ignored;BlobEndpoint=https://myaccount1.blob.core.windows.net;" + - "BlobSecondaryEndpoint=https://myaccount1-secondary.blob.core.windows.net"; + endpoint = "ignored;BlobEndpoint=https://myaccount1.blob.core.windows.net;" + + "BlobSecondaryEndpoint=https://myaccount1-secondary.blob.core.windows.net"; } else { endpoint = "core.windows.net"; } @@ -90,9 +92,10 @@ public void testCanNotCreateAClientWithSecondaryLocationWithoutAProperEndpoint() LocationMode locationMode = LocationMode.SECONDARY_ONLY; RequestRetryOptions requestRetryOptions = new RequestRetryOptions(); - expectThrows(IllegalArgumentException.class, () -> { - azureClientProvider.createClient(storageSettings, locationMode, requestRetryOptions, null, EMPTY_CONSUMER); - }); + expectThrows( + IllegalArgumentException.class, + () -> { azureClientProvider.createClient(storageSettings, locationMode, requestRetryOptions, null, EMPTY_CONSUMER); } + ); } private static String encodeKey(final String value) { diff --git a/plugins/repository-azure/src/test/java/org/elasticsearch/repositories/azure/AzureRepositorySettingsTests.java b/plugins/repository-azure/src/test/java/org/elasticsearch/repositories/azure/AzureRepositorySettingsTests.java index 71115cda1869b..40cd1ff49ac1f 100644 --- a/plugins/repository-azure/src/test/java/org/elasticsearch/repositories/azure/AzureRepositorySettingsTests.java +++ b/plugins/repository-azure/src/test/java/org/elasticsearch/repositories/azure/AzureRepositorySettingsTests.java @@ -14,11 +14,11 @@ import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.common.util.MockBigArrays; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.env.Environment; import org.elasticsearch.indices.recovery.RecoverySettings; import org.elasticsearch.repositories.blobstore.BlobStoreTestUtil; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; import static org.elasticsearch.repositories.blobstore.BlobStoreRepository.READONLY_SETTING_KEY; import static org.hamcrest.Matchers.is; @@ -33,10 +33,14 @@ private AzureRepository azureRepository(Settings settings) { .putList(Environment.PATH_DATA_SETTING.getKey(), tmpPaths()) .put(settings) .build(); - final AzureRepository azureRepository = new AzureRepository(new RepositoryMetadata("foo", "azure", internalSettings), - NamedXContentRegistry.EMPTY, mock(AzureStorageService.class), BlobStoreTestUtil.mockClusterService(), + final AzureRepository azureRepository = new AzureRepository( + new RepositoryMetadata("foo", "azure", internalSettings), + NamedXContentRegistry.EMPTY, + mock(AzureStorageService.class), + BlobStoreTestUtil.mockClusterService(), MockBigArrays.NON_RECYCLING_INSTANCE, - new RecoverySettings(settings, new ClusterSettings(settings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS))); + new RecoverySettings(settings, new ClusterSettings(settings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS)) + ); assertThat(azureRepository.getBlobStore(), is(nullValue())); return azureRepository; } @@ -46,50 +50,76 @@ public void testReadonlyDefault() { } public void testReadonlyDefaultAndReadonlyOn() { - assertThat(azureRepository(Settings.builder() - .put(READONLY_SETTING_KEY, true) - .build()).isReadOnly(), is(true)); + assertThat(azureRepository(Settings.builder().put(READONLY_SETTING_KEY, true).build()).isReadOnly(), is(true)); } public void testReadonlyWithPrimaryOnly() { - assertThat(azureRepository(Settings.builder() - .put(AzureRepository.Repository.LOCATION_MODE_SETTING.getKey(), LocationMode.PRIMARY_ONLY.name()) - .build()).isReadOnly(), is(false)); + assertThat( + azureRepository( + Settings.builder().put(AzureRepository.Repository.LOCATION_MODE_SETTING.getKey(), LocationMode.PRIMARY_ONLY.name()).build() + ).isReadOnly(), + is(false) + ); } public void testReadonlyWithPrimaryOnlyAndReadonlyOn() { - assertThat(azureRepository(Settings.builder() - .put(AzureRepository.Repository.LOCATION_MODE_SETTING.getKey(), LocationMode.PRIMARY_ONLY.name()) - .put(READONLY_SETTING_KEY, true) - .build()).isReadOnly(), is(true)); + assertThat( + azureRepository( + Settings.builder() + .put(AzureRepository.Repository.LOCATION_MODE_SETTING.getKey(), LocationMode.PRIMARY_ONLY.name()) + .put(READONLY_SETTING_KEY, true) + .build() + ).isReadOnly(), + is(true) + ); } public void testReadonlyWithSecondaryOnlyAndReadonlyOn() { - assertThat(azureRepository(Settings.builder() - .put(AzureRepository.Repository.LOCATION_MODE_SETTING.getKey(), LocationMode.SECONDARY_ONLY.name()) - .put(READONLY_SETTING_KEY, true) - .build()).isReadOnly(), is(true)); + assertThat( + azureRepository( + Settings.builder() + .put(AzureRepository.Repository.LOCATION_MODE_SETTING.getKey(), LocationMode.SECONDARY_ONLY.name()) + .put(READONLY_SETTING_KEY, true) + .build() + ).isReadOnly(), + is(true) + ); } public void testReadonlyWithSecondaryOnlyAndReadonlyOff() { - assertThat(azureRepository(Settings.builder() - .put(AzureRepository.Repository.LOCATION_MODE_SETTING.getKey(), LocationMode.SECONDARY_ONLY.name()) - .put(READONLY_SETTING_KEY, false) - .build()).isReadOnly(), is(false)); + assertThat( + azureRepository( + Settings.builder() + .put(AzureRepository.Repository.LOCATION_MODE_SETTING.getKey(), LocationMode.SECONDARY_ONLY.name()) + .put(READONLY_SETTING_KEY, false) + .build() + ).isReadOnly(), + is(false) + ); } public void testReadonlyWithPrimaryAndSecondaryOnlyAndReadonlyOn() { - assertThat(azureRepository(Settings.builder() - .put(AzureRepository.Repository.LOCATION_MODE_SETTING.getKey(), LocationMode.PRIMARY_THEN_SECONDARY.name()) - .put(READONLY_SETTING_KEY, true) - .build()).isReadOnly(), is(true)); + assertThat( + azureRepository( + Settings.builder() + .put(AzureRepository.Repository.LOCATION_MODE_SETTING.getKey(), LocationMode.PRIMARY_THEN_SECONDARY.name()) + .put(READONLY_SETTING_KEY, true) + .build() + ).isReadOnly(), + is(true) + ); } public void testReadonlyWithPrimaryAndSecondaryOnlyAndReadonlyOff() { - assertThat(azureRepository(Settings.builder() - .put(AzureRepository.Repository.LOCATION_MODE_SETTING.getKey(), LocationMode.PRIMARY_THEN_SECONDARY.name()) - .put(READONLY_SETTING_KEY, false) - .build()).isReadOnly(), is(false)); + assertThat( + azureRepository( + Settings.builder() + .put(AzureRepository.Repository.LOCATION_MODE_SETTING.getKey(), LocationMode.PRIMARY_THEN_SECONDARY.name()) + .put(READONLY_SETTING_KEY, false) + .build() + ).isReadOnly(), + is(false) + ); } public void testChunkSize() { @@ -103,20 +133,22 @@ public void testChunkSize() { assertEquals(new ByteSizeValue(size, ByteSizeUnit.MB), azureRepository.chunkSize()); // zero bytes is not allowed - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> - azureRepository(Settings.builder().put("chunk_size", "0").build())); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> azureRepository(Settings.builder().put("chunk_size", "0").build()) + ); assertEquals("failed to parse value [0] for setting [chunk_size], must be >= [1b]", e.getMessage()); // negative bytes not allowed - e = expectThrows(IllegalArgumentException.class, () -> - azureRepository(Settings.builder().put("chunk_size", "-1").build())); + e = expectThrows(IllegalArgumentException.class, () -> azureRepository(Settings.builder().put("chunk_size", "-1").build())); assertEquals("failed to parse value [-1] for setting [chunk_size], must be >= [1b]", e.getMessage()); // greater than max chunk size not allowed - e = expectThrows(IllegalArgumentException.class, () -> - azureRepository(Settings.builder().put("chunk_size", "6tb").build())); - assertEquals("failed to parse value [6tb] for setting [chunk_size], must be <= [" - + AzureStorageService.MAX_CHUNK_SIZE.getStringRep() + "]", e.getMessage()); + e = expectThrows(IllegalArgumentException.class, () -> azureRepository(Settings.builder().put("chunk_size", "6tb").build())); + assertEquals( + "failed to parse value [6tb] for setting [chunk_size], must be <= [" + AzureStorageService.MAX_CHUNK_SIZE.getStringRep() + "]", + e.getMessage() + ); } } diff --git a/plugins/repository-azure/src/test/java/org/elasticsearch/repositories/azure/AzureStorageServiceTests.java b/plugins/repository-azure/src/test/java/org/elasticsearch/repositories/azure/AzureStorageServiceTests.java index 1d06ac19881d2..4c6552a299280 100644 --- a/plugins/repository-azure/src/test/java/org/elasticsearch/repositories/azure/AzureStorageServiceTests.java +++ b/plugins/repository-azure/src/test/java/org/elasticsearch/repositories/azure/AzureStorageServiceTests.java @@ -43,7 +43,8 @@ public class AzureStorageServiceTests extends ESTestCase { @Before public void setUpThreadPool() { - threadPool = new TestThreadPool(AzureStorageServiceTests.class.getName(), + threadPool = new TestThreadPool( + AzureStorageServiceTests.class.getName(), AzureRepositoryPlugin.executorBuilder(), AzureRepositoryPlugin.nettyEventLoopExecutorBuilder(Settings.EMPTY) ); @@ -55,11 +56,13 @@ public void tearDownThreadPool() { } public void testReadSecuredSettings() { - final Settings settings = Settings.builder().setSecureSettings(buildSecureSettings()) - .put("azure.client.azure3.endpoint_suffix", "my_endpoint_suffix").build(); + final Settings settings = Settings.builder() + .setSecureSettings(buildSecureSettings()) + .put("azure.client.azure3.endpoint_suffix", "my_endpoint_suffix") + .build(); final Map loadedSettings = AzureStorageSettings.load(settings); - assertThat(loadedSettings.keySet(), containsInAnyOrder("azure1","azure2","azure3","default")); + assertThat(loadedSettings.keySet(), containsInAnyOrder("azure1", "azure2", "azure3", "default")); assertThat(loadedSettings.get("azure1").getEndpointSuffix(), is(emptyString())); assertThat(loadedSettings.get("azure2").getEndpointSuffix(), is(emptyString())); @@ -69,17 +72,7 @@ public void testReadSecuredSettings() { private AzureRepositoryPlugin pluginWithSettingsValidation(Settings settings) { final AzureRepositoryPlugin plugin = new AzureRepositoryPlugin(settings); new SettingsModule(settings, plugin.getSettings(), Collections.emptyList(), Collections.emptySet()); - plugin.createComponents(null, - null, - threadPool, - null, - null, - null, - null, - null, - null, - null, - null); + plugin.createComponents(null, null, threadPool, null, null, null, null, null, null, null, null); return plugin; } @@ -92,8 +85,10 @@ private AzureStorageService storageServiceWithSettingsValidation(Settings settin } public void testCreateClientWithEndpointSuffix() throws IOException { - final Settings settings = Settings.builder().setSecureSettings(buildSecureSettings()) - .put("azure.client.azure1.endpoint_suffix", "my_endpoint_suffix").build(); + final Settings settings = Settings.builder() + .setSecureSettings(buildSecureSettings()) + .put("azure.client.azure1.endpoint_suffix", "my_endpoint_suffix") + .build(); try (AzureRepositoryPlugin plugin = pluginWithSettingsValidation(settings)) { final AzureStorageService azureStorageService = plugin.azureStoreService.get(); AzureBlobServiceClient client1 = azureStorageService.client("azure1", LocationMode.PRIMARY_ONLY); @@ -127,8 +122,10 @@ public void testReinitClientSettings() throws IOException { assertThat(client12.getSyncClient().getAccountUrl(), equalTo("https://myaccount12.blob.core.windows.net")); // client 3 is missing - final SettingsException e1 = expectThrows(SettingsException.class, - () -> azureStorageService.client("azure3", LocationMode.PRIMARY_ONLY)); + final SettingsException e1 = expectThrows( + SettingsException.class, + () -> azureStorageService.client("azure3", LocationMode.PRIMARY_ONLY) + ); assertThat(e1.getMessage(), is("Unable to find client with name [azure3]")); // update client settings @@ -145,8 +142,10 @@ public void testReinitClientSettings() throws IOException { assertThat(client12.getSyncClient().getAccountUrl(), equalTo("https://myaccount12.blob.core.windows.net")); // new client2 is gone - final SettingsException e2 = expectThrows(SettingsException.class, - () -> azureStorageService.client("azure2", LocationMode.PRIMARY_ONLY)); + final SettingsException e2 = expectThrows( + SettingsException.class, + () -> azureStorageService.client("azure2", LocationMode.PRIMARY_ONLY) + ); assertThat(e2.getMessage(), is("Unable to find client with name [azure2]")); // client 3 emerged @@ -169,8 +168,10 @@ public void testReinitClientEmptySettings() throws IOException { // existing client untouched assertThat(client11.getSyncClient().getAccountUrl(), equalTo("https://myaccount1.blob.core.windows.net")); // client is no longer registered - final SettingsException e = - expectThrows(SettingsException.class, () -> azureStorageService.client("azure1", LocationMode.PRIMARY_ONLY)); + final SettingsException e = expectThrows( + SettingsException.class, + () -> azureStorageService.client("azure1", LocationMode.PRIMARY_ONLY) + ); assertThat(e.getMessage(), equalTo("Unable to find client with name [azure1]")); } } @@ -203,9 +204,7 @@ public void testReinitClientWrongSettings() throws IOException { } public void testNoProxy() { - final Settings settings = Settings.builder() - .setSecureSettings(buildSecureSettings()) - .build(); + final Settings settings = Settings.builder().setSecureSettings(buildSecureSettings()).build(); final AzureStorageService mock = storageServiceWithSettingsValidation(settings); assertThat(mock.storageSettings.get("azure1").getProxy(), nullValue()); assertThat(mock.storageSettings.get("azure2").getProxy(), nullValue()); @@ -312,15 +311,12 @@ public void testProxyWrongHost() { } public void testDefaultTimeOut() throws Exception { - final Settings settings = Settings.builder() - .setSecureSettings(buildSecureSettings()) - .build(); + final Settings settings = Settings.builder().setSecureSettings(buildSecureSettings()).build(); try (AzureRepositoryPlugin plugin = pluginWithSettingsValidation(settings)) { final AzureStorageService azureStorageService = plugin.azureStoreService.get(); AzureStorageSettings azureStorageSettings = azureStorageService.storageSettings.get("azure1"); - RequestRetryOptions retryOptions = - azureStorageService.getRetryOptions(LocationMode.PRIMARY_ONLY, azureStorageSettings); + RequestRetryOptions retryOptions = azureStorageService.getRetryOptions(LocationMode.PRIMARY_ONLY, azureStorageSettings); assertThat(retryOptions.getTryTimeout(), equalTo(Integer.MAX_VALUE)); } } @@ -334,8 +330,7 @@ public void testMillisecondsTimeOutIsRoundedUp() throws Exception { try (AzureRepositoryPlugin plugin = pluginWithSettingsValidation(settings)) { final AzureStorageService azureStorageService = plugin.azureStoreService.get(); AzureStorageSettings azureStorageSettings = azureStorageService.storageSettings.get("azure1"); - RequestRetryOptions retryOptions = - azureStorageService.getRetryOptions(LocationMode.PRIMARY_ONLY, azureStorageSettings); + RequestRetryOptions retryOptions = azureStorageService.getRetryOptions(LocationMode.PRIMARY_ONLY, azureStorageSettings); assertThat(retryOptions.getTryTimeout(), equalTo(1)); } } @@ -349,8 +344,7 @@ public void testTimeoutConfiguration() throws Exception { try (AzureRepositoryPlugin plugin = pluginWithSettingsValidation(settings)) { final AzureStorageService azureStorageService = plugin.azureStoreService.get(); AzureStorageSettings azureStorageSettings = azureStorageService.storageSettings.get("azure1"); - RequestRetryOptions retryOptions = - azureStorageService.getRetryOptions(LocationMode.PRIMARY_ONLY, azureStorageSettings); + RequestRetryOptions retryOptions = azureStorageService.getRetryOptions(LocationMode.PRIMARY_ONLY, azureStorageSettings); assertThat(retryOptions.getTryTimeout(), equalTo(200)); } } @@ -360,8 +354,8 @@ public void testRetryConfigurationForSecondaryFallbackLocationMode() throws Exce if (randomBoolean()) { endpoint = "core.windows.net"; } else { - endpoint = "ignored;BlobEndpoint=https://myaccount1.blob.core.windows.net;" + - "BlobSecondaryEndpoint=https://myaccount1-secondary.blob.core.windows.net"; + endpoint = "ignored;BlobEndpoint=https://myaccount1.blob.core.windows.net;" + + "BlobSecondaryEndpoint=https://myaccount1-secondary.blob.core.windows.net"; } final Settings settings = Settings.builder() @@ -372,8 +366,10 @@ public void testRetryConfigurationForSecondaryFallbackLocationMode() throws Exce try (AzureRepositoryPlugin plugin = pluginWithSettingsValidation(settings)) { final AzureStorageService azureStorageService = plugin.azureStoreService.get(); AzureStorageSettings azureStorageSettings = azureStorageService.storageSettings.get("azure1"); - RequestRetryOptions retryOptions = - azureStorageService.getRetryOptions(LocationMode.PRIMARY_THEN_SECONDARY, azureStorageSettings); + RequestRetryOptions retryOptions = azureStorageService.getRetryOptions( + LocationMode.PRIMARY_THEN_SECONDARY, + azureStorageSettings + ); assertThat(retryOptions.getSecondaryHost(), equalTo("https://myaccount1-secondary.blob.core.windows.net")); } } @@ -383,8 +379,8 @@ public void testRetryConfigurationForPrimaryFallbackLocationMode() throws Except if (randomBoolean()) { endpoint = "core.windows.net"; } else { - endpoint = "ignored;BlobEndpoint=https://myaccount1.blob.core.windows.net;" + - "BlobSecondaryEndpoint=https://myaccount1-secondary.blob.core.windows.net"; + endpoint = "ignored;BlobEndpoint=https://myaccount1.blob.core.windows.net;" + + "BlobSecondaryEndpoint=https://myaccount1-secondary.blob.core.windows.net"; } final Settings settings = Settings.builder() @@ -395,8 +391,10 @@ public void testRetryConfigurationForPrimaryFallbackLocationMode() throws Except try (AzureRepositoryPlugin plugin = pluginWithSettingsValidation(settings)) { final AzureStorageService azureStorageService = plugin.azureStoreService.get(); AzureStorageSettings azureStorageSettings = azureStorageService.storageSettings.get("azure1"); - RequestRetryOptions retryOptions = - azureStorageService.getRetryOptions(LocationMode.SECONDARY_THEN_PRIMARY, azureStorageSettings); + RequestRetryOptions retryOptions = azureStorageService.getRetryOptions( + LocationMode.SECONDARY_THEN_PRIMARY, + azureStorageSettings + ); assertThat(retryOptions.getSecondaryHost(), equalTo("https://myaccount1.blob.core.windows.net")); } } @@ -411,8 +409,7 @@ public void testRetryConfigurationForLocationModeWithoutFallback() throws Except final AzureStorageService azureStorageService = plugin.azureStoreService.get(); AzureStorageSettings azureStorageSettings = azureStorageService.storageSettings.get("azure1"); LocationMode locationMode = randomFrom(LocationMode.PRIMARY_ONLY, LocationMode.SECONDARY_ONLY); - RequestRetryOptions retryOptions = - azureStorageService.getRetryOptions(locationMode, azureStorageSettings); + RequestRetryOptions retryOptions = azureStorageService.getRetryOptions(locationMode, azureStorageSettings); assertThat(retryOptions.getSecondaryHost(), equalTo(null)); } @@ -429,12 +426,13 @@ public void testInvalidSettingsRetryConfigurationForLocationModeWithSecondaryFal final AzureStorageService azureStorageService = plugin.azureStoreService.get(); AzureStorageSettings azureStorageSettings = azureStorageService.storageSettings.get("azure1"); - expectThrows(IllegalArgumentException.class, - () -> azureStorageService.getRetryOptions(LocationMode.PRIMARY_THEN_SECONDARY, azureStorageSettings)); + expectThrows( + IllegalArgumentException.class, + () -> azureStorageService.getRetryOptions(LocationMode.PRIMARY_THEN_SECONDARY, azureStorageSettings) + ); } } - private static MockSecureSettings buildSecureSettings() { final MockSecureSettings secureSettings = new MockSecureSettings(); secureSettings.setString("azure.client.azure1.account", "myaccount1"); diff --git a/plugins/repository-azure/src/test/java/org/elasticsearch/repositories/azure/CancellableRateLimitedFluxIteratorTests.java b/plugins/repository-azure/src/test/java/org/elasticsearch/repositories/azure/CancellableRateLimitedFluxIteratorTests.java index 7c43fc443ad57..56b9c85005159 100644 --- a/plugins/repository-azure/src/test/java/org/elasticsearch/repositories/azure/CancellableRateLimitedFluxIteratorTests.java +++ b/plugins/repository-azure/src/test/java/org/elasticsearch/repositories/azure/CancellableRateLimitedFluxIteratorTests.java @@ -8,6 +8,8 @@ package org.elasticsearch.repositories.azure; +import reactor.core.publisher.Flux; + import org.elasticsearch.test.ESTestCase; import org.elasticsearch.threadpool.TestThreadPool; import org.elasticsearch.threadpool.ThreadPool; @@ -15,7 +17,6 @@ import org.junit.Before; import org.reactivestreams.Publisher; import org.reactivestreams.Subscription; -import reactor.core.publisher.Flux; import java.io.IOException; import java.util.ArrayList; @@ -45,12 +46,10 @@ public void tearDownThreadPool() { public void testConsumeAllElements() { Set cleanedElements = new HashSet<>(); - CancellableRateLimitedFluxIterator iterator = - new CancellableRateLimitedFluxIterator<>(2, cleanedElements::add); + CancellableRateLimitedFluxIterator iterator = new CancellableRateLimitedFluxIterator<>(2, cleanedElements::add); List numbers = randomList(randomIntBetween(1, 20), ESTestCase::randomInt); - Flux.fromIterable(numbers) - .subscribe(iterator); + Flux.fromIterable(numbers).subscribe(iterator); int consumedElements = 0; while (iterator.hasNext()) { @@ -68,8 +67,10 @@ public void testConsumeAllElements() { public void testItRequestsUpstreamInBatches() { final int elementsPerBatch = randomIntBetween(4, 10); final Set cleanedElements = new HashSet<>(); - final CancellableRateLimitedFluxIterator iterator = - new CancellableRateLimitedFluxIterator<>(elementsPerBatch, cleanedElements::add); + final CancellableRateLimitedFluxIterator iterator = new CancellableRateLimitedFluxIterator<>( + elementsPerBatch, + cleanedElements::add + ); final int providedElements = randomIntBetween(0, elementsPerBatch - 1); Publisher publisher = s -> runOnNewThread(() -> { @@ -133,15 +134,14 @@ public void cancel() { }); Set cleaning = new HashSet<>(); - CancellableRateLimitedFluxIterator iterator = - new CancellableRateLimitedFluxIterator<>(2, cleaning::add); + CancellableRateLimitedFluxIterator iterator = new CancellableRateLimitedFluxIterator<>(2, cleaning::add); publisher.subscribe(iterator); assertThat(iterator.hasNext(), equalTo(true)); assertThat(iterator.next(), equalTo(1)); latch.countDown(); - //noinspection ResultOfMethodCallIgnored + // noinspection ResultOfMethodCallIgnored assertBusy(() -> expectThrows(RuntimeException.class, iterator::hasNext)); assertThat(cleaning, equalTo(Set.of(2))); assertThat(iterator.getQueue(), is(empty())); @@ -154,6 +154,7 @@ public void testCancellation() throws Exception { Publisher publisher = s -> runOnNewThread(() -> { s.onSubscribe(new Subscription() { final CountDownLatch cancellationLatch = new CountDownLatch(1); + @Override public void request(long n) { assertThat(n, equalTo((long) requestedElements)); @@ -165,7 +166,6 @@ public void request(long n) { assert false; } - runOnNewThread(() -> { // It's possible that extra elements are emitted after cancellation s.onNext(3); @@ -183,8 +183,10 @@ public void cancel() { }); Set cleanedElements = new HashSet<>(); - CancellableRateLimitedFluxIterator iterator = - new CancellableRateLimitedFluxIterator<>(requestedElements, cleanedElements::add); + CancellableRateLimitedFluxIterator iterator = new CancellableRateLimitedFluxIterator<>( + requestedElements, + cleanedElements::add + ); publisher.subscribe(iterator); assertThat(iterator.hasNext(), equalTo(true)); @@ -215,7 +217,6 @@ public void request(long n) { assert false; } - runOnNewThread(() -> { // It's still possible that an error is emitted after cancelling the subscription s.onNext(3); @@ -232,15 +233,17 @@ public void cancel() { }); Set cleanedElements = new HashSet<>(); - CancellableRateLimitedFluxIterator iterator = - new CancellableRateLimitedFluxIterator<>(requestedElements, cleanedElements::add); + CancellableRateLimitedFluxIterator iterator = new CancellableRateLimitedFluxIterator<>( + requestedElements, + cleanedElements::add + ); publisher.subscribe(iterator); assertThat(iterator.hasNext(), equalTo(true)); assertThat(iterator.next(), equalTo(1)); assertThat(iterator.next(), equalTo(2)); iterator.cancel(); - //noinspection ResultOfMethodCallIgnored + // noinspection ResultOfMethodCallIgnored assertBusy(() -> expectThrows(RuntimeException.class, iterator::hasNext)); assertBusy(() -> assertThat(cleanedElements, equalTo(Set.of(3)))); assertThat(iterator.getQueue(), is(empty())); diff --git a/plugins/repository-azure/src/yamlRestTest/java/org/elasticsearch/repositories/azure/RepositoryAzureClientYamlTestSuiteIT.java b/plugins/repository-azure/src/yamlRestTest/java/org/elasticsearch/repositories/azure/RepositoryAzureClientYamlTestSuiteIT.java index 974501383efef..e9db99d19653d 100644 --- a/plugins/repository-azure/src/yamlRestTest/java/org/elasticsearch/repositories/azure/RepositoryAzureClientYamlTestSuiteIT.java +++ b/plugins/repository-azure/src/yamlRestTest/java/org/elasticsearch/repositories/azure/RepositoryAzureClientYamlTestSuiteIT.java @@ -10,6 +10,7 @@ import com.carrotsearch.randomizedtesting.annotations.Name; import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; + import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate; import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase; @@ -24,4 +25,3 @@ public static Iterable parameters() throws Exception { return ESClientYamlSuiteTestCase.createParameters(); } } - diff --git a/plugins/repository-gcs/src/internalClusterTest/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageBlobStoreRepositoryTests.java b/plugins/repository-gcs/src/internalClusterTest/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageBlobStoreRepositoryTests.java index 4fc328898e33e..4ef9fa2d03002 100644 --- a/plugins/repository-gcs/src/internalClusterTest/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageBlobStoreRepositoryTests.java +++ b/plugins/repository-gcs/src/internalClusterTest/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageBlobStoreRepositoryTests.java @@ -8,21 +8,22 @@ package org.elasticsearch.repositories.gcs; +import fixture.gcs.FakeOAuth2HttpHandler; +import fixture.gcs.GoogleCloudStorageHttpHandler; + import com.google.api.gax.retrying.RetrySettings; import com.google.cloud.http.HttpTransportOptions; import com.google.cloud.storage.StorageOptions; import com.sun.net.httpserver.Headers; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; -import fixture.gcs.FakeOAuth2HttpHandler; -import fixture.gcs.GoogleCloudStorageHttpHandler; + import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.BytesRefBuilder; import org.elasticsearch.action.ActionRunnable; import org.elasticsearch.action.support.PlainActionFuture; import org.elasticsearch.cluster.metadata.RepositoryMetadata; import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.common.blobstore.BlobContainer; import org.elasticsearch.common.blobstore.BlobPath; import org.elasticsearch.common.blobstore.BlobStore; @@ -35,7 +36,7 @@ import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.common.util.BigArrays; -import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.env.Environment; import org.elasticsearch.indices.recovery.RecoverySettings; import org.elasticsearch.plugins.Plugin; @@ -43,6 +44,7 @@ import org.elasticsearch.repositories.Repository; import org.elasticsearch.repositories.blobstore.BlobStoreRepository; import org.elasticsearch.repositories.blobstore.ESMockAPIBasedRepositoryIntegTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.threeten.bp.Duration; import java.io.IOException; @@ -72,9 +74,9 @@ protected String repositoryType() { @Override protected Settings repositorySettings(String repoName) { Settings.Builder settingsBuilder = Settings.builder() - .put(super.repositorySettings(repoName)) - .put(BUCKET.getKey(), "bucket") - .put(CLIENT_NAME.getKey(), "test"); + .put(super.repositorySettings(repoName)) + .put(BUCKET.getKey(), "bucket") + .put(CLIENT_NAME.getKey(), "test"); if (randomBoolean()) { settingsBuilder.put(BASE_PATH.getKey(), randomFrom("test", "test/1")); } @@ -89,8 +91,10 @@ protected Collection> nodePlugins() { @Override protected Map createHttpHandlers() { return Map.of( - "/", new GoogleCloudStorageStatsCollectorHttpHandler(new GoogleCloudStorageBlobStoreHttpHandler("bucket")), - "/token", new FakeOAuth2HttpHandler() + "/", + new GoogleCloudStorageStatsCollectorHttpHandler(new GoogleCloudStorageBlobStoreHttpHandler("bucket")), + "/token", + new FakeOAuth2HttpHandler() ); } @@ -117,8 +121,18 @@ public void testDeleteSingleItem() { final String repoName = createRepository(randomRepositoryName()); final RepositoriesService repositoriesService = internalCluster().getMasterNodeInstance(RepositoriesService.class); final BlobStoreRepository repository = (BlobStoreRepository) repositoriesService.repository(repoName); - PlainActionFuture.get(f -> repository.threadPool().generic().execute(ActionRunnable.run(f, () -> - repository.blobStore().blobContainer(repository.basePath()).deleteBlobsIgnoringIfNotExists(Iterators.single("foo"))))); + PlainActionFuture.get( + f -> repository.threadPool() + .generic() + .execute( + ActionRunnable.run( + f, + () -> repository.blobStore() + .blobContainer(repository.basePath()) + .deleteBlobsIgnoringIfNotExists(Iterators.single("foo")) + ) + ) + ); } public void testChunkSize() { @@ -129,31 +143,43 @@ public void testChunkSize() { // chunk size in settings final int size = randomIntBetween(1, 100); - repositoryMetadata = new RepositoryMetadata("repo", GoogleCloudStorageRepository.TYPE, - Settings.builder().put("chunk_size", size + "mb").build()); + repositoryMetadata = new RepositoryMetadata( + "repo", + GoogleCloudStorageRepository.TYPE, + Settings.builder().put("chunk_size", size + "mb").build() + ); chunkSize = GoogleCloudStorageRepository.getSetting(GoogleCloudStorageRepository.CHUNK_SIZE, repositoryMetadata); assertEquals(new ByteSizeValue(size, ByteSizeUnit.MB), chunkSize); // zero bytes is not allowed IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> { - final RepositoryMetadata repoMetadata = new RepositoryMetadata("repo", GoogleCloudStorageRepository.TYPE, - Settings.builder().put("chunk_size", "0").build()); + final RepositoryMetadata repoMetadata = new RepositoryMetadata( + "repo", + GoogleCloudStorageRepository.TYPE, + Settings.builder().put("chunk_size", "0").build() + ); GoogleCloudStorageRepository.getSetting(GoogleCloudStorageRepository.CHUNK_SIZE, repoMetadata); }); assertEquals("failed to parse value [0] for setting [chunk_size], must be >= [1b]", e.getMessage()); // negative bytes not allowed e = expectThrows(IllegalArgumentException.class, () -> { - final RepositoryMetadata repoMetadata = new RepositoryMetadata("repo", GoogleCloudStorageRepository.TYPE, - Settings.builder().put("chunk_size", "-1").build()); + final RepositoryMetadata repoMetadata = new RepositoryMetadata( + "repo", + GoogleCloudStorageRepository.TYPE, + Settings.builder().put("chunk_size", "-1").build() + ); GoogleCloudStorageRepository.getSetting(GoogleCloudStorageRepository.CHUNK_SIZE, repoMetadata); }); assertEquals("failed to parse value [-1] for setting [chunk_size], must be >= [1b]", e.getMessage()); // greater than max chunk size not allowed e = expectThrows(IllegalArgumentException.class, () -> { - final RepositoryMetadata repoMetadata = new RepositoryMetadata("repo", GoogleCloudStorageRepository.TYPE, - Settings.builder().put("chunk_size", "6tb").build()); + final RepositoryMetadata repoMetadata = new RepositoryMetadata( + "repo", + GoogleCloudStorageRepository.TYPE, + Settings.builder().put("chunk_size", "6tb").build() + ); GoogleCloudStorageRepository.getSetting(GoogleCloudStorageRepository.CHUNK_SIZE, repoMetadata); }); assertEquals("failed to parse value [6tb] for setting [chunk_size], must be <= [5tb]", e.getMessage()); @@ -194,47 +220,68 @@ public TestGoogleCloudStoragePlugin(Settings settings) { protected GoogleCloudStorageService createStorageService() { return new GoogleCloudStorageService() { @Override - StorageOptions createStorageOptions(final GoogleCloudStorageClientSettings clientSettings, - final HttpTransportOptions httpTransportOptions) { + StorageOptions createStorageOptions( + final GoogleCloudStorageClientSettings clientSettings, + final HttpTransportOptions httpTransportOptions + ) { StorageOptions options = super.createStorageOptions(clientSettings, httpTransportOptions); return options.toBuilder() .setHost(options.getHost()) .setCredentials(options.getCredentials()) - .setRetrySettings(RetrySettings.newBuilder() - .setTotalTimeout(options.getRetrySettings().getTotalTimeout()) - .setInitialRetryDelay(Duration.ofMillis(10L)) - .setRetryDelayMultiplier(options.getRetrySettings().getRetryDelayMultiplier()) - .setMaxRetryDelay(Duration.ofSeconds(1L)) - .setMaxAttempts(0) - .setJittered(false) - .setInitialRpcTimeout(options.getRetrySettings().getInitialRpcTimeout()) - .setRpcTimeoutMultiplier(options.getRetrySettings().getRpcTimeoutMultiplier()) - .setMaxRpcTimeout(options.getRetrySettings().getMaxRpcTimeout()) - .build()) + .setRetrySettings( + RetrySettings.newBuilder() + .setTotalTimeout(options.getRetrySettings().getTotalTimeout()) + .setInitialRetryDelay(Duration.ofMillis(10L)) + .setRetryDelayMultiplier(options.getRetrySettings().getRetryDelayMultiplier()) + .setMaxRetryDelay(Duration.ofSeconds(1L)) + .setMaxAttempts(0) + .setJittered(false) + .setInitialRpcTimeout(options.getRetrySettings().getInitialRpcTimeout()) + .setRpcTimeoutMultiplier(options.getRetrySettings().getRpcTimeoutMultiplier()) + .setMaxRpcTimeout(options.getRetrySettings().getMaxRpcTimeout()) + .build() + ) .build(); } }; } @Override - public Map getRepositories(Environment env, NamedXContentRegistry registry, - ClusterService clusterService, BigArrays bigArrays, - RecoverySettings recoverySettings) { - return Collections.singletonMap(GoogleCloudStorageRepository.TYPE, - metadata -> new GoogleCloudStorageRepository(metadata, registry, this.storageService, clusterService, - bigArrays, recoverySettings) { + public Map getRepositories( + Environment env, + NamedXContentRegistry registry, + ClusterService clusterService, + BigArrays bigArrays, + RecoverySettings recoverySettings + ) { + return Collections.singletonMap( + GoogleCloudStorageRepository.TYPE, + metadata -> new GoogleCloudStorageRepository( + metadata, + registry, + this.storageService, + clusterService, + bigArrays, + recoverySettings + ) { @Override protected GoogleCloudStorageBlobStore createBlobStore() { return new GoogleCloudStorageBlobStore( - metadata.settings().get("bucket"), "test", metadata.name(), storageService, bigArrays, - randomIntBetween(1, 8) * 1024) { + metadata.settings().get("bucket"), + "test", + metadata.name(), + storageService, + bigArrays, + randomIntBetween(1, 8) * 1024 + ) { @Override long getLargeBlobThresholdInBytes() { return ByteSizeUnit.MB.toBytes(1); } }; } - }); + } + ); } } @@ -272,9 +319,11 @@ protected String requestUniqueId(HttpExchange exchange) { final String range = exchange.getRequestHeaders().getFirst("Content-Range"); return exchange.getRemoteAddress().getHostString() - + " " + exchange.getRequestMethod() - + " " + exchange.getRequestURI() - + (range != null ? " " + range : ""); + + " " + + exchange.getRequestMethod() + + " " + + exchange.getRequestURI() + + (range != null ? " " + range : ""); } @Override @@ -316,16 +365,14 @@ public void maybeTrack(final String request, Headers requestHeaders) { } boolean isLastPart(Headers requestHeaders) { - if (requestHeaders.containsKey("Content-range") == false) - return false; + if (requestHeaders.containsKey("Content-range") == false) return false; // https://cloud.google.com/storage/docs/json_api/v1/parameters#contentrange final String contentRange = requestHeaders.getFirst("Content-range"); final Matcher matcher = contentRangeMatcher.matcher(contentRange); - if (matcher.matches() == false) - return false; + if (matcher.matches() == false) return false; String upperBound = matcher.group(1); String totalLength = matcher.group(2); diff --git a/plugins/repository-gcs/src/internalClusterTest/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageThirdPartyTests.java b/plugins/repository-gcs/src/internalClusterTest/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageThirdPartyTests.java index fdb4f4b447ce4..a14a1b5b8f17e 100644 --- a/plugins/repository-gcs/src/internalClusterTest/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageThirdPartyTests.java +++ b/plugins/repository-gcs/src/internalClusterTest/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageThirdPartyTests.java @@ -51,19 +51,25 @@ protected SecureSettings credentials() { assertThat(System.getProperty("test.google.bucket"), not(blankOrNullString())); MockSecureSettings secureSettings = new MockSecureSettings(); - secureSettings.setFile("gcs.client.default.credentials_file", - Base64.getDecoder().decode(System.getProperty("test.google.account"))); + secureSettings.setFile( + "gcs.client.default.credentials_file", + Base64.getDecoder().decode(System.getProperty("test.google.account")) + ); return secureSettings; } @Override protected void createRepository(final String repoName) { - AcknowledgedResponse putRepositoryResponse = client().admin().cluster().preparePutRepository("test-repo") + AcknowledgedResponse putRepositoryResponse = client().admin() + .cluster() + .preparePutRepository("test-repo") .setType("gcs") - .setSettings(Settings.builder() - .put("bucket", System.getProperty("test.google.bucket")) - .put("base_path", System.getProperty("test.google.base", "/")) - ).get(); + .setSettings( + Settings.builder() + .put("bucket", System.getProperty("test.google.bucket")) + .put("base_path", System.getProperty("test.google.base", "/")) + ) + .get(); assertThat(putRepositoryResponse.isAcknowledged(), equalTo(true)); } } diff --git a/plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageBlobContainer.java b/plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageBlobContainer.java index ce2eae99b59f3..caf66f5b6f7fe 100644 --- a/plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageBlobContainer.java +++ b/plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageBlobContainer.java @@ -79,10 +79,8 @@ public void writeBlob(String blobName, BytesReference bytes, boolean failIfAlrea } @Override - public void writeBlob(String blobName, - boolean failIfAlreadyExists, - boolean atomic, - CheckedConsumer writer) throws IOException { + public void writeBlob(String blobName, boolean failIfAlreadyExists, boolean atomic, CheckedConsumer writer) + throws IOException { blobStore.writeBlob(buildKey(blobName), failIfAlreadyExists, writer); } diff --git a/plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageBlobStore.java b/plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageBlobStore.java index ea867a0ff1f9f..d4462547b42fe 100644 --- a/plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageBlobStore.java +++ b/plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageBlobStore.java @@ -18,14 +18,11 @@ import com.google.cloud.storage.Storage.BlobListOption; import com.google.cloud.storage.StorageBatch; import com.google.cloud.storage.StorageException; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.message.ParameterizedMessage; import org.elasticsearch.ExceptionsHelper; -import org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput; -import org.elasticsearch.common.util.BigArrays; -import org.elasticsearch.core.CheckedConsumer; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.common.blobstore.BlobContainer; import org.elasticsearch.common.blobstore.BlobMetadata; import org.elasticsearch.common.blobstore.BlobPath; @@ -36,8 +33,12 @@ import org.elasticsearch.common.collect.MapBuilder; import org.elasticsearch.common.hash.MessageDigests; import org.elasticsearch.common.io.Streams; +import org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput; import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.common.util.BigArrays; +import org.elasticsearch.core.CheckedConsumer; +import org.elasticsearch.core.SuppressForbidden; import java.io.ByteArrayInputStream; import java.io.FilterOutputStream; @@ -79,7 +80,7 @@ class GoogleCloudStorageBlobStore implements BlobStore { } else { final int largeBlobThresholdByteSize; try { - largeBlobThresholdByteSize = Integer.parseInt(largeBlobThresholdByteSizeProperty); + largeBlobThresholdByteSize = Integer.parseInt(largeBlobThresholdByteSizeProperty); } catch (final NumberFormatException e) { throw new IllegalArgumentException("failed to parse " + key + " having value [" + largeBlobThresholdByteSizeProperty + "]"); } @@ -98,12 +99,14 @@ class GoogleCloudStorageBlobStore implements BlobStore { private final int bufferSize; private final BigArrays bigArrays; - GoogleCloudStorageBlobStore(String bucketName, - String clientName, - String repositoryName, - GoogleCloudStorageService storageService, - BigArrays bigArrays, - int bufferSize) { + GoogleCloudStorageBlobStore( + String bucketName, + String clientName, + String repositoryName, + GoogleCloudStorageService storageService, + BigArrays bigArrays, + int bufferSize + ) { this.bucketName = bucketName; this.clientName = clientName; this.repositoryName = repositoryName; @@ -150,23 +153,26 @@ Map listBlobsByPrefix(String path, String prefix) throws I final String pathPrefix = buildKey(path, prefix); final MapBuilder mapBuilder = MapBuilder.newMapBuilder(); SocketAccess.doPrivilegedVoidIOException( - () -> client().list(bucketName, BlobListOption.currentDirectory(), BlobListOption.prefix(pathPrefix)).iterateAll().forEach( - blob -> { + () -> client().list(bucketName, BlobListOption.currentDirectory(), BlobListOption.prefix(pathPrefix)) + .iterateAll() + .forEach(blob -> { assert blob.getName().startsWith(path); if (blob.isDirectory() == false) { final String suffixName = blob.getName().substring(path.length()); mapBuilder.put(suffixName, new PlainBlobMetadata(suffixName, blob.getSize())); } - })); + }) + ); return mapBuilder.immutableMap(); } Map listChildren(BlobPath path) throws IOException { final String pathStr = path.buildAsString(); final MapBuilder mapBuilder = MapBuilder.newMapBuilder(); - SocketAccess.doPrivilegedVoidIOException - (() -> client().list(bucketName, BlobListOption.currentDirectory(), BlobListOption.prefix(pathStr)).iterateAll().forEach( - blob -> { + SocketAccess.doPrivilegedVoidIOException( + () -> client().list(bucketName, BlobListOption.currentDirectory(), BlobListOption.prefix(pathStr)) + .iterateAll() + .forEach(blob -> { if (blob.isDirectory()) { assert blob.getName().startsWith(pathStr); assert blob.getName().endsWith("/"); @@ -176,7 +182,8 @@ Map listChildren(BlobPath path) throws IOException { mapBuilder.put(suffixName, new GoogleCloudStorageBlobContainer(path.add(suffixName), this)); } } - })); + }) + ); return mapBuilder.immutableMap(); } @@ -220,8 +227,12 @@ InputStream readBlob(String blobName, long position, long length) throws IOExcep if (length == 0) { return new ByteArrayInputStream(new byte[0]); } else { - return new GoogleCloudStorageRetryingInputStream(client(), BlobId.of(bucketName, blobName), position, - Math.addExact(position, length - 1)); + return new GoogleCloudStorageRetryingInputStream( + client(), + BlobId.of(bucketName, blobName), + position, + Math.addExact(position, length - 1) + ); } } @@ -236,8 +247,12 @@ void writeBlob(String blobName, BytesReference bytes, boolean failIfAlreadyExist // This is needed since we rely on atomic write behavior when writing BytesReferences in BlobStoreRepository which is not // guaranteed for resumable uploads. final String md5 = Base64.getEncoder().encodeToString(MessageDigests.digest(bytes, MessageDigests.md5())); - writeBlobResumable(BlobInfo.newBuilder(bucketName, blobName).setMd5(md5).build(), bytes.streamInput(), bytes.length(), - failIfAlreadyExists); + writeBlobResumable( + BlobInfo.newBuilder(bucketName, blobName).setMd5(md5).build(), + bytes.streamInput(), + bytes.length(), + failIfAlreadyExists + ); } else { final BlobInfo blobInfo = BlobInfo.newBuilder(bucketName, blobName).build(); if (bytes.hasArray()) { @@ -274,11 +289,12 @@ long getLargeBlobThresholdInBytes() { } // possible options for #writeBlobResumable uploads - private static final Storage.BlobWriteOption[] NO_OVERWRITE_NO_MD5 = {Storage.BlobWriteOption.doesNotExist()}; + private static final Storage.BlobWriteOption[] NO_OVERWRITE_NO_MD5 = { Storage.BlobWriteOption.doesNotExist() }; private static final Storage.BlobWriteOption[] OVERWRITE_NO_MD5 = new Storage.BlobWriteOption[0]; - private static final Storage.BlobWriteOption[] NO_OVERWRITE_CHECK_MD5 = - {Storage.BlobWriteOption.doesNotExist(), Storage.BlobWriteOption.md5Match()}; - private static final Storage.BlobWriteOption[] OVERWRITE_CHECK_MD5 = {Storage.BlobWriteOption.md5Match()}; + private static final Storage.BlobWriteOption[] NO_OVERWRITE_CHECK_MD5 = { + Storage.BlobWriteOption.doesNotExist(), + Storage.BlobWriteOption.md5Match() }; + private static final Storage.BlobWriteOption[] OVERWRITE_CHECK_MD5 = { Storage.BlobWriteOption.md5Match() }; void writeBlob(String blobName, boolean failIfAlreadyExists, CheckedConsumer writer) throws IOException { final BlobInfo blobInfo = BlobInfo.newBuilder(bucketName, blobName).build(); @@ -325,8 +341,9 @@ public void write(byte[] b, int off, int len) throws IOException { } private void initResumableStream() throws IOException { - final WriteChannel writeChannel = - SocketAccess.doPrivilegedIOException(() -> client().writer(blobInfo, writeOptions)); + final WriteChannel writeChannel = SocketAccess.doPrivilegedIOException( + () -> client().writer(blobInfo, writeOptions) + ); channelRef.set(writeChannel); resumableStream = new FilterOutputStream(Channels.newOutputStream(new WritableBlobChannel(writeChannel))) { @Override @@ -381,8 +398,7 @@ public void write(byte[] b, int off, int len) throws IOException { * @param size expected size of the blob to be written * @param failIfAlreadyExists whether to throw a FileAlreadyExistsException if the given blob already exists */ - private void writeBlobResumable(BlobInfo blobInfo, InputStream inputStream, long size, boolean failIfAlreadyExists) - throws IOException { + private void writeBlobResumable(BlobInfo blobInfo, InputStream inputStream, long size, boolean failIfAlreadyExists) throws IOException { // We retry 410 GONE errors to cover the unlikely but possible scenario where a resumable upload session becomes broken and // needs to be restarted from scratch. Given how unlikely a 410 error should be according to SLAs we retry only twice. assert inputStream.markSupported(); @@ -405,7 +421,10 @@ private void writeBlobResumable(BlobInfo blobInfo, InputStream inputStream, long * is in the stacktrace and is not granted the permissions needed to close and write the channel. */ org.elasticsearch.core.internal.io.Streams.copy( - inputStream, Channels.newOutputStream(new WritableBlobChannel(writeChannel)), buffer); + inputStream, + Channels.newOutputStream(new WritableBlobChannel(writeChannel)), + buffer + ); SocketAccess.doPrivilegedVoidIOException(writeChannel::close); // We don't track this operation on the http layer as // we do with the GET/LIST operations since this operations @@ -448,11 +467,10 @@ private void writeBlobMultipart(BlobInfo blobInfo, byte[] buffer, int offset, in throws IOException { assert blobSize <= getLargeBlobThresholdInBytes() : "large blob uploads should use the resumable upload method"; try { - final Storage.BlobTargetOption[] targetOptions = failIfAlreadyExists ? - new Storage.BlobTargetOption[] { Storage.BlobTargetOption.doesNotExist() } : - new Storage.BlobTargetOption[0]; - SocketAccess.doPrivilegedVoidIOException( - () -> client().create(blobInfo, buffer, offset, blobSize, targetOptions)); + final Storage.BlobTargetOption[] targetOptions = failIfAlreadyExists + ? new Storage.BlobTargetOption[] { Storage.BlobTargetOption.doesNotExist() } + : new Storage.BlobTargetOption[0]; + SocketAccess.doPrivilegedVoidIOException(() -> client().create(blobInfo, buffer, offset, blobSize, targetOptions)); // We don't track this operation on the http layer as // we do with the GET/LIST operations since this operations // can trigger multiple underlying http requests but only one @@ -527,25 +545,23 @@ public BlobId next() { final StorageBatch batch = client().batch(); while (blobIdsToDelete.hasNext()) { BlobId blob = blobIdsToDelete.next(); - batch.delete(blob).notify( - new BatchResult.Callback<>() { - @Override - public void success(Boolean result) { - } - - @Override - public void error(StorageException exception) { - if (exception.getCode() != HTTP_NOT_FOUND) { - // track up to 10 failed blob deletions for the exception message below - if (failedBlobs.size() < 10) { - failedBlobs.add(blob); - } - if (ioe.compareAndSet(null, exception) == false) { - ioe.get().addSuppressed(exception); - } + batch.delete(blob).notify(new BatchResult.Callback<>() { + @Override + public void success(Boolean result) {} + + @Override + public void error(StorageException exception) { + if (exception.getCode() != HTTP_NOT_FOUND) { + // track up to 10 failed blob deletions for the exception message below + if (failedBlobs.size() < 10) { + failedBlobs.add(blob); + } + if (ioe.compareAndSet(null, exception) == false) { + ioe.get().addSuppressed(exception); } } - }); + } + }); } batch.submit(); diff --git a/plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageClientSettings.java b/plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageClientSettings.java index f1f5836b1a8a9..09ba4aed4a4e3 100644 --- a/plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageClientSettings.java +++ b/plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageClientSettings.java @@ -36,38 +36,59 @@ public class GoogleCloudStorageClientSettings { private static final String PREFIX = "gcs.client."; /** A json Service Account file loaded from secure settings. */ - static final Setting.AffixSetting CREDENTIALS_FILE_SETTING = Setting.affixKeySetting(PREFIX, "credentials_file", - key -> SecureSetting.secureFile(key, null)); + static final Setting.AffixSetting CREDENTIALS_FILE_SETTING = Setting.affixKeySetting( + PREFIX, + "credentials_file", + key -> SecureSetting.secureFile(key, null) + ); /** An override for the Storage endpoint to connect to. */ - static final Setting.AffixSetting ENDPOINT_SETTING = Setting.affixKeySetting(PREFIX, "endpoint", - key -> Setting.simpleString(key, Setting.Property.NodeScope)); + static final Setting.AffixSetting ENDPOINT_SETTING = Setting.affixKeySetting( + PREFIX, + "endpoint", + key -> Setting.simpleString(key, Setting.Property.NodeScope) + ); /** An override for the Google Project ID. */ - static final Setting.AffixSetting PROJECT_ID_SETTING = Setting.affixKeySetting(PREFIX, "project_id", - key -> Setting.simpleString(key, Setting.Property.NodeScope)); + static final Setting.AffixSetting PROJECT_ID_SETTING = Setting.affixKeySetting( + PREFIX, + "project_id", + key -> Setting.simpleString(key, Setting.Property.NodeScope) + ); /** An override for the Token Server URI in the oauth flow. */ - static final Setting.AffixSetting TOKEN_URI_SETTING = Setting.affixKeySetting(PREFIX, "token_uri", - key -> new Setting<>(key, "", URI::create, Setting.Property.NodeScope)); + static final Setting.AffixSetting TOKEN_URI_SETTING = Setting.affixKeySetting( + PREFIX, + "token_uri", + key -> new Setting<>(key, "", URI::create, Setting.Property.NodeScope) + ); /** * The timeout to establish a connection. A value of {@code -1} corresponds to an infinite timeout. A value of {@code 0} * corresponds to the default timeout of the Google Cloud Storage Java Library. */ - static final Setting.AffixSetting CONNECT_TIMEOUT_SETTING = Setting.affixKeySetting(PREFIX, "connect_timeout", - key -> timeSetting(key, TimeValue.ZERO, TimeValue.MINUS_ONE, Setting.Property.NodeScope)); + static final Setting.AffixSetting CONNECT_TIMEOUT_SETTING = Setting.affixKeySetting( + PREFIX, + "connect_timeout", + key -> timeSetting(key, TimeValue.ZERO, TimeValue.MINUS_ONE, Setting.Property.NodeScope) + ); /** * The timeout to read data from an established connection. A value of {@code -1} corresponds to an infinite timeout. A value of * {@code 0} corresponds to the default timeout of the Google Cloud Storage Java Library. */ - static final Setting.AffixSetting READ_TIMEOUT_SETTING = Setting.affixKeySetting(PREFIX, "read_timeout", - key -> timeSetting(key, TimeValue.ZERO, TimeValue.MINUS_ONE, Setting.Property.NodeScope)); + static final Setting.AffixSetting READ_TIMEOUT_SETTING = Setting.affixKeySetting( + PREFIX, + "read_timeout", + key -> timeSetting(key, TimeValue.ZERO, TimeValue.MINUS_ONE, Setting.Property.NodeScope) + ); /** Name used by the client when it uses the Google Cloud JSON API. */ - static final Setting.AffixSetting APPLICATION_NAME_SETTING = Setting.affixKeySetting(PREFIX, "application_name", - key -> new Setting<>(key, "repository-gcs", Function.identity(), Setting.Property.NodeScope, Setting.Property.Deprecated)); + static final Setting.AffixSetting APPLICATION_NAME_SETTING = Setting.affixKeySetting( + PREFIX, + "application_name", + key -> new Setting<>(key, "repository-gcs", Function.identity(), Setting.Property.NodeScope, Setting.Property.Deprecated) + ); /** The credentials used by the client to connect to the Storage endpoint. */ private final ServiceAccountCredentials credential; @@ -90,13 +111,15 @@ public class GoogleCloudStorageClientSettings { /** The token server URI. This leases access tokens in the oauth flow. */ private final URI tokenUri; - GoogleCloudStorageClientSettings(final ServiceAccountCredentials credential, - final String endpoint, - final String projectId, - final TimeValue connectTimeout, - final TimeValue readTimeout, - final String applicationName, - final URI tokenUri) { + GoogleCloudStorageClientSettings( + final ServiceAccountCredentials credential, + final String endpoint, + final String projectId, + final TimeValue connectTimeout, + final TimeValue readTimeout, + final String applicationName, + final URI tokenUri + ) { this.credential = credential; this.endpoint = endpoint; this.projectId = projectId; @@ -136,7 +159,7 @@ public URI getTokenUri() { public static Map load(final Settings settings) { final Map clients = new HashMap<>(); - for (final String clientName: settings.getGroups(PREFIX).keySet()) { + for (final String clientName : settings.getGroups(PREFIX).keySet()) { clients.put(clientName, getClientSettings(settings, clientName)); } if (clients.containsKey("default") == false) { diff --git a/plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageHttpStatsCollector.java b/plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageHttpStatsCollector.java index c9ab15e35b307..ce36827afe77f 100644 --- a/plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageHttpStatsCollector.java +++ b/plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageHttpStatsCollector.java @@ -25,20 +25,22 @@ final class GoogleCloudStorageHttpStatsCollector implements HttpResponseInterceptor { // The specification for the current API (v1) endpoints can be found at: // https://cloud.google.com/storage/docs/json_api/v1 - private static final List> trackerFactories = - List.of( - (bucket) -> - HttpRequestTracker.get(format(Locale.ROOT, "/download/storage/v1/b/%s/o/.+", bucket), - GoogleCloudStorageOperationsStats::trackGetOperation), - - (bucket) -> - HttpRequestTracker.get(format(Locale.ROOT, "/storage/v1/b/%s/o/.+", bucket), - GoogleCloudStorageOperationsStats::trackGetOperation), - - (bucket) -> - HttpRequestTracker.get(format(Locale.ROOT, "/storage/v1/b/%s/o", bucket), - GoogleCloudStorageOperationsStats::trackListOperation) - ); + private static final List> trackerFactories = List.of( + (bucket) -> HttpRequestTracker.get( + format(Locale.ROOT, "/download/storage/v1/b/%s/o/.+", bucket), + GoogleCloudStorageOperationsStats::trackGetOperation + ), + + (bucket) -> HttpRequestTracker.get( + format(Locale.ROOT, "/storage/v1/b/%s/o/.+", bucket), + GoogleCloudStorageOperationsStats::trackGetOperation + ), + + (bucket) -> HttpRequestTracker.get( + format(Locale.ROOT, "/storage/v1/b/%s/o", bucket), + GoogleCloudStorageOperationsStats::trackListOperation + ) + ); private final GoogleCloudStorageOperationsStats gcsOperationStats; private final List trackers; @@ -53,8 +55,7 @@ final class GoogleCloudStorageHttpStatsCollector implements HttpResponseIntercep @Override public void interceptResponse(final HttpResponse response) { // TODO keep track of unsuccessful requests in different entries - if (response.isSuccessStatusCode() == false) - return; + if (response.isSuccessStatusCode() == false) return; final HttpRequest request = response.getRequest(); for (HttpRequestTracker tracker : trackers) { @@ -78,16 +79,17 @@ private static final class HttpRequestTracker { private final Pattern pathPattern; private final Consumer statsTracker; - private HttpRequestTracker(final String method, - final String pathPattern, - final Consumer statsTracker) { + private HttpRequestTracker( + final String method, + final String pathPattern, + final Consumer statsTracker + ) { this.method = method; this.pathPattern = Pattern.compile(pathPattern); this.statsTracker = statsTracker; } - private static HttpRequestTracker get(final String pathPattern, - final Consumer statsConsumer) { + private static HttpRequestTracker get(final String pathPattern, final Consumer statsConsumer) { return new HttpRequestTracker("GET", pathPattern, statsConsumer); } @@ -100,16 +102,14 @@ private static HttpRequestTracker get(final String pathPattern, * @return {@code true} if the http request was tracked, {@code false} otherwise. */ private boolean track(final HttpRequest httpRequest, final GoogleCloudStorageOperationsStats stats) { - if (matchesCriteria(httpRequest) == false) - return false; + if (matchesCriteria(httpRequest) == false) return false; statsTracker.accept(stats); return true; } private boolean matchesCriteria(final HttpRequest httpRequest) { - return method.equalsIgnoreCase(httpRequest.getRequestMethod()) && - pathMatches(httpRequest.getUrl()); + return method.equalsIgnoreCase(httpRequest.getRequestMethod()) && pathMatches(httpRequest.getUrl()); } private boolean pathMatches(final GenericUrl url) { diff --git a/plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStoragePlugin.java b/plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStoragePlugin.java index d25ae6b304fd9..d1217e83496bc 100644 --- a/plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStoragePlugin.java +++ b/plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStoragePlugin.java @@ -12,13 +12,13 @@ import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.BigArrays; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.env.Environment; import org.elasticsearch.indices.recovery.RecoverySettings; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.plugins.ReloadablePlugin; import org.elasticsearch.plugins.RepositoryPlugin; import org.elasticsearch.repositories.Repository; +import org.elasticsearch.xcontent.NamedXContentRegistry; import java.util.Arrays; import java.util.Collections; @@ -42,12 +42,24 @@ protected GoogleCloudStorageService createStorageService() { } @Override - public Map getRepositories(Environment env, NamedXContentRegistry namedXContentRegistry, - ClusterService clusterService, BigArrays bigArrays, - RecoverySettings recoverySettings) { - return Collections.singletonMap(GoogleCloudStorageRepository.TYPE, - metadata -> new GoogleCloudStorageRepository(metadata, namedXContentRegistry, this.storageService, clusterService, bigArrays, - recoverySettings)); + public Map getRepositories( + Environment env, + NamedXContentRegistry namedXContentRegistry, + ClusterService clusterService, + BigArrays bigArrays, + RecoverySettings recoverySettings + ) { + return Collections.singletonMap( + GoogleCloudStorageRepository.TYPE, + metadata -> new GoogleCloudStorageRepository( + metadata, + namedXContentRegistry, + this.storageService, + clusterService, + bigArrays, + recoverySettings + ) + ); } @Override @@ -59,7 +71,8 @@ public List> getSettings() { GoogleCloudStorageClientSettings.CONNECT_TIMEOUT_SETTING, GoogleCloudStorageClientSettings.READ_TIMEOUT_SETTING, GoogleCloudStorageClientSettings.APPLICATION_NAME_SETTING, - GoogleCloudStorageClientSettings.TOKEN_URI_SETTING); + GoogleCloudStorageClientSettings.TOKEN_URI_SETTING + ); } @Override diff --git a/plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageRepository.java b/plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageRepository.java index 3b0125fc31e67..11c1080da5d11 100644 --- a/plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageRepository.java +++ b/plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageRepository.java @@ -18,10 +18,10 @@ import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.common.util.BigArrays; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.indices.recovery.RecoverySettings; import org.elasticsearch.repositories.RepositoryException; import org.elasticsearch.repositories.blobstore.MeteredBlobStoreRepository; +import org.elasticsearch.xcontent.NamedXContentRegistry; import java.util.Map; import java.util.function.Function; @@ -44,12 +44,16 @@ class GoogleCloudStorageRepository extends MeteredBlobStoreRepository { static final String TYPE = "gcs"; - static final Setting BUCKET = - simpleString("bucket", Property.NodeScope, Property.Dynamic); - static final Setting BASE_PATH = - simpleString("base_path", Property.NodeScope, Property.Dynamic); - static final Setting CHUNK_SIZE = - byteSizeSetting("chunk_size", MAX_CHUNK_SIZE, MIN_CHUNK_SIZE, MAX_CHUNK_SIZE, Property.NodeScope, Property.Dynamic); + static final Setting BUCKET = simpleString("bucket", Property.NodeScope, Property.Dynamic); + static final Setting BASE_PATH = simpleString("base_path", Property.NodeScope, Property.Dynamic); + static final Setting CHUNK_SIZE = byteSizeSetting( + "chunk_size", + MAX_CHUNK_SIZE, + MIN_CHUNK_SIZE, + MAX_CHUNK_SIZE, + Property.NodeScope, + Property.Dynamic + ); static final Setting CLIENT_NAME = new Setting<>("client", "default", Function.identity()); private final GoogleCloudStorageService storageService; @@ -63,16 +67,23 @@ class GoogleCloudStorageRepository extends MeteredBlobStoreRepository { final GoogleCloudStorageService storageService, final ClusterService clusterService, final BigArrays bigArrays, - final RecoverySettings recoverySettings) { - super(metadata, namedXContentRegistry, clusterService, bigArrays, recoverySettings, buildBasePath(metadata), - buildLocation(metadata)); + final RecoverySettings recoverySettings + ) { + super( + metadata, + namedXContentRegistry, + clusterService, + bigArrays, + recoverySettings, + buildBasePath(metadata), + buildLocation(metadata) + ); this.storageService = storageService; this.chunkSize = getSetting(CHUNK_SIZE, metadata); this.bucket = getSetting(BUCKET, metadata); this.clientName = CLIENT_NAME.get(metadata.settings()); - logger.debug( - "using bucket [{}], base_path [{}], chunk_size [{}], compress [{}]", bucket, basePath(), chunkSize, isCompress()); + logger.debug("using bucket [{}], base_path [{}], chunk_size [{}], compress [{}]", bucket, basePath(), chunkSize, isCompress()); } private static BlobPath buildBasePath(RepositoryMetadata metadata) { @@ -89,8 +100,7 @@ private static BlobPath buildBasePath(RepositoryMetadata metadata) { } private static Map buildLocation(RepositoryMetadata metadata) { - return Map.of("base_path", BASE_PATH.get(metadata.settings()), - "bucket", getSetting(BUCKET, metadata)); + return Map.of("base_path", BASE_PATH.get(metadata.settings()), "bucket", getSetting(BUCKET, metadata)); } @Override diff --git a/plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageRetryingInputStream.java b/plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageRetryingInputStream.java index 9e236bdeae7d9..0016373e3b53d 100644 --- a/plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageRetryingInputStream.java +++ b/plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageRetryingInputStream.java @@ -15,6 +15,7 @@ import com.google.cloud.storage.Storage; import com.google.cloud.storage.StorageException; import com.google.cloud.storage.spi.v1.HttpStorageRpc; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.message.ParameterizedMessage; @@ -101,33 +102,34 @@ private InputStream openStream() throws IOException { try { try { return RetryHelper.runWithRetries(() -> { - try { - return SocketAccess.doPrivilegedIOException(() -> { - final Get get = storage.objects().get(blobId.getBucket(), blobId.getName()); - get.setReturnRawInputStream(true); + try { + return SocketAccess.doPrivilegedIOException(() -> { + final Get get = storage.objects().get(blobId.getBucket(), blobId.getName()); + get.setReturnRawInputStream(true); - if (currentOffset > 0 || start > 0 || end < Long.MAX_VALUE - 1) { - get.getRequestHeaders().setRange("bytes=" + Math.addExact(start, currentOffset) + "-" + end); - } - final HttpResponse resp = get.executeMedia(); - final Long contentLength = resp.getHeaders().getContentLength(); - InputStream content = resp.getContent(); - if (contentLength != null) { - content = new ContentLengthValidatingInputStream(content, contentLength); - } - return content; - }); - } catch (IOException e) { - throw StorageException.translate(e); - } - }, client.getOptions().getRetrySettings(), BaseService.EXCEPTION_HANDLER, client.getOptions().getClock()); + if (currentOffset > 0 || start > 0 || end < Long.MAX_VALUE - 1) { + get.getRequestHeaders().setRange("bytes=" + Math.addExact(start, currentOffset) + "-" + end); + } + final HttpResponse resp = get.executeMedia(); + final Long contentLength = resp.getHeaders().getContentLength(); + InputStream content = resp.getContent(); + if (contentLength != null) { + content = new ContentLengthValidatingInputStream(content, contentLength); + } + return content; + }); + } catch (IOException e) { + throw StorageException.translate(e); + } + }, client.getOptions().getRetrySettings(), BaseService.EXCEPTION_HANDLER, client.getOptions().getClock()); } catch (RetryHelper.RetryHelperException e) { throw StorageException.translateAndThrow(e); } } catch (StorageException e) { if (e.getCode() == 404) { throw addSuppressedExceptions( - new NoSuchFileException("Blob object [" + blobId.getName() + "] not found: " + e.getMessage())); + new NoSuchFileException("Blob object [" + blobId.getName() + "] not found: " + e.getMessage()) + ); } throw addSuppressedExceptions(e); } @@ -224,8 +226,16 @@ private void reopenStreamOrFail(StorageException e) throws IOException { if (attempt >= maxAttempts) { throw addSuppressedExceptions(e); } - logger.debug(new ParameterizedMessage("failed reading [{}] at offset [{}], attempt [{}] of [{}], retrying", - blobId, currentOffset, attempt, maxAttempts), e); + logger.debug( + new ParameterizedMessage( + "failed reading [{}] at offset [{}], attempt [{}] of [{}], retrying", + blobId, + currentOffset, + attempt, + maxAttempts + ), + e + ); attempt += 1; if (failures.size() < MAX_SUPPRESSED_EXCEPTIONS) { failures.add(e); diff --git a/plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageService.java b/plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageService.java index 43ddb04bc016b..9155c7a9c31c5 100644 --- a/plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageService.java +++ b/plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageService.java @@ -19,14 +19,15 @@ import com.google.cloud.http.HttpTransportOptions; import com.google.cloud.storage.Storage; import com.google.cloud.storage.StorageOptions; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.message.ParameterizedMessage; import org.elasticsearch.common.Strings; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.common.collect.MapBuilder; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.Maps; +import org.elasticsearch.core.SuppressForbidden; +import org.elasticsearch.core.TimeValue; import java.io.BufferedReader; import java.io.IOException; @@ -79,9 +80,8 @@ public synchronized void refreshAndClearCache(Map new ParameterizedMessage("creating GCS client with client_name [{}], endpoint [{}]", clientName, - settings.getHost())); + logger.debug( + () -> new ParameterizedMessage("creating GCS client with client_name [{}], endpoint [{}]", clientName, settings.getHost()) + ); final Storage storage = createClient(settings, stats); clientCache = Maps.copyMapWithAddedEntry(clientCache, repositoryName, storage); return storage; @@ -122,8 +127,8 @@ synchronized void closeRepositoryClient(String repositoryName) { * @return a new client storage instance that can be used to manage objects * (blobs) */ - private Storage createClient(GoogleCloudStorageClientSettings clientSettings, - GoogleCloudStorageOperationsStats stats) throws IOException { + private Storage createClient(GoogleCloudStorageClientSettings clientSettings, GoogleCloudStorageOperationsStats stats) + throws IOException { final HttpTransport httpTransport = SocketAccess.doPrivilegedIOException(() -> { final NetHttpTransport.Builder builder = new NetHttpTransport.Builder(); // requires java.lang.RuntimePermission "setFactory" @@ -140,18 +145,19 @@ private Storage createClient(GoogleCloudStorageClientSettings clientSettings, final GoogleCloudStorageHttpStatsCollector httpStatsCollector = new GoogleCloudStorageHttpStatsCollector(stats); - final HttpTransportOptions httpTransportOptions = new HttpTransportOptions(HttpTransportOptions.newBuilder() - .setConnectTimeout(toTimeout(clientSettings.getConnectTimeout())) - .setReadTimeout(toTimeout(clientSettings.getReadTimeout())) - .setHttpTransportFactory(() -> httpTransport)) { + final HttpTransportOptions httpTransportOptions = new HttpTransportOptions( + HttpTransportOptions.newBuilder() + .setConnectTimeout(toTimeout(clientSettings.getConnectTimeout())) + .setReadTimeout(toTimeout(clientSettings.getReadTimeout())) + .setHttpTransportFactory(() -> httpTransport) + ) { @Override public HttpRequestInitializer getHttpRequestInitializer(ServiceOptions serviceOptions) { HttpRequestInitializer requestInitializer = super.getHttpRequestInitializer(serviceOptions); return (httpRequest) -> { - if (requestInitializer != null) - requestInitializer.initialize(httpRequest); + if (requestInitializer != null) requestInitializer.initialize(httpRequest); httpRequest.setResponseInterceptor(httpStatsCollector); }; @@ -162,17 +168,19 @@ public HttpRequestInitializer getHttpRequestInitializer(ServiceOptions ser return storageOptions.getService(); } - StorageOptions createStorageOptions(final GoogleCloudStorageClientSettings clientSettings, - final HttpTransportOptions httpTransportOptions) { + StorageOptions createStorageOptions( + final GoogleCloudStorageClientSettings clientSettings, + final HttpTransportOptions httpTransportOptions + ) { final StorageOptions.Builder storageOptionsBuilder = StorageOptions.newBuilder() - .setTransportOptions(httpTransportOptions) - .setHeaderProvider(() -> { - final MapBuilder mapBuilder = MapBuilder.newMapBuilder(); - if (Strings.hasLength(clientSettings.getApplicationName())) { - mapBuilder.put("user-agent", clientSettings.getApplicationName()); - } - return mapBuilder.immutableMap(); - }); + .setTransportOptions(httpTransportOptions) + .setHeaderProvider(() -> { + final MapBuilder mapBuilder = MapBuilder.newMapBuilder(); + if (Strings.hasLength(clientSettings.getApplicationName())) { + mapBuilder.put("user-agent", clientSettings.getApplicationName()); + } + return mapBuilder.immutableMap(); + }); if (Strings.hasLength(clientSettings.getHost())) { storageOptionsBuilder.setHost(clientSettings.getHost()); } diff --git a/plugins/repository-gcs/src/test/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageBlobContainerRetriesTests.java b/plugins/repository-gcs/src/test/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageBlobContainerRetriesTests.java index 5f3a5e7dcda80..e004110461b1a 100644 --- a/plugins/repository-gcs/src/test/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageBlobContainerRetriesTests.java +++ b/plugins/repository-gcs/src/test/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageBlobContainerRetriesTests.java @@ -7,23 +7,21 @@ */ package org.elasticsearch.repositories.gcs; +import fixture.gcs.FakeOAuth2HttpHandler; + import com.google.api.gax.retrying.RetrySettings; import com.google.cloud.http.HttpTransportOptions; import com.google.cloud.storage.StorageException; import com.google.cloud.storage.StorageOptions; import com.sun.net.httpserver.HttpHandler; -import fixture.gcs.FakeOAuth2HttpHandler; + import org.apache.http.HttpStatus; -import org.elasticsearch.common.util.BigArrays; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.common.UUIDs; import org.elasticsearch.common.blobstore.BlobContainer; import org.elasticsearch.common.blobstore.BlobPath; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.io.Streams; import org.elasticsearch.common.lucene.store.ByteArrayIndexInput; import org.elasticsearch.common.lucene.store.InputStreamIndexInput; @@ -31,8 +29,12 @@ import org.elasticsearch.common.settings.MockSecureSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeValue; -import org.elasticsearch.core.TimeValue; +import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.common.util.concurrent.CountDown; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.core.SuppressForbidden; +import org.elasticsearch.core.TimeValue; +import org.elasticsearch.core.Tuple; import org.elasticsearch.repositories.blobstore.AbstractBlobContainerRetriesTestCase; import org.elasticsearch.repositories.blobstore.ESMockAPIBasedRepositoryIntegTestCase; import org.elasticsearch.rest.RestStatus; @@ -97,10 +99,12 @@ protected Class unresponsiveExceptionType() { } @Override - protected BlobContainer createBlobContainer(final @Nullable Integer maxRetries, + protected BlobContainer createBlobContainer( + final @Nullable Integer maxRetries, final @Nullable TimeValue readTimeout, final @Nullable Boolean disableChunkedEncoding, - final @Nullable ByteSizeValue bufferSize) { + final @Nullable ByteSizeValue bufferSize + ) { final Settings.Builder clientSettings = Settings.builder(); final String client = randomAlphaOfLength(5).toLowerCase(Locale.ROOT); clientSettings.put(ENDPOINT_SETTING.getConcreteSettingForNamespace(client).getKey(), httpServerUrl()); @@ -115,8 +119,10 @@ protected BlobContainer createBlobContainer(final @Nullable Integer maxRetries, final GoogleCloudStorageService service = new GoogleCloudStorageService() { @Override - StorageOptions createStorageOptions(final GoogleCloudStorageClientSettings clientSettings, - final HttpTransportOptions httpTransportOptions) { + StorageOptions createStorageOptions( + final GoogleCloudStorageClientSettings clientSettings, + final HttpTransportOptions httpTransportOptions + ) { StorageOptions options = super.createStorageOptions(clientSettings, httpTransportOptions); RetrySettings.Builder retrySettingsBuilder = RetrySettings.newBuilder() .setTotalTimeout(options.getRetrySettings().getTotalTimeout()) @@ -140,8 +146,14 @@ StorageOptions createStorageOptions(final GoogleCloudStorageClientSettings clien service.refreshAndClearCache(GoogleCloudStorageClientSettings.load(clientSettings.build())); httpServer.createContext("/token", new FakeOAuth2HttpHandler()); - final GoogleCloudStorageBlobStore blobStore = new GoogleCloudStorageBlobStore("bucket", client, "repo", service, - BigArrays.NON_RECYCLING_INSTANCE, randomIntBetween(1, 8) * 1024); + final GoogleCloudStorageBlobStore blobStore = new GoogleCloudStorageBlobStore( + "bucket", + client, + "repo", + service, + BigArrays.NON_RECYCLING_INSTANCE, + randomIntBetween(1, 8) * 1024 + ); return new GoogleCloudStorageBlobContainer(randomBoolean() ? BlobPath.EMPTY : BlobPath.EMPTY.add("foo"), blobStore); } @@ -249,8 +261,13 @@ public void testWriteLargeBlob() throws IOException { final byte[] data = randomBytes(defaultChunkSize * nbChunks + lastChunkSize); assertThat(data.length, greaterThan(GoogleCloudStorageBlobStore.LARGE_BLOB_THRESHOLD_BYTE_SIZE)); - logger.debug("resumable upload is composed of [{}] total chunks ([{}] chunks of length [{}] and last chunk of length [{}]", - totalChunks, nbChunks, defaultChunkSize, lastChunkSize); + logger.debug( + "resumable upload is composed of [{}] total chunks ([{}] chunks of length [{}] and last chunk of length [{}]", + totalChunks, + nbChunks, + defaultChunkSize, + lastChunkSize + ); final int nbErrors = 2; // we want all requests to fail at least once final AtomicInteger countInits = new AtomicInteger(nbErrors); @@ -278,8 +295,11 @@ public void testWriteLargeBlob() throws IOException { if (countInits.decrementAndGet() <= 0) { byte[] response = requestBody.utf8ToString().getBytes(UTF_8); exchange.getResponseHeaders().add("Content-Type", "application/json"); - exchange.getResponseHeaders().add("Location", httpServerUrl() + - "/upload/storage/v1/b/bucket/o?uploadType=resumable&upload_id=" + sessionUploadId.get()); + exchange.getResponseHeaders() + .add( + "Location", + httpServerUrl() + "/upload/storage/v1/b/bucket/o?uploadType=resumable&upload_id=" + sessionUploadId.get() + ); exchange.sendResponseHeaders(RestStatus.OK.getStatus(), response.length); exchange.getResponseBody().write(response); return; diff --git a/plugins/repository-gcs/src/test/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageBlobStoreContainerTests.java b/plugins/repository-gcs/src/test/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageBlobStoreContainerTests.java index c907afb814689..5386cad8dce9b 100644 --- a/plugins/repository-gcs/src/test/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageBlobStoreContainerTests.java +++ b/plugins/repository-gcs/src/test/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageBlobStoreContainerTests.java @@ -15,6 +15,7 @@ import com.google.cloud.storage.StorageBatch; import com.google.cloud.storage.StorageBatchResult; import com.google.cloud.storage.StorageException; + import org.elasticsearch.common.blobstore.BlobContainer; import org.elasticsearch.common.blobstore.BlobPath; import org.elasticsearch.common.blobstore.BlobStore; @@ -77,8 +78,16 @@ public void testDeleteBlobsIgnoringIfNotExistsThrowsIOException() throws Excepti final GoogleCloudStorageService storageService = mock(GoogleCloudStorageService.class); when(storageService.client(any(String.class), any(String.class), any(GoogleCloudStorageOperationsStats.class))).thenReturn(storage); - try (BlobStore store = new GoogleCloudStorageBlobStore("bucket", "test", "repo", storageService, - BigArrays.NON_RECYCLING_INSTANCE, randomIntBetween(1, 8) * 1024)) { + try ( + BlobStore store = new GoogleCloudStorageBlobStore( + "bucket", + "test", + "repo", + storageService, + BigArrays.NON_RECYCLING_INSTANCE, + randomIntBetween(1, 8) * 1024 + ) + ) { final BlobContainer container = store.blobContainer(BlobPath.EMPTY); IOException e = expectThrows(IOException.class, () -> container.deleteBlobsIgnoringIfNotExists(blobs.iterator())); diff --git a/plugins/repository-gcs/src/test/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageClientSettingsTests.java b/plugins/repository-gcs/src/test/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageClientSettingsTests.java index db167a386adf5..ffb142a85c59d 100644 --- a/plugins/repository-gcs/src/test/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageClientSettingsTests.java +++ b/plugins/repository-gcs/src/test/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageClientSettingsTests.java @@ -10,11 +10,11 @@ import com.google.api.services.storage.StorageScopes; import com.google.auth.oauth2.ServiceAccountCredentials; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.settings.MockSecureSettings; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.core.Tuple; import org.elasticsearch.test.ESTestCase; import java.net.URI; @@ -52,8 +52,9 @@ public void testLoad() throws Exception { final Tuple, Settings> randomClients = randomClients(nbClients, deprecationWarnings); final Map expectedClientsSettings = randomClients.v1(); - final Map actualClientsSettings = GoogleCloudStorageClientSettings - .load(randomClients.v2()); + final Map actualClientsSettings = GoogleCloudStorageClientSettings.load( + randomClients.v2() + ); assertEquals(expectedClientsSettings.size(), actualClientsSettings.size()); for (final String clientName : expectedClientsSettings.keySet()) { @@ -86,17 +87,23 @@ public void testProjectIdDefaultsToCredentials() throws Exception { final String clientName = randomAlphaOfLength(5); final Tuple credentials = randomCredential(clientName); final ServiceAccountCredentials credential = credentials.v1(); - final GoogleCloudStorageClientSettings googleCloudStorageClientSettings = new GoogleCloudStorageClientSettings(credential, - ENDPOINT_SETTING.getDefault(Settings.EMPTY), PROJECT_ID_SETTING.getDefault(Settings.EMPTY), - CONNECT_TIMEOUT_SETTING.getDefault(Settings.EMPTY), READ_TIMEOUT_SETTING.getDefault(Settings.EMPTY), - APPLICATION_NAME_SETTING.getDefault(Settings.EMPTY), new URI("")); + final GoogleCloudStorageClientSettings googleCloudStorageClientSettings = new GoogleCloudStorageClientSettings( + credential, + ENDPOINT_SETTING.getDefault(Settings.EMPTY), + PROJECT_ID_SETTING.getDefault(Settings.EMPTY), + CONNECT_TIMEOUT_SETTING.getDefault(Settings.EMPTY), + READ_TIMEOUT_SETTING.getDefault(Settings.EMPTY), + APPLICATION_NAME_SETTING.getDefault(Settings.EMPTY), + new URI("") + ); assertEquals(credential.getProjectId(), googleCloudStorageClientSettings.getProjectId()); } /** Generates a given number of GoogleCloudStorageClientSettings along with the Settings to build them from **/ - private Tuple, Settings> randomClients(final int nbClients, - final List> deprecationWarnings) - throws Exception { + private Tuple, Settings> randomClients( + final int nbClients, + final List> deprecationWarnings + ) throws Exception { final Map expectedClients = new HashMap<>(); final Settings.Builder settings = Settings.builder(); @@ -119,10 +126,12 @@ private Tuple, Settings> randomCli } /** Generates a random GoogleCloudStorageClientSettings along with the Settings to build it **/ - private static GoogleCloudStorageClientSettings randomClient(final String clientName, - final Settings.Builder settings, - final MockSecureSettings secureSettings, - final List> deprecationWarnings) throws Exception { + private static GoogleCloudStorageClientSettings randomClient( + final String clientName, + final Settings.Builder settings, + final MockSecureSettings secureSettings, + final List> deprecationWarnings + ) throws Exception { final Tuple credentials = randomCredential(clientName); final ServiceAccountCredentials credential = credentials.v1(); @@ -130,8 +139,14 @@ private static GoogleCloudStorageClientSettings randomClient(final String client String endpoint; if (randomBoolean()) { - endpoint = randomFrom("http://www.elastic.co", "http://metadata.google.com:88/oauth", "https://www.googleapis.com", - "https://www.elastic.co:443", "http://localhost:8443", "https://www.googleapis.com/oauth/token"); + endpoint = randomFrom( + "http://www.elastic.co", + "http://metadata.google.com:88/oauth", + "https://www.googleapis.com", + "https://www.elastic.co:443", + "http://localhost:8443", + "https://www.googleapis.com/oauth/token" + ); settings.put(ENDPOINT_SETTING.getConcreteSettingForNamespace(clientName).getKey(), endpoint); } else { endpoint = ENDPOINT_SETTING.getDefault(Settings.EMPTY); @@ -170,8 +185,15 @@ private static GoogleCloudStorageClientSettings randomClient(final String client applicationName = APPLICATION_NAME_SETTING.getDefault(Settings.EMPTY); } - return new GoogleCloudStorageClientSettings(credential, endpoint, projectId, connectTimeout, readTimeout, applicationName, - new URI("")); + return new GoogleCloudStorageClientSettings( + credential, + endpoint, + projectId, + connectTimeout, + readTimeout, + applicationName, + new URI("") + ); } /** Generates a random GoogleCredential along with its corresponding Service Account file provided as a byte array **/ @@ -185,15 +207,23 @@ private static Tuple randomCredential(final S credentialBuilder.setPrivateKeyId("private_key_id_" + clientName); credentialBuilder.setScopes(Collections.singleton(StorageScopes.DEVSTORAGE_FULL_CONTROL)); final String encodedPrivateKey = Base64.getEncoder().encodeToString(keyPair.getPrivate().getEncoded()); - final String serviceAccount = "{\"type\":\"service_account\"," + - "\"project_id\":\"project_id_" + clientName + "\"," + - "\"private_key_id\":\"private_key_id_" + clientName + "\"," + - "\"private_key\":\"-----BEGIN PRIVATE KEY-----\\n" + - encodedPrivateKey + - "\\n-----END PRIVATE KEY-----\\n\"," + - "\"client_email\":\"" + clientName + "\"," + - "\"client_id\":\"id_" + clientName + "\"" + - "}"; + final String serviceAccount = "{\"type\":\"service_account\"," + + "\"project_id\":\"project_id_" + + clientName + + "\"," + + "\"private_key_id\":\"private_key_id_" + + clientName + + "\"," + + "\"private_key\":\"-----BEGIN PRIVATE KEY-----\\n" + + encodedPrivateKey + + "\\n-----END PRIVATE KEY-----\\n\"," + + "\"client_email\":\"" + + clientName + + "\"," + + "\"client_id\":\"id_" + + clientName + + "\"" + + "}"; return Tuple.tuple(credentialBuilder.build(), serviceAccount.getBytes(StandardCharsets.UTF_8)); } diff --git a/plugins/repository-gcs/src/test/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageServiceTests.java b/plugins/repository-gcs/src/test/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageServiceTests.java index 7acba307d54e9..5734bd35046d5 100644 --- a/plugins/repository-gcs/src/test/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageServiceTests.java +++ b/plugins/repository-gcs/src/test/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageServiceTests.java @@ -17,8 +17,8 @@ import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentBuilder; import org.hamcrest.Matchers; import java.security.KeyPair; @@ -28,8 +28,8 @@ import java.util.UUID; import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; -import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.equalTo; public class GoogleCloudStorageServiceTests extends ESTestCase { @@ -39,36 +39,50 @@ public void testClientInitializer() throws Exception { final TimeValue readTimeValue = TimeValue.timeValueNanos(randomIntBetween(0, 2000000)); final String applicationName = randomAlphaOfLength(randomIntBetween(1, 10)).toLowerCase(Locale.ROOT); final String endpoint = randomFrom("http://", "https://") - + randomFrom("www.elastic.co", "www.googleapis.com", "localhost/api", "google.com/oauth") - + ":" + randomIntBetween(1, 65535); + + randomFrom("www.elastic.co", "www.googleapis.com", "localhost/api", "google.com/oauth") + + ":" + + randomIntBetween(1, 65535); final String projectIdName = randomAlphaOfLength(randomIntBetween(1, 10)).toLowerCase(Locale.ROOT); final Settings settings = Settings.builder() - .put(GoogleCloudStorageClientSettings.CONNECT_TIMEOUT_SETTING.getConcreteSettingForNamespace(clientName).getKey(), - connectTimeValue.getStringRep()) - .put(GoogleCloudStorageClientSettings.READ_TIMEOUT_SETTING.getConcreteSettingForNamespace(clientName).getKey(), - readTimeValue.getStringRep()) - .put(GoogleCloudStorageClientSettings.APPLICATION_NAME_SETTING.getConcreteSettingForNamespace(clientName).getKey(), - applicationName) - .put(GoogleCloudStorageClientSettings.ENDPOINT_SETTING.getConcreteSettingForNamespace(clientName).getKey(), endpoint) - .put(GoogleCloudStorageClientSettings.PROJECT_ID_SETTING.getConcreteSettingForNamespace(clientName).getKey(), projectIdName) - .build(); + .put( + GoogleCloudStorageClientSettings.CONNECT_TIMEOUT_SETTING.getConcreteSettingForNamespace(clientName).getKey(), + connectTimeValue.getStringRep() + ) + .put( + GoogleCloudStorageClientSettings.READ_TIMEOUT_SETTING.getConcreteSettingForNamespace(clientName).getKey(), + readTimeValue.getStringRep() + ) + .put( + GoogleCloudStorageClientSettings.APPLICATION_NAME_SETTING.getConcreteSettingForNamespace(clientName).getKey(), + applicationName + ) + .put(GoogleCloudStorageClientSettings.ENDPOINT_SETTING.getConcreteSettingForNamespace(clientName).getKey(), endpoint) + .put(GoogleCloudStorageClientSettings.PROJECT_ID_SETTING.getConcreteSettingForNamespace(clientName).getKey(), projectIdName) + .build(); final GoogleCloudStorageService service = new GoogleCloudStorageService(); service.refreshAndClearCache(GoogleCloudStorageClientSettings.load(settings)); GoogleCloudStorageOperationsStats statsCollector = new GoogleCloudStorageOperationsStats("bucket"); - final IllegalArgumentException e = - expectThrows(IllegalArgumentException.class, () -> service.client("another_client", "repo", statsCollector)); + final IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> service.client("another_client", "repo", statsCollector) + ); assertThat(e.getMessage(), Matchers.startsWith("Unknown client name")); assertSettingDeprecationsAndWarnings( - new Setting[] { GoogleCloudStorageClientSettings.APPLICATION_NAME_SETTING.getConcreteSettingForNamespace(clientName) }); + new Setting[] { GoogleCloudStorageClientSettings.APPLICATION_NAME_SETTING.getConcreteSettingForNamespace(clientName) } + ); final Storage storage = service.client(clientName, "repo", statsCollector); assertThat(storage.getOptions().getApplicationName(), Matchers.containsString(applicationName)); assertThat(storage.getOptions().getHost(), Matchers.is(endpoint)); assertThat(storage.getOptions().getProjectId(), Matchers.is(projectIdName)); assertThat(storage.getOptions().getTransportOptions(), Matchers.instanceOf(HttpTransportOptions.class)); - assertThat(((HttpTransportOptions) storage.getOptions().getTransportOptions()).getConnectTimeout(), - Matchers.is((int) connectTimeValue.millis())); - assertThat(((HttpTransportOptions) storage.getOptions().getTransportOptions()).getReadTimeout(), - Matchers.is((int) readTimeValue.millis())); + assertThat( + ((HttpTransportOptions) storage.getOptions().getTransportOptions()).getConnectTimeout(), + Matchers.is((int) connectTimeValue.millis()) + ); + assertThat( + ((HttpTransportOptions) storage.getOptions().getTransportOptions()).getReadTimeout(), + Matchers.is((int) readTimeValue.millis()) + ); assertThat(storage.getOptions().getCredentials(), Matchers.nullValue(Credentials.class)); } @@ -89,8 +103,10 @@ public void testReinitClientSettings() throws Exception { final Storage client12 = storageService.client("gcs2", "repo2", statsCollector); assertThat(client12.getOptions().getProjectId(), equalTo("project_gcs12")); // client 3 is missing - final IllegalArgumentException e1 = - expectThrows(IllegalArgumentException.class, () -> storageService.client("gcs3", "repo3", statsCollector)); + final IllegalArgumentException e1 = expectThrows( + IllegalArgumentException.class, + () -> storageService.client("gcs3", "repo3", statsCollector) + ); assertThat(e1.getMessage(), containsString("Unknown client name [gcs3].")); // update client settings plugin.reload(settings2); @@ -102,8 +118,10 @@ public void testReinitClientSettings() throws Exception { // old client 2 not changed assertThat(client12.getOptions().getProjectId(), equalTo("project_gcs12")); // new client2 is gone - final IllegalArgumentException e2 = - expectThrows(IllegalArgumentException.class, () -> storageService.client("gcs2", "repo2", statsCollector)); + final IllegalArgumentException e2 = expectThrows( + IllegalArgumentException.class, + () -> storageService.client("gcs2", "repo2", statsCollector) + ); assertThat(e2.getMessage(), containsString("Unknown client name [gcs2].")); // client 3 emerged final Storage client23 = storageService.client("gcs3", "repo3", statsCollector); @@ -118,12 +136,13 @@ public void testClientsAreNotSharedAcrossRepositories() throws Exception { try (GoogleCloudStoragePlugin plugin = new GoogleCloudStoragePlugin(settings)) { final GoogleCloudStorageService storageService = plugin.storageService; - final Storage repo1Client = - storageService.client("gcs1", "repo1", new GoogleCloudStorageOperationsStats("bucket")); - final Storage repo2Client = - storageService.client("gcs1", "repo2", new GoogleCloudStorageOperationsStats("bucket")); - final Storage repo1ClientSecondInstance = - storageService.client("gcs1", "repo1", new GoogleCloudStorageOperationsStats("bucket")); + final Storage repo1Client = storageService.client("gcs1", "repo1", new GoogleCloudStorageOperationsStats("bucket")); + final Storage repo2Client = storageService.client("gcs1", "repo2", new GoogleCloudStorageOperationsStats("bucket")); + final Storage repo1ClientSecondInstance = storageService.client( + "gcs1", + "repo1", + new GoogleCloudStorageOperationsStats("bucket") + ); assertNotSame(repo1Client, repo2Client); assertSame(repo1Client, repo1ClientSecondInstance); @@ -136,13 +155,13 @@ private byte[] serviceAccountFileContent(String projectId) throws Exception { final KeyPair keyPair = keyPairGenerator.generateKeyPair(); final String encodedKey = Base64.getEncoder().encodeToString(keyPair.getPrivate().getEncoded()); final XContentBuilder serviceAccountBuilder = jsonBuilder().startObject() - .field("type", "service_account") - .field("project_id", projectId) - .field("private_key_id", UUID.randomUUID().toString()) - .field("private_key", "-----BEGIN PRIVATE KEY-----\n" + encodedKey + "\n-----END PRIVATE KEY-----\n") - .field("client_email", "integration_test@appspot.gserviceaccount.com") - .field("client_id", "client_id") - .endObject(); + .field("type", "service_account") + .field("project_id", projectId) + .field("private_key_id", UUID.randomUUID().toString()) + .field("private_key", "-----BEGIN PRIVATE KEY-----\n" + encodedKey + "\n-----END PRIVATE KEY-----\n") + .field("client_email", "integration_test@appspot.gserviceaccount.com") + .field("client_id", "client_id") + .endObject(); return BytesReference.toBytes(BytesReference.bytes(serviceAccountBuilder)); } diff --git a/plugins/repository-gcs/src/yamlRestTest/java/org/elasticsearch/repositories/gcs/RepositoryGcsClientYamlTestSuiteIT.java b/plugins/repository-gcs/src/yamlRestTest/java/org/elasticsearch/repositories/gcs/RepositoryGcsClientYamlTestSuiteIT.java index 55b186632b2b5..ae762864a1760 100644 --- a/plugins/repository-gcs/src/yamlRestTest/java/org/elasticsearch/repositories/gcs/RepositoryGcsClientYamlTestSuiteIT.java +++ b/plugins/repository-gcs/src/yamlRestTest/java/org/elasticsearch/repositories/gcs/RepositoryGcsClientYamlTestSuiteIT.java @@ -25,4 +25,3 @@ public static Iterable parameters() throws Exception { return createParameters(); } } - diff --git a/plugins/repository-hdfs/src/main/java/org/elasticsearch/repositories/hdfs/HdfsBlobContainer.java b/plugins/repository-hdfs/src/main/java/org/elasticsearch/repositories/hdfs/HdfsBlobContainer.java index 51e356548413b..7ffb03b5a0329 100644 --- a/plugins/repository-hdfs/src/main/java/org/elasticsearch/repositories/hdfs/HdfsBlobContainer.java +++ b/plugins/repository-hdfs/src/main/java/org/elasticsearch/repositories/hdfs/HdfsBlobContainer.java @@ -15,8 +15,6 @@ import org.apache.hadoop.fs.Options; import org.apache.hadoop.fs.Options.CreateOpts; import org.apache.hadoop.fs.Path; -import org.elasticsearch.core.CheckedConsumer; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.blobstore.BlobContainer; import org.elasticsearch.common.blobstore.BlobMetadata; import org.elasticsearch.common.blobstore.BlobPath; @@ -26,6 +24,8 @@ import org.elasticsearch.common.blobstore.support.PlainBlobMetadata; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.Streams; +import org.elasticsearch.core.CheckedConsumer; +import org.elasticsearch.core.Nullable; import org.elasticsearch.repositories.hdfs.HdfsBlobStore.Operation; import java.io.FileNotFoundException; @@ -98,8 +98,8 @@ public InputStream readBlob(String blobName) throws IOException { // HDFSPrivilegedInputSteam which will ensure that underlying methods will // be called with the proper privileges. try { - return store.execute(fileContext -> - new HDFSPrivilegedInputSteam(fileContext.open(new Path(path, blobName), bufferSize), securityContext) + return store.execute( + fileContext -> new HDFSPrivilegedInputSteam(fileContext.open(new Path(path, blobName), bufferSize), securityContext) ); } catch (FileNotFoundException fnfe) { throw new NoSuchFileException("[" + blobName + "] blob not found"); @@ -130,7 +130,8 @@ public InputStream readBlob(String blobName, long position, long length) throws public void writeBlob(String blobName, InputStream inputStream, long blobSize, boolean failIfAlreadyExists) throws IOException { Path blob = new Path(path, blobName); // we pass CREATE, which means it fails if a blob already exists. - final EnumSet flags = failIfAlreadyExists ? EnumSet.of(CreateFlag.CREATE, CreateFlag.SYNC_BLOCK) + final EnumSet flags = failIfAlreadyExists + ? EnumSet.of(CreateFlag.CREATE, CreateFlag.SYNC_BLOCK) : EnumSet.of(CreateFlag.CREATE, CreateFlag.OVERWRITE, CreateFlag.SYNC_BLOCK); store.execute((Operation) fileContext -> { try { @@ -146,8 +147,9 @@ public void writeBlob(String blobName, InputStream inputStream, long blobSize, b public void writeBlob(String blobName, BytesReference bytes, boolean failIfAlreadyExists) throws IOException { Path blob = new Path(path, blobName); // we pass CREATE, which means it fails if a blob already exists. - final EnumSet flags = failIfAlreadyExists ? EnumSet.of(CreateFlag.CREATE, CreateFlag.SYNC_BLOCK) - : EnumSet.of(CreateFlag.CREATE, CreateFlag.OVERWRITE, CreateFlag.SYNC_BLOCK); + final EnumSet flags = failIfAlreadyExists + ? EnumSet.of(CreateFlag.CREATE, CreateFlag.SYNC_BLOCK) + : EnumSet.of(CreateFlag.CREATE, CreateFlag.OVERWRITE, CreateFlag.SYNC_BLOCK); store.execute((Operation) fileContext -> { try { writeToPath(bytes, blob, fileContext, flags); @@ -159,10 +161,8 @@ public void writeBlob(String blobName, BytesReference bytes, boolean failIfAlrea } @Override - public void writeBlob(String blobName, - boolean failIfAlreadyExists, - boolean atomic, - CheckedConsumer writer) throws IOException { + public void writeBlob(String blobName, boolean failIfAlreadyExists, boolean atomic, CheckedConsumer writer) + throws IOException { Path blob = new Path(path, blobName); if (atomic) { final Path tempBlobPath = new Path(path, FsBlobContainer.tempBlobName(blobName)); @@ -177,8 +177,9 @@ public void writeBlob(String blobName, }); } else { // we pass CREATE, which means it fails if a blob already exists. - final EnumSet flags = failIfAlreadyExists ? EnumSet.of(CreateFlag.CREATE, CreateFlag.SYNC_BLOCK) - : EnumSet.of(CreateFlag.CREATE, CreateFlag.OVERWRITE, CreateFlag.SYNC_BLOCK); + final EnumSet flags = failIfAlreadyExists + ? EnumSet.of(CreateFlag.CREATE, CreateFlag.SYNC_BLOCK) + : EnumSet.of(CreateFlag.CREATE, CreateFlag.OVERWRITE, CreateFlag.SYNC_BLOCK); store.execute((Operation) fileContext -> { try (FSDataOutputStream stream = fileContext.create(blob, flags)) { writer.accept(stream); @@ -206,15 +207,20 @@ public void writeBlobAtomic(String blobName, BytesReference bytes, boolean failI }); } - private void writeToPath(BytesReference bytes, Path blobPath, FileContext fileContext, - EnumSet createFlags) throws IOException { + private void writeToPath(BytesReference bytes, Path blobPath, FileContext fileContext, EnumSet createFlags) + throws IOException { try (FSDataOutputStream stream = fileContext.create(blobPath, createFlags)) { bytes.writeTo(stream); } } - private void writeToPath(InputStream inputStream, long blobSize, FileContext fileContext, Path blobPath, - EnumSet createFlags) throws IOException { + private void writeToPath( + InputStream inputStream, + long blobSize, + FileContext fileContext, + Path blobPath, + EnumSet createFlags + ) throws IOException { final byte[] buffer = new byte[blobSize < bufferSize ? Math.toIntExact(blobSize) : bufferSize]; try (FSDataOutputStream stream = fileContext.create(blobPath, createFlags, CreateOpts.bufferSize(buffer.length))) { int bytesRead; @@ -228,8 +234,9 @@ private void writeToPath(InputStream inputStream, long blobSize, FileContext fil public Map listBlobsByPrefix(@Nullable final String prefix) throws IOException { FileStatus[] files; try { - files = store.execute(fileContext -> fileContext.util().listStatus(path, - path -> prefix == null || path.getName().startsWith(prefix))); + files = store.execute( + fileContext -> fileContext.util().listStatus(path, path -> prefix == null || path.getName().startsWith(prefix)) + ); } catch (FileNotFoundException e) { files = new FileStatus[0]; } diff --git a/plugins/repository-hdfs/src/main/java/org/elasticsearch/repositories/hdfs/HdfsPlugin.java b/plugins/repository-hdfs/src/main/java/org/elasticsearch/repositories/hdfs/HdfsPlugin.java index c63c43759cfe8..957622fe66247 100644 --- a/plugins/repository-hdfs/src/main/java/org/elasticsearch/repositories/hdfs/HdfsPlugin.java +++ b/plugins/repository-hdfs/src/main/java/org/elasticsearch/repositories/hdfs/HdfsPlugin.java @@ -7,27 +7,27 @@ */ package org.elasticsearch.repositories.hdfs; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.security.AccessController; -import java.security.PrivilegedAction; -import java.util.Collections; -import java.util.Map; - import org.apache.hadoop.hdfs.protocolPB.ClientNamenodeProtocolPB; import org.apache.hadoop.security.KerberosInfo; import org.apache.hadoop.security.SecurityUtil; import org.elasticsearch.SpecialPermission; import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.common.util.BigArrays; -import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.env.Environment; import org.elasticsearch.indices.recovery.RecoverySettings; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.plugins.RepositoryPlugin; import org.elasticsearch.repositories.Repository; +import org.elasticsearch.xcontent.NamedXContentRegistry; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.util.Collections; +import java.util.Map; public final class HdfsPlugin extends Plugin implements RepositoryPlugin { @@ -92,8 +92,9 @@ private static Void eagerInit() { KerberosInfo info = SecurityUtil.getKerberosInfo(ClientNamenodeProtocolPB.class, null); // Make sure that the correct class loader was installed. if (info == null) { - throw new RuntimeException("Could not initialize SecurityUtil: " + - "Unable to find services for [org.apache.hadoop.security.SecurityInfo]"); + throw new RuntimeException( + "Could not initialize SecurityUtil: " + "Unable to find services for [org.apache.hadoop.security.SecurityInfo]" + ); } } finally { Thread.currentThread().setContextClassLoader(oldCCL); @@ -102,10 +103,16 @@ private static Void eagerInit() { } @Override - public Map getRepositories(Environment env, NamedXContentRegistry namedXContentRegistry, - ClusterService clusterService, BigArrays bigArrays, - RecoverySettings recoverySettings) { - return Collections.singletonMap("hdfs", (metadata) -> new HdfsRepository(metadata, env, namedXContentRegistry, clusterService, - bigArrays, recoverySettings)); + public Map getRepositories( + Environment env, + NamedXContentRegistry namedXContentRegistry, + ClusterService clusterService, + BigArrays bigArrays, + RecoverySettings recoverySettings + ) { + return Collections.singletonMap( + "hdfs", + (metadata) -> new HdfsRepository(metadata, env, namedXContentRegistry, clusterService, bigArrays, recoverySettings) + ); } } diff --git a/plugins/repository-hdfs/src/main/java/org/elasticsearch/repositories/hdfs/HdfsRepository.java b/plugins/repository-hdfs/src/main/java/org/elasticsearch/repositories/hdfs/HdfsRepository.java index cb4b3b9421ceb..b555b61fcac32 100644 --- a/plugins/repository-hdfs/src/main/java/org/elasticsearch/repositories/hdfs/HdfsRepository.java +++ b/plugins/repository-hdfs/src/main/java/org/elasticsearch/repositories/hdfs/HdfsRepository.java @@ -22,15 +22,15 @@ import org.elasticsearch.cluster.metadata.RepositoryMetadata; import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.Strings; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.common.blobstore.BlobPath; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.common.util.BigArrays; -import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.env.Environment; import org.elasticsearch.indices.recovery.RecoverySettings; import org.elasticsearch.repositories.blobstore.BlobStoreRepository; +import org.elasticsearch.xcontent.NamedXContentRegistry; import java.io.IOException; import java.io.UncheckedIOException; @@ -52,9 +52,14 @@ public final class HdfsRepository extends BlobStoreRepository { private final URI uri; private final String pathSetting; - public HdfsRepository(RepositoryMetadata metadata, Environment environment, - NamedXContentRegistry namedXContentRegistry, ClusterService clusterService, BigArrays bigArrays, - RecoverySettings recoverySettings) { + public HdfsRepository( + RepositoryMetadata metadata, + Environment environment, + NamedXContentRegistry namedXContentRegistry, + ClusterService clusterService, + BigArrays bigArrays, + RecoverySettings recoverySettings + ) { super(metadata, namedXContentRegistry, clusterService, bigArrays, recoverySettings, BlobPath.EMPTY); this.environment = environment; @@ -66,14 +71,24 @@ public HdfsRepository(RepositoryMetadata metadata, Environment environment, } uri = URI.create(uriSetting); if ("hdfs".equalsIgnoreCase(uri.getScheme()) == false) { - throw new IllegalArgumentException(String.format(Locale.ROOT, - "Invalid scheme [%s] specified in uri [%s]; only 'hdfs' uri allowed for hdfs snapshot/restore", - uri.getScheme(), - uriSetting)); + throw new IllegalArgumentException( + String.format( + Locale.ROOT, + "Invalid scheme [%s] specified in uri [%s]; only 'hdfs' uri allowed for hdfs snapshot/restore", + uri.getScheme(), + uriSetting + ) + ); } if (Strings.hasLength(uri.getPath()) && uri.getPath().equals("/") == false) { - throw new IllegalArgumentException(String.format(Locale.ROOT, - "Use 'path' option to specify a path [%s], not the uri [%s] for hdfs snapshot/restore", uri.getPath(), uriSetting)); + throw new IllegalArgumentException( + String.format( + Locale.ROOT, + "Use 'path' option to specify a path [%s], not the uri [%s] for hdfs snapshot/restore", + uri.getPath(), + uriSetting + ) + ); } pathSetting = getMetadata().settings().get("path"); @@ -83,7 +98,7 @@ public HdfsRepository(RepositoryMetadata metadata, Environment environment, } } - private HdfsBlobStore createBlobstore(URI uri, String path, Settings repositorySettings) { + private HdfsBlobStore createBlobstore(URI uri, String path, Settings repositorySettings) { Configuration hadoopConfiguration = new Configuration(repositorySettings.getAsBoolean("load_defaults", true)); hadoopConfiguration.setClassLoader(HdfsRepository.class.getClassLoader()); hadoopConfiguration.reloadConfiguration(); @@ -119,10 +134,12 @@ private HdfsBlobStore createBlobstore(URI uri, String path, Settings repositoryS } }); - logger.debug("Using file-system [{}] for URI [{}], path [{}]", + logger.debug( + "Using file-system [{}] for URI [{}], path [{}]", fileContext.getDefaultFileSystem(), fileContext.getDefaultFileSystem().getUri(), - path); + path + ); try { return new HdfsBlobStore(fileContext, path, bufferSize, isReadOnly(), haEnabled); @@ -134,9 +151,8 @@ private HdfsBlobStore createBlobstore(URI uri, String path, Settings repositoryS private UserGroupInformation login(Configuration hadoopConfiguration, Settings repositorySettings) { // Validate the authentication method: AuthenticationMethod authMethod = SecurityUtil.getAuthenticationMethod(hadoopConfiguration); - if (authMethod.equals(AuthenticationMethod.SIMPLE) == false - && authMethod.equals(AuthenticationMethod.KERBEROS) == false) { - throw new RuntimeException("Unsupported authorization mode ["+authMethod+"]"); + if (authMethod.equals(AuthenticationMethod.SIMPLE) == false && authMethod.equals(AuthenticationMethod.KERBEROS) == false) { + throw new RuntimeException("Unsupported authorization mode [" + authMethod + "]"); } // Check if the user added a principal to use, and that there is a keytab file provided @@ -144,13 +160,18 @@ private UserGroupInformation login(Configuration hadoopConfiguration, Settings r // Check to see if the authentication method is compatible if (kerberosPrincipal != null && authMethod.equals(AuthenticationMethod.SIMPLE)) { - logger.warn("Hadoop authentication method is set to [SIMPLE], but a Kerberos principal is " + - "specified. Continuing with [KERBEROS] authentication."); + logger.warn( + "Hadoop authentication method is set to [SIMPLE], but a Kerberos principal is " + + "specified. Continuing with [KERBEROS] authentication." + ); SecurityUtil.setAuthenticationMethod(AuthenticationMethod.KERBEROS, hadoopConfiguration); } else if (kerberosPrincipal == null && authMethod.equals(AuthenticationMethod.KERBEROS)) { - throw new RuntimeException("HDFS Repository does not support [KERBEROS] authentication without " + - "a valid Kerberos principal and keytab. Please specify a principal in the repository settings with [" + - CONF_SECURITY_PRINCIPAL + "]."); + throw new RuntimeException( + "HDFS Repository does not support [KERBEROS] authentication without " + + "a valid Kerberos principal and keytab. Please specify a principal in the repository settings with [" + + CONF_SECURITY_PRINCIPAL + + "]." + ); } // Now we can initialize the UGI with the configuration. @@ -186,8 +207,11 @@ private static String preparePrincipal(String originalPrincipal) { } if (originalPrincipal.equals(finalPrincipal) == false) { - logger.debug("Found service principal. Converted original principal name [{}] to server principal [{}]", - originalPrincipal, finalPrincipal); + logger.debug( + "Found service principal. Converted original principal name [{}] to server principal [{}]", + originalPrincipal, + finalPrincipal + ); } } return finalPrincipal; @@ -211,9 +235,9 @@ private static String getHostName() { protected HdfsBlobStore createBlobStore() { // initialize our blobstore using elevated privileges. SpecialPermission.check(); - final HdfsBlobStore blobStore = - AccessController.doPrivileged((PrivilegedAction) - () -> createBlobstore(uri, pathSetting, getMetadata().settings())); + final HdfsBlobStore blobStore = AccessController.doPrivileged( + (PrivilegedAction) () -> createBlobstore(uri, pathSetting, getMetadata().settings()) + ); return blobStore; } diff --git a/plugins/repository-hdfs/src/main/java/org/elasticsearch/repositories/hdfs/HdfsSecurityContext.java b/plugins/repository-hdfs/src/main/java/org/elasticsearch/repositories/hdfs/HdfsSecurityContext.java index d1c825a88f70b..2cd6ccadd0963 100644 --- a/plugins/repository-hdfs/src/main/java/org/elasticsearch/repositories/hdfs/HdfsSecurityContext.java +++ b/plugins/repository-hdfs/src/main/java/org/elasticsearch/repositories/hdfs/HdfsSecurityContext.java @@ -7,6 +7,10 @@ */ package org.elasticsearch.repositories.hdfs; +import org.apache.hadoop.security.UserGroupInformation; +import org.elasticsearch.SpecialPermission; +import org.elasticsearch.env.Environment; + import java.io.IOException; import java.io.UncheckedIOException; import java.lang.reflect.ReflectPermission; @@ -18,14 +22,11 @@ import java.security.PrivilegedActionException; import java.security.PrivilegedExceptionAction; import java.util.Arrays; + import javax.security.auth.AuthPermission; import javax.security.auth.PrivateCredentialPermission; import javax.security.auth.kerberos.ServicePermission; -import org.apache.hadoop.security.UserGroupInformation; -import org.elasticsearch.SpecialPermission; -import org.elasticsearch.env.Environment; - /** * Oversees all the security specific logic for the HDFS Repository plugin. * @@ -38,15 +39,14 @@ class HdfsSecurityContext { private static final Permission[] KERBEROS_AUTH_PERMISSIONS; static { // We can do FS ops with only a few elevated permissions: - SIMPLE_AUTH_PERMISSIONS = new Permission[]{ + SIMPLE_AUTH_PERMISSIONS = new Permission[] { new SocketPermission("*", "connect"), // 1) hadoop dynamic proxy is messy with access rules new ReflectPermission("suppressAccessChecks"), // 2) allow hadoop to add credentials to our Subject new AuthPermission("modifyPrivateCredentials"), // 3) RPC Engine requires this for re-establishing pooled connections over the lifetime of the client - new PrivateCredentialPermission("org.apache.hadoop.security.Credentials * \"*\"", "read") - }; + new PrivateCredentialPermission("org.apache.hadoop.security.Credentials * \"*\"", "read") }; // If Security is enabled, we need all the following elevated permissions: KERBEROS_AUTH_PERMISSIONS = new Permission[] { diff --git a/plugins/repository-hdfs/src/test/java/org/elasticsearch/repositories/hdfs/HaHdfsFailoverTestSuiteIT.java b/plugins/repository-hdfs/src/test/java/org/elasticsearch/repositories/hdfs/HaHdfsFailoverTestSuiteIT.java index 34d5a3597d5be..5b0beaf0c992a 100644 --- a/plugins/repository-hdfs/src/test/java/org/elasticsearch/repositories/hdfs/HaHdfsFailoverTestSuiteIT.java +++ b/plugins/repository-hdfs/src/test/java/org/elasticsearch/repositories/hdfs/HaHdfsFailoverTestSuiteIT.java @@ -50,12 +50,12 @@ public void testHAFailoverWithRepository() throws Exception { String nn1Port = "10001"; String nn2Port = "10002"; if (ports.length() > 0) { - final Path path = PathUtils.get(ports); - final List lines = AccessController.doPrivileged((PrivilegedExceptionAction>) () -> { - return Files.readAllLines(path); - }); - nn1Port = lines.get(0); - nn2Port = lines.get(1); + final Path path = PathUtils.get(ports); + final List lines = AccessController.doPrivileged( + (PrivilegedExceptionAction>) () -> { return Files.readAllLines(path); } + ); + nn1Port = lines.get(0); + nn2Port = lines.get(1); } boolean securityEnabled = hdfsKerberosPrincipal != null; @@ -101,21 +101,26 @@ public void testHAFailoverWithRepository() throws Exception { { Request request = new Request("PUT", "/_snapshot/hdfs_ha_repo_read"); request.setJsonEntity( - "{" + - "\"type\":\"hdfs\"," + - "\"settings\":{" + - "\"uri\": \"hdfs://ha-hdfs/\",\n" + - "\"path\": \"/user/elasticsearch/existing/readonly-repository\"," + - "\"readonly\": \"true\"," + - securityCredentials(securityEnabled, esKerberosPrincipal) + - "\"conf.dfs.nameservices\": \"ha-hdfs\"," + - "\"conf.dfs.ha.namenodes.ha-hdfs\": \"nn1,nn2\"," + - "\"conf.dfs.namenode.rpc-address.ha-hdfs.nn1\": \"localhost:"+nn1Port+"\"," + - "\"conf.dfs.namenode.rpc-address.ha-hdfs.nn2\": \"localhost:"+nn2Port+"\"," + - "\"conf.dfs.client.failover.proxy.provider.ha-hdfs\": " + - "\"org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider\"" + - "}" + - "}"); + "{" + + "\"type\":\"hdfs\"," + + "\"settings\":{" + + "\"uri\": \"hdfs://ha-hdfs/\",\n" + + "\"path\": \"/user/elasticsearch/existing/readonly-repository\"," + + "\"readonly\": \"true\"," + + securityCredentials(securityEnabled, esKerberosPrincipal) + + "\"conf.dfs.nameservices\": \"ha-hdfs\"," + + "\"conf.dfs.ha.namenodes.ha-hdfs\": \"nn1,nn2\"," + + "\"conf.dfs.namenode.rpc-address.ha-hdfs.nn1\": \"localhost:" + + nn1Port + + "\"," + + "\"conf.dfs.namenode.rpc-address.ha-hdfs.nn2\": \"localhost:" + + nn2Port + + "\"," + + "\"conf.dfs.client.failover.proxy.provider.ha-hdfs\": " + + "\"org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider\"" + + "}" + + "}" + ); Response response = client.performRequest(request); Assert.assertEquals(200, response.getStatusLine().getStatusCode()); @@ -139,8 +144,7 @@ public void testHAFailoverWithRepository() throws Exception { private String securityCredentials(boolean securityEnabled, String kerberosPrincipal) { if (securityEnabled) { - return "\"security.principal\": \""+kerberosPrincipal+"\"," + - "\"conf.dfs.data.transfer.protection\": \"authentication\","; + return "\"security.principal\": \"" + kerberosPrincipal + "\"," + "\"conf.dfs.data.transfer.protection\": \"authentication\","; } else { return ""; } @@ -235,11 +239,11 @@ public int run(String[] argv) throws Exception { } public int transitionToStandby(String namenodeID) throws Exception { - return run(new String[]{"-transitionToStandby", namenodeID}); + return run(new String[] { "-transitionToStandby", namenodeID }); } public int transitionToActive(String namenodeID) throws Exception { - return run(new String[]{"-transitionToActive", namenodeID}); + return run(new String[] { "-transitionToActive", namenodeID }); } public void close() { diff --git a/plugins/repository-hdfs/src/test/java/org/elasticsearch/repositories/hdfs/HdfsBlobStoreContainerTests.java b/plugins/repository-hdfs/src/test/java/org/elasticsearch/repositories/hdfs/HdfsBlobStoreContainerTests.java index 46b538168b9d0..624f327be1a01 100644 --- a/plugins/repository-hdfs/src/test/java/org/elasticsearch/repositories/hdfs/HdfsBlobStoreContainerTests.java +++ b/plugins/repository-hdfs/src/test/java/org/elasticsearch/repositories/hdfs/HdfsBlobStoreContainerTests.java @@ -9,21 +9,20 @@ package org.elasticsearch.repositories.hdfs; import com.carrotsearch.randomizedtesting.annotations.ThreadLeakFilters; + import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.AbstractFileSystem; import org.apache.hadoop.fs.FileContext; import org.apache.hadoop.fs.Path; import org.apache.hadoop.fs.UnsupportedFileSystemException; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.common.blobstore.BlobContainer; import org.elasticsearch.common.blobstore.BlobPath; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.io.Streams; +import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.test.ESTestCase; import org.hamcrest.CoreMatchers; -import javax.security.auth.Subject; - import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Constructor; @@ -37,18 +36,19 @@ import java.util.Arrays; import java.util.Collections; +import javax.security.auth.Subject; + import static org.elasticsearch.repositories.blobstore.ESBlobStoreRepositoryIntegTestCase.randomBytes; import static org.elasticsearch.repositories.blobstore.ESBlobStoreRepositoryIntegTestCase.readBlobFully; import static org.elasticsearch.repositories.blobstore.ESBlobStoreRepositoryIntegTestCase.writeBlob; -@ThreadLeakFilters(filters = {HdfsClientThreadLeakFilter.class}) +@ThreadLeakFilters(filters = { HdfsClientThreadLeakFilter.class }) public class HdfsBlobStoreContainerTests extends ESTestCase { private FileContext createTestContext() { FileContext fileContext; try { - fileContext = AccessController.doPrivileged((PrivilegedExceptionAction) - () -> createContext(new URI("hdfs:///"))); + fileContext = AccessController.doPrivileged((PrivilegedExceptionAction) () -> createContext(new URI("hdfs:///"))); } catch (PrivilegedActionException e) { throw new RuntimeException(e.getCause()); } @@ -75,8 +75,7 @@ private FileContext createContext(URI uri) { try { Principal principal = (Principal) ctor.newInstance(System.getProperty("user.name")); - subject = new Subject(false, Collections.singleton(principal), - Collections.emptySet(), Collections.emptySet()); + subject = new Subject(false, Collections.singleton(principal), Collections.emptySet(), Collections.emptySet()); } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) { throw new RuntimeException(e); } @@ -155,7 +154,7 @@ public void testReadRange() throws Exception { writeBlob(container, "foo", new BytesArray(data), randomBoolean()); int pos = randomIntBetween(0, data.length / 2); int len = randomIntBetween(pos, data.length) - pos; - assertArrayEquals(readBlobPartially(container, "foo", pos, len), Arrays.copyOfRange(data, pos, pos+len)); + assertArrayEquals(readBlobPartially(container, "foo", pos, len), Arrays.copyOfRange(data, pos, pos + len)); assertTrue(container.blobExists("foo")); } diff --git a/plugins/repository-hdfs/src/test/java/org/elasticsearch/repositories/hdfs/HdfsBlobStoreRepositoryTests.java b/plugins/repository-hdfs/src/test/java/org/elasticsearch/repositories/hdfs/HdfsBlobStoreRepositoryTests.java index 4c393f24a8295..7a9260b38bcda 100644 --- a/plugins/repository-hdfs/src/test/java/org/elasticsearch/repositories/hdfs/HdfsBlobStoreRepositoryTests.java +++ b/plugins/repository-hdfs/src/test/java/org/elasticsearch/repositories/hdfs/HdfsBlobStoreRepositoryTests.java @@ -8,6 +8,7 @@ package org.elasticsearch.repositories.hdfs; import com.carrotsearch.randomizedtesting.annotations.ThreadLeakFilters; + import org.elasticsearch.common.settings.Settings; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.repositories.blobstore.ESBlobStoreRepositoryIntegTestCase; @@ -33,7 +34,8 @@ protected Settings repositorySettings(String repoName) { .put("conf.fs.AbstractFileSystem.hdfs.impl", TestingFs.class.getName()) .put("path", "foo") .put("chunk_size", randomIntBetween(100, 1000) + "k") - .put("compress", randomBoolean()).build(); + .put("compress", randomBoolean()) + .build(); } @Override diff --git a/plugins/repository-hdfs/src/test/java/org/elasticsearch/repositories/hdfs/HdfsClientThreadLeakFilter.java b/plugins/repository-hdfs/src/test/java/org/elasticsearch/repositories/hdfs/HdfsClientThreadLeakFilter.java index 2256bf386e635..c79418557da20 100644 --- a/plugins/repository-hdfs/src/test/java/org/elasticsearch/repositories/hdfs/HdfsClientThreadLeakFilter.java +++ b/plugins/repository-hdfs/src/test/java/org/elasticsearch/repositories/hdfs/HdfsClientThreadLeakFilter.java @@ -25,8 +25,7 @@ */ public final class HdfsClientThreadLeakFilter implements ThreadFilter { - private static final String OFFENDING_THREAD_NAME = - "org.apache.hadoop.fs.FileSystem$Statistics$StatisticsDataReferenceCleaner"; + private static final String OFFENDING_THREAD_NAME = "org.apache.hadoop.fs.FileSystem$Statistics$StatisticsDataReferenceCleaner"; @Override public boolean reject(Thread t) { diff --git a/plugins/repository-hdfs/src/test/java/org/elasticsearch/repositories/hdfs/HdfsRepositoryTests.java b/plugins/repository-hdfs/src/test/java/org/elasticsearch/repositories/hdfs/HdfsRepositoryTests.java index 134272087401e..f81182b42d1e4 100644 --- a/plugins/repository-hdfs/src/test/java/org/elasticsearch/repositories/hdfs/HdfsRepositoryTests.java +++ b/plugins/repository-hdfs/src/test/java/org/elasticsearch/repositories/hdfs/HdfsRepositoryTests.java @@ -8,6 +8,7 @@ package org.elasticsearch.repositories.hdfs; import com.carrotsearch.randomizedtesting.annotations.ThreadLeakFilters; + import org.elasticsearch.action.admin.cluster.repositories.cleanup.CleanupRepositoryResponse; import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.common.settings.MockSecureSettings; @@ -36,15 +37,19 @@ protected SecureSettings credentials() { @Override protected void createRepository(String repoName) { - AcknowledgedResponse putRepositoryResponse = client().admin().cluster().preparePutRepository(repoName) + AcknowledgedResponse putRepositoryResponse = client().admin() + .cluster() + .preparePutRepository(repoName) .setType("hdfs") - .setSettings(Settings.builder() - .put("uri", "hdfs:///") - .put("conf.fs.AbstractFileSystem.hdfs.impl", TestingFs.class.getName()) - .put("path", "foo") - .put("chunk_size", randomIntBetween(100, 1000) + "k") - .put("compress", randomBoolean()) - ).get(); + .setSettings( + Settings.builder() + .put("uri", "hdfs:///") + .put("conf.fs.AbstractFileSystem.hdfs.impl", TestingFs.class.getName()) + .put("path", "foo") + .put("chunk_size", randomIntBetween(100, 1000) + "k") + .put("compress", randomBoolean()) + ) + .get(); assertThat(putRepositoryResponse.isAcknowledged(), equalTo(true)); } diff --git a/plugins/repository-hdfs/src/test/java/org/elasticsearch/repositories/hdfs/HdfsTests.java b/plugins/repository-hdfs/src/test/java/org/elasticsearch/repositories/hdfs/HdfsTests.java index 60851c270e3fa..0e6fd53c64182 100644 --- a/plugins/repository-hdfs/src/test/java/org/elasticsearch/repositories/hdfs/HdfsTests.java +++ b/plugins/repository-hdfs/src/test/java/org/elasticsearch/repositories/hdfs/HdfsTests.java @@ -8,13 +8,14 @@ package org.elasticsearch.repositories.hdfs; import com.carrotsearch.randomizedtesting.annotations.ThreadLeakFilters; + import org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse; import org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse; import org.elasticsearch.action.support.master.AcknowledgedResponse; -import org.elasticsearch.jdk.JavaVersion; import org.elasticsearch.client.Client; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.jdk.JavaVersion; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.repositories.RepositoriesService; import org.elasticsearch.repositories.RepositoryException; @@ -28,7 +29,7 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.greaterThan; -@ThreadLeakFilters(filters = {HdfsClientThreadLeakFilter.class}) +@ThreadLeakFilters(filters = { HdfsClientThreadLeakFilter.class }) public class HdfsTests extends ESSingleNodeTestCase { @Override @@ -40,15 +41,19 @@ public void testSimpleWorkflow() { assumeFalse("https://github.com/elastic/elasticsearch/issues/31498", JavaVersion.current().equals(JavaVersion.parse("11"))); Client client = client(); - AcknowledgedResponse putRepositoryResponse = client.admin().cluster().preparePutRepository("test-repo") - .setType("hdfs") - .setSettings(Settings.builder() - .put("uri", "hdfs:///") - .put("conf.fs.AbstractFileSystem.hdfs.impl", TestingFs.class.getName()) - .put("path", "foo") - .put("chunk_size", randomIntBetween(100, 1000) + "k") - .put("compress", randomBoolean()) - ).get(); + AcknowledgedResponse putRepositoryResponse = client.admin() + .cluster() + .preparePutRepository("test-repo") + .setType("hdfs") + .setSettings( + Settings.builder() + .put("uri", "hdfs:///") + .put("conf.fs.AbstractFileSystem.hdfs.impl", TestingFs.class.getName()) + .put("path", "foo") + .put("chunk_size", randomIntBetween(100, 1000) + "k") + .put("compress", randomBoolean()) + ) + .get(); assertThat(putRepositoryResponse.isAcknowledged(), equalTo(true)); createIndex("test-idx-1"); @@ -75,18 +80,15 @@ public void testSimpleWorkflow() { .setIndices("test-idx-*", "-test-idx-3") .get(); assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0)); - assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), - equalTo(createSnapshotResponse.getSnapshotInfo().totalShards())); + assertThat( + createSnapshotResponse.getSnapshotInfo().successfulShards(), + equalTo(createSnapshotResponse.getSnapshotInfo().totalShards()) + ); - assertThat(client.admin() - .cluster() - .prepareGetSnapshots("test-repo") - .setSnapshots("test-snap") - .get() - .getSnapshots() - .get(0) - .state(), - equalTo(SnapshotState.SUCCESS)); + assertThat( + client.admin().cluster().prepareGetSnapshots("test-repo").setSnapshots("test-snap").get().getSnapshots().get(0).state(), + equalTo(SnapshotState.SUCCESS) + ); logger.info("--> delete some data"); for (int i = 0; i < 50; i++) { @@ -137,16 +139,13 @@ public void testSimpleWorkflow() { ClusterState clusterState = client.admin().cluster().prepareState().get().getState(); assertThat(clusterState.getMetadata().hasIndex("test-idx-1"), equalTo(true)); assertThat(clusterState.getMetadata().hasIndex("test-idx-2"), equalTo(false)); - final BlobStoreRepository repo = - (BlobStoreRepository) getInstanceFromNode(RepositoriesService.class).repository("test-repo"); + final BlobStoreRepository repo = (BlobStoreRepository) getInstanceFromNode(RepositoriesService.class).repository("test-repo"); BlobStoreTestUtil.assertConsistency(repo); } public void testMissingUri() { try { - client().admin().cluster().preparePutRepository("test-repo") - .setType("hdfs") - .setSettings(Settings.EMPTY).get(); + client().admin().cluster().preparePutRepository("test-repo").setType("hdfs").setSettings(Settings.EMPTY).get(); fail(); } catch (RepositoryException e) { assertTrue(e.getCause() instanceof IllegalArgumentException); @@ -156,10 +155,12 @@ public void testMissingUri() { public void testEmptyUri() { try { - client().admin().cluster().preparePutRepository("test-repo") + client().admin() + .cluster() + .preparePutRepository("test-repo") .setType("hdfs") - .setSettings(Settings.builder() - .put("uri", "/path").build()).get(); + .setSettings(Settings.builder().put("uri", "/path").build()) + .get(); fail(); } catch (RepositoryException e) { assertTrue(e.getCause() instanceof IllegalArgumentException); @@ -169,10 +170,12 @@ public void testEmptyUri() { public void testNonHdfsUri() { try { - client().admin().cluster().preparePutRepository("test-repo") + client().admin() + .cluster() + .preparePutRepository("test-repo") .setType("hdfs") - .setSettings(Settings.builder() - .put("uri", "file:///").build()).get(); + .setSettings(Settings.builder().put("uri", "file:///").build()) + .get(); fail(); } catch (RepositoryException e) { assertTrue(e.getCause() instanceof IllegalArgumentException); @@ -182,10 +185,12 @@ public void testNonHdfsUri() { public void testPathSpecifiedInHdfs() { try { - client().admin().cluster().preparePutRepository("test-repo") + client().admin() + .cluster() + .preparePutRepository("test-repo") .setType("hdfs") - .setSettings(Settings.builder() - .put("uri", "hdfs:///some/path").build()).get(); + .setSettings(Settings.builder().put("uri", "hdfs:///some/path").build()) + .get(); fail(); } catch (RepositoryException e) { assertTrue(e.getCause() instanceof IllegalArgumentException); @@ -195,10 +200,12 @@ public void testPathSpecifiedInHdfs() { public void testMissingPath() { try { - client().admin().cluster().preparePutRepository("test-repo") + client().admin() + .cluster() + .preparePutRepository("test-repo") .setType("hdfs") - .setSettings(Settings.builder() - .put("uri", "hdfs:///").build()).get(); + .setSettings(Settings.builder().put("uri", "hdfs:///").build()) + .get(); fail(); } catch (RepositoryException e) { assertTrue(e.getCause() instanceof IllegalArgumentException); diff --git a/plugins/repository-hdfs/src/test/java/org/elasticsearch/repositories/hdfs/TestingFs.java b/plugins/repository-hdfs/src/test/java/org/elasticsearch/repositories/hdfs/TestingFs.java index 338bdcbe95c99..29589997953ef 100644 --- a/plugins/repository-hdfs/src/test/java/org/elasticsearch/repositories/hdfs/TestingFs.java +++ b/plugins/repository-hdfs/src/test/java/org/elasticsearch/repositories/hdfs/TestingFs.java @@ -19,11 +19,11 @@ import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; +import java.nio.file.Files; +import java.nio.file.NoSuchFileException; import java.nio.file.Path; import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.spi.FileSystemProvider; -import java.nio.file.Files; -import java.nio.file.NoSuchFileException; /** * Extends LFS to improve some operations to keep the security permissions at @@ -51,7 +51,7 @@ protected org.apache.hadoop.fs.Path getInitialWorkingDirectory() { @Override public void setPermission(org.apache.hadoop.fs.Path path, FsPermission permission) { - // no execution, thank you very much! + // no execution, thank you very much! } // pretend we don't support symlinks (which causes hadoop to want to do crazy things), @@ -101,6 +101,6 @@ public TestingFs(URI uri, Configuration configuration) throws URISyntaxException @Override public void checkPath(org.apache.hadoop.fs.Path path) { - // we do evil stuff, we admit it. + // we do evil stuff, we admit it. } } diff --git a/plugins/repository-s3/src/internalClusterTest/java/org/elasticsearch/repositories/s3/S3BlobStoreRepositoryTests.java b/plugins/repository-s3/src/internalClusterTest/java/org/elasticsearch/repositories/s3/S3BlobStoreRepositoryTests.java index 3f8ceb3290e60..94c90b14d1628 100644 --- a/plugins/repository-s3/src/internalClusterTest/java/org/elasticsearch/repositories/s3/S3BlobStoreRepositoryTests.java +++ b/plugins/repository-s3/src/internalClusterTest/java/org/elasticsearch/repositories/s3/S3BlobStoreRepositoryTests.java @@ -7,16 +7,17 @@ */ package org.elasticsearch.repositories.s3; +import fixture.s3.S3HttpHandler; + import com.amazonaws.http.AmazonHttpClient; import com.sun.net.httpserver.Headers; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; -import fixture.s3.S3HttpHandler; + import org.elasticsearch.action.ActionRunnable; import org.elasticsearch.action.support.PlainActionFuture; import org.elasticsearch.cluster.metadata.RepositoryMetadata; import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.common.blobstore.BlobContainer; import org.elasticsearch.common.blobstore.BlobPath; import org.elasticsearch.common.blobstore.BlobStore; @@ -26,10 +27,9 @@ import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeUnit; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.BigArrays; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.core.SuppressForbidden; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.indices.recovery.RecoverySettings; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.repositories.RepositoriesService; @@ -42,6 +42,8 @@ import org.elasticsearch.snapshots.mockstore.BlobStoreWrapper; import org.elasticsearch.test.ESIntegTestCase; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentFactory; import java.io.IOException; import java.util.ArrayList; @@ -140,30 +142,51 @@ protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { public void testEnforcedCooldownPeriod() throws IOException { final String repoName = randomRepositoryName(); - createRepository(repoName, Settings.builder().put(repositorySettings(repoName)) - .put(S3Repository.COOLDOWN_PERIOD.getKey(), TEST_COOLDOWN_PERIOD).build(), true); - - final SnapshotId fakeOldSnapshot = client().admin().cluster().prepareCreateSnapshot(repoName, "snapshot-old") - .setWaitForCompletion(true).setIndices().get().getSnapshotInfo().snapshotId(); + createRepository( + repoName, + Settings.builder().put(repositorySettings(repoName)).put(S3Repository.COOLDOWN_PERIOD.getKey(), TEST_COOLDOWN_PERIOD).build(), + true + ); + + final SnapshotId fakeOldSnapshot = client().admin() + .cluster() + .prepareCreateSnapshot(repoName, "snapshot-old") + .setWaitForCompletion(true) + .setIndices() + .get() + .getSnapshotInfo() + .snapshotId(); final RepositoriesService repositoriesService = internalCluster().getCurrentMasterNodeInstance(RepositoriesService.class); final BlobStoreRepository repository = (BlobStoreRepository) repositoriesService.repository(repoName); final RepositoryData repositoryData = getRepositoryData(repository); - final RepositoryData modifiedRepositoryData = repositoryData - .withoutUUIDs() - .withExtraDetails(Collections.singletonMap( - fakeOldSnapshot, - new RepositoryData.SnapshotDetails( - SnapshotState.SUCCESS, - SnapshotsService.SHARD_GEN_IN_REPO_DATA_VERSION.minimumCompatibilityVersion(), - 0L, // -1 would refresh RepositoryData and find the real version - 0L, // -1 would refresh RepositoryData and find the real version, - "" // null would refresh RepositoryData and find the real version - ))); - final BytesReference serialized = BytesReference.bytes(modifiedRepositoryData.snapshotsToXContent(XContentFactory.jsonBuilder(), - SnapshotsService.OLD_SNAPSHOT_FORMAT)); - PlainActionFuture.get(f -> repository.threadPool().generic().execute(ActionRunnable.run(f, () -> - repository.blobStore().blobContainer(repository.basePath()).writeBlobAtomic( - BlobStoreRepository.INDEX_FILE_PREFIX + modifiedRepositoryData.getGenId(), serialized, true)))); + final RepositoryData modifiedRepositoryData = repositoryData.withoutUUIDs() + .withExtraDetails( + Collections.singletonMap( + fakeOldSnapshot, + new RepositoryData.SnapshotDetails( + SnapshotState.SUCCESS, + SnapshotsService.SHARD_GEN_IN_REPO_DATA_VERSION.minimumCompatibilityVersion(), + 0L, // -1 would refresh RepositoryData and find the real version + 0L, // -1 would refresh RepositoryData and find the real version, + "" // null would refresh RepositoryData and find the real version + ) + ) + ); + final BytesReference serialized = BytesReference.bytes( + modifiedRepositoryData.snapshotsToXContent(XContentFactory.jsonBuilder(), SnapshotsService.OLD_SNAPSHOT_FORMAT) + ); + PlainActionFuture.get( + f -> repository.threadPool() + .generic() + .execute( + ActionRunnable.run( + f, + () -> repository.blobStore() + .blobContainer(repository.basePath()) + .writeBlobAtomic(BlobStoreRepository.INDEX_FILE_PREFIX + modifiedRepositoryData.getGenId(), serialized, true) + ) + ) + ); final String newSnapshotName = "snapshot-new"; final long beforeThrottledSnapshot = repository.threadPool().relativeTimeInNanos(); @@ -196,8 +219,13 @@ public List> getSettings() { } @Override - protected S3Repository createRepository(RepositoryMetadata metadata, NamedXContentRegistry registry, - ClusterService clusterService, BigArrays bigArrays, RecoverySettings recoverySettings) { + protected S3Repository createRepository( + RepositoryMetadata metadata, + NamedXContentRegistry registry, + ClusterService clusterService, + BigArrays bigArrays, + RecoverySettings recoverySettings + ) { return new S3Repository(metadata, registry, service, clusterService, bigArrays, recoverySettings) { @Override @@ -212,8 +240,7 @@ long getLargeBlobThresholdInBytes() { } @Override - void ensureMultiPartUploadSize(long blobSize) { - } + void ensureMultiPartUploadSize(long blobSize) {} }; } }; @@ -294,10 +321,9 @@ public void maybeTrack(final String request, Headers requestHeaders) { } private boolean isMultiPartUpload(String request) { - return Regex.simpleMatch("POST /*/*?uploads", request) || - Regex.simpleMatch("POST /*/*?*uploadId=*", request) || - Regex.simpleMatch("PUT /*/*?*uploadId=*", request); + return Regex.simpleMatch("POST /*/*?uploads", request) + || Regex.simpleMatch("POST /*/*?*uploadId=*", request) + || Regex.simpleMatch("PUT /*/*?*uploadId=*", request); } } } - diff --git a/plugins/repository-s3/src/internalClusterTest/java/org/elasticsearch/repositories/s3/S3RepositoryThirdPartyTests.java b/plugins/repository-s3/src/internalClusterTest/java/org/elasticsearch/repositories/s3/S3RepositoryThirdPartyTests.java index 60fa480377de9..bbfd17bf21399 100644 --- a/plugins/repository-s3/src/internalClusterTest/java/org/elasticsearch/repositories/s3/S3RepositoryThirdPartyTests.java +++ b/plugins/repository-s3/src/internalClusterTest/java/org/elasticsearch/repositories/s3/S3RepositoryThirdPartyTests.java @@ -50,15 +50,23 @@ protected void createRepository(String repoName) { } else { // only test different storage classes when running against the default endpoint, i.e. a genuine S3 service if (randomBoolean()) { - final String storageClass - = randomFrom("standard", "reduced_redundancy", "standard_ia", "onezone_ia", "intelligent_tiering"); + final String storageClass = randomFrom( + "standard", + "reduced_redundancy", + "standard_ia", + "onezone_ia", + "intelligent_tiering" + ); logger.info("--> using storage_class [{}]", storageClass); settings.put("storage_class", storageClass); } } - AcknowledgedResponse putRepositoryResponse = client().admin().cluster().preparePutRepository("test-repo") + AcknowledgedResponse putRepositoryResponse = client().admin() + .cluster() + .preparePutRepository("test-repo") .setType("s3") - .setSettings(settings).get(); + .setSettings(settings) + .get(); assertThat(putRepositoryResponse.isAcknowledged(), equalTo(true)); } } diff --git a/plugins/repository-s3/src/main/java/org/elasticsearch/repositories/s3/AmazonS3Reference.java b/plugins/repository-s3/src/main/java/org/elasticsearch/repositories/s3/AmazonS3Reference.java index cf48dcf1d04cf..684ff58c315e8 100644 --- a/plugins/repository-s3/src/main/java/org/elasticsearch/repositories/s3/AmazonS3Reference.java +++ b/plugins/repository-s3/src/main/java/org/elasticsearch/repositories/s3/AmazonS3Reference.java @@ -8,11 +8,10 @@ package org.elasticsearch.repositories.s3; -import org.elasticsearch.core.AbstractRefCounted; - import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3Client; +import org.elasticsearch.core.AbstractRefCounted; import org.elasticsearch.core.Releasable; /** diff --git a/plugins/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3BasicSessionCredentials.java b/plugins/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3BasicSessionCredentials.java index 79fec7ab8d1ce..2123fdc7168c0 100644 --- a/plugins/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3BasicSessionCredentials.java +++ b/plugins/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3BasicSessionCredentials.java @@ -34,9 +34,9 @@ public boolean equals(final Object o) { return false; } final S3BasicSessionCredentials that = (S3BasicSessionCredentials) o; - return sessionToken.equals(that.sessionToken) && - getAWSAccessKeyId().equals(that.getAWSAccessKeyId()) && - getAWSSecretKey().equals(that.getAWSSecretKey()); + return sessionToken.equals(that.sessionToken) + && getAWSAccessKeyId().equals(that.getAWSAccessKeyId()) + && getAWSSecretKey().equals(that.getAWSSecretKey()); } @Override diff --git a/plugins/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3BlobContainer.java b/plugins/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3BlobContainer.java index 7c75ef7ffb4e4..f4ee3967e49c4 100644 --- a/plugins/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3BlobContainer.java +++ b/plugins/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3BlobContainer.java @@ -22,13 +22,12 @@ import com.amazonaws.services.s3.model.S3ObjectSummary; import com.amazonaws.services.s3.model.UploadPartRequest; import com.amazonaws.services.s3.model.UploadPartResult; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.message.ParameterizedMessage; import org.apache.lucene.util.SetOnce; import org.elasticsearch.ExceptionsHelper; -import org.elasticsearch.core.CheckedConsumer; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.blobstore.BlobContainer; import org.elasticsearch.common.blobstore.BlobMetadata; @@ -39,9 +38,11 @@ import org.elasticsearch.common.blobstore.support.PlainBlobMetadata; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.collect.Iterators; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.core.CheckedConsumer; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.core.Tuple; import org.elasticsearch.repositories.blobstore.ChunkedBlobOutputStream; import java.io.ByteArrayInputStream; @@ -86,7 +87,7 @@ public boolean blobExists(String blobName) { try (AmazonS3Reference clientReference = blobStore.clientReference()) { return SocketAccess.doPrivileged(() -> clientReference.client().doesObjectExist(blobStore.bucket(), buildKey(blobName))); } catch (final Exception e) { - throw new BlobStoreException("Failed to check if blob [" + blobName +"] exists", e); + throw new BlobStoreException("Failed to check if blob [" + blobName + "] exists", e); } } @@ -133,72 +134,90 @@ public void writeBlob(String blobName, InputStream inputStream, long blobSize, b } @Override - public void writeBlob(String blobName, - boolean failIfAlreadyExists, - boolean atomic, - CheckedConsumer writer) throws IOException { + public void writeBlob(String blobName, boolean failIfAlreadyExists, boolean atomic, CheckedConsumer writer) + throws IOException { final String absoluteBlobKey = buildKey(blobName); - try (AmazonS3Reference clientReference = blobStore.clientReference(); - ChunkedBlobOutputStream out = new ChunkedBlobOutputStream<>(blobStore.bigArrays(), blobStore.bufferSizeInBytes()) { - - private final SetOnce uploadId = new SetOnce<>(); - - @Override - protected void flushBuffer() throws IOException { - flushBuffer(false); - } - - private void flushBuffer(boolean lastPart) throws IOException { - if (buffer.size() == 0) { - return; - } - if (flushedBytes == 0L) { - assert lastPart == false : "use single part upload if there's only a single part"; - uploadId.set(SocketAccess.doPrivileged(() -> - clientReference.client().initiateMultipartUpload(initiateMultiPartUpload(absoluteBlobKey)).getUploadId())); - if (Strings.isEmpty(uploadId.get())) { - throw new IOException("Failed to initialize multipart upload " + absoluteBlobKey); - } - } - assert lastPart == false || successful : "must only write last part if successful"; - final UploadPartRequest uploadRequest = createPartUploadRequest( - buffer.bytes().streamInput(), uploadId.get(), parts.size() + 1, absoluteBlobKey, buffer.size(), lastPart); - final UploadPartResult uploadResponse = - SocketAccess.doPrivileged(() -> clientReference.client().uploadPart(uploadRequest)); - finishPart(uploadResponse.getPartETag()); - } - - @Override - protected void onCompletion() throws IOException { - if (flushedBytes == 0L) { - writeBlob(blobName, buffer.bytes(), failIfAlreadyExists); - } else { - flushBuffer(true); - final CompleteMultipartUploadRequest complRequest = - new CompleteMultipartUploadRequest(blobStore.bucket(), absoluteBlobKey, uploadId.get(), parts); - complRequest.setRequestMetricCollector(blobStore.multiPartUploadMetricCollector); - SocketAccess.doPrivilegedVoid(() -> clientReference.client().completeMultipartUpload(complRequest)); - } - } - - @Override - protected void onFailure() { - if (Strings.hasText(uploadId.get())) { - abortMultiPartUpload(uploadId.get(), absoluteBlobKey); - } - } - }) { + try ( + AmazonS3Reference clientReference = blobStore.clientReference(); + ChunkedBlobOutputStream out = new ChunkedBlobOutputStream<>(blobStore.bigArrays(), blobStore.bufferSizeInBytes()) { + + private final SetOnce uploadId = new SetOnce<>(); + + @Override + protected void flushBuffer() throws IOException { + flushBuffer(false); + } + + private void flushBuffer(boolean lastPart) throws IOException { + if (buffer.size() == 0) { + return; + } + if (flushedBytes == 0L) { + assert lastPart == false : "use single part upload if there's only a single part"; + uploadId.set( + SocketAccess.doPrivileged( + () -> clientReference.client() + .initiateMultipartUpload(initiateMultiPartUpload(absoluteBlobKey)) + .getUploadId() + ) + ); + if (Strings.isEmpty(uploadId.get())) { + throw new IOException("Failed to initialize multipart upload " + absoluteBlobKey); + } + } + assert lastPart == false || successful : "must only write last part if successful"; + final UploadPartRequest uploadRequest = createPartUploadRequest( + buffer.bytes().streamInput(), + uploadId.get(), + parts.size() + 1, + absoluteBlobKey, + buffer.size(), + lastPart + ); + final UploadPartResult uploadResponse = SocketAccess.doPrivileged( + () -> clientReference.client().uploadPart(uploadRequest) + ); + finishPart(uploadResponse.getPartETag()); + } + + @Override + protected void onCompletion() throws IOException { + if (flushedBytes == 0L) { + writeBlob(blobName, buffer.bytes(), failIfAlreadyExists); + } else { + flushBuffer(true); + final CompleteMultipartUploadRequest complRequest = new CompleteMultipartUploadRequest( + blobStore.bucket(), + absoluteBlobKey, + uploadId.get(), + parts + ); + complRequest.setRequestMetricCollector(blobStore.multiPartUploadMetricCollector); + SocketAccess.doPrivilegedVoid(() -> clientReference.client().completeMultipartUpload(complRequest)); + } + } + + @Override + protected void onFailure() { + if (Strings.hasText(uploadId.get())) { + abortMultiPartUpload(uploadId.get(), absoluteBlobKey); + } + } + } + ) { writer.accept(out); out.markSuccess(); } } - private UploadPartRequest createPartUploadRequest(InputStream stream, - String uploadId, - int number, - String blobName, - long size, - boolean lastPart) { + private UploadPartRequest createPartUploadRequest( + InputStream stream, + String uploadId, + int number, + String blobName, + long size, + boolean lastPart + ) { final UploadPartRequest uploadRequest = new UploadPartRequest(); uploadRequest.setBucketName(blobStore.bucket()); uploadRequest.setKey(blobName); @@ -212,8 +231,7 @@ private UploadPartRequest createPartUploadRequest(InputStream stream, } private void abortMultiPartUpload(String uploadId, String blobName) { - final AbortMultipartUploadRequest abortRequest = - new AbortMultipartUploadRequest(blobStore.bucket(), blobName, uploadId); + final AbortMultipartUploadRequest abortRequest = new AbortMultipartUploadRequest(blobStore.bucket(), blobName, uploadId); try (AmazonS3Reference clientReference = blobStore.clientReference()) { SocketAccess.doPrivilegedVoid(() -> clientReference.client().abortMultipartUpload(abortRequest)); } @@ -347,9 +365,15 @@ private void deletePartition(AmazonS3Reference clientReference, List par // We are sending quiet mode requests so we can't use the deleted keys entry on the exception and instead // first remove all keys that were sent in the request and then add back those that ran into an exception. logger.warn( - () -> new ParameterizedMessage("Failed to delete some blobs {}", e.getErrors() - .stream().map(err -> "[" + err.getKey() + "][" + err.getCode() + "][" + err.getMessage() + "]") - .collect(Collectors.toList())), e); + () -> new ParameterizedMessage( + "Failed to delete some blobs {}", + e.getErrors() + .stream() + .map(err -> "[" + err.getKey() + "][" + err.getCode() + "][" + err.getMessage() + "]") + .collect(Collectors.toList()) + ), + e + ); aex.set(ExceptionsHelper.useOrSuppress(aex.get(), e)); } catch (AmazonClientException e) { // The AWS client threw any unexpected exception and did not execute the request at all so we do not @@ -365,8 +389,7 @@ private static DeleteObjectsRequest bulkDelete(String bucket, List blobs @Override public Map listBlobsByPrefix(@Nullable String blobNamePrefix) throws IOException { try (AmazonS3Reference clientReference = blobStore.clientReference()) { - return executeListing(clientReference, listObjectsRequest(blobNamePrefix == null ? keyPath : buildKey(blobNamePrefix))) - .stream() + return executeListing(clientReference, listObjectsRequest(blobNamePrefix == null ? keyPath : buildKey(blobNamePrefix))).stream() .flatMap(listing -> listing.getObjectSummaries().stream()) .map(summary -> new PlainBlobMetadata(summary.getKey().substring(keyPath.length()), summary.getSize())) .collect(Collectors.toMap(PlainBlobMetadata::name, Function.identity())); @@ -384,16 +407,16 @@ public Map listBlobs() throws IOException { public Map children() throws IOException { try (AmazonS3Reference clientReference = blobStore.clientReference()) { return executeListing(clientReference, listObjectsRequest(keyPath)).stream().flatMap(listing -> { - assert listing.getObjectSummaries().stream().noneMatch(s -> { - for (String commonPrefix : listing.getCommonPrefixes()) { - if (s.getKey().substring(keyPath.length()).startsWith(commonPrefix)) { - return true; - } + assert listing.getObjectSummaries().stream().noneMatch(s -> { + for (String commonPrefix : listing.getCommonPrefixes()) { + if (s.getKey().substring(keyPath.length()).startsWith(commonPrefix)) { + return true; } - return false; - }) : "Response contained children for listed common prefixes."; - return listing.getCommonPrefixes().stream(); - }) + } + return false; + }) : "Response contained children for listed common prefixes."; + return listing.getCommonPrefixes().stream(); + }) .map(prefix -> prefix.substring(keyPath.length())) .filter(name -> name.isEmpty() == false) // Stripping the trailing slash off of the common prefix @@ -426,7 +449,9 @@ private static List executeListing(AmazonS3Reference clientRefere } private ListObjectsRequest listObjectsRequest(String keyPath) { - return new ListObjectsRequest().withBucketName(blobStore.bucket()).withPrefix(keyPath).withDelimiter("/") + return new ListObjectsRequest().withBucketName(blobStore.bucket()) + .withPrefix(keyPath) + .withDelimiter("/") .withRequestMetricCollector(blobStore.listMetricCollector); } @@ -437,10 +462,8 @@ private String buildKey(String blobName) { /** * Uploads a blob using a single upload request */ - void executeSingleUpload(final S3BlobStore blobStore, - final String blobName, - final InputStream input, - final long blobSize) throws IOException { + void executeSingleUpload(final S3BlobStore blobStore, final String blobName, final InputStream input, final long blobSize) + throws IOException { // Extra safety checks if (blobSize > MAX_FILE_SIZE.getBytes()) { @@ -461,9 +484,7 @@ void executeSingleUpload(final S3BlobStore blobStore, putRequest.setRequestMetricCollector(blobStore.putMetricCollector); try (AmazonS3Reference clientReference = blobStore.clientReference()) { - SocketAccess.doPrivilegedVoid(() -> { - clientReference.client().putObject(putRequest); - }); + SocketAccess.doPrivilegedVoid(() -> { clientReference.client().putObject(putRequest); }); } catch (final AmazonClientException e) { throw new IOException("Unable to upload object [" + blobName + "] using a single upload", e); } @@ -472,10 +493,8 @@ void executeSingleUpload(final S3BlobStore blobStore, /** * Uploads a blob using multipart upload requests. */ - void executeMultipartUpload(final S3BlobStore blobStore, - final String blobName, - final InputStream input, - final long blobSize) throws IOException { + void executeMultipartUpload(final S3BlobStore blobStore, final String blobName, final InputStream input, final long blobSize) + throws IOException { ensureMultiPartUploadSize(blobSize); final long partSize = blobStore.bufferSizeInBytes(); @@ -494,8 +513,11 @@ void executeMultipartUpload(final S3BlobStore blobStore, boolean success = false; try (AmazonS3Reference clientReference = blobStore.clientReference()) { - uploadId.set(SocketAccess.doPrivileged(() -> - clientReference.client().initiateMultipartUpload(initiateMultiPartUpload(blobName)).getUploadId())); + uploadId.set( + SocketAccess.doPrivileged( + () -> clientReference.client().initiateMultipartUpload(initiateMultiPartUpload(blobName)).getUploadId() + ) + ); if (Strings.isEmpty(uploadId.get())) { throw new IOException("Failed to initialize multipart upload " + blobName); } @@ -505,8 +527,14 @@ void executeMultipartUpload(final S3BlobStore blobStore, long bytesCount = 0; for (int i = 1; i <= nbParts; i++) { final boolean lastPart = i == nbParts; - final UploadPartRequest uploadRequest = - createPartUploadRequest(input, uploadId.get(), i, blobName, lastPart ? lastPartSize : partSize, lastPart); + final UploadPartRequest uploadRequest = createPartUploadRequest( + input, + uploadId.get(), + i, + blobName, + lastPart ? lastPartSize : partSize, + lastPart + ); bytesCount += uploadRequest.getPartSize(); final UploadPartResult uploadResponse = SocketAccess.doPrivileged(() -> clientReference.client().uploadPart(uploadRequest)); @@ -514,12 +542,17 @@ void executeMultipartUpload(final S3BlobStore blobStore, } if (bytesCount != blobSize) { - throw new IOException("Failed to execute multipart upload for [" + blobName + "], expected " + blobSize - + "bytes sent but got " + bytesCount); + throw new IOException( + "Failed to execute multipart upload for [" + blobName + "], expected " + blobSize + "bytes sent but got " + bytesCount + ); } - final CompleteMultipartUploadRequest complRequest = new CompleteMultipartUploadRequest(bucketName, blobName, uploadId.get(), - parts); + final CompleteMultipartUploadRequest complRequest = new CompleteMultipartUploadRequest( + bucketName, + blobName, + uploadId.get(), + parts + ); complRequest.setRequestMetricCollector(blobStore.multiPartUploadMetricCollector); SocketAccess.doPrivilegedVoid(() -> clientReference.client().completeMultipartUpload(complRequest)); success = true; @@ -536,12 +569,14 @@ void executeMultipartUpload(final S3BlobStore blobStore, // non-static, package private for testing void ensureMultiPartUploadSize(final long blobSize) { if (blobSize > MAX_FILE_SIZE_USING_MULTIPART.getBytes()) { - throw new IllegalArgumentException("Multipart upload request size [" + blobSize - + "] can't be larger than " + MAX_FILE_SIZE_USING_MULTIPART); + throw new IllegalArgumentException( + "Multipart upload request size [" + blobSize + "] can't be larger than " + MAX_FILE_SIZE_USING_MULTIPART + ); } if (blobSize < MIN_PART_SIZE_USING_MULTIPART.getBytes()) { - throw new IllegalArgumentException("Multipart upload request size [" + blobSize - + "] can't be smaller than " + MIN_PART_SIZE_USING_MULTIPART); + throw new IllegalArgumentException( + "Multipart upload request size [" + blobSize + "] can't be smaller than " + MIN_PART_SIZE_USING_MULTIPART + ); } } diff --git a/plugins/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3BlobStore.java b/plugins/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3BlobStore.java index 90a86c4910ba5..473f76c8141f0 100644 --- a/plugins/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3BlobStore.java +++ b/plugins/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3BlobStore.java @@ -14,6 +14,7 @@ import com.amazonaws.services.s3.model.CannedAccessControlList; import com.amazonaws.services.s3.model.StorageClass; import com.amazonaws.util.AWSRequestMetrics; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.cluster.metadata.RepositoryMetadata; @@ -57,9 +58,16 @@ class S3BlobStore implements BlobStore { final RequestMetricCollector putMetricCollector; final RequestMetricCollector multiPartUploadMetricCollector; - S3BlobStore(S3Service service, String bucket, boolean serverSideEncryption, - ByteSizeValue bufferSize, String cannedACL, String storageClass, - RepositoryMetadata repositoryMetadata, BigArrays bigArrays) { + S3BlobStore( + S3Service service, + String bucket, + boolean serverSideEncryption, + ByteSizeValue bufferSize, + String cannedACL, + String storageClass, + RepositoryMetadata repositoryMetadata, + BigArrays bigArrays + ) { this.service = service; this.bigArrays = bigArrays; this.bucket = bucket; @@ -92,8 +100,7 @@ public void collectMetrics(Request request) { this.multiPartUploadMetricCollector = new IgnoreNoResponseMetricsCollector() { @Override public void collectMetrics(Request request) { - assert request.getHttpMethod().name().equals("PUT") - || request.getHttpMethod().name().equals("POST"); + assert request.getHttpMethod().name().equals("PUT") || request.getHttpMethod().name().equals("POST"); stats.postCount.addAndGet(getRequestCount(request)); } }; @@ -114,8 +121,7 @@ public final void collectMetrics(Request request, Response response) { } private long getRequestCount(Request request) { - Number requestCount = request.getAWSRequestMetrics().getTimingInfo() - .getCounter(AWSRequestMetrics.Field.RequestCount.name()); + Number requestCount = request.getAWSRequestMetrics().getTimingInfo().getCounter(AWSRequestMetrics.Field.RequestCount.name()); if (requestCount == null) { logger.warn("Expected request count to be tracked for request [{}] but found not count.", request); return 0L; diff --git a/plugins/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3ClientSettings.java b/plugins/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3ClientSettings.java index d6e596a90905f..ebdd8f9eaf326 100644 --- a/plugins/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3ClientSettings.java +++ b/plugins/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3ClientSettings.java @@ -10,6 +10,7 @@ import com.amazonaws.ClientConfiguration; import com.amazonaws.Protocol; + import org.elasticsearch.common.settings.SecureSetting; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Setting; @@ -47,68 +48,116 @@ final class S3ClientSettings { private static final String PLACEHOLDER_CLIENT = "placeholder"; /** The access key (ie login id) for connecting to s3. */ - static final Setting.AffixSetting ACCESS_KEY_SETTING = Setting.affixKeySetting(PREFIX, "access_key", - key -> SecureSetting.secureString(key, null)); + static final Setting.AffixSetting ACCESS_KEY_SETTING = Setting.affixKeySetting( + PREFIX, + "access_key", + key -> SecureSetting.secureString(key, null) + ); /** The secret key (ie password) for connecting to s3. */ - static final Setting.AffixSetting SECRET_KEY_SETTING = Setting.affixKeySetting(PREFIX, "secret_key", - key -> SecureSetting.secureString(key, null)); + static final Setting.AffixSetting SECRET_KEY_SETTING = Setting.affixKeySetting( + PREFIX, + "secret_key", + key -> SecureSetting.secureString(key, null) + ); /** The secret key (ie password) for connecting to s3. */ - static final Setting.AffixSetting SESSION_TOKEN_SETTING = Setting.affixKeySetting(PREFIX, "session_token", - key -> SecureSetting.secureString(key, null)); + static final Setting.AffixSetting SESSION_TOKEN_SETTING = Setting.affixKeySetting( + PREFIX, + "session_token", + key -> SecureSetting.secureString(key, null) + ); /** An override for the s3 endpoint to connect to. */ - static final Setting.AffixSetting ENDPOINT_SETTING = Setting.affixKeySetting(PREFIX, "endpoint", - key -> new Setting<>(key, "", s -> s.toLowerCase(Locale.ROOT), Property.NodeScope)); + static final Setting.AffixSetting ENDPOINT_SETTING = Setting.affixKeySetting( + PREFIX, + "endpoint", + key -> new Setting<>(key, "", s -> s.toLowerCase(Locale.ROOT), Property.NodeScope) + ); /** The protocol to use to connect to s3. */ - static final Setting.AffixSetting PROTOCOL_SETTING = Setting.affixKeySetting(PREFIX, "protocol", - key -> new Setting<>(key, "https", s -> Protocol.valueOf(s.toUpperCase(Locale.ROOT)), Property.NodeScope)); + static final Setting.AffixSetting PROTOCOL_SETTING = Setting.affixKeySetting( + PREFIX, + "protocol", + key -> new Setting<>(key, "https", s -> Protocol.valueOf(s.toUpperCase(Locale.ROOT)), Property.NodeScope) + ); /** The host name of a proxy to connect to s3 through. */ - static final Setting.AffixSetting PROXY_HOST_SETTING = Setting.affixKeySetting(PREFIX, "proxy.host", - key -> Setting.simpleString(key, Property.NodeScope)); + static final Setting.AffixSetting PROXY_HOST_SETTING = Setting.affixKeySetting( + PREFIX, + "proxy.host", + key -> Setting.simpleString(key, Property.NodeScope) + ); /** The port of a proxy to connect to s3 through. */ - static final Setting.AffixSetting PROXY_PORT_SETTING = Setting.affixKeySetting(PREFIX, "proxy.port", - key -> Setting.intSetting(key, 80, 0, 1<<16, Property.NodeScope)); + static final Setting.AffixSetting PROXY_PORT_SETTING = Setting.affixKeySetting( + PREFIX, + "proxy.port", + key -> Setting.intSetting(key, 80, 0, 1 << 16, Property.NodeScope) + ); /** The username of a proxy to connect to s3 through. */ - static final Setting.AffixSetting PROXY_USERNAME_SETTING = Setting.affixKeySetting(PREFIX, "proxy.username", - key -> SecureSetting.secureString(key, null)); + static final Setting.AffixSetting PROXY_USERNAME_SETTING = Setting.affixKeySetting( + PREFIX, + "proxy.username", + key -> SecureSetting.secureString(key, null) + ); /** The password of a proxy to connect to s3 through. */ - static final Setting.AffixSetting PROXY_PASSWORD_SETTING = Setting.affixKeySetting(PREFIX, "proxy.password", - key -> SecureSetting.secureString(key, null)); + static final Setting.AffixSetting PROXY_PASSWORD_SETTING = Setting.affixKeySetting( + PREFIX, + "proxy.password", + key -> SecureSetting.secureString(key, null) + ); /** The socket timeout for connecting to s3. */ - static final Setting.AffixSetting READ_TIMEOUT_SETTING = Setting.affixKeySetting(PREFIX, "read_timeout", - key -> Setting.timeSetting(key, TimeValue.timeValueMillis(ClientConfiguration.DEFAULT_SOCKET_TIMEOUT), Property.NodeScope)); + static final Setting.AffixSetting READ_TIMEOUT_SETTING = Setting.affixKeySetting( + PREFIX, + "read_timeout", + key -> Setting.timeSetting(key, TimeValue.timeValueMillis(ClientConfiguration.DEFAULT_SOCKET_TIMEOUT), Property.NodeScope) + ); /** The number of retries to use when an s3 request fails. */ - static final Setting.AffixSetting MAX_RETRIES_SETTING = Setting.affixKeySetting(PREFIX, "max_retries", - key -> Setting.intSetting(key, ClientConfiguration.DEFAULT_RETRY_POLICY.getMaxErrorRetry(), 0, Property.NodeScope)); + static final Setting.AffixSetting MAX_RETRIES_SETTING = Setting.affixKeySetting( + PREFIX, + "max_retries", + key -> Setting.intSetting(key, ClientConfiguration.DEFAULT_RETRY_POLICY.getMaxErrorRetry(), 0, Property.NodeScope) + ); /** Whether retries should be throttled (ie use backoff). */ - static final Setting.AffixSetting USE_THROTTLE_RETRIES_SETTING = Setting.affixKeySetting(PREFIX, "use_throttle_retries", - key -> Setting.boolSetting(key, ClientConfiguration.DEFAULT_THROTTLE_RETRIES, Property.NodeScope)); + static final Setting.AffixSetting USE_THROTTLE_RETRIES_SETTING = Setting.affixKeySetting( + PREFIX, + "use_throttle_retries", + key -> Setting.boolSetting(key, ClientConfiguration.DEFAULT_THROTTLE_RETRIES, Property.NodeScope) + ); /** Whether the s3 client should use path style access. */ - static final Setting.AffixSetting USE_PATH_STYLE_ACCESS = Setting.affixKeySetting(PREFIX, "path_style_access", - key -> Setting.boolSetting(key, false, Property.NodeScope)); + static final Setting.AffixSetting USE_PATH_STYLE_ACCESS = Setting.affixKeySetting( + PREFIX, + "path_style_access", + key -> Setting.boolSetting(key, false, Property.NodeScope) + ); /** Whether chunked encoding should be disabled or not (Default is false). */ - static final Setting.AffixSetting DISABLE_CHUNKED_ENCODING = Setting.affixKeySetting(PREFIX, "disable_chunked_encoding", - key -> Setting.boolSetting(key, false, Property.NodeScope)); + static final Setting.AffixSetting DISABLE_CHUNKED_ENCODING = Setting.affixKeySetting( + PREFIX, + "disable_chunked_encoding", + key -> Setting.boolSetting(key, false, Property.NodeScope) + ); /** An override for the s3 region to use for signing requests. */ - static final Setting.AffixSetting REGION = Setting.affixKeySetting(PREFIX, "region", - key -> new Setting<>(key, "", Function.identity(), Property.NodeScope)); + static final Setting.AffixSetting REGION = Setting.affixKeySetting( + PREFIX, + "region", + key -> new Setting<>(key, "", Function.identity(), Property.NodeScope) + ); /** An override for the signer to use. */ - static final Setting.AffixSetting SIGNER_OVERRIDE = Setting.affixKeySetting(PREFIX, "signer_override", - key -> new Setting<>(key, "", Function.identity(), Property.NodeScope)); + static final Setting.AffixSetting SIGNER_OVERRIDE = Setting.affixKeySetting( + PREFIX, + "signer_override", + key -> new Setting<>(key, "", Function.identity(), Property.NodeScope) + ); /** Credentials to authenticate with s3. */ final S3BasicCredentials credentials; @@ -154,10 +203,22 @@ final class S3ClientSettings { /** Signer override to use or empty string to use default. */ final String signerOverride; - private S3ClientSettings(S3BasicCredentials credentials, String endpoint, Protocol protocol, - String proxyHost, int proxyPort, String proxyUsername, String proxyPassword, - int readTimeoutMillis, int maxRetries, boolean throttleRetries, - boolean pathStyleAccess, boolean disableChunkedEncoding, String region, String signerOverride) { + private S3ClientSettings( + S3BasicCredentials credentials, + String endpoint, + Protocol protocol, + String proxyHost, + int proxyPort, + String proxyUsername, + String proxyPassword, + int readTimeoutMillis, + int maxRetries, + boolean throttleRetries, + boolean pathStyleAccess, + boolean disableChunkedEncoding, + String region, + String signerOverride + ) { this.credentials = credentials; this.endpoint = endpoint; this.protocol = protocol; @@ -182,28 +243,39 @@ private S3ClientSettings(S3BasicCredentials credentials, String endpoint, Protoc */ S3ClientSettings refine(Settings repositorySettings) { // Normalize settings to placeholder client settings prefix so that we can use the affix settings directly - final Settings normalizedSettings = - Settings.builder().put(repositorySettings).normalizePrefix(PREFIX + PLACEHOLDER_CLIENT + '.').build(); + final Settings normalizedSettings = Settings.builder() + .put(repositorySettings) + .normalizePrefix(PREFIX + PLACEHOLDER_CLIENT + '.') + .build(); final String newEndpoint = getRepoSettingOrDefault(ENDPOINT_SETTING, normalizedSettings, endpoint); final Protocol newProtocol = getRepoSettingOrDefault(PROTOCOL_SETTING, normalizedSettings, protocol); final String newProxyHost = getRepoSettingOrDefault(PROXY_HOST_SETTING, normalizedSettings, proxyHost); final int newProxyPort = getRepoSettingOrDefault(PROXY_PORT_SETTING, normalizedSettings, proxyPort); final int newReadTimeoutMillis = Math.toIntExact( - getRepoSettingOrDefault(READ_TIMEOUT_SETTING, normalizedSettings, TimeValue.timeValueMillis(readTimeoutMillis)).millis()); + getRepoSettingOrDefault(READ_TIMEOUT_SETTING, normalizedSettings, TimeValue.timeValueMillis(readTimeoutMillis)).millis() + ); final int newMaxRetries = getRepoSettingOrDefault(MAX_RETRIES_SETTING, normalizedSettings, maxRetries); final boolean newThrottleRetries = getRepoSettingOrDefault(USE_THROTTLE_RETRIES_SETTING, normalizedSettings, throttleRetries); final boolean newPathStyleAccess = getRepoSettingOrDefault(USE_PATH_STYLE_ACCESS, normalizedSettings, pathStyleAccess); final boolean newDisableChunkedEncoding = getRepoSettingOrDefault( - DISABLE_CHUNKED_ENCODING, normalizedSettings, disableChunkedEncoding); + DISABLE_CHUNKED_ENCODING, + normalizedSettings, + disableChunkedEncoding + ); final String newRegion = getRepoSettingOrDefault(REGION, normalizedSettings, region); final String newSignerOverride = getRepoSettingOrDefault(SIGNER_OVERRIDE, normalizedSettings, signerOverride); - if (Objects.equals(endpoint, newEndpoint) && protocol == newProtocol && Objects.equals(proxyHost, newProxyHost) - && proxyPort == newProxyPort && newReadTimeoutMillis == readTimeoutMillis && maxRetries == newMaxRetries + if (Objects.equals(endpoint, newEndpoint) + && protocol == newProtocol + && Objects.equals(proxyHost, newProxyHost) + && proxyPort == newProxyPort + && newReadTimeoutMillis == readTimeoutMillis + && maxRetries == newMaxRetries && newThrottleRetries == throttleRetries && newPathStyleAccess == pathStyleAccess && newDisableChunkedEncoding == disableChunkedEncoding - && Objects.equals(region, newRegion) && Objects.equals(signerOverride, newSignerOverride)) { + && Objects.equals(region, newRegion) + && Objects.equals(signerOverride, newSignerOverride)) { return this; } return new S3ClientSettings( @@ -244,9 +316,11 @@ static Map load(Settings settings) { } private static S3BasicCredentials loadCredentials(Settings settings, String clientName) { - try (SecureString accessKey = getConfigValue(settings, clientName, ACCESS_KEY_SETTING); - SecureString secretKey = getConfigValue(settings, clientName, SECRET_KEY_SETTING); - SecureString sessionToken = getConfigValue(settings, clientName, SESSION_TOKEN_SETTING)) { + try ( + SecureString accessKey = getConfigValue(settings, clientName, ACCESS_KEY_SETTING); + SecureString secretKey = getConfigValue(settings, clientName, SECRET_KEY_SETTING); + SecureString sessionToken = getConfigValue(settings, clientName, SESSION_TOKEN_SETTING) + ) { if (accessKey.length() != 0) { if (secretKey.length() != 0) { if (sessionToken.length() != 0) { @@ -272,8 +346,10 @@ private static S3BasicCredentials loadCredentials(Settings settings, String clie // pkg private for tests /** Parse settings for a single client. */ static S3ClientSettings getClientSettings(final Settings settings, final String clientName) { - try (SecureString proxyUsername = getConfigValue(settings, clientName, PROXY_USERNAME_SETTING); - SecureString proxyPassword = getConfigValue(settings, clientName, PROXY_PASSWORD_SETTING)) { + try ( + SecureString proxyUsername = getConfigValue(settings, clientName, PROXY_USERNAME_SETTING); + SecureString proxyPassword = getConfigValue(settings, clientName, PROXY_PASSWORD_SETTING) + ) { return new S3ClientSettings( S3ClientSettings.loadCredentials(settings, clientName), getConfigValue(settings, clientName, ENDPOINT_SETTING), @@ -302,29 +378,41 @@ public boolean equals(final Object o) { return false; } final S3ClientSettings that = (S3ClientSettings) o; - return proxyPort == that.proxyPort && - readTimeoutMillis == that.readTimeoutMillis && - maxRetries == that.maxRetries && - throttleRetries == that.throttleRetries && - Objects.equals(credentials, that.credentials) && - Objects.equals(endpoint, that.endpoint) && - protocol == that.protocol && - Objects.equals(proxyHost, that.proxyHost) && - Objects.equals(proxyUsername, that.proxyUsername) && - Objects.equals(proxyPassword, that.proxyPassword) && - Objects.equals(disableChunkedEncoding, that.disableChunkedEncoding) && - Objects.equals(region, that.region) && - Objects.equals(signerOverride, that.signerOverride); + return proxyPort == that.proxyPort + && readTimeoutMillis == that.readTimeoutMillis + && maxRetries == that.maxRetries + && throttleRetries == that.throttleRetries + && Objects.equals(credentials, that.credentials) + && Objects.equals(endpoint, that.endpoint) + && protocol == that.protocol + && Objects.equals(proxyHost, that.proxyHost) + && Objects.equals(proxyUsername, that.proxyUsername) + && Objects.equals(proxyPassword, that.proxyPassword) + && Objects.equals(disableChunkedEncoding, that.disableChunkedEncoding) + && Objects.equals(region, that.region) + && Objects.equals(signerOverride, that.signerOverride); } @Override public int hashCode() { - return Objects.hash(credentials, endpoint, protocol, proxyHost, proxyPort, proxyUsername, proxyPassword, - readTimeoutMillis, maxRetries, throttleRetries, disableChunkedEncoding, region, signerOverride); + return Objects.hash( + credentials, + endpoint, + protocol, + proxyHost, + proxyPort, + proxyUsername, + proxyPassword, + readTimeoutMillis, + maxRetries, + throttleRetries, + disableChunkedEncoding, + region, + signerOverride + ); } - private static T getConfigValue(Settings settings, String clientName, - Setting.AffixSetting clientSetting) { + private static T getConfigValue(Settings settings, String clientName, Setting.AffixSetting clientSetting) { final Setting concreteSetting = clientSetting.getConcreteSettingForNamespace(clientName); return concreteSetting.get(settings); } diff --git a/plugins/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3Repository.java b/plugins/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3Repository.java index de6f2df67112e..89706c813eb70 100644 --- a/plugins/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3Repository.java +++ b/plugins/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3Repository.java @@ -21,8 +21,8 @@ import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.BigArrays; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.indices.recovery.RecoverySettings; import org.elasticsearch.monitor.jvm.JvmInfo; import org.elasticsearch.repositories.FinalizeSnapshotContext; @@ -66,11 +66,10 @@ class S3Repository extends MeteredBlobStoreRepository { private static final ByteSizeValue DEFAULT_BUFFER_SIZE = new ByteSizeValue( Math.max( ByteSizeUnit.MB.toBytes(5), // minimum value - Math.min( - ByteSizeUnit.MB.toBytes(100), - JvmInfo.jvmInfo().getMem().getHeapMax().getBytes() / 20)), - ByteSizeUnit.BYTES); - + Math.min(ByteSizeUnit.MB.toBytes(100), JvmInfo.jvmInfo().getMem().getHeapMax().getBytes() / 20) + ), + ByteSizeUnit.BYTES + ); static final Setting BUCKET_SETTING = Setting.simpleString("bucket"); @@ -108,14 +107,22 @@ class S3Repository extends MeteredBlobStoreRepository { * to upload each part in its own request. Note that setting a buffer size lower than 5mb is not allowed since it will prevents the * use of the Multipart API and may result in upload errors. Defaults to the minimum between 100MB and 5% of the heap size. */ - static final Setting BUFFER_SIZE_SETTING = - Setting.byteSizeSetting("buffer_size", DEFAULT_BUFFER_SIZE, MIN_PART_SIZE_USING_MULTIPART, MAX_PART_SIZE_USING_MULTIPART); + static final Setting BUFFER_SIZE_SETTING = Setting.byteSizeSetting( + "buffer_size", + DEFAULT_BUFFER_SIZE, + MIN_PART_SIZE_USING_MULTIPART, + MAX_PART_SIZE_USING_MULTIPART + ); /** * Big files can be broken down into chunks during snapshotting if needed. Defaults to 5tb. */ - static final Setting CHUNK_SIZE_SETTING = Setting.byteSizeSetting("chunk_size", MAX_FILE_SIZE_USING_MULTIPART, - new ByteSizeValue(5, ByteSizeUnit.MB), MAX_FILE_SIZE_USING_MULTIPART); + static final Setting CHUNK_SIZE_SETTING = Setting.byteSizeSetting( + "chunk_size", + MAX_FILE_SIZE_USING_MULTIPART, + new ByteSizeValue(5, ByteSizeUnit.MB), + MAX_FILE_SIZE_USING_MULTIPART + ); /** * Sets the S3 storage class type for the backup files. Values may be standard, reduced_redundancy, @@ -146,7 +153,8 @@ class S3Repository extends MeteredBlobStoreRepository { "cooldown_period", new TimeValue(3, TimeUnit.MINUTES), new TimeValue(0, TimeUnit.MILLISECONDS), - Setting.Property.Dynamic); + Setting.Property.Dynamic + ); /** * Specifies the path within bucket to repository data. Defaults to root directory. @@ -182,14 +190,17 @@ class S3Repository extends MeteredBlobStoreRepository { final S3Service service, final ClusterService clusterService, final BigArrays bigArrays, - final RecoverySettings recoverySettings) { - super(metadata, + final RecoverySettings recoverySettings + ) { + super( + metadata, namedXContentRegistry, clusterService, bigArrays, recoverySettings, buildBasePath(metadata), - buildLocation(metadata)); + buildLocation(metadata) + ); this.service = service; // Parse and validate the user's S3 Storage Class setting @@ -203,8 +214,17 @@ class S3Repository extends MeteredBlobStoreRepository { // We make sure that chunkSize is bigger or equal than/to bufferSize if (this.chunkSize.getBytes() < bufferSize.getBytes()) { - throw new RepositoryException(metadata.name(), CHUNK_SIZE_SETTING.getKey() + " (" + this.chunkSize + - ") can't be lower than " + BUFFER_SIZE_SETTING.getKey() + " (" + bufferSize + ")."); + throw new RepositoryException( + metadata.name(), + CHUNK_SIZE_SETTING.getKey() + + " (" + + this.chunkSize + + ") can't be lower than " + + BUFFER_SIZE_SETTING.getKey() + + " (" + + bufferSize + + ")." + ); } this.serverSideEncryption = SERVER_SIDE_ENCRYPTION_SETTING.get(metadata.settings()); @@ -215,18 +235,18 @@ class S3Repository extends MeteredBlobStoreRepository { coolDown = COOLDOWN_PERIOD.get(metadata.settings()); logger.debug( - "using bucket [{}], chunk_size [{}], server_side_encryption [{}], buffer_size [{}], cannedACL [{}], storageClass [{}]", - bucket, - chunkSize, - serverSideEncryption, - bufferSize, - cannedACL, - storageClass); + "using bucket [{}], chunk_size [{}], server_side_encryption [{}], buffer_size [{}], cannedACL [{}], storageClass [{}]", + bucket, + chunkSize, + serverSideEncryption, + bufferSize, + cannedACL, + storageClass + ); } private static Map buildLocation(RepositoryMetadata metadata) { - return Map.of("base_path", BASE_PATH_SETTING.get(metadata.settings()), - "bucket", BUCKET_SETTING.get(metadata.settings())); + return Map.of("base_path", BASE_PATH_SETTING.get(metadata.settings()), "bucket", BUCKET_SETTING.get(metadata.settings())); } /** @@ -251,8 +271,12 @@ public void finalizeSnapshot(FinalizeSnapshotContext finalizeSnapshotContext) { } @Override - public void deleteSnapshots(Collection snapshotIds, long repositoryStateId, Version repositoryMetaVersion, - ActionListener listener) { + public void deleteSnapshots( + Collection snapshotIds, + long repositoryStateId, + Version repositoryMetaVersion, + ActionListener listener + ) { if (SnapshotsService.useShardGenerations(repositoryMetaVersion) == false) { listener = delayedListener(listener); } @@ -273,8 +297,12 @@ private ActionListener delayedListener(ActionListener listener) { public void onResponse(T response) { logCooldownInfo(); final Scheduler.Cancellable existing = finalizationFuture.getAndSet( - threadPool.schedule(ActionRunnable.wrap(wrappedListener, l -> l.onResponse(response)), - coolDown, ThreadPool.Names.SNAPSHOT)); + threadPool.schedule( + ActionRunnable.wrap(wrappedListener, l -> l.onResponse(response)), + coolDown, + ThreadPool.Names.SNAPSHOT + ) + ); assert existing == null : "Already have an ongoing finalization " + finalizationFuture; } @@ -282,19 +310,24 @@ public void onResponse(T response) { public void onFailure(Exception e) { logCooldownInfo(); final Scheduler.Cancellable existing = finalizationFuture.getAndSet( - threadPool.schedule(ActionRunnable.wrap(wrappedListener, l -> l.onFailure(e)), coolDown, ThreadPool.Names.SNAPSHOT)); + threadPool.schedule(ActionRunnable.wrap(wrappedListener, l -> l.onFailure(e)), coolDown, ThreadPool.Names.SNAPSHOT) + ); assert existing == null : "Already have an ongoing finalization " + finalizationFuture; } }; } private void logCooldownInfo() { - logger.info("Sleeping for [{}] after modifying repository [{}] because it contains snapshots older than version [{}]" + - " and therefore is using a backwards compatible metadata format that requires this cooldown period to avoid " + - "repository corruption. To get rid of this message and move to the new repository metadata format, either remove " + - "all snapshots older than version [{}] from the repository or create a new repository at an empty location.", - coolDown, metadata.name(), SnapshotsService.SHARD_GEN_IN_REPO_DATA_VERSION, - SnapshotsService.SHARD_GEN_IN_REPO_DATA_VERSION); + logger.info( + "Sleeping for [{}] after modifying repository [{}] because it contains snapshots older than version [{}]" + + " and therefore is using a backwards compatible metadata format that requires this cooldown period to avoid " + + "repository corruption. To get rid of this message and move to the new repository metadata format, either remove " + + "all snapshots older than version [{}] from the repository or create a new repository at an empty location.", + coolDown, + metadata.name(), + SnapshotsService.SHARD_GEN_IN_REPO_DATA_VERSION, + SnapshotsService.SHARD_GEN_IN_REPO_DATA_VERSION + ); } private static BlobPath buildBasePath(RepositoryMetadata metadata) { diff --git a/plugins/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3RepositoryPlugin.java b/plugins/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3RepositoryPlugin.java index da0d5765b121f..4f0364cfff35c 100644 --- a/plugins/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3RepositoryPlugin.java +++ b/plugins/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3RepositoryPlugin.java @@ -9,6 +9,7 @@ package org.elasticsearch.repositories.s3; import com.amazonaws.util.json.Jackson; + import org.elasticsearch.SpecialPermission; import org.elasticsearch.cluster.metadata.RepositoryMetadata; import org.elasticsearch.cluster.service.ClusterService; @@ -72,16 +73,23 @@ protected S3Repository createRepository( final NamedXContentRegistry registry, final ClusterService clusterService, final BigArrays bigArrays, - final RecoverySettings recoverySettings) { + final RecoverySettings recoverySettings + ) { return new S3Repository(metadata, registry, service, clusterService, bigArrays, recoverySettings); } @Override - public Map getRepositories(final Environment env, final NamedXContentRegistry registry, - final ClusterService clusterService, final BigArrays bigArrays, - final RecoverySettings recoverySettings) { - return Collections.singletonMap(S3Repository.TYPE, metadata -> createRepository(metadata, registry, clusterService, bigArrays, - recoverySettings)); + public Map getRepositories( + final Environment env, + final NamedXContentRegistry registry, + final ClusterService clusterService, + final BigArrays bigArrays, + final RecoverySettings recoverySettings + ) { + return Collections.singletonMap( + S3Repository.TYPE, + metadata -> createRepository(metadata, registry, clusterService, bigArrays, recoverySettings) + ); } @Override @@ -102,7 +110,8 @@ public List> getSettings() { S3ClientSettings.USE_THROTTLE_RETRIES_SETTING, S3ClientSettings.USE_PATH_STYLE_ACCESS, S3ClientSettings.SIGNER_OVERRIDE, - S3ClientSettings.REGION); + S3ClientSettings.REGION + ); } @Override diff --git a/plugins/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3RetryingInputStream.java b/plugins/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3RetryingInputStream.java index 4ecb010a95d89..b9dfb7def7049 100644 --- a/plugins/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3RetryingInputStream.java +++ b/plugins/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3RetryingInputStream.java @@ -13,6 +13,7 @@ import com.amazonaws.services.s3.model.ObjectMetadata; import com.amazonaws.services.s3.model.S3Object; import com.amazonaws.services.s3.model.S3ObjectInputStream; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.message.ParameterizedMessage; @@ -78,8 +79,8 @@ private void openStream() throws IOException { final GetObjectRequest getObjectRequest = new GetObjectRequest(blobStore.bucket(), blobKey); getObjectRequest.setRequestMetricCollector(blobStore.getMetricCollector); if (currentOffset > 0 || start > 0 || end < Long.MAX_VALUE - 1) { - assert start + currentOffset <= end : - "requesting beyond end, start = " + start + " offset=" + currentOffset + " end=" + end; + assert start + currentOffset <= end + : "requesting beyond end, start = " + start + " offset=" + currentOffset + " end=" + end; getObjectRequest.setRange(Math.addExact(start, currentOffset), end); } final S3Object s3Object = SocketAccess.doPrivileged(() -> clientReference.client().getObject(getObjectRequest)); @@ -102,8 +103,8 @@ private long getStreamLength(final S3Object object) { final Long[] range = metadata.getContentRange(); if (range != null) { assert range[1] >= range[0] : range[1] + " vs " + range[0]; - assert range[0] == start + currentOffset : - "Content-Range start value [" + range[0] + "] exceeds start [" + start + "] + current offset [" + currentOffset + ']'; + assert range[0] == start + currentOffset + : "Content-Range start value [" + range[0] + "] exceeds start [" + start + "] + current offset [" + currentOffset + ']'; assert range[1] == end : "Content-Range end value [" + range[1] + "] exceeds end [" + end + ']'; return range[1] - range[0] + 1L; } @@ -159,12 +160,30 @@ private void ensureOpen() { private void reopenStreamOrFail(IOException e) throws IOException { if (attempt >= maxAttempts) { - logger.debug(new ParameterizedMessage("failed reading [{}/{}] at offset [{}], attempt [{}] of [{}], giving up", - blobStore.bucket(), blobKey, start + currentOffset, attempt, maxAttempts), e); + logger.debug( + new ParameterizedMessage( + "failed reading [{}/{}] at offset [{}], attempt [{}] of [{}], giving up", + blobStore.bucket(), + blobKey, + start + currentOffset, + attempt, + maxAttempts + ), + e + ); throw addSuppressedExceptions(e); } - logger.debug(new ParameterizedMessage("failed reading [{}/{}] at offset [{}], attempt [{}] of [{}], retrying", - blobStore.bucket(), blobKey, start + currentOffset, attempt, maxAttempts), e); + logger.debug( + new ParameterizedMessage( + "failed reading [{}/{}] at offset [{}], attempt [{}] of [{}], retrying", + blobStore.bucket(), + blobKey, + start + currentOffset, + attempt, + maxAttempts + ), + e + ); attempt += 1; if (failures.size() < MAX_SUPPRESSED_EXCEPTIONS) { failures.add(e); diff --git a/plugins/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3Service.java b/plugins/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3Service.java index 0f53d41e603cc..b3efacfa21b25 100644 --- a/plugins/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3Service.java +++ b/plugins/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3Service.java @@ -18,6 +18,7 @@ import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.internal.Constants; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.cluster.metadata.RepositoryMetadata; @@ -30,7 +31,6 @@ import static java.util.Collections.emptyMap; - class S3Service implements Closeable { private static final Logger logger = LogManager.getLogger(S3Service.class); @@ -39,8 +39,10 @@ class S3Service implements Closeable { /** * Client settings calculated from static configuration and settings in the keystore. */ - private volatile Map staticClientSettings = - Map.of("default", S3ClientSettings.getClientSettings(Settings.EMPTY, "default")); + private volatile Map staticClientSettings = Map.of( + "default", + S3ClientSettings.getClientSettings(Settings.EMPTY, "default") + ); /** * Client settings derived from those in {@link #staticClientSettings} by combining them with settings @@ -115,8 +117,12 @@ S3ClientSettings settings(RepositoryMetadata repositoryMetadata) { return newSettings; } } - throw new IllegalArgumentException("Unknown s3 client name [" + clientName + "]. Existing client configs: " - + Strings.collectionToDelimitedString(staticClientSettings.keySet(), ",")); + throw new IllegalArgumentException( + "Unknown s3 client name [" + + clientName + + "]. Existing client configs: " + + Strings.collectionToDelimitedString(staticClientSettings.keySet(), ",") + ); } // proxy for testing diff --git a/plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/AmazonS3Wrapper.java b/plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/AmazonS3Wrapper.java index 618b94ea7f8c8..2e029ac3eab66 100644 --- a/plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/AmazonS3Wrapper.java +++ b/plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/AmazonS3Wrapper.java @@ -99,6 +99,7 @@ import com.amazonaws.services.s3.model.UploadPartRequest; import com.amazonaws.services.s3.model.UploadPartResult; import com.amazonaws.services.s3.model.VersionListing; + import org.elasticsearch.core.SuppressForbidden; import java.io.File; @@ -132,14 +133,14 @@ public void setS3ClientOptions(S3ClientOptions clientOptions) { } @Override - public void changeObjectStorageClass(String bucketName, String key, StorageClass newStorageClass) - throws AmazonClientException, AmazonServiceException { + public void changeObjectStorageClass(String bucketName, String key, StorageClass newStorageClass) throws AmazonClientException, + AmazonServiceException { delegate.changeObjectStorageClass(bucketName, key, newStorageClass); } @Override - public void setObjectRedirectLocation(String bucketName, String key, String newRedirectLocation) - throws AmazonClientException, AmazonServiceException { + public void setObjectRedirectLocation(String bucketName, String key, String newRedirectLocation) throws AmazonClientException, + AmazonServiceException { delegate.setObjectRedirectLocation(bucketName, key, newRedirectLocation); } @@ -169,14 +170,20 @@ public VersionListing listVersions(String bucketName, String prefix) throws Amaz } @Override - public VersionListing listNextBatchOfVersions(VersionListing previousVersionListing) - throws AmazonClientException, AmazonServiceException { + public VersionListing listNextBatchOfVersions(VersionListing previousVersionListing) throws AmazonClientException, + AmazonServiceException { return delegate.listNextBatchOfVersions(previousVersionListing); } @Override - public VersionListing listVersions(String bucketName, String prefix, String keyMarker, String versionIdMarker, - String delimiter, Integer maxResults) throws AmazonClientException, AmazonServiceException { + public VersionListing listVersions( + String bucketName, + String prefix, + String keyMarker, + String versionIdMarker, + String delimiter, + Integer maxResults + ) throws AmazonClientException, AmazonServiceException { return delegate.listVersions(bucketName, prefix, keyMarker, versionIdMarker, delimiter, maxResults); } @@ -211,8 +218,8 @@ public String getBucketLocation(String bucketName) throws AmazonClientException, } @Override - public String getBucketLocation(GetBucketLocationRequest getBucketLocationRequest) - throws AmazonClientException, AmazonServiceException { + public String getBucketLocation(GetBucketLocationRequest getBucketLocationRequest) throws AmazonClientException, + AmazonServiceException { return delegate.getBucketLocation(getBucketLocationRequest); } @@ -227,8 +234,8 @@ public Bucket createBucket(String bucketName) throws AmazonClientException, Amaz } @Override - public Bucket createBucket(String bucketName, com.amazonaws.services.s3.model.Region region) - throws AmazonClientException, AmazonServiceException { + public Bucket createBucket(String bucketName, com.amazonaws.services.s3.model.Region region) throws AmazonClientException, + AmazonServiceException { return delegate.createBucket(bucketName, region); } @@ -243,8 +250,8 @@ public AccessControlList getObjectAcl(String bucketName, String key) throws Amaz } @Override - public AccessControlList getObjectAcl(String bucketName, String key, String versionId) - throws AmazonClientException, AmazonServiceException { + public AccessControlList getObjectAcl(String bucketName, String key, String versionId) throws AmazonClientException, + AmazonServiceException { return delegate.getObjectAcl(bucketName, key, versionId); } @@ -259,20 +266,20 @@ public void setObjectAcl(String bucketName, String key, AccessControlList acl) t } @Override - public void setObjectAcl(String bucketName, String key, CannedAccessControlList acl) - throws AmazonClientException, AmazonServiceException { + public void setObjectAcl(String bucketName, String key, CannedAccessControlList acl) throws AmazonClientException, + AmazonServiceException { delegate.setObjectAcl(bucketName, key, acl); } @Override - public void setObjectAcl(String bucketName, String key, String versionId, AccessControlList acl) - throws AmazonClientException, AmazonServiceException { + public void setObjectAcl(String bucketName, String key, String versionId, AccessControlList acl) throws AmazonClientException, + AmazonServiceException { delegate.setObjectAcl(bucketName, key, versionId, acl); } @Override - public void setObjectAcl(String bucketName, String key, String versionId, CannedAccessControlList acl) - throws AmazonClientException, AmazonServiceException { + public void setObjectAcl(String bucketName, String key, String versionId, CannedAccessControlList acl) throws AmazonClientException, + AmazonServiceException { delegate.setObjectAcl(bucketName, key, versionId, acl); } @@ -312,8 +319,8 @@ public ObjectMetadata getObjectMetadata(String bucketName, String key) throws Am } @Override - public ObjectMetadata getObjectMetadata(GetObjectMetadataRequest getObjectMetadataRequest) - throws AmazonClientException, AmazonServiceException { + public ObjectMetadata getObjectMetadata(GetObjectMetadataRequest getObjectMetadataRequest) throws AmazonClientException, + AmazonServiceException { return delegate.getObjectMetadata(getObjectMetadataRequest); } @@ -328,8 +335,8 @@ public S3Object getObject(GetObjectRequest getObjectRequest) throws AmazonClient } @Override - public ObjectMetadata getObject(GetObjectRequest getObjectRequest, File destinationFile) - throws AmazonClientException, AmazonServiceException { + public ObjectMetadata getObject(GetObjectRequest getObjectRequest, File destinationFile) throws AmazonClientException, + AmazonServiceException { return delegate.getObject(getObjectRequest, destinationFile); } @@ -345,19 +352,19 @@ public void deleteBucket(String bucketName) throws AmazonClientException, Amazon @Override public void setBucketReplicationConfiguration(String bucketName, BucketReplicationConfiguration configuration) - throws AmazonServiceException, AmazonClientException { + throws AmazonServiceException, AmazonClientException { delegate.setBucketReplicationConfiguration(bucketName, configuration); } @Override public void setBucketReplicationConfiguration(SetBucketReplicationConfigurationRequest setBucketReplicationConfigurationRequest) - throws AmazonServiceException, AmazonClientException { + throws AmazonServiceException, AmazonClientException { delegate.setBucketReplicationConfiguration(setBucketReplicationConfigurationRequest); } @Override - public BucketReplicationConfiguration getBucketReplicationConfiguration(String bucketName) - throws AmazonServiceException, AmazonClientException { + public BucketReplicationConfiguration getBucketReplicationConfiguration(String bucketName) throws AmazonServiceException, + AmazonClientException { return delegate.getBucketReplicationConfiguration(bucketName); } @@ -367,8 +374,8 @@ public void deleteBucketReplicationConfiguration(String bucketName) throws Amazo } @Override - public void deleteBucketReplicationConfiguration(DeleteBucketReplicationConfigurationRequest request) - throws AmazonServiceException, AmazonClientException { + public void deleteBucketReplicationConfiguration(DeleteBucketReplicationConfigurationRequest request) throws AmazonServiceException, + AmazonClientException { delegate.deleteBucketReplicationConfiguration(request); } @@ -389,13 +396,13 @@ public PutObjectResult putObject(String bucketName, String key, File file) throw @Override public PutObjectResult putObject(String bucketName, String key, InputStream input, ObjectMetadata metadata) - throws AmazonClientException, AmazonServiceException { + throws AmazonClientException, AmazonServiceException { return delegate.putObject(bucketName, key, input, metadata); } @Override public CopyObjectResult copyObject(String sourceBucketName, String sourceKey, String destinationBucketName, String destinationKey) - throws AmazonClientException, AmazonServiceException { + throws AmazonClientException, AmazonServiceException { return delegate.copyObject(sourceBucketName, sourceKey, destinationBucketName, destinationKey); } @@ -420,8 +427,8 @@ public void deleteObject(DeleteObjectRequest deleteObjectRequest) throws AmazonC } @Override - public DeleteObjectsResult deleteObjects(DeleteObjectsRequest deleteObjectsRequest) - throws AmazonClientException, AmazonServiceException { + public DeleteObjectsResult deleteObjects(DeleteObjectsRequest deleteObjectsRequest) throws AmazonClientException, + AmazonServiceException { return delegate.deleteObjects(deleteObjectsRequest); } @@ -436,26 +443,26 @@ public void deleteVersion(DeleteVersionRequest deleteVersionRequest) throws Amaz } @Override - public BucketLoggingConfiguration getBucketLoggingConfiguration(String bucketName) - throws AmazonClientException, AmazonServiceException { + public BucketLoggingConfiguration getBucketLoggingConfiguration(String bucketName) throws AmazonClientException, + AmazonServiceException { return delegate.getBucketLoggingConfiguration(bucketName); } @Override public void setBucketLoggingConfiguration(SetBucketLoggingConfigurationRequest setBucketLoggingConfigurationRequest) - throws AmazonClientException, AmazonServiceException { + throws AmazonClientException, AmazonServiceException { delegate.setBucketLoggingConfiguration(setBucketLoggingConfigurationRequest); } @Override - public BucketVersioningConfiguration getBucketVersioningConfiguration(String bucketName) - throws AmazonClientException, AmazonServiceException { + public BucketVersioningConfiguration getBucketVersioningConfiguration(String bucketName) throws AmazonClientException, + AmazonServiceException { return delegate.getBucketVersioningConfiguration(bucketName); } @Override public void setBucketVersioningConfiguration(SetBucketVersioningConfigurationRequest setBucketVersioningConfigurationRequest) - throws AmazonClientException, AmazonServiceException { + throws AmazonClientException, AmazonServiceException { delegate.setBucketVersioningConfiguration(setBucketVersioningConfigurationRequest); } @@ -506,7 +513,8 @@ public void deleteBucketCrossOriginConfiguration(String bucketName) { @Override public void deleteBucketCrossOriginConfiguration( - DeleteBucketCrossOriginConfigurationRequest deleteBucketCrossOriginConfigurationRequest) { + DeleteBucketCrossOriginConfigurationRequest deleteBucketCrossOriginConfigurationRequest + ) { delegate.deleteBucketCrossOriginConfiguration(deleteBucketCrossOriginConfigurationRequest); } @@ -536,45 +544,45 @@ public void deleteBucketTaggingConfiguration(DeleteBucketTaggingConfigurationReq } @Override - public BucketNotificationConfiguration getBucketNotificationConfiguration(String bucketName) - throws AmazonClientException, AmazonServiceException { + public BucketNotificationConfiguration getBucketNotificationConfiguration(String bucketName) throws AmazonClientException, + AmazonServiceException { return delegate.getBucketNotificationConfiguration(bucketName); } @Override public void setBucketNotificationConfiguration(SetBucketNotificationConfigurationRequest setBucketNotificationConfigurationRequest) - throws AmazonClientException, AmazonServiceException { + throws AmazonClientException, AmazonServiceException { delegate.setBucketNotificationConfiguration(setBucketNotificationConfigurationRequest); } @Override public void setBucketNotificationConfiguration(String bucketName, BucketNotificationConfiguration bucketNotificationConfiguration) - throws AmazonClientException, AmazonServiceException { + throws AmazonClientException, AmazonServiceException { delegate.setBucketNotificationConfiguration(bucketName, bucketNotificationConfiguration); } @Override - public BucketWebsiteConfiguration getBucketWebsiteConfiguration(String bucketName) - throws AmazonClientException, AmazonServiceException { + public BucketWebsiteConfiguration getBucketWebsiteConfiguration(String bucketName) throws AmazonClientException, + AmazonServiceException { return delegate.getBucketWebsiteConfiguration(bucketName); } @Override public BucketWebsiteConfiguration getBucketWebsiteConfiguration( - GetBucketWebsiteConfigurationRequest getBucketWebsiteConfigurationRequest) - throws AmazonClientException, AmazonServiceException { + GetBucketWebsiteConfigurationRequest getBucketWebsiteConfigurationRequest + ) throws AmazonClientException, AmazonServiceException { return delegate.getBucketWebsiteConfiguration(getBucketWebsiteConfigurationRequest); } @Override - public void setBucketWebsiteConfiguration(String bucketName, BucketWebsiteConfiguration configuration) - throws AmazonClientException, AmazonServiceException { + public void setBucketWebsiteConfiguration(String bucketName, BucketWebsiteConfiguration configuration) throws AmazonClientException, + AmazonServiceException { delegate.setBucketWebsiteConfiguration(bucketName, configuration); } @Override public void setBucketWebsiteConfiguration(SetBucketWebsiteConfigurationRequest setBucketWebsiteConfigurationRequest) - throws AmazonClientException, AmazonServiceException { + throws AmazonClientException, AmazonServiceException { delegate.setBucketWebsiteConfiguration(setBucketWebsiteConfigurationRequest); } @@ -585,7 +593,7 @@ public void deleteBucketWebsiteConfiguration(String bucketName) throws AmazonCli @Override public void deleteBucketWebsiteConfiguration(DeleteBucketWebsiteConfigurationRequest deleteBucketWebsiteConfigurationRequest) - throws AmazonClientException, AmazonServiceException { + throws AmazonClientException, AmazonServiceException { delegate.deleteBucketWebsiteConfiguration(deleteBucketWebsiteConfigurationRequest); } @@ -595,8 +603,8 @@ public BucketPolicy getBucketPolicy(String bucketName) throws AmazonClientExcept } @Override - public BucketPolicy getBucketPolicy(GetBucketPolicyRequest getBucketPolicyRequest) - throws AmazonClientException, AmazonServiceException { + public BucketPolicy getBucketPolicy(GetBucketPolicyRequest getBucketPolicyRequest) throws AmazonClientException, + AmazonServiceException { return delegate.getBucketPolicy(getBucketPolicyRequest); } @@ -616,8 +624,8 @@ public void deleteBucketPolicy(String bucketName) throws AmazonClientException, } @Override - public void deleteBucketPolicy(DeleteBucketPolicyRequest deleteBucketPolicyRequest) - throws AmazonClientException, AmazonServiceException { + public void deleteBucketPolicy(DeleteBucketPolicyRequest deleteBucketPolicyRequest) throws AmazonClientException, + AmazonServiceException { delegate.deleteBucketPolicy(deleteBucketPolicyRequest); } @@ -637,8 +645,8 @@ public URL generatePresignedUrl(GeneratePresignedUrlRequest generatePresignedUrl } @Override - public InitiateMultipartUploadResult initiateMultipartUpload(InitiateMultipartUploadRequest request) - throws AmazonClientException, AmazonServiceException { + public InitiateMultipartUploadResult initiateMultipartUpload(InitiateMultipartUploadRequest request) throws AmazonClientException, + AmazonServiceException { return delegate.initiateMultipartUpload(request); } @@ -658,14 +666,14 @@ public void abortMultipartUpload(AbortMultipartUploadRequest request) throws Ama } @Override - public CompleteMultipartUploadResult completeMultipartUpload(CompleteMultipartUploadRequest request) - throws AmazonClientException, AmazonServiceException { + public CompleteMultipartUploadResult completeMultipartUpload(CompleteMultipartUploadRequest request) throws AmazonClientException, + AmazonServiceException { return delegate.completeMultipartUpload(request); } @Override - public MultipartUploadListing listMultipartUploads(ListMultipartUploadsRequest request) - throws AmazonClientException, AmazonServiceException { + public MultipartUploadListing listMultipartUploads(ListMultipartUploadsRequest request) throws AmazonClientException, + AmazonServiceException { return delegate.listMultipartUploads(request); } @@ -700,14 +708,14 @@ public boolean isRequesterPaysEnabled(String bucketName) throws AmazonServiceExc } @Override - public ObjectListing listNextBatchOfObjects(ListNextBatchOfObjectsRequest listNextBatchOfObjectsRequest) - throws AmazonClientException, AmazonServiceException { + public ObjectListing listNextBatchOfObjects(ListNextBatchOfObjectsRequest listNextBatchOfObjectsRequest) throws AmazonClientException, + AmazonServiceException { return delegate.listNextBatchOfObjects(listNextBatchOfObjectsRequest); } @Override public VersionListing listNextBatchOfVersions(ListNextBatchOfVersionsRequest listNextBatchOfVersionsRequest) - throws AmazonClientException, AmazonServiceException { + throws AmazonClientException, AmazonServiceException { return delegate.listNextBatchOfVersions(listNextBatchOfVersionsRequest); } @@ -718,47 +726,50 @@ public Owner getS3AccountOwner(GetS3AccountOwnerRequest getS3AccountOwnerRequest @Override public BucketLoggingConfiguration getBucketLoggingConfiguration( - GetBucketLoggingConfigurationRequest getBucketLoggingConfigurationRequest) - throws AmazonClientException, AmazonServiceException { + GetBucketLoggingConfigurationRequest getBucketLoggingConfigurationRequest + ) throws AmazonClientException, AmazonServiceException { return delegate.getBucketLoggingConfiguration(getBucketLoggingConfigurationRequest); } @Override public BucketVersioningConfiguration getBucketVersioningConfiguration( - GetBucketVersioningConfigurationRequest getBucketVersioningConfigurationRequest) - throws AmazonClientException, AmazonServiceException { + GetBucketVersioningConfigurationRequest getBucketVersioningConfigurationRequest + ) throws AmazonClientException, AmazonServiceException { return delegate.getBucketVersioningConfiguration(getBucketVersioningConfigurationRequest); } @Override public BucketLifecycleConfiguration getBucketLifecycleConfiguration( - GetBucketLifecycleConfigurationRequest getBucketLifecycleConfigurationRequest) { + GetBucketLifecycleConfigurationRequest getBucketLifecycleConfigurationRequest + ) { return delegate.getBucketLifecycleConfiguration(getBucketLifecycleConfigurationRequest); } @Override public BucketCrossOriginConfiguration getBucketCrossOriginConfiguration( - GetBucketCrossOriginConfigurationRequest getBucketCrossOriginConfigurationRequest) { + GetBucketCrossOriginConfigurationRequest getBucketCrossOriginConfigurationRequest + ) { return delegate.getBucketCrossOriginConfiguration(getBucketCrossOriginConfigurationRequest); } @Override public BucketTaggingConfiguration getBucketTaggingConfiguration( - GetBucketTaggingConfigurationRequest getBucketTaggingConfigurationRequest) { + GetBucketTaggingConfigurationRequest getBucketTaggingConfigurationRequest + ) { return delegate.getBucketTaggingConfiguration(getBucketTaggingConfigurationRequest); } @Override public BucketNotificationConfiguration getBucketNotificationConfiguration( - GetBucketNotificationConfigurationRequest getBucketNotificationConfigurationRequest) - throws AmazonClientException, AmazonServiceException { + GetBucketNotificationConfigurationRequest getBucketNotificationConfigurationRequest + ) throws AmazonClientException, AmazonServiceException { return delegate.getBucketNotificationConfiguration(getBucketNotificationConfigurationRequest); } @Override public BucketReplicationConfiguration getBucketReplicationConfiguration( - GetBucketReplicationConfigurationRequest getBucketReplicationConfigurationRequest) - throws AmazonServiceException, AmazonClientException { + GetBucketReplicationConfigurationRequest getBucketReplicationConfigurationRequest + ) throws AmazonServiceException, AmazonClientException { return delegate.getBucketReplicationConfiguration(getBucketReplicationConfigurationRequest); } diff --git a/plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/AwsS3ServiceImplTests.java b/plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/AwsS3ServiceImplTests.java index d20749bf033db..6700b18caac76 100644 --- a/plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/AwsS3ServiceImplTests.java +++ b/plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/AwsS3ServiceImplTests.java @@ -96,8 +96,17 @@ public void testCredentialsIncomplete() { } public void testAWSDefaultConfiguration() { - launchAWSConfigurationTest(Settings.EMPTY, Protocol.HTTPS, null, -1, null, null, 3, - ClientConfiguration.DEFAULT_THROTTLE_RETRIES, ClientConfiguration.DEFAULT_SOCKET_TIMEOUT); + launchAWSConfigurationTest( + Settings.EMPTY, + Protocol.HTTPS, + null, + -1, + null, + null, + 3, + ClientConfiguration.DEFAULT_THROTTLE_RETRIES, + ClientConfiguration.DEFAULT_SOCKET_TIMEOUT + ); } public void testAWSConfigurationWithAwsSettings() { @@ -111,16 +120,22 @@ public void testAWSConfigurationWithAwsSettings() { .put("s3.client.default.proxy.port", 8080) .put("s3.client.default.read_timeout", "10s") .build(); - launchAWSConfigurationTest(settings, Protocol.HTTP, "aws_proxy_host", 8080, "aws_proxy_username", - "aws_proxy_password", 3, ClientConfiguration.DEFAULT_THROTTLE_RETRIES, 10000); + launchAWSConfigurationTest( + settings, + Protocol.HTTP, + "aws_proxy_host", + 8080, + "aws_proxy_username", + "aws_proxy_password", + 3, + ClientConfiguration.DEFAULT_THROTTLE_RETRIES, + 10000 + ); } public void testRepositoryMaxRetries() { - final Settings settings = Settings.builder() - .put("s3.client.default.max_retries", 5) - .build(); - launchAWSConfigurationTest(settings, Protocol.HTTPS, null, -1, null, - null, 5, ClientConfiguration.DEFAULT_THROTTLE_RETRIES, 50000); + final Settings settings = Settings.builder().put("s3.client.default.max_retries", 5).build(); + launchAWSConfigurationTest(settings, Protocol.HTTPS, null, -1, null, null, 5, ClientConfiguration.DEFAULT_THROTTLE_RETRIES, 50000); } public void testRepositoryThrottleRetries() { @@ -130,15 +145,17 @@ public void testRepositoryThrottleRetries() { launchAWSConfigurationTest(settings, Protocol.HTTPS, null, -1, null, null, 3, throttling, 50000); } - private void launchAWSConfigurationTest(Settings settings, - Protocol expectedProtocol, - String expectedProxyHost, - int expectedProxyPort, - String expectedProxyUsername, - String expectedProxyPassword, - Integer expectedMaxRetries, - boolean expectedUseThrottleRetries, - int expectedReadTimeout) { + private void launchAWSConfigurationTest( + Settings settings, + Protocol expectedProtocol, + String expectedProxyHost, + int expectedProxyPort, + String expectedProxyUsername, + String expectedProxyPassword, + Integer expectedMaxRetries, + boolean expectedUseThrottleRetries, + int expectedReadTimeout + ) { final S3ClientSettings clientSettings = S3ClientSettings.getClientSettings(settings, "default"); final ClientConfiguration configuration = S3Service.buildConfiguration(clientSettings); @@ -155,9 +172,7 @@ private void launchAWSConfigurationTest(Settings settings, } public void testEndpointSetting() { - final Settings settings = Settings.builder() - .put("s3.client.default.endpoint", "s3.endpoint") - .build(); + final Settings settings = Settings.builder().put("s3.client.default.endpoint", "s3.endpoint").build(); assertEndpoint(Settings.EMPTY, settings, "s3.endpoint"); } diff --git a/plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/RepositoryCredentialsTests.java b/plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/RepositoryCredentialsTests.java index d7bc6b7c856fb..9825082638052 100644 --- a/plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/RepositoryCredentialsTests.java +++ b/plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/RepositoryCredentialsTests.java @@ -11,6 +11,7 @@ import com.amazonaws.auth.AWSCredentials; import com.amazonaws.auth.AWSCredentialsProvider; import com.amazonaws.services.s3.AmazonS3; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.cluster.metadata.RepositoryMetadata; @@ -18,12 +19,12 @@ import org.elasticsearch.common.settings.MockSecureSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.BigArrays; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.indices.recovery.RecoverySettings; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.plugins.PluginsService; import org.elasticsearch.repositories.RepositoriesService; import org.elasticsearch.test.ESSingleNodeTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; import java.util.Collection; import java.util.List; @@ -55,10 +56,7 @@ protected Settings nodeSettings() { secureSettings.setString(ACCESS_KEY_SETTING.getConcreteSettingForNamespace("other").getKey(), "secure_other_key"); secureSettings.setString(SECRET_KEY_SETTING.getConcreteSettingForNamespace("other").getKey(), "secure_other_secret"); - return Settings.builder() - .setSecureSettings(secureSettings) - .put(super.nodeSettings()) - .build(); + return Settings.builder().setSecureSettings(secureSettings).put(super.nodeSettings()).build(); } public void testReinitSecureCredentials() { @@ -120,10 +118,14 @@ public void testReinitSecureCredentials() { } private void createRepository(final String name, final Settings repositorySettings) { - assertAcked(client().admin().cluster().preparePutRepository(name) - .setType(S3Repository.TYPE) - .setVerify(false) - .setSettings(repositorySettings)); + assertAcked( + client().admin() + .cluster() + .preparePutRepository(name) + .setType(S3Repository.TYPE) + .setVerify(false) + .setSettings(repositorySettings) + ); } /** @@ -136,9 +138,13 @@ public ProxyS3RepositoryPlugin(Settings settings) { } @Override - protected S3Repository createRepository(RepositoryMetadata metadata, - NamedXContentRegistry registry, ClusterService clusterService, BigArrays bigArrays, - RecoverySettings recoverySettings) { + protected S3Repository createRepository( + RepositoryMetadata metadata, + NamedXContentRegistry registry, + ClusterService clusterService, + BigArrays bigArrays, + RecoverySettings recoverySettings + ) { return new S3Repository(metadata, registry, service, clusterService, bigArrays, recoverySettings) { @Override protected void assertSnapshotOrGenericThread() { diff --git a/plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/S3BlobContainerRetriesTests.java b/plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/S3BlobContainerRetriesTests.java index 789a67ca2e847..78aede78bc87a 100644 --- a/plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/S3BlobContainerRetriesTests.java +++ b/plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/S3BlobContainerRetriesTests.java @@ -10,11 +10,9 @@ import com.amazonaws.SdkClientException; import com.amazonaws.services.s3.internal.MD5DigestCalculatingInputStream; import com.amazonaws.util.Base16; + import org.apache.http.HttpStatus; import org.elasticsearch.cluster.metadata.RepositoryMetadata; -import org.elasticsearch.common.util.BigArrays; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.common.blobstore.BlobContainer; import org.elasticsearch.common.blobstore.BlobPath; import org.elasticsearch.common.bytes.BytesReference; @@ -26,8 +24,11 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; -import org.elasticsearch.core.TimeValue; +import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.common.util.concurrent.CountDown; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.core.SuppressForbidden; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.repositories.blobstore.AbstractBlobContainerRetriesTestCase; import org.junit.After; @@ -91,10 +92,12 @@ protected Class unresponsiveExceptionType() { } @Override - protected BlobContainer createBlobContainer(final @Nullable Integer maxRetries, - final @Nullable TimeValue readTimeout, - final @Nullable Boolean disableChunkedEncoding, - final @Nullable ByteSizeValue bufferSize) { + protected BlobContainer createBlobContainer( + final @Nullable Integer maxRetries, + final @Nullable TimeValue readTimeout, + final @Nullable Boolean disableChunkedEncoding, + final @Nullable ByteSizeValue bufferSize + ) { final Settings.Builder clientSettings = Settings.builder(); final String clientName = randomAlphaOfLength(5).toLowerCase(Locale.ROOT); @@ -113,31 +116,45 @@ protected BlobContainer createBlobContainer(final @Nullable Integer maxRetries, } final MockSecureSettings secureSettings = new MockSecureSettings(); - secureSettings.setString(S3ClientSettings.ACCESS_KEY_SETTING.getConcreteSettingForNamespace(clientName).getKey(), - "test_access_key"); - secureSettings.setString(S3ClientSettings.SECRET_KEY_SETTING.getConcreteSettingForNamespace(clientName).getKey(), - "test_secret_key"); + secureSettings.setString( + S3ClientSettings.ACCESS_KEY_SETTING.getConcreteSettingForNamespace(clientName).getKey(), + "test_access_key" + ); + secureSettings.setString( + S3ClientSettings.SECRET_KEY_SETTING.getConcreteSettingForNamespace(clientName).getKey(), + "test_secret_key" + ); clientSettings.setSecureSettings(secureSettings); service.refreshAndClearCache(S3ClientSettings.load(clientSettings.build())); - final RepositoryMetadata repositoryMetadata = new RepositoryMetadata("repository", S3Repository.TYPE, - Settings.builder().put(S3Repository.CLIENT_NAME.getKey(), clientName).build()); - - return new S3BlobContainer(randomBoolean() ? BlobPath.EMPTY : BlobPath.EMPTY.add("foo"), new S3BlobStore(service, "bucket", - S3Repository.SERVER_SIDE_ENCRYPTION_SETTING.getDefault(Settings.EMPTY), - bufferSize == null ? S3Repository.BUFFER_SIZE_SETTING.getDefault(Settings.EMPTY) : bufferSize, - S3Repository.CANNED_ACL_SETTING.getDefault(Settings.EMPTY), - S3Repository.STORAGE_CLASS_SETTING.getDefault(Settings.EMPTY), - repositoryMetadata, BigArrays.NON_RECYCLING_INSTANCE)) { - @Override - public InputStream readBlob(String blobName) throws IOException { - return new AssertingInputStream(super.readBlob(blobName), blobName); - } + final RepositoryMetadata repositoryMetadata = new RepositoryMetadata( + "repository", + S3Repository.TYPE, + Settings.builder().put(S3Repository.CLIENT_NAME.getKey(), clientName).build() + ); + + return new S3BlobContainer( + randomBoolean() ? BlobPath.EMPTY : BlobPath.EMPTY.add("foo"), + new S3BlobStore( + service, + "bucket", + S3Repository.SERVER_SIDE_ENCRYPTION_SETTING.getDefault(Settings.EMPTY), + bufferSize == null ? S3Repository.BUFFER_SIZE_SETTING.getDefault(Settings.EMPTY) : bufferSize, + S3Repository.CANNED_ACL_SETTING.getDefault(Settings.EMPTY), + S3Repository.STORAGE_CLASS_SETTING.getDefault(Settings.EMPTY), + repositoryMetadata, + BigArrays.NON_RECYCLING_INSTANCE + ) + ) { + @Override + public InputStream readBlob(String blobName) throws IOException { + return new AssertingInputStream(super.readBlob(blobName), blobName); + } - @Override - public InputStream readBlob(String blobName, long position, long length) throws IOException { - return new AssertingInputStream(super.readBlob(blobName, position, length), blobName, position, length); - } + @Override + public InputStream readBlob(String blobName, long position, long length) throws IOException { + return new AssertingInputStream(super.readBlob(blobName, position, length), blobName, position, length); + } }; } @@ -166,8 +183,15 @@ public void testWriteBlobWithRetries() throws Exception { Streams.readFully(exchange.getRequestBody(), new byte[randomIntBetween(1, Math.max(1, bytes.length - 1))]); } else { Streams.readFully(exchange.getRequestBody()); - exchange.sendResponseHeaders(randomFrom(HttpStatus.SC_INTERNAL_SERVER_ERROR, HttpStatus.SC_BAD_GATEWAY, - HttpStatus.SC_SERVICE_UNAVAILABLE, HttpStatus.SC_GATEWAY_TIMEOUT), -1); + exchange.sendResponseHeaders( + randomFrom( + HttpStatus.SC_INTERNAL_SERVER_ERROR, + HttpStatus.SC_BAD_GATEWAY, + HttpStatus.SC_SERVICE_UNAVAILABLE, + HttpStatus.SC_GATEWAY_TIMEOUT + ), + -1 + ); } } exchange.close(); @@ -200,9 +224,10 @@ public void testWriteBlobWithReadTimeouts() { blobContainer.writeBlob("write_blob_timeout", stream, bytes.length, false); } }); - assertThat(exception.getMessage().toLowerCase(Locale.ROOT), - containsString( - "unable to upload object [" + blobContainer.path().buildAsString() + "write_blob_timeout] using a single upload")); + assertThat( + exception.getMessage().toLowerCase(Locale.ROOT), + containsString("unable to upload object [" + blobContainer.path().buildAsString() + "write_blob_timeout] using a single upload") + ); assertThat(exception.getCause(), instanceOf(SdkClientException.class)); assertThat(exception.getCause().getMessage().toLowerCase(Locale.ROOT), containsString("read timed out")); @@ -229,16 +254,15 @@ public void testWriteLargeBlob() throws Exception { httpServer.createContext(downloadStorageEndpoint(blobContainer, "write_large_blob"), exchange -> { final long contentLength = Long.parseLong(exchange.getRequestHeaders().getFirst("Content-Length")); - if ("POST".equals(exchange.getRequestMethod()) - && exchange.getRequestURI().getQuery().equals("uploads")) { + if ("POST".equals(exchange.getRequestMethod()) && exchange.getRequestURI().getQuery().equals("uploads")) { // initiate multipart upload request if (countDownInitiate.countDown()) { - byte[] response = ("\n" + - "\n" + - " bucket\n" + - " write_large_blob\n" + - " TEST\n" + - "").getBytes(StandardCharsets.UTF_8); + byte[] response = ("\n" + + "\n" + + " bucket\n" + + " write_large_blob\n" + + " TEST\n" + + "").getBytes(StandardCharsets.UTF_8); exchange.getResponseHeaders().add("Content-Type", "application/xml"); exchange.sendResponseHeaders(HttpStatus.SC_OK, response.length); exchange.getResponseBody().write(response); @@ -248,36 +272,35 @@ public void testWriteLargeBlob() throws Exception { } else if ("PUT".equals(exchange.getRequestMethod()) && exchange.getRequestURI().getQuery().contains("uploadId=TEST") && exchange.getRequestURI().getQuery().contains("partNumber=")) { - // upload part request - MD5DigestCalculatingInputStream md5 = new MD5DigestCalculatingInputStream(exchange.getRequestBody()); - BytesReference bytes = Streams.readFully(md5); - assertThat((long) bytes.length(), anyOf(equalTo(lastPartSize), equalTo(bufferSize.getBytes()))); - assertThat(contentLength, anyOf(equalTo(lastPartSize), equalTo(bufferSize.getBytes()))); - - if (countDownUploads.decrementAndGet() % 2 == 0) { - exchange.getResponseHeaders().add("ETag", Base16.encodeAsString(md5.getMd5Digest())); - exchange.sendResponseHeaders(HttpStatus.SC_OK, -1); - exchange.close(); - return; - } + // upload part request + MD5DigestCalculatingInputStream md5 = new MD5DigestCalculatingInputStream(exchange.getRequestBody()); + BytesReference bytes = Streams.readFully(md5); + assertThat((long) bytes.length(), anyOf(equalTo(lastPartSize), equalTo(bufferSize.getBytes()))); + assertThat(contentLength, anyOf(equalTo(lastPartSize), equalTo(bufferSize.getBytes()))); + + if (countDownUploads.decrementAndGet() % 2 == 0) { + exchange.getResponseHeaders().add("ETag", Base16.encodeAsString(md5.getMd5Digest())); + exchange.sendResponseHeaders(HttpStatus.SC_OK, -1); + exchange.close(); + return; + } - } else if ("POST".equals(exchange.getRequestMethod()) - && exchange.getRequestURI().getQuery().equals("uploadId=TEST")) { - // complete multipart upload request - if (countDownComplete.countDown()) { - Streams.readFully(exchange.getRequestBody()); - byte[] response = ("\n" + - "\n" + - " bucket\n" + - " write_large_blob\n" + - "").getBytes(StandardCharsets.UTF_8); - exchange.getResponseHeaders().add("Content-Type", "application/xml"); - exchange.sendResponseHeaders(HttpStatus.SC_OK, response.length); - exchange.getResponseBody().write(response); - exchange.close(); - return; + } else if ("POST".equals(exchange.getRequestMethod()) && exchange.getRequestURI().getQuery().equals("uploadId=TEST")) { + // complete multipart upload request + if (countDownComplete.countDown()) { + Streams.readFully(exchange.getRequestBody()); + byte[] response = ("\n" + + "\n" + + " bucket\n" + + " write_large_blob\n" + + "").getBytes(StandardCharsets.UTF_8); + exchange.getResponseHeaders().add("Content-Type", "application/xml"); + exchange.sendResponseHeaders(HttpStatus.SC_OK, response.length); + exchange.getResponseBody().write(response); + exchange.close(); + return; + } } - } // sends an error back or let the request time out if (useTimeout == false) { @@ -285,8 +308,15 @@ public void testWriteLargeBlob() throws Exception { Streams.readFully(exchange.getRequestBody(), new byte[randomIntBetween(1, Math.toIntExact(contentLength - 1))]); } else { Streams.readFully(exchange.getRequestBody()); - exchange.sendResponseHeaders(randomFrom(HttpStatus.SC_INTERNAL_SERVER_ERROR, HttpStatus.SC_BAD_GATEWAY, - HttpStatus.SC_SERVICE_UNAVAILABLE, HttpStatus.SC_GATEWAY_TIMEOUT), -1); + exchange.sendResponseHeaders( + randomFrom( + HttpStatus.SC_INTERNAL_SERVER_ERROR, + HttpStatus.SC_BAD_GATEWAY, + HttpStatus.SC_SERVICE_UNAVAILABLE, + HttpStatus.SC_GATEWAY_TIMEOUT + ), + -1 + ); } exchange.close(); } @@ -318,16 +348,15 @@ public void testWriteLargeBlobStreaming() throws Exception { httpServer.createContext(downloadStorageEndpoint(blobContainer, "write_large_blob_streaming"), exchange -> { final long contentLength = Long.parseLong(exchange.getRequestHeaders().getFirst("Content-Length")); - if ("POST".equals(exchange.getRequestMethod()) - && exchange.getRequestURI().getQuery().equals("uploads")) { + if ("POST".equals(exchange.getRequestMethod()) && exchange.getRequestURI().getQuery().equals("uploads")) { // initiate multipart upload request if (countDownInitiate.countDown()) { - byte[] response = ("\n" + - "\n" + - " bucket\n" + - " write_large_blob_streaming\n" + - " TEST\n" + - "").getBytes(StandardCharsets.UTF_8); + byte[] response = ("\n" + + "\n" + + " bucket\n" + + " write_large_blob_streaming\n" + + " TEST\n" + + "").getBytes(StandardCharsets.UTF_8); exchange.getResponseHeaders().add("Content-Type", "application/xml"); exchange.sendResponseHeaders(HttpStatus.SC_OK, response.length); exchange.getResponseBody().write(response); @@ -335,37 +364,36 @@ public void testWriteLargeBlobStreaming() throws Exception { return; } } else if ("PUT".equals(exchange.getRequestMethod()) - && exchange.getRequestURI().getQuery().contains("uploadId=TEST") - && exchange.getRequestURI().getQuery().contains("partNumber=")) { - // upload part request - MD5DigestCalculatingInputStream md5 = new MD5DigestCalculatingInputStream(exchange.getRequestBody()); - BytesReference bytes = Streams.readFully(md5); - - if (counterUploads.incrementAndGet() % 2 == 0) { - bytesReceived.addAndGet(bytes.length()); - exchange.getResponseHeaders().add("ETag", Base16.encodeAsString(md5.getMd5Digest())); - exchange.sendResponseHeaders(HttpStatus.SC_OK, -1); - exchange.close(); - return; - } + && exchange.getRequestURI().getQuery().contains("uploadId=TEST") + && exchange.getRequestURI().getQuery().contains("partNumber=")) { + // upload part request + MD5DigestCalculatingInputStream md5 = new MD5DigestCalculatingInputStream(exchange.getRequestBody()); + BytesReference bytes = Streams.readFully(md5); - } else if ("POST".equals(exchange.getRequestMethod()) - && exchange.getRequestURI().getQuery().equals("uploadId=TEST")) { - // complete multipart upload request - if (countDownComplete.countDown()) { - Streams.readFully(exchange.getRequestBody()); - byte[] response = ("\n" + - "\n" + - " bucket\n" + - " write_large_blob_streaming\n" + - "").getBytes(StandardCharsets.UTF_8); - exchange.getResponseHeaders().add("Content-Type", "application/xml"); - exchange.sendResponseHeaders(HttpStatus.SC_OK, response.length); - exchange.getResponseBody().write(response); - exchange.close(); - return; + if (counterUploads.incrementAndGet() % 2 == 0) { + bytesReceived.addAndGet(bytes.length()); + exchange.getResponseHeaders().add("ETag", Base16.encodeAsString(md5.getMd5Digest())); + exchange.sendResponseHeaders(HttpStatus.SC_OK, -1); + exchange.close(); + return; + } + + } else if ("POST".equals(exchange.getRequestMethod()) && exchange.getRequestURI().getQuery().equals("uploadId=TEST")) { + // complete multipart upload request + if (countDownComplete.countDown()) { + Streams.readFully(exchange.getRequestBody()); + byte[] response = ("\n" + + "\n" + + " bucket\n" + + " write_large_blob_streaming\n" + + "").getBytes(StandardCharsets.UTF_8); + exchange.getResponseHeaders().add("Content-Type", "application/xml"); + exchange.sendResponseHeaders(HttpStatus.SC_OK, response.length); + exchange.getResponseBody().write(response); + exchange.close(); + return; + } } - } // sends an error back or let the request time out if (useTimeout == false) { @@ -373,8 +401,15 @@ public void testWriteLargeBlobStreaming() throws Exception { Streams.readFully(exchange.getRequestBody(), new byte[randomIntBetween(1, Math.toIntExact(contentLength - 1))]); } else { Streams.readFully(exchange.getRequestBody()); - exchange.sendResponseHeaders(randomFrom(HttpStatus.SC_INTERNAL_SERVER_ERROR, HttpStatus.SC_BAD_GATEWAY, - HttpStatus.SC_SERVICE_UNAVAILABLE, HttpStatus.SC_GATEWAY_TIMEOUT), -1); + exchange.sendResponseHeaders( + randomFrom( + HttpStatus.SC_INTERNAL_SERVER_ERROR, + HttpStatus.SC_BAD_GATEWAY, + HttpStatus.SC_SERVICE_UNAVAILABLE, + HttpStatus.SC_GATEWAY_TIMEOUT + ), + -1 + ); } exchange.close(); } @@ -440,8 +475,16 @@ public void close() throws IOException { super.close(); if (in instanceof S3RetryingInputStream) { final S3RetryingInputStream s3Stream = (S3RetryingInputStream) in; - assertTrue("Stream " + toString() + " should have reached EOF or should have been aborted but got [eof=" + s3Stream.isEof() - + ", aborted=" + s3Stream.isAborted() + ']', s3Stream.isEof() || s3Stream.isAborted()); + assertTrue( + "Stream " + + toString() + + " should have reached EOF or should have been aborted but got [eof=" + + s3Stream.isEof() + + ", aborted=" + + s3Stream.isAborted() + + ']', + s3Stream.isEof() || s3Stream.isAborted() + ); } else { assertThat(in, instanceOf(ByteArrayInputStream.class)); assertThat(((ByteArrayInputStream) in).available(), equalTo(0)); diff --git a/plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/S3BlobStoreContainerTests.java b/plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/S3BlobStoreContainerTests.java index f7da3bd619367..ade8c8a47be84 100644 --- a/plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/S3BlobStoreContainerTests.java +++ b/plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/S3BlobStoreContainerTests.java @@ -23,10 +23,11 @@ import com.amazonaws.services.s3.model.StorageClass; import com.amazonaws.services.s3.model.UploadPartRequest; import com.amazonaws.services.s3.model.UploadPartResult; + import org.elasticsearch.common.blobstore.BlobPath; import org.elasticsearch.common.blobstore.BlobStoreException; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.unit.ByteSizeUnit; +import org.elasticsearch.core.Tuple; import org.elasticsearch.test.ESTestCase; import org.mockito.ArgumentCaptor; @@ -41,12 +42,12 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.instanceOf; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import static org.mockito.Mockito.doAnswer; public class S3BlobStoreContainerTests extends ESTestCase { @@ -55,8 +56,10 @@ public void testExecuteSingleUploadBlobSizeTooLarge() { final S3BlobStore blobStore = mock(S3BlobStore.class); final S3BlobContainer blobContainer = new S3BlobContainer(mock(BlobPath.class), blobStore); - final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> - blobContainer.executeSingleUpload(blobStore, randomAlphaOfLengthBetween(1, 10), null, blobSize)); + final IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> blobContainer.executeSingleUpload(blobStore, randomAlphaOfLengthBetween(1, 10), null, blobSize) + ); assertEquals("Upload request size [" + blobSize + "] can't be larger than 5gb", e.getMessage()); } @@ -67,8 +70,10 @@ public void testExecuteSingleUploadBlobSizeLargerThanBufferSize() { final S3BlobContainer blobContainer = new S3BlobContainer(mock(BlobPath.class), blobStore); final String blobName = randomAlphaOfLengthBetween(1, 10); - final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> - blobContainer.executeSingleUpload(blobStore, blobName, new ByteArrayInputStream(new byte[0]), ByteSizeUnit.MB.toBytes(2))); + final IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> blobContainer.executeSingleUpload(blobStore, blobName, new ByteArrayInputStream(new byte[0]), ByteSizeUnit.MB.toBytes(2)) + ); assertEquals("Upload request size [2097152] can't be larger than buffer size", e.getMessage()); } @@ -128,8 +133,9 @@ public void testExecuteMultipartUploadBlobSizeTooLarge() { final S3BlobStore blobStore = mock(S3BlobStore.class); final S3BlobContainer blobContainer = new S3BlobContainer(mock(BlobPath.class), blobStore); - final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> - blobContainer.executeMultipartUpload(blobStore, randomAlphaOfLengthBetween(1, 10), null, blobSize) + final IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> blobContainer.executeMultipartUpload(blobStore, randomAlphaOfLengthBetween(1, 10), null, blobSize) ); assertEquals("Multipart upload request size [" + blobSize + "] can't be larger than 5tb", e.getMessage()); } @@ -139,8 +145,9 @@ public void testExecuteMultipartUploadBlobSizeTooSmall() { final S3BlobStore blobStore = mock(S3BlobStore.class); final S3BlobContainer blobContainer = new S3BlobContainer(mock(BlobPath.class), blobStore); - final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> - blobContainer.executeMultipartUpload(blobStore, randomAlphaOfLengthBetween(1, 10), null, blobSize) + final IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> blobContainer.executeMultipartUpload(blobStore, randomAlphaOfLengthBetween(1, 10), null, blobSize) ); assertEquals("Multipart upload request size [" + blobSize + "] can't be smaller than 5mb", e.getMessage()); } @@ -155,7 +162,7 @@ public void testExecuteMultipartUpload() throws IOException { } final long blobSize = ByteSizeUnit.GB.toBytes(randomIntBetween(1, 128)); - final long bufferSize = ByteSizeUnit.MB.toBytes(randomIntBetween(5, 1024)); + final long bufferSize = ByteSizeUnit.MB.toBytes(randomIntBetween(5, 1024)); final S3BlobStore blobStore = mock(S3BlobStore.class); when(blobStore.bucket()).thenReturn(bucketName); @@ -229,7 +236,7 @@ public void testExecuteMultipartUpload() throws IOException { assertEquals(i + 1, uploadRequest.getPartNumber()); assertEquals(inputStream, uploadRequest.getInputStream()); - if (i == (uploadRequests.size() -1)) { + if (i == (uploadRequests.size() - 1)) { assertTrue(uploadRequest.isLastPart()); assertEquals(numberOfParts.v2().longValue(), uploadRequest.getPartSize()); } else { @@ -253,7 +260,7 @@ public void testExecuteMultipartUploadAborted() { final BlobPath blobPath = BlobPath.EMPTY; final long blobSize = ByteSizeUnit.MB.toBytes(765); - final long bufferSize = ByteSizeUnit.MB.toBytes(150); + final long bufferSize = ByteSizeUnit.MB.toBytes(150); final S3BlobStore blobStore = mock(S3BlobStore.class); when(blobStore.bucket()).thenReturn(bucketName); @@ -278,8 +285,7 @@ public void testExecuteMultipartUploadAborted() { if (stage == 0) { // Fail the initialization request - when(client.initiateMultipartUpload(any(InitiateMultipartUploadRequest.class))) - .thenThrow(exceptions.get(stage)); + when(client.initiateMultipartUpload(any(InitiateMultipartUploadRequest.class))).thenThrow(exceptions.get(stage)); } else if (stage == 1) { final InitiateMultipartUploadResult initResult = new InitiateMultipartUploadResult(); @@ -287,8 +293,7 @@ public void testExecuteMultipartUploadAborted() { when(client.initiateMultipartUpload(any(InitiateMultipartUploadRequest.class))).thenReturn(initResult); // Fail the upload part request - when(client.uploadPart(any(UploadPartRequest.class))) - .thenThrow(exceptions.get(stage)); + when(client.uploadPart(any(UploadPartRequest.class))).thenThrow(exceptions.get(stage)); } else { final InitiateMultipartUploadResult initResult = new InitiateMultipartUploadResult(); @@ -304,8 +309,7 @@ public void testExecuteMultipartUploadAborted() { }); // Fail the completion request - when(client.completeMultipartUpload(any(CompleteMultipartUploadRequest.class))) - .thenThrow(exceptions.get(stage)); + when(client.completeMultipartUpload(any(CompleteMultipartUploadRequest.class))).thenThrow(exceptions.get(stage)); } final ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass(AbortMultipartUploadRequest.class); @@ -347,8 +351,10 @@ public void testExecuteMultipartUploadAborted() { } public void testNumberOfMultipartsWithZeroPartSize() { - final IllegalArgumentException e = - expectThrows(IllegalArgumentException.class, () -> S3BlobContainer.numberOfMultiparts(randomNonNegativeLong(), 0L)); + final IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> S3BlobContainer.numberOfMultiparts(randomNonNegativeLong(), 0L) + ); assertEquals("Part size must be greater than zero", e.getMessage()); } @@ -373,11 +379,16 @@ public void testNumberOfMultiparts() { } public void testInitCannedACL() { - String[] aclList = new String[]{ - "private", "public-read", "public-read-write", "authenticated-read", - "log-delivery-write", "bucket-owner-read", "bucket-owner-full-control"}; - - //empty acl + String[] aclList = new String[] { + "private", + "public-read", + "public-read-write", + "authenticated-read", + "log-delivery-write", + "bucket-owner-read", + "bucket-owner-full-control" }; + + // empty acl assertThat(S3BlobStore.initCannedACL(null), equalTo(CannedAccessControlList.Private)); assertThat(S3BlobStore.initCannedACL(""), equalTo(CannedAccessControlList.Private)); diff --git a/plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/S3ClientSettingsTests.java b/plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/S3ClientSettingsTests.java index 7773c63351173..4588164d6aead 100644 --- a/plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/S3ClientSettingsTests.java +++ b/plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/S3ClientSettingsTests.java @@ -11,6 +11,7 @@ import com.amazonaws.ClientConfiguration; import com.amazonaws.Protocol; import com.amazonaws.services.s3.AmazonS3Client; + import org.elasticsearch.common.settings.MockSecureSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.test.ESTestCase; @@ -41,8 +42,9 @@ public void testThereIsADefaultClientByDefault() { } public void testDefaultClientSettingsCanBeSet() { - final Map settings = S3ClientSettings.load(Settings.builder() - .put("s3.client.default.max_retries", 10).build()); + final Map settings = S3ClientSettings.load( + Settings.builder().put("s3.client.default.max_retries", 10).build() + ); assertThat(settings.keySet(), contains("default")); final S3ClientSettings defaultSettings = settings.get("default"); @@ -50,8 +52,9 @@ public void testDefaultClientSettingsCanBeSet() { } public void testNondefaultClientCreatedBySettingItsSettings() { - final Map settings = S3ClientSettings.load(Settings.builder() - .put("s3.client.another_client.max_retries", 10).build()); + final Map settings = S3ClientSettings.load( + Settings.builder().put("s3.client.another_client.max_retries", 10).build() + ); assertThat(settings.keySet(), contains("default", "another_client")); final S3ClientSettings defaultSettings = settings.get("default"); @@ -64,24 +67,30 @@ public void testNondefaultClientCreatedBySettingItsSettings() { public void testRejectionOfLoneAccessKey() { final MockSecureSettings secureSettings = new MockSecureSettings(); secureSettings.setString("s3.client.default.access_key", "aws_key"); - final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> S3ClientSettings.load(Settings.builder().setSecureSettings(secureSettings).build())); + final IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> S3ClientSettings.load(Settings.builder().setSecureSettings(secureSettings).build()) + ); assertThat(e.getMessage(), is("Missing secret key for s3 client [default]")); } public void testRejectionOfLoneSecretKey() { final MockSecureSettings secureSettings = new MockSecureSettings(); secureSettings.setString("s3.client.default.secret_key", "aws_key"); - final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> S3ClientSettings.load(Settings.builder().setSecureSettings(secureSettings).build())); + final IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> S3ClientSettings.load(Settings.builder().setSecureSettings(secureSettings).build()) + ); assertThat(e.getMessage(), is("Missing access key for s3 client [default]")); } public void testRejectionOfLoneSessionToken() { final MockSecureSettings secureSettings = new MockSecureSettings(); secureSettings.setString("s3.client.default.session_token", "aws_key"); - final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> S3ClientSettings.load(Settings.builder().setSecureSettings(secureSettings).build())); + final IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> S3ClientSettings.load(Settings.builder().setSecureSettings(secureSettings).build()) + ); assertThat(e.getMessage(), is("Missing access key and secret key for s3 client [default]")); } @@ -114,8 +123,8 @@ public void testRefineWithRepoSettings() { secureSettings.setString("s3.client.default.access_key", "access_key"); secureSettings.setString("s3.client.default.secret_key", "secret_key"); secureSettings.setString("s3.client.default.session_token", "session_token"); - final S3ClientSettings baseSettings = S3ClientSettings.load( - Settings.builder().setSecureSettings(secureSettings).build()).get("default"); + final S3ClientSettings baseSettings = S3ClientSettings.load(Settings.builder().setSecureSettings(secureSettings).build()) + .get("default"); { final S3ClientSettings refinedSettings = baseSettings.refine(Settings.EMPTY); @@ -144,14 +153,16 @@ public void testRefineWithRepoSettings() { public void testPathStyleAccessCanBeSet() { final Map settings = S3ClientSettings.load( - Settings.builder().put("s3.client.other.path_style_access", true).build()); + Settings.builder().put("s3.client.other.path_style_access", true).build() + ); assertThat(settings.get("default").pathStyleAccess, is(false)); assertThat(settings.get("other").pathStyleAccess, is(true)); } public void testUseChunkedEncodingCanBeSet() { final Map settings = S3ClientSettings.load( - Settings.builder().put("s3.client.other.disable_chunked_encoding", true).build()); + Settings.builder().put("s3.client.other.disable_chunked_encoding", true).build() + ); assertThat(settings.get("default").disableChunkedEncoding, is(false)); assertThat(settings.get("other").disableChunkedEncoding, is(true)); } @@ -159,7 +170,8 @@ public void testUseChunkedEncodingCanBeSet() { public void testRegionCanBeSet() { final String region = randomAlphaOfLength(5); final Map settings = S3ClientSettings.load( - Settings.builder().put("s3.client.other.region", region).build()); + Settings.builder().put("s3.client.other.region", region).build() + ); assertThat(settings.get("default").region, is("")); assertThat(settings.get("other").region, is(region)); try (S3Service s3Service = new S3Service()) { @@ -171,7 +183,8 @@ public void testRegionCanBeSet() { public void testSignerOverrideCanBeSet() { final String signerOverride = randomAlphaOfLength(5); final Map settings = S3ClientSettings.load( - Settings.builder().put("s3.client.other.signer_override", signerOverride).build()); + Settings.builder().put("s3.client.other.signer_override", signerOverride).build() + ); assertThat(settings.get("default").region, is("")); assertThat(settings.get("other").signerOverride, is(signerOverride)); ClientConfiguration defaultConfiguration = S3Service.buildConfiguration(settings.get("default")); diff --git a/plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/S3RepositoryTests.java b/plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/S3RepositoryTests.java index 4b3dfe442ad96..c92e50b2037b2 100644 --- a/plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/S3RepositoryTests.java +++ b/plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/S3RepositoryTests.java @@ -9,17 +9,18 @@ package org.elasticsearch.repositories.s3; import com.amazonaws.services.s3.AbstractAmazonS3; + import org.elasticsearch.cluster.metadata.RepositoryMetadata; import org.elasticsearch.common.settings.ClusterSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.common.util.MockBigArrays; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.indices.recovery.RecoverySettings; import org.elasticsearch.repositories.RepositoryException; import org.elasticsearch.repositories.blobstore.BlobStoreTestUtil; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.hamcrest.Matchers; import java.util.Map; @@ -46,19 +47,16 @@ public AmazonS3Reference client(RepositoryMetadata repositoryMetadata) { } @Override - public void refreshAndClearCache(Map clientsSettings) { - } + public void refreshAndClearCache(Map clientsSettings) {} @Override - public void close() { - } + public void close() {} } public void testInvalidChunkBufferSizeSettings() { // chunk < buffer should fail final Settings s1 = bufferAndChunkSettings(10, 5); - final Exception e1 = expectThrows(RepositoryException.class, - () -> createS3Repo(getRepositoryMetadata(s1))); + final Exception e1 = expectThrows(RepositoryException.class, () -> createS3Repo(getRepositoryMetadata(s1))); assertThat(e1.getMessage(), containsString("chunk_size (5mb) can't be lower than buffer_size (10mb)")); // chunk > buffer should pass final Settings s2 = bufferAndChunkSettings(5, 10); @@ -68,22 +66,24 @@ public void testInvalidChunkBufferSizeSettings() { createS3Repo(getRepositoryMetadata(s3)).close(); // buffer < 5mb should fail final Settings s4 = bufferAndChunkSettings(4, 10); - final IllegalArgumentException e2 = expectThrows(IllegalArgumentException.class, - () -> createS3Repo(getRepositoryMetadata(s4)) - .close()); + final IllegalArgumentException e2 = expectThrows( + IllegalArgumentException.class, + () -> createS3Repo(getRepositoryMetadata(s4)).close() + ); assertThat(e2.getMessage(), containsString("failed to parse value [4mb] for setting [buffer_size], must be >= [5mb]")); final Settings s5 = bufferAndChunkSettings(5, 6000000); - final IllegalArgumentException e3 = expectThrows(IllegalArgumentException.class, - () -> createS3Repo(getRepositoryMetadata(s5)) - .close()); + final IllegalArgumentException e3 = expectThrows( + IllegalArgumentException.class, + () -> createS3Repo(getRepositoryMetadata(s5)).close() + ); assertThat(e3.getMessage(), containsString("failed to parse value [6000000mb] for setting [chunk_size], must be <= [5tb]")); } private Settings bufferAndChunkSettings(long buffer, long chunk) { return Settings.builder() - .put(S3Repository.BUFFER_SIZE_SETTING.getKey(), new ByteSizeValue(buffer, ByteSizeUnit.MB).getStringRep()) - .put(S3Repository.CHUNK_SIZE_SETTING.getKey(), new ByteSizeValue(chunk, ByteSizeUnit.MB).getStringRep()) - .build(); + .put(S3Repository.BUFFER_SIZE_SETTING.getKey(), new ByteSizeValue(buffer, ByteSizeUnit.MB).getStringRep()) + .put(S3Repository.CHUNK_SIZE_SETTING.getKey(), new ByteSizeValue(chunk, ByteSizeUnit.MB).getStringRep()) + .build(); } private RepositoryMetadata getRepositoryMetadata(Settings settings) { @@ -91,8 +91,11 @@ private RepositoryMetadata getRepositoryMetadata(Settings settings) { } public void testBasePathSetting() { - final RepositoryMetadata metadata = new RepositoryMetadata("dummy-repo", "mock", Settings.builder() - .put(S3Repository.BASE_PATH_SETTING.getKey(), "foo/bar").build()); + final RepositoryMetadata metadata = new RepositoryMetadata( + "dummy-repo", + "mock", + Settings.builder().put(S3Repository.BASE_PATH_SETTING.getKey(), "foo/bar").build() + ); try (S3Repository s3repo = createS3Repo(metadata)) { assertEquals("foo/bar/", s3repo.basePath().buildAsString()); } @@ -103,7 +106,7 @@ public void testDefaultBufferSize() { try (S3Repository s3repo = createS3Repo(metadata)) { assertThat(s3repo.getBlobStore(), is(nullValue())); s3repo.start(); - final long defaultBufferSize = ((S3BlobStore)s3repo.blobStore()).bufferSizeInBytes(); + final long defaultBufferSize = ((S3BlobStore) s3repo.blobStore()).bufferSizeInBytes(); assertThat(s3repo.getBlobStore(), not(nullValue())); assertThat(defaultBufferSize, Matchers.lessThanOrEqualTo(100L * 1024 * 1024)); assertThat(defaultBufferSize, Matchers.greaterThanOrEqualTo(5L * 1024 * 1024)); @@ -111,9 +114,14 @@ public void testDefaultBufferSize() { } private S3Repository createS3Repo(RepositoryMetadata metadata) { - return new S3Repository(metadata, NamedXContentRegistry.EMPTY, new DummyS3Service(), BlobStoreTestUtil.mockClusterService(), - MockBigArrays.NON_RECYCLING_INSTANCE, - new RecoverySettings(Settings.EMPTY, new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS))) { + return new S3Repository( + metadata, + NamedXContentRegistry.EMPTY, + new DummyS3Service(), + BlobStoreTestUtil.mockClusterService(), + MockBigArrays.NON_RECYCLING_INSTANCE, + new RecoverySettings(Settings.EMPTY, new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS)) + ) { @Override protected void assertSnapshotOrGenericThread() { // eliminate thread name check as we create repo manually on test/main threads diff --git a/plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/S3RetryingInputStreamTests.java b/plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/S3RetryingInputStreamTests.java index 72294609ed7b9..f15e4ada6b609 100644 --- a/plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/S3RetryingInputStreamTests.java +++ b/plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/S3RetryingInputStreamTests.java @@ -12,9 +12,10 @@ import com.amazonaws.services.s3.model.GetObjectRequest; import com.amazonaws.services.s3.model.S3Object; import com.amazonaws.services.s3.model.S3ObjectInputStream; + import org.apache.http.client.methods.HttpGet; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.io.Streams; +import org.elasticsearch.core.Nullable; import org.elasticsearch.test.ESTestCase; import java.io.ByteArrayInputStream; @@ -79,11 +80,8 @@ public void testRangeInputStreamIsAborted() throws IOException { assertThat(stream.isAborted(), is(true)); } - private S3RetryingInputStream createInputStream( - final byte[] data, - @Nullable final Integer position, - @Nullable final Integer length - ) throws IOException { + private S3RetryingInputStream createInputStream(final byte[] data, @Nullable final Integer position, @Nullable final Integer length) + throws IOException { final S3Object s3Object = new S3Object(); final AmazonS3 client = mock(AmazonS3.class); when(client.getObject(any(GetObjectRequest.class))).thenReturn(s3Object); diff --git a/plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/S3ServiceTests.java b/plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/S3ServiceTests.java index c1a1bdd01fde6..c21eaeebd33ed 100644 --- a/plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/S3ServiceTests.java +++ b/plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/S3ServiceTests.java @@ -8,7 +8,6 @@ package org.elasticsearch.repositories.s3; import org.elasticsearch.cluster.metadata.RepositoryMetadata; - import org.elasticsearch.common.settings.Settings; import org.elasticsearch.test.ESTestCase; diff --git a/plugins/repository-s3/src/yamlRestTest/java/org/elasticsearch/repositories/s3/RepositoryS3ClientYamlTestSuiteIT.java b/plugins/repository-s3/src/yamlRestTest/java/org/elasticsearch/repositories/s3/RepositoryS3ClientYamlTestSuiteIT.java index 511020503489a..1cbdf357d821b 100644 --- a/plugins/repository-s3/src/yamlRestTest/java/org/elasticsearch/repositories/s3/RepositoryS3ClientYamlTestSuiteIT.java +++ b/plugins/repository-s3/src/yamlRestTest/java/org/elasticsearch/repositories/s3/RepositoryS3ClientYamlTestSuiteIT.java @@ -25,4 +25,3 @@ public static Iterable parameters() throws Exception { return ESClientYamlSuiteTestCase.createParameters(); } } - diff --git a/plugins/store-smb/src/internalClusterTest/java/org/elasticsearch/index/store/smb/SmbMMapFsTests.java b/plugins/store-smb/src/internalClusterTest/java/org/elasticsearch/index/store/smb/SmbMMapFsTests.java index ee9067f64c557..565354a934272 100644 --- a/plugins/store-smb/src/internalClusterTest/java/org/elasticsearch/index/store/smb/SmbMMapFsTests.java +++ b/plugins/store-smb/src/internalClusterTest/java/org/elasticsearch/index/store/smb/SmbMMapFsTests.java @@ -10,15 +10,11 @@ import org.elasticsearch.common.settings.Settings; - public class SmbMMapFsTests extends AbstractAzureFsTestCase { @Override public Settings indexSettings() { - return Settings.builder() - .put(super.indexSettings()) - .put("index.store.type", "smb_mmap_fs") - .build(); + return Settings.builder().put(super.indexSettings()).put("index.store.type", "smb_mmap_fs").build(); } } diff --git a/plugins/store-smb/src/internalClusterTest/java/org/elasticsearch/index/store/smb/SmbNIOFSTests.java b/plugins/store-smb/src/internalClusterTest/java/org/elasticsearch/index/store/smb/SmbNIOFSTests.java index bcfc913b64efd..8468c33686b3b 100644 --- a/plugins/store-smb/src/internalClusterTest/java/org/elasticsearch/index/store/smb/SmbNIOFSTests.java +++ b/plugins/store-smb/src/internalClusterTest/java/org/elasticsearch/index/store/smb/SmbNIOFSTests.java @@ -10,13 +10,9 @@ import org.elasticsearch.common.settings.Settings; - public class SmbNIOFSTests extends AbstractAzureFsTestCase { @Override public Settings indexSettings() { - return Settings.builder() - .put(super.indexSettings()) - .put("index.store.type", randomFrom("smb_simple_fs", "smb_nio_fs")) - .build(); + return Settings.builder().put(super.indexSettings()).put("index.store.type", randomFrom("smb_simple_fs", "smb_nio_fs")).build(); } } diff --git a/plugins/store-smb/src/main/java/org/elasticsearch/index/store/smb/SmbDirectoryWrapper.java b/plugins/store-smb/src/main/java/org/elasticsearch/index/store/smb/SmbDirectoryWrapper.java index 1e0f9a008a904..2e3d6eea69b93 100644 --- a/plugins/store-smb/src/main/java/org/elasticsearch/index/store/smb/SmbDirectoryWrapper.java +++ b/plugins/store-smb/src/main/java/org/elasticsearch/index/store/smb/SmbDirectoryWrapper.java @@ -50,22 +50,33 @@ final class SmbFSIndexOutput extends OutputStreamIndexOutput { static final int CHUNK_SIZE = 8192; SmbFSIndexOutput(String name) throws IOException { - super("SmbFSIndexOutput(path=\"" + fsDirectory.getDirectory().resolve(name) + "\")", name, - new FilterOutputStream(Channels.newOutputStream(Files.newByteChannel(fsDirectory.getDirectory().resolve(name), - StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, - StandardOpenOption.READ, StandardOpenOption.WRITE))) { - // This implementation ensures, that we never write more than CHUNK_SIZE bytes: - @Override - public void write(byte[] b, int offset, int length) throws IOException { - while (length > 0) { - final int chunk = Math.min(length, CHUNK_SIZE); - out.write(b, offset, chunk); - length -= chunk; - offset += chunk; - } + super( + "SmbFSIndexOutput(path=\"" + fsDirectory.getDirectory().resolve(name) + "\")", + name, + new FilterOutputStream( + Channels.newOutputStream( + Files.newByteChannel( + fsDirectory.getDirectory().resolve(name), + StandardOpenOption.CREATE, + StandardOpenOption.TRUNCATE_EXISTING, + StandardOpenOption.READ, + StandardOpenOption.WRITE + ) + ) + ) { + // This implementation ensures, that we never write more than CHUNK_SIZE bytes: + @Override + public void write(byte[] b, int offset, int length) throws IOException { + while (length > 0) { + final int chunk = Math.min(length, CHUNK_SIZE); + out.write(b, offset, chunk); + length -= chunk; + offset += chunk; } - }, - CHUNK_SIZE); + } + }, + CHUNK_SIZE + ); } } } diff --git a/plugins/store-smb/src/main/java/org/elasticsearch/index/store/smb/SmbMmapFsDirectoryFactory.java b/plugins/store-smb/src/main/java/org/elasticsearch/index/store/smb/SmbMmapFsDirectoryFactory.java index 66394e7729748..ac95538553b92 100644 --- a/plugins/store-smb/src/main/java/org/elasticsearch/index/store/smb/SmbMmapFsDirectoryFactory.java +++ b/plugins/store-smb/src/main/java/org/elasticsearch/index/store/smb/SmbMmapFsDirectoryFactory.java @@ -23,7 +23,12 @@ public final class SmbMmapFsDirectoryFactory extends FsDirectoryFactory { @Override protected Directory newFSDirectory(Path location, LockFactory lockFactory, IndexSettings indexSettings) throws IOException { - return new SmbDirectoryWrapper(setPreload(new MMapDirectory(location, lockFactory), lockFactory, new HashSet<>( - indexSettings.getValue(IndexModule.INDEX_STORE_PRE_LOAD_SETTING)))); + return new SmbDirectoryWrapper( + setPreload( + new MMapDirectory(location, lockFactory), + lockFactory, + new HashSet<>(indexSettings.getValue(IndexModule.INDEX_STORE_PRE_LOAD_SETTING)) + ) + ); } } diff --git a/plugins/store-smb/src/main/java/org/elasticsearch/plugin/store/smb/SMBStorePlugin.java b/plugins/store-smb/src/main/java/org/elasticsearch/plugin/store/smb/SMBStorePlugin.java index c524926ba1548..f4e1456c5aec4 100644 --- a/plugins/store-smb/src/main/java/org/elasticsearch/plugin/store/smb/SMBStorePlugin.java +++ b/plugins/store-smb/src/main/java/org/elasticsearch/plugin/store/smb/SMBStorePlugin.java @@ -20,10 +20,13 @@ public class SMBStorePlugin extends Plugin implements IndexStorePlugin { @Override public Map getDirectoryFactories() { return Map.of( - "smb_mmap_fs", new SmbMmapFsDirectoryFactory(), - "smb_simple_fs", new SmbNIOFSDirectoryFactory(), - "smb_nio_fs", new SmbNIOFSDirectoryFactory() - ); + "smb_mmap_fs", + new SmbMmapFsDirectoryFactory(), + "smb_simple_fs", + new SmbNIOFSDirectoryFactory(), + "smb_nio_fs", + new SmbNIOFSDirectoryFactory() + ); } } diff --git a/plugins/store-smb/src/test/java/org/elasticsearch/index/store/smb/SmbMMapDirectoryTests.java b/plugins/store-smb/src/test/java/org/elasticsearch/index/store/smb/SmbMMapDirectoryTests.java index 6a2e60ce16cdc..361c3e555a512 100644 --- a/plugins/store-smb/src/test/java/org/elasticsearch/index/store/smb/SmbMMapDirectoryTests.java +++ b/plugins/store-smb/src/test/java/org/elasticsearch/index/store/smb/SmbMMapDirectoryTests.java @@ -8,12 +8,13 @@ package org.elasticsearch.index.store.smb; -import java.io.IOException; -import java.nio.file.Path; import org.apache.lucene.store.Directory; import org.apache.lucene.store.MMapDirectory; import org.elasticsearch.index.store.EsBaseDirectoryTestCase; +import java.io.IOException; +import java.nio.file.Path; + public class SmbMMapDirectoryTests extends EsBaseDirectoryTestCase { @Override diff --git a/plugins/store-smb/src/yamlRestTest/java/org/elasticsearch/index/store/smb/StoreSmbClientYamlTestSuiteIT.java b/plugins/store-smb/src/yamlRestTest/java/org/elasticsearch/index/store/smb/StoreSmbClientYamlTestSuiteIT.java index 9a36ce37844ad..d7317ec32034a 100644 --- a/plugins/store-smb/src/yamlRestTest/java/org/elasticsearch/index/store/smb/StoreSmbClientYamlTestSuiteIT.java +++ b/plugins/store-smb/src/yamlRestTest/java/org/elasticsearch/index/store/smb/StoreSmbClientYamlTestSuiteIT.java @@ -25,4 +25,3 @@ public static Iterable parameters() throws Exception { return ESClientYamlSuiteTestCase.createParameters(); } } - diff --git a/plugins/transport-nio/src/internalClusterTest/java/org/elasticsearch/http/nio/NioPipeliningIT.java b/plugins/transport-nio/src/internalClusterTest/java/org/elasticsearch/http/nio/NioPipeliningIT.java index 6c4c742ccafe7..a375d2a972666 100644 --- a/plugins/transport-nio/src/internalClusterTest/java/org/elasticsearch/http/nio/NioPipeliningIT.java +++ b/plugins/transport-nio/src/internalClusterTest/java/org/elasticsearch/http/nio/NioPipeliningIT.java @@ -9,6 +9,7 @@ package org.elasticsearch.http.nio; import io.netty.handler.codec.http.FullHttpResponse; + import org.elasticsearch.NioIntegTestCase; import org.elasticsearch.common.transport.TransportAddress; import org.elasticsearch.http.HttpServerTransport; @@ -30,7 +31,7 @@ protected boolean addMockHttpTransport() { } public void testThatNioHttpServerSupportsPipelining() throws Exception { - String[] requests = new String[]{"/", "/_nodes/stats", "/", "/_cluster/state", "/"}; + String[] requests = new String[] { "/", "/_nodes/stats", "/", "/_cluster/state", "/" }; HttpServerTransport httpServerTransport = internalCluster().getInstance(HttpServerTransport.class); TransportAddress[] boundAddresses = httpServerTransport.boundAddress().boundAddresses(); diff --git a/plugins/transport-nio/src/internalClusterTest/java/org/elasticsearch/transport/nio/NioTransportLoggingIT.java b/plugins/transport-nio/src/internalClusterTest/java/org/elasticsearch/transport/nio/NioTransportLoggingIT.java index 54a1c0b451030..bbef4800dc7b4 100644 --- a/plugins/transport-nio/src/internalClusterTest/java/org/elasticsearch/transport/nio/NioTransportLoggingIT.java +++ b/plugins/transport-nio/src/internalClusterTest/java/org/elasticsearch/transport/nio/NioTransportLoggingIT.java @@ -44,28 +44,32 @@ public void tearDown() throws Exception { @TestLogging(value = "org.elasticsearch.transport.TransportLogger:trace", reason = "to ensure we log network events on TRACE level") public void testLoggingHandler() { - final String writePattern = - ".*\\[length: \\d+" + - ", request id: \\d+" + - ", type: request" + - ", version: .*" + - ", action: cluster:monitor/nodes/hot_threads\\[n\\]\\]" + - " WRITE: \\d+B"; - final MockLogAppender.LoggingExpectation writeExpectation = - new MockLogAppender.PatternSeenEventExpectation( - "hot threads request", TransportLogger.class.getCanonicalName(), Level.TRACE, writePattern); + final String writePattern = ".*\\[length: \\d+" + + ", request id: \\d+" + + ", type: request" + + ", version: .*" + + ", action: cluster:monitor/nodes/hot_threads\\[n\\]\\]" + + " WRITE: \\d+B"; + final MockLogAppender.LoggingExpectation writeExpectation = new MockLogAppender.PatternSeenEventExpectation( + "hot threads request", + TransportLogger.class.getCanonicalName(), + Level.TRACE, + writePattern + ); - final String readPattern = - ".*\\[length: \\d+" + - ", request id: \\d+" + - ", type: request" + - ", version: .*" + - ", action: cluster:monitor/nodes/hot_threads\\[n\\]\\]" + - " READ: \\d+B"; + final String readPattern = ".*\\[length: \\d+" + + ", request id: \\d+" + + ", type: request" + + ", version: .*" + + ", action: cluster:monitor/nodes/hot_threads\\[n\\]\\]" + + " READ: \\d+B"; - final MockLogAppender.LoggingExpectation readExpectation = - new MockLogAppender.PatternSeenEventExpectation( - "hot threads request", TransportLogger.class.getCanonicalName(), Level.TRACE, readPattern); + final MockLogAppender.LoggingExpectation readExpectation = new MockLogAppender.PatternSeenEventExpectation( + "hot threads request", + TransportLogger.class.getCanonicalName(), + Level.TRACE, + readPattern + ); appender.addExpectation(writeExpectation); appender.addExpectation(readExpectation); @@ -75,12 +79,22 @@ public void testLoggingHandler() { @TestLogging(value = "org.elasticsearch.transport.TcpTransport:DEBUG", reason = "to ensure we log connection events on DEBUG level") public void testConnectionLogging() throws IOException { - appender.addExpectation(new MockLogAppender.PatternSeenEventExpectation("open connection log", - TcpTransport.class.getCanonicalName(), Level.DEBUG, - ".*opened transport connection \\[[1-9][0-9]*\\] to .*")); - appender.addExpectation(new MockLogAppender.PatternSeenEventExpectation("close connection log", - TcpTransport.class.getCanonicalName(), Level.DEBUG, - ".*closed transport connection \\[[1-9][0-9]*\\] to .* with age \\[[0-9]+ms\\].*")); + appender.addExpectation( + new MockLogAppender.PatternSeenEventExpectation( + "open connection log", + TcpTransport.class.getCanonicalName(), + Level.DEBUG, + ".*opened transport connection \\[[1-9][0-9]*\\] to .*" + ) + ); + appender.addExpectation( + new MockLogAppender.PatternSeenEventExpectation( + "close connection log", + TcpTransport.class.getCanonicalName(), + Level.DEBUG, + ".*closed transport connection \\[[1-9][0-9]*\\] to .* with age \\[[0-9]+ms\\].*" + ) + ); final String nodeName = internalCluster().startNode(); internalCluster().stopRandomNode(InternalTestCluster.nameFilter(nodeName)); diff --git a/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/ByteBufUtils.java b/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/ByteBufUtils.java index bf5985273d471..a6749805c1da1 100644 --- a/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/ByteBufUtils.java +++ b/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/ByteBufUtils.java @@ -10,6 +10,7 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.CompositeByteBuf; import io.netty.buffer.Unpooled; + import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.BytesRefIterator; import org.elasticsearch.common.bytes.BytesArray; diff --git a/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/HttpReadWriteHandler.java b/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/HttpReadWriteHandler.java index 592191dfa9b10..7065122fcde5c 100644 --- a/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/HttpReadWriteHandler.java +++ b/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/HttpReadWriteHandler.java @@ -15,6 +15,7 @@ import io.netty.handler.codec.http.HttpObjectAggregator; import io.netty.handler.codec.http.HttpRequestDecoder; import io.netty.handler.codec.http.HttpResponseEncoder; + import org.elasticsearch.core.TimeValue; import org.elasticsearch.http.HttpHandlingSettings; import org.elasticsearch.http.HttpPipelinedRequest; @@ -46,8 +47,13 @@ public class HttpReadWriteHandler implements NioChannelHandler { private boolean requestSinceReadTimeoutTrigger = false; private int inFlightRequests = 0; - public HttpReadWriteHandler(NioHttpChannel nioHttpChannel, NioHttpServerTransport transport, HttpHandlingSettings settings, - TaskScheduler taskScheduler, LongSupplier nanoClock) { + public HttpReadWriteHandler( + NioHttpChannel nioHttpChannel, + NioHttpServerTransport transport, + HttpHandlingSettings settings, + TaskScheduler taskScheduler, + LongSupplier nanoClock + ) { this.nioHttpChannel = nioHttpChannel; this.transport = transport; this.taskScheduler = taskScheduler; @@ -55,8 +61,11 @@ public HttpReadWriteHandler(NioHttpChannel nioHttpChannel, NioHttpServerTranspor this.readTimeoutNanos = TimeUnit.MILLISECONDS.toNanos(settings.getReadTimeoutMillis()); List handlers = new ArrayList<>(8); - HttpRequestDecoder decoder = new HttpRequestDecoder(settings.getMaxInitialLineLength(), settings.getMaxHeaderSize(), - settings.getMaxChunkSize()); + HttpRequestDecoder decoder = new HttpRequestDecoder( + settings.getMaxInitialLineLength(), + settings.getMaxHeaderSize(), + settings.getMaxChunkSize() + ); decoder.setCumulator(ByteToMessageDecoder.COMPOSITE_CUMULATOR); handlers.add(decoder); handlers.add(new HttpContentDecompressor()); @@ -162,11 +171,18 @@ private void scheduleReadTimeout() { } private static boolean assertMessageTypes(Object message) { - assert message instanceof HttpPipelinedResponse : "This channel only supports messages that are of type: " - + HttpPipelinedResponse.class + ". Found type: " + message.getClass() + "."; - assert ((HttpPipelinedResponse) message).getDelegateRequest() instanceof NioHttpResponse : - "This channel only pipelined responses with a delegate of type: " + NioHttpResponse.class + - ". Found type: " + ((HttpPipelinedResponse) message).getDelegateRequest().getClass() + "."; + assert message instanceof HttpPipelinedResponse + : "This channel only supports messages that are of type: " + + HttpPipelinedResponse.class + + ". Found type: " + + message.getClass() + + "."; + assert ((HttpPipelinedResponse) message).getDelegateRequest() instanceof NioHttpResponse + : "This channel only pipelined responses with a delegate of type: " + + NioHttpResponse.class + + ". Found type: " + + ((HttpPipelinedResponse) message).getDelegateRequest().getClass() + + "."; return true; } } diff --git a/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/NettyAdaptor.java b/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/NettyAdaptor.java index 23e798a3e27d5..d709338e5c221 100644 --- a/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/NettyAdaptor.java +++ b/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/NettyAdaptor.java @@ -16,6 +16,7 @@ import io.netty.channel.ChannelOutboundHandlerAdapter; import io.netty.channel.ChannelPromise; import io.netty.channel.embedded.EmbeddedChannel; + import org.elasticsearch.ExceptionsHelper; import org.elasticsearch.nio.FlushOperation; import org.elasticsearch.nio.Page; diff --git a/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/NettyListener.java b/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/NettyListener.java index ba09a1c1ec95f..ba912c1947888 100644 --- a/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/NettyListener.java +++ b/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/NettyListener.java @@ -12,6 +12,7 @@ import io.netty.channel.ChannelPromise; import io.netty.util.concurrent.Future; import io.netty.util.concurrent.GenericFutureListener; + import org.elasticsearch.ExceptionsHelper; import org.elasticsearch.common.util.concurrent.FutureUtils; diff --git a/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/NioHttpChannel.java b/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/NioHttpChannel.java index 16ece82eec069..8e4141372e100 100644 --- a/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/NioHttpChannel.java +++ b/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/NioHttpChannel.java @@ -32,9 +32,6 @@ public void addCloseListener(ActionListener listener) { @Override public String toString() { - return "NioHttpChannel{" + - "localAddress=" + getLocalAddress() + - ", remoteAddress=" + getRemoteAddress() + - '}'; + return "NioHttpChannel{" + "localAddress=" + getLocalAddress() + ", remoteAddress=" + getRemoteAddress() + '}'; } } diff --git a/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/NioHttpPipeliningHandler.java b/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/NioHttpPipeliningHandler.java index a8834cdc46646..ed3f4395e766f 100644 --- a/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/NioHttpPipeliningHandler.java +++ b/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/NioHttpPipeliningHandler.java @@ -11,6 +11,7 @@ import io.netty.channel.ChannelDuplexHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelPromise; + import org.apache.logging.log4j.Logger; import org.elasticsearch.core.Tuple; import org.elasticsearch.http.HttpPipelinedRequest; diff --git a/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/NioHttpRequest.java b/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/NioHttpRequest.java index 16590c95dd274..77bb0468a0a43 100644 --- a/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/NioHttpRequest.java +++ b/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/NioHttpRequest.java @@ -19,6 +19,7 @@ import io.netty.handler.codec.http.cookie.Cookie; import io.netty.handler.codec.http.cookie.ServerCookieDecoder; import io.netty.handler.codec.http.cookie.ServerCookieEncoder; + import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.http.HttpRequest; import org.elasticsearch.rest.RestRequest; @@ -43,22 +44,44 @@ public class NioHttpRequest implements HttpRequest { private final boolean pooled; NioHttpRequest(FullHttpRequest request) { - this(request, new HttpHeadersMap(request.headers()), new AtomicBoolean(false), true, - ByteBufUtils.toBytesReference(request.content())); + this( + request, + new HttpHeadersMap(request.headers()), + new AtomicBoolean(false), + true, + ByteBufUtils.toBytesReference(request.content()) + ); } NioHttpRequest(FullHttpRequest request, Exception inboundException) { - this(request, new HttpHeadersMap(request.headers()), new AtomicBoolean(false), true, - ByteBufUtils.toBytesReference(request.content()), inboundException); + this( + request, + new HttpHeadersMap(request.headers()), + new AtomicBoolean(false), + true, + ByteBufUtils.toBytesReference(request.content()), + inboundException + ); } - private NioHttpRequest(FullHttpRequest request, HttpHeadersMap headers, AtomicBoolean released, boolean pooled, - BytesReference content) { + private NioHttpRequest( + FullHttpRequest request, + HttpHeadersMap headers, + AtomicBoolean released, + boolean pooled, + BytesReference content + ) { this(request, headers, released, pooled, content, null); } - private NioHttpRequest(FullHttpRequest request, HttpHeadersMap headers, AtomicBoolean released, boolean pooled, - BytesReference content, Exception inboundException) { + private NioHttpRequest( + FullHttpRequest request, + HttpHeadersMap headers, + AtomicBoolean released, + boolean pooled, + BytesReference content, + Exception inboundException + ) { this.request = request; this.headers = headers; this.content = content; @@ -70,17 +93,13 @@ private NioHttpRequest(FullHttpRequest request, HttpHeadersMap headers, AtomicBo @Override public RestRequest.Method method() { HttpMethod httpMethod = request.method(); - if (httpMethod == HttpMethod.GET) - return RestRequest.Method.GET; + if (httpMethod == HttpMethod.GET) return RestRequest.Method.GET; - if (httpMethod == HttpMethod.POST) - return RestRequest.Method.POST; + if (httpMethod == HttpMethod.POST) return RestRequest.Method.POST; - if (httpMethod == HttpMethod.PUT) - return RestRequest.Method.PUT; + if (httpMethod == HttpMethod.PUT) return RestRequest.Method.PUT; - if (httpMethod == HttpMethod.DELETE) - return RestRequest.Method.DELETE; + if (httpMethod == HttpMethod.DELETE) return RestRequest.Method.DELETE; if (httpMethod == HttpMethod.HEAD) { return RestRequest.Method.HEAD; @@ -132,9 +151,19 @@ public HttpRequest releaseAndCopy() { try { final ByteBuf copiedContent = Unpooled.copiedBuffer(request.content()); return new NioHttpRequest( - new DefaultFullHttpRequest(request.protocolVersion(), request.method(), request.uri(), copiedContent, request.headers(), - request.trailingHeaders()), - headers, new AtomicBoolean(false), false, ByteBufUtils.toBytesReference(copiedContent)); + new DefaultFullHttpRequest( + request.protocolVersion(), + request.method(), + request.uri(), + copiedContent, + request.headers(), + request.trailingHeaders() + ), + headers, + new AtomicBoolean(false), + false, + ByteBufUtils.toBytesReference(copiedContent) + ); } finally { release(); } @@ -176,8 +205,14 @@ public HttpRequest removeHeader(String header) { HttpHeaders trailingHeaders = new DefaultHttpHeaders(); trailingHeaders.add(request.trailingHeaders()); trailingHeaders.remove(header); - FullHttpRequest requestWithoutHeader = new DefaultFullHttpRequest(request.protocolVersion(), request.method(), request.uri(), - request.content(), headersWithoutContentTypeHeader, trailingHeaders); + FullHttpRequest requestWithoutHeader = new DefaultFullHttpRequest( + request.protocolVersion(), + request.method(), + request.uri(), + request.content(), + headersWithoutContentTypeHeader, + trailingHeaders + ); return new NioHttpRequest(requestWithoutHeader, new HttpHeadersMap(requestWithoutHeader.headers()), released, pooled, content); } @@ -268,7 +303,9 @@ public Collection> values() { @Override public Set>> entrySet() { - return httpHeaders.names().stream().map(k -> new AbstractMap.SimpleImmutableEntry<>(k, httpHeaders.getAll(k))) + return httpHeaders.names() + .stream() + .map(k -> new AbstractMap.SimpleImmutableEntry<>(k, httpHeaders.getAll(k))) .collect(Collectors.toSet()); } } diff --git a/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/NioHttpRequestCreator.java b/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/NioHttpRequestCreator.java index 14c2052b82301..5e42f562fe5f2 100644 --- a/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/NioHttpRequestCreator.java +++ b/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/NioHttpRequestCreator.java @@ -12,6 +12,7 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToMessageDecoder; import io.netty.handler.codec.http.FullHttpRequest; + import org.elasticsearch.ExceptionsHelper; import java.util.List; diff --git a/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/NioHttpResponse.java b/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/NioHttpResponse.java index 2757de273c87c..f9667263b866f 100644 --- a/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/NioHttpResponse.java +++ b/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/NioHttpResponse.java @@ -12,6 +12,7 @@ import io.netty.handler.codec.http.HttpHeaders; import io.netty.handler.codec.http.HttpResponseStatus; import io.netty.handler.codec.http.HttpVersion; + import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.http.HttpResponse; import org.elasticsearch.rest.RestStatus; diff --git a/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/NioHttpResponseCreator.java b/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/NioHttpResponseCreator.java index 873887439b2eb..0a983f9979070 100644 --- a/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/NioHttpResponseCreator.java +++ b/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/NioHttpResponseCreator.java @@ -16,6 +16,7 @@ import io.netty.handler.codec.http.DefaultHttpResponse; import io.netty.handler.codec.http.DefaultLastHttpContent; import io.netty.handler.codec.http.HttpResponse; + import org.elasticsearch.core.Booleans; import org.elasticsearch.monitor.jvm.JvmInfo; diff --git a/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/NioHttpServerTransport.java b/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/NioHttpServerTransport.java index fa23d238bf3b7..5dedd3705a93a 100644 --- a/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/NioHttpServerTransport.java +++ b/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/NioHttpServerTransport.java @@ -19,7 +19,6 @@ import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.common.util.PageCacheRecycler; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.http.AbstractHttpServerTransport; import org.elasticsearch.http.HttpChannel; import org.elasticsearch.http.HttpServerChannel; @@ -35,6 +34,7 @@ import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.nio.NioGroupFactory; import org.elasticsearch.transport.nio.PageAllocator; +import org.elasticsearch.xcontent.NamedXContentRegistry; import java.io.IOException; import java.net.InetSocketAddress; @@ -73,9 +73,17 @@ public class NioHttpServerTransport extends AbstractHttpServerTransport { private volatile NioGroup nioGroup; private ChannelFactory channelFactory; - public NioHttpServerTransport(Settings settings, NetworkService networkService, BigArrays bigArrays, - PageCacheRecycler pageCacheRecycler, ThreadPool threadPool, NamedXContentRegistry xContentRegistry, - Dispatcher dispatcher, NioGroupFactory nioGroupFactory, ClusterSettings clusterSettings) { + public NioHttpServerTransport( + Settings settings, + NetworkService networkService, + BigArrays bigArrays, + PageCacheRecycler pageCacheRecycler, + ThreadPool threadPool, + NamedXContentRegistry xContentRegistry, + Dispatcher dispatcher, + NioGroupFactory nioGroupFactory, + ClusterSettings clusterSettings + ) { super(settings, networkService, bigArrays, threadPool, xContentRegistry, dispatcher, clusterSettings); this.pageAllocator = new PageAllocator(pageCacheRecycler); this.nioGroupFactory = nioGroupFactory; @@ -94,10 +102,15 @@ public NioHttpServerTransport(Settings settings, NetworkService networkService, this.tcpSendBufferSize = Math.toIntExact(SETTING_HTTP_TCP_SEND_BUFFER_SIZE.get(settings).getBytes()); this.tcpReceiveBufferSize = Math.toIntExact(SETTING_HTTP_TCP_RECEIVE_BUFFER_SIZE.get(settings).getBytes()); - - logger.debug("using max_chunk_size[{}], max_header_size[{}], max_initial_line_length[{}], max_content_length[{}]," + - " pipelining_max_events[{}]", - maxChunkSize, maxHeaderSize, maxInitialLineLength, maxContentLength, pipeliningMaxEvents); + logger.debug( + "using max_chunk_size[{}], max_header_size[{}], max_initial_line_length[{}], max_content_length[{}]," + + " pipelining_max_events[{}]", + maxChunkSize, + maxHeaderSize, + maxInitialLineLength, + maxContentLength, + pipeliningMaxEvents + ); } public Logger getLogger() { @@ -150,30 +163,58 @@ protected void acceptChannel(NioSocketChannel socketChannel) { private class HttpChannelFactory extends ChannelFactory { private HttpChannelFactory() { - super(tcpNoDelay, tcpKeepAlive, tcpKeepIdle, tcpKeepInterval, tcpKeepCount, reuseAddress, tcpSendBufferSize, - tcpReceiveBufferSize); + super( + tcpNoDelay, + tcpKeepAlive, + tcpKeepIdle, + tcpKeepInterval, + tcpKeepCount, + reuseAddress, + tcpSendBufferSize, + tcpReceiveBufferSize + ); } @Override public NioHttpChannel createChannel(NioSelector selector, SocketChannel channel, Config.Socket socketConfig) { NioHttpChannel httpChannel = new NioHttpChannel(channel); - HttpReadWriteHandler handler = new HttpReadWriteHandler(httpChannel,NioHttpServerTransport.this, - handlingSettings, selector.getTaskScheduler(), threadPool::relativeTimeInMillis); + HttpReadWriteHandler handler = new HttpReadWriteHandler( + httpChannel, + NioHttpServerTransport.this, + handlingSettings, + selector.getTaskScheduler(), + threadPool::relativeTimeInMillis + ); Consumer exceptionHandler = (e) -> onException(httpChannel, e); - SocketChannelContext context = new BytesChannelContext(httpChannel, selector, socketConfig, exceptionHandler, handler, - new InboundChannelBuffer(pageAllocator)); + SocketChannelContext context = new BytesChannelContext( + httpChannel, + selector, + socketConfig, + exceptionHandler, + handler, + new InboundChannelBuffer(pageAllocator) + ); httpChannel.setContext(context); return httpChannel; } @Override - public NioHttpServerChannel createServerChannel(NioSelector selector, ServerSocketChannel channel, - Config.ServerSocket socketConfig) { + public NioHttpServerChannel createServerChannel( + NioSelector selector, + ServerSocketChannel channel, + Config.ServerSocket socketConfig + ) { NioHttpServerChannel httpServerChannel = new NioHttpServerChannel(channel); Consumer exceptionHandler = (e) -> onServerException(httpServerChannel, e); Consumer acceptor = NioHttpServerTransport.this::acceptChannel; - ServerChannelContext context = new ServerChannelContext(httpServerChannel, this, selector, socketConfig, acceptor, - exceptionHandler); + ServerChannelContext context = new ServerChannelContext( + httpServerChannel, + this, + selector, + socketConfig, + acceptor, + exceptionHandler + ); httpServerChannel.setContext(context); return httpServerChannel; } diff --git a/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/PagedByteBuf.java b/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/PagedByteBuf.java index aad7ea0f34ee9..d01288a013176 100644 --- a/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/PagedByteBuf.java +++ b/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/PagedByteBuf.java @@ -13,6 +13,7 @@ import io.netty.buffer.Unpooled; import io.netty.buffer.UnpooledByteBufAllocator; import io.netty.buffer.UnpooledHeapByteBuf; + import org.elasticsearch.nio.Page; import java.nio.ByteBuffer; @@ -52,7 +53,6 @@ private static ByteBuf byteBufFromPage(Page page) { return newByteBuf.slice(offset, buffer.remaining()); } - @Override protected void deallocate() { try { diff --git a/plugins/transport-nio/src/main/java/org/elasticsearch/transport/nio/NioGroupFactory.java b/plugins/transport-nio/src/main/java/org/elasticsearch/transport/nio/NioGroupFactory.java index 500e840224b49..d730010e9cbfb 100644 --- a/plugins/transport-nio/src/main/java/org/elasticsearch/transport/nio/NioGroupFactory.java +++ b/plugins/transport-nio/src/main/java/org/elasticsearch/transport/nio/NioGroupFactory.java @@ -61,16 +61,22 @@ public synchronized NioGroup getHttpGroup() throws IOException { if (httpWorkerCount == 0) { return getGenericGroup(); } else { - return new NioSelectorGroup(daemonThreadFactory(this.settings, HttpServerTransport.HTTP_SERVER_WORKER_THREAD_NAME_PREFIX), - httpWorkerCount, (s) -> new EventHandler(this::onException, s)); + return new NioSelectorGroup( + daemonThreadFactory(this.settings, HttpServerTransport.HTTP_SERVER_WORKER_THREAD_NAME_PREFIX), + httpWorkerCount, + (s) -> new EventHandler(this::onException, s) + ); } } private NioGroup getGenericGroup() throws IOException { if (refCountedGroup == null) { ThreadFactory threadFactory = daemonThreadFactory(this.settings, TcpTransport.TRANSPORT_WORKER_THREAD_NAME_PREFIX); - NioSelectorGroup nioGroup = new NioSelectorGroup(threadFactory, NioTransportPlugin.NIO_WORKER_COUNT.get(settings), - (s) -> new EventHandler(this::onException, s)); + NioSelectorGroup nioGroup = new NioSelectorGroup( + threadFactory, + NioTransportPlugin.NIO_WORKER_COUNT.get(settings), + (s) -> new EventHandler(this::onException, s) + ); this.refCountedGroup = new RefCountedNioGroup(nioGroup); return new WrappedNioGroup(refCountedGroup); } else { @@ -80,8 +86,10 @@ private NioGroup getGenericGroup() throws IOException { } private void onException(Exception exception) { - logger.warn(new ParameterizedMessage("exception caught on transport layer [thread={}]", Thread.currentThread().getName()), - exception); + logger.warn( + new ParameterizedMessage("exception caught on transport layer [thread={}]", Thread.currentThread().getName()), + exception + ); } private static class RefCountedNioGroup extends AbstractRefCounted implements NioGroup { diff --git a/plugins/transport-nio/src/main/java/org/elasticsearch/transport/nio/NioTcpChannel.java b/plugins/transport-nio/src/main/java/org/elasticsearch/transport/nio/NioTcpChannel.java index facc3aecce237..dfc089f65d1ba 100644 --- a/plugins/transport-nio/src/main/java/org/elasticsearch/transport/nio/NioTcpChannel.java +++ b/plugins/transport-nio/src/main/java/org/elasticsearch/transport/nio/NioTcpChannel.java @@ -63,10 +63,13 @@ public void close() { @Override public String toString() { - return "TcpNioSocketChannel{" + - "localAddress=" + getLocalAddress() + - ", remoteAddress=" + getRemoteAddress() + - ", profile=" + profile + - '}'; + return "TcpNioSocketChannel{" + + "localAddress=" + + getLocalAddress() + + ", remoteAddress=" + + getRemoteAddress() + + ", profile=" + + profile + + '}'; } } diff --git a/plugins/transport-nio/src/main/java/org/elasticsearch/transport/nio/NioTcpServerChannel.java b/plugins/transport-nio/src/main/java/org/elasticsearch/transport/nio/NioTcpServerChannel.java index ae742ec5ad0c6..e8ce955619842 100644 --- a/plugins/transport-nio/src/main/java/org/elasticsearch/transport/nio/NioTcpServerChannel.java +++ b/plugins/transport-nio/src/main/java/org/elasticsearch/transport/nio/NioTcpServerChannel.java @@ -35,8 +35,6 @@ public void addCloseListener(ActionListener listener) { @Override public String toString() { - return "TcpNioServerSocketChannel{" + - "localAddress=" + getLocalAddress() + - '}'; + return "TcpNioServerSocketChannel{" + "localAddress=" + getLocalAddress() + '}'; } } diff --git a/plugins/transport-nio/src/main/java/org/elasticsearch/transport/nio/NioTransport.java b/plugins/transport-nio/src/main/java/org/elasticsearch/transport/nio/NioTransport.java index f849873a5f3db..b8f63928814ef 100644 --- a/plugins/transport-nio/src/main/java/org/elasticsearch/transport/nio/NioTransport.java +++ b/plugins/transport-nio/src/main/java/org/elasticsearch/transport/nio/NioTransport.java @@ -54,9 +54,16 @@ public class NioTransport extends TcpTransport { private volatile NioGroup nioGroup; private volatile Function clientChannelFactory; - protected NioTransport(Settings settings, Version version, ThreadPool threadPool, NetworkService networkService, - PageCacheRecycler pageCacheRecycler, NamedWriteableRegistry namedWriteableRegistry, - CircuitBreakerService circuitBreakerService, NioGroupFactory groupFactory) { + protected NioTransport( + Settings settings, + Version version, + ThreadPool threadPool, + NetworkService networkService, + PageCacheRecycler pageCacheRecycler, + NamedWriteableRegistry namedWriteableRegistry, + CircuitBreakerService circuitBreakerService, + NioGroupFactory groupFactory + ) { super(settings, version, threadPool, pageCacheRecycler, circuitBreakerService, namedWriteableRegistry, networkService); this.pageAllocator = new PageAllocator(pageCacheRecycler); this.groupFactory = groupFactory; @@ -133,9 +140,16 @@ protected Function clientChannelFactoryFunctio protected abstract static class TcpChannelFactory extends ChannelFactory { protected TcpChannelFactory(ProfileSettings profileSettings) { - super(profileSettings.tcpNoDelay, profileSettings.tcpKeepAlive, profileSettings.tcpKeepIdle, profileSettings.tcpKeepInterval, - profileSettings.tcpKeepCount, profileSettings.reuseAddress, Math.toIntExact(profileSettings.sendBufferSize.getBytes()), - Math.toIntExact(profileSettings.receiveBufferSize.getBytes())); + super( + profileSettings.tcpNoDelay, + profileSettings.tcpKeepAlive, + profileSettings.tcpKeepIdle, + profileSettings.tcpKeepInterval, + profileSettings.tcpKeepCount, + profileSettings.reuseAddress, + Math.toIntExact(profileSettings.sendBufferSize.getBytes()), + Math.toIntExact(profileSettings.receiveBufferSize.getBytes()) + ); } } @@ -155,15 +169,24 @@ public NioTcpChannel createChannel(NioSelector selector, SocketChannel channel, NioTcpChannel nioChannel = new NioTcpChannel(isClient == false, profileName, channel); Consumer exceptionHandler = (e) -> onException(nioChannel, e); TcpReadWriteHandler handler = new TcpReadWriteHandler(nioChannel, bytesRefRecycler, NioTransport.this); - BytesChannelContext context = new BytesChannelContext(nioChannel, selector, socketConfig, exceptionHandler, handler, - new InboundChannelBuffer(pageAllocator)); + BytesChannelContext context = new BytesChannelContext( + nioChannel, + selector, + socketConfig, + exceptionHandler, + handler, + new InboundChannelBuffer(pageAllocator) + ); nioChannel.setContext(context); return nioChannel; } @Override - public NioTcpServerChannel createServerChannel(NioSelector selector, ServerSocketChannel channel, - Config.ServerSocket socketConfig) { + public NioTcpServerChannel createServerChannel( + NioSelector selector, + ServerSocketChannel channel, + Config.ServerSocket socketConfig + ) { NioTcpServerChannel nioChannel = new NioTcpServerChannel(channel); Consumer exceptionHandler = (e) -> onServerException(nioChannel, e); Consumer acceptor = NioTransport.this::acceptChannel; diff --git a/plugins/transport-nio/src/main/java/org/elasticsearch/transport/nio/NioTransportPlugin.java b/plugins/transport-nio/src/main/java/org/elasticsearch/transport/nio/NioTransportPlugin.java index 4fe52f90376ef..4ff1dc60d9c65 100644 --- a/plugins/transport-nio/src/main/java/org/elasticsearch/transport/nio/NioTransportPlugin.java +++ b/plugins/transport-nio/src/main/java/org/elasticsearch/transport/nio/NioTransportPlugin.java @@ -20,7 +20,6 @@ import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.common.util.PageCacheRecycler; import org.elasticsearch.common.util.concurrent.EsExecutors; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.http.HttpServerTransport; import org.elasticsearch.http.nio.NioHttpServerTransport; import org.elasticsearch.indices.breaker.CircuitBreakerService; @@ -28,6 +27,7 @@ import org.elasticsearch.plugins.Plugin; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.Transport; +import org.elasticsearch.xcontent.NamedXContentRegistry; import java.util.Arrays; import java.util.Collections; @@ -44,43 +44,71 @@ public class NioTransportPlugin extends Plugin implements NetworkPlugin { private static final Logger logger = LogManager.getLogger(NioTransportPlugin.class); - public static final Setting NIO_WORKER_COUNT = - new Setting<>("transport.nio.worker_count", - (s) -> Integer.toString(EsExecutors.allocatedProcessors(s)), - (s) -> Setting.parseInt(s, 1, "transport.nio.worker_count"), Setting.Property.NodeScope); - public static final Setting NIO_HTTP_WORKER_COUNT = - intSetting("http.nio.worker_count", 0, 0, Setting.Property.NodeScope); + public static final Setting NIO_WORKER_COUNT = new Setting<>( + "transport.nio.worker_count", + (s) -> Integer.toString(EsExecutors.allocatedProcessors(s)), + (s) -> Setting.parseInt(s, 1, "transport.nio.worker_count"), + Setting.Property.NodeScope + ); + public static final Setting NIO_HTTP_WORKER_COUNT = intSetting("http.nio.worker_count", 0, 0, Setting.Property.NodeScope); private final SetOnce groupFactory = new SetOnce<>(); @Override public List> getSettings() { - return Arrays.asList( - NIO_HTTP_WORKER_COUNT, - NIO_WORKER_COUNT - ); + return Arrays.asList(NIO_HTTP_WORKER_COUNT, NIO_WORKER_COUNT); } @Override - public Map> getTransports(Settings settings, ThreadPool threadPool, PageCacheRecycler pageCacheRecycler, - CircuitBreakerService circuitBreakerService, - NamedWriteableRegistry namedWriteableRegistry, NetworkService networkService) { - return Collections.singletonMap(NIO_TRANSPORT_NAME, - () -> new NioTransport(settings, Version.CURRENT, threadPool, networkService, pageCacheRecycler, namedWriteableRegistry, - circuitBreakerService, getNioGroupFactory(settings))); + public Map> getTransports( + Settings settings, + ThreadPool threadPool, + PageCacheRecycler pageCacheRecycler, + CircuitBreakerService circuitBreakerService, + NamedWriteableRegistry namedWriteableRegistry, + NetworkService networkService + ) { + return Collections.singletonMap( + NIO_TRANSPORT_NAME, + () -> new NioTransport( + settings, + Version.CURRENT, + threadPool, + networkService, + pageCacheRecycler, + namedWriteableRegistry, + circuitBreakerService, + getNioGroupFactory(settings) + ) + ); } @Override - public Map> getHttpTransports(Settings settings, ThreadPool threadPool, BigArrays bigArrays, - PageCacheRecycler pageCacheRecycler, - CircuitBreakerService circuitBreakerService, - NamedXContentRegistry xContentRegistry, - NetworkService networkService, - HttpServerTransport.Dispatcher dispatcher, - ClusterSettings clusterSettings) { - return Collections.singletonMap(NIO_HTTP_TRANSPORT_NAME, - () -> new NioHttpServerTransport(settings, networkService, bigArrays, pageCacheRecycler, threadPool, xContentRegistry, - dispatcher, getNioGroupFactory(settings), clusterSettings)); + public Map> getHttpTransports( + Settings settings, + ThreadPool threadPool, + BigArrays bigArrays, + PageCacheRecycler pageCacheRecycler, + CircuitBreakerService circuitBreakerService, + NamedXContentRegistry xContentRegistry, + NetworkService networkService, + HttpServerTransport.Dispatcher dispatcher, + ClusterSettings clusterSettings + ) { + return Collections.singletonMap( + NIO_HTTP_TRANSPORT_NAME, + () -> new NioHttpServerTransport( + settings, + networkService, + bigArrays, + pageCacheRecycler, + threadPool, + xContentRegistry, + dispatcher, + getNioGroupFactory(settings), + clusterSettings + ) + ); } private synchronized NioGroupFactory getNioGroupFactory(Settings settings) { diff --git a/plugins/transport-nio/src/main/java/org/elasticsearch/transport/nio/PageAllocator.java b/plugins/transport-nio/src/main/java/org/elasticsearch/transport/nio/PageAllocator.java index fcdfe7bbf2da0..54cfc410f0450 100644 --- a/plugins/transport-nio/src/main/java/org/elasticsearch/transport/nio/PageAllocator.java +++ b/plugins/transport-nio/src/main/java/org/elasticsearch/transport/nio/PageAllocator.java @@ -27,7 +27,7 @@ public PageAllocator(PageCacheRecycler recycler) { @Override public Page apply(int length) { - if (length >= RECYCLE_LOWER_THRESHOLD && length <= PageCacheRecycler.BYTE_PAGE_SIZE){ + if (length >= RECYCLE_LOWER_THRESHOLD && length <= PageCacheRecycler.BYTE_PAGE_SIZE) { Recycler.V bytePage = recycler.bytePage(false); return new Page(ByteBuffer.wrap(bytePage.v(), 0, length), bytePage); } else { diff --git a/plugins/transport-nio/src/main/java/org/elasticsearch/transport/nio/TcpReadWriteHandler.java b/plugins/transport-nio/src/main/java/org/elasticsearch/transport/nio/TcpReadWriteHandler.java index 66493bdcea24a..8c688ba71a891 100644 --- a/plugins/transport-nio/src/main/java/org/elasticsearch/transport/nio/TcpReadWriteHandler.java +++ b/plugins/transport-nio/src/main/java/org/elasticsearch/transport/nio/TcpReadWriteHandler.java @@ -36,8 +36,15 @@ public TcpReadWriteHandler(NioTcpChannel channel, BytesRefRecycler recycler, Tcp final ThreadPool threadPool = transport.getThreadPool(); final Supplier breaker = transport.getInflightBreaker(); final Transport.RequestHandlers requestHandlers = transport.getRequestHandlers(); - this.pipeline = new InboundPipeline(transport.getVersion(), transport.getStatsTracker(), recycler, - threadPool::relativeTimeInMillis, breaker, requestHandlers::getHandler, transport::inboundMessage); + this.pipeline = new InboundPipeline( + transport.getVersion(), + transport.getStatsTracker(), + recycler, + threadPool::relativeTimeInMillis, + breaker, + requestHandlers::getHandler, + transport::inboundMessage + ); } @Override diff --git a/plugins/transport-nio/src/test/java/org/elasticsearch/http/nio/HttpReadWriteHandlerTests.java b/plugins/transport-nio/src/test/java/org/elasticsearch/http/nio/HttpReadWriteHandlerTests.java index 234d5e6b5c68b..67ed75f631e90 100644 --- a/plugins/transport-nio/src/test/java/org/elasticsearch/http/nio/HttpReadWriteHandlerTests.java +++ b/plugins/transport-nio/src/test/java/org/elasticsearch/http/nio/HttpReadWriteHandlerTests.java @@ -22,6 +22,7 @@ import io.netty.handler.codec.http.HttpResponseStatus; import io.netty.handler.codec.http.HttpUtil; import io.netty.handler.codec.http.HttpVersion; + import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeValue; @@ -241,8 +242,6 @@ private static HttpPipelinedResponse emptyGetResponse(int sequence) { return httpResponse; } - - private void prepareHandlerForResponse(HttpReadWriteHandler handler) throws IOException { HttpMethod method = randomBoolean() ? HttpMethod.GET : HttpMethod.HEAD; HttpVersion version = randomBoolean() ? HttpVersion.HTTP_1_0 : HttpVersion.HTTP_1_1; diff --git a/plugins/transport-nio/src/test/java/org/elasticsearch/http/nio/NettyAdaptorTests.java b/plugins/transport-nio/src/test/java/org/elasticsearch/http/nio/NettyAdaptorTests.java index 72fb96222e464..30faa84a0c4a0 100644 --- a/plugins/transport-nio/src/test/java/org/elasticsearch/http/nio/NettyAdaptorTests.java +++ b/plugins/transport-nio/src/test/java/org/elasticsearch/http/nio/NettyAdaptorTests.java @@ -15,6 +15,7 @@ import io.netty.channel.ChannelOutboundHandlerAdapter; import io.netty.channel.ChannelPromise; import io.netty.channel.SimpleChannelInboundHandler; + import org.elasticsearch.nio.FlushOperation; import org.elasticsearch.test.ESTestCase; @@ -33,7 +34,7 @@ public void testBasicRead() { message.putInt(i); } message.flip(); - ByteBuffer[] buffers = {message}; + ByteBuffer[] buffers = { message }; assertEquals(40, nettyAdaptor.read(buffers)); assertEquals("0123456789", handler.result); } @@ -46,7 +47,7 @@ public void testBasicReadWithExcessData() { message.putInt(i); } message.flip(); - ByteBuffer[] buffers = {message}; + ByteBuffer[] buffers = { message }; assertEquals(40, nettyAdaptor.read(buffers)); assertEquals("0123456789", handler.result); } @@ -58,20 +59,22 @@ public void testUncaughtReadExceptionsBubbleUp() { message.putInt(i); } message.flip(); - ByteBuffer[] buffers = {message}; + ByteBuffer[] buffers = { message }; expectThrows(IllegalStateException.class, () -> nettyAdaptor.read(buffers)); } public void testWriteInsidePipelineIsCaptured() { TenIntsToStringsHandler tenIntsToStringsHandler = new TenIntsToStringsHandler(); PromiseCheckerHandler promiseCheckerHandler = new PromiseCheckerHandler(); - NettyAdaptor nettyAdaptor = new NettyAdaptor(new CapitalizeWriteHandler(), + NettyAdaptor nettyAdaptor = new NettyAdaptor( + new CapitalizeWriteHandler(), promiseCheckerHandler, new WriteInMiddleHandler(), - tenIntsToStringsHandler); + tenIntsToStringsHandler + ); byte[] bytes = "SHOULD_WRITE".getBytes(StandardCharsets.UTF_8); ByteBuffer message = ByteBuffer.wrap(bytes); - ByteBuffer[] buffers = {message}; + ByteBuffer[] buffers = { message }; assertNull(nettyAdaptor.pollOutboundOperation()); nettyAdaptor.read(buffers); assertFalse(tenIntsToStringsHandler.wasCalled); @@ -88,7 +91,7 @@ public void testCloseListener() { CloseChannelHandler handler = new CloseChannelHandler(); NettyAdaptor nettyAdaptor = new NettyAdaptor(handler); byte[] bytes = "SHOULD_CLOSE".getBytes(StandardCharsets.UTF_8); - ByteBuffer[] buffers = {ByteBuffer.wrap(bytes)}; + ByteBuffer[] buffers = { ByteBuffer.wrap(bytes) }; nettyAdaptor.addCloseListener((v, e) -> listenerCalled.set(true)); assertFalse(listenerCalled.get()); nettyAdaptor.read(buffers); diff --git a/plugins/transport-nio/src/test/java/org/elasticsearch/http/nio/NioHttpClient.java b/plugins/transport-nio/src/test/java/org/elasticsearch/http/nio/NioHttpClient.java index 1e57eb65d004e..5e7be463fc0be 100644 --- a/plugins/transport-nio/src/test/java/org/elasticsearch/http/nio/NioHttpClient.java +++ b/plugins/transport-nio/src/test/java/org/elasticsearch/http/nio/NioHttpClient.java @@ -21,6 +21,7 @@ import io.netty.handler.codec.http.HttpRequestEncoder; import io.netty.handler.codec.http.HttpResponse; import io.netty.handler.codec.http.HttpResponseDecoder; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.action.ActionListener; @@ -83,8 +84,11 @@ static Collection returnOpaqueIds(Collection responses NioHttpClient() { try { - nioGroup = new NioSelectorGroup(daemonThreadFactory(Settings.EMPTY, "nio-http-client"), 1, - (s) -> new EventHandler(this::onException, s)); + nioGroup = new NioSelectorGroup( + daemonThreadFactory(Settings.EMPTY, "nio-http-client"), + 1, + (s) -> new EventHandler(this::onException, s) + ); } catch (IOException e) { throw new UncheckedIOException(e); } @@ -108,8 +112,10 @@ public final FullHttpResponse send(InetSocketAddress remoteAddress, FullHttpRequ } public final NioSocketChannel connect(InetSocketAddress remoteAddress) { - ChannelFactory factory = new ClientChannelFactory(new CountDownLatch(0), new - ArrayList<>()); + ChannelFactory factory = new ClientChannelFactory( + new CountDownLatch(0), + new ArrayList<>() + ); try { NioSocketChannel nioSocketChannel = nioGroup.openChannel(remoteAddress, factory); PlainActionFuture connectFuture = PlainActionFuture.newFuture(); @@ -140,8 +146,7 @@ private synchronized Collection sendRequests(InetSocketAddress connectFuture.actionGet(); for (HttpRequest request : requests) { - nioSocketChannel.getContext().sendMessage(request, (v, e) -> { - }); + nioSocketChannel.getContext().sendMessage(request, (v, e) -> {}); } if (latch.await(30L, TimeUnit.SECONDS) == false) { fail("Failed to get all expected responses."); @@ -169,14 +174,16 @@ private class ClientChannelFactory extends ChannelFactory content; private ClientChannelFactory(CountDownLatch latch, Collection content) { - super(NetworkService.TCP_NO_DELAY.get(Settings.EMPTY), + super( + NetworkService.TCP_NO_DELAY.get(Settings.EMPTY), NetworkService.TCP_KEEP_ALIVE.get(Settings.EMPTY), NetworkService.TCP_KEEP_IDLE.get(Settings.EMPTY), NetworkService.TCP_KEEP_INTERVAL.get(Settings.EMPTY), NetworkService.TCP_KEEP_COUNT.get(Settings.EMPTY), NetworkService.TCP_REUSE_ADDRESS.get(Settings.EMPTY), Math.toIntExact(NetworkService.TCP_SEND_BUFFER_SIZE.get(Settings.EMPTY).getBytes()), - Math.toIntExact(NetworkService.TCP_RECEIVE_BUFFER_SIZE.get(Settings.EMPTY).getBytes())); + Math.toIntExact(NetworkService.TCP_RECEIVE_BUFFER_SIZE.get(Settings.EMPTY).getBytes()) + ); this.latch = latch; this.content = content; } @@ -190,15 +197,24 @@ public NioSocketChannel createChannel(NioSelector selector, java.nio.channels.So onException(e); nioSocketChannel.close(); }; - SocketChannelContext context = new BytesChannelContext(nioSocketChannel, selector, socketConfig, exceptionHandler, handler, - InboundChannelBuffer.allocatingInstance()); + SocketChannelContext context = new BytesChannelContext( + nioSocketChannel, + selector, + socketConfig, + exceptionHandler, + handler, + InboundChannelBuffer.allocatingInstance() + ); nioSocketChannel.setContext(context); return nioSocketChannel; } @Override - public NioServerSocketChannel createServerChannel(NioSelector selector, ServerSocketChannel channel, - Config.ServerSocket socketConfig) { + public NioServerSocketChannel createServerChannel( + NioSelector selector, + ServerSocketChannel channel, + Config.ServerSocket socketConfig + ) { throw new UnsupportedOperationException("Cannot create server channel"); } } @@ -296,11 +312,13 @@ public void close() throws IOException { private void handleResponse(Object message) { final FullHttpResponse response = (FullHttpResponse) message; - DefaultFullHttpResponse newResponse = new DefaultFullHttpResponse(response.protocolVersion(), + DefaultFullHttpResponse newResponse = new DefaultFullHttpResponse( + response.protocolVersion(), response.status(), Unpooled.copiedBuffer(response.content()), response.headers().copy(), - response.trailingHeaders().copy()); + response.trailingHeaders().copy() + ); response.release(); content.add(newResponse); latch.countDown(); diff --git a/plugins/transport-nio/src/test/java/org/elasticsearch/http/nio/NioHttpPipeliningHandlerTests.java b/plugins/transport-nio/src/test/java/org/elasticsearch/http/nio/NioHttpPipeliningHandlerTests.java index 03532013dc2e2..6be48df2d4605 100644 --- a/plugins/transport-nio/src/test/java/org/elasticsearch/http/nio/NioHttpPipeliningHandlerTests.java +++ b/plugins/transport-nio/src/test/java/org/elasticsearch/http/nio/NioHttpPipeliningHandlerTests.java @@ -17,6 +17,7 @@ import io.netty.handler.codec.http.FullHttpResponse; import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.QueryStringDecoder; + import org.elasticsearch.common.Randomness; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; @@ -76,8 +77,10 @@ private void shutdownExecutorService() throws InterruptedException { public void testThatPipeliningWorksWithFastSerializedRequests() throws InterruptedException { final int numberOfRequests = randomIntBetween(2, 128); - final EmbeddedChannel embeddedChannel = new EmbeddedChannel(new NioHttpPipeliningHandler(logger, numberOfRequests), - new WorkEmulatorHandler()); + final EmbeddedChannel embeddedChannel = new EmbeddedChannel( + new NioHttpPipeliningHandler(logger, numberOfRequests), + new WorkEmulatorHandler() + ); for (int i = 0; i < numberOfRequests; i++) { embeddedChannel.writeInbound(createHttpRequest("/" + String.valueOf(i))); @@ -103,8 +106,10 @@ public void testThatPipeliningWorksWithFastSerializedRequests() throws Interrupt public void testThatPipeliningWorksWhenSlowRequestsInDifferentOrder() throws InterruptedException { final int numberOfRequests = randomIntBetween(2, 128); - final EmbeddedChannel embeddedChannel = new EmbeddedChannel(new NioHttpPipeliningHandler(logger, numberOfRequests), - new WorkEmulatorHandler()); + final EmbeddedChannel embeddedChannel = new EmbeddedChannel( + new NioHttpPipeliningHandler(logger, numberOfRequests), + new WorkEmulatorHandler() + ); for (int i = 0; i < numberOfRequests; i++) { embeddedChannel.writeInbound(createHttpRequest("/" + String.valueOf(i))); @@ -133,8 +138,10 @@ public void testThatPipeliningWorksWhenSlowRequestsInDifferentOrder() throws Int public void testThatPipeliningClosesConnectionWithTooManyEvents() throws InterruptedException { final int numberOfRequests = randomIntBetween(2, 128); - final EmbeddedChannel embeddedChannel = new EmbeddedChannel(new NioHttpPipeliningHandler(logger, numberOfRequests), - new WorkEmulatorHandler()); + final EmbeddedChannel embeddedChannel = new EmbeddedChannel( + new NioHttpPipeliningHandler(logger, numberOfRequests), + new WorkEmulatorHandler() + ); for (int i = 0; i < 1 + numberOfRequests + 1; i++) { embeddedChannel.writeInbound(createHttpRequest("/" + Integer.toString(i))); @@ -161,8 +168,7 @@ public void testThatPipeliningClosesConnectionWithTooManyEvents() throws Interru public void testPipeliningRequestsAreReleased() { final int numberOfRequests = 10; - final EmbeddedChannel embeddedChannel = - new EmbeddedChannel(new NioHttpPipeliningHandler(logger, numberOfRequests + 1)); + final EmbeddedChannel embeddedChannel = new EmbeddedChannel(new NioHttpPipeliningHandler(logger, numberOfRequests + 1)); for (int i = 0; i < numberOfRequests; i++) { embeddedChannel.writeInbound(createHttpRequest("/" + i)); diff --git a/plugins/transport-nio/src/test/java/org/elasticsearch/http/nio/NioHttpServerTransportTests.java b/plugins/transport-nio/src/test/java/org/elasticsearch/http/nio/NioHttpServerTransportTests.java index a7769272582ab..74ff306d8d964 100644 --- a/plugins/transport-nio/src/test/java/org/elasticsearch/http/nio/NioHttpServerTransportTests.java +++ b/plugins/transport-nio/src/test/java/org/elasticsearch/http/nio/NioHttpServerTransportTests.java @@ -20,6 +20,7 @@ import io.netty.handler.codec.http.HttpResponseStatus; import io.netty.handler.codec.http.HttpUtil; import io.netty.handler.codec.http.HttpVersion; + import org.apache.logging.log4j.message.ParameterizedMessage; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.bytes.BytesArray; @@ -29,10 +30,10 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.TransportAddress; import org.elasticsearch.common.unit.ByteSizeValue; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.MockBigArrays; import org.elasticsearch.common.util.MockPageCacheRecycler; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.http.AbstractHttpServerTransportTestCase; import org.elasticsearch.http.BindHttpException; import org.elasticsearch.http.CorsHandler; @@ -117,8 +118,7 @@ public void testExpectContinueHeaderContentLengthTooLong() throws InterruptedExc final int maxContentLength = randomIntBetween(1, 104857600); final Settings settings = createBuilderWithPort().put(key, maxContentLength + "b").build(); final int contentLength = randomIntBetween(maxContentLength + 1, Integer.MAX_VALUE); - runExpectHeaderTest( - settings, HttpHeaderValues.CONTINUE.toString(), contentLength, HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE); + runExpectHeaderTest(settings, HttpHeaderValues.CONTINUE.toString(), contentLength, HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE); } /** @@ -134,7 +134,8 @@ private void runExpectHeaderTest( final Settings settings, final String expectation, final int contentLength, - final HttpResponseStatus expectedStatus) throws InterruptedException { + final HttpResponseStatus expectedStatus + ) throws InterruptedException { final HttpServerTransport.Dispatcher dispatcher = new HttpServerTransport.Dispatcher() { @Override public void dispatchRequest(RestRequest request, RestChannel channel, ThreadContext threadContext) { @@ -143,13 +144,26 @@ public void dispatchRequest(RestRequest request, RestChannel channel, ThreadCont @Override public void dispatchBadRequest(RestChannel channel, ThreadContext threadContext, Throwable cause) { - logger.error(new ParameterizedMessage("--> Unexpected bad request [{}]", - FakeRestRequest.requestToString(channel.request())), cause); + logger.error( + new ParameterizedMessage("--> Unexpected bad request [{}]", FakeRestRequest.requestToString(channel.request())), + cause + ); throw new AssertionError(); } }; - try (NioHttpServerTransport transport = new NioHttpServerTransport(settings, networkService, bigArrays, pageRecycler, threadPool, - xContentRegistry(), dispatcher, new NioGroupFactory(settings, logger), randomClusterSettings())) { + try ( + NioHttpServerTransport transport = new NioHttpServerTransport( + settings, + networkService, + bigArrays, + pageRecycler, + threadPool, + xContentRegistry(), + dispatcher, + new NioGroupFactory(settings, logger), + randomClusterSettings() + ) + ) { transport.start(); final TransportAddress remoteAddress = randomFrom(transport.boundAddress().boundAddresses()); try (NioHttpClient client = new NioHttpClient()) { @@ -161,13 +175,18 @@ public void dispatchBadRequest(RestChannel channel, ThreadContext threadContext, try { assertThat(response.status(), equalTo(expectedStatus)); if (expectedStatus.equals(HttpResponseStatus.CONTINUE)) { - final FullHttpRequest continuationRequest = - new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/", Unpooled.EMPTY_BUFFER); + final FullHttpRequest continuationRequest = new DefaultFullHttpRequest( + HttpVersion.HTTP_1_1, + HttpMethod.POST, + "/", + Unpooled.EMPTY_BUFFER + ); final FullHttpResponse continuationResponse = client.send(remoteAddress.address(), continuationRequest); try { assertThat(continuationResponse.status(), is(HttpResponseStatus.OK)); assertThat( - new String(ByteBufUtil.getBytes(continuationResponse.content()), StandardCharsets.UTF_8), is("done") + new String(ByteBufUtil.getBytes(continuationResponse.content()), StandardCharsets.UTF_8), + is("done") ); } finally { continuationResponse.release(); @@ -182,23 +201,40 @@ public void dispatchBadRequest(RestChannel channel, ThreadContext threadContext, public void testBindUnavailableAddress() { final Settings initialSettings = createSettings(); - try (NioHttpServerTransport transport = new NioHttpServerTransport(initialSettings, networkService, bigArrays, pageRecycler, - threadPool, xContentRegistry(), new NullDispatcher(), new NioGroupFactory(Settings.EMPTY, logger), - randomClusterSettings())) { + try ( + NioHttpServerTransport transport = new NioHttpServerTransport( + initialSettings, + networkService, + bigArrays, + pageRecycler, + threadPool, + xContentRegistry(), + new NullDispatcher(), + new NioGroupFactory(Settings.EMPTY, logger), + randomClusterSettings() + ) + ) { transport.start(); TransportAddress remoteAddress = randomFrom(transport.boundAddress().boundAddresses()); Settings settings = Settings.builder() .put("http.port", remoteAddress.getPort()) .put("network.host", remoteAddress.getAddress()) .build(); - try (NioHttpServerTransport otherTransport = new NioHttpServerTransport(settings, networkService, bigArrays, pageRecycler, - threadPool, xContentRegistry(), new NullDispatcher(), new NioGroupFactory(Settings.EMPTY, logger), - randomClusterSettings())) { + try ( + NioHttpServerTransport otherTransport = new NioHttpServerTransport( + settings, + networkService, + bigArrays, + pageRecycler, + threadPool, + xContentRegistry(), + new NullDispatcher(), + new NioGroupFactory(Settings.EMPTY, logger), + randomClusterSettings() + ) + ) { BindHttpException bindHttpException = expectThrows(BindHttpException.class, () -> otherTransport.start()); - assertEquals( - "Failed to bind to " + NetworkAddress.format(remoteAddress.address()), - bindHttpException.getMessage() - ); + assertEquals("Failed to bind to " + NetworkAddress.format(remoteAddress.address()), bindHttpException.getMessage()); } } } @@ -213,24 +249,33 @@ public void dispatchRequest(final RestRequest request, final RestChannel channel } @Override - public void dispatchBadRequest(final RestChannel channel, - final ThreadContext threadContext, - final Throwable cause) { - logger.error(new ParameterizedMessage("--> Unexpected bad request [{}]", - FakeRestRequest.requestToString(channel.request())), cause); + public void dispatchBadRequest(final RestChannel channel, final ThreadContext threadContext, final Throwable cause) { + logger.error( + new ParameterizedMessage("--> Unexpected bad request [{}]", FakeRestRequest.requestToString(channel.request())), + cause + ); throw new AssertionError(); } }; - final Settings settings = createBuilderWithPort() - .put(SETTING_CORS_ENABLED.getKey(), true) + final Settings settings = createBuilderWithPort().put(SETTING_CORS_ENABLED.getKey(), true) .put(SETTING_CORS_ALLOW_ORIGIN.getKey(), "elastic.co") .build(); - try (NioHttpServerTransport transport = new NioHttpServerTransport(settings, networkService, bigArrays, pageRecycler, - threadPool, xContentRegistry(), dispatcher, new NioGroupFactory(settings, logger), - randomClusterSettings())) { + try ( + NioHttpServerTransport transport = new NioHttpServerTransport( + settings, + networkService, + bigArrays, + pageRecycler, + threadPool, + xContentRegistry(), + dispatcher, + new NioGroupFactory(settings, logger), + randomClusterSettings() + ) + ) { transport.start(); final TransportAddress remoteAddress = randomFrom(transport.boundAddress().boundAddresses()); @@ -283,16 +328,28 @@ public void dispatchRequest(final RestRequest request, final RestChannel channel @Override public void dispatchBadRequest(final RestChannel channel, final ThreadContext threadContext, final Throwable cause) { - logger.error(new ParameterizedMessage("--> Unexpected bad request [{}]", - FakeRestRequest.requestToString(channel.request())), cause); + logger.error( + new ParameterizedMessage("--> Unexpected bad request [{}]", FakeRestRequest.requestToString(channel.request())), + cause + ); throw new AssertionError(); } }; - try (NioHttpServerTransport transport = new NioHttpServerTransport( - Settings.EMPTY, networkService, bigArrays, pageRecycler, threadPool, xContentRegistry(), dispatcher, - new NioGroupFactory(Settings.EMPTY, logger), randomClusterSettings())) { + try ( + NioHttpServerTransport transport = new NioHttpServerTransport( + Settings.EMPTY, + networkService, + bigArrays, + pageRecycler, + threadPool, + xContentRegistry(), + dispatcher, + new NioGroupFactory(Settings.EMPTY, logger), + randomClusterSettings() + ) + ) { transport.start(); final TransportAddress remoteAddress = randomFrom(transport.boundAddress().boundAddresses()); @@ -346,9 +403,19 @@ public void dispatchBadRequest(final RestChannel channel, final ThreadContext th settings = createBuilderWithPort().put(httpMaxInitialLineLengthSetting.getKey(), maxInitialLineLength + "b").build(); } - try (NioHttpServerTransport transport = new NioHttpServerTransport(settings, networkService, bigArrays, pageRecycler, - threadPool, xContentRegistry(), dispatcher, new NioGroupFactory(settings, logger), - randomClusterSettings())) { + try ( + NioHttpServerTransport transport = new NioHttpServerTransport( + settings, + networkService, + bigArrays, + pageRecycler, + threadPool, + xContentRegistry(), + dispatcher, + new NioGroupFactory(settings, logger), + randomClusterSettings() + ) + ) { transport.start(); final TransportAddress remoteAddress = randomFrom(transport.boundAddress().boundAddresses()); @@ -361,7 +428,8 @@ threadPool, xContentRegistry(), dispatcher, new NioGroupFactory(settings, logger assertThat(response.status(), equalTo(HttpResponseStatus.BAD_REQUEST)); assertThat( new String(response.content().array(), Charset.forName("UTF-8")), - containsString("you sent a bad request and you should feel bad")); + containsString("you sent a bad request and you should feel bad") + ); } finally { response.release(); } @@ -382,23 +450,34 @@ public void dispatchRequest(final RestRequest request, final RestChannel channel } @Override - public void dispatchBadRequest(final RestChannel channel, - final ThreadContext threadContext, - final Throwable cause) { - logger.error(new ParameterizedMessage("--> Unexpected bad request [{}]", - FakeRestRequest.requestToString(channel.request())), cause); + public void dispatchBadRequest(final RestChannel channel, final ThreadContext threadContext, final Throwable cause) { + logger.error( + new ParameterizedMessage("--> Unexpected bad request [{}]", FakeRestRequest.requestToString(channel.request())), + cause + ); throw new AssertionError("Should not have received a dispatched request"); } }; - Settings settings = createBuilderWithPort() - .put(HttpTransportSettings.SETTING_HTTP_READ_TIMEOUT.getKey(), new TimeValue(randomIntBetween(100, 300))) - .build(); - - try (NioHttpServerTransport transport = new NioHttpServerTransport(settings, networkService, bigArrays, pageRecycler, - threadPool, xContentRegistry(), dispatcher, new NioGroupFactory(settings, logger), - randomClusterSettings())) { + Settings settings = createBuilderWithPort().put( + HttpTransportSettings.SETTING_HTTP_READ_TIMEOUT.getKey(), + new TimeValue(randomIntBetween(100, 300)) + ).build(); + + try ( + NioHttpServerTransport transport = new NioHttpServerTransport( + settings, + networkService, + bigArrays, + pageRecycler, + threadPool, + xContentRegistry(), + dispatcher, + new NioGroupFactory(settings, logger), + randomClusterSettings() + ) + ) { transport.start(); final TransportAddress remoteAddress = randomFrom(transport.boundAddress().boundAddresses()); diff --git a/plugins/transport-nio/src/test/java/org/elasticsearch/http/nio/PagedByteBufTests.java b/plugins/transport-nio/src/test/java/org/elasticsearch/http/nio/PagedByteBufTests.java index fab0c84d496ae..c7982152bd8e0 100644 --- a/plugins/transport-nio/src/test/java/org/elasticsearch/http/nio/PagedByteBufTests.java +++ b/plugins/transport-nio/src/test/java/org/elasticsearch/http/nio/PagedByteBufTests.java @@ -9,6 +9,7 @@ package org.elasticsearch.http.nio; import io.netty.buffer.ByteBuf; + import org.elasticsearch.nio.Page; import org.elasticsearch.test.ESTestCase; diff --git a/plugins/transport-nio/src/test/java/org/elasticsearch/transport/nio/NioGroupFactoryTests.java b/plugins/transport-nio/src/test/java/org/elasticsearch/transport/nio/NioGroupFactoryTests.java index 03ac561a13a16..ecc2f592d1045 100644 --- a/plugins/transport-nio/src/test/java/org/elasticsearch/transport/nio/NioGroupFactoryTests.java +++ b/plugins/transport-nio/src/test/java/org/elasticsearch/transport/nio/NioGroupFactoryTests.java @@ -56,8 +56,11 @@ public NioSocketChannel createChannel(NioSelector selector, SocketChannel channe } @Override - public NioServerSocketChannel createServerChannel(NioSelector selector, ServerSocketChannel channel, - Config.ServerSocket socketConfig) { + public NioServerSocketChannel createServerChannel( + NioSelector selector, + ServerSocketChannel channel, + Config.ServerSocket socketConfig + ) { NioServerSocketChannel nioChannel = new NioServerSocketChannel(channel); Consumer exceptionHandler = (e) -> {}; Consumer acceptor = (c) -> {}; diff --git a/plugins/transport-nio/src/test/java/org/elasticsearch/transport/nio/SimpleNioTransportTests.java b/plugins/transport-nio/src/test/java/org/elasticsearch/transport/nio/SimpleNioTransportTests.java index 5de583d120f93..6c15e36f5047d 100644 --- a/plugins/transport-nio/src/test/java/org/elasticsearch/transport/nio/SimpleNioTransportTests.java +++ b/plugins/transport-nio/src/test/java/org/elasticsearch/transport/nio/SimpleNioTransportTests.java @@ -10,7 +10,6 @@ import org.elasticsearch.Version; import org.elasticsearch.action.ActionListener; -import org.elasticsearch.jdk.JavaVersion; import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.network.NetworkService; @@ -21,6 +20,7 @@ import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.core.internal.net.NetUtils; import org.elasticsearch.indices.breaker.NoneCircuitBreakerService; +import org.elasticsearch.jdk.JavaVersion; import org.elasticsearch.test.transport.MockTransportService; import org.elasticsearch.test.transport.StubbableTransport; import org.elasticsearch.transport.AbstractSimpleTransportTestCase; @@ -50,12 +50,24 @@ public class SimpleNioTransportTests extends AbstractSimpleTransportTestCase { protected Transport build(Settings settings, final Version version, ClusterSettings clusterSettings, boolean doHandshake) { NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(Collections.emptyList()); NetworkService networkService = new NetworkService(Collections.emptyList()); - return new NioTransport(settings, version, threadPool, networkService, new MockPageCacheRecycler(settings), - namedWriteableRegistry, new NoneCircuitBreakerService(), new NioGroupFactory(settings, logger)) { + return new NioTransport( + settings, + version, + threadPool, + networkService, + new MockPageCacheRecycler(settings), + namedWriteableRegistry, + new NoneCircuitBreakerService(), + new NioGroupFactory(settings, logger) + ) { @Override - public void executeHandshake(DiscoveryNode node, TcpChannel channel, ConnectionProfile profile, - ActionListener listener) { + public void executeHandshake( + DiscoveryNode node, + TcpChannel channel, + ConnectionProfile profile, + ActionListener listener + ) { if (doHandshake) { super.executeHandshake(node, channel, profile, listener); } else { @@ -67,8 +79,16 @@ public void executeHandshake(DiscoveryNode node, TcpChannel channel, ConnectionP public void testConnectException() throws UnknownHostException { try { - connectToNode(serviceA, new DiscoveryNode("C", new TransportAddress(InetAddress.getByName("localhost"), 9876), - emptyMap(), emptySet(),Version.CURRENT)); + connectToNode( + serviceA, + new DiscoveryNode( + "C", + new TransportAddress(InetAddress.getByName("localhost"), 9876), + emptyMap(), + emptySet(), + Version.CURRENT + ) + ); fail("Expected ConnectTransportException"); } catch (ConnectTransportException e) { assertThat(e.getMessage(), containsString("connect_exception")); @@ -79,12 +99,15 @@ public void testConnectException() throws UnknownHostException { } public void testDefaultKeepAliveSettings() throws IOException { - assumeTrue("setting default keepalive options not supported on this platform", - (IOUtils.LINUX || IOUtils.MAC_OS_X) && - JavaVersion.current().compareTo(JavaVersion.parse("11")) >= 0); - try (MockTransportService serviceC = buildService("TS_C", Version.CURRENT, Settings.EMPTY); - MockTransportService serviceD = buildService("TS_D", Version.CURRENT, Settings.EMPTY); - Transport.Connection connection = openConnection(serviceC, serviceD.getLocalDiscoNode(), TestProfiles.LIGHT_PROFILE)) { + assumeTrue( + "setting default keepalive options not supported on this platform", + (IOUtils.LINUX || IOUtils.MAC_OS_X) && JavaVersion.current().compareTo(JavaVersion.parse("11")) >= 0 + ); + try ( + MockTransportService serviceC = buildService("TS_C", Version.CURRENT, Settings.EMPTY); + MockTransportService serviceD = buildService("TS_D", Version.CURRENT, Settings.EMPTY); + Transport.Connection connection = openConnection(serviceC, serviceD.getLocalDiscoNode(), TestProfiles.LIGHT_PROFILE) + ) { assertThat(connection, instanceOf(StubbableTransport.WrappedConnection.class)); Transport.Connection conn = ((StubbableTransport.WrappedConnection) connection).getConnection(); diff --git a/qa/ccs-rolling-upgrade-remote-cluster/src/test/java/org/elasticsearch/upgrades/SearchStatesIT.java b/qa/ccs-rolling-upgrade-remote-cluster/src/test/java/org/elasticsearch/upgrades/SearchStatesIT.java index c748bee16c0e2..c8dccaff2ab6f 100644 --- a/qa/ccs-rolling-upgrade-remote-cluster/src/test/java/org/elasticsearch/upgrades/SearchStatesIT.java +++ b/qa/ccs-rolling-upgrade-remote-cluster/src/test/java/org/elasticsearch/upgrades/SearchStatesIT.java @@ -44,13 +44,13 @@ import org.elasticsearch.client.indices.CreateIndexRequest; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.test.hamcrest.ElasticsearchAssertions; +import org.elasticsearch.test.rest.ESRestTestCase; +import org.elasticsearch.test.rest.yaml.ObjectPath; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.hamcrest.ElasticsearchAssertions; -import org.elasticsearch.test.rest.ESRestTestCase; -import org.elasticsearch.test.rest.yaml.ObjectPath; import java.io.IOException; import java.io.UncheckedIOException; @@ -94,14 +94,24 @@ static class Node { @Override public String toString() { - return "Node{" + - "id='" + id + '\'' + - ", name='" + name + '\'' + - ", version=" + version + - ", transportAddress='" + transportAddress + '\'' + - ", httpAddress='" + httpAddress + '\'' + - ", attributes=" + attributes + - '}'; + return "Node{" + + "id='" + + id + + '\'' + + ", name='" + + name + + '\'' + + ", version=" + + version + + ", transportAddress='" + + transportAddress + + '\'' + + ", httpAddress='" + + httpAddress + + '\'' + + ", attributes=" + + attributes + + '}'; } } @@ -222,9 +232,13 @@ void verifySearch(String localIndex, int localNumDocs, String remoteIndex, int r int size = between(1, 100); request.setJsonEntity("{\"sort\": \"f\", \"size\": " + size + "}"); Response response = localClient.getLowLevelClient().performRequest(request); - try (XContentParser parser = JsonXContent.jsonXContent.createParser( - NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, - response.getEntity().getContent())) { + try ( + XContentParser parser = JsonXContent.jsonXContent.createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + response.getEntity().getContent() + ) + ) { SearchResponse searchResponse = SearchResponse.fromXContent(parser); ElasticsearchAssertions.assertNoFailures(searchResponse); ElasticsearchAssertions.assertHitCount(searchResponse, expectedDocs); @@ -237,16 +251,23 @@ void verifySearch(String localIndex, int localNumDocs, String remoteIndex, int r public void testBWCSearchStates() throws Exception { String localIndex = "test_bwc_search_states_index"; String remoteIndex = "test_bwc_search_states_remote_index"; - try (RestHighLevelClient localClient = newLocalClient(); - RestHighLevelClient remoteClient = newRemoteClient()) { - localClient.indices().create(new CreateIndexRequest(localIndex) - .settings(Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, between(1, 5))), - RequestOptions.DEFAULT); + try (RestHighLevelClient localClient = newLocalClient(); RestHighLevelClient remoteClient = newRemoteClient()) { + localClient.indices() + .create( + new CreateIndexRequest(localIndex).settings( + Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, between(1, 5)) + ), + RequestOptions.DEFAULT + ); int localNumDocs = indexDocs(localClient, localIndex, between(10, 100)); - remoteClient.indices().create(new CreateIndexRequest(remoteIndex) - .settings(Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, between(1, 5))), - RequestOptions.DEFAULT); + remoteClient.indices() + .create( + new CreateIndexRequest(remoteIndex).settings( + Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, between(1, 5)) + ), + RequestOptions.DEFAULT + ); int remoteNumDocs = indexDocs(remoteClient, remoteIndex, between(10, 100)); configureRemoteClusters(getNodes(remoteClient.getLowLevelClient())); @@ -262,16 +283,23 @@ public void testBWCSearchStates() throws Exception { public void testCanMatch() throws Exception { String localIndex = "test_can_match_local_index"; String remoteIndex = "test_can_match_remote_index"; - try (RestHighLevelClient localClient = newLocalClient(); - RestHighLevelClient remoteClient = newRemoteClient()) { - localClient.indices().create(new CreateIndexRequest(localIndex) - .settings(Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, between(5, 20))), - RequestOptions.DEFAULT); + try (RestHighLevelClient localClient = newLocalClient(); RestHighLevelClient remoteClient = newRemoteClient()) { + localClient.indices() + .create( + new CreateIndexRequest(localIndex).settings( + Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, between(5, 20)) + ), + RequestOptions.DEFAULT + ); int localNumDocs = indexDocs(localClient, localIndex, between(10, 100)); - remoteClient.indices().create(new CreateIndexRequest(remoteIndex) - .settings(Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, between(5, 20))), - RequestOptions.DEFAULT); + remoteClient.indices() + .create( + new CreateIndexRequest(remoteIndex).settings( + Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, between(5, 20)) + ), + RequestOptions.DEFAULT + ); int remoteNumDocs = indexDocs(remoteClient, remoteIndex, between(10, 100)); configureRemoteClusters(getNodes(remoteClient.getLowLevelClient())); diff --git a/qa/ccs-unavailable-clusters/src/test/java/org/elasticsearch/search/CrossClusterSearchUnavailableClusterIT.java b/qa/ccs-unavailable-clusters/src/test/java/org/elasticsearch/search/CrossClusterSearchUnavailableClusterIT.java index 33808a7edb6d0..f689cd2e10367 100644 --- a/qa/ccs-unavailable-clusters/src/test/java/org/elasticsearch/search/CrossClusterSearchUnavailableClusterIT.java +++ b/qa/ccs-unavailable-clusters/src/test/java/org/elasticsearch/search/CrossClusterSearchUnavailableClusterIT.java @@ -41,14 +41,14 @@ import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.search.aggregations.InternalAggregations; import org.elasticsearch.search.internal.InternalSearchResponse; import org.elasticsearch.test.rest.ESRestTestCase; import org.elasticsearch.test.transport.MockTransportService; import org.elasticsearch.threadpool.TestThreadPool; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.json.JsonXContent; import org.junit.AfterClass; import org.junit.Before; @@ -90,37 +90,65 @@ public void tearDown() throws Exception { } private static MockTransportService startTransport( - final String id, - final List knownNodes, - final Version version, - final ThreadPool threadPool) { + final String id, + final List knownNodes, + final Version version, + final ThreadPool threadPool + ) { boolean success = false; final Settings s = Settings.builder().put("node.name", id).build(); ClusterName clusterName = ClusterName.CLUSTER_NAME_SETTING.get(s); MockTransportService newService = MockTransportService.createNewService(s, version, threadPool, null); try { - newService.registerRequestHandler(ClusterSearchShardsAction.NAME, ThreadPool.Names.SAME, ClusterSearchShardsRequest::new, - (request, channel, task) -> { - channel.sendResponse(new ClusterSearchShardsResponse(new ClusterSearchShardsGroup[0], - knownNodes.toArray(new DiscoveryNode[0]), Collections.emptyMap())); - }); - newService.registerRequestHandler(SearchAction.NAME, ThreadPool.Names.SAME, SearchRequest::new, + newService.registerRequestHandler( + ClusterSearchShardsAction.NAME, + ThreadPool.Names.SAME, + ClusterSearchShardsRequest::new, (request, channel, task) -> { - InternalSearchResponse response = new InternalSearchResponse(new SearchHits(new SearchHit[0], - new TotalHits(0, TotalHits.Relation.EQUAL_TO), Float.NaN), InternalAggregations.EMPTY, null, null, false, null, 1); - SearchResponse searchResponse = new SearchResponse(response, null, 1, 1, 0, 100, ShardSearchFailure.EMPTY_ARRAY, - SearchResponse.Clusters.EMPTY); - channel.sendResponse(searchResponse); - }); - newService.registerRequestHandler(ClusterStateAction.NAME, ThreadPool.Names.SAME, ClusterStateRequest::new, + channel.sendResponse( + new ClusterSearchShardsResponse( + new ClusterSearchShardsGroup[0], + knownNodes.toArray(new DiscoveryNode[0]), + Collections.emptyMap() + ) + ); + } + ); + newService.registerRequestHandler(SearchAction.NAME, ThreadPool.Names.SAME, SearchRequest::new, (request, channel, task) -> { + InternalSearchResponse response = new InternalSearchResponse( + new SearchHits(new SearchHit[0], new TotalHits(0, TotalHits.Relation.EQUAL_TO), Float.NaN), + InternalAggregations.EMPTY, + null, + null, + false, + null, + 1 + ); + SearchResponse searchResponse = new SearchResponse( + response, + null, + 1, + 1, + 0, + 100, + ShardSearchFailure.EMPTY_ARRAY, + SearchResponse.Clusters.EMPTY + ); + channel.sendResponse(searchResponse); + }); + newService.registerRequestHandler( + ClusterStateAction.NAME, + ThreadPool.Names.SAME, + ClusterStateRequest::new, (request, channel, task) -> { - DiscoveryNodes.Builder builder = DiscoveryNodes.builder(); - for (DiscoveryNode node : knownNodes) { - builder.add(node); - } - ClusterState build = ClusterState.builder(clusterName).nodes(builder.build()).build(); - channel.sendResponse(new ClusterStateResponse(clusterName, build, false)); - }); + DiscoveryNodes.Builder builder = DiscoveryNodes.builder(); + for (DiscoveryNode node : knownNodes) { + builder.add(node); + } + ClusterState build = ClusterState.builder(clusterName).nodes(builder.build()).build(); + channel.sendResponse(new ClusterStateResponse(clusterName, build, false)); + } + ); newService.start(); newService.acceptIncomingRequests(); success = true; @@ -139,8 +167,7 @@ public void testSearchSkipUnavailable() throws IOException { updateRemoteClusterSettings(Collections.singletonMap("seeds", remoteNode.getAddress().toString())); for (int i = 0; i < 10; i++) { - restHighLevelClient.index( - new IndexRequest("index").id(String.valueOf(i)).source("field", "value"), RequestOptions.DEFAULT); + restHighLevelClient.index(new IndexRequest("index").id(String.valueOf(i)).source("field", "value"), RequestOptions.DEFAULT); } Response refreshResponse = client().performRequest(new Request("POST", "/index/_refresh")); assertEquals(200, refreshResponse.getStatusLine().getStatusCode()); @@ -168,8 +195,10 @@ public void testSearchSkipUnavailable() throws IOException { } { - SearchResponse response = restHighLevelClient.search(new SearchRequest("index", "remote1:index").scroll("1m"), - RequestOptions.DEFAULT); + SearchResponse response = restHighLevelClient.search( + new SearchRequest("index", "remote1:index").scroll("1m"), + RequestOptions.DEFAULT + ); assertEquals(2, response.getClusters().getTotal()); assertEquals(2, response.getClusters().getSuccessful()); assertEquals(0, response.getClusters().getSkipped()); @@ -203,8 +232,10 @@ public void testSearchSkipUnavailable() throws IOException { } { - SearchResponse response = restHighLevelClient.search(new SearchRequest("index", "remote1:index").scroll("1m"), - RequestOptions.DEFAULT); + SearchResponse response = restHighLevelClient.search( + new SearchRequest("index", "remote1:index").scroll("1m"), + RequestOptions.DEFAULT + ); assertEquals(2, response.getClusters().getTotal()); assertEquals(1, response.getClusters().getSuccessful()); assertEquals(1, response.getClusters().getSkipped()); @@ -232,16 +263,17 @@ public void testSkipUnavailableDependsOnSeeds() throws IOException { DiscoveryNode remoteNode = remoteTransport.getLocalDiscoNode(); { - //check that skip_unavailable alone cannot be set + // check that skip_unavailable alone cannot be set Request request = new Request("PUT", "/_cluster/settings"); - request.setEntity(buildUpdateSettingsRequestBody( - Collections.singletonMap("skip_unavailable", randomBoolean()))); - ResponseException responseException = expectThrows(ResponseException.class, - () -> client().performRequest(request)); + request.setEntity(buildUpdateSettingsRequestBody(Collections.singletonMap("skip_unavailable", randomBoolean()))); + ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest(request)); assertEquals(400, responseException.getResponse().getStatusLine().getStatusCode()); - assertThat(responseException.getMessage(), - containsString("Cannot configure setting [cluster.remote.remote1.skip_unavailable] if remote cluster is " + - "not enabled.")); + assertThat( + responseException.getMessage(), + containsString( + "Cannot configure setting [cluster.remote.remote1.skip_unavailable] if remote cluster is " + "not enabled." + ) + ); } Map settingsMap = new HashMap<>(); @@ -250,14 +282,17 @@ public void testSkipUnavailableDependsOnSeeds() throws IOException { updateRemoteClusterSettings(settingsMap); { - //check that seeds cannot be reset alone if skip_unavailable is set + // check that seeds cannot be reset alone if skip_unavailable is set Request request = new Request("PUT", "/_cluster/settings"); request.setEntity(buildUpdateSettingsRequestBody(Collections.singletonMap("seeds", null))); - ResponseException responseException = expectThrows(ResponseException.class, - () -> client().performRequest(request)); + ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest(request)); assertEquals(400, responseException.getResponse().getStatusLine().getStatusCode()); - assertThat(responseException.getMessage(), containsString("Cannot configure setting " + - "[cluster.remote.remote1.skip_unavailable] if remote cluster is not enabled.")); + assertThat( + responseException.getMessage(), + containsString( + "Cannot configure setting " + "[cluster.remote.remote1.skip_unavailable] if remote cluster is not enabled." + ) + ); } if (randomBoolean()) { @@ -274,27 +309,31 @@ public void testSkipUnavailableDependsOnSeeds() throws IOException { private static void assertSearchConnectFailure() { { - ElasticsearchException exception = expectThrows(ElasticsearchException.class, - () -> restHighLevelClient.search(new SearchRequest("index", "remote1:index"), RequestOptions.DEFAULT)); - ElasticsearchException rootCause = (ElasticsearchException)exception.getRootCause(); + ElasticsearchException exception = expectThrows( + ElasticsearchException.class, + () -> restHighLevelClient.search(new SearchRequest("index", "remote1:index"), RequestOptions.DEFAULT) + ); + ElasticsearchException rootCause = (ElasticsearchException) exception.getRootCause(); assertThat(rootCause.getMessage(), containsString("connect_exception")); } { - ElasticsearchException exception = expectThrows(ElasticsearchException.class, - () -> restHighLevelClient.search(new SearchRequest("remote1:index"), RequestOptions.DEFAULT)); - ElasticsearchException rootCause = (ElasticsearchException)exception.getRootCause(); + ElasticsearchException exception = expectThrows( + ElasticsearchException.class, + () -> restHighLevelClient.search(new SearchRequest("remote1:index"), RequestOptions.DEFAULT) + ); + ElasticsearchException rootCause = (ElasticsearchException) exception.getRootCause(); assertThat(rootCause.getMessage(), containsString("connect_exception")); } { - ElasticsearchException exception = expectThrows(ElasticsearchException.class, - () -> restHighLevelClient.search(new SearchRequest("remote1:index").scroll("1m"), RequestOptions.DEFAULT)); - ElasticsearchException rootCause = (ElasticsearchException)exception.getRootCause(); + ElasticsearchException exception = expectThrows( + ElasticsearchException.class, + () -> restHighLevelClient.search(new SearchRequest("remote1:index").scroll("1m"), RequestOptions.DEFAULT) + ); + ElasticsearchException rootCause = (ElasticsearchException) exception.getRootCause(); assertThat(rootCause.getMessage(), containsString("connect_exception")); } } - - private static void updateRemoteClusterSettings(Map settings) throws IOException { Request request = new Request("PUT", "/_cluster/settings"); request.setEntity(buildUpdateSettingsRequestBody(settings)); @@ -334,8 +373,6 @@ private HighLevelClient(RestClient restClient) { @Override protected Settings restClientSettings() { String token = basicAuthHeaderValue("admin", new SecureString("admin-password".toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } } diff --git a/qa/evil-tests/src/test/java/org/elasticsearch/bootstrap/ESPolicyUnitTests.java b/qa/evil-tests/src/test/java/org/elasticsearch/bootstrap/ESPolicyUnitTests.java index c33a6434c143e..c359929429d83 100644 --- a/qa/evil-tests/src/test/java/org/elasticsearch/bootstrap/ESPolicyUnitTests.java +++ b/qa/evil-tests/src/test/java/org/elasticsearch/bootstrap/ESPolicyUnitTests.java @@ -31,6 +31,7 @@ public class ESPolicyUnitTests extends ESTestCase { static final Map TEST_CODEBASES = BootstrapForTesting.getCodebases(); + /** * Test policy with null codesource. *

    @@ -61,8 +62,12 @@ public void testNullLocation() throws Exception { assumeTrue("test cannot run with security manager", System.getSecurityManager() == null); PermissionCollection noPermissions = new Permissions(); ESPolicy policy = new ESPolicy(TEST_CODEBASES, noPermissions, Collections.emptyMap(), true, new Permissions()); - assertFalse(policy.implies(new ProtectionDomain(new CodeSource(null, (Certificate[]) null), noPermissions), - new FilePermission("foo", "read"))); + assertFalse( + policy.implies( + new ProtectionDomain(new CodeSource(null, (Certificate[]) null), noPermissions), + new FilePermission("foo", "read") + ) + ); } public void testListen() { @@ -72,7 +77,9 @@ public void testListen() { assertFalse( policy.implies( new ProtectionDomain(ESPolicyUnitTests.class.getProtectionDomain().getCodeSource(), noPermissions), - new SocketPermission("localhost:" + randomFrom(0, randomIntBetween(49152, 65535)), "listen"))); + new SocketPermission("localhost:" + randomFrom(0, randomIntBetween(49152, 65535)), "listen") + ) + ); } @SuppressForbidden(reason = "to create FilePermission object") @@ -83,7 +90,9 @@ public void testDataPathPermissionIsChecked() { final ESPolicy policy = new ESPolicy(TEST_CODEBASES, new Permissions(), Collections.emptyMap(), true, dataPathPermission); assertTrue( policy.implies( - new ProtectionDomain(new CodeSource(null, (Certificate[]) null), new Permissions()), - new FilePermission("/home/elasticsearch/data/index/file.si", "read"))); + new ProtectionDomain(new CodeSource(null, (Certificate[]) null), new Permissions()), + new FilePermission("/home/elasticsearch/data/index/file.si", "read") + ) + ); } } diff --git a/qa/evil-tests/src/test/java/org/elasticsearch/bootstrap/EvilBootstrapChecksTests.java b/qa/evil-tests/src/test/java/org/elasticsearch/bootstrap/EvilBootstrapChecksTests.java index 00dc1b08c1d60..33d78ad7608af 100644 --- a/qa/evil-tests/src/test/java/org/elasticsearch/bootstrap/EvilBootstrapChecksTests.java +++ b/qa/evil-tests/src/test/java/org/elasticsearch/bootstrap/EvilBootstrapChecksTests.java @@ -52,10 +52,10 @@ public void testEnforceBootstrapChecks() throws NodeValidationException { final Logger logger = mock(Logger.class); final NodeValidationException e = expectThrows( - NodeValidationException.class, - () -> BootstrapChecks.check(emptyContext, false, checks, logger)); - final Matcher allOf = - allOf(containsString("bootstrap checks failed"), containsString("error")); + NodeValidationException.class, + () -> BootstrapChecks.check(emptyContext, false, checks, logger) + ); + final Matcher allOf = allOf(containsString("bootstrap checks failed"), containsString("error")); assertThat(e, hasToString(allOf)); verify(logger).info("explicitly enforcing bootstrap checks"); verifyNoMoreInteractions(logger); @@ -74,10 +74,10 @@ public void testInvalidValue() { setEsEnforceBootstrapChecks(value); final boolean enforceLimits = randomBoolean(); final IllegalArgumentException e = expectThrows( - IllegalArgumentException.class, - () -> BootstrapChecks.check(emptyContext, enforceLimits, emptyList())); - final Matcher matcher = containsString( - "[es.enforce.bootstrap.checks] must be [true] but was [" + value + "]"); + IllegalArgumentException.class, + () -> BootstrapChecks.check(emptyContext, enforceLimits, emptyList()) + ); + final Matcher matcher = containsString("[es.enforce.bootstrap.checks] must be [true] but was [" + value + "]"); assertThat(e, hasToString(matcher)); } diff --git a/qa/evil-tests/src/test/java/org/elasticsearch/bootstrap/EvilElasticsearchCliTests.java b/qa/evil-tests/src/test/java/org/elasticsearch/bootstrap/EvilElasticsearchCliTests.java index 6cb9ff881e102..6ed334ee2be4b 100644 --- a/qa/evil-tests/src/test/java/org/elasticsearch/bootstrap/EvilElasticsearchCliTests.java +++ b/qa/evil-tests/src/test/java/org/elasticsearch/bootstrap/EvilElasticsearchCliTests.java @@ -8,11 +8,10 @@ package org.elasticsearch.bootstrap; - import org.elasticsearch.cli.ExitCodes; -import org.elasticsearch.core.SuppressForbidden; -import org.elasticsearch.core.PathUtils; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.PathUtils; +import org.elasticsearch.core.SuppressForbidden; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.Matchers.hasItem; @@ -26,34 +25,24 @@ public void testPathHome() throws Exception { final String value = randomAlphaOfLength(16); System.setProperty("es.path.home", value); - runTest( - ExitCodes.OK, - true, - (output, error) -> {}, - (foreground, pidFile, quiet, esSettings) -> { - Settings settings = esSettings.settings(); - assertThat(settings.keySet(), hasSize(2)); - assertThat( - settings.get("path.home"), - equalTo(PathUtils.get(System.getProperty("user.dir")).resolve(value).toString())); - assertThat(settings.keySet(), hasItem("path.logs")); // added by env initialization - }); + runTest(ExitCodes.OK, true, (output, error) -> {}, (foreground, pidFile, quiet, esSettings) -> { + Settings settings = esSettings.settings(); + assertThat(settings.keySet(), hasSize(2)); + assertThat(settings.get("path.home"), equalTo(PathUtils.get(System.getProperty("user.dir")).resolve(value).toString())); + assertThat(settings.keySet(), hasItem("path.logs")); // added by env initialization + }); System.clearProperty("es.path.home"); final String commandLineValue = randomAlphaOfLength(16); - runTest( - ExitCodes.OK, - true, - (output, error) -> {}, - (foreground, pidFile, quiet, esSettings) -> { - Settings settings = esSettings.settings(); - assertThat(settings.keySet(), hasSize(2)); - assertThat( - settings.get("path.home"), - equalTo(PathUtils.get(System.getProperty("user.dir")).resolve(commandLineValue).toString())); - assertThat(settings.keySet(), hasItem("path.logs")); // added by env initialization - }, - "-Epath.home=" + commandLineValue); + runTest(ExitCodes.OK, true, (output, error) -> {}, (foreground, pidFile, quiet, esSettings) -> { + Settings settings = esSettings.settings(); + assertThat(settings.keySet(), hasSize(2)); + assertThat( + settings.get("path.home"), + equalTo(PathUtils.get(System.getProperty("user.dir")).resolve(commandLineValue).toString()) + ); + assertThat(settings.keySet(), hasItem("path.logs")); // added by env initialization + }, "-Epath.home=" + commandLineValue); if (pathHome != null) System.setProperty("es.path.home", pathHome); else System.clearProperty("es.path.home"); diff --git a/qa/evil-tests/src/test/java/org/elasticsearch/bootstrap/EvilJNANativesTests.java b/qa/evil-tests/src/test/java/org/elasticsearch/bootstrap/EvilJNANativesTests.java index bb723e838838b..8c4326082d509 100644 --- a/qa/evil-tests/src/test/java/org/elasticsearch/bootstrap/EvilJNANativesTests.java +++ b/qa/evil-tests/src/test/java/org/elasticsearch/bootstrap/EvilJNANativesTests.java @@ -28,10 +28,7 @@ public void testSetMaximumNumberOfThreads() throws IOException { for (final String line : lines) { if (line != null && line.startsWith("Max processes")) { final String[] fields = line.split("\\s+"); - final long limit = - "unlimited".equals(fields[2]) - ? JNACLibrary.RLIM_INFINITY - : Long.parseLong(fields[2]); + final long limit = "unlimited".equals(fields[2]) ? JNACLibrary.RLIM_INFINITY : Long.parseLong(fields[2]); assertThat(JNANatives.MAX_NUMBER_OF_THREADS, equalTo(limit)); return; } @@ -49,17 +46,13 @@ public void testSetMaxSizeVirtualMemory() throws IOException { if (line != null && line.startsWith("Max address space")) { final String[] fields = line.split("\\s+"); final String limit = fields[3]; - assertThat( - JNANatives.rlimitToString(JNANatives.MAX_SIZE_VIRTUAL_MEMORY), - equalTo(limit)); + assertThat(JNANatives.rlimitToString(JNANatives.MAX_SIZE_VIRTUAL_MEMORY), equalTo(limit)); return; } } fail("should have read max size virtual memory from /proc/self/limits"); } else if (Constants.MAC_OS_X) { - assertThat( - JNANatives.MAX_SIZE_VIRTUAL_MEMORY, - anyOf(equalTo(Long.MIN_VALUE), greaterThanOrEqualTo(0L))); + assertThat(JNANatives.MAX_SIZE_VIRTUAL_MEMORY, anyOf(equalTo(Long.MIN_VALUE), greaterThanOrEqualTo(0L))); } else { assertThat(JNANatives.MAX_SIZE_VIRTUAL_MEMORY, equalTo(Long.MIN_VALUE)); } @@ -72,17 +65,13 @@ public void testSetMaxFileSize() throws IOException { if (line != null && line.startsWith("Max file size")) { final String[] fields = line.split("\\s+"); final String limit = fields[3]; - assertThat( - JNANatives.rlimitToString(JNANatives.MAX_FILE_SIZE), - equalTo(limit)); + assertThat(JNANatives.rlimitToString(JNANatives.MAX_FILE_SIZE), equalTo(limit)); return; } } fail("should have read max file size from /proc/self/limits"); } else if (Constants.MAC_OS_X) { - assertThat( - JNANatives.MAX_FILE_SIZE, - anyOf(equalTo(Long.MIN_VALUE), greaterThanOrEqualTo(0L))); + assertThat(JNANatives.MAX_FILE_SIZE, anyOf(equalTo(Long.MIN_VALUE), greaterThanOrEqualTo(0L))); } else { assertThat(JNANatives.MAX_FILE_SIZE, equalTo(Long.MIN_VALUE)); } diff --git a/qa/evil-tests/src/test/java/org/elasticsearch/bootstrap/EvilSecurityTests.java b/qa/evil-tests/src/test/java/org/elasticsearch/bootstrap/EvilSecurityTests.java index d8b8e618cdad2..42ad37aac0872 100644 --- a/qa/evil-tests/src/test/java/org/elasticsearch/bootstrap/EvilSecurityTests.java +++ b/qa/evil-tests/src/test/java/org/elasticsearch/bootstrap/EvilSecurityTests.java @@ -9,9 +9,9 @@ package org.elasticsearch.bootstrap; import org.apache.lucene.util.Constants; -import org.elasticsearch.core.SuppressForbidden; -import org.elasticsearch.core.PathUtils; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.PathUtils; +import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.env.Environment; import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.test.ESTestCase; @@ -70,8 +70,11 @@ public void testEnvironmentPaths() throws Exception { Settings.Builder settingsBuilder = Settings.builder(); settingsBuilder.put(Environment.PATH_HOME_SETTING.getKey(), esHome.resolve("home").toString()); - settingsBuilder.putList(Environment.PATH_DATA_SETTING.getKey(), esHome.resolve("data1").toString(), - esHome.resolve("data2").toString()); + settingsBuilder.putList( + Environment.PATH_DATA_SETTING.getKey(), + esHome.resolve("data1").toString(), + esHome.resolve("data2").toString() + ); settingsBuilder.put(Environment.PATH_SHARED_DATA_SETTING.getKey(), esHome.resolve("custom").toString()); settingsBuilder.put(Environment.PATH_LOGS_SETTING.getKey(), esHome.resolve("logs").toString()); settingsBuilder.put(Environment.NODE_PIDFILE_SETTING.getKey(), esHome.resolve("test.pid").toString()); @@ -137,12 +140,10 @@ public void testDuplicateDataPaths() throws IOException { Files.createSymbolicLink(duplicate, data); } - final Settings settings = - Settings - .builder() - .put(Environment.PATH_HOME_SETTING.getKey(), home.toString()) - .putList(Environment.PATH_DATA_SETTING.getKey(), data.toString(), duplicate.toString()) - .build(); + final Settings settings = Settings.builder() + .put(Environment.PATH_HOME_SETTING.getKey(), home.toString()) + .putList(Environment.PATH_DATA_SETTING.getKey(), data.toString(), duplicate.toString()) + .build(); final Environment environment = TestEnvironment.newEnvironment(settings); final IllegalStateException e = expectThrows(IllegalStateException.class, () -> Security.createPermissions(environment)); diff --git a/qa/evil-tests/src/test/java/org/elasticsearch/bootstrap/PolicyUtilTests.java b/qa/evil-tests/src/test/java/org/elasticsearch/bootstrap/PolicyUtilTests.java index 95bf6c8e9cc38..a7ef98011f080 100644 --- a/qa/evil-tests/src/test/java/org/elasticsearch/bootstrap/PolicyUtilTests.java +++ b/qa/evil-tests/src/test/java/org/elasticsearch/bootstrap/PolicyUtilTests.java @@ -70,11 +70,7 @@ void clearProperty(String key) { } public void testCodebaseJarMap() throws Exception { - Set urls = new LinkedHashSet<>(List.of( - makeUrl("file:///foo.jar"), - makeUrl("file:///bar.txt"), - makeUrl("file:///a/bar.jar") - )); + Set urls = new LinkedHashSet<>(List.of(makeUrl("file:///foo.jar"), makeUrl("file:///bar.txt"), makeUrl("file:///a/bar.jar"))); Map jarMap = PolicyUtil.getCodebaseJarMap(urls); assertThat(jarMap, hasKey("foo.jar")); @@ -98,13 +94,10 @@ public void testPluginPolicyInfoNoJars() throws Exception { } public void testPluginPolicyInfo() throws Exception { - Path plugin = makeDummyPlugin("dummy.policy", - "foo.jar", "foo.txt", "bar.jar"); + Path plugin = makeDummyPlugin("dummy.policy", "foo.jar", "foo.txt", "bar.jar"); PluginPolicyInfo info = PolicyUtil.readPolicyInfo(plugin); assertThat(info.policy, is(not(nullValue()))); - assertThat(info.jars, containsInAnyOrder( - plugin.resolve("foo.jar").toUri().toURL(), - plugin.resolve("bar.jar").toUri().toURL())); + assertThat(info.jars, containsInAnyOrder(plugin.resolve("foo.jar").toUri().toURL(), plugin.resolve("bar.jar").toUri().toURL())); } public void testPolicyMissingCodebaseProperty() throws Exception { @@ -127,8 +120,7 @@ public void testPolicyPermissions() throws Exception { assertThat(globalPermissions, contains(new RuntimePermission("queuePrintJob"))); Set jarPermissions = PolicyUtil.getPolicyPermissions(jarUrl, policy, tmpDir); - assertThat(jarPermissions, - containsInAnyOrder(new RuntimePermission("getClassLoader"), new RuntimePermission("queuePrintJob"))); + assertThat(jarPermissions, containsInAnyOrder(new RuntimePermission("getClassLoader"), new RuntimePermission("queuePrintJob"))); } finally { clearProperty("jarUrl"); } @@ -185,9 +177,11 @@ void assertAllowedPermissions(List allowedPermissions, PolicyParser pars void assertIllegalPermission(String clazz, String name, String actions, Path tmpDir, PolicyParser parser) throws Exception { // global policy final Path globalPlugin = makeSinglePermissionPlugin(null, clazz, name, actions); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, "Permission (" + clazz + " " + name + (actions == null ? "" : (" " + actions)) + ") should be illegal", - () -> parser.parse(globalPlugin, tmpDir)); // no error + () -> parser.parse(globalPlugin, tmpDir) + ); // no error assertThat(e.getMessage(), containsString("contains illegal permission")); assertThat(e.getMessage(), containsString("in global grant")); diff --git a/qa/evil-tests/src/test/java/org/elasticsearch/cli/EvilCommandTests.java b/qa/evil-tests/src/test/java/org/elasticsearch/cli/EvilCommandTests.java index 44be4033068a5..7fb22afd76d23 100644 --- a/qa/evil-tests/src/test/java/org/elasticsearch/cli/EvilCommandTests.java +++ b/qa/evil-tests/src/test/java/org/elasticsearch/cli/EvilCommandTests.java @@ -9,6 +9,7 @@ package org.elasticsearch.cli; import joptsimple.OptionSet; + import org.elasticsearch.test.ESTestCase; import java.io.IOException; diff --git a/qa/evil-tests/src/test/java/org/elasticsearch/cli/EvilEnvironmentAwareCommandTests.java b/qa/evil-tests/src/test/java/org/elasticsearch/cli/EvilEnvironmentAwareCommandTests.java index 1fa799b8db5b2..c917e681dd963 100644 --- a/qa/evil-tests/src/test/java/org/elasticsearch/cli/EvilEnvironmentAwareCommandTests.java +++ b/qa/evil-tests/src/test/java/org/elasticsearch/cli/EvilEnvironmentAwareCommandTests.java @@ -9,6 +9,7 @@ package org.elasticsearch.cli; import joptsimple.OptionSet; + import org.apache.lucene.util.TestRuleRestoreSystemProperties; import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.env.Environment; @@ -41,12 +42,14 @@ protected void execute(Terminal terminal, OptionSet options, Environment env) th } final TestEnvironmentAwareCommand command = new TestEnvironmentAwareCommand("test"); - final UserException e = - expectThrows(UserException.class, () -> command.mainWithoutErrorHandling(new String[0], new MockTerminal())); + final UserException e = expectThrows( + UserException.class, + () -> command.mainWithoutErrorHandling(new String[0], new MockTerminal()) + ); assertThat(e, hasToString(containsString("the system property [es.path.conf] must be set"))); } - @SuppressForbidden(reason = "clears system property es.path.conf as part of test setup") + @SuppressForbidden(reason = "clears system property es.path.conf as part of test setup") private void clearEsPathConf() { System.clearProperty("es.path.conf"); } diff --git a/qa/evil-tests/src/test/java/org/elasticsearch/cluster/metadata/EvilSystemPropertyTests.java b/qa/evil-tests/src/test/java/org/elasticsearch/cluster/metadata/EvilSystemPropertyTests.java index 34b4804d3df50..799752d5399e2 100644 --- a/qa/evil-tests/src/test/java/org/elasticsearch/cluster/metadata/EvilSystemPropertyTests.java +++ b/qa/evil-tests/src/test/java/org/elasticsearch/cluster/metadata/EvilSystemPropertyTests.java @@ -7,17 +7,18 @@ */ package org.elasticsearch.cluster.metadata; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.test.ESTestCase; public class EvilSystemPropertyTests extends ESTestCase { @SuppressForbidden(reason = "manipulates system properties for testing") public void testMaxNumShards() { - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> - IndexMetadata.buildNumberOfShardsSetting() - .get(Settings.builder().put("index.number_of_shards", 1025).build())); + IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> IndexMetadata.buildNumberOfShardsSetting().get(Settings.builder().put("index.number_of_shards", 1025).build()) + ); assertEquals("Failed to parse value [1025] for setting [index.number_of_shards] must be <= 1024", exception.getMessage()); Integer numShards = IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.get(Settings.builder().put("index.number_of_shards", 100).build()); @@ -25,9 +26,10 @@ public void testMaxNumShards() { int limit = randomIntBetween(1, 10); System.setProperty("es.index.max_number_of_shards", Integer.toString(limit)); try { - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> - IndexMetadata.buildNumberOfShardsSetting() - .get(Settings.builder().put("index.number_of_shards", 11).build())); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> IndexMetadata.buildNumberOfShardsSetting().get(Settings.builder().put("index.number_of_shards", 11).build()) + ); assertEquals("Failed to parse value [11] for setting [index.number_of_shards] must be <= " + limit, e.getMessage()); } finally { System.clearProperty("es.index.max_number_of_shards"); diff --git a/qa/evil-tests/src/test/java/org/elasticsearch/common/logging/EvilLoggerConfigurationTests.java b/qa/evil-tests/src/test/java/org/elasticsearch/common/logging/EvilLoggerConfigurationTests.java index cd7867057f8ce..47cf56dee751f 100644 --- a/qa/evil-tests/src/test/java/org/elasticsearch/common/logging/EvilLoggerConfigurationTests.java +++ b/qa/evil-tests/src/test/java/org/elasticsearch/common/logging/EvilLoggerConfigurationTests.java @@ -50,9 +50,7 @@ public void testResolveMultipleConfigs() throws Exception { final Level level = LogManager.getLogger("test").getLevel(); try { final Path configDir = getDataPath("config"); - final Settings settings = Settings.builder() - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .build(); + final Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build(); final Environment environment = new Environment(settings, configDir); LogConfigurator.configure(environment); @@ -117,9 +115,7 @@ public void testResolveOrder() throws Exception { public void testHierarchy() throws Exception { final Path configDir = getDataPath("hierarchy"); - final Settings settings = Settings.builder() - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .build(); + final Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build(); final Environment environment = new Environment(settings, configDir); LogConfigurator.configure(environment); @@ -135,9 +131,7 @@ public void testHierarchy() throws Exception { public void testMissingConfigFile() { final Path configDir = getDataPath("does_not_exist"); - final Settings settings = Settings.builder() - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .build(); + final Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build(); final Environment environment = new Environment(settings, configDir); UserException e = expectThrows(UserException.class, () -> LogConfigurator.configure(environment)); assertThat(e, hasToString(containsString("no log4j2.properties found; tried"))); diff --git a/qa/evil-tests/src/test/java/org/elasticsearch/common/logging/EvilLoggerTests.java b/qa/evil-tests/src/test/java/org/elasticsearch/common/logging/EvilLoggerTests.java index 485db9612aebb..edf3d80e1b679 100644 --- a/qa/evil-tests/src/test/java/org/elasticsearch/common/logging/EvilLoggerTests.java +++ b/qa/evil-tests/src/test/java/org/elasticsearch/common/logging/EvilLoggerTests.java @@ -78,11 +78,10 @@ public void testLocationInfoTest() throws IOException, UserException { testLogger.info("This is an info message"); testLogger.debug("This is a debug message"); testLogger.trace("This is a trace message"); - final String path = - System.getProperty("es.logs.base_path") + - System.getProperty("file.separator") + - System.getProperty("es.logs.cluster_name") + - ".log"; + final String path = System.getProperty("es.logs.base_path") + + System.getProperty("file.separator") + + System.getProperty("es.logs.cluster_name") + + ".log"; final List events = Files.readAllLines(PathUtils.get(path)); assertThat(events.size(), equalTo(5)); final String location = "org.elasticsearch.common.logging.EvilLoggerTests.testLocationInfoTest"; @@ -116,8 +115,11 @@ public void testConcurrentDeprecationLogger() throws IOException, UserException, } for (int j = 0; j < iterations; j++) { for (final Integer id : ids) { - deprecationLogger.critical(DeprecationCategory.OTHER, Integer.toString(id), - "This is a maybe logged deprecation message" + id); + deprecationLogger.critical( + DeprecationCategory.OTHER, + Integer.toString(id), + "This is a maybe logged deprecation message" + id + ); } } @@ -127,13 +129,14 @@ public void testConcurrentDeprecationLogger() throws IOException, UserException, * on the other threads. */ final List warnings = threadContext.getResponseHeaders().get("Warning"); - final Set actualWarningValues = - warnings.stream().map(s -> HeaderWarning.extractWarningValueFromWarningHeader(s, true)) - .collect(Collectors.toSet()); + final Set actualWarningValues = warnings.stream() + .map(s -> HeaderWarning.extractWarningValueFromWarningHeader(s, true)) + .collect(Collectors.toSet()); for (int j = 0; j < 128; j++) { assertThat( - actualWarningValues, - hasItem(HeaderWarning.escapeAndEncode("This is a maybe logged deprecation message" + j))); + actualWarningValues, + hasItem(HeaderWarning.escapeAndEncode("This is a maybe logged deprecation message" + j)) + ); } try { @@ -152,11 +155,10 @@ public void testConcurrentDeprecationLogger() throws IOException, UserException, // wait for all threads to complete their iterations barrier.await(); - final String deprecationPath = - System.getProperty("es.logs.base_path") + - System.getProperty("file.separator") + - System.getProperty("es.logs.cluster_name") + - "_deprecation.log"; + final String deprecationPath = System.getProperty("es.logs.base_path") + + System.getProperty("file.separator") + + System.getProperty("es.logs.cluster_name") + + "_deprecation.log"; final List deprecationEvents = Files.readAllLines(PathUtils.get(deprecationPath)); // we appended an integer to each log message, use that for sorting Pattern pattern = Pattern.compile(".*message(\\d+)\""); @@ -169,10 +171,11 @@ public void testConcurrentDeprecationLogger() throws IOException, UserException, for (int i = 0; i < 128; i++) { assertLogLine( - deprecationEvents.get(i), - DeprecationLogger.CRITICAL, - "org.elasticsearch.common.logging.DeprecationLogger.logDeprecation", - "This is a maybe logged deprecation message" + i); + deprecationEvents.get(i), + DeprecationLogger.CRITICAL, + "org.elasticsearch.common.logging.DeprecationLogger.logDeprecation", + "This is a maybe logged deprecation message" + i + ); } for (final Thread thread : threads) { @@ -190,23 +193,23 @@ public void testDeprecatedSettings() throws IOException, UserException { final int iterations = randomIntBetween(0, 128); for (int i = 0; i < iterations; i++) { setting.get(settings); - assertSettingDeprecationsAndWarnings(new Setting[]{setting}); + assertSettingDeprecationsAndWarnings(new Setting[] { setting }); } - final String deprecationPath = - System.getProperty("es.logs.base_path") + - System.getProperty("file.separator") + - System.getProperty("es.logs.cluster_name") + - "_deprecation.log"; + final String deprecationPath = System.getProperty("es.logs.base_path") + + System.getProperty("file.separator") + + System.getProperty("es.logs.cluster_name") + + "_deprecation.log"; final List deprecationEvents = Files.readAllLines(PathUtils.get(deprecationPath)); if (iterations > 0) { assertThat(deprecationEvents.size(), equalTo(1)); assertLogLine( - deprecationEvents.get(0), - DeprecationLogger.CRITICAL, - "org.elasticsearch.common.logging.DeprecationLogger.logDeprecation", - "\\[deprecated.foo\\] setting was deprecated in Elasticsearch and will be removed in a future release! " + - "See the breaking changes documentation for the next major version."); + deprecationEvents.get(0), + DeprecationLogger.CRITICAL, + "org.elasticsearch.common.logging.DeprecationLogger.logDeprecation", + "\\[deprecated.foo\\] setting was deprecated in Elasticsearch and will be removed in a future release! " + + "See the breaking changes documentation for the next major version." + ); } } @@ -234,11 +237,10 @@ public void testPrefixLogger() throws IOException, IllegalAccessException, UserE final Exception e = new Exception("exception"); logger.info(new ParameterizedMessage("{}", "test"), e); - final String path = - System.getProperty("es.logs.base_path") + - System.getProperty("file.separator") + - System.getProperty("es.logs.cluster_name") + - ".log"; + final String path = System.getProperty("es.logs.base_path") + + System.getProperty("file.separator") + + System.getProperty("es.logs.cluster_name") + + ".log"; final List events = Files.readAllLines(PathUtils.get(path)); final StringWriter sw = new StringWriter(); @@ -248,8 +250,7 @@ public void testPrefixLogger() throws IOException, IllegalAccessException, UserE final int expectedLogLines = 3; assertThat(events.size(), equalTo(expectedLogLines + stackTraceLength)); for (int i = 0; i < expectedLogLines; i++) { - assertThat("Contents of [" + path + "] are wrong", - events.get(i), startsWith("[" + getTestName() + "]" + prefix + " test")); + assertThat("Contents of [" + path + "] are wrong", events.get(i), startsWith("[" + getTestName() + "]" + prefix + " test")); } } @@ -268,9 +269,9 @@ public void testPrefixLoggerMarkersCanBeCollected() throws IOException, UserExce public void testProperties() throws IOException, UserException { final Settings settings = Settings.builder() - .put("cluster.name", randomAlphaOfLength(16)) - .put("node.name", randomAlphaOfLength(16)) - .build(); + .put("cluster.name", randomAlphaOfLength(16)) + .put("node.name", randomAlphaOfLength(16)) + .build(); setupLogging("minimal", settings); assertNotNull(System.getProperty("es.logs.base_path")); @@ -283,18 +284,25 @@ public void testNoNodeNameInPatternWarning() throws IOException, UserException { String nodeName = randomAlphaOfLength(16); LogConfigurator.setNodeName(nodeName); setupLogging("no_node_name"); - final String path = - System.getProperty("es.logs.base_path") + - System.getProperty("file.separator") + - System.getProperty("es.logs.cluster_name") + ".log"; + final String path = System.getProperty("es.logs.base_path") + + System.getProperty("file.separator") + + System.getProperty("es.logs.cluster_name") + + ".log"; final List events = Files.readAllLines(PathUtils.get(path)); assertThat(events.size(), equalTo(2)); final String location = "org.elasticsearch.common.logging.LogConfigurator"; // the first message is a warning for unsupported configuration files - assertLogLine(events.get(0), Level.WARN, location, "\\[" + nodeName + "\\] Some logging configurations have " + assertLogLine( + events.get(0), + Level.WARN, + location, + "\\[" + + nodeName + + "\\] Some logging configurations have " + "%marker but don't have %node_name. We will automatically add %node_name to the pattern to ease the " + "migration for users who customize log4j2.properties but will stop this behavior in 7.0. You should " - + "manually replace `%node_name` with `\\[%node_name\\]%marker ` in these locations:"); + + "manually replace `%node_name` with `\\[%node_name\\]%marker ` in these locations:" + ); if (Constants.WINDOWS) { assertThat(events.get(1), endsWith("no_node_name\\log4j2.properties")); } else { diff --git a/qa/evil-tests/src/test/java/org/elasticsearch/common/settings/EvilKeyStoreWrapperTests.java b/qa/evil-tests/src/test/java/org/elasticsearch/common/settings/EvilKeyStoreWrapperTests.java index 7478ad144105a..78d4aafb4afb4 100644 --- a/qa/evil-tests/src/test/java/org/elasticsearch/common/settings/EvilKeyStoreWrapperTests.java +++ b/qa/evil-tests/src/test/java/org/elasticsearch/common/settings/EvilKeyStoreWrapperTests.java @@ -44,7 +44,8 @@ public void testWritePermissions() throws Exception { Locale.ROOT, "unable to create temporary keystore at [%s], write permissions required for [%s] or run [elasticsearch-keystore upgrade]", configDir.resolve("elasticsearch.keystore.tmp"), - configDir); + configDir + ); assertThat(e, hasToString(containsString(expected))); assertThat(e.exitCode, equalTo(ExitCodes.CONFIG)); assertThat(e.getCause(), instanceOf(AccessDeniedException.class)); diff --git a/qa/evil-tests/src/test/java/org/elasticsearch/env/NodeEnvironmentEvilTests.java b/qa/evil-tests/src/test/java/org/elasticsearch/env/NodeEnvironmentEvilTests.java index e0129742014b2..000ff07a5fbe5 100644 --- a/qa/evil-tests/src/test/java/org/elasticsearch/env/NodeEnvironmentEvilTests.java +++ b/qa/evil-tests/src/test/java/org/elasticsearch/env/NodeEnvironmentEvilTests.java @@ -35,16 +35,23 @@ public void testMissingWritePermission() throws IOException { final String[] tempPaths = tmpPaths(); Path path = PathUtils.get(randomFrom(tempPaths)); try (PosixPermissionsResetter attr = new PosixPermissionsResetter(path)) { - attr.setPermissions(new HashSet<>(Arrays.asList(PosixFilePermission.OTHERS_READ, PosixFilePermission.GROUP_READ, - PosixFilePermission.OWNER_READ))); + attr.setPermissions( + new HashSet<>( + Arrays.asList(PosixFilePermission.OTHERS_READ, PosixFilePermission.GROUP_READ, PosixFilePermission.OWNER_READ) + ) + ); Settings build = Settings.builder() - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toAbsolutePath().toString()) - .putList(Environment.PATH_DATA_SETTING.getKey(), tempPaths).build(); - IllegalStateException exception = expectThrows(IllegalStateException.class, () -> { - new NodeEnvironment(build, TestEnvironment.newEnvironment(build)); - }); - assertTrue(exception.getCause().getCause().getMessage(), - exception.getCause().getCause().getMessage().startsWith(path.toString())); + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toAbsolutePath().toString()) + .putList(Environment.PATH_DATA_SETTING.getKey(), tempPaths) + .build(); + IllegalStateException exception = expectThrows( + IllegalStateException.class, + () -> { new NodeEnvironment(build, TestEnvironment.newEnvironment(build)); } + ); + assertTrue( + exception.getCause().getCause().getMessage(), + exception.getCause().getCause().getMessage().startsWith(path.toString()) + ); } } @@ -52,18 +59,22 @@ public void testMissingWritePermissionOnIndex() throws IOException { assumeTrue("posix filesystem", isPosix); final String[] tempPaths = tmpPaths(); Path path = PathUtils.get(randomFrom(tempPaths)); - Path fooIndex = path.resolve(NodeEnvironment.INDICES_FOLDER) - .resolve("foo"); + Path fooIndex = path.resolve(NodeEnvironment.INDICES_FOLDER).resolve("foo"); Files.createDirectories(fooIndex); try (PosixPermissionsResetter attr = new PosixPermissionsResetter(fooIndex)) { - attr.setPermissions(new HashSet<>(Arrays.asList(PosixFilePermission.OTHERS_READ, PosixFilePermission.GROUP_READ, - PosixFilePermission.OWNER_READ))); + attr.setPermissions( + new HashSet<>( + Arrays.asList(PosixFilePermission.OTHERS_READ, PosixFilePermission.GROUP_READ, PosixFilePermission.OWNER_READ) + ) + ); Settings build = Settings.builder() .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toAbsolutePath().toString()) - .putList(Environment.PATH_DATA_SETTING.getKey(), tempPaths).build(); - IOException ioException = expectThrows(IOException.class, () -> { - new NodeEnvironment(build, TestEnvironment.newEnvironment(build)); - }); + .putList(Environment.PATH_DATA_SETTING.getKey(), tempPaths) + .build(); + IOException ioException = expectThrows( + IOException.class, + () -> { new NodeEnvironment(build, TestEnvironment.newEnvironment(build)); } + ); assertTrue(ioException.getMessage(), ioException.getMessage().startsWith("failed to test writes in data directory")); } } @@ -72,8 +83,7 @@ public void testMissingWritePermissionOnShard() throws IOException { assumeTrue("posix filesystem", isPosix); final String[] tempPaths = tmpPaths(); Path path = PathUtils.get(randomFrom(tempPaths)); - Path fooIndex = path.resolve(NodeEnvironment.INDICES_FOLDER) - .resolve("foo"); + Path fooIndex = path.resolve(NodeEnvironment.INDICES_FOLDER).resolve("foo"); Path fooShard = fooIndex.resolve("0"); Path fooShardIndex = fooShard.resolve("index"); Path fooShardTranslog = fooShard.resolve("translog"); @@ -81,14 +91,19 @@ public void testMissingWritePermissionOnShard() throws IOException { Path pick = randomFrom(fooShard, fooShardIndex, fooShardTranslog, fooShardState); Files.createDirectories(pick); try (PosixPermissionsResetter attr = new PosixPermissionsResetter(pick)) { - attr.setPermissions(new HashSet<>(Arrays.asList(PosixFilePermission.OTHERS_READ, PosixFilePermission.GROUP_READ, - PosixFilePermission.OWNER_READ))); + attr.setPermissions( + new HashSet<>( + Arrays.asList(PosixFilePermission.OTHERS_READ, PosixFilePermission.GROUP_READ, PosixFilePermission.OWNER_READ) + ) + ); Settings build = Settings.builder() .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toAbsolutePath().toString()) - .putList(Environment.PATH_DATA_SETTING.getKey(), tempPaths).build(); - IOException ioException = expectThrows(IOException.class, () -> { - new NodeEnvironment(build, TestEnvironment.newEnvironment(build)); - }); + .putList(Environment.PATH_DATA_SETTING.getKey(), tempPaths) + .build(); + IOException ioException = expectThrows( + IOException.class, + () -> { new NodeEnvironment(build, TestEnvironment.newEnvironment(build)); } + ); assertTrue(ioException.getMessage(), ioException.getMessage().startsWith("failed to test writes in data directory")); } } diff --git a/qa/evil-tests/src/test/java/org/elasticsearch/index/engine/EvilInternalEngineTests.java b/qa/evil-tests/src/test/java/org/elasticsearch/index/engine/EvilInternalEngineTests.java index f321d0309a4b4..089f3de3bf257 100644 --- a/qa/evil-tests/src/test/java/org/elasticsearch/index/engine/EvilInternalEngineTests.java +++ b/qa/evil-tests/src/test/java/org/elasticsearch/index/engine/EvilInternalEngineTests.java @@ -51,9 +51,12 @@ public void testOutOfMemoryErrorWhileMergingIsRethrownAndIsUncaught() throws IOE final FilterMergePolicy mergePolicy = new FilterMergePolicy(newMergePolicy()) { @Override - public MergeSpecification findForcedMerges(SegmentInfos segmentInfos, int maxSegmentCount, - Map segmentsToMerge, - MergeContext mergeContext) throws IOException { + public MergeSpecification findForcedMerges( + SegmentInfos segmentInfos, + int maxSegmentCount, + Map segmentsToMerge, + MergeContext mergeContext + ) throws IOException { final List segments = segmentsReference.get(); if (segments != null) { final MergeSpecification spec = new MergeSpecification(); @@ -64,8 +67,8 @@ public MergeSpecification findForcedMerges(SegmentInfos segmentInfos, int maxSeg } @Override - public MergeSpecification findMerges(MergeTrigger mergeTrigger, SegmentInfos segmentInfos, - MergeContext mergeContext) throws IOException { + public MergeSpecification findMerges(MergeTrigger mergeTrigger, SegmentInfos segmentInfos, MergeContext mergeContext) + throws IOException { final List segments = segmentsReference.get(); if (segments != null) { final MergeSpecification spec = new MergeSpecification(); @@ -76,54 +79,47 @@ public MergeSpecification findMerges(MergeTrigger mergeTrigger, SegmentInfos seg } }; - try (Engine e = createEngine( - defaultSettings, - store, - primaryTranslogDir, - mergePolicy, - (directory, iwc) -> { - final MergeScheduler mergeScheduler = iwc.getMergeScheduler(); - assertNotNull(mergeScheduler); - iwc.setMergeScheduler(new FilterMergeScheduler(mergeScheduler) { + try (Engine e = createEngine(defaultSettings, store, primaryTranslogDir, mergePolicy, (directory, iwc) -> { + final MergeScheduler mergeScheduler = iwc.getMergeScheduler(); + assertNotNull(mergeScheduler); + iwc.setMergeScheduler(new FilterMergeScheduler(mergeScheduler) { + @Override + public void merge(MergeSource mergeSource, MergeTrigger trigger) throws IOException { + final FilterMergeSource wrappedMergeSource = new FilterMergeSource(mergeSource) { @Override - public void merge(MergeSource mergeSource, MergeTrigger trigger) throws IOException { - final FilterMergeSource wrappedMergeSource = new FilterMergeSource(mergeSource) { - @Override - public MergePolicy.OneMerge getNextMerge() { - synchronized (mergeSource) { - /* - * This will be called when we flush when we will not be ready to return the segments. - * After the segments are on disk, we can only return them from here once or the merge - * scheduler will be stuck in a loop repeatedly peeling off the same segments to schedule - * for merging. - */ - if (segmentsReference.get() == null) { - return super.getNextMerge(); - } else { - final List segments = segmentsReference.getAndSet(null); - return new MergePolicy.OneMerge(segments); - } - } + public MergePolicy.OneMerge getNextMerge() { + synchronized (mergeSource) { + /* + * This will be called when we flush when we will not be ready to return the segments. + * After the segments are on disk, we can only return them from here once or the merge + * scheduler will be stuck in a loop repeatedly peeling off the same segments to schedule + * for merging. + */ + if (segmentsReference.get() == null) { + return super.getNextMerge(); + } else { + final List segments = segmentsReference.getAndSet(null); + return new MergePolicy.OneMerge(segments); } + } + } - @Override - public void merge(MergePolicy.OneMerge merge) { - throw new OutOfMemoryError("640K ought to be enough for anybody"); - } - }; - super.merge(wrappedMergeSource, trigger); + @Override + public void merge(MergePolicy.OneMerge merge) { + throw new OutOfMemoryError("640K ought to be enough for anybody"); } - }); - return new IndexWriter(directory, iwc); - }, - null, - null)) { + }; + super.merge(wrappedMergeSource, trigger); + } + }); + return new IndexWriter(directory, iwc); + }, null, null)) { // force segments to exist on disk final ParsedDocument doc1 = testParsedDocument("1", null, testDocumentWithTextField(), B_1, null); e.index(indexForDoc(doc1)); e.flush(); - final List segments = - StreamSupport.stream(e.getLastCommittedSegmentInfos().spliterator(), false).collect(Collectors.toList()); + final List segments = StreamSupport.stream(e.getLastCommittedSegmentInfos().spliterator(), false) + .collect(Collectors.toList()); segmentsReference.set(segments); // trigger a background merge that will be managed by the concurrent merge scheduler e.forceMerge(randomBoolean(), 0, false, UUIDs.randomBase64UUID()); diff --git a/qa/evil-tests/src/test/java/org/elasticsearch/monitor/os/EvilOsProbeTests.java b/qa/evil-tests/src/test/java/org/elasticsearch/monitor/os/EvilOsProbeTests.java index 49c62466a101f..71967cd25b45b 100644 --- a/qa/evil-tests/src/test/java/org/elasticsearch/monitor/os/EvilOsProbeTests.java +++ b/qa/evil-tests/src/test/java/org/elasticsearch/monitor/os/EvilOsProbeTests.java @@ -23,7 +23,7 @@ public class EvilOsProbeTests extends ESTestCase { - public void testOsPrettyName() throws IOException { + public void testOsPrettyName() throws IOException { final OsInfo osInfo = OsProbe.getInstance().osInfo(randomLongBetween(1, 100), randomIntBetween(1, 8)); if (Constants.LINUX) { final List lines; @@ -33,7 +33,8 @@ public void testOsPrettyName() throws IOException { lines = Files.readAllLines(PathUtils.get("/usr/lib/os-release")); } else { lines = Collections.singletonList( - "PRETTY_NAME=\"" + Files.readAllLines(PathUtils.get("/etc/system-release")).get(0) + "\""); + "PRETTY_NAME=\"" + Files.readAllLines(PathUtils.get("/etc/system-release")).get(0) + "\"" + ); } for (final String line : lines) { if (line != null && line.startsWith("PRETTY_NAME=")) { diff --git a/qa/evil-tests/src/test/java/org/elasticsearch/plugins/cli/PluginSecurityTests.java b/qa/evil-tests/src/test/java/org/elasticsearch/plugins/cli/PluginSecurityTests.java index 65f901c43883b..0cfc14a00012e 100644 --- a/qa/evil-tests/src/test/java/org/elasticsearch/plugins/cli/PluginSecurityTests.java +++ b/qa/evil-tests/src/test/java/org/elasticsearch/plugins/cli/PluginSecurityTests.java @@ -36,9 +36,7 @@ PluginPolicyInfo makeDummyPlugin(String policy, String... files) throws IOExcept /** Test that we can parse the set of permissions correctly for a simple policy */ public void testParsePermissions() throws Exception { - assumeTrue( - "test cannot run with security manager enabled", - System.getSecurityManager() == null); + assumeTrue("test cannot run with security manager enabled", System.getSecurityManager() == null); Path scratch = createTempDir(); PluginPolicyInfo info = makeDummyPlugin("simple-plugin-security.policy"); Set actual = PluginSecurity.getPermissionDescriptions(info, scratch); @@ -47,21 +45,24 @@ public void testParsePermissions() throws Exception { /** Test that we can parse the set of permissions correctly for a complex policy */ public void testParseTwoPermissions() throws Exception { - assumeTrue( - "test cannot run with security manager enabled", - System.getSecurityManager() == null); + assumeTrue("test cannot run with security manager enabled", System.getSecurityManager() == null); Path scratch = createTempDir(); PluginPolicyInfo info = makeDummyPlugin("complex-plugin-security.policy"); Set actual = PluginSecurity.getPermissionDescriptions(info, scratch); - assertThat(actual, containsInAnyOrder( - PluginSecurity.formatPermission(new RuntimePermission("getClassLoader")), - PluginSecurity.formatPermission(new RuntimePermission("setFactory")))); + assertThat( + actual, + containsInAnyOrder( + PluginSecurity.formatPermission(new RuntimePermission("getClassLoader")), + PluginSecurity.formatPermission(new RuntimePermission("setFactory")) + ) + ); } /** Test that we can format some simple permissions properly */ public void testFormatSimplePermission() throws Exception { assertEquals( - "java.lang.RuntimePermission accessDeclaredMembers", - PluginSecurity.formatPermission(new RuntimePermission("accessDeclaredMembers"))); + "java.lang.RuntimePermission accessDeclaredMembers", + PluginSecurity.formatPermission(new RuntimePermission("accessDeclaredMembers")) + ); } } diff --git a/qa/evil-tests/src/test/java/org/elasticsearch/threadpool/EvilThreadPoolTests.java b/qa/evil-tests/src/test/java/org/elasticsearch/threadpool/EvilThreadPoolTests.java index 9ff3dc5231f9a..3d1d134e60773 100644 --- a/qa/evil-tests/src/test/java/org/elasticsearch/threadpool/EvilThreadPoolTests.java +++ b/qa/evil-tests/src/test/java/org/elasticsearch/threadpool/EvilThreadPoolTests.java @@ -9,11 +9,11 @@ package org.elasticsearch.threadpool; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.AbstractRunnable; import org.elasticsearch.common.util.concurrent.EsExecutors; import org.elasticsearch.common.util.concurrent.EsThreadPoolExecutor; import org.elasticsearch.common.util.concurrent.PrioritizedEsThreadPoolExecutor; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.test.ESTestCase; import org.junit.After; import org.junit.Before; @@ -58,8 +58,14 @@ public void testExecutionErrorOnDirectExecutorService() throws InterruptedExcept } public void testExecutionErrorOnFixedESThreadPoolExecutor() throws InterruptedException { - final EsThreadPoolExecutor fixedExecutor = EsExecutors.newFixed("test", 1, 1, - EsExecutors.daemonThreadFactory("test"), threadPool.getThreadContext(), randomBoolean()); + final EsThreadPoolExecutor fixedExecutor = EsExecutors.newFixed( + "test", + 1, + 1, + EsExecutors.daemonThreadFactory("test"), + threadPool.getThreadContext(), + randomBoolean() + ); try { checkExecutionError(getExecuteRunner(fixedExecutor)); checkExecutionError(getSubmitRunner(fixedExecutor)); @@ -69,8 +75,15 @@ public void testExecutionErrorOnFixedESThreadPoolExecutor() throws InterruptedEx } public void testExecutionErrorOnScalingESThreadPoolExecutor() throws InterruptedException { - final EsThreadPoolExecutor scalingExecutor = EsExecutors.newScaling("test", 1, 1, - 10, TimeUnit.SECONDS, EsExecutors.daemonThreadFactory("test"), threadPool.getThreadContext()); + final EsThreadPoolExecutor scalingExecutor = EsExecutors.newScaling( + "test", + 1, + 1, + 10, + TimeUnit.SECONDS, + EsExecutors.daemonThreadFactory("test"), + threadPool.getThreadContext() + ); try { checkExecutionError(getExecuteRunner(scalingExecutor)); checkExecutionError(getSubmitRunner(scalingExecutor)); @@ -85,7 +98,8 @@ public void testExecutionErrorOnSinglePrioritizingThreadPoolExecutor() throws In EsExecutors.daemonThreadFactory("test"), threadPool.getThreadContext(), threadPool.scheduler(), - PrioritizedEsThreadPoolExecutor.StarvationWatcher.NOOP_STARVATION_WATCHER); + PrioritizedEsThreadPoolExecutor.StarvationWatcher.NOOP_STARVATION_WATCHER + ); try { checkExecutionError(getExecuteRunner(prioritizedExecutor)); checkExecutionError(getSubmitRunner(prioritizedExecutor)); @@ -115,9 +129,7 @@ private void checkExecutionError(Consumer runner) throws InterruptedEx logger.info("checking error for {}", runner); final Runnable runnable; if (randomBoolean()) { - runnable = () -> { - throw new Error("future error"); - }; + runnable = () -> { throw new Error("future error"); }; } else { runnable = new AbstractRunnable() { @Override @@ -131,15 +143,11 @@ protected void doRun() { } }; } - runExecutionTest( - runner, - runnable, - true, - o -> { - assertTrue(o.isPresent()); - assertThat(o.get(), instanceOf(Error.class)); - assertThat(o.get(), hasToString(containsString("future error"))); - }); + runExecutionTest(runner, runnable, true, o -> { + assertTrue(o.isPresent()); + assertThat(o.get(), instanceOf(Error.class)); + assertThat(o.get(), hasToString(containsString("future error"))); + }); } public void testExecutionExceptionOnDefaultThreadPoolTypes() throws InterruptedException { @@ -159,8 +167,14 @@ public void testExecutionExceptionOnDirectExecutorService() throws InterruptedEx } public void testExecutionExceptionOnFixedESThreadPoolExecutor() throws InterruptedException { - final EsThreadPoolExecutor fixedExecutor = EsExecutors.newFixed("test", 1, 1, - EsExecutors.daemonThreadFactory("test"), threadPool.getThreadContext(), randomBoolean()); + final EsThreadPoolExecutor fixedExecutor = EsExecutors.newFixed( + "test", + 1, + 1, + EsExecutors.daemonThreadFactory("test"), + threadPool.getThreadContext(), + randomBoolean() + ); try { checkExecutionException(getExecuteRunner(fixedExecutor), true); checkExecutionException(getSubmitRunner(fixedExecutor), false); @@ -170,8 +184,15 @@ public void testExecutionExceptionOnFixedESThreadPoolExecutor() throws Interrupt } public void testExecutionExceptionOnScalingESThreadPoolExecutor() throws InterruptedException { - final EsThreadPoolExecutor scalingExecutor = EsExecutors.newScaling("test", 1, 1, - 10, TimeUnit.SECONDS, EsExecutors.daemonThreadFactory("test"), threadPool.getThreadContext()); + final EsThreadPoolExecutor scalingExecutor = EsExecutors.newScaling( + "test", + 1, + 1, + 10, + TimeUnit.SECONDS, + EsExecutors.daemonThreadFactory("test"), + threadPool.getThreadContext() + ); try { checkExecutionException(getExecuteRunner(scalingExecutor), true); checkExecutionException(getSubmitRunner(scalingExecutor), false); @@ -186,7 +207,8 @@ public void testExecutionExceptionOnSinglePrioritizingThreadPoolExecutor() throw EsExecutors.daemonThreadFactory("test"), threadPool.getThreadContext(), threadPool.scheduler(), - PrioritizedEsThreadPoolExecutor.StarvationWatcher.NOOP_STARVATION_WATCHER); + PrioritizedEsThreadPoolExecutor.StarvationWatcher.NOOP_STARVATION_WATCHER + ); try { checkExecutionException(getExecuteRunner(prioritizedExecutor), true); checkExecutionException(getSubmitRunner(prioritizedExecutor), false); @@ -232,9 +254,7 @@ private void checkExecutionException(Consumer runner, boolean expectEx final boolean willThrow; if (randomBoolean()) { logger.info("checking direct exception for {}", runner); - runnable = () -> { - throw new IllegalStateException("future exception"); - }; + runnable = () -> { throw new IllegalStateException("future exception"); }; willThrow = expectException; } else { logger.info("checking abstract runnable exception for {}", runner); @@ -251,18 +271,14 @@ protected void doRun() { }; willThrow = false; } - runExecutionTest( - runner, - runnable, - willThrow, - o -> { - assertEquals(willThrow, o.isPresent()); - if (willThrow) { - if (o.get() instanceof Error) throw (Error) o.get(); - assertThat(o.get(), instanceOf(IllegalStateException.class)); - assertThat(o.get(), hasToString(containsString("future exception"))); - } - }); + runExecutionTest(runner, runnable, willThrow, o -> { + assertEquals(willThrow, o.isPresent()); + if (willThrow) { + if (o.get() instanceof Error) throw (Error) o.get(); + assertThat(o.get(), instanceOf(IllegalStateException.class)); + assertThat(o.get(), hasToString(containsString("future exception"))); + } + }); } Consumer getExecuteRunner(ExecutorService executor) { @@ -311,7 +327,8 @@ private void runExecutionTest( final Consumer runner, final Runnable runnable, final boolean expectThrowable, - final Consumer> consumer) throws InterruptedException { + final Consumer> consumer + ) throws InterruptedException { final AtomicReference throwableReference = new AtomicReference<>(); final Thread.UncaughtExceptionHandler uncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler(); final CountDownLatch uncaughtExceptionHandlerLatch = new CountDownLatch(1); diff --git a/qa/full-cluster-restart/src/test/java/org/elasticsearch/upgrades/FullClusterRestartIT.java b/qa/full-cluster-restart/src/test/java/org/elasticsearch/upgrades/FullClusterRestartIT.java index c3f4772a3201e..ed41c04931faa 100644 --- a/qa/full-cluster-restart/src/test/java/org/elasticsearch/upgrades/FullClusterRestartIT.java +++ b/qa/full-cluster-restart/src/test/java/org/elasticsearch/upgrades/FullClusterRestartIT.java @@ -19,9 +19,6 @@ import org.elasticsearch.cluster.metadata.MetadataIndexStateService; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.common.xcontent.support.XContentMapValues; import org.elasticsearch.core.Booleans; import org.elasticsearch.core.CheckedFunction; @@ -33,6 +30,9 @@ import org.elasticsearch.test.rest.ESRestTestCase; import org.elasticsearch.test.rest.yaml.ObjectPath; import org.elasticsearch.transport.Compression; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.json.JsonXContent; import org.junit.Before; import java.io.IOException; @@ -60,8 +60,8 @@ import static org.elasticsearch.cluster.metadata.IndexNameExpressionResolver.SYSTEM_INDEX_ENFORCEMENT_VERSION; import static org.elasticsearch.cluster.routing.UnassignedInfo.INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING; import static org.elasticsearch.cluster.routing.allocation.decider.MaxRetryAllocationDecider.SETTING_ALLOCATION_MAX_RETRY; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.transport.RemoteClusterService.REMOTE_CLUSTER_COMPRESS; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.greaterThan; @@ -131,19 +131,20 @@ public void testSearch() throws Exception { byte[] randomByteArray = new byte[16]; random().nextBytes(randomByteArray); indexRandomDocuments( - count, - true, - true, - randomBoolean(), - i -> JsonXContent.contentBuilder().startObject() - .field("string", randomAlphaOfLength(10)) - .field("int", randomInt(100)) - .field("float", randomFloat()) - // be sure to create a "proper" boolean (True, False) for the first document so that automapping is correct - .field("bool", i > 0 && randomBoolean()) - .field("field.with.dots", randomAlphaOfLength(10)) - .field("binary", Base64.getEncoder().encodeToString(randomByteArray)) - .endObject() + count, + true, + true, + randomBoolean(), + i -> JsonXContent.contentBuilder() + .startObject() + .field("string", randomAlphaOfLength(10)) + .field("int", randomInt(100)) + .field("float", randomFloat()) + // be sure to create a "proper" boolean (True, False) for the first document so that automapping is correct + .field("bool", i > 0 && randomBoolean()) + .field("field.with.dots", randomAlphaOfLength(10)) + .field("binary", Base64.getEncoder().encodeToString(randomByteArray)) + .endObject() ); refreshAllIndices(); } else { @@ -257,11 +258,11 @@ public void testSearchTimeSeriesMode() throws Exception { body.endObject(); request.setJsonEntity(Strings.toString(body)); Map response = entityAsMap(client().performRequest(request)); - assertMap(response, matchesMap().extraOk() - .entry( - "hits", - matchesMap().extraOk().entry("total", Map.of("value", numDocs, "relation", "eq"))) - .entry("aggregations", Map.of("check", Map.of("value", IntStream.range(0, 10).mapToObj(i -> "dim" + i).collect(toList())))) + assertMap( + response, + matchesMap().extraOk() + .entry("hits", matchesMap().extraOk().entry("total", Map.of("value", numDocs, "relation", "eq"))) + .entry("aggregations", Map.of("check", Map.of("value", IntStream.range(0, 10).mapToObj(i -> "dim" + i).collect(toList())))) ); } @@ -367,21 +368,32 @@ public void testClusterState() throws Exception { // Check some global properties: String numberOfShards = (String) XContentMapValues.extractValue( - "metadata.templates.template_1.settings.index.number_of_shards", clusterState); + "metadata.templates.template_1.settings.index.number_of_shards", + clusterState + ); assertEquals("1", numberOfShards); String numberOfReplicas = (String) XContentMapValues.extractValue( - "metadata.templates.template_1.settings.index.number_of_replicas", clusterState); + "metadata.templates.template_1.settings.index.number_of_replicas", + clusterState + ); assertEquals("0", numberOfReplicas); // Check some index properties: - numberOfShards = (String) XContentMapValues.extractValue("metadata.indices." + index + - ".settings.index.number_of_shards", clusterState); + numberOfShards = (String) XContentMapValues.extractValue( + "metadata.indices." + index + ".settings.index.number_of_shards", + clusterState + ); assertEquals("1", numberOfShards); - numberOfReplicas = (String) XContentMapValues.extractValue("metadata.indices." + index + - ".settings.index.number_of_replicas", clusterState); + numberOfReplicas = (String) XContentMapValues.extractValue( + "metadata.indices." + index + ".settings.index.number_of_replicas", + clusterState + ); assertEquals("0", numberOfReplicas); - Version version = Version.fromId(Integer.valueOf((String) XContentMapValues.extractValue("metadata.indices." + index + - ".settings.index.version.created", clusterState))); + Version version = Version.fromId( + Integer.valueOf( + (String) XContentMapValues.extractValue("metadata.indices." + index + ".settings.index.version.created", clusterState) + ) + ); assertEquals(getOldClusterVersion(), version); } @@ -453,7 +465,7 @@ public void testShrink() throws IOException { int totalHits = extractTotalHits(response); assertEquals(numDocs, totalHits); - response = entityAsMap(client().performRequest(new Request("GET", "/" + shrunkenIndex+ "/_search"))); + response = entityAsMap(client().performRequest(new Request("GET", "/" + shrunkenIndex + "/_search"))); assertNoFailures(response); totalShards = (int) XContentMapValues.extractValue("_shards.total", response); assertEquals(1, totalShards); @@ -496,11 +508,11 @@ public void testShrinkAfterUpgrade() throws IOException { numDocs = randomIntBetween(512, 1024); indexRandomDocuments( - numDocs, - true, - true, - randomBoolean(), - i -> JsonXContent.contentBuilder().startObject().field("field", "value").endObject() + numDocs, + true, + true, + randomBoolean(), + i -> JsonXContent.contentBuilder().startObject().field("field", "value").endObject() ); } else { ensureGreen(index); // wait for source index to be available on both nodes before starting shrink @@ -554,11 +566,7 @@ public void testShrinkAfterUpgrade() throws IOException { public void testRollover() throws IOException { if (isRunningAgainstOldCluster()) { Request createIndex = new Request("PUT", "/" + index + "-000001"); - createIndex.setJsonEntity("{" - + " \"aliases\": {" - + " \"" + index + "_write\": {}" - + " }" - + "}"); + createIndex.setJsonEntity("{" + " \"aliases\": {" + " \"" + index + "_write\": {}" + " }" + "}"); client().performRequest(createIndex); } @@ -577,15 +585,13 @@ public void testRollover() throws IOException { if (isRunningAgainstOldCluster()) { Request rolloverRequest = new Request("POST", "/" + index + "_write/_rollover"); - rolloverRequest.setJsonEntity("{" - + " \"conditions\": {" - + " \"max_docs\": 5" - + " }" - + "}"); + rolloverRequest.setJsonEntity("{" + " \"conditions\": {" + " \"max_docs\": 5" + " }" + "}"); client().performRequest(rolloverRequest); - assertThat(EntityUtils.toString(client().performRequest(new Request("GET", "/_cat/indices?v")).getEntity()), - containsString("testrollover-000002")); + assertThat( + EntityUtils.toString(client().performRequest(new Request("GET", "/_cat/indices?v")).getEntity()), + containsString("testrollover-000002") + ); } Request countRequest = new Request("POST", "/" + index + "-*/_search"); @@ -701,7 +707,7 @@ void assertRealtimeGetWorks() throws IOException { Request searchRequest = new Request("GET", "/" + index + "/_search"); searchRequest.setJsonEntity("{ \"query\": { \"match_all\" : {} }}"); Map searchResponse = entityAsMap(client().performRequest(searchRequest)); - Map hit = (Map) ((List)(XContentMapValues.extractValue("hits.hits", searchResponse))).get(0); + Map hit = (Map) ((List) (XContentMapValues.extractValue("hits.hits", searchResponse))).get(0); String docId = (String) hit.get("_id"); Request updateRequest = new Request("POST", "/" + index + "/_update/" + docId); @@ -768,7 +774,6 @@ public void testSingleDoc() throws IOException { client().performRequest(createDoc); } - Request request = new Request("GET", docLocation); assertThat(toStr(client().performRequest(request)), containsString(doc)); } @@ -794,7 +799,6 @@ public void testEmptyShard() throws IOException { ensureGreen(index); } - /** * Tests recovery of an index with or without a translog and the * statistics we gather about that. @@ -828,11 +832,11 @@ public void testRecovery() throws Exception { if (shouldHaveTranslog) { // Update a few documents so we are sure to have a translog indexRandomDocuments( - count / 10, - false, // flushing here would invalidate the whole thing - false, - true, - i -> jsonBuilder().startObject().field("field", "value").endObject() + count / 10, + false, // flushing here would invalidate the whole thing + false, + true, + i -> jsonBuilder().startObject().field("field", "value").endObject() ); } saveInfoDocument(index + "_should_have_translog", Boolean.toString(shouldHaveTranslog)); @@ -899,8 +903,11 @@ public void testRecovery() throws Exception { fail("expected version to be one of [" + currentLuceneVersion + "," + bwcLuceneVersion + "] but was " + line); } } - assertNotEquals("expected at least 1 current segment after translog recovery. segments:\n" + segmentsResponse, - 0, numCurrentVersion); + assertNotEquals( + "expected at least 1 current segment after translog recovery. segments:\n" + segmentsResponse, + 0, + numCurrentVersion + ); assertNotEquals("expected at least 1 old segment. segments:\n" + segmentsResponse, 0, numBwcVersion); } } @@ -942,17 +949,20 @@ public void testSnapshotRestore() throws IOException { // Stick a routing attribute into to cluster settings so we can see it after the restore Request addRoutingSettings = new Request("PUT", "/_cluster/settings"); addRoutingSettings.setJsonEntity( - "{\"persistent\": {\"cluster.routing.allocation.exclude.test_attr\": \"" + getOldClusterVersion() + "\"}}"); + "{\"persistent\": {\"cluster.routing.allocation.exclude.test_attr\": \"" + getOldClusterVersion() + "\"}}" + ); client().performRequest(addRoutingSettings); // Stick a template into the cluster so we can see it after the restore XContentBuilder templateBuilder = JsonXContent.contentBuilder().startObject(); templateBuilder.field("index_patterns", "evil_*"); // Don't confuse other tests by applying the template - templateBuilder.startObject("settings"); { + templateBuilder.startObject("settings"); + { templateBuilder.field("number_of_shards", 1); } templateBuilder.endObject(); - templateBuilder.startObject("mappings"); { + templateBuilder.startObject("mappings"); + { { templateBuilder.startObject("_source"); { @@ -962,11 +972,15 @@ public void testSnapshotRestore() throws IOException { } } templateBuilder.endObject(); - templateBuilder.startObject("aliases"); { + templateBuilder.startObject("aliases"); + { templateBuilder.startObject("alias1").endObject(); - templateBuilder.startObject("alias2"); { - templateBuilder.startObject("filter"); { - templateBuilder.startObject("term"); { + templateBuilder.startObject("alias2"); + { + templateBuilder.startObject("filter"); + { + templateBuilder.startObject("term"); + { templateBuilder.field("version", isRunningAgainstOldCluster() ? getOldClusterVersion() : Version.CURRENT); } templateBuilder.endObject(); @@ -984,9 +998,11 @@ public void testSnapshotRestore() throws IOException { if (isRunningAgainstOldCluster()) { // Create the repo - XContentBuilder repoConfig = JsonXContent.contentBuilder().startObject(); { + XContentBuilder repoConfig = JsonXContent.contentBuilder().startObject(); + { repoConfig.field("type", "fs"); - repoConfig.startObject("settings"); { + repoConfig.startObject("settings"); + { repoConfig.field("compress", randomBoolean()); repoConfig.field("location", System.getProperty("tests.path.repo")); } @@ -1042,8 +1058,11 @@ public void testHistoryUUIDIsAdded() throws Exception { if (globalHistoryUUID == null) { globalHistoryUUID = historyUUID; } else { - assertThat("history uuid mismatch on " + nodeId + " (primary: " + primary + ")", historyUUID, - equalTo(globalHistoryUUID)); + assertThat( + "history uuid mismatch on " + nodeId + " (primary: " + primary + ")", + historyUUID, + equalTo(globalHistoryUUID) + ); } } } @@ -1103,9 +1122,7 @@ public void testSoftDeletes() throws Exception { */ public void testClosedIndices() throws Exception { if (isRunningAgainstOldCluster()) { - createIndex(index, Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) - .build()); + createIndex(index, Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1).build()); ensureGreen(index); int numDocs = 0; @@ -1172,8 +1189,10 @@ private void assertClosedIndex(final String index, final boolean checkRoutingTab assertThat(nbShards, greaterThanOrEqualTo(1)); for (int i = 0; i < nbShards; i++) { - final Collection> shards = - (Collection>) XContentMapValues.extractValue("shards." + i, routingTable); + final Collection> shards = (Collection>) XContentMapValues.extractValue( + "shards." + i, + routingTable + ); assertThat(shards, notNullValue()); assertThat(shards.size(), equalTo(2)); for (Map shard : shards) { @@ -1241,7 +1260,7 @@ private void checkSnapshot(final String snapshotName, final int count, final Ver Request countAfterWriteRequest = new Request("GET", "/restored_" + index + "/_search"); countAfterWriteRequest.addParameter("size", "0"); Map countAfterResponse = entityAsMap(client().performRequest(countRequest)); - assertTotalHits(count+extras, countAfterResponse); + assertTotalHits(count + extras, countAfterResponse); // Clean up the index for the next iteration client().performRequest(new Request("DELETE", "/restored_*")); @@ -1250,8 +1269,8 @@ private void checkSnapshot(final String snapshotName, final int count, final Ver Request clusterSettingsRequest = new Request("GET", "/_cluster/settings"); clusterSettingsRequest.addParameter("flat_settings", "true"); Map clusterSettingsResponse = entityAsMap(client().performRequest(clusterSettingsRequest)); - @SuppressWarnings("unchecked") final Map persistentSettings = - (Map)clusterSettingsResponse.get("persistent"); + @SuppressWarnings("unchecked") + final Map persistentSettings = (Map) clusterSettingsResponse.get("persistent"); assertThat(persistentSettings.get("cluster.routing.allocation.exclude.test_attr"), equalTo(getOldClusterVersion().toString())); // Check that the template was restored successfully @@ -1264,7 +1283,6 @@ private void checkSnapshot(final String snapshotName, final int count, final Ver expectedTemplate.put("settings", singletonMap("index", singletonMap("number_of_shards", "1"))); expectedTemplate.put("mappings", singletonMap("_source", singletonMap("enabled", true))); - expectedTemplate.put("order", 0); Map aliases = new HashMap<>(); aliases.put("alias1", emptyMap()); @@ -1280,12 +1298,12 @@ private void checkSnapshot(final String snapshotName, final int count, final Ver } private void indexRandomDocuments( - final int count, - final boolean flushAllowed, - final boolean saveInfo, - final boolean specifyId, - final CheckedFunction docSupplier) - throws IOException { + final int count, + final boolean flushAllowed, + final boolean saveInfo, + final boolean specifyId, + final CheckedFunction docSupplier + ) throws IOException { logger.info("Indexing {} random documents", count); for (int i = 0; i < count; i++) { logger.debug("Indexing document [{}]", i); @@ -1393,7 +1411,8 @@ public void testPeerRecoveryRetentionLeases() throws Exception { */ public void testOperationBasedRecovery() throws Exception { if (isRunningAgainstOldCluster()) { - Settings.Builder settings = Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + Settings.Builder settings = Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1); if (minimumNodeVersion().before(Version.V_8_0_0) && randomBoolean()) { settings.put(IndexSettings.INDEX_SOFT_DELETES_SETTING.getKey(), randomBoolean()); @@ -1428,10 +1447,14 @@ public void testOperationBasedRecovery() throws Exception { */ public void testTurnOffTranslogRetentionAfterUpgraded() throws Exception { if (isRunningAgainstOldCluster()) { - createIndex(index, Settings.builder() - .put(IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), 1) - .put(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 1) - .put(IndexSettings.INDEX_SOFT_DELETES_SETTING.getKey(), true).build()); + createIndex( + index, + Settings.builder() + .put(IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), 1) + .put(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 1) + .put(IndexSettings.INDEX_SOFT_DELETES_SETTING.getKey(), true) + .build() + ); ensureGreen(index); int numDocs = randomIntBetween(10, 100); for (int i = 0; i < numDocs; i++) { @@ -1516,8 +1539,8 @@ public void testResize() throws Exception { @SuppressWarnings("unchecked") public void testSystemIndexMetadataIsUpgraded() throws Exception { - final String systemIndexWarning = "this request accesses system indices: [.tasks], but in a future major version, direct " + - "access to system indices will be prevented by default"; + final String systemIndexWarning = "this request accesses system indices: [.tasks], but in a future major version, direct " + + "access to system indices will be prevented by default"; if (isRunningAgainstOldCluster()) { // create index Request createTestIndex = new Request("PUT", "/test_index_old"); @@ -1526,21 +1549,21 @@ public void testSystemIndexMetadataIsUpgraded() throws Exception { Request bulk = new Request("POST", "/_bulk"); bulk.addParameter("refresh", "true"); - bulk.setJsonEntity("{\"index\": {\"_index\": \"test_index_old\"}}\n" + - "{\"f1\": \"v1\", \"f2\": \"v2\"}\n"); + bulk.setJsonEntity("{\"index\": {\"_index\": \"test_index_old\"}}\n" + "{\"f1\": \"v1\", \"f2\": \"v2\"}\n"); client().performRequest(bulk); // start a async reindex job Request reindex = new Request("POST", "/_reindex"); reindex.setJsonEntity( - "{\n" + - " \"source\":{\n" + - " \"index\":\"test_index_old\"\n" + - " },\n" + - " \"dest\":{\n" + - " \"index\":\"test_index_reindex\"\n" + - " }\n" + - "}"); + "{\n" + + " \"source\":{\n" + + " \"index\":\"test_index_old\"\n" + + " },\n" + + " \"dest\":{\n" + + " \"index\":\"test_index_reindex\"\n" + + " }\n" + + "}" + ); reindex.addParameter("wait_for_completion", "false"); Map response = entityAsMap(client().performRequest(reindex)); String taskId = (String) response.get("task"); @@ -1575,12 +1598,14 @@ public void testSystemIndexMetadataIsUpgraded() throws Exception { if (minimumNodeVersion().before(SYSTEM_INDEX_ENFORCEMENT_VERSION)) { // Create an alias to make sure it gets upgraded properly Request putAliasRequest = new Request("POST", "/_aliases"); - putAliasRequest.setJsonEntity("{\n" + - " \"actions\": [\n" + - " {\"add\": {\"index\": \".tasks\", \"alias\": \"test-system-alias\"}},\n" + - " {\"add\": {\"index\": \"test_index_reindex\", \"alias\": \"test-system-alias\"}}\n" + - " ]\n" + - "}"); + putAliasRequest.setJsonEntity( + "{\n" + + " \"actions\": [\n" + + " {\"add\": {\"index\": \".tasks\", \"alias\": \"test-system-alias\"}},\n" + + " {\"add\": {\"index\": \"test_index_reindex\", \"alias\": \"test-system-alias\"}}\n" + + " ]\n" + + "}" + ); putAliasRequest.setOptions(expectVersionSpecificWarnings(v -> { v.current(systemIndexWarning); v.compatible(systemIndexWarning); @@ -1745,8 +1770,10 @@ public void testForbidDisableSoftDeletesOnRestore() throws Exception { */ public void testTransportCompressionSetting() throws IOException { assumeTrue("the old transport.compress setting existed before 7.14", getOldClusterVersion().before(Version.V_7_14_0)); - assumeTrue("Early versions of 6.x do not have cluster.remote* prefixed settings", - getOldClusterVersion().onOrAfter(Version.V_7_14_0.minimumCompatibilityVersion())); + assumeTrue( + "Early versions of 6.x do not have cluster.remote* prefixed settings", + getOldClusterVersion().onOrAfter(Version.V_7_14_0.minimumCompatibilityVersion()) + ); if (isRunningAgainstOldCluster()) { final Request putSettingsRequest = new Request("PUT", "/_cluster/settings"); try (XContentBuilder builder = jsonBuilder()) { @@ -1769,9 +1796,7 @@ public void testTransportCompressionSetting() throws IOException { try (XContentParser parser = createParser(JsonXContent.jsonXContent, getSettingsResponse.getEntity().getContent())) { final ClusterGetSettingsResponse clusterGetSettingsResponse = ClusterGetSettingsResponse.fromXContent(parser); final Settings settings = clusterGetSettingsResponse.getPersistentSettings(); - assertThat( - REMOTE_CLUSTER_COMPRESS.getConcreteSettingForNamespace("foo").get(settings), - equalTo(Compression.Enabled.TRUE)); + assertThat(REMOTE_CLUSTER_COMPRESS.getConcreteSettingForNamespace("foo").get(settings), equalTo(Compression.Enabled.TRUE)); } } } diff --git a/qa/full-cluster-restart/src/test/java/org/elasticsearch/upgrades/QueryBuilderBWCIT.java b/qa/full-cluster-restart/src/test/java/org/elasticsearch/upgrades/QueryBuilderBWCIT.java index da17519d490ed..2235adeffdf63 100644 --- a/qa/full-cluster-restart/src/test/java/org/elasticsearch/upgrades/QueryBuilderBWCIT.java +++ b/qa/full-cluster-restart/src/test/java/org/elasticsearch/upgrades/QueryBuilderBWCIT.java @@ -18,9 +18,7 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.Fuzziness; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.ConstantScoreQueryBuilder; import org.elasticsearch.index.query.DisMaxQueryBuilder; @@ -35,6 +33,8 @@ import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder; import org.elasticsearch.index.query.functionscore.RandomScoreFunctionBuilder; import org.elasticsearch.search.SearchModule; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.json.JsonXContent; import java.io.ByteArrayInputStream; import java.io.IOException; @@ -86,14 +86,18 @@ public class QueryBuilderBWCIT extends AbstractFullClusterRestartTestCase { ); addCandidate("\"range\": { \"long_field\": {\"gte\": 1, \"lte\": 9}}", new RangeQueryBuilder("long_field").from(1).to(9)); addCandidate( - "\"bool\": { \"must_not\": [{\"match_all\": {}}], \"must\": [{\"match_all\": {}}], " + - "\"filter\": [{\"match_all\": {}}], \"should\": [{\"match_all\": {}}]}", - new BoolQueryBuilder().mustNot(new MatchAllQueryBuilder()).must(new MatchAllQueryBuilder()) - .filter(new MatchAllQueryBuilder()).should(new MatchAllQueryBuilder()) + "\"bool\": { \"must_not\": [{\"match_all\": {}}], \"must\": [{\"match_all\": {}}], " + + "\"filter\": [{\"match_all\": {}}], \"should\": [{\"match_all\": {}}]}", + new BoolQueryBuilder().mustNot(new MatchAllQueryBuilder()) + .must(new MatchAllQueryBuilder()) + .filter(new MatchAllQueryBuilder()) + .should(new MatchAllQueryBuilder()) ); addCandidate( "\"dis_max\": {\"queries\": [{\"match_all\": {}},{\"match_all\": {}},{\"match_all\": {}}], \"tie_breaker\": 0.01}", - new DisMaxQueryBuilder().add(new MatchAllQueryBuilder()).add(new MatchAllQueryBuilder()).add(new MatchAllQueryBuilder()) + new DisMaxQueryBuilder().add(new MatchAllQueryBuilder()) + .add(new MatchAllQueryBuilder()) + .add(new MatchAllQueryBuilder()) .tieBreaker(0.01f) ); addCandidate( @@ -101,34 +105,42 @@ public class QueryBuilderBWCIT extends AbstractFullClusterRestartTestCase { new ConstantScoreQueryBuilder(new MatchAllQueryBuilder()).boost(0.1f) ); addCandidate( - "\"function_score\": {\"query\": {\"match_all\": {}}," + - "\"functions\": [{\"random_score\": {}, \"filter\": {\"match_all\": {}}, \"weight\": 0.2}]}", - new FunctionScoreQueryBuilder(new MatchAllQueryBuilder(), new FunctionScoreQueryBuilder.FilterFunctionBuilder[]{ - new FunctionScoreQueryBuilder.FilterFunctionBuilder(new MatchAllQueryBuilder(), - new RandomScoreFunctionBuilder().setWeight(0.2f))}) + "\"function_score\": {\"query\": {\"match_all\": {}}," + + "\"functions\": [{\"random_score\": {}, \"filter\": {\"match_all\": {}}, \"weight\": 0.2}]}", + new FunctionScoreQueryBuilder( + new MatchAllQueryBuilder(), + new FunctionScoreQueryBuilder.FilterFunctionBuilder[] { + new FunctionScoreQueryBuilder.FilterFunctionBuilder( + new MatchAllQueryBuilder(), + new RandomScoreFunctionBuilder().setWeight(0.2f) + ) } + ) ); addCandidate( - "\"span_near\": {\"clauses\": [{ \"span_term\": { \"keyword_field\": \"value1\" }}, " + - "{ \"span_term\": { \"keyword_field\": \"value2\" }}]}", - new SpanNearQueryBuilder(new SpanTermQueryBuilder("keyword_field", "value1"), 0) - .addClause(new SpanTermQueryBuilder("keyword_field", "value2")) + "\"span_near\": {\"clauses\": [{ \"span_term\": { \"keyword_field\": \"value1\" }}, " + + "{ \"span_term\": { \"keyword_field\": \"value2\" }}]}", + new SpanNearQueryBuilder(new SpanTermQueryBuilder("keyword_field", "value1"), 0).addClause( + new SpanTermQueryBuilder("keyword_field", "value2") + ) ); addCandidate( - "\"span_near\": {\"clauses\": [{ \"span_term\": { \"keyword_field\": \"value1\" }}, " + - "{ \"span_term\": { \"keyword_field\": \"value2\" }}], \"slop\": 2}", - new SpanNearQueryBuilder(new SpanTermQueryBuilder("keyword_field", "value1"), 2) - .addClause(new SpanTermQueryBuilder("keyword_field", "value2")) + "\"span_near\": {\"clauses\": [{ \"span_term\": { \"keyword_field\": \"value1\" }}, " + + "{ \"span_term\": { \"keyword_field\": \"value2\" }}], \"slop\": 2}", + new SpanNearQueryBuilder(new SpanTermQueryBuilder("keyword_field", "value1"), 2).addClause( + new SpanTermQueryBuilder("keyword_field", "value2") + ) ); addCandidate( - "\"span_near\": {\"clauses\": [{ \"span_term\": { \"keyword_field\": \"value1\" }}, " + - "{ \"span_term\": { \"keyword_field\": \"value2\" }}], \"slop\": 2, \"in_order\": false}", - new SpanNearQueryBuilder(new SpanTermQueryBuilder("keyword_field", "value1"), 2) - .addClause(new SpanTermQueryBuilder("keyword_field", "value2")).inOrder(false) + "\"span_near\": {\"clauses\": [{ \"span_term\": { \"keyword_field\": \"value1\" }}, " + + "{ \"span_term\": { \"keyword_field\": \"value2\" }}], \"slop\": 2, \"in_order\": false}", + new SpanNearQueryBuilder(new SpanTermQueryBuilder("keyword_field", "value1"), 2).addClause( + new SpanTermQueryBuilder("keyword_field", "value2") + ).inOrder(false) ); } private static void addCandidate(String querySource, QueryBuilder expectedQb) { - CANDIDATES.add(new Object[]{"{\"query\": {" + querySource + "}}", expectedQb}); + CANDIDATES.add(new Object[] { "{\"query\": {" + querySource + "}}", expectedQb }); } public void testQueryBuilderBWC() throws Exception { @@ -176,17 +188,22 @@ public void testQueryBuilderBWC() throws Exception { assertEquals(201, rsp.getStatusLine().getStatusCode()); } } else { - NamedWriteableRegistry registry = new NamedWriteableRegistry(new SearchModule(Settings.EMPTY, - Collections.emptyList()).getNamedWriteables()); + NamedWriteableRegistry registry = new NamedWriteableRegistry( + new SearchModule(Settings.EMPTY, Collections.emptyList()).getNamedWriteables() + ); for (int i = 0; i < CANDIDATES.size(); i++) { QueryBuilder expectedQueryBuilder = (QueryBuilder) CANDIDATES.get(i)[1]; Request request = new Request("GET", "/" + index + "/_search"); - request.setJsonEntity("{\"query\": {\"ids\": {\"values\": [\"" + Integer.toString(i) + "\"]}}, " + - "\"docvalue_fields\": [{\"field\":\"query.query_builder_field\"}]}"); + request.setJsonEntity( + "{\"query\": {\"ids\": {\"values\": [\"" + + Integer.toString(i) + + "\"]}}, " + + "\"docvalue_fields\": [{\"field\":\"query.query_builder_field\"}]}" + ); Response rsp = client().performRequest(request); assertEquals(200, rsp.getStatusLine().getStatusCode()); - Map hitRsp = (Map) ((List) ((Map)toMap(rsp).get("hits")).get("hits")).get(0); + Map hitRsp = (Map) ((List) ((Map) toMap(rsp).get("hits")).get("hits")).get(0); String queryBuilderStr = (String) ((List) ((Map) hitRsp.get("fields")).get("query.query_builder_field")).get(0); byte[] qbSource = Base64.getDecoder().decode(queryBuilderStr); try (InputStream in = new ByteArrayInputStream(qbSource, 0, qbSource.length)) { diff --git a/qa/logging-config/src/test/java/org/elasticsearch/common/logging/ESJsonLayoutTests.java b/qa/logging-config/src/test/java/org/elasticsearch/common/logging/ESJsonLayoutTests.java index b4e15e7eafc98..e664de150b449 100644 --- a/qa/logging-config/src/test/java/org/elasticsearch/common/logging/ESJsonLayoutTests.java +++ b/qa/logging-config/src/test/java/org/elasticsearch/common/logging/ESJsonLayoutTests.java @@ -7,12 +7,10 @@ */ package org.elasticsearch.common.logging; - import org.elasticsearch.test.ESTestCase; import org.hamcrest.Matchers; import org.junit.BeforeClass; - public class ESJsonLayoutTests extends ESTestCase { @BeforeClass public static void initNodeName() { @@ -24,43 +22,48 @@ public void testEmptyType() { } public void testLayout() { - ESJsonLayout server = ESJsonLayout.newBuilder() - .setType("server") - .build(); + ESJsonLayout server = ESJsonLayout.newBuilder().setType("server").build(); String conversionPattern = server.getPatternLayout().getConversionPattern(); - assertThat(conversionPattern, Matchers.equalTo( - "{" + - "\"type\": \"server\", " + - "\"timestamp\": \"%d{yyyy-MM-dd'T'HH:mm:ss,SSSZZ}\", " + - "\"level\": \"%p\", " + - "\"component\": \"%c{1.}\", " + - "\"cluster.name\": \"${sys:es.logs.cluster_name}\", " + - "\"node.name\": \"%node_name\", " + - "\"message\": \"%notEmpty{%enc{%marker}{JSON} }%enc{%.-10000m}{JSON}\"" + - "%notEmpty{, %node_and_cluster_id }" + - "%notEmpty{, %CustomMapFields }" + - "%exceptionAsJson }" + System.lineSeparator())); + assertThat( + conversionPattern, + Matchers.equalTo( + "{" + + "\"type\": \"server\", " + + "\"timestamp\": \"%d{yyyy-MM-dd'T'HH:mm:ss,SSSZZ}\", " + + "\"level\": \"%p\", " + + "\"component\": \"%c{1.}\", " + + "\"cluster.name\": \"${sys:es.logs.cluster_name}\", " + + "\"node.name\": \"%node_name\", " + + "\"message\": \"%notEmpty{%enc{%marker}{JSON} }%enc{%.-10000m}{JSON}\"" + + "%notEmpty{, %node_and_cluster_id }" + + "%notEmpty{, %CustomMapFields }" + + "%exceptionAsJson }" + + System.lineSeparator() + ) + ); } public void testLayoutWithAdditionalFieldOverride() { - ESJsonLayout server = ESJsonLayout.newBuilder() - .setType("server") - .setOverrideFields("message") - .build(); + ESJsonLayout server = ESJsonLayout.newBuilder().setType("server").setOverrideFields("message").build(); String conversionPattern = server.getPatternLayout().getConversionPattern(); - //message field is removed as is expected to be provided by a field from a message - assertThat(conversionPattern, Matchers.equalTo( - "{" + - "\"type\": \"server\", " + - "\"timestamp\": \"%d{yyyy-MM-dd'T'HH:mm:ss,SSSZZ}\", " + - "\"level\": \"%p\", " + - "\"component\": \"%c{1.}\", " + - "\"cluster.name\": \"${sys:es.logs.cluster_name}\", " + - "\"node.name\": \"%node_name\"" + - "%notEmpty{, %node_and_cluster_id }" + - "%notEmpty{, %CustomMapFields }" + - "%exceptionAsJson }" + System.lineSeparator())); + // message field is removed as is expected to be provided by a field from a message + assertThat( + conversionPattern, + Matchers.equalTo( + "{" + + "\"type\": \"server\", " + + "\"timestamp\": \"%d{yyyy-MM-dd'T'HH:mm:ss,SSSZZ}\", " + + "\"level\": \"%p\", " + + "\"component\": \"%c{1.}\", " + + "\"cluster.name\": \"${sys:es.logs.cluster_name}\", " + + "\"node.name\": \"%node_name\"" + + "%notEmpty{, %node_and_cluster_id }" + + "%notEmpty{, %CustomMapFields }" + + "%exceptionAsJson }" + + System.lineSeparator() + ) + ); } } diff --git a/qa/logging-config/src/test/java/org/elasticsearch/common/logging/JsonLoggerTests.java b/qa/logging-config/src/test/java/org/elasticsearch/common/logging/JsonLoggerTests.java index d15edec2698f9..ec3367ba015dc 100644 --- a/qa/logging-config/src/test/java/org/elasticsearch/common/logging/JsonLoggerTests.java +++ b/qa/logging-config/src/test/java/org/elasticsearch/common/logging/JsonLoggerTests.java @@ -14,17 +14,17 @@ import org.apache.logging.log4j.core.LoggerContext; import org.apache.logging.log4j.core.config.Configurator; import org.elasticsearch.cli.UserException; -import org.elasticsearch.core.CheckedConsumer; -import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.core.RestApiVersion; -import org.elasticsearch.core.PathUtils; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.core.CheckedConsumer; +import org.elasticsearch.core.PathUtils; +import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.env.Environment; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.tasks.Task; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.ParseField; import org.hamcrest.FeatureMatcher; import org.hamcrest.Matcher; import org.hamcrest.Matchers; @@ -83,24 +83,28 @@ public void testDeprecationWarnMessage() throws IOException { testLogger.warn(DeprecationCategory.OTHER, "a key", "deprecated warn message1"); - final Path path = PathUtils.get(System.getProperty("es.logs.base_path"), - System.getProperty("es.logs.cluster_name") + "_deprecated.json"); + final Path path = PathUtils.get( + System.getProperty("es.logs.base_path"), + System.getProperty("es.logs.cluster_name") + "_deprecated.json" + ); try (Stream> stream = JsonLogsStream.mapStreamFrom(path)) { - List> jsonLogs = stream - .collect(Collectors.toList()); - - assertThat(jsonLogs, contains( - allOf( - hasEntry("log.level", "WARN"), - hasEntry("log.logger", "org.elasticsearch.deprecation.test"), - hasEntry("elasticsearch.event.category", "other"), - hasEntry("message", "deprecated warn message1") - )) + List> jsonLogs = stream.collect(Collectors.toList()); + + assertThat( + jsonLogs, + contains( + allOf( + hasEntry("log.level", "WARN"), + hasEntry("log.logger", "org.elasticsearch.deprecation.test"), + hasEntry("elasticsearch.event.category", "other"), + hasEntry("message", "deprecated warn message1") + ) + ) ); } - assertWarnings(true, new DeprecationWarning(Level.WARN, "deprecated warn message1")) ; + assertWarnings(true, new DeprecationWarning(Level.WARN, "deprecated warn message1")); } public void testDeprecatedMessageWithoutXOpaqueId() throws IOException { @@ -108,29 +112,33 @@ public void testDeprecatedMessageWithoutXOpaqueId() throws IOException { testLogger.critical(DeprecationCategory.OTHER, "a key", "deprecated message1"); - final Path path = PathUtils.get(System.getProperty("es.logs.base_path"), - System.getProperty("es.logs.cluster_name") + "_deprecated.json"); + final Path path = PathUtils.get( + System.getProperty("es.logs.base_path"), + System.getProperty("es.logs.cluster_name") + "_deprecated.json" + ); try (Stream> stream = JsonLogsStream.mapStreamFrom(path)) { - List> jsonLogs = stream - .collect(Collectors.toList()); - - assertThat(jsonLogs, contains( - allOf( - hasEntry("event.dataset", "deprecation.elasticsearch"), - hasEntry("log.level", "CRITICAL"), - hasEntry("log.logger", "org.elasticsearch.deprecation.test"), - hasEntry("elasticsearch.cluster.name", "elasticsearch"), - hasEntry("elasticsearch.node.name", "sample-name"), - hasEntry("message", "deprecated message1"), - hasEntry("data_stream.type", "logs"), - hasEntry("data_stream.dataset", "deprecation.elasticsearch"), - hasEntry("data_stream.namespace", "default"), - hasEntry("ecs.version", DeprecatedMessage.ECS_VERSION), - hasEntry(DeprecatedMessage.KEY_FIELD_NAME, "a key"), - not(hasKey(DeprecatedMessage.X_OPAQUE_ID_FIELD_NAME)), - hasEntry("elasticsearch.event.category", "other") - )) + List> jsonLogs = stream.collect(Collectors.toList()); + + assertThat( + jsonLogs, + contains( + allOf( + hasEntry("event.dataset", "deprecation.elasticsearch"), + hasEntry("log.level", "CRITICAL"), + hasEntry("log.logger", "org.elasticsearch.deprecation.test"), + hasEntry("elasticsearch.cluster.name", "elasticsearch"), + hasEntry("elasticsearch.node.name", "sample-name"), + hasEntry("message", "deprecated message1"), + hasEntry("data_stream.type", "logs"), + hasEntry("data_stream.dataset", "deprecation.elasticsearch"), + hasEntry("data_stream.namespace", "default"), + hasEntry("ecs.version", DeprecatedMessage.ECS_VERSION), + hasEntry(DeprecatedMessage.KEY_FIELD_NAME, "a key"), + not(hasKey(DeprecatedMessage.X_OPAQUE_ID_FIELD_NAME)), + hasEntry("elasticsearch.event.category", "other") + ) + ) ); } @@ -142,8 +150,8 @@ public void testCompatibleLog() throws Exception { threadContext.putHeader(Task.X_OPAQUE_ID, "someId"); threadContext.putHeader(Task.TRACE_ID, "someTraceId"); final DeprecationLogger testLogger = DeprecationLogger.getLogger("org.elasticsearch.test"); - testLogger.critical(DeprecationCategory.OTHER,"someKey", "deprecated message1") - .compatibleCritical("compatibleKey","compatible API message"); + testLogger.critical(DeprecationCategory.OTHER, "someKey", "deprecated message1") + .compatibleCritical("compatibleKey", "compatible API message"); final Path path = PathUtils.get( System.getProperty("es.logs.base_path"), @@ -208,8 +216,9 @@ public void testParseFieldEmittingDeprecatedLogs() throws Exception { ParseField deprecatedField2 = new ParseField("new_name", "deprecated_name2"); assertTrue(deprecatedField2.match("deprecated_name2", LoggingDeprecationHandler.INSTANCE)); - ParseField compatibleField = new ParseField("new_name", "compatible_deprecated_name") - .forRestApiVersion(RestApiVersion.equalTo(RestApiVersion.minimumSupported())); + ParseField compatibleField = new ParseField("new_name", "compatible_deprecated_name").forRestApiVersion( + RestApiVersion.equalTo(RestApiVersion.minimumSupported()) + ); assertTrue(compatibleField.match("compatible_deprecated_name", LoggingDeprecationHandler.INSTANCE)); final Path path = PathUtils.get( @@ -278,9 +287,11 @@ public void testParseFieldEmittingDeprecatedLogs() throws Exception { ); } - assertWarnings("Deprecated field [deprecated_name] used, expected [new_name] instead", + assertWarnings( + "Deprecated field [deprecated_name] used, expected [new_name] instead", "Deprecated field [deprecated_name2] used, expected [new_name] instead", - "Deprecated field [compatible_deprecated_name] used, expected [new_name] instead"); + "Deprecated field [compatible_deprecated_name] used, expected [new_name] instead" + ); }); } @@ -329,26 +340,25 @@ public void testBuildingMessage() throws IOException { final Logger testLogger = LogManager.getLogger("test"); - testLogger.info(new ESLogMessage("some message {} {}", "value0") - .argAndField("key1","value1") - .field("key2","value2")); + testLogger.info(new ESLogMessage("some message {} {}", "value0").argAndField("key1", "value1").field("key2", "value2")); - final Path path = PathUtils.get(System.getProperty("es.logs.base_path"), - System.getProperty("es.logs.cluster_name") + ".json"); + final Path path = PathUtils.get(System.getProperty("es.logs.base_path"), System.getProperty("es.logs.cluster_name") + ".json"); try (Stream> stream = JsonLogsStream.mapStreamFrom(path)) { - List> jsonLogs = stream - .collect(Collectors.toList()); - - assertThat(jsonLogs, contains( - allOf( - hasEntry("event.dataset", "elasticsearch.file"), - hasEntry("log.level", "INFO"), - hasEntry("log.logger", "test"), - hasEntry("elasticsearch.cluster.name", "elasticsearch"), - hasEntry("elasticsearch.node.name", "sample-name"), - hasEntry("message", "some message value0 value1"), - hasEntry("key1", "value1"), - hasEntry("key2", "value2")) + List> jsonLogs = stream.collect(Collectors.toList()); + + assertThat( + jsonLogs, + contains( + allOf( + hasEntry("event.dataset", "elasticsearch.file"), + hasEntry("log.level", "INFO"), + hasEntry("log.logger", "test"), + hasEntry("elasticsearch.cluster.name", "elasticsearch"), + hasEntry("elasticsearch.node.name", "sample-name"), + hasEntry("message", "some message value0 value1"), + hasEntry("key1", "value1"), + hasEntry("key2", "value2") + ) ) ); } @@ -357,26 +367,25 @@ public void testBuildingMessage() throws IOException { public void testCustomMessageWithMultipleFields() throws IOException { // If a field is defined to be overridden, it has to always be overridden in that appender. final Logger testLogger = LogManager.getLogger("test"); - testLogger.info(new ESLogMessage("some message") - .with("field1","value1") - .with("field2","value2")); + testLogger.info(new ESLogMessage("some message").with("field1", "value1").with("field2", "value2")); - final Path path = PathUtils.get(System.getProperty("es.logs.base_path"), - System.getProperty("es.logs.cluster_name") + ".json"); + final Path path = PathUtils.get(System.getProperty("es.logs.base_path"), System.getProperty("es.logs.cluster_name") + ".json"); try (Stream> stream = JsonLogsStream.mapStreamFrom(path)) { - List> jsonLogs = stream - .collect(Collectors.toList()); - - assertThat(jsonLogs, contains( - allOf( - hasEntry("event.dataset", "elasticsearch.file"), - hasEntry("log.level", "INFO"), - hasEntry("log.logger", "test"), - hasEntry("elasticsearch.cluster.name", "elasticsearch"), - hasEntry("elasticsearch.node.name", "sample-name"), - hasEntry("field1", "value1"), - hasEntry("field2", "value2"), - hasEntry("message", "some message")) + List> jsonLogs = stream.collect(Collectors.toList()); + + assertThat( + jsonLogs, + contains( + allOf( + hasEntry("event.dataset", "elasticsearch.file"), + hasEntry("log.level", "INFO"), + hasEntry("log.logger", "test"), + hasEntry("elasticsearch.cluster.name", "elasticsearch"), + hasEntry("elasticsearch.node.name", "sample-name"), + hasEntry("field1", "value1"), + hasEntry("field2", "value2"), + hasEntry("message", "some message") + ) ) ); } @@ -394,13 +403,16 @@ public void testJsonLayout() throws IOException { try (Stream stream = JsonLogsStream.from(path)) { List jsonLogs = collectLines(stream); - assertThat(jsonLogs, contains( - logLine("elasticsearch.file", Level.ERROR, "sample-name", "test", "This is an error message"), - logLine("elasticsearch.file", Level.WARN, "sample-name", "test", "This is a warning message"), - logLine("elasticsearch.file", Level.INFO, "sample-name", "test", "This is an info message"), - logLine("elasticsearch.file", Level.DEBUG, "sample-name", "test", "This is a debug message"), - logLine("elasticsearch.file", Level.TRACE, "sample-name", "test", "This is a trace message") - )); + assertThat( + jsonLogs, + contains( + logLine("elasticsearch.file", Level.ERROR, "sample-name", "test", "This is an error message"), + logLine("elasticsearch.file", Level.WARN, "sample-name", "test", "This is a warning message"), + logLine("elasticsearch.file", Level.INFO, "sample-name", "test", "This is an info message"), + logLine("elasticsearch.file", Level.DEBUG, "sample-name", "test", "This is a debug message"), + logLine("elasticsearch.file", Level.TRACE, "sample-name", "test", "This is a trace message") + ) + ); } } @@ -414,36 +426,58 @@ public void testPrefixLoggerInJson() throws IOException { final Path path = clusterLogsPath(); try (Stream stream = JsonLogsStream.from(path)) { List jsonLogs = collectLines(stream); - assertThat(jsonLogs, contains( - logLine("elasticsearch.file", Level.INFO, "sample-name", "prefix.shardIdLogger", - "This is an info message with a shardId", Map.of(JsonLogLine::getTags, List.of("[indexName][123]"))), - logLine("elasticsearch.file", Level.INFO, "sample-name", "prefix.prefixLogger", - "This is an info message with a prefix", Map.of(JsonLogLine::getTags, List.of("PREFIX"))) - )); + assertThat( + jsonLogs, + contains( + logLine( + "elasticsearch.file", + Level.INFO, + "sample-name", + "prefix.shardIdLogger", + "This is an info message with a shardId", + Map.of(JsonLogLine::getTags, List.of("[indexName][123]")) + ), + logLine( + "elasticsearch.file", + Level.INFO, + "sample-name", + "prefix.prefixLogger", + "This is an info message with a prefix", + Map.of(JsonLogLine::getTags, List.of("PREFIX")) + ) + ) + ); } } public void testJsonInMessage() throws IOException { final Logger testLogger = LogManager.getLogger("test"); - String json = "{" + LINE_SEPARATOR + - " \"terms\" : {" + LINE_SEPARATOR + - " \"user\" : [" + LINE_SEPARATOR + - " \"u1\"," + LINE_SEPARATOR + - " \"u2\"," + LINE_SEPARATOR + - " \"u3\"" + LINE_SEPARATOR + - " ]," + LINE_SEPARATOR + - " \"boost\" : 1.0" + LINE_SEPARATOR + - " }" + LINE_SEPARATOR + - "}"; + String json = "{" + + LINE_SEPARATOR + + " \"terms\" : {" + + LINE_SEPARATOR + + " \"user\" : [" + + LINE_SEPARATOR + + " \"u1\"," + + LINE_SEPARATOR + + " \"u2\"," + + LINE_SEPARATOR + + " \"u3\"" + + LINE_SEPARATOR + + " ]," + + LINE_SEPARATOR + + " \"boost\" : 1.0" + + LINE_SEPARATOR + + " }" + + LINE_SEPARATOR + + "}"; testLogger.info(json); final Path path = clusterLogsPath(); try (Stream stream = JsonLogsStream.from(path)) { List jsonLogs = collectLines(stream); - assertThat(jsonLogs, contains( - logLine("elasticsearch.file", Level.INFO, "sample-name", "test", json) - )); + assertThat(jsonLogs, contains(logLine("elasticsearch.file", Level.INFO, "sample-name", "test", json))); } } @@ -454,43 +488,58 @@ public void testStacktrace() throws IOException { final Path path = clusterLogsPath(); try (Stream stream = JsonLogsStream.from(path)) { List jsonLogs = collectLines(stream); - assertThat(jsonLogs, contains( - allOf( - logLine("elasticsearch.file", Level.ERROR, "sample-name", "test", "error message"), - stacktraceMatches("java.lang.Exception: exception message.*Caused by: java.lang.RuntimeException: cause message.*") + assertThat( + jsonLogs, + contains( + allOf( + logLine("elasticsearch.file", Level.ERROR, "sample-name", "test", "error message"), + stacktraceMatches("java.lang.Exception: exception message.*Caused by: java.lang.RuntimeException: cause message.*") + ) ) - )); + ); } } public void testJsonInStacktraceMessageIsNotSplitted() throws IOException { final Logger testLogger = LogManager.getLogger("test"); - String json = "{" + LINE_SEPARATOR + - " \"terms\" : {" + LINE_SEPARATOR + - " \"user\" : [" + LINE_SEPARATOR + - " \"u1\"," + LINE_SEPARATOR + - " \"u2\"," + LINE_SEPARATOR + - " \"u3\"" + LINE_SEPARATOR + - " ]," + LINE_SEPARATOR + - " \"boost\" : 1.0" + LINE_SEPARATOR + - " }" + LINE_SEPARATOR + - "}"; + String json = "{" + + LINE_SEPARATOR + + " \"terms\" : {" + + LINE_SEPARATOR + + " \"user\" : [" + + LINE_SEPARATOR + + " \"u1\"," + + LINE_SEPARATOR + + " \"u2\"," + + LINE_SEPARATOR + + " \"u3\"" + + LINE_SEPARATOR + + " ]," + + LINE_SEPARATOR + + " \"boost\" : 1.0" + + LINE_SEPARATOR + + " }" + + LINE_SEPARATOR + + "}"; testLogger.error("error message " + json, new Exception(json)); final Path path = clusterLogsPath(); try (Stream stream = JsonLogsStream.from(path)) { List jsonLogs = collectLines(stream); - assertThat(jsonLogs, contains( - allOf( - //message field will have a single line with json escaped - logLine("elasticsearch.file", Level.ERROR, "sample-name", "test", "error message " + json), + assertThat( + jsonLogs, + contains( + allOf( + // message field will have a single line with json escaped + logLine("elasticsearch.file", Level.ERROR, "sample-name", "test", "error message " + json), - //stacktrace message will be single line - stacktraceWith("java.lang.Exception: " + json) + // stacktrace message will be single line + stacktraceWith("java.lang.Exception: " + json) + ) ) - )); + ); } } @@ -504,29 +553,33 @@ public void testDuplicateLogMessages() throws Exception { deprecationLogger.critical(DeprecationCategory.OTHER, "key", "message2"); assertWarnings("message1", "message2"); - final Path path = PathUtils.get(System.getProperty("es.logs.base_path"), - System.getProperty("es.logs.cluster_name") + "_deprecated.json"); + final Path path = PathUtils.get( + System.getProperty("es.logs.base_path"), + System.getProperty("es.logs.cluster_name") + "_deprecated.json" + ); try (Stream> stream = JsonLogsStream.mapStreamFrom(path)) { - List> jsonLogs = stream - .collect(Collectors.toList()); + List> jsonLogs = stream.collect(Collectors.toList()); - assertThat(jsonLogs, contains( - allOf( - hasEntry("event.dataset", "deprecation.elasticsearch"), - hasEntry("log.level", "CRITICAL"), - hasEntry("log.logger", "org.elasticsearch.deprecation.test"), - hasEntry("elasticsearch.cluster.name", "elasticsearch"), - hasEntry("elasticsearch.node.name", "sample-name"), - hasEntry("message", "message1"), - hasEntry(DeprecatedMessage.X_OPAQUE_ID_FIELD_NAME, "ID1"), - hasEntry("elasticsearch.event.category", "other")) + assertThat( + jsonLogs, + contains( + allOf( + hasEntry("event.dataset", "deprecation.elasticsearch"), + hasEntry("log.level", "CRITICAL"), + hasEntry("log.logger", "org.elasticsearch.deprecation.test"), + hasEntry("elasticsearch.cluster.name", "elasticsearch"), + hasEntry("elasticsearch.node.name", "sample-name"), + hasEntry("message", "message1"), + hasEntry(DeprecatedMessage.X_OPAQUE_ID_FIELD_NAME, "ID1"), + hasEntry("elasticsearch.event.category", "other") + ) ) ); } }); // For the same key and different X-Opaque-ID should be multiple times per key/x-opaque-id - //continuing with message1-ID1 in logs already, adding a new deprecation log line with message2-ID2 + // continuing with message1-ID1 in logs already, adding a new deprecation log line with message2-ID2 withThreadContext(threadContext -> { threadContext.putHeader(Task.X_OPAQUE_ID, "ID2"); deprecationLogger.critical(DeprecationCategory.OTHER, "key", "message1"); @@ -570,8 +623,7 @@ public void testDuplicateLogMessages() throws Exception { } private List collectLines(Stream stream) { - return stream - .collect(Collectors.toList()); + return stream.collect(Collectors.toList()); } private Path clusterLogsPath() { @@ -586,9 +638,9 @@ private void setupLogging(final String config, final Settings settings) throws I assertFalse("Environment path.home variable should not be set", Environment.PATH_HOME_SETTING.exists(settings)); final Path configDir = getDataPath(config); final Settings mergedSettings = Settings.builder() - .put(settings) - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .build(); + .put(settings) + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) + .build(); // need to use custom config path so we can use a custom log4j2.properties file for the test final Environment environment = new Environment(mergedSettings, configDir); LogConfigurator.configure(environment); @@ -599,36 +651,56 @@ private Matcher logLine(String type, Level level, String nodeName, } private Map, Object> mapOfParamsToCheck( - String type, Level level, String nodeName, String component, String message) { - return Map.of(JsonLogLine::getDataset, type, - JsonLogLine::getLevel, level.toString(), - JsonLogLine::getNodeName, nodeName, - JsonLogLine::getComponent, component, - JsonLogLine::getMessage, message); + String type, + Level level, + String nodeName, + String component, + String message + ) { + return Map.of( + JsonLogLine::getDataset, + type, + JsonLogLine::getLevel, + level.toString(), + JsonLogLine::getNodeName, + nodeName, + JsonLogLine::getComponent, + component, + JsonLogLine::getMessage, + message + ); } - private Matcher logLine(String type, Level level, String nodeName, String component, String message, - Map, Object> additionalProperties) { + private Matcher logLine( + String type, + Level level, + String nodeName, + String component, + String message, + Map, Object> additionalProperties + ) { Map, Object> map = new HashMap<>(); map.putAll(mapOfParamsToCheck(type, level, nodeName, component, message)); map.putAll(additionalProperties); return logLine(map); } - private Matcher logLine(Map, Object> map) { + private Matcher logLine(Map, Object> map) { return new FeatureMatcher(Matchers.is(true), "logLine", "logLine") { @Override protected Boolean featureValueOf(JsonLogLine actual) { - return map.entrySet() - .stream() - .allMatch(entry -> Objects.equals(entry.getKey().apply(actual), entry.getValue())); + return map.entrySet().stream().allMatch(entry -> Objects.equals(entry.getKey().apply(actual), entry.getValue())); } }; } + private Matcher stacktraceWith(String line) { - return new FeatureMatcher>(hasItems(Matchers.containsString(line)), - "error.stack_trace", "error.stack_trace") { + return new FeatureMatcher>( + hasItems(Matchers.containsString(line)), + "error.stack_trace", + "error.stack_trace" + ) { @Override protected List featureValueOf(JsonLogLine actual) { @@ -638,8 +710,11 @@ protected List featureValueOf(JsonLogLine actual) { } private Matcher stacktraceMatches(String regexp) { - return new FeatureMatcher>(hasItems(matchesRegex(Pattern.compile(regexp, Pattern.DOTALL))), - "error.stack_trace", "error.stack_trace") { + return new FeatureMatcher>( + hasItems(matchesRegex(Pattern.compile(regexp, Pattern.DOTALL))), + "error.stack_trace", + "error.stack_trace" + ) { @Override protected List featureValueOf(JsonLogLine actual) { diff --git a/qa/mixed-cluster/src/test/java/org/elasticsearch/backwards/HotThreadsIT.java b/qa/mixed-cluster/src/test/java/org/elasticsearch/backwards/HotThreadsIT.java index 9c050bb4d69a1..b2db1953240ec 100644 --- a/qa/mixed-cluster/src/test/java/org/elasticsearch/backwards/HotThreadsIT.java +++ b/qa/mixed-cluster/src/test/java/org/elasticsearch/backwards/HotThreadsIT.java @@ -21,8 +21,10 @@ public void testHotThreads() throws Exception { final IndexingIT.Nodes nodes = IndexingIT.buildNodeAndVersions(client()); assumeFalse("no new node found", nodes.getNewNodes().isEmpty()); assumeFalse("no bwc node found", nodes.getBWCNodes().isEmpty()); - assumeTrue("new nodes are higher version than BWC nodes", - nodes.getNewNodes().get(0).getVersion().compareTo(nodes.getBWCNodes().get(0).getVersion()) > 0); + assumeTrue( + "new nodes are higher version than BWC nodes", + nodes.getNewNodes().get(0).getVersion().compareTo(nodes.getBWCNodes().get(0).getVersion()) > 0 + ); final Request request = new Request("GET", "/_nodes/hot_threads"); final Response response = client().performRequest(request); final String responseString = EntityUtils.toString(response.getEntity()); diff --git a/qa/mixed-cluster/src/test/java/org/elasticsearch/backwards/IndexingIT.java b/qa/mixed-cluster/src/test/java/org/elasticsearch/backwards/IndexingIT.java index 318e696480059..9545075e7bd74 100644 --- a/qa/mixed-cluster/src/test/java/org/elasticsearch/backwards/IndexingIT.java +++ b/qa/mixed-cluster/src/test/java/org/elasticsearch/backwards/IndexingIT.java @@ -17,15 +17,15 @@ import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.MediaType; -import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.common.xcontent.support.XContentMapValues; import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.index.seqno.SeqNoStats; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.test.rest.ESRestTestCase; import org.elasticsearch.test.rest.yaml.ObjectPath; +import org.elasticsearch.xcontent.MediaType; +import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.xcontent.json.JsonXContent; import java.io.IOException; import java.util.ArrayList; @@ -82,15 +82,19 @@ public void testIndexVersionPropagation() throws Exception { final List bwcNamesList = nodes.getBWCNodes().stream().map(Node::getNodeName).collect(Collectors.toList()); final String bwcNames = bwcNamesList.stream().collect(Collectors.joining(",")); Settings.Builder settings = Settings.builder() - .put(IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), 1) - .put(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 2) - .put("index.routing.allocation.include._name", bwcNames); + .put(IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), 1) + .put(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 2) + .put("index.routing.allocation.include._name", bwcNames); final String index = "indexversionprop"; final int minUpdates = 5; final int maxUpdates = 10; createIndex(index, settings.build()); - try (RestClient newNodeClient = buildClient(restClientSettings(), - nodes.getNewNodes().stream().map(Node::getPublishAddress).toArray(HttpHost[]::new))) { + try ( + RestClient newNodeClient = buildClient( + restClientSettings(), + nodes.getNewNodes().stream().map(Node::getPublishAddress).toArray(HttpHost[]::new) + ) + ) { int nUpdates = randomIntBetween(minUpdates, maxUpdates); logger.info("indexing docs with [{}] concurrent updates initially", nUpdates); @@ -174,8 +178,12 @@ public void testSeqNoCheckpoints() throws Exception { final String index = "test"; createIndex(index, settings.build()); - try (RestClient newNodeClient = buildClient(restClientSettings(), - nodes.getNewNodes().stream().map(Node::getPublishAddress).toArray(HttpHost[]::new))) { + try ( + RestClient newNodeClient = buildClient( + restClientSettings(), + nodes.getNewNodes().stream().map(Node::getPublishAddress).toArray(HttpHost[]::new) + ) + ) { int numDocs = 0; final int numberOfInitialDocs = 1 + randomInt(5); logger.info("indexing [{}] docs initially", numberOfInitialDocs); @@ -231,15 +239,18 @@ public void testUpdateSnapshotStatus() throws Exception { // Create the repository before taking the snapshot. Request request = new Request("PUT", "/_snapshot/repo"); - request.setJsonEntity(Strings - .toString(JsonXContent.contentBuilder() - .startObject() + request.setJsonEntity( + Strings.toString( + JsonXContent.contentBuilder() + .startObject() .field("type", "fs") .startObject("settings") - .field("compress", randomBoolean()) - .field("location", System.getProperty("tests.path.repo")) + .field("compress", randomBoolean()) + .field("location", System.getProperty("tests.path.repo")) + .endObject() .endObject() - .endObject())); + ) + ); assertOK(client().performRequest(request)); @@ -283,23 +294,35 @@ public void testSyncedFlushTransition() throws Exception { int numOfReplicas = randomIntBetween(0, nodes.getNewNodes().size() - 1); int totalShards = numShards * (numOfReplicas + 1); final String index = "test_synced_flush"; - createIndex(index, Settings.builder() - .put(IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), numShards) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, numOfReplicas) - .put("index.routing.allocation.include._name", newNodes).build()); + createIndex( + index, + Settings.builder() + .put(IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), numShards) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, numOfReplicas) + .put("index.routing.allocation.include._name", newNodes) + .build() + ); ensureGreen(index); indexDocs(index, randomIntBetween(0, 100), between(1, 100)); - try (RestClient oldNodeClient = buildClient(restClientSettings(), - nodes.getBWCNodes().stream().map(Node::getPublishAddress).toArray(HttpHost[]::new))) { + try ( + RestClient oldNodeClient = buildClient( + restClientSettings(), + nodes.getBWCNodes().stream().map(Node::getPublishAddress).toArray(HttpHost[]::new) + ) + ) { Request request = new Request("POST", index + "/_flush/synced"); assertBusy(() -> { ResponseException responseException = expectThrows(ResponseException.class, () -> oldNodeClient.performRequest(request)); assertThat(responseException.getResponse().getStatusLine().getStatusCode(), equalTo(RestStatus.CONFLICT.getStatus())); - assertThat(responseException.getResponse().getWarnings(), + assertThat( + responseException.getResponse().getWarnings(), contains( - oneOf("Synced flush is deprecated and will be removed in 8.0. Use flush at _/flush or /{index}/_flush instead.", - "Synced flush is deprecated and will be removed in 8.0. Use flush at /_flush or /{index}/_flush instead.") - )); + oneOf( + "Synced flush is deprecated and will be removed in 8.0. Use flush at _/flush or /{index}/_flush instead.", + "Synced flush is deprecated and will be removed in 8.0. Use flush at /_flush or /{index}/_flush instead." + ) + ) + ); Map result = ObjectPath.createFromResponse(responseException.getResponse()).evaluate("_shards"); assertThat(result.get("total"), equalTo(totalShards)); assertThat(result.get("successful"), equalTo(0)); @@ -309,17 +332,25 @@ public void testSyncedFlushTransition() throws Exception { assertThat(XContentMapValues.extractValue("indices." + index + ".total.translog.uncommitted_operations", stats), equalTo(0)); } indexDocs(index, randomIntBetween(0, 100), between(1, 100)); - try (RestClient newNodeClient = buildClient(restClientSettings(), - nodes.getNewNodes().stream().map(Node::getPublishAddress).toArray(HttpHost[]::new))) { + try ( + RestClient newNodeClient = buildClient( + restClientSettings(), + nodes.getNewNodes().stream().map(Node::getPublishAddress).toArray(HttpHost[]::new) + ) + ) { Request request = new Request("POST", index + "/_flush/synced"); final String v7MediaType = XContentType.VND_JSON.toParsedMediaType() - .responseContentTypeHeader(Map.of(MediaType.COMPATIBLE_WITH_PARAMETER_NAME, - String.valueOf(RestApiVersion.minimumSupported().major))); - List warningMsg = List.of("Synced flush is deprecated and will be removed in 8.0." + - " Use flush at /_flush or /{index}/_flush instead."); - request.setOptions(RequestOptions.DEFAULT.toBuilder() - .setWarningsHandler(warnings -> warnings.equals(warningMsg) == false) - .addHeader("Accept", v7MediaType)); + .responseContentTypeHeader( + Map.of(MediaType.COMPATIBLE_WITH_PARAMETER_NAME, String.valueOf(RestApiVersion.minimumSupported().major)) + ); + List warningMsg = List.of( + "Synced flush is deprecated and will be removed in 8.0." + " Use flush at /_flush or /{index}/_flush instead." + ); + request.setOptions( + RequestOptions.DEFAULT.toBuilder() + .setWarningsHandler(warnings -> warnings.equals(warningMsg) == false) + .addHeader("Accept", v7MediaType) + ); assertBusy(() -> { Map result = ObjectPath.createFromResponse(newNodeClient.performRequest(request)).evaluate("_shards"); @@ -342,14 +373,22 @@ public void testFlushTransition() throws Exception { int numOfReplicas = randomIntBetween(0, nodes.getNewNodes().size() - 1); int totalShards = numShards * (numOfReplicas + 1); final String index = "test_flush"; - createIndex(index, Settings.builder() - .put(IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), numShards) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, numOfReplicas) - .put("index.routing.allocation.include._name", newNodes).build()); + createIndex( + index, + Settings.builder() + .put(IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), numShards) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, numOfReplicas) + .put("index.routing.allocation.include._name", newNodes) + .build() + ); ensureGreen(index); indexDocs(index, randomIntBetween(0, 100), between(1, 100)); - try (RestClient oldNodeClient = buildClient(restClientSettings(), - nodes.getBWCNodes().stream().map(Node::getPublishAddress).toArray(HttpHost[]::new))) { + try ( + RestClient oldNodeClient = buildClient( + restClientSettings(), + nodes.getBWCNodes().stream().map(Node::getPublishAddress).toArray(HttpHost[]::new) + ) + ) { Request request = new Request("POST", index + "/_flush"); assertBusy(() -> { Map result = ObjectPath.createFromResponse(oldNodeClient.performRequest(request)).evaluate("_shards"); @@ -361,8 +400,12 @@ public void testFlushTransition() throws Exception { assertThat(XContentMapValues.extractValue("indices." + index + ".total.translog.uncommitted_operations", stats), equalTo(0)); } indexDocs(index, randomIntBetween(0, 100), between(1, 100)); - try (RestClient newNodeClient = buildClient(restClientSettings(), - nodes.getNewNodes().stream().map(Node::getPublishAddress).toArray(HttpHost[]::new))) { + try ( + RestClient newNodeClient = buildClient( + restClientSettings(), + nodes.getNewNodes().stream().map(Node::getPublishAddress).toArray(HttpHost[]::new) + ) + ) { Request request = new Request("POST", index + "/_flush"); assertBusy(() -> { Map result = ObjectPath.createFromResponse(newNodeClient.performRequest(request)).evaluate("_shards"); @@ -394,8 +437,7 @@ private void assertVersion(final String index, final int docId, final String pre assertThat("version mismatch for doc [" + docId + "] preference [" + preference + "]", actualVersion, equalTo(expectedVersion)); } - private void assertSeqNoOnShards(String index, Nodes nodes, int numDocs, RestClient client) - throws Exception { + private void assertSeqNoOnShards(String index, Nodes nodes, int numDocs, RestClient client) throws Exception { assertBusy(() -> { try { List shards = buildShards(index, nodes, client); @@ -408,10 +450,16 @@ private void assertSeqNoOnShards(String index, Nodes nodes, int numDocs, RestCli final SeqNoStats seqNoStats = shard.getSeqNoStats(); logger.info("stats for {}, primary [{}]: [{}]", shard.getNode(), shard.isPrimary(), seqNoStats); assertThat("max_seq no on " + shard.getNode() + " is wrong", seqNoStats.getMaxSeqNo(), equalTo(expectMaxSeqNo)); - assertThat("localCheckpoint no on " + shard.getNode() + " is wrong", - seqNoStats.getLocalCheckpoint(), equalTo(expectMaxSeqNo)); - assertThat("globalCheckpoint no on " + shard.getNode() + " is wrong", - seqNoStats.getGlobalCheckpoint(), equalTo(expectedGlobalCkp)); + assertThat( + "localCheckpoint no on " + shard.getNode() + " is wrong", + seqNoStats.getLocalCheckpoint(), + equalTo(expectMaxSeqNo) + ); + assertThat( + "globalCheckpoint no on " + shard.getNode() + " is wrong", + seqNoStats.getGlobalCheckpoint(), + equalTo(expectedGlobalCkp) + ); } } catch (IOException e) { throw new AssertionError("unexpected io exception", e); @@ -450,11 +498,14 @@ static Nodes buildNodeAndVersions(RestClient client) throws IOException { Map nodesAsMap = objectPath.evaluate("nodes"); Nodes nodes = new Nodes(); for (String id : nodesAsMap.keySet()) { - nodes.add(new Node( - id, - objectPath.evaluate("nodes." + id + ".name"), - Version.fromString(objectPath.evaluate("nodes." + id + ".version")), - HttpHost.create(objectPath.evaluate("nodes." + id + ".http.publish_address")))); + nodes.add( + new Node( + id, + objectPath.evaluate("nodes." + id + ".name"), + Version.fromString(objectPath.evaluate("nodes." + id + ".version")), + HttpHost.create(objectPath.evaluate("nodes." + id + ".http.publish_address")) + ) + ); } response = client.performRequest(new Request("GET", "_cluster/state")); nodes.setMasterNodeId(ObjectPath.createFromResponse(response).evaluate("master_node")); @@ -507,10 +558,12 @@ public Node getSafe(String id) { @Override public String toString() { - return "Nodes{" + - "masterNodeId='" + masterNodeId + "'\n" + - values().stream().map(Node::toString).collect(Collectors.joining("\n")) + - '}'; + return "Nodes{" + + "masterNodeId='" + + masterNodeId + + "'\n" + + values().stream().map(Node::toString).collect(Collectors.joining("\n")) + + '}'; } } @@ -545,11 +598,7 @@ public Version getVersion() { @Override public String toString() { - return "Node{" + - "id='" + id + '\'' + - ", nodeName='" + nodeName + '\'' + - ", version=" + version + - '}'; + return "Node{" + "id='" + id + '\'' + ", nodeName='" + nodeName + '\'' + ", version=" + version + '}'; } } @@ -578,11 +627,7 @@ public SeqNoStats getSeqNoStats() { @Override public String toString() { - return "Shard{" + - "node=" + node + - ", Primary=" + Primary + - ", seqNoStats=" + seqNoStats + - '}'; + return "Shard{" + "node=" + node + ", Primary=" + Primary + ", seqNoStats=" + seqNoStats + '}'; } } } diff --git a/qa/mixed-cluster/src/test/java/org/elasticsearch/backwards/MixedClusterClientYamlTestSuiteIT.java b/qa/mixed-cluster/src/test/java/org/elasticsearch/backwards/MixedClusterClientYamlTestSuiteIT.java index d3dbaf0156b1d..092cc6109c0ff 100644 --- a/qa/mixed-cluster/src/test/java/org/elasticsearch/backwards/MixedClusterClientYamlTestSuiteIT.java +++ b/qa/mixed-cluster/src/test/java/org/elasticsearch/backwards/MixedClusterClientYamlTestSuiteIT.java @@ -10,6 +10,7 @@ import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; import com.carrotsearch.randomizedtesting.annotations.TimeoutSuite; + import org.apache.lucene.util.TimeUnits; import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate; import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase; @@ -31,4 +32,3 @@ protected boolean randomizeContentType() { return false; } } - diff --git a/qa/mixed-cluster/src/test/java/org/elasticsearch/backwards/RareTermsIT.java b/qa/mixed-cluster/src/test/java/org/elasticsearch/backwards/RareTermsIT.java index 705caa526a7d9..86e85b98dbe37 100644 --- a/qa/mixed-cluster/src/test/java/org/elasticsearch/backwards/RareTermsIT.java +++ b/qa/mixed-cluster/src/test/java/org/elasticsearch/backwards/RareTermsIT.java @@ -26,7 +26,7 @@ public class RareTermsIT extends ESRestTestCase { private static final String index = "idx"; - private int indexDocs(int numDocs, int id) throws Exception { + private int indexDocs(int numDocs, int id) throws Exception { final Request request = new Request("POST", "/_bulk"); final StringBuilder builder = new StringBuilder(); for (int i = 0; i < numDocs; ++i) { diff --git a/qa/mixed-cluster/src/test/java/org/elasticsearch/backwards/SearchWithMinCompatibleSearchNodeIT.java b/qa/mixed-cluster/src/test/java/org/elasticsearch/backwards/SearchWithMinCompatibleSearchNodeIT.java index 3644cc39b4d0f..1908a2a473e96 100644 --- a/qa/mixed-cluster/src/test/java/org/elasticsearch/backwards/SearchWithMinCompatibleSearchNodeIT.java +++ b/qa/mixed-cluster/src/test/java/org/elasticsearch/backwards/SearchWithMinCompatibleSearchNodeIT.java @@ -16,8 +16,8 @@ import org.elasticsearch.client.ResponseException; import org.elasticsearch.client.RestClient; import org.elasticsearch.cluster.metadata.IndexMetadata; -import org.elasticsearch.core.CheckedRunnable; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.CheckedRunnable; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.test.rest.ESRestTestCase; import org.elasticsearch.test.rest.yaml.ObjectPath; @@ -54,9 +54,13 @@ public void prepareTestData() throws IOException { newVersion = nodes.getNewNodes().get(0).getVersion(); if (client().performRequest(new Request("HEAD", "/" + index)).getStatusLine().getStatusCode() == 404) { - createIndex(index, Settings.builder() - .put(IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), numShards) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, numReplicas).build()); + createIndex( + index, + Settings.builder() + .put(IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), numShards) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, numReplicas) + .build() + ); for (int i = 0; i < numDocs; i++) { Request request = new Request("PUT", index + "/_doc/" + i); request.setJsonEntity("{\"test\": \"test_" + randomAlphaOfLength(2) + "\"}"); @@ -67,27 +71,44 @@ public void prepareTestData() throws IOException { } public void testMinVersionAsNewVersion() throws Exception { - try (RestClient client = buildClient(restClientSettings(), - allNodes.stream().map(Node::getPublishAddress).toArray(HttpHost[]::new))) { - Request newVersionRequest = new Request("POST", - index + "/_search?min_compatible_shard_node=" + newVersion + "&ccs_minimize_roundtrips=false"); + try ( + RestClient client = buildClient(restClientSettings(), allNodes.stream().map(Node::getPublishAddress).toArray(HttpHost[]::new)) + ) { + Request newVersionRequest = new Request( + "POST", + index + "/_search?min_compatible_shard_node=" + newVersion + "&ccs_minimize_roundtrips=false" + ); assertBusy(() -> { ResponseException responseException = expectThrows(ResponseException.class, () -> client.performRequest(newVersionRequest)); - assertThat(responseException.getResponse().getStatusLine().getStatusCode(), - equalTo(RestStatus.INTERNAL_SERVER_ERROR.getStatus())); - assertThat(responseException.getMessage(), - containsString("{\"error\":{\"root_cause\":[],\"type\":\"search_phase_execution_exception\"")); - assertThat(responseException.getMessage(), containsString("caused_by\":{\"type\":\"version_mismatch_exception\"," - + "\"reason\":\"One of the shards is incompatible with the required minimum version [" + newVersion + "]\"")); + assertThat( + responseException.getResponse().getStatusLine().getStatusCode(), + equalTo(RestStatus.INTERNAL_SERVER_ERROR.getStatus()) + ); + assertThat( + responseException.getMessage(), + containsString("{\"error\":{\"root_cause\":[],\"type\":\"search_phase_execution_exception\"") + ); + assertThat( + responseException.getMessage(), + containsString( + "caused_by\":{\"type\":\"version_mismatch_exception\"," + + "\"reason\":\"One of the shards is incompatible with the required minimum version [" + + newVersion + + "]\"" + ) + ); }); } } public void testMinVersionAsOldVersion() throws Exception { - try (RestClient client = buildClient(restClientSettings(), - allNodes.stream().map(Node::getPublishAddress).toArray(HttpHost[]::new))) { - Request oldVersionRequest = new Request("POST", index + "/_search?min_compatible_shard_node=" + bwcVersion + - "&ccs_minimize_roundtrips=false"); + try ( + RestClient client = buildClient(restClientSettings(), allNodes.stream().map(Node::getPublishAddress).toArray(HttpHost[]::new)) + ) { + Request oldVersionRequest = new Request( + "POST", + index + "/_search?min_compatible_shard_node=" + bwcVersion + "&ccs_minimize_roundtrips=false" + ); oldVersionRequest.setJsonEntity("{\"query\":{\"match_all\":{}},\"_source\":false}"); assertBusy(() -> { assertWithBwcVersionCheck(() -> { @@ -106,21 +127,33 @@ public void testMinVersionAsOldVersion() throws Exception { } public void testCcsMinimizeRoundtripsIsFalse() throws Exception { - try (RestClient client = buildClient(restClientSettings(), - allNodes.stream().map(Node::getPublishAddress).toArray(HttpHost[]::new))) { + try ( + RestClient client = buildClient(restClientSettings(), allNodes.stream().map(Node::getPublishAddress).toArray(HttpHost[]::new)) + ) { Version version = randomBoolean() ? newVersion : bwcVersion; - Request request = new Request("POST", index + "/_search?min_compatible_shard_node=" + version - + "&ccs_minimize_roundtrips=true"); + Request request = new Request( + "POST", + index + "/_search?min_compatible_shard_node=" + version + "&ccs_minimize_roundtrips=true" + ); assertBusy(() -> { assertWithBwcVersionCheck(() -> { ResponseException responseException = expectThrows(ResponseException.class, () -> client.performRequest(request)); - assertThat(responseException.getResponse().getStatusLine().getStatusCode(), - equalTo(RestStatus.BAD_REQUEST.getStatus())); - assertThat(responseException.getMessage(), - containsString("{\"error\":{\"root_cause\":[{\"type\":\"action_request_validation_exception\"")); - assertThat(responseException.getMessage(), containsString("\"reason\":\"Validation Failed: 1: " - + "[ccs_minimize_roundtrips] cannot be [true] when setting a minimum compatible shard version;\"")); + assertThat( + responseException.getResponse().getStatusLine().getStatusCode(), + equalTo(RestStatus.BAD_REQUEST.getStatus()) + ); + assertThat( + responseException.getMessage(), + containsString("{\"error\":{\"root_cause\":[{\"type\":\"action_request_validation_exception\"") + ); + assertThat( + responseException.getMessage(), + containsString( + "\"reason\":\"Validation Failed: 1: " + + "[ccs_minimize_roundtrips] cannot be [true] when setting a minimum compatible shard version;\"" + ) + ); }, client, request); }); } @@ -130,8 +163,7 @@ private void assertWithBwcVersionCheck(CheckedRunnable code, RestClie if (bwcVersion.before(Version.V_7_12_0)) { // min_compatible_shard_node support doesn't exist in older versions and there will be an "unrecognized parameter" exception ResponseException exception = expectThrows(ResponseException.class, () -> client.performRequest(request)); - assertThat(exception.getResponse().getStatusLine().getStatusCode(), - equalTo(RestStatus.BAD_REQUEST.getStatus())); + assertThat(exception.getResponse().getStatusLine().getStatusCode(), equalTo(RestStatus.BAD_REQUEST.getStatus())); assertThat(exception.getMessage(), containsString("contains unrecognized parameter: [min_compatible_shard_node]")); } else { code.run(); diff --git a/qa/multi-cluster-search/src/test/java/org/elasticsearch/search/CCSDuelIT.java b/qa/multi-cluster-search/src/test/java/org/elasticsearch/search/CCSDuelIT.java index dc764b5f20dd3..9f2fb4d514da8 100644 --- a/qa/multi-cluster-search/src/test/java/org/elasticsearch/search/CCSDuelIT.java +++ b/qa/multi-cluster-search/src/test/java/org/elasticsearch/search/CCSDuelIT.java @@ -120,7 +120,7 @@ public class CCSDuelIT extends ESRestTestCase { private static final String INDEX_NAME = "ccs_duel_index"; private static final String REMOTE_INDEX_NAME = "my_remote_cluster:" + INDEX_NAME; - private static final String[] TAGS = new String[] {"java", "xml", "sql", "html", "php", "ruby", "python", "perl"}; + private static final String[] TAGS = new String[] { "java", "xml", "sql", "html", "php", "ruby", "python", "perl" }; private static RestHighLevelClient restHighLevelClient; @@ -130,10 +130,10 @@ public void init() throws Exception { if (restHighLevelClient == null) { restHighLevelClient = new HighLevelClient(client()); String destinationCluster = System.getProperty("tests.rest.suite"); - //we index docs with private randomness otherwise the two clusters end up with exactly the same documents - //given that this test class is run twice with same seed. - RandomizedContext.current().runWithPrivateRandomness(random().nextLong() + destinationCluster.hashCode(), - (Callable) () -> { + // we index docs with private randomness otherwise the two clusters end up with exactly the same documents + // given that this test class is run twice with same seed. + RandomizedContext.current() + .runWithPrivateRandomness(random().nextLong() + destinationCluster.hashCode(), (Callable) () -> { indexDocuments(destinationCluster + "-"); return null; }); @@ -163,7 +163,7 @@ protected boolean preserveDataStreamsUponCompletion() { } private static void indexDocuments(String idPrefix) throws IOException, InterruptedException { - //this index with a single document is used to test partial failures + // this index with a single document is used to test partial failures IndexRequest indexRequest = new IndexRequest(INDEX_NAME + "_err"); indexRequest.id("id"); indexRequest.source("id", "id", "creationDate", "err"); @@ -178,18 +178,21 @@ private static void indexDocuments(String idPrefix) throws IOException, Interrup int numShards = randomIntBetween(1, 5); CreateIndexRequest createIndexRequest = new CreateIndexRequest(INDEX_NAME); createIndexRequest.settings(Settings.builder().put("index.number_of_shards", numShards).put("index.number_of_replicas", 0)); - createIndexRequest.mapping("{\"properties\":{" + - "\"id\":{\"type\":\"keyword\"}," + - "\"suggest\":{\"type\":\"completion\"}," + - "\"join\":{\"type\":\"join\", \"relations\": {\"question\":\"answer\"}}}}", XContentType.JSON); + createIndexRequest.mapping( + "{\"properties\":{" + + "\"id\":{\"type\":\"keyword\"}," + + "\"suggest\":{\"type\":\"completion\"}," + + "\"join\":{\"type\":\"join\", \"relations\": {\"question\":\"answer\"}}}}", + XContentType.JSON + ); CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT); assertTrue(createIndexResponse.isAcknowledged()); - BulkProcessor bulkProcessor = BulkProcessor.builder((r, l) -> restHighLevelClient.bulkAsync(r, RequestOptions.DEFAULT, l), + BulkProcessor bulkProcessor = BulkProcessor.builder( + (r, l) -> restHighLevelClient.bulkAsync(r, RequestOptions.DEFAULT, l), new BulkProcessor.Listener() { @Override - public void beforeBulk(long executionId, BulkRequest request) { - } + public void beforeBulk(long executionId, BulkRequest request) {} @Override public void afterBulk(long executionId, BulkRequest request, BulkResponse response) { @@ -200,7 +203,9 @@ public void afterBulk(long executionId, BulkRequest request, BulkResponse respon public void afterBulk(long executionId, BulkRequest request, Throwable failure) { throw new AssertionError("Failed to execute bulk", failure); } - }, "CCSDuelIT").build(); + }, + "CCSDuelIT" + ).build(); int numQuestions = randomIntBetween(50, 100); for (int i = 0; i < numQuestions; i++) { @@ -237,27 +242,38 @@ private static IndexRequest buildIndexRequest(String id, String type, String que if (questionId != null) { joinField.put("parent", questionId); } - indexRequest.source(XContentType.JSON, - "id", id, - "type", type, - "votes", randomIntBetween(0, 30), - "questionId", questionId, - "tags", tagsArray, - "user", "user" + randomIntBetween(1, 10), - "suggest", Collections.singletonMap("input", tagsArray), - "creationDate", date, - "join", joinField); + indexRequest.source( + XContentType.JSON, + "id", + id, + "type", + type, + "votes", + randomIntBetween(0, 30), + "questionId", + questionId, + "tags", + tagsArray, + "user", + "user" + randomIntBetween(1, 10), + "suggest", + Collections.singletonMap("input", tagsArray), + "creationDate", + date, + "join", + joinField + ); return indexRequest; } public void testMatchAll() throws Exception { assumeMultiClusterSetup(); - //verify that the order in which documents are returned when they all have the same score is the same + // verify that the order in which documents are returned when they all have the same score is the same SearchRequest searchRequest = initSearchRequest(); duelSearch(searchRequest, CCSDuelIT::assertHits); } - public void testMatchQuery() throws Exception { + public void testMatchQuery() throws Exception { assumeMultiClusterSetup(); SearchRequest searchRequest = initSearchRequest(); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); @@ -287,7 +303,7 @@ public void testTerminateAfter() throws Exception { duelSearch(searchRequest, CCSDuelIT::assertHits); } - public void testPagination() throws Exception { + public void testPagination() throws Exception { assumeMultiClusterSetup(); SearchRequest searchRequest = initSearchRequest(); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); @@ -298,7 +314,7 @@ public void testPagination() throws Exception { duelSearch(searchRequest, response -> assertHits(response, 10)); } - public void testHighlighting() throws Exception { + public void testHighlighting() throws Exception { assumeMultiClusterSetup(); SearchRequest searchRequest = initSearchRequest(); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); @@ -311,11 +327,11 @@ public void testHighlighting() throws Exception { }); } - public void testFetchSource() throws Exception { + public void testFetchSource() throws Exception { assumeMultiClusterSetup(); SearchRequest searchRequest = initSearchRequest(); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); - sourceBuilder.fetchSource(new String[]{"tags"}, Strings.EMPTY_ARRAY); + sourceBuilder.fetchSource(new String[] { "tags" }, Strings.EMPTY_ARRAY); sourceBuilder.query(QueryBuilders.matchQuery("tags", "ruby")); searchRequest.source(sourceBuilder); duelSearch(searchRequest, response -> { @@ -324,7 +340,7 @@ public void testFetchSource() throws Exception { }); } - public void testDocValueFields() throws Exception { + public void testDocValueFields() throws Exception { assumeMultiClusterSetup(); SearchRequest searchRequest = initSearchRequest(); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); @@ -338,7 +354,7 @@ public void testDocValueFields() throws Exception { }); } - public void testScriptFields() throws Exception { + public void testScriptFields() throws Exception { assumeMultiClusterSetup(); SearchRequest searchRequest = initSearchRequest(); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); @@ -351,7 +367,7 @@ public void testScriptFields() throws Exception { }); } - public void testExplain() throws Exception { + public void testExplain() throws Exception { assumeMultiClusterSetup(); SearchRequest searchRequest = initSearchRequest(); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); @@ -400,7 +416,7 @@ public void testHasChildWithInnerHit() throws Exception { duelSearch(searchRequest, CCSDuelIT::assertHits); } - public void testProfile() throws Exception { + public void testProfile() throws Exception { assumeMultiClusterSetup(); SearchRequest searchRequest = initSearchRequest(); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); @@ -417,7 +433,7 @@ public void testProfile() throws Exception { }); } - public void testSortByField() throws Exception { + public void testSortByField() throws Exception { assumeMultiClusterSetup(); SearchRequest searchRequest = initSearchRequest(); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); @@ -547,8 +563,7 @@ private static SearchSourceBuilder buildTermsAggsSource() { tags.subAggregation(tags2); FilterAggregationBuilder answers = new FilterAggregationBuilder("answers", new TermQueryBuilder("type", "answer")); - TermsAggregationBuilder answerPerQuestion = new TermsAggregationBuilder("answer_per_question") - .userValueTypeHint(ValueType.STRING); + TermsAggregationBuilder answerPerQuestion = new TermsAggregationBuilder("answer_per_question").userValueTypeHint(ValueType.STRING); answerPerQuestion.showTermDocCountError(true); answerPerQuestion.field("questionId.keyword"); answers.subAggregation(answerPerQuestion); @@ -635,7 +650,7 @@ public void testTermsLookup() throws Exception { assumeMultiClusterSetup(); IndexRequest indexRequest = new IndexRequest("lookup_index"); indexRequest.id("id"); - indexRequest.source("tags", new String[]{"java", "sql", "html", "jax-ws"}); + indexRequest.source("tags", new String[] { "java", "sql", "html", "jax-ws" }); indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL); IndexResponse indexResponse = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT); assertEquals(201, indexResponse.status().getStatus()); @@ -670,8 +685,7 @@ public void testTermSuggester() throws Exception { searchRequest.source(sourceBuilder); SuggestBuilder suggestBuilder = new SuggestBuilder(); suggestBuilder.setGlobalText("jva hml"); - suggestBuilder.addSuggestion("tags", new TermSuggestionBuilder("tags") - .suggestMode(TermSuggestionBuilder.SuggestMode.POPULAR)); + suggestBuilder.addSuggestion("tags", new TermSuggestionBuilder("tags").suggestMode(TermSuggestionBuilder.SuggestMode.POPULAR)); sourceBuilder.suggest(suggestBuilder); duelSearch(searchRequest, response -> { assertMultiClusterSearchResponse(response); @@ -688,8 +702,11 @@ public void testPhraseSuggester() throws Exception { searchRequest.source(sourceBuilder); SuggestBuilder suggestBuilder = new SuggestBuilder(); suggestBuilder.setGlobalText("jva and hml"); - suggestBuilder.addSuggestion("tags", new PhraseSuggestionBuilder("tags").addCandidateGenerator( - new DirectCandidateGeneratorBuilder("tags").suggestMode("always")).highlight("", "")); + suggestBuilder.addSuggestion( + "tags", + new PhraseSuggestionBuilder("tags").addCandidateGenerator(new DirectCandidateGeneratorBuilder("tags").suggestMode("always")) + .highlight("", "") + ); sourceBuilder.suggest(suggestBuilder); duelSearch(searchRequest, response -> { assertMultiClusterSearchResponse(response); @@ -737,14 +754,20 @@ private static void duelSearch(SearchRequest searchRequest, Consumer exception1 = new AtomicReference<>(); AtomicReference minimizeRoundtripsResponse = new AtomicReference<>(); searchRequest.setCcsMinimizeRoundtrips(true); - restHighLevelClient.searchAsync(searchRequest, RequestOptions.DEFAULT, - new LatchedActionListener<>(ActionListener.wrap(minimizeRoundtripsResponse::set, exception1::set), latch)); + restHighLevelClient.searchAsync( + searchRequest, + RequestOptions.DEFAULT, + new LatchedActionListener<>(ActionListener.wrap(minimizeRoundtripsResponse::set, exception1::set), latch) + ); AtomicReference exception2 = new AtomicReference<>(); AtomicReference fanOutResponse = new AtomicReference<>(); searchRequest.setCcsMinimizeRoundtrips(false); - restHighLevelClient.searchAsync(searchRequest, RequestOptions.DEFAULT, - new LatchedActionListener<>(ActionListener.wrap(fanOutResponse::set, exception2::set), latch)); + restHighLevelClient.searchAsync( + searchRequest, + RequestOptions.DEFAULT, + new LatchedActionListener<>(ActionListener.wrap(fanOutResponse::set, exception2::set), latch) + ); latch.await(); @@ -809,8 +832,11 @@ private static void assertAggs(SearchResponse response) { for (Aggregation aggregation : aggregations) { if (aggregation instanceof MultiBucketsAggregation) { MultiBucketsAggregation multiBucketsAggregation = (MultiBucketsAggregation) aggregation; - assertThat("agg " + multiBucketsAggregation.getName() + " has 0 buckets", - multiBucketsAggregation.getBuckets().size(), greaterThan(0)); + assertThat( + "agg " + multiBucketsAggregation.getName() + " has 0 buckets", + multiBucketsAggregation.getBuckets().size(), + greaterThan(0) + ); } } } @@ -821,9 +847,9 @@ private static Map responseToMap(SearchResponse response) throws Map responseMap = XContentHelper.convertToMap(bytesReference, false, XContentType.JSON).v2(); assertNotNull(responseMap.put("took", -1)); responseMap.remove("num_reduce_phases"); - Map profile = (Map)responseMap.get("profile"); + Map profile = (Map) responseMap.get("profile"); if (profile != null) { - List> shards = (List >)profile.get("shards"); + List> shards = (List>) profile.get("shards"); for (Map shard : shards) { replaceProfileTime(shard); /* @@ -836,7 +862,7 @@ private static Map responseToMap(SearchResponse response) throws shard.remove("fetch"); } } - Map shards = (Map)responseMap.get("_shards"); + Map shards = (Map) responseMap.get("_shards"); if (shards != null) { shards.remove("skipped"); } diff --git a/qa/multi-cluster-search/src/test/java/org/elasticsearch/search/MultiClusterSearchYamlTestSuiteIT.java b/qa/multi-cluster-search/src/test/java/org/elasticsearch/search/MultiClusterSearchYamlTestSuiteIT.java index 86fa7f715df7c..b6a776a44a299 100644 --- a/qa/multi-cluster-search/src/test/java/org/elasticsearch/search/MultiClusterSearchYamlTestSuiteIT.java +++ b/qa/multi-cluster-search/src/test/java/org/elasticsearch/search/MultiClusterSearchYamlTestSuiteIT.java @@ -11,6 +11,7 @@ import com.carrotsearch.randomizedtesting.annotations.Name; import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; import com.carrotsearch.randomizedtesting.annotations.TimeoutSuite; + import org.apache.lucene.util.TimeUnits; import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate; import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase; diff --git a/qa/no-bootstrap-tests/src/test/java/org/elasticsearch/bootstrap/SpawnerNoBootstrapTests.java b/qa/no-bootstrap-tests/src/test/java/org/elasticsearch/bootstrap/SpawnerNoBootstrapTests.java index 5c3ffcc82571c..9879604cbf24c 100644 --- a/qa/no-bootstrap-tests/src/test/java/org/elasticsearch/bootstrap/SpawnerNoBootstrapTests.java +++ b/qa/no-bootstrap-tests/src/test/java/org/elasticsearch/bootstrap/SpawnerNoBootstrapTests.java @@ -14,8 +14,8 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.env.Environment; import org.elasticsearch.env.TestEnvironment; -import org.elasticsearch.plugins.PluginTestUtil; import org.elasticsearch.plugins.Platforms; +import org.elasticsearch.plugins.PluginTestUtil; import java.io.BufferedReader; import java.io.IOException; @@ -48,11 +48,7 @@ */ public class SpawnerNoBootstrapTests extends LuceneTestCase { - private static final String CONTROLLER_SOURCE = "#!/bin/bash\n" - + "\n" - + "echo I am alive\n" - + "\n" - + "read SOMETHING\n"; + private static final String CONTROLLER_SOURCE = "#!/bin/bash\n" + "\n" + "echo I am alive\n" + "\n" + "read SOMETHING\n"; /** * Simplest case: a module with no controller daemon. @@ -70,14 +66,22 @@ public void testNoControllerSpawn() throws IOException { Files.createDirectories(environment.modulesFile()); Files.createDirectories(plugin); PluginTestUtil.writePluginProperties( - plugin, - "description", "a_plugin", - "version", Version.CURRENT.toString(), - "elasticsearch.version", Version.CURRENT.toString(), - "name", "a_plugin", - "java.version", "1.8", - "classname", "APlugin", - "has.native.controller", "false"); + plugin, + "description", + "a_plugin", + "version", + Version.CURRENT.toString(), + "elasticsearch.version", + Version.CURRENT.toString(), + "name", + "a_plugin", + "java.version", + "1.8", + "classname", + "APlugin", + "has.native.controller", + "false" + ); try (Spawner spawner = new Spawner()) { spawner.spawnNativeControllers(environment, false); @@ -114,13 +118,21 @@ private void assertControllerSpawns(final Function pluginsDir Files.createDirectories(plugin); PluginTestUtil.writePluginProperties( plugin, - "description", "test_plugin", - "version", Version.CURRENT.toString(), - "elasticsearch.version", Version.CURRENT.toString(), - "name", "test_plugin", - "java.version", "1.8", - "classname", "TestPlugin", - "has.native.controller", "true"); + "description", + "test_plugin", + "version", + Version.CURRENT.toString(), + "elasticsearch.version", + Version.CURRENT.toString(), + "name", + "test_plugin", + "java.version", + "1.8", + "classname", + "TestPlugin", + "has.native.controller", + "true" + ); Path controllerProgram = Platforms.nativeControllerPath(plugin); createControllerProgram(controllerProgram); @@ -129,13 +141,21 @@ private void assertControllerSpawns(final Function pluginsDir Files.createDirectories(otherPlugin); PluginTestUtil.writePluginProperties( otherPlugin, - "description", "other_plugin", - "version", Version.CURRENT.toString(), - "elasticsearch.version", Version.CURRENT.toString(), - "name", "other_plugin", - "java.version", "1.8", - "classname", "OtherPlugin", - "has.native.controller", "false"); + "description", + "other_plugin", + "version", + Version.CURRENT.toString(), + "elasticsearch.version", + Version.CURRENT.toString(), + "name", + "other_plugin", + "java.version", + "1.8", + "classname", + "OtherPlugin", + "has.native.controller", + "false" + ); Spawner spawner = new Spawner(); spawner.spawnNativeControllers(environment, false); @@ -143,7 +163,7 @@ private void assertControllerSpawns(final Function pluginsDir List processes = spawner.getProcesses(); if (expectSpawn) { - // as there should only be a reference in the list for the module that had the controller daemon, we expect one here + // as there should only be a reference in the list for the module that had the controller daemon, we expect one here assertThat(processes, hasSize(1)); Process process = processes.get(0); final InputStreamReader in = new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8); @@ -171,24 +191,28 @@ public void testControllerSpawnWithIncorrectDescriptor() throws IOException { Path plugin = environment.modulesFile().resolve("test_plugin"); Files.createDirectories(plugin); PluginTestUtil.writePluginProperties( - plugin, - "description", "test_plugin", - "version", Version.CURRENT.toString(), - "elasticsearch.version", Version.CURRENT.toString(), - "name", "test_plugin", - "java.version", "1.8", - "classname", "TestPlugin", - "has.native.controller", "false"); + plugin, + "description", + "test_plugin", + "version", + Version.CURRENT.toString(), + "elasticsearch.version", + Version.CURRENT.toString(), + "name", + "test_plugin", + "java.version", + "1.8", + "classname", + "TestPlugin", + "has.native.controller", + "false" + ); Path controllerProgram = Platforms.nativeControllerPath(plugin); createControllerProgram(controllerProgram); Spawner spawner = new Spawner(); - IllegalArgumentException e = expectThrows( - IllegalArgumentException.class, - () -> spawner.spawnNativeControllers(environment, false)); - assertThat( - e.getMessage(), - equalTo("module [test_plugin] does not have permission to fork native controller")); + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> spawner.spawnNativeControllers(environment, false)); + assertThat(e.getMessage(), equalTo("module [test_plugin] does not have permission to fork native controller")); } public void testSpawnerHandlingOfDesktopServicesStoreFiles() throws IOException { @@ -222,8 +246,7 @@ private void createControllerProgram(final Path outputFile) throws IOException { final Path outputDir = outputFile.getParent(); Files.createDirectories(outputDir); Files.write(outputFile, CONTROLLER_SOURCE.getBytes(StandardCharsets.UTF_8)); - final PosixFileAttributeView view = - Files.getFileAttributeView(outputFile, PosixFileAttributeView.class); + final PosixFileAttributeView view = Files.getFileAttributeView(outputFile, PosixFileAttributeView.class); if (view != null) { final Set perms = new HashSet<>(); perms.add(PosixFilePermission.OWNER_READ); diff --git a/qa/remote-clusters/src/test/java/org/elasticsearch/cluster/remote/test/AbstractMultiClusterRemoteTestCase.java b/qa/remote-clusters/src/test/java/org/elasticsearch/cluster/remote/test/AbstractMultiClusterRemoteTestCase.java index eeff4a42a8616..c5bee9694a275 100644 --- a/qa/remote-clusters/src/test/java/org/elasticsearch/cluster/remote/test/AbstractMultiClusterRemoteTestCase.java +++ b/qa/remote-clusters/src/test/java/org/elasticsearch/cluster/remote/test/AbstractMultiClusterRemoteTestCase.java @@ -13,10 +13,10 @@ import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; -import org.elasticsearch.core.PathUtils; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.PathUtils; import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.test.rest.ESRestTestCase; import org.junit.AfterClass; @@ -44,7 +44,6 @@ protected boolean preserveClusterUponCompletion() { private static RestHighLevelClient cluster2Client; private static boolean initialized = false; - @Override protected String getTestRestCluster() { return "localhost:" + getProperty("test.fixtures.elasticsearch-" + getDistribution() + "-1.tcp.9200"); @@ -99,9 +98,12 @@ private HighLevelClient(RestClient restClient) { private RestHighLevelClient buildClient(final String url) throws IOException { int portSeparator = url.lastIndexOf(':'); - HttpHost httpHost = new HttpHost(url.substring(0, portSeparator), - Integer.parseInt(url.substring(portSeparator + 1)), getProtocol()); - return new HighLevelClient(buildClient(restAdminSettings(), new HttpHost[]{httpHost})); + HttpHost httpHost = new HttpHost( + url.substring(0, portSeparator), + Integer.parseInt(url.substring(portSeparator + 1)), + getProtocol() + ); + return new HighLevelClient(buildClient(restAdminSettings(), new HttpHost[] { httpHost })); } protected boolean isOss() { @@ -150,8 +152,10 @@ protected String getProtocol() { private String getProperty(String key) { String value = System.getProperty(key); if (value == null) { - throw new IllegalStateException("Could not find system properties from test.fixtures. " + - "This test expects to run with the elasticsearch.test.fixtures Gradle plugin"); + throw new IllegalStateException( + "Could not find system properties from test.fixtures. " + + "This test expects to run with the elasticsearch.test.fixtures Gradle plugin" + ); } return value; } diff --git a/qa/remote-clusters/src/test/java/org/elasticsearch/cluster/remote/test/RemoteClustersIT.java b/qa/remote-clusters/src/test/java/org/elasticsearch/cluster/remote/test/RemoteClustersIT.java index dfec8d7c8ebbf..6e4d83873f9df 100644 --- a/qa/remote-clusters/src/test/java/org/elasticsearch/cluster/remote/test/RemoteClustersIT.java +++ b/qa/remote-clusters/src/test/java/org/elasticsearch/cluster/remote/test/RemoteClustersIT.java @@ -30,16 +30,38 @@ public class RemoteClustersIT extends AbstractMultiClusterRemoteTestCase { @Before public void setupIndices() throws IOException { - assertTrue(cluster1Client().indices().create(new CreateIndexRequest("test1").settings(Settings.builder() - .put("index.number_of_replicas", 0).build()), RequestOptions.DEFAULT).isAcknowledged()); - cluster1Client().index(new IndexRequest("test1").id("id1").setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .source(XContentFactory.jsonBuilder().startObject().field("foo", "bar").endObject()), RequestOptions.DEFAULT); - assertTrue(cluster2Client().indices().create(new CreateIndexRequest("test2").settings(Settings.builder() - .put("index.number_of_replicas", 0).build()), RequestOptions.DEFAULT).isAcknowledged()); - cluster2Client().index(new IndexRequest("test2").id("id1") - .source(XContentFactory.jsonBuilder().startObject().field("foo", "bar").endObject()), RequestOptions.DEFAULT); - cluster2Client().index(new IndexRequest("test2").id("id2").setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .source(XContentFactory.jsonBuilder().startObject().field("foo", "bar").endObject()), RequestOptions.DEFAULT); + assertTrue( + cluster1Client().indices() + .create( + new CreateIndexRequest("test1").settings(Settings.builder().put("index.number_of_replicas", 0).build()), + RequestOptions.DEFAULT + ) + .isAcknowledged() + ); + cluster1Client().index( + new IndexRequest("test1").id("id1") + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + .source(XContentFactory.jsonBuilder().startObject().field("foo", "bar").endObject()), + RequestOptions.DEFAULT + ); + assertTrue( + cluster2Client().indices() + .create( + new CreateIndexRequest("test2").settings(Settings.builder().put("index.number_of_replicas", 0).build()), + RequestOptions.DEFAULT + ) + .isAcknowledged() + ); + cluster2Client().index( + new IndexRequest("test2").id("id1").source(XContentFactory.jsonBuilder().startObject().field("foo", "bar").endObject()), + RequestOptions.DEFAULT + ); + cluster2Client().index( + new IndexRequest("test2").id("id2") + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + .source(XContentFactory.jsonBuilder().startObject().field("foo", "bar").endObject()), + RequestOptions.DEFAULT + ); assertEquals(1L, cluster1Client().search(new SearchRequest("test1"), RequestOptions.DEFAULT).getHits().getTotalHits().value); assertEquals(2L, cluster2Client().search(new SearchRequest("test2"), RequestOptions.DEFAULT).getHits().getTotalHits().value); } @@ -53,7 +75,8 @@ public void clearIndices() throws IOException { @After public void clearRemoteClusterSettings() throws IOException { ClusterUpdateSettingsRequest request = new ClusterUpdateSettingsRequest().persistentSettings( - Settings.builder().putNull("cluster.remote.*").build()); + Settings.builder().putNull("cluster.remote.*").build() + ); assertTrue(cluster1Client().cluster().putSettings(request, RequestOptions.DEFAULT).isAcknowledged()); assertTrue(cluster2Client().cluster().putSettings(request, RequestOptions.DEFAULT).isAcknowledged()); } @@ -61,27 +84,33 @@ public void clearRemoteClusterSettings() throws IOException { public void testProxyModeConnectionWorks() throws IOException { String cluster2RemoteClusterSeed = "elasticsearch-" + getDistribution() + "-2:9300"; logger.info("Configuring remote cluster [{}]", cluster2RemoteClusterSeed); - ClusterUpdateSettingsRequest request = new ClusterUpdateSettingsRequest().persistentSettings(Settings.builder() - .put("cluster.remote.cluster2.mode", "proxy") - .put("cluster.remote.cluster2.proxy_address", cluster2RemoteClusterSeed) - .build()); + ClusterUpdateSettingsRequest request = new ClusterUpdateSettingsRequest().persistentSettings( + Settings.builder() + .put("cluster.remote.cluster2.mode", "proxy") + .put("cluster.remote.cluster2.proxy_address", cluster2RemoteClusterSeed) + .build() + ); assertTrue(cluster1Client().cluster().putSettings(request, RequestOptions.DEFAULT).isAcknowledged()); RemoteConnectionInfo rci = cluster1Client().cluster().remoteInfo(new RemoteInfoRequest(), RequestOptions.DEFAULT).getInfos().get(0); logger.info("Connection info: {}", rci); assertTrue(rci.isConnected()); - assertEquals(2L, cluster1Client().search( - new SearchRequest("cluster2:test2"), RequestOptions.DEFAULT).getHits().getTotalHits().value); + assertEquals( + 2L, + cluster1Client().search(new SearchRequest("cluster2:test2"), RequestOptions.DEFAULT).getHits().getTotalHits().value + ); } public void testSniffModeConnectionFails() throws IOException { String cluster2RemoteClusterSeed = "elasticsearch-" + getDistribution() + "-2:9300"; logger.info("Configuring remote cluster [{}]", cluster2RemoteClusterSeed); - ClusterUpdateSettingsRequest request = new ClusterUpdateSettingsRequest().persistentSettings(Settings.builder() - .put("cluster.remote.cluster2alt.mode", "sniff") - .put("cluster.remote.cluster2alt.seeds", cluster2RemoteClusterSeed) - .build()); + ClusterUpdateSettingsRequest request = new ClusterUpdateSettingsRequest().persistentSettings( + Settings.builder() + .put("cluster.remote.cluster2alt.mode", "sniff") + .put("cluster.remote.cluster2alt.seeds", cluster2RemoteClusterSeed) + .build() + ); assertTrue(cluster1Client().cluster().putSettings(request, RequestOptions.DEFAULT).isAcknowledged()); RemoteConnectionInfo rci = cluster1Client().cluster().remoteInfo(new RemoteInfoRequest(), RequestOptions.DEFAULT).getInfos().get(0); @@ -92,53 +121,65 @@ public void testSniffModeConnectionFails() throws IOException { public void testHAProxyModeConnectionWorks() throws IOException { String proxyAddress = "haproxy:9600"; logger.info("Configuring remote cluster [{}]", proxyAddress); - ClusterUpdateSettingsRequest request = new ClusterUpdateSettingsRequest().persistentSettings(Settings.builder() - .put("cluster.remote.haproxynosn.mode", "proxy") - .put("cluster.remote.haproxynosn.proxy_address", proxyAddress) - .build()); + ClusterUpdateSettingsRequest request = new ClusterUpdateSettingsRequest().persistentSettings( + Settings.builder() + .put("cluster.remote.haproxynosn.mode", "proxy") + .put("cluster.remote.haproxynosn.proxy_address", proxyAddress) + .build() + ); assertTrue(cluster1Client().cluster().putSettings(request, RequestOptions.DEFAULT).isAcknowledged()); RemoteConnectionInfo rci = cluster1Client().cluster().remoteInfo(new RemoteInfoRequest(), RequestOptions.DEFAULT).getInfos().get(0); logger.info("Connection info: {}", rci); assertTrue(rci.isConnected()); - assertEquals(2L, cluster1Client().search( - new SearchRequest("haproxynosn:test2"), RequestOptions.DEFAULT).getHits().getTotalHits().value); + assertEquals( + 2L, + cluster1Client().search(new SearchRequest("haproxynosn:test2"), RequestOptions.DEFAULT).getHits().getTotalHits().value + ); } public void testHAProxyModeConnectionWithSNIToCluster1Works() throws IOException { assumeThat("test is only supported if the distribution contains xpack", getDistribution(), equalTo("default")); - ClusterUpdateSettingsRequest request = new ClusterUpdateSettingsRequest().persistentSettings(Settings.builder() - .put("cluster.remote.haproxysni1.mode", "proxy") - .put("cluster.remote.haproxysni1.proxy_address", "haproxy:9600") - .put("cluster.remote.haproxysni1.server_name", "application1.example.com") - .build()); + ClusterUpdateSettingsRequest request = new ClusterUpdateSettingsRequest().persistentSettings( + Settings.builder() + .put("cluster.remote.haproxysni1.mode", "proxy") + .put("cluster.remote.haproxysni1.proxy_address", "haproxy:9600") + .put("cluster.remote.haproxysni1.server_name", "application1.example.com") + .build() + ); assertTrue(cluster2Client().cluster().putSettings(request, RequestOptions.DEFAULT).isAcknowledged()); RemoteConnectionInfo rci = cluster2Client().cluster().remoteInfo(new RemoteInfoRequest(), RequestOptions.DEFAULT).getInfos().get(0); logger.info("Connection info: {}", rci); assertTrue(rci.isConnected()); - assertEquals(1L, cluster2Client().search( - new SearchRequest("haproxysni1:test1"), RequestOptions.DEFAULT).getHits().getTotalHits().value); + assertEquals( + 1L, + cluster2Client().search(new SearchRequest("haproxysni1:test1"), RequestOptions.DEFAULT).getHits().getTotalHits().value + ); } public void testHAProxyModeConnectionWithSNIToCluster2Works() throws IOException { assumeThat("test is only supported if the distribution contains xpack", getDistribution(), equalTo("default")); - ClusterUpdateSettingsRequest request = new ClusterUpdateSettingsRequest().persistentSettings(Settings.builder() - .put("cluster.remote.haproxysni2.mode", "proxy") - .put("cluster.remote.haproxysni2.proxy_address", "haproxy:9600") - .put("cluster.remote.haproxysni2.server_name", "application2.example.com") - .build()); + ClusterUpdateSettingsRequest request = new ClusterUpdateSettingsRequest().persistentSettings( + Settings.builder() + .put("cluster.remote.haproxysni2.mode", "proxy") + .put("cluster.remote.haproxysni2.proxy_address", "haproxy:9600") + .put("cluster.remote.haproxysni2.server_name", "application2.example.com") + .build() + ); assertTrue(cluster1Client().cluster().putSettings(request, RequestOptions.DEFAULT).isAcknowledged()); RemoteConnectionInfo rci = cluster1Client().cluster().remoteInfo(new RemoteInfoRequest(), RequestOptions.DEFAULT).getInfos().get(0); logger.info("Connection info: {}", rci); assertTrue(rci.isConnected()); - assertEquals(2L, cluster1Client().search( - new SearchRequest("haproxysni2:test2"), RequestOptions.DEFAULT).getHits().getTotalHits().value); + assertEquals( + 2L, + cluster1Client().search(new SearchRequest("haproxysni2:test2"), RequestOptions.DEFAULT).getHits().getTotalHits().value + ); } } diff --git a/qa/repository-multi-version/src/test/java/org/elasticsearch/upgrades/MultiVersionRepositoryAccessIT.java b/qa/repository-multi-version/src/test/java/org/elasticsearch/upgrades/MultiVersionRepositoryAccessIT.java index d52cf409094af..c8af37bb90602 100644 --- a/qa/repository-multi-version/src/test/java/org/elasticsearch/upgrades/MultiVersionRepositoryAccessIT.java +++ b/qa/repository-multi-version/src/test/java/org/elasticsearch/upgrades/MultiVersionRepositoryAccessIT.java @@ -24,12 +24,12 @@ import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.DeprecationHandler; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.snapshots.RestoreInfo; import org.elasticsearch.snapshots.SnapshotsService; import org.elasticsearch.test.rest.ESRestTestCase; +import org.elasticsearch.xcontent.DeprecationHandler; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.json.JsonXContent; import java.io.IOException; import java.io.InputStream; @@ -117,23 +117,27 @@ public void testCreateAndRestoreSnapshot() throws IOException { final List> snapshotsIncludingToDelete = listSnapshots(repoName); // Every step creates one snapshot and we have to add one more for the temporary snapshot assertThat(snapshotsIncludingToDelete, hasSize(TEST_STEP.ordinal() + 1 + 1)); - assertThat(snapshotsIncludingToDelete.stream().map( - sn -> (String) sn.get("snapshot")).collect(Collectors.toList()), hasItem(snapshotToDeleteName)); + assertThat( + snapshotsIncludingToDelete.stream().map(sn -> (String) sn.get("snapshot")).collect(Collectors.toList()), + hasItem(snapshotToDeleteName) + ); deleteSnapshot(client, repoName, snapshotToDeleteName); final List> snapshots = listSnapshots(repoName); assertThat(snapshots, hasSize(TEST_STEP.ordinal() + 1)); switch (TEST_STEP) { case STEP2_NEW_CLUSTER: case STEP4_NEW_CLUSTER: - assertSnapshotStatusSuccessful(client, repoName, - snapshots.stream().map(sn -> (String) sn.get("snapshot")).toArray(String[]::new)); + assertSnapshotStatusSuccessful( + client, + repoName, + snapshots.stream().map(sn -> (String) sn.get("snapshot")).toArray(String[]::new) + ); break; case STEP1_OLD_CLUSTER: assertSnapshotStatusSuccessful(client, repoName, "snapshot-" + TEST_STEP); break; case STEP3_OLD_CLUSTER: - assertSnapshotStatusSuccessful( - client, repoName, "snapshot-" + TEST_STEP, "snapshot-" + TestStep.STEP3_OLD_CLUSTER); + assertSnapshotStatusSuccessful(client, repoName, "snapshot-" + TEST_STEP, "snapshot-" + TestStep.STEP3_OLD_CLUSTER); break; } if (TEST_STEP == TestStep.STEP3_OLD_CLUSTER) { @@ -173,8 +177,12 @@ public void testReadOnlyRepo() throws IOException { if (TEST_STEP == TestStep.STEP1_OLD_CLUSTER || TEST_STEP == TestStep.STEP3_OLD_CLUSTER) { assertSnapshotStatusSuccessful(client, repoName, "snapshot-" + TestStep.STEP1_OLD_CLUSTER); } else { - assertSnapshotStatusSuccessful(client, repoName, - "snapshot-" + TestStep.STEP1_OLD_CLUSTER, "snapshot-" + TestStep.STEP2_NEW_CLUSTER); + assertSnapshotStatusSuccessful( + client, + repoName, + "snapshot-" + TestStep.STEP1_OLD_CLUSTER, + "snapshot-" + TestStep.STEP2_NEW_CLUSTER + ); } if (TEST_STEP == TestStep.STEP3_OLD_CLUSTER) { ensureSnapshotRestoreWorks(client, repoName, "snapshot-" + TestStep.STEP1_OLD_CLUSTER, shards, index); @@ -185,21 +193,24 @@ public void testReadOnlyRepo() throws IOException { } } - private static final List> EXPECTED_BWC_EXCEPTIONS = - List.of(ResponseException.class, ElasticsearchStatusException.class); + private static final List> EXPECTED_BWC_EXCEPTIONS = List.of( + ResponseException.class, + ElasticsearchStatusException.class + ); public void testUpgradeMovesRepoToNewMetaVersion() throws IOException { final String repoName = getTestName(); try (RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(adminClient().getNodes().toArray(new Node[0])))) { final int shards = 3; - final String index= "test-index"; + final String index = "test-index"; createIndex(client, index, shards); final Version minNodeVersion = minimumNodeVersion(); // 7.12.0+ will try to load RepositoryData during repo creation if verify is true, which is impossible in case of version // incompatibility in the downgrade test step. We verify that it is impossible here and then create the repo using verify=false // to check behavior on other operations below. - final boolean verify = TEST_STEP != TestStep.STEP3_OLD_CLUSTER || SnapshotsService.includesUUIDs(minNodeVersion) - || minNodeVersion.before(Version.V_7_12_0); + final boolean verify = TEST_STEP != TestStep.STEP3_OLD_CLUSTER + || SnapshotsService.includesUUIDs(minNodeVersion) + || minNodeVersion.before(Version.V_7_12_0); if (verify == false) { expectThrowsAnyOf(EXPECTED_BWC_EXCEPTIONS, () -> createRepository(client, repoName, false, true)); } @@ -210,8 +221,11 @@ public void testUpgradeMovesRepoToNewMetaVersion() throws IOException { final List> snapshots = listSnapshots(repoName); // Every step creates one snapshot assertThat(snapshots, hasSize(TEST_STEP.ordinal() + 1)); - assertSnapshotStatusSuccessful(client, repoName, - snapshots.stream().map(sn -> (String) sn.get("snapshot")).toArray(String[]::new)); + assertSnapshotStatusSuccessful( + client, + repoName, + snapshots.stream().map(sn -> (String) sn.get("snapshot")).toArray(String[]::new) + ); if (TEST_STEP == TestStep.STEP1_OLD_CLUSTER) { ensureSnapshotRestoreWorks(client, repoName, "snapshot-" + TestStep.STEP1_OLD_CLUSTER, shards, index); } else { @@ -243,8 +257,8 @@ public void testUpgradeMovesRepoToNewMetaVersion() throws IOException { } } - private static void assertSnapshotStatusSuccessful(RestHighLevelClient client, String repoName, - String... snapshots) throws IOException { + private static void assertSnapshotStatusSuccessful(RestHighLevelClient client, String repoName, String... snapshots) + throws IOException { final SnapshotsStatusResponse statusResponse = client.snapshot() .status(new SnapshotsStatusRequest(repoName, snapshots), RequestOptions.DEFAULT); for (SnapshotStatus status : statusResponse.getSnapshots()) { @@ -258,31 +272,43 @@ private void deleteSnapshot(RestHighLevelClient client, String repoName, String @SuppressWarnings("unchecked") private List> listSnapshots(String repoName) throws IOException { - try (InputStream entity = client().performRequest( - new Request("GET", "/_snapshot/" + repoName + "/_all")).getEntity().getContent(); - XContentParser parser = JsonXContent.jsonXContent.createParser( - xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, entity)) { + try ( + InputStream entity = client().performRequest(new Request("GET", "/_snapshot/" + repoName + "/_all")).getEntity().getContent(); + XContentParser parser = JsonXContent.jsonXContent.createParser( + xContentRegistry(), + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + entity + ) + ) { return (List>) parser.map().get("snapshots"); } } - private static void ensureSnapshotRestoreWorks(RestHighLevelClient client, String repoName, String name, - int shards, String index) throws IOException { + private static void ensureSnapshotRestoreWorks(RestHighLevelClient client, String repoName, String name, int shards, String index) + throws IOException { wipeAllIndices(); - final RestoreInfo restoreInfo = - client.snapshot().restore( + final RestoreInfo restoreInfo = client.snapshot() + .restore( new RestoreSnapshotRequest().repository(repoName).snapshot(name).indices(index).waitForCompletion(true), - RequestOptions.DEFAULT).getRestoreInfo(); + RequestOptions.DEFAULT + ) + .getRestoreInfo(); assertThat(restoreInfo.failedShards(), is(0)); assertThat(restoreInfo.successfulShards(), equalTo(shards)); } - private static void createRepository(RestHighLevelClient client, String repoName, boolean readOnly, - boolean verify) throws IOException { - assertThat(client.snapshot().createRepository(new PutRepositoryRequest(repoName).type("fs").settings( - Settings.builder().put("location", "./" + repoName).put(READONLY_SETTING_KEY, readOnly)).verify(verify), RequestOptions.DEFAULT) + private static void createRepository(RestHighLevelClient client, String repoName, boolean readOnly, boolean verify) throws IOException { + assertThat( + client.snapshot() + .createRepository( + new PutRepositoryRequest(repoName).type("fs") + .settings(Settings.builder().put("location", "./" + repoName).put(READONLY_SETTING_KEY, readOnly)) + .verify(verify), + RequestOptions.DEFAULT + ) .isAcknowledged(), - is(true)); + is(true) + ); } private static void createSnapshot(RestHighLevelClient client, String repoName, String name, String index) throws IOException { @@ -295,14 +321,18 @@ private static void createSnapshot(RestHighLevelClient client, String repoName, private void createIndex(RestHighLevelClient client, String name, int shards) throws IOException { final Request putIndexRequest = new Request("PUT", "/" + name); - putIndexRequest.setJsonEntity("{\n" + - " \"settings\" : {\n" + - " \"index\" : {\n" + - " \"number_of_shards\" : " + shards + ", \n" + - " \"number_of_replicas\" : 0 \n" + - " }\n" + - " }\n" + - "}"); + putIndexRequest.setJsonEntity( + "{\n" + + " \"settings\" : {\n" + + " \"index\" : {\n" + + " \"number_of_shards\" : " + + shards + + ", \n" + + " \"number_of_replicas\" : 0 \n" + + " }\n" + + " }\n" + + "}" + ); final Response response = client.getLowLevelClient().performRequest(putIndexRequest); assertThat(response.getStatusLine().getStatusCode(), is(HttpURLConnection.HTTP_OK)); } diff --git a/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/AbstractRollingTestCase.java b/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/AbstractRollingTestCase.java index 26de71b9ba629..c905ebf65a01a 100644 --- a/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/AbstractRollingTestCase.java +++ b/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/AbstractRollingTestCase.java @@ -52,7 +52,8 @@ protected boolean preserveTemplatesUponCompletion() { @Override protected final Settings restClientSettings() { - return Settings.builder().put(super.restClientSettings()) + return Settings.builder() + .put(super.restClientSettings()) // increase the timeout here to 90 seconds to handle long waits for a green // cluster health. the waits for green need to be longer than a minute to // account for delayed shards diff --git a/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/FeatureUpgradeIT.java b/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/FeatureUpgradeIT.java index 0591e0521ff78..8d46490608652 100644 --- a/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/FeatureUpgradeIT.java +++ b/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/FeatureUpgradeIT.java @@ -25,8 +25,8 @@ public class FeatureUpgradeIT extends AbstractRollingTestCase { @SuppressWarnings("unchecked") public void testGetFeatureUpgradeStatus() throws Exception { - final String systemIndexWarning = "this request accesses system indices: [.tasks], but in a future major version, direct " + - "access to system indices will be prevented by default"; + final String systemIndexWarning = "this request accesses system indices: [.tasks], but in a future major version, direct " + + "access to system indices will be prevented by default"; if (CLUSTER_TYPE == ClusterType.OLD) { // setup - put something in the tasks index // create index @@ -36,21 +36,21 @@ public void testGetFeatureUpgradeStatus() throws Exception { Request bulk = new Request("POST", "/_bulk"); bulk.addParameter("refresh", "true"); - bulk.setJsonEntity("{\"index\": {\"_index\": \"feature_test_index_old\"}}\n" + - "{\"f1\": \"v1\", \"f2\": \"v2\"}\n"); + bulk.setJsonEntity("{\"index\": {\"_index\": \"feature_test_index_old\"}}\n" + "{\"f1\": \"v1\", \"f2\": \"v2\"}\n"); client().performRequest(bulk); // start a async reindex job Request reindex = new Request("POST", "/_reindex"); reindex.setJsonEntity( - "{\n" + - " \"source\":{\n" + - " \"index\":\"feature_test_index_old\"\n" + - " },\n" + - " \"dest\":{\n" + - " \"index\":\"feature_test_index_reindex\"\n" + - " }\n" + - "}"); + "{\n" + + " \"source\":{\n" + + " \"index\":\"feature_test_index_old\"\n" + + " },\n" + + " \"dest\":{\n" + + " \"index\":\"feature_test_index_reindex\"\n" + + " }\n" + + "}" + ); reindex.addParameter("wait_for_completion", "false"); Map response = entityAsMap(client().performRequest(reindex)); String taskId = (String) response.get("task"); @@ -81,7 +81,8 @@ public void testGetFeatureUpgradeStatus() throws Exception { assertBusy(() -> { Request clusterStateRequest = new Request("GET", "/_migration/system_features"); XContentTestUtils.JsonMapView view = new XContentTestUtils.JsonMapView( - entityAsMap(client().performRequest(clusterStateRequest))); + entityAsMap(client().performRequest(clusterStateRequest)) + ); List> features = view.get("features"); Map feature = features.stream() diff --git a/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/IndexingIT.java b/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/IndexingIT.java index 1973ddc218bf4..d204244b3ca4f 100644 --- a/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/IndexingIT.java +++ b/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/IndexingIT.java @@ -15,12 +15,12 @@ import org.elasticsearch.client.Response; import org.elasticsearch.client.ResponseException; import org.elasticsearch.common.Strings; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.common.xcontent.support.XContentMapValues; import org.elasticsearch.core.Booleans; import org.elasticsearch.index.mapper.DateFieldMapper; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.xcontent.json.JsonXContent; import org.hamcrest.Matcher; import java.io.IOException; @@ -51,25 +51,25 @@ public class IndexingIT extends AbstractRollingTestCase { public void testIndexing() throws IOException { switch (CLUSTER_TYPE) { - case OLD: - break; - case MIXED: - Request waitForYellow = new Request("GET", "/_cluster/health"); - waitForYellow.addParameter("wait_for_nodes", "3"); - waitForYellow.addParameter("wait_for_status", "yellow"); - client().performRequest(waitForYellow); - break; - case UPGRADED: - Request waitForGreen = new Request("GET", "/_cluster/health/test_index,index_with_replicas,empty_index"); - waitForGreen.addParameter("wait_for_nodes", "3"); - waitForGreen.addParameter("wait_for_status", "green"); - // wait for long enough that we give delayed unassigned shards to stop being delayed - waitForGreen.addParameter("timeout", "70s"); - waitForGreen.addParameter("level", "shards"); - client().performRequest(waitForGreen); - break; - default: - throw new UnsupportedOperationException("Unknown cluster type [" + CLUSTER_TYPE + "]"); + case OLD: + break; + case MIXED: + Request waitForYellow = new Request("GET", "/_cluster/health"); + waitForYellow.addParameter("wait_for_nodes", "3"); + waitForYellow.addParameter("wait_for_status", "yellow"); + client().performRequest(waitForYellow); + break; + case UPGRADED: + Request waitForGreen = new Request("GET", "/_cluster/health/test_index,index_with_replicas,empty_index"); + waitForGreen.addParameter("wait_for_nodes", "3"); + waitForGreen.addParameter("wait_for_status", "green"); + // wait for long enough that we give delayed unassigned shards to stop being delayed + waitForGreen.addParameter("timeout", "70s"); + waitForGreen.addParameter("level", "shards"); + client().performRequest(waitForGreen); + break; + default: + throw new UnsupportedOperationException("Unknown cluster type [" + CLUSTER_TYPE + "]"); } if (CLUSTER_TYPE == ClusterType.OLD) { @@ -96,21 +96,21 @@ public void testIndexing() throws IOException { int expectedCount; switch (CLUSTER_TYPE) { - case OLD: - expectedCount = 5; - break; - case MIXED: - if (Booleans.parseBoolean(System.getProperty("tests.first_round"))) { + case OLD: expectedCount = 5; - } else { - expectedCount = 10; - } - break; - case UPGRADED: - expectedCount = 15; - break; - default: - throw new UnsupportedOperationException("Unknown cluster type [" + CLUSTER_TYPE + "]"); + break; + case MIXED: + if (Booleans.parseBoolean(System.getProperty("tests.first_round"))) { + expectedCount = 5; + } else { + expectedCount = 10; + } + break; + case UPGRADED: + expectedCount = 15; + break; + default: + throw new UnsupportedOperationException("Unknown cluster type [" + CLUSTER_TYPE + "]"); } assertCount("test_index", expectedCount); @@ -158,12 +158,13 @@ public void testAutoIdWithOpTypeCreate() throws IOException { if (minNodeVersion.before(Version.V_7_5_0)) { ResponseException e = expectThrows(ResponseException.class, () -> client().performRequest(bulk)); assertEquals(400, e.getResponse().getStatusLine().getStatusCode()); - assertThat(e.getMessage(), + assertThat( + e.getMessage(), // if request goes to 7.5+ node either(containsString("optype create not supported for indexing requests without explicit id until")) // if request goes to < 7.5 node - .or(containsString("an id must be provided if version type or value are set") - )); + .or(containsString("an id must be provided if version type or value are set")) + ); } else { client().performRequest(bulk); } @@ -183,16 +184,16 @@ public void testDateNanosFormatUpgrade() throws IOException { Request createIndex = new Request("PUT", "/" + indexName); XContentBuilder mappings = XContentBuilder.builder(XContentType.JSON.xContent()) .startObject() - .startObject("mappings") - .startObject("properties") - .startObject("date") - .field("type", "date") - .endObject() - .startObject("date_nanos") - .field("type", "date_nanos") - .endObject() - .endObject() - .endObject() + .startObject("mappings") + .startObject("properties") + .startObject("date") + .field("type", "date") + .endObject() + .startObject("date_nanos") + .field("type", "date_nanos") + .endObject() + .endObject() + .endObject() .endObject(); createIndex.setJsonEntity(Strings.toString(mappings)); client().performRequest(createIndex); @@ -200,8 +201,8 @@ public void testDateNanosFormatUpgrade() throws IOException { Request index = new Request("POST", "/" + indexName + "/_doc/"); XContentBuilder doc = XContentBuilder.builder(XContentType.JSON.xContent()) .startObject() - .field("date", "2015-01-01T12:10:30.123Z") - .field("date_nanos", "2015-01-01T12:10:30.123456789Z") + .field("date", "2015-01-01T12:10:30.123Z") + .field("date_nanos", "2015-01-01T12:10:30.123456789Z") .endObject(); index.addParameter("refresh", "true"); index.setJsonEntity(Strings.toString(doc)); @@ -212,7 +213,7 @@ public void testDateNanosFormatUpgrade() throws IOException { Request search = new Request("POST", "/" + indexName + "/_search"); XContentBuilder query = XContentBuilder.builder(XContentType.JSON.xContent()) .startObject() - .array("fields", new String[] { "date", "date_nanos" }) + .array("fields", new String[] { "date", "date_nanos" }) .endObject(); search.setJsonEntity(Strings.toString(query)); Map response = entityAsMap(client().performRequest(search)); @@ -320,7 +321,7 @@ private void createTsdbIndex() throws IOException { indexSpec.endObject().endObject(); indexSpec.startObject("settings").startObject("index"); indexSpec.field("mode", "time_series"); - indexSpec.array("routing_path", new String[] {"dim"}); + indexSpec.array("routing_path", new String[] { "dim" }); indexSpec.endObject().endObject(); createIndex.setJsonEntity(Strings.toString(indexSpec.endObject())); client().performRequest(createIndex); @@ -366,11 +367,9 @@ private void assertTsdbAgg(Matcher... expected) throws IOException { request.setJsonEntity(Strings.toString(body.endObject())); ListMatcher tsidsExpected = matchesList(); for (int d = 0; d < expected.length; d++) { -// Object key = Map.of("dim", TSDB_DIMS.get(d)); TODO use this once tsid is real + // Object key = Map.of("dim", TSDB_DIMS.get(d)); TODO use this once tsid is real Object key = "dim:" + TSDB_DIMS.get(d); - tsidsExpected = tsidsExpected.item( - matchesMap().extraOk().entry("key", key).entry("avg", Map.of("value", expected[d])) - ); + tsidsExpected = tsidsExpected.item(matchesMap().extraOk().entry("key", key).entry("avg", Map.of("value", expected[d]))); } assertMap( entityAsMap(client().performRequest(request)), @@ -384,8 +383,10 @@ private void assertCount(String index, int count) throws IOException { searchTestIndexRequest.addParameter(TOTAL_HITS_AS_INT_PARAM, "true"); searchTestIndexRequest.addParameter("filter_path", "hits.total"); Response searchTestIndexResponse = client().performRequest(searchTestIndexRequest); - assertEquals("{\"hits\":{\"total\":" + count + "}}", - EntityUtils.toString(searchTestIndexResponse.getEntity(), StandardCharsets.UTF_8)); + assertEquals( + "{\"hits\":{\"total\":" + count + "}}", + EntityUtils.toString(searchTestIndexResponse.getEntity(), StandardCharsets.UTF_8) + ); } private Version minNodeVersion() throws IOException { diff --git a/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/RecoveryIT.java b/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/RecoveryIT.java index 41996f2b9e434..f3e9df923433f 100644 --- a/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/RecoveryIT.java +++ b/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/RecoveryIT.java @@ -16,11 +16,11 @@ import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.MetadataIndexStateService; import org.elasticsearch.cluster.routing.allocation.decider.EnableAllocationDecider; -import org.elasticsearch.core.Booleans; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.AbstractRunnable; import org.elasticsearch.common.xcontent.support.XContentMapValues; +import org.elasticsearch.core.Booleans; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.test.rest.yaml.ObjectPath; import org.hamcrest.Matchers; @@ -142,7 +142,7 @@ public void testRecoveryWithConcurrentIndexing() throws Exception { updateIndexSettings(index, Settings.builder().put(INDEX_ROUTING_ALLOCATION_ENABLE_SETTING.getKey(), "primaries")); break; case MIXED: - updateIndexSettings(index, Settings.builder().put(INDEX_ROUTING_ALLOCATION_ENABLE_SETTING.getKey(), (String)null)); + updateIndexSettings(index, Settings.builder().put(INDEX_ROUTING_ALLOCATION_ENABLE_SETTING.getKey(), (String) null)); asyncIndexDocs(index, 10, 50).get(); ensureGreen(index); client().performRequest(new Request("POST", index + "/_refresh")); @@ -153,7 +153,7 @@ public void testRecoveryWithConcurrentIndexing() throws Exception { updateIndexSettings(index, Settings.builder().put(INDEX_ROUTING_ALLOCATION_ENABLE_SETTING.getKey(), "primaries")); break; case UPGRADED: - updateIndexSettings(index, Settings.builder().put(INDEX_ROUTING_ALLOCATION_ENABLE_SETTING.getKey(), (String)null)); + updateIndexSettings(index, Settings.builder().put(INDEX_ROUTING_ALLOCATION_ENABLE_SETTING.getKey(), (String) null)); asyncIndexDocs(index, 60, 45).get(); ensureGreen(index); client().performRequest(new Request("POST", index + "/_refresh")); @@ -178,8 +178,19 @@ private void assertCount(final String index, final String preference, final int } catch (ResponseException e) { try { final Response recoveryStateResponse = client().performRequest(new Request("GET", index + "/_recovery")); - fail("failed to get doc count for index [" + index + "] with preference [" + preference + "]" + " response [" + e + "]" - + " recovery [" + EntityUtils.toString(recoveryStateResponse.getEntity()) + "]"); + fail( + "failed to get doc count for index [" + + index + + "] with preference [" + + preference + + "]" + + " response [" + + e + + "]" + + " recovery [" + + EntityUtils.toString(recoveryStateResponse.getEntity()) + + "]" + ); } catch (Exception inner) { e.addSuppressed(inner); } @@ -226,11 +237,13 @@ public void testRelocationWithConcurrentIndexing() throws Exception { final String newNode = getNodeId(v -> v.equals(Version.CURRENT)); final String oldNode = getNodeId(v -> v.before(Version.CURRENT)); // remove the replica and guaranteed the primary is placed on the old node - updateIndexSettings(index, Settings.builder() - .put(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 0) - .put(INDEX_ROUTING_ALLOCATION_ENABLE_SETTING.getKey(), (String)null) - .put("index.routing.allocation.include._id", oldNode) - .putNull("index.routing.allocation.include._tier_preference") + updateIndexSettings( + index, + Settings.builder() + .put(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 0) + .put(INDEX_ROUTING_ALLOCATION_ENABLE_SETTING.getKey(), (String) null) + .put("index.routing.allocation.include._id", oldNode) + .putNull("index.routing.allocation.include._tier_preference") ); ensureGreen(index); // wait for the primary to be assigned ensureNoInitializingShards(); // wait for all other shard activity to finish @@ -241,7 +254,8 @@ public void testRelocationWithConcurrentIndexing() throws Exception { assertBusy(() -> { Map state = entityAsMap(client().performRequest(new Request("GET", "/_cluster/state"))); String xpath = "routing_table.indices." + index + ".shards.0.node"; - @SuppressWarnings("unchecked") List assignedNodes = (List) XContentMapValues.extractValue(xpath, state); + @SuppressWarnings("unchecked") + List assignedNodes = (List) XContentMapValues.extractValue(xpath, state); assertNotNull(state.toString(), assignedNodes); assertThat(state.toString(), newNode, in(assignedNodes)); }, 60, TimeUnit.SECONDS); @@ -250,10 +264,12 @@ public void testRelocationWithConcurrentIndexing() throws Exception { assertCount(index, "_only_nodes:" + newNode, 60); break; case UPGRADED: - updateIndexSettings(index, Settings.builder() - .put(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 2) - .put("index.routing.allocation.include._id", (String)null) - .putNull("index.routing.allocation.include._tier_preference") + updateIndexSettings( + index, + Settings.builder() + .put(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 2) + .put("index.routing.allocation.include._id", (String) null) + .putNull("index.routing.allocation.include._tier_preference") ); asyncIndexDocs(index, 60, 45).get(); ensureGreen(index); @@ -359,8 +375,8 @@ public void testRetentionLeasesEstablishedWhenRelocatingPrimary() throws Excepti case MIXED: // trigger a primary relocation by excluding the last old node with a shard filter - final Map nodesMap - = ObjectPath.createFromResponse(client().performRequest(new Request("GET", "/_nodes"))).evaluate("nodes"); + final Map nodesMap = ObjectPath.createFromResponse(client().performRequest(new Request("GET", "/_nodes"))) + .evaluate("nodes"); final List oldNodeNames = new ArrayList<>(); for (Object nodeDetails : nodesMap.values()) { final Map nodeDetailsMap = (Map) nodeDetails; @@ -396,16 +412,19 @@ public void testRetentionLeasesEstablishedWhenRelocatingPrimary() throws Excepti public void testRecoveryClosedIndex() throws Exception { final String indexName = "closed_index_created_on_old"; if (CLUSTER_TYPE == ClusterType.OLD) { - createIndex(indexName, Settings.builder() - .put(IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), 1) - .put(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 1) - // if the node with the replica is the first to be restarted, while a replica is still recovering - // then delayed allocation will kick in. When the node comes back, the master will search for a copy - // but the recovering copy will be seen as invalid and the cluster health won't return to GREEN - // before timing out - .put(INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING.getKey(), "100ms") - .put(SETTING_ALLOCATION_MAX_RETRY.getKey(), "0") // fail faster - .build()); + createIndex( + indexName, + Settings.builder() + .put(IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), 1) + .put(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 1) + // if the node with the replica is the first to be restarted, while a replica is still recovering + // then delayed allocation will kick in. When the node comes back, the master will search for a copy + // but the recovering copy will be seen as invalid and the cluster health won't return to GREEN + // before timing out + .put(INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING.getKey(), "100ms") + .put(SETTING_ALLOCATION_MAX_RETRY.getKey(), "0") // fail faster + .build() + ); ensureGreen(indexName); closeIndex(indexName); } @@ -428,14 +447,17 @@ public void testRecoveryClosedIndex() throws Exception { */ public void testCloseIndexDuringRollingUpgrade() throws Exception { final Version minimumNodeVersion = minimumNodeVersion(); - final String indexName = - String.join("_", "index", CLUSTER_TYPE.toString(), Integer.toString(minimumNodeVersion.id)).toLowerCase(Locale.ROOT); + final String indexName = String.join("_", "index", CLUSTER_TYPE.toString(), Integer.toString(minimumNodeVersion.id)) + .toLowerCase(Locale.ROOT); if (indexExists(indexName) == false) { - createIndex(indexName, Settings.builder() - .put(IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), 1) - .put(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 0) - .build()); + createIndex( + indexName, + Settings.builder() + .put(IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), 1) + .put(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 0) + .build() + ); ensureGreen(indexName); closeIndex(indexName); } @@ -456,19 +478,22 @@ public void testCloseIndexDuringRollingUpgrade() throws Exception { public void testClosedIndexNoopRecovery() throws Exception { final String indexName = "closed_index_replica_allocation"; if (CLUSTER_TYPE == ClusterType.OLD) { - createIndex(indexName, Settings.builder() - .put(IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), 1) - .put(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 1) - .put(EnableAllocationDecider.INDEX_ROUTING_REBALANCE_ENABLE_SETTING.getKey(), "none") - .put(INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING.getKey(), "120s") - .put("index.routing.allocation.include._name", CLUSTER_NAME + "-0") - .build()); + createIndex( + indexName, + Settings.builder() + .put(IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), 1) + .put(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 1) + .put(EnableAllocationDecider.INDEX_ROUTING_REBALANCE_ENABLE_SETTING.getKey(), "none") + .put(INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING.getKey(), "120s") + .put("index.routing.allocation.include._name", CLUSTER_NAME + "-0") + .build() + ); indexDocs(indexName, 0, randomInt(10)); // allocate replica to node-2 - updateIndexSettings(indexName, - Settings.builder().put( - "index.routing.allocation.include._name", - CLUSTER_NAME + "-0," + CLUSTER_NAME + "-2," + CLUSTER_NAME + "-*") + updateIndexSettings( + indexName, + Settings.builder() + .put("index.routing.allocation.include._name", CLUSTER_NAME + "-0," + CLUSTER_NAME + "-2," + CLUSTER_NAME + "-*") ); ensureGreen(indexName); closeIndex(indexName); @@ -482,7 +507,8 @@ public void testClosedIndexNoopRecovery() throws Exception { assertClosedIndex(indexName, true); if (minimumNodeVersion().onOrAfter(Version.V_7_2_0)) { switch (CLUSTER_TYPE) { - case OLD: break; + case OLD: + break; case MIXED: assertNoopRecoveries(indexName, s -> s.startsWith(CLUSTER_NAME + "-0")); break; @@ -496,6 +522,7 @@ public void testClosedIndexNoopRecovery() throws Exception { } } + /** * Returns the version in which the given index has been created */ @@ -536,14 +563,15 @@ private void assertClosedIndex(final String index, final boolean checkRoutingTab assertThat(Booleans.parseBoolean((String) XContentMapValues.extractValue("index.verified_before_close", settings)), is(true)); for (int i = 0; i < numberOfShards; i++) { - final Collection> shards = - (Collection>) XContentMapValues.extractValue("shards." + i, routingTable); + final Collection> shards = (Collection>) XContentMapValues.extractValue( + "shards." + i, + routingTable + ); assertThat(shards, notNullValue()); assertThat(shards.size(), equalTo(numberOfReplicas + 1)); for (Map shard : shards) { assertThat(XContentMapValues.extractValue("shard", shard), equalTo(i)); - assertThat((String) XContentMapValues.extractValue("state", shard), - oneOf("STARTED", "RELOCATING", "RELOCATED")); + assertThat((String) XContentMapValues.extractValue("state", shard), oneOf("STARTED", "RELOCATING", "RELOCATED")); assertThat(XContentMapValues.extractValue("index", shard), equalTo(index)); } } @@ -589,16 +617,14 @@ public void testUpdateDoc() throws Exception { } private void assertNoopRecoveries(String indexName, Predicate targetNode) throws IOException { - Map recoveries = entityAsMap(client() - .performRequest(new Request("GET", indexName + "/_recovery?detailed=true"))); + Map recoveries = entityAsMap(client().performRequest(new Request("GET", indexName + "/_recovery?detailed=true"))); @SuppressWarnings("unchecked") - List> shards = (List>) XContentMapValues.extractValue(indexName + ".shards", recoveries); + List> shards = (List>) XContentMapValues.extractValue(indexName + ".shards", recoveries); assertNotNull(shards); boolean foundReplica = false; for (Map shard : shards) { - if (shard.get("primary") == Boolean.FALSE - && targetNode.test((String) XContentMapValues.extractValue("target.name", shard))) { + if (shard.get("primary") == Boolean.FALSE && targetNode.test((String) XContentMapValues.extractValue("target.name", shard))) { List details = (List) XContentMapValues.extractValue("index.files.details", shard); // once detailed recoveries works, remove this if. if (details == null) { @@ -645,10 +671,12 @@ public void testOperationBasedRecovery() throws Exception { indexDocs(index, randomIntBetween(0, 100), randomIntBetween(0, 3)); } else { ensureGreen(index); - assertNoFileBasedRecovery(index, nodeName -> - CLUSTER_TYPE == ClusterType.UPGRADED - || nodeName.startsWith(CLUSTER_NAME + "-0") - || (nodeName.startsWith(CLUSTER_NAME + "-1") && Booleans.parseBoolean(System.getProperty("tests.first_round")) == false)); + assertNoFileBasedRecovery( + index, + nodeName -> CLUSTER_TYPE == ClusterType.UPGRADED + || nodeName.startsWith(CLUSTER_NAME + "-0") + || (nodeName.startsWith(CLUSTER_NAME + "-1") && Booleans.parseBoolean(System.getProperty("tests.first_round")) == false) + ); indexDocs(index, randomIntBetween(0, 100), randomIntBetween(0, 3)); ensurePeerRecoveryRetentionLeasesRenewedAndSynced(index); } @@ -660,10 +688,14 @@ public void testOperationBasedRecovery() throws Exception { public void testTurnOffTranslogRetentionAfterUpgraded() throws Exception { final String index = "turn_off_translog_retention"; if (CLUSTER_TYPE == ClusterType.OLD) { - createIndex(index, Settings.builder() - .put(IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), 1) - .put(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), randomIntBetween(0, 2)) - .put(IndexSettings.INDEX_SOFT_DELETES_SETTING.getKey(), true).build()); + createIndex( + index, + Settings.builder() + .put(IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), 1) + .put(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), randomIntBetween(0, 2)) + .put(IndexSettings.INDEX_SOFT_DELETES_SETTING.getKey(), true) + .build() + ); ensureGreen(index); indexDocs(index, 0, randomIntBetween(100, 200)); flush(index, randomBoolean()); @@ -686,20 +718,26 @@ public void testAutoExpandIndicesDuringRollingUpgrade() throws Exception { List nodes = new ArrayList<>(nodeMap.keySet()); if (CLUSTER_TYPE == ClusterType.OLD) { - createIndex(indexName, Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, randomInt(2)) - .put(IndexMetadata.SETTING_AUTO_EXPAND_REPLICAS, "0-all") - .build()); + createIndex( + indexName, + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, randomInt(2)) + .put(IndexMetadata.SETTING_AUTO_EXPAND_REPLICAS, "0-all") + .build() + ); ensureGreen(indexName); - updateIndexSettings(indexName, - Settings.builder().put(IndexMetadata.INDEX_ROUTING_EXCLUDE_GROUP_PREFIX + "._id", nodes.get(randomInt(2)))); + updateIndexSettings( + indexName, + Settings.builder().put(IndexMetadata.INDEX_ROUTING_EXCLUDE_GROUP_PREFIX + "._id", nodes.get(randomInt(2))) + ); } ensureGreen(indexName); final int numberOfReplicas = Integer.parseInt( - getIndexSettingsAsMap(indexName).get(IndexMetadata.SETTING_NUMBER_OF_REPLICAS).toString()); + getIndexSettingsAsMap(indexName).get(IndexMetadata.SETTING_NUMBER_OF_REPLICAS).toString() + ); if (minimumNodeVersion.onOrAfter(Version.V_7_6_0)) { assertEquals(nodes.size() - 2, numberOfReplicas); } else { diff --git a/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/SnapshotBasedRecoveryIT.java b/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/SnapshotBasedRecoveryIT.java index ca0cc53d5c6ea..c15ca37424add 100644 --- a/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/SnapshotBasedRecoveryIT.java +++ b/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/SnapshotBasedRecoveryIT.java @@ -16,13 +16,13 @@ import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.support.XContentMapValues; import org.elasticsearch.core.Nullable; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.repositories.blobstore.BlobStoreRepository; import org.elasticsearch.search.builder.SearchSourceBuilder; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.List; @@ -76,11 +76,7 @@ public void testSnapshotBasedRecovery() throws Exception { String upgradedNodeId = getUpgradedNodeId(); if (upgradedNodeId != null) { - updateIndexSettings( - indexName, - Settings.builder() - .put("index.routing.allocation.exclude._id", upgradedNodeId) - ); + updateIndexSettings(indexName, Settings.builder().put("index.routing.allocation.exclude._id", upgradedNodeId)); } String primaryNodeId = getPrimaryNodeIdOfShard(indexName, 0); @@ -98,11 +94,7 @@ public void testSnapshotBasedRecovery() throws Exception { assertThat(getNodeVersion(currentPrimaryNodeId), is(equalTo(UPGRADE_FROM_VERSION))); } } else { - updateIndexSettings( - indexName, - Settings.builder() - .putNull("index.routing.allocation.exclude._id") - ); + updateIndexSettings(indexName, Settings.builder().putNull("index.routing.allocation.exclude._id")); } // Drop replicas @@ -218,10 +210,7 @@ private static Map search(String index, QueryBuilder query) thro assertOK(response); final Map responseAsMap = responseAsMap(response); - assertThat( - extractValue(responseAsMap, "_shards.failed"), - equalTo(0) - ); + assertThat(extractValue(responseAsMap, "_shards.failed"), equalTo(0)); return responseAsMap; } diff --git a/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/SystemIndicesUpgradeIT.java b/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/SystemIndicesUpgradeIT.java index bd9aa3996dc29..f814f899ae3c5 100644 --- a/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/SystemIndicesUpgradeIT.java +++ b/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/SystemIndicesUpgradeIT.java @@ -24,8 +24,8 @@ public class SystemIndicesUpgradeIT extends AbstractRollingTestCase { @SuppressWarnings("unchecked") public void testSystemIndicesUpgrades() throws Exception { - final String systemIndexWarning = "this request accesses system indices: [.tasks], but in a future major version, direct " + - "access to system indices will be prevented by default"; + final String systemIndexWarning = "this request accesses system indices: [.tasks], but in a future major version, direct " + + "access to system indices will be prevented by default"; if (CLUSTER_TYPE == ClusterType.OLD) { // create index Request createTestIndex = new Request("PUT", "/test_index_old"); @@ -34,21 +34,21 @@ public void testSystemIndicesUpgrades() throws Exception { Request bulk = new Request("POST", "/_bulk"); bulk.addParameter("refresh", "true"); - bulk.setJsonEntity("{\"index\": {\"_index\": \"test_index_old\"}}\n" + - "{\"f1\": \"v1\", \"f2\": \"v2\"}\n"); + bulk.setJsonEntity("{\"index\": {\"_index\": \"test_index_old\"}}\n" + "{\"f1\": \"v1\", \"f2\": \"v2\"}\n"); client().performRequest(bulk); // start a async reindex job Request reindex = new Request("POST", "/_reindex"); reindex.setJsonEntity( - "{\n" + - " \"source\":{\n" + - " \"index\":\"test_index_old\"\n" + - " },\n" + - " \"dest\":{\n" + - " \"index\":\"test_index_reindex\"\n" + - " }\n" + - "}"); + "{\n" + + " \"source\":{\n" + + " \"index\":\"test_index_old\"\n" + + " },\n" + + " \"dest\":{\n" + + " \"index\":\"test_index_reindex\"\n" + + " }\n" + + "}" + ); reindex.addParameter("wait_for_completion", "false"); Map response = entityAsMap(client().performRequest(reindex)); String taskId = (String) response.get("task"); @@ -83,12 +83,14 @@ public void testSystemIndicesUpgrades() throws Exception { if (minimumNodeVersion().before(SYSTEM_INDEX_ENFORCEMENT_VERSION)) { // Create an alias to make sure it gets upgraded properly Request putAliasRequest = new Request("POST", "/_aliases"); - putAliasRequest.setJsonEntity("{\n" + - " \"actions\": [\n" + - " {\"add\": {\"index\": \".tasks\", \"alias\": \"test-system-alias\"}},\n" + - " {\"add\": {\"index\": \"test_index_reindex\", \"alias\": \"test-system-alias\"}}\n" + - " ]\n" + - "}"); + putAliasRequest.setJsonEntity( + "{\n" + + " \"actions\": [\n" + + " {\"add\": {\"index\": \".tasks\", \"alias\": \"test-system-alias\"}},\n" + + " {\"add\": {\"index\": \"test_index_reindex\", \"alias\": \"test-system-alias\"}}\n" + + " ]\n" + + "}" + ); putAliasRequest.setOptions(expectVersionSpecificWarnings(v -> { v.current(systemIndexWarning); v.compatible(systemIndexWarning); @@ -98,8 +100,9 @@ public void testSystemIndicesUpgrades() throws Exception { } else if (CLUSTER_TYPE == ClusterType.UPGRADED) { assertBusy(() -> { Request clusterStateRequest = new Request("GET", "/_cluster/state/metadata"); - Map indices = new JsonMapView(entityAsMap(client().performRequest(clusterStateRequest))) - .get("metadata.indices"); + Map indices = new JsonMapView(entityAsMap(client().performRequest(clusterStateRequest))).get( + "metadata.indices" + ); // Make sure our non-system index is still non-system assertThat(new JsonMapView(indices).get("test_index_old.system"), is(false)); diff --git a/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/UpgradeClusterClientYamlTestSuiteIT.java b/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/UpgradeClusterClientYamlTestSuiteIT.java index 910b1e5428729..573fbbbab6b6e 100644 --- a/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/UpgradeClusterClientYamlTestSuiteIT.java +++ b/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/UpgradeClusterClientYamlTestSuiteIT.java @@ -10,6 +10,7 @@ import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; import com.carrotsearch.randomizedtesting.annotations.TimeoutSuite; + import org.apache.lucene.util.TimeUnits; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.test.rest.ESRestTestCase; @@ -45,7 +46,8 @@ public static Iterable parameters() throws Exception { @Override protected Settings restClientSettings() { - return Settings.builder().put(super.restClientSettings()) + return Settings.builder() + .put(super.restClientSettings()) // increase the timeout here to 90 seconds to handle long waits for a green // cluster health. the waits for green need to be longer than a minute to // account for delayed shards diff --git a/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/XPackIT.java b/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/XPackIT.java index 6e384d1134ea0..ecab6cfbc9808 100644 --- a/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/XPackIT.java +++ b/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/XPackIT.java @@ -23,10 +23,16 @@ public class XPackIT extends AbstractRollingTestCase { @Before public void skipIfNotXPack() { - assumeThat("test is only supported if the distribution contains xpack", - System.getProperty("tests.distribution"), equalTo("default")); - assumeThat("running this on the unupgraded cluster would change its state and it wouldn't work prior to 6.3 anyway", - CLUSTER_TYPE, equalTo(ClusterType.UPGRADED)); + assumeThat( + "test is only supported if the distribution contains xpack", + System.getProperty("tests.distribution"), + equalTo("default") + ); + assumeThat( + "running this on the unupgraded cluster would change its state and it wouldn't work prior to 6.3 anyway", + CLUSTER_TYPE, + equalTo(ClusterType.UPGRADED) + ); /* * *Mostly* we want this for when we're upgrading from pre-6.3's * zip distribution which doesn't contain xpack to post 6.3's zip @@ -42,11 +48,7 @@ public void skipIfNotXPack() { */ public void testBasicFeature() throws IOException { Request bulk = new Request("POST", "/sql_test/_bulk"); - bulk.setJsonEntity( - "{\"index\":{}}\n" - + "{\"f\": \"1\"}\n" - + "{\"index\":{}}\n" - + "{\"f\": \"2\"}\n"); + bulk.setJsonEntity("{\"index\":{}}\n" + "{\"f\": \"1\"}\n" + "{\"index\":{}}\n" + "{\"f\": \"2\"}\n"); bulk.addParameter("refresh", "true"); client().performRequest(bulk); @@ -71,13 +73,12 @@ public void testTrialLicense() throws IOException { startTrial.addParameter("acknowledge", "true"); client().performRequest(startTrial); - String noJobs = EntityUtils.toString( - client().performRequest(new Request("GET", "/_ml/anomaly_detectors")).getEntity()); + String noJobs = EntityUtils.toString(client().performRequest(new Request("GET", "/_ml/anomaly_detectors")).getEntity()); assertEquals("{\"count\":0,\"jobs\":[]}", noJobs); Request createJob = new Request("PUT", "/_ml/anomaly_detectors/test_job"); createJob.setJsonEntity( - "{\n" + "{\n" + " \"analysis_config\" : {\n" + " \"bucket_span\": \"10m\",\n" + " \"detectors\": [\n" @@ -91,7 +92,8 @@ public void testTrialLicense() throws IOException { + " \"time_field\": \"timestamp\",\n" + " \"time_format\": \"epoch_ms\"\n" + " }\n" - + "}\n"); + + "}\n" + ); client().performRequest(createJob); } } diff --git a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/AutoCreateIndexIT.java b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/AutoCreateIndexIT.java index eeed557cdefa5..5f6efad632ee6 100644 --- a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/AutoCreateIndexIT.java +++ b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/AutoCreateIndexIT.java @@ -14,9 +14,9 @@ import org.elasticsearch.client.ResponseException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.Streams; +import org.elasticsearch.test.rest.ESRestTestCase; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.rest.ESRestTestCase; import java.io.IOException; import java.io.InputStreamReader; @@ -79,7 +79,6 @@ public void testCannotAutoCreateIndexWhenDisallowedByTemplate() throws IOExcepti ); } - private void configureAutoCreateIndex(boolean value) throws IOException { XContentBuilder builder = JsonXContent.contentBuilder() .startObject() diff --git a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/BlockedSearcherRestCancellationTestCase.java b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/BlockedSearcherRestCancellationTestCase.java index 37d9abecb82e6..0d0e167517f79 100644 --- a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/BlockedSearcherRestCancellationTestCase.java +++ b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/BlockedSearcherRestCancellationTestCase.java @@ -12,11 +12,11 @@ import org.elasticsearch.client.Cancellable; import org.elasticsearch.client.Request; import org.elasticsearch.client.Response; -import org.elasticsearch.core.Releasable; -import org.elasticsearch.core.Releasables; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.CollectionUtils; +import org.elasticsearch.core.Releasable; +import org.elasticsearch.core.Releasables; import org.elasticsearch.index.IndexService; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.engine.Engine; @@ -55,8 +55,11 @@ */ public abstract class BlockedSearcherRestCancellationTestCase extends HttpSmokeTestCase { - private static final Setting BLOCK_SEARCHER_SETTING - = Setting.boolSetting("index.block_searcher", false, Setting.Property.IndexScope); + private static final Setting BLOCK_SEARCHER_SETTING = Setting.boolSetting( + "index.block_searcher", + false, + Setting.Property.IndexScope + ); @Override protected Collection> nodePlugins() { diff --git a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/ClusterStateRestCancellationIT.java b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/ClusterStateRestCancellationIT.java index e69e74ee72cd6..f921b9919439e 100644 --- a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/ClusterStateRestCancellationIT.java +++ b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/ClusterStateRestCancellationIT.java @@ -22,9 +22,9 @@ import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.util.CollectionUtils; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.tasks.TaskInfo; +import org.elasticsearch.xcontent.XContentBuilder; import java.util.Collection; import java.util.Collections; @@ -125,9 +125,9 @@ public static class AssertingCustomPlugin extends Plugin { @Override public List getNamedWriteables() { return Collections.singletonList( - new NamedWriteableRegistry.Entry(ClusterState.Custom.class, AssertingCustom.NAME, in -> AssertingCustom.INSTANCE)); + new NamedWriteableRegistry.Entry(ClusterState.Custom.class, AssertingCustom.NAME, in -> AssertingCustom.INSTANCE) + ); } } - } diff --git a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/ClusterStatsRestCancellationIT.java b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/ClusterStatsRestCancellationIT.java index e61b610552baf..ba71c781973d3 100644 --- a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/ClusterStatsRestCancellationIT.java +++ b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/ClusterStatsRestCancellationIT.java @@ -15,11 +15,11 @@ import org.elasticsearch.client.Request; import org.elasticsearch.client.Response; import org.elasticsearch.cluster.routing.allocation.DiskThresholdSettings; -import org.elasticsearch.core.Releasable; -import org.elasticsearch.core.Releasables; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.CollectionUtils; +import org.elasticsearch.core.Releasable; +import org.elasticsearch.core.Releasables; import org.elasticsearch.index.IndexService; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.engine.Engine; @@ -67,10 +67,10 @@ protected boolean addMockInternalEngine() { @Override protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { return Settings.builder() - .put(super.nodeSettings(nodeOrdinal, otherSettings)) - // disable internal cluster info service to avoid internal cluster stats calls - .put(DiskThresholdSettings.CLUSTER_ROUTING_ALLOCATION_DISK_THRESHOLD_ENABLED_SETTING.getKey(), false) - .build(); + .put(super.nodeSettings(nodeOrdinal, otherSettings)) + // disable internal cluster info service to avoid internal cluster stats calls + .put(DiskThresholdSettings.CLUSTER_ROUTING_ALLOCATION_DISK_THRESHOLD_ENABLED_SETTING.getKey(), false) + .build(); } public void testClusterStateRestCancellation() throws Exception { diff --git a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/CorsRegexIT.java b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/CorsRegexIT.java index 5750717743560..6ab236a89039b 100644 --- a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/CorsRegexIT.java +++ b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/CorsRegexIT.java @@ -33,12 +33,12 @@ public class CorsRegexIT extends HttpSmokeTestCase { @Override protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { return Settings.builder() - .put(super.nodeSettings(nodeOrdinal, otherSettings)) - .put(SETTING_CORS_ALLOW_ORIGIN.getKey(), "/https?:\\/\\/localhost(:[0-9]+)?/") - .put(SETTING_CORS_ALLOW_CREDENTIALS.getKey(), true) - .put(SETTING_CORS_ALLOW_METHODS.getKey(), "get, options, post") - .put(SETTING_CORS_ENABLED.getKey(), true) - .build(); + .put(super.nodeSettings(nodeOrdinal, otherSettings)) + .put(SETTING_CORS_ALLOW_ORIGIN.getKey(), "/https?:\\/\\/localhost(:[0-9]+)?/") + .put(SETTING_CORS_ALLOW_CREDENTIALS.getKey(), true) + .put(SETTING_CORS_ALLOW_METHODS.getKey(), "get, options, post") + .put(SETTING_CORS_ENABLED.getKey(), true) + .build(); } public void testThatRegularExpressionWorksOnMatch() throws IOException { @@ -74,7 +74,7 @@ public void testThatRegularExpressionReturnsForbiddenOnNonMatch() throws IOExcep try { getRestClient().performRequest(request); fail("request should have failed"); - } catch(ResponseException e) { + } catch (ResponseException e) { Response response = e.getResponse(); // a rejected origin gets a FORBIDDEN - 403 assertThat(response.getStatusLine().getStatusCode(), is(403)); @@ -122,7 +122,7 @@ public void testThatPreFlightRequestReturnsNullOnNonMatch() throws IOException { try { getRestClient().performRequest(request); fail("request should have failed"); - } catch(ResponseException e) { + } catch (ResponseException e) { Response response = e.getResponse(); // a rejected origin gets a FORBIDDEN - 403 assertThat(response.getStatusLine().getStatusCode(), is(403)); diff --git a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/DetailedErrorsDisabledIT.java b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/DetailedErrorsDisabledIT.java index c313af37bdead..93ca3876cd8df 100644 --- a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/DetailedErrorsDisabledIT.java +++ b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/DetailedErrorsDisabledIT.java @@ -8,8 +8,6 @@ package org.elasticsearch.http; -import java.io.IOException; - import org.apache.http.util.EntityUtils; import org.elasticsearch.client.Request; import org.elasticsearch.client.Response; @@ -18,6 +16,8 @@ import org.elasticsearch.test.ESIntegTestCase.ClusterScope; import org.elasticsearch.test.ESIntegTestCase.Scope; +import java.io.IOException; + import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.is; @@ -31,21 +31,22 @@ public class DetailedErrorsDisabledIT extends HttpSmokeTestCase { @Override protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { return Settings.builder() - .put(super.nodeSettings(nodeOrdinal, otherSettings)) - .put(HttpTransportSettings.SETTING_HTTP_DETAILED_ERRORS_ENABLED.getKey(), false) - .build(); + .put(super.nodeSettings(nodeOrdinal, otherSettings)) + .put(HttpTransportSettings.SETTING_HTTP_DETAILED_ERRORS_ENABLED.getKey(), false) + .build(); } public void testThatErrorTraceParamReturns400() throws IOException { Request request = new Request("DELETE", "/"); request.addParameter("error_trace", "true"); - ResponseException e = expectThrows(ResponseException.class, () -> - getRestClient().performRequest(request)); + ResponseException e = expectThrows(ResponseException.class, () -> getRestClient().performRequest(request)); Response response = e.getResponse(); assertThat(response.getHeader("Content-Type"), is("application/json")); - assertThat(EntityUtils.toString(e.getResponse().getEntity()), - containsString("\"error\":\"error traces in responses are disabled.\"")); + assertThat( + EntityUtils.toString(e.getResponse().getEntity()), + containsString("\"error\":\"error traces in responses are disabled.\"") + ); assertThat(response.getStatusLine().getStatusCode(), is(400)); } } diff --git a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/DetailedErrorsEnabledIT.java b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/DetailedErrorsEnabledIT.java index 764f2df7f4484..72817a762df88 100644 --- a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/DetailedErrorsEnabledIT.java +++ b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/DetailedErrorsEnabledIT.java @@ -10,6 +10,7 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; + import org.elasticsearch.client.Request; import org.elasticsearch.client.Response; import org.elasticsearch.client.ResponseException; @@ -40,14 +41,16 @@ public void testThatErrorTraceCanBeEnabled() throws IOException { assertThat( jsonNode.get("error").get("stack_trace").asText(), - startsWith("org.elasticsearch.action.ActionRequestValidationException: Validation Failed: 1: index / indices is missing")); + startsWith("org.elasticsearch.action.ActionRequestValidationException: Validation Failed: 1: index / indices is missing") + ); // An ActionRequestValidationException isn't an ElasticsearchException, so when the code tries // to work out the root cause, all it actually achieves is wrapping the actual exception in // an ElasticsearchException. At least this proves that the root cause logic is executing. assertThat( jsonNode.get("error").get("root_cause").get(0).get("stack_trace").asText(), - startsWith("org.elasticsearch.ElasticsearchException$1: Validation Failed: 1: index / indices is missing")); + startsWith("org.elasticsearch.ElasticsearchException$1: Validation Failed: 1: index / indices is missing") + ); } } @@ -66,7 +69,8 @@ public void testThatErrorTraceDefaultsToDisabled() throws IOException { assertFalse("Unexpected .stack_trace in JSON response", jsonNode.get("error").has("stack_trace")); assertFalse( "Unexpected .error.root_cause[0].stack_trace in JSON response", - jsonNode.get("error").get("root_cause").get(0).has("stack_trace")); + jsonNode.get("error").get("root_cause").get(0).has("stack_trace") + ); } } } diff --git a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/HttpCompressionIT.java b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/HttpCompressionIT.java index 42b67967486e6..cf434bb180884 100644 --- a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/HttpCompressionIT.java +++ b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/HttpCompressionIT.java @@ -25,12 +25,12 @@ public class HttpCompressionIT extends ESRestTestCase { private static final String GZIP_ENCODING = "gzip"; - private static final String SAMPLE_DOCUMENT = "{\n" + - " \"name\": {\n" + - " \"first name\": \"Steve\",\n" + - " \"last name\": \"Jobs\"\n" + - " }\n" + - "}"; + private static final String SAMPLE_DOCUMENT = "{\n" + + " \"name\": {\n" + + " \"first name\": \"Steve\",\n" + + " \"last name\": \"Jobs\"\n" + + " }\n" + + "}"; public void testCompressesResponseIfRequested() throws IOException { Request request = new Request("POST", "/company/_doc/2"); @@ -41,9 +41,7 @@ public void testCompressesResponseIfRequested() throws IOException { assertThat(response.getEntity(), is(not(instanceOf(GzipDecompressingEntity.class)))); request = new Request("GET", "/company/_doc/2"); - RequestOptions requestOptions = RequestOptions.DEFAULT.toBuilder() - .addHeader(HttpHeaders.ACCEPT_ENCODING, GZIP_ENCODING) - .build(); + RequestOptions requestOptions = RequestOptions.DEFAULT.toBuilder().addHeader(HttpHeaders.ACCEPT_ENCODING, GZIP_ENCODING).build(); request.setOptions(requestOptions); response = client().performRequest(request); diff --git a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/HttpSmokeTestCase.java b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/HttpSmokeTestCase.java index 719604add6a55..83f48d79a9129 100644 --- a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/HttpSmokeTestCase.java +++ b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/HttpSmokeTestCase.java @@ -61,9 +61,10 @@ protected boolean addMockHttpTransport() { @Override protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { return Settings.builder() - .put(super.nodeSettings(nodeOrdinal, otherSettings)) - .put(NetworkModule.TRANSPORT_TYPE_KEY, nodeTransportTypeKey) - .put(NetworkModule.HTTP_TYPE_KEY, nodeHttpTypeKey).build(); + .put(super.nodeSettings(nodeOrdinal, otherSettings)) + .put(NetworkModule.TRANSPORT_TYPE_KEY, nodeTransportTypeKey) + .put(NetworkModule.HTTP_TYPE_KEY, nodeHttpTypeKey) + .build(); } @Override diff --git a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/IndexingPressureRestIT.java b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/IndexingPressureRestIT.java index c2c1f2861d032..de3358967d12a 100644 --- a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/IndexingPressureRestIT.java +++ b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/IndexingPressureRestIT.java @@ -12,11 +12,11 @@ import org.elasticsearch.client.ResponseException; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.index.IndexingPressure; import org.elasticsearch.test.ESIntegTestCase.ClusterScope; import org.elasticsearch.test.ESIntegTestCase.Scope; import org.elasticsearch.test.XContentTestUtils; +import org.elasticsearch.xcontent.json.JsonXContent; import java.io.IOException; import java.util.ArrayList; @@ -49,8 +49,9 @@ protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { @SuppressWarnings("unchecked") public void testIndexingPressureStats() throws IOException { Request createRequest = new Request("PUT", "/index_name"); - createRequest.setJsonEntity("{\"settings\": {\"index\": {\"number_of_shards\": 1, \"number_of_replicas\": 1, " + - "\"write.wait_for_active_shards\": 2}}}"); + createRequest.setJsonEntity( + "{\"settings\": {\"index\": {\"number_of_shards\": 1, \"number_of_replicas\": 1, " + "\"write.wait_for_active_shards\": 2}}}" + ); final Response indexCreatedResponse = getRestClient().performRequest(createRequest); assertThat(indexCreatedResponse.getStatusLine().getStatusCode(), equalTo(OK.getStatus())); @@ -112,8 +113,11 @@ public void testIndexingPressureStats() throws IOException { Request getNodeStats2 = new Request("GET", "/_nodes/stats/indexing_pressure"); final Response nodeStats2 = getRestClient().performRequest(getNodeStats2); - Map nodeStatsMap2 = XContentHelper.convertToMap(JsonXContent.jsonXContent, nodeStats2.getEntity().getContent(), - true); + Map nodeStatsMap2 = XContentHelper.convertToMap( + JsonXContent.jsonXContent, + nodeStats2.getEntity().getContent(), + true + ); ArrayList values2 = new ArrayList<>(((Map) nodeStatsMap2.get("nodes")).values()); assertThat(values2.size(), equalTo(2)); XContentTestUtils.JsonMapView node1AfterRejection = new XContentTestUtils.JsonMapView((Map) values2.get(0)); diff --git a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/IndicesStatsRestCancellationIT.java b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/IndicesStatsRestCancellationIT.java index bc221ce4ca407..53ef4816729a9 100644 --- a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/IndicesStatsRestCancellationIT.java +++ b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/IndicesStatsRestCancellationIT.java @@ -19,10 +19,10 @@ public class IndicesStatsRestCancellationIT extends BlockedSearcherRestCancellat @Override protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { return Settings.builder() - .put(super.nodeSettings(nodeOrdinal, otherSettings)) - // disable internal cluster info service to avoid internal indices stats calls - .put(DiskThresholdSettings.CLUSTER_ROUTING_ALLOCATION_DISK_THRESHOLD_ENABLED_SETTING.getKey(), false) - .build(); + .put(super.nodeSettings(nodeOrdinal, otherSettings)) + // disable internal cluster info service to avoid internal indices stats calls + .put(DiskThresholdSettings.CLUSTER_ROUTING_ALLOCATION_DISK_THRESHOLD_ENABLED_SETTING.getKey(), false) + .build(); } public void testIndicesStatsRestCancellation() throws Exception { diff --git a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/NoHandlerIT.java b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/NoHandlerIT.java index c33c5253ba81c..386f49944b079 100644 --- a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/NoHandlerIT.java +++ b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/NoHandlerIT.java @@ -24,23 +24,24 @@ public class NoHandlerIT extends HttpSmokeTestCase { public void testNoHandlerRespectsAcceptHeader() throws IOException { runTestNoHandlerRespectsAcceptHeader( - "application/json", - "application/json", - "\"error\":\"no handler found for uri [/foo/bar/baz/qux/quux] and method [GET]\""); + "application/json", + "application/json", + "\"error\":\"no handler found for uri [/foo/bar/baz/qux/quux] and method [GET]\"" + ); runTestNoHandlerRespectsAcceptHeader( - "application/yaml", - "application/yaml", - "error: \"no handler found for uri [/foo/bar/baz/qux/quux] and method [GET]\""); + "application/yaml", + "application/yaml", + "error: \"no handler found for uri [/foo/bar/baz/qux/quux] and method [GET]\"" + ); } - private void runTestNoHandlerRespectsAcceptHeader( - final String accept, final String contentType, final String expect) throws IOException { + private void runTestNoHandlerRespectsAcceptHeader(final String accept, final String contentType, final String expect) + throws IOException { Request request = new Request("GET", "/foo/bar/baz/qux/quux"); RequestOptions.Builder options = request.getOptions().toBuilder(); options.addHeader("Accept", accept); request.setOptions(options); - final ResponseException e = expectThrows(ResponseException.class, - () -> getRestClient().performRequest(request)); + final ResponseException e = expectThrows(ResponseException.class, () -> getRestClient().performRequest(request)); final Response response = e.getResponse(); assertThat(response.getHeader("Content-Type"), equalTo(contentType)); diff --git a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/ResponseHeaderPluginIT.java b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/ResponseHeaderPluginIT.java index b961855fc0e6d..8d22cae7f4994 100644 --- a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/ResponseHeaderPluginIT.java +++ b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/ResponseHeaderPluginIT.java @@ -42,7 +42,7 @@ public void testThatSettingHeadersWorks() throws IOException { try { getRestClient().performRequest(new Request("GET", "/_protected")); fail("request should have failed"); - } catch(ResponseException e) { + } catch (ResponseException e) { Response response = e.getResponse(); assertThat(response.getStatusLine().getStatusCode(), equalTo(401)); assertThat(response.getHeader("Secret"), equalTo("required")); diff --git a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/RestGetMappingsCancellationIT.java b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/RestGetMappingsCancellationIT.java index e8ba3ab4e9191..ef71486079ad1 100644 --- a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/RestGetMappingsCancellationIT.java +++ b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/RestGetMappingsCancellationIT.java @@ -49,7 +49,8 @@ public void testGetMappingsCancellation() throws Exception { final String actionName = GetMappingsAction.NAME; // Add a retryable cluster block that would block the request execution updateClusterState(currentState -> { - ClusterBlock clusterBlock = new ClusterBlock(1000, + ClusterBlock clusterBlock = new ClusterBlock( + 1000, "Get mappings cancellation test cluster block", true, false, @@ -58,9 +59,7 @@ public void testGetMappingsCancellation() throws Exception { EnumSet.of(ClusterBlockLevel.METADATA_READ) ); - return ClusterState.builder(currentState) - .blocks(ClusterBlocks.builder().addGlobalBlock(clusterBlock).build()) - .build(); + return ClusterState.builder(currentState).blocks(ClusterBlocks.builder().addGlobalBlock(clusterBlock).build()).build(); }); final Request request = new Request(HttpGet.METHOD_NAME, "/test/_mappings"); @@ -97,8 +96,8 @@ public TimeValue masterNodeTimeout() { }; PlainActionFuture future = PlainActionFuture.newFuture(); - internalCluster().getMasterNodeInstance(ClusterService.class).submitStateUpdateTask("get_mappings_cancellation_test", - new AckedClusterStateUpdateTask(ackedRequest, future) { + internalCluster().getMasterNodeInstance(ClusterService.class) + .submitStateUpdateTask("get_mappings_cancellation_test", new AckedClusterStateUpdateTask(ackedRequest, future) { @Override public ClusterState execute(ClusterState currentState) throws Exception { return transformationFn.apply(currentState); diff --git a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/RestHttpResponseHeadersIT.java b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/RestHttpResponseHeadersIT.java index c1b4c414ea299..693475e70d824 100644 --- a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/RestHttpResponseHeadersIT.java +++ b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/RestHttpResponseHeadersIT.java @@ -41,8 +41,7 @@ public void testValidEndpointOptionsResponseHttpHeader() throws Exception { Response response = client().performRequest(new Request("OPTIONS", "/_tasks")); assertThat(response.getStatusLine().getStatusCode(), is(200)); assertThat(response.getHeader("Allow"), notNullValue()); - List responseAllowHeaderStringArray = - Arrays.asList(response.getHeader("Allow").split(",")); + List responseAllowHeaderStringArray = Arrays.asList(response.getHeader("Allow").split(",")); assertThat(responseAllowHeaderStringArray, containsInAnyOrder("GET")); } @@ -62,11 +61,12 @@ public void testUnsupportedMethodResponseHttpHeader() throws Exception { Response response = e.getResponse(); assertThat(response.getStatusLine().getStatusCode(), is(405)); assertThat(response.getHeader("Allow"), notNullValue()); - List responseAllowHeaderStringArray = - Arrays.asList(response.getHeader("Allow").split(",")); + List responseAllowHeaderStringArray = Arrays.asList(response.getHeader("Allow").split(",")); assertThat(responseAllowHeaderStringArray, containsInAnyOrder("GET")); - assertThat(EntityUtils.toString(response.getEntity()), - containsString("Incorrect HTTP method for uri [/_tasks] and method [DELETE], allowed: [GET]")); + assertThat( + EntityUtils.toString(response.getEntity()), + containsString("Incorrect HTTP method for uri [/_tasks] and method [DELETE], allowed: [GET]") + ); } } @@ -85,11 +85,12 @@ public void testIndexSettingsPostRequest() throws Exception { Response response = e.getResponse(); assertThat(response.getStatusLine().getStatusCode(), is(405)); assertThat(response.getHeader("Allow"), notNullValue()); - List responseAllowHeaderStringArray = - Arrays.asList(response.getHeader("Allow").split(",")); + List responseAllowHeaderStringArray = Arrays.asList(response.getHeader("Allow").split(",")); assertThat(responseAllowHeaderStringArray, containsInAnyOrder("PUT", "GET")); - assertThat(EntityUtils.toString(response.getEntity()), - containsString("Incorrect HTTP method for uri [/testindex/_settings] and method [POST], allowed:")); + assertThat( + EntityUtils.toString(response.getEntity()), + containsString("Incorrect HTTP method for uri [/testindex/_settings] and method [POST], allowed:") + ); assertThat(EntityUtils.toString(response.getEntity()), containsString("GET")); assertThat(EntityUtils.toString(response.getEntity()), containsString("PUT")); } diff --git a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/SearchRestCancellationIT.java b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/SearchRestCancellationIT.java index 69c8dace87fc8..012939744071b 100644 --- a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/SearchRestCancellationIT.java +++ b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/SearchRestCancellationIT.java @@ -26,7 +26,6 @@ import org.elasticsearch.client.Response; import org.elasticsearch.common.Strings; import org.elasticsearch.common.util.CollectionUtils; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.plugins.PluginsService; import org.elasticsearch.script.MockScriptPlugin; @@ -40,6 +39,7 @@ import org.elasticsearch.tasks.TaskInfo; import org.elasticsearch.tasks.TaskManager; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.XContentType; import java.nio.charset.Charset; import java.util.ArrayList; @@ -69,17 +69,23 @@ protected Collection> nodePlugins() { public void testAutomaticCancellationDuringQueryPhase() throws Exception { Request searchRequest = new Request("GET", "/test/_search"); - SearchSourceBuilder searchSource = new SearchSourceBuilder().query(scriptQuery( - new Script(ScriptType.INLINE, "mockscript", ScriptedBlockPlugin.SCRIPT_NAME, Collections.emptyMap()))); + SearchSourceBuilder searchSource = new SearchSourceBuilder().query( + scriptQuery(new Script(ScriptType.INLINE, "mockscript", ScriptedBlockPlugin.SCRIPT_NAME, Collections.emptyMap())) + ); searchRequest.setJsonEntity(Strings.toString(searchSource)); verifyCancellationDuringQueryPhase(SearchAction.NAME, searchRequest); } public void testAutomaticCancellationMultiSearchDuringQueryPhase() throws Exception { XContentType contentType = XContentType.JSON; - MultiSearchRequest multiSearchRequest = new MultiSearchRequest().add(new SearchRequest("test") - .source(new SearchSourceBuilder().scriptField("test_field", - new Script(ScriptType.INLINE, "mockscript", ScriptedBlockPlugin.SCRIPT_NAME, Collections.emptyMap())))); + MultiSearchRequest multiSearchRequest = new MultiSearchRequest().add( + new SearchRequest("test").source( + new SearchSourceBuilder().scriptField( + "test_field", + new Script(ScriptType.INLINE, "mockscript", ScriptedBlockPlugin.SCRIPT_NAME, Collections.emptyMap()) + ) + ) + ); Request restRequest = new Request("POST", "/_msearch"); byte[] requestBody = MultiSearchRequest.writeMultiLineFormat(multiSearchRequest, contentType.xContent()); restRequest.setEntity(new NByteArrayEntity(requestBody, createContentType(contentType))); @@ -105,17 +111,24 @@ void verifyCancellationDuringQueryPhase(String searchAction, Request searchReque public void testAutomaticCancellationDuringFetchPhase() throws Exception { Request searchRequest = new Request("GET", "/test/_search"); - SearchSourceBuilder searchSource = new SearchSourceBuilder().scriptField("test_field", - new Script(ScriptType.INLINE, "mockscript", ScriptedBlockPlugin.SCRIPT_NAME, Collections.emptyMap())); + SearchSourceBuilder searchSource = new SearchSourceBuilder().scriptField( + "test_field", + new Script(ScriptType.INLINE, "mockscript", ScriptedBlockPlugin.SCRIPT_NAME, Collections.emptyMap()) + ); searchRequest.setJsonEntity(Strings.toString(searchSource)); verifyCancellationDuringFetchPhase(SearchAction.NAME, searchRequest); } public void testAutomaticCancellationMultiSearchDuringFetchPhase() throws Exception { XContentType contentType = XContentType.JSON; - MultiSearchRequest multiSearchRequest = new MultiSearchRequest().add(new SearchRequest("test") - .source(new SearchSourceBuilder().scriptField("test_field", - new Script(ScriptType.INLINE, "mockscript", ScriptedBlockPlugin.SCRIPT_NAME, Collections.emptyMap())))); + MultiSearchRequest multiSearchRequest = new MultiSearchRequest().add( + new SearchRequest("test").source( + new SearchSourceBuilder().scriptField( + "test_field", + new Script(ScriptType.INLINE, "mockscript", ScriptedBlockPlugin.SCRIPT_NAME, Collections.emptyMap()) + ) + ) + ); Request restRequest = new Request("POST", "/_msearch"); byte[] requestBody = MultiSearchRequest.writeMultiLineFormat(multiSearchRequest, contentType.xContent()); restRequest.setEntity(new NByteArrayEntity(requestBody, createContentType(contentType))); @@ -164,7 +177,7 @@ private static void ensureSearchTaskIsCancelled(String transportAction, Function TaskManager taskManager = internalCluster().getInstance(TransportService.class, nodeName).getTaskManager(); Task task = taskManager.getTask(taskId.getId()); assertThat(task, instanceOf(CancellableTask.class)); - assertTrue(((CancellableTask)task).isCancelled()); + assertTrue(((CancellableTask) task).isCancelled()); }); } diff --git a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/SystemIndexRestIT.java b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/SystemIndexRestIT.java index e9ba48f5c2851..f2245b5830aba 100644 --- a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/SystemIndexRestIT.java +++ b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/SystemIndexRestIT.java @@ -23,7 +23,6 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsFilter; import org.elasticsearch.common.util.CollectionUtils; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.indices.SystemIndexDescriptor; import org.elasticsearch.indices.SystemIndexDescriptor.Type; import org.elasticsearch.plugins.Plugin; @@ -33,6 +32,7 @@ import org.elasticsearch.rest.RestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestStatusToXContentListener; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.io.UncheckedIOException; @@ -42,9 +42,9 @@ import java.util.Map; import java.util.function.Supplier; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.rest.RestRequest.Method.POST; import static org.elasticsearch.test.rest.ESRestTestCase.entityAsMap; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasKey; import static org.hamcrest.Matchers.is; @@ -64,12 +64,16 @@ public void testSystemIndexAccessBlockedByDefault() throws Exception { assertThat(resp.getStatusLine().getStatusCode(), equalTo(201)); } - // make sure the system index now exists assertBusy(() -> { Request searchRequest = new Request("GET", "/" + SystemIndexTestPlugin.SYSTEM_INDEX_NAME + "/_count"); - searchRequest.setOptions(expectWarnings("this request accesses system indices: [" + SystemIndexTestPlugin.SYSTEM_INDEX_NAME + - "], but in a future major version, direct access to system indices will be prevented by default")); + searchRequest.setOptions( + expectWarnings( + "this request accesses system indices: [" + + SystemIndexTestPlugin.SYSTEM_INDEX_NAME + + "], but in a future major version, direct access to system indices will be prevented by default" + ) + ); // Disallow no indices to cause an exception if the flag above doesn't work searchRequest.addParameter("allow_no_indices", "false"); @@ -90,8 +94,10 @@ public void testSystemIndexAccessBlockedByDefault() throws Exception { // Try to index a doc directly { - String expectedWarning = "this request accesses system indices: [" + SystemIndexTestPlugin.SYSTEM_INDEX_NAME + "], but in a " + - "future major version, direct access to system indices will be prevented by default"; + String expectedWarning = "this request accesses system indices: [" + + SystemIndexTestPlugin.SYSTEM_INDEX_NAME + + "], but in a " + + "future major version, direct access to system indices will be prevented by default"; Request putDocDirectlyRequest = new Request("PUT", "/" + SystemIndexTestPlugin.SYSTEM_INDEX_NAME + "/_doc/43"); putDocDirectlyRequest.setJsonEntity("{\"some_field\": \"some_other_value\"}"); putDocDirectlyRequest.setOptions(expectWarnings(expectedWarning)); @@ -101,8 +107,10 @@ public void testSystemIndexAccessBlockedByDefault() throws Exception { } private void assertDeprecationWarningOnAccess(String queryPattern, String warningIndexName) throws IOException { - String expectedWarning = "this request accesses system indices: [" + warningIndexName + "], but in a " + - "future major version, direct access to system indices will be prevented by default"; + String expectedWarning = "this request accesses system indices: [" + + warningIndexName + + "], but in a " + + "future major version, direct access to system indices will be prevented by default"; Request searchRequest = new Request("GET", "/" + queryPattern + randomFrom("/_count", "/_search")); searchRequest.setJsonEntity("{\"query\": {\"match\": {\"some_field\": \"some_value\"}}}"); // Disallow no indices to cause an exception if this resolves to zero indices, so that we're sure it resolved the index @@ -114,12 +122,9 @@ private void assertDeprecationWarningOnAccess(String queryPattern, String warnin } private RequestOptions expectWarnings(String expectedWarning) { - return RequestOptions.DEFAULT.toBuilder() - .setWarningsHandler(w -> w.contains(expectedWarning) == false || w.size() != 1) - .build(); + return RequestOptions.DEFAULT.toBuilder().setWarningsHandler(w -> w.contains(expectedWarning) == false || w.size() != 1).build(); } - public static class SystemIndexTestPlugin extends Plugin implements SystemIndexPlugin { public static final String SYSTEM_INDEX_NAME = ".test-system-idx"; @@ -131,10 +136,15 @@ public static class SystemIndexTestPlugin extends Plugin implements SystemIndexP .build(); @Override - public List getRestHandlers(Settings settings, RestController restController, ClusterSettings clusterSettings, - IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter, - IndexNameExpressionResolver indexNameExpressionResolver, - Supplier nodesInCluster) { + public List getRestHandlers( + Settings settings, + RestController restController, + ClusterSettings clusterSettings, + IndexScopedSettings indexScopedSettings, + SettingsFilter settingsFilter, + IndexNameExpressionResolver indexNameExpressionResolver, + Supplier nodesInCluster + ) { return List.of(new AddDocRestHandler()); } @@ -158,16 +168,17 @@ public Collection getSystemIndexDescriptors(Settings sett } builder.endObject(); - return Collections.singletonList(SystemIndexDescriptor.builder() - .setIndexPattern(SYSTEM_INDEX_NAME + "*") - .setPrimaryIndex(SYSTEM_INDEX_NAME) - .setDescription("Test system index") - .setOrigin(getClass().getName()) - .setVersionMetaKey("version") - .setMappings(builder) - .setSettings(SETTINGS) - .setType(Type.INTERNAL_MANAGED) - .build() + return Collections.singletonList( + SystemIndexDescriptor.builder() + .setIndexPattern(SYSTEM_INDEX_NAME + "*") + .setPrimaryIndex(SYSTEM_INDEX_NAME) + .setDescription("Test system index") + .setOrigin(getClass().getName()) + .setVersionMetaKey("version") + .setMappings(builder) + .setSettings(SETTINGS) + .setType(Type.INTERNAL_MANAGED) + .build() ); } catch (IOException e) { throw new UncheckedIOException("Failed to build " + SYSTEM_INDEX_NAME + " index mappings", e); @@ -206,8 +217,10 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli indexRequest.id(request.param("id")); indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); indexRequest.source(Map.of("some_field", "some_value")); - return channel -> client.index(indexRequest, - new RestStatusToXContentListener<>(channel, r -> r.getLocation(indexRequest.routing()))); + return channel -> client.index( + indexRequest, + new RestStatusToXContentListener<>(channel, r -> r.getLocation(indexRequest.routing())) + ); } } } diff --git a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/TestResponseHeaderPlugin.java b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/TestResponseHeaderPlugin.java index f657bb02a503d..6a1f598c5e529 100644 --- a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/TestResponseHeaderPlugin.java +++ b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/TestResponseHeaderPlugin.java @@ -26,9 +26,15 @@ public class TestResponseHeaderPlugin extends Plugin implements ActionPlugin { @Override - public List getRestHandlers(Settings settings, RestController restController, ClusterSettings clusterSettings, - IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter, IndexNameExpressionResolver indexNameExpressionResolver, - Supplier nodesInCluster) { + public List getRestHandlers( + Settings settings, + RestController restController, + ClusterSettings clusterSettings, + IndexScopedSettings indexScopedSettings, + SettingsFilter settingsFilter, + IndexNameExpressionResolver indexNameExpressionResolver, + Supplier nodesInCluster + ) { return singletonList(new TestResponseHeaderRestAction()); } } diff --git a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/snapshots/AbstractSnapshotRestTestCase.java b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/snapshots/AbstractSnapshotRestTestCase.java index 6e391a199a9b4..12cfdd0cd423b 100644 --- a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/snapshots/AbstractSnapshotRestTestCase.java +++ b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/snapshots/AbstractSnapshotRestTestCase.java @@ -24,8 +24,10 @@ public abstract class AbstractSnapshotRestTestCase extends HttpSmokeTestCase { * We use single threaded metadata fetching in some tests to make sure that once the snapshot meta thread is stuck on a blocked repo, * no other snapshot meta thread can concurrently finish a request/task */ - protected static final Settings SINGLE_THREADED_SNAPSHOT_META_SETTINGS = - Settings.builder().put("thread_pool.snapshot_meta.core", 1).put("thread_pool.snapshot_meta.max", 1).build(); + protected static final Settings SINGLE_THREADED_SNAPSHOT_META_SETTINGS = Settings.builder() + .put("thread_pool.snapshot_meta.core", 1) + .put("thread_pool.snapshot_meta.max", 1) + .build(); @Override protected Collection> nodePlugins() { diff --git a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/snapshots/RestGetSnapshotsIT.java b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/snapshots/RestGetSnapshotsIT.java index 545cc57da4e78..f74d1c1a57fb6 100644 --- a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/snapshots/RestGetSnapshotsIT.java +++ b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/snapshots/RestGetSnapshotsIT.java @@ -17,15 +17,15 @@ import org.elasticsearch.client.Response; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.DeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.snapshots.AbstractSnapshotIntegTestCase; import org.elasticsearch.snapshots.SnapshotInfo; import org.elasticsearch.snapshots.SnapshotsService; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.DeprecationHandler; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.json.JsonXContent; import java.io.IOException; import java.io.InputStream; @@ -48,21 +48,28 @@ public class RestGetSnapshotsIT extends AbstractSnapshotRestTestCase { @Override protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { - return Settings.builder().put(super.nodeSettings(nodeOrdinal, otherSettings)) - .put(ThreadPool.ESTIMATED_TIME_INTERVAL_SETTING.getKey(), 0) // We have tests that check by-timestamp order - .build(); + return Settings.builder() + .put(super.nodeSettings(nodeOrdinal, otherSettings)) + .put(ThreadPool.ESTIMATED_TIME_INTERVAL_SETTING.getKey(), 0) // We have tests that check by-timestamp order + .build(); } public void testSortOrder() throws Exception { final String repoName = "test-repo"; AbstractSnapshotIntegTestCase.createRepository(logger, repoName, "fs"); - final List snapshotNamesWithoutIndex = - AbstractSnapshotIntegTestCase.createNSnapshots(logger, repoName, randomIntBetween(3, 20)); + final List snapshotNamesWithoutIndex = AbstractSnapshotIntegTestCase.createNSnapshots( + logger, + repoName, + randomIntBetween(3, 20) + ); createIndexWithContent("test-index"); - final List snapshotNamesWithIndex = - AbstractSnapshotIntegTestCase.createNSnapshots(logger, repoName, randomIntBetween(3, 20)); + final List snapshotNamesWithIndex = AbstractSnapshotIntegTestCase.createNSnapshots( + logger, + repoName, + randomIntBetween(3, 20) + ); final Collection allSnapshotNames = new HashSet<>(snapshotNamesWithIndex); allSnapshotNames.addAll(snapshotNamesWithoutIndex); @@ -74,39 +81,39 @@ private void doTestSortOrder(String repoName, Collection allSnapshotName final List defaultSorting = clusterAdmin().prepareGetSnapshots(repoName).setOrder(order).get().getSnapshots(); assertSnapshotListSorted(defaultSorting, null, order); assertSnapshotListSorted( - allSnapshotsSorted(allSnapshotNames, repoName, GetSnapshotsRequest.SortBy.NAME, order), - GetSnapshotsRequest.SortBy.NAME, - order + allSnapshotsSorted(allSnapshotNames, repoName, GetSnapshotsRequest.SortBy.NAME, order), + GetSnapshotsRequest.SortBy.NAME, + order ); assertSnapshotListSorted( - allSnapshotsSorted(allSnapshotNames, repoName, GetSnapshotsRequest.SortBy.DURATION, order), - GetSnapshotsRequest.SortBy.DURATION, - order + allSnapshotsSorted(allSnapshotNames, repoName, GetSnapshotsRequest.SortBy.DURATION, order), + GetSnapshotsRequest.SortBy.DURATION, + order ); assertSnapshotListSorted( - allSnapshotsSorted(allSnapshotNames, repoName, GetSnapshotsRequest.SortBy.INDICES, order), - GetSnapshotsRequest.SortBy.INDICES, - order + allSnapshotsSorted(allSnapshotNames, repoName, GetSnapshotsRequest.SortBy.INDICES, order), + GetSnapshotsRequest.SortBy.INDICES, + order ); assertSnapshotListSorted( - allSnapshotsSorted(allSnapshotNames, repoName, GetSnapshotsRequest.SortBy.START_TIME, order), - GetSnapshotsRequest.SortBy.START_TIME, - order + allSnapshotsSorted(allSnapshotNames, repoName, GetSnapshotsRequest.SortBy.START_TIME, order), + GetSnapshotsRequest.SortBy.START_TIME, + order ); assertSnapshotListSorted( - allSnapshotsSorted(allSnapshotNames, repoName, GetSnapshotsRequest.SortBy.SHARDS, order), - GetSnapshotsRequest.SortBy.SHARDS, - order + allSnapshotsSorted(allSnapshotNames, repoName, GetSnapshotsRequest.SortBy.SHARDS, order), + GetSnapshotsRequest.SortBy.SHARDS, + order ); assertSnapshotListSorted( - allSnapshotsSorted(allSnapshotNames, repoName, GetSnapshotsRequest.SortBy.FAILED_SHARDS, order), - GetSnapshotsRequest.SortBy.FAILED_SHARDS, - order + allSnapshotsSorted(allSnapshotNames, repoName, GetSnapshotsRequest.SortBy.FAILED_SHARDS, order), + GetSnapshotsRequest.SortBy.FAILED_SHARDS, + order ); assertSnapshotListSorted( - allSnapshotsSorted(allSnapshotNames, repoName, GetSnapshotsRequest.SortBy.REPOSITORY, order), - GetSnapshotsRequest.SortBy.REPOSITORY, - order + allSnapshotsSorted(allSnapshotNames, repoName, GetSnapshotsRequest.SortBy.REPOSITORY, order), + GetSnapshotsRequest.SortBy.REPOSITORY, + order ); } @@ -122,10 +129,8 @@ public void testResponseSizeLimit() throws Exception { } } - private void doTestPagination(String repoName, - List names, - GetSnapshotsRequest.SortBy sort, - SortOrder order) throws IOException { + private void doTestPagination(String repoName, List names, GetSnapshotsRequest.SortBy sort, SortOrder order) + throws IOException { final List allSnapshotsSorted = allSnapshotsSorted(names, repoName, sort, order); final GetSnapshotsResponse batch1 = sortedWithLimit(repoName, sort, null, 2, order); assertEquals(allSnapshotsSorted.subList(0, 2), batch1.getSnapshots()); @@ -137,21 +142,15 @@ private void doTestPagination(String repoName, batch3.getSnapshots(), allSnapshotsSorted.subList(batch1.getSnapshots().size() + batch2.getSnapshots().size(), names.size()) ); - final GetSnapshotsResponse batch3NoLimit = sortedWithLimit( - repoName, - sort, - batch2.next(), - GetSnapshotsRequest.NO_LIMIT, - order - ); + final GetSnapshotsResponse batch3NoLimit = sortedWithLimit(repoName, sort, batch2.next(), GetSnapshotsRequest.NO_LIMIT, order); assertNull(batch3NoLimit.next()); assertEquals(batch3.getSnapshots(), batch3NoLimit.getSnapshots()); final GetSnapshotsResponse batch3LargeLimit = sortedWithLimit( - repoName, - sort, - batch2.next(), - lastBatch + randomIntBetween(1, 100), - order + repoName, + sort, + batch2.next(), + lastBatch + randomIntBetween(1, 100), + order ); assertEquals(batch3.getSnapshots(), batch3LargeLimit.getSnapshots()); assertNull(batch3LargeLimit.next()); @@ -161,8 +160,9 @@ private void doTestPagination(String repoName, public void testSortAndPaginateWithInProgress() throws Exception { final String repoName = "test-repo"; AbstractSnapshotIntegTestCase.createRepository(logger, repoName, "mock"); - final Collection allSnapshotNames = - new HashSet<>(AbstractSnapshotIntegTestCase.createNSnapshots(logger, repoName, randomIntBetween(3, 20))); + final Collection allSnapshotNames = new HashSet<>( + AbstractSnapshotIntegTestCase.createNSnapshots(logger, repoName, randomIntBetween(3, 20)) + ); createIndexWithContent("test-index-1"); allSnapshotNames.addAll(AbstractSnapshotIntegTestCase.createNSnapshots(logger, repoName, randomIntBetween(3, 20))); createIndexWithContent("test-index-2"); @@ -195,8 +195,11 @@ public void testFilterBySLMPolicy() throws Exception { final String repoName = "test-repo"; AbstractSnapshotIntegTestCase.createRepository(logger, repoName, "fs"); AbstractSnapshotIntegTestCase.createNSnapshots(logger, repoName, randomIntBetween(1, 5)); - final List snapshotsWithoutPolicy = clusterAdmin().prepareGetSnapshots("*").setSnapshots("*") - .setSort(GetSnapshotsRequest.SortBy.NAME).get().getSnapshots(); + final List snapshotsWithoutPolicy = clusterAdmin().prepareGetSnapshots("*") + .setSnapshots("*") + .setSort(GetSnapshotsRequest.SortBy.NAME) + .get() + .getSnapshots(); final String snapshotWithPolicy = "snapshot-with-policy"; final String policyName = "some-policy"; final SnapshotInfo withPolicy = AbstractSnapshotIntegTestCase.assertSuccessful( @@ -211,8 +214,7 @@ public void testFilterBySLMPolicy() throws Exception { assertThat(getAllSnapshotsForPolicies("some-*"), is(List.of(withPolicy))); assertThat(getAllSnapshotsForPolicies("*", "-" + policyName), empty()); assertThat(getAllSnapshotsForPolicies(GetSnapshotsRequest.NO_POLICY_PATTERN), is(snapshotsWithoutPolicy)); - assertThat( - getAllSnapshotsForPolicies(GetSnapshotsRequest.NO_POLICY_PATTERN, "-" + policyName), is(snapshotsWithoutPolicy)); + assertThat(getAllSnapshotsForPolicies(GetSnapshotsRequest.NO_POLICY_PATTERN, "-" + policyName), is(snapshotsWithoutPolicy)); assertThat(getAllSnapshotsForPolicies(GetSnapshotsRequest.NO_POLICY_PATTERN), is(snapshotsWithoutPolicy)); assertThat(getAllSnapshotsForPolicies(GetSnapshotsRequest.NO_POLICY_PATTERN, "-*"), is(snapshotsWithoutPolicy)); assertThat(getAllSnapshotsForPolicies("no-such-policy"), empty()); @@ -231,18 +233,12 @@ public void testFilterBySLMPolicy() throws Exception { assertThat(getAllSnapshotsForPolicies(policyName, otherPolicyName), is(List.of(withOtherPolicy, withPolicy))); assertThat(getAllSnapshotsForPolicies(policyName, otherPolicyName, "no-such-policy*"), is(List.of(withOtherPolicy, withPolicy))); final List allSnapshots = clusterAdmin().prepareGetSnapshots("*") - .setSnapshots("*") - .setSort(GetSnapshotsRequest.SortBy.NAME) - .get() - .getSnapshots(); - assertThat( - getAllSnapshotsForPolicies(GetSnapshotsRequest.NO_POLICY_PATTERN, policyName, otherPolicyName), - is(allSnapshots) - ); - assertThat( - getAllSnapshotsForPolicies(GetSnapshotsRequest.NO_POLICY_PATTERN, "*"), - is(allSnapshots) - ); + .setSnapshots("*") + .setSort(GetSnapshotsRequest.SortBy.NAME) + .get() + .getSnapshots(); + assertThat(getAllSnapshotsForPolicies(GetSnapshotsRequest.NO_POLICY_PATTERN, policyName, otherPolicyName), is(allSnapshots)); + assertThat(getAllSnapshotsForPolicies(GetSnapshotsRequest.NO_POLICY_PATTERN, "*"), is(allSnapshots)); } public void testSortAfterStartTime() throws Exception { @@ -254,10 +250,10 @@ public void testSortAfterStartTime() throws Exception { final SnapshotInfo snapshot3 = createFullSnapshotWithUniqueStartTime(repoName, "snapshot-3", startTimes); final List allSnapshotInfo = clusterAdmin().prepareGetSnapshots(matchAllPattern()) - .setSnapshots(matchAllPattern()) - .setSort(GetSnapshotsRequest.SortBy.START_TIME) - .get() - .getSnapshots(); + .setSnapshots(matchAllPattern()) + .setSort(GetSnapshotsRequest.SortBy.START_TIME) + .get() + .getSnapshots(); assertThat(allSnapshotInfo, is(List.of(snapshot1, snapshot2, snapshot3))); final long startTime1 = snapshot1.startTime(); @@ -271,11 +267,11 @@ public void testSortAfterStartTime() throws Exception { assertThat(allAfterStartTimeAscending(startTime3 + 1), empty()); final List allSnapshotInfoDesc = clusterAdmin().prepareGetSnapshots(matchAllPattern()) - .setSnapshots(matchAllPattern()) - .setSort(GetSnapshotsRequest.SortBy.START_TIME) - .setOrder(SortOrder.DESC) - .get() - .getSnapshots(); + .setSnapshots(matchAllPattern()) + .setSort(GetSnapshotsRequest.SortBy.START_TIME) + .setOrder(SortOrder.DESC) + .get() + .getSnapshots(); assertThat(allSnapshotInfoDesc, is(List.of(snapshot3, snapshot2, snapshot1))); assertThat(allBeforeStartTimeDescending(startTime3 + 1), is(allSnapshotInfoDesc)); @@ -330,9 +326,8 @@ private void createIndexWithContent(String indexName) { indexDoc(indexName, "some_id", "foo", "bar"); } - private static void assertStablePagination(String repoName, - Collection allSnapshotNames, - GetSnapshotsRequest.SortBy sort) throws IOException { + private static void assertStablePagination(String repoName, Collection allSnapshotNames, GetSnapshotsRequest.SortBy sort) + throws IOException { final SortOrder order = randomFrom(SortOrder.values()); final List allSorted = allSnapshotsSorted(allSnapshotNames, repoName, sort, order); @@ -344,8 +339,13 @@ private static void assertStablePagination(String repoName, for (int j = 0; j < allSnapshotNames.size(); j++) { final SnapshotInfo after = allSorted.get(j); for (int i = 1; i < allSnapshotNames.size() - j; i++) { - final GetSnapshotsResponse getSnapshotsResponse = - sortedWithLimit(repoName, sort, GetSnapshotsRequest.After.from(after, sort).asQueryParam(), i, order); + final GetSnapshotsResponse getSnapshotsResponse = sortedWithLimit( + repoName, + sort, + GetSnapshotsRequest.After.from(after, sort).asQueryParam(), + i, + order + ); final GetSnapshotsResponse getSnapshotsResponseNumeric = sortedWithLimit(repoName, sort, j + 1, i, order); final List subsetSorted = getSnapshotsResponse.getSnapshots(); assertEquals(subsetSorted, getSnapshotsResponseNumeric.getSnapshots()); @@ -358,10 +358,12 @@ private static void assertStablePagination(String repoName, } } - private static List allSnapshotsSorted(Collection allSnapshotNames, - String repoName, - GetSnapshotsRequest.SortBy sortBy, - SortOrder order) throws IOException { + private static List allSnapshotsSorted( + Collection allSnapshotNames, + String repoName, + GetSnapshotsRequest.SortBy sortBy, + SortOrder order + ) throws IOException { final Request request = baseGetSnapshotsRequest(repoName); request.addParameter("sort", sortBy.toString()); if (order == SortOrder.DESC || randomBoolean()) { @@ -383,18 +385,25 @@ private static Request baseGetSnapshotsRequest(String repoName) { } private static GetSnapshotsResponse readSnapshotInfos(Response response) throws IOException { - try (InputStream input = response.getEntity().getContent(); - XContentParser parser = JsonXContent.jsonXContent.createParser( - NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, input)) { + try ( + InputStream input = response.getEntity().getContent(); + XContentParser parser = JsonXContent.jsonXContent.createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + input + ) + ) { return GetSnapshotsResponse.fromXContent(parser); } } - private static GetSnapshotsResponse sortedWithLimit(String repoName, - GetSnapshotsRequest.SortBy sortBy, - String after, - int size, - SortOrder order) throws IOException { + private static GetSnapshotsResponse sortedWithLimit( + String repoName, + GetSnapshotsRequest.SortBy sortBy, + String after, + int size, + SortOrder order + ) throws IOException { final Request request = baseGetSnapshotsRequest(repoName); request.addParameter("sort", sortBy.toString()); if (size != GetSnapshotsRequest.NO_LIMIT || randomBoolean()) { @@ -410,11 +419,13 @@ private static GetSnapshotsResponse sortedWithLimit(String repoName, return readSnapshotInfos(response); } - private static GetSnapshotsResponse sortedWithLimit(String repoName, - GetSnapshotsRequest.SortBy sortBy, - int offset, - int size, - SortOrder order) throws IOException { + private static GetSnapshotsResponse sortedWithLimit( + String repoName, + GetSnapshotsRequest.SortBy sortBy, + int offset, + int size, + SortOrder order + ) throws IOException { final Request request = baseGetSnapshotsRequest(repoName); request.addParameter("sort", sortBy.toString()); if (size != GetSnapshotsRequest.NO_LIMIT || randomBoolean()) { diff --git a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/snapshots/RestSnapshotsStatusCancellationIT.java b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/snapshots/RestSnapshotsStatusCancellationIT.java index b7f147862eca2..840fcec3c4a20 100644 --- a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/snapshots/RestSnapshotsStatusCancellationIT.java +++ b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/snapshots/RestSnapshotsStatusCancellationIT.java @@ -44,7 +44,9 @@ public void testSnapshotStatusCancellation() throws Exception { final Request request = new Request( HttpGet.METHOD_NAME, - "/_snapshot/" + repoName + "/" + "/_snapshot/" + + repoName + + "/" + String.join(",", randomSubsetOf(randomIntBetween(1, snapshotCount), snapshotNames)) + "/_status" ); diff --git a/qa/smoke-test-ingest-with-all-dependencies/src/test/java/org/elasticsearch/ingest/IngestDocumentMustacheIT.java b/qa/smoke-test-ingest-with-all-dependencies/src/test/java/org/elasticsearch/ingest/IngestDocumentMustacheIT.java index ed2d13f44d822..a77785333bc70 100644 --- a/qa/smoke-test-ingest-with-all-dependencies/src/test/java/org/elasticsearch/ingest/IngestDocumentMustacheIT.java +++ b/qa/smoke-test-ingest-with-all-dependencies/src/test/java/org/elasticsearch/ingest/IngestDocumentMustacheIT.java @@ -38,12 +38,13 @@ public void testAccessMapMetadataViaTemplate() { innerObject.put("qux", Collections.singletonMap("fubar", "hello qux and fubar")); document.put("foo", innerObject); IngestDocument ingestDocument = new IngestDocument("index", "id", null, null, null, document); - ingestDocument.setFieldValue(compile("field1"), - ValueSource.wrap("1 {{foo.bar}} {{foo.baz}} {{foo.qux.fubar}}", scriptService)); + ingestDocument.setFieldValue(compile("field1"), ValueSource.wrap("1 {{foo.bar}} {{foo.baz}} {{foo.qux.fubar}}", scriptService)); assertThat(ingestDocument.getFieldValue("field1", String.class), equalTo("1 hello bar hello baz hello qux and fubar")); - ingestDocument.setFieldValue(compile("field1"), - ValueSource.wrap("2 {{_source.foo.bar}} {{_source.foo.baz}} {{_source.foo.qux.fubar}}", scriptService)); + ingestDocument.setFieldValue( + compile("field1"), + ValueSource.wrap("2 {{_source.foo.bar}} {{_source.foo.baz}} {{_source.foo.qux.fubar}}", scriptService) + ); assertThat(ingestDocument.getFieldValue("field1", String.class), equalTo("2 hello bar hello baz hello qux and fubar")); } @@ -67,9 +68,13 @@ public void testAccessIngestMetadataViaTemplate() { ingestMap.put("timestamp", "bogus_timestamp"); document.put("_ingest", ingestMap); IngestDocument ingestDocument = new IngestDocument("index", "id", null, null, null, document); - ingestDocument.setFieldValue(compile("ingest_timestamp"), - ValueSource.wrap("{{_ingest.timestamp}} and {{_source._ingest.timestamp}}", scriptService)); - assertThat(ingestDocument.getFieldValue("ingest_timestamp", String.class), - equalTo(ingestDocument.getIngestMetadata().get("timestamp") + " and bogus_timestamp")); + ingestDocument.setFieldValue( + compile("ingest_timestamp"), + ValueSource.wrap("{{_ingest.timestamp}} and {{_source._ingest.timestamp}}", scriptService) + ); + assertThat( + ingestDocument.getFieldValue("ingest_timestamp", String.class), + equalTo(ingestDocument.getIngestMetadata().get("timestamp") + " and bogus_timestamp") + ); } } diff --git a/qa/smoke-test-ingest-with-all-dependencies/src/test/java/org/elasticsearch/ingest/ValueSourceMustacheIT.java b/qa/smoke-test-ingest-with-all-dependencies/src/test/java/org/elasticsearch/ingest/ValueSourceMustacheIT.java index 5e49fcba772ab..15cf58ee3a660 100644 --- a/qa/smoke-test-ingest-with-all-dependencies/src/test/java/org/elasticsearch/ingest/ValueSourceMustacheIT.java +++ b/qa/smoke-test-ingest-with-all-dependencies/src/test/java/org/elasticsearch/ingest/ValueSourceMustacheIT.java @@ -70,8 +70,10 @@ public void testWithConfigurableEncoders() { // default encoder should be application/json ValueSource valueSource = ValueSource.wrap("{{log_line}}", scriptService); Object result = valueSource.copyAndResolve(model); - assertThat(result, - equalTo("10.10.1.1 - - [17/Nov/2020:04:59:43 +0000] \\\"GET /info HTTP/1.1\\\" 200 6229 \\\"-\\\" \\\"-\\\" 2")); + assertThat( + result, + equalTo("10.10.1.1 - - [17/Nov/2020:04:59:43 +0000] \\\"GET /info HTTP/1.1\\\" 200 6229 \\\"-\\\" \\\"-\\\" 2") + ); // text/plain encoder var scriptOptions = Map.of(Script.CONTENT_TYPE_OPTION, "text/plain"); @@ -83,7 +85,11 @@ public void testWithConfigurableEncoders() { scriptOptions = Map.of(Script.CONTENT_TYPE_OPTION, "application/x-www-form-urlencoded"); valueSource = ValueSource.wrap("{{log_line}}", scriptService, scriptOptions); result = valueSource.copyAndResolve(model); - assertThat(result, equalTo("10.10.1.1+-+-+%5B17%2FNov%2F2020%3A04%3A59%3A43+%2B0000%5D+%22GET+%2Finfo+HTTP%2F1.1%22+200" + - "+6229+%22-%22+%22-%22++2")); + assertThat( + result, + equalTo( + "10.10.1.1+-+-+%5B17%2FNov%2F2020%3A04%3A59%3A43+%2B0000%5D+%22GET+%2Finfo+HTTP%2F1.1%22+200" + "+6229+%22-%22+%22-%22++2" + ) + ); } } diff --git a/qa/smoke-test-multinode/src/test/java/org/elasticsearch/smoketest/SmokeTestMultiNodeClientYamlTestSuiteIT.java b/qa/smoke-test-multinode/src/test/java/org/elasticsearch/smoketest/SmokeTestMultiNodeClientYamlTestSuiteIT.java index 1b5e6a8fb44b9..e192a01211d34 100644 --- a/qa/smoke-test-multinode/src/test/java/org/elasticsearch/smoketest/SmokeTestMultiNodeClientYamlTestSuiteIT.java +++ b/qa/smoke-test-multinode/src/test/java/org/elasticsearch/smoketest/SmokeTestMultiNodeClientYamlTestSuiteIT.java @@ -10,8 +10,8 @@ import com.carrotsearch.randomizedtesting.annotations.Name; import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; - import com.carrotsearch.randomizedtesting.annotations.TimeoutSuite; + import org.apache.lucene.util.TimeUnits; import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate; import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase; @@ -28,4 +28,3 @@ public static Iterable parameters() throws Exception { return ESClientYamlSuiteTestCase.createParameters(); } } - diff --git a/qa/smoke-test-plugins/src/test/java/org/elasticsearch/smoketest/SmokeTestPluginsClientYamlTestSuiteIT.java b/qa/smoke-test-plugins/src/test/java/org/elasticsearch/smoketest/SmokeTestPluginsClientYamlTestSuiteIT.java index 2773521b780a6..b5367df0d1aaa 100644 --- a/qa/smoke-test-plugins/src/test/java/org/elasticsearch/smoketest/SmokeTestPluginsClientYamlTestSuiteIT.java +++ b/qa/smoke-test-plugins/src/test/java/org/elasticsearch/smoketest/SmokeTestPluginsClientYamlTestSuiteIT.java @@ -25,4 +25,3 @@ public static Iterable parameters() throws Exception { return ESClientYamlSuiteTestCase.createParameters(); } } - diff --git a/qa/verify-version-constants/src/test/java/org/elasticsearch/qa/verify_version_constants/VerifyVersionConstantsIT.java b/qa/verify-version-constants/src/test/java/org/elasticsearch/qa/verify_version_constants/VerifyVersionConstantsIT.java index ee9e922e66871..2baebb0434c9c 100644 --- a/qa/verify-version-constants/src/test/java/org/elasticsearch/qa/verify_version_constants/VerifyVersionConstantsIT.java +++ b/qa/verify-version-constants/src/test/java/org/elasticsearch/qa/verify_version_constants/VerifyVersionConstantsIT.java @@ -49,8 +49,6 @@ public boolean preserveClusterUponCompletion() { @Override protected Settings restClientSettings() { String token = basicAuthHeaderValue("admin", new SecureString("admin-password".toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } } diff --git a/rest-api-spec/src/yamlRestTest/java/org/elasticsearch/test/rest/ClientYamlTestSuiteIT.java b/rest-api-spec/src/yamlRestTest/java/org/elasticsearch/test/rest/ClientYamlTestSuiteIT.java index 3e03285926a7b..795975a175aeb 100644 --- a/rest-api-spec/src/yamlRestTest/java/org/elasticsearch/test/rest/ClientYamlTestSuiteIT.java +++ b/rest-api-spec/src/yamlRestTest/java/org/elasticsearch/test/rest/ClientYamlTestSuiteIT.java @@ -11,13 +11,14 @@ import com.carrotsearch.randomizedtesting.annotations.Name; import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; import com.carrotsearch.randomizedtesting.annotations.TimeoutSuite; + import org.apache.lucene.util.TimeUnits; import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate; import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase; /** Rest integration test. Runs against a cluster started by {@code gradle integTest} */ -//The default 20 minutes timeout isn't always enough, but Darwin CI hosts are incredibly slow... +// The default 20 minutes timeout isn't always enough, but Darwin CI hosts are incredibly slow... @TimeoutSuite(millis = 40 * TimeUnits.MINUTE) public class ClientYamlTestSuiteIT extends ESClientYamlSuiteTestCase { diff --git a/test/fixtures/geoip-fixture/src/main/java/fixture/geoip/GeoIpHttpFixture.java b/test/fixtures/geoip-fixture/src/main/java/fixture/geoip/GeoIpHttpFixture.java index c195c0a59fb53..cf2cf8bc4f9fe 100644 --- a/test/fixtures/geoip-fixture/src/main/java/fixture/geoip/GeoIpHttpFixture.java +++ b/test/fixtures/geoip-fixture/src/main/java/fixture/geoip/GeoIpHttpFixture.java @@ -9,6 +9,7 @@ package fixture.geoip; import com.sun.net.httpserver.HttpServer; + import org.elasticsearch.cli.Terminal; import org.elasticsearch.geoip.GeoIpCli; @@ -45,8 +46,10 @@ public class GeoIpHttpFixture { this.server.createContext("/db", exchange -> { exchange.sendResponseHeaders(200, 0); String dbName = exchange.getRequestURI().getPath().replaceAll(".*/db", ""); - try (OutputStream outputStream = exchange.getResponseBody(); - InputStream db = GeoIpHttpFixture.class.getResourceAsStream(dbName)) { + try ( + OutputStream outputStream = exchange.getResponseBody(); + InputStream db = GeoIpHttpFixture.class.getResourceAsStream(dbName) + ) { db.transferTo(outputStream); } }); @@ -54,8 +57,7 @@ public class GeoIpHttpFixture { String fileName = exchange.getRequestURI().getPath().replaceAll(".*/cli/", ""); Path target = Path.of("target").resolve(fileName); if (Files.isRegularFile(target)) { - try (OutputStream outputStream = exchange.getResponseBody(); - InputStream db = Files.newInputStream(target)) { + try (OutputStream outputStream = exchange.getResponseBody(); InputStream db = Files.newInputStream(target)) { exchange.sendResponseHeaders(200, 0); db.transferTo(outputStream); } catch (Exception e) { @@ -80,8 +82,10 @@ private void copyFiles() throws Exception { Files.copy(GeoIpHttpFixture.class.getResourceAsStream("/GeoLite2-City.mmdb"), source.resolve("GeoLite2-City.mmdb")); Files.copy(GeoIpHttpFixture.class.getResourceAsStream("/GeoLite2-Country.mmdb"), source.resolve("GeoLite2-Country.mmdb")); - new GeoIpCli().main(new String[]{"-s", source.toAbsolutePath().toString(), "-t", target.toAbsolutePath().toString()}, - Terminal.DEFAULT); + new GeoIpCli().main( + new String[] { "-s", source.toAbsolutePath().toString(), "-t", target.toAbsolutePath().toString() }, + Terminal.DEFAULT + ); } final void start() throws Exception { diff --git a/test/fixtures/old-elasticsearch/src/main/java/oldes/OldElasticsearch.java b/test/fixtures/old-elasticsearch/src/main/java/oldes/OldElasticsearch.java index f50344aac41f6..5e873cc106f33 100644 --- a/test/fixtures/old-elasticsearch/src/main/java/oldes/OldElasticsearch.java +++ b/test/fixtures/old-elasticsearch/src/main/java/oldes/OldElasticsearch.java @@ -40,16 +40,16 @@ public static void main(String[] args) throws IOException { // 0.90 must be explicitly foregrounded boolean explicitlyForeground; switch (args[2]) { - case "true": - explicitlyForeground = true; - break; - case "false": - explicitlyForeground = false; - break; - default: - System.err.println("the third argument must be true or false"); - System.exit(1); - return; + case "true": + explicitlyForeground = true; + break; + case "false": + explicitlyForeground = false; + break; + default: + System.err.println("the third argument must be true or false"); + System.exit(1); + return; } Iterator children = Files.list(unzipDir).iterator(); @@ -59,8 +59,13 @@ public static void main(String[] args) throws IOException { } Path esDir = children.next(); if (children.hasNext()) { - System.err.println("expected the es directory to contains a single child directory but contained [" + esDir + "] and [" - + children.next() + "]."); + System.err.println( + "expected the es directory to contains a single child directory but contained [" + + esDir + + "] and [" + + children.next() + + "]." + ); System.exit(1); } if (false == Files.isDirectory(esDir)) { @@ -97,7 +102,8 @@ public static void main(String[] args) throws IOException { Pattern pidPattern = Pattern.compile("pid\\[(\\d+)\\]"); Pattern httpPortPattern = Pattern.compile( - "(\\[http\\s+\\]|Netty4HttpServerTransport|HttpServer).+bound_address.+127\\.0\\.0\\.1:(\\d+)"); + "(\\[http\\s+\\]|Netty4HttpServerTransport|HttpServer).+bound_address.+127\\.0\\.0\\.1:(\\d+)" + ); try (BufferedReader stdout = new BufferedReader(new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8))) { String line; while ((line = stdout.readLine()) != null && (pid == 0 || port == 0)) { diff --git a/test/logger-usage/src/main/java/org/elasticsearch/test/loggerusage/ESLoggerUsageChecker.java b/test/logger-usage/src/main/java/org/elasticsearch/test/loggerusage/ESLoggerUsageChecker.java index 4b860428206fa..2c53ec667cffa 100644 --- a/test/logger-usage/src/main/java/org/elasticsearch/test/loggerusage/ESLoggerUsageChecker.java +++ b/test/logger-usage/src/main/java/org/elasticsearch/test/loggerusage/ESLoggerUsageChecker.java @@ -59,8 +59,7 @@ public class ESLoggerUsageChecker { public static final String IGNORE_CHECKS_ANNOTATION = "org.elasticsearch.common.SuppressLoggerChecks"; // types which are subject to checking when used in logger. TestMessage is also declared here to // make sure this functionality works - public static final Set CUSTOM_MESSAGE_TYPE = Set.of( - Type.getObjectType("org/elasticsearch/common/logging/ESLogMessage")); + public static final Set CUSTOM_MESSAGE_TYPE = Set.of(Type.getObjectType("org/elasticsearch/common/logging/ESLogMessage")); public static final Type PARAMETERIZED_MESSAGE_CLASS = Type.getType(ParameterizedMessage.class); @@ -79,8 +78,7 @@ public static void main(String... args) throws Exception { } } - private static void checkLoggerUsage(Consumer wrongUsageCallback, String... classDirectories) - throws IOException { + private static void checkLoggerUsage(Consumer wrongUsageCallback, String... classDirectories) throws IOException { for (String classDirectory : classDirectories) { Path root = Paths.get(classDirectory); if (Files.isDirectory(root) == false) { @@ -128,13 +126,22 @@ public WrongLoggerUsage(String className, String methodName, String logMethodNam @Override public String toString() { - return "WrongLoggerUsage{" + - "className='" + className + '\'' + - ", methodName='" + methodName + '\'' + - ", logMethodName='" + logMethodName + '\'' + - ", line=" + line + - ", errorMessage='" + errorMessage + '\'' + - '}'; + return "WrongLoggerUsage{" + + "className='" + + className + + '\'' + + ", methodName='" + + methodName + + '\'' + + ", logMethodName='" + + logMethodName + + '\'' + + ", line=" + + line + + ", errorMessage='" + + errorMessage + + '\'' + + '}'; } /** @@ -262,8 +269,17 @@ public void findBadLoggerUsages(MethodNode methodNode) { int lengthWithoutMarker = argumentTypes.length - markerOffset; - verifyLoggerUsage(methodNode, logMessageFrames, arraySizeFrames, lineNumber, i, - methodInsn, argumentTypes, markerOffset, lengthWithoutMarker); + verifyLoggerUsage( + methodNode, + logMessageFrames, + arraySizeFrames, + lineNumber, + i, + methodInsn, + argumentTypes, + markerOffset, + lengthWithoutMarker + ); } } else if (insn.getOpcode() == Opcodes.INVOKESPECIAL) { // constructor invocation MethodInsnNode methodInsn = (MethodInsnNode) insn; @@ -271,45 +287,61 @@ public void findBadLoggerUsages(MethodNode methodNode) { if (CUSTOM_MESSAGE_TYPE.contains(objectType)) { Type[] argumentTypes = Type.getArgumentTypes(methodInsn.desc); - if (argumentTypes.length == 2 && - argumentTypes[0].equals(STRING_CLASS) && - argumentTypes[1].equals(OBJECT_ARRAY_CLASS)) { + if (argumentTypes.length == 2 + && argumentTypes[0].equals(STRING_CLASS) + && argumentTypes[1].equals(OBJECT_ARRAY_CLASS)) { checkArrayArgs(methodNode, logMessageFrames[i], arraySizeFrames[i], lineNumber, methodInsn, 0, 1); } - }else if (objectType.equals(PARAMETERIZED_MESSAGE_CLASS)) { + } else if (objectType.equals(PARAMETERIZED_MESSAGE_CLASS)) { Type[] argumentTypes = Type.getArgumentTypes(methodInsn.desc); - if (argumentTypes.length == 2 && - argumentTypes[0].equals(STRING_CLASS) && - argumentTypes[1].equals(OBJECT_ARRAY_CLASS)) { - checkArrayArgs(methodNode, logMessageFrames[i], arraySizeFrames[i], lineNumber, methodInsn, 0, 1); - } else if (argumentTypes.length == 2 && - argumentTypes[0].equals(STRING_CLASS) && - argumentTypes[1].equals(OBJECT_CLASS)) { - checkFixedArityArgs(methodNode, logMessageFrames[i], lineNumber, methodInsn, 0, 1); - } else if (argumentTypes.length == 3 && - argumentTypes[0].equals(STRING_CLASS) && - argumentTypes[1].equals(OBJECT_CLASS) && - argumentTypes[2].equals(OBJECT_CLASS)) { - checkFixedArityArgs(methodNode, logMessageFrames[i], lineNumber, methodInsn, 0, 2); - } else if (argumentTypes.length == 3 && - argumentTypes[0].equals(STRING_CLASS) && - argumentTypes[1].equals(OBJECT_ARRAY_CLASS) && - argumentTypes[2].equals(THROWABLE_CLASS)) { + if (argumentTypes.length == 2 + && argumentTypes[0].equals(STRING_CLASS) + && argumentTypes[1].equals(OBJECT_ARRAY_CLASS)) { checkArrayArgs(methodNode, logMessageFrames[i], arraySizeFrames[i], lineNumber, methodInsn, 0, 1); - } else if (argumentTypes.length == 3 && - argumentTypes[0].equals(STRING_CLASS) && - argumentTypes[1].equals(STRING_ARRAY_CLASS) && - argumentTypes[2].equals(THROWABLE_CLASS)) { - checkArrayArgs(methodNode, logMessageFrames[i], arraySizeFrames[i], lineNumber, methodInsn, 0, 1); - } else { - throw new IllegalStateException("Constructor invoked on " + objectType + - " that is not supported by logger usage checker"+ - new WrongLoggerUsage(className, methodNode.name, methodInsn.name, lineNumber, - "Constructor: "+ Arrays.toString(argumentTypes))); - } + } else if (argumentTypes.length == 2 + && argumentTypes[0].equals(STRING_CLASS) + && argumentTypes[1].equals(OBJECT_CLASS)) { + checkFixedArityArgs(methodNode, logMessageFrames[i], lineNumber, methodInsn, 0, 1); + } else if (argumentTypes.length == 3 + && argumentTypes[0].equals(STRING_CLASS) + && argumentTypes[1].equals(OBJECT_CLASS) + && argumentTypes[2].equals(OBJECT_CLASS)) { + checkFixedArityArgs(methodNode, logMessageFrames[i], lineNumber, methodInsn, 0, 2); + } else if (argumentTypes.length == 3 + && argumentTypes[0].equals(STRING_CLASS) + && argumentTypes[1].equals(OBJECT_ARRAY_CLASS) + && argumentTypes[2].equals(THROWABLE_CLASS)) { + checkArrayArgs(methodNode, logMessageFrames[i], arraySizeFrames[i], lineNumber, methodInsn, 0, 1); + } else if (argumentTypes.length == 3 + && argumentTypes[0].equals(STRING_CLASS) + && argumentTypes[1].equals(STRING_ARRAY_CLASS) + && argumentTypes[2].equals(THROWABLE_CLASS)) { + checkArrayArgs( + methodNode, + logMessageFrames[i], + arraySizeFrames[i], + lineNumber, + methodInsn, + 0, + 1 + ); + } else { + throw new IllegalStateException( + "Constructor invoked on " + + objectType + + " that is not supported by logger usage checker" + + new WrongLoggerUsage( + className, + methodNode.name, + methodInsn.name, + lineNumber, + "Constructor: " + Arrays.toString(argumentTypes) + ) + ); + } } } else if (insn.getOpcode() == Opcodes.INVOKEVIRTUAL) { - //using strings because this test do not depend on server + // using strings because this test do not depend on server MethodInsnNode methodInsn = (MethodInsnNode) insn; if (methodInsn.owner.equals("org/elasticsearch/common/logging/DeprecationLogger")) { @@ -319,92 +351,164 @@ public void findBadLoggerUsages(MethodNode methodNode) { int lengthWithoutMarker = argumentTypes.length - markerOffset; - verifyLoggerUsage(methodNode, logMessageFrames, arraySizeFrames, lineNumber, i, - methodInsn, argumentTypes, markerOffset, lengthWithoutMarker); + verifyLoggerUsage( + methodNode, + logMessageFrames, + arraySizeFrames, + lineNumber, + i, + methodInsn, + argumentTypes, + markerOffset, + lengthWithoutMarker + ); } } } } } - private void verifyLoggerUsage(MethodNode methodNode, Frame[] logMessageFrames, Frame[] arraySizeFrames, - int lineNumber, int i, MethodInsnNode methodInsn, Type[] argumentTypes, - int markerOffset, int lengthWithoutMarker) { - if (lengthWithoutMarker == 2 && - argumentTypes[markerOffset + 0].equals(STRING_CLASS) && - (argumentTypes[markerOffset + 1].equals(OBJECT_ARRAY_CLASS) || - argumentTypes[markerOffset + 1].equals(SUPPLIER_ARRAY_CLASS))) { + private void verifyLoggerUsage( + MethodNode methodNode, + Frame[] logMessageFrames, + Frame[] arraySizeFrames, + int lineNumber, + int i, + MethodInsnNode methodInsn, + Type[] argumentTypes, + int markerOffset, + int lengthWithoutMarker + ) { + if (lengthWithoutMarker == 2 + && argumentTypes[markerOffset + 0].equals(STRING_CLASS) + && (argumentTypes[markerOffset + 1].equals(OBJECT_ARRAY_CLASS) + || argumentTypes[markerOffset + 1].equals(SUPPLIER_ARRAY_CLASS))) { // VARARGS METHOD: debug(Marker?, String, (Object...|Supplier...)) - checkArrayArgs(methodNode, logMessageFrames[i], arraySizeFrames[i], lineNumber, methodInsn, markerOffset + 0, - markerOffset + 1); - } else if (lengthWithoutMarker >= 2 && - argumentTypes[markerOffset + 0].equals(STRING_CLASS) && - argumentTypes[markerOffset + 1].equals(OBJECT_CLASS)) { - // MULTI-PARAM METHOD: debug(Marker?, String, Object p0, ...) - checkFixedArityArgs(methodNode, logMessageFrames[i], lineNumber, methodInsn, markerOffset + 0, - lengthWithoutMarker - 1); - } else if ((lengthWithoutMarker == 1 || lengthWithoutMarker == 2) && - lengthWithoutMarker == 2 ? argumentTypes[markerOffset + 1].equals(THROWABLE_CLASS) : true) { - // all the rest: debug(Marker?, (Message|MessageSupplier|CharSequence|Object|String|Supplier), Throwable?) - checkFixedArityArgs(methodNode, logMessageFrames[i], lineNumber, methodInsn, markerOffset + 0, 0); - } else { - throw new IllegalStateException("Method invoked on " + LOGGER_CLASS.getClassName() + - " that is not supported by logger usage checker"); - } + checkArrayArgs( + methodNode, + logMessageFrames[i], + arraySizeFrames[i], + lineNumber, + methodInsn, + markerOffset + 0, + markerOffset + 1 + ); + } else if (lengthWithoutMarker >= 2 + && argumentTypes[markerOffset + 0].equals(STRING_CLASS) + && argumentTypes[markerOffset + 1].equals(OBJECT_CLASS)) { + // MULTI-PARAM METHOD: debug(Marker?, String, Object p0, ...) + checkFixedArityArgs(methodNode, logMessageFrames[i], lineNumber, methodInsn, markerOffset + 0, lengthWithoutMarker - 1); + } else if ((lengthWithoutMarker == 1 || lengthWithoutMarker == 2) && lengthWithoutMarker == 2 + ? argumentTypes[markerOffset + 1].equals(THROWABLE_CLASS) + : true) { + // all the rest: debug(Marker?, (Message|MessageSupplier|CharSequence|Object|String|Supplier), Throwable?) + checkFixedArityArgs(methodNode, logMessageFrames[i], lineNumber, methodInsn, markerOffset + 0, 0); + } else { + throw new IllegalStateException( + "Method invoked on " + LOGGER_CLASS.getClassName() + " that is not supported by logger usage checker" + ); + } } - private void checkFixedArityArgs(MethodNode methodNode, Frame logMessageFrame, int lineNumber, - MethodInsnNode methodInsn, int messageIndex, int positionalArgsLength) { - PlaceHolderStringBasicValue logMessageLength = checkLogMessageConsistency(methodNode, logMessageFrame, lineNumber, methodInsn, - messageIndex, positionalArgsLength); + private void checkFixedArityArgs( + MethodNode methodNode, + Frame logMessageFrame, + int lineNumber, + MethodInsnNode methodInsn, + int messageIndex, + int positionalArgsLength + ) { + PlaceHolderStringBasicValue logMessageLength = checkLogMessageConsistency( + methodNode, + logMessageFrame, + lineNumber, + methodInsn, + messageIndex, + positionalArgsLength + ); if (logMessageLength == null) { return; } if (logMessageLength.minValue != positionalArgsLength) { - wrongUsageCallback.accept(new WrongLoggerUsage(className, methodNode.name, methodInsn.name, lineNumber, - "Expected " + logMessageLength.minValue + " arguments but got " + positionalArgsLength)); + wrongUsageCallback.accept( + new WrongLoggerUsage( + className, + methodNode.name, + methodInsn.name, + lineNumber, + "Expected " + logMessageLength.minValue + " arguments but got " + positionalArgsLength + ) + ); return; } } - private void checkArrayArgs(MethodNode methodNode, Frame logMessageFrame, Frame arraySizeFrame, - int lineNumber, MethodInsnNode methodInsn, int messageIndex, int arrayIndex) { + private void checkArrayArgs( + MethodNode methodNode, + Frame logMessageFrame, + Frame arraySizeFrame, + int lineNumber, + MethodInsnNode methodInsn, + int messageIndex, + int arrayIndex + ) { BasicValue arraySizeObject = getStackValue(arraySizeFrame, methodInsn, arrayIndex); if (arraySizeObject instanceof ArraySizeBasicValue == false) { - wrongUsageCallback.accept(new WrongLoggerUsage(className, methodNode.name, methodInsn.name, lineNumber, - "Could not determine size of array")); + wrongUsageCallback.accept( + new WrongLoggerUsage(className, methodNode.name, methodInsn.name, lineNumber, "Could not determine size of array") + ); return; } ArraySizeBasicValue arraySize = (ArraySizeBasicValue) arraySizeObject; - PlaceHolderStringBasicValue logMessageLength = checkLogMessageConsistency(methodNode, logMessageFrame, lineNumber, methodInsn, - messageIndex, arraySize.minValue); + PlaceHolderStringBasicValue logMessageLength = checkLogMessageConsistency( + methodNode, + logMessageFrame, + lineNumber, + methodInsn, + messageIndex, + arraySize.minValue + ); if (logMessageLength == null) { return; } if (arraySize.minValue != arraySize.maxValue) { - wrongUsageCallback.accept(new WrongLoggerUsage(className, methodNode.name, methodInsn.name, lineNumber, - "Multiple parameter arrays with conflicting sizes")); + wrongUsageCallback.accept( + new WrongLoggerUsage( + className, + methodNode.name, + methodInsn.name, + lineNumber, + "Multiple parameter arrays with conflicting sizes" + ) + ); return; } assert logMessageLength.minValue == logMessageLength.maxValue && arraySize.minValue == arraySize.maxValue; int chainedParams = getChainedParams(methodInsn); int args = arraySize.minValue + chainedParams; if (logMessageLength.minValue != args) { - wrongUsageCallback.accept(new WrongLoggerUsage(className, methodNode.name, methodInsn.name, lineNumber, - "Expected " + logMessageLength.minValue + " arguments but got " + arraySize.minValue)); + wrongUsageCallback.accept( + new WrongLoggerUsage( + className, + methodNode.name, + methodInsn.name, + lineNumber, + "Expected " + logMessageLength.minValue + " arguments but got " + arraySize.minValue + ) + ); return; } } - //counts how many times argAndField was called on the method chain + // counts how many times argAndField was called on the method chain private int getChainedParams(AbstractInsnNode startNode) { int c = 0; AbstractInsnNode current = startNode; - while(current.getNext() != null){ + while (current.getNext() != null) { current = current.getNext(); - if(current instanceof MethodInsnNode){ - MethodInsnNode method = (MethodInsnNode)current; - if(method.name.equals("argAndField")){ + if (current instanceof MethodInsnNode) { + MethodInsnNode method = (MethodInsnNode) current; + if (method.name.equals("argAndField")) { c++; } } @@ -412,14 +516,26 @@ private int getChainedParams(AbstractInsnNode startNode) { return c; } - private PlaceHolderStringBasicValue checkLogMessageConsistency(MethodNode methodNode, Frame logMessageFrame, - int lineNumber, MethodInsnNode methodInsn, int messageIndex, - int argsSize) { + private PlaceHolderStringBasicValue checkLogMessageConsistency( + MethodNode methodNode, + Frame logMessageFrame, + int lineNumber, + MethodInsnNode methodInsn, + int messageIndex, + int argsSize + ) { BasicValue logMessageLengthObject = getStackValue(logMessageFrame, methodInsn, messageIndex); if (logMessageLengthObject instanceof PlaceHolderStringBasicValue == false) { if (argsSize > 0) { - wrongUsageCallback.accept(new WrongLoggerUsage(className, methodNode.name, methodInsn.name, lineNumber, - "First argument must be a string constant so that we can statically ensure proper place holder usage")); + wrongUsageCallback.accept( + new WrongLoggerUsage( + className, + methodNode.name, + methodInsn.name, + lineNumber, + "First argument must be a string constant so that we can statically ensure proper place holder usage" + ) + ); } else { // don't check logger usage for logger.warn(someObject) } @@ -427,8 +543,15 @@ private PlaceHolderStringBasicValue checkLogMessageConsistency(MethodNode method } PlaceHolderStringBasicValue logMessageLength = (PlaceHolderStringBasicValue) logMessageLengthObject; if (logMessageLength.minValue != logMessageLength.maxValue) { - wrongUsageCallback.accept(new WrongLoggerUsage(className, methodNode.name, methodInsn.name, lineNumber, - "Multiple log messages with conflicting number of place holders")); + wrongUsageCallback.accept( + new WrongLoggerUsage( + className, + methodNode.name, + methodInsn.name, + lineNumber, + "Multiple log messages with conflicting number of place holders" + ) + ); return null; } return logMessageLength; @@ -491,10 +614,7 @@ public int hashCode() { @Override public String toString() { - return "IntMinMaxTrackingBasicValue{" + - "minValue=" + minValue + - ", maxValue=" + maxValue + - '}'; + return "IntMinMaxTrackingBasicValue{" + "minValue=" + minValue + ", maxValue=" + maxValue + '}'; } } @@ -545,7 +665,8 @@ public BasicValue newOperation(AbstractInsnNode insnNode) throws AnalyzerExcepti @Override public BasicValue merge(BasicValue value1, BasicValue value2) { - if (value1 instanceof PlaceHolderStringBasicValue && value2 instanceof PlaceHolderStringBasicValue + if (value1 instanceof PlaceHolderStringBasicValue + && value2 instanceof PlaceHolderStringBasicValue && value1.equals(value2) == false) { PlaceHolderStringBasicValue c1 = (PlaceHolderStringBasicValue) value1; PlaceHolderStringBasicValue c2 = (PlaceHolderStringBasicValue) value2; @@ -564,23 +685,31 @@ private static final class ArraySizeInterpreter extends BasicInterpreter { @Override public BasicValue newOperation(AbstractInsnNode insnNode) throws AnalyzerException { switch (insnNode.getOpcode()) { - case ICONST_0: return new IntegerConstantBasicValue(Type.INT_TYPE, 0); - case ICONST_1: return new IntegerConstantBasicValue(Type.INT_TYPE, 1); - case ICONST_2: return new IntegerConstantBasicValue(Type.INT_TYPE, 2); - case ICONST_3: return new IntegerConstantBasicValue(Type.INT_TYPE, 3); - case ICONST_4: return new IntegerConstantBasicValue(Type.INT_TYPE, 4); - case ICONST_5: return new IntegerConstantBasicValue(Type.INT_TYPE, 5); + case ICONST_0: + return new IntegerConstantBasicValue(Type.INT_TYPE, 0); + case ICONST_1: + return new IntegerConstantBasicValue(Type.INT_TYPE, 1); + case ICONST_2: + return new IntegerConstantBasicValue(Type.INT_TYPE, 2); + case ICONST_3: + return new IntegerConstantBasicValue(Type.INT_TYPE, 3); + case ICONST_4: + return new IntegerConstantBasicValue(Type.INT_TYPE, 4); + case ICONST_5: + return new IntegerConstantBasicValue(Type.INT_TYPE, 5); case BIPUSH: - case SIPUSH: return new IntegerConstantBasicValue(Type.INT_TYPE, ((IntInsnNode)insnNode).operand); + case SIPUSH: + return new IntegerConstantBasicValue(Type.INT_TYPE, ((IntInsnNode) insnNode).operand); case Opcodes.LDC: { - Object constant = ((LdcInsnNode)insnNode).cst; + Object constant = ((LdcInsnNode) insnNode).cst; if (constant instanceof Integer) { - return new IntegerConstantBasicValue(Type.INT_TYPE, (Integer)constant); + return new IntegerConstantBasicValue(Type.INT_TYPE, (Integer) constant); } else { return super.newOperation(insnNode); } } - default: return super.newOperation(insnNode); + default: + return super.newOperation(insnNode); } } @@ -603,8 +732,11 @@ public BasicValue unaryOperation(AbstractInsnNode insnNode, BasicValue value) th if (insnNode.getOpcode() == Opcodes.ANEWARRAY && value instanceof IntegerConstantBasicValue) { IntegerConstantBasicValue constantBasicValue = (IntegerConstantBasicValue) value; String desc = ((TypeInsnNode) insnNode).desc; - return new ArraySizeBasicValue(Type.getType("[" + Type.getObjectType(desc)), constantBasicValue.minValue, - constantBasicValue.maxValue); + return new ArraySizeBasicValue( + Type.getType("[" + Type.getObjectType(desc)), + constantBasicValue.minValue, + constantBasicValue.maxValue + ); } return super.unaryOperation(insnNode, value); } diff --git a/test/logger-usage/src/test/java/org/elasticsearch/test/loggerusage/ESLoggerUsageTests.java b/test/logger-usage/src/test/java/org/elasticsearch/test/loggerusage/ESLoggerUsageTests.java index 1b8194f7f87c3..669ceac3e612f 100644 --- a/test/logger-usage/src/test/java/org/elasticsearch/test/loggerusage/ESLoggerUsageTests.java +++ b/test/logger-usage/src/test/java/org/elasticsearch/test/loggerusage/ESLoggerUsageTests.java @@ -43,8 +43,11 @@ public void testLoggerUsageChecks() throws IOException { logger.info("Checking logger usage for method {}", method.getName()); InputStream classInputStream = getClass().getResourceAsStream(getClass().getSimpleName() + ".class"); List errors = new ArrayList<>(); - ESLoggerUsageChecker.check(errors::add, classInputStream, - m -> m.equals(method.getName()) || m.startsWith("lambda$" + method.getName())); + ESLoggerUsageChecker.check( + errors::add, + classInputStream, + m -> m.equals(method.getName()) || m.startsWith("lambda$" + method.getName()) + ); if (method.getName().startsWith("checkFail")) { assertFalse("Expected " + method.getName() + " to have wrong Logger usage", errors.isEmpty()); } else { @@ -68,8 +71,10 @@ public void testLoggerUsageCheckerCompatibilityWithLog4j2Logger() throws NoSuchM assertEquals(String.class, method.getParameterTypes()[markerOffset]); assertThat(method.getParameterTypes()[markerOffset + 1], is(oneOf(Object[].class, Supplier[].class))); } else { - assertThat(method.getParameterTypes()[markerOffset], is(oneOf(Message.class, MessageSupplier.class, - CharSequence.class, Object.class, String.class, Supplier.class))); + assertThat( + method.getParameterTypes()[markerOffset], + is(oneOf(Message.class, MessageSupplier.class, CharSequence.class, Object.class, String.class, Supplier.class)) + ); if (paramLength == 2) { assertThat(method.getParameterTypes()[markerOffset + 1], is(oneOf(Throwable.class, Object.class))); @@ -111,18 +116,15 @@ public void testLoggerUsageCheckerCompatibilityWithLog4j2Logger() throws NoSuchM } public void checkArgumentsProvidedInConstructor() { - logger.debug(new ESLogMessage("message {}", "some-arg") - .field("x-opaque-id", "some-value")); + logger.debug(new ESLogMessage("message {}", "some-arg").field("x-opaque-id", "some-value")); } public void checkWithUsage() { - logger.debug(new ESLogMessage("message {}") - .argAndField("x-opaque-id", "some-value") - .field("field", "value") - .with("field2", "value2")); + logger.debug( + new ESLogMessage("message {}").argAndField("x-opaque-id", "some-value").field("field", "value").with("field2", "value2") + ); } - public void checkFailArraySizeForSubclasses(Object... arr) { logger.debug(new ESLogMessage("message {}", arr)); } @@ -132,12 +134,11 @@ public void checkFailForTooManyArgumentsInConstr() { } public void checkFailForTooManyArgumentsWithChain() { - logger.debug(new ESLogMessage("message {}").argAndField("x-opaque-id", "some-value") - .argAndField("too-many-arg", "xxx")); + logger.debug(new ESLogMessage("message {}").argAndField("x-opaque-id", "some-value").argAndField("too-many-arg", "xxx")); } public void checkFailArraySize(String... arr) { - logger.debug(new ParameterizedMessage("text {}", (Object[])arr)); + logger.debug(new ParameterizedMessage("text {}", (Object[]) arr)); } public void checkNumberOfArguments1() { @@ -253,7 +254,7 @@ public void checkFailComplexUsage2(boolean b) { public void checkDeprecationLogger() { DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(ESLoggerUsageTests.class); - deprecationLogger.critical(DeprecationCategory.OTHER, "key","message {}", 123); + deprecationLogger.critical(DeprecationCategory.OTHER, "key", "message {}", 123); } } diff --git a/x-pack/docs/src/test/java/org/elasticsearch/smoketest/XDocsClientYamlTestSuiteIT.java b/x-pack/docs/src/test/java/org/elasticsearch/smoketest/XDocsClientYamlTestSuiteIT.java index 099cf545f305a..2c81a5686becc 100644 --- a/x-pack/docs/src/test/java/org/elasticsearch/smoketest/XDocsClientYamlTestSuiteIT.java +++ b/x-pack/docs/src/test/java/org/elasticsearch/smoketest/XDocsClientYamlTestSuiteIT.java @@ -44,8 +44,11 @@ protected void afterIfFailed(List errors) { String name = getTestName().split("=")[1]; name = name.substring(0, name.length() - 1); name = name.replaceAll("/([^/]+)$", ".asciidoc:$1"); - logger.error("This failing test was generated by documentation starting at {}. It may include many snippets. " - + "See Elasticsearch's docs/README.asciidoc for an explanation of test generation.", name); + logger.error( + "This failing test was generated by documentation starting at {}. It may include many snippets. " + + "See Elasticsearch's docs/README.asciidoc for an explanation of test generation.", + name + ); } @Override @@ -78,9 +81,7 @@ protected ClientYamlTestClient initClientYamlTestClient( */ @Override protected Settings restClientSettings() { - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", USER_TOKEN) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", USER_TOKEN).build(); } /** @@ -90,14 +91,17 @@ protected Settings restClientSettings() { public void reenableWatcher() throws Exception { if (isWatcherTest()) { assertBusy(() -> { - ClientYamlTestResponse response = - getAdminExecutionContext().callApi("watcher.stats", emptyMap(), emptyList(), emptyMap()); + ClientYamlTestResponse response = getAdminExecutionContext().callApi("watcher.stats", emptyMap(), emptyList(), emptyMap()); String state = (String) response.evaluate("stats.0.watcher_state"); switch (state) { case "stopped": - ClientYamlTestResponse startResponse = - getAdminExecutionContext().callApi("watcher.start", emptyMap(), emptyList(), emptyMap()); + ClientYamlTestResponse startResponse = getAdminExecutionContext().callApi( + "watcher.start", + emptyMap(), + emptyList(), + emptyMap() + ); boolean isAcknowledged = (boolean) startResponse.evaluate("acknowledged"); assertThat(isAcknowledged, is(true)); throw new AssertionError("waiting until stopped state reached started state"); @@ -131,11 +135,10 @@ protected boolean isMachineLearningTest() { */ @After public void deleteUsers() throws Exception { - ClientYamlTestResponse response = getAdminExecutionContext().callApi("security.get_user", emptyMap(), emptyList(), - emptyMap()); + ClientYamlTestResponse response = getAdminExecutionContext().callApi("security.get_user", emptyMap(), emptyList(), emptyMap()); @SuppressWarnings("unchecked") Map users = (Map) response.getBody(); - for (String user: users.keySet()) { + for (String user : users.keySet()) { Map metadataMap = (Map) ((Map) users.get(user)).get("metadata"); Boolean reserved = metadataMap == null ? null : (Boolean) metadataMap.get("_reserved"); if (reserved == null || reserved == false) { diff --git a/x-pack/license-tools/src/main/java/org/elasticsearch/license/licensor/LicenseSigner.java b/x-pack/license-tools/src/main/java/org/elasticsearch/license/licensor/LicenseSigner.java index 53e8cb8c048a1..4ca1dc2f1d092 100644 --- a/x-pack/license-tools/src/main/java/org/elasticsearch/license/licensor/LicenseSigner.java +++ b/x-pack/license-tools/src/main/java/org/elasticsearch/license/licensor/LicenseSigner.java @@ -10,12 +10,12 @@ import org.apache.lucene.util.BytesRefIterator; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.hash.MessageDigests; +import org.elasticsearch.license.CryptUtils; +import org.elasticsearch.license.License; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.license.CryptUtils; -import org.elasticsearch.license.License; import java.io.IOException; import java.nio.ByteBuffer; @@ -58,8 +58,7 @@ public LicenseSigner(final Path privateKeyPath, final Path publicKeyPath) { */ public License sign(License licenseSpec) throws IOException { XContentBuilder contentBuilder = XContentFactory.contentBuilder(XContentType.JSON); - final Map licenseSpecViewMode = - Collections.singletonMap(License.LICENSE_SPEC_VIEW_MODE, "true"); + final Map licenseSpecViewMode = Collections.singletonMap(License.LICENSE_SPEC_VIEW_MODE, "true"); licenseSpec.toXContent(contentBuilder, new ToXContent.MapParams(licenseSpecViewMode)); final byte[] signedContent; final boolean preV4 = licenseSpec.version() < License.VERSION_CRYPTO_ALGORITHMS; @@ -69,14 +68,11 @@ public License sign(License licenseSpec) throws IOException { rsa.initSign(decryptedPrivateKey); final BytesRefIterator iterator = BytesReference.bytes(contentBuilder).iterator(); BytesRef ref; - while((ref = iterator.next()) != null) { + while ((ref = iterator.next()) != null) { rsa.update(ref.bytes, ref.offset, ref.length); } signedContent = rsa.sign(); - } catch (InvalidKeyException - | IOException - | NoSuchAlgorithmException - | SignatureException e) { + } catch (InvalidKeyException | IOException | NoSuchAlgorithmException | SignatureException e) { throw new IllegalStateException(e); } final byte[] magic = new byte[MAGIC_LENGTH]; @@ -84,21 +80,20 @@ public License sign(License licenseSpec) throws IOException { random.nextBytes(magic); final byte[] publicKeyBytes = Files.readAllBytes(publicKeyPath); PublicKey publicKey = CryptUtils.readPublicKey(publicKeyBytes); - final byte[] pubKeyFingerprint = preV4 ? Base64.getEncoder().encode(CryptUtils.writeEncryptedPublicKey(publicKey)) : - getPublicKeyFingerprint(publicKeyBytes); + final byte[] pubKeyFingerprint = preV4 + ? Base64.getEncoder().encode(CryptUtils.writeEncryptedPublicKey(publicKey)) + : getPublicKeyFingerprint(publicKeyBytes); byte[] bytes = new byte[4 + 4 + MAGIC_LENGTH + 4 + pubKeyFingerprint.length + 4 + signedContent.length]; ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); byteBuffer.putInt(licenseSpec.version()) - .putInt(magic.length) - .put(magic) - .putInt(pubKeyFingerprint.length) - .put(pubKeyFingerprint) - .putInt(signedContent.length) - .put(signedContent); + .putInt(magic.length) + .put(magic) + .putInt(pubKeyFingerprint.length) + .put(pubKeyFingerprint) + .putInt(signedContent.length) + .put(signedContent); - return License.builder() - .fromLicenseSpec(licenseSpec, Base64.getEncoder().encodeToString(bytes)) - .build(); + return License.builder().fromLicenseSpec(licenseSpec, Base64.getEncoder().encodeToString(bytes)).build(); } private byte[] getPublicKeyFingerprint(byte[] keyBytes) { diff --git a/x-pack/license-tools/src/main/java/org/elasticsearch/license/licensor/tools/KeyPairGeneratorTool.java b/x-pack/license-tools/src/main/java/org/elasticsearch/license/licensor/tools/KeyPairGeneratorTool.java index 2dd328fd5a4c9..a4e69f0c1ab87 100644 --- a/x-pack/license-tools/src/main/java/org/elasticsearch/license/licensor/tools/KeyPairGeneratorTool.java +++ b/x-pack/license-tools/src/main/java/org/elasticsearch/license/licensor/tools/KeyPairGeneratorTool.java @@ -8,12 +8,13 @@ import joptsimple.OptionSet; import joptsimple.OptionSpec; + import org.elasticsearch.cli.ExitCodes; import org.elasticsearch.cli.LoggingAwareCommand; import org.elasticsearch.cli.Terminal; import org.elasticsearch.cli.UserException; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.core.PathUtils; +import org.elasticsearch.core.SuppressForbidden; import java.nio.file.Files; import java.nio.file.Path; @@ -31,10 +32,8 @@ public class KeyPairGeneratorTool extends LoggingAwareCommand { public KeyPairGeneratorTool() { super("Generates a key pair with RSA 2048-bit security"); // TODO: in jopt-simple 5.0 we can use a PathConverter to take Path instead of File - this.publicKeyPathOption = parser.accepts("publicKeyPath", "public key path") - .withRequiredArg().required(); - this.privateKeyPathOption = parser.accepts("privateKeyPath", "private key path") - .withRequiredArg().required(); + this.publicKeyPathOption = parser.accepts("publicKeyPath", "public key path").withRequiredArg().required(); + this.privateKeyPathOption = parser.accepts("privateKeyPath", "private key path").withRequiredArg().required(); } public static void main(String[] args) throws Exception { @@ -68,11 +67,9 @@ protected void execute(Terminal terminal, OptionSet options) throws Exception { Files.write(publicKeyPath, keyPair.getPublic().getEncoded()); terminal.println( - Terminal.Verbosity.VERBOSE, - "generating key pair [public key: " - + publicKeyPath - + ", private key: " - + privateKeyPath + "]"); + Terminal.Verbosity.VERBOSE, + "generating key pair [public key: " + publicKeyPath + ", private key: " + privateKeyPath + "]" + ); } @SuppressForbidden(reason = "Parsing command line path") diff --git a/x-pack/license-tools/src/main/java/org/elasticsearch/license/licensor/tools/LicenseGeneratorTool.java b/x-pack/license-tools/src/main/java/org/elasticsearch/license/licensor/tools/LicenseGeneratorTool.java index dcb0aa940f701..aa1f9bb58471c 100644 --- a/x-pack/license-tools/src/main/java/org/elasticsearch/license/licensor/tools/LicenseGeneratorTool.java +++ b/x-pack/license-tools/src/main/java/org/elasticsearch/license/licensor/tools/LicenseGeneratorTool.java @@ -8,20 +8,21 @@ import joptsimple.OptionSet; import joptsimple.OptionSpec; + import org.elasticsearch.cli.ExitCodes; import org.elasticsearch.cli.LoggingAwareCommand; import org.elasticsearch.cli.Terminal; import org.elasticsearch.cli.UserException; import org.elasticsearch.common.Strings; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.core.PathUtils; +import org.elasticsearch.core.SuppressForbidden; +import org.elasticsearch.license.License; +import org.elasticsearch.license.licensor.LicenseSigner; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.license.License; -import org.elasticsearch.license.licensor.LicenseSigner; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -36,16 +37,12 @@ public class LicenseGeneratorTool extends LoggingAwareCommand { public LicenseGeneratorTool() { super("Generates signed elasticsearch license(s) for a given license spec(s)"); - publicKeyPathOption = parser.accepts("publicKeyPath", "path to public key file") - .withRequiredArg().required(); - privateKeyPathOption = parser.accepts("privateKeyPath", "path to private key file") - .withRequiredArg().required(); + publicKeyPathOption = parser.accepts("publicKeyPath", "path to public key file").withRequiredArg().required(); + privateKeyPathOption = parser.accepts("privateKeyPath", "path to private key file").withRequiredArg().required(); // TODO: with jopt-simple 5.0, we can make these requiredUnless each other // which is effectively "one must be present" - licenseOption = parser.accepts("license", "license json spec") - .withRequiredArg(); - licenseFileOption = parser.accepts("licenseFile", "license json spec file") - .withRequiredArg(); + licenseOption = parser.accepts("license", "license json spec").withRequiredArg(); + licenseFileOption = parser.accepts("licenseFile", "license json spec file").withRequiredArg(); } public static void main(String[] args) throws Exception { @@ -73,10 +70,8 @@ protected void execute(Terminal terminal, OptionSet options) throws Exception { final License licenseSpec; if (options.has(licenseOption)) { - final BytesArray bytes = - new BytesArray(licenseOption.value(options).getBytes(StandardCharsets.UTF_8)); - licenseSpec = - License.fromSource(bytes, XContentType.JSON); + final BytesArray bytes = new BytesArray(licenseOption.value(options).getBytes(StandardCharsets.UTF_8)); + licenseSpec = License.fromSource(bytes, XContentType.JSON); } else if (options.has(licenseFileOption)) { Path licenseSpecPath = parsePath(licenseFileOption.value(options)); if (Files.exists(licenseSpecPath) == false) { @@ -85,9 +80,7 @@ protected void execute(Terminal terminal, OptionSet options) throws Exception { final BytesArray bytes = new BytesArray(Files.readAllBytes(licenseSpecPath)); licenseSpec = License.fromSource(bytes, XContentType.JSON); } else { - throw new UserException( - ExitCodes.USAGE, - "Must specify either --license or --licenseFile"); + throw new UserException(ExitCodes.USAGE, "Must specify either --license or --licenseFile"); } if (licenseSpec == null) { throw new UserException(ExitCodes.DATA_ERROR, "Could not parse license spec"); diff --git a/x-pack/license-tools/src/main/java/org/elasticsearch/license/licensor/tools/LicenseVerificationTool.java b/x-pack/license-tools/src/main/java/org/elasticsearch/license/licensor/tools/LicenseVerificationTool.java index 927b023306669..616ff9dff9ee9 100644 --- a/x-pack/license-tools/src/main/java/org/elasticsearch/license/licensor/tools/LicenseVerificationTool.java +++ b/x-pack/license-tools/src/main/java/org/elasticsearch/license/licensor/tools/LicenseVerificationTool.java @@ -8,20 +8,21 @@ import joptsimple.OptionSet; import joptsimple.OptionSpec; + import org.elasticsearch.cli.ExitCodes; import org.elasticsearch.cli.LoggingAwareCommand; import org.elasticsearch.cli.Terminal; import org.elasticsearch.cli.UserException; import org.elasticsearch.common.Strings; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.core.PathUtils; +import org.elasticsearch.core.SuppressForbidden; +import org.elasticsearch.license.License; +import org.elasticsearch.license.LicenseVerifier; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.license.License; -import org.elasticsearch.license.LicenseVerifier; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -35,14 +36,11 @@ public class LicenseVerificationTool extends LoggingAwareCommand { public LicenseVerificationTool() { super("Generates signed elasticsearch license(s) for a given license spec(s)"); - publicKeyPathOption = parser.accepts("publicKeyPath", "path to public key file") - .withRequiredArg().required(); + publicKeyPathOption = parser.accepts("publicKeyPath", "path to public key file").withRequiredArg().required(); // TODO: with jopt-simple 5.0, we can make these requiredUnless each other // which is effectively "one must be present" - licenseOption = parser.accepts("license", "license json spec") - .withRequiredArg(); - licenseFileOption = parser.accepts("licenseFile", "license json spec file") - .withRequiredArg(); + licenseOption = parser.accepts("license", "license json spec").withRequiredArg(); + licenseFileOption = parser.accepts("licenseFile", "license json spec file").withRequiredArg(); } public static void main(String[] args) throws Exception { @@ -58,10 +56,8 @@ protected void execute(Terminal terminal, OptionSet options) throws Exception { final License licenseSpec; if (options.has(licenseOption)) { - final BytesArray bytes = - new BytesArray(licenseOption.value(options).getBytes(StandardCharsets.UTF_8)); - licenseSpec = - License.fromSource(bytes, XContentType.JSON); + final BytesArray bytes = new BytesArray(licenseOption.value(options).getBytes(StandardCharsets.UTF_8)); + licenseSpec = License.fromSource(bytes, XContentType.JSON); } else if (options.has(licenseFileOption)) { Path licenseSpecPath = parsePath(licenseFileOption.value(options)); if (Files.exists(licenseSpecPath) == false) { @@ -70,9 +66,7 @@ protected void execute(Terminal terminal, OptionSet options) throws Exception { final BytesArray bytes = new BytesArray(Files.readAllBytes(licenseSpecPath)); licenseSpec = License.fromSource(bytes, XContentType.JSON); } else { - throw new UserException( - ExitCodes.USAGE, - "Must specify either --license or --licenseFile"); + throw new UserException(ExitCodes.USAGE, "Must specify either --license or --licenseFile"); } // verify diff --git a/x-pack/license-tools/src/test/java/org/elasticsearch/license/licensor/LicenseVerificationTests.java b/x-pack/license-tools/src/test/java/org/elasticsearch/license/licensor/LicenseVerificationTests.java index 0203ce3c70808..bed6e471c1c53 100644 --- a/x-pack/license-tools/src/test/java/org/elasticsearch/license/licensor/LicenseVerificationTests.java +++ b/x-pack/license-tools/src/test/java/org/elasticsearch/license/licensor/LicenseVerificationTests.java @@ -36,8 +36,7 @@ public void cleanUp() { public void testGeneratedLicenses() throws Exception { final TimeValue fortyEightHours = TimeValue.timeValueHours(2 * 24); - final License license = - TestUtils.generateSignedLicense(fortyEightHours, pubKeyPath, priKeyPath); + final License license = TestUtils.generateSignedLicense(fortyEightHours, pubKeyPath, priKeyPath); assertTrue(LicenseVerifier.verifyLicense(license, Files.readAllBytes(pubKeyPath))); } @@ -46,32 +45,32 @@ public void testLicenseTampering() throws Exception { License license = TestUtils.generateSignedLicense(twoHours, pubKeyPath, priKeyPath); final License tamperedLicense = License.builder() - .fromLicenseSpec(license, license.signature()) - .expiryDate(license.expiryDate() + 10 * 24 * 60 * 60 * 1000L) - .validate() - .build(); + .fromLicenseSpec(license, license.signature()) + .expiryDate(license.expiryDate() + 10 * 24 * 60 * 60 * 1000L) + .validate() + .build(); assertFalse(LicenseVerifier.verifyLicense(tamperedLicense, Files.readAllBytes(pubKeyPath))); } public void testRandomLicenseVerification() throws Exception { TestUtils.LicenseSpec licenseSpec = TestUtils.generateRandomLicenseSpec( - randomIntBetween(License.VERSION_START, License.VERSION_CURRENT)); + randomIntBetween(License.VERSION_START, License.VERSION_CURRENT) + ); License generatedLicense = generateSignedLicense(licenseSpec, pubKeyPath, priKeyPath); assertTrue(LicenseVerifier.verifyLicense(generatedLicense, Files.readAllBytes(pubKeyPath))); } - private static License generateSignedLicense( - TestUtils.LicenseSpec spec, Path pubKeyPath, Path priKeyPath) throws Exception { + private static License generateSignedLicense(TestUtils.LicenseSpec spec, Path pubKeyPath, Path priKeyPath) throws Exception { LicenseSigner signer = new LicenseSigner(priKeyPath, pubKeyPath); License.Builder builder = License.builder() - .uid(spec.uid) - .feature(spec.feature) - .type(spec.type) - .subscriptionType(spec.subscriptionType) - .issuedTo(spec.issuedTo) - .issuer(spec.issuer) - .maxNodes(spec.maxNodes); + .uid(spec.uid) + .feature(spec.feature) + .type(spec.type) + .subscriptionType(spec.subscriptionType) + .issuedTo(spec.issuedTo) + .issuer(spec.issuer) + .maxNodes(spec.maxNodes); if (spec.expiryDate != null) { builder.expiryDate(DateUtils.endOfTheDay(spec.expiryDate)); diff --git a/x-pack/license-tools/src/test/java/org/elasticsearch/license/licensor/TestUtils.java b/x-pack/license-tools/src/test/java/org/elasticsearch/license/licensor/TestUtils.java index abb46c053ea76..a9c49441d243c 100644 --- a/x-pack/license-tools/src/test/java/org/elasticsearch/license/licensor/TestUtils.java +++ b/x-pack/license-tools/src/test/java/org/elasticsearch/license/licensor/TestUtils.java @@ -10,13 +10,13 @@ import org.elasticsearch.common.time.DateFormatter; import org.elasticsearch.common.time.DateMathParser; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.license.DateUtils; +import org.elasticsearch.license.License; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.license.DateUtils; -import org.elasticsearch.license.License; -import org.elasticsearch.test.ESTestCase; import org.hamcrest.MatcherAssert; import java.io.IOException; @@ -27,8 +27,8 @@ import static com.carrotsearch.randomizedtesting.RandomizedTest.randomBoolean; import static com.carrotsearch.randomizedtesting.RandomizedTest.randomInt; import static com.carrotsearch.randomizedtesting.RandomizedTest.randomIntBetween; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.test.ESTestCase.randomFrom; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.core.IsEqual.equalTo; public class TestUtils { @@ -61,11 +61,11 @@ public static LicenseSpec generateRandomLicenseSpec(int version) { boolean datesInMillis = randomBoolean(); long now = System.currentTimeMillis(); String uid = UUID.randomUUID().toString(); - String issuer = "issuer__" + randomInt(); + String issuer = "issuer__" + randomInt(); String issuedTo = "issuedTo__" + randomInt(); - String type = version < License.VERSION_NO_FEATURE_TYPE ? - randomFrom("subscription", "internal", "development") : - randomFrom("basic", "silver", "dev", "gold", "platinum"); + String type = version < License.VERSION_NO_FEATURE_TYPE + ? randomFrom("subscription", "internal", "development") + : randomFrom("basic", "silver", "dev", "gold", "platinum"); final String subscriptionType; final String feature; if (version < License.VERSION_NO_FEATURE_TYPE) { @@ -80,29 +80,21 @@ public static LicenseSpec generateRandomLicenseSpec(int version) { long issueDateInMillis = dateMath("now", now); long expiryDateInMillis = dateMath("now+10d/d", now); return new LicenseSpec( - version, - uid, - feature, - issueDateInMillis, - expiryDateInMillis, - type, - subscriptionType, - issuedTo, - issuer, - maxNodes); + version, + uid, + feature, + issueDateInMillis, + expiryDateInMillis, + type, + subscriptionType, + issuedTo, + issuer, + maxNodes + ); } else { String issueDate = dateMathString("now", now); String expiryDate = dateMathString("now+10d/d", now); - return new LicenseSpec( - version, - uid, - feature, - issueDate, - expiryDate, type, - subscriptionType, - issuedTo, - issuer, - maxNodes); + return new LicenseSpec(version, uid, feature, issueDate, expiryDate, type, subscriptionType, issuedTo, issuer, maxNodes); } } @@ -110,13 +102,13 @@ public static String generateLicenseSpecString(LicenseSpec licenseSpec) throws I XContentBuilder licenses = jsonBuilder(); licenses.startObject(); licenses.startObject("license") - .field("uid", licenseSpec.uid) - .field("type", licenseSpec.type) - .field("subscription_type", licenseSpec.subscriptionType) - .field("issued_to", licenseSpec.issuedTo) - .field("issuer", licenseSpec.issuer) - .field("feature", licenseSpec.feature) - .field("max_nodes", licenseSpec.maxNodes); + .field("uid", licenseSpec.uid) + .field("type", licenseSpec.type) + .field("subscription_type", licenseSpec.subscriptionType) + .field("issued_to", licenseSpec.issuedTo) + .field("issuer", licenseSpec.issuer) + .field("feature", licenseSpec.feature) + .field("max_nodes", licenseSpec.maxNodes); if (licenseSpec.issueDate != null) { licenses.field("issue_date", licenseSpec.issueDate); @@ -141,37 +133,32 @@ public static void assertLicenseSpec(LicenseSpec spec, License license) { MatcherAssert.assertThat(license.type(), equalTo(spec.type)); MatcherAssert.assertThat(license.maxNodes(), equalTo(spec.maxNodes)); if (spec.issueDate != null) { - MatcherAssert.assertThat( - license.issueDate(), - equalTo(DateUtils.beginningOfTheDay(spec.issueDate))); + MatcherAssert.assertThat(license.issueDate(), equalTo(DateUtils.beginningOfTheDay(spec.issueDate))); } else { MatcherAssert.assertThat(license.issueDate(), equalTo(spec.issueDateInMillis)); } if (spec.expiryDate != null) { - MatcherAssert.assertThat( - license.expiryDate(), - equalTo(DateUtils.endOfTheDay(spec.expiryDate))); + MatcherAssert.assertThat(license.expiryDate(), equalTo(DateUtils.endOfTheDay(spec.expiryDate))); } else { MatcherAssert.assertThat(license.expiryDate(), equalTo(spec.expiryDateInMillis)); } } - public static License generateSignedLicense( - TimeValue expiryDuration, Path pubKeyPath, Path priKeyPath) throws Exception { + public static License generateSignedLicense(TimeValue expiryDuration, Path pubKeyPath, Path priKeyPath) throws Exception { long issue = System.currentTimeMillis(); int version = ESTestCase.randomIntBetween(License.VERSION_START, License.VERSION_CURRENT); - String type = version < License.VERSION_NO_FEATURE_TYPE ? - randomFrom("subscription", "internal", "development") : - randomFrom("trial", "basic", "silver", "dev", "gold", "platinum"); + String type = version < License.VERSION_NO_FEATURE_TYPE + ? randomFrom("subscription", "internal", "development") + : randomFrom("trial", "basic", "silver", "dev", "gold", "platinum"); final License.Builder builder = License.builder() - .uid(UUID.randomUUID().toString()) - .expiryDate(issue + expiryDuration.getMillis()) - .issueDate(issue) - .version(version) - .type(type) - .issuedTo("customer") - .issuer("elasticsearch") - .maxNodes(5); + .uid(UUID.randomUUID().toString()) + .expiryDate(issue + expiryDuration.getMillis()) + .issueDate(issue) + .version(version) + .type(type) + .issuedTo("customer") + .issuer("elasticsearch") + .maxNodes(5); if (version == License.VERSION_START) { builder.subscriptionType(randomFrom("dev", "gold", "platinum", "silver")); builder.feature(ESTestCase.randomAlphaOfLength(10)); @@ -195,16 +182,17 @@ public static class LicenseSpec { public final int maxNodes; public LicenseSpec( - int version, - String uid, - String feature, - long issueDateInMillis, - long expiryDateInMillis, - String type, - String subscriptionType, - String issuedTo, - String issuer, - int maxNodes) { + int version, + String uid, + String feature, + long issueDateInMillis, + long expiryDateInMillis, + String type, + String subscriptionType, + String issuedTo, + String issuer, + int maxNodes + ) { this.version = version; this.feature = feature; this.issueDateInMillis = issueDateInMillis; @@ -220,16 +208,17 @@ public LicenseSpec( } public LicenseSpec( - int version, - String uid, - String feature, - String issueDate, - String expiryDate, - String type, - String subscriptionType, - String issuedTo, - String issuer, - int maxNodes) { + int version, + String uid, + String feature, + String issueDate, + String expiryDate, + String type, + String subscriptionType, + String issuedTo, + String issuer, + int maxNodes + ) { this.version = version; this.feature = feature; this.issueDate = issueDate; diff --git a/x-pack/license-tools/src/test/java/org/elasticsearch/license/licensor/tools/KeyPairGenerationToolTests.java b/x-pack/license-tools/src/test/java/org/elasticsearch/license/licensor/tools/KeyPairGenerationToolTests.java index f383fc54fc9b2..8505f4cb58d7d 100644 --- a/x-pack/license-tools/src/test/java/org/elasticsearch/license/licensor/tools/KeyPairGenerationToolTests.java +++ b/x-pack/license-tools/src/test/java/org/elasticsearch/license/licensor/tools/KeyPairGenerationToolTests.java @@ -27,21 +27,12 @@ public void testMissingKeyPaths() throws Exception { Path exists = createTempFile("", "existing"); Path dne = createTempDir().resolve("dne"); UserException e = expectThrows( - UserException.class, - () -> execute( - "--publicKeyPath", - exists.toString(), - "--privateKeyPath", - dne.toString())); + UserException.class, + () -> execute("--publicKeyPath", exists.toString(), "--privateKeyPath", dne.toString()) + ); assertThat(e.getMessage(), containsString("existing")); assertEquals(ExitCodes.USAGE, e.exitCode); - e = expectThrows( - UserException.class, - () -> execute( - "--publicKeyPath", - dne.toString(), - "--privateKeyPath", - exists.toString())); + e = expectThrows(UserException.class, () -> execute("--publicKeyPath", dne.toString(), "--privateKeyPath", exists.toString())); assertThat(e.getMessage(), containsString("existing")); assertEquals(ExitCodes.USAGE, e.exitCode); } @@ -51,11 +42,7 @@ public void testTool() throws Exception { Path publicKeyFilePath = keysDir.resolve("public"); Path privateKeyFilePath = keysDir.resolve("private"); - execute( - "--publicKeyPath", - publicKeyFilePath.toString(), - "--privateKeyPath", - privateKeyFilePath.toString()); + execute("--publicKeyPath", publicKeyFilePath.toString(), "--privateKeyPath", privateKeyFilePath.toString()); assertTrue(publicKeyFilePath.toString(), Files.exists(publicKeyFilePath)); assertTrue(privateKeyFilePath.toString(), Files.exists(privateKeyFilePath)); } diff --git a/x-pack/license-tools/src/test/java/org/elasticsearch/license/licensor/tools/LicenseGenerationToolTests.java b/x-pack/license-tools/src/test/java/org/elasticsearch/license/licensor/tools/LicenseGenerationToolTests.java index c0353b485c11f..82292c7b3263f 100644 --- a/x-pack/license-tools/src/test/java/org/elasticsearch/license/licensor/tools/LicenseGenerationToolTests.java +++ b/x-pack/license-tools/src/test/java/org/elasticsearch/license/licensor/tools/LicenseGenerationToolTests.java @@ -11,9 +11,9 @@ import org.elasticsearch.cli.ExitCodes; import org.elasticsearch.cli.UserException; import org.elasticsearch.common.bytes.BytesArray; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.license.License; import org.elasticsearch.license.licensor.TestUtils; +import org.elasticsearch.xcontent.XContentType; import org.junit.Before; import java.nio.charset.StandardCharsets; @@ -40,70 +40,56 @@ public void testMissingKeyPaths() throws Exception { Path pub = createTempDir().resolve("pub"); Path pri = createTempDir().resolve("pri"); UserException e = expectThrows( - UserException.class, - () -> execute( - "--publicKeyPath", - pub.toString(), - "--privateKeyPath", - pri.toString())); + UserException.class, + () -> execute("--publicKeyPath", pub.toString(), "--privateKeyPath", pri.toString()) + ); assertTrue(e.getMessage(), e.getMessage().contains("pri does not exist")); assertEquals(ExitCodes.USAGE, e.exitCode); Files.createFile(pri); - e = expectThrows( - UserException.class, - () -> execute( - "--publicKeyPath", - pub.toString(), - "--privateKeyPath", - pri.toString())); + e = expectThrows(UserException.class, () -> execute("--publicKeyPath", pub.toString(), "--privateKeyPath", pri.toString())); assertTrue(e.getMessage(), e.getMessage().contains("pub does not exist")); assertEquals(ExitCodes.USAGE, e.exitCode); } public void testMissingLicenseSpec() throws Exception { UserException e = expectThrows( - UserException.class, - () -> execute( - "--publicKeyPath", - pubKeyPath.toString(), - "--privateKeyPath", - priKeyPath.toString())); - assertTrue( - e.getMessage(), - e.getMessage().contains("Must specify either --license or --licenseFile")); + UserException.class, + () -> execute("--publicKeyPath", pubKeyPath.toString(), "--privateKeyPath", priKeyPath.toString()) + ); + assertTrue(e.getMessage(), e.getMessage().contains("Must specify either --license or --licenseFile")); assertEquals(ExitCodes.USAGE, e.exitCode); } public void testLicenseSpecString() throws Exception { - TestUtils.LicenseSpec inputLicenseSpec = - TestUtils.generateRandomLicenseSpec(License.VERSION_CURRENT); + TestUtils.LicenseSpec inputLicenseSpec = TestUtils.generateRandomLicenseSpec(License.VERSION_CURRENT); String licenseSpecString = TestUtils.generateLicenseSpecString(inputLicenseSpec); String output = execute( - "--publicKeyPath", - pubKeyPath.toString(), - "--privateKeyPath", - priKeyPath.toString(), - "--license", - licenseSpecString); + "--publicKeyPath", + pubKeyPath.toString(), + "--privateKeyPath", + priKeyPath.toString(), + "--license", + licenseSpecString + ); final BytesArray bytes = new BytesArray(output.getBytes(StandardCharsets.UTF_8)); License outputLicense = License.fromSource(bytes, XContentType.JSON); TestUtils.assertLicenseSpec(inputLicenseSpec, outputLicense); } public void testLicenseSpecFile() throws Exception { - TestUtils.LicenseSpec inputLicenseSpec = - TestUtils.generateRandomLicenseSpec(License.VERSION_CURRENT); + TestUtils.LicenseSpec inputLicenseSpec = TestUtils.generateRandomLicenseSpec(License.VERSION_CURRENT); String licenseSpecString = TestUtils.generateLicenseSpecString(inputLicenseSpec); Path licenseSpecFile = createTempFile(); Files.write(licenseSpecFile, licenseSpecString.getBytes(StandardCharsets.UTF_8)); String output = execute( - "--publicKeyPath", - pubKeyPath.toString(), - "--privateKeyPath", - priKeyPath.toString(), - "--licenseFile", - licenseSpecFile.toString()); + "--publicKeyPath", + pubKeyPath.toString(), + "--privateKeyPath", + priKeyPath.toString(), + "--licenseFile", + licenseSpecFile.toString() + ); final BytesArray bytes = new BytesArray(output.getBytes(StandardCharsets.UTF_8)); License outputLicense = License.fromSource(bytes, XContentType.JSON); TestUtils.assertLicenseSpec(inputLicenseSpec, outputLicense); diff --git a/x-pack/license-tools/src/test/java/org/elasticsearch/license/licensor/tools/LicenseVerificationToolTests.java b/x-pack/license-tools/src/test/java/org/elasticsearch/license/licensor/tools/LicenseVerificationToolTests.java index a91a7450a81fb..e31413950b077 100644 --- a/x-pack/license-tools/src/test/java/org/elasticsearch/license/licensor/tools/LicenseVerificationToolTests.java +++ b/x-pack/license-tools/src/test/java/org/elasticsearch/license/licensor/tools/LicenseVerificationToolTests.java @@ -37,20 +37,14 @@ protected Command newCommand() { public void testMissingKeyPath() throws Exception { Path pub = createTempDir().resolve("pub"); - UserException e = expectThrows( - UserException.class, - () -> execute("--publicKeyPath", pub.toString())); + UserException e = expectThrows(UserException.class, () -> execute("--publicKeyPath", pub.toString())); assertTrue(e.getMessage(), e.getMessage().contains("pub does not exist")); assertEquals(ExitCodes.USAGE, e.exitCode); } public void testMissingLicenseSpec() throws Exception { - UserException e = expectThrows(UserException.class, () -> { - execute("--publicKeyPath", pubKeyPath.toString()); - }); - assertTrue( - e.getMessage(), - e.getMessage().contains("Must specify either --license or --licenseFile")); + UserException e = expectThrows(UserException.class, () -> { execute("--publicKeyPath", pubKeyPath.toString()); }); + assertTrue(e.getMessage(), e.getMessage().contains("Must specify either --license or --licenseFile")); assertEquals(ExitCodes.USAGE, e.exitCode); } @@ -59,14 +53,12 @@ public void testBrokenLicense() throws Exception { License signedLicense = TestUtils.generateSignedLicense(oneHour, pubKeyPath, priKeyPath); License tamperedLicense = License.builder() .fromLicenseSpec(signedLicense, signedLicense.signature()) - .expiryDate(signedLicense.expiryDate() + randomIntBetween(1, 1000)).build(); + .expiryDate(signedLicense.expiryDate() + randomIntBetween(1, 1000)) + .build(); UserException e = expectThrows( - UserException.class, - () -> execute( - "--publicKeyPath", - pubKeyPath.toString(), - "--license", - TestUtils.dumpLicense(tamperedLicense))); + UserException.class, + () -> execute("--publicKeyPath", pubKeyPath.toString(), "--license", TestUtils.dumpLicense(tamperedLicense)) + ); assertEquals("Invalid License!", e.getMessage()); assertEquals(ExitCodes.DATA_ERROR, e.exitCode); } @@ -74,11 +66,7 @@ public void testBrokenLicense() throws Exception { public void testLicenseSpecString() throws Exception { final TimeValue oneHour = TimeValue.timeValueHours(1); License signedLicense = TestUtils.generateSignedLicense(oneHour, pubKeyPath, priKeyPath); - String output = execute( - "--publicKeyPath", - pubKeyPath.toString(), - "--license", - TestUtils.dumpLicense(signedLicense)); + String output = execute("--publicKeyPath", pubKeyPath.toString(), "--license", TestUtils.dumpLicense(signedLicense)); assertFalse(output, output.isEmpty()); } @@ -86,14 +74,8 @@ public void testLicenseSpecFile() throws Exception { final TimeValue oneHour = TimeValue.timeValueHours(1); License signedLicense = TestUtils.generateSignedLicense(oneHour, pubKeyPath, priKeyPath); Path licenseSpecFile = createTempFile(); - Files.write( - licenseSpecFile, - TestUtils.dumpLicense(signedLicense).getBytes(StandardCharsets.UTF_8)); - String output = execute( - "--publicKeyPath", - pubKeyPath.toString(), - "--licenseFile", - licenseSpecFile.toString()); + Files.write(licenseSpecFile, TestUtils.dumpLicense(signedLicense).getBytes(StandardCharsets.UTF_8)); + String output = execute("--publicKeyPath", pubKeyPath.toString(), "--licenseFile", licenseSpecFile.toString()); assertFalse(output, output.isEmpty()); } diff --git a/x-pack/plugin/async-search/qa/security/src/javaRestTest/java/org/elasticsearch/xpack/search/AsyncSearchSecurityIT.java b/x-pack/plugin/async-search/qa/security/src/javaRestTest/java/org/elasticsearch/xpack/search/AsyncSearchSecurityIT.java index 8220bf9cadde3..b9fc52d7e01a3 100644 --- a/x-pack/plugin/async-search/qa/security/src/javaRestTest/java/org/elasticsearch/xpack/search/AsyncSearchSecurityIT.java +++ b/x-pack/plugin/async-search/qa/security/src/javaRestTest/java/org/elasticsearch/xpack/search/AsyncSearchSecurityIT.java @@ -17,16 +17,16 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.core.TimeValue; +import org.elasticsearch.search.SearchHit; +import org.elasticsearch.test.rest.ESRestTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.search.SearchHit; -import org.elasticsearch.test.rest.ESRestTestCase; import org.elasticsearch.xpack.core.async.AsyncExecutionId; import org.hamcrest.CustomMatcher; import org.hamcrest.Matcher; @@ -52,9 +52,7 @@ public class AsyncSearchSecurityIT extends ESRestTestCase { @Override protected Settings restClientSettings() { String token = basicAuthHeaderValue("test-admin", new SecureString("x-pack-test-password".toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } @Before @@ -79,34 +77,25 @@ public void testWithDlsAndFls() throws Exception { Response submitResp = submitAsyncSearch("*", "*", TimeValue.timeValueSeconds(10), "user-dls"); assertOK(submitResp); SearchHit[] hits = getSearchHits(extractResponseId(submitResp), "user-dls"); - assertThat(hits, arrayContainingInAnyOrder( - new CustomMatcher("\"index\" doc 1 matcher") { - @Override - public boolean matches(Object actual) { - SearchHit hit = (SearchHit) actual; - return "index".equals(hit.getIndex()) && - "1".equals(hit.getId()) && - hit.getSourceAsMap().isEmpty(); - } - }, - new CustomMatcher("\"index\" doc 2 matcher") { - @Override - public boolean matches(Object actual) { - SearchHit hit = (SearchHit) actual; - return "index".equals(hit.getIndex()) && - "2".equals(hit.getId()) && - "boo".equals(hit.getSourceAsMap().get("baz")); - } - }, - new CustomMatcher("\"index-user2\" doc 1 matcher") { - @Override - public boolean matches(Object actual) { - SearchHit hit = (SearchHit) actual; - return "index-user2".equals(hit.getIndex()) && - "1".equals(hit.getId()) && - hit.getSourceAsMap().isEmpty(); - } - })); + assertThat(hits, arrayContainingInAnyOrder(new CustomMatcher("\"index\" doc 1 matcher") { + @Override + public boolean matches(Object actual) { + SearchHit hit = (SearchHit) actual; + return "index".equals(hit.getIndex()) && "1".equals(hit.getId()) && hit.getSourceAsMap().isEmpty(); + } + }, new CustomMatcher("\"index\" doc 2 matcher") { + @Override + public boolean matches(Object actual) { + SearchHit hit = (SearchHit) actual; + return "index".equals(hit.getIndex()) && "2".equals(hit.getId()) && "boo".equals(hit.getSourceAsMap().get("baz")); + } + }, new CustomMatcher("\"index-user2\" doc 1 matcher") { + @Override + public boolean matches(Object actual) { + SearchHit hit = (SearchHit) actual; + return "index-user2".equals(hit.getIndex()) && "1".equals(hit.getId()) && hit.getSourceAsMap().isEmpty(); + } + })); } public void testWithUsers() throws Exception { @@ -115,7 +104,7 @@ public void testWithUsers() throws Exception { } private void testCase(String user, String other) throws Exception { - for (String indexName : new String[] {"index", "index-" + user}) { + for (String indexName : new String[] { "index", "index-" + user }) { Response submitResp = submitAsyncSearch(indexName, "foo:bar", TimeValue.timeValueSeconds(10), user); assertOK(submitResp); String id = extractResponseId(submitResp); @@ -136,7 +125,7 @@ private void testCase(String user, String other) throws Exception { // other and user cannot access the result from direct get calls AsyncExecutionId searchId = AsyncExecutionId.decode(id); - for (String runAs : new String[] {user, other}) { + for (String runAs : new String[] { user, other }) { exc = expectThrows(ResponseException.class, () -> get(ASYNC_RESULTS_INDEX, searchId.getDocId(), runAs)); assertThat(exc.getResponse().getStatusLine().getStatusCode(), equalTo(403)); assertThat(exc.getMessage(), containsString("unauthorized")); @@ -157,8 +146,10 @@ private void testCase(String user, String other) throws Exception { assertOK(delResp); } } - ResponseException exc = expectThrows(ResponseException.class, - () -> submitAsyncSearch("index-" + other, "*", TimeValue.timeValueSeconds(10), user)); + ResponseException exc = expectThrows( + ResponseException.class, + () -> submitAsyncSearch("index-" + other, "*", TimeValue.timeValueSeconds(10), user) + ); assertThat(exc.getResponse().getStatusLine().getStatusCode(), equalTo(403)); assertThat(exc.getMessage(), containsString("unauthorized")); } @@ -166,10 +157,14 @@ private void testCase(String user, String other) throws Exception { private SearchHit[] getSearchHits(String asyncId, String user) throws IOException { final Response resp = getAsyncSearch(asyncId, user); assertOK(resp); - AsyncSearchResponse searchResponse = AsyncSearchResponse.fromXContent(XContentHelper.createParser(NamedXContentRegistry.EMPTY, - LoggingDeprecationHandler.INSTANCE, - new BytesArray(EntityUtils.toByteArray(resp.getEntity())), - XContentType.JSON)); + AsyncSearchResponse searchResponse = AsyncSearchResponse.fromXContent( + XContentHelper.createParser( + NamedXContentRegistry.EMPTY, + LoggingDeprecationHandler.INSTANCE, + new BytesArray(EntityUtils.toByteArray(resp.getEntity())), + XContentType.JSON + ) + ); return searchResponse.getSearchResponse().getHits().getHits(); } @@ -182,7 +177,7 @@ public boolean matches(Object actual) { return hit.getIndex().equals("index-" + authorizedUser) && hit.getId().equals("0"); } }; - final String pitId = openPointInTime(new String[]{"index-" + authorizedUser}, authorizedUser); + final String pitId = openPointInTime(new String[] { "index-" + authorizedUser }, authorizedUser); try { Response submit = submitAsyncSearchWithPIT(pitId, "foo:bar", TimeValue.timeValueSeconds(10), authorizedUser); assertOK(submit); @@ -191,8 +186,10 @@ public boolean matches(Object actual) { assertThat(getSearchHits(extractResponseId(resp), authorizedUser), arrayContainingInAnyOrder(hitMatcher)); String unauthorizedUser = randomValueOtherThan(authorizedUser, () -> randomFrom("user1", "user2")); - ResponseException exc = expectThrows(ResponseException.class, - () -> submitAsyncSearchWithPIT(pitId, "*:*", TimeValue.timeValueSeconds(10), unauthorizedUser)); + ResponseException exc = expectThrows( + ResponseException.class, + () -> submitAsyncSearchWithPIT(pitId, "*:*", TimeValue.timeValueSeconds(10), unauthorizedUser) + ); assertThat(exc.getResponse().getStatusLine().getStatusCode(), equalTo(403)); assertThat(exc.getMessage(), containsString("unauthorized")); @@ -203,7 +200,7 @@ public boolean matches(Object actual) { public void testRejectPointInTimeWithIndices() throws Exception { String authorizedUser = randomFrom("user1", "user2"); - final String pitId = openPointInTime(new String[]{"index-" + authorizedUser}, authorizedUser); + final String pitId = openPointInTime(new String[] { "index-" + authorizedUser }, authorizedUser); try { final Request request = new Request("POST", "/_async_search"); setRunAsHeader(request, authorizedUser); @@ -224,7 +221,10 @@ public void testRejectPointInTimeWithIndices() throws Exception { request.setJsonEntity(Strings.toString(requestBody)); final ResponseException exc = expectThrows(ResponseException.class, () -> client().performRequest(request)); assertThat(exc.getResponse().getStatusLine().getStatusCode(), equalTo(400)); - assertThat(exc.getMessage(), containsString("[indices] cannot be used with point in time. Do not specify any index with point in time.")); + assertThat( + exc.getMessage(), + containsString("[indices] cannot be used with point in time. Do not specify any index with point in time.") + ); } finally { closePointInTime(pitId, authorizedUser); } @@ -239,7 +239,7 @@ public boolean matches(Object actual) { } }; String firstUser = randomFrom("user1", "user2"); - final String pitId = openPointInTime(new String[]{"index"}, firstUser); + final String pitId = openPointInTime(new String[] { "index" }, firstUser); try { { Response firstSubmit = submitAsyncSearchWithPIT(pitId, "foo:bar", TimeValue.timeValueSeconds(10), firstUser); @@ -264,7 +264,7 @@ public boolean matches(Object actual) { } public void testWithDLSPointInTime() throws Exception { - final String pitId = openPointInTime(new String[]{"index"}, "user1"); + final String pitId = openPointInTime(new String[] { "index" }, "user1"); try { Response userResp = submitAsyncSearchWithPIT(pitId, "*", TimeValue.timeValueSeconds(10), "user1"); assertOK(userResp); @@ -272,25 +272,22 @@ public void testWithDLSPointInTime() throws Exception { Response dlsResp = submitAsyncSearchWithPIT(pitId, "*", TimeValue.timeValueSeconds(10), "user-dls"); assertOK(dlsResp); - assertThat(getSearchHits(extractResponseId(dlsResp), "user-dls"), arrayContainingInAnyOrder( - new CustomMatcher("\"index\" doc 1 matcher") { + assertThat( + getSearchHits(extractResponseId(dlsResp), "user-dls"), + arrayContainingInAnyOrder(new CustomMatcher("\"index\" doc 1 matcher") { @Override public boolean matches(Object actual) { SearchHit hit = (SearchHit) actual; - return "index".equals(hit.getIndex()) && - "1".equals(hit.getId()) && - hit.getSourceAsMap().isEmpty(); + return "index".equals(hit.getIndex()) && "1".equals(hit.getId()) && hit.getSourceAsMap().isEmpty(); } - }, - new CustomMatcher("\"index\" doc 2 matcher") { + }, new CustomMatcher("\"index\" doc 2 matcher") { @Override public boolean matches(Object actual) { SearchHit hit = (SearchHit) actual; - return "index".equals(hit.getIndex()) && - "2".equals(hit.getId()) && - "boo".equals(hit.getSourceAsMap().get("baz")); + return "index".equals(hit.getIndex()) && "2".equals(hit.getId()) && "boo".equals(hit.getSourceAsMap().get("baz")); } - })); + }) + ); } finally { closePointInTime(pitId, "user1"); } @@ -304,7 +301,7 @@ static String extractResponseId(Response response) throws IOException { @SuppressWarnings("unchecked") static List>> extractHits(Map respMap) { Map response = ((Map) respMap.get("response")); - return ((List>>)((Map) response.get("hits")).get("hits")); + return ((List>>) ((Map) response.get("hits")).get("hits")); } static void index(String index, String id, Object... fields) throws IOException { @@ -339,14 +336,14 @@ static Response submitAsyncSearch(String indexName, String query, TimeValue wait } static Response getAsyncSearch(String id, String user) throws IOException { - final Request request = new Request("GET", "/_async_search/" + id); + final Request request = new Request("GET", "/_async_search/" + id); setRunAsHeader(request, user); request.addParameter("wait_for_completion_timeout", "0ms"); return client().performRequest(request); } static Response deleteAsyncSearch(String id, String user) throws IOException { - final Request request = new Request("DELETE", "/_async_search/" + id); + final Request request = new Request("DELETE", "/_async_search/" + id); setRunAsHeader(request, user); return client().performRequest(request); } @@ -383,10 +380,10 @@ static Response submitAsyncSearchWithPIT(String pit, String query, TimeValue wai request.addParameter("keep_on_completion", "true"); final XContentBuilder requestBody = JsonXContent.contentBuilder() .startObject() - .startObject("pit") - .field("id", pit) - .field("keep_alive", "1m") - .endObject() + .startObject("pit") + .field("id", pit) + .field("keep_alive", "1m") + .endObject() .endObject(); request.setJsonEntity(Strings.toString(requestBody)); return client().performRequest(request); @@ -395,10 +392,7 @@ static Response submitAsyncSearchWithPIT(String pit, String query, TimeValue wai private void closePointInTime(String pitId, String user) throws IOException { final Request request = new Request("DELETE", "/_pit"); setRunAsHeader(request, user); - final XContentBuilder requestBody = JsonXContent.contentBuilder() - .startObject() - .field("id", pitId) - .endObject(); + final XContentBuilder requestBody = JsonXContent.contentBuilder().startObject().field("id", pitId).endObject(); request.setJsonEntity(Strings.toString(requestBody)); assertOK(client().performRequest(request)); } diff --git a/x-pack/plugin/async-search/src/internalClusterTest/java/org/elasticsearch/xpack/search/AsyncSearchActionIT.java b/x-pack/plugin/async-search/src/internalClusterTest/java/org/elasticsearch/xpack/search/AsyncSearchActionIT.java index d33906fc3526c..a4c0e2a6ed9c6 100644 --- a/x-pack/plugin/async-search/src/internalClusterTest/java/org/elasticsearch/xpack/search/AsyncSearchActionIT.java +++ b/x-pack/plugin/async-search/src/internalClusterTest/java/org/elasticsearch/xpack/search/AsyncSearchActionIT.java @@ -61,9 +61,7 @@ public void setupSuiteScopeCluster() throws InterruptedException { indexName = "test-async"; numShards = randomIntBetween(1, 20); int numDocs = randomIntBetween(100, 1000); - createIndex(indexName, Settings.builder() - .put("index.number_of_shards", numShards) - .build()); + createIndex(indexName, Settings.builder().put("index.number_of_shards", numShards).build()); numKeywords = randomIntBetween(50, 100); keywordFreqs = new HashMap<>(); Set keywordSet = new HashSet<>(); @@ -77,14 +75,13 @@ public void setupSuiteScopeCluster() throws InterruptedException { float metric = randomFloat(); maxMetric = Math.max(metric, maxMetric); minMetric = Math.min(metric, minMetric); - String keyword = keywords[randomIntBetween(0, numKeywords-1)]; - keywordFreqs.compute(keyword, - (k, v) -> { - if (v == null) { - return new AtomicInteger(1); - } - v.incrementAndGet(); - return v; + String keyword = keywords[randomIntBetween(0, numKeywords - 1)]; + keywordFreqs.compute(keyword, (k, v) -> { + if (v == null) { + return new AtomicInteger(1); + } + v.incrementAndGet(); + return v; }); reqs.add(client().prepareIndex(indexName).setSource("terms", keyword, "metric", metric)); } @@ -94,11 +91,9 @@ public void setupSuiteScopeCluster() throws InterruptedException { public void testMaxMinAggregation() throws Exception { int step = numShards > 2 ? randomIntBetween(2, numShards) : 2; int numFailures = randomBoolean() ? randomIntBetween(0, numShards) : 0; - SearchSourceBuilder source = new SearchSourceBuilder() - .aggregation(AggregationBuilders.min("min").field("metric")) + SearchSourceBuilder source = new SearchSourceBuilder().aggregation(AggregationBuilders.min("min").field("metric")) .aggregation(AggregationBuilders.max("max").field("metric")); - try (SearchResponseIterator it = - assertBlockingIterator(indexName, numShards, source, numFailures, step)) { + try (SearchResponseIterator it = assertBlockingIterator(indexName, numShards, source, numFailures, step)) { AsyncSearchResponse response = it.next(); while (it.hasNext()) { response = it.next(); @@ -138,10 +133,10 @@ public void testMaxMinAggregation() throws Exception { public void testTermsAggregation() throws Exception { int step = numShards > 2 ? randomIntBetween(2, numShards) : 2; int numFailures = randomBoolean() ? randomIntBetween(0, numShards) : 0; - SearchSourceBuilder source = new SearchSourceBuilder() - .aggregation(AggregationBuilders.terms("terms").field("terms.keyword").size(numKeywords)); - try (SearchResponseIterator it = - assertBlockingIterator(indexName, numShards, source, numFailures, step)) { + SearchSourceBuilder source = new SearchSourceBuilder().aggregation( + AggregationBuilders.terms("terms").field("terms.keyword").size(numKeywords) + ); + try (SearchResponseIterator it = assertBlockingIterator(indexName, numShards, source, numFailures, step)) { AsyncSearchResponse response = it.next(); while (it.hasNext()) { response = it.next(); @@ -183,8 +178,7 @@ public void testTermsAggregation() throws Exception { public void testRestartAfterCompletion() throws Exception { final AsyncSearchResponse initial; - try (SearchResponseIterator it = - assertBlockingIterator(indexName, numShards, new SearchSourceBuilder(), 0, 2)) { + try (SearchResponseIterator it = assertBlockingIterator(indexName, numShards, new SearchSourceBuilder(), 0, 2)) { initial = it.next(); while (it.hasNext()) { it.next(); @@ -211,8 +205,9 @@ public void testRestartAfterCompletion() throws Exception { public void testDeleteCancelRunningTask() throws Exception { final AsyncSearchResponse initial; - try (SearchResponseIterator it = - assertBlockingIterator(indexName, numShards, new SearchSourceBuilder(), randomBoolean() ? 1 : 0, 2)) { + try ( + SearchResponseIterator it = assertBlockingIterator(indexName, numShards, new SearchSourceBuilder(), randomBoolean() ? 1 : 0, 2) + ) { initial = it.next(); deleteAsyncSearch(initial.getId()); it.close(); @@ -222,8 +217,9 @@ public void testDeleteCancelRunningTask() throws Exception { } public void testDeleteCleanupIndex() throws Exception { - try (SearchResponseIterator it = - assertBlockingIterator(indexName, numShards, new SearchSourceBuilder(), randomBoolean() ? 1 : 0, 2)) { + try ( + SearchResponseIterator it = assertBlockingIterator(indexName, numShards, new SearchSourceBuilder(), randomBoolean() ? 1 : 0, 2) + ) { AsyncSearchResponse response = it.next(); deleteAsyncSearch(response.getId()); it.close(); @@ -234,8 +230,7 @@ public void testDeleteCleanupIndex() throws Exception { public void testCleanupOnFailure() throws Exception { final AsyncSearchResponse initial; - try (SearchResponseIterator it = - assertBlockingIterator(indexName, numShards, new SearchSourceBuilder(), numShards, 2)) { + try (SearchResponseIterator it = assertBlockingIterator(indexName, numShards, new SearchSourceBuilder(), numShards, 2)) { initial = it.next(); } ensureTaskCompletion(initial.getId()); @@ -259,8 +254,9 @@ public void testCleanupOnFailure() throws Exception { } public void testInvalidId() throws Exception { - try (SearchResponseIterator it = - assertBlockingIterator(indexName, numShards, new SearchSourceBuilder(), randomBoolean() ? 1 : 0, 2)) { + try ( + SearchResponseIterator it = assertBlockingIterator(indexName, numShards, new SearchSourceBuilder(), randomBoolean() ? 1 : 0, 2) + ) { AsyncSearchResponse response = it.next(); ExecutionException exc = expectThrows(ExecutionException.class, () -> getAsyncSearch("invalid")); assertThat(exc.getCause(), instanceOf(IllegalArgumentException.class)); @@ -297,9 +293,7 @@ public void testNoIndex() throws Exception { public void testCancellation() throws Exception { SubmitAsyncSearchRequest request = new SubmitAsyncSearchRequest(indexName); - request.getSearchRequest().source( - new SearchSourceBuilder().aggregation(new CancellingAggregationBuilder("test", randomLong())) - ); + request.getSearchRequest().source(new SearchSourceBuilder().aggregation(new CancellingAggregationBuilder("test", randomLong()))); request.setWaitForCompletionTimeout(TimeValue.timeValueMillis(1)); AsyncSearchResponse response = submitAsyncSearch(request); assertNotNull(response.getSearchResponse()); @@ -328,8 +322,7 @@ public void testCancellation() throws Exception { public void testUpdateRunningKeepAlive() throws Exception { SubmitAsyncSearchRequest request = new SubmitAsyncSearchRequest(indexName); - request.getSearchRequest() - .source(new SearchSourceBuilder().aggregation(new CancellingAggregationBuilder("test", randomLong()))); + request.getSearchRequest().source(new SearchSourceBuilder().aggregation(new CancellingAggregationBuilder("test", randomLong()))); long now = System.currentTimeMillis(); request.setWaitForCompletionTimeout(TimeValue.timeValueMillis(1)); AsyncSearchResponse response = submitAsyncSearch(request); @@ -426,14 +419,13 @@ public void testRemoveAsyncIndex() throws Exception { client().admin().indices().prepareDelete(XPackPlugin.ASYNC_RESULTS_INDEX).get(); Exception exc = expectThrows(Exception.class, () -> getAsyncSearch(response.getId())); - Throwable cause = exc instanceof ExecutionException ? - ExceptionsHelper.unwrapCause(exc.getCause()) : ExceptionsHelper.unwrapCause(exc); + Throwable cause = exc instanceof ExecutionException + ? ExceptionsHelper.unwrapCause(exc.getCause()) + : ExceptionsHelper.unwrapCause(exc); assertThat(ExceptionsHelper.status(cause).getStatus(), equalTo(404)); SubmitAsyncSearchRequest newReq = new SubmitAsyncSearchRequest(indexName); - newReq.getSearchRequest().source( - new SearchSourceBuilder().aggregation(new CancellingAggregationBuilder("test", randomLong())) - ); + newReq.getSearchRequest().source(new SearchSourceBuilder().aggregation(new CancellingAggregationBuilder("test", randomLong()))); newReq.setWaitForCompletionTimeout(TimeValue.timeValueMillis(1)).setKeepAlive(TimeValue.timeValueSeconds(1)); AsyncSearchResponse newResp = submitAsyncSearch(newReq); assertNotNull(newResp.getSearchResponse()); @@ -463,13 +455,12 @@ public void testSearchPhaseFailure() throws Exception { } public void testMaxResponseSize() { - SearchSourceBuilder source = new SearchSourceBuilder() - .query(new MatchAllQueryBuilder()) + SearchSourceBuilder source = new SearchSourceBuilder().query(new MatchAllQueryBuilder()) .aggregation(AggregationBuilders.terms("terms").field("terms.keyword").size(numKeywords)); - final SubmitAsyncSearchRequest request = new SubmitAsyncSearchRequest(source, indexName) - .setWaitForCompletionTimeout(TimeValue.timeValueSeconds(10)) - .setKeepOnCompletion(true); + final SubmitAsyncSearchRequest request = new SubmitAsyncSearchRequest(source, indexName).setWaitForCompletionTimeout( + TimeValue.timeValueSeconds(10) + ).setKeepOnCompletion(true); int limit = 1000; // is not big enough to store the response ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); @@ -478,8 +469,17 @@ public void testMaxResponseSize() { ExecutionException e = expectThrows(ExecutionException.class, () -> submitAsyncSearch(request)); assertNotNull(e.getCause()); - assertThat(e.getMessage(), containsString("Can't store an async search response larger than [" + limit + "] bytes. " + - "This limit can be set by changing the [" + MAX_ASYNC_SEARCH_RESPONSE_SIZE_SETTING.getKey() + "] setting.")); + assertThat( + e.getMessage(), + containsString( + "Can't store an async search response larger than [" + + limit + + "] bytes. " + + "This limit can be set by changing the [" + + MAX_ASYNC_SEARCH_RESPONSE_SIZE_SETTING.getKey() + + "] setting." + ) + ); updateSettingsRequest = new ClusterUpdateSettingsRequest(); updateSettingsRequest.persistentSettings(Settings.builder().put("search.max_async_search_response_size", (String) null)); diff --git a/x-pack/plugin/async-search/src/internalClusterTest/java/org/elasticsearch/xpack/search/AsyncSearchIntegTestCase.java b/x-pack/plugin/async-search/src/internalClusterTest/java/org/elasticsearch/xpack/search/AsyncSearchIntegTestCase.java index cfe1a000033dd..769de751d5244 100644 --- a/x-pack/plugin/async-search/src/internalClusterTest/java/org/elasticsearch/xpack/search/AsyncSearchIntegTestCase.java +++ b/x-pack/plugin/async-search/src/internalClusterTest/java/org/elasticsearch/xpack/search/AsyncSearchIntegTestCase.java @@ -22,7 +22,6 @@ import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.common.component.Lifecycle; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.ContextParser; import org.elasticsearch.core.TimeValue; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.plugins.SearchPlugin; @@ -34,6 +33,7 @@ import org.elasticsearch.tasks.TaskId; import org.elasticsearch.test.ESIntegTestCase; import org.elasticsearch.test.InternalTestCluster; +import org.elasticsearch.xcontent.ContextParser; import org.elasticsearch.xpack.async.AsyncResultsIndexPlugin; import org.elasticsearch.xpack.core.LocalStateCompositeXPackPlugin; import org.elasticsearch.xpack.core.async.AsyncExecutionId; @@ -74,22 +74,28 @@ public SearchTestPlugin() {} @Override public List> getQueries() { return Arrays.asList( - new QuerySpec<>(BlockingQueryBuilder.NAME, BlockingQueryBuilder::new, - p -> { - throw new IllegalStateException("not implemented"); - }), - new QuerySpec<>(ThrowingQueryBuilder.NAME, ThrowingQueryBuilder::new, - p -> { - throw new IllegalStateException("not implemented"); - })); + new QuerySpec<>( + BlockingQueryBuilder.NAME, + BlockingQueryBuilder::new, + p -> { throw new IllegalStateException("not implemented"); } + ), + new QuerySpec<>( + ThrowingQueryBuilder.NAME, + ThrowingQueryBuilder::new, + p -> { throw new IllegalStateException("not implemented"); } + ) + ); } @Override public List getAggregations() { - return Collections.singletonList(new AggregationSpec(CancellingAggregationBuilder.NAME, CancellingAggregationBuilder::new, - (ContextParser) (p, c) -> { - throw new IllegalStateException("not implemented"); - }).addResultReader(InternalFilter::new)); + return Collections.singletonList( + new AggregationSpec( + CancellingAggregationBuilder.NAME, + CancellingAggregationBuilder::new, + (ContextParser) (p, c) -> { throw new IllegalStateException("not implemented"); } + ).addResultReader(InternalFilter::new) + ); } } @@ -119,8 +125,13 @@ public void releaseQueryLatch() { @Override protected Collection> nodePlugins() { - return Arrays.asList(LocalStateCompositeXPackPlugin.class, AsyncSearch.class, AsyncResultsIndexPlugin.class, - SearchTestPlugin.class, ReindexPlugin.class); + return Arrays.asList( + LocalStateCompositeXPackPlugin.class, + AsyncSearch.class, + AsyncResultsIndexPlugin.class, + SearchTestPlugin.class, + ReindexPlugin.class + ); } @Override @@ -136,11 +147,11 @@ protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { */ protected void restartTaskNode(String id, String indexName) throws Exception { AsyncExecutionId searchId = AsyncExecutionId.decode(id); - final ClusterStateResponse clusterState = client().admin().cluster() - .prepareState().clear().setNodes(true).get(); + final ClusterStateResponse clusterState = client().admin().cluster().prepareState().clear().setNodes(true).get(); DiscoveryNode node = clusterState.getState().nodes().get(searchId.getTaskId().getNodeId()); stopMaintenanceService(); - internalCluster().restartNode(node.getName(), new InternalTestCluster.RestartCallback() {}); + internalCluster().restartNode(node.getName(), new InternalTestCluster.RestartCallback() { + }); startMaintenanceService(); ensureYellow(ASYNC_RESULTS_INDEX, indexName); } @@ -171,10 +182,7 @@ protected AcknowledgedResponse deleteAsyncSearch(String id) throws ExecutionExce protected void ensureTaskRemoval(String id) throws Exception { AsyncExecutionId searchId = AsyncExecutionId.decode(id); assertBusy(() -> { - GetResponse resp = client().prepareGet() - .setIndex(ASYNC_RESULTS_INDEX) - .setId(searchId.getDocId()) - .get(); + GetResponse resp = client().prepareGet().setIndex(ASYNC_RESULTS_INDEX).setId(searchId.getDocId()).get(); assertFalse(resp.isExists()); }); } @@ -199,8 +207,7 @@ protected void ensureTaskCompletion(String id) throws Exception { assertBusy(() -> { TaskId taskId = AsyncExecutionId.decode(id).getTaskId(); try { - GetTaskResponse resp = client().admin().cluster() - .prepareGetTask(taskId).get(); + GetTaskResponse resp = client().admin().cluster().prepareGetTask(taskId).get(); assertNull(resp.getTask()); } catch (Exception exc) { if (exc.getCause() instanceof ResourceNotFoundException == false) { @@ -215,11 +222,13 @@ protected void ensureTaskCompletion(String id) throws Exception { * until {@link SearchResponseIterator#next()} is called. That allows to randomly * generate partial results that can be consumed in order. */ - protected SearchResponseIterator assertBlockingIterator(String indexName, - int numShards, - SearchSourceBuilder source, - int numFailures, - int progressStep) throws Exception { + protected SearchResponseIterator assertBlockingIterator( + String indexName, + int numShards, + SearchSourceBuilder source, + int numFailures, + int progressStep + ) throws Exception { final String pitId; final SubmitAsyncSearchRequest request; if (randomBoolean()) { @@ -272,9 +281,10 @@ private AsyncSearchResponse doNext() throws Exception { return response; } queryLatch.countDownAndReset(); - AsyncSearchResponse newResponse = client().execute(GetAsyncSearchAction.INSTANCE, - new GetAsyncResultRequest(response.getId()) - .setWaitForCompletionTimeout(TimeValue.timeValueMillis(10))).get(); + AsyncSearchResponse newResponse = client().execute( + GetAsyncSearchAction.INSTANCE, + new GetAsyncResultRequest(response.getId()).setWaitForCompletionTimeout(TimeValue.timeValueMillis(10)) + ).get(); if (newResponse.isRunning()) { assertThat(newResponse.status(), equalTo(RestStatus.OK)); @@ -294,8 +304,10 @@ private AsyncSearchResponse doNext() throws Exception { assertNull(newResponse.getSearchResponse().getAggregations()); assertNotNull(newResponse.getSearchResponse().getHits().getTotalHits()); assertThat(newResponse.getSearchResponse().getHits().getTotalHits().value, equalTo(0L)); - assertThat(newResponse.getSearchResponse().getHits().getTotalHits().relation, - equalTo(TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO)); + assertThat( + newResponse.getSearchResponse().getHits().getTotalHits().relation, + equalTo(TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO) + ); } else { assertThat(newResponse.status(), equalTo(RestStatus.OK)); assertNotNull(newResponse.getSearchResponse()); @@ -303,8 +315,10 @@ private AsyncSearchResponse doNext() throws Exception { assertThat(newResponse.status(), equalTo(RestStatus.OK)); assertThat(newResponse.getSearchResponse().getTotalShards(), equalTo(numShards)); assertThat(newResponse.getSearchResponse().getShardFailures().length, equalTo(numFailures)); - assertThat(newResponse.getSearchResponse().getSuccessfulShards(), - equalTo(numShards - newResponse.getSearchResponse().getShardFailures().length)); + assertThat( + newResponse.getSearchResponse().getSuccessfulShards(), + equalTo(numShards - newResponse.getSearchResponse().getShardFailures().length) + ); } return response = newResponse; } diff --git a/x-pack/plugin/async-search/src/internalClusterTest/java/org/elasticsearch/xpack/search/BlockingQueryBuilder.java b/x-pack/plugin/async-search/src/internalClusterTest/java/org/elasticsearch/xpack/search/BlockingQueryBuilder.java index f96df3b7afd3c..2c56d8e1c8574 100644 --- a/x-pack/plugin/async-search/src/internalClusterTest/java/org/elasticsearch/xpack/search/BlockingQueryBuilder.java +++ b/x-pack/plugin/async-search/src/internalClusterTest/java/org/elasticsearch/xpack/search/BlockingQueryBuilder.java @@ -14,9 +14,9 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.lucene.search.Queries; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.index.query.AbstractQueryBuilder; import org.elasticsearch.index.query.SearchExecutionContext; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.Closeable; import java.io.IOException; diff --git a/x-pack/plugin/async-search/src/main/java/org/elasticsearch/xpack/search/AsyncSearch.java b/x-pack/plugin/async-search/src/main/java/org/elasticsearch/xpack/search/AsyncSearch.java index 57acce645b4d6..64b1d5185eae5 100644 --- a/x-pack/plugin/async-search/src/main/java/org/elasticsearch/xpack/search/AsyncSearch.java +++ b/x-pack/plugin/async-search/src/main/java/org/elasticsearch/xpack/search/AsyncSearch.java @@ -42,10 +42,15 @@ public final class AsyncSearch extends Plugin implements ActionPlugin { } @Override - public List getRestHandlers(Settings settings, RestController restController, ClusterSettings clusterSettings, - IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter, - IndexNameExpressionResolver indexNameExpressionResolver, - Supplier nodesInCluster) { + public List getRestHandlers( + Settings settings, + RestController restController, + ClusterSettings clusterSettings, + IndexScopedSettings indexScopedSettings, + SettingsFilter settingsFilter, + IndexNameExpressionResolver indexNameExpressionResolver, + Supplier nodesInCluster + ) { return Arrays.asList( new RestSubmitAsyncSearchAction(), new RestGetAsyncSearchAction(), diff --git a/x-pack/plugin/async-search/src/main/java/org/elasticsearch/xpack/search/AsyncSearchTask.java b/x-pack/plugin/async-search/src/main/java/org/elasticsearch/xpack/search/AsyncSearchTask.java index cac11085e7bd7..924581f57c829 100644 --- a/x-pack/plugin/async-search/src/main/java/org/elasticsearch/xpack/search/AsyncSearchTask.java +++ b/x-pack/plugin/async-search/src/main/java/org/elasticsearch/xpack/search/AsyncSearchTask.java @@ -83,18 +83,20 @@ final class AsyncSearchTask extends SearchTask implements AsyncTask { * @param aggReduceContextSupplierFactory A factory that creates as supplier to create final reduce contexts, we need a factory in * order to inject the task itself to the reduce context. */ - AsyncSearchTask(long id, - String type, - String action, - TaskId parentTaskId, - Supplier descriptionSupplier, - TimeValue keepAlive, - Map originHeaders, - Map taskHeaders, - AsyncExecutionId searchId, - Client client, - ThreadPool threadPool, - Function, Supplier> aggReduceContextSupplierFactory) { + AsyncSearchTask( + long id, + String type, + String action, + TaskId parentTaskId, + Supplier descriptionSupplier, + TimeValue keepAlive, + Map originHeaders, + Map taskHeaders, + AsyncExecutionId searchId, + Client client, + ThreadPool threadPool, + Function, Supplier> aggReduceContextSupplierFactory + ) { super(id, type, action, () -> "async_search{" + descriptionSupplier.get() + "}", parentTaskId, taskHeaders); this.expirationTimeMillis = getStartTime() + keepAlive.getMillis(); this.originHeaders = originHeaders; @@ -160,7 +162,7 @@ public void onFailure(Exception exc) { }); } else { runnable.run(); - } + } } @Override @@ -228,29 +230,24 @@ private void internalAddCompletionListener(ActionListener l long id = completionId++; final Cancellable cancellable; try { - cancellable = threadPool.schedule( - () -> { - if (hasRun.compareAndSet(false, true)) { - // timeout occurred before completion - removeCompletionListener(id); - listener.onResponse(getResponseWithHeaders()); - } - }, - waitForCompletion, - "generic"); - } catch(Exception exc) { + cancellable = threadPool.schedule(() -> { + if (hasRun.compareAndSet(false, true)) { + // timeout occurred before completion + removeCompletionListener(id); + listener.onResponse(getResponseWithHeaders()); + } + }, waitForCompletion, "generic"); + } catch (Exception exc) { listener.onFailure(exc); return; } - completionListeners.put( - id, - resp -> { - if (hasRun.compareAndSet(false, true)) { - // completion occurred before timeout - cancellable.cancel(); - listener.onResponse(resp); - } - }); + completionListeners.put(id, resp -> { + if (hasRun.compareAndSet(false, true)) { + // completion occurred before timeout + cancellable.cancel(); + listener.onResponse(resp); + } + }); } } if (executeImmediately) { @@ -334,12 +331,15 @@ private AsyncSearchResponse getResponse(boolean restoreResponseHeaders) { AsyncSearchResponse asyncSearchResponse; try { asyncSearchResponse = mutableSearchResponse.toAsyncSearchResponse(this, expirationTimeMillis, restoreResponseHeaders); - } catch(Exception e) { - ElasticsearchException exception = new ElasticsearchStatusException("Async search: error while reducing partial results", - ExceptionsHelper.status(e), e); + } catch (Exception e) { + ElasticsearchException exception = new ElasticsearchStatusException( + "Async search: error while reducing partial results", + ExceptionsHelper.status(e), + e + ); asyncSearchResponse = mutableSearchResponse.toAsyncSearchResponse(this, expirationTimeMillis, exception); - } - return asyncSearchResponse; + } + return asyncSearchResponse; } // checks if the search task should be cancelled @@ -379,35 +379,39 @@ protected void onFetchResult(int shardIndex) { protected void onQueryFailure(int shardIndex, SearchShardTarget shardTarget, Exception exc) { // best effort to cancel expired tasks checkCancellation(); - searchResponse.get().addQueryFailure(shardIndex, - // the nodeId is null if all replicas of this shard failed - new ShardSearchFailure(exc, shardTarget.getNodeId() != null ? shardTarget : null)); + searchResponse.get() + .addQueryFailure( + shardIndex, + // the nodeId is null if all replicas of this shard failed + new ShardSearchFailure(exc, shardTarget.getNodeId() != null ? shardTarget : null) + ); } @Override protected void onFetchFailure(int shardIndex, SearchShardTarget shardTarget, Exception exc) { // best effort to cancel expired tasks checkCancellation(); - //ignore fetch failures: they make the shards count confusing if we count them as shard failures because the query + // ignore fetch failures: they make the shards count confusing if we count them as shard failures because the query // phase ran fine and we don't want to end up with e.g. total: 5 successful: 5 failed: 5. - //Given that partial results include only aggs they are not affected by fetch failures. Async search receives the fetch - //failures either as an exception (when all shards failed during fetch, in which case async search will return the error - //as well as the response obtained after the final reduction) or as part of the final response (if only some shards failed, - //in which case the final response already includes results as well as shard fetch failures) + // Given that partial results include only aggs they are not affected by fetch failures. Async search receives the fetch + // failures either as an exception (when all shards failed during fetch, in which case async search will return the error + // as well as the response obtained after the final reduction) or as part of the final response (if only some shards failed, + // in which case the final response already includes results as well as shard fetch failures) } @Override protected void onListShards(List shards, List skipped, Clusters clusters, boolean fetchPhase) { // best effort to cancel expired tasks checkCancellation(); - searchResponse.compareAndSet(null, - new MutableSearchResponse(shards.size() + skipped.size(), skipped.size(), clusters, threadPool.getThreadContext())); + searchResponse.compareAndSet( + null, + new MutableSearchResponse(shards.size() + skipped.size(), skipped.size(), clusters, threadPool.getThreadContext()) + ); executeInitListeners(); } @Override - public void onPartialReduce(List shards, TotalHits totalHits, - InternalAggregations aggregations, int reducePhase) { + public void onPartialReduce(List shards, TotalHits totalHits, InternalAggregations aggregations, int reducePhase) { // best effort to cancel expired tasks checkCancellation(); // The way that the MutableSearchResponse will build the aggs. @@ -424,8 +428,7 @@ public void onPartialReduce(List shards, TotalHits totalHits, * to the aggregations because SearchPhaseController * *already* has that reference so we're not creating more garbage. */ - reducedAggs = () -> - InternalAggregations.topLevelReduce(singletonList(aggregations), aggReduceContextSupplier.get()); + reducedAggs = () -> InternalAggregations.topLevelReduce(singletonList(aggregations), aggReduceContextSupplier.get()); } searchResponse.get().updatePartialResponse(shards.size(), totalHits, reducedAggs, reducePhase); } @@ -447,8 +450,8 @@ public void onResponse(SearchResponse response) { public void onFailure(Exception exc) { // if the failure occurred before calling onListShards searchResponse.compareAndSet(null, new MutableSearchResponse(-1, -1, null, threadPool.getThreadContext())); - searchResponse.get().updateWithFailure(new ElasticsearchStatusException("error while executing search", - ExceptionsHelper.status(exc), exc)); + searchResponse.get() + .updateWithFailure(new ElasticsearchStatusException("error while executing search", ExceptionsHelper.status(exc), exc)); executeInitListeners(); executeCompletionListeners(); } diff --git a/x-pack/plugin/async-search/src/main/java/org/elasticsearch/xpack/search/MutableSearchResponse.java b/x-pack/plugin/async-search/src/main/java/org/elasticsearch/xpack/search/MutableSearchResponse.java index 2a8232e97a9e4..482679085650b 100644 --- a/x-pack/plugin/async-search/src/main/java/org/elasticsearch/xpack/search/MutableSearchResponse.java +++ b/x-pack/plugin/async-search/src/main/java/org/elasticsearch/xpack/search/MutableSearchResponse.java @@ -12,9 +12,9 @@ import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchResponse.Clusters; import org.elasticsearch.action.search.ShardSearchFailure; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.AtomicArray; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.aggregations.InternalAggregations; import org.elasticsearch.search.internal.InternalSearchResponse; @@ -71,14 +71,11 @@ class MutableSearchResponse { * @param clusters The remote clusters statistics. * @param threadContext The thread context to retrieve the final response headers. */ - MutableSearchResponse(int totalShards, - int skippedShards, - Clusters clusters, - ThreadContext threadContext) { + MutableSearchResponse(int totalShards, int skippedShards, Clusters clusters, ThreadContext threadContext) { this.totalShards = totalShards; this.skippedShards = skippedShards; this.clusters = clusters; - this.queryFailures = totalShards == -1 ? null : new AtomicArray<>(totalShards-skippedShards); + this.queryFailures = totalShards == -1 ? null : new AtomicArray<>(totalShards - skippedShards); this.isPartial = true; this.threadContext = threadContext; this.totalHits = EMPTY_TOTAL_HITS; @@ -88,16 +85,19 @@ class MutableSearchResponse { * Updates the response with the result of a partial reduction. * @param reducedAggs is a strategy for producing the reduced aggs */ - synchronized void updatePartialResponse(int successfulShards, TotalHits totalHits, - Supplier reducedAggs, int reducePhase) { + synchronized void updatePartialResponse( + int successfulShards, + TotalHits totalHits, + Supplier reducedAggs, + int reducePhase + ) { failIfFrozen(); if (reducePhase < this.reducePhase) { // should never happen since partial response are updated under a lock // in the search phase controller - throw new IllegalStateException("received partial response out of order: " - + reducePhase + " < " + this.reducePhase); + throw new IllegalStateException("received partial response out of order: " + reducePhase + " < " + this.reducePhase); } - //when we get partial results skipped shards are not included in the provided number of successful shards + // when we get partial results skipped shards are not included in the provided number of successful shards this.successfulShards = successfulShards + skippedShards; this.totalHits = totalHits; this.reducedAggsSource = reducedAggs; @@ -110,10 +110,10 @@ synchronized void updatePartialResponse(int successfulShards, TotalHits totalHit */ synchronized void updateFinalResponse(SearchResponse response) { failIfFrozen(); - assert response.getTotalShards() == totalShards : "received number of total shards differs from the one " + - "notified through onListShards"; - assert response.getSkippedShards() == skippedShards : "received number of skipped shards differs from the one " + - "notified through onListShards"; + assert response.getTotalShards() == totalShards + : "received number of total shards differs from the one " + "notified through onListShards"; + assert response.getSkippedShards() == skippedShards + : "received number of skipped shards differs from the one " + "notified through onListShards"; this.responseHeaders = threadContext.getResponseHeaders(); this.finalResponse = response; this.isPartial = false; @@ -128,7 +128,7 @@ synchronized void updateWithFailure(ElasticsearchException exc) { failIfFrozen(); // copy the response headers from the current context this.responseHeaders = threadContext.getResponseHeaders(); - //note that when search fails, we may have gotten partial results before the failure. In that case async + // note that when search fails, we may have gotten partial results before the failure. In that case async // search will return an error plus the last partial results that were collected. this.isPartial = true; this.failure = exc; @@ -147,10 +147,25 @@ void addQueryFailure(int shardIndex, ShardSearchFailure failure) { private SearchResponse buildResponse(long taskStartTimeNanos, InternalAggregations reducedAggs) { InternalSearchResponse internal = new InternalSearchResponse( - new SearchHits(SearchHits.EMPTY, totalHits, Float.NaN), reducedAggs, null, null, false, false, reducePhase); + new SearchHits(SearchHits.EMPTY, totalHits, Float.NaN), + reducedAggs, + null, + null, + false, + false, + reducePhase + ); long tookInMillis = TimeValue.timeValueNanos(System.nanoTime() - taskStartTimeNanos).getMillis(); - return new SearchResponse(internal, null, totalShards, successfulShards, skippedShards, - tookInMillis, buildQueryFailures(), clusters); + return new SearchResponse( + internal, + null, + totalShards, + successfulShards, + skippedShards, + tookInMillis, + buildQueryFailures(), + clusters + ); } /** @@ -159,9 +174,7 @@ private SearchResponse buildResponse(long taskStartTimeNanos, InternalAggregatio * This method is synchronized to ensure that we don't perform final reduces concurrently. * This method also restores the response headers in the current thread context when requested, if the final response is available. */ - synchronized AsyncSearchResponse toAsyncSearchResponse(AsyncSearchTask task, - long expirationTime, - boolean restoreResponseHeaders) { + synchronized AsyncSearchResponse toAsyncSearchResponse(AsyncSearchTask task, long expirationTime, boolean restoreResponseHeaders) { if (restoreResponseHeaders && responseHeaders != null) { restoreResponseHeadersContext(threadContext, responseHeaders); } @@ -184,11 +197,17 @@ synchronized AsyncSearchResponse toAsyncSearchResponse(AsyncSearchTask task, reducedAggsSource = () -> reducedAggs; searchResponse = buildResponse(task.getStartTimeNanos(), reducedAggs); } - return new AsyncSearchResponse(task.getExecutionId().getEncoded(), searchResponse, - failure, isPartial, frozen == false, task.getStartTime(), expirationTime); + return new AsyncSearchResponse( + task.getExecutionId().getEncoded(), + searchResponse, + failure, + isPartial, + frozen == false, + task.getStartTime(), + expirationTime + ); } - /** * Creates an {@link AsyncStatusResponse} -- status of an async response. * Response is created based on the current state of the mutable response or based on {@code finalResponse} if it is available. @@ -240,14 +259,23 @@ synchronized AsyncStatusResponse toStatusResponse(String asyncExecutionId, long ); } - synchronized AsyncSearchResponse toAsyncSearchResponse(AsyncSearchTask task, - long expirationTime, - ElasticsearchException reduceException) { + synchronized AsyncSearchResponse toAsyncSearchResponse( + AsyncSearchTask task, + long expirationTime, + ElasticsearchException reduceException + ) { if (this.failure != null) { reduceException.addSuppressed(this.failure); } - return new AsyncSearchResponse(task.getExecutionId().getEncoded(), buildResponse(task.getStartTimeNanos(), null), - reduceException, isPartial, frozen == false, task.getStartTime(), expirationTime); + return new AsyncSearchResponse( + task.getExecutionId().getEncoded(), + buildResponse(task.getStartTimeNanos(), null), + reduceException, + isPartial, + frozen == false, + task.getStartTime(), + expirationTime + ); } private void failIfFrozen() { diff --git a/x-pack/plugin/async-search/src/main/java/org/elasticsearch/xpack/search/RestSubmitAsyncSearchAction.java b/x-pack/plugin/async-search/src/main/java/org/elasticsearch/xpack/search/RestSubmitAsyncSearchAction.java index e2b48c141e8c2..927172cc78ae0 100644 --- a/x-pack/plugin/async-search/src/main/java/org/elasticsearch/xpack/search/RestSubmitAsyncSearchAction.java +++ b/x-pack/plugin/async-search/src/main/java/org/elasticsearch/xpack/search/RestSubmitAsyncSearchAction.java @@ -30,10 +30,7 @@ public final class RestSubmitAsyncSearchAction extends BaseRestHandler { @Override public List routes() { - return List.of( - new Route(POST, "/_async_search"), - new Route(POST, "/{index}/_async_search") - ); + return List.of(new Route(POST, "/_async_search"), new Route(POST, "/{index}/_async_search")); } @Override @@ -45,11 +42,12 @@ public String getName() { protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException { SubmitAsyncSearchRequest submit = new SubmitAsyncSearchRequest(); IntConsumer setSize = size -> submit.getSearchRequest().source().size(size); - //for simplicity, we share parsing with ordinary search. That means a couple of unsupported parameters, like scroll, + // for simplicity, we share parsing with ordinary search. That means a couple of unsupported parameters, like scroll, // pre_filter_shard_size and ccs_minimize_roundtrips get set to the search request although the REST spec don't list - //them as supported. We rely on SubmitAsyncSearchRequest#validate to fail in case they are set. - request.withContentOrSourceParamParserOrNull(parser -> - parseSearchRequest(submit.getSearchRequest(), request, parser, client.getNamedWriteableRegistry(), setSize)); + // them as supported. We rely on SubmitAsyncSearchRequest#validate to fail in case they are set. + request.withContentOrSourceParamParserOrNull( + parser -> parseSearchRequest(submit.getSearchRequest(), request, parser, client.getNamedWriteableRegistry(), setSize) + ); if (request.hasParam("wait_for_completion_timeout")) { submit.setWaitForCompletionTimeout(request.paramAsTime("wait_for_completion_timeout", submit.getWaitForCompletionTimeout())); diff --git a/x-pack/plugin/async-search/src/main/java/org/elasticsearch/xpack/search/TransportGetAsyncSearchAction.java b/x-pack/plugin/async-search/src/main/java/org/elasticsearch/xpack/search/TransportGetAsyncSearchAction.java index 9d52231835ff0..9b99cf648a555 100644 --- a/x-pack/plugin/async-search/src/main/java/org/elasticsearch/xpack/search/TransportGetAsyncSearchAction.java +++ b/x-pack/plugin/async-search/src/main/java/org/elasticsearch/xpack/search/TransportGetAsyncSearchAction.java @@ -33,28 +33,46 @@ public class TransportGetAsyncSearchAction extends HandledTransportAction createResultsService(TransportService transportService, - ClusterService clusterService, - NamedWriteableRegistry registry, - Client client, - ThreadPool threadPool, - BigArrays bigArrays) { - AsyncTaskIndexService store = new AsyncTaskIndexService<>(XPackPlugin.ASYNC_RESULTS_INDEX, clusterService, - threadPool.getThreadContext(), client, ASYNC_SEARCH_ORIGIN, AsyncSearchResponse::new, registry, bigArrays); - return new AsyncResultsService<>(store, true, AsyncSearchTask.class, AsyncSearchTask::addCompletionListener, - transportService.getTaskManager(), clusterService); + static AsyncResultsService createResultsService( + TransportService transportService, + ClusterService clusterService, + NamedWriteableRegistry registry, + Client client, + ThreadPool threadPool, + BigArrays bigArrays + ) { + AsyncTaskIndexService store = new AsyncTaskIndexService<>( + XPackPlugin.ASYNC_RESULTS_INDEX, + clusterService, + threadPool.getThreadContext(), + client, + ASYNC_SEARCH_ORIGIN, + AsyncSearchResponse::new, + registry, + bigArrays + ); + return new AsyncResultsService<>( + store, + true, + AsyncSearchTask.class, + AsyncSearchTask::addCompletionListener, + transportService.getTaskManager(), + clusterService + ); } @Override @@ -63,8 +81,12 @@ protected void doExecute(Task task, GetAsyncResultRequest request, ActionListene if (node == null || resultsService.isLocalNode(node)) { resultsService.retrieveResult(request, listener); } else { - transportService.sendRequest(node, GetAsyncSearchAction.NAME, request, - new ActionListenerResponseHandler<>(listener, AsyncSearchResponse::new, ThreadPool.Names.SAME)); + transportService.sendRequest( + node, + GetAsyncSearchAction.NAME, + request, + new ActionListenerResponseHandler<>(listener, AsyncSearchResponse::new, ThreadPool.Names.SAME) + ); } } } diff --git a/x-pack/plugin/async-search/src/main/java/org/elasticsearch/xpack/search/TransportGetAsyncStatusAction.java b/x-pack/plugin/async-search/src/main/java/org/elasticsearch/xpack/search/TransportGetAsyncStatusAction.java index 3ee66fe918ecf..753987ff727e0 100644 --- a/x-pack/plugin/async-search/src/main/java/org/elasticsearch/xpack/search/TransportGetAsyncStatusAction.java +++ b/x-pack/plugin/async-search/src/main/java/org/elasticsearch/xpack/search/TransportGetAsyncStatusAction.java @@ -37,18 +37,28 @@ public class TransportGetAsyncStatusAction extends HandledTransportAction store; @Inject - public TransportGetAsyncStatusAction(TransportService transportService, - ActionFilters actionFilters, - ClusterService clusterService, - NamedWriteableRegistry registry, - Client client, - ThreadPool threadPool, - BigArrays bigArrays) { + public TransportGetAsyncStatusAction( + TransportService transportService, + ActionFilters actionFilters, + ClusterService clusterService, + NamedWriteableRegistry registry, + Client client, + ThreadPool threadPool, + BigArrays bigArrays + ) { super(GetAsyncStatusAction.NAME, transportService, actionFilters, GetAsyncStatusRequest::new); this.transportService = transportService; this.clusterService = clusterService; - this.store = new AsyncTaskIndexService<>(XPackPlugin.ASYNC_RESULTS_INDEX, clusterService, - threadPool.getThreadContext(), client, ASYNC_SEARCH_ORIGIN, AsyncSearchResponse::new, registry, bigArrays); + this.store = new AsyncTaskIndexService<>( + XPackPlugin.ASYNC_RESULTS_INDEX, + clusterService, + threadPool.getThreadContext(), + client, + ASYNC_SEARCH_ORIGIN, + AsyncSearchResponse::new, + registry, + bigArrays + ); } @Override @@ -66,8 +76,12 @@ protected void doExecute(Task task, GetAsyncStatusRequest request, ActionListene listener ); } else { - transportService.sendRequest(node, GetAsyncStatusAction.NAME, request, - new ActionListenerResponseHandler<>(listener, AsyncStatusResponse::new, ThreadPool.Names.SAME)); + transportService.sendRequest( + node, + GetAsyncStatusAction.NAME, + request, + new ActionListenerResponseHandler<>(listener, AsyncStatusResponse::new, ThreadPool.Names.SAME) + ); } } } diff --git a/x-pack/plugin/async-search/src/main/java/org/elasticsearch/xpack/search/TransportSubmitAsyncSearchAction.java b/x-pack/plugin/async-search/src/main/java/org/elasticsearch/xpack/search/TransportSubmitAsyncSearchAction.java index 8efef493a73b3..53ee19492f759 100644 --- a/x-pack/plugin/async-search/src/main/java/org/elasticsearch/xpack/search/TransportSubmitAsyncSearchAction.java +++ b/x-pack/plugin/async-search/src/main/java/org/elasticsearch/xpack/search/TransportSubmitAsyncSearchAction.java @@ -20,8 +20,8 @@ import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.util.BigArrays; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.search.SearchService; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.tasks.Task; @@ -50,22 +50,32 @@ public class TransportSubmitAsyncSearchAction extends HandledTransportAction store; @Inject - public TransportSubmitAsyncSearchAction(ClusterService clusterService, - TransportService transportService, - ActionFilters actionFilters, - NamedWriteableRegistry registry, - Client client, - NodeClient nodeClient, - SearchService searchService, - TransportSearchAction searchAction, - BigArrays bigArrays) { + public TransportSubmitAsyncSearchAction( + ClusterService clusterService, + TransportService transportService, + ActionFilters actionFilters, + NamedWriteableRegistry registry, + Client client, + NodeClient nodeClient, + SearchService searchService, + TransportSearchAction searchAction, + BigArrays bigArrays + ) { super(SubmitAsyncSearchAction.NAME, transportService, actionFilters, SubmitAsyncSearchRequest::new); this.nodeClient = nodeClient; this.requestToAggReduceContextBuilder = (task, request) -> searchService.aggReduceContextBuilder(task, request).forFinalReduction(); this.searchAction = searchAction; this.threadContext = transportService.getThreadPool().getThreadContext(); - this.store = new AsyncTaskIndexService<>(XPackPlugin.ASYNC_RESULTS_INDEX, clusterService, threadContext, client, - ASYNC_SEARCH_ORIGIN, AsyncSearchResponse::new, registry, bigArrays); + this.store = new AsyncTaskIndexService<>( + XPackPlugin.ASYNC_RESULTS_INDEX, + clusterService, + threadContext, + client, + ASYNC_SEARCH_ORIGIN, + AsyncSearchResponse::new, + registry, + bigArrays + ); } @Override @@ -73,61 +83,63 @@ protected void doExecute(Task submitTask, SubmitAsyncSearchRequest request, Acti final SearchRequest searchRequest = createSearchRequest(request, submitTask, request.getKeepAlive()); AsyncSearchTask searchTask = (AsyncSearchTask) taskManager.register("transport", SearchAction.INSTANCE.name(), searchRequest); searchAction.execute(searchTask, searchRequest, searchTask.getSearchProgressActionListener()); - searchTask.addCompletionListener( - new ActionListener<>() { - @Override - public void onResponse(AsyncSearchResponse searchResponse) { - if (searchResponse.isRunning() || request.isKeepOnCompletion()) { - // the task is still running and the user cannot wait more so we create - // a document for further retrieval - try { - final String docId = searchTask.getExecutionId().getDocId(); - // creates the fallback response if the node crashes/restarts in the middle of the request - // TODO: store intermediate results ? - AsyncSearchResponse initialResp = searchResponse.clone(searchResponse.getId()); - store.createResponse(docId, searchTask.getOriginHeaders(), initialResp, - new ActionListener<>() { - @Override - public void onResponse(IndexResponse r) { - if (searchResponse.isRunning()) { - try { - // store the final response on completion unless the submit is cancelled - searchTask.addCompletionListener( - finalResponse -> onFinalResponse(searchTask, finalResponse, () -> {})); - } finally { - submitListener.onResponse(searchResponse); - } - } else { - onFinalResponse(searchTask, searchResponse, () -> submitListener.onResponse(searchResponse)); - } + searchTask.addCompletionListener(new ActionListener<>() { + @Override + public void onResponse(AsyncSearchResponse searchResponse) { + if (searchResponse.isRunning() || request.isKeepOnCompletion()) { + // the task is still running and the user cannot wait more so we create + // a document for further retrieval + try { + final String docId = searchTask.getExecutionId().getDocId(); + // creates the fallback response if the node crashes/restarts in the middle of the request + // TODO: store intermediate results ? + AsyncSearchResponse initialResp = searchResponse.clone(searchResponse.getId()); + store.createResponse(docId, searchTask.getOriginHeaders(), initialResp, new ActionListener<>() { + @Override + public void onResponse(IndexResponse r) { + if (searchResponse.isRunning()) { + try { + // store the final response on completion unless the submit is cancelled + searchTask.addCompletionListener( + finalResponse -> onFinalResponse(searchTask, finalResponse, () -> {}) + ); + } finally { + submitListener.onResponse(searchResponse); } + } else { + onFinalResponse(searchTask, searchResponse, () -> submitListener.onResponse(searchResponse)); + } + } - @Override - public void onFailure(Exception exc) { - onFatalFailure(searchTask, exc, searchResponse.isRunning(), - "fatal failure: unable to store initial response", submitListener); - } - }); - } catch (Exception exc) { - onFatalFailure(searchTask, exc, searchResponse.isRunning(), - "fatal failure: generic error", submitListener); - } - } else { - // the task completed within the timeout so the response is sent back to the user - // with a null id since nothing was stored on the cluster. - taskManager.unregister(searchTask); - submitListener.onResponse(searchResponse.clone(null)); + @Override + public void onFailure(Exception exc) { + onFatalFailure( + searchTask, + exc, + searchResponse.isRunning(), + "fatal failure: unable to store initial response", + submitListener + ); + } + }); + } catch (Exception exc) { + onFatalFailure(searchTask, exc, searchResponse.isRunning(), "fatal failure: generic error", submitListener); } + } else { + // the task completed within the timeout so the response is sent back to the user + // with a null id since nothing was stored on the cluster. + taskManager.unregister(searchTask); + submitListener.onResponse(searchResponse.clone(null)); } + } - @Override - public void onFailure(Exception exc) { - //this will only ever be called if there is an issue scheduling the thread that executes - //the completion listener once the wait for completion timeout expires. - onFatalFailure(searchTask, exc, true, - "fatal failure: addCompletionListener", submitListener); - } - }, request.getWaitForCompletionTimeout()); + @Override + public void onFailure(Exception exc) { + // this will only ever be called if there is an issue scheduling the thread that executes + // the completion listener once the wait for completion timeout expires. + onFatalFailure(searchTask, exc, true, "fatal failure: addCompletionListener", submitListener); + } + }, request.getWaitForCompletionTimeout()); } private SearchRequest createSearchRequest(SubmitAsyncSearchRequest request, Task submitTask, TimeValue keepAlive) { @@ -137,19 +149,35 @@ private SearchRequest createSearchRequest(SubmitAsyncSearchRequest request, Task @Override public AsyncSearchTask createTask(long id, String type, String action, TaskId parentTaskId, Map taskHeaders) { AsyncExecutionId searchId = new AsyncExecutionId(docID, new TaskId(nodeClient.getLocalNodeId(), id)); - Function, Supplier> aggReduceContextSupplierFactory = - (isCancelled) -> () -> requestToAggReduceContextBuilder.apply(isCancelled, request.getSearchRequest()); - return new AsyncSearchTask(id, type, action, parentTaskId, this::buildDescription, keepAlive, - originHeaders, taskHeaders, searchId, store.getClientWithOrigin(), nodeClient.threadPool(), - aggReduceContextSupplierFactory); + Function, Supplier> aggReduceContextSupplierFactory = ( + isCancelled) -> () -> requestToAggReduceContextBuilder.apply(isCancelled, request.getSearchRequest()); + return new AsyncSearchTask( + id, + type, + action, + parentTaskId, + this::buildDescription, + keepAlive, + originHeaders, + taskHeaders, + searchId, + store.getClientWithOrigin(), + nodeClient.threadPool(), + aggReduceContextSupplierFactory + ); } }; searchRequest.setParentTask(new TaskId(nodeClient.getLocalNodeId(), submitTask.getId())); return searchRequest; } - private void onFatalFailure(AsyncSearchTask task, Exception error, boolean shouldCancel, String cancelReason, - ActionListener listener){ + private void onFatalFailure( + AsyncSearchTask task, + Exception error, + boolean shouldCancel, + String cancelReason, + ActionListener listener + ) { if (shouldCancel && task.isCancelled() == false) { task.cancelTask(() -> { try { @@ -167,13 +195,12 @@ private void onFatalFailure(AsyncSearchTask task, Exception error, boolean shoul } } - private void onFinalResponse(AsyncSearchTask searchTask, - AsyncSearchResponse response, - Runnable nextAction) { - store.updateResponse(searchTask.getExecutionId().getDocId(), + private void onFinalResponse(AsyncSearchTask searchTask, AsyncSearchResponse response, Runnable nextAction) { + store.updateResponse( + searchTask.getExecutionId().getDocId(), threadContext.getResponseHeaders(), response, - ActionListener.wrap(() -> { + ActionListener.wrap(() -> { taskManager.unregister(searchTask); nextAction.run(); }) diff --git a/x-pack/plugin/async-search/src/test/java/org/elasticsearch/xpack/search/AsyncSearchResponseTests.java b/x-pack/plugin/async-search/src/test/java/org/elasticsearch/xpack/search/AsyncSearchResponseTests.java index 73b733610fe90..1a90f42970c8f 100644 --- a/x-pack/plugin/async-search/src/test/java/org/elasticsearch/xpack/search/AsyncSearchResponseTests.java +++ b/x-pack/plugin/async-search/src/test/java/org/elasticsearch/xpack/search/AsyncSearchResponseTests.java @@ -14,14 +14,14 @@ import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentElasticsearchExtension; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.script.ScriptException; import org.elasticsearch.search.SearchModule; import org.elasticsearch.search.internal.InternalSearchResponse; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.search.action.AsyncSearchResponse; import org.junit.Before; @@ -46,7 +46,6 @@ public void registerNamedObjects() { namedWriteableRegistry = new NamedWriteableRegistry(namedWriteables); } - protected Writeable.Reader instanceReader() { return AsyncSearchResponse::new; } @@ -89,18 +88,41 @@ static AsyncSearchResponse randomAsyncSearchResponse(String searchId, SearchResp int rand = randomIntBetween(0, 2); switch (rand) { case 0: - return new AsyncSearchResponse(searchId, randomBoolean(), - randomBoolean(), randomNonNegativeLong(), randomNonNegativeLong()); + return new AsyncSearchResponse( + searchId, + randomBoolean(), + randomBoolean(), + randomNonNegativeLong(), + randomNonNegativeLong() + ); case 1: - return new AsyncSearchResponse(searchId, searchResponse, null, - randomBoolean(), randomBoolean(), randomNonNegativeLong(), randomNonNegativeLong()); + return new AsyncSearchResponse( + searchId, + searchResponse, + null, + randomBoolean(), + randomBoolean(), + randomNonNegativeLong(), + randomNonNegativeLong() + ); case 2: - return new AsyncSearchResponse(searchId, searchResponse, - new ScriptException("messageData", new Exception("causeData"), Arrays.asList("stack1", "stack2"), - "sourceData", "langData"), randomBoolean(), randomBoolean(), - randomNonNegativeLong(), randomNonNegativeLong()); + return new AsyncSearchResponse( + searchId, + searchResponse, + new ScriptException( + "messageData", + new Exception("causeData"), + Arrays.asList("stack1", "stack2"), + "sourceData", + "langData" + ), + randomBoolean(), + randomBoolean(), + randomNonNegativeLong(), + randomNonNegativeLong() + ); default: throw new AssertionError(); @@ -113,8 +135,16 @@ static SearchResponse randomSearchResponse() { int successfulShards = randomIntBetween(0, totalShards); int skippedShards = randomIntBetween(0, successfulShards); InternalSearchResponse internalSearchResponse = InternalSearchResponse.empty(); - return new SearchResponse(internalSearchResponse, null, totalShards, - successfulShards, skippedShards, tookInMillis, ShardSearchFailure.EMPTY_ARRAY, SearchResponse.Clusters.EMPTY); + return new SearchResponse( + internalSearchResponse, + null, + totalShards, + successfulShards, + skippedShards, + tookInMillis, + ShardSearchFailure.EMPTY_ARRAY, + SearchResponse.Clusters.EMPTY + ); } static void assertEqualResponses(AsyncSearchResponse expected, AsyncSearchResponse actual) { @@ -131,31 +161,49 @@ public void testToXContent() throws IOException { Date date = new Date(); AsyncSearchResponse asyncSearchResponse = new AsyncSearchResponse("id", true, true, date.getTime(), date.getTime()); - try ( XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent())) { + try (XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent())) { builder.prettyPrint(); asyncSearchResponse.toXContent(builder, ToXContent.EMPTY_PARAMS); - assertEquals("{\n" + - " \"id\" : \"id\",\n" + - " \"is_partial\" : true,\n" + - " \"is_running\" : true,\n" + - " \"start_time_in_millis\" : " + date.getTime() + ",\n" + - " \"expiration_time_in_millis\" : " + date.getTime() + "\n" + - "}", Strings.toString(builder)); + assertEquals( + "{\n" + + " \"id\" : \"id\",\n" + + " \"is_partial\" : true,\n" + + " \"is_running\" : true,\n" + + " \"start_time_in_millis\" : " + + date.getTime() + + ",\n" + + " \"expiration_time_in_millis\" : " + + date.getTime() + + "\n" + + "}", + Strings.toString(builder) + ); } - try ( XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent())) { + try (XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent())) { builder.prettyPrint(); builder.humanReadable(true); asyncSearchResponse.toXContent(builder, new ToXContent.MapParams(Collections.singletonMap("human", "true"))); - assertEquals("{\n" + - " \"id\" : \"id\",\n" + - " \"is_partial\" : true,\n" + - " \"is_running\" : true,\n" + - " \"start_time\" : \"" + XContentElasticsearchExtension.DEFAULT_FORMATTER.format(date.toInstant()) + "\",\n" + - " \"start_time_in_millis\" : " + date.getTime() + ",\n" + - " \"expiration_time\" : \"" + XContentElasticsearchExtension.DEFAULT_FORMATTER.format(date.toInstant()) + "\",\n" + - " \"expiration_time_in_millis\" : " + date.getTime() + "\n" + - "}", Strings.toString(builder)); + assertEquals( + "{\n" + + " \"id\" : \"id\",\n" + + " \"is_partial\" : true,\n" + + " \"is_running\" : true,\n" + + " \"start_time\" : \"" + + XContentElasticsearchExtension.DEFAULT_FORMATTER.format(date.toInstant()) + + "\",\n" + + " \"start_time_in_millis\" : " + + date.getTime() + + ",\n" + + " \"expiration_time\" : \"" + + XContentElasticsearchExtension.DEFAULT_FORMATTER.format(date.toInstant()) + + "\",\n" + + " \"expiration_time_in_millis\" : " + + date.getTime() + + "\n" + + "}", + Strings.toString(builder) + ); } } } diff --git a/x-pack/plugin/async-search/src/test/java/org/elasticsearch/xpack/search/AsyncSearchTaskTests.java b/x-pack/plugin/async-search/src/test/java/org/elasticsearch/xpack/search/AsyncSearchTaskTests.java index 4affb8204459e..945dc87f4967f 100644 --- a/x-pack/plugin/async-search/src/test/java/org/elasticsearch/xpack/search/AsyncSearchTaskTests.java +++ b/x-pack/plugin/async-search/src/test/java/org/elasticsearch/xpack/search/AsyncSearchTaskTests.java @@ -71,25 +71,62 @@ public void afterTest() { } private AsyncSearchTask createAsyncSearchTask() { - return new AsyncSearchTask(0L, "", "", new TaskId("node1", 0), () -> null, TimeValue.timeValueHours(1), - Collections.emptyMap(), Collections.emptyMap(), new AsyncExecutionId("0", new TaskId("node1", 1)), - new NoOpClient(threadPool), threadPool, (t) -> () -> null); + return new AsyncSearchTask( + 0L, + "", + "", + new TaskId("node1", 0), + () -> null, + TimeValue.timeValueHours(1), + Collections.emptyMap(), + Collections.emptyMap(), + new AsyncExecutionId("0", new TaskId("node1", 1)), + new NoOpClient(threadPool), + threadPool, + (t) -> () -> null + ); } public void testTaskDescription() { SearchRequest searchRequest = new SearchRequest("index1", "index2").source( - new SearchSourceBuilder().query(QueryBuilders.termQuery("field", "value"))); - AsyncSearchTask asyncSearchTask = new AsyncSearchTask(0L, "", "", new TaskId("node1", 0), searchRequest::buildDescription, - TimeValue.timeValueHours(1), Collections.emptyMap(), Collections.emptyMap(), new AsyncExecutionId("0", new TaskId("node1", 1)), - new NoOpClient(threadPool), threadPool, (t) -> () -> null); - assertEquals("async_search{indices[index1,index2], search_type[QUERY_THEN_FETCH], " + - "source[{\"query\":{\"term\":{\"field\":{\"value\":\"value\",\"boost\":1.0}}}}]}", asyncSearchTask.getDescription()); + new SearchSourceBuilder().query(QueryBuilders.termQuery("field", "value")) + ); + AsyncSearchTask asyncSearchTask = new AsyncSearchTask( + 0L, + "", + "", + new TaskId("node1", 0), + searchRequest::buildDescription, + TimeValue.timeValueHours(1), + Collections.emptyMap(), + Collections.emptyMap(), + new AsyncExecutionId("0", new TaskId("node1", 1)), + new NoOpClient(threadPool), + threadPool, + (t) -> () -> null + ); + assertEquals( + "async_search{indices[index1,index2], search_type[QUERY_THEN_FETCH], " + + "source[{\"query\":{\"term\":{\"field\":{\"value\":\"value\",\"boost\":1.0}}}}]}", + asyncSearchTask.getDescription() + ); } public void testWaitForInit() throws InterruptedException { - AsyncSearchTask task = new AsyncSearchTask(0L, "", "", new TaskId("node1", 0), () -> null, TimeValue.timeValueHours(1), - Collections.emptyMap(), Collections.emptyMap(), new AsyncExecutionId("0", new TaskId("node1", 1)), - new NoOpClient(threadPool), threadPool, (t) -> () -> null); + AsyncSearchTask task = new AsyncSearchTask( + 0L, + "", + "", + new TaskId("node1", 0), + () -> null, + TimeValue.timeValueHours(1), + Collections.emptyMap(), + Collections.emptyMap(), + new AsyncExecutionId("0", new TaskId("node1", 1)), + new NoOpClient(threadPool), + threadPool, + (t) -> () -> null + ); int numShards = randomIntBetween(0, 10); List shards = new ArrayList<>(); for (int i = 0; i < numShards; i++) { @@ -121,7 +158,7 @@ public void onFailure(Exception e) { }, TimeValue.timeValueMillis(1))); thread.start(); } - assertFalse(latch.await(numThreads*2, TimeUnit.MILLISECONDS)); + assertFalse(latch.await(numThreads * 2, TimeUnit.MILLISECONDS)); task.getSearchProgressActionListener().onListShards(shards, skippedShards, SearchResponse.Clusters.EMPTY, false); latch.await(); } @@ -147,19 +184,35 @@ public void onFailure(Exception e) { }, TimeValue.timeValueMillis(1))); thread.start(); } - assertFalse(latch.await(numThreads*2, TimeUnit.MILLISECONDS)); + assertFalse(latch.await(numThreads * 2, TimeUnit.MILLISECONDS)); task.getSearchProgressActionListener().onFailure(new Exception("boom")); latch.await(); } public void testWithFailureAndGetResponseFailureDuringReduction() throws InterruptedException { AsyncSearchTask task = createAsyncSearchTask(); - task.getSearchProgressActionListener().onListShards(Collections.emptyList(), Collections.emptyList(), - SearchResponse.Clusters.EMPTY, false); - InternalAggregations aggs = InternalAggregations.from(Collections.singletonList(new StringTerms("name", BucketOrder.key(true), - BucketOrder.key(true), 1, 1, Collections.emptyMap(), DocValueFormat.RAW, 1, false, 1, Collections.emptyList(), 0L))); - task.getSearchProgressActionListener().onPartialReduce(Collections.emptyList(), new TotalHits(0, TotalHits.Relation.EQUAL_TO), - aggs, 1); + task.getSearchProgressActionListener() + .onListShards(Collections.emptyList(), Collections.emptyList(), SearchResponse.Clusters.EMPTY, false); + InternalAggregations aggs = InternalAggregations.from( + Collections.singletonList( + new StringTerms( + "name", + BucketOrder.key(true), + BucketOrder.key(true), + 1, + 1, + Collections.emptyMap(), + DocValueFormat.RAW, + 1, + false, + 1, + Collections.emptyList(), + 0L + ) + ) + ); + task.getSearchProgressActionListener() + .onPartialReduce(Collections.emptyList(), new TotalHits(0, TotalHits.Relation.EQUAL_TO), aggs, 1); task.getSearchProgressActionListener().onFailure(new CircuitBreakingException("boom", CircuitBreaker.Durability.TRANSIENT)); AtomicReference response = new AtomicReference<>(); CountDownLatch latch = new CountDownLatch(1); @@ -206,15 +259,14 @@ public void testWaitForCompletion() throws InterruptedException { int totalShards = numShards + numSkippedShards; task.getSearchProgressActionListener().onListShards(shards, skippedShards, SearchResponse.Clusters.EMPTY, false); for (int i = 0; i < numShards; i++) { - task.getSearchProgressActionListener().onPartialReduce(shards.subList(i, i+1), - new TotalHits(0, TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO), null, 0); + task.getSearchProgressActionListener() + .onPartialReduce(shards.subList(i, i + 1), new TotalHits(0, TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO), null, 0); assertCompletionListeners(task, totalShards, 1 + numSkippedShards, numSkippedShards, 0, true, false); } - task.getSearchProgressActionListener().onFinalReduce(shards, - new TotalHits(0, TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO), null, 0); + task.getSearchProgressActionListener() + .onFinalReduce(shards, new TotalHits(0, TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO), null, 0); assertCompletionListeners(task, totalShards, totalShards, numSkippedShards, 0, true, false); - ((AsyncSearchTask.Listener)task.getProgressListener()).onResponse( - newSearchResponse(totalShards, totalShards, numSkippedShards)); + ((AsyncSearchTask.Listener) task.getProgressListener()).onResponse(newSearchResponse(totalShards, totalShards, numSkippedShards)); assertCompletionListeners(task, totalShards, totalShards, numSkippedShards, 0, false, false); } @@ -233,25 +285,24 @@ public void testWithFetchFailures() throws InterruptedException { int totalShards = numShards + numSkippedShards; task.getSearchProgressActionListener().onListShards(shards, skippedShards, SearchResponse.Clusters.EMPTY, false); for (int i = 0; i < numShards; i++) { - task.getSearchProgressActionListener().onPartialReduce(shards.subList(i, i+1), - new TotalHits(0, TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO), null, 0); + task.getSearchProgressActionListener() + .onPartialReduce(shards.subList(i, i + 1), new TotalHits(0, TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO), null, 0); assertCompletionListeners(task, totalShards, 1 + numSkippedShards, numSkippedShards, 0, true, false); } - task.getSearchProgressActionListener().onFinalReduce(shards, - new TotalHits(0, TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO), null, 0); + task.getSearchProgressActionListener() + .onFinalReduce(shards, new TotalHits(0, TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO), null, 0); int numFetchFailures = randomIntBetween(1, numShards - 1); ShardSearchFailure[] shardSearchFailures = new ShardSearchFailure[numFetchFailures]; for (int i = 0; i < numFetchFailures; i++) { IOException failure = new IOException("boum"); - //fetch failures are currently ignored, they come back with onFailure or onResponse anyways - task.getSearchProgressActionListener().onFetchFailure(i, - new SearchShardTarget("0", new ShardId("0", "0", 1), null), - failure); + // fetch failures are currently ignored, they come back with onFailure or onResponse anyways + task.getSearchProgressActionListener().onFetchFailure(i, new SearchShardTarget("0", new ShardId("0", "0", 1), null), failure); shardSearchFailures[i] = new ShardSearchFailure(failure); } assertCompletionListeners(task, totalShards, totalShards, numSkippedShards, 0, true, false); - ((AsyncSearchTask.Listener)task.getProgressListener()).onResponse( - newSearchResponse(totalShards, totalShards - numFetchFailures, numSkippedShards, shardSearchFailures)); + ((AsyncSearchTask.Listener) task.getProgressListener()).onResponse( + newSearchResponse(totalShards, totalShards - numFetchFailures, numSkippedShards, shardSearchFailures) + ); assertCompletionListeners(task, totalShards, totalShards - numFetchFailures, numSkippedShards, numFetchFailures, false, false); } @@ -270,20 +321,19 @@ public void testFatalFailureDuringFetch() throws InterruptedException { int totalShards = numShards + numSkippedShards; task.getSearchProgressActionListener().onListShards(shards, skippedShards, SearchResponse.Clusters.EMPTY, false); for (int i = 0; i < numShards; i++) { - task.getSearchProgressActionListener().onPartialReduce(shards.subList(0, i+1), - new TotalHits(0, TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO), null, 0); + task.getSearchProgressActionListener() + .onPartialReduce(shards.subList(0, i + 1), new TotalHits(0, TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO), null, 0); assertCompletionListeners(task, totalShards, i + 1 + numSkippedShards, numSkippedShards, 0, true, false); } - task.getSearchProgressActionListener().onFinalReduce(shards, - new TotalHits(0, TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO), null, 0); + task.getSearchProgressActionListener() + .onFinalReduce(shards, new TotalHits(0, TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO), null, 0); for (int i = 0; i < numShards; i++) { - //fetch failures are currently ignored, they come back with onFailure or onResponse anyways - task.getSearchProgressActionListener().onFetchFailure(i, - new SearchShardTarget("0", new ShardId("0", "0", 1), null), - new IOException("boum")); + // fetch failures are currently ignored, they come back with onFailure or onResponse anyways + task.getSearchProgressActionListener() + .onFetchFailure(i, new SearchShardTarget("0", new ShardId("0", "0", 1), null), new IOException("boum")); } assertCompletionListeners(task, totalShards, totalShards, numSkippedShards, 0, true, false); - ((AsyncSearchTask.Listener)task.getProgressListener()).onFailure(new IOException("boum")); + ((AsyncSearchTask.Listener) task.getProgressListener()).onFailure(new IOException("boum")); assertCompletionListeners(task, totalShards, totalShards, numSkippedShards, 0, true, true); } @@ -312,7 +362,7 @@ public void testAddCompletionListenerScheduleErrorWaitForInitListener() throws I AsyncSearchTask asyncSearchTask = createAsyncSearchTask(); AtomicReference failure = new AtomicReference<>(); CountDownLatch latch = new CountDownLatch(1); - //onListShards has not been executed, then addCompletionListener has to wait for the + // onListShards has not been executed, then addCompletionListener has to wait for the // onListShards call and is executed as init listener asyncSearchTask.addCompletionListener(new ActionListener<>() { @Override @@ -326,8 +376,8 @@ public void onFailure(Exception e) { latch.countDown(); } }, TimeValue.timeValueMillis(500L)); - asyncSearchTask.getSearchProgressActionListener().onListShards(Collections.emptyList(), Collections.emptyList(), - SearchResponse.Clusters.EMPTY, false); + asyncSearchTask.getSearchProgressActionListener() + .onListShards(Collections.emptyList(), Collections.emptyList(), SearchResponse.Clusters.EMPTY, false); assertTrue(latch.await(1000, TimeUnit.SECONDS)); assertThat(failure.get(), instanceOf(RuntimeException.class)); } @@ -335,11 +385,11 @@ public void onFailure(Exception e) { public void testAddCompletionListenerScheduleErrorInitListenerExecutedImmediately() throws InterruptedException { throwOnSchedule = true; AsyncSearchTask asyncSearchTask = createAsyncSearchTask(); - asyncSearchTask.getSearchProgressActionListener().onListShards(Collections.emptyList(), Collections.emptyList(), - SearchResponse.Clusters.EMPTY, false); + asyncSearchTask.getSearchProgressActionListener() + .onListShards(Collections.emptyList(), Collections.emptyList(), SearchResponse.Clusters.EMPTY, false); CountDownLatch latch = new CountDownLatch(1); AtomicReference failure = new AtomicReference<>(); - //onListShards has already been executed, then addCompletionListener is executed immediately + // onListShards has already been executed, then addCompletionListener is executed immediately asyncSearchTask.addCompletionListener(new ActionListener<>() { @Override public void onResponse(AsyncSearchResponse asyncSearchResponse) { @@ -356,21 +406,42 @@ public void onFailure(Exception e) { assertThat(failure.get(), instanceOf(RuntimeException.class)); } - private static SearchResponse newSearchResponse(int totalShards, int successfulShards, int skippedShards, - ShardSearchFailure... failures) { - InternalSearchResponse response = new InternalSearchResponse(SearchHits.empty(), - InternalAggregations.EMPTY, null, null, false, null, 1); - return new SearchResponse(response, null, totalShards, successfulShards, skippedShards, - 100, failures, SearchResponse.Clusters.EMPTY); + private static SearchResponse newSearchResponse( + int totalShards, + int successfulShards, + int skippedShards, + ShardSearchFailure... failures + ) { + InternalSearchResponse response = new InternalSearchResponse( + SearchHits.empty(), + InternalAggregations.EMPTY, + null, + null, + false, + null, + 1 + ); + return new SearchResponse( + response, + null, + totalShards, + successfulShards, + skippedShards, + 100, + failures, + SearchResponse.Clusters.EMPTY + ); } - private static void assertCompletionListeners(AsyncSearchTask task, - int expectedTotalShards, - int expectedSuccessfulShards, - int expectedSkippedShards, - int expectedShardFailures, - boolean isPartial, - boolean totalFailureExpected) throws InterruptedException { + private static void assertCompletionListeners( + AsyncSearchTask task, + int expectedTotalShards, + int expectedSuccessfulShards, + int expectedSkippedShards, + int expectedShardFailures, + boolean isPartial, + boolean totalFailureExpected + ) throws InterruptedException { int numThreads = randomIntBetween(1, 10); CountDownLatch latch = new CountDownLatch(numThreads); for (int i = 0; i < numThreads; i++) { diff --git a/x-pack/plugin/async-search/src/test/java/org/elasticsearch/xpack/search/AsyncStatusResponseTests.java b/x-pack/plugin/async-search/src/test/java/org/elasticsearch/xpack/search/AsyncStatusResponseTests.java index c6ed72df8f5e1..887e15e099ba1 100644 --- a/x-pack/plugin/async-search/src/test/java/org/elasticsearch/xpack/search/AsyncStatusResponseTests.java +++ b/x-pack/plugin/async-search/src/test/java/org/elasticsearch/xpack/search/AsyncStatusResponseTests.java @@ -9,15 +9,16 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.test.AbstractWireSerializingTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.rest.RestStatus; import org.elasticsearch.xpack.core.search.action.AsyncStatusResponse; -import org.elasticsearch.test.AbstractWireSerializingTestCase; import java.io.IOException; import java.util.Date; + import static org.elasticsearch.xpack.core.async.GetAsyncResultRequestTests.randomSearchId; public class AsyncStatusResponseTests extends AbstractWireSerializingTestCase { @@ -76,26 +77,44 @@ protected AsyncStatusResponse mutateInstance(AsyncStatusResponse instance) { public void testToXContent() throws IOException { AsyncStatusResponse response = createTestInstance(); try (XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent())) { - String expectedJson = "{\n" + - " \"id\" : \"" + response.getId() + "\",\n" + - " \"is_running\" : " + response.isRunning() + ",\n" + - " \"is_partial\" : " + response.isPartial() + ",\n" + - " \"start_time_in_millis\" : " + response.getStartTime() + ",\n" + - " \"expiration_time_in_millis\" : " + response.getExpirationTime() + ",\n" + - " \"_shards\" : {\n" + - " \"total\" : " + response.getTotalShards() + ",\n" + - " \"successful\" : " + response.getSuccessfulShards() + ",\n" + - " \"skipped\" : " + response.getSkippedShards() + ",\n" + - " \"failed\" : " + response.getFailedShards() + "\n"; + String expectedJson = "{\n" + + " \"id\" : \"" + + response.getId() + + "\",\n" + + " \"is_running\" : " + + response.isRunning() + + ",\n" + + " \"is_partial\" : " + + response.isPartial() + + ",\n" + + " \"start_time_in_millis\" : " + + response.getStartTime() + + ",\n" + + " \"expiration_time_in_millis\" : " + + response.getExpirationTime() + + ",\n" + + " \"_shards\" : {\n" + + " \"total\" : " + + response.getTotalShards() + + ",\n" + + " \"successful\" : " + + response.getSuccessfulShards() + + ",\n" + + " \"skipped\" : " + + response.getSkippedShards() + + ",\n" + + " \"failed\" : " + + response.getFailedShards() + + "\n"; if (response.getCompletionStatus() == null) { - expectedJson = expectedJson + - " }\n" + - "}"; + expectedJson = expectedJson + " }\n" + "}"; } else { - expectedJson = expectedJson + - " },\n" + - " \"completion_status\" : " + response.getCompletionStatus().getStatus() + "\n" + - "}"; + expectedJson = expectedJson + + " },\n" + + " \"completion_status\" : " + + response.getCompletionStatus().getStatus() + + "\n" + + "}"; } builder.prettyPrint(); response.toXContent(builder, ToXContent.EMPTY_PARAMS); diff --git a/x-pack/plugin/async-search/src/test/java/org/elasticsearch/xpack/search/CancellingAggregationBuilder.java b/x-pack/plugin/async-search/src/test/java/org/elasticsearch/xpack/search/CancellingAggregationBuilder.java index 9eb3ac1c0e9c4..193079804c444 100644 --- a/x-pack/plugin/async-search/src/test/java/org/elasticsearch/xpack/search/CancellingAggregationBuilder.java +++ b/x-pack/plugin/async-search/src/test/java/org/elasticsearch/xpack/search/CancellingAggregationBuilder.java @@ -8,8 +8,6 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.xcontent.ConstructingObjectParser; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.aggregations.AbstractAggregationBuilder; import org.elasticsearch.search.aggregations.AggregationBuilder; @@ -19,6 +17,8 @@ import org.elasticsearch.search.aggregations.CardinalityUpperBound; import org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder; import org.elasticsearch.search.aggregations.support.AggregationContext; +import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.Map; @@ -67,21 +67,25 @@ protected XContentBuilder internalXContent(XContentBuilder builder, Params param return builder; } - static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(NAME, false, (args, name) -> new CancellingAggregationBuilder(name, 0L)); - + static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + false, + (args, name) -> new CancellingAggregationBuilder(name, 0L) + ); @Override - protected AggregatorFactory doBuild(AggregationContext context, AggregatorFactory parent, - AggregatorFactories.Builder subfactoriesBuilder) throws IOException { + protected AggregatorFactory doBuild( + AggregationContext context, + AggregatorFactory parent, + AggregatorFactories.Builder subfactoriesBuilder + ) throws IOException { final FilterAggregationBuilder filterAgg = new FilterAggregationBuilder(name, QueryBuilders.matchAllQuery()); filterAgg.subAggregations(subfactoriesBuilder); final AggregatorFactory factory = filterAgg.build(context, parent); return new AggregatorFactory(name, context, parent, subfactoriesBuilder, metadata) { @Override - protected Aggregator createInternal(Aggregator parent, - CardinalityUpperBound cardinality, - Map metadata) throws IOException { + protected Aggregator createInternal(Aggregator parent, CardinalityUpperBound cardinality, Map metadata) + throws IOException { while (context.isCancelled() == false) { try { Thread.sleep(SLEEP_TIME); diff --git a/x-pack/plugin/async-search/src/test/java/org/elasticsearch/xpack/search/RestSubmitAsyncSearchActionTests.java b/x-pack/plugin/async-search/src/test/java/org/elasticsearch/xpack/search/RestSubmitAsyncSearchActionTests.java index 89c8fb8512ef5..75e5925dafecd 100644 --- a/x-pack/plugin/async-search/src/test/java/org/elasticsearch/xpack/search/RestSubmitAsyncSearchActionTests.java +++ b/x-pack/plugin/async-search/src/test/java/org/elasticsearch/xpack/search/RestSubmitAsyncSearchActionTests.java @@ -8,12 +8,12 @@ import org.apache.lucene.util.SetOnce; import org.elasticsearch.common.bytes.BytesArray; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.test.rest.FakeRestRequest; import org.elasticsearch.test.rest.RestActionTestCase; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.search.action.AsyncSearchResponse; import org.elasticsearch.xpack.core.search.action.SubmitAsyncSearchRequest; import org.junit.Before; @@ -44,47 +44,61 @@ public void setUpAction() { public void testRequestParameterDefaults() throws IOException { SetOnce executeCalled = new SetOnce<>(); verifyingClient.setExecuteLocallyVerifier((actionType, request) -> { - assertThat(request, instanceOf(SubmitAsyncSearchRequest.class)); - SubmitAsyncSearchRequest submitRequest = (SubmitAsyncSearchRequest) request; - assertThat(submitRequest.getWaitForCompletionTimeout(), equalTo(TimeValue.timeValueSeconds(1))); - assertThat(submitRequest.isKeepOnCompletion(), equalTo(false)); - assertThat(submitRequest.getKeepAlive(), equalTo(TimeValue.timeValueDays(5))); - // check parameters we implicitly set in the SubmitAsyncSearchRequest ctor - assertThat(submitRequest.getSearchRequest().isCcsMinimizeRoundtrips(), equalTo(false)); - assertThat(submitRequest.getSearchRequest().getBatchedReduceSize(), equalTo(5)); - assertThat(submitRequest.getSearchRequest().requestCache(), equalTo(true)); - assertThat(submitRequest.getSearchRequest().getPreFilterShardSize().intValue(), equalTo(1)); - executeCalled.set(true); - return new AsyncSearchResponse("", randomBoolean(), randomBoolean(), 0L, 0L); - }); - RestRequest submitAsyncRestRequest = new FakeRestRequest.Builder(xContentRegistry()) - .withMethod(RestRequest.Method.POST) - .withPath("/test_index/_async_search") - .withContent(new BytesArray("{}"), XContentType.JSON) - .build(); - dispatchRequest(submitAsyncRestRequest); - assertThat(executeCalled.get(), equalTo(true)); + assertThat(request, instanceOf(SubmitAsyncSearchRequest.class)); + SubmitAsyncSearchRequest submitRequest = (SubmitAsyncSearchRequest) request; + assertThat(submitRequest.getWaitForCompletionTimeout(), equalTo(TimeValue.timeValueSeconds(1))); + assertThat(submitRequest.isKeepOnCompletion(), equalTo(false)); + assertThat(submitRequest.getKeepAlive(), equalTo(TimeValue.timeValueDays(5))); + // check parameters we implicitly set in the SubmitAsyncSearchRequest ctor + assertThat(submitRequest.getSearchRequest().isCcsMinimizeRoundtrips(), equalTo(false)); + assertThat(submitRequest.getSearchRequest().getBatchedReduceSize(), equalTo(5)); + assertThat(submitRequest.getSearchRequest().requestCache(), equalTo(true)); + assertThat(submitRequest.getSearchRequest().getPreFilterShardSize().intValue(), equalTo(1)); + executeCalled.set(true); + return new AsyncSearchResponse("", randomBoolean(), randomBoolean(), 0L, 0L); + }); + RestRequest submitAsyncRestRequest = new FakeRestRequest.Builder(xContentRegistry()).withMethod(RestRequest.Method.POST) + .withPath("/test_index/_async_search") + .withContent(new BytesArray("{}"), XContentType.JSON) + .build(); + dispatchRequest(submitAsyncRestRequest); + assertThat(executeCalled.get(), equalTo(true)); } public void testParameters() throws Exception { String tvString = randomTimeValue(1, 100); doTestParameter("keep_alive", tvString, TimeValue.parseTimeValue(tvString, ""), SubmitAsyncSearchRequest::getKeepAlive); - doTestParameter("wait_for_completion_timeout", tvString, TimeValue.parseTimeValue(tvString, ""), - SubmitAsyncSearchRequest::getWaitForCompletionTimeout); + doTestParameter( + "wait_for_completion_timeout", + tvString, + TimeValue.parseTimeValue(tvString, ""), + SubmitAsyncSearchRequest::getWaitForCompletionTimeout + ); boolean keepOnCompletion = randomBoolean(); - doTestParameter("keep_on_completion", Boolean.toString(keepOnCompletion), keepOnCompletion, - SubmitAsyncSearchRequest::isKeepOnCompletion); + doTestParameter( + "keep_on_completion", + Boolean.toString(keepOnCompletion), + keepOnCompletion, + SubmitAsyncSearchRequest::isKeepOnCompletion + ); boolean requestCache = randomBoolean(); - doTestParameter("request_cache", Boolean.toString(requestCache), requestCache, - r -> r.getSearchRequest().requestCache()); + doTestParameter("request_cache", Boolean.toString(requestCache), requestCache, r -> r.getSearchRequest().requestCache()); int batchedReduceSize = randomIntBetween(2, 50); - doTestParameter("batched_reduce_size", Integer.toString(batchedReduceSize), batchedReduceSize, - r -> r.getSearchRequest().getBatchedReduceSize()); + doTestParameter( + "batched_reduce_size", + Integer.toString(batchedReduceSize), + batchedReduceSize, + r -> r.getSearchRequest().getBatchedReduceSize() + ); } @SuppressWarnings("unchecked") - private void doTestParameter(String paramName, String paramValue, T expectedValue, - Function valueAccessor) throws Exception { + private void doTestParameter( + String paramName, + String paramValue, + T expectedValue, + Function valueAccessor + ) throws Exception { SetOnce executeCalled = new SetOnce<>(); verifyingClient.setExecuteLocallyVerifier((actionType, request) -> { assertThat(request, instanceOf(SubmitAsyncSearchRequest.class)); @@ -95,9 +109,10 @@ private void doTestParameter(String paramName, String paramValue, T expected Map params = new HashMap<>(); params.put(paramName, paramValue); RestRequest submitAsyncRestRequest = new FakeRestRequest.Builder(xContentRegistry()).withMethod(RestRequest.Method.POST) - .withPath("/test_index/_async_search") - .withParams(params) - .withContent(new BytesArray("{}"), XContentType.JSON).build(); + .withPath("/test_index/_async_search") + .withParams(params) + .withContent(new BytesArray("{}"), XContentType.JSON) + .build(); // Get a new context each time, so we don't get exceptions due to trying to add the same header multiple times try (ThreadContext.StoredContext context = verifyingClient.threadPool().getThreadContext().stashContext()) { diff --git a/x-pack/plugin/async-search/src/test/java/org/elasticsearch/xpack/search/SubmitAsyncSearchRequestTests.java b/x-pack/plugin/async-search/src/test/java/org/elasticsearch/xpack/search/SubmitAsyncSearchRequestTests.java index 6e4162ec5ee72..655ebb1f10d7f 100644 --- a/x-pack/plugin/async-search/src/test/java/org/elasticsearch/xpack/search/SubmitAsyncSearchRequestTests.java +++ b/x-pack/plugin/async-search/src/test/java/org/elasticsearch/xpack/search/SubmitAsyncSearchRequestTests.java @@ -36,7 +36,7 @@ protected SubmitAsyncSearchRequest createTestInstance() { final SubmitAsyncSearchRequest searchRequest; if (randomBoolean()) { searchRequest = new SubmitAsyncSearchRequest(generateRandomStringArray(10, 10, false, false)); - } else { + } else { searchRequest = new SubmitAsyncSearchRequest(); } if (randomBoolean()) { @@ -51,8 +51,7 @@ protected SubmitAsyncSearchRequest createTestInstance() { .indicesOptions(IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean())); } if (randomBoolean()) { - searchRequest.getSearchRequest() - .preference(randomAlphaOfLengthBetween(3, 10)); + searchRequest.getSearchRequest().preference(randomAlphaOfLengthBetween(3, 10)); } if (randomBoolean()) { searchRequest.getSearchRequest().requestCache(randomBoolean()); @@ -126,9 +125,14 @@ public void testValidatePreFilterShardSize() { public void testTaskDescription() { SubmitAsyncSearchRequest request = new SubmitAsyncSearchRequest( - new SearchSourceBuilder().query(new MatchAllQueryBuilder()), "index"); + new SearchSourceBuilder().query(new MatchAllQueryBuilder()), + "index" + ); Task task = request.createTask(1, "type", "action", null, Collections.emptyMap()); - assertEquals("waitForCompletionTimeout[1s], keepOnCompletion[false] keepAlive[5d], request=indices[index], " + - "search_type[QUERY_THEN_FETCH], source[{\"query\":{\"match_all\":{\"boost\":1.0}}}]", task.getDescription()); + assertEquals( + "waitForCompletionTimeout[1s], keepOnCompletion[false] keepAlive[5d], request=indices[index], " + + "search_type[QUERY_THEN_FETCH], source[{\"query\":{\"match_all\":{\"boost\":1.0}}}]", + task.getDescription() + ); } } diff --git a/x-pack/plugin/async-search/src/test/java/org/elasticsearch/xpack/search/ThrowingQueryBuilder.java b/x-pack/plugin/async-search/src/test/java/org/elasticsearch/xpack/search/ThrowingQueryBuilder.java index d3f3af0ec7ef2..2056055850545 100644 --- a/x-pack/plugin/async-search/src/test/java/org/elasticsearch/xpack/search/ThrowingQueryBuilder.java +++ b/x-pack/plugin/async-search/src/test/java/org/elasticsearch/xpack/search/ThrowingQueryBuilder.java @@ -14,9 +14,9 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.lucene.search.Queries; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.index.query.AbstractQueryBuilder; import org.elasticsearch.index.query.SearchExecutionContext; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; diff --git a/x-pack/plugin/autoscaling/qa/rest/src/yamlRestTest/java/org/elasticsearch/xpack/autoscaling/AutoscalingRestIT.java b/x-pack/plugin/autoscaling/qa/rest/src/yamlRestTest/java/org/elasticsearch/xpack/autoscaling/AutoscalingRestIT.java index cd619001bfaf7..89bc24ecc1ed0 100644 --- a/x-pack/plugin/autoscaling/qa/rest/src/yamlRestTest/java/org/elasticsearch/xpack/autoscaling/AutoscalingRestIT.java +++ b/x-pack/plugin/autoscaling/qa/rest/src/yamlRestTest/java/org/elasticsearch/xpack/autoscaling/AutoscalingRestIT.java @@ -8,6 +8,7 @@ package org.elasticsearch.xpack.autoscaling; import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; + import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; diff --git a/x-pack/plugin/ccr/qa/downgrade-to-basic-license/src/test/java/org/elasticsearch/xpack/ccr/FollowIndexIT.java b/x-pack/plugin/ccr/qa/downgrade-to-basic-license/src/test/java/org/elasticsearch/xpack/ccr/FollowIndexIT.java index ef6679e578727..98f10a0a08c82 100644 --- a/x-pack/plugin/ccr/qa/downgrade-to-basic-license/src/test/java/org/elasticsearch/xpack/ccr/FollowIndexIT.java +++ b/x-pack/plugin/ccr/qa/downgrade-to-basic-license/src/test/java/org/elasticsearch/xpack/ccr/FollowIndexIT.java @@ -10,12 +10,12 @@ import org.elasticsearch.client.Request; import org.elasticsearch.client.ResponseException; import org.elasticsearch.client.RestClient; -import org.elasticsearch.core.PathUtils; import org.elasticsearch.common.logging.JsonLogLine; import org.elasticsearch.common.logging.JsonLogsStream; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.PathUtils; import org.hamcrest.FeatureMatcher; import org.hamcrest.Matcher; import org.hamcrest.Matchers; @@ -72,8 +72,10 @@ public void testDowngradeRemoteClusterToBasic() throws Exception { Map response = toMap(client().performRequest(statsRequest)); assertThat(eval("auto_follow_stats.number_of_successful_follow_indices", response), equalTo(1)); assertThat(eval("auto_follow_stats.number_of_failed_remote_cluster_state_requests", response), greaterThanOrEqualTo(1)); - assertThat(eval("auto_follow_stats.recent_auto_follow_errors.0.auto_follow_exception.reason", response), - containsString("the license mode [BASIC] on cluster [leader_cluster] does not enable [ccr]")); + assertThat( + eval("auto_follow_stats.recent_auto_follow_errors.0.auto_follow_exception.reason", response), + containsString("the license mode [BASIC] on cluster [leader_cluster] does not enable [ccr]") + ); // Follow indices actively following leader indices before the downgrade to basic license remain to follow // the leader index after the downgrade, so document with id 5 should be replicated to follower index: @@ -105,14 +107,17 @@ private Matcher autoFollowCoordinatorWarn() { @Override protected Boolean featureValueOf(JsonLogLine actual) { - return actual.getLevel().equals("WARN") && - actual.getComponent().contains("AutoFollowCoordinator") && - actual.getNodeName().startsWith("follow-cluster-0") && - actual.getMessage().contains("failure occurred while fetching cluster state for auto follow pattern [test_pattern]") && - actual.stacktrace().get(0) - .contains("org.elasticsearch.ElasticsearchStatusException: can not fetch remote cluster state " + - "as the remote cluster [leader_cluster] is not licensed for [ccr]; the license mode [BASIC]" + - " on cluster [leader_cluster] does not enable [ccr]"); + return actual.getLevel().equals("WARN") + && actual.getComponent().contains("AutoFollowCoordinator") + && actual.getNodeName().startsWith("follow-cluster-0") + && actual.getMessage().contains("failure occurred while fetching cluster state for auto follow pattern [test_pattern]") + && actual.stacktrace() + .get(0) + .contains( + "org.elasticsearch.ElasticsearchStatusException: can not fetch remote cluster state " + + "as the remote cluster [leader_cluster] is not licensed for [ccr]; the license mode [BASIC]" + + " on cluster [leader_cluster] does not enable [ccr]" + ); } }; } @@ -131,9 +136,7 @@ private void createNewIndexAndIndexDocs(RestClient client, String index) throws @Override protected Settings restClientSettings() { String token = basicAuthHeaderValue("admin", new SecureString("admin-password".toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } } diff --git a/x-pack/plugin/ccr/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ccr/AutoFollowIT.java b/x-pack/plugin/ccr/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ccr/AutoFollowIT.java index fd46726a6f725..1941d487e58e4 100644 --- a/x-pack/plugin/ccr/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ccr/AutoFollowIT.java +++ b/x-pack/plugin/ccr/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ccr/AutoFollowIT.java @@ -18,11 +18,11 @@ import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.CheckedRunnable; +import org.elasticsearch.rest.RestStatus; import org.elasticsearch.xcontent.ObjectPath; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.core.CheckedRunnable; -import org.elasticsearch.rest.RestStatus; import java.io.IOException; import java.text.SimpleDateFormat; @@ -96,16 +96,8 @@ public void testMultipleAutoFollowPatternsDifferentClusters() throws Exception { List.of(), List.of("leader_cluster_pattern", "middle_cluster_pattern") ); - cleanUpMiddle( - List.of("index-20200101"), - List.of(), - List.of() - ); - cleanUpLeader( - List.of("index-20190101"), - List.of(), - List.of() - ); + cleanUpMiddle(List.of("index-20200101"), List.of(), List.of()); + cleanUpLeader(List.of("index-20190101"), List.of(), List.of()); } } @@ -219,7 +211,8 @@ public void testPutAutoFollowPatternThatOverridesRequiredLeaderSetting() throws final Map responseAsMap = entityAsMap(response); assertThat(responseAsMap, hasKey("error")); assertThat(responseAsMap.get("error"), instanceOf(Map.class)); - @SuppressWarnings("unchecked") final Map error = (Map) responseAsMap.get("error"); + @SuppressWarnings("unchecked") + final Map error = (Map) responseAsMap.get("error"); assertThat(error, hasEntry("type", "illegal_argument_exception")); assertThat( error, @@ -282,8 +275,13 @@ public void testDataStreams() throws Exception { try (RestClient leaderClient = buildLeaderClient()) { Request rolloverRequest = new Request("POST", "/" + dataStreamName + "/_rollover"); assertOK(leaderClient.performRequest(rolloverRequest)); - verifyDataStream(leaderClient, dataStreamName, backingIndexName(dataStreamName, 1), backingIndexName(dataStreamName, 2), - backingIndexName(dataStreamName, 3)); + verifyDataStream( + leaderClient, + dataStreamName, + backingIndexName(dataStreamName, 1), + backingIndexName(dataStreamName, 2), + backingIndexName(dataStreamName, 3) + ); Request indexRequest = new Request("POST", "/" + dataStreamName + "/_doc"); indexRequest.addParameter("refresh", "true"); @@ -293,8 +291,13 @@ public void testDataStreams() throws Exception { } assertBusy(() -> { assertThat(getNumberOfSuccessfulFollowedIndices(), equalTo(initialNumberOfSuccessfulFollowedIndices + 3)); - verifyDataStream(client(), dataStreamName, backingIndexName(dataStreamName, 1), backingIndexName(dataStreamName, 2), - backingIndexName(dataStreamName, 3)); + verifyDataStream( + client(), + dataStreamName, + backingIndexName(dataStreamName, 1), + backingIndexName(dataStreamName, 2), + backingIndexName(dataStreamName, 3) + ); ensureYellow(dataStreamName); verifyDocuments(client(), dataStreamName, numDocs + 2); }); @@ -446,8 +449,12 @@ public void testRolloverDataStreamInFollowClusterForbidden() throws Exception { { var rolloverRequest1 = new Request("POST", "/" + dataStreamName + "/_rollover"); var e = expectThrows(ResponseException.class, () -> client().performRequest(rolloverRequest1)); - assertThat(e.getMessage(), containsString("data stream [" + dataStreamName + "] cannot be rolled over, " + - "because it is a replicated data stream")); + assertThat( + e.getMessage(), + containsString( + "data stream [" + dataStreamName + "] cannot be rolled over, " + "because it is a replicated data stream" + ) + ); verifyDataStream(client(), dataStreamName, backingIndexName(dataStreamName, 1), backingIndexName(dataStreamName, 2)); // Unfollow .ds-logs-tomcat-prod-000001 @@ -458,8 +465,12 @@ public void testRolloverDataStreamInFollowClusterForbidden() throws Exception { // Try again var rolloverRequest2 = new Request("POST", "/" + dataStreamName + "/_rollover"); e = expectThrows(ResponseException.class, () -> client().performRequest(rolloverRequest2)); - assertThat(e.getMessage(), containsString("data stream [" + dataStreamName + "] cannot be rolled over, " + - "because it is a replicated data stream")); + assertThat( + e.getMessage(), + containsString( + "data stream [" + dataStreamName + "] cannot be rolled over, " + "because it is a replicated data stream" + ) + ); verifyDataStream(client(), dataStreamName, backingIndexName(dataStreamName, 1), backingIndexName(dataStreamName, 2)); // Promote local data stream @@ -500,16 +511,8 @@ public void testRolloverDataStreamInFollowClusterForbidden() throws Exception { backingIndexName(dataStreamName, 3) ); } - cleanUpFollower( - backingIndexNames, - List.of(dataStreamName), - List.of(autoFollowPatternName) - ); - cleanUpLeader( - backingIndexNames.subList(0, 2), - List.of(dataStreamName), - List.of() - ); + cleanUpFollower(backingIndexNames, List.of(dataStreamName), List.of(autoFollowPatternName)); + cleanUpLeader(backingIndexNames.subList(0, 2), List.of(dataStreamName), List.of()); } } @@ -581,10 +584,8 @@ public void testRolloverAliasInFollowClusterForbidden() throws Exception { } } - private static void verifyAlias(RestClient client, - String aliasName, - boolean checkWriteIndex, - String... otherIndices) throws IOException { + private static void verifyAlias(RestClient client, String aliasName, boolean checkWriteIndex, String... otherIndices) + throws IOException { try { var getAliasRequest = new Request("GET", "/_alias/" + aliasName); var responseBody = toMap(client.performRequest(getAliasRequest)); @@ -663,9 +664,12 @@ public void testDataStreamsBiDirectionalReplication() throws Exception { Request createDataStreamRequest = new Request("PUT", "/_data_stream/" + leaderDataStreamName); assertOK(leaderClient.performRequest(createDataStreamRequest)); Request updateAliasesRequest = new Request("POST", "/_aliases"); - updateAliasesRequest.setJsonEntity("{\"actions\":[" + - "{\"add\":{\"index\":\"" + leaderDataStreamName + "\",\"alias\":\"logs-http\",\"is_write_index\":true}}" + - "]}" + updateAliasesRequest.setJsonEntity( + "{\"actions\":[" + + "{\"add\":{\"index\":\"" + + leaderDataStreamName + + "\",\"alias\":\"logs-http\",\"is_write_index\":true}}" + + "]}" ); assertOK(leaderClient.performRequest(updateAliasesRequest)); @@ -688,9 +692,12 @@ public void testDataStreamsBiDirectionalReplication() throws Exception { Request createDataStreamRequest = new Request("PUT", "/_data_stream/" + followerDataStreamName); assertOK(client().performRequest(createDataStreamRequest)); Request updateAliasesRequest = new Request("POST", "/_aliases"); - updateAliasesRequest.setJsonEntity("{\"actions\":[" + - "{\"add\":{\"index\":\"" + followerDataStreamName + "\",\"alias\":\"logs-http\",\"is_write_index\":true}}" + - "]}" + updateAliasesRequest.setJsonEntity( + "{\"actions\":[" + + "{\"add\":{\"index\":\"" + + followerDataStreamName + + "\",\"alias\":\"logs-http\",\"is_write_index\":true}}" + + "]}" ); assertOK(client().performRequest(updateAliasesRequest)); @@ -703,28 +710,28 @@ public void testDataStreamsBiDirectionalReplication() throws Exception { verifyDocuments(client(), followerDataStreamName, numDocs); // TODO: Don't update logs-http alias in follower cluster when data streams are automatically replicated - // from leader to follower cluster: + // from leader to follower cluster: // (only set the write flag to logs-http-na) // Create alias in follower cluster that point to leader and follower data streams: updateAliasesRequest = new Request("POST", "/_aliases"); - updateAliasesRequest.setJsonEntity("{\"actions\":[" + - "{\"add\":{\"index\":\"" + leaderDataStreamName + "\",\"alias\":\"logs-http\"}}" + - "]}" + updateAliasesRequest.setJsonEntity( + "{\"actions\":[" + "{\"add\":{\"index\":\"" + leaderDataStreamName + "\",\"alias\":\"logs-http\"}}" + "]}" ); assertOK(client().performRequest(updateAliasesRequest)); try (var leaderClient = buildLeaderClient()) { assertBusy(() -> { - assertThat(getNumberOfSuccessfulFollowedIndices(leaderClient), - equalTo(initialNumberOfSuccessfulFollowedIndicesInLeaderCluster + 1)); + assertThat( + getNumberOfSuccessfulFollowedIndices(leaderClient), + equalTo(initialNumberOfSuccessfulFollowedIndicesInLeaderCluster + 1) + ); verifyDataStream(leaderClient, followerDataStreamName, backingIndexName(followerDataStreamName, 1)); ensureYellow(followerDataStreamName); verifyDocuments(leaderClient, followerDataStreamName, numDocs); }); updateAliasesRequest = new Request("POST", "/_aliases"); - updateAliasesRequest.setJsonEntity("{\"actions\":[" + - "{\"add\":{\"index\":\"" + followerDataStreamName + "\",\"alias\":\"logs-http\"}}" + - "]}" + updateAliasesRequest.setJsonEntity( + "{\"actions\":[" + "{\"add\":{\"index\":\"" + followerDataStreamName + "\",\"alias\":\"logs-http\"}}" + "]}" ); assertOK(leaderClient.performRequest(updateAliasesRequest)); } @@ -747,9 +754,7 @@ public void testDataStreamsBiDirectionalReplication() throws Exception { } verifyDocuments(leaderClient, leaderDataStreamName, numDocs + moreDocs); } - assertBusy(() -> { - verifyDocuments(client(), leaderDataStreamName, numDocs + moreDocs); - }); + assertBusy(() -> { verifyDocuments(client(), leaderDataStreamName, numDocs + moreDocs); }); } // Index more docs into follower cluster { @@ -761,9 +766,7 @@ public void testDataStreamsBiDirectionalReplication() throws Exception { } verifyDocuments(client(), followerDataStreamName, numDocs + moreDocs); try (var leaderClient = buildLeaderClient()) { - assertBusy(() -> { - verifyDocuments(leaderClient, followerDataStreamName, numDocs + moreDocs); - }); + assertBusy(() -> { verifyDocuments(leaderClient, followerDataStreamName, numDocs + moreDocs); }); } } @@ -800,8 +803,11 @@ public void testAutoFollowSearchableSnapshotsFails() throws Exception { try { try (var leaderClient = buildLeaderClient()) { final String systemPropertyRepoPath = System.getProperty("tests.leader_cluster_repository_path"); - assertThat("Missing system property [tests.leader_cluster_repository_path]", - systemPropertyRepoPath, not(emptyOrNullString())); + assertThat( + "Missing system property [tests.leader_cluster_repository_path]", + systemPropertyRepoPath, + not(emptyOrNullString()) + ); final String repositoryPath = systemPropertyRepoPath + '/' + testPrefix; registerRepository(leaderClient, repository, "fs", true, Settings.builder().put("location", repositoryPath).build()); @@ -850,13 +856,17 @@ public void testAutoFollowSearchableSnapshotsFails() throws Exception { assertLongBusy(() -> { Map response = toMap(getAutoFollowStats()); - assertThat(eval("auto_follow_stats.number_of_failed_follow_indices", response), - greaterThanOrEqualTo(1)); - assertThat(eval("auto_follow_stats.recent_auto_follow_errors", response), - hasSize(greaterThanOrEqualTo(1))); - assertThat(eval("auto_follow_stats.recent_auto_follow_errors.0.auto_follow_exception.reason", response), - containsString("index to follow [" + mountedIndex + "] is a searchable snapshot index and cannot be used " + - "for cross-cluster replication purpose")); + assertThat(eval("auto_follow_stats.number_of_failed_follow_indices", response), greaterThanOrEqualTo(1)); + assertThat(eval("auto_follow_stats.recent_auto_follow_errors", response), hasSize(greaterThanOrEqualTo(1))); + assertThat( + eval("auto_follow_stats.recent_auto_follow_errors.0.auto_follow_exception.reason", response), + containsString( + "index to follow [" + + mountedIndex + + "] is a searchable snapshot index and cannot be used " + + "for cross-cluster replication purpose" + ) + ); ensureYellow(regularIndex); verifyDocuments(client(), regularIndex, 10); }); @@ -894,8 +904,13 @@ private void assertLongBusy(CheckedRunnable runnable) throws Exceptio } catch (AssertionError ae) { try { final String autoFollowStats = EntityUtils.toString(getAutoFollowStats().getEntity()); - logger.warn(() -> new ParameterizedMessage("AssertionError when waiting for auto-follower, auto-follow stats are: {}", - autoFollowStats), ae); + logger.warn( + () -> new ParameterizedMessage( + "AssertionError when waiting for auto-follower, auto-follow stats are: {}", + autoFollowStats + ), + ae + ); } catch (Exception e) { ae.addSuppressed(e); } @@ -906,34 +921,22 @@ private void assertLongBusy(CheckedRunnable runnable) throws Exceptio @Override protected Settings restClientSettings() { String token = basicAuthHeaderValue("admin", new SecureString("admin-password".toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } - private void cleanUpFollower( - final List indices, - final List dataStreams, - final List autoFollowPatterns - ) { + private void cleanUpFollower(final List indices, final List dataStreams, final List autoFollowPatterns) { cleanUp(adminClient(), indices, dataStreams, autoFollowPatterns); } - private void cleanUpMiddle( - final List indices, - final List dataStreams, - final List autoFollowPatterns - ) throws IOException { + private void cleanUpMiddle(final List indices, final List dataStreams, final List autoFollowPatterns) + throws IOException { try (RestClient middleClient = buildMiddleClient()) { cleanUp(middleClient, indices, dataStreams, autoFollowPatterns); } } - private void cleanUpLeader( - final List indices, - final List dataStreams, - final List autoFollowPatterns - ) throws IOException { + private void cleanUpLeader(final List indices, final List dataStreams, final List autoFollowPatterns) + throws IOException { try (RestClient leaderClient = buildLeaderClient()) { cleanUp(leaderClient, indices, dataStreams, autoFollowPatterns); } diff --git a/x-pack/plugin/ccr/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ccr/ChainIT.java b/x-pack/plugin/ccr/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ccr/ChainIT.java index 5b857ebadcaad..c8722ae4417b5 100644 --- a/x-pack/plugin/ccr/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ccr/ChainIT.java +++ b/x-pack/plugin/ccr/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ccr/ChainIT.java @@ -22,11 +22,7 @@ public void testFollowIndex() throws Exception { logger.info("Running against leader cluster"); String mapping = ""; if (randomBoolean()) { // randomly do source filtering on indexing - mapping = - "\"_source\": {" + - " \"includes\": [\"field\"]," + - " \"excludes\": [\"filtered_field\"]" + - "}"; + mapping = "\"_source\": {" + " \"includes\": [\"field\"]," + " \"excludes\": [\"filtered_field\"]" + "}"; } createIndex(leaderIndexName, Settings.EMPTY, mapping); for (int i = 0; i < numDocs; i++) { @@ -72,9 +68,7 @@ public void testFollowIndex() throws Exception { @Override protected Settings restClientSettings() { String token = basicAuthHeaderValue("admin", new SecureString("admin-password".toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } } diff --git a/x-pack/plugin/ccr/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ccr/FollowIndexIT.java b/x-pack/plugin/ccr/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ccr/FollowIndexIT.java index 6b8516b3597ed..9907b2ba71d74 100644 --- a/x-pack/plugin/ccr/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ccr/FollowIndexIT.java +++ b/x-pack/plugin/ccr/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ccr/FollowIndexIT.java @@ -45,11 +45,7 @@ public void testFollowIndex() throws Exception { logger.info("Running against leader cluster"); String mapping = ""; if (randomBoolean()) { // randomly do source filtering on indexing - mapping = - "\"_source\": {" + - " \"includes\": [\"field\"]," + - " \"excludes\": [\"filtered_field\"]" + - "}"; + mapping = "\"_source\": {" + " \"includes\": [\"field\"]," + " \"excludes\": [\"filtered_field\"]" + "}"; } createIndex(leaderIndexName, Settings.EMPTY, mapping); for (int i = 0; i < numDocs; i++) { @@ -115,7 +111,8 @@ public void testFollowThatOverridesRequiredLeaderSetting() throws IOException { final Map responseAsMap = entityAsMap(response); assertThat(responseAsMap, hasKey("error")); assertThat(responseAsMap.get("error"), instanceOf(Map.class)); - @SuppressWarnings("unchecked") final Map error = (Map) responseAsMap.get("error"); + @SuppressWarnings("unchecked") + final Map error = (Map) responseAsMap.get("error"); assertThat(error, hasEntry("type", "illegal_argument_exception")); assertThat( error, @@ -144,18 +141,16 @@ public void testFollowThatOverridesNonExistentSetting() throws IOException { final Map responseAsMap = entityAsMap(response); assertThat(responseAsMap, hasKey("error")); assertThat(responseAsMap.get("error"), instanceOf(Map.class)); - @SuppressWarnings("unchecked") final Map error = (Map) responseAsMap.get("error"); + @SuppressWarnings("unchecked") + final Map error = (Map) responseAsMap.get("error"); assertThat(error, hasEntry("type", "illegal_argument_exception")); - assertThat( - error, - hasEntry("reason", "unknown setting [index.non_existent_setting]") - ); + assertThat(error, hasEntry("reason", "unknown setting [index.non_existent_setting]")); } } public void testFollowNonExistingLeaderIndex() { if ("follow".equals(targetCluster) == false) { - logger.info("skipping test, waiting for target cluster [follow]" ); + logger.info("skipping test, waiting for target cluster [follow]"); return; } ResponseException e = expectThrows(ResponseException.class, () -> resumeFollow("non-existing-index")); @@ -196,8 +191,10 @@ public void testChangeBackingIndexNameFails() throws Exception { verifyDataStream(leaderClient, dataStreamName, DataStream.getDefaultBackingIndexName("logs-foobar-prod", 1)); } - ResponseException failure = expectThrows(ResponseException.class, - () -> followIndex(DataStream.getDefaultBackingIndexName("logs-foobar-prod", 1), ".ds-logs-barbaz-prod-000001")); + ResponseException failure = expectThrows( + ResponseException.class, + () -> followIndex(DataStream.getDefaultBackingIndexName("logs-foobar-prod", 1), ".ds-logs-barbaz-prod-000001") + ); assertThat(failure.getResponse().getStatusLine().getStatusCode(), equalTo(400)); assertThat(failure.getMessage(), containsString("a backing index name in the local and remote cluster must remain the same")); } @@ -234,8 +231,12 @@ public void testFollowSearchableSnapshotsFails() throws Exception { } else { final ResponseException e = expectThrows(ResponseException.class, () -> followIndex(mountedIndex, mountedIndex + "-follower")); - assertThat(e.getMessage(), containsString("is a searchable snapshot index and cannot be used as a leader index for " + - "cross-cluster replication purpose")); + assertThat( + e.getMessage(), + containsString( + "is a searchable snapshot index and cannot be used as a leader index for " + "cross-cluster replication purpose" + ) + ); assertThat(e.getResponse().getStatusLine().getStatusCode(), equalTo(400)); } } @@ -346,13 +347,16 @@ public void testFollowTsdbIndexCanNotOverrideMode() throws Exception { return; } logger.info("Running against follow cluster"); - Exception e = expectThrows(ResponseException.class, () -> followIndex( - client(), - "leader_cluster", - "tsdb_leader", - "tsdb_follower_bad", - Settings.builder().put("index.mode", "standard").build() - )); + Exception e = expectThrows( + ResponseException.class, + () -> followIndex( + client(), + "leader_cluster", + "tsdb_leader", + "tsdb_follower_bad", + Settings.builder().put("index.mode", "standard").build() + ) + ); assertThat( e.getMessage(), containsString("can not put follower index that could override leader settings {\\\"index.mode\\\":\\\"standard\\\"}") @@ -364,13 +368,16 @@ public void testFollowStandardIndexCanNotOverrideMode() throws Exception { return; } logger.info("Running against follow cluster"); - Exception e = expectThrows(ResponseException.class, () -> followIndex( - client(), - "leader_cluster", - "test_index1", - "tsdb_follower_bad", - Settings.builder().put("index.mode", "time_series").build() - )); + Exception e = expectThrows( + ResponseException.class, + () -> followIndex( + client(), + "leader_cluster", + "test_index1", + "tsdb_follower_bad", + Settings.builder().put("index.mode", "time_series").build() + ) + ); assertThat( e.getMessage(), containsString("can not put follower index that could override leader settings {\\\"index.mode\\\":\\\"time_series\\\"}") @@ -380,8 +387,6 @@ public void testFollowStandardIndexCanNotOverrideMode() throws Exception { @Override protected Settings restClientSettings() { String token = basicAuthHeaderValue("admin", new SecureString("admin-password".toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } } diff --git a/x-pack/plugin/ccr/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ccr/XPackUsageIT.java b/x-pack/plugin/ccr/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ccr/XPackUsageIT.java index 7acb53cdbb720..170f34be15e56 100644 --- a/x-pack/plugin/ccr/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ccr/XPackUsageIT.java +++ b/x-pack/plugin/ccr/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ccr/XPackUsageIT.java @@ -24,7 +24,7 @@ public class XPackUsageIT extends ESCCRRestTestCase { public void testXPackCcrUsage() throws Exception { if ("follow".equals(targetCluster) == false) { - logger.info("skipping test, waiting for target cluster [follow]" ); + logger.info("skipping test, waiting for target cluster [follow]"); return; } @@ -82,7 +82,7 @@ private void createLeaderIndex(String indexName) throws IOException { Request request = new Request("GET", "/_xpack/usage"); Map response = toMap(client().performRequest(request)); logger.info("xpack usage response={}", response); - return (Map) response.get("ccr"); + return (Map) response.get("ccr"); } private void assertIndexFollowingActive(String expectedFollowerIndex) throws IOException { @@ -97,9 +97,7 @@ private void assertIndexFollowingActive(String expectedFollowerIndex) throws IOE @Override protected Settings restClientSettings() { String token = basicAuthHeaderValue("admin", new SecureString("admin-password".toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } } diff --git a/x-pack/plugin/ccr/qa/non-compliant-license/src/test/java/org/elasticsearch/xpack/ccr/CcrMultiClusterLicenseIT.java b/x-pack/plugin/ccr/qa/non-compliant-license/src/test/java/org/elasticsearch/xpack/ccr/CcrMultiClusterLicenseIT.java index 7aa486d728737..00773f6c0bf08 100644 --- a/x-pack/plugin/ccr/qa/non-compliant-license/src/test/java/org/elasticsearch/xpack/ccr/CcrMultiClusterLicenseIT.java +++ b/x-pack/plugin/ccr/qa/non-compliant-license/src/test/java/org/elasticsearch/xpack/ccr/CcrMultiClusterLicenseIT.java @@ -39,19 +39,18 @@ public void testAutoFollow() { private static void assertNonCompliantLicense(final Request request, final String fetch) { final ResponseException e = expectThrows(ResponseException.class, () -> client().performRequest(request)); final String expected = String.format( - Locale.ROOT, - "can not fetch %s as the remote cluster [%s] is not licensed for [ccr]; " + - "the license mode [BASIC] on cluster [%2$s] does not enable [ccr]", - fetch, - "leader_cluster"); + Locale.ROOT, + "can not fetch %s as the remote cluster [%s] is not licensed for [ccr]; " + + "the license mode [BASIC] on cluster [%2$s] does not enable [ccr]", + fetch, + "leader_cluster" + ); assertThat(e, hasToString(containsString(expected))); } @Override protected Settings restClientSettings() { String token = basicAuthHeaderValue("admin", new SecureString("admin-password".toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } } diff --git a/x-pack/plugin/ccr/qa/rest/src/yamlRestTest/java/org/elasticsearch/xpack/ccr/CcrRestIT.java b/x-pack/plugin/ccr/qa/rest/src/yamlRestTest/java/org/elasticsearch/xpack/ccr/CcrRestIT.java index c55e3b326face..8b3b9699b371d 100644 --- a/x-pack/plugin/ccr/qa/rest/src/yamlRestTest/java/org/elasticsearch/xpack/ccr/CcrRestIT.java +++ b/x-pack/plugin/ccr/qa/rest/src/yamlRestTest/java/org/elasticsearch/xpack/ccr/CcrRestIT.java @@ -8,6 +8,7 @@ package org.elasticsearch.xpack.ccr; import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; + import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; diff --git a/x-pack/plugin/ccr/qa/restart/src/test/java/org/elasticsearch/xpack/ccr/RestartIT.java b/x-pack/plugin/ccr/qa/restart/src/test/java/org/elasticsearch/xpack/ccr/RestartIT.java index efb5e7a1a86cf..eda5e46bcf334 100644 --- a/x-pack/plugin/ccr/qa/restart/src/test/java/org/elasticsearch/xpack/ccr/RestartIT.java +++ b/x-pack/plugin/ccr/qa/restart/src/test/java/org/elasticsearch/xpack/ccr/RestartIT.java @@ -34,10 +34,12 @@ public void testRestart() throws Exception { // now create an auto-follow pattern for "leader-*" final Request putPatternRequest = new Request("PUT", "/_ccr/auto_follow/leader_cluster_pattern"); - putPatternRequest.setJsonEntity("{" + - "\"leader_index_patterns\": [\"leader-*\"]," + - "\"remote_cluster\": \"leader_cluster\"," + - "\"follow_index_pattern\":\"follow-{{leader_index}}\"}"); + putPatternRequest.setJsonEntity( + "{" + + "\"leader_index_patterns\": [\"leader-*\"]," + + "\"remote_cluster\": \"leader_cluster\"," + + "\"follow_index_pattern\":\"follow-{{leader_index}}\"}" + ); assertOK(client().performRequest(putPatternRequest)); try (RestClient leaderClient = buildLeaderClient()) { // create "leader-1" on the leader, which should be replicated to "follow-leader-1" on the follower @@ -51,11 +53,11 @@ public void testRestart() throws Exception { try (RestClient leaderClient = buildLeaderClient()) { // create "leader-2" on the leader, and index some additional documents into existing indices createIndexAndIndexDocuments("leader-2", numberOfDocuments, leaderClient); - for (final String index : new String[]{"leader", "leader-1", "leader-2"}) { + for (final String index : new String[] { "leader", "leader-1", "leader-2" }) { indexDocuments(index, numberOfDocuments, numberOfDocuments, leaderClient); } // the followers should catch up - for (final String index : new String[]{"follow-leader", "follow-leader-1", "follow-leader-2"}) { + for (final String index : new String[] { "follow-leader", "follow-leader-1", "follow-leader-2" }) { logger.info("verifying {} using {}", index, client().getNodes()); verifyFollower(index, 2 * numberOfDocuments, client()); } @@ -79,11 +81,8 @@ private void createIndexAndIndexDocuments(final String index, final int numberOf indexDocuments(index, numberOfDocuments, 0, client); } - private void indexDocuments( - final String index, - final int numberOfDocuments, - final int initial, - final RestClient client) throws IOException { + private void indexDocuments(final String index, final int numberOfDocuments, final int initial, final RestClient client) + throws IOException { for (int i = 0, j = initial; i < numberOfDocuments; i++, j++) { index(client, index, Integer.toString(j), "field", j); } @@ -100,9 +99,7 @@ private void verifyFollower(final String index, final int numberOfDocuments, fin @Override protected Settings restClientSettings() { String token = basicAuthHeaderValue("admin", new SecureString("admin-password".toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } } diff --git a/x-pack/plugin/ccr/qa/security/src/test/java/org/elasticsearch/xpack/ccr/FollowIndexSecurityIT.java b/x-pack/plugin/ccr/qa/security/src/test/java/org/elasticsearch/xpack/ccr/FollowIndexSecurityIT.java index 7943beca98f70..04477c62c182a 100644 --- a/x-pack/plugin/ccr/qa/security/src/test/java/org/elasticsearch/xpack/ccr/FollowIndexSecurityIT.java +++ b/x-pack/plugin/ccr/qa/security/src/test/java/org/elasticsearch/xpack/ccr/FollowIndexSecurityIT.java @@ -36,23 +36,19 @@ public class FollowIndexSecurityIT extends ESCCRRestTestCase { @Override protected Settings restClientSettings() { String token = basicAuthHeaderValue("test_ccr", new SecureString("x-pack-test-password".toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } @Override protected Settings restAdminSettings() { String token = basicAuthHeaderValue("test_admin", new SecureString("x-pack-test-password".toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } public void testFollowIndex() throws Exception { final int numDocs = 16; final String allowedIndex = "allowed-index"; - final String unallowedIndex = "unallowed-index"; + final String unallowedIndex = "unallowed-index"; if ("leader".equals(targetCluster)) { logger.info("Running against leader cluster"); createIndex(allowedIndex, Settings.EMPTY); @@ -99,8 +95,7 @@ public void testFollowIndex() throws Exception { // User does not have manage_follow_index index privilege for 'unallowedIndex': e = expectThrows(ResponseException.class, () -> followIndex(client(), "leader_cluster", unallowedIndex, unallowedIndex)); - assertThat(e.getMessage(), - containsString("action [indices:admin/xpack/ccr/put_follow] is unauthorized for user [test_ccr]")); + assertThat(e.getMessage(), containsString("action [indices:admin/xpack/ccr/put_follow] is unauthorized for user [test_ccr]")); // Verify that the follow index has not been created and no node tasks are running assertThat(indexExists(unallowedIndex), is(false)); assertBusy(() -> assertThat(countCcrNodeTasks(), equalTo(0))); @@ -108,9 +103,14 @@ public void testFollowIndex() throws Exception { // User does have manage_follow_index index privilege on 'allowed' index, // but not read / monitor roles on 'disallowed' index: e = expectThrows(ResponseException.class, () -> followIndex(client(), "leader_cluster", unallowedIndex, allowedIndex)); - assertThat(e.getMessage(), containsString("insufficient privileges to follow index [unallowed-index], " + - "privilege for action [indices:monitor/stats] is missing, " + - "privilege for action [indices:data/read/xpack/ccr/shard_changes] is missing")); + assertThat( + e.getMessage(), + containsString( + "insufficient privileges to follow index [unallowed-index], " + + "privilege for action [indices:monitor/stats] is missing, " + + "privilege for action [indices:data/read/xpack/ccr/shard_changes] is missing" + ) + ); // Verify that the follow index has not been created and no node tasks are running assertThat(indexExists(unallowedIndex), is(false)); assertBusy(() -> assertThat(countCcrNodeTasks(), equalTo(0))); @@ -119,13 +119,20 @@ public void testFollowIndex() throws Exception { pauseFollow(adminClient(), unallowedIndex); e = expectThrows(ResponseException.class, () -> resumeFollow(unallowedIndex)); - assertThat(e.getMessage(), containsString("insufficient privileges to follow index [unallowed-index], " + - "privilege for action [indices:monitor/stats] is missing, " + - "privilege for action [indices:data/read/xpack/ccr/shard_changes] is missing")); + assertThat( + e.getMessage(), + containsString( + "insufficient privileges to follow index [unallowed-index], " + + "privilege for action [indices:monitor/stats] is missing, " + + "privilege for action [indices:data/read/xpack/ccr/shard_changes] is missing" + ) + ); assertBusy(() -> assertThat(countCcrNodeTasks(), equalTo(0))); - e = expectThrows(ResponseException.class, - () -> client().performRequest(new Request("POST", "/" + unallowedIndex + "/_ccr/unfollow"))); + e = expectThrows( + ResponseException.class, + () -> client().performRequest(new Request("POST", "/" + unallowedIndex + "/_ccr/unfollow")) + ); assertThat(e.getMessage(), containsString("action [indices:admin/xpack/ccr/unfollow] is unauthorized for user [test_ccr]")); assertOK(adminClient().performRequest(new Request("POST", "/" + unallowedIndex + "/_close"))); assertOK(adminClient().performRequest(new Request("POST", "/" + unallowedIndex + "/_ccr/unfollow"))); @@ -150,7 +157,7 @@ public void testAutoFollowPatterns() throws Exception { assertOK(client().performRequest(request)); try (RestClient leaderClient = buildLeaderClient()) { - for (String index : new String[]{allowedIndex, disallowedIndex}) { + for (String index : new String[] { allowedIndex, disallowedIndex }) { String requestBody = "{\"mappings\": {\"properties\": {\"field\": {\"type\": \"keyword\"}}}}"; request = new Request("PUT", "/" + index); request.setJsonEntity(requestBody); @@ -197,12 +204,16 @@ public void testForgetFollower() throws IOException { try (RestClient leaderClient = buildLeaderClient(restAdminSettings())) { final Request request = new Request("POST", "/" + forgetLeader + "/_ccr/forget_follower"); - final String requestBody = "{" + - "\"follower_cluster\":\"follow-cluster\"," + - "\"follower_index\":\"" + forgetFollower + "\"," + - "\"follower_index_uuid\":\"" + followerIndexUUID + "\"," + - "\"leader_remote_cluster\":\"leader_cluster\"" + - "}"; + final String requestBody = "{" + + "\"follower_cluster\":\"follow-cluster\"," + + "\"follower_index\":\"" + + forgetFollower + + "\"," + + "\"follower_index_uuid\":\"" + + followerIndexUUID + + "\"," + + "\"leader_remote_cluster\":\"leader_cluster\"" + + "}"; request.setJsonEntity(requestBody); final Response forgetFollowerResponse = leaderClient.performRequest(request); assertOK(forgetFollowerResponse); @@ -215,8 +226,8 @@ public void testForgetFollower() throws IOException { final Request retentionLeasesRequest = new Request("GET", "/" + forgetLeader + "/_stats"); retentionLeasesRequest.addParameter("level", "shards"); final Response retentionLeasesResponse = leaderClient.performRequest(retentionLeasesRequest); - final ArrayList shardsStats = - ObjectPath.createFromResponse(retentionLeasesResponse).evaluate("indices." + forgetLeader + ".shards.0"); + final ArrayList shardsStats = ObjectPath.createFromResponse(retentionLeasesResponse) + .evaluate("indices." + forgetLeader + ".shards.0"); assertThat(shardsStats, hasSize(1)); final Map shardStatsAsMap = (Map) shardsStats.get(0); final Map retentionLeasesStats = (Map) shardStatsAsMap.get("retention_leases"); @@ -292,7 +303,7 @@ public void testUnPromoteAndFollowDataStream() throws Exception { var promoteRequest = new Request("POST", "/_data_stream/_promote/" + dataStreamName); assertOK(client().performRequest(promoteRequest)); // Now that the data stream is a non replicated data stream, rollover. - var rolloverRequest = new Request("POST", "/" + dataStreamName + "/_rollover"); + var rolloverRequest = new Request("POST", "/" + dataStreamName + "/_rollover"); assertOK(client().performRequest(rolloverRequest)); // Unfollow .ds-logs-eu-monitor1-000001, // which is now possible because this index can now be closed as it is no longer the write index. diff --git a/x-pack/plugin/ccr/qa/src/main/java/org/elasticsearch/xpack/ccr/ESCCRRestTestCase.java b/x-pack/plugin/ccr/qa/src/main/java/org/elasticsearch/xpack/ccr/ESCCRRestTestCase.java index d8483028c1abd..2d8217dbe9e80 100644 --- a/x-pack/plugin/ccr/qa/src/main/java/org/elasticsearch/xpack/ccr/ESCCRRestTestCase.java +++ b/x-pack/plugin/ccr/qa/src/main/java/org/elasticsearch/xpack/ccr/ESCCRRestTestCase.java @@ -15,21 +15,21 @@ import org.elasticsearch.cluster.metadata.DataStream; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.common.xcontent.support.XContentMapValues; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.test.rest.ESRestTestCase; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.json.JsonXContent; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Map; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.rest.action.search.RestSearchAction.TOTAL_HITS_AS_INT_PARAM; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.Matchers.endsWith; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.greaterThan; @@ -120,8 +120,9 @@ protected static void pauseFollow(RestClient client, String followIndex) throws protected static void putAutoFollowPattern(String patternName, String remoteCluster, String indexPattern) throws IOException { Request putPatternRequest = new Request("PUT", "/_ccr/auto_follow/" + patternName); - putPatternRequest.setJsonEntity("{\"leader_index_patterns\": [\"" + indexPattern + "\"], \"remote_cluster\": \"" + - remoteCluster + "\"}"); + putPatternRequest.setJsonEntity( + "{\"leader_index_patterns\": [\"" + indexPattern + "\"], \"remote_cluster\": \"" + remoteCluster + "\"}" + ); assertOK(client().performRequest(putPatternRequest)); } @@ -142,10 +143,8 @@ protected static void verifyDocuments(final String index, final int expectedNumD verifyDocuments(index, expectedNumDocs, query, adminClient()); } - protected static void verifyDocuments(final String index, - final int expectedNumDocs, - final String query, - final RestClient client) throws IOException { + protected static void verifyDocuments(final String index, final int expectedNumDocs, final String query, final RestClient client) + throws IOException { final Request request = new Request("GET", "/" + index + "/_search"); request.addParameter("size", Integer.toString(expectedNumDocs)); request.addParameter("sort", "field:asc"); @@ -164,9 +163,7 @@ protected static void verifyDocuments(final String index, } } - protected static void verifyDocuments(final RestClient client, - final String index, - final int expectedNumDocs) throws IOException { + protected static void verifyDocuments(final RestClient client, final String index, final int expectedNumDocs) throws IOException { final Request request = new Request("GET", "/" + index + "/_search"); request.addParameter(TOTAL_HITS_AS_INT_PARAM, "true"); Map response = toMap(client.performRequest(request)); @@ -201,17 +198,13 @@ protected static void verifyCcrMonitoring(final String expectedLeaderIndex, fina final String followerIndex = (String) XContentMapValues.extractValue("_source.ccr_stats.follower_index", hit); assertThat(followerIndex, equalTo(expectedFollowerIndex)); - int foundFollowerMaxSeqNo = - (int) XContentMapValues.extractValue("_source.ccr_stats.follower_max_seq_no", hit); + int foundFollowerMaxSeqNo = (int) XContentMapValues.extractValue("_source.ccr_stats.follower_max_seq_no", hit); followerMaxSeqNo = Math.max(followerMaxSeqNo, foundFollowerMaxSeqNo); - int foundFollowerMappingVersion = - (int) XContentMapValues.extractValue("_source.ccr_stats.follower_mapping_version", hit); + int foundFollowerMappingVersion = (int) XContentMapValues.extractValue("_source.ccr_stats.follower_mapping_version", hit); followerMappingVersion = Math.max(followerMappingVersion, foundFollowerMappingVersion); - int foundFollowerSettingsVersion = - (int) XContentMapValues.extractValue("_source.ccr_stats.follower_settings_version", hit); + int foundFollowerSettingsVersion = (int) XContentMapValues.extractValue("_source.ccr_stats.follower_settings_version", hit); followerSettingsVersion = Math.max(followerSettingsVersion, foundFollowerSettingsVersion); - int foundFollowerAliasesVersion = - (int) XContentMapValues.extractValue("_source.ccr_stats.follower_aliases_version", hit); + int foundFollowerAliasesVersion = (int) XContentMapValues.extractValue("_source.ccr_stats.follower_aliases_version", hit); followerAliasesVersion = Math.max(followerAliasesVersion, foundFollowerAliasesVersion); } @@ -239,8 +232,10 @@ protected static void verifyAutoFollowMonitoring() throws IOException { for (int i = 0; i < hits.size(); i++) { Map hit = (Map) hits.get(i); - int foundNumberOfOperationsReceived = - (int) XContentMapValues.extractValue("_source.ccr_auto_follow_stats.number_of_successful_follow_indices", hit); + int foundNumberOfOperationsReceived = (int) XContentMapValues.extractValue( + "_source.ccr_auto_follow_stats.number_of_successful_follow_indices", + hit + ); numberOfSuccessfulFollowIndices = Math.max(numberOfSuccessfulFollowIndices, foundNumberOfOperationsReceived); } @@ -305,9 +300,8 @@ protected static boolean indexExists(String index) throws IOException { return RestStatus.OK.getStatus() == response.getStatusLine().getStatusCode(); } - protected static List verifyDataStream(final RestClient client, - final String name, - final String... expectedBackingIndices) throws IOException { + protected static List verifyDataStream(final RestClient client, final String name, final String... expectedBackingIndices) + throws IOException { Request request = new Request("GET", "/_data_stream/" + name); Map response = toMap(client.performRequest(request)); List retrievedDataStreams = (List) response.get("data_streams"); @@ -375,9 +369,12 @@ private RestClient buildClient(final String url) throws IOException { private RestClient buildClient(final String url, final Settings settings) throws IOException { int portSeparator = url.lastIndexOf(':'); - HttpHost httpHost = new HttpHost(url.substring(0, portSeparator), - Integer.parseInt(url.substring(portSeparator + 1)), getProtocol()); - return buildClient(settings, new HttpHost[]{httpHost}); + HttpHost httpHost = new HttpHost( + url.substring(0, portSeparator), + Integer.parseInt(url.substring(portSeparator + 1)), + getProtocol() + ); + return buildClient(settings, new HttpHost[] { httpHost }); } } diff --git a/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/AutoFollowIT.java b/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/AutoFollowIT.java index 29e062883277c..3b16cdd33f5c4 100644 --- a/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/AutoFollowIT.java +++ b/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/AutoFollowIT.java @@ -13,12 +13,12 @@ import org.elasticsearch.client.Client; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.Metadata; -import org.elasticsearch.core.CheckedRunnable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.regex.Regex; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.core.CheckedRunnable; import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.IndexNotFoundException; import org.elasticsearch.indices.SystemIndexDescriptor; @@ -100,18 +100,16 @@ public void testAutoFollow() throws Exception { // Enabling auto following: if (randomBoolean()) { - putAutoFollowPatterns("my-pattern", new String[] {"logs-*", "transactions-*"}); + putAutoFollowPatterns("my-pattern", new String[] { "logs-*", "transactions-*" }); } else { - putAutoFollowPatterns("my-pattern1", new String[] {"logs-*"}); - putAutoFollowPatterns("my-pattern2", new String[] {"transactions-*"}); + putAutoFollowPatterns("my-pattern1", new String[] { "logs-*" }); + putAutoFollowPatterns("my-pattern2", new String[] { "transactions-*" }); } createLeaderIndex("metrics-201901", leaderIndexSettings); createLeaderIndex("logs-201901", leaderIndexSettings); - assertLongBusy(() -> { - assertTrue(ESIntegTestCase.indexExists("copy-logs-201901", followerClient())); - }); + assertLongBusy(() -> { assertTrue(ESIntegTestCase.indexExists("copy-logs-201901", followerClient())); }); createLeaderIndex("transactions-201901", leaderIndexSettings); assertLongBusy(() -> { AutoFollowStats autoFollowStats = getAutoFollowStats(); @@ -124,13 +122,10 @@ public void testAutoFollow() throws Exception { } public void testAutoFollowDoNotFollowSystemIndices() throws Exception { - putAutoFollowPatterns("my-pattern", new String[] {".*", "logs-*"}); + putAutoFollowPatterns("my-pattern", new String[] { ".*", "logs-*" }); // Trigger system index creation - leaderClient().prepareIndex(FakeSystemIndex.SYSTEM_INDEX_NAME) - .setSource(Map.of("a", "b")) - .execute() - .actionGet(); + leaderClient().prepareIndex(FakeSystemIndex.SYSTEM_INDEX_NAME).setSource(Map.of("a", "b")).execute().actionGet(); Settings leaderIndexSettings = Settings.builder() .put(IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), 1) @@ -151,7 +146,7 @@ public void testCleanFollowedLeaderIndexUUIDs() throws Exception { .put(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 0) .build(); - putAutoFollowPatterns("my-pattern", new String[] {"logs-*"}); + putAutoFollowPatterns("my-pattern", new String[] { "logs-*" }); createLeaderIndex("logs-201901", leaderIndexSettings); assertLongBusy(() -> { AutoFollowStats autoFollowStats = getAutoFollowStats(); @@ -174,7 +169,8 @@ public void testCleanFollowedLeaderIndexUUIDs() throws Exception { assertAcked(leaderClient().admin().indices().delete(deleteIndexRequest).actionGet()); assertLongBusy(() -> { - AutoFollowMetadata autoFollowMetadata = getFollowerCluster().clusterService().state() + AutoFollowMetadata autoFollowMetadata = getFollowerCluster().clusterService() + .state() .metadata() .custom(AutoFollowMetadata.TYPE); assertThat(autoFollowMetadata, notNullValue()); @@ -189,7 +185,7 @@ public void testAutoFollowManyIndices() throws Exception { .put(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 0) .build(); - putAutoFollowPatterns("my-pattern", new String[] {"logs-*"}); + putAutoFollowPatterns("my-pattern", new String[] { "logs-*" }); long numIndices = randomIntBetween(4, 8); for (int i = 0; i < numIndices; i++) { createLeaderIndex("logs-" + i, leaderIndexSettings); @@ -221,7 +217,7 @@ public void testAutoFollowManyIndices() throws Exception { metadata[0] = getFollowerCluster().clusterService().state().metadata(); autoFollowStats[0] = getAutoFollowStats(); - assertThat(metadata[0].indices().size(), equalTo((int )expectedVal1)); + assertThat(metadata[0].indices().size(), equalTo((int) expectedVal1)); AutoFollowMetadata autoFollowMetadata = metadata[0].custom(AutoFollowMetadata.TYPE); assertThat(autoFollowMetadata.getFollowedLeaderIndexUUIDs().get("my-pattern"), nullValue()); assertThat(autoFollowStats[0].getAutoFollowedClusters().size(), equalTo(0)); @@ -233,7 +229,7 @@ public void testAutoFollowManyIndices() throws Exception { } createLeaderIndex("logs-does-not-count", leaderIndexSettings); - putAutoFollowPatterns("my-pattern", new String[] {"logs-*"}); + putAutoFollowPatterns("my-pattern", new String[] { "logs-*" }); long i = numIndices; numIndices = numIndices + randomIntBetween(4, 8); for (; i < numIndices; i++) { @@ -310,7 +306,7 @@ public void testAutoFollowParameterAreDelegated() throws Exception { followInfoRequest.setFollowerIndices("copy-logs-201901"); FollowInfoAction.Response followInfoResponse; try { - followInfoResponse = followerClient().execute(FollowInfoAction.INSTANCE, followInfoRequest).actionGet(); + followInfoResponse = followerClient().execute(FollowInfoAction.INSTANCE, followInfoRequest).actionGet(); } catch (IndexNotFoundException e) { throw new AssertionError(e); } @@ -330,16 +326,22 @@ public void testAutoFollowParameterAreDelegated() throws Exception { assertThat(followParameters.getMaxWriteBufferSize(), equalTo(request.getParameters().getMaxWriteBufferSize())); } if (request.getParameters().getMaxOutstandingReadRequests() != null) { - assertThat(followParameters.getMaxOutstandingReadRequests(), - equalTo(request.getParameters().getMaxOutstandingReadRequests())); + assertThat( + followParameters.getMaxOutstandingReadRequests(), + equalTo(request.getParameters().getMaxOutstandingReadRequests()) + ); } if (request.getParameters().getMaxOutstandingWriteRequests() != null) { - assertThat(followParameters.getMaxOutstandingWriteRequests(), - equalTo(request.getParameters().getMaxOutstandingWriteRequests())); + assertThat( + followParameters.getMaxOutstandingWriteRequests(), + equalTo(request.getParameters().getMaxOutstandingWriteRequests()) + ); } if (request.getParameters().getMaxReadRequestOperationCount() != null) { - assertThat(followParameters.getMaxReadRequestOperationCount(), - equalTo(request.getParameters().getMaxReadRequestOperationCount())); + assertThat( + followParameters.getMaxReadRequestOperationCount(), + equalTo(request.getParameters().getMaxReadRequestOperationCount()) + ); } if (request.getParameters().getMaxReadRequestSize() != null) { assertThat(followParameters.getMaxReadRequestSize(), equalTo(request.getParameters().getMaxReadRequestSize())); @@ -351,8 +353,10 @@ public void testAutoFollowParameterAreDelegated() throws Exception { assertThat(followParameters.getReadPollTimeout(), equalTo(request.getParameters().getReadPollTimeout())); } if (request.getParameters().getMaxWriteRequestOperationCount() != null) { - assertThat(followParameters.getMaxWriteRequestOperationCount(), - equalTo(request.getParameters().getMaxWriteRequestOperationCount())); + assertThat( + followParameters.getMaxWriteRequestOperationCount(), + equalTo(request.getParameters().getMaxWriteRequestOperationCount()) + ); } if (request.getParameters().getMaxWriteRequestSize() != null) { assertThat(followParameters.getMaxWriteRequestSize(), equalTo(request.getParameters().getMaxWriteRequestSize())); @@ -367,8 +371,8 @@ public void testConflictingPatterns() throws Exception { .build(); // Enabling auto following: - putAutoFollowPatterns("my-pattern1", new String[] {"logs-*"}); - putAutoFollowPatterns("my-pattern2", new String[] {"logs-2018*"}); + putAutoFollowPatterns("my-pattern1", new String[] { "logs-*" }); + putAutoFollowPatterns("my-pattern2", new String[] { "logs-2018*" }); createLeaderIndex("logs-201701", leaderIndexSettings); assertLongBusy(() -> { @@ -389,13 +393,17 @@ public void testConflictingPatterns() throws Exception { assertThat(autoFollowStats.getRecentAutoFollowErrors().size(), equalTo(2)); ElasticsearchException autoFollowError1 = autoFollowStats.getRecentAutoFollowErrors().get("my-pattern1:logs-201801").v2(); assertThat(autoFollowError1, notNullValue()); - assertThat(autoFollowError1.getRootCause().getMessage(), equalTo("index to follow [logs-201801] for pattern [my-pattern1] " + - "matches with other patterns [my-pattern2]")); + assertThat( + autoFollowError1.getRootCause().getMessage(), + equalTo("index to follow [logs-201801] for pattern [my-pattern1] " + "matches with other patterns [my-pattern2]") + ); ElasticsearchException autoFollowError2 = autoFollowStats.getRecentAutoFollowErrors().get("my-pattern2:logs-201801").v2(); assertThat(autoFollowError2, notNullValue()); - assertThat(autoFollowError2.getRootCause().getMessage(), equalTo("index to follow [logs-201801] for pattern [my-pattern2] " + - "matches with other patterns [my-pattern1]")); + assertThat( + autoFollowError2.getRootCause().getMessage(), + equalTo("index to follow [logs-201801] for pattern [my-pattern2] " + "matches with other patterns [my-pattern1]") + ); }); assertFalse(ESIntegTestCase.indexExists("copy-logs-201801", followerClient())); @@ -411,7 +419,7 @@ public void testPauseAndResumeAutoFollowPattern() throws Exception { createLeaderIndex("test-existing-index-is-ignored", leaderIndexSettings); // create the auto follow pattern - putAutoFollowPatterns("test-pattern", new String[]{"test-*", "tests-*"}); + putAutoFollowPatterns("test-pattern", new String[] { "test-*", "tests-*" }); assertLongBusy(() -> { final AutoFollowStats autoFollowStats = getAutoFollowStats(); assertThat(autoFollowStats.getAutoFollowedClusters().size(), equalTo(1)); @@ -463,8 +471,20 @@ public void testPauseAndResumeAutoFollowPattern() throws Exception { assertLongBusy(() -> { final Client client = followerClient(); assertThat(getAutoFollowStats().getAutoFollowedClusters().size(), equalTo(1)); - assertThat(client.admin().cluster().prepareState().clear().setIndices("copy-*").setMetadata(true).get() - .getState().getMetadata().getIndices().size(), equalTo(1 + nbIndicesCreatedWhilePaused)); + assertThat( + client.admin() + .cluster() + .prepareState() + .clear() + .setIndices("copy-*") + .setMetadata(true) + .get() + .getState() + .getMetadata() + .getIndices() + .size(), + equalTo(1 + nbIndicesCreatedWhilePaused) + ); for (int i = 0; i < nbIndicesCreatedWhilePaused; i++) { assertTrue(ESIntegTestCase.indexExists("copy-test-index-created-while-pattern-is-paused-" + i, client)); } @@ -477,15 +497,14 @@ public void testPauseAndResumeWithMultipleAutoFollowPatterns() throws Exception .put(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 0) .build(); - final String[] prefixes = {"logs-", "users-", "docs-", "monitoring-", "data-", "system-", "events-", "files-"}; + final String[] prefixes = { "logs-", "users-", "docs-", "monitoring-", "data-", "system-", "events-", "files-" }; // create an auto follow pattern for each prefix - final List autoFollowPatterns = Arrays.stream(prefixes) - .map(prefix -> { - final String pattern = prefix + "pattern"; - putAutoFollowPatterns(pattern, new String[]{prefix + "*"}); - return pattern; - }).collect(toUnmodifiableList()); + final List autoFollowPatterns = Arrays.stream(prefixes).map(prefix -> { + final String pattern = prefix + "pattern"; + putAutoFollowPatterns(pattern, new String[] { prefix + "*" }); + return pattern; + }).collect(toUnmodifiableList()); // pick up some random pattern to pause final List pausedAutoFollowerPatterns = randomSubsetOf(randomIntBetween(1, 3), autoFollowPatterns); @@ -530,8 +549,14 @@ public void testPauseAndResumeWithMultipleAutoFollowPatterns() throws Exception // now pause some random patterns pausedAutoFollowerPatterns.forEach(this::pauseAutoFollowPattern); - assertLongBusy(() -> autoFollowPatterns.forEach(pattern -> - assertThat(getAutoFollowPattern(pattern).isActive(), equalTo(pausedAutoFollowerPatterns.contains(pattern) == false)))); + assertLongBusy( + () -> autoFollowPatterns.forEach( + pattern -> assertThat( + getAutoFollowPattern(pattern).isActive(), + equalTo(pausedAutoFollowerPatterns.contains(pattern) == false) + ) + ) + ); // wait for more leader indices to be created on the remote cluster latchSix.await(60L, TimeUnit.SECONDS); @@ -555,8 +580,13 @@ public void testPauseAndResumeWithMultipleAutoFollowPatterns() throws Exception final String leaderIndex = leaderIndexMetadata.getIndex().getName(); if (Regex.simpleMatch(matchingPrefixes, leaderIndex)) { String followingIndex = "copy-" + leaderIndex; - assertBusy(() -> assertThat("Following index [" + followingIndex + "] must exists", - ESIntegTestCase.indexExists(followingIndex, followerClient()), is(true))); + assertBusy( + () -> assertThat( + "Following index [" + followingIndex + "] must exists", + ESIntegTestCase.indexExists(followingIndex, followerClient()), + is(true) + ) + ); } } @@ -572,7 +602,7 @@ public void testAutoFollowExclusion() throws Exception { .put(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 0) .build(); - putAutoFollowPatterns("my-pattern1", new String[] {"logs-*"}, Collections.singletonList("logs-2018*")); + putAutoFollowPatterns("my-pattern1", new String[] { "logs-*" }, Collections.singletonList("logs-2018*")); createLeaderIndex("logs-201801", leaderIndexSettings); AutoFollowStats autoFollowStats = getAutoFollowStats(); @@ -653,8 +683,13 @@ private void assertLongBusy(CheckedRunnable codeBlock) throws Excepti ae.addSuppressed(e); } final AutoFollowStats finalAutoFollowStats = autoFollowStats; - logger.warn(() -> new ParameterizedMessage("AssertionError when waiting for auto-follower, auto-follow stats are: {}", - finalAutoFollowStats != null ? Strings.toString(finalAutoFollowStats) : "null"), ae); + logger.warn( + () -> new ParameterizedMessage( + "AssertionError when waiting for auto-follower, auto-follow stats are: {}", + finalAutoFollowStats != null ? Strings.toString(finalAutoFollowStats) : "null" + ), + ae + ); throw ae; } } diff --git a/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/CcrAliasesIT.java b/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/CcrAliasesIT.java index 924504fd896da..b2c552ab2207d 100644 --- a/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/CcrAliasesIT.java +++ b/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/CcrAliasesIT.java @@ -16,18 +16,18 @@ import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.AliasMetadata; import org.elasticsearch.common.CheckedBiConsumer; +import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.core.CheckedConsumer; import org.elasticsearch.core.CheckedRunnable; -import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.persistent.PersistentTasksCustomMetadata; import org.elasticsearch.rest.action.admin.indices.AliasesNotFoundException; import org.elasticsearch.tasks.TaskInfo; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.CcrIntegTestCase; -import org.elasticsearch.xpack.core.ccr.action.ShardFollowTask; import org.elasticsearch.xpack.core.ccr.action.PutFollowAction; +import org.elasticsearch.xpack.core.ccr.action.ShardFollowTask; import java.util.ArrayList; import java.util.Collection; @@ -38,9 +38,9 @@ import java.util.concurrent.CyclicBarrier; import java.util.concurrent.ExecutionException; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.index.query.QueryBuilders.termQuery; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasSize; @@ -103,9 +103,8 @@ private void runAddAliasTest(final Boolean isWriteAlias) throws Exception { * @param the type of checked exception the post assertions callback can throw * @throws Exception if a checked exception is thrown while executing the add alias test */ - private void runAddAliasTest( - final Boolean isWriteIndex, - final CheckedConsumer postAssertions) throws Exception { + private void runAddAliasTest(final Boolean isWriteIndex, final CheckedConsumer postAssertions) + throws Exception { assertAcked(leaderClient().admin().indices().prepareCreate("leader")); final PutFollowAction.Request followRequest = putFollow("leader", "follower"); // we set a low poll timeout so that shard changes requests are responded to quickly even without indexing @@ -196,28 +195,28 @@ public void testAddMultipleAliasesSequentially() throws Exception { public void testUpdateExistingAlias() throws Exception { runAddAliasTest( - null, - /* - * After the alias is added (via runAddAliasTest) we modify the alias in place, and then assert that the modification is - * eventually replicated. - */ - aliasName -> { - assertAcked(leaderClient().admin() - .indices() - .prepareAliases() - .addAlias("leader", aliasName, termQuery(randomAlphaOfLength(16), randomAlphaOfLength(16)))); - assertAliasesExist("leader", "follower", aliasName); - }); + null, + /* + * After the alias is added (via runAddAliasTest) we modify the alias in place, and then assert that the modification is + * eventually replicated. + */ + aliasName -> { + assertAcked( + leaderClient().admin() + .indices() + .prepareAliases() + .addAlias("leader", aliasName, termQuery(randomAlphaOfLength(16), randomAlphaOfLength(16))) + ); + assertAliasesExist("leader", "follower", aliasName); + } + ); } public void testRemoveExistingAlias() throws Exception { - runAddAliasTest( - false, - aliasName -> { - removeAlias(aliasName); - assertAliasExistence(aliasName, false); - } - ); + runAddAliasTest(false, aliasName -> { + removeAlias(aliasName); + assertAliasExistence(aliasName, false); + }); } private void removeAlias(final String aliasName) { @@ -316,42 +315,55 @@ private void assertAliasesExist(final String leaderIndex, final String followerI } private void assertAliasesExist( - final String leaderIndex, - final String followerIndex, - final CheckedBiConsumer aliasMetadataAssertion, - final String... aliases) throws Exception { + final String leaderIndex, + final String followerIndex, + final CheckedBiConsumer aliasMetadataAssertion, + final String... aliases + ) throws Exception { // we must check serially because aliases exist will return true if any but not necessarily all of the requested aliases exist for (final String alias : aliases) { assertAliasExistence(alias, true); } assertBusy(() -> { - final GetAliasesResponse followerResponse = - followerClient().admin().indices().getAliases(new GetAliasesRequest().indices(followerIndex)).get(); + final GetAliasesResponse followerResponse = followerClient().admin() + .indices() + .getAliases(new GetAliasesRequest().indices(followerIndex)) + .get(); assertThat( - "expected follower to have [" + aliases.length + "] aliases, but was " + followerResponse.getAliases().toString(), - followerResponse.getAliases().get(followerIndex), - hasSize(aliases.length)); + "expected follower to have [" + aliases.length + "] aliases, but was " + followerResponse.getAliases().toString(), + followerResponse.getAliases().get(followerIndex), + hasSize(aliases.length) + ); for (final String alias : aliases) { final AliasMetadata followerAliasMetadata = getAliasMetadata(followerResponse, followerIndex, alias); - final GetAliasesResponse leaderResponse = - leaderClient().admin().indices().getAliases(new GetAliasesRequest().indices(leaderIndex).aliases(alias)).get(); + final GetAliasesResponse leaderResponse = leaderClient().admin() + .indices() + .getAliases(new GetAliasesRequest().indices(leaderIndex).aliases(alias)) + .get(); final AliasMetadata leaderAliasMetadata = getAliasMetadata(leaderResponse, leaderIndex, alias); assertThat( - "alias [" + alias + "] index routing did not replicate, but was " + followerAliasMetadata.toString(), - followerAliasMetadata.indexRouting(), equalTo(leaderAliasMetadata.indexRouting())); + "alias [" + alias + "] index routing did not replicate, but was " + followerAliasMetadata.toString(), + followerAliasMetadata.indexRouting(), + equalTo(leaderAliasMetadata.indexRouting()) + ); assertThat( - "alias [" + alias + "] search routing did not replicate, but was " + followerAliasMetadata.toString(), - followerAliasMetadata.searchRoutingValues(), equalTo(leaderAliasMetadata.searchRoutingValues())); + "alias [" + alias + "] search routing did not replicate, but was " + followerAliasMetadata.toString(), + followerAliasMetadata.searchRoutingValues(), + equalTo(leaderAliasMetadata.searchRoutingValues()) + ); assertThat( - "alias [" + alias + "] filtering did not replicate, but was " + followerAliasMetadata.toString(), - followerAliasMetadata.filter(), equalTo(leaderAliasMetadata.filter())); + "alias [" + alias + "] filtering did not replicate, but was " + followerAliasMetadata.toString(), + followerAliasMetadata.filter(), + equalTo(leaderAliasMetadata.filter()) + ); assertThat( - "alias [" + alias + "] should not be a write index, but was " + followerAliasMetadata.toString(), - followerAliasMetadata.writeIndex(), - equalTo(false)); + "alias [" + alias + "] should not be a write index, but was " + followerAliasMetadata.toString(), + followerAliasMetadata.writeIndex(), + equalTo(false) + ); aliasMetadataAssertion.accept(alias, followerAliasMetadata); } }); @@ -361,9 +373,9 @@ private void assertAliasExistence(final String alias, final boolean exists) thro assertBusy(() -> { // we must check serially because aliases exist will return true if any but not necessarily all of the requested aliases exist final GetAliasesResponse response = followerClient().admin() - .indices() - .getAliases(new GetAliasesRequest().indices("follower").aliases(alias)) - .get(); + .indices() + .getAliases(new GetAliasesRequest().indices("follower").aliases(alias)) + .get(); if (exists) { assertFalse("alias [" + alias + "] did not exist", response.getAliases().isEmpty()); } else { @@ -373,8 +385,11 @@ private void assertAliasExistence(final String alias, final boolean exists) thro } private AliasMetadata getAliasMetadata(final GetAliasesResponse response, final String index, final String alias) { - final Optional maybeAliasMetadata = - response.getAliases().get(index).stream().filter(a -> a.getAlias().equals(alias)).findFirst(); + final Optional maybeAliasMetadata = response.getAliases() + .get(index) + .stream() + .filter(a -> a.getAlias().equals(alias)) + .findFirst(); assertTrue("alias [" + alias + "] did not exist", maybeAliasMetadata.isPresent()); return maybeAliasMetadata.get(); } @@ -394,8 +409,10 @@ private CheckedRunnable assertShardFollowTask(final int numberOfPrima final List taskInfos = listTasksResponse.getTasks(); assertThat("expected a task for each shard", taskInfos.size(), equalTo(numberOfPrimaryShards)); - final Collection> shardFollowTasks = - taskMetadata.findTasks(ShardFollowTask.NAME, Objects::nonNull); + final Collection> shardFollowTasks = taskMetadata.findTasks( + ShardFollowTask.NAME, + Objects::nonNull + ); for (final PersistentTasksCustomMetadata.PersistentTask shardFollowTask : shardFollowTasks) { TaskInfo taskInfo = null; final String expectedId = "id=" + shardFollowTask.getId(); diff --git a/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/CcrDisabledIT.java b/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/CcrDisabledIT.java index fc0d96fe820a2..3608d159d3e3c 100644 --- a/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/CcrDisabledIT.java +++ b/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/CcrDisabledIT.java @@ -24,8 +24,11 @@ public void testClusterCanStartWithCcrInstalledButNotEnabled() throws Exception @Override protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { - return Settings.builder().put(super.nodeSettings(nodeOrdinal, otherSettings)).put(XPackSettings.CCR_ENABLED_SETTING.getKey(), true) - .put(XPackSettings.SECURITY_ENABLED.getKey(), false).build(); + return Settings.builder() + .put(super.nodeSettings(nodeOrdinal, otherSettings)) + .put(XPackSettings.CCR_ENABLED_SETTING.getKey(), true) + .put(XPackSettings.SECURITY_ENABLED.getKey(), false) + .build(); } @Override diff --git a/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/CcrLicenseIT.java b/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/CcrLicenseIT.java index 6c180ded50091..5a72ab887a726 100644 --- a/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/CcrLicenseIT.java +++ b/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/CcrLicenseIT.java @@ -52,65 +52,60 @@ protected Settings nodeSettings() { public void testThatFollowingIndexIsUnavailableWithNonCompliantLicense() throws InterruptedException { final ResumeFollowAction.Request followRequest = getResumeFollowRequest("follower"); final CountDownLatch latch = new CountDownLatch(1); - client().execute( - ResumeFollowAction.INSTANCE, - followRequest, - new ActionListener() { - @Override - public void onResponse(final AcknowledgedResponse response) { - latch.countDown(); - fail(); - } - - @Override - public void onFailure(final Exception e) { - assertNonCompliantLicense(e); - latch.countDown(); - } - }); + client().execute(ResumeFollowAction.INSTANCE, followRequest, new ActionListener() { + @Override + public void onResponse(final AcknowledgedResponse response) { + latch.countDown(); + fail(); + } + + @Override + public void onFailure(final Exception e) { + assertNonCompliantLicense(e); + latch.countDown(); + } + }); latch.await(); } public void testThatCreateAndFollowingIndexIsUnavailableWithNonCompliantLicense() throws InterruptedException { final PutFollowAction.Request createAndFollowRequest = getPutFollowRequest("leader", "follower"); final CountDownLatch latch = new CountDownLatch(1); - client().execute( - PutFollowAction.INSTANCE, - createAndFollowRequest, - new ActionListener() { - @Override - public void onResponse(final PutFollowAction.Response response) { - latch.countDown(); - fail(); - } - - @Override - public void onFailure(final Exception e) { - assertNonCompliantLicense(e); - latch.countDown(); - } - }); + client().execute(PutFollowAction.INSTANCE, createAndFollowRequest, new ActionListener() { + @Override + public void onResponse(final PutFollowAction.Response response) { + latch.countDown(); + fail(); + } + + @Override + public void onFailure(final Exception e) { + assertNonCompliantLicense(e); + latch.countDown(); + } + }); latch.await(); } public void testThatFollowStatsAreUnavailableWithNonCompliantLicense() throws InterruptedException { final CountDownLatch latch = new CountDownLatch(1); client().execute( - FollowStatsAction.INSTANCE, - new FollowStatsAction.StatsRequest(), - new ActionListener() { - @Override - public void onResponse(final FollowStatsAction.StatsResponses statsResponses) { - latch.countDown(); - fail(); - } - - @Override - public void onFailure(final Exception e) { - assertNonCompliantLicense(e); - latch.countDown(); - } - }); + FollowStatsAction.INSTANCE, + new FollowStatsAction.StatsRequest(), + new ActionListener() { + @Override + public void onResponse(final FollowStatsAction.StatsResponses statsResponses) { + latch.countDown(); + fail(); + } + + @Override + public void onFailure(final Exception e) { + assertNonCompliantLicense(e); + latch.countDown(); + } + } + ); latch.await(); } @@ -121,22 +116,19 @@ public void testThatPutAutoFollowPatternsIsUnavailableWithNonCompliantLicense() request.setName("name"); request.setRemoteCluster("leader"); request.setLeaderIndexPatterns(Collections.singletonList("*")); - client().execute( - PutAutoFollowPatternAction.INSTANCE, - request, - new ActionListener() { - @Override - public void onResponse(final AcknowledgedResponse response) { - latch.countDown(); - fail(); - } - - @Override - public void onFailure(final Exception e) { - assertNonCompliantLicense(e); - latch.countDown(); - } - }); + client().execute(PutAutoFollowPatternAction.INSTANCE, request, new ActionListener() { + @Override + public void onResponse(final AcknowledgedResponse response) { + latch.countDown(); + fail(); + } + + @Override + public void onFailure(final Exception e) { + assertNonCompliantLicense(e); + latch.countDown(); + } + }); latch.await(); } @@ -151,7 +143,9 @@ public void testAutoFollowCoordinatorLogsSkippingAutoFollowCoordinationWithNonCo Level.WARN, "skipping auto-follower coordination", ElasticsearchSecurityException.class, - "current license is non-compliant for [ccr]")); + "current license is non-compliant for [ccr]" + ) + ); try { // Need to add mock log appender before submitting CS update, otherwise we miss the expected log: @@ -181,16 +175,18 @@ public ClusterState execute(ClusterState currentState) throws Exception { null, null, null, - null); + null + ); AutoFollowMetadata autoFollowMetadata = new AutoFollowMetadata( Collections.singletonMap("test_alias", autoFollowPattern), Collections.emptyMap(), - Collections.emptyMap()); + Collections.emptyMap() + ); ClusterState.Builder newState = ClusterState.builder(currentState); - newState.metadata(Metadata.builder(currentState.getMetadata()) - .putCustom(AutoFollowMetadata.TYPE, autoFollowMetadata) - .build()); + newState.metadata( + Metadata.builder(currentState.getMetadata()).putCustom(AutoFollowMetadata.TYPE, autoFollowMetadata).build() + ); return newState.build(); } @@ -213,7 +209,6 @@ public void onFailure(String source, Exception e) { } } - private void assertNonCompliantLicense(final Exception e) { assertThat(e, instanceOf(ElasticsearchSecurityException.class)); assertThat(e.getMessage(), equalTo("current license is non-compliant for [ccr]")); diff --git a/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/CcrRepositoryIT.java b/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/CcrRepositoryIT.java index fa37f6a36d239..f8093b705c8f9 100644 --- a/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/CcrRepositoryIT.java +++ b/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/CcrRepositoryIT.java @@ -35,9 +35,8 @@ import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeValue; -import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.common.xcontent.support.XContentMapValues; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.Index; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.index.snapshots.IndexShardSnapshotStatus; @@ -54,6 +53,7 @@ import org.elasticsearch.test.transport.MockTransportService; import org.elasticsearch.transport.TransportActionProxy; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.CcrIntegTestCase; import org.elasticsearch.xpack.ccr.action.repositories.GetCcrRestoreFileChunkAction; import org.elasticsearch.xpack.ccr.action.repositories.PutCcrRestoreSessionAction; @@ -94,8 +94,9 @@ public class CcrRepositoryIT extends CcrIntegTestCase { public void testThatRepositoryIsPutAndRemovedWhenRemoteClusterIsUpdated() throws Exception { String leaderClusterRepoName = CcrRepository.NAME_PREFIX + "leader_cluster"; - final RepositoriesService repositoriesService = - getFollowerCluster().getDataOrMasterNodeInstances(RepositoriesService.class).iterator().next(); + final RepositoriesService repositoriesService = getFollowerCluster().getDataOrMasterNodeInstances(RepositoriesService.class) + .iterator() + .next(); try { Repository repository = repositoriesService.repository(leaderClusterRepoName); assertEquals(CcrRepository.TYPE, repository.getMetadata().type()); @@ -152,9 +153,11 @@ public void testThatRepositoryRecoversEmptyIndexBasedOnLeaderSettings() throws I Settings.Builder settingsBuilder = Settings.builder() .put(IndexMetadata.SETTING_INDEX_PROVIDED_NAME, followerIndex) .put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true); - RestoreSnapshotRequest restoreRequest = new RestoreSnapshotRequest(leaderClusterRepoName, CcrRepository.LATEST) - .indices(leaderIndex).indicesOptions(indicesOptions).renamePattern("^(.*)$") - .renameReplacement(followerIndex).masterNodeTimeout(TimeValue.MAX_VALUE) + RestoreSnapshotRequest restoreRequest = new RestoreSnapshotRequest(leaderClusterRepoName, CcrRepository.LATEST).indices(leaderIndex) + .indicesOptions(indicesOptions) + .renamePattern("^(.*)$") + .renameReplacement(followerIndex) + .masterNodeTimeout(TimeValue.MAX_VALUE) .indexSettings(settingsBuilder); PlainActionFuture future = PlainActionFuture.newFuture(); @@ -164,16 +167,14 @@ public void testThatRepositoryRecoversEmptyIndexBasedOnLeaderSettings() throws I assertEquals(restoreInfo.totalShards(), restoreInfo.successfulShards()); assertEquals(0, restoreInfo.failedShards()); - ClusterStateResponse leaderState = leaderClient() - .admin() + ClusterStateResponse leaderState = leaderClient().admin() .cluster() .prepareState() .clear() .setMetadata(true) .setIndices(leaderIndex) .get(); - ClusterStateResponse followerState = followerClient() - .admin() + ClusterStateResponse followerState = followerClient().admin() .cluster() .prepareState() .clear() @@ -224,9 +225,11 @@ public void testDocsAreRecovered() throws Exception { Settings.Builder settingsBuilder = Settings.builder() .put(IndexMetadata.SETTING_INDEX_PROVIDED_NAME, followerIndex) .put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true); - RestoreSnapshotRequest restoreRequest = new RestoreSnapshotRequest(leaderClusterRepoName, CcrRepository.LATEST) - .indices(leaderIndex).indicesOptions(indicesOptions).renamePattern("^(.*)$") - .renameReplacement(followerIndex).masterNodeTimeout(new TimeValue(1L, TimeUnit.HOURS)) + RestoreSnapshotRequest restoreRequest = new RestoreSnapshotRequest(leaderClusterRepoName, CcrRepository.LATEST).indices(leaderIndex) + .indicesOptions(indicesOptions) + .renamePattern("^(.*)$") + .renameReplacement(followerIndex) + .masterNodeTimeout(new TimeValue(1L, TimeUnit.HOURS)) .indexSettings(settingsBuilder); PlainActionFuture future = PlainActionFuture.newFuture(); @@ -290,9 +293,11 @@ public void testRateLimitingIsEmployed() throws Exception { Settings.Builder settingsBuilder = Settings.builder() .put(IndexMetadata.SETTING_INDEX_PROVIDED_NAME, followerIndex) .put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true); - RestoreSnapshotRequest restoreRequest = new RestoreSnapshotRequest(leaderClusterRepoName, CcrRepository.LATEST) - .indices(leaderIndex).indicesOptions(indicesOptions).renamePattern("^(.*)$") - .renameReplacement(followerIndex).masterNodeTimeout(TimeValue.MAX_VALUE) + RestoreSnapshotRequest restoreRequest = new RestoreSnapshotRequest(leaderClusterRepoName, CcrRepository.LATEST).indices(leaderIndex) + .indicesOptions(indicesOptions) + .renamePattern("^(.*)$") + .renameReplacement(followerIndex) + .masterNodeTimeout(TimeValue.MAX_VALUE) .indexSettings(settingsBuilder); PlainActionFuture future = PlainActionFuture.newFuture(); @@ -336,8 +341,8 @@ public void testIndividualActionsTimeout() throws Exception { MockTransportService mockTransportService = (MockTransportService) transportService; transportServices.add(mockTransportService); mockTransportService.addSendBehavior((connection, requestId, action, request, options) -> { - if (action.equals(GetCcrRestoreFileChunkAction.NAME) == false && - action.equals(TransportActionProxy.getProxyAction(GetCcrRestoreFileChunkAction.NAME)) == false) { + if (action.equals(GetCcrRestoreFileChunkAction.NAME) == false + && action.equals(TransportActionProxy.getProxyAction(GetCcrRestoreFileChunkAction.NAME)) == false) { connection.sendRequest(requestId, action, request, options); } }); @@ -354,9 +359,11 @@ public void testIndividualActionsTimeout() throws Exception { Settings.Builder settingsBuilder = Settings.builder() .put(IndexMetadata.SETTING_INDEX_PROVIDED_NAME, followerIndex) .put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true); - RestoreSnapshotRequest restoreRequest = new RestoreSnapshotRequest(leaderClusterRepoName, CcrRepository.LATEST) - .indices(leaderIndex).indicesOptions(indicesOptions).renamePattern("^(.*)$") - .renameReplacement(followerIndex).masterNodeTimeout(new TimeValue(1L, TimeUnit.HOURS)) + RestoreSnapshotRequest restoreRequest = new RestoreSnapshotRequest(leaderClusterRepoName, CcrRepository.LATEST).indices(leaderIndex) + .indicesOptions(indicesOptions) + .renamePattern("^(.*)$") + .renameReplacement(followerIndex) + .masterNodeTimeout(new TimeValue(1L, TimeUnit.HOURS)) .indexSettings(settingsBuilder); try { @@ -383,8 +390,9 @@ public void testIndividualActionsTimeout() throws Exception { settingsRequest = new ClusterUpdateSettingsRequest(); TimeValue defaultValue = CcrSettings.INDICES_RECOVERY_ACTION_TIMEOUT_SETTING.getDefault(Settings.EMPTY); - settingsRequest.persistentSettings(Settings.builder().put(CcrSettings.INDICES_RECOVERY_ACTION_TIMEOUT_SETTING.getKey(), - defaultValue)); + settingsRequest.persistentSettings( + Settings.builder().put(CcrSettings.INDICES_RECOVERY_ACTION_TIMEOUT_SETTING.getKey(), defaultValue) + ); assertAcked(followerClient().admin().cluster().updateSettings(settingsRequest).actionGet()); // This test sets individual action timeouts low to attempt to replicated timeouts. Although the // clear session action is not blocked, it is possible that it will still occasionally timeout. @@ -414,19 +422,19 @@ public void testFollowerMappingIsUpdated() throws IOException { Settings.Builder settingsBuilder = Settings.builder() .put(IndexMetadata.SETTING_INDEX_PROVIDED_NAME, followerIndex) .put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true); - RestoreSnapshotRequest restoreRequest = new RestoreSnapshotRequest(leaderClusterRepoName, CcrRepository.LATEST) - .indices(leaderIndex).indicesOptions(indicesOptions).renamePattern("^(.*)$") - .renameReplacement(followerIndex).masterNodeTimeout(new TimeValue(1L, TimeUnit.HOURS)) + RestoreSnapshotRequest restoreRequest = new RestoreSnapshotRequest(leaderClusterRepoName, CcrRepository.LATEST).indices(leaderIndex) + .indicesOptions(indicesOptions) + .renamePattern("^(.*)$") + .renameReplacement(followerIndex) + .masterNodeTimeout(new TimeValue(1L, TimeUnit.HOURS)) .indexSettings(settingsBuilder); - List transportServices = new ArrayList<>(); CountDownLatch latch = new CountDownLatch(1); AtomicBoolean updateSent = new AtomicBoolean(false); Runnable updateMappings = () -> { if (updateSent.compareAndSet(false, true)) { - leaderClient() - .admin() + leaderClient().admin() .indices() .preparePutMapping(leaderIndex) .setSource("{\"properties\":{\"k\":{\"type\":\"long\"}}}", XContentType.JSON) @@ -464,7 +472,11 @@ public void testFollowerMappingIsUpdated() throws IOException { clusterStateRequest.clear(); clusterStateRequest.metadata(true); clusterStateRequest.indices(followerIndex); - MappingMetadata mappingMetadata = followerClient().admin().indices().prepareGetMappings("index2").get().getMappings() + MappingMetadata mappingMetadata = followerClient().admin() + .indices() + .prepareGetMappings("index2") + .get() + .getMappings() .get("index2"); assertThat(XContentMapValues.extractValue("properties.k.type", mappingMetadata.sourceAsMap()), equalTo("long")); } finally { @@ -477,9 +489,19 @@ public void testFollowerMappingIsUpdated() throws IOException { public void testCcrRepositoryFetchesSnapshotShardSizeFromIndexShardStoreStats() throws Exception { final String leaderIndex = "leader"; final int numberOfShards = randomIntBetween(1, 5); - assertAcked(leaderClient().admin().indices().prepareCreate(leaderIndex) - .setSource(getIndexSettings(numberOfShards, 0, - Map.of(Store.INDEX_STORE_STATS_REFRESH_INTERVAL_SETTING.getKey(), TimeValue.ZERO.getStringRep())), XContentType.JSON)); + assertAcked( + leaderClient().admin() + .indices() + .prepareCreate(leaderIndex) + .setSource( + getIndexSettings( + numberOfShards, + 0, + Map.of(Store.INDEX_STORE_STATS_REFRESH_INTERVAL_SETTING.getKey(), TimeValue.ZERO.getStringRep()) + ), + XContentType.JSON + ) + ); final int numDocs = scaledRandomIntBetween(0, 500); if (numDocs > 0) { @@ -491,13 +513,12 @@ public void testCcrRepositoryFetchesSnapshotShardSizeFromIndexShardStoreStats() } ensureLeaderGreen(leaderIndex); - assertAllSuccessful(leaderClient().admin().indices().prepareForceMerge(leaderIndex) - .setMaxNumSegments(1) - .setFlush(true) - .get()); + assertAllSuccessful(leaderClient().admin().indices().prepareForceMerge(leaderIndex).setMaxNumSegments(1).setFlush(true).get()); refresh(leaderClient(), leaderIndex); - final IndexStats indexStats = leaderClient().admin().indices().prepareStats(leaderIndex) + final IndexStats indexStats = leaderClient().admin() + .indices() + .prepareStats(leaderIndex) .clear() .setStore(true) .get() @@ -515,12 +536,15 @@ public void testCcrRepositoryFetchesSnapshotShardSizeFromIndexShardStoreStats() IndexShardSnapshotStatus.Copy indexShardSnapshotStatus = repository.getShardSnapshotStatus( new SnapshotId(CcrRepository.LATEST, CcrRepository.LATEST), new IndexId(indexStats.getIndex(), indexStats.getUuid()), - new ShardId(new Index(indexStats.getIndex(), indexStats.getUuid()), shardId)).asCopy(); + new ShardId(new Index(indexStats.getIndex(), indexStats.getUuid()), shardId) + ).asCopy(); assertThat(indexShardSnapshotStatus, notNullValue()); assertThat(indexShardSnapshotStatus.getStage(), is(IndexShardSnapshotStatus.Stage.DONE)); - assertThat(indexShardSnapshotStatus.getTotalSize(), - equalTo(indexStats.getIndexShards().get(shardId).getPrimary().getStore().getSizeInBytes())); + assertThat( + indexShardSnapshotStatus.getTotalSize(), + equalTo(indexStats.getIndexShards().get(shardId).getPrimary().getStore().getSizeInBytes()) + ); } final String followerIndex = "follower"; @@ -533,9 +557,7 @@ public void testCcrRepositoryFetchesSnapshotShardSizeFromIndexShardStoreStats() final PlainActionFuture waitForRestoreInProgress = PlainActionFuture.newFuture(); final ClusterStateListener listener = event -> { RestoreInProgress restoreInProgress = event.state().custom(RestoreInProgress.TYPE, RestoreInProgress.EMPTY); - if (restoreInProgress != null - && restoreInProgress.isEmpty() == false - && event.state().routingTable().hasIndex(followerIndex)) { + if (restoreInProgress != null && restoreInProgress.isEmpty() == false && event.state().routingTable().hasIndex(followerIndex)) { final IndexRoutingTable indexRoutingTable = event.state().routingTable().index(followerIndex); for (ShardRouting shardRouting : indexRoutingTable.shardsWithState(ShardRoutingState.UNASSIGNED)) { if (shardRouting.unassignedInfo().getLastAllocationStatus() == AllocationStatus.FETCHING_SHARD_DATA) { @@ -558,13 +580,16 @@ public void testCcrRepositoryFetchesSnapshotShardSizeFromIndexShardStoreStats() }; clusterService.addListener(listener); - final RestoreSnapshotRequest restoreRequest = new RestoreSnapshotRequest(leaderCluster, CcrRepository.LATEST) - .indices(leaderIndex).indicesOptions(indicesOptions).renamePattern("^(.*)$") + final RestoreSnapshotRequest restoreRequest = new RestoreSnapshotRequest(leaderCluster, CcrRepository.LATEST).indices(leaderIndex) + .indicesOptions(indicesOptions) + .renamePattern("^(.*)$") .renameReplacement(followerIndex) .masterNodeTimeout(TimeValue.MAX_VALUE) - .indexSettings(Settings.builder() - .put(IndexMetadata.SETTING_INDEX_PROVIDED_NAME, followerIndex) - .put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true)); + .indexSettings( + Settings.builder() + .put(IndexMetadata.SETTING_INDEX_PROVIDED_NAME, followerIndex) + .put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true) + ); restoreService.restoreSnapshot(restoreRequest, PlainActionFuture.newFuture()); waitForRestoreInProgress.get(30L, TimeUnit.SECONDS); @@ -572,9 +597,11 @@ public void testCcrRepositoryFetchesSnapshotShardSizeFromIndexShardStoreStats() ensureFollowerGreen(followerIndex); for (int shardId = 0; shardId < numberOfShards; shardId++) { - assertThat("Snapshot shard size fetched for follower shard [" + shardId + "] does not match leader store size", + assertThat( + "Snapshot shard size fetched for follower shard [" + shardId + "] does not match leader store size", fetchedSnapshotShardSizes.get(shardId), - equalTo(indexStats.getIndexShards().get(shardId).getPrimary().getStore().getSizeInBytes())); + equalTo(indexStats.getIndexShards().get(shardId).getPrimary().getStore().getSizeInBytes()) + ); } assertHitCount(followerClient().prepareSearch(followerIndex).setSize(0).get(), numDocs); @@ -591,17 +618,16 @@ public void testCcrRepositoryFailsToFetchSnapshotShardSizes() throws Exception { final AtomicInteger simulatedFailures = new AtomicInteger(); final List transportServices = new ArrayList<>(); - for(TransportService transportService : getLeaderCluster().getDataOrMasterNodeInstances(TransportService.class)) { + for (TransportService transportService : getLeaderCluster().getDataOrMasterNodeInstances(TransportService.class)) { final MockTransportService mockTransportService = (MockTransportService) transportService; transportServices.add(mockTransportService); mockTransportService.addRequestHandlingBehavior(IndicesStatsAction.NAME, (handler, request, channel, task) -> { if (request instanceof IndicesStatsRequest) { IndicesStatsRequest indicesStatsRequest = (IndicesStatsRequest) request; - if (Arrays.equals(indicesStatsRequest.indices(), new String[]{leaderIndex}) + if (Arrays.equals(indicesStatsRequest.indices(), new String[] { leaderIndex }) && indicesStatsRequest.store() && indicesStatsRequest.search() == false - && indicesStatsRequest.fieldData() == false - ) { + && indicesStatsRequest.fieldData() == false) { simulatedFailures.incrementAndGet(); channel.sendResponse(new ElasticsearchException("simulated")); return; @@ -626,7 +652,8 @@ public void testCcrRepositoryFailsToFetchSnapshotShardSizes() throws Exception { // this assertBusy completes because the listener is added after the InternalSnapshotsInfoService // and ClusterService preserves the order of listeners. assertBusy(() -> { - List sizes = indexRoutingTable.shardsWithState(ShardRoutingState.UNASSIGNED).stream() + List sizes = indexRoutingTable.shardsWithState(ShardRoutingState.UNASSIGNED) + .stream() .filter(shard -> shard.unassignedInfo().getLastAllocationStatus() == AllocationStatus.FETCHING_SHARD_DATA) .sorted(Comparator.comparingInt(ShardRouting::getId)) .map(shard -> snapshotsInfoService.snapshotShardSizes().getShardSize(shard)) diff --git a/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/CcrRetentionLeaseIT.java b/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/CcrRetentionLeaseIT.java index 8109896e1f544..a3cd5750f0d39 100644 --- a/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/CcrRetentionLeaseIT.java +++ b/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/CcrRetentionLeaseIT.java @@ -30,7 +30,6 @@ import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexService; import org.elasticsearch.index.seqno.RetentionLease; @@ -50,6 +49,7 @@ import org.elasticsearch.transport.TransportActionProxy; import org.elasticsearch.transport.TransportMessageListener; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.CcrIntegTestCase; import org.elasticsearch.xpack.ccr.action.repositories.ClearCcrRestoreSessionAction; import org.elasticsearch.xpack.ccr.repository.CcrRepository; @@ -98,28 +98,27 @@ public List> getSettings() { @Override protected Collection> nodePlugins() { - return Stream.concat( - super.nodePlugins().stream(), - Stream.of(RetentionLeaseRenewIntervalSettingPlugin.class)) - .collect(Collectors.toList()); + return Stream.concat(super.nodePlugins().stream(), Stream.of(RetentionLeaseRenewIntervalSettingPlugin.class)) + .collect(Collectors.toList()); } @Override protected Settings followerClusterSettings() { return Settings.builder() - .put(super.followerClusterSettings()) - .put(CcrRetentionLeases.RETENTION_LEASE_RENEW_INTERVAL_SETTING.getKey(), TimeValue.timeValueMillis(200)) - .build(); + .put(super.followerClusterSettings()) + .put(CcrRetentionLeases.RETENTION_LEASE_RENEW_INTERVAL_SETTING.getKey(), TimeValue.timeValueMillis(200)) + .build(); } private final IndicesOptions indicesOptions = IndicesOptions.strictSingleIndexNoExpandForbidClosed(); private RestoreSnapshotRequest setUpRestoreSnapshotRequest( - final String leaderIndex, - final int numberOfShards, - final int numberOfReplicas, - final String followerIndex, - final int numberOfDocuments) throws IOException { + final String leaderIndex, + final int numberOfShards, + final int numberOfReplicas, + final String followerIndex, + final int numberOfDocuments + ) throws IOException { final ClusterUpdateSettingsRequest settingsRequest = new ClusterUpdateSettingsRequest().masterNodeTimeout(TimeValue.MAX_VALUE); final String chunkSize = new ByteSizeValue(randomFrom(4, 128, 1024), ByteSizeUnit.KB).getStringRep(); settingsRequest.persistentSettings(Settings.builder().put(CcrSettings.RECOVERY_CHUNK_SIZE.getKey(), chunkSize)); @@ -130,8 +129,13 @@ private RestoreSnapshotRequest setUpRestoreSnapshotRequest( final Map additionalSettings = new HashMap<>(); additionalSettings.put(IndexService.RETENTION_LEASE_SYNC_INTERVAL_SETTING.getKey(), TimeValue.timeValueMillis(200).getStringRep()); final String leaderIndexSettings = getIndexSettings(numberOfShards, numberOfReplicas, additionalSettings); - assertAcked(leaderClient().admin().indices().prepareCreate(leaderIndex) - .setMasterNodeTimeout(TimeValue.MAX_VALUE).setSource(leaderIndexSettings, XContentType.JSON)); + assertAcked( + leaderClient().admin() + .indices() + .prepareCreate(leaderIndex) + .setMasterNodeTimeout(TimeValue.MAX_VALUE) + .setSource(leaderIndexSettings, XContentType.JSON) + ); ensureLeaderGreen(leaderIndex); logger.info("indexing [{}] docs", numberOfDocuments); @@ -146,15 +150,14 @@ private RestoreSnapshotRequest setUpRestoreSnapshotRequest( leaderClient().admin().indices().prepareFlush(leaderIndex).setForce(true).setWaitIfOngoing(true).get(); final Settings.Builder settingsBuilder = Settings.builder() - .put(IndexMetadata.SETTING_INDEX_PROVIDED_NAME, followerIndex) - .put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true); - return new RestoreSnapshotRequest(leaderClusterRepoName, CcrRepository.LATEST) - .indexSettings(settingsBuilder) - .indices(leaderIndex) - .indicesOptions(indicesOptions) - .renamePattern("^(.*)$") - .renameReplacement(followerIndex) - .masterNodeTimeout(TimeValue.MAX_VALUE); + .put(IndexMetadata.SETTING_INDEX_PROVIDED_NAME, followerIndex) + .put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true); + return new RestoreSnapshotRequest(leaderClusterRepoName, CcrRepository.LATEST).indexSettings(settingsBuilder) + .indices(leaderIndex) + .indicesOptions(indicesOptions) + .renamePattern("^(.*)$") + .renameReplacement(followerIndex) + .masterNodeTimeout(TimeValue.MAX_VALUE); } public void testRetentionLeaseIsTakenAtTheStartOfRecovery() throws Exception { @@ -163,8 +166,13 @@ public void testRetentionLeaseIsTakenAtTheStartOfRecovery() throws Exception { final int numberOfReplicas = between(0, 1); final String followerIndex = "follower"; final int numberOfDocuments = scaledRandomIntBetween(1, 8192); - final RestoreSnapshotRequest restoreRequest = - setUpRestoreSnapshotRequest(leaderIndex, numberOfShards, numberOfReplicas, followerIndex, numberOfDocuments); + final RestoreSnapshotRequest restoreRequest = setUpRestoreSnapshotRequest( + leaderIndex, + numberOfShards, + numberOfReplicas, + followerIndex, + numberOfDocuments + ); final RestoreService restoreService = getFollowerCluster().getCurrentMasterNodeInstance(RestoreService.class); final ClusterService clusterService = getFollowerCluster().getCurrentMasterNodeInstance(ClusterService.class); @@ -173,15 +181,18 @@ public void testRetentionLeaseIsTakenAtTheStartOfRecovery() throws Exception { // ensure that a retention lease has been put in place on each shard assertBusy(() -> { - final IndicesStatsResponse stats = - leaderClient().admin().indices().stats(new IndicesStatsRequest().clear().indices(leaderIndex)).actionGet(); + final IndicesStatsResponse stats = leaderClient().admin() + .indices() + .stats(new IndicesStatsRequest().clear().indices(leaderIndex)) + .actionGet(); assertNotNull(stats.getShards()); assertThat(stats.getShards(), arrayWithSize(numberOfShards * (1 + numberOfReplicas))); final List shardsStats = getShardsStats(stats); for (int i = 0; i < numberOfShards * (1 + numberOfReplicas); i++) { assertNotNull(shardsStats.get(i).getRetentionLeaseStats()); final Map currentRetentionLeases = RetentionLeaseUtils.toMapExcludingPeerRecoveryRetentionLeases( - shardsStats.get(i).getRetentionLeaseStats().retentionLeases()); + shardsStats.get(i).getRetentionLeaseStats().retentionLeases() + ); assertThat(Strings.toString(shardsStats.get(i)), currentRetentionLeases.values(), hasSize(1)); final RetentionLease retentionLease = currentRetentionLeases.values().iterator().next(); assertThat(retentionLease.id(), equalTo(getRetentionLeaseId(followerIndex, leaderIndex))); @@ -204,8 +215,13 @@ public void testRetentionLeaseIsRenewedDuringRecovery() throws Exception { final int numberOfReplicas = between(0, 1); final String followerIndex = "follower"; final int numberOfDocuments = scaledRandomIntBetween(1, 8192); - final RestoreSnapshotRequest restoreRequest = - setUpRestoreSnapshotRequest(leaderIndex, numberOfShards, numberOfReplicas, followerIndex, numberOfDocuments); + final RestoreSnapshotRequest restoreRequest = setUpRestoreSnapshotRequest( + leaderIndex, + numberOfShards, + numberOfReplicas, + followerIndex, + numberOfDocuments + ); final RestoreService restoreService = getFollowerCluster().getCurrentMasterNodeInstance(RestoreService.class); final ClusterService clusterService = getFollowerCluster().getCurrentMasterNodeInstance(ClusterService.class); @@ -214,20 +230,21 @@ public void testRetentionLeaseIsRenewedDuringRecovery() throws Exception { // block the recovery from completing; this ensures the background sync is still running final ClusterStateResponse followerClusterState = followerClient().admin().cluster().prepareState().clear().setNodes(true).get(); for (final DiscoveryNode senderNode : followerClusterState.getState().nodes()) { - final MockTransportService senderTransportService = - (MockTransportService) getFollowerCluster().getInstance(TransportService.class, senderNode.getName()); - senderTransportService.addSendBehavior( - (connection, requestId, action, request, options) -> { - if (ClearCcrRestoreSessionAction.NAME.equals(action) - || TransportActionProxy.getProxyAction(ClearCcrRestoreSessionAction.NAME).equals(action)) { - try { - latch.await(); - } catch (final InterruptedException e) { - fail(e.toString()); - } - } - connection.sendRequest(requestId, action, request, options); - }); + final MockTransportService senderTransportService = (MockTransportService) getFollowerCluster().getInstance( + TransportService.class, + senderNode.getName() + ); + senderTransportService.addSendBehavior((connection, requestId, action, request, options) -> { + if (ClearCcrRestoreSessionAction.NAME.equals(action) + || TransportActionProxy.getProxyAction(ClearCcrRestoreSessionAction.NAME).equals(action)) { + try { + latch.await(); + } catch (final InterruptedException e) { + fail(e.toString()); + } + } + connection.sendRequest(requestId, action, request, options); + }); } final PlainActionFuture future = PlainActionFuture.newFuture(); @@ -238,8 +255,10 @@ public void testRetentionLeaseIsRenewedDuringRecovery() throws Exception { latch.countDown(); } finally { for (final DiscoveryNode senderNode : followerClusterState.getState().nodes()) { - final MockTransportService senderTransportService = - (MockTransportService) getFollowerCluster().getInstance(TransportService.class, senderNode.getName()); + final MockTransportService senderTransportService = (MockTransportService) getFollowerCluster().getInstance( + TransportService.class, + senderNode.getName() + ); senderTransportService.clearAllRules(); } } @@ -248,7 +267,7 @@ public void testRetentionLeaseIsRenewedDuringRecovery() throws Exception { assertEquals(restoreInfo.totalShards(), restoreInfo. - successfulShards()); + successfulShards()); assertEquals(0, restoreInfo.failedShards()); for (int i = 0; i < numberOfDocuments; i++) { @@ -263,8 +282,13 @@ public void testRetentionLeasesAreNotBeingRenewedAfterRecoveryCompletes() throws final int numberOfReplicas = between(0, 1); final String followerIndex = "follower"; final int numberOfDocuments = scaledRandomIntBetween(1, 8192); - final RestoreSnapshotRequest restoreRequest = - setUpRestoreSnapshotRequest(leaderIndex, numberOfShards, numberOfReplicas, followerIndex, numberOfDocuments); + final RestoreSnapshotRequest restoreRequest = setUpRestoreSnapshotRequest( + leaderIndex, + numberOfShards, + numberOfReplicas, + followerIndex, + numberOfDocuments + ); final RestoreService restoreService = getFollowerCluster().getCurrentMasterNodeInstance(RestoreService.class); final ClusterService clusterService = getFollowerCluster().getCurrentMasterNodeInstance(ClusterService.class); @@ -279,18 +303,18 @@ public void testRetentionLeasesAreNotBeingRenewedAfterRecoveryCompletes() throws * times that we sample the retention leases, which would cause our check to fail. */ final TimeValue syncIntervalSetting = IndexService.RETENTION_LEASE_SYNC_INTERVAL_SETTING.get( - leaderClient() - .admin() - .indices() - .prepareGetSettings(leaderIndex) - .get() - .getIndexToSettings() - .get(leaderIndex)); + leaderClient().admin().indices().prepareGetSettings(leaderIndex).get().getIndexToSettings().get(leaderIndex) + ); final long syncEnd = System.nanoTime(); Thread.sleep(Math.max(0, randomIntBetween(2, 4) * syncIntervalSetting.millis() - TimeUnit.NANOSECONDS.toMillis(syncEnd - start))); - final ClusterStateResponse leaderIndexClusterState = - leaderClient().admin().cluster().prepareState().clear().setMetadata(true).setIndices(leaderIndex).get(); + final ClusterStateResponse leaderIndexClusterState = leaderClient().admin() + .cluster() + .prepareState() + .clear() + .setMetadata(true) + .setIndices(leaderIndex) + .get(); final String leaderUUID = leaderIndexClusterState.getState().metadata().index(leaderIndex).getIndexUUID(); /* @@ -301,30 +325,36 @@ public void testRetentionLeasesAreNotBeingRenewedAfterRecoveryCompletes() throws */ assertBusy(() -> { // sample the leases after recovery - final List< Map> retentionLeases = new ArrayList<>(); + final List> retentionLeases = new ArrayList<>(); assertBusy(() -> { retentionLeases.clear(); - final IndicesStatsResponse stats = - leaderClient().admin().indices().stats(new IndicesStatsRequest().clear().indices(leaderIndex)).actionGet(); + final IndicesStatsResponse stats = leaderClient().admin() + .indices() + .stats(new IndicesStatsRequest().clear().indices(leaderIndex)) + .actionGet(); assertNotNull(stats.getShards()); assertThat(stats.getShards(), arrayWithSize(numberOfShards * (1 + numberOfReplicas))); final List shardsStats = getShardsStats(stats); for (int i = 0; i < numberOfShards * (1 + numberOfReplicas); i++) { assertNotNull(shardsStats.get(i).getRetentionLeaseStats()); - final Map currentRetentionLeases - = RetentionLeaseUtils.toMapExcludingPeerRecoveryRetentionLeases( - shardsStats.get(i).getRetentionLeaseStats().retentionLeases()); + final Map currentRetentionLeases = RetentionLeaseUtils + .toMapExcludingPeerRecoveryRetentionLeases(shardsStats.get(i).getRetentionLeaseStats().retentionLeases()); assertThat(Strings.toString(shardsStats.get(i)), currentRetentionLeases.values(), hasSize(1)); - final ClusterStateResponse followerIndexClusterState = - followerClient().admin().cluster().prepareState().clear().setMetadata(true).setIndices(followerIndex).get(); + final ClusterStateResponse followerIndexClusterState = followerClient().admin() + .cluster() + .prepareState() + .clear() + .setMetadata(true) + .setIndices(followerIndex) + .get(); final String followerUUID = followerIndexClusterState.getState().metadata().index(followerIndex).getIndexUUID(); - final RetentionLease retentionLease = - currentRetentionLeases.values().iterator().next(); + final RetentionLease retentionLease = currentRetentionLeases.values().iterator().next(); final String expectedRetentionLeaseId = retentionLeaseId( getFollowerCluster().getClusterName(), new Index(followerIndex, followerUUID), getLeaderCluster().getClusterName(), - new Index(leaderIndex, leaderUUID)); + new Index(leaderIndex, leaderUUID) + ); assertThat(retentionLease.id(), equalTo(expectedRetentionLeaseId)); retentionLeases.add(currentRetentionLeases); } @@ -333,11 +363,14 @@ public void testRetentionLeasesAreNotBeingRenewedAfterRecoveryCompletes() throws final TimeValue renewIntervalSetting = CcrRetentionLeases.RETENTION_LEASE_RENEW_INTERVAL_SETTING.get(followerClusterSettings()); final long renewEnd = System.nanoTime(); Thread.sleep( - Math.max(0, randomIntBetween(2, 4) * renewIntervalSetting.millis() - TimeUnit.NANOSECONDS.toMillis(renewEnd - start))); + Math.max(0, randomIntBetween(2, 4) * renewIntervalSetting.millis() - TimeUnit.NANOSECONDS.toMillis(renewEnd - start)) + ); // now ensure that the retention leases are the same - final IndicesStatsResponse stats = - leaderClient().admin().indices().stats(new IndicesStatsRequest().clear().indices(leaderIndex)).actionGet(); + final IndicesStatsResponse stats = leaderClient().admin() + .indices() + .stats(new IndicesStatsRequest().clear().indices(leaderIndex)) + .actionGet(); assertNotNull(stats.getShards()); assertThat(stats.getShards(), arrayWithSize(numberOfShards * (1 + numberOfReplicas))); final List shardsStats = getShardsStats(stats); @@ -347,13 +380,18 @@ public void testRetentionLeasesAreNotBeingRenewedAfterRecoveryCompletes() throws } assertNotNull(shardsStats.get(i).getRetentionLeaseStats()); final Map currentRetentionLeases = RetentionLeaseUtils.toMapExcludingPeerRecoveryRetentionLeases( - shardsStats.get(i).getRetentionLeaseStats().retentionLeases()); + shardsStats.get(i).getRetentionLeaseStats().retentionLeases() + ); assertThat(Strings.toString(shardsStats.get(i)), currentRetentionLeases.values(), hasSize(1)); - final ClusterStateResponse followerIndexClusterState = - followerClient().admin().cluster().prepareState().clear().setMetadata(true).setIndices(followerIndex).get(); + final ClusterStateResponse followerIndexClusterState = followerClient().admin() + .cluster() + .prepareState() + .clear() + .setMetadata(true) + .setIndices(followerIndex) + .get(); final String followerUUID = followerIndexClusterState.getState().metadata().index(followerIndex).getIndexUUID(); - final RetentionLease retentionLease = - currentRetentionLeases.values().iterator().next(); + final RetentionLease retentionLease = currentRetentionLeases.values().iterator().next(); assertThat(retentionLease.id(), equalTo(getRetentionLeaseId(followerIndex, followerUUID, leaderIndex, leaderUUID))); // we assert that retention leases are being renewed by an increase in the timestamp assertThat(retentionLease.timestamp(), equalTo(retentionLeases.get(i).values().iterator().next().timestamp())); @@ -381,79 +419,90 @@ public void testUnfollowRemovesRetentionLeases() throws Exception { final String retentionLeaseId = getRetentionLeaseId(followerIndex, leaderIndex); - final IndicesStatsResponse stats = - leaderClient().admin().indices().stats(new IndicesStatsRequest().clear().indices(leaderIndex)).actionGet(); + final IndicesStatsResponse stats = leaderClient().admin() + .indices() + .stats(new IndicesStatsRequest().clear().indices(leaderIndex)) + .actionGet(); final List shardsStats = getShardsStats(stats); for (final ShardStats shardStats : shardsStats) { final Map retentionLeases = RetentionLeaseUtils.toMapExcludingPeerRecoveryRetentionLeases( - shardStats.getRetentionLeaseStats().retentionLeases()); + shardStats.getRetentionLeaseStats().retentionLeases() + ); assertThat(Strings.toString(shardStats), retentionLeases.values(), hasSize(1)); assertThat(retentionLeases.values().iterator().next().id(), equalTo(retentionLeaseId)); } // we will sometimes fake that some of the retention leases are already removed on the leader shard - final Set shardIds = - new HashSet<>(randomSubsetOf( - randomIntBetween(0, numberOfShards), - IntStream.range(0, numberOfShards).boxed().collect(Collectors.toSet()))); + final Set shardIds = new HashSet<>( + randomSubsetOf(randomIntBetween(0, numberOfShards), IntStream.range(0, numberOfShards).boxed().collect(Collectors.toSet())) + ); final ClusterStateResponse followerClusterState = followerClient().admin().cluster().prepareState().clear().setNodes(true).get(); try { for (final DiscoveryNode senderNode : followerClusterState.getState().nodes()) { - final MockTransportService senderTransportService = - (MockTransportService) getFollowerCluster().getInstance(TransportService.class, senderNode.getName()); - senderTransportService.addSendBehavior( - (connection, requestId, action, request, options) -> { - if (RetentionLeaseActions.Remove.ACTION_NAME.equals(action) - || TransportActionProxy.getProxyAction(RetentionLeaseActions.Remove.ACTION_NAME).equals(action)) { - final RetentionLeaseActions.RemoveRequest removeRequest = (RetentionLeaseActions.RemoveRequest) request; - if (shardIds.contains(removeRequest.getShardId().id())) { - final String primaryShardNodeId = - getLeaderCluster() - .clusterService() - .state() - .routingTable() - .index(leaderIndex) - .shard(removeRequest.getShardId().id()) - .primaryShard() - .currentNodeId(); - final String primaryShardNodeName = - getLeaderCluster().clusterService().state().nodes().get(primaryShardNodeId).getName(); - final IndexShard primary = - getLeaderCluster() - .getInstance(IndicesService.class, primaryShardNodeName) - .getShardOrNull(removeRequest.getShardId()); - final CountDownLatch latch = new CountDownLatch(1); - primary.removeRetentionLease( - retentionLeaseId, - ActionListener.wrap(r -> latch.countDown(), e -> fail(e.toString()))); - try { - latch.await(); - } catch (final InterruptedException e) { - Thread.currentThread().interrupt(); - fail(e.toString()); - } - } + final MockTransportService senderTransportService = (MockTransportService) getFollowerCluster().getInstance( + TransportService.class, + senderNode.getName() + ); + senderTransportService.addSendBehavior((connection, requestId, action, request, options) -> { + if (RetentionLeaseActions.Remove.ACTION_NAME.equals(action) + || TransportActionProxy.getProxyAction(RetentionLeaseActions.Remove.ACTION_NAME).equals(action)) { + final RetentionLeaseActions.RemoveRequest removeRequest = (RetentionLeaseActions.RemoveRequest) request; + if (shardIds.contains(removeRequest.getShardId().id())) { + final String primaryShardNodeId = getLeaderCluster().clusterService() + .state() + .routingTable() + .index(leaderIndex) + .shard(removeRequest.getShardId().id()) + .primaryShard() + .currentNodeId(); + final String primaryShardNodeName = getLeaderCluster().clusterService() + .state() + .nodes() + .get(primaryShardNodeId) + .getName(); + final IndexShard primary = getLeaderCluster().getInstance(IndicesService.class, primaryShardNodeName) + .getShardOrNull(removeRequest.getShardId()); + final CountDownLatch latch = new CountDownLatch(1); + primary.removeRetentionLease( + retentionLeaseId, + ActionListener.wrap(r -> latch.countDown(), e -> fail(e.toString())) + ); + try { + latch.await(); + } catch (final InterruptedException e) { + Thread.currentThread().interrupt(); + fail(e.toString()); } - connection.sendRequest(requestId, action, request, options); - }); + } + } + connection.sendRequest(requestId, action, request, options); + }); } pauseFollow(followerIndex); assertAcked(followerClient().admin().indices().close(new CloseIndexRequest(followerIndex)).actionGet()); assertAcked(followerClient().execute(UnfollowAction.INSTANCE, new UnfollowAction.Request(followerIndex)).actionGet()); - final IndicesStatsResponse afterUnfollowStats = - leaderClient().admin().indices().stats(new IndicesStatsRequest().clear().indices(leaderIndex)).actionGet(); + final IndicesStatsResponse afterUnfollowStats = leaderClient().admin() + .indices() + .stats(new IndicesStatsRequest().clear().indices(leaderIndex)) + .actionGet(); final List afterUnfollowShardsStats = getShardsStats(afterUnfollowStats); for (final ShardStats shardStats : afterUnfollowShardsStats) { - assertThat(Strings.toString(shardStats), RetentionLeaseUtils.toMapExcludingPeerRecoveryRetentionLeases( - shardStats.getRetentionLeaseStats().retentionLeases()).values(), empty()); + assertThat( + Strings.toString(shardStats), + RetentionLeaseUtils.toMapExcludingPeerRecoveryRetentionLeases(shardStats.getRetentionLeaseStats().retentionLeases()) + .values(), + empty() + ); } } finally { for (final DiscoveryNode senderNode : followerClusterState.getState().nodes()) { - final MockTransportService senderTransportService = - (MockTransportService) getFollowerCluster().getInstance(TransportService.class, senderNode.getName()); + final MockTransportService senderTransportService = (MockTransportService) getFollowerCluster().getInstance( + TransportService.class, + senderNode.getName() + ); senderTransportService.clearAllRules(); } } @@ -475,54 +524,71 @@ public void testUnfollowFailsToRemoveRetentionLeases() throws Exception { followerClient().admin().indices().close(new CloseIndexRequest(followerIndex).masterNodeTimeout(TimeValue.MAX_VALUE)).actionGet(); // we will disrupt requests to remove retention leases for these random shards - final Set shardIds = - new HashSet<>(randomSubsetOf( - randomIntBetween(1, numberOfShards), - IntStream.range(0, numberOfShards).boxed().collect(Collectors.toSet()))); + final Set shardIds = new HashSet<>( + randomSubsetOf(randomIntBetween(1, numberOfShards), IntStream.range(0, numberOfShards).boxed().collect(Collectors.toSet())) + ); final ClusterStateResponse followerClusterState = followerClient().admin().cluster().prepareState().clear().setNodes(true).get(); try { for (final DiscoveryNode senderNode : followerClusterState.getState().nodes()) { - final MockTransportService senderTransportService = - (MockTransportService) getFollowerCluster().getInstance(TransportService.class, senderNode.getName()); - senderTransportService.addSendBehavior( - (connection, requestId, action, request, options) -> { - if (RetentionLeaseActions.Remove.ACTION_NAME.equals(action) - || TransportActionProxy.getProxyAction(RetentionLeaseActions.Remove.ACTION_NAME).equals(action)) { - final RetentionLeaseActions.RemoveRequest removeRequest = (RetentionLeaseActions.RemoveRequest) request; - if (shardIds.contains(removeRequest.getShardId().id())) { - throw randomBoolean() - ? new ConnectTransportException(connection.getNode(), "connection failed") - : new IndexShardClosedException(removeRequest.getShardId()); - } - } - connection.sendRequest(requestId, action, request, options); - }); + final MockTransportService senderTransportService = (MockTransportService) getFollowerCluster().getInstance( + TransportService.class, + senderNode.getName() + ); + senderTransportService.addSendBehavior((connection, requestId, action, request, options) -> { + if (RetentionLeaseActions.Remove.ACTION_NAME.equals(action) + || TransportActionProxy.getProxyAction(RetentionLeaseActions.Remove.ACTION_NAME).equals(action)) { + final RetentionLeaseActions.RemoveRequest removeRequest = (RetentionLeaseActions.RemoveRequest) request; + if (shardIds.contains(removeRequest.getShardId().id())) { + throw randomBoolean() + ? new ConnectTransportException(connection.getNode(), "connection failed") + : new IndexShardClosedException(removeRequest.getShardId()); + } + } + connection.sendRequest(requestId, action, request, options); + }); } final ElasticsearchException e = expectThrows( - ElasticsearchException.class, - () -> followerClient().execute(UnfollowAction.INSTANCE, new UnfollowAction.Request(followerIndex)).actionGet()); - - final ClusterStateResponse followerIndexClusterState = - followerClient().admin().cluster().prepareState().clear().setMetadata(true).setIndices(followerIndex).get(); + ElasticsearchException.class, + () -> followerClient().execute(UnfollowAction.INSTANCE, new UnfollowAction.Request(followerIndex)).actionGet() + ); + + final ClusterStateResponse followerIndexClusterState = followerClient().admin() + .cluster() + .prepareState() + .clear() + .setMetadata(true) + .setIndices(followerIndex) + .get(); final String followerUUID = followerIndexClusterState.getState().metadata().index(followerIndex).getIndexUUID(); - final ClusterStateResponse leaderIndexClusterState = - leaderClient().admin().cluster().prepareState().clear().setMetadata(true).setIndices(leaderIndex).get(); + final ClusterStateResponse leaderIndexClusterState = leaderClient().admin() + .cluster() + .prepareState() + .clear() + .setMetadata(true) + .setIndices(leaderIndex) + .get(); final String leaderUUID = leaderIndexClusterState.getState().metadata().index(leaderIndex).getIndexUUID(); assertThat( - e.getMetadata("es.failed_to_remove_retention_leases"), - contains(retentionLeaseId( - getFollowerCluster().getClusterName(), - new Index(followerIndex, followerUUID), - getLeaderCluster().getClusterName(), - new Index(leaderIndex, leaderUUID)))); + e.getMetadata("es.failed_to_remove_retention_leases"), + contains( + retentionLeaseId( + getFollowerCluster().getClusterName(), + new Index(followerIndex, followerUUID), + getLeaderCluster().getClusterName(), + new Index(leaderIndex, leaderUUID) + ) + ) + ); } finally { for (final DiscoveryNode senderNode : followerClusterState.getState().nodes()) { - final MockTransportService senderTransportService = - (MockTransportService) getFollowerCluster().getInstance(TransportService.class, senderNode.getName()); + final MockTransportService senderTransportService = (MockTransportService) getFollowerCluster().getInstance( + TransportService.class, + senderNode.getName() + ); senderTransportService.clearAllRules(); } } @@ -535,8 +601,9 @@ public void testRetentionLeaseRenewedWhileFollowing() throws Exception { final int numberOfReplicas = randomIntBetween(0, 1); final Map additionalIndexSettings = new HashMap<>(); additionalIndexSettings.put( - IndexService.RETENTION_LEASE_SYNC_INTERVAL_SETTING.getKey(), - TimeValue.timeValueMillis(200).getStringRep()); + IndexService.RETENTION_LEASE_SYNC_INTERVAL_SETTING.getKey(), + TimeValue.timeValueMillis(200).getStringRep() + ); final String leaderIndexSettings = getIndexSettings(numberOfShards, numberOfReplicas, additionalIndexSettings); assertAcked(leaderClient().admin().indices().prepareCreate(leaderIndex).setSource(leaderIndexSettings, XContentType.JSON).get()); ensureLeaderYellow(leaderIndex); @@ -554,8 +621,9 @@ public void testRetentionLeaseAdvancesWhileFollowing() throws Exception { final int numberOfReplicas = randomIntBetween(0, 1); final Map additionalIndexSettings = new HashMap<>(); additionalIndexSettings.put( - IndexService.RETENTION_LEASE_SYNC_INTERVAL_SETTING.getKey(), - TimeValue.timeValueMillis(200).getStringRep()); + IndexService.RETENTION_LEASE_SYNC_INTERVAL_SETTING.getKey(), + TimeValue.timeValueMillis(200).getStringRep() + ); final String leaderIndexSettings = getIndexSettings(numberOfShards, numberOfReplicas, additionalIndexSettings); assertAcked(leaderClient().admin().indices().prepareCreate(leaderIndex).setSource(leaderIndexSettings, XContentType.JSON).get()); ensureLeaderYellow(leaderIndex); @@ -589,23 +657,26 @@ public void testRetentionLeaseAdvancesWhileFollowing() throws Exception { // now assert that the retention leases have advanced to the global checkpoints assertBusy(() -> { - final IndicesStatsResponse stats = - leaderClient().admin().indices().stats(new IndicesStatsRequest().clear().indices(leaderIndex)).actionGet(); + final IndicesStatsResponse stats = leaderClient().admin() + .indices() + .stats(new IndicesStatsRequest().clear().indices(leaderIndex)) + .actionGet(); assertNotNull(stats.getShards()); assertThat(stats.getShards(), arrayWithSize(numberOfShards * (1 + numberOfReplicas))); final List shardsStats = getShardsStats(stats); for (int i = 0; i < numberOfShards * (1 + numberOfReplicas); i++) { assertNotNull(shardsStats.get(i).getRetentionLeaseStats()); final Map currentRetentionLeases = RetentionLeaseUtils.toMapExcludingPeerRecoveryRetentionLeases( - shardsStats.get(i).getRetentionLeaseStats().retentionLeases()); + shardsStats.get(i).getRetentionLeaseStats().retentionLeases() + ); assertThat(Strings.toString(shardsStats.get(i)), currentRetentionLeases.values(), hasSize(1)); - final RetentionLease retentionLease = - currentRetentionLeases.values().iterator().next(); + final RetentionLease retentionLease = currentRetentionLeases.values().iterator().next(); assertThat(retentionLease.id(), equalTo(getRetentionLeaseId(followerIndex, leaderIndex))); // we assert that retention leases are being advanced assertThat( - retentionLease.retainingSequenceNumber(), - equalTo(leaderGlobalCheckpoints.get(shardsStats.get(i).getShardRouting().id()) + 1)); + retentionLease.retainingSequenceNumber(), + equalTo(leaderGlobalCheckpoints.get(shardsStats.get(i).getShardRouting().id()) + 1) + ); } }); } @@ -617,8 +688,9 @@ public void testRetentionLeaseRenewalIsCancelledWhenFollowingIsPaused() throws E final int numberOfReplicas = randomIntBetween(0, 1); final Map additionalIndexSettings = new HashMap<>(); additionalIndexSettings.put( - IndexService.RETENTION_LEASE_SYNC_INTERVAL_SETTING.getKey(), - TimeValue.timeValueMillis(200).getStringRep()); + IndexService.RETENTION_LEASE_SYNC_INTERVAL_SETTING.getKey(), + TimeValue.timeValueMillis(200).getStringRep() + ); final String leaderIndexSettings = getIndexSettings(numberOfShards, numberOfReplicas, additionalIndexSettings); assertAcked(leaderClient().admin().indices().prepareCreate(leaderIndex).setSource(leaderIndexSettings, XContentType.JSON).get()); ensureLeaderYellow(leaderIndex); @@ -635,18 +707,18 @@ public void testRetentionLeaseRenewalIsCancelledWhenFollowingIsPaused() throws E * times that we sample the retention leases, which would cause our check to fail. */ final TimeValue syncIntervalSetting = IndexService.RETENTION_LEASE_SYNC_INTERVAL_SETTING.get( - leaderClient() - .admin() - .indices() - .prepareGetSettings(leaderIndex) - .get() - .getIndexToSettings() - .get(leaderIndex)); + leaderClient().admin().indices().prepareGetSettings(leaderIndex).get().getIndexToSettings().get(leaderIndex) + ); final long syncEnd = System.nanoTime(); Thread.sleep(Math.max(0, randomIntBetween(2, 4) * syncIntervalSetting.millis() - TimeUnit.NANOSECONDS.toMillis(syncEnd - start))); - final ClusterStateResponse leaderIndexClusterState = - leaderClient().admin().cluster().prepareState().clear().setMetadata(true).setIndices(leaderIndex).get(); + final ClusterStateResponse leaderIndexClusterState = leaderClient().admin() + .cluster() + .prepareState() + .clear() + .setMetadata(true) + .setIndices(leaderIndex) + .get(); final String leaderUUID = leaderIndexClusterState.getState().metadata().index(leaderIndex).getIndexUUID(); /* * We want to ensure that the background renewal is cancelled after pausing. To do this, we will sleep a small multiple of the renew @@ -659,27 +731,33 @@ public void testRetentionLeaseRenewalIsCancelledWhenFollowingIsPaused() throws E final List> retentionLeases = new ArrayList<>(); assertBusy(() -> { retentionLeases.clear(); - final IndicesStatsResponse stats = - leaderClient().admin().indices().stats(new IndicesStatsRequest().clear().indices(leaderIndex)).actionGet(); + final IndicesStatsResponse stats = leaderClient().admin() + .indices() + .stats(new IndicesStatsRequest().clear().indices(leaderIndex)) + .actionGet(); assertNotNull(stats.getShards()); assertThat(stats.getShards(), arrayWithSize(numberOfShards * (1 + numberOfReplicas))); final List shardsStats = getShardsStats(stats); for (int i = 0; i < numberOfShards * (1 + numberOfReplicas); i++) { assertNotNull(shardsStats.get(i).getRetentionLeaseStats()); - final Map currentRetentionLeases - = RetentionLeaseUtils.toMapExcludingPeerRecoveryRetentionLeases( - shardsStats.get(i).getRetentionLeaseStats().retentionLeases()); + final Map currentRetentionLeases = RetentionLeaseUtils + .toMapExcludingPeerRecoveryRetentionLeases(shardsStats.get(i).getRetentionLeaseStats().retentionLeases()); assertThat(Strings.toString(shardsStats.get(i)), currentRetentionLeases.values(), hasSize(1)); - final ClusterStateResponse followerIndexClusterState = - followerClient().admin().cluster().prepareState().clear().setMetadata(true).setIndices(followerIndex).get(); + final ClusterStateResponse followerIndexClusterState = followerClient().admin() + .cluster() + .prepareState() + .clear() + .setMetadata(true) + .setIndices(followerIndex) + .get(); final String followerUUID = followerIndexClusterState.getState().metadata().index(followerIndex).getIndexUUID(); - final RetentionLease retentionLease = - currentRetentionLeases.values().iterator().next(); + final RetentionLease retentionLease = currentRetentionLeases.values().iterator().next(); final String expectedRetentionLeaseId = retentionLeaseId( getFollowerCluster().getClusterName(), new Index(followerIndex, followerUUID), getLeaderCluster().getClusterName(), - new Index(leaderIndex, leaderUUID)); + new Index(leaderIndex, leaderUUID) + ); assertThat(retentionLease.id(), equalTo(expectedRetentionLeaseId)); retentionLeases.add(currentRetentionLeases); } @@ -688,11 +766,14 @@ public void testRetentionLeaseRenewalIsCancelledWhenFollowingIsPaused() throws E final TimeValue renewIntervalSetting = CcrRetentionLeases.RETENTION_LEASE_RENEW_INTERVAL_SETTING.get(followerClusterSettings()); final long renewEnd = System.nanoTime(); Thread.sleep( - Math.max(0, randomIntBetween(2, 4) * renewIntervalSetting.millis() - TimeUnit.NANOSECONDS.toMillis(renewEnd - start))); + Math.max(0, randomIntBetween(2, 4) * renewIntervalSetting.millis() - TimeUnit.NANOSECONDS.toMillis(renewEnd - start)) + ); // now ensure that the retention leases are the same - final IndicesStatsResponse stats = - leaderClient().admin().indices().stats(new IndicesStatsRequest().clear().indices(leaderIndex)).actionGet(); + final IndicesStatsResponse stats = leaderClient().admin() + .indices() + .stats(new IndicesStatsRequest().clear().indices(leaderIndex)) + .actionGet(); assertNotNull(stats.getShards()); assertThat(stats.getShards(), arrayWithSize(numberOfShards * (1 + numberOfReplicas))); final List shardsStats = getShardsStats(stats); @@ -702,13 +783,18 @@ public void testRetentionLeaseRenewalIsCancelledWhenFollowingIsPaused() throws E } assertNotNull(shardsStats.get(i).getRetentionLeaseStats()); final Map currentRetentionLeases = RetentionLeaseUtils.toMapExcludingPeerRecoveryRetentionLeases( - shardsStats.get(i).getRetentionLeaseStats().retentionLeases()); + shardsStats.get(i).getRetentionLeaseStats().retentionLeases() + ); assertThat(Strings.toString(shardsStats.get(i)), currentRetentionLeases.values(), hasSize(1)); - final ClusterStateResponse followerIndexClusterState = - followerClient().admin().cluster().prepareState().clear().setMetadata(true).setIndices(followerIndex).get(); + final ClusterStateResponse followerIndexClusterState = followerClient().admin() + .cluster() + .prepareState() + .clear() + .setMetadata(true) + .setIndices(followerIndex) + .get(); final String followerUUID = followerIndexClusterState.getState().metadata().index(followerIndex).getIndexUUID(); - final RetentionLease retentionLease = - currentRetentionLeases.values().iterator().next(); + final RetentionLease retentionLease = currentRetentionLeases.values().iterator().next(); assertThat(retentionLease.id(), equalTo(getRetentionLeaseId(followerIndex, followerUUID, leaderIndex, leaderUUID))); // we assert that retention leases are not being renewed by an unchanged timestamp assertThat(retentionLease.timestamp(), equalTo(retentionLeases.get(i).values().iterator().next().timestamp())); @@ -723,8 +809,9 @@ public void testRetentionLeaseRenewalIsResumedWhenFollowingIsResumed() throws Ex final int numberOfReplicas = randomIntBetween(0, 1); final Map additionalIndexSettings = new HashMap<>(); additionalIndexSettings.put( - IndexService.RETENTION_LEASE_SYNC_INTERVAL_SETTING.getKey(), - TimeValue.timeValueMillis(200).getStringRep()); + IndexService.RETENTION_LEASE_SYNC_INTERVAL_SETTING.getKey(), + TimeValue.timeValueMillis(200).getStringRep() + ); final String leaderIndexSettings = getIndexSettings(numberOfShards, numberOfReplicas, additionalIndexSettings); assertAcked(leaderClient().admin().indices().prepareCreate(leaderIndex).setSource(leaderIndexSettings, XContentType.JSON).get()); ensureLeaderYellow(leaderIndex); @@ -749,8 +836,9 @@ public void testRetentionLeaseIsAddedIfItDisappearsWhileFollowing() throws Excep final int numberOfReplicas = 1; final Map additionalIndexSettings = new HashMap<>(); additionalIndexSettings.put( - IndexService.RETENTION_LEASE_SYNC_INTERVAL_SETTING.getKey(), - TimeValue.timeValueMillis(200).getStringRep()); + IndexService.RETENTION_LEASE_SYNC_INTERVAL_SETTING.getKey(), + TimeValue.timeValueMillis(200).getStringRep() + ); final String leaderIndexSettings = getIndexSettings(numberOfShards, numberOfReplicas, additionalIndexSettings); assertAcked(leaderClient().admin().indices().prepareCreate(leaderIndex).setSource(leaderIndexSettings, XContentType.JSON).get()); ensureLeaderYellow(leaderIndex); @@ -764,51 +852,55 @@ public void testRetentionLeaseIsAddedIfItDisappearsWhileFollowing() throws Excep final ClusterStateResponse followerClusterState = followerClient().admin().cluster().prepareState().clear().setNodes(true).get(); try { for (final DiscoveryNode senderNode : followerClusterState.getState().nodes()) { - final MockTransportService senderTransportService = - (MockTransportService) getFollowerCluster().getInstance(TransportService.class, senderNode.getName()); - senderTransportService.addSendBehavior( - (connection, requestId, action, request, options) -> { - if (RetentionLeaseActions.Renew.ACTION_NAME.equals(action) - || TransportActionProxy.getProxyAction(RetentionLeaseActions.Renew.ACTION_NAME).equals(action)) { - final RetentionLeaseActions.RenewRequest renewRequest = (RetentionLeaseActions.RenewRequest) request; - final String retentionLeaseId = getRetentionLeaseId(followerIndex, leaderIndex); - if (retentionLeaseId.equals(renewRequest.getId())) { - logger.info("--> intercepting renewal request for retention lease [{}]", retentionLeaseId); - senderTransportService.clearAllRules(); - final String primaryShardNodeId = - getLeaderCluster() - .clusterService() - .state() - .routingTable() - .index(leaderIndex) - .shard(renewRequest.getShardId().id()) - .primaryShard() - .currentNodeId(); - final String primaryShardNodeName = - getLeaderCluster().clusterService().state().nodes().get(primaryShardNodeId).getName(); - final IndexShard primary = - getLeaderCluster() - .getInstance(IndicesService.class, primaryShardNodeName) - .getShardOrNull(renewRequest.getShardId()); - final CountDownLatch innerLatch = new CountDownLatch(1); - try { - // this forces the background renewal from following to face a retention lease not found exception - logger.info("--> removing retention lease [{}] on the leader", retentionLeaseId); - primary.removeRetentionLease(retentionLeaseId, - ActionListener.wrap(r -> innerLatch.countDown(), e -> fail(e.toString()))); - logger.info("--> waiting for the removed retention lease [{}] to be synced on the leader", - retentionLeaseId); - innerLatch.await(); - logger.info("--> removed retention lease [{}] on the leader", retentionLeaseId); - } catch (final Exception e) { - throw new AssertionError("failed to remove retention lease [" + retentionLeaseId + "] on the leader"); - } finally { - latch.countDown(); - } + final MockTransportService senderTransportService = (MockTransportService) getFollowerCluster().getInstance( + TransportService.class, + senderNode.getName() + ); + senderTransportService.addSendBehavior((connection, requestId, action, request, options) -> { + if (RetentionLeaseActions.Renew.ACTION_NAME.equals(action) + || TransportActionProxy.getProxyAction(RetentionLeaseActions.Renew.ACTION_NAME).equals(action)) { + final RetentionLeaseActions.RenewRequest renewRequest = (RetentionLeaseActions.RenewRequest) request; + final String retentionLeaseId = getRetentionLeaseId(followerIndex, leaderIndex); + if (retentionLeaseId.equals(renewRequest.getId())) { + logger.info("--> intercepting renewal request for retention lease [{}]", retentionLeaseId); + senderTransportService.clearAllRules(); + final String primaryShardNodeId = getLeaderCluster().clusterService() + .state() + .routingTable() + .index(leaderIndex) + .shard(renewRequest.getShardId().id()) + .primaryShard() + .currentNodeId(); + final String primaryShardNodeName = getLeaderCluster().clusterService() + .state() + .nodes() + .get(primaryShardNodeId) + .getName(); + final IndexShard primary = getLeaderCluster().getInstance(IndicesService.class, primaryShardNodeName) + .getShardOrNull(renewRequest.getShardId()); + final CountDownLatch innerLatch = new CountDownLatch(1); + try { + // this forces the background renewal from following to face a retention lease not found exception + logger.info("--> removing retention lease [{}] on the leader", retentionLeaseId); + primary.removeRetentionLease( + retentionLeaseId, + ActionListener.wrap(r -> innerLatch.countDown(), e -> fail(e.toString())) + ); + logger.info( + "--> waiting for the removed retention lease [{}] to be synced on the leader", + retentionLeaseId + ); + innerLatch.await(); + logger.info("--> removed retention lease [{}] on the leader", retentionLeaseId); + } catch (final Exception e) { + throw new AssertionError("failed to remove retention lease [" + retentionLeaseId + "] on the leader"); + } finally { + latch.countDown(); } } - connection.sendRequest(requestId, action, request, options); - }); + } + connection.sendRequest(requestId, action, request, options); + }); } latch.await(); @@ -816,8 +908,10 @@ public void testRetentionLeaseIsAddedIfItDisappearsWhileFollowing() throws Excep assertRetentionLeaseRenewal(numberOfShards, numberOfReplicas, followerIndex, leaderIndex); } finally { for (final DiscoveryNode senderNode : followerClusterState.getState().nodes()) { - final MockTransportService senderTransportService = - (MockTransportService) getFollowerCluster().getInstance(TransportService.class, senderNode.getName()); + final MockTransportService senderTransportService = (MockTransportService) getFollowerCluster().getInstance( + TransportService.class, + senderNode.getName() + ); senderTransportService.clearAllRules(); } } @@ -849,8 +943,9 @@ public void testPeriodicRenewalDoesNotAddRetentionLeaseAfterUnfollow() throws Ex final int numberOfReplicas = 1; final Map additionalIndexSettings = new HashMap<>(); additionalIndexSettings.put( - IndexService.RETENTION_LEASE_SYNC_INTERVAL_SETTING.getKey(), - TimeValue.timeValueMillis(200).getStringRep()); + IndexService.RETENTION_LEASE_SYNC_INTERVAL_SETTING.getKey(), + TimeValue.timeValueMillis(200).getStringRep() + ); final String leaderIndexSettings = getIndexSettings(numberOfShards, numberOfReplicas, additionalIndexSettings); assertAcked(leaderClient().admin().indices().prepareCreate(leaderIndex).setSource(leaderIndexSettings, XContentType.JSON).get()); ensureLeaderYellow(leaderIndex); @@ -867,43 +962,41 @@ public void testPeriodicRenewalDoesNotAddRetentionLeaseAfterUnfollow() throws Ex try { for (final DiscoveryNode senderNode : followerClusterState.getState().nodes()) { - final MockTransportService senderTransportService = - (MockTransportService) getFollowerCluster().getInstance(TransportService.class, senderNode.getName()); - senderTransportService.addSendBehavior( - (connection, requestId, action, request, options) -> { - if (RetentionLeaseActions.Renew.ACTION_NAME.equals(action) - || TransportActionProxy.getProxyAction(RetentionLeaseActions.Renew.ACTION_NAME).equals(action)) { - final String retentionLeaseId = getRetentionLeaseId(followerIndex, leaderIndex); - logger.info("--> blocking renewal request for retention lease [{}] until unfollowed", retentionLeaseId); - try { - removeLeaseLatch.countDown(); - unfollowLatch.await(); - - senderTransportService.addMessageListener(new TransportMessageListener() { - - @SuppressWarnings("rawtypes") - @Override - public void onResponseReceived( - final long responseRequestId, - final Transport.ResponseContext context) { - if (requestId == responseRequestId) { - final RetentionLeaseNotFoundException e = - new RetentionLeaseNotFoundException(retentionLeaseId); - context.handler().handleException(new RemoteTransportException(e.getMessage(), e)); - responseLatch.countDown(); - senderTransportService.removeMessageListener(this); - } - } - - }); - - } catch (final InterruptedException e) { - Thread.currentThread().interrupt(); - fail(e.toString()); + final MockTransportService senderTransportService = (MockTransportService) getFollowerCluster().getInstance( + TransportService.class, + senderNode.getName() + ); + senderTransportService.addSendBehavior((connection, requestId, action, request, options) -> { + if (RetentionLeaseActions.Renew.ACTION_NAME.equals(action) + || TransportActionProxy.getProxyAction(RetentionLeaseActions.Renew.ACTION_NAME).equals(action)) { + final String retentionLeaseId = getRetentionLeaseId(followerIndex, leaderIndex); + logger.info("--> blocking renewal request for retention lease [{}] until unfollowed", retentionLeaseId); + try { + removeLeaseLatch.countDown(); + unfollowLatch.await(); + + senderTransportService.addMessageListener(new TransportMessageListener() { + + @SuppressWarnings("rawtypes") + @Override + public void onResponseReceived(final long responseRequestId, final Transport.ResponseContext context) { + if (requestId == responseRequestId) { + final RetentionLeaseNotFoundException e = new RetentionLeaseNotFoundException(retentionLeaseId); + context.handler().handleException(new RemoteTransportException(e.getMessage(), e)); + responseLatch.countDown(); + senderTransportService.removeMessageListener(this); + } } - } - connection.sendRequest(requestId, action, request, options); - }); + + }); + + } catch (final InterruptedException e) { + Thread.currentThread().interrupt(); + fail(e.toString()); + } + } + connection.sendRequest(requestId, action, request, options); + }); } removeLeaseLatch.await(); @@ -916,18 +1009,26 @@ public void onResponseReceived( responseLatch.await(); - final IndicesStatsResponse afterUnfollowStats = - leaderClient().admin().indices().stats(new IndicesStatsRequest().clear().indices(leaderIndex)).actionGet(); + final IndicesStatsResponse afterUnfollowStats = leaderClient().admin() + .indices() + .stats(new IndicesStatsRequest().clear().indices(leaderIndex)) + .actionGet(); final List afterUnfollowShardsStats = getShardsStats(afterUnfollowStats); for (final ShardStats shardStats : afterUnfollowShardsStats) { assertNotNull(shardStats.getRetentionLeaseStats()); - assertThat(Strings.toString(shardStats), RetentionLeaseUtils.toMapExcludingPeerRecoveryRetentionLeases( - shardStats.getRetentionLeaseStats().retentionLeases()).values(), empty()); + assertThat( + Strings.toString(shardStats), + RetentionLeaseUtils.toMapExcludingPeerRecoveryRetentionLeases(shardStats.getRetentionLeaseStats().retentionLeases()) + .values(), + empty() + ); } } finally { for (final DiscoveryNode senderNode : followerClusterState.getState().nodes()) { - final MockTransportService senderTransportService = - (MockTransportService) getFollowerCluster().getInstance(TransportService.class, senderNode.getName()); + final MockTransportService senderTransportService = (MockTransportService) getFollowerCluster().getInstance( + TransportService.class, + senderNode.getName() + ); senderTransportService.clearAllRules(); } } @@ -948,18 +1049,25 @@ public void testForgetFollower() throws Exception { pauseFollow(followerIndex); followerClient().admin().indices().close(new CloseIndexRequest(followerIndex).masterNodeTimeout(TimeValue.MAX_VALUE)).actionGet(); - final ClusterStateResponse followerIndexClusterState = - followerClient().admin().cluster().prepareState().clear().setMetadata(true).setIndices(followerIndex).get(); + final ClusterStateResponse followerIndexClusterState = followerClient().admin() + .cluster() + .prepareState() + .clear() + .setMetadata(true) + .setIndices(followerIndex) + .get(); final String followerUUID = followerIndexClusterState.getState().metadata().index(followerIndex).getIndexUUID(); final BroadcastResponse forgetFollowerResponse = leaderClient().execute( - ForgetFollowerAction.INSTANCE, - new ForgetFollowerAction.Request( - getFollowerCluster().getClusterName(), - followerIndex, - followerUUID, - "leader_cluster", - leaderIndex)).actionGet(); + ForgetFollowerAction.INSTANCE, + new ForgetFollowerAction.Request( + getFollowerCluster().getClusterName(), + followerIndex, + followerUUID, + "leader_cluster", + leaderIndex + ) + ).actionGet(); logger.info(Strings.toString(forgetFollowerResponse)); assertThat(forgetFollowerResponse.getTotalShards(), equalTo(numberOfShards)); @@ -967,37 +1075,46 @@ public void testForgetFollower() throws Exception { assertThat(forgetFollowerResponse.getFailedShards(), equalTo(0)); assertThat(forgetFollowerResponse.getShardFailures(), emptyArray()); - final IndicesStatsResponse afterForgetFollowerStats = - leaderClient().admin().indices().stats(new IndicesStatsRequest().clear().indices(leaderIndex)).actionGet(); + final IndicesStatsResponse afterForgetFollowerStats = leaderClient().admin() + .indices() + .stats(new IndicesStatsRequest().clear().indices(leaderIndex)) + .actionGet(); final List afterForgetFollowerShardsStats = getShardsStats(afterForgetFollowerStats); for (final ShardStats shardStats : afterForgetFollowerShardsStats) { assertNotNull(shardStats.getRetentionLeaseStats()); - assertThat(Strings.toString(shardStats), RetentionLeaseUtils.toMapExcludingPeerRecoveryRetentionLeases( - shardStats.getRetentionLeaseStats().retentionLeases()).values(), empty()); + assertThat( + Strings.toString(shardStats), + RetentionLeaseUtils.toMapExcludingPeerRecoveryRetentionLeases(shardStats.getRetentionLeaseStats().retentionLeases()) + .values(), + empty() + ); } } private void assertRetentionLeaseRenewal( - final int numberOfShards, - final int numberOfReplicas, - final String followerIndex, - final String leaderIndex) throws Exception { + final int numberOfShards, + final int numberOfReplicas, + final String followerIndex, + final String leaderIndex + ) throws Exception { // ensure that a retention lease has been put in place on each shard, and grab a copy of them final List> retentionLeases = new ArrayList<>(); assertBusy(() -> { retentionLeases.clear(); - final IndicesStatsResponse stats = - leaderClient().admin().indices().stats(new IndicesStatsRequest().clear().indices(leaderIndex)).actionGet(); + final IndicesStatsResponse stats = leaderClient().admin() + .indices() + .stats(new IndicesStatsRequest().clear().indices(leaderIndex)) + .actionGet(); assertNotNull(stats.getShards()); assertThat(stats.getShards(), arrayWithSize(numberOfShards * (1 + numberOfReplicas))); final List shardsStats = getShardsStats(stats); for (int i = 0; i < numberOfShards * (1 + numberOfReplicas); i++) { assertNotNull(shardsStats.get(i).getRetentionLeaseStats()); final Map currentRetentionLeases = RetentionLeaseUtils.toMapExcludingPeerRecoveryRetentionLeases( - shardsStats.get(i).getRetentionLeaseStats().retentionLeases()); + shardsStats.get(i).getRetentionLeaseStats().retentionLeases() + ); assertThat(Strings.toString(shardsStats.get(i)), currentRetentionLeases.values(), hasSize(1)); - final RetentionLease retentionLease = - currentRetentionLeases.values().iterator().next(); + final RetentionLease retentionLease = currentRetentionLeases.values().iterator().next(); assertThat(retentionLease.id(), equalTo(getRetentionLeaseId(followerIndex, leaderIndex))); retentionLeases.add(currentRetentionLeases); } @@ -1005,18 +1122,20 @@ private void assertRetentionLeaseRenewal( // now ensure that the retention leases are being renewed assertBusy(() -> { - final IndicesStatsResponse stats = - leaderClient().admin().indices().stats(new IndicesStatsRequest().clear().indices(leaderIndex)).actionGet(); + final IndicesStatsResponse stats = leaderClient().admin() + .indices() + .stats(new IndicesStatsRequest().clear().indices(leaderIndex)) + .actionGet(); assertNotNull(stats.getShards()); assertThat(stats.getShards(), arrayWithSize(numberOfShards * (1 + numberOfReplicas))); final List shardsStats = getShardsStats(stats); for (int i = 0; i < numberOfShards * (1 + numberOfReplicas); i++) { assertNotNull(shardsStats.get(i).getRetentionLeaseStats()); final Map currentRetentionLeases = RetentionLeaseUtils.toMapExcludingPeerRecoveryRetentionLeases( - shardsStats.get(i).getRetentionLeaseStats().retentionLeases()); + shardsStats.get(i).getRetentionLeaseStats().retentionLeases() + ); assertThat(Strings.toString(shardsStats.get(i)), currentRetentionLeases.values(), hasSize(1)); - final RetentionLease retentionLease = - currentRetentionLeases.values().iterator().next(); + final RetentionLease retentionLease = currentRetentionLeases.values().iterator().next(); assertThat(retentionLease.id(), equalTo(getRetentionLeaseId(followerIndex, leaderIndex))); // we assert that retention leases are being renewed by an increase in the timestamp assertThat(retentionLease.timestamp(), greaterThan(retentionLeases.get(i).values().iterator().next().timestamp())); @@ -1032,24 +1151,32 @@ private void assertRetentionLeaseRenewal( * @return the shard stats in sorted order with (shard ID, primary) as the sort key */ private List getShardsStats(final IndicesStatsResponse stats) { - return Arrays.stream(stats.getShards()) - .sorted((s, t) -> { - if (s.getShardRouting().shardId().id() == t.getShardRouting().shardId().id()) { - return -Boolean.compare(s.getShardRouting().primary(), t.getShardRouting().primary()); - } else { - return Integer.compare(s.getShardRouting().shardId().id(), t.getShardRouting().shardId().id()); - } - }) - .collect(Collectors.toList()); + return Arrays.stream(stats.getShards()).sorted((s, t) -> { + if (s.getShardRouting().shardId().id() == t.getShardRouting().shardId().id()) { + return -Boolean.compare(s.getShardRouting().primary(), t.getShardRouting().primary()); + } else { + return Integer.compare(s.getShardRouting().shardId().id(), t.getShardRouting().shardId().id()); + } + }).collect(Collectors.toList()); } private String getRetentionLeaseId(final String followerIndex, final String leaderIndex) { - final ClusterStateResponse followerIndexClusterState = - followerClient().admin().cluster().prepareState().clear().setMetadata(true).setIndices(followerIndex).get(); + final ClusterStateResponse followerIndexClusterState = followerClient().admin() + .cluster() + .prepareState() + .clear() + .setMetadata(true) + .setIndices(followerIndex) + .get(); final String followerUUID = followerIndexClusterState.getState().metadata().index(followerIndex).getIndexUUID(); - final ClusterStateResponse leaderIndexClusterState = - leaderClient().admin().cluster().prepareState().clear().setMetadata(true).setIndices(leaderIndex).get(); + final ClusterStateResponse leaderIndexClusterState = leaderClient().admin() + .cluster() + .prepareState() + .clear() + .setMetadata(true) + .setIndices(leaderIndex) + .get(); final String leaderUUID = leaderIndexClusterState.getState().metadata().index(leaderIndex).getIndexUUID(); return getRetentionLeaseId(followerIndex, followerUUID, leaderIndex, leaderUUID); @@ -1057,10 +1184,11 @@ private String getRetentionLeaseId(final String followerIndex, final String lead private String getRetentionLeaseId(String followerIndex, String followerUUID, String leaderIndex, String leaderUUID) { return retentionLeaseId( - getFollowerCluster().getClusterName(), - new Index(followerIndex, followerUUID), - getLeaderCluster().getClusterName(), - new Index(leaderIndex, leaderUUID)); + getFollowerCluster().getClusterName(), + new Index(followerIndex, followerUUID), + getLeaderCluster().getClusterName(), + new Index(leaderIndex, leaderUUID) + ); } private void assertExpectedDocument(final String followerIndex, final int value) { diff --git a/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/CloseFollowerIndexIT.java b/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/CloseFollowerIndexIT.java index 9aa01f4afd3ed..289c8f50ea674 100644 --- a/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/CloseFollowerIndexIT.java +++ b/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/CloseFollowerIndexIT.java @@ -17,8 +17,8 @@ import org.elasticsearch.cluster.metadata.MetadataIndexStateService; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.engine.ReadOnlyEngine; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.CcrIntegTestCase; import org.elasticsearch.xpack.core.ccr.action.PutFollowAction; import org.junit.After; diff --git a/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/FollowInfoIT.java b/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/FollowInfoIT.java index 1efedc74dd4bb..efa7fcd0eec53 100644 --- a/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/FollowInfoIT.java +++ b/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/FollowInfoIT.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.ccr; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.IndexNotFoundException; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.CcrSingleNodeTestCase; import org.elasticsearch.xpack.core.ccr.action.FollowInfoAction; import org.elasticsearch.xpack.core.ccr.action.PauseFollowAction; diff --git a/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/FollowStatsIT.java b/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/FollowStatsIT.java index 2f279617b6036..0d97bd7d1d11a 100644 --- a/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/FollowStatsIT.java +++ b/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/FollowStatsIT.java @@ -13,8 +13,8 @@ import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse; import org.elasticsearch.action.admin.indices.close.CloseIndexRequest; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.persistent.PersistentTasksCustomMetadata; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.CcrSingleNodeTestCase; import org.elasticsearch.xpack.core.ccr.action.CcrStatsAction; import org.elasticsearch.xpack.core.ccr.action.FollowStatsAction; @@ -51,29 +51,30 @@ public void testStatsWhenNoPersistentTasksMetadataExists() throws InterruptedExc final AtomicBoolean onResponse = new AtomicBoolean(); final CountDownLatch latch = new CountDownLatch(1); client().execute( - FollowStatsAction.INSTANCE, - new FollowStatsAction.StatsRequest(), - new ActionListener() { - @Override - public void onResponse(final FollowStatsAction.StatsResponses statsResponses) { - try { - assertThat(statsResponses.getTaskFailures(), empty()); - assertThat(statsResponses.getNodeFailures(), empty()); - onResponse.set(true); - } finally { - latch.countDown(); - } + FollowStatsAction.INSTANCE, + new FollowStatsAction.StatsRequest(), + new ActionListener() { + @Override + public void onResponse(final FollowStatsAction.StatsResponses statsResponses) { + try { + assertThat(statsResponses.getTaskFailures(), empty()); + assertThat(statsResponses.getNodeFailures(), empty()); + onResponse.set(true); + } finally { + latch.countDown(); } - - @Override - public void onFailure(final Exception e) { - try { - fail(e.toString()); - } finally { - latch.countDown(); - } + } + + @Override + public void onFailure(final Exception e) { + try { + fail(e.toString()); + } finally { + latch.countDown(); } - }); + } + } + ); latch.await(); assertTrue(onResponse.get()); } @@ -92,18 +93,18 @@ public void testFollowStatsApiFollowerIndexFiltering() throws Exception { client().execute(PutFollowAction.INSTANCE, followRequest).get(); FollowStatsAction.StatsRequest statsRequest = new FollowStatsAction.StatsRequest(); - statsRequest.setIndices(new String[] {"follower1"}); + statsRequest.setIndices(new String[] { "follower1" }); FollowStatsAction.StatsResponses response = client().execute(FollowStatsAction.INSTANCE, statsRequest).actionGet(); assertThat(response.getStatsResponses().size(), equalTo(1)); assertThat(response.getStatsResponses().get(0).status().followerIndex(), equalTo("follower1")); statsRequest = new FollowStatsAction.StatsRequest(); - statsRequest.setIndices(new String[] {"follower2"}); + statsRequest.setIndices(new String[] { "follower2" }); response = client().execute(FollowStatsAction.INSTANCE, statsRequest).actionGet(); assertThat(response.getStatsResponses().size(), equalTo(1)); assertThat(response.getStatsResponses().get(0).status().followerIndex(), equalTo("follower2")); - response = client().execute(FollowStatsAction.INSTANCE, new FollowStatsAction.StatsRequest()).actionGet(); + response = client().execute(FollowStatsAction.INSTANCE, new FollowStatsAction.StatsRequest()).actionGet(); assertThat(response.getStatsResponses().size(), equalTo(2)); response.getStatsResponses().sort(Comparator.comparing(o -> o.status().followerIndex())); assertThat(response.getStatsResponses().get(0).status().followerIndex(), equalTo("follower1")); @@ -113,8 +114,10 @@ public void testFollowStatsApiFollowerIndexFiltering() throws Exception { assertAcked(client().execute(PauseFollowAction.INSTANCE, new PauseFollowAction.Request("follower2")).actionGet()); assertBusy(() -> { - List responseList = - client().execute(CcrStatsAction.INSTANCE, new CcrStatsAction.Request()).actionGet().getFollowStats().getStatsResponses(); + List responseList = client().execute(CcrStatsAction.INSTANCE, new CcrStatsAction.Request()) + .actionGet() + .getFollowStats() + .getStatsResponses(); assertThat(responseList.size(), equalTo(0)); }); } @@ -124,9 +127,11 @@ public void testFollowStatsApiResourceNotFound() throws Exception { FollowStatsAction.StatsResponses response = client().execute(FollowStatsAction.INSTANCE, statsRequest).actionGet(); assertThat(response.getStatsResponses().size(), equalTo(0)); - statsRequest.setIndices(new String[] {"follower1"}); - Exception e = expectThrows(ResourceNotFoundException.class, - () -> client().execute(FollowStatsAction.INSTANCE, statsRequest).actionGet()); + statsRequest.setIndices(new String[] { "follower1" }); + Exception e = expectThrows( + ResourceNotFoundException.class, + () -> client().execute(FollowStatsAction.INSTANCE, statsRequest).actionGet() + ); assertThat(e.getMessage(), equalTo("No shard follow tasks for follower indices [follower1]")); final String leaderIndexSettings = getIndexSettings(1, 0, Collections.emptyMap()); @@ -140,9 +145,8 @@ public void testFollowStatsApiResourceNotFound() throws Exception { assertThat(response.getStatsResponses().size(), equalTo(1)); assertThat(response.getStatsResponses().get(0).status().followerIndex(), equalTo("follower1")); - statsRequest.setIndices(new String[] {"follower2"}); - e = expectThrows(ResourceNotFoundException.class, - () -> client().execute(FollowStatsAction.INSTANCE, statsRequest).actionGet()); + statsRequest.setIndices(new String[] { "follower2" }); + e = expectThrows(ResourceNotFoundException.class, () -> client().execute(FollowStatsAction.INSTANCE, statsRequest).actionGet()); assertThat(e.getMessage(), equalTo("No shard follow tasks for follower indices [follower2]")); assertAcked(client().execute(PauseFollowAction.INSTANCE, new PauseFollowAction.Request("follower1")).actionGet()); @@ -162,7 +166,7 @@ public void testFollowStatsApiWithDeletedFollowerIndex() throws Exception { assertThat(response.getStatsResponses().get(0).status().followerIndex(), equalTo("follower1")); statsRequest = new FollowStatsAction.StatsRequest(); - statsRequest.setIndices(new String[] {"follower1"}); + statsRequest.setIndices(new String[] { "follower1" }); response = client().execute(FollowStatsAction.INSTANCE, statsRequest).actionGet(); assertThat(response.getStatsResponses().size(), equalTo(1)); assertThat(response.getStatsResponses().get(0).status().followerIndex(), equalTo("follower1")); @@ -190,7 +194,7 @@ public void testFollowStatsApiIncludeShardFollowStatsWithClosedFollowerIndex() t assertThat(response.getStatsResponses().get(0).status().followerIndex(), equalTo("follower1")); statsRequest = new FollowStatsAction.StatsRequest(); - statsRequest.setIndices(new String[] {"follower1"}); + statsRequest.setIndices(new String[] { "follower1" }); response = client().execute(FollowStatsAction.INSTANCE, statsRequest).actionGet(); assertThat(response.getStatsResponses().size(), equalTo(1)); assertThat(response.getStatsResponses().get(0).status().followerIndex(), equalTo("follower1")); @@ -203,7 +207,7 @@ public void testFollowStatsApiIncludeShardFollowStatsWithClosedFollowerIndex() t assertThat(response.getStatsResponses().get(0).status().followerIndex(), equalTo("follower1")); statsRequest = new FollowStatsAction.StatsRequest(); - statsRequest.setIndices(new String[] {"follower1"}); + statsRequest.setIndices(new String[] { "follower1" }); response = client().execute(FollowStatsAction.INSTANCE, statsRequest).actionGet(); assertThat(response.getStatsResponses().size(), equalTo(1)); assertThat(response.getStatsResponses().get(0).status().followerIndex(), equalTo("follower1")); diff --git a/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/FollowerFailOverIT.java b/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/FollowerFailOverIT.java index ab751bdc2f736..6305363f3072c 100644 --- a/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/FollowerFailOverIT.java +++ b/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/FollowerFailOverIT.java @@ -20,14 +20,14 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; -import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.common.xcontent.support.XContentMapValues; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.shard.IndexShard; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.indices.IndicesService; import org.elasticsearch.test.InternalTestCluster; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.CcrIntegTestCase; import org.elasticsearch.xpack.core.ccr.action.FollowStatsAction; import org.elasticsearch.xpack.core.ccr.action.PutFollowAction; @@ -76,8 +76,10 @@ public void testFailOverOnFollower() throws Exception { } if (frequently()) { String id = Integer.toString(frequently() ? docID.incrementAndGet() : between(0, 10)); // sometimes update - IndexResponse indexResponse = leaderClient().prepareIndex(leaderIndex).setId(id) - .setSource("{\"f\":" + id + "}", XContentType.JSON).get(); + IndexResponse indexResponse = leaderClient().prepareIndex(leaderIndex) + .setId(id) + .setSource("{\"f\":" + id + "}", XContentType.JSON) + .get(); logger.info("--> index {} id={} seq_no={}", leaderIndex, indexResponse.getId(), indexResponse.getSeqNo()); } else { String id = Integer.toString(between(0, docID.get())); @@ -224,8 +226,12 @@ public void testAddNewReplicasOnFollower() throws Exception { }); flushingOnFollower.start(); awaitGlobalCheckpointAtLeast(followerClient(), new ShardId(resolveFollowerIndex("follower-index"), 0), 50); - followerClient().admin().indices().prepareUpdateSettings("follower-index").setMasterNodeTimeout(TimeValue.MAX_VALUE) - .setSettings(Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, numberOfReplicas + 1).build()).get(); + followerClient().admin() + .indices() + .prepareUpdateSettings("follower-index") + .setMasterNodeTimeout(TimeValue.MAX_VALUE) + .setSettings(Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, numberOfReplicas + 1).build()) + .get(); ensureFollowerGreen("follower-index"); awaitGlobalCheckpointAtLeast(followerClient(), new ShardId(resolveFollowerIndex("follower-index"), 0), 100); stopped.set(true); @@ -240,15 +246,20 @@ public void testReadRequestsReturnLatestMappingVersion() throws Exception { Settings nodeAttributes = Settings.builder().put("node.attr.box", "large").build(); String dataNode = leaderCluster.startDataOnlyNode(nodeAttributes); assertAcked( - leaderClient().admin().indices().prepareCreate("leader-index") - .setSettings(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put("index.routing.allocation.require.box", "large")) + leaderClient().admin() + .indices() + .prepareCreate("leader-index") + .setSettings( + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put("index.routing.allocation.require.box", "large") + ) .get() ); getFollowerCluster().startNode( - onlyRoles(nodeAttributes, Set.of(DiscoveryNodeRole.DATA_ROLE, DiscoveryNodeRole.REMOTE_CLUSTER_CLIENT_ROLE))); + onlyRoles(nodeAttributes, Set.of(DiscoveryNodeRole.DATA_ROLE, DiscoveryNodeRole.REMOTE_CLUSTER_CLIENT_ROLE)) + ); followerClient().execute(PutFollowAction.INSTANCE, putFollow("leader-index", "follower-index")).get(); ensureFollowerGreen("follower-index"); ClusterService clusterService = leaderCluster.clusterService(dataNode); @@ -260,8 +271,9 @@ public void testReadRequestsReturnLatestMappingVersion() throws Exception { final CountDownLatch latch = new CountDownLatch(1); clusterService.addLowPriorityApplier(event -> { IndexMetadata imd = event.state().metadata().index("leader-index"); - if (imd != null && imd.mapping() != null && - XContentMapValues.extractValue("properties.balance.type", imd.mapping().sourceAsMap()) != null) { + if (imd != null + && imd.mapping() != null + && XContentMapValues.extractValue("properties.balance.type", imd.mapping().sourceAsMap()) != null) { try { logger.info("--> block ClusterService from exposing new mapping version"); latch.await(); @@ -270,8 +282,7 @@ public void testReadRequestsReturnLatestMappingVersion() throws Exception { } } }); - leaderCluster.client().admin().indices().preparePutMapping() - .setSource("balance", "type=long").setTimeout(TimeValue.ZERO).get(); + leaderCluster.client().admin().indices().preparePutMapping().setSource("balance", "type=long").setTimeout(TimeValue.ZERO).get(); try { // Make sure the mapping is ready on the shard before we execute the index request; otherwise the index request // will perform a dynamic mapping update which however will be blocked because the latch is remained closed. @@ -280,14 +291,20 @@ public void testReadRequestsReturnLatestMappingVersion() throws Exception { assertNotNull(mapper); assertNotNull(mapper.mappers().getMapper("balance")); }); - IndexResponse indexResp = leaderCluster.client().prepareIndex("leader-index").setId("1") - .setSource("{\"balance\": 100}", XContentType.JSON).setTimeout(TimeValue.ZERO).get(); + IndexResponse indexResp = leaderCluster.client() + .prepareIndex("leader-index") + .setId("1") + .setSource("{\"balance\": 100}", XContentType.JSON) + .setTimeout(TimeValue.ZERO) + .get(); assertThat(indexResp.getResult(), equalTo(DocWriteResponse.Result.CREATED)); assertThat(indexShard.getLastKnownGlobalCheckpoint(), equalTo(0L)); // Make sure at least one read-request which requires mapping sync is completed. assertBusy(() -> { - FollowStatsAction.StatsResponses responses = - followerClient().execute(FollowStatsAction.INSTANCE, new FollowStatsAction.StatsRequest()).actionGet(); + FollowStatsAction.StatsResponses responses = followerClient().execute( + FollowStatsAction.INSTANCE, + new FollowStatsAction.StatsRequest() + ).actionGet(); long bytesRead = responses.getStatsResponses().stream().mapToLong(r -> r.status().bytesRead()).sum(); assertThat(bytesRead, Matchers.greaterThan(0L)); }, 60, TimeUnit.SECONDS); diff --git a/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/IndexFollowingIT.java b/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/IndexFollowingIT.java index 453cd18dd691f..eea783fa365c8 100644 --- a/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/IndexFollowingIT.java +++ b/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/IndexFollowingIT.java @@ -8,6 +8,7 @@ package org.elasticsearch.xpack.ccr; import com.carrotsearch.hppc.cursors.ObjectCursor; + import org.apache.lucene.util.SetOnce; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchStatusException; @@ -58,16 +59,14 @@ import org.elasticsearch.cluster.routing.RoutingTable; import org.elasticsearch.cluster.routing.allocation.DataTier; import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.core.CheckedRunnable; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; -import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.common.xcontent.support.XContentMapValues; +import org.elasticsearch.core.CheckedRunnable; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexNotFoundException; import org.elasticsearch.index.IndexService; @@ -87,8 +86,9 @@ import org.elasticsearch.transport.RemoteClusterService; import org.elasticsearch.transport.SniffConnectionStrategy; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.CcrIntegTestCase; -import org.elasticsearch.xpack.core.ccr.action.ShardFollowTask; import org.elasticsearch.xpack.core.ccr.ShardFollowNodeTaskStatus; import org.elasticsearch.xpack.core.ccr.action.FollowStatsAction; import org.elasticsearch.xpack.core.ccr.action.FollowStatsAction.StatsRequest; @@ -97,6 +97,7 @@ import org.elasticsearch.xpack.core.ccr.action.PutAutoFollowPatternAction; import org.elasticsearch.xpack.core.ccr.action.PutFollowAction; import org.elasticsearch.xpack.core.ccr.action.ResumeFollowAction; +import org.elasticsearch.xpack.core.ccr.action.ShardFollowTask; import org.elasticsearch.xpack.core.ccr.action.UnfollowAction; import java.io.IOException; @@ -120,8 +121,8 @@ import java.util.stream.IntStream; import java.util.stream.Stream; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.xpack.ccr.CcrRetentionLeases.retentionLeaseId; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.empty; @@ -145,9 +146,14 @@ public void testFollowIndex() throws Exception { final int numberOfPrimaryShards = randomIntBetween(1, 3); int numberOfReplicas = between(0, 1); - followerClient().admin().cluster().prepareUpdateSettings().setMasterNodeTimeout(TimeValue.MAX_VALUE) - .setPersistentSettings(Settings.builder().put(CcrSettings.RECOVERY_CHUNK_SIZE.getKey(), - new ByteSizeValue(randomIntBetween(1, 1000), ByteSizeUnit.KB))) + followerClient().admin() + .cluster() + .prepareUpdateSettings() + .setMasterNodeTimeout(TimeValue.MAX_VALUE) + .setPersistentSettings( + Settings.builder() + .put(CcrSettings.RECOVERY_CHUNK_SIZE.getKey(), new ByteSizeValue(randomIntBetween(1, 1000), ByteSizeUnit.KB)) + ) .get(); final String leaderIndexSettings = getIndexSettings(numberOfPrimaryShards, numberOfReplicas); @@ -163,8 +169,9 @@ public void testFollowIndex() throws Exception { } logger.info("Indexing [{}] docs as first batch", firstBatchNumDocs); - try (BackgroundIndexer indexer = new BackgroundIndexer("index1", "_doc", leaderClient(), firstBatchNumDocs, - randomIntBetween(1, 5))) { + try ( + BackgroundIndexer indexer = new BackgroundIndexer("index1", "_doc", leaderClient(), firstBatchNumDocs, randomIntBetween(1, 5)) + ) { waitForDocs(randomInt(firstBatchNumDocs), indexer); leaderClient().admin().indices().prepareFlush("index1").setWaitIfOngoing(true).get(); waitForDocs(firstBatchNumDocs, indexer); @@ -196,8 +203,12 @@ public void testFollowIndex() throws Exception { } final Map firstBatchNumDocsPerShard = new HashMap<>(); - final ShardStats[] firstBatchShardStats = - leaderClient().admin().indices().prepareStats("index1").get().getIndex("index1").getShards(); + final ShardStats[] firstBatchShardStats = leaderClient().admin() + .indices() + .prepareStats("index1") + .get() + .getIndex("index1") + .getShards(); for (final ShardStats shardStats : firstBatchShardStats) { if (shardStats.getShardRouting().primary()) { long value = shardStats.getStats().getIndexing().getTotal().getIndexCount() - 1; @@ -223,8 +234,12 @@ public void testFollowIndex() throws Exception { waitForDocs(firstBatchNumDocs + secondBatchNumDocs, indexer); final Map secondBatchNumDocsPerShard = new HashMap<>(); - final ShardStats[] secondBatchShardStats = - leaderClient().admin().indices().prepareStats("index1").get().getIndex("index1").getShards(); + final ShardStats[] secondBatchShardStats = leaderClient().admin() + .indices() + .prepareStats("index1") + .get() + .getIndex("index1") + .getShards(); for (final ShardStats shardStats : secondBatchShardStats) { if (shardStats.getShardRouting().primary()) { final long value = shardStats.getStats().getIndexing().getTotal().getIndexCount() - 1; @@ -324,8 +339,12 @@ public void testFollowIndexWithoutWaitForComplete() throws Exception { ensureFollowerGreen(true, "index2"); final Map firstBatchNumDocsPerShard = new HashMap<>(); - final ShardStats[] firstBatchShardStats = - leaderClient().admin().indices().prepareStats("index1").get().getIndex("index1").getShards(); + final ShardStats[] firstBatchShardStats = leaderClient().admin() + .indices() + .prepareStats("index1") + .get() + .getIndex("index1") + .getShards(); for (final ShardStats shardStats : firstBatchShardStats) { if (shardStats.getShardRouting().primary()) { long value = shardStats.getStats().getIndexing().getTotal().getIndexCount() - 1; @@ -355,10 +374,10 @@ public void testSyncMappings() throws Exception { leaderClient().prepareIndex("index1").setId(Long.toString(i)).setSource(source, XContentType.JSON).get(); } - assertBusy(() -> assertThat(followerClient().prepareSearch("index2").get() - .getHits().getTotalHits().value, equalTo(firstBatchNumDocs))); - MappingMetadata mappingMetadata = followerClient().admin().indices().prepareGetMappings("index2").get().getMappings() - .get("index2"); + assertBusy( + () -> assertThat(followerClient().prepareSearch("index2").get().getHits().getTotalHits().value, equalTo(firstBatchNumDocs)) + ); + MappingMetadata mappingMetadata = followerClient().admin().indices().prepareGetMappings("index2").get().getMappings().get("index2"); assertThat(XContentMapValues.extractValue("properties.f.type", mappingMetadata.sourceAsMap()), equalTo("integer")); assertThat(XContentMapValues.extractValue("properties.k", mappingMetadata.sourceAsMap()), nullValue()); @@ -368,10 +387,13 @@ public void testSyncMappings() throws Exception { leaderClient().prepareIndex("index1").setId(Long.toString(i)).setSource(source, XContentType.JSON).get(); } - assertBusy(() -> assertThat(followerClient().prepareSearch("index2").get().getHits().getTotalHits().value, - equalTo(firstBatchNumDocs + secondBatchNumDocs))); - mappingMetadata = followerClient().admin().indices().prepareGetMappings("index2").get().getMappings() - .get("index2"); + assertBusy( + () -> assertThat( + followerClient().prepareSearch("index2").get().getHits().getTotalHits().value, + equalTo(firstBatchNumDocs + secondBatchNumDocs) + ) + ); + mappingMetadata = followerClient().admin().indices().prepareGetMappings("index2").get().getMappings().get("index2"); assertThat(XContentMapValues.extractValue("properties.f.type", mappingMetadata.sourceAsMap()), equalTo("integer")); assertThat(XContentMapValues.extractValue("properties.k.type", mappingMetadata.sourceAsMap()), equalTo("long")); pauseFollow("index2"); @@ -379,11 +401,17 @@ public void testSyncMappings() throws Exception { } public void testNoMappingDefined() throws Exception { - assertAcked(leaderClient().admin().indices().prepareCreate("index1") - .setSettings(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .build())); + assertAcked( + leaderClient().admin() + .indices() + .prepareCreate("index1") + .setSettings( + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .build() + ) + ); ensureLeaderGreen("index1"); final PutFollowAction.Request followRequest = putFollow("index1", "index2"); @@ -393,8 +421,7 @@ public void testNoMappingDefined() throws Exception { assertBusy(() -> assertThat(followerClient().prepareSearch("index2").get().getHits().getTotalHits().value, equalTo(1L))); pauseFollow("index2"); - MappingMetadata mappingMetadata = followerClient().admin().indices().prepareGetMappings("index2").get().getMappings() - .get("index2"); + MappingMetadata mappingMetadata = followerClient().admin().indices().prepareGetMappings("index2").get().getMappings().get("index2"); assertThat(XContentMapValues.extractValue("properties.f.type", mappingMetadata.sourceAsMap()), equalTo("long")); assertThat(XContentMapValues.extractValue("properties.k", mappingMetadata.sourceAsMap()), nullValue()); } @@ -405,11 +432,17 @@ public void testDoNotAllowPutMappingToFollower() throws Exception { assertAcked(leaderClient().admin().indices().prepareCreate("index-1").setSource(leaderIndexSettings, XContentType.JSON)); followerClient().execute(PutFollowAction.INSTANCE, putFollow("index-1", "index-2")).get(); PutMappingRequest putMappingRequest = new PutMappingRequest("index-2").source("new_field", "type=keyword"); - ElasticsearchStatusException forbiddenException = expectThrows(ElasticsearchStatusException.class, - () -> followerClient().admin().indices().putMapping(putMappingRequest).actionGet()); - assertThat(forbiddenException.getMessage(), - equalTo("can't put mapping to the following indices [index-2]; " + - "the mapping of the following indices are self-replicated from its leader indices")); + ElasticsearchStatusException forbiddenException = expectThrows( + ElasticsearchStatusException.class, + () -> followerClient().admin().indices().putMapping(putMappingRequest).actionGet() + ); + assertThat( + forbiddenException.getMessage(), + equalTo( + "can't put mapping to the following indices [index-2]; " + + "the mapping of the following indices are self-replicated from its leader indices" + ) + ); assertThat(forbiddenException.status(), equalTo(RestStatus.FORBIDDEN)); pauseFollow("index-2"); followerClient().admin().indices().close(new CloseIndexRequest("index-2")).actionGet(); @@ -419,24 +452,29 @@ public void testDoNotAllowPutMappingToFollower() throws Exception { } public void testDoNotAllowAddAliasToFollower() throws Exception { - final String leaderIndexSettings = - getIndexSettings(between(1, 2), between(0, 1)); + final String leaderIndexSettings = getIndexSettings(between(1, 2), between(0, 1)); assertAcked(leaderClient().admin().indices().prepareCreate("leader").setSource(leaderIndexSettings, XContentType.JSON)); followerClient().execute(PutFollowAction.INSTANCE, putFollow("leader", "follower")).get(); final IndicesAliasesRequest request = new IndicesAliasesRequest().masterNodeTimeout(TimeValue.MAX_VALUE) - .addAliasAction(IndicesAliasesRequest.AliasActions.add().index("follower").alias("follower_alias")); - final ElasticsearchStatusException e = - expectThrows(ElasticsearchStatusException.class, () -> followerClient().admin().indices().aliases(request).actionGet()); + .addAliasAction(IndicesAliasesRequest.AliasActions.add().index("follower").alias("follower_alias")); + final ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> followerClient().admin().indices().aliases(request).actionGet() + ); assertThat( - e, - hasToString(containsString("can't modify aliases on indices [follower]; " - + "aliases of following indices are self-replicated from their leader indices"))); + e, + hasToString( + containsString( + "can't modify aliases on indices [follower]; " + + "aliases of following indices are self-replicated from their leader indices" + ) + ) + ); assertThat(e.status(), equalTo(RestStatus.FORBIDDEN)); } public void testAddAliasAfterUnfollow() throws Exception { - final String leaderIndexSettings = - getIndexSettings(between(1, 2), between(0, 1)); + final String leaderIndexSettings = getIndexSettings(between(1, 2), between(0, 1)); assertAcked(leaderClient().admin().indices().prepareCreate("leader").setSource(leaderIndexSettings, XContentType.JSON)); followerClient().execute(PutFollowAction.INSTANCE, putFollow("leader", "follower")).get(); pauseFollow("follower"); @@ -444,10 +482,12 @@ public void testAddAliasAfterUnfollow() throws Exception { assertAcked(followerClient().execute(UnfollowAction.INSTANCE, new UnfollowAction.Request("follower")).actionGet()); followerClient().admin().indices().open(new OpenIndexRequest("follower").masterNodeTimeout(TimeValue.MAX_VALUE)).actionGet(); final IndicesAliasesRequest request = new IndicesAliasesRequest().masterNodeTimeout(TimeValue.MAX_VALUE) - .addAliasAction(IndicesAliasesRequest.AliasActions.add().index("follower").alias("follower_alias")); + .addAliasAction(IndicesAliasesRequest.AliasActions.add().index("follower").alias("follower_alias")); assertAcked(followerClient().admin().indices().aliases(request).actionGet()); - final GetAliasesResponse response = - followerClient().admin().indices().getAliases(new GetAliasesRequest("follower_alias")).actionGet(); + final GetAliasesResponse response = followerClient().admin() + .indices() + .getAliases(new GetAliasesRequest("follower_alias")) + .actionGet(); assertThat(response.getAliases().keys().size(), equalTo(1)); assertThat(response.getAliases().keys().iterator().next().value, equalTo("follower")); final List aliasMetadata = response.getAliases().get("follower"); @@ -487,8 +527,7 @@ public void afterBulk(long executionId, BulkRequest request, Throwable failure) throw new AssertionError(e); } final String source = String.format(Locale.ROOT, "{\"f\":%d}", counter++); - IndexRequest indexRequest = new IndexRequest("index1") - .source(source, XContentType.JSON) + IndexRequest indexRequest = new IndexRequest("index1").source(source, XContentType.JSON) .timeout(TimeValue.timeValueSeconds(1)); bulkProcessor.add(indexRequest); } @@ -507,7 +546,7 @@ public void afterBulk(long executionId, BulkRequest request, Throwable failure) followRequest.getParameters().setMaxOutstandingWriteRequests(randomIntBetween(1, 10)); followRequest.getParameters().setMaxWriteBufferCount(randomIntBetween(1024, 10240)); followerClient().execute(PutFollowAction.INSTANCE, followRequest).get(); - availableDocs.release(numDocsIndexed * 2 + bulkSize); + availableDocs.release(numDocsIndexed * 2 + bulkSize); atLeastDocsIndexed(leaderClient(), "index1", numDocsIndexed); run.set(false); thread.join(); @@ -550,8 +589,10 @@ public void testFollowIndexWithNestedField() throws Exception { final GetResponse getResponse = followerClient().prepareGet("index2", Integer.toString(value)).get(); assertTrue(getResponse.isExists()); assertTrue((getResponse.getSource().containsKey("field"))); - assertThat(XContentMapValues.extractValue("objects.field", getResponse.getSource()), - equalTo(Collections.singletonList(value))); + assertThat( + XContentMapValues.extractValue("objects.field", getResponse.getSource()), + equalTo(Collections.singletonList(value)) + ); }); } pauseFollow("index2"); @@ -560,32 +601,37 @@ public void testFollowIndexWithNestedField() throws Exception { public void testUnfollowNonExistingIndex() { PauseFollowAction.Request unfollowRequest = new PauseFollowAction.Request("non-existing-index"); - expectThrows(IndexNotFoundException.class, - () -> followerClient().execute(PauseFollowAction.INSTANCE, unfollowRequest).actionGet()); + expectThrows(IndexNotFoundException.class, () -> followerClient().execute(PauseFollowAction.INSTANCE, unfollowRequest).actionGet()); } public void testFollowNonExistentIndex() throws Exception { String indexSettings = getIndexSettings(1, 0); assertAcked(leaderClient().admin().indices().prepareCreate("test-leader").setSource(indexSettings, XContentType.JSON).get()); - assertAcked(followerClient().admin().indices().prepareCreate("test-follower") - .setSource(indexSettings, XContentType.JSON) - .setMasterNodeTimeout(TimeValue.MAX_VALUE) - .get()); + assertAcked( + followerClient().admin() + .indices() + .prepareCreate("test-follower") + .setSource(indexSettings, XContentType.JSON) + .setMasterNodeTimeout(TimeValue.MAX_VALUE) + .get() + ); ensureLeaderGreen("test-leader"); ensureFollowerGreen("test-follower"); // Leader index does not exist. - expectThrows(IndexNotFoundException.class, - () -> followerClient().execute(PutFollowAction.INSTANCE, putFollow("non-existent-leader", "test-follower")) - .actionGet()); + expectThrows( + IndexNotFoundException.class, + () -> followerClient().execute(PutFollowAction.INSTANCE, putFollow("non-existent-leader", "test-follower")).actionGet() + ); // Follower index does not exist. ResumeFollowAction.Request followRequest1 = resumeFollow("non-existent-follower"); expectThrows(IndexNotFoundException.class, () -> followerClient().execute(ResumeFollowAction.INSTANCE, followRequest1).actionGet()); // Both indices do not exist. ResumeFollowAction.Request followRequest2 = resumeFollow("non-existent-follower"); expectThrows(IndexNotFoundException.class, () -> followerClient().execute(ResumeFollowAction.INSTANCE, followRequest2).actionGet()); - expectThrows(IndexNotFoundException.class, - () -> followerClient().execute(PutFollowAction.INSTANCE, putFollow("non-existing-leader", "non-existing-follower")) - .actionGet()); + expectThrows( + IndexNotFoundException.class, + () -> followerClient().execute(PutFollowAction.INSTANCE, putFollow("non-existing-leader", "non-existing-follower")).actionGet() + ); } public void testFollowIndexMaxOperationSizeInBytes() throws Exception { @@ -605,8 +651,12 @@ public void testFollowIndexMaxOperationSizeInBytes() throws Exception { followerClient().execute(PutFollowAction.INSTANCE, followRequest).get(); final Map firstBatchNumDocsPerShard = new HashMap<>(); - final ShardStats[] firstBatchShardStats = - leaderClient().admin().indices().prepareStats("index1").get().getIndex("index1").getShards(); + final ShardStats[] firstBatchShardStats = leaderClient().admin() + .indices() + .prepareStats("index1") + .get() + .getIndex("index1") + .getShards(); for (final ShardStats shardStats : firstBatchShardStats) { if (shardStats.getShardRouting().primary()) { long value = shardStats.getStats().getIndexing().getTotal().getIndexCount() - 1; @@ -633,18 +683,28 @@ public void testAttemptToChangeCcrFollowingIndexSetting() throws Exception { UpdateSettingsRequest updateSettingsRequest = new UpdateSettingsRequest("index2").masterNodeTimeout(TimeValue.MAX_VALUE); updateSettingsRequest.settings(Settings.builder().put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), false).build()); - Exception e = expectThrows(IllegalArgumentException.class, - () -> followerClient().admin().indices().updateSettings(updateSettingsRequest).actionGet()); - assertThat(e.getMessage(), equalTo("can not update internal setting [index.xpack.ccr.following_index]; " + - "this setting is managed via a dedicated API")); + Exception e = expectThrows( + IllegalArgumentException.class, + () -> followerClient().admin().indices().updateSettings(updateSettingsRequest).actionGet() + ); + assertThat( + e.getMessage(), + equalTo("can not update internal setting [index.xpack.ccr.following_index]; " + "this setting is managed via a dedicated API") + ); } public void testCloseLeaderIndex() throws Exception { - assertAcked(leaderClient().admin().indices().prepareCreate("index1") - .setSettings(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .build())); + assertAcked( + leaderClient().admin() + .indices() + .prepareCreate("index1") + .setSettings( + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .build() + ) + ); final PutFollowAction.Request followRequest = putFollow("index1", "index2"); followerClient().execute(PutFollowAction.INSTANCE, followRequest).get(); @@ -660,8 +720,15 @@ public void testCloseLeaderIndex() throws Exception { assertThat(response.getStatsResponses(), hasSize(1)); assertThat(response.getStatsResponses().get(0).status().failedReadRequests(), greaterThanOrEqualTo(1L)); assertThat(response.getStatsResponses().get(0).status().readExceptions().size(), equalTo(1)); - ElasticsearchException exception = response.getStatsResponses().get(0).status() - .readExceptions().entrySet().iterator().next().getValue().v2(); + ElasticsearchException exception = response.getStatsResponses() + .get(0) + .status() + .readExceptions() + .entrySet() + .iterator() + .next() + .getValue() + .v2(); assertThat(exception.getRootCause().getMessage(), equalTo("index [index1] blocked by: [FORBIDDEN/4/index closed];")); }); @@ -673,11 +740,17 @@ public void testCloseLeaderIndex() throws Exception { } public void testCloseFollowIndex() throws Exception { - assertAcked(leaderClient().admin().indices().prepareCreate("index1") - .setSettings(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .build())); + assertAcked( + leaderClient().admin() + .indices() + .prepareCreate("index1") + .setSettings( + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .build() + ) + ); final PutFollowAction.Request followRequest = putFollow("index1", "index2"); followerClient().execute(PutFollowAction.INSTANCE, followRequest).get(); @@ -701,11 +774,17 @@ public void testCloseFollowIndex() throws Exception { } public void testDeleteLeaderIndex() throws Exception { - assertAcked(leaderClient().admin().indices().prepareCreate("index1") - .setSettings(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .build())); + assertAcked( + leaderClient().admin() + .indices() + .prepareCreate("index1") + .setSettings( + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .build() + ) + ); final PutFollowAction.Request followRequest = putFollow("index1", "index2"); followerClient().execute(PutFollowAction.INSTANCE, followRequest).get(); @@ -734,26 +813,34 @@ public void testFollowClosedIndex() { assertAcked(leaderClient().admin().indices().prepareClose(leaderIndex)); final String followerIndex = "follow-test-index"; - expectThrows(IndexClosedException.class, - () -> followerClient().execute(PutFollowAction.INSTANCE, putFollow(leaderIndex, followerIndex)).actionGet()); + expectThrows( + IndexClosedException.class, + () -> followerClient().execute(PutFollowAction.INSTANCE, putFollow(leaderIndex, followerIndex)).actionGet() + ); assertFalse(ESIntegTestCase.indexExists(followerIndex, followerClient())); } public void testResumeFollowOnClosedIndex() throws Exception { final String leaderIndex = "test-index"; - assertAcked(leaderClient().admin().indices().prepareCreate(leaderIndex) - .setSettings(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .build())); + assertAcked( + leaderClient().admin() + .indices() + .prepareCreate(leaderIndex) + .setSettings( + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .build() + ) + ); ensureLeaderGreen(leaderIndex); final int nbDocs = randomIntBetween(10, 100); IntStream.of(nbDocs).forEach(i -> leaderClient().prepareIndex().setIndex(leaderIndex).setSource("field", i).get()); final String followerIndex = "follow-test-index"; - PutFollowAction.Response response = - followerClient().execute(PutFollowAction.INSTANCE, putFollow(leaderIndex, followerIndex)).actionGet(); + PutFollowAction.Response response = followerClient().execute(PutFollowAction.INSTANCE, putFollow(leaderIndex, followerIndex)) + .actionGet(); assertTrue(response.isFollowIndexCreated()); assertTrue(response.isFollowIndexShardsAcked()); assertTrue(response.isIndexFollowingStarted()); @@ -761,16 +848,24 @@ public void testResumeFollowOnClosedIndex() throws Exception { pauseFollow(followerIndex); assertAcked(leaderClient().admin().indices().prepareClose(leaderIndex).setMasterNodeTimeout(TimeValue.MAX_VALUE)); - expectThrows(IndexClosedException.class, () -> - followerClient().execute(ResumeFollowAction.INSTANCE, resumeFollow(followerIndex)).actionGet()); + expectThrows( + IndexClosedException.class, + () -> followerClient().execute(ResumeFollowAction.INSTANCE, resumeFollow(followerIndex)).actionGet() + ); } public void testDeleteFollowerIndex() throws Exception { - assertAcked(leaderClient().admin().indices().prepareCreate("index1") - .setSettings(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .build())); + assertAcked( + leaderClient().admin() + .indices() + .prepareCreate("index1") + .setSettings( + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .build() + ) + ); final PutFollowAction.Request followRequest = putFollow("index1", "index2"); followerClient().execute(PutFollowAction.INSTANCE, followRequest).get(); @@ -796,23 +891,41 @@ public void testDeleteFollowerIndex() throws Exception { } public void testPauseIndex() throws Exception { - assertAcked(leaderClient().admin().indices().prepareCreate("leader") - .setSettings(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .build())); + assertAcked( + leaderClient().admin() + .indices() + .prepareCreate("leader") + .setSettings( + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .build() + ) + ); followerClient().execute(PutFollowAction.INSTANCE, putFollow("leader", "follower")).get(); assertAcked(followerClient().admin().indices().prepareCreate("regular-index").setMasterNodeTimeout(TimeValue.MAX_VALUE)); assertAcked(followerClient().execute(PauseFollowAction.INSTANCE, new PauseFollowAction.Request("follower")).actionGet()); - assertThat(expectThrows(IllegalArgumentException.class, () -> followerClient().execute( - PauseFollowAction.INSTANCE, new PauseFollowAction.Request("follower")).actionGet()).getMessage(), - equalTo("no shard follow tasks for [follower]")); - assertThat(expectThrows(IllegalArgumentException.class, () -> followerClient().execute( - PauseFollowAction.INSTANCE, new PauseFollowAction.Request("regular-index")).actionGet()).getMessage(), - equalTo("index [regular-index] is not a follower index")); - assertThat(expectThrows(IndexNotFoundException.class, () -> followerClient().execute( - PauseFollowAction.INSTANCE, new PauseFollowAction.Request("xyz")).actionGet()).getMessage(), - equalTo("no such index [xyz]")); + assertThat( + expectThrows( + IllegalArgumentException.class, + () -> followerClient().execute(PauseFollowAction.INSTANCE, new PauseFollowAction.Request("follower")).actionGet() + ).getMessage(), + equalTo("no shard follow tasks for [follower]") + ); + assertThat( + expectThrows( + IllegalArgumentException.class, + () -> followerClient().execute(PauseFollowAction.INSTANCE, new PauseFollowAction.Request("regular-index")).actionGet() + ).getMessage(), + equalTo("index [regular-index] is not a follower index") + ); + assertThat( + expectThrows( + IndexNotFoundException.class, + () -> followerClient().execute(PauseFollowAction.INSTANCE, new PauseFollowAction.Request("xyz")).actionGet() + ).getMessage(), + equalTo("no such index [xyz]") + ); } public void testUnfollowIndex() throws Exception { @@ -821,9 +934,7 @@ public void testUnfollowIndex() throws Exception { PutFollowAction.Request followRequest = putFollow("index1", "index2"); followerClient().execute(PutFollowAction.INSTANCE, followRequest).get(); leaderClient().prepareIndex("index1").setSource("{}", XContentType.JSON).get(); - assertBusy(() -> { - assertThat(followerClient().prepareSearch("index2").get().getHits().getTotalHits().value, equalTo(1L)); - }); + assertBusy(() -> { assertThat(followerClient().prepareSearch("index2").get().getHits().getTotalHits().value, equalTo(1L)); }); // Indexing directly into index2 would fail now, because index2 is a follow index. // We can't test this here because an assertion trips before an actual error is thrown and then index call hangs. @@ -836,7 +947,8 @@ public void testUnfollowIndex() throws Exception { ensureFollowerGreen("index2"); // Indexing succeeds now, because index2 is no longer a follow index: - followerClient().prepareIndex("index2").setSource("{}", XContentType.JSON) + followerClient().prepareIndex("index2") + .setSource("{}", XContentType.JSON) .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) .get(); assertThat(followerClient().prepareSearch("index2").get().getHits().getTotalHits().value, equalTo(2L)); @@ -848,15 +960,19 @@ public void testUnknownClusterAlias() throws Exception { ensureLeaderGreen("index1"); PutFollowAction.Request followRequest = putFollow("index1", "index2"); followRequest.setRemoteCluster("another_cluster"); - Exception e = expectThrows(NoSuchRemoteClusterException.class, - () -> followerClient().execute(PutFollowAction.INSTANCE, followRequest).actionGet()); + Exception e = expectThrows( + NoSuchRemoteClusterException.class, + () -> followerClient().execute(PutFollowAction.INSTANCE, followRequest).actionGet() + ); assertThat(e.getMessage(), equalTo("no such remote cluster: [another_cluster]")); PutAutoFollowPatternAction.Request putAutoFollowRequest = new PutAutoFollowPatternAction.Request(); putAutoFollowRequest.setName("name"); putAutoFollowRequest.setRemoteCluster("another_cluster"); putAutoFollowRequest.setLeaderIndexPatterns(Collections.singletonList("logs-*")); - e = expectThrows(NoSuchRemoteClusterException.class, - () -> followerClient().execute(PutAutoFollowPatternAction.INSTANCE, putAutoFollowRequest).actionGet()); + e = expectThrows( + NoSuchRemoteClusterException.class, + () -> followerClient().execute(PutAutoFollowPatternAction.INSTANCE, putAutoFollowRequest).actionGet() + ); assertThat(e.getMessage(), equalTo("no such remote cluster: [another_cluster]")); } @@ -865,16 +981,24 @@ public void testLeaderIndexRed() throws Exception { ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); updateSettingsRequest.persistentSettings(Settings.builder().put("cluster.routing.allocation.enable", "none")); assertAcked(leaderClient().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); - assertAcked(leaderClient().admin().indices().prepareCreate("index1") - .setWaitForActiveShards(ActiveShardCount.NONE) - .setSettings(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .build())); + assertAcked( + leaderClient().admin() + .indices() + .prepareCreate("index1") + .setWaitForActiveShards(ActiveShardCount.NONE) + .setSettings( + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .build() + ) + ); final PutFollowAction.Request followRequest = putFollow("index1", "index2"); - Exception e = expectThrows(IllegalArgumentException.class, - () -> followerClient().execute(PutFollowAction.INSTANCE, followRequest).actionGet()); + Exception e = expectThrows( + IllegalArgumentException.class, + () -> followerClient().execute(PutFollowAction.INSTANCE, followRequest).actionGet() + ); assertThat(e.getMessage(), equalTo("no index stats available for the leader index")); assertThat(ESIntegTestCase.indexExists("index2", followerClient()), is(false)); @@ -899,8 +1023,9 @@ public void testUpdateDynamicLeaderIndexSettings() throws Exception { for (long i = 0; i < firstBatchNumDocs; i++) { leaderClient().prepareIndex("leader").setSource("{}", XContentType.JSON).get(); } - assertBusy(() -> assertThat(followerClient().prepareSearch("follower").get() - .getHits().getTotalHits().value, equalTo(firstBatchNumDocs))); + assertBusy( + () -> assertThat(followerClient().prepareSearch("follower").get().getHits().getTotalHits().value, equalTo(firstBatchNumDocs)) + ); // Sanity check that the setting has not been set in follower index: { @@ -927,8 +1052,10 @@ public void testUpdateDynamicLeaderIndexSettings() throws Exception { assertThat(getFollowTaskSettingsVersion("follower"), equalTo(2L)); try { - assertThat(followerClient().prepareSearch("follower").get().getHits().getTotalHits().value, - equalTo(firstBatchNumDocs + secondBatchNumDocs)); + assertThat( + followerClient().prepareSearch("follower").get().getHits().getTotalHits().value, + equalTo(firstBatchNumDocs + secondBatchNumDocs) + ); } catch (Exception e) { throw new AssertionError("error while searching", e); } @@ -952,8 +1079,9 @@ public void testLeaderIndexSettingNotPercolatedToFollower() throws Exception { for (long i = 0; i < firstBatchNumDocs; i++) { leaderClient().prepareIndex("leader").setSource("{}", XContentType.JSON).get(); } - assertBusy(() -> assertThat(followerClient().prepareSearch("follower").get() - .getHits().getTotalHits().value, equalTo(firstBatchNumDocs))); + assertBusy( + () -> assertThat(followerClient().prepareSearch("follower").get().getHits().getTotalHits().value, equalTo(firstBatchNumDocs)) + ); // Sanity check that the setting has not been set in follower index: { @@ -979,8 +1107,10 @@ public void testLeaderIndexSettingNotPercolatedToFollower() throws Exception { assertThat(getFollowTaskSettingsVersion("follower"), equalTo(2L)); try { - assertThat(followerClient().prepareSearch("follower").get().getHits().getTotalHits().value, - equalTo(firstBatchNumDocs + secondBatchNumDocs)); + assertThat( + followerClient().prepareSearch("follower").get().getHits().getTotalHits().value, + equalTo(firstBatchNumDocs + secondBatchNumDocs) + ); } catch (Exception e) { throw new AssertionError("error while searching", e); } @@ -1002,8 +1132,9 @@ public void testUpdateAnalysisLeaderIndexSettings() throws Exception { leaderClient().prepareIndex("leader").setSource("{}", XContentType.JSON).get(); } - assertBusy(() -> assertThat(followerClient().prepareSearch("follower").get() - .getHits().getTotalHits().value, equalTo(firstBatchNumDocs))); + assertBusy( + () -> assertThat(followerClient().prepareSearch("follower").get().getHits().getTotalHits().value, equalTo(firstBatchNumDocs)) + ); assertThat(getFollowTaskSettingsVersion("follower"), equalTo(1L)); assertThat(getFollowTaskMappingVersion("follower"), equalTo(1L)); @@ -1011,9 +1142,10 @@ public void testUpdateAnalysisLeaderIndexSettings() throws Exception { assertAcked(leaderClient().admin().indices().close(closeIndexRequest).actionGet()); UpdateSettingsRequest updateSettingsRequest = new UpdateSettingsRequest("leader"); - updateSettingsRequest.settings(Settings.builder() - .put("index.analysis.analyzer.my_analyzer.type", "custom") - .put("index.analysis.analyzer.my_analyzer.tokenizer", "keyword") + updateSettingsRequest.settings( + Settings.builder() + .put("index.analysis.analyzer.my_analyzer.type", "custom") + .put("index.analysis.analyzer.my_analyzer.tokenizer", "keyword") ); assertAcked(leaderClient().admin().indices().updateSettings(updateSettingsRequest).actionGet()); @@ -1046,12 +1178,16 @@ public void testUpdateAnalysisLeaderIndexSettings() throws Exception { GetMappingsResponse getMappingsResponse = followerClient().admin().indices().getMappings(getMappingsRequest).actionGet(); MappingMetadata mappingMetadata = getMappingsResponse.getMappings().get("follower"); assertThat(XContentMapValues.extractValue("properties.new_field.type", mappingMetadata.sourceAsMap()), equalTo("text")); - assertThat(XContentMapValues.extractValue("properties.new_field.analyzer", mappingMetadata.sourceAsMap()), - equalTo("my_analyzer")); + assertThat( + XContentMapValues.extractValue("properties.new_field.analyzer", mappingMetadata.sourceAsMap()), + equalTo("my_analyzer") + ); try { - assertThat(followerClient().prepareSearch("follower").get().getHits().getTotalHits().value, - equalTo(firstBatchNumDocs + secondBatchNumDocs)); + assertThat( + followerClient().prepareSearch("follower").get().getHits().getTotalHits().value, + equalTo(firstBatchNumDocs + secondBatchNumDocs) + ); } catch (Exception e) { throw new AssertionError("error while searching", e); } @@ -1069,9 +1205,7 @@ public void testDoNotReplicatePrivateSettings() throws Exception { @Override public ClusterState execute(ClusterState currentState) { final IndexMetadata indexMetadata = currentState.metadata().index("leader"); - Settings.Builder settings = Settings.builder() - .put(indexMetadata.getSettings()) - .put("index.max_ngram_diff", 2); + Settings.Builder settings = Settings.builder().put(indexMetadata.getSettings()).put("index.max_ngram_diff", 2); if (randomBoolean()) { settings.put(PrivateSettingPlugin.INDEX_INTERNAL_SETTING.getKey(), "private-value"); } @@ -1079,9 +1213,13 @@ public ClusterState execute(ClusterState currentState) { settings.put(PrivateSettingPlugin.INDEX_PRIVATE_SETTING.getKey(), "interval-value"); } final Metadata.Builder metadata = Metadata.builder(currentState.metadata()) - .put(IndexMetadata.builder(indexMetadata) - .settingsVersion(indexMetadata.getSettingsVersion() + 1) - .settings(settings).build(), true); + .put( + IndexMetadata.builder(indexMetadata) + .settingsVersion(indexMetadata.getSettingsVersion() + 1) + .settings(settings) + .build(), + true + ); return ClusterState.builder(currentState).metadata(metadata).build(); } @@ -1104,9 +1242,12 @@ public void onFailure(String source, Exception e) { */ public void testReplicateDataTierPreference() throws Exception { String tier = randomFrom(DataTier.DATA_HOT, DataTier.DATA_WARM, DataTier.DATA_COLD); - assertAcked(leaderClient().admin().indices().prepareCreate("leader") - .setSource(getIndexSettings(1, 0, Map.of(DataTier.TIER_PREFERENCE, tier)), - XContentType.JSON)); + assertAcked( + leaderClient().admin() + .indices() + .prepareCreate("leader") + .setSource(getIndexSettings(1, 0, Map.of(DataTier.TIER_PREFERENCE, tier)), XContentType.JSON) + ); ensureLeaderGreen("leader"); if (randomBoolean()) { String templateTier = randomValueOtherThan(tier, () -> randomFrom(DataTier.DATA_HOT, DataTier.DATA_WARM, DataTier.DATA_COLD)); @@ -1136,9 +1277,13 @@ public ClusterState execute(ClusterState currentState) { settings.put(PrivateSettingPlugin.INDEX_PRIVATE_SETTING.getKey(), "internal-value"); settings.put(PrivateSettingPlugin.INDEX_INTERNAL_SETTING.getKey(), "internal-value"); final Metadata.Builder metadata = Metadata.builder(currentState.metadata()) - .put(IndexMetadata.builder(indexMetadata) - .settingsVersion(indexMetadata.getSettingsVersion() + 1) - .settings(settings).build(), true); + .put( + IndexMetadata.builder(indexMetadata) + .settingsVersion(indexMetadata.getSettingsVersion() + 1) + .settings(settings) + .build(), + true + ); return ClusterState.builder(currentState).metadata(metadata).build(); } @@ -1174,58 +1319,64 @@ public void testMustCloseIndexAndPauseToRestartWithPutFollowing() throws Excepti assertTrue(response.isIndexFollowingStarted()); final PutFollowAction.Request followRequest2 = putFollow("index1", "index2"); - expectThrows(SnapshotRestoreException.class, - () -> followerClient().execute(PutFollowAction.INSTANCE, followRequest2).actionGet()); + expectThrows(SnapshotRestoreException.class, () -> followerClient().execute(PutFollowAction.INSTANCE, followRequest2).actionGet()); followerClient().admin().indices().prepareClose("index2").get(); - expectThrows(ResourceAlreadyExistsException.class, - () -> followerClient().execute(PutFollowAction.INSTANCE, followRequest2).actionGet()); + expectThrows( + ResourceAlreadyExistsException.class, + () -> followerClient().execute(PutFollowAction.INSTANCE, followRequest2).actionGet() + ); } public void testIndexFallBehind() throws Exception { - runFallBehindTest( - () -> { - // we have to remove the retention leases on the leader shards to ensure the follower falls behind - final ClusterStateResponse followerIndexClusterState = - followerClient().admin().cluster().prepareState().clear().setMetadata(true).setIndices("index2").get(); - final String followerUUID = followerIndexClusterState.getState().metadata().index("index2").getIndexUUID(); - final ClusterStateResponse leaderIndexClusterState = - leaderClient().admin().cluster().prepareState().clear().setMetadata(true).setIndices("index1").get(); - final String leaderUUID = leaderIndexClusterState.getState().metadata().index("index1").getIndexUUID(); - - final RoutingTable leaderRoutingTable = leaderClient() - .admin() - .cluster() - .prepareState() - .clear() - .setIndices("index1") - .setRoutingTable(true) - .get() - .getState() - .routingTable(); - - final String retentionLeaseId = retentionLeaseId( - getFollowerCluster().getClusterName(), - new Index("index2", followerUUID), - getLeaderCluster().getClusterName(), - new Index("index1", leaderUUID)); - - for (final ObjectCursor shardRoutingTable - : leaderRoutingTable.index("index1").shards().values()) { - final ShardId shardId = shardRoutingTable.value.shardId(); - leaderClient().execute( - RetentionLeaseActions.Remove.INSTANCE, - new RetentionLeaseActions.RemoveRequest(shardId, retentionLeaseId)) - .get(); - } - }, - exceptions -> assertThat(exceptions.size(), greaterThan(0))); + runFallBehindTest(() -> { + // we have to remove the retention leases on the leader shards to ensure the follower falls behind + final ClusterStateResponse followerIndexClusterState = followerClient().admin() + .cluster() + .prepareState() + .clear() + .setMetadata(true) + .setIndices("index2") + .get(); + final String followerUUID = followerIndexClusterState.getState().metadata().index("index2").getIndexUUID(); + final ClusterStateResponse leaderIndexClusterState = leaderClient().admin() + .cluster() + .prepareState() + .clear() + .setMetadata(true) + .setIndices("index1") + .get(); + final String leaderUUID = leaderIndexClusterState.getState().metadata().index("index1").getIndexUUID(); + + final RoutingTable leaderRoutingTable = leaderClient().admin() + .cluster() + .prepareState() + .clear() + .setIndices("index1") + .setRoutingTable(true) + .get() + .getState() + .routingTable(); + + final String retentionLeaseId = retentionLeaseId( + getFollowerCluster().getClusterName(), + new Index("index2", followerUUID), + getLeaderCluster().getClusterName(), + new Index("index1", leaderUUID) + ); + + for (final ObjectCursor shardRoutingTable : leaderRoutingTable.index("index1").shards().values()) { + final ShardId shardId = shardRoutingTable.value.shardId(); + leaderClient().execute( + RetentionLeaseActions.Remove.INSTANCE, + new RetentionLeaseActions.RemoveRequest(shardId, retentionLeaseId) + ).get(); + } + }, exceptions -> assertThat(exceptions.size(), greaterThan(0))); } public void testIndexDoesNotFallBehind() throws Exception { - runFallBehindTest( - () -> {}, - exceptions -> assertThat(exceptions.size(), equalTo(0))); + runFallBehindTest(() -> {}, exceptions -> assertThat(exceptions.size(), equalTo(0))); } /** @@ -1242,8 +1393,9 @@ public void testIndexDoesNotFallBehind() throws Exception { * @throws Exception if a checked exception is thrown during the test */ private void runFallBehindTest( - final CheckedRunnable afterPausingFollower, - final Consumer> exceptionConsumer) throws Exception { + final CheckedRunnable afterPausingFollower, + final Consumer> exceptionConsumer + ) throws Exception { final int numberOfPrimaryShards = randomIntBetween(1, 3); final Map extraSettingsMap = new HashMap<>(2); extraSettingsMap.put(IndexService.RETENTION_LEASE_SYNC_INTERVAL_SETTING.getKey(), "200ms"); @@ -1284,9 +1436,14 @@ private void runFallBehindTest( final ShardStats[] shardsStats = leaderClient().admin().indices().prepareStats("index1").get().getIndex("index1").getShards(); for (final ShardStats shardStats : shardsStats) { final long maxSeqNo = shardStats.getSeqNoStats().getMaxSeqNo(); - assertTrue(shardStats.getRetentionLeaseStats().retentionLeases().leases().stream() - .filter(retentionLease -> ReplicationTracker.PEER_RECOVERY_RETENTION_LEASE_SOURCE.equals(retentionLease.source())) - .allMatch(retentionLease -> retentionLease.retainingSequenceNumber() == maxSeqNo + 1)); + assertTrue( + shardStats.getRetentionLeaseStats() + .retentionLeases() + .leases() + .stream() + .filter(retentionLease -> ReplicationTracker.PEER_RECOVERY_RETENTION_LEASE_SOURCE.equals(retentionLease.source())) + .allMatch(retentionLease -> retentionLease.retainingSequenceNumber() == maxSeqNo + 1) + ); } }); ForceMergeRequest forceMergeRequest = new ForceMergeRequest("index1"); @@ -1298,13 +1455,13 @@ private void runFallBehindTest( assertBusy(() -> { List statuses = getFollowTaskStatuses("index2"); Set exceptions = statuses.stream() - .map(ShardFollowNodeTaskStatus::getFatalException) - .filter(Objects::nonNull) - .map(ExceptionsHelper::unwrapCause) - .filter(e -> e instanceof ResourceNotFoundException) - .map(e -> (ResourceNotFoundException) e) - .filter(e -> e.getMetadataKeys().contains("es.requested_operations_missing")) - .collect(Collectors.toSet()); + .map(ShardFollowNodeTaskStatus::getFatalException) + .filter(Objects::nonNull) + .map(ExceptionsHelper::unwrapCause) + .filter(e -> e instanceof ResourceNotFoundException) + .map(e -> (ResourceNotFoundException) e) + .filter(e -> e.getMetadataKeys().contains("es.requested_operations_missing")) + .collect(Collectors.toSet()); exceptionConsumer.accept(exceptions); }); @@ -1345,13 +1502,15 @@ public void testUpdateRemoteConfigsDuringFollowing() throws Exception { assertTrue(response.isIndexFollowingStarted()); logger.info("Indexing [{}] docs while updating remote config", firstBatchNumDocs); - try (BackgroundIndexer indexer = new BackgroundIndexer("index1", "_doc", leaderClient(), firstBatchNumDocs, - randomIntBetween(1, 5))) { + try ( + BackgroundIndexer indexer = new BackgroundIndexer("index1", "_doc", leaderClient(), firstBatchNumDocs, randomIntBetween(1, 5)) + ) { ClusterUpdateSettingsRequest settingsRequest = new ClusterUpdateSettingsRequest().masterNodeTimeout(TimeValue.MAX_VALUE); String address = getLeaderCluster().getDataNodeInstance(TransportService.class).boundAddress().publishAddress().toString(); - Setting compress = - RemoteClusterService.REMOTE_CLUSTER_COMPRESS.getConcreteSettingForNamespace("leader_cluster"); + Setting compress = RemoteClusterService.REMOTE_CLUSTER_COMPRESS.getConcreteSettingForNamespace( + "leader_cluster" + ); Setting> seeds = SniffConnectionStrategy.REMOTE_CLUSTER_SEEDS.getConcreteSettingForNamespace("leader_cluster"); settingsRequest.persistentSettings(Settings.builder().put(compress.getKey(), true).put(seeds.getKey(), address)); assertAcked(followerClient().admin().cluster().updateSettings(settingsRequest).actionGet()); @@ -1360,8 +1519,12 @@ public void testUpdateRemoteConfigsDuringFollowing() throws Exception { indexer.assertNoFailures(); final Map firstBatchNumDocsPerShard = new HashMap<>(); - final ShardStats[] firstBatchShardStats = - leaderClient().admin().indices().prepareStats("index1").get().getIndex("index1").getShards(); + final ShardStats[] firstBatchShardStats = leaderClient().admin() + .indices() + .prepareStats("index1") + .get() + .getIndex("index1") + .getShards(); for (final ShardStats shardStats : firstBatchShardStats) { if (shardStats.getShardRouting().primary()) { long value = shardStats.getStats().getIndexing().getTotal().getIndexCount() - 1; @@ -1382,22 +1545,30 @@ public void testUpdateRemoteConfigsDuringFollowing() throws Exception { } finally { ClusterUpdateSettingsRequest settingsRequest = new ClusterUpdateSettingsRequest().masterNodeTimeout(TimeValue.MAX_VALUE); String address = getLeaderCluster().getDataNodeInstance(TransportService.class).boundAddress().publishAddress().toString(); - Setting compress = - RemoteClusterService.REMOTE_CLUSTER_COMPRESS.getConcreteSettingForNamespace("leader_cluster"); + Setting compress = RemoteClusterService.REMOTE_CLUSTER_COMPRESS.getConcreteSettingForNamespace( + "leader_cluster" + ); Setting> seeds = SniffConnectionStrategy.REMOTE_CLUSTER_SEEDS.getConcreteSettingForNamespace("leader_cluster"); - settingsRequest.persistentSettings(Settings.builder().put(compress.getKey(), compress.getDefault(Settings.EMPTY)) - .put(seeds.getKey(), address)); + settingsRequest.persistentSettings( + Settings.builder().put(compress.getKey(), compress.getDefault(Settings.EMPTY)).put(seeds.getKey(), address) + ); assertAcked(followerClient().admin().cluster().updateSettings(settingsRequest).actionGet()); } } public void testCleanUpShardFollowTasksForDeletedIndices() throws Exception { final int numberOfShards = randomIntBetween(1, 10); - assertAcked(leaderClient().admin().indices().prepareCreate("index1") - .setSettings(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, numberOfShards) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, randomIntBetween(0, 1)) - .build())); + assertAcked( + leaderClient().admin() + .indices() + .prepareCreate("index1") + .setSettings( + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, numberOfShards) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, randomIntBetween(0, 1)) + .build() + ) + ); final PutFollowAction.Request followRequest = putFollow("index1", "index2"); followerClient().execute(PutFollowAction.INSTANCE, followRequest).get(); @@ -1447,9 +1618,10 @@ private long getFollowTaskMappingVersion(String followerIndex) { private List getFollowTaskStatuses(String followerIndex) { FollowStatsAction.StatsRequest request = new StatsRequest(); - request.setIndices(new String[]{followerIndex}); + request.setIndices(new String[] { followerIndex }); FollowStatsAction.StatsResponses response = followerClient().execute(FollowStatsAction.INSTANCE, request).actionGet(); - return response.getStatsResponses().stream() + return response.getStatsResponses() + .stream() .map(FollowStatsAction.StatsResponse::status) .filter(status -> status.followerIndex().equals(followerIndex)) .collect(Collectors.toList()); @@ -1461,7 +1633,7 @@ private BooleanSupplier hasFollowIndexBeenClosed(String indexName) { AtomicBoolean closed = new AtomicBoolean(false); clusterService.addListener(event -> { IndexMetadata indexMetadata = event.state().metadata().index(indexName); - if (indexMetadata != null && indexMetadata.getState() == IndexMetadata.State.CLOSE) { + if (indexMetadata != null && indexMetadata.getState() == IndexMetadata.State.CLOSE) { closed.set(true); } }); @@ -1483,8 +1655,10 @@ private CheckedRunnable assertTask(final int numberOfPrimaryShards, f List taskInfos = listTasksResponse.getTasks(); assertThat(taskInfos.size(), equalTo(numberOfPrimaryShards)); - Collection> shardFollowTasks = - taskMetadata.findTasks(ShardFollowTask.NAME, Objects::nonNull); + Collection> shardFollowTasks = taskMetadata.findTasks( + ShardFollowTask.NAME, + Objects::nonNull + ); for (PersistentTasksCustomMetadata.PersistentTask shardFollowTask : shardFollowTasks) { final ShardFollowTask shardFollowTaskParams = (ShardFollowTask) shardFollowTask.getParams(); TaskInfo taskInfo = null; @@ -1498,9 +1672,11 @@ private CheckedRunnable assertTask(final int numberOfPrimaryShards, f assertThat(taskInfo, notNullValue()); ShardFollowNodeTaskStatus status = (ShardFollowNodeTaskStatus) taskInfo.getStatus(); assertThat(status, notNullValue()); - assertThat("incorrect global checkpoint " + shardFollowTaskParams, + assertThat( + "incorrect global checkpoint " + shardFollowTaskParams, status.followerGlobalCheckpoint(), - equalTo(numDocsPerShard.get(shardFollowTaskParams.getLeaderShardId()))); + equalTo(numDocsPerShard.get(shardFollowTaskParams.getLeaderShardId())) + ); } }; } @@ -1520,8 +1696,11 @@ private CheckedRunnable assertExpectedDocumentRunnable(final int key, }; } - private String getIndexSettingsWithNestedMapping(final int numberOfShards, final int numberOfReplicas, - final Map additionalIndexSettings) throws IOException { + private String getIndexSettingsWithNestedMapping( + final int numberOfShards, + final int numberOfReplicas, + final Map additionalIndexSettings + ) throws IOException { final String settings; try (XContentBuilder builder = jsonBuilder()) { builder.startObject(); @@ -1576,15 +1755,25 @@ private String getIndexSettingsWithNestedMapping(final int numberOfShards, final private void putFollowerTemplate(String setting, String settingValue) throws InterruptedException, ExecutionException { Template template = new Template(Settings.builder().put(setting, settingValue).build(), null, null); ComposableIndexTemplate cit = new ComposableIndexTemplate(List.of("follower"), template, null, null, null, null); - assertAcked(followerClient().execute(PutComposableIndexTemplateAction.INSTANCE, - new PutComposableIndexTemplateAction.Request("my-it").indexTemplate(cit)).get()); + assertAcked( + followerClient().execute( + PutComposableIndexTemplateAction.INSTANCE, + new PutComposableIndexTemplateAction.Request("my-it").indexTemplate(cit) + ).get() + ); } public static class PrivateSettingPlugin extends Plugin { - static final Setting INDEX_INTERNAL_SETTING = - Setting.simpleString("index.internal", Setting.Property.IndexScope, Setting.Property.InternalIndex); - static final Setting INDEX_PRIVATE_SETTING = - Setting.simpleString("index.private", Setting.Property.IndexScope, Setting.Property.PrivateIndex); + static final Setting INDEX_INTERNAL_SETTING = Setting.simpleString( + "index.internal", + Setting.Property.IndexScope, + Setting.Property.InternalIndex + ); + static final Setting INDEX_PRIVATE_SETTING = Setting.simpleString( + "index.private", + Setting.Property.IndexScope, + Setting.Property.PrivateIndex + ); @Override public List> getSettings() { diff --git a/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/LocalIndexFollowingIT.java b/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/LocalIndexFollowingIT.java index 37533304142f1..3bf807b88c7e1 100644 --- a/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/LocalIndexFollowingIT.java +++ b/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/LocalIndexFollowingIT.java @@ -8,15 +8,15 @@ package org.elasticsearch.xpack.ccr; import org.elasticsearch.ExceptionsHelper; -import org.elasticsearch.index.IndexingPressure; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.index.IndexingPressure; +import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.xpack.CcrSingleNodeTestCase; import org.elasticsearch.xpack.core.ccr.action.CcrStatsAction; import org.elasticsearch.xpack.core.ccr.action.FollowStatsAction; @@ -31,8 +31,8 @@ import java.util.concurrent.CountDownLatch; import java.util.stream.StreamSupport; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasSize; @@ -53,19 +53,23 @@ public void testFollowIndex() throws Exception { final PutFollowAction.Request followRequest = getPutFollowRequest("leader", "follower"); client().execute(PutFollowAction.INSTANCE, followRequest).get(); - assertBusy(() -> { - assertThat(client().prepareSearch("follower").get().getHits().getTotalHits().value, equalTo(firstBatchNumDocs)); - }); + assertBusy( + () -> { assertThat(client().prepareSearch("follower").get().getHits().getTotalHits().value, equalTo(firstBatchNumDocs)); } + ); final long secondBatchNumDocs = randomIntBetween(2, 64); for (int i = 0; i < secondBatchNumDocs; i++) { client().prepareIndex("leader").setSource("{}", XContentType.JSON).get(); } - assertBusy(() -> { - assertThat(client().prepareSearch("follower").get() - .getHits().getTotalHits().value, equalTo(firstBatchNumDocs + secondBatchNumDocs)); - }); + assertBusy( + () -> { + assertThat( + client().prepareSearch("follower").get().getHits().getTotalHits().value, + equalTo(firstBatchNumDocs + secondBatchNumDocs) + ); + } + ); PauseFollowAction.Request pauseRequest = new PauseFollowAction.Request("follower"); client().execute(PauseFollowAction.INSTANCE, pauseRequest); @@ -77,8 +81,10 @@ public void testFollowIndex() throws Exception { client().execute(ResumeFollowAction.INSTANCE, getResumeFollowRequest("follower")).get(); assertBusy(() -> { - assertThat(client().prepareSearch("follower").get().getHits().getTotalHits().value, - equalTo(firstBatchNumDocs + secondBatchNumDocs + thirdBatchNumDocs)); + assertThat( + client().prepareSearch("follower").get().getHits().getTotalHits().value, + equalTo(firstBatchNumDocs + secondBatchNumDocs + thirdBatchNumDocs) + ); }); ensureEmptyWriteBuffers(); } @@ -100,7 +106,9 @@ public void testIndexingMetricsIncremented() throws Exception { ThreadPool nodeThreadPool = getInstanceFromNode(ThreadPool.class); ThreadPool.Info writeInfo = StreamSupport.stream(nodeThreadPool.info().spliterator(), false) - .filter(i -> i.getName().equals(ThreadPool.Names.WRITE)).findAny().get(); + .filter(i -> i.getName().equals(ThreadPool.Names.WRITE)) + .findAny() + .get(); int numberOfThreads = writeInfo.getMax(); CountDownLatch threadBlockedLatch = new CountDownLatch(numberOfThreads); CountDownLatch blocker = new CountDownLatch(1); @@ -130,9 +138,9 @@ public void testIndexingMetricsIncremented() throws Exception { assertEquals(firstBatchNumDocs, indexingPressure.stats().getCurrentPrimaryOps()); }); blocker.countDown(); - assertBusy(() -> { - assertThat(client().prepareSearch("follower").get().getHits().getTotalHits().value, equalTo(firstBatchNumDocs)); - }); + assertBusy( + () -> { assertThat(client().prepareSearch("follower").get().getHits().getTotalHits().value, equalTo(firstBatchNumDocs)); } + ); ensureEmptyWriteBuffers(); } finally { if (blocker.getCount() > 0) { @@ -160,8 +168,10 @@ public void testRemoveRemoteConnection() throws Exception { client().prepareIndex("logs-20200101").setSource("{}", XContentType.JSON).get(); assertBusy(() -> { CcrStatsAction.Response response = client().execute(CcrStatsAction.INSTANCE, new CcrStatsAction.Request()).actionGet(); - assertThat(response.getAutoFollowStats().getNumberOfSuccessfulFollowIndices(), - equalTo(previousNumberOfSuccessfulFollowedIndices + 1)); + assertThat( + response.getAutoFollowStats().getNumberOfSuccessfulFollowIndices(), + equalTo(previousNumberOfSuccessfulFollowedIndices + 1) + ); assertThat(response.getFollowStats().getStatsResponses().size(), equalTo(1)); assertThat(response.getFollowStats().getStatsResponses().get(0).status().followerGlobalCheckpoint(), equalTo(0L)); }); @@ -177,11 +187,13 @@ public void testRemoveRemoteConnection() throws Exception { client().prepareIndex("logs-20200101").setSource("{}", XContentType.JSON).get(); assertBusy(() -> { CcrStatsAction.Response response = client().execute(CcrStatsAction.INSTANCE, new CcrStatsAction.Request()).actionGet(); - assertThat(response.getAutoFollowStats().getNumberOfSuccessfulFollowIndices(), - equalTo(previousNumberOfSuccessfulFollowedIndices + 2)); + assertThat( + response.getAutoFollowStats().getNumberOfSuccessfulFollowIndices(), + equalTo(previousNumberOfSuccessfulFollowedIndices + 2) + ); FollowStatsAction.StatsRequest statsRequest = new FollowStatsAction.StatsRequest(); - statsRequest.setIndices(new String[]{"copy-logs-20200101"}); + statsRequest.setIndices(new String[] { "copy-logs-20200101" }); FollowStatsAction.StatsResponses responses = client().execute(FollowStatsAction.INSTANCE, statsRequest).actionGet(); assertThat(responses.getStatsResponses().size(), equalTo(1)); assertThat(responses.getStatsResponses().get(0).status().getFatalException(), nullValue()); @@ -222,7 +234,7 @@ public void testChangeLeaderIndex() throws Exception { // index-2 should detect that the leader index has changed assertBusy(() -> { FollowStatsAction.StatsRequest statsRequest = new FollowStatsAction.StatsRequest(); - statsRequest.setIndices(new String[]{"index-2"}); + statsRequest.setIndices(new String[] { "index-2" }); FollowStatsAction.StatsResponses resp = client().execute(FollowStatsAction.INSTANCE, statsRequest).actionGet(); assertThat(resp.getStatsResponses(), hasSize(1)); FollowStatsAction.StatsResponse stats = resp.getStatsResponses().get(0); @@ -233,9 +245,11 @@ public void testChangeLeaderIndex() throws Exception { }); } - public static String getIndexSettings(final int numberOfShards, - final int numberOfReplicas, - final Map additionalIndexSettings) throws IOException { + public static String getIndexSettings( + final int numberOfShards, + final int numberOfReplicas, + final Map additionalIndexSettings + ) throws IOException { final String settings; try (XContentBuilder builder = jsonBuilder()) { builder.startObject(); diff --git a/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/PrimaryFollowerAllocationIT.java b/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/PrimaryFollowerAllocationIT.java index 16d6684a085ad..f61dc1405969b 100644 --- a/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/PrimaryFollowerAllocationIT.java +++ b/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/PrimaryFollowerAllocationIT.java @@ -18,8 +18,8 @@ import org.elasticsearch.cluster.routing.allocation.NodeAllocationResult; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.test.NodeRoles; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.CcrIntegTestCase; import org.elasticsearch.xpack.core.ccr.action.PutFollowAction; @@ -43,29 +43,48 @@ protected boolean reuseClusters() { public void testDoNotAllocateFollowerPrimaryToNodesWithoutRemoteClusterClientRole() throws Exception { final String leaderIndex = "leader-not-allow-index"; final String followerIndex = "follower-not-allow-index"; - final List dataOnlyNodes = getFollowerCluster().startNodes(between(1, 2), - NodeRoles.onlyRoles(Set.of(DiscoveryNodeRole.DATA_ROLE))); - assertAcked(leaderClient().admin().indices().prepareCreate(leaderIndex) - .setSource(getIndexSettings(between(1, 2), between(0, 1)), XContentType.JSON)); + final List dataOnlyNodes = getFollowerCluster().startNodes( + between(1, 2), + NodeRoles.onlyRoles(Set.of(DiscoveryNodeRole.DATA_ROLE)) + ); + assertAcked( + leaderClient().admin() + .indices() + .prepareCreate(leaderIndex) + .setSource(getIndexSettings(between(1, 2), between(0, 1)), XContentType.JSON) + ); final PutFollowAction.Request putFollowRequest = putFollow(leaderIndex, followerIndex); - putFollowRequest.setSettings(Settings.builder() - .put("index.routing.allocation.include._name", String.join(",", dataOnlyNodes)) - .putNull("index.routing.allocation.include._tier_preference") - .build()); + putFollowRequest.setSettings( + Settings.builder() + .put("index.routing.allocation.include._name", String.join(",", dataOnlyNodes)) + .putNull("index.routing.allocation.include._tier_preference") + .build() + ); putFollowRequest.waitForActiveShards(ActiveShardCount.ONE); putFollowRequest.timeout(TimeValue.timeValueSeconds(2)); final PutFollowAction.Response response = followerClient().execute(PutFollowAction.INSTANCE, putFollowRequest).get(); assertFalse(response.isFollowIndexShardsAcked()); assertFalse(response.isIndexFollowingStarted()); - final ClusterAllocationExplanation explanation = followerClient().admin().cluster().prepareAllocationExplain() - .setIndex(followerIndex).setShard(0).setPrimary(true).get().getExplanation(); + final ClusterAllocationExplanation explanation = followerClient().admin() + .cluster() + .prepareAllocationExplain() + .setIndex(followerIndex) + .setShard(0) + .setPrimary(true) + .get() + .getExplanation(); for (NodeAllocationResult nodeDecision : explanation.getShardAllocationDecision().getAllocateDecision().getNodeDecisions()) { assertThat(nodeDecision.getNodeDecision(), equalTo(AllocationDecision.NO)); if (dataOnlyNodes.contains(nodeDecision.getNode().getName())) { - final List decisions = nodeDecision.getCanAllocateDecision().getDecisions() - .stream().map(Object::toString).collect(Collectors.toList()); - assertThat("NO(shard is a primary follower and being bootstrapped, but node does not have the remote_cluster_client role)", - in(decisions)); + final List decisions = nodeDecision.getCanAllocateDecision() + .getDecisions() + .stream() + .map(Object::toString) + .collect(Collectors.toList()); + assertThat( + "NO(shard is a primary follower and being bootstrapped, but node does not have the remote_cluster_client role)", + in(decisions) + ); } } } @@ -73,19 +92,31 @@ public void testDoNotAllocateFollowerPrimaryToNodesWithoutRemoteClusterClientRol public void testAllocateFollowerPrimaryToNodesWithRemoteClusterClientRole() throws Exception { final String leaderIndex = "leader-allow-index"; final String followerIndex = "follower-allow-index"; - final List dataOnlyNodes = getFollowerCluster().startNodes(between(2, 3), - NodeRoles.onlyRoles(Set.of(DiscoveryNodeRole.DATA_ROLE))); - final List dataAndRemoteNodes = getFollowerCluster().startNodes(between(1, 2), - NodeRoles.onlyRoles(Set.of(DiscoveryNodeRole.DATA_ROLE, DiscoveryNodeRole.REMOTE_CLUSTER_CLIENT_ROLE))); - assertAcked(leaderClient().admin().indices().prepareCreate(leaderIndex) - .setSource(getIndexSettings(between(1, 2), between(0, 1)), XContentType.JSON)); + final List dataOnlyNodes = getFollowerCluster().startNodes( + between(2, 3), + NodeRoles.onlyRoles(Set.of(DiscoveryNodeRole.DATA_ROLE)) + ); + final List dataAndRemoteNodes = getFollowerCluster().startNodes( + between(1, 2), + NodeRoles.onlyRoles(Set.of(DiscoveryNodeRole.DATA_ROLE, DiscoveryNodeRole.REMOTE_CLUSTER_CLIENT_ROLE)) + ); + assertAcked( + leaderClient().admin() + .indices() + .prepareCreate(leaderIndex) + .setSource(getIndexSettings(between(1, 2), between(0, 1)), XContentType.JSON) + ); final PutFollowAction.Request putFollowRequest = putFollow(leaderIndex, followerIndex); - putFollowRequest.setSettings(Settings.builder() - .put("index.routing.rebalance.enable", "none") - .put("index.routing.allocation.include._name", - Stream.concat(dataOnlyNodes.stream(), dataAndRemoteNodes.stream()).collect(Collectors.joining(","))) - .putNull("index.routing.allocation.include._tier_preference") - .build()); + putFollowRequest.setSettings( + Settings.builder() + .put("index.routing.rebalance.enable", "none") + .put( + "index.routing.allocation.include._name", + Stream.concat(dataOnlyNodes.stream(), dataAndRemoteNodes.stream()).collect(Collectors.joining(",")) + ) + .putNull("index.routing.allocation.include._tier_preference") + .build() + ); final PutFollowAction.Response response = followerClient().execute(PutFollowAction.INSTANCE, putFollowRequest).get(); assertTrue(response.isFollowIndexShardsAcked()); assertTrue(response.isIndexFollowingStarted()); @@ -105,11 +136,15 @@ public void testAllocateFollowerPrimaryToNodesWithRemoteClusterClientRole() thro } }, 30, TimeUnit.SECONDS); // Follower primaries can be relocated to nodes without the remote cluster client role - followerClient().admin().indices().prepareUpdateSettings(followerIndex) + followerClient().admin() + .indices() + .prepareUpdateSettings(followerIndex) .setMasterNodeTimeout(TimeValue.MAX_VALUE) - .setSettings(Settings.builder() - .putNull("index.routing.allocation.include._tier_preference") - .put("index.routing.allocation.include._name", String.join(",", dataOnlyNodes))) + .setSettings( + Settings.builder() + .putNull("index.routing.allocation.include._tier_preference") + .put("index.routing.allocation.include._name", String.join(",", dataOnlyNodes)) + ) .get(); assertBusy(() -> { final ClusterState state = getFollowerCluster().client().admin().cluster().prepareState().get().getState(); diff --git a/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/RestartIndexFollowingIT.java b/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/RestartIndexFollowingIT.java index fc71ed2eeaee4..64ac344133841 100644 --- a/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/RestartIndexFollowingIT.java +++ b/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/RestartIndexFollowingIT.java @@ -15,10 +15,10 @@ import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.transport.RemoteConnectionInfo; import org.elasticsearch.transport.RemoteConnectionStrategy; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.CcrIntegTestCase; import org.elasticsearch.xpack.core.ccr.action.PauseFollowAction; import org.elasticsearch.xpack.core.ccr.action.PutFollowAction; @@ -72,9 +72,9 @@ public void testFollowIndex() throws Exception { leaderClient().prepareIndex("index1").setId(Integer.toString(i)).setSource(source, XContentType.JSON).get(); } - assertBusy(() -> { - assertThat(followerClient().prepareSearch("index2").get().getHits().getTotalHits().value, equalTo(firstBatchNumDocs)); - }); + assertBusy( + () -> { assertThat(followerClient().prepareSearch("index2").get().getHits().getTotalHits().value, equalTo(firstBatchNumDocs)); } + ); getFollowerCluster().fullRestart(); ensureFollowerGreen("index2"); @@ -95,16 +95,21 @@ public void testFollowIndex() throws Exception { leaderClient().prepareIndex("index1").setSource("{}", XContentType.JSON).get(); } - assertBusy(() -> assertThat( + assertBusy( + () -> assertThat( followerClient().prepareSearch("index2").get().getHits().getTotalHits().value, - equalTo(firstBatchNumDocs + secondBatchNumDocs + thirdBatchNumDocs))); + equalTo(firstBatchNumDocs + secondBatchNumDocs + thirdBatchNumDocs) + ) + ); cleanRemoteCluster(); assertAcked(followerClient().execute(PauseFollowAction.INSTANCE, new PauseFollowAction.Request("index2")).actionGet()); assertAcked(followerClient().admin().indices().prepareClose("index2")); - final ActionFuture unfollowFuture - = followerClient().execute(UnfollowAction.INSTANCE, new UnfollowAction.Request("index2")); + final ActionFuture unfollowFuture = followerClient().execute( + UnfollowAction.INSTANCE, + new UnfollowAction.Request("index2") + ); final ElasticsearchException elasticsearchException = expectThrows(ElasticsearchException.class, unfollowFuture::actionGet); assertThat(elasticsearchException.getMessage(), containsString("no such remote cluster")); assertThat(elasticsearchException.getMetadataKeys(), hasItem("es.failed_to_remove_retention_leases")); @@ -115,8 +120,7 @@ private void setupRemoteCluster() throws Exception { String address = getLeaderCluster().getMasterNodeInstance(TransportService.class).boundAddress().publishAddress().toString(); updateSettingsRequest.persistentSettings(Settings.builder().put("cluster.remote.leader_cluster.seeds", address)); assertAcked(followerClient().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); - List infos = - followerClient().execute(RemoteInfoAction.INSTANCE, new RemoteInfoRequest()).get().getInfos(); + List infos = followerClient().execute(RemoteInfoAction.INSTANCE, new RemoteInfoRequest()).get().getInfos(); assertThat(infos.size(), equalTo(1)); assertTrue(infos.get(0).isConnected()); } @@ -127,8 +131,9 @@ private void cleanRemoteCluster() throws Exception { assertAcked(followerClient().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); assertBusy(() -> { - List infos = - followerClient().execute(RemoteInfoAction.INSTANCE, new RemoteInfoRequest()).get().getInfos(); + List infos = followerClient().execute(RemoteInfoAction.INSTANCE, new RemoteInfoRequest()) + .get() + .getInfos(); assertThat(infos.size(), equalTo(0)); }); } diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/CCRInfoTransportAction.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/CCRInfoTransportAction.java index d403569d13804..82ef953748534 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/CCRInfoTransportAction.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/CCRInfoTransportAction.java @@ -31,8 +31,12 @@ public class CCRInfoTransportAction extends XPackInfoFeatureTransportAction { private final XPackLicenseState licenseState; @Inject - public CCRInfoTransportAction(TransportService transportService, ActionFilters actionFilters, - Settings settings, XPackLicenseState licenseState) { + public CCRInfoTransportAction( + TransportService transportService, + ActionFilters actionFilters, + Settings settings, + XPackLicenseState licenseState + ) { super(XPackInfoFeatureAction.CCR.name(), transportService, actionFilters); this.enabled = XPackSettings.CCR_ENABLED_SETTING.get(settings); this.licenseState = licenseState; @@ -59,11 +63,13 @@ public static class Usage extends XPackFeatureSet.Usage { private final int numberOfAutoFollowPatterns; private final Long lastFollowTimeInMillis; - public Usage(boolean available, - boolean enabled, - int numberOfFollowerIndices, - int numberOfAutoFollowPatterns, - Long lastFollowTimeInMillis) { + public Usage( + boolean available, + boolean enabled, + int numberOfFollowerIndices, + int numberOfAutoFollowPatterns, + Long lastFollowTimeInMillis + ) { super(XPackField.CCR, available, enabled); this.numberOfFollowerIndices = numberOfFollowerIndices; this.numberOfAutoFollowPatterns = numberOfAutoFollowPatterns; @@ -126,9 +132,9 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Usage usage = (Usage) o; - return numberOfFollowerIndices == usage.numberOfFollowerIndices && - numberOfAutoFollowPatterns == usage.numberOfAutoFollowPatterns && - Objects.equals(lastFollowTimeInMillis, usage.lastFollowTimeInMillis); + return numberOfFollowerIndices == usage.numberOfFollowerIndices + && numberOfAutoFollowPatterns == usage.numberOfAutoFollowPatterns + && Objects.equals(lastFollowTimeInMillis, usage.lastFollowTimeInMillis); } @Override diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/CCRUsageTransportAction.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/CCRUsageTransportAction.java index e6f943acdbeb9..283e95b000f3e 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/CCRUsageTransportAction.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/CCRUsageTransportAction.java @@ -35,18 +35,27 @@ public class CCRUsageTransportAction extends XPackUsageFeatureTransportAction { private final XPackLicenseState licenseState; @Inject - public CCRUsageTransportAction(TransportService transportService, ClusterService clusterService, ThreadPool threadPool, - ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver, - Settings settings, XPackLicenseState licenseState) { - super(XPackUsageFeatureAction.CCR.name(), transportService, clusterService, - threadPool, actionFilters, indexNameExpressionResolver); + public CCRUsageTransportAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver, + Settings settings, + XPackLicenseState licenseState + ) { + super(XPackUsageFeatureAction.CCR.name(), transportService, clusterService, threadPool, actionFilters, indexNameExpressionResolver); this.settings = settings; this.licenseState = licenseState; } @Override - protected void masterOperation(Task task, XPackUsageRequest request, ClusterState state, - ActionListener listener) { + protected void masterOperation( + Task task, + XPackUsageRequest request, + ClusterState state, + ActionListener listener + ) { Metadata metadata = state.metadata(); int numberOfFollowerIndices = 0; @@ -70,8 +79,13 @@ protected void masterOperation(Task task, XPackUsageRequest request, ClusterStat lastFollowTimeInMillis = Math.max(0, Instant.now().toEpochMilli() - lastFollowerIndexCreationDate); } - CCRInfoTransportAction.Usage usage = new CCRInfoTransportAction.Usage(CcrConstants.CCR_FEATURE.checkWithoutTracking(licenseState), - XPackSettings.CCR_ENABLED_SETTING.get(settings), numberOfFollowerIndices, numberOfAutoFollowPatterns, lastFollowTimeInMillis); + CCRInfoTransportAction.Usage usage = new CCRInfoTransportAction.Usage( + CcrConstants.CCR_FEATURE.checkWithoutTracking(licenseState), + XPackSettings.CCR_ENABLED_SETTING.get(settings), + numberOfFollowerIndices, + numberOfAutoFollowPatterns, + lastFollowTimeInMillis + ); listener.onResponse(new XPackUsageFeatureResponse(usage)); } } diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/Ccr.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/Ccr.java index 7790886cad569..84edecd418852 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/Ccr.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/Ccr.java @@ -19,7 +19,6 @@ import org.elasticsearch.cluster.node.DiscoveryNodes; import org.elasticsearch.cluster.routing.allocation.decider.AllocationDecider; import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.settings.ClusterSettings; import org.elasticsearch.common.settings.IndexScopedSettings; @@ -27,7 +26,6 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsFilter; import org.elasticsearch.common.settings.SettingsModule; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.env.Environment; import org.elasticsearch.env.NodeEnvironment; import org.elasticsearch.index.IndexModule; @@ -53,21 +51,20 @@ import org.elasticsearch.threadpool.FixedExecutorBuilder; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.watcher.ResourceWatcherService; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xpack.ccr.action.AutoFollowCoordinator; import org.elasticsearch.xpack.ccr.action.CcrRequests; import org.elasticsearch.xpack.ccr.action.ShardChangesAction; -import org.elasticsearch.xpack.core.XPackFeatureSet; -import org.elasticsearch.xpack.core.XPackField; -import org.elasticsearch.xpack.core.ccr.action.ShardFollowTask; import org.elasticsearch.xpack.ccr.action.ShardFollowTaskCleaner; import org.elasticsearch.xpack.ccr.action.ShardFollowTasksExecutor; +import org.elasticsearch.xpack.ccr.action.TransportActivateAutoFollowPatternAction; import org.elasticsearch.xpack.ccr.action.TransportCcrStatsAction; import org.elasticsearch.xpack.ccr.action.TransportDeleteAutoFollowPatternAction; import org.elasticsearch.xpack.ccr.action.TransportFollowInfoAction; import org.elasticsearch.xpack.ccr.action.TransportFollowStatsAction; import org.elasticsearch.xpack.ccr.action.TransportForgetFollowerAction; import org.elasticsearch.xpack.ccr.action.TransportGetAutoFollowPatternAction; -import org.elasticsearch.xpack.ccr.action.TransportActivateAutoFollowPatternAction; import org.elasticsearch.xpack.ccr.action.TransportPauseFollowAction; import org.elasticsearch.xpack.ccr.action.TransportPutAutoFollowPatternAction; import org.elasticsearch.xpack.ccr.action.TransportPutFollowAction; @@ -97,22 +94,25 @@ import org.elasticsearch.xpack.ccr.rest.RestResumeAutoFollowPatternAction; import org.elasticsearch.xpack.ccr.rest.RestResumeFollowAction; import org.elasticsearch.xpack.ccr.rest.RestUnfollowAction; +import org.elasticsearch.xpack.core.XPackFeatureSet; +import org.elasticsearch.xpack.core.XPackField; import org.elasticsearch.xpack.core.XPackPlugin; import org.elasticsearch.xpack.core.action.XPackInfoFeatureAction; import org.elasticsearch.xpack.core.action.XPackUsageFeatureAction; import org.elasticsearch.xpack.core.ccr.AutoFollowMetadata; import org.elasticsearch.xpack.core.ccr.ShardFollowNodeTaskStatus; +import org.elasticsearch.xpack.core.ccr.action.ActivateAutoFollowPatternAction; import org.elasticsearch.xpack.core.ccr.action.CcrStatsAction; import org.elasticsearch.xpack.core.ccr.action.DeleteAutoFollowPatternAction; import org.elasticsearch.xpack.core.ccr.action.FollowInfoAction; import org.elasticsearch.xpack.core.ccr.action.FollowStatsAction; import org.elasticsearch.xpack.core.ccr.action.ForgetFollowerAction; import org.elasticsearch.xpack.core.ccr.action.GetAutoFollowPatternAction; -import org.elasticsearch.xpack.core.ccr.action.ActivateAutoFollowPatternAction; import org.elasticsearch.xpack.core.ccr.action.PauseFollowAction; import org.elasticsearch.xpack.core.ccr.action.PutAutoFollowPatternAction; import org.elasticsearch.xpack.core.ccr.action.PutFollowAction; import org.elasticsearch.xpack.core.ccr.action.ResumeFollowAction; +import org.elasticsearch.xpack.core.ccr.action.ShardFollowTask; import org.elasticsearch.xpack.core.ccr.action.UnfollowAction; import java.util.Arrays; @@ -173,17 +173,18 @@ public Ccr(final Settings settings) { @Override public Collection createComponents( - final Client client, - final ClusterService clusterService, - final ThreadPool threadPool, - final ResourceWatcherService resourceWatcherService, - final ScriptService scriptService, - final NamedXContentRegistry xContentRegistry, - final Environment environment, - final NodeEnvironment nodeEnvironment, - final NamedWriteableRegistry namedWriteableRegistry, - final IndexNameExpressionResolver expressionResolver, - final Supplier repositoriesServiceSupplier) { + final Client client, + final ClusterService clusterService, + final ThreadPool threadPool, + final ResourceWatcherService resourceWatcherService, + final ScriptService scriptService, + final NamedXContentRegistry xContentRegistry, + final Environment environment, + final NodeEnvironment nodeEnvironment, + final NamedWriteableRegistry namedWriteableRegistry, + final IndexNameExpressionResolver expressionResolver, + final Supplier repositoriesServiceSupplier + ) { this.client = client; if (enabled == false) { return emptyList(); @@ -205,15 +206,19 @@ public Collection createComponents( ccrLicenseChecker, threadPool::relativeTimeInMillis, threadPool::absoluteTimeInMillis, - threadPool.executor(Ccr.CCR_THREAD_POOL_NAME))); + threadPool.executor(Ccr.CCR_THREAD_POOL_NAME) + ) + ); } @Override - public List> getPersistentTasksExecutor(ClusterService clusterService, - ThreadPool threadPool, - Client client, - SettingsModule settingsModule, - IndexNameExpressionResolver expressionResolver) { + public List> getPersistentTasksExecutor( + ClusterService clusterService, + ThreadPool threadPool, + Client client, + SettingsModule settingsModule, + IndexNameExpressionResolver expressionResolver + ) { return Collections.singletonList(new ShardFollowTasksExecutor(client, threadPool, clusterService, settingsModule)); } @@ -225,99 +230,119 @@ public List> getPersistentTasksExecutor(ClusterServic } return Arrays.asList( - // internal actions - new ActionHandler<>(BulkShardOperationsAction.INSTANCE, TransportBulkShardOperationsAction.class), - new ActionHandler<>(ShardChangesAction.INSTANCE, ShardChangesAction.TransportAction.class), - new ActionHandler<>(PutInternalCcrRepositoryAction.INSTANCE, - PutInternalCcrRepositoryAction.TransportPutInternalRepositoryAction.class), - new ActionHandler<>(DeleteInternalCcrRepositoryAction.INSTANCE, - DeleteInternalCcrRepositoryAction.TransportDeleteInternalRepositoryAction.class), - new ActionHandler<>(PutCcrRestoreSessionAction.INSTANCE, - PutCcrRestoreSessionAction.TransportPutCcrRestoreSessionAction.class), - new ActionHandler<>(ClearCcrRestoreSessionAction.INSTANCE, - ClearCcrRestoreSessionAction.TransportDeleteCcrRestoreSessionAction.class), - new ActionHandler<>(GetCcrRestoreFileChunkAction.INSTANCE, - GetCcrRestoreFileChunkAction.TransportGetCcrRestoreFileChunkAction.class), - // stats action - new ActionHandler<>(FollowStatsAction.INSTANCE, TransportFollowStatsAction.class), - new ActionHandler<>(CcrStatsAction.INSTANCE, TransportCcrStatsAction.class), - new ActionHandler<>(FollowInfoAction.INSTANCE, TransportFollowInfoAction.class), - // follow actions - new ActionHandler<>(PutFollowAction.INSTANCE, TransportPutFollowAction.class), - new ActionHandler<>(ResumeFollowAction.INSTANCE, TransportResumeFollowAction.class), - new ActionHandler<>(PauseFollowAction.INSTANCE, TransportPauseFollowAction.class), - new ActionHandler<>(UnfollowAction.INSTANCE, TransportUnfollowAction.class), - // auto-follow actions - new ActionHandler<>(DeleteAutoFollowPatternAction.INSTANCE, TransportDeleteAutoFollowPatternAction.class), - new ActionHandler<>(PutAutoFollowPatternAction.INSTANCE, TransportPutAutoFollowPatternAction.class), - new ActionHandler<>(GetAutoFollowPatternAction.INSTANCE, TransportGetAutoFollowPatternAction.class), - new ActionHandler<>(ActivateAutoFollowPatternAction.INSTANCE, TransportActivateAutoFollowPatternAction.class), - // forget follower action - new ActionHandler<>(ForgetFollowerAction.INSTANCE, TransportForgetFollowerAction.class), - usageAction, - infoAction); + // internal actions + new ActionHandler<>(BulkShardOperationsAction.INSTANCE, TransportBulkShardOperationsAction.class), + new ActionHandler<>(ShardChangesAction.INSTANCE, ShardChangesAction.TransportAction.class), + new ActionHandler<>( + PutInternalCcrRepositoryAction.INSTANCE, + PutInternalCcrRepositoryAction.TransportPutInternalRepositoryAction.class + ), + new ActionHandler<>( + DeleteInternalCcrRepositoryAction.INSTANCE, + DeleteInternalCcrRepositoryAction.TransportDeleteInternalRepositoryAction.class + ), + new ActionHandler<>(PutCcrRestoreSessionAction.INSTANCE, PutCcrRestoreSessionAction.TransportPutCcrRestoreSessionAction.class), + new ActionHandler<>( + ClearCcrRestoreSessionAction.INSTANCE, + ClearCcrRestoreSessionAction.TransportDeleteCcrRestoreSessionAction.class + ), + new ActionHandler<>( + GetCcrRestoreFileChunkAction.INSTANCE, + GetCcrRestoreFileChunkAction.TransportGetCcrRestoreFileChunkAction.class + ), + // stats action + new ActionHandler<>(FollowStatsAction.INSTANCE, TransportFollowStatsAction.class), + new ActionHandler<>(CcrStatsAction.INSTANCE, TransportCcrStatsAction.class), + new ActionHandler<>(FollowInfoAction.INSTANCE, TransportFollowInfoAction.class), + // follow actions + new ActionHandler<>(PutFollowAction.INSTANCE, TransportPutFollowAction.class), + new ActionHandler<>(ResumeFollowAction.INSTANCE, TransportResumeFollowAction.class), + new ActionHandler<>(PauseFollowAction.INSTANCE, TransportPauseFollowAction.class), + new ActionHandler<>(UnfollowAction.INSTANCE, TransportUnfollowAction.class), + // auto-follow actions + new ActionHandler<>(DeleteAutoFollowPatternAction.INSTANCE, TransportDeleteAutoFollowPatternAction.class), + new ActionHandler<>(PutAutoFollowPatternAction.INSTANCE, TransportPutAutoFollowPatternAction.class), + new ActionHandler<>(GetAutoFollowPatternAction.INSTANCE, TransportGetAutoFollowPatternAction.class), + new ActionHandler<>(ActivateAutoFollowPatternAction.INSTANCE, TransportActivateAutoFollowPatternAction.class), + // forget follower action + new ActionHandler<>(ForgetFollowerAction.INSTANCE, TransportForgetFollowerAction.class), + usageAction, + infoAction + ); } - public List getRestHandlers(Settings settings, RestController restController, ClusterSettings clusterSettings, - IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter, - IndexNameExpressionResolver indexNameExpressionResolver, - Supplier nodesInCluster) { + public List getRestHandlers( + Settings settings, + RestController restController, + ClusterSettings clusterSettings, + IndexScopedSettings indexScopedSettings, + SettingsFilter settingsFilter, + IndexNameExpressionResolver indexNameExpressionResolver, + Supplier nodesInCluster + ) { if (enabled == false) { return emptyList(); } return Arrays.asList( - // stats API - new RestFollowStatsAction(), - new RestCcrStatsAction(), - new RestFollowInfoAction(), - // follow APIs - new RestPutFollowAction(), - new RestResumeFollowAction(), - new RestPauseFollowAction(), - new RestUnfollowAction(), - // auto-follow APIs - new RestDeleteAutoFollowPatternAction(), - new RestPutAutoFollowPatternAction(), - new RestGetAutoFollowPatternAction(), - new RestPauseAutoFollowPatternAction(), - new RestResumeAutoFollowPatternAction(), - // forget follower API - new RestForgetFollowerAction()); + // stats API + new RestFollowStatsAction(), + new RestCcrStatsAction(), + new RestFollowInfoAction(), + // follow APIs + new RestPutFollowAction(), + new RestResumeFollowAction(), + new RestPauseFollowAction(), + new RestUnfollowAction(), + // auto-follow APIs + new RestDeleteAutoFollowPatternAction(), + new RestPutAutoFollowPatternAction(), + new RestGetAutoFollowPatternAction(), + new RestPauseAutoFollowPatternAction(), + new RestResumeAutoFollowPatternAction(), + // forget follower API + new RestForgetFollowerAction() + ); } public List getNamedWriteables() { return Arrays.asList( - // Persistent action requests - new NamedWriteableRegistry.Entry(PersistentTaskParams.class, ShardFollowTask.NAME, - ShardFollowTask::readFrom), + // Persistent action requests + new NamedWriteableRegistry.Entry(PersistentTaskParams.class, ShardFollowTask.NAME, ShardFollowTask::readFrom), - // Task statuses - new NamedWriteableRegistry.Entry(Task.Status.class, ShardFollowNodeTaskStatus.STATUS_PARSER_NAME, - ShardFollowNodeTaskStatus::new), + // Task statuses + new NamedWriteableRegistry.Entry( + Task.Status.class, + ShardFollowNodeTaskStatus.STATUS_PARSER_NAME, + ShardFollowNodeTaskStatus::new + ), - // usage api - new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.CCR, CCRInfoTransportAction.Usage::new) + // usage api + new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.CCR, CCRInfoTransportAction.Usage::new) ); } public List getNamedXContent() { return Arrays.asList( - // auto-follow metadata, persisted into the cluster state as XContent - new NamedXContentRegistry.Entry( - Metadata.Custom.class, - new ParseField(AutoFollowMetadata.TYPE), - AutoFollowMetadata::fromXContent), - // persistent action requests - new NamedXContentRegistry.Entry( - PersistentTaskParams.class, - new ParseField(ShardFollowTask.NAME), - ShardFollowTask::fromXContent), - // task statuses - new NamedXContentRegistry.Entry( - ShardFollowNodeTaskStatus.class, - new ParseField(ShardFollowNodeTaskStatus.STATUS_PARSER_NAME), - ShardFollowNodeTaskStatus::fromXContent)); + // auto-follow metadata, persisted into the cluster state as XContent + new NamedXContentRegistry.Entry( + Metadata.Custom.class, + new ParseField(AutoFollowMetadata.TYPE), + AutoFollowMetadata::fromXContent + ), + // persistent action requests + new NamedXContentRegistry.Entry( + PersistentTaskParams.class, + new ParseField(ShardFollowTask.NAME), + ShardFollowTask::fromXContent + ), + // task statuses + new NamedXContentRegistry.Entry( + ShardFollowNodeTaskStatus.class, + new ParseField(ShardFollowNodeTaskStatus.STATUS_PARSER_NAME), + ShardFollowNodeTaskStatus::fromXContent + ) + ); } /** @@ -349,15 +374,25 @@ public List> getExecutorBuilders(Settings settings) { } return Collections.singletonList( - new FixedExecutorBuilder(settings, CCR_THREAD_POOL_NAME, 32, 100, "xpack.ccr.ccr_thread_pool", false)); + new FixedExecutorBuilder(settings, CCR_THREAD_POOL_NAME, 32, 100, "xpack.ccr.ccr_thread_pool", false) + ); } @Override - public Map getInternalRepositories(Environment env, NamedXContentRegistry namedXContentRegistry, - ClusterService clusterService, RecoverySettings recoverySettings) { - Repository.Factory repositoryFactory = - (metadata) -> new CcrRepository(metadata, client, ccrLicenseChecker, settings, ccrSettings.get(), - clusterService.getClusterApplierService().threadPool()); + public Map getInternalRepositories( + Environment env, + NamedXContentRegistry namedXContentRegistry, + ClusterService clusterService, + RecoverySettings recoverySettings + ) { + Repository.Factory repositoryFactory = (metadata) -> new CcrRepository( + metadata, + client, + ccrLicenseChecker, + settings, + ccrSettings.get(), + clusterService.getClusterApplierService().threadPool() + ); return Collections.singletonMap(CcrRepository.TYPE, repositoryFactory); } @@ -368,7 +403,9 @@ public void onIndexModule(IndexModule indexModule) { } } - protected XPackLicenseState getLicenseState() { return XPackPlugin.getSharedLicenseState(); } + protected XPackLicenseState getLicenseState() { + return XPackPlugin.getSharedLicenseState(); + } @Override public Collection> mappingRequestValidators() { diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/CcrLicenseChecker.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/CcrLicenseChecker.java index 5df89f5999752..f4b210ab0fc99 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/CcrLicenseChecker.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/CcrLicenseChecker.java @@ -77,8 +77,7 @@ public class CcrLicenseChecker { * Constructs a CCR license checker with the default rule based on the license state for checking if CCR is allowed. */ CcrLicenseChecker(Settings settings) { - this(() -> CcrConstants.CCR_FEATURE.check(XPackPlugin.getSharedLicenseState()), - () -> XPackSettings.SECURITY_ENABLED.get(settings)); + this(() -> CcrConstants.CCR_FEATURE.check(XPackPlugin.getSharedLicenseState()), () -> XPackSettings.SECURITY_ENABLED.get(settings)); } /** @@ -118,56 +117,65 @@ public void checkRemoteClusterLicenseAndFetchLeaderIndexMetadataAndHistoryUUIDs( final String clusterAlias, final String leaderIndex, final Consumer onFailure, - final BiConsumer> consumer) { + final BiConsumer> consumer + ) { final ClusterStateRequest request = new ClusterStateRequest(); request.clear(); request.metadata(true); request.indices(leaderIndex); checkRemoteClusterLicenseAndFetchClusterState( - client, - clusterAlias, - client.getRemoteClusterClient(clusterAlias), - request, - onFailure, - remoteClusterStateResponse -> { - ClusterState remoteClusterState = remoteClusterStateResponse.getState(); - final IndexMetadata leaderIndexMetadata = remoteClusterState.getMetadata().index(leaderIndex); - if (leaderIndexMetadata == null) { - final IndexAbstraction indexAbstraction = remoteClusterState.getMetadata().getIndicesLookup().get(leaderIndex); - final Exception failure; - if (indexAbstraction == null) { - failure = new IndexNotFoundException(leaderIndex); - } else { - // provided name may be an alias or data stream and in that case throw a specific error: - String message = String.format(Locale.ROOT, - "cannot follow [%s], because it is a %s", - leaderIndex, indexAbstraction.getType() - ); - failure = new IllegalArgumentException(message); - } - onFailure.accept(failure); - return; + client, + clusterAlias, + client.getRemoteClusterClient(clusterAlias), + request, + onFailure, + remoteClusterStateResponse -> { + ClusterState remoteClusterState = remoteClusterStateResponse.getState(); + final IndexMetadata leaderIndexMetadata = remoteClusterState.getMetadata().index(leaderIndex); + if (leaderIndexMetadata == null) { + final IndexAbstraction indexAbstraction = remoteClusterState.getMetadata().getIndicesLookup().get(leaderIndex); + final Exception failure; + if (indexAbstraction == null) { + failure = new IndexNotFoundException(leaderIndex); + } else { + // provided name may be an alias or data stream and in that case throw a specific error: + String message = String.format( + Locale.ROOT, + "cannot follow [%s], because it is a %s", + leaderIndex, + indexAbstraction.getType() + ); + failure = new IllegalArgumentException(message); } - if (leaderIndexMetadata.getState() == IndexMetadata.State.CLOSE) { - onFailure.accept(new IndexClosedException(leaderIndexMetadata.getIndex())); - return; + onFailure.accept(failure); + return; + } + if (leaderIndexMetadata.getState() == IndexMetadata.State.CLOSE) { + onFailure.accept(new IndexClosedException(leaderIndexMetadata.getIndex())); + return; + } + IndexAbstraction indexAbstraction = remoteClusterState.getMetadata().getIndicesLookup().get(leaderIndex); + final DataStream remoteDataStream = indexAbstraction.getParentDataStream() != null + ? indexAbstraction.getParentDataStream().getDataStream() + : null; + final Client remoteClient = client.getRemoteClusterClient(clusterAlias); + hasPrivilegesToFollowIndices(remoteClient, new String[] { leaderIndex }, e -> { + if (e == null) { + fetchLeaderHistoryUUIDs( + remoteClient, + leaderIndexMetadata, + onFailure, + historyUUIDs -> consumer.accept(historyUUIDs, Tuple.tuple(leaderIndexMetadata, remoteDataStream)) + ); + } else { + onFailure.accept(e); } - IndexAbstraction indexAbstraction = remoteClusterState.getMetadata().getIndicesLookup().get(leaderIndex); - final DataStream remoteDataStream = indexAbstraction.getParentDataStream() != null ? - indexAbstraction.getParentDataStream().getDataStream() : null; - final Client remoteClient = client.getRemoteClusterClient(clusterAlias); - hasPrivilegesToFollowIndices(remoteClient, new String[] {leaderIndex}, e -> { - if (e == null) { - fetchLeaderHistoryUUIDs(remoteClient, leaderIndexMetadata, onFailure, historyUUIDs -> - consumer.accept(historyUUIDs, Tuple.tuple(leaderIndexMetadata, remoteDataStream))); - } else { - onFailure.accept(e); - } - }); - }, - licenseCheck -> indexMetadataNonCompliantRemoteLicense(leaderIndex, licenseCheck), - e -> indexMetadataUnknownRemoteLicense(leaderIndex, clusterAlias, e)); + }); + }, + licenseCheck -> indexMetadataNonCompliantRemoteLicense(leaderIndex, licenseCheck), + e -> indexMetadataUnknownRemoteLicense(leaderIndex, clusterAlias, e) + ); } /** @@ -183,11 +191,12 @@ public void checkRemoteClusterLicenseAndFetchLeaderIndexMetadataAndHistoryUUIDs( * @param leaderClusterStateConsumer the leader cluster state consumer */ public void checkRemoteClusterLicenseAndFetchClusterState( - final Client client, - final String clusterAlias, - final ClusterStateRequest request, - final Consumer onFailure, - final Consumer leaderClusterStateConsumer) { + final Client client, + final String clusterAlias, + final ClusterStateRequest request, + final Consumer onFailure, + final Consumer leaderClusterStateConsumer + ) { try { Client remoteClient = systemClient(client.getRemoteClusterClient(clusterAlias)); checkRemoteClusterLicenseAndFetchClusterState( @@ -198,7 +207,8 @@ public void checkRemoteClusterLicenseAndFetchClusterState( onFailure, leaderClusterStateConsumer, CcrLicenseChecker::clusterStateNonCompliantRemoteLicense, - e -> clusterStateUnknownRemoteLicense(clusterAlias, e)); + e -> clusterStateUnknownRemoteLicense(clusterAlias, e) + ); } catch (Exception e) { // client.getRemoteClusterClient(...) can fail with a IllegalArgumentException if remote // connection is unknown @@ -222,37 +232,41 @@ public void checkRemoteClusterLicenseAndFetchClusterState( * @param unknownLicense the supplier for when the license state of the remote cluster is unknown due to failure */ private void checkRemoteClusterLicenseAndFetchClusterState( - final Client client, - final String clusterAlias, - final Client remoteClient, - final ClusterStateRequest request, - final Consumer onFailure, - final Consumer leaderClusterStateConsumer, - final Function nonCompliantLicense, - final Function unknownLicense) { + final Client client, + final String clusterAlias, + final Client remoteClient, + final ClusterStateRequest request, + final Consumer onFailure, + final Consumer leaderClusterStateConsumer, + final Function nonCompliantLicense, + final Function unknownLicense + ) { // we have to check the license on the remote cluster new RemoteClusterLicenseChecker(client, XPackLicenseState::isCcrAllowedForOperationMode).checkRemoteClusterLicenses( - Collections.singletonList(clusterAlias), - new ActionListener() { - - @Override - public void onResponse(final RemoteClusterLicenseChecker.LicenseCheck licenseCheck) { - if (licenseCheck.isSuccess()) { - final ActionListener clusterStateListener = - ActionListener.wrap(leaderClusterStateConsumer::accept, onFailure); - // following an index in remote cluster, so use remote client to fetch leader index metadata - remoteClient.admin().cluster().state(request, clusterStateListener); - } else { - onFailure.accept(nonCompliantLicense.apply(licenseCheck)); - } - } + Collections.singletonList(clusterAlias), + new ActionListener() { - @Override - public void onFailure(final Exception e) { - onFailure.accept(unknownLicense.apply(e)); + @Override + public void onResponse(final RemoteClusterLicenseChecker.LicenseCheck licenseCheck) { + if (licenseCheck.isSuccess()) { + final ActionListener clusterStateListener = ActionListener.wrap( + leaderClusterStateConsumer::accept, + onFailure + ); + // following an index in remote cluster, so use remote client to fetch leader index metadata + remoteClient.admin().cluster().state(request, clusterStateListener); + } else { + onFailure.accept(nonCompliantLicense.apply(licenseCheck)); } + } - }); + @Override + public void onFailure(final Exception e) { + onFailure.accept(unknownLicense.apply(e)); + } + + } + ); } /** @@ -269,7 +283,8 @@ public void fetchLeaderHistoryUUIDs( final Client remoteClient, final IndexMetadata leaderIndexMetadata, final Consumer onFailure, - final Consumer historyUUIDConsumer) { + final Consumer historyUUIDConsumer + ) { String leaderIndex = leaderIndexMetadata.getIndex().getName(); CheckedConsumer indicesStatsHandler = indicesStatsResponse -> { @@ -389,8 +404,11 @@ public static Client wrapClient(Client client, Map headers) { } return new FilterClient(client) { @Override - protected - void doExecute(ActionType action, Request request, ActionListener listener) { + protected void doExecute( + ActionType action, + Request request, + ActionListener listener + ) { ClientHelper.executeWithHeadersAsync(filteredHeaders, null, client, action, request, listener); } }; @@ -401,8 +419,11 @@ private static Client systemClient(Client client) { final ThreadContext threadContext = client.threadPool().getThreadContext(); return new FilterClient(client) { @Override - protected - void doExecute(ActionType action, Request request, ActionListener listener) { + protected void doExecute( + ActionType action, + Request request, + ActionListener listener + ) { final Supplier supplier = threadContext.newRestorableContext(false); try (ThreadContext.StoredContext ignore = threadContext.stashContext()) { threadContext.markAsSystemContext(); @@ -419,50 +440,63 @@ private static ThreadContext.StoredContext stashWithHeaders(ThreadContext thread } private static ElasticsearchStatusException indexMetadataNonCompliantRemoteLicense( - final String leaderIndex, final RemoteClusterLicenseChecker.LicenseCheck licenseCheck) { + final String leaderIndex, + final RemoteClusterLicenseChecker.LicenseCheck licenseCheck + ) { final String clusterAlias = licenseCheck.remoteClusterLicenseInfo().clusterAlias(); final String message = String.format( - Locale.ROOT, - "can not fetch remote index [%s:%s] metadata as the remote cluster [%s] is not licensed for [ccr]; %s", - clusterAlias, - leaderIndex, - clusterAlias, - RemoteClusterLicenseChecker.buildErrorMessage( - "ccr", - licenseCheck.remoteClusterLicenseInfo(), - RemoteClusterLicenseChecker::isAllowedByLicense)); + Locale.ROOT, + "can not fetch remote index [%s:%s] metadata as the remote cluster [%s] is not licensed for [ccr]; %s", + clusterAlias, + leaderIndex, + clusterAlias, + RemoteClusterLicenseChecker.buildErrorMessage( + "ccr", + licenseCheck.remoteClusterLicenseInfo(), + RemoteClusterLicenseChecker::isAllowedByLicense + ) + ); return new ElasticsearchStatusException(message, RestStatus.BAD_REQUEST); } private static ElasticsearchStatusException clusterStateNonCompliantRemoteLicense( - final RemoteClusterLicenseChecker.LicenseCheck licenseCheck) { + final RemoteClusterLicenseChecker.LicenseCheck licenseCheck + ) { final String clusterAlias = licenseCheck.remoteClusterLicenseInfo().clusterAlias(); final String message = String.format( - Locale.ROOT, - "can not fetch remote cluster state as the remote cluster [%s] is not licensed for [ccr]; %s", - clusterAlias, - RemoteClusterLicenseChecker.buildErrorMessage( - "ccr", - licenseCheck.remoteClusterLicenseInfo(), - RemoteClusterLicenseChecker::isAllowedByLicense)); + Locale.ROOT, + "can not fetch remote cluster state as the remote cluster [%s] is not licensed for [ccr]; %s", + clusterAlias, + RemoteClusterLicenseChecker.buildErrorMessage( + "ccr", + licenseCheck.remoteClusterLicenseInfo(), + RemoteClusterLicenseChecker::isAllowedByLicense + ) + ); return new ElasticsearchStatusException(message, RestStatus.BAD_REQUEST); } private static ElasticsearchStatusException indexMetadataUnknownRemoteLicense( - final String leaderIndex, final String clusterAlias, final Exception cause) { + final String leaderIndex, + final String clusterAlias, + final Exception cause + ) { final String message = String.format( - Locale.ROOT, - "can not fetch remote index [%s:%s] metadata as the license state of the remote cluster [%s] could not be determined", - clusterAlias, - leaderIndex, - clusterAlias); + Locale.ROOT, + "can not fetch remote index [%s:%s] metadata as the license state of the remote cluster [%s] could not be determined", + clusterAlias, + leaderIndex, + clusterAlias + ); return new ElasticsearchStatusException(message, RestStatus.BAD_REQUEST, cause); } private static ElasticsearchStatusException clusterStateUnknownRemoteLicense(final String clusterAlias, final Exception cause) { final String message = String.format( - Locale.ROOT, - "can not fetch remote cluster state as the license state of the remote cluster [%s] could not be determined", clusterAlias); + Locale.ROOT, + "can not fetch remote cluster state as the license state of the remote cluster [%s] could not be determined", + clusterAlias + ); return new ElasticsearchStatusException(message, RestStatus.BAD_REQUEST, cause); } diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/CcrRepositoryManager.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/CcrRepositoryManager.java index 18e6b1e88de27..3714ae703536c 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/CcrRepositoryManager.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/CcrRepositoryManager.java @@ -42,12 +42,10 @@ protected void doStart() { } @Override - protected void doStop() { - } + protected void doStop() {} @Override - protected void doClose() throws IOException { - } + protected void doClose() throws IOException {} private void putRepository(String repositoryName) { ActionRequest request = new PutInternalCcrRepositoryRequest(repositoryName, CcrRepository.TYPE); diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/CcrRetentionLeases.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/CcrRetentionLeases.java index 0b6ab251bfc69..267b7f5d4cd92 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/CcrRetentionLeases.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/CcrRetentionLeases.java @@ -26,12 +26,12 @@ public class CcrRetentionLeases { // this setting is intentionally not registered, it is only used in tests - public static final Setting RETENTION_LEASE_RENEW_INTERVAL_SETTING = - Setting.timeSetting( - "index.ccr.retention_lease.renew_interval", - new TimeValue(30, TimeUnit.SECONDS), - new TimeValue(0, TimeUnit.MILLISECONDS), - Setting.Property.NodeScope); + public static final Setting RETENTION_LEASE_RENEW_INTERVAL_SETTING = Setting.timeSetting( + "index.ccr.retention_lease.renew_interval", + new TimeValue(30, TimeUnit.SECONDS), + new TimeValue(0, TimeUnit.MILLISECONDS), + Setting.Property.NodeScope + ); /** * The retention lease ID used by followers. @@ -43,19 +43,21 @@ public class CcrRetentionLeases { * @return the retention lease ID */ public static String retentionLeaseId( - final String localClusterName, - final Index followerIndex, - final String remoteClusterAlias, - final Index leaderIndex) { + final String localClusterName, + final Index followerIndex, + final String remoteClusterAlias, + final Index leaderIndex + ) { return String.format( - Locale.ROOT, - "%s/%s/%s-following-%s/%s/%s", - localClusterName, - followerIndex.getName(), - followerIndex.getUUID(), - remoteClusterAlias, - leaderIndex.getName(), - leaderIndex.getUUID()); + Locale.ROOT, + "%s/%s/%s-following-%s/%s/%s", + localClusterName, + followerIndex.getName(), + followerIndex.getUUID(), + remoteClusterAlias, + leaderIndex.getName(), + leaderIndex.getUUID() + ); } /** @@ -70,11 +72,12 @@ public static String retentionLeaseId( * @return an optional exception indicating whether or not the retention lease already exists */ public static Optional syncAddRetentionLease( - final ShardId leaderShardId, - final String retentionLeaseId, - final long retainingSequenceNumber, - final Client remoteClient, - final TimeValue timeout) { + final ShardId leaderShardId, + final String retentionLeaseId, + final long retainingSequenceNumber, + final Client remoteClient, + final TimeValue timeout + ) { try { final PlainActionFuture response = new PlainActionFuture<>(); asyncAddRetentionLease(leaderShardId, retentionLeaseId, retainingSequenceNumber, remoteClient, response); @@ -97,13 +100,18 @@ public static Optional syncAddRetentionLea * @param listener the listener */ public static void asyncAddRetentionLease( - final ShardId leaderShardId, - final String retentionLeaseId, - final long retainingSequenceNumber, - final Client remoteClient, - final ActionListener listener) { - final RetentionLeaseActions.AddRequest request = - new RetentionLeaseActions.AddRequest(leaderShardId, retentionLeaseId, retainingSequenceNumber, "ccr"); + final ShardId leaderShardId, + final String retentionLeaseId, + final long retainingSequenceNumber, + final Client remoteClient, + final ActionListener listener + ) { + final RetentionLeaseActions.AddRequest request = new RetentionLeaseActions.AddRequest( + leaderShardId, + retentionLeaseId, + retainingSequenceNumber, + "ccr" + ); remoteClient.execute(RetentionLeaseActions.Add.INSTANCE, request, listener); } @@ -119,11 +127,12 @@ public static void asyncAddRetentionLease( * @return an optional exception indicating whether or not the retention lease already exists */ public static Optional syncRenewRetentionLease( - final ShardId leaderShardId, - final String retentionLeaseId, - final long retainingSequenceNumber, - final Client remoteClient, - final TimeValue timeout) { + final ShardId leaderShardId, + final String retentionLeaseId, + final long retainingSequenceNumber, + final Client remoteClient, + final TimeValue timeout + ) { try { final PlainActionFuture response = new PlainActionFuture<>(); asyncRenewRetentionLease(leaderShardId, retentionLeaseId, retainingSequenceNumber, remoteClient, response); @@ -146,13 +155,18 @@ public static Optional syncRenewRetentionLease( * @param listener the listener */ public static void asyncRenewRetentionLease( - final ShardId leaderShardId, - final String retentionLeaseId, - final long retainingSequenceNumber, - final Client remoteClient, - final ActionListener listener) { - final RetentionLeaseActions.RenewRequest request = - new RetentionLeaseActions.RenewRequest(leaderShardId, retentionLeaseId, retainingSequenceNumber, "ccr"); + final ShardId leaderShardId, + final String retentionLeaseId, + final long retainingSequenceNumber, + final Client remoteClient, + final ActionListener listener + ) { + final RetentionLeaseActions.RenewRequest request = new RetentionLeaseActions.RenewRequest( + leaderShardId, + retentionLeaseId, + retainingSequenceNumber, + "ccr" + ); remoteClient.execute(RetentionLeaseActions.Renew.INSTANCE, request, listener); } @@ -167,10 +181,11 @@ public static void asyncRenewRetentionLease( * @param listener the listener */ public static void asyncRemoveRetentionLease( - final ShardId leaderShardId, - final String retentionLeaseId, - final Client remoteClient, - final ActionListener listener) { + final ShardId leaderShardId, + final String retentionLeaseId, + final Client remoteClient, + final ActionListener listener + ) { final RetentionLeaseActions.RemoveRequest request = new RetentionLeaseActions.RemoveRequest(leaderShardId, retentionLeaseId); remoteClient.execute(RetentionLeaseActions.Remove.INSTANCE, request, listener); } diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/CcrSettings.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/CcrSettings.java index 9448ea08f615c..268b2ab47554a 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/CcrSettings.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/CcrSettings.java @@ -12,8 +12,8 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.CombinedRateLimiter; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.xpack.core.XPackSettings; import java.util.Arrays; @@ -27,57 +27,88 @@ public final class CcrSettings { /** * Index setting for a following index. */ - public static final Setting CCR_FOLLOWING_INDEX_SETTING = - Setting.boolSetting("index.xpack.ccr.following_index", false, Property.IndexScope, Property.InternalIndex); + public static final Setting CCR_FOLLOWING_INDEX_SETTING = Setting.boolSetting( + "index.xpack.ccr.following_index", + false, + Property.IndexScope, + Property.InternalIndex + ); /** * Dynamic node setting for specifying the wait_for_timeout that the auto follow coordinator and shard follow task should be using. */ public static final Setting CCR_WAIT_FOR_METADATA_TIMEOUT = Setting.timeSetting( - "ccr.wait_for_metadata_timeout", TimeValue.timeValueSeconds(60), Property.NodeScope, Property.Dynamic); + "ccr.wait_for_metadata_timeout", + TimeValue.timeValueSeconds(60), + Property.NodeScope, + Property.Dynamic + ); /** * Dynamic node setting for specifying the wait_for_timeout that the auto follow coordinator should be using. * TODO: Deprecate and remove this setting */ private static final Setting CCR_AUTO_FOLLOW_WAIT_FOR_METADATA_TIMEOUT = Setting.timeSetting( - "ccr.auto_follow.wait_for_metadata_timeout", CCR_WAIT_FOR_METADATA_TIMEOUT, Property.NodeScope, Property.Dynamic); + "ccr.auto_follow.wait_for_metadata_timeout", + CCR_WAIT_FOR_METADATA_TIMEOUT, + Property.NodeScope, + Property.Dynamic + ); /** * Max bytes a node can recover per second. */ - public static final Setting RECOVERY_MAX_BYTES_PER_SECOND = - Setting.byteSizeSetting("ccr.indices.recovery.max_bytes_per_sec", new ByteSizeValue(40, ByteSizeUnit.MB), - Setting.Property.Dynamic, Setting.Property.NodeScope); + public static final Setting RECOVERY_MAX_BYTES_PER_SECOND = Setting.byteSizeSetting( + "ccr.indices.recovery.max_bytes_per_sec", + new ByteSizeValue(40, ByteSizeUnit.MB), + Setting.Property.Dynamic, + Setting.Property.NodeScope + ); /** * File chunk size to send during recovery */ - public static final Setting RECOVERY_CHUNK_SIZE = - Setting.byteSizeSetting("ccr.indices.recovery.chunk_size", new ByteSizeValue(1, ByteSizeUnit.MB), - new ByteSizeValue(1, ByteSizeUnit.KB), new ByteSizeValue(1, ByteSizeUnit.GB), Setting.Property.Dynamic, - Setting.Property.NodeScope); + public static final Setting RECOVERY_CHUNK_SIZE = Setting.byteSizeSetting( + "ccr.indices.recovery.chunk_size", + new ByteSizeValue(1, ByteSizeUnit.MB), + new ByteSizeValue(1, ByteSizeUnit.KB), + new ByteSizeValue(1, ByteSizeUnit.GB), + Setting.Property.Dynamic, + Setting.Property.NodeScope + ); /** * Controls the maximum number of file chunk requests that are sent concurrently per recovery to the leader. */ - public static final Setting INDICES_RECOVERY_MAX_CONCURRENT_FILE_CHUNKS_SETTING = - Setting.intSetting("ccr.indices.recovery.max_concurrent_file_chunks", 5, 1, 10, Property.Dynamic, Property.NodeScope); + public static final Setting INDICES_RECOVERY_MAX_CONCURRENT_FILE_CHUNKS_SETTING = Setting.intSetting( + "ccr.indices.recovery.max_concurrent_file_chunks", + 5, + 1, + 10, + Property.Dynamic, + Property.NodeScope + ); /** * The leader must open resources for a ccr recovery. If there is no activity for this interval of time, * the leader will close the restore session. */ - public static final Setting INDICES_RECOVERY_ACTIVITY_TIMEOUT_SETTING = - Setting.timeSetting("ccr.indices.recovery.recovery_activity_timeout", TimeValue.timeValueSeconds(60), - Setting.Property.Dynamic, Setting.Property.NodeScope); + public static final Setting INDICES_RECOVERY_ACTIVITY_TIMEOUT_SETTING = Setting.timeSetting( + "ccr.indices.recovery.recovery_activity_timeout", + TimeValue.timeValueSeconds(60), + Setting.Property.Dynamic, + Setting.Property.NodeScope + ); /** * The timeout value to use for requests made as part of ccr recovery process. * */ - public static final Setting INDICES_RECOVERY_ACTION_TIMEOUT_SETTING = - Setting.positiveTimeSetting("ccr.indices.recovery.internal_action_timeout", TimeValue.timeValueSeconds(60), - Property.Dynamic, Property.NodeScope); + public static final Setting INDICES_RECOVERY_ACTION_TIMEOUT_SETTING = Setting.positiveTimeSetting( + "ccr.indices.recovery.internal_action_timeout", + TimeValue.timeValueSeconds(60), + Property.Dynamic, + Property.NodeScope + ); /** * The settings defined by CCR. @@ -86,15 +117,16 @@ public final class CcrSettings { */ public static List> getSettings() { return Arrays.asList( - XPackSettings.CCR_ENABLED_SETTING, - CCR_FOLLOWING_INDEX_SETTING, - RECOVERY_MAX_BYTES_PER_SECOND, - INDICES_RECOVERY_ACTION_TIMEOUT_SETTING, - INDICES_RECOVERY_ACTIVITY_TIMEOUT_SETTING, - CCR_AUTO_FOLLOW_WAIT_FOR_METADATA_TIMEOUT, - RECOVERY_CHUNK_SIZE, - INDICES_RECOVERY_MAX_CONCURRENT_FILE_CHUNKS_SETTING, - CCR_WAIT_FOR_METADATA_TIMEOUT); + XPackSettings.CCR_ENABLED_SETTING, + CCR_FOLLOWING_INDEX_SETTING, + RECOVERY_MAX_BYTES_PER_SECOND, + INDICES_RECOVERY_ACTION_TIMEOUT_SETTING, + INDICES_RECOVERY_ACTIVITY_TIMEOUT_SETTING, + CCR_AUTO_FOLLOW_WAIT_FOR_METADATA_TIMEOUT, + RECOVERY_CHUNK_SIZE, + INDICES_RECOVERY_MAX_CONCURRENT_FILE_CHUNKS_SETTING, + CCR_WAIT_FOR_METADATA_TIMEOUT + ); } private final CombinedRateLimiter ccrRateLimiter; diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinator.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinator.java index 188c444f80794..d2c825da6f3d8 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinator.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinator.java @@ -98,7 +98,8 @@ public AutoFollowCoordinator( final CcrLicenseChecker ccrLicenseChecker, final LongSupplier relativeMillisTimeProvider, final LongSupplier absoluteMillisTimeProvider, - final Executor executor) { + final Executor executor + ) { this.client = client; this.clusterService = clusterService; @@ -152,8 +153,10 @@ public synchronized AutoFollowStats getStats() { long lastSeenMetadataVersion = entry.getValue().metadataVersion; if (lastAutoFollowTimeInMillis != -1) { long timeSinceLastCheckInMillis = relativeMillisTimeProvider.getAsLong() - lastAutoFollowTimeInMillis; - timesSinceLastAutoFollowPerRemoteCluster.put(entry.getKey(), - new AutoFollowedCluster(timeSinceLastCheckInMillis, lastSeenMetadataVersion)); + timesSinceLastAutoFollowPerRemoteCluster.put( + entry.getKey(), + new AutoFollowedCluster(timeSinceLastCheckInMillis, lastSeenMetadataVersion) + ); } else { timesSinceLastAutoFollowPerRemoteCluster.put(entry.getKey(), new AutoFollowedCluster(-1L, lastSeenMetadataVersion)); } @@ -172,21 +175,36 @@ synchronized void updateStats(List results) { long newStatsReceivedTimeStamp = absoluteMillisTimeProvider.getAsLong(); for (AutoFollowResult result : results) { if (result.clusterStateFetchException != null) { - recentAutoFollowErrors.put(result.autoFollowPatternName, - Tuple.tuple(newStatsReceivedTimeStamp, new ElasticsearchException(result.clusterStateFetchException))); + recentAutoFollowErrors.put( + result.autoFollowPatternName, + Tuple.tuple(newStatsReceivedTimeStamp, new ElasticsearchException(result.clusterStateFetchException)) + ); numberOfFailedRemoteClusterStateRequests++; - LOGGER.warn(new ParameterizedMessage("failure occurred while fetching cluster state for auto follow pattern [{}]", - result.autoFollowPatternName), result.clusterStateFetchException); + LOGGER.warn( + new ParameterizedMessage( + "failure occurred while fetching cluster state for auto follow pattern [{}]", + result.autoFollowPatternName + ), + result.clusterStateFetchException + ); } else { recentAutoFollowErrors.remove(result.autoFollowPatternName); for (Map.Entry entry : result.autoFollowExecutionResults.entrySet()) { final String patternAndIndexKey = result.autoFollowPatternName + ":" + entry.getKey().getName(); if (entry.getValue() != null) { numberOfFailedIndicesAutoFollowed++; - recentAutoFollowErrors.put(patternAndIndexKey, - Tuple.tuple(newStatsReceivedTimeStamp, ExceptionsHelper.convertToElastic(entry.getValue()))); - LOGGER.warn(new ParameterizedMessage("failure occurred while auto following index [{}] for auto follow " + - "pattern [{}]", entry.getKey(), result.autoFollowPatternName), entry.getValue()); + recentAutoFollowErrors.put( + patternAndIndexKey, + Tuple.tuple(newStatsReceivedTimeStamp, ExceptionsHelper.convertToElastic(entry.getValue())) + ); + LOGGER.warn( + new ParameterizedMessage( + "failure occurred while auto following index [{}] for auto follow " + "pattern [{}]", + entry.getKey(), + result.autoFollowPatternName + ), + entry.getValue() + ); } else { numberOfSuccessfulIndicesAutoFollowed++; recentAutoFollowErrors.remove(patternAndIndexKey); @@ -210,7 +228,9 @@ void updateAutoFollowers(ClusterState followerClusterState) { } final CopyOnWriteHashMap autoFollowers = CopyOnWriteHashMap.copyOf(this.autoFollowers); - Set newRemoteClusters = autoFollowMetadata.getPatterns().values().stream() + Set newRemoteClusters = autoFollowMetadata.getPatterns() + .values() + .stream() .filter(AutoFollowPattern::isActive) .map(AutoFollowPattern::getRemoteCluster) .filter(remoteCluster -> autoFollowers.containsKey(remoteCluster) == false) @@ -218,13 +238,20 @@ void updateAutoFollowers(ClusterState followerClusterState) { Map newAutoFollowers = new HashMap<>(newRemoteClusters.size()); for (String remoteCluster : newRemoteClusters) { - AutoFollower autoFollower = - new AutoFollower(remoteCluster, this::updateStats, clusterService::state, relativeMillisTimeProvider, executor) { + AutoFollower autoFollower = new AutoFollower( + remoteCluster, + this::updateStats, + clusterService::state, + relativeMillisTimeProvider, + executor + ) { @Override - void getRemoteClusterState(final String remoteCluster, - final long metadataVersion, - final BiConsumer handler) { + void getRemoteClusterState( + final String remoteCluster, + final long metadataVersion, + final BiConsumer handler + ) { final ClusterStateRequest request = new ClusterStateRequest(); request.clear(); request.metadata(true); @@ -237,14 +264,17 @@ void getRemoteClusterState(final String remoteCluster, remoteCluster, request, e -> handler.accept(null, e), - remoteClusterStateResponse -> handler.accept(remoteClusterStateResponse, null)); + remoteClusterStateResponse -> handler.accept(remoteClusterStateResponse, null) + ); } @Override - void createAndFollow(Map headers, - PutFollowAction.Request request, - Runnable successHandler, - Consumer failureHandler) { + void createAndFollow( + Map headers, + PutFollowAction.Request request, + Runnable successHandler, + Consumer failureHandler + ) { Client followerClient = CcrLicenseChecker.wrapClient(client, headers); followerClient.execute( PutFollowAction.INSTANCE, @@ -254,8 +284,7 @@ void createAndFollow(Map headers, } @Override - void updateAutoFollowMetadata(Function updateFunction, - Consumer handler) { + void updateAutoFollowMetadata(Function updateFunction, Consumer handler) { clusterService.submitStateUpdateTask("update_auto_follow_metadata", new ClusterStateUpdateTask() { @Override @@ -287,7 +316,9 @@ public void clusterStateProcessed(String source, ClusterState oldState, ClusterS for (Map.Entry entry : autoFollowers.entrySet()) { String remoteCluster = entry.getKey(); AutoFollower autoFollower = entry.getValue(); - boolean exist = autoFollowMetadata.getPatterns().values().stream() + boolean exist = autoFollowMetadata.getPatterns() + .values() + .stream() .filter(AutoFollowPattern::isActive) .anyMatch(pattern -> pattern.getRemoteCluster().equals(remoteCluster)); if (exist == false) { @@ -303,9 +334,7 @@ public void clusterStateProcessed(String source, ClusterState oldState, ClusterS } } assert assertNoOtherActiveAutoFollower(newAutoFollowers); - this.autoFollowers = autoFollowers - .copyAndPutAll(newAutoFollowers) - .copyAndRemoveAll(removedRemoteClusters); + this.autoFollowers = autoFollowers.copyAndPutAll(newAutoFollowers).copyAndRemoveAll(removedRemoteClusters); } private boolean assertNoOtherActiveAutoFollower(Map newAutoFollowers) { @@ -316,7 +345,6 @@ private boolean assertNoOtherActiveAutoFollower(Map newAut return true; } - Map getAutoFollowers() { return autoFollowers; } @@ -353,11 +381,13 @@ abstract static class AutoFollower { private volatile boolean stop; private volatile List lastActivePatterns = List.of(); - AutoFollower(final String remoteCluster, - final Consumer> statsUpdater, - final Supplier followerClusterStateSupplier, - final LongSupplier relativeTimeProvider, - final Executor executor) { + AutoFollower( + final String remoteCluster, + final Consumer> statsUpdater, + final Supplier followerClusterStateSupplier, + final LongSupplier relativeTimeProvider, + final Executor executor + ) { this.remoteCluster = remoteCluster; this.statsUpdater = statsUpdater; this.followerClusterStateSupplier = followerClusterStateSupplier; @@ -389,7 +419,9 @@ void start() { return; } - final List patterns = autoFollowMetadata.getPatterns().entrySet().stream() + final List patterns = autoFollowMetadata.getPatterns() + .entrySet() + .stream() .filter(entry -> entry.getValue().getRemoteCluster().equals(remoteCluster)) .filter(entry -> entry.getValue().isActive()) .map(Map.Entry::getKey) @@ -449,11 +481,13 @@ void stop() { stop = true; } - private void autoFollowIndices(final AutoFollowMetadata autoFollowMetadata, - final ClusterState clusterState, - final ClusterState remoteClusterState, - final List patterns, - final Thread thread) { + private void autoFollowIndices( + final AutoFollowMetadata autoFollowMetadata, + final ClusterState clusterState, + final ClusterState remoteClusterState, + final List patterns, + final Thread thread + ) { int i = 0; for (String autoFollowPatternName : patterns) { final int slot = i; @@ -466,35 +500,49 @@ private void autoFollowIndices(final AutoFollowMetadata autoFollowMetadata, finalise(slot, new AutoFollowResult(autoFollowPatternName), thread); } else { List> patternsForTheSameRemoteCluster = autoFollowMetadata.getPatterns() - .entrySet().stream() + .entrySet() + .stream() .filter(item -> autoFollowPatternName.equals(item.getKey()) == false) .filter(item -> remoteCluster.equals(item.getValue().getRemoteCluster())) .map(item -> new Tuple<>(item.getKey(), item.getValue())) .collect(Collectors.toList()); Consumer resultHandler = result -> finalise(slot, result, thread); - checkAutoFollowPattern(autoFollowPatternName, remoteCluster, autoFollowPattern, leaderIndicesToFollow, headers, - patternsForTheSameRemoteCluster, remoteClusterState.metadata(), clusterState.metadata(), resultHandler); + checkAutoFollowPattern( + autoFollowPatternName, + remoteCluster, + autoFollowPattern, + leaderIndicesToFollow, + headers, + patternsForTheSameRemoteCluster, + remoteClusterState.metadata(), + clusterState.metadata(), + resultHandler + ); } i++; } cleanFollowedRemoteIndices(remoteClusterState, patterns); } - private void checkAutoFollowPattern(String autoFollowPattenName, - String remoteCluster, - AutoFollowPattern autoFollowPattern, - List leaderIndicesToFollow, - Map headers, - List> patternsForTheSameRemoteCluster, - Metadata remoteMetadata, - Metadata localMetadata, - Consumer resultHandler) { + private void checkAutoFollowPattern( + String autoFollowPattenName, + String remoteCluster, + AutoFollowPattern autoFollowPattern, + List leaderIndicesToFollow, + Map headers, + List> patternsForTheSameRemoteCluster, + Metadata remoteMetadata, + Metadata localMetadata, + Consumer resultHandler + ) { final GroupedActionListener> groupedListener = new GroupedActionListener<>( ActionListener.wrap( rs -> resultHandler.accept(new AutoFollowResult(autoFollowPattenName, new ArrayList<>(rs))), - e -> { throw new AssertionError("must never happen", e); }), - leaderIndicesToFollow.size()); + e -> { throw new AssertionError("must never happen", e); } + ), + leaderIndicesToFollow.size() + ); for (final Index indexToFollow : leaderIndicesToFollow) { IndexAbstraction indexAbstraction = remoteMetadata.getIndicesLookup().get(indexToFollow.getName()); @@ -504,13 +552,27 @@ private void checkAutoFollowPattern(String autoFollowPattenName, .collect(Collectors.toList()); if (otherMatchingPatterns.size() != 0) { groupedListener.onResponse( - new Tuple<>(indexToFollow, new ElasticsearchException("index to follow [" + indexToFollow.getName() + - "] for pattern [" + autoFollowPattenName + "] matches with other patterns " + otherMatchingPatterns + ""))); + new Tuple<>( + indexToFollow, + new ElasticsearchException( + "index to follow [" + + indexToFollow.getName() + + "] for pattern [" + + autoFollowPattenName + + "] matches with other patterns " + + otherMatchingPatterns + + "" + ) + ) + ); } else { final Settings leaderIndexSettings = remoteMetadata.getIndexSafe(indexToFollow).getSettings(); if (IndexSettings.INDEX_SOFT_DELETES_SETTING.get(leaderIndexSettings) == false) { - String message = String.format(Locale.ROOT, "index [%s] cannot be followed, because soft deletes are not enabled", - indexToFollow.getName()); + String message = String.format( + Locale.ROOT, + "index [%s] cannot be followed, because soft deletes are not enabled", + indexToFollow.getName() + ); LOGGER.warn(message); updateAutoFollowMetadata(recordLeaderIndexAsFollowFunction(autoFollowPattenName, indexToFollow), error -> { ElasticsearchException failure = new ElasticsearchException(message); @@ -520,7 +582,8 @@ private void checkAutoFollowPattern(String autoFollowPattenName, groupedListener.onResponse(new Tuple<>(indexToFollow, failure)); }); } else if (SearchableSnapshotsSettings.isSearchableSnapshotStore(leaderIndexSettings)) { - String message = String.format(Locale.ROOT, + String message = String.format( + Locale.ROOT, "index to follow [%s] is a searchable snapshot index and cannot be used for cross-cluster replication purpose", indexToFollow.getName() ); @@ -533,19 +596,25 @@ private void checkAutoFollowPattern(String autoFollowPattenName, groupedListener.onResponse(new Tuple<>(indexToFollow, failure)); }); } else if (leaderIndexAlreadyFollowed(autoFollowPattern, indexToFollow, localMetadata)) { - updateAutoFollowMetadata(recordLeaderIndexAsFollowFunction(autoFollowPattenName, indexToFollow), - error -> groupedListener.onResponse(new Tuple<>(indexToFollow, error))); + updateAutoFollowMetadata( + recordLeaderIndexAsFollowFunction(autoFollowPattenName, indexToFollow), + error -> groupedListener.onResponse(new Tuple<>(indexToFollow, error)) + ); } else { - followLeaderIndex(autoFollowPattenName, remoteCluster, indexToFollow, autoFollowPattern, headers, - error -> groupedListener.onResponse(new Tuple<>(indexToFollow, error))); + followLeaderIndex( + autoFollowPattenName, + remoteCluster, + indexToFollow, + autoFollowPattern, + headers, + error -> groupedListener.onResponse(new Tuple<>(indexToFollow, error)) + ); } } } } - private static boolean leaderIndexAlreadyFollowed(AutoFollowPattern autoFollowPattern, - Index leaderIndex, - Metadata localMetadata) { + private static boolean leaderIndexAlreadyFollowed(AutoFollowPattern autoFollowPattern, Index leaderIndex, Metadata localMetadata) { String followIndexName = getFollowerIndexName(autoFollowPattern, leaderIndex.getName()); IndexMetadata indexMetadata = localMetadata.index(followIndexName); if (indexMetadata != null) { @@ -562,12 +631,14 @@ private static boolean leaderIndexAlreadyFollowed(AutoFollowPattern autoFollowPa return false; } - private void followLeaderIndex(String autoFollowPattenName, - String remoteCluster, - Index indexToFollow, - AutoFollowPattern pattern, - Map headers, - Consumer onResult) { + private void followLeaderIndex( + String autoFollowPattenName, + String remoteCluster, + Index indexToFollow, + AutoFollowPattern pattern, + Map headers, + Consumer onResult + ) { final String leaderIndexName = indexToFollow.getName(); final String followIndexName = getFollowerIndexName(pattern, leaderIndexName); @@ -622,25 +693,28 @@ private void finalise(int slot, AutoFollowResult result, final Thread thread) { } } - static List getLeaderIndicesToFollow(AutoFollowPattern autoFollowPattern, - ClusterState remoteClusterState, - List followedIndexUUIDs) { + static List getLeaderIndicesToFollow( + AutoFollowPattern autoFollowPattern, + ClusterState remoteClusterState, + List followedIndexUUIDs + ) { List leaderIndicesToFollow = new ArrayList<>(); for (IndexMetadata leaderIndexMetadata : remoteClusterState.getMetadata()) { if (leaderIndexMetadata.getState() != IndexMetadata.State.OPEN) { continue; } - IndexAbstraction indexAbstraction = - remoteClusterState.getMetadata().getIndicesLookup().get(leaderIndexMetadata.getIndex().getName()); + IndexAbstraction indexAbstraction = remoteClusterState.getMetadata() + .getIndicesLookup() + .get(leaderIndexMetadata.getIndex().getName()); if (autoFollowPattern.isActive() && autoFollowPattern.match(indexAbstraction)) { IndexRoutingTable indexRoutingTable = remoteClusterState.routingTable().index(leaderIndexMetadata.getIndex()); if (indexRoutingTable != null && - // Leader indices can be in the cluster state, but not all primary shards may be ready yet. - // This checks ensures all primary shards have started, so that index following does not fail. - // If not all primary shards are ready, then the next time the auto follow coordinator runs - // this index will be auto followed. - indexRoutingTable.allPrimaryShardsActive() && - followedIndexUUIDs.contains(leaderIndexMetadata.getIndex().getUUID()) == false) { + // Leader indices can be in the cluster state, but not all primary shards may be ready yet. + // This checks ensures all primary shards have started, so that index following does not fail. + // If not all primary shards are ready, then the next time the auto follow coordinator runs + // this index will be auto followed. + indexRoutingTable.allPrimaryShardsActive() + && followedIndexUUIDs.contains(leaderIndexMetadata.getIndex().getUUID()) == false) { leaderIndicesToFollow.add(leaderIndexMetadata.getIndex()); } } @@ -656,8 +730,7 @@ static String getFollowerIndexName(AutoFollowPattern autoFollowPattern, String l } } - static Function recordLeaderIndexAsFollowFunction(String name, - Index indexToFollow) { + static Function recordLeaderIndexAsFollowFunction(String name, Index indexToFollow) { return currentState -> { AutoFollowMetadata currentAutoFollowMetadata = currentState.metadata().custom(AutoFollowMetadata.TYPE); Map> newFollowedIndexUUIDS = new HashMap<>(currentAutoFollowMetadata.getFollowedLeaderIndexUUIDs()); @@ -674,11 +747,15 @@ static Function recordLeaderIndexAsFollowFunction(St newUUIDs.add(indexToFollow.getUUID()); return Collections.unmodifiableList(newUUIDs); }); - final AutoFollowMetadata newAutoFollowMetadata = new AutoFollowMetadata(currentAutoFollowMetadata.getPatterns(), - newFollowedIndexUUIDS, currentAutoFollowMetadata.getHeaders()); + final AutoFollowMetadata newAutoFollowMetadata = new AutoFollowMetadata( + currentAutoFollowMetadata.getPatterns(), + newFollowedIndexUUIDS, + currentAutoFollowMetadata.getHeaders() + ); return ClusterState.builder(currentState) - .metadata(Metadata.builder(currentState.getMetadata()) - .putCustom(AutoFollowMetadata.TYPE, newAutoFollowMetadata).build()) + .metadata( + Metadata.builder(currentState.getMetadata()).putCustom(AutoFollowMetadata.TYPE, newAutoFollowMetadata).build() + ) .build(); }; } @@ -692,12 +769,17 @@ void cleanFollowedRemoteIndices(final ClusterState remoteClusterState, final Lis } static Function cleanFollowedRemoteIndices( - final Metadata remoteMetadata, final List autoFollowPatternNames) { + final Metadata remoteMetadata, + final List autoFollowPatternNames + ) { return currentState -> { AutoFollowMetadata currentAutoFollowMetadata = currentState.metadata().custom(AutoFollowMetadata.TYPE); - Map> autoFollowPatternNameToFollowedIndexUUIDs = - new HashMap<>(currentAutoFollowMetadata.getFollowedLeaderIndexUUIDs()); - Set remoteIndexUUIDS = remoteMetadata.getIndices().values().stream() + Map> autoFollowPatternNameToFollowedIndexUUIDs = new HashMap<>( + currentAutoFollowMetadata.getFollowedLeaderIndexUUIDs() + ); + Set remoteIndexUUIDS = remoteMetadata.getIndices() + .values() + .stream() .map(IndexMetadata::getIndexUUID) .collect(Collectors.toSet()); @@ -710,11 +792,11 @@ static Function cleanFollowedRemoteIndices( continue; } - List followedIndexUUIDs = - new ArrayList<>(autoFollowPatternNameToFollowedIndexUUIDs.get(autoFollowPatternName)); + List followedIndexUUIDs = new ArrayList<>(autoFollowPatternNameToFollowedIndexUUIDs.get(autoFollowPatternName)); // Remove leader indices that no longer exist in the remote cluster: boolean entriesRemoved = followedIndexUUIDs.removeIf( - followedLeaderIndexUUID -> remoteIndexUUIDS.contains(followedLeaderIndexUUID) == false); + followedLeaderIndexUUID -> remoteIndexUUIDS.contains(followedLeaderIndexUUID) == false + ); if (entriesRemoved) { requiresCSUpdate = true; } @@ -722,11 +804,15 @@ static Function cleanFollowedRemoteIndices( } if (requiresCSUpdate) { - final AutoFollowMetadata newAutoFollowMetadata = new AutoFollowMetadata(currentAutoFollowMetadata.getPatterns(), - autoFollowPatternNameToFollowedIndexUUIDs, currentAutoFollowMetadata.getHeaders()); + final AutoFollowMetadata newAutoFollowMetadata = new AutoFollowMetadata( + currentAutoFollowMetadata.getPatterns(), + autoFollowPatternNameToFollowedIndexUUIDs, + currentAutoFollowMetadata.getHeaders() + ); return ClusterState.builder(currentState) - .metadata(Metadata.builder(currentState.getMetadata()) - .putCustom(AutoFollowMetadata.TYPE, newAutoFollowMetadata).build()) + .metadata( + Metadata.builder(currentState.getMetadata()).putCustom(AutoFollowMetadata.TYPE, newAutoFollowMetadata).build() + ) .build(); } else { return currentState; @@ -753,10 +839,7 @@ abstract void createAndFollow( Consumer failureHandler ); - abstract void updateAutoFollowMetadata( - Function updateFunction, - Consumer handler - ); + abstract void updateAutoFollowMetadata(Function updateFunction, Consumer handler); } diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/CcrRequests.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/CcrRequests.java index dffe3daa887d5..857bf50e03732 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/CcrRequests.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/CcrRequests.java @@ -17,9 +17,9 @@ import org.elasticsearch.cluster.metadata.MappingMetadata; import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.Index; import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.ccr.CcrSettings; import java.util.Arrays; @@ -52,78 +52,96 @@ public static PutMappingRequest putMappingRequest(String followerIndex, MappingM * Gets an {@link IndexMetadata} of the given index. The mapping version and metadata version of the returned {@link IndexMetadata} * must be at least the provided {@code mappingVersion} and {@code metadataVersion} respectively. */ - public static void getIndexMetadata(Client client, Index index, long mappingVersion, long metadataVersion, - Supplier timeoutSupplier, ActionListener listener) { + public static void getIndexMetadata( + Client client, + Index index, + long mappingVersion, + long metadataVersion, + Supplier timeoutSupplier, + ActionListener listener + ) { final ClusterStateRequest request = CcrRequests.metadataRequest(index.getName()); if (metadataVersion > 0) { request.waitForMetadataVersion(metadataVersion).waitForTimeout(timeoutSupplier.get()); } - client.admin().cluster().state(request, ActionListener.wrap( - response -> { - if (response.getState() == null) { // timeout on wait_for_metadata_version - assert metadataVersion > 0 : metadataVersion; - if (timeoutSupplier.get().nanos() < 0) { - listener.onFailure(new IllegalStateException("timeout to get cluster state with" + - " metadata version [" + metadataVersion + "], mapping version [" + mappingVersion + "]")); - } else { - getIndexMetadata(client, index, mappingVersion, metadataVersion, timeoutSupplier, listener); - } + client.admin().cluster().state(request, ActionListener.wrap(response -> { + if (response.getState() == null) { // timeout on wait_for_metadata_version + assert metadataVersion > 0 : metadataVersion; + if (timeoutSupplier.get().nanos() < 0) { + listener.onFailure( + new IllegalStateException( + "timeout to get cluster state with" + + " metadata version [" + + metadataVersion + + "], mapping version [" + + mappingVersion + + "]" + ) + ); } else { - final Metadata metadata = response.getState().metadata(); - final IndexMetadata indexMetadata = metadata.getIndexSafe(index); - if (indexMetadata.getMappingVersion() >= mappingVersion) { - listener.onResponse(indexMetadata); - return; - } - if (timeoutSupplier.get().nanos() < 0) { - listener.onFailure(new IllegalStateException( - "timeout to get cluster state with mapping version [" + mappingVersion + "]")); - } else { - // ask for the next version. - getIndexMetadata(client, index, mappingVersion, metadata.version() + 1, timeoutSupplier, listener); - } + getIndexMetadata(client, index, mappingVersion, metadataVersion, timeoutSupplier, listener); } - }, - listener::onFailure - )); - } - - public static final RequestValidators.RequestValidator CCR_PUT_MAPPING_REQUEST_VALIDATOR = - (request, state, indices) -> { - if (request.origin() == null) { - return Optional.empty(); // a put-mapping-request on old versions does not have origin. + } else { + final Metadata metadata = response.getState().metadata(); + final IndexMetadata indexMetadata = metadata.getIndexSafe(index); + if (indexMetadata.getMappingVersion() >= mappingVersion) { + listener.onResponse(indexMetadata); + return; } - final List followingIndices = Arrays.stream(indices) - .filter(index -> { - final IndexMetadata indexMetadata = state.metadata().index(index); - return indexMetadata != null && CcrSettings.CCR_FOLLOWING_INDEX_SETTING.get(indexMetadata.getSettings()); - }).collect(Collectors.toList()); - if (followingIndices.isEmpty() == false && "ccr".equals(request.origin()) == false) { - final String errorMessage = "can't put mapping to the following indices " - + "[" + followingIndices.stream().map(Index::getName).collect(Collectors.joining(", ")) + "]; " - + "the mapping of the following indices are self-replicated from its leader indices"; - return Optional.of(new ElasticsearchStatusException(errorMessage, RestStatus.FORBIDDEN)); + if (timeoutSupplier.get().nanos() < 0) { + listener.onFailure( + new IllegalStateException("timeout to get cluster state with mapping version [" + mappingVersion + "]") + ); + } else { + // ask for the next version. + getIndexMetadata(client, index, mappingVersion, metadata.version() + 1, timeoutSupplier, listener); } - return Optional.empty(); - }; + } + }, listener::onFailure)); + } - public static final RequestValidators.RequestValidator CCR_INDICES_ALIASES_REQUEST_VALIDATOR = - (request, state, indices) -> { - if (request.origin() == null) { - return Optional.empty(); // an indices aliases request on old versions does not have origin - } - final List followingIndices = Arrays.stream(indices) - .filter(index -> { - final IndexMetadata indexMetadata = state.metadata().index(index); - return indexMetadata != null && CcrSettings.CCR_FOLLOWING_INDEX_SETTING.get(indexMetadata.getSettings()); - }).collect(Collectors.toList()); - if (followingIndices.isEmpty() == false && "ccr".equals(request.origin()) == false) { - final String errorMessage = "can't modify aliases on indices " - + "[" + followingIndices.stream().map(Index::getName).collect(Collectors.joining(", ")) + "]; " - + "aliases of following indices are self-replicated from their leader indices"; - return Optional.of(new ElasticsearchStatusException(errorMessage, RestStatus.FORBIDDEN)); - } - return Optional.empty(); - }; + public static final RequestValidators.RequestValidator CCR_PUT_MAPPING_REQUEST_VALIDATOR = ( + request, + state, + indices) -> { + if (request.origin() == null) { + return Optional.empty(); // a put-mapping-request on old versions does not have origin. + } + final List followingIndices = Arrays.stream(indices).filter(index -> { + final IndexMetadata indexMetadata = state.metadata().index(index); + return indexMetadata != null && CcrSettings.CCR_FOLLOWING_INDEX_SETTING.get(indexMetadata.getSettings()); + }).collect(Collectors.toList()); + if (followingIndices.isEmpty() == false && "ccr".equals(request.origin()) == false) { + final String errorMessage = "can't put mapping to the following indices " + + "[" + + followingIndices.stream().map(Index::getName).collect(Collectors.joining(", ")) + + "]; " + + "the mapping of the following indices are self-replicated from its leader indices"; + return Optional.of(new ElasticsearchStatusException(errorMessage, RestStatus.FORBIDDEN)); + } + return Optional.empty(); + }; + + public static final RequestValidators.RequestValidator CCR_INDICES_ALIASES_REQUEST_VALIDATOR = ( + request, + state, + indices) -> { + if (request.origin() == null) { + return Optional.empty(); // an indices aliases request on old versions does not have origin + } + final List followingIndices = Arrays.stream(indices).filter(index -> { + final IndexMetadata indexMetadata = state.metadata().index(index); + return indexMetadata != null && CcrSettings.CCR_FOLLOWING_INDEX_SETTING.get(indexMetadata.getSettings()); + }).collect(Collectors.toList()); + if (followingIndices.isEmpty() == false && "ccr".equals(request.origin()) == false) { + final String errorMessage = "can't modify aliases on indices " + + "[" + + followingIndices.stream().map(Index::getName).collect(Collectors.joining(", ")) + + "]; " + + "aliases of following indices are self-replicated from their leader indices"; + return Optional.of(new ElasticsearchStatusException(errorMessage, RestStatus.FORBIDDEN)); + } + return Optional.empty(); + }; } diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/ShardChangesAction.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/ShardChangesAction.java index 78a66db9fdd8e..d4de3177534b6 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/ShardChangesAction.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/ShardChangesAction.java @@ -144,12 +144,16 @@ public ActionRequestValidationException validate() { validationException = addValidationError("fromSeqNo [" + fromSeqNo + "] cannot be lower than 0", validationException); } if (maxOperationCount < 0) { - validationException = addValidationError("maxOperationCount [" + maxOperationCount + - "] cannot be lower than 0", validationException); + validationException = addValidationError( + "maxOperationCount [" + maxOperationCount + "] cannot be lower than 0", + validationException + ); } if (maxBatchSize.compareTo(ByteSizeValue.ZERO) <= 0) { - validationException = - addValidationError("maxBatchSize [" + maxBatchSize.getStringRep() + "] must be larger than 0", validationException); + validationException = addValidationError( + "maxBatchSize [" + maxBatchSize.getStringRep() + "] must be larger than 0", + validationException + ); } return validationException; } @@ -165,18 +169,17 @@ public void writeTo(StreamOutput out) throws IOException { maxBatchSize.writeTo(out); } - @Override public boolean equals(final Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; final Request request = (Request) o; - return fromSeqNo == request.fromSeqNo && - maxOperationCount == request.maxOperationCount && - Objects.equals(shardId, request.shardId) && - Objects.equals(expectedHistoryUUID, request.expectedHistoryUUID) && - Objects.equals(pollTimeout, request.pollTimeout) && - maxBatchSize.equals(request.maxBatchSize); + return fromSeqNo == request.fromSeqNo + && maxOperationCount == request.maxOperationCount + && Objects.equals(shardId, request.shardId) + && Objects.equals(expectedHistoryUUID, request.expectedHistoryUUID) + && Objects.equals(pollTimeout, request.pollTimeout) + && maxBatchSize.equals(request.maxBatchSize); } @Override @@ -186,14 +189,20 @@ public int hashCode() { @Override public String toString() { - return "Request{" + - "fromSeqNo=" + fromSeqNo + - ", maxOperationCount=" + maxOperationCount + - ", shardId=" + shardId + - ", expectedHistoryUUID=" + expectedHistoryUUID + - ", pollTimeout=" + pollTimeout + - ", maxBatchSize=" + maxBatchSize.getStringRep() + - '}'; + return "Request{" + + "fromSeqNo=" + + fromSeqNo + + ", maxOperationCount=" + + maxOperationCount + + ", shardId=" + + shardId + + ", expectedHistoryUUID=" + + expectedHistoryUUID + + ", pollTimeout=" + + pollTimeout + + ", maxBatchSize=" + + maxBatchSize.getStringRep() + + '}'; } } @@ -248,8 +257,7 @@ public long getTookInMillis() { return tookInMillis; } - Response() { - } + Response() {} Response(StreamInput in) throws IOException { super(in); @@ -268,14 +276,15 @@ public long getTookInMillis() { } Response( - final long mappingVersion, - final long settingsVersion, - final long aliasesVersion, - final long globalCheckpoint, - final long maxSeqNo, - final long maxSeqNoOfUpdatesOrDeletes, - final Translog.Operation[] operations, - final long tookInMillis) { + final long mappingVersion, + final long settingsVersion, + final long aliasesVersion, + final long globalCheckpoint, + final long maxSeqNo, + final long maxSeqNoOfUpdatesOrDeletes, + final Translog.Operation[] operations, + final long tookInMillis + ) { this.mappingVersion = mappingVersion; this.settingsVersion = settingsVersion; this.aliasesVersion = aliasesVersion; @@ -305,27 +314,28 @@ public boolean equals(final Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; final Response that = (Response) o; - return mappingVersion == that.mappingVersion && - settingsVersion == that.settingsVersion && - aliasesVersion == that.aliasesVersion && - globalCheckpoint == that.globalCheckpoint && - maxSeqNo == that.maxSeqNo && - maxSeqNoOfUpdatesOrDeletes == that.maxSeqNoOfUpdatesOrDeletes && - Arrays.equals(operations, that.operations) && - tookInMillis == that.tookInMillis; + return mappingVersion == that.mappingVersion + && settingsVersion == that.settingsVersion + && aliasesVersion == that.aliasesVersion + && globalCheckpoint == that.globalCheckpoint + && maxSeqNo == that.maxSeqNo + && maxSeqNoOfUpdatesOrDeletes == that.maxSeqNoOfUpdatesOrDeletes + && Arrays.equals(operations, that.operations) + && tookInMillis == that.tookInMillis; } @Override public int hashCode() { return Objects.hash( - mappingVersion, - settingsVersion, - aliasesVersion, - globalCheckpoint, - maxSeqNo, - maxSeqNoOfUpdatesOrDeletes, - Arrays.hashCode(operations), - tookInMillis); + mappingVersion, + settingsVersion, + aliasesVersion, + globalCheckpoint, + maxSeqNo, + maxSeqNoOfUpdatesOrDeletes, + Arrays.hashCode(operations), + tookInMillis + ); } } @@ -334,14 +344,24 @@ public static class TransportAction extends TransportSingleShardAction listener) throws IOException { + protected void asyncShardOperation(final Request request, final ShardId shardId, final ActionListener listener) + throws IOException { final IndexService indexService = indicesService.indexServiceSafe(request.getShard().getIndex()); final IndexShard indexShard = indexService.getShard(request.getShard().id()); final SeqNoStats seqNoStats = indexShard.seqNoStats(); if (request.getFromSeqNo() > seqNoStats.getGlobalCheckpoint()) { logger.trace( - "{} waiting for global checkpoint advancement from [{}] to [{}]", - shardId, - seqNoStats.getGlobalCheckpoint(), - request.getFromSeqNo()); - indexShard.addGlobalCheckpointListener( - request.getFromSeqNo(), - new GlobalCheckpointListeners.GlobalCheckpointListener() { - - @Override - public Executor executor() { - return threadPool.executor(Ccr.CCR_THREAD_POOL_NAME); - } + "{} waiting for global checkpoint advancement from [{}] to [{}]", + shardId, + seqNoStats.getGlobalCheckpoint(), + request.getFromSeqNo() + ); + indexShard.addGlobalCheckpointListener(request.getFromSeqNo(), new GlobalCheckpointListeners.GlobalCheckpointListener() { + + @Override + public Executor executor() { + return threadPool.executor(Ccr.CCR_THREAD_POOL_NAME); + } - @Override - public void accept(final long g, final Exception e) { - if (g != UNASSIGNED_SEQ_NO) { - assert request.getFromSeqNo() <= g - : shardId + " only advanced to [" + g + "] while waiting for [" + request.getFromSeqNo() + "]"; - globalCheckpointAdvanced(shardId, g, request, listener); - } else { - assert e != null; - globalCheckpointAdvancementFailure(shardId, e, request, listener, indexShard); - } + @Override + public void accept(final long g, final Exception e) { + if (g != UNASSIGNED_SEQ_NO) { + assert request.getFromSeqNo() <= g + : shardId + " only advanced to [" + g + "] while waiting for [" + request.getFromSeqNo() + "]"; + globalCheckpointAdvanced(shardId, g, request, listener); + } else { + assert e != null; + globalCheckpointAdvancementFailure(shardId, e, request, listener, indexShard); } + } - }, - request.getPollTimeout()); + }, request.getPollTimeout()); } else { super.asyncShardOperation(request, shardId, listener); } } private void globalCheckpointAdvanced( - final ShardId shardId, - final long globalCheckpoint, - final Request request, - final ActionListener listener) { + final ShardId shardId, + final long globalCheckpoint, + final Request request, + final ActionListener listener + ) { logger.trace("{} global checkpoint advanced to [{}] after waiting for [{}]", shardId, globalCheckpoint, request.getFromSeqNo()); try { super.asyncShardOperation(request, shardId, listener); @@ -433,15 +452,20 @@ private void globalCheckpointAdvanced( } private void globalCheckpointAdvancementFailure( - final ShardId shardId, - final Exception e, - final Request request, - final ActionListener listener, - final IndexShard indexShard) { + final ShardId shardId, + final Exception e, + final Request request, + final ActionListener listener, + final IndexShard indexShard + ) { logger.trace( - () -> new ParameterizedMessage( - "{} exception waiting for global checkpoint advancement to [{}]", shardId, request.getFromSeqNo()), - e); + () -> new ParameterizedMessage( + "{} exception waiting for global checkpoint advancement to [{}]", + shardId, + request.getFromSeqNo() + ), + e + ); if (e instanceof TimeoutException) { try { final IndexMetadata indexMetadata = clusterService.state().metadata().index(shardId.getIndex()); @@ -456,14 +480,16 @@ private void globalCheckpointAdvancementFailure( final SeqNoStats latestSeqNoStats = indexShard.seqNoStats(); final long maxSeqNoOfUpdatesOrDeletes = indexShard.getMaxSeqNoOfUpdatesOrDeletes(); listener.onResponse( - getResponse( - mappingVersion, - settingsVersion, - aliasesVersion, - latestSeqNoStats, - maxSeqNoOfUpdatesOrDeletes, - EMPTY_OPERATIONS_ARRAY, - request.relativeStartNanos)); + getResponse( + mappingVersion, + settingsVersion, + aliasesVersion, + latestSeqNoStats, + maxSeqNoOfUpdatesOrDeletes, + EMPTY_OPERATIONS_ARRAY, + request.relativeStartNanos + ) + ); } catch (final Exception caught) { caught.addSuppressed(e); listener.onFailure(caught); @@ -480,10 +506,9 @@ protected boolean resolveIndex(Request request) { @Override protected ShardsIterator shards(ClusterState state, InternalRequest request) { - return state - .routingTable() - .shardRoutingTable(request.concreteIndex(), request.request().getShard().id()) - .activeInitializingShardsRandomIt(); + return state.routingTable() + .shardRoutingTable(request.concreteIndex(), request.request().getShard().id()) + .activeInitializingShardsRandomIt(); } @Override @@ -499,7 +524,8 @@ private static void checkHistoryUUID(IndexShard indexShard, String expectedHisto final String historyUUID = indexShard.getHistoryUUID(); if (historyUUID.equals(expectedHistoryUUID) == false) { throw new IllegalStateException( - "unexpected history uuid, expected [" + expectedHistoryUUID + "], actual [" + historyUUID + "]"); + "unexpected history uuid, expected [" + expectedHistoryUUID + "], actual [" + historyUUID + "]" + ); } } @@ -520,19 +546,21 @@ private static void checkHistoryUUID(IndexShard indexShard, String expectedHisto * @throws IOException if an I/O exception occurs reading the operations */ static Translog.Operation[] getOperations( - final IndexShard indexShard, - final long globalCheckpoint, - final long fromSeqNo, - final int maxOperationCount, - final String expectedHistoryUUID, - final ByteSizeValue maxBatchSize) throws IOException { + final IndexShard indexShard, + final long globalCheckpoint, + final long fromSeqNo, + final int maxOperationCount, + final String expectedHistoryUUID, + final ByteSizeValue maxBatchSize + ) throws IOException { if (indexShard.state() != IndexShardState.STARTED) { throw new IndexShardNotStartedException(indexShard.shardId(), indexShard.state()); } checkHistoryUUID(indexShard, expectedHistoryUUID); if (fromSeqNo > globalCheckpoint) { throw new IllegalStateException( - "not exposing operations from [" + fromSeqNo + "] greater than the global checkpoint [" + globalCheckpoint + "]"); + "not exposing operations from [" + fromSeqNo + "] greater than the global checkpoint [" + globalCheckpoint + "]" + ); } int seenBytes = 0; // - 1 is needed, because toSeqNo is inclusive @@ -550,9 +578,13 @@ static Translog.Operation[] getOperations( } } catch (MissingHistoryOperationsException e) { final Collection retentionLeases = indexShard.getRetentionLeases().leases(); - final String message = "Operations are no longer available for replicating. " + - "Existing retention leases [" + retentionLeases + "]; maybe increase the retention lease period setting " + - "[" + IndexSettings.INDEX_SOFT_DELETES_RETENTION_LEASE_PERIOD_SETTING.getKey() + "]?"; + final String message = "Operations are no longer available for replicating. " + + "Existing retention leases [" + + retentionLeases + + "]; maybe increase the retention lease period setting " + + "[" + + IndexSettings.INDEX_SOFT_DELETES_RETENTION_LEASE_PERIOD_SETTING.getKey() + + "]?"; // Make it easy to detect this error in ShardFollowNodeTask: // (adding a metadata header instead of introducing a new exception that extends ElasticsearchException) ResourceNotFoundException wrapper = new ResourceNotFoundException(message, e); @@ -563,24 +595,26 @@ static Translog.Operation[] getOperations( } static Response getResponse( - final long mappingVersion, - final long settingsVersion, - final long aliasesVersion, - final SeqNoStats seqNoStats, - final long maxSeqNoOfUpdates, - final Translog.Operation[] operations, - long relativeStartNanos) { + final long mappingVersion, + final long settingsVersion, + final long aliasesVersion, + final SeqNoStats seqNoStats, + final long maxSeqNoOfUpdates, + final Translog.Operation[] operations, + long relativeStartNanos + ) { long tookInNanos = System.nanoTime() - relativeStartNanos; long tookInMillis = TimeUnit.NANOSECONDS.toMillis(tookInNanos); return new Response( - mappingVersion, - settingsVersion, - aliasesVersion, - seqNoStats.getGlobalCheckpoint(), - seqNoStats.getMaxSeqNo(), - maxSeqNoOfUpdates, - operations, - tookInMillis); + mappingVersion, + settingsVersion, + aliasesVersion, + seqNoStats.getGlobalCheckpoint(), + seqNoStats.getMaxSeqNo(), + maxSeqNoOfUpdates, + operations, + tookInMillis + ); } } diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/ShardFollowNodeTask.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/ShardFollowNodeTask.java index c3abd99905fef..541af27e6f6af 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/ShardFollowNodeTask.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/ShardFollowNodeTask.java @@ -20,10 +20,10 @@ import org.elasticsearch.cluster.block.ClusterBlockException; import org.elasticsearch.common.Randomness; import org.elasticsearch.common.breaker.CircuitBreakingException; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.transport.NetworkExceptionHelper; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException; +import org.elasticsearch.core.TimeValue; +import org.elasticsearch.core.Tuple; import org.elasticsearch.index.seqno.SequenceNumbers; import org.elasticsearch.index.shard.IllegalIndexShardStateException; import org.elasticsearch.index.shard.ShardId; @@ -108,8 +108,17 @@ synchronized Scheduler.Cancellable getRenewable() { return renewable; } - ShardFollowNodeTask(long id, String type, String action, String description, TaskId parentTask, Map headers, - ShardFollowTask params, BiConsumer scheduler, final LongSupplier relativeTimeProvider) { + ShardFollowNodeTask( + long id, + String type, + String action, + String description, + TaskId parentTask, + Map headers, + ShardFollowTask params, + BiConsumer scheduler, + final LongSupplier relativeTimeProvider + ) { super(id, type, action, description, parentTask, headers); this.params = params; this.scheduler = scheduler; @@ -132,7 +141,8 @@ void start( final long leaderGlobalCheckpoint, final long leaderMaxSeqNo, final long followerGlobalCheckpoint, - final long followerMaxSeqNo) { + final long followerMaxSeqNo + ) { /* * While this should only ever be called once and before any other threads can touch these fields, we use synchronization here to * avoid the need to declare these fields as volatile. That is, we are ensuring these fields are always accessed under the same @@ -165,17 +175,18 @@ void start( synchronized (ShardFollowNodeTask.this) { currentAliasesVersion = Math.max(currentAliasesVersion, leaderAliasesVersion); LOGGER.info( - "{} following leader shard {}, " + - "follower global checkpoint=[{}], " + - "mapping version=[{}], " + - "settings version=[{}], " + - "aliases version=[{}]", + "{} following leader shard {}, " + + "follower global checkpoint=[{}], " + + "mapping version=[{}], " + + "settings version=[{}], " + + "aliases version=[{}]", params.getFollowShardId(), params.getLeaderShardId(), followerGlobalCheckpoint, currentMappingVersion, currentSettingsVersion, - currentAliasesVersion); + currentAliasesVersion + ); } coordinateReads(); }); @@ -189,19 +200,29 @@ synchronized void coordinateReads() { return; } - LOGGER.trace("{} coordinate reads, lastRequestedSeqNo={}, leaderGlobalCheckpoint={}", - params.getFollowShardId(), lastRequestedSeqNo, leaderGlobalCheckpoint); - assert partialReadRequests.size() <= params.getMaxOutstandingReadRequests() : - "too many partial read requests [" + partialReadRequests + "]"; + LOGGER.trace( + "{} coordinate reads, lastRequestedSeqNo={}, leaderGlobalCheckpoint={}", + params.getFollowShardId(), + lastRequestedSeqNo, + leaderGlobalCheckpoint + ); + assert partialReadRequests.size() <= params.getMaxOutstandingReadRequests() + : "too many partial read requests [" + partialReadRequests + "]"; while (hasReadBudget() && partialReadRequests.isEmpty() == false) { final Tuple range = partialReadRequests.remove(); - assert range.v1() <= range.v2() && range.v2() <= lastRequestedSeqNo : - "invalid partial range [" + range.v1() + "," + range.v2() + "]; last requested seq_no [" + lastRequestedSeqNo + "]"; + assert range.v1() <= range.v2() && range.v2() <= lastRequestedSeqNo + : "invalid partial range [" + range.v1() + "," + range.v2() + "]; last requested seq_no [" + lastRequestedSeqNo + "]"; final long fromSeqNo = range.v1(); final long maxRequiredSeqNo = range.v2(); final int requestOpCount = Math.toIntExact(maxRequiredSeqNo - fromSeqNo + 1); - LOGGER.trace("{}[{} ongoing reads] continue partial read request from_seqno={} max_required_seqno={} batch_count={}", - params.getFollowShardId(), numOutstandingReads, fromSeqNo, maxRequiredSeqNo, requestOpCount); + LOGGER.trace( + "{}[{} ongoing reads] continue partial read request from_seqno={} max_required_seqno={} batch_count={}", + params.getFollowShardId(), + numOutstandingReads, + fromSeqNo, + maxRequiredSeqNo, + requestOpCount + ); numOutstandingReads++; sendShardChangesRequest(fromSeqNo, requestOpCount, maxRequiredSeqNo); } @@ -217,8 +238,14 @@ synchronized void coordinateReads() { requestOpCount = Math.toIntExact(maxRequiredSeqNo - from + 1); } assert 0 < requestOpCount && requestOpCount <= maxReadRequestOperationCount : "read_request_operation_count=" + requestOpCount; - LOGGER.trace("{}[{} ongoing reads] read from_seqno={} max_required_seqno={} batch_count={}", - params.getFollowShardId(), numOutstandingReads, from, maxRequiredSeqNo, requestOpCount); + LOGGER.trace( + "{}[{} ongoing reads] read from_seqno={} max_required_seqno={} batch_count={}", + params.getFollowShardId(), + numOutstandingReads, + from, + maxRequiredSeqNo, + requestOpCount + ); numOutstandingReads++; lastRequestedSeqNo = maxRequiredSeqNo; sendShardChangesRequest(from, requestOpCount, maxRequiredSeqNo); @@ -241,8 +268,11 @@ private boolean hasReadBudget() { // - Overestimate the size and count of the responses of the outstanding request when calculating the budget // - Limit the size and count of next read requests by the remaining size and count of the buffer if (numOutstandingReads >= params.getMaxOutstandingReadRequests()) { - LOGGER.trace("{} no new reads, maximum number of concurrent reads have been reached [{}]", - params.getFollowShardId(), numOutstandingReads); + LOGGER.trace( + "{} no new reads, maximum number of concurrent reads have been reached [{}]", + params.getFollowShardId(), + numOutstandingReads + ); return false; } if (bufferSizeInBytes >= params.getMaxWriteBufferSize().getBytes()) { @@ -276,8 +306,14 @@ private synchronized void coordinateWrites() { } bufferSizeInBytes -= sumEstimatedSize; numOutstandingWrites++; - LOGGER.trace("{}[{}] write [{}/{}] [{}]", params.getFollowShardId(), numOutstandingWrites, ops.get(0).seqNo(), - ops.get(ops.size() - 1).seqNo(), ops.size()); + LOGGER.trace( + "{}[{}] write [{}/{}] [{}]", + params.getFollowShardId(), + numOutstandingWrites, + ops.get(0).seqNo(), + ops.get(ops.size() - 1).seqNo(), + ops.size() + ); sendBulkShardOperationsRequest(ops, leaderMaxSeqNoOfUpdatesOrDeletes, new AtomicInteger(0)); } } @@ -285,8 +321,7 @@ private synchronized void coordinateWrites() { private boolean hasWriteBudget() { assert Thread.holdsLock(this); if (numOutstandingWrites >= params.getMaxOutstandingWriteRequests()) { - LOGGER.trace("{} maximum number of concurrent writes have been reached [{}]", - params.getFollowShardId(), numOutstandingWrites); + LOGGER.trace("{} maximum number of concurrent writes have been reached [{}]", params.getFollowShardId(), numOutstandingWrites); return false; } return true; @@ -301,39 +336,36 @@ private void sendShardChangesRequest(long from, int maxOperationCount, long maxR synchronized (this) { lastFetchTime = startTime; } - innerSendShardChangesRequest(from, maxOperationCount, - response -> { - synchronized (ShardFollowNodeTask.this) { - // Always clear fetch exceptions: - fetchExceptions.remove(from); - if (response.getOperations().length > 0) { - // do not count polls against fetch stats - totalReadRemoteExecTimeMillis += response.getTookInMillis(); - totalReadTimeMillis += TimeUnit.NANOSECONDS.toMillis(relativeTimeProvider.getAsLong() - startTime); - successfulReadRequests++; - operationsRead += response.getOperations().length; - bytesRead += - Arrays.stream(response.getOperations()).mapToLong(Translog.Operation::estimateSize).sum(); - } - } - handleReadResponse(from, maxRequiredSeqNo, response); - }, - e -> { - synchronized (ShardFollowNodeTask.this) { - totalReadTimeMillis += TimeUnit.NANOSECONDS.toMillis(relativeTimeProvider.getAsLong() - startTime); - failedReadRequests++; - fetchExceptions.put(from, Tuple.tuple(retryCounter, ExceptionsHelper.convertToElastic(e))); - } - Throwable cause = ExceptionsHelper.unwrapCause(e); - if (cause instanceof ResourceNotFoundException) { - ResourceNotFoundException resourceNotFoundException = (ResourceNotFoundException) cause; - if (resourceNotFoundException.getMetadataKeys().contains(Ccr.REQUESTED_OPS_MISSING_METADATA_KEY)) { - handleFallenBehindLeaderShard(e, from, maxOperationCount, maxRequiredSeqNo, retryCounter); - return; - } - } - handleFailure(e, retryCounter, () -> sendShardChangesRequest(from, maxOperationCount, maxRequiredSeqNo, retryCounter)); - }); + innerSendShardChangesRequest(from, maxOperationCount, response -> { + synchronized (ShardFollowNodeTask.this) { + // Always clear fetch exceptions: + fetchExceptions.remove(from); + if (response.getOperations().length > 0) { + // do not count polls against fetch stats + totalReadRemoteExecTimeMillis += response.getTookInMillis(); + totalReadTimeMillis += TimeUnit.NANOSECONDS.toMillis(relativeTimeProvider.getAsLong() - startTime); + successfulReadRequests++; + operationsRead += response.getOperations().length; + bytesRead += Arrays.stream(response.getOperations()).mapToLong(Translog.Operation::estimateSize).sum(); + } + } + handleReadResponse(from, maxRequiredSeqNo, response); + }, e -> { + synchronized (ShardFollowNodeTask.this) { + totalReadTimeMillis += TimeUnit.NANOSECONDS.toMillis(relativeTimeProvider.getAsLong() - startTime); + failedReadRequests++; + fetchExceptions.put(from, Tuple.tuple(retryCounter, ExceptionsHelper.convertToElastic(e))); + } + Throwable cause = ExceptionsHelper.unwrapCause(e); + if (cause instanceof ResourceNotFoundException) { + ResourceNotFoundException resourceNotFoundException = (ResourceNotFoundException) cause; + if (resourceNotFoundException.getMetadataKeys().contains(Ccr.REQUESTED_OPS_MISSING_METADATA_KEY)) { + handleFallenBehindLeaderShard(e, from, maxOperationCount, maxRequiredSeqNo, retryCounter); + return; + } + } + handleFailure(e, retryCounter, () -> sendShardChangesRequest(from, maxOperationCount, maxRequiredSeqNo, retryCounter)); + }); } void handleReadResponse(long from, long maxRequiredSeqNo, ShardChangesAction.Response response) { @@ -376,55 +408,60 @@ synchronized void innerHandleReadResponse(long from, long maxRequiredSeqNo, Shar if (response.getOperations().length == 0) { newFromSeqNo = from; } else { - assert response.getOperations()[0].seqNo() == from : - "first operation is not what we asked for. From is [" + from + "], got " + response.getOperations()[0]; + assert response.getOperations()[0].seqNo() == from + : "first operation is not what we asked for. From is [" + from + "], got " + response.getOperations()[0]; List operations = Arrays.asList(response.getOperations()); - long operationsSize = operations.stream() - .mapToLong(Translog.Operation::estimateSize) - .sum(); + long operationsSize = operations.stream().mapToLong(Translog.Operation::estimateSize).sum(); buffer.addAll(operations); bufferSizeInBytes += operationsSize; final long maxSeqNo = response.getOperations()[response.getOperations().length - 1].seqNo(); - assert maxSeqNo == - Arrays.stream(response.getOperations()).mapToLong(Translog.Operation::seqNo).max().getAsLong(); + assert maxSeqNo == Arrays.stream(response.getOperations()).mapToLong(Translog.Operation::seqNo).max().getAsLong(); newFromSeqNo = maxSeqNo + 1; // update last requested seq no as we may have gotten more than we asked for and we don't want to ask it again. lastRequestedSeqNo = Math.max(lastRequestedSeqNo, maxSeqNo); - assert lastRequestedSeqNo <= leaderGlobalCheckpoint : "lastRequestedSeqNo [" + lastRequestedSeqNo + - "] is larger than the global checkpoint [" + leaderGlobalCheckpoint + "]"; + assert lastRequestedSeqNo <= leaderGlobalCheckpoint + : "lastRequestedSeqNo [" + lastRequestedSeqNo + "] is larger than the global checkpoint [" + leaderGlobalCheckpoint + "]"; coordinateWrites(); } if (newFromSeqNo <= maxRequiredSeqNo) { - LOGGER.trace("{} received [{}] operations, enqueue partial read request [{}/{}]", - params.getFollowShardId(), response.getOperations().length, newFromSeqNo, maxRequiredSeqNo); + LOGGER.trace( + "{} received [{}] operations, enqueue partial read request [{}/{}]", + params.getFollowShardId(), + response.getOperations().length, + newFromSeqNo, + maxRequiredSeqNo + ); partialReadRequests.add(Tuple.tuple(newFromSeqNo, maxRequiredSeqNo)); } numOutstandingReads--; coordinateReads(); } - private void sendBulkShardOperationsRequest(List operations, long leaderMaxSeqNoOfUpdatesOrDeletes, - AtomicInteger retryCounter) { + private void sendBulkShardOperationsRequest( + List operations, + long leaderMaxSeqNoOfUpdatesOrDeletes, + AtomicInteger retryCounter + ) { assert leaderMaxSeqNoOfUpdatesOrDeletes != SequenceNumbers.UNASSIGNED_SEQ_NO : "mus is not replicated"; final long startTime = relativeTimeProvider.getAsLong(); - innerSendBulkShardOperationsRequest(followerHistoryUUID, operations, leaderMaxSeqNoOfUpdatesOrDeletes, - response -> { - synchronized (ShardFollowNodeTask.this) { - totalWriteTimeMillis += TimeUnit.NANOSECONDS.toMillis(relativeTimeProvider.getAsLong() - startTime); - successfulWriteRequests++; - operationWritten += operations.size(); - } - handleWriteResponse(response); - }, - e -> { - synchronized (ShardFollowNodeTask.this) { - totalWriteTimeMillis += TimeUnit.NANOSECONDS.toMillis(relativeTimeProvider.getAsLong() - startTime); - failedWriteRequests++; - } - handleFailure(e, retryCounter, - () -> sendBulkShardOperationsRequest(operations, leaderMaxSeqNoOfUpdatesOrDeletes, retryCounter)); - } - ); + innerSendBulkShardOperationsRequest(followerHistoryUUID, operations, leaderMaxSeqNoOfUpdatesOrDeletes, response -> { + synchronized (ShardFollowNodeTask.this) { + totalWriteTimeMillis += TimeUnit.NANOSECONDS.toMillis(relativeTimeProvider.getAsLong() - startTime); + successfulWriteRequests++; + operationWritten += operations.size(); + } + handleWriteResponse(response); + }, e -> { + synchronized (ShardFollowNodeTask.this) { + totalWriteTimeMillis += TimeUnit.NANOSECONDS.toMillis(relativeTimeProvider.getAsLong() - startTime); + failedWriteRequests++; + } + handleFailure( + e, + retryCounter, + () -> sendBulkShardOperationsRequest(operations, leaderMaxSeqNoOfUpdatesOrDeletes, retryCounter) + ); + }); } private synchronized void handleWriteResponse(final BulkShardOperationsResponse response) { @@ -441,12 +478,20 @@ private synchronized void handleWriteResponse(final BulkShardOperationsResponse private synchronized void maybeUpdateMapping(long minimumRequiredMappingVersion, Runnable task) { if (currentMappingVersion >= minimumRequiredMappingVersion) { - LOGGER.trace("{} mapping version [{}] is higher or equal than minimum required mapping version [{}]", - params.getFollowShardId(), currentMappingVersion, minimumRequiredMappingVersion); + LOGGER.trace( + "{} mapping version [{}] is higher or equal than minimum required mapping version [{}]", + params.getFollowShardId(), + currentMappingVersion, + minimumRequiredMappingVersion + ); task.run(); } else { - LOGGER.trace("{} updating mapping, mapping version [{}] is lower than minimum required mapping version [{}]", - params.getFollowShardId(), currentMappingVersion, minimumRequiredMappingVersion); + LOGGER.trace( + "{} updating mapping, mapping version [{}] is lower than minimum required mapping version [{}]", + params.getFollowShardId(), + currentMappingVersion, + minimumRequiredMappingVersion + ); updateMapping(minimumRequiredMappingVersion, mappingVersion -> { synchronized (ShardFollowNodeTask.this) { currentMappingVersion = Math.max(currentMappingVersion, mappingVersion); @@ -458,12 +503,20 @@ private synchronized void maybeUpdateMapping(long minimumRequiredMappingVersion, private synchronized void maybeUpdateSettings(final Long minimumRequiredSettingsVersion, Runnable task) { if (currentSettingsVersion >= minimumRequiredSettingsVersion) { - LOGGER.trace("{} settings version [{}] is higher or equal than minimum required settings version [{}]", - params.getFollowShardId(), currentSettingsVersion, minimumRequiredSettingsVersion); + LOGGER.trace( + "{} settings version [{}] is higher or equal than minimum required settings version [{}]", + params.getFollowShardId(), + currentSettingsVersion, + minimumRequiredSettingsVersion + ); task.run(); } else { - LOGGER.trace("{} updating settings, settings version [{}] is lower than minimum required settings version [{}]", - params.getFollowShardId(), currentSettingsVersion, minimumRequiredSettingsVersion); + LOGGER.trace( + "{} updating settings, settings version [{}] is lower than minimum required settings version [{}]", + params.getFollowShardId(), + currentSettingsVersion, + minimumRequiredSettingsVersion + ); updateSettings(settingsVersion -> { synchronized (ShardFollowNodeTask.this) { currentSettingsVersion = Math.max(currentSettingsVersion, settingsVersion); @@ -476,17 +529,19 @@ private synchronized void maybeUpdateSettings(final Long minimumRequiredSettings private synchronized void maybeUpdateAliases(final Long minimumRequiredAliasesVersion, final Runnable task) { if (currentAliasesVersion >= minimumRequiredAliasesVersion) { LOGGER.trace( - "{} aliases version [{}] is higher or equal than minimum required aliases version [{}]", - params.getFollowShardId(), - currentAliasesVersion, - minimumRequiredAliasesVersion); + "{} aliases version [{}] is higher or equal than minimum required aliases version [{}]", + params.getFollowShardId(), + currentAliasesVersion, + minimumRequiredAliasesVersion + ); task.run(); } else { LOGGER.trace( - "{} updating aliases, aliases version [{}] is lower than minimum required aliases version [{}]", - params.getFollowShardId(), - currentAliasesVersion, - minimumRequiredAliasesVersion); + "{} updating aliases, aliases version [{}] is lower than minimum required aliases version [{}]", + params.getFollowShardId(), + currentAliasesVersion, + minimumRequiredAliasesVersion + ); updateAliases(aliasesVersion -> { synchronized (ShardFollowNodeTask.this) { currentAliasesVersion = Math.max(currentAliasesVersion, aliasesVersion); @@ -501,8 +556,11 @@ private void updateMapping(long minRequiredMappingVersion, LongConsumer handler) } private void updateMapping(long minRequiredMappingVersion, LongConsumer handler, AtomicInteger retryCounter) { - innerUpdateMapping(minRequiredMappingVersion, handler, - e -> handleFailure(e, retryCounter, () -> updateMapping(minRequiredMappingVersion, handler, retryCounter))); + innerUpdateMapping( + minRequiredMappingVersion, + handler, + e -> handleFailure(e, retryCounter, () -> updateMapping(minRequiredMappingVersion, handler, retryCounter)) + ); } private void updateSettings(final LongConsumer handler) { @@ -527,8 +585,10 @@ private void handleFailure(Exception e, AtomicInteger retryCounter, Runnable tas if (isStopped() == false) { // Only retry is the shard follow task is not stopped. int currentRetry = retryCounter.incrementAndGet(); - LOGGER.debug(new ParameterizedMessage("{} error during follow shard task, retrying [{}]", - params.getFollowShardId(), currentRetry), e); + LOGGER.debug( + new ParameterizedMessage("{} error during follow shard task, retrying [{}]", params.getFollowShardId(), currentRetry), + e + ); long delay = computeDelay(currentRetry, params.getReadPollTimeout().getMillis()); scheduler.accept(TimeValue.timeValueMillis(delay), task); } @@ -566,20 +626,23 @@ static boolean shouldRetry(final Exception e) { } final Throwable actual = ExceptionsHelper.unwrapCause(e); - return actual instanceof ShardNotFoundException || - actual instanceof IllegalIndexShardStateException || - actual instanceof NoShardAvailableActionException || - actual instanceof UnavailableShardsException || - actual instanceof AlreadyClosedException || - actual instanceof ElasticsearchSecurityException || // If user does not have sufficient privileges - actual instanceof ClusterBlockException || // If leader index is closed or no elected master - actual instanceof IndexClosedException || // If follow index is closed - actual instanceof ConnectTransportException || - actual instanceof NodeClosedException || - actual instanceof NoSuchRemoteClusterException || - actual instanceof NoSeedNodeLeftException || - actual instanceof EsRejectedExecutionException || - actual instanceof CircuitBreakingException; + return actual instanceof ShardNotFoundException + || actual instanceof IllegalIndexShardStateException + || actual instanceof NoShardAvailableActionException + || actual instanceof UnavailableShardsException + || actual instanceof AlreadyClosedException + || actual instanceof ElasticsearchSecurityException + || // If user does not have sufficient privileges + actual instanceof ClusterBlockException + || // If leader index is closed or no elected master + actual instanceof IndexClosedException + || // If follow index is closed + actual instanceof ConnectTransportException + || actual instanceof NodeClosedException + || actual instanceof NoSuchRemoteClusterException + || actual instanceof NoSeedNodeLeftException + || actual instanceof EsRejectedExecutionException + || actual instanceof CircuitBreakingException; } // These methods are protected for testing purposes: @@ -589,14 +652,20 @@ static boolean shouldRetry(final Exception e) { protected abstract void innerUpdateAliases(LongConsumer handler, Consumer errorHandler); - protected abstract void innerSendBulkShardOperationsRequest(String followerHistoryUUID, - List operations, - long leaderMaxSeqNoOfUpdatesOrDeletes, - Consumer handler, - Consumer errorHandler); - - protected abstract void innerSendShardChangesRequest(long from, int maxOperationCount, Consumer handler, - Consumer errorHandler); + protected abstract void innerSendBulkShardOperationsRequest( + String followerHistoryUUID, + List operations, + long leaderMaxSeqNoOfUpdatesOrDeletes, + Consumer handler, + Consumer errorHandler + ); + + protected abstract void innerSendShardChangesRequest( + long from, + int maxOperationCount, + Consumer handler, + Consumer errorHandler + ); protected abstract Scheduler.Cancellable scheduleBackgroundRetentionLeaseRenewal(LongSupplier followerGlobalCheckpoint); @@ -623,46 +692,46 @@ public ShardId getFollowShardId() { public synchronized ShardFollowNodeTaskStatus getStatus() { final long timeSinceLastFetchMillis; if (lastFetchTime != -1) { - timeSinceLastFetchMillis = TimeUnit.NANOSECONDS.toMillis(relativeTimeProvider.getAsLong() - lastFetchTime); + timeSinceLastFetchMillis = TimeUnit.NANOSECONDS.toMillis(relativeTimeProvider.getAsLong() - lastFetchTime); } else { // To avoid confusion when ccr didn't yet execute a fetch: timeSinceLastFetchMillis = -1; } return new ShardFollowNodeTaskStatus( - params.getRemoteCluster(), - params.getLeaderShardId().getIndexName(), - params.getFollowShardId().getIndexName(), - getFollowShardId().getId(), - leaderGlobalCheckpoint, - leaderMaxSeqNo, - followerGlobalCheckpoint, - followerMaxSeqNo, - lastRequestedSeqNo, - numOutstandingReads, - numOutstandingWrites, - buffer.size(), - bufferSizeInBytes, - currentMappingVersion, - currentSettingsVersion, - currentAliasesVersion, - totalReadTimeMillis, - totalReadRemoteExecTimeMillis, - successfulReadRequests, - failedReadRequests, - operationsRead, - bytesRead, - totalWriteTimeMillis, - successfulWriteRequests, - failedWriteRequests, - operationWritten, - new TreeMap<>( - fetchExceptions - .entrySet() - .stream() - .collect( - Collectors.toMap(Map.Entry::getKey, e -> Tuple.tuple(e.getValue().v1().get(), e.getValue().v2())))), - timeSinceLastFetchMillis, - fatalException); + params.getRemoteCluster(), + params.getLeaderShardId().getIndexName(), + params.getFollowShardId().getIndexName(), + getFollowShardId().getId(), + leaderGlobalCheckpoint, + leaderMaxSeqNo, + followerGlobalCheckpoint, + followerMaxSeqNo, + lastRequestedSeqNo, + numOutstandingReads, + numOutstandingWrites, + buffer.size(), + bufferSizeInBytes, + currentMappingVersion, + currentSettingsVersion, + currentAliasesVersion, + totalReadTimeMillis, + totalReadRemoteExecTimeMillis, + successfulReadRequests, + failedReadRequests, + operationsRead, + bytesRead, + totalWriteTimeMillis, + successfulWriteRequests, + failedWriteRequests, + operationWritten, + new TreeMap<>( + fetchExceptions.entrySet() + .stream() + .collect(Collectors.toMap(Map.Entry::getKey, e -> Tuple.tuple(e.getValue().v1().get(), e.getValue().v2()))) + ), + timeSinceLastFetchMillis, + fatalException + ); } } diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/ShardFollowTaskCleaner.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/ShardFollowTaskCleaner.java index 0251ffa018941..09c473d87f936 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/ShardFollowTaskCleaner.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/ShardFollowTaskCleaner.java @@ -67,8 +67,12 @@ public void clusterChanged(final ClusterChangedEvent event) { continue; } IndexNotFoundException infe = new IndexNotFoundException(followerIndex); - CompletionPersistentTaskAction.Request request = - new CompletionPersistentTaskAction.Request(persistentTask.getId(), persistentTask.getAllocationId(), infe, null); + CompletionPersistentTaskAction.Request request = new CompletionPersistentTaskAction.Request( + persistentTask.getId(), + persistentTask.getAllocationId(), + infe, + null + ); threadPool.generic().submit(() -> { /* * We are executing under the system context, on behalf of the user to clean up the shard follow task after the follower diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/ShardFollowTasksExecutor.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/ShardFollowTasksExecutor.java index 87cebe6ebd7a1..07504e9ce41c0 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/ShardFollowTasksExecutor.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/ShardFollowTasksExecutor.java @@ -36,14 +36,14 @@ import org.elasticsearch.cluster.routing.IndexRoutingTable; import org.elasticsearch.cluster.routing.ShardRouting; import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.core.CheckedConsumer; import org.elasticsearch.common.settings.IndexScopedSettings; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsModule; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.CheckedConsumer; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexNotFoundException; import org.elasticsearch.index.engine.CommitStats; @@ -99,10 +99,7 @@ public class ShardFollowTasksExecutor extends PersistentTasksExecutor this.waitForMetadataTimeOut = newVal); + clusterService.getClusterSettings() + .addSettingsUpdateConsumer(CcrSettings.CCR_WAIT_FOR_METADATA_TIMEOUT, newVal -> this.waitForMetadataTimeOut = newVal); } @Override @@ -126,11 +123,14 @@ public void validate(ShardFollowTask params, ClusterState clusterState) { private static final Assignment NO_ASSIGNMENT = new Assignment(null, "no nodes found with data and remote cluster client roles"); @Override - public Assignment getAssignment(final ShardFollowTask params, - Collection candidateNodes, - final ClusterState clusterState) { + public Assignment getAssignment( + final ShardFollowTask params, + Collection candidateNodes, + final ClusterState clusterState + ) { final DiscoveryNode node = selectLeastLoadedNode( - clusterState, candidateNodes, + clusterState, + candidateNodes, ((Predicate) DiscoveryNode::canContainData).and(DiscoveryNode::isRemoteClusterClient) ); if (node == null) { @@ -141,38 +141,55 @@ public Assignment getAssignment(final ShardFollowTask params, } @Override - protected AllocatedPersistentTask createTask(long id, String type, String action, TaskId parentTaskId, - PersistentTasksCustomMetadata.PersistentTask taskInProgress, - Map headers) { + protected AllocatedPersistentTask createTask( + long id, + String type, + String action, + TaskId parentTaskId, + PersistentTasksCustomMetadata.PersistentTask taskInProgress, + Map headers + ) { ShardFollowTask params = taskInProgress.getParams(); Client followerClient = wrapClient(client, params.getHeaders()); - BiConsumer scheduler = (delay, command) -> - threadPool.scheduleUnlessShuttingDown(delay, Ccr.CCR_THREAD_POOL_NAME, command); + BiConsumer scheduler = (delay, command) -> threadPool.scheduleUnlessShuttingDown( + delay, + Ccr.CCR_THREAD_POOL_NAME, + command + ); final String recordedLeaderShardHistoryUUID = getLeaderShardHistoryUUID(params); - return new ShardFollowNodeTask(id, type, action, getDescription(taskInProgress), parentTaskId, headers, params, - scheduler, System::nanoTime) { + return new ShardFollowNodeTask( + id, + type, + action, + getDescription(taskInProgress), + parentTaskId, + headers, + params, + scheduler, + System::nanoTime + ) { @Override protected void innerUpdateMapping(long minRequiredMappingVersion, LongConsumer handler, Consumer errorHandler) { final Index followerIndex = params.getFollowShardId().getIndex(); final Index leaderIndex = params.getLeaderShardId().getIndex(); final Supplier timeout = () -> isStopped() ? TimeValue.MINUS_ONE : waitForMetadataTimeOut; - final ActionListener listener = ActionListener.wrap( - indexMetadata -> { - if (indexMetadata.mapping() == null) { - assert indexMetadata.getMappingVersion() == 1; - handler.accept(indexMetadata.getMappingVersion()); - return; - } - MappingMetadata mappingMetadata = indexMetadata.mapping(); - PutMappingRequest putMappingRequest = CcrRequests.putMappingRequest(followerIndex.getName(), mappingMetadata); - followerClient.admin().indices().putMapping(putMappingRequest, ActionListener.wrap( - putMappingResponse -> handler.accept(indexMetadata.getMappingVersion()), - errorHandler)); - }, - errorHandler - ); + final ActionListener listener = ActionListener.wrap(indexMetadata -> { + if (indexMetadata.mapping() == null) { + assert indexMetadata.getMappingVersion() == 1; + handler.accept(indexMetadata.getMappingVersion()); + return; + } + MappingMetadata mappingMetadata = indexMetadata.mapping(); + PutMappingRequest putMappingRequest = CcrRequests.putMappingRequest(followerIndex.getName(), mappingMetadata); + followerClient.admin() + .indices() + .putMapping( + putMappingRequest, + ActionListener.wrap(putMappingResponse -> handler.accept(indexMetadata.getMappingVersion()), errorHandler) + ); + }, errorHandler); try { CcrRequests.getIndexMetadata(remoteClient(params), leaderIndex, minRequiredMappingVersion, 0L, timeout, listener); } catch (NoSuchRemoteClusterException e) { @@ -216,8 +233,12 @@ protected void innerUpdateSettings(final LongConsumer finalHandler, final Consum final UpdateSettingsRequest updateSettingsRequest = new UpdateSettingsRequest(followIndex.getName()) .masterNodeTimeout(TimeValue.MAX_VALUE) .settings(updatedSettings); - followerClient.admin().indices().updateSettings(updateSettingsRequest, - ActionListener.wrap(response -> finalHandler.accept(leaderIMD.getSettingsVersion()), errorHandler)); + followerClient.admin() + .indices() + .updateSettings( + updateSettingsRequest, + ActionListener.wrap(response -> finalHandler.accept(leaderIMD.getSettingsVersion()), errorHandler) + ); } else { // If one or more setting are not dynamic then close follow index, update leader settings and // then open leader index: @@ -294,38 +315,43 @@ protected void innerUpdateAliases(final LongConsumer handler, final Consumer handler.accept(leaderIndexMetadata.getAliasesVersion()), errorHandler)); + ActionListener.wrap(r -> handler.accept(leaderIndexMetadata.getAliasesVersion()), errorHandler) + ); } }; @@ -352,10 +381,12 @@ protected void innerUpdateAliases(final LongConsumer handler, final Consumer onFailure) { + private void closeIndexUpdateSettingsAndOpenIndex( + String followIndex, + Settings updatedSettings, + Runnable handler, + Consumer onFailure + ) { CloseIndexRequest closeRequest = new CloseIndexRequest(followIndex).masterNodeTimeout(TimeValue.MAX_VALUE); CheckedConsumer onResponse = response -> { updateSettingsAndOpenIndex(followIndex, updatedSettings, handler, onFailure); @@ -363,20 +394,21 @@ private void closeIndexUpdateSettingsAndOpenIndex(String followIndex, followerClient.admin().indices().close(closeRequest, ActionListener.wrap(onResponse, onFailure)); } - private void updateSettingsAndOpenIndex(String followIndex, - Settings updatedSettings, - Runnable handler, - Consumer onFailure) { - final UpdateSettingsRequest updateSettingsRequest = new UpdateSettingsRequest(followIndex) - .masterNodeTimeout(TimeValue.MAX_VALUE); + private void updateSettingsAndOpenIndex( + String followIndex, + Settings updatedSettings, + Runnable handler, + Consumer onFailure + ) { + final UpdateSettingsRequest updateSettingsRequest = new UpdateSettingsRequest(followIndex).masterNodeTimeout( + TimeValue.MAX_VALUE + ); updateSettingsRequest.settings(updatedSettings); CheckedConsumer onResponse = response -> openIndex(followIndex, handler, onFailure); followerClient.admin().indices().updateSettings(updateSettingsRequest, ActionListener.wrap(onResponse, onFailure)); } - private void openIndex(String followIndex, - Runnable handler, - Consumer onFailure) { + private void openIndex(String followIndex, Runnable handler, Consumer onFailure) { OpenIndexRequest openIndexRequest = new OpenIndexRequest(followIndex).masterNodeTimeout(TimeValue.MAX_VALUE); CheckedConsumer onResponse = response -> handler.run(); followerClient.admin().indices().open(openIndexRequest, ActionListener.wrap(onResponse, onFailure)); @@ -388,18 +420,29 @@ protected void innerSendBulkShardOperationsRequest( final List operations, final long maxSeqNoOfUpdatesOrDeletes, final Consumer handler, - final Consumer errorHandler) { - - final BulkShardOperationsRequest request = new BulkShardOperationsRequest(params.getFollowShardId(), - followerHistoryUUID, operations, maxSeqNoOfUpdatesOrDeletes); + final Consumer errorHandler + ) { + + final BulkShardOperationsRequest request = new BulkShardOperationsRequest( + params.getFollowShardId(), + followerHistoryUUID, + operations, + maxSeqNoOfUpdatesOrDeletes + ); followerClient.execute(BulkShardOperationsAction.INSTANCE, request, ActionListener.wrap(handler::accept, errorHandler)); } @Override - protected void innerSendShardChangesRequest(long from, int maxOperationCount, Consumer handler, - Consumer errorHandler) { - ShardChangesAction.Request request = - new ShardChangesAction.Request(params.getLeaderShardId(), recordedLeaderShardHistoryUUID); + protected void innerSendShardChangesRequest( + long from, + int maxOperationCount, + Consumer handler, + Consumer errorHandler + ) { + ShardChangesAction.Request request = new ShardChangesAction.Request( + params.getLeaderShardId(), + recordedLeaderShardHistoryUUID + ); request.setFromSeqNo(from); request.setMaxOperationCount(maxOperationCount); request.setMaxBatchSize(params.getMaxReadRequestSize()); @@ -414,10 +457,11 @@ protected void innerSendShardChangesRequest(long from, int maxOperationCount, Co @Override protected Scheduler.Cancellable scheduleBackgroundRetentionLeaseRenewal(final LongSupplier followerGlobalCheckpoint) { final String retentionLeaseId = CcrRetentionLeases.retentionLeaseId( - clusterService.getClusterName().value(), - params.getFollowShardId().getIndex(), - params.getRemoteCluster(), - params.getLeaderShardId().getIndex()); + clusterService.getClusterName().value(), + params.getFollowShardId().getIndex(), + params.getRemoteCluster(), + params.getLeaderShardId().getIndex() + ); /* * We are going to attempt to renew the retention lease. If this fails it is either because the retention lease does not @@ -425,85 +469,85 @@ protected Scheduler.Cancellable scheduleBackgroundRetentionLeaseRenewal(final Lo * again. If that fails, it had better not be because the retention lease already exists. Either way, we will attempt to * renew again on the next scheduled execution. */ - final ActionListener listener = ActionListener.wrap( - r -> {}, - e -> { - /* - * We have to guard against the possibility that the shard follow node task has been stopped and the retention - * lease deliberately removed via the act of unfollowing. Note that the order of operations is important in - * TransportUnfollowAction. There, we first stop the shard follow node task, and then remove the retention - * leases on the leader. This means that if we end up here with the retention lease not existing because of an - * unfollow action, then we know that the unfollow action has already stopped the shard follow node task and - * there is no race condition with the unfollow action. - */ - if (isCancelled() || isCompleted()) { - return; - } - final Throwable cause = ExceptionsHelper.unwrapCause(e); - logRetentionLeaseFailure(retentionLeaseId, cause); - // noinspection StatementWithEmptyBody - if (cause instanceof RetentionLeaseNotFoundException) { - // note that we do not need to mark as system context here as that is restored from the original renew - logger.trace( - "{} background adding retention lease [{}] while following", - params.getFollowShardId(), - retentionLeaseId); - try { - final ActionListener wrappedListener = ActionListener.wrap( - r -> {}, - inner -> { - /* - * If this fails that the retention lease already exists, something highly unusual is - * going on. Log it, and renew again after another renew interval has passed. - */ - final Throwable innerCause = ExceptionsHelper.unwrapCause(inner); - logRetentionLeaseFailure(retentionLeaseId, innerCause); - }); - CcrRetentionLeases.asyncAddRetentionLease( - params.getLeaderShardId(), - retentionLeaseId, - followerGlobalCheckpoint.getAsLong() + 1, - remoteClient(params), - wrappedListener); - } catch (NoSuchRemoteClusterException rce) { - // we will attempt to renew again after another renew interval has passed - logRetentionLeaseFailure(retentionLeaseId, rce); - } - } else { - // if something else happened, we will attempt to renew again after another renew interval has passed - } - }); - - return threadPool.scheduleWithFixedDelay( - () -> { - final ThreadContext threadContext = threadPool.getThreadContext(); - try (ThreadContext.StoredContext ignore = threadContext.stashContext()) { - // we have to execute under the system context so that if security is enabled the management is authorized - threadContext.markAsSystemContext(); - logger.trace( - "{} background renewing retention lease [{}] while following", - params.getFollowShardId(), - retentionLeaseId); - CcrRetentionLeases.asyncRenewRetentionLease( - params.getLeaderShardId(), - retentionLeaseId, - followerGlobalCheckpoint.getAsLong() + 1, - remoteClient(params), - listener); - } - }, - retentionLeaseRenewInterval, - Ccr.CCR_THREAD_POOL_NAME); + final ActionListener listener = ActionListener.wrap(r -> {}, e -> { + /* + * We have to guard against the possibility that the shard follow node task has been stopped and the retention + * lease deliberately removed via the act of unfollowing. Note that the order of operations is important in + * TransportUnfollowAction. There, we first stop the shard follow node task, and then remove the retention + * leases on the leader. This means that if we end up here with the retention lease not existing because of an + * unfollow action, then we know that the unfollow action has already stopped the shard follow node task and + * there is no race condition with the unfollow action. + */ + if (isCancelled() || isCompleted()) { + return; + } + final Throwable cause = ExceptionsHelper.unwrapCause(e); + logRetentionLeaseFailure(retentionLeaseId, cause); + // noinspection StatementWithEmptyBody + if (cause instanceof RetentionLeaseNotFoundException) { + // note that we do not need to mark as system context here as that is restored from the original renew + logger.trace( + "{} background adding retention lease [{}] while following", + params.getFollowShardId(), + retentionLeaseId + ); + try { + final ActionListener wrappedListener = ActionListener.wrap(r -> {}, inner -> { + /* + * If this fails that the retention lease already exists, something highly unusual is + * going on. Log it, and renew again after another renew interval has passed. + */ + final Throwable innerCause = ExceptionsHelper.unwrapCause(inner); + logRetentionLeaseFailure(retentionLeaseId, innerCause); + }); + CcrRetentionLeases.asyncAddRetentionLease( + params.getLeaderShardId(), + retentionLeaseId, + followerGlobalCheckpoint.getAsLong() + 1, + remoteClient(params), + wrappedListener + ); + } catch (NoSuchRemoteClusterException rce) { + // we will attempt to renew again after another renew interval has passed + logRetentionLeaseFailure(retentionLeaseId, rce); + } + } else { + // if something else happened, we will attempt to renew again after another renew interval has passed + } + }); + + return threadPool.scheduleWithFixedDelay(() -> { + final ThreadContext threadContext = threadPool.getThreadContext(); + try (ThreadContext.StoredContext ignore = threadContext.stashContext()) { + // we have to execute under the system context so that if security is enabled the management is authorized + threadContext.markAsSystemContext(); + logger.trace( + "{} background renewing retention lease [{}] while following", + params.getFollowShardId(), + retentionLeaseId + ); + CcrRetentionLeases.asyncRenewRetentionLease( + params.getLeaderShardId(), + retentionLeaseId, + followerGlobalCheckpoint.getAsLong() + 1, + remoteClient(params), + listener + ); + } + }, retentionLeaseRenewInterval, Ccr.CCR_THREAD_POOL_NAME); } private void logRetentionLeaseFailure(final String retentionLeaseId, final Throwable cause) { assert cause instanceof ElasticsearchSecurityException == false : cause; if (cause instanceof RetentionLeaseInvalidRetainingSeqNoException == false) { - logger.warn(new ParameterizedMessage( + logger.warn( + new ParameterizedMessage( "{} background management of retention lease [{}] failed while following", params.getFollowShardId(), - retentionLeaseId), - cause); + retentionLeaseId + ), + cause + ); } } @@ -540,8 +584,13 @@ protected void nodeOperation(final AllocatedPersistentTask task, final ShardFoll } if (ShardFollowNodeTask.shouldRetry(e)) { - logger.debug(new ParameterizedMessage("failed to fetch follow shard global {} checkpoint and max sequence number", - shardFollowNodeTask), e); + logger.debug( + new ParameterizedMessage( + "failed to fetch follow shard global {} checkpoint and max sequence number", + shardFollowNodeTask + ), + e + ); try { threadPool.schedule(() -> nodeOperation(task, params, state), params.getMaxRetryDelay(), Ccr.CCR_THREAD_POOL_NAME); } catch (EsRejectedExecutionException rex) { @@ -557,10 +606,11 @@ protected void nodeOperation(final AllocatedPersistentTask task, final ShardFoll } private void fetchFollowerShardInfo( - final Client client, - final ShardId shardId, - final FollowerStatsInfoHandler handler, - final Consumer errorHandler) { + final Client client, + final ShardId shardId, + final FollowerStatsInfoHandler handler, + final Consumer errorHandler + ) { client.admin().indices().stats(new IndicesStatsRequest().indices(shardId.getIndexName()), ActionListener.wrap(r -> { IndexStats indexStats = r.getIndex(shardId.getIndexName()); if (indexStats == null) { @@ -574,9 +624,9 @@ private void fetchFollowerShardInfo( } Optional filteredShardStats = Arrays.stream(indexStats.getShards()) - .filter(shardStats -> shardStats.getShardRouting().shardId().equals(shardId)) - .filter(shardStats -> shardStats.getShardRouting().primary()) - .findAny(); + .filter(shardStats -> shardStats.getShardRouting().shardId().equals(shardId)) + .filter(shardStats -> shardStats.getShardRouting().primary()) + .findAny(); if (filteredShardStats.isPresent()) { final ShardStats shardStats = filteredShardStats.get(); final CommitStats commitStats = shardStats.getCommitStats(); diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportActivateAutoFollowPatternAction.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportActivateAutoFollowPatternAction.java index a79c63250ef42..49bdc88ee881c 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportActivateAutoFollowPatternAction.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportActivateAutoFollowPatternAction.java @@ -32,11 +32,23 @@ public class TransportActivateAutoFollowPatternAction extends AcknowledgedTransportMasterNodeAction { @Inject - public TransportActivateAutoFollowPatternAction(TransportService transportService, ClusterService clusterService, - ThreadPool threadPool, ActionFilters actionFilters, - IndexNameExpressionResolver resolver) { - super(ActivateAutoFollowPatternAction.NAME, transportService, clusterService, threadPool, actionFilters, Request::new, resolver, - ThreadPool.Names.SAME); + public TransportActivateAutoFollowPatternAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver resolver + ) { + super( + ActivateAutoFollowPatternAction.NAME, + transportService, + clusterService, + threadPool, + actionFilters, + Request::new, + resolver, + ThreadPool.Names.SAME + ); } @Override @@ -45,15 +57,21 @@ protected ClusterBlockException checkBlock(final Request request, final ClusterS } @Override - protected void masterOperation(final Task task, final Request request, final ClusterState state, - final ActionListener listener) { - clusterService.submitStateUpdateTask("activate-auto-follow-pattern-" + request.getName(), + protected void masterOperation( + final Task task, + final Request request, + final ClusterState state, + final ActionListener listener + ) { + clusterService.submitStateUpdateTask( + "activate-auto-follow-pattern-" + request.getName(), new AckedClusterStateUpdateTask(request, listener) { @Override public ClusterState execute(final ClusterState currentState) { return innerActivate(request, currentState); } - }); + } + ); } static ClusterState innerActivate(final Request request, ClusterState currentState) { @@ -73,7 +91,8 @@ static ClusterState innerActivate(final Request request, ClusterState currentSta } final Map newPatterns = new HashMap<>(patterns); - newPatterns.put(request.getName(), + newPatterns.put( + request.getName(), new AutoFollowMetadata.AutoFollowPattern( previousAutoFollowPattern.getRemoteCluster(), previousAutoFollowPattern.getLeaderIndexPatterns(), @@ -90,13 +109,23 @@ static ClusterState innerActivate(final Request request, ClusterState currentSta previousAutoFollowPattern.getMaxWriteBufferCount(), previousAutoFollowPattern.getMaxWriteBufferSize(), previousAutoFollowPattern.getMaxRetryDelay(), - previousAutoFollowPattern.getReadPollTimeout())); + previousAutoFollowPattern.getReadPollTimeout() + ) + ); return ClusterState.builder(currentState) - .metadata(Metadata.builder(currentState.getMetadata()) - .putCustom(AutoFollowMetadata.TYPE, - new AutoFollowMetadata(newPatterns, autoFollowMetadata.getFollowedLeaderIndexUUIDs(), autoFollowMetadata.getHeaders())) - .build()) + .metadata( + Metadata.builder(currentState.getMetadata()) + .putCustom( + AutoFollowMetadata.TYPE, + new AutoFollowMetadata( + newPatterns, + autoFollowMetadata.getFollowedLeaderIndexUUIDs(), + autoFollowMetadata.getHeaders() + ) + ) + .build() + ) .build(); } } diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportCcrStatsAction.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportCcrStatsAction.java index 3d90f5f2e8ada..4e80d8ff2cb31 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportCcrStatsAction.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportCcrStatsAction.java @@ -16,8 +16,8 @@ import org.elasticsearch.cluster.block.ClusterBlockLevel; import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.core.CheckedConsumer; import org.elasticsearch.common.inject.Inject; +import org.elasticsearch.core.CheckedConsumer; import org.elasticsearch.license.LicenseUtils; import org.elasticsearch.tasks.Task; import org.elasticsearch.threadpool.ThreadPool; @@ -38,14 +38,14 @@ public class TransportCcrStatsAction extends TransportMasterNodeAction listener ) throws Exception { diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportDeleteAutoFollowPatternAction.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportDeleteAutoFollowPatternAction.java index 13f1875a68b08..1b7dacec4ec43 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportDeleteAutoFollowPatternAction.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportDeleteAutoFollowPatternAction.java @@ -33,42 +33,58 @@ public class TransportDeleteAutoFollowPatternAction extends AcknowledgedTransportMasterNodeAction { @Inject - public TransportDeleteAutoFollowPatternAction(TransportService transportService, ClusterService clusterService, - ThreadPool threadPool, ActionFilters actionFilters, - IndexNameExpressionResolver indexNameExpressionResolver) { - super(DeleteAutoFollowPatternAction.NAME, transportService, clusterService, threadPool, actionFilters, - DeleteAutoFollowPatternAction.Request::new, indexNameExpressionResolver, ThreadPool.Names.SAME); + public TransportDeleteAutoFollowPatternAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver + ) { + super( + DeleteAutoFollowPatternAction.NAME, + transportService, + clusterService, + threadPool, + actionFilters, + DeleteAutoFollowPatternAction.Request::new, + indexNameExpressionResolver, + ThreadPool.Names.SAME + ); } @Override - protected void masterOperation(Task task, DeleteAutoFollowPatternAction.Request request, - ClusterState state, - ActionListener listener) { - clusterService.submitStateUpdateTask("put-auto-follow-pattern-" + request.getName(), + protected void masterOperation( + Task task, + DeleteAutoFollowPatternAction.Request request, + ClusterState state, + ActionListener listener + ) { + clusterService.submitStateUpdateTask( + "put-auto-follow-pattern-" + request.getName(), new AckedClusterStateUpdateTask(request, listener) { @Override public ClusterState execute(ClusterState currentState) { return innerDelete(request, currentState); } - }); + } + ); } static ClusterState innerDelete(DeleteAutoFollowPatternAction.Request request, ClusterState currentState) { AutoFollowMetadata currentAutoFollowMetadata = currentState.metadata().custom(AutoFollowMetadata.TYPE); if (currentAutoFollowMetadata == null) { - throw new ResourceNotFoundException("auto-follow pattern [{}] is missing", - request.getName()); + throw new ResourceNotFoundException("auto-follow pattern [{}] is missing", request.getName()); } Map patterns = currentAutoFollowMetadata.getPatterns(); AutoFollowPattern autoFollowPatternToRemove = patterns.get(request.getName()); if (autoFollowPatternToRemove == null) { - throw new ResourceNotFoundException("auto-follow pattern [{}] is missing", - request.getName()); + throw new ResourceNotFoundException("auto-follow pattern [{}] is missing", request.getName()); } final Map patternsCopy = new HashMap<>(patterns); - final Map> followedLeaderIndexUUIDSCopy = - new HashMap<>(currentAutoFollowMetadata.getFollowedLeaderIndexUUIDs()); + final Map> followedLeaderIndexUUIDSCopy = new HashMap<>( + currentAutoFollowMetadata.getFollowedLeaderIndexUUIDs() + ); final Map> headers = new HashMap<>(currentAutoFollowMetadata.getHeaders()); patternsCopy.remove(request.getName()); followedLeaderIndexUUIDSCopy.remove(request.getName()); @@ -76,9 +92,7 @@ static ClusterState innerDelete(DeleteAutoFollowPatternAction.Request request, C AutoFollowMetadata newAutoFollowMetadata = new AutoFollowMetadata(patternsCopy, followedLeaderIndexUUIDSCopy, headers); ClusterState.Builder newState = ClusterState.builder(currentState); - newState.metadata(Metadata.builder(currentState.getMetadata()) - .putCustom(AutoFollowMetadata.TYPE, newAutoFollowMetadata) - .build()); + newState.metadata(Metadata.builder(currentState.getMetadata()).putCustom(AutoFollowMetadata.TYPE, newAutoFollowMetadata).build()); return newState.build(); } diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportFollowInfoAction.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportFollowInfoAction.java index 7505ce93cf54a..f261417c40596 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportFollowInfoAction.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportFollowInfoAction.java @@ -37,19 +37,37 @@ public class TransportFollowInfoAction extends TransportMasterNodeReadAction { @Inject - public TransportFollowInfoAction(TransportService transportService, ClusterService clusterService, ThreadPool threadPool, - ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver) { - super(FollowInfoAction.NAME, transportService, clusterService, threadPool, actionFilters, FollowInfoAction.Request::new, - indexNameExpressionResolver, FollowInfoAction.Response::new, ThreadPool.Names.SAME); + public TransportFollowInfoAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver + ) { + super( + FollowInfoAction.NAME, + transportService, + clusterService, + threadPool, + actionFilters, + FollowInfoAction.Request::new, + indexNameExpressionResolver, + FollowInfoAction.Response::new, + ThreadPool.Names.SAME + ); } @Override - protected void masterOperation(Task task, FollowInfoAction.Request request, - ClusterState state, - ActionListener listener) throws Exception { + protected void masterOperation( + Task task, + FollowInfoAction.Request request, + ClusterState state, + ActionListener listener + ) throws Exception { - List concreteFollowerIndices = Arrays.asList(indexNameExpressionResolver.concreteIndexNames(state, - IndicesOptions.STRICT_EXPAND_OPEN_CLOSED, request.getFollowerIndices())); + List concreteFollowerIndices = Arrays.asList( + indexNameExpressionResolver.concreteIndexNames(state, IndicesOptions.STRICT_EXPAND_OPEN_CLOSED, request.getFollowerIndices()) + ); List followerInfos = getFollowInfos(concreteFollowerIndices, state); listener.onResponse(new FollowInfoAction.Response(followerInfos)); @@ -70,7 +88,8 @@ static List getFollowInfos(List concreteFollowerIndices, C if (ccrCustomData != null) { Optional result; if (persistentTasks != null) { - result = persistentTasks.findTasks(ShardFollowTask.NAME, task -> true).stream() + result = persistentTasks.findTasks(ShardFollowTask.NAME, task -> true) + .stream() .map(task -> (ShardFollowTask) task.getParams()) .filter(shardFollowTask -> index.equals(shardFollowTask.getFollowShardId().getIndexName())) .findAny(); diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportFollowStatsAction.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportFollowStatsAction.java index 452ecc861eb49..c5004f7c995d0 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportFollowStatsAction.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportFollowStatsAction.java @@ -38,35 +38,39 @@ import java.util.stream.Collectors; public class TransportFollowStatsAction extends TransportTasksAction< - ShardFollowNodeTask, - FollowStatsAction.StatsRequest, - FollowStatsAction.StatsResponses, FollowStatsAction.StatsResponse> { + ShardFollowNodeTask, + FollowStatsAction.StatsRequest, + FollowStatsAction.StatsResponses, + FollowStatsAction.StatsResponse> { private final CcrLicenseChecker ccrLicenseChecker; @Inject public TransportFollowStatsAction( - final ClusterService clusterService, - final TransportService transportService, - final ActionFilters actionFilters, - final CcrLicenseChecker ccrLicenseChecker) { + final ClusterService clusterService, + final TransportService transportService, + final ActionFilters actionFilters, + final CcrLicenseChecker ccrLicenseChecker + ) { super( - FollowStatsAction.NAME, - clusterService, - transportService, - actionFilters, - FollowStatsAction.StatsRequest::new, - FollowStatsAction.StatsResponses::new, - FollowStatsAction.StatsResponse::new, - Ccr.CCR_THREAD_POOL_NAME); + FollowStatsAction.NAME, + clusterService, + transportService, + actionFilters, + FollowStatsAction.StatsRequest::new, + FollowStatsAction.StatsResponses::new, + FollowStatsAction.StatsResponse::new, + Ccr.CCR_THREAD_POOL_NAME + ); this.ccrLicenseChecker = Objects.requireNonNull(ccrLicenseChecker); } @Override protected void doExecute( - final Task task, - final FollowStatsAction.StatsRequest request, - final ActionListener listener) { + final Task task, + final FollowStatsAction.StatsRequest request, + final ActionListener listener + ) { if (ccrLicenseChecker.isCcrAllowed() == false) { listener.onFailure(LicenseUtils.newComplianceException("ccr")); return; @@ -85,10 +89,11 @@ protected void doExecute( @Override protected FollowStatsAction.StatsResponses newResponse( - final FollowStatsAction.StatsRequest request, - final List statsRespons, - final List taskOperationFailures, - final List failedNodeExceptions) { + final FollowStatsAction.StatsRequest request, + final List statsRespons, + final List taskOperationFailures, + final List failedNodeExceptions + ) { return new FollowStatsAction.StatsResponses(taskOperationFailures, failedNodeExceptions, statsRespons); } @@ -109,9 +114,10 @@ protected void processTasks(final FollowStatsAction.StatsRequest request, final @Override protected void taskOperation( - final FollowStatsAction.StatsRequest request, - final ShardFollowNodeTask task, - final ActionListener listener) { + final FollowStatsAction.StatsRequest request, + final ShardFollowNodeTask task, + final ActionListener listener + ) { listener.onResponse(new FollowStatsAction.StatsResponse(task.getStatus())); } @@ -121,9 +127,9 @@ static Set findFollowerIndicesFromShardFollowTasks(ClusterState state, S return Collections.emptySet(); } final Metadata metadata = state.metadata(); - final Set requestedFollowerIndices = indices != null ? - new HashSet<>(Arrays.asList(indices)) : Collections.emptySet(); - return persistentTasksMetadata.tasks().stream() + final Set requestedFollowerIndices = indices != null ? new HashSet<>(Arrays.asList(indices)) : Collections.emptySet(); + return persistentTasksMetadata.tasks() + .stream() .filter(persistentTask -> persistentTask.getTaskName().equals(ShardFollowTask.NAME)) .map(persistentTask -> { ShardFollowTask shardFollowTask = (ShardFollowTask) persistentTask.getParams(); diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportForgetFollowerAction.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportForgetFollowerAction.java index df94b607bc3d1..70eeff821d1fe 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportForgetFollowerAction.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportForgetFollowerAction.java @@ -42,28 +42,30 @@ import java.util.Objects; public class TransportForgetFollowerAction extends TransportBroadcastByNodeAction< - ForgetFollowerAction.Request, - BroadcastResponse, - TransportBroadcastByNodeAction.EmptyResult> { + ForgetFollowerAction.Request, + BroadcastResponse, + TransportBroadcastByNodeAction.EmptyResult> { private final ClusterService clusterService; private final IndicesService indicesService; @Inject public TransportForgetFollowerAction( - final ClusterService clusterService, - final TransportService transportService, - final ActionFilters actionFilters, - final IndexNameExpressionResolver indexNameExpressionResolver, - final IndicesService indicesService) { + final ClusterService clusterService, + final TransportService transportService, + final ActionFilters actionFilters, + final IndexNameExpressionResolver indexNameExpressionResolver, + final IndicesService indicesService + ) { super( - ForgetFollowerAction.NAME, - Objects.requireNonNull(clusterService), - Objects.requireNonNull(transportService), - Objects.requireNonNull(actionFilters), - Objects.requireNonNull(indexNameExpressionResolver), - ForgetFollowerAction.Request::new, - ThreadPool.Names.MANAGEMENT); + ForgetFollowerAction.NAME, + Objects.requireNonNull(clusterService), + Objects.requireNonNull(transportService), + Objects.requireNonNull(actionFilters), + Objects.requireNonNull(indexNameExpressionResolver), + ForgetFollowerAction.Request::new, + ThreadPool.Names.MANAGEMENT + ); this.clusterService = clusterService; this.indicesService = Objects.requireNonNull(indicesService); } @@ -75,12 +77,14 @@ protected EmptyResult readShardResult(final StreamInput in) { @Override protected BroadcastResponse newResponse( - final ForgetFollowerAction.Request request, - final int totalShards, - final int successfulShards, - final int failedShards, List emptyResults, - final List shardFailures, - final ClusterState clusterState) { + final ForgetFollowerAction.Request request, + final int totalShards, + final int successfulShards, + final int failedShards, + List emptyResults, + final List shardFailures, + final ClusterState clusterState + ) { return new BroadcastResponse(totalShards, successfulShards, failedShards, shardFailures); } @@ -90,15 +94,20 @@ protected ForgetFollowerAction.Request readRequestFrom(final StreamInput in) thr } @Override - protected void shardOperation(final ForgetFollowerAction.Request request, final ShardRouting shardRouting, Task task, - ActionListener listener) { + protected void shardOperation( + final ForgetFollowerAction.Request request, + final ShardRouting shardRouting, + Task task, + ActionListener listener + ) { final Index followerIndex = new Index(request.followerIndex(), request.followerIndexUUID()); final Index leaderIndex = clusterService.state().metadata().index(request.leaderIndex()).getIndex(); final String id = CcrRetentionLeases.retentionLeaseId( request.followerCluster(), followerIndex, request.leaderRemoteCluster(), - leaderIndex); + leaderIndex + ); final IndexShard indexShard = indicesService.indexServiceSafe(leaderIndex).getShard(shardRouting.shardId().id()); @@ -129,11 +138,12 @@ public void onFailure(Exception e) { @Override protected ShardsIterator shards( - final ClusterState clusterState, - final ForgetFollowerAction.Request request, - final String[] concreteIndices) { - final GroupShardsIterator activePrimaryShards = - clusterState.routingTable().activePrimaryShardsGrouped(concreteIndices, false); + final ClusterState clusterState, + final ForgetFollowerAction.Request request, + final String[] concreteIndices + ) { + final GroupShardsIterator activePrimaryShards = clusterState.routingTable() + .activePrimaryShardsGrouped(concreteIndices, false); final List shardRoutings = new ArrayList<>(); final Iterator it = activePrimaryShards.iterator(); while (it.hasNext()) { @@ -156,9 +166,10 @@ protected ClusterBlockException checkGlobalBlock(final ClusterState state, final @Override protected ClusterBlockException checkRequestBlock( - final ClusterState state, - final ForgetFollowerAction.Request request, - final String[] concreteIndices) { + final ClusterState state, + final ForgetFollowerAction.Request request, + final String[] concreteIndices + ) { return null; } } diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportGetAutoFollowPatternAction.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportGetAutoFollowPatternAction.java index b4d86ef1715a9..69f5029d8d9d9 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportGetAutoFollowPatternAction.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportGetAutoFollowPatternAction.java @@ -28,24 +28,38 @@ import java.util.Collections; import java.util.Map; -public class TransportGetAutoFollowPatternAction - extends TransportMasterNodeReadAction { +public class TransportGetAutoFollowPatternAction extends TransportMasterNodeReadAction< + GetAutoFollowPatternAction.Request, + GetAutoFollowPatternAction.Response> { @Inject - public TransportGetAutoFollowPatternAction(TransportService transportService, - ClusterService clusterService, - ThreadPool threadPool, - ActionFilters actionFilters, - IndexNameExpressionResolver indexNameExpressionResolver) { - super(GetAutoFollowPatternAction.NAME, transportService, clusterService, threadPool, actionFilters, - GetAutoFollowPatternAction.Request::new, indexNameExpressionResolver, GetAutoFollowPatternAction.Response::new, - ThreadPool.Names.SAME); + public TransportGetAutoFollowPatternAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver + ) { + super( + GetAutoFollowPatternAction.NAME, + transportService, + clusterService, + threadPool, + actionFilters, + GetAutoFollowPatternAction.Request::new, + indexNameExpressionResolver, + GetAutoFollowPatternAction.Response::new, + ThreadPool.Names.SAME + ); } @Override - protected void masterOperation(Task task, GetAutoFollowPatternAction.Request request, - ClusterState state, - ActionListener listener) throws Exception { + protected void masterOperation( + Task task, + GetAutoFollowPatternAction.Request request, + ClusterState state, + ActionListener listener + ) throws Exception { Map autoFollowPatterns = getAutoFollowPattern(state.metadata(), request.getName()); listener.onResponse(new GetAutoFollowPatternAction.Response(autoFollowPatterns)); } diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportPauseFollowAction.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportPauseFollowAction.java index 712c6439588b5..8076fa27a310e 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportPauseFollowAction.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportPauseFollowAction.java @@ -37,20 +37,33 @@ public class TransportPauseFollowAction extends AcknowledgedTransportMasterNodeA @Inject public TransportPauseFollowAction( - final TransportService transportService, - final ActionFilters actionFilters, - final ClusterService clusterService, - final ThreadPool threadPool, - final IndexNameExpressionResolver indexNameExpressionResolver, - final PersistentTasksService persistentTasksService) { - super(PauseFollowAction.NAME, transportService, clusterService, threadPool, actionFilters, - PauseFollowAction.Request::new, indexNameExpressionResolver, ThreadPool.Names.SAME); + final TransportService transportService, + final ActionFilters actionFilters, + final ClusterService clusterService, + final ThreadPool threadPool, + final IndexNameExpressionResolver indexNameExpressionResolver, + final PersistentTasksService persistentTasksService + ) { + super( + PauseFollowAction.NAME, + transportService, + clusterService, + threadPool, + actionFilters, + PauseFollowAction.Request::new, + indexNameExpressionResolver, + ThreadPool.Names.SAME + ); this.persistentTasksService = persistentTasksService; } @Override - protected void masterOperation(Task task, PauseFollowAction.Request request, - ClusterState state, ActionListener listener) { + protected void masterOperation( + Task task, + PauseFollowAction.Request request, + ClusterState state, + ActionListener listener + ) { final IndexMetadata followerIMD = state.metadata().index(request.getFollowIndex()); if (followerIMD == null) { listener.onFailure(new IndexNotFoundException(request.getFollowIndex())); @@ -66,7 +79,8 @@ protected void masterOperation(Task task, PauseFollowAction.Request request, return; } - List shardFollowTaskIds = persistentTasksMetadata.tasks().stream() + List shardFollowTaskIds = persistentTasksMetadata.tasks() + .stream() .filter(persistentTask -> ShardFollowTask.NAME.equals(persistentTask.getTaskName())) .filter(persistentTask -> { ShardFollowTask shardFollowTask = (ShardFollowTask) persistentTask.getParams(); diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportPutAutoFollowPatternAction.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportPutAutoFollowPatternAction.java index 933ff36c4cf75..eac3321ea2c14 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportPutAutoFollowPatternAction.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportPutAutoFollowPatternAction.java @@ -49,23 +49,35 @@ public class TransportPutAutoFollowPatternAction extends AcknowledgedTransportMa @Inject public TransportPutAutoFollowPatternAction( - final TransportService transportService, - final ClusterService clusterService, - final ThreadPool threadPool, - final ActionFilters actionFilters, - final Client client, - final IndexNameExpressionResolver indexNameExpressionResolver, - final CcrLicenseChecker ccrLicenseChecker) { - super(PutAutoFollowPatternAction.NAME, transportService, clusterService, threadPool, actionFilters, - PutAutoFollowPatternAction.Request::new, indexNameExpressionResolver, ThreadPool.Names.SAME); + final TransportService transportService, + final ClusterService clusterService, + final ThreadPool threadPool, + final ActionFilters actionFilters, + final Client client, + final IndexNameExpressionResolver indexNameExpressionResolver, + final CcrLicenseChecker ccrLicenseChecker + ) { + super( + PutAutoFollowPatternAction.NAME, + transportService, + clusterService, + threadPool, + actionFilters, + PutAutoFollowPatternAction.Request::new, + indexNameExpressionResolver, + ThreadPool.Names.SAME + ); this.client = client; this.ccrLicenseChecker = Objects.requireNonNull(ccrLicenseChecker, "ccrLicenseChecker"); } @Override - protected void masterOperation(Task task, PutAutoFollowPatternAction.Request request, - ClusterState state, - ActionListener listener) { + protected void masterOperation( + Task task, + PutAutoFollowPatternAction.Request request, + ClusterState state, + ActionListener listener + ) { if (ccrLicenseChecker.isCcrAllowed() == false) { listener.onFailure(LicenseUtils.newComplianceException("ccr")); return; @@ -88,13 +100,15 @@ protected void masterOperation(Task task, PutAutoFollowPatternAction.Request req String[] indices = request.getLeaderIndexPatterns().toArray(new String[0]); ccrLicenseChecker.hasPrivilegesToFollowIndices(remoteClient, indices, e -> { if (e == null) { - clusterService.submitStateUpdateTask("put-auto-follow-pattern-" + request.getRemoteCluster(), + clusterService.submitStateUpdateTask( + "put-auto-follow-pattern-" + request.getRemoteCluster(), new AckedClusterStateUpdateTask(request, listener) { @Override public ClusterState execute(ClusterState currentState) { return innerPut(request, filteredHeaders, currentState, remoteClusterState.getState()); } - }); + } + ); } else { listener.onFailure(e); } @@ -105,15 +119,22 @@ public ClusterState execute(ClusterState currentState) { clusterStateRequest.clear(); clusterStateRequest.metadata(true); - ccrLicenseChecker.checkRemoteClusterLicenseAndFetchClusterState(client, request.getRemoteCluster(), - clusterStateRequest, listener::onFailure, consumer); + ccrLicenseChecker.checkRemoteClusterLicenseAndFetchClusterState( + client, + request.getRemoteCluster(), + clusterStateRequest, + listener::onFailure, + consumer + ); } - static ClusterState innerPut(PutAutoFollowPatternAction.Request request, - Map filteredHeaders, - ClusterState localState, - ClusterState remoteClusterState) { + static ClusterState innerPut( + PutAutoFollowPatternAction.Request request, + Map filteredHeaders, + ClusterState localState, + ClusterState remoteClusterState + ) { // auto patterns are always overwritten // only already followed index uuids are updated @@ -141,14 +162,16 @@ static ClusterState innerPut(PutAutoFollowPatternAction.Request request, followedLeaderIndices.put(request.getName(), followedIndexUUIDs); // Mark existing leader indices as already auto followed: if (previousPattern != null) { - markExistingIndicesAsAutoFollowedForNewPatterns(request.getLeaderIndexPatterns(), + markExistingIndicesAsAutoFollowedForNewPatterns( + request.getLeaderIndexPatterns(), request.getLeaderIndexExclusionPatterns(), remoteClusterState.metadata(), previousPattern, followedIndexUUIDs ); } else { - markExistingIndicesAsAutoFollowed(request.getLeaderIndexPatterns(), + markExistingIndicesAsAutoFollowed( + request.getLeaderIndexPatterns(), request.getLeaderIndexExclusionPatterns(), remoteClusterState.metadata(), followedIndexUUIDs @@ -175,12 +198,15 @@ static ClusterState innerPut(PutAutoFollowPatternAction.Request request, request.getParameters().getMaxWriteBufferCount(), request.getParameters().getMaxWriteBufferSize(), request.getParameters().getMaxRetryDelay(), - request.getParameters().getReadPollTimeout()); + request.getParameters().getReadPollTimeout() + ); patterns.put(request.getName(), autoFollowPattern); ClusterState.Builder newState = ClusterState.builder(localState); - newState.metadata(Metadata.builder(localState.getMetadata()) - .putCustom(AutoFollowMetadata.TYPE, new AutoFollowMetadata(patterns, followedLeaderIndices, headers)) - .build()); + newState.metadata( + Metadata.builder(localState.getMetadata()) + .putCustom(AutoFollowMetadata.TYPE, new AutoFollowMetadata(patterns, followedLeaderIndices, headers)) + .build() + ); return newState.build(); } @@ -189,10 +215,10 @@ private static void markExistingIndicesAsAutoFollowedForNewPatterns( List leaderIndexExclusionPatterns, Metadata leaderMetadata, AutoFollowPattern previousPattern, - List followedIndexUUIDS) { + List followedIndexUUIDS + ) { - final List newPatterns = leaderIndexPatterns - .stream() + final List newPatterns = leaderIndexPatterns.stream() .filter(p -> previousPattern.getLeaderIndexPatterns().contains(p) == false) .collect(Collectors.toList()); markExistingIndicesAsAutoFollowed(newPatterns, leaderIndexExclusionPatterns, leaderMetadata, followedIndexUUIDS); @@ -202,7 +228,8 @@ private static void markExistingIndicesAsAutoFollowed( List patterns, List exclusionPatterns, Metadata leaderMetadata, - List followedIndexUUIDS) { + List followedIndexUUIDS + ) { for (final IndexMetadata indexMetadata : leaderMetadata) { IndexAbstraction indexAbstraction = leaderMetadata.getIndicesLookup().get(indexMetadata.getIndex().getName()); diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportPutFollowAction.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportPutFollowAction.java index fc1c43e147a37..16931cd330a3c 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportPutFollowAction.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportPutFollowAction.java @@ -54,8 +54,7 @@ import java.util.function.BiConsumer; import java.util.stream.Collectors; -public final class TransportPutFollowAction - extends TransportMasterNodeAction { +public final class TransportPutFollowAction extends TransportMasterNodeAction { private static final Logger logger = LogManager.getLogger(TransportPutFollowAction.class); @@ -67,25 +66,27 @@ public final class TransportPutFollowAction @Inject public TransportPutFollowAction( - final ThreadPool threadPool, - final TransportService transportService, - final ClusterService clusterService, - final IndexScopedSettings indexScopedSettings, - final ActionFilters actionFilters, - final IndexNameExpressionResolver indexNameExpressionResolver, - final Client client, - final RestoreService restoreService, - final CcrLicenseChecker ccrLicenseChecker) { + final ThreadPool threadPool, + final TransportService transportService, + final ClusterService clusterService, + final IndexScopedSettings indexScopedSettings, + final ActionFilters actionFilters, + final IndexNameExpressionResolver indexNameExpressionResolver, + final Client client, + final RestoreService restoreService, + final CcrLicenseChecker ccrLicenseChecker + ) { super( - PutFollowAction.NAME, - transportService, - clusterService, - threadPool, - actionFilters, - PutFollowAction.Request::new, - indexNameExpressionResolver, - PutFollowAction.Response::new, - ThreadPool.Names.SAME); + PutFollowAction.NAME, + transportService, + clusterService, + threadPool, + actionFilters, + PutFollowAction.Request::new, + indexNameExpressionResolver, + PutFollowAction.Response::new, + ThreadPool.Names.SAME + ); this.indexScopedSettings = indexScopedSettings; this.client = client; this.restoreService = restoreService; @@ -95,9 +96,11 @@ public TransportPutFollowAction( @Override protected void masterOperation( - Task task, final PutFollowAction.Request request, + Task task, + final PutFollowAction.Request request, final ClusterState state, - final ActionListener listener) { + final ActionListener listener + ) { if (ccrLicenseChecker.isCcrAllowed() == false) { listener.onFailure(LicenseUtils.newComplianceException("ccr")); return; @@ -112,33 +115,43 @@ protected void masterOperation( remoteCluster, leaderIndex, listener::onFailure, - (historyUUID, tuple) -> createFollowerIndex(tuple.v1(), tuple.v2(), request, listener)); + (historyUUID, tuple) -> createFollowerIndex(tuple.v1(), tuple.v2(), request, listener) + ); } private void createFollowerIndex( - final IndexMetadata leaderIndexMetadata, - final DataStream remoteDataStream, - final PutFollowAction.Request request, - final ActionListener listener) { + final IndexMetadata leaderIndexMetadata, + final DataStream remoteDataStream, + final PutFollowAction.Request request, + final ActionListener listener + ) { if (leaderIndexMetadata == null) { listener.onFailure(new IllegalArgumentException("leader index [" + request.getLeaderIndex() + "] does not exist")); return; } if (IndexSettings.INDEX_SOFT_DELETES_SETTING.get(leaderIndexMetadata.getSettings()) == false) { - listener.onFailure(new IllegalArgumentException("leader index [" + request.getLeaderIndex() + - "] does not have soft deletes enabled")); + listener.onFailure( + new IllegalArgumentException("leader index [" + request.getLeaderIndex() + "] does not have soft deletes enabled") + ); return; } if (SearchableSnapshotsSettings.isSearchableSnapshotStore(leaderIndexMetadata.getSettings())) { - listener.onFailure(new IllegalArgumentException("leader index [" + request.getLeaderIndex() + - "] is a searchable snapshot index and cannot be used as a leader index for cross-cluster replication purpose")); + listener.onFailure( + new IllegalArgumentException( + "leader index [" + + request.getLeaderIndex() + + "] is a searchable snapshot index and cannot be used as a leader index for cross-cluster replication purpose" + ) + ); return; } final Settings replicatedRequestSettings = TransportResumeFollowAction.filter(request.getSettings()); if (replicatedRequestSettings.isEmpty() == false) { - final List unknownKeys = - replicatedRequestSettings.keySet().stream().filter(s -> indexScopedSettings.get(s) == null).collect(Collectors.toList()); + final List unknownKeys = replicatedRequestSettings.keySet() + .stream() + .filter(s -> indexScopedSettings.get(s) == null) + .collect(Collectors.toList()); final String message; if (unknownKeys.isEmpty()) { message = String.format( @@ -163,7 +176,8 @@ private void createFollowerIndex( // and remote cluster. if (request.getLeaderIndex().equals(request.getFollowerIndex()) == false) { listener.onFailure( - new IllegalArgumentException("a backing index name in the local and remote cluster must remain the same")); + new IllegalArgumentException("a backing index name in the local and remote cluster must remain the same") + ); return; } } @@ -175,9 +189,13 @@ private void createFollowerIndex( .build(); final String leaderClusterRepoName = CcrRepository.NAME_PREFIX + request.getRemoteCluster(); - final RestoreSnapshotRequest restoreRequest = new RestoreSnapshotRequest(leaderClusterRepoName, CcrRepository.LATEST) - .indices(request.getLeaderIndex()).indicesOptions(request.indicesOptions()).renamePattern("^(.*)$") - .renameReplacement(request.getFollowerIndex()).masterNodeTimeout(request.masterNodeTimeout()) + final RestoreSnapshotRequest restoreRequest = new RestoreSnapshotRequest(leaderClusterRepoName, CcrRepository.LATEST).indices( + request.getLeaderIndex() + ) + .indicesOptions(request.indicesOptions()) + .renamePattern("^(.*)$") + .renameReplacement(request.getFollowerIndex()) + .masterNodeTimeout(request.masterNodeTimeout()) .indexSettings(overrideSettings); final Client clientWithHeaders = CcrLicenseChecker.wrapClient(this.client, threadPool.getThreadContext().getHeaders()); @@ -191,7 +209,8 @@ public void onFailure(Exception e) { @Override protected void doRun() { ActionListener delegatelistener = listener.delegateFailure( - (delegatedListener, response) -> afterRestoreStarted(clientWithHeaders, request, delegatedListener, response)); + (delegatedListener, response) -> afterRestoreStarted(clientWithHeaders, request, delegatedListener, response) + ); if (remoteDataStream == null) { restoreService.restoreSnapshot(restoreRequest, delegatelistener); } else { @@ -210,9 +229,12 @@ protected void doRun() { }); } - private void afterRestoreStarted(Client clientWithHeaders, PutFollowAction.Request request, - ActionListener originalListener, - RestoreService.RestoreCompletionResponse response) { + private void afterRestoreStarted( + Client clientWithHeaders, + PutFollowAction.Request request, + ActionListener originalListener, + RestoreService.RestoreCompletionResponse response + ) { final ActionListener listener; if (ActiveShardCount.NONE.equals(request.waitForActiveShards())) { originalListener.onResponse(new PutFollowAction.Response(true, false, false)); @@ -232,8 +254,10 @@ public void onFailure(Exception e) { listener = originalListener; } - RestoreClusterStateListener.createAndRegisterListener(clusterService, response, listener.delegateFailure( - (delegatedListener, restoreSnapshotResponse) -> { + RestoreClusterStateListener.createAndRegisterListener( + clusterService, + response, + listener.delegateFailure((delegatedListener, restoreSnapshotResponse) -> { RestoreInfo restoreInfo = restoreSnapshotResponse.getRestoreInfo(); if (restoreInfo == null) { // If restoreInfo is null then it is possible there was a master failure during the @@ -245,40 +269,59 @@ public void onFailure(Exception e) { assert restoreInfo.failedShards() > 0 : "Should have failed shards"; delegatedListener.onResponse(new PutFollowAction.Response(true, false, false)); } - })); + }) + ); } private void initiateFollowing( final Client client, final PutFollowAction.Request request, - final ActionListener listener) { + final ActionListener listener + ) { assert request.waitForActiveShards() != ActiveShardCount.DEFAULT : "PutFollowAction does not support DEFAULT."; FollowParameters parameters = request.getParameters(); ResumeFollowAction.Request resumeFollowRequest = new ResumeFollowAction.Request(); resumeFollowRequest.setFollowerIndex(request.getFollowerIndex()); resumeFollowRequest.setParameters(new FollowParameters(parameters)); - client.execute(ResumeFollowAction.INSTANCE, resumeFollowRequest, ActionListener.wrap( - r -> activeShardsObserver.waitForActiveShards(new String[]{request.getFollowerIndex()}, - request.waitForActiveShards(), request.timeout(), result -> - listener.onResponse(new PutFollowAction.Response(true, result, r.isAcknowledged())), - listener::onFailure), - listener::onFailure - )); + client.execute( + ResumeFollowAction.INSTANCE, + resumeFollowRequest, + ActionListener.wrap( + r -> activeShardsObserver.waitForActiveShards( + new String[] { request.getFollowerIndex() }, + request.waitForActiveShards(), + request.timeout(), + result -> listener.onResponse(new PutFollowAction.Response(true, result, r.isAcknowledged())), + listener::onFailure + ), + listener::onFailure + ) + ); } - static DataStream updateLocalDataStream(Index backingIndexToFollow, - DataStream localDataStream, - DataStream remoteDataStream) { + static DataStream updateLocalDataStream(Index backingIndexToFollow, DataStream localDataStream, DataStream remoteDataStream) { if (localDataStream == null) { // The data stream and the backing indices have been created and validated in the remote cluster, // just copying the data stream is in this case safe. - return new DataStream(remoteDataStream.getName(), remoteDataStream.getTimeStampField(), - List.of(backingIndexToFollow), remoteDataStream.getGeneration(), remoteDataStream.getMetadata(), - remoteDataStream.isHidden(), true, remoteDataStream.isAllowCustomRouting()); + return new DataStream( + remoteDataStream.getName(), + remoteDataStream.getTimeStampField(), + List.of(backingIndexToFollow), + remoteDataStream.getGeneration(), + remoteDataStream.getMetadata(), + remoteDataStream.isHidden(), + true, + remoteDataStream.isAllowCustomRouting() + ); } else { if (localDataStream.isReplicated() == false) { - throw new IllegalArgumentException("cannot follow backing index [" + backingIndexToFollow.getName() + - "], because local data stream [" + localDataStream.getName() + "] is no longer marked as replicated"); + throw new IllegalArgumentException( + "cannot follow backing index [" + + backingIndexToFollow.getName() + + "], because local data stream [" + + localDataStream.getName() + + "] is no longer marked as replicated" + ); } List backingIndices = new ArrayList<>(localDataStream.getIndices()); @@ -290,9 +333,16 @@ static DataStream updateLocalDataStream(Index backingIndexToFollow, // (string sorting works because of the naming backing index naming scheme) backingIndices.sort(Comparator.comparing(Index::getName)); - return new DataStream(localDataStream.getName(), localDataStream.getTimeStampField(), backingIndices, - remoteDataStream.getGeneration(), remoteDataStream.getMetadata(), localDataStream.isHidden(), - localDataStream.isReplicated(), localDataStream.isAllowCustomRouting()); + return new DataStream( + localDataStream.getName(), + localDataStream.getTimeStampField(), + backingIndices, + remoteDataStream.getGeneration(), + remoteDataStream.getMetadata(), + localDataStream.isHidden(), + localDataStream.isReplicated(), + localDataStream.isAllowCustomRouting() + ); } } diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportResumeFollowAction.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportResumeFollowAction.java index c5d94af6affaf..7d3e120c7d659 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportResumeFollowAction.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportResumeFollowAction.java @@ -83,17 +83,27 @@ public class TransportResumeFollowAction extends AcknowledgedTransportMasterNode @Inject public TransportResumeFollowAction( - final ThreadPool threadPool, - final TransportService transportService, - final ActionFilters actionFilters, - final Client client, - final ClusterService clusterService, - final IndexNameExpressionResolver indexNameExpressionResolver, - final PersistentTasksService persistentTasksService, - final IndicesService indicesService, - final CcrLicenseChecker ccrLicenseChecker) { - super(ResumeFollowAction.NAME, true, transportService, clusterService, threadPool, actionFilters, - ResumeFollowAction.Request::new, indexNameExpressionResolver, ThreadPool.Names.SAME); + final ThreadPool threadPool, + final TransportService transportService, + final ActionFilters actionFilters, + final Client client, + final ClusterService clusterService, + final IndexNameExpressionResolver indexNameExpressionResolver, + final PersistentTasksService persistentTasksService, + final IndicesService indicesService, + final CcrLicenseChecker ccrLicenseChecker + ) { + super( + ResumeFollowAction.NAME, + true, + transportService, + clusterService, + threadPool, + actionFilters, + ResumeFollowAction.Request::new, + indexNameExpressionResolver, + ThreadPool.Names.SAME + ); this.client = client; this.threadPool = threadPool; this.persistentTasksService = persistentTasksService; @@ -107,9 +117,12 @@ protected ClusterBlockException checkBlock(ResumeFollowAction.Request request, C } @Override - protected void masterOperation(Task task, final ResumeFollowAction.Request request, - ClusterState state, - final ActionListener listener) throws Exception { + protected void masterOperation( + Task task, + final ResumeFollowAction.Request request, + ClusterState state, + final ActionListener listener + ) throws Exception { if (ccrLicenseChecker.isCcrAllowed() == false) { listener.onFailure(LicenseUtils.newComplianceException("ccr")); return; @@ -123,7 +136,7 @@ protected void masterOperation(Task task, final ResumeFollowAction.Request reque final Map ccrMetadata = followerIndexMetadata.getCustomData(Ccr.CCR_CUSTOM_METADATA_KEY); if (ccrMetadata == null) { - throw new IllegalArgumentException("follow index ["+ request.getFollowerIndex() + "] does not have ccr metadata"); + throw new IllegalArgumentException("follow index [" + request.getFollowerIndex() + "] does not have ccr metadata"); } final String leaderCluster = ccrMetadata.get(Ccr.CCR_CUSTOM_METADATA_REMOTE_CLUSTER_NAME_KEY); // Validates whether the leader cluster has been configured properly: @@ -140,7 +153,8 @@ protected void masterOperation(Task task, final ResumeFollowAction.Request reque } catch (final IOException e) { listener.onFailure(e); } - }); + } + ); } /** @@ -154,12 +168,13 @@ protected void masterOperation(Task task, final ResumeFollowAction.Request reque * */ void start( - ResumeFollowAction.Request request, - String clusterNameAlias, - IndexMetadata leaderIndexMetadata, - IndexMetadata followIndexMetadata, - String[] leaderIndexHistoryUUIDs, - ActionListener listener) throws IOException { + ResumeFollowAction.Request request, + String clusterNameAlias, + IndexMetadata leaderIndexMetadata, + IndexMetadata followIndexMetadata, + String[] leaderIndexHistoryUUIDs, + ActionListener listener + ) throws IOException { MapperService mapperService = followIndexMetadata != null ? indicesService.createIndexMapperService(followIndexMetadata) : null; validate(request, leaderIndexMetadata, followIndexMetadata, leaderIndexHistoryUUIDs, mapperService); @@ -169,28 +184,42 @@ void start( for (int shardId = 0; shardId < numShards; shardId++) { String taskId = followIndexMetadata.getIndexUUID() + "-" + shardId; - final ShardFollowTask shardFollowTask = createShardFollowTask(shardId, clusterNameAlias, request.getParameters(), - leaderIndexMetadata, followIndexMetadata, filteredHeaders); + final ShardFollowTask shardFollowTask = createShardFollowTask( + shardId, + clusterNameAlias, + request.getParameters(), + leaderIndexMetadata, + followIndexMetadata, + filteredHeaders + ); persistentTasksService.sendStartRequest(taskId, ShardFollowTask.NAME, shardFollowTask, handler.getActionListener(shardId)); } } static void validate( - final ResumeFollowAction.Request request, - final IndexMetadata leaderIndex, - final IndexMetadata followIndex, - final String[] leaderIndexHistoryUUID, - final MapperService followerMapperService) { + final ResumeFollowAction.Request request, + final IndexMetadata leaderIndex, + final IndexMetadata followIndex, + final String[] leaderIndexHistoryUUID, + final MapperService followerMapperService + ) { Map ccrIndexMetadata = followIndex.getCustomData(Ccr.CCR_CUSTOM_METADATA_KEY); if (ccrIndexMetadata == null) { - throw new IllegalArgumentException("follow index ["+ followIndex.getIndex().getName() + "] does not have ccr metadata"); + throw new IllegalArgumentException("follow index [" + followIndex.getIndex().getName() + "] does not have ccr metadata"); } String leaderIndexUUID = leaderIndex.getIndex().getUUID(); String recordedLeaderIndexUUID = ccrIndexMetadata.get(Ccr.CCR_CUSTOM_METADATA_LEADER_INDEX_UUID_KEY); if (leaderIndexUUID.equals(recordedLeaderIndexUUID) == false) { - throw new IllegalArgumentException("follow index [" + request.getFollowerIndex() + "] should reference [" + - leaderIndexUUID + "] as leader index but instead reference [" + recordedLeaderIndexUUID + "] as leader index"); + throw new IllegalArgumentException( + "follow index [" + + request.getFollowerIndex() + + "] should reference [" + + leaderIndexUUID + + "] as leader index but instead reference [" + + recordedLeaderIndexUUID + + "] as leader index" + ); } String[] recordedHistoryUUIDs = extractLeaderShardHistoryUUIDs(ccrIndexMetadata); @@ -199,41 +228,71 @@ static void validate( String recordedLeaderIndexHistoryUUID = recordedHistoryUUIDs[i]; String actualLeaderIndexHistoryUUID = leaderIndexHistoryUUID[i]; if (recordedLeaderIndexHistoryUUID.equals(actualLeaderIndexHistoryUUID) == false) { - throw new IllegalArgumentException("leader shard [" + request.getFollowerIndex() + "][" + i + "] should reference [" + - recordedLeaderIndexHistoryUUID + "] as history uuid but instead reference [" + actualLeaderIndexHistoryUUID + - "] as history uuid"); + throw new IllegalArgumentException( + "leader shard [" + + request.getFollowerIndex() + + "][" + + i + + "] should reference [" + + recordedLeaderIndexHistoryUUID + + "] as history uuid but instead reference [" + + actualLeaderIndexHistoryUUID + + "] as history uuid" + ); } } if (IndexSettings.INDEX_SOFT_DELETES_SETTING.get(leaderIndex.getSettings()) == false) { - throw new IllegalArgumentException("leader index [" + leaderIndex.getIndex().getName() + - "] does not have soft deletes enabled"); + throw new IllegalArgumentException( + "leader index [" + leaderIndex.getIndex().getName() + "] does not have soft deletes enabled" + ); } if (SearchableSnapshotsSettings.isSearchableSnapshotStore(leaderIndex.getSettings())) { - throw new IllegalArgumentException("leader index [" + leaderIndex.getIndex().getName() + - "] is a searchable snapshot index and cannot be used for cross-cluster replication purpose"); + throw new IllegalArgumentException( + "leader index [" + + leaderIndex.getIndex().getName() + + "] is a searchable snapshot index and cannot be used for cross-cluster replication purpose" + ); } if (IndexSettings.INDEX_SOFT_DELETES_SETTING.get(followIndex.getSettings()) == false) { - throw new IllegalArgumentException("follower index [" + request.getFollowerIndex() + - "] does not have soft deletes enabled"); + throw new IllegalArgumentException("follower index [" + request.getFollowerIndex() + "] does not have soft deletes enabled"); } if (SearchableSnapshotsSettings.isSearchableSnapshotStore(followIndex.getSettings())) { - throw new IllegalArgumentException("follower index [" + request.getFollowerIndex() + - "] is a searchable snapshot index and cannot be used for cross-cluster replication purpose"); + throw new IllegalArgumentException( + "follower index [" + + request.getFollowerIndex() + + "] is a searchable snapshot index and cannot be used for cross-cluster replication purpose" + ); } if (leaderIndex.getNumberOfShards() != followIndex.getNumberOfShards()) { - throw new IllegalArgumentException("leader index primary shards [" + leaderIndex.getNumberOfShards() + - "] does not match with the number of shards of the follow index [" + followIndex.getNumberOfShards() + "]"); + throw new IllegalArgumentException( + "leader index primary shards [" + + leaderIndex.getNumberOfShards() + + "] does not match with the number of shards of the follow index [" + + followIndex.getNumberOfShards() + + "]" + ); } if (leaderIndex.getRoutingNumShards() != followIndex.getRoutingNumShards()) { - throw new IllegalArgumentException("leader index number_of_routing_shards [" + leaderIndex.getRoutingNumShards() + - "] does not match with the number_of_routing_shards of the follow index [" + followIndex.getRoutingNumShards() + "]"); + throw new IllegalArgumentException( + "leader index number_of_routing_shards [" + + leaderIndex.getRoutingNumShards() + + "] does not match with the number_of_routing_shards of the follow index [" + + followIndex.getRoutingNumShards() + + "]" + ); } if (leaderIndex.getState() != IndexMetadata.State.OPEN || followIndex.getState() != IndexMetadata.State.OPEN) { throw new IllegalArgumentException("leader and follow index must be open"); } if (CcrSettings.CCR_FOLLOWING_INDEX_SETTING.get(followIndex.getSettings()) == false) { - throw new IllegalArgumentException("the following index [" + request.getFollowerIndex() + "] is not ready " + - "to follow; the setting [" + CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey() + "] must be enabled."); + throw new IllegalArgumentException( + "the following index [" + + request.getFollowerIndex() + + "] is not ready " + + "to follow; the setting [" + + CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey() + + "] must be enabled." + ); } validateSettings(leaderIndex.getSettings(), followIndex.getSettings()); @@ -288,7 +347,7 @@ private static ShardFollowTask createShardFollowTask( } int maxOutstandingReadRequests; - if (parameters.getMaxOutstandingReadRequests() != null){ + if (parameters.getMaxOutstandingReadRequests() != null) { maxOutstandingReadRequests = parameters.getMaxOutstandingReadRequests(); } else { maxOutstandingReadRequests = DEFAULT_MAX_OUTSTANDING_READ_REQUESTS; @@ -368,85 +427,86 @@ static String[] extractLeaderShardHistoryUUIDs(Map ccrIndexMetad * are inconvenient if they were replicated (e.g. changing number of replicas). */ static final Set> NON_REPLICATED_SETTINGS = Set.of( - IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING, - IndexMetadata.INDEX_AUTO_EXPAND_REPLICAS_SETTING, - IndexMetadata.INDEX_ROUTING_EXCLUDE_GROUP_SETTING, - IndexMetadata.INDEX_ROUTING_INCLUDE_GROUP_SETTING, - IndexMetadata.INDEX_ROUTING_REQUIRE_GROUP_SETTING, - IndexMetadata.INDEX_READ_ONLY_SETTING, - IndexMetadata.INDEX_BLOCKS_READ_SETTING, - IndexMetadata.INDEX_BLOCKS_WRITE_SETTING, - IndexMetadata.INDEX_BLOCKS_METADATA_SETTING, - IndexMetadata.INDEX_BLOCKS_READ_ONLY_ALLOW_DELETE_SETTING, - IndexMetadata.INDEX_PRIORITY_SETTING, - IndexMetadata.SETTING_WAIT_FOR_ACTIVE_SHARDS, - IndexMetadata.INDEX_HIDDEN_SETTING, - EnableAllocationDecider.INDEX_ROUTING_REBALANCE_ENABLE_SETTING, - EnableAllocationDecider.INDEX_ROUTING_ALLOCATION_ENABLE_SETTING, - ShardsLimitAllocationDecider.INDEX_TOTAL_SHARDS_PER_NODE_SETTING, - MaxRetryAllocationDecider.SETTING_ALLOCATION_MAX_RETRY, - UnassignedInfo.INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING, - IndexSettings.MAX_RESULT_WINDOW_SETTING, - IndexSettings.INDEX_WARMER_ENABLED_SETTING, - IndexSettings.INDEX_REFRESH_INTERVAL_SETTING, - IndexSettings.MAX_RESCORE_WINDOW_SETTING, - IndexSettings.MAX_INNER_RESULT_WINDOW_SETTING, - IndexSettings.DEFAULT_FIELD_SETTING, - IndexSettings.QUERY_STRING_LENIENT_SETTING, - IndexSettings.QUERY_STRING_ANALYZE_WILDCARD, - IndexSettings.QUERY_STRING_ALLOW_LEADING_WILDCARD, - IndexSettings.ALLOW_UNMAPPED, - IndexSettings.INDEX_SEARCH_IDLE_AFTER, - IndexSettings.INDEX_SOFT_DELETES_RETENTION_OPERATIONS_SETTING, - IndexSettings.INDEX_SOFT_DELETES_RETENTION_LEASE_PERIOD_SETTING, - IndexSettings.MAX_SCRIPT_FIELDS_SETTING, - IndexSettings.MAX_REGEX_LENGTH_SETTING, - IndexSettings.MAX_TERMS_COUNT_SETTING, - IndexSettings.MAX_ANALYZED_OFFSET_SETTING, - IndexSettings.MAX_DOCVALUE_FIELDS_SEARCH_SETTING, - IndexSettings.MAX_TOKEN_COUNT_SETTING, - IndexSettings.MAX_SLICES_PER_SCROLL, - IndexSettings.DEFAULT_PIPELINE, - IndexSettings.FINAL_PIPELINE, - IndexSettings.INDEX_SEARCH_THROTTLED, - IndexSettings.INDEX_TRANSLOG_RETENTION_AGE_SETTING, - IndexSettings.INDEX_TRANSLOG_RETENTION_SIZE_SETTING, - IndexSettings.INDEX_TRANSLOG_GENERATION_THRESHOLD_SIZE_SETTING, - IndexSettings.INDEX_TRANSLOG_FLUSH_THRESHOLD_SIZE_SETTING, - IndexSettings.INDEX_TRANSLOG_DURABILITY_SETTING, - IndexSettings.INDEX_TRANSLOG_SYNC_INTERVAL_SETTING, - IndexSettings.INDEX_FLUSH_AFTER_MERGE_THRESHOLD_SIZE_SETTING, - IndexSettings.INDEX_GC_DELETES_SETTING, - IndexSettings.MAX_REFRESH_LISTENERS_PER_SHARD, - IndicesRequestCache.INDEX_CACHE_REQUEST_ENABLED_SETTING, - BitsetFilterCache.INDEX_LOAD_RANDOM_ACCESS_FILTERS_EAGERLY_SETTING, - SearchSlowLog.INDEX_SEARCH_SLOWLOG_THRESHOLD_FETCH_DEBUG_SETTING, - SearchSlowLog.INDEX_SEARCH_SLOWLOG_THRESHOLD_FETCH_WARN_SETTING, - SearchSlowLog.INDEX_SEARCH_SLOWLOG_THRESHOLD_FETCH_INFO_SETTING, - SearchSlowLog.INDEX_SEARCH_SLOWLOG_THRESHOLD_FETCH_TRACE_SETTING, - SearchSlowLog.INDEX_SEARCH_SLOWLOG_THRESHOLD_QUERY_WARN_SETTING, - SearchSlowLog.INDEX_SEARCH_SLOWLOG_THRESHOLD_QUERY_DEBUG_SETTING, - SearchSlowLog.INDEX_SEARCH_SLOWLOG_THRESHOLD_QUERY_INFO_SETTING, - SearchSlowLog.INDEX_SEARCH_SLOWLOG_THRESHOLD_QUERY_TRACE_SETTING, - IndexingSlowLog.INDEX_INDEXING_SLOWLOG_THRESHOLD_INDEX_WARN_SETTING, - IndexingSlowLog.INDEX_INDEXING_SLOWLOG_THRESHOLD_INDEX_DEBUG_SETTING, - IndexingSlowLog.INDEX_INDEXING_SLOWLOG_THRESHOLD_INDEX_INFO_SETTING, - IndexingSlowLog.INDEX_INDEXING_SLOWLOG_THRESHOLD_INDEX_TRACE_SETTING, - IndexingSlowLog.INDEX_INDEXING_SLOWLOG_REFORMAT_SETTING, - IndexingSlowLog.INDEX_INDEXING_SLOWLOG_MAX_SOURCE_CHARS_TO_LOG_SETTING, - MergePolicyConfig.INDEX_COMPOUND_FORMAT_SETTING, - MergePolicyConfig.INDEX_MERGE_POLICY_MAX_MERGE_AT_ONCE_SETTING, - MergePolicyConfig.INDEX_MERGE_POLICY_SEGMENTS_PER_TIER_SETTING, - MergePolicyConfig.INDEX_MERGE_POLICY_DELETES_PCT_ALLOWED_SETTING, - MergePolicyConfig.INDEX_MERGE_POLICY_EXPUNGE_DELETES_ALLOWED_SETTING, - MergePolicyConfig.INDEX_MERGE_POLICY_FLOOR_SEGMENT_SETTING, - MergePolicyConfig.INDEX_MERGE_POLICY_MAX_MERGE_AT_ONCE_EXPLICIT_SETTING, - MergePolicyConfig.INDEX_MERGE_POLICY_MAX_MERGED_SEGMENT_SETTING, - MergeSchedulerConfig.AUTO_THROTTLE_SETTING, - MergeSchedulerConfig.MAX_MERGE_COUNT_SETTING, - MergeSchedulerConfig.MAX_THREAD_COUNT_SETTING, - EngineConfig.INDEX_CODEC_SETTING, - DataTier.TIER_PREFERENCE_SETTING); + IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING, + IndexMetadata.INDEX_AUTO_EXPAND_REPLICAS_SETTING, + IndexMetadata.INDEX_ROUTING_EXCLUDE_GROUP_SETTING, + IndexMetadata.INDEX_ROUTING_INCLUDE_GROUP_SETTING, + IndexMetadata.INDEX_ROUTING_REQUIRE_GROUP_SETTING, + IndexMetadata.INDEX_READ_ONLY_SETTING, + IndexMetadata.INDEX_BLOCKS_READ_SETTING, + IndexMetadata.INDEX_BLOCKS_WRITE_SETTING, + IndexMetadata.INDEX_BLOCKS_METADATA_SETTING, + IndexMetadata.INDEX_BLOCKS_READ_ONLY_ALLOW_DELETE_SETTING, + IndexMetadata.INDEX_PRIORITY_SETTING, + IndexMetadata.SETTING_WAIT_FOR_ACTIVE_SHARDS, + IndexMetadata.INDEX_HIDDEN_SETTING, + EnableAllocationDecider.INDEX_ROUTING_REBALANCE_ENABLE_SETTING, + EnableAllocationDecider.INDEX_ROUTING_ALLOCATION_ENABLE_SETTING, + ShardsLimitAllocationDecider.INDEX_TOTAL_SHARDS_PER_NODE_SETTING, + MaxRetryAllocationDecider.SETTING_ALLOCATION_MAX_RETRY, + UnassignedInfo.INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING, + IndexSettings.MAX_RESULT_WINDOW_SETTING, + IndexSettings.INDEX_WARMER_ENABLED_SETTING, + IndexSettings.INDEX_REFRESH_INTERVAL_SETTING, + IndexSettings.MAX_RESCORE_WINDOW_SETTING, + IndexSettings.MAX_INNER_RESULT_WINDOW_SETTING, + IndexSettings.DEFAULT_FIELD_SETTING, + IndexSettings.QUERY_STRING_LENIENT_SETTING, + IndexSettings.QUERY_STRING_ANALYZE_WILDCARD, + IndexSettings.QUERY_STRING_ALLOW_LEADING_WILDCARD, + IndexSettings.ALLOW_UNMAPPED, + IndexSettings.INDEX_SEARCH_IDLE_AFTER, + IndexSettings.INDEX_SOFT_DELETES_RETENTION_OPERATIONS_SETTING, + IndexSettings.INDEX_SOFT_DELETES_RETENTION_LEASE_PERIOD_SETTING, + IndexSettings.MAX_SCRIPT_FIELDS_SETTING, + IndexSettings.MAX_REGEX_LENGTH_SETTING, + IndexSettings.MAX_TERMS_COUNT_SETTING, + IndexSettings.MAX_ANALYZED_OFFSET_SETTING, + IndexSettings.MAX_DOCVALUE_FIELDS_SEARCH_SETTING, + IndexSettings.MAX_TOKEN_COUNT_SETTING, + IndexSettings.MAX_SLICES_PER_SCROLL, + IndexSettings.DEFAULT_PIPELINE, + IndexSettings.FINAL_PIPELINE, + IndexSettings.INDEX_SEARCH_THROTTLED, + IndexSettings.INDEX_TRANSLOG_RETENTION_AGE_SETTING, + IndexSettings.INDEX_TRANSLOG_RETENTION_SIZE_SETTING, + IndexSettings.INDEX_TRANSLOG_GENERATION_THRESHOLD_SIZE_SETTING, + IndexSettings.INDEX_TRANSLOG_FLUSH_THRESHOLD_SIZE_SETTING, + IndexSettings.INDEX_TRANSLOG_DURABILITY_SETTING, + IndexSettings.INDEX_TRANSLOG_SYNC_INTERVAL_SETTING, + IndexSettings.INDEX_FLUSH_AFTER_MERGE_THRESHOLD_SIZE_SETTING, + IndexSettings.INDEX_GC_DELETES_SETTING, + IndexSettings.MAX_REFRESH_LISTENERS_PER_SHARD, + IndicesRequestCache.INDEX_CACHE_REQUEST_ENABLED_SETTING, + BitsetFilterCache.INDEX_LOAD_RANDOM_ACCESS_FILTERS_EAGERLY_SETTING, + SearchSlowLog.INDEX_SEARCH_SLOWLOG_THRESHOLD_FETCH_DEBUG_SETTING, + SearchSlowLog.INDEX_SEARCH_SLOWLOG_THRESHOLD_FETCH_WARN_SETTING, + SearchSlowLog.INDEX_SEARCH_SLOWLOG_THRESHOLD_FETCH_INFO_SETTING, + SearchSlowLog.INDEX_SEARCH_SLOWLOG_THRESHOLD_FETCH_TRACE_SETTING, + SearchSlowLog.INDEX_SEARCH_SLOWLOG_THRESHOLD_QUERY_WARN_SETTING, + SearchSlowLog.INDEX_SEARCH_SLOWLOG_THRESHOLD_QUERY_DEBUG_SETTING, + SearchSlowLog.INDEX_SEARCH_SLOWLOG_THRESHOLD_QUERY_INFO_SETTING, + SearchSlowLog.INDEX_SEARCH_SLOWLOG_THRESHOLD_QUERY_TRACE_SETTING, + IndexingSlowLog.INDEX_INDEXING_SLOWLOG_THRESHOLD_INDEX_WARN_SETTING, + IndexingSlowLog.INDEX_INDEXING_SLOWLOG_THRESHOLD_INDEX_DEBUG_SETTING, + IndexingSlowLog.INDEX_INDEXING_SLOWLOG_THRESHOLD_INDEX_INFO_SETTING, + IndexingSlowLog.INDEX_INDEXING_SLOWLOG_THRESHOLD_INDEX_TRACE_SETTING, + IndexingSlowLog.INDEX_INDEXING_SLOWLOG_REFORMAT_SETTING, + IndexingSlowLog.INDEX_INDEXING_SLOWLOG_MAX_SOURCE_CHARS_TO_LOG_SETTING, + MergePolicyConfig.INDEX_COMPOUND_FORMAT_SETTING, + MergePolicyConfig.INDEX_MERGE_POLICY_MAX_MERGE_AT_ONCE_SETTING, + MergePolicyConfig.INDEX_MERGE_POLICY_SEGMENTS_PER_TIER_SETTING, + MergePolicyConfig.INDEX_MERGE_POLICY_DELETES_PCT_ALLOWED_SETTING, + MergePolicyConfig.INDEX_MERGE_POLICY_EXPUNGE_DELETES_ALLOWED_SETTING, + MergePolicyConfig.INDEX_MERGE_POLICY_FLOOR_SEGMENT_SETTING, + MergePolicyConfig.INDEX_MERGE_POLICY_MAX_MERGE_AT_ONCE_EXPLICIT_SETTING, + MergePolicyConfig.INDEX_MERGE_POLICY_MAX_MERGED_SEGMENT_SETTING, + MergeSchedulerConfig.AUTO_THROTTLE_SETTING, + MergeSchedulerConfig.MAX_MERGE_COUNT_SETTING, + MergeSchedulerConfig.MAX_THREAD_COUNT_SETTING, + EngineConfig.INDEX_CODEC_SETTING, + DataTier.TIER_PREFERENCE_SETTING + ); public static Settings filter(Settings originalSettings) { Settings.Builder settings = Settings.builder().put(originalSettings); diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportUnfollowAction.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportUnfollowAction.java index b26f2f6092e9d..22e0e1860d991 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportUnfollowAction.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/TransportUnfollowAction.java @@ -57,29 +57,33 @@ public class TransportUnfollowAction extends AcknowledgedTransportMasterNodeActi @Inject public TransportUnfollowAction( - final TransportService transportService, - final ClusterService clusterService, - final ThreadPool threadPool, - final ActionFilters actionFilters, - final IndexNameExpressionResolver indexNameExpressionResolver, - final Client client) { + final TransportService transportService, + final ClusterService clusterService, + final ThreadPool threadPool, + final ActionFilters actionFilters, + final IndexNameExpressionResolver indexNameExpressionResolver, + final Client client + ) { super( - UnfollowAction.NAME, - transportService, - clusterService, - threadPool, - actionFilters, - UnfollowAction.Request::new, - indexNameExpressionResolver, - ThreadPool.Names.SAME); + UnfollowAction.NAME, + transportService, + clusterService, + threadPool, + actionFilters, + UnfollowAction.Request::new, + indexNameExpressionResolver, + ThreadPool.Names.SAME + ); this.client = Objects.requireNonNull(client); } @Override protected void masterOperation( - Task task, final UnfollowAction.Request request, + Task task, + final UnfollowAction.Request request, final ClusterState state, - final ActionListener listener) { + final ActionListener listener + ) { clusterService.submitStateUpdateTask("unfollow_action", new ClusterStateUpdateTask() { @Override @@ -103,10 +107,11 @@ public void clusterStateProcessed(final String source, final ClusterState oldSta final String leaderIndexUuid = ccrCustomMetadata.get(Ccr.CCR_CUSTOM_METADATA_LEADER_INDEX_UUID_KEY); final Index leaderIndex = new Index(leaderIndexName, leaderIndexUuid); final String retentionLeaseId = CcrRetentionLeases.retentionLeaseId( - oldState.getClusterName().value(), - indexMetadata.getIndex(), - remoteClusterName, - leaderIndex); + oldState.getClusterName().value(), + indexMetadata.getIndex(), + remoteClusterName, + leaderIndex + ); final int numberOfShards = IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.get(indexMetadata.getSettings()); final Client remoteClient; @@ -117,61 +122,60 @@ public void clusterStateProcessed(final String source, final ClusterState oldSta return; } - final GroupedActionListener groupListener = new GroupedActionListener<>( - new ActionListener<>() { + final GroupedActionListener groupListener = new GroupedActionListener<>(new ActionListener<>() { - @Override - public void onResponse(final Collection responses) { - logger.trace( - "[{}] removed retention lease [{}] on all leader primary shards", - indexMetadata.getIndex(), - retentionLeaseId); - listener.onResponse(AcknowledgedResponse.TRUE); - } + @Override + public void onResponse(final Collection responses) { + logger.trace( + "[{}] removed retention lease [{}] on all leader primary shards", + indexMetadata.getIndex(), + retentionLeaseId + ); + listener.onResponse(AcknowledgedResponse.TRUE); + } - @Override - public void onFailure(final Exception e) { - onLeaseRemovalFailure(indexMetadata.getIndex(), retentionLeaseId, e); - } - }, - numberOfShards - ); + @Override + public void onFailure(final Exception e) { + onLeaseRemovalFailure(indexMetadata.getIndex(), retentionLeaseId, e); + } + }, numberOfShards); for (int i = 0; i < numberOfShards; i++) { final ShardId followerShardId = new ShardId(indexMetadata.getIndex(), i); final ShardId leaderShardId = new ShardId(leaderIndex, i); removeRetentionLeaseForShard( - followerShardId, - leaderShardId, - retentionLeaseId, - remoteClient, - ActionListener.wrap( - groupListener::onResponse, - e -> handleException( - followerShardId, - retentionLeaseId, - leaderShardId, - groupListener, - e))); + followerShardId, + leaderShardId, + retentionLeaseId, + remoteClient, + ActionListener.wrap( + groupListener::onResponse, + e -> handleException(followerShardId, retentionLeaseId, leaderShardId, groupListener, e) + ) + ); } } private void onLeaseRemovalFailure(Index index, String retentionLeaseId, Exception e) { - logger.warn(new ParameterizedMessage( - "[{}] failure while removing retention lease [{}] on leader primary shards", - index, - retentionLeaseId), - e); + logger.warn( + new ParameterizedMessage( + "[{}] failure while removing retention lease [{}] on leader primary shards", + index, + retentionLeaseId + ), + e + ); final ElasticsearchException wrapper = new ElasticsearchException(e); wrapper.addMetadata("es.failed_to_remove_retention_leases", retentionLeaseId); listener.onFailure(wrapper); } private void removeRetentionLeaseForShard( - final ShardId followerShardId, - final ShardId leaderShardId, - final String retentionLeaseId, - final Client remoteClient, - final ActionListener listener) { + final ShardId followerShardId, + final ShardId leaderShardId, + final String retentionLeaseId, + final Client remoteClient, + final ActionListener listener + ) { logger.trace("{} removing retention lease [{}] while unfollowing leader index", followerShardId, retentionLeaseId); final ThreadContext threadContext = threadPool.getThreadContext(); try (ThreadContext.StoredContext ignore = threadPool.getThreadContext().stashContext()) { @@ -182,29 +186,36 @@ private void removeRetentionLeaseForShard( } private void handleException( - final ShardId followerShardId, - final String retentionLeaseId, - final ShardId leaderShardId, - final ActionListener listener, - final Exception e) { + final ShardId followerShardId, + final String retentionLeaseId, + final ShardId leaderShardId, + final ActionListener listener, + final Exception e + ) { final Throwable cause = ExceptionsHelper.unwrapCause(e); assert cause instanceof ElasticsearchSecurityException == false : e; if (cause instanceof RetentionLeaseNotFoundException) { // treat as success - logger.trace(new ParameterizedMessage( + logger.trace( + new ParameterizedMessage( "{} retention lease [{}] not found on {} while unfollowing", followerShardId, retentionLeaseId, - leaderShardId), - e); + leaderShardId + ), + e + ); listener.onResponse(ActionResponse.Empty.INSTANCE); } else { - logger.warn(new ParameterizedMessage( + logger.warn( + new ParameterizedMessage( "{} failed to remove retention lease [{}] on {} while unfollowing", followerShardId, retentionLeaseId, - leaderShardId), - e); + leaderShardId + ), + e + ); listener.onFailure(e); } } @@ -228,8 +239,9 @@ static ClusterState unfollow(String followerIndex, ClusterState current) { } if (followerIMD.getState() != IndexMetadata.State.CLOSE) { - throw new IllegalArgumentException("cannot convert the follower index [" + followerIndex + - "] to a non-follower, because it has not been closed"); + throw new IllegalArgumentException( + "cannot convert the follower index [" + followerIndex + "] to a non-follower, because it has not been closed" + ); } PersistentTasksCustomMetadata persistentTasks = current.metadata().custom(PersistentTasksCustomMetadata.TYPE); @@ -238,8 +250,9 @@ static ClusterState unfollow(String followerIndex, ClusterState current) { if (persistentTask.getTaskName().equals(ShardFollowTask.NAME)) { ShardFollowTask shardFollowTask = (ShardFollowTask) persistentTask.getParams(); if (shardFollowTask.getFollowShardId().getIndexName().equals(followerIndex)) { - throw new IllegalArgumentException("cannot convert the follower index [" + followerIndex + - "] to a non-follower, because it has not been paused"); + throw new IllegalArgumentException( + "cannot convert the follower index [" + followerIndex + "] to a non-follower, because it has not been paused" + ); } } } @@ -256,11 +269,7 @@ static ClusterState unfollow(String followerIndex, ClusterState current) { // Remove ccr custom metadata newIndexMetadata.removeCustom(Ccr.CCR_CUSTOM_METADATA_KEY); - Metadata newMetadata = Metadata.builder(current.metadata()) - .put(newIndexMetadata) - .build(); - return ClusterState.builder(current) - .metadata(newMetadata) - .build(); + Metadata newMetadata = Metadata.builder(current.metadata()).put(newIndexMetadata).build(); + return ClusterState.builder(current).metadata(newMetadata).build(); } } diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/bulk/BulkShardOperationsRequest.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/bulk/BulkShardOperationsRequest.java index cf5466484a099..f2b10d08a8586 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/bulk/BulkShardOperationsRequest.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/bulk/BulkShardOperationsRequest.java @@ -17,7 +17,8 @@ import java.util.List; public final class BulkShardOperationsRequest extends ReplicatedWriteRequest - implements RawIndexingDataTransportRequest { + implements + RawIndexingDataTransportRequest { private final String historyUUID; private final List operations; @@ -30,10 +31,12 @@ public BulkShardOperationsRequest(StreamInput in) throws IOException { operations = in.readList(Translog.Operation::readOperation); } - public BulkShardOperationsRequest(final ShardId shardId, - final String historyUUID, - final List operations, - long maxSeqNoOfUpdatesOrDeletes) { + public BulkShardOperationsRequest( + final ShardId shardId, + final String historyUUID, + final List operations, + long maxSeqNoOfUpdatesOrDeletes + ) { super(shardId); setRefreshPolicy(RefreshPolicy.NONE); this.historyUUID = historyUUID; @@ -66,15 +69,23 @@ public void writeTo(final StreamOutput out) throws IOException { @Override public String toString() { - return "BulkShardOperationsRequest{" + - "historyUUID=" + historyUUID + - ", operations=" + operations.size() + - ", maxSeqNoUpdates=" + maxSeqNoOfUpdatesOrDeletes + - ", shardId=" + shardId + - ", timeout=" + timeout + - ", index='" + index + '\'' + - ", waitForActiveShards=" + waitForActiveShards + - '}'; + return "BulkShardOperationsRequest{" + + "historyUUID=" + + historyUUID + + ", operations=" + + operations.size() + + ", maxSeqNoUpdates=" + + maxSeqNoOfUpdatesOrDeletes + + ", shardId=" + + shardId + + ", timeout=" + + timeout + + ", index='" + + index + + '\'' + + ", waitForActiveShards=" + + waitForActiveShards + + '}'; } } diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/bulk/BulkShardOperationsResponse.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/bulk/BulkShardOperationsResponse.java index f81f0589ac4ab..cf6bfefce17e8 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/bulk/BulkShardOperationsResponse.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/bulk/BulkShardOperationsResponse.java @@ -35,8 +35,7 @@ public void setMaxSeqNo(final long maxSeqNo) { this.maxSeqNo = maxSeqNo; } - public BulkShardOperationsResponse() { - } + public BulkShardOperationsResponse() {} public BulkShardOperationsResponse(StreamInput in) throws IOException { super(in); @@ -45,8 +44,7 @@ public BulkShardOperationsResponse(StreamInput in) throws IOException { } @Override - public void setForcedRefresh(final boolean forcedRefresh) { - } + public void setForcedRefresh(final boolean forcedRefresh) {} @Override public void writeTo(final StreamOutput out) throws IOException { diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/bulk/TransportBulkShardOperationsAction.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/bulk/TransportBulkShardOperationsAction.java index d677936945d17..e04bf6c7ae49b 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/bulk/TransportBulkShardOperationsAction.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/bulk/TransportBulkShardOperationsAction.java @@ -17,8 +17,8 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.io.stream.StreamInput; -import org.elasticsearch.core.Releasable; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.Releasable; import org.elasticsearch.index.IndexingPressure; import org.elasticsearch.index.engine.Engine; import org.elasticsearch.index.seqno.SeqNoStats; @@ -38,21 +38,24 @@ import java.util.ArrayList; import java.util.List; -public class TransportBulkShardOperationsAction - extends TransportWriteAction { +public class TransportBulkShardOperationsAction extends TransportWriteAction< + BulkShardOperationsRequest, + BulkShardOperationsRequest, + BulkShardOperationsResponse> { @Inject public TransportBulkShardOperationsAction( - final Settings settings, - final TransportService transportService, - final ClusterService clusterService, - final IndicesService indicesService, - final ThreadPool threadPool, - final ShardStateAction shardStateAction, - final ActionFilters actionFilters, - final IndexingPressure indexingPressure, - final SystemIndices systemIndices, - final ExecutorSelector executorSelector) { + final Settings settings, + final TransportService transportService, + final ClusterService clusterService, + final IndicesService indicesService, + final ThreadPool threadPool, + final ShardStateAction shardStateAction, + final ActionFilters actionFilters, + final IndexingPressure indexingPressure, + final SystemIndices systemIndices, + final ExecutorSelector executorSelector + ) { super( settings, BulkShardOperationsAction.NAME, @@ -74,8 +77,11 @@ public TransportBulkShardOperationsAction( @Override protected void doExecute(Task task, BulkShardOperationsRequest request, ActionListener listener) { // This is executed on the follower coordinator node and we need to mark the bytes. - Releasable releasable = indexingPressure.markCoordinatingOperationStarted(primaryOperationCount(request), - primaryOperationSize(request), false); + Releasable releasable = indexingPressure.markCoordinatingOperationStarted( + primaryOperationCount(request), + primaryOperationSize(request), + false + ); ActionListener releasingListener = ActionListener.runBefore(listener, releasable::close); try { super.doExecute(task, request, releasingListener); @@ -85,13 +91,25 @@ protected void doExecute(Task task, BulkShardOperationsRequest request, ActionLi } @Override - protected void dispatchedShardOperationOnPrimary(BulkShardOperationsRequest request, IndexShard primary, - ActionListener> listener) { + protected void dispatchedShardOperationOnPrimary( + BulkShardOperationsRequest request, + IndexShard primary, + ActionListener> listener + ) { if (logger.isTraceEnabled()) { logger.trace("index [{}] on the following primary shard {}", request.getOperations(), primary.routingEntry()); } - ActionListener.completeWith(listener, () -> shardOperationOnPrimary(request.shardId(), request.getHistoryUUID(), - request.getOperations(), request.getMaxSeqNoOfUpdatesOrDeletes(), primary, logger)); + ActionListener.completeWith( + listener, + () -> shardOperationOnPrimary( + request.shardId(), + request.getHistoryUUID(), + request.getOperations(), + request.getMaxSeqNoOfUpdatesOrDeletes(), + primary, + logger + ) + ); } @Override @@ -116,15 +134,12 @@ public static Translog.Operation rewriteOperationWithPrimaryTerm(Translog.Operat index.version(), BytesReference.toBytes(index.source()), index.routing(), - index.getAutoGeneratedIdTimestamp()); + index.getAutoGeneratedIdTimestamp() + ); break; case DELETE: final Translog.Delete delete = (Translog.Delete) operation; - operationWithPrimaryTerm = new Translog.Delete( - delete.id(), - delete.seqNo(), - primaryTerm, - delete.version()); + operationWithPrimaryTerm = new Translog.Delete(delete.id(), delete.seqNo(), primaryTerm, delete.version()); break; case NO_OP: final Translog.NoOp noOp = (Translog.NoOp) operation; @@ -138,15 +153,21 @@ public static Translog.Operation rewriteOperationWithPrimaryTerm(Translog.Operat // public for testing purposes only public static WritePrimaryResult shardOperationOnPrimary( - final ShardId shardId, - final String historyUUID, - final List sourceOperations, - final long maxSeqNoOfUpdatesOrDeletes, - final IndexShard primary, - final Logger logger) throws IOException { + final ShardId shardId, + final String historyUUID, + final List sourceOperations, + final long maxSeqNoOfUpdatesOrDeletes, + final IndexShard primary, + final Logger logger + ) throws IOException { if (historyUUID.equalsIgnoreCase(primary.getHistoryUUID()) == false) { - throw new IllegalStateException("unexpected history uuid, expected [" + historyUUID + - "], actual [" + primary.getHistoryUUID() + "], shard is likely restored from snapshot or force allocated"); + throw new IllegalStateException( + "unexpected history uuid, expected [" + + historyUUID + + "], actual [" + + primary.getHistoryUUID() + + "], shard is likely restored from snapshot or force allocated" + ); } assert maxSeqNoOfUpdatesOrDeletes >= SequenceNumbers.NO_OPS_PERFORMED : "invalid msu [" + maxSeqNoOfUpdatesOrDeletes + "]"; @@ -169,17 +190,26 @@ public static WritePrimaryResult(replicaRequest, new BulkShardOperationsResponse(), location, null, primary, logger); } @Override - protected void dispatchedShardOperationOnReplica(BulkShardOperationsRequest request, IndexShard replica, - ActionListener listener) { + protected void dispatchedShardOperationOnReplica( + BulkShardOperationsRequest request, + IndexShard replica, + ActionListener listener + ) { ActionListener.completeWith(listener, () -> { if (logger.isTraceEnabled()) { logger.trace("index [{}] on the following replica shard {}", request.getOperations(), replica.routingEntry()); @@ -215,9 +252,12 @@ protected int replicaOperationCount(BulkShardOperationsRequest request) { // public for testing purposes only public static WriteReplicaResult shardOperationOnReplica( - final BulkShardOperationsRequest request, final IndexShard replica, final Logger logger) throws IOException { - assert replica.getMaxSeqNoOfUpdatesOrDeletes() >= request.getMaxSeqNoOfUpdatesOrDeletes() : - "mus on replica [" + replica + "] < mus of request [" + request.getMaxSeqNoOfUpdatesOrDeletes() + "]"; + final BulkShardOperationsRequest request, + final IndexShard replica, + final Logger logger + ) throws IOException { + assert replica.getMaxSeqNoOfUpdatesOrDeletes() >= request.getMaxSeqNoOfUpdatesOrDeletes() + : "mus on replica [" + replica + "] < mus of request [" + request.getMaxSeqNoOfUpdatesOrDeletes() + "]"; Translog.Location location = null; for (final Translog.Operation operation : request.getOperations()) { final Engine.Result result = replica.applyTranslogOperation(operation, Engine.Operation.Origin.REPLICA); diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/repositories/ClearCcrRestoreSessionAction.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/repositories/ClearCcrRestoreSessionAction.java index 24aadcaa19d38..547e01398391d 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/repositories/ClearCcrRestoreSessionAction.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/repositories/ClearCcrRestoreSessionAction.java @@ -28,22 +28,25 @@ private ClearCcrRestoreSessionAction() { super(NAME, in -> ActionResponse.Empty.INSTANCE); } - public static class TransportDeleteCcrRestoreSessionAction - extends HandledTransportAction { + public static class TransportDeleteCcrRestoreSessionAction extends HandledTransportAction< + ClearCcrRestoreSessionRequest, + ActionResponse.Empty> { private final CcrRestoreSourceService ccrRestoreService; @Inject - public TransportDeleteCcrRestoreSessionAction(ActionFilters actionFilters, TransportService transportService, - CcrRestoreSourceService ccrRestoreService) { + public TransportDeleteCcrRestoreSessionAction( + ActionFilters actionFilters, + TransportService transportService, + CcrRestoreSourceService ccrRestoreService + ) { super(NAME, transportService, actionFilters, ClearCcrRestoreSessionRequest::new, ThreadPool.Names.GENERIC); TransportActionProxy.registerProxyAction(transportService, NAME, false, in -> ActionResponse.Empty.INSTANCE); this.ccrRestoreService = ccrRestoreService; } @Override - protected void doExecute(Task task, ClearCcrRestoreSessionRequest request, - ActionListener listener) { + protected void doExecute(Task task, ClearCcrRestoreSessionRequest request, ActionListener listener) { ccrRestoreService.closeSession(request.getSessionUUID()); listener.onResponse(ActionResponse.Empty.INSTANCE); } diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/repositories/ClearCcrRestoreSessionRequest.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/repositories/ClearCcrRestoreSessionRequest.java index d3f5f46c41a96..0c20ec6aad46c 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/repositories/ClearCcrRestoreSessionRequest.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/repositories/ClearCcrRestoreSessionRequest.java @@ -9,10 +9,10 @@ import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionRequestValidationException; -import org.elasticsearch.transport.RemoteClusterAwareRequest; import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.transport.RemoteClusterAwareRequest; import java.io.IOException; diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/repositories/DeleteInternalCcrRepositoryAction.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/repositories/DeleteInternalCcrRepositoryAction.java index 35f6431c69bc2..a84b9cb2f52f2 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/repositories/DeleteInternalCcrRepositoryAction.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/repositories/DeleteInternalCcrRepositoryAction.java @@ -26,21 +26,24 @@ private DeleteInternalCcrRepositoryAction() { super(NAME, in -> ActionResponse.Empty.INSTANCE); } - public static class TransportDeleteInternalRepositoryAction - extends TransportAction { + public static class TransportDeleteInternalRepositoryAction extends TransportAction< + DeleteInternalCcrRepositoryRequest, + ActionResponse.Empty> { private final RepositoriesService repositoriesService; @Inject - public TransportDeleteInternalRepositoryAction(RepositoriesService repositoriesService, ActionFilters actionFilters, - TransportService transportService) { + public TransportDeleteInternalRepositoryAction( + RepositoriesService repositoriesService, + ActionFilters actionFilters, + TransportService transportService + ) { super(NAME, actionFilters, transportService.getTaskManager()); this.repositoriesService = repositoriesService; } @Override - protected void doExecute(Task task, DeleteInternalCcrRepositoryRequest request, - ActionListener listener) { + protected void doExecute(Task task, DeleteInternalCcrRepositoryRequest request, ActionListener listener) { repositoriesService.unregisterInternalRepository(request.getName()); listener.onResponse(ActionResponse.Empty.INSTANCE); } diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/repositories/DeleteInternalCcrRepositoryRequest.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/repositories/DeleteInternalCcrRepositoryRequest.java index 82a48720c4e53..23cf2b64d4118 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/repositories/DeleteInternalCcrRepositoryRequest.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/repositories/DeleteInternalCcrRepositoryRequest.java @@ -51,8 +51,6 @@ public int hashCode() { @Override public String toString() { - return "DeleteInternalRepositoryRequest{" + - "name='" + name + '\'' + - '}'; + return "DeleteInternalRepositoryRequest{" + "name='" + name + '\'' + '}'; } } diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/repositories/GetCcrRestoreFileChunkAction.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/repositories/GetCcrRestoreFileChunkAction.java index 7e857600272f1..271f275c88514 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/repositories/GetCcrRestoreFileChunkAction.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/repositories/GetCcrRestoreFileChunkAction.java @@ -37,15 +37,20 @@ private GetCcrRestoreFileChunkAction() { super(NAME, GetCcrRestoreFileChunkAction.GetCcrRestoreFileChunkResponse::new); } - public static class TransportGetCcrRestoreFileChunkAction - extends HandledTransportAction { + public static class TransportGetCcrRestoreFileChunkAction extends HandledTransportAction< + GetCcrRestoreFileChunkRequest, + GetCcrRestoreFileChunkAction.GetCcrRestoreFileChunkResponse> { private final CcrRestoreSourceService restoreSourceService; private final BigArrays bigArrays; @Inject - public TransportGetCcrRestoreFileChunkAction(BigArrays bigArrays, TransportService transportService, ActionFilters actionFilters, - CcrRestoreSourceService restoreSourceService) { + public TransportGetCcrRestoreFileChunkAction( + BigArrays bigArrays, + TransportService transportService, + ActionFilters actionFilters, + CcrRestoreSourceService restoreSourceService + ) { super(NAME, transportService, actionFilters, GetCcrRestoreFileChunkRequest::new, ThreadPool.Names.GENERIC); TransportActionProxy.registerProxyAction(transportService, NAME, false, GetCcrRestoreFileChunkResponse::new); this.restoreSourceService = restoreSourceService; @@ -53,8 +58,11 @@ public TransportGetCcrRestoreFileChunkAction(BigArrays bigArrays, TransportServi } @Override - protected void doExecute(Task task, GetCcrRestoreFileChunkRequest request, - ActionListener listener) { + protected void doExecute( + Task task, + GetCcrRestoreFileChunkRequest request, + ActionListener listener + ) { int bytesRequested = request.getSize(); ByteArray array = bigArrays.newByteArray(bytesRequested, false); String fileName = request.getFileName(); diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/repositories/PutCcrRestoreSessionAction.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/repositories/PutCcrRestoreSessionAction.java index f2f42c52e0df9..eb8038755d782 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/repositories/PutCcrRestoreSessionAction.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/repositories/PutCcrRestoreSessionAction.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.ccr.action.repositories; -import org.elasticsearch.action.ActionType; import org.elasticsearch.action.ActionResponse; +import org.elasticsearch.action.ActionType; import org.elasticsearch.action.support.ActionFilters; import org.elasticsearch.action.support.single.shard.TransportSingleShardAction; import org.elasticsearch.cluster.ClusterState; @@ -40,18 +40,33 @@ private PutCcrRestoreSessionAction() { super(NAME, PutCcrRestoreSessionResponse::new); } - public static class TransportPutCcrRestoreSessionAction - extends TransportSingleShardAction { + public static class TransportPutCcrRestoreSessionAction extends TransportSingleShardAction< + PutCcrRestoreSessionRequest, + PutCcrRestoreSessionResponse> { private final IndicesService indicesService; private final CcrRestoreSourceService ccrRestoreService; @Inject - public TransportPutCcrRestoreSessionAction(ThreadPool threadPool, ClusterService clusterService, ActionFilters actionFilters, - IndexNameExpressionResolver resolver, TransportService transportService, - IndicesService indicesService, CcrRestoreSourceService ccrRestoreService) { - super(NAME, threadPool, clusterService, transportService, actionFilters, resolver, PutCcrRestoreSessionRequest::new, - ThreadPool.Names.GENERIC); + public TransportPutCcrRestoreSessionAction( + ThreadPool threadPool, + ClusterService clusterService, + ActionFilters actionFilters, + IndexNameExpressionResolver resolver, + TransportService transportService, + IndicesService indicesService, + CcrRestoreSourceService ccrRestoreService + ) { + super( + NAME, + threadPool, + clusterService, + transportService, + actionFilters, + resolver, + PutCcrRestoreSessionRequest::new, + ThreadPool.Names.GENERIC + ); this.indicesService = indicesService; this.ccrRestoreService = ccrRestoreService; } @@ -84,7 +99,6 @@ protected ShardsIterator shards(ClusterState state, InternalRequest request) { } } - public static class PutCcrRestoreSessionResponse extends ActionResponse { private DiscoveryNode node; diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/repositories/PutInternalCcrRepositoryAction.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/repositories/PutInternalCcrRepositoryAction.java index 7cf883b7ca234..d8e323583c4de 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/repositories/PutInternalCcrRepositoryAction.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/repositories/PutInternalCcrRepositoryAction.java @@ -26,21 +26,24 @@ private PutInternalCcrRepositoryAction() { super(NAME, in -> ActionResponse.Empty.INSTANCE); } - public static class TransportPutInternalRepositoryAction - extends TransportAction { + public static class TransportPutInternalRepositoryAction extends TransportAction< + PutInternalCcrRepositoryRequest, + ActionResponse.Empty> { private final RepositoriesService repositoriesService; @Inject - public TransportPutInternalRepositoryAction(RepositoriesService repositoriesService, ActionFilters actionFilters, - TransportService transportService) { + public TransportPutInternalRepositoryAction( + RepositoriesService repositoriesService, + ActionFilters actionFilters, + TransportService transportService + ) { super(NAME, actionFilters, transportService.getTaskManager()); this.repositoriesService = repositoriesService; } @Override - protected void doExecute(Task task, PutInternalCcrRepositoryRequest request, - ActionListener listener) { + protected void doExecute(Task task, PutInternalCcrRepositoryRequest request, ActionListener listener) { repositoriesService.registerInternalRepository(request.getName(), request.getType()); listener.onResponse(ActionResponse.Empty.INSTANCE); } diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/repositories/PutInternalCcrRepositoryRequest.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/repositories/PutInternalCcrRepositoryRequest.java index 5b133ffc352a6..152555073ef00 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/repositories/PutInternalCcrRepositoryRequest.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/repositories/PutInternalCcrRepositoryRequest.java @@ -47,8 +47,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; PutInternalCcrRepositoryRequest that = (PutInternalCcrRepositoryRequest) o; - return Objects.equals(name, that.name) && - Objects.equals(type, that.type); + return Objects.equals(name, that.name) && Objects.equals(type, that.type); } @Override @@ -58,9 +57,6 @@ public int hashCode() { @Override public String toString() { - return "PutInternalCcrRepositoryRequest{" + - "name='" + name + '\'' + - ", type='" + type + '\'' + - '}'; + return "PutInternalCcrRepositoryRequest{" + "name='" + name + '\'' + ", type='" + type + '\'' + '}'; } } diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/allocation/CcrPrimaryFollowerAllocationDecider.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/allocation/CcrPrimaryFollowerAllocationDecider.java index b069dd6208c79..caa74524105ff 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/allocation/CcrPrimaryFollowerAllocationDecider.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/allocation/CcrPrimaryFollowerAllocationDecider.java @@ -36,14 +36,25 @@ public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, Routing } final RecoverySource recoverySource = shardRouting.recoverySource(); if (recoverySource == null || recoverySource.getType() != RecoverySource.Type.SNAPSHOT) { - return allocation.decision(Decision.YES, NAME, - "shard is a primary follower but was bootstrapped already; hence is not under the purview of this decider"); + return allocation.decision( + Decision.YES, + NAME, + "shard is a primary follower but was bootstrapped already; hence is not under the purview of this decider" + ); } if (node.node().isRemoteClusterClient() == false) { - return allocation.decision(Decision.NO, NAME, "shard is a primary follower and being bootstrapped, but node does not have the " - + DiscoveryNodeRole.REMOTE_CLUSTER_CLIENT_ROLE.roleName() + " role"); + return allocation.decision( + Decision.NO, + NAME, + "shard is a primary follower and being bootstrapped, but node does not have the " + + DiscoveryNodeRole.REMOTE_CLUSTER_CLIENT_ROLE.roleName() + + " role" + ); } - return allocation.decision(Decision.YES, NAME, - "shard is a primary follower and node has the " + DiscoveryNodeRole.REMOTE_CLUSTER_CLIENT_ROLE.roleName() + " role"); + return allocation.decision( + Decision.YES, + NAME, + "shard is a primary follower and node has the " + DiscoveryNodeRole.REMOTE_CLUSTER_CLIENT_ROLE.roleName() + " role" + ); } } diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/index/engine/FollowingEngine.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/index/engine/FollowingEngine.java index 772d9c6c86493..9987d16bae5a6 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/index/engine/FollowingEngine.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/index/engine/FollowingEngine.java @@ -37,7 +37,6 @@ */ public class FollowingEngine extends InternalEngine { - /** * Construct a new following engine with the specified engine configuration. * @@ -60,8 +59,10 @@ private static EngineConfig validateEngineConfig(final EngineConfig engineConfig private void preFlight(final Operation operation) { assert FollowingEngineAssertions.preFlight(operation); if (operation.seqNo() == SequenceNumbers.UNASSIGNED_SEQ_NO) { - throw new ElasticsearchStatusException("a following engine does not accept operations without an assigned sequence number", - RestStatus.FORBIDDEN); + throw new ElasticsearchStatusException( + "a following engine does not accept operations without an assigned sequence number", + RestStatus.FORBIDDEN + ); } } @@ -78,7 +79,10 @@ protected InternalEngine.IndexingStrategy indexingStrategyForOperation(final Ind * between the primary and replicas (see TransportBulkShardOperationsAction#shardOperationOnPrimary). */ final AlreadyProcessedFollowingEngineException error = new AlreadyProcessedFollowingEngineException( - shardId, index.seqNo(), lookupPrimaryTerm(index.seqNo())); + shardId, + index.seqNo(), + lookupPrimaryTerm(index.seqNo()) + ); return IndexingStrategy.skipDueToVersionConflict(error, false, index.version()); } else { return planIndexingAsNonPrimary(index); @@ -91,7 +95,10 @@ protected InternalEngine.DeletionStrategy deletionStrategyForOperation(final Del if (delete.origin() == Operation.Origin.PRIMARY && hasBeenProcessedBefore(delete)) { // See the comment in #indexingStrategyForOperation for the explanation why we can safely skip this operation. final AlreadyProcessedFollowingEngineException error = new AlreadyProcessedFollowingEngineException( - shardId, delete.seqNo(), lookupPrimaryTerm(delete.seqNo())); + shardId, + delete.seqNo(), + lookupPrimaryTerm(delete.seqNo()) + ); return DeletionStrategy.skipDueToVersionConflict(error, delete.version(), false); } else { return planDeletionAsNonPrimary(delete); @@ -122,8 +129,13 @@ protected void advanceMaxSeqNoOfDeletesOnPrimary(long seqNo) { if (Assertions.ENABLED) { final long localCheckpoint = getProcessedLocalCheckpoint(); final long maxSeqNoOfUpdates = getMaxSeqNoOfUpdatesOrDeletes(); - assert localCheckpoint < maxSeqNoOfUpdates || maxSeqNoOfUpdates >= seqNo : - "maxSeqNoOfUpdates is not advanced local_checkpoint=" + localCheckpoint + " msu=" + maxSeqNoOfUpdates + " seq_no=" + seqNo; + assert localCheckpoint < maxSeqNoOfUpdates || maxSeqNoOfUpdates >= seqNo + : "maxSeqNoOfUpdates is not advanced local_checkpoint=" + + localCheckpoint + + " msu=" + + maxSeqNoOfUpdates + + " seq_no=" + + seqNo; } super.advanceMaxSeqNoOfDeletesOnPrimary(seqNo); @@ -161,7 +173,7 @@ protected boolean assertNonPrimaryOrigin(final Operation operation) { @Override protected boolean assertPrimaryCanOptimizeAddDocument(final Index index) { assert index.version() == 1 && index.versionType() == VersionType.EXTERNAL - : "version [" + index.version() + "], type [" + index.versionType() + "]"; + : "version [" + index.version() + "], type [" + index.versionType() + "]"; return true; } @@ -175,8 +187,10 @@ private OptionalLong lookupPrimaryTerm(final long seqNo) throws IOException { final DirectoryReader reader = Lucene.wrapAllDocsLive(engineSearcher.getDirectoryReader()); final IndexSearcher searcher = new IndexSearcher(reader); searcher.setQueryCache(null); - final Query query = new BooleanQuery.Builder() - .add(LongPoint.newExactQuery(SeqNoFieldMapper.NAME, seqNo), BooleanClause.Occur.FILTER) + final Query query = new BooleanQuery.Builder().add( + LongPoint.newExactQuery(SeqNoFieldMapper.NAME, seqNo), + BooleanClause.Occur.FILTER + ) // excludes the non-root nested documents which don't have primary_term. .add(new DocValuesFieldExistsQuery(SeqNoFieldMapper.PRIMARY_TERM_NAME), BooleanClause.Occur.FILTER) .build(); diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/index/engine/FollowingEngineAssertions.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/index/engine/FollowingEngineAssertions.java index 8bd5c9f911c4a..a27f04784da9a 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/index/engine/FollowingEngineAssertions.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/index/engine/FollowingEngineAssertions.java @@ -24,8 +24,8 @@ static boolean preFlight(final Engine.Operation operation) { * that we also prevent issues in production code. */ assert operation.seqNo() != SequenceNumbers.UNASSIGNED_SEQ_NO; - assert (operation.origin() == Engine.Operation.Origin.PRIMARY) == (operation.versionType() == VersionType.EXTERNAL) : - "invalid version_type in a following engine; version_type=" + operation.versionType() + "origin=" + operation.origin(); + assert (operation.origin() == Engine.Operation.Origin.PRIMARY) == (operation.versionType() == VersionType.EXTERNAL) + : "invalid version_type in a following engine; version_type=" + operation.versionType() + "origin=" + operation.origin(); return true; } diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/repository/CcrRepository.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/repository/CcrRepository.java index 25053a7f09b3c..7b588aa922788 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/repository/CcrRepository.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/repository/CcrRepository.java @@ -140,8 +140,14 @@ public class CcrRepository extends AbstractLifecycleComponent implements Reposit private final CounterMetric throttledTime = new CounterMetric(); - public CcrRepository(RepositoryMetadata metadata, Client client, CcrLicenseChecker ccrLicenseChecker, Settings settings, - CcrSettings ccrSettings, ThreadPool threadPool) { + public CcrRepository( + RepositoryMetadata metadata, + Client client, + CcrLicenseChecker ccrLicenseChecker, + Settings settings, + CcrSettings ccrSettings, + ThreadPool threadPool + ) { this.metadata = metadata; this.ccrSettings = ccrSettings; this.localClusterName = ClusterName.CLUSTER_NAME_SETTING.get(settings).value(); @@ -180,25 +186,33 @@ private Client getRemoteClusterClient() { public void getSnapshotInfo(GetSnapshotInfoContext context) { final List snapshotIds = context.snapshotIds(); assert snapshotIds.size() == 1 && SNAPSHOT_ID.equals(snapshotIds.iterator().next()) - : "RemoteClusterRepository only supports " + SNAPSHOT_ID + " as the SnapshotId but saw " + snapshotIds; + : "RemoteClusterRepository only supports " + SNAPSHOT_ID + " as the SnapshotId but saw " + snapshotIds; Client remoteClient = getRemoteClusterClient(); - ClusterStateResponse response = remoteClient.admin().cluster().prepareState().clear().setMetadata(true).setNodes(true) + ClusterStateResponse response = remoteClient.admin() + .cluster() + .prepareState() + .clear() + .setMetadata(true) + .setNodes(true) .get(ccrSettings.getRecoveryActionTimeout()); Metadata metadata = response.getState().metadata(); ImmutableOpenMap indicesMap = metadata.indices(); List indices = new ArrayList<>(indicesMap.keySet()); // fork to the snapshot meta pool because the context expects to run on it and asserts that it does - threadPool.executor(ThreadPool.Names.SNAPSHOT_META).execute(() -> context.onResponse( - new SnapshotInfo( - new Snapshot(this.metadata.name(), SNAPSHOT_ID), - indices, - new ArrayList<>(metadata.dataStreams().keySet()), - Collections.emptyList(), - response.getState().getNodes().getMaxNodeVersion(), - SnapshotState.SUCCESS - ) - )); + threadPool.executor(ThreadPool.Names.SNAPSHOT_META) + .execute( + () -> context.onResponse( + new SnapshotInfo( + new Snapshot(this.metadata.name(), SNAPSHOT_ID), + indices, + new ArrayList<>(metadata.dataStreams().keySet()), + Collections.emptyList(), + response.getState().getNodes().getMaxNodeVersion(), + SnapshotState.SUCCESS + ) + ) + ); } @Override @@ -207,7 +221,9 @@ public Metadata getSnapshotGlobalMetadata(SnapshotId snapshotId) { Client remoteClient = getRemoteClusterClient(); // We set a single dummy index name to avoid fetching all the index data ClusterStateRequest clusterStateRequest = CcrRequests.metadataRequest("dummy_index_name"); - ClusterStateResponse clusterState = remoteClient.admin().cluster().state(clusterStateRequest) + ClusterStateResponse clusterState = remoteClient.admin() + .cluster() + .state(clusterStateRequest) .actionGet(ccrSettings.getRecoveryActionTimeout()); return clusterState.getState().metadata(); } @@ -219,7 +235,9 @@ public IndexMetadata getSnapshotIndexMetaData(RepositoryData repositoryData, Sna Client remoteClient = getRemoteClusterClient(); ClusterStateRequest clusterStateRequest = CcrRequests.metadataRequest(leaderIndex); - ClusterStateResponse clusterState = remoteClient.admin().cluster().state(clusterStateRequest) + ClusterStateResponse clusterState = remoteClient.admin() + .cluster() + .state(clusterStateRequest) .actionGet(ccrSettings.getRecoveryActionTimeout()); // Validates whether the leader cluster has been configured properly: @@ -254,7 +272,11 @@ public IndexMetadata getSnapshotIndexMetaData(RepositoryData repositoryData, Sna public void getRepositoryData(ActionListener listener) { ActionListener.completeWith(listener, () -> { Client remoteClient = getRemoteClusterClient(); - ClusterStateResponse response = remoteClient.admin().cluster().prepareState().clear().setMetadata(true) + ClusterStateResponse response = remoteClient.admin() + .cluster() + .prepareState() + .clear() + .setMetadata(true) .get(ccrSettings.getRecoveryActionTimeout()); Metadata remoteMetadata = response.getState().getMetadata(); @@ -268,24 +290,23 @@ public void getRepositoryData(ActionListener listener) { SnapshotId snapshotId = new SnapshotId(LATEST, LATEST); copiedSnapshotIds.put(indexName, snapshotId); final long nowMillis = threadPool.absoluteTimeInMillis(); - snapshotsDetails.put(indexName, new RepositoryData.SnapshotDetails( - SnapshotState.SUCCESS, - Version.CURRENT, - nowMillis, - nowMillis, - "")); + snapshotsDetails.put( + indexName, + new RepositoryData.SnapshotDetails(SnapshotState.SUCCESS, Version.CURRENT, nowMillis, nowMillis, "") + ); Index index = remoteIndices.get(indexName).getIndex(); indexSnapshots.put(new IndexId(indexName, index.getUUID()), Collections.singletonList(snapshotId)); } return new RepositoryData( - MISSING_UUID, - 1, - copiedSnapshotIds, - snapshotsDetails, - indexSnapshots, - ShardGenerations.EMPTY, - IndexMetaDataGenerations.EMPTY, - MISSING_UUID); + MISSING_UUID, + 1, + copiedSnapshotIds, + snapshotsDetails, + indexSnapshots, + ShardGenerations.EMPTY, + IndexMetaDataGenerations.EMPTY, + MISSING_UUID + ); }); } @@ -295,8 +316,12 @@ public void finalizeSnapshot(FinalizeSnapshotContext finalizeSnapshotContext) { } @Override - public void deleteSnapshots(Collection snapshotIds, long repositoryStateId, Version repositoryMetaVersion, - ActionListener listener) { + public void deleteSnapshots( + Collection snapshotIds, + long repositoryStateId, + Version repositoryMetaVersion, + ActionListener listener + ) { throw new UnsupportedOperationException("Unsupported for repository of type: " + TYPE); } @@ -321,8 +346,7 @@ public void endVerification(String verificationToken) { } @Override - public void verify(String verificationToken, DiscoveryNode localNode) { - } + public void verify(String verificationToken, DiscoveryNode localNode) {} @Override public boolean isReadOnly() { @@ -335,13 +359,22 @@ public void snapshotShard(SnapshotShardContext context) { } @Override - public void restoreShard(Store store, SnapshotId snapshotId, IndexId indexId, ShardId snapshotShardId, RecoveryState recoveryState, - ActionListener listener) { + public void restoreShard( + Store store, + SnapshotId snapshotId, + IndexId indexId, + ShardId snapshotShardId, + RecoveryState recoveryState, + ActionListener listener + ) { final ShardId shardId = store.shardId(); final LinkedList toClose = new LinkedList<>(); - final ActionListener restoreListener = ActionListener.runBefore(listener.delegateResponse( - (l, e) -> l.onFailure(new IndexShardRestoreFailedException(shardId, "failed to restore snapshot [" + snapshotId + "]", e))), - () -> IOUtils.close(toClose)); + final ActionListener restoreListener = ActionListener.runBefore( + listener.delegateResponse( + (l, e) -> l.onFailure(new IndexShardRestoreFailedException(shardId, "failed to restore snapshot [" + snapshotId + "]", e)) + ), + () -> IOUtils.close(toClose) + ); try { // TODO: Add timeouts to network calls / the restore process. createEmptyStore(store); @@ -354,46 +387,48 @@ public void restoreShard(Store store, SnapshotId snapshotId, IndexId indexId, Sh final Client remoteClient = getRemoteClusterClient(); - final String retentionLeaseId = - retentionLeaseId(localClusterName, shardId.getIndex(), remoteClusterAlias, leaderIndex); + final String retentionLeaseId = retentionLeaseId(localClusterName, shardId.getIndex(), remoteClusterAlias, leaderIndex); acquireRetentionLeaseOnLeader(shardId, retentionLeaseId, leaderShardId, remoteClient); // schedule renewals to run during the restore - final Scheduler.Cancellable renewable = threadPool.scheduleWithFixedDelay( - () -> { - logger.trace("{} background renewal of retention lease [{}] during restore", shardId, retentionLeaseId); - final ThreadContext threadContext = threadPool.getThreadContext(); - try (ThreadContext.StoredContext ignore = threadContext.stashContext()) { - // we have to execute under the system context so that if security is enabled the renewal is authorized - threadContext.markAsSystemContext(); - CcrRetentionLeases.asyncRenewRetentionLease( - leaderShardId, - retentionLeaseId, - RETAIN_ALL, - remoteClient, - ActionListener.wrap( - r -> {}, - e -> { - final Throwable cause = ExceptionsHelper.unwrapCause(e); - assert cause instanceof ElasticsearchSecurityException == false : cause; - if (cause instanceof RetentionLeaseInvalidRetainingSeqNoException == false) { - logger.warn(new ParameterizedMessage( - "{} background renewal of retention lease [{}] failed during restore", shardId, - retentionLeaseId), cause); - } - })); - } - }, + final Scheduler.Cancellable renewable = threadPool.scheduleWithFixedDelay(() -> { + logger.trace("{} background renewal of retention lease [{}] during restore", shardId, retentionLeaseId); + final ThreadContext threadContext = threadPool.getThreadContext(); + try (ThreadContext.StoredContext ignore = threadContext.stashContext()) { + // we have to execute under the system context so that if security is enabled the renewal is authorized + threadContext.markAsSystemContext(); + CcrRetentionLeases.asyncRenewRetentionLease( + leaderShardId, + retentionLeaseId, + RETAIN_ALL, + remoteClient, + ActionListener.wrap(r -> {}, e -> { + final Throwable cause = ExceptionsHelper.unwrapCause(e); + assert cause instanceof ElasticsearchSecurityException == false : cause; + if (cause instanceof RetentionLeaseInvalidRetainingSeqNoException == false) { + logger.warn( + new ParameterizedMessage( + "{} background renewal of retention lease [{}] failed during restore", + shardId, + retentionLeaseId + ), + cause + ); + } + }) + ); + } + }, CcrRetentionLeases.RETENTION_LEASE_RENEW_INTERVAL_SETTING.get(store.indexSettings().getNodeSettings()), - Ccr.CCR_THREAD_POOL_NAME); + Ccr.CCR_THREAD_POOL_NAME + ); toClose.add(() -> { - logger.trace( - "{} canceling background renewal of retention lease [{}] at the end of restore", shardId, retentionLeaseId); + logger.trace("{} canceling background renewal of retention lease [{}] at the end of restore", shardId, retentionLeaseId); renewable.cancel(); }); // TODO: There should be some local timeout. And if the remote cluster returns an unknown session - // response, we should be able to retry by creating a new session. + // response, we should be able to retry by creating a new session. final RestoreSession restoreSession = openSession(metadata.name(), remoteClient, leaderShardId, shardId, recoveryState); toClose.addFirst(restoreSession); // Some tests depend on closing session before cancelling retention lease renewal restoreSession.restoreFiles(store, ActionListener.wrap(v -> { @@ -418,31 +453,48 @@ private void createEmptyStore(Store store) { } void acquireRetentionLeaseOnLeader( - final ShardId shardId, - final String retentionLeaseId, - final ShardId leaderShardId, - final Client remoteClient) { - logger.trace( - () -> new ParameterizedMessage("{} requesting leader to add retention lease [{}]", shardId, retentionLeaseId)); + final ShardId shardId, + final String retentionLeaseId, + final ShardId leaderShardId, + final Client remoteClient + ) { + logger.trace(() -> new ParameterizedMessage("{} requesting leader to add retention lease [{}]", shardId, retentionLeaseId)); final TimeValue timeout = ccrSettings.getRecoveryActionTimeout(); - final Optional maybeAddAlready = - syncAddRetentionLease(leaderShardId, retentionLeaseId, RETAIN_ALL, remoteClient, timeout); + final Optional maybeAddAlready = syncAddRetentionLease( + leaderShardId, + retentionLeaseId, + RETAIN_ALL, + remoteClient, + timeout + ); maybeAddAlready.ifPresent(addAlready -> { - logger.trace(() -> new ParameterizedMessage( - "{} retention lease [{}] already exists, requesting a renewal", - shardId, - retentionLeaseId), - addAlready); - final Optional maybeRenewNotFound = - syncRenewRetentionLease(leaderShardId, retentionLeaseId, RETAIN_ALL, remoteClient, timeout); + logger.trace( + () -> new ParameterizedMessage("{} retention lease [{}] already exists, requesting a renewal", shardId, retentionLeaseId), + addAlready + ); + final Optional maybeRenewNotFound = syncRenewRetentionLease( + leaderShardId, + retentionLeaseId, + RETAIN_ALL, + remoteClient, + timeout + ); maybeRenewNotFound.ifPresent(renewNotFound -> { - logger.trace(() -> new ParameterizedMessage( - "{} retention lease [{}] not found while attempting to renew, requesting a final add", - shardId, - retentionLeaseId), - renewNotFound); - final Optional maybeFallbackAddAlready = - syncAddRetentionLease(leaderShardId, retentionLeaseId, RETAIN_ALL, remoteClient, timeout); + logger.trace( + () -> new ParameterizedMessage( + "{} retention lease [{}] not found while attempting to renew, requesting a final add", + shardId, + retentionLeaseId + ), + renewNotFound + ); + final Optional maybeFallbackAddAlready = syncAddRetentionLease( + leaderShardId, + retentionLeaseId, + RETAIN_ALL, + remoteClient, + timeout + ); maybeFallbackAddAlready.ifPresent(fallbackAddAlready -> { /* * At this point we tried to add the lease and the retention lease already existed. By the time we tried to renew the @@ -461,14 +513,15 @@ void acquireRetentionLeaseOnLeader( public IndexShardSnapshotStatus getShardSnapshotStatus(SnapshotId snapshotId, IndexId index, ShardId shardId) { assert SNAPSHOT_ID.equals(snapshotId) : "RemoteClusterRepository only supports " + SNAPSHOT_ID + " as the SnapshotId"; final String leaderIndex = index.getName(); - final IndicesStatsResponse response = getRemoteClusterClient().admin().indices().prepareStats(leaderIndex) - .clear().setStore(true) + final IndicesStatsResponse response = getRemoteClusterClient().admin() + .indices() + .prepareStats(leaderIndex) + .clear() + .setStore(true) .get(ccrSettings.getRecoveryActionTimeout()); for (ShardStats shardStats : response.getIndex(leaderIndex).getShards()) { final ShardRouting shardRouting = shardStats.getShardRouting(); - if (shardRouting.shardId().id() == shardId.getId() - && shardRouting.primary() - && shardRouting.active()) { + if (shardRouting.shardId().id() == shardId.getId() && shardRouting.primary() && shardRouting.active()) { // we only care about the shard size here for shard allocation, populate the rest with dummy values final long totalSize = shardStats.getStats().getStore().getSizeInBytes(); return IndexShardSnapshotStatus.newDone(0L, 0L, 1, 1, totalSize, totalSize, DUMMY_GENERATION); @@ -478,27 +531,38 @@ public IndexShardSnapshotStatus getShardSnapshotStatus(SnapshotId snapshotId, In } @Override - public void updateState(ClusterState state) { - } + public void updateState(ClusterState state) {} @Override - public void executeConsistentStateUpdate(Function createUpdateTask, String source, - Consumer onFailure) { + public void executeConsistentStateUpdate( + Function createUpdateTask, + String source, + Consumer onFailure + ) { throw new UnsupportedOperationException("Unsupported for repository of type: " + TYPE); } @Override - public void cloneShardSnapshot(SnapshotId source, SnapshotId target, RepositoryShardId shardId, ShardGeneration shardGeneration, - ActionListener listener) { + public void cloneShardSnapshot( + SnapshotId source, + SnapshotId target, + RepositoryShardId shardId, + ShardGeneration shardGeneration, + ActionListener listener + ) { throw new UnsupportedOperationException("Unsupported for repository of type: " + TYPE); } @Override - public void awaitIdle() { - } - - private void updateMappings(Client leaderClient, Index leaderIndex, long leaderMappingVersion, - Client followerClient, Index followerIndex) { + public void awaitIdle() {} + + private void updateMappings( + Client leaderClient, + Index leaderIndex, + long leaderMappingVersion, + Client followerClient, + Index followerIndex + ) { final PlainActionFuture indexMetadataFuture = new PlainActionFuture<>(); final long startTimeInNanos = System.nanoTime(); final Supplier timeout = () -> { @@ -514,13 +578,31 @@ private void updateMappings(Client leaderClient, Index leaderIndex, long leaderM } } - RestoreSession openSession(String repositoryName, Client remoteClient, ShardId leaderShardId, ShardId indexShardId, - RecoveryState recoveryState) { + RestoreSession openSession( + String repositoryName, + Client remoteClient, + ShardId leaderShardId, + ShardId indexShardId, + RecoveryState recoveryState + ) { String sessionUUID = UUIDs.randomBase64UUID(); - PutCcrRestoreSessionAction.PutCcrRestoreSessionResponse response = remoteClient.execute(PutCcrRestoreSessionAction.INSTANCE, - new PutCcrRestoreSessionRequest(sessionUUID, leaderShardId)).actionGet(ccrSettings.getRecoveryActionTimeout()); - return new RestoreSession(repositoryName, remoteClient, sessionUUID, response.getNode(), indexShardId, recoveryState, - response.getStoreFileMetadata(), response.getMappingVersion(), threadPool, ccrSettings, throttledTime::inc); + PutCcrRestoreSessionAction.PutCcrRestoreSessionResponse response = remoteClient.execute( + PutCcrRestoreSessionAction.INSTANCE, + new PutCcrRestoreSessionRequest(sessionUUID, leaderShardId) + ).actionGet(ccrSettings.getRecoveryActionTimeout()); + return new RestoreSession( + repositoryName, + remoteClient, + sessionUUID, + response.getNode(), + indexShardId, + recoveryState, + response.getStoreFileMetadata(), + response.getMappingVersion(), + threadPool, + ccrSettings, + throttledTime::inc + ); } private static class RestoreSession extends FileRestoreContext implements Closeable { @@ -534,9 +616,19 @@ private static class RestoreSession extends FileRestoreContext implements Closea private final LongConsumer throttleListener; private final ThreadPool threadPool; - RestoreSession(String repositoryName, Client remoteClient, String sessionUUID, DiscoveryNode node, ShardId shardId, - RecoveryState recoveryState, Store.MetadataSnapshot sourceMetadata, long mappingVersion, - ThreadPool threadPool, CcrSettings ccrSettings, LongConsumer throttleListener) { + RestoreSession( + String repositoryName, + Client remoteClient, + String sessionUUID, + DiscoveryNode node, + ShardId shardId, + RecoveryState recoveryState, + Store.MetadataSnapshot sourceMetadata, + long mappingVersion, + ThreadPool threadPool, + CcrSettings ccrSettings, + LongConsumer throttleListener + ) { super(repositoryName, shardId, SNAPSHOT_ID, recoveryState); this.remoteClient = remoteClient; this.sessionUUID = sessionUUID; @@ -563,10 +655,14 @@ protected void restoreFiles(List filesToRecover, Store store, ActionLi logger.trace("[{}] starting CCR restore of {} files", shardId, filesToRecover); final List mds = filesToRecover.stream().map(FileInfo::metadata).collect(Collectors.toList()); final MultiChunkTransfer multiFileTransfer = new MultiChunkTransfer<>( - logger, threadPool.getThreadContext(), allFilesListener, ccrSettings.getMaxConcurrentFileChunks(), mds) { - - final MultiFileWriter multiFileWriter = new MultiFileWriter(store, recoveryState.getIndex(), "", logger, () -> { - }); + logger, + threadPool.getThreadContext(), + allFilesListener, + ccrSettings.getMaxConcurrentFileChunks(), + mds + ) { + + final MultiFileWriter multiFileWriter = new MultiFileWriter(store, recoveryState.getIndex(), "", logger, () -> {}); long offset = 0; @Override @@ -583,47 +679,58 @@ protected FileChunk nextChunkRequest(StoreFileMetadata md) { @Override protected void executeChunkRequest(FileChunk request, ActionListener listener) { - remoteClient.execute(GetCcrRestoreFileChunkAction.INSTANCE, + remoteClient.execute( + GetCcrRestoreFileChunkAction.INSTANCE, new GetCcrRestoreFileChunkRequest(node, sessionUUID, request.md.name(), request.bytesRequested), - ListenerTimeouts.wrapWithTimeout(threadPool, new ActionListener<>() { - @Override - public void onResponse( - GetCcrRestoreFileChunkAction.GetCcrRestoreFileChunkResponse getCcrRestoreFileChunkResponse) { - getCcrRestoreFileChunkResponse.incRef(); - threadPool.generic().execute(new ActionRunnable<>(listener) { - @Override - protected void doRun() throws Exception { - writeFileChunk(request.md, getCcrRestoreFileChunkResponse); - listener.onResponse(null); - } - - @Override - public void onAfter() { - getCcrRestoreFileChunkResponse.decRef(); - } - }); - } - - @Override - public void onFailure(Exception e) { - threadPool.generic().execute(() -> { - try { - listener.onFailure(e); - } catch (Exception ex) { - e.addSuppressed(ex); - logger.warn(() -> - new ParameterizedMessage("failed to execute failure callback for chunk request"), e); - } - }); - } - }, ccrSettings.getRecoveryActionTimeout(), ThreadPool.Names.GENERIC, GetCcrRestoreFileChunkAction.NAME)); + ListenerTimeouts.wrapWithTimeout(threadPool, new ActionListener<>() { + @Override + public void onResponse( + GetCcrRestoreFileChunkAction.GetCcrRestoreFileChunkResponse getCcrRestoreFileChunkResponse + ) { + getCcrRestoreFileChunkResponse.incRef(); + threadPool.generic().execute(new ActionRunnable<>(listener) { + @Override + protected void doRun() throws Exception { + writeFileChunk(request.md, getCcrRestoreFileChunkResponse); + listener.onResponse(null); + } + + @Override + public void onAfter() { + getCcrRestoreFileChunkResponse.decRef(); + } + }); + } + + @Override + public void onFailure(Exception e) { + threadPool.generic().execute(() -> { + try { + listener.onFailure(e); + } catch (Exception ex) { + e.addSuppressed(ex); + logger.warn( + () -> new ParameterizedMessage("failed to execute failure callback for chunk request"), + e + ); + } + }); + } + }, ccrSettings.getRecoveryActionTimeout(), ThreadPool.Names.GENERIC, GetCcrRestoreFileChunkAction.NAME) + ); } - private void writeFileChunk(StoreFileMetadata md, - GetCcrRestoreFileChunkAction.GetCcrRestoreFileChunkResponse r) throws Exception { + private void writeFileChunk(StoreFileMetadata md, GetCcrRestoreFileChunkAction.GetCcrRestoreFileChunkResponse r) + throws Exception { final int actualChunkSize = r.getChunk().length(); - logger.trace("[{}] [{}] got response for file [{}], offset: {}, length: {}", - shardId, snapshotId, md.name(), r.getOffset(), actualChunkSize); + logger.trace( + "[{}] [{}] got response for file [{}], offset: {}, length: {}", + shardId, + snapshotId, + md.name(), + r.getOffset(), + actualChunkSize + ); final long nanosPaused = ccrSettings.getRateLimiter().maybePause(actualChunkSize); throttleListener.accept(nanosPaused); multiFileWriter.incRef(); @@ -661,8 +768,8 @@ public void close() { @Override public void close() { ClearCcrRestoreSessionRequest clearRequest = new ClearCcrRestoreSessionRequest(sessionUUID, node); - ActionResponse.Empty response = - remoteClient.execute(ClearCcrRestoreSessionAction.INSTANCE, clearRequest).actionGet(ccrSettings.getRecoveryActionTimeout()); + ActionResponse.Empty response = remoteClient.execute(ClearCcrRestoreSessionAction.INSTANCE, clearRequest) + .actionGet(ccrSettings.getRecoveryActionTimeout()); } private static class FileChunk implements MultiChunkTransfer.ChunkRequest { diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/repository/CcrRestoreSourceService.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/repository/CcrRestoreSourceService.java index 350584a46431c..164005f50ec15 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/repository/CcrRestoreSourceService.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/repository/CcrRestoreSourceService.java @@ -13,17 +13,17 @@ import org.apache.lucene.store.IndexInput; import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.BytesRefIterator; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.component.AbstractLifecycleComponent; -import org.elasticsearch.core.Releasable; import org.elasticsearch.common.metrics.CounterMetric; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.CombinedRateLimiter; -import org.elasticsearch.core.AbstractRefCounted; import org.elasticsearch.common.util.concurrent.ConcurrentCollections; import org.elasticsearch.common.util.concurrent.KeyedLock; +import org.elasticsearch.core.AbstractRefCounted; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.core.Releasable; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.index.engine.Engine; import org.elasticsearch.index.shard.IndexEventListener; @@ -190,8 +190,12 @@ private static class RestoreSession extends AbstractRefCounted { private final Map cachedInputs = new ConcurrentHashMap<>(); private volatile boolean idle = false; - private RestoreSession(String sessionUUID, IndexShard indexShard, Engine.IndexCommitRef commitRef, - Scheduler.Cancellable timeoutTask) { + private RestoreSession( + String sessionUUID, + IndexShard indexShard, + Engine.IndexCommitRef commitRef, + Scheduler.Cancellable timeoutTask + ) { this.sessionUUID = sessionUUID; this.indexShard = indexShard; this.commitRef = commitRef; diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestForgetFollowerAction.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestForgetFollowerAction.java index cf188bbe3dc33..f2cd557025af8 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestForgetFollowerAction.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestForgetFollowerAction.java @@ -8,10 +8,10 @@ package org.elasticsearch.xpack.ccr.rest; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestToXContentListener; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ccr.action.ForgetFollowerAction; import org.elasticsearch.xpack.core.ccr.action.ForgetFollowerAction.Request; diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestGetAutoFollowPatternAction.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestGetAutoFollowPatternAction.java index 35772597eaa7f..9294f4b387cd8 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestGetAutoFollowPatternAction.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestGetAutoFollowPatternAction.java @@ -21,9 +21,7 @@ public class RestGetAutoFollowPatternAction extends BaseRestHandler { @Override public List routes() { - return List.of( - new Route(GET, "/_ccr/auto_follow/{name}"), - new Route(GET, "/_ccr/auto_follow")); + return List.of(new Route(GET, "/_ccr/auto_follow/{name}"), new Route(GET, "/_ccr/auto_follow")); } @Override diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestPutAutoFollowPatternAction.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestPutAutoFollowPatternAction.java index 0e39fd2a026a5..3b8a3e844b34a 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestPutAutoFollowPatternAction.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestPutAutoFollowPatternAction.java @@ -7,10 +7,10 @@ package org.elasticsearch.xpack.ccr.rest; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestToXContentListener; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ccr.action.PutAutoFollowPatternAction.Request; import java.io.IOException; diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestPutFollowAction.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestPutFollowAction.java index 98789508028d7..5b7037e27f2a3 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestPutFollowAction.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestPutFollowAction.java @@ -8,10 +8,10 @@ import org.elasticsearch.action.support.ActiveShardCount; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestToXContentListener; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.List; diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestResumeFollowAction.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestResumeFollowAction.java index 4bfa239b348c5..0ee7722d56598 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestResumeFollowAction.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/rest/RestResumeFollowAction.java @@ -7,10 +7,10 @@ package org.elasticsearch.xpack.ccr.rest; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestToXContentListener; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.List; diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/CcrIntegTestCase.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/CcrIntegTestCase.java index b63fca82f41b9..d82f4e345ecfc 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/CcrIntegTestCase.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/CcrIntegTestCase.java @@ -46,9 +46,8 @@ import org.elasticsearch.common.network.NetworkModule; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeValue; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.set.Sets; -import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexService; @@ -83,6 +82,7 @@ import org.elasticsearch.transport.TransportRequest; import org.elasticsearch.transport.TransportService; import org.elasticsearch.transport.nio.MockNioTransportPlugin; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.ccr.CcrSettings; import org.elasticsearch.xpack.ccr.LocalStateCcr; import org.elasticsearch.xpack.core.XPackSettings; @@ -119,12 +119,12 @@ import java.util.stream.Collectors; import java.util.stream.Stream; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.discovery.DiscoveryModule.DISCOVERY_SEED_PROVIDERS_SETTING; import static org.elasticsearch.discovery.SettingsBasedSeedHostsProvider.DISCOVERY_SEED_HOSTS_SETTING; import static org.elasticsearch.snapshots.RestoreService.restoreInProgress; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.greaterThanOrEqualTo; @@ -160,13 +160,28 @@ public final void startClusters() throws Exception { } stopClusters(); - Collection> mockPlugins = Arrays.asList(ESIntegTestCase.TestSeedPlugin.class, - MockHttpTransport.TestPlugin.class, MockTransportService.TestPlugin.class, - MockNioTransportPlugin.class, InternalSettingsPlugin.class); + Collection> mockPlugins = Arrays.asList( + ESIntegTestCase.TestSeedPlugin.class, + MockHttpTransport.TestPlugin.class, + MockTransportService.TestPlugin.class, + MockNioTransportPlugin.class, + InternalSettingsPlugin.class + ); - InternalTestCluster leaderCluster = new InternalTestCluster(randomLong(), createTempDir(), true, true, numberOfNodesPerCluster(), - numberOfNodesPerCluster(), "leader_cluster", createNodeConfigurationSource(null, true), 0, "leader", mockPlugins, - Function.identity()); + InternalTestCluster leaderCluster = new InternalTestCluster( + randomLong(), + createTempDir(), + true, + true, + numberOfNodesPerCluster(), + numberOfNodesPerCluster(), + "leader_cluster", + createNodeConfigurationSource(null, true), + 0, + "leader", + mockPlugins, + Function.identity() + ); leaderCluster.beforeTest(random()); leaderCluster.ensureAtLeastNumDataNodes(numberOfNodesPerCluster()); assertBusy(() -> { @@ -175,9 +190,20 @@ public final void startClusters() throws Exception { }); String address = leaderCluster.getDataNodeInstance(TransportService.class).boundAddress().publishAddress().toString(); - InternalTestCluster followerCluster = new InternalTestCluster(randomLong(), createTempDir(), true, true, numberOfNodesPerCluster(), - numberOfNodesPerCluster(), "follower_cluster", createNodeConfigurationSource(address, false), 0, "follower", - mockPlugins, Function.identity()); + InternalTestCluster followerCluster = new InternalTestCluster( + randomLong(), + createTempDir(), + true, + true, + numberOfNodesPerCluster(), + numberOfNodesPerCluster(), + "follower_cluster", + createNodeConfigurationSource(address, false), + 0, + "follower", + mockPlugins, + Function.identity() + ); clusterGroup = new ClusterGroup(leaderCluster, followerCluster); followerCluster.beforeTest(random()); @@ -208,21 +234,23 @@ protected void setupMasterNodeRequestsValidatorOnFollowerCluster() { protected void removeMasterNodeRequestsValidatorOnFollowerCluster() { final InternalTestCluster followerCluster = clusterGroup.followerCluster; for (String nodeName : followerCluster.getNodeNames()) { - MockTransportService transportService = - (MockTransportService) getFollowerCluster().getInstance(TransportService.class, nodeName); + MockTransportService transportService = (MockTransportService) getFollowerCluster().getInstance( + TransportService.class, + nodeName + ); transportService.clearAllRules(); } } private static boolean isCcrAdminRequest(TransportRequest request) { - return request instanceof PutFollowAction.Request || - request instanceof ResumeFollowAction.Request || - request instanceof PauseFollowAction.Request || - request instanceof UnfollowAction.Request || - request instanceof ForgetFollowerAction.Request || - request instanceof PutAutoFollowPatternAction.Request || - request instanceof ActivateAutoFollowPatternAction.Request || - request instanceof DeleteAutoFollowPatternAction.Request; + return request instanceof PutFollowAction.Request + || request instanceof ResumeFollowAction.Request + || request instanceof PauseFollowAction.Request + || request instanceof UnfollowAction.Request + || request instanceof ForgetFollowerAction.Request + || request instanceof PutAutoFollowPatternAction.Request + || request instanceof ActivateAutoFollowPatternAction.Request + || request instanceof DeleteAutoFollowPatternAction.Request; } /** @@ -304,18 +332,22 @@ public Path nodeConfigPath(int nodeOrdinal) { @Override public Collection> nodePlugins() { return Stream.concat( - Stream.of(LocalStateCcr.class, CommonAnalysisPlugin.class), - CcrIntegTestCase.this.nodePlugins().stream()) - .collect(Collectors.toList()); + Stream.of(LocalStateCcr.class, CommonAnalysisPlugin.class), + CcrIntegTestCase.this.nodePlugins().stream() + ).collect(Collectors.toList()); } }; } @Override public List filteredWarnings() { - return Stream.concat(super.filteredWarnings().stream(), - List.of("Configuring multiple [path.data] paths is deprecated. Use RAID or other system level features for utilizing " + - "multiple disks. This feature will be removed in 8.0.").stream()).collect(Collectors.toList()); + return Stream.concat( + super.filteredWarnings().stream(), + List.of( + "Configuring multiple [path.data] paths is deprecated. Use RAID or other system level features for utilizing " + + "multiple disks. This feature will be removed in 8.0." + ).stream() + ).collect(Collectors.toList()); } @AfterClass @@ -367,15 +399,22 @@ protected final ClusterHealthStatus ensureFollowerGreen(String... indices) { protected final ClusterHealthStatus ensureFollowerGreen(boolean waitForNoInitializingShards, String... indices) { logger.info("ensure green follower indices {}", Arrays.toString(indices)); - return ensureColor(clusterGroup.followerCluster, ClusterHealthStatus.GREEN, TimeValue.timeValueSeconds(60), - waitForNoInitializingShards, indices); + return ensureColor( + clusterGroup.followerCluster, + ClusterHealthStatus.GREEN, + TimeValue.timeValueSeconds(60), + waitForNoInitializingShards, + indices + ); } - private ClusterHealthStatus ensureColor(TestCluster testCluster, - ClusterHealthStatus clusterHealthStatus, - TimeValue timeout, - boolean waitForNoInitializingShards, - String... indices) { + private ClusterHealthStatus ensureColor( + TestCluster testCluster, + ClusterHealthStatus clusterHealthStatus, + TimeValue timeout, + boolean waitForNoInitializingShards, + String... indices + ) { String color = clusterHealthStatus.name().toLowerCase(Locale.ROOT); String method = "ensure" + Strings.capitalize(color); @@ -389,13 +428,14 @@ private ClusterHealthStatus ensureColor(TestCluster testCluster, ClusterHealthResponse actionGet = testCluster.client().admin().cluster().health(healthRequest).actionGet(); if (actionGet.isTimedOut()) { - logger.info("{} timed out: " + - "\nleader cluster state:\n{}" + - "\nleader cluster hot threads:\n{}" + - "\nleader cluster tasks:\n{}" + - "\nfollower cluster state:\n{}" + - "\nfollower cluster hot threads:\n{}" + - "\nfollower cluster tasks:\n{}", + logger.info( + "{} timed out: " + + "\nleader cluster state:\n{}" + + "\nleader cluster hot threads:\n{}" + + "\nleader cluster tasks:\n{}" + + "\nfollower cluster state:\n{}" + + "\nfollower cluster hot threads:\n{}" + + "\nfollower cluster tasks:\n{}", method, leaderClient().admin().cluster().prepareState().get().getState(), getHotThreads(leaderClient()), @@ -406,15 +446,26 @@ private ClusterHealthStatus ensureColor(TestCluster testCluster, ); fail("timed out waiting for " + color + " state"); } - assertThat("Expected at least " + clusterHealthStatus + " but got " + actionGet.getStatus(), - actionGet.getStatus().value(), lessThanOrEqualTo(clusterHealthStatus.value())); + assertThat( + "Expected at least " + clusterHealthStatus + " but got " + actionGet.getStatus(), + actionGet.getStatus().value(), + lessThanOrEqualTo(clusterHealthStatus.value()) + ); logger.debug("indices {} are {}", indices.length == 0 ? "[_all]" : indices, color); return actionGet.getStatus(); } static String getHotThreads(Client client) { - return client.admin().cluster().prepareNodesHotThreads().setThreads(99999).setIgnoreIdleThreads(false) - .get().getNodes().stream().map(NodeHotThreads::getHotThreads).collect(Collectors.joining("\n")); + return client.admin() + .cluster() + .prepareNodesHotThreads() + .setThreads(99999) + .setIgnoreIdleThreads(false) + .get() + .getNodes() + .stream() + .map(NodeHotThreads::getHotThreads) + .collect(Collectors.joining("\n")); } protected final Index resolveLeaderIndex(String index) { @@ -439,8 +490,10 @@ protected final RefreshResponse refresh(Client client, String... indices) { protected void ensureEmptyWriteBuffers() throws Exception { assertBusy(() -> { - FollowStatsAction.StatsResponses statsResponses = - leaderClient().execute(FollowStatsAction.INSTANCE, new FollowStatsAction.StatsRequest()).actionGet(); + FollowStatsAction.StatsResponses statsResponses = leaderClient().execute( + FollowStatsAction.INSTANCE, + new FollowStatsAction.StatsRequest() + ).actionGet(); for (FollowStatsAction.StatsResponse statsResponse : statsResponses.getStatsResponses()) { ShardFollowNodeTaskStatus status = statsResponse.status(); assertThat(status.writeBufferOperationCount(), equalTo(0)); @@ -459,10 +512,13 @@ protected void pauseFollow(String... indices) throws Exception { protected void ensureNoCcrTasks() throws Exception { assertBusy(() -> { - CcrStatsAction.Response statsResponse = - followerClient().execute(CcrStatsAction.INSTANCE, new CcrStatsAction.Request()).actionGet(); - assertThat("Follow stats not empty: " + Strings.toString(statsResponse.getFollowStats()), - statsResponse.getFollowStats().getStatsResponses(), empty()); + CcrStatsAction.Response statsResponse = followerClient().execute(CcrStatsAction.INSTANCE, new CcrStatsAction.Request()) + .actionGet(); + assertThat( + "Follow stats not empty: " + Strings.toString(statsResponse.getFollowStats()), + statsResponse.getFollowStats().getStatsResponses(), + empty() + ); final ClusterState clusterState = followerClient().admin().cluster().prepareState().get().getState(); final PersistentTasksCustomMetadata tasks = clusterState.getMetadata().custom(PersistentTasksCustomMetadata.TYPE); @@ -481,7 +537,6 @@ protected void ensureNoCcrTasks() throws Exception { }, 30, TimeUnit.SECONDS); } - @Before public void setupSourceEnabledOrDisabled() { sourceEnabled = randomBoolean(); @@ -493,8 +548,11 @@ protected String getIndexSettings(final int numberOfShards, final int numberOfRe return getIndexSettings(numberOfShards, numberOfReplicas, Collections.emptyMap()); } - protected String getIndexSettings(final int numberOfShards, final int numberOfReplicas, - final Map additionalIndexSettings) throws IOException { + protected String getIndexSettings( + final int numberOfShards, + final int numberOfReplicas, + final Map additionalIndexSettings + ) throws IOException { final String settings; try (XContentBuilder builder = jsonBuilder()) { builder.startObject(); @@ -576,9 +634,13 @@ protected void assertIndexFullyReplicatedToFollower(String leaderIndex, String f Map> mismatchedDocs = new HashMap<>(); for (Map.Entry> fe : docsOnFollower.entrySet()) { Set d1 = Sets.difference( - Sets.newHashSet(fe.getValue()), Sets.newHashSet(docsOnLeader.getOrDefault(fe.getKey(), Collections.emptyList()))); + Sets.newHashSet(fe.getValue()), + Sets.newHashSet(docsOnLeader.getOrDefault(fe.getKey(), Collections.emptyList())) + ); Set d2 = Sets.difference( - Sets.newHashSet(docsOnLeader.getOrDefault(fe.getKey(), Collections.emptyList())), Sets.newHashSet(fe.getValue())); + Sets.newHashSet(docsOnLeader.getOrDefault(fe.getKey(), Collections.emptyList())), + Sets.newHashSet(fe.getValue()) + ); if (d1.isEmpty() == false || d2.isEmpty() == false) { mismatchedDocs.put(fe.getKey(), Sets.union(d1, d2)); } @@ -616,14 +678,18 @@ private Map> getDocIdAndSeqNos(InternalTestCl continue; } IndexShard indexShard = cluster.getInstance(IndicesService.class, state.nodes().get(shardRouting.currentNodeId()).getName()) - .indexServiceSafe(shardRouting.index()).getShard(shardRouting.id()); + .indexServiceSafe(shardRouting.index()) + .getShard(shardRouting.id()); try { final List docsOnShard = IndexShardTestCase.getDocIdAndSeqNos(indexShard); logger.info("--> shard {} docs {} seq_no_stats {}", shardRouting, docsOnShard, indexShard.seqNoStats()); - docs.put(shardRouting.shardId().id(), docsOnShard.stream() - // normalize primary term as the follower use its own term - .map(d -> new DocIdSeqNoAndSource(d.getId(), d.getSource(), d.getSeqNo(), 1L, d.getVersion())) - .collect(Collectors.toList())); + docs.put( + shardRouting.shardId().id(), + docsOnShard.stream() + // normalize primary term as the follower use its own term + .map(d -> new DocIdSeqNoAndSource(d.getId(), d.getSource(), d.getSeqNo(), 1L, d.getVersion())) + .collect(Collectors.toList()) + ); } catch (AlreadyClosedException e) { // Ignore this exception and try getting List from other IndexShard instance. } @@ -646,14 +712,26 @@ protected void atLeastDocsIndexed(Client client, String index, long numDocsRepli protected void awaitGlobalCheckpointAtLeast(Client client, ShardId shardId, long minimumGlobalCheckpoint) throws Exception { logger.info("waiting for the global checkpoint on [{}] at least [{}]", shardId, minimumGlobalCheckpoint); assertBusy(() -> { - ShardStats stats = client.admin().indices().prepareStats(shardId.getIndexName()).clear().get() - .asMap().entrySet().stream().filter(e -> e.getKey().shardId().equals(shardId)) - .map(Map.Entry::getValue).findFirst().orElse(null); + ShardStats stats = client.admin() + .indices() + .prepareStats(shardId.getIndexName()) + .clear() + .get() + .asMap() + .entrySet() + .stream() + .filter(e -> e.getKey().shardId().equals(shardId)) + .map(Map.Entry::getValue) + .findFirst() + .orElse(null); if (stats == null || stats.getSeqNoStats() == null) { throw new AssertionError("seq_no_stats for shard [" + shardId + "] is not found"); // causes assertBusy to retry } - assertThat(Strings.toString(stats.getSeqNoStats()), - stats.getSeqNoStats().getGlobalCheckpoint(), greaterThanOrEqualTo(minimumGlobalCheckpoint)); + assertThat( + Strings.toString(stats.getSeqNoStats()), + stats.getSeqNoStats().getGlobalCheckpoint(), + greaterThanOrEqualTo(minimumGlobalCheckpoint) + ); }, 60, TimeUnit.SECONDS); } @@ -706,48 +784,47 @@ public void waitForDocs(final long numDocs, final BackgroundIndexer indexer) thr // indexing threads can wait for up to ~1m before retrying when they first try to index into a shard which is not STARTED. final long maxWaitTimeMs = Math.max(90 * 1000, 200 * numDocs); - assertBusy( - () -> { - long lastKnownCount = indexer.totalIndexedDocs(); - - if (lastKnownCount >= numDocs) { - try { - long count = indexer.getClient().prepareSearch() - .setTrackTotalHits(true) - .setSize(0) - .setQuery(QueryBuilders.matchAllQuery()) - .get() - .getHits().getTotalHits().value; - - if (count == lastKnownCount) { - // no progress - try to refresh for the next time - indexer.getClient().admin().indices().prepareRefresh().get(); - } - lastKnownCount = count; - } catch (Exception e) { // count now acts like search and barfs if all shards failed... - logger.debug("failed to executed count", e); - throw e; + assertBusy(() -> { + long lastKnownCount = indexer.totalIndexedDocs(); + + if (lastKnownCount >= numDocs) { + try { + long count = indexer.getClient() + .prepareSearch() + .setTrackTotalHits(true) + .setSize(0) + .setQuery(QueryBuilders.matchAllQuery()) + .get() + .getHits() + .getTotalHits().value; + + if (count == lastKnownCount) { + // no progress - try to refresh for the next time + indexer.getClient().admin().indices().prepareRefresh().get(); } + lastKnownCount = count; + } catch (Exception e) { // count now acts like search and barfs if all shards failed... + logger.debug("failed to executed count", e); + throw e; } + } - if (logger.isDebugEnabled()) { - if (lastKnownCount < numDocs) { - logger.debug("[{}] docs indexed. waiting for [{}]", lastKnownCount, numDocs); - } else { - logger.debug("[{}] docs visible for search (needed [{}])", lastKnownCount, numDocs); - } + if (logger.isDebugEnabled()) { + if (lastKnownCount < numDocs) { + logger.debug("[{}] docs indexed. waiting for [{}]", lastKnownCount, numDocs); + } else { + logger.debug("[{}] docs visible for search (needed [{}])", lastKnownCount, numDocs); } + } - assertThat(lastKnownCount, greaterThanOrEqualTo(numDocs)); - }, - maxWaitTimeMs, - TimeUnit.MILLISECONDS - ); + assertThat(lastKnownCount, greaterThanOrEqualTo(numDocs)); + }, maxWaitTimeMs, TimeUnit.MILLISECONDS); } protected ActionListener waitForRestore( - final ClusterService clusterService, - final ActionListener listener) { + final ClusterService clusterService, + final ActionListener listener + ) { return new ActionListener() { @Override @@ -773,10 +850,12 @@ public void clusterChanged(ClusterChangedEvent changedEvent) { } else if (newEntry == null) { clusterService.removeListener(this); ImmutableOpenMap shards = prevEntry.shards(); - RestoreInfo ri = new RestoreInfo(prevEntry.snapshot().getSnapshotId().getName(), - prevEntry.indices(), - shards.size(), - shards.size() - RestoreService.failedShards(shards)); + RestoreInfo ri = new RestoreInfo( + prevEntry.snapshot().getSnapshotId().getName(), + prevEntry.indices(), + shards.size(), + shards.size() - RestoreService.failedShards(shards) + ); logger.debug("restore of [{}] completed", snapshot); listener.onResponse(ri); } else { @@ -805,13 +884,14 @@ static void removeCCRRelatedMetadataFromClusterState(ClusterService clusterServi clusterService.submitStateUpdateTask("remove-ccr-related-metadata", new ClusterStateUpdateTask() { @Override public ClusterState execute(ClusterState currentState) throws Exception { - AutoFollowMetadata empty = - new AutoFollowMetadata(Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap()); + AutoFollowMetadata empty = new AutoFollowMetadata(Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap()); ClusterState.Builder newState = ClusterState.builder(currentState); - newState.metadata(Metadata.builder(currentState.getMetadata()) - .putCustom(AutoFollowMetadata.TYPE, empty) - .removeCustom(PersistentTasksCustomMetadata.TYPE) - .build()); + newState.metadata( + Metadata.builder(currentState.getMetadata()) + .putCustom(AutoFollowMetadata.TYPE, empty) + .removeCustom(PersistentTasksCustomMetadata.TYPE) + .build() + ); return newState.build(); } diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/CcrSingleNodeTestCase.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/CcrSingleNodeTestCase.java index 92543b2dd38ca..117cc4fb8275b 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/CcrSingleNodeTestCase.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/CcrSingleNodeTestCase.java @@ -119,8 +119,10 @@ protected PutFollowAction.Request getPutFollowRequest(String leaderIndex, String protected void ensureEmptyWriteBuffers() throws Exception { assertBusy(() -> { - FollowStatsAction.StatsResponses statsResponses = - client().execute(FollowStatsAction.INSTANCE, new FollowStatsAction.StatsRequest()).actionGet(); + FollowStatsAction.StatsResponses statsResponses = client().execute( + FollowStatsAction.INSTANCE, + new FollowStatsAction.StatsRequest() + ).actionGet(); for (FollowStatsAction.StatsResponse statsResponse : statsResponses.getStatsResponses()) { ShardFollowNodeTaskStatus status = statsResponse.status(); assertThat(status.writeBufferOperationCount(), equalTo(0)); diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/AutoFollowMetadataTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/AutoFollowMetadataTests.java index bc4d8f0e242db..2b69cf2a2ce78 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/AutoFollowMetadataTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/AutoFollowMetadataTests.java @@ -12,8 +12,8 @@ import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ccr.AutoFollowMetadata; import java.io.IOException; @@ -60,7 +60,8 @@ protected AutoFollowMetadata createTestInstance() { randomIntBetween(0, Integer.MAX_VALUE), new ByteSizeValue(randomNonNegativeLong()), TimeValue.timeValueMillis(500), - TimeValue.timeValueMillis(500)); + TimeValue.timeValueMillis(500) + ); configs.put(Integer.toString(i), autoFollowPattern); followedLeaderIndices.put(Integer.toString(i), Arrays.asList(generateRandomStringArray(4, 4, false))); if (randomBoolean()) { diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/CCRFeatureSetUsageTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/CCRFeatureSetUsageTests.java index f69e312548cbc..0829d8ab3e136 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/CCRFeatureSetUsageTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/CCRFeatureSetUsageTests.java @@ -14,8 +14,13 @@ public class CCRFeatureSetUsageTests extends AbstractWireSerializingTestCase future = new PlainActionFuture<>(); usageAction.masterOperation(null, null, clusterState, future); CCRInfoTransportAction.Usage ccrUsage = (CCRInfoTransportAction.Usage) future.get().getUsage(); diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/CcrLicenseCheckerTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/CcrLicenseCheckerTests.java index 3338c2c888a15..ad3c884e086e1 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/CcrLicenseCheckerTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/CcrLicenseCheckerTests.java @@ -31,14 +31,11 @@ User getUser(final Client remoteClient) { }; final AtomicBoolean invoked = new AtomicBoolean(); - checker.hasPrivilegesToFollowIndices( - mock(Client.class), - new String[]{randomAlphaOfLength(8)}, - e -> { - invoked.set(true); - assertThat(e, instanceOf(IllegalStateException.class)); - assertThat(e, hasToString(containsString("missing or unable to read authentication info on request"))); - }); + checker.hasPrivilegesToFollowIndices(mock(Client.class), new String[] { randomAlphaOfLength(8) }, e -> { + invoked.set(true); + assertThat(e, instanceOf(IllegalStateException.class)); + assertThat(e, hasToString(containsString("missing or unable to read authentication info on request"))); + }); assertTrue(invoked.get()); } diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/CcrSettingsTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/CcrSettingsTests.java index 4ead06616d89d..6180ee19d4ab3 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/CcrSettingsTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/CcrSettingsTests.java @@ -16,18 +16,26 @@ public class CcrSettingsTests extends ESTestCase { public void testDefaultSettings() { final Settings settings = Settings.EMPTY; - final CcrSettings ccrSettings = new CcrSettings(settings, - new ClusterSettings(settings, Sets.newHashSet(CcrSettings.RECOVERY_CHUNK_SIZE, - CcrSettings.RECOVERY_MAX_BYTES_PER_SECOND, CcrSettings.INDICES_RECOVERY_MAX_CONCURRENT_FILE_CHUNKS_SETTING, - CcrSettings.INDICES_RECOVERY_ACTIVITY_TIMEOUT_SETTING, CcrSettings.INDICES_RECOVERY_ACTION_TIMEOUT_SETTING))); + final CcrSettings ccrSettings = new CcrSettings( + settings, + new ClusterSettings( + settings, + Sets.newHashSet( + CcrSettings.RECOVERY_CHUNK_SIZE, + CcrSettings.RECOVERY_MAX_BYTES_PER_SECOND, + CcrSettings.INDICES_RECOVERY_MAX_CONCURRENT_FILE_CHUNKS_SETTING, + CcrSettings.INDICES_RECOVERY_ACTIVITY_TIMEOUT_SETTING, + CcrSettings.INDICES_RECOVERY_ACTION_TIMEOUT_SETTING + ) + ) + ); assertEquals(CcrSettings.RECOVERY_CHUNK_SIZE.get(settings), ccrSettings.getChunkSize()); - assertEquals(CcrSettings.INDICES_RECOVERY_MAX_CONCURRENT_FILE_CHUNKS_SETTING.get(settings).intValue(), - ccrSettings.getMaxConcurrentFileChunks()); - assertEquals(CcrSettings.INDICES_RECOVERY_ACTIVITY_TIMEOUT_SETTING.get(settings), - ccrSettings.getRecoveryActivityTimeout()); - assertEquals(CcrSettings.INDICES_RECOVERY_ACTION_TIMEOUT_SETTING.get(settings), - ccrSettings.getRecoveryActionTimeout()); - assertEquals(CcrSettings.RECOVERY_MAX_BYTES_PER_SECOND.get(settings).getMbFrac(), - ccrSettings.getRateLimiter().getMBPerSec(), 0.0d); + assertEquals( + CcrSettings.INDICES_RECOVERY_MAX_CONCURRENT_FILE_CHUNKS_SETTING.get(settings).intValue(), + ccrSettings.getMaxConcurrentFileChunks() + ); + assertEquals(CcrSettings.INDICES_RECOVERY_ACTIVITY_TIMEOUT_SETTING.get(settings), ccrSettings.getRecoveryActivityTimeout()); + assertEquals(CcrSettings.INDICES_RECOVERY_ACTION_TIMEOUT_SETTING.get(settings), ccrSettings.getRecoveryActionTimeout()); + assertEquals(CcrSettings.RECOVERY_MAX_BYTES_PER_SECOND.get(settings).getMbFrac(), ccrSettings.getRateLimiter().getMBPerSec(), 0.0d); } } diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/CcrTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/CcrTests.java index d912d7a91a005..829809410a5f6 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/CcrTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/CcrTests.java @@ -15,7 +15,6 @@ import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.engine.EngineFactory; import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.xpack.ccr.Ccr; import org.elasticsearch.xpack.ccr.index.engine.FollowingEngineFactory; import java.io.IOException; @@ -31,17 +30,16 @@ public void testGetEngineFactory() throws IOException { final String indexName = "following-" + value; final Index index = new Index(indexName, UUIDs.randomBase64UUID()); final Settings.Builder builder = Settings.builder() - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(IndexMetadata.SETTING_INDEX_UUID, index.getUUID()); + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetadata.SETTING_INDEX_UUID, index.getUUID()); if (value != null) { builder.put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), value); } - final IndexMetadata indexMetadata = new IndexMetadata.Builder(index.getName()) - .settings(builder.build()) - .numberOfShards(1) - .numberOfReplicas(0) - .build(); + final IndexMetadata indexMetadata = new IndexMetadata.Builder(index.getName()).settings(builder.build()) + .numberOfShards(1) + .numberOfReplicas(0) + .build(); final Ccr ccr = new Ccr(Settings.EMPTY, new CcrLicenseChecker(() -> true, () -> false)); final Optional engineFactory = ccr.getEngineFactory(new IndexSettings(indexMetadata, Settings.EMPTY)); if (value != null && value) { diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/LocalStateCcr.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/LocalStateCcr.java index 9a2a688a0cd9a..16445868cabca 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/LocalStateCcr.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/LocalStateCcr.java @@ -29,4 +29,3 @@ protected XPackLicenseState getLicenseState() { } } - diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinatorTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinatorTests.java index e866a5b948817..baf6b5906af74 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinatorTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowCoordinatorTests.java @@ -126,7 +126,7 @@ public void testAutoFollower() { .metadata(Metadata.builder().putCustom(AutoFollowMetadata.TYPE, autoFollowMetadata)) .build(); - boolean[] invoked = new boolean[]{false}; + boolean[] invoked = new boolean[] { false }; Consumer> handler = results -> { invoked[0] = true; @@ -139,18 +139,18 @@ public void testAutoFollower() { }; AutoFollower autoFollower = new AutoFollower("remote", handler, localClusterStateSupplier(currentState), () -> 1L, Runnable::run) { @Override - void getRemoteClusterState(String remoteCluster, - long metadataVersion, - BiConsumer handler) { + void getRemoteClusterState(String remoteCluster, long metadataVersion, BiConsumer handler) { assertThat(remoteCluster, equalTo("remote")); handler.accept(new ClusterStateResponse(new ClusterName("name"), remoteState, false), null); } @Override - void createAndFollow(Map headers, - PutFollowAction.Request followRequest, - Runnable successHandler, - Consumer failureHandler) { + void createAndFollow( + Map headers, + PutFollowAction.Request followRequest, + Runnable successHandler, + Consumer failureHandler + ) { assertThat(headers, equalTo(autoFollowHeaders.get("remote"))); assertThat(followRequest.getRemoteCluster(), equalTo("remote")); assertThat(followRequest.getLeaderIndex(), equalTo("logs-20190101")); @@ -160,8 +160,7 @@ void createAndFollow(Map headers, } @Override - void updateAutoFollowMetadata(Function updateFunction, - Consumer handler) { + void updateAutoFollowMetadata(Function updateFunction, Consumer handler) { ClusterState resultCs = updateFunction.apply(currentState); AutoFollowMetadata result = resultCs.metadata().custom(AutoFollowMetadata.TYPE); assertThat(result.getFollowedLeaderIndexUUIDs().size(), equalTo(1)); @@ -214,7 +213,7 @@ public void testAutoFollower_dataStream() { .metadata(Metadata.builder().putCustom(AutoFollowMetadata.TYPE, autoFollowMetadata)) .build(); - boolean[] invoked = new boolean[]{false}; + boolean[] invoked = new boolean[] { false }; Consumer> handler = results -> { invoked[0] = true; @@ -227,18 +226,18 @@ public void testAutoFollower_dataStream() { }; AutoFollower autoFollower = new AutoFollower("remote", handler, localClusterStateSupplier(currentState), () -> 1L, Runnable::run) { @Override - void getRemoteClusterState(String remoteCluster, - long metadataVersion, - BiConsumer handler) { + void getRemoteClusterState(String remoteCluster, long metadataVersion, BiConsumer handler) { assertThat(remoteCluster, equalTo("remote")); handler.accept(new ClusterStateResponse(new ClusterName("name"), remoteState, false), null); } @Override - void createAndFollow(Map headers, - PutFollowAction.Request followRequest, - Runnable successHandler, - Consumer failureHandler) { + void createAndFollow( + Map headers, + PutFollowAction.Request followRequest, + Runnable successHandler, + Consumer failureHandler + ) { assertThat(headers, equalTo(autoFollowHeaders.get("remote"))); assertThat(followRequest.getRemoteCluster(), equalTo("remote")); assertThat(followRequest.getLeaderIndex(), matchesPattern(DataStreamTestHelper.backingIndexPattern("logs-foobar", 1))); @@ -248,8 +247,7 @@ void createAndFollow(Map headers, } @Override - void updateAutoFollowMetadata(Function updateFunction, - Consumer handler) { + void updateAutoFollowMetadata(Function updateFunction, Consumer handler) { ClusterState resultCs = updateFunction.apply(currentState); AutoFollowMetadata result = resultCs.metadata().custom(AutoFollowMetadata.TYPE); assertThat(result.getFollowedLeaderIndexUUIDs().size(), equalTo(1)); @@ -299,7 +297,7 @@ public void testAutoFollowerClusterStateApiFailure() { .build(); Exception failure = new RuntimeException("failure"); - boolean[] invoked = new boolean[]{false}; + boolean[] invoked = new boolean[] { false }; Consumer> handler = results -> { invoked[0] = true; @@ -309,23 +307,22 @@ public void testAutoFollowerClusterStateApiFailure() { }; AutoFollower autoFollower = new AutoFollower("remote", handler, localClusterStateSupplier(clusterState), () -> 1L, Runnable::run) { @Override - void getRemoteClusterState(String remoteCluster, - long metadataVersion, - BiConsumer handler) { + void getRemoteClusterState(String remoteCluster, long metadataVersion, BiConsumer handler) { handler.accept(null, failure); } @Override - void createAndFollow(Map headers, - PutFollowAction.Request followRequest, - Runnable successHandler, - Consumer failureHandler) { + void createAndFollow( + Map headers, + PutFollowAction.Request followRequest, + Runnable successHandler, + Consumer failureHandler + ) { fail("should not get here"); } @Override - void updateAutoFollowMetadata(Function updateFunction, - Consumer handler) { + void updateAutoFollowMetadata(Function updateFunction, Consumer handler) { fail("should not get here"); } }; @@ -367,7 +364,7 @@ public void testAutoFollowerUpdateClusterStateFailure() { .build(); Exception failure = new RuntimeException("failure"); - boolean[] invoked = new boolean[]{false}; + boolean[] invoked = new boolean[] { false }; Consumer> handler = results -> { invoked[0] = true; @@ -380,17 +377,17 @@ public void testAutoFollowerUpdateClusterStateFailure() { }; AutoFollower autoFollower = new AutoFollower("remote", handler, localClusterStateSupplier(clusterState), () -> 1L, Runnable::run) { @Override - void getRemoteClusterState(String remoteCluster, - long metadataVersion, - BiConsumer handler) { + void getRemoteClusterState(String remoteCluster, long metadataVersion, BiConsumer handler) { handler.accept(new ClusterStateResponse(new ClusterName("name"), remoteState, false), null); } @Override - void createAndFollow(Map headers, - PutFollowAction.Request followRequest, - Runnable successHandler, - Consumer failureHandler) { + void createAndFollow( + Map headers, + PutFollowAction.Request followRequest, + Runnable successHandler, + Consumer failureHandler + ) { assertThat(followRequest.getRemoteCluster(), equalTo("remote")); assertThat(followRequest.getLeaderIndex(), equalTo("logs-20190101")); assertThat(followRequest.getFollowerIndex(), equalTo("logs-20190101")); @@ -411,7 +408,9 @@ public void testAutoFollowerWithNoActivePatternsDoesNotStart() { final String remoteCluster = randomAlphaOfLength(5); final Map autoFollowPatterns = new HashMap<>(2); - autoFollowPatterns.put("pattern_1", new AutoFollowPattern( + autoFollowPatterns.put( + "pattern_1", + new AutoFollowPattern( remoteCluster, List.of("logs-*", "test-*"), Collections.emptyList(), @@ -430,7 +429,9 @@ public void testAutoFollowerWithNoActivePatternsDoesNotStart() { null ) ); - autoFollowPatterns.put("pattern_2", new AutoFollowPattern( + autoFollowPatterns.put( + "pattern_2", + new AutoFollowPattern( remoteCluster, List.of("users-*"), Collections.emptyList(), @@ -458,32 +459,45 @@ public void testAutoFollowerWithNoActivePatternsDoesNotStart() { headers.put("pattern_1", singletonMap("header", "value")); headers.put("pattern_2", emptyMap()); - final Supplier followerClusterStateSupplier = localClusterStateSupplier(ClusterState.builder(new ClusterName("test")) - .metadata(Metadata.builder() - .putCustom(AutoFollowMetadata.TYPE, new AutoFollowMetadata(autoFollowPatterns, followedLeaderIndexUUIDs, headers)) - .build()) - .build()); + final Supplier followerClusterStateSupplier = localClusterStateSupplier( + ClusterState.builder(new ClusterName("test")) + .metadata( + Metadata.builder() + .putCustom(AutoFollowMetadata.TYPE, new AutoFollowMetadata(autoFollowPatterns, followedLeaderIndexUUIDs, headers)) + .build() + ) + .build() + ); final AtomicBoolean invoked = new AtomicBoolean(false); - final AutoFollower autoFollower = - new AutoFollower(remoteCluster, v -> invoked.set(true), followerClusterStateSupplier, () -> 1L, Runnable::run) { - @Override - void getRemoteClusterState(String remote, long metadataVersion, BiConsumer handler) { - invoked.set(true); - } + final AutoFollower autoFollower = new AutoFollower( + remoteCluster, + v -> invoked.set(true), + followerClusterStateSupplier, + () -> 1L, + Runnable::run + ) { + @Override + void getRemoteClusterState(String remote, long metadataVersion, BiConsumer handler) { + invoked.set(true); + } - @Override - void createAndFollow(Map headers, PutFollowAction.Request request, - Runnable successHandler, Consumer failureHandler) { - invoked.set(true); - successHandler.run(); - } + @Override + void createAndFollow( + Map headers, + PutFollowAction.Request request, + Runnable successHandler, + Consumer failureHandler + ) { + invoked.set(true); + successHandler.run(); + } - @Override - void updateAutoFollowMetadata(Function updateFunction, Consumer handler) { - invoked.set(true); - } - }; + @Override + void updateAutoFollowMetadata(Function updateFunction, Consumer handler) { + invoked.set(true); + } + }; autoFollower.start(); assertThat(invoked.get(), is(false)); @@ -498,8 +512,7 @@ public void testAutoFollowerWithPausedActivePatterns() { final AtomicReference localClusterState = new AtomicReference<>( ClusterState.builder(new ClusterName("local")) - .metadata(Metadata.builder() - .putCustom(AutoFollowMetadata.TYPE, new AutoFollowMetadata(emptyMap(), emptyMap(), emptyMap()))) + .metadata(Metadata.builder().putCustom(AutoFollowMetadata.TYPE, new AutoFollowMetadata(emptyMap(), emptyMap(), emptyMap()))) .build() ); @@ -515,8 +528,12 @@ public void testAutoFollowerWithPausedActivePatterns() { request.setRemoteCluster(remoteCluster); request.setLeaderIndexPatterns(singletonList("patternLogs-*")); request.setFollowIndexNamePattern("copy-{{leader_index}}"); - nextLocalClusterState = - TransportPutAutoFollowPatternAction.innerPut(request, emptyMap(), currentLocalState, remoteClusterState.get()); + nextLocalClusterState = TransportPutAutoFollowPatternAction.innerPut( + request, + emptyMap(), + currentLocalState, + remoteClusterState.get() + ); } else if (nextClusterStateVersion == 2) { // cluster state #2 : still one pattern is active @@ -529,8 +546,12 @@ public void testAutoFollowerWithPausedActivePatterns() { request.setRemoteCluster(remoteCluster); request.setLeaderIndexPatterns(singletonList("patternDocs-*")); request.setFollowIndexNamePattern("copy-{{leader_index}}"); - nextLocalClusterState = - TransportPutAutoFollowPatternAction.innerPut(request, emptyMap(), currentLocalState, remoteClusterState.get()); + nextLocalClusterState = TransportPutAutoFollowPatternAction.innerPut( + request, + emptyMap(), + currentLocalState, + remoteClusterState.get() + ); } else if (nextClusterStateVersion == 4) { // cluster state #4 : still both patterns are active @@ -550,88 +571,112 @@ public void testAutoFollowerWithPausedActivePatterns() { return currentLocalState; } - return ClusterState.builder(nextLocalClusterState) - .version(nextClusterStateVersion) - .build(); + return ClusterState.builder(nextLocalClusterState).version(nextClusterStateVersion).build(); }); final Set followedIndices = ConcurrentCollections.newConcurrentSet(); final List autoFollowResults = new ArrayList<>(); - final AutoFollower autoFollower = - new AutoFollower(remoteCluster, autoFollowResults::addAll, localClusterStateSupplier, () -> 1L, Runnable::run) { - - int countFetches = 1; // to be aligned with local cluster state updates - ClusterState lastFetchedRemoteClusterState; + final AutoFollower autoFollower = new AutoFollower( + remoteCluster, + autoFollowResults::addAll, + localClusterStateSupplier, + () -> 1L, + Runnable::run + ) { - @Override - void getRemoteClusterState(String remote, long metadataVersion, BiConsumer handler) { - assertThat(remote, equalTo(remoteCluster)); - - // in this test, every time it fetches the remote cluster state new leader indices to follow appears - final String[] newLeaderIndices = {"patternLogs-" + countFetches, "patternDocs-" + countFetches}; - - if (countFetches == 1) { - assertThat("first invocation, it should retrieve the metadata version 1", metadataVersion, equalTo(1L)); - lastFetchedRemoteClusterState = createRemoteClusterState(remoteClusterState.get(), newLeaderIndices); - - } else if (countFetches == 2 || countFetches == 4) { - assertThat("no patterns changes, it should retrieve the last known metadata version + 1", - metadataVersion, equalTo(lastFetchedRemoteClusterState.metadata().version() + 1)); - lastFetchedRemoteClusterState = createRemoteClusterState(remoteClusterState.get(), newLeaderIndices); - assertThat("remote cluster state metadata version is aligned with what the auto-follower is requesting", - lastFetchedRemoteClusterState.getMetadata().version(), equalTo(metadataVersion)); - - } else if (countFetches == 3 || countFetches == 5) { - assertThat("patterns have changed, it should retrieve the last known metadata version again", - metadataVersion, equalTo(lastFetchedRemoteClusterState.metadata().version())); - lastFetchedRemoteClusterState = createRemoteClusterState(remoteClusterState.get(), newLeaderIndices); - assertThat("remote cluster state metadata version is incremented", - lastFetchedRemoteClusterState.getMetadata().version(), equalTo(metadataVersion + 1)); - } else { - fail("after the 5th invocation there are no more active patterns, the auto-follower should have stopped"); - } + int countFetches = 1; // to be aligned with local cluster state updates + ClusterState lastFetchedRemoteClusterState; - countFetches = countFetches + 1; - remoteClusterState.set(lastFetchedRemoteClusterState); - handler.accept(new ClusterStateResponse(lastFetchedRemoteClusterState.getClusterName(), - lastFetchedRemoteClusterState, false), null); + @Override + void getRemoteClusterState(String remote, long metadataVersion, BiConsumer handler) { + assertThat(remote, equalTo(remoteCluster)); + + // in this test, every time it fetches the remote cluster state new leader indices to follow appears + final String[] newLeaderIndices = { "patternLogs-" + countFetches, "patternDocs-" + countFetches }; + + if (countFetches == 1) { + assertThat("first invocation, it should retrieve the metadata version 1", metadataVersion, equalTo(1L)); + lastFetchedRemoteClusterState = createRemoteClusterState(remoteClusterState.get(), newLeaderIndices); + + } else if (countFetches == 2 || countFetches == 4) { + assertThat( + "no patterns changes, it should retrieve the last known metadata version + 1", + metadataVersion, + equalTo(lastFetchedRemoteClusterState.metadata().version() + 1) + ); + lastFetchedRemoteClusterState = createRemoteClusterState(remoteClusterState.get(), newLeaderIndices); + assertThat( + "remote cluster state metadata version is aligned with what the auto-follower is requesting", + lastFetchedRemoteClusterState.getMetadata().version(), + equalTo(metadataVersion) + ); + + } else if (countFetches == 3 || countFetches == 5) { + assertThat( + "patterns have changed, it should retrieve the last known metadata version again", + metadataVersion, + equalTo(lastFetchedRemoteClusterState.metadata().version()) + ); + lastFetchedRemoteClusterState = createRemoteClusterState(remoteClusterState.get(), newLeaderIndices); + assertThat( + "remote cluster state metadata version is incremented", + lastFetchedRemoteClusterState.getMetadata().version(), + equalTo(metadataVersion + 1) + ); + } else { + fail("after the 5th invocation there are no more active patterns, the auto-follower should have stopped"); } - @Override - void createAndFollow(Map headers, PutFollowAction.Request request, - Runnable successHandler, Consumer failureHandler) { - assertThat(request.getRemoteCluster(), equalTo(remoteCluster)); - assertThat(request.masterNodeTimeout(), equalTo(TimeValue.MAX_VALUE)); - assertThat(request.getFollowerIndex(), startsWith("copy-")); - followedIndices.add(request.getLeaderIndex()); - successHandler.run(); - } + countFetches = countFetches + 1; + remoteClusterState.set(lastFetchedRemoteClusterState); + handler.accept( + new ClusterStateResponse(lastFetchedRemoteClusterState.getClusterName(), lastFetchedRemoteClusterState, false), + null + ); + } - @Override - void updateAutoFollowMetadata(Function updateFunction, Consumer handler) { - localClusterState.updateAndGet(updateFunction::apply); - handler.accept(null); - } + @Override + void createAndFollow( + Map headers, + PutFollowAction.Request request, + Runnable successHandler, + Consumer failureHandler + ) { + assertThat(request.getRemoteCluster(), equalTo(remoteCluster)); + assertThat(request.masterNodeTimeout(), equalTo(TimeValue.MAX_VALUE)); + assertThat(request.getFollowerIndex(), startsWith("copy-")); + followedIndices.add(request.getLeaderIndex()); + successHandler.run(); + } - @Override - void cleanFollowedRemoteIndices(ClusterState remoteClusterState, List patterns) { - // Ignore, to avoid invoking updateAutoFollowMetadata(...) twice - } - }; + @Override + void updateAutoFollowMetadata(Function updateFunction, Consumer handler) { + localClusterState.updateAndGet(updateFunction::apply); + handler.accept(null); + } + + @Override + void cleanFollowedRemoteIndices(ClusterState remoteClusterState, List patterns) { + // Ignore, to avoid invoking updateAutoFollowMetadata(...) twice + } + }; autoFollower.start(); assertThat(autoFollowResults.size(), equalTo(7)); - assertThat(followedIndices, containsInAnyOrder( - "patternLogs-1", // iteration #1 : only pattern "patternLogs" is active in local cluster state - "patternLogs-2", // iteration #2 : only pattern "patternLogs" is active in local cluster state - "patternLogs-3", // iteration #3 : both patterns "patternLogs" and "patternDocs" are active in local cluster state - "patternDocs-3", // - "patternLogs-4", // iteration #4 : both patterns "patternLogs" and "patternDocs" are active in local cluster state - "patternDocs-4", // - "patternDocs-5" // iteration #5 : only pattern "patternDocs" is active in local cluster state, "patternLogs" is paused - )); + assertThat( + followedIndices, + containsInAnyOrder( + "patternLogs-1", // iteration #1 : only pattern "patternLogs" is active in local cluster state + "patternLogs-2", // iteration #2 : only pattern "patternLogs" is active in local cluster state + "patternLogs-3", // iteration #3 : both patterns "patternLogs" and "patternDocs" are active in local cluster state + "patternDocs-3", // + "patternLogs-4", // iteration #4 : both patterns "patternLogs" and "patternDocs" are active in local cluster state + "patternDocs-4", // + "patternDocs-5" // iteration #5 : only pattern "patternDocs" is active in local cluster state, "patternLogs" is paused + ) + ); final ClusterState finalRemoteClusterState = remoteClusterState.get(); final ClusterState finalLocalClusterState = localClusterState.get(); @@ -640,7 +685,8 @@ void cleanFollowedRemoteIndices(ClusterState remoteClusterState, List pa assertThat(autoFollowMetadata.getPatterns().size(), equalTo(2)); assertThat(autoFollowMetadata.getPatterns().values().stream().noneMatch(AutoFollowPattern::isActive), is(true)); - assertThat(autoFollowMetadata.getFollowedLeaderIndexUUIDs().get("patternLogs"), + assertThat( + autoFollowMetadata.getFollowedLeaderIndexUUIDs().get("patternLogs"), containsInAnyOrder( finalRemoteClusterState.metadata().index("patternLogs-0").getIndexUUID(), finalRemoteClusterState.metadata().index("patternLogs-1").getIndexUUID(), @@ -648,9 +694,11 @@ void cleanFollowedRemoteIndices(ClusterState remoteClusterState, List pa finalRemoteClusterState.metadata().index("patternLogs-3").getIndexUUID(), finalRemoteClusterState.metadata().index("patternLogs-4").getIndexUUID() // patternLogs-5 exists in remote cluster state but patternLogs was paused - )); + ) + ); - assertThat(autoFollowMetadata.getFollowedLeaderIndexUUIDs().get("patternDocs"), + assertThat( + autoFollowMetadata.getFollowedLeaderIndexUUIDs().get("patternDocs"), containsInAnyOrder( // patternDocs-0 does not exist in remote cluster state finalRemoteClusterState.metadata().index("patternDocs-1").getIndexUUID(), @@ -658,7 +706,8 @@ void cleanFollowedRemoteIndices(ClusterState remoteClusterState, List pa finalRemoteClusterState.metadata().index("patternDocs-3").getIndexUUID(), finalRemoteClusterState.metadata().index("patternDocs-4").getIndexUUID(), finalRemoteClusterState.metadata().index("patternDocs-5").getIndexUUID() - )); + ) + ); } public void testAutoFollowerCreateAndFollowApiCallFailure() { @@ -671,7 +720,8 @@ public void testAutoFollowerCreateAndFollowApiCallFailure() { Collections.singletonList("logs-*"), Collections.emptyList(), null, - Settings.EMPTY, true, + Settings.EMPTY, + true, null, null, null, @@ -694,7 +744,7 @@ public void testAutoFollowerCreateAndFollowApiCallFailure() { .build(); Exception failure = new RuntimeException("failure"); - boolean[] invoked = new boolean[]{false}; + boolean[] invoked = new boolean[] { false }; Consumer> handler = results -> { invoked[0] = true; @@ -707,17 +757,17 @@ public void testAutoFollowerCreateAndFollowApiCallFailure() { }; AutoFollower autoFollower = new AutoFollower("remote", handler, localClusterStateSupplier(clusterState), () -> 1L, Runnable::run) { @Override - void getRemoteClusterState(String remoteCluster, - long metadataVersion, - BiConsumer handler) { + void getRemoteClusterState(String remoteCluster, long metadataVersion, BiConsumer handler) { handler.accept(new ClusterStateResponse(new ClusterName("name"), remoteState, false), null); } @Override - void createAndFollow(Map headers, - PutFollowAction.Request followRequest, - Runnable successHandler, - Consumer failureHandler) { + void createAndFollow( + Map headers, + PutFollowAction.Request followRequest, + Runnable successHandler, + Consumer failureHandler + ) { assertThat(followRequest.getRemoteCluster(), equalTo("remote")); assertThat(followRequest.masterNodeTimeout(), equalTo(TimeValue.MAX_VALUE)); assertThat(followRequest.getLeaderIndex(), equalTo("logs-20190101")); @@ -726,8 +776,7 @@ void createAndFollow(Map headers, } @Override - void updateAutoFollowMetadata(Function updateFunction, - Consumer handler) { + void updateAutoFollowMetadata(Function updateFunction, Consumer handler) { fail("should not get here"); } @@ -767,25 +816,19 @@ public void testGetLeaderIndicesToFollow() { Settings.Builder builder = Settings.builder() .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) .put(IndexMetadata.SETTING_INDEX_UUID, indexName); - imdBuilder.put(IndexMetadata.builder("metrics-" + i) - .settings(builder) - .numberOfShards(1) - .numberOfReplicas(0)); + imdBuilder.put(IndexMetadata.builder("metrics-" + i).settings(builder).numberOfShards(1).numberOfReplicas(0)); - ShardRouting shardRouting = - TestShardRouting.newShardRouting(indexName, 0, "1", true, ShardRoutingState.INITIALIZING).moveToStarted(); + ShardRouting shardRouting = TestShardRouting.newShardRouting(indexName, 0, "1", true, ShardRoutingState.INITIALIZING) + .moveToStarted(); IndexRoutingTable indexRoutingTable = IndexRoutingTable.builder(imdBuilder.get(indexName).getIndex()) .addShard(shardRouting) .build(); routingTableBuilder.add(indexRoutingTable); } - imdBuilder.put(IndexMetadata.builder("logs-0") - .settings(settings(Version.CURRENT)) - .numberOfShards(1) - .numberOfReplicas(0)); - ShardRouting shardRouting = - TestShardRouting.newShardRouting("logs-0", 0, "1", true, ShardRoutingState.INITIALIZING).moveToStarted(); + imdBuilder.put(IndexMetadata.builder("logs-0").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(0)); + ShardRouting shardRouting = TestShardRouting.newShardRouting("logs-0", 0, "1", true, ShardRoutingState.INITIALIZING) + .moveToStarted(); IndexRoutingTable indexRoutingTable = IndexRoutingTable.builder(imdBuilder.get("logs-0").getIndex()).addShard(shardRouting).build(); routingTableBuilder.add(indexRoutingTable); @@ -860,15 +903,10 @@ public void testGetLeaderIndicesToFollow_shardsNotStarted() { // 1 shard started and another not started: ClusterState remoteState = createRemoteClusterState("index1", true); - Metadata.Builder mBuilder= Metadata.builder(remoteState.metadata()); - mBuilder.put(IndexMetadata.builder("index2") - .settings(settings(Version.CURRENT)) - .numberOfShards(1) - .numberOfReplicas(0)); - ShardRouting shardRouting = - TestShardRouting.newShardRouting("index2", 0, "1", true, ShardRoutingState.INITIALIZING); - IndexRoutingTable indexRoutingTable = IndexRoutingTable.builder(mBuilder.get("index2").getIndex() - ).addShard(shardRouting).build(); + Metadata.Builder mBuilder = Metadata.builder(remoteState.metadata()); + mBuilder.put(IndexMetadata.builder("index2").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(0)); + ShardRouting shardRouting = TestShardRouting.newShardRouting("index2", 0, "1", true, ShardRoutingState.INITIALIZING); + IndexRoutingTable indexRoutingTable = IndexRoutingTable.builder(mBuilder.get("index2").getIndex()).addShard(shardRouting).build(); remoteState = ClusterState.builder(remoteState.getClusterName()) .metadata(mBuilder) .routingTable(RoutingTable.builder(remoteState.routingTable()).add(indexRoutingTable).build()) @@ -881,7 +919,8 @@ public void testGetLeaderIndicesToFollow_shardsNotStarted() { // Start second shard: shardRouting = shardRouting.moveToStarted(); indexRoutingTable = IndexRoutingTable.builder(remoteState.metadata().indices().get("index2").getIndex()) - .addShard(shardRouting).build(); + .addShard(shardRouting) + .build(); remoteState = ClusterState.builder(remoteState.getClusterName()) .metadata(remoteState.metadata()) .routingTable(RoutingTable.builder(remoteState.routingTable()).add(indexRoutingTable).build()) @@ -922,20 +961,25 @@ public void testGetLeaderIndicesToFollowWithClosedIndices() { // index is closed remoteState = ClusterState.builder(remoteState) - .metadata(Metadata.builder(remoteState.metadata()) - .put(IndexMetadata.builder(remoteState.metadata().index("test-index")).state(IndexMetadata.State.CLOSE).build(), true) - .build()) + .metadata( + Metadata.builder(remoteState.metadata()) + .put(IndexMetadata.builder(remoteState.metadata().index("test-index")).state(IndexMetadata.State.CLOSE).build(), true) + .build() + ) .build(); result = AutoFollower.getLeaderIndicesToFollow(autoFollowPattern, remoteState, Collections.emptyList()); assertThat(result.size(), equalTo(0)); } public void testRecordLeaderIndexAsFollowFunction() { - AutoFollowMetadata autoFollowMetadata = new AutoFollowMetadata(Collections.emptyMap(), - Map.of("pattern1", Collections.emptyList()), Collections.emptyMap()); - ClusterState clusterState = new ClusterState.Builder(new ClusterName("name")) - .metadata(new Metadata.Builder().putCustom(AutoFollowMetadata.TYPE, autoFollowMetadata)) - .build(); + AutoFollowMetadata autoFollowMetadata = new AutoFollowMetadata( + Collections.emptyMap(), + Map.of("pattern1", Collections.emptyList()), + Collections.emptyMap() + ); + ClusterState clusterState = new ClusterState.Builder(new ClusterName("name")).metadata( + new Metadata.Builder().putCustom(AutoFollowMetadata.TYPE, autoFollowMetadata) + ).build(); Function function = recordLeaderIndexAsFollowFunction("pattern1", new Index("index1", "index1")); ClusterState result = function.apply(clusterState); @@ -946,11 +990,14 @@ public void testRecordLeaderIndexAsFollowFunction() { } public void testRecordLeaderIndexAsFollowFunctionNoEntry() { - AutoFollowMetadata autoFollowMetadata = new AutoFollowMetadata(Collections.emptyMap(), Collections.emptyMap(), - Collections.emptyMap()); - ClusterState clusterState = new ClusterState.Builder(new ClusterName("name")) - .metadata(new Metadata.Builder().putCustom(AutoFollowMetadata.TYPE, autoFollowMetadata)) - .build(); + AutoFollowMetadata autoFollowMetadata = new AutoFollowMetadata( + Collections.emptyMap(), + Collections.emptyMap(), + Collections.emptyMap() + ); + ClusterState clusterState = new ClusterState.Builder(new ClusterName("name")).metadata( + new Metadata.Builder().putCustom(AutoFollowMetadata.TYPE, autoFollowMetadata) + ).build(); Function function = recordLeaderIndexAsFollowFunction("pattern1", new Index("index1", "index1")); ClusterState result = function.apply(clusterState); @@ -958,23 +1005,27 @@ public void testRecordLeaderIndexAsFollowFunctionNoEntry() { } public void testCleanFollowedLeaderIndices() { - AutoFollowMetadata autoFollowMetadata = new AutoFollowMetadata(Collections.emptyMap(), - Map.of("pattern1", Arrays.asList("index1", "index2", "index3")), Collections.emptyMap()); - ClusterState clusterState = new ClusterState.Builder(new ClusterName("name")) - .metadata(new Metadata.Builder().putCustom(AutoFollowMetadata.TYPE, autoFollowMetadata)) - .build(); + AutoFollowMetadata autoFollowMetadata = new AutoFollowMetadata( + Collections.emptyMap(), + Map.of("pattern1", Arrays.asList("index1", "index2", "index3")), + Collections.emptyMap() + ); + ClusterState clusterState = new ClusterState.Builder(new ClusterName("name")).metadata( + new Metadata.Builder().putCustom(AutoFollowMetadata.TYPE, autoFollowMetadata) + ).build(); - Metadata remoteMetadata = new Metadata.Builder() - .put(IndexMetadata.builder("index1") - .settings(settings(Version.CURRENT) - .put(IndexMetadata.SETTING_INDEX_UUID, "index1")) - .numberOfShards(1) - .numberOfReplicas(0)) - .put(IndexMetadata.builder("index3") - .settings(settings(Version.CURRENT) - .put(IndexMetadata.SETTING_INDEX_UUID, "index3")) + Metadata remoteMetadata = new Metadata.Builder().put( + IndexMetadata.builder("index1") + .settings(settings(Version.CURRENT).put(IndexMetadata.SETTING_INDEX_UUID, "index1")) .numberOfShards(1) - .numberOfReplicas(0)) + .numberOfReplicas(0) + ) + .put( + IndexMetadata.builder("index3") + .settings(settings(Version.CURRENT).put(IndexMetadata.SETTING_INDEX_UUID, "index3")) + .numberOfShards(1) + .numberOfReplicas(0) + ) .build(); Function function = cleanFollowedRemoteIndices(remoteMetadata, Collections.singletonList("pattern1")); @@ -985,28 +1036,33 @@ public void testCleanFollowedLeaderIndices() { } public void testCleanFollowedLeaderIndicesNoChanges() { - AutoFollowMetadata autoFollowMetadata = new AutoFollowMetadata(Collections.emptyMap(), - Map.of("pattern1", Arrays.asList("index1", "index2", "index3")), Collections.emptyMap()); - ClusterState clusterState = new ClusterState.Builder(new ClusterName("name")) - .metadata(new Metadata.Builder().putCustom(AutoFollowMetadata.TYPE, autoFollowMetadata)) - .build(); + AutoFollowMetadata autoFollowMetadata = new AutoFollowMetadata( + Collections.emptyMap(), + Map.of("pattern1", Arrays.asList("index1", "index2", "index3")), + Collections.emptyMap() + ); + ClusterState clusterState = new ClusterState.Builder(new ClusterName("name")).metadata( + new Metadata.Builder().putCustom(AutoFollowMetadata.TYPE, autoFollowMetadata) + ).build(); - Metadata remoteMetadata = new Metadata.Builder() - .put(IndexMetadata.builder("index1") - .settings(settings(Version.CURRENT) - .put(IndexMetadata.SETTING_INDEX_UUID, "index1")) - .numberOfShards(1) - .numberOfReplicas(0)) - .put(IndexMetadata.builder("index2") - .settings(settings(Version.CURRENT) - .put(IndexMetadata.SETTING_INDEX_UUID, "index2")) - .numberOfShards(1) - .numberOfReplicas(0)) - .put(IndexMetadata.builder("index3") - .settings(settings(Version.CURRENT) - .put(IndexMetadata.SETTING_INDEX_UUID, "index3")) + Metadata remoteMetadata = new Metadata.Builder().put( + IndexMetadata.builder("index1") + .settings(settings(Version.CURRENT).put(IndexMetadata.SETTING_INDEX_UUID, "index1")) .numberOfShards(1) - .numberOfReplicas(0)) + .numberOfReplicas(0) + ) + .put( + IndexMetadata.builder("index2") + .settings(settings(Version.CURRENT).put(IndexMetadata.SETTING_INDEX_UUID, "index2")) + .numberOfShards(1) + .numberOfReplicas(0) + ) + .put( + IndexMetadata.builder("index3") + .settings(settings(Version.CURRENT).put(IndexMetadata.SETTING_INDEX_UUID, "index3")) + .numberOfShards(1) + .numberOfReplicas(0) + ) .build(); Function function = cleanFollowedRemoteIndices(remoteMetadata, Collections.singletonList("pattern1")); @@ -1015,18 +1071,18 @@ public void testCleanFollowedLeaderIndicesNoChanges() { } public void testCleanFollowedLeaderIndicesNoEntry() { - AutoFollowMetadata autoFollowMetadata = new AutoFollowMetadata(Collections.emptyMap(), - Map.of("pattern2", Arrays.asList("index1", "index2", "index3")), Collections.emptyMap()); - ClusterState clusterState = new ClusterState.Builder(new ClusterName("name")) - .metadata(new Metadata.Builder().putCustom(AutoFollowMetadata.TYPE, autoFollowMetadata)) - .build(); + AutoFollowMetadata autoFollowMetadata = new AutoFollowMetadata( + Collections.emptyMap(), + Map.of("pattern2", Arrays.asList("index1", "index2", "index3")), + Collections.emptyMap() + ); + ClusterState clusterState = new ClusterState.Builder(new ClusterName("name")).metadata( + new Metadata.Builder().putCustom(AutoFollowMetadata.TYPE, autoFollowMetadata) + ).build(); - Metadata remoteMetadata = new Metadata.Builder() - .put(IndexMetadata.builder("index1") - .settings(settings(Version.CURRENT)) - .numberOfShards(1) - .numberOfReplicas(0)) - .build(); + Metadata remoteMetadata = new Metadata.Builder().put( + IndexMetadata.builder("index1").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(0) + ).build(); Function function = cleanFollowedRemoteIndices(remoteMetadata, Collections.singletonList("pattern1")); ClusterState result = function.apply(clusterState); @@ -1103,19 +1159,18 @@ public void testStats() { new CcrLicenseChecker(() -> true, () -> false), () -> 1L, () -> 1L, - Runnable::run); - - autoFollowCoordinator.updateStats(Collections.singletonList( - new AutoFollowCoordinator.AutoFollowResult("_alias1")) + Runnable::run ); + + autoFollowCoordinator.updateStats(Collections.singletonList(new AutoFollowCoordinator.AutoFollowResult("_alias1"))); AutoFollowStats autoFollowStats = autoFollowCoordinator.getStats(); assertThat(autoFollowStats.getNumberOfFailedFollowIndices(), equalTo(0L)); assertThat(autoFollowStats.getNumberOfFailedRemoteClusterStateRequests(), equalTo(0L)); assertThat(autoFollowStats.getNumberOfSuccessfulFollowIndices(), equalTo(0L)); assertThat(autoFollowStats.getRecentAutoFollowErrors().size(), equalTo(0)); - autoFollowCoordinator.updateStats(Collections.singletonList( - new AutoFollowCoordinator.AutoFollowResult("_alias1", new RuntimeException("error"))) + autoFollowCoordinator.updateStats( + Collections.singletonList(new AutoFollowCoordinator.AutoFollowResult("_alias1", new RuntimeException("error"))) ); autoFollowStats = autoFollowCoordinator.getStats(); assertThat(autoFollowStats.getNumberOfFailedFollowIndices(), equalTo(0L)); @@ -1124,12 +1179,18 @@ public void testStats() { assertThat(autoFollowStats.getRecentAutoFollowErrors().size(), equalTo(1)); assertThat(autoFollowStats.getRecentAutoFollowErrors().get("_alias1").v2().getCause().getMessage(), equalTo("error")); - autoFollowCoordinator.updateStats(Arrays.asList( - new AutoFollowCoordinator.AutoFollowResult("_alias1", - Collections.singletonList(Tuple.tuple(new Index("index1", "_na_"), new RuntimeException("error-1")))), - new AutoFollowCoordinator.AutoFollowResult("_alias2", - Collections.singletonList(Tuple.tuple(new Index("index2", "_na_"), new RuntimeException("error-2")))) - )); + autoFollowCoordinator.updateStats( + Arrays.asList( + new AutoFollowCoordinator.AutoFollowResult( + "_alias1", + Collections.singletonList(Tuple.tuple(new Index("index1", "_na_"), new RuntimeException("error-1"))) + ), + new AutoFollowCoordinator.AutoFollowResult( + "_alias2", + Collections.singletonList(Tuple.tuple(new Index("index2", "_na_"), new RuntimeException("error-2"))) + ) + ) + ); autoFollowStats = autoFollowCoordinator.getStats(); assertThat(autoFollowStats.getNumberOfFailedFollowIndices(), equalTo(2L)); assertThat(autoFollowStats.getNumberOfFailedRemoteClusterStateRequests(), equalTo(1L)); @@ -1139,12 +1200,18 @@ public void testStats() { assertThat(autoFollowStats.getRecentAutoFollowErrors().get("_alias1:index1").v2().getCause().getMessage(), equalTo("error-1")); assertThat(autoFollowStats.getRecentAutoFollowErrors().get("_alias2:index2").v2().getCause().getMessage(), equalTo("error-2")); - autoFollowCoordinator.updateStats(Arrays.asList( - new AutoFollowCoordinator.AutoFollowResult("_alias1", - Collections.singletonList(Tuple.tuple(new Index("index1", "_na_"), null))), - new AutoFollowCoordinator.AutoFollowResult("_alias2", - Collections.singletonList(Tuple.tuple(new Index("index2", "_na_"), null))) - )); + autoFollowCoordinator.updateStats( + Arrays.asList( + new AutoFollowCoordinator.AutoFollowResult( + "_alias1", + Collections.singletonList(Tuple.tuple(new Index("index1", "_na_"), null)) + ), + new AutoFollowCoordinator.AutoFollowResult( + "_alias2", + Collections.singletonList(Tuple.tuple(new Index("index2", "_na_"), null)) + ) + ) + ); autoFollowStats = autoFollowCoordinator.getStats(); assertThat(autoFollowStats.getNumberOfFailedFollowIndices(), equalTo(2L)); assertThat(autoFollowStats.getNumberOfFailedRemoteClusterStateRequests(), equalTo(1L)); @@ -1157,8 +1224,13 @@ public void testUpdateAutoFollowers() { ClusterService clusterService = mockClusterService(); // Return a cluster state with no patterns so that the auto followers never really execute: ClusterState followerState = ClusterState.builder(new ClusterName("remote")) - .metadata(Metadata.builder().putCustom(AutoFollowMetadata.TYPE, - new AutoFollowMetadata(Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap()))) + .metadata( + Metadata.builder() + .putCustom( + AutoFollowMetadata.TYPE, + new AutoFollowMetadata(Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap()) + ) + ) .build(); when(clusterService.state()).thenReturn(followerState); AutoFollowCoordinator autoFollowCoordinator = new AutoFollowCoordinator( @@ -1168,11 +1240,13 @@ public void testUpdateAutoFollowers() { new CcrLicenseChecker(() -> true, () -> false), () -> 1L, () -> 1L, - Runnable::run); + Runnable::run + ); // Add 3 patterns: Map patterns = new HashMap<>(); patterns.put( - "pattern1", new AutoFollowPattern( + "pattern1", + new AutoFollowPattern( "remote1", Collections.singletonList("logs-*"), Collections.emptyList(), @@ -1234,8 +1308,10 @@ public void testUpdateAutoFollowers() { ) ); ClusterState clusterState = ClusterState.builder(new ClusterName("remote")) - .metadata(Metadata.builder().putCustom(AutoFollowMetadata.TYPE, - new AutoFollowMetadata(patterns, Collections.emptyMap(), Collections.emptyMap()))) + .metadata( + Metadata.builder() + .putCustom(AutoFollowMetadata.TYPE, new AutoFollowMetadata(patterns, Collections.emptyMap(), Collections.emptyMap())) + ) .build(); autoFollowCoordinator.updateAutoFollowers(clusterState); assertThat(autoFollowCoordinator.getStats().getAutoFollowedClusters().size(), equalTo(2)); @@ -1249,8 +1325,10 @@ public void testUpdateAutoFollowers() { patterns.remove("pattern1"); patterns.remove("pattern3"); clusterState = ClusterState.builder(new ClusterName("remote")) - .metadata(Metadata.builder().putCustom(AutoFollowMetadata.TYPE, - new AutoFollowMetadata(patterns, Collections.emptyMap(), Collections.emptyMap()))) + .metadata( + Metadata.builder() + .putCustom(AutoFollowMetadata.TYPE, new AutoFollowMetadata(patterns, Collections.emptyMap(), Collections.emptyMap())) + ) .build(); autoFollowCoordinator.updateAutoFollowers(clusterState); assertThat(autoFollowCoordinator.getStats().getAutoFollowedClusters().size(), equalTo(1)); @@ -1279,8 +1357,10 @@ public void testUpdateAutoFollowers() { ) ); clusterState = ClusterState.builder(new ClusterName("remote")) - .metadata(Metadata.builder().putCustom(AutoFollowMetadata.TYPE, - new AutoFollowMetadata(patterns, Collections.emptyMap(), Collections.emptyMap()))) + .metadata( + Metadata.builder() + .putCustom(AutoFollowMetadata.TYPE, new AutoFollowMetadata(patterns, Collections.emptyMap(), Collections.emptyMap())) + ) .build(); autoFollowCoordinator.updateAutoFollowers(clusterState); assertThat(autoFollowCoordinator.getStats().getAutoFollowedClusters().size(), equalTo(2)); @@ -1296,8 +1376,10 @@ public void testUpdateAutoFollowers() { patterns.remove("pattern2"); patterns.remove("pattern4"); clusterState = ClusterState.builder(new ClusterName("remote")) - .metadata(Metadata.builder().putCustom(AutoFollowMetadata.TYPE, - new AutoFollowMetadata(patterns, Collections.emptyMap(), Collections.emptyMap()))) + .metadata( + Metadata.builder() + .putCustom(AutoFollowMetadata.TYPE, new AutoFollowMetadata(patterns, Collections.emptyMap(), Collections.emptyMap())) + ) .build(); autoFollowCoordinator.updateAutoFollowers(clusterState); assertThat(autoFollowCoordinator.getStats().getAutoFollowedClusters().size(), equalTo(0)); @@ -1313,10 +1395,16 @@ public void testUpdateAutoFollowersNoPatterns() { new CcrLicenseChecker(() -> true, () -> false), () -> 1L, () -> 1L, - Runnable::run); + Runnable::run + ); ClusterState clusterState = ClusterState.builder(new ClusterName("remote")) - .metadata(Metadata.builder().putCustom(AutoFollowMetadata.TYPE, - new AutoFollowMetadata(Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap()))) + .metadata( + Metadata.builder() + .putCustom( + AutoFollowMetadata.TYPE, + new AutoFollowMetadata(Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap()) + ) + ) .build(); autoFollowCoordinator.updateAutoFollowers(clusterState); assertThat(autoFollowCoordinator.getStats().getAutoFollowedClusters().size(), equalTo(0)); @@ -1330,7 +1418,8 @@ public void testUpdateAutoFollowersNoAutoFollowMetadata() { new CcrLicenseChecker(() -> true, () -> false), () -> 1L, () -> 1L, - Runnable::run); + Runnable::run + ); ClusterState clusterState = ClusterState.builder(new ClusterName("remote")).build(); autoFollowCoordinator.updateAutoFollowers(clusterState); assertThat(autoFollowCoordinator.getStats().getAutoFollowedClusters().size(), equalTo(0)); @@ -1345,7 +1434,8 @@ public void testUpdateAutoFollowersNoActivePatterns() { new CcrLicenseChecker(() -> true, () -> false), () -> 1L, () -> 1L, - Runnable::run); + Runnable::run + ); autoFollowCoordinator.updateAutoFollowers(ClusterState.EMPTY_STATE); assertThat(autoFollowCoordinator.getStats().getAutoFollowedClusters().size(), equalTo(0)); @@ -1375,7 +1465,8 @@ public void testUpdateAutoFollowersNoActivePatterns() { ); patterns.put( "pattern2", - new AutoFollowPattern("remote2", + new AutoFollowPattern( + "remote2", Collections.singletonList("logs-*"), Collections.emptyList(), null, @@ -1395,7 +1486,8 @@ public void testUpdateAutoFollowersNoActivePatterns() { ); patterns.put( "pattern3", - new AutoFollowPattern("remote2", + new AutoFollowPattern( + "remote2", Collections.singletonList("metrics-*"), Collections.emptyList(), null, @@ -1414,10 +1506,17 @@ public void testUpdateAutoFollowersNoActivePatterns() { ) ); - autoFollowCoordinator.updateAutoFollowers(ClusterState.builder(new ClusterName("remote")) - .metadata(Metadata.builder().putCustom(AutoFollowMetadata.TYPE, - new AutoFollowMetadata(patterns, Collections.emptyMap(), Collections.emptyMap()))) - .build()); + autoFollowCoordinator.updateAutoFollowers( + ClusterState.builder(new ClusterName("remote")) + .metadata( + Metadata.builder() + .putCustom( + AutoFollowMetadata.TYPE, + new AutoFollowMetadata(patterns, Collections.emptyMap(), Collections.emptyMap()) + ) + ) + .build() + ); assertThat(autoFollowCoordinator.getStats().getAutoFollowedClusters().size(), equalTo(2)); assertThat(autoFollowCoordinator.getStats().getAutoFollowedClusters().get("remote1"), notNullValue()); assertThat(autoFollowCoordinator.getStats().getAutoFollowedClusters().get("remote2"), notNullValue()); @@ -1451,7 +1550,8 @@ public void testUpdateAutoFollowersNoActivePatterns() { ); patterns.computeIfPresent( "pattern3", - (name, pattern) -> new AutoFollowPattern(pattern.getRemoteCluster(), + (name, pattern) -> new AutoFollowPattern( + pattern.getRemoteCluster(), pattern.getLeaderIndexPatterns(), pattern.getLeaderIndexExclusionPatterns(), pattern.getFollowIndexPattern(), @@ -1470,10 +1570,17 @@ public void testUpdateAutoFollowersNoActivePatterns() { ) ); - autoFollowCoordinator.updateAutoFollowers(ClusterState.builder(new ClusterName("remote")) - .metadata(Metadata.builder().putCustom(AutoFollowMetadata.TYPE, - new AutoFollowMetadata(patterns, Collections.emptyMap(), Collections.emptyMap()))) - .build()); + autoFollowCoordinator.updateAutoFollowers( + ClusterState.builder(new ClusterName("remote")) + .metadata( + Metadata.builder() + .putCustom( + AutoFollowMetadata.TYPE, + new AutoFollowMetadata(patterns, Collections.emptyMap(), Collections.emptyMap()) + ) + ) + .build() + ); assertThat(autoFollowCoordinator.getStats().getAutoFollowedClusters().size(), equalTo(1)); assertThat(autoFollowCoordinator.getStats().getAutoFollowedClusters().get("remote2"), notNullValue()); assertThat(removedAutoFollower1.removed, is(true)); @@ -1503,7 +1610,8 @@ public void testUpdateAutoFollowersNoActivePatterns() { ); patterns.computeIfPresent( "pattern2", - (name, pattern) -> new AutoFollowPattern(pattern.getRemoteCluster(), + (name, pattern) -> new AutoFollowPattern( + pattern.getRemoteCluster(), pattern.getLeaderIndexPatterns(), pattern.getLeaderIndexExclusionPatterns(), pattern.getFollowIndexPattern(), @@ -1522,10 +1630,17 @@ public void testUpdateAutoFollowersNoActivePatterns() { ) ); - autoFollowCoordinator.updateAutoFollowers(ClusterState.builder(new ClusterName("remote")) - .metadata(Metadata.builder().putCustom(AutoFollowMetadata.TYPE, - new AutoFollowMetadata(patterns, Collections.emptyMap(), Collections.emptyMap()))) - .build()); + autoFollowCoordinator.updateAutoFollowers( + ClusterState.builder(new ClusterName("remote")) + .metadata( + Metadata.builder() + .putCustom( + AutoFollowMetadata.TYPE, + new AutoFollowMetadata(patterns, Collections.emptyMap(), Collections.emptyMap()) + ) + ) + .build() + ); assertThat(autoFollowCoordinator.getStats().getAutoFollowedClusters().size(), equalTo(1)); assertThat(autoFollowCoordinator.getStats().getAutoFollowedClusters().get("remote1"), notNullValue()); @@ -1534,10 +1649,17 @@ public void testUpdateAutoFollowersNoActivePatterns() { assertNotSame(removedAutoFollower4, removedAutoFollower1); assertThat(removedAutoFollower2.removed, is(true)); - autoFollowCoordinator.updateAutoFollowers(ClusterState.builder(new ClusterName("remote")) - .metadata(Metadata.builder().putCustom(AutoFollowMetadata.TYPE, - new AutoFollowMetadata(Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap()))) - .build()); + autoFollowCoordinator.updateAutoFollowers( + ClusterState.builder(new ClusterName("remote")) + .metadata( + Metadata.builder() + .putCustom( + AutoFollowMetadata.TYPE, + new AutoFollowMetadata(Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap()) + ) + ) + .build() + ); assertThat(autoFollowCoordinator.getStats().getAutoFollowedClusters().size(), equalTo(0)); assertThat(removedAutoFollower1.removed, is(true)); assertThat(removedAutoFollower2.removed, is(true)); @@ -1581,8 +1703,9 @@ public void testWaitForMetadataVersion() { .metadata(Metadata.builder().putCustom(AutoFollowMetadata.TYPE, autoFollowMetadata)) .build(); String indexName = "logs-" + i; - leaderStates.add(i == 0 ? createRemoteClusterState(indexName, true) : - createRemoteClusterState(leaderStates.get(i - 1), indexName)); + leaderStates.add( + i == 0 ? createRemoteClusterState(indexName, true) : createRemoteClusterState(leaderStates.get(i - 1), indexName) + ); } List allResults = new ArrayList<>(); @@ -1592,25 +1715,24 @@ public void testWaitForMetadataVersion() { long previousRequestedMetadataVersion = 0; @Override - void getRemoteClusterState(String remoteCluster, - long metadataVersion, - BiConsumer handler) { + void getRemoteClusterState(String remoteCluster, long metadataVersion, BiConsumer handler) { assertThat(remoteCluster, equalTo("remote")); assertThat(metadataVersion, greaterThan(previousRequestedMetadataVersion)); handler.accept(new ClusterStateResponse(new ClusterName("name"), leaderStates.poll(), false), null); } @Override - void createAndFollow(Map headers, - PutFollowAction.Request followRequest, - Runnable successHandler, - Consumer failureHandler) { + void createAndFollow( + Map headers, + PutFollowAction.Request followRequest, + Runnable successHandler, + Consumer failureHandler + ) { successHandler.run(); } @Override - void updateAutoFollowMetadata(Function updateFunction, - Consumer handler) { + void updateAutoFollowMetadata(Function updateFunction, Consumer handler) { handler.accept(null); } }; @@ -1618,8 +1740,10 @@ void updateAutoFollowMetadata(Function updateFunctio assertThat(allResults.size(), equalTo(states.length)); for (int i = 0; i < states.length; i++) { final String indexName = "logs-" + i; - assertThat(allResults.get(i).autoFollowExecutionResults.keySet().stream() - .anyMatch(index -> index.getName().equals(indexName)), is(true)); + assertThat( + allResults.get(i).autoFollowExecutionResults.keySet().stream().anyMatch(index -> index.getName().equals(indexName)), + is(true) + ); } } @@ -1659,18 +1783,14 @@ public void testWaitForTimeOut() { .metadata(Metadata.builder().putCustom(AutoFollowMetadata.TYPE, autoFollowMetadata)) .build(); } - Consumer> handler = results -> { - fail("should not be invoked"); - }; + Consumer> handler = results -> { fail("should not be invoked"); }; AtomicInteger counter = new AtomicInteger(); AutoFollower autoFollower = new AutoFollower("remote", handler, localClusterStateSupplier(states), () -> 1L, Runnable::run) { long previousRequestedMetadataVersion = 0; @Override - void getRemoteClusterState(String remoteCluster, - long metadataVersion, - BiConsumer handler) { + void getRemoteClusterState(String remoteCluster, long metadataVersion, BiConsumer handler) { counter.incrementAndGet(); assertThat(remoteCluster, equalTo("remote")); assertThat(metadataVersion, greaterThan(previousRequestedMetadataVersion)); @@ -1678,16 +1798,17 @@ void getRemoteClusterState(String remoteCluster, } @Override - void createAndFollow(Map headers, - PutFollowAction.Request followRequest, - Runnable successHandler, - Consumer failureHandler) { + void createAndFollow( + Map headers, + PutFollowAction.Request followRequest, + Runnable successHandler, + Consumer failureHandler + ) { fail("should not be invoked"); } @Override - void updateAutoFollowMetadata(Function updateFunction, - Consumer handler) { + void updateAutoFollowMetadata(Function updateFunction, Consumer handler) { fail("should not be invoked"); } }; @@ -1735,24 +1856,23 @@ public void testAutoFollowerSoftDeletesDisabled() { Consumer> handler = results::addAll; AutoFollower autoFollower = new AutoFollower("remote", handler, localClusterStateSupplier(currentState), () -> 1L, Runnable::run) { @Override - void getRemoteClusterState(String remoteCluster, - long metadataVersion, - BiConsumer handler) { + void getRemoteClusterState(String remoteCluster, long metadataVersion, BiConsumer handler) { assertThat(remoteCluster, equalTo("remote")); handler.accept(new ClusterStateResponse(new ClusterName("name"), remoteState, false), null); } @Override - void createAndFollow(Map headers, - PutFollowAction.Request followRequest, - Runnable successHandler, - Consumer failureHandler) { + void createAndFollow( + Map headers, + PutFollowAction.Request followRequest, + Runnable successHandler, + Consumer failureHandler + ) { fail("soft deletes are disabled; index should not be followed"); } @Override - void updateAutoFollowMetadata(Function updateFunction, - Consumer handler) { + void updateAutoFollowMetadata(Function updateFunction, Consumer handler) { ClusterState resultCs = updateFunction.apply(currentState); AutoFollowMetadata result = resultCs.metadata().custom(AutoFollowMetadata.TYPE); assertThat(result.getFollowedLeaderIndexUUIDs().size(), equalTo(1)); @@ -1773,8 +1893,10 @@ void cleanFollowedRemoteIndices(ClusterState remoteClusterState, List pa assertThat(entries.size(), equalTo(1)); assertThat(entries.get(0).getKey().getName(), equalTo("logs-20190101")); assertThat(entries.get(0).getValue(), notNullValue()); - assertThat(entries.get(0).getValue().getMessage(), equalTo("index [logs-20190101] cannot be followed, " + - "because soft deletes are not enabled")); + assertThat( + entries.get(0).getValue().getMessage(), + equalTo("index [logs-20190101] cannot be followed, " + "because soft deletes are not enabled") + ); } public void testAutoFollowerFollowerIndexAlreadyExists() { @@ -1810,41 +1932,46 @@ public void testAutoFollowerFollowerIndexAlreadyExists() { AutoFollowMetadata autoFollowMetadata = new AutoFollowMetadata(patterns, followedLeaderIndexUUIDS, autoFollowHeaders); ClusterState currentState = ClusterState.builder(new ClusterName("name")) - .metadata(Metadata.builder() - .put(IndexMetadata.builder("logs-20190101") - .settings(settings(Version.CURRENT)) - .putCustom(Ccr.CCR_CUSTOM_METADATA_KEY, Map.of(Ccr.CCR_CUSTOM_METADATA_LEADER_INDEX_UUID_KEY, - remoteState.metadata().index("logs-20190101").getIndexUUID())) - .numberOfShards(1) - .numberOfReplicas(0)) - .putCustom(AutoFollowMetadata.TYPE, autoFollowMetadata)) + .metadata( + Metadata.builder() + .put( + IndexMetadata.builder("logs-20190101") + .settings(settings(Version.CURRENT)) + .putCustom( + Ccr.CCR_CUSTOM_METADATA_KEY, + Map.of( + Ccr.CCR_CUSTOM_METADATA_LEADER_INDEX_UUID_KEY, + remoteState.metadata().index("logs-20190101").getIndexUUID() + ) + ) + .numberOfShards(1) + .numberOfReplicas(0) + ) + .putCustom(AutoFollowMetadata.TYPE, autoFollowMetadata) + ) .build(); - final Object[] resultHolder = new Object[1]; - Consumer> handler = results -> { - resultHolder[0] = results; - }; + Consumer> handler = results -> { resultHolder[0] = results; }; AutoFollower autoFollower = new AutoFollower("remote", handler, localClusterStateSupplier(currentState), () -> 1L, Runnable::run) { @Override - void getRemoteClusterState(String remoteCluster, - long metadataVersion, - BiConsumer handler) { + void getRemoteClusterState(String remoteCluster, long metadataVersion, BiConsumer handler) { assertThat(remoteCluster, equalTo("remote")); handler.accept(new ClusterStateResponse(new ClusterName("name"), remoteState, false), null); } @Override - void createAndFollow(Map headers, - PutFollowAction.Request followRequest, - Runnable successHandler, - Consumer failureHandler) { + void createAndFollow( + Map headers, + PutFollowAction.Request followRequest, + Runnable successHandler, + Consumer failureHandler + ) { fail("this should not be invoked"); } @Override - void updateAutoFollowMetadata(Function updateFunction, - Consumer handler) { + void updateAutoFollowMetadata(Function updateFunction, Consumer handler) { ClusterState resultCs = updateFunction.apply(currentState); AutoFollowMetadata result = resultCs.metadata().custom(AutoFollowMetadata.TYPE); assertThat(result.getFollowedLeaderIndexUUIDs().size(), equalTo(1)); @@ -1914,7 +2041,8 @@ public void testRepeatedFailures() throws InterruptedException { void getRemoteClusterState( final String remoteCluster, final long metadataVersion, - final BiConsumer handler) { + final BiConsumer handler + ) { counter.incrementAndGet(); if (counter.incrementAndGet() > iterations) { this.stop(); @@ -1934,14 +2062,16 @@ void createAndFollow( final Map headers, final PutFollowAction.Request followRequest, final Runnable successHandler, - final Consumer failureHandler) { + final Consumer failureHandler + ) { } @Override void updateAutoFollowMetadata( final Function updateFunction, - final Consumer handler) { + final Consumer handler + ) { } @@ -1959,32 +2089,37 @@ public void testClosedIndicesAreNotAutoFollowed() { final String pattern = "pattern1"; final ClusterState localState = ClusterState.builder(new ClusterName("local")) - .metadata(Metadata.builder() - .putCustom(AutoFollowMetadata.TYPE, - new AutoFollowMetadata( - Map.of( - pattern, - new AutoFollowPattern( - "remote", - List.of("docs-*"), - Collections.emptyList(), - null, - Settings.EMPTY, - true, - null, - null, - null, - null, - null, - null, - null, - null, - null, - null - ) - ), - Map.of(pattern, List.of()), - Map.of(pattern, Map.of())))) + .metadata( + Metadata.builder() + .putCustom( + AutoFollowMetadata.TYPE, + new AutoFollowMetadata( + Map.of( + pattern, + new AutoFollowPattern( + "remote", + List.of("docs-*"), + Collections.emptyList(), + null, + Settings.EMPTY, + true, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null + ) + ), + Map.of(pattern, List.of()), + Map.of(pattern, Map.of()) + ) + ) + ) .build(); ClusterState remoteState = null; @@ -2000,9 +2135,14 @@ public void testClosedIndicesAreNotAutoFollowed() { // randomly close the index remoteState = ClusterState.builder(remoteState.getClusterName()) .routingTable(remoteState.routingTable()) - .metadata(Metadata.builder(remoteState.metadata()) - .put(IndexMetadata.builder(remoteState.metadata().index(indexName)).state(IndexMetadata.State.CLOSE).build(), true) - .build()) + .metadata( + Metadata.builder(remoteState.metadata()) + .put( + IndexMetadata.builder(remoteState.metadata().index(indexName)).state(IndexMetadata.State.CLOSE).build(), + true + ) + .build() + ) .build(); } } @@ -2011,36 +2151,41 @@ public void testClosedIndicesAreNotAutoFollowed() { final AtomicReference lastModifiedClusterState = new AtomicReference<>(localState); final List results = new ArrayList<>(); final Set followedIndices = ConcurrentCollections.newConcurrentSet(); - final AutoFollower autoFollower = - new AutoFollower("remote", results::addAll, localClusterStateSupplier(localState), () -> 1L, Runnable::run) { - @Override - void getRemoteClusterState(String remoteCluster, - long metadataVersion, - BiConsumer handler) { - assertThat(remoteCluster, equalTo("remote")); - handler.accept(new ClusterStateResponse(new ClusterName("remote"), finalRemoteState, false), null); - } + final AutoFollower autoFollower = new AutoFollower( + "remote", + results::addAll, + localClusterStateSupplier(localState), + () -> 1L, + Runnable::run + ) { + @Override + void getRemoteClusterState(String remoteCluster, long metadataVersion, BiConsumer handler) { + assertThat(remoteCluster, equalTo("remote")); + handler.accept(new ClusterStateResponse(new ClusterName("remote"), finalRemoteState, false), null); + } - @Override - void createAndFollow(Map headers, - PutFollowAction.Request followRequest, - Runnable successHandler, - Consumer failureHandler) { - followedIndices.add(followRequest.getLeaderIndex()); - successHandler.run(); - } + @Override + void createAndFollow( + Map headers, + PutFollowAction.Request followRequest, + Runnable successHandler, + Consumer failureHandler + ) { + followedIndices.add(followRequest.getLeaderIndex()); + successHandler.run(); + } - @Override - void updateAutoFollowMetadata(Function updateFunction, Consumer handler) { - lastModifiedClusterState.updateAndGet(updateFunction::apply); - handler.accept(null); - } + @Override + void updateAutoFollowMetadata(Function updateFunction, Consumer handler) { + lastModifiedClusterState.updateAndGet(updateFunction::apply); + handler.accept(null); + } - @Override - void cleanFollowedRemoteIndices(ClusterState remoteClusterState, List patterns) { - // Ignore, to avoid invoking updateAutoFollowMetadata(...) twice - } - }; + @Override + void cleanFollowedRemoteIndices(ClusterState remoteClusterState, List patterns) { + // Ignore, to avoid invoking updateAutoFollowMetadata(...) twice + } + }; autoFollower.start(); assertThat(results, notNullValue()); @@ -2059,32 +2204,37 @@ public void testExcludedPatternIndicesAreNotAutoFollowed() { final String pattern = "pattern1"; final ClusterState localState = ClusterState.builder(new ClusterName("local")) - .metadata(Metadata.builder() - .putCustom(AutoFollowMetadata.TYPE, - new AutoFollowMetadata( - Map.of( - pattern, - new AutoFollowPattern( - "remote", - List.of("docs-*"), - List.of("docs-excluded-*"), - null, - Settings.EMPTY, - true, - null, - null, - null, - null, - null, - null, - null, - null, - null, - null - ) - ), - Map.of(pattern, List.of()), - Map.of(pattern, Map.of())))) + .metadata( + Metadata.builder() + .putCustom( + AutoFollowMetadata.TYPE, + new AutoFollowMetadata( + Map.of( + pattern, + new AutoFollowPattern( + "remote", + List.of("docs-*"), + List.of("docs-excluded-*"), + null, + Settings.EMPTY, + true, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null + ) + ), + Map.of(pattern, List.of()), + Map.of(pattern, Map.of()) + ) + ) + ) .build(); ClusterState remoteState = ClusterState.EMPTY_STATE; @@ -2104,36 +2254,41 @@ public void testExcludedPatternIndicesAreNotAutoFollowed() { final AtomicReference lastModifiedClusterState = new AtomicReference<>(localState); final List results = new ArrayList<>(); final Set followedIndices = ConcurrentCollections.newConcurrentSet(); - final AutoFollower autoFollower = - new AutoFollower("remote", results::addAll, localClusterStateSupplier(localState), () -> 1L, Runnable::run) { - @Override - void getRemoteClusterState(String remoteCluster, - long metadataVersion, - BiConsumer handler) { - assertThat(remoteCluster, equalTo("remote")); - handler.accept(new ClusterStateResponse(new ClusterName("remote"), finalRemoteState, false), null); - } + final AutoFollower autoFollower = new AutoFollower( + "remote", + results::addAll, + localClusterStateSupplier(localState), + () -> 1L, + Runnable::run + ) { + @Override + void getRemoteClusterState(String remoteCluster, long metadataVersion, BiConsumer handler) { + assertThat(remoteCluster, equalTo("remote")); + handler.accept(new ClusterStateResponse(new ClusterName("remote"), finalRemoteState, false), null); + } - @Override - void createAndFollow(Map headers, - PutFollowAction.Request followRequest, - Runnable successHandler, - Consumer failureHandler) { - followedIndices.add(followRequest.getLeaderIndex()); - successHandler.run(); - } + @Override + void createAndFollow( + Map headers, + PutFollowAction.Request followRequest, + Runnable successHandler, + Consumer failureHandler + ) { + followedIndices.add(followRequest.getLeaderIndex()); + successHandler.run(); + } - @Override - void updateAutoFollowMetadata(Function updateFunction, Consumer handler) { - lastModifiedClusterState.updateAndGet(updateFunction::apply); - handler.accept(null); - } + @Override + void updateAutoFollowMetadata(Function updateFunction, Consumer handler) { + lastModifiedClusterState.updateAndGet(updateFunction::apply); + handler.accept(null); + } - @Override - void cleanFollowedRemoteIndices(ClusterState remoteClusterState, List patterns) { - // Ignore, to avoid invoking updateAutoFollowMetadata(...) twice - } - }; + @Override + void cleanFollowedRemoteIndices(ClusterState remoteClusterState, List patterns) { + // Ignore, to avoid invoking updateAutoFollowMetadata(...) twice + } + }; autoFollower.start(); assertThat(results, notNullValue()); @@ -2171,8 +2326,10 @@ public void testSystemIndicesAreNotAutoFollowed() { } public void testSystemDataStreamsAreNotAutoFollowed() { - Tuple, Set> autoFollowResults = - executeAutoFollow("*.", createRemoteClusterStateWithDataStream(".test-data-stream")); + Tuple, Set> autoFollowResults = executeAutoFollow( + "*.", + createRemoteClusterStateWithDataStream(".test-data-stream") + ); assertThat(autoFollowResults.v1().size(), equalTo(1)); assertThat(autoFollowResults.v1().get(0).autoFollowExecutionResults, is(anEmptyMap())); @@ -2193,74 +2350,86 @@ public void testFollowerIndexIsCreatedInExecuteAutoFollow() { assertThat(autoFollowResults.v2().contains(indexName), equalTo(true)); } - private Tuple, Set> executeAutoFollow(String indexPattern, - ClusterState finalRemoteState) { + private Tuple, Set> executeAutoFollow( + String indexPattern, + ClusterState finalRemoteState + ) { final Client client = mock(Client.class); when(client.getRemoteClusterClient(anyString())).thenReturn(client); final String pattern = "pattern1"; final ClusterState localState = ClusterState.builder(new ClusterName("local")) - .metadata(Metadata.builder() - .putCustom(AutoFollowMetadata.TYPE, - new AutoFollowMetadata( - Map.of( - pattern, - new AutoFollowPattern( - "remote", - List.of(indexPattern), - Collections.emptyList(), - null, - Settings.EMPTY, - true, - null, - null, - null, - null, - null, - null, - null, - null, - null, - null - ) - ), - Map.of(pattern, List.of()), - Map.of(pattern, Map.of())))) + .metadata( + Metadata.builder() + .putCustom( + AutoFollowMetadata.TYPE, + new AutoFollowMetadata( + Map.of( + pattern, + new AutoFollowPattern( + "remote", + List.of(indexPattern), + Collections.emptyList(), + null, + Settings.EMPTY, + true, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null + ) + ), + Map.of(pattern, List.of()), + Map.of(pattern, Map.of()) + ) + ) + ) .build(); final AtomicReference lastModifiedClusterState = new AtomicReference<>(localState); final List results = new ArrayList<>(); final Set followedIndices = ConcurrentCollections.newConcurrentSet(); - final AutoFollower autoFollower = - new AutoFollower("remote", results::addAll, localClusterStateSupplier(localState), () -> 1L, Runnable::run) { - @Override - void getRemoteClusterState(String remoteCluster, - long metadataVersion, - BiConsumer handler) { - assertThat(remoteCluster, equalTo("remote")); - handler.accept(new ClusterStateResponse(new ClusterName("remote"), finalRemoteState, false), null); - } + final AutoFollower autoFollower = new AutoFollower( + "remote", + results::addAll, + localClusterStateSupplier(localState), + () -> 1L, + Runnable::run + ) { + @Override + void getRemoteClusterState(String remoteCluster, long metadataVersion, BiConsumer handler) { + assertThat(remoteCluster, equalTo("remote")); + handler.accept(new ClusterStateResponse(new ClusterName("remote"), finalRemoteState, false), null); + } - @Override - void createAndFollow(Map headers, - PutFollowAction.Request followRequest, - Runnable successHandler, - Consumer failureHandler) { - followedIndices.add(followRequest.getLeaderIndex()); - successHandler.run(); - } + @Override + void createAndFollow( + Map headers, + PutFollowAction.Request followRequest, + Runnable successHandler, + Consumer failureHandler + ) { + followedIndices.add(followRequest.getLeaderIndex()); + successHandler.run(); + } - @Override - void updateAutoFollowMetadata(Function updateFunction, Consumer handler) { - lastModifiedClusterState.updateAndGet(updateFunction::apply); - handler.accept(null); - } + @Override + void updateAutoFollowMetadata(Function updateFunction, Consumer handler) { + lastModifiedClusterState.updateAndGet(updateFunction::apply); + handler.accept(null); + } - @Override - void cleanFollowedRemoteIndices(ClusterState remoteClusterState, List patterns) { - // Ignore, to avoid invoking updateAutoFollowMetadata(...) twice - } - }; + @Override + void cleanFollowedRemoteIndices(ClusterState remoteClusterState, List patterns) { + // Ignore, to avoid invoking updateAutoFollowMetadata(...) twice + } + }; autoFollower.start(); assertThat(results, notNullValue()); @@ -2275,14 +2444,18 @@ private static ClusterState createRemoteClusterState(String indexName, boolean e return createRemoteClusterState(indexName, enableSoftDeletes, metadataVersion, false); } - private static ClusterState createRemoteClusterState(String indexName, - boolean enableSoftDeletes, - long metadataVersion, - boolean systemIndex) { + private static ClusterState createRemoteClusterState( + String indexName, + boolean enableSoftDeletes, + long metadataVersion, + boolean systemIndex + ) { Settings.Builder indexSettings; if (enableSoftDeletes == false) { - indexSettings = settings(VersionUtils.randomPreviousCompatibleVersion(random(), Version.V_8_0_0)) - .put(IndexSettings.INDEX_SOFT_DELETES_SETTING.getKey(), false); + indexSettings = settings(VersionUtils.randomPreviousCompatibleVersion(random(), Version.V_8_0_0)).put( + IndexSettings.INDEX_SOFT_DELETES_SETTING.getKey(), + false + ); } else { indexSettings = settings(Version.CURRENT); } @@ -2295,17 +2468,14 @@ private static ClusterState createRemoteClusterState(String indexName, .system(systemIndex) .build(); ClusterState.Builder csBuilder = ClusterState.builder(new ClusterName("remote")) - .metadata(Metadata.builder() - .put(indexMetadata, true) - .version(metadataVersion)); + .metadata(Metadata.builder().put(indexMetadata, true).version(metadataVersion)); - ShardRouting shardRouting = - TestShardRouting.newShardRouting(indexName, 0, "1", true, ShardRoutingState.INITIALIZING).moveToStarted(); + ShardRouting shardRouting = TestShardRouting.newShardRouting(indexName, 0, "1", true, ShardRoutingState.INITIALIZING) + .moveToStarted(); IndexRoutingTable indexRoutingTable = IndexRoutingTable.builder(indexMetadata.getIndex()).addShard(shardRouting).build(); return csBuilder.routingTable(RoutingTable.builder().add(indexRoutingTable).build()).build(); } - private static ClusterState createRemoteClusterState(final ClusterState previous, final String... indices) { return createRemoteClusterState(previous, false, indices); } @@ -2318,16 +2488,17 @@ private static ClusterState createRemoteClusterState(final ClusterState previous final RoutingTable.Builder routingTableBuilder = RoutingTable.builder(previous.routingTable()); for (String indexName : indices) { IndexMetadata indexMetadata = IndexMetadata.builder(indexName) - .settings(settings(Version.CURRENT) - .put(IndexMetadata.SETTING_INDEX_UUID, UUIDs.randomBase64UUID(random()))) + .settings(settings(Version.CURRENT).put(IndexMetadata.SETTING_INDEX_UUID, UUIDs.randomBase64UUID(random()))) .numberOfShards(1) .numberOfReplicas(0) .system(systemIndices) .build(); metadataBuilder.put(indexMetadata, true); - routingTableBuilder.add(IndexRoutingTable.builder(indexMetadata.getIndex()) - .addShard(TestShardRouting.newShardRouting(indexName, 0, "1", true, ShardRoutingState.INITIALIZING).moveToStarted()) - .build()); + routingTableBuilder.add( + IndexRoutingTable.builder(indexMetadata.getIndex()) + .addShard(TestShardRouting.newShardRouting(indexName, 0, "1", true, ShardRoutingState.INITIALIZING).moveToStarted()) + .build() + ); } return ClusterState.builder(previous.getClusterName()) .metadata(metadataBuilder.build()) @@ -2336,8 +2507,11 @@ private static ClusterState createRemoteClusterState(final ClusterState previous } private static Supplier localClusterStateSupplier(ClusterState... states) { - final AutoFollowMetadata emptyAutoFollowMetadata = - new AutoFollowMetadata(Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap()); + final AutoFollowMetadata emptyAutoFollowMetadata = new AutoFollowMetadata( + Collections.emptyMap(), + Collections.emptyMap(), + Collections.emptyMap() + ); final ClusterState lastState = ClusterState.builder(new ClusterName("remote")) .metadata(Metadata.builder().putCustom(AutoFollowMetadata.TYPE, emptyAutoFollowMetadata)) .build(); @@ -2354,8 +2528,10 @@ private static Supplier localClusterStateSupplier(ClusterState... private ClusterService mockClusterService() { ClusterService clusterService = mock(ClusterService.class); - ClusterSettings clusterSettings = - new ClusterSettings(Settings.EMPTY, Collections.singleton(CcrSettings.CCR_WAIT_FOR_METADATA_TIMEOUT)); + ClusterSettings clusterSettings = new ClusterSettings( + Settings.EMPTY, + Collections.singleton(CcrSettings.CCR_WAIT_FOR_METADATA_TIMEOUT) + ); when(clusterService.getClusterSettings()).thenReturn(clusterSettings); return clusterService; } @@ -2375,16 +2551,22 @@ private static ClusterState createRemoteClusterStateWithDataStream(String dataSt .numberOfReplicas(0) .system(system) .build(); - DataStream dataStream = new DataStream(dataStreamName, new DataStream.TimestampField("@timestamp"), - List.of(indexMetadata.getIndex()), 1, null, false, false, system, false); + DataStream dataStream = new DataStream( + dataStreamName, + new DataStream.TimestampField("@timestamp"), + List.of(indexMetadata.getIndex()), + 1, + null, + false, + false, + system, + false + ); ClusterState.Builder csBuilder = ClusterState.builder(new ClusterName("remote")) - .metadata(Metadata.builder() - .put(indexMetadata, true) - .put(dataStream) - .version(0L)); + .metadata(Metadata.builder().put(indexMetadata, true).put(dataStream).version(0L)); - ShardRouting shardRouting = - TestShardRouting.newShardRouting(dataStreamName, 0, "1", true, ShardRoutingState.INITIALIZING).moveToStarted(); + ShardRouting shardRouting = TestShardRouting.newShardRouting(dataStreamName, 0, "1", true, ShardRoutingState.INITIALIZING) + .moveToStarted(); IndexRoutingTable indexRoutingTable = IndexRoutingTable.builder(indexMetadata.getIndex()).addShard(shardRouting).build(); return csBuilder.routingTable(RoutingTable.builder().add(indexRoutingTable).build()).build(); } diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowStatsResponseTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowStatsResponseTests.java index b9794d9c2fcca..31cc73cb70226 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowStatsResponseTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowStatsResponseTests.java @@ -9,8 +9,8 @@ import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.test.AbstractWireSerializingTestCase; import org.elasticsearch.xpack.core.ccr.AutoFollowStats; -import org.elasticsearch.xpack.core.ccr.action.FollowStatsAction; import org.elasticsearch.xpack.core.ccr.action.CcrStatsAction; +import org.elasticsearch.xpack.core.ccr.action.FollowStatsAction; import static org.elasticsearch.xpack.ccr.action.AutoFollowStatsTests.randomReadExceptions; import static org.elasticsearch.xpack.ccr.action.AutoFollowStatsTests.randomTrackingClusters; diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowStatsTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowStatsTests.java index 4a855b05bc3f6..0ef7651e72f9e 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowStatsTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/AutoFollowStatsTests.java @@ -7,10 +7,10 @@ package org.elasticsearch.xpack.ccr.action; import org.elasticsearch.ElasticsearchException; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.core.Tuple; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ccr.AutoFollowStats; import org.elasticsearch.xpack.core.ccr.AutoFollowStats.AutoFollowedCluster; @@ -46,8 +46,10 @@ static NavigableMap> randomReadExcep final int count = randomIntBetween(0, 16); final NavigableMap> readExceptions = new TreeMap<>(); for (int i = 0; i < count; i++) { - readExceptions.put("" + i, Tuple.tuple(randomNonNegativeLong(), - new ElasticsearchException(new IllegalStateException("index [" + i + "]")))); + readExceptions.put( + "" + i, + Tuple.tuple(randomNonNegativeLong(), new ElasticsearchException(new IllegalStateException("index [" + i + "]"))) + ); } return readExceptions; } @@ -70,8 +72,10 @@ protected Writeable.Reader instanceReader() { protected void assertEqualInstances(AutoFollowStats expectedInstance, AutoFollowStats newInstance) { assertNotSame(expectedInstance, newInstance); - assertThat(newInstance.getNumberOfFailedRemoteClusterStateRequests(), - equalTo(expectedInstance.getNumberOfFailedRemoteClusterStateRequests())); + assertThat( + newInstance.getNumberOfFailedRemoteClusterStateRequests(), + equalTo(expectedInstance.getNumberOfFailedRemoteClusterStateRequests()) + ); assertThat(newInstance.getNumberOfFailedFollowIndices(), equalTo(expectedInstance.getNumberOfFailedFollowIndices())); assertThat(newInstance.getNumberOfSuccessfulFollowIndices(), equalTo(expectedInstance.getNumberOfSuccessfulFollowIndices())); @@ -85,7 +89,8 @@ protected void assertEqualInstances(AutoFollowStats expectedInstance, AutoFollow assertNotNull(entry.getValue().v2().getCause()); assertThat( entry.getValue().v2().getCause(), - anyOf(instanceOf(ElasticsearchException.class), instanceOf(IllegalStateException.class))); + anyOf(instanceOf(ElasticsearchException.class), instanceOf(IllegalStateException.class)) + ); assertThat(entry.getValue().v2().getCause().getMessage(), containsString(expected.v2().getCause().getMessage())); } diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/FollowInfoResponseTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/FollowInfoResponseTests.java index f068a68169a3e..66dfea290bdb4 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/FollowInfoResponseTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/FollowInfoResponseTests.java @@ -7,52 +7,49 @@ package org.elasticsearch.xpack.ccr.action; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xpack.core.ccr.action.FollowInfoAction; import org.elasticsearch.xpack.core.ccr.action.FollowInfoAction.Response.FollowerInfo; +import org.elasticsearch.xpack.core.ccr.action.FollowParameters; import java.io.IOException; import java.util.ArrayList; import java.util.List; import static org.elasticsearch.xpack.core.ccr.action.FollowInfoAction.Response.FOLLOWER_INDICES_FIELD; - -import org.elasticsearch.xpack.core.ccr.action.FollowParameters; import static org.elasticsearch.xpack.core.ccr.action.FollowInfoAction.Response.Status; public class FollowInfoResponseTests extends AbstractSerializingTestCase { - static final ConstructingObjectParser INFO_PARSER = new ConstructingObjectParser<>( - "info_parser", - args -> { - return new FollowerInfo( - (String) args[0], - (String) args[1], - (String) args[2], - Status.fromString((String) args[3]), - (FollowParameters) args[4] - ); - }); + static final ConstructingObjectParser INFO_PARSER = new ConstructingObjectParser<>("info_parser", args -> { + return new FollowerInfo( + (String) args[0], + (String) args[1], + (String) args[2], + Status.fromString((String) args[3]), + (FollowParameters) args[4] + ); + }); static { INFO_PARSER.declareString(ConstructingObjectParser.constructorArg(), FollowerInfo.FOLLOWER_INDEX_FIELD); INFO_PARSER.declareString(ConstructingObjectParser.constructorArg(), FollowerInfo.REMOTE_CLUSTER_FIELD); INFO_PARSER.declareString(ConstructingObjectParser.constructorArg(), FollowerInfo.LEADER_INDEX_FIELD); INFO_PARSER.declareString(ConstructingObjectParser.constructorArg(), FollowerInfo.STATUS_FIELD); - INFO_PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), FollowParametersTests.PARSER, - FollowerInfo.PARAMETERS_FIELD); + INFO_PARSER.declareObject( + ConstructingObjectParser.optionalConstructorArg(), + FollowParametersTests.PARSER, + FollowerInfo.PARAMETERS_FIELD + ); } @SuppressWarnings("unchecked") static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( "response", - args -> { - return new FollowInfoAction.Response( - (List) args[0] - ); - }); + args -> { return new FollowInfoAction.Response((List) args[0]); } + ); static { PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), INFO_PARSER, FOLLOWER_INDICES_FIELD); @@ -78,8 +75,15 @@ protected FollowInfoAction.Response createTestInstance() { followParameters = FollowParametersTests.randomInstance(); } - infos.add(new FollowerInfo(randomAlphaOfLength(4), randomAlphaOfLength(4), randomAlphaOfLength(4), - randomFrom(Status.values()), followParameters)); + infos.add( + new FollowerInfo( + randomAlphaOfLength(4), + randomAlphaOfLength(4), + randomAlphaOfLength(4), + randomFrom(Status.values()), + followParameters + ) + ); } return new FollowInfoAction.Response(infos); } diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/FollowParametersTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/FollowParametersTests.java index 63edf1d7b1247..fd92bc3ecff99 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/FollowParametersTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/FollowParametersTests.java @@ -9,9 +9,9 @@ import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xcontent.ObjectParser; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xpack.core.ccr.action.FollowParameters; import java.io.IOException; diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/GetAutoFollowPatternResponseTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/GetAutoFollowPatternResponseTests.java index 33cf841f2cd6e..1434492ca64f9 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/GetAutoFollowPatternResponseTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/GetAutoFollowPatternResponseTests.java @@ -48,7 +48,8 @@ protected GetAutoFollowPatternAction.Response createTestInstance() { randomIntBetween(0, Integer.MAX_VALUE), new ByteSizeValue(randomNonNegativeLong()), TimeValue.timeValueMillis(500), - TimeValue.timeValueMillis(500)); + TimeValue.timeValueMillis(500) + ); patterns.put(randomAlphaOfLength(4), autoFollowPattern); } return new GetAutoFollowPatternAction.Response(patterns); diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/PutAutoFollowPatternRequestTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/PutAutoFollowPatternRequestTests.java index 19e755ce59dbc..6898adf2d9624 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/PutAutoFollowPatternRequestTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/PutAutoFollowPatternRequestTests.java @@ -9,9 +9,9 @@ import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xpack.core.ccr.action.PutAutoFollowPatternAction; import java.io.IOException; diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/PutFollowActionRequestTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/PutFollowActionRequestTests.java index 88946553e10e8..50fe5ce87182e 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/PutFollowActionRequestTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/PutFollowActionRequestTests.java @@ -10,9 +10,9 @@ import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xpack.core.ccr.action.PutFollowAction; import java.io.IOException; @@ -28,8 +28,9 @@ protected Writeable.Reader instanceReader() { protected PutFollowAction.Request createTestInstance() { PutFollowAction.Request request = new PutFollowAction.Request(); request.setFollowerIndex(randomAlphaOfLength(4)); - request.waitForActiveShards(randomFrom(ActiveShardCount.DEFAULT, ActiveShardCount.NONE, ActiveShardCount.ONE, - ActiveShardCount.ALL)); + request.waitForActiveShards( + randomFrom(ActiveShardCount.DEFAULT, ActiveShardCount.NONE, ActiveShardCount.ONE, ActiveShardCount.ALL) + ); request.setRemoteCluster(randomAlphaOfLength(4)); request.setLeaderIndex(randomAlphaOfLength(4)); diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/ResumeFollowActionRequestTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/ResumeFollowActionRequestTests.java index cefbea1d3bbc0..3da767c9c49d0 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/ResumeFollowActionRequestTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/ResumeFollowActionRequestTests.java @@ -11,9 +11,9 @@ import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xpack.core.ccr.action.FollowParameters; import org.elasticsearch.xpack.core.ccr.action.ResumeFollowAction; diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/ShardChangesActionTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/ShardChangesActionTests.java index 4e6ff7e534972..785e3d65d48e7 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/ShardChangesActionTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/ShardChangesActionTests.java @@ -14,7 +14,6 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexNotFoundException; import org.elasticsearch.index.IndexService; @@ -24,6 +23,7 @@ import org.elasticsearch.index.shard.ShardNotFoundException; import org.elasticsearch.index.translog.Translog; import org.elasticsearch.test.ESSingleNodeTestCase; +import org.elasticsearch.xcontent.XContentType; import org.mockito.Mockito; import java.util.Arrays; @@ -47,10 +47,7 @@ protected boolean resetNodeAfterTest() { } public void testGetOperations() throws Exception { - final Settings settings = Settings.builder() - .put("index.number_of_shards", 1) - .put("index.number_of_replicas", 0) - .build(); + final Settings settings = Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0).build(); final IndexService indexService = createIndex("index", settings); final int numWrites = randomIntBetween(10, 4096); @@ -66,12 +63,13 @@ public void testGetOperations() throws Exception { int max = randomIntBetween(min, numWrites - 1); int size = max - min + 1; final Translog.Operation[] operations = ShardChangesAction.getOperations( - indexShard, - indexShard.getLastKnownGlobalCheckpoint(), - min, - size, - indexShard.getHistoryUUID(), - new ByteSizeValue(Long.MAX_VALUE, ByteSizeUnit.BYTES)); + indexShard, + indexShard.getLastKnownGlobalCheckpoint(), + min, + size, + indexShard.getHistoryUUID(), + new ByteSizeValue(Long.MAX_VALUE, ByteSizeUnit.BYTES) + ); final List seenSeqNos = Arrays.stream(operations).map(Translog.Operation::seqNo).collect(Collectors.toList()); final List expectedSeqNos = LongStream.rangeClosed(min, max).boxed().collect(Collectors.toList()); assertThat(seenSeqNos, equalTo(expectedSeqNos)); @@ -80,43 +78,72 @@ public void testGetOperations() throws Exception { { // get operations for a range for which no operations exist final IllegalStateException e = expectThrows( - IllegalStateException.class, - () -> ShardChangesAction.getOperations( - indexShard, - indexShard.getLastKnownGlobalCheckpoint(), - numWrites, - numWrites + 1, - indexShard.getHistoryUUID(), - new ByteSizeValue(Long.MAX_VALUE, ByteSizeUnit.BYTES))); - final String message = String.format( - Locale.ROOT, - "not exposing operations from [%d] greater than the global checkpoint [%d]", + IllegalStateException.class, + () -> ShardChangesAction.getOperations( + indexShard, + indexShard.getLastKnownGlobalCheckpoint(), numWrites, - indexShard.getLastKnownGlobalCheckpoint()); + numWrites + 1, + indexShard.getHistoryUUID(), + new ByteSizeValue(Long.MAX_VALUE, ByteSizeUnit.BYTES) + ) + ); + final String message = String.format( + Locale.ROOT, + "not exposing operations from [%d] greater than the global checkpoint [%d]", + numWrites, + indexShard.getLastKnownGlobalCheckpoint() + ); assertThat(e, hasToString(containsString(message))); } // get operations for a range some operations do not exist: - Translog.Operation[] operations = ShardChangesAction.getOperations(indexShard, indexShard.getLastKnownGlobalCheckpoint(), - numWrites - 10, numWrites + 10, indexShard.getHistoryUUID(), new ByteSizeValue(Long.MAX_VALUE, ByteSizeUnit.BYTES)); + Translog.Operation[] operations = ShardChangesAction.getOperations( + indexShard, + indexShard.getLastKnownGlobalCheckpoint(), + numWrites - 10, + numWrites + 10, + indexShard.getHistoryUUID(), + new ByteSizeValue(Long.MAX_VALUE, ByteSizeUnit.BYTES) + ); assertThat(operations.length, equalTo(10)); // Unexpected history UUID: - Exception e = expectThrows(IllegalStateException.class, () -> ShardChangesAction.getOperations(indexShard, - indexShard.getLastKnownGlobalCheckpoint(), 0, 10, "different-history-uuid", - new ByteSizeValue(Long.MAX_VALUE, ByteSizeUnit.BYTES))); - assertThat(e.getMessage(), equalTo("unexpected history uuid, expected [different-history-uuid], actual [" + - indexShard.getHistoryUUID() + "]")); + Exception e = expectThrows( + IllegalStateException.class, + () -> ShardChangesAction.getOperations( + indexShard, + indexShard.getLastKnownGlobalCheckpoint(), + 0, + 10, + "different-history-uuid", + new ByteSizeValue(Long.MAX_VALUE, ByteSizeUnit.BYTES) + ) + ); + assertThat( + e.getMessage(), + equalTo("unexpected history uuid, expected [different-history-uuid], actual [" + indexShard.getHistoryUUID() + "]") + ); // invalid range { final long fromSeqNo = randomLongBetween(Long.MIN_VALUE, -1); final int batchSize = randomIntBetween(0, Integer.MAX_VALUE); - final IllegalArgumentException invalidRangeError = expectThrows(IllegalArgumentException.class, - () -> ShardChangesAction.getOperations(indexShard, indexShard.getLastKnownGlobalCheckpoint(), - fromSeqNo, batchSize, indexShard.getHistoryUUID(), new ByteSizeValue(Long.MAX_VALUE, ByteSizeUnit.BYTES))); - assertThat(invalidRangeError.getMessage(), - equalTo("Invalid range; from_seqno [" + fromSeqNo + "], to_seqno [" + (fromSeqNo + batchSize - 1) + "]")); + final IllegalArgumentException invalidRangeError = expectThrows( + IllegalArgumentException.class, + () -> ShardChangesAction.getOperations( + indexShard, + indexShard.getLastKnownGlobalCheckpoint(), + fromSeqNo, + batchSize, + indexShard.getHistoryUUID(), + new ByteSizeValue(Long.MAX_VALUE, ByteSizeUnit.BYTES) + ) + ); + assertThat( + invalidRangeError.getMessage(), + equalTo("Invalid range; from_seqno [" + fromSeqNo + "], to_seqno [" + (fromSeqNo + batchSize - 1) + "]") + ); } } @@ -125,16 +152,21 @@ public void testGetOperationsWhenShardNotStarted() throws Exception { ShardRouting shardRouting = TestShardRouting.newShardRouting("index", 0, "_node_id", true, ShardRoutingState.INITIALIZING); Mockito.when(indexShard.routingEntry()).thenReturn(shardRouting); - expectThrows(IndexShardNotStartedException.class, () -> ShardChangesAction.getOperations(indexShard, - indexShard.getLastKnownGlobalCheckpoint(), 0, 1, indexShard.getHistoryUUID(), - new ByteSizeValue(Long.MAX_VALUE, ByteSizeUnit.BYTES))); + expectThrows( + IndexShardNotStartedException.class, + () -> ShardChangesAction.getOperations( + indexShard, + indexShard.getLastKnownGlobalCheckpoint(), + 0, + 1, + indexShard.getHistoryUUID(), + new ByteSizeValue(Long.MAX_VALUE, ByteSizeUnit.BYTES) + ) + ); } public void testGetOperationsExceedByteLimit() throws Exception { - final Settings settings = Settings.builder() - .put("index.number_of_shards", 1) - .put("index.number_of_replicas", 0) - .build(); + final Settings settings = Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0).build(); final IndexService indexService = createIndex("index", settings); final long numWrites = 32; @@ -143,8 +175,14 @@ public void testGetOperationsExceedByteLimit() throws Exception { } final IndexShard indexShard = indexService.getShard(0); - final Translog.Operation[] operations = ShardChangesAction.getOperations(indexShard, indexShard.getLastKnownGlobalCheckpoint(), - 0, randomIntBetween(100, 500), indexShard.getHistoryUUID(), new ByteSizeValue(256, ByteSizeUnit.BYTES)); + final Translog.Operation[] operations = ShardChangesAction.getOperations( + indexShard, + indexShard.getLastKnownGlobalCheckpoint(), + 0, + randomIntBetween(100, 500), + indexShard.getHistoryUUID(), + new ByteSizeValue(256, ByteSizeUnit.BYTES) + ); assertThat(operations.length, equalTo(8)); assertThat(operations[0].seqNo(), equalTo(0L)); assertThat(operations[1].seqNo(), equalTo(1L)); @@ -157,18 +195,20 @@ public void testGetOperationsExceedByteLimit() throws Exception { } public void testGetOperationsAlwaysReturnAtLeastOneOp() throws Exception { - final Settings settings = Settings.builder() - .put("index.number_of_shards", 1) - .put("index.number_of_replicas", 0) - .build(); + final Settings settings = Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0).build(); final IndexService indexService = createIndex("index", settings); client().prepareIndex("index").setId("0").setSource("{}", XContentType.JSON).get(); final IndexShard indexShard = indexService.getShard(0); - final Translog.Operation[] operations = - ShardChangesAction.getOperations( - indexShard, indexShard.getLastKnownGlobalCheckpoint(), 0, 1, indexShard.getHistoryUUID(), ByteSizeValue.ZERO); + final Translog.Operation[] operations = ShardChangesAction.getOperations( + indexShard, + indexShard.getLastKnownGlobalCheckpoint(), + 0, + 1, + indexShard.getHistoryUUID(), + ByteSizeValue.ZERO + ); assertThat(operations.length, equalTo(1)); assertThat(operations[0].seqNo(), equalTo(0L)); } @@ -177,20 +217,23 @@ public void testIndexNotFound() throws InterruptedException { final CountDownLatch latch = new CountDownLatch(1); final AtomicReference reference = new AtomicReference<>(); final ShardChangesAction.TransportAction transportAction = node().injector().getInstance(ShardChangesAction.TransportAction.class); - ActionTestUtils.execute(transportAction, null, - new ShardChangesAction.Request(new ShardId(new Index("non-existent", "uuid"), 0), "uuid"), - new ActionListener() { - @Override - public void onResponse(final ShardChangesAction.Response response) { - fail(); - } - - @Override - public void onFailure(final Exception e) { - reference.set(e); - latch.countDown(); - } - }); + ActionTestUtils.execute( + transportAction, + null, + new ShardChangesAction.Request(new ShardId(new Index("non-existent", "uuid"), 0), "uuid"), + new ActionListener() { + @Override + public void onResponse(final ShardChangesAction.Response response) { + fail(); + } + + @Override + public void onFailure(final Exception e) { + reference.set(e); + latch.countDown(); + } + } + ); latch.await(); assertNotNull(reference.get()); assertThat(reference.get(), instanceOf(IndexNotFoundException.class)); @@ -202,20 +245,23 @@ public void testShardNotFound() throws InterruptedException { final CountDownLatch latch = new CountDownLatch(1); final AtomicReference reference = new AtomicReference<>(); final ShardChangesAction.TransportAction transportAction = node().injector().getInstance(ShardChangesAction.TransportAction.class); - ActionTestUtils.execute(transportAction, null, - new ShardChangesAction.Request(new ShardId(indexService.getMetadata().getIndex(), numberOfShards), "uuid"), - new ActionListener() { - @Override - public void onResponse(final ShardChangesAction.Response response) { - fail(); - } - - @Override - public void onFailure(final Exception e) { - reference.set(e); - latch.countDown(); - } - }); + ActionTestUtils.execute( + transportAction, + null, + new ShardChangesAction.Request(new ShardId(indexService.getMetadata().getIndex(), numberOfShards), "uuid"), + new ActionListener() { + @Override + public void onResponse(final ShardChangesAction.Response response) { + fail(); + } + + @Override + public void onFailure(final Exception e) { + reference.set(e); + latch.countDown(); + } + } + ); latch.await(); assertNotNull(reference.get()); assertThat(reference.get(), instanceOf(ShardNotFoundException.class)); diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/ShardChangesRequestTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/ShardChangesRequestTests.java index ecf1869d5a25e..cb52e3870690a 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/ShardChangesRequestTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/ShardChangesRequestTests.java @@ -17,8 +17,7 @@ public class ShardChangesRequestTests extends AbstractWireSerializingTestCase retentionLease.retainingSequenceNumber() == maxSeqNo + 1)); + assertTrue( + shardStats.getRetentionLeaseStats() + .retentionLeases() + .leases() + .stream() + .allMatch(retentionLease -> retentionLease.retainingSequenceNumber() == maxSeqNo + 1) + ); } }); @@ -129,22 +138,36 @@ public void testMissingOperations() throws Exception { forceMergeRequest.maxNumSegments(1); client().admin().indices().forceMerge(forceMergeRequest).actionGet(); - client().admin().indices().execute(RetentionLeaseActions.Add.INSTANCE, new RetentionLeaseActions.AddRequest( - new ShardId(resolveIndex("index"), 0), "test", RetentionLeaseActions.RETAIN_ALL, "ccr")).get(); + client().admin() + .indices() + .execute( + RetentionLeaseActions.Add.INSTANCE, + new RetentionLeaseActions.AddRequest(new ShardId(resolveIndex("index"), 0), "test", RetentionLeaseActions.RETAIN_ALL, "ccr") + ) + .get(); ShardStats shardStats = client().admin().indices().prepareStats("index").get().getIndex("index").getShards()[0]; String historyUUID = shardStats.getCommitStats().getUserData().get(Engine.HISTORY_UUID_KEY); Collection retentionLeases = shardStats.getRetentionLeaseStats().retentionLeases().leases(); - ShardChangesAction.Request request = new ShardChangesAction.Request(shardStats.getShardRouting().shardId(), historyUUID); + ShardChangesAction.Request request = new ShardChangesAction.Request(shardStats.getShardRouting().shardId(), historyUUID); request.setFromSeqNo(0L); request.setMaxOperationCount(1); { - ResourceNotFoundException e = - expectThrows(ResourceNotFoundException.class, () -> client().execute(ShardChangesAction.INSTANCE, request).actionGet()); - assertThat(e.getMessage(), equalTo("Operations are no longer available for replicating. " + - "Existing retention leases [" + retentionLeases + "]; maybe increase the retention lease period setting " + - "[index.soft_deletes.retention_lease.period]?")); + ResourceNotFoundException e = expectThrows( + ResourceNotFoundException.class, + () -> client().execute(ShardChangesAction.INSTANCE, request).actionGet() + ); + assertThat( + e.getMessage(), + equalTo( + "Operations are no longer available for replicating. " + + "Existing retention leases [" + + retentionLeases + + "]; maybe increase the retention lease period setting " + + "[index.soft_deletes.retention_lease.period]?" + ) + ); assertThat(e.getMetadataKeys().size(), equalTo(1)); assertThat(e.getMetadata(Ccr.REQUESTED_OPS_MISSING_METADATA_KEY), notNullValue()); @@ -153,8 +176,11 @@ public void testMissingOperations() throws Exception { { AtomicReference holder = new AtomicReference<>(); CountDownLatch latch = new CountDownLatch(1); - client().execute(ShardChangesAction.INSTANCE, request, - new LatchedActionListener<>(ActionListener.wrap(r -> fail("expected an exception"), holder::set), latch)); + client().execute( + ShardChangesAction.INSTANCE, + request, + new LatchedActionListener<>(ActionListener.wrap(r -> fail("expected an exception"), holder::set), latch) + ); latch.await(); ElasticsearchException e = (ElasticsearchException) holder.get(); @@ -162,9 +188,16 @@ public void testMissingOperations() throws Exception { assertThat(e.getMetadataKeys().size(), equalTo(0)); ResourceNotFoundException cause = (ResourceNotFoundException) e.getCause(); - assertThat(cause.getMessage(), equalTo("Operations are no longer available for replicating. " + - "Existing retention leases [" + retentionLeases + "]; maybe increase the retention lease period setting " + - "[index.soft_deletes.retention_lease.period]?")); + assertThat( + cause.getMessage(), + equalTo( + "Operations are no longer available for replicating. " + + "Existing retention leases [" + + retentionLeases + + "]; maybe increase the retention lease period setting " + + "[index.soft_deletes.retention_lease.period]?" + ) + ); assertThat(cause.getMetadataKeys().size(), equalTo(1)); assertThat(cause.getMetadata(Ccr.REQUESTED_OPS_MISSING_METADATA_KEY), notNullValue()); assertThat(cause.getMetadata(Ccr.REQUESTED_OPS_MISSING_METADATA_KEY), contains("0", "0")); diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/ShardFollowNodeTaskRandomTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/ShardFollowNodeTaskRandomTests.java index fe70eb52cf4ef..b085c6098dd0b 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/ShardFollowNodeTaskRandomTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/ShardFollowNodeTaskRandomTests.java @@ -12,8 +12,8 @@ import org.elasticsearch.common.breaker.CircuitBreakingException; import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.seqno.LocalCheckpointTracker; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.index.translog.Translog; @@ -49,11 +49,12 @@ public class ShardFollowNodeTaskRandomTests extends ESTestCase { public void testSingleReaderWriter() throws Exception { TestRun testRun = createTestRun( - randomNonNegativeLong(), - randomNonNegativeLong(), - randomNonNegativeLong(), - randomNonNegativeLong(), - randomIntBetween(1, 2048)); + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomIntBetween(1, 2048) + ); ShardFollowNodeTask task = createShardFollowTask(1, testRun); startAndAssertAndStopTask(task, testRun); } @@ -71,8 +72,11 @@ private void startAndAssertAndStopTask(ShardFollowNodeTask task, TestRun testRun ShardFollowNodeTaskStatus status = task.getStatus(); assertThat(status.leaderGlobalCheckpoint(), equalTo(testRun.finalExpectedGlobalCheckpoint)); assertThat(status.followerGlobalCheckpoint(), equalTo(testRun.finalExpectedGlobalCheckpoint)); - final long numberOfFailedFetches = - testRun.responses.values().stream().flatMap(List::stream).filter(f -> f.exception != null).count(); + final long numberOfFailedFetches = testRun.responses.values() + .stream() + .flatMap(List::stream) + .filter(f -> f.exception != null) + .count(); assertThat(status.failedReadRequests(), equalTo(numberOfFailedFetches)); // the failures were able to be retried so fetch failures should have cleared assertThat(status.readExceptions().entrySet(), hasSize(0)); @@ -116,7 +120,16 @@ private ShardFollowNodeTask createShardFollowTask(int concurrency, TestRun testR List receivedOperations = Collections.synchronizedList(new ArrayList<>()); LocalCheckpointTracker tracker = new LocalCheckpointTracker(testRun.startSeqNo - 1, testRun.startSeqNo - 1); return new ShardFollowNodeTask( - 1L, "type", ShardFollowTask.NAME, "description", null, Collections.emptyMap(), params, scheduler, System::nanoTime) { + 1L, + "type", + ShardFollowTask.NAME, + "description", + null, + Collections.emptyMap(), + params, + scheduler, + System::nanoTime + ) { private volatile long mappingVersion = 0L; private volatile long settingsVersion = 0L; @@ -140,11 +153,13 @@ protected void innerUpdateAliases(LongConsumer handler, Consumer erro @Override protected void innerSendBulkShardOperationsRequest( - String followerHistoryUUID, List operations, + String followerHistoryUUID, + List operations, long maxSeqNoOfUpdates, Consumer handler, - Consumer errorHandler) { - for(Translog.Operation op : operations) { + Consumer errorHandler + ) { + for (Translog.Operation op : operations) { tracker.markSeqNoAsProcessed(op.seqNo()); } receivedOperations.addAll(operations); @@ -157,8 +172,12 @@ protected void innerSendBulkShardOperationsRequest( } @Override - protected void innerSendShardChangesRequest(long from, int maxOperationCount, Consumer handler, - Consumer errorHandler) { + protected void innerSendShardChangesRequest( + long from, + int maxOperationCount, + Consumer handler, + Consumer errorHandler + ) { // Emulate network thread and avoid SO: Runnable task = () -> { @@ -188,7 +207,8 @@ protected void innerSendShardChangesRequest(long from, int maxOperationCount, Co assert from >= testRun.finalExpectedGlobalCheckpoint; final long globalCheckpoint = tracker.getProcessedCheckpoint(); final long maxSeqNo = tracker.getMaxSeqNo(); - handler.accept(new ShardChangesAction.Response( + handler.accept( + new ShardChangesAction.Response( 0L, 0L, 0L, @@ -196,7 +216,9 @@ protected void innerSendShardChangesRequest(long from, int maxOperationCount, Co maxSeqNo, randomNonNegativeLong(), new Translog.Operation[0], - 1L)); + 1L + ) + ); } }; threadPool.generic().execute(task); @@ -238,7 +260,8 @@ public void markAsFailed(Exception e) { private void tearDown() { threadPool.shutdown(); - List expectedOperations = testRun.responses.values().stream() + List expectedOperations = testRun.responses.values() + .stream() .flatMap(List::stream) .map(testResponse -> testResponse.response) .filter(Objects::nonNull) @@ -257,11 +280,12 @@ private void tearDown() { } private static TestRun createTestRun( - final long startSeqNo, - final long startMappingVersion, - final long startSettingsVersion, - final long startAliasesVersion, - final int maxOperationCount) { + final long startSeqNo, + final long startMappingVersion, + final long startSettingsVersion, + final long startAliasesVersion, + final int maxOperationCount + ) { long prevGlobalCheckpoint = startSeqNo; long mappingVersion = startMappingVersion; long settingsVersion = startSettingsVersion; @@ -292,19 +316,21 @@ private static TestRun createTestRun( byte[] source = "{}".getBytes(StandardCharsets.UTF_8); ops.add(new Translog.Index(id, seqNo, 0, source)); } - item.add(new TestResponse( - null, - mappingVersion, - settingsVersion, - new ShardChangesAction.Response( + item.add( + new TestResponse( + null, mappingVersion, settingsVersion, - aliasesVersion, - nextGlobalCheckPoint, - nextGlobalCheckPoint, - randomNonNegativeLong(), - ops.toArray(EMPTY), - randomNonNegativeLong()) + new ShardChangesAction.Response( + mappingVersion, + settingsVersion, + aliasesVersion, + nextGlobalCheckPoint, + nextGlobalCheckPoint, + randomNonNegativeLong(), + ops.toArray(EMPTY), + randomNonNegativeLong() + ) ) ); responses.put(prevGlobalCheckpoint, item); @@ -321,7 +347,8 @@ private static TestRun createTestRun( new UnavailableShardsException(new ShardId("test", "test", 0), ""), new NoSeedNodeLeftException("cluster_a"), new CircuitBreakingException("test", randomInt(), randomInt(), randomFrom(CircuitBreaker.Durability.values())), - new EsRejectedExecutionException("test")); + new EsRejectedExecutionException("test") + ); item.add(new TestResponse(error, mappingVersion, settingsVersion, null)); } // Sometimes add an empty shard changes response to also simulate a leader shard lagging behind @@ -362,8 +389,7 @@ private static TestRun createTestRun( } prevGlobalCheckpoint = nextGlobalCheckPoint + 1; } - return new TestRun(maxOperationCount, startSeqNo, startMappingVersion, mappingVersion, - prevGlobalCheckpoint - 1, responses); + return new TestRun(maxOperationCount, startSeqNo, startMappingVersion, mappingVersion, prevGlobalCheckpoint - 1, responses); } // Instead of rarely(), which returns true very rarely especially not running in nightly mode or a multiplier have not been set @@ -381,8 +407,14 @@ private static class TestRun { final long finalExpectedGlobalCheckpoint; final Map> responses; - private TestRun(int maxOperationCount, long startSeqNo, long startMappingVersion, long finalMappingVersion, - long finalExpectedGlobalCheckpoint, Map> responses) { + private TestRun( + int maxOperationCount, + long startSeqNo, + long startMappingVersion, + long finalMappingVersion, + long finalExpectedGlobalCheckpoint, + Map> responses + ) { this.maxOperationCount = maxOperationCount; this.startSeqNo = startSeqNo; this.startMappingVersion = startMappingVersion; diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/ShardFollowNodeTaskStatusTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/ShardFollowNodeTaskStatusTests.java index db79c46a2e36b..ebcf6db26448f 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/ShardFollowNodeTaskStatusTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/ShardFollowNodeTaskStatusTests.java @@ -8,10 +8,10 @@ package org.elasticsearch.xpack.ccr.action; import org.elasticsearch.ElasticsearchException; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.core.Tuple; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ccr.ShardFollowNodeTaskStatus; import java.io.IOException; @@ -35,35 +35,36 @@ protected ShardFollowNodeTaskStatus doParseInstance(XContentParser parser) throw protected ShardFollowNodeTaskStatus createTestInstance() { // if you change this constructor, reflect the changes in the hand-written assertions below return new ShardFollowNodeTaskStatus( - randomAlphaOfLength(4), - randomAlphaOfLength(4), - randomAlphaOfLength(4), - randomInt(), - randomNonNegativeLong(), - randomNonNegativeLong(), - randomNonNegativeLong(), - randomNonNegativeLong(), - randomNonNegativeLong(), - randomIntBetween(0, Integer.MAX_VALUE), - randomIntBetween(0, Integer.MAX_VALUE), - randomIntBetween(0, Integer.MAX_VALUE), - randomNonNegativeLong(), - randomNonNegativeLong(), - randomNonNegativeLong(), - randomNonNegativeLong(), - randomNonNegativeLong(), - randomNonNegativeLong(), - randomNonNegativeLong(), - randomNonNegativeLong(), - randomNonNegativeLong(), - randomNonNegativeLong(), - randomNonNegativeLong(), - randomNonNegativeLong(), - randomNonNegativeLong(), - randomNonNegativeLong(), - randomReadExceptions(), - randomNonNegativeLong(), - randomBoolean() ? new ElasticsearchException("fatal error") : null); + randomAlphaOfLength(4), + randomAlphaOfLength(4), + randomAlphaOfLength(4), + randomInt(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomIntBetween(0, Integer.MAX_VALUE), + randomIntBetween(0, Integer.MAX_VALUE), + randomIntBetween(0, Integer.MAX_VALUE), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomReadExceptions(), + randomNonNegativeLong(), + randomBoolean() ? new ElasticsearchException("fatal error") : null + ); } @Override @@ -102,8 +103,9 @@ protected void assertEqualInstances(final ShardFollowNodeTaskStatus expectedInst assertThat(entry.getValue().v2().getMessage(), containsString(expected.getMessage())); assertNotNull(entry.getValue().v2().getCause()); assertThat( - entry.getValue().v2().getCause(), - anyOf(instanceOf(ElasticsearchException.class), instanceOf(IllegalStateException.class))); + entry.getValue().v2().getCause(), + anyOf(instanceOf(ElasticsearchException.class), instanceOf(IllegalStateException.class)) + ); assertThat(entry.getValue().v2().getCause().getMessage(), containsString(expected.getCause().getMessage())); } assertThat(newInstance.timeSinceLastReadMillis(), equalTo(expectedInstance.timeSinceLastReadMillis())); @@ -119,10 +121,12 @@ private NavigableMap> randomReadExc final NavigableMap> readExceptions = new TreeMap<>(); for (int i = 0; i < count; i++) { readExceptions.put( - randomNonNegativeLong(), - Tuple.tuple( - randomIntBetween(0, Integer.MAX_VALUE), - new ElasticsearchException(new IllegalStateException("index [" + i + "]")))); + randomNonNegativeLong(), + Tuple.tuple( + randomIntBetween(0, Integer.MAX_VALUE), + new ElasticsearchException(new IllegalStateException("index [" + i + "]")) + ) + ); } return readExceptions; } diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/ShardFollowNodeTaskTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/ShardFollowNodeTaskTests.java index 7e1f3fd1eb72a..47e40b126d044 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/ShardFollowNodeTaskTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/ShardFollowNodeTaskTests.java @@ -9,12 +9,12 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.action.ActionListener; import org.elasticsearch.common.UUIDs; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException; +import org.elasticsearch.core.TimeValue; +import org.elasticsearch.core.Tuple; import org.elasticsearch.index.seqno.SequenceNumbers; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.index.shard.ShardNotFoundException; @@ -96,12 +96,13 @@ public void testCoordinateReads() { ShardFollowNodeTask task = createShardFollowTask(params); startTask(task, 3, -1); task.coordinateReads(); - assertThat(shardChangesRequests, contains(new long[]{0L, 8L})); // treat this a peak request + assertThat(shardChangesRequests, contains(new long[] { 0L, 8L })); // treat this a peak request shardChangesRequests.clear(); task.innerHandleReadResponse(0, 5L, generateShardChangesResponse(0, 5L, 0L, 0L, 1L, 60L)); - assertThat(shardChangesRequests, contains(new long[][]{ - {6L, 8L}, {14L, 8L}, {22L, 8L}, {30L, 8L}, {38L, 8L}, {46L, 8L}, {54L, 7L}} - )); + assertThat( + shardChangesRequests, + contains(new long[][] { { 6L, 8L }, { 14L, 8L }, { 22L, 8L }, { 30L, 8L }, { 38L, 8L }, { 46L, 8L }, { 54L, 7L } }) + ); ShardFollowNodeTaskStatus status = task.getStatus(); assertThat(status.outstandingReadRequests(), equalTo(7)); assertThat(status.lastRequestedSeqNo(), equalTo(60L)); @@ -297,8 +298,9 @@ public void testReceiveRetryableError() { assertThat(shardNotFoundException.getShardId().getId(), equalTo(0)); } else { assertThat(entry.getValue().v2().getCause(), instanceOf(EsRejectedExecutionException.class)); - final EsRejectedExecutionException rejectedExecutionException = - (EsRejectedExecutionException) entry.getValue().v2().getCause(); + final EsRejectedExecutionException rejectedExecutionException = (EsRejectedExecutionException) entry.getValue() + .v2() + .getCause(); assertThat(rejectedExecutionException.getMessage(), equalTo("leader_index rejected")); } } @@ -317,7 +319,7 @@ public void testReceiveRetryableError() { ShardFollowNodeTaskStatus status = task.getStatus(); assertThat(status.outstandingReadRequests(), equalTo(1)); assertThat(status.outstandingWriteRequests(), equalTo(0)); - assertThat(status.failedReadRequests(), equalTo((long)max)); + assertThat(status.failedReadRequests(), equalTo((long) max)); assertThat(status.successfulReadRequests(), equalTo(1L)); // the fetch failure has cleared assertThat(status.readExceptions().entrySet(), hasSize(0)); @@ -1155,7 +1157,16 @@ public void testUpdateMappingSettingsAndAliasesConcurrently() throws Exception { final Phaser updates = new Phaser(1); final ShardFollowNodeTask shardFollowNodeTask = new ShardFollowNodeTask( - 1L, "type", ShardFollowTask.NAME, "description", null, Collections.emptyMap(), followTask, scheduler, System::nanoTime) { + 1L, + "type", + ShardFollowTask.NAME, + "description", + null, + Collections.emptyMap(), + followTask, + scheduler, + System::nanoTime + ) { @Override protected void innerUpdateMapping(long minRequiredMappingVersion, LongConsumer handler, Consumer errorHandler) { updates.register(); @@ -1190,18 +1201,23 @@ protected void innerUpdateAliases(LongConsumer handler, Consumer erro } @Override - protected void innerSendBulkShardOperationsRequest(String followerHistoryUUID, - List operations, - long leaderMaxSeqNoOfUpdatesOrDeletes, - Consumer handler, - Consumer errorHandler) { + protected void innerSendBulkShardOperationsRequest( + String followerHistoryUUID, + List operations, + long leaderMaxSeqNoOfUpdatesOrDeletes, + Consumer handler, + Consumer errorHandler + ) { } @Override - protected void innerSendShardChangesRequest(long from, int maxOperationCount, - Consumer handler, - Consumer errorHandler) { + protected void innerSendShardChangesRequest( + long from, + int maxOperationCount, + Consumer handler, + Consumer errorHandler + ) { } @@ -1292,7 +1308,16 @@ private ShardFollowNodeTask createShardFollowTask(ShardFollowTaskParams params) responseSizes = new LinkedList<>(); pendingBulkShardRequests = new LinkedList<>(); return new ShardFollowNodeTask( - 1L, "type", ShardFollowTask.NAME, "description", null, Collections.emptyMap(), followTask, scheduler, System::nanoTime) { + 1L, + "type", + ShardFollowTask.NAME, + "description", + null, + Collections.emptyMap(), + followTask, + scheduler, + System::nanoTime + ) { @Override protected void innerUpdateMapping(long minRequiredMappingVersion, LongConsumer handler, Consumer errorHandler) { @@ -1338,10 +1363,12 @@ protected void innerUpdateAliases(final LongConsumer handler, final Consumer operations, + String followerHistoryUUID, + final List operations, final long maxSeqNoOfUpdates, final Consumer handler, - final Consumer errorHandler) { + final Consumer errorHandler + ) { bulkShardOperationRequests.add(operations); Exception writeFailure = ShardFollowNodeTaskTests.this.writeFailures.poll(); if (writeFailure != null) { @@ -1360,10 +1387,14 @@ protected void innerSendBulkShardOperationsRequest( } @Override - protected void innerSendShardChangesRequest(long from, int requestBatchSize, Consumer handler, - Consumer errorHandler) { + protected void innerSendShardChangesRequest( + long from, + int requestBatchSize, + Consumer handler, + Consumer errorHandler + ) { beforeSendShardChangesRequest.accept(getStatus()); - shardChangesRequests.add(new long[]{from, requestBatchSize}); + shardChangesRequests.add(new long[] { from, requestBatchSize }); Exception readFailure = ShardFollowNodeTaskTests.this.readFailures.poll(); if (readFailure != null) { errorHandler.accept(readFailure); @@ -1392,10 +1423,11 @@ protected Scheduler.Cancellable scheduleBackgroundRetentionLeaseRenewal(final Lo if (scheduleRetentionLeaseRenewal.get()) { final ScheduledThreadPoolExecutor scheduler = Scheduler.initScheduler(Settings.EMPTY, "test-scheduler"); final ScheduledFuture future = scheduler.scheduleWithFixedDelay( - () -> retentionLeaseRenewal.accept(followerGlobalCheckpoint.getAsLong()), - 0, - TimeValue.timeValueMillis(200).millis(), - TimeUnit.MILLISECONDS); + () -> retentionLeaseRenewal.accept(followerGlobalCheckpoint.getAsLong()), + 0, + TimeValue.timeValueMillis(200).millis(), + TimeUnit.MILLISECONDS + ); return new Scheduler.Cancellable() { @Override @@ -1440,12 +1472,14 @@ public void markAsCompleted() { }; } - private static ShardChangesAction.Response generateShardChangesResponse(long fromSeqNo, - long toSeqNo, - long mappingVersion, - long settingsVersion, - long aliasesVersion, - long leaderGlobalCheckPoint) { + private static ShardChangesAction.Response generateShardChangesResponse( + long fromSeqNo, + long toSeqNo, + long mappingVersion, + long settingsVersion, + long aliasesVersion, + long leaderGlobalCheckPoint + ) { List ops = new ArrayList<>(); for (long seqNo = fromSeqNo; seqNo <= toSeqNo; seqNo++) { String id = UUIDs.randomBase64UUID(); @@ -1469,5 +1503,4 @@ void startTask(ShardFollowNodeTask task, long leaderGlobalCheckpoint, long follo task.start("uuid", leaderGlobalCheckpoint, leaderGlobalCheckpoint, followerGlobalCheckpoint, followerGlobalCheckpoint); } - } diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/ShardFollowTaskReplicationTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/ShardFollowTaskReplicationTests.java index 411a9b1aeb20f..b188b1663994e 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/ShardFollowTaskReplicationTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/ShardFollowTaskReplicationTests.java @@ -104,14 +104,17 @@ public void testSimpleCcrReplication() throws Exception { leaderSeqNoStats.getGlobalCheckpoint(), leaderSeqNoStats.getMaxSeqNo(), followerSeqNoStats.getGlobalCheckpoint(), - followerSeqNoStats.getMaxSeqNo()); + followerSeqNoStats.getMaxSeqNo() + ); docCount += leaderGroup.appendDocs(randomInt(128)); leaderGroup.syncGlobalCheckpoint(); leaderGroup.assertAllEqual(docCount); Set indexedDocIds = getShardDocUIDs(leaderGroup.getPrimary()); assertBusy(() -> { - assertThat(followerGroup.getPrimary().getLastKnownGlobalCheckpoint(), - equalTo(leaderGroup.getPrimary().getLastKnownGlobalCheckpoint())); + assertThat( + followerGroup.getPrimary().getLastKnownGlobalCheckpoint(), + equalTo(leaderGroup.getPrimary().getLastKnownGlobalCheckpoint()) + ); followerGroup.assertAllEqual(indexedDocIds.size()); }); for (IndexShard shard : followerGroup) { @@ -125,8 +128,10 @@ public void testSimpleCcrReplication() throws Exception { } leaderGroup.syncGlobalCheckpoint(); assertBusy(() -> { - assertThat(followerGroup.getPrimary().getLastKnownGlobalCheckpoint(), - equalTo(leaderGroup.getPrimary().getLastKnownGlobalCheckpoint())); + assertThat( + followerGroup.getPrimary().getLastKnownGlobalCheckpoint(), + equalTo(leaderGroup.getPrimary().getLastKnownGlobalCheckpoint()) + ); followerGroup.assertAllEqual(indexedDocIds.size() - deleteDocIds.size()); }); assertNull(shardFollowTask.getStatus().getFatalException()); @@ -149,7 +154,8 @@ public void testAddRemoveShardOnLeader() throws Exception { leaderSeqNoStats.getGlobalCheckpoint(), leaderSeqNoStats.getMaxSeqNo(), followerSeqNoStats.getGlobalCheckpoint(), - followerSeqNoStats.getMaxSeqNo()); + followerSeqNoStats.getMaxSeqNo() + ); int batches = between(0, 10); int docCount = 0; boolean hasPromotion = false; @@ -196,13 +202,16 @@ public void testChangeLeaderHistoryUUID() throws Exception { leaderSeqNoStats.getGlobalCheckpoint(), leaderSeqNoStats.getMaxSeqNo(), followerSeqNoStats.getGlobalCheckpoint(), - followerSeqNoStats.getMaxSeqNo()); + followerSeqNoStats.getMaxSeqNo() + ); leaderGroup.syncGlobalCheckpoint(); leaderGroup.assertAllEqual(docCount); Set indexedDocIds = getShardDocUIDs(leaderGroup.getPrimary()); assertBusy(() -> { - assertThat(followerGroup.getPrimary().getLastKnownGlobalCheckpoint(), - equalTo(leaderGroup.getPrimary().getLastKnownGlobalCheckpoint())); + assertThat( + followerGroup.getPrimary().getLastKnownGlobalCheckpoint(), + equalTo(leaderGroup.getPrimary().getLastKnownGlobalCheckpoint()) + ); followerGroup.assertAllEqual(indexedDocIds.size()); }); @@ -218,8 +227,10 @@ public void testChangeLeaderHistoryUUID() throws Exception { assertBusy(() -> { assertThat(shardFollowTask.isStopped(), is(true)); ElasticsearchException failure = shardFollowTask.getStatus().getFatalException(); - assertThat(failure.getRootCause().getMessage(), equalTo("unexpected history uuid, expected [" + oldHistoryUUID + - "], actual [" + newHistoryUUID + "]")); + assertThat( + failure.getRootCause().getMessage(), + equalTo("unexpected history uuid, expected [" + oldHistoryUUID + "], actual [" + newHistoryUUID + "]") + ); }); } } @@ -228,7 +239,7 @@ public void testChangeLeaderHistoryUUID() throws Exception { public void testChangeFollowerHistoryUUID() throws Exception { try (ReplicationGroup leaderGroup = createLeaderGroup(0)) { leaderGroup.startAll(); - try(ReplicationGroup followerGroup = createFollowGroup(leaderGroup, 0)) { + try (ReplicationGroup followerGroup = createFollowGroup(leaderGroup, 0)) { int docCount = leaderGroup.appendDocs(randomInt(64)); leaderGroup.assertAllEqual(docCount); followerGroup.startAll(); @@ -240,13 +251,16 @@ public void testChangeFollowerHistoryUUID() throws Exception { leaderSeqNoStats.getGlobalCheckpoint(), leaderSeqNoStats.getMaxSeqNo(), followerSeqNoStats.getGlobalCheckpoint(), - followerSeqNoStats.getMaxSeqNo()); + followerSeqNoStats.getMaxSeqNo() + ); leaderGroup.syncGlobalCheckpoint(); leaderGroup.assertAllEqual(docCount); Set indexedDocIds = getShardDocUIDs(leaderGroup.getPrimary()); assertBusy(() -> { - assertThat(followerGroup.getPrimary().getLastKnownGlobalCheckpoint(), - equalTo(leaderGroup.getPrimary().getLastKnownGlobalCheckpoint())); + assertThat( + followerGroup.getPrimary().getLastKnownGlobalCheckpoint(), + equalTo(leaderGroup.getPrimary().getLastKnownGlobalCheckpoint()) + ); followerGroup.assertAllEqual(indexedDocIds.size()); }); @@ -262,8 +276,16 @@ public void testChangeFollowerHistoryUUID() throws Exception { assertBusy(() -> { assertThat(shardFollowTask.isStopped(), is(true)); ElasticsearchException failure = shardFollowTask.getStatus().getFatalException(); - assertThat(failure.getRootCause().getMessage(), equalTo("unexpected history uuid, expected [" + oldHistoryUUID + - "], actual [" + newHistoryUUID + "], shard is likely restored from snapshot or force allocated")); + assertThat( + failure.getRootCause().getMessage(), + equalTo( + "unexpected history uuid, expected [" + + oldHistoryUUID + + "], actual [" + + newHistoryUUID + + "], shard is likely restored from snapshot or force allocated" + ) + ); }); } } @@ -272,14 +294,19 @@ public void testChangeFollowerHistoryUUID() throws Exception { public void testRetryBulkShardOperations() throws Exception { try (ReplicationGroup leaderGroup = createLeaderGroup(between(0, 1))) { leaderGroup.startAll(); - try(ReplicationGroup followerGroup = createFollowGroup(leaderGroup, between(1, 3))) { + try (ReplicationGroup followerGroup = createFollowGroup(leaderGroup, between(1, 3))) { followerGroup.startAll(); leaderGroup.appendDocs(between(10, 100)); leaderGroup.refresh("test"); for (int numNoOps = between(1, 10), i = 0; i < numNoOps; i++) { long seqNo = leaderGroup.getPrimary().seqNoStats().getMaxSeqNo() + 1; - Engine.NoOp noOp = new Engine.NoOp(seqNo, leaderGroup.getPrimary().getOperationPrimaryTerm(), - Engine.Operation.Origin.REPLICA, threadPool.relativeTimeInMillis(), "test-" + i); + Engine.NoOp noOp = new Engine.NoOp( + seqNo, + leaderGroup.getPrimary().getOperationPrimaryTerm(), + Engine.Operation.Origin.REPLICA, + threadPool.relativeTimeInMillis(), + "test-" + i + ); for (IndexShard shard : leaderGroup) { getEngine(shard).noOp(noOp); } @@ -296,20 +323,35 @@ public void testRetryBulkShardOperations() throws Exception { long fromSeqNo = randomLongBetween(0, leadingPrimary.getLastKnownGlobalCheckpoint()); long toSeqNo = randomLongBetween(fromSeqNo, leadingPrimary.getLastKnownGlobalCheckpoint()); int numOps = Math.toIntExact(toSeqNo + 1 - fromSeqNo); - Translog.Operation[] ops = ShardChangesAction.getOperations(leadingPrimary, - leadingPrimary.getLastKnownGlobalCheckpoint(), fromSeqNo, numOps, leadingPrimary.getHistoryUUID(), - new ByteSizeValue(Long.MAX_VALUE, ByteSizeUnit.BYTES)); + Translog.Operation[] ops = ShardChangesAction.getOperations( + leadingPrimary, + leadingPrimary.getLastKnownGlobalCheckpoint(), + fromSeqNo, + numOps, + leadingPrimary.getHistoryUUID(), + new ByteSizeValue(Long.MAX_VALUE, ByteSizeUnit.BYTES) + ); IndexShard followingPrimary = followerGroup.getPrimary(); TransportWriteAction.WritePrimaryResult primaryResult = - TransportBulkShardOperationsAction.shardOperationOnPrimary(followingPrimary.shardId(), - followingPrimary.getHistoryUUID(), Arrays.asList(ops), leadingPrimary.getMaxSeqNoOfUpdatesOrDeletes(), - followingPrimary, logger); + TransportBulkShardOperationsAction.shardOperationOnPrimary( + followingPrimary.shardId(), + followingPrimary.getHistoryUUID(), + Arrays.asList(ops), + leadingPrimary.getMaxSeqNoOfUpdatesOrDeletes(), + followingPrimary, + logger + ); for (IndexShard replica : randomSubsetOf(followerGroup.getReplicas())) { final PlainActionFuture permitFuture = new PlainActionFuture<>(); - replica.acquireReplicaOperationPermit(followingPrimary.getOperationPrimaryTerm(), - followingPrimary.getLastKnownGlobalCheckpoint(), followingPrimary.getMaxSeqNoOfUpdatesOrDeletes(), - permitFuture, ThreadPool.Names.SAME, primaryResult); + replica.acquireReplicaOperationPermit( + followingPrimary.getOperationPrimaryTerm(), + followingPrimary.getLastKnownGlobalCheckpoint(), + followingPrimary.getMaxSeqNoOfUpdatesOrDeletes(), + permitFuture, + ThreadPool.Names.SAME, + primaryResult + ); try (Releasable ignored = permitFuture.get()) { TransportBulkShardOperationsAction.shardOperationOnReplica(primaryResult.replicaRequest(), replica, logger); } @@ -319,15 +361,19 @@ public void testRetryBulkShardOperations() throws Exception { followerGroup.promoteReplicaToPrimary(randomFrom(followerGroup.getReplicas())); ShardFollowNodeTask shardFollowTask = createShardFollowTask(leaderGroup, followerGroup); SeqNoStats followerSeqNoStats = followerGroup.getPrimary().seqNoStats(); - shardFollowTask.start(followerGroup.getPrimary().getHistoryUUID(), + shardFollowTask.start( + followerGroup.getPrimary().getHistoryUUID(), leadingPrimary.getLastKnownGlobalCheckpoint(), leadingPrimary.getMaxSeqNoOfUpdatesOrDeletes(), followerSeqNoStats.getGlobalCheckpoint(), - followerSeqNoStats.getMaxSeqNo()); + followerSeqNoStats.getMaxSeqNo() + ); try { assertBusy(() -> { - assertThat(followerGroup.getPrimary().getLastKnownGlobalCheckpoint(), - equalTo(leadingPrimary.getLastKnownGlobalCheckpoint())); + assertThat( + followerGroup.getPrimary().getLastKnownGlobalCheckpoint(), + equalTo(leadingPrimary.getLastKnownGlobalCheckpoint()) + ); assertConsistentHistoryBetweenLeaderAndFollower(leaderGroup, followerGroup, true); }); assertNull(shardFollowTask.getStatus().getFatalException()); @@ -346,7 +392,8 @@ public void testAddNewFollowingReplica() throws Exception { operations.add(new Translog.Index(Integer.toString(i), i, primaryTerm, 0, source, null, -1)); } Future recoveryFuture = null; - Settings settings = Settings.builder().put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true) + Settings settings = Settings.builder() + .put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true) .put(IndexSettings.INDEX_TRANSLOG_FLUSH_THRESHOLD_SIZE_SETTING.getKey(), new ByteSizeValue(between(1, 1000), ByteSizeUnit.KB)) .build(); IndexMetadata indexMetadata = buildIndexMetadata(between(0, 1), settings, indexMapping); @@ -360,8 +407,12 @@ protected EngineFactory getEngineFactory(ShardRouting routing) { while (operations.isEmpty() == false) { List bulkOps = randomSubsetOf(between(1, operations.size()), operations); operations.removeAll(bulkOps); - BulkShardOperationsRequest bulkRequest = new BulkShardOperationsRequest(group.getPrimary().shardId(), - group.getPrimary().getHistoryUUID(), bulkOps, -1); + BulkShardOperationsRequest bulkRequest = new BulkShardOperationsRequest( + group.getPrimary().shardId(), + group.getPrimary().getHistoryUUID(), + bulkOps, + -1 + ); new CcrAction(bulkRequest, new PlainActionFuture<>(), group).execute(); if (randomInt(100) < 10) { group.getPrimary().flush(new FlushRequest()); @@ -371,8 +422,11 @@ protected EngineFactory getEngineFactory(ShardRouting routing) { IndexShard newReplica = group.addReplica(); // We need to recover the replica async to release the main thread for the following task to fill missing // operations between the local checkpoint and max_seq_no which the recovering replica is waiting for. - recoveryFuture = group.asyncRecoverReplica(newReplica, - (shard, sourceNode) -> new RecoveryTarget(shard, sourceNode, null, null, recoveryListener) {}); + recoveryFuture = group.asyncRecoverReplica( + newReplica, + (shard, sourceNode) -> new RecoveryTarget(shard, sourceNode, null, null, recoveryListener) { + } + ); } } if (recoveryFuture != null) { @@ -416,11 +470,12 @@ public void testRetentionLeaseManagement() throws Exception { follower.startAll(); final ShardFollowNodeTask task = createShardFollowTask(leader, follower); task.start( - follower.getPrimary().getHistoryUUID(), - leader.getPrimary().getLastKnownGlobalCheckpoint(), - leader.getPrimary().seqNoStats().getMaxSeqNo(), - follower.getPrimary().getLastKnownGlobalCheckpoint(), - follower.getPrimary().seqNoStats().getMaxSeqNo()); + follower.getPrimary().getHistoryUUID(), + leader.getPrimary().getLastKnownGlobalCheckpoint(), + leader.getPrimary().seqNoStats().getMaxSeqNo(), + follower.getPrimary().getLastKnownGlobalCheckpoint(), + follower.getPrimary().seqNoStats().getMaxSeqNo() + ); final Scheduler.Cancellable renewable = task.getRenewable(); assertNotNull(renewable); assertFalse(renewable.isCancelled()); @@ -432,44 +487,56 @@ public void testRetentionLeaseManagement() throws Exception { } private ReplicationGroup createLeaderGroup(int replicas) throws IOException { - Settings settings = Settings.builder() - .put(IndexSettings.INDEX_SOFT_DELETES_RETENTION_OPERATIONS_SETTING.getKey(), 10000) - .build(); + Settings settings = Settings.builder().put(IndexSettings.INDEX_SOFT_DELETES_RETENTION_OPERATIONS_SETTING.getKey(), 10000).build(); return createGroup(replicas, settings); } private ReplicationGroup createFollowGroup(ReplicationGroup leaderGroup, int replicas) throws IOException { - final Settings settings = Settings.builder().put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true) - .put(IndexSettings.INDEX_TRANSLOG_FLUSH_THRESHOLD_SIZE_SETTING.getKey(), - new ByteSizeValue(between(1, 1000), ByteSizeUnit.KB)) - .build(); + final Settings settings = Settings.builder() + .put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true) + .put(IndexSettings.INDEX_TRANSLOG_FLUSH_THRESHOLD_SIZE_SETTING.getKey(), new ByteSizeValue(between(1, 1000), ByteSizeUnit.KB)) + .build(); IndexMetadata indexMetadata = buildIndexMetadata(replicas, settings, indexMapping); return new ReplicationGroup(indexMetadata) { @Override protected EngineFactory getEngineFactory(ShardRouting routing) { return new FollowingEngineFactory(); } + @Override protected synchronized void recoverPrimary(IndexShard primary) { DiscoveryNode localNode = new DiscoveryNode("foo", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT); Snapshot snapshot = new Snapshot("foo", new SnapshotId("bar", UUIDs.randomBase64UUID())); - ShardRouting routing = ShardRoutingHelper.newWithRestoreSource(primary.routingEntry(), - new RecoverySource.SnapshotRecoverySource(UUIDs.randomBase64UUID(), snapshot, Version.CURRENT, - new IndexId("test", UUIDs.randomBase64UUID(random())))); + ShardRouting routing = ShardRoutingHelper.newWithRestoreSource( + primary.routingEntry(), + new RecoverySource.SnapshotRecoverySource( + UUIDs.randomBase64UUID(), + snapshot, + Version.CURRENT, + new IndexId("test", UUIDs.randomBase64UUID(random())) + ) + ); primary.markAsRecovering("remote recovery from leader", new RecoveryState(routing, localNode, null)); final PlainActionFuture future = PlainActionFuture.newFuture(); primary.restoreFromRepository(new RestoreOnlyRepository(index.getName()) { @Override - public void restoreShard(Store store, SnapshotId snapshotId, IndexId indexId, ShardId snapshotShardId, - RecoveryState recoveryState, ActionListener listener) { + public void restoreShard( + Store store, + SnapshotId snapshotId, + IndexId indexId, + ShardId snapshotShardId, + RecoveryState recoveryState, + ActionListener listener + ) { ActionListener.completeWith(listener, () -> { IndexShard leader = leaderGroup.getPrimary(); Lucene.cleanLuceneIndex(primary.store().directory()); try (Engine.IndexCommitRef sourceCommit = leader.acquireSafeIndexCommit()) { Store.MetadataSnapshot sourceSnapshot = leader.store().getMetadata(sourceCommit.getIndexCommit()); for (StoreFileMetadata md : sourceSnapshot) { - primary.store().directory().copyFrom( - leader.store().directory(), md.name(), md.name(), IOContext.DEFAULT); + primary.store() + .directory() + .copyFrom(leader.store().directory(), md.name(), md.name(), IOContext.DEFAULT); } } recoveryState.getIndex().setFileDetailsComplete(); @@ -509,7 +576,16 @@ private ShardFollowNodeTask createShardFollowTask(ReplicationGroup leaderGroup, AtomicBoolean stopped = new AtomicBoolean(false); LongSet fetchOperations = new LongHashSet(); return new ShardFollowNodeTask( - 1L, "type", ShardFollowTask.NAME, "description", null, Collections.emptyMap(), params, scheduler, System::nanoTime) { + 1L, + "type", + ShardFollowTask.NAME, + "description", + null, + Collections.emptyMap(), + params, + scheduler, + System::nanoTime + ) { @Override protected synchronized void onOperationsFetched(Translog.Operation[] operations) { super.onOperationsFetched(operations); @@ -544,10 +620,15 @@ protected void innerSendBulkShardOperationsRequest( final List operations, final long maxSeqNoOfUpdates, final Consumer handler, - final Consumer errorHandler) { + final Consumer errorHandler + ) { Runnable task = () -> { - BulkShardOperationsRequest request = new BulkShardOperationsRequest(params.getFollowShardId(), - followerHistoryUUID, operations, maxSeqNoOfUpdates); + BulkShardOperationsRequest request = new BulkShardOperationsRequest( + params.getFollowShardId(), + followerHistoryUUID, + operations, + maxSeqNoOfUpdates + ); ActionListener listener = ActionListener.wrap(handler::accept, errorHandler); new CcrAction(request, listener, followerGroup).execute(); }; @@ -555,8 +636,12 @@ protected void innerSendBulkShardOperationsRequest( } @Override - protected void innerSendShardChangesRequest(long from, int maxOperationCount, Consumer handler, - Consumer errorHandler) { + protected void innerSendShardChangesRequest( + long from, + int maxOperationCount, + Consumer handler, + Consumer errorHandler + ) { Runnable task = () -> { List indexShards = new ArrayList<>(leaderGroup.getReplicas()); indexShards.add(leaderGroup.getPrimary()); @@ -568,18 +653,27 @@ protected void innerSendShardChangesRequest(long from, int maxOperationCount, Co final SeqNoStats seqNoStats = indexShard.seqNoStats(); final long maxSeqNoOfUpdatesOrDeletes = indexShard.getMaxSeqNoOfUpdatesOrDeletes(); if (from > seqNoStats.getGlobalCheckpoint()) { - handler.accept(ShardChangesAction.getResponse( + handler.accept( + ShardChangesAction.getResponse( 1L, 1L, 1L, seqNoStats, maxSeqNoOfUpdatesOrDeletes, ShardChangesAction.EMPTY_OPERATIONS_ARRAY, - 1L)); + 1L + ) + ); return; } - Translog.Operation[] ops = ShardChangesAction.getOperations(indexShard, seqNoStats.getGlobalCheckpoint(), from, - maxOperationCount, recordedLeaderIndexHistoryUUID, params.getMaxReadRequestSize()); + Translog.Operation[] ops = ShardChangesAction.getOperations( + indexShard, + seqNoStats.getGlobalCheckpoint(), + from, + maxOperationCount, + recordedLeaderIndexHistoryUUID, + params.getMaxReadRequestSize() + ); // hard code mapping version; this is ok, as mapping updates are not tested here final ShardChangesAction.Response response = new ShardChangesAction.Response( 1L, @@ -606,22 +700,24 @@ protected void innerSendShardChangesRequest(long from, int maxOperationCount, Co @Override protected Scheduler.Cancellable scheduleBackgroundRetentionLeaseRenewal(final LongSupplier followerGlobalCheckpoint) { final String retentionLeaseId = CcrRetentionLeases.retentionLeaseId( - "follower", - followerGroup.getPrimary().routingEntry().index(), - "remote", - leaderGroup.getPrimary().routingEntry().index()); + "follower", + followerGroup.getPrimary().routingEntry().index(), + "remote", + leaderGroup.getPrimary().routingEntry().index() + ); final PlainActionFuture response = new PlainActionFuture<>(); leaderGroup.addRetentionLease( - retentionLeaseId, - followerGlobalCheckpoint.getAsLong(), - "ccr", - ActionListener.wrap(response::onResponse, e -> fail(e.toString()))); + retentionLeaseId, + followerGlobalCheckpoint.getAsLong(), + "ccr", + ActionListener.wrap(response::onResponse, e -> fail(e.toString())) + ); response.actionGet(); return threadPool.scheduleWithFixedDelay( - () -> leaderGroup.renewRetentionLease(retentionLeaseId, followerGlobalCheckpoint.getAsLong(), "ccr"), - CcrRetentionLeases.RETENTION_LEASE_RENEW_INTERVAL_SETTING.get( - followerGroup.getPrimary().indexSettings().getSettings()), - ThreadPool.Names.GENERIC); + () -> leaderGroup.renewRetentionLease(retentionLeaseId, followerGlobalCheckpoint.getAsLong(), "ccr"), + CcrRetentionLeases.RETENTION_LEASE_RENEW_INTERVAL_SETTING.get(followerGroup.getPrimary().indexSettings().getSettings()), + ThreadPool.Names.GENERIC + ); } @Override @@ -637,13 +733,19 @@ public void markAsCompleted() { }; } - private void assertConsistentHistoryBetweenLeaderAndFollower(ReplicationGroup leader, ReplicationGroup follower, - boolean assertMaxSeqNoOfUpdatesOrDeletes) throws Exception { + private void assertConsistentHistoryBetweenLeaderAndFollower( + ReplicationGroup leader, + ReplicationGroup follower, + boolean assertMaxSeqNoOfUpdatesOrDeletes + ) throws Exception { final List> docAndSeqNosOnLeader = getDocIdAndSeqNos(leader.getPrimary()).stream() - .map(d -> Tuple.tuple(d.getId(), d.getSeqNo())).collect(Collectors.toList()); + .map(d -> Tuple.tuple(d.getId(), d.getSeqNo())) + .collect(Collectors.toList()); final Map operationsOnLeader = new HashMap<>(); - try (Translog.Snapshot snapshot = - leader.getPrimary().newChangesSnapshot("test", 0, Long.MAX_VALUE, false, randomBoolean(), randomBoolean())) { + try ( + Translog.Snapshot snapshot = leader.getPrimary() + .newChangesSnapshot("test", 0, Long.MAX_VALUE, false, randomBoolean(), randomBoolean()) + ) { Translog.Operation op; while ((op = snapshot.next()) != null) { operationsOnLeader.put(op.seqNo(), op); @@ -651,19 +753,32 @@ private void assertConsistentHistoryBetweenLeaderAndFollower(ReplicationGroup le } for (IndexShard followingShard : follower) { if (assertMaxSeqNoOfUpdatesOrDeletes) { - assertThat(followingShard.getMaxSeqNoOfUpdatesOrDeletes(), - greaterThanOrEqualTo(leader.getPrimary().getMaxSeqNoOfUpdatesOrDeletes())); + assertThat( + followingShard.getMaxSeqNoOfUpdatesOrDeletes(), + greaterThanOrEqualTo(leader.getPrimary().getMaxSeqNoOfUpdatesOrDeletes()) + ); } List> docAndSeqNosOnFollower = getDocIdAndSeqNos(followingShard).stream() - .map(d -> Tuple.tuple(d.getId(), d.getSeqNo())).collect(Collectors.toList()); + .map(d -> Tuple.tuple(d.getId(), d.getSeqNo())) + .collect(Collectors.toList()); assertThat(docAndSeqNosOnFollower, equalTo(docAndSeqNosOnLeader)); - try (Translog.Snapshot snapshot = - followingShard.newChangesSnapshot("test", 0, Long.MAX_VALUE, false, randomBoolean(), randomBoolean())) { + try ( + Translog.Snapshot snapshot = followingShard.newChangesSnapshot( + "test", + 0, + Long.MAX_VALUE, + false, + randomBoolean(), + randomBoolean() + ) + ) { Translog.Operation op; while ((op = snapshot.next()) != null) { Translog.Operation leaderOp = operationsOnLeader.get(op.seqNo()); - assertThat(TransportBulkShardOperationsAction.rewriteOperationWithPrimaryTerm(op, leaderOp.primaryTerm()), - equalTo(leaderOp)); + assertThat( + TransportBulkShardOperationsAction.rewriteOperationWithPrimaryTerm(op, leaderOp.primaryTerm()), + equalTo(leaderOp) + ); } } } @@ -681,8 +796,14 @@ protected void performOnPrimary(IndexShard primary, BulkShardOperationsRequest r primary.acquirePrimaryOperationPermit(permitFuture, ThreadPool.Names.SAME, request); final TransportWriteAction.WritePrimaryResult ccrResult; try (Releasable ignored = permitFuture.get()) { - ccrResult = TransportBulkShardOperationsAction.shardOperationOnPrimary(primary.shardId(), request.getHistoryUUID(), - request.getOperations(), request.getMaxSeqNoOfUpdatesOrDeletes(), primary, logger); + ccrResult = TransportBulkShardOperationsAction.shardOperationOnPrimary( + primary.shardId(), + request.getHistoryUUID(), + request.getOperations(), + request.getMaxSeqNoOfUpdatesOrDeletes(), + primary, + logger + ); TransportWriteActionTestHelper.performPostWriteActions(primary, request, ccrResult.location, logger); } catch (InterruptedException | ExecutionException | IOException e) { throw new RuntimeException(e); @@ -697,9 +818,18 @@ protected void adaptResponse(BulkShardOperationsResponse response, IndexShard in @Override protected void performOnReplica(BulkShardOperationsRequest request, IndexShard replica) throws Exception { - try (Releasable ignored = PlainActionFuture.get(f -> replica.acquireReplicaOperationPermit( - getPrimaryShard().getPendingPrimaryTerm(), getPrimaryShard().getLastKnownGlobalCheckpoint(), - getPrimaryShard().getMaxSeqNoOfUpdatesOrDeletes(), f, ThreadPool.Names.SAME, request))) { + try ( + Releasable ignored = PlainActionFuture.get( + f -> replica.acquireReplicaOperationPermit( + getPrimaryShard().getPendingPrimaryTerm(), + getPrimaryShard().getLastKnownGlobalCheckpoint(), + getPrimaryShard().getMaxSeqNoOfUpdatesOrDeletes(), + f, + ThreadPool.Names.SAME, + request + ) + ) + ) { Translog.Location location = TransportBulkShardOperationsAction.shardOperationOnReplica(request, replica, logger).location; TransportWriteActionTestHelper.performPostWriteActions(replica, request, location, logger); } diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/ShardFollowTasksExecutorAssignmentTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/ShardFollowTasksExecutorAssignmentTests.java index 02a09d247dbc0..6640c8654b209 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/ShardFollowTasksExecutorAssignmentTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/ShardFollowTasksExecutorAssignmentTests.java @@ -58,15 +58,10 @@ public void testRemoteClusterClientRoleWithoutDataRole() { } private void runNoAssignmentTest(final Set roles) { - runAssignmentTest( - roles, - 0, - Set::of, - (theSpecial, assignment) -> { - assertFalse(assignment.isAssigned()); - assertThat(assignment.getExplanation(), equalTo("no nodes found with data and remote cluster client roles")); - } - ); + runAssignmentTest(roles, 0, Set::of, (theSpecial, assignment) -> { + assertFalse(assignment.isAssigned()); + assertThat(assignment.getExplanation(), equalTo("no nodes found with data and remote cluster client roles")); + }); } private void runAssignmentTest( @@ -76,12 +71,17 @@ private void runAssignmentTest( final BiConsumer consumer ) { final ClusterService clusterService = mock(ClusterService.class); - when(clusterService.getClusterSettings()) - .thenReturn(new ClusterSettings(Settings.EMPTY, Set.of(CcrSettings.CCR_WAIT_FOR_METADATA_TIMEOUT))); + when(clusterService.getClusterSettings()).thenReturn( + new ClusterSettings(Settings.EMPTY, Set.of(CcrSettings.CCR_WAIT_FOR_METADATA_TIMEOUT)) + ); final SettingsModule settingsModule = mock(SettingsModule.class); when(settingsModule.getSettings()).thenReturn(Settings.EMPTY); - final ShardFollowTasksExecutor executor = - new ShardFollowTasksExecutor(mock(Client.class), mock(ThreadPool.class), clusterService, settingsModule); + final ShardFollowTasksExecutor executor = new ShardFollowTasksExecutor( + mock(Client.class), + mock(ThreadPool.class), + clusterService, + settingsModule + ); final ClusterState.Builder clusterStateBuilder = ClusterState.builder(new ClusterName("test")); final DiscoveryNodes.Builder nodesBuilder = DiscoveryNodes.builder(); final DiscoveryNode theSpecial = newNode(theSpecialRoles); @@ -90,8 +90,11 @@ private void runAssignmentTest( nodesBuilder.add(newNode(otherNodesRolesSupplier.get())); } clusterStateBuilder.nodes(nodesBuilder); - final Assignment assignment = executor.getAssignment(mock(ShardFollowTask.class), - clusterStateBuilder.nodes().getAllNodes(), clusterStateBuilder.build()); + final Assignment assignment = executor.getAssignment( + mock(ShardFollowTask.class), + clusterStateBuilder.nodes().getAllNodes(), + clusterStateBuilder.build() + ); consumer.accept(theSpecial, assignment); } diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/StatsResponsesTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/StatsResponsesTests.java index f13e4a4b67f97..10759072b6149 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/StatsResponsesTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/StatsResponsesTests.java @@ -61,7 +61,8 @@ static FollowStatsAction.StatsResponses createStatsResponse() { randomNonNegativeLong(), Collections.emptyNavigableMap(), randomNonNegativeLong(), - randomBoolean() ? new ElasticsearchException("fatal error") : null); + randomBoolean() ? new ElasticsearchException("fatal error") : null + ); responses.add(new FollowStatsAction.StatsResponse(status)); } return new FollowStatsAction.StatsResponses(Collections.emptyList(), Collections.emptyList(), responses); diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportActivateAutoFollowPatternActionTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportActivateAutoFollowPatternActionTests.java index 134ce91a181dd..206c71e82c52c 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportActivateAutoFollowPatternActionTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportActivateAutoFollowPatternActionTests.java @@ -31,32 +31,48 @@ public class TransportActivateAutoFollowPatternActionTests extends ESTestCase { public void testInnerActivateNoAutoFollowMetadata() { - Exception e = expectThrows(ResourceNotFoundException.class, - () -> TransportActivateAutoFollowPatternAction.innerActivate(new Request("test", true), ClusterState.EMPTY_STATE)); + Exception e = expectThrows( + ResourceNotFoundException.class, + () -> TransportActivateAutoFollowPatternAction.innerActivate(new Request("test", true), ClusterState.EMPTY_STATE) + ); assertThat(e.getMessage(), equalTo("auto-follow pattern [test] is missing")); } public void testInnerActivateDoesNotExist() { ClusterState clusterState = ClusterState.builder(new ClusterName("cluster")) - .metadata(Metadata.builder().putCustom(AutoFollowMetadata.TYPE, - new AutoFollowMetadata( - singletonMap("remote_cluster", randomAutoFollowPattern()), - singletonMap("remote_cluster", randomSubsetOf(randomIntBetween(1, 3), "uuid0", "uuid1", "uuid2")), - singletonMap("remote_cluster", singletonMap("header0", randomFrom("val0", "val2", "val3")))))) + .metadata( + Metadata.builder() + .putCustom( + AutoFollowMetadata.TYPE, + new AutoFollowMetadata( + singletonMap("remote_cluster", randomAutoFollowPattern()), + singletonMap("remote_cluster", randomSubsetOf(randomIntBetween(1, 3), "uuid0", "uuid1", "uuid2")), + singletonMap("remote_cluster", singletonMap("header0", randomFrom("val0", "val2", "val3"))) + ) + ) + ) .build(); - Exception e = expectThrows(ResourceNotFoundException.class, - () -> TransportActivateAutoFollowPatternAction.innerActivate(new Request("does_not_exist", true), clusterState)); + Exception e = expectThrows( + ResourceNotFoundException.class, + () -> TransportActivateAutoFollowPatternAction.innerActivate(new Request("does_not_exist", true), clusterState) + ); assertThat(e.getMessage(), equalTo("auto-follow pattern [does_not_exist] is missing")); } public void testInnerActivateToggle() { final AutoFollowMetadata.AutoFollowPattern autoFollowPattern = randomAutoFollowPattern(); final ClusterState clusterState = ClusterState.builder(new ClusterName("cluster")) - .metadata(Metadata.builder().putCustom(AutoFollowMetadata.TYPE, - new AutoFollowMetadata( - singletonMap("remote_cluster", autoFollowPattern), - singletonMap("remote_cluster", randomSubsetOf(randomIntBetween(1, 3), "uuid0", "uuid1", "uuid2")), - singletonMap("remote_cluster", singletonMap("header0", randomFrom("val0", "val2", "val3")))))) + .metadata( + Metadata.builder() + .putCustom( + AutoFollowMetadata.TYPE, + new AutoFollowMetadata( + singletonMap("remote_cluster", autoFollowPattern), + singletonMap("remote_cluster", randomSubsetOf(randomIntBetween(1, 3), "uuid0", "uuid1", "uuid2")), + singletonMap("remote_cluster", singletonMap("header0", randomFrom("val0", "val2", "val3"))) + ) + ) + ) .build(); { Request pauseRequest = new Request("remote_cluster", autoFollowPattern.isActive()); @@ -82,7 +98,8 @@ public void testInnerActivateToggle() { } private static AutoFollowMetadata.AutoFollowPattern randomAutoFollowPattern() { - return new AutoFollowMetadata.AutoFollowPattern(randomAlphaOfLength(5), + return new AutoFollowMetadata.AutoFollowPattern( + randomAlphaOfLength(5), randomSubsetOf(Arrays.asList("test-*", "user-*", "logs-*", "failures-*")), Collections.emptyList(), randomFrom("{{leader_index}}", "{{leader_index}}-follower", "test"), @@ -97,6 +114,7 @@ private static AutoFollowMetadata.AutoFollowPattern randomAutoFollowPattern() { randomIntBetween(1, 100), new ByteSizeValue(randomIntBetween(1, 100), randomFrom(ByteSizeUnit.values())), TimeValue.timeValueSeconds(randomIntBetween(30, 600)), - TimeValue.timeValueSeconds(randomIntBetween(30, 600))); + TimeValue.timeValueSeconds(randomIntBetween(30, 600)) + ); } } diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportDeleteAutoFollowPatternActionTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportDeleteAutoFollowPatternActionTests.java index 3a6c9b467fb4f..470c15d86dfb1 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportDeleteAutoFollowPatternActionTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportDeleteAutoFollowPatternActionTests.java @@ -12,9 +12,9 @@ import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xpack.core.ccr.AutoFollowMetadata; import org.elasticsearch.xpack.core.ccr.AutoFollowMetadata.AutoFollowPattern; import org.elasticsearch.xpack.core.ccr.action.DeleteAutoFollowPatternAction.Request; -import org.elasticsearch.xpack.core.ccr.AutoFollowMetadata; import java.util.ArrayList; import java.util.Collections; @@ -41,7 +41,8 @@ public void testInnerDelete() { existingPatterns, Collections.emptyList(), null, - Settings.EMPTY, true, + Settings.EMPTY, + true, null, null, null, @@ -91,8 +92,13 @@ public void testInnerDelete() { existingHeaders.put("name2", Collections.singletonMap("key", "val")); } ClusterState clusterState = ClusterState.builder(new ClusterName("us_cluster")) - .metadata(Metadata.builder().putCustom(AutoFollowMetadata.TYPE, - new AutoFollowMetadata(existingAutoFollowPatterns, existingAlreadyFollowedIndexUUIDS, existingHeaders))) + .metadata( + Metadata.builder() + .putCustom( + AutoFollowMetadata.TYPE, + new AutoFollowMetadata(existingAutoFollowPatterns, existingAlreadyFollowedIndexUUIDS, existingHeaders) + ) + ) .build(); Request request = new Request("name1"); @@ -139,24 +145,31 @@ public void testInnerDeleteDoesNotExist() { existingHeaders.put("key", Collections.singletonMap("key", "val")); } ClusterState clusterState = ClusterState.builder(new ClusterName("us_cluster")) - .metadata(Metadata.builder().putCustom(AutoFollowMetadata.TYPE, - new AutoFollowMetadata(existingAutoFollowPatterns, existingAlreadyFollowedIndexUUIDS, existingHeaders))) + .metadata( + Metadata.builder() + .putCustom( + AutoFollowMetadata.TYPE, + new AutoFollowMetadata(existingAutoFollowPatterns, existingAlreadyFollowedIndexUUIDS, existingHeaders) + ) + ) .build(); Request request = new Request("name2"); - Exception e = expectThrows(ResourceNotFoundException.class, - () -> TransportDeleteAutoFollowPatternAction.innerDelete(request, clusterState)); + Exception e = expectThrows( + ResourceNotFoundException.class, + () -> TransportDeleteAutoFollowPatternAction.innerDelete(request, clusterState) + ); assertThat(e.getMessage(), equalTo("auto-follow pattern [name2] is missing")); } public void testInnerDeleteNoAutoFollowMetadata() { - ClusterState clusterState = ClusterState.builder(new ClusterName("us_cluster")) - .metadata(Metadata.builder()) - .build(); + ClusterState clusterState = ClusterState.builder(new ClusterName("us_cluster")).metadata(Metadata.builder()).build(); Request request = new Request("name1"); - Exception e = expectThrows(ResourceNotFoundException.class, - () -> TransportDeleteAutoFollowPatternAction.innerDelete(request, clusterState)); + Exception e = expectThrows( + ResourceNotFoundException.class, + () -> TransportDeleteAutoFollowPatternAction.innerDelete(request, clusterState) + ); assertThat(e.getMessage(), equalTo("auto-follow pattern [name1] is missing")); } diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportFollowInfoActionTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportFollowInfoActionTests.java index ea21cb3f19df1..0435ee0cbaced 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportFollowInfoActionTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportFollowInfoActionTests.java @@ -31,9 +31,9 @@ public class TransportFollowInfoActionTests extends ESTestCase { public void testGetFollowInfos() { ClusterState state = createCS( - new String[] {"follower1", "follower2", "follower3", "index4"}, - new boolean[]{true, true, true, false}, - new boolean[]{true, true, false, false} + new String[] { "follower1", "follower2", "follower3", "index4" }, + new boolean[] { true, true, true, false }, + new boolean[] { true, true, false, false } ); List concreteIndices = Arrays.asList("follower1", "follower3"); @@ -61,17 +61,19 @@ private static ClusterState createCS(String[] indices, boolean[] followerIndices if (isFollowIndex) { imdBuilder.putCustom(Ccr.CCR_CUSTOM_METADATA_KEY, new HashMap<>()); if (active) { - persistentTasks.addTask(Integer.toString(i), ShardFollowTask.NAME, - createShardFollowTask(new Index(index, IndexMetadata.INDEX_UUID_NA_VALUE)), null); + persistentTasks.addTask( + Integer.toString(i), + ShardFollowTask.NAME, + createShardFollowTask(new Index(index, IndexMetadata.INDEX_UUID_NA_VALUE)), + null + ); } } mdBuilder.put(imdBuilder); } mdBuilder.putCustom(PersistentTasksCustomMetadata.TYPE, persistentTasks.build()); - return ClusterState.builder(new ClusterName("_cluster")) - .metadata(mdBuilder.build()) - .build(); + return ClusterState.builder(new ClusterName("_cluster")).metadata(mdBuilder.build()).build(); } } diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportFollowStatsActionTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportFollowStatsActionTests.java index fc7871fc45dda..2fc3ba8eb0cec 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportFollowStatsActionTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportFollowStatsActionTests.java @@ -46,25 +46,31 @@ public void testFindFollowerIndicesFromShardFollowTasks() { .addTask("3", ShardFollowTask.NAME, createShardFollowTask(index3.getIndex()), null); ClusterState clusterState = ClusterState.builder(new ClusterName("_cluster")) - .metadata(Metadata.builder() - .putCustom(PersistentTasksCustomMetadata.TYPE, persistentTasks.build()) - // only add index1 and index2 - .put(index1, false) - .put(index2, false) - .build()) + .metadata( + Metadata.builder() + .putCustom(PersistentTasksCustomMetadata.TYPE, persistentTasks.build()) + // only add index1 and index2 + .put(index1, false) + .put(index2, false) + .build() + ) .build(); Set result = TransportFollowStatsAction.findFollowerIndicesFromShardFollowTasks(clusterState, null); assertThat(result.size(), equalTo(2)); assertThat(result.contains(index1.getIndex().getName()), is(true)); assertThat(result.contains(index2.getIndex().getName()), is(true)); - result = TransportFollowStatsAction.findFollowerIndicesFromShardFollowTasks(clusterState, - new String[]{index2.getIndex().getName()}); + result = TransportFollowStatsAction.findFollowerIndicesFromShardFollowTasks( + clusterState, + new String[] { index2.getIndex().getName() } + ); assertThat(result.size(), equalTo(1)); assertThat(result.contains(index2.getIndex().getName()), is(true)); - result = TransportFollowStatsAction.findFollowerIndicesFromShardFollowTasks(clusterState, - new String[]{index3.getIndex().getName()}); + result = TransportFollowStatsAction.findFollowerIndicesFromShardFollowTasks( + clusterState, + new String[] { index3.getIndex().getName() } + ); assertThat(result.size(), equalTo(0)); } diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportGetAutoFollowPatternActionTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportGetAutoFollowPatternActionTests.java index 04d700704fe05..deab8dce2c821 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportGetAutoFollowPatternActionTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportGetAutoFollowPatternActionTests.java @@ -80,18 +80,20 @@ public void testGetAutoFollowPattern() { assertThat(result, hasEntry("name1", patterns.get("name1"))); assertThat(result, hasEntry("name2", patterns.get("name2"))); - expectThrows(ResourceNotFoundException.class, - () -> TransportGetAutoFollowPatternAction.getAutoFollowPattern(metadata, "another_alias")); + expectThrows( + ResourceNotFoundException.class, + () -> TransportGetAutoFollowPatternAction.getAutoFollowPattern(metadata, "another_alias") + ); } public void testGetAutoFollowPatternNoAutoFollowPatterns() { - AutoFollowMetadata autoFollowMetadata = - new AutoFollowMetadata(Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap()); - Metadata metadata = Metadata.builder() - .putCustom(AutoFollowMetadata.TYPE, autoFollowMetadata) - .build(); - expectThrows(ResourceNotFoundException.class, - () -> TransportGetAutoFollowPatternAction.getAutoFollowPattern(metadata, "name1")); + AutoFollowMetadata autoFollowMetadata = new AutoFollowMetadata( + Collections.emptyMap(), + Collections.emptyMap(), + Collections.emptyMap() + ); + Metadata metadata = Metadata.builder().putCustom(AutoFollowMetadata.TYPE, autoFollowMetadata).build(); + expectThrows(ResourceNotFoundException.class, () -> TransportGetAutoFollowPatternAction.getAutoFollowPattern(metadata, "name1")); Map result = TransportGetAutoFollowPatternAction.getAutoFollowPattern(metadata, null); assertThat(result.size(), equalTo(0)); @@ -99,8 +101,7 @@ public void testGetAutoFollowPatternNoAutoFollowPatterns() { public void testGetAutoFollowPatternNoAutoFollowMetadata() { Metadata metadata = Metadata.builder().build(); - expectThrows(ResourceNotFoundException.class, - () -> TransportGetAutoFollowPatternAction.getAutoFollowPattern(metadata, "name1")); + expectThrows(ResourceNotFoundException.class, () -> TransportGetAutoFollowPatternAction.getAutoFollowPattern(metadata, "name1")); Map result = TransportGetAutoFollowPatternAction.getAutoFollowPattern(metadata, null); assertThat(result.size(), equalTo(0)); diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportPutAutoFollowPatternActionTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportPutAutoFollowPatternActionTests.java index 2433f4fdcc9ab..ce288481d301f 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportPutAutoFollowPatternActionTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportPutAutoFollowPatternActionTests.java @@ -42,13 +42,9 @@ public void testInnerPut() { final int numberOfReplicas = randomIntBetween(0, 4); request.setSettings(Settings.builder().put(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), numberOfReplicas).build()); - ClusterState localState = ClusterState.builder(new ClusterName("us_cluster")) - .metadata(Metadata.builder()) - .build(); + ClusterState localState = ClusterState.builder(new ClusterName("us_cluster")).metadata(Metadata.builder()).build(); - ClusterState remoteState = ClusterState.builder(new ClusterName("eu_cluster")) - .metadata(Metadata.builder()) - .build(); + ClusterState remoteState = ClusterState.builder(new ClusterName("eu_cluster")).metadata(Metadata.builder()).build(); ClusterState result = TransportPutAutoFollowPatternAction.innerPut(request, null, localState, remoteState); AutoFollowMetadata autoFollowMetadata = result.metadata().custom(AutoFollowMetadata.TYPE); @@ -70,7 +66,8 @@ public void testInnerPut() { ); assertThat( IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.get(autoFollowMetadata.getPatterns().get("name1").getSettings()), - equalTo(numberOfReplicas)); + equalTo(numberOfReplicas) + ); assertThat(autoFollowMetadata.getFollowedLeaderIndexUUIDs().size(), equalTo(1)); assertThat(autoFollowMetadata.getFollowedLeaderIndexUUIDs().get("name1").size(), equalTo(0)); } @@ -85,36 +82,27 @@ public void testInnerPut_existingLeaderIndices() { request.setLeaderIndexExclusionPatterns(Collections.singletonList("logs-excluded-*")); } - ClusterState localState = ClusterState.builder(new ClusterName("us_cluster")) - .metadata(Metadata.builder()) - .build(); + ClusterState localState = ClusterState.builder(new ClusterName("us_cluster")).metadata(Metadata.builder()).build(); int numLeaderIndices = randomIntBetween(1, 8); int numMatchingLeaderIndices = randomIntBetween(1, 8); int numExcludedLeaderIndices = randomIntBetween(1, 8); Metadata.Builder mdBuilder = Metadata.builder(); for (int i = 0; i < numLeaderIndices; i++) { - mdBuilder.put(IndexMetadata.builder("transactions-" + i) - .settings(settings(Version.CURRENT)) - .numberOfShards(1) - .numberOfReplicas(0)); + mdBuilder.put( + IndexMetadata.builder("transactions-" + i).settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(0) + ); } for (int i = 0; i < numMatchingLeaderIndices; i++) { - mdBuilder.put(IndexMetadata.builder("logs-" + i) - .settings(settings(Version.CURRENT)) - .numberOfShards(1) - .numberOfReplicas(0)); + mdBuilder.put(IndexMetadata.builder("logs-" + i).settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(0)); } for (int i = 0; i < numExcludedLeaderIndices; i++) { - mdBuilder.put(IndexMetadata.builder("logs-excluded-" + i) - .settings(settings(Version.CURRENT)) - .numberOfShards(1) - .numberOfReplicas(0)); + mdBuilder.put( + IndexMetadata.builder("logs-excluded-" + i).settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(0) + ); } - ClusterState remoteState = ClusterState.builder(new ClusterName("eu_cluster")) - .metadata(mdBuilder) - .build(); + ClusterState remoteState = ClusterState.builder(new ClusterName("eu_cluster")).metadata(mdBuilder).build(); ClusterState result = TransportPutAutoFollowPatternAction.innerPut(request, null, localState, remoteState); AutoFollowMetadata autoFollowMetadata = result.metadata().custom(AutoFollowMetadata.TYPE); @@ -131,8 +119,9 @@ public void testInnerPut_existingLeaderIndices() { assertThat(exclusionPatterns.size(), equalTo(0)); } - final int expectedAutoFollowIndexCount = - withExclusionPatterns ? numMatchingLeaderIndices : numMatchingLeaderIndices + numExcludedLeaderIndices; + final int expectedAutoFollowIndexCount = withExclusionPatterns + ? numMatchingLeaderIndices + : numMatchingLeaderIndices + numExcludedLeaderIndices; assertThat(autoFollowMetadata.getFollowedLeaderIndexUUIDs().size(), equalTo(1)); assertThat(autoFollowMetadata.getFollowedLeaderIndexUUIDs().get("name1").size(), equalTo(expectedAutoFollowIndexCount)); } @@ -168,7 +157,9 @@ public void testInnerPut_existingLeaderIndicesAndAutoFollowMetadata() { null, null, null, - null)); + null + ) + ); Map> existingAlreadyFollowedIndexUUIDS = new HashMap<>(); List existingUUIDS = new ArrayList<>(); existingUUIDS.add("_val"); @@ -177,29 +168,28 @@ public void testInnerPut_existingLeaderIndicesAndAutoFollowMetadata() { existingHeaders.put("name1", Collections.singletonMap("key", "val")); ClusterState localState = ClusterState.builder(new ClusterName("us_cluster")) - .metadata(Metadata.builder().putCustom(AutoFollowMetadata.TYPE, - new AutoFollowMetadata(existingAutoFollowPatterns, existingAlreadyFollowedIndexUUIDS, existingHeaders))) + .metadata( + Metadata.builder() + .putCustom( + AutoFollowMetadata.TYPE, + new AutoFollowMetadata(existingAutoFollowPatterns, existingAlreadyFollowedIndexUUIDS, existingHeaders) + ) + ) .build(); int numLeaderIndices = randomIntBetween(1, 8); Metadata.Builder mdBuilder = Metadata.builder(); for (int i = 0; i < numLeaderIndices; i++) { - mdBuilder.put(IndexMetadata.builder("logs-" + i) - .settings(settings(Version.CURRENT)) - .numberOfShards(1) - .numberOfReplicas(0)); + mdBuilder.put(IndexMetadata.builder("logs-" + i).settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(0)); } int numExcludedLeaderIndices = randomIntBetween(1, 8); for (int i = 0; i < numExcludedLeaderIndices; i++) { - mdBuilder.put(IndexMetadata.builder("logs-excluded-" + i) - .settings(settings(Version.CURRENT)) - .numberOfShards(1) - .numberOfReplicas(0)); + mdBuilder.put( + IndexMetadata.builder("logs-excluded-" + i).settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(0) + ); } - ClusterState remoteState = ClusterState.builder(new ClusterName("eu_cluster")) - .metadata(mdBuilder) - .build(); + ClusterState remoteState = ClusterState.builder(new ClusterName("eu_cluster")).metadata(mdBuilder).build(); ClusterState result = TransportPutAutoFollowPatternAction.innerPut(request, null, localState, remoteState); AutoFollowMetadata autoFollowMetadata = result.metadata().custom(AutoFollowMetadata.TYPE); @@ -216,8 +206,7 @@ public void testInnerPut_existingLeaderIndicesAndAutoFollowMetadata() { } else { assertThat(exclusionPatterns.size(), equalTo(0)); } - final int expectedAutoFollowIndexCount = - withExclusionPatterns ? numLeaderIndices : numLeaderIndices + numExcludedLeaderIndices; + final int expectedAutoFollowIndexCount = withExclusionPatterns ? numLeaderIndices : numLeaderIndices + numExcludedLeaderIndices; assertThat(autoFollowMetadata.getFollowedLeaderIndexUUIDs().size(), equalTo(1)); assertThat(autoFollowMetadata.getFollowedLeaderIndexUUIDs().get("name1").size(), equalTo(expectedAutoFollowIndexCount + 1)); assertThat(autoFollowMetadata.getHeaders().size(), equalTo(1)); diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportPutFollowActionTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportPutFollowActionTests.java index 26f7bea220d79..1cf89d8a7e858 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportPutFollowActionTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportPutFollowActionTests.java @@ -91,9 +91,7 @@ static DataStream generateDataSteam(String name, int numBackingIndices, boolean } static DataStream generateDataSteam(String name, int generation, boolean replicate, String... backingIndexNames) { - List backingIndices = Arrays.stream(backingIndexNames) - .map(value -> new Index(value, "uuid")) - .collect(Collectors.toList()); + List backingIndices = Arrays.stream(backingIndexNames).map(value -> new Index(value, "uuid")).collect(Collectors.toList()); return new DataStream(name, new TimestampField("@timestamp"), backingIndices, generation, Map.of(), false, replicate, false); } diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportResumeFollowActionTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportResumeFollowActionTests.java index 561015a5888be..d687e9d16d81e 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportResumeFollowActionTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportResumeFollowActionTests.java @@ -49,23 +49,30 @@ public void testValidation() throws IOException { customMetadata.put(Ccr.CCR_CUSTOM_METADATA_LEADER_INDEX_UUID_KEY, "_na_"); ResumeFollowAction.Request request = resumeFollow("index2"); - String[] UUIDs = new String[]{"uuid"}; + String[] UUIDs = new String[] { "uuid" }; { IndexMetadata leaderIMD = createIMD("index1", 5, Settings.EMPTY, null); IndexMetadata followIMD = createIMD("index2", 5, Settings.EMPTY, null); - Exception e = expectThrows(IllegalArgumentException.class, - () -> validate(request, leaderIMD, followIMD, UUIDs, null)); + Exception e = expectThrows(IllegalArgumentException.class, () -> validate(request, leaderIMD, followIMD, UUIDs, null)); assertThat(e.getMessage(), equalTo("follow index [index2] does not have ccr metadata")); } { // should fail because the recorded leader index uuid is not equal to the leader actual index IndexMetadata leaderIMD = createIMD("index1", 5, Settings.EMPTY, null); - IndexMetadata followIMD = createIMD("index2", 5, Settings.EMPTY, - singletonMap(Ccr.CCR_CUSTOM_METADATA_LEADER_INDEX_UUID_KEY, "another-value")); - Exception e = expectThrows(IllegalArgumentException.class, - () -> validate(request, leaderIMD, followIMD, UUIDs, null)); - assertThat(e.getMessage(), equalTo("follow index [index2] should reference [_na_] as leader index but " + - "instead reference [another-value] as leader index")); + IndexMetadata followIMD = createIMD( + "index2", + 5, + Settings.EMPTY, + singletonMap(Ccr.CCR_CUSTOM_METADATA_LEADER_INDEX_UUID_KEY, "another-value") + ); + Exception e = expectThrows(IllegalArgumentException.class, () -> validate(request, leaderIMD, followIMD, UUIDs, null)); + assertThat( + e.getMessage(), + equalTo( + "follow index [index2] should reference [_na_] as leader index but " + + "instead reference [another-value] as leader index" + ) + ); } { // should fail because the recorded leader index history uuid is not equal to the leader actual index history uuid: @@ -74,15 +81,23 @@ public void testValidation() throws IOException { anotherCustomMetadata.put(Ccr.CCR_CUSTOM_METADATA_LEADER_INDEX_UUID_KEY, "_na_"); anotherCustomMetadata.put(Ccr.CCR_CUSTOM_METADATA_LEADER_INDEX_SHARD_HISTORY_UUIDS, "another-uuid"); IndexMetadata followIMD = createIMD("index2", 5, Settings.EMPTY, anotherCustomMetadata); - Exception e = expectThrows(IllegalArgumentException.class, - () -> validate(request, leaderIMD, followIMD, UUIDs, null)); - assertThat(e.getMessage(), equalTo("leader shard [index2][0] should reference [another-uuid] as history uuid but " + - "instead reference [uuid] as history uuid")); + Exception e = expectThrows(IllegalArgumentException.class, () -> validate(request, leaderIMD, followIMD, UUIDs, null)); + assertThat( + e.getMessage(), + equalTo( + "leader shard [index2][0] should reference [another-uuid] as history uuid but " + + "instead reference [uuid] as history uuid" + ) + ); } { // should fail because leader index does not have soft deletes enabled - IndexMetadata leaderIMD = createIMD("index1", 5, Settings.builder() - .put(IndexSettings.INDEX_SOFT_DELETES_SETTING.getKey(), "false").build(), null); + IndexMetadata leaderIMD = createIMD( + "index1", + 5, + Settings.builder().put(IndexSettings.INDEX_SOFT_DELETES_SETTING.getKey(), "false").build(), + null + ); IndexMetadata followIMD = createIMD("index2", 5, Settings.EMPTY, customMetadata); Exception e = expectThrows(IllegalArgumentException.class, () -> validate(request, leaderIMD, followIMD, UUIDs, null)); assertThat(e.getMessage(), equalTo("leader index [index1] does not have soft deletes enabled")); @@ -90,8 +105,12 @@ public void testValidation() throws IOException { { // should fail because the follower index does not have soft deletes enabled IndexMetadata leaderIMD = createIMD("index1", 5, Settings.EMPTY, null); - IndexMetadata followIMD = createIMD("index2", 5, Settings.builder() - .put(IndexSettings.INDEX_SOFT_DELETES_SETTING.getKey(), "false").build(), customMetadata); + IndexMetadata followIMD = createIMD( + "index2", + 5, + Settings.builder().put(IndexSettings.INDEX_SOFT_DELETES_SETTING.getKey(), "false").build(), + customMetadata + ); Exception e = expectThrows(IllegalArgumentException.class, () -> validate(request, leaderIMD, followIMD, UUIDs, null)); assertThat(e.getMessage(), equalTo("follower index [index2] does not have soft deletes enabled")); } @@ -100,8 +119,10 @@ public void testValidation() throws IOException { IndexMetadata leaderIMD = createIMD("index1", 5, Settings.EMPTY, null); IndexMetadata followIMD = createIMD("index2", 4, Settings.EMPTY, customMetadata); Exception e = expectThrows(IllegalArgumentException.class, () -> validate(request, leaderIMD, followIMD, UUIDs, null)); - assertThat(e.getMessage(), - equalTo("leader index primary shards [5] does not match with the number of shards of the follow index [4]")); + assertThat( + e.getMessage(), + equalTo("leader index primary shards [5] does not match with the number of shards of the follow index [4]") + ); } { // should fail, because leader index is closed @@ -116,17 +137,33 @@ public void testValidation() throws IOException { IndexMetadata followIMD = createIMD("index2", 1, Settings.EMPTY, customMetadata); MapperService mapperService = MapperTestUtils.newMapperService(xContentRegistry(), createTempDir(), Settings.EMPTY, "index2"); mapperService.merge(followIMD, MapperService.MergeReason.MAPPING_RECOVERY); - Exception e = expectThrows(IllegalArgumentException.class, - () -> validate(request, leaderIMD, followIMD, UUIDs, mapperService)); - assertThat(e.getMessage(), equalTo("the following index [index2] is not ready to follow; " + - "the setting [index.xpack.ccr.following_index] must be enabled.")); + Exception e = expectThrows(IllegalArgumentException.class, () -> validate(request, leaderIMD, followIMD, UUIDs, mapperService)); + assertThat( + e.getMessage(), + equalTo( + "the following index [index2] is not ready to follow; " + + "the setting [index.xpack.ccr.following_index] must be enabled." + ) + ); } { // should fail, because leader has a field with the same name mapped as keyword and follower as text - IndexMetadata leaderIMD = createIMD("index1", State.OPEN, "{\"properties\": {\"field\": {\"type\": \"keyword\"}}}", 5, - Settings.EMPTY, null); - IndexMetadata followIMD = createIMD("index2", State.OPEN, "{\"properties\": {\"field\": {\"type\": \"text\"}}}", 5, - Settings.builder().put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true).build(), customMetadata); + IndexMetadata leaderIMD = createIMD( + "index1", + State.OPEN, + "{\"properties\": {\"field\": {\"type\": \"keyword\"}}}", + 5, + Settings.EMPTY, + null + ); + IndexMetadata followIMD = createIMD( + "index2", + State.OPEN, + "{\"properties\": {\"field\": {\"type\": \"text\"}}}", + 5, + Settings.builder().put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true).build(), + customMetadata + ); MapperService mapperService = MapperTestUtils.newMapperService(xContentRegistry(), createTempDir(), Settings.EMPTY, "index2"); mapperService.merge(followIMD, MapperService.MergeReason.MAPPING_RECOVERY); Exception e = expectThrows(IllegalArgumentException.class, () -> validate(request, leaderIMD, followIMD, UUIDs, mapperService)); @@ -135,37 +172,73 @@ public void testValidation() throws IOException { { // should fail because of non whitelisted settings not the same between leader and follow index String mapping = "{\"properties\": {\"field\": {\"type\": \"text\", \"analyzer\": \"my_analyzer\"}}}"; - IndexMetadata leaderIMD = createIMD("index1", State.OPEN, mapping, 5, Settings.builder() - .put("index.analysis.analyzer.my_analyzer.type", "custom") - .put("index.analysis.analyzer.my_analyzer.tokenizer", "whitespace").build(), null); - IndexMetadata followIMD = createIMD("index2", State.OPEN, mapping, 5, Settings.builder() - .put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true) - .put("index.analysis.analyzer.my_analyzer.type", "custom") - .put("index.analysis.analyzer.my_analyzer.tokenizer", "standard").build(), customMetadata); + IndexMetadata leaderIMD = createIMD( + "index1", + State.OPEN, + mapping, + 5, + Settings.builder() + .put("index.analysis.analyzer.my_analyzer.type", "custom") + .put("index.analysis.analyzer.my_analyzer.tokenizer", "whitespace") + .build(), + null + ); + IndexMetadata followIMD = createIMD( + "index2", + State.OPEN, + mapping, + 5, + Settings.builder() + .put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true) + .put("index.analysis.analyzer.my_analyzer.type", "custom") + .put("index.analysis.analyzer.my_analyzer.tokenizer", "standard") + .build(), + customMetadata + ); Exception e = expectThrows(IllegalArgumentException.class, () -> validate(request, leaderIMD, followIMD, UUIDs, null)); - assertThat(e.getMessage(), equalTo("the leader index settings [{\"index.analysis.analyzer.my_analyzer.tokenizer\"" + - ":\"whitespace\",\"index.analysis.analyzer.my_analyzer.type\":\"custom\",\"index.number_of_shards\":\"5\"}] " + - "and follower index settings [{\"index.analysis.analyzer.my_analyzer.tokenizer\":\"standard\"," + - "\"index.analysis.analyzer.my_analyzer.type\":\"custom\",\"index.number_of_shards\":\"5\"}] must be identical")); + assertThat( + e.getMessage(), + equalTo( + "the leader index settings [{\"index.analysis.analyzer.my_analyzer.tokenizer\"" + + ":\"whitespace\",\"index.analysis.analyzer.my_analyzer.type\":\"custom\",\"index.number_of_shards\":\"5\"}] " + + "and follower index settings [{\"index.analysis.analyzer.my_analyzer.tokenizer\":\"standard\"," + + "\"index.analysis.analyzer.my_analyzer.type\":\"custom\",\"index.number_of_shards\":\"5\"}] must be identical" + ) + ); } { // should fail because the following index does not have the following_index settings IndexMetadata leaderIMD = createIMD("index1", 5, Settings.EMPTY, null); Settings followingIndexSettings = Settings.builder().put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), false).build(); IndexMetadata followIMD = createIMD("index2", 5, followingIndexSettings, customMetadata); - MapperService mapperService = MapperTestUtils.newMapperService(xContentRegistry(), createTempDir(), - followingIndexSettings, "index2"); + MapperService mapperService = MapperTestUtils.newMapperService( + xContentRegistry(), + createTempDir(), + followingIndexSettings, + "index2" + ); mapperService.merge(followIMD, MapperService.MergeReason.MAPPING_RECOVERY); - IllegalArgumentException error = - expectThrows(IllegalArgumentException.class, () -> validate(request, leaderIMD, followIMD, UUIDs, mapperService)); - assertThat(error.getMessage(), equalTo("the following index [index2] is not ready to follow; " + - "the setting [index.xpack.ccr.following_index] must be enabled.")); + IllegalArgumentException error = expectThrows( + IllegalArgumentException.class, + () -> validate(request, leaderIMD, followIMD, UUIDs, mapperService) + ); + assertThat( + error.getMessage(), + equalTo( + "the following index [index2] is not ready to follow; " + + "the setting [index.xpack.ccr.following_index] must be enabled." + ) + ); } { // should succeed IndexMetadata leaderIMD = createIMD("index1", 5, Settings.EMPTY, null); - IndexMetadata followIMD = createIMD("index2", 5, Settings.builder() - .put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true).build(), customMetadata); + IndexMetadata followIMD = createIMD( + "index2", + 5, + Settings.builder().put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true).build(), + customMetadata + ); MapperService mapperService = MapperTestUtils.newMapperService(xContentRegistry(), createTempDir(), Settings.EMPTY, "index2"); mapperService.merge(followIMD, MapperService.MergeReason.MAPPING_RECOVERY); validate(request, leaderIMD, followIMD, UUIDs, mapperService); @@ -173,32 +246,72 @@ public void testValidation() throws IOException { { // should succeed, index settings are identical String mapping = "{\"properties\": {\"field\": {\"type\": \"text\", \"analyzer\": \"my_analyzer\"}}}"; - IndexMetadata leaderIMD = createIMD("index1", State.OPEN, mapping, 5, Settings.builder() - .put("index.analysis.analyzer.my_analyzer.type", "custom") - .put("index.analysis.analyzer.my_analyzer.tokenizer", "standard").build(), null); - IndexMetadata followIMD = createIMD("index2", State.OPEN, mapping, 5, Settings.builder() - .put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true) - .put("index.analysis.analyzer.my_analyzer.type", "custom") - .put("index.analysis.analyzer.my_analyzer.tokenizer", "standard").build(), customMetadata); - MapperService mapperService = MapperTestUtils.newMapperService(xContentRegistry(), createTempDir(), - followIMD.getSettings(), "index2"); + IndexMetadata leaderIMD = createIMD( + "index1", + State.OPEN, + mapping, + 5, + Settings.builder() + .put("index.analysis.analyzer.my_analyzer.type", "custom") + .put("index.analysis.analyzer.my_analyzer.tokenizer", "standard") + .build(), + null + ); + IndexMetadata followIMD = createIMD( + "index2", + State.OPEN, + mapping, + 5, + Settings.builder() + .put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true) + .put("index.analysis.analyzer.my_analyzer.type", "custom") + .put("index.analysis.analyzer.my_analyzer.tokenizer", "standard") + .build(), + customMetadata + ); + MapperService mapperService = MapperTestUtils.newMapperService( + xContentRegistry(), + createTempDir(), + followIMD.getSettings(), + "index2" + ); mapperService.merge(followIMD, MapperService.MergeReason.MAPPING_RECOVERY); validate(request, leaderIMD, followIMD, UUIDs, mapperService); } { // should succeed despite whitelisted settings being different String mapping = "{\"properties\": {\"field\": {\"type\": \"text\", \"analyzer\": \"my_analyzer\"}}}"; - IndexMetadata leaderIMD = createIMD("index1", State.OPEN, mapping, 5, Settings.builder() - .put(IndexSettings.INDEX_REFRESH_INTERVAL_SETTING.getKey(), "1s") - .put("index.analysis.analyzer.my_analyzer.type", "custom") - .put("index.analysis.analyzer.my_analyzer.tokenizer", "standard").build(), null); - IndexMetadata followIMD = createIMD("index2", State.OPEN, mapping, 5, Settings.builder() - .put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true) - .put(IndexSettings.INDEX_REFRESH_INTERVAL_SETTING.getKey(), "10s") - .put("index.analysis.analyzer.my_analyzer.type", "custom") - .put("index.analysis.analyzer.my_analyzer.tokenizer", "standard").build(), customMetadata); - MapperService mapperService = MapperTestUtils.newMapperService(xContentRegistry(), createTempDir(), - followIMD.getSettings(), "index2"); + IndexMetadata leaderIMD = createIMD( + "index1", + State.OPEN, + mapping, + 5, + Settings.builder() + .put(IndexSettings.INDEX_REFRESH_INTERVAL_SETTING.getKey(), "1s") + .put("index.analysis.analyzer.my_analyzer.type", "custom") + .put("index.analysis.analyzer.my_analyzer.tokenizer", "standard") + .build(), + null + ); + IndexMetadata followIMD = createIMD( + "index2", + State.OPEN, + mapping, + 5, + Settings.builder() + .put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true) + .put(IndexSettings.INDEX_REFRESH_INTERVAL_SETTING.getKey(), "10s") + .put("index.analysis.analyzer.my_analyzer.type", "custom") + .put("index.analysis.analyzer.my_analyzer.tokenizer", "standard") + .build(), + customMetadata + ); + MapperService mapperService = MapperTestUtils.newMapperService( + xContentRegistry(), + createTempDir(), + followIMD.getSettings(), + "index2" + ); mapperService.merge(followIMD, MapperService.MergeReason.MAPPING_RECOVERY); validate(request, leaderIMD, followIMD, UUIDs, mapperService); } @@ -224,8 +337,11 @@ public void testDynamicIndexSettingsAreClassified() { if (setting.isDynamic()) { boolean notReplicated = TransportResumeFollowAction.NON_REPLICATED_SETTINGS.contains(setting); boolean replicated = replicatedSettings.contains(setting); - assertThat("setting [" + setting.getKey() + "] is not classified as replicated or not replicated", - notReplicated ^ replicated, is(true)); + assertThat( + "setting [" + setting.getKey() + "] is not classified as replicated or not replicated", + notReplicated ^ replicated, + is(true) + ); } } } @@ -245,19 +361,19 @@ public void testFilter() { assertThat(result.size(), equalTo(0)); } - private static IndexMetadata createIMD(String index, - int numberOfShards, - Settings settings, - Map custom) throws IOException { + private static IndexMetadata createIMD(String index, int numberOfShards, Settings settings, Map custom) + throws IOException { return createIMD(index, State.OPEN, "{\"properties\": {}}", numberOfShards, settings, custom); } - private static IndexMetadata createIMD(String index, - State state, - String mapping, - int numberOfShards, - Settings settings, - Map custom) throws IOException { + private static IndexMetadata createIMD( + String index, + State state, + String mapping, + int numberOfShards, + Settings settings, + Map custom + ) throws IOException { IndexMetadata.Builder builder = IndexMetadata.builder(index) .settings(settings(Version.CURRENT).put(settings)) .numberOfShards(numberOfShards) diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportUnfollowActionTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportUnfollowActionTests.java index cd641ea201667..f9994cd3609b7 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportUnfollowActionTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportUnfollowActionTests.java @@ -42,9 +42,7 @@ public void testUnfollow() { .putCustom(Ccr.CCR_CUSTOM_METADATA_KEY, new HashMap<>()); ClusterState current = ClusterState.builder(new ClusterName("cluster_name")) - .metadata(Metadata.builder() - .put(followerIndex) - .build()) + .metadata(Metadata.builder().put(followerIndex).build()) .build(); ClusterState result = TransportUnfollowAction.unfollow("follow_index", current); @@ -62,13 +60,13 @@ public void testUnfollowIndexOpen() { .putCustom(Ccr.CCR_CUSTOM_METADATA_KEY, new HashMap<>()); ClusterState current = ClusterState.builder(new ClusterName("cluster_name")) - .metadata(Metadata.builder() - .put(followerIndex) - .build()) + .metadata(Metadata.builder().put(followerIndex).build()) .build(); Exception e = expectThrows(IllegalArgumentException.class, () -> TransportUnfollowAction.unfollow("follow_index", current)); - assertThat(e.getMessage(), - equalTo("cannot convert the follower index [follow_index] to a non-follower, because it has not been closed")); + assertThat( + e.getMessage(), + equalTo("cannot convert the follower index [follow_index] to a non-follower, because it has not been closed") + ); } public void testUnfollowRunningShardFollowTasks() { @@ -79,7 +77,6 @@ public void testUnfollowRunningShardFollowTasks() { .state(IndexMetadata.State.CLOSE) .putCustom(Ccr.CCR_CUSTOM_METADATA_KEY, new HashMap<>()); - ShardFollowTask params = new ShardFollowTask( null, new ShardId("follow_index", "", 0), @@ -96,18 +93,30 @@ public void testUnfollowRunningShardFollowTasks() { TimeValue.timeValueMillis(10), Collections.emptyMap() ); - PersistentTasksCustomMetadata.PersistentTask task = - new PersistentTasksCustomMetadata.PersistentTask<>("id", ShardFollowTask.NAME, params, 0, null); + PersistentTasksCustomMetadata.PersistentTask task = new PersistentTasksCustomMetadata.PersistentTask<>( + "id", + ShardFollowTask.NAME, + params, + 0, + null + ); ClusterState current = ClusterState.builder(new ClusterName("cluster_name")) - .metadata(Metadata.builder() - .put(followerIndex) - .putCustom(PersistentTasksCustomMetadata.TYPE, new PersistentTasksCustomMetadata(0, Collections.singletonMap("id", task))) - .build()) + .metadata( + Metadata.builder() + .put(followerIndex) + .putCustom( + PersistentTasksCustomMetadata.TYPE, + new PersistentTasksCustomMetadata(0, Collections.singletonMap("id", task)) + ) + .build() + ) .build(); Exception e = expectThrows(IllegalArgumentException.class, () -> TransportUnfollowAction.unfollow("follow_index", current)); - assertThat(e.getMessage(), - equalTo("cannot convert the follower index [follow_index] to a non-follower, because it has not been paused")); + assertThat( + e.getMessage(), + equalTo("cannot convert the follower index [follow_index] to a non-follower, because it has not been paused") + ); } public void testUnfollowMissingIndex() { @@ -119,9 +128,7 @@ public void testUnfollowMissingIndex() { .putCustom(Ccr.CCR_CUSTOM_METADATA_KEY, new HashMap<>()); ClusterState current = ClusterState.builder(new ClusterName("cluster_name")) - .metadata(Metadata.builder() - .put(followerIndex) - .build()) + .metadata(Metadata.builder().put(followerIndex).build()) .build(); expectThrows(IndexNotFoundException.class, () -> TransportUnfollowAction.unfollow("another_index", current)); } @@ -134,9 +141,7 @@ public void testUnfollowNoneFollowIndex() { .state(IndexMetadata.State.CLOSE); ClusterState current = ClusterState.builder(new ClusterName("cluster_name")) - .metadata(Metadata.builder() - .put(followerIndex) - .build()) + .metadata(Metadata.builder().put(followerIndex).build()) .build(); expectThrows(IllegalArgumentException.class, () -> TransportUnfollowAction.unfollow("follow_index", current)); } diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/bulk/BulkShardOperationsTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/bulk/BulkShardOperationsTests.java index 88dbda7a01b9d..c1bd768b9a05f 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/bulk/BulkShardOperationsTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/bulk/BulkShardOperationsTests.java @@ -49,15 +49,16 @@ public void testPrimaryTermFromFollower() throws IOException { for (int i = 0; i < numOps; i++) { final String id = Integer.toString(i); final long seqNo = i; - final Translog.Operation.Type type = - randomValueOtherThan(Translog.Operation.Type.CREATE, () -> randomFrom(Translog.Operation.Type.values())); + final Translog.Operation.Type type = randomValueOtherThan( + Translog.Operation.Type.CREATE, + () -> randomFrom(Translog.Operation.Type.values()) + ); switch (type) { case INDEX: operations.add(new Translog.Index(id, seqNo, primaryTerm, 0, SOURCE, null, -1)); break; case DELETE: - operations.add( - new Translog.Delete(id, seqNo, primaryTerm, 0)); + operations.add(new Translog.Delete(id, seqNo, primaryTerm, 0)); break; case NO_OP: operations.add(new Translog.NoOp(seqNo, primaryTerm, "test")); @@ -68,13 +69,19 @@ public void testPrimaryTermFromFollower() throws IOException { } final TransportWriteAction.WritePrimaryResult result = - TransportBulkShardOperationsAction.shardOperationOnPrimary(followerPrimary.shardId(), followerPrimary.getHistoryUUID(), - operations, - numOps - 1, followerPrimary, logger); + TransportBulkShardOperationsAction.shardOperationOnPrimary( + followerPrimary.shardId(), + followerPrimary.getHistoryUUID(), + operations, + numOps - 1, + followerPrimary, + logger + ); boolean accessStats = randomBoolean(); - try (Translog.Snapshot snapshot = - followerPrimary.newChangesSnapshot("test", 0, Long.MAX_VALUE, false, randomBoolean(), accessStats)) { + try ( + Translog.Snapshot snapshot = followerPrimary.newChangesSnapshot("test", 0, Long.MAX_VALUE, false, randomBoolean(), accessStats) + ) { if (accessStats) { assertThat(snapshot.totalOperations(), equalTo(operations.size())); } @@ -124,10 +131,18 @@ public void testPrimaryResultIncludeOnlyAppliedOperations() throws Exception { Randomness.shuffle(secondBulk); oldPrimary.advanceMaxSeqNoOfUpdatesOrDeletes(seqno); final TransportWriteAction.WritePrimaryResult fullResult = - TransportBulkShardOperationsAction.shardOperationOnPrimary(oldPrimary.shardId(), - oldPrimary.getHistoryUUID(), firstBulk, seqno, oldPrimary, logger); - assertThat(fullResult.replicaRequest().getOperations(), - equalTo(firstBulk.stream().map(op -> rewriteOperationWithPrimaryTerm(op, oldPrimaryTerm)).collect(Collectors.toList()))); + TransportBulkShardOperationsAction.shardOperationOnPrimary( + oldPrimary.shardId(), + oldPrimary.getHistoryUUID(), + firstBulk, + seqno, + oldPrimary, + logger + ); + assertThat( + fullResult.replicaRequest().getOperations(), + equalTo(firstBulk.stream().map(op -> rewriteOperationWithPrimaryTerm(op, oldPrimaryTerm)).collect(Collectors.toList())) + ); primaryTerm = randomLongBetween(primaryTerm, primaryTerm + 10); final IndexShard newPrimary = reinitShard(oldPrimary); DiscoveryNode localNode = new DiscoveryNode("foo", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT); @@ -139,9 +154,14 @@ public void testPrimaryResultIncludeOnlyAppliedOperations() throws Exception { // only a subset of these operations will be included the result but with the old primary term. final List existingOps = randomSubsetOf(firstBulk); final TransportWriteAction.WritePrimaryResult partialResult = - TransportBulkShardOperationsAction.shardOperationOnPrimary(newPrimary.shardId(), - newPrimary.getHistoryUUID(), Stream.concat(secondBulk.stream(), existingOps.stream()).collect(Collectors.toList()), - seqno, newPrimary, logger); + TransportBulkShardOperationsAction.shardOperationOnPrimary( + newPrimary.shardId(), + newPrimary.getHistoryUUID(), + Stream.concat(secondBulk.stream(), existingOps.stream()).collect(Collectors.toList()), + seqno, + newPrimary, + logger + ); final long newPrimaryTerm = newPrimary.getOperationPrimaryTerm(); final long globalCheckpoint = newPrimary.getLastKnownGlobalCheckpoint(); final List appliedOperations = Stream.concat( diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/allocation/CcrPrimaryFollowerAllocationDeciderTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/allocation/CcrPrimaryFollowerAllocationDeciderTests.java index c26dbdd0aec96..815c7fc4eba67 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/allocation/CcrPrimaryFollowerAllocationDeciderTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/allocation/CcrPrimaryFollowerAllocationDeciderTests.java @@ -8,6 +8,7 @@ package org.elasticsearch.xpack.ccr.allocation; import com.carrotsearch.hppc.IntHashSet; + import org.elasticsearch.Version; import org.elasticsearch.cluster.ClusterInfo; import org.elasticsearch.cluster.ClusterName; @@ -48,8 +49,10 @@ public class CcrPrimaryFollowerAllocationDeciderTests extends ESAllocationTestCa public void testRegularIndex() { String index = "test-index"; - IndexMetadata.Builder indexMetadata = IndexMetadata.builder(index).settings(settings(Version.CURRENT)) - .numberOfShards(1).numberOfReplicas(1); + IndexMetadata.Builder indexMetadata = IndexMetadata.builder(index) + .settings(settings(Version.CURRENT)) + .numberOfShards(1) + .numberOfReplicas(1); List nodes = new ArrayList<>(); for (int i = 0; i < 2; i++) { final Set roles = new HashSet<>(); @@ -73,7 +76,10 @@ public void testRegularIndex() { routingTable.addAsRestore(metadata.index(index), newSnapshotRecoverySource()); } ClusterState clusterState = ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)) - .nodes(DiscoveryNodes.EMPTY_NODES).metadata(metadata).routingTable(routingTable.build()).build(); + .nodes(DiscoveryNodes.EMPTY_NODES) + .metadata(metadata) + .routingTable(routingTable.build()) + .build(); for (int i = 0; i < clusterState.routingTable().index(index).shards().size(); i++) { IndexShardRoutingTable shardRouting = clusterState.routingTable().index(index).shard(i); assertThat(shardRouting.size(), equalTo(2)); @@ -94,7 +100,8 @@ public void testAlreadyBootstrappedFollowerIndex() { String index = "test-index"; IndexMetadata.Builder indexMetadata = IndexMetadata.builder(index) .settings(settings(Version.CURRENT).put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true)) - .numberOfShards(1).numberOfReplicas(1); + .numberOfShards(1) + .numberOfReplicas(1); List nodes = new ArrayList<>(); for (int i = 0; i < 2; i++) { final Set roles = new HashSet<>(); @@ -109,15 +116,20 @@ public void testAlreadyBootstrappedFollowerIndex() { Metadata metadata = Metadata.builder().put(indexMetadata).build(); RoutingTable.Builder routingTable = RoutingTable.builder().addAsRecovery(metadata.index(index)); ClusterState clusterState = ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)) - .nodes(discoveryNodes).metadata(metadata).routingTable(routingTable.build()).build(); + .nodes(discoveryNodes) + .metadata(metadata) + .routingTable(routingTable.build()) + .build(); for (int i = 0; i < clusterState.routingTable().index(index).shards().size(); i++) { IndexShardRoutingTable shardRouting = clusterState.routingTable().index(index).shard(i); assertThat(shardRouting.size(), equalTo(2)); assertThat(shardRouting.primaryShard().state(), equalTo(UNASSIGNED)); Decision decision = executeAllocation(clusterState, shardRouting.primaryShard(), randomFrom(nodes)); assertThat(decision.type(), equalTo(Decision.Type.YES)); - assertThat(decision.getExplanation(), - equalTo("shard is a primary follower but was bootstrapped already; hence is not under the purview of this decider")); + assertThat( + decision.getExplanation(), + equalTo("shard is a primary follower but was bootstrapped already; hence is not under the purview of this decider") + ); for (ShardRouting replica : shardRouting.replicaShards()) { assertThat(replica.state(), equalTo(UNASSIGNED)); decision = executeAllocation(clusterState, replica, randomFrom(nodes)); @@ -131,7 +143,8 @@ public void testBootstrappingFollowerIndex() { String index = "test-index"; IndexMetadata.Builder indexMetadata = IndexMetadata.builder(index) .settings(settings(Version.CURRENT).put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true)) - .numberOfShards(1).numberOfReplicas(1); + .numberOfShards(1) + .numberOfReplicas(1); DiscoveryNode dataOnlyNode = newNode("d1", Set.of(DiscoveryNodeRole.DATA_ROLE)); DiscoveryNode dataAndRemoteNode = newNode("dr1", Set.of(DiscoveryNodeRole.DATA_ROLE, DiscoveryNodeRole.REMOTE_CLUSTER_CLIENT_ROLE)); DiscoveryNodes discoveryNodes = DiscoveryNodes.builder().add(dataOnlyNode).add(dataAndRemoteNode).build(); @@ -139,15 +152,20 @@ public void testBootstrappingFollowerIndex() { RoutingTable.Builder routingTable = RoutingTable.builder() .addAsNewRestore(metadata.index(index), newSnapshotRecoverySource(), new IntHashSet()); ClusterState clusterState = ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)) - .nodes(discoveryNodes).metadata(metadata).routingTable(routingTable.build()).build(); + .nodes(discoveryNodes) + .metadata(metadata) + .routingTable(routingTable.build()) + .build(); for (int i = 0; i < clusterState.routingTable().index(index).shards().size(); i++) { IndexShardRoutingTable shardRouting = clusterState.routingTable().index(index).shard(i); assertThat(shardRouting.size(), equalTo(2)); assertThat(shardRouting.primaryShard().state(), equalTo(UNASSIGNED)); Decision noDecision = executeAllocation(clusterState, shardRouting.primaryShard(), dataOnlyNode); assertThat(noDecision.type(), equalTo(Decision.Type.NO)); - assertThat(noDecision.getExplanation(), - equalTo("shard is a primary follower and being bootstrapped, but node does not have the remote_cluster_client role")); + assertThat( + noDecision.getExplanation(), + equalTo("shard is a primary follower and being bootstrapped, but node does not have the remote_cluster_client role") + ); Decision yesDecision = executeAllocation(clusterState, shardRouting.primaryShard(), dataAndRemoteNode); assertThat(yesDecision.type(), equalTo(Decision.Type.YES)); assertThat(yesDecision.getExplanation(), equalTo("shard is a primary follower and node has the remote_cluster_client role")); @@ -155,23 +173,35 @@ public void testBootstrappingFollowerIndex() { assertThat(replica.state(), equalTo(UNASSIGNED)); yesDecision = executeAllocation(clusterState, replica, randomFrom(dataOnlyNode, dataAndRemoteNode)); assertThat(yesDecision.type(), equalTo(Decision.Type.YES)); - assertThat(yesDecision.getExplanation(), - equalTo("shard is a replica follower and is not under the purview of this decider")); + assertThat( + yesDecision.getExplanation(), + equalTo("shard is a replica follower and is not under the purview of this decider") + ); } } } static Decision executeAllocation(ClusterState clusterState, ShardRouting shardRouting, DiscoveryNode node) { final AllocationDecider decider = new CcrPrimaryFollowerAllocationDecider(); - final RoutingAllocation routingAllocation = new RoutingAllocation(new AllocationDeciders(List.of(decider)), - new RoutingNodes(clusterState), clusterState, ClusterInfo.EMPTY, SnapshotShardSizeInfo.EMPTY, System.nanoTime()); + final RoutingAllocation routingAllocation = new RoutingAllocation( + new AllocationDeciders(List.of(decider)), + new RoutingNodes(clusterState), + clusterState, + ClusterInfo.EMPTY, + SnapshotShardSizeInfo.EMPTY, + System.nanoTime() + ); routingAllocation.debugDecision(true); return decider.canAllocate(shardRouting, new RoutingNode(node.getId(), node), routingAllocation); } static RecoverySource.SnapshotRecoverySource newSnapshotRecoverySource() { Snapshot snapshot = new Snapshot("repo", new SnapshotId("name", "_uuid")); - return new RecoverySource.SnapshotRecoverySource(UUIDs.randomBase64UUID(), snapshot, Version.CURRENT, - new IndexId("test", UUIDs.randomBase64UUID(random()))); + return new RecoverySource.SnapshotRecoverySource( + UUIDs.randomBase64UUID(), + snapshot, + Version.CURRENT, + new IndexId("test", UUIDs.randomBase64UUID(random())) + ); } } diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/index/engine/FollowEngineIndexShardTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/index/engine/FollowEngineIndexShardTests.java index ef48a5032f24f..4b7418cc546e5 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/index/engine/FollowEngineIndexShardTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/index/engine/FollowEngineIndexShardTests.java @@ -19,9 +19,8 @@ import org.elasticsearch.cluster.routing.ShardRoutingState; import org.elasticsearch.common.UUIDs; import org.elasticsearch.common.bytes.BytesArray; -import org.elasticsearch.core.Releasable; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.core.Releasable; import org.elasticsearch.index.engine.EngineTestCase; import org.elasticsearch.index.mapper.SourceToParse; import org.elasticsearch.index.shard.IndexShard; @@ -34,6 +33,7 @@ import org.elasticsearch.snapshots.Snapshot; import org.elasticsearch.snapshots.SnapshotId; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.ccr.CcrSettings; import java.io.IOException; @@ -56,37 +56,57 @@ public void testDoNotFillGaps() throws Exception { long seqNo = -1; for (int i = 0; i < 8; i++) { final String id = Long.toString(i); - SourceToParse sourceToParse = new SourceToParse(indexShard.shardId().getIndexName(), id, - new BytesArray("{}"), XContentType.JSON); - indexShard.applyIndexOperationOnReplica(++seqNo, indexShard.getOperationPrimaryTerm(), 1, - IndexRequest.UNSET_AUTO_GENERATED_TIMESTAMP, false, sourceToParse); + SourceToParse sourceToParse = new SourceToParse( + indexShard.shardId().getIndexName(), + id, + new BytesArray("{}"), + XContentType.JSON + ); + indexShard.applyIndexOperationOnReplica( + ++seqNo, + indexShard.getOperationPrimaryTerm(), + 1, + IndexRequest.UNSET_AUTO_GENERATED_TIMESTAMP, + false, + sourceToParse + ); } long seqNoBeforeGap = seqNo; seqNo += 8; - SourceToParse sourceToParse = new SourceToParse(indexShard.shardId().getIndexName(), "9", - new BytesArray("{}"), XContentType.JSON); - indexShard.applyIndexOperationOnReplica(seqNo, indexShard.getOperationPrimaryTerm(), 1, IndexRequest.UNSET_AUTO_GENERATED_TIMESTAMP, - false, sourceToParse); + SourceToParse sourceToParse = new SourceToParse(indexShard.shardId().getIndexName(), "9", new BytesArray("{}"), XContentType.JSON); + indexShard.applyIndexOperationOnReplica( + seqNo, + indexShard.getOperationPrimaryTerm(), + 1, + IndexRequest.UNSET_AUTO_GENERATED_TIMESTAMP, + false, + sourceToParse + ); // promote the replica to primary: final ShardRouting replicaRouting = indexShard.routingEntry(); - final ShardRouting primaryRouting = - newShardRouting( - replicaRouting.shardId(), - replicaRouting.currentNodeId(), - null, - true, - ShardRoutingState.STARTED, - replicaRouting.allocationId()); - indexShard.updateShardState(primaryRouting, indexShard.getOperationPrimaryTerm() + 1, (shard, listener) -> {}, - 0L, Collections.singleton(primaryRouting.allocationId().getId()), - new IndexShardRoutingTable.Builder(primaryRouting.shardId()).addShard(primaryRouting).build()); + final ShardRouting primaryRouting = newShardRouting( + replicaRouting.shardId(), + replicaRouting.currentNodeId(), + null, + true, + ShardRoutingState.STARTED, + replicaRouting.allocationId() + ); + indexShard.updateShardState( + primaryRouting, + indexShard.getOperationPrimaryTerm() + 1, + (shard, listener) -> {}, + 0L, + Collections.singleton(primaryRouting.allocationId().getId()), + new IndexShardRoutingTable.Builder(primaryRouting.shardId()).addShard(primaryRouting).build() + ); final CountDownLatch latch = new CountDownLatch(1); ActionListener actionListener = ActionListener.wrap(releasable -> { releasable.close(); latch.countDown(); - }, e -> {assert false : "expected no exception, but got [" + e.getMessage() + "]";}); + }, e -> { assert false : "expected no exception, but got [" + e.getMessage() + "]"; }); indexShard.acquirePrimaryOperationPermit(actionListener, ThreadPool.Names.GENERIC, ""); latch.await(); assertThat(indexShard.getLocalCheckpoint(), equalTo(seqNoBeforeGap)); @@ -97,9 +117,7 @@ public void testDoNotFillGaps() throws Exception { public void testRestoreShard() throws IOException { final IndexShard source = newStartedShard(true, Settings.EMPTY); - final Settings targetSettings = Settings.builder() - .put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true) - .build(); + final Settings targetSettings = Settings.builder().put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true).build(); IndexShard target = newStartedShard(true, targetSettings, new FollowingEngineFactory()); assertThat(IndexShardTestCase.getEngine(target), instanceOf(FollowingEngine.class)); @@ -110,12 +128,20 @@ public void testRestoreShard() throws IOException { source.refresh("test"); } flushShard(source); // only flush source - ShardRouting routing = ShardRoutingHelper.initWithSameId(target.routingEntry(), - RecoverySource.ExistingStoreRecoverySource.INSTANCE); + ShardRouting routing = ShardRoutingHelper.initWithSameId( + target.routingEntry(), + RecoverySource.ExistingStoreRecoverySource.INSTANCE + ); final Snapshot snapshot = new Snapshot("foo", new SnapshotId("bar", UUIDs.randomBase64UUID())); - routing = ShardRoutingHelper.newWithRestoreSource(routing, - new RecoverySource.SnapshotRecoverySource(UUIDs.randomBase64UUID(), snapshot, Version.CURRENT, - new IndexId("test", UUIDs.randomBase64UUID(random())))); + routing = ShardRoutingHelper.newWithRestoreSource( + routing, + new RecoverySource.SnapshotRecoverySource( + UUIDs.randomBase64UUID(), + snapshot, + Version.CURRENT, + new IndexId("test", UUIDs.randomBase64UUID(random())) + ) + ); target = reinitShard(target, routing); Store sourceStore = source.store(); Store targetStore = target.store(); @@ -125,8 +151,14 @@ public void testRestoreShard() throws IOException { final PlainActionFuture future = PlainActionFuture.newFuture(); target.restoreFromRepository(new RestoreOnlyRepository("test") { @Override - public void restoreShard(Store store, SnapshotId snapshotId, IndexId indexId, ShardId snapshotShardId, - RecoveryState recoveryState, ActionListener listener) { + public void restoreShard( + Store store, + SnapshotId snapshotId, + IndexId indexId, + ShardId snapshotShardId, + RecoveryState recoveryState, + ActionListener listener + ) { ActionListener.completeWith(listener, () -> { cleanLuceneIndex(targetStore.directory()); for (String file : sourceStore.directory().listAll()) { diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/index/engine/FollowingEngineTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/index/engine/FollowingEngineTests.java index c9b2e82ddb466..970be2675f9a2 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/index/engine/FollowingEngineTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/index/engine/FollowingEngineTests.java @@ -90,11 +90,10 @@ public void tearDown() throws Exception { } public void testFollowingEngineRejectsNonFollowingIndex() throws IOException { - final Settings.Builder builder = - Settings.builder() - .put("index.number_of_shards", 1) - .put("index.number_of_replicas", 0) - .put("index.version.created", Version.CURRENT); + final Settings.Builder builder = Settings.builder() + .put("index.number_of_shards", 1) + .put("index.number_of_replicas", 0) + .put("index.version.created", Version.CURRENT); if (randomBoolean()) { builder.put("index.xpack.ccr.following_index", false); } @@ -110,13 +109,10 @@ public void testFollowingEngineRejectsNonFollowingIndex() throws IOException { public void testIndexSeqNoIsMaintained() throws IOException { final long seqNo = randomIntBetween(0, Integer.MAX_VALUE); - runIndexTest( - seqNo, - Engine.Operation.Origin.PRIMARY, - (followingEngine, index) -> { - final Engine.IndexResult result = followingEngine.index(index); - assertThat(result.getSeqNo(), equalTo(seqNo)); - }); + runIndexTest(seqNo, Engine.Operation.Origin.PRIMARY, (followingEngine, index) -> { + final Engine.IndexResult result = followingEngine.index(index); + assertThat(result.getSeqNo(), equalTo(seqNo)); + }); } /* @@ -125,20 +121,18 @@ public void testIndexSeqNoIsMaintained() throws IOException { * ensures that these semantics are maintained. */ public void testOutOfOrderDocuments() throws IOException { - final Settings settings = - Settings.builder() - .put("index.number_of_shards", 1) - .put("index.number_of_replicas", 0) - .put("index.version.created", Version.CURRENT) - .put("index.xpack.ccr.following_index", true) - .build(); + final Settings settings = Settings.builder() + .put("index.number_of_shards", 1) + .put("index.number_of_replicas", 0) + .put("index.version.created", Version.CURRENT) + .put("index.xpack.ccr.following_index", true) + .build(); final IndexMetadata indexMetadata = IndexMetadata.builder(index.getName()).settings(settings).build(); final IndexSettings indexSettings = new IndexSettings(indexMetadata, settings); try (Store store = createStore(shardId, indexSettings, newDirectory())) { final EngineConfig engineConfig = engineConfig(shardId, indexSettings, threadPool, store); try (FollowingEngine followingEngine = createEngine(store, engineConfig)) { - final VersionType versionType = - randomFrom(VersionType.INTERNAL, VersionType.EXTERNAL, VersionType.EXTERNAL_GTE); + final VersionType versionType = randomFrom(VersionType.INTERNAL, VersionType.EXTERNAL, VersionType.EXTERNAL_GTE); final List ops = EngineTestCase.generateSingleDocHistory(true, versionType, 2, 2, 20, "id"); ops.stream().mapToLong(op -> op.seqNo()).max().ifPresent(followingEngine::advanceMaxSeqNoOfUpdatesOrDeletes); EngineTestCase.assertOpsOnReplica(ops, followingEngine, true, logger); @@ -147,16 +141,16 @@ public void testOutOfOrderDocuments() throws IOException { } public void runIndexTest( - final long seqNo, - final Engine.Operation.Origin origin, - final CheckedBiConsumer consumer) throws IOException { - final Settings settings = - Settings.builder() - .put("index.number_of_shards", 1) - .put("index.number_of_replicas", 0) - .put("index.version.created", Version.CURRENT) - .put("index.xpack.ccr.following_index", true) - .build(); + final long seqNo, + final Engine.Operation.Origin origin, + final CheckedBiConsumer consumer + ) throws IOException { + final Settings settings = Settings.builder() + .put("index.number_of_shards", 1) + .put("index.number_of_replicas", 0) + .put("index.version.created", Version.CURRENT) + .put("index.xpack.ccr.following_index", true) + .build(); final IndexMetadata indexMetadata = IndexMetadata.builder(index.getName()).settings(settings).build(); final IndexSettings indexSettings = new IndexSettings(indexMetadata, settings); try (Store store = createStore(shardId, indexSettings, newDirectory())) { @@ -170,27 +164,24 @@ public void runIndexTest( public void testDeleteSeqNoIsMaintained() throws IOException { final long seqNo = randomIntBetween(0, Integer.MAX_VALUE); - runDeleteTest( - seqNo, - Engine.Operation.Origin.PRIMARY, - (followingEngine, delete) -> { - followingEngine.advanceMaxSeqNoOfUpdatesOrDeletes(randomLongBetween(seqNo, Long.MAX_VALUE)); - final Engine.DeleteResult result = followingEngine.delete(delete); - assertThat(result.getSeqNo(), equalTo(seqNo)); - }); + runDeleteTest(seqNo, Engine.Operation.Origin.PRIMARY, (followingEngine, delete) -> { + followingEngine.advanceMaxSeqNoOfUpdatesOrDeletes(randomLongBetween(seqNo, Long.MAX_VALUE)); + final Engine.DeleteResult result = followingEngine.delete(delete); + assertThat(result.getSeqNo(), equalTo(seqNo)); + }); } public void runDeleteTest( - final long seqNo, - final Engine.Operation.Origin origin, - final CheckedBiConsumer consumer) throws IOException { - final Settings settings = - Settings.builder() - .put("index.number_of_shards", 1) - .put("index.number_of_replicas", 0) - .put("index.version.created", Version.CURRENT) - .put("index.xpack.ccr.following_index", true) - .build(); + final long seqNo, + final Engine.Operation.Origin origin, + final CheckedBiConsumer consumer + ) throws IOException { + final Settings settings = Settings.builder() + .put("index.number_of_shards", 1) + .put("index.number_of_replicas", 0) + .put("index.version.created", Version.CURRENT) + .put("index.xpack.ccr.following_index", true) + .build(); final IndexMetadata indexMetadata = IndexMetadata.builder(index.getName()).settings(settings).build(); final IndexSettings indexSettings = new IndexSettings(indexMetadata, settings); try (Store store = createStore(shardId, indexSettings, newDirectory())) { @@ -198,15 +189,17 @@ public void runDeleteTest( try (FollowingEngine followingEngine = createEngine(store, engineConfig)) { final String id = "id"; final Engine.Delete delete = new Engine.Delete( - id, - new Term("_id", id), - seqNo, - primaryTerm.get(), - randomNonNegativeLong(), - VersionType.EXTERNAL, - origin, - System.currentTimeMillis(), - SequenceNumbers.UNASSIGNED_SEQ_NO, 0); + id, + new Term("_id", id), + seqNo, + primaryTerm.get(), + randomNonNegativeLong(), + VersionType.EXTERNAL, + origin, + System.currentTimeMillis(), + SequenceNumbers.UNASSIGNED_SEQ_NO, + 0 + ); consumer.accept(followingEngine, delete); } @@ -214,13 +207,12 @@ public void runDeleteTest( } public void testDoNotFillSeqNoGaps() throws Exception { - final Settings settings = - Settings.builder() - .put("index.number_of_shards", 1) - .put("index.number_of_replicas", 0) - .put("index.version.created", Version.CURRENT) - .put("index.xpack.ccr.following_index", true) - .build(); + final Settings settings = Settings.builder() + .put("index.number_of_shards", 1) + .put("index.number_of_replicas", 0) + .put("index.version.created", Version.CURRENT) + .put("index.xpack.ccr.following_index", true) + .build(); final IndexMetadata indexMetadata = IndexMetadata.builder(index.getName()).settings(settings).build(); final IndexSettings indexSettings = new IndexSettings(indexMetadata, settings); try (Store store = createStore(shardId, indexSettings, newDirectory())) { @@ -234,53 +226,58 @@ public void testDoNotFillSeqNoGaps() throws Exception { } private EngineConfig engineConfig( - final ShardId shardId, - final IndexSettings indexSettings, - final ThreadPool threadPool, - final Store store) { + final ShardId shardId, + final IndexSettings indexSettings, + final ThreadPool threadPool, + final Store store + ) { final IndexWriterConfig indexWriterConfig = newIndexWriterConfig(); final Path translogPath = createTempDir("translog"); final TranslogConfig translogConfig = new TranslogConfig(shardId, translogPath, indexSettings, BigArrays.NON_RECYCLING_INSTANCE); return new EngineConfig( - shardId, - threadPool, - indexSettings, - null, - store, - newMergePolicy(), - indexWriterConfig.getAnalyzer(), - indexWriterConfig.getSimilarity(), - new CodecService(null), - new Engine.EventListener() { - @Override - public void onFailedEngine(String reason, Exception e) { + shardId, + threadPool, + indexSettings, + null, + store, + newMergePolicy(), + indexWriterConfig.getAnalyzer(), + indexWriterConfig.getSimilarity(), + new CodecService(null), + new Engine.EventListener() { + @Override + public void onFailedEngine(String reason, Exception e) { - } - }, - IndexSearcher.getDefaultQueryCache(), - IndexSearcher.getDefaultQueryCachingPolicy(), - translogConfig, - TimeValue.timeValueMinutes(5), - Collections.emptyList(), - Collections.emptyList(), - null, - new NoneCircuitBreakerService(), - globalCheckpoint::longValue, - () -> RetentionLeases.EMPTY, - () -> primaryTerm.get(), - IndexModule.DEFAULT_SNAPSHOT_COMMIT_SUPPLIER, - null); + } + }, + IndexSearcher.getDefaultQueryCache(), + IndexSearcher.getDefaultQueryCachingPolicy(), + translogConfig, + TimeValue.timeValueMinutes(5), + Collections.emptyList(), + Collections.emptyList(), + null, + new NoneCircuitBreakerService(), + globalCheckpoint::longValue, + () -> RetentionLeases.EMPTY, + () -> primaryTerm.get(), + IndexModule.DEFAULT_SNAPSHOT_COMMIT_SUPPLIER, + null + ); } - private static Store createStore( - final ShardId shardId, final IndexSettings indexSettings, final Directory directory) { + private static Store createStore(final ShardId shardId, final IndexSettings indexSettings, final Directory directory) { return new Store(shardId, indexSettings, directory, new DummyShardLock(shardId)); } private FollowingEngine createEngine(Store store, EngineConfig config) throws IOException { store.createEmpty(); - final String translogUuid = Translog.createEmptyTranslog(config.getTranslogConfig().getTranslogPath(), - SequenceNumbers.NO_OPS_PERFORMED, shardId, 1L); + final String translogUuid = Translog.createEmptyTranslog( + config.getTranslogConfig().getTranslogPath(), + SequenceNumbers.NO_OPS_PERFORMED, + shardId, + 1L + ); store.associateIndexWithNewTranslog(translogUuid); FollowingEngine followingEngine = new FollowingEngine(config); TranslogHandler translogHandler = new TranslogHandler(xContentRegistry(), config.getIndexSettings()); @@ -295,14 +292,33 @@ private Engine.Index indexForFollowing(String id, long seqNo, Engine.Operation.O private Engine.Index indexForFollowing(String id, long seqNo, Engine.Operation.Origin origin, long version) { final ParsedDocument parsedDocument = EngineTestCase.createParsedDoc(id, null); - return new Engine.Index(EngineTestCase.newUid(parsedDocument), parsedDocument, seqNo, primaryTerm.get(), version, - VersionType.EXTERNAL, origin, System.currentTimeMillis(), IndexRequest.UNSET_AUTO_GENERATED_TIMESTAMP, randomBoolean(), - SequenceNumbers.UNASSIGNED_SEQ_NO, 0); + return new Engine.Index( + EngineTestCase.newUid(parsedDocument), + parsedDocument, + seqNo, + primaryTerm.get(), + version, + VersionType.EXTERNAL, + origin, + System.currentTimeMillis(), + IndexRequest.UNSET_AUTO_GENERATED_TIMESTAMP, + randomBoolean(), + SequenceNumbers.UNASSIGNED_SEQ_NO, + 0 + ); } private Engine.Delete deleteForFollowing(String id, long seqNo, Engine.Operation.Origin origin, long version) { - return IndexShard.prepareDelete(id, seqNo, primaryTerm.get(), version, VersionType.EXTERNAL, - origin, SequenceNumbers.UNASSIGNED_SEQ_NO, SequenceNumbers.UNASSIGNED_PRIMARY_TERM); + return IndexShard.prepareDelete( + id, + seqNo, + primaryTerm.get(), + version, + VersionType.EXTERNAL, + origin, + SequenceNumbers.UNASSIGNED_SEQ_NO, + SequenceNumbers.UNASSIGNED_PRIMARY_TERM + ); } private Engine.Index indexForPrimary(String id) { @@ -315,19 +331,44 @@ private Engine.Delete deleteForPrimary(String id) { return new Engine.Delete(parsedDoc.id(), EngineTestCase.newUid(parsedDoc), primaryTerm.get()); } - private Engine.Result applyOperation(Engine engine, Engine.Operation op, - long primaryTerm, Engine.Operation.Origin origin) throws IOException { + private Engine.Result applyOperation(Engine engine, Engine.Operation op, long primaryTerm, Engine.Operation.Origin origin) + throws IOException { final VersionType versionType = origin == Engine.Operation.Origin.PRIMARY ? VersionType.EXTERNAL : null; final Engine.Result result; if (op instanceof Engine.Index) { Engine.Index index = (Engine.Index) op; - result = engine.index(new Engine.Index(index.uid(), index.parsedDoc(), index.seqNo(), primaryTerm, index.version(), - versionType, origin, index.startTime(), index.getAutoGeneratedIdTimestamp(), index.isRetry(), - index.getIfSeqNo(), index.getIfPrimaryTerm())); + result = engine.index( + new Engine.Index( + index.uid(), + index.parsedDoc(), + index.seqNo(), + primaryTerm, + index.version(), + versionType, + origin, + index.startTime(), + index.getAutoGeneratedIdTimestamp(), + index.isRetry(), + index.getIfSeqNo(), + index.getIfPrimaryTerm() + ) + ); } else if (op instanceof Engine.Delete) { Engine.Delete delete = (Engine.Delete) op; - result = engine.delete(new Engine.Delete(delete.id(), delete.uid(), delete.seqNo(), primaryTerm, - delete.version(), versionType, origin, delete.startTime(), delete.getIfSeqNo(), delete.getIfPrimaryTerm())); + result = engine.delete( + new Engine.Delete( + delete.id(), + delete.uid(), + delete.seqNo(), + primaryTerm, + delete.version(), + versionType, + origin, + delete.startTime(), + delete.getIfSeqNo(), + delete.getIfPrimaryTerm() + ) + ); } else { Engine.NoOp noOp = (Engine.NoOp) op; result = engine.noOp(new Engine.NoOp(noOp.seqNo(), primaryTerm, origin, noOp.startTime(), noOp.reason())); @@ -470,10 +511,10 @@ public void testConcurrentIndexOperationsWithDeletesCanAdvanceMaxSeqNoOfUpdates( IndexMetadata followerIndexMetadata = IndexMetadata.builder(index.getName()).settings(followerSettings).build(); IndexSettings followerIndexSettings = new IndexSettings(followerIndexMetadata, Settings.EMPTY); try (Store followerStore = createStore(shardId, followerIndexSettings, newDirectory())) { - EngineConfig followerConfig = - engineConfig(shardId, followerIndexSettings, threadPool, followerStore); + EngineConfig followerConfig = engineConfig(shardId, followerIndexSettings, threadPool, followerStore); followerStore.createEmpty(); - String translogUuid = Translog.createEmptyTranslog(followerConfig.getTranslogConfig().getTranslogPath(), + String translogUuid = Translog.createEmptyTranslog( + followerConfig.getTranslogConfig().getTranslogPath(), SequenceNumbers.NO_OPS_PERFORMED, shardId, 1L @@ -573,25 +614,35 @@ private void runFollowTest(CheckedBiConsumer nestedDocFunc = EngineTestCase.nestedParsedDocFactory(); @@ -669,16 +729,41 @@ public void testProcessOnceOnPrimary() throws Exception { String docId = Integer.toString(between(1, 100)); ParsedDocument doc = randomBoolean() ? EngineTestCase.createParsedDoc(docId, null) : nestedDocFunc.apply(docId, randomInt(3)); if (randomBoolean()) { - operations.add(new Engine.Index(EngineTestCase.newUid(doc), doc, i, primaryTerm.get(), 1L, - VersionType.EXTERNAL, Engine.Operation.Origin.PRIMARY, threadPool.relativeTimeInMillis(), -1, true, - SequenceNumbers.UNASSIGNED_SEQ_NO, 0)); + operations.add( + new Engine.Index( + EngineTestCase.newUid(doc), + doc, + i, + primaryTerm.get(), + 1L, + VersionType.EXTERNAL, + Engine.Operation.Origin.PRIMARY, + threadPool.relativeTimeInMillis(), + -1, + true, + SequenceNumbers.UNASSIGNED_SEQ_NO, + 0 + ) + ); } else if (randomBoolean()) { - operations.add(new Engine.Delete(doc.id(), EngineTestCase.newUid(doc), i, primaryTerm.get(), 1L, - VersionType.EXTERNAL, Engine.Operation.Origin.PRIMARY, threadPool.relativeTimeInMillis(), - SequenceNumbers.UNASSIGNED_SEQ_NO, 0)); + operations.add( + new Engine.Delete( + doc.id(), + EngineTestCase.newUid(doc), + i, + primaryTerm.get(), + 1L, + VersionType.EXTERNAL, + Engine.Operation.Origin.PRIMARY, + threadPool.relativeTimeInMillis(), + SequenceNumbers.UNASSIGNED_SEQ_NO, + 0 + ) + ); } else { - operations.add(new Engine.NoOp(i, primaryTerm.get(), Engine.Operation.Origin.PRIMARY, - threadPool.relativeTimeInMillis(), "test-" + i)); + operations.add( + new Engine.NoOp(i, primaryTerm.get(), Engine.Operation.Origin.PRIMARY, threadPool.relativeTimeInMillis(), "test-" + i) + ); } } Randomness.shuffle(operations); @@ -688,7 +773,7 @@ public void testProcessOnceOnPrimary() throws Exception { final EngineConfig engineConfig = engineConfig(shardId, indexSettings, threadPool, store); try (FollowingEngine followingEngine = createEngine(store, engineConfig)) { followingEngine.advanceMaxSeqNoOfUpdatesOrDeletes(operations.size() - 1L); - final Map operationWithTerms = new HashMap<>(); + final Map operationWithTerms = new HashMap<>(); for (Engine.Operation op : operations) { long term = randomLongBetween(1, oldTerm); Engine.Result result = applyOperation(followingEngine, op, term, randomFrom(Engine.Operation.Origin.values())); @@ -707,8 +792,11 @@ public void testProcessOnceOnPrimary() throws Exception { assertThat(result.getFailure(), instanceOf(AlreadyProcessedFollowingEngineException.class)); AlreadyProcessedFollowingEngineException failure = (AlreadyProcessedFollowingEngineException) result.getFailure(); if (op.seqNo() <= globalCheckpoint.get()) { - assertThat("should not look-up term for operations at most the global checkpoint", - failure.getExistingPrimaryTerm().isPresent(), equalTo(false)); + assertThat( + "should not look-up term for operations at most the global checkpoint", + failure.getExistingPrimaryTerm().isPresent(), + equalTo(false) + ); } else { assertThat(failure.getExistingPrimaryTerm().getAsLong(), equalTo(operationWithTerms.get(op.seqNo()))); } @@ -720,8 +808,10 @@ public void testProcessOnceOnPrimary() throws Exception { primaryTerm.set(newTerm); followingEngine.rollTranslogGeneration(); for (Engine.Operation op : operations) { - Engine.Operation.Origin nonPrimary = randomValueOtherThan(Engine.Operation.Origin.PRIMARY, - () -> randomFrom(Engine.Operation.Origin.values())); + Engine.Operation.Origin nonPrimary = randomValueOtherThan( + Engine.Operation.Origin.PRIMARY, + () -> randomFrom(Engine.Operation.Origin.values()) + ); Engine.Result result = applyOperation(followingEngine, op, newTerm, nonPrimary); assertThat(result.getResultType(), equalTo(Engine.Result.Type.SUCCESS)); } @@ -738,22 +828,23 @@ public void testProcessOnceOnPrimary() throws Exception { */ public void testVerifyShardBeforeIndexClosingIsNoOp() throws IOException { final long seqNo = randomIntBetween(0, Integer.MAX_VALUE); - runIndexTest( - seqNo, - Engine.Operation.Origin.PRIMARY, - (followingEngine, index) -> { - globalCheckpoint.set(randomNonNegativeLong()); - try { - followingEngine.verifyEngineBeforeIndexClosing(); - } catch (final IllegalStateException e) { - fail("Following engine pre-closing verifications failed"); - } - }); + runIndexTest(seqNo, Engine.Operation.Origin.PRIMARY, (followingEngine, index) -> { + globalCheckpoint.set(randomNonNegativeLong()); + try { + followingEngine.verifyEngineBeforeIndexClosing(); + } catch (final IllegalStateException e) { + fail("Following engine pre-closing verifications failed"); + } + }); } public void testMaxSeqNoInCommitUserData() throws Exception { - final Settings settings = Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0) - .put("index.version.created", Version.CURRENT).put("index.xpack.ccr.following_index", true).build(); + final Settings settings = Settings.builder() + .put("index.number_of_shards", 1) + .put("index.number_of_replicas", 0) + .put("index.version.created", Version.CURRENT) + .put("index.xpack.ccr.following_index", true) + .build(); final IndexMetadata indexMetadata = IndexMetadata.builder(index.getName()).settings(settings).build(); final IndexSettings indexSettings = new IndexSettings(indexMetadata, settings); try (Store store = createStore(shardId, indexSettings, newDirectory())) { diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/repository/CcrRepositoryRetentionLeaseTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/repository/CcrRepositoryRetentionLeaseTests.java index 3806699b51b44..15fbcd0c05611 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/repository/CcrRepositoryRetentionLeaseTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/repository/CcrRepositoryRetentionLeaseTests.java @@ -50,50 +50,50 @@ public class CcrRepositoryRetentionLeaseTests extends ESTestCase { public void testWhenRetentionLeaseAlreadyExistsWeTryToRenewIt() { final RepositoryMetadata repositoryMetadata = mock(RepositoryMetadata.class); when(repositoryMetadata.name()).thenReturn(CcrRepository.NAME_PREFIX); - final Set> settings = - Stream.concat( - ClusterSettings.BUILT_IN_CLUSTER_SETTINGS.stream(), - CcrSettings.getSettings().stream().filter(Setting::hasNodeScope)) - .collect(Collectors.toSet()); + final Set> settings = Stream.concat( + ClusterSettings.BUILT_IN_CLUSTER_SETTINGS.stream(), + CcrSettings.getSettings().stream().filter(Setting::hasNodeScope) + ).collect(Collectors.toSet()); final CcrRepository repository = new CcrRepository( - repositoryMetadata, - mock(Client.class), - new CcrLicenseChecker(() -> true, () -> true), - Settings.EMPTY, - new CcrSettings(Settings.EMPTY, new ClusterSettings(Settings.EMPTY, settings)), - mock(ThreadPool.class)); + repositoryMetadata, + mock(Client.class), + new CcrLicenseChecker(() -> true, () -> true), + Settings.EMPTY, + new CcrSettings(Settings.EMPTY, new ClusterSettings(Settings.EMPTY, settings)), + mock(ThreadPool.class) + ); final ShardId followerShardId = new ShardId(new Index("follower-index-name", "follower-index-uuid"), 0); final ShardId leaderShardId = new ShardId(new Index("leader-index-name", "leader-index-uuid"), 0); - final String retentionLeaseId = - retentionLeaseId("local-cluster", followerShardId.getIndex(), "remote-cluster", leaderShardId.getIndex()); + final String retentionLeaseId = retentionLeaseId( + "local-cluster", + followerShardId.getIndex(), + "remote-cluster", + leaderShardId.getIndex() + ); // simulate that the retention lease already exists on the leader, and verify that we attempt to renew it final Client remoteClient = mock(Client.class); - final ArgumentCaptor addRequestCaptor = - ArgumentCaptor.forClass(RetentionLeaseActions.AddRequest.class); - doAnswer( - invocationOnMock -> { - @SuppressWarnings("unchecked") final ActionListener listener = - (ActionListener) invocationOnMock.getArguments()[2]; - listener.onFailure(new RetentionLeaseAlreadyExistsException(retentionLeaseId)); - return null; - }) - .when(remoteClient) - .execute(same(RetentionLeaseActions.Add.INSTANCE), addRequestCaptor.capture(), any()); - final ArgumentCaptor renewRequestCaptor = - ArgumentCaptor.forClass(RetentionLeaseActions.RenewRequest.class); - doAnswer( - invocationOnMock -> { - @SuppressWarnings("unchecked") final ActionListener listener = - (ActionListener) invocationOnMock.getArguments()[2]; - listener.onResponse(ActionResponse.Empty.INSTANCE); - return null; - }) - .when(remoteClient) - .execute(same(RetentionLeaseActions.Renew.INSTANCE), renewRequestCaptor.capture(), any()); + final ArgumentCaptor addRequestCaptor = ArgumentCaptor.forClass( + RetentionLeaseActions.AddRequest.class + ); + doAnswer(invocationOnMock -> { + @SuppressWarnings("unchecked") + final ActionListener listener = (ActionListener) invocationOnMock.getArguments()[2]; + listener.onFailure(new RetentionLeaseAlreadyExistsException(retentionLeaseId)); + return null; + }).when(remoteClient).execute(same(RetentionLeaseActions.Add.INSTANCE), addRequestCaptor.capture(), any()); + final ArgumentCaptor renewRequestCaptor = ArgumentCaptor.forClass( + RetentionLeaseActions.RenewRequest.class + ); + doAnswer(invocationOnMock -> { + @SuppressWarnings("unchecked") + final ActionListener listener = (ActionListener) invocationOnMock.getArguments()[2]; + listener.onResponse(ActionResponse.Empty.INSTANCE); + return null; + }).when(remoteClient).execute(same(RetentionLeaseActions.Renew.INSTANCE), renewRequestCaptor.capture(), any()); repository.acquireRetentionLeaseOnLeader(followerShardId, retentionLeaseId, leaderShardId, remoteClient); @@ -115,67 +115,72 @@ public void testWhenRetentionLeaseAlreadyExistsWeTryToRenewIt() { public void testWhenRetentionLeaseExpiresBeforeWeCanRenewIt() { final RepositoryMetadata repositoryMetadata = mock(RepositoryMetadata.class); when(repositoryMetadata.name()).thenReturn(CcrRepository.NAME_PREFIX); - final Set> settings = - Stream.concat( - ClusterSettings.BUILT_IN_CLUSTER_SETTINGS.stream(), - CcrSettings.getSettings().stream().filter(Setting::hasNodeScope)) - .collect(Collectors.toSet()); + final Set> settings = Stream.concat( + ClusterSettings.BUILT_IN_CLUSTER_SETTINGS.stream(), + CcrSettings.getSettings().stream().filter(Setting::hasNodeScope) + ).collect(Collectors.toSet()); final CcrRepository repository = new CcrRepository( - repositoryMetadata, - mock(Client.class), - new CcrLicenseChecker(() -> true, () -> true), - Settings.EMPTY, - new CcrSettings(Settings.EMPTY, new ClusterSettings(Settings.EMPTY, settings)), - mock(ThreadPool.class)); + repositoryMetadata, + mock(Client.class), + new CcrLicenseChecker(() -> true, () -> true), + Settings.EMPTY, + new CcrSettings(Settings.EMPTY, new ClusterSettings(Settings.EMPTY, settings)), + mock(ThreadPool.class) + ); final ShardId followerShardId = new ShardId(new Index("follower-index-name", "follower-index-uuid"), 0); final ShardId leaderShardId = new ShardId(new Index("leader-index-name", "leader-index-uuid"), 0); - final String retentionLeaseId = - retentionLeaseId("local-cluster", followerShardId.getIndex(), "remote-cluster", leaderShardId.getIndex()); + final String retentionLeaseId = retentionLeaseId( + "local-cluster", + followerShardId.getIndex(), + "remote-cluster", + leaderShardId.getIndex() + ); // simulate that the retention lease already exists on the leader, expires before we renew, and verify that we attempt to add it final Client remoteClient = mock(Client.class); - final ArgumentCaptor addRequestCaptor = - ArgumentCaptor.forClass(RetentionLeaseActions.AddRequest.class); + final ArgumentCaptor addRequestCaptor = ArgumentCaptor.forClass( + RetentionLeaseActions.AddRequest.class + ); final PlainActionFuture response = new PlainActionFuture<>(); response.onResponse(ActionResponse.Empty.INSTANCE); - doAnswer( - new Answer() { - - final AtomicBoolean firstInvocation = new AtomicBoolean(true); - - @Override - public Void answer(final InvocationOnMock invocationOnMock) { - @SuppressWarnings("unchecked") final ActionListener listener = - (ActionListener) invocationOnMock.getArguments()[2]; - if (firstInvocation.compareAndSet(true, false)) { - listener.onFailure(new RetentionLeaseAlreadyExistsException(retentionLeaseId)); - } else { - listener.onResponse(ActionResponse.Empty.INSTANCE); - } - return null; - } - - }) - .when(remoteClient).execute(same(RetentionLeaseActions.Add.INSTANCE), addRequestCaptor.capture(), any()); - final ArgumentCaptor renewRequestCaptor = - ArgumentCaptor.forClass(RetentionLeaseActions.RenewRequest.class); - doAnswer( - invocationOnMock -> { - @SuppressWarnings("unchecked") final ActionListener listener = - (ActionListener) invocationOnMock.getArguments()[2]; - listener.onFailure(new RetentionLeaseNotFoundException(retentionLeaseId)); - return null; + doAnswer(new Answer() { + + final AtomicBoolean firstInvocation = new AtomicBoolean(true); + + @Override + public Void answer(final InvocationOnMock invocationOnMock) { + @SuppressWarnings("unchecked") + final ActionListener listener = (ActionListener) invocationOnMock + .getArguments()[2]; + if (firstInvocation.compareAndSet(true, false)) { + listener.onFailure(new RetentionLeaseAlreadyExistsException(retentionLeaseId)); + } else { + listener.onResponse(ActionResponse.Empty.INSTANCE); } - ).when(remoteClient) - .execute(same(RetentionLeaseActions.Renew.INSTANCE), renewRequestCaptor.capture(), any()); + return null; + } + + }).when(remoteClient).execute(same(RetentionLeaseActions.Add.INSTANCE), addRequestCaptor.capture(), any()); + final ArgumentCaptor renewRequestCaptor = ArgumentCaptor.forClass( + RetentionLeaseActions.RenewRequest.class + ); + doAnswer(invocationOnMock -> { + @SuppressWarnings("unchecked") + final ActionListener listener = (ActionListener) invocationOnMock.getArguments()[2]; + listener.onFailure(new RetentionLeaseNotFoundException(retentionLeaseId)); + return null; + }).when(remoteClient).execute(same(RetentionLeaseActions.Renew.INSTANCE), renewRequestCaptor.capture(), any()); repository.acquireRetentionLeaseOnLeader(followerShardId, retentionLeaseId, leaderShardId, remoteClient); - verify(remoteClient, times(2)) - .execute(same(RetentionLeaseActions.Add.INSTANCE), any(RetentionLeaseActions.AddRequest.class), any()); + verify(remoteClient, times(2)).execute( + same(RetentionLeaseActions.Add.INSTANCE), + any(RetentionLeaseActions.AddRequest.class), + any() + ); assertThat(addRequestCaptor.getValue().getShardId(), equalTo(leaderShardId)); assertThat(addRequestCaptor.getValue().getId(), equalTo(retentionLeaseId)); assertThat(addRequestCaptor.getValue().getRetainingSequenceNumber(), equalTo(RETAIN_ALL)); diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/repository/CcrRestoreSourceServiceTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/repository/CcrRestoreSourceServiceTests.java index 8637f0debd6c5..bf71e8a2bf59f 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/repository/CcrRestoreSourceServiceTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/repository/CcrRestoreSourceServiceTests.java @@ -36,8 +36,10 @@ public class CcrRestoreSourceServiceTests extends IndexShardTestCase { public void setUp() throws Exception { super.setUp(); taskQueue = new DeterministicTaskQueue(); - ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, CcrSettings.getSettings() - .stream().filter(s -> s.hasNodeScope()).collect(Collectors.toSet())); + ClusterSettings clusterSettings = new ClusterSettings( + Settings.EMPTY, + CcrSettings.getSettings().stream().filter(s -> s.hasNodeScope()).collect(Collectors.toSet()) + ); restoreSourceService = new CcrRestoreSourceService(taskQueue.getThreadPool(), new CcrSettings(Settings.EMPTY, clusterSettings)); } @@ -51,16 +53,20 @@ public void testOpenSession() throws IOException { restoreSourceService.openSession(sessionUUID1, indexShard1); restoreSourceService.openSession(sessionUUID2, indexShard1); - try (CcrRestoreSourceService.SessionReader reader1 = restoreSourceService.getSessionReader(sessionUUID1); - CcrRestoreSourceService.SessionReader reader2 = restoreSourceService.getSessionReader(sessionUUID2)) { + try ( + CcrRestoreSourceService.SessionReader reader1 = restoreSourceService.getSessionReader(sessionUUID1); + CcrRestoreSourceService.SessionReader reader2 = restoreSourceService.getSessionReader(sessionUUID2) + ) { // Would throw exception if missing } restoreSourceService.openSession(sessionUUID3, indexShard2); - try (CcrRestoreSourceService.SessionReader reader1 = restoreSourceService.getSessionReader(sessionUUID1); - CcrRestoreSourceService.SessionReader reader2 = restoreSourceService.getSessionReader(sessionUUID2); - CcrRestoreSourceService.SessionReader reader3 = restoreSourceService.getSessionReader(sessionUUID3)) { + try ( + CcrRestoreSourceService.SessionReader reader1 = restoreSourceService.getSessionReader(sessionUUID1); + CcrRestoreSourceService.SessionReader reader2 = restoreSourceService.getSessionReader(sessionUUID2); + CcrRestoreSourceService.SessionReader reader3 = restoreSourceService.getSessionReader(sessionUUID3) + ) { // Would throw exception if missing } @@ -89,9 +95,11 @@ public void testCloseSession() throws IOException { restoreSourceService.openSession(sessionUUID2, indexShard1); restoreSourceService.openSession(sessionUUID3, indexShard2); - try (CcrRestoreSourceService.SessionReader reader1 = restoreSourceService.getSessionReader(sessionUUID1); - CcrRestoreSourceService.SessionReader reader2 = restoreSourceService.getSessionReader(sessionUUID2); - CcrRestoreSourceService.SessionReader reader3 = restoreSourceService.getSessionReader(sessionUUID3)) { + try ( + CcrRestoreSourceService.SessionReader reader1 = restoreSourceService.getSessionReader(sessionUUID1); + CcrRestoreSourceService.SessionReader reader2 = restoreSourceService.getSessionReader(sessionUUID2); + CcrRestoreSourceService.SessionReader reader3 = restoreSourceService.getSessionReader(sessionUUID3) + ) { // Would throw exception if missing } @@ -124,9 +132,11 @@ public void testCloseShardListenerFunctionality() throws IOException { restoreSourceService.openSession(sessionUUID2, indexShard1); restoreSourceService.openSession(sessionUUID3, indexShard2); - try (CcrRestoreSourceService.SessionReader reader1 = restoreSourceService.getSessionReader(sessionUUID1); - CcrRestoreSourceService.SessionReader reader2 = restoreSourceService.getSessionReader(sessionUUID2); - CcrRestoreSourceService.SessionReader reader3 = restoreSourceService.getSessionReader(sessionUUID3)) { + try ( + CcrRestoreSourceService.SessionReader reader1 = restoreSourceService.getSessionReader(sessionUUID1); + CcrRestoreSourceService.SessionReader reader2 = restoreSourceService.getSessionReader(sessionUUID2); + CcrRestoreSourceService.SessionReader reader3 = restoreSourceService.getSessionReader(sessionUUID3) + ) { // Would throw exception if missing } diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/AutoFollowStatsMonitoringDocTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/AutoFollowStatsMonitoringDocTests.java index 308d8dfce8616..5a86f44bb5b90 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/AutoFollowStatsMonitoringDocTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/AutoFollowStatsMonitoringDocTests.java @@ -9,12 +9,12 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.time.DateFormatter; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.common.xcontent.support.XContentMapValues; +import org.elasticsearch.core.Tuple; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.ccr.AutoFollowStats; import org.elasticsearch.xpack.core.ccr.AutoFollowStats.AutoFollowedCluster; import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; @@ -44,18 +44,25 @@ public class AutoFollowStatsMonitoringDocTests extends BaseMonitoringDocTestCase @Before public void instantiateAutoFollowStats() { - autoFollowStats = new AutoFollowStats(randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), - Collections.emptyNavigableMap(), Collections.emptyNavigableMap()); + autoFollowStats = new AutoFollowStats( + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + Collections.emptyNavigableMap(), + Collections.emptyNavigableMap() + ); } @Override - protected AutoFollowStatsMonitoringDoc createMonitoringDoc(String cluster, - long timestamp, - long interval, - MonitoringDoc.Node node, - MonitoredSystem system, - String type, - String id) { + protected AutoFollowStatsMonitoringDoc createMonitoringDoc( + String cluster, + long timestamp, + long interval, + MonitoringDoc.Node node, + MonitoredSystem system, + String type, + String id + ) { return new AutoFollowStatsMonitoringDoc(cluster, timestamp, interval, node, autoFollowStats); } @@ -74,83 +81,115 @@ public void testToXContent() throws IOException { final long nodeTimestamp = System.currentTimeMillis(); final MonitoringDoc.Node node = new MonitoringDoc.Node("_uuid", "_host", "_addr", "_ip", "_name", nodeTimestamp); - final NavigableMap> recentAutoFollowExceptions = - new TreeMap<>(Collections.singletonMap( - randomAlphaOfLength(4), - Tuple.tuple(1L, new ElasticsearchException("cannot follow index")))); - - final NavigableMap trackingClusters = - new TreeMap<>(Collections.singletonMap( - randomAlphaOfLength(4), - new AutoFollowedCluster(1L, 1L))); - final AutoFollowStats autoFollowStats = - new AutoFollowStats(randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), recentAutoFollowExceptions, - trackingClusters); - - final AutoFollowStatsMonitoringDoc document = - new AutoFollowStatsMonitoringDoc("_cluster", timestamp, intervalMillis, node, autoFollowStats); + final NavigableMap> recentAutoFollowExceptions = new TreeMap<>( + Collections.singletonMap(randomAlphaOfLength(4), Tuple.tuple(1L, new ElasticsearchException("cannot follow index"))) + ); + + final NavigableMap trackingClusters = new TreeMap<>( + Collections.singletonMap(randomAlphaOfLength(4), new AutoFollowedCluster(1L, 1L)) + ); + final AutoFollowStats autoFollowStats = new AutoFollowStats( + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + recentAutoFollowExceptions, + trackingClusters + ); + + final AutoFollowStatsMonitoringDoc document = new AutoFollowStatsMonitoringDoc( + "_cluster", + timestamp, + intervalMillis, + node, + autoFollowStats + ); final BytesReference xContent = XContentHelper.toXContent(document, XContentType.JSON, false); assertThat( xContent.utf8ToString(), equalTo( "{" + "\"cluster_uuid\":\"_cluster\"," - + "\"timestamp\":\"" + DATE_TIME_FORMATTER.formatMillis(timestamp) + "\"," - + "\"interval_ms\":" + intervalMillis + "," + + "\"timestamp\":\"" + + DATE_TIME_FORMATTER.formatMillis(timestamp) + + "\"," + + "\"interval_ms\":" + + intervalMillis + + "," + "\"type\":\"ccr_auto_follow_stats\"," + "\"source_node\":{" - + "\"uuid\":\"_uuid\"," - + "\"host\":\"_host\"," - + "\"transport_address\":\"_addr\"," - + "\"ip\":\"_ip\"," - + "\"name\":\"_name\"," - + "\"timestamp\":\"" + DATE_TIME_FORMATTER.formatMillis(nodeTimestamp) + "\"" + + "\"uuid\":\"_uuid\"," + + "\"host\":\"_host\"," + + "\"transport_address\":\"_addr\"," + + "\"ip\":\"_ip\"," + + "\"name\":\"_name\"," + + "\"timestamp\":\"" + + DATE_TIME_FORMATTER.formatMillis(nodeTimestamp) + + "\"" + "}," + "\"ccr_auto_follow_stats\":{" - + "\"number_of_failed_follow_indices\":" + autoFollowStats.getNumberOfFailedFollowIndices() + "," - + "\"number_of_failed_remote_cluster_state_requests\":" + - autoFollowStats.getNumberOfFailedRemoteClusterStateRequests() + "," - + "\"number_of_successful_follow_indices\":" + autoFollowStats.getNumberOfSuccessfulFollowIndices() + "," - + "\"recent_auto_follow_errors\":[" - + "{" - + "\"leader_index\":\"" + recentAutoFollowExceptions.keySet().iterator().next() + "\"," - + "\"timestamp\":1," - + "\"auto_follow_exception\":{" - + "\"type\":\"exception\"," - + "\"reason\":\"cannot follow index\"" - + "}" - + "}" - + "]," - + "\"auto_followed_clusters\":[" - + "{" - + "\"cluster_name\":\"" + trackingClusters.keySet().iterator().next() + "\"," - + "\"time_since_last_check_millis\":" + - trackingClusters.values().iterator().next().getTimeSinceLastCheckMillis() + "," - + "\"last_seen_metadata_version\":" + - trackingClusters.values().iterator().next().getLastSeenMetadataVersion() - + "}" - + "]" + + "\"number_of_failed_follow_indices\":" + + autoFollowStats.getNumberOfFailedFollowIndices() + + "," + + "\"number_of_failed_remote_cluster_state_requests\":" + + autoFollowStats.getNumberOfFailedRemoteClusterStateRequests() + + "," + + "\"number_of_successful_follow_indices\":" + + autoFollowStats.getNumberOfSuccessfulFollowIndices() + + "," + + "\"recent_auto_follow_errors\":[" + + "{" + + "\"leader_index\":\"" + + recentAutoFollowExceptions.keySet().iterator().next() + + "\"," + + "\"timestamp\":1," + + "\"auto_follow_exception\":{" + + "\"type\":\"exception\"," + + "\"reason\":\"cannot follow index\"" + + "}" + + "}" + + "]," + + "\"auto_followed_clusters\":[" + + "{" + + "\"cluster_name\":\"" + + trackingClusters.keySet().iterator().next() + + "\"," + + "\"time_since_last_check_millis\":" + + trackingClusters.values().iterator().next().getTimeSinceLastCheckMillis() + + "," + + "\"last_seen_metadata_version\":" + + trackingClusters.values().iterator().next().getLastSeenMetadataVersion() + + "}" + + "]" + + "}" + "}" - + "}")); + ) + ); } public void testShardFollowNodeTaskStatusFieldsMapped() throws IOException { - final NavigableMap> fetchExceptions = - new TreeMap<>(Collections.singletonMap("leader_index", Tuple.tuple(1L, new ElasticsearchException("cannot follow index")))); - final NavigableMap trackingClusters = - new TreeMap<>(Collections.singletonMap( - randomAlphaOfLength(4), - new AutoFollowedCluster(1L, 1L))); + final NavigableMap> fetchExceptions = new TreeMap<>( + Collections.singletonMap("leader_index", Tuple.tuple(1L, new ElasticsearchException("cannot follow index"))) + ); + final NavigableMap trackingClusters = new TreeMap<>( + Collections.singletonMap(randomAlphaOfLength(4), new AutoFollowedCluster(1L, 1L)) + ); final AutoFollowStats status = new AutoFollowStats(1, 0, 2, fetchExceptions, trackingClusters); XContentBuilder builder = jsonBuilder(); builder.value(status); Map serializedStatus = XContentHelper.convertToMap(XContentType.JSON.xContent(), Strings.toString(builder), false); byte[] loadedTemplate = MonitoringTemplateRegistry.getTemplateConfigForMonitoredSystem(MonitoredSystem.ES).loadBytes(); - Map template = - XContentHelper.convertToMap(XContentType.JSON.xContent(), loadedTemplate, 0, loadedTemplate.length, false); - Map autoFollowStatsMapping = - (Map) XContentMapValues.extractValue("mappings._doc.properties.ccr_auto_follow_stats.properties", template); + Map template = XContentHelper.convertToMap( + XContentType.JSON.xContent(), + loadedTemplate, + 0, + loadedTemplate.length, + false + ); + Map autoFollowStatsMapping = (Map) XContentMapValues.extractValue( + "mappings._doc.properties.ccr_auto_follow_stats.properties", + template + ); assertThat(serializedStatus.size(), equalTo(autoFollowStatsMapping.size())); for (Map.Entry entry : serializedStatus.entrySet()) { @@ -161,11 +200,13 @@ public void testShardFollowNodeTaskStatusFieldsMapped() throws IOException { Object fieldValue = entry.getValue(); String fieldType = (String) fieldMapping.get("type"); if (fieldValue instanceof Long || fieldValue instanceof Integer) { - assertThat("expected long field type for field [" + fieldName + "]", fieldType, - anyOf(equalTo("long"), equalTo("integer"))); + assertThat("expected long field type for field [" + fieldName + "]", fieldType, anyOf(equalTo("long"), equalTo("integer"))); } else if (fieldValue instanceof String) { - assertThat("expected keyword field type for field [" + fieldName + "]", fieldType, - anyOf(equalTo("keyword"), equalTo("text"))); + assertThat( + "expected keyword field type for field [" + fieldName + "]", + fieldType, + anyOf(equalTo("keyword"), equalTo("text")) + ); } else { Map innerFieldValue = (Map) ((List) fieldValue).get(0); // Manual test specific object fields and if not just fail: @@ -177,8 +218,10 @@ public void testShardFollowNodeTaskStatusFieldsMapped() throws IOException { assertThat(XContentMapValues.extractValue("properties.auto_follow_exception.type", fieldMapping), equalTo("object")); innerFieldValue = (Map) innerFieldValue.get("auto_follow_exception"); - Map exceptionFieldMapping = - (Map) XContentMapValues.extractValue("properties.auto_follow_exception.properties", fieldMapping); + Map exceptionFieldMapping = (Map) XContentMapValues.extractValue( + "properties.auto_follow_exception.properties", + fieldMapping + ); assertThat(exceptionFieldMapping.size(), equalTo(innerFieldValue.size())); assertThat(XContentMapValues.extractValue("type.type", exceptionFieldMapping), equalTo("keyword")); assertThat(XContentMapValues.extractValue("reason.type", exceptionFieldMapping), equalTo("text")); diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/FollowStatsMonitoringDocTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/FollowStatsMonitoringDocTests.java index 06f0a78a36e3f..a561e4d79e0e4 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/FollowStatsMonitoringDocTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/FollowStatsMonitoringDocTests.java @@ -10,12 +10,12 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.time.DateFormatter; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.common.xcontent.support.XContentMapValues; +import org.elasticsearch.core.Tuple; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.ccr.ShardFollowNodeTaskStatus; import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc; @@ -52,20 +52,23 @@ public void setUp() throws Exception { } public void testConstructorStatusMustNotBeNull() { - final NullPointerException e = - expectThrows(NullPointerException.class, () -> new FollowStatsMonitoringDoc(cluster, timestamp, interval, node, null)); + final NullPointerException e = expectThrows( + NullPointerException.class, + () -> new FollowStatsMonitoringDoc(cluster, timestamp, interval, node, null) + ); assertThat(e, hasToString(containsString("status"))); } @Override protected FollowStatsMonitoringDoc createMonitoringDoc( - final String cluster, - final long timestamp, - final long interval, - final MonitoringDoc.Node node, - final MonitoredSystem system, - final String type, - final String id) { + final String cluster, + final long timestamp, + final long interval, + final MonitoringDoc.Node node, + final MonitoredSystem system, + final String type, + final String id + ) { return new FollowStatsMonitoringDoc(cluster, timestamp, interval, node, status); } @@ -107,105 +110,169 @@ public void testToXContent() throws IOException { final long successfulWriteRequests = randomNonNegativeLong(); final long failedWriteRequests = randomNonNegativeLong(); final long operationWritten = randomNonNegativeLong(); - final NavigableMap> fetchExceptions = - new TreeMap<>(Collections.singletonMap( - randomNonNegativeLong(), - Tuple.tuple(randomIntBetween(0, Integer.MAX_VALUE), new ElasticsearchException("shard is sad")))); + final NavigableMap> fetchExceptions = new TreeMap<>( + Collections.singletonMap( + randomNonNegativeLong(), + Tuple.tuple(randomIntBetween(0, Integer.MAX_VALUE), new ElasticsearchException("shard is sad")) + ) + ); final long timeSinceLastReadMillis = randomNonNegativeLong(); final ShardFollowNodeTaskStatus status = new ShardFollowNodeTaskStatus( - "leader_cluster", - "leader_index", - "follower_index", - shardId, - leaderGlobalCheckpoint, - leaderMaxSeqNo, - followerGlobalCheckpoint, - followerMaxSeqNo, - lastRequestedSeqNo, - numberOfConcurrentReads, - numberOfConcurrentWrites, - writeBufferOperationCount, - writeBufferSizeInBytes, - followerMappingVersion, - followerSettingsVersion, - followerAliasesVersion, - totalReadTimeMillis, - totalReadRemoteExecTimeMillis, - successfulReadRequests, - failedReadRequests, - operationsRead, - bytesRead, - totalWriteTimeMillis, - successfulWriteRequests, - failedWriteRequests, - operationWritten, - fetchExceptions, - timeSinceLastReadMillis, - new ElasticsearchException("fatal error")); + "leader_cluster", + "leader_index", + "follower_index", + shardId, + leaderGlobalCheckpoint, + leaderMaxSeqNo, + followerGlobalCheckpoint, + followerMaxSeqNo, + lastRequestedSeqNo, + numberOfConcurrentReads, + numberOfConcurrentWrites, + writeBufferOperationCount, + writeBufferSizeInBytes, + followerMappingVersion, + followerSettingsVersion, + followerAliasesVersion, + totalReadTimeMillis, + totalReadRemoteExecTimeMillis, + successfulReadRequests, + failedReadRequests, + operationsRead, + bytesRead, + totalWriteTimeMillis, + successfulWriteRequests, + failedWriteRequests, + operationWritten, + fetchExceptions, + timeSinceLastReadMillis, + new ElasticsearchException("fatal error") + ); final FollowStatsMonitoringDoc document = new FollowStatsMonitoringDoc("_cluster", timestamp, intervalMillis, node, status); final BytesReference xContent = XContentHelper.toXContent(document, XContentType.JSON, false); assertThat( - xContent.utf8ToString(), - equalTo( - "{" - + "\"cluster_uuid\":\"_cluster\"," - + "\"timestamp\":\"" + DATE_TIME_FORMATTER.formatMillis(timestamp) + "\"," - + "\"interval_ms\":" + intervalMillis + "," - + "\"type\":\"ccr_stats\"," - + "\"source_node\":{" - + "\"uuid\":\"_uuid\"," - + "\"host\":\"_host\"," - + "\"transport_address\":\"_addr\"," - + "\"ip\":\"_ip\"," - + "\"name\":\"_name\"," - + "\"timestamp\":\"" + DATE_TIME_FORMATTER.formatMillis(nodeTimestamp) + "\"" - + "}," - + "\"ccr_stats\":{" - + "\"remote_cluster\":\"leader_cluster\"," - + "\"leader_index\":\"leader_index\"," - + "\"follower_index\":\"follower_index\"," - + "\"shard_id\":" + shardId + "," - + "\"leader_global_checkpoint\":" + leaderGlobalCheckpoint + "," - + "\"leader_max_seq_no\":" + leaderMaxSeqNo + "," - + "\"follower_global_checkpoint\":" + followerGlobalCheckpoint + "," - + "\"follower_max_seq_no\":" + followerMaxSeqNo + "," - + "\"last_requested_seq_no\":" + lastRequestedSeqNo + "," - + "\"outstanding_read_requests\":" + numberOfConcurrentReads + "," - + "\"outstanding_write_requests\":" + numberOfConcurrentWrites + "," - + "\"write_buffer_operation_count\":" + writeBufferOperationCount + "," - + "\"write_buffer_size_in_bytes\":" + writeBufferSizeInBytes + "," - + "\"follower_mapping_version\":" + followerMappingVersion + "," - + "\"follower_settings_version\":" + followerSettingsVersion + "," - + "\"follower_aliases_version\":" + followerAliasesVersion + "," - + "\"total_read_time_millis\":" + totalReadTimeMillis + "," - + "\"total_read_remote_exec_time_millis\":" + totalReadRemoteExecTimeMillis + "," - + "\"successful_read_requests\":" + successfulReadRequests + "," - + "\"failed_read_requests\":" + failedReadRequests + "," - + "\"operations_read\":" + operationsRead + "," - + "\"bytes_read\":" + bytesRead + "," - + "\"total_write_time_millis\":" + totalWriteTimeMillis +"," - + "\"successful_write_requests\":" + successfulWriteRequests + "," - + "\"failed_write_requests\":" + failedWriteRequests + "," - + "\"operations_written\":" + operationWritten + "," - + "\"read_exceptions\":[" - + "{" - + "\"from_seq_no\":" + fetchExceptions.keySet().iterator().next() + "," - + "\"retries\":" + fetchExceptions.values().iterator().next().v1() + "," - + "\"exception\":{" - + "\"type\":\"exception\"," - + "\"reason\":\"shard is sad\"" - + "}" - + "}" - + "]," - + "\"time_since_last_read_millis\":" + timeSinceLastReadMillis + "," - + "\"fatal_exception\":{\"type\":\"exception\",\"reason\":\"fatal error\"}" - + "}" - + "}")); + xContent.utf8ToString(), + equalTo( + "{" + + "\"cluster_uuid\":\"_cluster\"," + + "\"timestamp\":\"" + + DATE_TIME_FORMATTER.formatMillis(timestamp) + + "\"," + + "\"interval_ms\":" + + intervalMillis + + "," + + "\"type\":\"ccr_stats\"," + + "\"source_node\":{" + + "\"uuid\":\"_uuid\"," + + "\"host\":\"_host\"," + + "\"transport_address\":\"_addr\"," + + "\"ip\":\"_ip\"," + + "\"name\":\"_name\"," + + "\"timestamp\":\"" + + DATE_TIME_FORMATTER.formatMillis(nodeTimestamp) + + "\"" + + "}," + + "\"ccr_stats\":{" + + "\"remote_cluster\":\"leader_cluster\"," + + "\"leader_index\":\"leader_index\"," + + "\"follower_index\":\"follower_index\"," + + "\"shard_id\":" + + shardId + + "," + + "\"leader_global_checkpoint\":" + + leaderGlobalCheckpoint + + "," + + "\"leader_max_seq_no\":" + + leaderMaxSeqNo + + "," + + "\"follower_global_checkpoint\":" + + followerGlobalCheckpoint + + "," + + "\"follower_max_seq_no\":" + + followerMaxSeqNo + + "," + + "\"last_requested_seq_no\":" + + lastRequestedSeqNo + + "," + + "\"outstanding_read_requests\":" + + numberOfConcurrentReads + + "," + + "\"outstanding_write_requests\":" + + numberOfConcurrentWrites + + "," + + "\"write_buffer_operation_count\":" + + writeBufferOperationCount + + "," + + "\"write_buffer_size_in_bytes\":" + + writeBufferSizeInBytes + + "," + + "\"follower_mapping_version\":" + + followerMappingVersion + + "," + + "\"follower_settings_version\":" + + followerSettingsVersion + + "," + + "\"follower_aliases_version\":" + + followerAliasesVersion + + "," + + "\"total_read_time_millis\":" + + totalReadTimeMillis + + "," + + "\"total_read_remote_exec_time_millis\":" + + totalReadRemoteExecTimeMillis + + "," + + "\"successful_read_requests\":" + + successfulReadRequests + + "," + + "\"failed_read_requests\":" + + failedReadRequests + + "," + + "\"operations_read\":" + + operationsRead + + "," + + "\"bytes_read\":" + + bytesRead + + "," + + "\"total_write_time_millis\":" + + totalWriteTimeMillis + + "," + + "\"successful_write_requests\":" + + successfulWriteRequests + + "," + + "\"failed_write_requests\":" + + failedWriteRequests + + "," + + "\"operations_written\":" + + operationWritten + + "," + + "\"read_exceptions\":[" + + "{" + + "\"from_seq_no\":" + + fetchExceptions.keySet().iterator().next() + + "," + + "\"retries\":" + + fetchExceptions.values().iterator().next().v1() + + "," + + "\"exception\":{" + + "\"type\":\"exception\"," + + "\"reason\":\"shard is sad\"" + + "}" + + "}" + + "]," + + "\"time_since_last_read_millis\":" + + timeSinceLastReadMillis + + "," + + "\"fatal_exception\":{\"type\":\"exception\",\"reason\":\"fatal error\"}" + + "}" + + "}" + ) + ); } public void testShardFollowNodeTaskStatusFieldsMapped() throws IOException { - final NavigableMap> fetchExceptions = - new TreeMap<>(Collections.singletonMap(1L, Tuple.tuple(2, new ElasticsearchException("shard is sad")))); + final NavigableMap> fetchExceptions = new TreeMap<>( + Collections.singletonMap(1L, Tuple.tuple(2, new ElasticsearchException("shard is sad"))) + ); final ShardFollowNodeTaskStatus status = new ShardFollowNodeTaskStatus( "remote_cluster", "leader_index", @@ -235,16 +302,24 @@ public void testShardFollowNodeTaskStatusFieldsMapped() throws IOException { 10, fetchExceptions, 2, - new ElasticsearchException("fatal error")); + new ElasticsearchException("fatal error") + ); XContentBuilder builder = jsonBuilder(); builder.value(status); Map serializedStatus = XContentHelper.convertToMap(XContentType.JSON.xContent(), Strings.toString(builder), false); byte[] loadedTemplate = MonitoringTemplateRegistry.getTemplateConfigForMonitoredSystem(MonitoredSystem.ES).loadBytes(); - Map template = - XContentHelper.convertToMap(XContentType.JSON.xContent(), loadedTemplate, 0, loadedTemplate.length, false); - Map followStatsMapping = (Map) XContentMapValues - .extractValue("mappings._doc.properties.ccr_stats.properties", template); + Map template = XContentHelper.convertToMap( + XContentType.JSON.xContent(), + loadedTemplate, + 0, + loadedTemplate.length, + false + ); + Map followStatsMapping = (Map) XContentMapValues.extractValue( + "mappings._doc.properties.ccr_stats.properties", + template + ); assertThat(serializedStatus.size(), equalTo(followStatsMapping.size())); for (Map.Entry entry : serializedStatus.entrySet()) { String fieldName = entry.getKey(); @@ -254,11 +329,13 @@ public void testShardFollowNodeTaskStatusFieldsMapped() throws IOException { Object fieldValue = entry.getValue(); String fieldType = (String) fieldMapping.get("type"); if (fieldValue instanceof Long || fieldValue instanceof Integer) { - assertThat("expected long field type for field [" + fieldName + "]", fieldType, - anyOf(equalTo("long"), equalTo("integer"))); + assertThat("expected long field type for field [" + fieldName + "]", fieldType, anyOf(equalTo("long"), equalTo("integer"))); } else if (fieldValue instanceof String) { - assertThat("expected keyword field type for field [" + fieldName + "]", fieldType, - anyOf(equalTo("keyword"), equalTo("text"))); + assertThat( + "expected keyword field type for field [" + fieldName + "]", + fieldType, + anyOf(equalTo("keyword"), equalTo("text")) + ); } else { // Manual test specific object fields and if not just fail: if (fieldName.equals("read_exceptions")) { @@ -268,8 +345,10 @@ public void testShardFollowNodeTaskStatusFieldsMapped() throws IOException { assertThat(XContentMapValues.extractValue("properties.retries.type", fieldMapping), equalTo("integer")); assertThat(XContentMapValues.extractValue("properties.exception.type", fieldMapping), equalTo("object")); - Map exceptionFieldMapping = - (Map) XContentMapValues.extractValue("properties.exception.properties", fieldMapping); + Map exceptionFieldMapping = (Map) XContentMapValues.extractValue( + "properties.exception.properties", + fieldMapping + ); assertThat(exceptionFieldMapping.size(), equalTo(2)); assertThat(XContentMapValues.extractValue("type.type", exceptionFieldMapping), equalTo("keyword")); assertThat(XContentMapValues.extractValue("reason.type", exceptionFieldMapping), equalTo("text")); diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/StatsCollectorTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/StatsCollectorTests.java index f0e2e87a57b5b..54285dc20b2b4 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/StatsCollectorTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/StatsCollectorTests.java @@ -150,8 +150,10 @@ public void testDoCollect() throws Exception { assertThat(document.getId(), nullValue()); assertThat(document.stats(), is(autoFollowStats)); - assertWarnings("[xpack.monitoring.collection.ccr.stats.timeout] setting was deprecated in Elasticsearch and will be removed in " + - "a future release! See the breaking changes documentation for the next major version."); + assertWarnings( + "[xpack.monitoring.collection.ccr.stats.timeout] setting was deprecated in Elasticsearch and will be removed in " + + "a future release! See the breaking changes documentation for the next major version." + ); } private List mockStatuses() { @@ -169,10 +171,12 @@ private List mockStatuses() { return statuses; } - private StatsCollector createCollector(Settings settings, - ClusterService clusterService, - XPackLicenseState licenseState, - Client client) { + private StatsCollector createCollector( + Settings settings, + ClusterService clusterService, + XPackLicenseState licenseState, + Client client + ) { return new StatsCollector(settings, clusterService, licenseState, client); } diff --git a/x-pack/plugin/core/src/internalClusterTest/java/org/elasticsearch/snapshots/sourceonly/SourceOnlySnapshotIT.java b/x-pack/plugin/core/src/internalClusterTest/java/org/elasticsearch/snapshots/sourceonly/SourceOnlySnapshotIT.java index 4600a99cfb7af..aee2df5817305 100644 --- a/x-pack/plugin/core/src/internalClusterTest/java/org/elasticsearch/snapshots/sourceonly/SourceOnlySnapshotIT.java +++ b/x-pack/plugin/core/src/internalClusterTest/java/org/elasticsearch/snapshots/sourceonly/SourceOnlySnapshotIT.java @@ -18,11 +18,9 @@ import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.common.util.CollectionUtils; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.env.Environment; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.engine.EngineFactory; @@ -40,6 +38,8 @@ import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.snapshots.AbstractSnapshotIntegTestCase; import org.elasticsearch.test.ESIntegTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentBuilder; import org.hamcrest.Matchers; import java.io.IOException; @@ -55,9 +55,9 @@ import java.util.Optional; import java.util.function.BiConsumer; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; @ESIntegTestCase.ClusterScope(numDataNodes = 0) public class SourceOnlySnapshotIT extends AbstractSnapshotIntegTestCase { @@ -80,11 +80,16 @@ public void setUp() throws Exception { public static final class MyPlugin extends Plugin implements RepositoryPlugin, EnginePlugin { @Override - public Map getRepositories(Environment env, NamedXContentRegistry namedXContentRegistry, - ClusterService clusterService, BigArrays bigArrays, - RecoverySettings recoverySettings) { + public Map getRepositories( + Environment env, + NamedXContentRegistry namedXContentRegistry, + ClusterService clusterService, + BigArrays bigArrays, + RecoverySettings recoverySettings + ) { return Collections.singletonMap("source", SourceOnlySnapshotRepository.newRepositoryFactory()); } + @Override public Optional getEngineFactory(IndexSettings indexSettings) { if (indexSettings.getValue(SourceOnlySnapshotRepository.SOURCE_ONLY)) { @@ -109,22 +114,32 @@ public void testSnapshotAndRestore() throws Exception { boolean sourceHadDeletions = deleted > 0; // we use indexRandom which might create holes ie. deleted docs assertHits(sourceIdx, builders.length, sourceHadDeletions); assertMappings(sourceIdx, requireRouting, useNested); - SearchPhaseExecutionException e = expectThrows(SearchPhaseExecutionException.class, () -> { - client().prepareSearch(sourceIdx).setQuery(QueryBuilders.idsQuery() - .addIds("" + randomIntBetween(0, builders.length))).get(); - }); + SearchPhaseExecutionException e = expectThrows( + SearchPhaseExecutionException.class, + () -> { + client().prepareSearch(sourceIdx) + .setQuery(QueryBuilders.idsQuery().addIds("" + randomIntBetween(0, builders.length))) + .get(); + } + ); assertTrue(e.toString().contains("_source only indices can't be searched or filtered")); // can-match phase pre-filters access to non-existing field - assertEquals(0, - client().prepareSearch(sourceIdx).setQuery(QueryBuilders.termQuery("field1", "bar")).get().getHits().getTotalHits().value); + assertEquals( + 0, + client().prepareSearch(sourceIdx).setQuery(QueryBuilders.termQuery("field1", "bar")).get().getHits().getTotalHits().value + ); // make sure deletes do not work String idToDelete = "" + randomIntBetween(0, builders.length); - expectThrows(ClusterBlockException.class, () -> client().prepareDelete(sourceIdx, idToDelete) - .setRouting("r" + idToDelete).get()); + expectThrows(ClusterBlockException.class, () -> client().prepareDelete(sourceIdx, idToDelete).setRouting("r" + idToDelete).get()); internalCluster().ensureAtLeastNumDataNodes(2); - assertAcked(client().admin().indices().prepareUpdateSettings(sourceIdx) - .setSettings(Settings.builder().put("index.number_of_replicas", 1)).get()); + assertAcked( + client().admin() + .indices() + .prepareUpdateSettings(sourceIdx) + .setSettings(Settings.builder().put("index.number_of_replicas", 1)) + .get() + ); ensureGreen(sourceIdx); assertHits(sourceIdx, builders.length, sourceHadDeletions); } @@ -137,19 +152,29 @@ public void testSnapshotAndRestoreWithNested() throws Exception { assertThat(indicesStatsResponse.getTotal().docs.getDeleted(), Matchers.greaterThan(0L)); assertHits(sourceIdx, builders.length, true); assertMappings(sourceIdx, requireRouting, true); - SearchPhaseExecutionException e = expectThrows(SearchPhaseExecutionException.class, () -> - client().prepareSearch(sourceIdx).setQuery(QueryBuilders.idsQuery().addIds("" + randomIntBetween(0, builders.length))).get()); + SearchPhaseExecutionException e = expectThrows( + SearchPhaseExecutionException.class, + () -> client().prepareSearch(sourceIdx) + .setQuery(QueryBuilders.idsQuery().addIds("" + randomIntBetween(0, builders.length))) + .get() + ); assertTrue(e.toString().contains("_source only indices can't be searched or filtered")); // can-match phase pre-filters access to non-existing field - assertEquals(0, - client().prepareSearch(sourceIdx).setQuery(QueryBuilders.termQuery("field1", "bar")).get().getHits().getTotalHits().value); + assertEquals( + 0, + client().prepareSearch(sourceIdx).setQuery(QueryBuilders.termQuery("field1", "bar")).get().getHits().getTotalHits().value + ); // make sure deletes do not work String idToDelete = "" + randomIntBetween(0, builders.length); - expectThrows(ClusterBlockException.class, () -> client().prepareDelete(sourceIdx, idToDelete) - .setRouting("r" + idToDelete).get()); + expectThrows(ClusterBlockException.class, () -> client().prepareDelete(sourceIdx, idToDelete).setRouting("r" + idToDelete).get()); internalCluster().ensureAtLeastNumDataNodes(2); - assertAcked(client().admin().indices().prepareUpdateSettings(sourceIdx) - .setSettings(Settings.builder().put("index.number_of_replicas", 1)).get()); + assertAcked( + client().admin() + .indices() + .prepareUpdateSettings(sourceIdx) + .setSettings(Settings.builder().put("index.number_of_replicas", 1)) + .get() + ); ensureGreen(sourceIdx); assertHits(sourceIdx, builders.length, true); } @@ -160,8 +185,11 @@ public void testSnapshotWithDanglingLocalSegment() throws Exception { final String dataNode = internalCluster().startDataOnlyNode(); final String repo = "test-repo"; - createRepository(repo, "source", - Settings.builder().put("location", randomRepoPath()).put("delegate_type", "fs").put("compress", randomBoolean())); + createRepository( + repo, + "source", + Settings.builder().put("location", randomRepoPath()).put("delegate_type", "fs").put("compress", randomBoolean()) + ); final String indexName = "test-idx"; createIndex(indexName); @@ -172,9 +200,12 @@ public void testSnapshotWithDanglingLocalSegment() throws Exception { assertSuccessful(startFullSnapshot(repo, "snapshot-2")); logger.info("--> randomly deleting files from the local _snapshot path to simulate corruption"); - Path snapshotShardPath = internalCluster().getInstance(IndicesService.class, dataNode).indexService( - clusterService().state().metadata().index(indexName).getIndex()).getShard(0).shardPath().getDataPath() - .resolve("_snapshot"); + Path snapshotShardPath = internalCluster().getInstance(IndicesService.class, dataNode) + .indexService(clusterService().state().metadata().index(indexName).getIndex()) + .getShard(0) + .shardPath() + .getDataPath() + .resolve("_snapshot"); try (DirectoryStream localFiles = Files.newDirectoryStream(snapshotShardPath)) { for (Path localFile : localFiles) { if (randomBoolean()) { @@ -189,26 +220,36 @@ public void testSnapshotWithDanglingLocalSegment() throws Exception { private static void assertMappings(String sourceIdx, boolean requireRouting, boolean useNested) { GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings(sourceIdx).get(); MappingMetadata mapping = getMappingsResponse.getMappings().get(sourceIdx); - String nested = useNested ? - ",\"incorrect\":{\"type\":\"object\"},\"nested\":{\"type\":\"nested\",\"properties\":{\"value\":{\"type\":\"long\"}}}" : ""; + String nested = useNested + ? ",\"incorrect\":{\"type\":\"object\"},\"nested\":{\"type\":\"nested\",\"properties\":{\"value\":{\"type\":\"long\"}}}" + : ""; if (requireRouting) { - assertEquals("{\"_doc\":{\"enabled\":false," + - "\"_meta\":{\"_doc\":{\"_routing\":{\"required\":true}," + - "\"properties\":{\"field1\":{\"type\":\"text\"," + - "\"fields\":{\"keyword\":{\"type\":\"keyword\",\"ignore_above\":256}}}" + nested + - "}}}}}", mapping.source().string()); + assertEquals( + "{\"_doc\":{\"enabled\":false," + + "\"_meta\":{\"_doc\":{\"_routing\":{\"required\":true}," + + "\"properties\":{\"field1\":{\"type\":\"text\"," + + "\"fields\":{\"keyword\":{\"type\":\"keyword\",\"ignore_above\":256}}}" + + nested + + "}}}}}", + mapping.source().string() + ); } else { - assertEquals("{\"_doc\":{\"enabled\":false," + - "\"_meta\":{\"_doc\":{\"properties\":{\"field1\":{\"type\":\"text\"," + - "\"fields\":{\"keyword\":{\"type\":\"keyword\",\"ignore_above\":256}}}" + nested + "}}}}}", - mapping.source().string()); + assertEquals( + "{\"_doc\":{\"enabled\":false," + + "\"_meta\":{\"_doc\":{\"properties\":{\"field1\":{\"type\":\"text\"," + + "\"fields\":{\"keyword\":{\"type\":\"keyword\",\"ignore_above\":256}}}" + + nested + + "}}}}}", + mapping.source().string() + ); } } private void assertHits(String index, int numDocsExpected, boolean sourceHadDeletions) { SearchResponse searchResponse = client().prepareSearch(index) .addSort(SeqNoFieldMapper.NAME, SortOrder.ASC) - .setSize(numDocsExpected).get(); + .setSize(numDocsExpected) + .get(); BiConsumer assertConsumer = (res, allowHoles) -> { SearchHits hits = res.getHits(); long i = 0; @@ -232,8 +273,9 @@ private void assertHits(String index, int numDocsExpected, boolean sourceHadDele searchResponse = client().prepareSearch(index) .addSort(SeqNoFieldMapper.NAME, SortOrder.ASC) .setScroll("1m") - .slice(new SliceBuilder(SeqNoFieldMapper.NAME, randomIntBetween(0,1), 2)) - .setSize(randomIntBetween(1, 10)).get(); + .slice(new SliceBuilder(SeqNoFieldMapper.NAME, randomIntBetween(0, 1), 2)) + .setSize(randomIntBetween(1, 10)) + .get(); try { do { // now do a scroll with a slice @@ -247,9 +289,8 @@ private void assertHits(String index, int numDocsExpected, boolean sourceHadDele } } - private IndexRequestBuilder[] snapshotAndRestore(final String sourceIdx, - final boolean requireRouting, - final boolean useNested) throws InterruptedException, IOException { + private IndexRequestBuilder[] snapshotAndRestore(final String sourceIdx, final boolean requireRouting, final boolean useNested) + throws InterruptedException, IOException { logger.info("--> starting a master node and a data node"); internalCluster().startMasterOnlyNode(); internalCluster().startDataOnlyNode(); @@ -257,8 +298,11 @@ private IndexRequestBuilder[] snapshotAndRestore(final String sourceIdx, final String repo = "test-repo"; final String snapshot = "test-snap"; - createRepository(repo, "source", - Settings.builder().put("location", randomRepoPath()).put("delegate_type", "fs").put("compress", randomBoolean())); + createRepository( + repo, + "source", + Settings.builder().put("location", randomRepoPath()).put("delegate_type", "fs").put("compress", randomBoolean()) + ); CreateIndexRequestBuilder createIndexRequestBuilder = prepareCreate(sourceIdx, 0, indexSettingsNoReplicas(1)); List mappings = new ArrayList<>(); @@ -278,9 +322,7 @@ private IndexRequestBuilder[] snapshotAndRestore(final String sourceIdx, logger.info("--> indexing some data"); IndexRequestBuilder[] builders = new IndexRequestBuilder[randomIntBetween(10, 100)]; for (int i = 0; i < builders.length; i++) { - XContentBuilder source = jsonBuilder() - .startObject() - .field("field1", "bar " + i); + XContentBuilder source = jsonBuilder().startObject().field("field1", "bar " + i); if (useNested) { source.startArray("nested"); for (int j = 0; j < 2; ++j) { @@ -307,11 +349,13 @@ private IndexRequestBuilder[] snapshotAndRestore(final String sourceIdx, assertFalse(client().admin().cluster().prepareHealth().setTimeout("30s").setWaitForNodes("2").get().isTimedOut()); logger.info("--> restore the index and ensure all shards are allocated"); - RestoreSnapshotResponse restoreResponse = client().admin().cluster() - .prepareRestoreSnapshot(repo, snapshot).setWaitForCompletion(true) - .setIndices(sourceIdx).get(); - assertEquals(restoreResponse.getRestoreInfo().totalShards(), - restoreResponse.getRestoreInfo().successfulShards()); + RestoreSnapshotResponse restoreResponse = client().admin() + .cluster() + .prepareRestoreSnapshot(repo, snapshot) + .setWaitForCompletion(true) + .setIndices(sourceIdx) + .get(); + assertEquals(restoreResponse.getRestoreInfo().totalShards(), restoreResponse.getRestoreInfo().successfulShards()); ensureYellow(); return builders; } diff --git a/x-pack/plugin/core/src/internalClusterTest/java/org/elasticsearch/xpack/cluster/routing/allocation/DataTierAllocationDeciderIT.java b/x-pack/plugin/core/src/internalClusterTest/java/org/elasticsearch/xpack/cluster/routing/allocation/DataTierAllocationDeciderIT.java index 070b1ebc2c934..039bb8b840be1 100644 --- a/x-pack/plugin/core/src/internalClusterTest/java/org/elasticsearch/xpack/cluster/routing/allocation/DataTierAllocationDeciderIT.java +++ b/x-pack/plugin/core/src/internalClusterTest/java/org/elasticsearch/xpack/cluster/routing/allocation/DataTierAllocationDeciderIT.java @@ -50,8 +50,10 @@ public void testDefaultIndexAllocateToContent() { assertThat(DataTier.TIER_PREFERENCE_SETTING.get(idxSettings), equalTo(DataTier.DATA_CONTENT)); // index should be red - assertThat(client().admin().cluster().prepareHealth(index).get().getIndices().get(index).getStatus(), - equalTo(ClusterHealthStatus.RED)); + assertThat( + client().admin().cluster().prepareHealth(index).get().getIndices().get(index).getStatus(), + equalTo(ClusterHealthStatus.RED) + ); if (randomBoolean()) { logger.info("--> starting content node"); @@ -70,10 +72,11 @@ public void testOverrideDefaultAllocation() { startColdOnlyNode(); ensureGreen(); - client().admin().indices().prepareCreate(index) + client().admin() + .indices() + .prepareCreate(index) .setWaitForActiveShards(0) - .setSettings(Settings.builder() - .put(DataTier.TIER_PREFERENCE, DataTier.DATA_WARM)) + .setSettings(Settings.builder().put(DataTier.TIER_PREFERENCE, DataTier.DATA_WARM)) .get(); Settings idxSettings = client().admin().indices().prepareGetIndex().addIndices(index).get().getSettings().get(index); @@ -88,10 +91,11 @@ public void testRequestSettingOverridden() { startContentOnlyNode(); ensureGreen(); - client().admin().indices().prepareCreate(index) + client().admin() + .indices() + .prepareCreate(index) .setWaitForActiveShards(0) - .setSettings(Settings.builder() - .putNull(DataTier.TIER_PREFERENCE)) // will be overridden to data_content + .setSettings(Settings.builder().putNull(DataTier.TIER_PREFERENCE)) // will be overridden to data_content .get(); Settings idxSettings = client().admin().indices().prepareGetIndex().addIndices(index).get().getSettings().get(index); @@ -110,47 +114,59 @@ public void testShrinkStaysOnTier() { startWarmOnlyNode(); startHotOnlyNode(); - client().admin().indices().prepareCreate(index) + client().admin() + .indices() + .prepareCreate(index) .setWaitForActiveShards(0) - .setSettings(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 2) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put(DataTier.TIER_PREFERENCE, "data_warm")) + .setSettings( + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 2) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put(DataTier.TIER_PREFERENCE, "data_warm") + ) .get(); client().admin().indices().prepareAddBlock(IndexMetadata.APIBlock.READ_ONLY, index).get(); - client().admin().indices().prepareResizeIndex(index, index + "-shrunk") + client().admin() + .indices() + .prepareResizeIndex(index, index + "-shrunk") .setResizeType(ResizeType.SHRINK) - .setSettings(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .build()).get(); + .setSettings( + Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).build() + ) + .get(); ensureGreen(index + "-shrunk"); - Settings idxSettings = client().admin().indices().prepareGetIndex().addIndices(index + "-shrunk") - .get().getSettings().get(index + "-shrunk"); + Settings idxSettings = client().admin() + .indices() + .prepareGetIndex() + .addIndices(index + "-shrunk") + .get() + .getSettings() + .get(index + "-shrunk"); // It should inherit the setting of its originator assertThat(DataTier.TIER_PREFERENCE_SETTING.get(idxSettings), equalTo(DataTier.DATA_WARM)); // Required or else the test cleanup fails because it can't delete the indices - client().admin().indices().prepareUpdateSettings(index, index + "-shrunk") - .setSettings(Settings.builder() - .put("index.blocks.read_only", false)) + client().admin() + .indices() + .prepareUpdateSettings(index, index + "-shrunk") + .setSettings(Settings.builder().put("index.blocks.read_only", false)) .get(); } public void testTemplateOverridden() { startContentOnlyNode(); - Template t = new Template(Settings.builder() - .putNull(DataTier.TIER_PREFERENCE) - .build(), null, null); - ComposableIndexTemplate ct = new ComposableIndexTemplate.Builder() - .indexPatterns(Collections.singletonList(index)) - .template(t).build(); - client().execute(PutComposableIndexTemplateAction.INSTANCE, - new PutComposableIndexTemplateAction.Request("template").indexTemplate(ct)).actionGet(); + Template t = new Template(Settings.builder().putNull(DataTier.TIER_PREFERENCE).build(), null, null); + ComposableIndexTemplate ct = new ComposableIndexTemplate.Builder().indexPatterns(Collections.singletonList(index)) + .template(t) + .build(); + client().execute( + PutComposableIndexTemplateAction.INSTANCE, + new PutComposableIndexTemplateAction.Request("template").indexTemplate(ct) + ).actionGet(); client().admin().indices().prepareCreate(index).setWaitForActiveShards(0).get(); @@ -166,18 +182,22 @@ public void testDataTierTelemetry() { startContentOnlyNode(); startHotOnlyNode(); - client().admin().indices().prepareCreate(index) - .setSettings(Settings.builder() - .put(DataTier.TIER_PREFERENCE, "data_hot") - .put("index.number_of_shards", 2) - .put("index.number_of_replicas", 0)) + client().admin() + .indices() + .prepareCreate(index) + .setSettings( + Settings.builder() + .put(DataTier.TIER_PREFERENCE, "data_hot") + .put("index.number_of_shards", 2) + .put("index.number_of_replicas", 0) + ) .setWaitForActiveShards(0) .get(); - client().admin().indices().prepareCreate(index + "2") - .setSettings(Settings.builder() - .put("index.number_of_shards", 1) - .put("index.number_of_replicas", 1)) + client().admin() + .indices() + .prepareCreate(index + "2") + .setSettings(Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 1)) .setWaitForActiveShards(0) .get(); @@ -210,20 +230,28 @@ public void testDataTierTelemetry() { public void testIllegalOnFrozen() { startDataNode(); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> createIndex(index, Settings.builder() - .put("index.number_of_shards", 1) - .put("index.number_of_replicas", 0) - .put(DataTier.TIER_PREFERENCE, DataTier.DATA_FROZEN) - .build())); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> createIndex( + index, + Settings.builder() + .put("index.number_of_shards", 1) + .put("index.number_of_replicas", 0) + .put(DataTier.TIER_PREFERENCE, DataTier.DATA_FROZEN) + .build() + ) + ); assertThat(e.getMessage(), equalTo("[data_frozen] tier can only be used for partial searchable snapshots")); String initialTier = randomFrom(DataTier.DATA_HOT, DataTier.DATA_WARM, DataTier.DATA_COLD); - createIndex(index, Settings.builder() - .put("index.number_of_shards", 1) - .put("index.number_of_replicas", 0) - .put(DataTier.TIER_PREFERENCE, initialTier) - .build()); + createIndex( + index, + Settings.builder() + .put("index.number_of_shards", 1) + .put("index.number_of_replicas", 0) + .put(DataTier.TIER_PREFERENCE, initialTier) + .build() + ); IllegalArgumentException e2 = expectThrows(IllegalArgumentException.class, () -> updatePreference(DataTier.DATA_FROZEN)); assertThat(e2.getMessage(), equalTo("[data_frozen] tier can only be used for partial searchable snapshots")); @@ -232,13 +260,16 @@ public void testIllegalOnFrozen() { } private void updatePreference(String tier) { - client().admin().indices().updateSettings(new UpdateSettingsRequest(index) - .settings(Map.of(DataTier.TIER_PREFERENCE, tier))).actionGet(); + client().admin() + .indices() + .updateSettings(new UpdateSettingsRequest(index).settings(Map.of(DataTier.TIER_PREFERENCE, tier))) + .actionGet(); } private DataTiersFeatureSetUsage getUsage() { XPackUsageResponse usages = new XPackUsageRequestBuilder(client()).execute().actionGet(); - return usages.getUsages().stream() + return usages.getUsages() + .stream() .filter(u -> u instanceof DataTiersFeatureSetUsage) .findFirst() .map(u -> (DataTiersFeatureSetUsage) u) diff --git a/x-pack/plugin/core/src/internalClusterTest/java/org/elasticsearch/xpack/cluster/routing/allocation/DataTierTelemetryPlugin.java b/x-pack/plugin/core/src/internalClusterTest/java/org/elasticsearch/xpack/cluster/routing/allocation/DataTierTelemetryPlugin.java index fe6a63db04f0f..fb3cc346df3b1 100644 --- a/x-pack/plugin/core/src/internalClusterTest/java/org/elasticsearch/xpack/cluster/routing/allocation/DataTierTelemetryPlugin.java +++ b/x-pack/plugin/core/src/internalClusterTest/java/org/elasticsearch/xpack/cluster/routing/allocation/DataTierTelemetryPlugin.java @@ -40,11 +40,17 @@ public class DataTierTelemetryPlugin extends LocalStateCompositeXPackPlugin { public static class DataTiersTransportXPackUsageAction extends TransportXPackUsageAction { @Inject - public DataTiersTransportXPackUsageAction(ThreadPool threadPool, TransportService transportService, - ClusterService clusterService, ActionFilters actionFilters, - IndexNameExpressionResolver indexNameExpressionResolver, NodeClient client) { + public DataTiersTransportXPackUsageAction( + ThreadPool threadPool, + TransportService transportService, + ClusterService clusterService, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver, + NodeClient client + ) { super(threadPool, transportService, clusterService, actionFilters, indexNameExpressionResolver, client); } + @Override protected List usageActions() { return Collections.singletonList(XPackUsageFeatureAction.DATA_TIERS); @@ -53,8 +59,12 @@ protected List usageActions() { public static class DataTiersTransportXPackInfoAction extends TransportXPackInfoAction { @Inject - public DataTiersTransportXPackInfoAction(TransportService transportService, ActionFilters actionFilters, - LicenseService licenseService, NodeClient client) { + public DataTiersTransportXPackInfoAction( + TransportService transportService, + ActionFilters actionFilters, + LicenseService licenseService, + NodeClient client + ) { super(transportService, actionFilters, licenseService, client); } diff --git a/x-pack/plugin/core/src/internalClusterTest/java/org/elasticsearch/xpack/core/rest/action/ReloadSynonymAnalyzerIT.java b/x-pack/plugin/core/src/internalClusterTest/java/org/elasticsearch/xpack/core/rest/action/ReloadSynonymAnalyzerIT.java index 83b7d66f2a7dc..dc27336e8d2b8 100644 --- a/x-pack/plugin/core/src/internalClusterTest/java/org/elasticsearch/xpack/core/rest/action/ReloadSynonymAnalyzerIT.java +++ b/x-pack/plugin/core/src/internalClusterTest/java/org/elasticsearch/xpack/core/rest/action/ReloadSynonymAnalyzerIT.java @@ -68,19 +68,25 @@ public void testSynonymsUpdateable() throws FileNotFoundException, IOException, Path config = internalCluster().getInstance(Environment.class).configFile(); String synonymsFileName = "synonyms.txt"; Path synonymsFile = config.resolve(synonymsFileName); - try (PrintWriter out = new PrintWriter( - new OutputStreamWriter(Files.newOutputStream(synonymsFile), StandardCharsets.UTF_8))) { + try (PrintWriter out = new PrintWriter(new OutputStreamWriter(Files.newOutputStream(synonymsFile), StandardCharsets.UTF_8))) { out.println("foo, baz"); } - assertAcked(client().admin().indices().prepareCreate("test").setSettings(Settings.builder() - .put("index.number_of_shards", cluster().numDataNodes() * 2) - .put("index.number_of_replicas", 1) - .put("analysis.analyzer.my_synonym_analyzer.tokenizer", "standard") - .put("analysis.analyzer.my_synonym_analyzer.filter", "my_synonym_filter") - .put("analysis.filter.my_synonym_filter.type", "synonym") - .put("analysis.filter.my_synonym_filter.updateable", "true") - .put("analysis.filter.my_synonym_filter.synonyms_path", synonymsFileName)) - .setMapping("field", "type=text,analyzer=standard,search_analyzer=my_synonym_analyzer")); + assertAcked( + client().admin() + .indices() + .prepareCreate("test") + .setSettings( + Settings.builder() + .put("index.number_of_shards", cluster().numDataNodes() * 2) + .put("index.number_of_replicas", 1) + .put("analysis.analyzer.my_synonym_analyzer.tokenizer", "standard") + .put("analysis.analyzer.my_synonym_analyzer.filter", "my_synonym_filter") + .put("analysis.filter.my_synonym_filter.type", "synonym") + .put("analysis.filter.my_synonym_filter.updateable", "true") + .put("analysis.filter.my_synonym_filter.synonyms_path", synonymsFileName) + ) + .setMapping("field", "type=text,analyzer=standard,search_analyzer=my_synonym_analyzer") + ); client().prepareIndex("test").setId("1").setSource("field", "foo").get(); assertNoFailures(client().admin().indices().prepareRefresh("test").execute().actionGet()); @@ -97,18 +103,23 @@ public void testSynonymsUpdateable() throws FileNotFoundException, IOException, // now update synonyms file several times and trigger reloading for (int i = 0; i < 10; i++) { String testTerm = randomAlphaOfLength(10); - try (PrintWriter out = new PrintWriter( - new OutputStreamWriter(Files.newOutputStream(synonymsFile, StandardOpenOption.WRITE), StandardCharsets.UTF_8))) { + try ( + PrintWriter out = new PrintWriter( + new OutputStreamWriter(Files.newOutputStream(synonymsFile, StandardOpenOption.WRITE), StandardCharsets.UTF_8) + ) + ) { out.println("foo, baz, " + testTerm); } ReloadAnalyzersResponse reloadResponse = client().execute(ReloadAnalyzerAction.INSTANCE, new ReloadAnalyzersRequest("test")) - .actionGet(); + .actionGet(); assertNoFailures(reloadResponse); assertEquals(cluster().numDataNodes(), reloadResponse.getSuccessfulShards()); assertTrue(reloadResponse.getReloadDetails().containsKey("test")); assertEquals("test", reloadResponse.getReloadDetails().get("test").getIndexName()); - assertEquals(Collections.singleton("my_synonym_analyzer"), - reloadResponse.getReloadDetails().get("test").getReloadedAnalyzers()); + assertEquals( + Collections.singleton("my_synonym_analyzer"), + reloadResponse.getReloadDetails().get("test").getReloadedAnalyzers() + ); analyzeResponse = client().admin().indices().prepareAnalyze("test", "foo").setAnalyzer("my_synonym_analyzer").get(); assertEquals(3, analyzeResponse.getTokens().size()); diff --git a/x-pack/plugin/core/src/internalClusterTest/java/org/elasticsearch/xpack/core/rest/action/XPackUsageRestCancellationIT.java b/x-pack/plugin/core/src/internalClusterTest/java/org/elasticsearch/xpack/core/rest/action/XPackUsageRestCancellationIT.java index a24bd4384b742..ab0a8537b8732 100644 --- a/x-pack/plugin/core/src/internalClusterTest/java/org/elasticsearch/xpack/core/rest/action/XPackUsageRestCancellationIT.java +++ b/x-pack/plugin/core/src/internalClusterTest/java/org/elasticsearch/xpack/core/rest/action/XPackUsageRestCancellationIT.java @@ -67,8 +67,10 @@ protected Collection> nodePlugins() { @Override protected Settings nodeSettings(int ordinal, Settings otherSettings) { - return Settings.builder().put(super.nodeSettings(ordinal, otherSettings)) - .put(NetworkModule.HTTP_DEFAULT_TYPE_SETTING.getKey(), NioTransportPlugin.NIO_HTTP_TRANSPORT_NAME).build(); + return Settings.builder() + .put(super.nodeSettings(ordinal, otherSettings)) + .put(NetworkModule.HTTP_DEFAULT_TYPE_SETTING.getKey(), NioTransportPlugin.NIO_HTTP_TRANSPORT_NAME) + .build(); } @Override @@ -100,6 +102,7 @@ public void testCancellation() throws Exception { public static class BlockingUsageActionXPackPlugin extends LocalStateCompositeXPackPlugin { public static final XPackUsageFeatureAction BLOCKING_XPACK_USAGE = new XPackUsageFeatureAction("blocking_xpack_usage"); public static final XPackUsageFeatureAction NON_BLOCKING_XPACK_USAGE = new XPackUsageFeatureAction("regular_xpack_usage"); + public BlockingUsageActionXPackPlugin(Settings settings, Path configPath) { super(settings, configPath); } @@ -111,8 +114,7 @@ protected Class @Override public List> getActions() { - final ArrayList> actions = - new ArrayList<>(super.getActions()); + final ArrayList> actions = new ArrayList<>(super.getActions()); actions.add(new ActionHandler<>(BLOCKING_XPACK_USAGE, BlockingXPackUsageAction.class)); actions.add(new ActionHandler<>(NON_BLOCKING_XPACK_USAGE, NonBlockingXPackUsageAction.class)); return actions; @@ -121,12 +123,14 @@ protected Class public static class ClusterBlockAwareTransportXPackUsageAction extends TransportXPackUsageAction { @Inject - public ClusterBlockAwareTransportXPackUsageAction(ThreadPool threadPool, - TransportService transportService, - ClusterService clusterService, - ActionFilters actionFilters, - IndexNameExpressionResolver indexNameExpressionResolver, - NodeClient client) { + public ClusterBlockAwareTransportXPackUsageAction( + ThreadPool threadPool, + TransportService transportService, + ClusterService clusterService, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver, + NodeClient client + ) { super(threadPool, transportService, clusterService, actionFilters, indexNameExpressionResolver, client); } @@ -158,10 +162,12 @@ public BlockingXPackUsageAction( } @Override - protected void masterOperation(Task task, - XPackUsageRequest request, - ClusterState state, - ActionListener listener) throws Exception { + protected void masterOperation( + Task task, + XPackUsageRequest request, + ClusterState state, + ActionListener listener + ) throws Exception { blockActionLatch.await(); listener.onResponse(new XPackUsageFeatureResponse(new XPackFeatureSet.Usage("test", false, false) { @Override @@ -194,10 +200,12 @@ public NonBlockingXPackUsageAction( } @Override - protected void masterOperation(Task task, - XPackUsageRequest request, - ClusterState state, - ActionListener listener) { + protected void masterOperation( + Task task, + XPackUsageRequest request, + ClusterState state, + ActionListener listener + ) { assert false : "Unexpected execution"; } } diff --git a/x-pack/plugin/core/src/internalClusterTest/java/org/elasticsearch/xpack/core/termsenum/CCSTermsEnumIT.java b/x-pack/plugin/core/src/internalClusterTest/java/org/elasticsearch/xpack/core/termsenum/CCSTermsEnumIT.java index b54d747470b47..9261fa6abd727 100644 --- a/x-pack/plugin/core/src/internalClusterTest/java/org/elasticsearch/xpack/core/termsenum/CCSTermsEnumIT.java +++ b/x-pack/plugin/core/src/internalClusterTest/java/org/elasticsearch/xpack/core/termsenum/CCSTermsEnumIT.java @@ -62,8 +62,7 @@ public void testBasic() { remoteClient.admin().indices().prepareRefresh(remoteIndex).get(); // _terms_enum on a remote cluster - TermsEnumRequest req = new TermsEnumRequest("remote_cluster:remote_test") - .field("foo.keyword"); + TermsEnumRequest req = new TermsEnumRequest("remote_cluster:remote_test").field("foo.keyword"); TermsEnumResponse response = client().execute(TermsEnumAction.INSTANCE, req).actionGet(); assertTrue(response.isComplete()); assertThat(response.getTotalShards(), equalTo(1)); @@ -75,8 +74,7 @@ public void testBasic() { assertThat(response.getTerms().get(2), equalTo("zar")); // _terms_enum on mixed clusters (local + remote) - req = new TermsEnumRequest("remote_cluster:remote_test", "local_test") - .field("foo.keyword"); + req = new TermsEnumRequest("remote_cluster:remote_test", "local_test").field("foo.keyword"); response = client().execute(TermsEnumAction.INSTANCE, req).actionGet(); assertTrue(response.isComplete()); assertThat(response.getTotalShards(), equalTo(2)); @@ -88,9 +86,7 @@ public void testBasic() { assertThat(response.getTerms().get(2), equalTo("foobar")); assertThat(response.getTerms().get(3), equalTo("zar")); - req = new TermsEnumRequest("remote_cluster:remote_test", "local_test") - .field("foo.keyword") - .searchAfter("foobar"); + req = new TermsEnumRequest("remote_cluster:remote_test", "local_test").field("foo.keyword").searchAfter("foobar"); response = client().execute(TermsEnumAction.INSTANCE, req).actionGet(); assertTrue(response.isComplete()); assertThat(response.getTotalShards(), equalTo(2)); @@ -99,9 +95,7 @@ public void testBasic() { assertThat(response.getTerms().size(), equalTo(1)); assertThat(response.getTerms().get(0), equalTo("zar")); - req = new TermsEnumRequest("remote_cluster:remote_test", "local_test") - .field("foo.keyword") - .searchAfter("bar"); + req = new TermsEnumRequest("remote_cluster:remote_test", "local_test").field("foo.keyword").searchAfter("bar"); response = client().execute(TermsEnumAction.INSTANCE, req).actionGet(); assertTrue(response.isComplete()); assertThat(response.getTotalShards(), equalTo(2)); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/index/engine/frozen/FrozenEngine.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/index/engine/frozen/FrozenEngine.java index 34ad2fc7e6b04..3d38c25fbfd97 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/index/engine/frozen/FrozenEngine.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/index/engine/frozen/FrozenEngine.java @@ -52,8 +52,12 @@ * stats in order to obtain the number of reopens. */ public final class FrozenEngine extends ReadOnlyEngine { - public static final Setting INDEX_FROZEN = Setting.boolSetting("index.frozen", false, Setting.Property.IndexScope, - Setting.Property.PrivateIndex); + public static final Setting INDEX_FROZEN = Setting.boolSetting( + "index.frozen", + false, + Setting.Property.IndexScope, + Setting.Property.PrivateIndex + ); private final SegmentsStats segmentsStats; private final DocsStats docsStats; private volatile ElasticsearchDirectoryReader lastOpenedReader; @@ -65,9 +69,15 @@ public FrozenEngine(EngineConfig config, boolean requireCompleteHistory, boolean this(config, null, null, true, Function.identity(), requireCompleteHistory, lazilyLoadSoftDeletes); } - public FrozenEngine(EngineConfig config, SeqNoStats seqNoStats, TranslogStats translogStats, boolean obtainLock, - Function readerWrapperFunction, boolean requireCompleteHistory, - boolean lazilyLoadSoftDeletes) { + public FrozenEngine( + EngineConfig config, + SeqNoStats seqNoStats, + TranslogStats translogStats, + boolean obtainLock, + Function readerWrapperFunction, + boolean requireCompleteHistory, + boolean lazilyLoadSoftDeletes + ) { super(config, seqNoStats, translogStats, obtainLock, readerWrapperFunction, requireCompleteHistory, lazilyLoadSoftDeletes); boolean success = false; Directory directory = store.directory(); @@ -81,7 +91,9 @@ public FrozenEngine(EngineConfig config, SeqNoStats seqNoStats, TranslogStats tr } this.docsStats = docsStats(reader); canMatchReader = ElasticsearchDirectoryReader.wrap( - new RewriteCachingDirectoryReader(directory, reader.leaves(), null), config.getShardId()); + new RewriteCachingDirectoryReader(directory, reader.leaves(), null), + config.getShardId() + ); success = true; } catch (IOException e) { throw new UncheckedIOException(e); @@ -129,8 +141,7 @@ public IndexCommit getIndexCommit() { } @Override - protected void doClose() { - } + protected void doClose() {} @Override public CacheHelper getReaderCacheHelper() { @@ -164,7 +175,7 @@ private synchronized ElasticsearchDirectoryReader getOrOpenReader() throws IOExc try { reader = getReader(); if (reader == null) { - for (ReferenceManager.RefreshListener listeners : config ().getInternalRefreshListener()) { + for (ReferenceManager.RefreshListener listeners : config().getInternalRefreshListener()) { listeners.beforeRefresh(); } final DirectoryReader dirReader = openDirectory(engineConfig.getStore().directory()); @@ -255,17 +266,35 @@ private Engine.Searcher openSearcher(String source, SearcherScope scope) throws if (reader == null) { if (CAN_MATCH_SEARCH_SOURCE.equals(source) || FIELD_RANGE_SEARCH_SOURCE.equals(source)) { canMatchReader.incRef(); - return new Searcher(source, canMatchReader, engineConfig.getSimilarity(), engineConfig.getQueryCache(), - engineConfig.getQueryCachingPolicy(), canMatchReader::decRef); + return new Searcher( + source, + canMatchReader, + engineConfig.getSimilarity(), + engineConfig.getQueryCache(), + engineConfig.getQueryCachingPolicy(), + canMatchReader::decRef + ); } else { ReferenceManager manager = getReferenceManager(scope); ElasticsearchDirectoryReader acquire = manager.acquire(); - return new Searcher(source, acquire, engineConfig.getSimilarity(), engineConfig.getQueryCache(), - engineConfig.getQueryCachingPolicy(), () -> manager.release(acquire)); + return new Searcher( + source, + acquire, + engineConfig.getSimilarity(), + engineConfig.getQueryCache(), + engineConfig.getQueryCachingPolicy(), + () -> manager.release(acquire) + ); } } else { - return new Searcher(source, reader, engineConfig.getSimilarity(), engineConfig.getQueryCache(), - engineConfig.getQueryCachingPolicy(), () -> closeReader(reader)); + return new Searcher( + source, + reader, + engineConfig.getSimilarity(), + engineConfig.getQueryCache(), + engineConfig.getQueryCachingPolicy(), + () -> closeReader(reader) + ); } } @@ -286,9 +315,8 @@ public SegmentsStats segmentsStats(boolean includeSegmentFileSizes, boolean incl @Override protected void closeNoLock(String reason, CountDownLatch closedLatch) { super.closeNoLock(reason, closedLatch); - synchronized(closedListeners) { - IOUtils.closeWhileHandlingException( - closedListeners.stream().map(t -> (Closeable) () -> t.onClose(cacheIdentity))::iterator); + synchronized (closedListeners) { + IOUtils.closeWhileHandlingException(closedListeners.stream().map(t -> (Closeable) () -> t.onClose(cacheIdentity))::iterator); closedListeners.clear(); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/index/engine/frozen/RewriteCachingDirectoryReader.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/index/engine/frozen/RewriteCachingDirectoryReader.java index 5e7dbfff564e7..9472df3cf3593 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/index/engine/frozen/RewriteCachingDirectoryReader.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/index/engine/frozen/RewriteCachingDirectoryReader.java @@ -42,8 +42,8 @@ */ final class RewriteCachingDirectoryReader extends DirectoryReader { - RewriteCachingDirectoryReader(Directory directory, List segmentReaders, - Comparator leafSorter) throws IOException { + RewriteCachingDirectoryReader(Directory directory, List segmentReaders, Comparator leafSorter) + throws IOException { super(directory, wrap(segmentReaders), leafSorter); } @@ -242,8 +242,7 @@ public PointValues getPointValues(String field) { } @Override - public void checkIntegrity() { - } + public void checkIntegrity() {} @Override public LeafMetaData getMetaData() { @@ -271,8 +270,7 @@ public void document(int docID, StoredFieldVisitor visitor) { } @Override - protected void doClose() { - } + protected void doClose() {} @Override public CacheHelper getReaderCacheHelper() { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/CryptUtils.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/CryptUtils.java index 90f2c204348da..1978c02db4cc1 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/CryptUtils.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/CryptUtils.java @@ -6,37 +6,48 @@ */ package org.elasticsearch.license; - -import javax.crypto.BadPaddingException; -import javax.crypto.Cipher; -import javax.crypto.IllegalBlockSizeException; -import javax.crypto.NoSuchPaddingException; -import javax.crypto.SecretKey; -import javax.crypto.SecretKeyFactory; -import javax.crypto.spec.PBEKeySpec; -import javax.crypto.spec.SecretKeySpec; import java.nio.charset.StandardCharsets; import java.security.GeneralSecurityException; +import java.security.InvalidKeyException; import java.security.KeyFactory; import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.PublicKey; import java.security.SecureRandom; -import java.security.NoSuchAlgorithmException; -import java.security.InvalidKeyException; import java.security.spec.InvalidKeySpecException; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import java.util.Base64; +import javax.crypto.BadPaddingException; +import javax.crypto.Cipher; +import javax.crypto.IllegalBlockSizeException; +import javax.crypto.NoSuchPaddingException; +import javax.crypto.SecretKey; +import javax.crypto.SecretKeyFactory; +import javax.crypto.spec.PBEKeySpec; +import javax.crypto.spec.SecretKeySpec; + public class CryptUtils { // SALT must be at least 128bits for FIPS 140-2 compliance private static final byte[] SALT = { - (byte) 0x74, (byte) 0x68, (byte) 0x69, (byte) 0x73, - (byte) 0x69, (byte) 0x73, (byte) 0x74, (byte) 0x68, - (byte) 0x65, (byte) 0x73, (byte) 0x61, (byte) 0x6C, - (byte) 0x74, (byte) 0x77, (byte) 0x65, (byte) 0x75 - }; + (byte) 0x74, + (byte) 0x68, + (byte) 0x69, + (byte) 0x73, + (byte) 0x69, + (byte) 0x73, + (byte) 0x74, + (byte) 0x68, + (byte) 0x65, + (byte) 0x73, + (byte) 0x61, + (byte) 0x6C, + (byte) 0x74, + (byte) 0x77, + (byte) 0x65, + (byte) 0x75 }; private static final String KEY_ALGORITHM = "RSA"; private static final char[] DEFAULT_PASS_PHRASE = "elasticsearch-license".toCharArray(); private static final String KDF_ALGORITHM = "PBKDF2WithHmacSHA512"; @@ -161,21 +172,16 @@ static byte[] decryptV3Format(byte[] data) { } private static SecretKey getV3Key() throws NoSuchAlgorithmException, InvalidKeySpecException { - final byte[] salt = { - (byte) 0xA9, (byte) 0xA2, (byte) 0xB5, (byte) 0xDE, - (byte) 0x2A, (byte) 0x8A, (byte) 0x9A, (byte) 0xE6 - }; + final byte[] salt = { (byte) 0xA9, (byte) 0xA2, (byte) 0xB5, (byte) 0xDE, (byte) 0x2A, (byte) 0x8A, (byte) 0x9A, (byte) 0xE6 }; final byte[] passBytes = "elasticsearch-license".getBytes(StandardCharsets.UTF_8); final byte[] digest = MessageDigest.getInstance("SHA-512").digest(passBytes); final char[] hashedPassphrase = Base64.getEncoder().encodeToString(digest).toCharArray(); PBEKeySpec keySpec = new PBEKeySpec(hashedPassphrase, salt, 1024, 128); - byte[] shortKey = SecretKeyFactory.getInstance("PBEWithSHA1AndDESede"). - generateSecret(keySpec).getEncoded(); + byte[] shortKey = SecretKeyFactory.getInstance("PBEWithSHA1AndDESede").generateSecret(keySpec).getEncoded(); byte[] intermediaryKey = new byte[16]; for (int i = 0, j = 0; i < 16; i++) { intermediaryKey[i] = shortKey[j]; - if (++j == shortKey.length) - j = 0; + if (++j == shortKey.length) j = 0; } return new SecretKeySpec(intermediaryKey, "AES"); } @@ -184,8 +190,7 @@ private static SecretKey deriveSecretKey(char[] passPhrase) { try { PBEKeySpec keySpec = new PBEKeySpec(passPhrase, SALT, KDF_ITERATION_COUNT, ENCRYPTION_KEY_LENGTH); - SecretKey secretKey = SecretKeyFactory.getInstance(KDF_ALGORITHM). - generateSecret(keySpec); + SecretKey secretKey = SecretKeyFactory.getInstance(KDF_ALGORITHM).generateSecret(keySpec); return new SecretKeySpec(secretKey.getEncoded(), CIPHER_ALGORITHM); } catch (NoSuchAlgorithmException | InvalidKeySpecException e) { throw new IllegalStateException(e); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/DeleteLicenseRequestBuilder.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/DeleteLicenseRequestBuilder.java index a1db2f381ddcd..f8ef08e17de25 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/DeleteLicenseRequestBuilder.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/DeleteLicenseRequestBuilder.java @@ -11,8 +11,10 @@ import org.elasticsearch.client.ElasticsearchClient; import org.elasticsearch.protocol.xpack.license.DeleteLicenseRequest; -public class DeleteLicenseRequestBuilder extends AcknowledgedRequestBuilder { +public class DeleteLicenseRequestBuilder extends AcknowledgedRequestBuilder< + DeleteLicenseRequest, + AcknowledgedResponse, + DeleteLicenseRequestBuilder> { public DeleteLicenseRequestBuilder(ElasticsearchClient client) { this(client, DeleteLicenseAction.INSTANCE); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/ExpirationCallback.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/ExpirationCallback.java index 49bdda9c0a987..0f565212a6fe1 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/ExpirationCallback.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/ExpirationCallback.java @@ -15,7 +15,10 @@ abstract class ExpirationCallback { static final String EXPIRATION_JOB_PREFIX = ".license_expiration_job_"; - public enum Orientation {PRE, POST} + public enum Orientation { + PRE, + POST + } /** * Callback that is triggered every frequency when @@ -166,8 +169,13 @@ public final long nextScheduledTimeForExpiry(long expiryDate, long startTime, lo public abstract void on(License license); public final String toString() { - return LoggerMessageFormat.format(null, "ExpirationCallback:(orientation [{}], min [{}], max [{}], freq [{}])", - orientation.name(), TimeValue.timeValueMillis(min), TimeValue.timeValueMillis(max), - TimeValue.timeValueMillis(frequency)); + return LoggerMessageFormat.format( + null, + "ExpirationCallback:(orientation [{}], min [{}], max [{}], freq [{}])", + orientation.name(), + TimeValue.timeValueMillis(min), + TimeValue.timeValueMillis(max), + TimeValue.timeValueMillis(frequency) + ); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/GetBasicStatusRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/GetBasicStatusRequest.java index f2901dfc78e5b..5883c36c9e2c5 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/GetBasicStatusRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/GetBasicStatusRequest.java @@ -14,8 +14,7 @@ public class GetBasicStatusRequest extends MasterNodeReadRequest { - public GetBasicStatusRequest() { - } + public GetBasicStatusRequest() {} public GetBasicStatusRequest(StreamInput in) throws IOException { super(in); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/GetFeatureUsageResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/GetFeatureUsageResponse.java index 7d1a6333757ee..8a91c9b0d2456 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/GetFeatureUsageResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/GetFeatureUsageResponse.java @@ -12,9 +12,9 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.core.Nullable; import java.io.IOException; import java.time.Instant; @@ -33,8 +33,13 @@ public static class FeatureUsageInfo implements Writeable { private final String context; private final String licenseLevel; - public FeatureUsageInfo(@Nullable String family, String name, ZonedDateTime lastUsedTime, - @Nullable String context, String licenseLevel) { + public FeatureUsageInfo( + @Nullable String family, + String name, + ZonedDateTime lastUsedTime, + @Nullable String context, + String licenseLevel + ) { this.family = family; this.name = Objects.requireNonNull(name, "Feature name may not be null"); this.lastUsedTime = Objects.requireNonNull(lastUsedTime, "Last used time may not be null"); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/GetLicenseRequestBuilder.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/GetLicenseRequestBuilder.java index 416e66987d46e..85b2590c856af 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/GetLicenseRequestBuilder.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/GetLicenseRequestBuilder.java @@ -10,8 +10,10 @@ import org.elasticsearch.client.ElasticsearchClient; import org.elasticsearch.protocol.xpack.license.GetLicenseRequest; -public class GetLicenseRequestBuilder extends MasterNodeReadOperationRequestBuilder { +public class GetLicenseRequestBuilder extends MasterNodeReadOperationRequestBuilder< + GetLicenseRequest, + GetLicenseResponse, + GetLicenseRequestBuilder> { public GetLicenseRequestBuilder(ElasticsearchClient client) { this(client, GetLicenseAction.INSTANCE); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/GetTrialStatusRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/GetTrialStatusRequest.java index cbda5358ab0c5..93a0206ac70c3 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/GetTrialStatusRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/GetTrialStatusRequest.java @@ -14,8 +14,7 @@ public class GetTrialStatusRequest extends MasterNodeReadRequest { - public GetTrialStatusRequest() { - } + public GetTrialStatusRequest() {} public GetTrialStatusRequest(StreamInput in) throws IOException { super(in); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/License.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/License.java index f08c147236991..b62afdd39c818 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/License.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/License.java @@ -14,6 +14,8 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.core.RestApiVersion; +import org.elasticsearch.protocol.xpack.license.LicenseStatus; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; @@ -21,8 +23,6 @@ import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.core.RestApiVersion; -import org.elasticsearch.protocol.xpack.license.LicenseStatus; import java.io.IOException; import java.io.InputStream; @@ -58,8 +58,13 @@ public static LicenseType parse(String type) throws IllegalArgumentException { try { return LicenseType.valueOf(type.toUpperCase(Locale.ROOT)); } catch (IllegalArgumentException e) { - throw new IllegalArgumentException("unrecognised license type [ " + type + "], supported license types are [" - + Stream.of(values()).map(LicenseType::getTypeName).collect(Collectors.joining(",")) + "]"); + throw new IllegalArgumentException( + "unrecognised license type [ " + + type + + "], supported license types are [" + + Stream.of(values()).map(LicenseType::getTypeName).collect(Collectors.joining(",")) + + "]" + ); } } @@ -239,8 +244,13 @@ public static OperationMode parse(String mode) { try { return OperationMode.valueOf(mode.toUpperCase(Locale.ROOT)); } catch (IllegalArgumentException e) { - throw new IllegalArgumentException("unrecognised license operating mode [ " + mode + "], supported modes are [" - + Stream.of(values()).map(OperationMode::description).collect(Collectors.joining(",")) + "]"); + throw new IllegalArgumentException( + "unrecognised license operating mode [ " + + mode + + "], supported modes are [" + + Stream.of(values()).map(OperationMode::description).collect(Collectors.joining(",")) + + "]" + ); } } @@ -249,8 +259,21 @@ public String description() { } } - private License(int version, String uid, String issuer, String issuedTo, long issueDate, String type, String subscriptionType, - String feature, String signature, long expiryDate, int maxNodes, int maxResourceUnits, long startDate) { + private License( + int version, + String uid, + String issuer, + String issuedTo, + long issueDate, + String type, + String subscriptionType, + String feature, + String signature, + long expiryDate, + int maxNodes, + int maxResourceUnits, + long startDate + ) { this.version = version; this.uid = uid; this.issuer = issuer; @@ -441,8 +464,9 @@ private static void validateLimits(String type, int maxNodes, int maxResourceUni if (maxNodes == -1) { throw new IllegalStateException("maxNodes has to be set"); } else if (maxResourceUnits != -1) { - throw new IllegalStateException("maxResourceUnits may only be set for enterprise licenses (not permitted for type=[" + - type + "])"); + throw new IllegalStateException( + "maxResourceUnits may only be set for enterprise licenses (not permitted for type=[" + type + "])" + ); } } } @@ -653,8 +677,9 @@ public static License fromXContent(XContentParser parser) throws IOException { if (version == 0) { throw new ElasticsearchParseException("malformed signature for license [" + builder.uid + "]"); } else if (version > VERSION_CURRENT) { - throw new ElasticsearchParseException("Unknown license version found, please upgrade all nodes to the latest " + - "elasticsearch-license plugin"); + throw new ElasticsearchParseException( + "Unknown license version found, please upgrade all nodes to the latest " + "elasticsearch-license plugin" + ); } // signature version is the source of truth builder.version(version); @@ -684,9 +709,11 @@ public static License fromSource(BytesReference bytes, XContentType xContentType throw new ElasticsearchParseException("failed to parse license - no content-type provided"); } // EMPTY is safe here because we don't call namedObject - try (InputStream byteStream = bytes.streamInput(); - XContentParser parser = xContentType.xContent() - .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, byteStream)) { + try ( + InputStream byteStream = bytes.streamInput(); + XContentParser parser = xContentType.xContent() + .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, byteStream) + ) { License license = null; if (parser.nextToken() == XContentParser.Token.START_OBJECT) { if (parser.nextToken() == XContentParser.Token.FIELD_NAME) { @@ -895,8 +922,7 @@ public Builder startDate(long startDate) { } public Builder fromLicenseSpec(License license, String signature) { - return uid(license.uid()) - .version(license.version()) + return uid(license.uid()).version(license.version()) .issuedTo(license.issuedTo()) .issueDate(license.issueDate()) .startDate(license.startDate()) @@ -915,16 +941,28 @@ public Builder fromLicenseSpec(License license, String signature) { * to the new license format */ public Builder fromPre20LicenseSpec(License pre20License) { - return uid(pre20License.uid()) - .issuedTo(pre20License.issuedTo()) + return uid(pre20License.uid()).issuedTo(pre20License.issuedTo()) .issueDate(pre20License.issueDate()) .maxNodes(pre20License.maxNodes()) .expiryDate(pre20License.expiryDate()); } public License build() { - return new License(version, uid, issuer, issuedTo, issueDate, type, - subscriptionType, feature, signature, expiryDate, maxNodes, maxResourceUnits, startDate); + return new License( + version, + uid, + issuer, + issuedTo, + issueDate, + type, + subscriptionType, + feature, + signature, + expiryDate, + maxNodes, + maxResourceUnits, + startDate + ); } public Builder validate() { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/LicenseService.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/LicenseService.java index 10124e5a27f44..343520657ebab 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/LicenseService.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/LicenseService.java @@ -59,23 +59,35 @@ public class LicenseService extends AbstractLifecycleComponent implements ClusterStateListener, SchedulerEngine.Listener { private static final Logger logger = LogManager.getLogger(LicenseService.class); - public static final Setting SELF_GENERATED_LICENSE_TYPE = new Setting<>("xpack.license.self_generated.type", - (s) -> License.LicenseType.BASIC.getTypeName(), (s) -> { - final License.LicenseType type = License.LicenseType.parse(s); - return SelfGeneratedLicense.validateSelfGeneratedType(type); - }, Setting.Property.NodeScope); + public static final Setting SELF_GENERATED_LICENSE_TYPE = new Setting<>( + "xpack.license.self_generated.type", + (s) -> License.LicenseType.BASIC.getTypeName(), + (s) -> { + final License.LicenseType type = License.LicenseType.parse(s); + return SelfGeneratedLicense.validateSelfGeneratedType(type); + }, + Setting.Property.NodeScope + ); static final List ALLOWABLE_UPLOAD_TYPES = getAllowableUploadTypes(); - public static final Setting> ALLOWED_LICENSE_TYPES_SETTING = Setting.listSetting("xpack.license.upload.types", + public static final Setting> ALLOWED_LICENSE_TYPES_SETTING = Setting.listSetting( + "xpack.license.upload.types", ALLOWABLE_UPLOAD_TYPES.stream().map(License.LicenseType::getTypeName).collect(Collectors.toUnmodifiableList()), - License.LicenseType::parse, LicenseService::validateUploadTypesSetting, Setting.Property.NodeScope); + License.LicenseType::parse, + LicenseService::validateUploadTypesSetting, + Setting.Property.NodeScope + ); // pkg private for tests static final TimeValue NON_BASIC_SELF_GENERATED_LICENSE_DURATION = TimeValue.timeValueHours(30 * 24); static final Set VALID_TRIAL_TYPES = Set.of( - License.LicenseType.GOLD, License.LicenseType.PLATINUM, License.LicenseType.ENTERPRISE, License.LicenseType.TRIAL); + License.LicenseType.GOLD, + License.LicenseType.PLATINUM, + License.LicenseType.ENTERPRISE, + License.LicenseType.TRIAL + ); /** * Period before the license expires when warning starts being added to the response header @@ -127,20 +139,30 @@ public class LicenseService extends AbstractLifecycleComponent implements Cluste public static final DateFormatter DATE_FORMATTER = DateFormatter.forPattern("EEEE, MMMM dd, yyyy"); - private static final String ACKNOWLEDGEMENT_HEADER = "This license update requires acknowledgement. To acknowledge the license, " + - "please read the following messages and update the license again, this time with the \"acknowledge=true\" parameter:"; - - public LicenseService(Settings settings, ThreadPool threadPool, ClusterService clusterService, Clock clock, Environment env, - ResourceWatcherService resourceWatcherService, XPackLicenseState licenseState) { + private static final String ACKNOWLEDGEMENT_HEADER = "This license update requires acknowledgement. To acknowledge the license, " + + "please read the following messages and update the license again, this time with the \"acknowledge=true\" parameter:"; + + public LicenseService( + Settings settings, + ThreadPool threadPool, + ClusterService clusterService, + Clock clock, + Environment env, + ResourceWatcherService resourceWatcherService, + XPackLicenseState licenseState + ) { this.settings = settings; this.clusterService = clusterService; this.clock = clock; this.scheduler = new SchedulerEngine(settings, clock); this.licenseState = licenseState; this.allowedLicenseTypes = ALLOWED_LICENSE_TYPES_SETTING.get(settings); - this.operationModeFileWatcher = new OperationModeFileWatcher(resourceWatcherService, - XPackPlugin.resolveConfigFile(env, "license_mode"), logger, - () -> updateLicenseState(getLicensesMetadata())); + this.operationModeFileWatcher = new OperationModeFileWatcher( + resourceWatcherService, + XPackPlugin.resolveConfigFile(env, "license_mode"), + logger, + () -> updateLicenseState(getLicensesMetadata()) + ); this.scheduler.register(this); populateExpirationCallbacks(); @@ -153,10 +175,15 @@ private void logExpirationWarning(long expirationMillis, boolean expired) { static CharSequence buildExpirationMessage(long expirationMillis, boolean expired) { String expiredMsg = expired ? "expired" : "will expire"; - String general = LoggerMessageFormat.format(null, "License [{}] on [{}].\n" + - "# If you have a new license, please update it. Otherwise, please reach out to\n" + - "# your support contact.\n" + - "# ", expiredMsg, DATE_FORMATTER.formatMillis(expirationMillis)); + String general = LoggerMessageFormat.format( + null, + "License [{}] on [{}].\n" + + "# If you have a new license, please update it. Otherwise, please reach out to\n" + + "# your support contact.\n" + + "# ", + expiredMsg, + DATE_FORMATTER.formatMillis(expirationMillis) + ); if (expired) { general = general.toUpperCase(Locale.ROOT); } @@ -218,8 +245,9 @@ public void registerLicense(final PutLicenseRequest request, final ActionListene if (licenseType == License.LicenseType.BASIC) { listener.onFailure(new IllegalArgumentException("Registering basic licenses is not allowed.")); } else if (isAllowedLicenseType(licenseType) == false) { - listener.onFailure(new IllegalArgumentException( - "Registering [" + licenseType.getTypeName() + "] licenses is not allowed on this cluster")); + listener.onFailure( + new IllegalArgumentException("Registering [" + licenseType.getTypeName() + "] licenses is not allowed on this cluster") + ); } else if (newLicense.expiryDate() < now) { listener.onResponse(new PutLicenseResponse(true, LicensesStatus.EXPIRED)); } else { @@ -230,8 +258,9 @@ public void registerLicense(final PutLicenseRequest request, final ActionListene Map acknowledgeMessages = getAckMessages(newLicense, currentLicense); if (acknowledgeMessages.isEmpty() == false) { // needs acknowledgement - listener.onResponse(new PutLicenseResponse(false, LicensesStatus.VALID, ACKNOWLEDGEMENT_HEADER, - acknowledgeMessages)); + listener.onResponse( + new PutLicenseResponse(false, LicensesStatus.VALID, ACKNOWLEDGEMENT_HEADER, acknowledgeMessages) + ); return; } } @@ -243,13 +272,15 @@ public void registerLicense(final PutLicenseRequest request, final ActionListene if (XPackSettings.SECURITY_ENABLED.get(settings)) { if (XPackSettings.FIPS_MODE_ENABLED.get(settings) && false == XPackLicenseState.isFipsAllowedForOperationMode(newLicense.operationMode())) { - throw new IllegalStateException("Cannot install a [" + newLicense.operationMode() + - "] license unless FIPS mode is disabled"); + throw new IllegalStateException( + "Cannot install a [" + newLicense.operationMode() + "] license unless FIPS mode is disabled" + ); } } - clusterService.submitStateUpdateTask("register license [" + newLicense.uid() + "]", new - AckedClusterStateUpdateTask(request, listener) { + clusterService.submitStateUpdateTask( + "register license [" + newLicense.uid() + "]", + new AckedClusterStateUpdateTask(request, listener) { @Override protected PutLicenseResponse newResponse(boolean acknowledged) { return new PutLicenseResponse(acknowledged, LicensesStatus.VALID); @@ -260,8 +291,9 @@ public ClusterState execute(ClusterState currentState) throws Exception { XPackPlugin.checkReadyForXPackCustomMetadata(currentState); final Version oldestNodeVersion = currentState.nodes().getSmallestNonClientNodeVersion(); if (licenseIsCompatible(newLicense, oldestNodeVersion) == false) { - throw new IllegalStateException("The provided license is not compatible with node version [" + - oldestNodeVersion + "]"); + throw new IllegalStateException( + "The provided license is not compatible with node version [" + oldestNodeVersion + "]" + ); } Metadata currentMetadata = currentState.metadata(); LicensesMetadata licensesMetadata = currentMetadata.custom(LicensesMetadata.TYPE); @@ -273,7 +305,8 @@ public ClusterState execute(ClusterState currentState) throws Exception { mdBuilder.putCustom(LicensesMetadata.TYPE, new LicensesMetadata(newLicense, trialVersion)); return ClusterState.builder(currentState).metadata(mdBuilder).build(); } - }); + } + ); } } @@ -291,9 +324,12 @@ public static Map getAckMessages(License newLicense, License c Map acknowledgeMessages = new HashMap<>(); if (License.isAutoGeneratedLicense(currentLicense.signature()) == false // current license is not auto-generated && currentLicense.issueDate() > newLicense.issueDate()) { // and has a later issue date - acknowledgeMessages.put("license", new String[] { - "The new license is older than the currently installed license. " + - "Are you sure you want to override the current license?" }); + acknowledgeMessages.put( + "license", + new String[] { + "The new license is older than the currently installed license. " + + "Are you sure you want to override the current license?" } + ); } XPackLicenseState.ACKNOWLEDGMENT_MESSAGES.forEach((feature, ackMessages) -> { String[] messages = ackMessages.apply(currentLicense.operationMode(), newLicense.operationMode()); @@ -304,7 +340,6 @@ public static Map getAckMessages(License newLicense, License c return acknowledgeMessages; } - private static TimeValue days(int days) { return TimeValue.timeValueHours(days * 24); } @@ -329,8 +364,10 @@ public void triggered(SchedulerEngine.Event event) { */ public void removeLicense(final DeleteLicenseRequest request, final ActionListener listener) { final PostStartBasicRequest startBasicRequest = new PostStartBasicRequest().acknowledge(true); - clusterService.submitStateUpdateTask("delete license", - new StartBasicClusterTask(logger, clusterService.getClusterName().value(), clock, startBasicRequest, listener)); + clusterService.submitStateUpdateTask( + "delete license", + new StartBasicClusterTask(logger, clusterService.getClusterName().value(), clock, startBasicRequest, listener) + ); } public License getLicense() { @@ -345,8 +382,13 @@ private LicensesMetadata getLicensesMetadata() { void startTrialLicense(PostStartTrialRequest request, final ActionListener listener) { License.LicenseType requestedType = License.LicenseType.parse(request.getType()); if (VALID_TRIAL_TYPES.contains(requestedType) == false) { - throw new IllegalArgumentException("Cannot start trial of type [" + requestedType.getTypeName() + "]. Valid trial types are [" - + VALID_TRIAL_TYPES.stream().map(License.LicenseType::getTypeName).sorted().collect(Collectors.joining(",")) + "]"); + throw new IllegalArgumentException( + "Cannot start trial of type [" + + requestedType.getTypeName() + + "]. Valid trial types are [" + + VALID_TRIAL_TYPES.stream().map(License.LicenseType::getTypeName).sorted().collect(Collectors.joining(",")) + + "]" + ); } StartTrialClusterTask task = new StartTrialClusterTask(logger, clusterService.getClusterName().value(), clock, request, listener); clusterService.submitStateUpdateTask("started trial license", task); @@ -364,8 +406,10 @@ void startBasicLicense(PostStartBasicRequest request, final ActionListener XPackPlugin.nodesNotReadyForXPackCustomMetadata(currentClusterState)); + logger.debug( + "cannot add license to cluster as the following nodes might not understand the license metadata: {}", + () -> XPackPlugin.nodesNotReadyForXPackCustomMetadata(currentClusterState) + ); return; } @@ -422,8 +468,7 @@ public void clusterChanged(ClusterChangedEvent event) { logger.debug("current [{}]", currentLicensesMetadata); } // notify all interested plugins - if (previousClusterState.blocks().hasGlobalBlock(GatewayService.STATE_NOT_RECOVERED_BLOCK) - || prevLicensesMetadata == null) { + if (previousClusterState.blocks().hasGlobalBlock(GatewayService.STATE_NOT_RECOVERED_BLOCK) || prevLicensesMetadata == null) { if (currentLicensesMetadata != null) { onUpdate(currentLicensesMetadata); } @@ -444,9 +489,10 @@ public void clusterChanged(ClusterChangedEvent event) { boolean noLicense = noLicenseInPrevMetadata && noLicenseInCurrentMetadata; // auto-generate license if no licenses ever existed or if the current license is basic and // needs extended or if the license signature needs to be updated. this will trigger a subsequent cluster changed event - if (currentClusterState.getNodes().isLocalNodeElectedMaster() && - (noLicense || LicenseUtils.licenseNeedsExtended(currentLicense) || - LicenseUtils.signatureNeedsUpdate(currentLicense, currentClusterState.nodes()))) { + if (currentClusterState.getNodes().isLocalNodeElectedMaster() + && (noLicense + || LicenseUtils.licenseNeedsExtended(currentLicense) + || LicenseUtils.signatureNeedsUpdate(currentLicense, currentClusterState.nodes()))) { registerOrUpdateSelfGeneratedLicense(); } } else if (logger.isDebugEnabled()) { @@ -464,11 +510,15 @@ protected static String getExpiryWarning(long licenseExpiryDate, long currentTim final long diff = licenseExpiryDate - currentTime; if (LICENSE_EXPIRATION_WARNING_PERIOD.getMillis() > diff) { final long days = TimeUnit.MILLISECONDS.toDays(diff); - final String expiryMessage = (days == 0 && diff > 0)? "expires today": - (diff > 0? String.format(Locale.ROOT, "will expire in [%d] days", days): - String.format(Locale.ROOT, "expired on [%s]", LicenseService.DATE_FORMATTER.formatMillis(licenseExpiryDate))); - return "Your license " + expiryMessage + ". " + - "Contact your administrator or update your license for continued use of features"; + final String expiryMessage = (days == 0 && diff > 0) + ? "expires today" + : (diff > 0 + ? String.format(Locale.ROOT, "will expire in [%d] days", days) + : String.format(Locale.ROOT, "expired on [%s]", LicenseService.DATE_FORMATTER.formatMillis(licenseExpiryDate))); + return "Your license " + + expiryMessage + + ". " + + "Contact your administrator or update your license for continued use of features"; } return null; } @@ -477,7 +527,7 @@ protected void updateLicenseState(final License license) { long time = clock.millis(); if (license == LicensesMetadata.LICENSE_TOMBSTONE) { // implies license has been explicitly deleted - licenseState.update(License.OperationMode.MISSING,false, getExpiryWarning(license.expiryDate(), time)); + licenseState.update(License.OperationMode.MISSING, false, getExpiryWarning(license.expiryDate(), time)); return; } if (license != null) { @@ -514,16 +564,18 @@ private void onUpdate(final LicensesMetadata currentLicensesMetadata) { license.setOperationModeFileWatcher(operationModeFileWatcher); scheduler.add(new SchedulerEngine.Job(LICENSE_JOB, nextLicenseCheck(license))); for (ExpirationCallback expirationCallback : expirationCallbacks) { - scheduler.add(new SchedulerEngine.Job(expirationCallback.getId(), - (startTime, now) -> - expirationCallback.nextScheduledTimeForExpiry(license.expiryDate(), startTime, now))); + scheduler.add( + new SchedulerEngine.Job( + expirationCallback.getId(), + (startTime, now) -> expirationCallback.nextScheduledTimeForExpiry(license.expiryDate(), startTime, now) + ) + ); } if (previousLicense != null) { // remove operationModeFileWatcher to gc the old license object previousLicense.removeOperationModeFileWatcher(); } - logger.info("license [{}] mode [{}] - valid", license.uid(), - license.operationMode().name().toLowerCase(Locale.ROOT)); + logger.info("license [{}] mode [{}] - valid", license.uid(), license.operationMode().name().toLowerCase(Locale.ROOT)); } updateLicenseState(license); } @@ -574,18 +626,20 @@ static License getLicense(final LicensesMetadata metadata) { } private static List getAllowableUploadTypes() { - return Stream.of(License.LicenseType.values()) - .filter(t -> t != License.LicenseType.BASIC) - .collect(Collectors.toUnmodifiableList()); + return Stream.of(License.LicenseType.values()).filter(t -> t != License.LicenseType.BASIC).collect(Collectors.toUnmodifiableList()); } private static void validateUploadTypesSetting(List value) { if (ALLOWABLE_UPLOAD_TYPES.containsAll(value) == false) { - throw new IllegalArgumentException("Invalid value [" + - value.stream().map(License.LicenseType::getTypeName).collect(Collectors.joining(",")) + - "] for " + ALLOWED_LICENSE_TYPES_SETTING.getKey() + ", allowed values are [" + - ALLOWABLE_UPLOAD_TYPES.stream().map(License.LicenseType::getTypeName).collect(Collectors.joining(",")) + - "]"); + throw new IllegalArgumentException( + "Invalid value [" + + value.stream().map(License.LicenseType::getTypeName).collect(Collectors.joining(",")) + + "] for " + + ALLOWED_LICENSE_TYPES_SETTING.getKey() + + ", allowed values are [" + + ALLOWABLE_UPLOAD_TYPES.stream().map(License.LicenseType::getTypeName).collect(Collectors.joining(",")) + + "]" + ); } } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/LicenseUtils.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/LicenseUtils.java index 94f2588276a97..c5604c8e80bb3 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/LicenseUtils.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/LicenseUtils.java @@ -24,8 +24,11 @@ public class LicenseUtils { * exception's rest header */ public static ElasticsearchSecurityException newComplianceException(String feature) { - ElasticsearchSecurityException e = new ElasticsearchSecurityException("current license is non-compliant for [{}]", - RestStatus.FORBIDDEN, feature); + ElasticsearchSecurityException e = new ElasticsearchSecurityException( + "current license is non-compliant for [{}]", + RestStatus.FORBIDDEN, + feature + ); e.addMetadata(EXPIRED_FEATURE_METADATA, feature); return e; } @@ -39,8 +42,7 @@ public static boolean isLicenseExpiredException(ElasticsearchSecurityException e } public static boolean licenseNeedsExtended(License license) { - return LicenseType.isBasic(license.type()) && - license.expiryDate() != LicenseService.BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS; + return LicenseType.isBasic(license.type()) && license.expiryDate() != LicenseService.BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS; } /** @@ -52,10 +54,9 @@ public static boolean signatureNeedsUpdate(License license, DiscoveryNodes curre String typeName = license.type(); return (LicenseType.isBasic(typeName) || LicenseType.isTrial(typeName)) && - // only upgrade signature when all nodes are ready to deserialize the new signature - (license.version() < License.VERSION_CRYPTO_ALGORITHMS && - compatibleLicenseVersion(currentNodes) >= License.VERSION_CRYPTO_ALGORITHMS - ); + // only upgrade signature when all nodes are ready to deserialize the new signature + (license.version() < License.VERSION_CRYPTO_ALGORITHMS + && compatibleLicenseVersion(currentNodes) >= License.VERSION_CRYPTO_ALGORITHMS); } public static int compatibleLicenseVersion(DiscoveryNodes currentNodes) { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/LicenseVerifier.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/LicenseVerifier.java index fd4df172b1c7b..f31c7096bae68 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/LicenseVerifier.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/LicenseVerifier.java @@ -9,11 +9,11 @@ import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.BytesRefIterator; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.core.internal.io.Streams; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.core.internal.io.Streams; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -61,7 +61,7 @@ public static boolean verifyLicense(final License license, byte[] publicKeyData) rsa.initVerify(CryptUtils.readPublicKey(publicKeyData)); BytesRefIterator iterator = BytesReference.bytes(contentBuilder).iterator(); BytesRef ref; - while((ref = iterator.next()) != null) { + while ((ref = iterator.next()) != null) { rsa.update(ref.bytes, ref.offset, ref.length); } return rsa.verify(signedContent); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/LicensedAllocatedPersistentTask.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/LicensedAllocatedPersistentTask.java index 82ee5fd271ab9..7c7096850e3f8 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/LicensedAllocatedPersistentTask.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/LicensedAllocatedPersistentTask.java @@ -22,9 +22,17 @@ public class LicensedAllocatedPersistentTask extends AllocatedPersistentTask { private final String featureContext; private final XPackLicenseState licenseState; - public LicensedAllocatedPersistentTask(long id, String type, String action, String description, TaskId parentTask, - Map headers, LicensedFeature.Persistent feature, String featureContext, - XPackLicenseState licenseState) { + public LicensedAllocatedPersistentTask( + long id, + String type, + String action, + String description, + TaskId parentTask, + Map headers, + LicensedFeature.Persistent feature, + String featureContext, + XPackLicenseState licenseState + ) { super(id, type, action, description, parentTask, headers); this.licensedFeature = feature; this.featureContext = featureContext; @@ -78,8 +86,12 @@ protected void doMarkAsLocallyAborted(String localAbortReason) { // this is made public for tests, and final to ensure it is not overridden with something that may throw @Override - public final void init(PersistentTasksService persistentTasksService, TaskManager taskManager, - String persistentTaskId, long allocationId) { + public final void init( + PersistentTasksService persistentTasksService, + TaskManager taskManager, + String persistentTaskId, + long allocationId + ) { super.init(persistentTasksService, taskManager, persistentTaskId, allocationId); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/LicensesMetadata.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/LicensesMetadata.java index 9928d664c1115..10d11553ddfd7 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/LicensesMetadata.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/LicensesMetadata.java @@ -11,12 +11,12 @@ import org.elasticsearch.cluster.MergableCustomMetadata; import org.elasticsearch.cluster.NamedDiff; import org.elasticsearch.cluster.metadata.Metadata; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.license.License.OperationMode; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.license.License.OperationMode; import java.io.IOException; import java.util.EnumSet; @@ -25,7 +25,9 @@ /** * Contains metadata about registered licenses */ -public class LicensesMetadata extends AbstractNamedDiffable implements Metadata.Custom, +public class LicensesMetadata extends AbstractNamedDiffable + implements + Metadata.Custom, MergableCustomMetadata { public static final String TYPE = "licenses"; @@ -41,14 +43,14 @@ public class LicensesMetadata extends AbstractNamedDiffable imp * ever existed in the cluster state */ public static final License LICENSE_TOMBSTONE = License.builder() - .type(License.LicenseType.TRIAL) - .issuer("elasticsearch") - .uid("TOMBSTONE") - .issuedTo("") - .maxNodes(0) - .issueDate(0) - .expiryDate(0) - .build(); + .type(License.LicenseType.TRIAL) + .issuer("elasticsearch") + .uid("TOMBSTONE") + .issuedTo("") + .maxNodes(0) + .issueDate(0) + .expiryDate(0) + .build(); private License license; @@ -80,10 +82,7 @@ Version getMostRecentTrialVersion() { @Override public String toString() { - return "LicensesMetadata{" + - "license=" + license + - ", trialVersion=" + trialVersion + - '}'; + return "LicensesMetadata{" + "license=" + license + ", trialVersion=" + trialVersion + '}'; } @Override @@ -93,8 +92,7 @@ public boolean equals(Object o) { LicensesMetadata that = (LicensesMetadata) o; - return Objects.equals(license, that.license) - && Objects.equals(trialVersion, that.trialVersion); + return Objects.equals(license, that.license) && Objects.equals(trialVersion, that.trialVersion); } @Override @@ -207,8 +205,7 @@ public static License extractLicense(LicensesMetadata licensesMetadata) { public LicensesMetadata merge(LicensesMetadata other) { if (other.license == null) { return this; - } else if (license == null - || OperationMode.compare(other.license.operationMode(), license.operationMode()) > 0) { + } else if (license == null || OperationMode.compare(other.license.operationMode(), license.operationMode()) > 0) { return other; } return this; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/Licensing.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/Licensing.java index a9c1a55d130a1..2a3acd6bf022c 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/Licensing.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/Licensing.java @@ -12,17 +12,17 @@ import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.cluster.node.DiscoveryNodes; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.settings.ClusterSettings; import org.elasticsearch.common.settings.IndexScopedSettings; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsFilter; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.plugins.ActionPlugin; import org.elasticsearch.rest.RestController; import org.elasticsearch.rest.RestHandler; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import java.util.ArrayList; import java.util.Arrays; @@ -30,7 +30,6 @@ import java.util.List; import java.util.function.Supplier; - public class Licensing implements ActionPlugin { public static final String NAME = "license"; @@ -50,8 +49,9 @@ public List getNamedWriteables() { public List getNamedXContent() { List entries = new ArrayList<>(); // Metadata - entries.add(new NamedXContentRegistry.Entry(Metadata.Custom.class, new ParseField(LicensesMetadata.TYPE), - LicensesMetadata::fromXContent)); + entries.add( + new NamedXContentRegistry.Entry(Metadata.Custom.class, new ParseField(LicensesMetadata.TYPE), LicensesMetadata::fromXContent) + ); return entries; } @@ -61,20 +61,28 @@ public Licensing(Settings settings) { @Override public List> getActions() { - return Arrays.asList(new ActionHandler<>(PutLicenseAction.INSTANCE, TransportPutLicenseAction.class), - new ActionHandler<>(GetLicenseAction.INSTANCE, TransportGetLicenseAction.class), - new ActionHandler<>(DeleteLicenseAction.INSTANCE, TransportDeleteLicenseAction.class), - new ActionHandler<>(PostStartTrialAction.INSTANCE, TransportPostStartTrialAction.class), - new ActionHandler<>(GetTrialStatusAction.INSTANCE, TransportGetTrialStatusAction.class), - new ActionHandler<>(PostStartBasicAction.INSTANCE, TransportPostStartBasicAction.class), - new ActionHandler<>(GetBasicStatusAction.INSTANCE, TransportGetBasicStatusAction.class), - new ActionHandler<>(TransportGetFeatureUsageAction.TYPE, TransportGetFeatureUsageAction.class)); + return Arrays.asList( + new ActionHandler<>(PutLicenseAction.INSTANCE, TransportPutLicenseAction.class), + new ActionHandler<>(GetLicenseAction.INSTANCE, TransportGetLicenseAction.class), + new ActionHandler<>(DeleteLicenseAction.INSTANCE, TransportDeleteLicenseAction.class), + new ActionHandler<>(PostStartTrialAction.INSTANCE, TransportPostStartTrialAction.class), + new ActionHandler<>(GetTrialStatusAction.INSTANCE, TransportGetTrialStatusAction.class), + new ActionHandler<>(PostStartBasicAction.INSTANCE, TransportPostStartBasicAction.class), + new ActionHandler<>(GetBasicStatusAction.INSTANCE, TransportGetBasicStatusAction.class), + new ActionHandler<>(TransportGetFeatureUsageAction.TYPE, TransportGetFeatureUsageAction.class) + ); } @Override - public List getRestHandlers(Settings settings, RestController restController, ClusterSettings clusterSettings, - IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter, IndexNameExpressionResolver indexNameExpressionResolver, - Supplier nodesInCluster) { + public List getRestHandlers( + Settings settings, + RestController restController, + ClusterSettings clusterSettings, + IndexScopedSettings indexScopedSettings, + SettingsFilter settingsFilter, + IndexNameExpressionResolver indexNameExpressionResolver, + Supplier nodesInCluster + ) { List handlers = new ArrayList<>(); handlers.add(new RestGetLicenseAction()); handlers.add(new RestPutLicenseAction()); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/OperationModeFileWatcher.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/OperationModeFileWatcher.java index 95615420c4400..341ffb8a245f6 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/OperationModeFileWatcher.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/OperationModeFileWatcher.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.license; - import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.message.ParameterizedMessage; import org.apache.logging.log4j.util.Supplier; @@ -38,8 +37,7 @@ public final class OperationModeFileWatcher implements FileChangesListener { private final Logger logger; private final Runnable onChange; - public OperationModeFileWatcher(ResourceWatcherService resourceWatcherService, Path licenseModePath, - Logger logger, Runnable onChange) { + public OperationModeFileWatcher(ResourceWatcherService resourceWatcherService, Path licenseModePath, Logger logger, Runnable onChange) { this.resourceWatcherService = resourceWatcherService; this.licenseModePath = licenseModePath; this.logger = logger; @@ -94,15 +92,18 @@ private synchronized void onChange(Path file) { final OperationMode savedOperationMode = this.currentOperationMode; OperationMode newOperationMode = defaultOperationMode; try { - if (Files.exists(licenseModePath) - && Files.isReadable(licenseModePath)) { + if (Files.exists(licenseModePath) && Files.isReadable(licenseModePath)) { final byte[] content; try { content = Files.readAllBytes(licenseModePath); } catch (IOException e) { logger.error( - (Supplier) () -> new ParameterizedMessage( - "couldn't read operation mode from [{}]", licenseModePath.toAbsolutePath()), e); + (Supplier) () -> new ParameterizedMessage( + "couldn't read operation mode from [{}]", + licenseModePath.toAbsolutePath() + ), + e + ); return; } // this UTF-8 conversion is much pickier than java String @@ -111,8 +112,12 @@ private synchronized void onChange(Path file) { newOperationMode = OperationMode.parse(operationMode); } catch (IllegalArgumentException e) { logger.error( - (Supplier) () -> new ParameterizedMessage( - "invalid operation mode in [{}]", licenseModePath.toAbsolutePath()), e); + (Supplier) () -> new ParameterizedMessage( + "invalid operation mode in [{}]", + licenseModePath.toAbsolutePath() + ), + e + ); return; } } @@ -127,4 +132,3 @@ private synchronized void onChange(Path file) { } } } - diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/PostStartBasicResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/PostStartBasicResponse.java index 8a0ee8448433c..ef60beb7e3f0d 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/PostStartBasicResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/PostStartBasicResponse.java @@ -7,13 +7,13 @@ package org.elasticsearch.license; import org.elasticsearch.action.support.master.AcknowledgedResponse; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.StatusToXContentObject; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.protocol.xpack.common.ProtocolUtils; import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.Collections; @@ -150,9 +150,9 @@ public boolean equals(Object o) { if (super.equals(o) == false) return false; PostStartBasicResponse that = (PostStartBasicResponse) o; - return status == that.status && - ProtocolUtils.equals(acknowledgeMessages, that.acknowledgeMessages) && - Objects.equals(acknowledgeMessage, that.acknowledgeMessage); + return status == that.status + && ProtocolUtils.equals(acknowledgeMessages, that.acknowledgeMessages) + && Objects.equals(acknowledgeMessage, that.acknowledgeMessage); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/PostStartTrialResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/PostStartTrialResponse.java index b2490029f9c4a..f0fa738741462 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/PostStartTrialResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/PostStartTrialResponse.java @@ -21,12 +21,13 @@ public class PostStartTrialResponse extends ActionResponse { public enum Status { UPGRADED_TO_TRIAL(true, null, RestStatus.OK), TRIAL_ALREADY_ACTIVATED(false, "Operation failed: Trial was already activated.", RestStatus.FORBIDDEN), - NEED_ACKNOWLEDGEMENT(false,"Operation failed: Needs acknowledgement.", RestStatus.OK); + NEED_ACKNOWLEDGEMENT(false, "Operation failed: Needs acknowledgement.", RestStatus.OK); private final boolean isTrialStarted; private final String errorMessage; private final RestStatus restStatus; + Status(boolean isTrialStarted, String errorMessage, RestStatus restStatus) { this.isTrialStarted = isTrialStarted; this.errorMessage = errorMessage; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/PutLicenseRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/PutLicenseRequest.java index dc9e43f1875d2..5cf2a430df85f 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/PutLicenseRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/PutLicenseRequest.java @@ -16,7 +16,6 @@ import java.io.IOException; - public class PutLicenseRequest extends AcknowledgedRequest { private License license; @@ -28,8 +27,7 @@ public PutLicenseRequest(StreamInput in) throws IOException { acknowledge = in.readBoolean(); } - public PutLicenseRequest() { - } + public PutLicenseRequest() {} @Override public ActionRequestValidationException validate() { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/PutLicenseRequestBuilder.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/PutLicenseRequestBuilder.java index 762f008694fb3..bcc2655277c76 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/PutLicenseRequestBuilder.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/PutLicenseRequestBuilder.java @@ -9,8 +9,8 @@ import org.elasticsearch.action.support.master.AcknowledgedRequestBuilder; import org.elasticsearch.client.ElasticsearchClient; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.protocol.xpack.license.PutLicenseResponse; +import org.elasticsearch.xcontent.XContentType; /** * Register license request builder diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RemoteClusterLicenseChecker.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RemoteClusterLicenseChecker.java index fdf0017f8a9d6..420902cce4ab7 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RemoteClusterLicenseChecker.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RemoteClusterLicenseChecker.java @@ -170,7 +170,7 @@ public void onResponse(final XPackInfoResponse xPackInfoResponse) { return; } if ((licenseInfo.getStatus() == LicenseStatus.ACTIVE) == false - || predicate.test(License.OperationMode.parse(licenseInfo.getMode())) == false) { + || predicate.test(License.OperationMode.parse(licenseInfo.getMode())) == false) { listener.onResponse(LicenseCheck.failure(new RemoteClusterLicenseInfo(clusterAlias.get(), licenseInfo))); return; } @@ -199,8 +199,10 @@ public void onFailure(final Exception e) { private void remoteClusterLicense(final String clusterAlias, final ActionListener listener) { final ThreadContext threadContext = client.threadPool().getThreadContext(); - final ContextPreservingActionListener contextPreservingActionListener = - new ContextPreservingActionListener<>(threadContext.newRestorableContext(false), listener); + final ContextPreservingActionListener contextPreservingActionListener = new ContextPreservingActionListener<>( + threadContext.newRestorableContext(false), + listener + ); try (ThreadContext.StoredContext ignore = threadContext.stashContext()) { // we stash any context here since this is an internal execution and should not leak any existing context information threadContext.markAsSystemContext(); @@ -257,12 +259,12 @@ public static List remoteIndices(final Collection indices) { */ public static List remoteClusterAliases(final Set remoteClusters, final List indices) { return indices.stream() - .filter(RemoteClusterLicenseChecker::isRemoteIndex) - .map(index -> index.substring(0, index.indexOf(RemoteClusterAware.REMOTE_CLUSTER_INDEX_SEPARATOR))) - .distinct() - .flatMap(clusterExpression -> clusterNameExpressionResolver.resolveClusterNames(remoteClusters, clusterExpression).stream()) - .distinct() - .collect(Collectors.toList()); + .filter(RemoteClusterLicenseChecker::isRemoteIndex) + .map(index -> index.substring(0, index.indexOf(RemoteClusterAware.REMOTE_CLUSTER_INDEX_SEPARATOR))) + .distinct() + .flatMap(clusterExpression -> clusterNameExpressionResolver.resolveClusterNames(remoteClusters, clusterExpression).stream()) + .distinct() + .collect(Collectors.toList()); } /** @@ -273,20 +275,22 @@ public static List remoteClusterAliases(final Set remoteClusters * @return an error message representing license incompatibility */ public static String buildErrorMessage( - final String feature, - final RemoteClusterLicenseInfo remoteClusterLicenseInfo, - final Predicate predicate) { + final String feature, + final RemoteClusterLicenseInfo remoteClusterLicenseInfo, + final Predicate predicate + ) { final StringBuilder error = new StringBuilder(); if (remoteClusterLicenseInfo.licenseInfo().getStatus() != LicenseStatus.ACTIVE) { error.append(String.format(Locale.ROOT, "the license on cluster [%s] is not active", remoteClusterLicenseInfo.clusterAlias())); } else { assert predicate.test(remoteClusterLicenseInfo.licenseInfo()) == false : "license must be incompatible to build error message"; final String message = String.format( - Locale.ROOT, - "the license mode [%s] on cluster [%s] does not enable [%s]", - License.OperationMode.parse(remoteClusterLicenseInfo.licenseInfo().getMode()), - remoteClusterLicenseInfo.clusterAlias(), - feature); + Locale.ROOT, + "the license mode [%s] on cluster [%s] does not enable [%s]", + License.OperationMode.parse(remoteClusterLicenseInfo.licenseInfo().getMode()), + remoteClusterLicenseInfo.clusterAlias(), + feature + ); error.append(message); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestDeleteLicenseAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestDeleteLicenseAction.java index 12af2f1158d0e..5840a6c54a535 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestDeleteLicenseAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestDeleteLicenseAction.java @@ -25,10 +25,7 @@ public class RestDeleteLicenseAction extends BaseRestHandler { @Override public List routes() { - return List.of( - Route.builder(DELETE, "/_license") - .replaces(DELETE, "/_xpack/license", RestApiVersion.V_7).build() - ); + return List.of(Route.builder(DELETE, "/_license").replaces(DELETE, "/_xpack/license", RestApiVersion.V_7).build()); } @Override @@ -42,7 +39,8 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC deleteLicenseRequest.timeout(request.paramAsTime("timeout", deleteLicenseRequest.timeout())); deleteLicenseRequest.masterNodeTimeout(request.paramAsTime("master_timeout", deleteLicenseRequest.masterNodeTimeout())); - return channel -> client.admin().cluster().execute(DeleteLicenseAction.INSTANCE, deleteLicenseRequest, - new RestToXContentListener<>(channel)); + return channel -> client.admin() + .cluster() + .execute(DeleteLicenseAction.INSTANCE, deleteLicenseRequest, new RestToXContentListener<>(channel)); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestGetBasicStatus.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestGetBasicStatus.java index 0a9ac3ebcfd76..07edee3a462ab 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestGetBasicStatus.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestGetBasicStatus.java @@ -24,8 +24,7 @@ public class RestGetBasicStatus extends BaseRestHandler { @Override public List routes() { return List.of( - Route.builder(GET, "/_license/basic_status") - .replaces(GET, "/_xpack/license/basic_status", RestApiVersion.V_7).build() + Route.builder(GET, "/_license/basic_status").replaces(GET, "/_xpack/license/basic_status", RestApiVersion.V_7).build() ); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestGetFeatureUsageAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestGetFeatureUsageAction.java index 90ae941de0d1e..48d3026076535 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestGetFeatureUsageAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestGetFeatureUsageAction.java @@ -31,7 +31,10 @@ public List routes() { @Override protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException { - return channel -> client.execute(TransportGetFeatureUsageAction.TYPE, new GetFeatureUsageRequest(), - new RestToXContentListener<>(channel)); + return channel -> client.execute( + TransportGetFeatureUsageAction.TYPE, + new GetFeatureUsageRequest(), + new RestToXContentListener<>(channel) + ); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestGetLicenseAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestGetLicenseAction.java index ebbffef33ef94..ab8caf4021a5e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestGetLicenseAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestGetLicenseAction.java @@ -10,8 +10,6 @@ import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.common.logging.DeprecationCategory; import org.elasticsearch.common.logging.DeprecationLogger; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.protocol.xpack.license.GetLicenseRequest; import org.elasticsearch.rest.BaseRestHandler; @@ -19,6 +17,8 @@ import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.HashMap; @@ -40,10 +40,7 @@ public class RestGetLicenseAction extends BaseRestHandler { @Override public List routes() { - return List.of( - Route.builder(GET, "/_license") - .replaces(GET, "/_xpack/license", RestApiVersion.V_7).build() - ); + return List.of(Route.builder(GET, "/_license").replaces(GET, "/_xpack/license", RestApiVersion.V_7).build()); } @Override @@ -73,11 +70,13 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC // In 7.x, there was an opt-in flag to show "enterprise" licenses. In 8.0 the flag is deprecated and can only be true // TODO Remove this from 9.0 if (request.hasParam("accept_enterprise")) { - deprecationLogger.critical(DeprecationCategory.API, "get_license_accept_enterprise", - "Including [accept_enterprise] in get license requests is deprecated." + - " The parameter will be removed in the next major version"); - if (request.paramAsBoolean("accept_enterprise", true) == false - && request.getRestApiVersion().matches(onOrAfter(V_8))) { + deprecationLogger.critical( + DeprecationCategory.API, + "get_license_accept_enterprise", + "Including [accept_enterprise] in get license requests is deprecated." + + " The parameter will be removed in the next major version" + ); + if (request.paramAsBoolean("accept_enterprise", true) == false && request.getRestApiVersion().matches(onOrAfter(V_8))) { throw new IllegalArgumentException("The [accept_enterprise] parameters may not be false"); } } @@ -85,25 +84,26 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC final ToXContent.Params params = new ToXContent.DelegatingMapParams(overrideParams, request); GetLicenseRequest getLicenseRequest = new GetLicenseRequest(); getLicenseRequest.local(request.paramAsBoolean("local", getLicenseRequest.local())); - return channel -> client.admin().cluster().execute(GetLicenseAction.INSTANCE, getLicenseRequest, - new RestBuilderListener<>(channel) { - @Override - public RestResponse buildResponse(GetLicenseResponse response, XContentBuilder builder) throws Exception { - // Default to pretty printing, but allow ?pretty=false to disable - if (request.hasParam("pretty") == false) { - builder.prettyPrint().lfAtEnd(); - } - boolean hasLicense = response.license() != null; - builder.startObject(); - if (hasLicense) { - builder.startObject("license"); - response.license().toInnerXContent(builder, params); - builder.endObject(); - } + return channel -> client.admin() + .cluster() + .execute(GetLicenseAction.INSTANCE, getLicenseRequest, new RestBuilderListener<>(channel) { + @Override + public RestResponse buildResponse(GetLicenseResponse response, XContentBuilder builder) throws Exception { + // Default to pretty printing, but allow ?pretty=false to disable + if (request.hasParam("pretty") == false) { + builder.prettyPrint().lfAtEnd(); + } + boolean hasLicense = response.license() != null; + builder.startObject(); + if (hasLicense) { + builder.startObject("license"); + response.license().toInnerXContent(builder, params); builder.endObject(); - return new BytesRestResponse(hasLicense ? OK : NOT_FOUND, builder); } - }); + builder.endObject(); + return new BytesRestResponse(hasLicense ? OK : NOT_FOUND, builder); + } + }); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestGetTrialStatus.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestGetTrialStatus.java index 38a19cb370925..52717e152769e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestGetTrialStatus.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestGetTrialStatus.java @@ -24,8 +24,7 @@ public class RestGetTrialStatus extends BaseRestHandler { @Override public List routes() { return List.of( - Route.builder(GET, "/_license/trial_status") - .replaces(GET, "/_xpack/license/trial_status", RestApiVersion.V_7).build() + Route.builder(GET, "/_license/trial_status").replaces(GET, "/_xpack/license/trial_status", RestApiVersion.V_7).build() ); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestPostStartBasicLicense.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestPostStartBasicLicense.java index 01813b04e2f9f..f62eaeac38da1 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestPostStartBasicLicense.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestPostStartBasicLicense.java @@ -25,8 +25,7 @@ public class RestPostStartBasicLicense extends BaseRestHandler { @Override public List routes() { return List.of( - Route.builder(POST, "/_license/start_basic") - .replaces(POST, "/_xpack/license/start_basic", RestApiVersion.V_7).build() + Route.builder(POST, "/_license/start_basic").replaces(POST, "/_xpack/license/start_basic", RestApiVersion.V_7).build() ); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestPostStartTrialLicense.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestPostStartTrialLicense.java index 12814fed494f6..b2792106c95b9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestPostStartTrialLicense.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestPostStartTrialLicense.java @@ -8,13 +8,13 @@ package org.elasticsearch.license; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.List; @@ -29,8 +29,7 @@ public class RestPostStartTrialLicense extends BaseRestHandler { @Override public List routes() { return List.of( - Route.builder(POST, "/_license/start_trial") - .replaces(POST, "/_xpack/license/start_trial", RestApiVersion.V_7).build() + Route.builder(POST, "/_license/start_trial").replaces(POST, "/_xpack/license/start_trial", RestApiVersion.V_7).build() ); } @@ -39,38 +38,37 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli PostStartTrialRequest startTrialRequest = new PostStartTrialRequest(); startTrialRequest.setType(request.param("type", License.LicenseType.TRIAL.getTypeName())); startTrialRequest.acknowledge(request.paramAsBoolean("acknowledge", false)); - return channel -> client.execute(PostStartTrialAction.INSTANCE, startTrialRequest, - new RestBuilderListener<>(channel) { - @Override - public RestResponse buildResponse(PostStartTrialResponse response, XContentBuilder builder) throws Exception { - PostStartTrialResponse.Status status = response.getStatus(); - builder.startObject(); - builder.field("acknowledged", startTrialRequest.isAcknowledged()); - if (status.isTrialStarted()) { - builder.field("trial_was_started", true); - builder.field("type", startTrialRequest.getType()); - } else { - builder.field("trial_was_started", false); - builder.field("error_message", status.getErrorMessage()); - } + return channel -> client.execute(PostStartTrialAction.INSTANCE, startTrialRequest, new RestBuilderListener<>(channel) { + @Override + public RestResponse buildResponse(PostStartTrialResponse response, XContentBuilder builder) throws Exception { + PostStartTrialResponse.Status status = response.getStatus(); + builder.startObject(); + builder.field("acknowledged", startTrialRequest.isAcknowledged()); + if (status.isTrialStarted()) { + builder.field("trial_was_started", true); + builder.field("type", startTrialRequest.getType()); + } else { + builder.field("trial_was_started", false); + builder.field("error_message", status.getErrorMessage()); + } - Map acknowledgementMessages = response.getAcknowledgementMessages(); - if (acknowledgementMessages.isEmpty() == false) { - builder.startObject("acknowledge"); - builder.field("message", response.getAcknowledgementMessage()); - for (Map.Entry entry : acknowledgementMessages.entrySet()) { - builder.startArray(entry.getKey()); - for (String message : entry.getValue()) { - builder.value(message); - } - builder.endArray(); - } - builder.endObject(); + Map acknowledgementMessages = response.getAcknowledgementMessages(); + if (acknowledgementMessages.isEmpty() == false) { + builder.startObject("acknowledge"); + builder.field("message", response.getAcknowledgementMessage()); + for (Map.Entry entry : acknowledgementMessages.entrySet()) { + builder.startArray(entry.getKey()); + for (String message : entry.getValue()) { + builder.value(message); } - builder.endObject(); - return new BytesRestResponse(status.getRestStatus(), builder); + builder.endArray(); } - }); + builder.endObject(); + } + builder.endObject(); + return new BytesRestResponse(status.getRestStatus(), builder); + } + }); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestPutLicenseAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestPutLicenseAction.java index 7865c330cc6ea..30455e30abbd3 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestPutLicenseAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestPutLicenseAction.java @@ -27,10 +27,8 @@ public class RestPutLicenseAction extends BaseRestHandler { public List routes() { // TODO: remove POST endpoint? return List.of( - Route.builder(POST, "/_license") - .replaces(POST, "/_xpack/license", RestApiVersion.V_7).build(), - Route.builder(PUT, "/_license") - .replaces(PUT, "/_xpack/license", RestApiVersion.V_7).build() + Route.builder(POST, "/_license").replaces(POST, "/_xpack/license", RestApiVersion.V_7).build(), + Route.builder(PUT, "/_license").replaces(PUT, "/_xpack/license", RestApiVersion.V_7).build() ); } @@ -51,8 +49,10 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC putLicenseRequest.masterNodeTimeout(request.paramAsTime("master_timeout", putLicenseRequest.masterNodeTimeout())); if (License.LicenseType.isBasic(putLicenseRequest.license().type())) { - throw new IllegalArgumentException("Installing basic licenses is no longer allowed. Use the POST " + - "/_license/start_basic API to install a basic license that does not expire."); + throw new IllegalArgumentException( + "Installing basic licenses is no longer allowed. Use the POST " + + "/_license/start_basic API to install a basic license that does not expire." + ); } return channel -> client.execute(PutLicenseAction.INSTANCE, putLicenseRequest, new RestToXContentListener<>(channel)); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/SelfGeneratedLicense.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/SelfGeneratedLicense.java index 88412d1804d75..0ec54c690d5fe 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/SelfGeneratedLicense.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/SelfGeneratedLicense.java @@ -33,10 +33,7 @@ public static License create(License.Builder specBuilder, DiscoveryNodes current } public static License create(License.Builder specBuilder, int version) { - License spec = specBuilder - .issuer("elasticsearch") - .version(version) - .build(); + License spec = specBuilder.issuer("elasticsearch").version(version).build(); final String signature; try { XContentBuilder contentBuilder = XContentFactory.contentBuilder(XContentType.JSON); @@ -50,9 +47,7 @@ public static License create(License.Builder specBuilder, int version) { byte[] bytes = new byte[4 + 4 + encrypt.length]; ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); // Set -version in signature - byteBuffer.putInt(-version) - .putInt(encrypt.length) - .put(encrypt); + byteBuffer.putInt(-version).putInt(encrypt.length).put(encrypt); signature = Base64.getEncoder().encodeToString(bytes); } catch (IOException e) { throw new IllegalStateException(e); @@ -72,11 +67,15 @@ public static boolean verify(final License license) { // Version in signature is -version, so check for -(-version) < 4 byte[] decryptedContent = (-version < License.VERSION_CRYPTO_ALGORITHMS) ? decryptV3Format(content) : decrypt(content); // EMPTY is safe here because we don't call namedObject - try (XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, decryptedContent)) { + try ( + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, decryptedContent) + ) { parser.nextToken(); - expectedLicense = License.builder().fromLicenseSpec(License.fromXContent(parser), - license.signature()).version(-version).build(); + expectedLicense = License.builder() + .fromLicenseSpec(License.fromXContent(parser), license.signature()) + .version(-version) + .build(); } return license.equals(expectedLicense); } catch (IOException e) { @@ -90,7 +89,14 @@ static License.LicenseType validateSelfGeneratedType(License.LicenseType type) { case TRIAL: return type; } - throw new IllegalArgumentException("invalid self generated license type [" + type + "], only " + - License.LicenseType.BASIC + " and " + License.LicenseType.TRIAL + " are accepted"); + throw new IllegalArgumentException( + "invalid self generated license type [" + + type + + "], only " + + License.LicenseType.BASIC + + " and " + + License.LicenseType.TRIAL + + " are accepted" + ); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/StartBasicClusterTask.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/StartBasicClusterTask.java index 564c2b7f5b6a2..cac19a6560e45 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/StartBasicClusterTask.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/StartBasicClusterTask.java @@ -24,8 +24,8 @@ public class StartBasicClusterTask extends ClusterStateUpdateTask { - private static final String ACKNOWLEDGEMENT_HEADER = "This license update requires acknowledgement. To acknowledge the license, " + - "please read the following messages and call /start_basic again, this time with the \"acknowledge=true\" parameter:"; + private static final String ACKNOWLEDGEMENT_HEADER = "This license update requires acknowledgement. To acknowledge the license, " + + "please read the following messages and call /start_basic again, this time with the \"acknowledge=true\" parameter:"; private final Logger logger; private final String clusterName; @@ -34,8 +34,13 @@ public class StartBasicClusterTask extends ClusterStateUpdateTask { private final Clock clock; private AtomicReference> ackMessages = new AtomicReference<>(Collections.emptyMap()); - StartBasicClusterTask(Logger logger, String clusterName, Clock clock, PostStartBasicRequest request, - ActionListener listener) { + StartBasicClusterTask( + Logger logger, + String clusterName, + Clock clock, + PostStartBasicRequest request, + ActionListener listener + ) { this.logger = logger; this.clusterName = clusterName; this.request = request; @@ -50,11 +55,12 @@ public void clusterStateProcessed(String source, ClusterState oldState, ClusterS License oldLicense = LicensesMetadata.extractLicense(oldLicensesMetadata); Map acknowledgeMessages = ackMessages.get(); if (acknowledgeMessages.isEmpty() == false) { - listener.onResponse(new PostStartBasicResponse(PostStartBasicResponse.Status.NEED_ACKNOWLEDGEMENT, acknowledgeMessages, - ACKNOWLEDGEMENT_HEADER)); + listener.onResponse( + new PostStartBasicResponse(PostStartBasicResponse.Status.NEED_ACKNOWLEDGEMENT, acknowledgeMessages, ACKNOWLEDGEMENT_HEADER) + ); } else if (oldLicense != null && License.LicenseType.isBasic(oldLicense.type())) { listener.onResponse(new PostStartBasicResponse(PostStartBasicResponse.Status.ALREADY_USING_BASIC)); - } else { + } else { listener.onResponse(new PostStartBasicResponse(PostStartBasicResponse.Status.GENERATED_BASIC)); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/StartTrialClusterTask.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/StartTrialClusterTask.java index afffe846174eb..798a5a16a1957 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/StartTrialClusterTask.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/StartTrialClusterTask.java @@ -23,13 +23,15 @@ public class StartTrialClusterTask extends ClusterStateUpdateTask { - private static final String ACKNOWLEDGEMENT_HEADER = "This API initiates a free 30-day trial for all platinum features. " + - "By starting this trial, you agree that it is subject to the terms and conditions at" + - " https://www.elastic.co/legal/trial_license/. To begin your free trial, call /start_trial again and specify " + - "the \"acknowledge=true\" parameter."; + private static final String ACKNOWLEDGEMENT_HEADER = "This API initiates a free 30-day trial for all platinum features. " + + "By starting this trial, you agree that it is subject to the terms and conditions at" + + " https://www.elastic.co/legal/trial_license/. To begin your free trial, call /start_trial again and specify " + + "the \"acknowledge=true\" parameter."; - private static final Map ACK_MESSAGES = Collections.singletonMap("security", - new String[] {"With a trial license, X-Pack security features are available, but are not enabled by default."}); + private static final Map ACK_MESSAGES = Collections.singletonMap( + "security", + new String[] { "With a trial license, X-Pack security features are available, but are not enabled by default." } + ); private final Logger logger; private final String clusterName; @@ -37,8 +39,13 @@ public class StartTrialClusterTask extends ClusterStateUpdateTask { private final ActionListener listener; private final Clock clock; - StartTrialClusterTask(Logger logger, String clusterName, Clock clock, PostStartTrialRequest request, - ActionListener listener) { + StartTrialClusterTask( + Logger logger, + String clusterName, + Clock clock, + PostStartTrialRequest request, + ActionListener listener + ) { this.logger = logger; this.clusterName = clusterName; this.request = request; @@ -52,8 +59,9 @@ public void clusterStateProcessed(String source, ClusterState oldState, ClusterS logger.debug("started self generated trial license: {}", oldLicensesMetadata); if (request.isAcknowledged() == false) { - listener.onResponse(new PostStartTrialResponse(PostStartTrialResponse.Status.NEED_ACKNOWLEDGEMENT, - ACK_MESSAGES, ACKNOWLEDGEMENT_HEADER)); + listener.onResponse( + new PostStartTrialResponse(PostStartTrialResponse.Status.NEED_ACKNOWLEDGEMENT, ACK_MESSAGES, ACKNOWLEDGEMENT_HEADER) + ); } else if (oldLicensesMetadata == null || oldLicensesMetadata.isEligibleForTrial()) { listener.onResponse(new PostStartTrialResponse(PostStartTrialResponse.Status.UPGRADED_TO_TRIAL)); } else { @@ -74,11 +82,11 @@ public ClusterState execute(ClusterState currentState) throws Exception { long expiryDate = issueDate + LicenseService.NON_BASIC_SELF_GENERATED_LICENSE_DURATION.getMillis(); License.Builder specBuilder = License.builder() - .uid(UUID.randomUUID().toString()) - .issuedTo(clusterName) - .issueDate(issueDate) - .type(request.getType()) - .expiryDate(expiryDate); + .uid(UUID.randomUUID().toString()) + .issuedTo(clusterName) + .issueDate(issueDate) + .type(request.getType()) + .expiryDate(expiryDate); if (License.LicenseType.isEnterprise(request.getType())) { specBuilder.maxResourceUnits(LicenseService.SELF_GENERATED_LICENSE_MAX_RESOURCE_UNITS); } else { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/StartupSelfGeneratedLicenseTask.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/StartupSelfGeneratedLicenseTask.java index 9790c04d9c73b..8963f420cf033 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/StartupSelfGeneratedLicenseTask.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/StartupSelfGeneratedLicenseTask.java @@ -15,8 +15,8 @@ import org.elasticsearch.cluster.ClusterStateUpdateTask; import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xpack.core.XPackPlugin; import java.time.Clock; @@ -56,7 +56,8 @@ public ClusterState execute(ClusterState currentState) throws Exception { // do not generate a license if any license is present if (currentLicensesMetadata == null) { License.LicenseType type = SelfGeneratedLicense.validateSelfGeneratedType( - LicenseService.SELF_GENERATED_LICENSE_TYPE.get(settings)); + LicenseService.SELF_GENERATED_LICENSE_TYPE.get(settings) + ); return updateWithLicense(currentState, type); } else if (LicenseUtils.signatureNeedsUpdate(currentLicensesMetadata.getLicense(), currentState.nodes())) { return updateLicenseSignature(currentState, currentLicensesMetadata); @@ -74,22 +75,25 @@ private ClusterState updateLicenseSignature(ClusterState currentState, LicensesM long issueDate = license.issueDate(); long expiryDate = license.expiryDate(); // extend the basic license expiration date if needed since extendBasic will not be called now - if (License.LicenseType.isBasic(type) && expiryDate != LicenseService.BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS) { + if (License.LicenseType.isBasic(type) && expiryDate != LicenseService.BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS) { expiryDate = LicenseService.BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS; } License.Builder specBuilder = License.builder() - .uid(license.uid()) - .issuedTo(license.issuedTo()) - .maxNodes(selfGeneratedLicenseMaxNodes) - .issueDate(issueDate) - .type(type) - .expiryDate(expiryDate); + .uid(license.uid()) + .issuedTo(license.issuedTo()) + .maxNodes(selfGeneratedLicenseMaxNodes) + .issueDate(issueDate) + .type(type) + .expiryDate(expiryDate); License selfGeneratedLicense = SelfGeneratedLicense.create(specBuilder, currentState.nodes()); Version trialVersion = currentLicenseMetadata.getMostRecentTrialVersion(); LicensesMetadata newLicenseMetadata = new LicensesMetadata(selfGeneratedLicense, trialVersion); mdBuilder.putCustom(LicensesMetadata.TYPE, newLicenseMetadata); - logger.info("Updating existing license to the new version.\n\nOld license:\n {}\n\n New license:\n{}", - license, newLicenseMetadata.getLicense()); + logger.info( + "Updating existing license to the new version.\n\nOld license:\n {}\n\n New license:\n{}", + license, + newLicenseMetadata.getLicense() + ); return ClusterState.builder(currentState).metadata(mdBuilder).build(); } @@ -103,20 +107,24 @@ private ClusterState extendBasic(ClusterState currentState, LicensesMetadata cur Metadata.Builder mdBuilder = Metadata.builder(currentState.metadata()); LicensesMetadata newLicenseMetadata = createBasicLicenseFromExistingLicense(currentLicenseMetadata); mdBuilder.putCustom(LicensesMetadata.TYPE, newLicenseMetadata); - logger.info("Existing basic license has an expiration. Basic licenses no longer expire." + - "Regenerating license.\n\nOld license:\n {}\n\n New license:\n{}", license, newLicenseMetadata.getLicense()); + logger.info( + "Existing basic license has an expiration. Basic licenses no longer expire." + + "Regenerating license.\n\nOld license:\n {}\n\n New license:\n{}", + license, + newLicenseMetadata.getLicense() + ); return ClusterState.builder(currentState).metadata(mdBuilder).build(); } private LicensesMetadata createBasicLicenseFromExistingLicense(LicensesMetadata currentLicenseMetadata) { License currentLicense = currentLicenseMetadata.getLicense(); License.Builder specBuilder = License.builder() - .uid(currentLicense.uid()) - .issuedTo(currentLicense.issuedTo()) - .maxNodes(selfGeneratedLicenseMaxNodes) - .issueDate(currentLicense.issueDate()) - .type(License.LicenseType.BASIC) - .expiryDate(LicenseService.BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS); + .uid(currentLicense.uid()) + .issuedTo(currentLicense.issuedTo()) + .maxNodes(selfGeneratedLicenseMaxNodes) + .issueDate(currentLicense.issueDate()) + .type(License.LicenseType.BASIC) + .expiryDate(LicenseService.BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS); License selfGeneratedLicense = SelfGeneratedLicense.create(specBuilder, currentLicense.version()); Version trialVersion = currentLicenseMetadata.getMostRecentTrialVersion(); return new LicensesMetadata(selfGeneratedLicense, trialVersion); @@ -132,12 +140,12 @@ private ClusterState updateWithLicense(ClusterState currentState, License.Licens expiryDate = issueDate + LicenseService.NON_BASIC_SELF_GENERATED_LICENSE_DURATION.getMillis(); } License.Builder specBuilder = License.builder() - .uid(UUID.randomUUID().toString()) - .issuedTo(clusterService.getClusterName().value()) - .maxNodes(selfGeneratedLicenseMaxNodes) - .issueDate(issueDate) - .type(type) - .expiryDate(expiryDate); + .uid(UUID.randomUUID().toString()) + .issuedTo(clusterService.getClusterName().value()) + .maxNodes(selfGeneratedLicenseMaxNodes) + .issueDate(issueDate) + .type(type) + .expiryDate(expiryDate); License selfGeneratedLicense = SelfGeneratedLicense.create(specBuilder, currentState.nodes()); LicensesMetadata licensesMetadata; if (License.LicenseType.TRIAL.equals(type)) { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/TransportDeleteLicenseAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/TransportDeleteLicenseAction.java index 7456a8604ad05..ec1c075f888d9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/TransportDeleteLicenseAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/TransportDeleteLicenseAction.java @@ -27,11 +27,24 @@ public class TransportDeleteLicenseAction extends AcknowledgedTransportMasterNod private final LicenseService licenseService; @Inject - public TransportDeleteLicenseAction(TransportService transportService, ClusterService clusterService, - LicenseService licenseService, ThreadPool threadPool, ActionFilters actionFilters, - IndexNameExpressionResolver indexNameExpressionResolver) { - super(DeleteLicenseAction.NAME, transportService, clusterService, threadPool, actionFilters, - DeleteLicenseRequest::new, indexNameExpressionResolver, ThreadPool.Names.MANAGEMENT); + public TransportDeleteLicenseAction( + TransportService transportService, + ClusterService clusterService, + LicenseService licenseService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver + ) { + super( + DeleteLicenseAction.NAME, + transportService, + clusterService, + threadPool, + actionFilters, + DeleteLicenseRequest::new, + indexNameExpressionResolver, + ThreadPool.Names.MANAGEMENT + ); this.licenseService = licenseService; } @@ -41,9 +54,17 @@ protected ClusterBlockException checkBlock(DeleteLicenseRequest request, Cluster } @Override - protected void masterOperation(Task task, final DeleteLicenseRequest request, ClusterState state, - final ActionListener listener) throws ElasticsearchException { - licenseService.removeLicense(request, listener.delegateFailure((l, postStartBasicResponse) -> - l.onResponse(AcknowledgedResponse.of(postStartBasicResponse.isAcknowledged())))); + protected void masterOperation( + Task task, + final DeleteLicenseRequest request, + ClusterState state, + final ActionListener listener + ) throws ElasticsearchException { + licenseService.removeLicense( + request, + listener.delegateFailure( + (l, postStartBasicResponse) -> l.onResponse(AcknowledgedResponse.of(postStartBasicResponse.isAcknowledged())) + ) + ); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/TransportGetBasicStatusAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/TransportGetBasicStatusAction.java index b74e7648925f4..4ceb3d59a49b2 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/TransportGetBasicStatusAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/TransportGetBasicStatusAction.java @@ -22,16 +22,33 @@ public class TransportGetBasicStatusAction extends TransportMasterNodeReadAction { @Inject - public TransportGetBasicStatusAction(TransportService transportService, ClusterService clusterService, - ThreadPool threadPool, ActionFilters actionFilters, - IndexNameExpressionResolver indexNameExpressionResolver) { - super(GetBasicStatusAction.NAME, transportService, clusterService, threadPool, actionFilters, - GetBasicStatusRequest::new, indexNameExpressionResolver, GetBasicStatusResponse::new, ThreadPool.Names.SAME); + public TransportGetBasicStatusAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver + ) { + super( + GetBasicStatusAction.NAME, + transportService, + clusterService, + threadPool, + actionFilters, + GetBasicStatusRequest::new, + indexNameExpressionResolver, + GetBasicStatusResponse::new, + ThreadPool.Names.SAME + ); } @Override - protected void masterOperation(Task task, GetBasicStatusRequest request, ClusterState state, - ActionListener listener) throws Exception { + protected void masterOperation( + Task task, + GetBasicStatusRequest request, + ClusterState state, + ActionListener listener + ) throws Exception { LicensesMetadata licensesMetadata = state.metadata().custom(LicensesMetadata.TYPE); if (licensesMetadata == null) { listener.onResponse(new GetBasicStatusResponse(true)); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/TransportGetFeatureUsageAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/TransportGetFeatureUsageAction.java index 20c67447fa3db..d57269b131e2a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/TransportGetFeatureUsageAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/TransportGetFeatureUsageAction.java @@ -24,19 +24,19 @@ public class TransportGetFeatureUsageAction extends HandledTransportAction { - public static final ActionType TYPE = - new ActionType<>("cluster:admin/xpack/license/feature_usage", GetFeatureUsageResponse::new); + public static final ActionType TYPE = new ActionType<>( + "cluster:admin/xpack/license/feature_usage", + GetFeatureUsageResponse::new + ); private final XPackLicenseState licenseState; @Inject - public TransportGetFeatureUsageAction(TransportService transportService, ActionFilters actionFilters, - XPackLicenseState licenseState) { + public TransportGetFeatureUsageAction(TransportService transportService, ActionFilters actionFilters, XPackLicenseState licenseState) { super(TYPE.name(), transportService, actionFilters, GetFeatureUsageRequest::new); this.licenseState = licenseState; } - @Override protected void doExecute(Task task, GetFeatureUsageRequest request, ActionListener listener) { Map featureUsage = licenseState.getLastUsed(); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/TransportGetLicenseAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/TransportGetLicenseAction.java index 2e7146b217746..75ecf85968283 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/TransportGetLicenseAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/TransportGetLicenseAction.java @@ -26,11 +26,25 @@ public class TransportGetLicenseAction extends TransportMasterNodeReadAction listener) throws ElasticsearchException { + protected void masterOperation( + Task task, + final GetLicenseRequest request, + ClusterState state, + final ActionListener listener + ) throws ElasticsearchException { listener.onResponse(new GetLicenseResponse(licenseService.getLicense())); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/TransportGetTrialStatusAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/TransportGetTrialStatusAction.java index b4ea97ed42e44..86f9744cb57a8 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/TransportGetTrialStatusAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/TransportGetTrialStatusAction.java @@ -22,15 +22,33 @@ public class TransportGetTrialStatusAction extends TransportMasterNodeReadAction { @Inject - public TransportGetTrialStatusAction(TransportService transportService, ClusterService clusterService, ThreadPool threadPool, - ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver) { - super(GetTrialStatusAction.NAME, transportService, clusterService, threadPool, actionFilters, - GetTrialStatusRequest::new, indexNameExpressionResolver, GetTrialStatusResponse::new, ThreadPool.Names.SAME); + public TransportGetTrialStatusAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver + ) { + super( + GetTrialStatusAction.NAME, + transportService, + clusterService, + threadPool, + actionFilters, + GetTrialStatusRequest::new, + indexNameExpressionResolver, + GetTrialStatusResponse::new, + ThreadPool.Names.SAME + ); } @Override - protected void masterOperation(Task task, GetTrialStatusRequest request, ClusterState state, - ActionListener listener) throws Exception { + protected void masterOperation( + Task task, + GetTrialStatusRequest request, + ClusterState state, + ActionListener listener + ) throws Exception { LicensesMetadata licensesMetadata = state.metadata().custom(LicensesMetadata.TYPE); listener.onResponse(new GetTrialStatusResponse(licensesMetadata == null || licensesMetadata.isEligibleForTrial())); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/TransportPostStartBasicAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/TransportPostStartBasicAction.java index d16b54ac28f81..a58281dc1420c 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/TransportPostStartBasicAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/TransportPostStartBasicAction.java @@ -24,17 +24,35 @@ public class TransportPostStartBasicAction extends TransportMasterNodeAction listener) throws Exception { + protected void masterOperation( + Task task, + PostStartBasicRequest request, + ClusterState state, + ActionListener listener + ) throws Exception { licenseService.startBasicLicense(request, listener); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/TransportPostStartTrialAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/TransportPostStartTrialAction.java index cbcdbb04852d1..820f31f512712 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/TransportPostStartTrialAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/TransportPostStartTrialAction.java @@ -24,17 +24,35 @@ public class TransportPostStartTrialAction extends TransportMasterNodeAction listener) throws Exception { + protected void masterOperation( + Task task, + PostStartTrialRequest request, + ClusterState state, + ActionListener listener + ) throws Exception { licenseService.startTrialLicense(request, listener); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/TransportPutLicenseAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/TransportPutLicenseAction.java index 86ac82ff989b6..1188cdc091bf2 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/TransportPutLicenseAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/TransportPutLicenseAction.java @@ -26,11 +26,25 @@ public class TransportPutLicenseAction extends TransportMasterNodeAction - listener) throws ElasticsearchException { + protected void masterOperation( + Task task, + final PutLicenseRequest request, + ClusterState state, + final ActionListener listener + ) throws ElasticsearchException { licenseService.registerLicense(request, listener); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/XPackLicenseState.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/XPackLicenseState.java index 0af7e8dd1d359..dff8fccb0130e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/XPackLicenseState.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/XPackLicenseState.java @@ -37,57 +37,49 @@ public class XPackLicenseState { static final Map EXPIRATION_MESSAGES; static { Map messages = new LinkedHashMap<>(); - messages.put(XPackField.SECURITY, new String[] { - "Cluster health, cluster stats and indices stats operations are blocked", - "All data operations (read and write) continue to work" - }); - messages.put(XPackField.WATCHER, new String[] { - "PUT / GET watch APIs are disabled, DELETE watch API continues to work", - "Watches execute and write to the history", - "The actions of the watches don't execute" - }); - messages.put(XPackField.MONITORING, new String[] { - "The agent will stop collecting cluster and indices metrics" - }); - messages.put(XPackField.GRAPH, new String[] { - "Graph explore APIs are disabled" - }); - messages.put(XPackField.MACHINE_LEARNING, new String[] { - "Machine learning APIs are disabled" - }); - messages.put(XPackField.LOGSTASH, new String[] { - "Logstash will continue to poll centrally-managed pipelines" - }); - messages.put(XPackField.BEATS, new String[] { - "Beats will continue to poll centrally-managed configuration" - }); - messages.put(XPackField.DEPRECATION, new String[] { - "Deprecation APIs are disabled" - }); - messages.put(XPackField.UPGRADE, new String[] { - "Upgrade API is disabled" - }); - messages.put(XPackField.SQL, new String[] { - "SQL support is disabled" - }); - messages.put(XPackField.ROLLUP, new String[] { - "Creating and Starting rollup jobs will no longer be allowed.", - "Stopping/Deleting existing jobs, RollupCaps API and RollupSearch continue to function." - }); - messages.put(XPackField.TRANSFORM, new String[] { - "Creating, starting, updating transforms will no longer be allowed.", - "Stopping/Deleting existing transforms continue to function." - }); - messages.put(XPackField.ANALYTICS, new String[] { - "Aggregations provided by Analytics plugin are no longer usable." - }); - messages.put(XPackField.CCR, new String[]{ - "Creating new follower indices will be blocked", - "Configuring auto-follow patterns will be blocked", - "Auto-follow patterns will no longer discover new leader indices", - "The CCR monitoring endpoint will be blocked", - "Existing follower indices will continue to replicate data" - }); + messages.put( + XPackField.SECURITY, + new String[] { + "Cluster health, cluster stats and indices stats operations are blocked", + "All data operations (read and write) continue to work" } + ); + messages.put( + XPackField.WATCHER, + new String[] { + "PUT / GET watch APIs are disabled, DELETE watch API continues to work", + "Watches execute and write to the history", + "The actions of the watches don't execute" } + ); + messages.put(XPackField.MONITORING, new String[] { "The agent will stop collecting cluster and indices metrics" }); + messages.put(XPackField.GRAPH, new String[] { "Graph explore APIs are disabled" }); + messages.put(XPackField.MACHINE_LEARNING, new String[] { "Machine learning APIs are disabled" }); + messages.put(XPackField.LOGSTASH, new String[] { "Logstash will continue to poll centrally-managed pipelines" }); + messages.put(XPackField.BEATS, new String[] { "Beats will continue to poll centrally-managed configuration" }); + messages.put(XPackField.DEPRECATION, new String[] { "Deprecation APIs are disabled" }); + messages.put(XPackField.UPGRADE, new String[] { "Upgrade API is disabled" }); + messages.put(XPackField.SQL, new String[] { "SQL support is disabled" }); + messages.put( + XPackField.ROLLUP, + new String[] { + "Creating and Starting rollup jobs will no longer be allowed.", + "Stopping/Deleting existing jobs, RollupCaps API and RollupSearch continue to function." } + ); + messages.put( + XPackField.TRANSFORM, + new String[] { + "Creating, starting, updating transforms will no longer be allowed.", + "Stopping/Deleting existing transforms continue to function." } + ); + messages.put(XPackField.ANALYTICS, new String[] { "Aggregations provided by Analytics plugin are no longer usable." }); + messages.put( + XPackField.CCR, + new String[] { + "Creating new follower indices will be blocked", + "Configuring auto-follow patterns will be blocked", + "Auto-follow patterns will no longer discover new leader indices", + "The CCR monitoring endpoint will be blocked", + "Existing follower indices will continue to replicate data" } + ); EXPIRATION_MESSAGES = Collections.unmodifiableMap(messages); } @@ -126,8 +118,7 @@ private static String[] securityAcknowledgementMessages(OperationMode currentMod "IP filtering and auditing will be disabled.", "Field and document level access control will be disabled.", "Custom realms will be ignored.", - "A custom authorization engine will be ignored." - }; + "A custom authorization engine will be ignored." }; } break; case GOLD: @@ -141,8 +132,7 @@ private static String[] securityAcknowledgementMessages(OperationMode currentMod return new String[] { "Field and document level access control will be disabled.", "Custom realms will be ignored.", - "A custom authorization engine will be ignored." - }; + "A custom authorization engine will be ignored." }; } break; case STANDARD: @@ -158,8 +148,7 @@ private static String[] securityAcknowledgementMessages(OperationMode currentMod "IP filtering and auditing will be disabled.", "Field and document level access control will be disabled.", "Custom realms will be ignored.", - "A custom authorization engine will be ignored." - }; + "A custom authorization engine will be ignored." }; } } return Strings.EMPTY_ARRAY; @@ -192,12 +181,14 @@ private static String[] monitoringAcknowledgementMessages(OperationMode currentM case ENTERPRISE: return new String[] { LoggerMessageFormat.format( - "Multi-cluster support is disabled for clusters with [{}] license. If you are\n" + - "running multiple clusters, users won't be able to access the clusters with\n" + - "[{}] licenses from within a single X-Pack Kibana instance. You will have to deploy a\n" + - "separate and dedicated X-pack Kibana instance for each [{}] cluster you wish to monitor.", - newMode, newMode, newMode) - }; + "Multi-cluster support is disabled for clusters with [{}] license. If you are\n" + + "running multiple clusters, users won't be able to access the clusters with\n" + + "[{}] licenses from within a single X-Pack Kibana instance. You will have to deploy a\n" + + "separate and dedicated X-pack Kibana instance for each [{}] cluster you wish to monitor.", + newMode, + newMode, + newMode + ) }; } break; } @@ -268,7 +259,7 @@ private static String[] sqlAcknowledgementMessages(OperationMode currentMode, Op case PLATINUM: case ENTERPRISE: return new String[] { - "JDBC and ODBC support will be disabled, but you can continue to use SQL CLI and REST endpoint" }; + "JDBC and ODBC support will be disabled, but you can continue to use SQL CLI and REST endpoint" }; } break; } @@ -288,9 +279,7 @@ private static String[] ccrAcknowledgementMessages(final OperationMode current, case STANDARD: case GOLD: // so CCR will be disabled - return new String[]{ - "Cross-Cluster Replication will be disabled" - }; + return new String[] { "Cross-Cluster Replication will be disabled" }; } } return Strings.EMPTY_ARRAY; @@ -463,8 +452,7 @@ public static boolean isCcrAllowedForOperationMode(final OperationMode operation return isAllowedByOperationMode(operationMode, OperationMode.PLATINUM); } - public static boolean isAllowedByOperationMode( - final OperationMode operationMode, final OperationMode minimumMode) { + public static boolean isAllowedByOperationMode(final OperationMode operationMode, final OperationMode minimumMode) { if (OperationMode.TRIAL == operationMode) { return true; } @@ -479,8 +467,7 @@ public static boolean isAllowedByOperationMode( * is needed for multiple interactions with the license state. */ public XPackLicenseState copyCurrentLicenseState() { - return executeAgainstStatus(status -> - new XPackLicenseState(listeners, status, usage, epochMillisProvider)); + return executeAgainstStatus(status -> new XPackLicenseState(listeners, status, usage, epochMillisProvider)); } /** diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/XPackInfoRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/XPackInfoRequest.java index 9efc29645bdaf..9889a88c91382 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/XPackInfoRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/XPackInfoRequest.java @@ -23,7 +23,9 @@ public class XPackInfoRequest extends ActionRequest { public enum Category { - BUILD, LICENSE, FEATURES; + BUILD, + LICENSE, + FEATURES; public static EnumSet toSet(String... categories) { EnumSet set = EnumSet.noneOf(Category.class); @@ -44,8 +46,7 @@ public static EnumSet toSet(String... categories) { private boolean verbose; private EnumSet categories = EnumSet.noneOf(Category.class); - public XPackInfoRequest() { - } + public XPackInfoRequest() {} public XPackInfoRequest(StreamInput in) throws IOException { super(in); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/XPackInfoResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/XPackInfoResponse.java index 68d2c6068cc4a..b69ab12eff76c 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/XPackInfoResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/XPackInfoResponse.java @@ -8,18 +8,18 @@ import org.elasticsearch.Version; import org.elasticsearch.action.ActionResponse; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.ConstructingObjectParser; -import org.elasticsearch.xcontent.ToXContentObject; -import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.license.License; import org.elasticsearch.protocol.xpack.license.LicenseStatus; +import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.ToXContentObject; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.ArrayList; @@ -42,9 +42,12 @@ public class XPackInfoResponse extends ActionResponse implements ToXContentObjec public static final long BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS = Long.MAX_VALUE - TimeUnit.HOURS.toMillis(24 * 365); // TODO move this constant to License.java once we move License.java to the protocol jar - @Nullable private BuildInfo buildInfo; - @Nullable private LicenseInfo licenseInfo; - @Nullable private FeatureSetsInfo featureSetsInfo; + @Nullable + private BuildInfo buildInfo; + @Nullable + private LicenseInfo licenseInfo; + @Nullable + private FeatureSetsInfo featureSetsInfo; public XPackInfoResponse(StreamInput in) throws IOException { super(in); @@ -94,8 +97,8 @@ public boolean equals(Object other) { if (this == other) return true; XPackInfoResponse rhs = (XPackInfoResponse) other; return Objects.equals(buildInfo, rhs.buildInfo) - && Objects.equals(licenseInfo, rhs.licenseInfo) - && Objects.equals(featureSetsInfo, rhs.featureSetsInfo); + && Objects.equals(licenseInfo, rhs.licenseInfo) + && Objects.equals(featureSetsInfo, rhs.featureSetsInfo); } @Override @@ -116,8 +119,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.field("build", buildInfo, params); } - EnumSet categories = XPackInfoRequest.Category - .toSet(Strings.splitStringByCommaToArray(params.param("categories", "_all"))); + EnumSet categories = XPackInfoRequest.Category.toSet( + Strings.splitStringByCommaToArray(params.param("categories", "_all")) + ); if (licenseInfo != null) { builder.field("license", licenseInfo, params); } else if (categories.contains(XPackInfoRequest.Category.LICENSE)) { @@ -192,10 +196,10 @@ public boolean equals(Object other) { if (this == other) return true; LicenseInfo rhs = (LicenseInfo) other; return Objects.equals(uid, rhs.uid) - && Objects.equals(type, rhs.type) - && Objects.equals(mode, rhs.mode) - && Objects.equals(status, rhs.status) - && expiryDate == rhs.expiryDate; + && Objects.equals(type, rhs.type) + && Objects.equals(mode, rhs.mode) + && Objects.equals(status, rhs.status) + && expiryDate == rhs.expiryDate; } @Override @@ -265,8 +269,7 @@ public boolean equals(Object other) { if (other == null || other.getClass() != getClass()) return false; if (this == other) return true; BuildInfo rhs = (BuildInfo) other; - return Objects.equals(hash, rhs.hash) - && Objects.equals(timestamp, rhs.timestamp); + return Objects.equals(hash, rhs.hash) && Objects.equals(timestamp, rhs.timestamp); } @Override @@ -275,7 +278,10 @@ public int hashCode() { } private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "build_info", true, (a, v) -> new BuildInfo((String) a[0], (String) a[1])); + "build_info", + true, + (a, v) -> new BuildInfo((String) a[0], (String) a[1]) + ); static { PARSER.declareString(constructorArg(), new ParseField("hash")); PARSER.declareString(constructorArg(), new ParseField("date")); @@ -283,10 +289,7 @@ public int hashCode() { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder.startObject() - .field("hash", hash) - .field("date", timestamp) - .endObject(); + return builder.startObject().field("hash", hash).field("date", timestamp).endObject(); } } @@ -403,9 +406,7 @@ public boolean equals(Object other) { if (other == null || other.getClass() != getClass()) return false; if (this == other) return true; FeatureSet rhs = (FeatureSet) other; - return Objects.equals(name, rhs.name) - && available == rhs.available - && enabled == rhs.enabled; + return Objects.equals(name, rhs.name) && available == rhs.available && enabled == rhs.enabled; } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/XPackUsageResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/XPackUsageResponse.java index 24a193826fe8f..f1a734c352269 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/XPackUsageResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/XPackUsageResponse.java @@ -27,7 +27,7 @@ private XPackUsageResponse(Map> usages) throws IOExc @SuppressWarnings("unchecked") private static Map castMap(Object value) { - return (Map)value; + return (Map) value; } /** Return a map from feature name to usage information for that feature. */ @@ -37,8 +37,9 @@ public Map> getUsages() { public static XPackUsageResponse fromXContent(XContentParser parser) throws IOException { Map rawMap = parser.map(); - Map> usages = rawMap.entrySet().stream().collect( - Collectors.toMap(Map.Entry::getKey, e -> castMap(e.getValue()))); + Map> usages = rawMap.entrySet() + .stream() + .collect(Collectors.toMap(Map.Entry::getKey, e -> castMap(e.getValue()))); return new XPackUsageResponse(usages); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/frozen/FreezeRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/frozen/FreezeRequest.java index 78cab77c10c3d..ea83e340841b1 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/frozen/FreezeRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/frozen/FreezeRequest.java @@ -20,8 +20,7 @@ import static org.elasticsearch.action.ValidateActions.addValidationError; -public class FreezeRequest extends AcknowledgedRequest - implements IndicesRequest.Replaceable { +public class FreezeRequest extends AcknowledgedRequest implements IndicesRequest.Replaceable { private String[] indices; private boolean freeze = true; private IndicesOptions indicesOptions = IndicesOptions.strictExpandOpen(); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/graph/Connection.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/graph/Connection.java index c2fb52e5214a3..27686da06f0ea 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/graph/Connection.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/graph/Connection.java @@ -8,14 +8,14 @@ import com.carrotsearch.hppc.ObjectIntHashMap; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.protocol.xpack.graph.Vertex.VertexId; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent.Params; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.protocol.xpack.graph.Vertex.VertexId; import java.io.IOException; import java.util.List; @@ -50,8 +50,7 @@ public Connection(StreamInput in, Map vertices) throws IOExcep docCount = in.readVLong(); } - Connection() { - } + Connection() {} void writeTo(StreamOutput out) throws IOException { out.writeString(from.getField()); @@ -91,17 +90,11 @@ public long getDocCount() { @Override public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; + if (this == obj) return true; + if (obj == null) return false; + if (getClass() != obj.getClass()) return false; Connection other = (Connection) obj; - return docCount == other.docCount && - weight == other.weight && - Objects.equals(to, other.to) && - Objects.equals(from, other.from); + return docCount == other.docCount && weight == other.weight && Objects.equals(to, other.to) && Objects.equals(from, other.from); } @Override @@ -109,13 +102,11 @@ public int hashCode() { return Objects.hash(docCount, weight, from, to); } - private static final ParseField SOURCE = new ParseField("source"); private static final ParseField TARGET = new ParseField("target"); private static final ParseField WEIGHT = new ParseField("weight"); private static final ParseField DOC_COUNT = new ParseField("doc_count"); - void toXContent(XContentBuilder builder, Params params, ObjectIntHashMap vertexNumbers) throws IOException { builder.field(SOURCE.getPreferredName(), vertexNumbers.get(from)); builder.field(TARGET.getPreferredName(), vertexNumbers.get(to)); @@ -123,7 +114,7 @@ void toXContent(XContentBuilder builder, Params params, ObjectIntHashMap builder.field(DOC_COUNT.getPreferredName(), docCount); } - //When deserializing from XContent we need to wait for all vertices to be loaded before + // When deserializing from XContent we need to wait for all vertices to be loaded before // Connection objects can be created that reference them. This class provides the interim // state for connections. static class UnresolvedConnection { @@ -131,6 +122,7 @@ static class UnresolvedConnection { int toIndex; double weight; long docCount; + UnresolvedConnection(int fromIndex, int toIndex, double weight, long docCount) { super(); this.fromIndex = fromIndex; @@ -138,19 +130,22 @@ static class UnresolvedConnection { this.weight = weight; this.docCount = docCount; } + public Connection resolve(List vertices) { return new Connection(vertices.get(fromIndex), vertices.get(toIndex), weight, docCount); } private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "ConnectionParser", true, - args -> { - int source = (Integer) args[0]; - int target = (Integer) args[1]; - double weight = (Double) args[2]; - long docCount = (Long) args[3]; - return new UnresolvedConnection(source, target, weight, docCount); - }); + "ConnectionParser", + true, + args -> { + int source = (Integer) args[0]; + int target = (Integer) args[1]; + double weight = (Double) args[2]; + long docCount = (Long) args[3]; + return new UnresolvedConnection(source, target, weight, docCount); + } + ); static { PARSER.declareInt(constructorArg(), SOURCE); @@ -158,12 +153,12 @@ public Connection resolve(List vertices) { PARSER.declareDouble(constructorArg(), WEIGHT); PARSER.declareLong(constructorArg(), DOC_COUNT); } + static UnresolvedConnection fromXContent(XContentParser parser) throws IOException { return PARSER.apply(parser, null); } } - /** * An identifier (implements hashcode and equals) that represents a * unique key for a {@link Connection} diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/graph/GraphExploreRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/graph/GraphExploreRequest.java index 15f189b6330fa..729a287a4cde3 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/graph/GraphExploreRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/graph/GraphExploreRequest.java @@ -16,12 +16,12 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.ToXContentObject; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.search.aggregations.bucket.sampler.SamplerAggregationBuilder; import org.elasticsearch.search.aggregations.bucket.terms.SignificantTerms; import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregator; +import org.elasticsearch.xcontent.ToXContentObject; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.ArrayList; @@ -49,8 +49,7 @@ public class GraphExploreRequest extends ActionRequest implements IndicesRequest private List hops = new ArrayList<>(); - public GraphExploreRequest() { - } + public GraphExploreRequest() {} /** * Constructs a new graph request to run against the provided indices. No @@ -327,8 +326,7 @@ public TermBoost(String term, float boost) { this.boost = boost; } - TermBoost() { - } + TermBoost() {} public String getTerm() { return term; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/graph/GraphExploreResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/graph/GraphExploreResponse.java index 2a058681bb68d..e6d43a232142b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/graph/GraphExploreResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/graph/GraphExploreResponse.java @@ -11,17 +11,17 @@ import org.elasticsearch.action.ActionResponse; import org.elasticsearch.action.ShardOperationFailedException; import org.elasticsearch.action.search.ShardSearchFailure; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.protocol.xpack.graph.Connection.ConnectionId; +import org.elasticsearch.protocol.xpack.graph.Connection.UnresolvedConnection; +import org.elasticsearch.protocol.xpack.graph.Vertex.VertexId; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.protocol.xpack.graph.Connection.ConnectionId; -import org.elasticsearch.protocol.xpack.graph.Connection.UnresolvedConnection; -import org.elasticsearch.protocol.xpack.graph.Vertex.VertexId; import java.io.IOException; import java.util.Collection; @@ -85,8 +85,14 @@ public GraphExploreResponse(StreamInput in) throws IOException { } - public GraphExploreResponse(long tookInMillis, boolean timedOut, ShardOperationFailedException[] shardFailures, - Map vertices, Map connections, boolean returnDetailedInfo) { + public GraphExploreResponse( + long tookInMillis, + boolean timedOut, + ShardOperationFailedException[] shardFailures, + Map vertices, + Map connections, + boolean returnDetailedInfo + ) { this.tookInMillis = tookInMillis; this.timedOut = timedOut; this.shardFailures = shardFailures; @@ -95,7 +101,6 @@ public GraphExploreResponse(long tookInMillis, boolean timedOut, ShardOperationF this.returnDetailedInfo = returnDetailedInfo; } - public TimeValue getTook() { return new TimeValue(tookInMillis); } @@ -111,6 +116,7 @@ public long getTookInMillis() { public boolean isTimedOut() { return this.timedOut; } + public ShardOperationFailedException[] getShardFailures() { return shardFailures; } @@ -198,36 +204,38 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws } private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "GraphExploreResponsenParser", true, - args -> { - GraphExploreResponse result = new GraphExploreResponse(); - result.vertices = new HashMap<>(); - result.connections = new HashMap<>(); - - result.tookInMillis = (Long) args[0]; - result.timedOut = (Boolean) args[1]; - - @SuppressWarnings("unchecked") - List vertices = (List) args[2]; - @SuppressWarnings("unchecked") - List unresolvedConnections = (List) args[3]; - @SuppressWarnings("unchecked") - List failures = (List) args[4]; - for (Vertex vertex : vertices) { - // reverse-engineer if detailed stats were requested - - // mainly here for testing framework's equality tests - result.returnDetailedInfo = result.returnDetailedInfo || vertex.getFg() > 0; - result.vertices.put(vertex.getId(), vertex); - } - for (UnresolvedConnection unresolvedConnection : unresolvedConnections) { - Connection resolvedConnection = unresolvedConnection.resolve(vertices); - result.connections.put(resolvedConnection.getId(), resolvedConnection); - } - if (failures.size() > 0) { - result.shardFailures = failures.toArray(new ShardSearchFailure[failures.size()]); - } - return result; - }); + "GraphExploreResponsenParser", + true, + args -> { + GraphExploreResponse result = new GraphExploreResponse(); + result.vertices = new HashMap<>(); + result.connections = new HashMap<>(); + + result.tookInMillis = (Long) args[0]; + result.timedOut = (Boolean) args[1]; + + @SuppressWarnings("unchecked") + List vertices = (List) args[2]; + @SuppressWarnings("unchecked") + List unresolvedConnections = (List) args[3]; + @SuppressWarnings("unchecked") + List failures = (List) args[4]; + for (Vertex vertex : vertices) { + // reverse-engineer if detailed stats were requested - + // mainly here for testing framework's equality tests + result.returnDetailedInfo = result.returnDetailedInfo || vertex.getFg() > 0; + result.vertices.put(vertex.getId(), vertex); + } + for (UnresolvedConnection unresolvedConnection : unresolvedConnections) { + Connection resolvedConnection = unresolvedConnection.resolve(vertices); + result.connections.put(resolvedConnection.getId(), resolvedConnection); + } + if (failures.size() > 0) { + result.shardFailures = failures.toArray(new ShardSearchFailure[failures.size()]); + } + return result; + } + ); static { PARSER.declareLong(constructorArg(), TOOK); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/graph/Hop.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/graph/Hop.java index 4a5ac89f6c327..88a3913ae4bbf 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/graph/Hop.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/graph/Hop.java @@ -10,10 +10,10 @@ import org.elasticsearch.action.ValidateActions; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.xcontent.ToXContentFragment; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; +import org.elasticsearch.xcontent.ToXContentFragment; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.ArrayList; @@ -44,7 +44,7 @@ *

    * */ -public class Hop implements ToXContentFragment{ +public class Hop implements ToXContentFragment { final Hop parentHop; List vertices = null; QueryBuilder guidingQuery = null; @@ -149,7 +149,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.field("query"); guidingQuery.toXContent(builder, params); } - if(vertices != null && vertices.size()>0) { + if (vertices != null && vertices.size() > 0) { builder.startArray("vertices"); for (VertexRequest vertexRequest : vertices) { vertexRequest.toXContent(builder, params); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/graph/Vertex.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/graph/Vertex.java index 253bc708cf13c..a266008dd9dd8 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/graph/Vertex.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/graph/Vertex.java @@ -6,10 +6,10 @@ */ package org.elasticsearch.protocol.xpack.graph; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentFragment; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -42,7 +42,6 @@ public class Vertex implements ToXContentFragment { private static final ParseField FG = new ParseField("fg"); private static final ParseField BG = new ParseField("bg"); - public Vertex(String field, String term, double weight, int depth, long bg, long fg) { super(); this.field = field; @@ -73,19 +72,16 @@ public int hashCode() { @Override public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; + if (this == obj) return true; + if (obj == null) return false; + if (getClass() != obj.getClass()) return false; Vertex other = (Vertex) obj; - return depth == other.depth && - weight == other.weight && - bg == other.bg && - fg == other.fg && - Objects.equals(field, other.field) && - Objects.equals(term, other.term); + return depth == other.depth + && weight == other.weight + && bg == other.bg + && fg == other.fg + && Objects.equals(field, other.field) + && Objects.equals(term, other.term); } @@ -103,20 +99,17 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws return builder; } - - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "VertexParser", true, - args -> { - String field = (String) args[0]; - String term = (String) args[1]; - double weight = (Double) args[2]; - int depth = (Integer) args[3]; - Long optionalBg = (Long) args[4]; - Long optionalFg = (Long) args[5]; - long bg = optionalBg == null ? 0 : optionalBg; - long fg = optionalFg == null ? 0 : optionalFg; - return new Vertex(field, term, weight, depth, bg, fg); - }); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("VertexParser", true, args -> { + String field = (String) args[0]; + String term = (String) args[1]; + double weight = (Double) args[2]; + int depth = (Integer) args[3]; + Long optionalBg = (Long) args[4]; + Long optionalFg = (Long) args[5]; + long bg = optionalBg == null ? 0 : optionalBg; + long fg = optionalFg == null ? 0 : optionalFg; + return new Vertex(field, term, weight, depth, bg, fg); + }); static { PARSER.declareString(constructorArg(), FIELD); @@ -131,7 +124,6 @@ static Vertex fromXContent(XContentParser parser) throws IOException { return PARSER.apply(parser, null); } - /** * @return a {@link VertexId} object that uniquely identifies this Vertex */ @@ -146,7 +138,7 @@ public VertexId getId() { * @return a {@link VertexId} that can be used for looking up vertices */ public static VertexId createId(String field, String term) { - return new VertexId(field,term); + return new VertexId(field, term); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/graph/VertexRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/graph/VertexRequest.java index a5e19ead0a7e6..90f0610faee3f 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/graph/VertexRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/graph/VertexRequest.java @@ -8,9 +8,9 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.protocol.xpack.graph.GraphExploreRequest.TermBoost; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.protocol.xpack.graph.GraphExploreRequest.TermBoost; import java.io.IOException; import java.util.HashMap; @@ -35,7 +35,6 @@ public class VertexRequest implements ToXContentObject { public static final int DEFAULT_SHARD_MIN_DOC_COUNT = 2; private int shardMinDocCount = DEFAULT_SHARD_MIN_DOC_COUNT; - public VertexRequest() { } @@ -183,7 +182,6 @@ public VertexRequest minDocCount(int value) { return this; } - public int shardMinDocCount() { return Math.min(shardMinDocCount, minDocCount); } @@ -212,7 +210,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws if (shardMinDocCount != DEFAULT_SHARD_MIN_DOC_COUNT) { builder.field("shard_min_doc_count", shardMinDocCount); } - if(includes!=null) { + if (includes != null) { builder.startArray("include"); for (TermBoost tb : includes.values()) { builder.startObject(); @@ -222,7 +220,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws } builder.endArray(); } - if(excludes!=null) { + if (excludes != null) { builder.startArray("exclude"); for (String value : excludes) { builder.value(value); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/license/DeleteLicenseRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/license/DeleteLicenseRequest.java index d30f63b34cf7b..b4f0642d37075 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/license/DeleteLicenseRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/license/DeleteLicenseRequest.java @@ -12,7 +12,6 @@ import java.io.IOException; - public class DeleteLicenseRequest extends AcknowledgedRequest { public DeleteLicenseRequest() {} diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/license/GetLicenseRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/license/GetLicenseRequest.java index e9dee7a1009ac..e96c6a7632ec1 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/license/GetLicenseRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/license/GetLicenseRequest.java @@ -12,11 +12,9 @@ import java.io.IOException; - public class GetLicenseRequest extends MasterNodeReadRequest { - public GetLicenseRequest() { - } + public GetLicenseRequest() {} public GetLicenseRequest(StreamInput in) throws IOException { super(in); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/license/LicenseStatus.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/license/LicenseStatus.java index b3ecbc3357e08..5e311f4e86a55 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/license/LicenseStatus.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/license/LicenseStatus.java @@ -6,12 +6,12 @@ */ package org.elasticsearch.protocol.xpack.license; -import java.io.IOException; - import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import java.io.IOException; + /** * Status of an X-Pack license. */ diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/license/LicensesStatus.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/license/LicensesStatus.java index ef87638a54ddb..a555336939b45 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/license/LicensesStatus.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/license/LicensesStatus.java @@ -35,7 +35,6 @@ public static LicensesStatus fromId(int id) { } } - @Override public String toString() { return this.name().toLowerCase(Locale.ROOT); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponse.java index e0b57c2e08229..84a3b1ba984c9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponse.java @@ -10,8 +10,8 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.protocol.xpack.common.ProtocolUtils; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.Collections; @@ -47,8 +47,12 @@ public PutLicenseResponse(boolean acknowledged, LicensesStatus status) { this(acknowledged, status, null, Collections.emptyMap()); } - public PutLicenseResponse(boolean acknowledged, LicensesStatus status, String acknowledgeHeader, - Map acknowledgeMessages) { + public PutLicenseResponse( + boolean acknowledged, + LicensesStatus status, + String acknowledgeHeader, + Map acknowledgeMessages + ) { super(acknowledged); this.status = status; this.acknowledgeHeader = acknowledgeHeader; @@ -111,9 +115,9 @@ public boolean equals(Object o) { if (super.equals(o) == false) return false; PutLicenseResponse that = (PutLicenseResponse) o; - return status == that.status && - ProtocolUtils.equals(acknowledgeMessages, that.acknowledgeMessages) && - Objects.equals(acknowledgeHeader, that.acknowledgeHeader); + return status == that.status + && ProtocolUtils.equals(acknowledgeMessages, that.acknowledgeMessages) + && Objects.equals(acknowledgeHeader, that.acknowledgeHeader); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/watcher/DeleteWatchRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/watcher/DeleteWatchRequest.java index b4ea92e24e61e..1b67e9af02fd0 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/watcher/DeleteWatchRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/watcher/DeleteWatchRequest.java @@ -52,7 +52,7 @@ public void setId(String id) { @Override public ActionRequestValidationException validate() { ActionRequestValidationException validationException = null; - if (id == null){ + if (id == null) { validationException = ValidateActions.addValidationError("watch id is missing", validationException); } else if (PutWatchRequest.isValidId(id) == false) { validationException = ValidateActions.addValidationError("watch id contains whitespace", validationException); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/watcher/DeleteWatchResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/watcher/DeleteWatchResponse.java index bc1eb4c8d1191..80863c8b20cec 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/watcher/DeleteWatchResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/watcher/DeleteWatchResponse.java @@ -7,10 +7,10 @@ package org.elasticsearch.protocol.xpack.watcher; import org.elasticsearch.action.ActionResponse; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -20,8 +20,10 @@ public class DeleteWatchResponse extends ActionResponse implements ToXContentObject { - private static final ObjectParser PARSER - = new ObjectParser<>("x_pack_delete_watch_response", DeleteWatchResponse::new); + private static final ObjectParser PARSER = new ObjectParser<>( + "x_pack_delete_watch_response", + DeleteWatchResponse::new + ); static { PARSER.declareString(DeleteWatchResponse::setId, new ParseField("_id")); PARSER.declareLong(DeleteWatchResponse::setVersion, new ParseField("_version")); @@ -95,11 +97,7 @@ public void writeTo(StreamOutput out) throws IOException { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder.startObject() - .field("_id", id) - .field("_version", version) - .field("found", found) - .endObject(); + return builder.startObject().field("_id", id).field("_version", version).field("found", found).endObject(); } public static DeleteWatchResponse fromXContent(XContentParser parser) throws IOException { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/watcher/PutWatchRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/watcher/PutWatchRequest.java index b6e9a079402e8..9e863511d6ed1 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/watcher/PutWatchRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/watcher/PutWatchRequest.java @@ -14,8 +14,8 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.lucene.uid.Versions; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.seqno.SequenceNumbers; +import org.elasticsearch.xcontent.XContentType; import java.io.IOException; import java.util.regex.Pattern; @@ -48,7 +48,8 @@ public PutWatchRequest(StreamInput in) throws IOException { id = in.readString(); source = in.readBytesReference(); active = in.readBoolean(); - xContentType = in.readEnum(XContentType.class);; + xContentType = in.readEnum(XContentType.class); + ; version = in.readZLong(); ifSeqNo = in.readZLong(); ifPrimaryTerm = in.readVLong(); @@ -139,7 +140,7 @@ public void setVersion(long version) { */ public PutWatchRequest setIfSeqNo(long seqNo) { if (seqNo < 0 && seqNo != UNASSIGNED_SEQ_NO) { - throw new IllegalArgumentException("sequence numbers must be non negative. got [" + seqNo + "]."); + throw new IllegalArgumentException("sequence numbers must be non negative. got [" + seqNo + "]."); } ifSeqNo = seqNo; return this; @@ -200,8 +201,10 @@ public ActionRequestValidationException validate() { validationException = addValidationError("ifSeqNo is set, but primary term is [0]", validationException); } if (ifPrimaryTerm != UNASSIGNED_PRIMARY_TERM && ifSeqNo == UNASSIGNED_SEQ_NO) { - validationException = - addValidationError("ifSeqNo is unassigned, but primary term is [" + ifPrimaryTerm + "]", validationException); + validationException = addValidationError( + "ifSeqNo is unassigned, but primary term is [" + ifPrimaryTerm + "]", + validationException + ); } return validationException; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/watcher/PutWatchResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/watcher/PutWatchResponse.java index 283b42fff54a0..f4b355b7ff1f1 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/watcher/PutWatchResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/watcher/PutWatchResponse.java @@ -7,28 +7,30 @@ package org.elasticsearch.protocol.xpack.watcher; import org.elasticsearch.action.ActionResponse; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.index.seqno.SequenceNumbers; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.index.seqno.SequenceNumbers; import java.io.IOException; import java.util.Objects; public class PutWatchResponse extends ActionResponse implements ToXContentObject { - private static final ObjectParser PARSER - = new ObjectParser<>("x_pack_put_watch_response", PutWatchResponse::new); + private static final ObjectParser PARSER = new ObjectParser<>( + "x_pack_put_watch_response", + PutWatchResponse::new + ); static { - PARSER.declareString(PutWatchResponse::setId, new ParseField("_id")); - PARSER.declareLong(PutWatchResponse::setVersion, new ParseField("_version")); - PARSER.declareLong(PutWatchResponse::setSeqNo, new ParseField("_seq_no")); - PARSER.declareLong(PutWatchResponse::setPrimaryTerm, new ParseField("_primary_term")); - PARSER.declareBoolean(PutWatchResponse::setCreated, new ParseField("created")); + PARSER.declareString(PutWatchResponse::setId, new ParseField("_id")); + PARSER.declareLong(PutWatchResponse::setVersion, new ParseField("_version")); + PARSER.declareLong(PutWatchResponse::setSeqNo, new ParseField("_seq_no")); + PARSER.declareLong(PutWatchResponse::setPrimaryTerm, new ParseField("_primary_term")); + PARSER.declareBoolean(PutWatchResponse::setCreated, new ParseField("created")); } private String id; @@ -103,9 +105,11 @@ public boolean equals(Object o) { PutWatchResponse that = (PutWatchResponse) o; - return Objects.equals(id, that.id) && Objects.equals(version, that.version) + return Objects.equals(id, that.id) + && Objects.equals(version, that.version) && Objects.equals(seqNo, that.seqNo) - && Objects.equals(primaryTerm, that.primaryTerm) && Objects.equals(created, that.created); + && Objects.equals(primaryTerm, that.primaryTerm) + && Objects.equals(created, that.created); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/snapshots/sourceonly/SeqIdGeneratingFilterReader.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/snapshots/sourceonly/SeqIdGeneratingFilterReader.java index 01a324c105cdf..5a3527ec28134 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/snapshots/sourceonly/SeqIdGeneratingFilterReader.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/snapshots/sourceonly/SeqIdGeneratingFilterReader.java @@ -67,7 +67,7 @@ public int docID() { @Override public int nextDoc() { - if (docID+1 < maxDoc) { + if (docID + 1 < maxDoc) { docID++; } else { docID = NO_MORE_DOCS; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/snapshots/sourceonly/SourceOnlySnapshot.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/snapshots/sourceonly/SourceOnlySnapshot.java index 30f047a0e9a94..855c25cc6ae16 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/snapshots/sourceonly/SourceOnlySnapshot.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/snapshots/sourceonly/SourceOnlySnapshot.java @@ -96,8 +96,10 @@ public synchronized List syncSnapshot(IndexCommit commit) throws IOExcep } List createdFiles = new ArrayList<>(); String segmentFileName; - try (Lock writeLock = targetDirectory.obtainLock(IndexWriter.WRITE_LOCK_NAME); - StandardDirectoryReader reader = (StandardDirectoryReader) DirectoryReader.open(commit)) { + try ( + Lock writeLock = targetDirectory.obtainLock(IndexWriter.WRITE_LOCK_NAME); + StandardDirectoryReader reader = (StandardDirectoryReader) DirectoryReader.open(commit) + ) { SegmentInfos segmentInfos = reader.getSegmentInfos().clone(); DirectoryReader wrappedReader = wrapReader(reader); List newInfos = new ArrayList<>(); @@ -113,8 +115,11 @@ public synchronized List syncSnapshot(IndexCommit commit) throws IOExcep segmentInfos.clear(); segmentInfos.addAll(newInfos); segmentInfos.setNextWriteGeneration(Math.max(segmentInfos.getGeneration(), generation) + 1); - String pendingSegmentFileName = IndexFileNames.fileNameFromGeneration(IndexFileNames.PENDING_SEGMENTS, - "", segmentInfos.getGeneration()); + String pendingSegmentFileName = IndexFileNames.fileNameFromGeneration( + IndexFileNames.PENDING_SEGMENTS, + "", + segmentInfos.getGeneration() + ); try (IndexOutput segnOutput = targetDirectory.createOutput(pendingSegmentFileName, IOContext.DEFAULT)) { segmentInfos.write(segnOutput); } @@ -172,7 +177,6 @@ private int apply(DocIdSetIterator iterator, FixedBitSet bits) throws IOExceptio return newDeletes; } - private boolean assertCheckIndex() throws IOException { ByteArrayOutputStream output = new ByteArrayOutputStream(1024); try (CheckIndex checkIndex = new CheckIndex(targetDirectory)) { @@ -198,8 +202,13 @@ DirectoryReader wrapReader(DirectoryReader reader) throws IOException { return softDeletesField == null ? reader : new SoftDeletesDirectoryReaderWrapper(reader, softDeletesField); } - private SegmentCommitInfo syncSegment(SegmentCommitInfo segmentCommitInfo, LiveDocs liveDocs, FieldInfos fieldInfos, - Map existingSegments, List createdFiles) throws IOException { + private SegmentCommitInfo syncSegment( + SegmentCommitInfo segmentCommitInfo, + LiveDocs liveDocs, + FieldInfos fieldInfos, + Map existingSegments, + List createdFiles + ) throws IOException { Directory toClose = null; try { SegmentInfo si = segmentCommitInfo.info; @@ -207,7 +216,8 @@ private SegmentCommitInfo syncSegment(SegmentCommitInfo segmentCommitInfo, LiveD Directory sourceDir = si.dir; if (si.getUseCompoundFile()) { sourceDir = new LinkedFilesDirectory.CloseMePleaseWrapper( - codec.compoundFormat().getCompoundReader(sourceDir, si, IOContext.DEFAULT)); + codec.compoundFormat().getCompoundReader(sourceDir, si, IOContext.DEFAULT) + ); toClose = sourceDir; } final String segmentSuffix = ""; @@ -216,15 +226,42 @@ private SegmentCommitInfo syncSegment(SegmentCommitInfo segmentCommitInfo, LiveD BytesRef segmentId = new BytesRef(si.getId()); boolean exists = existingSegments.containsKey(segmentId); if (exists == false) { - SegmentInfo newSegmentInfo = new SegmentInfo(targetDirectory, si.getVersion(), si.getMinVersion(), si.name, si.maxDoc(), - false, si.getCodec(), si.getDiagnostics(), si.getId(), si.getAttributes(), null); + SegmentInfo newSegmentInfo = new SegmentInfo( + targetDirectory, + si.getVersion(), + si.getMinVersion(), + si.name, + si.maxDoc(), + false, + si.getCodec(), + si.getDiagnostics(), + si.getId(), + si.getAttributes(), + null + ); // we drop the sort on purpose since the field we sorted on doesn't exist in the target index anymore. newInfo = new SegmentCommitInfo(newSegmentInfo, 0, 0, -1, -1, -1, StringHelper.randomId()); List fieldInfoCopy = new ArrayList<>(fieldInfos.size()); for (FieldInfo fieldInfo : fieldInfos) { - fieldInfoCopy.add(new FieldInfo(fieldInfo.name, fieldInfo.number, - false, false, false, IndexOptions.NONE, DocValuesType.NONE, -1, fieldInfo.attributes(), 0, 0, 0, - 0, VectorSimilarityFunction.EUCLIDEAN, fieldInfo.isSoftDeletesField())); + fieldInfoCopy.add( + new FieldInfo( + fieldInfo.name, + fieldInfo.number, + false, + false, + false, + IndexOptions.NONE, + DocValuesType.NONE, + -1, + fieldInfo.attributes(), + 0, + 0, + 0, + 0, + VectorSimilarityFunction.EUCLIDEAN, + fieldInfo.isSoftDeletesField() + ) + ); } FieldInfos newFieldInfos = new FieldInfos(fieldInfoCopy.toArray(new FieldInfo[0])); codec.fieldInfosFormat().write(trackingDir, newSegmentInfo, segmentSuffix, newFieldInfos, IOContext.DEFAULT); @@ -252,10 +289,17 @@ private SegmentCommitInfo syncSegment(SegmentCommitInfo segmentCommitInfo, LiveD if (liveDocs.bits != null && liveDocs.numDeletes != 0 && liveDocs.numDeletes != newInfo.getDelCount()) { assert newInfo.getDelCount() == 0 || assertLiveDocs(liveDocs.bits, liveDocs.numDeletes); - codec.liveDocsFormat().writeLiveDocs(liveDocs.bits, trackingDir, newInfo, liveDocs.numDeletes - newInfo.getDelCount(), - IOContext.DEFAULT); - SegmentCommitInfo info = new SegmentCommitInfo(newInfo.info, liveDocs.numDeletes, 0, newInfo.getNextDelGen(), - -1, -1, StringHelper.randomId()); + codec.liveDocsFormat() + .writeLiveDocs(liveDocs.bits, trackingDir, newInfo, liveDocs.numDeletes - newInfo.getDelCount(), IOContext.DEFAULT); + SegmentCommitInfo info = new SegmentCommitInfo( + newInfo.info, + liveDocs.numDeletes, + 0, + newInfo.getNextDelGen(), + -1, + -1, + StringHelper.randomId() + ); info.setFieldInfosFiles(newInfo.getFieldInfosFiles()); info.info.setFiles(trackingDir.getCreatedFiles()); newInfo = info; @@ -380,8 +424,9 @@ public void syncMetaData() throws IOException { @Override public void rename(String source, String dest) throws IOException { if (linkedFiles.containsKey(source) || linkedFiles.containsKey(dest)) { - throw new IllegalArgumentException("file cannot be renamed as linked file with name " + source + " or " + dest + - " already exists"); + throw new IllegalArgumentException( + "file cannot be renamed as linked file with name " + source + " or " + dest + " already exists" + ); } else { wrapped.rename(source, dest); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/snapshots/sourceonly/SourceOnlySnapshotRepository.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/snapshots/sourceonly/SourceOnlySnapshotRepository.java index a246f560cd6b6..66be6d081dbda 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/snapshots/sourceonly/SourceOnlySnapshotRepository.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/snapshots/sourceonly/SourceOnlySnapshotRepository.java @@ -71,10 +71,19 @@ * match_all scroll searches in order to reindex the data. */ public final class SourceOnlySnapshotRepository extends FilterRepository { - private static final Setting DELEGATE_TYPE = new Setting<>("delegate_type", "", Function.identity(), Setting.Property - .NodeScope); - public static final Setting SOURCE_ONLY = Setting.boolSetting("index.source_only", false, Setting - .Property.IndexScope, Setting.Property.Final, Setting.Property.PrivateIndex); + private static final Setting DELEGATE_TYPE = new Setting<>( + "delegate_type", + "", + Function.identity(), + Setting.Property.NodeScope + ); + public static final Setting SOURCE_ONLY = Setting.boolSetting( + "index.source_only", + false, + Setting.Property.IndexScope, + Setting.Property.Final, + Setting.Property.PrivateIndex + ); private static final Logger logger = LogManager.getLogger(SourceOnlySnapshotRepository.class); @@ -93,10 +102,7 @@ public void finalizeSnapshot(FinalizeSnapshotContext finalizeSnapshotContext) { new FinalizeSnapshotContext( finalizeSnapshotContext.updatedShardGenerations(), finalizeSnapshotContext.repositoryStateId(), - metadataToSnapshot( - finalizeSnapshotContext.updatedShardGenerations().indices(), - finalizeSnapshotContext.clusterMetadata() - ), + metadataToSnapshot(finalizeSnapshotContext.updatedShardGenerations().indices(), finalizeSnapshotContext.clusterMetadata()), finalizeSnapshotContext.snapshotInfo(), finalizeSnapshotContext.repositoryMetaVersion(), finalizeSnapshotContext @@ -118,24 +124,25 @@ private static Metadata metadataToSnapshot(Collection indices, Metadata final String mapping = "{ \"_doc\" : { \"enabled\": false, \"_meta\": " + mmd.source().string() + " } }"; indexMetadataBuilder.putMapping(mapping); } - indexMetadataBuilder.settings(Settings.builder().put(index.getSettings()) - .put(SOURCE_ONLY.getKey(), true) - .put("index.blocks.write", true)); // read-only! + indexMetadataBuilder.settings( + Settings.builder().put(index.getSettings()).put(SOURCE_ONLY.getKey(), true).put("index.blocks.write", true) + ); // read-only! indexMetadataBuilder.settingsVersion(1 + indexMetadataBuilder.settingsVersion()); builder.put(indexMetadataBuilder); } return builder.build(); } - @Override public void snapshotShard(SnapshotShardContext context) { final MapperService mapperService = context.mapperService(); if (mapperService.documentMapper() != null // if there is no mapping this is null && mapperService.documentMapper().sourceMapper().isComplete() == false) { context.onFailure( - new IllegalStateException("Can't snapshot _source only on an index that has incomplete source ie. has _source disabled " + - "or filters the source")); + new IllegalStateException( + "Can't snapshot _source only on an index that has incomplete source ie. has _source disabled " + "or filters the source" + ) + ); return; } final Store store = context.store(); @@ -148,8 +155,7 @@ public void snapshotShard(SnapshotShardContext context) { Path snapPath = dataPath.resolve(SNAPSHOT_DIR_NAME); final List toClose = new ArrayList<>(3); try { - SourceOnlySnapshot.LinkedFilesDirectory overlayDir = new SourceOnlySnapshot.LinkedFilesDirectory( - new NIOFSDirectory(snapPath)); + SourceOnlySnapshot.LinkedFilesDirectory overlayDir = new SourceOnlySnapshot.LinkedFilesDirectory(new NIOFSDirectory(snapPath)); toClose.add(overlayDir); Store tempStore = new Store(store.shardId(), store.indexSettings(), overlayDir, new ShardLock(store.shardId()) { @Override @@ -165,8 +171,13 @@ protected void closeInternal() { try { snapshot.syncSnapshot(snapshotIndexCommit); } catch (NoSuchFileException | CorruptIndexException | FileAlreadyExistsException e) { - logger.warn(() -> new ParameterizedMessage( - "Existing staging directory [{}] appears corrupted and will be pruned and recreated.", snapPath), e); + logger.warn( + () -> new ParameterizedMessage( + "Existing staging directory [{}] appears corrupted and will be pruned and recreated.", + snapPath + ), + e + ); Lucene.cleanLuceneIndex(overlayDir); snapshot.syncSnapshot(snapshotIndexCommit); } @@ -179,9 +190,20 @@ protected void closeInternal() { DirectoryReader reader = DirectoryReader.open(tempStore.directory()); toClose.add(reader); IndexCommit indexCommit = reader.getIndexCommit(); - super.snapshotShard(new SnapshotShardContext(tempStore, mapperService, context.snapshotId(), context.indexId(), - new Engine.IndexCommitRef(indexCommit, () -> IOUtils.close(toClose)), context.stateIdentifier(), - context.status(), context.getRepositoryMetaVersion(), context.userMetadata(), context)); + super.snapshotShard( + new SnapshotShardContext( + tempStore, + mapperService, + context.snapshotId(), + context.indexId(), + new Engine.IndexCommitRef(indexCommit, () -> IOUtils.close(toClose)), + context.stateIdentifier(), + context.status(), + context.getRepositoryMetaVersion(), + context.userMetadata(), + context + ) + ); } catch (IOException e) { try { IOUtils.close(toClose); @@ -196,8 +218,7 @@ protected void closeInternal() { * Returns an {@link EngineFactory} for the source only snapshots. */ public static EngineFactory getEngineFactory() { - return config -> new ReadOnlyEngine(config, null, new TranslogStats(0, 0, 0, 0, 0), true, - readerWrapper(config), true, false); + return config -> new ReadOnlyEngine(config, null, new TranslogStats(0, 0, 0, 0, 0), true, readerWrapper(config), true, false); } public static Function readerWrapper(EngineConfig engineConfig) { @@ -228,8 +249,9 @@ public Repository create(RepositoryMetadata metadata, Function PARSER = new ConstructingObjectParser<>("index_template", + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "index_template", false, - a -> new MigrateToDataTiersRequest((String) a[0], (String) a[1])); + a -> new MigrateToDataTiersRequest((String) a[0], (String) a[1]) + ); static { PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), LEGACY_TEMPLATE_TO_DELETE); @@ -105,9 +107,9 @@ public boolean equals(Object o) { return false; } MigrateToDataTiersRequest that = (MigrateToDataTiersRequest) o; - return dryRun == that.dryRun && - Objects.equals(nodeAttributeName, that.nodeAttributeName) && - Objects.equals(legacyTemplateToDelete, that.legacyTemplateToDelete); + return dryRun == that.dryRun + && Objects.equals(nodeAttributeName, that.nodeAttributeName) + && Objects.equals(legacyTemplateToDelete, that.legacyTemplateToDelete); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/cluster/action/MigrateToDataTiersResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/cluster/action/MigrateToDataTiersResponse.java index f2c09498b8469..6780f6c69d540 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/cluster/action/MigrateToDataTiersResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/cluster/action/MigrateToDataTiersResponse.java @@ -10,10 +10,10 @@ import org.elasticsearch.action.ActionResponse; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.core.Nullable; import java.io.IOException; import java.util.List; @@ -32,8 +32,12 @@ public class MigrateToDataTiersResponse extends ActionResponse implements ToXCon private final List migratedIndices; private final boolean dryRun; - public MigrateToDataTiersResponse(@Nullable String removedIndexTemplateName, List migratedPolicies, - List migratedIndices, boolean dryRun) { + public MigrateToDataTiersResponse( + @Nullable String removedIndexTemplateName, + List migratedPolicies, + List migratedIndices, + boolean dryRun + ) { this.removedIndexTemplateName = removedIndexTemplateName; this.migratedPolicies = migratedPolicies; this.migratedIndices = migratedIndices; @@ -106,8 +110,10 @@ public boolean equals(Object o) { return false; } MigrateToDataTiersResponse that = (MigrateToDataTiersResponse) o; - return dryRun == that.dryRun && Objects.equals(removedIndexTemplateName, that.removedIndexTemplateName) && - Objects.equals(migratedPolicies, that.migratedPolicies) && Objects.equals(migratedIndices, that.migratedIndices); + return dryRun == that.dryRun + && Objects.equals(removedIndexTemplateName, that.removedIndexTemplateName) + && Objects.equals(migratedPolicies, that.migratedPolicies) + && Objects.equals(migratedIndices, that.migratedIndices); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/cluster/routing/allocation/DataTierAllocationDecider.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/cluster/routing/allocation/DataTierAllocationDecider.java index 9466d09a30d62..30082aa65f50d 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/cluster/routing/allocation/DataTierAllocationDecider.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/cluster/routing/allocation/DataTierAllocationDecider.java @@ -13,11 +13,11 @@ import org.elasticsearch.cluster.node.DiscoveryNodes; import org.elasticsearch.cluster.routing.RoutingNode; import org.elasticsearch.cluster.routing.ShardRouting; +import org.elasticsearch.cluster.routing.allocation.DataTier; import org.elasticsearch.cluster.routing.allocation.RoutingAllocation; import org.elasticsearch.cluster.routing.allocation.decider.AllocationDecider; import org.elasticsearch.cluster.routing.allocation.decider.Decision; import org.elasticsearch.common.Strings; -import org.elasticsearch.cluster.routing.allocation.DataTier; import java.util.List; import java.util.Optional; @@ -32,8 +32,7 @@ public class DataTierAllocationDecider extends AllocationDecider { public static final String NAME = "data_tier"; - public DataTierAllocationDecider() { - } + public DataTierAllocationDecider() {} @Override public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) { @@ -67,8 +66,12 @@ public interface PreferredTierFunction { Optional apply(List tierPreference, DiscoveryNodes nodes); } - public Decision shouldFilter(IndexMetadata indexMd, Set roles, - PreferredTierFunction preferredTierFunction, RoutingAllocation allocation) { + public Decision shouldFilter( + IndexMetadata indexMd, + Set roles, + PreferredTierFunction preferredTierFunction, + RoutingAllocation allocation + ) { Decision decision = shouldIndexPreferTier(indexMd, roles, preferredTierFunction, allocation); if (decision != null) { return decision; @@ -77,8 +80,12 @@ public Decision shouldFilter(IndexMetadata indexMd, Set roles return allocation.decision(Decision.YES, NAME, "node passes tier preference filters"); } - private Decision shouldIndexPreferTier(IndexMetadata indexMetadata, Set roles, - PreferredTierFunction preferredTierFunction, RoutingAllocation allocation) { + private Decision shouldIndexPreferTier( + IndexMetadata indexMetadata, + Set roles, + PreferredTierFunction preferredTierFunction, + RoutingAllocation allocation + ) { List tierPreference = indexMetadata.getTierPreference(); if (tierPreference.isEmpty() == false) { @@ -89,8 +96,13 @@ private Decision shouldIndexPreferTier(IndexMetadata indexMetadata, Set preferredAvailableTier(List prioritizedTi } static boolean tierNodesPresent(String singleTier, DiscoveryNodes nodes) { - assert singleTier.equals(DiscoveryNodeRole.DATA_ROLE.roleName()) || DataTier.validTierName(singleTier) : - "tier " + singleTier + " is an invalid tier name"; + assert singleTier.equals(DiscoveryNodeRole.DATA_ROLE.roleName()) || DataTier.validTierName(singleTier) + : "tier " + singleTier + " is an invalid tier name"; for (DiscoveryNode node : nodes.getNodes().values()) { for (DiscoveryNodeRole discoveryNodeRole : node.getRoles()) { String s = discoveryNodeRole.roleName(); @@ -143,7 +159,6 @@ static boolean tierNodesPresent(String singleTier, DiscoveryNodes nodes) { return false; } - private static boolean allocationAllowed(String tierName, Set roles) { assert Strings.hasText(tierName) : "tierName must be not null and non-empty, but was [" + tierName + "]"; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/cluster/routing/allocation/mapper/DataTierFieldMapper.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/cluster/routing/allocation/mapper/DataTierFieldMapper.java index 6e8f073bf4a0f..e1f6ec33bc74c 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/cluster/routing/allocation/mapper/DataTierFieldMapper.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/cluster/routing/allocation/mapper/DataTierFieldMapper.java @@ -78,9 +78,7 @@ public ValueFetcher valueFetcher(SearchExecutionContext context, String format) } String tierPreference = getTierPreference(context); - return tierPreference == null - ? (lookup, ignoredValues) -> List.of() - : (lookup, ignoredValues) -> List.of(tierPreference); + return tierPreference == null ? (lookup, ignoredValues) -> List.of() : (lookup, ignoredValues) -> List.of(tierPreference); } /** diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ClientHelper.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ClientHelper.java index 67c44f742a1cf..86da69982bda4 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ClientHelper.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ClientHelper.java @@ -34,8 +34,10 @@ */ public final class ClientHelper { - private static Pattern authorizationHeaderPattern = Pattern.compile("\\s*" + Pattern.quote("Authorization") + "\\s*", - Pattern.CASE_INSENSITIVE); + private static Pattern authorizationHeaderPattern = Pattern.compile( + "\\s*" + Pattern.quote("Authorization") + "\\s*", + Pattern.CASE_INSENSITIVE + ); public static void assertNoAuthorizationHeader(Map headers) { if (org.elasticsearch.Assertions.ENABLED) { @@ -50,11 +52,11 @@ public static void assertNoAuthorizationHeader(Map headers) { /** * List of headers that are related to security */ - public static final Set SECURITY_HEADER_FILTERS = - Sets.newHashSet( - AuthenticationServiceField.RUN_AS_USER_HEADER, - AuthenticationField.AUTHENTICATION_KEY, - SecondaryAuthentication.THREAD_CTX_KEY); + public static final Set SECURITY_HEADER_FILTERS = Sets.newHashSet( + AuthenticationServiceField.RUN_AS_USER_HEADER, + AuthenticationField.AUTHENTICATION_KEY, + SecondaryAuthentication.THREAD_CTX_KEY + ); /** * Leaves only headers that are related to security and filters out the rest. @@ -67,9 +69,11 @@ public static Map filterSecurityHeaders(Map head // fast-track to skip the artifice below return headers; } else { - return Objects.requireNonNull(headers).entrySet().stream() - .filter(e -> SECURITY_HEADER_FILTERS.contains(e.getKey())) - .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); + return Objects.requireNonNull(headers) + .entrySet() + .stream() + .filter(e -> SECURITY_HEADER_FILTERS.contains(e.getKey())) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); } } @@ -110,8 +114,12 @@ public static Client clientWithOrigin(Client client, String origin) { * Executes a consumer after setting the origin and wrapping the listener so that the proper context is restored */ public static void executeAsyncWithOrigin( - ThreadContext threadContext, String origin, Request request, ActionListener listener, - BiConsumer> consumer) { + ThreadContext threadContext, + String origin, + Request request, + ActionListener listener, + BiConsumer> consumer + ) { final Supplier supplier = threadContext.newRestorableContext(false); try (ThreadContext.StoredContext ignore = threadContext.stashWithOrigin(origin)) { consumer.accept(request, new ContextPreservingActionListener<>(supplier, listener)); @@ -122,10 +130,16 @@ public static v * Executes an asynchronous action using the provided client. The origin is set in the context and the listener * is wrapped to ensure the proper context is restored */ - public static > void executeAsyncWithOrigin( - Client client, String origin, ActionType action, Request request, - ActionListener listener) { + public static < + Request extends ActionRequest, + Response extends ActionResponse, + RequestBuilder extends ActionRequestBuilder> void executeAsyncWithOrigin( + Client client, + String origin, + ActionType action, + Request request, + ActionListener listener + ) { final ThreadContext threadContext = client.threadPool().getThreadContext(); final Supplier supplier = threadContext.newRestorableContext(false); try (ThreadContext.StoredContext ignore = threadContext.stashWithOrigin(origin)) { @@ -147,8 +161,12 @@ RequestBuilder extends ActionRequestBuilder> void executeAsyn * The action to run * @return An instance of the response class */ - public static T executeWithHeaders(Map headers, String origin, Client client, - Supplier supplier) { + public static T executeWithHeaders( + Map headers, + String origin, + Client client, + Supplier supplier + ) { Map filteredHeaders = filterSecurityHeaders(headers); // no security headers, we will have to use the xpack internal user for @@ -180,9 +198,14 @@ public static T executeWithHeaders(Map - void executeWithHeadersAsync(Map headers, String origin, Client client, ActionType action, Request request, - ActionListener listener) { + public static void executeWithHeadersAsync( + Map headers, + String origin, + Client client, + ActionType action, + Request request, + ActionListener listener + ) { final Map filteredHeaders = filterSecurityHeaders(headers); final ThreadContext threadContext = client.threadPool().getThreadContext(); // No headers (e.g. security not installed/in use) so execute as origin diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/DataTiersFeatureSetUsage.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/DataTiersFeatureSetUsage.java index 6acbe58f81822..d4bfd43aec44f 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/DataTiersFeatureSetUsage.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/DataTiersFeatureSetUsage.java @@ -77,9 +77,9 @@ public boolean equals(Object obj) { return false; } DataTiersFeatureSetUsage other = (DataTiersFeatureSetUsage) obj; - return Objects.equals(available, other.available) && - Objects.equals(enabled, other.enabled) && - Objects.equals(tierStats, other.tierStats); + return Objects.equals(available, other.available) + && Objects.equals(enabled, other.enabled) + && Objects.equals(tierStats, other.tierStats); } @Override @@ -116,8 +116,17 @@ public TierSpecificStats(StreamInput in) throws IOException { this.primaryShardBytesMAD = in.readVLong(); } - public TierSpecificStats(int nodeCount, int indexCount, int totalShardCount, int primaryShardCount, long docCount, - long totalByteCount, long primaryByteCount, long primaryByteCountMedian, long primaryShardBytesMAD) { + public TierSpecificStats( + int nodeCount, + int indexCount, + int totalShardCount, + int primaryShardCount, + long docCount, + long totalByteCount, + long primaryByteCount, + long primaryByteCountMedian, + long primaryShardBytesMAD + ) { this.nodeCount = nodeCount; this.indexCount = indexCount; this.totalShardCount = totalShardCount; @@ -152,20 +161,34 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.field("doc_count", docCount); builder.humanReadableField("total_size_bytes", "total_size", new ByteSizeValue(totalByteCount)); builder.humanReadableField("primary_size_bytes", "primary_size", new ByteSizeValue(primaryByteCount)); - builder.humanReadableField("primary_shard_size_avg_bytes", "primary_shard_size_avg", - new ByteSizeValue(primaryShardCount == 0 ? 0 : (primaryByteCount / primaryShardCount))); - builder.humanReadableField("primary_shard_size_median_bytes", "primary_shard_size_median", - new ByteSizeValue(primaryByteCountMedian)); - builder.humanReadableField("primary_shard_size_mad_bytes", "primary_shard_size_mad", - new ByteSizeValue(primaryShardBytesMAD)); + builder.humanReadableField( + "primary_shard_size_avg_bytes", + "primary_shard_size_avg", + new ByteSizeValue(primaryShardCount == 0 ? 0 : (primaryByteCount / primaryShardCount)) + ); + builder.humanReadableField( + "primary_shard_size_median_bytes", + "primary_shard_size_median", + new ByteSizeValue(primaryByteCountMedian) + ); + builder.humanReadableField("primary_shard_size_mad_bytes", "primary_shard_size_mad", new ByteSizeValue(primaryShardBytesMAD)); builder.endObject(); return builder; } @Override public int hashCode() { - return Objects.hash(this.nodeCount, this.indexCount, this.totalShardCount, this.primaryShardCount, this.totalByteCount, - this.primaryByteCount, this.docCount, this.primaryByteCountMedian, this.primaryShardBytesMAD); + return Objects.hash( + this.nodeCount, + this.indexCount, + this.totalShardCount, + this.primaryShardCount, + this.totalByteCount, + this.primaryByteCount, + this.docCount, + this.primaryByteCountMedian, + this.primaryShardBytesMAD + ); } @Override @@ -177,15 +200,15 @@ public boolean equals(Object obj) { return false; } TierSpecificStats other = (TierSpecificStats) obj; - return nodeCount == other.nodeCount && - indexCount == other.indexCount && - totalShardCount == other.totalShardCount && - primaryShardCount == other.primaryShardCount && - docCount == other.docCount && - totalByteCount == other.totalByteCount && - primaryByteCount == other.primaryByteCount && - primaryByteCountMedian == other.primaryByteCountMedian && - primaryShardBytesMAD == other.primaryShardBytesMAD; + return nodeCount == other.nodeCount + && indexCount == other.indexCount + && totalShardCount == other.totalShardCount + && primaryShardCount == other.primaryShardCount + && docCount == other.docCount + && totalByteCount == other.totalByteCount + && primaryByteCount == other.primaryByteCount + && primaryByteCountMedian == other.primaryByteCountMedian + && primaryShardBytesMAD == other.primaryShardBytesMAD; } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/DataTiersUsageTransportAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/DataTiersUsageTransportAction.java index 5f0a228051004..fd4f39875ec5b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/DataTiersUsageTransportAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/DataTiersUsageTransportAction.java @@ -49,18 +49,35 @@ public class DataTiersUsageTransportAction extends XPackUsageFeatureTransportAct private final Client client; @Inject - public DataTiersUsageTransportAction(TransportService transportService, ClusterService clusterService, - ThreadPool threadPool, ActionFilters actionFilters, - IndexNameExpressionResolver indexNameExpressionResolver, Client client) { - super(XPackUsageFeatureAction.DATA_TIERS.name(), transportService, clusterService, - threadPool, actionFilters, indexNameExpressionResolver); + public DataTiersUsageTransportAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver, + Client client + ) { + super( + XPackUsageFeatureAction.DATA_TIERS.name(), + transportService, + clusterService, + threadPool, + actionFilters, + indexNameExpressionResolver + ); this.client = client; } @Override - protected void masterOperation(Task task, XPackUsageRequest request, ClusterState state, - ActionListener listener) { - client.admin().cluster().prepareNodesStats() + protected void masterOperation( + Task task, + XPackUsageRequest request, + ClusterState state, + ActionListener listener + ) { + client.admin() + .cluster() + .prepareNodesStats() .all() .setIndices(CommonStatsFlags.ALL) .execute(ActionListener.wrap(nodesStatsResponse -> { @@ -71,8 +88,11 @@ protected void masterOperation(Task task, XPackUsageRequest request, ClusterStat Map indicesToTiers = tierIndices(indices); // Generate tier specific stats for the nodes and indices - Map tierSpecificStats = calculateStats(nodesStatsResponse.getNodes(), - indicesToTiers, routingNodes); + Map tierSpecificStats = calculateStats( + nodesStatsResponse.getNodes(), + indicesToTiers, + routingNodes + ); listener.onResponse(new XPackUsageFeatureResponse(new DataTiersFeatureSetUsage(tierSpecificStats))); }, listener::onFailure)); @@ -109,9 +129,11 @@ private static class TierStatsAccumulator { } // Visible for testing - static Map calculateStats(List nodesStats, - Map indexByTier, - RoutingNodes routingNodes) { + static Map calculateStats( + List nodesStats, + Map indexByTier, + RoutingNodes routingNodes + ) { Map statsAccumulators = new HashMap<>(); for (NodeStats nodeStats : nodesStats) { aggregateDataTierNodeCounts(nodeStats, statsAccumulators); @@ -128,7 +150,9 @@ static Map calculateStats(Li * Determine which data tiers this node belongs to (if any), and increment the node counts for those tiers. */ private static void aggregateDataTierNodeCounts(NodeStats nodeStats, Map tiersStats) { - nodeStats.getNode().getRoles().stream() + nodeStats.getNode() + .getRoles() + .stream() .map(DiscoveryNodeRole::roleName) .filter(DataTier::validTierName) .forEach(tier -> tiersStats.computeIfAbsent(tier, k -> new TierStatsAccumulator()).nodeCount++); @@ -137,8 +161,12 @@ private static void aggregateDataTierNodeCounts(NodeStats nodeStats, Map indexByTier, - Map accumulators) { + private static void aggregateDataTierIndexStats( + NodeStats nodeStats, + RoutingNodes routingNodes, + Map indexByTier, + Map accumulators + ) { final RoutingNode node = routingNodes.node(nodeStats.getNode().getId()); if (node != null) { StreamSupport.stream(node.spliterator(), false) @@ -151,8 +179,13 @@ private static void aggregateDataTierIndexStats(NodeStats nodeStats, RoutingNode /** * Determine which tier an index belongs in, then accumulate its stats into that tier's stats. */ - private static void classifyIndexAndCollectStats(Index index, NodeStats nodeStats, Map indexByTier, - RoutingNode node, Map accumulators) { + private static void classifyIndexAndCollectStats( + Index index, + NodeStats nodeStats, + Map indexByTier, + RoutingNode node, + Map accumulators + ) { // Look up which tier this index belongs to (its most preferred) String indexTier = indexByTier.get(index.getName()); if (indexTier != null) { @@ -194,9 +227,17 @@ private static void aggregateDataTierShardStats(NodeStats nodeStats, Index index private static DataTiersFeatureSetUsage.TierSpecificStats calculateFinalTierStats(TierStatsAccumulator accumulator) { long primaryShardSizeMedian = (long) accumulator.valueSketch.quantile(0.5); long primaryShardSizeMAD = computeMedianAbsoluteDeviation(accumulator.valueSketch); - return new DataTiersFeatureSetUsage.TierSpecificStats(accumulator.nodeCount, accumulator.indexNames.size(), - accumulator.totalShardCount, accumulator.primaryShardCount, accumulator.docCount, - accumulator.totalByteCount, accumulator.primaryByteCount, primaryShardSizeMedian, primaryShardSizeMAD); + return new DataTiersFeatureSetUsage.TierSpecificStats( + accumulator.nodeCount, + accumulator.indexNames.size(), + accumulator.totalShardCount, + accumulator.primaryShardCount, + accumulator.docCount, + accumulator.totalByteCount, + accumulator.primaryByteCount, + primaryShardSizeMedian, + primaryShardSizeMAD + ); } // Visible for testing diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/MigrateToDataStreamAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/MigrateToDataStreamAction.java index 797e1437d6927..c436e08376e5b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/MigrateToDataStreamAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/MigrateToDataStreamAction.java @@ -77,7 +77,7 @@ public int hashCode() { @Override public String[] indices() { - return new String[]{aliasName}; + return new String[] { aliasName }; } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackBuild.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackBuild.java index 4159d5a5063b3..1cea4c23bcca0 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackBuild.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackBuild.java @@ -6,8 +6,8 @@ */ package org.elasticsearch.xpack.core; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.core.PathUtils; +import org.elasticsearch.core.SuppressForbidden; import java.io.IOException; import java.net.URISyntaxException; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackClientPlugin.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackClientPlugin.java index d3ea820889529..9e9e7bc7d3243 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackClientPlugin.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackClientPlugin.java @@ -11,11 +11,9 @@ import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.NamedDiff; import org.elasticsearch.cluster.metadata.Metadata; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.license.DeleteLicenseAction; import org.elasticsearch.license.GetBasicStatusAction; import org.elasticsearch.license.GetLicenseAction; @@ -32,6 +30,8 @@ import org.elasticsearch.plugins.Plugin; import org.elasticsearch.rollup.RollupV2; import org.elasticsearch.tasks.Task; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xpack.cluster.action.MigrateToDataTiersAction; import org.elasticsearch.xpack.core.action.XPackInfoAction; import org.elasticsearch.xpack.core.action.XPackUsageAction; @@ -251,7 +251,7 @@ public List> getSettings() { // the only licensing one settings.add(Setting.groupSetting("license.", Setting.Property.NodeScope)); - //TODO split these settings up + // TODO split these settings up settings.addAll(XPackSettings.getAllSettings()); settings.add(LicenseService.SELF_GENERATED_LICENSE_TYPE); @@ -265,157 +265,159 @@ public List> getSettings() { @Override public List> getClientActions() { - List> actions = new ArrayList<>(Arrays.asList( - // graph - GraphExploreAction.INSTANCE, - // ML - GetJobsAction.INSTANCE, - GetJobsStatsAction.INSTANCE, - MlInfoAction.INSTANCE, - PutJobAction.INSTANCE, - UpdateJobAction.INSTANCE, - DeleteJobAction.INSTANCE, - OpenJobAction.INSTANCE, - GetFiltersAction.INSTANCE, - PutFilterAction.INSTANCE, - UpdateFilterAction.INSTANCE, - DeleteFilterAction.INSTANCE, - KillProcessAction.INSTANCE, - GetBucketsAction.INSTANCE, - GetInfluencersAction.INSTANCE, - GetOverallBucketsAction.INSTANCE, - GetRecordsAction.INSTANCE, - PostDataAction.INSTANCE, - CloseJobAction.INSTANCE, - FinalizeJobExecutionAction.INSTANCE, - FlushJobAction.INSTANCE, - ValidateDetectorAction.INSTANCE, - ValidateJobConfigAction.INSTANCE, - GetCategoriesAction.INSTANCE, - GetModelSnapshotsAction.INSTANCE, - RevertModelSnapshotAction.INSTANCE, - UpdateModelSnapshotAction.INSTANCE, - GetDatafeedsAction.INSTANCE, - GetDatafeedsStatsAction.INSTANCE, - PutDatafeedAction.INSTANCE, - UpdateDatafeedAction.INSTANCE, - DeleteDatafeedAction.INSTANCE, - PreviewDatafeedAction.INSTANCE, - StartDatafeedAction.INSTANCE, - StopDatafeedAction.INSTANCE, - IsolateDatafeedAction.INSTANCE, - DeleteModelSnapshotAction.INSTANCE, - UpdateProcessAction.INSTANCE, - DeleteExpiredDataAction.INSTANCE, - ForecastJobAction.INSTANCE, - DeleteForecastAction.INSTANCE, - GetCalendarsAction.INSTANCE, - PutCalendarAction.INSTANCE, - DeleteCalendarAction.INSTANCE, - DeleteCalendarEventAction.INSTANCE, - UpdateCalendarJobAction.INSTANCE, - GetCalendarEventsAction.INSTANCE, - PostCalendarEventsAction.INSTANCE, - PersistJobAction.INSTANCE, - SetUpgradeModeAction.INSTANCE, - PutDataFrameAnalyticsAction.INSTANCE, - GetDataFrameAnalyticsAction.INSTANCE, - GetDataFrameAnalyticsStatsAction.INSTANCE, - UpdateDataFrameAnalyticsAction.INSTANCE, - DeleteDataFrameAnalyticsAction.INSTANCE, - StartDataFrameAnalyticsAction.INSTANCE, - EvaluateDataFrameAction.INSTANCE, - ExplainDataFrameAnalyticsAction.INSTANCE, - InternalInferModelAction.INSTANCE, - GetTrainedModelsAction.INSTANCE, - DeleteTrainedModelAction.INSTANCE, - GetTrainedModelsStatsAction.INSTANCE, - PutTrainedModelAction.INSTANCE, - // security - ClearRealmCacheAction.INSTANCE, - ClearRolesCacheAction.INSTANCE, - GetUsersAction.INSTANCE, - PutUserAction.INSTANCE, - DeleteUserAction.INSTANCE, - GetRolesAction.INSTANCE, - PutRoleAction.INSTANCE, - DeleteRoleAction.INSTANCE, - ChangePasswordAction.INSTANCE, - AuthenticateAction.INSTANCE, - SetEnabledAction.INSTANCE, - HasPrivilegesAction.INSTANCE, - GetRoleMappingsAction.INSTANCE, - PutRoleMappingAction.INSTANCE, - DeleteRoleMappingAction.INSTANCE, - CreateTokenAction.INSTANCE, - InvalidateTokenAction.INSTANCE, - GetCertificateInfoAction.INSTANCE, - RefreshTokenAction.INSTANCE, - CreateApiKeyAction.INSTANCE, - InvalidateApiKeyAction.INSTANCE, - GetApiKeyAction.INSTANCE, - // watcher - PutWatchAction.INSTANCE, - DeleteWatchAction.INSTANCE, - GetWatchAction.INSTANCE, - WatcherStatsAction.INSTANCE, - AckWatchAction.INSTANCE, - ActivateWatchAction.INSTANCE, - WatcherServiceAction.INSTANCE, - ExecuteWatchAction.INSTANCE, - // license - PutLicenseAction.INSTANCE, - GetLicenseAction.INSTANCE, - DeleteLicenseAction.INSTANCE, - PostStartTrialAction.INSTANCE, - GetTrialStatusAction.INSTANCE, - PostStartBasicAction.INSTANCE, - GetBasicStatusAction.INSTANCE, - // x-pack - XPackInfoAction.INSTANCE, - XPackUsageAction.INSTANCE, - // rollup - RollupSearchAction.INSTANCE, - PutRollupJobAction.INSTANCE, - StartRollupJobAction.INSTANCE, - StopRollupJobAction.INSTANCE, - DeleteRollupJobAction.INSTANCE, - GetRollupJobsAction.INSTANCE, - GetRollupCapsAction.INSTANCE, - // ILM - DeleteLifecycleAction.INSTANCE, - GetLifecycleAction.INSTANCE, - PutLifecycleAction.INSTANCE, - ExplainLifecycleAction.INSTANCE, - RemoveIndexLifecyclePolicyAction.INSTANCE, - MoveToStepAction.INSTANCE, - RetryAction.INSTANCE, - PutSnapshotLifecycleAction.INSTANCE, - GetSnapshotLifecycleAction.INSTANCE, - DeleteSnapshotLifecycleAction.INSTANCE, - ExecuteSnapshotLifecycleAction.INSTANCE, - GetSnapshotLifecycleStatsAction.INSTANCE, - MigrateToDataTiersAction.INSTANCE, + List> actions = new ArrayList<>( + Arrays.asList( + // graph + GraphExploreAction.INSTANCE, + // ML + GetJobsAction.INSTANCE, + GetJobsStatsAction.INSTANCE, + MlInfoAction.INSTANCE, + PutJobAction.INSTANCE, + UpdateJobAction.INSTANCE, + DeleteJobAction.INSTANCE, + OpenJobAction.INSTANCE, + GetFiltersAction.INSTANCE, + PutFilterAction.INSTANCE, + UpdateFilterAction.INSTANCE, + DeleteFilterAction.INSTANCE, + KillProcessAction.INSTANCE, + GetBucketsAction.INSTANCE, + GetInfluencersAction.INSTANCE, + GetOverallBucketsAction.INSTANCE, + GetRecordsAction.INSTANCE, + PostDataAction.INSTANCE, + CloseJobAction.INSTANCE, + FinalizeJobExecutionAction.INSTANCE, + FlushJobAction.INSTANCE, + ValidateDetectorAction.INSTANCE, + ValidateJobConfigAction.INSTANCE, + GetCategoriesAction.INSTANCE, + GetModelSnapshotsAction.INSTANCE, + RevertModelSnapshotAction.INSTANCE, + UpdateModelSnapshotAction.INSTANCE, + GetDatafeedsAction.INSTANCE, + GetDatafeedsStatsAction.INSTANCE, + PutDatafeedAction.INSTANCE, + UpdateDatafeedAction.INSTANCE, + DeleteDatafeedAction.INSTANCE, + PreviewDatafeedAction.INSTANCE, + StartDatafeedAction.INSTANCE, + StopDatafeedAction.INSTANCE, + IsolateDatafeedAction.INSTANCE, + DeleteModelSnapshotAction.INSTANCE, + UpdateProcessAction.INSTANCE, + DeleteExpiredDataAction.INSTANCE, + ForecastJobAction.INSTANCE, + DeleteForecastAction.INSTANCE, + GetCalendarsAction.INSTANCE, + PutCalendarAction.INSTANCE, + DeleteCalendarAction.INSTANCE, + DeleteCalendarEventAction.INSTANCE, + UpdateCalendarJobAction.INSTANCE, + GetCalendarEventsAction.INSTANCE, + PostCalendarEventsAction.INSTANCE, + PersistJobAction.INSTANCE, + SetUpgradeModeAction.INSTANCE, + PutDataFrameAnalyticsAction.INSTANCE, + GetDataFrameAnalyticsAction.INSTANCE, + GetDataFrameAnalyticsStatsAction.INSTANCE, + UpdateDataFrameAnalyticsAction.INSTANCE, + DeleteDataFrameAnalyticsAction.INSTANCE, + StartDataFrameAnalyticsAction.INSTANCE, + EvaluateDataFrameAction.INSTANCE, + ExplainDataFrameAnalyticsAction.INSTANCE, + InternalInferModelAction.INSTANCE, + GetTrainedModelsAction.INSTANCE, + DeleteTrainedModelAction.INSTANCE, + GetTrainedModelsStatsAction.INSTANCE, + PutTrainedModelAction.INSTANCE, + // security + ClearRealmCacheAction.INSTANCE, + ClearRolesCacheAction.INSTANCE, + GetUsersAction.INSTANCE, + PutUserAction.INSTANCE, + DeleteUserAction.INSTANCE, + GetRolesAction.INSTANCE, + PutRoleAction.INSTANCE, + DeleteRoleAction.INSTANCE, + ChangePasswordAction.INSTANCE, + AuthenticateAction.INSTANCE, + SetEnabledAction.INSTANCE, + HasPrivilegesAction.INSTANCE, + GetRoleMappingsAction.INSTANCE, + PutRoleMappingAction.INSTANCE, + DeleteRoleMappingAction.INSTANCE, + CreateTokenAction.INSTANCE, + InvalidateTokenAction.INSTANCE, + GetCertificateInfoAction.INSTANCE, + RefreshTokenAction.INSTANCE, + CreateApiKeyAction.INSTANCE, + InvalidateApiKeyAction.INSTANCE, + GetApiKeyAction.INSTANCE, + // watcher + PutWatchAction.INSTANCE, + DeleteWatchAction.INSTANCE, + GetWatchAction.INSTANCE, + WatcherStatsAction.INSTANCE, + AckWatchAction.INSTANCE, + ActivateWatchAction.INSTANCE, + WatcherServiceAction.INSTANCE, + ExecuteWatchAction.INSTANCE, + // license + PutLicenseAction.INSTANCE, + GetLicenseAction.INSTANCE, + DeleteLicenseAction.INSTANCE, + PostStartTrialAction.INSTANCE, + GetTrialStatusAction.INSTANCE, + PostStartBasicAction.INSTANCE, + GetBasicStatusAction.INSTANCE, + // x-pack + XPackInfoAction.INSTANCE, + XPackUsageAction.INSTANCE, + // rollup + RollupSearchAction.INSTANCE, + PutRollupJobAction.INSTANCE, + StartRollupJobAction.INSTANCE, + StopRollupJobAction.INSTANCE, + DeleteRollupJobAction.INSTANCE, + GetRollupJobsAction.INSTANCE, + GetRollupCapsAction.INSTANCE, + // ILM + DeleteLifecycleAction.INSTANCE, + GetLifecycleAction.INSTANCE, + PutLifecycleAction.INSTANCE, + ExplainLifecycleAction.INSTANCE, + RemoveIndexLifecyclePolicyAction.INSTANCE, + MoveToStepAction.INSTANCE, + RetryAction.INSTANCE, + PutSnapshotLifecycleAction.INSTANCE, + GetSnapshotLifecycleAction.INSTANCE, + DeleteSnapshotLifecycleAction.INSTANCE, + ExecuteSnapshotLifecycleAction.INSTANCE, + GetSnapshotLifecycleStatsAction.INSTANCE, + MigrateToDataTiersAction.INSTANCE, - // Freeze - FreezeIndexAction.INSTANCE, - // Data Frame - PutTransformAction.INSTANCE, - StartTransformAction.INSTANCE, - StopTransformAction.INSTANCE, - DeleteTransformAction.INSTANCE, - GetTransformAction.INSTANCE, - GetTransformStatsAction.INSTANCE, - PreviewTransformAction.INSTANCE, - // Async Search - SubmitAsyncSearchAction.INSTANCE, - GetAsyncSearchAction.INSTANCE, - DeleteAsyncResultAction.INSTANCE, - // Text Structure - FindStructureAction.INSTANCE, - // Terms enum API - TermsEnumAction.INSTANCE - )); + // Freeze + FreezeIndexAction.INSTANCE, + // Data Frame + PutTransformAction.INSTANCE, + StartTransformAction.INSTANCE, + StopTransformAction.INSTANCE, + DeleteTransformAction.INSTANCE, + GetTransformAction.INSTANCE, + GetTransformStatsAction.INSTANCE, + PreviewTransformAction.INSTANCE, + // Async Search + SubmitAsyncSearchAction.INSTANCE, + GetAsyncSearchAction.INSTANCE, + DeleteAsyncResultAction.INSTANCE, + // Text Structure + FindStructureAction.INSTANCE, + // Terms enum API + TermsEnumAction.INSTANCE + ) + ); // rollupV2 if (RollupV2.isEnabled()) { @@ -428,112 +430,141 @@ public List> getClientActions() { @Override public List getNamedWriteables() { - List namedWriteables = new ArrayList<>(Arrays.asList( - // graph - new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.GRAPH, GraphFeatureSetUsage::new), - // logstash - new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.LOGSTASH, LogstashFeatureSetUsage::new), - // ML - new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.MACHINE_LEARNING, MachineLearningFeatureSetUsage::new), - // monitoring - new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.MONITORING, MonitoringFeatureSetUsage::new), - // security - new NamedWriteableRegistry.Entry(ClusterState.Custom.class, TokenMetadata.TYPE, TokenMetadata::new), - new NamedWriteableRegistry.Entry(NamedDiff.class, TokenMetadata.TYPE, TokenMetadata::readDiffFrom), - new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.SECURITY, SecurityFeatureSetUsage::new), - // security : conditional privileges - new NamedWriteableRegistry.Entry(ConfigurableClusterPrivilege.class, - ConfigurableClusterPrivileges.ManageApplicationPrivileges.WRITEABLE_NAME, - ConfigurableClusterPrivileges.ManageApplicationPrivileges::createFrom), - // security : role-mappings - new NamedWriteableRegistry.Entry(RoleMapperExpression.class, AllExpression.NAME, AllExpression::new), - new NamedWriteableRegistry.Entry(RoleMapperExpression.class, AnyExpression.NAME, AnyExpression::new), - new NamedWriteableRegistry.Entry(RoleMapperExpression.class, FieldExpression.NAME, FieldExpression::new), - new NamedWriteableRegistry.Entry(RoleMapperExpression.class, ExceptExpression.NAME, ExceptExpression::new), - // eql - new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.EQL, EqlFeatureSetUsage::new), - // sql - new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.SQL, SqlFeatureSetUsage::new), - // watcher - new NamedWriteableRegistry.Entry(Metadata.Custom.class, WatcherMetadata.TYPE, WatcherMetadata::new), - new NamedWriteableRegistry.Entry(NamedDiff.class, WatcherMetadata.TYPE, WatcherMetadata::readDiffFrom), - new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.WATCHER, WatcherFeatureSetUsage::new), - // licensing - new NamedWriteableRegistry.Entry(Metadata.Custom.class, LicensesMetadata.TYPE, LicensesMetadata::new), - new NamedWriteableRegistry.Entry(NamedDiff.class, LicensesMetadata.TYPE, LicensesMetadata::readDiffFrom), - // rollup - new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.ROLLUP, RollupFeatureSetUsage::new), - new NamedWriteableRegistry.Entry(PersistentTaskParams.class, RollupJob.NAME, RollupJob::new), - new NamedWriteableRegistry.Entry(Task.Status.class, RollupJobStatus.NAME, RollupJobStatus::new), - new NamedWriteableRegistry.Entry(PersistentTaskState.class, RollupJobStatus.NAME, RollupJobStatus::new), - // ccr - new NamedWriteableRegistry.Entry(AutoFollowMetadata.class, AutoFollowMetadata.TYPE, AutoFollowMetadata::new), - new NamedWriteableRegistry.Entry(Metadata.Custom.class, AutoFollowMetadata.TYPE, AutoFollowMetadata::new), - new NamedWriteableRegistry.Entry(NamedDiff.class, AutoFollowMetadata.TYPE, - in -> AutoFollowMetadata.readDiffFrom(Metadata.Custom.class, AutoFollowMetadata.TYPE, in)), - // ILM - new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.INDEX_LIFECYCLE, - IndexLifecycleFeatureSetUsage::new), - // SLM - new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.SNAPSHOT_LIFECYCLE, - SLMFeatureSetUsage::new), - // ILM - Custom Metadata - new NamedWriteableRegistry.Entry(Metadata.Custom.class, IndexLifecycleMetadata.TYPE, IndexLifecycleMetadata::new), - new NamedWriteableRegistry.Entry(NamedDiff.class, IndexLifecycleMetadata.TYPE, - IndexLifecycleMetadata.IndexLifecycleMetadataDiff::new), - new NamedWriteableRegistry.Entry(Metadata.Custom.class, SnapshotLifecycleMetadata.TYPE, SnapshotLifecycleMetadata::new), - new NamedWriteableRegistry.Entry(NamedDiff.class, SnapshotLifecycleMetadata.TYPE, - SnapshotLifecycleMetadata.SnapshotLifecycleMetadataDiff::new), - // ILM - LifecycleTypes - new NamedWriteableRegistry.Entry(LifecycleType.class, TimeseriesLifecycleType.TYPE, - (in) -> TimeseriesLifecycleType.INSTANCE), - // ILM - Lifecycle Actions - new NamedWriteableRegistry.Entry(LifecycleAction.class, AllocateAction.NAME, AllocateAction::new), - new NamedWriteableRegistry.Entry(LifecycleAction.class, ForceMergeAction.NAME, ForceMergeAction::new), - new NamedWriteableRegistry.Entry(LifecycleAction.class, ReadOnlyAction.NAME, ReadOnlyAction::new), - new NamedWriteableRegistry.Entry(LifecycleAction.class, RolloverAction.NAME, RolloverAction::new), - new NamedWriteableRegistry.Entry(LifecycleAction.class, ShrinkAction.NAME, ShrinkAction::new), - new NamedWriteableRegistry.Entry(LifecycleAction.class, DeleteAction.NAME, DeleteAction::new), - new NamedWriteableRegistry.Entry(LifecycleAction.class, FreezeAction.NAME, FreezeAction::new), - new NamedWriteableRegistry.Entry(LifecycleAction.class, SetPriorityAction.NAME, SetPriorityAction::new), - new NamedWriteableRegistry.Entry(LifecycleAction.class, UnfollowAction.NAME, UnfollowAction::new), - new NamedWriteableRegistry.Entry(LifecycleAction.class, WaitForSnapshotAction.NAME, WaitForSnapshotAction::new), - new NamedWriteableRegistry.Entry(LifecycleAction.class, SearchableSnapshotAction.NAME, SearchableSnapshotAction::new), - new NamedWriteableRegistry.Entry(LifecycleAction.class, MigrateAction.NAME, MigrateAction::new), - // Transforms - new NamedWriteableRegistry.Entry(Metadata.Custom.class, TransformMetadata.TYPE, TransformMetadata::new), - new NamedWriteableRegistry.Entry(NamedDiff.class, TransformMetadata.TYPE, TransformMetadata.TransformMetadataDiff::new), - new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.TRANSFORM, TransformFeatureSetUsage::new), - new NamedWriteableRegistry.Entry(PersistentTaskParams.class, TransformField.TASK_NAME, TransformTaskParams::new), - new NamedWriteableRegistry.Entry(Task.Status.class, TransformField.TASK_NAME, TransformState::new), - new NamedWriteableRegistry.Entry(PersistentTaskState.class, TransformField.TASK_NAME, TransformState::new), - new NamedWriteableRegistry.Entry(SyncConfig.class, TransformField.TIME.getPreferredName(), TimeSyncConfig::new), - new NamedWriteableRegistry.Entry( - RetentionPolicyConfig.class, - TransformField.TIME.getPreferredName(), - TimeRetentionPolicyConfig::new - ), - // Voting Only Node - new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.VOTING_ONLY, VotingOnlyNodeFeatureSetUsage::new), - // Frozen indices - new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.FROZEN_INDICES, FrozenIndicesFeatureSetUsage::new), - // Spatial - new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.SPATIAL, SpatialFeatureSetUsage::new), - // Analytics - new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.ANALYTICS, AnalyticsFeatureSetUsage::new), - // Aggregate metric field type - new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.AGGREGATE_METRIC, AggregateMetricFeatureSetUsage::new), - // Enrich - new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.ENRICH, EnrichFeatureSetUsage::new), - new NamedWriteableRegistry.Entry(Task.Status.class, ExecuteEnrichPolicyStatus.NAME, ExecuteEnrichPolicyStatus::new), - // Searchable snapshots - new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.SEARCHABLE_SNAPSHOTS, - SearchableSnapshotFeatureSetUsage::new), - // Data Streams - new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.DATA_STREAMS, DataStreamFeatureSetUsage::new), - // Data Tiers - new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.DATA_TIERS, DataTiersFeatureSetUsage::new) - )); + List namedWriteables = new ArrayList<>( + Arrays.asList( + // graph + new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.GRAPH, GraphFeatureSetUsage::new), + // logstash + new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.LOGSTASH, LogstashFeatureSetUsage::new), + // ML + new NamedWriteableRegistry.Entry( + XPackFeatureSet.Usage.class, + XPackField.MACHINE_LEARNING, + MachineLearningFeatureSetUsage::new + ), + // monitoring + new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.MONITORING, MonitoringFeatureSetUsage::new), + // security + new NamedWriteableRegistry.Entry(ClusterState.Custom.class, TokenMetadata.TYPE, TokenMetadata::new), + new NamedWriteableRegistry.Entry(NamedDiff.class, TokenMetadata.TYPE, TokenMetadata::readDiffFrom), + new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.SECURITY, SecurityFeatureSetUsage::new), + // security : conditional privileges + new NamedWriteableRegistry.Entry( + ConfigurableClusterPrivilege.class, + ConfigurableClusterPrivileges.ManageApplicationPrivileges.WRITEABLE_NAME, + ConfigurableClusterPrivileges.ManageApplicationPrivileges::createFrom + ), + // security : role-mappings + new NamedWriteableRegistry.Entry(RoleMapperExpression.class, AllExpression.NAME, AllExpression::new), + new NamedWriteableRegistry.Entry(RoleMapperExpression.class, AnyExpression.NAME, AnyExpression::new), + new NamedWriteableRegistry.Entry(RoleMapperExpression.class, FieldExpression.NAME, FieldExpression::new), + new NamedWriteableRegistry.Entry(RoleMapperExpression.class, ExceptExpression.NAME, ExceptExpression::new), + // eql + new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.EQL, EqlFeatureSetUsage::new), + // sql + new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.SQL, SqlFeatureSetUsage::new), + // watcher + new NamedWriteableRegistry.Entry(Metadata.Custom.class, WatcherMetadata.TYPE, WatcherMetadata::new), + new NamedWriteableRegistry.Entry(NamedDiff.class, WatcherMetadata.TYPE, WatcherMetadata::readDiffFrom), + new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.WATCHER, WatcherFeatureSetUsage::new), + // licensing + new NamedWriteableRegistry.Entry(Metadata.Custom.class, LicensesMetadata.TYPE, LicensesMetadata::new), + new NamedWriteableRegistry.Entry(NamedDiff.class, LicensesMetadata.TYPE, LicensesMetadata::readDiffFrom), + // rollup + new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.ROLLUP, RollupFeatureSetUsage::new), + new NamedWriteableRegistry.Entry(PersistentTaskParams.class, RollupJob.NAME, RollupJob::new), + new NamedWriteableRegistry.Entry(Task.Status.class, RollupJobStatus.NAME, RollupJobStatus::new), + new NamedWriteableRegistry.Entry(PersistentTaskState.class, RollupJobStatus.NAME, RollupJobStatus::new), + // ccr + new NamedWriteableRegistry.Entry(AutoFollowMetadata.class, AutoFollowMetadata.TYPE, AutoFollowMetadata::new), + new NamedWriteableRegistry.Entry(Metadata.Custom.class, AutoFollowMetadata.TYPE, AutoFollowMetadata::new), + new NamedWriteableRegistry.Entry( + NamedDiff.class, + AutoFollowMetadata.TYPE, + in -> AutoFollowMetadata.readDiffFrom(Metadata.Custom.class, AutoFollowMetadata.TYPE, in) + ), + // ILM + new NamedWriteableRegistry.Entry( + XPackFeatureSet.Usage.class, + XPackField.INDEX_LIFECYCLE, + IndexLifecycleFeatureSetUsage::new + ), + // SLM + new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.SNAPSHOT_LIFECYCLE, SLMFeatureSetUsage::new), + // ILM - Custom Metadata + new NamedWriteableRegistry.Entry(Metadata.Custom.class, IndexLifecycleMetadata.TYPE, IndexLifecycleMetadata::new), + new NamedWriteableRegistry.Entry( + NamedDiff.class, + IndexLifecycleMetadata.TYPE, + IndexLifecycleMetadata.IndexLifecycleMetadataDiff::new + ), + new NamedWriteableRegistry.Entry(Metadata.Custom.class, SnapshotLifecycleMetadata.TYPE, SnapshotLifecycleMetadata::new), + new NamedWriteableRegistry.Entry( + NamedDiff.class, + SnapshotLifecycleMetadata.TYPE, + SnapshotLifecycleMetadata.SnapshotLifecycleMetadataDiff::new + ), + // ILM - LifecycleTypes + new NamedWriteableRegistry.Entry( + LifecycleType.class, + TimeseriesLifecycleType.TYPE, + (in) -> TimeseriesLifecycleType.INSTANCE + ), + // ILM - Lifecycle Actions + new NamedWriteableRegistry.Entry(LifecycleAction.class, AllocateAction.NAME, AllocateAction::new), + new NamedWriteableRegistry.Entry(LifecycleAction.class, ForceMergeAction.NAME, ForceMergeAction::new), + new NamedWriteableRegistry.Entry(LifecycleAction.class, ReadOnlyAction.NAME, ReadOnlyAction::new), + new NamedWriteableRegistry.Entry(LifecycleAction.class, RolloverAction.NAME, RolloverAction::new), + new NamedWriteableRegistry.Entry(LifecycleAction.class, ShrinkAction.NAME, ShrinkAction::new), + new NamedWriteableRegistry.Entry(LifecycleAction.class, DeleteAction.NAME, DeleteAction::new), + new NamedWriteableRegistry.Entry(LifecycleAction.class, FreezeAction.NAME, FreezeAction::new), + new NamedWriteableRegistry.Entry(LifecycleAction.class, SetPriorityAction.NAME, SetPriorityAction::new), + new NamedWriteableRegistry.Entry(LifecycleAction.class, UnfollowAction.NAME, UnfollowAction::new), + new NamedWriteableRegistry.Entry(LifecycleAction.class, WaitForSnapshotAction.NAME, WaitForSnapshotAction::new), + new NamedWriteableRegistry.Entry(LifecycleAction.class, SearchableSnapshotAction.NAME, SearchableSnapshotAction::new), + new NamedWriteableRegistry.Entry(LifecycleAction.class, MigrateAction.NAME, MigrateAction::new), + // Transforms + new NamedWriteableRegistry.Entry(Metadata.Custom.class, TransformMetadata.TYPE, TransformMetadata::new), + new NamedWriteableRegistry.Entry(NamedDiff.class, TransformMetadata.TYPE, TransformMetadata.TransformMetadataDiff::new), + new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.TRANSFORM, TransformFeatureSetUsage::new), + new NamedWriteableRegistry.Entry(PersistentTaskParams.class, TransformField.TASK_NAME, TransformTaskParams::new), + new NamedWriteableRegistry.Entry(Task.Status.class, TransformField.TASK_NAME, TransformState::new), + new NamedWriteableRegistry.Entry(PersistentTaskState.class, TransformField.TASK_NAME, TransformState::new), + new NamedWriteableRegistry.Entry(SyncConfig.class, TransformField.TIME.getPreferredName(), TimeSyncConfig::new), + new NamedWriteableRegistry.Entry( + RetentionPolicyConfig.class, + TransformField.TIME.getPreferredName(), + TimeRetentionPolicyConfig::new + ), + // Voting Only Node + new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.VOTING_ONLY, VotingOnlyNodeFeatureSetUsage::new), + // Frozen indices + new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.FROZEN_INDICES, FrozenIndicesFeatureSetUsage::new), + // Spatial + new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.SPATIAL, SpatialFeatureSetUsage::new), + // Analytics + new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.ANALYTICS, AnalyticsFeatureSetUsage::new), + // Aggregate metric field type + new NamedWriteableRegistry.Entry( + XPackFeatureSet.Usage.class, + XPackField.AGGREGATE_METRIC, + AggregateMetricFeatureSetUsage::new + ), + // Enrich + new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.ENRICH, EnrichFeatureSetUsage::new), + new NamedWriteableRegistry.Entry(Task.Status.class, ExecuteEnrichPolicyStatus.NAME, ExecuteEnrichPolicyStatus::new), + // Searchable snapshots + new NamedWriteableRegistry.Entry( + XPackFeatureSet.Usage.class, + XPackField.SEARCHABLE_SNAPSHOTS, + SearchableSnapshotFeatureSetUsage::new + ), + // Data Streams + new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.DATA_STREAMS, DataStreamFeatureSetUsage::new), + // Data Tiers + new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, XPackField.DATA_TIERS, DataTiersFeatureSetUsage::new) + ) + ); if (RollupV2.isEnabled()) { namedWriteables.add(new NamedWriteableRegistry.Entry(LifecycleAction.class, RollupILMAction.NAME, RollupILMAction::new)); @@ -545,45 +576,66 @@ public List getNamedWriteables() { @Override public List getNamedXContent() { return Arrays.asList( - // ML - Custom metadata - new NamedXContentRegistry.Entry(Metadata.Custom.class, new ParseField("ml"), - parser -> MlMetadata.LENIENT_PARSER.parse(parser, null).build()), - // ML - Persistent action requests - new NamedXContentRegistry.Entry(PersistentTaskParams.class, new ParseField(MlTasks.DATAFEED_TASK_NAME), - StartDatafeedAction.DatafeedParams::fromXContent), - new NamedXContentRegistry.Entry(PersistentTaskParams.class, new ParseField(MlTasks.JOB_TASK_NAME), - OpenJobAction.JobParams::fromXContent), - new NamedXContentRegistry.Entry(PersistentTaskParams.class, new ParseField(MlTasks.DATA_FRAME_ANALYTICS_TASK_NAME), - StartDataFrameAnalyticsAction.TaskParams::fromXContent), - // ML - Task states - new NamedXContentRegistry.Entry(PersistentTaskState.class, new ParseField(DatafeedState.NAME), DatafeedState::fromXContent), - new NamedXContentRegistry.Entry(PersistentTaskState.class, new ParseField(JobTaskState.NAME), JobTaskState::fromXContent), - new NamedXContentRegistry.Entry(PersistentTaskState.class, new ParseField(DataFrameAnalyticsTaskState.NAME), - DataFrameAnalyticsTaskState::fromXContent), - new NamedXContentRegistry.Entry(PersistentTaskState.class, new ParseField(SnapshotUpgradeTaskState.NAME), - SnapshotUpgradeTaskState::fromXContent), - // watcher - new NamedXContentRegistry.Entry(Metadata.Custom.class, new ParseField(WatcherMetadata.TYPE), - WatcherMetadata::fromXContent), - // licensing - new NamedXContentRegistry.Entry(Metadata.Custom.class, new ParseField(LicensesMetadata.TYPE), - LicensesMetadata::fromXContent), - //rollup - new NamedXContentRegistry.Entry(PersistentTaskParams.class, new ParseField(RollupField.TASK_NAME), - RollupJob::fromXContent), - new NamedXContentRegistry.Entry(Task.Status.class, new ParseField(RollupJobStatus.NAME), - RollupJobStatus::fromXContent), - new NamedXContentRegistry.Entry(PersistentTaskState.class, new ParseField(RollupJobStatus.NAME), - RollupJobStatus::fromXContent), + // ML - Custom metadata + new NamedXContentRegistry.Entry( + Metadata.Custom.class, + new ParseField("ml"), + parser -> MlMetadata.LENIENT_PARSER.parse(parser, null).build() + ), + // ML - Persistent action requests + new NamedXContentRegistry.Entry( + PersistentTaskParams.class, + new ParseField(MlTasks.DATAFEED_TASK_NAME), + StartDatafeedAction.DatafeedParams::fromXContent + ), + new NamedXContentRegistry.Entry( + PersistentTaskParams.class, + new ParseField(MlTasks.JOB_TASK_NAME), + OpenJobAction.JobParams::fromXContent + ), + new NamedXContentRegistry.Entry( + PersistentTaskParams.class, + new ParseField(MlTasks.DATA_FRAME_ANALYTICS_TASK_NAME), + StartDataFrameAnalyticsAction.TaskParams::fromXContent + ), + // ML - Task states + new NamedXContentRegistry.Entry(PersistentTaskState.class, new ParseField(DatafeedState.NAME), DatafeedState::fromXContent), + new NamedXContentRegistry.Entry(PersistentTaskState.class, new ParseField(JobTaskState.NAME), JobTaskState::fromXContent), + new NamedXContentRegistry.Entry( + PersistentTaskState.class, + new ParseField(DataFrameAnalyticsTaskState.NAME), + DataFrameAnalyticsTaskState::fromXContent + ), + new NamedXContentRegistry.Entry( + PersistentTaskState.class, + new ParseField(SnapshotUpgradeTaskState.NAME), + SnapshotUpgradeTaskState::fromXContent + ), + // watcher + new NamedXContentRegistry.Entry(Metadata.Custom.class, new ParseField(WatcherMetadata.TYPE), WatcherMetadata::fromXContent), + // licensing + new NamedXContentRegistry.Entry(Metadata.Custom.class, new ParseField(LicensesMetadata.TYPE), LicensesMetadata::fromXContent), + // rollup + new NamedXContentRegistry.Entry(PersistentTaskParams.class, new ParseField(RollupField.TASK_NAME), RollupJob::fromXContent), + new NamedXContentRegistry.Entry(Task.Status.class, new ParseField(RollupJobStatus.NAME), RollupJobStatus::fromXContent), + new NamedXContentRegistry.Entry(PersistentTaskState.class, new ParseField(RollupJobStatus.NAME), RollupJobStatus::fromXContent), // Transforms - new NamedXContentRegistry.Entry(PersistentTaskParams.class, new ParseField(TransformField.TASK_NAME), - TransformTaskParams::fromXContent), - new NamedXContentRegistry.Entry(Task.Status.class, new ParseField(TransformField.TASK_NAME), - TransformState::fromXContent), - new NamedXContentRegistry.Entry(PersistentTaskState.class, new ParseField(TransformField.TASK_NAME), - TransformState::fromXContent), - new NamedXContentRegistry.Entry(Metadata.Custom.class, new ParseField(TransformMetadata.TYPE), - parser -> TransformMetadata.LENIENT_PARSER.parse(parser, null).build()) - ); + new NamedXContentRegistry.Entry( + PersistentTaskParams.class, + new ParseField(TransformField.TASK_NAME), + TransformTaskParams::fromXContent + ), + new NamedXContentRegistry.Entry(Task.Status.class, new ParseField(TransformField.TASK_NAME), TransformState::fromXContent), + new NamedXContentRegistry.Entry( + PersistentTaskState.class, + new ParseField(TransformField.TASK_NAME), + TransformState::fromXContent + ), + new NamedXContentRegistry.Entry( + Metadata.Custom.class, + new ParseField(TransformMetadata.TYPE), + parser -> TransformMetadata.LENIENT_PARSER.parse(parser, null).build() + ) + ); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackPlugin.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackPlugin.java index 1ff67b73b1d11..dd8924fde6b81 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackPlugin.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackPlugin.java @@ -150,7 +150,7 @@ public Void run() { } protected final Settings settings; - //private final Environment env; + // private final Environment env; protected final Licensing licensing; // These should not be directly accessed as they cannot be overridden in tests. Please use the getters so they can be overridden. private static final SetOnce licenseState = new SetOnce<>(); @@ -158,9 +158,7 @@ public Void run() { private static final SetOnce licenseService = new SetOnce<>(); private static final SetOnce epochMillisSupplier = new SetOnce<>(); - public XPackPlugin( - final Settings settings, - final Path configPath) { + public XPackPlugin(final Settings settings, final Path configPath) { super(settings); // FIXME: The settings might be changed after this (e.g. from "additionalSettings" method in other plugins) // We should only depend on the settings from the Environment object passed to createComponents @@ -176,13 +174,34 @@ protected Clock getClock() { return Clock.systemUTC(); } - protected SSLService getSslService() { return getSharedSslService(); } - protected LicenseService getLicenseService() { return getSharedLicenseService(); } - protected XPackLicenseState getLicenseState() { return getSharedLicenseState(); } - protected LongSupplier getEpochMillisSupplier() { return getSharedEpochMillisSupplier(); } - protected void setSslService(SSLService sslService) { XPackPlugin.sslService.set(sslService); } - protected void setLicenseService(LicenseService licenseService) { XPackPlugin.licenseService.set(licenseService); } - protected void setLicenseState(XPackLicenseState licenseState) { XPackPlugin.licenseState.set(licenseState); } + protected SSLService getSslService() { + return getSharedSslService(); + } + + protected LicenseService getLicenseService() { + return getSharedLicenseService(); + } + + protected XPackLicenseState getLicenseState() { + return getSharedLicenseState(); + } + + protected LongSupplier getEpochMillisSupplier() { + return getSharedEpochMillisSupplier(); + } + + protected void setSslService(SSLService sslService) { + XPackPlugin.sslService.set(sslService); + } + + protected void setLicenseService(LicenseService licenseService) { + XPackPlugin.licenseService.set(licenseService); + } + + protected void setLicenseState(XPackLicenseState licenseState) { + XPackPlugin.licenseState.set(licenseState); + } + protected void setEpochMillisSupplier(LongSupplier epochMillisSupplier) { XPackPlugin.epochMillisSupplier.set(epochMillisSupplier); } @@ -194,9 +213,18 @@ public static SSLService getSharedSslService() { } return ssl; } - public static LicenseService getSharedLicenseService() { return licenseService.get(); } - public static XPackLicenseState getSharedLicenseState() { return licenseState.get(); } - public static LongSupplier getSharedEpochMillisSupplier() { return epochMillisSupplier.get(); } + + public static LicenseService getSharedLicenseService() { + return licenseService.get(); + } + + public static XPackLicenseState getSharedLicenseState() { + return licenseState.get(); + } + + public static LongSupplier getSharedEpochMillisSupplier() { + return epochMillisSupplier.get(); + } /** * Checks if the cluster state allows this node to add x-pack metadata to the cluster state, @@ -242,11 +270,11 @@ public static List nodesNotReadyForXPackCustomMetadata(ClusterSta private static boolean alreadyContainsXPackCustomMetadata(ClusterState clusterState) { final Metadata metadata = clusterState.metadata(); - return metadata.custom(LicensesMetadata.TYPE) != null || - metadata.custom(MlMetadata.TYPE) != null || - metadata.custom(WatcherMetadata.TYPE) != null || - clusterState.custom(TokenMetadata.TYPE) != null || - metadata.custom(TransformMetadata.TYPE) != null; + return metadata.custom(LicensesMetadata.TYPE) != null + || metadata.custom(MlMetadata.TYPE) != null + || metadata.custom(WatcherMetadata.TYPE) != null + || clusterState.custom(TokenMetadata.TYPE) != null + || metadata.custom(TransformMetadata.TYPE) != null; } @Override @@ -265,17 +293,25 @@ public Settings additionalSettings() { } @Override - public Collection createComponents(Client client, ClusterService clusterService, ThreadPool threadPool, - ResourceWatcherService resourceWatcherService, ScriptService scriptService, - NamedXContentRegistry xContentRegistry, Environment environment, - NodeEnvironment nodeEnvironment, NamedWriteableRegistry namedWriteableRegistry, - IndexNameExpressionResolver expressionResolver, - Supplier repositoriesServiceSupplier) { + public Collection createComponents( + Client client, + ClusterService clusterService, + ThreadPool threadPool, + ResourceWatcherService resourceWatcherService, + ScriptService scriptService, + NamedXContentRegistry xContentRegistry, + Environment environment, + NodeEnvironment nodeEnvironment, + NamedWriteableRegistry namedWriteableRegistry, + IndexNameExpressionResolver expressionResolver, + Supplier repositoriesServiceSupplier + ) { List components = new ArrayList<>(); final SSLService sslService = createSSLService(environment, resourceWatcherService); - setLicenseService(new LicenseService(settings, threadPool, clusterService, getClock(), - environment, resourceWatcherService, getLicenseState())); + setLicenseService( + new LicenseService(settings, threadPool, clusterService, getClock(), environment, resourceWatcherService, getLicenseState()) + ); setEpochMillisSupplier(threadPool::absoluteTimeInMillis); @@ -327,16 +363,31 @@ public List getActionFilters() { } @Override - public List getRestHandlers(Settings settings, RestController restController, ClusterSettings clusterSettings, - IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter, IndexNameExpressionResolver indexNameExpressionResolver, - Supplier nodesInCluster) { + public List getRestHandlers( + Settings settings, + RestController restController, + ClusterSettings clusterSettings, + IndexScopedSettings indexScopedSettings, + SettingsFilter settingsFilter, + IndexNameExpressionResolver indexNameExpressionResolver, + Supplier nodesInCluster + ) { List handlers = new ArrayList<>(); handlers.add(new RestXPackInfoAction()); handlers.add(new RestXPackUsageAction()); handlers.add(new RestReloadAnalyzersAction()); handlers.add(new RestTermsEnumAction()); - handlers.addAll(licensing.getRestHandlers(settings, restController, clusterSettings, indexScopedSettings, settingsFilter, - indexNameExpressionResolver, nodesInCluster)); + handlers.addAll( + licensing.getRestHandlers( + settings, + restController, + clusterSettings, + indexScopedSettings, + settingsFilter, + indexNameExpressionResolver, + nodesInCluster + ) + ); return handlers; } @@ -351,13 +402,20 @@ public static Multibinder createFeatureSetMultiBinder(Binder bi } public static Path resolveConfigFile(Environment env, String name) { - Path config = env.configFile().resolve(name); + Path config = env.configFile().resolve(name); if (Files.exists(config) == false) { Path legacyConfig = env.configFile().resolve("x-pack").resolve(name); if (Files.exists(legacyConfig)) { - deprecationLogger.critical(DeprecationCategory.OTHER, "config_file_path", - "Config file [" + name + "] is in a deprecated location. Move from " + - legacyConfig.toString() + " to " + config.toString()); + deprecationLogger.critical( + DeprecationCategory.OTHER, + "config_file_path", + "Config file [" + + name + + "] is in a deprecated location. Move from " + + legacyConfig.toString() + + " to " + + config.toString() + ); return legacyConfig; } } @@ -365,16 +423,20 @@ public static Path resolveConfigFile(Environment env, String name) { } @Override - public Map getRepositories(Environment env, NamedXContentRegistry namedXContentRegistry, - ClusterService clusterService, BigArrays bigArrays, - RecoverySettings recoverySettings) { + public Map getRepositories( + Environment env, + NamedXContentRegistry namedXContentRegistry, + ClusterService clusterService, + BigArrays bigArrays, + RecoverySettings recoverySettings + ) { return Collections.singletonMap("source", SourceOnlySnapshotRepository.newRepositoryFactory()); } @Override public Optional getEngineFactory(IndexSettings indexSettings) { - if (indexSettings.getValue(SourceOnlySnapshotRepository.SOURCE_ONLY) && - SearchableSnapshotsSettings.isSearchableSnapshotStore(indexSettings.getSettings()) == false) { + if (indexSettings.getValue(SourceOnlySnapshotRepository.SOURCE_ONLY) + && SearchableSnapshotsSettings.isSearchableSnapshotStore(indexSettings.getSettings()) == false) { return Optional.of(SourceOnlySnapshotRepository.getEngineFactory()); } @@ -404,8 +466,7 @@ public Collection getAdditionalIndexSettingProviders() { */ private SSLService createSSLService(Environment environment, ResourceWatcherService resourceWatcherService) { final Map sslConfigurations = SSLService.getSSLConfigurations(environment); - final SSLConfigurationReloader reloader = - new SSLConfigurationReloader(resourceWatcherService, sslConfigurations.values()); + final SSLConfigurationReloader reloader = new SSLConfigurationReloader(resourceWatcherService, sslConfigurations.values()); final SSLService sslService = new SSLService(environment, sslConfigurations); reloader.setSSLService(sslService); setSslService(sslService); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackSettings.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackSettings.java index 83793feb410c4..571103fdc5c41 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackSettings.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackSettings.java @@ -24,6 +24,7 @@ import java.util.List; import java.util.Locale; import java.util.function.Function; + import javax.crypto.SecretKeyFactory; import javax.net.ssl.SSLContext; @@ -59,40 +60,67 @@ private XPackSettings() { public static final Setting GRAPH_ENABLED = Setting.boolSetting("xpack.graph.enabled", true, Setting.Property.NodeScope); /** Setting for enabling or disabling machine learning. Defaults to true. */ - public static final Setting MACHINE_LEARNING_ENABLED = Setting.boolSetting("xpack.ml.enabled", true, - Setting.Property.NodeScope); + public static final Setting MACHINE_LEARNING_ENABLED = Setting.boolSetting( + "xpack.ml.enabled", + true, + Setting.Property.NodeScope + ); /** Setting for enabling or disabling auditing. Defaults to false. */ - public static final Setting AUDIT_ENABLED = Setting.boolSetting("xpack.security.audit.enabled", false, - Setting.Property.NodeScope); + public static final Setting AUDIT_ENABLED = Setting.boolSetting( + "xpack.security.audit.enabled", + false, + Setting.Property.NodeScope + ); /** Setting for enabling or disabling document/field level security. Defaults to true. */ - public static final Setting DLS_FLS_ENABLED = Setting.boolSetting("xpack.security.dls_fls.enabled", true, - Setting.Property.NodeScope); + public static final Setting DLS_FLS_ENABLED = Setting.boolSetting( + "xpack.security.dls_fls.enabled", + true, + Setting.Property.NodeScope + ); /** Setting for enabling or disabling TLS. Defaults to false. */ - public static final Setting TRANSPORT_SSL_ENABLED = Setting.boolSetting("xpack.security.transport.ssl.enabled", false, - Property.NodeScope); + public static final Setting TRANSPORT_SSL_ENABLED = Setting.boolSetting( + "xpack.security.transport.ssl.enabled", + false, + Property.NodeScope + ); /** Setting for enabling or disabling http ssl. Defaults to false. */ - public static final Setting HTTP_SSL_ENABLED = Setting.boolSetting("xpack.security.http.ssl.enabled", false, - Setting.Property.NodeScope); + public static final Setting HTTP_SSL_ENABLED = Setting.boolSetting( + "xpack.security.http.ssl.enabled", + false, + Setting.Property.NodeScope + ); /** Setting for enabling or disabling the reserved realm. Defaults to true */ - public static final Setting RESERVED_REALM_ENABLED_SETTING = Setting.boolSetting("xpack.security.authc.reserved_realm.enabled", - true, Setting.Property.NodeScope); + public static final Setting RESERVED_REALM_ENABLED_SETTING = Setting.boolSetting( + "xpack.security.authc.reserved_realm.enabled", + true, + Setting.Property.NodeScope + ); /** Setting for enabling or disabling the token service. Defaults to the value of https being enabled */ - public static final Setting TOKEN_SERVICE_ENABLED_SETTING = - Setting.boolSetting("xpack.security.authc.token.enabled", XPackSettings.HTTP_SSL_ENABLED, Setting.Property.NodeScope); + public static final Setting TOKEN_SERVICE_ENABLED_SETTING = Setting.boolSetting( + "xpack.security.authc.token.enabled", + XPackSettings.HTTP_SSL_ENABLED, + Setting.Property.NodeScope + ); /** Setting for enabling or disabling the api key service. Defaults to true */ - public static final Setting API_KEY_SERVICE_ENABLED_SETTING = - Setting.boolSetting("xpack.security.authc.api_key.enabled", true, Setting.Property.NodeScope); + public static final Setting API_KEY_SERVICE_ENABLED_SETTING = Setting.boolSetting( + "xpack.security.authc.api_key.enabled", + true, + Setting.Property.NodeScope + ); /** Setting for enabling or disabling FIPS mode. Defaults to false */ - public static final Setting FIPS_MODE_ENABLED = - Setting.boolSetting("xpack.security.fips_mode.enabled", false, Property.NodeScope); + public static final Setting FIPS_MODE_ENABLED = Setting.boolSetting( + "xpack.security.fips_mode.enabled", + false, + Property.NodeScope + ); /** * Setting for enabling the enrollment process, ie the enroll APIs are enabled, and the initial cluster node generates and displays @@ -103,48 +131,78 @@ private XPackSettings() { * nodes. Nevertheless, we assumes that when {@code ENROLLMENT_ENABLED} is {@code true} the node MUST have been configured by said * start-up scripts (eg we don't support enrollment with general TLS certificates). */ - public static final Setting ENROLLMENT_ENABLED = - Setting.boolSetting("xpack.security.enrollment.enabled", false, Property.NodeScope); + public static final Setting ENROLLMENT_ENABLED = Setting.boolSetting( + "xpack.security.enrollment.enabled", + false, + Property.NodeScope + ); /** * Setting for enabling or disabling the TLS auto-configuration as well as credentials auto-generation for nodes, before starting for * the first time, and in the absence of other conflicting configurations. */ - public static final Setting SECURITY_AUTOCONFIGURATION_ENABLED = - Setting.boolSetting("xpack.security.autoconfiguration.enabled", true, Property.NodeScope); + public static final Setting SECURITY_AUTOCONFIGURATION_ENABLED = Setting.boolSetting( + "xpack.security.autoconfiguration.enabled", + true, + Property.NodeScope + ); /* * SSL settings. These are the settings that are specifically registered for SSL. Many are private as we do not explicitly use them * but instead parse based on a prefix (eg *.ssl.*) */ private static final List JDK11_CIPHERS = List.of( - "TLS_AES_256_GCM_SHA384", "TLS_AES_128_GCM_SHA256", // TLSv1.3 cipher has PFS, AEAD, hardware support - "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384", "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", // PFS, AEAD, hardware support - "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384", "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", // PFS, AEAD, hardware support - "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384", "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256", // PFS, hardware support - "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384", "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256", // PFS, hardware support - "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA", "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA", // PFS, hardware support - "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA", "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA", // PFS, hardware support - "TLS_RSA_WITH_AES_256_GCM_SHA384", "TLS_RSA_WITH_AES_128_GCM_SHA256", // AEAD, hardware support - "TLS_RSA_WITH_AES_256_CBC_SHA256", "TLS_RSA_WITH_AES_128_CBC_SHA256", // hardware support - "TLS_RSA_WITH_AES_256_CBC_SHA", "TLS_RSA_WITH_AES_128_CBC_SHA"); // hardware support + "TLS_AES_256_GCM_SHA384", + "TLS_AES_128_GCM_SHA256", // TLSv1.3 cipher has PFS, AEAD, hardware support + "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384", + "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", // PFS, AEAD, hardware support + "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384", + "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", // PFS, AEAD, hardware support + "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384", + "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256", // PFS, hardware support + "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384", + "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256", // PFS, hardware support + "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA", + "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA", // PFS, hardware support + "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA", + "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA", // PFS, hardware support + "TLS_RSA_WITH_AES_256_GCM_SHA384", + "TLS_RSA_WITH_AES_128_GCM_SHA256", // AEAD, hardware support + "TLS_RSA_WITH_AES_256_CBC_SHA256", + "TLS_RSA_WITH_AES_128_CBC_SHA256", // hardware support + "TLS_RSA_WITH_AES_256_CBC_SHA", + "TLS_RSA_WITH_AES_128_CBC_SHA" + ); // hardware support private static final List JDK12_CIPHERS = List.of( - "TLS_AES_256_GCM_SHA384", "TLS_AES_128_GCM_SHA256", // TLSv1.3 cipher has PFS, AEAD, hardware support + "TLS_AES_256_GCM_SHA384", + "TLS_AES_128_GCM_SHA256", // TLSv1.3 cipher has PFS, AEAD, hardware support "TLS_CHACHA20_POLY1305_SHA256", // TLSv1.3 cipher has PFS, AEAD - "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384", "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", // PFS, AEAD, hardware support - "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384", "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", // PFS, AEAD, hardware support - "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256", "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256", // PFS, AEAD - "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384", "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256", // PFS, hardware support - "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384", "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256", // PFS, hardware support - "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA", "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA", // PFS, hardware support - "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA", "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA", // PFS, hardware support - "TLS_RSA_WITH_AES_256_GCM_SHA384", "TLS_RSA_WITH_AES_128_GCM_SHA256", // AEAD, hardware support - "TLS_RSA_WITH_AES_256_CBC_SHA256", "TLS_RSA_WITH_AES_128_CBC_SHA256", // hardware support - "TLS_RSA_WITH_AES_256_CBC_SHA", "TLS_RSA_WITH_AES_128_CBC_SHA"); // hardware support - - public static final List DEFAULT_CIPHERS = - JavaVersion.current().compareTo(JavaVersion.parse("12")) > -1 ? JDK12_CIPHERS : JDK11_CIPHERS; + "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384", + "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", // PFS, AEAD, hardware support + "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384", + "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", // PFS, AEAD, hardware support + "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256", + "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256", // PFS, AEAD + "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384", + "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256", // PFS, hardware support + "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384", + "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256", // PFS, hardware support + "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA", + "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA", // PFS, hardware support + "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA", + "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA", // PFS, hardware support + "TLS_RSA_WITH_AES_256_GCM_SHA384", + "TLS_RSA_WITH_AES_128_GCM_SHA256", // AEAD, hardware support + "TLS_RSA_WITH_AES_256_CBC_SHA256", + "TLS_RSA_WITH_AES_128_CBC_SHA256", // hardware support + "TLS_RSA_WITH_AES_256_CBC_SHA", + "TLS_RSA_WITH_AES_128_CBC_SHA" + ); // hardware support + + public static final List DEFAULT_CIPHERS = JavaVersion.current().compareTo(JavaVersion.parse("12")) > -1 + ? JDK12_CIPHERS + : JDK11_CIPHERS; /* * Do not allow insecure hashing algorithms to be used for password hashing @@ -161,18 +219,23 @@ private XPackSettings() { Function.identity(), v -> { if (Hasher.getAvailableAlgoStoredHash().contains(v.toLowerCase(Locale.ROOT)) == false) { - throw new IllegalArgumentException("Invalid algorithm: " + v + ". Valid values for password hashing are " + - Hasher.getAvailableAlgoStoredHash().toString()); + throw new IllegalArgumentException( + "Invalid algorithm: " + v + ". Valid values for password hashing are " + Hasher.getAvailableAlgoStoredHash().toString() + ); } else if (v.regionMatches(true, 0, "pbkdf2", 0, "pbkdf2".length())) { try { SecretKeyFactory.getInstance("PBKDF2withHMACSHA512"); } catch (NoSuchAlgorithmException e) { throw new IllegalArgumentException( - "Support for PBKDF2WithHMACSHA512 must be available in order to use any of the " + - "PBKDF2 algorithms for the [xpack.security.authc.password_hashing.algorithm] setting.", e); + "Support for PBKDF2WithHMACSHA512 must be available in order to use any of the " + + "PBKDF2 algorithms for the [xpack.security.authc.password_hashing.algorithm] setting.", + e + ); } } - }, Property.NodeScope); + }, + Property.NodeScope + ); // TODO: This setting of hashing algorithm can share code with the one for password when pbkdf2_stretch is the default for both public static final Setting SERVICE_TOKEN_HASHING_ALGORITHM = new Setting<>( @@ -181,18 +244,23 @@ private XPackSettings() { Function.identity(), v -> { if (Hasher.getAvailableAlgoStoredHash().contains(v.toLowerCase(Locale.ROOT)) == false) { - throw new IllegalArgumentException("Invalid algorithm: " + v + ". Valid values for password hashing are " + - Hasher.getAvailableAlgoStoredHash().toString()); + throw new IllegalArgumentException( + "Invalid algorithm: " + v + ". Valid values for password hashing are " + Hasher.getAvailableAlgoStoredHash().toString() + ); } else if (v.regionMatches(true, 0, "pbkdf2", 0, "pbkdf2".length())) { try { SecretKeyFactory.getInstance("PBKDF2withHMACSHA512"); } catch (NoSuchAlgorithmException e) { throw new IllegalArgumentException( - "Support for PBKDF2WithHMACSHA512 must be available in order to use any of the " + - "PBKDF2 algorithms for the [xpack.security.authc.service_token_hashing.algorithm] setting.", e); + "Support for PBKDF2WithHMACSHA512 must be available in order to use any of the " + + "PBKDF2 algorithms for the [xpack.security.authc.service_token_hashing.algorithm] setting.", + e + ); } } - }, Property.NodeScope); + }, + Property.NodeScope + ); public static final List DEFAULT_SUPPORTED_PROTOCOLS; @@ -205,8 +273,7 @@ private XPackSettings() { // BCJSSE in FIPS mode doesn't support TLSv1.3 yet. LogManager.getLogger(XPackSettings.class).debug("TLSv1.3 is not supported", e); } - DEFAULT_SUPPORTED_PROTOCOLS = supportsTLSv13 ? - Arrays.asList("TLSv1.3", "TLSv1.2", "TLSv1.1") : Arrays.asList("TLSv1.2", "TLSv1.1"); + DEFAULT_SUPPORTED_PROTOCOLS = supportsTLSv13 ? Arrays.asList("TLSv1.3", "TLSv1.2", "TLSv1.1") : Arrays.asList("TLSv1.2", "TLSv1.1"); } public static final SslClientAuthenticationMode CLIENT_AUTH_DEFAULT = SslClientAuthenticationMode.REQUIRED; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/AbstractGetResourcesRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/AbstractGetResourcesRequest.java index d53f0516518aa..27088eee660a0 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/AbstractGetResourcesRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/AbstractGetResourcesRequest.java @@ -21,8 +21,7 @@ public abstract class AbstractGetResourcesRequest extends ActionRequest { private PageParams pageParams = PageParams.defaultParams(); private boolean allowNoResources = false; - public AbstractGetResourcesRequest() { - } + public AbstractGetResourcesRequest() {} public AbstractGetResourcesRequest(StreamInput in) throws IOException { super(in); @@ -89,9 +88,9 @@ public boolean equals(Object obj) { return false; } AbstractGetResourcesRequest other = (AbstractGetResourcesRequest) obj; - return Objects.equals(resourceId, other.resourceId) && - Objects.equals(pageParams, other.pageParams) && - allowNoResources == other.allowNoResources; + return Objects.equals(resourceId, other.resourceId) + && Objects.equals(pageParams, other.pageParams) + && allowNoResources == other.allowNoResources; } public abstract String getResourceIdField(); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/AbstractGetResourcesResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/AbstractGetResourcesResponse.java index df53869537b4b..521577976a8d0 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/AbstractGetResourcesResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/AbstractGetResourcesResponse.java @@ -12,16 +12,17 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.xcontent.StatusToXContentObject; +import org.elasticsearch.rest.RestStatus; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.rest.RestStatus; import org.elasticsearch.xpack.core.action.util.QueryPage; import java.io.IOException; import java.util.Objects; public abstract class AbstractGetResourcesResponse extends ActionResponse - implements StatusToXContentObject { + implements + StatusToXContentObject { private QueryPage resources; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/AbstractTransportGetResourcesAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/AbstractTransportGetResourcesAction.java index 990c5f5c40aed..8931e5fafc3f3 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/AbstractTransportGetResourcesAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/AbstractTransportGetResourcesAction.java @@ -14,18 +14,12 @@ import org.elasticsearch.action.support.HandledTransportAction; import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.client.Client; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.regex.Regex; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.core.Nullable; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; @@ -33,6 +27,12 @@ import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.sort.SortBuilders; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.action.util.ExpandedIdsMatcher; import org.elasticsearch.xpack.core.action.util.QueryPage; @@ -52,16 +52,22 @@ * @param The type of the Request * @param The type of the Response */ -public abstract class AbstractTransportGetResourcesAction> - extends HandledTransportAction { +public abstract class AbstractTransportGetResourcesAction< + Resource extends ToXContent & Writeable, + Request extends AbstractGetResourcesRequest, + Response extends AbstractGetResourcesResponse> extends HandledTransportAction { private final Client client; private final NamedXContentRegistry xContentRegistry; - protected AbstractTransportGetResourcesAction(String actionName, TransportService transportService, ActionFilters actionFilters, - Writeable.Reader request, Client client, - NamedXContentRegistry xContentRegistry) { + protected AbstractTransportGetResourcesAction( + String actionName, + TransportService transportService, + ActionFilters actionFilters, + Writeable.Reader request, + Client client, + NamedXContentRegistry xContentRegistry + ) { super(actionName, transportService, actionFilters, request); this.client = Objects.requireNonNull(client); this.xContentRegistry = Objects.requireNonNull(xContentRegistry); @@ -69,65 +75,70 @@ protected AbstractTransportGetResourcesAction(String actionName, TransportServic protected void searchResources(AbstractGetResourcesRequest request, ActionListener> listener) { String[] tokens = Strings.tokenizeToStringArray(request.getResourceId(), ","); - SearchSourceBuilder sourceBuilder = new SearchSourceBuilder() - .sort(SortBuilders.fieldSort(request.getResourceIdField()) + SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().sort( + SortBuilders.fieldSort(request.getResourceIdField()) // If there are no resources, there might be no mapping for the id field. // This makes sure we don't get an error if that happens. - .unmappedType("long")) - .query(buildQuery(tokens, request.getResourceIdField())); + .unmappedType("long") + ).query(buildQuery(tokens, request.getResourceIdField())); if (request.getPageParams() != null) { - sourceBuilder.from(request.getPageParams().getFrom()) - .size(request.getPageParams().getSize()); + sourceBuilder.from(request.getPageParams().getFrom()).size(request.getPageParams().getSize()); } sourceBuilder.trackTotalHits(true); IndicesOptions indicesOptions = SearchRequest.DEFAULT_INDICES_OPTIONS; - SearchRequest searchRequest = new SearchRequest(getIndices()) - .indicesOptions(IndicesOptions.fromOptions(true, + SearchRequest searchRequest = new SearchRequest(getIndices()).indicesOptions( + IndicesOptions.fromOptions( + true, indicesOptions.allowNoIndices(), indicesOptions.expandWildcardsOpen(), indicesOptions.expandWildcardsClosed(), - indicesOptions)) - .source(customSearchOptions(sourceBuilder)); + indicesOptions + ) + ).source(customSearchOptions(sourceBuilder)); - executeAsyncWithOrigin(client.threadPool().getThreadContext(), + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), executionOrigin(), searchRequest, listener.delegateFailure((l, response) -> { - List docs = new ArrayList<>(); - Set foundResourceIds = new HashSet<>(); - long totalHitCount = response.getHits().getTotalHits().value; - for (SearchHit hit : response.getHits().getHits()) { - BytesReference docSource = hit.getSourceRef(); - try (InputStream stream = docSource.streamInput(); - XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser( - xContentRegistry, LoggingDeprecationHandler.INSTANCE, stream)) { - Resource resource = parse(parser); - String id = extractIdFromResource(resource); - // Do not include a resource with the same ID twice - if (foundResourceIds.contains(id) == false) { - docs.add(resource); - foundResourceIds.add(id); - } - } catch (IOException e) { - l.onFailure(e); + List docs = new ArrayList<>(); + Set foundResourceIds = new HashSet<>(); + long totalHitCount = response.getHits().getTotalHits().value; + for (SearchHit hit : response.getHits().getHits()) { + BytesReference docSource = hit.getSourceRef(); + try ( + InputStream stream = docSource.streamInput(); + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, stream) + ) { + Resource resource = parse(parser); + String id = extractIdFromResource(resource); + // Do not include a resource with the same ID twice + if (foundResourceIds.contains(id) == false) { + docs.add(resource); + foundResourceIds.add(id); } + } catch (IOException e) { + l.onFailure(e); } - ExpandedIdsMatcher requiredMatches = new ExpandedIdsMatcher(tokens, request.isAllowNoResources()); - requiredMatches.filterMatchedIds(foundResourceIds); - if (requiredMatches.hasUnmatchedIds()) { - l.onFailure(notFoundException(requiredMatches.unmatchedIdsString())); + } + ExpandedIdsMatcher requiredMatches = new ExpandedIdsMatcher(tokens, request.isAllowNoResources()); + requiredMatches.filterMatchedIds(foundResourceIds); + if (requiredMatches.hasUnmatchedIds()) { + l.onFailure(notFoundException(requiredMatches.unmatchedIdsString())); + } else { + // if only exact ids have been given, take the count from docs to avoid potential duplicates + // in versioned indexes (like transform) + if (requiredMatches.isOnlyExact()) { + l.onResponse(new QueryPage<>(docs, docs.size(), getResultsField())); } else { - // if only exact ids have been given, take the count from docs to avoid potential duplicates - // in versioned indexes (like transform) - if (requiredMatches.isOnlyExact()) { - l.onResponse(new QueryPage<>(docs, docs.size(), getResultsField())); - } else { - l.onResponse(new QueryPage<>(docs, totalHitCount, getResultsField())); - } + l.onResponse(new QueryPage<>(docs, totalHitCount, getResultsField())); } - }), - client::search); + } + }), + client::search + ); } private QueryBuilder buildQuery(String[] tokens, String resourceIdField) { @@ -184,6 +195,7 @@ protected QueryBuilder additionalQuery() { * @return parsed Resource typed object */ protected abstract Resource parse(XContentParser parser) throws IOException; + /** * @param resourceId Resource ID or expression that was not found in the search results * @return The exception to throw in the event that an ID or expression is not found diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/AbstractTransportSetResetModeAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/AbstractTransportSetResetModeAction.java index 55bf8d8fd3736..952e4a48406ef 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/AbstractTransportSetResetModeAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/AbstractTransportSetResetModeAction.java @@ -38,7 +38,8 @@ public AbstractTransportSetResetModeAction( ThreadPool threadPool, ClusterService clusterService, ActionFilters actionFilters, - IndexNameExpressionResolver indexNameExpressionResolver) { + IndexNameExpressionResolver indexNameExpressionResolver + ) { super( actionName, transportService, @@ -59,10 +60,12 @@ public AbstractTransportSetResetModeAction( protected abstract ClusterState setState(ClusterState oldState, SetResetModeActionRequest request); @Override - protected void masterOperation(Task task, - SetResetModeActionRequest request, - ClusterState state, - ActionListener listener) throws Exception { + protected void masterOperation( + Task task, + SetResetModeActionRequest request, + ClusterState state, + ActionListener listener + ) throws Exception { final boolean isResetModeEnabled = isResetMode(state); // Noop, nothing for us to do, simply return fast to the caller @@ -81,32 +84,24 @@ protected void masterOperation(Task task, ) ); - ActionListener wrappedListener = ActionListener.wrap( - r -> { - logger.debug(() -> new ParameterizedMessage("Completed reset mode request for [{}]", featureName())); - listener.onResponse(r); - }, - e -> { - logger.debug( - () -> new ParameterizedMessage("Completed reset mode for [{}] request but with failure", featureName()), - e - ); - listener.onFailure(e); + ActionListener wrappedListener = ActionListener.wrap(r -> { + logger.debug(() -> new ParameterizedMessage("Completed reset mode request for [{}]", featureName())); + listener.onResponse(r); + }, e -> { + logger.debug(() -> new ParameterizedMessage("Completed reset mode for [{}] request but with failure", featureName()), e); + listener.onFailure(e); + }); + + ActionListener clusterStateUpdateListener = ActionListener.wrap(acknowledgedResponse -> { + if (acknowledgedResponse.isAcknowledged() == false) { + wrappedListener.onFailure(new ElasticsearchTimeoutException("Unknown error occurred while updating cluster state")); + return; } - ); - - ActionListener clusterStateUpdateListener = ActionListener.wrap( - acknowledgedResponse -> { - if (acknowledgedResponse.isAcknowledged() == false) { - wrappedListener.onFailure(new ElasticsearchTimeoutException("Unknown error occurred while updating cluster state")); - return; - } - wrappedListener.onResponse(acknowledgedResponse); - }, - wrappedListener::onFailure - ); + wrappedListener.onResponse(acknowledgedResponse); + }, wrappedListener::onFailure); - clusterService.submitStateUpdateTask(featureName() + "-set-reset-mode", + clusterService.submitStateUpdateTask( + featureName() + "-set-reset-mode", new AckedClusterStateUpdateTask(request, clusterStateUpdateListener) { @Override @@ -120,7 +115,8 @@ public ClusterState execute(ClusterState currentState) { logger.trace(() -> new ParameterizedMessage("Executing cluster state update for [{}]", featureName())); return setState(currentState, request); } - }); + } + ); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/CreateDataStreamAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/CreateDataStreamAction.java index 5e0381e0283e3..47c1571b0d7e2 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/CreateDataStreamAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/CreateDataStreamAction.java @@ -86,8 +86,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Request request = (Request) o; - return name.equals(request.name) && - startTime == request.startTime; + return name.equals(request.name) && startTime == request.startTime; } @Override @@ -97,7 +96,7 @@ public int hashCode() { @Override public String[] indices() { - return new String[]{name}; + return new String[] { name }; } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/DataStreamsStatsAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/DataStreamsStatsAction.java index a1776ae6c5367..78f71bebe163f 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/DataStreamsStatsAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/DataStreamsStatsAction.java @@ -16,9 +16,9 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.index.store.StoreStats; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.index.store.StoreStats; import java.io.IOException; import java.util.Arrays; @@ -52,8 +52,16 @@ public static class Response extends BroadcastResponse { private final ByteSizeValue totalStoreSize; private final DataStreamStats[] dataStreams; - public Response(int totalShards, int successfulShards, int failedShards, List shardFailures, - int dataStreamCount, int backingIndices, ByteSizeValue totalStoreSize, DataStreamStats[] dataStreams) { + public Response( + int totalShards, + int successfulShards, + int failedShards, + List shardFailures, + int dataStreamCount, + int backingIndices, + ByteSizeValue totalStoreSize, + DataStreamStats[] dataStreams + ) { super(totalShards, successfulShards, failedShards, shardFailures); this.dataStreamCount = dataStreamCount; this.backingIndices = backingIndices; @@ -111,10 +119,10 @@ public boolean equals(Object obj) { return false; } Response response = (Response) obj; - return dataStreamCount == response.dataStreamCount && - backingIndices == response.backingIndices && - Objects.equals(totalStoreSize, response.totalStoreSize) && - Arrays.equals(dataStreams, response.dataStreams); + return dataStreamCount == response.dataStreamCount + && backingIndices == response.backingIndices + && Objects.equals(totalStoreSize, response.totalStoreSize) + && Arrays.equals(dataStreams, response.dataStreams); } @Override @@ -126,12 +134,16 @@ public int hashCode() { @Override public String toString() { - return "Response{" + - "dataStreamCount=" + dataStreamCount + - ", backingIndices=" + backingIndices + - ", totalStoreSize=" + totalStoreSize + - ", dataStreams=" + Arrays.toString(dataStreams) + - '}'; + return "Response{" + + "dataStreamCount=" + + dataStreamCount + + ", backingIndices=" + + backingIndices + + ", totalStoreSize=" + + totalStoreSize + + ", dataStreams=" + + Arrays.toString(dataStreams) + + '}'; } } @@ -199,10 +211,10 @@ public boolean equals(Object obj) { return false; } DataStreamStats that = (DataStreamStats) obj; - return backingIndices == that.backingIndices && - maximumTimestamp == that.maximumTimestamp && - Objects.equals(dataStream, that.dataStream) && - Objects.equals(storeSize, that.storeSize); + return backingIndices == that.backingIndices + && maximumTimestamp == that.maximumTimestamp + && Objects.equals(dataStream, that.dataStream) + && Objects.equals(storeSize, that.storeSize); } @Override @@ -212,12 +224,17 @@ public int hashCode() { @Override public String toString() { - return "DataStreamStats{" + - "dataStream='" + dataStream + '\'' + - ", backingIndices=" + backingIndices + - ", storeSize=" + storeSize + - ", maximumTimestamp=" + maximumTimestamp + - '}'; + return "DataStreamStats{" + + "dataStream='" + + dataStream + + '\'' + + ", backingIndices=" + + backingIndices + + ", storeSize=" + + storeSize + + ", maximumTimestamp=" + + maximumTimestamp + + '}'; } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/DeleteDataStreamAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/DeleteDataStreamAction.java index 3cd99168b1641..628b9c34440c2 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/DeleteDataStreamAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/DeleteDataStreamAction.java @@ -83,8 +83,9 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Request request = (Request) o; - return wildcardExpressionsOriginallySpecified == request.wildcardExpressionsOriginallySpecified && - Arrays.equals(names, request.names) && indicesOptions.equals(request.indicesOptions); + return wildcardExpressionsOriginallySpecified == request.wildcardExpressionsOriginallySpecified + && Arrays.equals(names, request.names) + && indicesOptions.equals(request.indicesOptions); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/GetDataStreamAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/GetDataStreamAction.java index 8d4cfcb05c830..840ffd730b8e1 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/GetDataStreamAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/GetDataStreamAction.java @@ -15,10 +15,10 @@ import org.elasticsearch.cluster.AbstractDiffable; import org.elasticsearch.cluster.health.ClusterHealthStatus; import org.elasticsearch.cluster.metadata.DataStream; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -72,8 +72,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Request request = (Request) o; - return Arrays.equals(names, request.names) && - indicesOptions.equals(request.indicesOptions); + return Arrays.equals(names, request.names) && indicesOptions.equals(request.indicesOptions); } @Override @@ -93,7 +92,7 @@ public IndicesOptions indicesOptions() { return indicesOptions; } - public Request indicesOptions(IndicesOptions indicesOptions){ + public Request indicesOptions(IndicesOptions indicesOptions) { this.indicesOptions = indicesOptions; return this; } @@ -124,11 +123,17 @@ public static class DataStreamInfo extends AbstractDiffable impl DataStream dataStream; ClusterHealthStatus dataStreamStatus; - @Nullable String indexTemplate; - @Nullable String ilmPolicyName; - - public DataStreamInfo(DataStream dataStream, ClusterHealthStatus dataStreamStatus, @Nullable String indexTemplate, - @Nullable String ilmPolicyName) { + @Nullable + String indexTemplate; + @Nullable + String ilmPolicyName; + + public DataStreamInfo( + DataStream dataStream, + ClusterHealthStatus dataStreamStatus, + @Nullable String indexTemplate, + @Nullable String ilmPolicyName + ) { this.dataStream = dataStream; this.dataStreamStatus = dataStreamStatus; this.indexTemplate = indexTemplate; @@ -194,10 +199,10 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; DataStreamInfo that = (DataStreamInfo) o; - return dataStream.equals(that.dataStream) && - dataStreamStatus == that.dataStreamStatus && - Objects.equals(indexTemplate, that.indexTemplate) && - Objects.equals(ilmPolicyName, that.ilmPolicyName); + return dataStream.equals(that.dataStream) + && dataStreamStatus == that.dataStreamStatus + && Objects.equals(indexTemplate, that.indexTemplate) + && Objects.equals(ilmPolicyName, that.ilmPolicyName); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/PromoteDataStreamAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/PromoteDataStreamAction.java index 36bd2ca2a5f0f..14dfdd3ac7d11 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/PromoteDataStreamAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/PromoteDataStreamAction.java @@ -58,7 +58,7 @@ public Request(StreamInput in) throws IOException { @Override public String[] indices() { - return new String[]{name}; + return new String[] { name }; } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/ReloadAnalyzersRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/ReloadAnalyzersRequest.java index a00c72267bcb7..e26c7968aead7 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/ReloadAnalyzersRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/ReloadAnalyzersRequest.java @@ -38,8 +38,7 @@ public boolean equals(Object o) { return false; } ReloadAnalyzersRequest that = (ReloadAnalyzersRequest) o; - return Objects.equals(indicesOptions(), that.indicesOptions()) - && Arrays.equals(indices, that.indices); + return Objects.equals(indicesOptions(), that.indicesOptions()) && Arrays.equals(indices, that.indices); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/ReloadAnalyzersResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/ReloadAnalyzersResponse.java index b0705b41ed3ec..f973ab20281a2 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/ReloadAnalyzersResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/ReloadAnalyzersResponse.java @@ -8,11 +8,11 @@ import org.elasticsearch.action.support.DefaultShardOperationFailedException; import org.elasticsearch.action.support.broadcast.BroadcastResponse; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.action.TransportReloadAnalyzersAction.ReloadResult; @@ -32,7 +32,7 @@ /** * The response object that will be returned when reloading analyzers */ -public class ReloadAnalyzersResponse extends BroadcastResponse { +public class ReloadAnalyzersResponse extends BroadcastResponse { private final Map reloadDetails; @@ -46,8 +46,13 @@ public ReloadAnalyzersResponse(StreamInput in) throws IOException { this.reloadDetails = in.readMap(StreamInput::readString, ReloadDetails::new); } - public ReloadAnalyzersResponse(int totalShards, int successfulShards, int failedShards, - List shardFailures, Map reloadedIndicesNodes) { + public ReloadAnalyzersResponse( + int totalShards, + int successfulShards, + int failedShards, + List shardFailures, + Map reloadedIndicesNodes + ) { super(totalShards, successfulShards, failedShards, shardFailures); this.reloadDetails = reloadedIndicesNodes; } @@ -74,23 +79,32 @@ protected void addCustomXContentFields(XContentBuilder builder, Params params) t } @SuppressWarnings({ "unchecked" }) - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("reload_analyzer", - true, arg -> { - BroadcastResponse response = (BroadcastResponse) arg[0]; - List results = (List) arg[1]; - Map reloadedNodeIds = new HashMap<>(); - for (ReloadDetails result : results) { - reloadedNodeIds.put(result.getIndexName(), result); - } - return new ReloadAnalyzersResponse(response.getTotalShards(), response.getSuccessfulShards(), response.getFailedShards(), - Arrays.asList(response.getShardFailures()), reloadedNodeIds); - }); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "reload_analyzer", + true, + arg -> { + BroadcastResponse response = (BroadcastResponse) arg[0]; + List results = (List) arg[1]; + Map reloadedNodeIds = new HashMap<>(); + for (ReloadDetails result : results) { + reloadedNodeIds.put(result.getIndexName(), result); + } + return new ReloadAnalyzersResponse( + response.getTotalShards(), + response.getSuccessfulShards(), + response.getFailedShards(), + Arrays.asList(response.getShardFailures()), + reloadedNodeIds + ); + } + ); @SuppressWarnings({ "unchecked" }) private static final ConstructingObjectParser ENTRY_PARSER = new ConstructingObjectParser<>( - "reload_analyzer.entry", true, arg -> { - return new ReloadDetails((String) arg[0], new HashSet<>((List) arg[1]), new HashSet<>((List) arg[2])); - }); + "reload_analyzer.entry", + true, + arg -> { return new ReloadDetails((String) arg[0], new HashSet<>((List) arg[1]), new HashSet<>((List) arg[2])); } + ); static { declareBroadcastFields(PARSER); @@ -180,8 +194,8 @@ public boolean equals(Object o) { } ReloadDetails that = (ReloadDetails) o; return Objects.equals(indexName, that.indexName) - && Objects.equals(reloadedIndicesNodes, that.reloadedIndicesNodes) - && Objects.equals(reloadedAnalyzers, that.reloadedAnalyzers); + && Objects.equals(reloadedIndicesNodes, that.reloadedIndicesNodes) + && Objects.equals(reloadedAnalyzers, that.reloadedAnalyzers); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/SetResetModeActionRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/SetResetModeActionRequest.java index 84a6ae049dbe0..f5dbeddaf6ee1 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/SetResetModeActionRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/SetResetModeActionRequest.java @@ -9,10 +9,10 @@ import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.support.master.AcknowledgedRequest; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -34,9 +34,10 @@ public static SetResetModeActionRequest disabled(boolean deleteMetadata) { private static final ParseField ENABLED = new ParseField("enabled"); private static final ParseField DELETE_METADATA = new ParseField("delete_metadata"); - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("set_reset_mode_action_request", - a -> new SetResetModeActionRequest((Boolean)a[0], (Boolean)a[1])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "set_reset_mode_action_request", + a -> new SetResetModeActionRequest((Boolean) a[0], (Boolean) a[1]) + ); static { PARSER.declareBoolean(ConstructingObjectParser.constructorArg(), ENABLED); @@ -88,8 +89,7 @@ public boolean equals(Object obj) { return false; } SetResetModeActionRequest other = (SetResetModeActionRequest) obj; - return Objects.equals(enabled, other.enabled) - && Objects.equals(deleteMetadata, other.deleteMetadata); + return Objects.equals(enabled, other.enabled) && Objects.equals(deleteMetadata, other.deleteMetadata); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/TransportReloadAnalyzersAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/TransportReloadAnalyzersAction.java index 2d02d31713cbb..1b787b28e472f 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/TransportReloadAnalyzersAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/TransportReloadAnalyzersAction.java @@ -43,21 +43,35 @@ import java.util.Map; import java.util.Set; - /** * Indices clear cache action. */ -public class TransportReloadAnalyzersAction - extends TransportBroadcastByNodeAction { +public class TransportReloadAnalyzersAction extends TransportBroadcastByNodeAction< + ReloadAnalyzersRequest, + ReloadAnalyzersResponse, + ReloadResult> { private static final Logger logger = LogManager.getLogger(TransportReloadAnalyzersAction.class); private final IndicesService indicesService; @Inject - public TransportReloadAnalyzersAction(ClusterService clusterService, TransportService transportService, IndicesService indicesService, - ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver) { - super(ReloadAnalyzerAction.NAME, clusterService, transportService, actionFilters, indexNameExpressionResolver, - ReloadAnalyzersRequest::new, ThreadPool.Names.MANAGEMENT, false); + public TransportReloadAnalyzersAction( + ClusterService clusterService, + TransportService transportService, + IndicesService indicesService, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver + ) { + super( + ReloadAnalyzerAction.NAME, + clusterService, + transportService, + actionFilters, + indexNameExpressionResolver, + ReloadAnalyzersRequest::new, + ThreadPool.Names.MANAGEMENT, + false + ); this.indicesService = indicesService; } @@ -67,12 +81,20 @@ protected ReloadResult readShardResult(StreamInput in) throws IOException { } @Override - protected ReloadAnalyzersResponse newResponse(ReloadAnalyzersRequest request, int totalShards, int successfulShards, int failedShards, - List responses, List shardFailures, ClusterState clusterState) { + protected ReloadAnalyzersResponse newResponse( + ReloadAnalyzersRequest request, + int totalShards, + int successfulShards, + int failedShards, + List responses, + List shardFailures, + ClusterState clusterState + ) { Map reloadedIndicesDetails = new HashMap(); for (ReloadResult result : responses) { if (reloadedIndicesDetails.containsKey(result.index)) { - reloadedIndicesDetails.get(result.index).merge(result);; + reloadedIndicesDetails.get(result.index).merge(result); + ; } else { HashSet nodeIds = new HashSet(); nodeIds.add(result.nodeId); @@ -89,8 +111,12 @@ protected ReloadAnalyzersRequest readRequestFrom(StreamInput in) throws IOExcept } @Override - protected void shardOperation(ReloadAnalyzersRequest request, ShardRouting shardRouting, Task task, - ActionListener listener) { + protected void shardOperation( + ReloadAnalyzersRequest request, + ShardRouting shardRouting, + Task task, + ActionListener listener + ) { ActionListener.completeWith(listener, () -> { logger.info("reloading analyzers for index shard " + shardRouting); IndexService indexService = indicesService.indexService(shardRouting.index()); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/TransportXPackInfoAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/TransportXPackInfoAction.java index 2b2a4cc468215..fee6497265cfc 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/TransportXPackInfoAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/TransportXPackInfoAction.java @@ -32,8 +32,12 @@ public class TransportXPackInfoAction extends HandledTransportAction infoActions; @Inject - public TransportXPackInfoAction(TransportService transportService, ActionFilters actionFilters, LicenseService licenseService, - NodeClient client) { + public TransportXPackInfoAction( + TransportService transportService, + ActionFilters actionFilters, + LicenseService licenseService, + NodeClient client + ) { super(XPackInfoAction.NAME, transportService, actionFilters, XPackInfoRequest::new); this.licenseService = licenseService; this.client = client; @@ -48,7 +52,6 @@ protected List infoActions() { @Override protected void doExecute(Task task, XPackInfoRequest request, ActionListener listener) { - XPackInfoResponse.BuildInfo buildInfo = null; if (request.getCategories().contains(XPackInfoRequest.Category.BUILD)) { buildInfo = new XPackInfoResponse.BuildInfo(XPackBuild.CURRENT.shortHash(), XPackBuild.CURRENT.date()); @@ -58,8 +61,13 @@ protected void doExecute(Task task, XPackInfoRequest request, ActionListener(); for (var infoAction : infoActions) { // local actions are executed directly, not on a separate thread, so no thread safe collection is necessary - client.executeLocally(infoAction, request, - ActionListener.wrap(response -> featureSets.add(response.getInfo()), listener::onFailure)); + client.executeLocally( + infoAction, + request, + ActionListener.wrap(response -> featureSets.add(response.getInfo()), listener::onFailure) + ); } featureSetsInfo = new FeatureSetsInfo(featureSets); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/TransportXPackUsageAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/TransportXPackUsageAction.java index 63fc0e99fa641..28033160b8e2c 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/TransportXPackUsageAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/TransportXPackUsageAction.java @@ -36,11 +36,25 @@ public class TransportXPackUsageAction extends TransportMasterNodeAction usageActions; @Inject - public TransportXPackUsageAction(ThreadPool threadPool, TransportService transportService, - ClusterService clusterService, ActionFilters actionFilters, - IndexNameExpressionResolver indexNameExpressionResolver, NodeClient client) { - super(XPackUsageAction.NAME, transportService, clusterService, threadPool, actionFilters, XPackUsageRequest::new, - indexNameExpressionResolver, XPackUsageResponse::new, ThreadPool.Names.MANAGEMENT); + public TransportXPackUsageAction( + ThreadPool threadPool, + TransportService transportService, + ClusterService clusterService, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver, + NodeClient client + ) { + super( + XPackUsageAction.NAME, + transportService, + clusterService, + threadPool, + actionFilters, + XPackUsageRequest::new, + indexNameExpressionResolver, + XPackUsageResponse::new, + ThreadPool.Names.MANAGEMENT + ); this.client = client; this.usageActions = usageActions(); } @@ -52,8 +66,9 @@ protected List usageActions() { @Override protected void masterOperation(Task task, XPackUsageRequest request, ClusterState state, ActionListener listener) { - final ActionListener> usageActionListener = - listener.delegateFailure((l, usages) -> l.onResponse(new XPackUsageResponse(usages))); + final ActionListener> usageActionListener = listener.delegateFailure( + (l, usages) -> l.onResponse(new XPackUsageResponse(usages)) + ); final AtomicReferenceArray featureSetUsages = new AtomicReferenceArray<>(usageActions.size()); final AtomicInteger position = new AtomicInteger(0); final BiConsumer>> consumer = (featureUsageAction, iteratingListener) -> { @@ -69,14 +84,13 @@ protected void masterOperation(Task task, XPackUsageRequest request, ClusterStat })); }; IteratingActionListener, XPackUsageFeatureAction> iteratingActionListener = - new IteratingActionListener<>(usageActionListener, consumer, usageActions, - threadPool.getThreadContext(), (ignore) -> { - final List usageList = new ArrayList<>(featureSetUsages.length()); - for (int i = 0; i < featureSetUsages.length(); i++) { - usageList.add(featureSetUsages.get(i)); - } - return usageList; - }, (ignore) -> true); + new IteratingActionListener<>(usageActionListener, consumer, usageActions, threadPool.getThreadContext(), (ignore) -> { + final List usageList = new ArrayList<>(featureSetUsages.length()); + for (int i = 0; i < featureSetUsages.length(); i++) { + usageList.add(featureSetUsages.get(i)); + } + return usageList; + }, (ignore) -> true); iteratingActionListener.run(); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/XPackInfoFeatureAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/XPackInfoFeatureAction.java index 9f62ddc868de1..c6c941ef3092d 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/XPackInfoFeatureAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/XPackInfoFeatureAction.java @@ -51,11 +51,32 @@ public class XPackInfoFeatureAction extends ActionType public static final List ALL; static { final List actions = new ArrayList<>(); - actions.addAll(Arrays.asList( - SECURITY, MONITORING, WATCHER, GRAPH, MACHINE_LEARNING, LOGSTASH, EQL, SQL, ROLLUP, INDEX_LIFECYCLE, SNAPSHOT_LIFECYCLE, CCR, - TRANSFORM, VOTING_ONLY, FROZEN_INDICES, SPATIAL, ANALYTICS, ENRICH, DATA_STREAMS, SEARCHABLE_SNAPSHOTS, DATA_TIERS, - AGGREGATE_METRIC - )); + actions.addAll( + Arrays.asList( + SECURITY, + MONITORING, + WATCHER, + GRAPH, + MACHINE_LEARNING, + LOGSTASH, + EQL, + SQL, + ROLLUP, + INDEX_LIFECYCLE, + SNAPSHOT_LIFECYCLE, + CCR, + TRANSFORM, + VOTING_ONLY, + FROZEN_INDICES, + SPATIAL, + ANALYTICS, + ENRICH, + DATA_STREAMS, + SEARCHABLE_SNAPSHOTS, + DATA_TIERS, + AGGREGATE_METRIC + ) + ); ALL = Collections.unmodifiableList(actions); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/XPackInfoFeatureResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/XPackInfoFeatureResponse.java index 9af343291cdfd..5d066a4dc6c50 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/XPackInfoFeatureResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/XPackInfoFeatureResponse.java @@ -35,4 +35,4 @@ public void writeTo(StreamOutput out) throws IOException { info.writeTo(out); } - } +} diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/XPackInfoRequestBuilder.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/XPackInfoRequestBuilder.java index 467b8a6aba289..bdd7c2b4f7b15 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/XPackInfoRequestBuilder.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/XPackInfoRequestBuilder.java @@ -28,7 +28,6 @@ public XPackInfoRequestBuilder setVerbose(boolean verbose) { return this; } - public XPackInfoRequestBuilder setCategories(EnumSet categories) { request.setCategories(categories); return this; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/XPackUsageFeatureTransportAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/XPackUsageFeatureTransportAction.java index a9f23b985ab08..ed4fbff6f8a4b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/XPackUsageFeatureTransportAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/XPackUsageFeatureTransportAction.java @@ -18,11 +18,25 @@ public abstract class XPackUsageFeatureTransportAction extends TransportMasterNodeAction { - public XPackUsageFeatureTransportAction(String name, TransportService transportService, ClusterService clusterService, - ThreadPool threadPool, ActionFilters actionFilters, - IndexNameExpressionResolver indexNameExpressionResolver) { - super(name, transportService, clusterService, threadPool, actionFilters, XPackUsageRequest::new, indexNameExpressionResolver, - XPackUsageFeatureResponse::new, ThreadPool.Names.MANAGEMENT); + public XPackUsageFeatureTransportAction( + String name, + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver + ) { + super( + name, + transportService, + clusterService, + threadPool, + actionFilters, + XPackUsageRequest::new, + indexNameExpressionResolver, + XPackUsageFeatureResponse::new, + ThreadPool.Names.MANAGEMENT + ); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/XPackUsageRequestBuilder.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/XPackUsageRequestBuilder.java index 4e0602b5bc22c..39432dbb147b6 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/XPackUsageRequestBuilder.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/XPackUsageRequestBuilder.java @@ -10,8 +10,10 @@ import org.elasticsearch.client.ElasticsearchClient; import org.elasticsearch.protocol.xpack.XPackUsageRequest; -public class XPackUsageRequestBuilder - extends MasterNodeOperationRequestBuilder { +public class XPackUsageRequestBuilder extends MasterNodeOperationRequestBuilder< + XPackUsageRequest, + XPackUsageResponse, + XPackUsageRequestBuilder> { public XPackUsageRequestBuilder(ElasticsearchClient client) { this(client, XPackUsageAction.INSTANCE); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/XPackUsageResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/XPackUsageResponse.java index 8c38fba711a54..5b07a743735b7 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/XPackUsageResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/XPackUsageResponse.java @@ -35,8 +35,7 @@ public List getUsages() { @Override public void writeTo(final StreamOutput out) throws IOException { // we can only write the usages with version the coordinating node is compatible with otherwise it will not know the named writeable - final List usagesToWrite = usages - .stream() + final List usagesToWrite = usages.stream() .filter(usage -> out.getVersion().onOrAfter(usage.getMinimalSupportedVersion())) .collect(Collectors.toUnmodifiableList()); writeTo(out, usagesToWrite); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/util/ExpandedIdsMatcher.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/util/ExpandedIdsMatcher.java index 7e578a0d480cd..a0233e6a5b79f 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/util/ExpandedIdsMatcher.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/util/ExpandedIdsMatcher.java @@ -103,7 +103,7 @@ public ExpandedIdsMatcher(String[] tokens, boolean allowNoMatchForWildcards) { * matcher is removed from {@code requiredMatchers}. */ public void filterMatchedIds(Collection ids) { - for (String id: ids) { + for (String id : ids) { Iterator itr = requiredMatches.iterator(); if (itr.hasNext() == false) { break; @@ -137,7 +137,6 @@ public boolean isOnlyExact() { return onlyExact; } - /** * A simple matcher with one purpose to test whether an id * matches a expression that may contain wildcards. diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/util/PageParams.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/util/PageParams.java index cf1113955c4df..a6f9188122e48 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/util/PageParams.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/util/PageParams.java @@ -6,11 +6,11 @@ */ package org.elasticsearch.xpack.core.action.util; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -29,8 +29,10 @@ public class PageParams implements ToXContentObject, Writeable { public static final int DEFAULT_FROM = 0; public static final int DEFAULT_SIZE = 100; - public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(PAGE.getPreferredName(), - a -> new PageParams(a[0] == null ? DEFAULT_FROM : (int) a[0], a[1] == null ? DEFAULT_SIZE : (int) a[1])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + PAGE.getPreferredName(), + a -> new PageParams(a[0] == null ? DEFAULT_FROM : (int) a[0], a[1] == null ? DEFAULT_SIZE : (int) a[1]) + ); static { PARSER.declareInt(ConstructingObjectParser.optionalConstructorArg(), FROM); @@ -101,8 +103,7 @@ public boolean equals(Object obj) { return false; } PageParams other = (PageParams) obj; - return Objects.equals(from, other.from) && - Objects.equals(size, other.size); + return Objects.equals(from, other.from) && Objects.equals(size, other.size); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/util/QueryPage.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/util/QueryPage.java index 59cec62ad253f..b1303246337b0 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/util/QueryPage.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/util/QueryPage.java @@ -7,10 +7,10 @@ package org.elasticsearch.xpack.core.action.util; import org.elasticsearch.ResourceNotFoundException; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -107,7 +107,6 @@ public boolean equals(Object obj) { @SuppressWarnings("unchecked") QueryPage other = (QueryPage) obj; - return Objects.equals(results, other.results) && - Objects.equals(count, other.count); + return Objects.equals(results, other.results) && Objects.equals(count, other.count); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/aggregatemetric/AggregateMetricFeatureSetUsage.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/aggregatemetric/AggregateMetricFeatureSetUsage.java index 9fb3724036600..b02601a8286d4 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/aggregatemetric/AggregateMetricFeatureSetUsage.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/aggregatemetric/AggregateMetricFeatureSetUsage.java @@ -25,7 +25,8 @@ public AggregateMetricFeatureSetUsage(boolean available, boolean enabled) { super(XPackField.AGGREGATE_METRIC, available, enabled); } - @Override public Version getMinimalSupportedVersion() { + @Override + public Version getMinimalSupportedVersion() { return Version.V_7_11_0; } @@ -38,8 +39,7 @@ public boolean equals(Object obj) { return false; } AggregateMetricFeatureSetUsage other = (AggregateMetricFeatureSetUsage) obj; - return Objects.equals(available, other.available) && - Objects.equals(enabled, other.enabled); + return Objects.equals(available, other.available) && Objects.equals(enabled, other.enabled); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/analytics/action/AnalyticsStatsAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/analytics/action/AnalyticsStatsAction.java index 2237ea003c306..4d9d6b9824cdb 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/analytics/action/AnalyticsStatsAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/analytics/action/AnalyticsStatsAction.java @@ -17,9 +17,9 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.transport.TransportRequest; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.transport.TransportRequest; import org.elasticsearch.xpack.core.common.stats.EnumCounters; import java.io.IOException; @@ -116,8 +116,7 @@ protected void writeNodesTo(StreamOutput out, List nodes) throws I } public EnumCounters getStats() { - List> countersPerNode = getNodes() - .stream() + List> countersPerNode = getNodes().stream() .map(AnalyticsStatsAction.NodeResponse::getStats) .collect(Collectors.toList()); return EnumCounters.merge(Item.class, countersPerNode); @@ -186,8 +185,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; NodeResponse that = (NodeResponse) o; - return counters.equals(that.counters) && - getNode().equals(that.getNode()); + return counters.equals(that.counters) && getNode().equals(that.getNode()); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/async/AsyncExecutionId.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/async/AsyncExecutionId.java index 2adac89170262..b4cc8bc0d3f30 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/async/AsyncExecutionId.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/async/AsyncExecutionId.java @@ -61,8 +61,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; AsyncExecutionId searchId = (AsyncExecutionId) o; - return docId.equals(searchId.docId) && - taskId.equals(searchId.taskId); + return docId.equals(searchId.docId) && taskId.equals(searchId.taskId); } @Override @@ -72,10 +71,7 @@ public int hashCode() { @Override public String toString() { - return "AsyncExecutionId{" + - "docId='" + docId + '\'' + - ", taskId=" + taskId + - '}'; + return "AsyncExecutionId{" + "docId='" + docId + '\'' + ", taskId=" + taskId + '}'; } /** diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/async/AsyncResultsService.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/async/AsyncResultsService.java index d008d166a4a36..736d0db49f7b5 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/async/AsyncResultsService.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/async/AsyncResultsService.java @@ -45,12 +45,14 @@ public class AsyncResultsService store, - boolean updateInitialResultsInStore, - Class asyncTaskClass, - TriConsumer, TimeValue> addCompletionListener, - TaskManager taskManager, - ClusterService clusterService) { + public AsyncResultsService( + AsyncTaskIndexService store, + boolean updateInitialResultsInStore, + Class asyncTaskClass, + TriConsumer, TimeValue> addCompletionListener, + TaskManager taskManager, + ClusterService clusterService + ) { this.updateInitialResultsInStore = updateInitialResultsInStore; this.asyncTaskClass = asyncTaskClass; this.addCompletionListener = addCompletionListener; @@ -82,22 +84,27 @@ public void retrieveResult(GetAsyncResultRequest request, ActionListener 0) { - store.updateExpirationTime(searchId.getDocId(), expirationTime, - ActionListener.wrap( - p -> getSearchResponseFromTask(searchId, request, nowInMillis, expirationTime, listener), - exc -> { - RestStatus status = ExceptionsHelper.status(ExceptionsHelper.unwrapCause(exc)); - if (status != RestStatus.NOT_FOUND) { - logger.error(() -> new ParameterizedMessage("failed to update expiration time for async-search [{}]", - searchId.getEncoded()), exc); - listener.onFailure(exc); - } else { - //the async search document or its index is not found. - //That can happen if an invalid/deleted search id is provided. - listener.onFailure(new ResourceNotFoundException(searchId.getEncoded())); - } + store.updateExpirationTime( + searchId.getDocId(), + expirationTime, + ActionListener.wrap(p -> getSearchResponseFromTask(searchId, request, nowInMillis, expirationTime, listener), exc -> { + RestStatus status = ExceptionsHelper.status(ExceptionsHelper.unwrapCause(exc)); + if (status != RestStatus.NOT_FOUND) { + logger.error( + () -> new ParameterizedMessage( + "failed to update expiration time for async-search [{}]", + searchId.getEncoded() + ), + exc + ); + listener.onFailure(exc); + } else { + // the async search document or its index is not found. + // That can happen if an invalid/deleted search id is provided. + listener.onFailure(new ResourceNotFoundException(searchId.getEncoded())); } - )); + }) + ); } else { getSearchResponseFromTask(searchId, request, nowInMillis, expirationTime, listener); } @@ -106,11 +113,13 @@ public void retrieveResult(GetAsyncResultRequest request, ActionListener listener) { + private void getSearchResponseFromTask( + AsyncExecutionId searchId, + GetAsyncResultRequest request, + long nowInMillis, + long expirationTimeMillis, + ActionListener listener + ) { try { final Task task = store.getTaskAndCheckAuthentication(taskManager, searchId, asyncTaskClass); if (task == null) { @@ -126,24 +135,26 @@ private void getSearchResponseFromTask(AsyncExecutionId searchId, if (expirationTimeMillis != -1) { task.setExpirationTime(expirationTimeMillis); } - addCompletionListener.apply(task, listener.delegateFailure((l, response) -> - sendFinalResponse(request, response, nowInMillis, l)), request.getWaitForCompletionTimeout()); + addCompletionListener.apply( + task, + listener.delegateFailure((l, response) -> sendFinalResponse(request, response, nowInMillis, l)), + request.getWaitForCompletionTimeout() + ); } catch (Exception exc) { listener.onFailure(exc); } } - private void getSearchResponseFromIndex(AsyncExecutionId searchId, - GetAsyncResultRequest request, - long nowInMillis, - ActionListener listener) { + private void getSearchResponseFromIndex( + AsyncExecutionId searchId, + GetAsyncResultRequest request, + long nowInMillis, + ActionListener listener + ) { store.getResponse(searchId, true, listener.delegateFailure((l, response) -> sendFinalResponse(request, response, nowInMillis, l))); } - private void sendFinalResponse(GetAsyncResultRequest request, - Response response, - long nowInMillis, - ActionListener listener) { + private void sendFinalResponse(GetAsyncResultRequest request, Response response, long nowInMillis, ActionListener listener) { // check if the result has expired if (response.getExpirationTime() < nowInMillis) { listener.onFailure(new ResourceNotFoundException(request.getId())); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/async/AsyncTaskIndexService.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/async/AsyncTaskIndexService.java index e564c1d902bd7..24a8277188997 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/async/AsyncTaskIndexService.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/async/AsyncTaskIndexService.java @@ -9,8 +9,8 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.message.ParameterizedMessage; -import org.elasticsearch.ExceptionsHelper; import org.elasticsearch.ElasticsearchParseException; +import org.elasticsearch.ExceptionsHelper; import org.elasticsearch.ResourceNotFoundException; import org.elasticsearch.Version; import org.elasticsearch.action.ActionListener; @@ -39,20 +39,20 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.DeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParserUtils; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.engine.DocumentMissingException; import org.elasticsearch.index.engine.VersionConflictEngineException; import org.elasticsearch.indices.SystemIndexDescriptor; import org.elasticsearch.search.fetch.subphase.FetchSourceContext; import org.elasticsearch.tasks.Task; import org.elasticsearch.tasks.TaskManager; +import org.elasticsearch.xcontent.DeprecationHandler; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.XPackPlugin; import org.elasticsearch.xpack.core.search.action.AsyncSearchResponse; import org.elasticsearch.xpack.core.search.action.SearchStatusResponse; @@ -73,10 +73,10 @@ import java.util.Objects; import java.util.function.Function; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken; import static org.elasticsearch.index.mapper.MapperService.SINGLE_MAPPING_NAME; import static org.elasticsearch.search.SearchService.MAX_ASYNC_SEARCH_RESPONSE_SIZE_SETTING; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.xpack.core.ClientHelper.ASYNC_SEARCH_ORIGIN; import static org.elasticsearch.xpack.core.security.authc.AuthenticationField.AUTHENTICATION_KEY; @@ -108,31 +108,30 @@ static Settings settings() { private static XContentBuilder mappings() { try { - XContentBuilder builder = jsonBuilder() - .startObject() - .startObject(SINGLE_MAPPING_NAME) - .startObject("_meta") - .field("version", Version.CURRENT) - .endObject() - .field("dynamic", "strict") - .startObject("properties") - .startObject(HEADERS_FIELD) - .field("type", "object") - .field("enabled", "false") - .endObject() - .startObject(RESPONSE_HEADERS_FIELD) - .field("type", "object") - .field("enabled", "false") - .endObject() - .startObject(RESULT_FIELD) - .field("type", "object") - .field("enabled", "false") - .endObject() - .startObject(EXPIRATION_TIME_FIELD) - .field("type", "long") - .endObject() - .endObject() - .endObject() + XContentBuilder builder = jsonBuilder().startObject() + .startObject(SINGLE_MAPPING_NAME) + .startObject("_meta") + .field("version", Version.CURRENT) + .endObject() + .field("dynamic", "strict") + .startObject("properties") + .startObject(HEADERS_FIELD) + .field("type", "object") + .field("enabled", "false") + .endObject() + .startObject(RESPONSE_HEADERS_FIELD) + .field("type", "object") + .field("enabled", "false") + .endObject() + .startObject(RESULT_FIELD) + .field("type", "object") + .field("enabled", "false") + .endObject() + .startObject(EXPIRATION_TIME_FIELD) + .field("type", "long") + .endObject() + .endObject() + .endObject() .endObject(); return builder; } catch (IOException e) { @@ -163,14 +162,16 @@ public static SystemIndexDescriptor getSystemIndexDescriptor() { private final ClusterService clusterService; private final CircuitBreaker circuitBreaker; - public AsyncTaskIndexService(String index, - ClusterService clusterService, - ThreadContext threadContext, - Client client, - String origin, - Writeable.Reader reader, - NamedWriteableRegistry registry, - BigArrays bigArrays) { + public AsyncTaskIndexService( + String index, + ClusterService clusterService, + ThreadContext threadContext, + Client client, + String origin, + Writeable.Reader reader, + NamedWriteableRegistry registry, + BigArrays bigArrays + ) { this.index = index; this.securityContext = new SecurityContext(clusterService.getSettings(), threadContext); this.client = client; @@ -179,8 +180,8 @@ public AsyncTaskIndexService(String index, this.reader = reader; this.bigArrays = bigArrays; this.maxResponseSize = MAX_ASYNC_SEARCH_RESPONSE_SIZE_SETTING.get(clusterService.getSettings()).getBytes(); - clusterService.getClusterSettings().addSettingsUpdateConsumer( - MAX_ASYNC_SEARCH_RESPONSE_SIZE_SETTING, (v) -> maxResponseSize = v.getBytes()); + clusterService.getClusterSettings() + .addSettingsUpdateConsumer(MAX_ASYNC_SEARCH_RESPONSE_SIZE_SETTING, (v) -> maxResponseSize = v.getBytes()); this.clusterService = clusterService; this.circuitBreaker = bigArrays.breakerService().getBreaker(CircuitBreaker.REQUEST); } @@ -212,16 +213,13 @@ public Authentication getAuthentication() { * Currently for EQL we don't set limit for a stored async response * TODO: add limit for stored async response in EQL, and instead of this method use createResponse */ - public void createResponseForEQL(String docId, - Map headers, - R response, - ActionListener listener) throws IOException { + public void createResponseForEQL(String docId, Map headers, R response, ActionListener listener) + throws IOException { try { final ReleasableBytesStreamOutput buffer = new ReleasableBytesStreamOutput(0, bigArrays.withCircuitBreaking()); final XContentBuilder source = XContentFactory.jsonBuilder(buffer); listener = ActionListener.runBefore(listener, buffer::close); - source - .startObject() + source.startObject() .field(HEADERS_FIELD, headers) .field(EXPIRATION_TIME_FIELD, response.getExpirationTime()) .directFieldAsBase64(RESULT_FIELD, os -> writeResponse(response, os)) @@ -230,10 +228,7 @@ public void createResponseForEQL(String docId, // do not close the buffer or the XContentBuilder until the IndexRequest is completed (i.e., listener is notified); // otherwise, we underestimate the memory usage in case the circuit breaker does not use the real memory usage. source.flush(); - final IndexRequest indexRequest = new IndexRequest(index) - .create(true) - .id(docId) - .source(buffer.bytes(), source.contentType()); + final IndexRequest indexRequest = new IndexRequest(index).create(true).id(docId).source(buffer.bytes(), source.contentType()); clientWithOrigin.index(indexRequest, listener); } catch (Exception e) { listener.onFailure(e); @@ -244,17 +239,17 @@ public void createResponseForEQL(String docId, * Stores the initial response with the original headers of the authenticated user * and the expected expiration time. */ - public void createResponse(String docId, - Map headers, - R response, - ActionListener listener) throws IOException { + public void createResponse(String docId, Map headers, R response, ActionListener listener) + throws IOException { try { final ReleasableBytesStreamOutput buffer = new ReleasableBytesStreamOutputWithLimit( - 0, bigArrays.withCircuitBreaking(), maxResponseSize); + 0, + bigArrays.withCircuitBreaking(), + maxResponseSize + ); final XContentBuilder source = XContentFactory.jsonBuilder(buffer); listener = ActionListener.runBefore(listener, buffer::close); - source - .startObject() + source.startObject() .field(HEADERS_FIELD, headers) .field(EXPIRATION_TIME_FIELD, response.getExpirationTime()) .directFieldAsBase64(RESULT_FIELD, os -> writeResponse(response, os)) @@ -263,47 +258,46 @@ public void createResponse(String docId, // do not close the buffer or the XContentBuilder until the IndexRequest is completed (i.e., listener is notified); // otherwise, we underestimate the memory usage in case the circuit breaker does not use the real memory usage. source.flush(); - final IndexRequest indexRequest = new IndexRequest(index) - .create(true) - .id(docId) - .source(buffer.bytes(), source.contentType()); + final IndexRequest indexRequest = new IndexRequest(index).create(true).id(docId).source(buffer.bytes(), source.contentType()); clientWithOrigin.index(indexRequest, listener); } catch (Exception e) { listener.onFailure(e); } } - public void updateResponse(String docId, - Map> responseHeaders, - R response, - ActionListener listener) { + public void updateResponse( + String docId, + Map> responseHeaders, + R response, + ActionListener listener + ) { updateResponse(docId, responseHeaders, response, listener, false); } /** * Stores the final response if the place-holder document is still present (update). */ - private void updateResponse(String docId, - Map> responseHeaders, - R response, - ActionListener listener, - boolean isFailure) { + private void updateResponse( + String docId, + Map> responseHeaders, + R response, + ActionListener listener, + boolean isFailure + ) { try { - final ReleasableBytesStreamOutput buffer = isFailure ? - new ReleasableBytesStreamOutput(0, bigArrays.withCircuitBreaking()) : - new ReleasableBytesStreamOutputWithLimit(0, bigArrays.withCircuitBreaking(), maxResponseSize); + final ReleasableBytesStreamOutput buffer = isFailure + ? new ReleasableBytesStreamOutput(0, bigArrays.withCircuitBreaking()) + : new ReleasableBytesStreamOutputWithLimit(0, bigArrays.withCircuitBreaking(), maxResponseSize); final XContentBuilder source = XContentFactory.jsonBuilder(buffer); listener = ActionListener.runBefore(listener, buffer::close); - source - .startObject() + source.startObject() .field(RESPONSE_HEADERS_FIELD, responseHeaders) .directFieldAsBase64(RESULT_FIELD, os -> writeResponse(response, os)) .endObject(); // do not close the buffer or the XContentBuilder until the UpdateRequest is completed (i.e., listener is notified); // otherwise, we underestimate the memory usage in case the circuit breaker does not use the real memory usage. source.flush(); - final UpdateRequest request = new UpdateRequest() - .index(index) + final UpdateRequest request = new UpdateRequest().index(index) .id(docId) .doc(buffer.bytes(), source.contentType()) .retryOnConflict(5); @@ -336,11 +330,13 @@ private void updateResponse(String docId, /** * Update the initial stored response with a failure */ - private void updateStoredResponseWithFailure(String docId, - Map> responseHeaders, - R response, - Exception updateException, - ActionListener listener) { + private void updateStoredResponseWithFailure( + String docId, + Map> responseHeaders, + R response, + Exception updateException, + ActionListener listener + ) { R failureResponse = response.convertToFailure(updateException); updateResponse(docId, responseHeaders, failureResponse, listener, true); } @@ -349,26 +345,20 @@ private void updateStoredResponseWithFailure(String docId, * Updates the expiration time of the provided docId if the place-holder * document is still present (update). */ - public void updateExpirationTime(String docId, - long expirationTimeMillis, - ActionListener listener) { + public void updateExpirationTime(String docId, long expirationTimeMillis, ActionListener listener) { Map source = Collections.singletonMap(EXPIRATION_TIME_FIELD, expirationTimeMillis); - UpdateRequest request = new UpdateRequest().index(index) - .id(docId) - .doc(source, XContentType.JSON) - .retryOnConflict(5); + UpdateRequest request = new UpdateRequest().index(index).id(docId).doc(source, XContentType.JSON).retryOnConflict(5); clientWithOrigin.update(request, listener); } /** * Deletes the provided asyncTaskId from the index if present. */ - public void deleteResponse(AsyncExecutionId asyncExecutionId, - ActionListener listener) { + public void deleteResponse(AsyncExecutionId asyncExecutionId, ActionListener listener) { try { DeleteRequest request = new DeleteRequest(index).id(asyncExecutionId.getDocId()); clientWithOrigin.delete(request, listener); - } catch(Exception e) { + } catch (Exception e) { listener.onFailure(e); } } @@ -377,21 +367,19 @@ public void deleteResponse(AsyncExecutionId asyncExecutionId, * Returns the {@link AsyncTask} if the provided asyncTaskId * is registered in the task manager, null otherwise. */ - public T getTask(TaskManager taskManager, - AsyncExecutionId asyncExecutionId, - Class tClass) throws IOException { + public T getTask(TaskManager taskManager, AsyncExecutionId asyncExecutionId, Class tClass) throws IOException { Task task = taskManager.getTask(asyncExecutionId.getTaskId().getId()); if (tClass.isInstance(task) == false) { return null; } - @SuppressWarnings("unchecked") T asyncTask = (T) task; + @SuppressWarnings("unchecked") + T asyncTask = (T) task; if (asyncTask.getExecutionId().equals(asyncExecutionId) == false) { return null; } return asyncTask; } - /** * Returns the {@link AsyncTask} if the provided asyncTaskId * is registered in the task manager, null otherwise. @@ -399,9 +387,11 @@ public T getTask(TaskManager taskManager, * This method throws a {@link ResourceNotFoundException} if the authenticated user * is not the creator of the original task. */ - public T getTaskAndCheckAuthentication(TaskManager taskManager, - AsyncExecutionId asyncExecutionId, - Class tClass) throws IOException { + public T getTaskAndCheckAuthentication( + TaskManager taskManager, + AsyncExecutionId asyncExecutionId, + Class tClass + ) throws IOException { T asyncTask = getTask(taskManager, asyncExecutionId, tClass); if (asyncTask == null) { return null; @@ -414,25 +404,23 @@ public T getTaskAndCheckAuthentication(TaskManager taskMan return asyncTask; } - /** * Gets the response from the index if present, or delegate a {@link ResourceNotFoundException} * failure to the provided listener if not. * When the provided restoreResponseHeaders is true, this method also restores the * response headers of the original request in the current thread context. */ - public void getResponse(AsyncExecutionId asyncExecutionId, - boolean restoreResponseHeaders, - ActionListener listener) { + public void getResponse(AsyncExecutionId asyncExecutionId, boolean restoreResponseHeaders, ActionListener listener) { getResponseFromIndex(asyncExecutionId, restoreResponseHeaders, true, listener); } - private void getResponseFromIndex(AsyncExecutionId asyncExecutionId, - boolean restoreResponseHeaders, - boolean checkAuthentication, - ActionListener outerListener) { - final GetRequest getRequest = new GetRequest(index) - .preference(asyncExecutionId.getEncoded()) + private void getResponseFromIndex( + AsyncExecutionId asyncExecutionId, + boolean restoreResponseHeaders, + boolean checkAuthentication, + ActionListener outerListener + ) { + final GetRequest getRequest = new GetRequest(index).preference(asyncExecutionId.getEncoded()) .id(asyncExecutionId.getDocId()) .realtime(true); clientWithOrigin.get(getRequest, outerListener.delegateFailure((listener, getResponse) -> { @@ -456,10 +444,20 @@ private void getResponseFromIndex(AsyncExecutionId asyncExecutionId, })); } - private R parseResponseFromIndex(AsyncExecutionId asyncExecutionId, BytesReference source, - boolean restoreResponseHeaders, boolean checkAuthentication) { - try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, source, XContentType.JSON)) { + private R parseResponseFromIndex( + AsyncExecutionId asyncExecutionId, + BytesReference source, + boolean restoreResponseHeaders, + boolean checkAuthentication + ) { + try ( + XContentParser parser = XContentHelper.createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + source, + XContentType.JSON + ) + ) { ensureExpectedToken(parser.nextToken(), XContentParser.Token.START_OBJECT, parser); R resp = null; Long expirationTime = null; @@ -474,17 +472,18 @@ private R parseResponseFromIndex(AsyncExecutionId asyncExecutionId, BytesReferen expirationTime = (long) parser.numberValue(); break; case HEADERS_FIELD: - @SuppressWarnings("unchecked") final Map headers = - (Map) XContentParserUtils.parseFieldsValue(parser); + @SuppressWarnings("unchecked") + final Map headers = (Map) XContentParserUtils.parseFieldsValue(parser); // check the authentication of the current user against the user that initiated the async task - if (checkAuthentication && - ensureAuthenticatedUserIsSame(headers, securityContext.getAuthentication()) == false) { + if (checkAuthentication && ensureAuthenticatedUserIsSame(headers, securityContext.getAuthentication()) == false) { throw new ResourceNotFoundException(asyncExecutionId.getEncoded()); } break; case RESPONSE_HEADERS_FIELD: - @SuppressWarnings("unchecked") final Map> responseHeaders = - (Map>) XContentParserUtils.parseFieldsValue(parser); + @SuppressWarnings("unchecked") + final Map> responseHeaders = (Map>) XContentParserUtils.parseFieldsValue( + parser + ); if (restoreResponseHeaders) { restoreResponseHeadersContext(securityContext.getThreadContext(), responseHeaders); } @@ -512,7 +511,8 @@ public void retrieveStatu Class tClass, Function statusProducerFromTask, TriFunction statusProducerFromIndex, - ActionListener outerListener) { + ActionListener outerListener + ) { // check if the result has expired outerListener = outerListener.delegateFailure((listener, resp) -> { if (resp.getExpirationTime() < System.currentTimeMillis()) { @@ -529,8 +529,16 @@ public void retrieveStatu outerListener.onResponse(response); } else { // get status response from index - getResponseFromIndex(asyncExecutionId, false, false, outerListener.delegateFailure((listener, resp) -> - listener.onResponse(statusProducerFromIndex.apply(resp, resp.getExpirationTime(), asyncExecutionId.getEncoded())))); + getResponseFromIndex( + asyncExecutionId, + false, + false, + outerListener.delegateFailure( + (listener, resp) -> listener.onResponse( + statusProducerFromIndex.apply(resp, resp.getExpirationTime(), asyncExecutionId.getEncoded()) + ) + ) + ); } } catch (Exception exc) { outerListener.onFailure(exc); @@ -542,27 +550,24 @@ public void retrieveStatu * in the async search index. **/ void ensureAuthenticatedUserCanDeleteFromIndex(AsyncExecutionId executionId, ActionListener listener) { - GetRequest internalGet = new GetRequest(index) - .preference(executionId.getEncoded()) + GetRequest internalGet = new GetRequest(index).preference(executionId.getEncoded()) .id(executionId.getDocId()) .fetchSourceContext(new FetchSourceContext(true, new String[] { HEADERS_FIELD }, new String[] {})); - clientWithOrigin.get(internalGet, ActionListener.wrap( - get -> { - if (get.isExists() == false) { - listener.onFailure(new ResourceNotFoundException(executionId.getEncoded())); - return; - } - // Check authentication for the user - @SuppressWarnings("unchecked") - Map headers = (Map) get.getSource().get(HEADERS_FIELD); - if (ensureAuthenticatedUserIsSame(headers, securityContext.getAuthentication())) { - listener.onResponse(null); - } else { - listener.onFailure(new ResourceNotFoundException(executionId.getEncoded())); - } - }, - exc -> listener.onFailure(new ResourceNotFoundException(executionId.getEncoded())))); + clientWithOrigin.get(internalGet, ActionListener.wrap(get -> { + if (get.isExists() == false) { + listener.onFailure(new ResourceNotFoundException(executionId.getEncoded())); + return; + } + // Check authentication for the user + @SuppressWarnings("unchecked") + Map headers = (Map) get.getSource().get(HEADERS_FIELD); + if (ensureAuthenticatedUserIsSame(headers, securityContext.getAuthentication())) { + listener.onResponse(null); + } else { + listener.onFailure(new ResourceNotFoundException(executionId.getEncoded())); + } + }, exc -> listener.onFailure(new ResourceNotFoundException(executionId.getEncoded())))); } /** @@ -648,8 +653,14 @@ private static class ReleasableBytesStreamOutputWithLimit extends ReleasableByte @Override protected void ensureCapacity(long offset) { if (offset > limit) { - throw new IllegalArgumentException("Can't store an async search response larger than [" + limit + "] bytes. " + - "This limit can be set by changing the [" + MAX_ASYNC_SEARCH_RESPONSE_SIZE_SETTING.getKey() + "] setting."); + throw new IllegalArgumentException( + "Can't store an async search response larger than [" + + limit + + "] bytes. " + + "This limit can be set by changing the [" + + MAX_ASYNC_SEARCH_RESPONSE_SIZE_SETTING.getKey() + + "] setting." + ); } super.ensureCapacity(offset); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/async/AsyncTaskMaintenanceService.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/async/AsyncTaskMaintenanceService.java index 9877d25c7e8c8..51329c91bb36d 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/async/AsyncTaskMaintenanceService.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/async/AsyncTaskMaintenanceService.java @@ -19,8 +19,8 @@ import org.elasticsearch.common.component.AbstractLifecycleComponent; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.gateway.GatewayService; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.reindex.DeleteByQueryAction; @@ -47,8 +47,11 @@ public class AsyncTaskMaintenanceService extends AbstractLifecycleComponent impl * is mainly used by integration tests to make the garbage * collection of search responses more reactive. */ - public static final Setting ASYNC_SEARCH_CLEANUP_INTERVAL_SETTING = - Setting.timeSetting("async_search.index_cleanup_interval", TimeValue.timeValueHours(1), Setting.Property.NodeScope); + public static final Setting ASYNC_SEARCH_CLEANUP_INTERVAL_SETTING = Setting.timeSetting( + "async_search.index_cleanup_interval", + TimeValue.timeValueHours(1), + Setting.Property.NodeScope + ); private static final Logger logger = LogManager.getLogger(AsyncTaskMaintenanceService.class); @@ -62,11 +65,13 @@ public class AsyncTaskMaintenanceService extends AbstractLifecycleComponent impl private boolean isCleanupRunning; private volatile Scheduler.Cancellable cancellable; - public AsyncTaskMaintenanceService(ClusterService clusterService, - String localNodeId, - Settings nodeSettings, - ThreadPool threadPool, - Client clientWithOrigin) { + public AsyncTaskMaintenanceService( + ClusterService clusterService, + String localNodeId, + Settings nodeSettings, + ThreadPool threadPool, + Client clientWithOrigin + ) { this.clusterService = clusterService; this.index = XPackPlugin.ASYNC_RESULTS_INDEX; this.localNodeId = localNodeId; @@ -75,7 +80,6 @@ public AsyncTaskMaintenanceService(ClusterService clusterService, this.delay = ASYNC_SEARCH_CLEANUP_INTERVAL_SETTING.get(nodeSettings); } - @Override protected void doStart() { clusterService.addListener(this); @@ -88,8 +92,7 @@ protected void doStop() { } @Override - protected final void doClose() throws IOException { - } + protected final void doClose() throws IOException {} @Override public void clusterChanged(ClusterChangedEvent event) { @@ -124,8 +127,9 @@ synchronized void tryStartCleanup(ClusterState state) { synchronized void executeNextCleanup() { if (isCleanupRunning) { long nowInMillis = System.currentTimeMillis(); - DeleteByQueryRequest toDelete = new DeleteByQueryRequest(index) - .setQuery(QueryBuilders.rangeQuery(EXPIRATION_TIME_FIELD).lte(nowInMillis)); + DeleteByQueryRequest toDelete = new DeleteByQueryRequest(index).setQuery( + QueryBuilders.rangeQuery(EXPIRATION_TIME_FIELD).lte(nowInMillis) + ); clientWithOrigin.execute(DeleteByQueryAction.INSTANCE, toDelete, ActionListener.wrap(this::scheduleNextCleanup)); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/async/DeleteAsyncResultsService.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/async/DeleteAsyncResultsService.java index b917d63178217..793d790e957e7 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/async/DeleteAsyncResultsService.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/async/DeleteAsyncResultsService.java @@ -39,14 +39,12 @@ public class DeleteAsyncResultsService { * @param store AsyncTaskIndexService for the response we are working with * @param taskManager task manager */ - public DeleteAsyncResultsService(AsyncTaskIndexService> store, - TaskManager taskManager) { + public DeleteAsyncResultsService(AsyncTaskIndexService> store, TaskManager taskManager) { this.taskManager = taskManager; this.store = store; } - public void deleteResponse(DeleteAsyncResultRequest request, - ActionListener listener) { + public void deleteResponse(DeleteAsyncResultRequest request, ActionListener listener) { hasCancelTaskPrivilegeAsync(resp -> deleteResponseAsync(request, resp, listener)); } @@ -60,12 +58,15 @@ private void hasCancelTaskPrivilegeAsync(Consumer consumer) { HasPrivilegesRequest req = new HasPrivilegesRequest(); req.username(current.getUser().principal()); req.clusterPrivileges(ClusterPrivilegeResolver.CANCEL_TASK.name()); - req.indexPrivileges(new RoleDescriptor.IndicesPrivileges[]{}); - req.applicationPrivileges(new RoleDescriptor.ApplicationResourcePrivileges[]{}); + req.indexPrivileges(new RoleDescriptor.IndicesPrivileges[] {}); + req.applicationPrivileges(new RoleDescriptor.ApplicationResourcePrivileges[] {}); try { - store.getClient().execute(HasPrivilegesAction.INSTANCE, req, ActionListener.wrap( - resp -> consumer.accept(resp.isCompleteMatch()), - exc -> consumer.accept(false))); + store.getClient() + .execute( + HasPrivilegesAction.INSTANCE, + req, + ActionListener.wrap(resp -> consumer.accept(resp.isCompleteMatch()), exc -> consumer.accept(false)) + ); } catch (Exception exc) { consumer.accept(false); } @@ -74,22 +75,27 @@ private void hasCancelTaskPrivilegeAsync(Consumer consumer) { } } - private void deleteResponseAsync(DeleteAsyncResultRequest request, - boolean hasCancelTaskPrivilege, - ActionListener listener) { + private void deleteResponseAsync( + DeleteAsyncResultRequest request, + boolean hasCancelTaskPrivilege, + ActionListener listener + ) { try { AsyncExecutionId searchId = AsyncExecutionId.decode(request.getId()); - AsyncTask task = hasCancelTaskPrivilege ? store.getTask(taskManager, searchId, AsyncTask.class) : - store.getTaskAndCheckAuthentication(taskManager, searchId, AsyncTask.class); + AsyncTask task = hasCancelTaskPrivilege + ? store.getTask(taskManager, searchId, AsyncTask.class) + : store.getTaskAndCheckAuthentication(taskManager, searchId, AsyncTask.class); if (task != null) { - //the task was found and gets cancelled. The response may or may not be found, but we will return 200 anyways. + // the task was found and gets cancelled. The response may or may not be found, but we will return 200 anyways. task.cancelTask(taskManager, () -> deleteResponseFromIndex(searchId, true, listener), "cancelled by user"); } else { if (hasCancelTaskPrivilege) { deleteResponseFromIndex(searchId, false, listener); } else { - store.ensureAuthenticatedUserCanDeleteFromIndex(searchId, - ActionListener.wrap(res -> deleteResponseFromIndex(searchId, false, listener), listener::onFailure)); + store.ensureAuthenticatedUserCanDeleteFromIndex( + searchId, + ActionListener.wrap(res -> deleteResponseFromIndex(searchId, false, listener), listener::onFailure) + ); } } } catch (Exception exc) { @@ -97,28 +103,23 @@ private void deleteResponseAsync(DeleteAsyncResultRequest request, } } - private void deleteResponseFromIndex(AsyncExecutionId taskId, - boolean taskWasFound, - ActionListener listener) { - store.deleteResponse(taskId, ActionListener.wrap( - resp -> { - if (resp.status() == RestStatus.OK || taskWasFound) { - listener.onResponse(AcknowledgedResponse.TRUE); - } else { - listener.onFailure(new ResourceNotFoundException(taskId.getEncoded())); - } - }, - exc -> { - RestStatus status = ExceptionsHelper.status(ExceptionsHelper.unwrapCause(exc)); - //the index may not be there (no initial async search response stored yet?): we still want to return 200 - //note that index missing comes back as 200 hence it's handled in the onResponse callback - if (status == RestStatus.NOT_FOUND && taskWasFound) { - listener.onResponse(AcknowledgedResponse.TRUE); - } else { - logger.error(() -> new ParameterizedMessage("failed to clean async result [{}]", - taskId.getEncoded()), exc); - listener.onFailure(new ResourceNotFoundException(taskId.getEncoded())); - } - })); + private void deleteResponseFromIndex(AsyncExecutionId taskId, boolean taskWasFound, ActionListener listener) { + store.deleteResponse(taskId, ActionListener.wrap(resp -> { + if (resp.status() == RestStatus.OK || taskWasFound) { + listener.onResponse(AcknowledgedResponse.TRUE); + } else { + listener.onFailure(new ResourceNotFoundException(taskId.getEncoded())); + } + }, exc -> { + RestStatus status = ExceptionsHelper.status(ExceptionsHelper.unwrapCause(exc)); + // the index may not be there (no initial async search response stored yet?): we still want to return 200 + // note that index missing comes back as 200 hence it's handled in the onResponse callback + if (status == RestStatus.NOT_FOUND && taskWasFound) { + listener.onResponse(AcknowledgedResponse.TRUE); + } else { + logger.error(() -> new ParameterizedMessage("failed to clean async result [{}]", taskId.getEncoded()), exc); + listener.onFailure(new ResourceNotFoundException(taskId.getEncoded())); + } + })); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/async/GetAsyncResultRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/async/GetAsyncResultRequest.java index 427bb7c9dd549..1b51cf87dcd83 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/async/GetAsyncResultRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/async/GetAsyncResultRequest.java @@ -85,9 +85,9 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; GetAsyncResultRequest request = (GetAsyncResultRequest) o; - return Objects.equals(id, request.id) && - waitForCompletionTimeout.equals(request.waitForCompletionTimeout) && - keepAlive.equals(request.keepAlive); + return Objects.equals(id, request.id) + && waitForCompletionTimeout.equals(request.waitForCompletionTimeout) + && keepAlive.equals(request.keepAlive); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/async/StoredAsyncResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/async/StoredAsyncResponse.java index 916abaf87278e..cff42f5800543 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/async/StoredAsyncResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/async/StoredAsyncResponse.java @@ -21,7 +21,9 @@ * Internal class for temporary storage of eql search results */ public class StoredAsyncResponse extends ActionResponse - implements AsyncResponse>, ToXContentObject { + implements + AsyncResponse>, + ToXContentObject { private final R response; private final Exception exception; private final long expirationTimeMillis; @@ -77,8 +79,8 @@ public boolean equals(Object o) { if (o == null || getClass() != o.getClass()) return false; StoredAsyncResponse response1 = (StoredAsyncResponse) o; if (exception != null && response1.exception != null) { - if (Objects.equals(exception.getClass(), response1.exception.getClass()) == false || - Objects.equals(exception.getMessage(), response1.exception.getMessage()) == false) { + if (Objects.equals(exception.getClass(), response1.exception.getClass()) == false + || Objects.equals(exception.getMessage(), response1.exception.getMessage()) == false) { return false; } } else { @@ -86,14 +88,17 @@ public boolean equals(Object o) { return false; } } - return expirationTimeMillis == response1.expirationTimeMillis && - Objects.equals(response, response1.response); + return expirationTimeMillis == response1.expirationTimeMillis && Objects.equals(response, response1.response); } @Override public int hashCode() { - return Objects.hash(response, exception == null ? null : exception.getClass(), - exception == null ? null : exception.getMessage(), expirationTimeMillis); + return Objects.hash( + response, + exception == null ? null : exception.getClass(), + exception == null ? null : exception.getMessage(), + expirationTimeMillis + ); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/async/StoredAsyncTask.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/async/StoredAsyncTask.java index 08e9b613b352c..1a61f48b2d982 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/async/StoredAsyncTask.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/async/StoredAsyncTask.java @@ -18,7 +18,6 @@ import java.util.List; import java.util.Map; - public abstract class StoredAsyncTask extends CancellableTask implements AsyncTask { private final AsyncExecutionId asyncExecutionId; @@ -26,9 +25,17 @@ public abstract class StoredAsyncTask extends C private volatile long expirationTimeMillis; private final List> completionListeners; - public StoredAsyncTask(long id, String type, String action, String description, TaskId parentTaskId, - Map headers, Map originHeaders, AsyncExecutionId asyncExecutionId, - TimeValue keepAlive) { + public StoredAsyncTask( + long id, + String type, + String action, + String description, + TaskId parentTaskId, + Map headers, + Map originHeaders, + AsyncExecutionId asyncExecutionId, + TimeValue keepAlive + ) { super(id, type, action, description, parentTaskId, headers); this.asyncExecutionId = asyncExecutionId; this.originHeaders = originHeaders; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/async/TransportDeleteAsyncResultAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/async/TransportDeleteAsyncResultAction.java index d178b41f918fa..aa9004459f1f0 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/async/TransportDeleteAsyncResultAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/async/TransportDeleteAsyncResultAction.java @@ -30,23 +30,31 @@ public class TransportDeleteAsyncResultAction extends HandledTransportAction store = new AsyncTaskIndexService<>(XPackPlugin.ASYNC_RESULTS_INDEX, clusterService, - threadPool.getThreadContext(), client, ASYNC_SEARCH_ORIGIN, - (in) -> {throw new UnsupportedOperationException("Reading is not supported during deletion");}, registry, bigArrays); + AsyncTaskIndexService store = new AsyncTaskIndexService<>( + XPackPlugin.ASYNC_RESULTS_INDEX, + clusterService, + threadPool.getThreadContext(), + client, + ASYNC_SEARCH_ORIGIN, + (in) -> { throw new UnsupportedOperationException("Reading is not supported during deletion"); }, + registry, + bigArrays + ); this.deleteResultsService = new DeleteAsyncResultsService(store, transportService.getTaskManager()); } - @Override protected void doExecute(Task task, DeleteAsyncResultRequest request, ActionListener listener) { AsyncExecutionId searchId = AsyncExecutionId.decode(request.getId()); @@ -54,8 +62,12 @@ protected void doExecute(Task task, DeleteAsyncResultRequest request, ActionList if (clusterService.localNode().getId().equals(searchId.getTaskId().getNodeId()) || node == null) { deleteResultsService.deleteResponse(request, listener); } else { - transportService.sendRequest(node, DeleteAsyncResultAction.NAME, request, - new ActionListenerResponseHandler<>(listener, AcknowledgedResponse::readFrom, ThreadPool.Names.SAME)); + transportService.sendRequest( + node, + DeleteAsyncResultAction.NAME, + request, + new ActionListenerResponseHandler<>(listener, AcknowledgedResponse::readFrom, ThreadPool.Names.SAME) + ); } } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/AutoFollowMetadata.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/AutoFollowMetadata.java index a9e8c5f32e0cb..86ee465127a36 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/AutoFollowMetadata.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/AutoFollowMetadata.java @@ -11,7 +11,6 @@ import org.elasticsearch.cluster.AbstractNamedDiffable; import org.elasticsearch.cluster.metadata.IndexAbstraction; import org.elasticsearch.cluster.metadata.Metadata; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.regex.Regex; @@ -19,6 +18,7 @@ import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentFragment; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -45,12 +45,14 @@ public class AutoFollowMetadata extends AbstractNamedDiffable i private static final ParseField HEADERS = new ParseField("headers"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("auto_follow", + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "auto_follow", args -> new AutoFollowMetadata( (Map) args[0], (Map>) args[1], (Map>) args[2] - )); + ) + ); static { PARSER.declareObject(ConstructingObjectParser.constructorArg(), (p, c) -> { @@ -79,14 +81,20 @@ public static AutoFollowMetadata fromXContent(XContentParser parser) throws IOEx private final Map> followedLeaderIndexUUIDs; private final Map> headers; - public AutoFollowMetadata(Map patterns, - Map> followedLeaderIndexUUIDs, - Map> headers) { + public AutoFollowMetadata( + Map patterns, + Map> followedLeaderIndexUUIDs, + Map> headers + ) { this.patterns = Collections.unmodifiableMap(patterns); - this.followedLeaderIndexUUIDs = Collections.unmodifiableMap(followedLeaderIndexUUIDs.entrySet().stream() - .collect(Collectors.toMap(Map.Entry::getKey, e -> Collections.unmodifiableList(e.getValue())))); - this.headers = Collections.unmodifiableMap(headers.entrySet().stream() - .collect(Collectors.toMap(Map.Entry::getKey, e -> Collections.unmodifiableMap(e.getValue())))); + this.followedLeaderIndexUUIDs = Collections.unmodifiableMap( + followedLeaderIndexUUIDs.entrySet() + .stream() + .collect(Collectors.toMap(Map.Entry::getKey, e -> Collections.unmodifiableList(e.getValue()))) + ); + this.headers = Collections.unmodifiableMap( + headers.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> Collections.unmodifiableMap(e.getValue()))) + ); } public AutoFollowMetadata(StreamInput in) throws IOException { @@ -129,8 +137,11 @@ public Version getMinimalSupportedVersion() { public void writeTo(StreamOutput out) throws IOException { out.writeMap(patterns, StreamOutput::writeString, (out1, value) -> value.writeTo(out1)); out.writeMapOfLists(followedLeaderIndexUUIDs, StreamOutput::writeString, StreamOutput::writeString); - out.writeMap(headers, StreamOutput::writeString, - (valOut, header) -> valOut.writeMap(header, StreamOutput::writeString, StreamOutput::writeString)); + out.writeMap( + headers, + StreamOutput::writeString, + (valOut, header) -> valOut.writeMap(header, StreamOutput::writeString, StreamOutput::writeString) + ); } @Override @@ -166,9 +177,9 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; AutoFollowMetadata that = (AutoFollowMetadata) o; - return Objects.equals(patterns, that.patterns) && - Objects.equals(followedLeaderIndexUUIDs, that.followedLeaderIndexUUIDs) && - Objects.equals(headers, that.headers); + return Objects.equals(patterns, that.patterns) + && Objects.equals(followedLeaderIndexUUIDs, that.followedLeaderIndexUUIDs) + && Objects.equals(headers, that.headers); } @Override @@ -186,37 +197,34 @@ public static class AutoFollowPattern extends ImmutableFollowParameters implemen public static final ParseField SETTINGS_FIELD = new ParseField("settings"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("auto_follow_pattern", - args -> new AutoFollowPattern( - (String) args[0], - (List) args[1], - args[2] == null ? Collections.emptyList() : (List) args[2], - (String) args[3], - args[4] == null ? Settings.EMPTY : (Settings) args[4], - args[5] == null || (boolean) args[5], - (Integer) args[6], - (Integer) args[7], - (Integer) args[8], - (Integer) args[9], - (ByteSizeValue) args[10], - (ByteSizeValue) args[11], - (Integer) args[12], - (ByteSizeValue) args[13], - (TimeValue) args[14], - (TimeValue) args[15]) - ); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "auto_follow_pattern", + args -> new AutoFollowPattern( + (String) args[0], + (List) args[1], + args[2] == null ? Collections.emptyList() : (List) args[2], + (String) args[3], + args[4] == null ? Settings.EMPTY : (Settings) args[4], + args[5] == null || (boolean) args[5], + (Integer) args[6], + (Integer) args[7], + (Integer) args[8], + (Integer) args[9], + (ByteSizeValue) args[10], + (ByteSizeValue) args[11], + (Integer) args[12], + (ByteSizeValue) args[13], + (TimeValue) args[14], + (TimeValue) args[15] + ) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), REMOTE_CLUSTER_FIELD); PARSER.declareStringArray(ConstructingObjectParser.constructorArg(), LEADER_PATTERNS_FIELD); PARSER.declareStringArray(ConstructingObjectParser.optionalConstructorArg(), LEADER_EXCLUSION_PATTERNS_FIELD); PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), FOLLOW_PATTERN_FIELD); - PARSER.declareObject( - ConstructingObjectParser.optionalConstructorArg(), - (p, c) -> Settings.fromXContent(p), - SETTINGS_FIELD - ); + PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> Settings.fromXContent(p), SETTINGS_FIELD); PARSER.declareBoolean(ConstructingObjectParser.optionalConstructorArg(), ACTIVE); ImmutableFollowParameters.initParser(PARSER); } @@ -246,8 +254,18 @@ public AutoFollowPattern( TimeValue maxRetryDelay, TimeValue pollTimeout ) { - super(maxReadRequestOperationCount, maxWriteRequestOperationCount, maxOutstandingReadRequests, maxOutstandingWriteRequests, - maxReadRequestSize, maxWriteRequestSize, maxWriteBufferCount, maxWriteBufferSize, maxRetryDelay, pollTimeout); + super( + maxReadRequestOperationCount, + maxWriteRequestOperationCount, + maxOutstandingReadRequests, + maxOutstandingWriteRequests, + maxReadRequestSize, + maxWriteRequestSize, + maxWriteBufferCount, + maxWriteBufferSize, + maxRetryDelay, + pollTimeout + ); this.remoteCluster = remoteCluster; this.leaderIndexPatterns = leaderIndexPatterns; this.leaderIndexExclusionPatterns = Objects.requireNonNull(leaderIndexExclusionPatterns); @@ -269,11 +287,13 @@ public static AutoFollowPattern readFrom(StreamInput in) throws IOException { return new AutoFollowPattern(remoteCluster, leaderIndexPatterns, followIndexPattern, settings, in); } - private AutoFollowPattern(String remoteCluster, - List leaderIndexPatterns, - String followIndexPattern, - Settings settings, - StreamInput in) throws IOException { + private AutoFollowPattern( + String remoteCluster, + List leaderIndexPatterns, + String followIndexPattern, + Settings settings, + StreamInput in + ) throws IOException { super(in); this.remoteCluster = remoteCluster; this.leaderIndexPatterns = leaderIndexPatterns; @@ -295,21 +315,23 @@ public boolean match(IndexAbstraction indexAbstraction) { return match(leaderIndexPatterns, leaderIndexExclusionPatterns, indexAbstraction); } - public static boolean match(List leaderIndexPatterns, - List leaderIndexExclusionPatterns, - IndexAbstraction indexAbstraction) { - boolean matches = indexAbstraction.isSystem() == false && - Regex.simpleMatch(leaderIndexExclusionPatterns, indexAbstraction.getName()) == false && - Regex.simpleMatch(leaderIndexPatterns, indexAbstraction.getName()); + public static boolean match( + List leaderIndexPatterns, + List leaderIndexExclusionPatterns, + IndexAbstraction indexAbstraction + ) { + boolean matches = indexAbstraction.isSystem() == false + && Regex.simpleMatch(leaderIndexExclusionPatterns, indexAbstraction.getName()) == false + && Regex.simpleMatch(leaderIndexPatterns, indexAbstraction.getName()); if (matches) { return true; } else { final IndexAbstraction.DataStream parentDataStream = indexAbstraction.getParentDataStream(); - return parentDataStream != null && - parentDataStream.isSystem() == false && - Regex.simpleMatch(leaderIndexExclusionPatterns, indexAbstraction.getParentDataStream().getName()) == false && - Regex.simpleMatch(leaderIndexPatterns, indexAbstraction.getParentDataStream().getName()); + return parentDataStream != null + && parentDataStream.isSystem() == false + && Regex.simpleMatch(leaderIndexExclusionPatterns, indexAbstraction.getParentDataStream().getName()) == false + && Regex.simpleMatch(leaderIndexPatterns, indexAbstraction.getParentDataStream().getName()); } } @@ -380,17 +402,18 @@ public boolean equals(Object o) { if (o == null || getClass() != o.getClass()) return false; if (super.equals(o) == false) return false; AutoFollowPattern pattern = (AutoFollowPattern) o; - return active == pattern.active && - remoteCluster.equals(pattern.remoteCluster) && - leaderIndexPatterns.equals(pattern.leaderIndexPatterns) && - leaderIndexExclusionPatterns.equals(pattern.leaderIndexExclusionPatterns) && - followIndexPattern.equals(pattern.followIndexPattern) && - settings.equals(pattern.settings); + return active == pattern.active + && remoteCluster.equals(pattern.remoteCluster) + && leaderIndexPatterns.equals(pattern.leaderIndexPatterns) + && leaderIndexExclusionPatterns.equals(pattern.leaderIndexExclusionPatterns) + && followIndexPattern.equals(pattern.followIndexPattern) + && settings.equals(pattern.settings); } @Override public int hashCode() { - return Objects.hash(super.hashCode(), + return Objects.hash( + super.hashCode(), remoteCluster, leaderIndexPatterns, leaderIndexExclusionPatterns, diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/AutoFollowStats.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/AutoFollowStats.java index ac9497776a89a..debf4dc65f78b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/AutoFollowStats.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/AutoFollowStats.java @@ -7,12 +7,12 @@ package org.elasticsearch.xpack.core.ccr; import org.elasticsearch.ElasticsearchException; -import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Tuple; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -30,8 +30,9 @@ public class AutoFollowStats implements Writeable, ToXContentObject { private static final ParseField NUMBER_OF_SUCCESSFUL_INDICES_AUTO_FOLLOWED = new ParseField("number_of_successful_follow_indices"); private static final ParseField NUMBER_OF_FAILED_INDICES_AUTO_FOLLOWED = new ParseField("number_of_failed_follow_indices"); - private static final ParseField NUMBER_OF_FAILED_REMOTE_CLUSTER_STATE_REQUESTS = - new ParseField("number_of_failed_remote_cluster_state_requests"); + private static final ParseField NUMBER_OF_FAILED_REMOTE_CLUSTER_STATE_REQUESTS = new ParseField( + "number_of_failed_remote_cluster_state_requests" + ); private static final ParseField RECENT_AUTO_FOLLOW_ERRORS = new ParseField("recent_auto_follow_errors"); private static final ParseField LEADER_INDEX = new ParseField("leader_index"); private static final ParseField AUTO_FOLLOW_EXCEPTION = new ParseField("auto_follow_exception"); @@ -42,30 +43,35 @@ public class AutoFollowStats implements Writeable, ToXContentObject { private static final ParseField LAST_SEEN_METADATA_VERSION = new ParseField("last_seen_metadata_version"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser STATS_PARSER = new ConstructingObjectParser<>("auto_follow_stats", + private static final ConstructingObjectParser STATS_PARSER = new ConstructingObjectParser<>( + "auto_follow_stats", args -> new AutoFollowStats( (Long) args[0], (Long) args[1], (Long) args[2], new TreeMap<>( - ((List>>) args[3]) - .stream() - .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))), + ((List>>) args[3]).stream() + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)) + ), new TreeMap<>( - ((List>) args[4]) - .stream() - .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))))); - - private static final ConstructingObjectParser>, Void> AUTO_FOLLOW_EXCEPTIONS_PARSER = - new ConstructingObjectParser<>( + ((List>) args[4]).stream() + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)) + ) + ) + ); + + private static final ConstructingObjectParser< + Map.Entry>, + Void> AUTO_FOLLOW_EXCEPTIONS_PARSER = new ConstructingObjectParser<>( "auto_follow_stats_errors", - args -> new AbstractMap.SimpleEntry<>((String) args[0], new Tuple<>((Long) args[1], (ElasticsearchException) args[2]))); + args -> new AbstractMap.SimpleEntry<>((String) args[0], new Tuple<>((Long) args[1], (ElasticsearchException) args[2])) + ); private static final ConstructingObjectParser, Void> AUTO_FOLLOWED_CLUSTERS_PARSER = new ConstructingObjectParser<>( "auto_followed_clusters", - args -> new AbstractMap.SimpleEntry<>((String) args[0], new AutoFollowedCluster((Long) args[1], (Long) args[2]))); + args -> new AbstractMap.SimpleEntry<>((String) args[0], new AutoFollowedCluster((Long) args[1], (Long) args[2])) + ); static { AUTO_FOLLOW_EXCEPTIONS_PARSER.declareString(ConstructingObjectParser.constructorArg(), LEADER_INDEX); @@ -73,7 +79,8 @@ public class AutoFollowStats implements Writeable, ToXContentObject { AUTO_FOLLOW_EXCEPTIONS_PARSER.declareObject( ConstructingObjectParser.constructorArg(), (p, c) -> ElasticsearchException.fromXContent(p), - AUTO_FOLLOW_EXCEPTION); + AUTO_FOLLOW_EXCEPTION + ); AUTO_FOLLOWED_CLUSTERS_PARSER.declareString(ConstructingObjectParser.constructorArg(), CLUSTER_NAME); AUTO_FOLLOWED_CLUSTERS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), TIME_SINCE_LAST_CHECK_MILLIS); AUTO_FOLLOWED_CLUSTERS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), LAST_SEEN_METADATA_VERSION); @@ -81,10 +88,12 @@ public class AutoFollowStats implements Writeable, ToXContentObject { STATS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), NUMBER_OF_FAILED_INDICES_AUTO_FOLLOWED); STATS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), NUMBER_OF_FAILED_REMOTE_CLUSTER_STATE_REQUESTS); STATS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), NUMBER_OF_SUCCESSFUL_INDICES_AUTO_FOLLOWED); - STATS_PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), AUTO_FOLLOW_EXCEPTIONS_PARSER, - RECENT_AUTO_FOLLOW_ERRORS); - STATS_PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), AUTO_FOLLOWED_CLUSTERS_PARSER, - AUTO_FOLLOWED_CLUSTERS); + STATS_PARSER.declareObjectArray( + ConstructingObjectParser.constructorArg(), + AUTO_FOLLOW_EXCEPTIONS_PARSER, + RECENT_AUTO_FOLLOW_ERRORS + ); + STATS_PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), AUTO_FOLLOWED_CLUSTERS_PARSER, AUTO_FOLLOWED_CLUSTERS); } public static AutoFollowStats fromXContent(final XContentParser parser) { @@ -116,7 +125,8 @@ public AutoFollowStats(StreamInput in) throws IOException { numberOfFailedRemoteClusterStateRequests = in.readVLong(); numberOfSuccessfulFollowIndices = in.readVLong(); recentAutoFollowErrors = new TreeMap<>( - in.readMap(StreamInput::readString, in1 -> new Tuple<>(in1.readZLong(), in1.readException()))); + in.readMap(StreamInput::readString, in1 -> new Tuple<>(in1.readZLong(), in1.readException())) + ); autoFollowedClusters = new TreeMap<>(in.readMap(StreamInput::readString, AutoFollowedCluster::new)); } @@ -205,17 +215,18 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; AutoFollowStats that = (AutoFollowStats) o; - return numberOfFailedFollowIndices == that.numberOfFailedFollowIndices && - numberOfFailedRemoteClusterStateRequests == that.numberOfFailedRemoteClusterStateRequests && - numberOfSuccessfulFollowIndices == that.numberOfSuccessfulFollowIndices && + return numberOfFailedFollowIndices == that.numberOfFailedFollowIndices + && numberOfFailedRemoteClusterStateRequests == that.numberOfFailedRemoteClusterStateRequests + && numberOfSuccessfulFollowIndices == that.numberOfSuccessfulFollowIndices + && /* * ElasticsearchException does not implement equals so we will assume the fetch exceptions are equal if they are equal * up to the key set and their messages. Note that we are relying on the fact that the auto follow exceptions are ordered by * keys. */ - recentAutoFollowErrors.keySet().equals(that.recentAutoFollowErrors.keySet()) && - getFetchExceptionMessages(this).equals(getFetchExceptionMessages(that)) && - Objects.equals(autoFollowedClusters, that.autoFollowedClusters); + recentAutoFollowErrors.keySet().equals(that.recentAutoFollowErrors.keySet()) + && getFetchExceptionMessages(this).equals(getFetchExceptionMessages(that)) + && Objects.equals(autoFollowedClusters, that.autoFollowedClusters); } @Override @@ -235,7 +246,8 @@ public int hashCode() { } private static List getFetchExceptionMessages(final AutoFollowStats status) { - return status.getRecentAutoFollowErrors().values() + return status.getRecentAutoFollowErrors() + .values() .stream() .map(Tuple::v2) .map(ElasticsearchException::getMessage) @@ -244,13 +256,18 @@ private static List getFetchExceptionMessages(final AutoFollowStats stat @Override public String toString() { - return "AutoFollowStats{" + - "numberOfFailedFollowIndices=" + numberOfFailedFollowIndices + - ", numberOfFailedRemoteClusterStateRequests=" + numberOfFailedRemoteClusterStateRequests + - ", numberOfSuccessfulFollowIndices=" + numberOfSuccessfulFollowIndices + - ", recentAutoFollowErrors=" + recentAutoFollowErrors + - ", autoFollowedClusters=" + autoFollowedClusters + - '}'; + return "AutoFollowStats{" + + "numberOfFailedFollowIndices=" + + numberOfFailedFollowIndices + + ", numberOfFailedRemoteClusterStateRequests=" + + numberOfFailedRemoteClusterStateRequests + + ", numberOfSuccessfulFollowIndices=" + + numberOfSuccessfulFollowIndices + + ", recentAutoFollowErrors=" + + recentAutoFollowErrors + + ", autoFollowedClusters=" + + autoFollowedClusters + + '}'; } public static class AutoFollowedCluster implements Writeable { @@ -286,8 +303,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; AutoFollowedCluster that = (AutoFollowedCluster) o; - return timeSinceLastCheckMillis == that.timeSinceLastCheckMillis && - lastSeenMetadataVersion == that.lastSeenMetadataVersion; + return timeSinceLastCheckMillis == that.timeSinceLastCheckMillis && lastSeenMetadataVersion == that.lastSeenMetadataVersion; } @Override @@ -297,10 +313,12 @@ public int hashCode() { @Override public String toString() { - return "AutoFollowedCluster{" + - "timeSinceLastCheckMillis=" + timeSinceLastCheckMillis + - ", lastSeenMetadataVersion=" + lastSeenMetadataVersion + - '}'; + return "AutoFollowedCluster{" + + "timeSinceLastCheckMillis=" + + timeSinceLastCheckMillis + + ", lastSeenMetadataVersion=" + + lastSeenMetadataVersion + + '}'; } } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/CcrConstants.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/CcrConstants.java index e0cb70b76fe39..dfdae089a4002 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/CcrConstants.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/CcrConstants.java @@ -12,8 +12,7 @@ public class CcrConstants { - public static final LicensedFeature.Momentary CCR_FEATURE = - LicensedFeature.momentary(null, "ccr", License.OperationMode.PLATINUM); + public static final LicensedFeature.Momentary CCR_FEATURE = LicensedFeature.momentary(null, "ccr", License.OperationMode.PLATINUM); // no construction private CcrConstants() {} diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/ShardFollowNodeTaskStatus.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/ShardFollowNodeTaskStatus.java index fecac75ff1e96..8198054cd370b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/ShardFollowNodeTaskStatus.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/ShardFollowNodeTaskStatus.java @@ -15,13 +15,13 @@ import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.common.util.Maps; +import org.elasticsearch.core.TimeValue; +import org.elasticsearch.core.Tuple; +import org.elasticsearch.tasks.Task; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.core.TimeValue; -import org.elasticsearch.core.Tuple; -import org.elasticsearch.tasks.Task; import java.io.IOException; import java.util.AbstractMap; @@ -68,48 +68,49 @@ public class ShardFollowNodeTaskStatus implements Task.Status { private static final ParseField FATAL_EXCEPTION = new ParseField("fatal_exception"); @SuppressWarnings("unchecked") - static final ConstructingObjectParser STATUS_PARSER = - new ConstructingObjectParser<>( - STATUS_PARSER_NAME, - args -> new ShardFollowNodeTaskStatus( - (String) args[0], - (String) args[1], - (String) args[2], - (int) args[3], - (long) args[4], - (long) args[5], - (long) args[6], - (long) args[7], - (long) args[8], - (int) args[9], - (int) args[10], - (int) args[11], - (long) args[12], - (long) args[13], - (long) args[14], - (long) args[15], - (long) args[16], - (long) args[17], - (long) args[18], - (long) args[19], - (long) args[20], - (long) args[21], - (long) args[22], - (long) args[23], - (long) args[24], - (long) args[25], - ((List>>) args[26]) - .stream() - .collect(Maps.toUnmodifiableSortedMap(Map.Entry::getKey, Map.Entry::getValue)), - (long) args[27], - (ElasticsearchException) args[28])); + static final ConstructingObjectParser STATUS_PARSER = new ConstructingObjectParser<>( + STATUS_PARSER_NAME, + args -> new ShardFollowNodeTaskStatus( + (String) args[0], + (String) args[1], + (String) args[2], + (int) args[3], + (long) args[4], + (long) args[5], + (long) args[6], + (long) args[7], + (long) args[8], + (int) args[9], + (int) args[10], + (int) args[11], + (long) args[12], + (long) args[13], + (long) args[14], + (long) args[15], + (long) args[16], + (long) args[17], + (long) args[18], + (long) args[19], + (long) args[20], + (long) args[21], + (long) args[22], + (long) args[23], + (long) args[24], + (long) args[25], + ((List>>) args[26]).stream() + .collect(Maps.toUnmodifiableSortedMap(Map.Entry::getKey, Map.Entry::getValue)), + (long) args[27], + (ElasticsearchException) args[28] + ) + ); public static final String READ_EXCEPTIONS_ENTRY_PARSER_NAME = "shard-follow-node-task-status-read-exceptions-entry"; static final ConstructingObjectParser>, Void> READ_EXCEPTIONS_ENTRY_PARSER = - new ConstructingObjectParser<>( - READ_EXCEPTIONS_ENTRY_PARSER_NAME, - args -> new AbstractMap.SimpleEntry<>((long) args[0], Tuple.tuple((Integer)args[1], (ElasticsearchException)args[2]))); + new ConstructingObjectParser<>( + READ_EXCEPTIONS_ENTRY_PARSER_NAME, + args -> new AbstractMap.SimpleEntry<>((long) args[0], Tuple.tuple((Integer) args[1], (ElasticsearchException) args[2])) + ); static { STATUS_PARSER.declareString(ConstructingObjectParser.constructorArg(), LEADER_CLUSTER); @@ -140,9 +141,11 @@ public class ShardFollowNodeTaskStatus implements Task.Status { STATUS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), OPERATIONS_WRITTEN); STATUS_PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), READ_EXCEPTIONS_ENTRY_PARSER, READ_EXCEPTIONS); STATUS_PARSER.declareLong(ConstructingObjectParser.constructorArg(), TIME_SINCE_LAST_READ_MILLIS_FIELD); - STATUS_PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), - (p, c) -> ElasticsearchException.fromXContent(p), - FATAL_EXCEPTION); + STATUS_PARSER.declareObject( + ConstructingObjectParser.optionalConstructorArg(), + (p, c) -> ElasticsearchException.fromXContent(p), + FATAL_EXCEPTION + ); } static final ParseField READ_EXCEPTIONS_ENTRY_FROM_SEQ_NO = new ParseField("from_seq_no"); @@ -153,9 +156,10 @@ public class ShardFollowNodeTaskStatus implements Task.Status { READ_EXCEPTIONS_ENTRY_PARSER.declareLong(ConstructingObjectParser.constructorArg(), READ_EXCEPTIONS_ENTRY_FROM_SEQ_NO); READ_EXCEPTIONS_ENTRY_PARSER.declareInt(ConstructingObjectParser.constructorArg(), READ_EXCEPTIONS_RETRIES); READ_EXCEPTIONS_ENTRY_PARSER.declareObject( - ConstructingObjectParser.constructorArg(), - (p, c) -> ElasticsearchException.fromXContent(p), - READ_EXCEPTIONS_ENTRY_EXCEPTION); + ConstructingObjectParser.constructorArg(), + (p, c) -> ElasticsearchException.fromXContent(p), + READ_EXCEPTIONS_ENTRY_EXCEPTION + ); } private final String remoteCluster; @@ -333,35 +337,36 @@ public ElasticsearchException getFatalException() { } public ShardFollowNodeTaskStatus( - final String remoteCluster, - final String leaderIndex, - final String followerIndex, - final int shardId, - final long leaderGlobalCheckpoint, - final long leaderMaxSeqNo, - final long followerGlobalCheckpoint, - final long followerMaxSeqNo, - final long lastRequestedSeqNo, - final int outstandingReadRequests, - final int outstandingWriteRequests, - final int writeBufferOperationCount, - final long writeBufferSizeInBytes, - final long followerMappingVersion, - final long followerSettingsVersion, - final long followerAliasesVersion, - final long totalReadTimeMillis, - final long totalReadRemoteExecTimeMillis, - final long successfulReadRequests, - final long failedReadRequests, - final long operationsReads, - final long bytesRead, - final long totalWriteTimeMillis, - final long successfulWriteRequests, - final long failedWriteRequests, - final long operationWritten, - final NavigableMap> readExceptions, - final long timeSinceLastReadMillis, - final ElasticsearchException fatalException) { + final String remoteCluster, + final String leaderIndex, + final String followerIndex, + final int shardId, + final long leaderGlobalCheckpoint, + final long leaderMaxSeqNo, + final long followerGlobalCheckpoint, + final long followerMaxSeqNo, + final long lastRequestedSeqNo, + final int outstandingReadRequests, + final int outstandingWriteRequests, + final int writeBufferOperationCount, + final long writeBufferSizeInBytes, + final long followerMappingVersion, + final long followerSettingsVersion, + final long followerAliasesVersion, + final long totalReadTimeMillis, + final long totalReadRemoteExecTimeMillis, + final long successfulReadRequests, + final long failedReadRequests, + final long operationsReads, + final long bytesRead, + final long totalWriteTimeMillis, + final long successfulWriteRequests, + final long failedWriteRequests, + final long operationWritten, + final NavigableMap> readExceptions, + final long timeSinceLastReadMillis, + final ElasticsearchException fatalException + ) { this.remoteCluster = remoteCluster; this.leaderIndex = leaderIndex; this.followerIndex = followerIndex; @@ -424,8 +429,9 @@ public ShardFollowNodeTaskStatus(final StreamInput in) throws IOException { this.successfulWriteRequests = in.readVLong(); this.failedWriteRequests = in.readVLong(); this.operationWritten = in.readVLong(); - this.readExceptions = - new TreeMap<>(in.readMap(StreamInput::readVLong, stream -> Tuple.tuple(stream.readVInt(), stream.readException()))); + this.readExceptions = new TreeMap<>( + in.readMap(StreamInput::readVLong, stream -> Tuple.tuple(stream.readVInt(), stream.readException())) + ); this.timeSinceLastReadMillis = in.readZLong(); this.fatalException = in.readException(); } @@ -465,13 +471,10 @@ public void writeTo(final StreamOutput out) throws IOException { out.writeVLong(successfulWriteRequests); out.writeVLong(failedWriteRequests); out.writeVLong(operationWritten); - out.writeMap( - readExceptions, - StreamOutput::writeVLong, - (stream, value) -> { - stream.writeVInt(value.v1()); - stream.writeException(value.v2()); - }); + out.writeMap(readExceptions, StreamOutput::writeVLong, (stream, value) -> { + stream.writeVInt(value.v1()); + stream.writeException(value.v2()); + }); out.writeZLong(timeSinceLastReadMillis); out.writeException(fatalException); } @@ -500,31 +503,32 @@ public XContentBuilder toXContentFragment(final XContentBuilder builder, final P builder.field(OUTSTANDING_WRITE_REQUESTS.getPreferredName(), outstandingWriteRequests); builder.field(WRITE_BUFFER_OPERATION_COUNT_FIELD.getPreferredName(), writeBufferOperationCount); builder.humanReadableField( - WRITE_BUFFER_SIZE_IN_BYTES_FIELD.getPreferredName(), - "write_buffer_size", - new ByteSizeValue(writeBufferSizeInBytes)); + WRITE_BUFFER_SIZE_IN_BYTES_FIELD.getPreferredName(), + "write_buffer_size", + new ByteSizeValue(writeBufferSizeInBytes) + ); builder.field(FOLLOWER_MAPPING_VERSION_FIELD.getPreferredName(), followerMappingVersion); builder.field(FOLLOWER_SETTINGS_VERSION_FIELD.getPreferredName(), followerSettingsVersion); builder.field(FOLLOWER_ALIASES_VERSION_FIELD.getPreferredName(), followerAliasesVersion); builder.humanReadableField( - TOTAL_READ_TIME_MILLIS_FIELD.getPreferredName(), - "total_read_time", - new TimeValue(totalReadTimeMillis, TimeUnit.MILLISECONDS)); + TOTAL_READ_TIME_MILLIS_FIELD.getPreferredName(), + "total_read_time", + new TimeValue(totalReadTimeMillis, TimeUnit.MILLISECONDS) + ); builder.humanReadableField( - TOTAL_READ_REMOTE_EXEC_TIME_MILLIS_FIELD.getPreferredName(), - "total_read_remote_exec_time", - new TimeValue(totalReadRemoteExecTimeMillis, TimeUnit.MILLISECONDS)); + TOTAL_READ_REMOTE_EXEC_TIME_MILLIS_FIELD.getPreferredName(), + "total_read_remote_exec_time", + new TimeValue(totalReadRemoteExecTimeMillis, TimeUnit.MILLISECONDS) + ); builder.field(SUCCESSFUL_READ_REQUESTS_FIELD.getPreferredName(), successfulReadRequests); builder.field(FAILED_READ_REQUESTS_FIELD.getPreferredName(), failedReadRequests); builder.field(OPERATIONS_READ_FIELD.getPreferredName(), operationsReads); + builder.humanReadableField(BYTES_READ.getPreferredName(), "total_read", new ByteSizeValue(bytesRead, ByteSizeUnit.BYTES)); builder.humanReadableField( - BYTES_READ.getPreferredName(), - "total_read", - new ByteSizeValue(bytesRead, ByteSizeUnit.BYTES)); - builder.humanReadableField( - TOTAL_WRITE_TIME_MILLIS_FIELD.getPreferredName(), - "total_write_time", - new TimeValue(totalWriteTimeMillis, TimeUnit.MILLISECONDS)); + TOTAL_WRITE_TIME_MILLIS_FIELD.getPreferredName(), + "total_write_time", + new TimeValue(totalWriteTimeMillis, TimeUnit.MILLISECONDS) + ); builder.field(SUCCESSFUL_WRITE_REQUESTS_FIELD.getPreferredName(), successfulWriteRequests); builder.field(FAILED_WRITE_REQUEST_FIELD.getPreferredName(), failedWriteRequests); builder.field(OPERATIONS_WRITTEN.getPreferredName(), operationWritten); @@ -547,9 +551,10 @@ public XContentBuilder toXContentFragment(final XContentBuilder builder, final P } builder.endArray(); builder.humanReadableField( - TIME_SINCE_LAST_READ_MILLIS_FIELD.getPreferredName(), - "time_since_last_read", - new TimeValue(timeSinceLastReadMillis, TimeUnit.MILLISECONDS)); + TIME_SINCE_LAST_READ_MILLIS_FIELD.getPreferredName(), + "time_since_last_read", + new TimeValue(timeSinceLastReadMillis, TimeUnit.MILLISECONDS) + ); if (fatalException != null) { builder.field(FATAL_EXCEPTION.getPreferredName()); builder.startObject(); @@ -572,79 +577,81 @@ public boolean equals(final Object o) { final ShardFollowNodeTaskStatus that = (ShardFollowNodeTaskStatus) o; String fatalExceptionMessage = fatalException != null ? fatalException.getMessage() : null; String otherFatalExceptionMessage = that.fatalException != null ? that.fatalException.getMessage() : null; - return remoteCluster.equals(that.remoteCluster) && - leaderIndex.equals(that.leaderIndex) && - followerIndex.equals(that.followerIndex) && - shardId == that.shardId && - leaderGlobalCheckpoint == that.leaderGlobalCheckpoint && - leaderMaxSeqNo == that.leaderMaxSeqNo && - followerGlobalCheckpoint == that.followerGlobalCheckpoint && - followerMaxSeqNo == that.followerMaxSeqNo && - lastRequestedSeqNo == that.lastRequestedSeqNo && - outstandingReadRequests == that.outstandingReadRequests && - outstandingWriteRequests == that.outstandingWriteRequests && - writeBufferOperationCount == that.writeBufferOperationCount && - writeBufferSizeInBytes == that.writeBufferSizeInBytes && - followerMappingVersion == that.followerMappingVersion && - followerSettingsVersion == that.followerSettingsVersion && - followerAliasesVersion == that.followerAliasesVersion && - totalReadTimeMillis == that.totalReadTimeMillis && - totalReadRemoteExecTimeMillis == that.totalReadRemoteExecTimeMillis && - successfulReadRequests == that.successfulReadRequests && - failedReadRequests == that.failedReadRequests && - operationsReads == that.operationsReads && - bytesRead == that.bytesRead && - successfulWriteRequests == that.successfulWriteRequests && - failedWriteRequests == that.failedWriteRequests && - operationWritten == that.operationWritten && - /* - * ElasticsearchException does not implement equals so we will assume the fetch exceptions are equal if they are equal - * up to the key set and their messages. Note that we are relying on the fact that the fetch exceptions are ordered by - * keys. - */ - readExceptions.keySet().equals(that.readExceptions.keySet()) && - getReadExceptionMessages(this).equals(getReadExceptionMessages(that)) && - timeSinceLastReadMillis == that.timeSinceLastReadMillis && - Objects.equals(fatalExceptionMessage, otherFatalExceptionMessage); + return remoteCluster.equals(that.remoteCluster) + && leaderIndex.equals(that.leaderIndex) + && followerIndex.equals(that.followerIndex) + && shardId == that.shardId + && leaderGlobalCheckpoint == that.leaderGlobalCheckpoint + && leaderMaxSeqNo == that.leaderMaxSeqNo + && followerGlobalCheckpoint == that.followerGlobalCheckpoint + && followerMaxSeqNo == that.followerMaxSeqNo + && lastRequestedSeqNo == that.lastRequestedSeqNo + && outstandingReadRequests == that.outstandingReadRequests + && outstandingWriteRequests == that.outstandingWriteRequests + && writeBufferOperationCount == that.writeBufferOperationCount + && writeBufferSizeInBytes == that.writeBufferSizeInBytes + && followerMappingVersion == that.followerMappingVersion + && followerSettingsVersion == that.followerSettingsVersion + && followerAliasesVersion == that.followerAliasesVersion + && totalReadTimeMillis == that.totalReadTimeMillis + && totalReadRemoteExecTimeMillis == that.totalReadRemoteExecTimeMillis + && successfulReadRequests == that.successfulReadRequests + && failedReadRequests == that.failedReadRequests + && operationsReads == that.operationsReads + && bytesRead == that.bytesRead + && successfulWriteRequests == that.successfulWriteRequests + && failedWriteRequests == that.failedWriteRequests + && operationWritten == that.operationWritten + && + /* + * ElasticsearchException does not implement equals so we will assume the fetch exceptions are equal if they are equal + * up to the key set and their messages. Note that we are relying on the fact that the fetch exceptions are ordered by + * keys. + */ + readExceptions.keySet().equals(that.readExceptions.keySet()) + && getReadExceptionMessages(this).equals(getReadExceptionMessages(that)) + && timeSinceLastReadMillis == that.timeSinceLastReadMillis + && Objects.equals(fatalExceptionMessage, otherFatalExceptionMessage); } @Override public int hashCode() { String fatalExceptionMessage = fatalException != null ? fatalException.getMessage() : null; return Objects.hash( - remoteCluster, - leaderIndex, - followerIndex, - shardId, - leaderGlobalCheckpoint, - leaderMaxSeqNo, - followerGlobalCheckpoint, - followerMaxSeqNo, - lastRequestedSeqNo, - outstandingReadRequests, - outstandingWriteRequests, - writeBufferOperationCount, - writeBufferSizeInBytes, - followerMappingVersion, - followerSettingsVersion, - followerAliasesVersion, - totalReadTimeMillis, - totalReadRemoteExecTimeMillis, - successfulReadRequests, - failedReadRequests, - operationsReads, - bytesRead, - successfulWriteRequests, - failedWriteRequests, - operationWritten, - /* - * ElasticsearchException does not implement hash code so we will compute the hash code based on the key set and the - * messages. Note that we are relying on the fact that the fetch exceptions are ordered by keys. - */ - readExceptions.keySet(), - getReadExceptionMessages(this), - timeSinceLastReadMillis, - fatalExceptionMessage); + remoteCluster, + leaderIndex, + followerIndex, + shardId, + leaderGlobalCheckpoint, + leaderMaxSeqNo, + followerGlobalCheckpoint, + followerMaxSeqNo, + lastRequestedSeqNo, + outstandingReadRequests, + outstandingWriteRequests, + writeBufferOperationCount, + writeBufferSizeInBytes, + followerMappingVersion, + followerSettingsVersion, + followerAliasesVersion, + totalReadTimeMillis, + totalReadRemoteExecTimeMillis, + successfulReadRequests, + failedReadRequests, + operationsReads, + bytesRead, + successfulWriteRequests, + failedWriteRequests, + operationWritten, + /* + * ElasticsearchException does not implement hash code so we will compute the hash code based on the key set and the + * messages. Note that we are relying on the fact that the fetch exceptions are ordered by keys. + */ + readExceptions.keySet(), + getReadExceptionMessages(this), + timeSinceLastReadMillis, + fatalExceptionMessage + ); } private static List getReadExceptionMessages(final ShardFollowNodeTaskStatus status) { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/ActivateAutoFollowPatternAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/ActivateAutoFollowPatternAction.java index 5292340cff700..1c0204618bd45 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/ActivateAutoFollowPatternAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/ActivateAutoFollowPatternAction.java @@ -73,8 +73,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Request request = (Request) o; - return active == request.active - && Objects.equals(name, request.name); + return active == request.active && Objects.equals(name, request.name); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/CcrStatsAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/CcrStatsAction.java index 5a5a1ca20b7d0..1c9fb93b8f111 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/CcrStatsAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/CcrStatsAction.java @@ -35,8 +35,7 @@ public Request(StreamInput in) throws IOException { super(in); } - public Request() { - } + public Request() {} @Override public ActionRequestValidationException validate() { @@ -95,8 +94,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Response response = (Response) o; - return Objects.equals(autoFollowStats, response.autoFollowStats) && - Objects.equals(followStats, response.followStats); + return Objects.equals(autoFollowStats, response.autoFollowStats) && Objects.equals(followStats, response.followStats); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/FollowInfoAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/FollowInfoAction.java index 627c2730185b7..8b15da5ea3cab 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/FollowInfoAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/FollowInfoAction.java @@ -6,15 +6,15 @@ */ package org.elasticsearch.xpack.core.ccr.action; -import org.elasticsearch.action.ActionType; import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.ActionResponse; +import org.elasticsearch.action.ActionType; import org.elasticsearch.action.support.master.MasterNodeReadRequest; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -37,8 +37,7 @@ public static class Request extends MasterNodeReadRequest { private String[] followerIndices; - public Request() { - } + public Request() {} public String[] getFollowerIndices() { return followerIndices; @@ -145,8 +144,13 @@ public static class FollowerInfo implements Writeable, ToXContentObject { private final Status status; private final FollowParameters parameters; - public FollowerInfo(String followerIndex, String remoteCluster, String leaderIndex, Status status, - FollowParameters parameters) { + public FollowerInfo( + String followerIndex, + String remoteCluster, + String leaderIndex, + Status status, + FollowParameters parameters + ) { this.followerIndex = followerIndex; this.remoteCluster = remoteCluster; this.leaderIndex = leaderIndex; @@ -214,11 +218,11 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; FollowerInfo that = (FollowerInfo) o; - return Objects.equals(followerIndex, that.followerIndex) && - Objects.equals(remoteCluster, that.remoteCluster) && - Objects.equals(leaderIndex, that.leaderIndex) && - status == that.status && - Objects.equals(parameters, that.parameters); + return Objects.equals(followerIndex, that.followerIndex) + && Objects.equals(remoteCluster, that.remoteCluster) + && Objects.equals(leaderIndex, that.leaderIndex) + && status == that.status + && Objects.equals(parameters, that.parameters); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/FollowParameters.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/FollowParameters.java index efa670de62fdb..89c723bb2edca 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/FollowParameters.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/FollowParameters.java @@ -7,7 +7,6 @@ package org.elasticsearch.xpack.core.ccr.action; import org.elasticsearch.action.ActionRequestValidationException; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; @@ -15,6 +14,7 @@ import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.AbstractObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -49,8 +49,7 @@ public class FollowParameters implements Writeable, ToXContentObject { TimeValue maxRetryDelay; TimeValue readPollTimeout; - public FollowParameters() { - } + public FollowParameters() {} public FollowParameters(FollowParameters source) { this.maxReadRequestOperationCount = source.maxReadRequestOperationCount; @@ -173,13 +172,17 @@ public ActionRequestValidationException validate() { e = addValidationError(MAX_WRITE_BUFFER_SIZE.getPreferredName() + " must be larger than 0", e); } if (maxRetryDelay != null && maxRetryDelay.millis() <= 0) { - String message = "[" + MAX_RETRY_DELAY.getPreferredName() + "] must be positive but was [" + - maxRetryDelay.getStringRep() + "]"; + String message = "[" + MAX_RETRY_DELAY.getPreferredName() + "] must be positive but was [" + maxRetryDelay.getStringRep() + "]"; e = addValidationError(message, e); } if (maxRetryDelay != null && maxRetryDelay.millis() > RETRY_DELAY_MAX.millis()) { - String message = "[" + MAX_RETRY_DELAY.getPreferredName() + "] must be less than [" + RETRY_DELAY_MAX.getStringRep() + - "] but was [" + maxRetryDelay.getStringRep() + "]"; + String message = "[" + + MAX_RETRY_DELAY.getPreferredName() + + "] must be less than [" + + RETRY_DELAY_MAX.getStringRep() + + "] but was [" + + maxRetryDelay.getStringRep() + + "]"; e = addValidationError(message, e); } @@ -268,24 +271,33 @@ public static

    void initParser(AbstractObjectParser< FollowParameters::setMaxReadRequestSize, (p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), MAX_READ_REQUEST_SIZE.getPreferredName()), MAX_READ_REQUEST_SIZE, - ObjectParser.ValueType.STRING); + ObjectParser.ValueType.STRING + ); parser.declareField( FollowParameters::setMaxWriteRequestSize, (p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), MAX_WRITE_REQUEST_SIZE.getPreferredName()), MAX_WRITE_REQUEST_SIZE, - ObjectParser.ValueType.STRING); + ObjectParser.ValueType.STRING + ); parser.declareInt(FollowParameters::setMaxWriteBufferCount, MAX_WRITE_BUFFER_COUNT); parser.declareField( FollowParameters::setMaxWriteBufferSize, (p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), MAX_WRITE_BUFFER_SIZE.getPreferredName()), MAX_WRITE_BUFFER_SIZE, - ObjectParser.ValueType.STRING); - parser.declareField(FollowParameters::setMaxRetryDelay, + ObjectParser.ValueType.STRING + ); + parser.declareField( + FollowParameters::setMaxRetryDelay, (p, c) -> TimeValue.parseTimeValue(p.text(), MAX_RETRY_DELAY.getPreferredName()), - MAX_RETRY_DELAY, ObjectParser.ValueType.STRING); - parser.declareField(FollowParameters::setReadPollTimeout, + MAX_RETRY_DELAY, + ObjectParser.ValueType.STRING + ); + parser.declareField( + FollowParameters::setReadPollTimeout, (p, c) -> TimeValue.parseTimeValue(p.text(), READ_POLL_TIMEOUT.getPreferredName()), - READ_POLL_TIMEOUT, ObjectParser.ValueType.STRING); + READ_POLL_TIMEOUT, + ObjectParser.ValueType.STRING + ); } @Override @@ -293,16 +305,16 @@ public boolean equals(Object o) { if (this == o) return true; if (o instanceof FollowParameters == false) return false; FollowParameters that = (FollowParameters) o; - return Objects.equals(maxReadRequestOperationCount, that.maxReadRequestOperationCount) && - Objects.equals(maxWriteRequestOperationCount, that.maxWriteRequestOperationCount) && - Objects.equals(maxOutstandingReadRequests, that.maxOutstandingReadRequests) && - Objects.equals(maxOutstandingWriteRequests, that.maxOutstandingWriteRequests) && - Objects.equals(maxReadRequestSize, that.maxReadRequestSize) && - Objects.equals(maxWriteRequestSize, that.maxWriteRequestSize) && - Objects.equals(maxWriteBufferCount, that.maxWriteBufferCount) && - Objects.equals(maxWriteBufferSize, that.maxWriteBufferSize) && - Objects.equals(maxRetryDelay, that.maxRetryDelay) && - Objects.equals(readPollTimeout, that.readPollTimeout); + return Objects.equals(maxReadRequestOperationCount, that.maxReadRequestOperationCount) + && Objects.equals(maxWriteRequestOperationCount, that.maxWriteRequestOperationCount) + && Objects.equals(maxOutstandingReadRequests, that.maxOutstandingReadRequests) + && Objects.equals(maxOutstandingWriteRequests, that.maxOutstandingWriteRequests) + && Objects.equals(maxReadRequestSize, that.maxReadRequestSize) + && Objects.equals(maxWriteRequestSize, that.maxWriteRequestSize) + && Objects.equals(maxWriteBufferCount, that.maxWriteBufferCount) + && Objects.equals(maxWriteBufferSize, that.maxWriteBufferSize) + && Objects.equals(maxRetryDelay, that.maxRetryDelay) + && Objects.equals(readPollTimeout, that.readPollTimeout); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/FollowStatsAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/FollowStatsAction.java index aa68cbde2be6d..977d269db588a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/FollowStatsAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/FollowStatsAction.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ccr.action; -import org.elasticsearch.action.ActionType; import org.elasticsearch.action.ActionRequestValidationException; +import org.elasticsearch.action.ActionType; import org.elasticsearch.action.FailedNodeException; import org.elasticsearch.action.IndicesRequest; import org.elasticsearch.action.TaskOperationFailure; @@ -18,9 +18,9 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.tasks.Task; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.tasks.Task; import org.elasticsearch.xpack.core.ccr.ShardFollowNodeTaskStatus; import java.io.IOException; @@ -49,9 +49,10 @@ public List getStatsResponses() { } public StatsResponses( - final List taskFailures, - final List nodeFailures, - final List statsResponse) { + final List taskFailures, + final List nodeFailures, + final List statsResponse + ) { super(taskFailures, nodeFailures); this.statsResponse = statsResponse; } @@ -72,9 +73,8 @@ public XContentBuilder toXContent(final XContentBuilder builder, final Params pa // sort by index name, then shard ID final Map> taskResponsesByIndex = new TreeMap<>(); for (final StatsResponse statsResponse : statsResponse) { - taskResponsesByIndex.computeIfAbsent( - statsResponse.status().followerIndex(), - k -> new TreeMap<>()).put(statsResponse.status().getShardId(), statsResponse); + taskResponsesByIndex.computeIfAbsent(statsResponse.status().followerIndex(), k -> new TreeMap<>()) + .put(statsResponse.status().getShardId(), statsResponse); } builder.startObject(); { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/ForgetFollowerAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/ForgetFollowerAction.java index c62ee935bc2fa..bf01216f9fe34 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/ForgetFollowerAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/ForgetFollowerAction.java @@ -11,10 +11,10 @@ import org.elasticsearch.action.ActionType; import org.elasticsearch.action.support.broadcast.BroadcastRequest; import org.elasticsearch.action.support.broadcast.BroadcastResponse; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -49,9 +49,7 @@ public static class Request extends BroadcastRequest { PARSER.declareString((parameters, value) -> parameters[3] = value, LEADER_REMOTE_CLUSTER); } - public static ForgetFollowerAction.Request fromXContent( - final XContentParser parser, - final String leaderIndex) throws IOException { + public static ForgetFollowerAction.Request fromXContent(final XContentParser parser, final String leaderIndex) throws IOException { final String[] parameters = PARSER.parse(parser, null); return new Request(parameters[0], parameters[1], parameters[2], parameters[3], leaderIndex); } @@ -130,12 +128,13 @@ public Request(StreamInput in) throws IOException { * @param leaderIndex the name of the leader index */ public Request( - final String followerCluster, - final String followerIndex, - final String followerIndexUUID, - final String leaderRemoteCluster, - final String leaderIndex) { - super(new String[]{leaderIndex}); + final String followerCluster, + final String followerIndex, + final String followerIndexUUID, + final String leaderRemoteCluster, + final String leaderIndex + ) { + super(new String[] { leaderIndex }); this.followerCluster = Objects.requireNonNull(followerCluster); this.leaderIndex = Objects.requireNonNull(leaderIndex); this.leaderRemoteCluster = Objects.requireNonNull(leaderRemoteCluster); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/GetAutoFollowPatternAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/GetAutoFollowPatternAction.java index b366c3b8e515b..ddab94431f29f 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/GetAutoFollowPatternAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/GetAutoFollowPatternAction.java @@ -34,8 +34,7 @@ public static class Request extends MasterNodeReadRequest { private String name; - public Request() { - } + public Request() {} public Request(StreamInput in) throws IOException { super(in); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/ImmutableFollowParameters.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/ImmutableFollowParameters.java index c93023188bc6a..4d5d967ff2e88 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/ImmutableFollowParameters.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/ImmutableFollowParameters.java @@ -31,11 +31,18 @@ public class ImmutableFollowParameters implements Writeable { private final TimeValue maxRetryDelay; private final TimeValue readPollTimeout; - public ImmutableFollowParameters(Integer maxReadRequestOperationCount, Integer maxWriteRequestOperationCount, - Integer maxOutstandingReadRequests, Integer maxOutstandingWriteRequests, - ByteSizeValue maxReadRequestSize, ByteSizeValue maxWriteRequestSize, - Integer maxWriteBufferCount, ByteSizeValue maxWriteBufferSize, - TimeValue maxRetryDelay, TimeValue readPollTimeout) { + public ImmutableFollowParameters( + Integer maxReadRequestOperationCount, + Integer maxWriteRequestOperationCount, + Integer maxOutstandingReadRequests, + Integer maxOutstandingWriteRequests, + ByteSizeValue maxReadRequestSize, + ByteSizeValue maxWriteRequestSize, + Integer maxWriteBufferCount, + ByteSizeValue maxWriteBufferSize, + TimeValue maxRetryDelay, + TimeValue readPollTimeout + ) { this.maxReadRequestOperationCount = maxReadRequestOperationCount; this.maxWriteRequestOperationCount = maxWriteRequestOperationCount; this.maxOutstandingReadRequests = maxOutstandingReadRequests; @@ -159,24 +166,33 @@ public static

    void initParser(Constructing ConstructingObjectParser.optionalConstructorArg(), (p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), FollowParameters.MAX_READ_REQUEST_SIZE.getPreferredName()), FollowParameters.MAX_READ_REQUEST_SIZE, - ObjectParser.ValueType.STRING); + ObjectParser.ValueType.STRING + ); parser.declareField( ConstructingObjectParser.optionalConstructorArg(), (p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), FollowParameters.MAX_WRITE_REQUEST_SIZE.getPreferredName()), FollowParameters.MAX_WRITE_REQUEST_SIZE, - ObjectParser.ValueType.STRING); + ObjectParser.ValueType.STRING + ); parser.declareInt(ConstructingObjectParser.optionalConstructorArg(), FollowParameters.MAX_WRITE_BUFFER_COUNT); parser.declareField( ConstructingObjectParser.optionalConstructorArg(), (p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), FollowParameters.MAX_WRITE_BUFFER_SIZE.getPreferredName()), FollowParameters.MAX_WRITE_BUFFER_SIZE, - ObjectParser.ValueType.STRING); - parser.declareField(ConstructingObjectParser.optionalConstructorArg(), + ObjectParser.ValueType.STRING + ); + parser.declareField( + ConstructingObjectParser.optionalConstructorArg(), (p, c) -> TimeValue.parseTimeValue(p.text(), FollowParameters.MAX_RETRY_DELAY.getPreferredName()), - FollowParameters.MAX_RETRY_DELAY, ObjectParser.ValueType.STRING); - parser.declareField(ConstructingObjectParser.optionalConstructorArg(), + FollowParameters.MAX_RETRY_DELAY, + ObjectParser.ValueType.STRING + ); + parser.declareField( + ConstructingObjectParser.optionalConstructorArg(), (p, c) -> TimeValue.parseTimeValue(p.text(), FollowParameters.READ_POLL_TIMEOUT.getPreferredName()), - FollowParameters.READ_POLL_TIMEOUT, ObjectParser.ValueType.STRING); + FollowParameters.READ_POLL_TIMEOUT, + ObjectParser.ValueType.STRING + ); } @Override @@ -184,16 +200,16 @@ public boolean equals(Object o) { if (this == o) return true; if (o instanceof ImmutableFollowParameters == false) return false; ImmutableFollowParameters that = (ImmutableFollowParameters) o; - return Objects.equals(maxReadRequestOperationCount, that.maxReadRequestOperationCount) && - Objects.equals(maxWriteRequestOperationCount, that.maxWriteRequestOperationCount) && - Objects.equals(maxOutstandingReadRequests, that.maxOutstandingReadRequests) && - Objects.equals(maxOutstandingWriteRequests, that.maxOutstandingWriteRequests) && - Objects.equals(maxReadRequestSize, that.maxReadRequestSize) && - Objects.equals(maxWriteRequestSize, that.maxWriteRequestSize) && - Objects.equals(maxWriteBufferCount, that.maxWriteBufferCount) && - Objects.equals(maxWriteBufferSize, that.maxWriteBufferSize) && - Objects.equals(maxRetryDelay, that.maxRetryDelay) && - Objects.equals(readPollTimeout, that.readPollTimeout); + return Objects.equals(maxReadRequestOperationCount, that.maxReadRequestOperationCount) + && Objects.equals(maxWriteRequestOperationCount, that.maxWriteRequestOperationCount) + && Objects.equals(maxOutstandingReadRequests, that.maxOutstandingReadRequests) + && Objects.equals(maxOutstandingWriteRequests, that.maxOutstandingWriteRequests) + && Objects.equals(maxReadRequestSize, that.maxReadRequestSize) + && Objects.equals(maxWriteRequestSize, that.maxWriteRequestSize) + && Objects.equals(maxWriteBufferCount, that.maxWriteBufferCount) + && Objects.equals(maxWriteBufferSize, that.maxWriteBufferSize) + && Objects.equals(maxRetryDelay, that.maxRetryDelay) + && Objects.equals(readPollTimeout, that.readPollTimeout); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/PutAutoFollowPatternAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/PutAutoFollowPatternAction.java index 73aef1e5c3a98..a3154cfec7925 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/PutAutoFollowPatternAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/PutAutoFollowPatternAction.java @@ -45,8 +45,10 @@ public static class Request extends AcknowledgedRequest implements ToXC // Note that Request should be the Value class here for this parser with a 'parameters' field that maps to // PutAutoFollowPatternParameters class. But since two minor version are already released with duplicate // follow parameters in several APIs, PutAutoFollowPatternParameters is now the Value class here. - private static final ObjectParser PARSER = - new ObjectParser<>("put_auto_follow_pattern_request", PutAutoFollowPatternParameters::new); + private static final ObjectParser PARSER = new ObjectParser<>( + "put_auto_follow_pattern_request", + PutAutoFollowPatternParameters::new + ); static { PARSER.declareString((params, value) -> params.remoteCluster = value, REMOTE_CLUSTER_FIELD); @@ -83,8 +85,7 @@ public static Request fromXContent(XContentParser parser, String name) throws IO private FollowParameters parameters = new FollowParameters(); private List leaderIndexExclusionPatterns = Collections.emptyList(); - public Request() { - } + public Request() {} @Override public ActionRequestValidationException validate() { @@ -101,17 +102,23 @@ public ActionRequestValidationException validate() { } int byteCount = name.getBytes(StandardCharsets.UTF_8).length; if (byteCount > MAX_NAME_BYTES) { - validationException = addValidationError("[name] name is too long (" + byteCount + " > " + MAX_NAME_BYTES + ")", - validationException); + validationException = addValidationError( + "[name] name is too long (" + byteCount + " > " + MAX_NAME_BYTES + ")", + validationException + ); } } if (remoteCluster == null) { - validationException = addValidationError("[" + REMOTE_CLUSTER_FIELD.getPreferredName() + - "] is missing", validationException); + validationException = addValidationError( + "[" + REMOTE_CLUSTER_FIELD.getPreferredName() + "] is missing", + validationException + ); } if (leaderIndexPatterns == null || leaderIndexPatterns.isEmpty()) { - validationException = addValidationError("[" + AutoFollowPattern.LEADER_PATTERNS_FIELD.getPreferredName() + - "] is missing", validationException); + validationException = addValidationError( + "[" + AutoFollowPattern.LEADER_PATTERNS_FIELD.getPreferredName() + "] is missing", + validationException + ); } return validationException; } @@ -231,12 +238,12 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Request request = (Request) o; - return Objects.equals(name, request.name) && - Objects.equals(remoteCluster, request.remoteCluster) && - Objects.equals(leaderIndexPatterns, request.leaderIndexPatterns) && - Objects.equals(leaderIndexExclusionPatterns, request.leaderIndexExclusionPatterns) && - Objects.equals(followIndexNamePattern, request.followIndexNamePattern) && - Objects.equals(parameters, request.parameters); + return Objects.equals(name, request.name) + && Objects.equals(remoteCluster, request.remoteCluster) + && Objects.equals(leaderIndexPatterns, request.leaderIndexPatterns) + && Objects.equals(leaderIndexExclusionPatterns, request.leaderIndexExclusionPatterns) + && Objects.equals(followIndexNamePattern, request.followIndexNamePattern) + && Objects.equals(parameters, request.parameters); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/PutFollowAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/PutFollowAction.java index de6742e345b95..cf4846b761041 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/PutFollowAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/PutFollowAction.java @@ -15,11 +15,11 @@ import org.elasticsearch.action.support.ActiveShardCount; import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.action.support.master.AcknowledgedRequest; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -81,8 +81,7 @@ public static Request fromXContent(final XContentParser parser, final String fol private FollowParameters parameters = new FollowParameters(); private ActiveShardCount waitForActiveShards = ActiveShardCount.NONE; - public Request() { - } + public Request() {} public String getFollowerIndex() { return followerIndex; @@ -162,7 +161,7 @@ public ActionRequestValidationException validate() { @Override public String[] indices() { - return new String[]{followerIndex}; + return new String[] { followerIndex }; } @Override @@ -219,11 +218,11 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Request request = (Request) o; - return Objects.equals(remoteCluster, request.remoteCluster) && - Objects.equals(leaderIndex, request.leaderIndex) && - Objects.equals(followerIndex, request.followerIndex) && - Objects.equals(parameters, request.parameters) && - Objects.equals(waitForActiveShards, request.waitForActiveShards); + return Objects.equals(remoteCluster, request.remoteCluster) + && Objects.equals(leaderIndex, request.leaderIndex) + && Objects.equals(followerIndex, request.followerIndex) + && Objects.equals(parameters, request.parameters) + && Objects.equals(waitForActiveShards, request.waitForActiveShards); } @Override @@ -297,9 +296,9 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Response response = (Response) o; - return followIndexCreated == response.followIndexCreated && - followIndexShardsAcked == response.followIndexShardsAcked && - indexFollowingStarted == response.indexFollowingStarted; + return followIndexCreated == response.followIndexCreated + && followIndexShardsAcked == response.followIndexShardsAcked + && indexFollowingStarted == response.indexFollowingStarted; } @Override @@ -309,11 +308,14 @@ public int hashCode() { @Override public String toString() { - return "PutFollowAction.Response{" + - "followIndexCreated=" + followIndexCreated + - ", followIndexShardsAcked=" + followIndexShardsAcked + - ", indexFollowingStarted=" + indexFollowingStarted + - '}'; + return "PutFollowAction.Response{" + + "followIndexCreated=" + + followIndexCreated + + ", followIndexShardsAcked=" + + followIndexShardsAcked + + ", indexFollowingStarted=" + + indexFollowingStarted + + '}'; } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/ResumeFollowAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/ResumeFollowAction.java index 0a6a1422be09e..0e8c95bde2bba 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/ResumeFollowAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/ResumeFollowAction.java @@ -54,8 +54,7 @@ public static Request fromXContent(final XContentParser parser, final String fol private String followerIndex; private FollowParameters parameters = new FollowParameters(); - public Request() { - } + public Request() {} public String getFollowerIndex() { return followerIndex; @@ -110,8 +109,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Request request = (Request) o; - return Objects.equals(followerIndex, request.followerIndex) && - Objects.equals(parameters, request.parameters); + return Objects.equals(followerIndex, request.followerIndex) && Objects.equals(parameters, request.parameters); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/ShardFollowTask.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/ShardFollowTask.java index 90bd9e8d25e28..bca6529bc4efc 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/ShardFollowTask.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/ShardFollowTask.java @@ -8,17 +8,17 @@ package org.elasticsearch.xpack.core.ccr.action; import org.elasticsearch.Version; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.index.shard.ShardId; +import org.elasticsearch.persistent.PersistentTaskParams; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.index.shard.ShardId; -import org.elasticsearch.persistent.PersistentTaskParams; import java.io.IOException; import java.util.Collections; @@ -39,11 +39,25 @@ public class ShardFollowTask extends ImmutableFollowParameters implements Persis private static final ParseField HEADERS = new ParseField("headers"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(NAME, - (a) -> new ShardFollowTask((String) a[0], - new ShardId((String) a[1], (String) a[2], (int) a[3]), new ShardId((String) a[4], (String) a[5], (int) a[6]), - (Integer) a[7], (Integer) a[8], (Integer) a[9], (Integer) a[10], (ByteSizeValue) a[11], (ByteSizeValue) a[12], - (Integer) a[13], (ByteSizeValue) a[14], (TimeValue) a[15], (TimeValue) a[16], (Map) a[17])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + (a) -> new ShardFollowTask( + (String) a[0], + new ShardId((String) a[1], (String) a[2], (int) a[3]), + new ShardId((String) a[4], (String) a[5], (int) a[6]), + (Integer) a[7], + (Integer) a[8], + (Integer) a[9], + (Integer) a[10], + (ByteSizeValue) a[11], + (ByteSizeValue) a[12], + (Integer) a[13], + (ByteSizeValue) a[14], + (TimeValue) a[15], + (TimeValue) a[16], + (Map) a[17] + ) + ); static { PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), REMOTE_CLUSTER_FIELD); @@ -63,22 +77,33 @@ public class ShardFollowTask extends ImmutableFollowParameters implements Persis private final Map headers; public ShardFollowTask( - final String remoteCluster, - final ShardId followShardId, - final ShardId leaderShardId, - final int maxReadRequestOperationCount, - final int maxWriteRequestOperationCount, - final int maxOutstandingReadRequests, - final int maxOutstandingWriteRequests, - final ByteSizeValue maxReadRequestSize, - final ByteSizeValue maxWriteRequestSize, - final int maxWriteBufferCount, - final ByteSizeValue maxWriteBufferSize, - final TimeValue maxRetryDelay, - final TimeValue readPollTimeout, - final Map headers) { - super(maxReadRequestOperationCount, maxWriteRequestOperationCount, maxOutstandingReadRequests, maxOutstandingWriteRequests, - maxReadRequestSize, maxWriteRequestSize, maxWriteBufferCount, maxWriteBufferSize, maxRetryDelay, readPollTimeout); + final String remoteCluster, + final ShardId followShardId, + final ShardId leaderShardId, + final int maxReadRequestOperationCount, + final int maxWriteRequestOperationCount, + final int maxOutstandingReadRequests, + final int maxOutstandingWriteRequests, + final ByteSizeValue maxReadRequestSize, + final ByteSizeValue maxWriteRequestSize, + final int maxWriteBufferCount, + final ByteSizeValue maxWriteBufferSize, + final TimeValue maxRetryDelay, + final TimeValue readPollTimeout, + final Map headers + ) { + super( + maxReadRequestOperationCount, + maxWriteRequestOperationCount, + maxOutstandingReadRequests, + maxOutstandingWriteRequests, + maxReadRequestSize, + maxWriteRequestSize, + maxWriteBufferCount, + maxWriteBufferSize, + maxRetryDelay, + readPollTimeout + ); this.remoteCluster = remoteCluster; this.followShardId = followShardId; this.leaderShardId = leaderShardId; @@ -155,21 +180,15 @@ public boolean equals(Object o) { if (o == null || getClass() != o.getClass()) return false; if (super.equals(o) == false) return false; ShardFollowTask that = (ShardFollowTask) o; - return Objects.equals(remoteCluster, that.remoteCluster) && - Objects.equals(followShardId, that.followShardId) && - Objects.equals(leaderShardId, that.leaderShardId) && - Objects.equals(headers, that.headers); + return Objects.equals(remoteCluster, that.remoteCluster) + && Objects.equals(followShardId, that.followShardId) + && Objects.equals(leaderShardId, that.leaderShardId) + && Objects.equals(headers, that.headers); } @Override public int hashCode() { - return Objects.hash( - super.hashCode(), - remoteCluster, - followShardId, - leaderShardId, - headers - ); + return Objects.hash(super.hashCode(), remoteCluster, followShardId, leaderShardId, headers); } public String toString() { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/UnfollowAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/UnfollowAction.java index f4a4d2ce439ab..630496c822050 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/UnfollowAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ccr/action/UnfollowAction.java @@ -48,7 +48,7 @@ public String getFollowerIndex() { @Override public String[] indices() { - return new String[] {followerIndex}; + return new String[] { followerIndex }; } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/common/IteratingActionListener.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/common/IteratingActionListener.java index f6699fd4ff309..fa94a7b0649dc 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/common/IteratingActionListener.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/common/IteratingActionListener.java @@ -48,8 +48,12 @@ public final class IteratingActionListener implements ActionListener, R * @param consumables the instances that can be consumed to produce a response which is ultimately sent on the delegate listener * @param threadContext the thread context for the thread pool that created the listener */ - public IteratingActionListener(ActionListener delegate, BiConsumer> consumer, List consumables, - ThreadContext threadContext) { + public IteratingActionListener( + ActionListener delegate, + BiConsumer> consumer, + List consumables, + ThreadContext threadContext + ) { this(delegate, consumer, consumables, threadContext, Function.identity()); } @@ -64,8 +68,13 @@ public IteratingActionListener(ActionListener delegate, BiConsumer delegate, BiConsumer> consumer, List consumables, - ThreadContext threadContext, Function finalResultFunction) { + public IteratingActionListener( + ActionListener delegate, + BiConsumer> consumer, + List consumables, + ThreadContext threadContext, + Function finalResultFunction + ) { this(delegate, consumer, consumables, threadContext, finalResultFunction, Objects::isNull); } @@ -81,9 +90,14 @@ public IteratingActionListener(ActionListener delegate, BiConsumer delegate, BiConsumer> consumer, List consumables, - ThreadContext threadContext, Function finalResultFunction, - Predicate iterationPredicate) { + public IteratingActionListener( + ActionListener delegate, + BiConsumer> consumer, + List consumables, + ThreadContext threadContext, + Function finalResultFunction, + Predicate iterationPredicate + ) { this.delegate = delegate; this.consumer = consumer; this.consumables = Collections.unmodifiableList(consumables); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/common/notifications/AbstractAuditMessage.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/common/notifications/AbstractAuditMessage.java index 52ac4a1aaf04e..9deb38b4ff2c7 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/common/notifications/AbstractAuditMessage.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/common/notifications/AbstractAuditMessage.java @@ -6,9 +6,9 @@ */ package org.elasticsearch.xpack.core.common.notifications; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -38,20 +38,26 @@ public abstract class AbstractAuditMessage implements ToXContentObject { public static final int MAX_AUDIT_MESSAGE_CHARS = 8191; protected static final ConstructingObjectParser createParser( - String name, AbstractAuditMessageFactory messageFactory, ParseField resourceField) { + String name, + AbstractAuditMessageFactory messageFactory, + ParseField resourceField + ) { ConstructingObjectParser PARSER = new ConstructingObjectParser<>( name, true, - a -> messageFactory.newMessage((String)a[0], (String)a[1], (Level)a[2], (Date)a[3], (String)a[4])); + a -> messageFactory.newMessage((String) a[0], (String) a[1], (Level) a[2], (Date) a[3], (String) a[4]) + ); PARSER.declareString(optionalConstructorArg(), resourceField); PARSER.declareString(constructorArg(), MESSAGE); PARSER.declareString(constructorArg(), Level::fromString, LEVEL); - PARSER.declareField(constructorArg(), + PARSER.declareField( + constructorArg(), p -> TimeUtils.parseTimeField(p, TIMESTAMP.getPreferredName()), TIMESTAMP, - ObjectParser.ValueType.VALUE); + ObjectParser.ValueType.VALUE + ); PARSER.declareString(optionalConstructorArg(), NODE_NAME); return PARSER; @@ -132,12 +138,12 @@ public boolean equals(Object obj) { } AbstractAuditMessage other = (AbstractAuditMessage) obj; - return Objects.equals(resourceId, other.resourceId) && - Objects.equals(message, other.message) && - Objects.equals(level, other.level) && - Objects.equals(timestamp, other.timestamp) && - Objects.equals(nodeName, other.nodeName) && - Objects.equals(getJobType(), other.getJobType()); + return Objects.equals(resourceId, other.resourceId) + && Objects.equals(message, other.message) + && Objects.equals(level, other.level) + && Objects.equals(timestamp, other.timestamp) + && Objects.equals(nodeName, other.nodeName) + && Objects.equals(getJobType(), other.getJobType()); } /** diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/common/notifications/AbstractAuditor.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/common/notifications/AbstractAuditor.java index b2175dd540354..42aeb3baba3b8 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/common/notifications/AbstractAuditor.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/common/notifications/AbstractAuditor.java @@ -57,34 +57,41 @@ public abstract class AbstractAuditor { private final ClusterService clusterService; private final AtomicBoolean putTemplateInProgress; - protected AbstractAuditor(OriginSettingClient client, - String auditIndex, - IndexTemplateConfig templateConfig, - String nodeName, - AbstractAuditMessageFactory messageFactory, - ClusterService clusterService) { - - this(client, auditIndex, templateConfig.getTemplateName(), - () -> { - try { - return new PutComposableIndexTemplateAction.Request(templateConfig.getTemplateName()) - .indexTemplate(ComposableIndexTemplate.parse(JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, templateConfig.loadBytes()))) - .masterNodeTimeout(MASTER_TIMEOUT); - } catch (IOException e) { - throw new ElasticsearchParseException("unable to parse composable template " + templateConfig.getTemplateName(), e); - } - }, - nodeName, messageFactory, clusterService); + protected AbstractAuditor( + OriginSettingClient client, + String auditIndex, + IndexTemplateConfig templateConfig, + String nodeName, + AbstractAuditMessageFactory messageFactory, + ClusterService clusterService + ) { + + this(client, auditIndex, templateConfig.getTemplateName(), () -> { + try { + return new PutComposableIndexTemplateAction.Request(templateConfig.getTemplateName()).indexTemplate( + ComposableIndexTemplate.parse( + JsonXContent.jsonXContent.createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + templateConfig.loadBytes() + ) + ) + ).masterNodeTimeout(MASTER_TIMEOUT); + } catch (IOException e) { + throw new ElasticsearchParseException("unable to parse composable template " + templateConfig.getTemplateName(), e); + } + }, nodeName, messageFactory, clusterService); } - protected AbstractAuditor(OriginSettingClient client, - String auditIndex, - String templateName, - Supplier templateSupplier, - String nodeName, - AbstractAuditMessageFactory messageFactory, - ClusterService clusterService) { + protected AbstractAuditor( + OriginSettingClient client, + String auditIndex, + String templateName, + Supplier templateSupplier, + String nodeName, + AbstractAuditMessageFactory messageFactory, + ClusterService clusterService + ) { this.client = Objects.requireNonNull(client); this.auditIndex = Objects.requireNonNull(auditIndex); this.templateName = Objects.requireNonNull(templateName); @@ -132,21 +139,18 @@ protected void indexDoc(ToXContent toXContent) { return; } - ActionListener putTemplateListener = ActionListener.wrap( - r -> { - synchronized (this) { - // synchronized so nothing can be added to backlog while this value changes - hasLatestTemplate.set(true); - } - logger.info("Auditor template [{}] successfully installed", templateName); - writeBacklog(); - putTemplateInProgress.set(false); - }, - e -> { - logger.warn("Error putting latest template [{}]", templateName); - putTemplateInProgress.set(false); + ActionListener putTemplateListener = ActionListener.wrap(r -> { + synchronized (this) { + // synchronized so nothing can be added to backlog while this value changes + hasLatestTemplate.set(true); } - ); + logger.info("Auditor template [{}] successfully installed", templateName); + writeBacklog(); + putTemplateInProgress.set(false); + }, e -> { + logger.warn("Error putting latest template [{}]", templateName); + putTemplateInProgress.set(false); + }); synchronized (this) { if (hasLatestTemplate.get() == false) { @@ -164,21 +168,22 @@ protected void indexDoc(ToXContent toXContent) { // stop multiple invocations if (putTemplateInProgress.compareAndSet(false, true)) { - MlIndexAndAlias.installIndexTemplateIfRequired(clusterService.state(), client, - templateSupplier.get(), putTemplateListener); + MlIndexAndAlias.installIndexTemplateIfRequired( + clusterService.state(), + client, + templateSupplier.get(), + putTemplateListener + ); } return; } } indexDoc(toXContent); - } + } private void writeDoc(ToXContent toXContent) { - client.index(indexRequest(toXContent), ActionListener.wrap( - this::onIndexResponse, - this::onIndexFailure - )); + client.index(indexRequest(toXContent), ActionListener.wrap(this::onIndexResponse, this::onIndexFailure)); } private IndexRequest indexRequest(ToXContent toXContent) { @@ -214,17 +219,14 @@ protected void writeBacklog() { doc = backlog.poll(); } - client.bulk(bulkRequest, ActionListener.wrap( - bulkItemResponses -> { - if (bulkItemResponses.hasFailures()) { - logger.warn("Failures bulk indexing the message back log: {}", bulkItemResponses.buildFailureMessage()); - } else { - logger.trace("Successfully wrote audit message backlog after upgrading template"); - } - backlog = null; - }, - this::onIndexFailure - )); + client.bulk(bulkRequest, ActionListener.wrap(bulkItemResponses -> { + if (bulkItemResponses.hasFailures()) { + logger.warn("Failures bulk indexing the message back log: {}", bulkItemResponses.buildFailureMessage()); + } else { + logger.trace("Successfully wrote audit message backlog after upgrading template"); + } + backlog = null; + }, this::onIndexFailure)); } // for testing diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/common/notifications/Level.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/common/notifications/Level.java index 2bc20720df90e..2db973f8122c1 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/common/notifications/Level.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/common/notifications/Level.java @@ -9,7 +9,9 @@ import java.util.Locale; public enum Level { - INFO, WARNING, ERROR; + INFO, + WARNING, + ERROR; /** * Case-insensitive from string method. diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/common/search/aggregations/MissingHelper.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/common/search/aggregations/MissingHelper.java index 81032839bb592..74c28d3b28598 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/common/search/aggregations/MissingHelper.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/common/search/aggregations/MissingHelper.java @@ -7,9 +7,9 @@ package org.elasticsearch.xpack.core.common.search.aggregations; -import org.elasticsearch.core.Releasable; import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.common.util.BitArray; +import org.elasticsearch.core.Releasable; /** * Helps long-valued {@link org.elasticsearch.search.sort.BucketedSort.ExtraData} track "empty" slots. It attempts to have diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/common/socket/SocketAccess.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/common/socket/SocketAccess.java index fc78bc272104d..cccb43e885dfa 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/common/socket/SocketAccess.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/common/socket/SocketAccess.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.common.socket; import org.elasticsearch.SpecialPermission; -import org.elasticsearch.core.CheckedRunnable; import org.elasticsearch.common.CheckedSupplier; +import org.elasticsearch.core.CheckedRunnable; import java.io.IOException; import java.net.SocketPermission; @@ -24,8 +24,7 @@ */ public final class SocketAccess { - private SocketAccess() { - } + private SocketAccess() {} public static R doPrivileged(CheckedSupplier supplier) throws IOException { SpecialPermission.check(); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/common/stats/EnumCounters.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/common/stats/EnumCounters.java index b551e70e1dcde..98f66ab4587a5 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/common/stats/EnumCounters.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/common/stats/EnumCounters.java @@ -103,8 +103,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; EnumCounters that = (EnumCounters) o; - return Arrays.equals(toArray(), that.toArray()) && - Arrays.equals(enums, that.enums); + return Arrays.equals(toArray(), that.toArray()) && Arrays.equals(enums, that.enums); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/common/time/TimeUtils.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/common/time/TimeUtils.java index cea209a711b2a..398c5e848745f 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/common/time/TimeUtils.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/common/time/TimeUtils.java @@ -7,10 +7,10 @@ package org.elasticsearch.xpack.core.common.time; import org.elasticsearch.ElasticsearchParseException; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.index.mapper.DateFieldMapper; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.time.Instant; @@ -33,8 +33,7 @@ public static Date parseTimeField(XContentParser parser, String fieldName) throw } else if (parser.currentToken() == XContentParser.Token.VALUE_STRING) { return new Date(dateStringToEpoch(parser.text())); } - throw new IllegalArgumentException( - "unexpected token [" + parser.currentToken() + "] for [" + fieldName + "]"); + throw new IllegalArgumentException("unexpected token [" + parser.currentToken() + "] for [" + fieldName + "]"); } public static Instant parseTimeFieldToInstant(XContentParser parser, String fieldName) throws IOException { @@ -43,8 +42,7 @@ public static Instant parseTimeFieldToInstant(XContentParser parser, String fiel } else if (parser.currentToken() == XContentParser.Token.VALUE_STRING) { return Instant.from(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parse(parser.text())); } - throw new IllegalArgumentException( - "unexpected token [" + parser.currentToken() + "] for [" + fieldName + "]"); + throw new IllegalArgumentException("unexpected token [" + parser.currentToken() + "] for [" + fieldName + "]"); } /** @@ -104,8 +102,7 @@ public static long dateStringToEpoch(String date) { try { return DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parseMillis(date); - } catch (ElasticsearchParseException | IllegalArgumentException e) { - } + } catch (ElasticsearchParseException | IllegalArgumentException e) {} // Could not do the conversion return -1; } @@ -147,8 +144,9 @@ public static void checkPositiveMultiple(TimeValue timeValue, TimeUnit baseUnit, public static void checkPositive(TimeValue timeValue, ParseField field) { long nanos = timeValue.getNanos(); if (nanos <= 0) { - throw new IllegalArgumentException(field.getPreferredName() + " cannot be less or equal than 0. Value = " - + timeValue.toString()); + throw new IllegalArgumentException( + field.getPreferredName() + " cannot be less or equal than 0. Value = " + timeValue.toString() + ); } } @@ -167,8 +165,9 @@ public static void checkMultiple(TimeValue timeValue, TimeUnit baseUnit, ParseFi TimeValue base = new TimeValue(1, baseUnit); long baseNanos = base.getNanos(); if (nanos % baseNanos != 0) { - throw new IllegalArgumentException(field.getPreferredName() + " has to be a multiple of " + base.toString() + "; actual was '" - + timeValue.toString() + "'"); + throw new IllegalArgumentException( + field.getPreferredName() + " has to be a multiple of " + base.toString() + "; actual was '" + timeValue.toString() + "'" + ); } } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/common/validation/SourceDestValidator.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/common/validation/SourceDestValidator.java index ab4958f79e9b9..189945133e53a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/common/validation/SourceDestValidator.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/common/validation/SourceDestValidator.java @@ -14,10 +14,10 @@ import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.ValidationException; import org.elasticsearch.common.regex.Regex; +import org.elasticsearch.core.Nullable; import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexNotFoundException; import org.elasticsearch.indices.InvalidIndexNameException; @@ -59,7 +59,9 @@ public final class SourceDestValidator { public static final String DEST_LOWERCASE = "Destination index [{0}] must be lowercase"; public static final String NEEDS_REMOTE_CLUSTER_SEARCH = "Source index is configured with a remote index pattern(s) [{0}]" + " but the current node [{1}] is not allowed to connect to remote clusters." - + " Please enable " + REMOTE_CLUSTER_CLIENT_ROLE.roleName() + " for all {2} nodes."; + + " Please enable " + + REMOTE_CLUSTER_CLIENT_ROLE.roleName() + + " for all {2} nodes."; public static final String ERROR_REMOTE_CLUSTER_SEARCH = "Error resolving remote source: {0}"; public static final String UNKNOWN_REMOTE_CLUSTER_LICENSE = "Error during license check ({0}) for remote cluster " + "alias(es) {1}, error: {2}"; @@ -187,7 +189,8 @@ public String resolveDest() { IndicesOptions.lenientExpandOpen(), destIndex, true, - false); + false + ); resolvedDest = singleWriteIndex != null ? singleWriteIndex.getName() : destIndex; } catch (IllegalArgumentException e) { @@ -403,7 +406,8 @@ public void validate(Context context, ActionListener listener) { NEEDS_REMOTE_CLUSTER_SEARCH, context.resolveRemoteSource(), context.getNodeName(), - nodeRoleThatRequiresRemoteClusterClient); + nodeRoleThatRequiresRemoteClusterClient + ); listener.onResponse(context); return; } @@ -466,8 +470,10 @@ public void validate(Context context, ActionListener listener) { List remoteIndices = new ArrayList<>(context.resolveRemoteSource()); Map remoteClusterVersions; try { - List remoteAliases = - RemoteClusterLicenseChecker.remoteClusterAliases(context.getRegisteredRemoteClusterNames(), remoteIndices); + List remoteAliases = RemoteClusterLicenseChecker.remoteClusterAliases( + context.getRegisteredRemoteClusterNames(), + remoteIndices + ); remoteClusterVersions = remoteAliases.stream().collect(toMap(identity(), context::getRemoteClusterVersion)); } catch (NoSuchRemoteClusterException e) { context.addValidationError(e.getMessage()); @@ -478,19 +484,21 @@ public void validate(Context context, ActionListener listener) { listener.onResponse(context); return; } - Map oldRemoteClusterVersions = - remoteClusterVersions.entrySet().stream() - .filter(entry -> entry.getValue().before(minExpectedVersion)) - .collect(toMap(Map.Entry::getKey, Map.Entry::getValue)); + Map oldRemoteClusterVersions = remoteClusterVersions.entrySet() + .stream() + .filter(entry -> entry.getValue().before(minExpectedVersion)) + .collect(toMap(Map.Entry::getKey, Map.Entry::getValue)); if (oldRemoteClusterVersions.isEmpty() == false) { context.addValidationError( REMOTE_CLUSTERS_TOO_OLD, minExpectedVersion, reason, - oldRemoteClusterVersions.entrySet().stream() + oldRemoteClusterVersions.entrySet() + .stream() .sorted(comparingByKey()) // sort to have a deterministic order among clusters in the resulting string .map(e -> e.getKey() + " (" + e.getValue() + ")") - .collect(joining(", "))); + .collect(joining(", ")) + ); } listener.onResponse(context); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/datastreams/DataStreamFeatureSetUsage.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/datastreams/DataStreamFeatureSetUsage.java index beaf576498453..42a99b5c25af9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/datastreams/DataStreamFeatureSetUsage.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/datastreams/DataStreamFeatureSetUsage.java @@ -104,8 +104,7 @@ public boolean equals(Object obj) { return false; } DataStreamStats other = (DataStreamStats) obj; - return totalDataStreamCount == other.totalDataStreamCount && - indicesBehindDataStream == other.indicesBehindDataStream; + return totalDataStreamCount == other.totalDataStreamCount && indicesBehindDataStream == other.indicesBehindDataStream; } } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/deprecation/DeprecationIssue.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/deprecation/DeprecationIssue.java index 45cc4c975beb3..43553df5bceb9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/deprecation/DeprecationIssue.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/deprecation/DeprecationIssue.java @@ -10,9 +10,9 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.core.Nullable; import java.io.IOException; import java.util.Locale; @@ -33,8 +33,7 @@ public enum Level implements Writeable { /** * This issue must be resolved to upgrade. Failures will occur unless this is resolved before upgrading. */ - CRITICAL - ; + CRITICAL; public static Level fromString(String value) { return Level.valueOf(value.toUpperCase(Locale.ROOT)); @@ -66,8 +65,14 @@ public String toString() { private final boolean resolveDuringRollingUpgrade; private final Map meta; - public DeprecationIssue(Level level, String message, String url, @Nullable String details, boolean resolveDuringRollingUpgrade, - @Nullable Map meta) { + public DeprecationIssue( + Level level, + String message, + String url, + @Nullable String details, + boolean resolveDuringRollingUpgrade, + @Nullable Map meta + ) { this.level = level; this.message = message; this.url = url; @@ -128,10 +133,7 @@ public void writeTo(StreamOutput out) throws IOException { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.startObject() - .field("level", level) - .field("message", message) - .field("url", url); + builder.startObject().field("level", level).field("message", message).field("url", url); if (details != null) { builder.field("details", details); } @@ -151,12 +153,12 @@ public boolean equals(Object o) { return false; } DeprecationIssue that = (DeprecationIssue) o; - return Objects.equals(level, that.level) && - Objects.equals(message, that.message) && - Objects.equals(url, that.url) && - Objects.equals(details, that.details) && - Objects.equals(resolveDuringRollingUpgrade, that.resolveDuringRollingUpgrade) && - Objects.equals(meta, that.meta); + return Objects.equals(level, that.level) + && Objects.equals(message, that.message) + && Objects.equals(url, that.url) + && Objects.equals(details, that.details) + && Objects.equals(resolveDuringRollingUpgrade, that.resolveDuringRollingUpgrade) + && Objects.equals(meta, that.meta); } @Override @@ -169,4 +171,3 @@ public String toString() { return Strings.toString(this); } } - diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/deprecation/LoggingDeprecationAccumulationHandler.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/deprecation/LoggingDeprecationAccumulationHandler.java index a4e2f3a3caa5a..68034d2597617 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/deprecation/LoggingDeprecationAccumulationHandler.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/deprecation/LoggingDeprecationAccumulationHandler.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.deprecation; import org.elasticsearch.common.logging.LoggerMessageFormat; -import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.XContentLocation; import java.util.ArrayList; @@ -32,24 +32,30 @@ public class LoggingDeprecationAccumulationHandler implements DeprecationHandler public void logRenamedField(String parserName, Supplier location, String oldName, String currentName) { LoggingDeprecationHandler.INSTANCE.logRenamedField(parserName, location, oldName, currentName); String prefix = parserName == null ? "" : "[" + parserName + "][" + location.get() + "] "; - deprecations.add(LoggerMessageFormat.format("{}Deprecated field [{}] used, expected [{}] instead", - new Object[]{prefix, oldName, currentName})); + deprecations.add( + LoggerMessageFormat.format("{}Deprecated field [{}] used, expected [{}] instead", new Object[] { prefix, oldName, currentName }) + ); } @Override public void logReplacedField(String parserName, Supplier location, String oldName, String replacedName) { LoggingDeprecationHandler.INSTANCE.logReplacedField(parserName, location, oldName, replacedName); String prefix = parserName == null ? "" : "[" + parserName + "][" + location.get() + "] "; - deprecations.add(LoggerMessageFormat.format("{}Deprecated field [{}] used, replaced by [{}]", - new Object[]{prefix, oldName, replacedName})); + deprecations.add( + LoggerMessageFormat.format("{}Deprecated field [{}] used, replaced by [{}]", new Object[] { prefix, oldName, replacedName }) + ); } @Override public void logRemovedField(String parserName, Supplier location, String removedName) { LoggingDeprecationHandler.INSTANCE.logRemovedField(parserName, location, removedName); String prefix = parserName == null ? "" : "[" + parserName + "][" + location.get() + "] "; - deprecations.add(LoggerMessageFormat.format("{}Deprecated field [{}] used, unused and will be removed entirely", - new Object[]{prefix, removedName})); + deprecations.add( + LoggerMessageFormat.format( + "{}Deprecated field [{}] used, unused and will be removed entirely", + new Object[] { prefix, removedName } + ) + ); } /** diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/EnrichPolicy.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/EnrichPolicy.java index 033192fcfdc93..1beaa8451a53d 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/EnrichPolicy.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/EnrichPolicy.java @@ -7,18 +7,18 @@ package org.elasticsearch.xpack.core.enrich; import org.elasticsearch.Version; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentFragment; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentParser.Token; import org.elasticsearch.xcontent.XContentType; @@ -39,11 +39,7 @@ public final class EnrichPolicy implements Writeable, ToXContentFragment { public static final String MATCH_TYPE = "match"; public static final String GEO_MATCH_TYPE = "geo_match"; public static final String RANGE_TYPE = "range"; - public static final String[] SUPPORTED_POLICY_TYPES = new String[]{ - MATCH_TYPE, - GEO_MATCH_TYPE, - RANGE_TYPE - }; + public static final String[] SUPPORTED_POLICY_TYPES = new String[] { MATCH_TYPE, GEO_MATCH_TYPE, RANGE_TYPE }; private static final ParseField QUERY = new ParseField("query"); private static final ParseField INDICES = new ParseField("indices"); @@ -78,8 +74,12 @@ private static void declareCommonConstructorParsingOptions(ConstructingObjec parser.declareStringArray(ConstructingObjectParser.constructorArg(), INDICES); parser.declareString(ConstructingObjectParser.constructorArg(), MATCH_FIELD); parser.declareStringArray(ConstructingObjectParser.constructorArg(), ENRICH_FIELDS); - parser.declareField(ConstructingObjectParser.optionalConstructorArg(), ((p, c) -> Version.fromString(p.text())), - ELASTICSEARCH_VERSION, ValueType.STRING); + parser.declareField( + ConstructingObjectParser.optionalConstructorArg(), + ((p, c) -> Version.fromString(p.text())), + ELASTICSEARCH_VERSION, + ValueType.STRING + ); } public static EnrichPolicy fromXContent(XContentParser parser) throws IOException { @@ -121,20 +121,18 @@ public EnrichPolicy(StreamInput in) throws IOException { ); } - public EnrichPolicy(String type, - QuerySource query, - List indices, - String matchField, - List enrichFields) { + public EnrichPolicy(String type, QuerySource query, List indices, String matchField, List enrichFields) { this(type, query, indices, matchField, enrichFields, Version.CURRENT); } - public EnrichPolicy(String type, - QuerySource query, - List indices, - String matchField, - List enrichFields, - Version elasticsearchVersion) { + public EnrichPolicy( + String type, + QuerySource query, + List indices, + String matchField, + List enrichFields, + Version elasticsearchVersion + ) { this.type = type; this.query = query; this.indices = indices; @@ -208,24 +206,17 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; EnrichPolicy policy = (EnrichPolicy) o; - return type.equals(policy.type) && - Objects.equals(query, policy.query) && - indices.equals(policy.indices) && - matchField.equals(policy.matchField) && - enrichFields.equals(policy.enrichFields) && - elasticsearchVersion.equals(policy.elasticsearchVersion); + return type.equals(policy.type) + && Objects.equals(query, policy.query) + && indices.equals(policy.indices) + && matchField.equals(policy.matchField) + && enrichFields.equals(policy.enrichFields) + && elasticsearchVersion.equals(policy.elasticsearchVersion); } @Override public int hashCode() { - return Objects.hash( - type, - query, - indices, - matchField, - enrichFields, - elasticsearchVersion - ); + return Objects.hash(type, query, indices, matchField, enrichFields, elasticsearchVersion); } public String toString() { @@ -269,8 +260,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; QuerySource that = (QuerySource) o; - return query.equals(that.query) && - contentType == that.contentType; + return query.equals(that.query) && contentType == that.contentType; } @Override @@ -288,12 +278,14 @@ public static class NamedPolicy implements Writeable, ToXContentFragment { false, (args, policyType) -> new NamedPolicy( (String) args[0], - new EnrichPolicy(policyType, + new EnrichPolicy( + policyType, (QuerySource) args[1], (List) args[2], (String) args[3], (List) args[4], - (Version) args[5]) + (Version) args[5] + ) ) ); @@ -370,8 +362,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; NamedPolicy that = (NamedPolicy) o; - return name.equals(that.name) && - policy.equals(that.policy); + return name.equals(that.name) && policy.equals(that.policy); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/action/EnrichStatsAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/action/EnrichStatsAction.java index 6691de4cff7a2..7285ab2fb9f78 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/action/EnrichStatsAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/action/EnrichStatsAction.java @@ -14,10 +14,10 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.tasks.TaskInfo; import org.elasticsearch.xcontent.ToXContentFragment; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.tasks.TaskInfo; import java.io.IOException; import java.util.List; @@ -34,8 +34,7 @@ private EnrichStatsAction() { public static class Request extends MasterNodeRequest { - public Request() { - } + public Request() {} public Request(StreamInput in) throws IOException { super(in); @@ -122,9 +121,9 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Response response = (Response) o; - return executingPolicies.equals(response.executingPolicies) && - coordinatorStats.equals(response.coordinatorStats) && - Objects.equals(cacheStats, response.cacheStats); + return executingPolicies.equals(response.executingPolicies) + && coordinatorStats.equals(response.coordinatorStats) + && Objects.equals(cacheStats, response.cacheStats); } @Override @@ -140,11 +139,13 @@ public static class CoordinatorStats implements Writeable, ToXContentFragment { private final long remoteRequestsTotal; private final long executedSearchesTotal; - public CoordinatorStats(String nodeId, - int queueSize, - int remoteRequestsCurrent, - long remoteRequestsTotal, - long executedSearchesTotal) { + public CoordinatorStats( + String nodeId, + int queueSize, + int remoteRequestsCurrent, + long remoteRequestsTotal, + long executedSearchesTotal + ) { this.nodeId = nodeId; this.queueSize = queueSize; this.remoteRequestsCurrent = remoteRequestsCurrent; @@ -200,11 +201,11 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; CoordinatorStats stats = (CoordinatorStats) o; - return Objects.equals(nodeId, stats.nodeId) && - queueSize == stats.queueSize && - remoteRequestsCurrent == stats.remoteRequestsCurrent && - remoteRequestsTotal == stats.remoteRequestsTotal && - executedSearchesTotal == stats.executedSearchesTotal; + return Objects.equals(nodeId, stats.nodeId) + && queueSize == stats.queueSize + && remoteRequestsCurrent == stats.remoteRequestsCurrent + && remoteRequestsTotal == stats.remoteRequestsTotal + && executedSearchesTotal == stats.executedSearchesTotal; } @Override @@ -257,8 +258,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; ExecutingPolicy that = (ExecutingPolicy) o; - return name.equals(that.name) && - taskInfo.equals(that.taskInfo); + return name.equals(that.name) && taskInfo.equals(that.taskInfo); } @Override @@ -331,8 +331,11 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; CacheStats that = (CacheStats) o; - return count == that.count && hits == that.hits && misses == that.misses && evictions == that.evictions && - nodeId.equals(that.nodeId); + return count == that.count + && hits == that.hits + && misses == that.misses + && evictions == that.evictions + && nodeId.equals(that.nodeId); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/action/ExecuteEnrichPolicyAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/action/ExecuteEnrichPolicyAction.java index 6110c220840c1..ba802f9bf9ea8 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/action/ExecuteEnrichPolicyAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/action/ExecuteEnrichPolicyAction.java @@ -12,9 +12,9 @@ import org.elasticsearch.action.support.master.MasterNodeRequest; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.tasks.TaskId; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.tasks.TaskId; import java.io.IOException; import java.util.Objects; @@ -74,8 +74,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Request request = (Request) o; - return waitForCompletion == request.waitForCompletion && - Objects.equals(name, request.name); + return waitForCompletion == request.waitForCompletion && Objects.equals(name, request.name); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/action/ExecuteEnrichPolicyStatus.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/action/ExecuteEnrichPolicyStatus.java index 8dbcd4b6ba3a4..9c207ff05bf6a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/action/ExecuteEnrichPolicyStatus.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/action/ExecuteEnrichPolicyStatus.java @@ -6,13 +6,13 @@ */ package org.elasticsearch.xpack.core.enrich.action; -import java.io.IOException; - import org.elasticsearch.Version; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.tasks.Task; +import org.elasticsearch.xcontent.XContentBuilder; + +import java.io.IOException; public class ExecuteEnrichPolicyStatus implements Task.Status { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/action/GetEnrichPolicyAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/action/GetEnrichPolicyAction.java index f891a112e1d3e..7396d22f184c3 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/action/GetEnrichPolicyAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/action/GetEnrichPolicyAction.java @@ -87,8 +87,10 @@ public static class Response extends ActionResponse implements ToXContentObject public Response(Map policies) { Objects.requireNonNull(policies, "policies cannot be null"); // use a treemap to guarantee ordering in the set, then transform it to the list of named policies - this.policies = new TreeMap<>(policies).entrySet().stream() - .map(entry -> new EnrichPolicy.NamedPolicy(entry.getKey(), entry.getValue())).collect(Collectors.toList()); + this.policies = new TreeMap<>(policies).entrySet() + .stream() + .map(entry -> new EnrichPolicy.NamedPolicy(entry.getKey(), entry.getValue())) + .collect(Collectors.toList()); } public Response(StreamInput in) throws IOException { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/action/PutEnrichPolicyAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/action/PutEnrichPolicyAction.java index 5edd6b8b47e49..e42a5a05022d2 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/action/PutEnrichPolicyAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/action/PutEnrichPolicyAction.java @@ -40,8 +40,9 @@ public static class Request extends MasterNodeRequest listener) { + public final void performAction( + IndexMetadata indexMetadata, + ClusterState currentClusterState, + ClusterStateObserver observer, + ActionListener listener + ) { String followerIndex = indexMetadata.getIndex().getName(); Map customIndexMetadata = indexMetadata.getCustomData(CCR_METADATA_KEY); if (customIndexMetadata == null) { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AllocateAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AllocateAction.java index 249d346c522f8..c26cfbee98931 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AllocateAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AllocateAction.java @@ -10,12 +10,12 @@ import org.elasticsearch.client.Client; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.routing.allocation.decider.ShardsLimitAllocationDecider; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ilm.Step.StepKey; @@ -37,9 +37,16 @@ public class AllocateAction implements LifecycleAction { public static final ParseField REQUIRE_FIELD = new ParseField("require"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(NAME, - a -> new AllocateAction((Integer) a[0], (Integer) a[1], (Map) a[2], (Map) a[3], - (Map) a[4])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + a -> new AllocateAction( + (Integer) a[0], + (Integer) a[1], + (Map) a[2], + (Map) a[3], + (Map) a[4] + ) + ); static { PARSER.declareInt(ConstructingObjectParser.optionalConstructorArg(), NUMBER_OF_REPLICAS_FIELD); @@ -59,8 +66,13 @@ public static AllocateAction parse(XContentParser parser) { return PARSER.apply(parser, null); } - public AllocateAction(Integer numberOfReplicas, Integer totalShardsPerNode, Map include, Map exclude, - Map require) { + public AllocateAction( + Integer numberOfReplicas, + Integer totalShardsPerNode, + Map include, + Map exclude, + Map require + ) { if (include == null) { this.include = Collections.emptyMap(); } else { @@ -78,8 +90,15 @@ public AllocateAction(Integer numberOfReplicas, Integer totalShardsPerNode, Map< } if (this.include.isEmpty() && this.exclude.isEmpty() && this.require.isEmpty() && numberOfReplicas == null) { throw new IllegalArgumentException( - "At least one of " + INCLUDE_FIELD.getPreferredName() + ", " + EXCLUDE_FIELD.getPreferredName() + " or " - + REQUIRE_FIELD.getPreferredName() + "must contain attributes for action " + NAME); + "At least one of " + + INCLUDE_FIELD.getPreferredName() + + ", " + + EXCLUDE_FIELD.getPreferredName() + + " or " + + REQUIRE_FIELD.getPreferredName() + + "must contain attributes for action " + + NAME + ); } if (numberOfReplicas != null && numberOfReplicas < 0) { throw new IllegalArgumentException("[" + NUMBER_OF_REPLICAS_FIELD.getPreferredName() + "] must be >= 0"); @@ -93,9 +112,13 @@ public AllocateAction(Integer numberOfReplicas, Integer totalShardsPerNode, Map< @SuppressWarnings("unchecked") public AllocateAction(StreamInput in) throws IOException { - this(in.readOptionalVInt(), in.getVersion().onOrAfter(Version.V_7_16_0) ? in.readOptionalInt() : null, - (Map) in.readGenericValue(), (Map) in.readGenericValue(), - (Map) in.readGenericValue()); + this( + in.readOptionalVInt(), + in.getVersion().onOrAfter(Version.V_7_16_0) ? in.readOptionalInt() : null, + (Map) in.readGenericValue(), + (Map) in.readGenericValue(), + (Map) in.readGenericValue() + ); } public Integer getNumberOfReplicas() { @@ -189,11 +212,11 @@ public boolean equals(Object obj) { return false; } AllocateAction other = (AllocateAction) obj; - return Objects.equals(numberOfReplicas, other.numberOfReplicas) && - Objects.equals(totalShardsPerNode, other.totalShardsPerNode) && - Objects.equals(include, other.include) && - Objects.equals(exclude, other.exclude) && - Objects.equals(require, other.require); + return Objects.equals(numberOfReplicas, other.numberOfReplicas) + && Objects.equals(totalShardsPerNode, other.totalShardsPerNode) + && Objects.equals(include, other.include) + && Objects.equals(exclude, other.exclude) + && Objects.equals(require, other.require); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AllocationRoutedStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AllocationRoutedStep.java index aed74be430ba4..6d099589a03a8 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AllocationRoutedStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AllocationRoutedStep.java @@ -7,6 +7,7 @@ package org.elasticsearch.xpack.core.ilm; import com.carrotsearch.hppc.cursors.ObjectCursor; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.action.support.ActiveShardCount; @@ -54,19 +55,31 @@ public Result isConditionMet(Index index, ClusterState clusterState) { return new Result(false, null); } if (ActiveShardCount.ALL.enoughShardsActive(clusterState, index.getName()) == false) { - logger.debug("[{}] lifecycle action for index [{}] cannot make progress because not all shards are active", - getKey().getAction(), index.getName()); + logger.debug( + "[{}] lifecycle action for index [{}] cannot make progress because not all shards are active", + getKey().getAction(), + index.getName() + ); return new Result(false, waitingForActiveShardsAllocationInfo(idxMeta.getNumberOfReplicas())); } - AllocationDeciders allocationDeciders = new AllocationDeciders(Collections.singletonList( - new FilterAllocationDecider(clusterState.getMetadata().settings(), - new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS)))); + AllocationDeciders allocationDeciders = new AllocationDeciders( + Collections.singletonList( + new FilterAllocationDecider( + clusterState.getMetadata().settings(), + new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS) + ) + ) + ); int allocationPendingAllShards = getPendingAllocations(index, allocationDeciders, clusterState); if (allocationPendingAllShards > 0) { - logger.debug("{} lifecycle action [{}] waiting for [{}] shards to be allocated to nodes matching the given filters", - index, getKey().getAction(), allocationPendingAllShards); + logger.debug( + "{} lifecycle action [{}] waiting for [{}] shards to be allocated to nodes matching the given filters", + index, + getKey().getAction(), + allocationPendingAllShards + ); return new Result(false, allShardsActiveAllocationInfo(idxMeta.getNumberOfReplicas(), allocationPendingAllShards)); } else { logger.debug("{} lifecycle action for [{}] complete", index, getKey().getAction()); @@ -77,8 +90,14 @@ public Result isConditionMet(Index index, ClusterState clusterState) { static int getPendingAllocations(Index index, AllocationDeciders allocationDeciders, ClusterState clusterState) { // All the allocation attributes are already set so just need to check // if the allocation has happened - RoutingAllocation allocation = new RoutingAllocation(allocationDeciders, clusterState.getRoutingNodes(), clusterState, null, - null, System.nanoTime()); + RoutingAllocation allocation = new RoutingAllocation( + allocationDeciders, + clusterState.getRoutingNodes(), + clusterState, + null, + null, + System.nanoTime() + ); int allocationPendingAllShards = 0; @@ -86,9 +105,11 @@ static int getPendingAllocations(Index index, AllocationDeciders allocationDecid for (ObjectCursor shardRoutingTable : allShards.values()) { for (ShardRouting shardRouting : shardRoutingTable.value.shards()) { String currentNodeId = shardRouting.currentNodeId(); - boolean canRemainOnCurrentNode = allocationDeciders - .canRemain(shardRouting, clusterState.getRoutingNodes().node(currentNodeId), allocation) - .type() == Decision.Type.YES; + boolean canRemainOnCurrentNode = allocationDeciders.canRemain( + shardRouting, + clusterState.getRoutingNodes().node(currentNodeId), + allocation + ).type() == Decision.Type.YES; if (canRemainOnCurrentNode == false || shardRouting.started() == false) { allocationPendingAllShards++; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AsyncActionStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AsyncActionStep.java index e0b69cb28954b..8db8bd83e6b28 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AsyncActionStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AsyncActionStep.java @@ -32,6 +32,10 @@ public boolean indexSurvives() { return true; } - public abstract void performAction(IndexMetadata indexMetadata, ClusterState currentClusterState, - ClusterStateObserver observer, ActionListener listener); + public abstract void performAction( + IndexMetadata indexMetadata, + ClusterState currentClusterState, + ClusterStateObserver observer, + ActionListener listener + ); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AsyncRetryDuringSnapshotActionStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AsyncRetryDuringSnapshotActionStep.java index a6e8b51b6d0a1..b16e3fcb9b235 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AsyncRetryDuringSnapshotActionStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AsyncRetryDuringSnapshotActionStep.java @@ -37,11 +37,19 @@ public AsyncRetryDuringSnapshotActionStep(StepKey key, StepKey nextStepKey, Clie } @Override - public final void performAction(IndexMetadata indexMetadata, ClusterState currentClusterState, - ClusterStateObserver observer, ActionListener listener) { + public final void performAction( + IndexMetadata indexMetadata, + ClusterState currentClusterState, + ClusterStateObserver observer, + ActionListener listener + ) { // Wrap the original listener to handle exceptions caused by ongoing snapshots - SnapshotExceptionListener snapshotExceptionListener = new SnapshotExceptionListener(indexMetadata.getIndex(), listener, observer, - currentClusterState.nodes().getLocalNode()); + SnapshotExceptionListener snapshotExceptionListener = new SnapshotExceptionListener( + indexMetadata.getIndex(), + listener, + observer, + currentClusterState.nodes().getLocalNode() + ); performDuringNoSnapshot(indexMetadata, currentClusterState, snapshotExceptionListener); } @@ -63,8 +71,12 @@ class SnapshotExceptionListener implements ActionListener { private final ClusterStateObserver observer; private final DiscoveryNode localNode; - SnapshotExceptionListener(Index index, ActionListener originalListener, ClusterStateObserver observer, - DiscoveryNode localNode) { + SnapshotExceptionListener( + Index index, + ActionListener originalListener, + ClusterStateObserver observer, + DiscoveryNode localNode + ) { this.index = index; this.originalListener = originalListener; this.observer = observer; @@ -80,65 +92,63 @@ public void onResponse(Void unused) { public void onFailure(Exception e) { if (e instanceof SnapshotInProgressException) { try { - logger.debug("[{}] attempted to run ILM step but a snapshot is in progress, step will retry at a later time", - index.getName()); + logger.debug( + "[{}] attempted to run ILM step but a snapshot is in progress, step will retry at a later time", + index.getName() + ); final String indexName = index.getName(); - observer.waitForNextChange( - new ClusterStateObserver.Listener() { - @Override - public void onNewClusterState(ClusterState state) { - if (state.nodes().isLocalNodeElectedMaster() == false) { - originalListener.onFailure(new NotMasterException("no longer master")); - return; - } - try { - logger.debug("[{}] retrying ILM step after snapshot has completed", indexName); - IndexMetadata idxMeta = state.metadata().index(index); - if (idxMeta == null) { - // The index has since been deleted, mission accomplished! - originalListener.onResponse(null); - } else { - // Re-invoke the performAction method with the new state - performAction(idxMeta, state, observer, originalListener); - } - } catch (Exception e) { - originalListener.onFailure(e); - } + observer.waitForNextChange(new ClusterStateObserver.Listener() { + @Override + public void onNewClusterState(ClusterState state) { + if (state.nodes().isLocalNodeElectedMaster() == false) { + originalListener.onFailure(new NotMasterException("no longer master")); + return; + } + try { + logger.debug("[{}] retrying ILM step after snapshot has completed", indexName); + IndexMetadata idxMeta = state.metadata().index(index); + if (idxMeta == null) { + // The index has since been deleted, mission accomplished! + originalListener.onResponse(null); + } else { + // Re-invoke the performAction method with the new state + performAction(idxMeta, state, observer, originalListener); } + } catch (Exception e) { + originalListener.onFailure(e); + } + } - @Override - public void onClusterServiceClose() { - originalListener.onFailure(new NodeClosedException(localNode)); - } + @Override + public void onClusterServiceClose() { + originalListener.onFailure(new NodeClosedException(localNode)); + } - @Override - public void onTimeout(TimeValue timeout) { - originalListener.onFailure( - new IllegalStateException("step timed out while waiting for snapshots to complete")); - } - }, - state -> { - if (state.nodes().isLocalNodeElectedMaster() == false) { - // ILM actions should only run on master, lets bail on failover - return true; - } - if (state.metadata().index(index) == null) { - // The index has since been deleted, mission accomplished! - return true; - } - for (List snapshots : - state.custom(SnapshotsInProgress.TYPE, SnapshotsInProgress.EMPTY).entriesByRepo()) { - for (SnapshotsInProgress.Entry snapshot : snapshots) { - if (snapshot.indices().containsKey(indexName)) { - // There is a snapshot running with this index name - return false; - } - } + @Override + public void onTimeout(TimeValue timeout) { + originalListener.onFailure(new IllegalStateException("step timed out while waiting for snapshots to complete")); + } + }, state -> { + if (state.nodes().isLocalNodeElectedMaster() == false) { + // ILM actions should only run on master, lets bail on failover + return true; + } + if (state.metadata().index(index) == null) { + // The index has since been deleted, mission accomplished! + return true; + } + for (List snapshots : state.custom(SnapshotsInProgress.TYPE, SnapshotsInProgress.EMPTY) + .entriesByRepo()) { + for (SnapshotsInProgress.Entry snapshot : snapshots) { + if (snapshot.indices().containsKey(indexName)) { + // There is a snapshot running with this index name + return false; } - // There are no snapshots for this index, so it's okay to proceed with this state - return true; - }, - TimeValue.MAX_VALUE); + } + } + // There are no snapshots for this index, so it's okay to proceed with this state + return true; + }, TimeValue.MAX_VALUE); } catch (Exception secondError) { // There was a second error trying to set up an observer, // fail the original listener diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AsyncWaitStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AsyncWaitStep.java index 658335aef1224..2a9373277cf9c 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AsyncWaitStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AsyncWaitStep.java @@ -9,8 +9,8 @@ import org.elasticsearch.client.Client; import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.index.Index; +import org.elasticsearch.xcontent.ToXContentObject; /** * A step which will be called periodically, waiting for some condition to become true. diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/BranchingStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/BranchingStep.java index 32f0920708057..a4e762b403da0 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/BranchingStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/BranchingStep.java @@ -76,13 +76,13 @@ public ClusterState performAction(Index index, ClusterState clusterState) { * * @return next step to execute */ - @Override - public final StepKey getNextStepKey() { + @Override + public final StepKey getNextStepKey() { if (predicateValue.get() == null) { throw new IllegalStateException("Cannot call getNextStepKey before performAction"); } return predicateValue.get() ? nextStepKeyOnTrue : nextStepKeyOnFalse; - } + } /** * @return the next step if {@code predicate} is false diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CheckNotDataStreamWriteIndexStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CheckNotDataStreamWriteIndexStep.java index 54150d327134d..d20ad498259f8 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CheckNotDataStreamWriteIndexStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CheckNotDataStreamWriteIndexStep.java @@ -12,10 +12,10 @@ import org.elasticsearch.cluster.metadata.IndexAbstraction; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.Metadata; +import org.elasticsearch.index.Index; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.index.Index; import java.io.IOException; import java.util.Locale; @@ -49,8 +49,12 @@ public Result isConditionMet(Index index, ClusterState clusterState) { String indexName = index.getName(); if (indexMetadata == null) { - String errorMessage = String.format(Locale.ROOT, "[%s] lifecycle action for index [%s] executed but index no longer exists", - getKey().getAction(), indexName); + String errorMessage = String.format( + Locale.ROOT, + "[%s] lifecycle action for index [%s] executed but index no longer exists", + getKey().getAction(), + indexName + ); // Index must have been since deleted logger.debug(errorMessage); return new Result(false, new Info(errorMessage)); @@ -63,9 +67,15 @@ public Result isConditionMet(Index index, ClusterState clusterState) { if (dataStream != null) { assert dataStream.getWriteIndex() != null : dataStream.getName() + " has no write index"; if (dataStream.getWriteIndex().equals(index)) { - String errorMessage = String.format(Locale.ROOT, "index [%s] is the write index for data stream [%s], pausing " + - "ILM execution of lifecycle [%s] until this index is no longer the write index for the data stream via manual or " + - "automated rollover", indexName, dataStream.getName(), policyName); + String errorMessage = String.format( + Locale.ROOT, + "index [%s] is the write index for data stream [%s], pausing " + + "ILM execution of lifecycle [%s] until this index is no longer the write index for the data stream via manual or " + + "automated rollover", + indexName, + dataStream.getName(), + policyName + ); logger.debug(errorMessage); return new Result(false, new Info(errorMessage)); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CheckShrinkReadyStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CheckShrinkReadyStep.java index 5d6efa39d6865..0248084775632 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CheckShrinkReadyStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CheckShrinkReadyStep.java @@ -16,12 +16,12 @@ import org.elasticsearch.cluster.routing.IndexRoutingTable; import org.elasticsearch.cluster.routing.ShardRouting; import org.elasticsearch.cluster.routing.ShardRoutingState; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; +import org.elasticsearch.index.Index; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.index.Index; import org.elasticsearch.xpack.core.ilm.step.info.SingleMessageFieldInfo; import java.io.IOException; @@ -59,8 +59,7 @@ public Result isConditionMet(Index index, ClusterState clusterState) { if (idxMeta == null) { // Index must have been since deleted, ignore it - logger.debug("[{}] lifecycle action for index [{}] executed but index no longer exists", - getKey().getAction(), index.getName()); + logger.debug("[{}] lifecycle action for index [{}] executed but index no longer exists", getKey().getAction(), index.getName()); return new Result(false, null); } @@ -91,24 +90,44 @@ public Result isConditionMet(Index index, ClusterState clusterState) { } } - logger.trace("{} checking for shrink readiness on [{}], found {} shards and need {}", - index, idShardsShouldBeOn, foundShards, expectedShardCount); + logger.trace( + "{} checking for shrink readiness on [{}], found {} shards and need {}", + index, + idShardsShouldBeOn, + foundShards, + expectedShardCount + ); if (foundShards == expectedShardCount) { - logger.trace("{} successfully found {} allocated shards for shrink readiness on node [{}] ({})", - index, expectedShardCount, idShardsShouldBeOn, getKey().getAction()); + logger.trace( + "{} successfully found {} allocated shards for shrink readiness on node [{}] ({})", + index, + expectedShardCount, + idShardsShouldBeOn, + getKey().getAction() + ); return new Result(true, null); } else { if (nodeBeingRemoved) { completable = false; - return new Result(false, new SingleMessageFieldInfo("node with id [" + idShardsShouldBeOn + - "] is currently marked as shutting down for removal")); + return new Result( + false, + new SingleMessageFieldInfo("node with id [" + idShardsShouldBeOn + "] is currently marked as shutting down for removal") + ); } - logger.trace("{} failed to find {} allocated shards (found {}) on node [{}] for shrink readiness ({})", - index, expectedShardCount, foundShards, idShardsShouldBeOn, getKey().getAction()); - return new Result(false, new CheckShrinkReadyStep.Info(idShardsShouldBeOn, expectedShardCount, - expectedShardCount - foundShards)); + logger.trace( + "{} failed to find {} allocated shards (found {}) on node [{}] for shrink readiness ({})", + index, + expectedShardCount, + foundShards, + idShardsShouldBeOn, + getKey().getAction() + ); + return new Result( + false, + new CheckShrinkReadyStep.Info(idShardsShouldBeOn, expectedShardCount, expectedShardCount - foundShards) + ); } } @@ -140,7 +159,9 @@ public static final class Info implements ToXContentObject { static final ParseField SHARDS_TO_ALLOCATE = new ParseField("shards_left_to_allocate"); static final ParseField MESSAGE = new ParseField("message"); static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "check_shrink_ready_step_info", a -> new CheckShrinkReadyStep.Info((String) a[0], (long) a[1], (long) a[2])); + "check_shrink_ready_step_info", + a -> new CheckShrinkReadyStep.Info((String) a[0], (long) a[1], (long) a[2]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), NODE_ID); PARSER.declareLong(ConstructingObjectParser.constructorArg(), EXPECTED_SHARDS); @@ -155,8 +176,14 @@ public Info(String nodeId, long expectedShards, long numberShardsLeftToAllocate) if (numberShardsLeftToAllocate < 0) { this.message = "Waiting for all shards to become active"; } else { - this.message = String.format(Locale.ROOT, "Waiting for node [%s] to contain [%d] shards, found [%d], remaining [%d]", - nodeId, expectedShards, expectedShards - numberShardsLeftToAllocate, numberShardsLeftToAllocate); + this.message = String.format( + Locale.ROOT, + "Waiting for node [%s] to contain [%d] shards, found [%d], remaining [%d]", + nodeId, + expectedShards, + expectedShards - numberShardsLeftToAllocate, + numberShardsLeftToAllocate + ); } } @@ -185,9 +212,9 @@ public boolean equals(Object obj) { return false; } CheckShrinkReadyStep.Info other = (CheckShrinkReadyStep.Info) obj; - return Objects.equals(actualReplicas, other.actualReplicas) && - Objects.equals(numberShardsLeftToAllocate, other.numberShardsLeftToAllocate) && - Objects.equals(nodeId, other.nodeId); + return Objects.equals(actualReplicas, other.actualReplicas) + && Objects.equals(numberShardsLeftToAllocate, other.numberShardsLeftToAllocate) + && Objects.equals(nodeId, other.nodeId); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CheckTargetShardsCountStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CheckTargetShardsCountStep.java index 8f463918d739e..77c214522117e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CheckTargetShardsCountStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CheckTargetShardsCountStep.java @@ -45,8 +45,7 @@ public Result isConditionMet(Index index, ClusterState clusterState) { IndexMetadata indexMetadata = clusterState.metadata().index(index); if (indexMetadata == null) { // Index must have been since deleted, ignore it - logger.debug("[{}] lifecycle action for index [{}] executed but index no longer exists", - getKey().getAction(), index.getName()); + logger.debug("[{}] lifecycle action for index [{}] executed but index no longer exists", getKey().getAction(), index.getName()); return new Result(false, null); } String indexName = indexMetadata.getIndex().getName(); @@ -54,9 +53,15 @@ public Result isConditionMet(Index index, ClusterState clusterState) { int sourceNumberOfShards = indexMetadata.getNumberOfShards(); if (sourceNumberOfShards % numberOfShards != 0) { String policyName = indexMetadata.getSettings().get(LifecycleSettings.LIFECYCLE_NAME); - String errorMessage = String.format(Locale.ROOT, "lifecycle action of policy [%s] for index [%s] cannot make progress " + - "because the target shards count [%d] must be a factor of the source index's shards count [%d]", - policyName, indexName, numberOfShards, sourceNumberOfShards); + String errorMessage = String.format( + Locale.ROOT, + "lifecycle action of policy [%s] for index [%s] cannot make progress " + + "because the target shards count [%d] must be a factor of the source index's shards count [%d]", + policyName, + indexName, + numberOfShards, + sourceNumberOfShards + ); logger.debug(errorMessage); return new Result(false, new SingleMessageFieldInfo(errorMessage)); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CleanupShrinkIndexStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CleanupShrinkIndexStep.java index 99b9a17f142c7..9fb1ad266fa1e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CleanupShrinkIndexStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CleanupShrinkIndexStep.java @@ -45,8 +45,14 @@ void performDuringNoSnapshot(IndexMetadata indexMetadata, ClusterState currentCl // if the source index does not exist, we'll skip deleting the // (managed) shrunk index as that will cause data loss String policyName = LifecycleSettings.LIFECYCLE_NAME_SETTING.get(indexMetadata.getSettings()); - logger.warn("managed index [{}] as part of policy [{}] is a shrunk index and the source index [{}] does not exist " + - "anymore. will skip the [{}] step", indexMetadata.getIndex().getName(), policyName, shrunkenIndexSource, NAME); + logger.warn( + "managed index [{}] as part of policy [{}] is a shrunk index and the source index [{}] does not exist " + + "anymore. will skip the [{}] step", + indexMetadata.getIndex().getName(), + policyName, + shrunkenIndexSource, + NAME + ); listener.onResponse(null); return; } @@ -59,8 +65,10 @@ void performDuringNoSnapshot(IndexMetadata indexMetadata, ClusterState currentCl listener.onResponse(null); return; } - getClient().admin().indices() - .delete(new DeleteIndexRequest(shrinkIndexName).masterNodeTimeout(TimeValue.MAX_VALUE), + getClient().admin() + .indices() + .delete( + new DeleteIndexRequest(shrinkIndexName).masterNodeTimeout(TimeValue.MAX_VALUE), new ActionListener() { @Override public void onResponse(AcknowledgedResponse acknowledgedResponse) { @@ -78,7 +86,8 @@ public void onFailure(Exception e) { listener.onFailure(e); } } - }); + } + ); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CleanupSnapshotStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CleanupSnapshotStep.java index f1a8f419d52cc..83b2399bff816 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CleanupSnapshotStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CleanupSnapshotStep.java @@ -50,34 +50,57 @@ void performDuringNoSnapshot(IndexMetadata indexMetadata, ClusterState currentCl listener.onResponse(null); return; } - getClient().admin().cluster().prepareDeleteSnapshot(repositoryName, snapshotName).setMasterNodeTimeout(TimeValue.MAX_VALUE) - .execute(new ActionListener<>() { + getClient().admin() + .cluster() + .prepareDeleteSnapshot(repositoryName, snapshotName) + .setMasterNodeTimeout(TimeValue.MAX_VALUE) + .execute(new ActionListener<>() { - @Override - public void onResponse(AcknowledgedResponse acknowledgedResponse) { - if (acknowledgedResponse.isAcknowledged() == false) { - String policyName = indexMetadata.getSettings().get(LifecycleSettings.LIFECYCLE_NAME); - throw new ElasticsearchException("cleanup snapshot step request for repository [" + repositoryName + "] and snapshot " + - "[" + snapshotName + "] policy [" + policyName + "] and index [" + indexName + "] failed to be acknowledged"); + @Override + public void onResponse(AcknowledgedResponse acknowledgedResponse) { + if (acknowledgedResponse.isAcknowledged() == false) { + String policyName = indexMetadata.getSettings().get(LifecycleSettings.LIFECYCLE_NAME); + throw new ElasticsearchException( + "cleanup snapshot step request for repository [" + + repositoryName + + "] and snapshot " + + "[" + + snapshotName + + "] policy [" + + policyName + + "] and index [" + + indexName + + "] failed to be acknowledged" + ); + } + listener.onResponse(null); } - listener.onResponse(null); - } - @Override - public void onFailure(Exception e) { - if (e instanceof SnapshotMissingException) { - // during the happy flow we generate a snapshot name and that snapshot doesn't exist in the repository - listener.onResponse(null); - } else { - if (e instanceof RepositoryMissingException) { - String policyName = indexMetadata.getSettings().get(LifecycleSettings.LIFECYCLE_NAME); - listener.onFailure(new IllegalStateException("repository [" + repositoryName + "] is missing. [" + policyName + - "] policy for index [" + indexName + "] cannot continue until the repository is created", e)); + @Override + public void onFailure(Exception e) { + if (e instanceof SnapshotMissingException) { + // during the happy flow we generate a snapshot name and that snapshot doesn't exist in the repository + listener.onResponse(null); } else { - listener.onFailure(e); + if (e instanceof RepositoryMissingException) { + String policyName = indexMetadata.getSettings().get(LifecycleSettings.LIFECYCLE_NAME); + listener.onFailure( + new IllegalStateException( + "repository [" + + repositoryName + + "] is missing. [" + + policyName + + "] policy for index [" + + indexName + + "] cannot continue until the repository is created", + e + ) + ); + } else { + listener.onFailure(e); + } } } - } - }); + }); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CloseFollowerIndexStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CloseFollowerIndexStep.java index 89a6ff8227d08..fb0b41dff029d 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CloseFollowerIndexStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CloseFollowerIndexStep.java @@ -41,17 +41,13 @@ void performDuringNoSnapshot(IndexMetadata indexMetadata, ClusterState currentCl } if (indexMetadata.getState() == IndexMetadata.State.OPEN) { - CloseIndexRequest closeIndexRequest = new CloseIndexRequest(followerIndex) - .masterNodeTimeout(TimeValue.MAX_VALUE); - getClient().admin().indices().close(closeIndexRequest, ActionListener.wrap( - r -> { - if (r.isAcknowledged() == false) { - throw new ElasticsearchException("close index request failed to be acknowledged"); - } - listener.onResponse(null); - }, - listener::onFailure) - ); + CloseIndexRequest closeIndexRequest = new CloseIndexRequest(followerIndex).masterNodeTimeout(TimeValue.MAX_VALUE); + getClient().admin().indices().close(closeIndexRequest, ActionListener.wrap(r -> { + if (r.isAcknowledged() == false) { + throw new ElasticsearchException("close index request failed to be acknowledged"); + } + listener.onResponse(null); + }, listener::onFailure)); } else { listener.onResponse(null); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CloseIndexStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CloseIndexStep.java index b94d75bc4950f..0729a2652ddb3 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CloseIndexStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CloseIndexStep.java @@ -28,19 +28,21 @@ public class CloseIndexStep extends AsyncActionStep { } @Override - public void performAction(IndexMetadata indexMetadata, ClusterState currentClusterState, - ClusterStateObserver observer, ActionListener listener) { + public void performAction( + IndexMetadata indexMetadata, + ClusterState currentClusterState, + ClusterStateObserver observer, + ActionListener listener + ) { if (indexMetadata.getState() == IndexMetadata.State.OPEN) { CloseIndexRequest request = new CloseIndexRequest(indexMetadata.getIndex().getName()).masterNodeTimeout(TimeValue.MAX_VALUE); - getClient().admin().indices() - .close(request, ActionListener.wrap(closeIndexResponse -> { - if (closeIndexResponse.isAcknowledged() == false) { - throw new ElasticsearchException("close index request failed to be acknowledged"); - } - listener.onResponse(null); - }, listener::onFailure)); - } - else { + getClient().admin().indices().close(request, ActionListener.wrap(closeIndexResponse -> { + if (closeIndexResponse.isAcknowledged() == false) { + throw new ElasticsearchException("close index request failed to be acknowledged"); + } + listener.onResponse(null); + }, listener::onFailure)); + } else { listener.onResponse(null); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ClusterStateWaitStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ClusterStateWaitStep.java index 36b37c73fe19e..bc76d53226ee0 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ClusterStateWaitStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ClusterStateWaitStep.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ilm; import org.elasticsearch.cluster.ClusterState; -import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.index.Index; +import org.elasticsearch.xcontent.ToXContentObject; /** * Checks whether a condition has been met based on the cluster state. diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ClusterStateWaitUntilThresholdStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ClusterStateWaitUntilThresholdStep.java index cfb0f09423c68..8342bbcaad554 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ClusterStateWaitUntilThresholdStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ClusterStateWaitUntilThresholdStep.java @@ -11,8 +11,8 @@ import org.apache.logging.log4j.Logger; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.IndexMetadata; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.Index; import org.elasticsearch.xpack.core.ilm.step.info.SingleMessageFieldInfo; @@ -58,8 +58,7 @@ public Result isConditionMet(Index index, ClusterState clusterState) { IndexMetadata idxMeta = clusterState.metadata().index(index); if (idxMeta == null) { // Index must have been since deleted, ignore it - logger.debug("[{}] lifecycle action for index [{}] executed but index no longer exists", - getKey().getAction(), index.getName()); + logger.debug("[{}] lifecycle action for index [{}] executed but index no longer exists", getKey().getAction(), index.getName()); return new Result(false, null); } @@ -74,13 +73,16 @@ public Result isConditionMet(Index index, ClusterState clusterState) { // we may not have passed the time threshold, but the step is not completable due to a different reason thresholdPassed.set(true); - String message = String.format(Locale.ROOT, "[%s] lifecycle step, as part of [%s] action, for index [%s] Is not " + - "completable, reason: [%s]. Abandoning execution and moving to the next fallback step [%s]", + String message = String.format( + Locale.ROOT, + "[%s] lifecycle step, as part of [%s] action, for index [%s] Is not " + + "completable, reason: [%s]. Abandoning execution and moving to the next fallback step [%s]", getKey().getName(), getKey().getAction(), idxMeta.getIndex().getName(), Strings.toString(stepResult.getInfomationContext()), - nextKeyOnThresholdBreach); + nextKeyOnThresholdBreach + ); logger.debug(message); return new Result(true, new SingleMessageFieldInfo(message)); @@ -89,10 +91,16 @@ public Result isConditionMet(Index index, ClusterState clusterState) { // we retried this step enough, next step will be the configured to {@code nextKeyOnThresholdBreach} thresholdPassed.set(true); - String message = String.format(Locale.ROOT, "[%s] lifecycle step, as part of [%s] action, for index [%s] executed for" + - " more than [%s]. Abandoning execution and moving to the next fallback step [%s]", - getKey().getName(), getKey().getAction(), idxMeta.getIndex().getName(), retryThreshold, - nextKeyOnThresholdBreach); + String message = String.format( + Locale.ROOT, + "[%s] lifecycle step, as part of [%s] action, for index [%s] executed for" + + " more than [%s]. Abandoning execution and moving to the next fallback step [%s]", + getKey().getName(), + getKey().getAction(), + idxMeta.getIndex().getName(), + retryThreshold, + nextKeyOnThresholdBreach + ); logger.debug(message); return new Result(true, new SingleMessageFieldInfo(message)); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CopyExecutionStateStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CopyExecutionStateStep.java index 25a9290a4d469..f5dd11bf91edf 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CopyExecutionStateStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CopyExecutionStateStep.java @@ -34,9 +34,12 @@ public class CopyExecutionStateStep extends ClusterStateActionStep { private final BiFunction targetIndexNameSupplier; private final StepKey targetNextStepKey; - public CopyExecutionStateStep(StepKey key, StepKey nextStepKey, - BiFunction targetIndexNameSupplier, - StepKey targetNextStepKey) { + public CopyExecutionStateStep( + StepKey key, + StepKey nextStepKey, + BiFunction targetIndexNameSupplier, + StepKey targetNextStepKey + ) { super(key, nextStepKey); this.targetIndexNameSupplier = targetIndexNameSupplier; this.targetNextStepKey = targetNextStepKey; @@ -69,10 +72,15 @@ public ClusterState performAction(Index index, ClusterState clusterState) { IndexMetadata targetIndexMetadata = clusterState.metadata().index(targetIndexName); if (targetIndexMetadata == null) { - logger.warn("[{}] index [{}] unable to copy execution state to target index [{}] as target index does not exist", - getKey().getAction(), index.getName(), targetIndexName); - throw new IllegalStateException("unable to copy execution state from [" + index.getName() + - "] to [" + targetIndexName + "] as target index does not exist"); + logger.warn( + "[{}] index [{}] unable to copy execution state to target index [{}] as target index does not exist", + getKey().getAction(), + index.getName(), + targetIndexName + ); + throw new IllegalStateException( + "unable to copy execution state from [" + index.getName() + "] to [" + targetIndexName + "] as target index does not exist" + ); } String phase = targetNextStepKey.getPhase(); @@ -86,8 +94,7 @@ public ClusterState performAction(Index index, ClusterState clusterState) { relevantTargetCustomData.setStep(step); Metadata.Builder newMetadata = Metadata.builder(clusterState.getMetadata()) - .put(IndexMetadata.builder(targetIndexMetadata) - .putCustom(ILM_CUSTOM_METADATA_KEY, relevantTargetCustomData.build().asMap())); + .put(IndexMetadata.builder(targetIndexMetadata).putCustom(ILM_CUSTOM_METADATA_KEY, relevantTargetCustomData.build().asMap())); return ClusterState.builder(clusterState).metadata(newMetadata.build(false)).build(); } @@ -101,8 +108,9 @@ public boolean equals(Object o) { return false; } CopyExecutionStateStep that = (CopyExecutionStateStep) o; - return super.equals(o) && Objects.equals(targetIndexNameSupplier, that.targetIndexNameSupplier) && - Objects.equals(targetNextStepKey, that.targetNextStepKey); + return super.equals(o) + && Objects.equals(targetIndexNameSupplier, that.targetIndexNameSupplier) + && Objects.equals(targetNextStepKey, that.targetNextStepKey); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CopySettingsStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CopySettingsStep.java index c4bedc0eb7230..0be46f8c0bf4b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CopySettingsStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CopySettingsStep.java @@ -71,8 +71,13 @@ public ClusterState performAction(Index index, ClusterState clusterState) { } if (targetIndexMetadata == null) { - String errorMessage = String.format(Locale.ROOT, "index [%s] is being referenced by ILM action [%s] on step [%s] but " + - "it doesn't exist", targetIndexName, getKey().getAction(), getKey().getName()); + String errorMessage = String.format( + Locale.ROOT, + "index [%s] is being referenced by ILM action [%s] on step [%s] but " + "it doesn't exist", + targetIndexName, + getKey().getAction(), + getKey().getName() + ); logger.debug(errorMessage); throw new IllegalStateException(errorMessage); } @@ -84,9 +89,9 @@ public ClusterState performAction(Index index, ClusterState clusterState) { } Metadata.Builder newMetaData = Metadata.builder(clusterState.getMetadata()) - .put(IndexMetadata.builder(targetIndexMetadata) - .settingsVersion(targetIndexMetadata.getSettingsVersion() + 1) - .settings(settings)); + .put( + IndexMetadata.builder(targetIndexMetadata).settingsVersion(targetIndexMetadata.getSettingsVersion() + 1).settings(settings) + ); return ClusterState.builder(clusterState).metadata(newMetaData.build(false)).build(); } @@ -102,8 +107,7 @@ public boolean equals(Object o) { return false; } CopySettingsStep that = (CopySettingsStep) o; - return Objects.equals(settingsKeys, that.settingsKeys) && - Objects.equals(indexPrefix, that.indexPrefix); + return Objects.equals(settingsKeys, that.settingsKeys) && Objects.equals(indexPrefix, that.indexPrefix); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CreateSnapshotStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CreateSnapshotStep.java index 40079402c8b60..00a6be1c3c4f5 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CreateSnapshotStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CreateSnapshotStep.java @@ -75,15 +75,19 @@ void createSnapshot(IndexMetadata indexMetadata, ActionListener listene final String policyName = indexMetadata.getSettings().get(LifecycleSettings.LIFECYCLE_NAME); final String snapshotRepository = lifecycleState.getSnapshotRepository(); if (Strings.hasText(snapshotRepository) == false) { - listener.onFailure(new IllegalStateException("snapshot repository is not present for policy [" + policyName + "] and index [" + - indexName + "]")); + listener.onFailure( + new IllegalStateException( + "snapshot repository is not present for policy [" + policyName + "] and index [" + indexName + "]" + ) + ); return; } final String snapshotName = lifecycleState.getSnapshotName(); if (Strings.hasText(snapshotName) == false) { listener.onFailure( - new IllegalStateException("snapshot name was not generated for policy [" + policyName + "] and index [" + indexName + "]")); + new IllegalStateException("snapshot name was not generated for policy [" + policyName + "] and index [" + indexName + "]") + ); return; } CreateSnapshotRequest request = new CreateSnapshotRequest(snapshotRepository, snapshotName); @@ -93,25 +97,32 @@ void createSnapshot(IndexMetadata indexMetadata, ActionListener listene request.waitForCompletion(true); request.includeGlobalState(false); request.masterNodeTimeout(TimeValue.MAX_VALUE); - getClient().admin().cluster().createSnapshot(request, - ActionListener.wrap(response -> { - logger.debug("create snapshot response for policy [{}] and index [{}] is: {}", policyName, indexName, - Strings.toString(response)); - final SnapshotInfo snapInfo = response.getSnapshotInfo(); - - // Check that there are no failed shards, since the request may not entirely - // fail, but may still have failures (such as in the case of an aborted snapshot) - if (snapInfo.failedShards() == 0) { - listener.onResponse(true); - } else { - int failures = snapInfo.failedShards(); - int total = snapInfo.totalShards(); - String message = String.format(Locale.ROOT, - "failed to create snapshot successfully, %s failures out of %s total shards failed", failures, total); - logger.warn(message); - listener.onResponse(false); - } - }, listener::onFailure)); + getClient().admin().cluster().createSnapshot(request, ActionListener.wrap(response -> { + logger.debug( + "create snapshot response for policy [{}] and index [{}] is: {}", + policyName, + indexName, + Strings.toString(response) + ); + final SnapshotInfo snapInfo = response.getSnapshotInfo(); + + // Check that there are no failed shards, since the request may not entirely + // fail, but may still have failures (such as in the case of an aborted snapshot) + if (snapInfo.failedShards() == 0) { + listener.onResponse(true); + } else { + int failures = snapInfo.failedShards(); + int total = snapInfo.totalShards(); + String message = String.format( + Locale.ROOT, + "failed to create snapshot successfully, %s failures out of %s total shards failed", + failures, + total + ); + logger.warn(message); + listener.onResponse(false); + } + }, listener::onFailure)); } @Override @@ -150,8 +161,7 @@ public boolean equals(Object o) { return false; } CreateSnapshotStep that = (CreateSnapshotStep) o; - return Objects.equals(nextKeyOnComplete, that.nextKeyOnComplete) && - Objects.equals(nextKeyOnIncomplete, that.nextKeyOnIncomplete); + return Objects.equals(nextKeyOnComplete, that.nextKeyOnComplete) && Objects.equals(nextKeyOnIncomplete, that.nextKeyOnIncomplete); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/DataTierMigrationRoutedStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/DataTierMigrationRoutedStep.java index 91f04f114986d..b8293279527ab 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/DataTierMigrationRoutedStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/DataTierMigrationRoutedStep.java @@ -43,11 +43,7 @@ public boolean isRetryable() { @Override public Result isConditionMet(Index index, ClusterState clusterState) { - AllocationDeciders allocationDeciders = new AllocationDeciders( - List.of( - new DataTierAllocationDecider() - ) - ); + AllocationDeciders allocationDeciders = new AllocationDeciders(List.of(new DataTierAllocationDecider())); IndexMetadata idxMeta = clusterState.metadata().index(index); if (idxMeta == null) { // Index must have been since deleted, ignore it @@ -55,29 +51,46 @@ public Result isConditionMet(Index index, ClusterState clusterState) { return new Result(false, null); } List preferredTierConfiguration = idxMeta.getTierPreference(); - Optional availableDestinationTier = DataTierAllocationDecider.preferredAvailableTier(preferredTierConfiguration, - clusterState.getNodes()); + Optional availableDestinationTier = DataTierAllocationDecider.preferredAvailableTier( + preferredTierConfiguration, + clusterState.getNodes() + ); if (ActiveShardCount.ALL.enoughShardsActive(clusterState, index.getName()) == false) { if (preferredTierConfiguration.isEmpty()) { - logger.debug("[{}] lifecycle action for index [{}] cannot make progress because not all shards are active", - getKey().getAction(), index.getName()); + logger.debug( + "[{}] lifecycle action for index [{}] cannot make progress because not all shards are active", + getKey().getAction(), + index.getName() + ); } else { if (availableDestinationTier.isPresent()) { - logger.debug("[{}] migration of index [{}] to the {} tier preference cannot progress, as not all shards are active", - getKey().getAction(), index.getName(), preferredTierConfiguration); + logger.debug( + "[{}] migration of index [{}] to the {} tier preference cannot progress, as not all shards are active", + getKey().getAction(), + index.getName(), + preferredTierConfiguration + ); } else { - logger.debug("[{}] migration of index [{}] to the next tier cannot progress as there is no available tier for the " + - "configured preferred tiers {} and not all shards are active", getKey().getAction(), index.getName(), - preferredTierConfiguration); + logger.debug( + "[{}] migration of index [{}] to the next tier cannot progress as there is no available tier for the " + + "configured preferred tiers {} and not all shards are active", + getKey().getAction(), + index.getName(), + preferredTierConfiguration + ); } } return new Result(false, waitingForActiveShardsAllocationInfo(idxMeta.getNumberOfReplicas())); } if (preferredTierConfiguration.isEmpty()) { - logger.debug("index [{}] has no data tier routing preference setting configured and all its shards are active. considering " + - "the [{}] step condition met and continuing to the next step", index.getName(), getKey().getName()); + logger.debug( + "index [{}] has no data tier routing preference setting configured and all its shards are active. considering " + + "the [{}] step condition met and continuing to the next step", + index.getName(), + getKey().getName() + ); // the user removed the tier routing setting and all the shards are active so we'll cary on return new Result(true, null); } @@ -86,17 +99,35 @@ public Result isConditionMet(Index index, ClusterState clusterState) { if (allocationPendingAllShards > 0) { String statusMessage = availableDestinationTier.map( - s -> String.format(Locale.ROOT, "[%s] lifecycle action [%s] waiting for [%s] shards to be moved to the [%s] tier (tier " + - "migration preference configuration is %s)", index.getName(), getKey().getAction(), allocationPendingAllShards, s, - preferredTierConfiguration) - ).orElseGet( - () -> String.format(Locale.ROOT, "index [%s] has a preference for tiers %s, but no nodes for any of those tiers are " + - "available in the cluster", index.getName(), preferredTierConfiguration)); + s -> String.format( + Locale.ROOT, + "[%s] lifecycle action [%s] waiting for [%s] shards to be moved to the [%s] tier (tier " + + "migration preference configuration is %s)", + index.getName(), + getKey().getAction(), + allocationPendingAllShards, + s, + preferredTierConfiguration + ) + ) + .orElseGet( + () -> String.format( + Locale.ROOT, + "index [%s] has a preference for tiers %s, but no nodes for any of those tiers are " + "available in the cluster", + index.getName(), + preferredTierConfiguration + ) + ); logger.debug(statusMessage); return new Result(false, new AllocationInfo(idxMeta.getNumberOfReplicas(), allocationPendingAllShards, true, statusMessage)); } else { - logger.debug("[{}] migration of index [{}] to tier [{}] (preference [{}]) complete", - getKey().getAction(), index, availableDestinationTier.orElse(""), preferredTierConfiguration); + logger.debug( + "[{}] migration of index [{}] to tier [{}] (preference [{}]) complete", + getKey().getAction(), + index, + availableDestinationTier.orElse(""), + preferredTierConfiguration + ); return new Result(true, null); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/DeleteAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/DeleteAction.java index e0c18f6fd46bc..b33a1412e3427 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/DeleteAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/DeleteAction.java @@ -7,11 +7,11 @@ package org.elasticsearch.xpack.core.ilm; import org.elasticsearch.client.Client; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -28,8 +28,10 @@ public class DeleteAction implements LifecycleAction { public static final ParseField DELETE_SEARCHABLE_SNAPSHOT_FIELD = new ParseField("delete_searchable_snapshot"); - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(NAME, - a -> new DeleteAction(a[0] == null ? true : (boolean) a[0])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + a -> new DeleteAction(a[0] == null ? true : (boolean) a[0]) + ); static { PARSER.declareBoolean(ConstructingObjectParser.optionalConstructorArg(), DELETE_SEARCHABLE_SNAPSHOT_FIELD); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/DeleteStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/DeleteStep.java index 93ea45b57bffe..8c1f49f835003 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/DeleteStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/DeleteStep.java @@ -43,23 +43,34 @@ public void performDuringNoSnapshot(IndexMetadata indexMetadata, ClusterState cu if (dataStream.getIndices().size() == 1 && dataStream.getIndices().get(0).equals(indexMetadata.getIndex())) { // This is the last index in the data stream, the entire stream // needs to be deleted, because we can't have an empty data stream - DeleteDataStreamAction.Request deleteReq = new DeleteDataStreamAction.Request(new String[]{dataStream.getName()}); - getClient().execute(DeleteDataStreamAction.INSTANCE, deleteReq, - ActionListener.wrap(response -> listener.onResponse(null), listener::onFailure)); + DeleteDataStreamAction.Request deleteReq = new DeleteDataStreamAction.Request(new String[] { dataStream.getName() }); + getClient().execute( + DeleteDataStreamAction.INSTANCE, + deleteReq, + ActionListener.wrap(response -> listener.onResponse(null), listener::onFailure) + ); return; } else if (dataStream.getWriteIndex().getName().equals(indexName)) { - String errorMessage = String.format(Locale.ROOT, "index [%s] is the write index for data stream [%s]. " + - "stopping execution of lifecycle [%s] as a data stream's write index cannot be deleted. manually rolling over the" + - " index will resume the execution of the policy as the index will not be the data stream's write index anymore", - indexName, dataStream.getName(), policyName); + String errorMessage = String.format( + Locale.ROOT, + "index [%s] is the write index for data stream [%s]. " + + "stopping execution of lifecycle [%s] as a data stream's write index cannot be deleted. manually rolling over the" + + " index will resume the execution of the policy as the index will not be the data stream's write index anymore", + indexName, + dataStream.getName(), + policyName + ); logger.debug(errorMessage); throw new IllegalStateException(errorMessage); } } - getClient().admin().indices() - .delete(new DeleteIndexRequest(indexName).masterNodeTimeout(TimeValue.MAX_VALUE), - ActionListener.wrap(response -> listener.onResponse(null), listener::onFailure)); + getClient().admin() + .indices() + .delete( + new DeleteIndexRequest(indexName).masterNodeTimeout(TimeValue.MAX_VALUE), + ActionListener.wrap(response -> listener.onResponse(null), listener::onFailure) + ); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ExplainLifecycleRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ExplainLifecycleRequest.java index d6f8b67ad630a..00bb4e6f70702 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ExplainLifecycleRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ExplainLifecycleRequest.java @@ -81,16 +81,23 @@ public boolean equals(Object obj) { return false; } ExplainLifecycleRequest other = (ExplainLifecycleRequest) obj; - return Objects.deepEquals(indices(), other.indices()) && - Objects.equals(indicesOptions(), other.indicesOptions()) && - Objects.equals(onlyErrors(), other.onlyErrors()) && - Objects.equals(onlyManaged(), other.onlyManaged()); + return Objects.deepEquals(indices(), other.indices()) + && Objects.equals(indicesOptions(), other.indicesOptions()) + && Objects.equals(onlyErrors(), other.onlyErrors()) + && Objects.equals(onlyManaged(), other.onlyManaged()); } @Override public String toString() { - return "ExplainLifecycleRequest [indices()=" + Arrays.toString(indices()) + ", indicesOptions()=" + indicesOptions() + - ", onlyErrors()=" + onlyErrors() + ", onlyManaged()=" + onlyManaged() + "]"; + return "ExplainLifecycleRequest [indices()=" + + Arrays.toString(indices()) + + ", indicesOptions()=" + + indicesOptions() + + ", onlyErrors()=" + + onlyErrors() + + ", onlyManaged()=" + + onlyManaged() + + "]"; } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ExplainLifecycleResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ExplainLifecycleResponse.java index ac70c24d721aa..9267f303daa0d 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ExplainLifecycleResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ExplainLifecycleResponse.java @@ -8,11 +8,11 @@ package org.elasticsearch.xpack.core.ilm; import org.elasticsearch.action.ActionResponse; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -39,11 +39,18 @@ public class ExplainLifecycleResponse extends ActionResponse implements ToXConte @SuppressWarnings("unchecked") private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "explain_lifecycle_response", a -> new ExplainLifecycleResponse(((List) a[0]).stream() - .collect(Collectors.toMap(IndexLifecycleExplainResponse::getIndex, Function.identity())))); + "explain_lifecycle_response", + a -> new ExplainLifecycleResponse( + ((List) a[0]).stream() + .collect(Collectors.toMap(IndexLifecycleExplainResponse::getIndex, Function.identity())) + ) + ); static { - PARSER.declareNamedObjects(ConstructingObjectParser.constructorArg(), (p, c, n) -> IndexLifecycleExplainResponse.PARSER.apply(p, c), - INDICES_FIELD); + PARSER.declareNamedObjects( + ConstructingObjectParser.constructorArg(), + (p, c, n) -> IndexLifecycleExplainResponse.PARSER.apply(p, c), + INDICES_FIELD + ); } public static ExplainLifecycleResponse fromXContent(XContentParser parser) { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ForceMergeAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ForceMergeAction.java index ab29f891f2269..2876b230ef45f 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ForceMergeAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ForceMergeAction.java @@ -11,17 +11,17 @@ import org.elasticsearch.client.Client; import org.elasticsearch.cluster.health.ClusterHealthStatus; import org.elasticsearch.cluster.metadata.IndexMetadata; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.index.codec.CodecService; +import org.elasticsearch.index.engine.EngineConfig; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.index.codec.CodecService; -import org.elasticsearch.index.engine.EngineConfig; import org.elasticsearch.xpack.core.ilm.Step.StepKey; import java.io.IOException; @@ -37,16 +37,16 @@ public class ForceMergeAction implements LifecycleAction { private static final Settings READ_ONLY_SETTINGS = Settings.builder().put(IndexMetadata.SETTING_BLOCKS_WRITE, true).build(); - private static final Settings BEST_COMPRESSION_SETTINGS = - Settings.builder().put(EngineConfig.INDEX_CODEC_SETTING.getKey(), CodecService.BEST_COMPRESSION_CODEC).build(); + private static final Settings BEST_COMPRESSION_SETTINGS = Settings.builder() + .put(EngineConfig.INDEX_CODEC_SETTING.getKey(), CodecService.BEST_COMPRESSION_CODEC) + .build(); public static final String NAME = "forcemerge"; public static final ParseField MAX_NUM_SEGMENTS_FIELD = new ParseField("max_num_segments"); public static final ParseField CODEC = new ParseField("index_codec"); public static final String CONDITIONAL_SKIP_FORCE_MERGE_STEP = BranchingStep.NAME + "-forcemerge-check-prerequisites"; - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(NAME, - false, a -> { + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(NAME, false, a -> { int maxNumSegments = (int) a[0]; String codec = a[1] != null ? (String) a[1] : null; return new ForceMergeAction(maxNumSegments, codec); @@ -66,8 +66,7 @@ public static ForceMergeAction parse(XContentParser parser) { public ForceMergeAction(int maxNumSegments, @Nullable String codec) { if (maxNumSegments <= 0) { - throw new IllegalArgumentException("[" + MAX_NUM_SEGMENTS_FIELD.getPreferredName() - + "] must be a positive integer"); + throw new IllegalArgumentException("[" + MAX_NUM_SEGMENTS_FIELD.getPreferredName() + "] must be a positive integer"); } this.maxNumSegments = maxNumSegments; if (codec != null && CodecService.BEST_COMPRESSION_CODEC.equals(codec) == false) { @@ -132,29 +131,48 @@ public List toSteps(Client client, String phase, Step.StepKey nextStepKey) StepKey forceMergeKey = new StepKey(phase, NAME, ForceMergeStep.NAME); StepKey countKey = new StepKey(phase, NAME, SegmentCountStep.NAME); - BranchingStep conditionalSkipShrinkStep = new BranchingStep(preForceMergeBranchingKey, checkNotWriteIndex, nextStepKey, + BranchingStep conditionalSkipShrinkStep = new BranchingStep( + preForceMergeBranchingKey, + checkNotWriteIndex, + nextStepKey, (index, clusterState) -> { IndexMetadata indexMetadata = clusterState.metadata().index(index); assert indexMetadata != null : "index " + index.getName() + " must exist in the cluster state"; if (indexMetadata.getSettings().get(LifecycleSettings.SNAPSHOT_INDEX_NAME) != null) { String policyName = LifecycleSettings.LIFECYCLE_NAME_SETTING.get(indexMetadata.getSettings()); - logger.warn("[{}] action is configured for index [{}] in policy [{}] which is mounted as searchable snapshot. " + - "Skipping this action", ForceMergeAction.NAME, index.getName(), policyName); + logger.warn( + "[{}] action is configured for index [{}] in policy [{}] which is mounted as searchable snapshot. " + + "Skipping this action", + ForceMergeAction.NAME, + index.getName(), + policyName + ); return true; } return false; - }); - CheckNotDataStreamWriteIndexStep checkNotWriteIndexStep = new CheckNotDataStreamWriteIndexStep(checkNotWriteIndex, - readOnlyKey); - UpdateSettingsStep readOnlyStep = - new UpdateSettingsStep(readOnlyKey, codecChange ? closeKey : forceMergeKey, client, READ_ONLY_SETTINGS); + } + ); + CheckNotDataStreamWriteIndexStep checkNotWriteIndexStep = new CheckNotDataStreamWriteIndexStep(checkNotWriteIndex, readOnlyKey); + UpdateSettingsStep readOnlyStep = new UpdateSettingsStep( + readOnlyKey, + codecChange ? closeKey : forceMergeKey, + client, + READ_ONLY_SETTINGS + ); CloseIndexStep closeIndexStep = new CloseIndexStep(closeKey, updateCompressionKey, client); - UpdateSettingsStep updateBestCompressionSettings = new UpdateSettingsStep(updateCompressionKey, - openKey, client, BEST_COMPRESSION_SETTINGS); + UpdateSettingsStep updateBestCompressionSettings = new UpdateSettingsStep( + updateCompressionKey, + openKey, + client, + BEST_COMPRESSION_SETTINGS + ); OpenIndexStep openIndexStep = new OpenIndexStep(openKey, waitForGreenIndexKey, client); - WaitForIndexColorStep waitForIndexGreenStep = new WaitForIndexColorStep(waitForGreenIndexKey, - forceMergeKey, ClusterHealthStatus.GREEN); + WaitForIndexColorStep waitForIndexGreenStep = new WaitForIndexColorStep( + waitForGreenIndexKey, + forceMergeKey, + ClusterHealthStatus.GREEN + ); ForceMergeStep forceMergeStep = new ForceMergeStep(forceMergeKey, countKey, client, maxNumSegments); SegmentCountStep segmentCountStep = new SegmentCountStep(countKey, nextStepKey, client, maxNumSegments); @@ -190,8 +208,7 @@ public boolean equals(Object obj) { return false; } ForceMergeAction other = (ForceMergeAction) obj; - return Objects.equals(this.maxNumSegments, other.maxNumSegments) - && Objects.equals(this.codec, other.codec); + return Objects.equals(this.maxNumSegments, other.maxNumSegments) && Objects.equals(this.codec, other.codec); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ForceMergeStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ForceMergeStep.java index ca31cc8f6dd87..2a27ae8ae3ee7 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ForceMergeStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ForceMergeStep.java @@ -47,32 +47,39 @@ public int getMaxNumSegments() { } @Override - public void performAction(IndexMetadata indexMetadata, ClusterState currentState, - ClusterStateObserver observer, ActionListener listener) { + public void performAction( + IndexMetadata indexMetadata, + ClusterState currentState, + ClusterStateObserver observer, + ActionListener listener + ) { String indexName = indexMetadata.getIndex().getName(); ForceMergeRequest request = new ForceMergeRequest(indexName); request.maxNumSegments(maxNumSegments); - getClient().admin().indices() - .forceMerge(request, ActionListener.wrap( - response -> { - if (response.getFailedShards() == 0) { - listener.onResponse(null); - } else { - DefaultShardOperationFailedException[] failures = response.getShardFailures(); - String policyName = LifecycleSettings.LIFECYCLE_NAME_SETTING.get(indexMetadata.getSettings()); - String errorMessage = - String.format(Locale.ROOT, "index [%s] in policy [%s] encountered failures [%s] on step [%s]", - indexName, policyName, - failures == null ? "n/a" : Strings.collectionToDelimitedString(Arrays.stream(failures) - .map(Strings::toString) - .collect(Collectors.toList()), ","), - NAME); - logger.warn(errorMessage); - // let's report it as a failure and retry - listener.onFailure(new ElasticsearchException(errorMessage)); - } - }, - listener::onFailure)); + getClient().admin().indices().forceMerge(request, ActionListener.wrap(response -> { + if (response.getFailedShards() == 0) { + listener.onResponse(null); + } else { + DefaultShardOperationFailedException[] failures = response.getShardFailures(); + String policyName = LifecycleSettings.LIFECYCLE_NAME_SETTING.get(indexMetadata.getSettings()); + String errorMessage = String.format( + Locale.ROOT, + "index [%s] in policy [%s] encountered failures [%s] on step [%s]", + indexName, + policyName, + failures == null + ? "n/a" + : Strings.collectionToDelimitedString( + Arrays.stream(failures).map(Strings::toString).collect(Collectors.toList()), + "," + ), + NAME + ); + logger.warn(errorMessage); + // let's report it as a failure and retry + listener.onFailure(new ElasticsearchException(errorMessage)); + } + }, listener::onFailure)); } @Override @@ -89,7 +96,6 @@ public boolean equals(Object obj) { return false; } ForceMergeStep other = (ForceMergeStep) obj; - return super.equals(obj) && - Objects.equals(maxNumSegments, other.maxNumSegments); + return super.equals(obj) && Objects.equals(maxNumSegments, other.maxNumSegments); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/FreezeAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/FreezeAction.java index f5bc8b8d6fb24..38e0789fe2bab 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/FreezeAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/FreezeAction.java @@ -37,15 +37,12 @@ public static FreezeAction parse(XContentParser parser) { return PARSER.apply(parser, null); } - public FreezeAction() { - } + public FreezeAction() {} - public FreezeAction(StreamInput in) { - } + public FreezeAction(StreamInput in) {} @Override - public void writeTo(StreamOutput out) throws IOException { - } + public void writeTo(StreamOutput out) throws IOException {} @Override public String getWriteableName() { @@ -70,25 +67,37 @@ public List toSteps(Client client, String phase, StepKey nextStepKey) { StepKey checkNotWriteIndex = new StepKey(phase, NAME, CheckNotDataStreamWriteIndexStep.NAME); StepKey freezeStepKey = new StepKey(phase, NAME, FreezeStep.NAME); - BranchingStep conditionalSkipFreezeStep = new BranchingStep(preFreezeMergeBranchingKey, checkNotWriteIndex, nextStepKey, + BranchingStep conditionalSkipFreezeStep = new BranchingStep( + preFreezeMergeBranchingKey, + checkNotWriteIndex, + nextStepKey, (index, clusterState) -> { IndexMetadata indexMetadata = clusterState.getMetadata().index(index); assert indexMetadata != null : "index " + index.getName() + " must exist in the cluster state"; String policyName = LifecycleSettings.LIFECYCLE_NAME_SETTING.get(indexMetadata.getSettings()); if (indexMetadata.getSettings().get(LifecycleSettings.SNAPSHOT_INDEX_NAME) != null) { - logger.warn("[{}] action is configured for index [{}] in policy [{}] which is mounted as searchable snapshot. " + - "Skipping this action", FreezeAction.NAME, index.getName(), policyName); + logger.warn( + "[{}] action is configured for index [{}] in policy [{}] which is mounted as searchable snapshot. " + + "Skipping this action", + FreezeAction.NAME, + index.getName(), + policyName + ); return true; } if (indexMetadata.getSettings().getAsBoolean("index.frozen", false)) { - logger.debug("skipping [{}] action for index [{}] in policy [{}] as the index is already frozen", FreezeAction.NAME, - index.getName(), policyName); + logger.debug( + "skipping [{}] action for index [{}] in policy [{}] as the index is already frozen", + FreezeAction.NAME, + index.getName(), + policyName + ); return true; } return false; - }); - CheckNotDataStreamWriteIndexStep checkNoWriteIndexStep = new CheckNotDataStreamWriteIndexStep(checkNotWriteIndex, - freezeStepKey); + } + ); + CheckNotDataStreamWriteIndexStep checkNoWriteIndexStep = new CheckNotDataStreamWriteIndexStep(checkNotWriteIndex, freezeStepKey); FreezeStep freezeStep = new FreezeStep(freezeStepKey, nextStepKey, client); return Arrays.asList(conditionalSkipFreezeStep, checkNoWriteIndexStep, freezeStep); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/GenerateSnapshotNameStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/GenerateSnapshotNameStep.java index 47492df01b123..68c5e65dc0d34 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/GenerateSnapshotNameStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/GenerateSnapshotNameStep.java @@ -69,8 +69,16 @@ public ClusterState performAction(Index index, ClusterState clusterState) { // this fails prior to the snapshot repository being recorded in the ilm metadata, the policy can just be corrected // and everything will pass on the subsequent retry if (clusterState.metadata().custom(RepositoriesMetadata.TYPE, RepositoriesMetadata.EMPTY).repository(snapshotRepository) == null) { - throw new IllegalStateException("repository [" + snapshotRepository + "] is missing. [" + policy + "] policy for " + - "index [" + index.getName() + "] cannot continue until the repository is created or the policy is changed"); + throw new IllegalStateException( + "repository [" + + snapshotRepository + + "] is missing. [" + + policy + + "] policy for " + + "index [" + + index.getName() + + "] cannot continue until the repository is created or the policy is changed" + ); } LifecycleExecutionState.Builder newCustomData = LifecycleExecutionState.builder(lifecycleState); @@ -82,8 +90,12 @@ public ClusterState performAction(Index index, ClusterState clusterState) { String snapshotName = generateSnapshotName(snapshotNamePrefix); ActionRequestValidationException validationException = validateGeneratedSnapshotName(snapshotNamePrefix, snapshotName); if (validationException != null) { - logger.warn("unable to generate a snapshot name as part of policy [{}] for index [{}] due to [{}]", - policy, index.getName(), validationException.getMessage()); + logger.warn( + "unable to generate a snapshot name as part of policy [{}] for index [{}] due to [{}]", + policy, + index.getName(), + validationException.getMessage() + ); throw validationException; } @@ -91,10 +103,11 @@ public ClusterState performAction(Index index, ClusterState clusterState) { } return ClusterState.builder(clusterState) - .metadata(Metadata.builder(clusterState.getMetadata()) - .put(IndexMetadata.builder(indexMetaData) - .putCustom(ILM_CUSTOM_METADATA_KEY, newCustomData.build().asMap())) - .build(false)) + .metadata( + Metadata.builder(clusterState.getMetadata()) + .put(IndexMetadata.builder(indexMetaData).putCustom(ILM_CUSTOM_METADATA_KEY, newCustomData.build().asMap())) + .build(false) + ) .build(); } @@ -117,8 +130,7 @@ public boolean equals(Object obj) { return false; } GenerateSnapshotNameStep other = (GenerateSnapshotNameStep) obj; - return super.equals(obj) && - Objects.equals(snapshotRepository, other.snapshotRepository); + return super.equals(obj) && Objects.equals(snapshotRepository, other.snapshotRepository); } /** @@ -155,8 +167,12 @@ public static ActionRequestValidationException validateGeneratedSnapshotName(Str err.addValidationError("invalid snapshot name [" + snapshotPrefix + "]: must be lowercase"); } if (Strings.validFileName(snapshotName) == false) { - err.addValidationError("invalid snapshot name [" + snapshotPrefix + "]: must not contain contain the following characters " + - Strings.INVALID_FILENAME_CHARS); + err.addValidationError( + "invalid snapshot name [" + + snapshotPrefix + + "]: must not contain contain the following characters " + + Strings.INVALID_FILENAME_CHARS + ); } if (err.validationErrors().size() > 0) { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/GenerateUniqueIndexNameStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/GenerateUniqueIndexNameStep.java index 216cdee02efb6..91bcef31a40e3 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/GenerateUniqueIndexNameStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/GenerateUniqueIndexNameStep.java @@ -13,8 +13,8 @@ import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.cluster.metadata.MetadataCreateIndexService; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.UUIDs; +import org.elasticsearch.core.Nullable; import org.elasticsearch.index.Index; import org.elasticsearch.indices.InvalidIndexNameException; import org.elasticsearch.xpack.core.ilm.LifecycleExecutionState.Builder; @@ -44,8 +44,12 @@ public class GenerateUniqueIndexNameStep extends ClusterStateActionStep { private final String prefix; private final BiFunction lifecycleStateSetter; - public GenerateUniqueIndexNameStep(StepKey key, StepKey nextStepKey, String prefix, - BiFunction lifecycleStateSetter) { + public GenerateUniqueIndexNameStep( + StepKey key, + StepKey nextStepKey, + String prefix, + BiFunction lifecycleStateSetter + ) { super(key, nextStepKey); this.prefix = prefix; this.lifecycleStateSetter = lifecycleStateSetter; @@ -82,8 +86,12 @@ public ClusterState performAction(Index index, ClusterState clusterState) { String generatedIndexName = generateValidIndexName(prefix, index.getName()); ActionRequestValidationException validationException = validateGeneratedIndexName(generatedIndexName, clusterState); if (validationException != null) { - logger.warn("unable to generate a valid index name as part of policy [{}] for index [{}] due to [{}]", - policy, index.getName(), validationException.getMessage()); + logger.warn( + "unable to generate a valid index name as part of policy [{}] for index [{}] due to [{}]", + policy, + index.getName(), + validationException.getMessage() + ); throw validationException; } lifecycleStateSetter.apply(generatedIndexName, newCustomData); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/IndexLifecycleExplainResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/IndexLifecycleExplainResponse.java index f55e5c36f57b3..a99748b7bf1d4 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/IndexLifecycleExplainResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/IndexLifecycleExplainResponse.java @@ -7,7 +7,6 @@ package org.elasticsearch.xpack.core.ilm; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.StreamInput; @@ -15,6 +14,7 @@ import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentType; @@ -51,28 +51,29 @@ public class IndexLifecycleExplainResponse implements ToXContentObject, Writeabl private static final ParseField SNAPSHOT_NAME = new ParseField("snapshot_name"); public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "index_lifecycle_explain_response", - a -> new IndexLifecycleExplainResponse( - (String) a[0], - (boolean) a[1], - (String) a[2], - (Long) (a[3]), - (String) a[4], - (String) a[5], - (String) a[6], - (String) a[7], - (Boolean) a[14], - (Integer) a[15], - (Long) (a[8]), - (Long) (a[9]), - (Long) (a[10]), - (String) a[16], - (String) a[17], - (String) a[18], - (BytesReference) a[11], - (PhaseExecutionInfo) a[12] - // a[13] == "age" - )); + "index_lifecycle_explain_response", + a -> new IndexLifecycleExplainResponse( + (String) a[0], + (boolean) a[1], + (String) a[2], + (Long) (a[3]), + (String) a[4], + (String) a[5], + (String) a[6], + (String) a[7], + (Boolean) a[14], + (Integer) a[15], + (Long) (a[8]), + (Long) (a[9]), + (Long) (a[10]), + (String) a[16], + (String) a[17], + (String) a[18], + (BytesReference) a[11], + (PhaseExecutionInfo) a[12] + // a[13] == "age" + ) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), INDEX_FIELD); PARSER.declareBoolean(ConstructingObjectParser.constructorArg(), MANAGED_BY_ILM_FIELD); @@ -90,8 +91,11 @@ public class IndexLifecycleExplainResponse implements ToXContentObject, Writeabl builder.copyCurrentStructure(p); return BytesReference.bytes(builder); }, STEP_INFO_FIELD); - PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> PhaseExecutionInfo.parse(p, ""), - PHASE_EXECUTION_INFO); + PARSER.declareObject( + ConstructingObjectParser.optionalConstructorArg(), + (p, c) -> PhaseExecutionInfo.parse(p, ""), + PHASE_EXECUTION_INFO + ); PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), AGE_FIELD); PARSER.declareBoolean(ConstructingObjectParser.optionalConstructorArg(), IS_AUTO_RETRYABLE_ERROR_FIELD); PARSER.declareInt(ConstructingObjectParser.optionalConstructorArg(), FAILED_STEP_RETRY_COUNT_FIELD); @@ -119,27 +123,90 @@ public class IndexLifecycleExplainResponse implements ToXContentObject, Writeabl private final String snapshotName; private final String shrinkIndexName; - public static IndexLifecycleExplainResponse newManagedIndexResponse(String index, String policyName, Long lifecycleDate, - String phase, String action, String step, String failedStep, - Boolean isAutoRetryableError, Integer failedStepRetryCount, - Long phaseTime, Long actionTime, Long stepTime, - String repositoryName, String snapshotName, String shrinkIndexName, - BytesReference stepInfo, PhaseExecutionInfo phaseExecutionInfo) { - return new IndexLifecycleExplainResponse(index, true, policyName, lifecycleDate, phase, action, step, failedStep, - isAutoRetryableError, failedStepRetryCount, phaseTime, actionTime, stepTime, repositoryName, snapshotName, shrinkIndexName, - stepInfo, phaseExecutionInfo); + public static IndexLifecycleExplainResponse newManagedIndexResponse( + String index, + String policyName, + Long lifecycleDate, + String phase, + String action, + String step, + String failedStep, + Boolean isAutoRetryableError, + Integer failedStepRetryCount, + Long phaseTime, + Long actionTime, + Long stepTime, + String repositoryName, + String snapshotName, + String shrinkIndexName, + BytesReference stepInfo, + PhaseExecutionInfo phaseExecutionInfo + ) { + return new IndexLifecycleExplainResponse( + index, + true, + policyName, + lifecycleDate, + phase, + action, + step, + failedStep, + isAutoRetryableError, + failedStepRetryCount, + phaseTime, + actionTime, + stepTime, + repositoryName, + snapshotName, + shrinkIndexName, + stepInfo, + phaseExecutionInfo + ); } public static IndexLifecycleExplainResponse newUnmanagedIndexResponse(String index) { - return new IndexLifecycleExplainResponse(index, false, null, null, null, null, null, null, null, null, null, null, null, null, - null, null, null, null); + return new IndexLifecycleExplainResponse( + index, + false, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null + ); } - private IndexLifecycleExplainResponse(String index, boolean managedByILM, String policyName, Long lifecycleDate, - String phase, String action, String step, String failedStep, Boolean isAutoRetryableError, - Integer failedStepRetryCount, Long phaseTime, Long actionTime, Long stepTime, - String repositoryName, String snapshotName, String shrinkIndexName, BytesReference stepInfo, - PhaseExecutionInfo phaseExecutionInfo) { + private IndexLifecycleExplainResponse( + String index, + boolean managedByILM, + String policyName, + Long lifecycleDate, + String phase, + String action, + String step, + String failedStep, + Boolean isAutoRetryableError, + Integer failedStepRetryCount, + Long phaseTime, + Long actionTime, + Long stepTime, + String repositoryName, + String snapshotName, + String shrinkIndexName, + BytesReference stepInfo, + PhaseExecutionInfo phaseExecutionInfo + ) { if (managedByILM) { if (policyName == null) { throw new IllegalArgumentException("[" + POLICY_NAME_FIELD.getPreferredName() + "] cannot be null for managed index"); @@ -147,16 +214,37 @@ private IndexLifecycleExplainResponse(String index, boolean managedByILM, String // check to make sure that step details are either all null or all set. long numNull = Stream.of(phase, action, step).filter(Objects::isNull).count(); if (numNull > 0 && numNull < 3) { - throw new IllegalArgumentException("managed index response must have complete step details [" + - PHASE_FIELD.getPreferredName() + "=" + phase + ", " + - ACTION_FIELD.getPreferredName() + "=" + action + ", " + - STEP_FIELD.getPreferredName() + "=" + step + "]"); + throw new IllegalArgumentException( + "managed index response must have complete step details [" + + PHASE_FIELD.getPreferredName() + + "=" + + phase + + ", " + + ACTION_FIELD.getPreferredName() + + "=" + + action + + ", " + + STEP_FIELD.getPreferredName() + + "=" + + step + + "]" + ); } } else { - if (policyName != null || lifecycleDate != null || phase != null || action != null || step != null || failedStep != null - || phaseTime != null || actionTime != null || stepTime != null || stepInfo != null || phaseExecutionInfo != null) { + if (policyName != null + || lifecycleDate != null + || phase != null + || action != null + || step != null + || failedStep != null + || phaseTime != null + || actionTime != null + || stepTime != null + || stepInfo != null + || phaseExecutionInfo != null) { throw new IllegalArgumentException( - "Unmanaged index response must only contain fields: [" + MANAGED_BY_ILM_FIELD + ", " + INDEX_FIELD + "]"); + "Unmanaged index response must only contain fields: [" + MANAGED_BY_ILM_FIELD + ", " + INDEX_FIELD + "]" + ); } } this.index = index; @@ -383,9 +471,26 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws @Override public int hashCode() { - return Objects.hash(index, managedByILM, policyName, lifecycleDate, phase, action, step, failedStep, isAutoRetryableError, - failedStepRetryCount, phaseTime, actionTime, stepTime, repositoryName, snapshotName, shrinkIndexName, stepInfo, - phaseExecutionInfo); + return Objects.hash( + index, + managedByILM, + policyName, + lifecycleDate, + phase, + action, + step, + failedStep, + isAutoRetryableError, + failedStepRetryCount, + phaseTime, + actionTime, + stepTime, + repositoryName, + snapshotName, + shrinkIndexName, + stepInfo, + phaseExecutionInfo + ); } @Override @@ -397,24 +502,24 @@ public boolean equals(Object obj) { return false; } IndexLifecycleExplainResponse other = (IndexLifecycleExplainResponse) obj; - return Objects.equals(index, other.index) && - Objects.equals(managedByILM, other.managedByILM) && - Objects.equals(policyName, other.policyName) && - Objects.equals(lifecycleDate, other.lifecycleDate) && - Objects.equals(phase, other.phase) && - Objects.equals(action, other.action) && - Objects.equals(step, other.step) && - Objects.equals(failedStep, other.failedStep) && - Objects.equals(isAutoRetryableError, other.isAutoRetryableError) && - Objects.equals(failedStepRetryCount, other.failedStepRetryCount) && - Objects.equals(phaseTime, other.phaseTime) && - Objects.equals(actionTime, other.actionTime) && - Objects.equals(stepTime, other.stepTime) && - Objects.equals(repositoryName, other.repositoryName) && - Objects.equals(snapshotName, other.snapshotName) && - Objects.equals(shrinkIndexName, other.shrinkIndexName) && - Objects.equals(stepInfo, other.stepInfo) && - Objects.equals(phaseExecutionInfo, other.phaseExecutionInfo); + return Objects.equals(index, other.index) + && Objects.equals(managedByILM, other.managedByILM) + && Objects.equals(policyName, other.policyName) + && Objects.equals(lifecycleDate, other.lifecycleDate) + && Objects.equals(phase, other.phase) + && Objects.equals(action, other.action) + && Objects.equals(step, other.step) + && Objects.equals(failedStep, other.failedStep) + && Objects.equals(isAutoRetryableError, other.isAutoRetryableError) + && Objects.equals(failedStepRetryCount, other.failedStepRetryCount) + && Objects.equals(phaseTime, other.phaseTime) + && Objects.equals(actionTime, other.actionTime) + && Objects.equals(stepTime, other.stepTime) + && Objects.equals(repositoryName, other.repositoryName) + && Objects.equals(snapshotName, other.snapshotName) + && Objects.equals(shrinkIndexName, other.shrinkIndexName) + && Objects.equals(stepInfo, other.stepInfo) + && Objects.equals(phaseExecutionInfo, other.phaseExecutionInfo); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/IndexLifecycleFeatureSetUsage.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/IndexLifecycleFeatureSetUsage.java index 506f34ae0a91e..1c134fdd82452 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/IndexLifecycleFeatureSetUsage.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/IndexLifecycleFeatureSetUsage.java @@ -12,10 +12,10 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.xpack.core.XPackFeatureSet; import org.elasticsearch.xpack.core.XPackField; @@ -52,7 +52,7 @@ public void writeTo(StreamOutput out) throws IOException { } public IndexLifecycleFeatureSetUsage() { - this((List)null); + this((List) null); } public IndexLifecycleFeatureSetUsage(List policyStats) { @@ -86,9 +86,9 @@ public boolean equals(Object obj) { return false; } IndexLifecycleFeatureSetUsage other = (IndexLifecycleFeatureSetUsage) obj; - return Objects.equals(available, other.available) && - Objects.equals(enabled, other.enabled) && - Objects.equals(policyStats, other.policyStats); + return Objects.equals(available, other.available) + && Objects.equals(enabled, other.enabled) + && Objects.equals(policyStats, other.policyStats); } public static final class PolicyStats implements ToXContentObject, Writeable { @@ -145,8 +145,7 @@ public boolean equals(Object obj) { return false; } PolicyStats other = (PolicyStats) obj; - return Objects.equals(phaseStats, other.phaseStats) && - Objects.equals(indicesManaged, other.indicesManaged); + return Objects.equals(phaseStats, other.phaseStats) && Objects.equals(indicesManaged, other.indicesManaged); } @Override @@ -218,9 +217,9 @@ public boolean equals(Object obj) { return false; } PhaseStats other = (PhaseStats) obj; - return Objects.equals(minimumAge, other.minimumAge) && - Objects.deepEquals(configurations, other.configurations) && - Objects.deepEquals(actionNames, other.actionNames); + return Objects.equals(minimumAge, other.minimumAge) + && Objects.deepEquals(configurations, other.configurations) + && Objects.deepEquals(actionNames, other.actionNames); } } @@ -314,14 +313,31 @@ public Builder setShrinkNumberOfShards(Integer shrinkNumberOfShards) { } public ActionConfigStats build() { - return new ActionConfigStats(allocateNumberOfReplicas, forceMergeMaxNumberOfSegments, rolloverMaxAge, rolloverMaxDocs, - rolloverMaxPrimaryShardSize, rolloverMaxSize, setPriorityPriority, shrinkMaxPrimaryShardSize, shrinkNumberOfShards); + return new ActionConfigStats( + allocateNumberOfReplicas, + forceMergeMaxNumberOfSegments, + rolloverMaxAge, + rolloverMaxDocs, + rolloverMaxPrimaryShardSize, + rolloverMaxSize, + setPriorityPriority, + shrinkMaxPrimaryShardSize, + shrinkNumberOfShards + ); } } - public ActionConfigStats(Integer allocateNumberOfReplicas, Integer forceMergeMaxNumberOfSegments, TimeValue rolloverMaxAge, - Long rolloverMaxDocs, ByteSizeValue rolloverMaxPrimaryShardSize, ByteSizeValue rolloverMaxSize, - Integer setPriorityPriority, ByteSizeValue shrinkMaxPrimaryShardSize, Integer shrinkNumberOfShards) { + public ActionConfigStats( + Integer allocateNumberOfReplicas, + Integer forceMergeMaxNumberOfSegments, + TimeValue rolloverMaxAge, + Long rolloverMaxDocs, + ByteSizeValue rolloverMaxPrimaryShardSize, + ByteSizeValue rolloverMaxSize, + Integer setPriorityPriority, + ByteSizeValue shrinkMaxPrimaryShardSize, + Integer shrinkNumberOfShards + ) { this.allocateNumberOfReplicas = allocateNumberOfReplicas; this.forceMergeMaxNumberOfSegments = forceMergeMaxNumberOfSegments; this.rolloverMaxAge = rolloverMaxAge; @@ -385,10 +401,14 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.field(RolloverAction.MAX_SIZE_FIELD.getPreferredName() + "_bytes", rolloverMaxSize.getBytes()); } if (rolloverMaxPrimaryShardSize != null) { - builder.field(RolloverAction.MAX_PRIMARY_SHARD_SIZE_FIELD.getPreferredName(), - rolloverMaxPrimaryShardSize.getStringRep()); - builder.field(RolloverAction.MAX_PRIMARY_SHARD_SIZE_FIELD.getPreferredName() + "_bytes", - rolloverMaxPrimaryShardSize.getBytes()); + builder.field( + RolloverAction.MAX_PRIMARY_SHARD_SIZE_FIELD.getPreferredName(), + rolloverMaxPrimaryShardSize.getStringRep() + ); + builder.field( + RolloverAction.MAX_PRIMARY_SHARD_SIZE_FIELD.getPreferredName() + "_bytes", + rolloverMaxPrimaryShardSize.getBytes() + ); } builder.endObject(); } @@ -453,21 +473,30 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; ActionConfigStats that = (ActionConfigStats) o; - return Objects.equals(allocateNumberOfReplicas, that.allocateNumberOfReplicas) && - Objects.equals(forceMergeMaxNumberOfSegments, that.forceMergeMaxNumberOfSegments) && - Objects.equals(rolloverMaxAge, that.rolloverMaxAge) && - Objects.equals(rolloverMaxDocs, that.rolloverMaxDocs) && - Objects.equals(rolloverMaxPrimaryShardSize, that.rolloverMaxPrimaryShardSize) && - Objects.equals(rolloverMaxSize, that.rolloverMaxSize) && - Objects.equals(setPriorityPriority, that.setPriorityPriority) && - Objects.equals(shrinkMaxPrimaryShardSize, that.shrinkMaxPrimaryShardSize) && - Objects.equals(shrinkNumberOfShards, that.shrinkNumberOfShards); + return Objects.equals(allocateNumberOfReplicas, that.allocateNumberOfReplicas) + && Objects.equals(forceMergeMaxNumberOfSegments, that.forceMergeMaxNumberOfSegments) + && Objects.equals(rolloverMaxAge, that.rolloverMaxAge) + && Objects.equals(rolloverMaxDocs, that.rolloverMaxDocs) + && Objects.equals(rolloverMaxPrimaryShardSize, that.rolloverMaxPrimaryShardSize) + && Objects.equals(rolloverMaxSize, that.rolloverMaxSize) + && Objects.equals(setPriorityPriority, that.setPriorityPriority) + && Objects.equals(shrinkMaxPrimaryShardSize, that.shrinkMaxPrimaryShardSize) + && Objects.equals(shrinkNumberOfShards, that.shrinkNumberOfShards); } @Override public int hashCode() { - return Objects.hash(allocateNumberOfReplicas, forceMergeMaxNumberOfSegments, rolloverMaxAge, rolloverMaxDocs, - rolloverMaxPrimaryShardSize, rolloverMaxSize, setPriorityPriority, shrinkMaxPrimaryShardSize, shrinkNumberOfShards); + return Objects.hash( + allocateNumberOfReplicas, + forceMergeMaxNumberOfSegments, + rolloverMaxAge, + rolloverMaxDocs, + rolloverMaxPrimaryShardSize, + rolloverMaxSize, + setPriorityPriority, + shrinkMaxPrimaryShardSize, + shrinkNumberOfShards + ); } } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/IndexLifecycleMetadata.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/IndexLifecycleMetadata.java index d9f2c6051eab0..97d4249049916 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/IndexLifecycleMetadata.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/IndexLifecycleMetadata.java @@ -13,11 +13,11 @@ import org.elasticsearch.cluster.NamedDiff; import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.cluster.metadata.Metadata.Custom; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; @@ -30,7 +30,6 @@ import java.util.function.Function; import java.util.stream.Collectors; - public class IndexLifecycleMetadata implements Metadata.Custom { public static final String TYPE = "index_lifecycle"; public static final ParseField OPERATION_MODE_FIELD = new ParseField("operation_mode"); @@ -38,16 +37,21 @@ public class IndexLifecycleMetadata implements Metadata.Custom { public static final IndexLifecycleMetadata EMPTY = new IndexLifecycleMetadata(Collections.emptySortedMap(), OperationMode.RUNNING); @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(TYPE, - a -> new IndexLifecycleMetadata( - ((List) a[0]).stream() - .collect(Collectors.toMap(LifecyclePolicyMetadata::getName, Function.identity())), - OperationMode.valueOf((String) a[1]))); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + TYPE, + a -> new IndexLifecycleMetadata( + ((List) a[0]).stream() + .collect(Collectors.toMap(LifecyclePolicyMetadata::getName, Function.identity())), + OperationMode.valueOf((String) a[1]) + ) + ); static { - PARSER.declareNamedObjects(ConstructingObjectParser.constructorArg(), (p, c, n) -> LifecyclePolicyMetadata.parse(p, n), - v -> { - throw new IllegalArgumentException("ordered " + POLICIES_FIELD.getPreferredName() + " are not supported"); - }, POLICIES_FIELD); + PARSER.declareNamedObjects( + ConstructingObjectParser.constructorArg(), + (p, c, n) -> LifecyclePolicyMetadata.parse(p, n), + v -> { throw new IllegalArgumentException("ordered " + POLICIES_FIELD.getPreferredName() + " are not supported"); }, + POLICIES_FIELD + ); PARSER.declareString(ConstructingObjectParser.constructorArg(), OPERATION_MODE_FIELD); } @@ -84,8 +88,10 @@ public OperationMode getOperationMode() { } public Map getPolicies() { - return policyMetadatas.values().stream().map(LifecyclePolicyMetadata::getPolicy) - .collect(Collectors.toMap(LifecyclePolicy::getName, Function.identity())); + return policyMetadatas.values() + .stream() + .map(LifecyclePolicyMetadata::getPolicy) + .collect(Collectors.toMap(LifecyclePolicy::getName, Function.identity())); } @Override @@ -129,8 +135,7 @@ public boolean equals(Object obj) { return false; } IndexLifecycleMetadata other = (IndexLifecycleMetadata) obj; - return Objects.equals(policyMetadatas, other.policyMetadatas) - && Objects.equals(operationMode, other.operationMode); + return Objects.equals(policyMetadatas, other.policyMetadatas) && Objects.equals(operationMode, other.operationMode); } @Override @@ -149,15 +154,20 @@ public static class IndexLifecycleMetadataDiff implements NamedDiff newPolicies = new TreeMap<>( - policies.apply(((IndexLifecycleMetadata) part).policyMetadatas)); + policies.apply(((IndexLifecycleMetadata) part).policyMetadatas) + ); return new IndexLifecycleMetadata(newPolicies, this.operationMode); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/IndexLifecycleOriginationDateParser.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/IndexLifecycleOriginationDateParser.java index 896f1e5fb7a39..e5cfb54415ef1 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/IndexLifecycleOriginationDateParser.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/IndexLifecycleOriginationDateParser.java @@ -26,8 +26,8 @@ public class IndexLifecycleOriginationDateParser { * Determines if the origination date needs to be parsed from the index name. */ public static boolean shouldParseIndexName(Settings indexSettings) { - return indexSettings.getAsLong(LIFECYCLE_ORIGINATION_DATE, -1L) == -1L && - indexSettings.getAsBoolean(LIFECYCLE_PARSE_ORIGINATION_DATE, false); + return indexSettings.getAsLong(LIFECYCLE_ORIGINATION_DATE, -1L) == -1L + && indexSettings.getAsBoolean(LIFECYCLE_PARSE_ORIGINATION_DATE, false); } /** @@ -41,8 +41,15 @@ public static long parseIndexNameAndExtractDate(String indexName) { try { return DATE_FORMATTER.parseMillis(dateAsString); } catch (ElasticsearchParseException | IllegalArgumentException e) { - throw new IllegalArgumentException("index name [" + indexName + "] contains date [" + dateAsString + "] which " + - "couldn't be parsed using the 'yyyy.MM.dd' format", e); + throw new IllegalArgumentException( + "index name [" + + indexName + + "] contains date [" + + dateAsString + + "] which " + + "couldn't be parsed using the 'yyyy.MM.dd' format", + e + ); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/InitializePolicyContextStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/InitializePolicyContextStep.java index d541106dae5d0..a5c20b4985557 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/InitializePolicyContextStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/InitializePolicyContextStep.java @@ -50,10 +50,11 @@ public ClusterState performAction(Index index, ClusterState clusterState) { if (shouldParseIndexName(indexMetadata.getSettings())) { long parsedOriginationDate = parseIndexNameAndExtractDate(index.getName()); indexMetadataBuilder.settingsVersion(indexMetadata.getSettingsVersion() + 1) - .settings(Settings.builder() - .put(indexMetadata.getSettings()) - .put(LifecycleSettings.LIFECYCLE_ORIGINATION_DATE, parsedOriginationDate) - .build() + .settings( + Settings.builder() + .put(indexMetadata.getSettings()) + .put(LifecycleSettings.LIFECYCLE_ORIGINATION_DATE, parsedOriginationDate) + .build() ); } } catch (Exception e) { @@ -67,9 +68,7 @@ public ClusterState performAction(Index index, ClusterState clusterState) { newCustomData.setIndexCreationDate(indexMetadata.getCreationDate()); indexMetadataBuilder.putCustom(ILM_CUSTOM_METADATA_KEY, newCustomData.build().asMap()); - newClusterStateBuilder.metadata( - Metadata.builder(clusterState.getMetadata()).put(indexMetadataBuilder).build(false) - ); + newClusterStateBuilder.metadata(Metadata.builder(clusterState.getMetadata()).put(indexMetadataBuilder).build(false)); return newClusterStateBuilder.build(); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/LifecycleAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/LifecycleAction.java index 35d26fa36eb15..02a7b10cf70f3 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/LifecycleAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/LifecycleAction.java @@ -7,10 +7,10 @@ package org.elasticsearch.xpack.core.ilm; import org.elasticsearch.client.Client; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.io.stream.NamedWriteable; -import org.elasticsearch.xcontent.ToXContentObject; +import org.elasticsearch.core.Nullable; import org.elasticsearch.license.XPackLicenseState; +import org.elasticsearch.xcontent.ToXContentObject; import java.util.List; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/LifecycleExecutionState.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/LifecycleExecutionState.java index f4651be75bcc2..380a8db2a4cf8 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/LifecycleExecutionState.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/LifecycleExecutionState.java @@ -9,8 +9,8 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.cluster.metadata.IndexMetadata; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; +import org.elasticsearch.core.Nullable; import java.util.Collections; import java.util.HashMap; @@ -40,7 +40,7 @@ public class LifecycleExecutionState { private static final String SNAPSHOT_NAME = "snapshot_name"; private static final String SNAPSHOT_REPOSITORY = "snapshot_repository"; private static final String SNAPSHOT_INDEX_NAME = "snapshot_index_name"; - private static final String SHRINK_INDEX_NAME ="shrink_index_name"; + private static final String SHRINK_INDEX_NAME = "shrink_index_name"; private static final String ROLLUP_INDEX_NAME = "rollup_index_name"; public static final LifecycleExecutionState EMPTY_STATE = LifecycleExecutionState.builder().build(); @@ -63,10 +63,25 @@ public class LifecycleExecutionState { private final String snapshotIndexName; private final String rollupIndexName; - private LifecycleExecutionState(String phase, String action, String step, String failedStep, Boolean isAutoRetryableError, - Integer failedStepRetryCount, String stepInfo, String phaseDefinition, Long lifecycleDate, - Long phaseTime, Long actionTime, Long stepTime, String snapshotRepository, String snapshotName, - String shrinkIndexName, String snapshotIndexName, String rollupIndexName) { + private LifecycleExecutionState( + String phase, + String action, + String step, + String failedStep, + Boolean isAutoRetryableError, + Integer failedStepRetryCount, + String stepInfo, + String phaseDefinition, + Long lifecycleDate, + Long phaseTime, + Long actionTime, + Long stepTime, + String snapshotRepository, + String snapshotName, + String shrinkIndexName, + String snapshotIndexName, + String rollupIndexName + ) { this.phase = phase; this.action = action; this.step = step; @@ -113,6 +128,7 @@ public static boolean isFrozenPhase(IndexMetadata indexMetadata) { // used heavily by autoscaling. return customData != null && TimeseriesLifecycleType.FROZEN_PHASE.equals(customData.get(PHASE)); } + /** * Retrieves the current {@link Step.StepKey} from the lifecycle state. Note that * it is illegal for the step to be set with the phase and/or action unset, @@ -143,8 +159,7 @@ public static Builder builder() { } public static Builder builder(LifecycleExecutionState state) { - return new Builder() - .setPhase(state.phase) + return new Builder().setPhase(state.phase) .setAction(state.action) .setStep(state.step) .setFailedStep(state.failedStep) @@ -214,8 +229,12 @@ static LifecycleExecutionState fromCustomMetadata(Map customData try { builder.setIndexCreationDate(Long.parseLong(indexCreationDate)); } catch (NumberFormatException e) { - throw new ElasticsearchException("Custom metadata field [{}] does not contain a valid long. Actual value: [{}]", - e, INDEX_CREATION_DATE, customData.get(INDEX_CREATION_DATE)); + throw new ElasticsearchException( + "Custom metadata field [{}] does not contain a valid long. Actual value: [{}]", + e, + INDEX_CREATION_DATE, + customData.get(INDEX_CREATION_DATE) + ); } } String phaseTime = customData.get(PHASE_TIME); @@ -223,8 +242,12 @@ static LifecycleExecutionState fromCustomMetadata(Map customData try { builder.setPhaseTime(Long.parseLong(phaseTime)); } catch (NumberFormatException e) { - throw new ElasticsearchException("Custom metadata field [{}] does not contain a valid long. Actual value: [{}]", - e, PHASE_TIME, customData.get(PHASE_TIME)); + throw new ElasticsearchException( + "Custom metadata field [{}] does not contain a valid long. Actual value: [{}]", + e, + PHASE_TIME, + customData.get(PHASE_TIME) + ); } } String actionTime = customData.get(ACTION_TIME); @@ -232,8 +255,12 @@ static LifecycleExecutionState fromCustomMetadata(Map customData try { builder.setActionTime(Long.parseLong(actionTime)); } catch (NumberFormatException e) { - throw new ElasticsearchException("Custom metadata field [{}] does not contain a valid long. Actual value: [{}]", - e, ACTION_TIME, customData.get(ACTION_TIME)); + throw new ElasticsearchException( + "Custom metadata field [{}] does not contain a valid long. Actual value: [{}]", + e, + ACTION_TIME, + customData.get(ACTION_TIME) + ); } } String stepTime = customData.get(STEP_TIME); @@ -241,8 +268,12 @@ static LifecycleExecutionState fromCustomMetadata(Map customData try { builder.setStepTime(Long.parseLong(stepTime)); } catch (NumberFormatException e) { - throw new ElasticsearchException("Custom metadata field [{}] does not contain a valid long. Actual value: [{}]", - e, STEP_TIME, customData.get(STEP_TIME)); + throw new ElasticsearchException( + "Custom metadata field [{}] does not contain a valid long. Actual value: [{}]", + e, + STEP_TIME, + customData.get(STEP_TIME) + ); } } String snapshotIndexName = customData.get(SNAPSHOT_INDEX_NAME); @@ -390,30 +421,46 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; LifecycleExecutionState that = (LifecycleExecutionState) o; - return Objects.equals(getLifecycleDate(), that.getLifecycleDate()) && - Objects.equals(getPhaseTime(), that.getPhaseTime()) && - Objects.equals(getActionTime(), that.getActionTime()) && - Objects.equals(getStepTime(), that.getStepTime()) && - Objects.equals(getPhase(), that.getPhase()) && - Objects.equals(getAction(), that.getAction()) && - Objects.equals(getStep(), that.getStep()) && - Objects.equals(getFailedStep(), that.getFailedStep()) && - Objects.equals(isAutoRetryableError(), that.isAutoRetryableError()) && - Objects.equals(getFailedStepRetryCount(), that.getFailedStepRetryCount()) && - Objects.equals(getStepInfo(), that.getStepInfo()) && - Objects.equals(getSnapshotRepository(), that.getSnapshotRepository()) && - Objects.equals(getSnapshotName(), that.getSnapshotName()) && - Objects.equals(getSnapshotIndexName(), that.getSnapshotIndexName()) && - Objects.equals(getShrinkIndexName(), that.getShrinkIndexName()) && - Objects.equals(getRollupIndexName(), that.getRollupIndexName()) && - Objects.equals(getPhaseDefinition(), that.getPhaseDefinition()); + return Objects.equals(getLifecycleDate(), that.getLifecycleDate()) + && Objects.equals(getPhaseTime(), that.getPhaseTime()) + && Objects.equals(getActionTime(), that.getActionTime()) + && Objects.equals(getStepTime(), that.getStepTime()) + && Objects.equals(getPhase(), that.getPhase()) + && Objects.equals(getAction(), that.getAction()) + && Objects.equals(getStep(), that.getStep()) + && Objects.equals(getFailedStep(), that.getFailedStep()) + && Objects.equals(isAutoRetryableError(), that.isAutoRetryableError()) + && Objects.equals(getFailedStepRetryCount(), that.getFailedStepRetryCount()) + && Objects.equals(getStepInfo(), that.getStepInfo()) + && Objects.equals(getSnapshotRepository(), that.getSnapshotRepository()) + && Objects.equals(getSnapshotName(), that.getSnapshotName()) + && Objects.equals(getSnapshotIndexName(), that.getSnapshotIndexName()) + && Objects.equals(getShrinkIndexName(), that.getShrinkIndexName()) + && Objects.equals(getRollupIndexName(), that.getRollupIndexName()) + && Objects.equals(getPhaseDefinition(), that.getPhaseDefinition()); } @Override public int hashCode() { - return Objects.hash(getPhase(), getAction(), getStep(), getFailedStep(), isAutoRetryableError(), getFailedStepRetryCount(), - getStepInfo(), getPhaseDefinition(), getLifecycleDate(), getPhaseTime(), getActionTime(), getStepTime(), - getSnapshotRepository(), getSnapshotName(), getSnapshotIndexName(), getShrinkIndexName(), getRollupIndexName()); + return Objects.hash( + getPhase(), + getAction(), + getStep(), + getFailedStep(), + isAutoRetryableError(), + getFailedStepRetryCount(), + getStepInfo(), + getPhaseDefinition(), + getLifecycleDate(), + getPhaseTime(), + getActionTime(), + getStepTime(), + getSnapshotRepository(), + getSnapshotName(), + getSnapshotIndexName(), + getShrinkIndexName(), + getRollupIndexName() + ); } @Override @@ -526,9 +573,25 @@ public Builder setRollupIndexName(String rollupIndexName) { } public LifecycleExecutionState build() { - return new LifecycleExecutionState(phase, action, step, failedStep, isAutoRetryableError, failedStepRetryCount, stepInfo, - phaseDefinition, indexCreationDate, phaseTime, actionTime, stepTime, snapshotRepository, snapshotName, shrinkIndexName, - snapshotIndexName, rollupIndexName); + return new LifecycleExecutionState( + phase, + action, + step, + failedStep, + isAutoRetryableError, + failedStepRetryCount, + stepInfo, + phaseDefinition, + indexCreationDate, + phaseTime, + actionTime, + stepTime, + snapshotRepository, + snapshotName, + shrinkIndexName, + snapshotIndexName, + rollupIndexName + ); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/LifecyclePolicy.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/LifecyclePolicy.java index e56f7ddf77680..9a3c397644efa 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/LifecyclePolicy.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/LifecyclePolicy.java @@ -12,13 +12,13 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.xpack.core.ilm.Step.StepKey; import java.io.IOException; @@ -40,24 +40,29 @@ * {@link Phase}s and {@link LifecycleAction}s are allowed to be defined and in which order * they are executed. */ -public class LifecyclePolicy extends AbstractDiffable - implements ToXContentObject, Diffable { +public class LifecyclePolicy extends AbstractDiffable implements ToXContentObject, Diffable { private static final int MAX_INDEX_NAME_BYTES = 255; public static final ParseField PHASES_FIELD = new ParseField("phases"); private static final ParseField METADATA = new ParseField("_meta"); @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("lifecycle_policy", false, - (a, name) -> { - List phases = (List) a[0]; - Map phaseMap = phases.stream().collect(Collectors.toMap(Phase::getName, Function.identity())); - return new LifecyclePolicy(TimeseriesLifecycleType.INSTANCE, name, phaseMap, (Map) a[1]); - }); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "lifecycle_policy", + false, + (a, name) -> { + List phases = (List) a[0]; + Map phaseMap = phases.stream().collect(Collectors.toMap(Phase::getName, Function.identity())); + return new LifecyclePolicy(TimeseriesLifecycleType.INSTANCE, name, phaseMap, (Map) a[1]); + } + ); static { - PARSER.declareNamedObjects(ConstructingObjectParser.constructorArg(), (p, c, n) -> Phase.parse(p, n), v -> { - throw new IllegalArgumentException("ordered " + PHASES_FIELD.getPreferredName() + " are not supported"); - }, PHASES_FIELD); + PARSER.declareNamedObjects( + ConstructingObjectParser.constructorArg(), + (p, c, n) -> Phase.parse(p, n), + v -> { throw new IllegalArgumentException("ordered " + PHASES_FIELD.getPreferredName() + " are not supported"); }, + PHASES_FIELD + ); PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> p.map(), METADATA); } @@ -168,11 +173,11 @@ public Map getMetadata() { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); - builder.startObject(PHASES_FIELD.getPreferredName()); - for (Phase phase : phases.values()) { - builder.field(phase.getName(), phase); - } - builder.endObject(); + builder.startObject(PHASES_FIELD.getPreferredName()); + for (Phase phase : phases.values()) { + builder.field(phase.getName(), phase); + } + builder.endObject(); if (this.metadata != null) { builder.field(METADATA.getPreferredName(), this.metadata); } @@ -269,8 +274,9 @@ public boolean isActionSafe(StepKey stepKey) { if (action != null) { return action.isSafeAction(); } else { - throw new IllegalArgumentException("Action [" + stepKey.getAction() + "] in phase [" + stepKey.getPhase() - + "] does not exist in policy [" + name + "]"); + throw new IllegalArgumentException( + "Action [" + stepKey.getAction() + "] in phase [" + stepKey.getPhase() + "] does not exist in policy [" + name + "]" + ); } } else { throw new IllegalArgumentException("Phase [" + stepKey.getPhase() + "] does not exist in policy [" + name + "]"); @@ -296,8 +302,9 @@ public static void validatePolicyName(String policy) { int byteCount = 0; byteCount = policy.getBytes(StandardCharsets.UTF_8).length; if (byteCount > MAX_INDEX_NAME_BYTES) { - throw new IllegalArgumentException("invalid policy name [" + policy + "]: name is too long, (" + byteCount + " > " + - MAX_INDEX_NAME_BYTES + ")"); + throw new IllegalArgumentException( + "invalid policy name [" + policy + "]: name is too long, (" + byteCount + " > " + MAX_INDEX_NAME_BYTES + ")" + ); } } @@ -315,9 +322,7 @@ public boolean equals(Object obj) { return false; } LifecyclePolicy other = (LifecyclePolicy) obj; - return Objects.equals(name, other.name) && - Objects.equals(phases, other.phases) && - Objects.equals(metadata, other.metadata); + return Objects.equals(name, other.name) && Objects.equals(phases, other.phases) && Objects.equals(metadata, other.metadata); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/LifecyclePolicyMetadata.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/LifecyclePolicyMetadata.java index ba57b30c3dd8e..99c418fa386ed 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/LifecyclePolicyMetadata.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/LifecyclePolicyMetadata.java @@ -9,11 +9,11 @@ import org.elasticsearch.cluster.AbstractDiffable; import org.elasticsearch.cluster.Diffable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -26,7 +26,9 @@ import java.util.Objects; public class LifecyclePolicyMetadata extends AbstractDiffable - implements ToXContentObject, Diffable { + implements + ToXContentObject, + Diffable { static final ParseField POLICY = new ParseField("policy"); static final ParseField HEADERS = new ParseField("headers"); @@ -35,11 +37,13 @@ public class LifecyclePolicyMetadata extends AbstractDiffable PARSER = new ConstructingObjectParser<>("policy_metadata", - a -> { - LifecyclePolicy policy = (LifecyclePolicy) a[0]; - return new LifecyclePolicyMetadata(policy, (Map) a[1], (long) a[2], (long) a[3]); - }); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "policy_metadata", + a -> { + LifecyclePolicy policy = (LifecyclePolicy) a[0]; + return new LifecyclePolicyMetadata(policy, (Map) a[1], (long) a[2], (long) a[3]); + } + ); static { PARSER.declareObject(ConstructingObjectParser.constructorArg(), LifecyclePolicy::parse, POLICY); PARSER.declareField(ConstructingObjectParser.constructorArg(), XContentParser::mapStrings, HEADERS, ValueType.OBJECT); @@ -131,10 +135,10 @@ public boolean equals(Object obj) { return false; } LifecyclePolicyMetadata other = (LifecyclePolicyMetadata) obj; - return Objects.equals(policy, other.policy) && - Objects.equals(headers, other.headers) && - Objects.equals(version, other.version) && - Objects.equals(modifiedDate, other.modifiedDate); + return Objects.equals(policy, other.policy) + && Objects.equals(headers, other.headers) + && Objects.equals(version, other.version) + && Objects.equals(modifiedDate, other.modifiedDate); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/LifecyclePolicyUtils.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/LifecyclePolicyUtils.java index e867288874802..fe6cb5f6f8e9c 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/LifecyclePolicyUtils.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/LifecyclePolicyUtils.java @@ -18,11 +18,11 @@ import org.elasticsearch.common.compress.NotXContentException; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.core.internal.io.Streams; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.core.internal.io.Streams; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -45,8 +45,10 @@ public static LifecyclePolicy loadPolicy(String name, String resource, NamedXCon BytesReference source = load(resource); validate(source); - try (XContentParser parser = XContentType.JSON.xContent() - .createParser(xContentRegistry, LoggingDeprecationHandler.THROW_UNSUPPORTED_OPERATION, source.utf8ToString())) { + try ( + XContentParser parser = XContentType.JSON.xContent() + .createParser(xContentRegistry, LoggingDeprecationHandler.THROW_UNSUPPORTED_OPERATION, source.utf8ToString()) + ) { LifecyclePolicy policy = LifecyclePolicy.parse(parser, name); policy.validate(); return policy; @@ -89,34 +91,38 @@ private static void validate(BytesReference source) { * Given a cluster state and ILM policy, calculate the {@link ItemUsage} of * the policy (what indices, data streams, and templates use the policy) */ - public static ItemUsage calculateUsage(final IndexNameExpressionResolver indexNameExpressionResolver, - final ClusterState state, final String policyName) { - final List indices = state.metadata().indices().values().stream() + public static ItemUsage calculateUsage( + final IndexNameExpressionResolver indexNameExpressionResolver, + final ClusterState state, + final String policyName + ) { + final List indices = state.metadata() + .indices() + .values() + .stream() .filter(indexMetadata -> policyName.equals(LifecycleSettings.LIFECYCLE_NAME_SETTING.get(indexMetadata.getSettings()))) .map(indexMetadata -> indexMetadata.getIndex().getName()) .collect(Collectors.toList()); - final List allDataStreams = indexNameExpressionResolver.dataStreamNames(state, - IndicesOptions.LENIENT_EXPAND_OPEN_CLOSED_HIDDEN); + final List allDataStreams = indexNameExpressionResolver.dataStreamNames( + state, + IndicesOptions.LENIENT_EXPAND_OPEN_CLOSED_HIDDEN + ); - final List dataStreams = allDataStreams.stream() - .filter(dsName -> { - String indexTemplate = MetadataIndexTemplateService.findV2Template(state.metadata(), dsName, false); - if (indexTemplate != null) { - Settings settings = MetadataIndexTemplateService.resolveSettings(state.metadata(), indexTemplate); - return policyName.equals(LifecycleSettings.LIFECYCLE_NAME_SETTING.get(settings)); - } else { - return false; - } - }) - .collect(Collectors.toList()); - - final List composableTemplates = state.metadata().templatesV2().keySet().stream() - .filter(templateName -> { - Settings settings = MetadataIndexTemplateService.resolveSettings(state.metadata(), templateName); + final List dataStreams = allDataStreams.stream().filter(dsName -> { + String indexTemplate = MetadataIndexTemplateService.findV2Template(state.metadata(), dsName, false); + if (indexTemplate != null) { + Settings settings = MetadataIndexTemplateService.resolveSettings(state.metadata(), indexTemplate); return policyName.equals(LifecycleSettings.LIFECYCLE_NAME_SETTING.get(settings)); - }) - .collect(Collectors.toList()); + } else { + return false; + } + }).collect(Collectors.toList()); + + final List composableTemplates = state.metadata().templatesV2().keySet().stream().filter(templateName -> { + Settings settings = MetadataIndexTemplateService.resolveSettings(state.metadata(), templateName); + return policyName.equals(LifecycleSettings.LIFECYCLE_NAME_SETTING.get(settings)); + }).collect(Collectors.toList()); return new ItemUsage(indices, dataStreams, composableTemplates); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/LifecycleSettings.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/LifecycleSettings.java index 1678bc833e943..f2a78f382b5f9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/LifecycleSettings.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/LifecycleSettings.java @@ -35,48 +35,96 @@ public class LifecycleSettings { // already mounted as a searchable snapshot. Those ILM actions will check if the index has this setting name configured. public static final String SNAPSHOT_INDEX_NAME = "index.store.snapshot.index_name"; - public static final Setting LIFECYCLE_POLL_INTERVAL_SETTING = timeSetting(LIFECYCLE_POLL_INTERVAL, - TimeValue.timeValueMinutes(10), TimeValue.timeValueSeconds(1), Setting.Property.Dynamic, Setting.Property.NodeScope); - public static final Setting LIFECYCLE_NAME_SETTING = Setting.simpleString(LIFECYCLE_NAME, - Setting.Property.Dynamic, Setting.Property.IndexScope); - public static final Setting LIFECYCLE_INDEXING_COMPLETE_SETTING = Setting.boolSetting(LIFECYCLE_INDEXING_COMPLETE, false, - Setting.Property.Dynamic, Setting.Property.IndexScope); - public static final Setting LIFECYCLE_ORIGINATION_DATE_SETTING = - Setting.longSetting(LIFECYCLE_ORIGINATION_DATE, -1, -1, Setting.Property.Dynamic, Setting.Property.IndexScope); - public static final Setting LIFECYCLE_PARSE_ORIGINATION_DATE_SETTING = Setting.boolSetting(LIFECYCLE_PARSE_ORIGINATION_DATE, - false, Setting.Property.Dynamic, Setting.Property.IndexScope); - public static final Setting LIFECYCLE_HISTORY_INDEX_ENABLED_SETTING = Setting.boolSetting(LIFECYCLE_HISTORY_INDEX_ENABLED, - true, Setting.Property.NodeScope); - public static final Setting LIFECYCLE_STEP_MASTER_TIMEOUT_SETTING = - Setting.positiveTimeSetting(LIFECYCLE_STEP_MASTER_TIMEOUT, TimeValue.timeValueSeconds(30), Setting.Property.Dynamic, - Setting.Property.NodeScope, Setting.Property.Deprecated); + public static final Setting LIFECYCLE_POLL_INTERVAL_SETTING = timeSetting( + LIFECYCLE_POLL_INTERVAL, + TimeValue.timeValueMinutes(10), + TimeValue.timeValueSeconds(1), + Setting.Property.Dynamic, + Setting.Property.NodeScope + ); + public static final Setting LIFECYCLE_NAME_SETTING = Setting.simpleString( + LIFECYCLE_NAME, + Setting.Property.Dynamic, + Setting.Property.IndexScope + ); + public static final Setting LIFECYCLE_INDEXING_COMPLETE_SETTING = Setting.boolSetting( + LIFECYCLE_INDEXING_COMPLETE, + false, + Setting.Property.Dynamic, + Setting.Property.IndexScope + ); + public static final Setting LIFECYCLE_ORIGINATION_DATE_SETTING = Setting.longSetting( + LIFECYCLE_ORIGINATION_DATE, + -1, + -1, + Setting.Property.Dynamic, + Setting.Property.IndexScope + ); + public static final Setting LIFECYCLE_PARSE_ORIGINATION_DATE_SETTING = Setting.boolSetting( + LIFECYCLE_PARSE_ORIGINATION_DATE, + false, + Setting.Property.Dynamic, + Setting.Property.IndexScope + ); + public static final Setting LIFECYCLE_HISTORY_INDEX_ENABLED_SETTING = Setting.boolSetting( + LIFECYCLE_HISTORY_INDEX_ENABLED, + true, + Setting.Property.NodeScope + ); + public static final Setting LIFECYCLE_STEP_MASTER_TIMEOUT_SETTING = Setting.positiveTimeSetting( + LIFECYCLE_STEP_MASTER_TIMEOUT, + TimeValue.timeValueSeconds(30), + Setting.Property.Dynamic, + Setting.Property.NodeScope, + Setting.Property.Deprecated + ); // This setting configures how much time since step_time should ILM wait for a condition to be met. After the threshold wait time has // elapsed ILM will likely stop waiting and go to the next step. // Also see {@link org.elasticsearch.xpack.core.ilm.ClusterStateWaitUntilThresholdStep} - public static final Setting LIFECYCLE_STEP_WAIT_TIME_THRESHOLD_SETTING = - timeSetting(LIFECYCLE_STEP_WAIT_TIME_THRESHOLD, TimeValue.timeValueHours(12), TimeValue.timeValueHours(1), Setting.Property.Dynamic, - Setting.Property.IndexScope); + public static final Setting LIFECYCLE_STEP_WAIT_TIME_THRESHOLD_SETTING = timeSetting( + LIFECYCLE_STEP_WAIT_TIME_THRESHOLD, + TimeValue.timeValueHours(12), + TimeValue.timeValueHours(1), + Setting.Property.Dynamic, + Setting.Property.IndexScope + ); - - public static final Setting SLM_HISTORY_INDEX_ENABLED_SETTING = Setting.boolSetting(SLM_HISTORY_INDEX_ENABLED, true, - Setting.Property.NodeScope); - public static final Setting SLM_RETENTION_SCHEDULE_SETTING = Setting.simpleString(SLM_RETENTION_SCHEDULE, + public static final Setting SLM_HISTORY_INDEX_ENABLED_SETTING = Setting.boolSetting( + SLM_HISTORY_INDEX_ENABLED, + true, + Setting.Property.NodeScope + ); + public static final Setting SLM_RETENTION_SCHEDULE_SETTING = Setting.simpleString( + SLM_RETENTION_SCHEDULE, // Default to 1:30am every day "0 30 1 * * ?", str -> { - try { - if (Strings.hasText(str)) { - // Test that the setting is a valid cron syntax - new CronSchedule(str); + try { + if (Strings.hasText(str)) { + // Test that the setting is a valid cron syntax + new CronSchedule(str); + } + } catch (Exception e) { + throw new IllegalArgumentException( + "invalid cron expression [" + str + "] for SLM retention schedule [" + SLM_RETENTION_SCHEDULE + "]", + e + ); } - } catch (Exception e) { - throw new IllegalArgumentException("invalid cron expression [" + str + "] for SLM retention schedule [" + - SLM_RETENTION_SCHEDULE + "]", e); - } - }, Setting.Property.Dynamic, Setting.Property.NodeScope); - public static final Setting SLM_RETENTION_DURATION_SETTING = timeSetting(SLM_RETENTION_DURATION, - TimeValue.timeValueHours(1), TimeValue.timeValueMillis(500), Setting.Property.Dynamic, Setting.Property.NodeScope); - public static final Setting SLM_MINIMUM_INTERVAL_SETTING = - Setting.positiveTimeSetting(SLM_MINIMUM_INTERVAL, TimeValue.timeValueMinutes(15), Setting.Property.Dynamic, - Setting.Property.NodeScope); + }, + Setting.Property.Dynamic, + Setting.Property.NodeScope + ); + public static final Setting SLM_RETENTION_DURATION_SETTING = timeSetting( + SLM_RETENTION_DURATION, + TimeValue.timeValueHours(1), + TimeValue.timeValueMillis(500), + Setting.Property.Dynamic, + Setting.Property.NodeScope + ); + public static final Setting SLM_MINIMUM_INTERVAL_SETTING = Setting.positiveTimeSetting( + SLM_MINIMUM_INTERVAL, + TimeValue.timeValueMinutes(15), + Setting.Property.Dynamic, + Setting.Property.NodeScope + ); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/LifecycleType.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/LifecycleType.java index 623d4f6d7140c..1a2867faa7618 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/LifecycleType.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/LifecycleType.java @@ -61,7 +61,6 @@ public interface LifecycleType extends NamedWriteable { */ String getNextActionName(String currentActionName, Phase phase); - /** * validates whether the specified phases are valid for this * policy instance. diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/MigrateAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/MigrateAction.java index 041feca58a8e2..2c5637fe2afad 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/MigrateAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/MigrateAction.java @@ -9,16 +9,16 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.client.Client; +import org.elasticsearch.cluster.routing.allocation.DataTier; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.snapshots.SearchableSnapshotsSettings; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.snapshots.SearchableSnapshotsSettings; -import org.elasticsearch.cluster.routing.allocation.DataTier; import org.elasticsearch.xpack.core.ilm.Step.StepKey; import java.io.IOException; @@ -38,8 +38,10 @@ public class MigrateAction implements LifecycleAction { private static final Logger logger = LogManager.getLogger(MigrateAction.class); public static final String CONDITIONAL_SKIP_MIGRATE_STEP = BranchingStep.NAME + "-check-skip-action"; - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(NAME, - a -> new MigrateAction(a[0] == null ? true : (boolean) a[0])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + a -> new MigrateAction(a[0] == null ? true : (boolean) a[0]) + ); static { PARSER.declareBoolean(ConstructingObjectParser.optionalConstructorArg(), ENABLED_FIELD); @@ -100,22 +102,35 @@ public List toSteps(Client client, String phase, StepKey nextStepKey) { String targetTier = "data_" + phase; assert DataTier.validTierName(targetTier) : "invalid data tier name:" + targetTier; - BranchingStep conditionalSkipActionStep = new BranchingStep(preMigrateBranchingKey, migrationKey, nextStepKey, + BranchingStep conditionalSkipActionStep = new BranchingStep( + preMigrateBranchingKey, + migrationKey, + nextStepKey, (index, clusterState) -> { Settings indexSettings = clusterState.metadata().index(index).getSettings(); // partially mounted indices will already have data_frozen, and we don't want to change that if they do if (SearchableSnapshotsSettings.isPartialSearchableSnapshotIndex(indexSettings)) { String policyName = LifecycleSettings.LIFECYCLE_NAME_SETTING.get(indexSettings); - logger.debug("[{}] action in policy [{}] is configured for index [{}] which is a partially mounted index. " + - "skipping this action", MigrateAction.NAME, policyName, index.getName()); + logger.debug( + "[{}] action in policy [{}] is configured for index [{}] which is a partially mounted index. " + + "skipping this action", + MigrateAction.NAME, + policyName, + index.getName() + ); return true; } return false; - }); - UpdateSettingsStep updateMigrationSettingStep = new UpdateSettingsStep(migrationKey, migrationRoutedKey, client, - getPreferredTiersConfigurationSettings(targetTier)); + } + ); + UpdateSettingsStep updateMigrationSettingStep = new UpdateSettingsStep( + migrationKey, + migrationRoutedKey, + client, + getPreferredTiersConfigurationSettings(targetTier) + ); DataTierMigrationRoutedStep migrationRoutedStep = new DataTierMigrationRoutedStep(migrationRoutedKey, nextStepKey); return List.of(conditionalSkipActionStep, updateMigrationSettingStep, migrationRoutedStep); } else { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/MountSnapshotStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/MountSnapshotStep.java index 3649798cd3e9d..0cca1701373ec 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/MountSnapshotStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/MountSnapshotStep.java @@ -13,11 +13,11 @@ import org.elasticsearch.client.Client; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.IndexMetadata; +import org.elasticsearch.cluster.routing.allocation.DataTier; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.TimeValue; import org.elasticsearch.rest.RestStatus; -import org.elasticsearch.cluster.routing.allocation.DataTier; import org.elasticsearch.xpack.core.searchablesnapshots.MountSearchableSnapshotAction; import org.elasticsearch.xpack.core.searchablesnapshots.MountSearchableSnapshotRequest; @@ -38,8 +38,13 @@ public class MountSnapshotStep extends AsyncRetryDuringSnapshotActionStep { private final String restoredIndexPrefix; private final MountSearchableSnapshotRequest.Storage storageType; - public MountSnapshotStep(StepKey key, StepKey nextStepKey, Client client, String restoredIndexPrefix, - MountSearchableSnapshotRequest.Storage storageType) { + public MountSnapshotStep( + StepKey key, + StepKey nextStepKey, + Client client, + String restoredIndexPrefix, + MountSearchableSnapshotRequest.Storage storageType + ) { super(key, nextStepKey, client); this.restoredIndexPrefix = restoredIndexPrefix; this.storageType = Objects.requireNonNull(storageType, "a storage type must be specified"); @@ -67,22 +72,30 @@ void performDuringNoSnapshot(IndexMetadata indexMetadata, ClusterState currentCl String policyName = indexMetadata.getSettings().get(LifecycleSettings.LIFECYCLE_NAME); final String snapshotRepository = lifecycleState.getSnapshotRepository(); if (Strings.hasText(snapshotRepository) == false) { - listener.onFailure(new IllegalStateException("snapshot repository is not present for policy [" + policyName + "] and index [" + - indexName + "]")); + listener.onFailure( + new IllegalStateException( + "snapshot repository is not present for policy [" + policyName + "] and index [" + indexName + "]" + ) + ); return; } final String snapshotName = lifecycleState.getSnapshotName(); if (Strings.hasText(snapshotName) == false) { listener.onFailure( - new IllegalStateException("snapshot name was not generated for policy [" + policyName + "] and index [" + indexName + "]")); + new IllegalStateException("snapshot name was not generated for policy [" + policyName + "] and index [" + indexName + "]") + ); return; } String mountedIndexName = restoredIndexPrefix + indexName; if (currentClusterState.metadata().index(mountedIndexName) != null) { - logger.debug("mounted index [{}] for policy [{}] and index [{}] already exists. will not attempt to mount the index again", - mountedIndexName, policyName, indexName); + logger.debug( + "mounted index [{}] for policy [{}] and index [{}] already exists. will not attempt to mount the index again", + mountedIndexName, + policyName, + indexName + ); listener.onResponse(null); return; } @@ -92,43 +105,55 @@ void performDuringNoSnapshot(IndexMetadata indexMetadata, ClusterState currentCl // This index had its searchable snapshot created prior to a version where we captured // the original index name, so make our best guess at the name indexName = bestEffortIndexNameResolution(indexName); - logger.debug("index [{}] using policy [{}] does not have a stored snapshot index name, " + - "using our best effort guess of [{}] for the original snapshotted index name", - indexMetadata.getIndex().getName(), policyName, indexName); + logger.debug( + "index [{}] using policy [{}] does not have a stored snapshot index name, " + + "using our best effort guess of [{}] for the original snapshotted index name", + indexMetadata.getIndex().getName(), + policyName, + indexName + ); } else { // Use the name of the snapshot as specified in the metadata, because the current index // name not might not reflect the name of the index actually in the snapshot - logger.debug("index [{}] using policy [{}] has a different name [{}] within the snapshot to be restored, " + - "using the snapshot index name from generated metadata for mounting", indexName, policyName, snapshotIndexName); + logger.debug( + "index [{}] using policy [{}] has a different name [{}] within the snapshot to be restored, " + + "using the snapshot index name from generated metadata for mounting", + indexName, + policyName, + snapshotIndexName + ); indexName = snapshotIndexName; } final Settings.Builder settingsBuilder = Settings.builder(); - overrideTierPreference(this.getKey().getPhase()) - .ifPresent(override -> settingsBuilder.put(DataTier.TIER_PREFERENCE, override)); + overrideTierPreference(this.getKey().getPhase()).ifPresent(override -> settingsBuilder.put(DataTier.TIER_PREFERENCE, override)); - final MountSearchableSnapshotRequest mountSearchableSnapshotRequest = new MountSearchableSnapshotRequest(mountedIndexName, - snapshotRepository, snapshotName, indexName, settingsBuilder.build(), + final MountSearchableSnapshotRequest mountSearchableSnapshotRequest = new MountSearchableSnapshotRequest( + mountedIndexName, + snapshotRepository, + snapshotName, + indexName, + settingsBuilder.build(), // we captured the index metadata when we took the snapshot. the index likely had the ILM execution state in the metadata. // if we were to restore the lifecycle.name setting, the restored index would be captured by the ILM runner and, // depending on what ILM execution state was captured at snapshot time, make it's way forward from _that_ step forward in // the ILM policy. // we'll re-set this setting on the restored index at a later step once we restored a deterministic execution state - new String[]{LifecycleSettings.LIFECYCLE_NAME}, + new String[] { LifecycleSettings.LIFECYCLE_NAME }, // we'll not wait for the snapshot to complete in this step as the async steps are executed from threads that shouldn't // perform expensive operations (ie. clusterStateProcessed) false, - storageType); + storageType + ); mountSearchableSnapshotRequest.masterNodeTimeout(TimeValue.MAX_VALUE); - getClient().execute(MountSearchableSnapshotAction.INSTANCE, mountSearchableSnapshotRequest, - ActionListener.wrap(response -> { - if (response.status() != RestStatus.OK && response.status() != RestStatus.ACCEPTED) { - logger.debug("mount snapshot response failed to complete"); - throw new ElasticsearchException("mount snapshot response failed to complete, got response " + response.status()); - } - listener.onResponse(null); - }, listener::onFailure)); + getClient().execute(MountSearchableSnapshotAction.INSTANCE, mountSearchableSnapshotRequest, ActionListener.wrap(response -> { + if (response.status() != RestStatus.OK && response.status() != RestStatus.ACCEPTED) { + logger.debug("mount snapshot response failed to complete"); + throw new ElasticsearchException("mount snapshot response failed to complete, got response " + response.status()); + } + listener.onResponse(null); + }, listener::onFailure)); } /** @@ -170,8 +195,8 @@ public boolean equals(Object obj) { return false; } MountSnapshotStep other = (MountSnapshotStep) obj; - return super.equals(obj) && - Objects.equals(restoredIndexPrefix, other.restoredIndexPrefix) && - Objects.equals(storageType, other.storageType); + return super.equals(obj) + && Objects.equals(restoredIndexPrefix, other.restoredIndexPrefix) + && Objects.equals(storageType, other.storageType); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/OpenIndexStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/OpenIndexStep.java index dc93f019b00f5..49686ceb08077 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/OpenIndexStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/OpenIndexStep.java @@ -29,18 +29,20 @@ final class OpenIndexStep extends AsyncActionStep { } @Override - public void performAction(IndexMetadata indexMetadata, ClusterState currentClusterState, - ClusterStateObserver observer, ActionListener listener) { + public void performAction( + IndexMetadata indexMetadata, + ClusterState currentClusterState, + ClusterStateObserver observer, + ActionListener listener + ) { if (indexMetadata.getState() == IndexMetadata.State.CLOSE) { OpenIndexRequest request = new OpenIndexRequest(indexMetadata.getIndex().getName()).masterNodeTimeout(TimeValue.MAX_VALUE); - getClient().admin().indices() - .open(request, - ActionListener.wrap(openIndexResponse -> { - if (openIndexResponse.isAcknowledged() == false) { - throw new ElasticsearchException("open index request failed to be acknowledged"); - } - listener.onResponse(null); - }, listener::onFailure)); + getClient().admin().indices().open(request, ActionListener.wrap(openIndexResponse -> { + if (openIndexResponse.isAcknowledged() == false) { + throw new ElasticsearchException("open index request failed to be acknowledged"); + } + listener.onResponse(null); + }, listener::onFailure)); } else { listener.onResponse(null); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/PauseFollowerIndexStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/PauseFollowerIndexStep.java index feeaac7e8df7d..44c897a3374e0 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/PauseFollowerIndexStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/PauseFollowerIndexStep.java @@ -39,7 +39,8 @@ void innerPerformAction(String followerIndex, ClusterState currentClusterState, return; } - List> shardFollowTasks = persistentTasksMetadata.tasks().stream() + List> shardFollowTasks = persistentTasksMetadata.tasks() + .stream() .filter(persistentTask -> ShardFollowTask.NAME.equals(persistentTask.getTaskName())) .filter(persistentTask -> { ShardFollowTask shardFollowTask = (ShardFollowTask) persistentTask.getParams(); @@ -54,14 +55,11 @@ void innerPerformAction(String followerIndex, ClusterState currentClusterState, PauseFollowAction.Request request = new PauseFollowAction.Request(followerIndex); request.masterNodeTimeout(TimeValue.MAX_VALUE); - getClient().execute(PauseFollowAction.INSTANCE, request, ActionListener.wrap( - r -> { - if (r.isAcknowledged() == false) { - throw new ElasticsearchException("pause follow request failed to be acknowledged"); - } - listener.onResponse(null); - }, - listener::onFailure - )); + getClient().execute(PauseFollowAction.INSTANCE, request, ActionListener.wrap(r -> { + if (r.isAcknowledged() == false) { + throw new ElasticsearchException("pause follow request failed to be acknowledged"); + } + listener.onResponse(null); + }, listener::onFailure)); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/Phase.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/Phase.java index dace3f8d0c13a..315415acd059f 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/Phase.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/Phase.java @@ -9,7 +9,6 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.Version; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; @@ -18,6 +17,7 @@ import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ContextParser; import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -40,37 +40,36 @@ public class Phase implements ToXContentObject, Writeable { public static final ParseField ACTIONS_FIELD = new ParseField("actions"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("phase", false, - (a, name) -> { - final List lifecycleActions = (List) a[1]; - Map map = new HashMap<>(lifecycleActions.size()); - for (LifecycleAction lifecycleAction : lifecycleActions) { - if (map.put(lifecycleAction.getWriteableName(), lifecycleAction) != null) { - throw new IllegalStateException("Duplicate key"); - } - } - return new Phase(name, (TimeValue) a[0], map); - }); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("phase", false, (a, name) -> { + final List lifecycleActions = (List) a[1]; + Map map = new HashMap<>(lifecycleActions.size()); + for (LifecycleAction lifecycleAction : lifecycleActions) { + if (map.put(lifecycleAction.getWriteableName(), lifecycleAction) != null) { + throw new IllegalStateException("Duplicate key"); + } + } + return new Phase(name, (TimeValue) a[0], map); + }); static { - PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(), - (ContextParser) (p, c) -> { - // In earlier versions it was possible to create a Phase with a negative `min_age` which would then cause errors - // when the phase is read from the cluster state during startup (even before negative timevalues were strictly - // disallowed) so this is a hack to treat negative `min_age`s as 0 to prevent those errors. - // They will be saved as `0` so this hack can be removed once we no longer have to read cluster states from 7.x. - assert Version.CURRENT.major < 9 : "remove this hack now that we don't have to read 7.x cluster states"; - final String timeValueString = p.text(); - if (timeValueString.startsWith("-")) { - logger.warn("phase has negative min_age value of [{}] - this will be treated as a min_age of 0", - timeValueString); - return TimeValue.ZERO; - } - return TimeValue.parseTimeValue(timeValueString, MIN_AGE.getPreferredName()); - }, MIN_AGE, ValueType.VALUE); - PARSER.declareNamedObjects(ConstructingObjectParser.constructorArg(), - (p, c, n) -> p.namedObject(LifecycleAction.class, n, null), v -> { - throw new IllegalArgumentException("ordered " + ACTIONS_FIELD.getPreferredName() + " are not supported"); - }, ACTIONS_FIELD); + PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(), (ContextParser) (p, c) -> { + // In earlier versions it was possible to create a Phase with a negative `min_age` which would then cause errors + // when the phase is read from the cluster state during startup (even before negative timevalues were strictly + // disallowed) so this is a hack to treat negative `min_age`s as 0 to prevent those errors. + // They will be saved as `0` so this hack can be removed once we no longer have to read cluster states from 7.x. + assert Version.CURRENT.major < 9 : "remove this hack now that we don't have to read 7.x cluster states"; + final String timeValueString = p.text(); + if (timeValueString.startsWith("-")) { + logger.warn("phase has negative min_age value of [{}] - this will be treated as a min_age of 0", timeValueString); + return TimeValue.ZERO; + } + return TimeValue.parseTimeValue(timeValueString, MIN_AGE.getPreferredName()); + }, MIN_AGE, ValueType.VALUE); + PARSER.declareNamedObjects( + ConstructingObjectParser.constructorArg(), + (p, c, n) -> p.namedObject(LifecycleAction.class, n, null), + v -> { throw new IllegalArgumentException("ordered " + ACTIONS_FIELD.getPreferredName() + " are not supported"); }, + ACTIONS_FIELD + ); } public static Phase parse(XContentParser parser, String name) { @@ -174,9 +173,7 @@ public boolean equals(Object obj) { return false; } Phase other = (Phase) obj; - return Objects.equals(name, other.name) && - Objects.equals(minimumAge, other.minimumAge) && - Objects.equals(actions, other.actions); + return Objects.equals(name, other.name) && Objects.equals(minimumAge, other.minimumAge) && Objects.equals(actions, other.actions); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/PhaseCacheManagement.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/PhaseCacheManagement.java index 984311aa23d00..db9daf282456a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/PhaseCacheManagement.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/PhaseCacheManagement.java @@ -15,12 +15,12 @@ import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.common.Strings; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.license.XPackLicenseState; import java.util.ArrayList; import java.util.LinkedHashSet; @@ -40,14 +40,16 @@ public final class PhaseCacheManagement { private static final Logger logger = LogManager.getLogger(PhaseCacheManagement.class); - private PhaseCacheManagement() { - } + private PhaseCacheManagement() {} /** * Rereads the phase JSON for the given index, returning a new cluster state. */ - public static ClusterState refreshPhaseDefinition(final ClusterState state, final String index, - final LifecyclePolicyMetadata updatedPolicy) { + public static ClusterState refreshPhaseDefinition( + final ClusterState state, + final String index, + final LifecyclePolicyMetadata updatedPolicy + ) { final IndexMetadata idxMeta = state.metadata().index(index); Metadata.Builder metadataBuilder = Metadata.builder(state.metadata()); refreshPhaseDefinition(metadataBuilder, idxMeta, updatedPolicy); @@ -57,8 +59,11 @@ public static ClusterState refreshPhaseDefinition(final ClusterState state, fina /** * Rereads the phase JSON for the given index, and updates the provided metadata. */ - public static void refreshPhaseDefinition(final Metadata.Builder metadataBuilder, final IndexMetadata idxMeta, - final LifecyclePolicyMetadata updatedPolicy) { + public static void refreshPhaseDefinition( + final Metadata.Builder metadataBuilder, + final IndexMetadata idxMeta, + final LifecyclePolicyMetadata updatedPolicy + ) { String index = idxMeta.getIndex().getName(); assert eligibleToCheckForRefresh(idxMeta) : "index " + index + " is missing crucial information needed to refresh phase definition"; @@ -66,18 +71,20 @@ public static void refreshPhaseDefinition(final Metadata.Builder metadataBuilder LifecycleExecutionState currentExState = LifecycleExecutionState.fromIndexMetadata(idxMeta); String currentPhase = currentExState.getPhase(); - PhaseExecutionInfo pei = new PhaseExecutionInfo(updatedPolicy.getName(), - updatedPolicy.getPolicy().getPhases().get(currentPhase), updatedPolicy.getVersion(), updatedPolicy.getModifiedDate()); + PhaseExecutionInfo pei = new PhaseExecutionInfo( + updatedPolicy.getName(), + updatedPolicy.getPolicy().getPhases().get(currentPhase), + updatedPolicy.getVersion(), + updatedPolicy.getModifiedDate() + ); LifecycleExecutionState newExState = LifecycleExecutionState.builder(currentExState) .setPhaseDefinition(Strings.toString(pei, false, false)) .build(); - metadataBuilder.put(IndexMetadata.builder(idxMeta) - .putCustom(ILM_CUSTOM_METADATA_KEY, newExState.asMap())); + metadataBuilder.put(IndexMetadata.builder(idxMeta).putCustom(ILM_CUSTOM_METADATA_KEY, newExState.asMap())); } - /** * Ensure that we have the minimum amount of metadata necessary to check for cache phase * refresh. This includes: @@ -104,9 +111,14 @@ public static boolean eligibleToCheckForRefresh(final IndexMetadata metadata) { /** * For the given new policy, returns a new cluster with all updateable indices' phase JSON refreshed. */ - public static ClusterState updateIndicesForPolicy(final ClusterState state, final NamedXContentRegistry xContentRegistry, - final Client client, final LifecyclePolicy oldPolicy, - final LifecyclePolicyMetadata newPolicy, XPackLicenseState licenseState) { + public static ClusterState updateIndicesForPolicy( + final ClusterState state, + final NamedXContentRegistry xContentRegistry, + final Client client, + final LifecyclePolicy oldPolicy, + final LifecyclePolicyMetadata newPolicy, + XPackLicenseState licenseState + ) { Metadata.Builder mb = Metadata.builder(state.metadata()); if (updateIndicesForPolicy(mb, state, xContentRegistry, client, oldPolicy, newPolicy, licenseState)) { return ClusterState.builder(state).metadata(mb.build(false)).build(); @@ -119,12 +131,17 @@ public static ClusterState updateIndicesForPolicy(final ClusterState state, fina * Returns true if any indices were updated and false otherwise. * Users of this API should consider the returned value and only create a new {@link ClusterState} if `true` is returned. */ - public static boolean updateIndicesForPolicy(final Metadata.Builder mb, final ClusterState currentState, - final NamedXContentRegistry xContentRegistry, final Client client, - final LifecyclePolicy oldPolicy, final LifecyclePolicyMetadata newPolicy, - final XPackLicenseState licenseState) { - assert oldPolicy.getName().equals(newPolicy.getName()) : "expected both policies to have the same id but they were: [" + - oldPolicy.getName() + "] vs. [" + newPolicy.getName() + "]"; + public static boolean updateIndicesForPolicy( + final Metadata.Builder mb, + final ClusterState currentState, + final NamedXContentRegistry xContentRegistry, + final Client client, + final LifecyclePolicy oldPolicy, + final LifecyclePolicyMetadata newPolicy, + final XPackLicenseState licenseState + ) { + assert oldPolicy.getName().equals(newPolicy.getName()) + : "expected both policies to have the same id but they were: [" + oldPolicy.getName() + "] vs. [" + newPolicy.getName() + "]"; // No need to update anything if the policies are identical in contents if (oldPolicy.equals(newPolicy.getPolicy())) { @@ -132,11 +149,13 @@ public static boolean updateIndicesForPolicy(final Metadata.Builder mb, final Cl return false; } - final List indicesThatCanBeUpdated = - currentState.metadata().indices().values().stream() - .filter(meta -> newPolicy.getName().equals(LifecycleSettings.LIFECYCLE_NAME_SETTING.get(meta.getSettings()))) - .filter(meta -> isIndexPhaseDefinitionUpdatable(xContentRegistry, client, meta, newPolicy.getPolicy(), licenseState)) - .collect(Collectors.toList()); + final List indicesThatCanBeUpdated = currentState.metadata() + .indices() + .values() + .stream() + .filter(meta -> newPolicy.getName().equals(LifecycleSettings.LIFECYCLE_NAME_SETTING.get(meta.getSettings()))) + .filter(meta -> isIndexPhaseDefinitionUpdatable(xContentRegistry, client, meta, newPolicy.getPolicy(), licenseState)) + .collect(Collectors.toList()); final List refreshedIndices = new ArrayList<>(indicesThatCanBeUpdated.size()); for (IndexMetadata index : indicesThatCanBeUpdated) { @@ -144,8 +163,10 @@ public static boolean updateIndicesForPolicy(final Metadata.Builder mb, final Cl refreshPhaseDefinition(mb, index, newPolicy); refreshedIndices.add(index.getIndex().getName()); } catch (Exception e) { - logger.warn(new ParameterizedMessage("[{}] unable to refresh phase definition for updated policy [{}]", - index, newPolicy.getName()), e); + logger.warn( + new ParameterizedMessage("[{}] unable to refresh phase definition for updated policy [{}]", index, newPolicy.getName()), + e + ); } } logger.debug("refreshed policy [{}] phase definition for [{}] indices", newPolicy.getName(), refreshedIndices.size()); @@ -155,9 +176,13 @@ public static boolean updateIndicesForPolicy(final Metadata.Builder mb, final Cl /** * Returns 'true' if the index's cached phase JSON can be safely reread, 'false' otherwise. */ - public static boolean isIndexPhaseDefinitionUpdatable(final NamedXContentRegistry xContentRegistry, final Client client, - final IndexMetadata metadata, final LifecyclePolicy newPolicy, - final XPackLicenseState licenseState) { + public static boolean isIndexPhaseDefinitionUpdatable( + final NamedXContentRegistry xContentRegistry, + final Client client, + final IndexMetadata metadata, + final LifecyclePolicy newPolicy, + final XPackLicenseState licenseState + ) { final String index = metadata.getIndex().getName(); if (eligibleToCheckForRefresh(metadata) == false) { logger.debug("[{}] does not contain enough information to check for eligibility of refreshing phase", index); @@ -169,23 +194,31 @@ public static boolean isIndexPhaseDefinitionUpdatable(final NamedXContentRegistr final Step.StepKey currentStepKey = LifecycleExecutionState.getCurrentStepKey(executionState); final String currentPhase = currentStepKey.getPhase(); - final Set newStepKeys = newPolicy.toSteps(client, licenseState).stream() + final Set newStepKeys = newPolicy.toSteps(client, licenseState) + .stream() .map(Step::getKey) .collect(Collectors.toCollection(LinkedHashSet::new)); if (newStepKeys.contains(currentStepKey) == false) { // The index is on a step that doesn't exist in the new policy, we // can't safely re-read the JSON - logger.debug("[{}] updated policy [{}] does not contain the current step key [{}], so the policy phase will not be refreshed", - index, policyId, currentStepKey); + logger.debug( + "[{}] updated policy [{}] does not contain the current step key [{}], so the policy phase will not be refreshed", + index, + policyId, + currentStepKey + ); return false; } final String phaseDef = executionState.getPhaseDefinition(); final Set oldStepKeys = readStepKeys(xContentRegistry, client, phaseDef, currentPhase, licenseState); if (oldStepKeys == null) { - logger.debug("[{}] unable to parse phase definition for cached policy [{}], policy phase will not be refreshed", - index, policyId); + logger.debug( + "[{}] unable to parse phase definition for cached policy [{}], policy phase will not be refreshed", + index, + policyId + ); return false; } @@ -198,8 +231,13 @@ public static boolean isIndexPhaseDefinitionUpdatable(final NamedXContentRegistr final Set newPhaseStepKeys = readStepKeys(xContentRegistry, client, peiJson, currentPhase, licenseState); if (newPhaseStepKeys == null) { - logger.debug(new ParameterizedMessage("[{}] unable to parse phase definition for policy [{}] " + - "to determine if it could be refreshed", index, policyId)); + logger.debug( + new ParameterizedMessage( + "[{}] unable to parse phase definition for policy [{}] " + "to determine if it could be refreshed", + index, + policyId + ) + ); return false; } @@ -209,9 +247,14 @@ public static boolean isIndexPhaseDefinitionUpdatable(final NamedXContentRegistr logger.debug("[{}] updated policy [{}] contains the same phase step keys and can be refreshed", index, policyId); return true; } else { - logger.debug("[{}] updated policy [{}] has different phase step keys and will NOT refresh phase " + - "definition as it differs too greatly. old: {}, new: {}", - index, policyId, oldPhaseStepKeys, newPhaseStepKeys); + logger.debug( + "[{}] updated policy [{}] has different phase step keys and will NOT refresh phase " + + "definition as it differs too greatly. old: {}, new: {}", + index, + policyId, + oldPhaseStepKeys, + newPhaseStepKeys + ); return false; } } @@ -222,19 +265,31 @@ public static boolean isIndexPhaseDefinitionUpdatable(final NamedXContentRegistr * information, returns null. */ @Nullable - public static Set readStepKeys(final NamedXContentRegistry xContentRegistry, final Client client, - final String phaseDef, final String currentPhase, final XPackLicenseState licenseState) { + public static Set readStepKeys( + final NamedXContentRegistry xContentRegistry, + final Client client, + final String phaseDef, + final String currentPhase, + final XPackLicenseState licenseState + ) { if (phaseDef == null) { return null; } final PhaseExecutionInfo phaseExecutionInfo; - try (XContentParser parser = JsonXContent.jsonXContent.createParser(xContentRegistry, - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, phaseDef)) { + try ( + XContentParser parser = JsonXContent.jsonXContent.createParser( + xContentRegistry, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + phaseDef + ) + ) { phaseExecutionInfo = PhaseExecutionInfo.parse(parser, currentPhase); } catch (Exception e) { - logger.trace(new ParameterizedMessage("exception reading step keys checking for refreshability, phase definition: {}", - phaseDef), e); + logger.trace( + new ParameterizedMessage("exception reading step keys checking for refreshability, phase definition: {}", phaseDef), + e + ); return null; } @@ -242,7 +297,10 @@ public static Set readStepKeys(final NamedXContentRegistry xConten return null; } - return phaseExecutionInfo.getPhase().getActions().values().stream() + return phaseExecutionInfo.getPhase() + .getActions() + .values() + .stream() .flatMap(a -> a.toSteps(client, phaseExecutionInfo.getPhase().getName(), null, licenseState).stream()) .map(Step::getKey) .collect(Collectors.toCollection(LinkedHashSet::new)); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/PhaseExecutionInfo.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/PhaseExecutionInfo.java index e96913d6fc513..78ff08d5ced5b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/PhaseExecutionInfo.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/PhaseExecutionInfo.java @@ -6,13 +6,13 @@ */ package org.elasticsearch.xpack.core.ilm; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -31,8 +31,10 @@ public class PhaseExecutionInfo implements ToXContentObject, Writeable { private static final ParseField MODIFIED_DATE_IN_MILLIS_FIELD = new ParseField("modified_date_in_millis"); private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "phase_execution_info", false, - (a, name) -> new PhaseExecutionInfo((String) a[0], (Phase) a[1], (long) a[2], (long) a[3])); + "phase_execution_info", + false, + (a, name) -> new PhaseExecutionInfo((String) a[0], (Phase) a[1], (long) a[2], (long) a[3]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), POLICY_NAME_FIELD); PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), Phase::parse, PHASE_DEFINITION_FIELD); @@ -109,10 +111,10 @@ public boolean equals(Object obj) { return false; } PhaseExecutionInfo other = (PhaseExecutionInfo) obj; - return Objects.equals(policyName, other.policyName) && - Objects.equals(phase, other.phase) && - Objects.equals(version, other.version) && - Objects.equals(modifiedDate, other.modifiedDate); + return Objects.equals(policyName, other.policyName) + && Objects.equals(phase, other.phase) + && Objects.equals(version, other.version) + && Objects.equals(modifiedDate, other.modifiedDate); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ReadOnlyAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ReadOnlyAction.java index 2f63fb3605fde..37385b387094d 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ReadOnlyAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ReadOnlyAction.java @@ -31,11 +31,9 @@ public static ReadOnlyAction parse(XContentParser parser) { return PARSER.apply(parser, null); } - public ReadOnlyAction() { - } + public ReadOnlyAction() {} - public ReadOnlyAction(StreamInput in) { - } + public ReadOnlyAction(StreamInput in) {} @Override public String getWriteableName() { @@ -50,8 +48,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws } @Override - public void writeTo(StreamOutput out) throws IOException { - } + public void writeTo(StreamOutput out) throws IOException {} @Override public boolean isSafeAction() { @@ -62,8 +59,7 @@ public boolean isSafeAction() { public List toSteps(Client client, String phase, StepKey nextStepKey) { StepKey checkNotWriteIndex = new StepKey(phase, NAME, CheckNotDataStreamWriteIndexStep.NAME); StepKey readOnlyKey = new StepKey(phase, NAME, NAME); - CheckNotDataStreamWriteIndexStep checkNotWriteIndexStep = new CheckNotDataStreamWriteIndexStep(checkNotWriteIndex, - readOnlyKey); + CheckNotDataStreamWriteIndexStep checkNotWriteIndexStep = new CheckNotDataStreamWriteIndexStep(checkNotWriteIndex, readOnlyKey); ReadOnlyStep readOnlyStep = new ReadOnlyStep(readOnlyKey, nextStepKey, client); return Arrays.asList(checkNotWriteIndexStep, readOnlyStep); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ReadOnlyStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ReadOnlyStep.java index f82a74a828dd0..a87a25f88837b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ReadOnlyStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ReadOnlyStep.java @@ -29,16 +29,24 @@ public ReadOnlyStep(StepKey key, StepKey nextStepKey, Client client) { } @Override - public void performAction(IndexMetadata indexMetadata, ClusterState currentState, - ClusterStateObserver observer, ActionListener listener) { - getClient().admin().indices().execute(AddIndexBlockAction.INSTANCE, - new AddIndexBlockRequest(WRITE, indexMetadata.getIndex().getName()).masterNodeTimeout(TimeValue.MAX_VALUE), - ActionListener.wrap(response -> { - if (response.isAcknowledged() == false) { - throw new ElasticsearchException("read only add block index request failed to be acknowledged"); - } - listener.onResponse(null); - }, listener::onFailure)); + public void performAction( + IndexMetadata indexMetadata, + ClusterState currentState, + ClusterStateObserver observer, + ActionListener listener + ) { + getClient().admin() + .indices() + .execute( + AddIndexBlockAction.INSTANCE, + new AddIndexBlockRequest(WRITE, indexMetadata.getIndex().getName()).masterNodeTimeout(TimeValue.MAX_VALUE), + ActionListener.wrap(response -> { + if (response.isAcknowledged() == false) { + throw new ElasticsearchException("read only add block index request failed to be acknowledged"); + } + listener.onResponse(null); + }, listener::onFailure) + ); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ReplaceDataStreamBackingIndexStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ReplaceDataStreamBackingIndexStep.java index 30bc2d8cf053e..bef3f65a56369 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ReplaceDataStreamBackingIndexStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ReplaceDataStreamBackingIndexStep.java @@ -41,8 +41,11 @@ public class ReplaceDataStreamBackingIndexStep extends ClusterStateActionStep { private final BiFunction targetIndexNameSupplier; - public ReplaceDataStreamBackingIndexStep(StepKey key, StepKey nextStepKey, - BiFunction targetIndexNameSupplier) { + public ReplaceDataStreamBackingIndexStep( + StepKey key, + StepKey nextStepKey, + BiFunction targetIndexNameSupplier + ) { super(key, nextStepKey); this.targetIndexNameSupplier = targetIndexNameSupplier; } @@ -72,25 +75,41 @@ public ClusterState performAction(Index index, ClusterState clusterState) { assert indexAbstraction != null : "invalid cluster metadata. index [" + index.getName() + "] was not found"; IndexAbstraction.DataStream dataStream = indexAbstraction.getParentDataStream(); if (dataStream == null) { - String errorMessage = String.format(Locale.ROOT, "index [%s] is not part of a data stream. stopping execution of lifecycle " + - "[%s] until the index is added to a data stream", originalIndex, policyName); + String errorMessage = String.format( + Locale.ROOT, + "index [%s] is not part of a data stream. stopping execution of lifecycle " + + "[%s] until the index is added to a data stream", + originalIndex, + policyName + ); logger.debug(errorMessage); throw new IllegalStateException(errorMessage); } assert dataStream.getWriteIndex() != null : dataStream.getName() + " has no write index"; if (dataStream.getWriteIndex().equals(index)) { - String errorMessage = String.format(Locale.ROOT, "index [%s] is the write index for data stream [%s], pausing " + - "ILM execution of lifecycle [%s] until this index is no longer the write index for the data stream via manual or " + - "automated rollover", originalIndex, dataStream.getName(), policyName); + String errorMessage = String.format( + Locale.ROOT, + "index [%s] is the write index for data stream [%s], pausing " + + "ILM execution of lifecycle [%s] until this index is no longer the write index for the data stream via manual or " + + "automated rollover", + originalIndex, + dataStream.getName(), + policyName + ); logger.debug(errorMessage); throw new IllegalStateException(errorMessage); } IndexMetadata targetIndexMetadata = clusterState.metadata().index(targetIndexName); if (targetIndexMetadata == null) { - String errorMessage = String.format(Locale.ROOT, "target index [%s] doesn't exist. stopping execution of lifecycle [%s] for" + - " index [%s]", targetIndexName, policyName, originalIndex); + String errorMessage = String.format( + Locale.ROOT, + "target index [%s] doesn't exist. stopping execution of lifecycle [%s] for" + " index [%s]", + targetIndexName, + policyName, + originalIndex + ); logger.debug(errorMessage); throw new IllegalStateException(errorMessage); } @@ -114,7 +133,6 @@ public boolean equals(Object obj) { return false; } ReplaceDataStreamBackingIndexStep other = (ReplaceDataStreamBackingIndexStep) obj; - return super.equals(obj) && - Objects.equals(targetIndexNameSupplier, other.targetIndexNameSupplier); + return super.equals(obj) && Objects.equals(targetIndexNameSupplier, other.targetIndexNameSupplier); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/RolloverAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/RolloverAction.java index 038aec6477938..acb980f44a869 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/RolloverAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/RolloverAction.java @@ -7,17 +7,17 @@ package org.elasticsearch.xpack.core.ilm; import org.elasticsearch.client.Client; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ilm.Step.StepKey; @@ -38,24 +38,38 @@ public class RolloverAction implements LifecycleAction { public static final ParseField MAX_DOCS_FIELD = new ParseField("max_docs"); public static final ParseField MAX_AGE_FIELD = new ParseField("max_age"); public static final String LIFECYCLE_ROLLOVER_ALIAS = "index.lifecycle.rollover_alias"; - public static final Setting LIFECYCLE_ROLLOVER_ALIAS_SETTING = Setting.simpleString(LIFECYCLE_ROLLOVER_ALIAS, - Setting.Property.Dynamic, Setting.Property.IndexScope); + public static final Setting LIFECYCLE_ROLLOVER_ALIAS_SETTING = Setting.simpleString( + LIFECYCLE_ROLLOVER_ALIAS, + Setting.Property.Dynamic, + Setting.Property.IndexScope + ); private static final Settings INDEXING_COMPLETE = Settings.builder().put(LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE, true).build(); - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(NAME, - a -> new RolloverAction((ByteSizeValue) a[0], (ByteSizeValue) a[1], (TimeValue) a[2], (Long) a[3])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + a -> new RolloverAction((ByteSizeValue) a[0], (ByteSizeValue) a[1], (TimeValue) a[2], (Long) a[3]) + ); static { - PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(), + PARSER.declareField( + ConstructingObjectParser.optionalConstructorArg(), (p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), MAX_SIZE_FIELD.getPreferredName()), - MAX_SIZE_FIELD, ValueType.VALUE); - PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(), + MAX_SIZE_FIELD, + ValueType.VALUE + ); + PARSER.declareField( + ConstructingObjectParser.optionalConstructorArg(), (p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), MAX_PRIMARY_SHARD_SIZE_FIELD.getPreferredName()), - MAX_PRIMARY_SHARD_SIZE_FIELD, ValueType.VALUE); - PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(), + MAX_PRIMARY_SHARD_SIZE_FIELD, + ValueType.VALUE + ); + PARSER.declareField( + ConstructingObjectParser.optionalConstructorArg(), (p, c) -> TimeValue.parseTimeValue(p.text(), MAX_AGE_FIELD.getPreferredName()), - MAX_AGE_FIELD, ValueType.VALUE); + MAX_AGE_FIELD, + ValueType.VALUE + ); PARSER.declareLong(ConstructingObjectParser.optionalConstructorArg(), MAX_DOCS_FIELD); } @@ -68,8 +82,12 @@ public static RolloverAction parse(XContentParser parser) { return PARSER.apply(parser, null); } - public RolloverAction(@Nullable ByteSizeValue maxSize, @Nullable ByteSizeValue maxPrimaryShardSize, @Nullable TimeValue maxAge, - @Nullable Long maxDocs) { + public RolloverAction( + @Nullable ByteSizeValue maxSize, + @Nullable ByteSizeValue maxPrimaryShardSize, + @Nullable TimeValue maxAge, + @Nullable Long maxDocs + ) { if (maxSize == null && maxPrimaryShardSize == null && maxAge == null && maxDocs == null) { throw new IllegalArgumentException("At least one rollover condition must be set."); } @@ -163,14 +181,28 @@ public List toSteps(Client client, String phase, Step.StepKey nextStepKey) StepKey updateDateStepKey = new StepKey(phase, NAME, UpdateRolloverLifecycleDateStep.NAME); StepKey setIndexingCompleteStepKey = new StepKey(phase, NAME, INDEXING_COMPLETE_STEP_NAME); - WaitForRolloverReadyStep waitForRolloverReadyStep = new WaitForRolloverReadyStep(waitForRolloverReadyStepKey, rolloverStepKey, - client, maxSize, maxPrimaryShardSize, maxAge, maxDocs); + WaitForRolloverReadyStep waitForRolloverReadyStep = new WaitForRolloverReadyStep( + waitForRolloverReadyStepKey, + rolloverStepKey, + client, + maxSize, + maxPrimaryShardSize, + maxAge, + maxDocs + ); RolloverStep rolloverStep = new RolloverStep(rolloverStepKey, waitForActiveShardsKey, client); WaitForActiveShardsStep waitForActiveShardsStep = new WaitForActiveShardsStep(waitForActiveShardsKey, updateDateStepKey); - UpdateRolloverLifecycleDateStep updateDateStep = new UpdateRolloverLifecycleDateStep(updateDateStepKey, setIndexingCompleteStepKey, - System::currentTimeMillis); - UpdateSettingsStep setIndexingCompleteStep = new UpdateSettingsStep(setIndexingCompleteStepKey, nextStepKey, - client, INDEXING_COMPLETE); + UpdateRolloverLifecycleDateStep updateDateStep = new UpdateRolloverLifecycleDateStep( + updateDateStepKey, + setIndexingCompleteStepKey, + System::currentTimeMillis + ); + UpdateSettingsStep setIndexingCompleteStep = new UpdateSettingsStep( + setIndexingCompleteStepKey, + nextStepKey, + client, + INDEXING_COMPLETE + ); return Arrays.asList(waitForRolloverReadyStep, rolloverStep, waitForActiveShardsStep, updateDateStep, setIndexingCompleteStep); } @@ -188,10 +220,10 @@ public boolean equals(Object obj) { return false; } RolloverAction other = (RolloverAction) obj; - return Objects.equals(maxSize, other.maxSize) && - Objects.equals(maxPrimaryShardSize, other.maxPrimaryShardSize) && - Objects.equals(maxAge, other.maxAge) && - Objects.equals(maxDocs, other.maxDocs); + return Objects.equals(maxSize, other.maxSize) + && Objects.equals(maxPrimaryShardSize, other.maxPrimaryShardSize) + && Objects.equals(maxAge, other.maxAge) + && Objects.equals(maxDocs, other.maxDocs); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/RolloverStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/RolloverStep.java index 85f984385ad8c..4f4486b2a21c7 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/RolloverStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/RolloverStep.java @@ -40,8 +40,12 @@ public boolean isRetryable() { } @Override - public void performAction(IndexMetadata indexMetadata, ClusterState currentClusterState, - ClusterStateObserver observer, ActionListener listener) { + public void performAction( + IndexMetadata indexMetadata, + ClusterState currentClusterState, + ClusterStateObserver observer, + ActionListener listener + ) { String indexName = indexMetadata.getIndex().getName(); boolean indexingComplete = LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE_SETTING.get(indexMetadata.getSettings()); if (indexingComplete) { @@ -56,8 +60,12 @@ public void performAction(IndexMetadata indexMetadata, ClusterState currentClust if (dataStream != null) { assert dataStream.getWriteIndex() != null : "datastream " + dataStream.getName() + " has no write index"; if (dataStream.getWriteIndex().equals(indexMetadata.getIndex()) == false) { - logger.warn("index [{}] is not the write index for data stream [{}]. skipping rollover for policy [{}]", - indexName, dataStream.getName(), LifecycleSettings.LIFECYCLE_NAME_SETTING.get(indexMetadata.getSettings())); + logger.warn( + "index [{}] is not the write index for data stream [{}]. skipping rollover for policy [{}]", + indexName, + dataStream.getName(), + LifecycleSettings.LIFECYCLE_NAME_SETTING.get(indexMetadata.getSettings()) + ); listener.onResponse(null); return; } @@ -66,23 +74,42 @@ public void performAction(IndexMetadata indexMetadata, ClusterState currentClust String rolloverAlias = RolloverAction.LIFECYCLE_ROLLOVER_ALIAS_SETTING.get(indexMetadata.getSettings()); if (Strings.isNullOrEmpty(rolloverAlias)) { - listener.onFailure(new IllegalArgumentException(String.format(Locale.ROOT, - "setting [%s] for index [%s] is empty or not defined, it must be set to the name of the alias " + - "pointing to the group of indices being rolled over", RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, indexName))); + listener.onFailure( + new IllegalArgumentException( + String.format( + Locale.ROOT, + "setting [%s] for index [%s] is empty or not defined, it must be set to the name of the alias " + + "pointing to the group of indices being rolled over", + RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, + indexName + ) + ) + ); return; } if (indexMetadata.getRolloverInfos().get(rolloverAlias) != null) { - logger.info("index [{}] was already rolled over for alias [{}], not attempting to roll over again", - indexName, rolloverAlias); + logger.info( + "index [{}] was already rolled over for alias [{}], not attempting to roll over again", + indexName, + rolloverAlias + ); listener.onResponse(null); return; } if (indexMetadata.getAliases().containsKey(rolloverAlias) == false) { - listener.onFailure(new IllegalArgumentException(String.format(Locale.ROOT, - "%s [%s] does not point to index [%s]", RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, rolloverAlias, - indexName))); + listener.onFailure( + new IllegalArgumentException( + String.format( + Locale.ROOT, + "%s [%s] does not point to index [%s]", + RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, + rolloverAlias, + indexName + ) + ) + ); return; } @@ -94,18 +121,16 @@ public void performAction(IndexMetadata indexMetadata, ClusterState currentClust // We don't wait for active shards when we perform the rollover because the // {@link org.elasticsearch.xpack.core.ilm.WaitForActiveShardsStep} step will do so rolloverRequest.setWaitForActiveShards(ActiveShardCount.NONE); - getClient().admin().indices().rolloverIndex(rolloverRequest, - ActionListener.wrap(response -> { - assert response.isRolledOver() : "the only way this rollover call should fail is with an exception"; - if (response.isRolledOver()) { - listener.onResponse(null); - } else { - listener.onFailure(new IllegalStateException("unexepected exception on unconditional rollover")); - } - }, listener::onFailure)); + getClient().admin().indices().rolloverIndex(rolloverRequest, ActionListener.wrap(response -> { + assert response.isRolledOver() : "the only way this rollover call should fail is with an exception"; + if (response.isRolledOver()) { + listener.onResponse(null); + } else { + listener.onFailure(new IllegalStateException("unexepected exception on unconditional rollover")); + } + }, listener::onFailure)); } - @Override public int hashCode() { return Objects.hash(super.hashCode()); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/RollupILMAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/RollupILMAction.java index db1cc7350d51c..dbe7f758ab76e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/RollupILMAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/RollupILMAction.java @@ -7,13 +7,13 @@ package org.elasticsearch.xpack.core.ilm; import org.elasticsearch.client.Client; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ilm.Step.StepKey; @@ -33,8 +33,10 @@ public class RollupILMAction implements LifecycleAction { private static final ParseField POLICY_FIELD = new ParseField("rollup_policy"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(NAME, - a -> new RollupILMAction((RollupActionConfig) a[0], (String) a[1])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + a -> new RollupILMAction((RollupActionConfig) a[0], (String) a[1]) + ); public static final String ROLLUP_INDEX_PREFIX = "rollup-"; public static final String GENERATE_ROLLUP_STEP_NAME = "generate-rollup-name"; @@ -42,8 +44,12 @@ public class RollupILMAction implements LifecycleAction { private final String rollupPolicy; static { - PARSER.declareField(ConstructingObjectParser.constructorArg(), - (p, c) -> RollupActionConfig.fromXContent(p), CONFIG_FIELD, ObjectParser.ValueType.OBJECT); + PARSER.declareField( + ConstructingObjectParser.constructorArg(), + (p, c) -> RollupActionConfig.fromXContent(p), + CONFIG_FIELD, + ObjectParser.ValueType.OBJECT + ); PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), POLICY_FIELD); } @@ -101,19 +107,26 @@ public List toSteps(Client client, String phase, StepKey nextStepKey) { StepKey readOnlyKey = new StepKey(phase, NAME, ReadOnlyStep.NAME); StepKey generateRollupIndexNameKey = new StepKey(phase, NAME, GENERATE_ROLLUP_STEP_NAME); StepKey rollupKey = new StepKey(phase, NAME, NAME); - CheckNotDataStreamWriteIndexStep checkNotWriteIndexStep = new CheckNotDataStreamWriteIndexStep(checkNotWriteIndex, - readOnlyKey); + CheckNotDataStreamWriteIndexStep checkNotWriteIndexStep = new CheckNotDataStreamWriteIndexStep(checkNotWriteIndex, readOnlyKey); ReadOnlyStep readOnlyStep = new ReadOnlyStep(readOnlyKey, generateRollupIndexNameKey, client); - GenerateUniqueIndexNameStep generateRollupIndexNameStep = new GenerateUniqueIndexNameStep(generateRollupIndexNameKey, rollupKey, - ROLLUP_INDEX_PREFIX, (rollupIndexName, lifecycleStateBuilder) -> lifecycleStateBuilder.setRollupIndexName(rollupIndexName)); + GenerateUniqueIndexNameStep generateRollupIndexNameStep = new GenerateUniqueIndexNameStep( + generateRollupIndexNameKey, + rollupKey, + ROLLUP_INDEX_PREFIX, + (rollupIndexName, lifecycleStateBuilder) -> lifecycleStateBuilder.setRollupIndexName(rollupIndexName) + ); if (rollupPolicy == null) { Step rollupStep = new RollupStep(rollupKey, nextStepKey, client, config); return List.of(checkNotWriteIndexStep, readOnlyStep, generateRollupIndexNameStep, rollupStep); } else { StepKey updateRollupIndexPolicyStepKey = new StepKey(phase, NAME, UpdateRollupIndexPolicyStep.NAME); Step rollupStep = new RollupStep(rollupKey, updateRollupIndexPolicyStepKey, client, config); - Step updateRollupIndexPolicyStep = new UpdateRollupIndexPolicyStep(updateRollupIndexPolicyStepKey, nextStepKey, - client, rollupPolicy); + Step updateRollupIndexPolicyStep = new UpdateRollupIndexPolicyStep( + updateRollupIndexPolicyStepKey, + nextStepKey, + client, + rollupPolicy + ); return List.of(checkNotWriteIndexStep, readOnlyStep, generateRollupIndexNameStep, rollupStep, updateRollupIndexPolicyStep); } } @@ -125,8 +138,7 @@ public boolean equals(Object o) { RollupILMAction that = (RollupILMAction) o; - return Objects.equals(this.config, that.config) - && Objects.equals(this.rollupPolicy, that.rollupPolicy); + return Objects.equals(this.config, that.config) && Objects.equals(this.rollupPolicy, that.rollupPolicy); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/RollupStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/RollupStep.java index 11da6e15b1ceb..5ce5594bf2fdf 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/RollupStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/RollupStep.java @@ -40,21 +40,31 @@ public boolean isRetryable() { } @Override - public void performAction(IndexMetadata indexMetadata, ClusterState currentState, - ClusterStateObserver observer, ActionListener listener) { + public void performAction( + IndexMetadata indexMetadata, + ClusterState currentState, + ClusterStateObserver observer, + ActionListener listener + ) { final String policyName = indexMetadata.getSettings().get(LifecycleSettings.LIFECYCLE_NAME); final String indexName = indexMetadata.getIndex().getName(); final LifecycleExecutionState lifecycleState = fromIndexMetadata(indexMetadata); final String rollupIndexName = lifecycleState.getRollupIndexName(); if (Strings.hasText(rollupIndexName) == false) { - listener.onFailure(new IllegalStateException("rollup index name was not generated for policy [" + policyName + - "] and index [" + indexName + "]")); + listener.onFailure( + new IllegalStateException( + "rollup index name was not generated for policy [" + policyName + "] and index [" + indexName + "]" + ) + ); return; } RollupAction.Request request = new RollupAction.Request(indexName, rollupIndexName, config).masterNodeTimeout(TimeValue.MAX_VALUE); // currently RollupAction always acknowledges action was complete when no exceptions are thrown. - getClient().execute(RollupAction.INSTANCE, request, - ActionListener.wrap(response -> listener.onResponse(null), listener::onFailure)); + getClient().execute( + RollupAction.INSTANCE, + request, + ActionListener.wrap(response -> listener.onResponse(null), listener::onFailure) + ); } public RollupActionConfig getConfig() { @@ -75,7 +85,6 @@ public boolean equals(Object obj) { return false; } RollupStep other = (RollupStep) obj; - return super.equals(obj) - && Objects.equals(config, other.config); + return super.equals(obj) && Objects.equals(config, other.config); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/SearchableSnapshotAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/SearchableSnapshotAction.java index 2af137341218d..963821bd03809 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/SearchableSnapshotAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/SearchableSnapshotAction.java @@ -15,12 +15,12 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.license.LicenseUtils; +import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.license.LicenseUtils; -import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.xpack.core.ilm.Step.StepKey; import org.elasticsearch.xpack.core.searchablesnapshots.MountSearchableSnapshotRequest; @@ -52,15 +52,16 @@ public class SearchableSnapshotAction implements LifecycleAction { public static final String FULL_RESTORED_INDEX_PREFIX = "restored-"; public static final String PARTIAL_RESTORED_INDEX_PREFIX = "partial-"; - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(NAME, - a -> new SearchableSnapshotAction((String) a[0], a[1] == null || (boolean) a[1])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + a -> new SearchableSnapshotAction((String) a[0], a[1] == null || (boolean) a[1]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), SNAPSHOT_REPOSITORY); PARSER.declareBoolean(ConstructingObjectParser.optionalConstructorArg(), FORCE_MERGE_INDEX); } - public static SearchableSnapshotAction parse(XContentParser parser) { return PARSER.apply(parser, null); } @@ -124,7 +125,10 @@ public List toSteps(Client client, String phase, StepKey nextStepKey, XPac // a searchable snapshot of the same type and repository, in which case we don't need to do anything. If that is detected, // this branching step jumps right to the end, skipping the searchable snapshot action entirely. We also check the license // here before generating snapshots that can't be used if the user doesn't have the right license level. - BranchingStep conditionalSkipActionStep = new BranchingStep(preActionBranchingKey, checkNoWriteIndex, nextStepKey, + BranchingStep conditionalSkipActionStep = new BranchingStep( + preActionBranchingKey, + checkNoWriteIndex, + nextStepKey, (index, clusterState) -> { if (SEARCHABLE_SNAPSHOT_FEATURE.checkWithoutTracking(licenseState) == false) { logger.error("[{}] action is not available in the current license", SearchableSnapshotAction.NAME); @@ -139,54 +143,86 @@ public List toSteps(Client client, String phase, StepKey nextStepKey, XPac String repo = indexMetadata.getSettings().get(SEARCHABLE_SNAPSHOTS_REPOSITORY_NAME_SETTING_KEY); if (this.snapshotRepository.equals(repo) == false) { // Okay, different repo, we need to go ahead with the searchable snapshot - logger.debug("[{}] action is configured for index [{}] in policy [{}] which is already mounted as a searchable " + - "snapshot, but with a different repository (existing: [{}] vs new: [{}]), a new snapshot and " + - "index will be created", - SearchableSnapshotAction.NAME, index.getName(), policyName, repo, this.snapshotRepository); + logger.debug( + "[{}] action is configured for index [{}] in policy [{}] which is already mounted as a searchable " + + "snapshot, but with a different repository (existing: [{}] vs new: [{}]), a new snapshot and " + + "index will be created", + SearchableSnapshotAction.NAME, + index.getName(), + policyName, + repo, + this.snapshotRepository + ); return false; } // Check to the storage type to see if we need to convert between full <-> partial final boolean partial = indexMetadata.getSettings().getAsBoolean(SEARCHABLE_SNAPSHOT_PARTIAL_SETTING_KEY, false); - MountSearchableSnapshotRequest.Storage existingType = - partial ? MountSearchableSnapshotRequest.Storage.SHARED_CACHE : MountSearchableSnapshotRequest.Storage.FULL_COPY; + MountSearchableSnapshotRequest.Storage existingType = partial + ? MountSearchableSnapshotRequest.Storage.SHARED_CACHE + : MountSearchableSnapshotRequest.Storage.FULL_COPY; MountSearchableSnapshotRequest.Storage type = getConcreteStorageType(preActionBranchingKey); if (existingType == type) { - logger.debug("[{}] action is configured for index [{}] in policy [{}] which is already mounted " + - "as a searchable snapshot with the same repository [{}] and storage type [{}], skipping this action", - SearchableSnapshotAction.NAME, index.getName(), policyName, repo, type); + logger.debug( + "[{}] action is configured for index [{}] in policy [{}] which is already mounted " + + "as a searchable snapshot with the same repository [{}] and storage type [{}], skipping this action", + SearchableSnapshotAction.NAME, + index.getName(), + policyName, + repo, + type + ); return true; } - logger.debug("[{}] action is configured for index [{}] in policy [{}] which is already mounted " + - "as a searchable snapshot in repository [{}], however, the storage type ([{}] vs [{}]) " + - "differs, so a new index will be created", - SearchableSnapshotAction.NAME, index.getName(), policyName, this.snapshotRepository, existingType, type); + logger.debug( + "[{}] action is configured for index [{}] in policy [{}] which is already mounted " + + "as a searchable snapshot in repository [{}], however, the storage type ([{}] vs [{}]) " + + "differs, so a new index will be created", + SearchableSnapshotAction.NAME, + index.getName(), + policyName, + this.snapshotRepository, + existingType, + type + ); // Perform the searchable snapshot return false; } // Perform the searchable snapshot, as the index is not currently a searchable snapshot return false; - }); - CheckNotDataStreamWriteIndexStep checkNoWriteIndexStep = - new CheckNotDataStreamWriteIndexStep(checkNoWriteIndex, waitForNoFollowerStepKey); - WaitForNoFollowersStep waitForNoFollowersStep = - new WaitForNoFollowersStep(waitForNoFollowerStepKey, skipGeneratingSnapshotKey, client); + } + ); + CheckNotDataStreamWriteIndexStep checkNoWriteIndexStep = new CheckNotDataStreamWriteIndexStep( + checkNoWriteIndex, + waitForNoFollowerStepKey + ); + WaitForNoFollowersStep waitForNoFollowersStep = new WaitForNoFollowersStep( + waitForNoFollowerStepKey, + skipGeneratingSnapshotKey, + client + ); // When generating a snapshot, we either jump to the force merge step, or we skip the // forcemerge and go straight to steps for creating the snapshot StepKey keyForSnapshotGeneration = forceMergeIndex ? forceMergeStepKey : generateSnapshotNameKey; // Branch, deciding whether there is an existing searchable snapshot snapshot that can be used for mounting the index // (in which case, skip generating a new name and the snapshot cleanup), or if we need to generate a new snapshot - BranchingStep skipGeneratingSnapshotStep = - new BranchingStep(skipGeneratingSnapshotKey, keyForSnapshotGeneration, waitForDataTierKey, (index, clusterState) -> { + BranchingStep skipGeneratingSnapshotStep = new BranchingStep( + skipGeneratingSnapshotKey, + keyForSnapshotGeneration, + waitForDataTierKey, + (index, clusterState) -> { IndexMetadata indexMetadata = clusterState.getMetadata().index(index); String policyName = LifecycleSettings.LIFECYCLE_NAME_SETTING.get(indexMetadata.getSettings()); LifecycleExecutionState lifecycleExecutionState = LifecycleExecutionState.fromIndexMetadata(indexMetadata); if (lifecycleExecutionState.getSnapshotName() == null) { // No name exists, so it must be generated - logger.trace("no snapshot name for index [{}] in policy [{}] exists, so one will be generated", - index.getName(), policyName); + logger.trace( + "no snapshot name for index [{}] in policy [{}] exists, so one will be generated", + index.getName(), + policyName + ); return false; } @@ -198,50 +234,87 @@ public List toSteps(Client client, String phase, StepKey nextStepKey, XPac // We can skip the generate, initial cleanup, and snapshot taking for this index, as we already have a generated snapshot. // This will jump ahead directly to the "mount snapshot" step - logger.debug("an existing snapshot [{}] in repository [{}] (index name: [{}]) " + - "will be used for mounting [{}] as a searchable snapshot", - lifecycleExecutionState.getSnapshotName(), lifecycleExecutionState.getSnapshotRepository(), - lifecycleExecutionState.getSnapshotIndexName(), index.getName()); + logger.debug( + "an existing snapshot [{}] in repository [{}] (index name: [{}]) " + + "will be used for mounting [{}] as a searchable snapshot", + lifecycleExecutionState.getSnapshotName(), + lifecycleExecutionState.getSnapshotRepository(), + lifecycleExecutionState.getSnapshotIndexName(), + index.getName() + ); return true; - }); + } + ); // If a new snapshot is needed, these steps are executed ForceMergeStep forceMergeStep = new ForceMergeStep(forceMergeStepKey, waitForSegmentCountKey, client, 1); SegmentCountStep segmentCountStep = new SegmentCountStep(waitForSegmentCountKey, generateSnapshotNameKey, client, 1); - GenerateSnapshotNameStep generateSnapshotNameStep = new GenerateSnapshotNameStep(generateSnapshotNameKey, cleanSnapshotKey, - snapshotRepository); + GenerateSnapshotNameStep generateSnapshotNameStep = new GenerateSnapshotNameStep( + generateSnapshotNameKey, + cleanSnapshotKey, + snapshotRepository + ); CleanupSnapshotStep cleanupSnapshotStep = new CleanupSnapshotStep(cleanSnapshotKey, createSnapshotKey, client); - CreateSnapshotStep createSnapshotStep = new CreateSnapshotStep(createSnapshotKey, waitForDataTierKey, cleanSnapshotKey, - client); + CreateSnapshotStep createSnapshotStep = new CreateSnapshotStep(createSnapshotKey, waitForDataTierKey, cleanSnapshotKey, client); MountSearchableSnapshotRequest.Storage storageType = getConcreteStorageType(mountSnapshotKey); // If the skipGeneratingSnapshotStep determined a snapshot already existed that // can be used, it jumps directly here, skipping the snapshot generation steps above. - WaitForDataTierStep waitForDataTierStep = - new WaitForDataTierStep(waitForDataTierKey, mountSnapshotKey, - MountSnapshotStep.overrideTierPreference(phase).orElse(storageType.defaultDataTiersPreference())); - MountSnapshotStep mountSnapshotStep = new MountSnapshotStep(mountSnapshotKey, waitForGreenRestoredIndexKey, - client, getRestoredIndexPrefix(mountSnapshotKey), storageType); - WaitForIndexColorStep waitForGreenIndexHealthStep = new WaitForIndexColorStep(waitForGreenRestoredIndexKey, - copyMetadataKey, ClusterHealthStatus.GREEN, getRestoredIndexPrefix(waitForGreenRestoredIndexKey)); - CopyExecutionStateStep copyMetadataStep = new CopyExecutionStateStep(copyMetadataKey, copyLifecyclePolicySettingKey, - (index, executionState) -> getRestoredIndexPrefix(copyMetadataKey) + index, nextStepKey); - CopySettingsStep copySettingsStep = new CopySettingsStep(copyLifecyclePolicySettingKey, dataStreamCheckBranchingKey, - getRestoredIndexPrefix(copyLifecyclePolicySettingKey), LifecycleSettings.LIFECYCLE_NAME); - BranchingStep isDataStreamBranchingStep = new BranchingStep(dataStreamCheckBranchingKey, swapAliasesKey, replaceDataStreamIndexKey, + WaitForDataTierStep waitForDataTierStep = new WaitForDataTierStep( + waitForDataTierKey, + mountSnapshotKey, + MountSnapshotStep.overrideTierPreference(phase).orElse(storageType.defaultDataTiersPreference()) + ); + MountSnapshotStep mountSnapshotStep = new MountSnapshotStep( + mountSnapshotKey, + waitForGreenRestoredIndexKey, + client, + getRestoredIndexPrefix(mountSnapshotKey), + storageType + ); + WaitForIndexColorStep waitForGreenIndexHealthStep = new WaitForIndexColorStep( + waitForGreenRestoredIndexKey, + copyMetadataKey, + ClusterHealthStatus.GREEN, + getRestoredIndexPrefix(waitForGreenRestoredIndexKey) + ); + CopyExecutionStateStep copyMetadataStep = new CopyExecutionStateStep( + copyMetadataKey, + copyLifecyclePolicySettingKey, + (index, executionState) -> getRestoredIndexPrefix(copyMetadataKey) + index, + nextStepKey + ); + CopySettingsStep copySettingsStep = new CopySettingsStep( + copyLifecyclePolicySettingKey, + dataStreamCheckBranchingKey, + getRestoredIndexPrefix(copyLifecyclePolicySettingKey), + LifecycleSettings.LIFECYCLE_NAME + ); + BranchingStep isDataStreamBranchingStep = new BranchingStep( + dataStreamCheckBranchingKey, + swapAliasesKey, + replaceDataStreamIndexKey, (index, clusterState) -> { IndexAbstraction indexAbstraction = clusterState.metadata().getIndicesLookup().get(index.getName()); assert indexAbstraction != null : "invalid cluster metadata. index [" + index.getName() + "] was not found"; return indexAbstraction.getParentDataStream() != null; - }); - ReplaceDataStreamBackingIndexStep replaceDataStreamBackingIndex = new ReplaceDataStreamBackingIndexStep(replaceDataStreamIndexKey, - deleteIndexKey, (index, executionState) -> getRestoredIndexPrefix(replaceDataStreamIndexKey) + index); + } + ); + ReplaceDataStreamBackingIndexStep replaceDataStreamBackingIndex = new ReplaceDataStreamBackingIndexStep( + replaceDataStreamIndexKey, + deleteIndexKey, + (index, executionState) -> getRestoredIndexPrefix(replaceDataStreamIndexKey) + index + ); DeleteStep deleteSourceIndexStep = new DeleteStep(deleteIndexKey, null, client); // sending this step to null as the restored index (which will after this step essentially be the source index) was sent to the next // key after we restored the lifecycle execution state - SwapAliasesAndDeleteSourceIndexStep swapAliasesAndDeleteSourceIndexStep = new SwapAliasesAndDeleteSourceIndexStep(swapAliasesKey, - null, client, getRestoredIndexPrefix(swapAliasesKey)); + SwapAliasesAndDeleteSourceIndexStep swapAliasesAndDeleteSourceIndexStep = new SwapAliasesAndDeleteSourceIndexStep( + swapAliasesKey, + null, + client, + getRestoredIndexPrefix(swapAliasesKey) + ); List steps = new ArrayList<>(); steps.add(conditionalSkipActionStep); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/SegmentCountStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/SegmentCountStep.java index d749b5a22ec61..9c17341eff998 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/SegmentCountStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/SegmentCountStep.java @@ -16,13 +16,13 @@ import org.elasticsearch.client.Client; import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.cluster.routing.ShardRouting; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.index.Index; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.index.Index; import java.io.IOException; import java.util.Arrays; @@ -57,34 +57,44 @@ public int getMaxNumSegments() { @Override public void evaluateCondition(Metadata metadata, Index index, Listener listener, TimeValue masterTimeout) { - getClient().admin().indices().segments(new IndicesSegmentsRequest(index.getName()), - ActionListener.wrap(response -> { - IndexSegments idxSegments = response.getIndices().get(index.getName()); - if (idxSegments == null || (response.getShardFailures() != null && response.getShardFailures().length > 0)) { - final DefaultShardOperationFailedException[] failures = response.getShardFailures(); - logger.info("[{}] retrieval of segment counts after force merge did not succeed, " + - "there were {} shard failures. failures: {}", + getClient().admin().indices().segments(new IndicesSegmentsRequest(index.getName()), ActionListener.wrap(response -> { + IndexSegments idxSegments = response.getIndices().get(index.getName()); + if (idxSegments == null || (response.getShardFailures() != null && response.getShardFailures().length > 0)) { + final DefaultShardOperationFailedException[] failures = response.getShardFailures(); + logger.info( + "[{}] retrieval of segment counts after force merge did not succeed, " + "there were {} shard failures. failures: {}", + index.getName(), + response.getFailedShards(), + failures == null + ? "n/a" + : Strings.collectionToDelimitedString( + Arrays.stream(failures).map(Strings::toString).collect(Collectors.toList()), + "," + ) + ); + listener.onResponse(true, new Info(-1)); + } else { + List unmergedShards = idxSegments.getShards() + .values() + .stream() + .flatMap(iss -> Arrays.stream(iss.getShards())) + .filter(shardSegments -> shardSegments.getSegments().size() > maxNumSegments) + .collect(Collectors.toList()); + if (unmergedShards.size() > 0) { + Map unmergedShardCounts = unmergedShards.stream() + .collect(Collectors.toMap(ShardSegments::getShardRouting, ss -> ss.getSegments().size())); + logger.info( + "[{}] best effort force merge to [{}] segments did not succeed for {} shards: {}", index.getName(), - response.getFailedShards(), - failures == null ? "n/a" : Strings.collectionToDelimitedString(Arrays.stream(failures) - .map(Strings::toString) - .collect(Collectors.toList()), ",")); - listener.onResponse(true, new Info(-1)); - } else { - List unmergedShards = idxSegments.getShards().values().stream() - .flatMap(iss -> Arrays.stream(iss.getShards())) - .filter(shardSegments -> shardSegments.getSegments().size() > maxNumSegments) - .collect(Collectors.toList()); - if (unmergedShards.size() > 0) { - Map unmergedShardCounts = unmergedShards.stream() - .collect(Collectors.toMap(ShardSegments::getShardRouting, ss -> ss.getSegments().size())); - logger.info("[{}] best effort force merge to [{}] segments did not succeed for {} shards: {}", - index.getName(), maxNumSegments, unmergedShards.size(), unmergedShardCounts); - } - // Force merging is best effort, so always return true that the condition has been met. - listener.onResponse(true, new Info(unmergedShards.size())); + maxNumSegments, + unmergedShards.size(), + unmergedShardCounts + ); } - }, listener::onFailure)); + // Force merging is best effort, so always return true that the condition has been met. + listener.onResponse(true, new Info(unmergedShards.size())); + } + }, listener::onFailure)); } @Override @@ -101,8 +111,7 @@ public boolean equals(Object obj) { return false; } SegmentCountStep other = (SegmentCountStep) obj; - return super.equals(obj) - && Objects.equals(maxNumSegments, other.maxNumSegments); + return super.equals(obj) && Objects.equals(maxNumSegments, other.maxNumSegments); } public static class Info implements ToXContentObject { @@ -111,8 +120,10 @@ public static class Info implements ToXContentObject { static final ParseField SHARDS_TO_MERGE = new ParseField("shards_left_to_merge"); static final ParseField MESSAGE = new ParseField("message"); - static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("segment_count_step_info", - a -> new Info((long) a[0])); + static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "segment_count_step_info", + a -> new Info((long) a[0]) + ); static { PARSER.declareLong(ConstructingObjectParser.constructorArg(), SHARDS_TO_MERGE); PARSER.declareString((i, s) -> {}, MESSAGE); @@ -132,8 +143,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws if (numberShardsLeftToMerge == 0) { builder.field(MESSAGE.getPreferredName(), "all shards force merged successfully"); } else { - builder.field(MESSAGE.getPreferredName(), - "[" + numberShardsLeftToMerge + "] shards did not successfully force merge"); + builder.field(MESSAGE.getPreferredName(), "[" + numberShardsLeftToMerge + "] shards did not successfully force merge"); } builder.field(SHARDS_TO_MERGE.getPreferredName(), numberShardsLeftToMerge); builder.endObject(); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/SetPriorityAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/SetPriorityAction.java index e1ad51b4beb46..616e38a32147c 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/SetPriorityAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/SetPriorityAction.java @@ -8,14 +8,14 @@ import org.elasticsearch.client.Client; import org.elasticsearch.cluster.metadata.IndexMetadata; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ilm.Step.StepKey; @@ -32,19 +32,25 @@ public class SetPriorityAction implements LifecycleAction { public static final ParseField RECOVERY_PRIORITY_FIELD = new ParseField("priority"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(NAME, - a -> new SetPriorityAction((Integer) a[0])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + a -> new SetPriorityAction((Integer) a[0]) + ); - private static final Settings NULL_PRIORITY_SETTINGS = - Settings.builder().putNull(IndexMetadata.INDEX_PRIORITY_SETTING.getKey()).build(); + private static final Settings NULL_PRIORITY_SETTINGS = Settings.builder() + .putNull(IndexMetadata.INDEX_PRIORITY_SETTING.getKey()) + .build(); - //package private for testing + // package private for testing final Integer recoveryPriority; static { - PARSER.declareField(ConstructingObjectParser.constructorArg(), - (p) -> p.currentToken() == XContentParser.Token.VALUE_NULL ? null : p.intValue() - , RECOVERY_PRIORITY_FIELD, ObjectParser.ValueType.INT_OR_NULL); + PARSER.declareField( + ConstructingObjectParser.constructorArg(), + (p) -> p.currentToken() == XContentParser.Token.VALUE_NULL ? null : p.intValue(), + RECOVERY_PRIORITY_FIELD, + ObjectParser.ValueType.INT_OR_NULL + ); } public static SetPriorityAction parse(XContentParser parser) { @@ -92,8 +98,8 @@ public boolean isSafeAction() { @Override public List toSteps(Client client, String phase, StepKey nextStepKey) { StepKey key = new StepKey(phase, NAME, NAME); - Settings indexPriority = recoveryPriority == null ? - NULL_PRIORITY_SETTINGS + Settings indexPriority = recoveryPriority == null + ? NULL_PRIORITY_SETTINGS : Settings.builder().put(IndexMetadata.INDEX_PRIORITY_SETTING.getKey(), recoveryPriority).build(); return Collections.singletonList(new UpdateSettingsStep(key, nextStepKey, client, indexPriority)); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/SetSingleNodeAllocateStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/SetSingleNodeAllocateStep.java index decf1f6a598b7..4bff8d350b79b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/SetSingleNodeAllocateStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/SetSingleNodeAllocateStep.java @@ -58,23 +58,30 @@ public boolean isRetryable() { } @Override - public void performAction(IndexMetadata indexMetadata, ClusterState clusterState, - ClusterStateObserver observer, ActionListener listener) { + public void performAction( + IndexMetadata indexMetadata, + ClusterState clusterState, + ClusterStateObserver observer, + ActionListener listener + ) { // These allocation deciders were chosen because these are the conditions that can prevent // allocation long-term, and that we can inspect in advance. Most other allocation deciders // will either only delay relocation (e.g. ThrottlingAllocationDecider), or don't work very // well when reallocating potentially many shards at once (e.g. DiskThresholdDecider) - AllocationDeciders allocationDeciders = new AllocationDeciders(List.of( - new FilterAllocationDecider(clusterState.getMetadata().settings(), - new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS)), - new DataTierAllocationDecider(), - new NodeVersionAllocationDecider(), - new NodeShutdownAllocationDecider(), - new NodeReplacementAllocationDecider() - )); + AllocationDeciders allocationDeciders = new AllocationDeciders( + List.of( + new FilterAllocationDecider( + clusterState.getMetadata().settings(), + new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS) + ), + new DataTierAllocationDecider(), + new NodeVersionAllocationDecider(), + new NodeShutdownAllocationDecider(), + new NodeReplacementAllocationDecider() + ) + ); final RoutingNodes routingNodes = clusterState.getRoutingNodes(); - RoutingAllocation allocation = new RoutingAllocation(allocationDeciders, routingNodes, clusterState, null, - null, System.nanoTime()); + RoutingAllocation allocation = new RoutingAllocation(allocationDeciders, routingNodes, clusterState, null, null, System.nanoTime()); List validNodeIds = new ArrayList<>(); String indexName = indexMetadata.getIndex().getName(); final Map> routingsByShardId = clusterState.getRoutingTable() @@ -84,10 +91,13 @@ public void performAction(IndexMetadata indexMetadata, ClusterState clusterState if (routingsByShardId.isEmpty() == false) { for (RoutingNode node : routingNodes) { - boolean canAllocateOneCopyOfEachShard = routingsByShardId.values().stream() // For each shard - .allMatch(shardRoutings -> shardRoutings.stream() // Can we allocate at least one shard copy to this node? - .map(shardRouting -> allocationDeciders.canAllocate(shardRouting, node, allocation).type()) - .anyMatch(Decision.Type.YES::equals)); + boolean canAllocateOneCopyOfEachShard = routingsByShardId.values() + .stream() // For each shard + .allMatch( + shardRoutings -> shardRoutings.stream() // Can we allocate at least one shard copy to this node? + .map(shardRouting -> allocationDeciders.canAllocate(shardRouting, node, allocation).type()) + .anyMatch(Decision.Type.YES::equals) + ); if (canAllocateOneCopyOfEachShard) { validNodeIds.add(node.node().getId()); } @@ -98,18 +108,20 @@ public void performAction(IndexMetadata indexMetadata, ClusterState clusterState if (nodeId.isPresent()) { Settings settings = Settings.builder() - .put(IndexMetadata.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + "_id", nodeId.get()) - .putNull(ShardsLimitAllocationDecider.INDEX_TOTAL_SHARDS_PER_NODE_SETTING.getKey()).build(); - UpdateSettingsRequest updateSettingsRequest = new UpdateSettingsRequest(indexName) - .masterNodeTimeout(TimeValue.MAX_VALUE) - .settings(settings); - getClient().admin().indices().updateSettings(updateSettingsRequest, - ActionListener.wrap(response -> listener.onResponse(null), listener::onFailure)); + .put(IndexMetadata.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + "_id", nodeId.get()) + .putNull(ShardsLimitAllocationDecider.INDEX_TOTAL_SHARDS_PER_NODE_SETTING.getKey()) + .build(); + UpdateSettingsRequest updateSettingsRequest = new UpdateSettingsRequest(indexName).masterNodeTimeout(TimeValue.MAX_VALUE) + .settings(settings); + getClient().admin() + .indices() + .updateSettings(updateSettingsRequest, ActionListener.wrap(response -> listener.onResponse(null), listener::onFailure)); } else { // No nodes currently match the allocation rules, so report this as an error and we'll retry logger.debug("could not find any nodes to allocate index [{}] onto prior to shrink", indexName); - listener.onFailure(new NoNodeAvailableException("could not find any nodes to allocate index [" + indexName + "] onto" + - " prior to shrink")); + listener.onFailure( + new NoNodeAvailableException("could not find any nodes to allocate index [" + indexName + "] onto" + " prior to shrink") + ); } } else { // There are no shards for the index, the index might be gone. Even though this is a retryable step ILM will not retry in diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrinkAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrinkAction.java index d743b1a8bf35f..69a4a47d2daa9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrinkAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrinkAction.java @@ -11,14 +11,14 @@ import org.elasticsearch.client.Client; import org.elasticsearch.cluster.metadata.IndexAbstraction; import org.elasticsearch.cluster.metadata.IndexMetadata; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ilm.Step.StepKey; @@ -42,14 +42,19 @@ public class ShrinkAction implements LifecycleAction { public static final String CONDITIONAL_SKIP_SHRINK_STEP = BranchingStep.NAME + "-check-prerequisites"; public static final String CONDITIONAL_DATASTREAM_CHECK_KEY = BranchingStep.NAME + "-on-datastream-check"; - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(NAME, a -> new ShrinkAction((Integer) a[0], (ByteSizeValue) a[1])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + a -> new ShrinkAction((Integer) a[0], (ByteSizeValue) a[1]) + ); static { PARSER.declareInt(ConstructingObjectParser.optionalConstructorArg(), NUMBER_OF_SHARDS_FIELD); - PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(), + PARSER.declareField( + ConstructingObjectParser.optionalConstructorArg(), (p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), MAX_PRIMARY_SHARD_SIZE.getPreferredName()), - MAX_PRIMARY_SHARD_SIZE, ObjectParser.ValueType.STRING); + MAX_PRIMARY_SHARD_SIZE, + ObjectParser.ValueType.STRING + ); } private Integer numberOfShards; @@ -151,35 +156,54 @@ public List toSteps(Client client, String phase, Step.StepKey nextStepKey) StepKey replaceDataStreamIndexKey = new StepKey(phase, NAME, ReplaceDataStreamBackingIndexStep.NAME); StepKey deleteIndexKey = new StepKey(phase, NAME, DeleteStep.NAME); - BranchingStep conditionalSkipShrinkStep = new BranchingStep(preShrinkBranchingKey, checkNotWriteIndex, nextStepKey, + BranchingStep conditionalSkipShrinkStep = new BranchingStep( + preShrinkBranchingKey, + checkNotWriteIndex, + nextStepKey, (index, clusterState) -> { IndexMetadata indexMetadata = clusterState.getMetadata().index(index); if (numberOfShards != null && indexMetadata.getNumberOfShards() == numberOfShards) { return true; } if (indexMetadata.getSettings().get(LifecycleSettings.SNAPSHOT_INDEX_NAME) != null) { - logger.warn("[{}] action is configured for index [{}] in policy [{}] which is mounted as searchable snapshot. " + - "Skipping this action", ShrinkAction.NAME, indexMetadata.getIndex().getName(), - LifecycleSettings.LIFECYCLE_NAME_SETTING.get(indexMetadata.getSettings())); + logger.warn( + "[{}] action is configured for index [{}] in policy [{}] which is mounted as searchable snapshot. " + + "Skipping this action", + ShrinkAction.NAME, + indexMetadata.getIndex().getName(), + LifecycleSettings.LIFECYCLE_NAME_SETTING.get(indexMetadata.getSettings()) + ); return true; } return false; - }); - CheckNotDataStreamWriteIndexStep checkNotWriteIndexStep = new CheckNotDataStreamWriteIndexStep(checkNotWriteIndex, - waitForNoFollowerStepKey); + } + ); + CheckNotDataStreamWriteIndexStep checkNotWriteIndexStep = new CheckNotDataStreamWriteIndexStep( + checkNotWriteIndex, + waitForNoFollowerStepKey + ); WaitForNoFollowersStep waitForNoFollowersStep = new WaitForNoFollowersStep(waitForNoFollowerStepKey, readOnlyKey, client); ReadOnlyStep readOnlyStep = new ReadOnlyStep(readOnlyKey, checkTargetShardsCountKey, client); - CheckTargetShardsCountStep checkTargetShardsCountStep = new CheckTargetShardsCountStep(checkTargetShardsCountKey, - cleanupShrinkIndexKey, numberOfShards); + CheckTargetShardsCountStep checkTargetShardsCountStep = new CheckTargetShardsCountStep( + checkTargetShardsCountKey, + cleanupShrinkIndexKey, + numberOfShards + ); // we generate a unique shrink index name but we also retry if the allocation of the shrunk index is not possible, so we want to // delete the "previously generated" shrink index (this is a no-op if it's the first run of the action and he haven't generated a // shrink index name) - CleanupShrinkIndexStep cleanupShrinkIndexStep = new CleanupShrinkIndexStep(cleanupShrinkIndexKey, generateShrinkIndexNameKey, - client); + CleanupShrinkIndexStep cleanupShrinkIndexStep = new CleanupShrinkIndexStep( + cleanupShrinkIndexKey, + generateShrinkIndexNameKey, + client + ); // generate a unique shrink index name and store it in the ILM execution state - GenerateUniqueIndexNameStep generateUniqueIndexNameStep = - new GenerateUniqueIndexNameStep(generateShrinkIndexNameKey, setSingleNodeKey, SHRUNKEN_INDEX_PREFIX, - (generatedIndexName, lifecycleStateBuilder) -> lifecycleStateBuilder.setShrinkIndexName(generatedIndexName)); + GenerateUniqueIndexNameStep generateUniqueIndexNameStep = new GenerateUniqueIndexNameStep( + generateShrinkIndexNameKey, + setSingleNodeKey, + SHRUNKEN_INDEX_PREFIX, + (generatedIndexName, lifecycleStateBuilder) -> lifecycleStateBuilder.setShrinkIndexName(generatedIndexName) + ); // choose a node to collocate the source index in preparation for shrink SetSingleNodeAllocateStep setSingleNodeStep = new SetSingleNodeAllocateStep(setSingleNodeKey, allocationRoutedKey, client); @@ -187,36 +211,66 @@ public List toSteps(Client client, String phase, Step.StepKey nextStepKey) // breached (controlled by LifecycleSettings.LIFECYCLE_STEP_WAIT_TIME_THRESHOLD) at which point we rewind to the // "set-single-node-allocation" step to choose another node to host the shrink operation ClusterStateWaitUntilThresholdStep checkShrinkReadyStep = new ClusterStateWaitUntilThresholdStep( - new CheckShrinkReadyStep(allocationRoutedKey, shrinkKey), setSingleNodeKey); + new CheckShrinkReadyStep(allocationRoutedKey, shrinkKey), + setSingleNodeKey + ); ShrinkStep shrink = new ShrinkStep(shrinkKey, enoughShardsKey, client, numberOfShards, maxPrimaryShardSize); // wait until the shrunk index is recovered. we again wait until the configured threshold is breached and if the shrunk index has // not successfully recovered until then, we rewind to the "cleanup-shrink-index" step to delete this unsuccessful shrunk index // and retry the operation by generating a new shrink index name and attempting to shrink again ClusterStateWaitUntilThresholdStep allocated = new ClusterStateWaitUntilThresholdStep( - new ShrunkShardsAllocatedStep(enoughShardsKey, copyMetadataKey), cleanupShrinkIndexKey); - CopyExecutionStateStep copyMetadata = new CopyExecutionStateStep(copyMetadataKey, dataStreamCheckBranchingKey, - ShrinkIndexNameSupplier::getShrinkIndexName, isShrunkIndexKey); + new ShrunkShardsAllocatedStep(enoughShardsKey, copyMetadataKey), + cleanupShrinkIndexKey + ); + CopyExecutionStateStep copyMetadata = new CopyExecutionStateStep( + copyMetadataKey, + dataStreamCheckBranchingKey, + ShrinkIndexNameSupplier::getShrinkIndexName, + isShrunkIndexKey + ); // by the time we get to this step we have 2 indices, the source and the shrunken one. we now need to choose an index // swapping strategy such that the shrunken index takes the place of the source index (which is also deleted). // if the source index is part of a data stream it's a matter of replacing it with the shrunken index one in the data stream and // then deleting the source index; otherwise we'll use the alias management api to atomically transfer the aliases from source to // the shrunken index and delete the source - BranchingStep isDataStreamBranchingStep = new BranchingStep(dataStreamCheckBranchingKey, aliasKey, replaceDataStreamIndexKey, + BranchingStep isDataStreamBranchingStep = new BranchingStep( + dataStreamCheckBranchingKey, + aliasKey, + replaceDataStreamIndexKey, (index, clusterState) -> { IndexAbstraction indexAbstraction = clusterState.metadata().getIndicesLookup().get(index.getName()); assert indexAbstraction != null : "invalid cluster metadata. index [" + index.getName() + "] was not found"; return indexAbstraction.getParentDataStream() != null; - }); + } + ); ShrinkSetAliasStep aliasSwapAndDelete = new ShrinkSetAliasStep(aliasKey, isShrunkIndexKey, client); - ReplaceDataStreamBackingIndexStep replaceDataStreamBackingIndex = new ReplaceDataStreamBackingIndexStep(replaceDataStreamIndexKey, - deleteIndexKey, ShrinkIndexNameSupplier::getShrinkIndexName); + ReplaceDataStreamBackingIndexStep replaceDataStreamBackingIndex = new ReplaceDataStreamBackingIndexStep( + replaceDataStreamIndexKey, + deleteIndexKey, + ShrinkIndexNameSupplier::getShrinkIndexName + ); DeleteStep deleteSourceIndexStep = new DeleteStep(deleteIndexKey, isShrunkIndexKey, client); ShrunkenIndexCheckStep waitOnShrinkTakeover = new ShrunkenIndexCheckStep(isShrunkIndexKey, nextStepKey); - return Arrays.asList(conditionalSkipShrinkStep, checkNotWriteIndexStep, waitForNoFollowersStep, readOnlyStep, - checkTargetShardsCountStep, cleanupShrinkIndexStep, generateUniqueIndexNameStep, setSingleNodeStep, checkShrinkReadyStep, - shrink, allocated, copyMetadata, isDataStreamBranchingStep, aliasSwapAndDelete, waitOnShrinkTakeover, - replaceDataStreamBackingIndex, deleteSourceIndexStep); + return Arrays.asList( + conditionalSkipShrinkStep, + checkNotWriteIndexStep, + waitForNoFollowersStep, + readOnlyStep, + checkTargetShardsCountStep, + cleanupShrinkIndexStep, + generateUniqueIndexNameStep, + setSingleNodeStep, + checkShrinkReadyStep, + shrink, + allocated, + copyMetadata, + isDataStreamBranchingStep, + aliasSwapAndDelete, + waitOnShrinkTakeover, + replaceDataStreamBackingIndex, + deleteSourceIndexStep + ); } @Override @@ -224,8 +278,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; ShrinkAction that = (ShrinkAction) o; - return Objects.equals(numberOfShards, that.numberOfShards) && - Objects.equals(maxPrimaryShardSize, that.maxPrimaryShardSize); + return Objects.equals(numberOfShards, that.numberOfShards) && Objects.equals(maxPrimaryShardSize, that.maxPrimaryShardSize); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrinkIndexNameSupplier.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrinkIndexNameSupplier.java index 0fba484223b1c..b5bd8916c2901 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrinkIndexNameSupplier.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrinkIndexNameSupplier.java @@ -11,8 +11,7 @@ public final class ShrinkIndexNameSupplier { public static final String SHRUNKEN_INDEX_PREFIX = "shrink-"; - private ShrinkIndexNameSupplier() { - } + private ShrinkIndexNameSupplier() {} /** * This could be seen as a getter with a fallback, as it'll attempt to read the shrink index name from the provided lifecycle execution diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrinkStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrinkStep.java index de1eb002b1ae0..fdf25ca04c7aa 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrinkStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrinkStep.java @@ -29,12 +29,10 @@ public class ShrinkStep extends AsyncActionStep { public static final String NAME = "shrink"; private static final Logger logger = LogManager.getLogger(ShrinkStep.class); - private Integer numberOfShards; private ByteSizeValue maxPrimaryShardSize; - public ShrinkStep(StepKey key, StepKey nextStepKey, Client client, Integer numberOfShards, - ByteSizeValue maxPrimaryShardSize) { + public ShrinkStep(StepKey key, StepKey nextStepKey, Client client, Integer numberOfShards, ByteSizeValue maxPrimaryShardSize) { super(key, nextStepKey, client); this.numberOfShards = numberOfShards; this.maxPrimaryShardSize = maxPrimaryShardSize; @@ -54,19 +52,26 @@ public ByteSizeValue getMaxPrimaryShardSize() { } @Override - public void performAction(IndexMetadata indexMetadata, ClusterState currentState, - ClusterStateObserver observer, ActionListener listener) { + public void performAction( + IndexMetadata indexMetadata, + ClusterState currentState, + ClusterStateObserver observer, + ActionListener listener + ) { LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(indexMetadata); if (lifecycleState.getLifecycleDate() == null) { - throw new IllegalStateException("source index [" + indexMetadata.getIndex().getName() + - "] is missing lifecycle date"); + throw new IllegalStateException("source index [" + indexMetadata.getIndex().getName() + "] is missing lifecycle date"); } String shrunkenIndexName = getShrinkIndexName(indexMetadata.getIndex().getName(), lifecycleState); if (currentState.metadata().index(shrunkenIndexName) != null) { - logger.warn("skipping [{}] step for index [{}] as part of policy [{}] as the shrunk index [{}] already exists", - ShrinkStep.NAME, indexMetadata.getIndex().getName(), - LifecycleSettings.LIFECYCLE_NAME_SETTING.get(indexMetadata.getSettings()), shrunkenIndexName); + logger.warn( + "skipping [{}] step for index [{}] as part of policy [{}] as the shrunk index [{}] already exists", + ShrinkStep.NAME, + indexMetadata.getIndex().getName(), + LifecycleSettings.LIFECYCLE_NAME_SETTING.get(indexMetadata.getSettings()), + shrunkenIndexName + ); listener.onResponse(null); return; } @@ -83,8 +88,9 @@ public void performAction(IndexMetadata indexMetadata, ClusterState currentState } Settings relevantTargetSettings = builder.build(); - ResizeRequest resizeRequest = new ResizeRequest(shrunkenIndexName, indexMetadata.getIndex().getName()) - .masterNodeTimeout(TimeValue.MAX_VALUE); + ResizeRequest resizeRequest = new ResizeRequest(shrunkenIndexName, indexMetadata.getIndex().getName()).masterNodeTimeout( + TimeValue.MAX_VALUE + ); resizeRequest.setMaxPrimaryShardSize(maxPrimaryShardSize); resizeRequest.getTargetIndexRequest().settings(relevantTargetSettings); @@ -111,9 +117,9 @@ public boolean equals(Object obj) { return false; } ShrinkStep other = (ShrinkStep) obj; - return super.equals(obj) && - Objects.equals(numberOfShards, other.numberOfShards) && - Objects.equals(maxPrimaryShardSize, other.maxPrimaryShardSize); + return super.equals(obj) + && Objects.equals(numberOfShards, other.numberOfShards) + && Objects.equals(maxPrimaryShardSize, other.maxPrimaryShardSize); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrunkShardsAllocatedStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrunkShardsAllocatedStep.java index 0654959bc01c2..a994e162b8b66 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrunkShardsAllocatedStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrunkShardsAllocatedStep.java @@ -11,12 +11,12 @@ import org.elasticsearch.action.support.ActiveShardCount; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.IndexMetadata; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; +import org.elasticsearch.index.Index; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.index.Index; import java.io.IOException; import java.util.Objects; @@ -78,8 +78,10 @@ public static final class Info implements ToXContentObject { static final ParseField SHRUNK_INDEX_EXISTS = new ParseField("shrunk_index_exists"); static final ParseField ALL_SHARDS_ACTIVE = new ParseField("all_shards_active"); static final ParseField MESSAGE = new ParseField("message"); - static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("shrunk_shards_allocated_step_info", - a -> new Info((boolean) a[0], (int) a[1], (boolean) a[2])); + static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "shrunk_shards_allocated_step_info", + a -> new Info((boolean) a[0], (int) a[1], (boolean) a[2]) + ); static { PARSER.declareBoolean(ConstructingObjectParser.constructorArg(), SHRUNK_INDEX_EXISTS); PARSER.declareInt(ConstructingObjectParser.constructorArg(), ACTUAL_SHARDS); @@ -137,9 +139,9 @@ public boolean equals(Object obj) { return false; } Info other = (Info) obj; - return Objects.equals(shrunkIndexExists, other.shrunkIndexExists) && - Objects.equals(actualShards, other.actualShards) && - Objects.equals(allShardsActive, other.allShardsActive); + return Objects.equals(shrunkIndexExists, other.shrunkIndexExists) + && Objects.equals(actualShards, other.actualShards) + && Objects.equals(allShardsActive, other.allShardsActive); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrunkenIndexCheckStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrunkenIndexCheckStep.java index 22ec03a0e4fb8..415e2502e1e8e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrunkenIndexCheckStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrunkenIndexCheckStep.java @@ -10,12 +10,12 @@ import org.apache.logging.log4j.Logger; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.IndexMetadata; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; +import org.elasticsearch.index.Index; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.index.Index; import java.io.IOException; import java.util.Objects; @@ -48,16 +48,14 @@ public Result isConditionMet(Index index, ClusterState clusterState) { // Index must have been since deleted, ignore it return new Result(false, null); } - String shrunkenIndexSource = IndexMetadata.INDEX_RESIZE_SOURCE_NAME.get( - clusterState.metadata().index(index).getSettings()); + String shrunkenIndexSource = IndexMetadata.INDEX_RESIZE_SOURCE_NAME.get(clusterState.metadata().index(index).getSettings()); if (Strings.isNullOrEmpty(shrunkenIndexSource)) { throw new IllegalStateException("step[" + NAME + "] is checking an un-shrunken index[" + index.getName() + "]"); } LifecycleExecutionState lifecycleState = fromIndexMetadata(idxMeta); String targetIndexName = getShrinkIndexName(shrunkenIndexSource, lifecycleState); - boolean isConditionMet = index.getName().equals(targetIndexName) && - clusterState.metadata().index(shrunkenIndexSource) == null; + boolean isConditionMet = index.getName().equals(targetIndexName) && clusterState.metadata().index(shrunkenIndexSource) == null; if (isConditionMet) { return new Result(true, null); } else { @@ -72,8 +70,10 @@ public static final class Info implements ToXContentObject { static final ParseField ORIGINAL_INDEX_NAME = new ParseField("original_index_name"); static final ParseField MESSAGE = new ParseField("message"); - static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("shrunken_index_check_step_info", - a -> new Info((String) a[0])); + static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "shrunken_index_check_step_info", + a -> new Info((String) a[0]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), ORIGINAL_INDEX_NAME); PARSER.declareString((i, s) -> {}, MESSAGE); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/StartILMRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/StartILMRequest.java index b4b25a36163d5..03d647e867698 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/StartILMRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/StartILMRequest.java @@ -20,8 +20,7 @@ public StartILMRequest(StreamInput in) throws IOException { } - public StartILMRequest() { - } + public StartILMRequest() {} @Override public ActionRequestValidationException validate() { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/Step.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/Step.java index 9d7906d91f899..5d0caecb685a2 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/Step.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/Step.java @@ -6,12 +6,12 @@ */ package org.elasticsearch.xpack.core.ilm; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -58,8 +58,7 @@ public boolean equals(Object obj) { return false; } Step other = (Step) obj; - return Objects.equals(key, other.key) && - Objects.equals(nextStepKey, other.nextStepKey); + return Objects.equals(key, other.key) && Objects.equals(nextStepKey, other.nextStepKey); } @Override @@ -75,8 +74,10 @@ public static final class StepKey implements Writeable, ToXContentObject { public static final ParseField PHASE_FIELD = new ParseField("phase"); public static final ParseField ACTION_FIELD = new ParseField("action"); public static final ParseField NAME_FIELD = new ParseField("name"); - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("stepkey", a -> new StepKey((String) a[0], (String) a[1], (String) a[2])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "stepkey", + a -> new StepKey((String) a[0], (String) a[1], (String) a[2]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), PHASE_FIELD); PARSER.declareString(ConstructingObjectParser.constructorArg(), ACTION_FIELD); @@ -132,9 +133,7 @@ public boolean equals(Object obj) { return false; } StepKey other = (StepKey) obj; - return Objects.equals(phase, other.phase) && - Objects.equals(action, other.action) && - Objects.equals(name, other.name); + return Objects.equals(phase, other.phase) && Objects.equals(action, other.action) && Objects.equals(name, other.name); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/StopILMRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/StopILMRequest.java index a6c77c85b5b2e..7588c209a0d44 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/StopILMRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/StopILMRequest.java @@ -20,8 +20,7 @@ public StopILMRequest(StreamInput in) throws IOException { } - public StopILMRequest() { - } + public StopILMRequest() {} @Override public ActionRequestValidationException validate() { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/SwapAliasesAndDeleteSourceIndexStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/SwapAliasesAndDeleteSourceIndexStep.java index 63cc99301b7a6..2107673ae2ac9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/SwapAliasesAndDeleteSourceIndexStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/SwapAliasesAndDeleteSourceIndexStep.java @@ -44,16 +44,25 @@ public String getTargetIndexPrefix() { } @Override - public void performAction(IndexMetadata indexMetadata, ClusterState currentClusterState, ClusterStateObserver observer, - ActionListener listener) { + public void performAction( + IndexMetadata indexMetadata, + ClusterState currentClusterState, + ClusterStateObserver observer, + ActionListener listener + ) { String originalIndex = indexMetadata.getIndex().getName(); final String targetIndexName = targetIndexPrefix + originalIndex; IndexMetadata targetIndexMetadata = currentClusterState.metadata().index(targetIndexName); if (targetIndexMetadata == null) { String policyName = indexMetadata.getSettings().get(LifecycleSettings.LIFECYCLE_NAME); - String errorMessage = String.format(Locale.ROOT, "target index [%s] doesn't exist. stopping execution of lifecycle [%s] for" + - " index [%s]", targetIndexName, policyName, originalIndex); + String errorMessage = String.format( + Locale.ROOT, + "target index [%s] doesn't exist. stopping execution of lifecycle [%s] for" + " index [%s]", + targetIndexName, + policyName, + originalIndex + ); logger.debug(errorMessage); listener.onFailure(new IllegalStateException(errorMessage)); return; @@ -68,31 +77,36 @@ public void performAction(IndexMetadata indexMetadata, ClusterState currentClust *

    * The is_write_index will *not* be set on the target index as this operation is currently executed on read-only indices. */ - static void deleteSourceIndexAndTransferAliases(Client client, IndexMetadata sourceIndex, String targetIndex, - ActionListener listener) { + static void deleteSourceIndexAndTransferAliases( + Client client, + IndexMetadata sourceIndex, + String targetIndex, + ActionListener listener + ) { String sourceIndexName = sourceIndex.getIndex().getName(); - IndicesAliasesRequest aliasesRequest = new IndicesAliasesRequest() - .masterNodeTimeout(TimeValue.MAX_VALUE) + IndicesAliasesRequest aliasesRequest = new IndicesAliasesRequest().masterNodeTimeout(TimeValue.MAX_VALUE) .addAliasAction(IndicesAliasesRequest.AliasActions.removeIndex().index(sourceIndexName)) .addAliasAction(IndicesAliasesRequest.AliasActions.add().index(targetIndex).alias(sourceIndexName)); // copy over other aliases from source index sourceIndex.getAliases().values().forEach(aliasMetaDataToAdd -> { // inherit all alias properties except `is_write_index` - aliasesRequest.addAliasAction(IndicesAliasesRequest.AliasActions.add() - .index(targetIndex).alias(aliasMetaDataToAdd.alias()) - .indexRouting(aliasMetaDataToAdd.indexRouting()) - .searchRouting(aliasMetaDataToAdd.searchRouting()) - .filter(aliasMetaDataToAdd.filter() == null ? null : aliasMetaDataToAdd.filter().string()) - .writeIndex(null)); + aliasesRequest.addAliasAction( + IndicesAliasesRequest.AliasActions.add() + .index(targetIndex) + .alias(aliasMetaDataToAdd.alias()) + .indexRouting(aliasMetaDataToAdd.indexRouting()) + .searchRouting(aliasMetaDataToAdd.searchRouting()) + .filter(aliasMetaDataToAdd.filter() == null ? null : aliasMetaDataToAdd.filter().string()) + .writeIndex(null) + ); }); - client.admin().indices().aliases(aliasesRequest, - ActionListener.wrap(response -> { - if (response.isAcknowledged() == false) { - logger.warn("aliases swap from [{}] to [{}] response was not acknowledged", sourceIndexName, targetIndex); - } - listener.onResponse(null); - }, listener::onFailure)); + client.admin().indices().aliases(aliasesRequest, ActionListener.wrap(response -> { + if (response.isAcknowledged() == false) { + logger.warn("aliases swap from [{}] to [{}] response was not acknowledged", sourceIndexName, targetIndex); + } + listener.onResponse(null); + }, listener::onFailure)); } @Override @@ -114,7 +128,6 @@ public boolean equals(Object obj) { return false; } SwapAliasesAndDeleteSourceIndexStep other = (SwapAliasesAndDeleteSourceIndexStep) obj; - return super.equals(obj) && - Objects.equals(targetIndexPrefix, other.targetIndexPrefix); + return super.equals(obj) && Objects.equals(targetIndexPrefix, other.targetIndexPrefix); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/TimeseriesLifecycleType.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/TimeseriesLifecycleType.java index bb4b5f5fb3033..8bf42e11af3b2 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/TimeseriesLifecycleType.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/TimeseriesLifecycleType.java @@ -51,16 +51,35 @@ public class TimeseriesLifecycleType implements LifecycleType { static final String DELETE_PHASE = "delete"; static final List ORDERED_VALID_PHASES = Arrays.asList(HOT_PHASE, WARM_PHASE, COLD_PHASE, FROZEN_PHASE, DELETE_PHASE); - static final List ORDERED_VALID_HOT_ACTIONS = Stream.of(SetPriorityAction.NAME, UnfollowAction.NAME, RolloverAction.NAME, - ReadOnlyAction.NAME, RollupV2.isEnabled() ? RollupILMAction.NAME : null, ShrinkAction.NAME, ForceMergeAction.NAME, - SearchableSnapshotAction.NAME) - .filter(Objects::nonNull).collect(toList()); - static final List ORDERED_VALID_WARM_ACTIONS = Arrays.asList(SetPriorityAction.NAME, UnfollowAction.NAME, ReadOnlyAction.NAME, - AllocateAction.NAME, MigrateAction.NAME, ShrinkAction.NAME, ForceMergeAction.NAME); - static final List ORDERED_VALID_COLD_ACTIONS = Stream.of(SetPriorityAction.NAME, UnfollowAction.NAME, ReadOnlyAction.NAME, - SearchableSnapshotAction.NAME, AllocateAction.NAME, MigrateAction.NAME, FreezeAction.NAME, - RollupV2.isEnabled() ? RollupILMAction.NAME : null) - .filter(Objects::nonNull).collect(toList()); + static final List ORDERED_VALID_HOT_ACTIONS = Stream.of( + SetPriorityAction.NAME, + UnfollowAction.NAME, + RolloverAction.NAME, + ReadOnlyAction.NAME, + RollupV2.isEnabled() ? RollupILMAction.NAME : null, + ShrinkAction.NAME, + ForceMergeAction.NAME, + SearchableSnapshotAction.NAME + ).filter(Objects::nonNull).collect(toList()); + static final List ORDERED_VALID_WARM_ACTIONS = Arrays.asList( + SetPriorityAction.NAME, + UnfollowAction.NAME, + ReadOnlyAction.NAME, + AllocateAction.NAME, + MigrateAction.NAME, + ShrinkAction.NAME, + ForceMergeAction.NAME + ); + static final List ORDERED_VALID_COLD_ACTIONS = Stream.of( + SetPriorityAction.NAME, + UnfollowAction.NAME, + ReadOnlyAction.NAME, + SearchableSnapshotAction.NAME, + AllocateAction.NAME, + MigrateAction.NAME, + FreezeAction.NAME, + RollupV2.isEnabled() ? RollupILMAction.NAME : null + ).filter(Objects::nonNull).collect(toList()); static final List ORDERED_VALID_FROZEN_ACTIONS = Arrays.asList(SearchableSnapshotAction.NAME); static final List ORDERED_VALID_DELETE_ACTIONS = Arrays.asList(WaitForSnapshotAction.NAME, DeleteAction.NAME); @@ -71,26 +90,35 @@ public class TimeseriesLifecycleType implements LifecycleType { static final Set VALID_DELETE_ACTIONS = Sets.newHashSet(ORDERED_VALID_DELETE_ACTIONS); private static final Map> ALLOWED_ACTIONS = Map.of( - HOT_PHASE, VALID_HOT_ACTIONS, - WARM_PHASE, VALID_WARM_ACTIONS, - COLD_PHASE, VALID_COLD_ACTIONS, - DELETE_PHASE, VALID_DELETE_ACTIONS, - FROZEN_PHASE, VALID_FROZEN_ACTIONS + HOT_PHASE, + VALID_HOT_ACTIONS, + WARM_PHASE, + VALID_WARM_ACTIONS, + COLD_PHASE, + VALID_COLD_ACTIONS, + DELETE_PHASE, + VALID_DELETE_ACTIONS, + FROZEN_PHASE, + VALID_FROZEN_ACTIONS ); - static final Set HOT_ACTIONS_THAT_REQUIRE_ROLLOVER = Sets.newHashSet(ReadOnlyAction.NAME, ShrinkAction.NAME, - ForceMergeAction.NAME, RollupILMAction.NAME, SearchableSnapshotAction.NAME); + static final Set HOT_ACTIONS_THAT_REQUIRE_ROLLOVER = Sets.newHashSet( + ReadOnlyAction.NAME, + ShrinkAction.NAME, + ForceMergeAction.NAME, + RollupILMAction.NAME, + SearchableSnapshotAction.NAME + ); // Set of actions that cannot be defined (executed) after the managed index has been mounted as searchable snapshot. // It's ordered to produce consistent error messages which can be unit tested. - static final Set ACTIONS_CANNOT_FOLLOW_SEARCHABLE_SNAPSHOT = new LinkedHashSet<>(Arrays.asList( - ForceMergeAction.NAME, FreezeAction.NAME, ShrinkAction.NAME, RollupILMAction.NAME)); + static final Set ACTIONS_CANNOT_FOLLOW_SEARCHABLE_SNAPSHOT = new LinkedHashSet<>( + Arrays.asList(ForceMergeAction.NAME, FreezeAction.NAME, ShrinkAction.NAME, RollupILMAction.NAME) + ); - private TimeseriesLifecycleType() { - } + private TimeseriesLifecycleType() {} @Override - public void writeTo(StreamOutput out) throws IOException { - } + public void writeTo(StreamOutput out) throws IOException {} @Override public String getWriteableName() { @@ -103,9 +131,10 @@ public List getOrderedPhases(Map phases) { Phase phase = phases.get(phaseName); if (phase != null) { Map actions = phase.getActions(); - if (actions.containsKey(UnfollowAction.NAME) == false && - (actions.containsKey(RolloverAction.NAME) || actions.containsKey(ShrinkAction.NAME) || - actions.containsKey(SearchableSnapshotAction.NAME))) { + if (actions.containsKey(UnfollowAction.NAME) == false + && (actions.containsKey(RolloverAction.NAME) + || actions.containsKey(ShrinkAction.NAME) + || actions.containsKey(SearchableSnapshotAction.NAME))) { Map actionMap = new HashMap<>(phase.getActions()); actionMap.put(UnfollowAction.NAME, new UnfollowAction()); phase = new Phase(phase.getName(), phase.getMinimumAge(), actionMap); @@ -183,20 +212,15 @@ public List getOrderedActions(Phase phase) { Map actions = phase.getActions(); switch (phase.getName()) { case HOT_PHASE: - return ORDERED_VALID_HOT_ACTIONS.stream().map(actions::get) - .filter(Objects::nonNull).collect(toList()); + return ORDERED_VALID_HOT_ACTIONS.stream().map(actions::get).filter(Objects::nonNull).collect(toList()); case WARM_PHASE: - return ORDERED_VALID_WARM_ACTIONS.stream().map(actions::get) - .filter(Objects::nonNull).collect(toList()); + return ORDERED_VALID_WARM_ACTIONS.stream().map(actions::get).filter(Objects::nonNull).collect(toList()); case COLD_PHASE: - return ORDERED_VALID_COLD_ACTIONS.stream().map(actions::get) - .filter(Objects::nonNull).collect(toList()); + return ORDERED_VALID_COLD_ACTIONS.stream().map(actions::get).filter(Objects::nonNull).collect(toList()); case FROZEN_PHASE: - return ORDERED_VALID_FROZEN_ACTIONS.stream().map(actions::get) - .filter(Objects::nonNull).collect(toList()); + return ORDERED_VALID_FROZEN_ACTIONS.stream().map(actions::get).filter(Objects::nonNull).collect(toList()); case DELETE_PHASE: - return ORDERED_VALID_DELETE_ACTIONS.stream().map(actions::get) - .filter(Objects::nonNull).collect(toList()); + return ORDERED_VALID_DELETE_ACTIONS.stream().map(actions::get).filter(Objects::nonNull).collect(toList()); default: throw new IllegalArgumentException("lifecycle type [" + TYPE + "] does not support phase [" + phase.getName() + "]"); } @@ -227,8 +251,9 @@ public String getNextActionName(String currentActionName, Phase phase) { int index = orderedActionNames.indexOf(currentActionName); if (index < 0) { - throw new IllegalArgumentException("[" + currentActionName + "] is not a valid action for phase [" + phase.getName() - + "] in lifecycle type [" + TYPE + "]"); + throw new IllegalArgumentException( + "[" + currentActionName + "] is not a valid action for phase [" + phase.getName() + "] in lifecycle type [" + TYPE + "]" + ); } else { // Find the next action after `index` that exists in the phase and return it while (++index < orderedActionNames.size()) { @@ -252,8 +277,9 @@ public void validate(Collection phases) { } phase.getActions().forEach((actionName, action) -> { if (ALLOWED_ACTIONS.get(phase.getName()).contains(actionName) == false) { - throw new IllegalArgumentException("invalid action [" + actionName + "] " + - "defined in phase [" + phase.getName() + "]"); + throw new IllegalArgumentException( + "invalid action [" + actionName + "] " + "defined in phase [" + phase.getName() + "]" + ); } }); }); @@ -268,24 +294,38 @@ public void validate(Collection phases) { .flatMap(phase -> Sets.intersection(phase.getActions().keySet(), HOT_ACTIONS_THAT_REQUIRE_ROLLOVER).stream()) .collect(Collectors.joining(", ")); if (Strings.hasText(invalidHotPhaseActions)) { - throw new IllegalArgumentException("the [" + invalidHotPhaseActions + - "] action(s) may not be used in the [" + HOT_PHASE + - "] phase without an accompanying [" + RolloverAction.NAME + "] action"); + throw new IllegalArgumentException( + "the [" + + invalidHotPhaseActions + + "] action(s) may not be used in the [" + + HOT_PHASE + + "] phase without an accompanying [" + + RolloverAction.NAME + + "] action" + ); } // look for phases that have the migrate action enabled and also specify allocation rules via the AllocateAction String phasesWithConflictingMigrationActions = phases.stream() - .filter(phase -> phase.getActions().containsKey(MigrateAction.NAME) && - ((MigrateAction) phase.getActions().get(MigrateAction.NAME)).isEnabled() && - phase.getActions().containsKey(AllocateAction.NAME) && - definesAllocationRules((AllocateAction) phase.getActions().get(AllocateAction.NAME)) + .filter( + phase -> phase.getActions().containsKey(MigrateAction.NAME) + && ((MigrateAction) phase.getActions().get(MigrateAction.NAME)).isEnabled() + && phase.getActions().containsKey(AllocateAction.NAME) + && definesAllocationRules((AllocateAction) phase.getActions().get(AllocateAction.NAME)) ) .map(Phase::getName) .collect(Collectors.joining(",")); if (Strings.hasText(phasesWithConflictingMigrationActions)) { - throw new IllegalArgumentException("phases [" + phasesWithConflictingMigrationActions + "] specify an enabled " + - MigrateAction.NAME + " action and an " + AllocateAction.NAME + " action with allocation rules. specify only a single " + - "data migration in each phase"); + throw new IllegalArgumentException( + "phases [" + + phasesWithConflictingMigrationActions + + "] specify an enabled " + + MigrateAction.NAME + + " action and an " + + AllocateAction.NAME + + " action with allocation rules. specify only a single " + + "data migration in each phase" + ); } validateActionsFollowingSearchableSnapshot(phases); @@ -328,14 +368,18 @@ static void validateActionsFollowingSearchableSnapshot(Collection phases) final String phasesDefiningIllegalActions = phasesFollowingSearchableSnapshot.stream() // filter the phases that define illegal actions - .filter(phase -> - Collections.disjoint(ACTIONS_CANNOT_FOLLOW_SEARCHABLE_SNAPSHOT, phase.getActions().keySet()) == false) + .filter(phase -> Collections.disjoint(ACTIONS_CANNOT_FOLLOW_SEARCHABLE_SNAPSHOT, phase.getActions().keySet()) == false) .map(Phase::getName) .collect(Collectors.joining(",")); if (Strings.hasText(phasesDefiningIllegalActions)) { - throw new IllegalArgumentException("phases [" + phasesDefiningIllegalActions + "] define one or more of " + - ACTIONS_CANNOT_FOLLOW_SEARCHABLE_SNAPSHOT + " actions which are not allowed after a " + - "managed index is mounted as a searchable snapshot"); + throw new IllegalArgumentException( + "phases [" + + phasesDefiningIllegalActions + + "] define one or more of " + + ACTIONS_CANNOT_FOLLOW_SEARCHABLE_SNAPSHOT + + " actions which are not allowed after a " + + "managed index is mounted as a searchable snapshot" + ); } } @@ -349,9 +393,13 @@ static void validateAllSearchableSnapshotActionsUseSameRepository(Collection 1) { - throw new IllegalArgumentException("policy specifies [" + SearchableSnapshotAction.NAME + - "] action multiple times with differing repositories " + allRepos + - ", the same repository must be used for all searchable snapshot actions"); + throw new IllegalArgumentException( + "policy specifies [" + + SearchableSnapshotAction.NAME + + "] action multiple times with differing repositories " + + allRepos + + ", the same repository must be used for all searchable snapshot actions" + ); } } @@ -392,18 +440,20 @@ public static String validateMonotonicallyIncreasingPhaseTimings(Collection 0) { phasesWithBadAges.forEach(p -> invalidPhases.add(p.getName())); - //build an error message string + // build an error message string Iterator it = phasesWithBadAges.iterator(); Phase badPhase = it.next(); String error = "Your policy is configured to run the " - + badPhase.getName() + " phase (min_age: " + badPhase.getMinimumAge() + ")"; + + badPhase.getName() + + " phase (min_age: " + + badPhase.getMinimumAge() + + ")"; if (phasesWithBadAges.size() > 1) { while (it.hasNext()) { badPhase = it.next(); - error = error + ", the " + badPhase.getName() + " phase (min_age: " - + badPhase.getMinimumAge() + ")"; + error = error + ", the " + badPhase.getName() + " phase (min_age: " + badPhase.getMinimumAge() + ")"; } // if multiple phases are cited replace last occurrence of "," with " and" StringBuilder builder = new StringBuilder(); @@ -413,8 +463,12 @@ public static String validateMonotonicallyIncreasingPhaseTimings(Collection phases) { - Optional maybeFrozenPhase = phases.stream() - .filter(p -> FROZEN_PHASE.equals(p.getName())) - .findFirst(); + Optional maybeFrozenPhase = phases.stream().filter(p -> FROZEN_PHASE.equals(p.getName())).findFirst(); maybeFrozenPhase.ifPresent(p -> { if (p.getActions().containsKey(SearchableSnapshotAction.NAME) == false) { - throw new IllegalArgumentException("policy specifies the [" + FROZEN_PHASE + "] phase without a corresponding [" + - SearchableSnapshotAction.NAME + "] action, but a searchable snapshot action is required in the frozen phase"); + throw new IllegalArgumentException( + "policy specifies the [" + + FROZEN_PHASE + + "] phase without a corresponding [" + + SearchableSnapshotAction.NAME + + "] action, but a searchable snapshot action is required in the frozen phase" + ); } }); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/UnfollowAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/UnfollowAction.java index 95122bb48d1e0..934e3c22a9b00 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/UnfollowAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/UnfollowAction.java @@ -53,13 +53,17 @@ public List toSteps(Client client, String phase, StepKey nextStepKey) { StepKey openFollowerIndex = new StepKey(phase, NAME, OPEN_FOLLOWER_INDEX_STEP_NAME); StepKey waitForYellowStep = new StepKey(phase, NAME, WaitForIndexColorStep.NAME); - BranchingStep conditionalSkipUnfollowStep = new BranchingStep(preUnfollowKey, indexingComplete, nextStepKey, + BranchingStep conditionalSkipUnfollowStep = new BranchingStep( + preUnfollowKey, + indexingComplete, + nextStepKey, (index, clusterState) -> { IndexMetadata followerIndex = clusterState.metadata().index(index); Map customIndexMetadata = followerIndex.getCustomData(CCR_METADATA_KEY); // if the index has no CCR metadata we'll skip the unfollow action completely return customIndexMetadata == null; - }); + } + ); WaitForIndexingCompleteStep step1 = new WaitForIndexingCompleteStep(indexingComplete, waitForFollowShardTasks); WaitForFollowShardTasksStep step2 = new WaitForFollowShardTasksStep(waitForFollowShardTasks, pauseFollowerIndex, client); PauseFollowerIndexStep step3 = new PauseFollowerIndexStep(pauseFollowerIndex, closeFollowerIndex, client); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/UnfollowFollowerIndexStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/UnfollowFollowerIndexStep.java index 09bd24f9378b2..c9ac1aca475b2 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/UnfollowFollowerIndexStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/UnfollowFollowerIndexStep.java @@ -34,27 +34,25 @@ public boolean isRetryable() { @Override void innerPerformAction(String followerIndex, ClusterState currentClusterState, ActionListener listener) { UnfollowAction.Request request = new UnfollowAction.Request(followerIndex).masterNodeTimeout(TimeValue.MAX_VALUE); - getClient().execute(UnfollowAction.INSTANCE, request, ActionListener.wrap( - r -> { - if (r.isAcknowledged() == false) { - throw new ElasticsearchException("unfollow request failed to be acknowledged"); - } + getClient().execute(UnfollowAction.INSTANCE, request, ActionListener.wrap(r -> { + if (r.isAcknowledged() == false) { + throw new ElasticsearchException("unfollow request failed to be acknowledged"); + } + listener.onResponse(null); + }, exception -> { + if (exception instanceof ElasticsearchException + && ((ElasticsearchException) exception).getMetadata("es.failed_to_remove_retention_leases") != null) { + List leasesNotRemoved = ((ElasticsearchException) exception).getMetadata("es.failed_to_remove_retention_leases"); + logger.debug( + "failed to remove leader retention lease(s) {} while unfollowing index [{}], " + "continuing with lifecycle execution", + leasesNotRemoved, + followerIndex + ); listener.onResponse(null); - }, - exception -> { - if (exception instanceof ElasticsearchException - && ((ElasticsearchException) exception).getMetadata("es.failed_to_remove_retention_leases") != null) { - List leasesNotRemoved = ((ElasticsearchException) exception) - .getMetadata("es.failed_to_remove_retention_leases"); - logger.debug("failed to remove leader retention lease(s) {} while unfollowing index [{}], " + - "continuing with lifecycle execution", - leasesNotRemoved, followerIndex); - listener.onResponse(null); - } else { - listener.onFailure(exception); - } + } else { + listener.onFailure(exception); } - )); + })); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/UpdateRolloverLifecycleDateStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/UpdateRolloverLifecycleDateStep.java index cc942fec2a453..1caad9ea5ba92 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/UpdateRolloverLifecycleDateStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/UpdateRolloverLifecycleDateStep.java @@ -57,20 +57,27 @@ public ClusterState performAction(Index index, ClusterState currentState) { final String rolloverTarget = getRolloverTarget(index, currentState); RolloverInfo rolloverInfo = indexMetadata.getRolloverInfos().get(rolloverTarget); if (rolloverInfo == null) { - throw new IllegalStateException("no rollover info found for [" + indexMetadata.getIndex().getName() + - "] with rollover target [" + rolloverTarget + "], the index has not yet rolled over with that target"); + throw new IllegalStateException( + "no rollover info found for [" + + indexMetadata.getIndex().getName() + + "] with rollover target [" + + rolloverTarget + + "], the index has not yet rolled over with that target" + ); } newIndexTime = rolloverInfo.getTime(); } - LifecycleExecutionState.Builder newLifecycleState = LifecycleExecutionState - .builder(LifecycleExecutionState.fromIndexMetadata(indexMetadata)); + LifecycleExecutionState.Builder newLifecycleState = LifecycleExecutionState.builder( + LifecycleExecutionState.fromIndexMetadata(indexMetadata) + ); newLifecycleState.setIndexCreationDate(newIndexTime); IndexMetadata.Builder newIndexMetadata = IndexMetadata.builder(indexMetadata); newIndexMetadata.putCustom(ILM_CUSTOM_METADATA_KEY, newLifecycleState.build().asMap()); - return ClusterState.builder(currentState).metadata(Metadata.builder(currentState.metadata()) - .put(newIndexMetadata).build(false)).build(); + return ClusterState.builder(currentState) + .metadata(Metadata.builder(currentState.metadata()).put(newIndexMetadata).build(false)) + .build(); } private static String getRolloverTarget(Index index, ClusterState currentState) { @@ -83,8 +90,13 @@ private static String getRolloverTarget(Index index, ClusterState currentState) IndexMetadata indexMetadata = currentState.metadata().index(index); String rolloverAlias = RolloverAction.LIFECYCLE_ROLLOVER_ALIAS_SETTING.get(indexMetadata.getSettings()); if (Strings.isNullOrEmpty(rolloverAlias)) { - throw new IllegalStateException("setting [" + RolloverAction.LIFECYCLE_ROLLOVER_ALIAS - + "] is not set on index [" + indexMetadata.getIndex().getName() + "]"); + throw new IllegalStateException( + "setting [" + + RolloverAction.LIFECYCLE_ROLLOVER_ALIAS + + "] is not set on index [" + + indexMetadata.getIndex().getName() + + "]" + ); } rolloverTarget = rolloverAlias; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/UpdateRollupIndexPolicyStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/UpdateRollupIndexPolicyStep.java index de6e599dcf5b4..029b3b649bfed 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/UpdateRollupIndexPolicyStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/UpdateRollupIndexPolicyStep.java @@ -44,20 +44,26 @@ public String getRollupPolicy() { } @Override - public void performAction(IndexMetadata indexMetadata, ClusterState currentState, - ClusterStateObserver observer, ActionListener listener) { + public void performAction( + IndexMetadata indexMetadata, + ClusterState currentState, + ClusterStateObserver observer, + ActionListener listener + ) { final String policyName = indexMetadata.getSettings().get(LifecycleSettings.LIFECYCLE_NAME); final String indexName = indexMetadata.getIndex().getName(); final LifecycleExecutionState lifecycleState = fromIndexMetadata(indexMetadata); final String rollupIndexName = lifecycleState.getRollupIndexName(); if (Strings.hasText(rollupIndexName) == false) { - listener.onFailure(new IllegalStateException("rollup index name was not generated for policy [" + policyName + - "] and index [" + indexName + "]")); + listener.onFailure( + new IllegalStateException( + "rollup index name was not generated for policy [" + policyName + "] and index [" + indexName + "]" + ) + ); return; } Settings settings = Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, rollupPolicy).build(); - UpdateSettingsRequest updateSettingsRequest = new UpdateSettingsRequest(rollupIndexName) - .masterNodeTimeout(TimeValue.MAX_VALUE) + UpdateSettingsRequest updateSettingsRequest = new UpdateSettingsRequest(rollupIndexName).masterNodeTimeout(TimeValue.MAX_VALUE) .settings(settings); getClient().admin().indices().updateSettings(updateSettingsRequest, ActionListener.wrap(response -> { if (response.isAcknowledged()) { @@ -68,7 +74,6 @@ public void performAction(IndexMetadata indexMetadata, ClusterState currentState }, listener::onFailure)); } - @Override public int hashCode() { return Objects.hash(super.hashCode(), rollupPolicy); @@ -83,7 +88,6 @@ public boolean equals(Object obj) { return false; } UpdateRollupIndexPolicyStep other = (UpdateRollupIndexPolicyStep) obj; - return super.equals(obj) && - Objects.equals(rollupPolicy, other.rollupPolicy); + return super.equals(obj) && Objects.equals(rollupPolicy, other.rollupPolicy); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/UpdateSettingsStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/UpdateSettingsStep.java index 10012b7636fb2..d5b0885d63aa0 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/UpdateSettingsStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/UpdateSettingsStep.java @@ -36,13 +36,18 @@ public boolean isRetryable() { } @Override - public void performAction(IndexMetadata indexMetadata, ClusterState currentState, - ClusterStateObserver observer, ActionListener listener) { - UpdateSettingsRequest updateSettingsRequest = new UpdateSettingsRequest(indexMetadata.getIndex().getName()) - .masterNodeTimeout(TimeValue.MAX_VALUE) - .settings(settings); - getClient().admin().indices().updateSettings(updateSettingsRequest, - ActionListener.wrap(response -> listener.onResponse(null), listener::onFailure)); + public void performAction( + IndexMetadata indexMetadata, + ClusterState currentState, + ClusterStateObserver observer, + ActionListener listener + ) { + UpdateSettingsRequest updateSettingsRequest = new UpdateSettingsRequest(indexMetadata.getIndex().getName()).masterNodeTimeout( + TimeValue.MAX_VALUE + ).settings(settings); + getClient().admin() + .indices() + .updateSettings(updateSettingsRequest, ActionListener.wrap(response -> listener.onResponse(null), listener::onFailure)); } public Settings getSettings() { @@ -63,7 +68,6 @@ public boolean equals(Object obj) { return false; } UpdateSettingsStep other = (UpdateSettingsStep) obj; - return super.equals(obj) && - Objects.equals(settings, other.settings); + return super.equals(obj) && Objects.equals(settings, other.settings); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForActiveShardsStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForActiveShardsStep.java index ce6f16e9d6928..18fa4dc334143 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForActiveShardsStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForActiveShardsStep.java @@ -7,6 +7,7 @@ package org.elasticsearch.xpack.core.ilm; import com.carrotsearch.hppc.cursors.IntObjectCursor; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.action.support.ActiveShardCount; @@ -17,11 +18,11 @@ import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.cluster.routing.IndexRoutingTable; import org.elasticsearch.cluster.routing.IndexShardRoutingTable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; +import org.elasticsearch.index.Index; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.index.Index; import java.io.IOException; import java.util.List; @@ -55,8 +56,12 @@ public Result isConditionMet(Index index, ClusterState clusterState) { IndexMetadata originalIndexMeta = metadata.index(index); if (originalIndexMeta == null) { - String errorMessage = String.format(Locale.ROOT, "[%s] lifecycle action for index [%s] executed but index no longer exists", - getKey().getAction(), index.getName()); + String errorMessage = String.format( + Locale.ROOT, + "[%s] lifecycle action for index [%s] executed but index no longer exists", + getKey().getAction(), + index.getName() + ); // Index must have been since deleted logger.debug(errorMessage); return new Result(false, new Info(errorMessage)); @@ -64,8 +69,12 @@ public Result isConditionMet(Index index, ClusterState clusterState) { boolean indexingComplete = LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE_SETTING.get(originalIndexMeta.getSettings()); if (indexingComplete) { - String message = String.format(Locale.ROOT, "index [%s] has lifecycle complete set, skipping [%s]", - originalIndexMeta.getIndex().getName(), WaitForActiveShardsStep.NAME); + String message = String.format( + Locale.ROOT, + "index [%s] has lifecycle complete set, skipping [%s]", + originalIndexMeta.getIndex().getName(), + WaitForActiveShardsStep.NAME + ); logger.trace(message); return new Result(true, new Info(message)); } @@ -80,14 +89,19 @@ public Result isConditionMet(Index index, ClusterState clusterState) { if (dataStreamAbstraction.getWriteIndex() == null) { return getErrorResultOnNullMetadata(getKey(), index); } - IndexMetadata rolledIndexMeta = metadata.index(dataStreamAbstraction.getWriteIndex() ); + IndexMetadata rolledIndexMeta = metadata.index(dataStreamAbstraction.getWriteIndex()); rolledIndexName = rolledIndexMeta.getIndex().getName(); waitForActiveShardsSettingValue = rolledIndexMeta.getSettings().get(IndexMetadata.SETTING_WAIT_FOR_ACTIVE_SHARDS.getKey()); } else { String rolloverAlias = RolloverAction.LIFECYCLE_ROLLOVER_ALIAS_SETTING.get(originalIndexMeta.getSettings()); if (Strings.isNullOrEmpty(rolloverAlias)) { - throw new IllegalStateException("setting [" + RolloverAction.LIFECYCLE_ROLLOVER_ALIAS - + "] is not set on index [" + originalIndexMeta.getIndex().getName() + "]"); + throw new IllegalStateException( + "setting [" + + RolloverAction.LIFECYCLE_ROLLOVER_ALIAS + + "] is not set on index [" + + originalIndexMeta.getIndex().getName() + + "]" + ); } IndexAbstraction aliasAbstraction = metadata.getIndicesLookup().get(rolloverAlias); @@ -129,9 +143,12 @@ public Result isConditionMet(Index index, ClusterState clusterState) { } private static Result getErrorResultOnNullMetadata(StepKey key, Index originalIndex) { - String errorMessage = String.format(Locale.ROOT, - "unable to find the index that was rolled over from [%s] as part of lifecycle action [%s]", originalIndex.getName(), - key.getAction()); + String errorMessage = String.format( + Locale.ROOT, + "unable to find the index that was rolled over from [%s] as part of lifecycle action [%s]", + originalIndex.getName(), + key.getAction() + ); // Index must have been since deleted logger.debug(errorMessage); @@ -158,8 +175,11 @@ static final class ActiveShardsInfo implements ToXContentObject { if (enoughShardsActive) { message = "the target of [" + targetActiveShardsCount + "] are active. Don't need to wait anymore"; } else { - message = "waiting for [" + targetActiveShardsCount + "] shards to become active, but only [" + currentActiveShardsCount + - "] are active"; + message = "waiting for [" + + targetActiveShardsCount + + "] shards to become active, but only [" + + currentActiveShardsCount + + "] are active"; } } @@ -183,10 +203,10 @@ public boolean equals(Object o) { return false; } ActiveShardsInfo info = (ActiveShardsInfo) o; - return currentActiveShardsCount == info.currentActiveShardsCount && - enoughShardsActive == info.enoughShardsActive && - Objects.equals(targetActiveShardsCount, info.targetActiveShardsCount) && - Objects.equals(message, info.message); + return currentActiveShardsCount == info.currentActiveShardsCount + && enoughShardsActive == info.enoughShardsActive + && Objects.equals(targetActiveShardsCount, info.targetActiveShardsCount) + && Objects.equals(message, info.message); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForDataTierStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForDataTierStep.java index 5679b50191b41..0b557e7c3e034 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForDataTierStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForDataTierStep.java @@ -33,8 +33,8 @@ public WaitForDataTierStep(StepKey key, StepKey nextStepKey, String tierPreferen @Override public Result isConditionMet(Index index, ClusterState clusterState) { - boolean present = DataTierAllocationDecider.preferredAvailableTier( - DataTier.parseTierList(tierPreference), clusterState.nodes()).isPresent(); + boolean present = DataTierAllocationDecider.preferredAvailableTier(DataTier.parseTierList(tierPreference), clusterState.nodes()) + .isPresent(); SingleMessageFieldInfo info = present ? null : new SingleMessageFieldInfo("no nodes for tiers [" + tierPreference + "] available"); return new Result(present, info); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForFollowShardTasksStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForFollowShardTasksStep.java index 48c8771f43387..a35f6ab9b894b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForFollowShardTasksStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForFollowShardTasksStep.java @@ -10,12 +10,12 @@ import org.elasticsearch.client.Client; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.Metadata; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.index.Index; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.index.Index; import org.elasticsearch.xpack.core.ccr.ShardFollowNodeTaskStatus; import org.elasticsearch.xpack.core.ccr.action.FollowStatsAction; @@ -50,9 +50,12 @@ public void evaluateCondition(Metadata metadata, Index index, Listener listener, } FollowStatsAction.StatsRequest request = new FollowStatsAction.StatsRequest(); - request.setIndices(new String[]{index.getName()}); - getClient().execute(FollowStatsAction.INSTANCE, request, - ActionListener.wrap(r -> handleResponse(r, listener), listener::onFailure)); + request.setIndices(new String[] { index.getName() }); + getClient().execute( + FollowStatsAction.INSTANCE, + request, + ActionListener.wrap(r -> handleResponse(r, listener), listener::onFailure) + ); } void handleResponse(FollowStatsAction.StatsResponses responses, Listener listener) { @@ -67,10 +70,15 @@ void handleResponse(FollowStatsAction.StatsResponses responses, Listener listene if (conditionMet) { listener.onResponse(true, null); } else { - List shardFollowTaskInfos = unSyncedShardFollowStatuses - .stream() - .map(status -> new Info.ShardFollowTaskInfo(status.followerIndex(), status.getShardId(), - status.leaderGlobalCheckpoint(), status.followerGlobalCheckpoint())) + List shardFollowTaskInfos = unSyncedShardFollowStatuses.stream() + .map( + status -> new Info.ShardFollowTaskInfo( + status.followerIndex(), + status.getShardId(), + status.leaderGlobalCheckpoint(), + status.followerGlobalCheckpoint() + ) + ) .collect(Collectors.toList()); listener.onResponse(false, new Info(shardFollowTaskInfos)); } @@ -147,7 +155,6 @@ String getFollowerIndex() { return followerIndex; } - int getShardId() { return shardId; } @@ -176,10 +183,10 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; ShardFollowTaskInfo that = (ShardFollowTaskInfo) o; - return shardId == that.shardId && - leaderGlobalCheckpoint == that.leaderGlobalCheckpoint && - followerGlobalCheckpoint == that.followerGlobalCheckpoint && - Objects.equals(followerIndex, that.followerIndex); + return shardId == that.shardId + && leaderGlobalCheckpoint == that.leaderGlobalCheckpoint + && followerGlobalCheckpoint == that.followerGlobalCheckpoint + && Objects.equals(followerIndex, that.followerIndex); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForIndexColorStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForIndexColorStep.java index 780cc6ec09823..ec00746aeb1a9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForIndexColorStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForIndexColorStep.java @@ -8,6 +8,7 @@ package org.elasticsearch.xpack.core.ilm; import com.carrotsearch.hppc.cursors.ObjectCursor; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.cluster.ClusterState; @@ -17,10 +18,10 @@ import org.elasticsearch.cluster.routing.IndexShardRoutingTable; import org.elasticsearch.cluster.routing.ShardRouting; import org.elasticsearch.core.Nullable; +import org.elasticsearch.index.Index; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.index.Index; import java.io.IOException; import java.util.Locale; @@ -80,9 +81,13 @@ public Result isConditionMet(Index index, ClusterState clusterState) { IndexMetadata indexMetadata = clusterState.metadata().index(indexName); // check if the (potentially) derived index exists if (indexMetadata == null) { - String errorMessage = String.format(Locale.ROOT, "[%s] lifecycle action for index [%s] executed but the target index [%s] " + - "does not exist", - getKey().getAction(), index.getName(), indexName); + String errorMessage = String.format( + Locale.ROOT, + "[%s] lifecycle action for index [%s] executed but the target index [%s] " + "does not exist", + getKey().getAction(), + index.getName(), + indexName + ); logger.debug(errorMessage); return new Result(false, new Info(errorMessage)); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForIndexingCompleteStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForIndexingCompleteStep.java index 2e0f938cb4ea9..06803e3d27ee5 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForIndexingCompleteStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForIndexingCompleteStep.java @@ -10,10 +10,10 @@ import org.apache.logging.log4j.Logger; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.IndexMetadata; +import org.elasticsearch.index.Index; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.index.Index; import java.io.IOException; import java.util.Map; @@ -64,8 +64,9 @@ static final class IndexingNotCompleteInfo implements ToXContentObject { private final String message; IndexingNotCompleteInfo() { - this.message = "waiting for the [" + LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE + - "] setting to be set to true on the leader index, it is currently [false]"; + this.message = "waiting for the [" + + LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE + + "] setting to be set to true on the leader index, it is currently [false]"; } String getMessage() { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForNoFollowersStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForNoFollowersStep.java index dc4ed0ee8dafc..4277426d79c91 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForNoFollowersStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForNoFollowersStep.java @@ -15,11 +15,11 @@ import org.elasticsearch.action.admin.indices.stats.ShardStats; import org.elasticsearch.client.Client; import org.elasticsearch.cluster.metadata.Metadata; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.index.Index; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.index.Index; import java.io.IOException; import java.util.Arrays; @@ -84,10 +84,10 @@ static final class Info implements ToXContentObject { static final ParseField MESSAGE_FIELD = new ParseField("message"); - private static final String message = "this index is a leader index; waiting for all following indices to cease " + - "following before proceeding"; + private static final String message = "this index is a leader index; waiting for all following indices to cease " + + "following before proceeding"; - Info() { } + Info() {} String getMessage() { return message; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForRolloverReadyStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForRolloverReadyStep.java index 564a14150a0c7..e4325c230b7e9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForRolloverReadyStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForRolloverReadyStep.java @@ -18,9 +18,9 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.index.Index; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.index.Index; import java.io.IOException; import java.util.Locale; @@ -39,8 +39,15 @@ public class WaitForRolloverReadyStep extends AsyncWaitStep { private final TimeValue maxAge; private final Long maxDocs; - public WaitForRolloverReadyStep(StepKey key, StepKey nextStepKey, Client client, - ByteSizeValue maxSize, ByteSizeValue maxPrimaryShardSize, TimeValue maxAge, Long maxDocs) { + public WaitForRolloverReadyStep( + StepKey key, + StepKey nextStepKey, + Client client, + ByteSizeValue maxSize, + ByteSizeValue maxPrimaryShardSize, + TimeValue maxAge, + Long maxDocs + ) { super(key, nextStepKey, client); this.maxSize = maxSize; this.maxPrimaryShardSize = maxPrimaryShardSize; @@ -62,9 +69,12 @@ public void evaluateCondition(Metadata metadata, Index index, Listener listener, if (dataStream != null) { assert dataStream.getWriteIndex() != null : "datastream " + dataStream.getName() + " has no write index"; if (dataStream.getWriteIndex().equals(index) == false) { - logger.warn("index [{}] is not the write index for data stream [{}]. skipping rollover for policy [{}]", - index.getName(), dataStream.getName(), - LifecycleSettings.LIFECYCLE_NAME_SETTING.get(metadata.index(index).getSettings())); + logger.warn( + "index [{}] is not the write index for data stream [{}]. skipping rollover for policy [{}]", + index.getName(), + dataStream.getName(), + LifecycleSettings.LIFECYCLE_NAME_SETTING.get(metadata.index(index).getSettings()) + ); listener.onResponse(true, EmptyInfo.INSTANCE); return; } @@ -74,15 +84,25 @@ public void evaluateCondition(Metadata metadata, Index index, Listener listener, String rolloverAlias = RolloverAction.LIFECYCLE_ROLLOVER_ALIAS_SETTING.get(indexMetadata.getSettings()); if (Strings.isNullOrEmpty(rolloverAlias)) { - listener.onFailure(new IllegalArgumentException(String.format(Locale.ROOT, - "setting [%s] for index [%s] is empty or not defined", RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, - index.getName()))); + listener.onFailure( + new IllegalArgumentException( + String.format( + Locale.ROOT, + "setting [%s] for index [%s] is empty or not defined", + RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, + index.getName() + ) + ) + ); return; } if (indexMetadata.getRolloverInfos().get(rolloverAlias) != null) { - logger.info("index [{}] was already rolled over for alias [{}], not attempting to roll over again", - index.getName(), rolloverAlias); + logger.info( + "index [{}] was already rolled over for alias [{}], not attempting to roll over again", + index.getName(), + rolloverAlias + ); listener.onResponse(true, EmptyInfo.INSTANCE); return; } @@ -96,10 +116,10 @@ public void evaluateCondition(Metadata metadata, Index index, Listener listener, Boolean isWriteIndex = null; if (aliasPointsToThisIndex) { // The writeIndex() call returns a tri-state boolean: - // true -> this index is the write index for this alias + // true -> this index is the write index for this alias // false -> this index is not the write index for this alias - // null -> this alias is a "classic-style" alias and does not have a write index configured, but only points to one index - // and is thus the write index by default + // null -> this alias is a "classic-style" alias and does not have a write index configured, but only points to one index + // and is thus the write index by default isWriteIndex = indexMetadata.getAliases().get(rolloverAlias).writeIndex(); } @@ -111,9 +131,17 @@ public void evaluateCondition(Metadata metadata, Index index, Listener listener, // If the alias doesn't point to this index, that's okay as that will be the result if this index is using a // "classic-style" alias and has already rolled over, and we want to continue with the policy. if (aliasPointsToThisIndex && Boolean.TRUE.equals(isWriteIndex)) { - listener.onFailure(new IllegalStateException(String.format(Locale.ROOT, - "index [%s] has [%s] set to [true], but is still the write index for alias [%s]", - index.getName(), LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE, rolloverAlias))); + listener.onFailure( + new IllegalStateException( + String.format( + Locale.ROOT, + "index [%s] has [%s] set to [true], but is still the write index for alias [%s]", + index.getName(), + LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE, + rolloverAlias + ) + ) + ); return; } @@ -123,16 +151,27 @@ public void evaluateCondition(Metadata metadata, Index index, Listener listener, // If indexing_complete is *not* set, and the alias does not point to this index, we can't roll over this index, so error out. if (aliasPointsToThisIndex == false) { - listener.onFailure(new IllegalArgumentException(String.format(Locale.ROOT, - "%s [%s] does not point to index [%s]", RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, rolloverAlias, - index.getName()))); + listener.onFailure( + new IllegalArgumentException( + String.format( + Locale.ROOT, + "%s [%s] does not point to index [%s]", + RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, + rolloverAlias, + index.getName() + ) + ) + ); return; } // Similarly, if isWriteIndex is false (see note above on false vs. null), we can't roll over this index, so error out. if (Boolean.FALSE.equals(isWriteIndex)) { - listener.onFailure(new IllegalArgumentException(String.format(Locale.ROOT, - "index [%s] is not the write index for alias [%s]", index.getName(), rolloverAlias))); + listener.onFailure( + new IllegalArgumentException( + String.format(Locale.ROOT, "index [%s] is not the write index for alias [%s]", index.getName(), rolloverAlias) + ) + ); return; } @@ -153,9 +192,15 @@ public void evaluateCondition(Metadata metadata, Index index, Listener listener, if (maxDocs != null) { rolloverRequest.addMaxIndexDocsCondition(maxDocs); } - getClient().admin().indices().rolloverIndex(rolloverRequest, - ActionListener.wrap(response -> listener.onResponse(response.getConditionStatus().values().stream().anyMatch(i -> i), - EmptyInfo.INSTANCE), listener::onFailure)); + getClient().admin() + .indices() + .rolloverIndex( + rolloverRequest, + ActionListener.wrap( + response -> listener.onResponse(response.getConditionStatus().values().stream().anyMatch(i -> i), EmptyInfo.INSTANCE), + listener::onFailure + ) + ); } ByteSizeValue getMaxSize() { @@ -188,11 +233,11 @@ public boolean equals(Object obj) { return false; } WaitForRolloverReadyStep other = (WaitForRolloverReadyStep) obj; - return super.equals(obj) && - Objects.equals(maxSize, other.maxSize) && - Objects.equals(maxPrimaryShardSize, other.maxPrimaryShardSize) && - Objects.equals(maxAge, other.maxAge) && - Objects.equals(maxDocs, other.maxDocs); + return super.equals(obj) + && Objects.equals(maxSize, other.maxSize) + && Objects.equals(maxPrimaryShardSize, other.maxPrimaryShardSize) + && Objects.equals(maxAge, other.maxAge) + && Objects.equals(maxDocs, other.maxDocs); } // We currently have no information to provide for this AsyncWaitStep, so this is an empty object @@ -200,8 +245,7 @@ private static final class EmptyInfo implements ToXContentObject { static final EmptyInfo INSTANCE = new EmptyInfo(); - private EmptyInfo() { - } + private EmptyInfo() {} @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForSnapshotAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForSnapshotAction.java index 2c898b55799ab..3a235f65a56bf 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForSnapshotAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForSnapshotAction.java @@ -7,11 +7,11 @@ package org.elasticsearch.xpack.core.ilm; import org.elasticsearch.client.Client; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ilm.Step.StepKey; @@ -29,8 +29,10 @@ public class WaitForSnapshotAction implements LifecycleAction { public static final String NAME = "wait_for_snapshot"; public static final ParseField POLICY_FIELD = new ParseField("policy"); - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(NAME, - a -> new WaitForSnapshotAction((String) a[0])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + a -> new WaitForSnapshotAction((String) a[0]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), POLICY_FIELD); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForSnapshotStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForSnapshotStep.java index 9ba605a8ce3b6..b11f90f9c57e7 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForSnapshotStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForSnapshotStep.java @@ -10,8 +10,8 @@ import org.apache.logging.log4j.Logger; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.IndexMetadata; -import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.index.Index; +import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xpack.core.slm.SnapshotLifecycleMetadata; import org.elasticsearch.xpack.core.slm.SnapshotLifecyclePolicyMetadata; @@ -60,8 +60,9 @@ public Result isConditionMet(Index index, ClusterState clusterState) { throw error(POLICY_NOT_FOUND_MESSAGE, policy); } SnapshotLifecyclePolicyMetadata snapPolicyMeta = snapMeta.getSnapshotConfigurations().get(policy); - if (snapPolicyMeta.getLastSuccess() == null || snapPolicyMeta.getLastSuccess().getSnapshotStartTimestamp() == null || - snapPolicyMeta.getLastSuccess().getSnapshotStartTimestamp() < actionTime) { + if (snapPolicyMeta.getLastSuccess() == null + || snapPolicyMeta.getLastSuccess().getSnapshotStartTimestamp() == null + || snapPolicyMeta.getLastSuccess().getSnapshotStartTimestamp() < actionTime) { if (snapPolicyMeta.getLastSuccess() == null) { logger.debug("skipping ILM policy execution because there is no last snapshot success, action time: {}", actionTime); } else if (snapPolicyMeta.getLastSuccess().getSnapshotStartTimestamp() == null) { @@ -70,20 +71,22 @@ public Result isConditionMet(Index index, ClusterState clusterState) { * down before this check could happen. We'll wait until a snapshot is taken on this newer master before passing this check. */ logger.debug("skipping ILM policy execution because no last snapshot start date, action time: {}", actionTime); - } - else { - logger.debug("skipping ILM policy execution because snapshot start time {} is before action time {}, snapshot timestamp " + - "is {}", + } else { + logger.debug( + "skipping ILM policy execution because snapshot start time {} is before action time {}, snapshot timestamp " + "is {}", snapPolicyMeta.getLastSuccess().getSnapshotStartTimestamp(), actionTime, - snapPolicyMeta.getLastSuccess().getSnapshotFinishTimestamp()); + snapPolicyMeta.getLastSuccess().getSnapshotFinishTimestamp() + ); } return new Result(false, notExecutedMessage(actionTime)); } - logger.debug("executing policy because snapshot start time {} is after action time {}, snapshot timestamp is {}", + logger.debug( + "executing policy because snapshot start time {} is after action time {}, snapshot timestamp is {}", snapPolicyMeta.getLastSuccess().getSnapshotStartTimestamp(), actionTime, - snapPolicyMeta.getLastSuccess().getSnapshotFinishTimestamp()); + snapPolicyMeta.getLastSuccess().getSnapshotFinishTimestamp() + ); return new Result(true, null); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/action/DeleteLifecycleAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/action/DeleteLifecycleAction.java index 0bb605edb7883..623c9797ffde1 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/action/DeleteLifecycleAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/action/DeleteLifecycleAction.java @@ -11,9 +11,9 @@ import org.elasticsearch.action.ActionType; import org.elasticsearch.action.support.master.AcknowledgedRequest; import org.elasticsearch.action.support.master.AcknowledgedResponse; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.xcontent.ParseField; import java.io.IOException; import java.util.Objects; @@ -41,8 +41,7 @@ public Request(StreamInput in) throws IOException { policyName = in.readString(); } - public Request() { - } + public Request() {} public String getPolicyName() { return policyName; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/action/GetLifecycleAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/action/GetLifecycleAction.java index 8cf6d2de1030d..fb082b75212e9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/action/GetLifecycleAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/action/GetLifecycleAction.java @@ -205,10 +205,10 @@ public boolean equals(Object obj) { return false; } LifecyclePolicyResponseItem other = (LifecyclePolicyResponseItem) obj; - return Objects.equals(lifecyclePolicy, other.lifecyclePolicy) && - Objects.equals(version, other.version) && - Objects.equals(modifiedDate, other.modifiedDate) && - Objects.equals(usage, other.usage); + return Objects.equals(lifecyclePolicy, other.lifecyclePolicy) + && Objects.equals(version, other.version) + && Objects.equals(modifiedDate, other.modifiedDate) + && Objects.equals(usage, other.usage); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/action/GetStatusAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/action/GetStatusAction.java index 66f40b84b2876..5f16b88e212ef 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/action/GetStatusAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/action/GetStatusAction.java @@ -89,8 +89,7 @@ public Request(StreamInput in) throws IOException { super(in); } - public Request() { - } + public Request() {} @Override public ActionRequestValidationException validate() { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/action/MoveToStepAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/action/MoveToStepAction.java index f0d08b4805938..4c45e052f7457 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/action/MoveToStepAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/action/MoveToStepAction.java @@ -15,12 +15,12 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.core.Nullable; import org.elasticsearch.xpack.core.ilm.Step.StepKey; import java.io.IOException; @@ -37,13 +37,15 @@ protected MoveToStepAction() { public static class Request extends AcknowledgedRequest implements ToXContentObject { static final ParseField CURRENT_KEY_FIELD = new ParseField("current_step"); static final ParseField NEXT_KEY_FIELD = new ParseField("next_step"); - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("move_to_step_request", false, - (a, index) -> { - StepKey currentStepKey = (StepKey) a[0]; - PartialStepKey nextStepKey = (PartialStepKey) a[1]; - return new Request(index, currentStepKey, nextStepKey); - }); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "move_to_step_request", + false, + (a, index) -> { + StepKey currentStepKey = (StepKey) a[0]; + PartialStepKey nextStepKey = (PartialStepKey) a[1]; + return new Request(index, currentStepKey, nextStepKey); + } + ); static { // The current step uses the strict parser (meaning it requires all three parts of a stepkey) @@ -69,8 +71,7 @@ public Request(StreamInput in) throws IOException { this.nextStepKey = new PartialStepKey(in); } - public Request() { - } + public Request() {} public String getIndex() { return index; @@ -115,7 +116,8 @@ public boolean equals(Object obj) { return false; } Request other = (Request) obj; - return Objects.equals(index, other.index) && Objects.equals(currentStepKey, other.currentStepKey) + return Objects.equals(index, other.index) + && Objects.equals(currentStepKey, other.currentStepKey) && Objects.equals(nextStepKey, other.nextStepKey); } @@ -143,9 +145,10 @@ public static class PartialStepKey implements Writeable, ToXContentObject { public static final ParseField PHASE_FIELD = new ParseField("phase"); public static final ParseField ACTION_FIELD = new ParseField("action"); public static final ParseField NAME_FIELD = new ParseField("name"); - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("step_specification", - a -> new PartialStepKey((String) a[0], (String) a[1], (String) a[2])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "step_specification", + a -> new PartialStepKey((String) a[0], (String) a[1], (String) a[2]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), PHASE_FIELD); PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), ACTION_FIELD); @@ -157,8 +160,10 @@ public PartialStepKey(String phase, @Nullable String action, @Nullable String na this.action = action; this.name = name; if (name != null && action == null) { - throw new IllegalArgumentException("phase; phase and action; or phase, action, and step must be provided, " + - "but a step name was specified without a corresponding action"); + throw new IllegalArgumentException( + "phase; phase and action; or phase, action, and step must be provided, " + + "but a step name was specified without a corresponding action" + ); } } @@ -167,8 +172,10 @@ public PartialStepKey(StreamInput in) throws IOException { this.action = in.readOptionalString(); this.name = in.readOptionalString(); if (name != null && action == null) { - throw new IllegalArgumentException("phase; phase and action; or phase, action, and step must be provided, " + - "but a step name was specified without a corresponding action"); + throw new IllegalArgumentException( + "phase; phase and action; or phase, action, and step must be provided, " + + "but a step name was specified without a corresponding action" + ); } } @@ -212,9 +219,7 @@ public boolean equals(Object obj) { return false; } PartialStepKey other = (PartialStepKey) obj; - return Objects.equals(phase, other.phase) && - Objects.equals(action, other.action) && - Objects.equals(name, other.name); + return Objects.equals(phase, other.phase) && Objects.equals(action, other.action) && Objects.equals(name, other.name); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/action/PutLifecycleAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/action/PutLifecycleAction.java index 8bd3362f7e094..bee317085815a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/action/PutLifecycleAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/action/PutLifecycleAction.java @@ -10,11 +10,11 @@ import org.elasticsearch.action.ActionType; import org.elasticsearch.action.support.master.AcknowledgedRequest; import org.elasticsearch.action.support.master.AcknowledgedResponse; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -35,8 +35,10 @@ protected PutLifecycleAction() { public static class Request extends AcknowledgedRequest implements ToXContentObject { public static final ParseField POLICY_FIELD = new ParseField("policy"); - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("put_lifecycle_request", a -> new Request((LifecyclePolicy) a[0])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "put_lifecycle_request", + a -> new Request((LifecyclePolicy) a[0]) + ); static { PARSER.declareObject(ConstructingObjectParser.constructorArg(), LifecyclePolicy::parse, POLICY_FIELD); } @@ -52,8 +54,7 @@ public Request(StreamInput in) throws IOException { policy = new LifecyclePolicy(in); } - public Request() { - } + public Request() {} public LifecyclePolicy getPolicy() { return policy; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/action/RemoveIndexLifecyclePolicyAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/action/RemoveIndexLifecyclePolicyAction.java index e65a998b06a43..b3cda42b57561 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/action/RemoveIndexLifecyclePolicyAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/action/RemoveIndexLifecyclePolicyAction.java @@ -9,14 +9,14 @@ import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.ActionResponse; -import org.elasticsearch.action.IndicesRequest; import org.elasticsearch.action.ActionType; +import org.elasticsearch.action.IndicesRequest; import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.action.support.master.AcknowledgedRequest; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -39,7 +39,9 @@ public static class Response extends ActionResponse implements ToXContentObject public static final ParseField FAILED_INDEXES_FIELD = new ParseField("failed_indexes"); @SuppressWarnings("unchecked") public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "change_policy_for_index_response", a -> new Response((List) a[0])); + "change_policy_for_index_response", + a -> new Response((List) a[0]) + ); static { PARSER.declareStringArray(ConstructingObjectParser.constructorArg(), FAILED_INDEXES_FIELD); // Needs to be declared but not used in constructing the response object @@ -112,8 +114,7 @@ public Request(StreamInput in) throws IOException { indicesOptions = IndicesOptions.readIndicesOptions(in); } - public Request() { - } + public Request() {} public Request(String... indices) { if (indices == null) { @@ -167,8 +168,7 @@ public boolean equals(Object obj) { return false; } Request other = (Request) obj; - return Objects.deepEquals(indices, other.indices) && - Objects.equals(indicesOptions, other.indicesOptions); + return Objects.deepEquals(indices, other.indices) && Objects.equals(indicesOptions, other.indicesOptions); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/action/RetryAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/action/RetryAction.java index ade8169ad7175..5e20a17f31c9e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/action/RetryAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/action/RetryAction.java @@ -43,8 +43,7 @@ public Request(StreamInput in) throws IOException { this.indicesOptions = IndicesOptions.readIndicesOptions(in); } - public Request() { - } + public Request() {} @Override public Request indices(String... indices) { @@ -93,8 +92,7 @@ public boolean equals(Object obj) { return false; } Request other = (Request) obj; - return Objects.deepEquals(indices, other.indices) - && Objects.equals(indicesOptions, other.indicesOptions); + return Objects.deepEquals(indices, other.indices) && Objects.equals(indicesOptions, other.indicesOptions); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/step/info/AllocationInfo.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/step/info/AllocationInfo.java index 77bd83de2e018..5732f5e72a42f 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/step/info/AllocationInfo.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/step/info/AllocationInfo.java @@ -7,9 +7,9 @@ package org.elasticsearch.xpack.core.ilm.step.info; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -32,8 +32,10 @@ public class AllocationInfo implements ToXContentObject { static final ParseField SHARDS_TO_ALLOCATE = new ParseField("shards_left_to_allocate"); static final ParseField ALL_SHARDS_ACTIVE = new ParseField("all_shards_active"); static final ParseField MESSAGE = new ParseField("message"); - static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("allocation_routed_step_info", - a -> new AllocationInfo((long) a[0], (long) a[1], (boolean) a[2], (String) a[3])); + static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "allocation_routed_step_info", + a -> new AllocationInfo((long) a[0], (long) a[1], (boolean) a[2], (String) a[3]) + ); static { PARSER.declareLong(ConstructingObjectParser.constructorArg(), NUMBER_OF_REPLICAS); @@ -54,8 +56,7 @@ public AllocationInfo(long numberOfReplicas, long numberShardsLeftToAllocate, bo * particular index. */ public static AllocationInfo waitingForActiveShardsAllocationInfo(long numReplicas) { - return new AllocationInfo(numReplicas, -1, false, - "Waiting for all shard copies to be active"); + return new AllocationInfo(numReplicas, -1, false, "Waiting for all shard copies to be active"); } /** @@ -63,8 +64,12 @@ public static AllocationInfo waitingForActiveShardsAllocationInfo(long numReplic * but there are still {@link #numberShardsLeftToAllocate} left to be allocated. */ public static AllocationInfo allShardsActiveAllocationInfo(long numReplicas, long numberShardsLeftToAllocate) { - return new AllocationInfo(numReplicas, numberShardsLeftToAllocate, true, "Waiting for [" + numberShardsLeftToAllocate + - "] shards to be allocated to nodes matching the given filters"); + return new AllocationInfo( + numReplicas, + numberShardsLeftToAllocate, + true, + "Waiting for [" + numberShardsLeftToAllocate + "] shards to be allocated to nodes matching the given filters" + ); } public long getNumberOfReplicas() { @@ -108,10 +113,10 @@ public boolean equals(Object obj) { return false; } AllocationInfo other = (AllocationInfo) obj; - return Objects.equals(numberOfReplicas, other.numberOfReplicas) && - Objects.equals(numberShardsLeftToAllocate, other.numberShardsLeftToAllocate) && - Objects.equals(message, other.message) && - Objects.equals(allShardsActive, other.allShardsActive); + return Objects.equals(numberOfReplicas, other.numberOfReplicas) + && Objects.equals(numberShardsLeftToAllocate, other.numberShardsLeftToAllocate) + && Objects.equals(message, other.message) + && Objects.equals(allShardsActive, other.allShardsActive); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/index/query/PinnedQueryBuilder.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/index/query/PinnedQueryBuilder.java index eb09b99cdd51f..717ffa68e8dfe 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/index/query/PinnedQueryBuilder.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/index/query/PinnedQueryBuilder.java @@ -7,15 +7,15 @@ package org.elasticsearch.xpack.core.index.query; import org.apache.lucene.search.Query; +import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.regex.Regex; -import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.xcontent.ToXContentObject; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.index.query.AbstractQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.SearchExecutionContext; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.ToXContentObject; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.Arrays; @@ -93,21 +93,20 @@ public PinnedQueryBuilder(QueryBuilder organicQuery, Item... docs) { * Creates a new PinnedQueryBuilder */ private PinnedQueryBuilder(QueryBuilder organicQuery, List ids, List docs) { - if (organicQuery == null) { - throw new IllegalArgumentException("[" + NAME + "] organicQuery cannot be null"); - } - this.organicQuery = organicQuery; - if (ids == null && docs == null) { - throw new IllegalArgumentException("[" + NAME + "] ids and docs cannot both be null"); - } - if (ids != null && docs != null) { - throw new IllegalArgumentException("[" + NAME + "] ids and docs cannot both be used"); - } - this.ids = ids; - this.docs = docs; + if (organicQuery == null) { + throw new IllegalArgumentException("[" + NAME + "] organicQuery cannot be null"); + } + this.organicQuery = organicQuery; + if (ids == null && docs == null) { + throw new IllegalArgumentException("[" + NAME + "] ids and docs cannot both be null"); + } + if (ids != null && docs != null) { + throw new IllegalArgumentException("[" + NAME + "] ids and docs cannot both be used"); + } + this.ids = ids; + this.docs = docs; } - @Override protected void doWriteTo(StreamOutput out) throws IOException { out.writeOptionalStringCollection(this.ids); @@ -147,7 +146,6 @@ public List docs() { return Collections.unmodifiableList(this.docs); } - @Override protected void doXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(NAME); @@ -178,7 +176,6 @@ protected Query doToQuery(SearchExecutionContext context) throws IOException { throw new UnsupportedOperationException("Client side-only class for use in HLRC"); } - @Override protected int doHashCode() { return Objects.hash(ids, docs, organicQuery); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexing/AsyncTwoPhaseIndexer.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexing/AsyncTwoPhaseIndexer.java index f5718c5911209..63c1d3e694ab6 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexing/AsyncTwoPhaseIndexer.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexing/AsyncTwoPhaseIndexer.java @@ -192,46 +192,54 @@ public synchronized boolean abort() { public synchronized boolean maybeTriggerAsyncJob(long now) { final IndexerState currentState = state.get(); switch (currentState) { - case INDEXING: - case STOPPING: - case ABORTING: - logger.warn("Schedule was triggered for job [" + getJobId() + "], but prior indexer is still running " + - "(with state [" + currentState + "]"); - return false; - - case STOPPED: - logger.debug("Schedule was triggered for job [" + getJobId() + "] but job is stopped. Ignoring trigger."); - return false; - - case STARTED: - logger.debug("Schedule was triggered for job [" + getJobId() + "], state: [" + currentState + "]"); - stats.incrementNumInvocations(1); - - if (state.compareAndSet(IndexerState.STARTED, IndexerState.INDEXING)) { - // fire off the search. Note this is async, the method will return from here - threadPool.executor(ThreadPool.Names.GENERIC).execute(() -> { - onStart(now, ActionListener.wrap(r -> { - assert r != null; - if (r) { - nextSearch(); - } else { - onFinish(ActionListener.wrap( - onFinishResponse -> doSaveState(finishAndSetState(), position.get(), this::afterFinishOrFailure), - onFinishFailure -> doSaveState(finishAndSetState(), position.get(), this::afterFinishOrFailure))); - } - }, - this::finishWithFailure)); - }); - logger.debug("Beginning to index [" + getJobId() + "], state: [" + currentState + "]"); - return true; - } else { - logger.debug("Could not move from STARTED to INDEXING state because current state is [" + state.get() + "]"); + case INDEXING: + case STOPPING: + case ABORTING: + logger.warn( + "Schedule was triggered for job [" + + getJobId() + + "], but prior indexer is still running " + + "(with state [" + + currentState + + "]" + ); return false; - } - default: - logger.warn("Encountered unexpected state [" + currentState + "] while indexing"); - throw new IllegalStateException("Job encountered an illegal state [" + currentState + "]"); + case STOPPED: + logger.debug("Schedule was triggered for job [" + getJobId() + "] but job is stopped. Ignoring trigger."); + return false; + + case STARTED: + logger.debug("Schedule was triggered for job [" + getJobId() + "], state: [" + currentState + "]"); + stats.incrementNumInvocations(1); + + if (state.compareAndSet(IndexerState.STARTED, IndexerState.INDEXING)) { + // fire off the search. Note this is async, the method will return from here + threadPool.executor(ThreadPool.Names.GENERIC).execute(() -> { + onStart(now, ActionListener.wrap(r -> { + assert r != null; + if (r) { + nextSearch(); + } else { + onFinish( + ActionListener.wrap( + onFinishResponse -> doSaveState(finishAndSetState(), position.get(), this::afterFinishOrFailure), + onFinishFailure -> doSaveState(finishAndSetState(), position.get(), this::afterFinishOrFailure) + ) + ); + } + }, this::finishWithFailure)); + }); + logger.debug("Beginning to index [" + getJobId() + "], state: [" + currentState + "]"); + return true; + } else { + logger.debug("Could not move from STARTED to INDEXING state because current state is [" + state.get() + "]"); + return false; + } + + default: + logger.warn("Encountered unexpected state [" + currentState + "] while indexing"); + throw new IllegalStateException("Job encountered an illegal state [" + currentState + "]"); } } @@ -377,16 +385,14 @@ protected float getMaxDocsPerSecond() { /** * Called after onFinish or after onFailure and all the following steps - in particular state persistence - are completed. */ - protected void afterFinishOrFailure() { - } + protected void afterFinishOrFailure() {} /** * Called when the indexer is stopped. This is only called when the indexer is stopped * via {@link #stop()} as opposed to {@link #onFinish(ActionListener)} which is called * when the indexer's work is done. */ - protected void onStop() { - } + protected void onStop() {} /** * Called when a background job detects that the indexer is aborted causing the @@ -419,30 +425,30 @@ private IndexerState finishAndSetState() { callOnAbort.set(false); callOnStop.set(false); switch (prev) { - case INDEXING: - // ready for another job - return IndexerState.STARTED; - - case STOPPING: - callOnStop.set(true); - // must be started again - return IndexerState.STOPPED; - - case ABORTING: - callOnAbort.set(true); - // abort and exit - return IndexerState.ABORTING; // This shouldn't matter, since onAbort() will kill the task first - - case STOPPED: - // No-op. Shouldn't really be possible to get here (should have to go through - // STOPPING - // first which will be handled) but is harmless to no-op and we don't want to - // throw exception here - return IndexerState.STOPPED; - - default: - // any other state is unanticipated at this point - throw new IllegalStateException("Indexer job encountered an illegal state [" + prev + "]"); + case INDEXING: + // ready for another job + return IndexerState.STARTED; + + case STOPPING: + callOnStop.set(true); + // must be started again + return IndexerState.STOPPED; + + case ABORTING: + callOnAbort.set(true); + // abort and exit + return IndexerState.ABORTING; // This shouldn't matter, since onAbort() will kill the task first + + case STOPPED: + // No-op. Shouldn't really be possible to get here (should have to go through + // STOPPING + // first which will be handled) but is harmless to no-op and we don't want to + // throw exception here + return IndexerState.STOPPED; + + default: + // any other state is unanticipated at this point + throw new IllegalStateException("Indexer job encountered an illegal state [" + prev + "]"); } }); @@ -466,9 +472,12 @@ private void onSearchResponse(SearchResponse searchResponse) { if (searchResponse == null) { logger.debug("No indexing necessary for job [{}], saving state and shutting down.", getJobId()); // execute finishing tasks - onFinish(ActionListener.wrap( - r -> doSaveState(finishAndSetState(), position.get(), this::afterFinishOrFailure), - e -> doSaveState(finishAndSetState(), position.get(), this::afterFinishOrFailure))); + onFinish( + ActionListener.wrap( + r -> doSaveState(finishAndSetState(), position.get(), this::afterFinishOrFailure), + e -> doSaveState(finishAndSetState(), position.get(), this::afterFinishOrFailure) + ) + ); return; } @@ -489,9 +498,12 @@ private void onSearchResponse(SearchResponse searchResponse) { position.set(iterationResult.getPosition()); stats.markEndProcessing(); // execute finishing tasks - onFinish(ActionListener.wrap( + onFinish( + ActionListener.wrap( r -> doSaveState(finishAndSetState(), position.get(), this::afterFinishOrFailure), - e -> doSaveState(finishAndSetState(), position.get(), this::afterFinishOrFailure))); + e -> doSaveState(finishAndSetState(), position.get(), this::afterFinishOrFailure) + ) + ); return; } @@ -568,18 +580,8 @@ protected void nextSearch() { ); if (executionDelay.duration() > 0) { - logger.debug( - "throttling job [{}], wait for {} ({} {})", - getJobId(), - executionDelay, - currentMaxDocsPerSecond, - lastDocCount - ); - scheduledNextSearch = new ScheduledRunnable( - threadPool, - executionDelay, - () -> triggerNextSearch(executionDelay.getNanos()) - ); + logger.debug("throttling job [{}], wait for {} ({} {})", getJobId(), executionDelay, currentMaxDocsPerSecond, lastDocCount); + scheduledNextSearch = new ScheduledRunnable(threadPool, executionDelay, () -> triggerNextSearch(executionDelay.getNanos())); // corner case: if meanwhile stop() has been called or state persistence has been requested: fast forward, run search now if (getState().equals(IndexerState.STOPPING) || triggerSaveState()) { @@ -611,27 +613,27 @@ private void triggerNextSearch(long waitTimeInNanos) { */ private boolean checkState(IndexerState currentState) { switch (currentState) { - case INDEXING: - // normal state; - return true; + case INDEXING: + // normal state; + return true; - case STOPPING: - logger.info("Indexer job encountered [" + IndexerState.STOPPING + "] state, halting indexer."); - doSaveState(finishAndSetState(), getPosition(), this::afterFinishOrFailure); - return false; + case STOPPING: + logger.info("Indexer job encountered [" + IndexerState.STOPPING + "] state, halting indexer."); + doSaveState(finishAndSetState(), getPosition(), this::afterFinishOrFailure); + return false; - case STOPPED: - return false; + case STOPPED: + return false; - case ABORTING: - logger.info("Requested shutdown of indexer for job [" + getJobId() + "]"); - onAbort(); - return false; + case ABORTING: + logger.info("Requested shutdown of indexer for job [" + getJobId() + "]"); + onAbort(); + return false; - default: - // Anything other than indexing, aborting or stopping is unanticipated - logger.warn("Encountered unexpected state [" + currentState + "] while indexing"); - throw new IllegalStateException("Indexer job encountered an illegal state [" + currentState + "]"); + default: + // Anything other than indexing, aborting or stopping is unanticipated + logger.warn("Encountered unexpected state [" + currentState + "] while indexing"); + throw new IllegalStateException("Indexer job encountered an illegal state [" + currentState + "]"); } } @@ -647,11 +649,7 @@ private synchronized void reQueueThrottledSearch() { getTimeNanos() ); - logger.trace( - "[{}] rethrottling job, wait {} until next search", - getJobId(), - executionDelay - ); + logger.trace("[{}] rethrottling job, wait {} until next search", getJobId(), executionDelay); runnable.reschedule(executionDelay); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexing/IndexerJobStats.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexing/IndexerJobStats.java index fff4474d98f3e..b3020dddd5aaa 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexing/IndexerJobStats.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexing/IndexerJobStats.java @@ -7,10 +7,10 @@ package org.elasticsearch.xpack.core.indexing; import org.elasticsearch.Version; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import java.io.IOException; @@ -42,12 +42,22 @@ public abstract class IndexerJobStats implements ToXContentObject, Writeable { private long startSearchTime; private long startProcessingTime; - public IndexerJobStats() { - } - - public IndexerJobStats(long numPages, long numInputDocuments, long numOuputDocuments, long numInvocations, - long indexTime, long searchTime, long processingTime, long indexTotal, long searchTotal, - long processingTotal, long indexFailures, long searchFailures) { + public IndexerJobStats() {} + + public IndexerJobStats( + long numPages, + long numInputDocuments, + long numOuputDocuments, + long numInvocations, + long indexTime, + long searchTime, + long processingTime, + long indexTotal, + long searchTotal, + long processingTotal, + long indexFailures, + long searchFailures + ) { this.numPages = numPages; this.numInputDocuments = numInputDocuments; this.numOuputDocuments = numOuputDocuments; @@ -129,22 +139,22 @@ public long getProcessingTotal() { } public void incrementNumPages(long n) { - assert(n >= 0); + assert (n >= 0); numPages += n; } public void incrementNumDocuments(long n) { - assert(n >= 0); + assert (n >= 0); numInputDocuments += n; } public void incrementNumInvocations(long n) { - assert(n >= 0); + assert (n >= 0); numInvocations += n; } public void incrementNumOutputDocuments(long n) { - assert(n >= 0); + assert (n >= 0); numOuputDocuments += n; } @@ -229,7 +239,19 @@ public boolean equals(Object other) { @Override public int hashCode() { - return Objects.hash(numPages, numInputDocuments, numOuputDocuments, numInvocations, - indexTime, searchTime, processingTime, indexFailures, searchFailures, indexTotal, searchTotal, processingTotal); + return Objects.hash( + numPages, + numInputDocuments, + numOuputDocuments, + numInvocations, + indexTime, + searchTime, + processingTime, + indexFailures, + searchFailures, + indexTotal, + searchTotal, + processingTotal + ); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexing/IndexerState.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexing/IndexerState.java index 740123faa326f..e8f7f9bbd797e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexing/IndexerState.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexing/IndexerState.java @@ -6,10 +6,10 @@ */ package org.elasticsearch.xpack.core.indexing; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.xcontent.ParseField; import java.io.IOException; import java.util.Locale; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/MachineLearningFeatureSetUsage.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/MachineLearningFeatureSetUsage.java index c2e40246451e1..4431cb4c53322 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/MachineLearningFeatureSetUsage.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/MachineLearningFeatureSetUsage.java @@ -37,13 +37,15 @@ public class MachineLearningFeatureSetUsage extends XPackFeatureSet.Usage { private final Map inferenceUsage; private final int nodeCount; - public MachineLearningFeatureSetUsage(boolean available, - boolean enabled, - Map jobsUsage, - Map datafeedsUsage, - Map analyticsUsage, - Map inferenceUsage, - int nodeCount) { + public MachineLearningFeatureSetUsage( + boolean available, + boolean enabled, + Map jobsUsage, + Map datafeedsUsage, + Map analyticsUsage, + Map inferenceUsage, + int nodeCount + ) { super(XPackField.MACHINE_LEARNING, available, enabled); this.jobsUsage = Objects.requireNonNull(jobsUsage); this.datafeedsUsage = Objects.requireNonNull(datafeedsUsage); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/MachineLearningField.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/MachineLearningField.java index 3438c03956806..128f6315994a9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/MachineLearningField.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/MachineLearningField.java @@ -21,11 +21,17 @@ import java.util.stream.Collectors; public final class MachineLearningField { - public static final Setting AUTODETECT_PROCESS = - Setting.boolSetting("xpack.ml.autodetect_process", true, Setting.Property.NodeScope); - public static final Setting MAX_MODEL_MEMORY_LIMIT = - Setting.memorySizeSetting("xpack.ml.max_model_memory_limit", ByteSizeValue.ZERO, - Setting.Property.Dynamic, Setting.Property.NodeScope); + public static final Setting AUTODETECT_PROCESS = Setting.boolSetting( + "xpack.ml.autodetect_process", + true, + Setting.Property.NodeScope + ); + public static final Setting MAX_MODEL_MEMORY_LIMIT = Setting.memorySizeSetting( + "xpack.ml.max_model_memory_limit", + ByteSizeValue.ZERO, + Setting.Property.Dynamic, + Setting.Property.NodeScope + ); public static final TimeValue STATE_PERSIST_RESTORE_TIMEOUT = TimeValue.timeValueMinutes(30); public static final String ML_FEATURE_FAMILY = "machine-learning"; public static final LicensedFeature.Momentary ML_API_FEATURE = LicensedFeature.momentary( diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/MlConfigIndex.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/MlConfigIndex.java index 1bcc40ae6d945..8e9382a65983e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/MlConfigIndex.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/MlConfigIndex.java @@ -32,7 +32,8 @@ public static String mapping() { return TemplateUtils.loadTemplate( "/org/elasticsearch/xpack/core/ml/config_index_mappings.json", Version.CURRENT.toString(), - MAPPINGS_VERSION_VARIABLE); + MAPPINGS_VERSION_VARIABLE + ); } public static Settings settings() { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/MlMetaIndex.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/MlMetaIndex.java index 547a0553e3fc8..d09db7ece0363 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/MlMetaIndex.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/MlMetaIndex.java @@ -30,7 +30,8 @@ public static String mapping() { return TemplateUtils.loadTemplate( "/org/elasticsearch/xpack/core/ml/meta_index_mappings.json", Version.CURRENT.toString(), - MAPPINGS_VERSION_VARIABLE); + MAPPINGS_VERSION_VARIABLE + ); } public static Settings settings() { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/MlMetadata.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/MlMetadata.java index 2a0f6d3d02bb8..ed605f4fea43c 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/MlMetadata.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/MlMetadata.java @@ -13,14 +13,14 @@ import org.elasticsearch.cluster.DiffableUtils; import org.elasticsearch.cluster.NamedDiff; import org.elasticsearch.cluster.metadata.Metadata; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfig; @@ -65,8 +65,11 @@ public class MlMetadata implements Metadata.Custom { static { LENIENT_PARSER.declareObjectArray(Builder::putJobs, (p, c) -> Job.LENIENT_PARSER.apply(p, c).build(), JOBS_FIELD); - LENIENT_PARSER.declareObjectArray(Builder::putDatafeeds, - (p, c) -> DatafeedConfig.LENIENT_PARSER.apply(p, c).build(), DATAFEEDS_FIELD); + LENIENT_PARSER.declareObjectArray( + Builder::putDatafeeds, + (p, c) -> DatafeedConfig.LENIENT_PARSER.apply(p, c).build(), + DATAFEEDS_FIELD + ); LENIENT_PARSER.declareBoolean(Builder::isUpgradeMode, UPGRADE_MODE); LENIENT_PARSER.declareBoolean(Builder::isResetMode, RESET_MODE); } @@ -113,8 +116,7 @@ public Map getDatafeedsByJobIds(Set jobIds) { } public Set expandDatafeedIds(String expression, boolean allowNoMatch) { - return NameResolver.newUnaliased(datafeeds.keySet(), ExceptionsHelper::missingDatafeedException) - .expand(expression, allowNoMatch); + return NameResolver.newUnaliased(datafeeds.keySet(), ExceptionsHelper::missingDatafeedException).expand(expression, allowNoMatch); } public boolean isUpgradeMode() { @@ -187,8 +189,10 @@ private static void writeMap(Map map, StreamOut @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - DelegatingMapParams extendedParams = - new DelegatingMapParams(Collections.singletonMap(ToXContentParams.FOR_INTERNAL_STORAGE, "true"), params); + DelegatingMapParams extendedParams = new DelegatingMapParams( + Collections.singletonMap(ToXContentParams.FOR_INTERNAL_STORAGE, "true"), + params + ); mapValuesToXContent(JOBS_FIELD, jobs, builder, extendedParams); mapValuesToXContent(DATAFEEDS_FIELD, datafeeds, builder, extendedParams); builder.field(UPGRADE_MODE.getPreferredName(), upgradeMode); @@ -196,8 +200,12 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws return builder; } - private static void mapValuesToXContent(ParseField field, Map map, XContentBuilder builder, - Params params) throws IOException { + private static void mapValuesToXContent( + ParseField field, + Map map, + XContentBuilder builder, + Params params + ) throws IOException { if (map.isEmpty()) { return; } @@ -224,10 +232,13 @@ public static class MlMetadataDiff implements NamedDiff { } public MlMetadataDiff(StreamInput in) throws IOException { - this.jobs = DiffableUtils.readJdkMapDiff(in, DiffableUtils.getStringKeySerializer(), Job::new, - MlMetadataDiff::readJobDiffFrom); - this.datafeeds = DiffableUtils.readJdkMapDiff(in, DiffableUtils.getStringKeySerializer(), DatafeedConfig::new, - MlMetadataDiff::readDatafeedDiffFrom); + this.jobs = DiffableUtils.readJdkMapDiff(in, DiffableUtils.getStringKeySerializer(), Job::new, MlMetadataDiff::readJobDiffFrom); + this.datafeeds = DiffableUtils.readJdkMapDiff( + in, + DiffableUtils.getStringKeySerializer(), + DatafeedConfig::new, + MlMetadataDiff::readDatafeedDiffFrom + ); upgradeMode = in.readBoolean(); if (in.getVersion().onOrAfter(Version.V_8_0_0)) { resetMode = in.readBoolean(); @@ -274,15 +285,13 @@ static Diff readDatafeedDiffFrom(StreamInput in) throws IOExcept @Override public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; MlMetadata that = (MlMetadata) o; - return Objects.equals(jobs, that.jobs) && - Objects.equals(datafeeds, that.datafeeds) && - upgradeMode == that.upgradeMode && - resetMode == that.resetMode; + return Objects.equals(jobs, that.jobs) + && Objects.equals(datafeeds, that.datafeeds) + && upgradeMode == that.upgradeMode + && resetMode == that.resetMode; } @Override @@ -350,9 +359,7 @@ public Builder putDatafeed(DatafeedConfig datafeedConfig, Map he if (headers.isEmpty() == false) { // Adjust the request, adding security headers from the current thread context - datafeedConfig = new DatafeedConfig.Builder(datafeedConfig) - .setHeaders(filterSecurityHeaders(headers)) - .build(); + datafeedConfig = new DatafeedConfig.Builder(datafeedConfig).setHeaders(filterSecurityHeaders(headers)).build(); } datafeeds.put(datafeedConfig.getId(), datafeedConfig); @@ -366,8 +373,9 @@ private void checkJobIsAvailableForDatafeed(String jobId) { } Optional existingDatafeed = getDatafeedByJobId(jobId); if (existingDatafeed.isPresent()) { - throw ExceptionsHelper.conflictStatusException("A datafeed [" + existingDatafeed.get().getId() - + "] already exists for job [" + jobId + "]"); + throw ExceptionsHelper.conflictStatusException( + "A datafeed [" + existingDatafeed.get().getId() + "] already exists for job [" + jobId + "]" + ); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/MlStatsIndex.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/MlStatsIndex.java index fa51f7336b39a..f7a24ccaca448 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/MlStatsIndex.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/MlStatsIndex.java @@ -31,8 +31,11 @@ public static String wrappedMapping() { } public static String mapping() { - return TemplateUtils.loadTemplate("/org/elasticsearch/xpack/core/ml/stats_index_mappings.json", - Version.CURRENT.toString(), MAPPINGS_VERSION_VARIABLE); + return TemplateUtils.loadTemplate( + "/org/elasticsearch/xpack/core/ml/stats_index_mappings.json", + Version.CURRENT.toString(), + MAPPINGS_VERSION_VARIABLE + ); } public static String indexPattern() { @@ -49,8 +52,13 @@ public static String writeAlias() { * The listener will be notified with a boolean to indicate if the index was created because of this call, * but unless there is a failure after this method returns the index and alias should be present. */ - public static void createStatsIndexAndAliasIfNecessary(Client client, ClusterState state, IndexNameExpressionResolver resolver, - TimeValue masterNodeTimeout, ActionListener listener) { + public static void createStatsIndexAndAliasIfNecessary( + Client client, + ClusterState state, + IndexNameExpressionResolver resolver, + TimeValue masterNodeTimeout, + ActionListener listener + ) { MlIndexAndAlias.createIndexAndAliasIfNecessary(client, state, resolver, TEMPLATE_NAME, writeAlias(), masterNodeTimeout, listener); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/MlTasks.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/MlTasks.java index 2523b5e9b9334..acfbf6a7ea774 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/MlTasks.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/MlTasks.java @@ -42,12 +42,14 @@ public final class MlTasks { public static final String JOB_SNAPSHOT_UPGRADE_TASK_ID_PREFIX = "job-snapshot-upgrade-"; public static final String TRAINED_MODEL_DEPLOYMENT_TASK_ID_PREFIX = "trained_model_deployment-"; - public static final PersistentTasksCustomMetadata.Assignment AWAITING_UPGRADE = - new PersistentTasksCustomMetadata.Assignment(null, - "persistent task cannot be assigned while upgrade mode is enabled."); - public static final PersistentTasksCustomMetadata.Assignment RESET_IN_PROGRESS = - new PersistentTasksCustomMetadata.Assignment(null, - "persistent task will not be assigned as a feature reset is in progress."); + public static final PersistentTasksCustomMetadata.Assignment AWAITING_UPGRADE = new PersistentTasksCustomMetadata.Assignment( + null, + "persistent task cannot be assigned while upgrade mode is enabled." + ); + public static final PersistentTasksCustomMetadata.Assignment RESET_IN_PROGRESS = new PersistentTasksCustomMetadata.Assignment( + null, + "persistent task will not be assigned as a feature reset is in progress." + ); // When a master node action is executed and there is no master node the transport will wait // for a new master node to be elected and retry against that, but will only wait as long as @@ -60,8 +62,7 @@ public final class MlTasks { // defeats the point of the task being "persistent". public static final TimeValue PERSISTENT_TASK_MASTER_NODE_TIMEOUT = TimeValue.timeValueDays(365); - private MlTasks() { - } + private MlTasks() {} /** * Namespaces the task ids for jobs. @@ -112,21 +113,27 @@ public static PersistentTasksCustomMetadata.PersistentTask getJobTask(String } @Nullable - public static PersistentTasksCustomMetadata.PersistentTask getDatafeedTask(String datafeedId, - @Nullable PersistentTasksCustomMetadata tasks) { + public static PersistentTasksCustomMetadata.PersistentTask getDatafeedTask( + String datafeedId, + @Nullable PersistentTasksCustomMetadata tasks + ) { return tasks == null ? null : tasks.getTask(datafeedTaskId(datafeedId)); } @Nullable - public static PersistentTasksCustomMetadata.PersistentTask getDataFrameAnalyticsTask(String analyticsId, - @Nullable PersistentTasksCustomMetadata tasks) { + public static PersistentTasksCustomMetadata.PersistentTask getDataFrameAnalyticsTask( + String analyticsId, + @Nullable PersistentTasksCustomMetadata tasks + ) { return tasks == null ? null : tasks.getTask(dataFrameAnalyticsTaskId(analyticsId)); } @Nullable - public static PersistentTasksCustomMetadata.PersistentTask getSnapshotUpgraderTask(String jobId, - String snapshotId, - @Nullable PersistentTasksCustomMetadata tasks) { + public static PersistentTasksCustomMetadata.PersistentTask getSnapshotUpgraderTask( + String jobId, + String snapshotId, + @Nullable PersistentTasksCustomMetadata tasks + ) { return tasks == null ? null : tasks.getTask(snapshotUpgradeTaskId(jobId, snapshotId)); } @@ -236,10 +243,7 @@ public static Set openJobIds(@Nullable PersistentTasksCustomMetadata tas return Collections.emptySet(); } - return openJobTasks(tasks) - .stream() - .map(t -> t.getId().substring(JOB_TASK_ID_PREFIX.length())) - .collect(Collectors.toSet()); + return openJobTasks(tasks).stream().map(t -> t.getId().substring(JOB_TASK_ID_PREFIX.length())).collect(Collectors.toSet()); } public static Collection> openJobTasks(@Nullable PersistentTasksCustomMetadata tasks) { @@ -251,7 +255,9 @@ public static Collection> openJo } public static Collection> datafeedTasksOnNode( - @Nullable PersistentTasksCustomMetadata tasks, String nodeId) { + @Nullable PersistentTasksCustomMetadata tasks, + String nodeId + ) { if (tasks == null) { return Collections.emptyList(); } @@ -260,7 +266,9 @@ public static Collection> datafe } public static Collection> jobTasksOnNode( - @Nullable PersistentTasksCustomMetadata tasks, String nodeId) { + @Nullable PersistentTasksCustomMetadata tasks, + String nodeId + ) { if (tasks == null) { return Collections.emptyList(); } @@ -269,7 +277,9 @@ public static Collection> jobTas } public static Collection> nonFailedJobTasksOnNode( - @Nullable PersistentTasksCustomMetadata tasks, String nodeId) { + @Nullable PersistentTasksCustomMetadata tasks, + String nodeId + ) { if (tasks == null) { return Collections.emptyList(); } @@ -287,7 +297,9 @@ public static Collection> nonFai } public static Collection> snapshotUpgradeTasksOnNode( - @Nullable PersistentTasksCustomMetadata tasks, String nodeId) { + @Nullable PersistentTasksCustomMetadata tasks, + String nodeId + ) { if (tasks == null) { return Collections.emptyList(); } @@ -296,7 +308,9 @@ public static Collection> snapsh } public static Collection> nonFailedSnapshotUpgradeTasksOnNode( - @Nullable PersistentTasksCustomMetadata tasks, String nodeId) { + @Nullable PersistentTasksCustomMetadata tasks, + String nodeId + ) { if (tasks == null) { return Collections.emptyList(); } @@ -322,11 +336,10 @@ public static Collection> nonFai * @param nodes The cluster nodes * @return The job Ids of tasks to do not have an assignment. */ - public static Set unassignedJobIds(@Nullable PersistentTasksCustomMetadata tasks, - DiscoveryNodes nodes) { + public static Set unassignedJobIds(@Nullable PersistentTasksCustomMetadata tasks, DiscoveryNodes nodes) { return unassignedJobTasks(tasks, nodes).stream() - .map(task -> task.getId().substring(JOB_TASK_ID_PREFIX.length())) - .collect(Collectors.toSet()); + .map(task -> task.getId().substring(JOB_TASK_ID_PREFIX.length())) + .collect(Collectors.toSet()); } /** @@ -338,8 +351,9 @@ public static Set unassignedJobIds(@Nullable PersistentTasksCustomMetada * @return Unassigned job tasks */ public static Collection> unassignedJobTasks( - @Nullable PersistentTasksCustomMetadata tasks, - DiscoveryNodes nodes) { + @Nullable PersistentTasksCustomMetadata tasks, + DiscoveryNodes nodes + ) { if (tasks == null) { return Collections.emptyList(); } @@ -359,9 +373,9 @@ public static Set startedDatafeedIds(@Nullable PersistentTasksCustomMeta } return tasks.findTasks(DATAFEED_TASK_NAME, task -> true) - .stream() - .map(t -> t.getId().substring(DATAFEED_TASK_ID_PREFIX.length())) - .collect(Collectors.toSet()); + .stream() + .map(t -> t.getId().substring(DATAFEED_TASK_ID_PREFIX.length())) + .collect(Collectors.toSet()); } /** @@ -372,12 +386,11 @@ public static Set startedDatafeedIds(@Nullable PersistentTasksCustomMeta * @param nodes The cluster nodes * @return The job Ids of tasks to do not have an assignment. */ - public static Set unassignedDatafeedIds(@Nullable PersistentTasksCustomMetadata tasks, - DiscoveryNodes nodes) { + public static Set unassignedDatafeedIds(@Nullable PersistentTasksCustomMetadata tasks, DiscoveryNodes nodes) { return unassignedDatafeedTasks(tasks, nodes).stream() - .map(task -> task.getId().substring(DATAFEED_TASK_ID_PREFIX.length())) - .collect(Collectors.toSet()); + .map(task -> task.getId().substring(DATAFEED_TASK_ID_PREFIX.length())) + .collect(Collectors.toSet()); } /** @@ -389,8 +402,9 @@ public static Set unassignedDatafeedIds(@Nullable PersistentTasksCustomM * @return Unassigned datafeed tasks */ public static Collection> unassignedDatafeedTasks( - @Nullable PersistentTasksCustomMetadata tasks, - DiscoveryNodes nodes) { + @Nullable PersistentTasksCustomMetadata tasks, + DiscoveryNodes nodes + ) { if (tasks == null) { return Collections.emptyList(); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/CloseJobAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/CloseJobAction.java index 7e9910a782e8c..27c8e4d048018 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/CloseJobAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/CloseJobAction.java @@ -9,16 +9,16 @@ import org.elasticsearch.action.ActionType; import org.elasticsearch.action.support.tasks.BaseTasksRequest; import org.elasticsearch.action.support.tasks.BaseTasksResponse; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.tasks.Task; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.tasks.Task; import org.elasticsearch.xpack.core.ml.MachineLearningField; import org.elasticsearch.xpack.core.ml.job.config.Job; @@ -45,8 +45,10 @@ public static class Request extends BaseTasksRequest implements ToXCont static { PARSER.declareString(Request::setJobId, Job.ID); - PARSER.declareString((request, val) -> - request.setCloseTimeout(TimeValue.parseTimeValue(val, TIMEOUT.getPreferredName())), TIMEOUT); + PARSER.declareString( + (request, val) -> request.setCloseTimeout(TimeValue.parseTimeValue(val, TIMEOUT.getPreferredName())), + TIMEOUT + ); PARSER.declareBoolean(Request::setForce, FORCE); PARSER.declareBoolean(Request::setAllowNoMatch, ALLOW_NO_MATCH); } @@ -62,7 +64,7 @@ public static Request parseRequest(String jobId, XContentParser parser) { private String jobId; private boolean force = false; private boolean allowNoMatch = true; - // A big state can take a while to persist. For symmetry with the _open endpoint any + // A big state can take a while to persist. For symmetry with the _open endpoint any // changes here should be reflected there too. private TimeValue timeout = MachineLearningField.STATE_PERSIST_RESTORE_TIMEOUT; @@ -136,14 +138,18 @@ public Request setAllowNoMatch(boolean allowNoMatch) { return this; } - public boolean isLocal() { return local; } + public boolean isLocal() { + return local; + } public Request setLocal(boolean local) { this.local = local; return this; } - public String[] getOpenJobIds() { return openJobIds; } + public String[] getOpenJobIds() { + return openJobIds; + } public Request setOpenJobIds(String[] openJobIds) { this.openJobIds = openJobIds; @@ -188,10 +194,10 @@ public boolean equals(Object obj) { } Request other = (Request) obj; // openJobIds are excluded - return Objects.equals(jobId, other.jobId) && - Objects.equals(timeout, other.timeout) && - Objects.equals(force, other.force) && - Objects.equals(allowNoMatch, other.allowNoMatch); + return Objects.equals(jobId, other.jobId) + && Objects.equals(timeout, other.timeout) + && Objects.equals(force, other.force) + && Objects.equals(allowNoMatch, other.allowNoMatch); } } @@ -242,4 +248,3 @@ public int hashCode() { } } - diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/CreateTrainedModelAllocationAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/CreateTrainedModelAllocationAction.java index 9ac6c6b5e4a31..5e7c595197ef9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/CreateTrainedModelAllocationAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/CreateTrainedModelAllocationAction.java @@ -84,6 +84,7 @@ public static class Response extends ActionResponse implements ToXContentObject static { PARSER.declareObject(ConstructingObjectParser.constructorArg(), (p, c) -> TrainedModelAllocation.fromXContent(p), ALLOCATION); } + static Response fromXContent(XContentParser parser) { return PARSER.apply(parser, null); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/DeleteCalendarAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/DeleteCalendarAction.java index 13a156315a74d..a187848e86896 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/DeleteCalendarAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/DeleteCalendarAction.java @@ -29,7 +29,6 @@ private DeleteCalendarAction() { public static class Request extends AcknowledgedRequest { - private String calendarId; public Request(StreamInput in) throws IOException { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/DeleteDataFrameAnalyticsAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/DeleteDataFrameAnalyticsAction.java index 90469e580f82d..e06427681c2ee 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/DeleteDataFrameAnalyticsAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/DeleteDataFrameAnalyticsAction.java @@ -10,10 +10,10 @@ import org.elasticsearch.action.ActionType; import org.elasticsearch.action.support.master.AcknowledgedRequest; import org.elasticsearch.action.support.master.AcknowledgedResponse; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsConfig; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; @@ -84,9 +84,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; DeleteDataFrameAnalyticsAction.Request request = (DeleteDataFrameAnalyticsAction.Request) o; - return Objects.equals(id, request.id) - && force == request.force - && Objects.equals(timeout, request.timeout); + return Objects.equals(id, request.id) && force == request.force && Objects.equals(timeout, request.timeout); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/DeleteDatafeedAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/DeleteDatafeedAction.java index 40f8e6821efda..968aa00df4633 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/DeleteDatafeedAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/DeleteDatafeedAction.java @@ -10,9 +10,9 @@ import org.elasticsearch.action.ActionType; import org.elasticsearch.action.support.master.AcknowledgedRequest; import org.elasticsearch.action.support.master.AcknowledgedResponse; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentFragment; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfig; @@ -55,7 +55,7 @@ public boolean isForce() { return force; } - public void setForce(boolean force) { + public void setForce(boolean force) { this.force = force; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/DeleteExpiredDataAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/DeleteExpiredDataAction.java index 20a72d251c92c..e6f362bbea14c 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/DeleteExpiredDataAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/DeleteExpiredDataAction.java @@ -10,11 +10,11 @@ import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.ActionResponse; import org.elasticsearch.action.ActionType; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -37,15 +37,11 @@ public static class Request extends ActionRequest { public static final ParseField REQUESTS_PER_SECOND = new ParseField("requests_per_second"); public static final ParseField TIMEOUT = new ParseField("timeout"); - public static final ObjectParser PARSER = new ObjectParser<>( - "delete_expired_data_request", - false, - Request::new); + public static final ObjectParser PARSER = new ObjectParser<>("delete_expired_data_request", false, Request::new); static { PARSER.declareFloat(Request::setRequestsPerSecond, REQUESTS_PER_SECOND); - PARSER.declareString((obj, value) -> obj.setTimeout(TimeValue.parseTimeValue(value, TIMEOUT.getPreferredName())), - TIMEOUT); + PARSER.declareString((obj, value) -> obj.setTimeout(TimeValue.parseTimeValue(value, TIMEOUT.getPreferredName())), TIMEOUT); PARSER.declareString(Request::setJobId, Job.ID); } @@ -60,7 +56,7 @@ public static Request parseRequest(String jobId, XContentParser parser) { private Float requestsPerSecond; private TimeValue timeout; private String jobId; - private String [] expandedJobIds; + private String[] expandedJobIds; public Request() {} @@ -109,11 +105,11 @@ public Request setJobId(String jobId) { * @return The expanded Ids in the case where {@code jobId} is not `_all` * otherwise null. */ - public String [] getExpandedJobIds() { + public String[] getExpandedJobIds() { return expandedJobIds; } - public void setExpandedJobIds(String [] expandedJobIds) { + public void setExpandedJobIds(String[] expandedJobIds) { this.expandedJobIds = expandedJobIds; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/DeleteFilterAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/DeleteFilterAction.java index 2214a3f5bea2c..3e3aebe179851 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/DeleteFilterAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/DeleteFilterAction.java @@ -10,15 +10,14 @@ import org.elasticsearch.action.ActionType; import org.elasticsearch.action.support.master.AcknowledgedRequest; import org.elasticsearch.action.support.master.AcknowledgedResponse; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; import java.io.IOException; import java.util.Objects; - public class DeleteFilterAction extends ActionType { public static final DeleteFilterAction INSTANCE = new DeleteFilterAction(); @@ -73,4 +72,3 @@ public boolean equals(Object obj) { } } } - diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/DeleteTrainedModelAliasAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/DeleteTrainedModelAliasAction.java index d53ab36ff10ae..5295263962430 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/DeleteTrainedModelAliasAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/DeleteTrainedModelAliasAction.java @@ -19,7 +19,6 @@ import java.io.IOException; import java.util.Objects; - public class DeleteTrainedModelAliasAction extends ActionType { public static final DeleteTrainedModelAliasAction INSTANCE = new DeleteTrainedModelAliasAction(); @@ -56,7 +55,7 @@ public String getModelId() { } @Override - public void writeTo(StreamOutput out) throws IOException { + public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); out.writeString(modelAlias); out.writeString(modelId); @@ -72,8 +71,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Request request = (Request) o; - return Objects.equals(modelAlias, request.modelAlias) - && Objects.equals(modelId, request.modelId); + return Objects.equals(modelAlias, request.modelAlias) && Objects.equals(modelId, request.modelId); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/EstimateModelMemoryAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/EstimateModelMemoryAction.java index 37d325831a841..3494b09d43e82 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/EstimateModelMemoryAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/EstimateModelMemoryAction.java @@ -10,11 +10,11 @@ import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.ActionResponse; import org.elasticsearch.action.ActionType; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -43,17 +43,20 @@ public static class Request extends ActionRequest { public static final ParseField OVERALL_CARDINALITY = new ParseField("overall_cardinality"); public static final ParseField MAX_BUCKET_CARDINALITY = new ParseField("max_bucket_cardinality"); - public static final ObjectParser PARSER = - new ObjectParser<>(NAME, EstimateModelMemoryAction.Request::new); + public static final ObjectParser PARSER = new ObjectParser<>(NAME, EstimateModelMemoryAction.Request::new); static { PARSER.declareObject(Request::setAnalysisConfig, (p, c) -> AnalysisConfig.STRICT_PARSER.apply(p, c).build(), ANALYSIS_CONFIG); - PARSER.declareObject(Request::setOverallCardinality, + PARSER.declareObject( + Request::setOverallCardinality, (p, c) -> p.map(HashMap::new, parser -> Request.parseNonNegativeLong(parser, OVERALL_CARDINALITY)), - OVERALL_CARDINALITY); - PARSER.declareObject(Request::setMaxBucketCardinality, + OVERALL_CARDINALITY + ); + PARSER.declareObject( + Request::setMaxBucketCardinality, (p, c) -> p.map(HashMap::new, parser -> Request.parseNonNegativeLong(parser, MAX_BUCKET_CARDINALITY)), - MAX_BUCKET_CARDINALITY); + MAX_BUCKET_CARDINALITY + ); } public static Request parseRequest(XContentParser parser) { @@ -111,8 +114,7 @@ public Map getOverallCardinality() { } public void setOverallCardinality(Map overallCardinality) { - this.overallCardinality = - Collections.unmodifiableMap(ExceptionsHelper.requireNonNull(overallCardinality, OVERALL_CARDINALITY)); + this.overallCardinality = Collections.unmodifiableMap(ExceptionsHelper.requireNonNull(overallCardinality, OVERALL_CARDINALITY)); } public Map getMaxBucketCardinality() { @@ -120,15 +122,19 @@ public Map getMaxBucketCardinality() { } public void setMaxBucketCardinality(Map maxBucketCardinality) { - this.maxBucketCardinality = - Collections.unmodifiableMap(ExceptionsHelper.requireNonNull(maxBucketCardinality, MAX_BUCKET_CARDINALITY)); + this.maxBucketCardinality = Collections.unmodifiableMap( + ExceptionsHelper.requireNonNull(maxBucketCardinality, MAX_BUCKET_CARDINALITY) + ); } private static long parseNonNegativeLong(XContentParser parser, ParseField enclosingField) throws IOException { long value = parser.longValue(); if (value < 0) { - throw ExceptionsHelper.badRequestException("[{}] contained negative cardinality [{}]", - enclosingField.getPreferredName(), value); + throw ExceptionsHelper.badRequestException( + "[{}] contained negative cardinality [{}]", + enclosingField.getPreferredName(), + value + ); } return value; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/EvaluateDataFrameAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/EvaluateDataFrameAction.java index 9b8eb2ed3588d..2277cd2acb08d 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/EvaluateDataFrameAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/EvaluateDataFrameAction.java @@ -10,17 +10,17 @@ import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.ActionResponse; import org.elasticsearch.action.ActionType; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.common.xcontent.XContentParserUtils; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.XContentParserUtils; -import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.Evaluation; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationMetricResult; import org.elasticsearch.xpack.core.ml.job.messages.Messages; @@ -51,17 +51,19 @@ public static class Request extends ActionRequest implements ToXContentObject { private static final ParseField QUERY = new ParseField("query"); private static final ParseField EVALUATION = new ParseField("evaluation"); - @SuppressWarnings({ "unchecked"}) + @SuppressWarnings({ "unchecked" }) private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( NAME, - a -> new Request((List) a[0], (QueryProvider) a[1], (Evaluation) a[2])); + a -> new Request((List) a[0], (QueryProvider) a[1], (Evaluation) a[2]) + ); static { PARSER.declareStringArray(constructorArg(), INDEX); PARSER.declareObject( optionalConstructorArg(), (p, c) -> QueryProvider.fromXContent(p, true, Messages.DATA_FRAME_ANALYTICS_BAD_QUERY_FORMAT), - QUERY); + QUERY + ); PARSER.declareObject(constructorArg(), (p, c) -> parseEvaluation(p), EVALUATION); } @@ -154,10 +156,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws if (queryProvider != null) { builder.field(QUERY.getPreferredName(), queryProvider.getQuery()); } - builder - .startObject(EVALUATION.getPreferredName()) - .field(evaluation.getName(), evaluation) - .endObject(); + builder.startObject(EVALUATION.getPreferredName()).field(evaluation.getName(), evaluation).endObject(); builder.endObject(); return builder; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/ExplainDataFrameAnalyticsAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/ExplainDataFrameAnalyticsAction.java index 697f02860ef3c..801705e3b0697 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/ExplainDataFrameAnalyticsAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/ExplainDataFrameAnalyticsAction.java @@ -8,10 +8,10 @@ import org.elasticsearch.action.ActionResponse; import org.elasticsearch.action.ActionType; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ml.dataframe.explain.FieldSelection; @@ -37,11 +37,11 @@ public static class Response extends ActionResponse implements ToXContentObject public static final ParseField FIELD_SELECTION = new ParseField("field_selection"); public static final ParseField MEMORY_ESTIMATION = new ParseField("memory_estimation"); - @SuppressWarnings({ "unchecked"}) - static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>( - TYPE.getPreferredName(), - args -> new Response((List) args[0], (MemoryEstimation) args[1])); + @SuppressWarnings({ "unchecked" }) + static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + TYPE.getPreferredName(), + args -> new Response((List) args[0], (MemoryEstimation) args[1]) + ); static { PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), FieldSelection.PARSER, FIELD_SELECTION); @@ -83,8 +83,7 @@ public boolean equals(Object other) { if (other == null || getClass() != other.getClass()) return false; Response that = (Response) other; - return Objects.equals(fieldSelection, that.fieldSelection) - && Objects.equals(memoryEstimation, that.memoryEstimation); + return Objects.equals(fieldSelection, that.fieldSelection) && Objects.equals(memoryEstimation, that.memoryEstimation); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/FlushJobAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/FlushJobAction.java index 48c7bfac9314f..2bb8e3363b3e4 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/FlushJobAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/FlushJobAction.java @@ -8,12 +8,12 @@ import org.elasticsearch.action.ActionType; import org.elasticsearch.action.support.tasks.BaseTasksResponse; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -67,8 +67,7 @@ public static Request parseRequest(String jobId, XContentParser parser) { private String advanceTime; private String skipTime; - public Request() { - } + public Request() {} public Request(StreamInput in) throws IOException { super(in); @@ -162,13 +161,13 @@ public boolean equals(Object obj) { return false; } Request other = (Request) obj; - return Objects.equals(jobId, other.jobId) && - calcInterim == other.calcInterim && - waitForNormalization == other.waitForNormalization && - Objects.equals(start, other.start) && - Objects.equals(end, other.end) && - Objects.equals(advanceTime, other.advanceTime) && - Objects.equals(skipTime, other.skipTime); + return Objects.equals(jobId, other.jobId) + && calcInterim == other.calcInterim + && waitForNormalization == other.waitForNormalization + && Objects.equals(start, other.start) + && Objects.equals(end, other.end) + && Objects.equals(advanceTime, other.advanceTime) + && Objects.equals(skipTime, other.skipTime); } @Override @@ -202,8 +201,9 @@ public Response(boolean flushed, @Nullable Instant lastFinalizedBucketEnd) { super(null, null); this.flushed = flushed; // Round to millisecond accuracy to ensure round-tripping via XContent results in an equal object - this.lastFinalizedBucketEnd = - (lastFinalizedBucketEnd != null) ? Instant.ofEpochMilli(lastFinalizedBucketEnd.toEpochMilli()) : null; + this.lastFinalizedBucketEnd = (lastFinalizedBucketEnd != null) + ? Instant.ofEpochMilli(lastFinalizedBucketEnd.toEpochMilli()) + : null; } public Response(StreamInput in) throws IOException { @@ -232,9 +232,11 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.startObject(); builder.field("flushed", flushed); if (lastFinalizedBucketEnd != null) { - builder.timeField(FlushAcknowledgement.LAST_FINALIZED_BUCKET_END.getPreferredName(), + builder.timeField( + FlushAcknowledgement.LAST_FINALIZED_BUCKET_END.getPreferredName(), FlushAcknowledgement.LAST_FINALIZED_BUCKET_END.getPreferredName() + "_string", - lastFinalizedBucketEnd.toEpochMilli()); + lastFinalizedBucketEnd.toEpochMilli() + ); } builder.endObject(); return builder; @@ -245,8 +247,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Response response = (Response) o; - return flushed == response.flushed && - Objects.equals(lastFinalizedBucketEnd, response.lastFinalizedBucketEnd); + return flushed == response.flushed && Objects.equals(lastFinalizedBucketEnd, response.lastFinalizedBucketEnd); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/ForecastJobAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/ForecastJobAction.java index 548828a071235..ed94d145a0276 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/ForecastJobAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/ForecastJobAction.java @@ -8,13 +8,13 @@ import org.elasticsearch.action.ActionType; import org.elasticsearch.action.support.tasks.BaseTasksResponse; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParseException; @@ -75,8 +75,7 @@ public static Request parseRequest(String jobId, XContentParser parser) { private TimeValue expiresIn; private Long maxModelMemory; - public Request() { - } + public Request() {} public Request(StreamInput in) throws IOException { super(in); @@ -108,12 +107,20 @@ public void setDuration(String duration) { public void setDuration(TimeValue duration) { this.duration = duration; if (this.duration.compareTo(TimeValue.ZERO) <= 0) { - throw new IllegalArgumentException("[" + DURATION.getPreferredName() + "] must be positive: [" - + duration.getStringRep() + "]"); + throw new IllegalArgumentException( + "[" + DURATION.getPreferredName() + "] must be positive: [" + duration.getStringRep() + "]" + ); } if (this.duration.compareTo(MAX_DURATION) > 0) { - throw new IllegalArgumentException("[" + DURATION.getPreferredName() + "] must be " - + MAX_DURATION.getStringRep() + " or less: [" + duration.getStringRep() + "]"); + throw new IllegalArgumentException( + "[" + + DURATION.getPreferredName() + + "] must be " + + MAX_DURATION.getStringRep() + + " or less: [" + + duration.getStringRep() + + "]" + ); } } @@ -128,8 +135,9 @@ public void setExpiresIn(String expiration) { public void setExpiresIn(TimeValue expiresIn) { this.expiresIn = expiresIn; if (this.expiresIn.compareTo(TimeValue.ZERO) < 0) { - throw new IllegalArgumentException("[" + EXPIRES_IN.getPreferredName() + "] must be non-negative: [" - + expiresIn.getStringRep() + "]"); + throw new IllegalArgumentException( + "[" + EXPIRES_IN.getPreferredName() + "] must be non-negative: [" + expiresIn.getStringRep() + "]" + ); } } @@ -141,7 +149,8 @@ public void setMaxModelMemory(long numBytes) { throw ExceptionsHelper.badRequestException( "[{}] must be less than {}", MAX_MODEL_MEMORY.getPreferredName(), - FORECAST_LOCAL_STORAGE_LIMIT.getStringRep()); + FORECAST_LOCAL_STORAGE_LIMIT.getStringRep() + ); } this.maxModelMemory = numBytes; } @@ -165,9 +174,9 @@ public boolean equals(Object obj) { } Request other = (Request) obj; return Objects.equals(jobId, other.jobId) - && Objects.equals(duration, other.duration) - && Objects.equals(expiresIn, other.expiresIn) - && Objects.equals(maxModelMemory, other.maxModelMemory); + && Objects.equals(duration, other.duration) + && Objects.equals(expiresIn, other.expiresIn) + && Objects.equals(maxModelMemory, other.maxModelMemory); } @Override @@ -247,4 +256,3 @@ public int hashCode() { } } } - diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetBucketsAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetBucketsAction.java index fad3eafc2a802..6aa4d284872b3 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetBucketsAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetBucketsAction.java @@ -9,10 +9,10 @@ import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.ActionType; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -81,8 +81,7 @@ public static Request parseRequest(String jobId, XContentParser parser) { private String sort = Result.TIMESTAMP.getPreferredName(); private boolean descending = false; - public Request() { - } + public Request() {} public Request(StreamInput in) throws IOException { super(in); @@ -108,12 +107,21 @@ public String getJobId() { public void setTimestamp(String timestamp) { if (pageParams != null || start != null || end != null || anomalyScore != null) { - throw new IllegalArgumentException("Param [" + TIMESTAMP.getPreferredName() + "] is incompatible with [" - + PageParams.FROM.getPreferredName() + "," - + PageParams.SIZE.getPreferredName() + "," - + START.getPreferredName() + "," - + END.getPreferredName() + "," - + ANOMALY_SCORE.getPreferredName() + "]"); + throw new IllegalArgumentException( + "Param [" + + TIMESTAMP.getPreferredName() + + "] is incompatible with [" + + PageParams.FROM.getPreferredName() + + "," + + PageParams.SIZE.getPreferredName() + + "," + + START.getPreferredName() + + "," + + END.getPreferredName() + + "," + + ANOMALY_SCORE.getPreferredName() + + "]" + ); } this.timestamp = ExceptionsHelper.requireNonNull(timestamp, Result.TIMESTAMP.getPreferredName()); } @@ -144,8 +152,9 @@ public String getStart() { public void setStart(String start) { if (timestamp != null) { - throw new IllegalArgumentException("Param [" + START.getPreferredName() + "] is incompatible with [" - + TIMESTAMP.getPreferredName() + "]."); + throw new IllegalArgumentException( + "Param [" + START.getPreferredName() + "] is incompatible with [" + TIMESTAMP.getPreferredName() + "]." + ); } this.start = start; } @@ -156,8 +165,9 @@ public String getEnd() { public void setEnd(String end) { if (timestamp != null) { - throw new IllegalArgumentException("Param [" + END.getPreferredName() + "] is incompatible with [" - + TIMESTAMP.getPreferredName() + "]."); + throw new IllegalArgumentException( + "Param [" + END.getPreferredName() + "] is incompatible with [" + TIMESTAMP.getPreferredName() + "]." + ); } this.end = end; } @@ -168,8 +178,15 @@ public PageParams getPageParams() { public void setPageParams(PageParams pageParams) { if (timestamp != null) { - throw new IllegalArgumentException("Param [" + PageParams.FROM.getPreferredName() - + ", " + PageParams.SIZE.getPreferredName() + "] is incompatible with [" + TIMESTAMP.getPreferredName() + "]."); + throw new IllegalArgumentException( + "Param [" + + PageParams.FROM.getPreferredName() + + ", " + + PageParams.SIZE.getPreferredName() + + "] is incompatible with [" + + TIMESTAMP.getPreferredName() + + "]." + ); } this.pageParams = ExceptionsHelper.requireNonNull(pageParams, PageParams.PAGE.getPreferredName()); } @@ -180,8 +197,9 @@ public Double getAnomalyScore() { public void setAnomalyScore(double anomalyScore) { if (timestamp != null) { - throw new IllegalArgumentException("Param [" + ANOMALY_SCORE.getPreferredName() + "] is incompatible with [" - + TIMESTAMP.getPreferredName() + "]."); + throw new IllegalArgumentException( + "Param [" + ANOMALY_SCORE.getPreferredName() + "] is incompatible with [" + TIMESTAMP.getPreferredName() + "]." + ); } this.anomalyScore = anomalyScore; } @@ -263,16 +281,16 @@ public boolean equals(Object obj) { return false; } Request other = (Request) obj; - return Objects.equals(jobId, other.jobId) && - Objects.equals(timestamp, other.timestamp) && - Objects.equals(expand, other.expand) && - Objects.equals(excludeInterim, other.excludeInterim) && - Objects.equals(anomalyScore, other.anomalyScore) && - Objects.equals(pageParams, other.pageParams) && - Objects.equals(start, other.start) && - Objects.equals(end, other.end) && - Objects.equals(sort, other.sort) && - Objects.equals(descending, other.descending); + return Objects.equals(jobId, other.jobId) + && Objects.equals(timestamp, other.timestamp) + && Objects.equals(expand, other.expand) + && Objects.equals(excludeInterim, other.excludeInterim) + && Objects.equals(anomalyScore, other.anomalyScore) + && Objects.equals(pageParams, other.pageParams) + && Objects.equals(start, other.start) + && Objects.equals(end, other.end) + && Objects.equals(sort, other.sort) + && Objects.equals(descending, other.descending); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetCalendarEventsAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetCalendarEventsAction.java index ef9355b026c88..65017cf11fcef 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetCalendarEventsAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetCalendarEventsAction.java @@ -10,11 +10,11 @@ import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.ActionType; import org.elasticsearch.action.ValidateActions; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -66,8 +66,7 @@ public static Request parseRequest(String calendarId, XContentParser parser) { private String jobId; private PageParams pageParams = PageParams.defaultParams(); - public Request() { - } + public Request() {} public Request(StreamInput in) throws IOException { super(in); @@ -93,6 +92,7 @@ private void setCalendarId(String calendarId) { public String getStart() { return start; } + public void setStart(String start) { this.start = start; } @@ -126,8 +126,16 @@ public ActionRequestValidationException validate() { ActionRequestValidationException e = null; if (jobId != null && Strings.isAllOrWildcard(calendarId) == false) { - e = ValidateActions.addValidationError("If " + Job.ID.getPreferredName() + " is used " + - Calendar.ID.getPreferredName() + " must be '" + GetCalendarsAction.Request.ALL + "' or '*'", e); + e = ValidateActions.addValidationError( + "If " + + Job.ID.getPreferredName() + + " is used " + + Calendar.ID.getPreferredName() + + " must be '" + + GetCalendarsAction.Request.ALL + + "' or '*'", + e + ); } return e; } @@ -156,9 +164,11 @@ public boolean equals(Object obj) { return false; } Request other = (Request) obj; - return Objects.equals(calendarId, other.calendarId) && Objects.equals(start, other.start) - && Objects.equals(end, other.end) && Objects.equals(pageParams, other.pageParams) - && Objects.equals(jobId, other.jobId); + return Objects.equals(calendarId, other.calendarId) + && Objects.equals(start, other.start) + && Objects.equals(end, other.end) + && Objects.equals(pageParams, other.pageParams) + && Objects.equals(jobId, other.jobId); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetCalendarsAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetCalendarsAction.java index b189141e77ac7..120a59f685641 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetCalendarsAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetCalendarsAction.java @@ -11,12 +11,12 @@ import org.elasticsearch.action.ActionType; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.xcontent.ObjectParser; import org.elasticsearch.common.xcontent.StatusToXContentObject; +import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.xcontent.ObjectParser; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.rest.RestStatus; import org.elasticsearch.xpack.core.action.AbstractGetResourcesResponse; import org.elasticsearch.xpack.core.action.util.PageParams; import org.elasticsearch.xpack.core.action.util.QueryPage; @@ -58,8 +58,7 @@ public static Request parseRequest(String calendarId, XContentParser parser) { private String calendarId; private PageParams pageParams; - public Request() { - } + public Request() {} public Request(StreamInput in) throws IOException { super(in); @@ -88,10 +87,16 @@ public ActionRequestValidationException validate() { ActionRequestValidationException validationException = null; if (calendarId != null && pageParams != null) { - validationException = addValidationError("Params [" + PageParams.FROM.getPreferredName() - + ", " + PageParams.SIZE.getPreferredName() + "] are incompatible with [" - + Calendar.ID.getPreferredName() + "].", - validationException); + validationException = addValidationError( + "Params [" + + PageParams.FROM.getPreferredName() + + ", " + + PageParams.SIZE.getPreferredName() + + "] are incompatible with [" + + Calendar.ID.getPreferredName() + + "].", + validationException + ); } return validationException; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetCategoriesAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetCategoriesAction.java index 1f808613de057..ccb159c53e315 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetCategoriesAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetCategoriesAction.java @@ -9,10 +9,10 @@ import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.ActionType; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -70,8 +70,7 @@ public Request(String jobId) { this.jobId = ExceptionsHelper.requireNonNull(jobId, Job.ID.getPreferredName()); } - public Request() { - } + public Request() {} public Request(StreamInput in) throws IOException { super(in); @@ -81,24 +80,44 @@ public Request(StreamInput in) throws IOException { partitionFieldValue = in.readOptionalString(); } - public String getJobId() { return jobId; } + public String getJobId() { + return jobId; + } - public PageParams getPageParams() { return pageParams; } + public PageParams getPageParams() { + return pageParams; + } - public Long getCategoryId() { return categoryId; } + public Long getCategoryId() { + return categoryId; + } public void setCategoryId(Long categoryId) { if (pageParams != null) { - throw new IllegalArgumentException("Param [" + CATEGORY_ID.getPreferredName() + "] is incompatible with [" - + PageParams.FROM.getPreferredName() + ", " + PageParams.SIZE.getPreferredName() + "]."); + throw new IllegalArgumentException( + "Param [" + + CATEGORY_ID.getPreferredName() + + "] is incompatible with [" + + PageParams.FROM.getPreferredName() + + ", " + + PageParams.SIZE.getPreferredName() + + "]." + ); } this.categoryId = ExceptionsHelper.requireNonNull(categoryId, CATEGORY_ID.getPreferredName()); } public void setPageParams(PageParams pageParams) { if (categoryId != null) { - throw new IllegalArgumentException("Param [" + PageParams.FROM.getPreferredName() + ", " - + PageParams.SIZE.getPreferredName() + "] is incompatible with [" + CATEGORY_ID.getPreferredName() + "]."); + throw new IllegalArgumentException( + "Param [" + + PageParams.FROM.getPreferredName() + + ", " + + PageParams.SIZE.getPreferredName() + + "] is incompatible with [" + + CATEGORY_ID.getPreferredName() + + "]." + ); } this.pageParams = pageParams; } @@ -115,9 +134,17 @@ public void setPartitionFieldValue(String partitionFieldValue) { public ActionRequestValidationException validate() { ActionRequestValidationException validationException = null; if (pageParams == null && categoryId == null) { - validationException = addValidationError("Both [" + CATEGORY_ID.getPreferredName() + "] and [" - + PageParams.FROM.getPreferredName() + ", " + PageParams.SIZE.getPreferredName() + "] " - + "cannot be null" , validationException); + validationException = addValidationError( + "Both [" + + CATEGORY_ID.getPreferredName() + + "] and [" + + PageParams.FROM.getPreferredName() + + ", " + + PageParams.SIZE.getPreferredName() + + "] " + + "cannot be null", + validationException + ); } return validationException; } @@ -150,15 +177,13 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws @Override public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; Request request = (Request) o; return Objects.equals(jobId, request.jobId) - && Objects.equals(categoryId, request.categoryId) - && Objects.equals(pageParams, request.pageParams) - && Objects.equals(partitionFieldValue, request.partitionFieldValue); + && Objects.equals(categoryId, request.categoryId) + && Objects.equals(pageParams, request.pageParams) + && Objects.equals(partitionFieldValue, request.partitionFieldValue); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetDataFrameAnalyticsAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetDataFrameAnalyticsAction.java index e5ef4bf8f2bef..1ed331cfab674 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetDataFrameAnalyticsAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetDataFrameAnalyticsAction.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ml.action; import org.elasticsearch.action.ActionType; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xpack.core.action.AbstractGetResourcesRequest; import org.elasticsearch.xpack.core.action.AbstractGetResourcesResponse; import org.elasticsearch.xpack.core.action.util.QueryPage; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetDataFrameAnalyticsStatsAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetDataFrameAnalyticsStatsAction.java index 448b20147ba5a..a178661be97aa 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetDataFrameAnalyticsStatsAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetDataFrameAnalyticsStatsAction.java @@ -18,11 +18,11 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.tasks.Task; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.tasks.Task; import org.elasticsearch.xpack.core.action.util.PageParams; import org.elasticsearch.xpack.core.action.util.QueryPage; import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsConfig; @@ -141,9 +141,7 @@ public boolean equals(Object obj) { return false; } Request other = (Request) obj; - return Objects.equals(id, other.id) - && allowNoMatch == other.allowNoMatch - && Objects.equals(pageParams, other.pageParams); + return Objects.equals(id, other.id) && allowNoMatch == other.allowNoMatch && Objects.equals(pageParams, other.pageParams); } } @@ -176,9 +174,17 @@ public static class Stats implements ToXContentObject, Writeable { @Nullable private final String assignmentExplanation; - public Stats(String id, DataFrameAnalyticsState state, @Nullable String failureReason, List progress, - @Nullable DataCounts dataCounts, @Nullable MemoryUsage memoryUsage, @Nullable AnalysisStats analysisStats, - @Nullable DiscoveryNode node, @Nullable String assignmentExplanation) { + public Stats( + String id, + DataFrameAnalyticsState state, + @Nullable String failureReason, + List progress, + @Nullable DataCounts dataCounts, + @Nullable MemoryUsage memoryUsage, + @Nullable AnalysisStats analysisStats, + @Nullable DiscoveryNode node, + @Nullable String assignmentExplanation + ) { this.id = Objects.requireNonNull(id); this.state = Objects.requireNonNull(state); this.failureReason = failureReason; @@ -248,7 +254,8 @@ private static List readProgressFromLegacy(DataFrameAnalyticsStat new PhaseProgress("reindexing", reindexingProgress), new PhaseProgress("loading_data", loadingDataProgress), new PhaseProgress("analyzing", analyzingProgress), - new PhaseProgress("writing_results", 0)); + new PhaseProgress("writing_results", 0) + ); } public String getId() { @@ -316,7 +323,11 @@ private XContentBuilder toUnwrappedXContent(XContentBuilder builder, Params para analysisStats, new MapParams( Collections.singletonMap( - ToXContentParams.FOR_INTERNAL_STORAGE, Boolean.toString(params.paramAsBoolean(VERBOSE, false))))); + ToXContentParams.FOR_INTERNAL_STORAGE, + Boolean.toString(params.paramAsBoolean(VERBOSE, false)) + ) + ) + ); builder.endObject(); } if (node != null) { @@ -390,8 +401,17 @@ private void writeProgressToLegacy(StreamOutput out) throws IOException { @Override public int hashCode() { - return Objects.hash(id, state, failureReason, progress, dataCounts, memoryUsage, analysisStats, node, - assignmentExplanation); + return Objects.hash( + id, + state, + failureReason, + progress, + dataCounts, + memoryUsage, + analysisStats, + node, + assignmentExplanation + ); } @Override @@ -404,14 +424,14 @@ public boolean equals(Object obj) { } Stats other = (Stats) obj; return Objects.equals(id, other.id) - && Objects.equals(this.state, other.state) - && Objects.equals(this.failureReason, other.failureReason) - && Objects.equals(this.progress, other.progress) - && Objects.equals(this.dataCounts, other.dataCounts) - && Objects.equals(this.memoryUsage, other.memoryUsage) - && Objects.equals(this.analysisStats, other.analysisStats) - && Objects.equals(this.node, other.node) - && Objects.equals(this.assignmentExplanation, other.assignmentExplanation); + && Objects.equals(this.state, other.state) + && Objects.equals(this.failureReason, other.failureReason) + && Objects.equals(this.progress, other.progress) + && Objects.equals(this.dataCounts, other.dataCounts) + && Objects.equals(this.memoryUsage, other.memoryUsage) + && Objects.equals(this.analysisStats, other.analysisStats) + && Objects.equals(this.node, other.node) + && Objects.equals(this.assignmentExplanation, other.assignmentExplanation); } } @@ -421,8 +441,11 @@ public Response(QueryPage stats) { this(Collections.emptyList(), Collections.emptyList(), stats); } - public Response(List taskFailures, List nodeFailures, - QueryPage stats) { + public Response( + List taskFailures, + List nodeFailures, + QueryPage stats + ) { super(taskFailures, nodeFailures); this.stats = stats; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetDatafeedRunningStateAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetDatafeedRunningStateAction.java index a17f7e58556dc..1d4e9279e7f35 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetDatafeedRunningStateAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetDatafeedRunningStateAction.java @@ -13,9 +13,9 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.tasks.Task; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.tasks.Task; import org.elasticsearch.xpack.core.ml.MlTasks; import java.io.IOException; @@ -26,7 +26,6 @@ import java.util.Set; import java.util.stream.Collectors; - /** * Internal only action to get the current running state of a datafeed */ @@ -119,10 +118,12 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws private final Map datafeedRunningState; public static Response fromResponses(List responses) { - return new Response(responses.stream() - .flatMap(r -> r.datafeedRunningState.entrySet().stream()) - .filter(entry -> entry.getValue() != null) - .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))); + return new Response( + responses.stream() + .flatMap(r -> r.datafeedRunningState.entrySet().stream()) + .filter(entry -> entry.getValue() != null) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)) + ); } public static Response fromTaskAndState(String datafeedId, RunningState runningState) { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetDatafeedsStatsAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetDatafeedsStatsAction.java index 9aeea60a142c7..07f79a8446e0a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetDatafeedsStatsAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetDatafeedsStatsAction.java @@ -11,13 +11,13 @@ import org.elasticsearch.action.support.master.MasterNodeReadRequest; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.node.DiscoveryNode; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.persistent.PersistentTasksCustomMetadata; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.persistent.PersistentTasksCustomMetadata; import org.elasticsearch.xpack.core.action.AbstractGetResourcesResponse; import org.elasticsearch.xpack.core.action.util.QueryPage; import org.elasticsearch.xpack.core.ml.MlTasks; @@ -201,7 +201,8 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.field( TIMING_STATS, timingStats, - new MapParams(Collections.singletonMap(ToXContentParams.INCLUDE_CALCULATED_FIELDS, "true"))); + new MapParams(Collections.singletonMap(ToXContentParams.INCLUDE_CALCULATED_FIELDS, "true")) + ); } if (runningState != null) { builder.field(RUNNING_STATE, runningState); @@ -343,11 +344,11 @@ public Builder setDatafeedRuntimeState(GetDatafeedRunningStateAction.Response da } public Response build(PersistentTasksCustomMetadata tasksInProgress, ClusterState state) { - List stats = statsBuilders.stream().map(statsBuilder-> { + List stats = statsBuilders.stream().map(statsBuilder -> { final String jobId = datafeedToJobId.get(statsBuilder.datafeedId); - DatafeedTimingStats timingStats = jobId == null ? - null : - timingStatsMap.getOrDefault(jobId, new DatafeedTimingStats(jobId)); + DatafeedTimingStats timingStats = jobId == null + ? null + : timingStatsMap.getOrDefault(jobId, new DatafeedTimingStats(jobId)); PersistentTasksCustomMetadata.PersistentTask maybeTask = MlTasks.getDatafeedTask( statsBuilder.datafeedId, tasksInProgress diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetDeploymentStatsAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetDeploymentStatsAction.java index 394d047d17fe3..14c726861a9c4 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetDeploymentStatsAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetDeploymentStatsAction.java @@ -105,9 +105,9 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Request request = (Request) o; - return Objects.equals(deploymentId, request.deploymentId) && - this.allowNoMatch == request.allowNoMatch && - Objects.equals(expandedIds, request.expandedIds); + return Objects.equals(deploymentId, request.deploymentId) + && this.allowNoMatch == request.allowNoMatch + && Objects.equals(expandedIds, request.expandedIds); } @Override @@ -116,7 +116,6 @@ public int hashCode() { } } - public static class Response extends BaseTasksResponse implements ToXContentObject { public static final ParseField DEPLOYMENT_STATS = new ParseField("deployment_stats"); @@ -130,26 +129,32 @@ public static class NodeStats implements ToXContentObject, Writeable { private final Instant lastAccess; private final RoutingStateAndReason routingState; - public static NodeStats forStartedState(DiscoveryNode node, - long inferenceCount, - double avgInferenceTime, - Instant lastAccess) { - return new NodeStats(node, inferenceCount, avgInferenceTime, lastAccess, - new RoutingStateAndReason(RoutingState.STARTED, null)); + public static NodeStats forStartedState( + DiscoveryNode node, + long inferenceCount, + double avgInferenceTime, + Instant lastAccess + ) { + return new NodeStats( + node, + inferenceCount, + avgInferenceTime, + lastAccess, + new RoutingStateAndReason(RoutingState.STARTED, null) + ); } - public static NodeStats forNotStartedState(DiscoveryNode node, - RoutingState state, - String reason) { - return new NodeStats(node, null, null, null, - new RoutingStateAndReason(state, reason)); + public static NodeStats forNotStartedState(DiscoveryNode node, RoutingState state, String reason) { + return new NodeStats(node, null, null, null, new RoutingStateAndReason(state, reason)); } - private NodeStats(DiscoveryNode node, - Long inferenceCount, - Double avgInferenceTime, - Instant lastAccess, - RoutingStateAndReason routingState) { + private NodeStats( + DiscoveryNode node, + Long inferenceCount, + Double avgInferenceTime, + Instant lastAccess, + RoutingStateAndReason routingState + ) { this.node = node; this.inferenceCount = inferenceCount; this.avgInferenceTime = avgInferenceTime; @@ -212,11 +217,11 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; NodeStats that = (NodeStats) o; - return Objects.equals(inferenceCount, that.inferenceCount) && - Objects.equals(that.avgInferenceTime, avgInferenceTime) && - Objects.equals(node, that.node) && - Objects.equals(lastAccess, that.lastAccess) && - Objects.equals(routingState, that.routingState); + return Objects.equals(inferenceCount, that.inferenceCount) + && Objects.equals(that.avgInferenceTime, avgInferenceTime) + && Objects.equals(node, that.node) + && Objects.equals(lastAccess, that.lastAccess) + && Objects.equals(routingState, that.routingState); } @Override @@ -225,15 +230,18 @@ public int hashCode() { } } - private final String modelId; private AllocationState state; private AllocationStatus allocationStatus; private String reason; - @Nullable private final ByteSizeValue modelSize; - @Nullable private final Integer inferenceThreads; - @Nullable private final Integer modelThreads; - @Nullable private final Integer queueCapacity; + @Nullable + private final ByteSizeValue modelSize; + @Nullable + private final Integer inferenceThreads; + @Nullable + private final Integer modelThreads; + @Nullable + private final Integer queueCapacity; private final List nodeStats; public AllocationStats( @@ -342,7 +350,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.field("allocation_status", allocationStatus); } builder.startArray("nodes"); - for (NodeStats nodeStat : nodeStats){ + for (NodeStats nodeStat : nodeStats) { nodeStat.toXContent(builder, params); } builder.endArray(); @@ -368,28 +376,41 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; AllocationStats that = (AllocationStats) o; - return Objects.equals(modelId, that.modelId) && - Objects.equals(modelSize, that.modelSize) && - Objects.equals(inferenceThreads, that.inferenceThreads) && - Objects.equals(modelThreads, that.modelThreads) && - Objects.equals(queueCapacity, that.queueCapacity) && - Objects.equals(state, that.state) && - Objects.equals(reason, that.reason) && - Objects.equals(allocationStatus, that.allocationStatus) && - Objects.equals(nodeStats, that.nodeStats); + return Objects.equals(modelId, that.modelId) + && Objects.equals(modelSize, that.modelSize) + && Objects.equals(inferenceThreads, that.inferenceThreads) + && Objects.equals(modelThreads, that.modelThreads) + && Objects.equals(queueCapacity, that.queueCapacity) + && Objects.equals(state, that.state) + && Objects.equals(reason, that.reason) + && Objects.equals(allocationStatus, that.allocationStatus) + && Objects.equals(nodeStats, that.nodeStats); } @Override public int hashCode() { - return Objects.hash(modelId, modelSize, inferenceThreads, modelThreads, queueCapacity, nodeStats, state, reason, - allocationStatus); + return Objects.hash( + modelId, + modelSize, + inferenceThreads, + modelThreads, + queueCapacity, + nodeStats, + state, + reason, + allocationStatus + ); } } private final QueryPage stats; - public Response(List taskFailures, List nodeFailures, - List stats, long count) { + public Response( + List taskFailures, + List nodeFailures, + List stats, + long count + ) { super(taskFailures, nodeFailures); this.stats = new QueryPage<>(stats, count, DEPLOYMENT_STATS); } @@ -471,7 +492,9 @@ public static GetDeploymentStatsAction.Response addFailedRoutes( GetDeploymentStatsAction.Response.AllocationStats.NodeStats.forNotStartedState( nodeStat.getNode(), stateAndReason.getState(), - stateAndReason.getReason())); + stateAndReason.getReason() + ) + ); } else { updatedNodeStats.add(nodeStat); } @@ -486,7 +509,9 @@ public static GetDeploymentStatsAction.Response addFailedRoutes( GetDeploymentStatsAction.Response.AllocationStats.NodeStats.forNotStartedState( nodes.get(nodeRoutingState.getKey()), nodeRoutingState.getValue().getState(), - nodeRoutingState.getValue().getReason())); + nodeRoutingState.getValue().getReason() + ) + ); } } @@ -509,34 +534,37 @@ public static GetDeploymentStatsAction.Response addFailedRoutes( // Merge any models in the non-started that were not in the task responses for (var nonStartedEntries : nonStartedModelRoutes.entrySet()) { String modelId = nonStartedEntries.getKey(); - if (tasksResponse.getStats().results() - .stream() - .anyMatch(e -> modelId.equals(e.getModelId())) == false) { + if (tasksResponse.getStats().results().stream().anyMatch(e -> modelId.equals(e.getModelId())) == false) { // no tasks for this model so build the allocation stats from the non-started states List nodeStats = new ArrayList<>(); for (var routingEntry : nonStartedEntries.getValue().entrySet()) { - nodeStats.add(AllocationStats.NodeStats.forNotStartedState( + nodeStats.add( + AllocationStats.NodeStats.forNotStartedState( nodes.get(routingEntry.getKey()), routingEntry.getValue().getState(), - routingEntry.getValue().getReason())); + routingEntry.getValue().getReason() + ) + ); } nodeStats.sort(Comparator.comparing(n -> n.getNode().getId())); - updatedAllocationStats.add(new GetDeploymentStatsAction.Response.AllocationStats( - modelId, null, null, null, null, nodeStats) + updatedAllocationStats.add( + new GetDeploymentStatsAction.Response.AllocationStats(modelId, null, null, null, null, nodeStats) ); } } updatedAllocationStats.sort(Comparator.comparing(GetDeploymentStatsAction.Response.AllocationStats::getModelId)); - return new GetDeploymentStatsAction.Response(tasksResponse.getTaskFailures(), + return new GetDeploymentStatsAction.Response( + tasksResponse.getTaskFailures(), tasksResponse.getNodeFailures(), updatedAllocationStats, - updatedAllocationStats.size()); + updatedAllocationStats.size() + ); } } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetFiltersAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetFiltersAction.java index 0fd34956c8440..d49cf36ce091a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetFiltersAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetFiltersAction.java @@ -17,7 +17,6 @@ import java.io.IOException; - public class GetFiltersAction extends ActionType { public static final GetFiltersAction INSTANCE = new GetFiltersAction(); @@ -74,4 +73,3 @@ protected Reader getReader() { } } - diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetInfluencersAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetInfluencersAction.java index 44de64454c493..a11484c48a981 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetInfluencersAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetInfluencersAction.java @@ -9,10 +9,10 @@ import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.ActionType; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -74,8 +74,7 @@ public static Request parseRequest(String jobId, XContentParser parser) { private String sort = Influencer.INFLUENCER_SCORE.getPreferredName(); private boolean descending = true; - public Request() { - } + public Request() {} public Request(StreamInput in) throws IOException { super(in); @@ -200,20 +199,20 @@ public boolean equals(Object obj) { return false; } Request other = (Request) obj; - return Objects.equals(jobId, other.jobId) && Objects.equals(start, other.start) - && Objects.equals(end, other.end) - && Objects.equals(excludeInterim, other.excludeInterim) - && Objects.equals(pageParams, other.pageParams) - && Objects.equals(influencerScore, other.influencerScore) - && Objects.equals(descending, other.descending) - && Objects.equals(sort, other.sort); + return Objects.equals(jobId, other.jobId) + && Objects.equals(start, other.start) + && Objects.equals(end, other.end) + && Objects.equals(excludeInterim, other.excludeInterim) + && Objects.equals(pageParams, other.pageParams) + && Objects.equals(influencerScore, other.influencerScore) + && Objects.equals(descending, other.descending) + && Objects.equals(sort, other.sort); } } public static class Response extends AbstractGetResourcesResponse implements ToXContentObject { - public Response() { - } + public Response() {} public Response(StreamInput in) throws IOException { super(in); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetJobsAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetJobsAction.java index bdbba36d5ed7a..d9c86c24746ee 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetJobsAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetJobsAction.java @@ -115,5 +115,4 @@ protected Reader getReader() { } } - } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetJobsStatsAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetJobsStatsAction.java index 8d603ac4d9baf..6d676cfd9b832 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetJobsStatsAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetJobsStatsAction.java @@ -13,15 +13,15 @@ import org.elasticsearch.action.support.tasks.BaseTasksRequest; import org.elasticsearch.action.support.tasks.BaseTasksResponse; import org.elasticsearch.cluster.node.DiscoveryNode; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.tasks.Task; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.tasks.Task; import org.elasticsearch.xpack.core.action.util.QueryPage; import org.elasticsearch.xpack.core.ml.job.config.Job; import org.elasticsearch.xpack.core.ml.job.config.JobState; @@ -88,9 +88,13 @@ public void writeTo(StreamOutput out) throws IOException { out.writeBoolean(allowNoMatch); } - public List getExpandedJobsIds() { return expandedJobsIds; } + public List getExpandedJobsIds() { + return expandedJobsIds; + } - public void setExpandedJobsIds(List expandedJobsIds) { this.expandedJobsIds = expandedJobsIds; } + public void setExpandedJobsIds(List expandedJobsIds) { + this.expandedJobsIds = expandedJobsIds; + } public void setAllowNoMatch(boolean allowNoMatch) { this.allowNoMatch = allowNoMatch; @@ -129,8 +133,8 @@ public boolean equals(Object obj) { } Request other = (Request) obj; return Objects.equals(jobId, other.jobId) - && Objects.equals(allowNoMatch, other.allowNoMatch) - && Objects.equals(getTimeout(), other.getTimeout()); + && Objects.equals(allowNoMatch, other.allowNoMatch) + && Objects.equals(getTimeout(), other.getTimeout()); } } @@ -153,9 +157,17 @@ public static class JobStats implements ToXContentObject, Writeable { @Nullable private final TimingStats timingStats; - public JobStats(String jobId, DataCounts dataCounts, @Nullable ModelSizeStats modelSizeStats, - @Nullable ForecastStats forecastStats, JobState state, @Nullable DiscoveryNode node, - @Nullable String assignmentExplanation, @Nullable TimeValue openTime, @Nullable TimingStats timingStats) { + public JobStats( + String jobId, + DataCounts dataCounts, + @Nullable ModelSizeStats modelSizeStats, + @Nullable ForecastStats forecastStats, + JobState state, + @Nullable DiscoveryNode node, + @Nullable String assignmentExplanation, + @Nullable TimeValue openTime, + @Nullable TimingStats timingStats + ) { this.jobId = Objects.requireNonNull(jobId); this.dataCounts = Objects.requireNonNull(dataCounts); this.modelSizeStats = modelSizeStats; @@ -260,7 +272,8 @@ public XContentBuilder toUnwrappedXContent(XContentBuilder builder) throws IOExc builder.field( TIMING_STATS, timingStats, - new MapParams(Collections.singletonMap(ToXContentParams.INCLUDE_CALCULATED_FIELDS, "true"))); + new MapParams(Collections.singletonMap(ToXContentParams.INCLUDE_CALCULATED_FIELDS, "true")) + ); } return builder; } @@ -281,7 +294,16 @@ public void writeTo(StreamOutput out) throws IOException { @Override public int hashCode() { return Objects.hash( - jobId, dataCounts, modelSizeStats, forecastStats, state, node, assignmentExplanation, openTime, timingStats); + jobId, + dataCounts, + modelSizeStats, + forecastStats, + state, + node, + assignmentExplanation, + openTime, + timingStats + ); } @Override @@ -312,8 +334,11 @@ public Response(QueryPage jobsStats) { this.jobsStats = jobsStats; } - public Response(List taskFailures, List nodeFailures, - QueryPage jobsStats) { + public Response( + List taskFailures, + List nodeFailures, + QueryPage jobsStats + ) { super(taskFailures, nodeFailures); this.jobsStats = jobsStats; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetModelSnapshotsAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetModelSnapshotsAction.java index 615823f62dce6..6135eae0e23f6 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetModelSnapshotsAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetModelSnapshotsAction.java @@ -9,11 +9,11 @@ import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.ActionType; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -75,8 +75,7 @@ public static Request parseRequest(String jobId, String snapshotId, XContentPars private boolean desc = true; private PageParams pageParams = new PageParams(); - public Request() { - } + public Request() {} public Request(StreamInput in) throws IOException { super(in); @@ -200,11 +199,11 @@ public boolean equals(Object obj) { } Request other = (Request) obj; return Objects.equals(jobId, other.jobId) - && Objects.equals(snapshotId, other.snapshotId) - && Objects.equals(start, other.start) - && Objects.equals(end, other.end) - && Objects.equals(sort, other.sort) - && Objects.equals(desc, other.desc); + && Objects.equals(snapshotId, other.snapshotId) + && Objects.equals(start, other.start) + && Objects.equals(end, other.end) + && Objects.equals(sort, other.sort) + && Objects.equals(desc, other.desc); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetOverallBucketsAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetOverallBucketsAction.java index f17c951ef8da3..f0264f307f6df 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetOverallBucketsAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetOverallBucketsAction.java @@ -10,16 +10,16 @@ import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.ActionType; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.time.DateMathParser; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.index.mapper.DateFieldMapper; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.index.mapper.DateFieldMapper; import org.elasticsearch.xpack.core.action.AbstractGetResourcesResponse; import org.elasticsearch.xpack.core.action.util.QueryPage; import org.elasticsearch.xpack.core.ml.job.config.Job; @@ -74,10 +74,11 @@ public static class Request extends ActionRequest implements ToXContentObject { PARSER.declareString(Request::setBucketSpan, BUCKET_SPAN); PARSER.declareDouble(Request::setOverallScore, OVERALL_SCORE); PARSER.declareBoolean(Request::setExcludeInterim, EXCLUDE_INTERIM); - PARSER.declareString((request, startTime) -> request.setStart(parseDateOrThrow( - startTime, START, System::currentTimeMillis)), START); - PARSER.declareString((request, endTime) -> request.setEnd(parseDateOrThrow( - endTime, END, System::currentTimeMillis)), END); + PARSER.declareString( + (request, startTime) -> request.setStart(parseDateOrThrow(startTime, START, System::currentTimeMillis)), + START + ); + PARSER.declareString((request, endTime) -> request.setEnd(parseDateOrThrow(endTime, END, System::currentTimeMillis)), END); PARSER.declareBoolean(Request::setAllowNoMatch, ALLOW_NO_MATCH); } @@ -109,8 +110,7 @@ public static Request parseRequest(String jobId, XContentParser parser) { private Long end; private boolean allowNoMatch = true; - public Request() { - } + public Request() {} public Request(StreamInput in) throws IOException { super(in); @@ -256,14 +256,14 @@ public boolean equals(Object other) { return false; } Request that = (Request) other; - return Objects.equals(jobId, that.jobId) && - this.topN == that.topN && - Objects.equals(bucketSpan, that.bucketSpan) && - this.excludeInterim == that.excludeInterim && - this.overallScore == that.overallScore && - Objects.equals(start, that.start) && - Objects.equals(end, that.end) && - this.allowNoMatch == that.allowNoMatch; + return Objects.equals(jobId, that.jobId) + && this.topN == that.topN + && Objects.equals(bucketSpan, that.bucketSpan) + && this.excludeInterim == that.excludeInterim + && this.overallScore == that.overallScore + && Objects.equals(start, that.start) + && Objects.equals(end, that.end) + && this.allowNoMatch == that.allowNoMatch; } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetRecordsAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetRecordsAction.java index 2938a59ff7856..05691448f33ee 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetRecordsAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetRecordsAction.java @@ -9,10 +9,10 @@ import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.ActionType; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -74,8 +74,7 @@ public static Request parseRequest(String jobId, XContentParser parser) { private String sort = RECORD_SCORE_FILTER.getPreferredName(); private boolean descending = true; - public Request() { - } + public Request() {} public Request(StreamInput in) throws IOException { super(in); @@ -132,6 +131,7 @@ public void setExcludeInterim(boolean excludeInterim) { public void setPageParams(PageParams pageParams) { this.pageParams = pageParams; } + public PageParams getPageParams() { return pageParams; } @@ -199,14 +199,14 @@ public boolean equals(Object obj) { return false; } Request other = (Request) obj; - return Objects.equals(jobId, other.jobId) && - Objects.equals(start, other.start) && - Objects.equals(end, other.end) && - Objects.equals(sort, other.sort) && - Objects.equals(descending, other.descending) && - Objects.equals(recordScoreFilter, other.recordScoreFilter) && - Objects.equals(excludeInterim, other.excludeInterim) && - Objects.equals(pageParams, other.pageParams); + return Objects.equals(jobId, other.jobId) + && Objects.equals(start, other.start) + && Objects.equals(end, other.end) + && Objects.equals(sort, other.sort) + && Objects.equals(descending, other.descending) + && Objects.equals(recordScoreFilter, other.recordScoreFilter) + && Objects.equals(excludeInterim, other.excludeInterim) + && Objects.equals(pageParams, other.pageParams); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetTrainedModelsAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetTrainedModelsAction.java index 3b1459b43d1fc..943f0d81af992 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetTrainedModelsAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetTrainedModelsAction.java @@ -8,11 +8,11 @@ import org.elasticsearch.Version; import org.elasticsearch.action.ActionType; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.util.set.Sets; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xpack.core.action.AbstractGetResourcesRequest; import org.elasticsearch.xpack.core.action.AbstractGetResourcesResponse; import org.elasticsearch.xpack.core.action.util.QueryPage; @@ -26,7 +26,6 @@ import java.util.Objects; import java.util.Set; - public class GetTrainedModelsAction extends ActionType { public static final GetTrainedModelsAction INSTANCE = new GetTrainedModelsAction(); @@ -73,7 +72,8 @@ public Includes(Set includes) { throw ExceptionsHelper.badRequestException( "unknown [include] parameters {}. Valid options are {}", unknownIncludes, - KNOWN_INCLUDES); + KNOWN_INCLUDES + ); } } @@ -227,8 +227,7 @@ public static class Builder { private long totalCount; private List configs = Collections.emptyList(); - private Builder() { - } + private Builder() {} public Builder setTotalCount(long totalCount) { this.totalCount = totalCount; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetTrainedModelsStatsAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetTrainedModelsStatsAction.java index f82f55c689f95..d892843799db5 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetTrainedModelsStatsAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetTrainedModelsStatsAction.java @@ -8,13 +8,13 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.action.ActionType; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.ingest.IngestStats; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.ingest.IngestStats; import org.elasticsearch.xpack.core.action.AbstractGetResourcesRequest; import org.elasticsearch.xpack.core.action.AbstractGetResourcesResponse; import org.elasticsearch.xpack.core.action.util.QueryPage; @@ -75,9 +75,11 @@ public static class TrainedModelStats implements ToXContentObject, Writeable { private final InferenceStats inferenceStats; private final int pipelineCount; - private static final IngestStats EMPTY_INGEST_STATS = new IngestStats(new IngestStats.Stats(0, 0, 0, 0), + private static final IngestStats EMPTY_INGEST_STATS = new IngestStats( + new IngestStats.Stats(0, 0, 0, 0), Collections.emptyList(), - Collections.emptyMap()); + Collections.emptyMap() + ); public TrainedModelStats(String modelId, IngestStats ingestStats, int pipelineCount, InferenceStats inferenceStats) { this.modelId = Objects.requireNonNull(modelId); @@ -204,13 +206,14 @@ public Response build() { expandedIdsWithAliases.keySet().forEach(id -> { IngestStats ingestStats = ingestStatsMap.get(id); InferenceStats inferenceStats = inferenceStatsMap.get(id); - trainedModelStats.add(new TrainedModelStats( - id, - ingestStats, - ingestStats == null ? - 0 : - ingestStats.getPipelineStats().size(), - inferenceStats)); + trainedModelStats.add( + new TrainedModelStats( + id, + ingestStats, + ingestStats == null ? 0 : ingestStats.getPipelineStats().size(), + inferenceStats + ) + ); }); trainedModelStats.sort(Comparator.comparing(TrainedModelStats::getModelId)); return new Response(new QueryPage<>(trainedModelStats, totalModelCount, RESULTS_FIELD)); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/InferTrainedModelDeploymentAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/InferTrainedModelDeploymentAction.java index 83d1e8bdf4126..5b9619076f380 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/InferTrainedModelDeploymentAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/InferTrainedModelDeploymentAction.java @@ -133,17 +133,14 @@ public TimeValue getTimeout() { public ActionRequestValidationException validate() { ActionRequestValidationException validationException = super.validate(); if (docs == null) { - validationException = addValidationError("[" + DOCS.getPreferredName() + "] must not be null", - validationException); + validationException = addValidationError("[" + DOCS.getPreferredName() + "] must not be null", validationException); } else { if (docs.isEmpty()) { - validationException = addValidationError("at least one document is required", - validationException); + validationException = addValidationError("at least one document is required", validationException); } if (docs.size() > 1) { // TODO support multiple docs - validationException = addValidationError("multiple documents are not supported", - validationException); + validationException = addValidationError("multiple documents are not supported", validationException); } } return validationException; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/InternalInferModelAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/InternalInferModelAction.java index 57b93ca29f048..362ac370057de 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/InternalInferModelAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/InternalInferModelAction.java @@ -49,24 +49,25 @@ public Request(String modelId, boolean previouslyLicensed) { this(modelId, Collections.emptyList(), RegressionConfigUpdate.EMPTY_PARAMS, previouslyLicensed); } - public Request(String modelId, - List> objectsToInfer, - InferenceConfigUpdate inferenceConfig, - boolean previouslyLicensed) { + public Request( + String modelId, + List> objectsToInfer, + InferenceConfigUpdate inferenceConfig, + boolean previouslyLicensed + ) { this.modelId = ExceptionsHelper.requireNonNull(modelId, TrainedModelConfig.MODEL_ID); this.objectsToInfer = Collections.unmodifiableList(ExceptionsHelper.requireNonNull(objectsToInfer, "objects_to_infer")); this.update = ExceptionsHelper.requireNonNull(inferenceConfig, "inference_config"); this.previouslyLicensed = previouslyLicensed; } - public Request(String modelId, - Map objectToInfer, - InferenceConfigUpdate update, - boolean previouslyLicensed) { - this(modelId, + public Request(String modelId, Map objectToInfer, InferenceConfigUpdate update, boolean previouslyLicensed) { + this( + modelId, Collections.singletonList(ExceptionsHelper.requireNonNull(objectToInfer, "objects_to_infer")), update, - previouslyLicensed); + previouslyLicensed + ); } public Request(StreamInput in) throws IOException { @@ -78,7 +79,7 @@ public Request(StreamInput in) throws IOException { } else { InferenceConfig oldConfig = in.readNamedWriteable(InferenceConfig.class); if (oldConfig instanceof RegressionConfig) { - this.update = RegressionConfigUpdate.fromConfig((RegressionConfig)oldConfig); + this.update = RegressionConfigUpdate.fromConfig((RegressionConfig) oldConfig); } else if (oldConfig instanceof ClassificationConfig) { this.update = ClassificationConfigUpdate.fromConfig((ClassificationConfig) oldConfig); } else { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/IsolateDatafeedAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/IsolateDatafeedAction.java index e8533def32d62..630c7b60c961b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/IsolateDatafeedAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/IsolateDatafeedAction.java @@ -6,18 +6,18 @@ */ package org.elasticsearch.xpack.core.ml.action; -import org.elasticsearch.action.ActionType; import org.elasticsearch.action.ActionRequestValidationException; +import org.elasticsearch.action.ActionType; import org.elasticsearch.action.support.tasks.BaseTasksRequest; import org.elasticsearch.action.support.tasks.BaseTasksResponse; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.tasks.Task; import org.elasticsearch.xcontent.ObjectParser; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.tasks.Task; import org.elasticsearch.xpack.core.ml.MlTasks; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfig; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; @@ -68,8 +68,7 @@ public Request(String datafeedId) { this.datafeedId = ExceptionsHelper.requireNonNull(datafeedId, DatafeedConfig.ID.getPreferredName()); } - public Request() { - } + public Request() {} public Request(StreamInput in) throws IOException { super(in); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/JobTaskRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/JobTaskRequest.java index 9357ac149e1ef..0e4cef62dc20f 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/JobTaskRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/JobTaskRequest.java @@ -19,8 +19,7 @@ public class JobTaskRequest> extends BaseTasksReques String jobId; - JobTaskRequest() { - } + JobTaskRequest() {} JobTaskRequest(StreamInput in) throws IOException { super(in); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/NodeAcknowledgedResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/NodeAcknowledgedResponse.java index e4a4dd5f1bf6b..e04330632210e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/NodeAcknowledgedResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/NodeAcknowledgedResponse.java @@ -55,8 +55,7 @@ public boolean equals(Object o) { return false; } NodeAcknowledgedResponse that = (NodeAcknowledgedResponse) o; - return isAcknowledged() == that.isAcknowledged() - && Objects.equals(node, that.node); + return isAcknowledged() == that.isAcknowledged() && Objects.equals(node, that.node); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/OpenJobAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/OpenJobAction.java index c2274716b272e..e6a8a56013621 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/OpenJobAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/OpenJobAction.java @@ -10,19 +10,19 @@ import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.ActionType; import org.elasticsearch.action.support.master.MasterNodeRequest; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.persistent.PersistentTaskParams; +import org.elasticsearch.tasks.Task; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.persistent.PersistentTaskParams; -import org.elasticsearch.tasks.Task; import org.elasticsearch.xpack.core.ml.MachineLearningField; import org.elasticsearch.xpack.core.ml.MlTasks; import org.elasticsearch.xpack.core.ml.job.config.Job; @@ -122,8 +122,7 @@ public static class JobParams implements PersistentTaskParams, MlTaskParams { public static final ObjectParser PARSER = new ObjectParser<>(MlTasks.JOB_TASK_NAME, true, JobParams::new); static { PARSER.declareString(JobParams::setJobId, Job.ID); - PARSER.declareString((params, val) -> - params.setTimeout(TimeValue.parseTimeValue(val, TIMEOUT.getPreferredName())), TIMEOUT); + PARSER.declareString((params, val) -> params.setTimeout(TimeValue.parseTimeValue(val, TIMEOUT.getPreferredName())), TIMEOUT); PARSER.declareObject(JobParams::setJob, (p, c) -> Job.LENIENT_PARSER.apply(p, c).build(), JOB); } @@ -140,13 +139,12 @@ public static JobParams parseRequest(String jobId, XContentParser parser) { } private String jobId; - // A big state can take a while to restore. For symmetry with the _close endpoint any + // A big state can take a while to restore. For symmetry with the _close endpoint any // changes here should be reflected there too. private TimeValue timeout = MachineLearningField.STATE_PERSIST_RESTORE_TIMEOUT; private Job job; - JobParams() { - } + JobParams() {} public JobParams(String jobId) { this.jobId = ExceptionsHelper.requireNonNull(jobId, Job.ID.getPreferredName()); @@ -222,9 +220,7 @@ public boolean equals(Object obj) { return false; } OpenJobAction.JobParams other = (OpenJobAction.JobParams) obj; - return Objects.equals(jobId, other.jobId) && - Objects.equals(timeout, other.timeout) && - Objects.equals(job, other.job); + return Objects.equals(jobId, other.jobId) && Objects.equals(timeout, other.timeout) && Objects.equals(job, other.job); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PostCalendarEventsAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PostCalendarEventsAction.java index 8ae627d5dbe80..b396e1be1af1f 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PostCalendarEventsAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PostCalendarEventsAction.java @@ -10,10 +10,10 @@ import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.ActionResponse; import org.elasticsearch.action.ActionType; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -51,8 +51,9 @@ public static Request parseRequest(String calendarId, XContentParser parser) thr for (ScheduledEvent.Builder event : events) { if (event.getCalendarId() != null && event.getCalendarId().equals(calendarId) == false) { - throw ExceptionsHelper.badRequestException(Messages.getMessage(Messages.INCONSISTENT_ID, - Calendar.ID.getPreferredName(), event.getCalendarId(), calendarId)); + throw ExceptionsHelper.badRequestException( + Messages.getMessage(Messages.INCONSISTENT_ID, Calendar.ID.getPreferredName(), event.getCalendarId(), calendarId) + ); } // Set the calendar Id in case it is null event.calendarId(calendarId); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PostDataAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PostDataAction.java index f9c841a5a23d6..f149ded0a4725 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PostDataAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PostDataAction.java @@ -8,16 +8,16 @@ import org.elasticsearch.action.ActionType; import org.elasticsearch.action.support.tasks.BaseTasksResponse; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.xcontent.StatusToXContentObject; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.ml.job.config.DataDescription; import org.elasticsearch.xpack.core.ml.job.process.autodetect.state.DataCounts; @@ -159,7 +159,9 @@ public void setDataDescription(DataDescription dataDescription) { this.dataDescription = dataDescription; } - public BytesReference getContent() { return content; } + public BytesReference getContent() { + return content; + } public XContentType getXContentType() { return xContentType; @@ -187,13 +189,12 @@ public boolean equals(Object obj) { Request other = (Request) obj; // content stream not included - return Objects.equals(jobId, other.jobId) && - Objects.equals(resetStart, other.resetStart) && - Objects.equals(resetEnd, other.resetEnd) && - Objects.equals(dataDescription, other.dataDescription) && - Objects.equals(xContentType, other.xContentType); + return Objects.equals(jobId, other.jobId) + && Objects.equals(resetStart, other.resetStart) + && Objects.equals(resetEnd, other.resetEnd) + && Objects.equals(dataDescription, other.dataDescription) + && Objects.equals(xContentType, other.xContentType); } } - } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PreviewDataFrameAnalyticsAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PreviewDataFrameAnalyticsAction.java index a0137a1866d0c..fb978ebb813ad 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PreviewDataFrameAnalyticsAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PreviewDataFrameAnalyticsAction.java @@ -10,11 +10,11 @@ import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.ActionResponse; import org.elasticsearch.action.ActionType; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -26,7 +26,6 @@ import java.util.Map; import java.util.Objects; - public class PreviewDataFrameAnalyticsAction extends ActionType { public static final PreviewDataFrameAnalyticsAction INSTANCE = new PreviewDataFrameAnalyticsAction(); @@ -42,10 +41,7 @@ public static class Request extends ActionRequest { private final DataFrameAnalyticsConfig config; - static final ObjectParser PARSER = new ObjectParser<>( - "preview_data_frame_analytics_response", - Request.Builder::new - ); + static final ObjectParser PARSER = new ObjectParser<>("preview_data_frame_analytics_response", Request.Builder::new); static { PARSER.declareObject(Request.Builder::setConfig, DataFrameAnalyticsConfig.STRICT_PARSER::apply, CONFIG); } @@ -78,7 +74,6 @@ public void writeTo(StreamOutput out) throws IOException { config.writeTo(out); } - @Override public boolean equals(Object o) { if (this == o) return true; @@ -121,10 +116,10 @@ public static class Response extends ActionResponse implements ToXContentObject public static final ParseField FEATURE_VALUES = new ParseField("feature_values"); @SuppressWarnings("unchecked") - static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>( - TYPE.getPreferredName(), - args -> new Response((List>) args[0])); + static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + TYPE.getPreferredName(), + args -> new Response((List>) args[0]) + ); static { PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), (p, c) -> p.map(), FEATURE_VALUES); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PreviewDatafeedAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PreviewDatafeedAction.java index d9ec4761375a0..fa521a7e3a4af 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PreviewDatafeedAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PreviewDatafeedAction.java @@ -10,13 +10,13 @@ import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.ActionResponse; import org.elasticsearch.action.ActionType; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -45,10 +45,7 @@ public static class Request extends ActionRequest implements ToXContentObject { public static final ParseField DATAFEED_CONFIG = new ParseField("datafeed_config"); public static final ParseField JOB_CONFIG = new ParseField("job_config"); - private static final ObjectParser PARSER = new ObjectParser<>( - "preview_datafeed_action", - Request.Builder::new - ); + private static final ObjectParser PARSER = new ObjectParser<>("preview_datafeed_action", Request.Builder::new); static { PARSER.declareObject(Builder::setDatafeedBuilder, DatafeedConfig.STRICT_PARSER, DATAFEED_CONFIG); PARSER.declareObject(Builder::setJobBuilder, Job.STRICT_PARSER, JOB_CONFIG); @@ -198,9 +195,9 @@ public Request build() { "[datafeed_id] cannot be supplied when either [job_config] or [datafeed_config] is present" ); } - return datafeedId != null ? - new Request(datafeedId) : - new Request(datafeedBuilder == null ? null : datafeedBuilder.build(), jobBuilder); + return datafeedId != null + ? new Request(datafeedId) + : new Request(datafeedBuilder == null ? null : datafeedBuilder.build(), jobBuilder); } } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PutCalendarAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PutCalendarAction.java index 5c16e95fe5987..066c7aad201f5 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PutCalendarAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PutCalendarAction.java @@ -42,8 +42,9 @@ public static Request parseRequest(String calendarId, XContentParser parser) { builder.setId(calendarId); } else if (Strings.isNullOrEmpty(calendarId) == false && calendarId.equals(builder.getId()) == false) { // If we have both URI and body filter ID, they must be identical - throw new IllegalArgumentException(Messages.getMessage(Messages.INCONSISTENT_ID, Calendar.ID.getPreferredName(), - builder.getId(), calendarId)); + throw new IllegalArgumentException( + Messages.getMessage(Messages.INCONSISTENT_ID, Calendar.ID.getPreferredName(), builder.getId(), calendarId) + ); } return new Request(builder.build()); } @@ -67,19 +68,19 @@ public Calendar getCalendar() { public ActionRequestValidationException validate() { ActionRequestValidationException validationException = null; if ("_all".equals(calendar.getId())) { - validationException = - addValidationError("Cannot create a Calendar with the reserved name [_all]", - validationException); + validationException = addValidationError("Cannot create a Calendar with the reserved name [_all]", validationException); } if (MlStrings.isValidId(calendar.getId()) == false) { - validationException = addValidationError(Messages.getMessage( - Messages.INVALID_ID, Calendar.ID.getPreferredName(), calendar.getId()), - validationException); + validationException = addValidationError( + Messages.getMessage(Messages.INVALID_ID, Calendar.ID.getPreferredName(), calendar.getId()), + validationException + ); } if (MlStrings.hasValidLengthForId(calendar.getId()) == false) { - validationException = addValidationError(Messages.getMessage( - Messages.JOB_CONFIG_ID_TOO_LONG, MlStrings.ID_LENGTH_LIMIT), - validationException); + validationException = addValidationError( + Messages.getMessage(Messages.JOB_CONFIG_ID_TOO_LONG, MlStrings.ID_LENGTH_LIMIT), + validationException + ); } return validationException; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PutDataFrameAnalyticsAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PutDataFrameAnalyticsAction.java index 8c630d4767e95..a2004b839d8c8 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PutDataFrameAnalyticsAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PutDataFrameAnalyticsAction.java @@ -46,8 +46,9 @@ public static Request parseRequest(String id, XContentParser parser) { config.setId(id); } else if (Strings.isNullOrEmpty(id) == false && id.equals(config.getId()) == false) { // If we have both URI and body ID, they must be identical - throw new IllegalArgumentException(Messages.getMessage(Messages.INCONSISTENT_ID, DataFrameAnalyticsConfig.ID, - config.getId(), id)); + throw new IllegalArgumentException( + Messages.getMessage(Messages.INCONSISTENT_ID, DataFrameAnalyticsConfig.ID, config.getId(), id) + ); } return new PutDataFrameAnalyticsAction.Request(config.build()); @@ -95,30 +96,46 @@ public ActionRequestValidationException validate() { return error; } - private ActionRequestValidationException checkConfigIdIsValid(DataFrameAnalyticsConfig config, - ActionRequestValidationException error) { + private ActionRequestValidationException checkConfigIdIsValid( + DataFrameAnalyticsConfig config, + ActionRequestValidationException error + ) { if (MlStrings.isValidId(config.getId()) == false) { - error = ValidateActions.addValidationError(Messages.getMessage(Messages.INVALID_ID, DataFrameAnalyticsConfig.ID, - config.getId()), error); + error = ValidateActions.addValidationError( + Messages.getMessage(Messages.INVALID_ID, DataFrameAnalyticsConfig.ID, config.getId()), + error + ); } if (MlStrings.hasValidLengthForId(config.getId()) == false) { - error = ValidateActions.addValidationError(Messages.getMessage(Messages.ID_TOO_LONG, DataFrameAnalyticsConfig.ID, - config.getId(), MlStrings.ID_LENGTH_LIMIT), error); + error = ValidateActions.addValidationError( + Messages.getMessage(Messages.ID_TOO_LONG, DataFrameAnalyticsConfig.ID, config.getId(), MlStrings.ID_LENGTH_LIMIT), + error + ); } return error; } private ActionRequestValidationException checkNoIncludedAnalyzedFieldsAreExcludedBySourceFiltering( - DataFrameAnalyticsConfig config, ActionRequestValidationException error) { + DataFrameAnalyticsConfig config, + ActionRequestValidationException error + ) { if (config.getAnalyzedFields() == null) { return error; } for (String analyzedInclude : config.getAnalyzedFields().includes()) { if (config.getSource().isFieldExcluded(analyzedInclude)) { - return ValidateActions.addValidationError("field [" + analyzedInclude + "] is included in [" - + DataFrameAnalyticsConfig.ANALYZED_FIELDS.getPreferredName() + "] but not in [" - + DataFrameAnalyticsConfig.SOURCE.getPreferredName() + "." - + DataFrameAnalyticsSource._SOURCE.getPreferredName() + "]", error); + return ValidateActions.addValidationError( + "field [" + + analyzedInclude + + "] is included in [" + + DataFrameAnalyticsConfig.ANALYZED_FIELDS.getPreferredName() + + "] but not in [" + + DataFrameAnalyticsConfig.SOURCE.getPreferredName() + + "." + + DataFrameAnalyticsSource._SOURCE.getPreferredName() + + "]", + error + ); } } return error; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PutFilterAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PutFilterAction.java index 7cf7ed93f9368..b2d3196824036 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PutFilterAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PutFilterAction.java @@ -23,7 +23,6 @@ import java.io.IOException; import java.util.Objects; - public class PutFilterAction extends ActionType { public static final PutFilterAction INSTANCE = new PutFilterAction(); @@ -41,8 +40,9 @@ public static Request parseRequest(String filterId, XContentParser parser) { filter.setId(filterId); } else if (Strings.isNullOrEmpty(filterId) == false && filterId.equals(filter.getId()) == false) { // If we have both URI and body filter ID, they must be identical - throw new IllegalArgumentException(Messages.getMessage(Messages.INCONSISTENT_ID, MlFilter.ID.getPreferredName(), - filter.getId(), filterId)); + throw new IllegalArgumentException( + Messages.getMessage(Messages.INCONSISTENT_ID, MlFilter.ID.getPreferredName(), filter.getId(), filterId) + ); } return new Request(filter.build()); } @@ -101,8 +101,7 @@ public static class Response extends ActionResponse implements ToXContentObject private MlFilter filter; - Response() { - } + Response() {} Response(StreamInput in) throws IOException { super(in); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PutJobAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PutJobAction.java index 9a96d5bd25335..ad363e0984ddc 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PutJobAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PutJobAction.java @@ -41,8 +41,9 @@ public static Request parseRequest(String jobId, XContentParser parser, IndicesO jobBuilder.setId(jobId); } else if (Strings.isNullOrEmpty(jobId) == false && jobId.equals(jobBuilder.getId()) == false) { // If we have both URI and body jobBuilder ID, they must be identical - throw new IllegalArgumentException(Messages.getMessage(Messages.INCONSISTENT_ID, Job.ID.getPreferredName(), - jobBuilder.getId(), jobId)); + throw new IllegalArgumentException( + Messages.getMessage(Messages.INCONSISTENT_ID, Job.ID.getPreferredName(), jobBuilder.getId(), jobId) + ); } jobBuilder.setDatafeedIndicesOptionsIfRequired(indicesOptions); return new Request(jobBuilder); @@ -61,8 +62,9 @@ public Request(Job.Builder jobBuilder) { // Some fields cannot be set at create time List invalidJobCreationSettings = jobBuilder.invalidCreateTimeSettings(); if (invalidJobCreationSettings.isEmpty() == false) { - throw new IllegalArgumentException(Messages.getMessage(Messages.JOB_CONFIG_INVALID_CREATE_SETTINGS, - String.join(",", invalidJobCreationSettings))); + throw new IllegalArgumentException( + Messages.getMessage(Messages.JOB_CONFIG_INVALID_CREATE_SETTINGS, String.join(",", invalidJobCreationSettings)) + ); } this.jobBuilder = jobBuilder; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PutTrainedModelAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PutTrainedModelAction.java index 515a520f97f14..7bbd3c855bcf9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PutTrainedModelAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PutTrainedModelAction.java @@ -24,12 +24,12 @@ import static org.elasticsearch.xpack.core.ml.inference.TrainedModelConfig.ESTIMATED_HEAP_MEMORY_USAGE_BYTES; - public class PutTrainedModelAction extends ActionType { public static final String DEFER_DEFINITION_DECOMPRESSION = "defer_definition_decompression"; public static final PutTrainedModelAction INSTANCE = new PutTrainedModelAction(); public static final String NAME = "cluster:admin/xpack/ml/inference/put"; + private PutTrainedModelAction() { super(NAME, Response::new); } @@ -43,10 +43,14 @@ public static Request parseRequest(String modelId, boolean deferDefinitionValida builder.setModelId(modelId).build(); } else if (Strings.isNullOrEmpty(modelId) == false && modelId.equals(builder.getModelId()) == false) { // If we have model_id in both URI and body, they must be identical - throw new IllegalArgumentException(Messages.getMessage(Messages.INCONSISTENT_ID, - TrainedModelConfig.MODEL_ID.getPreferredName(), - builder.getModelId(), - modelId)); + throw new IllegalArgumentException( + Messages.getMessage( + Messages.INCONSISTENT_ID, + TrainedModelConfig.MODEL_ID.getPreferredName(), + builder.getModelId(), + modelId + ) + ); } // Validations are done against the builder so we can build the full config object. // This allows us to not worry about serializing a builder class between nodes. @@ -73,14 +77,14 @@ public TrainedModelConfig getTrainedModelConfig() { @Override public ActionRequestValidationException validate() { - if (deferDefinitionDecompression - && config.getEstimatedHeapMemory() == 0 - && config.getCompressedDefinitionIfSet() != null) { + if (deferDefinitionDecompression && config.getEstimatedHeapMemory() == 0 && config.getCompressedDefinitionIfSet() != null) { ActionRequestValidationException validationException = new ActionRequestValidationException(); validationException.addValidationError( "when [" + DEFER_DEFINITION_DECOMPRESSION - + "] is true and a compressed definition is provided, " + ESTIMATED_HEAP_MEMORY_USAGE_BYTES + " must be set" + + "] is true and a compressed definition is provided, " + + ESTIMATED_HEAP_MEMORY_USAGE_BYTES + + " must be set" ); return validationException; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PutTrainedModelAliasAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PutTrainedModelAliasAction.java index d5078c1dadd21..b5fc85bdc4f99 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PutTrainedModelAliasAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PutTrainedModelAliasAction.java @@ -73,7 +73,7 @@ public boolean isReassign() { } @Override - public void writeTo(StreamOutput out) throws IOException { + public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); out.writeString(modelAlias); out.writeString(modelId); @@ -85,12 +85,7 @@ public ActionRequestValidationException validate() { ActionRequestValidationException validationException = null; if (modelAlias.equals(modelId)) { validationException = addValidationError( - String.format( - Locale.ROOT, - "model_alias [%s] cannot equal model_id [%s]", - modelAlias, - modelId - ), + String.format(Locale.ROOT, "model_alias [%s] cannot equal model_id [%s]", modelAlias, modelId), validationException ); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PutTrainedModelVocabularyAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PutTrainedModelVocabularyAction.java index 35084cf5f2108..00ee0781afce0 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PutTrainedModelVocabularyAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PutTrainedModelVocabularyAction.java @@ -38,10 +38,7 @@ public static class Request extends AcknowledgedRequest { public static final ParseField VOCABULARY = new ParseField("vocabulary"); - private static final ObjectParser PARSER = new ObjectParser<>( - "put_trained_model_vocabulary", - Builder::new - ); + private static final ObjectParser PARSER = new ObjectParser<>("put_trained_model_vocabulary", Builder::new); static { PARSER.declareStringArray(Builder::setVocabulary, VOCABULARY); } @@ -78,8 +75,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Request request = (Request) o; - return Objects.equals(modelId, request.modelId) - && Objects.equals(vocabulary, request.vocabulary); + return Objects.equals(modelId, request.modelId) && Objects.equals(vocabulary, request.vocabulary); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/RevertModelSnapshotAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/RevertModelSnapshotAction.java index 3eabd9c94904c..54edbc4e0c328 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/RevertModelSnapshotAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/RevertModelSnapshotAction.java @@ -11,16 +11,16 @@ import org.elasticsearch.action.ActionResponse; import org.elasticsearch.action.ActionType; import org.elasticsearch.action.support.master.AcknowledgedRequest; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.xcontent.ObjectParser; import org.elasticsearch.common.xcontent.StatusToXContentObject; +import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.rest.RestStatus; import org.elasticsearch.xpack.core.ml.job.config.Job; import org.elasticsearch.xpack.core.ml.job.process.autodetect.state.ModelSnapshot; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; @@ -67,8 +67,7 @@ public static Request parseRequest(String jobId, String snapshotId, XContentPars private boolean deleteInterveningResults; private boolean force; - public Request() { - } + public Request() {} public Request(StreamInput in) throws IOException { super(in); @@ -153,9 +152,9 @@ public boolean equals(Object obj) { } Request other = (Request) obj; return Objects.equals(jobId, other.jobId) - && Objects.equals(snapshotId, other.snapshotId) - && Objects.equals(deleteInterveningResults, other.deleteInterveningResults) - && force == other.force; + && Objects.equals(snapshotId, other.snapshotId) + && Objects.equals(deleteInterveningResults, other.deleteInterveningResults) + && force == other.force; } } @@ -169,7 +168,6 @@ public Response(StreamInput in) throws IOException { model = new ModelSnapshot(in); } - public Response(ModelSnapshot modelSnapshot) { model = modelSnapshot; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/SetResetModeAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/SetResetModeAction.java index 0bf17c1fb71c1..3fc9b8a0f10b1 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/SetResetModeAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/SetResetModeAction.java @@ -9,7 +9,6 @@ import org.elasticsearch.action.ActionType; import org.elasticsearch.action.support.master.AcknowledgedResponse; - public class SetResetModeAction extends ActionType { public static final SetResetModeAction INSTANCE = new SetResetModeAction(); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/SetUpgradeModeAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/SetUpgradeModeAction.java index e2b462cc37311..666a9797fd1ad 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/SetUpgradeModeAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/SetUpgradeModeAction.java @@ -10,10 +10,10 @@ import org.elasticsearch.action.ActionType; import org.elasticsearch.action.support.master.AcknowledgedRequest; import org.elasticsearch.action.support.master.AcknowledgedResponse; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -34,8 +34,10 @@ public static class Request extends AcknowledgedRequest implements ToXC private final boolean enabled; private static final ParseField ENABLED = new ParseField("enabled"); - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(NAME, a -> new Request((Boolean)a[0])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + a -> new Request((Boolean) a[0]) + ); static { PARSER.declareBoolean(ConstructingObjectParser.constructorArg(), ENABLED); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/StartDataFrameAnalyticsAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/StartDataFrameAnalyticsAction.java index eef68d9cb1134..fec7800e6b69b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/StartDataFrameAnalyticsAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/StartDataFrameAnalyticsAction.java @@ -13,15 +13,15 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.TimeValue; +import org.elasticsearch.persistent.PersistentTaskParams; +import org.elasticsearch.tasks.Task; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.core.TimeValue; -import org.elasticsearch.persistent.PersistentTaskParams; -import org.elasticsearch.tasks.Task; import org.elasticsearch.xpack.core.ml.MlTasks; import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsConfig; import org.elasticsearch.xpack.core.ml.job.messages.Messages; @@ -59,8 +59,9 @@ public static Request parseRequest(String id, XContentParser parser) { if (request.getId() == null) { request.setId(id); } else if (Strings.isNullOrEmpty(id) == false && id.equals(request.getId()) == false) { - throw new IllegalArgumentException(Messages.getMessage(Messages.INCONSISTENT_ID, DataFrameAnalyticsConfig.ID, - request.getId(), id)); + throw new IllegalArgumentException( + Messages.getMessage(Messages.INCONSISTENT_ID, DataFrameAnalyticsConfig.ID, request.getId(), id) + ); } return request; } @@ -146,8 +147,10 @@ public static class TaskParams implements PersistentTaskParams, MlTaskParams { public static final Version VERSION_DESTINATION_INDEX_MAPPINGS_CHANGED = Version.V_7_10_0; public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - MlTasks.DATA_FRAME_ANALYTICS_TASK_NAME, true, - a -> new TaskParams((String) a[0], (String) a[1], (Boolean) a[2])); + MlTasks.DATA_FRAME_ANALYTICS_TASK_NAME, + true, + a -> new TaskParams((String) a[0], (String) a[1], (Boolean) a[2]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), DataFrameAnalyticsConfig.ID); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/StartDatafeedAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/StartDatafeedAction.java index d2d3c82969499..08c03c15008ab 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/StartDatafeedAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/StartDatafeedAction.java @@ -14,18 +14,18 @@ import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.action.support.master.MasterNodeRequest; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.time.DateMathParser; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.index.mapper.DateFieldMapper; +import org.elasticsearch.persistent.PersistentTaskParams; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.index.mapper.DateFieldMapper; -import org.elasticsearch.persistent.PersistentTaskParams; import org.elasticsearch.xpack.core.ml.MlTasks; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfig; import org.elasticsearch.xpack.core.ml.job.config.Job; @@ -93,9 +93,17 @@ public DatafeedParams getParams() { public ActionRequestValidationException validate() { ActionRequestValidationException e = null; if (params.endTime != null && params.endTime <= params.startTime) { - e = ValidateActions.addValidationError(START_TIME.getPreferredName() + " [" - + params.startTime + "] must be earlier than " + END_TIME.getPreferredName() - + " [" + params.endTime + "]", e); + e = ValidateActions.addValidationError( + START_TIME.getPreferredName() + + " [" + + params.startTime + + "] must be earlier than " + + END_TIME.getPreferredName() + + " [" + + params.endTime + + "]", + e + ); } return e; } @@ -141,16 +149,19 @@ public static class DatafeedParams implements PersistentTaskParams, MlTaskParams ); static { PARSER.declareString((params, datafeedId) -> params.datafeedId = datafeedId, DatafeedConfig.ID); - PARSER.declareString((params, startTime) -> params.startTime = parseDateOrThrow( - startTime, START_TIME, System::currentTimeMillis), START_TIME); + PARSER.declareString( + (params, startTime) -> params.startTime = parseDateOrThrow(startTime, START_TIME, System::currentTimeMillis), + START_TIME + ); PARSER.declareString(DatafeedParams::setEndTime, END_TIME); - PARSER.declareString((params, val) -> - params.setTimeout(TimeValue.parseTimeValue(val, TIMEOUT.getPreferredName())), TIMEOUT); + PARSER.declareString((params, val) -> params.setTimeout(TimeValue.parseTimeValue(val, TIMEOUT.getPreferredName())), TIMEOUT); PARSER.declareString(DatafeedParams::setJobId, Job.ID); PARSER.declareStringArray(DatafeedParams::setDatafeedIndices, INDICES); - PARSER.declareObject(DatafeedParams::setIndicesOptions, + PARSER.declareObject( + DatafeedParams::setIndicesOptions, (p, c) -> IndicesOptions.fromMap(p.map(), SearchRequest.DEFAULT_INDICES_OPTIONS), - DatafeedConfig.INDICES_OPTIONS); + DatafeedConfig.INDICES_OPTIONS + ); } static long parseDateOrThrow(String date, ParseField paramName, LongSupplier now) { @@ -195,8 +206,7 @@ public DatafeedParams(StreamInput in) throws IOException { indicesOptions = IndicesOptions.readIndicesOptions(in); } - DatafeedParams() { - } + DatafeedParams() {} private String datafeedId; private long startTime; @@ -206,7 +216,6 @@ public DatafeedParams(StreamInput in) throws IOException { private String jobId; private IndicesOptions indicesOptions = SearchRequest.DEFAULT_INDICES_OPTIONS; - public String getDatafeedId() { return datafeedId; } @@ -319,13 +328,13 @@ public boolean equals(Object obj) { return false; } DatafeedParams other = (DatafeedParams) obj; - return Objects.equals(datafeedId, other.datafeedId) && - Objects.equals(startTime, other.startTime) && - Objects.equals(endTime, other.endTime) && - Objects.equals(timeout, other.timeout) && - Objects.equals(jobId, other.jobId) && - Objects.equals(indicesOptions, other.indicesOptions) && - Objects.equals(datafeedIndices, other.datafeedIndices); + return Objects.equals(datafeedId, other.datafeedId) + && Objects.equals(startTime, other.startTime) + && Objects.equals(endTime, other.endTime) + && Objects.equals(timeout, other.timeout) + && Objects.equals(jobId, other.jobId) + && Objects.equals(indicesOptions, other.indicesOptions) + && Objects.equals(datafeedIndices, other.datafeedIndices); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/StartTrainedModelDeploymentAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/StartTrainedModelDeploymentAction.java index 8d250900a11b3..451797e7552d8 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/StartTrainedModelDeploymentAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/StartTrainedModelDeploymentAction.java @@ -18,14 +18,14 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.core.TimeValue; +import org.elasticsearch.tasks.Task; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.core.TimeValue; -import org.elasticsearch.tasks.Task; import org.elasticsearch.xpack.core.ml.inference.TrainedModelConfig; import org.elasticsearch.xpack.core.ml.inference.allocation.AllocationStatus; import org.elasticsearch.xpack.core.ml.job.messages.Messages; @@ -79,7 +79,8 @@ public static Request parseRequest(String modelId, XContentParser parser) { request.setModelId(modelId); } else if (Strings.isNullOrEmpty(modelId) == false && modelId.equals(request.getModelId()) == false) { throw ExceptionsHelper.badRequestException( - Messages.getMessage(Messages.INCONSISTENT_ID, MODEL_ID, request.getModelId(), modelId)); + Messages.getMessage(Messages.INCONSISTENT_ID, MODEL_ID, request.getModelId(), modelId) + ); } return request; } @@ -249,7 +250,7 @@ public static boolean mayAllocateToNode(DiscoveryNode node) { private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( "trained_model_deployment_params", true, - a -> new TaskParams((String)a[0], (Long)a[1], (int) a[2], (int) a[3], (int) a[4]) + a -> new TaskParams((String) a[0], (Long) a[1], (int) a[2], (int) a[3], (int) a[4]) ); static { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/StopDataFrameAnalyticsAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/StopDataFrameAnalyticsAction.java index d5be99aa337dd..44f458ef1d71c 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/StopDataFrameAnalyticsAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/StopDataFrameAnalyticsAction.java @@ -10,18 +10,18 @@ import org.elasticsearch.action.ActionType; import org.elasticsearch.action.support.tasks.BaseTasksRequest; import org.elasticsearch.action.support.tasks.BaseTasksResponse; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.tasks.Task; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.tasks.Task; import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsConfig; import org.elasticsearch.xpack.core.ml.job.messages.Messages; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; @@ -65,8 +65,9 @@ public static Request parseRequest(String id, XContentParser parser) { if (request.getId() == null) { request.setId(id); } else if (Strings.isNullOrEmpty(id) == false && id.equals(request.getId()) == false) { - throw new IllegalArgumentException(Messages.getMessage(Messages.INCONSISTENT_ID, DataFrameAnalyticsConfig.ID, - request.getId(), id)); + throw new IllegalArgumentException( + Messages.getMessage(Messages.INCONSISTENT_ID, DataFrameAnalyticsConfig.ID, request.getId(), id) + ); } return request; } @@ -150,8 +151,7 @@ public void writeTo(StreamOutput out) throws IOException { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder - .startObject() + return builder.startObject() .field(DataFrameAnalyticsConfig.ID.getPreferredName(), id) .field(ALLOW_NO_MATCH.getPreferredName(), allowNoMatch) .field(FORCE.getPreferredName(), force) @@ -220,10 +220,8 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws @Override public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; Response response = (Response) o; return stopped == response.stopped; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/StopDatafeedAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/StopDatafeedAction.java index 6ca4cde7fa3cd..d0791691589da 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/StopDatafeedAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/StopDatafeedAction.java @@ -6,20 +6,20 @@ */ package org.elasticsearch.xpack.core.ml.action; -import org.elasticsearch.action.ActionType; import org.elasticsearch.action.ActionRequestValidationException; +import org.elasticsearch.action.ActionType; import org.elasticsearch.action.support.tasks.BaseTasksRequest; import org.elasticsearch.action.support.tasks.BaseTasksResponse; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.tasks.Task; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.tasks.Task; import org.elasticsearch.xpack.core.ml.MlTasks; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfig; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; @@ -48,8 +48,10 @@ public static class Request extends BaseTasksRequest implements ToXCont public static final ObjectParser PARSER = new ObjectParser<>(NAME, Request::new); static { PARSER.declareString((request, datafeedId) -> request.datafeedId = datafeedId, DatafeedConfig.ID); - PARSER.declareString((request, val) -> - request.setStopTimeout(TimeValue.parseTimeValue(val, TIMEOUT.getPreferredName())), TIMEOUT); + PARSER.declareString( + (request, val) -> request.setStopTimeout(TimeValue.parseTimeValue(val, TIMEOUT.getPreferredName())), + TIMEOUT + ); PARSER.declareBoolean(Request::setForce, FORCE); PARSER.declareBoolean(Request::setAllowNoMatch, ALLOW_NO_MATCH); } @@ -76,8 +78,7 @@ public Request(String datafeedId) { this.datafeedId = ExceptionsHelper.requireNonNull(datafeedId, DatafeedConfig.ID.getPreferredName()); } - public Request() { - } + public Request() {} public Request(StreamInput in) throws IOException { super(in); @@ -133,7 +134,7 @@ public Request setAllowNoMatch(boolean allowNoMatch) { public boolean match(Task task) { for (String id : resolvedStartedDatafeedIds) { String expectedDescription = MlTasks.datafeedTaskId(id); - if (task instanceof StartDatafeedAction.DatafeedTaskMatcher && expectedDescription.equals(task.getDescription())){ + if (task instanceof StartDatafeedAction.DatafeedTaskMatcher && expectedDescription.equals(task.getDescription())) { return true; } } @@ -180,10 +181,10 @@ public boolean equals(Object obj) { return false; } Request other = (Request) obj; - return Objects.equals(datafeedId, other.datafeedId) && - Objects.equals(stopTimeout, other.stopTimeout) && - Objects.equals(force, other.force) && - Objects.equals(allowNoMatch, other.allowNoMatch); + return Objects.equals(datafeedId, other.datafeedId) + && Objects.equals(stopTimeout, other.stopTimeout) + && Objects.equals(force, other.force) + && Objects.equals(allowNoMatch, other.allowNoMatch); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/StopTrainedModelDeploymentAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/StopTrainedModelDeploymentAction.java index 073a43dc20fc1..24a6adc899920 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/StopTrainedModelDeploymentAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/StopTrainedModelDeploymentAction.java @@ -10,13 +10,13 @@ import org.elasticsearch.action.ActionType; import org.elasticsearch.action.support.tasks.BaseTasksRequest; import org.elasticsearch.action.support.tasks.BaseTasksResponse; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.tasks.Task; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.tasks.Task; import org.elasticsearch.xpack.core.ml.inference.TrainedModelConfig; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; @@ -110,9 +110,7 @@ public boolean equals(Object o) { if (o == null || getClass() != o.getClass()) return false; Request that = (Request) o; - return Objects.equals(id, that.id) && - allowNoMatch == that.allowNoMatch && - force == that.force; + return Objects.equals(id, that.id) && allowNoMatch == that.allowNoMatch && force == that.force; } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/UpdateCalendarJobAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/UpdateCalendarJobAction.java index 4db67e3d72dfe..e5096051b1eae 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/UpdateCalendarJobAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/UpdateCalendarJobAction.java @@ -87,9 +87,9 @@ public boolean equals(Object obj) { return false; } Request other = (Request) obj; - return Objects.equals(calendarId, other.calendarId) && Objects.equals(jobIdsToAddExpression, other.jobIdsToAddExpression) - && Objects.equals(jobIdsToRemoveExpression, other.jobIdsToRemoveExpression); + return Objects.equals(calendarId, other.calendarId) + && Objects.equals(jobIdsToAddExpression, other.jobIdsToAddExpression) + && Objects.equals(jobIdsToRemoveExpression, other.jobIdsToRemoveExpression); } } } - diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/UpdateDataFrameAnalyticsAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/UpdateDataFrameAnalyticsAction.java index c0db9360a992a..ec77c924c5f83 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/UpdateDataFrameAnalyticsAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/UpdateDataFrameAnalyticsAction.java @@ -43,7 +43,8 @@ public static Request parseRequest(String id, XContentParser parser) { } else if (Strings.isNullOrEmpty(id) == false && id.equals(updateBuilder.getId()) == false) { // If we have both URI and body ID, they must be identical throw new IllegalArgumentException( - Messages.getMessage(Messages.INCONSISTENT_ID, DataFrameAnalyticsConfig.ID, updateBuilder.getId(), id)); + Messages.getMessage(Messages.INCONSISTENT_ID, DataFrameAnalyticsConfig.ID, updateBuilder.getId(), id) + ); } return new UpdateDataFrameAnalyticsAction.Request(updateBuilder.build()); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/UpdateDatafeedAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/UpdateDatafeedAction.java index 39ce7f9ff2479..53e0927b31312 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/UpdateDatafeedAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/UpdateDatafeedAction.java @@ -10,9 +10,9 @@ import org.elasticsearch.action.ActionType; import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.action.support.master.AcknowledgedRequest; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/UpdateFilterAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/UpdateFilterAction.java index 574dce24ebbd3..184c92d1e36f2 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/UpdateFilterAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/UpdateFilterAction.java @@ -9,12 +9,12 @@ import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.ActionType; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -30,7 +30,6 @@ import java.util.SortedSet; import java.util.TreeSet; - public class UpdateFilterAction extends ActionType { public static final UpdateFilterAction INSTANCE = new UpdateFilterAction(); @@ -60,8 +59,9 @@ public static Request parseRequest(String filterId, XContentParser parser) { request.filterId = filterId; } else if (Strings.isNullOrEmpty(filterId) == false && filterId.equals(request.filterId) == false) { // If we have both URI and body filter ID, they must be identical - throw new IllegalArgumentException(Messages.getMessage(Messages.INCONSISTENT_ID, MlFilter.ID.getPreferredName(), - request.filterId, filterId)); + throw new IllegalArgumentException( + Messages.getMessage(Messages.INCONSISTENT_ID, MlFilter.ID.getPreferredName(), request.filterId, filterId) + ); } return request; } @@ -72,8 +72,7 @@ public static Request parseRequest(String filterId, XContentParser parser) { private SortedSet addItems = Collections.emptySortedSet(); private SortedSet removeItems = Collections.emptySortedSet(); - public Request() { - } + public Request() {} public Request(StreamInput in) throws IOException { super(in); @@ -165,9 +164,9 @@ public boolean equals(Object obj) { } Request other = (Request) obj; return Objects.equals(filterId, other.filterId) - && Objects.equals(description, other.description) - && Objects.equals(addItems, other.addItems) - && Objects.equals(removeItems, other.removeItems); + && Objects.equals(description, other.description) + && Objects.equals(addItems, other.addItems) + && Objects.equals(removeItems, other.removeItems); } } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/UpdateJobAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/UpdateJobAction.java index 8def6d0a2fcae..982ff2e36f9e8 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/UpdateJobAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/UpdateJobAction.java @@ -103,9 +103,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; UpdateJobAction.Request that = (UpdateJobAction.Request) o; - return Objects.equals(jobId, that.jobId) && - Objects.equals(update, that.update) && - isInternal == that.isInternal; + return Objects.equals(jobId, that.jobId) && Objects.equals(update, that.update) && isInternal == that.isInternal; } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/UpdateModelSnapshotAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/UpdateModelSnapshotAction.java index f0b3dae86ade9..b253e4144ba2b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/UpdateModelSnapshotAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/UpdateModelSnapshotAction.java @@ -10,16 +10,16 @@ import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.ActionResponse; import org.elasticsearch.action.ActionType; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.xcontent.ObjectParser; import org.elasticsearch.common.xcontent.StatusToXContentObject; +import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.rest.RestStatus; import org.elasticsearch.xpack.core.ml.job.config.Job; import org.elasticsearch.xpack.core.ml.job.process.autodetect.state.ModelSnapshot; import org.elasticsearch.xpack.core.ml.job.process.autodetect.state.ModelSnapshotField; @@ -64,8 +64,7 @@ public static Request parseRequest(String jobId, String snapshotId, XContentPars private String description; private Boolean retain; - public Request() { - } + public Request() {} public Request(StreamInput in) throws IOException { super(in); @@ -148,9 +147,9 @@ public boolean equals(Object obj) { } Request other = (Request) obj; return Objects.equals(jobId, other.jobId) - && Objects.equals(snapshotId, other.snapshotId) - && Objects.equals(description, other.description) - && Objects.equals(retain, other.retain); + && Objects.equals(snapshotId, other.snapshotId) + && Objects.equals(description, other.description) + && Objects.equals(retain, other.retain); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/UpdateProcessAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/UpdateProcessAction.java index e3d89768bd437..b6bd1fb0a2c0a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/UpdateProcessAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/UpdateProcessAction.java @@ -12,8 +12,8 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.xcontent.StatusToXContentObject; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ml.job.config.JobUpdate; import org.elasticsearch.xpack.core.ml.job.config.MlFilter; import org.elasticsearch.xpack.core.ml.job.config.ModelPlotConfig; @@ -121,8 +121,14 @@ public void writeTo(StreamOutput out) throws IOException { out.writeBoolean(updateScheduledEvents); } - public Request(String jobId, ModelPlotConfig modelPlotConfig, PerPartitionCategorizationConfig perPartitionCategorizationConfig, - List detectorUpdates, MlFilter filter, boolean updateScheduledEvents) { + public Request( + String jobId, + ModelPlotConfig modelPlotConfig, + PerPartitionCategorizationConfig perPartitionCategorizationConfig, + List detectorUpdates, + MlFilter filter, + boolean updateScheduledEvents + ) { super(jobId); this.modelPlotConfig = modelPlotConfig; this.perPartitionCategorizationConfig = perPartitionCategorizationConfig; @@ -153,8 +159,14 @@ public boolean isUpdateScheduledEvents() { @Override public int hashCode() { - return Objects.hash(getJobId(), modelPlotConfig, perPartitionCategorizationConfig, detectorUpdates, filter, - updateScheduledEvents); + return Objects.hash( + getJobId(), + modelPlotConfig, + perPartitionCategorizationConfig, + detectorUpdates, + filter, + updateScheduledEvents + ); } @Override @@ -167,12 +179,12 @@ public boolean equals(Object obj) { } Request other = (Request) obj; - return Objects.equals(getJobId(), other.getJobId()) && - Objects.equals(modelPlotConfig, other.modelPlotConfig) && - Objects.equals(perPartitionCategorizationConfig, other.perPartitionCategorizationConfig) && - Objects.equals(detectorUpdates, other.detectorUpdates) && - Objects.equals(filter, other.filter) && - Objects.equals(updateScheduledEvents, other.updateScheduledEvents); + return Objects.equals(getJobId(), other.getJobId()) + && Objects.equals(modelPlotConfig, other.modelPlotConfig) + && Objects.equals(perPartitionCategorizationConfig, other.perPartitionCategorizationConfig) + && Objects.equals(detectorUpdates, other.detectorUpdates) + && Objects.equals(filter, other.filter) + && Objects.equals(updateScheduledEvents, other.updateScheduledEvents); } } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/UpgradeJobModelSnapshotAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/UpgradeJobModelSnapshotAction.java index 6cd22760ea347..4996209cf0e29 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/UpgradeJobModelSnapshotAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/UpgradeJobModelSnapshotAction.java @@ -10,11 +10,11 @@ import org.elasticsearch.action.ActionResponse; import org.elasticsearch.action.ActionType; import org.elasticsearch.action.support.master.MasterNodeRequest; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -43,7 +43,8 @@ public static class Request extends MasterNodeRequest implements ToXCon private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( NAME, - a -> new UpgradeJobModelSnapshotAction.Request((String) a[0], (String) a[1], (String) a[2], (Boolean) a[3])); + a -> new UpgradeJobModelSnapshotAction.Request((String) a[0], (String) a[1], (String) a[2], (Boolean) a[3]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), Job.ID); PARSER.declareString(ConstructingObjectParser.constructorArg(), SNAPSHOT_ID); @@ -61,10 +62,12 @@ public static UpgradeJobModelSnapshotAction.Request parseRequest(XContentParser private final boolean waitForCompletion; Request(String jobId, String snapshotId, String timeout, Boolean waitForCompletion) { - this(jobId, + this( + jobId, snapshotId, timeout == null ? null : TimeValue.parseTimeValue(timeout, TIMEOUT.getPreferredName()), - waitForCompletion != null && waitForCompletion); + waitForCompletion != null && waitForCompletion + ); } public Request(String jobId, String snapshotId, TimeValue timeValue, boolean waitForCompletion) { @@ -117,10 +120,10 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Request request = (Request) o; - return Objects.equals(jobId, request.jobId) && - Objects.equals(timeout, request.timeout) && - Objects.equals(snapshotId, request.snapshotId) && - waitForCompletion == request.waitForCompletion; + return Objects.equals(jobId, request.jobId) + && Objects.equals(timeout, request.timeout) + && Objects.equals(snapshotId, request.snapshotId) + && waitForCompletion == request.waitForCompletion; } @Override @@ -147,7 +150,8 @@ public static class Response extends ActionResponse implements ToXContentObject private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( NAME, - a -> new UpgradeJobModelSnapshotAction.Response((boolean) a[0], (String) a[1])); + a -> new UpgradeJobModelSnapshotAction.Response((boolean) a[0], (String) a[1]) + ); static { PARSER.declareBoolean(ConstructingObjectParser.constructorArg(), COMPLETED); PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), NODE); @@ -193,8 +197,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Response response = (Response) o; - return completed == response.completed && - Objects.equals(node, response.node); + return completed == response.completed && Objects.equals(node, response.node); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/ValidateJobConfigAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/ValidateJobConfigAction.java index 9ff1d519cadb3..e90ac5c47210e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/ValidateJobConfigAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/ValidateJobConfigAction.java @@ -48,8 +48,9 @@ public static Request parseRequest(XContentParser parser) { // Some fields cannot be set at create time List invalidJobCreationSettings = jobBuilder.invalidCreateTimeSettings(); if (invalidJobCreationSettings.isEmpty() == false) { - throw new IllegalArgumentException(Messages.getMessage(Messages.JOB_CONFIG_INVALID_CREATE_SETTINGS, - String.join(",", invalidJobCreationSettings))); + throw new IllegalArgumentException( + Messages.getMessage(Messages.JOB_CONFIG_INVALID_CREATE_SETTINGS, String.join(",", invalidJobCreationSettings)) + ); } return new Request(jobBuilder.build(new Date())); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/annotations/Annotation.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/annotations/Annotation.java index 10ea532284533..d4da74df85ba9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/annotations/Annotation.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/annotations/Annotation.java @@ -6,12 +6,12 @@ */ package org.elasticsearch.xpack.core.ml.annotations; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -88,21 +88,40 @@ public static Annotation fromXContent(XContentParser parser, Void context) { /** * Strict parser for cases when {@link Annotation} is returned from C++ as an ML result. */ - private static final ObjectParser STRICT_PARSER = - new ObjectParser<>(RESULTS_FIELD.getPreferredName(), false, Builder::new); + private static final ObjectParser STRICT_PARSER = new ObjectParser<>( + RESULTS_FIELD.getPreferredName(), + false, + Builder::new + ); static { STRICT_PARSER.declareString(Builder::setAnnotation, ANNOTATION); - STRICT_PARSER.declareField(Builder::setCreateTime, - p -> TimeUtils.parseTimeField(p, CREATE_TIME.getPreferredName()), CREATE_TIME, ObjectParser.ValueType.VALUE); + STRICT_PARSER.declareField( + Builder::setCreateTime, + p -> TimeUtils.parseTimeField(p, CREATE_TIME.getPreferredName()), + CREATE_TIME, + ObjectParser.ValueType.VALUE + ); STRICT_PARSER.declareString(Builder::setCreateUsername, CREATE_USERNAME); - STRICT_PARSER.declareField(Builder::setTimestamp, - p -> TimeUtils.parseTimeField(p, TIMESTAMP.getPreferredName()), TIMESTAMP, ObjectParser.ValueType.VALUE); - STRICT_PARSER.declareField(Builder::setEndTimestamp, - p -> TimeUtils.parseTimeField(p, END_TIMESTAMP.getPreferredName()), END_TIMESTAMP, ObjectParser.ValueType.VALUE); + STRICT_PARSER.declareField( + Builder::setTimestamp, + p -> TimeUtils.parseTimeField(p, TIMESTAMP.getPreferredName()), + TIMESTAMP, + ObjectParser.ValueType.VALUE + ); + STRICT_PARSER.declareField( + Builder::setEndTimestamp, + p -> TimeUtils.parseTimeField(p, END_TIMESTAMP.getPreferredName()), + END_TIMESTAMP, + ObjectParser.ValueType.VALUE + ); STRICT_PARSER.declareString(Builder::setJobId, Job.ID); - STRICT_PARSER.declareField(Builder::setModifiedTime, - p -> TimeUtils.parseTimeField(p, MODIFIED_TIME.getPreferredName()), MODIFIED_TIME, ObjectParser.ValueType.VALUE); + STRICT_PARSER.declareField( + Builder::setModifiedTime, + p -> TimeUtils.parseTimeField(p, MODIFIED_TIME.getPreferredName()), + MODIFIED_TIME, + ObjectParser.ValueType.VALUE + ); STRICT_PARSER.declareString(Builder::setModifiedUsername, MODIFIED_USERNAME); STRICT_PARSER.declareString(Builder::setType, Type::fromString, TYPE); STRICT_PARSER.declareString(Builder::setEvent, Event::fromString, EVENT); @@ -139,10 +158,25 @@ public static Annotation fromXContent(XContentParser parser, Void context) { private final String byFieldName; private final String byFieldValue; - private Annotation(String annotation, Date createTime, String createUsername, Date timestamp, Date endTimestamp, String jobId, - Date modifiedTime, String modifiedUsername, Type type, Event event, Integer detectorIndex, - String partitionFieldName, String partitionFieldValue, String overFieldName, String overFieldValue, - String byFieldName, String byFieldValue) { + private Annotation( + String annotation, + Date createTime, + String createUsername, + Date timestamp, + Date endTimestamp, + String jobId, + Date modifiedTime, + String modifiedUsername, + Type type, + Event event, + Integer detectorIndex, + String partitionFieldName, + String partitionFieldValue, + String overFieldName, + String overFieldValue, + String byFieldName, + String byFieldValue + ) { this.annotation = Objects.requireNonNull(annotation); this.createTime = Objects.requireNonNull(createTime); this.createUsername = Objects.requireNonNull(createUsername); @@ -345,8 +379,24 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws @Override public int hashCode() { return Objects.hash( - annotation, createTime, createUsername, timestamp, endTimestamp, jobId, modifiedTime, modifiedUsername, type, event, - detectorIndex, partitionFieldName, partitionFieldValue, overFieldName, overFieldValue, byFieldName, byFieldValue); + annotation, + createTime, + createUsername, + timestamp, + endTimestamp, + jobId, + modifiedTime, + modifiedUsername, + type, + event, + detectorIndex, + partitionFieldName, + partitionFieldValue, + overFieldName, + overFieldValue, + byFieldName, + byFieldValue + ); } @Override @@ -358,26 +408,25 @@ public boolean equals(Object obj) { return false; } Annotation other = (Annotation) obj; - return Objects.equals(annotation, other.annotation) && - Objects.equals(createTime, other.createTime) && - Objects.equals(createUsername, other.createUsername) && - Objects.equals(timestamp, other.timestamp) && - Objects.equals(endTimestamp, other.endTimestamp) && - Objects.equals(jobId, other.jobId) && - Objects.equals(modifiedTime, other.modifiedTime) && - Objects.equals(modifiedUsername, other.modifiedUsername) && - Objects.equals(type, other.type) && - Objects.equals(event, other.event) && - Objects.equals(detectorIndex, other.detectorIndex) && - Objects.equals(partitionFieldName, other.partitionFieldName) && - Objects.equals(partitionFieldValue, other.partitionFieldValue) && - Objects.equals(overFieldName, other.overFieldName) && - Objects.equals(overFieldValue, other.overFieldValue) && - Objects.equals(byFieldName, other.byFieldName) && - Objects.equals(byFieldValue, other.byFieldValue); + return Objects.equals(annotation, other.annotation) + && Objects.equals(createTime, other.createTime) + && Objects.equals(createUsername, other.createUsername) + && Objects.equals(timestamp, other.timestamp) + && Objects.equals(endTimestamp, other.endTimestamp) + && Objects.equals(jobId, other.jobId) + && Objects.equals(modifiedTime, other.modifiedTime) + && Objects.equals(modifiedUsername, other.modifiedUsername) + && Objects.equals(type, other.type) + && Objects.equals(event, other.event) + && Objects.equals(detectorIndex, other.detectorIndex) + && Objects.equals(partitionFieldName, other.partitionFieldName) + && Objects.equals(partitionFieldValue, other.partitionFieldValue) + && Objects.equals(overFieldName, other.overFieldName) + && Objects.equals(overFieldValue, other.overFieldValue) + && Objects.equals(byFieldName, other.byFieldName) + && Objects.equals(byFieldValue, other.byFieldValue); } - public String toString() { return Strings.toString(this); } @@ -515,8 +564,24 @@ public Builder setByFieldValue(String value) { public Annotation build() { return new Annotation( - annotation, createTime, createUsername, timestamp, endTimestamp, jobId, modifiedTime, modifiedUsername, type, event, - detectorIndex, partitionFieldName, partitionFieldValue, overFieldName, overFieldValue, byFieldName, byFieldValue); + annotation, + createTime, + createUsername, + timestamp, + endTimestamp, + jobId, + modifiedTime, + modifiedUsername, + type, + event, + detectorIndex, + partitionFieldName, + partitionFieldValue, + overFieldName, + overFieldValue, + byFieldName, + byFieldValue + ); } } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/annotations/AnnotationIndex.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/annotations/AnnotationIndex.java index 3f1782911b060..3d565ceba0c12 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/annotations/AnnotationIndex.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/annotations/AnnotationIndex.java @@ -61,8 +61,12 @@ public class AnnotationIndex { * results views, so needs to exist when there might be ML results to view. This method also waits for the index to be ready to search * before it returns. */ - public static void createAnnotationsIndexIfNecessaryAndWaitForYellow(Client client, ClusterState state, TimeValue masterNodeTimeout, - final ActionListener finalListener) { + public static void createAnnotationsIndexIfNecessaryAndWaitForYellow( + Client client, + ClusterState state, + TimeValue masterNodeTimeout, + final ActionListener finalListener + ) { final ActionListener annotationsIndexCreatedListener = ActionListener.wrap(success -> { final ClusterHealthRequest request = Requests.clusterHealthRequest(READ_ALIAS_NAME) @@ -84,43 +88,56 @@ public static void createAnnotationsIndexIfNecessaryAndWaitForYellow(Client clie * Create the .ml-annotations-6 index with correct mappings if it does not already exist. This index is read and written by the UI * results views, so needs to exist when there might be ML results to view. */ - public static void createAnnotationsIndexIfNecessary(Client client, ClusterState state, TimeValue masterNodeTimeout, - final ActionListener finalListener) { - - final ActionListener checkMappingsListener = ActionListener.wrap(success -> - ElasticsearchMappings.addDocMappingIfMissing( - WRITE_ALIAS_NAME, - AnnotationIndex::annotationsMapping, - client, - state, - masterNodeTimeout, - finalListener), - finalListener::onFailure); + public static void createAnnotationsIndexIfNecessary( + Client client, + ClusterState state, + TimeValue masterNodeTimeout, + final ActionListener finalListener + ) { + + final ActionListener checkMappingsListener = ActionListener.wrap( + success -> ElasticsearchMappings.addDocMappingIfMissing( + WRITE_ALIAS_NAME, + AnnotationIndex::annotationsMapping, + client, + state, + masterNodeTimeout, + finalListener + ), + finalListener::onFailure + ); final ActionListener createAliasListener = ActionListener.wrap(success -> { - final IndicesAliasesRequestBuilder requestBuilder = - client.admin().indices().prepareAliases() - .addAliasAction(IndicesAliasesRequest.AliasActions.add() - .index(LATEST_INDEX_NAME).alias(READ_ALIAS_NAME).isHidden(true)) - .addAliasAction(IndicesAliasesRequest.AliasActions.add() - .index(LATEST_INDEX_NAME).alias(WRITE_ALIAS_NAME).isHidden(true)); + final IndicesAliasesRequestBuilder requestBuilder = client.admin() + .indices() + .prepareAliases() + .addAliasAction(IndicesAliasesRequest.AliasActions.add().index(LATEST_INDEX_NAME).alias(READ_ALIAS_NAME).isHidden(true)) + .addAliasAction(IndicesAliasesRequest.AliasActions.add().index(LATEST_INDEX_NAME).alias(WRITE_ALIAS_NAME).isHidden(true)); for (String oldIndexName : OLD_INDEX_NAMES) { if (state.getMetadata().getIndicesLookup().containsKey(oldIndexName)) { requestBuilder.removeAlias(oldIndexName, WRITE_ALIAS_NAME); } } - executeAsyncWithOrigin(client.threadPool().getThreadContext(), ML_ORIGIN, requestBuilder.request(), + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + ML_ORIGIN, + requestBuilder.request(), ActionListener.wrap( - r -> checkMappingsListener.onResponse(r.isAcknowledged()), finalListener::onFailure), - client.admin().indices()::aliases); + r -> checkMappingsListener.onResponse(r.isAcknowledged()), + finalListener::onFailure + ), + client.admin().indices()::aliases + ); }, finalListener::onFailure); // Only create the index or aliases if some other ML index exists - saves clutter if ML is never used. // Also, don't do this if there's a reset in progress or if ML upgrade mode is enabled. MlMetadata mlMetadata = MlMetadata.getMlMetadata(state); SortedMap mlLookup = state.getMetadata().getIndicesLookup().tailMap(".ml"); - if (mlMetadata.isResetMode() == false && mlMetadata.isUpgradeMode() == false && - mlLookup.isEmpty() == false && mlLookup.firstKey().startsWith(".ml")) { + if (mlMetadata.isResetMode() == false + && mlMetadata.isUpgradeMode() == false + && mlLookup.isEmpty() == false + && mlLookup.firstKey().startsWith(".ml")) { // Create the annotations index if it doesn't exist already. if (mlLookup.containsKey(LATEST_INDEX_NAME) == false) { @@ -133,28 +150,30 @@ public static void createAnnotationsIndexIfNecessary(Client client, ClusterState ) ); - CreateIndexRequest createIndexRequest = - new CreateIndexRequest(LATEST_INDEX_NAME) - .mapping(annotationsMapping()) - .settings(Settings.builder() + CreateIndexRequest createIndexRequest = new CreateIndexRequest(LATEST_INDEX_NAME).mapping(annotationsMapping()) + .settings( + Settings.builder() .put(IndexMetadata.SETTING_AUTO_EXPAND_REPLICAS, "0-1") .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, "1") - .put(IndexMetadata.SETTING_INDEX_HIDDEN, true)); - - executeAsyncWithOrigin(client.threadPool().getThreadContext(), ML_ORIGIN, createIndexRequest, - ActionListener.wrap( - r -> createAliasListener.onResponse(r.isAcknowledged()), - e -> { - // Possible that the index was created while the request was executing, - // so we need to handle that possibility - if (ExceptionsHelper.unwrapCause(e) instanceof ResourceAlreadyExistsException) { - // Create the alias - createAliasListener.onResponse(true); - } else { - finalListener.onFailure(e); - } + .put(IndexMetadata.SETTING_INDEX_HIDDEN, true) + ); + + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + ML_ORIGIN, + createIndexRequest, + ActionListener.wrap(r -> createAliasListener.onResponse(r.isAcknowledged()), e -> { + // Possible that the index was created while the request was executing, + // so we need to handle that possibility + if (ExceptionsHelper.unwrapCause(e) instanceof ResourceAlreadyExistsException) { + // Create the alias + createAliasListener.onResponse(true); + } else { + finalListener.onFailure(e); } - ), client.admin().indices()::create); + }), + client.admin().indices()::create + ); return; } @@ -182,6 +201,9 @@ public static void createAnnotationsIndexIfNecessary(Client client, ClusterState public static String annotationsMapping() { return TemplateUtils.loadTemplate( - "/org/elasticsearch/xpack/core/ml/annotations_index_mappings.json", Version.CURRENT.toString(), MAPPINGS_VERSION_VARIABLE); + "/org/elasticsearch/xpack/core/ml/annotations_index_mappings.json", + Version.CURRENT.toString(), + MAPPINGS_VERSION_VARIABLE + ); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/calendars/Calendar.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/calendars/Calendar.java index b6677585316ed..8f1970514bd9e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/calendars/Calendar.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/calendars/Calendar.java @@ -6,12 +6,12 @@ */ package org.elasticsearch.xpack.core.ml.calendars; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ml.utils.ToXContentParams; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/calendars/ScheduledEvent.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/calendars/ScheduledEvent.java index bef1e4378c6d0..95a68e4391da7 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/calendars/ScheduledEvent.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/calendars/ScheduledEvent.java @@ -6,15 +6,16 @@ */ package org.elasticsearch.xpack.core.ml.calendars; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xpack.core.common.time.TimeUtils; import org.elasticsearch.xpack.core.ml.job.config.DetectionRule; import org.elasticsearch.xpack.core.ml.job.config.Operator; import org.elasticsearch.xpack.core.ml.job.config.RuleAction; @@ -23,7 +24,6 @@ import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; import org.elasticsearch.xpack.core.ml.utils.Intervals; import org.elasticsearch.xpack.core.ml.utils.ToXContentParams; -import org.elasticsearch.xpack.core.common.time.TimeUtils; import java.io.IOException; import java.time.Instant; @@ -51,14 +51,18 @@ private static ObjectParser createParser(boolean i ObjectParser parser = new ObjectParser<>("scheduled_event", ignoreUnknownFields, Builder::new); parser.declareString(ScheduledEvent.Builder::description, DESCRIPTION); - parser.declareField(ScheduledEvent.Builder::startTime, + parser.declareField( + ScheduledEvent.Builder::startTime, p -> TimeUtils.parseTimeFieldToInstant(p, START_TIME.getPreferredName()), START_TIME, - ObjectParser.ValueType.VALUE); - parser.declareField(ScheduledEvent.Builder::endTime, + ObjectParser.ValueType.VALUE + ); + parser.declareField( + ScheduledEvent.Builder::endTime, p -> TimeUtils.parseTimeFieldToInstant(p, END_TIME.getPreferredName()), END_TIME, - ObjectParser.ValueType.VALUE); + ObjectParser.ValueType.VALUE + ); parser.declareString(ScheduledEvent.Builder::calendarId, Calendar.ID); parser.declareString((builder, s) -> {}, TYPE); @@ -176,9 +180,9 @@ public boolean equals(Object obj) { ScheduledEvent other = (ScheduledEvent) obj; return description.equals(other.description) - && Objects.equals(startTime, other.startTime) - && Objects.equals(endTime, other.endTime) - && calendarId.equals(other.calendarId); + && Objects.equals(startTime, other.startTime) + && Objects.equals(endTime, other.endTime) + && calendarId.equals(other.calendarId); } @Override @@ -225,27 +229,30 @@ public Builder eventId(String eventId) { public ScheduledEvent build() { if (description == null) { throw ExceptionsHelper.badRequestException( - Messages.getMessage(Messages.FIELD_CANNOT_BE_NULL, DESCRIPTION.getPreferredName())); + Messages.getMessage(Messages.FIELD_CANNOT_BE_NULL, DESCRIPTION.getPreferredName()) + ); } if (startTime == null) { throw ExceptionsHelper.badRequestException( - Messages.getMessage(Messages.FIELD_CANNOT_BE_NULL, START_TIME.getPreferredName())); + Messages.getMessage(Messages.FIELD_CANNOT_BE_NULL, START_TIME.getPreferredName()) + ); } if (endTime == null) { - throw ExceptionsHelper.badRequestException( - Messages.getMessage(Messages.FIELD_CANNOT_BE_NULL, END_TIME.getPreferredName())); + throw ExceptionsHelper.badRequestException(Messages.getMessage(Messages.FIELD_CANNOT_BE_NULL, END_TIME.getPreferredName())); } if (calendarId == null) { throw ExceptionsHelper.badRequestException( - Messages.getMessage(Messages.FIELD_CANNOT_BE_NULL, Calendar.ID.getPreferredName())); + Messages.getMessage(Messages.FIELD_CANNOT_BE_NULL, Calendar.ID.getPreferredName()) + ); } if (startTime.isBefore(endTime) == false) { - throw ExceptionsHelper.badRequestException("Event start time [" + startTime + - "] must come before end time [" + endTime + "]"); + throw ExceptionsHelper.badRequestException( + "Event start time [" + startTime + "] must come before end time [" + endTime + "]" + ); } ScheduledEvent event = new ScheduledEvent(description, startTime, endTime, calendarId, eventId); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/datafeed/AggProvider.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/datafeed/AggProvider.java index b82dce3f434f0..135bef89dd0ad 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/datafeed/AggProvider.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/datafeed/AggProvider.java @@ -12,13 +12,13 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.search.aggregations.AggregatorFactories; +import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder; +import org.elasticsearch.search.aggregations.bucket.histogram.Histogram; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.search.aggregations.AggregatorFactories; -import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder; -import org.elasticsearch.search.aggregations.bucket.histogram.Histogram; import org.elasticsearch.xpack.core.ml.job.messages.Messages; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; import org.elasticsearch.xpack.core.ml.utils.XContentObjectTransformer; @@ -54,9 +54,9 @@ static AggProvider fromXContent(XContentParser parser, boolean lenient) throws I throw new Exception("aggs cannot be empty"); } parsedAggs = XContentObjectTransformer.aggregatorTransformer(parser.getXContentRegistry()).fromMap(aggs); - } catch(Exception ex) { + } catch (Exception ex) { if (ex.getCause() instanceof IllegalArgumentException) { - ex = (Exception)ex.getCause(); + ex = (Exception) ex.getCause(); } exception = ex; if (lenient) { @@ -77,20 +77,24 @@ static boolean rewriteDateHistogramInterval(Map aggs, boolean in aggs.put("calendar_interval", currentInterval.toString()); didRewrite = true; } else if (currentInterval instanceof Number) { - aggs.put("fixed_interval", ((Number)currentInterval).longValue() + "ms"); + aggs.put("fixed_interval", ((Number) currentInterval).longValue() + "ms"); didRewrite = true; } else if (currentInterval instanceof String) { aggs.put("fixed_interval", currentInterval.toString()); didRewrite = true; } else { - throw ExceptionsHelper.badRequestException(Messages.DATAFEED_CONFIG_AGG_BAD_FORMAT, - new IllegalArgumentException("unable to parse date_histogram interval parameter")); + throw ExceptionsHelper.badRequestException( + Messages.DATAFEED_CONFIG_AGG_BAD_FORMAT, + new IllegalArgumentException("unable to parse date_histogram interval parameter") + ); } } - for(Map.Entry entry : aggs.entrySet()) { + for (Map.Entry entry : aggs.entrySet()) { if (entry.getValue() instanceof Map) { - boolean rewrite = rewriteDateHistogramInterval((Map)entry.getValue(), - entry.getKey().equals(DateHistogramAggregationBuilder.NAME)); + boolean rewrite = rewriteDateHistogramInterval( + (Map) entry.getValue(), + entry.getKey().equals(DateHistogramAggregationBuilder.NAME) + ); didRewrite = didRewrite || rewrite; } } @@ -98,13 +102,14 @@ static boolean rewriteDateHistogramInterval(Map aggs, boolean in } static AggProvider fromParsedAggs(AggregatorFactories.Builder parsedAggs) throws IOException { - return parsedAggs == null ? - null : - new AggProvider( + return parsedAggs == null + ? null + : new AggProvider( XContentObjectTransformer.aggregatorTransformer(NamedXContentRegistry.EMPTY).toMap(parsedAggs), parsedAggs, null, - false); + false + ); } static AggProvider fromStream(StreamInput in) throws IOException { @@ -112,7 +117,8 @@ static AggProvider fromStream(StreamInput in) throws IOException { in.readMap(), in.readOptionalWriteable(AggregatorFactories.Builder::new), in.readException(), - in.getVersion().onOrAfter(Version.V_8_0_0) ? in.readBoolean() : false); + in.getVersion().onOrAfter(Version.V_8_0_0) ? in.readBoolean() : false + ); } AggProvider(Map aggs, AggregatorFactories.Builder parsedAggs, Exception parsingException, boolean rewroteAggs) { @@ -196,11 +202,15 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws @Override public String toString() { - return "AggProvider{" + - "parsingException=" + parsingException + - ", parsedAggs=" + parsedAggs + - ", aggs=" + aggs + - ", rewroteAggs=" + rewroteAggs + - '}'; + return "AggProvider{" + + "parsingException=" + + parsingException + + ", parsedAggs=" + + parsedAggs + + ", aggs=" + + aggs + + ", rewroteAggs=" + + rewroteAggs + + '}'; } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/datafeed/ChunkingConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/datafeed/ChunkingConfig.java index 1c9df7e9c073b..e26c447ae437d 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/datafeed/ChunkingConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/datafeed/ChunkingConfig.java @@ -6,13 +6,13 @@ */ package org.elasticsearch.xpack.core.ml.datafeed; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; @@ -35,13 +35,17 @@ public class ChunkingConfig implements ToXContentObject, Writeable { private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { ConstructingObjectParser parser = new ConstructingObjectParser<>( - "chunking_config", ignoreUnknownFields, a -> new ChunkingConfig((Mode) a[0], (TimeValue) a[1])); + "chunking_config", + ignoreUnknownFields, + a -> new ChunkingConfig((Mode) a[0], (TimeValue) a[1]) + ); parser.declareString(ConstructingObjectParser.constructorArg(), Mode::fromString, MODE_FIELD); parser.declareString( ConstructingObjectParser.optionalConstructorArg(), text -> TimeValue.parseTimeValue(text, TIME_SPAN_FIELD.getPreferredName()), - TIME_SPAN_FIELD); + TIME_SPAN_FIELD + ); return parser; } @@ -124,8 +128,7 @@ public boolean equals(Object obj) { } ChunkingConfig other = (ChunkingConfig) obj; - return Objects.equals(this.mode, other.mode) && - Objects.equals(this.timeSpan, other.timeSpan); + return Objects.equals(this.mode, other.mode) && Objects.equals(this.timeSpan, other.timeSpan); } public static ChunkingConfig newAuto() { @@ -140,8 +143,10 @@ public static ChunkingConfig newManual(TimeValue timeSpan) { return new ChunkingConfig(Mode.MANUAL, timeSpan); } - public enum Mode implements Writeable { - AUTO, MANUAL, OFF; + public enum Mode implements Writeable { + AUTO, + MANUAL, + OFF; public static Mode fromString(String value) { return Mode.valueOf(value.toUpperCase(Locale.ROOT)); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedConfig.java index 84cd031c5a701..d3ad5dd721dbd 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedConfig.java @@ -13,19 +13,13 @@ import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.cluster.AbstractDiffable; -import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.ObjectParser; -import org.elasticsearch.xcontent.ToXContentObject; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.core.Tuple; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.search.aggregations.AggregationBuilder; import org.elasticsearch.search.aggregations.AggregatorFactories; @@ -36,6 +30,12 @@ import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.sort.SortOrder; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.ToXContentObject; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.common.time.TimeUtils; import org.elasticsearch.xpack.core.ml.datafeed.extractor.ExtractorUtils; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; @@ -163,19 +163,21 @@ private static ObjectParser createParser(boolean ignoreUnknownFie parser.declareString(Builder::setJobId, JOB_ID); parser.declareStringArray(Builder::setIndices, INDEXES); parser.declareStringArray(Builder::setIndices, INDICES); - parser.declareString((builder, val) -> - builder.setQueryDelay(TimeValue.parseTimeValue(val, QUERY_DELAY.getPreferredName())), QUERY_DELAY); - parser.declareString((builder, val) -> - builder.setFrequency(TimeValue.parseTimeValue(val, FREQUENCY.getPreferredName())), FREQUENCY); - parser.declareObject(Builder::setQueryProvider, + parser.declareString( + (builder, val) -> builder.setQueryDelay(TimeValue.parseTimeValue(val, QUERY_DELAY.getPreferredName())), + QUERY_DELAY + ); + parser.declareString( + (builder, val) -> builder.setFrequency(TimeValue.parseTimeValue(val, FREQUENCY.getPreferredName())), + FREQUENCY + ); + parser.declareObject( + Builder::setQueryProvider, (p, c) -> QueryProvider.fromXContent(p, ignoreUnknownFields, DATAFEED_CONFIG_QUERY_BAD_FORMAT), - QUERY); - parser.declareObject(Builder::setAggregationsSafe, - (p, c) -> AggProvider.fromXContent(p, ignoreUnknownFields), - AGGREGATIONS); - parser.declareObject(Builder::setAggregationsSafe, - (p, c) -> AggProvider.fromXContent(p, ignoreUnknownFields), - AGGS); + QUERY + ); + parser.declareObject(Builder::setAggregationsSafe, (p, c) -> AggProvider.fromXContent(p, ignoreUnknownFields), AGGREGATIONS); + parser.declareObject(Builder::setAggregationsSafe, (p, c) -> AggProvider.fromXContent(p, ignoreUnknownFields), AGGS); parser.declareObject(Builder::setScriptFields, (p, c) -> { List parsedScriptFields = new ArrayList<>(); while (p.nextToken() != XContentParser.Token.END_OBJECT) { @@ -185,21 +187,28 @@ private static ObjectParser createParser(boolean ignoreUnknownFie return parsedScriptFields; }, SCRIPT_FIELDS); parser.declareInt(Builder::setScrollSize, SCROLL_SIZE); - parser.declareObject(Builder::setChunkingConfig, ignoreUnknownFields ? ChunkingConfig.LENIENT_PARSER : ChunkingConfig.STRICT_PARSER, - CHUNKING_CONFIG); + parser.declareObject( + Builder::setChunkingConfig, + ignoreUnknownFields ? ChunkingConfig.LENIENT_PARSER : ChunkingConfig.STRICT_PARSER, + CHUNKING_CONFIG + ); if (ignoreUnknownFields) { // Headers are not parsed by the strict (config) parser, so headers supplied in the _body_ of a REST request will be rejected. // (For config, headers are explicitly transferred from the auth headers by code in the put/update datafeed actions.) parser.declareObject(Builder::setHeaders, (p, c) -> p.mapStrings(), HEADERS); } - parser.declareObject(Builder::setDelayedDataCheckConfig, + parser.declareObject( + Builder::setDelayedDataCheckConfig, ignoreUnknownFields ? DelayedDataCheckConfig.LENIENT_PARSER : DelayedDataCheckConfig.STRICT_PARSER, - DELAYED_DATA_CHECK_CONFIG); + DELAYED_DATA_CHECK_CONFIG + ); parser.declareInt(Builder::setMaxEmptySearches, MAX_EMPTY_SEARCHES); - parser.declareObject(Builder::setIndicesOptions, + parser.declareObject( + Builder::setIndicesOptions, (p, c) -> IndicesOptions.fromMap(p.map(), SearchRequest.DEFAULT_INDICES_OPTIONS), - INDICES_OPTIONS); + INDICES_OPTIONS + ); parser.declareObject(Builder::setRuntimeMappings, (p, c) -> p.map(), SearchSourceBuilder.RUNTIME_MAPPINGS_FIELD); return parser; } @@ -229,11 +238,23 @@ private static ObjectParser createParser(boolean ignoreUnknownFie private final IndicesOptions indicesOptions; private final Map runtimeMappings; - private DatafeedConfig(String id, String jobId, TimeValue queryDelay, TimeValue frequency, List indices, - QueryProvider queryProvider, AggProvider aggProvider, List scriptFields, - Integer scrollSize, ChunkingConfig chunkingConfig, Map headers, - DelayedDataCheckConfig delayedDataCheckConfig, Integer maxEmptySearches, IndicesOptions indicesOptions, - Map runtimeMappings) { + private DatafeedConfig( + String id, + String jobId, + TimeValue queryDelay, + TimeValue frequency, + List indices, + QueryProvider queryProvider, + AggProvider aggProvider, + List scriptFields, + Integer scrollSize, + ChunkingConfig chunkingConfig, + Map headers, + DelayedDataCheckConfig delayedDataCheckConfig, + Integer maxEmptySearches, + IndicesOptions indicesOptions, + Map runtimeMappings + ) { this.id = id; this.jobId = jobId; this.queryDelay = queryDelay; @@ -319,9 +340,9 @@ public Integer getScrollSize() { } public Optional> minRequiredClusterVersion() { - return runtimeMappings.isEmpty() ? - Optional.empty() : - Optional.of(Tuple.tuple(RUNTIME_MAPPINGS_INTRODUCED, SearchSourceBuilder.RUNTIME_MAPPINGS_FIELD.getPreferredName())); + return runtimeMappings.isEmpty() + ? Optional.empty() + : Optional.of(Tuple.tuple(RUNTIME_MAPPINGS_INTRODUCED, SearchSourceBuilder.RUNTIME_MAPPINGS_FIELD.getPreferredName())); } /** @@ -340,13 +361,13 @@ public QueryBuilder getParsedQuery(NamedXContentRegistry namedXContentRegistry) // We will still need `NamedXContentRegistry` for getting deprecations, but that is a special situation private QueryBuilder parseQuery(NamedXContentRegistry namedXContentRegistry, List deprecations) { try { - return queryProvider == null || queryProvider.getQuery() == null ? - null : - XContentObjectTransformer.queryBuilderTransformer(namedXContentRegistry).fromMap(queryProvider.getQuery(), deprecations); + return queryProvider == null || queryProvider.getQuery() == null + ? null + : XContentObjectTransformer.queryBuilderTransformer(namedXContentRegistry).fromMap(queryProvider.getQuery(), deprecations); } catch (Exception exception) { // Certain thrown exceptions wrap up the real Illegal argument making it hard to determine cause for the user if (exception.getCause() instanceof IllegalArgumentException) { - exception = (Exception)exception.getCause(); + exception = (Exception) exception.getCause(); } throw ExceptionsHelper.badRequestException(DATAFEED_CONFIG_QUERY_BAD_FORMAT, exception); } @@ -388,13 +409,13 @@ public AggregatorFactories.Builder getParsedAggregations(NamedXContentRegistry n // We will still need `NamedXContentRegistry` for getting deprecations, but that is a special situation private AggregatorFactories.Builder parseAggregations(NamedXContentRegistry namedXContentRegistry, List deprecations) { try { - return aggProvider == null || aggProvider.getAggs() == null ? - null : - XContentObjectTransformer.aggregatorTransformer(namedXContentRegistry).fromMap(aggProvider.getAggs(), deprecations); + return aggProvider == null || aggProvider.getAggs() == null + ? null + : XContentObjectTransformer.aggregatorTransformer(namedXContentRegistry).fromMap(aggProvider.getAggs(), deprecations); } catch (Exception exception) { // Certain thrown exceptions wrap up the real Illegal argument making it hard to determine cause for the user if (exception.getCause() instanceof IllegalArgumentException) { - exception = (Exception)exception.getCause(); + exception = (Exception) exception.getCause(); } throw ExceptionsHelper.badRequestException(DATAFEED_CONFIG_AGG_BAD_FORMAT, exception); } @@ -625,26 +646,41 @@ public boolean equals(Object other) { DatafeedConfig that = (DatafeedConfig) other; return Objects.equals(this.id, that.id) - && Objects.equals(this.jobId, that.jobId) - && Objects.equals(this.frequency, that.frequency) - && Objects.equals(this.queryDelay, that.queryDelay) - && Objects.equals(this.indices, that.indices) - && Objects.equals(this.queryProvider, that.queryProvider) - && Objects.equals(this.scrollSize, that.scrollSize) - && Objects.equals(this.aggProvider, that.aggProvider) - && Objects.equals(this.scriptFields, that.scriptFields) - && Objects.equals(this.chunkingConfig, that.chunkingConfig) - && Objects.equals(this.headers, that.headers) - && Objects.equals(this.delayedDataCheckConfig, that.delayedDataCheckConfig) - && Objects.equals(this.maxEmptySearches, that.maxEmptySearches) - && Objects.equals(this.indicesOptions, that.indicesOptions) - && Objects.equals(this.runtimeMappings, that.runtimeMappings); + && Objects.equals(this.jobId, that.jobId) + && Objects.equals(this.frequency, that.frequency) + && Objects.equals(this.queryDelay, that.queryDelay) + && Objects.equals(this.indices, that.indices) + && Objects.equals(this.queryProvider, that.queryProvider) + && Objects.equals(this.scrollSize, that.scrollSize) + && Objects.equals(this.aggProvider, that.aggProvider) + && Objects.equals(this.scriptFields, that.scriptFields) + && Objects.equals(this.chunkingConfig, that.chunkingConfig) + && Objects.equals(this.headers, that.headers) + && Objects.equals(this.delayedDataCheckConfig, that.delayedDataCheckConfig) + && Objects.equals(this.maxEmptySearches, that.maxEmptySearches) + && Objects.equals(this.indicesOptions, that.indicesOptions) + && Objects.equals(this.runtimeMappings, that.runtimeMappings); } @Override public int hashCode() { - return Objects.hash(id, jobId, frequency, queryDelay, indices, queryProvider, scrollSize, aggProvider, scriptFields, chunkingConfig, - headers, delayedDataCheckConfig, maxEmptySearches, indicesOptions, runtimeMappings); + return Objects.hash( + id, + jobId, + frequency, + queryDelay, + indices, + queryProvider, + scrollSize, + aggProvider, + scriptFields, + chunkingConfig, + headers, + delayedDataCheckConfig, + maxEmptySearches, + indicesOptions, + runtimeMappings + ); } @Override @@ -674,8 +710,9 @@ public TimeValue defaultFrequency(TimeValue bucketSpan, NamedXContentRegistry xC if (hasAggregations()) { long histogramIntervalMillis = getHistogramIntervalMillis(xContentRegistry); long targetFrequencyMillis = defaultFrequency.millis(); - long defaultFrequencyMillis = histogramIntervalMillis > targetFrequencyMillis ? histogramIntervalMillis - : (targetFrequencyMillis / histogramIntervalMillis) * histogramIntervalMillis; + long defaultFrequencyMillis = histogramIntervalMillis > targetFrequencyMillis + ? histogramIntervalMillis + : (targetFrequencyMillis / histogramIntervalMillis) * histogramIntervalMillis; defaultFrequency = TimeValue.timeValueMillis(defaultFrequencyMillis); } return defaultFrequency; @@ -717,7 +754,7 @@ public static class Builder implements Writeable { private IndicesOptions indicesOptions; private Map runtimeMappings = Collections.emptyMap(); - public Builder() { } + public Builder() {} public Builder(String id, String jobId) { this(); @@ -985,8 +1022,10 @@ public IndicesOptions getIndicesOptions() { } public Builder setRuntimeMappings(Map runtimeMappings) { - this.runtimeMappings = ExceptionsHelper.requireNonNull(runtimeMappings, - SearchSourceBuilder.RUNTIME_MAPPINGS_FIELD.getPreferredName()); + this.runtimeMappings = ExceptionsHelper.requireNonNull( + runtimeMappings, + SearchSourceBuilder.RUNTIME_MAPPINGS_FIELD.getPreferredName() + ); return this; } @@ -1008,8 +1047,23 @@ public DatafeedConfig build() { if (indicesOptions == null) { indicesOptions = SearchRequest.DEFAULT_INDICES_OPTIONS; } - return new DatafeedConfig(id, jobId, queryDelay, frequency, indices, queryProvider, aggProvider, scriptFields, scrollSize, - chunkingConfig, headers, delayedDataCheckConfig, maxEmptySearches, indicesOptions, runtimeMappings); + return new DatafeedConfig( + id, + jobId, + queryDelay, + frequency, + indices, + queryProvider, + aggProvider, + scriptFields, + scrollSize, + chunkingConfig, + headers, + delayedDataCheckConfig, + maxEmptySearches, + indicesOptions, + runtimeMappings + ); } void validateScriptFields() { @@ -1044,7 +1098,7 @@ static void checkHistogramAggregationHasChildMaxTimeAgg(AggregationBuilder histo for (AggregationBuilder agg : histogramAggregation.getSubAggregations()) { if (agg instanceof MaxAggregationBuilder) { - MaxAggregationBuilder maxAgg = (MaxAggregationBuilder)agg; + MaxAggregationBuilder maxAgg = (MaxAggregationBuilder) agg; if (maxAgg.field().equals(timeField)) { return; } @@ -1068,10 +1122,7 @@ static void validateCompositeAggregationSources(CompositeAggregationBuilder hist if (valueSource instanceof DateHistogramValuesSourceBuilder) { if (hasDateValueSource) { throw ExceptionsHelper.badRequestException( - getMessage( - DATAFEED_AGGREGATIONS_COMPOSITE_AGG_MUST_HAVE_SINGLE_DATE_SOURCE, - histogramAggregation.getName() - ) + getMessage(DATAFEED_AGGREGATIONS_COMPOSITE_AGG_MUST_HAVE_SINGLE_DATE_SOURCE, histogramAggregation.getName()) ); } hasDateValueSource = true; @@ -1080,10 +1131,7 @@ static void validateCompositeAggregationSources(CompositeAggregationBuilder hist } if (foundBuilder == null) { throw ExceptionsHelper.badRequestException( - getMessage( - DATAFEED_AGGREGATIONS_COMPOSITE_AGG_MUST_HAVE_SINGLE_DATE_SOURCE, - histogramAggregation.getName() - ) + getMessage(DATAFEED_AGGREGATIONS_COMPOSITE_AGG_MUST_HAVE_SINGLE_DATE_SOURCE, histogramAggregation.getName()) ); } if (foundBuilder.missingBucket()) { @@ -1097,11 +1145,7 @@ static void validateCompositeAggregationSources(CompositeAggregationBuilder hist } if (foundBuilder.order() != SortOrder.ASC) { throw ExceptionsHelper.badRequestException( - getMessage( - DATAFEED_AGGREGATIONS_COMPOSITE_AGG_DATE_HISTOGRAM_SORT, - histogramAggregation.getName(), - foundBuilder.name() - ) + getMessage(DATAFEED_AGGREGATIONS_COMPOSITE_AGG_DATE_HISTOGRAM_SORT, histogramAggregation.getName(), foundBuilder.name()) ); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedJobValidator.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedJobValidator.java index e6dc9c2cc64ce..aa1c8d9a8ea03 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedJobValidator.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedJobValidator.java @@ -48,47 +48,58 @@ public static void validate(DatafeedConfig datafeedConfig, Job job, NamedXConten } private static void checkValidDelayedDataCheckConfig(TimeValue bucketSpan, DelayedDataCheckConfig delayedDataCheckConfig) { - TimeValue delayedDataCheckWindow = delayedDataCheckConfig.getCheckWindow(); + TimeValue delayedDataCheckWindow = delayedDataCheckConfig.getCheckWindow(); if (delayedDataCheckWindow != null) { // NULL implies we calculate on use and thus is always valid if (delayedDataCheckWindow.compareTo(bucketSpan) < 0) { throw ExceptionsHelper.badRequestException( - Messages.getMessage(Messages.DATAFEED_CONFIG_DELAYED_DATA_CHECK_TOO_SMALL, - delayedDataCheckWindow, - bucketSpan)); + Messages.getMessage(Messages.DATAFEED_CONFIG_DELAYED_DATA_CHECK_TOO_SMALL, delayedDataCheckWindow, bucketSpan) + ); } if (delayedDataCheckWindow.millis() > bucketSpan.millis() * DelayedDataCheckConfig.MAX_NUMBER_SPANABLE_BUCKETS) { throw ExceptionsHelper.badRequestException( - Messages.getMessage(Messages.DATAFEED_CONFIG_DELAYED_DATA_CHECK_SPANS_TOO_MANY_BUCKETS, + Messages.getMessage( + Messages.DATAFEED_CONFIG_DELAYED_DATA_CHECK_SPANS_TOO_MANY_BUCKETS, delayedDataCheckWindow, - bucketSpan)); + bucketSpan + ) + ); } } } private static void checkSummaryCountFieldNameIsSet(AnalysisConfig analysisConfig) { if (Strings.isNullOrEmpty(analysisConfig.getSummaryCountFieldName())) { - throw ExceptionsHelper.badRequestException(Messages.getMessage( - Messages.DATAFEED_AGGREGATIONS_REQUIRES_JOB_WITH_SUMMARY_COUNT_FIELD)); + throw ExceptionsHelper.badRequestException( + Messages.getMessage(Messages.DATAFEED_AGGREGATIONS_REQUIRES_JOB_WITH_SUMMARY_COUNT_FIELD) + ); } } - private static void checkValidHistogramInterval(DatafeedConfig datafeedConfig, - AnalysisConfig analysisConfig, - NamedXContentRegistry xContentRegistry) { + private static void checkValidHistogramInterval( + DatafeedConfig datafeedConfig, + AnalysisConfig analysisConfig, + NamedXContentRegistry xContentRegistry + ) { long histogramIntervalMillis = datafeedConfig.getHistogramIntervalMillis(xContentRegistry); long bucketSpanMillis = analysisConfig.getBucketSpan().millis(); if (histogramIntervalMillis > bucketSpanMillis) { - throw ExceptionsHelper.badRequestException(Messages.getMessage( + throw ExceptionsHelper.badRequestException( + Messages.getMessage( Messages.DATAFEED_AGGREGATIONS_INTERVAL_MUST_LESS_OR_EQUAL_TO_BUCKET_SPAN, TimeValue.timeValueMillis(histogramIntervalMillis).getStringRep(), - TimeValue.timeValueMillis(bucketSpanMillis).getStringRep())); + TimeValue.timeValueMillis(bucketSpanMillis).getStringRep() + ) + ); } if (bucketSpanMillis % histogramIntervalMillis != 0) { - throw ExceptionsHelper.badRequestException(Messages.getMessage( + throw ExceptionsHelper.badRequestException( + Messages.getMessage( Messages.DATAFEED_AGGREGATIONS_INTERVAL_MUST_BE_DIVISOR_OF_BUCKET_SPAN, TimeValue.timeValueMillis(histogramIntervalMillis).getStringRep(), - TimeValue.timeValueMillis(bucketSpanMillis).getStringRep())); + TimeValue.timeValueMillis(bucketSpanMillis).getStringRep() + ) + ); } } @@ -98,9 +109,13 @@ private static void checkFrequencyIsMultipleOfHistogramInterval(DatafeedConfig d long histogramIntervalMillis = datafeedConfig.getHistogramIntervalMillis(xContentRegistry); long frequencyMillis = frequency.millis(); if (frequencyMillis % histogramIntervalMillis != 0) { - throw ExceptionsHelper.badRequestException(Messages.getMessage( + throw ExceptionsHelper.badRequestException( + Messages.getMessage( Messages.DATAFEED_FREQUENCY_MUST_BE_MULTIPLE_OF_AGGREGATIONS_INTERVAL, - frequency, TimeValue.timeValueMillis(histogramIntervalMillis).getStringRep())); + frequency, + TimeValue.timeValueMillis(histogramIntervalMillis).getStringRep() + ) + ); } } } @@ -112,8 +127,9 @@ private static void checkTimeFieldIsNotASearchRuntimeField(DatafeedConfig datafe // top level objects are fields String fieldName = entry.getKey(); if (timeField.equals(fieldName)) { - throw ExceptionsHelper.badRequestException(Messages.getMessage( - Messages.JOB_CONFIG_TIME_FIELD_CANNOT_BE_RUNTIME, timeField)); + throw ExceptionsHelper.badRequestException( + Messages.getMessage(Messages.JOB_CONFIG_TIME_FIELD_CANNOT_BE_RUNTIME, timeField) + ); } } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedState.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedState.java index 8cdb4c30d2ccf..298c325a389c9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedState.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedState.java @@ -6,13 +6,13 @@ */ package org.elasticsearch.xpack.core.ml.datafeed; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.persistent.PersistentTaskState; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.persistent.PersistentTaskState; import org.elasticsearch.xpack.core.ml.MlTasks; import java.io.IOException; @@ -22,12 +22,17 @@ public enum DatafeedState implements PersistentTaskState { - STARTED, STOPPED, STARTING, STOPPING; + STARTED, + STOPPED, + STARTING, + STOPPING; public static final String NAME = MlTasks.DATAFEED_TASK_NAME; - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(NAME, args -> fromString((String) args[0])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + args -> fromString((String) args[0]) + ); static { PARSER.declareString(constructorArg(), new ParseField("state")); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedTimingStats.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedTimingStats.java index 0a1d67fca26d1..c736ef1f7ae6d 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedTimingStats.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedTimingStats.java @@ -6,13 +6,13 @@ */ package org.elasticsearch.xpack.core.ml.datafeed; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -42,23 +42,20 @@ public class DatafeedTimingStats implements ToXContentObject, Writeable { public static final ConstructingObjectParser PARSER = createParser(); private static ConstructingObjectParser createParser() { - ConstructingObjectParser parser = - new ConstructingObjectParser<>( - TYPE.getPreferredName(), - true, - args -> { - String jobId = (String) args[0]; - Long searchCount = (Long) args[1]; - Long bucketCount = (Long) args[2]; - Double totalSearchTimeMs = (Double) args[3]; - ExponentialAverageCalculationContext exponentialAvgCalculationContext = (ExponentialAverageCalculationContext) args[4]; - return new DatafeedTimingStats( - jobId, - getOrDefault(searchCount, 0L), - getOrDefault(bucketCount, 0L), - getOrDefault(totalSearchTimeMs, 0.0), - getOrDefault(exponentialAvgCalculationContext, new ExponentialAverageCalculationContext())); - }); + ConstructingObjectParser parser = new ConstructingObjectParser<>(TYPE.getPreferredName(), true, args -> { + String jobId = (String) args[0]; + Long searchCount = (Long) args[1]; + Long bucketCount = (Long) args[2]; + Double totalSearchTimeMs = (Double) args[3]; + ExponentialAverageCalculationContext exponentialAvgCalculationContext = (ExponentialAverageCalculationContext) args[4]; + return new DatafeedTimingStats( + jobId, + getOrDefault(searchCount, 0L), + getOrDefault(bucketCount, 0L), + getOrDefault(totalSearchTimeMs, 0.0), + getOrDefault(exponentialAvgCalculationContext, new ExponentialAverageCalculationContext()) + ); + }); parser.declareString(constructorArg(), JOB_ID); parser.declareLong(optionalConstructorArg(), SEARCH_COUNT); parser.declareLong(optionalConstructorArg(), BUCKET_COUNT); @@ -78,11 +75,12 @@ public static String documentId(String jobId) { private final ExponentialAverageCalculationContext exponentialAvgCalculationContext; public DatafeedTimingStats( - String jobId, - long searchCount, - long bucketCount, - double totalSearchTimeMs, - ExponentialAverageCalculationContext exponentialAvgCalculationContext) { + String jobId, + long searchCount, + long bucketCount, + double totalSearchTimeMs, + ExponentialAverageCalculationContext exponentialAvgCalculationContext + ) { this.jobId = Objects.requireNonNull(jobId); this.searchCount = searchCount; this.bucketCount = bucketCount; @@ -108,7 +106,8 @@ public DatafeedTimingStats(DatafeedTimingStats other) { other.searchCount, other.bucketCount, other.totalSearchTimeMs, - new ExponentialAverageCalculationContext(other.exponentialAvgCalculationContext)); + new ExponentialAverageCalculationContext(other.exponentialAvgCalculationContext) + ); } public String getJobId() { @@ -210,12 +209,7 @@ public boolean equals(Object obj) { @Override public int hashCode() { - return Objects.hash( - jobId, - searchCount, - bucketCount, - totalSearchTimeMs, - exponentialAvgCalculationContext); + return Objects.hash(jobId, searchCount, bucketCount, totalSearchTimeMs, exponentialAvgCalculationContext); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedUpdate.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedUpdate.java index 9f496f26bef33..76951db37131e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedUpdate.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedUpdate.java @@ -8,20 +8,20 @@ import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.support.IndicesOptions; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.index.query.QueryBuilder; +import org.elasticsearch.search.aggregations.AggregatorFactories; +import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.index.query.QueryBuilder; -import org.elasticsearch.search.aggregations.AggregatorFactories; -import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.xpack.core.ml.job.config.Job; import org.elasticsearch.xpack.core.ml.job.messages.Messages; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; @@ -40,7 +40,6 @@ import static org.elasticsearch.xpack.core.ClientHelper.filterSecurityHeaders; - /** * A datafeed update contains partial properties to update a {@link DatafeedConfig}. * The main difference between this class and {@link DatafeedConfig} is that here all @@ -57,35 +56,42 @@ public class DatafeedUpdate implements Writeable, ToXContentObject { PARSER.declareString(Builder::setJobId, Job.ID); PARSER.declareStringArray(Builder::setIndices, DatafeedConfig.INDEXES); PARSER.declareStringArray(Builder::setIndices, DatafeedConfig.INDICES); - PARSER.declareString((builder, val) -> builder.setQueryDelay( - TimeValue.parseTimeValue(val, DatafeedConfig.QUERY_DELAY.getPreferredName())), DatafeedConfig.QUERY_DELAY); - PARSER.declareString((builder, val) -> builder.setFrequency( - TimeValue.parseTimeValue(val, DatafeedConfig.FREQUENCY.getPreferredName())), DatafeedConfig.FREQUENCY); - PARSER.declareObject(Builder::setQuery, (p, c) -> QueryProvider.fromXContent(p, false, Messages.DATAFEED_CONFIG_QUERY_BAD_FORMAT), - DatafeedConfig.QUERY); - PARSER.declareObject(Builder::setAggregationsSafe, - (p, c) -> AggProvider.fromXContent(p, false), - DatafeedConfig.AGGREGATIONS); - PARSER.declareObject(Builder::setAggregationsSafe, - (p, c) -> AggProvider.fromXContent(p, false), - DatafeedConfig.AGGS); + PARSER.declareString( + (builder, val) -> builder.setQueryDelay(TimeValue.parseTimeValue(val, DatafeedConfig.QUERY_DELAY.getPreferredName())), + DatafeedConfig.QUERY_DELAY + ); + PARSER.declareString( + (builder, val) -> builder.setFrequency(TimeValue.parseTimeValue(val, DatafeedConfig.FREQUENCY.getPreferredName())), + DatafeedConfig.FREQUENCY + ); + PARSER.declareObject( + Builder::setQuery, + (p, c) -> QueryProvider.fromXContent(p, false, Messages.DATAFEED_CONFIG_QUERY_BAD_FORMAT), + DatafeedConfig.QUERY + ); + PARSER.declareObject(Builder::setAggregationsSafe, (p, c) -> AggProvider.fromXContent(p, false), DatafeedConfig.AGGREGATIONS); + PARSER.declareObject(Builder::setAggregationsSafe, (p, c) -> AggProvider.fromXContent(p, false), DatafeedConfig.AGGS); PARSER.declareObject(Builder::setScriptFields, (p, c) -> { - List parsedScriptFields = new ArrayList<>(); - while (p.nextToken() != XContentParser.Token.END_OBJECT) { - parsedScriptFields.add(new SearchSourceBuilder.ScriptField(p)); + List parsedScriptFields = new ArrayList<>(); + while (p.nextToken() != XContentParser.Token.END_OBJECT) { + parsedScriptFields.add(new SearchSourceBuilder.ScriptField(p)); } parsedScriptFields.sort(Comparator.comparing(SearchSourceBuilder.ScriptField::fieldName)); return parsedScriptFields; }, DatafeedConfig.SCRIPT_FIELDS); PARSER.declareInt(Builder::setScrollSize, DatafeedConfig.SCROLL_SIZE); PARSER.declareObject(Builder::setChunkingConfig, ChunkingConfig.STRICT_PARSER, DatafeedConfig.CHUNKING_CONFIG); - PARSER.declareObject(Builder::setDelayedDataCheckConfig, + PARSER.declareObject( + Builder::setDelayedDataCheckConfig, DelayedDataCheckConfig.STRICT_PARSER, - DatafeedConfig.DELAYED_DATA_CHECK_CONFIG); + DatafeedConfig.DELAYED_DATA_CHECK_CONFIG + ); PARSER.declareInt(Builder::setMaxEmptySearches, DatafeedConfig.MAX_EMPTY_SEARCHES); - PARSER.declareObject(Builder::setIndicesOptions, + PARSER.declareObject( + Builder::setIndicesOptions, (p, c) -> IndicesOptions.fromMap(p.map(), SearchRequest.DEFAULT_INDICES_OPTIONS), - DatafeedConfig.INDICES_OPTIONS); + DatafeedConfig.INDICES_OPTIONS + ); PARSER.declareObject(Builder::setRuntimeMappings, (p, c) -> p.map(), SearchSourceBuilder.RUNTIME_MAPPINGS_FIELD); } @@ -104,12 +110,22 @@ public class DatafeedUpdate implements Writeable, ToXContentObject { private final IndicesOptions indicesOptions; private final Map runtimeMappings; - private DatafeedUpdate(String id, String jobId, TimeValue queryDelay, TimeValue frequency, List indices, - QueryProvider queryProvider, AggProvider aggProvider, - List scriptFields, - Integer scrollSize, ChunkingConfig chunkingConfig, DelayedDataCheckConfig delayedDataCheckConfig, - Integer maxEmptySearches, IndicesOptions indicesOptions, - Map runtimeMappings) { + private DatafeedUpdate( + String id, + String jobId, + TimeValue queryDelay, + TimeValue frequency, + List indices, + QueryProvider queryProvider, + AggProvider aggProvider, + List scriptFields, + Integer scrollSize, + ChunkingConfig chunkingConfig, + DelayedDataCheckConfig delayedDataCheckConfig, + Integer maxEmptySearches, + IndicesOptions indicesOptions, + Map runtimeMappings + ) { this.id = id; this.jobId = jobId; this.queryDelay = queryDelay; @@ -274,8 +290,8 @@ Map getQuery() { } QueryBuilder getParsedQuery(NamedXContentRegistry namedXContentRegistry) throws IOException { - return XContentObjectTransformer.queryBuilderTransformer(namedXContentRegistry).fromMap(queryProvider.getQuery(), - new ArrayList<>()); + return XContentObjectTransformer.queryBuilderTransformer(namedXContentRegistry) + .fromMap(queryProvider.getQuery(), new ArrayList<>()); } Map getAggregations() { @@ -283,8 +299,7 @@ Map getAggregations() { } AggregatorFactories.Builder getParsedAgg(NamedXContentRegistry namedXContentRegistry) throws IOException { - return XContentObjectTransformer.aggregatorTransformer(namedXContentRegistry).fromMap(aggProvider.getAggs(), - new ArrayList<>()); + return XContentObjectTransformer.aggregatorTransformer(namedXContentRegistry).fromMap(aggProvider.getAggs(), new ArrayList<>()); } /** @@ -392,25 +407,39 @@ public boolean equals(Object other) { DatafeedUpdate that = (DatafeedUpdate) other; return Objects.equals(this.id, that.id) - && Objects.equals(this.jobId, that.jobId) - && Objects.equals(this.frequency, that.frequency) - && Objects.equals(this.queryDelay, that.queryDelay) - && Objects.equals(this.indices, that.indices) - && Objects.equals(this.queryProvider, that.queryProvider) - && Objects.equals(this.scrollSize, that.scrollSize) - && Objects.equals(this.aggProvider, that.aggProvider) - && Objects.equals(this.delayedDataCheckConfig, that.delayedDataCheckConfig) - && Objects.equals(this.scriptFields, that.scriptFields) - && Objects.equals(this.chunkingConfig, that.chunkingConfig) - && Objects.equals(this.maxEmptySearches, that.maxEmptySearches) - && Objects.equals(this.indicesOptions, that.indicesOptions) - && Objects.equals(this.runtimeMappings, that.runtimeMappings); + && Objects.equals(this.jobId, that.jobId) + && Objects.equals(this.frequency, that.frequency) + && Objects.equals(this.queryDelay, that.queryDelay) + && Objects.equals(this.indices, that.indices) + && Objects.equals(this.queryProvider, that.queryProvider) + && Objects.equals(this.scrollSize, that.scrollSize) + && Objects.equals(this.aggProvider, that.aggProvider) + && Objects.equals(this.delayedDataCheckConfig, that.delayedDataCheckConfig) + && Objects.equals(this.scriptFields, that.scriptFields) + && Objects.equals(this.chunkingConfig, that.chunkingConfig) + && Objects.equals(this.maxEmptySearches, that.maxEmptySearches) + && Objects.equals(this.indicesOptions, that.indicesOptions) + && Objects.equals(this.runtimeMappings, that.runtimeMappings); } @Override public int hashCode() { - return Objects.hash(id, jobId, frequency, queryDelay, indices, queryProvider, scrollSize, aggProvider, scriptFields, chunkingConfig, - delayedDataCheckConfig, maxEmptySearches, indicesOptions, runtimeMappings); + return Objects.hash( + id, + jobId, + frequency, + queryDelay, + indices, + queryProvider, + scrollSize, + aggProvider, + scriptFields, + chunkingConfig, + delayedDataCheckConfig, + maxEmptySearches, + indicesOptions, + runtimeMappings + ); } @Override @@ -420,18 +449,19 @@ public String toString() { boolean isNoop(DatafeedConfig datafeed) { return (frequency == null || Objects.equals(frequency, datafeed.getFrequency())) - && (queryDelay == null || Objects.equals(queryDelay, datafeed.getQueryDelay())) - && (indices == null || Objects.equals(indices, datafeed.getIndices())) - && (queryProvider == null || Objects.equals(queryProvider.getQuery(), datafeed.getQuery())) - && (scrollSize == null || Objects.equals(scrollSize, datafeed.getScrollSize())) - && (aggProvider == null || Objects.equals(aggProvider.getAggs(), datafeed.getAggregations())) - && (scriptFields == null || Objects.equals(scriptFields, datafeed.getScriptFields())) - && (delayedDataCheckConfig == null || Objects.equals(delayedDataCheckConfig, datafeed.getDelayedDataCheckConfig())) - && (chunkingConfig == null || Objects.equals(chunkingConfig, datafeed.getChunkingConfig())) - && (maxEmptySearches == null || Objects.equals(maxEmptySearches, datafeed.getMaxEmptySearches()) - || (maxEmptySearches == -1 && datafeed.getMaxEmptySearches() == null)) - && (indicesOptions == null || Objects.equals(indicesOptions, datafeed.getIndicesOptions())) - && (runtimeMappings == null || Objects.equals(runtimeMappings, datafeed.getRuntimeMappings())); + && (queryDelay == null || Objects.equals(queryDelay, datafeed.getQueryDelay())) + && (indices == null || Objects.equals(indices, datafeed.getIndices())) + && (queryProvider == null || Objects.equals(queryProvider.getQuery(), datafeed.getQuery())) + && (scrollSize == null || Objects.equals(scrollSize, datafeed.getScrollSize())) + && (aggProvider == null || Objects.equals(aggProvider.getAggs(), datafeed.getAggregations())) + && (scriptFields == null || Objects.equals(scriptFields, datafeed.getScriptFields())) + && (delayedDataCheckConfig == null || Objects.equals(delayedDataCheckConfig, datafeed.getDelayedDataCheckConfig())) + && (chunkingConfig == null || Objects.equals(chunkingConfig, datafeed.getChunkingConfig())) + && (maxEmptySearches == null + || Objects.equals(maxEmptySearches, datafeed.getMaxEmptySearches()) + || (maxEmptySearches == -1 && datafeed.getMaxEmptySearches() == null)) + && (indicesOptions == null || Objects.equals(indicesOptions, datafeed.getIndicesOptions())) + && (runtimeMappings == null || Objects.equals(runtimeMappings, datafeed.getRuntimeMappings())); } public static class Builder { @@ -451,8 +481,7 @@ public static class Builder { private IndicesOptions indicesOptions; private Map runtimeMappings; - public Builder() { - } + public Builder() {} public Builder(String id) { this.id = ExceptionsHelper.requireNonNull(id, DatafeedConfig.ID.getPreferredName()); @@ -472,9 +501,7 @@ public Builder(DatafeedUpdate config) { this.delayedDataCheckConfig = config.delayedDataCheckConfig; this.maxEmptySearches = config.maxEmptySearches; this.indicesOptions = config.indicesOptions; - this.runtimeMappings = config.runtimeMappings != null ? - new HashMap<>(config.runtimeMappings) : - null; + this.runtimeMappings = config.runtimeMappings != null ? new HashMap<>(config.runtimeMappings) : null; } public Builder setId(String datafeedId) { @@ -554,8 +581,11 @@ public Builder setChunkingConfig(ChunkingConfig chunkingConfig) { public Builder setMaxEmptySearches(int maxEmptySearches) { if (maxEmptySearches < -1 || maxEmptySearches == 0) { - String msg = Messages.getMessage(Messages.DATAFEED_CONFIG_INVALID_OPTION_VALUE, - DatafeedConfig.MAX_EMPTY_SEARCHES.getPreferredName(), maxEmptySearches); + String msg = Messages.getMessage( + Messages.DATAFEED_CONFIG_INVALID_OPTION_VALUE, + DatafeedConfig.MAX_EMPTY_SEARCHES.getPreferredName(), + maxEmptySearches + ); throw ExceptionsHelper.badRequestException(msg); } this.maxEmptySearches = maxEmptySearches; @@ -573,8 +603,22 @@ public Builder setRuntimeMappings(Map runtimeMappings) { } public DatafeedUpdate build() { - return new DatafeedUpdate(id, jobId, queryDelay, frequency, indices, queryProvider, aggProvider, scriptFields, scrollSize, - chunkingConfig, delayedDataCheckConfig, maxEmptySearches, indicesOptions, runtimeMappings); + return new DatafeedUpdate( + id, + jobId, + queryDelay, + frequency, + indices, + queryProvider, + aggProvider, + scriptFields, + scrollSize, + chunkingConfig, + delayedDataCheckConfig, + maxEmptySearches, + indicesOptions, + runtimeMappings + ); } } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/datafeed/DelayedDataCheckConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/datafeed/DelayedDataCheckConfig.java index 00b7861b6bb98..d4d26710f3571 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/datafeed/DelayedDataCheckConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/datafeed/DelayedDataCheckConfig.java @@ -6,13 +6,13 @@ */ package org.elasticsearch.xpack.core.ml.datafeed; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -35,13 +35,17 @@ public class DelayedDataCheckConfig implements ToXContentObject, Writeable { private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { ConstructingObjectParser parser = new ConstructingObjectParser<>( - "delayed_data_check_config", ignoreUnknownFields, a -> new DelayedDataCheckConfig((Boolean) a[0], (TimeValue) a[1])); + "delayed_data_check_config", + ignoreUnknownFields, + a -> new DelayedDataCheckConfig((Boolean) a[0], (TimeValue) a[1]) + ); parser.declareBoolean(ConstructingObjectParser.constructorArg(), ENABLED); parser.declareString( ConstructingObjectParser.optionalConstructorArg(), text -> TimeValue.parseTimeValue(text, CHECK_WINDOW.getPreferredName()), - CHECK_WINDOW); + CHECK_WINDOW + ); return parser; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/datafeed/extractor/ExtractorUtils.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/datafeed/extractor/ExtractorUtils.java index 35fae4150cd6e..f79daacf86814 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/datafeed/extractor/ExtractorUtils.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/datafeed/extractor/ExtractorUtils.java @@ -87,14 +87,14 @@ public static boolean isHistogram(AggregationBuilder aggregationBuilder) { public static boolean isCompositeWithDateHistogramSource(AggregationBuilder aggregationBuilder) { return aggregationBuilder instanceof CompositeAggregationBuilder && ((CompositeAggregationBuilder) aggregationBuilder).sources() - .stream() - .anyMatch(DateHistogramValuesSourceBuilder.class::isInstance); + .stream() + .anyMatch(DateHistogramValuesSourceBuilder.class::isInstance); } public static DateHistogramValuesSourceBuilder getDateHistogramValuesSource(CompositeAggregationBuilder compositeAggregationBuilder) { for (CompositeValuesSourceBuilder valuesSourceBuilder : compositeAggregationBuilder.sources()) { if (valuesSourceBuilder instanceof DateHistogramValuesSourceBuilder) { - return (DateHistogramValuesSourceBuilder)valuesSourceBuilder; + return (DateHistogramValuesSourceBuilder) valuesSourceBuilder; } } throw ExceptionsHelper.badRequestException("[composite] aggregations require exactly one [date_histogram] value source"); @@ -118,7 +118,7 @@ public static long getHistogramIntervalMillis(AggregationBuilder histogramAggreg ); } else if (histogramAggregation instanceof CompositeAggregationBuilder) { return validateAndGetDateHistogramInterval( - DateHistogramAggOrValueSource.fromCompositeAgg((CompositeAggregationBuilder)histogramAggregation) + DateHistogramAggOrValueSource.fromCompositeAgg((CompositeAggregationBuilder) histogramAggregation) ); } else { throw new IllegalStateException("Invalid histogram aggregation [" + histogramAggregation.getName() + "]"); @@ -139,7 +139,7 @@ private static long validateAndGetDateHistogramInterval(DateHistogramAggOrValueS return validateAndGetCalendarInterval(dateHistogram.getCalendarInterval().toString()); } else if (dateHistogram.getFixedInterval() != null) { return dateHistogram.getFixedInterval().estimateMillis(); - } else { + } else { throw new IllegalArgumentException("Must specify an interval for date_histogram"); } } @@ -181,9 +181,12 @@ public static long validateAndGetCalendarInterval(String calendarInterval) { } private static String invalidDateHistogramCalendarIntervalMessage(String interval) { - throw ExceptionsHelper.badRequestException("When specifying a date_histogram calendar interval [" - + interval + "], ML does not accept intervals longer than a week because of " + - "variable lengths of periods greater than a week"); + throw ExceptionsHelper.badRequestException( + "When specifying a date_histogram calendar interval [" + + interval + + "], ML does not accept intervals longer than a week because of " + + "variable lengths of periods greater than a week" + ); } private static class DateHistogramAggOrValueSource { @@ -206,21 +209,15 @@ private DateHistogramAggOrValueSource(DateHistogramAggregationBuilder agg, DateH } private ZoneId timeZone() { - return agg != null ? - agg.timeZone() : - sourceBuilder.timeZone(); + return agg != null ? agg.timeZone() : sourceBuilder.timeZone(); } private DateHistogramInterval getFixedInterval() { - return agg != null ? - agg.getFixedInterval() : - sourceBuilder.getIntervalAsFixed(); + return agg != null ? agg.getFixedInterval() : sourceBuilder.getIntervalAsFixed(); } private DateHistogramInterval getCalendarInterval() { - return agg != null ? - agg.getCalendarInterval() : - sourceBuilder.getIntervalAsCalendar(); + return agg != null ? agg.getCalendarInterval() : sourceBuilder.getIntervalAsCalendar(); } } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsConfig.java index 56c4c25f5d499..d641b3f45e698 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsConfig.java @@ -7,19 +7,19 @@ package org.elasticsearch.xpack.core.ml.dataframe; import org.elasticsearch.Version; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.common.xcontent.XContentParserUtils; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.search.fetch.subphase.FetchSourceContext; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.XContentParserUtils; -import org.elasticsearch.search.fetch.subphase.FetchSourceContext; import org.elasticsearch.xpack.core.common.time.TimeUtils; import org.elasticsearch.xpack.core.ml.dataframe.analyses.DataFrameAnalysis; import org.elasticsearch.xpack.core.ml.job.messages.Messages; @@ -79,12 +79,18 @@ private static ObjectParser createParser(boolean ignoreUnknownFie parser.declareObject(Builder::setSource, DataFrameAnalyticsSource.createParser(ignoreUnknownFields), SOURCE); parser.declareObject(Builder::setDest, DataFrameAnalyticsDest.createParser(ignoreUnknownFields), DEST); parser.declareObject(Builder::setAnalysis, (p, c) -> parseAnalysis(p, ignoreUnknownFields), ANALYSIS); - parser.declareField(Builder::setAnalyzedFields, + parser.declareField( + Builder::setAnalyzedFields, (p, c) -> FetchSourceContext.fromXContent(p), ANALYZED_FIELDS, - OBJECT_ARRAY_BOOLEAN_OR_STRING); - parser.declareField(Builder::setModelMemoryLimit, - (p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), MODEL_MEMORY_LIMIT.getPreferredName()), MODEL_MEMORY_LIMIT, VALUE); + OBJECT_ARRAY_BOOLEAN_OR_STRING + ); + parser.declareField( + Builder::setModelMemoryLimit, + (p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), MODEL_MEMORY_LIMIT.getPreferredName()), + MODEL_MEMORY_LIMIT, + VALUE + ); parser.declareBoolean(Builder::setAllowLazyStart, ALLOW_LAZY_START); parser.declareInt(Builder::setMaxNumThreads, MAX_NUM_THREADS); if (ignoreUnknownFields) { @@ -92,10 +98,12 @@ private static ObjectParser createParser(boolean ignoreUnknownFie // (For config, headers are explicitly transferred from the auth headers by code in the put data frame actions.) parser.declareObject(Builder::setHeaders, (p, c) -> p.mapStrings(), HEADERS); // Creation time is set automatically during PUT, so create_time supplied in the _body_ of a REST request will be rejected. - parser.declareField(Builder::setCreateTime, + parser.declareField( + Builder::setCreateTime, p -> TimeUtils.parseTimeFieldToInstant(p, CREATE_TIME.getPreferredName()), CREATE_TIME, - ObjectParser.ValueType.VALUE); + ObjectParser.ValueType.VALUE + ); // Version is set automatically during PUT, so version supplied in the _body_ of a REST request will be rejected. parser.declareString(Builder::setVersion, Version::fromString, VERSION); } @@ -131,10 +139,20 @@ private static DataFrameAnalysis parseAnalysis(XContentParser parser, boolean ig private final boolean allowLazyStart; private final int maxNumThreads; - private DataFrameAnalyticsConfig(String id, String description, DataFrameAnalyticsSource source, DataFrameAnalyticsDest dest, - DataFrameAnalysis analysis, Map headers, ByteSizeValue modelMemoryLimit, - FetchSourceContext analyzedFields, Instant createTime, Version version, boolean allowLazyStart, - Integer maxNumThreads) { + private DataFrameAnalyticsConfig( + String id, + String description, + DataFrameAnalyticsSource source, + DataFrameAnalyticsDest dest, + DataFrameAnalysis analysis, + Map headers, + ByteSizeValue modelMemoryLimit, + FetchSourceContext analyzedFields, + Instant createTime, + Version version, + boolean allowLazyStart, + Integer maxNumThreads + ) { this.id = ExceptionsHelper.requireNonNull(id, ID); this.description = description; this.source = ExceptionsHelper.requireNonNull(source, SOURCE); @@ -254,8 +272,11 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.field(SOURCE.getPreferredName(), source); builder.field(DEST.getPreferredName(), dest); builder.startObject(ANALYSIS.getPreferredName()); - builder.field(analysis.getWriteableName(), analysis, - new MapParams(Collections.singletonMap(VERSION.getPreferredName(), version == null ? null : version.toString()))); + builder.field( + analysis.getWriteableName(), + analysis, + new MapParams(Collections.singletonMap(VERSION.getPreferredName(), version == null ? null : version.toString())) + ); builder.endObject(); if (analyzedFields != null) { builder.field(ANALYZED_FIELDS.getPreferredName(), analyzedFields); @@ -316,8 +337,20 @@ public boolean equals(Object o) { @Override public int hashCode() { - return Objects.hash(id, description, source, dest, analysis, headers, getModelMemoryLimit(), analyzedFields, createTime, version, - allowLazyStart, maxNumThreads); + return Objects.hash( + id, + description, + source, + dest, + analysis, + headers, + getModelMemoryLimit(), + analyzedFields, + createTime, + version, + allowLazyStart, + maxNumThreads + ); } @Override @@ -334,7 +367,7 @@ public static String documentId(String id) { */ @Nullable public static String extractJobIdFromDocId(String docId) { - String jobId = docId.replaceAll("^" + TYPE +"-", ""); + String jobId = docId.replaceAll("^" + TYPE + "-", ""); return jobId.equals(docId) ? null : jobId; } @@ -448,8 +481,20 @@ public Builder setMaxNumThreads(Integer maxNumThreads) { */ public DataFrameAnalyticsConfig build() { applyMaxModelMemoryLimit(); - return new DataFrameAnalyticsConfig(id, description, source, dest, analysis, headers, modelMemoryLimit, analyzedFields, - createTime, version, allowLazyStart, maxNumThreads); + return new DataFrameAnalyticsConfig( + id, + description, + source, + dest, + analysis, + headers, + modelMemoryLimit, + analyzedFields, + createTime, + version, + allowLazyStart, + maxNumThreads + ); } /** @@ -470,7 +515,8 @@ public DataFrameAnalyticsConfig buildForExplain() { createTime, version, allowLazyStart, - maxNumThreads); + maxNumThreads + ); } private void applyMaxModelMemoryLimit() { @@ -481,13 +527,17 @@ private void applyMaxModelMemoryLimit() { // Explicit setting lower than minimum is an error throw ExceptionsHelper.badRequestException( Messages.getMessage( - Messages.JOB_CONFIG_MODEL_MEMORY_LIMIT_TOO_LOW, modelMemoryLimit, MIN_MODEL_MEMORY_LIMIT.getStringRep())); + Messages.JOB_CONFIG_MODEL_MEMORY_LIMIT_TOO_LOW, + modelMemoryLimit, + MIN_MODEL_MEMORY_LIMIT.getStringRep() + ) + ); } if (maxModelMemoryIsSet && modelMemoryLimit.compareTo(maxModelMemoryLimit) > 0) { // Explicit setting higher than limit is an error throw ExceptionsHelper.badRequestException( - Messages.getMessage( - Messages.JOB_CONFIG_MODEL_MEMORY_LIMIT_GREATER_THAN_MAX, modelMemoryLimit, maxModelMemoryLimit)); + Messages.getMessage(Messages.JOB_CONFIG_MODEL_MEMORY_LIMIT_GREATER_THAN_MAX, modelMemoryLimit, maxModelMemoryLimit) + ); } } else { // Default is silently capped if higher than limit diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsConfigUpdate.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsConfigUpdate.java index bd5ea5dbdf82a..49f5322004c6b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsConfigUpdate.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsConfigUpdate.java @@ -6,11 +6,11 @@ */ package org.elasticsearch.xpack.core.ml.dataframe; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -25,8 +25,10 @@ public class DataFrameAnalyticsConfigUpdate implements Writeable, ToXContentObject { - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("data_frame_analytics_config_update", args -> new Builder((String) args[0])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "data_frame_analytics_config_update", + args -> new Builder((String) args[0]) + ); static { PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), DataFrameAnalyticsConfig.ID); @@ -35,7 +37,8 @@ public class DataFrameAnalyticsConfigUpdate implements Writeable, ToXContentObje Builder::setModelMemoryLimit, (p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), DataFrameAnalyticsConfig.MODEL_MEMORY_LIMIT.getPreferredName()), DataFrameAnalyticsConfig.MODEL_MEMORY_LIMIT, - VALUE); + VALUE + ); PARSER.declareBoolean(Builder::setAllowLazyStart, DataFrameAnalyticsConfig.ALLOW_LAZY_START); PARSER.declareInt(Builder::setMaxNumThreads, DataFrameAnalyticsConfig.MAX_NUM_THREADS); } @@ -46,19 +49,23 @@ public class DataFrameAnalyticsConfigUpdate implements Writeable, ToXContentObje private final Boolean allowLazyStart; private final Integer maxNumThreads; - private DataFrameAnalyticsConfigUpdate(String id, - @Nullable String description, - @Nullable ByteSizeValue modelMemoryLimit, - @Nullable Boolean allowLazyStart, - @Nullable Integer maxNumThreads) { + private DataFrameAnalyticsConfigUpdate( + String id, + @Nullable String description, + @Nullable ByteSizeValue modelMemoryLimit, + @Nullable Boolean allowLazyStart, + @Nullable Integer maxNumThreads + ) { this.id = id; this.description = description; this.modelMemoryLimit = modelMemoryLimit; this.allowLazyStart = allowLazyStart; if (maxNumThreads != null && maxNumThreads < 1) { - throw ExceptionsHelper.badRequestException("[{}] must be a positive integer", - DataFrameAnalyticsConfig.MAX_NUM_THREADS.getPreferredName()); + throw ExceptionsHelper.badRequestException( + "[{}] must be a positive integer", + DataFrameAnalyticsConfig.MAX_NUM_THREADS.getPreferredName() + ); } this.maxNumThreads = maxNumThreads; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsDest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsDest.java index 3bed995cdd383..fd4c6637bbd6d 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsDest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsDest.java @@ -6,12 +6,12 @@ */ package org.elasticsearch.xpack.core.ml.dataframe; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; @@ -27,8 +27,11 @@ public class DataFrameAnalyticsDest implements Writeable, ToXContentObject { private static final String DEFAULT_RESULTS_FIELD = "ml"; public static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { - ConstructingObjectParser parser = new ConstructingObjectParser<>("data_frame_analytics_dest", - ignoreUnknownFields, a -> new DataFrameAnalyticsDest((String) a[0], (String) a[1])); + ConstructingObjectParser parser = new ConstructingObjectParser<>( + "data_frame_analytics_dest", + ignoreUnknownFields, + a -> new DataFrameAnalyticsDest((String) a[0], (String) a[1]) + ); parser.declareString(ConstructingObjectParser.constructorArg(), INDEX); parser.declareString(ConstructingObjectParser.optionalConstructorArg(), RESULTS_FIELD); return parser; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsSource.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsSource.java index 17e8a730aa986..20d59ae2b3f53 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsSource.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsSource.java @@ -7,21 +7,21 @@ package org.elasticsearch.xpack.core.ml.dataframe; import org.elasticsearch.ElasticsearchException; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.regex.Regex; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.index.query.QueryBuilder; +import org.elasticsearch.search.builder.SearchSourceBuilder; +import org.elasticsearch.search.fetch.subphase.FetchSourceContext; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.index.query.QueryBuilder; -import org.elasticsearch.search.builder.SearchSourceBuilder; -import org.elasticsearch.search.fetch.subphase.FetchSourceContext; import org.elasticsearch.xpack.core.ml.job.messages.Messages; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; import org.elasticsearch.xpack.core.ml.utils.QueryProvider; @@ -43,23 +43,35 @@ public class DataFrameAnalyticsSource implements Writeable, ToXContentObject { public static final ParseField QUERY = new ParseField("query"); public static final ParseField _SOURCE = new ParseField("_source"); - @SuppressWarnings({ "unchecked"}) + @SuppressWarnings({ "unchecked" }) public static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { - ConstructingObjectParser parser = new ConstructingObjectParser<>("data_frame_analytics_source", - ignoreUnknownFields, a -> new DataFrameAnalyticsSource( + ConstructingObjectParser parser = new ConstructingObjectParser<>( + "data_frame_analytics_source", + ignoreUnknownFields, + a -> new DataFrameAnalyticsSource( ((List) a[0]).toArray(new String[0]), (QueryProvider) a[1], (FetchSourceContext) a[2], - (Map) a[3])); + (Map) a[3] + ) + ); parser.declareStringArray(ConstructingObjectParser.constructorArg(), INDEX); - parser.declareObject(ConstructingObjectParser.optionalConstructorArg(), - (p, c) -> QueryProvider.fromXContent(p, ignoreUnknownFields, Messages.DATA_FRAME_ANALYTICS_BAD_QUERY_FORMAT), QUERY); - parser.declareField(ConstructingObjectParser.optionalConstructorArg(), + parser.declareObject( + ConstructingObjectParser.optionalConstructorArg(), + (p, c) -> QueryProvider.fromXContent(p, ignoreUnknownFields, Messages.DATA_FRAME_ANALYTICS_BAD_QUERY_FORMAT), + QUERY + ); + parser.declareField( + ConstructingObjectParser.optionalConstructorArg(), (p, c) -> FetchSourceContext.fromXContent(p), _SOURCE, - ObjectParser.ValueType.OBJECT_ARRAY_BOOLEAN_OR_STRING); - parser.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> p.map(), - SearchSourceBuilder.RUNTIME_MAPPINGS_FIELD); + ObjectParser.ValueType.OBJECT_ARRAY_BOOLEAN_OR_STRING + ); + parser.declareObject( + ConstructingObjectParser.optionalConstructorArg(), + (p, c) -> p.map(), + SearchSourceBuilder.RUNTIME_MAPPINGS_FIELD + ); return parser; } @@ -68,8 +80,12 @@ public static ConstructingObjectParser createPar private final FetchSourceContext sourceFiltering; private final Map runtimeMappings; - public DataFrameAnalyticsSource(String[] index, @Nullable QueryProvider queryProvider, @Nullable FetchSourceContext sourceFiltering, - @Nullable Map runtimeMappings) { + public DataFrameAnalyticsSource( + String[] index, + @Nullable QueryProvider queryProvider, + @Nullable FetchSourceContext sourceFiltering, + @Nullable Map runtimeMappings + ) { this.index = ExceptionsHelper.requireNonNull(index, INDEX); if (index.length == 0) { throw new IllegalArgumentException("source.index must specify at least one index"); @@ -96,8 +112,13 @@ public DataFrameAnalyticsSource(StreamInput in) throws IOException { public DataFrameAnalyticsSource(DataFrameAnalyticsSource other) { this.index = Arrays.copyOf(other.index, other.index.length); this.queryProvider = new QueryProvider(other.queryProvider); - this.sourceFiltering = other.sourceFiltering == null ? null : new FetchSourceContext( - other.sourceFiltering.fetchSource(), other.sourceFiltering.includes(), other.sourceFiltering.excludes()); + this.sourceFiltering = other.sourceFiltering == null + ? null + : new FetchSourceContext( + other.sourceFiltering.fetchSource(), + other.sourceFiltering.includes(), + other.sourceFiltering.excludes() + ); this.runtimeMappings = Collections.unmodifiableMap(new HashMap<>(other.runtimeMappings)); } @@ -184,8 +205,7 @@ QueryProvider getQueryProvider() { public List getQueryDeprecations(NamedXContentRegistry namedXContentRegistry) { List deprecations = new ArrayList<>(); try { - XContentObjectTransformer.queryBuilderTransformer(namedXContentRegistry).fromMap(queryProvider.getQuery(), - deprecations); + XContentObjectTransformer.queryBuilderTransformer(namedXContentRegistry).fromMap(queryProvider.getQuery(), deprecations); } catch (Exception exception) { // Certain thrown exceptions wrap up the real Illegal argument making it hard to determine cause for the user if (exception.getCause() instanceof IllegalArgumentException) { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsState.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsState.java index 9f5dd4f3c4ab6..08ab9e8b9b397 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsState.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsState.java @@ -20,7 +20,13 @@ public enum DataFrameAnalyticsState implements Writeable, MemoryTrackedTaskState // States reindexing and analyzing are no longer used. // However, we need to keep them for BWC as tasks may be // awaiting assignment in older versioned nodes. - STARTED, REINDEXING, ANALYZING, STOPPING, STOPPED, FAILED, STARTING; + STARTED, + REINDEXING, + ANALYZING, + STOPPING, + STOPPED, + FAILED, + STARTING; public static DataFrameAnalyticsState fromString(String name) { return valueOf(name.trim().toUpperCase(Locale.ROOT)); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsTaskState.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsTaskState.java index f705096c04fa0..e6fdc7886ce53 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsTaskState.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsTaskState.java @@ -8,13 +8,13 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.persistent.PersistentTaskState; +import org.elasticsearch.persistent.PersistentTasksCustomMetadata; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.persistent.PersistentTaskState; -import org.elasticsearch.persistent.PersistentTasksCustomMetadata; import org.elasticsearch.xpack.core.ml.MlTasks; import java.io.IOException; @@ -32,9 +32,11 @@ public class DataFrameAnalyticsTaskState implements PersistentTaskState { private final long allocationId; private final String reason; - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(NAME, true, - a -> new DataFrameAnalyticsTaskState((DataFrameAnalyticsState) a[0], (long) a[1], (String) a[2])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + true, + a -> new DataFrameAnalyticsTaskState((DataFrameAnalyticsState) a[0], (long) a[1], (String) a[2]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), DataFrameAnalyticsState::fromString, STATE); @@ -104,9 +106,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; DataFrameAnalyticsTaskState that = (DataFrameAnalyticsTaskState) o; - return allocationId == that.allocationId && - state == that.state && - Objects.equals(reason, that.reason); + return allocationId == that.allocationId && state == that.state && Objects.equals(reason, that.reason); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/BoostedTreeParams.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/BoostedTreeParams.java index 725fb58a8515a..1a6d6739106cf 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/BoostedTreeParams.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/BoostedTreeParams.java @@ -6,12 +6,12 @@ */ package org.elasticsearch.xpack.core.ml.dataframe.analyses; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.AbstractObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentFragment; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; @@ -41,8 +41,9 @@ public class BoostedTreeParams implements ToXContentFragment, Writeable { public static final ParseField SOFT_TREE_DEPTH_LIMIT = new ParseField("soft_tree_depth_limit"); public static final ParseField SOFT_TREE_DEPTH_TOLERANCE = new ParseField("soft_tree_depth_tolerance"); public static final ParseField DOWNSAMPLE_FACTOR = new ParseField("downsample_factor"); - public static final ParseField MAX_OPTIMIZATION_ROUNDS_PER_HYPERPARAMETER = - new ParseField("max_optimization_rounds_per_hyperparameter"); + public static final ParseField MAX_OPTIMIZATION_ROUNDS_PER_HYPERPARAMETER = new ParseField( + "max_optimization_rounds_per_hyperparameter" + ); static void declareFields(AbstractObjectParser parser) { parser.declareDouble(optionalConstructorArg(), LAMBDA); @@ -72,18 +73,20 @@ static void declareFields(AbstractObjectParser parser) { private final Double downsampleFactor; private final Integer maxOptimizationRoundsPerHyperparameter; - public BoostedTreeParams(@Nullable Double lambda, - @Nullable Double gamma, - @Nullable Double eta, - @Nullable Integer maxTrees, - @Nullable Double featureBagFraction, - @Nullable Integer numTopFeatureImportanceValues, - @Nullable Double alpha, - @Nullable Double etaGrowthRatePerTree, - @Nullable Double softTreeDepthLimit, - @Nullable Double softTreeDepthTolerance, - @Nullable Double downsampleFactor, - @Nullable Integer maxOptimizationRoundsPerHyperparameter) { + public BoostedTreeParams( + @Nullable Double lambda, + @Nullable Double gamma, + @Nullable Double eta, + @Nullable Integer maxTrees, + @Nullable Double featureBagFraction, + @Nullable Integer numTopFeatureImportanceValues, + @Nullable Double alpha, + @Nullable Double etaGrowthRatePerTree, + @Nullable Double softTreeDepthLimit, + @Nullable Double softTreeDepthTolerance, + @Nullable Double downsampleFactor, + @Nullable Integer maxOptimizationRoundsPerHyperparameter + ) { if (lambda != null && lambda < 0) { throw ExceptionsHelper.badRequestException("[{}] must be a non-negative double", LAMBDA.getPreferredName()); } @@ -100,8 +103,10 @@ public BoostedTreeParams(@Nullable Double lambda, throw ExceptionsHelper.badRequestException("[{}] must be a double in (0, 1]", FEATURE_BAG_FRACTION.getPreferredName()); } if (numTopFeatureImportanceValues != null && numTopFeatureImportanceValues < 0) { - throw ExceptionsHelper.badRequestException("[{}] must be a non-negative integer", - NUM_TOP_FEATURE_IMPORTANCE_VALUES.getPreferredName()); + throw ExceptionsHelper.badRequestException( + "[{}] must be a non-negative integer", + NUM_TOP_FEATURE_IMPORTANCE_VALUES.getPreferredName() + ); } if (alpha != null && alpha < 0) { throw ExceptionsHelper.badRequestException("[{}] must be a non-negative double", ALPHA.getPreferredName()); @@ -113,16 +118,20 @@ public BoostedTreeParams(@Nullable Double lambda, throw ExceptionsHelper.badRequestException("[{}] must be a non-negative double", SOFT_TREE_DEPTH_LIMIT.getPreferredName()); } if (softTreeDepthTolerance != null && softTreeDepthTolerance < 0.01) { - throw ExceptionsHelper.badRequestException("[{}] must be a double greater than or equal to 0.01", - SOFT_TREE_DEPTH_TOLERANCE.getPreferredName()); + throw ExceptionsHelper.badRequestException( + "[{}] must be a double greater than or equal to 0.01", + SOFT_TREE_DEPTH_TOLERANCE.getPreferredName() + ); } if (downsampleFactor != null && (downsampleFactor <= 0 || downsampleFactor > 1.0)) { throw ExceptionsHelper.badRequestException("[{}] must be a double in (0, 1]", DOWNSAMPLE_FACTOR.getPreferredName()); } if (maxOptimizationRoundsPerHyperparameter != null - && (maxOptimizationRoundsPerHyperparameter < 0 || maxOptimizationRoundsPerHyperparameter > 20)) { - throw ExceptionsHelper.badRequestException("[{}] must be an integer in [0, 20]", - MAX_OPTIMIZATION_ROUNDS_PER_HYPERPARAMETER.getPreferredName()); + && (maxOptimizationRoundsPerHyperparameter < 0 || maxOptimizationRoundsPerHyperparameter > 20)) { + throw ExceptionsHelper.badRequestException( + "[{}] must be an integer in [0, 20]", + MAX_OPTIMIZATION_ROUNDS_PER_HYPERPARAMETER.getPreferredName() + ); } this.lambda = lambda; this.gamma = gamma; @@ -320,8 +329,20 @@ public boolean equals(Object o) { @Override public int hashCode() { - return Objects.hash(lambda, gamma, eta, maxTrees, featureBagFraction, numTopFeatureImportanceValues, alpha, etaGrowthRatePerTree, - softTreeDepthLimit, softTreeDepthTolerance, downsampleFactor, maxOptimizationRoundsPerHyperparameter); + return Objects.hash( + lambda, + gamma, + eta, + maxTrees, + featureBagFraction, + numTopFeatureImportanceValues, + alpha, + etaGrowthRatePerTree, + softTreeDepthLimit, + softTreeDepthTolerance, + downsampleFactor, + maxOptimizationRoundsPerHyperparameter + ); } public static Builder builder() { @@ -421,8 +442,20 @@ public Builder setMaxOptimizationRoundsPerHyperparameter(Integer maxOptimization } public BoostedTreeParams build() { - return new BoostedTreeParams(lambda, gamma, eta, maxTrees, featureBagFraction, numTopFeatureImportanceValues, alpha, - etaGrowthRatePerTree, softTreeDepthLimit, softTreeDepthTolerance, downsampleFactor, maxOptimizationRoundsPerHyperparameter); + return new BoostedTreeParams( + lambda, + gamma, + eta, + maxTrees, + featureBagFraction, + numTopFeatureImportanceValues, + alpha, + etaGrowthRatePerTree, + softTreeDepthLimit, + softTreeDepthTolerance, + downsampleFactor, + maxOptimizationRoundsPerHyperparameter + ); } } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/Classification.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/Classification.java index 5da9e2784376f..964b7a5a05bde 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/Classification.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/Classification.java @@ -9,18 +9,18 @@ import org.elasticsearch.Version; import org.elasticsearch.action.fieldcaps.FieldCapabilities; import org.elasticsearch.action.fieldcaps.FieldCapabilitiesResponse; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Randomness; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.xcontent.ConstructingObjectParser; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.core.Nullable; import org.elasticsearch.index.mapper.BooleanFieldMapper; import org.elasticsearch.index.mapper.KeywordFieldMapper; import org.elasticsearch.index.mapper.NestedObjectMapper; import org.elasticsearch.index.mapper.NumberFieldMapper; +import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.inference.preprocessing.LenientlyParsedPreProcessor; import org.elasticsearch.xpack.core.ml.inference.preprocessing.PreProcessor; import org.elasticsearch.xpack.core.ml.inference.preprocessing.StrictlyParsedPreProcessor; @@ -77,15 +77,29 @@ private static ConstructingObjectParser createParser(boole lenient, a -> new Classification( (String) a[0], - new BoostedTreeParams((Double) a[1], (Double) a[2], (Double) a[3], (Integer) a[4], (Double) a[5], (Integer) a[6], - (Double) a[7], (Double) a[8], (Double) a[9], (Double) a[10], (Double) a[11], (Integer) a[12]), + new BoostedTreeParams( + (Double) a[1], + (Double) a[2], + (Double) a[3], + (Integer) a[4], + (Double) a[5], + (Integer) a[6], + (Double) a[7], + (Double) a[8], + (Double) a[9], + (Double) a[10], + (Double) a[11], + (Integer) a[12] + ), (String) a[13], (ClassAssignmentObjective) a[14], (Integer) a[15], (Double) a[16], (Long) a[17], (List) a[18], - (Boolean) a[19])); + (Boolean) a[19] + ) + ); parser.declareString(constructorArg(), DEPENDENT_VARIABLE); BoostedTreeParams.declareFields(parser); parser.declareString(optionalConstructorArg(), PREDICTION_FIELD_NAME); @@ -93,12 +107,14 @@ private static ConstructingObjectParser createParser(boole parser.declareInt(optionalConstructorArg(), NUM_TOP_CLASSES); parser.declareDouble(optionalConstructorArg(), TRAINING_PERCENT); parser.declareLong(optionalConstructorArg(), RANDOMIZE_SEED); - parser.declareNamedObjects(optionalConstructorArg(), - (p, c, n) -> lenient ? - p.namedObject(LenientlyParsedPreProcessor.class, n, new PreProcessor.PreProcessorParseContext(true)) : - p.namedObject(StrictlyParsedPreProcessor.class, n, new PreProcessor.PreProcessorParseContext(true)), + parser.declareNamedObjects( + optionalConstructorArg(), + (p, c, n) -> lenient + ? p.namedObject(LenientlyParsedPreProcessor.class, n, new PreProcessor.PreProcessorParseContext(true)) + : p.namedObject(StrictlyParsedPreProcessor.class, n, new PreProcessor.PreProcessorParseContext(true)), (classification) -> {/*TODO should we throw if this is not set?*/}, - FEATURE_PROCESSORS); + FEATURE_PROCESSORS + ); parser.declareBoolean(optionalConstructorArg(), EARLY_STOPPING_ENABLED); return parser; } @@ -107,10 +123,11 @@ public static Classification fromXContent(XContentParser parser, boolean ignoreU return ignoreUnknownFields ? LENIENT_PARSER.apply(parser, null) : STRICT_PARSER.apply(parser, null); } - private static final Set ALLOWED_DEPENDENT_VARIABLE_TYPES = - Stream.of(Types.categorical(), Types.discreteNumerical(), Types.bool()) - .flatMap(Set::stream) - .collect(Collectors.toUnmodifiableSet()); + private static final Set ALLOWED_DEPENDENT_VARIABLE_TYPES = Stream.of( + Types.categorical(), + Types.discreteNumerical(), + Types.bool() + ).flatMap(Set::stream).collect(Collectors.toUnmodifiableSet()); /** * Name of the parameter passed down to C++. * This parameter is used to decide which JSON data type from {string, int, bool} to use when writing the prediction. @@ -124,12 +141,7 @@ public static Classification fromXContent(XContentParser parser, boolean ignoreU private static final int DEFAULT_NUM_TOP_CLASSES = 2; private static final List PROGRESS_PHASES = Collections.unmodifiableList( - Arrays.asList( - "feature_selection", - "coarse_parameter_search", - "fine_tuning_parameters", - "final_training" - ) + Arrays.asList("feature_selection", "coarse_parameter_search", "fine_tuning_parameters", "final_training") ); static final Map FEATURE_IMPORTANCE_MAPPING; @@ -165,18 +177,22 @@ public static Classification fromXContent(XContentParser parser, boolean ignoreU private final List featureProcessors; private final boolean earlyStoppingEnabled; - public Classification(String dependentVariable, - BoostedTreeParams boostedTreeParams, - @Nullable String predictionFieldName, - @Nullable ClassAssignmentObjective classAssignmentObjective, - @Nullable Integer numTopClasses, - @Nullable Double trainingPercent, - @Nullable Long randomizeSeed, - @Nullable List featureProcessors, - @Nullable Boolean earlyStoppingEnabled) { + public Classification( + String dependentVariable, + BoostedTreeParams boostedTreeParams, + @Nullable String predictionFieldName, + @Nullable ClassAssignmentObjective classAssignmentObjective, + @Nullable Integer numTopClasses, + @Nullable Double trainingPercent, + @Nullable Long randomizeSeed, + @Nullable List featureProcessors, + @Nullable Boolean earlyStoppingEnabled + ) { if (numTopClasses != null && (numTopClasses < -1 || numTopClasses > 1000)) { throw ExceptionsHelper.badRequestException( - "[{}] must be an integer in [0, 1000] or a special value -1", NUM_TOP_CLASSES.getPreferredName()); + "[{}] must be an integer in [0, 1000] or a special value -1", + NUM_TOP_CLASSES.getPreferredName() + ); } if (trainingPercent != null && (trainingPercent <= 0.0 || trainingPercent > 100.0)) { throw ExceptionsHelper.badRequestException("[{}] must be a positive double in (0, 100]", TRAINING_PERCENT.getPreferredName()); @@ -184,8 +200,9 @@ public Classification(String dependentVariable, this.dependentVariable = ExceptionsHelper.requireNonNull(dependentVariable, DEPENDENT_VARIABLE); this.boostedTreeParams = ExceptionsHelper.requireNonNull(boostedTreeParams, BoostedTreeParams.NAME); this.predictionFieldName = predictionFieldName == null ? dependentVariable + "_prediction" : predictionFieldName; - this.classAssignmentObjective = classAssignmentObjective == null ? - ClassAssignmentObjective.MAXIMIZE_MINIMUM_RECALL : classAssignmentObjective; + this.classAssignmentObjective = classAssignmentObjective == null + ? ClassAssignmentObjective.MAXIMIZE_MINIMUM_RECALL + : classAssignmentObjective; this.numTopClasses = numTopClasses == null ? DEFAULT_NUM_TOP_CLASSES : numTopClasses; this.trainingPercent = trainingPercent == null ? 100.0 : trainingPercent; this.randomizeSeed = randomizeSeed == null ? Randomness.get().nextLong() : randomizeSeed; @@ -324,8 +341,10 @@ public Map getParams(FieldInfo fieldInfo) { params.put(NUM_CLASSES, fieldInfo.getCardinality(dependentVariable)); params.put(TRAINING_PERCENT.getPreferredName(), trainingPercent); if (featureProcessors.isEmpty() == false) { - params.put(FEATURE_PROCESSORS.getPreferredName(), - featureProcessors.stream().map(p -> Collections.singletonMap(p.getName(), p)).collect(Collectors.toList())); + params.put( + FEATURE_PROCESSORS.getPreferredName(), + featureProcessors.stream().map(p -> Collections.singletonMap(p.getName(), p)).collect(Collectors.toList()) + ); } params.put(EARLY_STOPPING_ENABLED.getPreferredName(), earlyStoppingEnabled); return params; @@ -335,8 +354,7 @@ private static String getPredictionFieldTypeParamString(PredictionFieldType pred if (predictionFieldType == null) { return null; } - switch(predictionFieldType) - { + switch (predictionFieldType) { case NUMBER: // C++ process uses int64_t type, so it is safe for the dependent variable to use long numbers. return "int"; @@ -394,10 +412,14 @@ public List getFieldCardinalityConstraints() { public Map getResultMappings(String resultsFieldName, FieldCapabilitiesResponse fieldCapabilitiesResponse) { Map additionalProperties = new HashMap<>(); additionalProperties.put(resultsFieldName + ".is_training", Collections.singletonMap("type", BooleanFieldMapper.CONTENT_TYPE)); - additionalProperties.put(resultsFieldName + ".prediction_probability", - Collections.singletonMap("type", NumberFieldMapper.NumberType.DOUBLE.typeName())); - additionalProperties.put(resultsFieldName + ".prediction_score", - Collections.singletonMap("type", NumberFieldMapper.NumberType.DOUBLE.typeName())); + additionalProperties.put( + resultsFieldName + ".prediction_probability", + Collections.singletonMap("type", NumberFieldMapper.NumberType.DOUBLE.typeName()) + ); + additionalProperties.put( + resultsFieldName + ".prediction_score", + Collections.singletonMap("type", NumberFieldMapper.NumberType.DOUBLE.typeName()) + ); additionalProperties.put(resultsFieldName + ".feature_importance", FEATURE_IMPORTANCE_MAPPING); Map dependentVariableFieldCaps = fieldCapabilitiesResponse.getField(dependentVariable); @@ -406,7 +428,9 @@ public Map getResultMappings(String resultsFieldName, FieldCapab } Object dependentVariableMappingType = dependentVariableFieldCaps.values().iterator().next().getType(); additionalProperties.put( - resultsFieldName + "." + predictionFieldName, Collections.singletonMap("type", dependentVariableMappingType)); + resultsFieldName + "." + predictionFieldName, + Collections.singletonMap("type", dependentVariableMappingType) + ); Map topClassesProperties = new HashMap<>(); topClassesProperties.put("class_name", Collections.singletonMap("type", dependentVariableMappingType)); @@ -480,13 +504,22 @@ public boolean equals(Object o) { @Override public int hashCode() { - return Objects.hash(dependentVariable, boostedTreeParams, predictionFieldName, classAssignmentObjective, - numTopClasses, trainingPercent, randomizeSeed, featureProcessors, - earlyStoppingEnabled); + return Objects.hash( + dependentVariable, + boostedTreeParams, + predictionFieldName, + classAssignmentObjective, + numTopClasses, + trainingPercent, + randomizeSeed, + featureProcessors, + earlyStoppingEnabled + ); } public enum ClassAssignmentObjective { - MAXIMIZE_ACCURACY, MAXIMIZE_MINIMUM_RECALL; + MAXIMIZE_ACCURACY, + MAXIMIZE_MINIMUM_RECALL; public static ClassAssignmentObjective fromString(String value) { return ClassAssignmentObjective.valueOf(value.toUpperCase(Locale.ROOT)); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/DataFrameAnalysis.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/DataFrameAnalysis.java index 1a251a135ea64..9ec6c34f34e83 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/DataFrameAnalysis.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/DataFrameAnalysis.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ml.dataframe.analyses; import org.elasticsearch.action.fieldcaps.FieldCapabilitiesResponse; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.io.stream.NamedWriteable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.InferenceConfig; @@ -91,6 +91,7 @@ public interface DataFrameAnalysis extends ToXContentObject, NamedWriteable { default double getTrainingPercent() { return 100.0; } + /** * Summarizes information about the fields that is necessary for analysis to generate * the parameters needed for the process configuration. diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/FieldCardinalityConstraint.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/FieldCardinalityConstraint.java index c6b6de0d86ef2..fc15407357c86 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/FieldCardinalityConstraint.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/FieldCardinalityConstraint.java @@ -44,13 +44,19 @@ public long getUpperBound() { public void check(long fieldCardinality) { if (fieldCardinality < lowerBound) { throw ExceptionsHelper.badRequestException( - "Field [{}] must have at least [{}] distinct values but there were [{}]", - field, lowerBound, fieldCardinality); + "Field [{}] must have at least [{}] distinct values but there were [{}]", + field, + lowerBound, + fieldCardinality + ); } if (fieldCardinality > upperBound) { throw ExceptionsHelper.badRequestException( "Field [{}] must have at most [{}] distinct values but there were at least [{}]", - field, upperBound, fieldCardinality); + field, + upperBound, + fieldCardinality + ); } } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/MlDataFrameAnalysisNamedXContentProvider.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/MlDataFrameAnalysisNamedXContentProvider.java index f1f7af0789a39..473767bf5b4bf 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/MlDataFrameAnalysisNamedXContentProvider.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/MlDataFrameAnalysisNamedXContentProvider.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ml.dataframe.analyses; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.plugins.spi.NamedXContentProvider; +import org.elasticsearch.xcontent.NamedXContentRegistry; import java.util.Arrays; import java.util.List; @@ -17,20 +17,16 @@ public class MlDataFrameAnalysisNamedXContentProvider implements NamedXContentPr @Override public List getNamedXContentParsers() { - return Arrays.asList( - new NamedXContentRegistry.Entry(DataFrameAnalysis.class, OutlierDetection.NAME, (p, c) -> { - boolean ignoreUnknownFields = (boolean) c; - return OutlierDetection.fromXContent(p, ignoreUnknownFields); - }), - new NamedXContentRegistry.Entry(DataFrameAnalysis.class, Regression.NAME, (p, c) -> { - boolean ignoreUnknownFields = (boolean) c; - return Regression.fromXContent(p, ignoreUnknownFields); - }), - new NamedXContentRegistry.Entry(DataFrameAnalysis.class, Classification.NAME, (p, c) -> { - boolean ignoreUnknownFields = (boolean) c; - return Classification.fromXContent(p, ignoreUnknownFields); - }) - ); + return Arrays.asList(new NamedXContentRegistry.Entry(DataFrameAnalysis.class, OutlierDetection.NAME, (p, c) -> { + boolean ignoreUnknownFields = (boolean) c; + return OutlierDetection.fromXContent(p, ignoreUnknownFields); + }), new NamedXContentRegistry.Entry(DataFrameAnalysis.class, Regression.NAME, (p, c) -> { + boolean ignoreUnknownFields = (boolean) c; + return Regression.fromXContent(p, ignoreUnknownFields); + }), new NamedXContentRegistry.Entry(DataFrameAnalysis.class, Classification.NAME, (p, c) -> { + boolean ignoreUnknownFields = (boolean) c; + return Classification.fromXContent(p, ignoreUnknownFields); + })); } public List getNamedWriteables() { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/OutlierDetection.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/OutlierDetection.java index 8a5b9028c547e..174292dfa3f56 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/OutlierDetection.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/OutlierDetection.java @@ -8,16 +8,16 @@ import org.elasticsearch.Version; import org.elasticsearch.action.fieldcaps.FieldCapabilitiesResponse; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.xcontent.ObjectParser; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.core.Nullable; import org.elasticsearch.index.mapper.KeywordFieldMapper; import org.elasticsearch.index.mapper.NestedObjectMapper; import org.elasticsearch.index.mapper.NumberFieldMapper; +import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.InferenceConfig; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; @@ -108,8 +108,14 @@ public static OutlierDetection fromXContent(XContentParser parser, boolean ignor */ private final boolean standardizationEnabled; - private OutlierDetection(Integer nNeighbors, Method method, Double featureInfluenceThreshold, boolean computeFeatureInfluence, - double outlierFraction, boolean standardizationEnabled) { + private OutlierDetection( + Integer nNeighbors, + Method method, + Double featureInfluenceThreshold, + boolean computeFeatureInfluence, + double outlierFraction, + boolean standardizationEnabled + ) { if (nNeighbors != null && nNeighbors <= 0) { throw ExceptionsHelper.badRequestException("[{}] must be a positive integer", N_NEIGHBORS.getPreferredName()); } @@ -204,8 +210,14 @@ public boolean equals(Object o) { @Override public int hashCode() { - return Objects.hash(nNeighbors, method, featureInfluenceThreshold, computeFeatureInfluence, outlierFraction, - standardizationEnabled); + return Objects.hash( + nNeighbors, + method, + featureInfluenceThreshold, + computeFeatureInfluence, + outlierFraction, + standardizationEnabled + ); } @Override @@ -249,8 +261,10 @@ public List getFieldCardinalityConstraints() { @Override public Map getResultMappings(String resultsFieldName, FieldCapabilitiesResponse fieldCapabilitiesResponse) { Map additionalProperties = new HashMap<>(); - additionalProperties.put(resultsFieldName + ".outlier_score", - Collections.singletonMap("type", NumberFieldMapper.NumberType.DOUBLE.typeName())); + additionalProperties.put( + resultsFieldName + ".outlier_score", + Collections.singletonMap("type", NumberFieldMapper.NumberType.DOUBLE.typeName()) + ); additionalProperties.put(resultsFieldName + ".feature_influence", FEATURE_INFLUENCE_MAPPING); return additionalProperties; } @@ -286,7 +300,10 @@ public boolean supportsInference() { } public enum Method { - LOF, LDOF, DISTANCE_KTH_NN, DISTANCE_KNN; + LOF, + LDOF, + DISTANCE_KTH_NN, + DISTANCE_KNN; public static Method fromString(String value) { return Method.valueOf(value.toUpperCase(Locale.ROOT)); @@ -349,8 +366,14 @@ public Builder setStandardizationEnabled(boolean standardizationEnabled) { } public OutlierDetection build() { - return new OutlierDetection(nNeighbors, method, featureInfluenceThreshold, computeFeatureInfluence, outlierFraction, - standardizationEnabled); + return new OutlierDetection( + nNeighbors, + method, + featureInfluenceThreshold, + computeFeatureInfluence, + outlierFraction, + standardizationEnabled + ); } } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/Regression.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/Regression.java index 4670dccc43e4d..df5181faae786 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/Regression.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/Regression.java @@ -8,18 +8,18 @@ import org.elasticsearch.Version; import org.elasticsearch.action.fieldcaps.FieldCapabilitiesResponse; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Randomness; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.xcontent.ConstructingObjectParser; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.core.Nullable; import org.elasticsearch.index.mapper.BooleanFieldMapper; import org.elasticsearch.index.mapper.KeywordFieldMapper; import org.elasticsearch.index.mapper.NestedObjectMapper; import org.elasticsearch.index.mapper.NumberFieldMapper; +import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.inference.preprocessing.LenientlyParsedPreProcessor; import org.elasticsearch.xpack.core.ml.inference.preprocessing.PreProcessor; import org.elasticsearch.xpack.core.ml.inference.preprocessing.StrictlyParsedPreProcessor; @@ -67,15 +67,29 @@ private static ConstructingObjectParser createParser(boolean l lenient, a -> new Regression( (String) a[0], - new BoostedTreeParams((Double) a[1], (Double) a[2], (Double) a[3], (Integer) a[4], (Double) a[5], (Integer) a[6], - (Double) a[7], (Double) a[8], (Double) a[9], (Double) a[10], (Double) a[11], (Integer) a[12]), + new BoostedTreeParams( + (Double) a[1], + (Double) a[2], + (Double) a[3], + (Integer) a[4], + (Double) a[5], + (Integer) a[6], + (Double) a[7], + (Double) a[8], + (Double) a[9], + (Double) a[10], + (Double) a[11], + (Integer) a[12] + ), (String) a[13], (Double) a[14], (Long) a[15], (LossFunction) a[16], (Double) a[17], (List) a[18], - (Boolean) a[19])); + (Boolean) a[19] + ) + ); parser.declareString(constructorArg(), DEPENDENT_VARIABLE); BoostedTreeParams.declareFields(parser); parser.declareString(optionalConstructorArg(), PREDICTION_FIELD_NAME); @@ -83,12 +97,14 @@ private static ConstructingObjectParser createParser(boolean l parser.declareLong(optionalConstructorArg(), RANDOMIZE_SEED); parser.declareString(optionalConstructorArg(), LossFunction::fromString, LOSS_FUNCTION); parser.declareDouble(optionalConstructorArg(), LOSS_FUNCTION_PARAMETER); - parser.declareNamedObjects(optionalConstructorArg(), - (p, c, n) -> lenient ? - p.namedObject(LenientlyParsedPreProcessor.class, n, new PreProcessor.PreProcessorParseContext(true)) : - p.namedObject(StrictlyParsedPreProcessor.class, n, new PreProcessor.PreProcessorParseContext(true)), + parser.declareNamedObjects( + optionalConstructorArg(), + (p, c, n) -> lenient + ? p.namedObject(LenientlyParsedPreProcessor.class, n, new PreProcessor.PreProcessorParseContext(true)) + : p.namedObject(StrictlyParsedPreProcessor.class, n, new PreProcessor.PreProcessorParseContext(true)), (regression) -> {/*TODO should we throw if this is not set?*/}, - FEATURE_PROCESSORS); + FEATURE_PROCESSORS + ); parser.declareBoolean(optionalConstructorArg(), EARLY_STOPPING_ENABLED); return parser; } @@ -98,12 +114,7 @@ public static Regression fromXContent(XContentParser parser, boolean ignoreUnkno } private static final List PROGRESS_PHASES = Collections.unmodifiableList( - Arrays.asList( - "feature_selection", - "coarse_parameter_search", - "fine_tuning_parameters", - "final_training" - ) + Arrays.asList("feature_selection", "coarse_parameter_search", "fine_tuning_parameters", "final_training") ); static final Map FEATURE_IMPORTANCE_MAPPING; @@ -130,15 +141,17 @@ public static Regression fromXContent(XContentParser parser, boolean ignoreUnkno private final List featureProcessors; private final boolean earlyStoppingEnabled; - public Regression(String dependentVariable, - BoostedTreeParams boostedTreeParams, - @Nullable String predictionFieldName, - @Nullable Double trainingPercent, - @Nullable Long randomizeSeed, - @Nullable LossFunction lossFunction, - @Nullable Double lossFunctionParameter, - @Nullable List featureProcessors, - @Nullable Boolean earlyStoppingEnabled) { + public Regression( + String dependentVariable, + BoostedTreeParams boostedTreeParams, + @Nullable String predictionFieldName, + @Nullable Double trainingPercent, + @Nullable Long randomizeSeed, + @Nullable LossFunction lossFunction, + @Nullable Double lossFunctionParameter, + @Nullable List featureProcessors, + @Nullable Boolean earlyStoppingEnabled + ) { if (trainingPercent != null && (trainingPercent <= 0.0 || trainingPercent > 100.0)) { throw ExceptionsHelper.badRequestException("[{}] must be a positive double in (0, 100]", TRAINING_PERCENT.getPreferredName()); } @@ -275,8 +288,10 @@ public Map getParams(FieldInfo fieldInfo) { params.put(LOSS_FUNCTION_PARAMETER.getPreferredName(), lossFunctionParameter); } if (featureProcessors.isEmpty() == false) { - params.put(FEATURE_PROCESSORS.getPreferredName(), - featureProcessors.stream().map(p -> Collections.singletonMap(p.getName(), p)).collect(Collectors.toList())); + params.put( + FEATURE_PROCESSORS.getPreferredName(), + featureProcessors.stream().map(p -> Collections.singletonMap(p.getName(), p)).collect(Collectors.toList()) + ); } params.put(EARLY_STOPPING_ENABLED.getPreferredName(), earlyStoppingEnabled); return params; @@ -309,8 +324,10 @@ public Map getResultMappings(String resultsFieldName, FieldCapab additionalProperties.put(resultsFieldName + ".feature_importance", FEATURE_IMPORTANCE_MAPPING); // Prediction field should be always mapped as "double" rather than "float" in order to increase precision in case of // high (over 10M) values of dependent variable. - additionalProperties.put(resultsFieldName + "." + predictionFieldName, - Collections.singletonMap("type", NumberFieldMapper.NumberType.DOUBLE.typeName())); + additionalProperties.put( + resultsFieldName + "." + predictionFieldName, + Collections.singletonMap("type", NumberFieldMapper.NumberType.DOUBLE.typeName()) + ); return additionalProperties; } @@ -370,12 +387,23 @@ public boolean equals(Object o) { @Override public int hashCode() { - return Objects.hash(dependentVariable, boostedTreeParams, predictionFieldName, trainingPercent, randomizeSeed, lossFunction, - lossFunctionParameter, featureProcessors, earlyStoppingEnabled); + return Objects.hash( + dependentVariable, + boostedTreeParams, + predictionFieldName, + trainingPercent, + randomizeSeed, + lossFunction, + lossFunctionParameter, + featureProcessors, + earlyStoppingEnabled + ); } public enum LossFunction { - MSE, MSLE, HUBER; + MSE, + MSLE, + HUBER; private static LossFunction fromString(String value) { return LossFunction.valueOf(value.toUpperCase(Locale.ROOT)); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/Types.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/Types.java index 4902fe789cf7e..79aec5bcda220 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/Types.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/Types.java @@ -24,19 +24,21 @@ public final class Types { private Types() {} - private static final Set CATEGORICAL_TYPES = - Stream.of(TextFieldMapper.CONTENT_TYPE, KeywordFieldMapper.CONTENT_TYPE, IpFieldMapper.CONTENT_TYPE) - .collect(Collectors.toUnmodifiableSet()); - - private static final Set NUMERICAL_TYPES = - Stream.concat(Stream.of(NumberType.values()).map(NumberType::typeName), Stream.of("scaled_float", "unsigned_long")) - .collect(Collectors.toUnmodifiableSet()); - - private static final Set DISCRETE_NUMERICAL_TYPES = - Stream.concat( - Stream.of(NumberType.BYTE, NumberType.SHORT, NumberType.INTEGER, NumberType.LONG).map(NumberType::typeName), - Stream.of("unsigned_long")) - .collect(Collectors.toUnmodifiableSet()); + private static final Set CATEGORICAL_TYPES = Stream.of( + TextFieldMapper.CONTENT_TYPE, + KeywordFieldMapper.CONTENT_TYPE, + IpFieldMapper.CONTENT_TYPE + ).collect(Collectors.toUnmodifiableSet()); + + private static final Set NUMERICAL_TYPES = Stream.concat( + Stream.of(NumberType.values()).map(NumberType::typeName), + Stream.of("scaled_float", "unsigned_long") + ).collect(Collectors.toUnmodifiableSet()); + + private static final Set DISCRETE_NUMERICAL_TYPES = Stream.concat( + Stream.of(NumberType.BYTE, NumberType.SHORT, NumberType.INTEGER, NumberType.LONG).map(NumberType::typeName), + Stream.of("unsigned_long") + ).collect(Collectors.toUnmodifiableSet()); private static final Set BOOL_TYPES = Collections.singleton(BooleanFieldMapper.CONTENT_TYPE); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/Evaluation.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/Evaluation.java index 77f061c78b43b..de43f744c307b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/Evaluation.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/Evaluation.java @@ -8,16 +8,16 @@ import org.apache.lucene.search.join.ScoreMode; import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.common.io.stream.NamedWriteable; import org.elasticsearch.core.Nullable; import org.elasticsearch.core.Tuple; -import org.elasticsearch.common.io.stream.NamedWriteable; -import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.aggregations.AggregationBuilder; import org.elasticsearch.search.aggregations.PipelineAggregationBuilder; import org.elasticsearch.search.builder.SearchSourceBuilder; +import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; import java.util.ArrayList; @@ -71,15 +71,17 @@ private void checkRequiredFieldsAreSet(List metr String fieldDescriptor = requiredField.v1(); String field = requiredField.v2(); if (field == null) { - String metricNamesString = - metrics.stream() - .filter(m -> m.getRequiredFields().contains(fieldDescriptor)) - .map(EvaluationMetric::getName) - .collect(joining(", ")); + String metricNamesString = metrics.stream() + .filter(m -> m.getRequiredFields().contains(fieldDescriptor)) + .map(EvaluationMetric::getName) + .collect(joining(", ")); if (metricNamesString.isEmpty() == false) { throw ExceptionsHelper.badRequestException( "[{}] must define [{}] as required by the following metrics [{}]", - getName(), fieldDescriptor, metricNamesString); + getName(), + fieldDescriptor, + metricNamesString + ); } } } @@ -107,7 +109,8 @@ default SearchSourceBuilder buildSearch(EvaluationParameters parameters, QueryBu QueryBuilder predictedClassFieldExistsQuery = QueryBuilders.existsQuery(getFields().getPredictedClassField()); boolQuery.filter( QueryBuilders.nestedQuery(getFields().getTopClassesField(), predictedClassFieldExistsQuery, ScoreMode.None) - .ignoreUnmapped(true)); + .ignoreUnmapped(true) + ); } if (getFields().getPredictedProbabilityField() != null && requiredFields.contains(getFields().getPredictedProbabilityField())) { // Verify existence of the predicted probability field if required @@ -118,7 +121,8 @@ default SearchSourceBuilder buildSearch(EvaluationParameters parameters, QueryBu assert getFields().getTopClassesField() != null; boolQuery.filter( QueryBuilders.nestedQuery(getFields().getTopClassesField(), predictedProbabilityFieldExistsQuery, ScoreMode.None) - .ignoreUnmapped(true)); + .ignoreUnmapped(true) + ); } else { boolQuery.filter(predictedProbabilityFieldExistsQuery); } @@ -154,16 +158,15 @@ default void process(SearchResponse searchResponse) { * @return list of fields which are required by at least one of the metrics */ private List getRequiredFields() { - Set requiredFieldDescriptors = - getMetrics().stream() - .map(EvaluationMetric::getRequiredFields) - .flatMap(Set::stream) - .collect(toSet()); - List requiredFields = - getFields().listPotentiallyRequiredFields().stream() - .filter(f -> requiredFieldDescriptors.contains(f.v1())) - .map(Tuple::v2) - .collect(toList()); + Set requiredFieldDescriptors = getMetrics().stream() + .map(EvaluationMetric::getRequiredFields) + .flatMap(Set::stream) + .collect(toSet()); + List requiredFields = getFields().listPotentiallyRequiredFields() + .stream() + .filter(f -> requiredFieldDescriptors.contains(f.v1())) + .map(Tuple::v2) + .collect(toList()); return requiredFields; } @@ -179,10 +182,6 @@ default boolean hasAllResults() { * @return list of evaluation results */ default List getResults() { - return getMetrics().stream() - .map(EvaluationMetric::getResult) - .filter(Optional::isPresent) - .map(Optional::get) - .collect(toList()); + return getMetrics().stream().map(EvaluationMetric::getResult).filter(Optional::isPresent).map(Optional::get).collect(toList()); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/EvaluationFields.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/EvaluationFields.java index f5a2793512043..99b1dede6be9c 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/EvaluationFields.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/EvaluationFields.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ml.dataframe.evaluation; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.core.Tuple; +import org.elasticsearch.xcontent.ParseField; import java.util.Arrays; import java.util.List; @@ -55,12 +55,14 @@ public final class EvaluationFields { */ private final boolean predictedProbabilityFieldNested; - public EvaluationFields(@Nullable String actualField, - @Nullable String predictedField, - @Nullable String topClassesField, - @Nullable String predictedClassField, - @Nullable String predictedProbabilityField, - boolean predictedProbabilityFieldNested) { + public EvaluationFields( + @Nullable String actualField, + @Nullable String predictedField, + @Nullable String topClassesField, + @Nullable String predictedClassField, + @Nullable String predictedProbabilityField, + boolean predictedProbabilityFieldNested + ) { this.actualField = actualField; this.predictedField = predictedField; @@ -118,7 +120,8 @@ public List> listPotentiallyRequiredFields() { Tuple.tuple(PREDICTED_FIELD.getPreferredName(), predictedField), Tuple.tuple(TOP_CLASSES_FIELD.getPreferredName(), topClassesField), Tuple.tuple(PREDICTED_CLASS_FIELD.getPreferredName(), predictedClassField), - Tuple.tuple(PREDICTED_PROBABILITY_FIELD.getPreferredName(), predictedProbabilityField)); + Tuple.tuple(PREDICTED_PROBABILITY_FIELD.getPreferredName(), predictedProbabilityField) + ); } @Override @@ -137,6 +140,12 @@ public boolean equals(Object o) { @Override public int hashCode() { return Objects.hash( - actualField, predictedField, topClassesField, predictedClassField, predictedProbabilityField, predictedProbabilityFieldNested); + actualField, + predictedField, + topClassesField, + predictedClassField, + predictedProbabilityField, + predictedProbabilityFieldNested + ); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/EvaluationMetric.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/EvaluationMetric.java index 106beffa7764c..bc24ca129635e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/EvaluationMetric.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/EvaluationMetric.java @@ -7,12 +7,12 @@ package org.elasticsearch.xpack.core.ml.dataframe.evaluation; import org.elasticsearch.action.search.SearchResponse; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.io.stream.NamedWriteable; -import org.elasticsearch.xcontent.ToXContentObject; +import org.elasticsearch.core.Tuple; import org.elasticsearch.search.aggregations.AggregationBuilder; import org.elasticsearch.search.aggregations.Aggregations; import org.elasticsearch.search.aggregations.PipelineAggregationBuilder; +import org.elasticsearch.xcontent.ToXContentObject; import java.util.List; import java.util.Optional; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/MlEvaluationNamedXContentProvider.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/MlEvaluationNamedXContentProvider.java index 2c0f1cf919090..05e8582c7b4dc 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/MlEvaluationNamedXContentProvider.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/MlEvaluationNamedXContentProvider.java @@ -6,10 +6,10 @@ */ package org.elasticsearch.xpack.core.ml.dataframe.evaluation; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.plugins.spi.NamedXContentProvider; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.classification.Accuracy; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.classification.AucRoc; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.classification.Classification; @@ -52,7 +52,7 @@ public static String registeredMetricName(ParseField evaluationName, ParseField * @return name appropriate for registering a metric (or metric result) in {@link NamedXContentRegistry} */ public static String registeredMetricName(String evaluationName, String metricName) { - return evaluationName + "." + metricName; + return evaluationName + "." + metricName; } @Override @@ -64,149 +64,212 @@ public List getNamedXContentParsers() { new NamedXContentRegistry.Entry(Evaluation.class, Regression.NAME, Regression::fromXContent), // Outlier detection metrics - new NamedXContentRegistry.Entry(EvaluationMetric.class, + new NamedXContentRegistry.Entry( + EvaluationMetric.class, new ParseField( registeredMetricName( - OutlierDetection.NAME, org.elasticsearch.xpack.core.ml.dataframe.evaluation.outlierdetection.AucRoc.NAME)), - org.elasticsearch.xpack.core.ml.dataframe.evaluation.outlierdetection.AucRoc::fromXContent), - new NamedXContentRegistry.Entry(EvaluationMetric.class, + OutlierDetection.NAME, + org.elasticsearch.xpack.core.ml.dataframe.evaluation.outlierdetection.AucRoc.NAME + ) + ), + org.elasticsearch.xpack.core.ml.dataframe.evaluation.outlierdetection.AucRoc::fromXContent + ), + new NamedXContentRegistry.Entry( + EvaluationMetric.class, new ParseField( registeredMetricName( - OutlierDetection.NAME, org.elasticsearch.xpack.core.ml.dataframe.evaluation.outlierdetection.Precision.NAME)), - org.elasticsearch.xpack.core.ml.dataframe.evaluation.outlierdetection.Precision::fromXContent), - new NamedXContentRegistry.Entry(EvaluationMetric.class, + OutlierDetection.NAME, + org.elasticsearch.xpack.core.ml.dataframe.evaluation.outlierdetection.Precision.NAME + ) + ), + org.elasticsearch.xpack.core.ml.dataframe.evaluation.outlierdetection.Precision::fromXContent + ), + new NamedXContentRegistry.Entry( + EvaluationMetric.class, new ParseField( registeredMetricName( - OutlierDetection.NAME, org.elasticsearch.xpack.core.ml.dataframe.evaluation.outlierdetection.Recall.NAME)), - org.elasticsearch.xpack.core.ml.dataframe.evaluation.outlierdetection.Recall::fromXContent), - new NamedXContentRegistry.Entry(EvaluationMetric.class, + OutlierDetection.NAME, + org.elasticsearch.xpack.core.ml.dataframe.evaluation.outlierdetection.Recall.NAME + ) + ), + org.elasticsearch.xpack.core.ml.dataframe.evaluation.outlierdetection.Recall::fromXContent + ), + new NamedXContentRegistry.Entry( + EvaluationMetric.class, new ParseField(registeredMetricName(OutlierDetection.NAME, ConfusionMatrix.NAME)), - ConfusionMatrix::fromXContent), + ConfusionMatrix::fromXContent + ), // Classification metrics - new NamedXContentRegistry.Entry(EvaluationMetric.class, + new NamedXContentRegistry.Entry( + EvaluationMetric.class, new ParseField(registeredMetricName(Classification.NAME, AucRoc.NAME)), - AucRoc::fromXContent), - new NamedXContentRegistry.Entry(EvaluationMetric.class, + AucRoc::fromXContent + ), + new NamedXContentRegistry.Entry( + EvaluationMetric.class, new ParseField(registeredMetricName(Classification.NAME, MulticlassConfusionMatrix.NAME)), - MulticlassConfusionMatrix::fromXContent), - new NamedXContentRegistry.Entry(EvaluationMetric.class, + MulticlassConfusionMatrix::fromXContent + ), + new NamedXContentRegistry.Entry( + EvaluationMetric.class, new ParseField(registeredMetricName(Classification.NAME, Accuracy.NAME)), - Accuracy::fromXContent), - new NamedXContentRegistry.Entry(EvaluationMetric.class, + Accuracy::fromXContent + ), + new NamedXContentRegistry.Entry( + EvaluationMetric.class, new ParseField(registeredMetricName(Classification.NAME, Precision.NAME)), - Precision::fromXContent), - new NamedXContentRegistry.Entry(EvaluationMetric.class, + Precision::fromXContent + ), + new NamedXContentRegistry.Entry( + EvaluationMetric.class, new ParseField(registeredMetricName(Classification.NAME, Recall.NAME)), - Recall::fromXContent), + Recall::fromXContent + ), // Regression metrics - new NamedXContentRegistry.Entry(EvaluationMetric.class, + new NamedXContentRegistry.Entry( + EvaluationMetric.class, new ParseField(registeredMetricName(Regression.NAME, MeanSquaredError.NAME)), - MeanSquaredError::fromXContent), - new NamedXContentRegistry.Entry(EvaluationMetric.class, + MeanSquaredError::fromXContent + ), + new NamedXContentRegistry.Entry( + EvaluationMetric.class, new ParseField(registeredMetricName(Regression.NAME, MeanSquaredLogarithmicError.NAME)), - MeanSquaredLogarithmicError::fromXContent), - new NamedXContentRegistry.Entry(EvaluationMetric.class, + MeanSquaredLogarithmicError::fromXContent + ), + new NamedXContentRegistry.Entry( + EvaluationMetric.class, new ParseField(registeredMetricName(Regression.NAME, Huber.NAME)), - Huber::fromXContent), - new NamedXContentRegistry.Entry(EvaluationMetric.class, + Huber::fromXContent + ), + new NamedXContentRegistry.Entry( + EvaluationMetric.class, new ParseField(registeredMetricName(Regression.NAME, RSquared.NAME)), - RSquared::fromXContent) + RSquared::fromXContent + ) ); } public static List getNamedWriteables() { return Arrays.asList( // Evaluations - new NamedWriteableRegistry.Entry(Evaluation.class, - OutlierDetection.NAME.getPreferredName(), - OutlierDetection::new), - new NamedWriteableRegistry.Entry(Evaluation.class, - Classification.NAME.getPreferredName(), - Classification::new), - new NamedWriteableRegistry.Entry(Evaluation.class, - Regression.NAME.getPreferredName(), - Regression::new), + new NamedWriteableRegistry.Entry(Evaluation.class, OutlierDetection.NAME.getPreferredName(), OutlierDetection::new), + new NamedWriteableRegistry.Entry(Evaluation.class, Classification.NAME.getPreferredName(), Classification::new), + new NamedWriteableRegistry.Entry(Evaluation.class, Regression.NAME.getPreferredName(), Regression::new), // Evaluation metrics - new NamedWriteableRegistry.Entry(EvaluationMetric.class, + new NamedWriteableRegistry.Entry( + EvaluationMetric.class, registeredMetricName( - OutlierDetection.NAME, org.elasticsearch.xpack.core.ml.dataframe.evaluation.outlierdetection.AucRoc.NAME), - org.elasticsearch.xpack.core.ml.dataframe.evaluation.outlierdetection.AucRoc::new), - new NamedWriteableRegistry.Entry(EvaluationMetric.class, + OutlierDetection.NAME, + org.elasticsearch.xpack.core.ml.dataframe.evaluation.outlierdetection.AucRoc.NAME + ), + org.elasticsearch.xpack.core.ml.dataframe.evaluation.outlierdetection.AucRoc::new + ), + new NamedWriteableRegistry.Entry( + EvaluationMetric.class, registeredMetricName( - OutlierDetection.NAME, org.elasticsearch.xpack.core.ml.dataframe.evaluation.outlierdetection.Precision.NAME), - org.elasticsearch.xpack.core.ml.dataframe.evaluation.outlierdetection.Precision::new), - new NamedWriteableRegistry.Entry(EvaluationMetric.class, + OutlierDetection.NAME, + org.elasticsearch.xpack.core.ml.dataframe.evaluation.outlierdetection.Precision.NAME + ), + org.elasticsearch.xpack.core.ml.dataframe.evaluation.outlierdetection.Precision::new + ), + new NamedWriteableRegistry.Entry( + EvaluationMetric.class, registeredMetricName( - OutlierDetection.NAME, org.elasticsearch.xpack.core.ml.dataframe.evaluation.outlierdetection.Recall.NAME), - org.elasticsearch.xpack.core.ml.dataframe.evaluation.outlierdetection.Recall::new), - new NamedWriteableRegistry.Entry(EvaluationMetric.class, + OutlierDetection.NAME, + org.elasticsearch.xpack.core.ml.dataframe.evaluation.outlierdetection.Recall.NAME + ), + org.elasticsearch.xpack.core.ml.dataframe.evaluation.outlierdetection.Recall::new + ), + new NamedWriteableRegistry.Entry( + EvaluationMetric.class, registeredMetricName(OutlierDetection.NAME, ConfusionMatrix.NAME), - ConfusionMatrix::new), - new NamedWriteableRegistry.Entry(EvaluationMetric.class, - registeredMetricName(Classification.NAME, AucRoc.NAME), - AucRoc::new), - new NamedWriteableRegistry.Entry(EvaluationMetric.class, + ConfusionMatrix::new + ), + new NamedWriteableRegistry.Entry(EvaluationMetric.class, registeredMetricName(Classification.NAME, AucRoc.NAME), AucRoc::new), + new NamedWriteableRegistry.Entry( + EvaluationMetric.class, registeredMetricName(Classification.NAME, MulticlassConfusionMatrix.NAME), - MulticlassConfusionMatrix::new), - new NamedWriteableRegistry.Entry(EvaluationMetric.class, + MulticlassConfusionMatrix::new + ), + new NamedWriteableRegistry.Entry( + EvaluationMetric.class, registeredMetricName(Classification.NAME, Accuracy.NAME), - Accuracy::new), - new NamedWriteableRegistry.Entry(EvaluationMetric.class, + Accuracy::new + ), + new NamedWriteableRegistry.Entry( + EvaluationMetric.class, registeredMetricName(Classification.NAME, Precision.NAME), - Precision::new), - new NamedWriteableRegistry.Entry(EvaluationMetric.class, - registeredMetricName(Classification.NAME, Recall.NAME), - Recall::new), - new NamedWriteableRegistry.Entry(EvaluationMetric.class, + Precision::new + ), + new NamedWriteableRegistry.Entry(EvaluationMetric.class, registeredMetricName(Classification.NAME, Recall.NAME), Recall::new), + new NamedWriteableRegistry.Entry( + EvaluationMetric.class, registeredMetricName(Regression.NAME, MeanSquaredError.NAME), - MeanSquaredError::new), - new NamedWriteableRegistry.Entry(EvaluationMetric.class, + MeanSquaredError::new + ), + new NamedWriteableRegistry.Entry( + EvaluationMetric.class, registeredMetricName(Regression.NAME, MeanSquaredLogarithmicError.NAME), - MeanSquaredLogarithmicError::new), - new NamedWriteableRegistry.Entry(EvaluationMetric.class, - registeredMetricName(Regression.NAME, Huber.NAME), - Huber::new), - new NamedWriteableRegistry.Entry(EvaluationMetric.class, - registeredMetricName(Regression.NAME, RSquared.NAME), - RSquared::new), + MeanSquaredLogarithmicError::new + ), + new NamedWriteableRegistry.Entry(EvaluationMetric.class, registeredMetricName(Regression.NAME, Huber.NAME), Huber::new), + new NamedWriteableRegistry.Entry(EvaluationMetric.class, registeredMetricName(Regression.NAME, RSquared.NAME), RSquared::new), // Evaluation metrics results - new NamedWriteableRegistry.Entry(EvaluationMetricResult.class, + new NamedWriteableRegistry.Entry( + EvaluationMetricResult.class, registeredMetricName(OutlierDetection.NAME, ScoreByThresholdResult.NAME), - ScoreByThresholdResult::new), - new NamedWriteableRegistry.Entry(EvaluationMetricResult.class, + ScoreByThresholdResult::new + ), + new NamedWriteableRegistry.Entry( + EvaluationMetricResult.class, registeredMetricName(OutlierDetection.NAME, ConfusionMatrix.NAME), - ConfusionMatrix.Result::new), - new NamedWriteableRegistry.Entry(EvaluationMetricResult.class, - AbstractAucRoc.Result.NAME, - AbstractAucRoc.Result::new), - new NamedWriteableRegistry.Entry(EvaluationMetricResult.class, + ConfusionMatrix.Result::new + ), + new NamedWriteableRegistry.Entry(EvaluationMetricResult.class, AbstractAucRoc.Result.NAME, AbstractAucRoc.Result::new), + new NamedWriteableRegistry.Entry( + EvaluationMetricResult.class, registeredMetricName(Classification.NAME, MulticlassConfusionMatrix.NAME), - MulticlassConfusionMatrix.Result::new), - new NamedWriteableRegistry.Entry(EvaluationMetricResult.class, + MulticlassConfusionMatrix.Result::new + ), + new NamedWriteableRegistry.Entry( + EvaluationMetricResult.class, registeredMetricName(Classification.NAME, Accuracy.NAME), - Accuracy.Result::new), - new NamedWriteableRegistry.Entry(EvaluationMetricResult.class, + Accuracy.Result::new + ), + new NamedWriteableRegistry.Entry( + EvaluationMetricResult.class, registeredMetricName(Classification.NAME, Precision.NAME), - Precision.Result::new), - new NamedWriteableRegistry.Entry(EvaluationMetricResult.class, + Precision.Result::new + ), + new NamedWriteableRegistry.Entry( + EvaluationMetricResult.class, registeredMetricName(Classification.NAME, Recall.NAME), - Recall.Result::new), - new NamedWriteableRegistry.Entry(EvaluationMetricResult.class, + Recall.Result::new + ), + new NamedWriteableRegistry.Entry( + EvaluationMetricResult.class, registeredMetricName(Regression.NAME, MeanSquaredError.NAME), - MeanSquaredError.Result::new), - new NamedWriteableRegistry.Entry(EvaluationMetricResult.class, + MeanSquaredError.Result::new + ), + new NamedWriteableRegistry.Entry( + EvaluationMetricResult.class, registeredMetricName(Regression.NAME, MeanSquaredLogarithmicError.NAME), - MeanSquaredLogarithmicError.Result::new), - new NamedWriteableRegistry.Entry(EvaluationMetricResult.class, + MeanSquaredLogarithmicError.Result::new + ), + new NamedWriteableRegistry.Entry( + EvaluationMetricResult.class, registeredMetricName(Regression.NAME, Huber.NAME), - Huber.Result::new), - new NamedWriteableRegistry.Entry(EvaluationMetricResult.class, + Huber.Result::new + ), + new NamedWriteableRegistry.Entry( + EvaluationMetricResult.class, registeredMetricName(Regression.NAME, RSquared.NAME), - RSquared.Result::new) + RSquared.Result::new + ) ); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/Accuracy.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/Accuracy.java index 6579bba468277..cd084dfc51609 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/Accuracy.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/Accuracy.java @@ -7,21 +7,21 @@ package org.elasticsearch.xpack.core.ml.dataframe.evaluation.classification; import org.apache.lucene.util.SetOnce; -import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.util.set.Sets; -import org.elasticsearch.xcontent.ConstructingObjectParser; -import org.elasticsearch.xcontent.ObjectParser; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.core.Tuple; import org.elasticsearch.script.Script; import org.elasticsearch.search.aggregations.AggregationBuilder; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.Aggregations; import org.elasticsearch.search.aggregations.PipelineAggregationBuilder; import org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregation; +import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationFields; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationMetric; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationMetricResult; @@ -103,8 +103,10 @@ public Set getRequiredFields() { } @Override - public final Tuple, List> aggs(EvaluationParameters parameters, - EvaluationFields fields) { + public final Tuple, List> aggs( + EvaluationParameters parameters, + EvaluationFields fields + ) { // Store given {@code actualField} for the purpose of generating error message in {@code process}. this.actualField.trySet(fields.getActualField()); List aggs = new ArrayList<>(); @@ -133,7 +135,9 @@ public void process(Aggregations aggs) { // This means there were more than {@code maxClassesCardinality} buckets. // We cannot calculate per-class accuracy accurately, so we fail. throw ExceptionsHelper.badRequestException( - "Cannot calculate per-class accuracy. Cardinality of field [{}] is too high", actualField.get()); + "Cannot calculate per-class accuracy. Cardinality of field [{}] is too high", + actualField.get() + ); } result.set(new Result(computePerClassAccuracy(matrix.getResult().get()), overallAccuracy.get())); } @@ -154,8 +158,10 @@ static List computePerClassAccuracy(MulticlassConfusionMatr // Number of actual classes taken into account int n = matrixResult.getConfusionMatrix().size(); // Total number of documents taken into account - long totalDocCount = - matrixResult.getConfusionMatrix().stream().mapToLong(MulticlassConfusionMatrix.ActualClass::getActualClassDocCount).sum(); + long totalDocCount = matrixResult.getConfusionMatrix() + .stream() + .mapToLong(MulticlassConfusionMatrix.ActualClass::getActualClassDocCount) + .sum(); List classes = new ArrayList<>(n); for (int i = 0; i < n; ++i) { String className = matrixResult.getConfusionMatrix().get(i).getActualClass(); @@ -171,7 +177,7 @@ static List computePerClassAccuracy(MulticlassConfusionMatr } // Subtract errors (false negatives) for classes other than explicitly listed in confusion matrix correctDocCount -= matrixResult.getConfusionMatrix().get(i).getOtherPredictedClassDocCount(); - classes.add(new PerClassSingleValue(className, ((double)correctDocCount) / totalDocCount)); + classes.add(new PerClassSingleValue(className, ((double) correctDocCount) / totalDocCount)); } return classes; } @@ -207,8 +213,11 @@ public static class Result implements EvaluationMetricResult { private static final ParseField OVERALL_ACCURACY = new ParseField("overall_accuracy"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("accuracy_result", true, a -> new Result((List) a[0], (double) a[1])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "accuracy_result", + true, + a -> new Result((List) a[0], (double) a[1]) + ); static { PARSER.declareObjectArray(constructorArg(), PerClassSingleValue.PARSER, CLASSES); @@ -272,8 +281,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Result that = (Result) o; - return Objects.equals(this.classes, that.classes) - && this.overallAccuracy == that.overallAccuracy; + return Objects.equals(this.classes, that.classes) && this.overallAccuracy == that.overallAccuracy; } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/AucRoc.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/AucRoc.java index fa84677e786ec..affe6e06f4f9c 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/AucRoc.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/AucRoc.java @@ -7,14 +7,10 @@ package org.elasticsearch.xpack.core.ml.dataframe.evaluation.classification; import org.apache.lucene.util.SetOnce; -import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.util.set.Sets; -import org.elasticsearch.xcontent.ConstructingObjectParser; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.core.Tuple; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.aggregations.AggregationBuilder; @@ -24,6 +20,10 @@ import org.elasticsearch.search.aggregations.bucket.filter.Filter; import org.elasticsearch.search.aggregations.bucket.nested.Nested; import org.elasticsearch.search.aggregations.metrics.Percentiles; +import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationFields; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationMetricResult; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationParameters; @@ -63,8 +63,10 @@ public class AucRoc extends AbstractAucRoc { public static final ParseField INCLUDE_CURVE = new ParseField("include_curve"); public static final ParseField CLASS_NAME = new ParseField("class_name"); - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(NAME.getPreferredName(), a -> new AucRoc((Boolean) a[0], (String) a[1])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME.getPreferredName(), + a -> new AucRoc((Boolean) a[0], (String) a[1]) + ); static { PARSER.declareBoolean(ConstructingObjectParser.optionalConstructorArg(), INCLUDE_CURVE); @@ -123,7 +125,8 @@ public Set getRequiredFields() { return Sets.newHashSet( EvaluationFields.ACTUAL_FIELD.getPreferredName(), EvaluationFields.PREDICTED_CLASS_FIELD.getPreferredName(), - EvaluationFields.PREDICTED_PROBABILITY_FIELD.getPreferredName()); + EvaluationFields.PREDICTED_PROBABILITY_FIELD.getPreferredName() + ); } @Override @@ -131,8 +134,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; AucRoc that = (AucRoc) o; - return includeCurve == that.includeCurve - && Objects.equals(className, that.className); + return includeCurve == that.includeCurve && Objects.equals(className, that.className); } @Override @@ -141,8 +143,10 @@ public int hashCode() { } @Override - public Tuple, List> aggs(EvaluationParameters parameters, - EvaluationFields fields) { + public Tuple, List> aggs( + EvaluationParameters parameters, + EvaluationFields fields + ) { if (result.get() != null) { return Tuple.tuple(List.of(), List.of()); } @@ -150,30 +154,22 @@ public Tuple, List> aggs(Ev this.fields.trySet(fields); double[] percentiles = IntStream.range(1, 100).mapToDouble(v -> (double) v).toArray(); - AggregationBuilder percentilesAgg = - AggregationBuilders - .percentiles(PERCENTILES_AGG_NAME) - .field(fields.getPredictedProbabilityField()) - .percentiles(percentiles); - AggregationBuilder nestedAgg = - AggregationBuilders - .nested(NESTED_AGG_NAME, fields.getTopClassesField()) - .subAggregation( - AggregationBuilders - .filter(NESTED_FILTER_AGG_NAME, QueryBuilders.termQuery(fields.getPredictedClassField(), className)) - .subAggregation(percentilesAgg)); + AggregationBuilder percentilesAgg = AggregationBuilders.percentiles(PERCENTILES_AGG_NAME) + .field(fields.getPredictedProbabilityField()) + .percentiles(percentiles); + AggregationBuilder nestedAgg = AggregationBuilders.nested(NESTED_AGG_NAME, fields.getTopClassesField()) + .subAggregation( + AggregationBuilders.filter(NESTED_FILTER_AGG_NAME, QueryBuilders.termQuery(fields.getPredictedClassField(), className)) + .subAggregation(percentilesAgg) + ); QueryBuilder actualIsTrueQuery = QueryBuilders.termQuery(fields.getActualField(), className); - AggregationBuilder percentilesForClassValueAgg = - AggregationBuilders - .filter(TRUE_AGG_NAME, actualIsTrueQuery) - .subAggregation(nestedAgg); - AggregationBuilder percentilesForRestAgg = - AggregationBuilders - .filter(NON_TRUE_AGG_NAME, QueryBuilders.boolQuery().mustNot(actualIsTrueQuery)) - .subAggregation(nestedAgg); - return Tuple.tuple( - List.of(percentilesForClassValueAgg, percentilesForRestAgg), - List.of()); + AggregationBuilder percentilesForClassValueAgg = AggregationBuilders.filter(TRUE_AGG_NAME, actualIsTrueQuery) + .subAggregation(nestedAgg); + AggregationBuilder percentilesForRestAgg = AggregationBuilders.filter( + NON_TRUE_AGG_NAME, + QueryBuilders.boolQuery().mustNot(actualIsTrueQuery) + ).subAggregation(nestedAgg); + return Tuple.tuple(List.of(percentilesForClassValueAgg, percentilesForRestAgg), List.of()); } @Override @@ -192,21 +188,32 @@ public void process(Aggregations aggs) { if (classAgg.getDocCount() == 0) { throw ExceptionsHelper.badRequestException( "[{}] requires at least one [{}] to have the value [{}]", - getName(), fields.get().getActualField(), className); + getName(), + fields.get().getActualField(), + className + ); } if (restAgg.getDocCount() == 0) { throw ExceptionsHelper.badRequestException( "[{}] requires at least one [{}] to have a different value than [{}]", - getName(), fields.get().getActualField(), className); + getName(), + fields.get().getActualField(), + className + ); } long filteredDocCount = classNestedFilter.getDocCount() + restNestedFilter.getDocCount(); long totalDocCount = classAgg.getDocCount() + restAgg.getDocCount(); if (filteredDocCount < totalDocCount) { throw ExceptionsHelper.badRequestException( "[{}] requires that [{}] appears as one of the [{}] for every document (appeared in {} out of {}). " - + "This is probably caused by the {} value being less than the total number of actual classes in the dataset.", - getName(), className, fields.get().getPredictedClassField(), filteredDocCount, totalDocCount, - org.elasticsearch.xpack.core.ml.dataframe.analyses.Classification.NUM_TOP_CLASSES.getPreferredName()); + + "This is probably caused by the {} value being less than the total number of actual classes in the dataset.", + getName(), + className, + fields.get().getPredictedClassField(), + filteredDocCount, + totalDocCount, + org.elasticsearch.xpack.core.ml.dataframe.analyses.Classification.NUM_TOP_CLASSES.getPreferredName() + ); } Percentiles classPercentiles = classNestedFilter.getAggregations().get(PERCENTILES_AGG_NAME); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/Classification.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/Classification.java index dd557e3989bee..7934c97b7f113 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/Classification.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/Classification.java @@ -6,11 +6,11 @@ */ package org.elasticsearch.xpack.core.ml.dataframe.evaluation.classification; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.Evaluation; @@ -42,17 +42,20 @@ public class Classification implements Evaluation { private static final String DEFAULT_PREDICTED_PROBABILITY_FIELD_SUFFIX = ".class_probability"; @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>( - NAME.getPreferredName(), - a -> new Classification((String) a[0], (String) a[1], (String) a[2], (List) a[3])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME.getPreferredName(), + a -> new Classification((String) a[0], (String) a[1], (String) a[2], (List) a[3]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), ACTUAL_FIELD); PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), PREDICTED_FIELD); PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), TOP_CLASSES_FIELD); - PARSER.declareNamedObjects(ConstructingObjectParser.optionalConstructorArg(), - (p, c, n) -> p.namedObject(EvaluationMetric.class, registeredMetricName(NAME.getPreferredName(), n), c), METRICS); + PARSER.declareNamedObjects( + ConstructingObjectParser.optionalConstructorArg(), + (p, c, n) -> p.namedObject(EvaluationMetric.class, registeredMetricName(NAME.getPreferredName(), n), c), + METRICS + ); } public static Classification fromXContent(XContentParser parser) { @@ -72,23 +75,25 @@ public static Classification fromXContent(XContentParser parser) { */ private final List metrics; - public Classification(String actualField, - @Nullable String predictedField, - @Nullable String topClassesField, - @Nullable List metrics) { + public Classification( + String actualField, + @Nullable String predictedField, + @Nullable String topClassesField, + @Nullable List metrics + ) { if (topClassesField == null) { topClassesField = DEFAULT_TOP_CLASSES_FIELD; } String predictedClassField = topClassesField + DEFAULT_PREDICTED_CLASS_FIELD_SUFFIX; String predictedProbabilityField = topClassesField + DEFAULT_PREDICTED_PROBABILITY_FIELD_SUFFIX; - this.fields = - new EvaluationFields( - ExceptionsHelper.requireNonNull(actualField, ACTUAL_FIELD), - predictedField, - topClassesField, - predictedClassField, - predictedProbabilityField, - true); + this.fields = new EvaluationFields( + ExceptionsHelper.requireNonNull(actualField, ACTUAL_FIELD), + predictedField, + topClassesField, + predictedClassField, + predictedProbabilityField, + true + ); this.metrics = initMetrics(metrics, Classification::defaultMetrics); } @@ -97,14 +102,14 @@ private static List defaultMetrics() { } public Classification(StreamInput in) throws IOException { - this.fields = - new EvaluationFields( - in.readString(), - in.readOptionalString(), - in.readOptionalString(), - in.readOptionalString(), - in.readOptionalString(), - true); + this.fields = new EvaluationFields( + in.readString(), + in.readOptionalString(), + in.readOptionalString(), + in.readOptionalString(), + in.readOptionalString(), + true + ); this.metrics = in.readNamedWriteableList(EvaluationMetric.class); } @@ -163,8 +168,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Classification that = (Classification) o; - return Objects.equals(that.fields, this.fields) - && Objects.equals(that.metrics, this.metrics); + return Objects.equals(that.fields, this.fields) && Objects.equals(that.metrics, this.metrics); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/MulticlassConfusionMatrix.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/MulticlassConfusionMatrix.java index 6954202772f04..c904ed5d8ae76 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/MulticlassConfusionMatrix.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/MulticlassConfusionMatrix.java @@ -7,17 +7,12 @@ package org.elasticsearch.xpack.core.ml.dataframe.evaluation.classification; import org.apache.lucene.util.SetOnce; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.util.set.Sets; -import org.elasticsearch.xcontent.ConstructingObjectParser; -import org.elasticsearch.xcontent.ToXContentObject; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.core.Tuple; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.aggregations.AggregationBuilder; import org.elasticsearch.search.aggregations.AggregationBuilders; @@ -28,6 +23,11 @@ import org.elasticsearch.search.aggregations.bucket.filter.FiltersAggregator.KeyedFilter; import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.aggregations.metrics.Cardinality; +import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.ToXContentObject; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationFields; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationMetric; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationMetricResult; @@ -63,9 +63,11 @@ public class MulticlassConfusionMatrix implements EvaluationMetric { private static final ConstructingObjectParser PARSER = createParser(); private static ConstructingObjectParser createParser() { - ConstructingObjectParser parser = - new ConstructingObjectParser<>( - NAME.getPreferredName(), true, args -> new MulticlassConfusionMatrix((Integer) args[0], (String) args[1])); + ConstructingObjectParser parser = new ConstructingObjectParser<>( + NAME.getPreferredName(), + true, + args -> new MulticlassConfusionMatrix((Integer) args[0], (String) args[1]) + ); parser.declareInt(optionalConstructorArg(), SIZE); parser.declareString(optionalConstructorArg(), AGG_NAME_PREFIX); return parser; @@ -129,8 +131,10 @@ public Set getRequiredFields() { } @Override - public final Tuple, List> aggs(EvaluationParameters parameters, - EvaluationFields fields) { + public final Tuple, List> aggs( + EvaluationParameters parameters, + EvaluationFields fields + ) { String actualField = fields.getActualField(); String predictedField = fields.getPredictedField(); if (topActualClassNames.get() == null && actualClassesCardinality.get() == null) { // This is step 1 @@ -140,34 +144,39 @@ public final Tuple, List> a .field(actualField) .order(List.of(BucketOrder.count(false), BucketOrder.key(true))) .size(size), - AggregationBuilders.cardinality(aggName(STEP_1_CARDINALITY_OF_ACTUAL_CLASS)) - .field(actualField)), - List.of()); + AggregationBuilders.cardinality(aggName(STEP_1_CARDINALITY_OF_ACTUAL_CLASS)).field(actualField) + ), + List.of() + ); } if (result.get() == null) { // These are steps 2, 3, 4 etc. - KeyedFilter[] keyedFiltersPredicted = - topActualClassNames.get().stream() - .map(className -> new KeyedFilter(className, QueryBuilders.matchQuery(predictedField, className).lenient(true))) - .toArray(KeyedFilter[]::new); + KeyedFilter[] keyedFiltersPredicted = topActualClassNames.get() + .stream() + .map(className -> new KeyedFilter(className, QueryBuilders.matchQuery(predictedField, className).lenient(true))) + .toArray(KeyedFilter[]::new); // Knowing exactly how many buckets does each aggregation use, we can choose the size of the batch so that // too_many_buckets_exception exception is not thrown. // The only exception is when "search.max_buckets" is set far too low to even have 1 actual class in the batch. // In such case, the exception will be thrown telling the user they should increase the value of "search.max_buckets". int actualClassesPerBatch = Math.max(parameters.getMaxBuckets() / (topActualClassNames.get().size() + 2), 1); - KeyedFilter[] keyedFiltersActual = - topActualClassNames.get().stream() - .skip(actualClasses.size()) - .limit(actualClassesPerBatch) - .map(className -> new KeyedFilter(className, QueryBuilders.matchQuery(actualField, className).lenient(true))) - .toArray(KeyedFilter[]::new); + KeyedFilter[] keyedFiltersActual = topActualClassNames.get() + .stream() + .skip(actualClasses.size()) + .limit(actualClassesPerBatch) + .map(className -> new KeyedFilter(className, QueryBuilders.matchQuery(actualField, className).lenient(true))) + .toArray(KeyedFilter[]::new); if (keyedFiltersActual.length > 0) { return Tuple.tuple( List.of( AggregationBuilders.filters(aggName(STEP_2_AGGREGATE_BY_ACTUAL_CLASS), keyedFiltersActual) - .subAggregation(AggregationBuilders.filters(aggName(STEP_2_AGGREGATE_BY_PREDICTED_CLASS), keyedFiltersPredicted) - .otherBucket(true) - .otherBucketKey(OTHER_BUCKET_KEY))), - List.of()); + .subAggregation( + AggregationBuilders.filters(aggName(STEP_2_AGGREGATE_BY_PREDICTED_CLASS), keyedFiltersPredicted) + .otherBucket(true) + .otherBucketKey(OTHER_BUCKET_KEY) + ) + ), + List.of() + ); } } return Tuple.tuple(List.of(), List.of()); @@ -237,8 +246,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; MulticlassConfusionMatrix that = (MulticlassConfusionMatrix) o; - return this.size == that.size - && Objects.equals(this.aggNamePrefix, that.aggNamePrefix); + return this.size == that.size && Objects.equals(this.aggNamePrefix, that.aggNamePrefix); } @Override @@ -252,9 +260,11 @@ public static class Result implements EvaluationMetricResult { private static final ParseField OTHER_ACTUAL_CLASS_COUNT = new ParseField("other_actual_class_count"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>( - "multiclass_confusion_matrix_result", true, a -> new Result((List) a[0], (long) a[1])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "multiclass_confusion_matrix_result", + true, + a -> new Result((List) a[0], (long) a[1]) + ); static { PARSER.declareObjectArray(constructorArg(), ActualClass.PARSER, CONFUSION_MATRIX); @@ -318,8 +328,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Result that = (Result) o; - return Objects.equals(this.actualClasses, that.actualClasses) - && this.otherActualClassCount == that.otherActualClassCount; + return Objects.equals(this.actualClasses, that.actualClasses) && this.otherActualClassCount == that.otherActualClassCount; } @Override @@ -336,11 +345,11 @@ public static class ActualClass implements ToXContentObject, Writeable { private static final ParseField OTHER_PREDICTED_CLASS_DOC_COUNT = new ParseField("other_predicted_class_doc_count"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>( - "multiclass_confusion_matrix_actual_class", - true, - a -> new ActualClass((String) a[0], (long) a[1], (List) a[2], (long) a[3])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "multiclass_confusion_matrix_actual_class", + true, + a -> new ActualClass((String) a[0], (long) a[1], (List) a[2], (long) a[3]) + ); static { PARSER.declareString(constructorArg(), ACTUAL_CLASS); @@ -359,7 +368,11 @@ public static class ActualClass implements ToXContentObject, Writeable { private final long otherPredictedClassDocCount; public ActualClass( - String actualClass, long actualClassDocCount, List predictedClasses, long otherPredictedClassDocCount) { + String actualClass, + long actualClassDocCount, + List predictedClasses, + long otherPredictedClassDocCount + ) { this.actualClass = ExceptionsHelper.requireNonNull(actualClass, ACTUAL_CLASS); this.actualClassDocCount = requireNonNegative(actualClassDocCount, ACTUAL_CLASS_DOC_COUNT); this.predictedClasses = Collections.unmodifiableList(ExceptionsHelper.requireNonNull(predictedClasses, PREDICTED_CLASSES)); @@ -431,9 +444,11 @@ public static class PredictedClass implements ToXContentObject, Writeable { private static final ParseField COUNT = new ParseField("count"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>( - "multiclass_confusion_matrix_predicted_class", true, a -> new PredictedClass((String) a[0], (long) a[1])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "multiclass_confusion_matrix_predicted_class", + true, + a -> new PredictedClass((String) a[0], (long) a[1]) + ); static { PARSER.declareString(constructorArg(), PREDICTED_CLASS); @@ -481,8 +496,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; PredictedClass that = (PredictedClass) o; - return Objects.equals(this.predictedClass, that.predictedClass) - && this.count == that.count; + return Objects.equals(this.predictedClass, that.predictedClass) && this.count == that.count; } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/PainlessScripts.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/PainlessScripts.java index 8a3d1446bd341..ef14942267683 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/PainlessScripts.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/PainlessScripts.java @@ -20,8 +20,10 @@ final class PainlessScripts { * Template for the comparison script. * It uses "String.valueOf" method in case the mapping types of the two fields are different. */ - private static final MessageFormat COMPARISON_SCRIPT_TEMPLATE = - new MessageFormat("String.valueOf(doc[''{0}''].value).equals(String.valueOf(doc[''{1}''].value))", Locale.ROOT); + private static final MessageFormat COMPARISON_SCRIPT_TEMPLATE = new MessageFormat( + "String.valueOf(doc[''{0}''].value).equals(String.valueOf(doc[''{1}''].value))", + Locale.ROOT + ); /** * Builds script that tests field values equality for the given actual and predicted field names. @@ -31,6 +33,6 @@ final class PainlessScripts { * @return script that tests whether the values of actualField and predictedField are equal */ static Script buildIsEqualScript(String actualField, String predictedField) { - return new Script(COMPARISON_SCRIPT_TEMPLATE.format(new Object[]{ actualField, predictedField })); + return new Script(COMPARISON_SCRIPT_TEMPLATE.format(new Object[] { actualField, predictedField })); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/PerClassSingleValue.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/PerClassSingleValue.java index ffb4d6d0095c0..3b3cac7cdf01d 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/PerClassSingleValue.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/PerClassSingleValue.java @@ -7,11 +7,11 @@ package org.elasticsearch.xpack.core.ml.dataframe.evaluation.classification; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; @@ -26,8 +26,11 @@ public class PerClassSingleValue implements ToXContentObject, Writeable { private static final ParseField CLASS_NAME = new ParseField("class_name"); private static final ParseField VALUE = new ParseField("value"); - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("per_class_result", true, a -> new PerClassSingleValue((String) a[0], (double) a[1])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "per_class_result", + true, + a -> new PerClassSingleValue((String) a[0], (double) a[1]) + ); static { PARSER.declareString(constructorArg(), CLASS_NAME); @@ -75,8 +78,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; PerClassSingleValue that = (PerClassSingleValue) o; - return Objects.equals(this.className, that.className) - && this.value == that.value; + return Objects.equals(this.className, that.className) && this.value == that.value; } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/Precision.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/Precision.java index 30ebd998e35ec..b7b41844a3371 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/Precision.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/Precision.java @@ -7,15 +7,10 @@ package org.elasticsearch.xpack.core.ml.dataframe.evaluation.classification; import org.apache.lucene.util.SetOnce; -import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.util.set.Sets; -import org.elasticsearch.xcontent.ConstructingObjectParser; -import org.elasticsearch.xcontent.ObjectParser; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.core.Tuple; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.script.Script; import org.elasticsearch.search.aggregations.AggregationBuilder; @@ -28,6 +23,11 @@ import org.elasticsearch.search.aggregations.bucket.filter.FiltersAggregator.KeyedFilter; import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregation; +import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationFields; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationMetric; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationMetricResult; @@ -97,8 +97,10 @@ public Set getRequiredFields() { } @Override - public final Tuple, List> aggs(EvaluationParameters parameters, - EvaluationFields fields) { + public final Tuple, List> aggs( + EvaluationParameters parameters, + EvaluationFields fields + ) { String actualField = fields.getActualField(); String predictedField = fields.getPredictedField(); // Store given {@code actualField} for the purpose of generating error message in {@code process}. @@ -109,23 +111,29 @@ public final Tuple, List> a AggregationBuilders.terms(ACTUAL_CLASSES_NAMES_AGG_NAME) .field(actualField) .order(List.of(BucketOrder.count(false), BucketOrder.key(true))) - .size(MAX_CLASSES_CARDINALITY)), - List.of()); + .size(MAX_CLASSES_CARDINALITY) + ), + List.of() + ); } if (result.get() == null) { // This is step 2 - KeyedFilter[] keyedFiltersPredicted = - topActualClassNames.get().stream() - .map(className -> new KeyedFilter(className, QueryBuilders.matchQuery(predictedField, className).lenient(true))) - .toArray(KeyedFilter[]::new); + KeyedFilter[] keyedFiltersPredicted = topActualClassNames.get() + .stream() + .map(className -> new KeyedFilter(className, QueryBuilders.matchQuery(predictedField, className).lenient(true))) + .toArray(KeyedFilter[]::new); Script script = PainlessScripts.buildIsEqualScript(actualField, predictedField); return Tuple.tuple( List.of( AggregationBuilders.filters(BY_PREDICTED_CLASS_AGG_NAME, keyedFiltersPredicted) - .subAggregation(AggregationBuilders.avg(PER_PREDICTED_CLASS_PRECISION_AGG_NAME).script(script))), + .subAggregation(AggregationBuilders.avg(PER_PREDICTED_CLASS_PRECISION_AGG_NAME).script(script)) + ), List.of( PipelineAggregatorBuilders.avgBucket( AVG_PRECISION_AGG_NAME, - BY_PREDICTED_CLASS_AGG_NAME + ">" + PER_PREDICTED_CLASS_PRECISION_AGG_NAME))); + BY_PREDICTED_CLASS_AGG_NAME + ">" + PER_PREDICTED_CLASS_PRECISION_AGG_NAME + ) + ) + ); } return Tuple.tuple(List.of(), List.of()); } @@ -138,14 +146,17 @@ public void process(Aggregations aggs) { // This means there were more than {@code MAX_CLASSES_CARDINALITY} buckets. // We cannot calculate average precision accurately, so we fail. throw ExceptionsHelper.badRequestException( - "Cannot calculate average precision. Cardinality of field [{}] is too high", actualField.get()); + "Cannot calculate average precision. Cardinality of field [{}] is too high", + actualField.get() + ); } topActualClassNames.set( - topActualClassesAgg.getBuckets().stream().map(Terms.Bucket::getKeyAsString).sorted().collect(Collectors.toList())); + topActualClassesAgg.getBuckets().stream().map(Terms.Bucket::getKeyAsString).sorted().collect(Collectors.toList()) + ); } - if (result.get() == null && - aggs.get(BY_PREDICTED_CLASS_AGG_NAME) instanceof Filters && - aggs.get(AVG_PRECISION_AGG_NAME) instanceof NumericMetricsAggregation.SingleValue) { + if (result.get() == null + && aggs.get(BY_PREDICTED_CLASS_AGG_NAME) instanceof Filters + && aggs.get(AVG_PRECISION_AGG_NAME) instanceof NumericMetricsAggregation.SingleValue) { Filters byPredictedClassAgg = aggs.get(BY_PREDICTED_CLASS_AGG_NAME); NumericMetricsAggregation.SingleValue avgPrecisionAgg = aggs.get(AVG_PRECISION_AGG_NAME); List classes = new ArrayList<>(byPredictedClassAgg.getBuckets().size()); @@ -167,8 +178,7 @@ public Optional getResult() { } @Override - public void writeTo(StreamOutput out) throws IOException { - } + public void writeTo(StreamOutput out) throws IOException {} @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { @@ -195,8 +205,11 @@ public static class Result implements EvaluationMetricResult { private static final ParseField AVG_PRECISION = new ParseField("avg_precision"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("precision_result", true, a -> new Result((List) a[0], (double) a[1])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "precision_result", + true, + a -> new Result((List) a[0], (double) a[1]) + ); static { PARSER.declareObjectArray(constructorArg(), PerClassSingleValue.PARSER, CLASSES); @@ -260,8 +273,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Result that = (Result) o; - return Objects.equals(this.classes, that.classes) - && this.avgPrecision == that.avgPrecision; + return Objects.equals(this.classes, that.classes) && this.avgPrecision == that.avgPrecision; } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/Recall.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/Recall.java index 76f6e8d68c002..1413ea1dadda1 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/Recall.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/Recall.java @@ -7,15 +7,10 @@ package org.elasticsearch.xpack.core.ml.dataframe.evaluation.classification; import org.apache.lucene.util.SetOnce; -import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.util.set.Sets; -import org.elasticsearch.xcontent.ConstructingObjectParser; -import org.elasticsearch.xcontent.ObjectParser; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.core.Tuple; import org.elasticsearch.script.Script; import org.elasticsearch.search.aggregations.AggregationBuilder; import org.elasticsearch.search.aggregations.AggregationBuilders; @@ -25,6 +20,11 @@ import org.elasticsearch.search.aggregations.PipelineAggregatorBuilders; import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregation; +import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationFields; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationMetric; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationMetricResult; @@ -91,8 +91,10 @@ public Set getRequiredFields() { } @Override - public final Tuple, List> aggs(EvaluationParameters parameters, - EvaluationFields fields) { + public final Tuple, List> aggs( + EvaluationParameters parameters, + EvaluationFields fields + ) { String actualField = fields.getActualField(); String predictedField = fields.getPredictedField(); // Store given {@code actualField} for the purpose of generating error message in {@code process}. @@ -107,24 +109,27 @@ public final Tuple, List> a .field(actualField) .order(List.of(BucketOrder.count(false), BucketOrder.key(true))) .size(MAX_CLASSES_CARDINALITY) - .subAggregation(AggregationBuilders.avg(PER_ACTUAL_CLASS_RECALL_AGG_NAME).script(script))), + .subAggregation(AggregationBuilders.avg(PER_ACTUAL_CLASS_RECALL_AGG_NAME).script(script)) + ), List.of( - PipelineAggregatorBuilders.avgBucket( - AVG_RECALL_AGG_NAME, - BY_ACTUAL_CLASS_AGG_NAME + ">" + PER_ACTUAL_CLASS_RECALL_AGG_NAME))); + PipelineAggregatorBuilders.avgBucket(AVG_RECALL_AGG_NAME, BY_ACTUAL_CLASS_AGG_NAME + ">" + PER_ACTUAL_CLASS_RECALL_AGG_NAME) + ) + ); } @Override public void process(Aggregations aggs) { - if (result.get() == null && - aggs.get(BY_ACTUAL_CLASS_AGG_NAME) instanceof Terms && - aggs.get(AVG_RECALL_AGG_NAME) instanceof NumericMetricsAggregation.SingleValue) { + if (result.get() == null + && aggs.get(BY_ACTUAL_CLASS_AGG_NAME) instanceof Terms + && aggs.get(AVG_RECALL_AGG_NAME) instanceof NumericMetricsAggregation.SingleValue) { Terms byActualClassAgg = aggs.get(BY_ACTUAL_CLASS_AGG_NAME); if (byActualClassAgg.getSumOfOtherDocCounts() > 0) { // This means there were more than {@code MAX_CLASSES_CARDINALITY} buckets. // We cannot calculate average recall accurately, so we fail. throw ExceptionsHelper.badRequestException( - "Cannot calculate average recall. Cardinality of field [{}] is too high", actualField.get()); + "Cannot calculate average recall. Cardinality of field [{}] is too high", + actualField.get() + ); } NumericMetricsAggregation.SingleValue avgRecallAgg = aggs.get(AVG_RECALL_AGG_NAME); List classes = new ArrayList<>(byActualClassAgg.getBuckets().size()); @@ -143,8 +148,7 @@ public Optional getResult() { } @Override - public void writeTo(StreamOutput out) throws IOException { - } + public void writeTo(StreamOutput out) throws IOException {} @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { @@ -171,8 +175,11 @@ public static class Result implements EvaluationMetricResult { private static final ParseField AVG_RECALL = new ParseField("avg_recall"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("recall_result", true, a -> new Result((List) a[0], (double) a[1])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "recall_result", + true, + a -> new Result((List) a[0], (double) a[1]) + ); static { PARSER.declareObjectArray(constructorArg(), PerClassSingleValue.PARSER, CLASSES); @@ -236,8 +243,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Result that = (Result) o; - return Objects.equals(this.classes, that.classes) - && this.avgRecall == that.avgRecall; + return Objects.equals(this.classes, that.classes) && this.avgRecall == that.avgRecall; } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/common/AbstractAucRoc.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/common/AbstractAucRoc.java index 50f2e0e5b86e0..25eb993e72057 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/common/AbstractAucRoc.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/common/AbstractAucRoc.java @@ -6,14 +6,14 @@ */ package org.elasticsearch.xpack.core.ml.dataframe.evaluation.common; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.search.aggregations.metrics.Percentiles; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.search.aggregations.metrics.Percentiles; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationMetric; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationMetricResult; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; @@ -60,7 +60,9 @@ protected static double[] percentilesArray(Percentiles percentiles) { percentiles.forEach(percentile -> { if (Double.isNaN(percentile.getValue())) { throw ExceptionsHelper.badRequestException( - "[{}] requires at all the percentiles values to be finite numbers", NAME.getPreferredName()); + "[{}] requires at all the percentiles values to be finite numbers", + NAME.getPreferredName() + ); } result[((int) percentile.getPercent()) - 1] = percentile.getValue(); }); @@ -184,7 +186,7 @@ private double interpolateRate(double threshold) { if (binarySearchResult >= 0) { return getRate(binarySearchResult); } else { - int right = (binarySearchResult * -1) -1; + int right = (binarySearchResult * -1) - 1; int left = right - 1; if (right >= percentiles.length) { return 0.0; @@ -240,7 +242,8 @@ private AucRocPoint(StreamInput in) throws IOException { @Override public int compareTo(AucRocPoint o) { - return Comparator.comparingDouble((AucRocPoint p) -> p.threshold).reversed() + return Comparator.comparingDouble((AucRocPoint p) -> p.threshold) + .reversed() .thenComparing(p -> p.fpr) .thenComparing(p -> p.tpr) .compare(this, o); @@ -268,9 +271,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; AucRocPoint that = (AucRocPoint) o; - return tpr == that.tpr - && fpr == that.fpr - && threshold == that.threshold; + return tpr == that.tpr && fpr == that.fpr && threshold == that.threshold; } @Override @@ -348,8 +349,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Result that = (Result) o; - return value == that.value - && Objects.equals(curve, that.curve); + return value == that.value && Objects.equals(curve, that.curve); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/AbstractConfusionMatrixMetric.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/AbstractConfusionMatrixMetric.java index d0c7cc2436ad7..99d7853ddab3a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/AbstractConfusionMatrixMetric.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/AbstractConfusionMatrixMetric.java @@ -6,12 +6,10 @@ */ package org.elasticsearch.xpack.core.ml.dataframe.evaluation.outlierdetection; -import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.util.set.Sets; -import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.core.Tuple; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; @@ -19,6 +17,8 @@ import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.Aggregations; import org.elasticsearch.search.aggregations.PipelineAggregationBuilder; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationFields; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationMetric; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationMetricResult; @@ -46,8 +46,9 @@ protected AbstractConfusionMatrixMetric(List at) { } for (double threshold : thresholds) { if (threshold < 0 || threshold > 1.0) { - throw ExceptionsHelper.badRequestException("[" + getName() + "." + AT.getPreferredName() - + "] values must be in [0.0, 1.0]"); + throw ExceptionsHelper.badRequestException( + "[" + getName() + "." + AT.getPreferredName() + "] values must be in [0.0, 1.0]" + ); } } } @@ -72,12 +73,16 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws @Override public Set getRequiredFields() { return Sets.newHashSet( - EvaluationFields.ACTUAL_FIELD.getPreferredName(), EvaluationFields.PREDICTED_PROBABILITY_FIELD.getPreferredName()); + EvaluationFields.ACTUAL_FIELD.getPreferredName(), + EvaluationFields.PREDICTED_PROBABILITY_FIELD.getPreferredName() + ); } @Override - public Tuple, List> aggs(EvaluationParameters parameters, - EvaluationFields fields) { + public Tuple, List> aggs( + EvaluationParameters parameters, + EvaluationFields fields + ) { if (result != null) { return Tuple.tuple(List.of(), List.of()); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/AucRoc.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/AucRoc.java index be9bf6f2b482a..d90657a2ab8a0 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/AucRoc.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/AucRoc.java @@ -7,20 +7,20 @@ package org.elasticsearch.xpack.core.ml.dataframe.evaluation.outlierdetection; import org.apache.lucene.util.SetOnce; -import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.util.set.Sets; -import org.elasticsearch.xcontent.ConstructingObjectParser; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.core.Tuple; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.aggregations.AggregationBuilder; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.Aggregations; import org.elasticsearch.search.aggregations.PipelineAggregationBuilder; import org.elasticsearch.search.aggregations.bucket.filter.Filter; +import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationFields; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationMetricResult; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationParameters; @@ -60,8 +60,10 @@ public class AucRoc extends AbstractAucRoc { public static final ParseField INCLUDE_CURVE = new ParseField("include_curve"); - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(NAME.getPreferredName(), a -> new AucRoc((Boolean) a[0])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME.getPreferredName(), + a -> new AucRoc((Boolean) a[0]) + ); static { PARSER.declareBoolean(ConstructingObjectParser.optionalConstructorArg(), INCLUDE_CURVE); @@ -108,7 +110,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws @Override public Set getRequiredFields() { return Sets.newHashSet( - EvaluationFields.ACTUAL_FIELD.getPreferredName(), EvaluationFields.PREDICTED_PROBABILITY_FIELD.getPreferredName()); + EvaluationFields.ACTUAL_FIELD.getPreferredName(), + EvaluationFields.PREDICTED_PROBABILITY_FIELD.getPreferredName() + ); } @Override @@ -125,8 +129,10 @@ public int hashCode() { } @Override - public Tuple, List> aggs(EvaluationParameters parameters, - EvaluationFields fields) { + public Tuple, List> aggs( + EvaluationParameters parameters, + EvaluationFields fields + ) { if (result.get() != null) { return Tuple.tuple(List.of(), List.of()); } @@ -136,22 +142,16 @@ public Tuple, List> aggs(Ev String actualField = fields.getActualField(); String predictedProbabilityField = fields.getPredictedProbabilityField(); double[] percentiles = IntStream.range(1, 100).mapToDouble(v -> (double) v).toArray(); - AggregationBuilder percentilesAgg = - AggregationBuilders - .percentiles(PERCENTILES_AGG_NAME) - .field(predictedProbabilityField) - .percentiles(percentiles); - AggregationBuilder percentilesForClassValueAgg = - AggregationBuilders - .filter(TRUE_AGG_NAME, actualIsTrueQuery(actualField)) - .subAggregation(percentilesAgg); - AggregationBuilder percentilesForRestAgg = - AggregationBuilders - .filter(NON_TRUE_AGG_NAME, QueryBuilders.boolQuery().mustNot(actualIsTrueQuery(actualField))) - .subAggregation(percentilesAgg); - return Tuple.tuple( - List.of(percentilesForClassValueAgg, percentilesForRestAgg), - List.of()); + AggregationBuilder percentilesAgg = AggregationBuilders.percentiles(PERCENTILES_AGG_NAME) + .field(predictedProbabilityField) + .percentiles(percentiles); + AggregationBuilder percentilesForClassValueAgg = AggregationBuilders.filter(TRUE_AGG_NAME, actualIsTrueQuery(actualField)) + .subAggregation(percentilesAgg); + AggregationBuilder percentilesForRestAgg = AggregationBuilders.filter( + NON_TRUE_AGG_NAME, + QueryBuilders.boolQuery().mustNot(actualIsTrueQuery(actualField)) + ).subAggregation(percentilesAgg); + return Tuple.tuple(List.of(percentilesForClassValueAgg, percentilesForRestAgg), List.of()); } @Override @@ -162,13 +162,21 @@ public void process(Aggregations aggs) { Filter classAgg = aggs.get(TRUE_AGG_NAME); if (classAgg.getDocCount() == 0) { throw ExceptionsHelper.badRequestException( - "[{}] requires at least one [{}] to have the value [{}]", getName(), fields.get().getActualField(), "true"); + "[{}] requires at least one [{}] to have the value [{}]", + getName(), + fields.get().getActualField(), + "true" + ); } double[] tpPercentiles = percentilesArray(classAgg.getAggregations().get(PERCENTILES_AGG_NAME)); Filter restAgg = aggs.get(NON_TRUE_AGG_NAME); if (restAgg.getDocCount() == 0) { throw ExceptionsHelper.badRequestException( - "[{}] requires at least one [{}] to have a different value than [{}]", getName(), fields.get().getActualField(), "true"); + "[{}] requires at least one [{}] to have a different value than [{}]", + getName(), + fields.get().getActualField(), + "true" + ); } double[] fpPercentiles = percentilesArray(restAgg.getAggregations().get(PERCENTILES_AGG_NAME)); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/ConfusionMatrix.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/ConfusionMatrix.java index 8567deb282422..bf13b882f3e98 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/ConfusionMatrix.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/ConfusionMatrix.java @@ -6,15 +6,15 @@ */ package org.elasticsearch.xpack.core.ml.dataframe.evaluation.outlierdetection; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.xcontent.ConstructingObjectParser; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.aggregations.AggregationBuilder; import org.elasticsearch.search.aggregations.Aggregations; import org.elasticsearch.search.aggregations.bucket.filter.Filter; +import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationMetricResult; import java.io.IOException; @@ -29,8 +29,10 @@ public class ConfusionMatrix extends AbstractConfusionMatrixMetric { public static final ParseField NAME = new ParseField("confusion_matrix"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(NAME.getPreferredName(), - a -> new ConfusionMatrix((List) a[0])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME.getPreferredName(), + a -> new ConfusionMatrix((List) a[0]) + ); static { PARSER.declareDoubleArray(ConstructingObjectParser.constructorArg(), AT); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/OutlierDetection.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/OutlierDetection.java index a132de57d6321..7de3308670cad 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/OutlierDetection.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/OutlierDetection.java @@ -6,15 +6,15 @@ */ package org.elasticsearch.xpack.core.ml.dataframe.evaluation.outlierdetection; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.index.query.QueryBuilder; +import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.index.query.QueryBuilder; -import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.Evaluation; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationFields; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationMetric; @@ -40,13 +40,18 @@ public class OutlierDetection implements Evaluation { @SuppressWarnings("unchecked") public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - NAME.getPreferredName(), a -> new OutlierDetection((String) a[0], (String) a[1], (List) a[2])); + NAME.getPreferredName(), + a -> new OutlierDetection((String) a[0], (String) a[1], (List) a[2]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), ACTUAL_FIELD); PARSER.declareString(ConstructingObjectParser.constructorArg(), PREDICTED_PROBABILITY_FIELD); - PARSER.declareNamedObjects(ConstructingObjectParser.optionalConstructorArg(), - (p, c, n) -> p.namedObject(EvaluationMetric.class, registeredMetricName(NAME.getPreferredName(), n), c), METRICS); + PARSER.declareNamedObjects( + ConstructingObjectParser.optionalConstructorArg(), + (p, c, n) -> p.namedObject(EvaluationMetric.class, registeredMetricName(NAME.getPreferredName(), n), c), + METRICS + ); } public static OutlierDetection fromXContent(XContentParser parser) { @@ -70,17 +75,15 @@ public static QueryBuilder actualIsTrueQuery(String actualField) { */ private final List metrics; - public OutlierDetection(String actualField, - String predictedProbabilityField, - @Nullable List metrics) { - this.fields = - new EvaluationFields( - ExceptionsHelper.requireNonNull(actualField, ACTUAL_FIELD), - null, - null, - null, - ExceptionsHelper.requireNonNull(predictedProbabilityField, PREDICTED_PROBABILITY_FIELD), - false); + public OutlierDetection(String actualField, String predictedProbabilityField, @Nullable List metrics) { + this.fields = new EvaluationFields( + ExceptionsHelper.requireNonNull(actualField, ACTUAL_FIELD), + null, + null, + null, + ExceptionsHelper.requireNonNull(predictedProbabilityField, PREDICTED_PROBABILITY_FIELD), + false + ); this.metrics = initMetrics(metrics, OutlierDetection::defaultMetrics); } @@ -89,7 +92,8 @@ private static List defaultMetrics() { new AucRoc(false), new Precision(Arrays.asList(0.25, 0.5, 0.75)), new Recall(Arrays.asList(0.25, 0.5, 0.75)), - new ConfusionMatrix(Arrays.asList(0.25, 0.5, 0.75))); + new ConfusionMatrix(Arrays.asList(0.25, 0.5, 0.75)) + ); } public OutlierDetection(StreamInput in) throws IOException { @@ -145,8 +149,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; OutlierDetection that = (OutlierDetection) o; - return Objects.equals(fields, that.fields) - && Objects.equals(metrics, that.metrics); + return Objects.equals(fields, that.fields) && Objects.equals(metrics, that.metrics); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/Precision.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/Precision.java index 64b3e38116009..fcbf1c6216239 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/Precision.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/Precision.java @@ -6,13 +6,13 @@ */ package org.elasticsearch.xpack.core.ml.dataframe.evaluation.outlierdetection; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; -import org.elasticsearch.xcontent.ConstructingObjectParser; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.aggregations.AggregationBuilder; import org.elasticsearch.search.aggregations.Aggregations; import org.elasticsearch.search.aggregations.bucket.filter.Filter; +import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationMetricResult; import java.io.IOException; @@ -27,8 +27,10 @@ public class Precision extends AbstractConfusionMatrixMetric { public static final ParseField NAME = new ParseField("precision"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(NAME.getPreferredName(), - a -> new Precision((List) a[0])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME.getPreferredName(), + a -> new Precision((List) a[0]) + ); static { PARSER.declareDoubleArray(ConstructingObjectParser.constructorArg(), AT); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/Recall.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/Recall.java index 415f9a1c81cfd..07f0cdbb6c17a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/Recall.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/Recall.java @@ -6,13 +6,13 @@ */ package org.elasticsearch.xpack.core.ml.dataframe.evaluation.outlierdetection; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; -import org.elasticsearch.xcontent.ConstructingObjectParser; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.aggregations.AggregationBuilder; import org.elasticsearch.search.aggregations.Aggregations; import org.elasticsearch.search.aggregations.bucket.filter.Filter; +import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationMetricResult; import java.io.IOException; @@ -27,8 +27,10 @@ public class Recall extends AbstractConfusionMatrixMetric { public static final ParseField NAME = new ParseField("recall"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(NAME.getPreferredName(), - a -> new Recall((List) a[0])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME.getPreferredName(), + a -> new Recall((List) a[0]) + ); static { PARSER.declareDoubleArray(ConstructingObjectParser.constructorArg(), AT); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/ScoreByThresholdResult.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/ScoreByThresholdResult.java index 6c9c73012702b..0cf9b96ca4a37 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/ScoreByThresholdResult.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/ScoreByThresholdResult.java @@ -6,9 +6,9 @@ */ package org.elasticsearch.xpack.core.ml.dataframe.evaluation.outlierdetection; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationMetricResult; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/regression/Huber.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/regression/Huber.java index ede894314c62a..28802148220b6 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/regression/Huber.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/regression/Huber.java @@ -6,21 +6,21 @@ */ package org.elasticsearch.xpack.core.ml.dataframe.evaluation.regression; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.util.set.Sets; -import org.elasticsearch.xcontent.ConstructingObjectParser; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.core.Tuple; import org.elasticsearch.script.Script; import org.elasticsearch.search.aggregations.AggregationBuilder; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.Aggregations; import org.elasticsearch.search.aggregations.PipelineAggregationBuilder; import org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregation; +import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.dataframe.analyses.Regression.LossFunction; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationFields; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationMetric; @@ -53,18 +53,20 @@ public class Huber implements EvaluationMetric { public static final ParseField DELTA = new ParseField("delta"); private static final double DEFAULT_DELTA = 1.0; - private static final String PAINLESS_TEMPLATE = - "def a = doc[''{0}''].value - doc[''{1}''].value;" + - "def delta2 = {2};" + - "return delta2 * (Math.sqrt(1.0 + Math.pow(a, 2) / delta2) - 1.0);"; + private static final String PAINLESS_TEMPLATE = "def a = doc[''{0}''].value - doc[''{1}''].value;" + + "def delta2 = {2};" + + "return delta2 * (Math.sqrt(1.0 + Math.pow(a, 2) / delta2) - 1.0);"; private static final String AGG_NAME = "regression_" + NAME.getPreferredName(); - private static String buildScript(Object...args) { + private static String buildScript(Object... args) { return new MessageFormat(PAINLESS_TEMPLATE, Locale.ROOT).format(args); } - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(NAME.getPreferredName(), true, args -> new Huber((Double) args[0])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME.getPreferredName(), + true, + args -> new Huber((Double) args[0]) + ); static { PARSER.declareDouble(optionalConstructorArg(), DELTA); @@ -100,8 +102,10 @@ public Set getRequiredFields() { } @Override - public Tuple, List> aggs(EvaluationParameters parameters, - EvaluationFields fields) { + public Tuple, List> aggs( + EvaluationParameters parameters, + EvaluationFields fields + ) { if (result != null) { return Tuple.tuple(Collections.emptyList(), Collections.emptyList()); } @@ -109,7 +113,8 @@ public Tuple, List> aggs(Ev String predictedField = fields.getPredictedField(); return Tuple.tuple( Arrays.asList(AggregationBuilders.avg(AGG_NAME).script(new Script(buildScript(actualField, predictedField, delta * delta)))), - Collections.emptyList()); + Collections.emptyList() + ); } @Override @@ -198,7 +203,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - Result other = (Result)o; + Result other = (Result) o; return value == other.value; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/regression/MeanSquaredError.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/regression/MeanSquaredError.java index f9caf2392ee76..2a50383494abe 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/regression/MeanSquaredError.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/regression/MeanSquaredError.java @@ -6,20 +6,20 @@ */ package org.elasticsearch.xpack.core.ml.dataframe.evaluation.regression; -import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.util.set.Sets; -import org.elasticsearch.xcontent.ObjectParser; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.core.Tuple; import org.elasticsearch.script.Script; import org.elasticsearch.search.aggregations.AggregationBuilder; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.Aggregations; import org.elasticsearch.search.aggregations.PipelineAggregationBuilder; import org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregation; +import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.dataframe.analyses.Regression.LossFunction; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationFields; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationMetric; @@ -47,17 +47,18 @@ public class MeanSquaredError implements EvaluationMetric { public static final ParseField NAME = new ParseField(LossFunction.MSE.toString()); - private static final String PAINLESS_TEMPLATE = - "def diff = doc[''{0}''].value - doc[''{1}''].value;" + - "return diff * diff;"; + private static final String PAINLESS_TEMPLATE = "def diff = doc[''{0}''].value - doc[''{1}''].value;" + "return diff * diff;"; private static final String AGG_NAME = "regression_" + NAME.getPreferredName(); - private static String buildScript(Object...args) { + private static String buildScript(Object... args) { return new MessageFormat(PAINLESS_TEMPLATE, Locale.ROOT).format(args); } - private static final ObjectParser PARSER = - new ObjectParser<>(NAME.getPreferredName(), true, MeanSquaredError::new); + private static final ObjectParser PARSER = new ObjectParser<>( + NAME.getPreferredName(), + true, + MeanSquaredError::new + ); public static MeanSquaredError fromXContent(XContentParser parser) { return PARSER.apply(parser, null); @@ -80,8 +81,10 @@ public Set getRequiredFields() { } @Override - public Tuple, List> aggs(EvaluationParameters parameters, - EvaluationFields fields) { + public Tuple, List> aggs( + EvaluationParameters parameters, + EvaluationFields fields + ) { if (result != null) { return Tuple.tuple(Collections.emptyList(), Collections.emptyList()); } @@ -89,7 +92,8 @@ public Tuple, List> aggs(Ev String predictedField = fields.getPredictedField(); return Tuple.tuple( Arrays.asList(AggregationBuilders.avg(AGG_NAME).script(new Script(buildScript(actualField, predictedField)))), - Collections.emptyList()); + Collections.emptyList() + ); } @Override @@ -109,8 +113,7 @@ public String getWriteableName() { } @Override - public void writeTo(StreamOutput out) throws IOException { - } + public void writeTo(StreamOutput out) throws IOException {} @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { @@ -176,7 +179,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - Result other = (Result)o; + Result other = (Result) o; return value == other.value; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/regression/MeanSquaredLogarithmicError.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/regression/MeanSquaredLogarithmicError.java index c3b3927326f02..9ca3e39d53c4b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/regression/MeanSquaredLogarithmicError.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/regression/MeanSquaredLogarithmicError.java @@ -6,21 +6,21 @@ */ package org.elasticsearch.xpack.core.ml.dataframe.evaluation.regression; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.util.set.Sets; -import org.elasticsearch.xcontent.ConstructingObjectParser; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.core.Tuple; import org.elasticsearch.script.Script; import org.elasticsearch.search.aggregations.AggregationBuilder; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.Aggregations; import org.elasticsearch.search.aggregations.PipelineAggregationBuilder; import org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregation; +import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.dataframe.analyses.Regression.LossFunction; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationFields; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationMetric; @@ -52,18 +52,20 @@ public class MeanSquaredLogarithmicError implements EvaluationMetric { public static final ParseField OFFSET = new ParseField("offset"); private static final double DEFAULT_OFFSET = 1.0; - private static final String PAINLESS_TEMPLATE = - "def offset = {2};" + - "def diff = Math.log(doc[''{0}''].value + offset) - Math.log(doc[''{1}''].value + offset);" + - "return diff * diff;"; + private static final String PAINLESS_TEMPLATE = "def offset = {2};" + + "def diff = Math.log(doc[''{0}''].value + offset) - Math.log(doc[''{1}''].value + offset);" + + "return diff * diff;"; private static final String AGG_NAME = "regression_" + NAME.getPreferredName(); - private static String buildScript(Object...args) { + private static String buildScript(Object... args) { return new MessageFormat(PAINLESS_TEMPLATE, Locale.ROOT).format(args); } - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(NAME.getPreferredName(), true, args -> new MeanSquaredLogarithmicError((Double) args[0])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME.getPreferredName(), + true, + args -> new MeanSquaredLogarithmicError((Double) args[0]) + ); static { PARSER.declareDouble(optionalConstructorArg(), OFFSET); @@ -95,8 +97,10 @@ public Set getRequiredFields() { } @Override - public Tuple, List> aggs(EvaluationParameters parameters, - EvaluationFields fields) { + public Tuple, List> aggs( + EvaluationParameters parameters, + EvaluationFields fields + ) { if (result != null) { return Tuple.tuple(Collections.emptyList(), Collections.emptyList()); } @@ -104,7 +108,8 @@ public Tuple, List> aggs(Ev String predictedField = fields.getPredictedField(); return Tuple.tuple( Arrays.asList(AggregationBuilders.avg(AGG_NAME).script(new Script(buildScript(actualField, predictedField, offset)))), - Collections.emptyList()); + Collections.emptyList() + ); } @Override @@ -193,7 +198,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - Result other = (Result)o; + Result other = (Result) o; return value == other.value; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/regression/RSquared.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/regression/RSquared.java index 225190d1a5c4b..fa41661771f62 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/regression/RSquared.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/regression/RSquared.java @@ -6,14 +6,10 @@ */ package org.elasticsearch.xpack.core.ml.dataframe.evaluation.regression; -import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.util.set.Sets; -import org.elasticsearch.xcontent.ObjectParser; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.core.Tuple; import org.elasticsearch.script.Script; import org.elasticsearch.search.aggregations.AggregationBuilder; import org.elasticsearch.search.aggregations.AggregationBuilders; @@ -22,6 +18,10 @@ import org.elasticsearch.search.aggregations.metrics.ExtendedStats; import org.elasticsearch.search.aggregations.metrics.ExtendedStatsAggregationBuilder; import org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregation; +import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationFields; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationMetric; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationMetricResult; @@ -51,17 +51,14 @@ public class RSquared implements EvaluationMetric { public static final ParseField NAME = new ParseField("r_squared"); - private static final String PAINLESS_TEMPLATE = - "def diff = doc[''{0}''].value - doc[''{1}''].value;" + - "return diff * diff;"; + private static final String PAINLESS_TEMPLATE = "def diff = doc[''{0}''].value - doc[''{1}''].value;" + "return diff * diff;"; private static final String SS_RES = "residual_sum_of_squares"; - private static String buildScript(Object...args) { + private static String buildScript(Object... args) { return new MessageFormat(PAINLESS_TEMPLATE, Locale.ROOT).format(args); } - private static final ObjectParser PARSER = - new ObjectParser<>(NAME.getPreferredName(), true, RSquared::new); + private static final ObjectParser PARSER = new ObjectParser<>(NAME.getPreferredName(), true, RSquared::new); public static RSquared fromXContent(XContentParser parser) { return PARSER.apply(parser, null); @@ -84,8 +81,10 @@ public Set getRequiredFields() { } @Override - public Tuple, List> aggs(EvaluationParameters parameters, - EvaluationFields fields) { + public Tuple, List> aggs( + EvaluationParameters parameters, + EvaluationFields fields + ) { if (result != null) { return Tuple.tuple(Collections.emptyList(), Collections.emptyList()); } @@ -94,8 +93,10 @@ public Tuple, List> aggs(Ev return Tuple.tuple( Arrays.asList( AggregationBuilders.sum(SS_RES).script(new Script(buildScript(actualField, predictedField))), - AggregationBuilders.extendedStats(ExtendedStatsAggregationBuilder.NAME + "_actual").field(actualField)), - Collections.emptyList()); + AggregationBuilders.extendedStats(ExtendedStatsAggregationBuilder.NAME + "_actual").field(actualField) + ), + Collections.emptyList() + ); } @Override @@ -107,9 +108,9 @@ public void process(Aggregations aggs) { || extendedStats == null || extendedStats.getCount() == 0 || extendedStats.getVariance() == 0; - result = validResult ? - new Result(0.0) : - new Result(1 - (residualSumOfSquares.value() / (extendedStats.getVariance() * extendedStats.getCount()))); + result = validResult + ? new Result(0.0) + : new Result(1 - (residualSumOfSquares.value() / (extendedStats.getVariance() * extendedStats.getCount()))); } @Override @@ -123,8 +124,7 @@ public String getWriteableName() { } @Override - public void writeTo(StreamOutput out) throws IOException { - } + public void writeTo(StreamOutput out) throws IOException {} @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { @@ -190,7 +190,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - Result other = (Result)o; + Result other = (Result) o; return value == other.value; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/regression/Regression.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/regression/Regression.java index de02ee891c7a9..0989c9ccd0f8c 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/regression/Regression.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/regression/Regression.java @@ -6,11 +6,11 @@ */ package org.elasticsearch.xpack.core.ml.dataframe.evaluation.regression; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.Evaluation; @@ -38,13 +38,18 @@ public class Regression implements Evaluation { @SuppressWarnings("unchecked") public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - NAME.getPreferredName(), a -> new Regression((String) a[0], (String) a[1], (List) a[2])); + NAME.getPreferredName(), + a -> new Regression((String) a[0], (String) a[1], (List) a[2]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), ACTUAL_FIELD); PARSER.declareString(ConstructingObjectParser.constructorArg(), PREDICTED_FIELD); - PARSER.declareNamedObjects(ConstructingObjectParser.optionalConstructorArg(), - (p, c, n) -> p.namedObject(EvaluationMetric.class, registeredMetricName(NAME.getPreferredName(), n), c), METRICS); + PARSER.declareNamedObjects( + ConstructingObjectParser.optionalConstructorArg(), + (p, c, n) -> p.namedObject(EvaluationMetric.class, registeredMetricName(NAME.getPreferredName(), n), c), + METRICS + ); } public static Regression fromXContent(XContentParser parser) { @@ -65,14 +70,14 @@ public static Regression fromXContent(XContentParser parser) { private final List metrics; public Regression(String actualField, String predictedField, @Nullable List metrics) { - this.fields = - new EvaluationFields( - ExceptionsHelper.requireNonNull(actualField, ACTUAL_FIELD), - ExceptionsHelper.requireNonNull(predictedField, PREDICTED_FIELD), - null, - null, - null, - false); + this.fields = new EvaluationFields( + ExceptionsHelper.requireNonNull(actualField, ACTUAL_FIELD), + ExceptionsHelper.requireNonNull(predictedField, PREDICTED_FIELD), + null, + null, + null, + false + ); this.metrics = initMetrics(metrics, Regression::defaultMetrics); } @@ -133,8 +138,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Regression that = (Regression) o; - return Objects.equals(that.fields, this.fields) - && Objects.equals(that.metrics, this.metrics); + return Objects.equals(that.fields, this.fields) && Objects.equals(that.metrics, this.metrics); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/explain/FieldSelection.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/explain/FieldSelection.java index ea7167d637285..0bdf0fecf203f 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/explain/FieldSelection.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/explain/FieldSelection.java @@ -6,12 +6,12 @@ */ package org.elasticsearch.xpack.core.ml.dataframe.explain; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -33,7 +33,8 @@ public class FieldSelection implements ToXContentObject, Writeable { private static final ParseField REASON = new ParseField("reason"); public enum FeatureType { - CATEGORICAL, NUMERICAL; + CATEGORICAL, + NUMERICAL; public static FeatureType fromString(String value) { return FeatureType.valueOf(value.toUpperCase(Locale.ROOT)); @@ -46,9 +47,17 @@ public String toString() { } @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("field_selection", - a -> new FieldSelection((String) a[0], new HashSet<>((List) a[1]), (boolean) a[2], (boolean) a[3], (FeatureType) a[4], - (String) a[5])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "field_selection", + a -> new FieldSelection( + (String) a[0], + new HashSet<>((List) a[1]), + (boolean) a[2], + (boolean) a[3], + (FeatureType) a[4], + (String) a[5] + ) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), NAME); @@ -74,8 +83,14 @@ public static FieldSelection excluded(String name, Set mappingTypes, Str return new FieldSelection(name, mappingTypes, false, false, null, reason); } - FieldSelection(String name, Set mappingTypes, boolean isIncluded, boolean isRequired, @Nullable FeatureType featureType, - @Nullable String reason) { + FieldSelection( + String name, + Set mappingTypes, + boolean isIncluded, + boolean isRequired, + @Nullable FeatureType featureType, + @Nullable String reason + ) { this.name = Objects.requireNonNull(name); this.mappingTypes = Collections.unmodifiableSet(mappingTypes); this.isIncluded = isIncluded; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/explain/MemoryEstimation.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/explain/MemoryEstimation.java index d32366974ad44..a44e2531e3a6d 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/explain/MemoryEstimation.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/explain/MemoryEstimation.java @@ -6,14 +6,14 @@ */ package org.elasticsearch.xpack.core.ml.dataframe.explain; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -27,20 +27,24 @@ public class MemoryEstimation implements ToXContentObject, Writeable { public static final ParseField EXPECTED_MEMORY_WITHOUT_DISK = new ParseField("expected_memory_without_disk"); public static final ParseField EXPECTED_MEMORY_WITH_DISK = new ParseField("expected_memory_with_disk"); - public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("memory_estimation", - a -> new MemoryEstimation((ByteSizeValue) a[0], (ByteSizeValue) a[1])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "memory_estimation", + a -> new MemoryEstimation((ByteSizeValue) a[0], (ByteSizeValue) a[1]) + ); static { PARSER.declareField( optionalConstructorArg(), (p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), EXPECTED_MEMORY_WITHOUT_DISK.getPreferredName()), EXPECTED_MEMORY_WITHOUT_DISK, - ObjectParser.ValueType.VALUE); + ObjectParser.ValueType.VALUE + ); PARSER.declareField( optionalConstructorArg(), (p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), EXPECTED_MEMORY_WITH_DISK.getPreferredName()), EXPECTED_MEMORY_WITH_DISK, - ObjectParser.ValueType.VALUE); + ObjectParser.ValueType.VALUE + ); } private final ByteSizeValue expectedMemoryWithoutDisk; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/AnalysisStats.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/AnalysisStats.java index 4f99f5dcba999..ea9e553d74676 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/AnalysisStats.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/AnalysisStats.java @@ -12,5 +12,4 @@ /** * Statistics for the data frame analysis */ -public interface AnalysisStats extends ToXContentObject, NamedWriteable { -} +public interface AnalysisStats extends ToXContentObject, NamedWriteable {} diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/classification/ClassificationStats.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/classification/ClassificationStats.java index dff8c49480d87..51f06b9c7f52a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/classification/ClassificationStats.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/classification/ClassificationStats.java @@ -6,11 +6,11 @@ */ package org.elasticsearch.xpack.core.ml.dataframe.stats.classification; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.common.time.TimeUtils; import org.elasticsearch.xpack.core.ml.dataframe.stats.AnalysisStats; @@ -35,7 +35,9 @@ public class ClassificationStats implements AnalysisStats { public static final ConstructingObjectParser LENIENT_PARSER = createParser(true); private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { - ConstructingObjectParser parser = new ConstructingObjectParser<>(TYPE_VALUE, ignoreUnknownFields, + ConstructingObjectParser parser = new ConstructingObjectParser<>( + TYPE_VALUE, + ignoreUnknownFields, a -> new ClassificationStats( (String) a[0], (Instant) a[1], @@ -48,17 +50,28 @@ private static ConstructingObjectParser createParser( parser.declareString((bucket, s) -> {}, Fields.TYPE); parser.declareString(ConstructingObjectParser.constructorArg(), Fields.JOB_ID); - parser.declareField(ConstructingObjectParser.constructorArg(), + parser.declareField( + ConstructingObjectParser.constructorArg(), p -> TimeUtils.parseTimeFieldToInstant(p, Fields.TIMESTAMP.getPreferredName()), Fields.TIMESTAMP, - ObjectParser.ValueType.VALUE); + ObjectParser.ValueType.VALUE + ); parser.declareInt(ConstructingObjectParser.constructorArg(), ITERATION); - parser.declareObject(ConstructingObjectParser.constructorArg(), - (p, c) -> Hyperparameters.fromXContent(p, ignoreUnknownFields), HYPERPARAMETERS); - parser.declareObject(ConstructingObjectParser.constructorArg(), - (p, c) -> TimingStats.fromXContent(p, ignoreUnknownFields), TIMING_STATS); - parser.declareObject(ConstructingObjectParser.constructorArg(), - (p, c) -> ValidationLoss.fromXContent(p, ignoreUnknownFields), VALIDATION_LOSS); + parser.declareObject( + ConstructingObjectParser.constructorArg(), + (p, c) -> Hyperparameters.fromXContent(p, ignoreUnknownFields), + HYPERPARAMETERS + ); + parser.declareObject( + ConstructingObjectParser.constructorArg(), + (p, c) -> TimingStats.fromXContent(p, ignoreUnknownFields), + TIMING_STATS + ); + parser.declareObject( + ConstructingObjectParser.constructorArg(), + (p, c) -> ValidationLoss.fromXContent(p, ignoreUnknownFields), + VALIDATION_LOSS + ); return parser; } @@ -69,8 +82,14 @@ private static ConstructingObjectParser createParser( private final TimingStats timingStats; private final ValidationLoss validationLoss; - public ClassificationStats(String jobId, Instant timestamp, int iteration, Hyperparameters hyperparameters, TimingStats timingStats, - ValidationLoss validationLoss) { + public ClassificationStats( + String jobId, + Instant timestamp, + int iteration, + Hyperparameters hyperparameters, + TimingStats timingStats, + ValidationLoss validationLoss + ) { this.jobId = Objects.requireNonNull(jobId); // We intend to store this timestamp in millis granularity. Thus we're rounding here to ensure // internal representation matches toXContent diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/classification/Hyperparameters.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/classification/Hyperparameters.java index 16d5da913cdda..87b4d2c084450 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/classification/Hyperparameters.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/classification/Hyperparameters.java @@ -6,11 +6,11 @@ */ package org.elasticsearch.xpack.core.ml.dataframe.stats.classification; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -32,7 +32,8 @@ public class Hyperparameters implements ToXContentObject, Writeable { public static final ParseField LAMBDA = new ParseField("lambda"); public static final ParseField MAX_ATTEMPTS_TO_ADD_TREE = new ParseField("max_attempts_to_add_tree"); public static final ParseField MAX_OPTIMIZATION_ROUNDS_PER_HYPERPARAMETER = new ParseField( - "max_optimization_rounds_per_hyperparameter"); + "max_optimization_rounds_per_hyperparameter" + ); public static final ParseField MAX_TREES = new ParseField("max_trees"); public static final ParseField NUM_FOLDS = new ParseField("num_folds"); public static final ParseField NUM_SPLITS_PER_FEATURE = new ParseField("num_splits_per_feature"); @@ -44,7 +45,8 @@ public static Hyperparameters fromXContent(XContentParser parser, boolean ignore } private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { - ConstructingObjectParser parser = new ConstructingObjectParser<>("classification_hyperparameters", + ConstructingObjectParser parser = new ConstructingObjectParser<>( + "classification_hyperparameters", ignoreUnknownFields, a -> new Hyperparameters( (String) a[0], @@ -62,7 +64,8 @@ private static ConstructingObjectParser createParser(bool (int) a[12], (double) a[13], (double) a[14] - )); + ) + ); parser.declareString(constructorArg(), CLASS_ASSIGNMENT_OBJECTIVE); parser.declareDouble(constructorArg(), ALPHA); @@ -99,21 +102,23 @@ private static ConstructingObjectParser createParser(bool private final double softTreeDepthLimit; private final double softTreeDepthTolerance; - public Hyperparameters(String classAssignmentObjective, - double alpha, - double downsampleFactor, - double eta, - double etaGrowthRatePerTree, - double featureBagFraction, - double gamma, - double lambda, - int maxAttemptsToAddTree, - int maxOptimizationRoundsPerHyperparameter, - int maxTrees, - int numFolds, - int numSplitsPerFeature, - double softTreeDepthLimit, - double softTreeDepthTolerance) { + public Hyperparameters( + String classAssignmentObjective, + double alpha, + double downsampleFactor, + double eta, + double etaGrowthRatePerTree, + double featureBagFraction, + double gamma, + double lambda, + int maxAttemptsToAddTree, + int maxOptimizationRoundsPerHyperparameter, + int maxTrees, + int numFolds, + int numSplitsPerFeature, + double softTreeDepthLimit, + double softTreeDepthTolerance + ) { this.classAssignmentObjective = Objects.requireNonNull(classAssignmentObjective); this.alpha = alpha; this.downsampleFactor = downsampleFactor; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/classification/TimingStats.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/classification/TimingStats.java index 5c38676793405..b64bb9787024a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/classification/TimingStats.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/classification/TimingStats.java @@ -6,12 +6,12 @@ */ package org.elasticsearch.xpack.core.ml.dataframe.stats.classification; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -29,9 +29,11 @@ public static TimingStats fromXContent(XContentParser parser, boolean ignoreUnkn } private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { - ConstructingObjectParser parser = new ConstructingObjectParser<>("classification_timing_stats", + ConstructingObjectParser parser = new ConstructingObjectParser<>( + "classification_timing_stats", ignoreUnknownFields, - a -> new TimingStats(TimeValue.timeValueMillis((long) a[0]), TimeValue.timeValueMillis((long) a[1]))); + a -> new TimingStats(TimeValue.timeValueMillis((long) a[0]), TimeValue.timeValueMillis((long) a[1])) + ); parser.declareLong(ConstructingObjectParser.constructorArg(), ELAPSED_TIME); parser.declareLong(ConstructingObjectParser.constructorArg(), ITERATION_TIME); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/classification/ValidationLoss.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/classification/ValidationLoss.java index 4b4e58a8280a7..d4a105a9ca605 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/classification/ValidationLoss.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/classification/ValidationLoss.java @@ -6,11 +6,11 @@ */ package org.elasticsearch.xpack.core.ml.dataframe.stats.classification; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -32,13 +32,18 @@ public static ValidationLoss fromXContent(XContentParser parser, boolean ignoreU @SuppressWarnings("unchecked") private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { - ConstructingObjectParser parser = new ConstructingObjectParser<>("classification_validation_loss", + ConstructingObjectParser parser = new ConstructingObjectParser<>( + "classification_validation_loss", ignoreUnknownFields, - a -> new ValidationLoss((String) a[0], (List) a[1])); + a -> new ValidationLoss((String) a[0], (List) a[1]) + ); parser.declareString(ConstructingObjectParser.constructorArg(), LOSS_TYPE); - parser.declareObjectArray(ConstructingObjectParser.constructorArg(), - (p, c) -> FoldValues.fromXContent(p, ignoreUnknownFields), FOLD_VALUES); + parser.declareObjectArray( + ConstructingObjectParser.constructorArg(), + (p, c) -> FoldValues.fromXContent(p, ignoreUnknownFields), + FOLD_VALUES + ); return parser; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/common/DataCounts.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/common/DataCounts.java index fdaa98c6f6694..4df94aa057afc 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/common/DataCounts.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/common/DataCounts.java @@ -6,11 +6,11 @@ */ package org.elasticsearch.xpack.core.ml.dataframe.stats.common; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ml.dataframe.stats.Fields; @@ -31,8 +31,11 @@ public class DataCounts implements ToXContentObject, Writeable { public static final ConstructingObjectParser LENIENT_PARSER = createParser(true); private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { - ConstructingObjectParser parser = new ConstructingObjectParser<>(TYPE_VALUE, ignoreUnknownFields, - a -> new DataCounts((String) a[0], (long) a[1], (long) a[2], (long) a[3])); + ConstructingObjectParser parser = new ConstructingObjectParser<>( + TYPE_VALUE, + ignoreUnknownFields, + a -> new DataCounts((String) a[0], (long) a[1], (long) a[2], (long) a[3]) + ); parser.declareString((bucket, s) -> {}, Fields.TYPE); parser.declareString(ConstructingObjectParser.constructorArg(), Fields.JOB_ID); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/common/FoldValues.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/common/FoldValues.java index a9ea8dc42c0c4..23de75a611780 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/common/FoldValues.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/common/FoldValues.java @@ -6,11 +6,11 @@ */ package org.elasticsearch.xpack.core.ml.dataframe.stats.common; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -31,8 +31,11 @@ public static FoldValues fromXContent(XContentParser parser, boolean ignoreUnkno @SuppressWarnings("unchecked") private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { - ConstructingObjectParser parser = new ConstructingObjectParser<>("fold_values", ignoreUnknownFields, - a -> new FoldValues((int) a[0], (List) a[1])); + ConstructingObjectParser parser = new ConstructingObjectParser<>( + "fold_values", + ignoreUnknownFields, + a -> new FoldValues((int) a[0], (List) a[1]) + ); parser.declareInt(ConstructingObjectParser.constructorArg(), FOLD); parser.declareDoubleArray(ConstructingObjectParser.constructorArg(), VALUES); return parser; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/common/MemoryUsage.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/common/MemoryUsage.java index 60374f83fa281..52890f9f3b5f8 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/common/MemoryUsage.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/common/MemoryUsage.java @@ -6,14 +6,14 @@ */ package org.elasticsearch.xpack.core.ml.dataframe.stats.common; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.common.time.TimeUtils; @@ -38,15 +38,20 @@ public class MemoryUsage implements Writeable, ToXContentObject { public static final ConstructingObjectParser LENIENT_PARSER = createParser(true); private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { - ConstructingObjectParser parser = new ConstructingObjectParser<>(TYPE_VALUE, - ignoreUnknownFields, a -> new MemoryUsage((String) a[0], (Instant) a[1], (long) a[2], (Status) a[3], (Long) a[4])); + ConstructingObjectParser parser = new ConstructingObjectParser<>( + TYPE_VALUE, + ignoreUnknownFields, + a -> new MemoryUsage((String) a[0], (Instant) a[1], (long) a[2], (Status) a[3], (Long) a[4]) + ); parser.declareString((bucket, s) -> {}, Fields.TYPE); parser.declareString(ConstructingObjectParser.constructorArg(), Fields.JOB_ID); - parser.declareField(ConstructingObjectParser.constructorArg(), + parser.declareField( + ConstructingObjectParser.constructorArg(), p -> TimeUtils.parseTimeFieldToInstant(p, Fields.TIMESTAMP.getPreferredName()), Fields.TIMESTAMP, - ObjectParser.ValueType.VALUE); + ObjectParser.ValueType.VALUE + ); parser.declareLong(ConstructingObjectParser.constructorArg(), PEAK_USAGE_BYTES); parser.declareString(ConstructingObjectParser.optionalConstructorArg(), Status::fromString, STATUS); parser.declareLong(ConstructingObjectParser.optionalConstructorArg(), MEMORY_REESTIMATE_BYTES); @@ -60,7 +65,8 @@ private static ConstructingObjectParser createParser(boolean private final Instant timestamp; private final long peakUsageBytes; private final Status status; - @Nullable private final Long memoryReestimateBytes; + @Nullable + private final Long memoryReestimateBytes; /** * Creates a zero usage object @@ -69,13 +75,19 @@ public MemoryUsage(String jobId) { this(jobId, null, 0, null, null); } - public MemoryUsage(String jobId, Instant timestamp, long peakUsageBytes, @Nullable Status status, - @Nullable Long memoryReestimateBytes) { + public MemoryUsage( + String jobId, + Instant timestamp, + long peakUsageBytes, + @Nullable Status status, + @Nullable Long memoryReestimateBytes + ) { this.jobId = Objects.requireNonNull(jobId); // We intend to store this timestamp in millis granularity. Thus we're rounding here to ensure // internal representation matches toXContent - this.timestamp = timestamp == null ? null : Instant.ofEpochMilli( - ExceptionsHelper.requireNonNull(timestamp, Fields.TIMESTAMP).toEpochMilli()); + this.timestamp = timestamp == null + ? null + : Instant.ofEpochMilli(ExceptionsHelper.requireNonNull(timestamp, Fields.TIMESTAMP).toEpochMilli()); this.peakUsageBytes = peakUsageBytes; this.status = status == null ? Status.OK : status; this.memoryReestimateBytes = memoryReestimateBytes; @@ -114,8 +126,11 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.field(Fields.JOB_ID.getPreferredName(), jobId); } if (timestamp != null) { - builder.timeField(Fields.TIMESTAMP.getPreferredName(), Fields.TIMESTAMP.getPreferredName() + "_string", - timestamp.toEpochMilli()); + builder.timeField( + Fields.TIMESTAMP.getPreferredName(), + Fields.TIMESTAMP.getPreferredName() + "_string", + timestamp.toEpochMilli() + ); } builder.field(PEAK_USAGE_BYTES.getPreferredName(), peakUsageBytes); builder.field(STATUS.getPreferredName(), status); @@ -158,7 +173,7 @@ public static String documentIdPrefix(String jobId) { return TYPE_VALUE + "_" + jobId + "_"; } - public enum Status implements Writeable { + public enum Status implements Writeable { OK, HARD_LIMIT; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/outlierdetection/OutlierDetectionStats.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/outlierdetection/OutlierDetectionStats.java index 9fbcff7a3cca1..b7f6bb7118f68 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/outlierdetection/OutlierDetectionStats.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/outlierdetection/OutlierDetectionStats.java @@ -6,11 +6,11 @@ */ package org.elasticsearch.xpack.core.ml.dataframe.stats.outlierdetection; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.common.time.TimeUtils; import org.elasticsearch.xpack.core.ml.dataframe.stats.AnalysisStats; @@ -33,19 +33,30 @@ public class OutlierDetectionStats implements AnalysisStats { public static final ConstructingObjectParser LENIENT_PARSER = createParser(true); private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { - ConstructingObjectParser parser = new ConstructingObjectParser<>(TYPE_VALUE, ignoreUnknownFields, - a -> new OutlierDetectionStats((String) a[0], (Instant) a[1], (Parameters) a[2], (TimingStats) a[3])); + ConstructingObjectParser parser = new ConstructingObjectParser<>( + TYPE_VALUE, + ignoreUnknownFields, + a -> new OutlierDetectionStats((String) a[0], (Instant) a[1], (Parameters) a[2], (TimingStats) a[3]) + ); parser.declareString((bucket, s) -> {}, Fields.TYPE); parser.declareString(ConstructingObjectParser.constructorArg(), Fields.JOB_ID); - parser.declareField(ConstructingObjectParser.constructorArg(), + parser.declareField( + ConstructingObjectParser.constructorArg(), p -> TimeUtils.parseTimeFieldToInstant(p, Fields.TIMESTAMP.getPreferredName()), Fields.TIMESTAMP, - ObjectParser.ValueType.VALUE); - parser.declareObject(ConstructingObjectParser.constructorArg(), - (p, c) -> Parameters.fromXContent(p, ignoreUnknownFields), PARAMETERS); - parser.declareObject(ConstructingObjectParser.constructorArg(), - (p, c) -> TimingStats.fromXContent(p, ignoreUnknownFields), TIMING_STATS); + ObjectParser.ValueType.VALUE + ); + parser.declareObject( + ConstructingObjectParser.constructorArg(), + (p, c) -> Parameters.fromXContent(p, ignoreUnknownFields), + PARAMETERS + ); + parser.declareObject( + ConstructingObjectParser.constructorArg(), + (p, c) -> TimingStats.fromXContent(p, ignoreUnknownFields), + TIMING_STATS + ); return parser; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/outlierdetection/Parameters.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/outlierdetection/Parameters.java index d4b4724b5f015..aa38f5a982c4a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/outlierdetection/Parameters.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/outlierdetection/Parameters.java @@ -6,11 +6,11 @@ */ package org.elasticsearch.xpack.core.ml.dataframe.stats.outlierdetection; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -34,16 +34,11 @@ public static Parameters fromXContent(XContentParser parser, boolean ignoreUnkno } private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { - ConstructingObjectParser parser = new ConstructingObjectParser<>("outlier_detection_parameters", + ConstructingObjectParser parser = new ConstructingObjectParser<>( + "outlier_detection_parameters", ignoreUnknownFields, - a -> new Parameters( - (int) a[0], - (String) a[1], - (boolean) a[2], - (double) a[3], - (double) a[4], - (boolean) a[5] - )); + a -> new Parameters((int) a[0], (String) a[1], (boolean) a[2], (double) a[3], (double) a[4], (boolean) a[5]) + ); parser.declareInt(constructorArg(), N_NEIGHBORS); parser.declareString(constructorArg(), METHOD); @@ -62,8 +57,14 @@ private static ConstructingObjectParser createParser(boolean i private final double outlierFraction; private final boolean standardizationEnabled; - public Parameters(int nNeighbors, String method, boolean computeFeatureInfluence, double featureInfluenceThreshold, - double outlierFraction, boolean standardizationEnabled) { + public Parameters( + int nNeighbors, + String method, + boolean computeFeatureInfluence, + double featureInfluenceThreshold, + double outlierFraction, + boolean standardizationEnabled + ) { this.nNeighbors = nNeighbors; this.method = method; this.computeFeatureInfluence = computeFeatureInfluence; @@ -120,7 +121,13 @@ public boolean equals(Object o) { @Override public int hashCode() { - return Objects.hash(nNeighbors, method, computeFeatureInfluence, featureInfluenceThreshold, outlierFraction, - standardizationEnabled); + return Objects.hash( + nNeighbors, + method, + computeFeatureInfluence, + featureInfluenceThreshold, + outlierFraction, + standardizationEnabled + ); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/outlierdetection/TimingStats.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/outlierdetection/TimingStats.java index b1ce4f46d2980..f3d90ddbeec63 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/outlierdetection/TimingStats.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/outlierdetection/TimingStats.java @@ -6,12 +6,12 @@ */ package org.elasticsearch.xpack.core.ml.dataframe.stats.outlierdetection; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -28,9 +28,11 @@ public static TimingStats fromXContent(XContentParser parser, boolean ignoreUnkn } private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { - ConstructingObjectParser parser = new ConstructingObjectParser<>("outlier_detection_timing_stats", + ConstructingObjectParser parser = new ConstructingObjectParser<>( + "outlier_detection_timing_stats", ignoreUnknownFields, - a -> new TimingStats(TimeValue.timeValueMillis((long) a[0]))); + a -> new TimingStats(TimeValue.timeValueMillis((long) a[0])) + ); parser.declareLong(ConstructingObjectParser.constructorArg(), ELAPSED_TIME); return parser; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/regression/Hyperparameters.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/regression/Hyperparameters.java index 965df9334217f..3382b2f66d3f2 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/regression/Hyperparameters.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/regression/Hyperparameters.java @@ -6,11 +6,11 @@ */ package org.elasticsearch.xpack.core.ml.dataframe.stats.regression; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -31,7 +31,8 @@ public class Hyperparameters implements ToXContentObject, Writeable { public static final ParseField LAMBDA = new ParseField("lambda"); public static final ParseField MAX_ATTEMPTS_TO_ADD_TREE = new ParseField("max_attempts_to_add_tree"); public static final ParseField MAX_OPTIMIZATION_ROUNDS_PER_HYPERPARAMETER = new ParseField( - "max_optimization_rounds_per_hyperparameter"); + "max_optimization_rounds_per_hyperparameter" + ); public static final ParseField MAX_TREES = new ParseField("max_trees"); public static final ParseField NUM_FOLDS = new ParseField("num_folds"); public static final ParseField NUM_SPLITS_PER_FEATURE = new ParseField("num_splits_per_feature"); @@ -43,7 +44,8 @@ public static Hyperparameters fromXContent(XContentParser parser, boolean ignore } private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { - ConstructingObjectParser parser = new ConstructingObjectParser<>("regression_hyperparameters", + ConstructingObjectParser parser = new ConstructingObjectParser<>( + "regression_hyperparameters", ignoreUnknownFields, a -> new Hyperparameters( (double) a[0], @@ -60,7 +62,8 @@ private static ConstructingObjectParser createParser(bool (int) a[11], (double) a[12], (double) a[13] - )); + ) + ); parser.declareDouble(constructorArg(), ALPHA); parser.declareDouble(constructorArg(), DOWNSAMPLE_FACTOR); @@ -95,20 +98,22 @@ private static ConstructingObjectParser createParser(bool private final double softTreeDepthLimit; private final double softTreeDepthTolerance; - public Hyperparameters(double alpha, - double downsampleFactor, - double eta, - double etaGrowthRatePerTree, - double featureBagFraction, - double gamma, - double lambda, - int maxAttemptsToAddTree, - int maxOptimizationRoundsPerHyperparameter, - int maxTrees, - int numFolds, - int numSplitsPerFeature, - double softTreeDepthLimit, - double softTreeDepthTolerance) { + public Hyperparameters( + double alpha, + double downsampleFactor, + double eta, + double etaGrowthRatePerTree, + double featureBagFraction, + double gamma, + double lambda, + int maxAttemptsToAddTree, + int maxOptimizationRoundsPerHyperparameter, + int maxTrees, + int numFolds, + int numSplitsPerFeature, + double softTreeDepthLimit, + double softTreeDepthTolerance + ) { this.alpha = alpha; this.downsampleFactor = downsampleFactor; this.eta = eta; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/regression/RegressionStats.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/regression/RegressionStats.java index 1c6712b2deefd..8b92ea64283aa 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/regression/RegressionStats.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/regression/RegressionStats.java @@ -6,11 +6,11 @@ */ package org.elasticsearch.xpack.core.ml.dataframe.stats.regression; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.common.time.TimeUtils; import org.elasticsearch.xpack.core.ml.dataframe.stats.AnalysisStats; @@ -35,7 +35,9 @@ public class RegressionStats implements AnalysisStats { public static final ConstructingObjectParser LENIENT_PARSER = createParser(true); private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { - ConstructingObjectParser parser = new ConstructingObjectParser<>(TYPE_VALUE, ignoreUnknownFields, + ConstructingObjectParser parser = new ConstructingObjectParser<>( + TYPE_VALUE, + ignoreUnknownFields, a -> new RegressionStats( (String) a[0], (Instant) a[1], @@ -48,17 +50,28 @@ private static ConstructingObjectParser createParser(bool parser.declareString((bucket, s) -> {}, Fields.TYPE); parser.declareString(ConstructingObjectParser.constructorArg(), Fields.JOB_ID); - parser.declareField(ConstructingObjectParser.constructorArg(), + parser.declareField( + ConstructingObjectParser.constructorArg(), p -> TimeUtils.parseTimeFieldToInstant(p, Fields.TIMESTAMP.getPreferredName()), Fields.TIMESTAMP, - ObjectParser.ValueType.VALUE); + ObjectParser.ValueType.VALUE + ); parser.declareInt(ConstructingObjectParser.constructorArg(), ITERATION); - parser.declareObject(ConstructingObjectParser.constructorArg(), - (p, c) -> Hyperparameters.fromXContent(p, ignoreUnknownFields), HYPERPARAMETERS); - parser.declareObject(ConstructingObjectParser.constructorArg(), - (p, c) -> TimingStats.fromXContent(p, ignoreUnknownFields), TIMING_STATS); - parser.declareObject(ConstructingObjectParser.constructorArg(), - (p, c) -> ValidationLoss.fromXContent(p, ignoreUnknownFields), VALIDATION_LOSS); + parser.declareObject( + ConstructingObjectParser.constructorArg(), + (p, c) -> Hyperparameters.fromXContent(p, ignoreUnknownFields), + HYPERPARAMETERS + ); + parser.declareObject( + ConstructingObjectParser.constructorArg(), + (p, c) -> TimingStats.fromXContent(p, ignoreUnknownFields), + TIMING_STATS + ); + parser.declareObject( + ConstructingObjectParser.constructorArg(), + (p, c) -> ValidationLoss.fromXContent(p, ignoreUnknownFields), + VALIDATION_LOSS + ); return parser; } @@ -69,8 +82,14 @@ private static ConstructingObjectParser createParser(bool private final TimingStats timingStats; private final ValidationLoss validationLoss; - public RegressionStats(String jobId, Instant timestamp, int iteration, Hyperparameters hyperparameters, TimingStats timingStats, - ValidationLoss validationLoss) { + public RegressionStats( + String jobId, + Instant timestamp, + int iteration, + Hyperparameters hyperparameters, + TimingStats timingStats, + ValidationLoss validationLoss + ) { this.jobId = Objects.requireNonNull(jobId); // We intend to store this timestamp in millis granularity. Thus we're rounding here to ensure // internal representation matches toXContent diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/regression/TimingStats.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/regression/TimingStats.java index e6698ea4e651c..a0624e9c97872 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/regression/TimingStats.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/regression/TimingStats.java @@ -6,12 +6,12 @@ */ package org.elasticsearch.xpack.core.ml.dataframe.stats.regression; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -29,8 +29,11 @@ public static TimingStats fromXContent(XContentParser parser, boolean ignoreUnkn } private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { - ConstructingObjectParser parser = new ConstructingObjectParser<>("regression_timing_stats", ignoreUnknownFields, - a -> new TimingStats(TimeValue.timeValueMillis((long) a[0]), TimeValue.timeValueMillis((long) a[1]))); + ConstructingObjectParser parser = new ConstructingObjectParser<>( + "regression_timing_stats", + ignoreUnknownFields, + a -> new TimingStats(TimeValue.timeValueMillis((long) a[0]), TimeValue.timeValueMillis((long) a[1])) + ); parser.declareLong(ConstructingObjectParser.constructorArg(), ELAPSED_TIME); parser.declareLong(ConstructingObjectParser.constructorArg(), ITERATION_TIME); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/regression/ValidationLoss.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/regression/ValidationLoss.java index 8ac67eac610f4..c647c784948fa 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/regression/ValidationLoss.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/dataframe/stats/regression/ValidationLoss.java @@ -6,11 +6,11 @@ */ package org.elasticsearch.xpack.core.ml.dataframe.stats.regression; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -32,13 +32,18 @@ public static ValidationLoss fromXContent(XContentParser parser, boolean ignoreU @SuppressWarnings("unchecked") private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { - ConstructingObjectParser parser = new ConstructingObjectParser<>("regression_validation_loss", - ignoreUnknownFields, - a -> new ValidationLoss((String) a[0], (List) a[1])); + ConstructingObjectParser parser = new ConstructingObjectParser<>( + "regression_validation_loss", + ignoreUnknownFields, + a -> new ValidationLoss((String) a[0], (List) a[1]) + ); parser.declareString(ConstructingObjectParser.constructorArg(), LOSS_TYPE); - parser.declareObjectArray(ConstructingObjectParser.constructorArg(), - (p, c) -> FoldValues.fromXContent(p, ignoreUnknownFields), FOLD_VALUES); + parser.declareObjectArray( + ConstructingObjectParser.constructorArg(), + (p, c) -> FoldValues.fromXContent(p, ignoreUnknownFields), + FOLD_VALUES + ); return parser; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/InferenceToXContentCompressor.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/InferenceToXContentCompressor.java index c488386441432..d7b9e7c734585 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/InferenceToXContentCompressor.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/InferenceToXContentCompressor.java @@ -8,21 +8,21 @@ package org.elasticsearch.xpack.core.ml.inference; import org.elasticsearch.ExceptionsHelper; -import org.elasticsearch.core.CheckedFunction; import org.elasticsearch.common.breaker.CircuitBreaker; import org.elasticsearch.common.breaker.CircuitBreakingException; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.core.CheckedFunction; +import org.elasticsearch.monitor.jvm.JvmInfo; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ToXContentObject; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentParseException; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.monitor.jvm.JvmInfo; import org.elasticsearch.xpack.core.ml.inference.utils.SimpleBoundedInputStream; import java.io.IOException; @@ -39,8 +39,9 @@ public final class InferenceToXContentCompressor { private static final int BUFFER_SIZE = 4096; // Either 25% of the configured JVM heap, or 1 GB, which ever is smaller private static final long MAX_INFLATED_BYTES = Math.min( - (long)((0.25) * JvmInfo.jvmInfo().getMem().getHeapMax().getBytes()), - ByteSizeValue.ofGb(1).getBytes()); + (long) ((0.25) * JvmInfo.jvmInfo().getMem().getHeapMax().getBytes()), + ByteSizeValue.ofGb(1).getBytes() + ); private InferenceToXContentCompressor() {} @@ -49,36 +50,52 @@ public static BytesReference deflate(T objectToComp return deflate(reference); } - public static T inflateUnsafe(BytesReference compressedBytes, - CheckedFunction parserFunction, - NamedXContentRegistry xContentRegistry) throws IOException { + public static T inflateUnsafe( + BytesReference compressedBytes, + CheckedFunction parserFunction, + NamedXContentRegistry xContentRegistry + ) throws IOException { return inflate(compressedBytes, parserFunction, xContentRegistry, Long.MAX_VALUE); } - public static T inflate(BytesReference compressedBytes, - CheckedFunction parserFunction, - NamedXContentRegistry xContentRegistry) throws IOException { + public static T inflate( + BytesReference compressedBytes, + CheckedFunction parserFunction, + NamedXContentRegistry xContentRegistry + ) throws IOException { return inflate(compressedBytes, parserFunction, xContentRegistry, MAX_INFLATED_BYTES); } - static T inflate(BytesReference compressedBytes, - CheckedFunction parserFunction, - NamedXContentRegistry xContentRegistry, - long maxBytes) throws IOException { - try(XContentParser parser = JsonXContent.jsonXContent.createParser(xContentRegistry, - LoggingDeprecationHandler.INSTANCE, - inflate(compressedBytes, maxBytes))) { + static T inflate( + BytesReference compressedBytes, + CheckedFunction parserFunction, + NamedXContentRegistry xContentRegistry, + long maxBytes + ) throws IOException { + try ( + XContentParser parser = JsonXContent.jsonXContent.createParser( + xContentRegistry, + LoggingDeprecationHandler.INSTANCE, + inflate(compressedBytes, maxBytes) + ) + ) { return parserFunction.apply(parser); } catch (XContentParseException parseException) { SimpleBoundedInputStream.StreamSizeExceededException streamSizeCause = - (SimpleBoundedInputStream.StreamSizeExceededException) - ExceptionsHelper.unwrap(parseException, SimpleBoundedInputStream.StreamSizeExceededException.class); + (SimpleBoundedInputStream.StreamSizeExceededException) ExceptionsHelper.unwrap( + parseException, + SimpleBoundedInputStream.StreamSizeExceededException.class + ); if (streamSizeCause != null) { // The root cause is that the model is too big. - throw new CircuitBreakingException("Cannot parse model definition as the content is larger than the maximum stream size " + - "of [" + streamSizeCause.getMaxBytes() + "] bytes. Max stream size is 10% of the JVM heap or 1GB whichever is smallest", - CircuitBreaker.Durability.PERMANENT); + throw new CircuitBreakingException( + "Cannot parse model definition as the content is larger than the maximum stream size " + + "of [" + + streamSizeCause.getMaxBytes() + + "] bytes. Max stream size is 10% of the JVM heap or 1GB whichever is smallest", + CircuitBreaker.Durability.PERMANENT + ); } else { throw parseException; } @@ -87,9 +104,13 @@ static T inflate(BytesReference compressedBytes, static Map inflateToMap(BytesReference compressedBytes) throws IOException { // Don't need the xcontent registry as we are not deflating named objects. - try(XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, - LoggingDeprecationHandler.INSTANCE, - inflate(compressedBytes, MAX_INFLATED_BYTES))) { + try ( + XContentParser parser = JsonXContent.jsonXContent.createParser( + NamedXContentRegistry.EMPTY, + LoggingDeprecationHandler.INSTANCE, + inflate(compressedBytes, MAX_INFLATED_BYTES) + ) + ) { return parser.mapOrdered(); } } @@ -98,8 +119,10 @@ static InputStream inflate(BytesReference compressedBytes, long streamSize) thro // If the compressed length is already too large, it make sense that the inflated length would be as well // In the extremely small string case, the compressed data could actually be longer than the compressed stream if (compressedBytes.length() > Math.max(100L, streamSize)) { - throw new CircuitBreakingException("compressed stream is longer than maximum allowed bytes [" + streamSize + "]", - CircuitBreaker.Durability.PERMANENT); + throw new CircuitBreakingException( + "compressed stream is longer than maximum allowed bytes [" + streamSize + "]", + CircuitBreaker.Durability.PERMANENT + ); } InputStream gzipStream = new GZIPInputStream(compressedBytes.streamInput(), BUFFER_SIZE); return new SimpleBoundedInputStream(gzipStream, streamSize); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/MlInferenceNamedXContentProvider.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/MlInferenceNamedXContentProvider.java index 823b38c486664..521aec151e503 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/MlInferenceNamedXContentProvider.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/MlInferenceNamedXContentProvider.java @@ -7,9 +7,9 @@ package org.elasticsearch.xpack.core.ml.inference; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; +import org.elasticsearch.plugins.spi.NamedXContentProvider; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.plugins.spi.NamedXContentProvider; import org.elasticsearch.xpack.core.ml.inference.preprocessing.CustomWordEmbedding; import org.elasticsearch.xpack.core.ml.inference.preprocessing.FrequencyEncoding; import org.elasticsearch.xpack.core.ml.inference.preprocessing.LenientlyParsedPreProcessor; @@ -82,136 +82,335 @@ public List getNamedXContentParsers() { List namedXContent = new ArrayList<>(); // PreProcessing Lenient - namedXContent.add(new NamedXContentRegistry.Entry(LenientlyParsedPreProcessor.class, OneHotEncoding.NAME, - (p, c) -> OneHotEncoding.fromXContentLenient(p, (PreProcessor.PreProcessorParseContext) c))); - namedXContent.add(new NamedXContentRegistry.Entry(LenientlyParsedPreProcessor.class, TargetMeanEncoding.NAME, - (p, c) -> TargetMeanEncoding.fromXContentLenient(p, (PreProcessor.PreProcessorParseContext) c))); - namedXContent.add(new NamedXContentRegistry.Entry(LenientlyParsedPreProcessor.class, FrequencyEncoding.NAME, - (p, c) -> FrequencyEncoding.fromXContentLenient(p, (PreProcessor.PreProcessorParseContext) c))); - namedXContent.add(new NamedXContentRegistry.Entry(LenientlyParsedPreProcessor.class, CustomWordEmbedding.NAME, - (p, c) -> CustomWordEmbedding.fromXContentLenient(p))); - namedXContent.add(new NamedXContentRegistry.Entry(LenientlyParsedPreProcessor.class, NGram.NAME, - (p, c) -> NGram.fromXContentLenient(p, (PreProcessor.PreProcessorParseContext) c))); - namedXContent.add(new NamedXContentRegistry.Entry(LenientlyParsedPreProcessor.class, Multi.NAME, - (p, c) -> Multi.fromXContentLenient(p, (PreProcessor.PreProcessorParseContext) c))); + namedXContent.add( + new NamedXContentRegistry.Entry( + LenientlyParsedPreProcessor.class, + OneHotEncoding.NAME, + (p, c) -> OneHotEncoding.fromXContentLenient(p, (PreProcessor.PreProcessorParseContext) c) + ) + ); + namedXContent.add( + new NamedXContentRegistry.Entry( + LenientlyParsedPreProcessor.class, + TargetMeanEncoding.NAME, + (p, c) -> TargetMeanEncoding.fromXContentLenient(p, (PreProcessor.PreProcessorParseContext) c) + ) + ); + namedXContent.add( + new NamedXContentRegistry.Entry( + LenientlyParsedPreProcessor.class, + FrequencyEncoding.NAME, + (p, c) -> FrequencyEncoding.fromXContentLenient(p, (PreProcessor.PreProcessorParseContext) c) + ) + ); + namedXContent.add( + new NamedXContentRegistry.Entry( + LenientlyParsedPreProcessor.class, + CustomWordEmbedding.NAME, + (p, c) -> CustomWordEmbedding.fromXContentLenient(p) + ) + ); + namedXContent.add( + new NamedXContentRegistry.Entry( + LenientlyParsedPreProcessor.class, + NGram.NAME, + (p, c) -> NGram.fromXContentLenient(p, (PreProcessor.PreProcessorParseContext) c) + ) + ); + namedXContent.add( + new NamedXContentRegistry.Entry( + LenientlyParsedPreProcessor.class, + Multi.NAME, + (p, c) -> Multi.fromXContentLenient(p, (PreProcessor.PreProcessorParseContext) c) + ) + ); // PreProcessing Strict - namedXContent.add(new NamedXContentRegistry.Entry(StrictlyParsedPreProcessor.class, OneHotEncoding.NAME, - (p, c) -> OneHotEncoding.fromXContentStrict(p, (PreProcessor.PreProcessorParseContext) c))); - namedXContent.add(new NamedXContentRegistry.Entry(StrictlyParsedPreProcessor.class, TargetMeanEncoding.NAME, - (p, c) -> TargetMeanEncoding.fromXContentStrict(p, (PreProcessor.PreProcessorParseContext) c))); - namedXContent.add(new NamedXContentRegistry.Entry(StrictlyParsedPreProcessor.class, FrequencyEncoding.NAME, - (p, c) -> FrequencyEncoding.fromXContentStrict(p, (PreProcessor.PreProcessorParseContext) c))); - namedXContent.add(new NamedXContentRegistry.Entry(StrictlyParsedPreProcessor.class, CustomWordEmbedding.NAME, - (p, c) -> CustomWordEmbedding.fromXContentStrict(p))); - namedXContent.add(new NamedXContentRegistry.Entry(StrictlyParsedPreProcessor.class, NGram.NAME, - (p, c) -> NGram.fromXContentStrict(p, (PreProcessor.PreProcessorParseContext) c))); - namedXContent.add(new NamedXContentRegistry.Entry(StrictlyParsedPreProcessor.class, Multi.NAME, - (p, c) -> Multi.fromXContentStrict(p, (PreProcessor.PreProcessorParseContext) c))); + namedXContent.add( + new NamedXContentRegistry.Entry( + StrictlyParsedPreProcessor.class, + OneHotEncoding.NAME, + (p, c) -> OneHotEncoding.fromXContentStrict(p, (PreProcessor.PreProcessorParseContext) c) + ) + ); + namedXContent.add( + new NamedXContentRegistry.Entry( + StrictlyParsedPreProcessor.class, + TargetMeanEncoding.NAME, + (p, c) -> TargetMeanEncoding.fromXContentStrict(p, (PreProcessor.PreProcessorParseContext) c) + ) + ); + namedXContent.add( + new NamedXContentRegistry.Entry( + StrictlyParsedPreProcessor.class, + FrequencyEncoding.NAME, + (p, c) -> FrequencyEncoding.fromXContentStrict(p, (PreProcessor.PreProcessorParseContext) c) + ) + ); + namedXContent.add( + new NamedXContentRegistry.Entry( + StrictlyParsedPreProcessor.class, + CustomWordEmbedding.NAME, + (p, c) -> CustomWordEmbedding.fromXContentStrict(p) + ) + ); + namedXContent.add( + new NamedXContentRegistry.Entry( + StrictlyParsedPreProcessor.class, + NGram.NAME, + (p, c) -> NGram.fromXContentStrict(p, (PreProcessor.PreProcessorParseContext) c) + ) + ); + namedXContent.add( + new NamedXContentRegistry.Entry( + StrictlyParsedPreProcessor.class, + Multi.NAME, + (p, c) -> Multi.fromXContentStrict(p, (PreProcessor.PreProcessorParseContext) c) + ) + ); // Model Lenient namedXContent.add(new NamedXContentRegistry.Entry(LenientlyParsedTrainedModel.class, Tree.NAME, Tree::fromXContentLenient)); namedXContent.add(new NamedXContentRegistry.Entry(LenientlyParsedTrainedModel.class, Ensemble.NAME, Ensemble::fromXContentLenient)); - namedXContent.add(new NamedXContentRegistry.Entry(LenientlyParsedTrainedModel.class, - LangIdentNeuralNetwork.NAME, - LangIdentNeuralNetwork::fromXContentLenient)); + namedXContent.add( + new NamedXContentRegistry.Entry( + LenientlyParsedTrainedModel.class, + LangIdentNeuralNetwork.NAME, + LangIdentNeuralNetwork::fromXContentLenient + ) + ); // Output Aggregator Lenient - namedXContent.add(new NamedXContentRegistry.Entry(LenientlyParsedOutputAggregator.class, - WeightedMode.NAME, - WeightedMode::fromXContentLenient)); - namedXContent.add(new NamedXContentRegistry.Entry(LenientlyParsedOutputAggregator.class, - WeightedSum.NAME, - WeightedSum::fromXContentLenient)); - namedXContent.add(new NamedXContentRegistry.Entry(LenientlyParsedOutputAggregator.class, - LogisticRegression.NAME, - LogisticRegression::fromXContentLenient)); - namedXContent.add(new NamedXContentRegistry.Entry(LenientlyParsedOutputAggregator.class, - Exponent.NAME, - Exponent::fromXContentLenient)); + namedXContent.add( + new NamedXContentRegistry.Entry(LenientlyParsedOutputAggregator.class, WeightedMode.NAME, WeightedMode::fromXContentLenient) + ); + namedXContent.add( + new NamedXContentRegistry.Entry(LenientlyParsedOutputAggregator.class, WeightedSum.NAME, WeightedSum::fromXContentLenient) + ); + namedXContent.add( + new NamedXContentRegistry.Entry( + LenientlyParsedOutputAggregator.class, + LogisticRegression.NAME, + LogisticRegression::fromXContentLenient + ) + ); + namedXContent.add( + new NamedXContentRegistry.Entry(LenientlyParsedOutputAggregator.class, Exponent.NAME, Exponent::fromXContentLenient) + ); // Model Strict namedXContent.add(new NamedXContentRegistry.Entry(StrictlyParsedTrainedModel.class, Tree.NAME, Tree::fromXContentStrict)); namedXContent.add(new NamedXContentRegistry.Entry(StrictlyParsedTrainedModel.class, Ensemble.NAME, Ensemble::fromXContentStrict)); - namedXContent.add(new NamedXContentRegistry.Entry(StrictlyParsedTrainedModel.class, - LangIdentNeuralNetwork.NAME, - LangIdentNeuralNetwork::fromXContentStrict)); + namedXContent.add( + new NamedXContentRegistry.Entry( + StrictlyParsedTrainedModel.class, + LangIdentNeuralNetwork.NAME, + LangIdentNeuralNetwork::fromXContentStrict + ) + ); // Output Aggregator Strict - namedXContent.add(new NamedXContentRegistry.Entry(StrictlyParsedOutputAggregator.class, - WeightedMode.NAME, - WeightedMode::fromXContentStrict)); - namedXContent.add(new NamedXContentRegistry.Entry(StrictlyParsedOutputAggregator.class, - WeightedSum.NAME, - WeightedSum::fromXContentStrict)); - namedXContent.add(new NamedXContentRegistry.Entry(StrictlyParsedOutputAggregator.class, - LogisticRegression.NAME, - LogisticRegression::fromXContentStrict)); - namedXContent.add(new NamedXContentRegistry.Entry(StrictlyParsedOutputAggregator.class, - Exponent.NAME, - Exponent::fromXContentStrict)); + namedXContent.add( + new NamedXContentRegistry.Entry(StrictlyParsedOutputAggregator.class, WeightedMode.NAME, WeightedMode::fromXContentStrict) + ); + namedXContent.add( + new NamedXContentRegistry.Entry(StrictlyParsedOutputAggregator.class, WeightedSum.NAME, WeightedSum::fromXContentStrict) + ); + namedXContent.add( + new NamedXContentRegistry.Entry( + StrictlyParsedOutputAggregator.class, + LogisticRegression.NAME, + LogisticRegression::fromXContentStrict + ) + ); + namedXContent.add( + new NamedXContentRegistry.Entry(StrictlyParsedOutputAggregator.class, Exponent.NAME, Exponent::fromXContentStrict) + ); // Location lenient - namedXContent.add(new NamedXContentRegistry.Entry(LenientlyParsedTrainedModelLocation.class, - IndexLocation.INDEX, - IndexLocation::fromXContentLenient)); + namedXContent.add( + new NamedXContentRegistry.Entry( + LenientlyParsedTrainedModelLocation.class, + IndexLocation.INDEX, + IndexLocation::fromXContentLenient + ) + ); // Location strict - namedXContent.add(new NamedXContentRegistry.Entry(StrictlyParsedTrainedModelLocation.class, - IndexLocation.INDEX, - IndexLocation::fromXContentStrict)); + namedXContent.add( + new NamedXContentRegistry.Entry( + StrictlyParsedTrainedModelLocation.class, + IndexLocation.INDEX, + IndexLocation::fromXContentStrict + ) + ); // Inference Configs - namedXContent.add(new NamedXContentRegistry.Entry(LenientlyParsedInferenceConfig.class, ClassificationConfig.NAME, - ClassificationConfig::fromXContentLenient)); - namedXContent.add(new NamedXContentRegistry.Entry(StrictlyParsedInferenceConfig.class, ClassificationConfig.NAME, - ClassificationConfig::fromXContentStrict)); - namedXContent.add(new NamedXContentRegistry.Entry(LenientlyParsedInferenceConfig.class, RegressionConfig.NAME, - RegressionConfig::fromXContentLenient)); - namedXContent.add(new NamedXContentRegistry.Entry(StrictlyParsedInferenceConfig.class, RegressionConfig.NAME, - RegressionConfig::fromXContentStrict)); - namedXContent.add(new NamedXContentRegistry.Entry(LenientlyParsedInferenceConfig.class, new ParseField(NerConfig.NAME), - NerConfig::fromXContentLenient)); - namedXContent.add(new NamedXContentRegistry.Entry(StrictlyParsedInferenceConfig.class, new ParseField(NerConfig.NAME), - NerConfig::fromXContentStrict)); - namedXContent.add(new NamedXContentRegistry.Entry(LenientlyParsedInferenceConfig.class, new ParseField(FillMaskConfig.NAME), - FillMaskConfig::fromXContentLenient)); - namedXContent.add(new NamedXContentRegistry.Entry(StrictlyParsedInferenceConfig.class, new ParseField(FillMaskConfig.NAME), - FillMaskConfig::fromXContentStrict)); - namedXContent.add(new NamedXContentRegistry.Entry(LenientlyParsedInferenceConfig.class, - new ParseField(TextClassificationConfig.NAME), TextClassificationConfig::fromXContentLenient)); - namedXContent.add(new NamedXContentRegistry.Entry(StrictlyParsedInferenceConfig.class, - new ParseField(TextClassificationConfig.NAME), TextClassificationConfig::fromXContentStrict)); - namedXContent.add(new NamedXContentRegistry.Entry(LenientlyParsedInferenceConfig.class, - new ParseField(PassThroughConfig.NAME), PassThroughConfig::fromXContentLenient)); - namedXContent.add(new NamedXContentRegistry.Entry(StrictlyParsedInferenceConfig.class, new ParseField(PassThroughConfig.NAME), - PassThroughConfig::fromXContentStrict)); - namedXContent.add(new NamedXContentRegistry.Entry(LenientlyParsedInferenceConfig.class, - new ParseField(TextEmbeddingConfig.NAME), TextEmbeddingConfig::fromXContentLenient)); - namedXContent.add(new NamedXContentRegistry.Entry(StrictlyParsedInferenceConfig.class, new ParseField(TextEmbeddingConfig.NAME), - TextEmbeddingConfig::fromXContentStrict)); - namedXContent.add(new NamedXContentRegistry.Entry(LenientlyParsedInferenceConfig.class, - new ParseField(ZeroShotClassificationConfig.NAME), ZeroShotClassificationConfig::fromXContentLenient)); - namedXContent.add(new NamedXContentRegistry.Entry(StrictlyParsedInferenceConfig.class, - new ParseField(ZeroShotClassificationConfig.NAME), - ZeroShotClassificationConfig::fromXContentStrict)); + namedXContent.add( + new NamedXContentRegistry.Entry( + LenientlyParsedInferenceConfig.class, + ClassificationConfig.NAME, + ClassificationConfig::fromXContentLenient + ) + ); + namedXContent.add( + new NamedXContentRegistry.Entry( + StrictlyParsedInferenceConfig.class, + ClassificationConfig.NAME, + ClassificationConfig::fromXContentStrict + ) + ); + namedXContent.add( + new NamedXContentRegistry.Entry( + LenientlyParsedInferenceConfig.class, + RegressionConfig.NAME, + RegressionConfig::fromXContentLenient + ) + ); + namedXContent.add( + new NamedXContentRegistry.Entry( + StrictlyParsedInferenceConfig.class, + RegressionConfig.NAME, + RegressionConfig::fromXContentStrict + ) + ); + namedXContent.add( + new NamedXContentRegistry.Entry( + LenientlyParsedInferenceConfig.class, + new ParseField(NerConfig.NAME), + NerConfig::fromXContentLenient + ) + ); + namedXContent.add( + new NamedXContentRegistry.Entry( + StrictlyParsedInferenceConfig.class, + new ParseField(NerConfig.NAME), + NerConfig::fromXContentStrict + ) + ); + namedXContent.add( + new NamedXContentRegistry.Entry( + LenientlyParsedInferenceConfig.class, + new ParseField(FillMaskConfig.NAME), + FillMaskConfig::fromXContentLenient + ) + ); + namedXContent.add( + new NamedXContentRegistry.Entry( + StrictlyParsedInferenceConfig.class, + new ParseField(FillMaskConfig.NAME), + FillMaskConfig::fromXContentStrict + ) + ); + namedXContent.add( + new NamedXContentRegistry.Entry( + LenientlyParsedInferenceConfig.class, + new ParseField(TextClassificationConfig.NAME), + TextClassificationConfig::fromXContentLenient + ) + ); + namedXContent.add( + new NamedXContentRegistry.Entry( + StrictlyParsedInferenceConfig.class, + new ParseField(TextClassificationConfig.NAME), + TextClassificationConfig::fromXContentStrict + ) + ); + namedXContent.add( + new NamedXContentRegistry.Entry( + LenientlyParsedInferenceConfig.class, + new ParseField(PassThroughConfig.NAME), + PassThroughConfig::fromXContentLenient + ) + ); + namedXContent.add( + new NamedXContentRegistry.Entry( + StrictlyParsedInferenceConfig.class, + new ParseField(PassThroughConfig.NAME), + PassThroughConfig::fromXContentStrict + ) + ); + namedXContent.add( + new NamedXContentRegistry.Entry( + LenientlyParsedInferenceConfig.class, + new ParseField(TextEmbeddingConfig.NAME), + TextEmbeddingConfig::fromXContentLenient + ) + ); + namedXContent.add( + new NamedXContentRegistry.Entry( + StrictlyParsedInferenceConfig.class, + new ParseField(TextEmbeddingConfig.NAME), + TextEmbeddingConfig::fromXContentStrict + ) + ); + namedXContent.add( + new NamedXContentRegistry.Entry( + LenientlyParsedInferenceConfig.class, + new ParseField(ZeroShotClassificationConfig.NAME), + ZeroShotClassificationConfig::fromXContentLenient + ) + ); + namedXContent.add( + new NamedXContentRegistry.Entry( + StrictlyParsedInferenceConfig.class, + new ParseField(ZeroShotClassificationConfig.NAME), + ZeroShotClassificationConfig::fromXContentStrict + ) + ); // Inference Configs Update - namedXContent.add(new NamedXContentRegistry.Entry(InferenceConfigUpdate.class, ClassificationConfigUpdate.NAME, - ClassificationConfigUpdate::fromXContentStrict)); - namedXContent.add(new NamedXContentRegistry.Entry(InferenceConfigUpdate.class, new ParseField(FillMaskConfigUpdate.NAME), - FillMaskConfigUpdate::fromXContentStrict)); - namedXContent.add(new NamedXContentRegistry.Entry(InferenceConfigUpdate.class, new ParseField(NerConfigUpdate.NAME), - NerConfigUpdate::fromXContentStrict)); - namedXContent.add(new NamedXContentRegistry.Entry(InferenceConfigUpdate.class, new ParseField(PassThroughConfigUpdate.NAME), - PassThroughConfigUpdate::fromXContentStrict)); - namedXContent.add(new NamedXContentRegistry.Entry(InferenceConfigUpdate.class, RegressionConfigUpdate.NAME, - RegressionConfigUpdate::fromXContentStrict)); - namedXContent.add(new NamedXContentRegistry.Entry(InferenceConfigUpdate.class, new ParseField(TextClassificationConfig.NAME), - TextClassificationConfigUpdate::fromXContentStrict)); - namedXContent.add(new NamedXContentRegistry.Entry(InferenceConfigUpdate.class, new ParseField(TextEmbeddingConfigUpdate.NAME), - TextEmbeddingConfigUpdate::fromXContentStrict)); - namedXContent.add(new NamedXContentRegistry.Entry( + namedXContent.add( + new NamedXContentRegistry.Entry( + InferenceConfigUpdate.class, + ClassificationConfigUpdate.NAME, + ClassificationConfigUpdate::fromXContentStrict + ) + ); + namedXContent.add( + new NamedXContentRegistry.Entry( + InferenceConfigUpdate.class, + new ParseField(FillMaskConfigUpdate.NAME), + FillMaskConfigUpdate::fromXContentStrict + ) + ); + namedXContent.add( + new NamedXContentRegistry.Entry( + InferenceConfigUpdate.class, + new ParseField(NerConfigUpdate.NAME), + NerConfigUpdate::fromXContentStrict + ) + ); + namedXContent.add( + new NamedXContentRegistry.Entry( + InferenceConfigUpdate.class, + new ParseField(PassThroughConfigUpdate.NAME), + PassThroughConfigUpdate::fromXContentStrict + ) + ); + namedXContent.add( + new NamedXContentRegistry.Entry( + InferenceConfigUpdate.class, + RegressionConfigUpdate.NAME, + RegressionConfigUpdate::fromXContentStrict + ) + ); + namedXContent.add( + new NamedXContentRegistry.Entry( + InferenceConfigUpdate.class, + new ParseField(TextClassificationConfig.NAME), + TextClassificationConfigUpdate::fromXContentStrict + ) + ); + namedXContent.add( + new NamedXContentRegistry.Entry( + InferenceConfigUpdate.class, + new ParseField(TextEmbeddingConfigUpdate.NAME), + TextEmbeddingConfigUpdate::fromXContentStrict + ) + ); + namedXContent.add( + new NamedXContentRegistry.Entry( InferenceConfigUpdate.class, new ParseField(ZeroShotClassificationConfigUpdate.NAME), ZeroShotClassificationConfigUpdate::fromXContentStrict @@ -221,9 +420,9 @@ public List getNamedXContentParsers() { // Inference models namedXContent.add(new NamedXContentRegistry.Entry(InferenceModel.class, Ensemble.NAME, EnsembleInferenceModel::fromXContent)); namedXContent.add(new NamedXContentRegistry.Entry(InferenceModel.class, Tree.NAME, TreeInferenceModel::fromXContent)); - namedXContent.add(new NamedXContentRegistry.Entry(InferenceModel.class, - LangIdentNeuralNetwork.NAME, - LangIdentNeuralNetwork::fromXContentLenient)); + namedXContent.add( + new NamedXContentRegistry.Entry(InferenceModel.class, LangIdentNeuralNetwork.NAME, LangIdentNeuralNetwork::fromXContentLenient) + ); // Tokenization namedXContent.add( @@ -241,114 +440,139 @@ public List getNamedWriteables() { List namedWriteables = new ArrayList<>(); // PreProcessing - namedWriteables.add(new NamedWriteableRegistry.Entry(PreProcessor.class, OneHotEncoding.NAME.getPreferredName(), - OneHotEncoding::new)); - namedWriteables.add(new NamedWriteableRegistry.Entry(PreProcessor.class, TargetMeanEncoding.NAME.getPreferredName(), - TargetMeanEncoding::new)); - namedWriteables.add(new NamedWriteableRegistry.Entry(PreProcessor.class, FrequencyEncoding.NAME.getPreferredName(), - FrequencyEncoding::new)); - namedWriteables.add(new NamedWriteableRegistry.Entry(PreProcessor.class, CustomWordEmbedding.NAME.getPreferredName(), - CustomWordEmbedding::new)); - namedWriteables.add(new NamedWriteableRegistry.Entry(PreProcessor.class, NGram.NAME.getPreferredName(), - NGram::new)); - namedWriteables.add(new NamedWriteableRegistry.Entry(PreProcessor.class, Multi.NAME.getPreferredName(), - Multi::new)); + namedWriteables.add( + new NamedWriteableRegistry.Entry(PreProcessor.class, OneHotEncoding.NAME.getPreferredName(), OneHotEncoding::new) + ); + namedWriteables.add( + new NamedWriteableRegistry.Entry(PreProcessor.class, TargetMeanEncoding.NAME.getPreferredName(), TargetMeanEncoding::new) + ); + namedWriteables.add( + new NamedWriteableRegistry.Entry(PreProcessor.class, FrequencyEncoding.NAME.getPreferredName(), FrequencyEncoding::new) + ); + namedWriteables.add( + new NamedWriteableRegistry.Entry(PreProcessor.class, CustomWordEmbedding.NAME.getPreferredName(), CustomWordEmbedding::new) + ); + namedWriteables.add(new NamedWriteableRegistry.Entry(PreProcessor.class, NGram.NAME.getPreferredName(), NGram::new)); + namedWriteables.add(new NamedWriteableRegistry.Entry(PreProcessor.class, Multi.NAME.getPreferredName(), Multi::new)); // Model namedWriteables.add(new NamedWriteableRegistry.Entry(TrainedModel.class, Tree.NAME.getPreferredName(), Tree::new)); namedWriteables.add(new NamedWriteableRegistry.Entry(TrainedModel.class, Ensemble.NAME.getPreferredName(), Ensemble::new)); - namedWriteables.add(new NamedWriteableRegistry.Entry(TrainedModel.class, - LangIdentNeuralNetwork.NAME.getPreferredName(), - LangIdentNeuralNetwork::new)); + namedWriteables.add( + new NamedWriteableRegistry.Entry( + TrainedModel.class, + LangIdentNeuralNetwork.NAME.getPreferredName(), + LangIdentNeuralNetwork::new + ) + ); // Output Aggregator - namedWriteables.add(new NamedWriteableRegistry.Entry(OutputAggregator.class, - WeightedSum.NAME.getPreferredName(), - WeightedSum::new)); - namedWriteables.add(new NamedWriteableRegistry.Entry(OutputAggregator.class, - WeightedMode.NAME.getPreferredName(), - WeightedMode::new)); - namedWriteables.add(new NamedWriteableRegistry.Entry(OutputAggregator.class, - LogisticRegression.NAME.getPreferredName(), - LogisticRegression::new)); - namedWriteables.add(new NamedWriteableRegistry.Entry(OutputAggregator.class, - Exponent.NAME.getPreferredName(), - Exponent::new)); + namedWriteables.add( + new NamedWriteableRegistry.Entry(OutputAggregator.class, WeightedSum.NAME.getPreferredName(), WeightedSum::new) + ); + namedWriteables.add( + new NamedWriteableRegistry.Entry(OutputAggregator.class, WeightedMode.NAME.getPreferredName(), WeightedMode::new) + ); + namedWriteables.add( + new NamedWriteableRegistry.Entry(OutputAggregator.class, LogisticRegression.NAME.getPreferredName(), LogisticRegression::new) + ); + namedWriteables.add(new NamedWriteableRegistry.Entry(OutputAggregator.class, Exponent.NAME.getPreferredName(), Exponent::new)); // Inference Results - namedWriteables.add(new NamedWriteableRegistry.Entry(InferenceResults.class, - ClassificationInferenceResults.NAME, - ClassificationInferenceResults::new)); - namedWriteables.add(new NamedWriteableRegistry.Entry(InferenceResults.class, - RegressionInferenceResults.NAME, - RegressionInferenceResults::new)); - namedWriteables.add(new NamedWriteableRegistry.Entry(InferenceResults.class, - WarningInferenceResults.NAME, - WarningInferenceResults::new)); - namedWriteables.add(new NamedWriteableRegistry.Entry(InferenceResults.class, - NerResults.NAME, - NerResults::new)); - namedWriteables.add(new NamedWriteableRegistry.Entry(InferenceResults.class, - FillMaskResults.NAME, - FillMaskResults::new)); - namedWriteables.add(new NamedWriteableRegistry.Entry(InferenceResults.class, - PyTorchPassThroughResults.NAME, - PyTorchPassThroughResults::new)); - namedWriteables.add(new NamedWriteableRegistry.Entry(InferenceResults.class, - TextEmbeddingResults.NAME, - TextEmbeddingResults::new)); + namedWriteables.add( + new NamedWriteableRegistry.Entry( + InferenceResults.class, + ClassificationInferenceResults.NAME, + ClassificationInferenceResults::new + ) + ); + namedWriteables.add( + new NamedWriteableRegistry.Entry(InferenceResults.class, RegressionInferenceResults.NAME, RegressionInferenceResults::new) + ); + namedWriteables.add( + new NamedWriteableRegistry.Entry(InferenceResults.class, WarningInferenceResults.NAME, WarningInferenceResults::new) + ); + namedWriteables.add(new NamedWriteableRegistry.Entry(InferenceResults.class, NerResults.NAME, NerResults::new)); + namedWriteables.add(new NamedWriteableRegistry.Entry(InferenceResults.class, FillMaskResults.NAME, FillMaskResults::new)); + namedWriteables.add( + new NamedWriteableRegistry.Entry(InferenceResults.class, PyTorchPassThroughResults.NAME, PyTorchPassThroughResults::new) + ); + namedWriteables.add(new NamedWriteableRegistry.Entry(InferenceResults.class, TextEmbeddingResults.NAME, TextEmbeddingResults::new)); // Inference Configs - namedWriteables.add(new NamedWriteableRegistry.Entry(InferenceConfig.class, - ClassificationConfig.NAME.getPreferredName(), ClassificationConfig::new)); - namedWriteables.add(new NamedWriteableRegistry.Entry(InferenceConfig.class, - RegressionConfig.NAME.getPreferredName(), RegressionConfig::new)); - namedWriteables.add(new NamedWriteableRegistry.Entry(InferenceConfig.class, - NerConfig.NAME, NerConfig::new)); - namedWriteables.add(new NamedWriteableRegistry.Entry(InferenceConfig.class, - FillMaskConfig.NAME, FillMaskConfig::new)); - namedWriteables.add(new NamedWriteableRegistry.Entry(InferenceConfig.class, - TextClassificationConfig.NAME, TextClassificationConfig::new)); - namedWriteables.add(new NamedWriteableRegistry.Entry(InferenceConfig.class, - PassThroughConfig.NAME, PassThroughConfig::new)); - namedWriteables.add(new NamedWriteableRegistry.Entry(InferenceConfig.class, - TextEmbeddingConfig.NAME, TextEmbeddingConfig::new)); - namedWriteables.add(new NamedWriteableRegistry.Entry(InferenceConfig.class, - ZeroShotClassificationConfig.NAME, ZeroShotClassificationConfig::new)); + namedWriteables.add( + new NamedWriteableRegistry.Entry(InferenceConfig.class, ClassificationConfig.NAME.getPreferredName(), ClassificationConfig::new) + ); + namedWriteables.add( + new NamedWriteableRegistry.Entry(InferenceConfig.class, RegressionConfig.NAME.getPreferredName(), RegressionConfig::new) + ); + namedWriteables.add(new NamedWriteableRegistry.Entry(InferenceConfig.class, NerConfig.NAME, NerConfig::new)); + namedWriteables.add(new NamedWriteableRegistry.Entry(InferenceConfig.class, FillMaskConfig.NAME, FillMaskConfig::new)); + namedWriteables.add( + new NamedWriteableRegistry.Entry(InferenceConfig.class, TextClassificationConfig.NAME, TextClassificationConfig::new) + ); + namedWriteables.add(new NamedWriteableRegistry.Entry(InferenceConfig.class, PassThroughConfig.NAME, PassThroughConfig::new)); + namedWriteables.add(new NamedWriteableRegistry.Entry(InferenceConfig.class, TextEmbeddingConfig.NAME, TextEmbeddingConfig::new)); + namedWriteables.add( + new NamedWriteableRegistry.Entry(InferenceConfig.class, ZeroShotClassificationConfig.NAME, ZeroShotClassificationConfig::new) + ); // Inference Configs Updates - namedWriteables.add(new NamedWriteableRegistry.Entry(InferenceConfigUpdate.class, - ClassificationConfigUpdate.NAME.getPreferredName(), ClassificationConfigUpdate::new)); - namedWriteables.add(new NamedWriteableRegistry.Entry(InferenceConfigUpdate.class, - EmptyConfigUpdate.NAME, EmptyConfigUpdate::new)); - namedWriteables.add(new NamedWriteableRegistry.Entry(InferenceConfigUpdate.class, - FillMaskConfigUpdate.NAME, FillMaskConfigUpdate::new)); - namedWriteables.add(new NamedWriteableRegistry.Entry(InferenceConfigUpdate.class, - NerConfigUpdate.NAME, NerConfigUpdate::new)); - namedWriteables.add(new NamedWriteableRegistry.Entry(InferenceConfigUpdate.class, - PassThroughConfigUpdate.NAME, PassThroughConfigUpdate::new)); - namedWriteables.add(new NamedWriteableRegistry.Entry(InferenceConfigUpdate.class, - RegressionConfigUpdate.NAME.getPreferredName(), RegressionConfigUpdate::new)); - namedWriteables.add(new NamedWriteableRegistry.Entry(InferenceConfigUpdate.class, - ResultsFieldUpdate.NAME, ResultsFieldUpdate::new)); - namedWriteables.add(new NamedWriteableRegistry.Entry(InferenceConfigUpdate.class, - TextClassificationConfigUpdate.NAME, TextClassificationConfigUpdate::new)); - namedWriteables.add(new NamedWriteableRegistry.Entry(InferenceConfigUpdate.class, - TextEmbeddingConfigUpdate.NAME, TextClassificationConfigUpdate::new)); - namedWriteables.add(new NamedWriteableRegistry.Entry(InferenceConfigUpdate.class, - ZeroShotClassificationConfigUpdate.NAME, ZeroShotClassificationConfigUpdate::new)); + namedWriteables.add( + new NamedWriteableRegistry.Entry( + InferenceConfigUpdate.class, + ClassificationConfigUpdate.NAME.getPreferredName(), + ClassificationConfigUpdate::new + ) + ); + namedWriteables.add(new NamedWriteableRegistry.Entry(InferenceConfigUpdate.class, EmptyConfigUpdate.NAME, EmptyConfigUpdate::new)); + namedWriteables.add( + new NamedWriteableRegistry.Entry(InferenceConfigUpdate.class, FillMaskConfigUpdate.NAME, FillMaskConfigUpdate::new) + ); + namedWriteables.add(new NamedWriteableRegistry.Entry(InferenceConfigUpdate.class, NerConfigUpdate.NAME, NerConfigUpdate::new)); + namedWriteables.add( + new NamedWriteableRegistry.Entry(InferenceConfigUpdate.class, PassThroughConfigUpdate.NAME, PassThroughConfigUpdate::new) + ); + namedWriteables.add( + new NamedWriteableRegistry.Entry( + InferenceConfigUpdate.class, + RegressionConfigUpdate.NAME.getPreferredName(), + RegressionConfigUpdate::new + ) + ); + namedWriteables.add( + new NamedWriteableRegistry.Entry(InferenceConfigUpdate.class, ResultsFieldUpdate.NAME, ResultsFieldUpdate::new) + ); + namedWriteables.add( + new NamedWriteableRegistry.Entry( + InferenceConfigUpdate.class, + TextClassificationConfigUpdate.NAME, + TextClassificationConfigUpdate::new + ) + ); + namedWriteables.add( + new NamedWriteableRegistry.Entry( + InferenceConfigUpdate.class, + TextEmbeddingConfigUpdate.NAME, + TextClassificationConfigUpdate::new + ) + ); + namedWriteables.add( + new NamedWriteableRegistry.Entry( + InferenceConfigUpdate.class, + ZeroShotClassificationConfigUpdate.NAME, + ZeroShotClassificationConfigUpdate::new + ) + ); // Location - namedWriteables.add(new NamedWriteableRegistry.Entry(TrainedModelLocation.class, - IndexLocation.INDEX.getPreferredName(), IndexLocation::new)); + namedWriteables.add( + new NamedWriteableRegistry.Entry(TrainedModelLocation.class, IndexLocation.INDEX.getPreferredName(), IndexLocation::new) + ); // Tokenization namedWriteables.add( - new NamedWriteableRegistry.Entry( - Tokenization.class, - BertTokenization.NAME.getPreferredName(), - BertTokenization::new - ) + new NamedWriteableRegistry.Entry(Tokenization.class, BertTokenization.NAME.getPreferredName(), BertTokenization::new) ); return namedWriteables; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/TrainedModelConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/TrainedModelConfig.java index 5f2ddae51436a..cad6a3668c223 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/TrainedModelConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/TrainedModelConfig.java @@ -9,8 +9,6 @@ import org.apache.logging.log4j.message.ParameterizedMessage; import org.elasticsearch.Version; import org.elasticsearch.action.ActionRequestValidationException; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; @@ -18,12 +16,14 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.license.License; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.license.License; import org.elasticsearch.xpack.core.common.time.TimeUtils; import org.elasticsearch.xpack.core.ml.inference.persistence.InferenceIndexConstants; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.InferenceConfig; @@ -58,7 +58,6 @@ import static org.elasticsearch.xpack.core.ml.utils.NamedXContentObjectHelper.writeNamedObject; import static org.elasticsearch.xpack.core.ml.utils.ToXContentParams.EXCLUDE_GENERATED; - public class TrainedModelConfig implements ToXContentObject, Writeable { public static final String NAME = "trained_model_config"; @@ -96,41 +95,50 @@ public class TrainedModelConfig implements ToXContentObject, Writeable { public static final ObjectParser STRICT_PARSER = createParser(false); private static ObjectParser createParser(boolean ignoreUnknownFields) { - ObjectParser parser = new ObjectParser<>(NAME, + ObjectParser parser = new ObjectParser<>( + NAME, ignoreUnknownFields, - TrainedModelConfig.Builder::new); + TrainedModelConfig.Builder::new + ); parser.declareString(TrainedModelConfig.Builder::setModelId, MODEL_ID); parser.declareString(TrainedModelConfig.Builder::setModelType, MODEL_TYPE); parser.declareString(TrainedModelConfig.Builder::setCreatedBy, CREATED_BY); parser.declareString(TrainedModelConfig.Builder::setVersion, VERSION); parser.declareString(TrainedModelConfig.Builder::setDescription, DESCRIPTION); - parser.declareField(TrainedModelConfig.Builder::setCreateTime, + parser.declareField( + TrainedModelConfig.Builder::setCreateTime, (p, c) -> TimeUtils.parseTimeFieldToInstant(p, CREATE_TIME.getPreferredName()), CREATE_TIME, - ObjectParser.ValueType.VALUE); + ObjectParser.ValueType.VALUE + ); parser.declareStringArray(TrainedModelConfig.Builder::setTags, TAGS); parser.declareObject(TrainedModelConfig.Builder::setMetadata, (p, c) -> p.map(), METADATA); parser.declareString((trainedModelConfig, s) -> {}, InferenceIndexConstants.DOC_TYPE); - parser.declareObject(TrainedModelConfig.Builder::setInput, - (p, c) -> TrainedModelInput.fromXContent(p, ignoreUnknownFields), - INPUT); + parser.declareObject(TrainedModelConfig.Builder::setInput, (p, c) -> TrainedModelInput.fromXContent(p, ignoreUnknownFields), INPUT); parser.declareLong(TrainedModelConfig.Builder::setEstimatedHeapMemory, ESTIMATED_HEAP_MEMORY_USAGE_BYTES); parser.declareLong(TrainedModelConfig.Builder::setEstimatedOperations, ESTIMATED_OPERATIONS); - parser.declareObject(TrainedModelConfig.Builder::setLazyDefinition, + parser.declareObject( + TrainedModelConfig.Builder::setLazyDefinition, (p, c) -> TrainedModelDefinition.fromXContent(p, ignoreUnknownFields), - DEFINITION); + DEFINITION + ); parser.declareString(TrainedModelConfig.Builder::setLazyDefinition, COMPRESSED_DEFINITION); parser.declareString(TrainedModelConfig.Builder::setLicenseLevel, LICENSE_LEVEL); parser.declareObject(TrainedModelConfig.Builder::setDefaultFieldMap, (p, c) -> p.mapStrings(), DEFAULT_FIELD_MAP); - parser.declareNamedObject(TrainedModelConfig.Builder::setInferenceConfig, (p, c, n) -> ignoreUnknownFields ? - p.namedObject(LenientlyParsedInferenceConfig.class, n, null) : - p.namedObject(StrictlyParsedInferenceConfig.class, n, null), - INFERENCE_CONFIG); - parser.declareNamedObject(TrainedModelConfig.Builder::setLocation, - (p, c, n) -> ignoreUnknownFields ? - p.namedObject(LenientlyParsedTrainedModelLocation.class, n, null) : - p.namedObject(StrictlyParsedTrainedModelLocation.class, n, null), - LOCATION); + parser.declareNamedObject( + TrainedModelConfig.Builder::setInferenceConfig, + (p, c, n) -> ignoreUnknownFields + ? p.namedObject(LenientlyParsedInferenceConfig.class, n, null) + : p.namedObject(StrictlyParsedInferenceConfig.class, n, null), + INFERENCE_CONFIG + ); + parser.declareNamedObject( + TrainedModelConfig.Builder::setLocation, + (p, c, n) -> ignoreUnknownFields + ? p.namedObject(LenientlyParsedTrainedModelLocation.class, n, null) + : p.namedObject(StrictlyParsedTrainedModelLocation.class, n, null), + LOCATION + ); return parser; } @@ -156,22 +164,24 @@ public static TrainedModelConfig.Builder fromXContent(XContentParser parser, boo private final LazyModelDefinition definition; private final TrainedModelLocation location; - TrainedModelConfig(String modelId, - TrainedModelType modelType, - String createdBy, - Version version, - String description, - Instant createTime, - LazyModelDefinition definition, - List tags, - Map metadata, - TrainedModelInput input, - Long estimatedHeapMemory, - Long estimatedOperations, - String licenseLevel, - Map defaultFieldMap, - InferenceConfig inferenceConfig, - TrainedModelLocation location) { + TrainedModelConfig( + String modelId, + TrainedModelType modelType, + String createdBy, + Version version, + String description, + Instant createTime, + LazyModelDefinition definition, + List tags, + Map metadata, + TrainedModelInput input, + Long estimatedHeapMemory, + Long estimatedOperations, + String licenseLevel, + Map defaultFieldMap, + InferenceConfig inferenceConfig, + TrainedModelLocation location + ) { this.modelId = ExceptionsHelper.requireNonNull(modelId, MODEL_ID); this.modelType = modelType; this.createdBy = ExceptionsHelper.requireNonNull(createdBy, CREATED_BY); @@ -184,7 +194,8 @@ public static TrainedModelConfig.Builder fromXContent(XContentParser parser, boo this.input = ExceptionsHelper.requireNonNull(handleDefaultInput(input, modelType), INPUT); if (ExceptionsHelper.requireNonNull(estimatedHeapMemory, ESTIMATED_HEAP_MEMORY_USAGE_BYTES) < 0) { throw new IllegalArgumentException( - "[" + ESTIMATED_HEAP_MEMORY_USAGE_BYTES.getPreferredName() + "] must be greater than or equal to 0"); + "[" + ESTIMATED_HEAP_MEMORY_USAGE_BYTES.getPreferredName() + "] must be greater than or equal to 0" + ); } this.estimatedHeapMemory = estimatedHeapMemory; if (ExceptionsHelper.requireNonNull(estimatedOperations, ESTIMATED_OPERATIONS) < 0) { @@ -192,8 +203,8 @@ public static TrainedModelConfig.Builder fromXContent(XContentParser parser, boo } this.estimatedOperations = estimatedOperations; this.licenseLevel = License.OperationMode.parse(ExceptionsHelper.requireNonNull(licenseLevel, LICENSE_LEVEL)); - assert this.licenseLevel.equals(License.OperationMode.PLATINUM) || this.licenseLevel.equals(License.OperationMode.BASIC) : - "[" + LICENSE_LEVEL.getPreferredName() + "] only [platinum] or [basic] is supported"; + assert this.licenseLevel.equals(License.OperationMode.PLATINUM) || this.licenseLevel.equals(License.OperationMode.BASIC) + : "[" + LICENSE_LEVEL.getPreferredName() + "] only [platinum] or [basic] is supported"; this.defaultFieldMap = defaultFieldMap == null ? null : Collections.unmodifiableMap(defaultFieldMap); this.inferenceConfig = inferenceConfig; this.location = location; @@ -219,9 +230,9 @@ public TrainedModelConfig(StreamInput in) throws IOException { estimatedHeapMemory = in.readVLong(); estimatedOperations = in.readVLong(); licenseLevel = License.OperationMode.parse(in.readString()); - this.defaultFieldMap = in.readBoolean() ? - Collections.unmodifiableMap(in.readMap(StreamInput::readString, StreamInput::readString)) : - null; + this.defaultFieldMap = in.readBoolean() + ? Collections.unmodifiableMap(in.readMap(StreamInput::readString, StreamInput::readString)) + : null; this.inferenceConfig = in.readOptionalNamedWriteable(InferenceConfig.class); if (in.getVersion().onOrAfter(VERSION_3RD_PARTY_CONFIG_ADDED)) { @@ -290,7 +301,6 @@ public BytesReference getCompressedDefinitionIfSet() { return definition.getCompressedDefinitionIfSet(); } - public void clearCompressed() { definition.compressedRepresentation = null; } @@ -340,7 +350,7 @@ public long getEstimatedOperations() { return estimatedOperations; } - //TODO if we ever support anything other than "basic" and platinum, we need to adjust our feature tracking logic + // TODO if we ever support anything other than "basic" and platinum, we need to adjust our feature tracking logic public License.OperationMode getLicenseLevel() { return licenseLevel; } @@ -391,7 +401,8 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.humanReadableField( ESTIMATED_HEAP_MEMORY_USAGE_BYTES.getPreferredName(), ESTIMATED_HEAP_MEMORY_USAGE_HUMAN, - ByteSizeValue.ofBytes(estimatedHeapMemory)); + ByteSizeValue.ofBytes(estimatedHeapMemory) + ); builder.field(ESTIMATED_OPERATIONS.getPreferredName(), estimatedOperations); builder.field(LICENSE_LEVEL.getPreferredName(), licenseLevel.description()); } @@ -437,27 +448,28 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; TrainedModelConfig that = (TrainedModelConfig) o; - return Objects.equals(modelId, that.modelId) && - Objects.equals(modelType, that.modelType) && - Objects.equals(createdBy, that.createdBy) && - Objects.equals(version, that.version) && - Objects.equals(description, that.description) && - Objects.equals(createTime, that.createTime) && - Objects.equals(definition, that.definition) && - Objects.equals(tags, that.tags) && - Objects.equals(input, that.input) && - Objects.equals(estimatedHeapMemory, that.estimatedHeapMemory) && - Objects.equals(estimatedOperations, that.estimatedOperations) && - Objects.equals(licenseLevel, that.licenseLevel) && - Objects.equals(defaultFieldMap, that.defaultFieldMap) && - Objects.equals(inferenceConfig, that.inferenceConfig) && - Objects.equals(metadata, that.metadata) && - Objects.equals(location, that.location); + return Objects.equals(modelId, that.modelId) + && Objects.equals(modelType, that.modelType) + && Objects.equals(createdBy, that.createdBy) + && Objects.equals(version, that.version) + && Objects.equals(description, that.description) + && Objects.equals(createTime, that.createTime) + && Objects.equals(definition, that.definition) + && Objects.equals(tags, that.tags) + && Objects.equals(input, that.input) + && Objects.equals(estimatedHeapMemory, that.estimatedHeapMemory) + && Objects.equals(estimatedOperations, that.estimatedOperations) + && Objects.equals(licenseLevel, that.licenseLevel) + && Objects.equals(defaultFieldMap, that.defaultFieldMap) + && Objects.equals(inferenceConfig, that.inferenceConfig) + && Objects.equals(metadata, that.metadata) + && Objects.equals(location, that.location); } @Override public int hashCode() { - return Objects.hash(modelId, + return Objects.hash( + modelId, modelType, createdBy, version, @@ -472,7 +484,8 @@ public int hashCode() { licenseLevel, inferenceConfig, defaultFieldMap, - location); + location + ); } public static class Builder { @@ -597,10 +610,7 @@ public Builder setHyperparameters(List hyperparameters) { if (hyperparameters == null) { return this; } - return addToMetadata( - HYPERPARAMETERS, - hyperparameters.stream().map(Hyperparameters::asMap).collect(Collectors.toList()) - ); + return addToMetadata(HYPERPARAMETERS, hyperparameters.stream().map(Hyperparameters::asMap).collect(Collectors.toList())); } public Builder setModelAliases(Set modelAliases) { @@ -645,11 +655,13 @@ private Builder setLazyDefinition(TrainedModelDefinition.Builder parsedTrainedMo } if (this.definition != null) { - throw new IllegalArgumentException(new ParameterizedMessage( - "both [{}] and [{}] cannot be set.", - COMPRESSED_DEFINITION.getPreferredName(), - DEFINITION.getPreferredName()) - .getFormattedMessage()); + throw new IllegalArgumentException( + new ParameterizedMessage( + "both [{}] and [{}] cannot be set.", + COMPRESSED_DEFINITION.getPreferredName(), + DEFINITION.getPreferredName() + ).getFormattedMessage() + ); } this.definition = LazyModelDefinition.fromParsedDefinition(parsedTrainedModel.build()); return this; @@ -661,11 +673,13 @@ private Builder setLazyDefinition(String compressedString) { } if (this.definition != null) { - throw new IllegalArgumentException(new ParameterizedMessage( - "both [{}] and [{}] cannot be set.", - COMPRESSED_DEFINITION.getPreferredName(), - DEFINITION.getPreferredName()) - .getFormattedMessage()); + throw new IllegalArgumentException( + new ParameterizedMessage( + "both [{}] and [{}] cannot be set.", + COMPRESSED_DEFINITION.getPreferredName(), + DEFINITION.getPreferredName() + ).getFormattedMessage() + ); } this.definition = LazyModelDefinition.fromBase64String(compressedString); return this; @@ -720,44 +734,60 @@ public Builder validate(boolean forCreation) { // We require a definition to be available here even though it will be stored in a different doc ActionRequestValidationException validationException = null; if (definition != null && location != null) { - validationException = addValidationError("[" + DEFINITION.getPreferredName() + "] " + - "and [" + LOCATION.getPreferredName() + "] are both defined but only one can be used.", validationException); + validationException = addValidationError( + "[" + + DEFINITION.getPreferredName() + + "] " + + "and [" + + LOCATION.getPreferredName() + + "] are both defined but only one can be used.", + validationException + ); } if (definition == null && modelType == null) { - validationException = addValidationError("[" + MODEL_TYPE.getPreferredName() + "] must be set if " + - "[" + DEFINITION.getPreferredName() + "] is not defined.", validationException); + validationException = addValidationError( + "[" + MODEL_TYPE.getPreferredName() + "] must be set if " + "[" + DEFINITION.getPreferredName() + "] is not defined.", + validationException + ); } if (modelId == null) { validationException = addValidationError("[" + MODEL_ID.getPreferredName() + "] must not be null.", validationException); } if (inferenceConfig == null && forCreation) { - validationException = addValidationError("[" + INFERENCE_CONFIG.getPreferredName() + "] must not be null.", - validationException); + validationException = addValidationError( + "[" + INFERENCE_CONFIG.getPreferredName() + "] must not be null.", + validationException + ); } if (modelId != null && MlStrings.isValidId(modelId) == false) { - validationException = addValidationError(Messages.getMessage(Messages.INVALID_ID, - TrainedModelConfig.MODEL_ID.getPreferredName(), - modelId), - validationException); + validationException = addValidationError( + Messages.getMessage(Messages.INVALID_ID, TrainedModelConfig.MODEL_ID.getPreferredName(), modelId), + validationException + ); } if (modelId != null && MlStrings.hasValidLengthForId(modelId) == false) { - validationException = addValidationError(Messages.getMessage(Messages.ID_TOO_LONG, - TrainedModelConfig.MODEL_ID.getPreferredName(), - modelId, - MlStrings.ID_LENGTH_LIMIT), validationException); + validationException = addValidationError( + Messages.getMessage( + Messages.ID_TOO_LONG, + TrainedModelConfig.MODEL_ID.getPreferredName(), + modelId, + MlStrings.ID_LENGTH_LIMIT + ), + validationException + ); } List badTags = tags.stream() .filter(tag -> (MlStrings.isValidId(tag) && MlStrings.hasValidLengthForId(tag)) == false) .collect(Collectors.toList()); if (badTags.isEmpty() == false) { - validationException = addValidationError(Messages.getMessage(Messages.INFERENCE_INVALID_TAGS, - badTags, - MlStrings.ID_LENGTH_LIMIT), - validationException); + validationException = addValidationError( + Messages.getMessage(Messages.INFERENCE_INVALID_TAGS, badTags, MlStrings.ID_LENGTH_LIMIT), + validationException + ); } - for(String tag : tags) { + for (String tag : tags) { if (tag.equals(modelId)) { validationException = addValidationError("none of the tags must equal the model_id", validationException); break; @@ -766,13 +796,16 @@ public Builder validate(boolean forCreation) { if (input != null && input.getFieldNames().isEmpty()) { validationException = addValidationError("[input.field_names] must not be empty", validationException); } - if (input != null && input.getFieldNames() - .stream() - .filter(s -> s.contains(".")) - .flatMap(s -> Arrays.stream(Strings.delimitedListToStringArray(s, "."))) - .anyMatch(String::isEmpty)) { - validationException = addValidationError("[input.field_names] must only contain valid dot delimited field names", - validationException); + if (input != null + && input.getFieldNames() + .stream() + .filter(s -> s.contains(".")) + .flatMap(s -> Arrays.stream(Strings.delimitedListToStringArray(s, "."))) + .anyMatch(String::isEmpty)) { + validationException = addValidationError( + "[input.field_names] must only contain valid dot delimited field names", + validationException + ); } if (forCreation) { validationException = checkIllegalSetting(version, VERSION.getPreferredName(), validationException); @@ -784,11 +817,13 @@ public Builder validate(boolean forCreation) { validationException = checkIllegalSetting( metadata.get(TOTAL_FEATURE_IMPORTANCE), METADATA.getPreferredName() + "." + TOTAL_FEATURE_IMPORTANCE, - validationException); + validationException + ); validationException = checkIllegalSetting( metadata.get(MODEL_ALIASES), METADATA.getPreferredName() + "." + MODEL_ALIASES, - validationException); + validationException + ); } } if (validationException != null) { @@ -798,9 +833,11 @@ public Builder validate(boolean forCreation) { return this; } - private static ActionRequestValidationException checkIllegalSetting(Object value, - String setting, - ActionRequestValidationException validationException) { + private static ActionRequestValidationException checkIllegalSetting( + Object value, + String setting, + ActionRequestValidationException validationException + ) { if (value != null) { return addValidationError("illegal to set [" + setting + "] at inference model creation", validationException); } @@ -824,7 +861,8 @@ public TrainedModelConfig build() { licenseLevel == null ? License.OperationMode.PLATINUM.description() : licenseLevel, defaultFieldMap, inferenceConfig, - location); + location + ); } } @@ -883,25 +921,29 @@ private BytesReference getCompressedDefinitionIfSet() { private String getBase64CompressedDefinition() throws IOException { BytesReference compressedDef = getCompressedDefinition(); - ByteBuffer bb = Base64.getEncoder().encode( - ByteBuffer.wrap(compressedDef.array(), compressedDef.arrayOffset(), compressedDef.length())); + ByteBuffer bb = Base64.getEncoder() + .encode(ByteBuffer.wrap(compressedDef.array(), compressedDef.arrayOffset(), compressedDef.length())); return new String(bb.array(), StandardCharsets.UTF_8); } private void ensureParsedDefinition(NamedXContentRegistry xContentRegistry) throws IOException { if (parsedDefinition == null) { - parsedDefinition = InferenceToXContentCompressor.inflate(compressedRepresentation, + parsedDefinition = InferenceToXContentCompressor.inflate( + compressedRepresentation, parser -> TrainedModelDefinition.fromXContent(parser, true).build(), - xContentRegistry); + xContentRegistry + ); } } private void ensureParsedDefinitionUnsafe(NamedXContentRegistry xContentRegistry) throws IOException { if (parsedDefinition == null) { - parsedDefinition = InferenceToXContentCompressor.inflateUnsafe(compressedRepresentation, + parsedDefinition = InferenceToXContentCompressor.inflateUnsafe( + compressedRepresentation, parser -> TrainedModelDefinition.fromXContent(parser, true).build(), - xContentRegistry); + xContentRegistry + ); } } @@ -928,8 +970,8 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; LazyModelDefinition that = (LazyModelDefinition) o; - return Objects.equals(compressedRepresentation, that.compressedRepresentation) && - Objects.equals(parsedDefinition, that.parsedDefinition); + return Objects.equals(compressedRepresentation, that.compressedRepresentation) + && Objects.equals(parsedDefinition, that.parsedDefinition); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/TrainedModelDefinition.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/TrainedModelDefinition.java index 8a1f6bb6a5e31..3bab816b4c5be 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/TrainedModelDefinition.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/TrainedModelDefinition.java @@ -9,12 +9,12 @@ import org.apache.lucene.util.Accountable; import org.apache.lucene.util.Accountables; import org.apache.lucene.util.RamUsageEstimator; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -47,20 +47,26 @@ public class TrainedModelDefinition implements ToXContentObject, Writeable, Acco public static final ObjectParser STRICT_PARSER = createParser(false); private static ObjectParser createParser(boolean ignoreUnknownFields) { - ObjectParser parser = new ObjectParser<>(NAME, + ObjectParser parser = new ObjectParser<>( + NAME, ignoreUnknownFields, - TrainedModelDefinition.Builder::builderForParser); - parser.declareNamedObject(TrainedModelDefinition.Builder::setTrainedModel, - (p, c, n) -> ignoreUnknownFields ? - p.namedObject(LenientlyParsedTrainedModel.class, n, null) : - p.namedObject(StrictlyParsedTrainedModel.class, n, null), - TRAINED_MODEL); - parser.declareNamedObjects(TrainedModelDefinition.Builder::setPreProcessors, - (p, c, n) -> ignoreUnknownFields ? - p.namedObject(LenientlyParsedPreProcessor.class, n, PreProcessor.PreProcessorParseContext.DEFAULT) : - p.namedObject(StrictlyParsedPreProcessor.class, n, PreProcessor.PreProcessorParseContext.DEFAULT), + TrainedModelDefinition.Builder::builderForParser + ); + parser.declareNamedObject( + TrainedModelDefinition.Builder::setTrainedModel, + (p, c, n) -> ignoreUnknownFields + ? p.namedObject(LenientlyParsedTrainedModel.class, n, null) + : p.namedObject(StrictlyParsedTrainedModel.class, n, null), + TRAINED_MODEL + ); + parser.declareNamedObjects( + TrainedModelDefinition.Builder::setPreProcessors, + (p, c, n) -> ignoreUnknownFields + ? p.namedObject(LenientlyParsedPreProcessor.class, n, PreProcessor.PreProcessorParseContext.DEFAULT) + : p.namedObject(StrictlyParsedPreProcessor.class, n, PreProcessor.PreProcessorParseContext.DEFAULT), (trainedModelDefBuilder) -> trainedModelDefBuilder.setProcessorsInOrder(true), - PREPROCESSORS); + PREPROCESSORS + ); return parser; } @@ -90,16 +96,14 @@ public void writeTo(StreamOutput out) throws IOException { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); - NamedXContentObjectHelper.writeNamedObjects(builder, + NamedXContentObjectHelper.writeNamedObjects( + builder, params, false, TRAINED_MODEL.getPreferredName(), - Collections.singletonList(trainedModel)); - NamedXContentObjectHelper.writeNamedObjects(builder, - params, - true, - PREPROCESSORS.getPreferredName(), - preProcessors); + Collections.singletonList(trainedModel) + ); + NamedXContentObjectHelper.writeNamedObjects(builder, params, true, PREPROCESSORS.getPreferredName(), preProcessors); builder.endObject(); return builder; } @@ -122,8 +126,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; TrainedModelDefinition that = (TrainedModelDefinition) o; - return Objects.equals(trainedModel, that.trainedModel) && - Objects.equals(preProcessors, that.preProcessors); + return Objects.equals(trainedModel, that.trainedModel) && Objects.equals(preProcessors, that.preProcessors); } @Override @@ -143,7 +146,7 @@ public long ramBytesUsed() { public Collection getChildResources() { List accountables = new ArrayList<>(preProcessors.size() + 2); accountables.add(Accountables.namedAccountable("trained_model", trainedModel)); - for(PreProcessor preProcessor : preProcessors) { + for (PreProcessor preProcessor : preProcessors) { accountables.add(Accountables.namedAccountable("pre_processor_" + preProcessor.getName(), preProcessor)); } return accountables; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/TrainedModelInput.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/TrainedModelInput.java index 2b88a21d0eb3f..2a64bd34d3fb7 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/TrainedModelInput.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/TrainedModelInput.java @@ -6,11 +6,11 @@ */ package org.elasticsearch.xpack.core.ml.inference; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -21,7 +21,6 @@ import java.util.List; import java.util.Objects; - public class TrainedModelInput implements ToXContentObject, Writeable { public static final String NAME = "trained_model_config_input"; @@ -41,9 +40,11 @@ public TrainedModelInput(StreamInput in) throws IOException { @SuppressWarnings("unchecked") private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { - ConstructingObjectParser parser = new ConstructingObjectParser<>(NAME, + ConstructingObjectParser parser = new ConstructingObjectParser<>( + NAME, ignoreUnknownFields, - a -> new TrainedModelInput((List) a[0])); + a -> new TrainedModelInput((List) a[0]) + ); parser.declareStringArray(ConstructingObjectParser.constructorArg(), FIELD_NAMES); return parser; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/TrainedModelType.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/TrainedModelType.java index 95264b2997dbd..00c5d245a4861 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/TrainedModelType.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/TrainedModelType.java @@ -48,7 +48,7 @@ public static TrainedModelType typeFromTrainedModel(TrainedModel model) { private final TrainedModelInput defaultInput; TrainedModelType(@Nullable TrainedModelInput defaultInput) { - this.defaultInput =defaultInput; + this.defaultInput = defaultInput; } @Override @@ -70,11 +70,7 @@ public TrainedModelLocation getDefaultLocation(String modelId) { return new IndexLocation(InferenceIndexConstants.nativeDefinitionStore()); default: throw new IllegalArgumentException( - "can not determine appropriate location for type [" - + this - + " for model [" - + modelId - + "]" + "can not determine appropriate location for type [" + this + " for model [" + modelId + "]" ); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/allocation/AllocationStatus.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/allocation/AllocationStatus.java index ab16ba3b476ca..d756c0132393e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/allocation/AllocationStatus.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/allocation/AllocationStatus.java @@ -48,7 +48,7 @@ public String toString() { private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( "allocation_health", - a -> new AllocationStatus((int)a[0], (int)a[1]) + a -> new AllocationStatus((int) a[0], (int) a[1]) ); static { PARSER.declareInt(ConstructingObjectParser.constructorArg(), ALLOCATION_COUNT); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/allocation/TrainedModelAllocation.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/allocation/TrainedModelAllocation.java index fc7b6f1620cc3..684ae09290b7a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/allocation/TrainedModelAllocation.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/allocation/TrainedModelAllocation.java @@ -51,7 +51,7 @@ public class TrainedModelAllocation extends AbstractDiffable new TrainedModelAllocation( (StartTrainedModelDeploymentAction.TaskParams) a[0], (Map) a[1], - AllocationState.fromString((String)a[2]), + AllocationState.fromString((String) a[2]), (String) a[3] ) ); @@ -115,8 +115,7 @@ public AllocationState getAllocationState() { } public String[] getStartedNodes() { - return nodeRoutingTable - .entrySet() + return nodeRoutingTable.entrySet() .stream() .filter(entry -> RoutingState.STARTED.equals(entry.getValue().getState())) .map(Map.Entry::getKey) @@ -184,7 +183,6 @@ public Optional calculateAllocationStatus(List return Optional.of(new AllocationStatus(numStarted, numAllocatableNodes)); } - public static class Builder { private final Map nodeRoutingTable; private final StartTrainedModelDeploymentAction.TaskParams taskParams; @@ -221,7 +219,9 @@ private Builder(StartTrainedModelDeploymentAction.TaskParams taskParams) { public Builder addNewRoutingEntry(String nodeId) { if (nodeRoutingTable.containsKey(nodeId)) { throw new ResourceAlreadyExistsException( - "routing entry for node [{}] for model [{}] already exists", nodeId, taskParams.getModelId() + "routing entry for node [{}] for model [{}] already exists", + nodeId, + taskParams.getModelId() ); } isChanged = true; @@ -238,7 +238,9 @@ Builder addRoutingEntry(String nodeId, RoutingState state) { public Builder addNewFailedRoutingEntry(String nodeId, String reason) { if (nodeRoutingTable.containsKey(nodeId)) { throw new ResourceAlreadyExistsException( - "routing entry for node [{}] for model [{}] already exists", nodeId, taskParams.getModelId() + "routing entry for node [{}] for model [{}] already exists", + nodeId, + taskParams.getModelId() ); } isChanged = true; @@ -250,7 +252,9 @@ public Builder updateExistingRoutingEntry(String nodeId, RoutingStateAndReason s RoutingStateAndReason stateAndReason = nodeRoutingTable.get(nodeId); if (stateAndReason == null) { throw new ResourceNotFoundException( - "routing entry for node [{}] for model [{}] does not exist", nodeId, taskParams.getModelId() + "routing entry for node [{}] for model [{}] does not exist", + nodeId, + taskParams.getModelId() ); } if (stateAndReason.equals(state)) { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/persistence/InferenceIndexConstants.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/persistence/InferenceIndexConstants.java index 38fc66bdba0b4..3322af0b1810e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/persistence/InferenceIndexConstants.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/persistence/InferenceIndexConstants.java @@ -8,8 +8,8 @@ import org.elasticsearch.Version; import org.elasticsearch.cluster.metadata.IndexMetadata; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xpack.core.template.TemplateUtils; /** @@ -43,7 +43,8 @@ public static String mapping() { return TemplateUtils.loadTemplate( "/org/elasticsearch/xpack/core/ml/inference_index_mappings.json", Version.CURRENT.toString(), - MAPPINGS_VERSION_VARIABLE); + MAPPINGS_VERSION_VARIABLE + ); } public static String nativeDefinitionStore() { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/CustomWordEmbedding.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/CustomWordEmbedding.java index 6694f0708a83c..989f53f19ef2e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/CustomWordEmbedding.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/CustomWordEmbedding.java @@ -8,11 +8,11 @@ package org.elasticsearch.xpack.core.ml.inference.preprocessing; import org.apache.lucene.util.RamUsageEstimator; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.inference.preprocessing.customwordembedding.FeatureExtractor; @@ -59,41 +59,38 @@ private static ConstructingObjectParser parser = new ConstructingObjectParser<>( NAME.getPreferredName(), lenient, - (a, c) -> new CustomWordEmbedding((short[][])a[0], (byte[][])a[1], (String)a[2], (String)a[3])); - - parser.declareField(ConstructingObjectParser.constructorArg(), - (p, c) -> { - List> listOfListOfShorts = MlParserUtils.parseArrayOfArrays(EMBEDDING_QUANT_SCALES.getPreferredName(), - XContentParser::shortValue, - p); - short[][] primitiveShorts = new short[listOfListOfShorts.size()][]; - int i = 0; - for (List shorts : listOfListOfShorts) { - short[] innerShorts = new short[shorts.size()]; - for (int j = 0; j < shorts.size(); j++) { - innerShorts[j] = shorts.get(j); - } - primitiveShorts[i++] = innerShorts; - } - return primitiveShorts; - }, - EMBEDDING_QUANT_SCALES, - ObjectParser.ValueType.VALUE_ARRAY); - parser.declareField(ConstructingObjectParser.constructorArg(), - (p, c) -> { - List values = new ArrayList<>(); - while(p.nextToken() != XContentParser.Token.END_ARRAY) { - values.add(p.binaryValue()); - } - byte[][] primitiveBytes = new byte[values.size()][]; - int i = 0; - for (byte[] bytes : values) { - primitiveBytes[i++] = bytes; + (a, c) -> new CustomWordEmbedding((short[][]) a[0], (byte[][]) a[1], (String) a[2], (String) a[3]) + ); + + parser.declareField(ConstructingObjectParser.constructorArg(), (p, c) -> { + List> listOfListOfShorts = MlParserUtils.parseArrayOfArrays( + EMBEDDING_QUANT_SCALES.getPreferredName(), + XContentParser::shortValue, + p + ); + short[][] primitiveShorts = new short[listOfListOfShorts.size()][]; + int i = 0; + for (List shorts : listOfListOfShorts) { + short[] innerShorts = new short[shorts.size()]; + for (int j = 0; j < shorts.size(); j++) { + innerShorts[j] = shorts.get(j); } - return primitiveBytes; - }, - EMBEDDING_WEIGHTS, - ObjectParser.ValueType.VALUE_ARRAY); + primitiveShorts[i++] = innerShorts; + } + return primitiveShorts; + }, EMBEDDING_QUANT_SCALES, ObjectParser.ValueType.VALUE_ARRAY); + parser.declareField(ConstructingObjectParser.constructorArg(), (p, c) -> { + List values = new ArrayList<>(); + while (p.nextToken() != XContentParser.Token.END_ARRAY) { + values.add(p.binaryValue()); + } + byte[][] primitiveBytes = new byte[values.size()][]; + int i = 0; + for (byte[] bytes : values) { + primitiveBytes[i++] = bytes; + } + return primitiveBytes; + }, EMBEDDING_WEIGHTS, ObjectParser.ValueType.VALUE_ARRAY); parser.declareString(ConstructingObjectParser.constructorArg(), FIELD); parser.declareString(ConstructingObjectParser.constructorArg(), DEST_FIELD); return parser; @@ -108,7 +105,7 @@ public static CustomWordEmbedding fromXContentLenient(XContentParser parser) { } private static final int CONCAT_LAYER_SIZE = 80; - private static final int[] EMBEDDING_DIMENSIONS = new int[]{16, 16, 8, 8, 16, 16}; + private static final int[] EMBEDDING_DIMENSIONS = new int[] { 16, 16, 8, 8, 16, 16 }; // Order matters private static final List FEATURE_EXTRACTORS = Arrays.asList( @@ -213,7 +210,7 @@ public void process(Map fields) { if ((field instanceof String) == false) { return; } - String text = (String)field; + String text = (String) field; text = FeatureUtils.cleanAndLowerText(text); text = FeatureUtils.truncateToNumValidBytes(text, MAX_STRING_SIZE_IN_BYTES); String finalText = text; @@ -241,10 +238,10 @@ public String getOutputFieldType(String outputField) { @Override public long ramBytesUsed() { long size = SHALLOW_SIZE; - for(byte[] bytes : embeddingsWeights) { + for (byte[] bytes : embeddingsWeights) { size += RamUsageEstimator.sizeOf(bytes); } - for(short[] shorts : embeddingsQuantScales) { + for (short[] shorts : embeddingsQuantScales) { size += RamUsageEstimator.sizeOf(shorts); } return size; @@ -262,7 +259,7 @@ public void writeTo(StreamOutput out) throws IOException { out.writeArray(StreamOutput::writeByteArray, embeddingsWeights); out.writeArray((output, value) -> { output.writeVInt(value.length); - for(short s : value) { + for (short s : value) { output.writeShort(s); } }, embeddingsQuantScales); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/FrequencyEncoding.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/FrequencyEncoding.java index 3e173c8622649..9b96efe3ac4a0 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/FrequencyEncoding.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/FrequencyEncoding.java @@ -7,14 +7,14 @@ package org.elasticsearch.xpack.core.ml.inference.preprocessing; import org.apache.lucene.util.RamUsageEstimator; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.index.mapper.NumberFieldMapper; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.index.mapper.NumberFieldMapper; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; import java.io.IOException; @@ -24,7 +24,6 @@ import java.util.Map; import java.util.Objects; - /** * PreProcessor for frequency encoding a set of categorical values for a given field. */ @@ -46,25 +45,30 @@ private static ConstructingObjectParser parser = new ConstructingObjectParser<>( NAME.getPreferredName(), lenient, - (a, c) -> new FrequencyEncoding((String)a[0], - (String)a[1], - (Map)a[2], - a[3] == null ? c.isCustomByDefault() : (Boolean)a[3])); + (a, c) -> new FrequencyEncoding( + (String) a[0], + (String) a[1], + (Map) a[2], + a[3] == null ? c.isCustomByDefault() : (Boolean) a[3] + ) + ); parser.declareString(ConstructingObjectParser.constructorArg(), FIELD); parser.declareString(ConstructingObjectParser.constructorArg(), FEATURE_NAME); - parser.declareObject(ConstructingObjectParser.constructorArg(), + parser.declareObject( + ConstructingObjectParser.constructorArg(), (p, c) -> p.map(HashMap::new, XContentParser::doubleValue), - FREQUENCY_MAP); + FREQUENCY_MAP + ); parser.declareBoolean(ConstructingObjectParser.optionalConstructorArg(), CUSTOM); return parser; } public static FrequencyEncoding fromXContentStrict(XContentParser parser, PreProcessorParseContext context) { - return STRICT_PARSER.apply(parser, context == null ? PreProcessorParseContext.DEFAULT : context); + return STRICT_PARSER.apply(parser, context == null ? PreProcessorParseContext.DEFAULT : context); } public static FrequencyEncoding fromXContentLenient(XContentParser parser, PreProcessorParseContext context) { - return LENIENT_PARSER.apply(parser, context == null ? PreProcessorParseContext.DEFAULT : context); + return LENIENT_PARSER.apply(parser, context == null ? PreProcessorParseContext.DEFAULT : context); } private final String field; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/LenientlyParsedPreProcessor.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/LenientlyParsedPreProcessor.java index 9d6e09126283a..63cb3df235cf7 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/LenientlyParsedPreProcessor.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/LenientlyParsedPreProcessor.java @@ -9,5 +9,4 @@ /** * To be used in conjunction with a lenient parser. */ -public interface LenientlyParsedPreProcessor extends PreProcessor { -} +public interface LenientlyParsedPreProcessor extends PreProcessor {} diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/Multi.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/Multi.java index 08a1c5ef39d10..bd5b6d980d45b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/Multi.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/Multi.java @@ -6,6 +6,16 @@ */ package org.elasticsearch.xpack.core.ml.inference.preprocessing; +import org.apache.lucene.util.RamUsageEstimator; +import org.elasticsearch.common.Strings; +import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; +import org.elasticsearch.xpack.core.ml.utils.NamedXContentObjectHelper; import java.io.IOException; import java.util.ArrayList; @@ -20,17 +30,6 @@ import java.util.function.Function; import java.util.stream.Collectors; -import org.apache.lucene.util.RamUsageEstimator; -import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.common.Strings; -import org.elasticsearch.common.io.stream.StreamInput; -import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.xcontent.ObjectParser; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; -import org.elasticsearch.xpack.core.ml.utils.NamedXContentObjectHelper; - /** * Multi-PreProcessor for chaining together multiple processors */ @@ -50,22 +49,24 @@ private static ObjectParser createParse lenient, Multi.Builder::new ); - parser.declareNamedObjects(Multi.Builder::setProcessors, - (p, c, n) -> lenient ? - p.namedObject(LenientlyParsedPreProcessor.class, n, PreProcessor.PreProcessorParseContext.DEFAULT) : - p.namedObject(StrictlyParsedPreProcessor.class, n, PreProcessor.PreProcessorParseContext.DEFAULT), + parser.declareNamedObjects( + Multi.Builder::setProcessors, + (p, c, n) -> lenient + ? p.namedObject(LenientlyParsedPreProcessor.class, n, PreProcessor.PreProcessorParseContext.DEFAULT) + : p.namedObject(StrictlyParsedPreProcessor.class, n, PreProcessor.PreProcessorParseContext.DEFAULT), (multiBuilder) -> multiBuilder.setOrdered(true), - PROCESSORS); + PROCESSORS + ); parser.declareBoolean(Multi.Builder::setCustom, CUSTOM); return parser; } public static Multi fromXContentStrict(XContentParser parser, PreProcessorParseContext context) { - return STRICT_PARSER.apply(parser, context == null ? PreProcessorParseContext.DEFAULT : context).build(); + return STRICT_PARSER.apply(parser, context == null ? PreProcessorParseContext.DEFAULT : context).build(); } public static Multi fromXContentLenient(XContentParser parser, PreProcessorParseContext context) { - return LENIENT_PARSER.apply(parser, context == null ? PreProcessorParseContext.DEFAULT : context).build(); + return LENIENT_PARSER.apply(parser, context == null ? PreProcessorParseContext.DEFAULT : context).build(); } private final PreProcessor[] processors; @@ -110,8 +111,8 @@ public Multi(PreProcessor[] processors, Boolean custom) { throw new IllegalArgumentException( String.format( Locale.ROOT, - "[custom] cannot be false as [%s] is unable to accurately determine" + - " field reverse encoding for input fields [%s] and output fields %s", + "[custom] cannot be false as [%s] is unable to accurately determine" + + " field reverse encoding for input fields [%s] and output fields %s", NAME.getPreferredName(), Strings.arrayToCommaDelimitedString(this.inputFields), this.outputFields.keySet() diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/NGram.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/NGram.java index 1144536abdb73..58860d7a72454 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/NGram.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/NGram.java @@ -7,15 +7,15 @@ package org.elasticsearch.xpack.core.ml.inference.preprocessing; import org.apache.lucene.util.RamUsageEstimator; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.util.set.Sets; +import org.elasticsearch.index.mapper.TextFieldMapper; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.index.mapper.TextFieldMapper; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; import java.io.IOException; @@ -44,10 +44,7 @@ public class NGram implements LenientlyParsedPreProcessor, StrictlyParsedPreProc private static final int MAX_GRAM = 5; private static String defaultPrefix(Integer start, Integer length) { - return "ngram_" - + (start == null ? DEFAULT_START : start) - + "_" - + (length == null ? DEFAULT_LENGTH : length); + return "ngram_" + (start == null ? DEFAULT_START : start) + "_" + (length == null ? DEFAULT_LENGTH : length); } public static final long SHALLOW_SIZE = RamUsageEstimator.shallowSizeOfInstance(NGram.class); @@ -67,12 +64,15 @@ private static ConstructingObjectParser createP ConstructingObjectParser parser = new ConstructingObjectParser<>( NAME.getPreferredName(), lenient, - (a, c) -> new NGram((String)a[0], - (List)a[1], - (Integer)a[2], - (Integer)a[3], - a[4] == null ? c.isCustomByDefault() : (Boolean)a[4], - (String)a[5])); + (a, c) -> new NGram( + (String) a[0], + (List) a[1], + (Integer) a[2], + (Integer) a[3], + a[4] == null ? c.isCustomByDefault() : (Boolean) a[4], + (String) a[5] + ) + ); parser.declareString(ConstructingObjectParser.constructorArg(), FIELD); parser.declareIntArray(ConstructingObjectParser.constructorArg(), NGRAMS); parser.declareInt(ConstructingObjectParser.optionalConstructorArg(), START); @@ -83,11 +83,11 @@ private static ConstructingObjectParser createP } public static NGram fromXContentStrict(XContentParser parser, PreProcessorParseContext context) { - return STRICT_PARSER.apply(parser, context == null ? PreProcessorParseContext.DEFAULT : context); + return STRICT_PARSER.apply(parser, context == null ? PreProcessorParseContext.DEFAULT : context); } public static NGram fromXContentLenient(XContentParser parser, PreProcessorParseContext context) { - return LENIENT_PARSER.apply(parser, context == null ? PreProcessorParseContext.DEFAULT : context); + return LENIENT_PARSER.apply(parser, context == null ? PreProcessorParseContext.DEFAULT : context); } private final String field; @@ -97,18 +97,15 @@ public static NGram fromXContentLenient(XContentParser parser, PreProcessorParse private final int length; private final boolean custom; - NGram(String field, - List nGrams, - Integer start, - Integer length, - Boolean custom, - String featurePrefix) { - this(field, + NGram(String field, List nGrams, Integer start, Integer length, Boolean custom, String featurePrefix) { + this( + field, featurePrefix == null ? defaultPrefix(start, length) : featurePrefix, Sets.newHashSet(nGrams).stream().mapToInt(Integer::intValue).toArray(), start == null ? DEFAULT_START : start, length == null ? DEFAULT_LENGTH : length, - custom != null && custom); + custom != null && custom + ); } public NGram(String field, String featurePrefix, int[] nGrams, int start, int length, boolean custom) { @@ -124,12 +121,12 @@ public NGram(String field, String featurePrefix, int[] nGrams, int start, int le NGRAMS.getPreferredName(), Arrays.stream(nGrams).mapToObj(String::valueOf).collect(Collectors.joining(", ")), MIN_GRAM, - MAX_GRAM); + MAX_GRAM + ); } this.start = start; if (start < 0 && length + start > 0) { - throw ExceptionsHelper.badRequestException( - "if [start] is negative, [length] + [start] must be less than 0"); + throw ExceptionsHelper.badRequestException("if [start] is negative, [length] + [start] must be less than 0"); } this.length = length; if (length <= 0) { @@ -143,7 +140,8 @@ public NGram(String field, String featurePrefix, int[] nGrams, int start, int le "[{}] and [{}] are invalid; all ngrams must be shorter than or equal to length [{}]", NGRAMS.getPreferredName(), LENGTH.getPreferredName(), - length); + length + ); } this.custom = custom; } @@ -277,12 +275,12 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; NGram nGram = (NGram) o; - return start == nGram.start && - length == nGram.length && - custom == nGram.custom && - Objects.equals(field, nGram.field) && - Objects.equals(featurePrefix, nGram.featurePrefix) && - Arrays.equals(nGrams, nGram.nGrams); + return start == nGram.start + && length == nGram.length + && custom == nGram.custom + && Objects.equals(field, nGram.field) + && Objects.equals(featurePrefix, nGram.featurePrefix) + && Arrays.equals(nGrams, nGram.nGrams); } @Override @@ -293,10 +291,7 @@ public int hashCode() { } private String nGramFeature(int nGram, int pos) { - return featurePrefix - + "." - + nGram - + pos; + return featurePrefix + "." + nGram + pos; } private List allPossibleNGramOutputFeatureNames() { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/OneHotEncoding.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/OneHotEncoding.java index 40955364d39d4..ba2c802c9addb 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/OneHotEncoding.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/OneHotEncoding.java @@ -7,14 +7,14 @@ package org.elasticsearch.xpack.core.ml.inference.preprocessing; import org.apache.lucene.util.RamUsageEstimator; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.index.mapper.NumberFieldMapper; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.index.mapper.NumberFieldMapper; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; import java.io.IOException; @@ -46,9 +46,8 @@ private static ConstructingObjectParser parser = new ConstructingObjectParser<>( NAME.getPreferredName(), lenient, - (a, c) -> new OneHotEncoding((String)a[0], - (Map)a[1], - a[2] == null ? c.isCustomByDefault() : (Boolean)a[2])); + (a, c) -> new OneHotEncoding((String) a[0], (Map) a[1], a[2] == null ? c.isCustomByDefault() : (Boolean) a[2]) + ); parser.declareString(ConstructingObjectParser.constructorArg(), FIELD); parser.declareObject(ConstructingObjectParser.constructorArg(), (p, c) -> p.mapStrings(), HOT_MAP); parser.declareBoolean(ConstructingObjectParser.optionalConstructorArg(), CUSTOM); @@ -56,11 +55,11 @@ private static ConstructingObjectParser parser = new ConstructingObjectParser<>( NAME.getPreferredName(), lenient, - (a, c) -> new TargetMeanEncoding((String)a[0], - (String)a[1], - (Map)a[2], - (Double)a[3], - a[4] == null ? c.isCustomByDefault() : (Boolean)a[4])); + (a, c) -> new TargetMeanEncoding( + (String) a[0], + (String) a[1], + (Map) a[2], + (Double) a[3], + a[4] == null ? c.isCustomByDefault() : (Boolean) a[4] + ) + ); parser.declareString(ConstructingObjectParser.constructorArg(), FIELD); parser.declareString(ConstructingObjectParser.constructorArg(), FEATURE_NAME); - parser.declareObject(ConstructingObjectParser.constructorArg(), + parser.declareObject( + ConstructingObjectParser.constructorArg(), (p, c) -> p.map(HashMap::new, XContentParser::doubleValue), - TARGET_MAP); + TARGET_MAP + ); parser.declareDouble(ConstructingObjectParser.constructorArg(), DEFAULT_VALUE); parser.declareBoolean(ConstructingObjectParser.optionalConstructorArg(), CUSTOM); return parser; } public static TargetMeanEncoding fromXContentStrict(XContentParser parser, PreProcessorParseContext context) { - return STRICT_PARSER.apply(parser, context == null ? PreProcessorParseContext.DEFAULT : context); + return STRICT_PARSER.apply(parser, context == null ? PreProcessorParseContext.DEFAULT : context); } public static TargetMeanEncoding fromXContentLenient(XContentParser parser, PreProcessorParseContext context) { - return LENIENT_PARSER.apply(parser, context == null ? PreProcessorParseContext.DEFAULT : context); + return LENIENT_PARSER.apply(parser, context == null ? PreProcessorParseContext.DEFAULT : context); } private final String field; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/customwordembedding/DiscreteFeatureValue.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/customwordembedding/DiscreteFeatureValue.java index 4cebbaa4ed6b3..30decd1352daa 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/customwordembedding/DiscreteFeatureValue.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/customwordembedding/DiscreteFeatureValue.java @@ -12,6 +12,7 @@ public class DiscreteFeatureValue extends FeatureValue { private final int id; + DiscreteFeatureValue(int id) { this.id = id; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/customwordembedding/Hash32.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/customwordembedding/Hash32.java index 0a92b90bfcaf1..9d8e3f739d19e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/customwordembedding/Hash32.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/customwordembedding/Hash32.java @@ -81,10 +81,8 @@ private int hash32(byte[] data) { } private static int decodeFixed32(byte[] ptr, int offset) { - return Byte.toUnsignedInt(ptr[offset]) | - Byte.toUnsignedInt(ptr[offset + 1]) << 8 | - Byte.toUnsignedInt(ptr[offset + 2]) << 16 | - Byte.toUnsignedInt(ptr[offset + 3]) << 24; + return Byte.toUnsignedInt(ptr[offset]) | Byte.toUnsignedInt(ptr[offset + 1]) << 8 | Byte.toUnsignedInt(ptr[offset + 2]) << 16 | Byte + .toUnsignedInt(ptr[offset + 3]) << 24; } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/customwordembedding/NGramFeatureExtractor.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/customwordembedding/NGramFeatureExtractor.java index 52e544db8b9e4..5249209201b32 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/customwordembedding/NGramFeatureExtractor.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/customwordembedding/NGramFeatureExtractor.java @@ -85,7 +85,7 @@ public FeatureValue[] extractFeatures(String text) { double weight = (double) value / (double) countSum; // We need to use the special hashing so that we choose the appropriate weight+ quantile // when building the feature vector. - int id = (int)(hashing.hash(key) % dimensionId); + int id = (int) (hashing.hash(key) % dimensionId); results[index++] = new ContinuousFeatureValue(id, weight); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/customwordembedding/RelevantScriptFeatureExtractor.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/customwordembedding/RelevantScriptFeatureExtractor.java index adf097516270f..134ab641dc961 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/customwordembedding/RelevantScriptFeatureExtractor.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/customwordembedding/RelevantScriptFeatureExtractor.java @@ -38,7 +38,7 @@ public FeatureValue[] extractFeatures(String text) { // Get anything that is a letter, or anything complex enough warranting a check (more than one UTF-8 byte). // cp > Byte.MAX_VALUE works as the first 127 codepoints are the same as the ASCII encoding, // which is the same as one UTF-8 byte. - if(Character.isLetter(cp) || cp > Byte.MAX_VALUE) { + if (Character.isLetter(cp) || cp > Byte.MAX_VALUE) { ScriptDetector.Script script = ScriptDetector.Script.fromCodePoint(cp); counts.computeIfAbsent(script, (s) -> Counter.newCounter()).addAndGet(1); totalCount.addAndGet(1L); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/customwordembedding/ScriptCode.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/customwordembedding/ScriptCode.java index ecabaa494b4c9..72644033e0a5d 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/customwordembedding/ScriptCode.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/customwordembedding/ScriptCode.java @@ -14,108 +14,108 @@ * See https://github.com/google/cld3/blob/master/src/script_span/generated_ulscript.h */ public enum ScriptCode { - Common(0),//Zyyy - Latin(1),//Latn - Greek(2),//Grek - Cyrillic(3),//Cyrl - Armenian(4),//Armn - Hebrew(5),//Hebr - Arabic(6),//Arab - Syriac(7),//Syrc - Thaana(8),//Thaa - Devanagari(9),//Deva - Bengali(10),//Beng - Gurmukhi(11),//Guru - Gujarati(12),//Gujr - Oriya(13),//Orya - Tamil(14),//Taml - Telugu(15),//Telu - Kannada(16),//Knda - Malayalam(17),//Mlym - Sinhala(18),//Sinh - Thai(19),//Thai - Lao(20),//Laoo - Tibetan(21),//Tibt - Myanmar(22),//Mymr - Georgian(23),//Geor - Hani(24),//Hani - Ethiopic(25),//Ethi - Cherokee(26),//Cher - Canadian_Aboriginal(27),//Cans - Ogham(28),//Ogam - Runic(29),//Runr - Khmer(30),//Khmr - Mongolian(31),//Mong + Common(0),// Zyyy + Latin(1),// Latn + Greek(2),// Grek + Cyrillic(3),// Cyrl + Armenian(4),// Armn + Hebrew(5),// Hebr + Arabic(6),// Arab + Syriac(7),// Syrc + Thaana(8),// Thaa + Devanagari(9),// Deva + Bengali(10),// Beng + Gurmukhi(11),// Guru + Gujarati(12),// Gujr + Oriya(13),// Orya + Tamil(14),// Taml + Telugu(15),// Telu + Kannada(16),// Knda + Malayalam(17),// Mlym + Sinhala(18),// Sinh + Thai(19),// Thai + Lao(20),// Laoo + Tibetan(21),// Tibt + Myanmar(22),// Mymr + Georgian(23),// Geor + Hani(24),// Hani + Ethiopic(25),// Ethi + Cherokee(26),// Cher + Canadian_Aboriginal(27),// Cans + Ogham(28),// Ogam + Runic(29),// Runr + Khmer(30),// Khmr + Mongolian(31),// Mong Undefined_32(32),// Undefined_33(33),// - Bopomofo(34),//Bopo + Bopomofo(34),// Bopo Undefined_35(35),// - Yi(36),//Yiii - Old_Italic(37),//Ital - Gothic(38),//Goth - Deseret(39),//Dsrt - Inherited(40),//Zinh - Tagalog(41),//Tglg - Hanunoo(42),//Hano - Buhid(43),//Buhd - Tagbanwa(44),//Tagb - Limbu(45),//Limb - Tai_Le(46),//Tale - Linear_B(47),//Linb - Ugaritic(48),//Ugar - Shavian(49),//Shaw - Osmanya(50),//Osma - Cypriot(51),//Cprt - Braille(52),//Brai - Buginese(53),//Bugi - Coptic(54),//Copt - New_Tai_Lue(55),//Talu - Glagolitic(56),//Glag - Tifinagh(57),//Tfng - Syloti_Nagri(58),//Sylo - Old_Persian(59),//Xpeo - Kharoshthi(60),//Khar - Balinese(61),//Bali - Cuneiform(62),//Xsux - Phoenician(63),//Phnx - Phags_Pa(64),//Phag - Nko(65),//Nkoo - Sundanese(66),//Sund - Lepcha(67),//Lepc - Ol_Chiki(68),//Olck - Vai(69),//Vaii - Saurashtra(70),//Saur - Kayah_Li(71),//Kali - Rejang(72),//Rjng - Lycian(73),//Lyci - Carian(74),//Cari - Lydian(75),//Lydi - Cham(76),//Cham - Tai_Tham(77),//Lana - Tai_Viet(78),//Tavt - Avestan(79),//Avst - Egyptian_Hieroglyphs(80),//Egyp - Samaritan(81),//Samr - Lisu(82),//Lisu - Bamum(83),//Bamu - Javanese(84),//Java - Meetei_Mayek(85),//Mtei - Imperial_Aramaic(86),//Armi - Old_South_Arabian(87),//Sarb - Inscriptional_Parthian(88),//Prti - Inscriptional_Pahlavi(89),//Phli - Old_Turkic(90),//Orkh - Kaithi(91),//Kthi - Batak(92),//Batk - Brahmi(93),//Brah - Mandaic(94),//Mand - Chakma(95),//Cakm - Meroitic_Cursive(96),//Merc - Meroitic_Hieroglyphs(97),//Mero - Miao(98),//Plrd - Sharada(99),//Shrd - Sora_Sompeng(100),//Sora - Takri(101),//Takr + Yi(36),// Yiii + Old_Italic(37),// Ital + Gothic(38),// Goth + Deseret(39),// Dsrt + Inherited(40),// Zinh + Tagalog(41),// Tglg + Hanunoo(42),// Hano + Buhid(43),// Buhd + Tagbanwa(44),// Tagb + Limbu(45),// Limb + Tai_Le(46),// Tale + Linear_B(47),// Linb + Ugaritic(48),// Ugar + Shavian(49),// Shaw + Osmanya(50),// Osma + Cypriot(51),// Cprt + Braille(52),// Brai + Buginese(53),// Bugi + Coptic(54),// Copt + New_Tai_Lue(55),// Talu + Glagolitic(56),// Glag + Tifinagh(57),// Tfng + Syloti_Nagri(58),// Sylo + Old_Persian(59),// Xpeo + Kharoshthi(60),// Khar + Balinese(61),// Bali + Cuneiform(62),// Xsux + Phoenician(63),// Phnx + Phags_Pa(64),// Phag + Nko(65),// Nkoo + Sundanese(66),// Sund + Lepcha(67),// Lepc + Ol_Chiki(68),// Olck + Vai(69),// Vaii + Saurashtra(70),// Saur + Kayah_Li(71),// Kali + Rejang(72),// Rjng + Lycian(73),// Lyci + Carian(74),// Cari + Lydian(75),// Lydi + Cham(76),// Cham + Tai_Tham(77),// Lana + Tai_Viet(78),// Tavt + Avestan(79),// Avst + Egyptian_Hieroglyphs(80),// Egyp + Samaritan(81),// Samr + Lisu(82),// Lisu + Bamum(83),// Bamu + Javanese(84),// Java + Meetei_Mayek(85),// Mtei + Imperial_Aramaic(86),// Armi + Old_South_Arabian(87),// Sarb + Inscriptional_Parthian(88),// Prti + Inscriptional_Pahlavi(89),// Phli + Old_Turkic(90),// Orkh + Kaithi(91),// Kthi + Batak(92),// Batk + Brahmi(93),// Brah + Mandaic(94),// Mand + Chakma(95),// Cakm + Meroitic_Cursive(96),// Merc + Meroitic_Hieroglyphs(97),// Mero + Miao(98),// Plrd + Sharada(99),// Shrd + Sora_Sompeng(100),// Sora + Takri(101),// Takr MAX_SCRIPT_CODE(102); private final int code; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/customwordembedding/ScriptDetector.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/customwordembedding/ScriptDetector.java index dcf9797c7f12e..148ad1009c9bf 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/customwordembedding/ScriptDetector.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/customwordembedding/ScriptDetector.java @@ -23,18 +23,18 @@ */ public final class ScriptDetector { - private ScriptDetector() { } + private ScriptDetector() {} - // Unicode scripts we care about. To get compact and fast code, we detect only + // Unicode scripts we care about. To get compact and fast code, we detect only // a few Unicode scripts that offer a strong indication about the language of // the text (e.g., Hiragana -> Japanese). public enum Script { // Special value to indicate internal errors in the script detection code. kScriptError(0), - // Special values for all Unicode scripts that we do not detect. One special + // Special values for all Unicode scripts that we do not detect. One special // value for Unicode characters of 1, 2, 3, respectively 4 bytes (as we - // already have that information, we use it). kScriptOtherUtf8OneByte means + // already have that information, we use it). kScriptOtherUtf8OneByte means // ~Latin and kScriptOtherUtf8FourBytes means ~Han. kScriptOtherUtf8OneByte(1), kScriptOtherUtf8TwoBytes(2), diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/ClassificationFeatureImportance.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/ClassificationFeatureImportance.java index 7aae582e12a97..ef918209bf19e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/ClassificationFeatureImportance.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/ClassificationFeatureImportance.java @@ -6,12 +6,12 @@ */ package org.elasticsearch.xpack.core.ml.inference.results; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParseException; @@ -37,16 +37,18 @@ public class ClassificationFeatureImportance extends AbstractFeatureImportance { static final String CLASSES = "classes"; @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("classification_feature_importance", - a -> new ClassificationFeatureImportance((String) a[0], (List) a[1]) - ); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "classification_feature_importance", + a -> new ClassificationFeatureImportance((String) a[0], (List) a[1]) + ); static { PARSER.declareString(constructorArg(), new ParseField(ClassificationFeatureImportance.FEATURE_NAME)); - PARSER.declareObjectArray(optionalConstructorArg(), + PARSER.declareObjectArray( + optionalConstructorArg(), (p, c) -> ClassImportance.fromXContent(p), - new ParseField(ClassificationFeatureImportance.CLASSES)); + new ParseField(ClassificationFeatureImportance.CLASSES) + ); } public static ClassificationFeatureImportance fromXContent(XContentParser parser) { @@ -98,11 +100,14 @@ public Map toMap() { @Override public boolean equals(Object object) { - if (object == this) { return true; } - if (object == null || getClass() != object.getClass()) { return false; } + if (object == this) { + return true; + } + if (object == null || getClass() != object.getClass()) { + return false; + } ClassificationFeatureImportance that = (ClassificationFeatureImportance) object; - return Objects.equals(featureName, that.featureName) - && Objects.equals(classImportance, that.classImportance); + return Objects.equals(featureName, that.featureName) && Objects.equals(classImportance, that.classImportance); } @Override @@ -115,10 +120,10 @@ public static class ClassImportance implements Writeable, ToXContentObject { static final String CLASS_NAME = "class_name"; static final String IMPORTANCE = "importance"; - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("classification_feature_importance_class_importance", - a -> new ClassImportance(a[0], (Double) a[1]) - ); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "classification_feature_importance_class_importance", + a -> new ClassImportance(a[0], (Double) a[1]) + ); static { PARSER.declareField(ConstructingObjectParser.constructorArg(), (p, c) -> { @@ -182,8 +187,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; ClassImportance that = (ClassImportance) o; - return Double.compare(that.importance, importance) == 0 && - Objects.equals(className, that.className); + return Double.compare(that.importance, importance) == 0 && Objects.equals(className, that.className); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/ClassificationInferenceResults.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/ClassificationInferenceResults.java index 584445b7c362c..ebe78b797f020 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/ClassificationInferenceResults.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/ClassificationInferenceResults.java @@ -37,29 +37,35 @@ public class ClassificationInferenceResults extends SingleValueInferenceResults private final List featureImportance; private final PredictionFieldType predictionFieldType; - public ClassificationInferenceResults(double value, - String classificationLabel, - List topClasses, - List featureImportance, - InferenceConfig config, - Double predictionProbability, - Double predictionScore) { - this(value, + public ClassificationInferenceResults( + double value, + String classificationLabel, + List topClasses, + List featureImportance, + InferenceConfig config, + Double predictionProbability, + Double predictionScore + ) { + this( + value, classificationLabel, topClasses, featureImportance, - (ClassificationConfig)config, + (ClassificationConfig) config, predictionProbability, - predictionScore); + predictionScore + ); } - private ClassificationInferenceResults(double value, - String classificationLabel, - List topClasses, - List featureImportance, - ClassificationConfig classificationConfig, - Double predictionProbability, - Double predictionScore) { + private ClassificationInferenceResults( + double value, + String classificationLabel, + List topClasses, + List featureImportance, + ClassificationConfig classificationConfig, + Double predictionProbability, + Double predictionScore + ) { this( value, classificationLabel, @@ -97,13 +103,15 @@ public ClassificationInferenceResults( this.featureImportance = takeTopFeatureImportances(featureImportance, numTopFeatureImportanceValues); } - static List takeTopFeatureImportances(List featureImportances, - int numTopFeatures) { + static List takeTopFeatureImportances( + List featureImportances, + int numTopFeatures + ) { if (featureImportances == null || featureImportances.isEmpty()) { return Collections.emptyList(); } return featureImportances.stream() - .sorted((l, r)-> Double.compare(r.getTotalImportance(), l.getTotalImportance())) + .sorted((l, r) -> Double.compare(r.getTotalImportance(), l.getTotalImportance())) .limit(numTopFeatures) .collect(Collectors.toUnmodifiableList()); } @@ -151,8 +159,12 @@ public void writeTo(StreamOutput out) throws IOException { @Override public boolean equals(Object object) { - if (object == this) { return true; } - if (object == null || getClass() != object.getClass()) { return false; } + if (object == this) { + return true; + } + if (object == null || getClass() != object.getClass()) { + return false; + } ClassificationInferenceResults that = (ClassificationInferenceResults) object; return Objects.equals(value(), that.value()) && Objects.equals(classificationLabel, that.classificationLabel) @@ -167,7 +179,8 @@ public boolean equals(Object object) { @Override public int hashCode() { - return Objects.hash(value(), + return Objects.hash( + value(), classificationLabel, topClasses, resultsField, @@ -175,7 +188,8 @@ public int hashCode() { predictionProbability, predictionScore, featureImportance, - predictionFieldType); + predictionFieldType + ); } @Override @@ -215,8 +229,10 @@ public Map asMap() { map.put(PREDICTION_SCORE, predictionScore); } if (featureImportance.isEmpty() == false) { - map.put(FEATURE_IMPORTANCE, featureImportance.stream().map(ClassificationFeatureImportance::toMap) - .collect(Collectors.toList())); + map.put( + FEATURE_IMPORTANCE, + featureImportance.stream().map(ClassificationFeatureImportance::toMap).collect(Collectors.toList()) + ); } return map; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/InferenceResults.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/InferenceResults.java index 466b60a38f38c..83f08391c656b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/InferenceResults.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/InferenceResults.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ml.inference.results; import org.elasticsearch.common.io.stream.NamedWriteable; -import org.elasticsearch.xcontent.ToXContentFragment; import org.elasticsearch.ingest.IngestDocument; +import org.elasticsearch.xcontent.ToXContentFragment; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; import java.util.Map; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/NerResults.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/NerResults.java index ee07cff858d73..43bb4381d9946 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/NerResults.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/NerResults.java @@ -20,7 +20,6 @@ import java.util.Objects; import java.util.stream.Collectors; - public class NerResults implements InferenceResults { public static final String NAME = "ner_result"; @@ -120,13 +119,7 @@ public static class EntityGroup implements ToXContentObject, Writeable { private final int startPos; private final int endPos; - public EntityGroup( - String entity, - String className, - double classProbability, - int startPos, - int endPos - ) { + public EntityGroup(String entity, String className, double classProbability, int startPos, int endPos) { this.entity = entity; this.className = className; this.classProbability = classProbability; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/RawInferenceResults.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/RawInferenceResults.java index e174e49de6a23..97b2949d0020c 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/RawInferenceResults.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/RawInferenceResults.java @@ -43,11 +43,14 @@ public void writeTo(StreamOutput out) throws IOException { @Override public boolean equals(Object object) { - if (object == this) { return true; } - if (object == null || getClass() != object.getClass()) { return false; } + if (object == this) { + return true; + } + if (object == null || getClass() != object.getClass()) { + return false; + } RawInferenceResults that = (RawInferenceResults) object; - return Arrays.equals(value, that.value) - && Arrays.deepEquals(featureImportance, that.featureImportance); + return Arrays.equals(value, that.value) && Arrays.deepEquals(featureImportance, that.featureImportance); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/RegressionFeatureImportance.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/RegressionFeatureImportance.java index b58481ba8b982..4fbbba9553f68 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/RegressionFeatureImportance.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/RegressionFeatureImportance.java @@ -6,10 +6,10 @@ */ package org.elasticsearch.xpack.core.ml.inference.results; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -26,10 +26,10 @@ public class RegressionFeatureImportance extends AbstractFeatureImportance { static final String IMPORTANCE = "importance"; static final String FEATURE_NAME = "feature_name"; - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("regression_feature_importance", - a -> new RegressionFeatureImportance((String) a[0], (Double) a[1]) - ); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "regression_feature_importance", + a -> new RegressionFeatureImportance((String) a[0], (Double) a[1]) + ); static { PARSER.declareString(constructorArg(), new ParseField(RegressionFeatureImportance.FEATURE_NAME)); @@ -75,11 +75,14 @@ public Map toMap() { @Override public boolean equals(Object object) { - if (object == this) { return true; } - if (object == null || getClass() != object.getClass()) { return false; } + if (object == this) { + return true; + } + if (object == null || getClass() != object.getClass()) { + return false; + } RegressionFeatureImportance that = (RegressionFeatureImportance) object; - return Objects.equals(featureName, that.featureName) - && Objects.equals(importance, that.importance); + return Objects.equals(featureName, that.featureName) && Objects.equals(importance, that.importance); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/RegressionInferenceResults.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/RegressionInferenceResults.java index 95cef180cefa8..7bff1f29245fe 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/RegressionInferenceResults.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/RegressionInferenceResults.java @@ -34,8 +34,8 @@ public RegressionInferenceResults(double value, InferenceConfig config) { public RegressionInferenceResults(double value, InferenceConfig config, List featureImportance) { this( value, - ((RegressionConfig)config).getResultsField(), - ((RegressionConfig)config).getNumTopFeatureImportanceValues(), + ((RegressionConfig) config).getResultsField(), + ((RegressionConfig) config).getNumTopFeatureImportanceValues(), featureImportance ); } @@ -44,25 +44,30 @@ public RegressionInferenceResults(double value, String resultsField) { this(value, resultsField, 0, Collections.emptyList()); } - public RegressionInferenceResults(double value, String resultsField, - List featureImportance) { + public RegressionInferenceResults(double value, String resultsField, List featureImportance) { this(value, resultsField, featureImportance.size(), featureImportance); } - public RegressionInferenceResults(double value, String resultsField, int topNFeatures, - List featureImportance) { + public RegressionInferenceResults( + double value, + String resultsField, + int topNFeatures, + List featureImportance + ) { super(value); this.resultsField = resultsField; this.featureImportance = takeTopFeatureImportances(featureImportance, topNFeatures); } - static List takeTopFeatureImportances(List featureImportances, - int numTopFeatures) { + static List takeTopFeatureImportances( + List featureImportances, + int numTopFeatures + ) { if (featureImportances == null || featureImportances.isEmpty()) { return Collections.emptyList(); } return featureImportances.stream() - .sorted((l, r)-> Double.compare(Math.abs(r.getImportance()), Math.abs(l.getImportance()))) + .sorted((l, r) -> Double.compare(Math.abs(r.getImportance()), Math.abs(l.getImportance()))) .limit(numTopFeatures) .collect(Collectors.toUnmodifiableList()); } @@ -86,8 +91,12 @@ public List getFeatureImportance() { @Override public boolean equals(Object object) { - if (object == this) { return true; } - if (object == null || getClass() != object.getClass()) { return false; } + if (object == this) { + return true; + } + if (object == null || getClass() != object.getClass()) { + return false; + } RegressionInferenceResults that = (RegressionInferenceResults) object; return Objects.equals(value(), that.value()) && Objects.equals(this.resultsField, that.resultsField) diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/SingleValueInferenceResults.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/SingleValueInferenceResults.java index 2c36c1bffa991..0daaa1ebc35a9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/SingleValueInferenceResults.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/SingleValueInferenceResults.java @@ -17,7 +17,6 @@ public abstract class SingleValueInferenceResults implements InferenceResults { private final double value; - SingleValueInferenceResults(StreamInput in) throws IOException { value = in.readDouble(); } @@ -30,7 +29,6 @@ public Double value() { return value; } - public String valueAsString() { return String.valueOf(value); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/TopClassEntry.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/TopClassEntry.java index b4e05dcfdc7c2..4caafa94ef5e0 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/TopClassEntry.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/TopClassEntry.java @@ -7,12 +7,12 @@ package org.elasticsearch.xpack.core.ml.inference.results; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParseException; @@ -34,8 +34,10 @@ public class TopClassEntry implements Writeable, ToXContentObject { public static final String NAME = "top_class"; - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(NAME, a -> new TopClassEntry(a[0], (Double) a[1], (Double) a[2])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + a -> new TopClassEntry(a[0], (Double) a[1], (Double) a[2]) + ); static { PARSER.declareField(constructorArg(), (p, n) -> { @@ -48,9 +50,10 @@ public class TopClassEntry implements Writeable, ToXContentObject { } else if (token == XContentParser.Token.VALUE_NUMBER) { o = p.doubleValue(); } else { - throw new XContentParseException(p.getTokenLocation(), - "[" + NAME + "] failed to parse field [" + CLASS_NAME + "] value [" + token - + "] is not a string, boolean or number"); + throw new XContentParseException( + p.getTokenLocation(), + "[" + NAME + "] failed to parse field [" + CLASS_NAME + "] value [" + token + "] is not a string, boolean or number" + ); } return o; }, CLASS_NAME, ObjectParser.ValueType.VALUE); @@ -121,8 +124,12 @@ public void writeTo(StreamOutput out) throws IOException { @Override public boolean equals(Object object) { - if (object == this) { return true; } - if (object == null || getClass() != object.getClass()) { return false; } + if (object == this) { + return true; + } + if (object == null || getClass() != object.getClass()) { + return false; + } TopClassEntry that = (TopClassEntry) object; return Objects.equals(classification, that.classification) && probability == that.probability && score == that.score; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/WarningInferenceResults.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/WarningInferenceResults.java index de1a60ca4e2ba..fc399b8298335 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/WarningInferenceResults.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/results/WarningInferenceResults.java @@ -6,10 +6,10 @@ */ package org.elasticsearch.xpack.core.ml.inference.results; -import org.elasticsearch.common.logging.LoggerMessageFormat; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.common.logging.LoggerMessageFormat; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; @@ -47,8 +47,12 @@ public void writeTo(StreamOutput out) throws IOException { @Override public boolean equals(Object object) { - if (object == this) { return true; } - if (object == null || getClass() != object.getClass()) { return false; } + if (object == this) { + return true; + } + if (object == null || getClass() != object.getClass()) { + return false; + } WarningInferenceResults that = (WarningInferenceResults) object; return Objects.equals(warning, that.warning); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/BertTokenization.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/BertTokenization.java index 68f33a68c4fb7..58f941ea3e86a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/BertTokenization.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/BertTokenization.java @@ -9,11 +9,11 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.core.Nullable; import java.io.IOException; @@ -29,7 +29,7 @@ public static ConstructingObjectParser createParser(bool (Boolean) a[0], (Boolean) a[1], (Integer) a[2], - a[3] == null ? null : Truncate.fromString((String)a[3]) + a[3] == null ? null : Truncate.fromString((String) a[3]) ) ); Tokenization.declareCommonFields(parser); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ClassificationConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ClassificationConfig.java index ea5cd6a9c6287..f05cef7f4a67d 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ClassificationConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ClassificationConfig.java @@ -7,10 +7,10 @@ package org.elasticsearch.xpack.core.ml.inference.trainedmodel; import org.elasticsearch.Version; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -28,8 +28,13 @@ public class ClassificationConfig implements LenientlyParsedInferenceConfig, Str public static final ParseField PREDICTION_FIELD_TYPE = new ParseField("prediction_field_type"); private static final Version MIN_SUPPORTED_VERSION = Version.V_7_6_0; - public static ClassificationConfig EMPTY_PARAMS = - new ClassificationConfig(0, DEFAULT_RESULTS_FIELD, DEFAULT_TOP_CLASSES_RESULTS_FIELD, null, null); + public static ClassificationConfig EMPTY_PARAMS = new ClassificationConfig( + 0, + DEFAULT_RESULTS_FIELD, + DEFAULT_TOP_CLASSES_RESULTS_FIELD, + null, + null + ); private final int numTopClasses; private final String topClassesResultsField; @@ -44,13 +49,13 @@ private static ObjectParser createParser(boo ObjectParser parser = new ObjectParser<>( NAME.getPreferredName(), lenient, - ClassificationConfig.Builder::new); + ClassificationConfig.Builder::new + ); parser.declareInt(ClassificationConfig.Builder::setNumTopClasses, NUM_TOP_CLASSES); parser.declareString(ClassificationConfig.Builder::setResultsField, RESULTS_FIELD); parser.declareString(ClassificationConfig.Builder::setTopClassesResultsField, TOP_CLASSES_RESULTS_FIELD); parser.declareInt(ClassificationConfig.Builder::setNumTopFeatureImportanceValues, NUM_TOP_FEATURE_IMPORTANCE_VALUES); - parser.declareField(ClassificationConfig.Builder::setPredictionFieldType, - (p, c) -> { + parser.declareField(ClassificationConfig.Builder::setPredictionFieldType, (p, c) -> { try { return PredictionFieldType.fromString(p.text()); } catch (IllegalArgumentException iae) { @@ -75,17 +80,20 @@ public ClassificationConfig(Integer numTopClasses) { this(numTopClasses, null, null, null, null); } - public ClassificationConfig(Integer numTopClasses, - String resultsField, - String topClassesResultsField, - Integer featureImportance, - PredictionFieldType predictionFieldType) { + public ClassificationConfig( + Integer numTopClasses, + String resultsField, + String topClassesResultsField, + Integer featureImportance, + PredictionFieldType predictionFieldType + ) { this.numTopClasses = numTopClasses == null ? 0 : numTopClasses; this.topClassesResultsField = topClassesResultsField == null ? DEFAULT_TOP_CLASSES_RESULTS_FIELD : topClassesResultsField; this.resultsField = resultsField == null ? DEFAULT_RESULTS_FIELD : resultsField; if (featureImportance != null && featureImportance < 0) { - throw new IllegalArgumentException("[" + NUM_TOP_FEATURE_IMPORTANCE_VALUES.getPreferredName() + - "] must be greater than or equal to 0"); + throw new IllegalArgumentException( + "[" + NUM_TOP_FEATURE_IMPORTANCE_VALUES.getPreferredName() + "] must be greater than or equal to 0" + ); } this.numTopFeatureImportanceValues = featureImportance == null ? 0 : featureImportance; this.predictionFieldType = predictionFieldType == null ? PredictionFieldType.STRING : predictionFieldType; @@ -235,11 +243,13 @@ public Builder setPredictionFieldType(PredictionFieldType predictionFieldType) { } public ClassificationConfig build() { - return new ClassificationConfig(numTopClasses, + return new ClassificationConfig( + numTopClasses, resultsField, topClassesResultsField, numTopFeatureImportanceValues, - predictionFieldType); + predictionFieldType + ); } } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ClassificationConfigUpdate.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ClassificationConfigUpdate.java index 602931ee6947c..55d1084553fd6 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ClassificationConfigUpdate.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ClassificationConfigUpdate.java @@ -6,10 +6,10 @@ */ package org.elasticsearch.xpack.core.ml.inference.trainedmodel; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; @@ -30,8 +30,7 @@ public class ClassificationConfigUpdate implements InferenceConfigUpdate, NamedX public static final ParseField NAME = ClassificationConfig.NAME; - public static ClassificationConfigUpdate EMPTY_PARAMS = - new ClassificationConfigUpdate(null, null, null, null, null); + public static ClassificationConfigUpdate EMPTY_PARAMS = new ClassificationConfigUpdate(null, null, null, null, null); private final Integer numTopClasses; private final String topClassesResultsField; @@ -41,37 +40,38 @@ public class ClassificationConfigUpdate implements InferenceConfigUpdate, NamedX public static ClassificationConfigUpdate fromMap(Map map) { Map options = new HashMap<>(map); - Integer numTopClasses = (Integer)options.remove(NUM_TOP_CLASSES.getPreferredName()); - String topClassesResultsField = (String)options.remove(TOP_CLASSES_RESULTS_FIELD.getPreferredName()); - String resultsField = (String)options.remove(RESULTS_FIELD.getPreferredName()); - Integer featureImportance = (Integer)options.remove(NUM_TOP_FEATURE_IMPORTANCE_VALUES.getPreferredName()); - String predictionFieldTypeStr = (String)options.remove(PREDICTION_FIELD_TYPE.getPreferredName()); + Integer numTopClasses = (Integer) options.remove(NUM_TOP_CLASSES.getPreferredName()); + String topClassesResultsField = (String) options.remove(TOP_CLASSES_RESULTS_FIELD.getPreferredName()); + String resultsField = (String) options.remove(RESULTS_FIELD.getPreferredName()); + Integer featureImportance = (Integer) options.remove(NUM_TOP_FEATURE_IMPORTANCE_VALUES.getPreferredName()); + String predictionFieldTypeStr = (String) options.remove(PREDICTION_FIELD_TYPE.getPreferredName()); if (options.isEmpty() == false) { throw ExceptionsHelper.badRequestException("Unrecognized fields {}.", options.keySet()); } - return new ClassificationConfigUpdate(numTopClasses, + return new ClassificationConfigUpdate( + numTopClasses, resultsField, topClassesResultsField, featureImportance, - predictionFieldTypeStr == null ? null : PredictionFieldType.fromString(predictionFieldTypeStr)); + predictionFieldTypeStr == null ? null : PredictionFieldType.fromString(predictionFieldTypeStr) + ); } public static ClassificationConfigUpdate fromConfig(ClassificationConfig config) { - return new ClassificationConfigUpdate(config.getNumTopClasses(), + return new ClassificationConfigUpdate( + config.getNumTopClasses(), config.getResultsField(), config.getTopClassesResultsField(), config.getNumTopFeatureImportanceValues(), - config.getPredictionFieldType()); + config.getPredictionFieldType() + ); } private static final ObjectParser STRICT_PARSER = createParser(false); private static ObjectParser createParser(boolean lenient) { - ObjectParser parser = new ObjectParser<>( - NAME.getPreferredName(), - lenient, - Builder::new); + ObjectParser parser = new ObjectParser<>(NAME.getPreferredName(), lenient, Builder::new); parser.declareInt(Builder::setNumTopClasses, NUM_TOP_CLASSES); parser.declareString(Builder::setResultsField, RESULTS_FIELD); parser.declareString(Builder::setTopClassesResultsField, TOP_CLASSES_RESULTS_FIELD); @@ -84,17 +84,20 @@ public static ClassificationConfigUpdate fromXContentStrict(XContentParser parse return STRICT_PARSER.apply(parser, null).build(); } - public ClassificationConfigUpdate(Integer numTopClasses, - String resultsField, - String topClassesResultsField, - Integer featureImportance, - PredictionFieldType predictionFieldType) { + public ClassificationConfigUpdate( + Integer numTopClasses, + String resultsField, + String topClassesResultsField, + Integer featureImportance, + PredictionFieldType predictionFieldType + ) { this.numTopClasses = numTopClasses; this.topClassesResultsField = topClassesResultsField; this.resultsField = resultsField; if (featureImportance != null && featureImportance < 0) { - throw new IllegalArgumentException("[" + NUM_TOP_FEATURE_IMPORTANCE_VALUES.getPreferredName() + - "] must be greater than or equal to 0"); + throw new IllegalArgumentException( + "[" + NUM_TOP_FEATURE_IMPORTANCE_VALUES.getPreferredName() + "] must be greater than or equal to 0" + ); } this.numTopFeatureImportanceValues = featureImportance; this.predictionFieldType = predictionFieldType; @@ -124,8 +127,7 @@ public String getResultsField() { @Override public InferenceConfigUpdate.Builder, ? extends InferenceConfigUpdate> newBuilder() { - return new Builder() - .setNumTopClasses(numTopClasses) + return new Builder().setNumTopClasses(numTopClasses) .setTopClassesResultsField(topClassesResultsField) .setResultsField(resultsField) .setNumTopFeatureImportanceValues(numTopFeatureImportanceValues) @@ -204,9 +206,10 @@ public InferenceConfig apply(InferenceConfig originalConfig) { throw ExceptionsHelper.badRequestException( "Inference config of type [{}] can not be updated with a inference request of type [{}]", originalConfig.getName(), - getName()); + getName() + ); } - ClassificationConfig classificationConfig = (ClassificationConfig)originalConfig; + ClassificationConfig classificationConfig = (ClassificationConfig) originalConfig; if (isNoop(classificationConfig)) { return originalConfig; @@ -242,8 +245,7 @@ public boolean isSupported(InferenceConfig inferenceConfig) { boolean isNoop(ClassificationConfig originalConfig) { return (resultsField == null || resultsField.equals(originalConfig.getResultsField())) - && (numTopFeatureImportanceValues == null - || originalConfig.getNumTopFeatureImportanceValues() == numTopFeatureImportanceValues) + && (numTopFeatureImportanceValues == null || originalConfig.getNumTopFeatureImportanceValues() == numTopFeatureImportanceValues) && (topClassesResultsField == null || topClassesResultsField.equals(originalConfig.getTopClassesResultsField())) && (numTopClasses == null || originalConfig.getNumTopClasses() == numTopClasses) && (predictionFieldType == null || predictionFieldType.equals(originalConfig.getPredictionFieldType())); @@ -288,11 +290,13 @@ private Builder setPredictionFieldType(String predictionFieldType) { @Override public ClassificationConfigUpdate build() { - return new ClassificationConfigUpdate(numTopClasses, + return new ClassificationConfigUpdate( + numTopClasses, resultsField, topClassesResultsField, numTopFeatureImportanceValues, - predictionFieldType); + predictionFieldType + ); } } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/EmptyConfigUpdate.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/EmptyConfigUpdate.java index df7501e49d25c..7ae48c09cd501 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/EmptyConfigUpdate.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/EmptyConfigUpdate.java @@ -21,11 +21,9 @@ public static Version minimumSupportedVersion() { return Version.V_7_9_0; } - public EmptyConfigUpdate() { - } + public EmptyConfigUpdate() {} - public EmptyConfigUpdate(StreamInput in) { - } + public EmptyConfigUpdate(StreamInput in) {} @Override public String getResultsField() { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/FillMaskConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/FillMaskConfig.java index e47b6405e5f91..babf006d31ac3 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/FillMaskConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/FillMaskConfig.java @@ -10,10 +10,10 @@ import org.elasticsearch.Version; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ObjectParser; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.core.Nullable; import org.elasticsearch.xpack.core.ml.inference.persistence.InferenceIndexConstants; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; import org.elasticsearch.xpack.core.ml.utils.NamedXContentObjectHelper; @@ -40,22 +40,19 @@ public static FillMaskConfig fromXContentLenient(XContentParser parser) { private static ObjectParser createParser(boolean ignoreUnknownFields) { ObjectParser parser = new ObjectParser<>(NAME, ignoreUnknownFields, Builder::new); - parser.declareObject( - Builder::setVocabularyConfig, - (p, c) -> { - if (ignoreUnknownFields == false) { - throw ExceptionsHelper.badRequestException( - "illegal setting [{}] on inference model creation", - VOCABULARY.getPreferredName() - ); - } - return VocabularyConfig.fromXContentLenient(p); - }, - VOCABULARY - ); + parser.declareObject(Builder::setVocabularyConfig, (p, c) -> { + if (ignoreUnknownFields == false) { + throw ExceptionsHelper.badRequestException( + "illegal setting [{}] on inference model creation", + VOCABULARY.getPreferredName() + ); + } + return VocabularyConfig.fromXContentLenient(p); + }, VOCABULARY); parser.declareNamedObject( - Builder::setTokenization, (p, c, n) -> p.namedObject(Tokenization.class, n, ignoreUnknownFields), - TOKENIZATION + Builder::setTokenization, + (p, c, n) -> p.namedObject(Tokenization.class, n, ignoreUnknownFields), + TOKENIZATION ); parser.declareInt(Builder::setNumTopClasses, NUM_TOP_CLASSES); parser.declareString(Builder::setResultsField, RESULTS_FIELD); @@ -67,10 +64,12 @@ private static ObjectParser createParser(boolean i private final int numTopClasses; private final String resultsField; - public FillMaskConfig(@Nullable VocabularyConfig vocabularyConfig, - @Nullable Tokenization tokenization, - @Nullable Integer numTopClasses, - @Nullable String resultsField) { + public FillMaskConfig( + @Nullable VocabularyConfig vocabularyConfig, + @Nullable Tokenization tokenization, + @Nullable Integer numTopClasses, + @Nullable String resultsField + ) { this.vocabularyConfig = Optional.ofNullable(vocabularyConfig) .orElse(new VocabularyConfig(InferenceIndexConstants.nativeDefinitionStore())); this.tokenization = tokenization == null ? Tokenization.createDefault() : tokenization; @@ -203,10 +202,7 @@ public FillMaskConfig.Builder setResultsField(String resultsField) { } public FillMaskConfig build() { - return new FillMaskConfig(vocabularyConfig, - tokenization, - numTopClasses, - resultsField); + return new FillMaskConfig(vocabularyConfig, tokenization, numTopClasses, resultsField); } } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/FillMaskConfigUpdate.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/FillMaskConfigUpdate.java index 9c85af9e294b4..10fb9be7a5d93 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/FillMaskConfigUpdate.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/FillMaskConfigUpdate.java @@ -29,8 +29,8 @@ public class FillMaskConfigUpdate extends NlpConfigUpdate implements NamedXConte public static FillMaskConfigUpdate fromMap(Map map) { Map options = new HashMap<>(map); - Integer numTopClasses = (Integer)options.remove(NUM_TOP_CLASSES.getPreferredName()); - String resultsField = (String)options.remove(RESULTS_FIELD.getPreferredName()); + Integer numTopClasses = (Integer) options.remove(NUM_TOP_CLASSES.getPreferredName()); + String resultsField = (String) options.remove(RESULTS_FIELD.getPreferredName()); if (options.isEmpty() == false) { throw ExceptionsHelper.badRequestException("Unrecognized fields {}.", options.keySet()); @@ -41,10 +41,7 @@ public static FillMaskConfigUpdate fromMap(Map map) { private static final ObjectParser STRICT_PARSER = createParser(false); private static ObjectParser createParser(boolean lenient) { - ObjectParser parser = new ObjectParser<>( - NAME, - lenient, - FillMaskConfigUpdate.Builder::new); + ObjectParser parser = new ObjectParser<>(NAME, lenient, FillMaskConfigUpdate.Builder::new); parser.declareString(FillMaskConfigUpdate.Builder::setResultsField, RESULTS_FIELD); parser.declareInt(FillMaskConfigUpdate.Builder::setNumTopClasses, NUM_TOP_CLASSES); return parser; @@ -102,10 +99,11 @@ public InferenceConfig apply(InferenceConfig originalConfig) { throw ExceptionsHelper.badRequestException( "Inference config of type [{}] can not be updated with a request of type [{}]", originalConfig.getName(), - getName()); + getName() + ); } - FillMaskConfig fillMaskConfig = (FillMaskConfig)originalConfig; + FillMaskConfig fillMaskConfig = (FillMaskConfig) originalConfig; if (isNoop(fillMaskConfig)) { return originalConfig; } @@ -121,8 +119,8 @@ public InferenceConfig apply(InferenceConfig originalConfig) { } boolean isNoop(FillMaskConfig originalConfig) { - return (this.numTopClasses == null || this.numTopClasses == originalConfig.getNumTopClasses()) && - (this.resultsField == null || this.resultsField.equals(originalConfig.getResultsField())); + return (this.numTopClasses == null || this.numTopClasses == originalConfig.getNumTopClasses()) + && (this.resultsField == null || this.resultsField.equals(originalConfig.getResultsField())); } @Override @@ -137,9 +135,7 @@ public String getResultsField() { @Override public InferenceConfigUpdate.Builder, ? extends InferenceConfigUpdate> newBuilder() { - return new Builder() - .setNumTopClasses(numTopClasses) - .setResultsField(resultsField); + return new Builder().setNumTopClasses(numTopClasses).setResultsField(resultsField); } @Override @@ -147,8 +143,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; FillMaskConfigUpdate that = (FillMaskConfigUpdate) o; - return Objects.equals(numTopClasses, that.numTopClasses) && - Objects.equals(resultsField, that.resultsField); + return Objects.equals(numTopClasses, that.numTopClasses) && Objects.equals(resultsField, that.resultsField); } @Override @@ -156,8 +151,7 @@ public int hashCode() { return Objects.hash(numTopClasses, resultsField); } - public static class Builder - implements InferenceConfigUpdate.Builder { + public static class Builder implements InferenceConfigUpdate.Builder { private Integer numTopClasses; private String resultsField; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/IndexLocation.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/IndexLocation.java index 275c88ace08a2..cedbeb739bf82 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/IndexLocation.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/IndexLocation.java @@ -7,10 +7,10 @@ package org.elasticsearch.xpack.core.ml.inference.trainedmodel; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -29,7 +29,8 @@ private static ConstructingObjectParser createParser(boolea ConstructingObjectParser parser = new ConstructingObjectParser<>( NAME.getPreferredName(), lenient, - a -> new IndexLocation((String) a[0])); + a -> new IndexLocation((String) a[0]) + ); parser.declareString(ConstructingObjectParser.constructorArg(), NAME); return parser; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/InferenceConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/InferenceConfig.java index 942c39ad414f8..a5900a9a615b5 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/InferenceConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/InferenceConfig.java @@ -10,7 +10,6 @@ import org.elasticsearch.common.io.stream.NamedWriteable; import org.elasticsearch.xpack.core.ml.utils.NamedXContentObject; - public interface InferenceConfig extends NamedXContentObject, NamedWriteable { String DEFAULT_TOP_CLASSES_RESULTS_FIELD = "top_classes"; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/InferenceConfigUpdate.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/InferenceConfigUpdate.java index f209066ed9a10..667594ae9fe4e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/InferenceConfigUpdate.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/InferenceConfigUpdate.java @@ -15,11 +15,10 @@ import java.util.HashSet; import java.util.Set; - public interface InferenceConfigUpdate extends NamedWriteable { - Set RESERVED_ML_FIELD_NAMES = new HashSet<>(Arrays.asList( - WarningInferenceResults.WARNING.getPreferredName(), - TrainedModelConfig.MODEL_ID.getPreferredName())); + Set RESERVED_ML_FIELD_NAMES = new HashSet<>( + Arrays.asList(WarningInferenceResults.WARNING.getPreferredName(), TrainedModelConfig.MODEL_ID.getPreferredName()) + ); InferenceConfig apply(InferenceConfig originalConfig); @@ -31,6 +30,7 @@ public interface InferenceConfigUpdate extends NamedWriteable { interface Builder, U extends InferenceConfigUpdate> { U build(); + T setResultsField(String resultsField); } @@ -43,7 +43,7 @@ default String getName() { static void checkFieldUniqueness(String... fieldNames) { Set duplicatedFieldNames = new HashSet<>(); Set currentFieldNames = new HashSet<>(RESERVED_ML_FIELD_NAMES); - for(String fieldName : fieldNames) { + for (String fieldName : fieldNames) { if (fieldName == null) { continue; } @@ -54,9 +54,10 @@ static void checkFieldUniqueness(String... fieldNames) { } } if (duplicatedFieldNames.isEmpty() == false) { - throw ExceptionsHelper.badRequestException("Invalid inference config." + - " More than one field is configured as {}", - duplicatedFieldNames); + throw ExceptionsHelper.badRequestException( + "Invalid inference config." + " More than one field is configured as {}", + duplicatedFieldNames + ); } } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/InferenceHelpers.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/InferenceHelpers.java index 53a297a1c9ece..0011abb45fc70 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/InferenceHelpers.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/InferenceHelpers.java @@ -25,58 +25,62 @@ public final class InferenceHelpers { - private InferenceHelpers() { } + private InferenceHelpers() {} /** * @return Tuple of the highest scored index and the top classes */ - public static Tuple> topClasses(double[] probabilities, - List classificationLabels, - @Nullable double[] classificationWeights, - int numToInclude, - PredictionFieldType predictionFieldType) { + public static Tuple> topClasses( + double[] probabilities, + List classificationLabels, + @Nullable double[] classificationWeights, + int numToInclude, + PredictionFieldType predictionFieldType + ) { if (classificationLabels != null && probabilities.length != classificationLabels.size()) { - throw ExceptionsHelper - .serverError( - "model returned classification probabilities of size [{}] which is not equal to classification labels size [{}]", - null, - probabilities.length, - classificationLabels.size()); + throw ExceptionsHelper.serverError( + "model returned classification probabilities of size [{}] which is not equal to classification labels size [{}]", + null, + probabilities.length, + classificationLabels.size() + ); } - double[] scores = classificationWeights == null ? - probabilities : - IntStream.range(0, probabilities.length) - .mapToDouble(i -> probabilities[i] * classificationWeights[i]) - .toArray(); + double[] scores = classificationWeights == null + ? probabilities + : IntStream.range(0, probabilities.length).mapToDouble(i -> probabilities[i] * classificationWeights[i]).toArray(); int[] sortedIndices = IntStream.range(0, scores.length) .boxed() - .sorted(Comparator.comparing(i -> scores[(Integer)i]).reversed()) + .sorted(Comparator.comparing(i -> scores[(Integer) i]).reversed()) .mapToInt(i -> i) .toArray(); - final TopClassificationValue topClassificationValue = new TopClassificationValue(sortedIndices[0], + final TopClassificationValue topClassificationValue = new TopClassificationValue( + sortedIndices[0], probabilities[sortedIndices[0]], - scores[sortedIndices[0]]); + scores[sortedIndices[0]] + ); if (numToInclude == 0) { return Tuple.tuple(topClassificationValue, Collections.emptyList()); } List labels = classificationLabels == null ? - // If we don't have the labels we should return the top classification values anyways, they will just be numeric - IntStream.range(0, probabilities.length).boxed().map(String::valueOf).collect(Collectors.toList()) : - classificationLabels; + // If we don't have the labels we should return the top classification values anyways, they will just be numeric + IntStream.range(0, probabilities.length).boxed().map(String::valueOf).collect(Collectors.toList()) : classificationLabels; int count = numToInclude < 0 ? probabilities.length : Math.min(numToInclude, probabilities.length); List topClassEntries = new ArrayList<>(count); - for(int i = 0; i < count; i++) { + for (int i = 0; i < count; i++) { int idx = sortedIndices[i]; - topClassEntries.add(new TopClassEntry( - predictionFieldType.transformPredictedValue((double)idx, labels.get(idx)), - probabilities[idx], - scores[idx])); + topClassEntries.add( + new TopClassEntry( + predictionFieldType.transformPredictedValue((double) idx, labels.get(idx)), + probabilities[idx], + scores[idx] + ) + ); } return Tuple.tuple(topClassificationValue, topClassEntries); @@ -91,14 +95,15 @@ public static String classificationLabel(Integer inferenceValue, @Nullable List< "model returned classification value of [{}] which is not a valid index in classification labels [{}]", null, inferenceValue, - classificationLabels); + classificationLabels + ); } return classificationLabels.get(inferenceValue); } public static Double toDouble(Object value) { if (value instanceof Number) { - return ((Number)value).doubleValue(); + return ((Number) value).doubleValue(); } if (value instanceof String) { return stringToDouble((String) value); @@ -118,8 +123,10 @@ private static Double stringToDouble(String value) { } } - public static Map decodeFeatureImportances(Map processedFeatureToOriginalFeatureMap, - Map featureImportances) { + public static Map decodeFeatureImportances( + Map processedFeatureToOriginalFeatureMap, + Map featureImportances + ) { if (processedFeatureToOriginalFeatureMap == null || processedFeatureToOriginalFeatureMap.isEmpty()) { return featureImportances; } @@ -139,9 +146,10 @@ public static List transformFeatureImportanceRegres } public static List transformFeatureImportanceClassification( - Map featureImportance, - @Nullable List classificationLabels, - @Nullable PredictionFieldType predictionFieldType) { + Map featureImportance, + @Nullable List classificationLabels, + @Nullable PredictionFieldType predictionFieldType + ) { List importances = new ArrayList<>(featureImportance.size()); final PredictionFieldType fieldType = predictionFieldType == null ? PredictionFieldType.STRING : predictionFieldType; featureImportance.forEach((k, v) -> { @@ -154,24 +162,24 @@ public static List transformFeatureImportanceCl // These leaves indicate which direction the feature pulls the value // The original importance is an indication of how it pushes or pulls the value towards or from `1` // To get the importance for the `0` class, we simply invert it. - importances.add(new ClassificationFeatureImportance(k, - Arrays.asList( - new ClassificationFeatureImportance.ClassImportance( - fieldType.transformPredictedValue(0.0, zeroLabel), - -v[0]), - new ClassificationFeatureImportance.ClassImportance( - fieldType.transformPredictedValue(1.0, oneLabel), - v[0]) - ))); + importances.add( + new ClassificationFeatureImportance( + k, + Arrays.asList( + new ClassificationFeatureImportance.ClassImportance(fieldType.transformPredictedValue(0.0, zeroLabel), -v[0]), + new ClassificationFeatureImportance.ClassImportance(fieldType.transformPredictedValue(1.0, oneLabel), v[0]) + ) + ) + ); } else { List classImportance = new ArrayList<>(v.length); // If the classificationLabels exist, their length must match leaf_value length assert classificationLabels == null || classificationLabels.size() == v.length; for (int i = 0; i < v.length; i++) { String label = classificationLabels == null ? null : classificationLabels.get(i); - classImportance.add(new ClassificationFeatureImportance.ClassImportance( - fieldType.transformPredictedValue((double)i, label), - v[i])); + classImportance.add( + new ClassificationFeatureImportance.ClassImportance(fieldType.transformPredictedValue((double) i, label), v[i]) + ); } importances.add(new ClassificationFeatureImportance(k, classImportance)); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/InferenceStats.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/InferenceStats.java index 485be68329f8f..5314702be0688 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/InferenceStats.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/InferenceStats.java @@ -6,13 +6,13 @@ */ package org.elasticsearch.xpack.core.ml.inference.trainedmodel; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.common.time.TimeUtils; @@ -37,7 +37,7 @@ public class InferenceStats implements ToXContentObject, Writeable { public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( NAME, true, - a -> new InferenceStats((Long)a[0], (Long)a[1], (Long)a[2], (Long)a[3], (String)a[4], (String)a[5], (Instant)a[6]) + a -> new InferenceStats((Long) a[0], (Long) a[1], (Long) a[2], (Long) a[3], (String) a[4], (String) a[5], (Instant) a[6]) ); static { PARSER.declareLong(ConstructingObjectParser.constructorArg(), MISSING_ALL_FIELDS_COUNT); @@ -46,10 +46,12 @@ public class InferenceStats implements ToXContentObject, Writeable { PARSER.declareLong(ConstructingObjectParser.optionalConstructorArg(), CACHE_MISS_COUNT); PARSER.declareString(ConstructingObjectParser.constructorArg(), MODEL_ID); PARSER.declareString(ConstructingObjectParser.constructorArg(), NODE_ID); - PARSER.declareField(ConstructingObjectParser.constructorArg(), + PARSER.declareField( + ConstructingObjectParser.constructorArg(), p -> TimeUtils.parseTimeFieldToInstant(p, TIMESTAMP.getPreferredName()), TIMESTAMP, - ObjectParser.ValueType.VALUE); + ObjectParser.ValueType.VALUE + ); } public static String docId(String modelId, String nodeId) { @@ -64,38 +66,44 @@ public static String docId(String modelId, String nodeId) { private final String nodeId; private final Instant timeStamp; - private InferenceStats(Long missingAllFieldsCount, - Long inferenceCount, - Long failureCount, - Long cacheMissCount, - String modelId, - String nodeId, - Instant instant) { - this(unboxOrZero(missingAllFieldsCount), + private InferenceStats( + Long missingAllFieldsCount, + Long inferenceCount, + Long failureCount, + Long cacheMissCount, + String modelId, + String nodeId, + Instant instant + ) { + this( + unboxOrZero(missingAllFieldsCount), unboxOrZero(inferenceCount), unboxOrZero(failureCount), unboxOrZero(cacheMissCount), modelId, nodeId, - instant); + instant + ); } - public InferenceStats(long missingAllFieldsCount, - long inferenceCount, - long failureCount, - long cacheMissCount, - String modelId, - String nodeId, - Instant timeStamp) { + public InferenceStats( + long missingAllFieldsCount, + long inferenceCount, + long failureCount, + long cacheMissCount, + String modelId, + String nodeId, + Instant timeStamp + ) { this.missingAllFieldsCount = missingAllFieldsCount; this.inferenceCount = inferenceCount; this.failureCount = failureCount; this.cacheMissCount = cacheMissCount; this.modelId = modelId; this.nodeId = nodeId; - this.timeStamp = timeStamp == null ? - Instant.ofEpochMilli(Instant.now().toEpochMilli()) : - Instant.ofEpochMilli(timeStamp.toEpochMilli()); + this.timeStamp = timeStamp == null + ? Instant.ofEpochMilli(Instant.now().toEpochMilli()) + : Instant.ofEpochMilli(timeStamp.toEpochMilli()); } public InferenceStats(StreamInput in) throws IOException { @@ -180,15 +188,24 @@ public int hashCode() { @Override public String toString() { - return "InferenceStats{" + - "missingAllFieldsCount=" + missingAllFieldsCount + - ", inferenceCount=" + inferenceCount + - ", failureCount=" + failureCount + - ", cacheMissCount=" + cacheMissCount + - ", modelId='" + modelId + '\'' + - ", nodeId='" + nodeId + '\'' + - ", timeStamp=" + timeStamp + - '}'; + return "InferenceStats{" + + "missingAllFieldsCount=" + + missingAllFieldsCount + + ", inferenceCount=" + + inferenceCount + + ", failureCount=" + + failureCount + + ", cacheMissCount=" + + cacheMissCount + + ", modelId='" + + modelId + + '\'' + + ", nodeId='" + + nodeId + + '\'' + + ", timeStamp=" + + timeStamp + + '}'; } private static long unboxOrZero(@Nullable Long value) { @@ -279,13 +296,15 @@ public synchronized InferenceStats currentStatsAndReset() { } public InferenceStats currentStats(Instant timeStamp) { - return new InferenceStats(missingFieldsAccumulator, + return new InferenceStats( + missingFieldsAccumulator, inferenceAccumulator, failureCountAccumulator, cacheMissAccumulator, modelId, nodeId, - timeStamp); + timeStamp + ); } } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/LenientlyParsedInferenceConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/LenientlyParsedInferenceConfig.java index 2674cfb26538b..8d348e276e71e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/LenientlyParsedInferenceConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/LenientlyParsedInferenceConfig.java @@ -6,5 +6,4 @@ */ package org.elasticsearch.xpack.core.ml.inference.trainedmodel; -public interface LenientlyParsedInferenceConfig extends InferenceConfig { -} +public interface LenientlyParsedInferenceConfig extends InferenceConfig {} diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/LenientlyParsedTrainedModel.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/LenientlyParsedTrainedModel.java index 73c7ca11dcc92..5356d59cc4d2a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/LenientlyParsedTrainedModel.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/LenientlyParsedTrainedModel.java @@ -6,5 +6,4 @@ */ package org.elasticsearch.xpack.core.ml.inference.trainedmodel; -public interface LenientlyParsedTrainedModel extends TrainedModel { -} +public interface LenientlyParsedTrainedModel extends TrainedModel {} diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/LenientlyParsedTrainedModelLocation.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/LenientlyParsedTrainedModelLocation.java index c70062416dbda..afd619eb512f9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/LenientlyParsedTrainedModelLocation.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/LenientlyParsedTrainedModelLocation.java @@ -7,5 +7,4 @@ package org.elasticsearch.xpack.core.ml.inference.trainedmodel; -public interface LenientlyParsedTrainedModelLocation extends TrainedModelLocation{ -} +public interface LenientlyParsedTrainedModelLocation extends TrainedModelLocation {} diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/NerConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/NerConfig.java index 1f7e8b721da69..9c2ba0409914d 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/NerConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/NerConfig.java @@ -10,10 +10,10 @@ import org.elasticsearch.Version; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.core.Nullable; import org.elasticsearch.xpack.core.ml.inference.persistence.InferenceIndexConstants; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; import org.elasticsearch.xpack.core.ml.utils.NamedXContentObjectHelper; @@ -39,26 +39,26 @@ public static NerConfig fromXContentLenient(XContentParser parser) { private static final ConstructingObjectParser STRICT_PARSER = createParser(false); private static final ConstructingObjectParser LENIENT_PARSER = createParser(true); - @SuppressWarnings({ "unchecked"}) + @SuppressWarnings({ "unchecked" }) private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { - ConstructingObjectParser parser = new ConstructingObjectParser<>(NAME, ignoreUnknownFields, - a -> new NerConfig((VocabularyConfig) a[0], (Tokenization) a[1], (List) a[2], (String) a[3])); - parser.declareObject( - ConstructingObjectParser.optionalConstructorArg(), - (p, c) -> { - if (ignoreUnknownFields == false) { - throw ExceptionsHelper.badRequestException( - "illegal setting [{}] on inference model creation", - VOCABULARY.getPreferredName() - ); - } - return VocabularyConfig.fromXContentLenient(p); - }, - VOCABULARY + ConstructingObjectParser parser = new ConstructingObjectParser<>( + NAME, + ignoreUnknownFields, + a -> new NerConfig((VocabularyConfig) a[0], (Tokenization) a[1], (List) a[2], (String) a[3]) ); + parser.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> { + if (ignoreUnknownFields == false) { + throw ExceptionsHelper.badRequestException( + "illegal setting [{}] on inference model creation", + VOCABULARY.getPreferredName() + ); + } + return VocabularyConfig.fromXContentLenient(p); + }, VOCABULARY); parser.declareNamedObject( - ConstructingObjectParser.optionalConstructorArg(), (p, c, n) -> p.namedObject(Tokenization.class, n, ignoreUnknownFields), - TOKENIZATION + ConstructingObjectParser.optionalConstructorArg(), + (p, c, n) -> p.namedObject(Tokenization.class, n, ignoreUnknownFields), + TOKENIZATION ); parser.declareStringArray(ConstructingObjectParser.optionalConstructorArg(), CLASSIFICATION_LABELS); parser.declareString(ConstructingObjectParser.optionalConstructorArg(), RESULTS_FIELD); @@ -70,10 +70,12 @@ private static ConstructingObjectParser createParser(boolean ig private final List classificationLabels; private final String resultsField; - public NerConfig(@Nullable VocabularyConfig vocabularyConfig, - @Nullable Tokenization tokenization, - @Nullable List classificationLabels, - @Nullable String resultsField) { + public NerConfig( + @Nullable VocabularyConfig vocabularyConfig, + @Nullable Tokenization tokenization, + @Nullable List classificationLabels, + @Nullable String resultsField + ) { this.vocabularyConfig = Optional.ofNullable(vocabularyConfig) .orElse(new VocabularyConfig(InferenceIndexConstants.nativeDefinitionStore())); this.tokenization = tokenization == null ? Tokenization.createDefault() : tokenization; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/NerConfigUpdate.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/NerConfigUpdate.java index 4996324db77ef..2ff85e7160cb4 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/NerConfigUpdate.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/NerConfigUpdate.java @@ -28,7 +28,7 @@ public class NerConfigUpdate extends NlpConfigUpdate implements NamedXContentObj public static NerConfigUpdate fromMap(Map map) { Map options = new HashMap<>(map); - String resultsField = (String)options.remove(RESULTS_FIELD.getPreferredName()); + String resultsField = (String) options.remove(RESULTS_FIELD.getPreferredName()); if (options.isEmpty() == false) { throw ExceptionsHelper.badRequestException("Unrecognized fields {}.", options.keySet()); @@ -39,10 +39,7 @@ public static NerConfigUpdate fromMap(Map map) { private static final ObjectParser STRICT_PARSER = createParser(false); private static ObjectParser createParser(boolean lenient) { - ObjectParser parser = new ObjectParser<>( - NAME, - lenient, - NerConfigUpdate.Builder::new); + ObjectParser parser = new ObjectParser<>(NAME, lenient, NerConfigUpdate.Builder::new); parser.declareString(NerConfigUpdate.Builder::setResultsField, RESULTS_FIELD); return parser; } @@ -92,9 +89,10 @@ public InferenceConfig apply(InferenceConfig originalConfig) { throw ExceptionsHelper.badRequestException( "Inference config of type [{}] can not be updated with a request of type [{}]", originalConfig.getName(), - getName()); + getName() + ); } - NerConfig nerConfig = (NerConfig)originalConfig; + NerConfig nerConfig = (NerConfig) originalConfig; if (isNoop(nerConfig)) { return nerConfig; } @@ -123,8 +121,7 @@ public String getResultsField() { @Override public InferenceConfigUpdate.Builder, ? extends InferenceConfigUpdate> newBuilder() { - return new NerConfigUpdate.Builder() - .setResultsField(resultsField); + return new NerConfigUpdate.Builder().setResultsField(resultsField); } @Override @@ -140,8 +137,7 @@ public int hashCode() { return Objects.hash(resultsField); } - public static class Builder - implements InferenceConfigUpdate.Builder { + public static class Builder implements InferenceConfigUpdate.Builder { private String resultsField; @Override @@ -155,4 +151,3 @@ public NerConfigUpdate build() { } } } - diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/NullInferenceConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/NullInferenceConfig.java index f5cf9364f9e3d..71498ca9fb302 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/NullInferenceConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/NullInferenceConfig.java @@ -19,7 +19,6 @@ public class NullInferenceConfig implements InferenceConfig { private final boolean requestingFeatureImportance; - public NullInferenceConfig(boolean requestingFeatureImportance) { this.requestingFeatureImportance = requestingFeatureImportance; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/PassThroughConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/PassThroughConfig.java index 633df2a5d3e45..60fa003b142e3 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/PassThroughConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/PassThroughConfig.java @@ -10,10 +10,10 @@ import org.elasticsearch.Version; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.core.Nullable; import org.elasticsearch.xpack.core.ml.inference.persistence.InferenceIndexConstants; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; import org.elasticsearch.xpack.core.ml.utils.NamedXContentObjectHelper; @@ -38,23 +38,23 @@ public static PassThroughConfig fromXContentLenient(XContentParser parser) { private static final ConstructingObjectParser LENIENT_PARSER = createParser(true); private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { - ConstructingObjectParser parser = new ConstructingObjectParser<>(NAME, ignoreUnknownFields, - a -> new PassThroughConfig((VocabularyConfig) a[0], (Tokenization) a[1], (String) a[2])); - parser.declareObject( - ConstructingObjectParser.optionalConstructorArg(), - (p, c) -> { - if (ignoreUnknownFields == false) { - throw ExceptionsHelper.badRequestException( - "illegal setting [{}] on inference model creation", - VOCABULARY.getPreferredName() - ); - } - return VocabularyConfig.fromXContentLenient(p); - }, - VOCABULARY + ConstructingObjectParser parser = new ConstructingObjectParser<>( + NAME, + ignoreUnknownFields, + a -> new PassThroughConfig((VocabularyConfig) a[0], (Tokenization) a[1], (String) a[2]) ); + parser.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> { + if (ignoreUnknownFields == false) { + throw ExceptionsHelper.badRequestException( + "illegal setting [{}] on inference model creation", + VOCABULARY.getPreferredName() + ); + } + return VocabularyConfig.fromXContentLenient(p); + }, VOCABULARY); parser.declareNamedObject( - ConstructingObjectParser.optionalConstructorArg(), (p, c, n) -> p.namedObject(Tokenization.class, n, ignoreUnknownFields), + ConstructingObjectParser.optionalConstructorArg(), + (p, c, n) -> p.namedObject(Tokenization.class, n, ignoreUnknownFields), TOKENIZATION ); parser.declareString(ConstructingObjectParser.optionalConstructorArg(), RESULTS_FIELD); @@ -65,9 +65,10 @@ private static ConstructingObjectParser createParser(bo private final Tokenization tokenization; private final String resultsField; - public PassThroughConfig(@Nullable VocabularyConfig vocabularyConfig, - @Nullable Tokenization tokenization, - @Nullable String resultsField + public PassThroughConfig( + @Nullable VocabularyConfig vocabularyConfig, + @Nullable Tokenization tokenization, + @Nullable String resultsField ) { this.vocabularyConfig = Optional.ofNullable(vocabularyConfig) .orElse(new VocabularyConfig(InferenceIndexConstants.nativeDefinitionStore())); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/PassThroughConfigUpdate.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/PassThroughConfigUpdate.java index 33c78e7933704..c7b3127307029 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/PassThroughConfigUpdate.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/PassThroughConfigUpdate.java @@ -27,7 +27,7 @@ public class PassThroughConfigUpdate extends NlpConfigUpdate implements NamedXCo public static PassThroughConfigUpdate fromMap(Map map) { Map options = new HashMap<>(map); - String resultsField = (String)options.remove(RESULTS_FIELD.getPreferredName()); + String resultsField = (String) options.remove(RESULTS_FIELD.getPreferredName()); if (options.isEmpty() == false) { throw ExceptionsHelper.badRequestException("Unrecognized fields {}.", options.keySet()); @@ -41,7 +41,8 @@ private static ObjectParser createParser( ObjectParser parser = new ObjectParser<>( NAME, lenient, - PassThroughConfigUpdate.Builder::new); + PassThroughConfigUpdate.Builder::new + ); parser.declareString(PassThroughConfigUpdate.Builder::setResultsField, RESULTS_FIELD); return parser; } @@ -95,14 +96,12 @@ public InferenceConfig apply(InferenceConfig originalConfig) { throw ExceptionsHelper.badRequestException( "Inference config of type [{}] can not be updated with a inference request of type [{}]", originalConfig.getName(), - getName()); + getName() + ); } - PassThroughConfig passThroughConfig = (PassThroughConfig)originalConfig; - return new PassThroughConfig( - passThroughConfig.getVocabularyConfig(), - passThroughConfig.getTokenization(), - resultsField); + PassThroughConfig passThroughConfig = (PassThroughConfig) originalConfig; + return new PassThroughConfig(passThroughConfig.getVocabularyConfig(), passThroughConfig.getTokenization(), resultsField); } @Override @@ -117,8 +116,7 @@ public String getResultsField() { @Override public InferenceConfigUpdate.Builder, ? extends InferenceConfigUpdate> newBuilder() { - return new PassThroughConfigUpdate.Builder() - .setResultsField(resultsField); + return new PassThroughConfigUpdate.Builder().setResultsField(resultsField); } @Override @@ -134,8 +132,7 @@ public int hashCode() { return Objects.hash(resultsField); } - public static class Builder - implements InferenceConfigUpdate.Builder { + public static class Builder implements InferenceConfigUpdate.Builder { private String resultsField; @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/PredictionFieldType.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/PredictionFieldType.java index b1f38da2cd761..5a34efcc449ce 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/PredictionFieldType.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/PredictionFieldType.java @@ -49,7 +49,7 @@ public Object transformPredictedValue(Double value, String stringRep) { if (value == null) { return null; } - switch(this) { + switch (this) { case STRING: return stringRep == null ? value.toString() : stringRep; case BOOLEAN: @@ -83,7 +83,8 @@ public Object transformPredictedValue(Double value, String stringRep) { private static boolean fromDouble(double value) { if ((areClose(value, 1.0D) || areClose(value, 0.0D)) == false) { throw new IllegalArgumentException( - "Cannot transform numbers other than 0.0 or 1.0 to boolean. Provided number [" + value + "]"); + "Cannot transform numbers other than 0.0 or 1.0 to boolean. Provided number [" + value + "]" + ); } return areClose(value, 1.0D); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/RegressionConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/RegressionConfig.java index caed2c9b2211c..d50cf82ce7b51 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/RegressionConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/RegressionConfig.java @@ -7,17 +7,17 @@ package org.elasticsearch.xpack.core.ml.inference.trainedmodel; import org.elasticsearch.Version; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.Objects; -public class RegressionConfig implements LenientlyParsedInferenceConfig, StrictlyParsedInferenceConfig { +public class RegressionConfig implements LenientlyParsedInferenceConfig, StrictlyParsedInferenceConfig { public static final ParseField NAME = new ParseField("regression"); private static final Version MIN_SUPPORTED_VERSION = Version.V_7_6_0; @@ -33,7 +33,8 @@ private static ObjectParser createParser(boolean ObjectParser parser = new ObjectParser<>( NAME.getPreferredName(), lenient, - RegressionConfig.Builder::new); + RegressionConfig.Builder::new + ); parser.declareString(RegressionConfig.Builder::setResultsField, RESULTS_FIELD); parser.declareInt(RegressionConfig.Builder::setNumTopFeatureImportanceValues, NUM_TOP_FEATURE_IMPORTANCE_VALUES); return parser; @@ -57,8 +58,9 @@ public RegressionConfig(String resultsField) { public RegressionConfig(String resultsField, Integer numTopFeatureImportanceValues) { this.resultsField = resultsField == null ? DEFAULT_RESULTS_FIELD : resultsField; if (numTopFeatureImportanceValues != null && numTopFeatureImportanceValues < 0) { - throw new IllegalArgumentException("[" + NUM_TOP_FEATURE_IMPORTANCE_VALUES.getPreferredName() + - "] must be greater than or equal to 0"); + throw new IllegalArgumentException( + "[" + NUM_TOP_FEATURE_IMPORTANCE_VALUES.getPreferredName() + "] must be greater than or equal to 0" + ); } this.numTopFeatureImportanceValues = numTopFeatureImportanceValues == null ? 0 : numTopFeatureImportanceValues; } @@ -116,7 +118,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - RegressionConfig that = (RegressionConfig)o; + RegressionConfig that = (RegressionConfig) o; return Objects.equals(this.resultsField, that.resultsField) && Objects.equals(this.numTopFeatureImportanceValues, that.numTopFeatureImportanceValues); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/RegressionConfigUpdate.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/RegressionConfigUpdate.java index 239c2a39ccf87..e1f5a96e1793a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/RegressionConfigUpdate.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/RegressionConfigUpdate.java @@ -6,10 +6,10 @@ */ package org.elasticsearch.xpack.core.ml.inference.trainedmodel; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; @@ -31,8 +31,8 @@ public class RegressionConfigUpdate implements InferenceConfigUpdate, NamedXCont public static RegressionConfigUpdate fromMap(Map map) { Map options = new HashMap<>(map); - String resultsField = (String)options.remove(RESULTS_FIELD.getPreferredName()); - Integer featureImportance = (Integer)options.remove(NUM_TOP_FEATURE_IMPORTANCE_VALUES.getPreferredName()); + String resultsField = (String) options.remove(RESULTS_FIELD.getPreferredName()); + Integer featureImportance = (Integer) options.remove(NUM_TOP_FEATURE_IMPORTANCE_VALUES.getPreferredName()); if (options.isEmpty() == false) { throw ExceptionsHelper.badRequestException("Unrecognized fields {}.", map.keySet()); } @@ -49,7 +49,8 @@ private static ObjectParser createParser(b ObjectParser parser = new ObjectParser<>( NAME.getPreferredName(), lenient, - RegressionConfigUpdate.Builder::new); + RegressionConfigUpdate.Builder::new + ); parser.declareString(RegressionConfigUpdate.Builder::setResultsField, RESULTS_FIELD); parser.declareInt(RegressionConfigUpdate.Builder::setNumTopFeatureImportanceValues, NUM_TOP_FEATURE_IMPORTANCE_VALUES); return parser; @@ -65,8 +66,9 @@ public static RegressionConfigUpdate fromXContentStrict(XContentParser parser) { public RegressionConfigUpdate(String resultsField, Integer numTopFeatureImportanceValues) { this.resultsField = resultsField; if (numTopFeatureImportanceValues != null && numTopFeatureImportanceValues < 0) { - throw new IllegalArgumentException("[" + NUM_TOP_FEATURE_IMPORTANCE_VALUES.getPreferredName() + - "] must be greater than or equal to 0"); + throw new IllegalArgumentException( + "[" + NUM_TOP_FEATURE_IMPORTANCE_VALUES.getPreferredName() + "] must be greater than or equal to 0" + ); } this.numTopFeatureImportanceValues = numTopFeatureImportanceValues; @@ -88,9 +90,7 @@ public String getResultsField() { @Override public InferenceConfigUpdate.Builder, ? extends InferenceConfigUpdate> newBuilder() { - return new Builder() - .setNumTopFeatureImportanceValues(numTopFeatureImportanceValues) - .setResultsField(resultsField); + return new Builder().setNumTopFeatureImportanceValues(numTopFeatureImportanceValues).setResultsField(resultsField); } @Override @@ -126,7 +126,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - RegressionConfigUpdate that = (RegressionConfigUpdate)o; + RegressionConfigUpdate that = (RegressionConfigUpdate) o; return Objects.equals(this.resultsField, that.resultsField) && Objects.equals(this.numTopFeatureImportanceValues, that.numTopFeatureImportanceValues); } @@ -142,10 +142,11 @@ public InferenceConfig apply(InferenceConfig originalConfig) { throw ExceptionsHelper.badRequestException( "Inference config of type [{}] can not be updated with a inference request of type [{}]", originalConfig.getName(), - getName()); + getName() + ); } - RegressionConfig regressionConfig = (RegressionConfig)originalConfig; + RegressionConfig regressionConfig = (RegressionConfig) originalConfig; if (isNoop(regressionConfig)) { return originalConfig; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ResultsFieldUpdate.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ResultsFieldUpdate.java index 073cb09064441..4813e00d7ea9d 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ResultsFieldUpdate.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ResultsFieldUpdate.java @@ -42,7 +42,9 @@ public InferenceConfig apply(InferenceConfig originalConfig) { return update.apply(originalConfig); } else { throw ExceptionsHelper.badRequestException( - "Inference config of unknown type [{}] can not be updated", originalConfig.getName()); + "Inference config of unknown type [{}] can not be updated", + originalConfig.getName() + ); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ShapPath.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ShapPath.java index d7a8fdaf255cf..3ae0eedbcca64 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ShapPath.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ShapPath.java @@ -6,11 +6,10 @@ */ package org.elasticsearch.xpack.core.ml.inference.trainedmodel; - /** * Ported from https://github.com/elastic/ml-cpp/blob/master/include/maths/CTreeShapFeatureImportance.h Path struct */ -public class ShapPath { +public class ShapPath { private static final double DBL_EPSILON = Double.MIN_VALUE; private final PathElement[] pathElements; @@ -35,7 +34,7 @@ public ShapPath(PathElement[] elements, double[] scale) { this.elementAndScaleOffset = 0; } - // Update binomial coefficients to be able to compute Equation (2) from the paper. In particular, + // Update binomial coefficients to be able to compute Equation (2) from the paper. In particular, // we have in the line path.scale[i + 1] += fractionOne * path.scale[i] * (i + 1.0) / (pathDepth + // 1.0) that if we're on the "one" path, i.e. if the last feature selects this path if we include that // feature in S (then fractionOne is 1), and we need to consider all the additional ways we now have of @@ -46,8 +45,8 @@ public ShapPath(PathElement[] elements, double[] scale) { public int extend(double fractionZero, double fractionOne, int featureIndex, int nextIndex) { setValues(nextIndex, fractionOne, fractionZero, featureIndex); setScale(nextIndex, nextIndex == 0 ? 1.0 : 0.0); - double stepDown = fractionOne / (double)(nextIndex + 1); - double stepUp = fractionZero / (double)(nextIndex + 1); + double stepDown = fractionOne / (double) (nextIndex + 1); + double stepUp = fractionZero / (double) (nextIndex + 1); double countDown = nextIndex * stepDown; double countUp = stepUp; for (int i = (nextIndex - 1); i >= 0; --i, countDown -= stepDown, countUp += stepUp) { @@ -77,7 +76,7 @@ public double sumUnwoundPath(int pathIndex, int nextIndex) { } else { double pD = pathDepth; - for(int i = 0; i < pathDepth; i++) { + for (int i = 0; i < pathDepth; i++) { total += getScale(i) / pD--; } total *= (pathDepth + 1) / (fractionZero + DBL_EPSILON); @@ -93,8 +92,8 @@ public int unwind(int pathIndex, int nextIndex) { double fractionZero = fractionZeros(pathIndex); if (fractionOne != 0) { - double stepUp = fractionZero / (double)(pathDepth + 1); - double stepDown = fractionOne / (double)nextIndex; + double stepUp = fractionZero / (double) (pathDepth + 1); + double stepDown = fractionOne / (double) nextIndex; double countUp = 0.0; double countDown = nextIndex * stepDown; for (int i = pathDepth; i >= 0; --i, countUp += stepUp, countDown -= stepDown) { @@ -103,7 +102,7 @@ public int unwind(int pathIndex, int nextIndex) { setScale(i, tmp); } } else { - double stepDown = (fractionZero + DBL_EPSILON) / (double)(pathDepth + 1); + double stepDown = (fractionZero + DBL_EPSILON) / (double) (pathDepth + 1); double countDown = pathDepth * stepDown; for (int i = 0; i <= pathDepth; ++i, countDown -= stepDown) { setScale(i, getScale(i) / countDown); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/StrictlyParsedInferenceConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/StrictlyParsedInferenceConfig.java index a5486e280431c..9d0cddaf32589 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/StrictlyParsedInferenceConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/StrictlyParsedInferenceConfig.java @@ -6,5 +6,4 @@ */ package org.elasticsearch.xpack.core.ml.inference.trainedmodel; -public interface StrictlyParsedInferenceConfig extends InferenceConfig { -} +public interface StrictlyParsedInferenceConfig extends InferenceConfig {} diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/StrictlyParsedTrainedModel.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/StrictlyParsedTrainedModel.java index b2ba596779f66..6e0530e2308df 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/StrictlyParsedTrainedModel.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/StrictlyParsedTrainedModel.java @@ -6,5 +6,4 @@ */ package org.elasticsearch.xpack.core.ml.inference.trainedmodel; -public interface StrictlyParsedTrainedModel extends TrainedModel { -} +public interface StrictlyParsedTrainedModel extends TrainedModel {} diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/StrictlyParsedTrainedModelLocation.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/StrictlyParsedTrainedModelLocation.java index 36c027583926d..f322ac9cdb418 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/StrictlyParsedTrainedModelLocation.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/StrictlyParsedTrainedModelLocation.java @@ -7,5 +7,4 @@ package org.elasticsearch.xpack.core.ml.inference.trainedmodel; -public interface StrictlyParsedTrainedModelLocation extends TrainedModelLocation{ -} +public interface StrictlyParsedTrainedModelLocation extends TrainedModelLocation {} diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/TargetType.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/TargetType.java index c25bc618d5607..4ad4d1c458d78 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/TargetType.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/TargetType.java @@ -6,17 +6,18 @@ */ package org.elasticsearch.xpack.core.ml.inference.trainedmodel; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.xcontent.ParseField; import java.io.IOException; import java.util.Locale; public enum TargetType implements Writeable { - REGRESSION, CLASSIFICATION; + REGRESSION, + CLASSIFICATION; public static final ParseField TARGET_TYPE = new ParseField("target_type"); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/TextClassificationConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/TextClassificationConfig.java index 74805ced94f53..3c5a5204ea933 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/TextClassificationConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/TextClassificationConfig.java @@ -10,10 +10,10 @@ import org.elasticsearch.Version; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ObjectParser; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.core.Nullable; import org.elasticsearch.xpack.core.ml.inference.persistence.InferenceIndexConstants; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; import org.elasticsearch.xpack.core.ml.utils.NamedXContentObjectHelper; @@ -39,25 +39,21 @@ public static TextClassificationConfig fromXContentLenient(XContentParser parser private static final ObjectParser LENIENT_PARSER = createParser(true); private static ObjectParser createParser(boolean ignoreUnknownFields) { - ObjectParser parser = - new ObjectParser<>(NAME, ignoreUnknownFields, Builder::new); - - parser.declareObject( - Builder::setVocabularyConfig, - (p, c) -> { - if (ignoreUnknownFields == false) { - throw ExceptionsHelper.badRequestException( - "illegal setting [{}] on inference model creation", - VOCABULARY.getPreferredName() - ); - } - return VocabularyConfig.fromXContentLenient(p); - }, - VOCABULARY - ); + ObjectParser parser = new ObjectParser<>(NAME, ignoreUnknownFields, Builder::new); + + parser.declareObject(Builder::setVocabularyConfig, (p, c) -> { + if (ignoreUnknownFields == false) { + throw ExceptionsHelper.badRequestException( + "illegal setting [{}] on inference model creation", + VOCABULARY.getPreferredName() + ); + } + return VocabularyConfig.fromXContentLenient(p); + }, VOCABULARY); parser.declareNamedObject( - Builder::setTokenization, (p, c, n) -> p.namedObject(Tokenization.class, n, ignoreUnknownFields), - TOKENIZATION + Builder::setTokenization, + (p, c, n) -> p.namedObject(Tokenization.class, n, ignoreUnknownFields), + TOKENIZATION ); parser.declareStringArray(Builder::setClassificationLabels, CLASSIFICATION_LABELS); parser.declareInt(Builder::setNumTopClasses, NUM_TOP_CLASSES); @@ -71,17 +67,23 @@ private static ObjectParser createParser private final int numTopClasses; private final String resultsField; - public TextClassificationConfig(@Nullable VocabularyConfig vocabularyConfig, - @Nullable Tokenization tokenization, - List classificationLabels, - @Nullable Integer numTopClasses, - @Nullable String resultsField) { + public TextClassificationConfig( + @Nullable VocabularyConfig vocabularyConfig, + @Nullable Tokenization tokenization, + List classificationLabels, + @Nullable Integer numTopClasses, + @Nullable String resultsField + ) { this.vocabularyConfig = Optional.ofNullable(vocabularyConfig) .orElse(new VocabularyConfig(InferenceIndexConstants.nativeDefinitionStore())); this.tokenization = tokenization == null ? Tokenization.createDefault() : tokenization; if (classificationLabels == null || classificationLabels.size() < 2) { - throw ExceptionsHelper.badRequestException("[{}] requires at least 2 [{}]; provided {}", - NAME, CLASSIFICATION_LABELS, classificationLabels); + throw ExceptionsHelper.badRequestException( + "[{}] requires at least 2 [{}]; provided {}", + NAME, + CLASSIFICATION_LABELS, + classificationLabels + ); } this.classificationLabels = classificationLabels; this.numTopClasses = Optional.ofNullable(numTopClasses).orElse(-1); @@ -227,11 +229,7 @@ public Builder setResultsField(String resultsField) { } public TextClassificationConfig build() { - return new TextClassificationConfig(vocabularyConfig, - tokenization, - classificationLabels, - numTopClasses, - resultsField); + return new TextClassificationConfig(vocabularyConfig, tokenization, classificationLabels, numTopClasses, resultsField); } } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/TextClassificationConfigUpdate.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/TextClassificationConfigUpdate.java index 06b9bc424d0b6..47605e29d9703 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/TextClassificationConfigUpdate.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/TextClassificationConfigUpdate.java @@ -32,9 +32,9 @@ public class TextClassificationConfigUpdate extends NlpConfigUpdate implements N @SuppressWarnings("unchecked") public static TextClassificationConfigUpdate fromMap(Map map) { Map options = new HashMap<>(map); - Integer numTopClasses = (Integer)options.remove(NUM_TOP_CLASSES.getPreferredName()); - String resultsField = (String)options.remove(RESULTS_FIELD.getPreferredName()); - List classificationLabels = (List)options.remove(CLASSIFICATION_LABELS.getPreferredName()); + Integer numTopClasses = (Integer) options.remove(NUM_TOP_CLASSES.getPreferredName()); + String resultsField = (String) options.remove(RESULTS_FIELD.getPreferredName()); + List classificationLabels = (List) options.remove(CLASSIFICATION_LABELS.getPreferredName()); if (options.isEmpty() == false) { throw ExceptionsHelper.badRequestException("Unrecognized fields {}.", options.keySet()); @@ -45,10 +45,7 @@ public static TextClassificationConfigUpdate fromMap(Map map) { private static final ObjectParser STRICT_PARSER = createParser(false); private static ObjectParser createParser(boolean lenient) { - ObjectParser parser = new ObjectParser<>( - NAME, - lenient, - TextClassificationConfigUpdate.Builder::new); + ObjectParser parser = new ObjectParser<>(NAME, lenient, TextClassificationConfigUpdate.Builder::new); parser.declareStringArray(Builder::setClassificationLabels, CLASSIFICATION_LABELS); parser.declareString(Builder::setResultsField, RESULTS_FIELD); parser.declareInt(Builder::setNumTopClasses, NUM_TOP_CLASSES); @@ -98,10 +95,11 @@ public InferenceConfig apply(InferenceConfig originalConfig) { throw ExceptionsHelper.badRequestException( "Inference config of type [{}] can not be updated with a request of type [{}]", originalConfig.getName(), - getName()); + getName() + ); } - TextClassificationConfig classificationConfig = (TextClassificationConfig)originalConfig; + TextClassificationConfig classificationConfig = (TextClassificationConfig) originalConfig; if (isNoop(classificationConfig)) { return originalConfig; } @@ -128,9 +126,9 @@ public InferenceConfig apply(InferenceConfig originalConfig) { } boolean isNoop(TextClassificationConfig originalConfig) { - return (this.numTopClasses == null || this.numTopClasses == originalConfig.getNumTopClasses()) && - (this.classificationLabels == null) && - (this.resultsField == null || this.resultsField.equals(originalConfig.getResultsField())); + return (this.numTopClasses == null || this.numTopClasses == originalConfig.getNumTopClasses()) + && (this.classificationLabels == null) + && (this.resultsField == null || this.resultsField.equals(originalConfig.getResultsField())); } @Override @@ -145,10 +143,7 @@ public String getResultsField() { @Override public InferenceConfigUpdate.Builder, ? extends InferenceConfigUpdate> newBuilder() { - return new Builder() - .setClassificationLabels(classificationLabels) - .setNumTopClasses(numTopClasses) - .setResultsField(resultsField); + return new Builder().setClassificationLabels(classificationLabels).setNumTopClasses(numTopClasses).setResultsField(resultsField); } @Override @@ -172,9 +167,9 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; TextClassificationConfigUpdate that = (TextClassificationConfigUpdate) o; - return Objects.equals(classificationLabels, that.classificationLabels) && - Objects.equals(numTopClasses, that.numTopClasses) && - Objects.equals(resultsField, that.resultsField); + return Objects.equals(classificationLabels, that.classificationLabels) + && Objects.equals(numTopClasses, that.numTopClasses) + && Objects.equals(resultsField, that.resultsField); } @Override @@ -183,7 +178,8 @@ public int hashCode() { } public static class Builder - implements InferenceConfigUpdate.Builder { + implements + InferenceConfigUpdate.Builder { private List classificationLabels; private Integer numTopClasses; private String resultsField; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/TextEmbeddingConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/TextEmbeddingConfig.java index f5cf09e1b05d5..77ef7481f1cb3 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/TextEmbeddingConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/TextEmbeddingConfig.java @@ -10,10 +10,10 @@ import org.elasticsearch.Version; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.core.Nullable; import org.elasticsearch.xpack.core.ml.inference.persistence.InferenceIndexConstants; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; import org.elasticsearch.xpack.core.ml.utils.NamedXContentObjectHelper; @@ -38,23 +38,23 @@ public static TextEmbeddingConfig fromXContentLenient(XContentParser parser) { private static final ConstructingObjectParser LENIENT_PARSER = createParser(true); private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { - ConstructingObjectParser parser = new ConstructingObjectParser<>(NAME, ignoreUnknownFields, - a -> new TextEmbeddingConfig((VocabularyConfig) a[0], (Tokenization) a[1], (String) a[2])); - parser.declareObject( - ConstructingObjectParser.optionalConstructorArg(), - (p, c) -> { - if (ignoreUnknownFields == false) { - throw ExceptionsHelper.badRequestException( - "illegal setting [{}] on inference model creation", - VOCABULARY.getPreferredName() - ); - } - return VocabularyConfig.fromXContentLenient(p); - }, - VOCABULARY + ConstructingObjectParser parser = new ConstructingObjectParser<>( + NAME, + ignoreUnknownFields, + a -> new TextEmbeddingConfig((VocabularyConfig) a[0], (Tokenization) a[1], (String) a[2]) ); + parser.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> { + if (ignoreUnknownFields == false) { + throw ExceptionsHelper.badRequestException( + "illegal setting [{}] on inference model creation", + VOCABULARY.getPreferredName() + ); + } + return VocabularyConfig.fromXContentLenient(p); + }, VOCABULARY); parser.declareNamedObject( - ConstructingObjectParser.optionalConstructorArg(), (p, c, n) -> p.namedObject(Tokenization.class, n, ignoreUnknownFields), + ConstructingObjectParser.optionalConstructorArg(), + (p, c, n) -> p.namedObject(Tokenization.class, n, ignoreUnknownFields), TOKENIZATION ); parser.declareString(ConstructingObjectParser.optionalConstructorArg(), RESULTS_FIELD); @@ -65,9 +65,11 @@ private static ConstructingObjectParser createParser( private final Tokenization tokenization; private final String resultsField; - public TextEmbeddingConfig(@Nullable VocabularyConfig vocabularyConfig, - @Nullable Tokenization tokenization, - @Nullable String resultsField) { + public TextEmbeddingConfig( + @Nullable VocabularyConfig vocabularyConfig, + @Nullable Tokenization tokenization, + @Nullable String resultsField + ) { this.vocabularyConfig = Optional.ofNullable(vocabularyConfig) .orElse(new VocabularyConfig(InferenceIndexConstants.nativeDefinitionStore())); this.tokenization = tokenization == null ? Tokenization.createDefault() : tokenization; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/TextEmbeddingConfigUpdate.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/TextEmbeddingConfigUpdate.java index df1d39655215d..d4ae0b7367afa 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/TextEmbeddingConfigUpdate.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/TextEmbeddingConfigUpdate.java @@ -28,7 +28,7 @@ public class TextEmbeddingConfigUpdate extends NlpConfigUpdate implements NamedX public static TextEmbeddingConfigUpdate fromMap(Map map) { Map options = new HashMap<>(map); - String resultsField = (String)options.remove(RESULTS_FIELD.getPreferredName()); + String resultsField = (String) options.remove(RESULTS_FIELD.getPreferredName()); if (options.isEmpty() == false) { throw ExceptionsHelper.badRequestException("Unrecognized fields {}.", options.keySet()); @@ -42,7 +42,8 @@ private static ObjectParser createParse ObjectParser parser = new ObjectParser<>( NAME, lenient, - TextEmbeddingConfigUpdate.Builder::new); + TextEmbeddingConfigUpdate.Builder::new + ); parser.declareString(TextEmbeddingConfigUpdate.Builder::setResultsField, RESULTS_FIELD); return parser; } @@ -96,14 +97,12 @@ public InferenceConfig apply(InferenceConfig originalConfig) { throw ExceptionsHelper.badRequestException( "Inference config of type [{}] can not be updated with a inference request of type [{}]", originalConfig.getName(), - getName()); + getName() + ); } - TextEmbeddingConfig embeddingConfig = (TextEmbeddingConfig)originalConfig; - return new TextEmbeddingConfig( - embeddingConfig.getVocabularyConfig(), - embeddingConfig.getTokenization(), - resultsField); + TextEmbeddingConfig embeddingConfig = (TextEmbeddingConfig) originalConfig; + return new TextEmbeddingConfig(embeddingConfig.getVocabularyConfig(), embeddingConfig.getTokenization(), resultsField); } @Override @@ -118,8 +117,7 @@ public String getResultsField() { @Override public InferenceConfigUpdate.Builder, ? extends InferenceConfigUpdate> newBuilder() { - return new Builder() - .setResultsField(resultsField); + return new Builder().setResultsField(resultsField); } @Override @@ -135,8 +133,7 @@ public int hashCode() { return Objects.hash(resultsField); } - public static class Builder - implements InferenceConfigUpdate.Builder { + public static class Builder implements InferenceConfigUpdate.Builder { private String resultsField; @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/Tokenization.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/Tokenization.java index d0ee7d7a7e539..3e6d5e5f522fd 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/Tokenization.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/Tokenization.java @@ -10,10 +10,10 @@ import org.elasticsearch.common.io.stream.NamedWriteable; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.core.Nullable; import org.elasticsearch.xpack.core.ml.utils.NamedXContentObject; import java.io.IOException; @@ -38,7 +38,7 @@ public String toString() { } } - //TODO add global params like never_split, bos_token, eos_token, mask_token, tokenize_chinese_chars, strip_accents, etc. + // TODO add global params like never_split, bos_token, eos_token, mask_token, tokenize_chinese_chars, strip_accents, etc. public static final ParseField DO_LOWER_CASE = new ParseField("do_lower_case"); public static final ParseField WITH_SPECIAL_TOKENS = new ParseField("with_special_tokens"); public static final ParseField MAX_SEQUENCE_LENGTH = new ParseField("max_sequence_length"); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/TrainedModel.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/TrainedModel.java index ba6f848a4c9d7..fe4ebf8f0a459 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/TrainedModel.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/TrainedModel.java @@ -11,7 +11,6 @@ import org.elasticsearch.common.io.stream.NamedWriteable; import org.elasticsearch.xpack.core.ml.utils.NamedXContentObject; - public interface TrainedModel extends NamedXContentObject, NamedWriteable, Accountable { /** diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/VocabularyConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/VocabularyConfig.java index 03061e9a97f1e..dd1f502af6005 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/VocabularyConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/VocabularyConfig.java @@ -25,13 +25,13 @@ public class VocabularyConfig implements ToXContentObject, Writeable { private static final ParseField INDEX = new ParseField("index"); public static String docId(String modelId) { - return modelId+ "_vocabulary"; + return modelId + "_vocabulary"; } private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( "vocabulary_config", true, - a -> new VocabularyConfig((String)a[0]) + a -> new VocabularyConfig((String) a[0]) ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), INDEX); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ZeroShotClassificationConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ZeroShotClassificationConfig.java index c7b75069ae80a..3843678019105 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ZeroShotClassificationConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ZeroShotClassificationConfig.java @@ -10,11 +10,11 @@ import org.elasticsearch.Version; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.core.Nullable; import org.elasticsearch.xpack.core.ml.inference.persistence.InferenceIndexConstants; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; import org.elasticsearch.xpack.core.ml.utils.NamedXContentObjectHelper; @@ -54,13 +54,13 @@ public static ZeroShotClassificationConfig fromXContentLenient(XContentParser pa private static final ConstructingObjectParser STRICT_PARSER = createParser(false); private static final ConstructingObjectParser LENIENT_PARSER = createParser(true); - @SuppressWarnings({ "unchecked"}) + @SuppressWarnings({ "unchecked" }) private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { ConstructingObjectParser parser = new ConstructingObjectParser<>( NAME, ignoreUnknownFields, a -> new ZeroShotClassificationConfig( - (List)a[0], + (List) a[0], (VocabularyConfig) a[1], (Tokenization) a[2], (String) a[3], @@ -70,22 +70,19 @@ private static ConstructingObjectParser crea ) ); parser.declareStringArray(ConstructingObjectParser.constructorArg(), CLASSIFICATION_LABELS); - parser.declareObject( - ConstructingObjectParser.optionalConstructorArg(), - (p, c) -> { - if (ignoreUnknownFields == false) { - throw ExceptionsHelper.badRequestException( - "illegal setting [{}] on inference model creation", - VOCABULARY.getPreferredName() - ); - } - return VocabularyConfig.fromXContentLenient(p); - }, - VOCABULARY - ); + parser.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> { + if (ignoreUnknownFields == false) { + throw ExceptionsHelper.badRequestException( + "illegal setting [{}] on inference model creation", + VOCABULARY.getPreferredName() + ); + } + return VocabularyConfig.fromXContentLenient(p); + }, VOCABULARY); parser.declareNamedObject( - ConstructingObjectParser.optionalConstructorArg(), (p, c, n) -> p.namedObject(Tokenization.class, n, ignoreUnknownFields), - TOKENIZATION + ConstructingObjectParser.optionalConstructorArg(), + (p, c, n) -> p.namedObject(Tokenization.class, n, ignoreUnknownFields), + TOKENIZATION ); parser.declareString(ConstructingObjectParser.optionalConstructorArg(), HYPOTHESIS_TEMPLATE); parser.declareBoolean(ConstructingObjectParser.optionalConstructorArg(), MULTI_LABEL); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ZeroShotClassificationConfigUpdate.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ZeroShotClassificationConfigUpdate.java index 71fafc49a6984..35697e95129b1 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ZeroShotClassificationConfigUpdate.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ZeroShotClassificationConfigUpdate.java @@ -9,10 +9,10 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.core.Nullable; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; import org.elasticsearch.xpack.core.ml.utils.NamedXContentObject; @@ -35,22 +35,22 @@ public static ZeroShotClassificationConfigUpdate fromXContentStrict(XContentPars return STRICT_PARSER.apply(parser, null); } - @SuppressWarnings({ "unchecked"}) + @SuppressWarnings({ "unchecked" }) public static ZeroShotClassificationConfigUpdate fromMap(Map map) { Map options = new HashMap<>(map); - Boolean isMultiLabel = (Boolean)options.remove(MULTI_LABEL.getPreferredName()); - List labels = (List)options.remove(LABELS.getPreferredName()); - String resultsField = (String)options.remove(RESULTS_FIELD.getPreferredName()); + Boolean isMultiLabel = (Boolean) options.remove(MULTI_LABEL.getPreferredName()); + List labels = (List) options.remove(LABELS.getPreferredName()); + String resultsField = (String) options.remove(RESULTS_FIELD.getPreferredName()); if (options.isEmpty() == false) { throw ExceptionsHelper.badRequestException("Unrecognized fields {}.", map.keySet()); } return new ZeroShotClassificationConfigUpdate(labels, isMultiLabel, resultsField); } - @SuppressWarnings({ "unchecked"}) + @SuppressWarnings({ "unchecked" }) private static final ConstructingObjectParser STRICT_PARSER = new ConstructingObjectParser<>( NAME, - a -> new ZeroShotClassificationConfigUpdate((List)a[0], (Boolean) a[1], (String) a[2]) + a -> new ZeroShotClassificationConfigUpdate((List) a[0], (Boolean) a[1], (String) a[2]) ); static { @@ -116,10 +116,11 @@ public InferenceConfig apply(InferenceConfig originalConfig) { throw ExceptionsHelper.badRequestException( "Inference config of type [{}] can not be updated with a inference request of type [{}]", originalConfig.getName(), - getName()); + getName() + ); } - ZeroShotClassificationConfig zeroShotConfig = (ZeroShotClassificationConfig)originalConfig; + ZeroShotClassificationConfig zeroShotConfig = (ZeroShotClassificationConfig) originalConfig; if ((labels == null || labels.isEmpty()) && (zeroShotConfig.getLabels() == null || zeroShotConfig.getLabels().isEmpty())) { throw ExceptionsHelper.badRequestException( "stored configuration has no [{}] defined, supplied inference_config update must supply [{}]", @@ -173,9 +174,9 @@ public boolean equals(Object o) { if (o == null || getClass() != o.getClass()) return false; ZeroShotClassificationConfigUpdate that = (ZeroShotClassificationConfigUpdate) o; - return Objects.equals(isMultiLabel, that.isMultiLabel) && - Objects.equals(labels, that.labels) && - Objects.equals(resultsField, that.resultsField); + return Objects.equals(isMultiLabel, that.isMultiLabel) + && Objects.equals(labels, that.labels) + && Objects.equals(resultsField, that.resultsField); } @Override @@ -187,10 +188,9 @@ public List getLabels() { return labels; } - public static class Builder implements InferenceConfigUpdate.Builder< - ZeroShotClassificationConfigUpdate.Builder, - ZeroShotClassificationConfigUpdate - > { + public static class Builder + implements + InferenceConfigUpdate.Builder { private List labels; private Boolean isMultiLabel; private String resultsField; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/Ensemble.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/Ensemble.java index 922d10ba3fec4..1287b3b75f611 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/Ensemble.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/Ensemble.java @@ -10,11 +10,11 @@ import org.apache.lucene.util.Accountables; import org.apache.lucene.util.RamUsageEstimator; import org.elasticsearch.Version; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.LenientlyParsedTrainedModel; @@ -33,7 +33,6 @@ import java.util.Objects; import java.util.OptionalDouble; - public class Ensemble implements LenientlyParsedTrainedModel, StrictlyParsedTrainedModel { private static final long SHALLOW_SIZE = RamUsageEstimator.shallowSizeOfInstance(Ensemble.class); @@ -41,7 +40,7 @@ public class Ensemble implements LenientlyParsedTrainedModel, StrictlyParsedTrai public static final ParseField NAME = new ParseField("ensemble"); public static final ParseField FEATURE_NAMES = new ParseField("feature_names"); public static final ParseField TRAINED_MODELS = new ParseField("trained_models"); - public static final ParseField AGGREGATE_OUTPUT = new ParseField("aggregate_output"); + public static final ParseField AGGREGATE_OUTPUT = new ParseField("aggregate_output"); public static final ParseField CLASSIFICATION_LABELS = new ParseField("classification_labels"); public static final ParseField CLASSIFICATION_WEIGHTS = new ParseField("classification_weights"); @@ -52,19 +51,24 @@ private static ObjectParser createParser(boolean lenient ObjectParser parser = new ObjectParser<>( NAME.getPreferredName(), lenient, - Ensemble.Builder::builderForParser); + Ensemble.Builder::builderForParser + ); parser.declareStringArray(Ensemble.Builder::setFeatureNames, FEATURE_NAMES); - parser.declareNamedObjects(Ensemble.Builder::setTrainedModels, - (p, c, n) -> - lenient ? p.namedObject(LenientlyParsedTrainedModel.class, n, null) : - p.namedObject(StrictlyParsedTrainedModel.class, n, null), + parser.declareNamedObjects( + Ensemble.Builder::setTrainedModels, + (p, c, n) -> lenient + ? p.namedObject(LenientlyParsedTrainedModel.class, n, null) + : p.namedObject(StrictlyParsedTrainedModel.class, n, null), (ensembleBuilder) -> ensembleBuilder.setModelsAreOrdered(true), - TRAINED_MODELS); - parser.declareNamedObject(Ensemble.Builder::setOutputAggregator, - (p, c, n) -> - lenient ? p.namedObject(LenientlyParsedOutputAggregator.class, n, null) : - p.namedObject(StrictlyParsedOutputAggregator.class, n, null), - AGGREGATE_OUTPUT); + TRAINED_MODELS + ); + parser.declareNamedObject( + Ensemble.Builder::setOutputAggregator, + (p, c, n) -> lenient + ? p.namedObject(LenientlyParsedOutputAggregator.class, n, null) + : p.namedObject(StrictlyParsedOutputAggregator.class, n, null), + AGGREGATE_OUTPUT + ); parser.declareString(Ensemble.Builder::setTargetType, TargetType.TARGET_TYPE); parser.declareStringArray(Ensemble.Builder::setClassificationLabels, CLASSIFICATION_LABELS); parser.declareDoubleArray(Ensemble.Builder::setClassificationWeights, CLASSIFICATION_WEIGHTS); @@ -86,20 +90,22 @@ public static Ensemble fromXContentLenient(XContentParser parser) { private final List classificationLabels; private final double[] classificationWeights; - Ensemble(List featureNames, - List models, - OutputAggregator outputAggregator, - TargetType targetType, - @Nullable List classificationLabels, - @Nullable double[] classificationWeights) { + Ensemble( + List featureNames, + List models, + OutputAggregator outputAggregator, + TargetType targetType, + @Nullable List classificationLabels, + @Nullable double[] classificationWeights + ) { this.featureNames = Collections.unmodifiableList(ExceptionsHelper.requireNonNull(featureNames, FEATURE_NAMES)); this.models = Collections.unmodifiableList(ExceptionsHelper.requireNonNull(models, TRAINED_MODELS)); this.outputAggregator = ExceptionsHelper.requireNonNull(outputAggregator, AGGREGATE_OUTPUT); this.targetType = ExceptionsHelper.requireNonNull(targetType, TargetType.TARGET_TYPE); this.classificationLabels = classificationLabels == null ? null : Collections.unmodifiableList(classificationLabels); - this.classificationWeights = classificationWeights == null ? - null : - Arrays.copyOf(classificationWeights, classificationWeights.length); + this.classificationWeights = classificationWeights == null + ? null + : Arrays.copyOf(classificationWeights, classificationWeights.length); } public Ensemble(StreamInput in) throws IOException { @@ -157,11 +163,13 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.field(FEATURE_NAMES.getPreferredName(), featureNames); } NamedXContentObjectHelper.writeNamedObjects(builder, params, true, TRAINED_MODELS.getPreferredName(), models); - NamedXContentObjectHelper.writeNamedObjects(builder, + NamedXContentObjectHelper.writeNamedObjects( + builder, params, false, AGGREGATE_OUTPUT.getPreferredName(), - Collections.singletonList(outputAggregator)); + Collections.singletonList(outputAggregator) + ); builder.field(TargetType.TARGET_TYPE.getPreferredName(), targetType.toString()); if (classificationLabels != null) { builder.field(CLASSIFICATION_LABELS.getPreferredName(), classificationLabels); @@ -188,12 +196,14 @@ public boolean equals(Object o) { @Override public int hashCode() { - return Objects.hash(featureNames, + return Objects.hash( + featureNames, models, outputAggregator, targetType, classificationLabels, - Arrays.hashCode(classificationWeights)); + Arrays.hashCode(classificationWeights) + ); } @Override @@ -209,21 +219,20 @@ public void validate() { outputAggregator.getName() ); } - if (outputAggregator.expectedValueSize() != null && - outputAggregator.expectedValueSize() != models.size()) { + if (outputAggregator.expectedValueSize() != null && outputAggregator.expectedValueSize() != models.size()) { throw ExceptionsHelper.badRequestException( "[{}] expects value array of size [{}] but number of models is [{}]", AGGREGATE_OUTPUT.getPreferredName(), outputAggregator.expectedValueSize(), - models.size()); + models.size() + ); } if ((this.classificationLabels != null || this.classificationWeights != null) && (this.targetType != TargetType.CLASSIFICATION)) { throw ExceptionsHelper.badRequestException( - "[target_type] should be [classification] if [classification_labels] or [classification_weights] are provided"); + "[target_type] should be [classification] if [classification_labels] or [classification_weights] are provided" + ); } - if (classificationWeights != null && - classificationLabels != null && - classificationWeights.length != classificationLabels.size()) { + if (classificationWeights != null && classificationLabels != null && classificationWeights.length != classificationLabels.size()) { throw ExceptionsHelper.badRequestException( "[classification_weights] and [classification_labels] should be the same length if both are provided" ); @@ -236,7 +245,7 @@ public long estimatedNumOperations() { OptionalDouble avg = models.stream().mapToLong(TrainedModel::estimatedNumOperations).average(); assert avg.isPresent() : "unexpected null when calculating number of operations"; // Average operations for each model and the operations required for processing and aggregating with the outputAggregator - return (long)Math.ceil(avg.getAsDouble()) + 2 * (models.size() - 1); + return (long) Math.ceil(avg.getAsDouble()) + 2 * (models.size() - 1); } public static Builder builder() { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/Exponent.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/Exponent.java index 87d2832388d7b..72d6509943363 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/Exponent.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/Exponent.java @@ -7,10 +7,10 @@ package org.elasticsearch.xpack.core.ml.inference.trainedmodel.ensemble; import org.apache.lucene.util.RamUsageEstimator; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.TargetType; @@ -34,7 +34,8 @@ private static ConstructingObjectParser createParser(boolean len ConstructingObjectParser parser = new ConstructingObjectParser<>( NAME.getPreferredName(), lenient, - a -> new Exponent((List)a[0])); + a -> new Exponent((List) a[0]) + ); parser.declareDoubleArray(ConstructingObjectParser.optionalConstructorArg(), WEIGHTS); return parser; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/LenientlyParsedOutputAggregator.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/LenientlyParsedOutputAggregator.java index 877782c9cd369..6e12834ac4306 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/LenientlyParsedOutputAggregator.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/LenientlyParsedOutputAggregator.java @@ -6,6 +6,4 @@ */ package org.elasticsearch.xpack.core.ml.inference.trainedmodel.ensemble; - -public interface LenientlyParsedOutputAggregator extends OutputAggregator { -} +public interface LenientlyParsedOutputAggregator extends OutputAggregator {} diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/LogisticRegression.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/LogisticRegression.java index ec587d84e2530..4ce0c67849a0c 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/LogisticRegression.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/LogisticRegression.java @@ -6,12 +6,11 @@ */ package org.elasticsearch.xpack.core.ml.inference.trainedmodel.ensemble; - import org.apache.lucene.util.RamUsageEstimator; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.TargetType; @@ -38,7 +37,8 @@ private static ConstructingObjectParser createParser(b ConstructingObjectParser parser = new ConstructingObjectParser<>( NAME.getPreferredName(), lenient, - a -> new LogisticRegression((List)a[0])); + a -> new LogisticRegression((List) a[0]) + ); parser.declareDoubleArray(ConstructingObjectParser.optionalConstructorArg(), WEIGHTS); return parser; } @@ -88,7 +88,7 @@ public double[] processValues(double[][] values) { for (int j = 0; j < values.length; j++) { double[] value = values[j]; double weight = weights == null ? 1.0 : weights[j]; - for(int i = 0; i < value.length; i++) { + for (int i = 0; i < value.length; i++) { if (i >= sumOnAxis1.length) { throw new IllegalArgumentException("value entries must have the same dimensions"); } @@ -101,7 +101,7 @@ public double[] processValues(double[][] values) { double probOfClassOne = sigmoid(sumOnAxis1[0]); assert 0.0 <= probOfClassOne && probOfClassOne <= 1.0; - return new double[] {1.0 - probOfClassOne, probOfClassOne}; + return new double[] { 1.0 - probOfClassOne, probOfClassOne }; } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/StrictlyParsedOutputAggregator.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/StrictlyParsedOutputAggregator.java index 1d0d68363ae0f..a9cd00e4b5b53 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/StrictlyParsedOutputAggregator.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/StrictlyParsedOutputAggregator.java @@ -6,6 +6,4 @@ */ package org.elasticsearch.xpack.core.ml.inference.trainedmodel.ensemble; - -public interface StrictlyParsedOutputAggregator extends OutputAggregator { -} +public interface StrictlyParsedOutputAggregator extends OutputAggregator {} diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/WeightedMode.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/WeightedMode.java index e707a8e30bbaf..dbd2f42803b09 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/WeightedMode.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/WeightedMode.java @@ -6,12 +6,11 @@ */ package org.elasticsearch.xpack.core.ml.inference.trainedmodel.ensemble; - import org.apache.lucene.util.RamUsageEstimator; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.TargetType; @@ -41,7 +40,8 @@ private static ConstructingObjectParser createParser(boolean ConstructingObjectParser parser = new ConstructingObjectParser<>( NAME.getPreferredName(), lenient, - a -> new WeightedMode((Integer) a[0], (List)a[1])); + a -> new WeightedMode((Integer) a[0], (List) a[1]) + ); parser.declareInt(ConstructingObjectParser.constructorArg(), NUM_CLASSES); parser.declareDoubleArray(ConstructingObjectParser.optionalConstructorArg(), WEIGHTS); return parser; @@ -101,7 +101,7 @@ public double[] processValues(double[][] values) { for (int j = 0; j < values.length; j++) { double[] value = values[j]; double weight = weights == null ? 1.0 : weights[j]; - for(int i = 0; i < value.length; i++) { + for (int i = 0; i < value.length; i++) { if (i >= sumOnAxis1.length) { throw new IllegalArgumentException("value entries must have the same dimensions"); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/WeightedSum.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/WeightedSum.java index 40e0563ed634b..a055ad1513610 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/WeightedSum.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/WeightedSum.java @@ -6,12 +6,11 @@ */ package org.elasticsearch.xpack.core.ml.inference.trainedmodel.ensemble; - import org.apache.lucene.util.RamUsageEstimator; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.TargetType; @@ -36,7 +35,8 @@ private static ConstructingObjectParser createParser(boolean ConstructingObjectParser parser = new ConstructingObjectParser<>( NAME.getPreferredName(), lenient, - a -> new WeightedSum((List)a[0])); + a -> new WeightedSum((List) a[0]) + ); parser.declareDoubleArray(ConstructingObjectParser.optionalConstructorArg(), WEIGHTS); return parser; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/inference/EnsembleInferenceModel.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/inference/EnsembleInferenceModel.java index a93b6b24da4e6..c1aa113b61e84 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/inference/EnsembleInferenceModel.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/inference/EnsembleInferenceModel.java @@ -11,8 +11,8 @@ import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.message.ParameterizedMessage; import org.apache.lucene.util.RamUsageEstimator; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.Tuple; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.XContentParser; @@ -61,19 +61,26 @@ public class EnsembleInferenceModel implements InferenceModel { private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( "ensemble_inference_model", true, - a -> new EnsembleInferenceModel((List)a[0], - (OutputAggregator)a[1], - TargetType.fromString((String)a[2]), - (List)a[3], - (List)a[4])); + a -> new EnsembleInferenceModel( + (List) a[0], + (OutputAggregator) a[1], + TargetType.fromString((String) a[2]), + (List) a[3], + (List) a[4] + ) + ); static { - PARSER.declareNamedObjects(constructorArg(), + PARSER.declareNamedObjects( + constructorArg(), (p, c, n) -> p.namedObject(InferenceModel.class, n, null), (ensembleBuilder) -> {}, - TRAINED_MODELS); - PARSER.declareNamedObject(constructorArg(), + TRAINED_MODELS + ); + PARSER.declareNamedObject( + constructorArg(), (p, c, n) -> p.namedObject(LenientlyParsedOutputAggregator.class, n, null), - AGGREGATE_OUTPUT); + AGGREGATE_OUTPUT + ); PARSER.declareString(constructorArg(), TargetType.TARGET_TYPE); PARSER.declareStringArray(optionalConstructorArg(), CLASSIFICATION_LABELS); PARSER.declareDoubleArray(optionalConstructorArg(), CLASSIFICATION_WEIGHTS); @@ -91,18 +98,20 @@ public static EnsembleInferenceModel fromXContent(XContentParser parser) { private final double[] classificationWeights; private volatile boolean preparedForInference = false; - private EnsembleInferenceModel(List models, - OutputAggregator outputAggregator, - TargetType targetType, - @Nullable List classificationLabels, - List classificationWeights) { + private EnsembleInferenceModel( + List models, + OutputAggregator outputAggregator, + TargetType targetType, + @Nullable List classificationLabels, + List classificationWeights + ) { this.models = ExceptionsHelper.requireNonNull(models, TRAINED_MODELS); this.outputAggregator = ExceptionsHelper.requireNonNull(outputAggregator, AGGREGATE_OUTPUT); this.targetType = ExceptionsHelper.requireNonNull(targetType, TargetType.TARGET_TYPE); this.classificationLabels = classificationLabels; - this.classificationWeights = classificationWeights == null ? - null : - classificationWeights.stream().mapToDouble(Double::doubleValue).toArray(); + this.classificationWeights = classificationWeights == null + ? null + : classificationWeights.stream().mapToDouble(Double::doubleValue).toArray(); } @Override @@ -128,7 +137,10 @@ public InferenceResults infer(double[] features, InferenceConfig config) { private InferenceResults innerInfer(double[] features, InferenceConfig config, Map featureDecoderMap) { if (config.isTargetTypeSupported(targetType) == false) { throw ExceptionsHelper.badRequestException( - "Cannot infer using configuration for [{}] when model target_type is [{}]", config.getName(), targetType.toString()); + "Cannot infer using configuration for [{}] when model target_type is [{}]", + config.getName(), + targetType.toString() + ); } if (preparedForInference == false) { throw ExceptionsHelper.serverError("model is not prepared for inference"); @@ -153,7 +165,7 @@ private InferenceResults innerInfer(double[] features, InferenceConfig config, M return buildResults(processed, featureInfluence, featureDecoderMap, config); } - //For testing + // For testing double[][] featureImportance(double[] features) { double[][] featureInfluence = new double[features.length][]; NullInferenceConfig subModelInferenceConfig = new NullInferenceConfig(true); @@ -177,27 +189,31 @@ private void addFeatureImportance(double[][] featureInfluence, RawInferenceResul } } - private InferenceResults buildResults(double[] processedInferences, - double[][] featureImportance, - Map featureDecoderMap, - InferenceConfig config) { + private InferenceResults buildResults( + double[] processedInferences, + double[][] featureImportance, + Map featureDecoderMap, + InferenceConfig config + ) { // Indicates that the config is useless and the caller just wants the raw value if (config instanceof NullInferenceConfig) { - return new RawInferenceResults( - new double[] {outputAggregator.aggregate(processedInferences)}, - featureImportance); + return new RawInferenceResults(new double[] { outputAggregator.aggregate(processedInferences) }, featureImportance); } - Map decodedFeatureImportance = config.requestingImportance() ? - decodeFeatureImportances(featureDecoderMap, + Map decodedFeatureImportance = config.requestingImportance() + ? decodeFeatureImportances( + featureDecoderMap, IntStream.range(0, featureImportance.length) .boxed() - .collect(Collectors.toMap(i -> featureNames[i], i -> featureImportance[i]))) : - Collections.emptyMap(); - switch(targetType) { + .collect(Collectors.toMap(i -> featureNames[i], i -> featureImportance[i])) + ) + : Collections.emptyMap(); + switch (targetType) { case REGRESSION: - return new RegressionInferenceResults(outputAggregator.aggregate(processedInferences), + return new RegressionInferenceResults( + outputAggregator.aggregate(processedInferences), config, - transformFeatureImportanceRegression(decodedFeatureImportance)); + transformFeatureImportanceRegression(decodedFeatureImportance) + ); case CLASSIFICATION: ClassificationConfig classificationConfig = (ClassificationConfig) config; assert classificationWeights == null || processedInferences.length == classificationWeights.length; @@ -207,17 +223,22 @@ private InferenceResults buildResults(double[] processedInferences, classificationLabels, classificationWeights, classificationConfig.getNumTopClasses(), - classificationConfig.getPredictionFieldType()); + classificationConfig.getPredictionFieldType() + ); final InferenceHelpers.TopClassificationValue value = topClasses.v1(); - return new ClassificationInferenceResults(value.getValue(), + return new ClassificationInferenceResults( + value.getValue(), classificationLabel(topClasses.v1().getValue(), classificationLabels), topClasses.v2(), - transformFeatureImportanceClassification(decodedFeatureImportance, + transformFeatureImportanceClassification( + decodedFeatureImportance, classificationLabels, - classificationConfig.getPredictionFieldType()), + classificationConfig.getPredictionFieldType() + ), config, value.getProbability(), - value.getScore()); + value.getScore() + ); default: throw new UnsupportedOperationException("unsupported target_type [" + targetType + "] for inference on ensemble model"); } @@ -304,14 +325,21 @@ public double[] getClassificationWeights() { @Override public String toString() { - return "EnsembleInferenceModel{" + - "featureNames=" + Arrays.toString(featureNames) + - ", models=" + models + - ", outputAggregator=" + outputAggregator + - ", targetType=" + targetType + - ", classificationLabels=" + classificationLabels + - ", classificationWeights=" + Arrays.toString(classificationWeights) + - ", preparedForInference=" + preparedForInference + - '}'; + return "EnsembleInferenceModel{" + + "featureNames=" + + Arrays.toString(featureNames) + + ", models=" + + models + + ", outputAggregator=" + + outputAggregator + + ", targetType=" + + targetType + + ", classificationLabels=" + + classificationLabels + + ", classificationWeights=" + + Arrays.toString(classificationWeights) + + ", preparedForInference=" + + preparedForInference + + '}'; } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/inference/InferenceDefinition.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/inference/InferenceDefinition.java index f1d2da413b9fe..7ae5b141a3c0f 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/inference/InferenceDefinition.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/inference/InferenceDefinition.java @@ -34,17 +34,23 @@ public class InferenceDefinition { private final List preProcessors; private Map decoderMap; - private static final ObjectParser PARSER = new ObjectParser<>(NAME, + private static final ObjectParser PARSER = new ObjectParser<>( + NAME, true, - InferenceDefinition.Builder::new); + InferenceDefinition.Builder::new + ); static { - PARSER.declareNamedObject(InferenceDefinition.Builder::setTrainedModel, + PARSER.declareNamedObject( + InferenceDefinition.Builder::setTrainedModel, (p, c, n) -> p.namedObject(InferenceModel.class, n, null), - TRAINED_MODEL); - PARSER.declareNamedObjects(InferenceDefinition.Builder::setPreProcessors, + TRAINED_MODEL + ); + PARSER.declareNamedObjects( + InferenceDefinition.Builder::setPreProcessors, (p, c, n) -> p.namedObject(LenientlyParsedPreProcessor.class, n, PreProcessor.PreProcessorParseContext.DEFAULT), (trainedModelDefBuilder) -> {}, - PREPROCESSORS); + PREPROCESSORS + ); } public static InferenceDefinition fromXContent(XContentParser parser) { @@ -76,11 +82,10 @@ public InferenceResults infer(Map fields, InferenceConfig config if (config.requestingImportance() && trainedModel.supportsFeatureImportance() == false) { throw ExceptionsHelper.badRequestException( "Feature importance is not supported for the configured model of type [{}]", - trainedModel.getName()); + trainedModel.getName() + ); } - return trainedModel.infer(fields, - config, - config.requestingImportance() ? getDecoderMap() : Collections.emptyMap()); + return trainedModel.infer(fields, config, config.requestingImportance() ? getDecoderMap() : Collections.emptyMap()); } public TargetType getTargetType() { @@ -105,11 +110,14 @@ private Map getDecoderMap() { @Override public String toString() { - return "InferenceDefinition{" + - "trainedModel=" + trainedModel + - ", preProcessors=" + preProcessors + - ", decoderMap=" + decoderMap + - '}'; + return "InferenceDefinition{" + + "trainedModel=" + + trainedModel + + ", preProcessors=" + + preProcessors + + ", decoderMap=" + + decoderMap + + '}'; } public static Builder builder() { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/inference/TreeInferenceModel.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/inference/TreeInferenceModel.java index 8ea8b9821c7b0..8ad70dea0aeaa 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/inference/TreeInferenceModel.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/inference/TreeInferenceModel.java @@ -11,8 +11,8 @@ import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.message.ParameterizedMessage; import org.apache.lucene.util.Accountable; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Numbers; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.Tuple; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; @@ -68,10 +68,12 @@ public class TreeInferenceModel implements InferenceModel { "tree_inference_model", true, a -> new TreeInferenceModel( - (List)a[0], - (List)a[1], - a[2] == null ? null : TargetType.fromString((String)a[2]), - (List)a[3])); + (List) a[0], + (List) a[1], + a[2] == null ? null : TargetType.fromString((String) a[2]), + (List) a[3] + ) + ); static { PARSER.declareStringArray(constructorArg(), FEATURE_NAMES); @@ -93,12 +95,14 @@ public static TreeInferenceModel fromXContent(XContentParser parser) { private final int leafSize; private volatile boolean preparedForInference = false; - TreeInferenceModel(List featureNames, - List nodes, - @Nullable TargetType targetType, - List classificationLabels) { + TreeInferenceModel( + List featureNames, + List nodes, + @Nullable TargetType targetType, + List classificationLabels + ) { this.featureNames = ExceptionsHelper.requireNonNull(featureNames, FEATURE_NAMES).toArray(String[]::new); - if(ExceptionsHelper.requireNonNull(nodes, TREE_STRUCTURE).size() == 0) { + if (ExceptionsHelper.requireNonNull(nodes, TREE_STRUCTURE).size() == 0) { throw new IllegalArgumentException("[tree_structure] must not be empty"); } this.nodes = nodes.stream().map(NodeBuilder::build).toArray(Node[]::new); @@ -108,7 +112,7 @@ public static TreeInferenceModel fromXContent(XContentParser parser) { int leafSize = 1; for (Node node : this.nodes) { if (node instanceof LeafNode) { - leafSize = ((LeafNode)node).leafValue.length; + leafSize = ((LeafNode) node).leafValue.length; break; } } @@ -139,33 +143,38 @@ public InferenceResults infer(double[] features, InferenceConfig config) { private InferenceResults innerInfer(double[] features, InferenceConfig config, Map featureDecoderMap) { if (config.isTargetTypeSupported(targetType) == false) { throw ExceptionsHelper.badRequestException( - "Cannot infer using configuration for [{}] when model target_type is [{}]", config.getName(), targetType.toString()); + "Cannot infer using configuration for [{}] when model target_type is [{}]", + config.getName(), + targetType.toString() + ); } if (preparedForInference == false) { throw ExceptionsHelper.serverError("model is not prepared for inference"); } - double[][] featureImportance = config.requestingImportance() ? - featureImportance(features) : - new double[0][]; + double[][] featureImportance = config.requestingImportance() ? featureImportance(features) : new double[0][]; return buildResult(getLeaf(features), featureImportance, featureDecoderMap, config); } - private InferenceResults buildResult(double[] value, - double[][] featureImportance, - Map featureDecoderMap, - InferenceConfig config) { + private InferenceResults buildResult( + double[] value, + double[][] featureImportance, + Map featureDecoderMap, + InferenceConfig config + ) { assert value != null && value.length > 0; // Indicates that the config is useless and the caller just wants the raw value if (config instanceof NullInferenceConfig) { return new RawInferenceResults(value, featureImportance); } - Map decodedFeatureImportance = config.requestingImportance() ? - decodeFeatureImportances(featureDecoderMap, + Map decodedFeatureImportance = config.requestingImportance() + ? decodeFeatureImportances( + featureDecoderMap, IntStream.range(0, featureImportance.length) .boxed() - .collect(Collectors.toMap(i -> featureNames[i], i -> featureImportance[i]))) : - Collections.emptyMap(); + .collect(Collectors.toMap(i -> featureNames[i], i -> featureImportance[i])) + ) + : Collections.emptyMap(); switch (targetType) { case CLASSIFICATION: ClassificationConfig classificationConfig = (ClassificationConfig) config; @@ -174,21 +183,28 @@ private InferenceResults buildResult(double[] value, classificationLabels, null, classificationConfig.getNumTopClasses(), - classificationConfig.getPredictionFieldType()); + classificationConfig.getPredictionFieldType() + ); final InferenceHelpers.TopClassificationValue classificationValue = topClasses.v1(); - return new ClassificationInferenceResults(classificationValue.getValue(), + return new ClassificationInferenceResults( + classificationValue.getValue(), classificationLabel(classificationValue.getValue(), classificationLabels), topClasses.v2(), - InferenceHelpers.transformFeatureImportanceClassification(decodedFeatureImportance, + InferenceHelpers.transformFeatureImportanceClassification( + decodedFeatureImportance, classificationLabels, - classificationConfig.getPredictionFieldType()), + classificationConfig.getPredictionFieldType() + ), config, classificationValue.getProbability(), - classificationValue.getScore()); + classificationValue.getScore() + ); case REGRESSION: - return new RegressionInferenceResults(value[0], + return new RegressionInferenceResults( + value[0], config, - InferenceHelpers.transformFeatureImportanceRegression(decodedFeatureImportance)); + InferenceHelpers.transformFeatureImportanceRegression(decodedFeatureImportance) + ); default: throw new UnsupportedOperationException("unsupported target_type [" + targetType + "] for inference on tree model"); } @@ -215,10 +231,10 @@ private double[] classificationProbability(double[] inferenceValue) { private double[] getLeaf(double[] features) { Node node = nodes[0]; - while(node.isLeaf() == false) { + while (node.isLeaf() == false) { node = nodes[node.compare(features)]; } - return ((LeafNode)node).leafValue; + return ((LeafNode) node).leafValue; } public double[][] featureImportance(double[] fieldValues) { @@ -226,7 +242,7 @@ public double[][] featureImportance(double[] fieldValues) { for (int i = 0; i < fieldValues.length; i++) { featureImportance[i] = new double[leafSize]; } - int arrSize = ((this.maxDepth + 1) * (this.maxDepth + 2))/2; + int arrSize = ((this.maxDepth + 1) * (this.maxDepth + 2)) / 2; ShapPath.PathElement[] elements = new ShapPath.PathElement[arrSize]; for (int i = 0; i < arrSize; i++) { elements[i] = new ShapPath.PathElement(); @@ -243,19 +259,21 @@ public double[][] featureImportance(double[] fieldValues) { * If improvements in performance or accuracy have been found, it is probably best that the changes are implemented on the native * side first and then ported to the Java side. */ - private void shapRecursive(double[] processedFeatures, - ShapPath parentSplitPath, - int nodeIndex, - double parentFractionZero, - double parentFractionOne, - int parentFeatureIndex, - double[][] featureImportance, - int nextIndex) { + private void shapRecursive( + double[] processedFeatures, + ShapPath parentSplitPath, + int nodeIndex, + double parentFractionZero, + double parentFractionOne, + int parentFeatureIndex, + double[][] featureImportance, + int nextIndex + ) { ShapPath splitPath = new ShapPath(parentSplitPath, nextIndex); Node currNode = nodes[nodeIndex]; nextIndex = splitPath.extend(parentFractionZero, parentFractionOne, parentFeatureIndex, nextIndex); if (currNode.isLeaf()) { - double[] leafValue = ((LeafNode)currNode).leafValue; + double[] leafValue = ((LeafNode) currNode).leafValue; for (int i = 1; i < nextIndex; ++i) { int inputColumnIndex = splitPath.featureIndex(i); double scaled = splitPath.sumUnwoundPath(i, nextIndex) * (splitPath.fractionOnes(i) - splitPath.fractionZeros(i)); @@ -264,7 +282,7 @@ private void shapRecursive(double[] processedFeatures, } } } else { - InnerNode innerNode = (InnerNode)currNode; + InnerNode innerNode = (InnerNode) currNode; int hotIndex = currNode.compare(processedFeatures); int coldIndex = hotIndex == innerNode.leftChild ? innerNode.rightChild : innerNode.leftChild; @@ -278,14 +296,28 @@ private void shapRecursive(double[] processedFeatures, nextIndex = splitPath.unwind(pathIndex, nextIndex); } - double hotFractionZero = nodes[hotIndex].getNumberSamples() / (double)currNode.getNumberSamples(); - double coldFractionZero = nodes[coldIndex].getNumberSamples() / (double)currNode.getNumberSamples(); - shapRecursive(processedFeatures, splitPath, - hotIndex, incomingFractionZero * hotFractionZero, - incomingFractionOne, splitFeature, featureImportance, nextIndex); - shapRecursive(processedFeatures, splitPath, - coldIndex, incomingFractionZero * coldFractionZero, - 0.0, splitFeature, featureImportance, nextIndex); + double hotFractionZero = nodes[hotIndex].getNumberSamples() / (double) currNode.getNumberSamples(); + double coldFractionZero = nodes[coldIndex].getNumberSamples() / (double) currNode.getNumberSamples(); + shapRecursive( + processedFeatures, + splitPath, + hotIndex, + incomingFractionZero * hotFractionZero, + incomingFractionOne, + splitFeature, + featureImportance, + nextIndex + ); + shapRecursive( + processedFeatures, + splitPath, + coldIndex, + incomingFractionZero * coldFractionZero, + 0.0, + splitFeature, + featureImportance, + nextIndex + ); } } @@ -313,7 +345,7 @@ public void rewriteFeatureIndices(Map newFeatureIndexMapping) { if (node.isLeaf()) { continue; } - InnerNode treeNode = (InnerNode)node; + InnerNode treeNode = (InnerNode) node; Integer newSplitFeatureIndex = newFeatureIndexMapping.get(featureNames[treeNode.splitFeature]); if (newSplitFeatureIndex == null) { throw new IllegalArgumentException("[tree] failed to optimize for inference"); @@ -358,16 +390,24 @@ public Node[] getNodes() { @Override public String toString() { - return "TreeInferenceModel{" + - "nodes=" + Arrays.toString(nodes) + - ", featureNames=" + Arrays.toString(featureNames) + - ", targetType=" + targetType + - ", classificationLabels=" + classificationLabels + - ", highOrderCategory=" + highOrderCategory + - ", maxDepth=" + maxDepth + - ", leafSize=" + leafSize + - ", preparedForInference=" + preparedForInference + - '}'; + return "TreeInferenceModel{" + + "nodes=" + + Arrays.toString(nodes) + + ", featureNames=" + + Arrays.toString(featureNames) + + ", targetType=" + + targetType + + ", classificationLabels=" + + classificationLabels + + ", highOrderCategory=" + + highOrderCategory + + ", maxDepth=" + + maxDepth + + ", leafSize=" + + leafSize + + ", preparedForInference=" + + preparedForInference + + '}'; } private static int getDepth(Node[] nodes, int nodeIndex, int depth) { @@ -375,7 +415,7 @@ private static int getDepth(Node[] nodes, int nodeIndex, int depth) { if (node instanceof LeafNode) { return 0; } - InnerNode innerNode = (InnerNode)node; + InnerNode innerNode = (InnerNode) node; int depthLeft = getDepth(nodes, innerNode.leftChild, depth + 1); int depthRight = getDepth(nodes, innerNode.rightChild, depth + 1); return Math.max(depthLeft, depthRight) + 1; @@ -386,13 +426,11 @@ static class NodeBuilder { private static final ObjectParser PARSER = new ObjectParser<>( "tree_inference_model_node", true, - NodeBuilder::new); + NodeBuilder::new + ); static { PARSER.declareDouble(NodeBuilder::setThreshold, THRESHOLD); - PARSER.declareField(NodeBuilder::setOperator, - p -> Operator.fromString(p.text()), - DECISION_TYPE, - ObjectParser.ValueType.STRING); + PARSER.declareField(NodeBuilder::setOperator, p -> Operator.fromString(p.text()), DECISION_TYPE, ObjectParser.ValueType.STRING); PARSER.declareInt(NodeBuilder::setLeftChild, LEFT_CHILD); PARSER.declareInt(NodeBuilder::setRightChild, RIGHT_CHILD); PARSER.declareBoolean(NodeBuilder::setDefaultLeft, DEFAULT_LEFT); @@ -458,13 +496,7 @@ Node build() { if (this.leftChild < 0) { return new LeafNode(leafValue, numberSamples); } - return new InnerNode(operator, - threshold, - splitFeature, - defaultLeft, - leftChild, - rightChild, - numberSamples); + return new InnerNode(operator, threshold, splitFeature, defaultLeft, leftChild, rightChild, numberSamples); } } @@ -493,13 +525,15 @@ public static class InnerNode extends Node { private final int rightChild; private final long numberSamples; - InnerNode(Operator operator, - double threshold, - int splitFeature, - boolean defaultLeft, - int leftChild, - int rightChild, - long numberSamples) { + InnerNode( + Operator operator, + double threshold, + int splitFeature, + boolean defaultLeft, + int leftChild, + int rightChild, + long numberSamples + ) { this.operator = operator; this.threshold = threshold; this.splitFeature = splitFeature; @@ -534,15 +568,22 @@ public long ramBytesUsed() { @Override public String toString() { - return "InnerNode{" + - "operator=" + operator + - ", threshold=" + threshold + - ", splitFeature=" + splitFeature + - ", defaultLeft=" + defaultLeft + - ", leftChild=" + leftChild + - ", rightChild=" + rightChild + - ", numberSamples=" + numberSamples + - '}'; + return "InnerNode{" + + "operator=" + + operator + + ", threshold=" + + threshold + + ", splitFeature=" + + splitFeature + + ", defaultLeft=" + + defaultLeft + + ", leftChild=" + + leftChild + + ", rightChild=" + + rightChild + + ", numberSamples=" + + numberSamples + + '}'; } } @@ -572,10 +613,7 @@ public double[] getLeafValue() { @Override public String toString() { - return "LeafNode{" + - "leafValue=" + Arrays.toString(leafValue) + - ", numberSamples=" + numberSamples + - '}'; + return "LeafNode{" + "leafValue=" + Arrays.toString(leafValue) + ", numberSamples=" + numberSamples + '}'; } } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/langident/LangIdentNeuralNetwork.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/langident/LangIdentNeuralNetwork.java index ead27ceab3dd9..9890eda059172 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/langident/LangIdentNeuralNetwork.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/langident/LangIdentNeuralNetwork.java @@ -8,11 +8,11 @@ package org.elasticsearch.xpack.core.ml.inference.trainedmodel.langident; import org.apache.lucene.util.RamUsageEstimator; -import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Tuple; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.inference.results.ClassificationInferenceResults; @@ -48,16 +48,116 @@ public class LangIdentNeuralNetwork implements StrictlyParsedTrainedModel, Lenie public static final ConstructingObjectParser LENIENT_PARSER = createParser(true); private static final List LANGUAGE_NAMES = Arrays.asList( - "eo", "co", "eu", "ta", "de", "mt", "ps", "te", "su", "uz", "zh-Latn", "ne", - "nl", "sw", "sq", "hmn", "ja", "no", "mn", "so", "ko", "kk", "sl", "ig", - "mr", "th", "zu", "ml", "hr", "bs", "lo", "sd", "cy", "hy", "uk", "pt", - "lv", "iw", "cs", "vi", "jv", "be", "km", "mk", "tr", "fy", "am", "zh", - "da", "sv", "fi", "ht", "af", "la", "id", "fil", "sm", "ca", "el", "ka", - "sr", "it", "sk", "ru", "ru-Latn", "bg", "ny", "fa", "haw", "gl", "et", - "ms", "gd", "bg-Latn", "ha", "is", "ur", "mi", "hi", "bn", "hi-Latn", "fr", - "yi", "hu", "xh", "my", "tg", "ro", "ar", "lb", "el-Latn", "st", "ceb", - "kn", "az", "si", "ky", "mg", "en", "gu", "es", "pl", "ja-Latn", "ga", "lt", - "sn", "yo", "pa", "ku"); + "eo", + "co", + "eu", + "ta", + "de", + "mt", + "ps", + "te", + "su", + "uz", + "zh-Latn", + "ne", + "nl", + "sw", + "sq", + "hmn", + "ja", + "no", + "mn", + "so", + "ko", + "kk", + "sl", + "ig", + "mr", + "th", + "zu", + "ml", + "hr", + "bs", + "lo", + "sd", + "cy", + "hy", + "uk", + "pt", + "lv", + "iw", + "cs", + "vi", + "jv", + "be", + "km", + "mk", + "tr", + "fy", + "am", + "zh", + "da", + "sv", + "fi", + "ht", + "af", + "la", + "id", + "fil", + "sm", + "ca", + "el", + "ka", + "sr", + "it", + "sk", + "ru", + "ru-Latn", + "bg", + "ny", + "fa", + "haw", + "gl", + "et", + "ms", + "gd", + "bg-Latn", + "ha", + "is", + "ur", + "mi", + "hi", + "bn", + "hi-Latn", + "fr", + "yi", + "hu", + "xh", + "my", + "tg", + "ro", + "ar", + "lb", + "el-Latn", + "st", + "ceb", + "kn", + "az", + "si", + "ky", + "mg", + "en", + "gu", + "es", + "pl", + "ja-Latn", + "ga", + "lt", + "sn", + "yo", + "pa", + "ku" + ); private static final long SHALLOW_SIZE = RamUsageEstimator.shallowSizeOfInstance(LangIdentNeuralNetwork.class); @@ -68,16 +168,19 @@ private static ConstructingObjectParser createPars ConstructingObjectParser parser = new ConstructingObjectParser<>( NAME.getPreferredName(), lenient, - a -> new LangIdentNeuralNetwork((String) a[0], - (LangNetLayer) a[1], - (LangNetLayer) a[2])); + a -> new LangIdentNeuralNetwork((String) a[0], (LangNetLayer) a[1], (LangNetLayer) a[2]) + ); parser.declareString(constructorArg(), EMBEDDED_VECTOR_FEATURE_NAME); - parser.declareObject(constructorArg(), + parser.declareObject( + constructorArg(), (p, c) -> lenient ? LangNetLayer.LENIENT_PARSER.apply(p, c) : LangNetLayer.STRICT_PARSER.apply(p, c), - HIDDEN_LAYER); - parser.declareObject(constructorArg(), + HIDDEN_LAYER + ); + parser.declareObject( + constructorArg(), (p, c) -> lenient ? LangNetLayer.LENIENT_PARSER.apply(p, c) : LangNetLayer.STRICT_PARSER.apply(p, c), - SOFTMAX_LAYER); + SOFTMAX_LAYER + ); return parser; } @@ -93,9 +196,7 @@ public static LangIdentNeuralNetwork fromXContentLenient(XContentParser parser) private final LangNetLayer softmaxLayer; private final String embeddedVectorFeatureName; - public LangIdentNeuralNetwork(String embeddedVectorFeatureName, - LangNetLayer hiddenLayer, - LangNetLayer softmaxLayer) { + public LangIdentNeuralNetwork(String embeddedVectorFeatureName, LangNetLayer hiddenLayer, LangNetLayer softmaxLayer) { this.embeddedVectorFeatureName = ExceptionsHelper.requireNonNull(embeddedVectorFeatureName, EMBEDDED_VECTOR_FEATURE_NAME); this.hiddenLayer = ExceptionsHelper.requireNonNull(hiddenLayer, HIDDEN_LAYER); this.softmaxLayer = ExceptionsHelper.requireNonNull(softmaxLayer, SOFTMAX_LAYER); @@ -110,25 +211,27 @@ public LangIdentNeuralNetwork(StreamInput in) throws IOException { @Override public InferenceResults infer(Map fields, InferenceConfig config, Map featureDecoderMap) { if (config.requestingImportance()) { - throw ExceptionsHelper.badRequestException("[{}] model does not supports feature importance", - NAME.getPreferredName()); + throw ExceptionsHelper.badRequestException("[{}] model does not supports feature importance", NAME.getPreferredName()); } if (config instanceof ClassificationConfig == false) { - throw ExceptionsHelper.badRequestException("[{}] model only supports classification", - NAME.getPreferredName()); + throw ExceptionsHelper.badRequestException("[{}] model only supports classification", NAME.getPreferredName()); } Object vector = fields.get(embeddedVectorFeatureName); if (vector instanceof double[] == false) { - throw ExceptionsHelper.badRequestException("[{}] model could not find non-null numerical array named [{}]", + throw ExceptionsHelper.badRequestException( + "[{}] model could not find non-null numerical array named [{}]", NAME.getPreferredName(), - embeddedVectorFeatureName); + embeddedVectorFeatureName + ); } double[] embeddedVector = (double[]) vector; if (embeddedVector.length != EMBEDDING_VECTOR_LENGTH) { - throw ExceptionsHelper.badRequestException("[{}] model is expecting embedding vector of length [{}] but got [{}]", + throw ExceptionsHelper.badRequestException( + "[{}] model is expecting embedding vector of length [{}] but got [{}]", NAME.getPreferredName(), EMBEDDING_VECTOR_LENGTH, - embeddedVector.length); + embeddedVector.length + ); } double[] h0 = hiddenLayer.productPlusBias(false, embeddedVector); double[] scores = softmaxLayer.productPlusBias(true, h0); @@ -141,17 +244,20 @@ public InferenceResults infer(Map fields, InferenceConfig config LANGUAGE_NAMES, null, classificationConfig.getNumTopClasses(), - PredictionFieldType.STRING); + PredictionFieldType.STRING + ); final InferenceHelpers.TopClassificationValue classificationValue = topClasses.v1(); - assert classificationValue.getValue() >= 0 && classificationValue.getValue() < LANGUAGE_NAMES.size() : - "Invalid language predicted. Predicted language index " + topClasses.v1(); - return new ClassificationInferenceResults(classificationValue.getValue(), + assert classificationValue.getValue() >= 0 && classificationValue.getValue() < LANGUAGE_NAMES.size() + : "Invalid language predicted. Predicted language index " + topClasses.v1(); + return new ClassificationInferenceResults( + classificationValue.getValue(), LANGUAGE_NAMES.get(classificationValue.getValue()), topClasses.v2(), Collections.emptyList(), classificationConfig, classificationValue.getProbability(), - classificationValue.getScore()); + classificationValue.getScore() + ); } @Override @@ -168,7 +274,7 @@ public void rewriteFeatureIndices(Map newFeatureIndexMapping) { @Override public String[] getFeatureNames() { - return new String[] {embeddedVectorFeatureName}; + return new String[] { embeddedVectorFeatureName }; } @Override @@ -177,8 +283,7 @@ public TargetType targetType() { } @Override - public void validate() { - } + public void validate() {} @Override public long estimatedNumOperations() { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/langident/LangNetLayer.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/langident/LangNetLayer.java index b51c6e9975419..90a422ca7490c 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/langident/LangNetLayer.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/langident/LangNetLayer.java @@ -9,11 +9,11 @@ import org.apache.lucene.util.Accountable; import org.apache.lucene.util.RamUsageEstimator; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; @@ -41,17 +41,13 @@ public class LangNetLayer implements ToXContentObject, Writeable, Accountable { public static final ConstructingObjectParser STRICT_PARSER = createParser(false); public static final ConstructingObjectParser LENIENT_PARSER = createParser(true); - @SuppressWarnings("unchecked") private static ConstructingObjectParser createParser(boolean lenient) { ConstructingObjectParser parser = new ConstructingObjectParser<>( NAME.getPreferredName(), lenient, - a -> new LangNetLayer( - (List) a[0], - (int) a[1], - (int) a[2], - (List) a[3])); + a -> new LangNetLayer((List) a[0], (int) a[1], (int) a[2], (List) a[3]) + ); parser.declareDoubleArray(constructorArg(), WEIGHTS); parser.declareInt(constructorArg(), NUM_COLS); parser.declareInt(constructorArg(), NUM_ROWS); @@ -65,10 +61,12 @@ private static ConstructingObjectParser createParser(boolean private final double[] bias; private LangNetLayer(List weights, int numCols, int numRows, List bias) { - this(weights.stream().mapToDouble(Double::doubleValue).toArray(), + this( + weights.stream().mapToDouble(Double::doubleValue).toArray(), numCols, numRows, - bias.stream().mapToDouble(Double::doubleValue).toArray()); + bias.stream().mapToDouble(Double::doubleValue).toArray() + ); } LangNetLayer(double[] weights, int numCols, int numRows, double[] bias) { @@ -77,10 +75,12 @@ private LangNetLayer(List weights, int numCols, int numRows, List createParser(boolean ignoreUnknownFields) { - ConstructingObjectParser parser = new ConstructingObjectParser<>(NAME, + ConstructingObjectParser parser = new ConstructingObjectParser<>( + NAME, ignoreUnknownFields, - a -> new FeatureImportanceBaseline((Double)a[0], (List)a[1])); + a -> new FeatureImportanceBaseline((Double) a[0], (List) a[1]) + ); parser.declareDouble(ConstructingObjectParser.optionalConstructorArg(), BASELINE); - parser.declareObjectArray(ConstructingObjectParser.optionalConstructorArg(), + parser.declareObjectArray( + ConstructingObjectParser.optionalConstructorArg(), ignoreUnknownFields ? ClassBaseline.LENIENT_PARSER : ClassBaseline.STRICT_PARSER, - CLASSES); + CLASSES + ); return parser; } @@ -81,8 +85,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; FeatureImportanceBaseline that = (FeatureImportanceBaseline) o; - return Objects.equals(that.baseline, baseline) - && Objects.equals(classBaselines, that.classBaselines); + return Objects.equals(that.baseline, baseline) && Objects.equals(classBaselines, that.classBaselines); } public Map asMap() { @@ -110,9 +113,11 @@ public static class ClassBaseline implements ToXContentObject, Writeable { public static final ConstructingObjectParser STRICT_PARSER = createParser(false); private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { - ConstructingObjectParser parser = new ConstructingObjectParser<>(NAME, + ConstructingObjectParser parser = new ConstructingObjectParser<>( + NAME, ignoreUnknownFields, - a -> new ClassBaseline(a[0], (double)a[1])); + a -> new ClassBaseline(a[0], (double) a[1]) + ); parser.declareField(ConstructingObjectParser.constructorArg(), (p, c) -> { if (p.currentToken() == XContentParser.Token.VALUE_STRING) { return p.text(); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/metadata/Hyperparameters.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/metadata/Hyperparameters.java index 5f8c263dffebf..065a4e6d3f53c 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/metadata/Hyperparameters.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/metadata/Hyperparameters.java @@ -7,11 +7,11 @@ package org.elasticsearch.xpack.core.ml.inference.trainedmodel.metadata; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -31,16 +31,17 @@ public class Hyperparameters implements ToXContentObject, Writeable { public static final ParseField RELATIVE_IMPORTANCE = new ParseField("relative_importance"); public static final ParseField SUPPLIED = new ParseField("supplied"); - // These parsers follow the pattern that metadata is parsed leniently (to allow for enhancements), whilst config is parsed strictly public static final ConstructingObjectParser LENIENT_PARSER = createParser(true); public static final ConstructingObjectParser STRICT_PARSER = createParser(false); @SuppressWarnings("unchecked") private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { - ConstructingObjectParser parser = new ConstructingObjectParser<>(NAME, + ConstructingObjectParser parser = new ConstructingObjectParser<>( + NAME, ignoreUnknownFields, - a -> new Hyperparameters((String)a[0], (Double)a[1], (Double)a[2], (Double)a[3], (Boolean)a[4])); + a -> new Hyperparameters((String) a[0], (Double) a[1], (Double) a[2], (Double) a[3], (Boolean) a[4]) + ); parser.declareString(ConstructingObjectParser.constructorArg(), HYPERPARAMETER_NAME); parser.declareDouble(ConstructingObjectParser.constructorArg(), VALUE); parser.declareDouble(ConstructingObjectParser.optionalConstructorArg(), ABSOLUTE_IMPORTANCE); @@ -102,8 +103,7 @@ public boolean equals(Object o) { && Objects.equals(value, that.value) && Objects.equals(absoluteImportance, that.absoluteImportance) && Objects.equals(relativeImportance, that.relativeImportance) - && Objects.equals(supplied, that.supplied) - ; + && Objects.equals(supplied, that.supplied); } public Map asMap() { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/metadata/TotalFeatureImportance.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/metadata/TotalFeatureImportance.java index c494a57a8b252..fbe2d16211183 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/metadata/TotalFeatureImportance.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/metadata/TotalFeatureImportance.java @@ -7,13 +7,13 @@ package org.elasticsearch.xpack.core.ml.inference.trainedmodel.metadata; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParseException; @@ -43,16 +43,22 @@ public class TotalFeatureImportance implements ToXContentObject, Writeable { @SuppressWarnings("unchecked") private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { - ConstructingObjectParser parser = new ConstructingObjectParser<>(NAME, + ConstructingObjectParser parser = new ConstructingObjectParser<>( + NAME, ignoreUnknownFields, - a -> new TotalFeatureImportance((String)a[0], (Importance)a[1], (List)a[2])); + a -> new TotalFeatureImportance((String) a[0], (Importance) a[1], (List) a[2]) + ); parser.declareString(ConstructingObjectParser.constructorArg(), FEATURE_NAME); - parser.declareObject(ConstructingObjectParser.optionalConstructorArg(), + parser.declareObject( + ConstructingObjectParser.optionalConstructorArg(), ignoreUnknownFields ? Importance.LENIENT_PARSER : Importance.STRICT_PARSER, - IMPORTANCE); - parser.declareObjectArray(ConstructingObjectParser.optionalConstructorArg(), + IMPORTANCE + ); + parser.declareObjectArray( + ConstructingObjectParser.optionalConstructorArg(), ignoreUnknownFields ? ClassImportance.LENIENT_PARSER : ClassImportance.STRICT_PARSER, - CLASSES); + CLASSES + ); return parser; } @@ -123,9 +129,11 @@ public static class Importance implements ToXContentObject, Writeable { public static final ConstructingObjectParser STRICT_PARSER = createParser(false); private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { - ConstructingObjectParser parser = new ConstructingObjectParser<>(NAME, + ConstructingObjectParser parser = new ConstructingObjectParser<>( + NAME, ignoreUnknownFields, - a -> new Importance((double)a[0], (double)a[1], (double)a[2])); + a -> new Importance((double) a[0], (double) a[1], (double) a[2]) + ); parser.declareDouble(ConstructingObjectParser.constructorArg(), MEAN_MAGNITUDE); parser.declareDouble(ConstructingObjectParser.constructorArg(), MIN); parser.declareDouble(ConstructingObjectParser.constructorArg(), MAX); @@ -153,9 +161,9 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Importance that = (Importance) o; - return Double.compare(that.meanMagnitude, meanMagnitude) == 0 && - Double.compare(that.min, min) == 0 && - Double.compare(that.max, max) == 0; + return Double.compare(that.meanMagnitude, meanMagnitude) == 0 + && Double.compare(that.min, min) == 0 + && Double.compare(that.max, max) == 0; } @Override @@ -195,9 +203,11 @@ public static class ClassImportance implements ToXContentObject, Writeable { public static final ConstructingObjectParser STRICT_PARSER = createParser(false); private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { - ConstructingObjectParser parser = new ConstructingObjectParser<>(NAME, + ConstructingObjectParser parser = new ConstructingObjectParser<>( + NAME, ignoreUnknownFields, - a -> new ClassImportance(a[0], (Importance)a[1])); + a -> new ClassImportance(a[0], (Importance) a[1]) + ); parser.declareField(ConstructingObjectParser.constructorArg(), (p, c) -> { if (p.currentToken() == XContentParser.Token.VALUE_STRING) { return p.text(); @@ -208,9 +218,11 @@ private static ConstructingObjectParser createParser(bool } throw new XContentParseException("Unsupported token [" + p.currentToken() + "]"); }, CLASS_NAME, ObjectParser.ValueType.VALUE); - parser.declareObject(ConstructingObjectParser.constructorArg(), + parser.declareObject( + ConstructingObjectParser.constructorArg(), ignoreUnknownFields ? Importance.LENIENT_PARSER : Importance.STRICT_PARSER, - IMPORTANCE); + IMPORTANCE + ); return parser; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/metadata/TrainedModelMetadata.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/metadata/TrainedModelMetadata.java index 430dd848be32d..ee8f807b00010 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/metadata/TrainedModelMetadata.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/metadata/TrainedModelMetadata.java @@ -7,11 +7,11 @@ package org.elasticsearch.xpack.core.ml.inference.trainedmodel.metadata; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -38,20 +38,32 @@ public class TrainedModelMetadata implements ToXContentObject, Writeable { @SuppressWarnings("unchecked") private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { - ConstructingObjectParser parser = new ConstructingObjectParser<>(NAME, + ConstructingObjectParser parser = new ConstructingObjectParser<>( + NAME, ignoreUnknownFields, - a -> new TrainedModelMetadata((String)a[0], (List)a[1], (FeatureImportanceBaseline)a[2], - (List)a[3])); + a -> new TrainedModelMetadata( + (String) a[0], + (List) a[1], + (FeatureImportanceBaseline) a[2], + (List) a[3] + ) + ); parser.declareString(ConstructingObjectParser.constructorArg(), MODEL_ID); - parser.declareObjectArray(ConstructingObjectParser.constructorArg(), + parser.declareObjectArray( + ConstructingObjectParser.constructorArg(), ignoreUnknownFields ? TotalFeatureImportance.LENIENT_PARSER : TotalFeatureImportance.STRICT_PARSER, - TOTAL_FEATURE_IMPORTANCE); - parser.declareObject(ConstructingObjectParser.optionalConstructorArg(), + TOTAL_FEATURE_IMPORTANCE + ); + parser.declareObject( + ConstructingObjectParser.optionalConstructorArg(), ignoreUnknownFields ? FeatureImportanceBaseline.LENIENT_PARSER : FeatureImportanceBaseline.STRICT_PARSER, - FEATURE_IMPORTANCE_BASELINE); - parser.declareObjectArray(ConstructingObjectParser.optionalConstructorArg(), + FEATURE_IMPORTANCE_BASELINE + ); + parser.declareObjectArray( + ConstructingObjectParser.optionalConstructorArg(), ignoreUnknownFields ? Hyperparameters.LENIENT_PARSER : Hyperparameters.STRICT_PARSER, - HYPERPARAMETERS); + HYPERPARAMETERS + ); return parser; } @@ -79,14 +91,16 @@ public TrainedModelMetadata(StreamInput in) throws IOException { this.hyperparameters = in.readList(Hyperparameters::new); } - public TrainedModelMetadata(String modelId, - List totalFeatureImportances, - FeatureImportanceBaseline featureImportanceBaselines, - List hyperparameters) { + public TrainedModelMetadata( + String modelId, + List totalFeatureImportances, + FeatureImportanceBaseline featureImportanceBaselines, + List hyperparameters + ) { this.modelId = ExceptionsHelper.requireNonNull(modelId, MODEL_ID); this.totalFeatureImportances = Collections.unmodifiableList(totalFeatureImportances); this.featureImportanceBaselines = featureImportanceBaselines; - this.hyperparameters = hyperparameters == null ? Collections.emptyList() : Collections.unmodifiableList(hyperparameters); + this.hyperparameters = hyperparameters == null ? Collections.emptyList() : Collections.unmodifiableList(hyperparameters); } public String getModelId() { @@ -114,10 +128,10 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; TrainedModelMetadata that = (TrainedModelMetadata) o; - return Objects.equals(totalFeatureImportances, that.totalFeatureImportances) && - Objects.equals(featureImportanceBaselines, that.featureImportanceBaselines) && - Objects.equals(hyperparameters, that.hyperparameters) && - Objects.equals(modelId, that.modelId); + return Objects.equals(totalFeatureImportances, that.totalFeatureImportances) + && Objects.equals(featureImportanceBaselines, that.featureImportanceBaselines) + && Objects.equals(hyperparameters, that.hyperparameters) + && Objects.equals(modelId, that.modelId); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/tree/Tree.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/tree/Tree.java index 561b7e41e5cf9..d86794ca6a2f5 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/tree/Tree.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/tree/Tree.java @@ -10,11 +10,11 @@ import org.apache.lucene.util.Accountables; import org.apache.lucene.util.RamUsageEstimator; import org.elasticsearch.Version; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.LenientlyParsedTrainedModel; @@ -49,10 +49,7 @@ public class Tree implements LenientlyParsedTrainedModel, StrictlyParsedTrainedM private static final ObjectParser STRICT_PARSER = createParser(false); private static ObjectParser createParser(boolean lenient) { - ObjectParser parser = new ObjectParser<>( - NAME.getPreferredName(), - lenient, - Tree.Builder::new); + ObjectParser parser = new ObjectParser<>(NAME.getPreferredName(), lenient, Tree.Builder::new); parser.declareStringArray(Tree.Builder::setFeatureNames, FEATURE_NAMES); parser.declareObjectArray(Tree.Builder::setNodes, (p, c) -> TreeNode.fromXContent(p, lenient), TREE_STRUCTURE); parser.declareString(Tree.Builder::setTargetType, TargetType.TARGET_TYPE); @@ -75,7 +72,7 @@ public static Tree fromXContentLenient(XContentParser parser) { Tree(List featureNames, List nodes, TargetType targetType, List classificationLabels) { this.featureNames = Collections.unmodifiableList(ExceptionsHelper.requireNonNull(featureNames, FEATURE_NAMES)); - if(ExceptionsHelper.requireNonNull(nodes, TREE_STRUCTURE).size() == 0) { + if (ExceptionsHelper.requireNonNull(nodes, TREE_STRUCTURE).size() == 0) { throw new IllegalArgumentException("[tree_structure] must not be empty"); } this.nodes = Collections.unmodifiableList(nodes); @@ -126,11 +123,11 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.field(FEATURE_NAMES.getPreferredName(), featureNames); builder.field(TREE_STRUCTURE.getPreferredName(), nodes); builder.field(TargetType.TARGET_TYPE.getPreferredName(), targetType.toString()); - if(classificationLabels != null) { + if (classificationLabels != null) { builder.field(CLASSIFICATION_LABELS.getPreferredName(), classificationLabels); } builder.endObject(); - return builder; + return builder; } @Override @@ -162,14 +159,19 @@ public static Builder builder() { public void validate() { int maxFeatureIndex = maxFeatureIndex(); if (maxFeatureIndex >= featureNames.size()) { - throw ExceptionsHelper.badRequestException("feature index [{}] is out of bounds for the [{}] array", - maxFeatureIndex, FEATURE_NAMES.getPreferredName()); + throw ExceptionsHelper.badRequestException( + "feature index [{}] is out of bounds for the [{}] array", + maxFeatureIndex, + FEATURE_NAMES.getPreferredName() + ); } if (nodes.size() > 1) { if (featureNames.isEmpty()) { - throw ExceptionsHelper.badRequestException("[{}] is empty and the tree has > 1 nodes; num nodes [{}]. " + - "The model Must have features if tree is not a stump", - FEATURE_NAMES.getPreferredName(), nodes.size()); + throw ExceptionsHelper.badRequestException( + "[{}] is empty and the tree has > 1 nodes; num nodes [{}]. " + "The model Must have features if tree is not a stump", + FEATURE_NAMES.getPreferredName(), + nodes.size() + ); } } checkTargetType(); @@ -181,7 +183,7 @@ public void validate() { @Override public long estimatedNumOperations() { // Grabbing the features from the doc + the depth of the tree - return (long)Math.ceil(Math.log(nodes.size())) + featureNames.size(); + return (long) Math.ceil(Math.log(nodes.size())) + featureNames.size(); } /** @@ -203,12 +205,10 @@ int maxFeatureIndex() { private void checkTargetType() { if (this.classificationLabels != null && this.targetType != TargetType.CLASSIFICATION) { - throw ExceptionsHelper.badRequestException( - "[target_type] should be [classification] if [classification_labels] are provided"); + throw ExceptionsHelper.badRequestException("[target_type] should be [classification] if [classification_labels] are provided"); } if (this.targetType != TargetType.CLASSIFICATION && this.nodes.stream().anyMatch(n -> n.getLeafValue().length > 1)) { - throw ExceptionsHelper.badRequestException( - "[target_type] should be [classification] if leaf nodes have multiple values"); + throw ExceptionsHelper.badRequestException("[target_type] should be [classification] if leaf nodes have multiple values"); } } @@ -216,7 +216,7 @@ private void detectCycle() { Set visited = new HashSet<>(nodes.size()); Queue toVisit = new ArrayDeque<>(nodes.size()); toVisit.add(0); - while(toVisit.isEmpty() == false) { + while (toVisit.isEmpty() == false) { Integer nodeIdx = toVisit.remove(); if (visited.contains(nodeIdx)) { throw ExceptionsHelper.badRequestException("[tree] contains cycle at node {}", nodeIdx); @@ -258,8 +258,7 @@ private void verifyLeafNodeUniformity() { if (leafValueLengths == null) { leafValueLengths = node.getLeafValue().length; } else if (leafValueLengths != node.getLeafValue().length) { - throw ExceptionsHelper.badRequestException( - "[tree.tree_structure] all leaf nodes must have the same number of values"); + throw ExceptionsHelper.badRequestException("[tree.tree_structure] all leaf nodes must have the same number of values"); } } } @@ -334,7 +333,6 @@ public Builder setNodes(TreeNode.Builder... nodes) { return setNodes(Arrays.asList(nodes)); } - public Builder setTargetType(TargetType targetType) { this.targetType = targetType; return this; @@ -403,10 +401,12 @@ public Tree build() { if (nodes.stream().anyMatch(Objects::isNull)) { throw ExceptionsHelper.badRequestException("[tree] cannot contain null nodes"); } - return new Tree(featureNames, + return new Tree( + featureNames, nodes.stream().map(TreeNode.Builder::build).collect(Collectors.toList()), targetType, - classificationLabels); + classificationLabels + ); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/tree/TreeNode.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/tree/TreeNode.java index 79b23c5335633..ba5ef0ae663e4 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/tree/TreeNode.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/tree/TreeNode.java @@ -9,12 +9,12 @@ import org.apache.lucene.util.Accountable; import org.apache.lucene.util.RamUsageEstimator; import org.elasticsearch.common.Numbers; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -26,7 +26,6 @@ import java.util.List; import java.util.Objects; - public class TreeNode implements ToXContentObject, Writeable, Accountable { private static final long SHALLOW_SIZE = RamUsageEstimator.shallowSizeOfInstance(TreeNode.class); @@ -47,15 +46,14 @@ public class TreeNode implements ToXContentObject, Writeable, Accountable { private static final ObjectParser STRICT_PARSER = createParser(false); private static ObjectParser createParser(boolean lenient) { - ObjectParser parser = new ObjectParser<>( - NAME, - lenient, - TreeNode.Builder::new); + ObjectParser parser = new ObjectParser<>(NAME, lenient, TreeNode.Builder::new); parser.declareDouble(TreeNode.Builder::setThreshold, THRESHOLD); - parser.declareField(TreeNode.Builder::setOperator, + parser.declareField( + TreeNode.Builder::setOperator, p -> Operator.fromString(p.text()), DECISION_TYPE, - ObjectParser.ValueType.STRING); + ObjectParser.ValueType.STRING + ); parser.declareInt(TreeNode.Builder::setLeftChild, LEFT_CHILD); parser.declareInt(TreeNode.Builder::setRightChild, RIGHT_CHILD); parser.declareBoolean(TreeNode.Builder::setDefaultLeft, DEFAULT_LEFT); @@ -82,25 +80,26 @@ public static TreeNode.Builder fromXContent(XContentParser parser, boolean lenie private final int rightChild; private final long numberSamples; - - private TreeNode(Operator operator, - Double threshold, - Integer splitFeature, - int nodeIndex, - Double splitGain, - List leafValue, - Boolean defaultLeft, - Integer leftChild, - Integer rightChild, - long numberSamples) { + private TreeNode( + Operator operator, + Double threshold, + Integer splitFeature, + int nodeIndex, + Double splitGain, + List leafValue, + Boolean defaultLeft, + Integer leftChild, + Integer rightChild, + long numberSamples + ) { this.operator = operator == null ? Operator.LTE : operator; - this.threshold = threshold == null ? Double.NaN : threshold; + this.threshold = threshold == null ? Double.NaN : threshold; this.splitFeature = splitFeature == null ? -1 : splitFeature; this.nodeIndex = nodeIndex; - this.splitGain = splitGain == null ? Double.NaN : splitGain; + this.splitGain = splitGain == null ? Double.NaN : splitGain; this.leafValue = leafValue == null ? new double[0] : leafValue.stream().mapToDouble(Double::doubleValue).toArray(); this.defaultLeft = defaultLeft == null ? false : defaultLeft; - this.leftChild = leftChild == null ? -1 : leftChild; + this.leftChild = leftChild == null ? -1 : leftChild; this.rightChild = rightChild == null ? -1 : rightChild; if (numberSamples < 0) { throw new IllegalArgumentException("[" + NUMBER_SAMPLES.getPreferredName() + "] must be greater than or equal to 0"); @@ -229,7 +228,8 @@ public boolean equals(Object o) { @Override public int hashCode() { - return Objects.hash(operator, + return Objects.hash( + operator, threshold, splitFeature, splitGain, @@ -238,7 +238,8 @@ public int hashCode() { defaultLeft, leftChild, rightChild, - numberSamples); + numberSamples + ); } @Override @@ -271,8 +272,7 @@ public Builder(int nodeIndex) { this.nodeIndex = nodeIndex; } - private Builder() { - } + private Builder() {} public Builder setOperator(Operator operator) { this.operator = operator; @@ -366,7 +366,8 @@ public void validate() { public TreeNode build() { validate(); - return new TreeNode(operator, + return new TreeNode( + operator, threshold, splitFeature, nodeIndex, @@ -375,7 +376,8 @@ public TreeNode build() { defaultLeft, leftChild, rightChild, - numberSamples); + numberSamples + ); } } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/utils/Statistics.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/utils/Statistics.java index 9759396455860..b3025504c9389 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/utils/Statistics.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/inference/utils/Statistics.java @@ -10,7 +10,7 @@ public final class Statistics { - private Statistics(){} + private Statistics() {} /** * Calculates the softMax of the passed values. @@ -52,7 +52,7 @@ public static double[] softMax(double[] values) { } public static double sigmoid(double value) { - return 1/(1 + Math.exp(-value)); + return 1 / (1 + Math.exp(-value)); } private static boolean isValid(double v) { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/AnalysisConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/AnalysisConfig.java index 88281f1849bd5..96f7ae111cab9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/AnalysisConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/AnalysisConfig.java @@ -11,12 +11,12 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.xpack.core.common.time.TimeUtils; import org.elasticsearch.xpack.core.ml.job.messages.Messages; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; @@ -79,30 +79,44 @@ public class AnalysisConfig implements ToXContentObject, Writeable { @SuppressWarnings("unchecked") private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { - ConstructingObjectParser parser = new ConstructingObjectParser<>(ANALYSIS_CONFIG.getPreferredName(), - ignoreUnknownFields, a -> new AnalysisConfig.Builder((List) a[0])); - - parser.declareObjectArray(ConstructingObjectParser.constructorArg(), - (p, c) -> (ignoreUnknownFields ? Detector.LENIENT_PARSER : Detector.STRICT_PARSER).apply(p, c).build(), DETECTORS); - parser.declareString((builder, val) -> - builder.setBucketSpan(TimeValue.parseTimeValue(val, BUCKET_SPAN.getPreferredName())), BUCKET_SPAN); + ConstructingObjectParser parser = new ConstructingObjectParser<>( + ANALYSIS_CONFIG.getPreferredName(), + ignoreUnknownFields, + a -> new AnalysisConfig.Builder((List) a[0]) + ); + + parser.declareObjectArray( + ConstructingObjectParser.constructorArg(), + (p, c) -> (ignoreUnknownFields ? Detector.LENIENT_PARSER : Detector.STRICT_PARSER).apply(p, c).build(), + DETECTORS + ); + parser.declareString( + (builder, val) -> builder.setBucketSpan(TimeValue.parseTimeValue(val, BUCKET_SPAN.getPreferredName())), + BUCKET_SPAN + ); parser.declareString(Builder::setCategorizationFieldName, CATEGORIZATION_FIELD_NAME); parser.declareStringArray(Builder::setCategorizationFilters, CATEGORIZATION_FILTERS); // This one is nasty - the syntax for analyzers takes either names or objects at many levels, hence it's not // possible to simply declare whether the field is a string or object and a completely custom parser is required - parser.declareField(Builder::setCategorizationAnalyzerConfig, + parser.declareField( + Builder::setCategorizationAnalyzerConfig, (p, c) -> CategorizationAnalyzerConfig.buildFromXContentFragment(p, ignoreUnknownFields), - CATEGORIZATION_ANALYZER, ObjectParser.ValueType.OBJECT_OR_STRING); - parser.declareObject(Builder::setPerPartitionCategorizationConfig, + CATEGORIZATION_ANALYZER, + ObjectParser.ValueType.OBJECT_OR_STRING + ); + parser.declareObject( + Builder::setPerPartitionCategorizationConfig, ignoreUnknownFields ? PerPartitionCategorizationConfig.LENIENT_PARSER : PerPartitionCategorizationConfig.STRICT_PARSER, - PER_PARTITION_CATEGORIZATION); - parser.declareString((builder, val) -> - builder.setLatency(TimeValue.parseTimeValue(val, LATENCY.getPreferredName())), LATENCY); + PER_PARTITION_CATEGORIZATION + ); + parser.declareString((builder, val) -> builder.setLatency(TimeValue.parseTimeValue(val, LATENCY.getPreferredName())), LATENCY); parser.declareString(Builder::setSummaryCountFieldName, SUMMARY_COUNT_FIELD_NAME); parser.declareStringArray(Builder::setInfluencers, INFLUENCERS); parser.declareBoolean(Builder::setMultivariateByFields, MULTIVARIATE_BY_FIELDS); - parser.declareString((builder, val) -> - builder.setModelPruneWindow(TimeValue.parseTimeValue(val, MODEL_PRUNE_WINDOW.getPreferredName())), MODEL_PRUNE_WINDOW); + parser.declareString( + (builder, val) -> builder.setModelPruneWindow(TimeValue.parseTimeValue(val, MODEL_PRUNE_WINDOW.getPreferredName())), + MODEL_PRUNE_WINDOW + ); return parser; } @@ -122,12 +136,19 @@ private static ConstructingObjectParser createPars private final Boolean multivariateByFields; private final TimeValue modelPruneWindow; - - private AnalysisConfig(TimeValue bucketSpan, String categorizationFieldName, List categorizationFilters, - CategorizationAnalyzerConfig categorizationAnalyzerConfig, - PerPartitionCategorizationConfig perPartitionCategorizationConfig, TimeValue latency, - String summaryCountFieldName, List detectors, List influencers, Boolean multivariateByFields, - TimeValue modelPruneWindow) { + private AnalysisConfig( + TimeValue bucketSpan, + String categorizationFieldName, + List categorizationFilters, + CategorizationAnalyzerConfig categorizationAnalyzerConfig, + PerPartitionCategorizationConfig perPartitionCategorizationConfig, + TimeValue latency, + String summaryCountFieldName, + List detectors, + List influencers, + Boolean multivariateByFields, + TimeValue modelPruneWindow + ) { this.detectors = detectors; this.bucketSpan = bucketSpan; this.latency = latency; @@ -273,8 +294,7 @@ static SortedSet termFields(List detectors, List influ } public Set extractReferencedFilters() { - return detectors.stream().map(Detector::extractReferencedFilters) - .flatMap(Set::stream).collect(Collectors.toSet()); + return detectors.stream().map(Detector::extractReferencedFilters).flatMap(Set::stream).collect(Collectors.toSet()); } public Boolean getMultivariateByFields() { @@ -321,8 +341,7 @@ public List fields() { return collectNonNullAndNonEmptyDetectorFields(Detector::getFieldName); } - private List collectNonNullAndNonEmptyDetectorFields( - Function fieldGetter) { + private List collectNonNullAndNonEmptyDetectorFields(Function fieldGetter) { Set fields = new HashSet<>(); for (Detector d : getDetectors()) { @@ -343,7 +362,6 @@ public List overFields() { return collectNonNullAndNonEmptyDetectorFields(Detector::getOverFieldName); } - public List partitionFields() { return collectNonNullAndNonEmptyDetectorFields(Detector::getPartitionFieldName); } @@ -376,7 +394,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.field(SUMMARY_COUNT_FIELD_NAME.getPreferredName(), summaryCountFieldName); } builder.startArray(DETECTORS.getPreferredName()); - for (Detector detector: detectors) { + for (Detector detector : detectors) { detector.toXContent(builder, params); } builder.endArray(); @@ -397,25 +415,34 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; AnalysisConfig that = (AnalysisConfig) o; - return Objects.equals(latency, that.latency) && - Objects.equals(bucketSpan, that.bucketSpan) && - Objects.equals(categorizationFieldName, that.categorizationFieldName) && - Objects.equals(categorizationFilters, that.categorizationFilters) && - Objects.equals(categorizationAnalyzerConfig, that.categorizationAnalyzerConfig) && - Objects.equals(perPartitionCategorizationConfig, that.perPartitionCategorizationConfig) && - Objects.equals(summaryCountFieldName, that.summaryCountFieldName) && - Objects.equals(detectors, that.detectors) && - Objects.equals(influencers, that.influencers) && - Objects.equals(multivariateByFields, that.multivariateByFields) && - Objects.equals(modelPruneWindow, that.modelPruneWindow); + return Objects.equals(latency, that.latency) + && Objects.equals(bucketSpan, that.bucketSpan) + && Objects.equals(categorizationFieldName, that.categorizationFieldName) + && Objects.equals(categorizationFilters, that.categorizationFilters) + && Objects.equals(categorizationAnalyzerConfig, that.categorizationAnalyzerConfig) + && Objects.equals(perPartitionCategorizationConfig, that.perPartitionCategorizationConfig) + && Objects.equals(summaryCountFieldName, that.summaryCountFieldName) + && Objects.equals(detectors, that.detectors) + && Objects.equals(influencers, that.influencers) + && Objects.equals(multivariateByFields, that.multivariateByFields) + && Objects.equals(modelPruneWindow, that.modelPruneWindow); } @Override public int hashCode() { return Objects.hash( - bucketSpan, categorizationFieldName, categorizationFilters, categorizationAnalyzerConfig, - perPartitionCategorizationConfig, latency, summaryCountFieldName, detectors, influencers, multivariateByFields, - modelPruneWindow); + bucketSpan, + categorizationFieldName, + categorizationFilters, + categorizationAnalyzerConfig, + perPartitionCategorizationConfig, + latency, + summaryCountFieldName, + detectors, + influencers, + multivariateByFields, + modelPruneWindow + ); } public static class Builder { @@ -443,8 +470,9 @@ public Builder(AnalysisConfig analysisConfig) { this.bucketSpan = analysisConfig.bucketSpan; this.latency = analysisConfig.latency; this.categorizationFieldName = analysisConfig.categorizationFieldName; - this.categorizationFilters = analysisConfig.categorizationFilters == null ? null - : new ArrayList<>(analysisConfig.categorizationFilters); + this.categorizationFilters = analysisConfig.categorizationFilters == null + ? null + : new ArrayList<>(analysisConfig.categorizationFilters); this.categorizationAnalyzerConfig = analysisConfig.categorizationAnalyzerConfig; this.perPartitionCategorizationConfig = analysisConfig.perPartitionCategorizationConfig; this.summaryCountFieldName = analysisConfig.summaryCountFieldName; @@ -501,8 +529,10 @@ public Builder setCategorizationAnalyzerConfig(CategorizationAnalyzerConfig cate } public Builder setPerPartitionCategorizationConfig(PerPartitionCategorizationConfig perPartitionCategorizationConfig) { - this.perPartitionCategorizationConfig = - ExceptionsHelper.requireNonNull(perPartitionCategorizationConfig, PER_PARTITION_CATEGORIZATION.getPreferredName()); + this.perPartitionCategorizationConfig = ExceptionsHelper.requireNonNull( + perPartitionCategorizationConfig, + PER_PARTITION_CATEGORIZATION.getPreferredName() + ); return this; } @@ -560,9 +590,19 @@ public AnalysisConfig build() { verifyNoInconsistentNestedFieldNames(); - return new AnalysisConfig(bucketSpan, categorizationFieldName, categorizationFilters, categorizationAnalyzerConfig, - perPartitionCategorizationConfig, latency, summaryCountFieldName, detectors, influencers, multivariateByFields, - modelPruneWindow); + return new AnalysisConfig( + bucketSpan, + categorizationFieldName, + categorizationFilters, + categorizationAnalyzerConfig, + perPartitionCategorizationConfig, + latency, + summaryCountFieldName, + detectors, + influencers, + multivariateByFields, + modelPruneWindow + ); } private void verifyModelPruneWindow() { @@ -574,14 +614,33 @@ private void verifyModelPruneWindow() { long bucketSpanSecs = bucketSpan.seconds(); if (modelPruneWindowSecs % bucketSpanSecs != 0) { - throw ExceptionsHelper.badRequestException(MODEL_PRUNE_WINDOW.getPreferredName() + " [" + modelPruneWindow.toString() + "]" - + " must be a multiple of " + BUCKET_SPAN.getPreferredName() + " [" + bucketSpan.toString() + "]"); + throw ExceptionsHelper.badRequestException( + MODEL_PRUNE_WINDOW.getPreferredName() + + " [" + + modelPruneWindow.toString() + + "]" + + " must be a multiple of " + + BUCKET_SPAN.getPreferredName() + + " [" + + bucketSpan.toString() + + "]" + ); } if (modelPruneWindowSecs / bucketSpanSecs < MINIMUM_MODEL_PRUNE_WINDOW_BUCKETS) { - throw ExceptionsHelper.badRequestException(MODEL_PRUNE_WINDOW.getPreferredName() + " [" + modelPruneWindow.toString() + "]" - + " must be at least " + MINIMUM_MODEL_PRUNE_WINDOW_BUCKETS + " times greater than " + BUCKET_SPAN.getPreferredName() - + " [" + bucketSpan.toString() + "]"); + throw ExceptionsHelper.badRequestException( + MODEL_PRUNE_WINDOW.getPreferredName() + + " [" + + modelPruneWindow.toString() + + "]" + + " must be at least " + + MINIMUM_MODEL_PRUNE_WINDOW_BUCKETS + + " times greater than " + + BUCKET_SPAN.getPreferredName() + + " [" + + bucketSpan.toString() + + "]" + ); } } @@ -591,8 +650,9 @@ private void verifyConfigConsistentWithPerPartitionCategorization() { } if (categorizationFieldName == null) { - throw ExceptionsHelper.badRequestException(CATEGORIZATION_FIELD_NAME.getPreferredName() - + " must be set when per-partition categorization is enabled"); + throw ExceptionsHelper.badRequestException( + CATEGORIZATION_FIELD_NAME.getPreferredName() + " must be set when per-partition categorization is enabled" + ); } AtomicReference singlePartitionFieldName = new AtomicReference<>(); @@ -600,33 +660,44 @@ private void verifyConfigConsistentWithPerPartitionCategorization() { String thisDetectorPartitionFieldName = d.getPartitionFieldName(); if (d.getByOverPartitionTerms().contains(ML_CATEGORY_FIELD)) { if (ML_CATEGORY_FIELD.equals(d.getPartitionFieldName())) { - throw ExceptionsHelper.badRequestException(ML_CATEGORY_FIELD + " cannot be used as a " - + Detector.PARTITION_FIELD_NAME_FIELD.getPreferredName() - + " when per-partition categorization is enabled"); + throw ExceptionsHelper.badRequestException( + ML_CATEGORY_FIELD + + " cannot be used as a " + + Detector.PARTITION_FIELD_NAME_FIELD.getPreferredName() + + " when per-partition categorization is enabled" + ); } if (thisDetectorPartitionFieldName == null) { - throw ExceptionsHelper.badRequestException(Detector.PARTITION_FIELD_NAME_FIELD.getPreferredName() - + " must be set for detectors that reference " + ML_CATEGORY_FIELD - + " when per-partition categorization is enabled"); + throw ExceptionsHelper.badRequestException( + Detector.PARTITION_FIELD_NAME_FIELD.getPreferredName() + + " must be set for detectors that reference " + + ML_CATEGORY_FIELD + + " when per-partition categorization is enabled" + ); } } if (thisDetectorPartitionFieldName != null) { String previousPartitionFieldName = singlePartitionFieldName.getAndSet(thisDetectorPartitionFieldName); - if (previousPartitionFieldName != null && - previousPartitionFieldName.equals(thisDetectorPartitionFieldName) == false) { - throw ExceptionsHelper.badRequestException(Detector.PARTITION_FIELD_NAME_FIELD.getPreferredName() - + " cannot vary between detectors when per-partition categorization is enabled: [" - + previousPartitionFieldName + "] and [" + thisDetectorPartitionFieldName + "] are used"); + if (previousPartitionFieldName != null && previousPartitionFieldName.equals(thisDetectorPartitionFieldName) == false) { + throw ExceptionsHelper.badRequestException( + Detector.PARTITION_FIELD_NAME_FIELD.getPreferredName() + + " cannot vary between detectors when per-partition categorization is enabled: [" + + previousPartitionFieldName + + "] and [" + + thisDetectorPartitionFieldName + + "] are used" + ); } } }); } private void verifyNoMetricFunctionsWhenSummaryCountFieldNameIsSet() { - if (Strings.isNullOrEmpty(summaryCountFieldName) == false && - detectors.stream().anyMatch(d -> DetectorFunction.METRIC.equals(d.getFunction()))) { + if (Strings.isNullOrEmpty(summaryCountFieldName) == false + && detectors.stream().anyMatch(d -> DetectorFunction.METRIC.equals(d.getFunction()))) { throw ExceptionsHelper.badRequestException( - Messages.getMessage(Messages.JOB_CONFIG_FUNCTION_INCOMPATIBLE_PRESUMMARIZED, DetectorFunction.METRIC)); + Messages.getMessage(Messages.JOB_CONFIG_FUNCTION_INCOMPATIBLE_PRESUMMARIZED, DetectorFunction.METRIC) + ); } } @@ -652,8 +723,9 @@ private void verifyNoInconsistentNestedFieldNames() { String prevTermField = null; for (String termField : termFields) { if (prevTermField != null && termField.startsWith(prevTermField + ".")) { - throw ExceptionsHelper.badRequestException("Fields [" + prevTermField + "] and [" + termField + - "] cannot both be used in the same analysis_config"); + throw ExceptionsHelper.badRequestException( + "Fields [" + prevTermField + "] and [" + termField + "] cannot both be used in the same analysis_config" + ); } prevTermField = termField; } @@ -664,12 +736,17 @@ private void verifyMlCategoryIsUsedWhenCategorizationFieldNameIsSet() { detectors.forEach(d -> byOverPartitionFields.addAll(d.getByOverPartitionTerms())); boolean isMlCategoryUsed = byOverPartitionFields.contains(ML_CATEGORY_FIELD); if (isMlCategoryUsed && categorizationFieldName == null) { - throw ExceptionsHelper.badRequestException(CATEGORIZATION_FIELD_NAME.getPreferredName() - + " must be set for " + ML_CATEGORY_FIELD + " to be available"); + throw ExceptionsHelper.badRequestException( + CATEGORIZATION_FIELD_NAME.getPreferredName() + " must be set for " + ML_CATEGORY_FIELD + " to be available" + ); } if (categorizationFieldName != null && isMlCategoryUsed == false) { - throw ExceptionsHelper.badRequestException(CATEGORIZATION_FIELD_NAME.getPreferredName() - + " is set but " + ML_CATEGORY_FIELD + " is not used in any detector by/over/partition field"); + throw ExceptionsHelper.badRequestException( + CATEGORIZATION_FIELD_NAME.getPreferredName() + + " is set but " + + ML_CATEGORY_FIELD + + " is not used in any detector by/over/partition field" + ); } } @@ -683,8 +760,9 @@ private void verifyCategorizationAnalyzer() { private void verifyCategorizationFieldNameSetIfAnalyzerIsSet() { if (categorizationFieldName == null) { - throw ExceptionsHelper.badRequestException(Messages.getMessage( - Messages.JOB_CONFIG_CATEGORIZATION_ANALYZER_REQUIRES_CATEGORIZATION_FIELD_NAME)); + throw ExceptionsHelper.badRequestException( + Messages.getMessage(Messages.JOB_CONFIG_CATEGORIZATION_ANALYZER_REQUIRES_CATEGORIZATION_FIELD_NAME) + ); } } @@ -702,22 +780,25 @@ private void verifyCategorizationFilters() { private void verifyCategorizationAnalyzerNotSetIfFiltersAreSet() { if (categorizationAnalyzerConfig != null) { - throw ExceptionsHelper.badRequestException(Messages.getMessage( - Messages.JOB_CONFIG_CATEGORIZATION_FILTERS_INCOMPATIBLE_WITH_CATEGORIZATION_ANALYZER)); + throw ExceptionsHelper.badRequestException( + Messages.getMessage(Messages.JOB_CONFIG_CATEGORIZATION_FILTERS_INCOMPATIBLE_WITH_CATEGORIZATION_ANALYZER) + ); } } private void verifyCategorizationFieldNameSetIfFiltersAreSet() { if (categorizationFieldName == null) { - throw ExceptionsHelper.badRequestException(Messages.getMessage( - Messages.JOB_CONFIG_CATEGORIZATION_FILTERS_REQUIRE_CATEGORIZATION_FIELD_NAME)); + throw ExceptionsHelper.badRequestException( + Messages.getMessage(Messages.JOB_CONFIG_CATEGORIZATION_FILTERS_REQUIRE_CATEGORIZATION_FIELD_NAME) + ); } } private void verifyCategorizationFiltersAreDistinct() { if (categorizationFilters.stream().distinct().count() != categorizationFilters.size()) { throw ExceptionsHelper.badRequestException( - Messages.getMessage(Messages.JOB_CONFIG_CATEGORIZATION_FILTERS_CONTAINS_DUPLICATES)); + Messages.getMessage(Messages.JOB_CONFIG_CATEGORIZATION_FILTERS_CONTAINS_DUPLICATES) + ); } } @@ -731,7 +812,8 @@ private void verifyCategorizationFiltersAreValidRegex() { for (String filter : categorizationFilters) { if (isValidRegex(filter) == false) { throw ExceptionsHelper.badRequestException( - Messages.getMessage(Messages.JOB_CONFIG_CATEGORIZATION_FILTERS_CONTAINS_INVALID_REGEX, filter)); + Messages.getMessage(Messages.JOB_CONFIG_CATEGORIZATION_FILTERS_CONTAINS_INVALID_REGEX, filter) + ); } } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/AnalysisLimits.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/AnalysisLimits.java index aa68c3a1ddfe7..2e17d83a8b73c 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/AnalysisLimits.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/AnalysisLimits.java @@ -6,14 +6,14 @@ */ package org.elasticsearch.xpack.core.ml.job.config; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -52,9 +52,15 @@ public class AnalysisLimits implements ToXContentObject, Writeable { private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { ConstructingObjectParser parser = new ConstructingObjectParser<>( - "analysis_limits", ignoreUnknownFields, a -> ignoreUnknownFields ? new AnalysisLimits( - a[0] == null ? PRE_6_1_DEFAULT_MODEL_MEMORY_LIMIT_MB : (Long) a[0], - a[1] == null ? DEFAULT_CATEGORIZATION_EXAMPLES_LIMIT : (Long) a[1]) : new AnalysisLimits((Long) a[0], (Long) a[1])); + "analysis_limits", + ignoreUnknownFields, + a -> ignoreUnknownFields + ? new AnalysisLimits( + a[0] == null ? PRE_6_1_DEFAULT_MODEL_MEMORY_LIMIT_MB : (Long) a[0], + a[1] == null ? DEFAULT_CATEGORIZATION_EXAMPLES_LIMIT : (Long) a[1] + ) + : new AnalysisLimits((Long) a[0], (Long) a[1]) + ); parser.declareField(ConstructingObjectParser.optionalConstructorArg(), p -> { if (p.currentToken() == XContentParser.Token.VALUE_STRING) { @@ -94,8 +100,12 @@ public AnalysisLimits(Long modelMemoryLimitMb, Long categorizationExamplesLimit) throw ExceptionsHelper.badRequestException(msg); } if (categorizationExamplesLimit != null && categorizationExamplesLimit < 0) { - String msg = Messages.getMessage(Messages.JOB_CONFIG_FIELD_VALUE_TOO_LOW, CATEGORIZATION_EXAMPLES_LIMIT, 0, - categorizationExamplesLimit); + String msg = Messages.getMessage( + Messages.JOB_CONFIG_FIELD_VALUE_TOO_LOW, + CATEGORIZATION_EXAMPLES_LIMIT, + 0, + categorizationExamplesLimit + ); throw ExceptionsHelper.badRequestException(msg); } this.modelMemoryLimit = modelMemoryLimitMb; @@ -119,8 +129,11 @@ public AnalysisLimits(StreamInput in) throws IOException { * @param defaultModelMemoryLimit the default model memory limit to be used if an explicit value is missing * @return a new {@code AnalysisLimits} that is validated and has no missing values */ - public static AnalysisLimits validateAndSetDefaults(@Nullable AnalysisLimits source, @Nullable ByteSizeValue maxModelMemoryLimit, - long defaultModelMemoryLimit) { + public static AnalysisLimits validateAndSetDefaults( + @Nullable AnalysisLimits source, + @Nullable ByteSizeValue maxModelMemoryLimit, + long defaultModelMemoryLimit + ) { boolean maxModelMemoryIsSet = maxModelMemoryLimit != null && maxModelMemoryLimit.getMb() > 0; @@ -141,9 +154,13 @@ public static AnalysisLimits validateAndSetDefaults(@Nullable AnalysisLimits sou } if (maxModelMemoryIsSet && modelMemoryLimit > maxModelMemoryLimit.getMb()) { - throw ExceptionsHelper.badRequestException(Messages.getMessage(Messages.JOB_CONFIG_MODEL_MEMORY_LIMIT_GREATER_THAN_MAX, + throw ExceptionsHelper.badRequestException( + Messages.getMessage( + Messages.JOB_CONFIG_MODEL_MEMORY_LIMIT_GREATER_THAN_MAX, ByteSizeValue.ofMb(modelMemoryLimit), - maxModelMemoryLimit)); + maxModelMemoryLimit + ) + ); } return new AnalysisLimits(modelMemoryLimit, categorizationExamplesLimit); @@ -204,8 +221,8 @@ public boolean equals(Object other) { } AnalysisLimits that = (AnalysisLimits) other; - return Objects.equals(this.modelMemoryLimit, that.modelMemoryLimit) && - Objects.equals(this.categorizationExamplesLimit, that.categorizationExamplesLimit); + return Objects.equals(this.modelMemoryLimit, that.modelMemoryLimit) + && Objects.equals(this.categorizationExamplesLimit, that.categorizationExamplesLimit); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/Blocked.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/Blocked.java index fb5f454ba81b1..09ad804266e18 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/Blocked.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/Blocked.java @@ -10,12 +10,12 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.tasks.TaskId; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.tasks.TaskId; import java.io.IOException; import java.util.Locale; @@ -24,7 +24,10 @@ public class Blocked implements ToXContentObject, Writeable { public enum Reason { - NONE, DELETE, RESET, REVERT; + NONE, + DELETE, + RESET, + REVERT; public static Reason fromString(String value) { return Reason.valueOf(value.toUpperCase(Locale.ROOT)); @@ -43,8 +46,11 @@ public String toString() { public static final ConstructingObjectParser STRICT_PARSER = createParser(false); private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { - ConstructingObjectParser parser = new ConstructingObjectParser<>("blocked", ignoreUnknownFields, - a -> new Blocked((Reason) a[0], (TaskId) a[1])); + ConstructingObjectParser parser = new ConstructingObjectParser<>( + "blocked", + ignoreUnknownFields, + a -> new Blocked((Reason) a[0], (TaskId) a[1]) + ); parser.declareString(ConstructingObjectParser.constructorArg(), Reason::fromString, REASON); parser.declareString(ConstructingObjectParser.optionalConstructorArg(), TaskId::new, TASK_ID); return parser; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/CategorizationAnalyzerConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/CategorizationAnalyzerConfig.java index bf2849f568d55..e842e164b03c4 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/CategorizationAnalyzerConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/CategorizationAnalyzerConfig.java @@ -7,19 +7,19 @@ package org.elasticsearch.xpack.core.ml.job.config; import org.elasticsearch.action.admin.indices.analyze.AnalyzeAction; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.index.analysis.NameOrDefinition; +import org.elasticsearch.rest.action.admin.indices.RestAnalyzeAction; import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentFragment; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.index.analysis.NameOrDefinition; -import org.elasticsearch.rest.action.admin.indices.RestAnalyzeAction; import java.io.IOException; import java.util.ArrayList; @@ -70,7 +70,7 @@ public static CategorizationAnalyzerConfig buildFromXContentObject(XContentParse throw new IllegalArgumentException("Expected start object but got [" + parser.currentToken() + "]"); } if (parser.nextToken() != XContentParser.Token.FIELD_NAME - || CATEGORIZATION_ANALYZER.match(parser.currentName(), parser.getDeprecationHandler()) == false) { + || CATEGORIZATION_ANALYZER.match(parser.currentName(), parser.getDeprecationHandler()) == false) { throw new IllegalArgumentException("Expected [" + CATEGORIZATION_ANALYZER + "] field but got [" + parser.currentToken() + "]"); } parser.nextToken(); @@ -86,10 +86,8 @@ public static CategorizationAnalyzerConfig buildFromXContentObject(XContentParse * * The parser is strict when parsing config and lenient when parsing cluster state. */ - public static CategorizationAnalyzerConfig buildFromXContentFragment( - XContentParser parser, - boolean ignoreUnknownFields - ) throws IOException { + public static CategorizationAnalyzerConfig buildFromXContentFragment(XContentParser parser, boolean ignoreUnknownFields) + throws IOException { CategorizationAnalyzerConfig.Builder builder = new CategorizationAnalyzerConfig.Builder(); XContentParser.Token token = parser.currentToken(); @@ -103,43 +101,71 @@ public static CategorizationAnalyzerConfig buildFromXContentFragment( if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (CHAR_FILTERS.match(currentFieldName, parser.getDeprecationHandler()) - && token == XContentParser.Token.START_ARRAY) { - while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { + && token == XContentParser.Token.START_ARRAY) { + while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { + if (token == XContentParser.Token.VALUE_STRING) { + builder.addCharFilter(parser.text()); + } else if (token == XContentParser.Token.START_OBJECT) { + builder.addCharFilter(parser.map()); + } else { + throw new IllegalArgumentException( + "[" + + currentFieldName + + "] in [" + + CATEGORIZATION_ANALYZER + + "] array element should contain char_filter's name or settings [" + + token + + "]" + ); + } + } + } else if (TOKENIZER.match(currentFieldName, parser.getDeprecationHandler())) { if (token == XContentParser.Token.VALUE_STRING) { - builder.addCharFilter(parser.text()); + builder.setTokenizer(parser.text()); } else if (token == XContentParser.Token.START_OBJECT) { - builder.addCharFilter(parser.map()); + builder.setTokenizer(parser.map()); } else { - throw new IllegalArgumentException("[" + currentFieldName + "] in [" + CATEGORIZATION_ANALYZER + - "] array element should contain char_filter's name or settings [" + token + "]"); + throw new IllegalArgumentException( + "[" + + currentFieldName + + "] in [" + + CATEGORIZATION_ANALYZER + + "] should be tokenizer's name or settings [" + + token + + "]" + ); } - } - } else if (TOKENIZER.match(currentFieldName, parser.getDeprecationHandler())) { - if (token == XContentParser.Token.VALUE_STRING) { - builder.setTokenizer(parser.text()); - } else if (token == XContentParser.Token.START_OBJECT) { - builder.setTokenizer(parser.map()); - } else { - throw new IllegalArgumentException("[" + currentFieldName + "] in [" + CATEGORIZATION_ANALYZER + - "] should be tokenizer's name or settings [" + token + "]"); - } - } else if (TOKEN_FILTERS.match(currentFieldName, parser.getDeprecationHandler()) + } else if (TOKEN_FILTERS.match(currentFieldName, parser.getDeprecationHandler()) && token == XContentParser.Token.START_ARRAY) { - while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { - if (token == XContentParser.Token.VALUE_STRING) { - builder.addTokenFilter(parser.text()); - } else if (token == XContentParser.Token.START_OBJECT) { - builder.addTokenFilter(parser.map()); - } else { - throw new IllegalArgumentException("[" + currentFieldName + "] in [" + CATEGORIZATION_ANALYZER + - "] array element should contain token_filter's name or settings [" + token + "]"); + while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { + if (token == XContentParser.Token.VALUE_STRING) { + builder.addTokenFilter(parser.text()); + } else if (token == XContentParser.Token.START_OBJECT) { + builder.addTokenFilter(parser.map()); + } else { + throw new IllegalArgumentException( + "[" + + currentFieldName + + "] in [" + + CATEGORIZATION_ANALYZER + + "] array element should contain token_filter's name or settings [" + + token + + "]" + ); + } + } + // Be lenient when parsing cluster state - assume unknown fields are from future versions + } else if (ignoreUnknownFields == false) { + throw new IllegalArgumentException( + "Parameter [" + + currentFieldName + + "] in [" + + CATEGORIZATION_ANALYZER + + "] is unknown or of the wrong type [" + + token + + "]" + ); } - } - // Be lenient when parsing cluster state - assume unknown fields are from future versions - } else if (ignoreUnknownFields == false) { - throw new IllegalArgumentException("Parameter [" + currentFieldName + "] in [" + CATEGORIZATION_ANALYZER + - "] is unknown or of the wrong type [" + token + "]"); - } } } @@ -155,8 +181,7 @@ public static CategorizationAnalyzerConfig buildFromXContentFragment( */ public static CategorizationAnalyzerConfig buildDefaultCategorizationAnalyzer(List categorizationFilters) { - return new CategorizationAnalyzerConfig.Builder() - .addCategorizationFilters(categorizationFilters) + return new CategorizationAnalyzerConfig.Builder().addCategorizationFilters(categorizationFilters) .setTokenizer("ml_classic") .addDateWordsTokenFilter() .build(); @@ -174,8 +199,7 @@ public static CategorizationAnalyzerConfig buildDefaultCategorizationAnalyzer(Li */ public static CategorizationAnalyzerConfig buildStandardCategorizationAnalyzer(List categorizationFilters) { - return new CategorizationAnalyzerConfig.Builder() - .addCharFilter("first_line_with_letters") + return new CategorizationAnalyzerConfig.Builder().addCharFilter("first_line_with_letters") .addCategorizationFilters(categorizationFilters) .setTokenizer("ml_standard") .addDateWordsTokenFilter() @@ -187,8 +211,12 @@ public static CategorizationAnalyzerConfig buildStandardCategorizationAnalyzer(L private final NameOrDefinition tokenizer; private final List tokenFilters; - private CategorizationAnalyzerConfig(String analyzer, List charFilters, NameOrDefinition tokenizer, - List tokenFilters) { + private CategorizationAnalyzerConfig( + String analyzer, + List charFilters, + NameOrDefinition tokenizer, + List tokenFilters + ) { this.analyzer = analyzer; this.charFilters = Objects.requireNonNull(charFilters); this.tokenizer = tokenizer; @@ -271,10 +299,10 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; CategorizationAnalyzerConfig that = (CategorizationAnalyzerConfig) o; - return Objects.equals(analyzer, that.analyzer) && - Objects.equals(charFilters, that.charFilters) && - Objects.equals(tokenizer, that.tokenizer) && - Objects.equals(tokenFilters, that.tokenFilters); + return Objects.equals(analyzer, that.analyzer) + && Objects.equals(charFilters, that.charFilters) + && Objects.equals(tokenizer, that.tokenizer) + && Objects.equals(tokenFilters, that.tokenFilters); } @Override @@ -289,8 +317,7 @@ public static class Builder { private NameOrDefinition tokenizer; private List tokenFilters = new ArrayList<>(); - public Builder() { - } + public Builder() {} public Builder(CategorizationAnalyzerConfig categorizationAnalyzerConfig) { this.analyzer = categorizationAnalyzerConfig.analyzer; @@ -349,12 +376,51 @@ public Builder addTokenFilter(Map tokenFilter) { Builder addDateWordsTokenFilter() { Map tokenFilter = new HashMap<>(); tokenFilter.put("type", "stop"); - tokenFilter.put("stopwords", Arrays.asList( - "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", - "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", - "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December", - "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", - "GMT", "UTC")); + tokenFilter.put( + "stopwords", + Arrays.asList( + "Monday", + "Tuesday", + "Wednesday", + "Thursday", + "Friday", + "Saturday", + "Sunday", + "Mon", + "Tue", + "Wed", + "Thu", + "Fri", + "Sat", + "Sun", + "January", + "February", + "March", + "April", + "May", + "June", + "July", + "August", + "September", + "October", + "November", + "December", + "Jan", + "Feb", + "Mar", + "Apr", + "May", + "Jun", + "Jul", + "Aug", + "Sep", + "Oct", + "Nov", + "Dec", + "GMT", + "UTC" + ) + ); addTokenFilter(tokenFilter); return this; } @@ -364,20 +430,24 @@ Builder addDateWordsTokenFilter() { */ public CategorizationAnalyzerConfig build() { if (analyzer == null && tokenizer == null) { - throw new IllegalArgumentException(CATEGORIZATION_ANALYZER + " that is not a global analyzer must specify a [" - + TOKENIZER + "] field"); + throw new IllegalArgumentException( + CATEGORIZATION_ANALYZER + " that is not a global analyzer must specify a [" + TOKENIZER + "] field" + ); } if (analyzer != null && charFilters.isEmpty() == false) { - throw new IllegalArgumentException(CATEGORIZATION_ANALYZER + " that is a global analyzer cannot also specify a [" - + CHAR_FILTERS + "] field"); + throw new IllegalArgumentException( + CATEGORIZATION_ANALYZER + " that is a global analyzer cannot also specify a [" + CHAR_FILTERS + "] field" + ); } if (analyzer != null && tokenizer != null) { - throw new IllegalArgumentException(CATEGORIZATION_ANALYZER + " that is a global analyzer cannot also specify a [" - + TOKENIZER + "] field"); + throw new IllegalArgumentException( + CATEGORIZATION_ANALYZER + " that is a global analyzer cannot also specify a [" + TOKENIZER + "] field" + ); } if (analyzer != null && tokenFilters.isEmpty() == false) { - throw new IllegalArgumentException(CATEGORIZATION_ANALYZER + " that is a global analyzer cannot also specify a [" - + TOKEN_FILTERS + "] field"); + throw new IllegalArgumentException( + CATEGORIZATION_ANALYZER + " that is a global analyzer cannot also specify a [" + TOKEN_FILTERS + "] field" + ); } return new CategorizationAnalyzerConfig(analyzer, charFilters, tokenizer, tokenFilters); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/DataDescription.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/DataDescription.java index 149a6092ee8aa..85712f611ca32 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/DataDescription.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/DataDescription.java @@ -7,11 +7,11 @@ package org.elasticsearch.xpack.core.ml.job.config; import org.elasticsearch.Version; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ml.job.messages.Messages; @@ -109,8 +109,11 @@ public String toString() { public static final ObjectParser STRICT_PARSER = createParser(false); private static ObjectParser createParser(boolean ignoreUnknownFields) { - ObjectParser parser = - new ObjectParser<>(DATA_DESCRIPTION_FIELD.getPreferredName(), ignoreUnknownFields, Builder::new); + ObjectParser parser = new ObjectParser<>( + DATA_DESCRIPTION_FIELD.getPreferredName(), + ignoreUnknownFields, + Builder::new + ); if (ignoreUnknownFields == false) { // The strict parser needs to tolerate this field as it's documented, but there's only one value so we don't need to store it @@ -254,7 +257,9 @@ public Builder setTimeFormat(String format) { DateTimeFormatterTimestampConverter.ofPattern(format, ZoneOffset.UTC); } catch (IllegalArgumentException e) { throw ExceptionsHelper.badRequestException( - Messages.getMessage(Messages.JOB_CONFIG_INVALID_TIMEFORMAT, format), e.getCause()); + Messages.getMessage(Messages.JOB_CONFIG_INVALID_TIMEFORMAT, format), + e.getCause() + ); } } timeFormat = format; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/DefaultDetectorDescription.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/DefaultDetectorDescription.java index 1e38ca1fb4b24..982e1cf73a1a9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/DefaultDetectorDescription.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/DefaultDetectorDescription.java @@ -9,7 +9,6 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.xpack.core.ml.utils.MlStrings; - public final class DefaultDetectorDescription { private static final String BY_TOKEN = " by "; private static final String OVER_TOKEN = " over "; @@ -45,8 +44,7 @@ public static void appendOn(Detector detector, StringBuilder sb) { if (isNotNullOrEmpty(detector.getFunction().getFullName())) { sb.append(detector.getFunction()); if (isNotNullOrEmpty(detector.getFieldName())) { - sb.append('(').append(quoteField(detector.getFieldName())) - .append(')'); + sb.append('(').append(quoteField(detector.getFieldName())).append(')'); } } else if (isNotNullOrEmpty(detector.getFieldName())) { sb.append(quoteField(detector.getFieldName())); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/DetectionRule.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/DetectionRule.java index 2cc3fb9b2a57a..ddfccfc7f62e0 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/DetectionRule.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/DetectionRule.java @@ -6,11 +6,11 @@ */ package org.elasticsearch.xpack.core.ml.job.config; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ml.job.messages.Messages; @@ -40,8 +40,11 @@ private static ObjectParser createParser(boolean ignoreUnknownFie parser.declareStringArray(Builder::setActions, ACTIONS_FIELD); parser.declareObject(Builder::setScope, RuleScope.parser(ignoreUnknownFields), SCOPE_FIELD); - parser.declareObjectArray(Builder::setConditions, ignoreUnknownFields ? RuleCondition.LENIENT_PARSER : RuleCondition.STRICT_PARSER, - CONDITIONS_FIELD); + parser.declareObjectArray( + Builder::setConditions, + ignoreUnknownFields ? RuleCondition.LENIENT_PARSER : RuleCondition.STRICT_PARSER, + CONDITIONS_FIELD + ); return parser; } @@ -110,9 +113,7 @@ public boolean equals(Object obj) { } DetectionRule other = (DetectionRule) obj; - return Objects.equals(actions, other.actions) - && Objects.equals(scope, other.scope) - && Objects.equals(conditions, other.conditions); + return Objects.equals(actions, other.actions) && Objects.equals(scope, other.scope) && Objects.equals(conditions, other.conditions); } @Override @@ -133,8 +134,7 @@ public Builder(List conditions) { this.conditions = ExceptionsHelper.requireNonNull(conditions, CONDITIONS_FIELD.getPreferredName()); } - Builder() { - } + Builder() {} public Builder setActions(List actions) { this.actions.clear(); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/Detector.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/Detector.java index a821e1ecca0ff..f8ebd02e98a55 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/Detector.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/Detector.java @@ -7,12 +7,12 @@ package org.elasticsearch.xpack.core.ml.job.config; import org.elasticsearch.ElasticsearchParseException; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ml.job.messages.Messages; @@ -33,7 +33,6 @@ import java.util.TreeSet; import java.util.stream.Collectors; - /** * Defines the fields to be used in the analysis. * fieldname must be set and only one of byFieldName @@ -99,9 +98,11 @@ private static ObjectParser createParser(boolean ignoreUnknownFie parser.declareString(Builder::setPartitionFieldName, PARTITION_FIELD_NAME_FIELD); parser.declareBoolean(Builder::setUseNull, USE_NULL_FIELD); parser.declareString(Builder::setExcludeFrequent, ExcludeFrequent::forString, EXCLUDE_FREQUENT_FIELD); - parser.declareObjectArray(Builder::setRules, + parser.declareObjectArray( + Builder::setRules, (p, c) -> (ignoreUnknownFields ? DetectionRule.LENIENT_PARSER : DetectionRule.STRICT_PARSER).apply(p, c).build(), - CUSTOM_RULES_FIELD); + CUSTOM_RULES_FIELD + ); parser.declareInt(Builder::setDetectorIndex, DETECTOR_INDEX); return parser; @@ -114,69 +115,67 @@ private static ObjectParser createParser(boolean ignoreUnknownFie * The set of functions that do not require a field, by field or over field */ public static final EnumSet COUNT_WITHOUT_FIELD_FUNCTIONS = EnumSet.of( - DetectorFunction.COUNT, - DetectorFunction.HIGH_COUNT, - DetectorFunction.LOW_COUNT, - DetectorFunction.NON_ZERO_COUNT, - DetectorFunction.LOW_NON_ZERO_COUNT, - DetectorFunction.HIGH_NON_ZERO_COUNT, - DetectorFunction.TIME_OF_DAY, - DetectorFunction.TIME_OF_WEEK + DetectorFunction.COUNT, + DetectorFunction.HIGH_COUNT, + DetectorFunction.LOW_COUNT, + DetectorFunction.NON_ZERO_COUNT, + DetectorFunction.LOW_NON_ZERO_COUNT, + DetectorFunction.HIGH_NON_ZERO_COUNT, + DetectorFunction.TIME_OF_DAY, + DetectorFunction.TIME_OF_WEEK ); /** * The set of functions that require a fieldname */ public static final EnumSet FIELD_NAME_FUNCTIONS = EnumSet.of( - DetectorFunction.DISTINCT_COUNT, - DetectorFunction.LOW_DISTINCT_COUNT, - DetectorFunction.HIGH_DISTINCT_COUNT, - DetectorFunction.INFO_CONTENT, - DetectorFunction.LOW_INFO_CONTENT, - DetectorFunction.HIGH_INFO_CONTENT, - DetectorFunction.METRIC, - DetectorFunction.MEAN, DetectorFunction.AVG, - DetectorFunction.HIGH_MEAN, DetectorFunction.HIGH_AVG, - DetectorFunction.LOW_MEAN, DetectorFunction.LOW_AVG, - DetectorFunction.MEDIAN, - DetectorFunction.LOW_MEDIAN, - DetectorFunction.HIGH_MEDIAN, - DetectorFunction.MIN, - DetectorFunction.MAX, - DetectorFunction.SUM, - DetectorFunction.LOW_SUM, - DetectorFunction.HIGH_SUM, - DetectorFunction.NON_NULL_SUM, - DetectorFunction.LOW_NON_NULL_SUM, - DetectorFunction.HIGH_NON_NULL_SUM, - DetectorFunction.VARP, - DetectorFunction.LOW_VARP, - DetectorFunction.HIGH_VARP, - DetectorFunction.LAT_LONG + DetectorFunction.DISTINCT_COUNT, + DetectorFunction.LOW_DISTINCT_COUNT, + DetectorFunction.HIGH_DISTINCT_COUNT, + DetectorFunction.INFO_CONTENT, + DetectorFunction.LOW_INFO_CONTENT, + DetectorFunction.HIGH_INFO_CONTENT, + DetectorFunction.METRIC, + DetectorFunction.MEAN, + DetectorFunction.AVG, + DetectorFunction.HIGH_MEAN, + DetectorFunction.HIGH_AVG, + DetectorFunction.LOW_MEAN, + DetectorFunction.LOW_AVG, + DetectorFunction.MEDIAN, + DetectorFunction.LOW_MEDIAN, + DetectorFunction.HIGH_MEDIAN, + DetectorFunction.MIN, + DetectorFunction.MAX, + DetectorFunction.SUM, + DetectorFunction.LOW_SUM, + DetectorFunction.HIGH_SUM, + DetectorFunction.NON_NULL_SUM, + DetectorFunction.LOW_NON_NULL_SUM, + DetectorFunction.HIGH_NON_NULL_SUM, + DetectorFunction.VARP, + DetectorFunction.LOW_VARP, + DetectorFunction.HIGH_VARP, + DetectorFunction.LAT_LONG ); /** * The set of functions that require a by fieldname */ - public static final EnumSet BY_FIELD_NAME_FUNCTIONS = EnumSet.of( - DetectorFunction.RARE, - DetectorFunction.FREQ_RARE - ); + public static final EnumSet BY_FIELD_NAME_FUNCTIONS = EnumSet.of(DetectorFunction.RARE, DetectorFunction.FREQ_RARE); /** * The set of functions that require a over fieldname */ - public static final EnumSet OVER_FIELD_NAME_FUNCTIONS = EnumSet.of( - DetectorFunction.FREQ_RARE - ); + public static final EnumSet OVER_FIELD_NAME_FUNCTIONS = EnumSet.of(DetectorFunction.FREQ_RARE); /** * The set of functions that cannot have an over fieldname */ public static final EnumSet NO_OVER_FIELD_NAME_FUNCTIONS = EnumSet.of( - DetectorFunction.NON_ZERO_COUNT, - DetectorFunction.LOW_NON_ZERO_COUNT, - DetectorFunction.HIGH_NON_ZERO_COUNT + DetectorFunction.NON_ZERO_COUNT, + DetectorFunction.LOW_NON_ZERO_COUNT, + DetectorFunction.HIGH_NON_ZERO_COUNT ); /** @@ -190,17 +189,21 @@ private static ObjectParser createParser(boolean ignoreUnknownFie * */ static final EnumSet FUNCTIONS_WITHOUT_RULE_CONDITION_SUPPORT = EnumSet.of( - DetectorFunction.LAT_LONG, DetectorFunction.METRIC, DetectorFunction.RARE, DetectorFunction.FREQ_RARE); + DetectorFunction.LAT_LONG, + DetectorFunction.METRIC, + DetectorFunction.RARE, + DetectorFunction.FREQ_RARE + ); /** * field names cannot contain any of these characters * ", \ */ - public static final Character[] PROHIBITED_FIELDNAME_CHARACTERS = {'"', '\\'}; - public static final String PROHIBITED = String.join(",", - Arrays.stream(PROHIBITED_FIELDNAME_CHARACTERS).map( - c -> Character.toString(c)).collect(Collectors.toList())); - + public static final Character[] PROHIBITED_FIELDNAME_CHARACTERS = { '"', '\\' }; + public static final String PROHIBITED = String.join( + ",", + Arrays.stream(PROHIBITED_FIELDNAME_CHARACTERS).map(c -> Character.toString(c)).collect(Collectors.toList()) + ); private final String detectorDescription; private final DetectorFunction function; @@ -273,17 +276,26 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws } // negative means "unknown", which should only happen for a 5.4 job if (detectorIndex >= 0 - // no point writing this to cluster state, as the indexes will get reassigned on reload anyway - && params.paramAsBoolean(ToXContentParams.FOR_INTERNAL_STORAGE, false) == false) { + // no point writing this to cluster state, as the indexes will get reassigned on reload anyway + && params.paramAsBoolean(ToXContentParams.FOR_INTERNAL_STORAGE, false) == false) { builder.field(DETECTOR_INDEX.getPreferredName(), detectorIndex); } builder.endObject(); return builder; } - private Detector(String detectorDescription, DetectorFunction function, String fieldName, String byFieldName, String overFieldName, - String partitionFieldName, boolean useNull, ExcludeFrequent excludeFrequent, List rules, - int detectorIndex) { + private Detector( + String detectorDescription, + DetectorFunction function, + String fieldName, + String byFieldName, + String overFieldName, + String partitionFieldName, + boolean useNull, + ExcludeFrequent excludeFrequent, + List rules, + int detectorIndex + ) { this.function = function; this.fieldName = fieldName; this.byFieldName = byFieldName; @@ -383,15 +395,14 @@ public int getDetectorIndex() { * @return a list with the byFieldName, overFieldName and partitionFieldName that are not null */ public List extractAnalysisFields() { - List analysisFields = Arrays.asList(getByFieldName(), - getOverFieldName(), getPartitionFieldName()); + List analysisFields = Arrays.asList(getByFieldName(), getOverFieldName(), getPartitionFieldName()); return analysisFields.stream().filter(item -> item != null).collect(Collectors.toList()); } public Set extractReferencedFilters() { - return rules == null ? Collections.emptySet() - : rules.stream().map(DetectionRule::extractReferencedFilters) - .flatMap(Set::stream).collect(Collectors.toSet()); + return rules == null + ? Collections.emptySet() + : rules.stream().map(DetectionRule::extractReferencedFilters).flatMap(Set::stream).collect(Collectors.toSet()); } /** @@ -423,22 +434,32 @@ public boolean equals(Object other) { Detector that = (Detector) other; - return Objects.equals(this.detectorDescription, that.detectorDescription) && - Objects.equals(this.function, that.function) && - Objects.equals(this.fieldName, that.fieldName) && - Objects.equals(this.byFieldName, that.byFieldName) && - Objects.equals(this.overFieldName, that.overFieldName) && - Objects.equals(this.partitionFieldName, that.partitionFieldName) && - Objects.equals(this.useNull, that.useNull) && - Objects.equals(this.excludeFrequent, that.excludeFrequent) && - Objects.equals(this.rules, that.rules) && - this.detectorIndex == that.detectorIndex; + return Objects.equals(this.detectorDescription, that.detectorDescription) + && Objects.equals(this.function, that.function) + && Objects.equals(this.fieldName, that.fieldName) + && Objects.equals(this.byFieldName, that.byFieldName) + && Objects.equals(this.overFieldName, that.overFieldName) + && Objects.equals(this.partitionFieldName, that.partitionFieldName) + && Objects.equals(this.useNull, that.useNull) + && Objects.equals(this.excludeFrequent, that.excludeFrequent) + && Objects.equals(this.rules, that.rules) + && this.detectorIndex == that.detectorIndex; } @Override public int hashCode() { - return Objects.hash(detectorDescription, function, fieldName, byFieldName, overFieldName, partitionFieldName, useNull, - excludeFrequent, rules, detectorIndex); + return Objects.hash( + detectorDescription, + function, + fieldName, + byFieldName, + overFieldName, + partitionFieldName, + useNull, + excludeFrequent, + rules, + detectorIndex + ); } public static class Builder { @@ -455,8 +476,7 @@ public static class Builder { // negative means unknown, and is expected for v5.4 jobs private int detectorIndex = -1; - public Builder() { - } + public Builder() {} public Builder(Detector detector) { detectorDescription = detector.detectorDescription; @@ -550,7 +570,8 @@ public Detector build() { if (emptyField == false && (Detector.FIELD_NAME_FUNCTIONS.contains(function) == false)) { throw ExceptionsHelper.badRequestException( - Messages.getMessage(Messages.JOB_CONFIG_FIELDNAME_INCOMPATIBLE_FUNCTION, function)); + Messages.getMessage(Messages.JOB_CONFIG_FIELDNAME_INCOMPATIBLE_FUNCTION, function) + ); } if (emptyByField && Detector.BY_FIELD_NAME_FUNCTIONS.contains(function)) { @@ -563,7 +584,8 @@ public Detector build() { if (emptyOverField == false && Detector.NO_OVER_FIELD_NAME_FUNCTIONS.contains(function)) { throw ExceptionsHelper.badRequestException( - Messages.getMessage(Messages.JOB_CONFIG_OVERFIELD_INCOMPATIBLE_FUNCTION, function)); + Messages.getMessage(Messages.JOB_CONFIG_OVERFIELD_INCOMPATIBLE_FUNCTION, function) + ); } // field names cannot contain certain characters @@ -580,54 +602,85 @@ public Detector build() { // partition, by and over field names cannot be duplicates if (emptyPartitionField == false) { if (partitionFieldName.equals(byFieldName)) { - throw ExceptionsHelper.badRequestException(Messages.getMessage(Messages.JOB_CONFIG_DETECTOR_DUPLICATE_FIELD_NAME, - PARTITION_FIELD_NAME_FIELD.getPreferredName(), BY_FIELD_NAME_FIELD.getPreferredName(), - partitionFieldName)); + throw ExceptionsHelper.badRequestException( + Messages.getMessage( + Messages.JOB_CONFIG_DETECTOR_DUPLICATE_FIELD_NAME, + PARTITION_FIELD_NAME_FIELD.getPreferredName(), + BY_FIELD_NAME_FIELD.getPreferredName(), + partitionFieldName + ) + ); } if (partitionFieldName.equals(overFieldName)) { - throw ExceptionsHelper.badRequestException(Messages.getMessage(Messages.JOB_CONFIG_DETECTOR_DUPLICATE_FIELD_NAME, - PARTITION_FIELD_NAME_FIELD.getPreferredName(), OVER_FIELD_NAME_FIELD.getPreferredName(), - partitionFieldName)); + throw ExceptionsHelper.badRequestException( + Messages.getMessage( + Messages.JOB_CONFIG_DETECTOR_DUPLICATE_FIELD_NAME, + PARTITION_FIELD_NAME_FIELD.getPreferredName(), + OVER_FIELD_NAME_FIELD.getPreferredName(), + partitionFieldName + ) + ); } } if (emptyByField == false && byFieldName.equals(overFieldName)) { - throw ExceptionsHelper.badRequestException(Messages.getMessage(Messages.JOB_CONFIG_DETECTOR_DUPLICATE_FIELD_NAME, - BY_FIELD_NAME_FIELD.getPreferredName(), OVER_FIELD_NAME_FIELD.getPreferredName(), - byFieldName)); + throw ExceptionsHelper.badRequestException( + Messages.getMessage( + Messages.JOB_CONFIG_DETECTOR_DUPLICATE_FIELD_NAME, + BY_FIELD_NAME_FIELD.getPreferredName(), + OVER_FIELD_NAME_FIELD.getPreferredName(), + byFieldName + ) + ); } // by/over field names cannot be "count", "over', "by" - this requirement dates back to the early // days of the ML code and could be removed now BUT ONLY IF THE C++ CODE IS CHANGED // FIRST - see https://github.com/elastic/x-pack-elasticsearch/issues/858 if (DetectorFunction.COUNT.getFullName().equals(byFieldName)) { - throw ExceptionsHelper.badRequestException(Messages.getMessage(Messages.JOB_CONFIG_DETECTOR_COUNT_DISALLOWED, - BY_FIELD_NAME_FIELD.getPreferredName())); + throw ExceptionsHelper.badRequestException( + Messages.getMessage(Messages.JOB_CONFIG_DETECTOR_COUNT_DISALLOWED, BY_FIELD_NAME_FIELD.getPreferredName()) + ); } if (DetectorFunction.COUNT.getFullName().equals(overFieldName)) { - throw ExceptionsHelper.badRequestException(Messages.getMessage(Messages.JOB_CONFIG_DETECTOR_COUNT_DISALLOWED, - OVER_FIELD_NAME_FIELD.getPreferredName())); + throw ExceptionsHelper.badRequestException( + Messages.getMessage(Messages.JOB_CONFIG_DETECTOR_COUNT_DISALLOWED, OVER_FIELD_NAME_FIELD.getPreferredName()) + ); } if (BY.equals(byFieldName)) { - throw ExceptionsHelper.badRequestException(Messages.getMessage(Messages.JOB_CONFIG_DETECTOR_BY_DISALLOWED, - BY_FIELD_NAME_FIELD.getPreferredName())); + throw ExceptionsHelper.badRequestException( + Messages.getMessage(Messages.JOB_CONFIG_DETECTOR_BY_DISALLOWED, BY_FIELD_NAME_FIELD.getPreferredName()) + ); } if (BY.equals(overFieldName)) { - throw ExceptionsHelper.badRequestException(Messages.getMessage(Messages.JOB_CONFIG_DETECTOR_BY_DISALLOWED, - OVER_FIELD_NAME_FIELD.getPreferredName())); + throw ExceptionsHelper.badRequestException( + Messages.getMessage(Messages.JOB_CONFIG_DETECTOR_BY_DISALLOWED, OVER_FIELD_NAME_FIELD.getPreferredName()) + ); } if (OVER.equals(byFieldName)) { - throw ExceptionsHelper.badRequestException(Messages.getMessage(Messages.JOB_CONFIG_DETECTOR_OVER_DISALLOWED, - BY_FIELD_NAME_FIELD.getPreferredName())); + throw ExceptionsHelper.badRequestException( + Messages.getMessage(Messages.JOB_CONFIG_DETECTOR_OVER_DISALLOWED, BY_FIELD_NAME_FIELD.getPreferredName()) + ); } if (OVER.equals(overFieldName)) { - throw ExceptionsHelper.badRequestException(Messages.getMessage(Messages.JOB_CONFIG_DETECTOR_OVER_DISALLOWED, - OVER_FIELD_NAME_FIELD.getPreferredName())); + throw ExceptionsHelper.badRequestException( + Messages.getMessage(Messages.JOB_CONFIG_DETECTOR_OVER_DISALLOWED, OVER_FIELD_NAME_FIELD.getPreferredName()) + ); } - return new Detector(detectorDescription, function, fieldName, byFieldName, overFieldName, partitionFieldName, - useNull, excludeFrequent, rules, detectorIndex); + return new Detector( + detectorDescription, + function, + fieldName, + byFieldName, + overFieldName, + partitionFieldName, + useNull, + excludeFrequent, + rules, + detectorIndex + ); } public List extractAnalysisFields() { @@ -643,11 +696,13 @@ public List extractAnalysisFields() { public static void verifyFieldName(String field) throws ElasticsearchParseException { if (field != null && containsInvalidChar(field)) { throw ExceptionsHelper.badRequestException( - Messages.getMessage(Messages.JOB_CONFIG_INVALID_FIELDNAME_CHARS, field, Detector.PROHIBITED)); + Messages.getMessage(Messages.JOB_CONFIG_INVALID_FIELDNAME_CHARS, field, Detector.PROHIBITED) + ); } if (RecordWriter.CONTROL_FIELD_NAME.equals(field)) { throw ExceptionsHelper.badRequestException( - Messages.getMessage(Messages.JOB_CONFIG_INVALID_FIELDNAME, field, RecordWriter.CONTROL_FIELD_NAME)); + Messages.getMessage(Messages.JOB_CONFIG_INVALID_FIELDNAME, field, RecordWriter.CONTROL_FIELD_NAME) + ); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/FilterRef.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/FilterRef.java index 1443e4b6f7db2..0272f3b042095 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/FilterRef.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/FilterRef.java @@ -6,11 +6,11 @@ */ package org.elasticsearch.xpack.core.ml.job.config; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -25,7 +25,8 @@ public class FilterRef implements ToXContentObject, Writeable { public static final ParseField FILTER_TYPE = new ParseField("filter_type"); public enum FilterType { - INCLUDE, EXCLUDE; + INCLUDE, + EXCLUDE; public static FilterType fromString(String value) { return valueOf(value.toUpperCase(Locale.ROOT)); @@ -42,8 +43,11 @@ public String toString() { public static final ConstructingObjectParser STRICT_PARSER = createParser(false); private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { - ConstructingObjectParser parser = new ConstructingObjectParser<>(FILTER_REF_FIELD.getPreferredName(), - ignoreUnknownFields, a -> new FilterRef((String) a[0], (FilterType) a[1])); + ConstructingObjectParser parser = new ConstructingObjectParser<>( + FILTER_REF_FIELD.getPreferredName(), + ignoreUnknownFields, + a -> new FilterRef((String) a[0], (FilterType) a[1]) + ); parser.declareString(ConstructingObjectParser.constructorArg(), FILTER_ID); parser.declareString(ConstructingObjectParser.optionalConstructorArg(), FilterType::fromString, FILTER_TYPE); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/Job.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/Job.java index 1d06f55b45189..289c887c3b6a9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/Job.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/Job.java @@ -10,25 +10,25 @@ import org.elasticsearch.Version; import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.cluster.AbstractDiffable; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ObjectParser; import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xpack.core.common.time.TimeUtils; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfig; import org.elasticsearch.xpack.core.ml.job.messages.Messages; import org.elasticsearch.xpack.core.ml.job.persistence.AnomalyDetectorsIndexFields; import org.elasticsearch.xpack.core.ml.job.process.autodetect.state.DataCounts; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; import org.elasticsearch.xpack.core.ml.utils.MlStrings; -import org.elasticsearch.xpack.core.common.time.TimeUtils; import org.elasticsearch.xpack.core.ml.utils.ToXContentParams; import java.io.IOException; @@ -80,8 +80,7 @@ public class Job extends AbstractDiffable implements Writeable, ToXContentO public static final ParseField RENORMALIZATION_WINDOW_DAYS = new ParseField("renormalization_window_days"); public static final ParseField BACKGROUND_PERSIST_INTERVAL = new ParseField("background_persist_interval"); public static final ParseField MODEL_SNAPSHOT_RETENTION_DAYS = new ParseField("model_snapshot_retention_days"); - public static final ParseField DAILY_MODEL_SNAPSHOT_RETENTION_AFTER_DAYS = - new ParseField("daily_model_snapshot_retention_after_days"); + public static final ParseField DAILY_MODEL_SNAPSHOT_RETENTION_AFTER_DAYS = new ParseField("daily_model_snapshot_retention_after_days"); public static final ParseField RESULTS_RETENTION_DAYS = new ParseField("results_retention_days"); public static final ParseField MODEL_SNAPSHOT_ID = new ParseField("model_snapshot_id"); public static final ParseField MODEL_SNAPSHOT_MIN_VERSION = new ParseField("model_snapshot_min_version"); @@ -119,21 +118,45 @@ private static ObjectParser createParser(boolean ignoreUnknownFie parser.declareString(Builder::setJobVersion, JOB_VERSION); parser.declareStringArray(Builder::setGroups, GROUPS); parser.declareStringOrNull(Builder::setDescription, DESCRIPTION); - parser.declareField(Builder::setCreateTime, - p -> TimeUtils.parseTimeField(p, CREATE_TIME.getPreferredName()), CREATE_TIME, ValueType.VALUE); - parser.declareField(Builder::setFinishedTime, - p -> TimeUtils.parseTimeField(p, FINISHED_TIME.getPreferredName()), FINISHED_TIME, ValueType.VALUE); - parser.declareObject(Builder::setAnalysisConfig, ignoreUnknownFields ? AnalysisConfig.LENIENT_PARSER : AnalysisConfig.STRICT_PARSER, - ANALYSIS_CONFIG); - parser.declareObject(Builder::setAnalysisLimits, ignoreUnknownFields ? AnalysisLimits.LENIENT_PARSER : AnalysisLimits.STRICT_PARSER, - ANALYSIS_LIMITS); - parser.declareObject(Builder::setDataDescription, - ignoreUnknownFields ? DataDescription.LENIENT_PARSER : DataDescription.STRICT_PARSER, DATA_DESCRIPTION); - parser.declareObject(Builder::setModelPlotConfig, - ignoreUnknownFields ? ModelPlotConfig.LENIENT_PARSER : ModelPlotConfig.STRICT_PARSER, MODEL_PLOT_CONFIG); + parser.declareField( + Builder::setCreateTime, + p -> TimeUtils.parseTimeField(p, CREATE_TIME.getPreferredName()), + CREATE_TIME, + ValueType.VALUE + ); + parser.declareField( + Builder::setFinishedTime, + p -> TimeUtils.parseTimeField(p, FINISHED_TIME.getPreferredName()), + FINISHED_TIME, + ValueType.VALUE + ); + parser.declareObject( + Builder::setAnalysisConfig, + ignoreUnknownFields ? AnalysisConfig.LENIENT_PARSER : AnalysisConfig.STRICT_PARSER, + ANALYSIS_CONFIG + ); + parser.declareObject( + Builder::setAnalysisLimits, + ignoreUnknownFields ? AnalysisLimits.LENIENT_PARSER : AnalysisLimits.STRICT_PARSER, + ANALYSIS_LIMITS + ); + parser.declareObject( + Builder::setDataDescription, + ignoreUnknownFields ? DataDescription.LENIENT_PARSER : DataDescription.STRICT_PARSER, + DATA_DESCRIPTION + ); + parser.declareObject( + Builder::setModelPlotConfig, + ignoreUnknownFields ? ModelPlotConfig.LENIENT_PARSER : ModelPlotConfig.STRICT_PARSER, + MODEL_PLOT_CONFIG + ); parser.declareLong(Builder::setRenormalizationWindowDays, RENORMALIZATION_WINDOW_DAYS); - parser.declareString((builder, val) -> builder.setBackgroundPersistInterval( - TimeValue.parseTimeValue(val, BACKGROUND_PERSIST_INTERVAL.getPreferredName())), BACKGROUND_PERSIST_INTERVAL); + parser.declareString( + (builder, val) -> builder.setBackgroundPersistInterval( + TimeValue.parseTimeValue(val, BACKGROUND_PERSIST_INTERVAL.getPreferredName()) + ), + BACKGROUND_PERSIST_INTERVAL + ); parser.declareLong(Builder::setResultsRetentionDays, RESULTS_RETENTION_DAYS); parser.declareLong(Builder::setModelSnapshotRetentionDays, MODEL_SNAPSHOT_RETENTION_DAYS); parser.declareLong(Builder::setDailyModelSnapshotRetentionAfterDays, DAILY_MODEL_SNAPSHOT_RETENTION_AFTER_DAYS); @@ -144,9 +167,11 @@ private static ObjectParser createParser(boolean ignoreUnknownFie parser.declareBoolean(Builder::setDeleting, DELETING); parser.declareBoolean(Builder::setAllowLazyOpen, ALLOW_LAZY_OPEN); parser.declareObject(Builder::setBlocked, ignoreUnknownFields ? Blocked.LENIENT_PARSER : Blocked.STRICT_PARSER, BLOCKED); - parser.declareObject(Builder::setDatafeed, + parser.declareObject( + Builder::setDatafeed, ignoreUnknownFields ? DatafeedConfig.LENIENT_PARSER : DatafeedConfig.STRICT_PARSER, - DATAFEED_CONFIG); + DATAFEED_CONFIG + ); return parser; } @@ -183,14 +208,32 @@ private static ObjectParser createParser(boolean ignoreUnknownFie private final Blocked blocked; private final DatafeedConfig datafeedConfig; - private Job(String jobId, String jobType, Version jobVersion, List groups, String description, - Date createTime, Date finishedTime, - AnalysisConfig analysisConfig, AnalysisLimits analysisLimits, DataDescription dataDescription, - ModelPlotConfig modelPlotConfig, Long renormalizationWindowDays, TimeValue backgroundPersistInterval, - Long modelSnapshotRetentionDays, Long dailyModelSnapshotRetentionAfterDays, Long resultsRetentionDays, - Map customSettings, String modelSnapshotId, - Version modelSnapshotMinVersion, String resultsIndexName, boolean deleting, boolean allowLazyOpen, Blocked blocked, - DatafeedConfig datafeedConfig) { + private Job( + String jobId, + String jobType, + Version jobVersion, + List groups, + String description, + Date createTime, + Date finishedTime, + AnalysisConfig analysisConfig, + AnalysisLimits analysisLimits, + DataDescription dataDescription, + ModelPlotConfig modelPlotConfig, + Long renormalizationWindowDays, + TimeValue backgroundPersistInterval, + Long modelSnapshotRetentionDays, + Long dailyModelSnapshotRetentionAfterDays, + Long resultsRetentionDays, + Map customSettings, + String modelSnapshotId, + Version modelSnapshotMinVersion, + String resultsIndexName, + boolean deleting, + boolean allowLazyOpen, + Blocked blocked, + DatafeedConfig datafeedConfig + ) { this.jobId = jobId; this.jobType = jobType; this.jobVersion = jobVersion; @@ -282,7 +325,7 @@ public static String documentId(String jobId) { */ @Nullable public static String extractJobIdFromDocumentId(String docId) { - String jobId = docId.replaceAll("^" + ANOMALY_DETECTOR_JOB_TYPE +"-", ""); + String jobId = docId.replaceAll("^" + ANOMALY_DETECTOR_JOB_TYPE + "-", ""); return jobId.equals(docId) ? null : jobId; } @@ -559,8 +602,11 @@ public XContentBuilder doXContentBody(XContentBuilder builder, Params params) th } builder.timeField(CREATE_TIME.getPreferredName(), CREATE_TIME.getPreferredName() + humanReadableSuffix, createTime.getTime()); if (finishedTime != null) { - builder.timeField(FINISHED_TIME.getPreferredName(), FINISHED_TIME.getPreferredName() + humanReadableSuffix, - finishedTime.getTime()); + builder.timeField( + FINISHED_TIME.getPreferredName(), + FINISHED_TIME.getPreferredName() + humanReadableSuffix, + finishedTime.getTime() + ); } if (modelSnapshotId != null) { builder.field(MODEL_SNAPSHOT_ID.getPreferredName(), modelSnapshotId); @@ -574,7 +620,7 @@ public XContentBuilder doXContentBody(XContentBuilder builder, Params params) th if (customSettings != null) { builder.field(CUSTOM_SETTINGS.getPreferredName(), customSettings); } - //TODO in v8.0.0 move this out so that it will be included when `exclude_generated` is `true` + // TODO in v8.0.0 move this out so that it will be included when `exclude_generated` is `true` if (params.paramAsBoolean(ToXContentParams.FOR_INTERNAL_STORAGE, false) == false) { if (datafeedConfig != null) { builder.field(DATAFEED_CONFIG.getPreferredName(), datafeedConfig, params); @@ -638,38 +684,59 @@ public boolean equals(Object other) { Job that = (Job) other; return Objects.equals(this.jobId, that.jobId) - && Objects.equals(this.jobType, that.jobType) - && Objects.equals(this.jobVersion, that.jobVersion) - && Objects.equals(this.groups, that.groups) - && Objects.equals(this.description, that.description) - && Objects.equals(this.createTime, that.createTime) - && Objects.equals(this.finishedTime, that.finishedTime) - && Objects.equals(this.analysisConfig, that.analysisConfig) - && Objects.equals(this.analysisLimits, that.analysisLimits) - && Objects.equals(this.dataDescription, that.dataDescription) - && Objects.equals(this.modelPlotConfig, that.modelPlotConfig) - && Objects.equals(this.renormalizationWindowDays, that.renormalizationWindowDays) - && Objects.equals(this.backgroundPersistInterval, that.backgroundPersistInterval) - && Objects.equals(this.modelSnapshotRetentionDays, that.modelSnapshotRetentionDays) - && Objects.equals(this.dailyModelSnapshotRetentionAfterDays, that.dailyModelSnapshotRetentionAfterDays) - && Objects.equals(this.resultsRetentionDays, that.resultsRetentionDays) - && Objects.equals(this.customSettings, that.customSettings) - && Objects.equals(this.modelSnapshotId, that.modelSnapshotId) - && Objects.equals(this.modelSnapshotMinVersion, that.modelSnapshotMinVersion) - && Objects.equals(this.resultsIndexName, that.resultsIndexName) - && Objects.equals(this.deleting, that.deleting) - && Objects.equals(this.allowLazyOpen, that.allowLazyOpen) - && Objects.equals(this.blocked, that.blocked) - && Objects.equals(this.datafeedConfig, that.datafeedConfig); + && Objects.equals(this.jobType, that.jobType) + && Objects.equals(this.jobVersion, that.jobVersion) + && Objects.equals(this.groups, that.groups) + && Objects.equals(this.description, that.description) + && Objects.equals(this.createTime, that.createTime) + && Objects.equals(this.finishedTime, that.finishedTime) + && Objects.equals(this.analysisConfig, that.analysisConfig) + && Objects.equals(this.analysisLimits, that.analysisLimits) + && Objects.equals(this.dataDescription, that.dataDescription) + && Objects.equals(this.modelPlotConfig, that.modelPlotConfig) + && Objects.equals(this.renormalizationWindowDays, that.renormalizationWindowDays) + && Objects.equals(this.backgroundPersistInterval, that.backgroundPersistInterval) + && Objects.equals(this.modelSnapshotRetentionDays, that.modelSnapshotRetentionDays) + && Objects.equals(this.dailyModelSnapshotRetentionAfterDays, that.dailyModelSnapshotRetentionAfterDays) + && Objects.equals(this.resultsRetentionDays, that.resultsRetentionDays) + && Objects.equals(this.customSettings, that.customSettings) + && Objects.equals(this.modelSnapshotId, that.modelSnapshotId) + && Objects.equals(this.modelSnapshotMinVersion, that.modelSnapshotMinVersion) + && Objects.equals(this.resultsIndexName, that.resultsIndexName) + && Objects.equals(this.deleting, that.deleting) + && Objects.equals(this.allowLazyOpen, that.allowLazyOpen) + && Objects.equals(this.blocked, that.blocked) + && Objects.equals(this.datafeedConfig, that.datafeedConfig); } @Override public int hashCode() { - return Objects.hash(jobId, jobType, jobVersion, groups, description, createTime, finishedTime, - analysisConfig, analysisLimits, dataDescription, modelPlotConfig, renormalizationWindowDays, - backgroundPersistInterval, modelSnapshotRetentionDays, dailyModelSnapshotRetentionAfterDays, resultsRetentionDays, - customSettings, modelSnapshotId, modelSnapshotMinVersion, resultsIndexName, deleting, - allowLazyOpen, blocked, datafeedConfig); + return Objects.hash( + jobId, + jobType, + jobVersion, + groups, + description, + createTime, + finishedTime, + analysisConfig, + analysisLimits, + dataDescription, + modelPlotConfig, + renormalizationWindowDays, + backgroundPersistInterval, + modelSnapshotRetentionDays, + dailyModelSnapshotRetentionAfterDays, + resultsRetentionDays, + customSettings, + modelSnapshotId, + modelSnapshotMinVersion, + resultsIndexName, + deleting, + allowLazyOpen, + blocked, + datafeedConfig + ); } // Class already extends from AbstractDiffable, so copied from ToXContentToBytes#toString() @@ -722,8 +789,7 @@ public static class Builder implements Writeable { private Blocked blocked = Blocked.none(); private DatafeedConfig.Builder datafeedConfig; - public Builder() { - } + public Builder() {} public Builder(String id) { this.id = id; @@ -832,7 +898,7 @@ public Builder setAnalysisConfig(AnalysisConfig.Builder configBuilder) { } public AnalysisConfig getAnalysisConfig() { - return analysisConfig; + return analysisConfig; } public Builder setAnalysisLimits(AnalysisLimits analysisLimits) { @@ -1021,38 +1087,59 @@ public boolean equals(Object o) { Job.Builder that = (Job.Builder) o; return Objects.equals(this.id, that.id) - && Objects.equals(this.jobType, that.jobType) - && Objects.equals(this.jobVersion, that.jobVersion) - && Objects.equals(this.groups, that.groups) - && Objects.equals(this.description, that.description) - && Objects.equals(this.analysisConfig, that.analysisConfig) - && Objects.equals(this.analysisLimits, that.analysisLimits) - && Objects.equals(this.dataDescription, that.dataDescription) - && Objects.equals(this.createTime, that.createTime) - && Objects.equals(this.finishedTime, that.finishedTime) - && Objects.equals(this.modelPlotConfig, that.modelPlotConfig) - && Objects.equals(this.renormalizationWindowDays, that.renormalizationWindowDays) - && Objects.equals(this.backgroundPersistInterval, that.backgroundPersistInterval) - && Objects.equals(this.modelSnapshotRetentionDays, that.modelSnapshotRetentionDays) - && Objects.equals(this.dailyModelSnapshotRetentionAfterDays, that.dailyModelSnapshotRetentionAfterDays) - && Objects.equals(this.resultsRetentionDays, that.resultsRetentionDays) - && Objects.equals(this.customSettings, that.customSettings) - && Objects.equals(this.modelSnapshotId, that.modelSnapshotId) - && Objects.equals(this.modelSnapshotMinVersion, that.modelSnapshotMinVersion) - && Objects.equals(this.resultsIndexName, that.resultsIndexName) - && Objects.equals(this.deleting, that.deleting) - && Objects.equals(this.allowLazyOpen, that.allowLazyOpen) - && Objects.equals(this.blocked, that.blocked) - && Objects.equals(this.datafeedConfig, that.datafeedConfig); + && Objects.equals(this.jobType, that.jobType) + && Objects.equals(this.jobVersion, that.jobVersion) + && Objects.equals(this.groups, that.groups) + && Objects.equals(this.description, that.description) + && Objects.equals(this.analysisConfig, that.analysisConfig) + && Objects.equals(this.analysisLimits, that.analysisLimits) + && Objects.equals(this.dataDescription, that.dataDescription) + && Objects.equals(this.createTime, that.createTime) + && Objects.equals(this.finishedTime, that.finishedTime) + && Objects.equals(this.modelPlotConfig, that.modelPlotConfig) + && Objects.equals(this.renormalizationWindowDays, that.renormalizationWindowDays) + && Objects.equals(this.backgroundPersistInterval, that.backgroundPersistInterval) + && Objects.equals(this.modelSnapshotRetentionDays, that.modelSnapshotRetentionDays) + && Objects.equals(this.dailyModelSnapshotRetentionAfterDays, that.dailyModelSnapshotRetentionAfterDays) + && Objects.equals(this.resultsRetentionDays, that.resultsRetentionDays) + && Objects.equals(this.customSettings, that.customSettings) + && Objects.equals(this.modelSnapshotId, that.modelSnapshotId) + && Objects.equals(this.modelSnapshotMinVersion, that.modelSnapshotMinVersion) + && Objects.equals(this.resultsIndexName, that.resultsIndexName) + && Objects.equals(this.deleting, that.deleting) + && Objects.equals(this.allowLazyOpen, that.allowLazyOpen) + && Objects.equals(this.blocked, that.blocked) + && Objects.equals(this.datafeedConfig, that.datafeedConfig); } @Override public int hashCode() { - return Objects.hash(id, jobType, jobVersion, groups, description, analysisConfig, analysisLimits, dataDescription, - createTime, finishedTime, modelPlotConfig, renormalizationWindowDays, - backgroundPersistInterval, modelSnapshotRetentionDays, dailyModelSnapshotRetentionAfterDays, resultsRetentionDays, - customSettings, modelSnapshotId, modelSnapshotMinVersion, resultsIndexName, deleting, - allowLazyOpen, blocked, datafeedConfig); + return Objects.hash( + id, + jobType, + jobVersion, + groups, + description, + analysisConfig, + analysisLimits, + dataDescription, + createTime, + finishedTime, + modelPlotConfig, + renormalizationWindowDays, + backgroundPersistInterval, + modelSnapshotRetentionDays, + dailyModelSnapshotRetentionAfterDays, + resultsRetentionDays, + customSettings, + modelSnapshotId, + modelSnapshotMinVersion, + resultsIndexName, + deleting, + allowLazyOpen, + blocked, + datafeedConfig + ); } /** @@ -1089,7 +1176,8 @@ public void validateInputFields() { // Results index name not specified in user input means use the default, so is acceptable in this validation if (Strings.isNullOrEmpty(resultsIndexName) == false && MlStrings.isValidId(resultsIndexName) == false) { throw new IllegalArgumentException( - Messages.getMessage(Messages.INVALID_ID, RESULTS_INDEX_NAME.getPreferredName(), resultsIndexName)); + Messages.getMessage(Messages.INVALID_ID, RESULTS_INDEX_NAME.getPreferredName(), resultsIndexName) + ); } // Creation time is NOT required in user input, hence validated only on build @@ -1101,8 +1189,11 @@ public void validateInputFields() { * and it will set the current version defaults to missing values. */ public void validateAnalysisLimitsAndSetDefaults(@Nullable ByteSizeValue maxModelMemoryLimit) { - analysisLimits = AnalysisLimits.validateAndSetDefaults(analysisLimits, maxModelMemoryLimit, - AnalysisLimits.DEFAULT_MODEL_MEMORY_LIMIT_MB); + analysisLimits = AnalysisLimits.validateAndSetDefaults( + analysisLimits, + maxModelMemoryLimit, + AnalysisLimits.DEFAULT_MODEL_MEMORY_LIMIT_MB + ); } /** @@ -1111,9 +1202,9 @@ public void validateAnalysisLimitsAndSetDefaults(@Nullable ByteSizeValue maxMode */ public void validateModelSnapshotRetentionSettingsAndSetDefaults() { validateModelSnapshotRetentionSettings(); - if (dailyModelSnapshotRetentionAfterDays == null && - modelSnapshotRetentionDays != null && - modelSnapshotRetentionDays > DEFAULT_DAILY_MODEL_SNAPSHOT_RETENTION_AFTER_DAYS) { + if (dailyModelSnapshotRetentionAfterDays == null + && modelSnapshotRetentionDays != null + && modelSnapshotRetentionDays > DEFAULT_DAILY_MODEL_SNAPSHOT_RETENTION_AFTER_DAYS) { dailyModelSnapshotRetentionAfterDays = DEFAULT_DAILY_MODEL_SNAPSHOT_RETENTION_AFTER_DAYS; } } @@ -1125,14 +1216,18 @@ public void validateModelSnapshotRetentionSettingsAndSetDefaults() { public void validateModelSnapshotRetentionSettings() { checkValueNotLessThan(0, MODEL_SNAPSHOT_RETENTION_DAYS.getPreferredName(), modelSnapshotRetentionDays); - checkValueNotLessThan(0, DAILY_MODEL_SNAPSHOT_RETENTION_AFTER_DAYS.getPreferredName(), - dailyModelSnapshotRetentionAfterDays); - - if (modelSnapshotRetentionDays != null && - dailyModelSnapshotRetentionAfterDays != null && - dailyModelSnapshotRetentionAfterDays > modelSnapshotRetentionDays) { - throw new IllegalArgumentException(Messages.getMessage(Messages.JOB_CONFIG_MODEL_SNAPSHOT_RETENTION_SETTINGS_INCONSISTENT, - dailyModelSnapshotRetentionAfterDays, modelSnapshotRetentionDays)); + checkValueNotLessThan(0, DAILY_MODEL_SNAPSHOT_RETENTION_AFTER_DAYS.getPreferredName(), dailyModelSnapshotRetentionAfterDays); + + if (modelSnapshotRetentionDays != null + && dailyModelSnapshotRetentionAfterDays != null + && dailyModelSnapshotRetentionAfterDays > modelSnapshotRetentionDays) { + throw new IllegalArgumentException( + Messages.getMessage( + Messages.JOB_CONFIG_MODEL_SNAPSHOT_RETENTION_SETTINGS_INCONSISTENT, + dailyModelSnapshotRetentionAfterDays, + modelSnapshotRetentionDays + ) + ); } } @@ -1158,7 +1253,8 @@ public void validateDetectorsAreUnique() { Detector canonicalDetector = new Detector.Builder(detector).setDetectorIndex(0).build(); if (canonicalDetectors.add(canonicalDetector) == false) { throw new IllegalArgumentException( - Messages.getMessage(Messages.JOB_CONFIG_DUPLICATE_DETECTORS_DISALLOWED, detector.getDetectorDescription())); + Messages.getMessage(Messages.JOB_CONFIG_DUPLICATE_DETECTORS_DISALLOWED, detector.getDetectorDescription()) + ); } } } @@ -1189,8 +1285,11 @@ public Job build() { // it means we are reading a pre 6.3 job. Since 6.1, the model_memory_limit // is always populated. So, if the value is missing, we fill with the pre 6.1 // default. We do not need to check against the max limit here so we pass null. - analysisLimits = AnalysisLimits.validateAndSetDefaults(analysisLimits, null, - AnalysisLimits.PRE_6_1_DEFAULT_MODEL_MEMORY_LIMIT_MB); + analysisLimits = AnalysisLimits.validateAndSetDefaults( + analysisLimits, + null, + AnalysisLimits.PRE_6_1_DEFAULT_MODEL_MEMORY_LIMIT_MB + ); validateInputFields(); @@ -1202,9 +1301,7 @@ public Job build() { } else if (resultsIndexName.equals(AnomalyDetectorsIndexFields.RESULTS_INDEX_DEFAULT) == false) { // User-defined names are prepended with "custom" // Conditional guards against multiple prepending due to updates instead of first creation - resultsIndexName = resultsIndexName.startsWith("custom-") - ? resultsIndexName - : "custom-" + resultsIndexName; + resultsIndexName = resultsIndexName.startsWith("custom-") ? resultsIndexName : "custom-" + resultsIndexName; } if (datafeedConfig != null) { if (datafeedConfig.getId() == null) { @@ -1219,18 +1316,41 @@ public Job build() { } return new Job( - id, jobType, jobVersion, groups, description, createTime, finishedTime, - analysisConfig, analysisLimits, dataDescription, modelPlotConfig, renormalizationWindowDays, - backgroundPersistInterval, modelSnapshotRetentionDays, dailyModelSnapshotRetentionAfterDays, resultsRetentionDays, - customSettings, modelSnapshotId, modelSnapshotMinVersion, resultsIndexName, deleting, - allowLazyOpen, blocked, Optional.ofNullable(datafeedConfig).map(DatafeedConfig.Builder::build).orElse(null)); + id, + jobType, + jobVersion, + groups, + description, + createTime, + finishedTime, + analysisConfig, + analysisLimits, + dataDescription, + modelPlotConfig, + renormalizationWindowDays, + backgroundPersistInterval, + modelSnapshotRetentionDays, + dailyModelSnapshotRetentionAfterDays, + resultsRetentionDays, + customSettings, + modelSnapshotId, + modelSnapshotMinVersion, + resultsIndexName, + deleting, + allowLazyOpen, + blocked, + Optional.ofNullable(datafeedConfig).map(DatafeedConfig.Builder::build).orElse(null) + ); } private void checkValidBackgroundPersistInterval() { if (backgroundPersistInterval != null) { TimeUtils.checkMultiple(backgroundPersistInterval, TimeUnit.SECONDS, BACKGROUND_PERSIST_INTERVAL); - checkValueNotLessThan(MIN_BACKGROUND_PERSIST_INTERVAL.getSeconds(), BACKGROUND_PERSIST_INTERVAL.getPreferredName(), - backgroundPersistInterval.getSeconds()); + checkValueNotLessThan( + MIN_BACKGROUND_PERSIST_INTERVAL.getSeconds(), + BACKGROUND_PERSIST_INTERVAL.getPreferredName(), + backgroundPersistInterval.getSeconds() + ); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/JobState.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/JobState.java index 3a20f9c44b660..9f1bbdb542c5d 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/JobState.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/JobState.java @@ -22,7 +22,11 @@ */ public enum JobState implements Writeable, MemoryTrackedTaskState { - CLOSING, CLOSED, OPENED, FAILED, OPENING; + CLOSING, + CLOSED, + OPENED, + FAILED, + OPENING; public static JobState fromString(String name) { return valueOf(name.trim().toUpperCase(Locale.ROOT)); @@ -42,7 +46,6 @@ public String value() { return name().toLowerCase(Locale.ROOT); } - /** * @return {@code true} if state matches any of the given {@code candidates} */ diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/JobTaskState.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/JobTaskState.java index 5c5ab2e9cea9a..0c892c3f2f802 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/JobTaskState.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/JobTaskState.java @@ -8,13 +8,13 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.persistent.PersistentTaskState; +import org.elasticsearch.persistent.PersistentTasksCustomMetadata.PersistentTask; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.persistent.PersistentTaskState; -import org.elasticsearch.persistent.PersistentTasksCustomMetadata.PersistentTask; import org.elasticsearch.xpack.core.ml.MlTasks; import java.io.IOException; @@ -31,9 +31,11 @@ public class JobTaskState implements PersistentTaskState { private static ParseField ALLOCATION_ID = new ParseField("allocation_id"); private static ParseField REASON = new ParseField("reason"); - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(NAME, true, - args -> new JobTaskState((JobState) args[0], (Long) args[1], (String) args[2])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + true, + args -> new JobTaskState((JobState) args[0], (Long) args[1], (String) args[2]) + ); static { PARSER.declareString(constructorArg(), JobState::fromString, STATE); @@ -123,9 +125,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; JobTaskState that = (JobTaskState) o; - return state == that.state && - Objects.equals(allocationId, that.allocationId) && - Objects.equals(reason, that.reason); + return state == that.state && Objects.equals(allocationId, that.allocationId) && Objects.equals(reason, that.reason); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/JobUpdate.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/JobUpdate.java index f3ee728050869..263f3607b85e7 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/JobUpdate.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/JobUpdate.java @@ -7,15 +7,15 @@ package org.elasticsearch.xpack.core.ml.job.config; import org.elasticsearch.Version; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ml.job.process.autodetect.state.ModelSnapshot; @@ -35,11 +35,15 @@ public class JobUpdate implements Writeable, ToXContentObject { // For internal updates static final ConstructingObjectParser INTERNAL_PARSER = new ConstructingObjectParser<>( - "job_update", args -> new Builder((String) args[0])); + "job_update", + args -> new Builder((String) args[0]) + ); // For parsing REST requests public static final ConstructingObjectParser EXTERNAL_PARSER = new ConstructingObjectParser<>( - "job_update", args -> new Builder((String) args[0])); + "job_update", + args -> new Builder((String) args[0]) + ); static { for (ConstructingObjectParser parser : Arrays.asList(INTERNAL_PARSER, EXTERNAL_PARSER)) { @@ -49,19 +53,30 @@ public class JobUpdate implements Writeable, ToXContentObject { parser.declareObjectArray(Builder::setDetectorUpdates, DetectorUpdate.PARSER, DETECTORS); parser.declareObject(Builder::setModelPlotConfig, ModelPlotConfig.STRICT_PARSER, Job.MODEL_PLOT_CONFIG); parser.declareObject(Builder::setAnalysisLimits, AnalysisLimits.STRICT_PARSER, Job.ANALYSIS_LIMITS); - parser.declareString((builder, val) -> builder.setBackgroundPersistInterval( - TimeValue.parseTimeValue(val, Job.BACKGROUND_PERSIST_INTERVAL.getPreferredName())), Job.BACKGROUND_PERSIST_INTERVAL); + parser.declareString( + (builder, val) -> builder.setBackgroundPersistInterval( + TimeValue.parseTimeValue(val, Job.BACKGROUND_PERSIST_INTERVAL.getPreferredName()) + ), + Job.BACKGROUND_PERSIST_INTERVAL + ); parser.declareLong(Builder::setRenormalizationWindowDays, Job.RENORMALIZATION_WINDOW_DAYS); parser.declareLong(Builder::setResultsRetentionDays, Job.RESULTS_RETENTION_DAYS); parser.declareLong(Builder::setModelSnapshotRetentionDays, Job.MODEL_SNAPSHOT_RETENTION_DAYS); parser.declareLong(Builder::setDailyModelSnapshotRetentionAfterDays, Job.DAILY_MODEL_SNAPSHOT_RETENTION_AFTER_DAYS); parser.declareStringArray(Builder::setCategorizationFilters, AnalysisConfig.CATEGORIZATION_FILTERS); - parser.declareObject(Builder::setPerPartitionCategorizationConfig, PerPartitionCategorizationConfig.STRICT_PARSER, - AnalysisConfig.PER_PARTITION_CATEGORIZATION); + parser.declareObject( + Builder::setPerPartitionCategorizationConfig, + PerPartitionCategorizationConfig.STRICT_PARSER, + AnalysisConfig.PER_PARTITION_CATEGORIZATION + ); parser.declareField(Builder::setCustomSettings, (p, c) -> p.map(), Job.CUSTOM_SETTINGS, ObjectParser.ValueType.OBJECT); parser.declareBoolean(Builder::setAllowLazyOpen, Job.ALLOW_LAZY_OPEN); - parser.declareString((builder, val) -> builder.setModelPruneWindow( - TimeValue.parseTimeValue(val, AnalysisConfig.MODEL_PRUNE_WINDOW.getPreferredName())), AnalysisConfig.MODEL_PRUNE_WINDOW); + parser.declareString( + (builder, val) -> builder.setModelPruneWindow( + TimeValue.parseTimeValue(val, AnalysisConfig.MODEL_PRUNE_WINDOW.getPreferredName()) + ), + AnalysisConfig.MODEL_PRUNE_WINDOW + ); } // These fields should not be set by a REST request INTERNAL_PARSER.declareString(Builder::setModelSnapshotId, Job.MODEL_SNAPSHOT_ID); @@ -93,16 +108,29 @@ public class JobUpdate implements Writeable, ToXContentObject { private final Blocked blocked; private final TimeValue modelPruneWindow; - private JobUpdate(String jobId, @Nullable List groups, @Nullable String description, - @Nullable List detectorUpdates, @Nullable ModelPlotConfig modelPlotConfig, - @Nullable AnalysisLimits analysisLimits, @Nullable TimeValue backgroundPersistInterval, - @Nullable Long renormalizationWindowDays, @Nullable Long resultsRetentionDays, - @Nullable Long modelSnapshotRetentionDays, - @Nullable Long dailyModelSnapshotRetentionAfterDays, @Nullable List categorizationFilters, - @Nullable PerPartitionCategorizationConfig perPartitionCategorizationConfig, - @Nullable Map customSettings, @Nullable String modelSnapshotId, - @Nullable Version modelSnapshotMinVersion, @Nullable Version jobVersion, @Nullable Boolean clearJobFinishTime, - @Nullable Boolean allowLazyOpen, @Nullable Blocked blocked, @Nullable TimeValue modelPruneWindow) { + private JobUpdate( + String jobId, + @Nullable List groups, + @Nullable String description, + @Nullable List detectorUpdates, + @Nullable ModelPlotConfig modelPlotConfig, + @Nullable AnalysisLimits analysisLimits, + @Nullable TimeValue backgroundPersistInterval, + @Nullable Long renormalizationWindowDays, + @Nullable Long resultsRetentionDays, + @Nullable Long modelSnapshotRetentionDays, + @Nullable Long dailyModelSnapshotRetentionAfterDays, + @Nullable List categorizationFilters, + @Nullable PerPartitionCategorizationConfig perPartitionCategorizationConfig, + @Nullable Map customSettings, + @Nullable String modelSnapshotId, + @Nullable Version modelSnapshotMinVersion, + @Nullable Version jobVersion, + @Nullable Boolean clearJobFinishTime, + @Nullable Boolean allowLazyOpen, + @Nullable Blocked blocked, + @Nullable TimeValue modelPruneWindow + ) { this.jobId = jobId; this.groups = groups; this.description = description; @@ -454,8 +482,11 @@ public Job mergeWithJob(Job source, ByteSizeValue maxModelMemoryLimit) { int numDetectors = currentAnalysisConfig.getDetectors().size(); for (DetectorUpdate dd : detectorUpdates) { if (dd.getDetectorIndex() >= numDetectors) { - throw ExceptionsHelper.badRequestException("Supplied detector_index [{}] is >= the number of detectors [{}]", - dd.getDetectorIndex(), numDetectors); + throw ExceptionsHelper.badRequestException( + "Supplied detector_index [{}] is >= the number of detectors [{}]", + dd.getDetectorIndex(), + numDetectors + ); } Detector.Builder detectorBuilder = new Detector.Builder(currentAnalysisConfig.getDetectors().get(dd.getDetectorIndex())); @@ -473,8 +504,11 @@ public Job mergeWithJob(Job source, ByteSizeValue maxModelMemoryLimit) { builder.setModelPlotConfig(modelPlotConfig); } if (analysisLimits != null) { - AnalysisLimits validatedLimits = AnalysisLimits.validateAndSetDefaults(analysisLimits, maxModelMemoryLimit, - AnalysisLimits.DEFAULT_MODEL_MEMORY_LIMIT_MB); + AnalysisLimits validatedLimits = AnalysisLimits.validateAndSetDefaults( + analysisLimits, + maxModelMemoryLimit, + AnalysisLimits.DEFAULT_MODEL_MEMORY_LIMIT_MB + ); builder.setAnalysisLimits(validatedLimits); } if (renormalizationWindowDays != null) { @@ -497,8 +531,7 @@ public Job mergeWithJob(Job source, ByteSizeValue maxModelMemoryLimit) { } if (perPartitionCategorizationConfig != null) { // Whether per-partition categorization is enabled cannot be changed, only the lower level details - if (perPartitionCategorizationConfig.isEnabled() != - currentAnalysisConfig.getPerPartitionCategorizationConfig().isEnabled()) { + if (perPartitionCategorizationConfig.isEnabled() != currentAnalysisConfig.getPerPartitionCategorizationConfig().isEnabled()) { throw ExceptionsHelper.badRequestException("analysis_config.per_partition_categorization.enabled cannot be updated"); } newAnalysisConfig.setPerPartitionCategorizationConfig(perPartitionCategorizationConfig); @@ -534,28 +567,27 @@ public Job mergeWithJob(Job source, ByteSizeValue maxModelMemoryLimit) { boolean isNoop(Job job) { return (groups == null || Objects.equals(groups, job.getGroups())) - && (description == null || Objects.equals(description, job.getDescription())) - && (modelPlotConfig == null || Objects.equals(modelPlotConfig, job.getModelPlotConfig())) - && (analysisLimits == null || Objects.equals(analysisLimits, job.getAnalysisLimits())) - && updatesDetectors(job) == false - && (renormalizationWindowDays == null || Objects.equals(renormalizationWindowDays, job.getRenormalizationWindowDays())) - && (backgroundPersistInterval == null || Objects.equals(backgroundPersistInterval, job.getBackgroundPersistInterval())) - && (modelSnapshotRetentionDays == null || Objects.equals(modelSnapshotRetentionDays, job.getModelSnapshotRetentionDays())) - && (dailyModelSnapshotRetentionAfterDays == null - || Objects.equals(dailyModelSnapshotRetentionAfterDays, job.getDailyModelSnapshotRetentionAfterDays())) - && (resultsRetentionDays == null || Objects.equals(resultsRetentionDays, job.getResultsRetentionDays())) - && (categorizationFilters == null - || Objects.equals(categorizationFilters, job.getAnalysisConfig().getCategorizationFilters())) - && (perPartitionCategorizationConfig == null - || Objects.equals(perPartitionCategorizationConfig, job.getAnalysisConfig().getPerPartitionCategorizationConfig())) - && (customSettings == null || Objects.equals(customSettings, job.getCustomSettings())) - && (modelSnapshotId == null || Objects.equals(modelSnapshotId, job.getModelSnapshotId())) - && (modelSnapshotMinVersion == null || Objects.equals(modelSnapshotMinVersion, job.getModelSnapshotMinVersion())) - && (jobVersion == null || Objects.equals(jobVersion, job.getJobVersion())) - && (clearJobFinishTime == null || clearJobFinishTime == false || job.getFinishedTime() == null) - && (allowLazyOpen == null || Objects.equals(allowLazyOpen, job.allowLazyOpen())) - && (blocked == null || Objects.equals(blocked, job.getBlocked())) - && (modelPruneWindow == null || Objects.equals(modelPruneWindow, job.getAnalysisConfig().getModelPruneWindow())); + && (description == null || Objects.equals(description, job.getDescription())) + && (modelPlotConfig == null || Objects.equals(modelPlotConfig, job.getModelPlotConfig())) + && (analysisLimits == null || Objects.equals(analysisLimits, job.getAnalysisLimits())) + && updatesDetectors(job) == false + && (renormalizationWindowDays == null || Objects.equals(renormalizationWindowDays, job.getRenormalizationWindowDays())) + && (backgroundPersistInterval == null || Objects.equals(backgroundPersistInterval, job.getBackgroundPersistInterval())) + && (modelSnapshotRetentionDays == null || Objects.equals(modelSnapshotRetentionDays, job.getModelSnapshotRetentionDays())) + && (dailyModelSnapshotRetentionAfterDays == null + || Objects.equals(dailyModelSnapshotRetentionAfterDays, job.getDailyModelSnapshotRetentionAfterDays())) + && (resultsRetentionDays == null || Objects.equals(resultsRetentionDays, job.getResultsRetentionDays())) + && (categorizationFilters == null || Objects.equals(categorizationFilters, job.getAnalysisConfig().getCategorizationFilters())) + && (perPartitionCategorizationConfig == null + || Objects.equals(perPartitionCategorizationConfig, job.getAnalysisConfig().getPerPartitionCategorizationConfig())) + && (customSettings == null || Objects.equals(customSettings, job.getCustomSettings())) + && (modelSnapshotId == null || Objects.equals(modelSnapshotId, job.getModelSnapshotId())) + && (modelSnapshotMinVersion == null || Objects.equals(modelSnapshotMinVersion, job.getModelSnapshotMinVersion())) + && (jobVersion == null || Objects.equals(jobVersion, job.getJobVersion())) + && (clearJobFinishTime == null || clearJobFinishTime == false || job.getFinishedTime() == null) + && (allowLazyOpen == null || Objects.equals(allowLazyOpen, job.allowLazyOpen())) + && (blocked == null || Objects.equals(blocked, job.getBlocked())) + && (modelPruneWindow == null || Objects.equals(modelPruneWindow, job.getAnalysisConfig().getModelPruneWindow())); } boolean updatesDetectors(Job job) { @@ -569,7 +601,7 @@ boolean updatesDetectors(Job job) { } Detector detector = analysisConfig.getDetectors().get(detectorUpdate.detectorIndex); if (Objects.equals(detectorUpdate.description, detector.getDetectorDescription()) == false - || Objects.equals(detectorUpdate.rules, detector.getRules()) == false) { + || Objects.equals(detectorUpdate.rules, detector.getRules()) == false) { return true; } } @@ -589,47 +621,70 @@ public boolean equals(Object other) { JobUpdate that = (JobUpdate) other; return Objects.equals(this.jobId, that.jobId) - && Objects.equals(this.groups, that.groups) - && Objects.equals(this.description, that.description) - && Objects.equals(this.detectorUpdates, that.detectorUpdates) - && Objects.equals(this.modelPlotConfig, that.modelPlotConfig) - && Objects.equals(this.analysisLimits, that.analysisLimits) - && Objects.equals(this.renormalizationWindowDays, that.renormalizationWindowDays) - && Objects.equals(this.backgroundPersistInterval, that.backgroundPersistInterval) - && Objects.equals(this.modelSnapshotRetentionDays, that.modelSnapshotRetentionDays) - && Objects.equals(this.dailyModelSnapshotRetentionAfterDays, that.dailyModelSnapshotRetentionAfterDays) - && Objects.equals(this.resultsRetentionDays, that.resultsRetentionDays) - && Objects.equals(this.categorizationFilters, that.categorizationFilters) - && Objects.equals(this.perPartitionCategorizationConfig, that.perPartitionCategorizationConfig) - && Objects.equals(this.customSettings, that.customSettings) - && Objects.equals(this.modelSnapshotId, that.modelSnapshotId) - && Objects.equals(this.modelSnapshotMinVersion, that.modelSnapshotMinVersion) - && Objects.equals(this.jobVersion, that.jobVersion) - && Objects.equals(this.clearJobFinishTime, that.clearJobFinishTime) - && Objects.equals(this.allowLazyOpen, that.allowLazyOpen) - && Objects.equals(this.blocked, that.blocked) - && Objects.equals(this.modelPruneWindow, that.modelPruneWindow); + && Objects.equals(this.groups, that.groups) + && Objects.equals(this.description, that.description) + && Objects.equals(this.detectorUpdates, that.detectorUpdates) + && Objects.equals(this.modelPlotConfig, that.modelPlotConfig) + && Objects.equals(this.analysisLimits, that.analysisLimits) + && Objects.equals(this.renormalizationWindowDays, that.renormalizationWindowDays) + && Objects.equals(this.backgroundPersistInterval, that.backgroundPersistInterval) + && Objects.equals(this.modelSnapshotRetentionDays, that.modelSnapshotRetentionDays) + && Objects.equals(this.dailyModelSnapshotRetentionAfterDays, that.dailyModelSnapshotRetentionAfterDays) + && Objects.equals(this.resultsRetentionDays, that.resultsRetentionDays) + && Objects.equals(this.categorizationFilters, that.categorizationFilters) + && Objects.equals(this.perPartitionCategorizationConfig, that.perPartitionCategorizationConfig) + && Objects.equals(this.customSettings, that.customSettings) + && Objects.equals(this.modelSnapshotId, that.modelSnapshotId) + && Objects.equals(this.modelSnapshotMinVersion, that.modelSnapshotMinVersion) + && Objects.equals(this.jobVersion, that.jobVersion) + && Objects.equals(this.clearJobFinishTime, that.clearJobFinishTime) + && Objects.equals(this.allowLazyOpen, that.allowLazyOpen) + && Objects.equals(this.blocked, that.blocked) + && Objects.equals(this.modelPruneWindow, that.modelPruneWindow); } @Override public int hashCode() { - return Objects.hash(jobId, groups, description, detectorUpdates, modelPlotConfig, analysisLimits, renormalizationWindowDays, - backgroundPersistInterval, modelSnapshotRetentionDays, dailyModelSnapshotRetentionAfterDays, resultsRetentionDays, - categorizationFilters, perPartitionCategorizationConfig, customSettings, modelSnapshotId, - modelSnapshotMinVersion, jobVersion, clearJobFinishTime, allowLazyOpen, blocked, modelPruneWindow); + return Objects.hash( + jobId, + groups, + description, + detectorUpdates, + modelPlotConfig, + analysisLimits, + renormalizationWindowDays, + backgroundPersistInterval, + modelSnapshotRetentionDays, + dailyModelSnapshotRetentionAfterDays, + resultsRetentionDays, + categorizationFilters, + perPartitionCategorizationConfig, + customSettings, + modelSnapshotId, + modelSnapshotMinVersion, + jobVersion, + clearJobFinishTime, + allowLazyOpen, + blocked, + modelPruneWindow + ); } public static class DetectorUpdate implements Writeable, ToXContentObject { @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("detector_update", a -> new DetectorUpdate((int) a[0], (String) a[1], - (List) a[2])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "detector_update", + a -> new DetectorUpdate((int) a[0], (String) a[1], (List) a[2]) + ); static { PARSER.declareInt(ConstructingObjectParser.optionalConstructorArg(), Detector.DETECTOR_INDEX); PARSER.declareStringOrNull(ConstructingObjectParser.optionalConstructorArg(), Job.DESCRIPTION); - PARSER.declareObjectArray(ConstructingObjectParser.optionalConstructorArg(), (parser, parseFieldMatcher) -> - DetectionRule.STRICT_PARSER.apply(parser, parseFieldMatcher).build(), Detector.CUSTOM_RULES_FIELD); + PARSER.declareObjectArray( + ConstructingObjectParser.optionalConstructorArg(), + (parser, parseFieldMatcher) -> DetectionRule.STRICT_PARSER.apply(parser, parseFieldMatcher).build(), + Detector.CUSTOM_RULES_FIELD + ); } private final int detectorIndex; @@ -705,8 +760,9 @@ public boolean equals(Object other) { } DetectorUpdate that = (DetectorUpdate) other; - return this.detectorIndex == that.detectorIndex && Objects.equals(this.description, that.description) - && Objects.equals(this.rules, that.rules); + return this.detectorIndex == that.detectorIndex + && Objects.equals(this.description, that.description) + && Objects.equals(this.rules, that.rules); } } @@ -854,10 +910,29 @@ public Builder setModelPruneWindow(TimeValue modelPruneWindow) { } public JobUpdate build() { - return new JobUpdate(jobId, groups, description, detectorUpdates, modelPlotConfig, analysisLimits, backgroundPersistInterval, - renormalizationWindowDays, resultsRetentionDays, modelSnapshotRetentionDays, - dailyModelSnapshotRetentionAfterDays, categorizationFilters, perPartitionCategorizationConfig, customSettings, - modelSnapshotId, modelSnapshotMinVersion, jobVersion, clearJobFinishTime, allowLazyOpen, blocked, modelPruneWindow); + return new JobUpdate( + jobId, + groups, + description, + detectorUpdates, + modelPlotConfig, + analysisLimits, + backgroundPersistInterval, + renormalizationWindowDays, + resultsRetentionDays, + modelSnapshotRetentionDays, + dailyModelSnapshotRetentionAfterDays, + categorizationFilters, + perPartitionCategorizationConfig, + customSettings, + modelSnapshotId, + modelSnapshotMinVersion, + jobVersion, + clearJobFinishTime, + allowLazyOpen, + blocked, + modelPruneWindow + ); } } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/MlFilter.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/MlFilter.java index 175a9e2058c04..08bb09c4672ed 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/MlFilter.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/MlFilter.java @@ -6,12 +6,12 @@ */ package org.elasticsearch.xpack.core.ml.job.config; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ml.job.messages.Messages; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/ModelPlotConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/ModelPlotConfig.java index 231088288c4da..118205c5f3597 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/ModelPlotConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/ModelPlotConfig.java @@ -6,11 +6,11 @@ */ package org.elasticsearch.xpack.core.ml.job.config; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -29,8 +29,11 @@ public class ModelPlotConfig implements ToXContentObject, Writeable { public static final ConstructingObjectParser STRICT_PARSER = createParser(false); private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { - ConstructingObjectParser parser = new ConstructingObjectParser<>(TYPE_FIELD.getPreferredName(), - ignoreUnknownFields, a -> new ModelPlotConfig((boolean) a[0], (String) a[1], (Boolean) a[2])); + ConstructingObjectParser parser = new ConstructingObjectParser<>( + TYPE_FIELD.getPreferredName(), + ignoreUnknownFields, + a -> new ModelPlotConfig((boolean) a[0], (String) a[1], (Boolean) a[2]) + ); parser.declareBoolean(ConstructingObjectParser.constructorArg(), ENABLED_FIELD); parser.declareString(ConstructingObjectParser.optionalConstructorArg(), TERMS_FIELD); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/Operator.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/Operator.java index 616cec1220988..a3fd7fa9283e8 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/Operator.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/Operator.java @@ -6,10 +6,10 @@ */ package org.elasticsearch.xpack.core.ml.job.config; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.xcontent.ParseField; import java.io.IOException; import java.util.Locale; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/PerPartitionCategorizationConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/PerPartitionCategorizationConfig.java index 9d73d743e91f9..9cc37c869d075 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/PerPartitionCategorizationConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/PerPartitionCategorizationConfig.java @@ -6,11 +6,11 @@ */ package org.elasticsearch.xpack.core.ml.job.config; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; @@ -29,9 +29,11 @@ public class PerPartitionCategorizationConfig implements ToXContentObject, Write public static final ConstructingObjectParser STRICT_PARSER = createParser(false); private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { - ConstructingObjectParser parser = - new ConstructingObjectParser<>(TYPE_FIELD.getPreferredName(), ignoreUnknownFields, - a -> new PerPartitionCategorizationConfig((boolean) a[0], (Boolean) a[1])); + ConstructingObjectParser parser = new ConstructingObjectParser<>( + TYPE_FIELD.getPreferredName(), + ignoreUnknownFields, + a -> new PerPartitionCategorizationConfig((boolean) a[0], (Boolean) a[1]) + ); parser.declareBoolean(ConstructingObjectParser.constructorArg(), ENABLED_FIELD); parser.declareBoolean(ConstructingObjectParser.optionalConstructorArg(), STOP_ON_WARN); @@ -50,8 +52,14 @@ public PerPartitionCategorizationConfig(boolean enabled, Boolean stopOnWarn) { this.enabled = enabled; this.stopOnWarn = (stopOnWarn == null) ? false : stopOnWarn; if (this.enabled == false && this.stopOnWarn) { - throw ExceptionsHelper.badRequestException(STOP_ON_WARN.getPreferredName() + " cannot be true in " - + TYPE_FIELD.getPreferredName() + " when " + ENABLED_FIELD.getPreferredName() + " is false"); + throw ExceptionsHelper.badRequestException( + STOP_ON_WARN.getPreferredName() + + " cannot be true in " + + TYPE_FIELD.getPreferredName() + + " when " + + ENABLED_FIELD.getPreferredName() + + " is false" + ); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/RuleCondition.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/RuleCondition.java index 2705533f481cd..9baddb5311315 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/RuleCondition.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/RuleCondition.java @@ -6,11 +6,11 @@ */ package org.elasticsearch.xpack.core.ml.job.config; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -30,8 +30,11 @@ public class RuleCondition implements ToXContentObject, Writeable { public static final ConstructingObjectParser STRICT_PARSER = createParser(false); private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { - ConstructingObjectParser parser = new ConstructingObjectParser<>(RULE_CONDITION_FIELD.getPreferredName(), - ignoreUnknownFields, a -> new RuleCondition((AppliesTo) a[0], (Operator) a[1], (double) a[2])); + ConstructingObjectParser parser = new ConstructingObjectParser<>( + RULE_CONDITION_FIELD.getPreferredName(), + ignoreUnknownFields, + a -> new RuleCondition((AppliesTo) a[0], (Operator) a[1], (double) a[2]) + ); parser.declareString(ConstructingObjectParser.constructorArg(), AppliesTo::fromString, APPLIES_TO_FIELD); parser.declareString(ConstructingObjectParser.constructorArg(), Operator::fromString, Operator.OPERATOR_FIELD); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/RuleScope.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/RuleScope.java index 599b10680603d..07e11165fbc1b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/RuleScope.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/RuleScope.java @@ -10,9 +10,9 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ContextParser; -import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -39,14 +39,17 @@ public static ContextParser parser(boolean ignoreUnknownFields) if (unparsedScope.isEmpty()) { return new RuleScope(); } - ConstructingObjectParser filterRefParser = - ignoreUnknownFields ? FilterRef.LENIENT_PARSER : FilterRef.STRICT_PARSER; + ConstructingObjectParser filterRefParser = ignoreUnknownFields + ? FilterRef.LENIENT_PARSER + : FilterRef.STRICT_PARSER; Map scope = new HashMap<>(); for (Map.Entry entry : unparsedScope.entrySet()) { try (XContentBuilder builder = XContentFactory.jsonBuilder()) { builder.map((Map) entry.getValue()); - try (XContentParser scopeParser = XContentFactory.xContent(builder.contentType()).createParser( - NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, Strings.toString(builder))) { + try ( + XContentParser scopeParser = XContentFactory.xContent(builder.contentType()) + .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, Strings.toString(builder)) + ) { scope.put(entry.getKey(), filterRefParser.parse(scopeParser, null)); } } @@ -87,11 +90,13 @@ public void validate(Set validKeys) { Optional invalidKey = scope.keySet().stream().filter(k -> validKeys.contains(k) == false).findFirst(); if (invalidKey.isPresent()) { if (validKeys.isEmpty()) { - throw ExceptionsHelper.badRequestException(Messages.getMessage(Messages.JOB_CONFIG_DETECTION_RULE_SCOPE_NO_AVAILABLE_FIELDS, - invalidKey.get())); + throw ExceptionsHelper.badRequestException( + Messages.getMessage(Messages.JOB_CONFIG_DETECTION_RULE_SCOPE_NO_AVAILABLE_FIELDS, invalidKey.get()) + ); } - throw ExceptionsHelper.badRequestException(Messages.getMessage(Messages.JOB_CONFIG_DETECTION_RULE_SCOPE_HAS_INVALID_FIELD, - invalidKey.get(), validKeys)); + throw ExceptionsHelper.badRequestException( + Messages.getMessage(Messages.JOB_CONFIG_DETECTION_RULE_SCOPE_HAS_INVALID_FIELD, invalidKey.get(), validKeys) + ); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/groups/GroupOrJob.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/groups/GroupOrJob.java index 2b507c53cd53b..238bff8814e6d 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/groups/GroupOrJob.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/groups/GroupOrJob.java @@ -18,6 +18,7 @@ interface GroupOrJob { boolean isGroup(); + List jobs(); final class Group implements GroupOrJob { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/groups/GroupOrJobLookup.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/groups/GroupOrJobLookup.java index 1a0abefffc986..e8810a6b5be3c 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/groups/GroupOrJobLookup.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/groups/GroupOrJobLookup.java @@ -77,10 +77,11 @@ protected Set keys() { @Override protected Set nameSet() { - return groupOrJobLookup.values().stream() - .filter(groupOrJob -> groupOrJob.isGroup() == false) - .map(groupOrJob -> groupOrJob.jobs().get(0).getId()) - .collect(Collectors.toSet()); + return groupOrJobLookup.values() + .stream() + .filter(groupOrJob -> groupOrJob.isGroup() == false) + .map(groupOrJob -> groupOrJob.jobs().get(0).getId()) + .collect(Collectors.toSet()); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/messages/Messages.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/messages/Messages.java index bc066581e7d1d..fe99df5c602d7 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/messages/Messages.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/messages/Messages.java @@ -18,11 +18,11 @@ public final class Messages { public static final String DATAFEED_AGGREGATIONS_REQUIRES_JOB_WITH_SUMMARY_COUNT_FIELD = - "A job configured with a datafeed with aggregations must set summary_count_field_name; use doc_count or suitable alternative"; + "A job configured with a datafeed with aggregations must set summary_count_field_name; use doc_count or suitable alternative"; public static final String DATAFEED_CANNOT_DELETE_IN_CURRENT_STATE = "Cannot delete datafeed [{0}] while its status is {1}"; public static final String DATAFEED_CANNOT_UPDATE_IN_CURRENT_STATE = "Cannot update datafeed [{0}] while its status is {1}"; public static final String DATAFEED_CONFIG_CANNOT_USE_SCRIPT_FIELDS_WITH_AGGS = - "script_fields cannot be used in combination with aggregations"; + "script_fields cannot be used in combination with aggregations"; public static final String DATAFEED_CONFIG_INVALID_OPTION_VALUE = "Invalid {0} value ''{1}'' in datafeed configuration"; public static final String DATAFEED_CONFIG_DELAYED_DATA_CHECK_TOO_SMALL = "delayed_data_check_config: check_window [{0}] must be greater than the bucket_span [{1}]"; @@ -33,23 +33,21 @@ public final class Messages { public static final String DATAFEED_DOES_NOT_SUPPORT_JOB_WITH_LATENCY = "A job configured with datafeed cannot support latency"; public static final String DATAFEED_NOT_FOUND = "No datafeed with id [{0}] exists"; - public static final String DATAFEED_AGGREGATIONS_REQUIRES_DATE_HISTOGRAM = - "A date_histogram (or histogram) aggregation is required"; + public static final String DATAFEED_AGGREGATIONS_REQUIRES_DATE_HISTOGRAM = "A date_histogram (or histogram) aggregation is required"; public static final String DATAFEED_AGGREGATIONS_MAX_ONE_DATE_HISTOGRAM = - "Aggregations can only have 1 date_histogram or histogram aggregation"; + "Aggregations can only have 1 date_histogram or histogram aggregation"; public static final String DATAFEED_AGGREGATIONS_REQUIRES_DATE_HISTOGRAM_NO_SIBLINGS = - "The date_histogram (or histogram) aggregation cannot have sibling aggregations"; - public static final String DATAFEED_AGGREGATIONS_INTERVAL_MUST_BE_GREATER_THAN_ZERO = - "Aggregation interval must be greater than 0"; + "The date_histogram (or histogram) aggregation cannot have sibling aggregations"; + public static final String DATAFEED_AGGREGATIONS_INTERVAL_MUST_BE_GREATER_THAN_ZERO = "Aggregation interval must be greater than 0"; public static final String DATAFEED_AGGREGATIONS_INTERVAL_MUST_BE_DIVISOR_OF_BUCKET_SPAN = - "Aggregation interval [{0}] must be a divisor of the bucket_span [{1}]"; + "Aggregation interval [{0}] must be a divisor of the bucket_span [{1}]"; public static final String DATAFEED_AGGREGATIONS_INTERVAL_MUST_LESS_OR_EQUAL_TO_BUCKET_SPAN = - "Aggregation interval [{0}] must be less than or equal to the bucket_span [{1}]"; + "Aggregation interval [{0}] must be less than or equal to the bucket_span [{1}]"; public static final String DATAFEED_DATA_HISTOGRAM_MUST_HAVE_NESTED_MAX_AGGREGATION = - "Date histogram must have nested max aggregation for time_field [{0}]"; + "Date histogram must have nested max aggregation for time_field [{0}]"; public static final String DATAFEED_MISSING_MAX_AGGREGATION_FOR_TIME_FIELD = "Missing max aggregation for time_field [{0}]"; public static final String DATAFEED_FREQUENCY_MUST_BE_MULTIPLE_OF_AGGREGATIONS_INTERVAL = - "Datafeed frequency [{0}] must be a multiple of the aggregation interval [{1}]"; + "Datafeed frequency [{0}] must be a multiple of the aggregation interval [{1}]"; public static final String DATAFEED_AGGREGATIONS_COMPOSITE_AGG_MUST_HAVE_SINGLE_DATE_SOURCE = "Composite aggregation [{0}] must have exactly one date_histogram source"; public static final String DATAFEED_AGGREGATIONS_COMPOSITE_AGG_DATE_HISTOGRAM_SOURCE_MISSING_BUCKET = @@ -59,9 +57,9 @@ public final class Messages { public static final String DATAFEED_AGGREGATIONS_COMPOSITE_AGG_MUST_BE_TOP_LEVEL_AND_ALONE = "Composite aggregation [{0}] must be the only composite agg and should be the only top level aggregation"; public static final String DATAFEED_ID_ALREADY_TAKEN = "A datafeed with id [{0}] already exists"; - public static final String DATAFEED_NEEDS_REMOTE_CLUSTER_SEARCH = "Datafeed [{0}] is configured with a remote index pattern(s) {1}" + - " but the current node [{2}] is not allowed to connect to remote clusters." + - " Please enable node.remote_cluster_client for all machine learning nodes and master-eligible nodes."; + public static final String DATAFEED_NEEDS_REMOTE_CLUSTER_SEARCH = "Datafeed [{0}] is configured with a remote index pattern(s) {1}" + + " but the current node [{2}] is not allowed to connect to remote clusters." + + " Please enable node.remote_cluster_client for all machine learning nodes and master-eligible nodes."; public static final String DATA_FRAME_ANALYTICS_BAD_QUERY_FORMAT = "Data Frame Analytics config query is not parsable"; public static final String DATA_FRAME_ANALYTICS_BAD_FIELD_FILTER = "No field [{0}] could be detected"; @@ -73,11 +71,11 @@ public final class Messages { public static final String DATA_FRAME_ANALYTICS_AUDIT_FORCE_STOPPED = "Stopped analytics (forced)"; public static final String DATA_FRAME_ANALYTICS_AUDIT_DELETED = "Deleted analytics"; public static final String DATA_FRAME_ANALYTICS_AUDIT_UPDATED_STATE_WITH_REASON = - "Updated analytics task state to [{0}] with reason [{1}]"; + "Updated analytics task state to [{0}] with reason [{1}]"; public static final String DATA_FRAME_ANALYTICS_AUDIT_ESTIMATED_MEMORY_USAGE = "Estimated memory usage [{0}]"; public static final String DATA_FRAME_ANALYTICS_AUDIT_ESTIMATED_MEMORY_USAGE_HIGHER_THAN_CONFIGURED = - "Configured model memory limit [{0}] is lower than the expected memory usage [{1}]. " + - "The analytics job may fail due to configured memory constraints."; + "Configured model memory limit [{0}] is lower than the expected memory usage [{1}]. " + + "The analytics job may fail due to configured memory constraints."; public static final String DATA_FRAME_ANALYTICS_AUDIT_CREATING_DEST_INDEX = "Creating destination index [{0}]"; public static final String DATA_FRAME_ANALYTICS_AUDIT_REUSING_DEST_INDEX = "Using existing destination index [{0}]"; public static final String DATA_FRAME_ANALYTICS_AUDIT_STARTED_REINDEXING = "Started reindexing to destination index [{0}]"; @@ -95,12 +93,12 @@ public final class Messages { public static final String FILTER_NOT_FOUND = "No filter with id [{0}] exists"; public static final String INCONSISTENT_ID = - "Inconsistent {0}; ''{1}'' specified in the body differs from ''{2}'' specified as a URL argument"; - public static final String INVALID_ID = "Invalid {0}; ''{1}'' can contain lowercase alphanumeric (a-z and 0-9), hyphens or " + - "underscores; must start and end with alphanumeric"; + "Inconsistent {0}; ''{1}'' specified in the body differs from ''{2}'' specified as a URL argument"; + public static final String INVALID_ID = "Invalid {0}; ''{1}'' can contain lowercase alphanumeric (a-z and 0-9), hyphens or " + + "underscores; must start and end with alphanumeric"; public static final String ID_TOO_LONG = "Invalid {0}; ''{1}'' cannot contain more than {2} characters."; - public static final String INVALID_GROUP = "Invalid group id ''{0}''; must be non-empty string and may contain lowercase alphanumeric" + - " (a-z and 0-9), hyphens or underscores; must start and end with alphanumeric"; + public static final String INVALID_GROUP = "Invalid group id ''{0}''; must be non-empty string and may contain lowercase alphanumeric" + + " (a-z and 0-9), hyphens or underscores; must start and end with alphanumeric"; public static final String INFERENCE_TRAINED_MODEL_EXISTS = "Trained machine learning model [{0}] already exists"; public static final String INFERENCE_TRAINED_MODEL_DOC_EXISTS = "Trained machine learning model chunked doc [{0}][{1}] already exists"; @@ -126,13 +124,13 @@ public final class Messages { public static final String INFERENCE_TOO_MANY_DEFINITIONS_REQUESTED = "Getting model definition is not supported when getting more than one model"; public static final String INFERENCE_WARNING_ALL_FIELDS_MISSING = "Model [{0}] could not be inferred as all fields were missing"; - public static final String INFERENCE_INVALID_TAGS = "Invalid tags {0}; must only can contain lowercase alphanumeric (a-z and 0-9), " + - "hyphens or underscores, must start and end with alphanumeric, and must be less than {1} characters."; + public static final String INFERENCE_INVALID_TAGS = "Invalid tags {0}; must only can contain lowercase alphanumeric (a-z and 0-9), " + + "hyphens or underscores, must start and end with alphanumeric, and must be less than {1} characters."; public static final String INFERENCE_TAGS_AND_MODEL_IDS_UNIQUE = "The provided tags {0} must not match existing model_ids."; public static final String INFERENCE_MODEL_ID_AND_TAGS_UNIQUE = "The provided model_id {0} must not match existing tags."; - public static final String INVALID_MODEL_ALIAS = "Invalid model_alias; ''{0}'' can contain lowercase alphanumeric (a-z and 0-9), " + - "hyphens or underscores; must start with alphanumeric and cannot end with numbers"; + public static final String INVALID_MODEL_ALIAS = "Invalid model_alias; ''{0}'' can contain lowercase alphanumeric (a-z and 0-9), " + + "hyphens or underscores; must start with alphanumeric and cannot end with numbers"; public static final String TRAINED_MODEL_INPUTS_DIFFER_SIGNIFICANTLY = "The input fields for new model [{0}] and for old model [{1}] differ significantly, model results may change drastically."; @@ -149,8 +147,8 @@ public final class Messages { public static final String JOB_AUDIT_DATAFEED_LOOKBACK_NO_DATA = "Datafeed lookback retrieved no data"; public static final String JOB_AUDIT_DATAFEED_NO_DATA = "Datafeed has been retrieving no data for a while"; public static final String JOB_AUDIT_DATAFEED_MISSING_DATA = - "Datafeed has missed {0} documents due to ingest latency, latest bucket with missing data is [{1}]." + - " Consider increasing query_delay"; + "Datafeed has missed {0} documents due to ingest latency, latest bucket with missing data is [{1}]." + + " Consider increasing query_delay"; public static final String JOB_AUDIT_DATAFEED_RECOVERED = "Datafeed has recovered data extraction and analysis"; public static final String JOB_AUDIT_DATAFEED_STARTED_FROM_TO = "Datafeed started (from: {0} to: {1}) with frequency [{2}]"; public static final String JOB_AUDIT_DATAFEED_STARTED_REALTIME = "Datafeed started in real-time"; @@ -167,13 +165,13 @@ public final class Messages { public static final String JOB_AUDIT_SNAPSHOT_DELETED = "Model snapshot [{0}] with description ''{1}'' deleted"; public static final String JOB_AUDIT_SNAPSHOTS_DELETED = "[{0}] expired model snapshots deleted"; public static final String JOB_AUDIT_CALENDARS_UPDATED_ON_PROCESS = "Updated calendars in running process"; - public static final String JOB_AUDIT_MEMORY_STATUS_SOFT_LIMIT = "Job memory status changed to soft_limit; memory pruning will now be " + - "more aggressive"; - public static final String JOB_AUDIT_MEMORY_STATUS_HARD_LIMIT = "Job memory status changed to hard_limit; " + - "job exceeded model memory limit {0} by {1}. " + - "Adjust the analysis_limits.model_memory_limit setting to ensure all data is analyzed"; - public static final String JOB_AUDIT_MEMORY_STATUS_HARD_LIMIT_PRE_7_2 = "Job memory status changed to hard_limit at {0}; adjust the " + - "analysis_limits.model_memory_limit setting to ensure all data is analyzed"; + public static final String JOB_AUDIT_MEMORY_STATUS_SOFT_LIMIT = "Job memory status changed to soft_limit; memory pruning will now be " + + "more aggressive"; + public static final String JOB_AUDIT_MEMORY_STATUS_HARD_LIMIT = "Job memory status changed to hard_limit; " + + "job exceeded model memory limit {0} by {1}. " + + "Adjust the analysis_limits.model_memory_limit setting to ensure all data is analyzed"; + public static final String JOB_AUDIT_MEMORY_STATUS_HARD_LIMIT_PRE_7_2 = "Job memory status changed to hard_limit at {0}; adjust the " + + "analysis_limits.model_memory_limit setting to ensure all data is analyzed"; public static final String JOB_AUDIT_REQUIRES_MORE_MEMORY_TO_RUN = "Job requires at least [{0}] free memory " + "on a machine learning capable node to run; [{1}] are available. " @@ -181,90 +179,83 @@ public final class Messages { public static final String JOB_CONFIG_CATEGORIZATION_FILTERS_CONTAINS_DUPLICATES = "categorization_filters contain duplicates"; public static final String JOB_CONFIG_CATEGORIZATION_FILTERS_CONTAINS_EMPTY = - "categorization_filters are not allowed to contain empty strings"; + "categorization_filters are not allowed to contain empty strings"; public static final String JOB_CONFIG_CATEGORIZATION_FILTERS_CONTAINS_INVALID_REGEX = - "categorization_filters contains invalid regular expression ''{0}''"; + "categorization_filters contains invalid regular expression ''{0}''"; public static final String JOB_CONFIG_CATEGORIZATION_FILTERS_INCOMPATIBLE_WITH_CATEGORIZATION_ANALYZER = - "categorization_filters cannot be used with categorization_analyzer - " + - "instead specify them as pattern_replace char_filters in the analyzer"; + "categorization_filters cannot be used with categorization_analyzer - " + + "instead specify them as pattern_replace char_filters in the analyzer"; public static final String JOB_CONFIG_CATEGORIZATION_FILTERS_REQUIRE_CATEGORIZATION_FIELD_NAME = - "categorization_filters require setting categorization_field_name"; + "categorization_filters require setting categorization_field_name"; public static final String JOB_CONFIG_CATEGORIZATION_ANALYZER_REQUIRES_CATEGORIZATION_FIELD_NAME = - "categorization_analyzer requires setting categorization_field_name"; + "categorization_analyzer requires setting categorization_field_name"; public static final String JOB_CONFIG_DETECTION_RULE_NOT_SUPPORTED_BY_FUNCTION = - "Invalid detector rule: function {0} only supports conditions that apply to time"; + "Invalid detector rule: function {0} only supports conditions that apply to time"; public static final String JOB_CONFIG_DETECTION_RULE_REQUIRES_SCOPE_OR_CONDITION = - "Invalid detector rule: at least scope or a condition is required"; + "Invalid detector rule: at least scope or a condition is required"; public static final String JOB_CONFIG_DETECTION_RULE_SCOPE_NO_AVAILABLE_FIELDS = - "Invalid detector rule: scope field ''{0}'' is invalid; detector has no available fields for scoping"; + "Invalid detector rule: scope field ''{0}'' is invalid; detector has no available fields for scoping"; public static final String JOB_CONFIG_DETECTION_RULE_SCOPE_HAS_INVALID_FIELD = - "Invalid detector rule: scope field ''{0}'' is invalid; select from {1}"; + "Invalid detector rule: scope field ''{0}'' is invalid; select from {1}"; public static final String JOB_CONFIG_FIELDNAME_INCOMPATIBLE_FUNCTION = "field_name cannot be used with function ''{0}''"; public static final String JOB_CONFIG_FIELD_VALUE_TOO_LOW = "{0} cannot be less than {1,number}. Value = {2,number}"; public static final String JOB_CONFIG_MODEL_MEMORY_LIMIT_TOO_LOW = "model_memory_limit must be at least {1}. Value = {0}"; public static final String JOB_CONFIG_MODEL_MEMORY_LIMIT_GREATER_THAN_MAX = - "model_memory_limit [{0}] must be less than the value of the " + - MachineLearningField.MAX_MODEL_MEMORY_LIMIT.getKey() + - " setting [{1}]"; + "model_memory_limit [{0}] must be less than the value of the " + + MachineLearningField.MAX_MODEL_MEMORY_LIMIT.getKey() + + " setting [{1}]"; public static final String JOB_CONFIG_FUNCTION_INCOMPATIBLE_PRESUMMARIZED = - "The ''{0}'' function cannot be used in jobs that will take pre-summarized input"; + "The ''{0}'' function cannot be used in jobs that will take pre-summarized input"; public static final String JOB_CONFIG_FUNCTION_REQUIRES_BYFIELD = "by_field_name must be set when the ''{0}'' function is used"; public static final String JOB_CONFIG_FUNCTION_REQUIRES_FIELDNAME = "field_name must be set when the ''{0}'' function is used"; public static final String JOB_CONFIG_FUNCTION_REQUIRES_OVERFIELD = "over_field_name must be set when the ''{0}'' function is used"; public static final String JOB_CONFIG_ID_ALREADY_TAKEN = "The job cannot be created with the Id ''{0}''. The Id is already used."; public static final String JOB_CONFIG_ID_TOO_LONG = "The job id cannot contain more than {0,number,integer} characters."; public static final String JOB_CONFIG_INVALID_CREATE_SETTINGS = - "The job is configured with fields [{0}] that are illegal to set at job creation"; + "The job is configured with fields [{0}] that are illegal to set at job creation"; public static final String JOB_CONFIG_INVALID_FIELDNAME_CHARS = - "Invalid field name ''{0}''. Field names including over, by and partition " + - "fields cannot contain any of these characters: {1}"; + "Invalid field name ''{0}''. Field names including over, by and partition " + "fields cannot contain any of these characters: {1}"; public static final String JOB_CONFIG_INVALID_FIELDNAME = - "Invalid field name ''{0}''. Field names including over, by and partition fields cannot be ''{1}''"; + "Invalid field name ''{0}''. Field names including over, by and partition fields cannot be ''{1}''"; public static final String JOB_CONFIG_INVALID_TIMEFORMAT = "Invalid Time format string ''{0}''"; public static final String JOB_CONFIG_MISSING_ANALYSISCONFIG = "An analysis_config must be set"; public static final String JOB_CONFIG_MISSING_DATA_DESCRIPTION = "A data_description must be set"; public static final String JOB_CONFIG_ANALYSIS_FIELD_MUST_BE_SET = - "Unless a count or temporal function is used one of field_name, by_field_name or over_field_name must be set"; + "Unless a count or temporal function is used one of field_name, by_field_name or over_field_name must be set"; public static final String JOB_CONFIG_NO_DETECTORS = "No detectors configured"; - public static final String JOB_CONFIG_OVERFIELD_INCOMPATIBLE_FUNCTION = - "over_field_name cannot be used with function ''{0}''"; + public static final String JOB_CONFIG_OVERFIELD_INCOMPATIBLE_FUNCTION = "over_field_name cannot be used with function ''{0}''"; public static final String JOB_CONFIG_UNKNOWN_FUNCTION = "Unknown function ''{0}''"; public static final String JOB_CONFIG_UPDATE_ANALYSIS_LIMITS_MODEL_MEMORY_LIMIT_CANNOT_BE_DECREASED = - "Invalid update value for analysis_limits: model_memory_limit cannot be decreased below current usage; " + - "current usage [{0}], update had [{1}]"; - public static final String JOB_CONFIG_DUPLICATE_DETECTORS_DISALLOWED = - "Duplicate detectors are not allowed: [{0}]"; - public static final String JOB_CONFIG_DETECTOR_DUPLICATE_FIELD_NAME = - "{0} and {1} cannot be the same: ''{2}''"; - public static final String JOB_CONFIG_DETECTOR_COUNT_DISALLOWED = - "''count'' is not a permitted value for {0}"; - public static final String JOB_CONFIG_DETECTOR_BY_DISALLOWED = - "''by'' is not a permitted value for {0}"; - public static final String JOB_CONFIG_DETECTOR_OVER_DISALLOWED = - "''over'' is not a permitted value for {0}"; + "Invalid update value for analysis_limits: model_memory_limit cannot be decreased below current usage; " + + "current usage [{0}], update had [{1}]"; + public static final String JOB_CONFIG_DUPLICATE_DETECTORS_DISALLOWED = "Duplicate detectors are not allowed: [{0}]"; + public static final String JOB_CONFIG_DETECTOR_DUPLICATE_FIELD_NAME = "{0} and {1} cannot be the same: ''{2}''"; + public static final String JOB_CONFIG_DETECTOR_COUNT_DISALLOWED = "''count'' is not a permitted value for {0}"; + public static final String JOB_CONFIG_DETECTOR_BY_DISALLOWED = "''by'' is not a permitted value for {0}"; + public static final String JOB_CONFIG_DETECTOR_OVER_DISALLOWED = "''over'' is not a permitted value for {0}"; public static final String JOB_CONFIG_MAPPING_TYPE_CLASH = - "This job would cause a mapping clash with existing field [{0}] - avoid the clash by assigning a dedicated results index"; + "This job would cause a mapping clash with existing field [{0}] - avoid the clash by assigning a dedicated results index"; public static final String JOB_CONFIG_TIME_FIELD_NOT_ALLOWED_IN_ANALYSIS_CONFIG = - "data_description.time_field may not be used in the analysis_config"; - public static final String JOB_CONFIG_TIME_FIELD_CANNOT_BE_RUNTIME = - "data_description.time_field [{0}] cannot be a runtime field"; - public static final String JOB_CONFIG_MODEL_SNAPSHOT_RETENTION_SETTINGS_INCONSISTENT = - "The value of '" + Job.DAILY_MODEL_SNAPSHOT_RETENTION_AFTER_DAYS + "' [{0}] cannot be greater than '" + - Job.MODEL_SNAPSHOT_RETENTION_DAYS + "' [{1}]"; - public static final String JOB_CONFIG_DATAFEED_CONFIG_JOB_ID_MISMATCH = - "datafeed job_id [{0}] does not equal job id [{1}}"; + "data_description.time_field may not be used in the analysis_config"; + public static final String JOB_CONFIG_TIME_FIELD_CANNOT_BE_RUNTIME = "data_description.time_field [{0}] cannot be a runtime field"; + public static final String JOB_CONFIG_MODEL_SNAPSHOT_RETENTION_SETTINGS_INCONSISTENT = "The value of '" + + Job.DAILY_MODEL_SNAPSHOT_RETENTION_AFTER_DAYS + + "' [{0}] cannot be greater than '" + + Job.MODEL_SNAPSHOT_RETENTION_DAYS + + "' [{1}]"; + public static final String JOB_CONFIG_DATAFEED_CONFIG_JOB_ID_MISMATCH = "datafeed job_id [{0}] does not equal job id [{1}}"; public static final String JOB_AND_GROUP_NAMES_MUST_BE_UNIQUE = - "job and group names must be unique but job [{0}] and group [{0}] have the same name"; + "job and group names must be unique but job [{0}] and group [{0}] have the same name"; public static final String JOB_UNKNOWN_ID = "No known job with id ''{0}''"; public static final String JOB_FORECAST_NATIVE_PROCESS_KILLED = "forecast unable to complete as native process was killed."; public static final String REST_CANNOT_DELETE_HIGHEST_PRIORITY = - "Model snapshot ''{0}'' is the active snapshot for job ''{1}'', so cannot be deleted"; + "Model snapshot ''{0}'' is the active snapshot for job ''{1}'', so cannot be deleted"; public static final String REST_INVALID_DATETIME_PARAMS = - "Query param [{0}] with value [{1}] cannot be parsed as a date or converted to a number (epoch)."; + "Query param [{0}] with value [{1}] cannot be parsed as a date or converted to a number (epoch)."; public static final String REST_INVALID_FLUSH_PARAMS_MISSING = "Invalid flush parameters: ''{0}'' has not been specified."; public static final String REST_INVALID_FLUSH_PARAMS_UNEXPECTED = "Invalid flush parameters: unexpected ''{0}''."; public static final String REST_JOB_NOT_CLOSED_REVERT = "Can only revert to a model snapshot when the job is closed."; @@ -276,8 +267,7 @@ public final class Messages { "Forecast(s) [{0}] for job [{1}] needs to be either FAILED or FINISHED to be deleted"; public static final String FIELD_CANNOT_BE_NULL = "Field [{0}] cannot be null"; - private Messages() { - } + private Messages() {} /** * Returns the message parameter @@ -294,7 +284,7 @@ public static String getMessage(String message) { * @param message Should be one of the statics defined in this class * @param args MessageFormat arguments. See {@linkplain MessageFormat#format(Object)}] */ - public static String getMessage(String message, Object...args) { + public static String getMessage(String message, Object... args) { return new MessageFormat(message, Locale.ROOT).format(args); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/persistence/AnomalyDetectorsIndex.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/persistence/AnomalyDetectorsIndex.java index 30919dbfcdbdd..7a9cbbc557d52 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/persistence/AnomalyDetectorsIndex.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/persistence/AnomalyDetectorsIndex.java @@ -75,10 +75,13 @@ public static String jobStateIndexPattern() { * Creates the .ml-state-000001 index (if necessary) * Creates the .ml-state-write alias for the .ml-state-000001 index (if necessary) */ - public static void createStateIndexAndAliasIfNecessary(Client client, ClusterState state, - IndexNameExpressionResolver resolver, - TimeValue masterNodeTimeout, - final ActionListener finalListener) { + public static void createStateIndexAndAliasIfNecessary( + Client client, + ClusterState state, + IndexNameExpressionResolver resolver, + TimeValue masterNodeTimeout, + final ActionListener finalListener + ) { MlIndexAndAlias.createIndexAndAliasIfNecessary( client, state, @@ -86,14 +89,17 @@ public static void createStateIndexAndAliasIfNecessary(Client client, ClusterSta AnomalyDetectorsIndexFields.STATE_INDEX_PREFIX, AnomalyDetectorsIndex.jobStateIndexWriteAlias(), masterNodeTimeout, - finalListener); + finalListener + ); } - public static void createStateIndexAndAliasIfNecessaryAndWaitForYellow(Client client, - ClusterState state, - IndexNameExpressionResolver resolver, - TimeValue masterNodeTimeout, - final ActionListener finalListener) { + public static void createStateIndexAndAliasIfNecessaryAndWaitForYellow( + Client client, + ClusterState state, + IndexNameExpressionResolver resolver, + TimeValue masterNodeTimeout, + final ActionListener finalListener + ) { final ActionListener stateIndexAndAliasCreated = ActionListener.wrap(success -> { final ClusterHealthRequest request = Requests.clusterHealthRequest(AnomalyDetectorsIndex.jobStateIndexWriteAlias()) .waitForYellowStatus() @@ -123,7 +129,10 @@ public static String wrappedResultsMapping() { } public static String resultsMapping() { - return TemplateUtils.loadTemplate(RESOURCE_PATH + "results_index_mappings.json", - Version.CURRENT.toString(), RESULTS_MAPPINGS_VERSION_VARIABLE); + return TemplateUtils.loadTemplate( + RESOURCE_PATH + "results_index_mappings.json", + Version.CURRENT.toString(), + RESULTS_MAPPINGS_VERSION_VARIABLE + ); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/persistence/ElasticsearchMappings.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/persistence/ElasticsearchMappings.java index 69ac7144e29db..e755ddfe34c7c 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/persistence/ElasticsearchMappings.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/persistence/ElasticsearchMappings.java @@ -22,9 +22,9 @@ import org.elasticsearch.common.CheckedSupplier; import org.elasticsearch.common.collect.ImmutableOpenMap; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.Index; import org.elasticsearch.plugins.MapperPlugin; +import org.elasticsearch.xcontent.XContentType; import java.io.IOException; import java.util.ArrayList; @@ -96,16 +96,13 @@ public class ElasticsearchMappings { private static final Logger logger = LogManager.getLogger(ElasticsearchMappings.class); - private ElasticsearchMappings() { - } + private ElasticsearchMappings() {} static String[] mappingRequiresUpdate(ClusterState state, String[] concreteIndices, Version minVersion) { List indicesToUpdate = new ArrayList<>(); - ImmutableOpenMap currentMapping = state.metadata().findMappings(concreteIndices, - MapperPlugin.NOOP_FIELD_FILTER, - Metadata.ON_NEXT_INDEX_FIND_MAPPINGS_NOOP - ); + ImmutableOpenMap currentMapping = state.metadata() + .findMappings(concreteIndices, MapperPlugin.NOOP_FIELD_FILTER, Metadata.ON_NEXT_INDEX_FIND_MAPPINGS_NOOP); for (String index : concreteIndices) { MappingMetadata metadata = currentMapping.get(index); @@ -148,10 +145,14 @@ static String[] mappingRequiresUpdate(ClusterState state, String[] concreteIndic return indicesToUpdate.toArray(new String[indicesToUpdate.size()]); } - public static void addDocMappingIfMissing(String alias, - CheckedSupplier mappingSupplier, - Client client, ClusterState state, TimeValue masterNodeTimeout, - ActionListener listener) { + public static void addDocMappingIfMissing( + String alias, + CheckedSupplier mappingSupplier, + Client client, + ClusterState state, + TimeValue masterNodeTimeout, + ActionListener listener + ) { IndexAbstraction indexAbstraction = state.metadata().getIndicesLookup().get(alias); if (indexAbstraction == null) { // The index has never been created yet @@ -168,15 +169,19 @@ public static void addDocMappingIfMissing(String alias, putMappingRequest.source(mapping, XContentType.JSON); putMappingRequest.origin(ML_ORIGIN); putMappingRequest.masterNodeTimeout(masterNodeTimeout); - executeAsyncWithOrigin(client, ML_ORIGIN, PutMappingAction.INSTANCE, putMappingRequest, - ActionListener.wrap(response -> { - if (response.isAcknowledged()) { - listener.onResponse(true); - } else { - listener.onFailure(new ElasticsearchException("Attempt to put missing mapping in indices " - + Arrays.toString(indicesThatRequireAnUpdate) + " was not acknowledged")); - } - }, listener::onFailure)); + executeAsyncWithOrigin(client, ML_ORIGIN, PutMappingAction.INSTANCE, putMappingRequest, ActionListener.wrap(response -> { + if (response.isAcknowledged()) { + listener.onResponse(true); + } else { + listener.onFailure( + new ElasticsearchException( + "Attempt to put missing mapping in indices " + + Arrays.toString(indicesThatRequireAnUpdate) + + " was not acknowledged" + ) + ); + } + }, listener::onFailure)); } catch (IOException e) { listener.onFailure(e); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/output/FlushAcknowledgement.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/output/FlushAcknowledgement.java index d84e904f44025..c741138e1758e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/output/FlushAcknowledgement.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/output/FlushAcknowledgement.java @@ -6,11 +6,11 @@ */ package org.elasticsearch.xpack.core.ml.job.process.autodetect.output; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -30,7 +30,9 @@ public class FlushAcknowledgement implements ToXContentObject, Writeable { public static final ParseField LAST_FINALIZED_BUCKET_END = new ParseField("last_finalized_bucket_end"); public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - TYPE.getPreferredName(), a -> new FlushAcknowledgement((String) a[0], (Long) a[1])); + TYPE.getPreferredName(), + a -> new FlushAcknowledgement((String) a[0], (Long) a[1]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), ID); @@ -43,8 +45,9 @@ public class FlushAcknowledgement implements ToXContentObject, Writeable { public FlushAcknowledgement(String id, Long lastFinalizedBucketEndMs) { this.id = id; // The C++ passes 0 when last finalized bucket end is not available, so treat 0 as null - this.lastFinalizedBucketEnd = - (lastFinalizedBucketEndMs != null && lastFinalizedBucketEndMs > 0) ? Instant.ofEpochMilli(lastFinalizedBucketEndMs) : null; + this.lastFinalizedBucketEnd = (lastFinalizedBucketEndMs != null && lastFinalizedBucketEndMs > 0) + ? Instant.ofEpochMilli(lastFinalizedBucketEndMs) + : null; } public FlushAcknowledgement(String id, Instant lastFinalizedBucketEnd) { @@ -78,8 +81,11 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.startObject(); builder.field(ID.getPreferredName(), id); if (lastFinalizedBucketEnd != null) { - builder.timeField(LAST_FINALIZED_BUCKET_END.getPreferredName(), LAST_FINALIZED_BUCKET_END.getPreferredName() + "_string", - lastFinalizedBucketEnd.toEpochMilli()); + builder.timeField( + LAST_FINALIZED_BUCKET_END.getPreferredName(), + LAST_FINALIZED_BUCKET_END.getPreferredName() + "_string", + lastFinalizedBucketEnd.toEpochMilli() + ); } builder.endObject(); return builder; @@ -99,7 +105,6 @@ public boolean equals(Object obj) { return false; } FlushAcknowledgement other = (FlushAcknowledgement) obj; - return Objects.equals(id, other.id) && - Objects.equals(lastFinalizedBucketEnd, other.lastFinalizedBucketEnd); + return Objects.equals(id, other.id) && Objects.equals(lastFinalizedBucketEnd, other.lastFinalizedBucketEnd); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/CategorizationStatus.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/CategorizationStatus.java index e1b4e498a26f8..3096168cdaab2 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/CategorizationStatus.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/CategorizationStatus.java @@ -19,7 +19,8 @@ * means that inappropriate numbers of categories are being found */ public enum CategorizationStatus implements Writeable { - OK, WARN; + OK, + WARN; public static CategorizationStatus fromString(String statusName) { return valueOf(statusName.trim().toUpperCase(Locale.ROOT)); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/CategorizerState.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/CategorizerState.java index 071ce87c9cda0..7ff28a12c4fda 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/CategorizerState.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/CategorizerState.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.xpack.core.ml.job.process.autodetect.state; - /** * The categorizer state does not need to be understood on the Java side. * The Java code only needs to know how to form the document IDs so that @@ -48,7 +47,5 @@ public static final String extractJobId(String docId) { return suffixIndex <= 0 ? null : docId.substring(0, suffixIndex); } - private CategorizerState() { - } + private CategorizerState() {} } - diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/CategorizerStats.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/CategorizerStats.java index 6fce6b37d8bda..91f09bc8171da 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/CategorizerStats.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/CategorizerStats.java @@ -7,13 +7,13 @@ package org.elasticsearch.xpack.core.ml.job.process.autodetect.state; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.common.time.TimeUtils; @@ -53,8 +53,11 @@ public class CategorizerStats implements ToXContentObject, Writeable { public static final ConstructingObjectParser LENIENT_PARSER = createParser(true); private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { - ConstructingObjectParser parser = new ConstructingObjectParser<>(RESULT_TYPE_FIELD.getPreferredName(), - ignoreUnknownFields, a -> new Builder((String) a[0])); + ConstructingObjectParser parser = new ConstructingObjectParser<>( + RESULT_TYPE_FIELD.getPreferredName(), + ignoreUnknownFields, + a -> new Builder((String) a[0]) + ); parser.declareString(ConstructingObjectParser.constructorArg(), Job.ID); parser.declareString((modelSizeStat, s) -> {}, Result.RESULT_TYPE); @@ -66,12 +69,24 @@ private static ConstructingObjectParser createParser(boolean igno parser.declareLong(Builder::setRareCategoryCount, RARE_CATEGORY_COUNT_FIELD); parser.declareLong(Builder::setDeadCategoryCount, DEAD_CATEGORY_COUNT_FIELD); parser.declareLong(Builder::setFailedCategoryCount, FAILED_CATEGORY_COUNT_FIELD); - parser.declareField(Builder::setCategorizationStatus, - p -> CategorizationStatus.fromString(p.text()), CATEGORIZATION_STATUS_FIELD, ValueType.STRING); - parser.declareField(Builder::setLogTime, - p -> TimeUtils.parseTimeFieldToInstant(p, LOG_TIME_FIELD.getPreferredName()), LOG_TIME_FIELD, ValueType.VALUE); - parser.declareField(Builder::setTimestamp, - p -> TimeUtils.parseTimeFieldToInstant(p, TIMESTAMP_FIELD.getPreferredName()), TIMESTAMP_FIELD, ValueType.VALUE); + parser.declareField( + Builder::setCategorizationStatus, + p -> CategorizationStatus.fromString(p.text()), + CATEGORIZATION_STATUS_FIELD, + ValueType.STRING + ); + parser.declareField( + Builder::setLogTime, + p -> TimeUtils.parseTimeFieldToInstant(p, LOG_TIME_FIELD.getPreferredName()), + LOG_TIME_FIELD, + ValueType.VALUE + ); + parser.declareField( + Builder::setTimestamp, + p -> TimeUtils.parseTimeFieldToInstant(p, TIMESTAMP_FIELD.getPreferredName()), + TIMESTAMP_FIELD, + ValueType.VALUE + ); return parser; } @@ -89,10 +104,20 @@ private static ConstructingObjectParser createParser(boolean igno private final Instant timestamp; private final Instant logTime; - private CategorizerStats(String jobId, @Nullable String partitionFieldName, @Nullable String partitionFieldValue, - long categorizedDocCount, long totalCategoryCount, long frequentCategoryCount, long rareCategoryCount, - long deadCategoryCount, long failedCategoryCount, CategorizationStatus categorizationStatus, Instant timestamp, - Instant logTime) { + private CategorizerStats( + String jobId, + @Nullable String partitionFieldName, + @Nullable String partitionFieldValue, + long categorizedDocCount, + long totalCategoryCount, + long frequentCategoryCount, + long rareCategoryCount, + long deadCategoryCount, + long failedCategoryCount, + CategorizationStatus categorizationStatus, + Instant timestamp, + Instant logTime + ) { this.jobId = Objects.requireNonNull(jobId); this.partitionFieldName = partitionFieldName; this.partitionFieldValue = partitionFieldValue; @@ -234,8 +259,20 @@ public Instant getLogTime() { @Override public int hashCode() { - return Objects.hash(jobId, partitionFieldName, partitionFieldValue, categorizedDocCount, totalCategoryCount, - frequentCategoryCount, rareCategoryCount, deadCategoryCount, failedCategoryCount, categorizationStatus, timestamp, logTime); + return Objects.hash( + jobId, + partitionFieldName, + partitionFieldValue, + categorizedDocCount, + totalCategoryCount, + frequentCategoryCount, + rareCategoryCount, + deadCategoryCount, + failedCategoryCount, + categorizationStatus, + timestamp, + logTime + ); } /** @@ -342,8 +379,11 @@ public Builder setFailedCategoryCount(long failedCategoryCount) { } public Builder setCategorizationStatus(CategorizationStatus categorizationStatus) { - this.categorizationStatus = Objects.requireNonNull(categorizationStatus, - "[" + CATEGORIZATION_STATUS_FIELD.getPreferredName() + "] must not be null");; + this.categorizationStatus = Objects.requireNonNull( + categorizationStatus, + "[" + CATEGORIZATION_STATUS_FIELD.getPreferredName() + "] must not be null" + ); + ; return this; } @@ -358,9 +398,20 @@ public Builder setLogTime(Instant logTime) { } public CategorizerStats build() { - return new CategorizerStats(jobId, partitionFieldName, partitionFieldValue, categorizedDocCount, totalCategoryCount, - frequentCategoryCount, rareCategoryCount, deadCategoryCount, failedCategoryCount, categorizationStatus, timestamp, - logTime); + return new CategorizerStats( + jobId, + partitionFieldName, + partitionFieldValue, + categorizedDocCount, + totalCategoryCount, + frequentCategoryCount, + rareCategoryCount, + deadCategoryCount, + failedCategoryCount, + categorizationStatus, + timestamp, + logTime + ); } } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/DataCounts.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/DataCounts.java index 7a34a90f6165c..60ef264417c9c 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/DataCounts.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/DataCounts.java @@ -6,12 +6,12 @@ */ package org.elasticsearch.xpack.core.ml.job.process.autodetect.state; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.common.time.TimeUtils; @@ -59,10 +59,29 @@ public class DataCounts implements ToXContentObject, Writeable { public static final ParseField TYPE = new ParseField("data_counts"); - public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("data_counts", true, - a -> new DataCounts((String) a[0], (long) a[1], (long) a[2], (long) a[3], (long) a[4], (long) a[5], (long) a[6], - (long) a[7], (long) a[8], (long) a[9], (long) a[10], (Date) a[11], (Date) a[12], (Date) a[13], (Date) a[14], - (Date) a[15], (Instant) a[16])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "data_counts", + true, + a -> new DataCounts( + (String) a[0], + (long) a[1], + (long) a[2], + (long) a[3], + (long) a[4], + (long) a[5], + (long) a[6], + (long) a[7], + (long) a[8], + (long) a[9], + (long) a[10], + (Date) a[11], + (Date) a[12], + (Date) a[13], + (Date) a[14], + (Date) a[15], + (Instant) a[16] + ) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), Job.ID); @@ -76,16 +95,36 @@ public class DataCounts implements ToXContentObject, Writeable { PARSER.declareLong(ConstructingObjectParser.constructorArg(), EMPTY_BUCKET_COUNT); PARSER.declareLong(ConstructingObjectParser.constructorArg(), SPARSE_BUCKET_COUNT); PARSER.declareLong(ConstructingObjectParser.constructorArg(), BUCKET_COUNT); - PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(), - p -> TimeUtils.parseTimeField(p, EARLIEST_RECORD_TIME.getPreferredName()), EARLIEST_RECORD_TIME, ValueType.VALUE); - PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(), - p -> TimeUtils.parseTimeField(p, LATEST_RECORD_TIME.getPreferredName()), LATEST_RECORD_TIME, ValueType.VALUE); - PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(), - p -> TimeUtils.parseTimeField(p, LAST_DATA_TIME.getPreferredName()), LAST_DATA_TIME, ValueType.VALUE); - PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(), - p -> TimeUtils.parseTimeField(p, LATEST_EMPTY_BUCKET_TIME.getPreferredName()), LATEST_EMPTY_BUCKET_TIME, ValueType.VALUE); - PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(), - p -> TimeUtils.parseTimeField(p, LATEST_SPARSE_BUCKET_TIME.getPreferredName()), LATEST_SPARSE_BUCKET_TIME, ValueType.VALUE); + PARSER.declareField( + ConstructingObjectParser.optionalConstructorArg(), + p -> TimeUtils.parseTimeField(p, EARLIEST_RECORD_TIME.getPreferredName()), + EARLIEST_RECORD_TIME, + ValueType.VALUE + ); + PARSER.declareField( + ConstructingObjectParser.optionalConstructorArg(), + p -> TimeUtils.parseTimeField(p, LATEST_RECORD_TIME.getPreferredName()), + LATEST_RECORD_TIME, + ValueType.VALUE + ); + PARSER.declareField( + ConstructingObjectParser.optionalConstructorArg(), + p -> TimeUtils.parseTimeField(p, LAST_DATA_TIME.getPreferredName()), + LAST_DATA_TIME, + ValueType.VALUE + ); + PARSER.declareField( + ConstructingObjectParser.optionalConstructorArg(), + p -> TimeUtils.parseTimeField(p, LATEST_EMPTY_BUCKET_TIME.getPreferredName()), + LATEST_EMPTY_BUCKET_TIME, + ValueType.VALUE + ); + PARSER.declareField( + ConstructingObjectParser.optionalConstructorArg(), + p -> TimeUtils.parseTimeField(p, LATEST_SPARSE_BUCKET_TIME.getPreferredName()), + LATEST_SPARSE_BUCKET_TIME, + ValueType.VALUE + ); PARSER.declareLong((t, u) -> {/* intentionally empty */}, INPUT_RECORD_COUNT); PARSER.declareField( ConstructingObjectParser.optionalConstructorArg(), @@ -122,11 +161,25 @@ public static String v54DocumentId(String jobId) { private Date latestSparseBucketTimeStamp; private Instant logTime; - public DataCounts(String jobId, long processedRecordCount, long processedFieldCount, long inputBytes, - long inputFieldCount, long invalidDateCount, long missingFieldCount, long outOfOrderTimeStampCount, - long emptyBucketCount, long sparseBucketCount, long bucketCount, - Date earliestRecordTimeStamp, Date latestRecordTimeStamp, Date lastDataTimeStamp, - Date latestEmptyBucketTimeStamp, Date latestSparseBucketTimeStamp, Instant logTime) { + public DataCounts( + String jobId, + long processedRecordCount, + long processedFieldCount, + long inputBytes, + long inputFieldCount, + long invalidDateCount, + long missingFieldCount, + long outOfOrderTimeStampCount, + long emptyBucketCount, + long sparseBucketCount, + long bucketCount, + Date earliestRecordTimeStamp, + Date latestRecordTimeStamp, + Date lastDataTimeStamp, + Date latestEmptyBucketTimeStamp, + Date latestSparseBucketTimeStamp, + Instant logTime + ) { this.jobId = jobId; this.processedRecordCount = processedRecordCount; this.processedFieldCount = processedFieldCount; @@ -233,9 +286,7 @@ public long getProcessedFieldCount() { } public void calcProcessedFieldCount(long analysisFieldsPerRecord) { - processedFieldCount = - (processedRecordCount * analysisFieldsPerRecord) - - missingFieldCount; + processedFieldCount = (processedRecordCount * analysisFieldsPerRecord) - missingFieldCount; // processedFieldCount could be a -ve value if no // records have been written in which case it should be 0 @@ -252,8 +303,7 @@ public void calcProcessedFieldCount(long analysisFieldsPerRecord) { * @return Total number of input records read {@code long} */ public long getInputRecordCount() { - return processedRecordCount + outOfOrderTimeStampCount - + invalidDateCount; + return processedRecordCount + outOfOrderTimeStampCount + invalidDateCount; } /** @@ -300,7 +350,6 @@ public void incrementInvalidDateCount(long additional) { invalidDateCount += additional; } - /** * The number of missing fields that had been * configured for analysis. @@ -370,6 +419,7 @@ public long getBucketCount() { public void incrementBucketCount(long additional) { bucketCount += additional; } + /** * The time of the first record seen. * @@ -393,7 +443,6 @@ public void setEarliestRecordTimeStamp(Date timeStamp) { earliestRecordTimeStamp = timeStamp; } - /** * The time of the latest record seen. * @@ -434,9 +483,8 @@ public void setLatestEmptyBucketTimeStamp(Date latestEmptyBucketTimeStamp) { } public void updateLatestEmptyBucketTimeStamp(Date latestEmptyBucketTimeStamp) { - if (latestEmptyBucketTimeStamp != null && - (this.latestEmptyBucketTimeStamp == null || - latestEmptyBucketTimeStamp.after(this.latestEmptyBucketTimeStamp))) { + if (latestEmptyBucketTimeStamp != null + && (this.latestEmptyBucketTimeStamp == null || latestEmptyBucketTimeStamp.after(this.latestEmptyBucketTimeStamp))) { this.latestEmptyBucketTimeStamp = latestEmptyBucketTimeStamp; } } @@ -455,9 +503,8 @@ public void setLatestSparseBucketTimeStamp(Date latestSparseBucketTimeStamp) { } public void updateLatestSparseBucketTimeStamp(Date latestSparseBucketTimeStamp) { - if (latestSparseBucketTimeStamp != null && - (this.latestSparseBucketTimeStamp == null || - latestSparseBucketTimeStamp.after(this.latestSparseBucketTimeStamp))) { + if (latestSparseBucketTimeStamp != null + && (this.latestSparseBucketTimeStamp == null || latestSparseBucketTimeStamp.after(this.latestSparseBucketTimeStamp))) { this.latestSparseBucketTimeStamp = latestSparseBucketTimeStamp; } } @@ -538,24 +585,39 @@ public XContentBuilder doXContentBody(XContentBuilder builder, Params params) th builder.field(SPARSE_BUCKET_COUNT.getPreferredName(), sparseBucketCount); builder.field(BUCKET_COUNT.getPreferredName(), bucketCount); if (earliestRecordTimeStamp != null) { - builder.timeField(EARLIEST_RECORD_TIME.getPreferredName(), EARLIEST_RECORD_TIME.getPreferredName() + "_string", - earliestRecordTimeStamp.getTime()); + builder.timeField( + EARLIEST_RECORD_TIME.getPreferredName(), + EARLIEST_RECORD_TIME.getPreferredName() + "_string", + earliestRecordTimeStamp.getTime() + ); } if (latestRecordTimeStamp != null) { - builder.timeField(LATEST_RECORD_TIME.getPreferredName(), LATEST_RECORD_TIME.getPreferredName() + "_string", - latestRecordTimeStamp.getTime()); + builder.timeField( + LATEST_RECORD_TIME.getPreferredName(), + LATEST_RECORD_TIME.getPreferredName() + "_string", + latestRecordTimeStamp.getTime() + ); } if (lastDataTimeStamp != null) { - builder.timeField(LAST_DATA_TIME.getPreferredName(), LAST_DATA_TIME.getPreferredName() + "_string", - lastDataTimeStamp.getTime()); + builder.timeField( + LAST_DATA_TIME.getPreferredName(), + LAST_DATA_TIME.getPreferredName() + "_string", + lastDataTimeStamp.getTime() + ); } if (latestEmptyBucketTimeStamp != null) { - builder.timeField(LATEST_EMPTY_BUCKET_TIME.getPreferredName(), LATEST_EMPTY_BUCKET_TIME.getPreferredName() + "_string", - latestEmptyBucketTimeStamp.getTime()); + builder.timeField( + LATEST_EMPTY_BUCKET_TIME.getPreferredName(), + LATEST_EMPTY_BUCKET_TIME.getPreferredName() + "_string", + latestEmptyBucketTimeStamp.getTime() + ); } if (latestSparseBucketTimeStamp != null) { - builder.timeField(LATEST_SPARSE_BUCKET_TIME.getPreferredName(), LATEST_SPARSE_BUCKET_TIME.getPreferredName() + "_string", - latestSparseBucketTimeStamp.getTime()); + builder.timeField( + LATEST_SPARSE_BUCKET_TIME.getPreferredName(), + LATEST_SPARSE_BUCKET_TIME.getPreferredName() + "_string", + latestSparseBucketTimeStamp.getTime() + ); } builder.field(INPUT_RECORD_COUNT.getPreferredName(), getInputRecordCount()); if (logTime != null) { @@ -580,31 +642,46 @@ public boolean equals(Object other) { DataCounts that = (DataCounts) other; - return Objects.equals(this.jobId, that.jobId) && - this.processedRecordCount == that.processedRecordCount && - this.processedFieldCount == that.processedFieldCount && - this.inputBytes == that.inputBytes && - this.inputFieldCount == that.inputFieldCount && - this.invalidDateCount == that.invalidDateCount && - this.missingFieldCount == that.missingFieldCount && - this.outOfOrderTimeStampCount == that.outOfOrderTimeStampCount && - this.emptyBucketCount == that.emptyBucketCount && - this.sparseBucketCount == that.sparseBucketCount && - this.bucketCount == that.bucketCount && - Objects.equals(this.latestRecordTimeStamp, that.latestRecordTimeStamp) && - Objects.equals(this.earliestRecordTimeStamp, that.earliestRecordTimeStamp) && - Objects.equals(this.lastDataTimeStamp, that.lastDataTimeStamp) && - Objects.equals(this.latestEmptyBucketTimeStamp, that.latestEmptyBucketTimeStamp) && - Objects.equals(this.latestSparseBucketTimeStamp, that.latestSparseBucketTimeStamp) && - Objects.equals(this.logTime, that.logTime); + return Objects.equals(this.jobId, that.jobId) + && this.processedRecordCount == that.processedRecordCount + && this.processedFieldCount == that.processedFieldCount + && this.inputBytes == that.inputBytes + && this.inputFieldCount == that.inputFieldCount + && this.invalidDateCount == that.invalidDateCount + && this.missingFieldCount == that.missingFieldCount + && this.outOfOrderTimeStampCount == that.outOfOrderTimeStampCount + && this.emptyBucketCount == that.emptyBucketCount + && this.sparseBucketCount == that.sparseBucketCount + && this.bucketCount == that.bucketCount + && Objects.equals(this.latestRecordTimeStamp, that.latestRecordTimeStamp) + && Objects.equals(this.earliestRecordTimeStamp, that.earliestRecordTimeStamp) + && Objects.equals(this.lastDataTimeStamp, that.lastDataTimeStamp) + && Objects.equals(this.latestEmptyBucketTimeStamp, that.latestEmptyBucketTimeStamp) + && Objects.equals(this.latestSparseBucketTimeStamp, that.latestSparseBucketTimeStamp) + && Objects.equals(this.logTime, that.logTime); } @Override public int hashCode() { - return Objects.hash(jobId, processedRecordCount, processedFieldCount, - inputBytes, inputFieldCount, invalidDateCount, missingFieldCount, - outOfOrderTimeStampCount, lastDataTimeStamp, emptyBucketCount, sparseBucketCount, bucketCount, - latestRecordTimeStamp, earliestRecordTimeStamp, latestEmptyBucketTimeStamp, latestSparseBucketTimeStamp, logTime); + return Objects.hash( + jobId, + processedRecordCount, + processedFieldCount, + inputBytes, + inputFieldCount, + invalidDateCount, + missingFieldCount, + outOfOrderTimeStampCount, + lastDataTimeStamp, + emptyBucketCount, + sparseBucketCount, + bucketCount, + latestRecordTimeStamp, + earliestRecordTimeStamp, + latestEmptyBucketTimeStamp, + latestSparseBucketTimeStamp, + logTime + ); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/ModelSizeStats.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/ModelSizeStats.java index 8192af2db0c73..3812c012e2a3d 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/ModelSizeStats.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/ModelSizeStats.java @@ -6,18 +6,18 @@ */ package org.elasticsearch.xpack.core.ml.job.process.autodetect.state; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xpack.core.common.time.TimeUtils; import org.elasticsearch.xpack.core.ml.job.config.Job; import org.elasticsearch.xpack.core.ml.job.results.Result; -import org.elasticsearch.xpack.core.common.time.TimeUtils; import java.io.IOException; import java.util.Date; @@ -62,8 +62,11 @@ public class ModelSizeStats implements ToXContentObject, Writeable { public static final ConstructingObjectParser LENIENT_PARSER = createParser(true); private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { - ConstructingObjectParser parser = new ConstructingObjectParser<>(RESULT_TYPE_FIELD.getPreferredName(), - ignoreUnknownFields, a -> new Builder((String) a[0])); + ConstructingObjectParser parser = new ConstructingObjectParser<>( + RESULT_TYPE_FIELD.getPreferredName(), + ignoreUnknownFields, + a -> new Builder((String) a[0]) + ); parser.declareString(ConstructingObjectParser.constructorArg(), Job.ID); parser.declareString((modelSizeStat, s) -> {}, Result.RESULT_TYPE); @@ -76,20 +79,36 @@ private static ConstructingObjectParser createParser(boolean igno parser.declareLong(Builder::setTotalOverFieldCount, TOTAL_OVER_FIELD_COUNT_FIELD); parser.declareLong(Builder::setTotalPartitionFieldCount, TOTAL_PARTITION_FIELD_COUNT_FIELD); parser.declareField(Builder::setMemoryStatus, p -> MemoryStatus.fromString(p.text()), MEMORY_STATUS_FIELD, ValueType.STRING); - parser.declareField(Builder::setAssignmentMemoryBasis, - p -> AssignmentMemoryBasis.fromString(p.text()), ASSIGNMENT_MEMORY_BASIS_FIELD, ValueType.STRING); + parser.declareField( + Builder::setAssignmentMemoryBasis, + p -> AssignmentMemoryBasis.fromString(p.text()), + ASSIGNMENT_MEMORY_BASIS_FIELD, + ValueType.STRING + ); parser.declareLong(Builder::setCategorizedDocCount, CATEGORIZED_DOC_COUNT_FIELD); parser.declareLong(Builder::setTotalCategoryCount, TOTAL_CATEGORY_COUNT_FIELD); parser.declareLong(Builder::setFrequentCategoryCount, FREQUENT_CATEGORY_COUNT_FIELD); parser.declareLong(Builder::setRareCategoryCount, RARE_CATEGORY_COUNT_FIELD); parser.declareLong(Builder::setDeadCategoryCount, DEAD_CATEGORY_COUNT_FIELD); parser.declareLong(Builder::setFailedCategoryCount, FAILED_CATEGORY_COUNT_FIELD); - parser.declareField(Builder::setCategorizationStatus, - p -> CategorizationStatus.fromString(p.text()), CATEGORIZATION_STATUS_FIELD, ValueType.STRING); - parser.declareField(Builder::setLogTime, - p -> TimeUtils.parseTimeField(p, LOG_TIME_FIELD.getPreferredName()), LOG_TIME_FIELD, ValueType.VALUE); - parser.declareField(Builder::setTimestamp, - p -> TimeUtils.parseTimeField(p, TIMESTAMP_FIELD.getPreferredName()), TIMESTAMP_FIELD, ValueType.VALUE); + parser.declareField( + Builder::setCategorizationStatus, + p -> CategorizationStatus.fromString(p.text()), + CATEGORIZATION_STATUS_FIELD, + ValueType.STRING + ); + parser.declareField( + Builder::setLogTime, + p -> TimeUtils.parseTimeField(p, LOG_TIME_FIELD.getPreferredName()), + LOG_TIME_FIELD, + ValueType.VALUE + ); + parser.declareField( + Builder::setTimestamp, + p -> TimeUtils.parseTimeField(p, TIMESTAMP_FIELD.getPreferredName()), + TIMESTAMP_FIELD, + ValueType.VALUE + ); return parser; } @@ -101,7 +120,9 @@ private static ConstructingObjectParser createParser(boolean igno * been dropped */ public enum MemoryStatus implements Writeable { - OK, SOFT_LIMIT, HARD_LIMIT; + OK, + SOFT_LIMIT, + HARD_LIMIT; public static MemoryStatus fromString(String statusName) { return valueOf(statusName.trim().toUpperCase(Locale.ROOT)); @@ -133,7 +154,9 @@ public String toString() { * to 7.11. */ public enum AssignmentMemoryBasis implements Writeable { - MODEL_MEMORY_LIMIT, CURRENT_MODEL_BYTES, PEAK_MODEL_BYTES; + MODEL_MEMORY_LIMIT, + CURRENT_MODEL_BYTES, + PEAK_MODEL_BYTES; public static AssignmentMemoryBasis fromString(String statusName) { return valueOf(statusName.trim().toUpperCase(Locale.ROOT)); @@ -175,12 +198,28 @@ public String toString() { private final Date timestamp; private final Date logTime; - private ModelSizeStats(String jobId, long modelBytes, Long peakModelBytes, Long modelBytesExceeded, Long modelBytesMemoryLimit, - long totalByFieldCount, long totalOverFieldCount, long totalPartitionFieldCount, - long bucketAllocationFailuresCount, MemoryStatus memoryStatus, - AssignmentMemoryBasis assignmentMemoryBasis, long categorizedDocCount, long totalCategoryCount, - long frequentCategoryCount, long rareCategoryCount, long deadCategoryCount, long failedCategoryCount, - CategorizationStatus categorizationStatus, Date timestamp, Date logTime) { + private ModelSizeStats( + String jobId, + long modelBytes, + Long peakModelBytes, + Long modelBytesExceeded, + Long modelBytesMemoryLimit, + long totalByFieldCount, + long totalOverFieldCount, + long totalPartitionFieldCount, + long bucketAllocationFailuresCount, + MemoryStatus memoryStatus, + AssignmentMemoryBasis assignmentMemoryBasis, + long categorizedDocCount, + long totalCategoryCount, + long frequentCategoryCount, + long rareCategoryCount, + long deadCategoryCount, + long failedCategoryCount, + CategorizationStatus categorizationStatus, + Date timestamp, + Date logTime + ) { this.jobId = jobId; this.modelBytes = modelBytes; this.peakModelBytes = peakModelBytes; @@ -408,10 +447,27 @@ public Date getLogTime() { public int hashCode() { // this.id excluded here as it is generated by the datastore return Objects.hash( - jobId, modelBytes, peakModelBytes, modelBytesExceeded, modelBytesMemoryLimit, totalByFieldCount, totalOverFieldCount, - totalPartitionFieldCount, bucketAllocationFailuresCount, memoryStatus, assignmentMemoryBasis, categorizedDocCount, - totalCategoryCount, frequentCategoryCount, rareCategoryCount, deadCategoryCount, failedCategoryCount, categorizationStatus, - timestamp, logTime); + jobId, + modelBytes, + peakModelBytes, + modelBytesExceeded, + modelBytesMemoryLimit, + totalByFieldCount, + totalOverFieldCount, + totalPartitionFieldCount, + bucketAllocationFailuresCount, + memoryStatus, + assignmentMemoryBasis, + categorizedDocCount, + totalCategoryCount, + frequentCategoryCount, + rareCategoryCount, + deadCategoryCount, + failedCategoryCount, + categorizationStatus, + timestamp, + logTime + ); } /** @@ -430,24 +486,25 @@ public boolean equals(Object other) { ModelSizeStats that = (ModelSizeStats) other; return this.modelBytes == that.modelBytes - && Objects.equals(this.peakModelBytes, that.peakModelBytes) - && Objects.equals(this.modelBytesExceeded, that.modelBytesExceeded) - && Objects.equals(this.modelBytesMemoryLimit, that.modelBytesMemoryLimit) - && this.totalByFieldCount == that.totalByFieldCount - && this.totalOverFieldCount == that.totalOverFieldCount && this.totalPartitionFieldCount == that.totalPartitionFieldCount - && this.bucketAllocationFailuresCount == that.bucketAllocationFailuresCount - && Objects.equals(this.memoryStatus, that.memoryStatus) - && Objects.equals(this.assignmentMemoryBasis, that.assignmentMemoryBasis) - && Objects.equals(this.categorizedDocCount, that.categorizedDocCount) - && Objects.equals(this.totalCategoryCount, that.totalCategoryCount) - && Objects.equals(this.frequentCategoryCount, that.frequentCategoryCount) - && Objects.equals(this.rareCategoryCount, that.rareCategoryCount) - && Objects.equals(this.deadCategoryCount, that.deadCategoryCount) - && Objects.equals(this.failedCategoryCount, that.failedCategoryCount) - && Objects.equals(this.categorizationStatus, that.categorizationStatus) - && Objects.equals(this.timestamp, that.timestamp) - && Objects.equals(this.logTime, that.logTime) - && Objects.equals(this.jobId, that.jobId); + && Objects.equals(this.peakModelBytes, that.peakModelBytes) + && Objects.equals(this.modelBytesExceeded, that.modelBytesExceeded) + && Objects.equals(this.modelBytesMemoryLimit, that.modelBytesMemoryLimit) + && this.totalByFieldCount == that.totalByFieldCount + && this.totalOverFieldCount == that.totalOverFieldCount + && this.totalPartitionFieldCount == that.totalPartitionFieldCount + && this.bucketAllocationFailuresCount == that.bucketAllocationFailuresCount + && Objects.equals(this.memoryStatus, that.memoryStatus) + && Objects.equals(this.assignmentMemoryBasis, that.assignmentMemoryBasis) + && Objects.equals(this.categorizedDocCount, that.categorizedDocCount) + && Objects.equals(this.totalCategoryCount, that.totalCategoryCount) + && Objects.equals(this.frequentCategoryCount, that.frequentCategoryCount) + && Objects.equals(this.rareCategoryCount, that.rareCategoryCount) + && Objects.equals(this.deadCategoryCount, that.deadCategoryCount) + && Objects.equals(this.failedCategoryCount, that.failedCategoryCount) + && Objects.equals(this.categorizationStatus, that.categorizationStatus) + && Objects.equals(this.timestamp, that.timestamp) + && Objects.equals(this.logTime, that.logTime) + && Objects.equals(this.jobId, that.jobId); } public static class Builder { @@ -602,10 +659,27 @@ public Builder setLogTime(Date logTime) { public ModelSizeStats build() { return new ModelSizeStats( - jobId, modelBytes, peakModelBytes, modelBytesExceeded, modelBytesMemoryLimit, totalByFieldCount, totalOverFieldCount, - totalPartitionFieldCount, bucketAllocationFailuresCount, memoryStatus, assignmentMemoryBasis, categorizedDocCount, - totalCategoryCount, frequentCategoryCount, rareCategoryCount, deadCategoryCount, failedCategoryCount, categorizationStatus, - timestamp, logTime); + jobId, + modelBytes, + peakModelBytes, + modelBytesExceeded, + modelBytesMemoryLimit, + totalByFieldCount, + totalOverFieldCount, + totalPartitionFieldCount, + bucketAllocationFailuresCount, + memoryStatus, + assignmentMemoryBasis, + categorizedDocCount, + totalCategoryCount, + frequentCategoryCount, + rareCategoryCount, + deadCategoryCount, + failedCategoryCount, + categorizationStatus, + timestamp, + logTime + ); } } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/ModelSnapshot.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/ModelSnapshot.java index de1c9963b60a4..10cfa3d7a6d64 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/ModelSnapshot.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/ModelSnapshot.java @@ -8,7 +8,6 @@ import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.Version; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; @@ -17,6 +16,7 @@ import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ObjectParser; import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; @@ -64,17 +64,32 @@ private static ObjectParser createParser(boolean ignoreUnknownFie parser.declareString(Builder::setJobId, Job.ID); parser.declareString(Builder::setMinVersion, MIN_VERSION); - parser.declareField(Builder::setTimestamp, - p -> TimeUtils.parseTimeField(p, TIMESTAMP.getPreferredName()), TIMESTAMP, ValueType.VALUE); + parser.declareField( + Builder::setTimestamp, + p -> TimeUtils.parseTimeField(p, TIMESTAMP.getPreferredName()), + TIMESTAMP, + ValueType.VALUE + ); parser.declareString(Builder::setDescription, DESCRIPTION); parser.declareString(Builder::setSnapshotId, ModelSnapshotField.SNAPSHOT_ID); parser.declareInt(Builder::setSnapshotDocCount, SNAPSHOT_DOC_COUNT); - parser.declareObject(Builder::setModelSizeStats, ignoreUnknownFields ? ModelSizeStats.LENIENT_PARSER : ModelSizeStats.STRICT_PARSER, - ModelSizeStats.RESULT_TYPE_FIELD); - parser.declareField(Builder::setLatestRecordTimeStamp, - p -> TimeUtils.parseTimeField(p, LATEST_RECORD_TIME.getPreferredName()), LATEST_RECORD_TIME, ValueType.VALUE); - parser.declareField(Builder::setLatestResultTimeStamp, - p -> TimeUtils.parseTimeField(p, LATEST_RESULT_TIME.getPreferredName()), LATEST_RESULT_TIME, ValueType.VALUE); + parser.declareObject( + Builder::setModelSizeStats, + ignoreUnknownFields ? ModelSizeStats.LENIENT_PARSER : ModelSizeStats.STRICT_PARSER, + ModelSizeStats.RESULT_TYPE_FIELD + ); + parser.declareField( + Builder::setLatestRecordTimeStamp, + p -> TimeUtils.parseTimeField(p, LATEST_RECORD_TIME.getPreferredName()), + LATEST_RECORD_TIME, + ValueType.VALUE + ); + parser.declareField( + Builder::setLatestResultTimeStamp, + p -> TimeUtils.parseTimeField(p, LATEST_RESULT_TIME.getPreferredName()), + LATEST_RESULT_TIME, + ValueType.VALUE + ); parser.declareObject(Builder::setQuantiles, ignoreUnknownFields ? Quantiles.LENIENT_PARSER : Quantiles.STRICT_PARSER, QUANTILES); parser.declareBoolean(Builder::setRetain, RETAIN); @@ -105,10 +120,19 @@ private static ObjectParser createParser(boolean ignoreUnknownFie private final Quantiles quantiles; private final boolean retain; - - private ModelSnapshot(String jobId, Version minVersion, Date timestamp, String description, String snapshotId, int snapshotDocCount, - ModelSizeStats modelSizeStats, Date latestRecordTimeStamp, Date latestResultTimeStamp, Quantiles quantiles, - boolean retain) { + private ModelSnapshot( + String jobId, + Version minVersion, + Date timestamp, + String description, + String snapshotId, + int snapshotDocCount, + ModelSizeStats modelSizeStats, + Date latestRecordTimeStamp, + Date latestResultTimeStamp, + Quantiles quantiles, + boolean retain + ) { this.jobId = jobId; this.minVersion = minVersion; this.timestamp = timestamp; @@ -185,12 +209,18 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.field(ModelSizeStats.RESULT_TYPE_FIELD.getPreferredName(), modelSizeStats); } if (latestRecordTimeStamp != null) { - builder.timeField(LATEST_RECORD_TIME.getPreferredName(), LATEST_RECORD_TIME.getPreferredName() + "_string", - latestRecordTimeStamp.getTime()); + builder.timeField( + LATEST_RECORD_TIME.getPreferredName(), + LATEST_RECORD_TIME.getPreferredName() + "_string", + latestRecordTimeStamp.getTime() + ); } if (latestResultTimeStamp != null) { - builder.timeField(LATEST_RESULT_TIME.getPreferredName(), LATEST_RESULT_TIME.getPreferredName() + "_string", - latestResultTimeStamp.getTime()); + builder.timeField( + LATEST_RESULT_TIME.getPreferredName(), + LATEST_RESULT_TIME.getPreferredName() + "_string", + latestResultTimeStamp.getTime() + ); } if (quantiles != null) { builder.field(QUANTILES.getPreferredName(), quantiles); @@ -246,8 +276,19 @@ public boolean isRetain() { @Override public int hashCode() { - return Objects.hash(jobId, minVersion, timestamp, description, snapshotId, quantiles, snapshotDocCount, modelSizeStats, - latestRecordTimeStamp, latestResultTimeStamp, retain); + return Objects.hash( + jobId, + minVersion, + timestamp, + description, + snapshotId, + quantiles, + snapshotDocCount, + modelSizeStats, + latestRecordTimeStamp, + latestResultTimeStamp, + retain + ); } /** @@ -266,16 +307,16 @@ public boolean equals(Object other) { ModelSnapshot that = (ModelSnapshot) other; return Objects.equals(this.jobId, that.jobId) - && Objects.equals(this.minVersion, that.minVersion) - && Objects.equals(this.timestamp, that.timestamp) - && Objects.equals(this.description, that.description) - && Objects.equals(this.snapshotId, that.snapshotId) - && this.snapshotDocCount == that.snapshotDocCount - && Objects.equals(this.modelSizeStats, that.modelSizeStats) - && Objects.equals(this.quantiles, that.quantiles) - && Objects.equals(this.latestRecordTimeStamp, that.latestRecordTimeStamp) - && Objects.equals(this.latestResultTimeStamp, that.latestResultTimeStamp) - && this.retain == that.retain; + && Objects.equals(this.minVersion, that.minVersion) + && Objects.equals(this.timestamp, that.timestamp) + && Objects.equals(this.description, that.description) + && Objects.equals(this.snapshotId, that.snapshotId) + && this.snapshotDocCount == that.snapshotDocCount + && Objects.equals(this.modelSizeStats, that.modelSizeStats) + && Objects.equals(this.quantiles, that.quantiles) + && Objects.equals(this.latestRecordTimeStamp, that.latestRecordTimeStamp) + && Objects.equals(this.latestResultTimeStamp, that.latestResultTimeStamp) + && this.retain == that.retain; } public List stateDocumentIds() { @@ -326,9 +367,11 @@ public static String v54DocumentId(String jobId, String snapshotId) { } public static ModelSnapshot fromJson(BytesReference bytesReference) { - try (InputStream stream = bytesReference.streamInput(); - XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) { + try ( + InputStream stream = bytesReference.streamInput(); + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream) + ) { return LENIENT_PARSER.apply(parser, null).build(); } catch (IOException e) { throw new ElasticsearchParseException("failed to parse modelSnapshot", e); @@ -352,9 +395,7 @@ public static class Builder { private Quantiles quantiles; private boolean retain; - - public Builder() { - } + public Builder() {} public Builder(String jobId) { this(); @@ -441,13 +482,35 @@ public Builder setRetain(boolean value) { } public ModelSnapshot build() { - return new ModelSnapshot(jobId, minVersion, timestamp, description, snapshotId, snapshotDocCount, modelSizeStats, - latestRecordTimeStamp, latestResultTimeStamp, quantiles, retain); + return new ModelSnapshot( + jobId, + minVersion, + timestamp, + description, + snapshotId, + snapshotDocCount, + modelSizeStats, + latestRecordTimeStamp, + latestResultTimeStamp, + quantiles, + retain + ); } } public static ModelSnapshot emptySnapshot(String jobId) { - return new ModelSnapshot(jobId, Version.CURRENT, new Date(), "empty snapshot", EMPTY_SNAPSHOT_ID, 0, - new ModelSizeStats.Builder(jobId).build(), null, null, null, false); + return new ModelSnapshot( + jobId, + Version.CURRENT, + new Date(), + "empty snapshot", + EMPTY_SNAPSHOT_ID, + 0, + new ModelSizeStats.Builder(jobId).build(), + null, + null, + null, + false + ); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/ModelState.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/ModelState.java index 04901f7928b05..de2f6d1fe7849 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/ModelState.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/ModelState.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.xpack.core.ml.job.process.autodetect.state; - import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -51,7 +50,5 @@ private static String v54ExtractJobId(String docId) { return null; } - private ModelState() { - } + private ModelState() {} } - diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/Quantiles.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/Quantiles.java index 8e78f540390d0..f5f28dbc75886 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/Quantiles.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/Quantiles.java @@ -6,12 +6,12 @@ */ package org.elasticsearch.xpack.core.ml.job.process.autodetect.state; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ml.job.config.Job; @@ -40,8 +40,11 @@ public class Quantiles implements ToXContentObject, Writeable { public static final ConstructingObjectParser LENIENT_PARSER = createParser(true); private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { - ConstructingObjectParser parser = new ConstructingObjectParser<>(TYPE.getPreferredName(), ignoreUnknownFields, - a -> new Quantiles((String) a[0], (Date) a[1], (String) a[2])); + ConstructingObjectParser parser = new ConstructingObjectParser<>( + TYPE.getPreferredName(), + ignoreUnknownFields, + a -> new Quantiles((String) a[0], (Date) a[1], (String) a[2]) + ); parser.declareString(ConstructingObjectParser.constructorArg(), Job.ID); parser.declareField(ConstructingObjectParser.optionalConstructorArg(), p -> new Date(p.longValue()), TIMESTAMP, ValueType.LONG); @@ -145,10 +148,9 @@ public boolean equals(Object other) { Quantiles that = (Quantiles) other; - return Objects.equals(this.jobId, that.jobId) && Objects.equals(this.timestamp, that.timestamp) - && Objects.equals(this.quantileState, that.quantileState); - + return Objects.equals(this.jobId, that.jobId) + && Objects.equals(this.timestamp, that.timestamp) + && Objects.equals(this.quantileState, that.quantileState); } } - diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/TimingStats.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/TimingStats.java index 65a7b221f55e2..538f08592a1e7 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/TimingStats.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/TimingStats.java @@ -6,13 +6,13 @@ */ package org.elasticsearch.xpack.core.ml.job.process.autodetect.state; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ml.job.config.Job; @@ -37,35 +37,38 @@ public class TimingStats implements ToXContentObject, Writeable { public static final ParseField MIN_BUCKET_PROCESSING_TIME_MS = new ParseField("minimum_bucket_processing_time_ms"); public static final ParseField MAX_BUCKET_PROCESSING_TIME_MS = new ParseField("maximum_bucket_processing_time_ms"); public static final ParseField AVG_BUCKET_PROCESSING_TIME_MS = new ParseField("average_bucket_processing_time_ms"); - public static final ParseField EXPONENTIAL_AVG_BUCKET_PROCESSING_TIME_MS = - new ParseField("exponential_average_bucket_processing_time_ms"); + public static final ParseField EXPONENTIAL_AVG_BUCKET_PROCESSING_TIME_MS = new ParseField( + "exponential_average_bucket_processing_time_ms" + ); public static final ParseField EXPONENTIAL_AVG_CALCULATION_CONTEXT = new ParseField("exponential_average_calculation_context"); - public static final ParseField EXPONENTIAL_AVG_BUCKET_PROCESSING_TIME_PER_HOUR_MS = - new ParseField("exponential_average_bucket_processing_time_per_hour_ms"); + public static final ParseField EXPONENTIAL_AVG_BUCKET_PROCESSING_TIME_PER_HOUR_MS = new ParseField( + "exponential_average_bucket_processing_time_per_hour_ms" + ); public static final ParseField TYPE = new ParseField("timing_stats"); - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>( - TYPE.getPreferredName(), - true, - args -> { - String jobId = (String) args[0]; - long bucketCount = (long) args[1]; - Double minBucketProcessingTimeMs = (Double) args[2]; - Double maxBucketProcessingTimeMs = (Double) args[3]; - Double avgBucketProcessingTimeMs = (Double) args[4]; - Double exponentialAvgBucketProcessingTimeMs = (Double) args[5]; - ExponentialAverageCalculationContext exponentialAvgCalculationContext = (ExponentialAverageCalculationContext) args[6]; - return new TimingStats( - jobId, - bucketCount, - minBucketProcessingTimeMs, - maxBucketProcessingTimeMs, - avgBucketProcessingTimeMs, - exponentialAvgBucketProcessingTimeMs, - getOrDefault(exponentialAvgCalculationContext, new ExponentialAverageCalculationContext())); - }); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + TYPE.getPreferredName(), + true, + args -> { + String jobId = (String) args[0]; + long bucketCount = (long) args[1]; + Double minBucketProcessingTimeMs = (Double) args[2]; + Double maxBucketProcessingTimeMs = (Double) args[3]; + Double avgBucketProcessingTimeMs = (Double) args[4]; + Double exponentialAvgBucketProcessingTimeMs = (Double) args[5]; + ExponentialAverageCalculationContext exponentialAvgCalculationContext = (ExponentialAverageCalculationContext) args[6]; + return new TimingStats( + jobId, + bucketCount, + minBucketProcessingTimeMs, + maxBucketProcessingTimeMs, + avgBucketProcessingTimeMs, + exponentialAvgBucketProcessingTimeMs, + getOrDefault(exponentialAvgCalculationContext, new ExponentialAverageCalculationContext()) + ); + } + ); static { PARSER.declareString(constructorArg(), Job.ID); @@ -90,13 +93,14 @@ public static String documentId(String jobId) { private final ExponentialAverageCalculationContext exponentialAvgCalculationContext; public TimingStats( - String jobId, - long bucketCount, - @Nullable Double minBucketProcessingTimeMs, - @Nullable Double maxBucketProcessingTimeMs, - @Nullable Double avgBucketProcessingTimeMs, - @Nullable Double exponentialAvgBucketProcessingTimeMs, - ExponentialAverageCalculationContext exponentialAvgCalculationContext) { + String jobId, + long bucketCount, + @Nullable Double minBucketProcessingTimeMs, + @Nullable Double maxBucketProcessingTimeMs, + @Nullable Double avgBucketProcessingTimeMs, + @Nullable Double exponentialAvgBucketProcessingTimeMs, + ExponentialAverageCalculationContext exponentialAvgCalculationContext + ) { this.jobId = Objects.requireNonNull(jobId); this.bucketCount = bucketCount; this.minBucketProcessingTimeMs = minBucketProcessingTimeMs; @@ -118,7 +122,8 @@ public TimingStats(TimingStats lhs) { lhs.maxBucketProcessingTimeMs, lhs.avgBucketProcessingTimeMs, lhs.exponentialAvgBucketProcessingTimeMs, - new ExponentialAverageCalculationContext(lhs.exponentialAvgCalculationContext)); + new ExponentialAverageCalculationContext(lhs.exponentialAvgCalculationContext) + ); } public TimingStats(StreamInput in) throws IOException { @@ -141,9 +146,7 @@ public long getBucketCount() { /** Calculates total bucket processing time as a product of the all-time average bucket processing time and the number of buckets. */ public double getTotalBucketProcessingTimeMs() { - return avgBucketProcessingTimeMs != null - ? bucketCount * avgBucketProcessingTimeMs - : 0.0; + return avgBucketProcessingTimeMs != null ? bucketCount * avgBucketProcessingTimeMs : 0.0; } public Double getMinBucketProcessingTimeMs() { @@ -281,7 +284,8 @@ public int hashCode() { maxBucketProcessingTimeMs, avgBucketProcessingTimeMs, exponentialAvgBucketProcessingTimeMs, - exponentialAvgCalculationContext); + exponentialAvgCalculationContext + ); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/AnomalyCause.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/AnomalyCause.java index b79212b2087fc..685dc93cf962c 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/AnomalyCause.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/AnomalyCause.java @@ -6,12 +6,12 @@ */ package org.elasticsearch.xpack.core.ml.job.results; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -52,8 +52,11 @@ public class AnomalyCause implements ToXContentObject, Writeable { public static final ObjectParser LENIENT_PARSER = createParser(true); private static ObjectParser createParser(boolean ignoreUnknownFields) { - ObjectParser parser = new ObjectParser<>(ANOMALY_CAUSE.getPreferredName(), ignoreUnknownFields, - AnomalyCause::new); + ObjectParser parser = new ObjectParser<>( + ANOMALY_CAUSE.getPreferredName(), + ignoreUnknownFields, + AnomalyCause::new + ); parser.declareDouble(AnomalyCause::setProbability, PROBABILITY); parser.declareString(AnomalyCause::setByFieldName, BY_FIELD_NAME); @@ -68,11 +71,16 @@ private static ObjectParser createParser(boolean ignoreUnkno parser.declareString(AnomalyCause::setFieldName, FIELD_NAME); parser.declareString(AnomalyCause::setOverFieldName, OVER_FIELD_NAME); parser.declareString(AnomalyCause::setOverFieldValue, OVER_FIELD_VALUE); - parser.declareObjectArray(AnomalyCause::setInfluencers, ignoreUnknownFields ? Influence.LENIENT_PARSER : Influence.STRICT_PARSER, - INFLUENCERS); - parser.declareObject(AnomalyCause::setGeoResults, + parser.declareObjectArray( + AnomalyCause::setInfluencers, + ignoreUnknownFields ? Influence.LENIENT_PARSER : Influence.STRICT_PARSER, + INFLUENCERS + ); + parser.declareObject( + AnomalyCause::setGeoResults, ignoreUnknownFields ? GeoResults.LENIENT_PARSER : GeoResults.STRICT_PARSER, - GEO_RESULTS); + GEO_RESULTS + ); return parser; } @@ -96,8 +104,7 @@ private static ObjectParser createParser(boolean ignoreUnkno private List influencers; - public AnomalyCause() { - } + public AnomalyCause() {} @SuppressWarnings("unchecked") public AnomalyCause(StreamInput in) throws IOException { @@ -213,7 +220,6 @@ public void setProbability(double value) { probability = value; } - public String getByFieldName() { return byFieldName; } @@ -328,21 +334,23 @@ public void setGeoResults(GeoResults geoResults) { @Override public int hashCode() { - return Objects.hash(probability, - actual, - typical, - byFieldName, - byFieldValue, - correlatedByFieldValue, - fieldName, - function, - functionDescription, - overFieldName, - overFieldValue, - partitionFieldName, - partitionFieldValue, - influencers, - geoResults); + return Objects.hash( + probability, + actual, + typical, + byFieldName, + byFieldValue, + correlatedByFieldValue, + fieldName, + function, + functionDescription, + overFieldName, + overFieldValue, + partitionFieldName, + partitionFieldValue, + influencers, + geoResults + ); } @Override @@ -355,23 +363,23 @@ public boolean equals(Object other) { return false; } - AnomalyCause that = (AnomalyCause)other; - - return this.probability == that.probability && - Objects.deepEquals(this.typical, that.typical) && - Objects.deepEquals(this.actual, that.actual) && - Objects.equals(this.function, that.function) && - Objects.equals(this.functionDescription, that.functionDescription) && - Objects.equals(this.fieldName, that.fieldName) && - Objects.equals(this.byFieldName, that.byFieldName) && - Objects.equals(this.byFieldValue, that.byFieldValue) && - Objects.equals(this.correlatedByFieldValue, that.correlatedByFieldValue) && - Objects.equals(this.partitionFieldName, that.partitionFieldName) && - Objects.equals(this.partitionFieldValue, that.partitionFieldValue) && - Objects.equals(this.overFieldName, that.overFieldName) && - Objects.equals(this.overFieldValue, that.overFieldValue) && - Objects.equals(this.geoResults, that.geoResults) && - Objects.equals(this.influencers, that.influencers); + AnomalyCause that = (AnomalyCause) other; + + return this.probability == that.probability + && Objects.deepEquals(this.typical, that.typical) + && Objects.deepEquals(this.actual, that.actual) + && Objects.equals(this.function, that.function) + && Objects.equals(this.functionDescription, that.functionDescription) + && Objects.equals(this.fieldName, that.fieldName) + && Objects.equals(this.byFieldName, that.byFieldName) + && Objects.equals(this.byFieldValue, that.byFieldValue) + && Objects.equals(this.correlatedByFieldValue, that.correlatedByFieldValue) + && Objects.equals(this.partitionFieldName, that.partitionFieldName) + && Objects.equals(this.partitionFieldValue, that.partitionFieldValue) + && Objects.equals(this.overFieldName, that.overFieldName) + && Objects.equals(this.overFieldValue, that.overFieldValue) + && Objects.equals(this.geoResults, that.geoResults) + && Objects.equals(this.influencers, that.influencers); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/AnomalyRecord.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/AnomalyRecord.java index 8d98d255e7843..c00941e5e178e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/AnomalyRecord.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/AnomalyRecord.java @@ -6,20 +6,20 @@ */ package org.elasticsearch.xpack.core.ml.job.results; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xpack.core.common.time.TimeUtils; import org.elasticsearch.xpack.core.ml.MachineLearningField; import org.elasticsearch.xpack.core.ml.job.config.Detector; import org.elasticsearch.xpack.core.ml.job.config.Job; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; -import org.elasticsearch.xpack.core.common.time.TimeUtils; import java.io.IOException; import java.util.ArrayList; @@ -83,16 +83,22 @@ public class AnomalyRecord implements ToXContentObject, Writeable { public static final ConstructingObjectParser STRICT_PARSER = createParser(false); public static final ConstructingObjectParser LENIENT_PARSER = createParser(true); - private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { // As a record contains fields named after the data fields, the parser for the record should always ignore unknown fields. // However, it makes sense to offer strict/lenient parsing for other members, e.g. influences, anomaly causes, etc. - ConstructingObjectParser parser = new ConstructingObjectParser<>(RESULT_TYPE_VALUE, true, - a -> new AnomalyRecord((String) a[0], (Date) a[1], (long) a[2])); + ConstructingObjectParser parser = new ConstructingObjectParser<>( + RESULT_TYPE_VALUE, + true, + a -> new AnomalyRecord((String) a[0], (Date) a[1], (long) a[2]) + ); parser.declareString(ConstructingObjectParser.constructorArg(), Job.ID); - parser.declareField(ConstructingObjectParser.constructorArg(), - p -> TimeUtils.parseTimeField(p, Result.TIMESTAMP.getPreferredName()), Result.TIMESTAMP, ValueType.VALUE); + parser.declareField( + ConstructingObjectParser.constructorArg(), + p -> TimeUtils.parseTimeField(p, Result.TIMESTAMP.getPreferredName()), + Result.TIMESTAMP, + ValueType.VALUE + ); parser.declareLong(ConstructingObjectParser.constructorArg(), BUCKET_SPAN); parser.declareString((anomalyRecord, s) -> {}, Result.RESULT_TYPE); parser.declareDouble(AnomalyRecord::setProbability, PROBABILITY); @@ -113,13 +119,21 @@ private static ConstructingObjectParser createParser(boolea parser.declareString(AnomalyRecord::setFieldName, FIELD_NAME); parser.declareString(AnomalyRecord::setOverFieldName, OVER_FIELD_NAME); parser.declareString(AnomalyRecord::setOverFieldValue, OVER_FIELD_VALUE); - parser.declareObjectArray(AnomalyRecord::setCauses, ignoreUnknownFields ? AnomalyCause.LENIENT_PARSER : AnomalyCause.STRICT_PARSER, - CAUSES); - parser.declareObjectArray(AnomalyRecord::setInfluencers, ignoreUnknownFields ? Influence.LENIENT_PARSER : Influence.STRICT_PARSER, - INFLUENCERS); - parser.declareObject(AnomalyRecord::setGeoResults, + parser.declareObjectArray( + AnomalyRecord::setCauses, + ignoreUnknownFields ? AnomalyCause.LENIENT_PARSER : AnomalyCause.STRICT_PARSER, + CAUSES + ); + parser.declareObjectArray( + AnomalyRecord::setInfluencers, + ignoreUnknownFields ? Influence.LENIENT_PARSER : Influence.STRICT_PARSER, + INFLUENCERS + ); + parser.declareObject( + AnomalyRecord::setGeoResults, ignoreUnknownFields ? GeoResults.LENIENT_PARSER : GeoResults.STRICT_PARSER, - GEO_RESULTS); + GEO_RESULTS + ); return parser; } @@ -348,9 +362,23 @@ public String getId() { return buildId(jobId, timestamp, bucketSpan, detectorIndex, byFieldValue, overFieldValue, partitionFieldValue); } - static String buildId(String jobId, Date timestamp, long bucketSpan, int detectorIndex, - String byFieldValue, String overFieldValue, String partitionFieldValue) { - return jobId + "_record_" + timestamp.getTime() + "_" + bucketSpan + "_" + detectorIndex + "_" + static String buildId( + String jobId, + Date timestamp, + long bucketSpan, + int detectorIndex, + String byFieldValue, + String overFieldValue, + String partitionFieldValue + ) { + return jobId + + "_record_" + + timestamp.getTime() + + "_" + + bucketSpan + + "_" + + detectorIndex + + "_" + MachineLearningField.valuesToId(byFieldValue, overFieldValue, partitionFieldValue); } @@ -542,11 +570,33 @@ public void setGeoResults(GeoResults geoResults) { @Override public int hashCode() { - return Objects.hash(jobId, detectorIndex, bucketSpan, probability, multiBucketImpact, recordScore, - initialRecordScore, typical, actual,function, functionDescription, fieldName, - byFieldName, byFieldValue, correlatedByFieldValue, partitionFieldName, - partitionFieldValue, overFieldName, overFieldValue, timestamp, isInterim, - causes, influences, jobId, geoResults); + return Objects.hash( + jobId, + detectorIndex, + bucketSpan, + probability, + multiBucketImpact, + recordScore, + initialRecordScore, + typical, + actual, + function, + functionDescription, + fieldName, + byFieldName, + byFieldValue, + correlatedByFieldValue, + partitionFieldName, + partitionFieldValue, + overFieldName, + overFieldValue, + timestamp, + isInterim, + causes, + influences, + jobId, + geoResults + ); } @Override @@ -562,29 +612,29 @@ public boolean equals(Object other) { AnomalyRecord that = (AnomalyRecord) other; return Objects.equals(this.jobId, that.jobId) - && this.detectorIndex == that.detectorIndex - && this.bucketSpan == that.bucketSpan - && this.probability == that.probability - && Objects.equals(this.multiBucketImpact, that.multiBucketImpact) - && this.recordScore == that.recordScore - && this.initialRecordScore == that.initialRecordScore - && Objects.deepEquals(this.typical, that.typical) - && Objects.deepEquals(this.actual, that.actual) - && Objects.equals(this.function, that.function) - && Objects.equals(this.functionDescription, that.functionDescription) - && Objects.equals(this.fieldName, that.fieldName) - && Objects.equals(this.byFieldName, that.byFieldName) - && Objects.equals(this.byFieldValue, that.byFieldValue) - && Objects.equals(this.correlatedByFieldValue, that.correlatedByFieldValue) - && Objects.equals(this.partitionFieldName, that.partitionFieldName) - && Objects.equals(this.partitionFieldValue, that.partitionFieldValue) - && Objects.equals(this.overFieldName, that.overFieldName) - && Objects.equals(this.overFieldValue, that.overFieldValue) - && Objects.equals(this.timestamp, that.timestamp) - && Objects.equals(this.isInterim, that.isInterim) - && Objects.equals(this.causes, that.causes) - && Objects.equals(this.geoResults, that.geoResults) - && Objects.equals(this.influences, that.influences); + && this.detectorIndex == that.detectorIndex + && this.bucketSpan == that.bucketSpan + && this.probability == that.probability + && Objects.equals(this.multiBucketImpact, that.multiBucketImpact) + && this.recordScore == that.recordScore + && this.initialRecordScore == that.initialRecordScore + && Objects.deepEquals(this.typical, that.typical) + && Objects.deepEquals(this.actual, that.actual) + && Objects.equals(this.function, that.function) + && Objects.equals(this.functionDescription, that.functionDescription) + && Objects.equals(this.fieldName, that.fieldName) + && Objects.equals(this.byFieldName, that.byFieldName) + && Objects.equals(this.byFieldValue, that.byFieldValue) + && Objects.equals(this.correlatedByFieldValue, that.correlatedByFieldValue) + && Objects.equals(this.partitionFieldName, that.partitionFieldName) + && Objects.equals(this.partitionFieldValue, that.partitionFieldValue) + && Objects.equals(this.overFieldName, that.overFieldName) + && Objects.equals(this.overFieldValue, that.overFieldValue) + && Objects.equals(this.timestamp, that.timestamp) + && Objects.equals(this.isInterim, that.isInterim) + && Objects.equals(this.causes, that.causes) + && Objects.equals(this.geoResults, that.geoResults) + && Objects.equals(this.influences, that.influences); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/Bucket.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/Bucket.java index 73053254f69ae..91a9635660977 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/Bucket.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/Bucket.java @@ -7,17 +7,17 @@ package org.elasticsearch.xpack.core.ml.job.results; import org.elasticsearch.common.Strings; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xpack.core.common.time.TimeUtils; import org.elasticsearch.xpack.core.ml.job.config.Job; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; -import org.elasticsearch.xpack.core.common.time.TimeUtils; import java.io.IOException; import java.util.ArrayList; @@ -59,7 +59,7 @@ public class Bucket implements ToXContentObject, Writeable { /* * * Read and discard the old (prior to 6.5) perPartitionNormalization values */ - public static Bucket readOldPerPartitionNormalization(StreamInput in) throws IOException { + public static Bucket readOldPerPartitionNormalization(StreamInput in) throws IOException { in.readString(); in.readString(); in.readDouble(); @@ -70,21 +70,34 @@ public static Bucket readOldPerPartitionNormalization(StreamInput in) throws IO } private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { - ConstructingObjectParser parser = new ConstructingObjectParser<>(RESULT_TYPE_VALUE, ignoreUnknownFields, - a -> new Bucket((String) a[0], (Date) a[1], (long) a[2])); + ConstructingObjectParser parser = new ConstructingObjectParser<>( + RESULT_TYPE_VALUE, + ignoreUnknownFields, + a -> new Bucket((String) a[0], (Date) a[1], (long) a[2]) + ); parser.declareString(ConstructingObjectParser.constructorArg(), JOB_ID); - parser.declareField(ConstructingObjectParser.constructorArg(), - p -> TimeUtils.parseTimeField(p, Result.TIMESTAMP.getPreferredName()), Result.TIMESTAMP, ValueType.VALUE); + parser.declareField( + ConstructingObjectParser.constructorArg(), + p -> TimeUtils.parseTimeField(p, Result.TIMESTAMP.getPreferredName()), + Result.TIMESTAMP, + ValueType.VALUE + ); parser.declareLong(ConstructingObjectParser.constructorArg(), BUCKET_SPAN); parser.declareDouble(Bucket::setAnomalyScore, ANOMALY_SCORE); parser.declareDouble(Bucket::setInitialAnomalyScore, INITIAL_ANOMALY_SCORE); parser.declareBoolean(Bucket::setInterim, Result.IS_INTERIM); parser.declareLong(Bucket::setEventCount, EVENT_COUNT); - parser.declareObjectArray(Bucket::setRecords, ignoreUnknownFields ? AnomalyRecord.LENIENT_PARSER : AnomalyRecord.STRICT_PARSER, - RECORDS); - parser.declareObjectArray(Bucket::setBucketInfluencers, ignoreUnknownFields ? - BucketInfluencer.LENIENT_PARSER : BucketInfluencer.STRICT_PARSER, BUCKET_INFLUENCERS); + parser.declareObjectArray( + Bucket::setRecords, + ignoreUnknownFields ? AnomalyRecord.LENIENT_PARSER : AnomalyRecord.STRICT_PARSER, + RECORDS + ); + parser.declareObjectArray( + Bucket::setBucketInfluencers, + ignoreUnknownFields ? BucketInfluencer.LENIENT_PARSER : BucketInfluencer.STRICT_PARSER, + BUCKET_INFLUENCERS + ); parser.declareLong(Bucket::setProcessingTimeMs, PROCESSING_TIME_MS); parser.declareString((bucket, s) -> {}, Result.RESULT_TYPE); parser.declareStringArray(Bucket::setScheduledEvents, SCHEDULED_EVENTS); @@ -293,8 +306,19 @@ public void setScheduledEvents(List scheduledEvents) { @Override public int hashCode() { - return Objects.hash(jobId, timestamp, eventCount, initialAnomalyScore, anomalyScore, records, - isInterim, bucketSpan, bucketInfluencers, processingTimeMs, scheduledEvents); + return Objects.hash( + jobId, + timestamp, + eventCount, + initialAnomalyScore, + anomalyScore, + records, + isInterim, + bucketSpan, + bucketInfluencers, + processingTimeMs, + scheduledEvents + ); } /** @@ -312,13 +336,17 @@ public boolean equals(Object other) { Bucket that = (Bucket) other; - return Objects.equals(this.jobId, that.jobId) && Objects.equals(this.timestamp, that.timestamp) - && (this.eventCount == that.eventCount) && (this.bucketSpan == that.bucketSpan) - && (this.anomalyScore == that.anomalyScore) && (this.initialAnomalyScore == that.initialAnomalyScore) - && Objects.equals(this.records, that.records) && Objects.equals(this.isInterim, that.isInterim) - && Objects.equals(this.bucketInfluencers, that.bucketInfluencers) - && (this.processingTimeMs == that.processingTimeMs) - && Objects.equals(this.scheduledEvents, that.scheduledEvents); + return Objects.equals(this.jobId, that.jobId) + && Objects.equals(this.timestamp, that.timestamp) + && (this.eventCount == that.eventCount) + && (this.bucketSpan == that.bucketSpan) + && (this.anomalyScore == that.anomalyScore) + && (this.initialAnomalyScore == that.initialAnomalyScore) + && Objects.equals(this.records, that.records) + && Objects.equals(this.isInterim, that.isInterim) + && Objects.equals(this.bucketInfluencers, that.bucketInfluencers) + && (this.processingTimeMs == that.processingTimeMs) + && Objects.equals(this.scheduledEvents, that.scheduledEvents); } /** diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/BucketInfluencer.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/BucketInfluencer.java index 39b9179be9369..f659ceced3565 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/BucketInfluencer.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/BucketInfluencer.java @@ -6,17 +6,17 @@ */ package org.elasticsearch.xpack.core.ml.job.results; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xpack.core.common.time.TimeUtils; import org.elasticsearch.xpack.core.ml.job.config.Job; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; -import org.elasticsearch.xpack.core.common.time.TimeUtils; import java.io.IOException; import java.util.Date; @@ -48,12 +48,19 @@ public class BucketInfluencer implements ToXContentObject, Writeable { public static final ConstructingObjectParser LENIENT_PARSER = createParser(true); private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { - ConstructingObjectParser parser = new ConstructingObjectParser<>(RESULT_TYPE_FIELD.getPreferredName(), - ignoreUnknownFields, a -> new BucketInfluencer((String) a[0], (Date) a[1], (long) a[2])); + ConstructingObjectParser parser = new ConstructingObjectParser<>( + RESULT_TYPE_FIELD.getPreferredName(), + ignoreUnknownFields, + a -> new BucketInfluencer((String) a[0], (Date) a[1], (long) a[2]) + ); parser.declareString(ConstructingObjectParser.constructorArg(), Job.ID); - parser.declareField(ConstructingObjectParser.constructorArg(), - p -> TimeUtils.parseTimeField(p, Result.TIMESTAMP.getPreferredName()), Result.TIMESTAMP, ValueType.VALUE); + parser.declareField( + ConstructingObjectParser.constructorArg(), + p -> TimeUtils.parseTimeField(p, Result.TIMESTAMP.getPreferredName()), + Result.TIMESTAMP, + ValueType.VALUE + ); parser.declareLong(ConstructingObjectParser.constructorArg(), BUCKET_SPAN); parser.declareString((bucketInfluencer, s) -> {}, Result.RESULT_TYPE); parser.declareString(BucketInfluencer::setInfluencerFieldName, INFLUENCER_FIELD_NAME); @@ -135,8 +142,12 @@ XContentBuilder innerToXContent(XContentBuilder builder, Params params) throws I * Data store ID of this bucket influencer. */ public String getId() { - return jobId + "_bucket_influencer_" + timestamp.getTime() + "_" + bucketSpan - + (influenceField == null ? "" : "_" + influenceField); + return jobId + + "_bucket_influencer_" + + timestamp.getTime() + + "_" + + bucketSpan + + (influenceField == null ? "" : "_" + influenceField); } public String getJobId() { @@ -197,8 +208,17 @@ public Date getTimestamp() { @Override public int hashCode() { - return Objects.hash(influenceField, initialAnomalyScore, anomalyScore, rawAnomalyScore, probability, isInterim, timestamp, jobId, - bucketSpan); + return Objects.hash( + influenceField, + initialAnomalyScore, + anomalyScore, + rawAnomalyScore, + probability, + isInterim, + timestamp, + jobId, + bucketSpan + ); } @Override @@ -217,10 +237,15 @@ public boolean equals(Object obj) { BucketInfluencer other = (BucketInfluencer) obj; - return Objects.equals(influenceField, other.influenceField) && Double.compare(initialAnomalyScore, other.initialAnomalyScore) == 0 - && Double.compare(anomalyScore, other.anomalyScore) == 0 && Double.compare(rawAnomalyScore, other.rawAnomalyScore) == 0 - && Double.compare(probability, other.probability) == 0 && Objects.equals(isInterim, other.isInterim) - && Objects.equals(timestamp, other.timestamp) && Objects.equals(jobId, other.jobId) && bucketSpan == other.bucketSpan; + return Objects.equals(influenceField, other.influenceField) + && Double.compare(initialAnomalyScore, other.initialAnomalyScore) == 0 + && Double.compare(anomalyScore, other.anomalyScore) == 0 + && Double.compare(rawAnomalyScore, other.rawAnomalyScore) == 0 + && Double.compare(probability, other.probability) == 0 + && Objects.equals(isInterim, other.isInterim) + && Objects.equals(timestamp, other.timestamp) + && Objects.equals(jobId, other.jobId) + && bucketSpan == other.bucketSpan; } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/CategoryDefinition.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/CategoryDefinition.java index 217ee348daccb..bc258167140c7 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/CategoryDefinition.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/CategoryDefinition.java @@ -6,11 +6,11 @@ */ package org.elasticsearch.xpack.core.ml.job.results; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ml.job.config.Job; @@ -50,8 +50,11 @@ public class CategoryDefinition implements ToXContentObject, Writeable { public static final ConstructingObjectParser LENIENT_PARSER = createParser(true); private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { - ConstructingObjectParser parser = new ConstructingObjectParser<>(TYPE.getPreferredName(), - ignoreUnknownFields, a -> new CategoryDefinition((String) a[0])); + ConstructingObjectParser parser = new ConstructingObjectParser<>( + TYPE.getPreferredName(), + ignoreUnknownFields, + a -> new CategoryDefinition((String) a[0]) + ); parser.declareString(ConstructingObjectParser.constructorArg(), Job.ID); parser.declareLong(CategoryDefinition::setCategoryId, CATEGORY_ID); @@ -269,21 +272,22 @@ public boolean equals(Object other) { } CategoryDefinition that = (CategoryDefinition) other; return Objects.equals(this.jobId, that.jobId) - && Objects.equals(this.categoryId, that.categoryId) - && Objects.equals(this.partitionFieldName, that.partitionFieldName) - && Objects.equals(this.partitionFieldValue, that.partitionFieldValue) - && Objects.equals(this.terms, that.terms) - && Objects.equals(this.regex, that.regex) - && Objects.equals(this.maxMatchingLength, that.maxMatchingLength) - && Objects.equals(this.examples, that.examples) - && Objects.equals(this.grokPattern, that.grokPattern) - && Arrays.equals(this.preferredToCategories, that.preferredToCategories) - && Objects.equals(this.numMatches, that.numMatches); + && Objects.equals(this.categoryId, that.categoryId) + && Objects.equals(this.partitionFieldName, that.partitionFieldName) + && Objects.equals(this.partitionFieldValue, that.partitionFieldValue) + && Objects.equals(this.terms, that.terms) + && Objects.equals(this.regex, that.regex) + && Objects.equals(this.maxMatchingLength, that.maxMatchingLength) + && Objects.equals(this.examples, that.examples) + && Objects.equals(this.grokPattern, that.grokPattern) + && Arrays.equals(this.preferredToCategories, that.preferredToCategories) + && Objects.equals(this.numMatches, that.numMatches); } @Override public int hashCode() { - return Objects.hash(jobId, + return Objects.hash( + jobId, categoryId, partitionFieldName, partitionFieldValue, @@ -293,6 +297,7 @@ public int hashCode() { examples, grokPattern, Arrays.hashCode(preferredToCategories), - numMatches); + numMatches + ); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/Forecast.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/Forecast.java index f93568a870e99..20a2fa95b08f3 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/Forecast.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/Forecast.java @@ -6,17 +6,17 @@ */ package org.elasticsearch.xpack.core.ml.job.results; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xpack.core.common.time.TimeUtils; import org.elasticsearch.xpack.core.ml.MachineLearningField; import org.elasticsearch.xpack.core.ml.job.config.Job; -import org.elasticsearch.xpack.core.common.time.TimeUtils; import java.io.IOException; import java.util.Date; @@ -47,13 +47,20 @@ public class Forecast implements ToXContentObject, Writeable { public static final ConstructingObjectParser STRICT_PARSER = createParser(false); private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { - ConstructingObjectParser parser = new ConstructingObjectParser<>(RESULT_TYPE_VALUE, ignoreUnknownFields, - a -> new Forecast((String) a[0], (String) a[1], (Date) a[2], (long) a[3], (int) a[4])); + ConstructingObjectParser parser = new ConstructingObjectParser<>( + RESULT_TYPE_VALUE, + ignoreUnknownFields, + a -> new Forecast((String) a[0], (String) a[1], (Date) a[2], (long) a[3], (int) a[4]) + ); parser.declareString(ConstructingObjectParser.constructorArg(), Job.ID); parser.declareString(ConstructingObjectParser.constructorArg(), FORECAST_ID); - parser.declareField(ConstructingObjectParser.constructorArg(), - p -> TimeUtils.parseTimeField(p, Result.TIMESTAMP.getPreferredName()), Result.TIMESTAMP, ValueType.VALUE); + parser.declareField( + ConstructingObjectParser.constructorArg(), + p -> TimeUtils.parseTimeField(p, Result.TIMESTAMP.getPreferredName()), + Result.TIMESTAMP, + ValueType.VALUE + ); parser.declareLong(ConstructingObjectParser.constructorArg(), BUCKET_SPAN); parser.declareInt(ConstructingObjectParser.constructorArg(), DETECTOR_INDEX); parser.declareString((modelForecast, s) -> {}, Result.RESULT_TYPE); @@ -133,8 +140,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.field(BUCKET_SPAN.getPreferredName(), bucketSpan); builder.field(DETECTOR_INDEX.getPreferredName(), detectorIndex); if (timestamp != null) { - builder.timeField(Result.TIMESTAMP.getPreferredName(), - Result.TIMESTAMP.getPreferredName() + "_string", timestamp.getTime()); + builder.timeField(Result.TIMESTAMP.getPreferredName(), Result.TIMESTAMP.getPreferredName() + "_string", timestamp.getTime()); } if (partitionFieldName != null) { builder.field(PARTITION_FIELD_NAME.getPreferredName(), partitionFieldName); @@ -167,9 +173,17 @@ public String getForecastId() { } public String getId() { - return jobId + "_model_forecast_" + forecastId + "_" + timestamp.getTime() - + "_" + bucketSpan + "_" + detectorIndex + "_" - + MachineLearningField.valuesToId(byFieldValue, partitionFieldValue); + return jobId + + "_model_forecast_" + + forecastId + + "_" + + timestamp.getTime() + + "_" + + bucketSpan + + "_" + + detectorIndex + + "_" + + MachineLearningField.valuesToId(byFieldValue, partitionFieldValue); } public Date getTimestamp() { @@ -257,25 +271,37 @@ public boolean equals(Object other) { return false; } Forecast that = (Forecast) other; - return Objects.equals(this.jobId, that.jobId) && - Objects.equals(this.forecastId, that.forecastId) && - Objects.equals(this.timestamp, that.timestamp) && - Objects.equals(this.partitionFieldValue, that.partitionFieldValue) && - Objects.equals(this.partitionFieldName, that.partitionFieldName) && - Objects.equals(this.byFieldValue, that.byFieldValue) && - Objects.equals(this.byFieldName, that.byFieldName) && - Objects.equals(this.modelFeature, that.modelFeature) && - this.forecastLower == that.forecastLower && - this.forecastUpper == that.forecastUpper && - this.forecastPrediction == that.forecastPrediction && - this.bucketSpan == that.bucketSpan && - this.detectorIndex == that.detectorIndex; + return Objects.equals(this.jobId, that.jobId) + && Objects.equals(this.forecastId, that.forecastId) + && Objects.equals(this.timestamp, that.timestamp) + && Objects.equals(this.partitionFieldValue, that.partitionFieldValue) + && Objects.equals(this.partitionFieldName, that.partitionFieldName) + && Objects.equals(this.byFieldValue, that.byFieldValue) + && Objects.equals(this.byFieldName, that.byFieldName) + && Objects.equals(this.modelFeature, that.modelFeature) + && this.forecastLower == that.forecastLower + && this.forecastUpper == that.forecastUpper + && this.forecastPrediction == that.forecastPrediction + && this.bucketSpan == that.bucketSpan + && this.detectorIndex == that.detectorIndex; } @Override public int hashCode() { - return Objects.hash(jobId, forecastId, timestamp, partitionFieldName, partitionFieldValue, - byFieldName, byFieldValue, modelFeature, forecastLower, forecastUpper, - forecastPrediction, bucketSpan, detectorIndex); + return Objects.hash( + jobId, + forecastId, + timestamp, + partitionFieldName, + partitionFieldValue, + byFieldName, + byFieldValue, + modelFeature, + forecastLower, + forecastUpper, + forecastPrediction, + bucketSpan, + detectorIndex + ); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/ForecastRequestStats.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/ForecastRequestStats.java index 3836568b7552f..21797baa4f8e9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/ForecastRequestStats.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/ForecastRequestStats.java @@ -6,12 +6,12 @@ */ package org.elasticsearch.xpack.core.ml.job.results; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ml.job.config.Job; @@ -53,8 +53,11 @@ public class ForecastRequestStats implements ToXContentObject, Writeable { public static final ConstructingObjectParser LENIENT_PARSER = createParser(true); private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { - ConstructingObjectParser parser = new ConstructingObjectParser<>(RESULT_TYPE_VALUE, ignoreUnknownFields, - a -> new ForecastRequestStats((String) a[0], (String) a[1])); + ConstructingObjectParser parser = new ConstructingObjectParser<>( + RESULT_TYPE_VALUE, + ignoreUnknownFields, + a -> new ForecastRequestStats((String) a[0], (String) a[1]) + ); parser.declareString(ConstructingObjectParser.constructorArg(), Job.ID); parser.declareString(ConstructingObjectParser.constructorArg(), FORECAST_ID); @@ -62,16 +65,11 @@ private static ConstructingObjectParser createParser parser.declareString((modelForecastRequestStats, s) -> {}, Result.RESULT_TYPE); parser.declareLong(ForecastRequestStats::setRecordCount, PROCESSED_RECORD_COUNT); parser.declareStringArray(ForecastRequestStats::setMessages, MESSAGES); - parser.declareField(ForecastRequestStats::setTimeStamp, - p -> Instant.ofEpochMilli(p.longValue()), Result.TIMESTAMP, ValueType.LONG); - parser.declareField(ForecastRequestStats::setStartTime, - p -> Instant.ofEpochMilli(p.longValue()), START_TIME, ValueType.LONG); - parser.declareField(ForecastRequestStats::setEndTime, - p -> Instant.ofEpochMilli(p.longValue()), END_TIME, ValueType.LONG); - parser.declareField(ForecastRequestStats::setCreateTime, - p -> Instant.ofEpochMilli(p.longValue()), CREATE_TIME, ValueType.LONG); - parser.declareField(ForecastRequestStats::setExpiryTime, - p -> Instant.ofEpochMilli(p.longValue()), EXPIRY_TIME, ValueType.LONG); + parser.declareField(ForecastRequestStats::setTimeStamp, p -> Instant.ofEpochMilli(p.longValue()), Result.TIMESTAMP, ValueType.LONG); + parser.declareField(ForecastRequestStats::setStartTime, p -> Instant.ofEpochMilli(p.longValue()), START_TIME, ValueType.LONG); + parser.declareField(ForecastRequestStats::setEndTime, p -> Instant.ofEpochMilli(p.longValue()), END_TIME, ValueType.LONG); + parser.declareField(ForecastRequestStats::setCreateTime, p -> Instant.ofEpochMilli(p.longValue()), CREATE_TIME, ValueType.LONG); + parser.declareField(ForecastRequestStats::setExpiryTime, p -> Instant.ofEpochMilli(p.longValue()), EXPIRY_TIME, ValueType.LONG); parser.declareDouble(ForecastRequestStats::setProgress, PROGRESS); parser.declareLong(ForecastRequestStats::setProcessingTime, PROCESSING_TIME_MS); parser.declareField(ForecastRequestStats::setStatus, p -> ForecastRequestStatus.fromString(p.text()), STATUS, ValueType.STRING); @@ -81,7 +79,12 @@ private static ConstructingObjectParser createParser } public enum ForecastRequestStatus implements Writeable { - OK, FAILED, STOPPED, STARTED, FINISHED, SCHEDULED; + OK, + FAILED, + STOPPED, + STARTED, + FINISHED, + SCHEDULED; public static ForecastRequestStatus fromString(String statusName) { return valueOf(statusName.trim().toUpperCase(Locale.ROOT)); @@ -98,7 +101,6 @@ public boolean isAnyOf(ForecastRequestStatus... candidates) { return Arrays.stream(candidates).anyMatch(candidate -> this == candidate); } - @Override public void writeTo(StreamOutput out) throws IOException { out.writeEnum(this); @@ -350,24 +352,37 @@ public boolean equals(Object other) { return false; } ForecastRequestStats that = (ForecastRequestStats) other; - return Objects.equals(this.jobId, that.jobId) && - Objects.equals(this.forecastId, that.forecastId) && - this.recordCount == that.recordCount && - Objects.equals(this.messages, that.messages) && - Objects.equals(this.timestamp, that.timestamp) && - Objects.equals(this.startTime, that.startTime) && - Objects.equals(this.endTime, that.endTime) && - Objects.equals(this.createTime, that.createTime) && - Objects.equals(this.expiryTime, that.expiryTime) && - this.progress == that.progress && - this.processingTime == that.processingTime && - this.memoryUsage == that.memoryUsage && - Objects.equals(this.status, that.status); + return Objects.equals(this.jobId, that.jobId) + && Objects.equals(this.forecastId, that.forecastId) + && this.recordCount == that.recordCount + && Objects.equals(this.messages, that.messages) + && Objects.equals(this.timestamp, that.timestamp) + && Objects.equals(this.startTime, that.startTime) + && Objects.equals(this.endTime, that.endTime) + && Objects.equals(this.createTime, that.createTime) + && Objects.equals(this.expiryTime, that.expiryTime) + && this.progress == that.progress + && this.processingTime == that.processingTime + && this.memoryUsage == that.memoryUsage + && Objects.equals(this.status, that.status); } @Override public int hashCode() { - return Objects.hash(jobId, forecastId, recordCount, messages, timestamp, startTime, endTime, createTime, expiryTime, - progress, processingTime, memoryUsage, status); + return Objects.hash( + jobId, + forecastId, + recordCount, + messages, + timestamp, + startTime, + endTime, + createTime, + expiryTime, + progress, + processingTime, + memoryUsage, + status + ); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/GeoResults.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/GeoResults.java index 53f6d042799f7..e7be1236c5252 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/GeoResults.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/GeoResults.java @@ -6,11 +6,11 @@ */ package org.elasticsearch.xpack.core.ml.job.results; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -28,8 +28,7 @@ public class GeoResults implements ToXContentObject, Writeable { public static final ObjectParser LENIENT_PARSER = createParser(true); private static ObjectParser createParser(boolean ignoreUnknownFields) { - ObjectParser parser = new ObjectParser<>(GEO_RESULTS.getPreferredName(), ignoreUnknownFields, - GeoResults::new); + ObjectParser parser = new ObjectParser<>(GEO_RESULTS.getPreferredName(), ignoreUnknownFields, GeoResults::new); parser.declareString(GeoResults::setActualPoint, ACTUAL_POINT); parser.declareString(GeoResults::setTypicalPoint, TYPICAL_POINT); return parser; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/Influence.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/Influence.java index a461967e5d1ce..7492f4d6d3aa3 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/Influence.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/Influence.java @@ -6,11 +6,11 @@ */ package org.elasticsearch.xpack.core.ml.job.results; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -34,16 +34,20 @@ public class Influence implements ToXContentObject, Writeable { public static final ConstructingObjectParser STRICT_PARSER = createParser(false); public static final ConstructingObjectParser LENIENT_PARSER = createParser(true); - @SuppressWarnings({ "unchecked"}) + @SuppressWarnings({ "unchecked" }) private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { - ConstructingObjectParser parser = new ConstructingObjectParser<>(INFLUENCER.getPreferredName(), - ignoreUnknownFields, a -> new Influence((String) a[0], (List) a[1])); + ConstructingObjectParser parser = new ConstructingObjectParser<>( + INFLUENCER.getPreferredName(), + ignoreUnknownFields, + a -> new Influence((String) a[0], (List) a[1]) + ); parser.declareString(ConstructingObjectParser.constructorArg(), INFLUENCER_FIELD_NAME); parser.declareStringArray(ConstructingObjectParser.constructorArg(), INFLUENCER_FIELD_VALUES); return parser; } + private String field; private List fieldValues; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/Influencer.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/Influencer.java index 2a89e71893855..b544c43295bc5 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/Influencer.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/Influencer.java @@ -6,18 +6,18 @@ */ package org.elasticsearch.xpack.core.ml.job.results; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xpack.core.common.time.TimeUtils; import org.elasticsearch.xpack.core.ml.MachineLearningField; import org.elasticsearch.xpack.core.ml.job.config.Job; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; -import org.elasticsearch.xpack.core.common.time.TimeUtils; import java.io.IOException; import java.util.Date; @@ -45,15 +45,21 @@ public class Influencer implements ToXContentObject, Writeable { // Influencers contain data fields, thus we always parse them leniently public static final ConstructingObjectParser LENIENT_PARSER = new ConstructingObjectParser<>( - RESULT_TYPE_FIELD.getPreferredName(), true, - a -> new Influencer((String) a[0], (String) a[1], (String) a[2], (Date) a[3], (long) a[4])); + RESULT_TYPE_FIELD.getPreferredName(), + true, + a -> new Influencer((String) a[0], (String) a[1], (String) a[2], (Date) a[3], (long) a[4]) + ); static { LENIENT_PARSER.declareString(ConstructingObjectParser.constructorArg(), Job.ID); LENIENT_PARSER.declareString(ConstructingObjectParser.constructorArg(), INFLUENCER_FIELD_NAME); LENIENT_PARSER.declareString(ConstructingObjectParser.constructorArg(), INFLUENCER_FIELD_VALUE); - LENIENT_PARSER.declareField(ConstructingObjectParser.constructorArg(), - p -> TimeUtils.parseTimeField(p, Result.TIMESTAMP.getPreferredName()), Result.TIMESTAMP, ValueType.VALUE); + LENIENT_PARSER.declareField( + ConstructingObjectParser.constructorArg(), + p -> TimeUtils.parseTimeField(p, Result.TIMESTAMP.getPreferredName()), + Result.TIMESTAMP, + ValueType.VALUE + ); LENIENT_PARSER.declareLong(ConstructingObjectParser.constructorArg(), BUCKET_SPAN); LENIENT_PARSER.declareString((influencer, s) -> {}, Result.RESULT_TYPE); LENIENT_PARSER.declareDouble(Influencer::setProbability, PROBABILITY); @@ -135,8 +141,15 @@ public String getJobId() { } public String getId() { - return jobId + "_influencer_" + timestamp.getTime() + "_" + bucketSpan + "_" + - influenceField + "_" + MachineLearningField.valuesToId(influenceValue); + return jobId + + "_influencer_" + + timestamp.getTime() + + "_" + + bucketSpan + + "_" + + influenceField + + "_" + + MachineLearningField.valuesToId(influenceValue); } public double getProbability() { @@ -185,8 +198,17 @@ public void setInterim(boolean value) { @Override public int hashCode() { - return Objects.hash(jobId, timestamp, influenceField, influenceValue, initialInfluencerScore, - influencerScore, probability, isInterim, bucketSpan); + return Objects.hash( + jobId, + timestamp, + influenceField, + influenceValue, + initialInfluencerScore, + influencerScore, + probability, + isInterim, + bucketSpan + ); } @Override @@ -204,11 +226,14 @@ public boolean equals(Object obj) { } Influencer other = (Influencer) obj; - return Objects.equals(jobId, other.jobId) && Objects.equals(timestamp, other.timestamp) - && Objects.equals(influenceField, other.influenceField) - && Objects.equals(influenceValue, other.influenceValue) - && Double.compare(initialInfluencerScore, other.initialInfluencerScore) == 0 - && Double.compare(influencerScore, other.influencerScore) == 0 && Double.compare(probability, other.probability) == 0 - && (isInterim == other.isInterim) && (bucketSpan == other.bucketSpan); + return Objects.equals(jobId, other.jobId) + && Objects.equals(timestamp, other.timestamp) + && Objects.equals(influenceField, other.influenceField) + && Objects.equals(influenceValue, other.influenceValue) + && Double.compare(initialInfluencerScore, other.initialInfluencerScore) == 0 + && Double.compare(influencerScore, other.influencerScore) == 0 + && Double.compare(probability, other.probability) == 0 + && (isInterim == other.isInterim) + && (bucketSpan == other.bucketSpan); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/ModelPlot.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/ModelPlot.java index 5058e95c0b21b..ba1a03c64e15e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/ModelPlot.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/ModelPlot.java @@ -6,17 +6,17 @@ */ package org.elasticsearch.xpack.core.ml.job.results; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xpack.core.common.time.TimeUtils; import org.elasticsearch.xpack.core.ml.MachineLearningField; import org.elasticsearch.xpack.core.ml.job.config.Job; -import org.elasticsearch.xpack.core.common.time.TimeUtils; import java.io.IOException; import java.util.Date; @@ -50,12 +50,19 @@ public class ModelPlot implements ToXContentObject, Writeable { public static final ConstructingObjectParser LENIENT_PARSER = createParser(true); private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { - ConstructingObjectParser parser = new ConstructingObjectParser<>(RESULT_TYPE_VALUE, ignoreUnknownFields, - a -> new ModelPlot((String) a[0], (Date) a[1], (long) a[2], (int) a[3])); + ConstructingObjectParser parser = new ConstructingObjectParser<>( + RESULT_TYPE_VALUE, + ignoreUnknownFields, + a -> new ModelPlot((String) a[0], (Date) a[1], (long) a[2], (int) a[3]) + ); parser.declareString(ConstructingObjectParser.constructorArg(), Job.ID); - parser.declareField(ConstructingObjectParser.constructorArg(), - p -> TimeUtils.parseTimeField(p, Result.TIMESTAMP.getPreferredName()), Result.TIMESTAMP, ValueType.VALUE); + parser.declareField( + ConstructingObjectParser.constructorArg(), + p -> TimeUtils.parseTimeField(p, Result.TIMESTAMP.getPreferredName()), + Result.TIMESTAMP, + ValueType.VALUE + ); parser.declareLong(ConstructingObjectParser.constructorArg(), BUCKET_SPAN); parser.declareInt(ConstructingObjectParser.constructorArg(), DETECTOR_INDEX); parser.declareString((modelPlot, s) -> {}, Result.RESULT_TYPE); @@ -146,8 +153,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.field(DETECTOR_INDEX.getPreferredName(), detectorIndex); if (timestamp != null) { - builder.timeField(Result.TIMESTAMP.getPreferredName(), - Result.TIMESTAMP.getPreferredName() + "_string", timestamp.getTime()); + builder.timeField(Result.TIMESTAMP.getPreferredName(), Result.TIMESTAMP.getPreferredName() + "_string", timestamp.getTime()); } if (partitionFieldName != null) { builder.field(PARTITION_FIELD_NAME.getPreferredName(), partitionFieldName); @@ -185,8 +191,15 @@ public String getJobId() { } public String getId() { - return jobId + "_model_plot_" + timestamp.getTime() + "_" + bucketSpan - + "_" + detectorIndex + "_" + MachineLearningField.valuesToId(byFieldValue, overFieldValue, partitionFieldValue); + return jobId + + "_model_plot_" + + timestamp.getTime() + + "_" + + bucketSpan + + "_" + + detectorIndex + + "_" + + MachineLearningField.valuesToId(byFieldValue, overFieldValue, partitionFieldValue); } public Date getTimestamp() { @@ -298,27 +311,41 @@ public boolean equals(Object other) { return false; } ModelPlot that = (ModelPlot) other; - return Objects.equals(this.jobId, that.jobId) && - Objects.equals(this.timestamp, that.timestamp) && - Objects.equals(this.partitionFieldValue, that.partitionFieldValue) && - Objects.equals(this.partitionFieldName, that.partitionFieldName) && - Objects.equals(this.overFieldValue, that.overFieldValue) && - Objects.equals(this.overFieldName, that.overFieldName) && - Objects.equals(this.byFieldValue, that.byFieldValue) && - Objects.equals(this.byFieldName, that.byFieldName) && - Objects.equals(this.modelFeature, that.modelFeature) && - this.modelLower == that.modelLower && - this.modelUpper == that.modelUpper && - this.modelMedian == that.modelMedian && - Objects.equals(this.actual, that.actual) && - this.bucketSpan == that.bucketSpan && - this.detectorIndex == that.detectorIndex; + return Objects.equals(this.jobId, that.jobId) + && Objects.equals(this.timestamp, that.timestamp) + && Objects.equals(this.partitionFieldValue, that.partitionFieldValue) + && Objects.equals(this.partitionFieldName, that.partitionFieldName) + && Objects.equals(this.overFieldValue, that.overFieldValue) + && Objects.equals(this.overFieldName, that.overFieldName) + && Objects.equals(this.byFieldValue, that.byFieldValue) + && Objects.equals(this.byFieldName, that.byFieldName) + && Objects.equals(this.modelFeature, that.modelFeature) + && this.modelLower == that.modelLower + && this.modelUpper == that.modelUpper + && this.modelMedian == that.modelMedian + && Objects.equals(this.actual, that.actual) + && this.bucketSpan == that.bucketSpan + && this.detectorIndex == that.detectorIndex; } @Override public int hashCode() { - return Objects.hash(jobId, timestamp, partitionFieldName, partitionFieldValue, - overFieldName, overFieldValue, byFieldName, byFieldValue, - modelFeature, modelLower, modelUpper, modelMedian, actual, bucketSpan, detectorIndex); + return Objects.hash( + jobId, + timestamp, + partitionFieldName, + partitionFieldValue, + overFieldName, + overFieldValue, + byFieldName, + byFieldValue, + modelFeature, + modelLower, + modelUpper, + modelMedian, + actual, + bucketSpan, + detectorIndex + ); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/OverallBucket.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/OverallBucket.java index cbcde34cef3dc..a7d46cfd981e4 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/OverallBucket.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/OverallBucket.java @@ -6,10 +6,10 @@ */ package org.elasticsearch.xpack.core.ml.job.results; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ml.job.config.Job; @@ -125,10 +125,10 @@ public boolean equals(Object other) { OverallBucket that = (OverallBucket) other; return Objects.equals(this.timestamp, that.timestamp) - && this.bucketSpan == that.bucketSpan - && this.overallScore == that.overallScore - && Objects.equals(this.jobs, that.jobs) - && this.isInterim == that.isInterim; + && this.bucketSpan == that.bucketSpan + && this.overallScore == that.overallScore + && Objects.equals(this.jobs, that.jobs) + && this.isInterim == that.isInterim; } public static class JobInfo implements ToXContentObject, Writeable, Comparable { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/ReservedFieldNames.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/ReservedFieldNames.java index 43d017d68dd12..02d1c89807f49 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/ReservedFieldNames.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/results/ReservedFieldNames.java @@ -23,7 +23,6 @@ import java.util.Set; import java.util.regex.Pattern; - /** * Defines the field names that we use for our results. * Fields from the raw data with these names are not added to any result. Even @@ -42,161 +41,168 @@ public final class ReservedFieldNames { * in a given index.) */ private static final String[] RESERVED_RESULT_FIELD_NAME_ARRAY = { - ElasticsearchMappings.ALL_FIELD_VALUES, - - Job.ID.getPreferredName(), - - AnomalyCause.PROBABILITY.getPreferredName(), - AnomalyCause.OVER_FIELD_NAME.getPreferredName(), - AnomalyCause.OVER_FIELD_VALUE.getPreferredName(), - AnomalyCause.BY_FIELD_NAME.getPreferredName(), - AnomalyCause.BY_FIELD_VALUE.getPreferredName(), - AnomalyCause.CORRELATED_BY_FIELD_VALUE.getPreferredName(), - AnomalyCause.PARTITION_FIELD_NAME.getPreferredName(), - AnomalyCause.PARTITION_FIELD_VALUE.getPreferredName(), - AnomalyCause.FUNCTION.getPreferredName(), - AnomalyCause.FUNCTION_DESCRIPTION.getPreferredName(), - AnomalyCause.TYPICAL.getPreferredName(), - AnomalyCause.ACTUAL.getPreferredName(), - AnomalyCause.GEO_RESULTS.getPreferredName(), - AnomalyCause.INFLUENCERS.getPreferredName(), - AnomalyCause.FIELD_NAME.getPreferredName(), - - AnomalyRecord.PROBABILITY.getPreferredName(), - AnomalyRecord.MULTI_BUCKET_IMPACT.getPreferredName(), - AnomalyRecord.BY_FIELD_NAME.getPreferredName(), - AnomalyRecord.BY_FIELD_VALUE.getPreferredName(), - AnomalyRecord.CORRELATED_BY_FIELD_VALUE.getPreferredName(), - AnomalyRecord.PARTITION_FIELD_NAME.getPreferredName(), - AnomalyRecord.PARTITION_FIELD_VALUE.getPreferredName(), - AnomalyRecord.FUNCTION.getPreferredName(), - AnomalyRecord.FUNCTION_DESCRIPTION.getPreferredName(), - AnomalyRecord.TYPICAL.getPreferredName(), - AnomalyRecord.ACTUAL.getPreferredName(), - AnomalyRecord.GEO_RESULTS.getPreferredName(), - AnomalyRecord.INFLUENCERS.getPreferredName(), - AnomalyRecord.FIELD_NAME.getPreferredName(), - AnomalyRecord.OVER_FIELD_NAME.getPreferredName(), - AnomalyRecord.OVER_FIELD_VALUE.getPreferredName(), - AnomalyRecord.CAUSES.getPreferredName(), - AnomalyRecord.RECORD_SCORE.getPreferredName(), - AnomalyRecord.INITIAL_RECORD_SCORE.getPreferredName(), - AnomalyRecord.BUCKET_SPAN.getPreferredName(), - - GeoResults.TYPICAL_POINT.getPreferredName(), - GeoResults.ACTUAL_POINT.getPreferredName(), - - Bucket.ANOMALY_SCORE.getPreferredName(), - Bucket.BUCKET_INFLUENCERS.getPreferredName(), - Bucket.BUCKET_SPAN.getPreferredName(), - Bucket.EVENT_COUNT.getPreferredName(), - Bucket.INITIAL_ANOMALY_SCORE.getPreferredName(), - Bucket.PROCESSING_TIME_MS.getPreferredName(), - Bucket.SCHEDULED_EVENTS.getPreferredName(), - - BucketInfluencer.INITIAL_ANOMALY_SCORE.getPreferredName(), BucketInfluencer.ANOMALY_SCORE.getPreferredName(), - BucketInfluencer.RAW_ANOMALY_SCORE.getPreferredName(), BucketInfluencer.PROBABILITY.getPreferredName(), - - CategoryDefinition.CATEGORY_ID.getPreferredName(), - CategoryDefinition.TERMS.getPreferredName(), - CategoryDefinition.REGEX.getPreferredName(), - CategoryDefinition.MAX_MATCHING_LENGTH.getPreferredName(), - CategoryDefinition.EXAMPLES.getPreferredName(), - CategoryDefinition.NUM_MATCHES.getPreferredName(), - CategoryDefinition.PREFERRED_TO_CATEGORIES.getPreferredName(), - - DataCounts.PROCESSED_RECORD_COUNT.getPreferredName(), - DataCounts.PROCESSED_FIELD_COUNT.getPreferredName(), - DataCounts.INPUT_BYTES.getPreferredName(), - DataCounts.INPUT_RECORD_COUNT.getPreferredName(), - DataCounts.INPUT_FIELD_COUNT.getPreferredName(), - DataCounts.INVALID_DATE_COUNT.getPreferredName(), - DataCounts.MISSING_FIELD_COUNT.getPreferredName(), - DataCounts.OUT_OF_ORDER_TIME_COUNT.getPreferredName(), - DataCounts.EMPTY_BUCKET_COUNT.getPreferredName(), - DataCounts.SPARSE_BUCKET_COUNT.getPreferredName(), - DataCounts.BUCKET_COUNT.getPreferredName(), - DataCounts.LATEST_RECORD_TIME.getPreferredName(), - DataCounts.EARLIEST_RECORD_TIME.getPreferredName(), - DataCounts.LAST_DATA_TIME.getPreferredName(), - DataCounts.LATEST_EMPTY_BUCKET_TIME.getPreferredName(), - DataCounts.LATEST_SPARSE_BUCKET_TIME.getPreferredName(), - DataCounts.LOG_TIME.getPreferredName(), - - Detector.DETECTOR_INDEX.getPreferredName(), - - Influence.INFLUENCER_FIELD_NAME.getPreferredName(), - Influence.INFLUENCER_FIELD_VALUES.getPreferredName(), - - Influencer.PROBABILITY.getPreferredName(), - Influencer.INFLUENCER_FIELD_NAME.getPreferredName(), - Influencer.INFLUENCER_FIELD_VALUE.getPreferredName(), - Influencer.INITIAL_INFLUENCER_SCORE.getPreferredName(), - Influencer.INFLUENCER_SCORE.getPreferredName(), - Influencer.BUCKET_SPAN.getPreferredName(), - - ModelPlot.PARTITION_FIELD_NAME.getPreferredName(), ModelPlot.PARTITION_FIELD_VALUE.getPreferredName(), - ModelPlot.OVER_FIELD_NAME.getPreferredName(), ModelPlot.OVER_FIELD_VALUE.getPreferredName(), - ModelPlot.BY_FIELD_NAME.getPreferredName(), ModelPlot.BY_FIELD_VALUE.getPreferredName(), - ModelPlot.MODEL_FEATURE.getPreferredName(), ModelPlot.MODEL_LOWER.getPreferredName(), - ModelPlot.MODEL_UPPER.getPreferredName(), ModelPlot.MODEL_MEDIAN.getPreferredName(), - ModelPlot.ACTUAL.getPreferredName(), - - Forecast.FORECAST_LOWER.getPreferredName(), Forecast.FORECAST_UPPER.getPreferredName(), - Forecast.FORECAST_PREDICTION.getPreferredName(), - Forecast.FORECAST_ID.getPreferredName(), - - //re-use: TIMESTAMP - ForecastRequestStats.START_TIME.getPreferredName(), - ForecastRequestStats.END_TIME.getPreferredName(), - ForecastRequestStats.CREATE_TIME.getPreferredName(), - ForecastRequestStats.EXPIRY_TIME.getPreferredName(), - ForecastRequestStats.MESSAGES.getPreferredName(), - ForecastRequestStats.PROGRESS.getPreferredName(), - ForecastRequestStats.STATUS.getPreferredName(), - ForecastRequestStats.MEMORY_USAGE.getPreferredName(), - - ModelSizeStats.MODEL_BYTES_FIELD.getPreferredName(), - ModelSizeStats.PEAK_MODEL_BYTES_FIELD.getPreferredName(), - ModelSizeStats.TOTAL_BY_FIELD_COUNT_FIELD.getPreferredName(), - ModelSizeStats.TOTAL_OVER_FIELD_COUNT_FIELD.getPreferredName(), - ModelSizeStats.TOTAL_PARTITION_FIELD_COUNT_FIELD.getPreferredName(), - ModelSizeStats.BUCKET_ALLOCATION_FAILURES_COUNT_FIELD.getPreferredName(), - ModelSizeStats.MEMORY_STATUS_FIELD.getPreferredName(), - ModelSizeStats.ASSIGNMENT_MEMORY_BASIS_FIELD.getPreferredName(), - ModelSizeStats.LOG_TIME_FIELD.getPreferredName(), - - ModelSnapshot.DESCRIPTION.getPreferredName(), - ModelSnapshotField.SNAPSHOT_ID.getPreferredName(), - ModelSnapshot.SNAPSHOT_DOC_COUNT.getPreferredName(), - ModelSnapshot.LATEST_RECORD_TIME.getPreferredName(), - ModelSnapshot.LATEST_RESULT_TIME.getPreferredName(), - ModelSnapshot.RETAIN.getPreferredName(), - ModelSnapshot.MIN_VERSION.getPreferredName(), - - Result.RESULT_TYPE.getPreferredName(), - Result.TIMESTAMP.getPreferredName(), - Result.IS_INTERIM.getPreferredName(), - - TimingStats.BUCKET_COUNT.getPreferredName(), - TimingStats.MIN_BUCKET_PROCESSING_TIME_MS.getPreferredName(), - TimingStats.MAX_BUCKET_PROCESSING_TIME_MS.getPreferredName(), - TimingStats.AVG_BUCKET_PROCESSING_TIME_MS.getPreferredName(), - TimingStats.EXPONENTIAL_AVG_BUCKET_PROCESSING_TIME_MS.getPreferredName(), - TimingStats.EXPONENTIAL_AVG_CALCULATION_CONTEXT.getPreferredName(), - - DatafeedTimingStats.SEARCH_COUNT.getPreferredName(), - DatafeedTimingStats.BUCKET_COUNT.getPreferredName(), - DatafeedTimingStats.TOTAL_SEARCH_TIME_MS.getPreferredName(), - DatafeedTimingStats.EXPONENTIAL_AVG_CALCULATION_CONTEXT.getPreferredName(), - - ExponentialAverageCalculationContext.INCREMENTAL_METRIC_VALUE_MS.getPreferredName(), - ExponentialAverageCalculationContext.LATEST_TIMESTAMP.getPreferredName(), - ExponentialAverageCalculationContext.PREVIOUS_EXPONENTIAL_AVERAGE_MS.getPreferredName(), - - GetResult._ID, - GetResult._INDEX - }; + ElasticsearchMappings.ALL_FIELD_VALUES, + + Job.ID.getPreferredName(), + + AnomalyCause.PROBABILITY.getPreferredName(), + AnomalyCause.OVER_FIELD_NAME.getPreferredName(), + AnomalyCause.OVER_FIELD_VALUE.getPreferredName(), + AnomalyCause.BY_FIELD_NAME.getPreferredName(), + AnomalyCause.BY_FIELD_VALUE.getPreferredName(), + AnomalyCause.CORRELATED_BY_FIELD_VALUE.getPreferredName(), + AnomalyCause.PARTITION_FIELD_NAME.getPreferredName(), + AnomalyCause.PARTITION_FIELD_VALUE.getPreferredName(), + AnomalyCause.FUNCTION.getPreferredName(), + AnomalyCause.FUNCTION_DESCRIPTION.getPreferredName(), + AnomalyCause.TYPICAL.getPreferredName(), + AnomalyCause.ACTUAL.getPreferredName(), + AnomalyCause.GEO_RESULTS.getPreferredName(), + AnomalyCause.INFLUENCERS.getPreferredName(), + AnomalyCause.FIELD_NAME.getPreferredName(), + + AnomalyRecord.PROBABILITY.getPreferredName(), + AnomalyRecord.MULTI_BUCKET_IMPACT.getPreferredName(), + AnomalyRecord.BY_FIELD_NAME.getPreferredName(), + AnomalyRecord.BY_FIELD_VALUE.getPreferredName(), + AnomalyRecord.CORRELATED_BY_FIELD_VALUE.getPreferredName(), + AnomalyRecord.PARTITION_FIELD_NAME.getPreferredName(), + AnomalyRecord.PARTITION_FIELD_VALUE.getPreferredName(), + AnomalyRecord.FUNCTION.getPreferredName(), + AnomalyRecord.FUNCTION_DESCRIPTION.getPreferredName(), + AnomalyRecord.TYPICAL.getPreferredName(), + AnomalyRecord.ACTUAL.getPreferredName(), + AnomalyRecord.GEO_RESULTS.getPreferredName(), + AnomalyRecord.INFLUENCERS.getPreferredName(), + AnomalyRecord.FIELD_NAME.getPreferredName(), + AnomalyRecord.OVER_FIELD_NAME.getPreferredName(), + AnomalyRecord.OVER_FIELD_VALUE.getPreferredName(), + AnomalyRecord.CAUSES.getPreferredName(), + AnomalyRecord.RECORD_SCORE.getPreferredName(), + AnomalyRecord.INITIAL_RECORD_SCORE.getPreferredName(), + AnomalyRecord.BUCKET_SPAN.getPreferredName(), + + GeoResults.TYPICAL_POINT.getPreferredName(), + GeoResults.ACTUAL_POINT.getPreferredName(), + + Bucket.ANOMALY_SCORE.getPreferredName(), + Bucket.BUCKET_INFLUENCERS.getPreferredName(), + Bucket.BUCKET_SPAN.getPreferredName(), + Bucket.EVENT_COUNT.getPreferredName(), + Bucket.INITIAL_ANOMALY_SCORE.getPreferredName(), + Bucket.PROCESSING_TIME_MS.getPreferredName(), + Bucket.SCHEDULED_EVENTS.getPreferredName(), + + BucketInfluencer.INITIAL_ANOMALY_SCORE.getPreferredName(), + BucketInfluencer.ANOMALY_SCORE.getPreferredName(), + BucketInfluencer.RAW_ANOMALY_SCORE.getPreferredName(), + BucketInfluencer.PROBABILITY.getPreferredName(), + + CategoryDefinition.CATEGORY_ID.getPreferredName(), + CategoryDefinition.TERMS.getPreferredName(), + CategoryDefinition.REGEX.getPreferredName(), + CategoryDefinition.MAX_MATCHING_LENGTH.getPreferredName(), + CategoryDefinition.EXAMPLES.getPreferredName(), + CategoryDefinition.NUM_MATCHES.getPreferredName(), + CategoryDefinition.PREFERRED_TO_CATEGORIES.getPreferredName(), + + DataCounts.PROCESSED_RECORD_COUNT.getPreferredName(), + DataCounts.PROCESSED_FIELD_COUNT.getPreferredName(), + DataCounts.INPUT_BYTES.getPreferredName(), + DataCounts.INPUT_RECORD_COUNT.getPreferredName(), + DataCounts.INPUT_FIELD_COUNT.getPreferredName(), + DataCounts.INVALID_DATE_COUNT.getPreferredName(), + DataCounts.MISSING_FIELD_COUNT.getPreferredName(), + DataCounts.OUT_OF_ORDER_TIME_COUNT.getPreferredName(), + DataCounts.EMPTY_BUCKET_COUNT.getPreferredName(), + DataCounts.SPARSE_BUCKET_COUNT.getPreferredName(), + DataCounts.BUCKET_COUNT.getPreferredName(), + DataCounts.LATEST_RECORD_TIME.getPreferredName(), + DataCounts.EARLIEST_RECORD_TIME.getPreferredName(), + DataCounts.LAST_DATA_TIME.getPreferredName(), + DataCounts.LATEST_EMPTY_BUCKET_TIME.getPreferredName(), + DataCounts.LATEST_SPARSE_BUCKET_TIME.getPreferredName(), + DataCounts.LOG_TIME.getPreferredName(), + + Detector.DETECTOR_INDEX.getPreferredName(), + + Influence.INFLUENCER_FIELD_NAME.getPreferredName(), + Influence.INFLUENCER_FIELD_VALUES.getPreferredName(), + + Influencer.PROBABILITY.getPreferredName(), + Influencer.INFLUENCER_FIELD_NAME.getPreferredName(), + Influencer.INFLUENCER_FIELD_VALUE.getPreferredName(), + Influencer.INITIAL_INFLUENCER_SCORE.getPreferredName(), + Influencer.INFLUENCER_SCORE.getPreferredName(), + Influencer.BUCKET_SPAN.getPreferredName(), + + ModelPlot.PARTITION_FIELD_NAME.getPreferredName(), + ModelPlot.PARTITION_FIELD_VALUE.getPreferredName(), + ModelPlot.OVER_FIELD_NAME.getPreferredName(), + ModelPlot.OVER_FIELD_VALUE.getPreferredName(), + ModelPlot.BY_FIELD_NAME.getPreferredName(), + ModelPlot.BY_FIELD_VALUE.getPreferredName(), + ModelPlot.MODEL_FEATURE.getPreferredName(), + ModelPlot.MODEL_LOWER.getPreferredName(), + ModelPlot.MODEL_UPPER.getPreferredName(), + ModelPlot.MODEL_MEDIAN.getPreferredName(), + ModelPlot.ACTUAL.getPreferredName(), + + Forecast.FORECAST_LOWER.getPreferredName(), + Forecast.FORECAST_UPPER.getPreferredName(), + Forecast.FORECAST_PREDICTION.getPreferredName(), + Forecast.FORECAST_ID.getPreferredName(), + + // re-use: TIMESTAMP + ForecastRequestStats.START_TIME.getPreferredName(), + ForecastRequestStats.END_TIME.getPreferredName(), + ForecastRequestStats.CREATE_TIME.getPreferredName(), + ForecastRequestStats.EXPIRY_TIME.getPreferredName(), + ForecastRequestStats.MESSAGES.getPreferredName(), + ForecastRequestStats.PROGRESS.getPreferredName(), + ForecastRequestStats.STATUS.getPreferredName(), + ForecastRequestStats.MEMORY_USAGE.getPreferredName(), + + ModelSizeStats.MODEL_BYTES_FIELD.getPreferredName(), + ModelSizeStats.PEAK_MODEL_BYTES_FIELD.getPreferredName(), + ModelSizeStats.TOTAL_BY_FIELD_COUNT_FIELD.getPreferredName(), + ModelSizeStats.TOTAL_OVER_FIELD_COUNT_FIELD.getPreferredName(), + ModelSizeStats.TOTAL_PARTITION_FIELD_COUNT_FIELD.getPreferredName(), + ModelSizeStats.BUCKET_ALLOCATION_FAILURES_COUNT_FIELD.getPreferredName(), + ModelSizeStats.MEMORY_STATUS_FIELD.getPreferredName(), + ModelSizeStats.ASSIGNMENT_MEMORY_BASIS_FIELD.getPreferredName(), + ModelSizeStats.LOG_TIME_FIELD.getPreferredName(), + + ModelSnapshot.DESCRIPTION.getPreferredName(), + ModelSnapshotField.SNAPSHOT_ID.getPreferredName(), + ModelSnapshot.SNAPSHOT_DOC_COUNT.getPreferredName(), + ModelSnapshot.LATEST_RECORD_TIME.getPreferredName(), + ModelSnapshot.LATEST_RESULT_TIME.getPreferredName(), + ModelSnapshot.RETAIN.getPreferredName(), + ModelSnapshot.MIN_VERSION.getPreferredName(), + + Result.RESULT_TYPE.getPreferredName(), + Result.TIMESTAMP.getPreferredName(), + Result.IS_INTERIM.getPreferredName(), + + TimingStats.BUCKET_COUNT.getPreferredName(), + TimingStats.MIN_BUCKET_PROCESSING_TIME_MS.getPreferredName(), + TimingStats.MAX_BUCKET_PROCESSING_TIME_MS.getPreferredName(), + TimingStats.AVG_BUCKET_PROCESSING_TIME_MS.getPreferredName(), + TimingStats.EXPONENTIAL_AVG_BUCKET_PROCESSING_TIME_MS.getPreferredName(), + TimingStats.EXPONENTIAL_AVG_CALCULATION_CONTEXT.getPreferredName(), + + DatafeedTimingStats.SEARCH_COUNT.getPreferredName(), + DatafeedTimingStats.BUCKET_COUNT.getPreferredName(), + DatafeedTimingStats.TOTAL_SEARCH_TIME_MS.getPreferredName(), + DatafeedTimingStats.EXPONENTIAL_AVG_CALCULATION_CONTEXT.getPreferredName(), + + ExponentialAverageCalculationContext.INCREMENTAL_METRIC_VALUE_MS.getPreferredName(), + ExponentialAverageCalculationContext.LATEST_TIMESTAMP.getPreferredName(), + ExponentialAverageCalculationContext.PREVIOUS_EXPONENTIAL_AVERAGE_MS.getPreferredName(), + + GetResult._ID, + GetResult._INDEX }; /** * Test if fieldName is one of the reserved result fieldnames or if it contains @@ -219,6 +225,5 @@ public static boolean isValidFieldName(String fieldName) { */ public static final Set RESERVED_RESULT_FIELD_NAMES = new HashSet<>(Arrays.asList(RESERVED_RESULT_FIELD_NAME_ARRAY)); - private ReservedFieldNames() { - } + private ReservedFieldNames() {} } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/snapshot/upgrade/SnapshotUpgradeState.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/snapshot/upgrade/SnapshotUpgradeState.java index dc61a655a4b93..1372bfba8e7ab 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/snapshot/upgrade/SnapshotUpgradeState.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/snapshot/upgrade/SnapshotUpgradeState.java @@ -17,7 +17,10 @@ public enum SnapshotUpgradeState implements Writeable, MemoryTrackedTaskState { - LOADING_OLD_STATE, SAVING_NEW_STATE, STOPPED, FAILED; + LOADING_OLD_STATE, + SAVING_NEW_STATE, + STOPPED, + FAILED; public static SnapshotUpgradeState fromString(String name) { return valueOf(name.trim().toUpperCase(Locale.ROOT)); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/snapshot/upgrade/SnapshotUpgradeTaskState.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/snapshot/upgrade/SnapshotUpgradeTaskState.java index 3863a2fc7548f..985faff43fcc1 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/snapshot/upgrade/SnapshotUpgradeTaskState.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/snapshot/upgrade/SnapshotUpgradeTaskState.java @@ -9,19 +9,19 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.persistent.PersistentTaskState; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.persistent.PersistentTaskState; import org.elasticsearch.xpack.core.ml.MlTasks; import java.io.IOException; import java.io.UncheckedIOException; import java.util.Objects; -public class SnapshotUpgradeTaskState implements PersistentTaskState{ +public class SnapshotUpgradeTaskState implements PersistentTaskState { public static final String NAME = MlTasks.JOB_SNAPSHOT_UPGRADE_TASK_NAME; @@ -33,9 +33,11 @@ public class SnapshotUpgradeTaskState implements PersistentTaskState{ private final long allocationId; private final String reason; - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(NAME, true, - a -> new SnapshotUpgradeTaskState((SnapshotUpgradeState) a[0], (long) a[1], (String) a[2])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + true, + a -> new SnapshotUpgradeTaskState((SnapshotUpgradeState) a[0], (long) a[1], (String) a[2]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), SnapshotUpgradeState::fromString, STATE); @@ -101,9 +103,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; SnapshotUpgradeTaskState that = (SnapshotUpgradeTaskState) o; - return allocationId == that.allocationId && - state == that.state && - Objects.equals(reason, that.reason); + return allocationId == that.allocationId && state == that.state && Objects.equals(reason, that.reason); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/notifications/AnomalyDetectionAuditMessage.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/notifications/AnomalyDetectionAuditMessage.java index c43929bd17aec..4c1b017de5619 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/notifications/AnomalyDetectionAuditMessage.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/notifications/AnomalyDetectionAuditMessage.java @@ -6,8 +6,8 @@ */ package org.elasticsearch.xpack.core.ml.notifications; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xpack.core.common.notifications.AbstractAuditMessage; import org.elasticsearch.xpack.core.common.notifications.Level; import org.elasticsearch.xpack.core.ml.job.config.Job; @@ -17,8 +17,11 @@ public class AnomalyDetectionAuditMessage extends AbstractAuditMessage { private static final ParseField JOB_ID = Job.ID; - public static final ConstructingObjectParser PARSER = - createParser("ml_audit_message", AnomalyDetectionAuditMessage::new, JOB_ID); + public static final ConstructingObjectParser PARSER = createParser( + "ml_audit_message", + AnomalyDetectionAuditMessage::new, + JOB_ID + ); public AnomalyDetectionAuditMessage(String resourceId, String message, Level level, Date timestamp, String nodeName) { super(resourceId, message, level, timestamp, nodeName); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/notifications/DataFrameAnalyticsAuditMessage.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/notifications/DataFrameAnalyticsAuditMessage.java index f7f8ddb2536a7..ec0705d9bc79d 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/notifications/DataFrameAnalyticsAuditMessage.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/notifications/DataFrameAnalyticsAuditMessage.java @@ -6,8 +6,8 @@ */ package org.elasticsearch.xpack.core.ml.notifications; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xpack.core.common.notifications.AbstractAuditMessage; import org.elasticsearch.xpack.core.common.notifications.Level; import org.elasticsearch.xpack.core.ml.job.config.Job; @@ -17,8 +17,11 @@ public class DataFrameAnalyticsAuditMessage extends AbstractAuditMessage { private static final ParseField JOB_ID = Job.ID; - public static final ConstructingObjectParser PARSER = - createParser("ml_analytics_audit_message", DataFrameAnalyticsAuditMessage::new, JOB_ID); + public static final ConstructingObjectParser PARSER = createParser( + "ml_analytics_audit_message", + DataFrameAnalyticsAuditMessage::new, + JOB_ID + ); public DataFrameAnalyticsAuditMessage(String resourceId, String message, Level level, Date timestamp, String nodeName) { super(resourceId, message, level, timestamp, nodeName); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/notifications/InferenceAuditMessage.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/notifications/InferenceAuditMessage.java index 31095325dc52f..d6286f08333b5 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/notifications/InferenceAuditMessage.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/notifications/InferenceAuditMessage.java @@ -6,21 +6,23 @@ */ package org.elasticsearch.xpack.core.ml.notifications; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xpack.core.common.notifications.AbstractAuditMessage; import org.elasticsearch.xpack.core.common.notifications.Level; import org.elasticsearch.xpack.core.ml.job.config.Job; import java.util.Date; - public class InferenceAuditMessage extends AbstractAuditMessage { - //TODO this should be MODEL_ID... + // TODO this should be MODEL_ID... private static final ParseField JOB_ID = Job.ID; - public static final ConstructingObjectParser PARSER = - createParser("ml_inference_audit_message", InferenceAuditMessage::new, JOB_ID); + public static final ConstructingObjectParser PARSER = createParser( + "ml_inference_audit_message", + InferenceAuditMessage::new, + JOB_ID + ); public InferenceAuditMessage(String resourceId, String message, Level level, Date timestamp, String nodeName) { super(resourceId, message, level, timestamp, nodeName); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/notifications/NotificationsIndex.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/notifications/NotificationsIndex.java index 46e76da6060e5..675db3dd92b24 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/notifications/NotificationsIndex.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/notifications/NotificationsIndex.java @@ -19,7 +19,10 @@ public final class NotificationsIndex { private NotificationsIndex() {} public static String mapping() { - return TemplateUtils.loadTemplate(RESOURCE_PATH + "notifications_index_mappings.json", - Version.CURRENT.toString(), MAPPINGS_VERSION_VARIABLE); + return TemplateUtils.loadTemplate( + RESOURCE_PATH + "notifications_index_mappings.json", + Version.CURRENT.toString(), + MAPPINGS_VERSION_VARIABLE + ); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/stats/CountAccumulator.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/stats/CountAccumulator.java index 5cb280cec08a4..98c07ccdf4d64 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/stats/CountAccumulator.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/stats/CountAccumulator.java @@ -15,8 +15,8 @@ import java.io.IOException; import java.util.HashMap; import java.util.Map; -import java.util.Objects; import java.util.Map.Entry; +import java.util.Objects; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -41,8 +41,9 @@ public CountAccumulator(StreamInput in) throws IOException { } public void merge(CountAccumulator other) { - counts = Stream.of(counts, other.counts).flatMap(m -> m.entrySet().stream()) - .collect(Collectors.toMap(Entry::getKey, Entry::getValue, (x, y) -> x + y)); + counts = Stream.of(counts, other.counts) + .flatMap(m -> m.entrySet().stream()) + .collect(Collectors.toMap(Entry::getKey, Entry::getValue, (x, y) -> x + y)); } public void add(String key, Long count) { @@ -54,8 +55,11 @@ public Map asMap() { } public static CountAccumulator fromTermsAggregation(StringTerms termsAggregation) { - return new CountAccumulator(termsAggregation.getBuckets().stream() - .collect(Collectors.toMap(bucket -> bucket.getKeyAsString(), bucket -> bucket.getDocCount()))); + return new CountAccumulator( + termsAggregation.getBuckets() + .stream() + .collect(Collectors.toMap(bucket -> bucket.getKeyAsString(), bucket -> bucket.getDocCount())) + ); } public void writeTo(StreamOutput out) throws IOException { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/stats/ForecastStats.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/stats/ForecastStats.java index 520d07c359baa..a1dcf99c96d92 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/stats/ForecastStats.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/stats/ForecastStats.java @@ -51,8 +51,13 @@ public ForecastStats() { /* * Construct ForecastStats for 1 job. Additional statistics can be added by merging other ForecastStats into it. */ - public ForecastStats(long total, StatsAccumulator memoryStats, StatsAccumulator recordStats, StatsAccumulator runtimeStats, - CountAccumulator statusCounts) { + public ForecastStats( + long total, + StatsAccumulator memoryStats, + StatsAccumulator recordStats, + StatsAccumulator runtimeStats, + CountAccumulator statusCounts + ) { this.total = total; this.forecastedJobs = total > 0 ? 1 : 0; this.memoryStats = Objects.requireNonNull(memoryStats); @@ -170,8 +175,11 @@ public boolean equals(Object obj) { } ForecastStats other = (ForecastStats) obj; - return Objects.equals(total, other.total) && Objects.equals(forecastedJobs, other.forecastedJobs) - && Objects.equals(memoryStats, other.memoryStats) && Objects.equals(recordStats, other.recordStats) - && Objects.equals(runtimeStats, other.runtimeStats) && Objects.equals(statusCounts, other.statusCounts); + return Objects.equals(total, other.total) + && Objects.equals(forecastedJobs, other.forecastedJobs) + && Objects.equals(memoryStats, other.memoryStats) + && Objects.equals(recordStats, other.recordStats) + && Objects.equals(runtimeStats, other.runtimeStats) + && Objects.equals(statusCounts, other.statusCounts); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/stats/StatsAccumulator.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/stats/StatsAccumulator.java index 3e9ded29f7c38..bf8411da2751b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/stats/StatsAccumulator.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/stats/StatsAccumulator.java @@ -33,8 +33,7 @@ public static class Fields { private Double min; private Double max; - public StatsAccumulator() { - } + public StatsAccumulator() {} public StatsAccumulator(StreamInput in) throws IOException { count = in.readLong(); @@ -66,7 +65,7 @@ public double getMax() { } public double getAvg() { - return count == 0.0 ? 0.0 : total/count; + return count == 0.0 ? 0.0 : total / count; } public double getTotal() { @@ -100,8 +99,12 @@ public void writeTo(StreamOutput out) throws IOException { } public static StatsAccumulator fromStatsAggregation(Stats statsAggregation) { - return new StatsAccumulator(statsAggregation.getCount(), statsAggregation.getSum(), statsAggregation.getMin(), - statsAggregation.getMax()); + return new StatsAccumulator( + statsAggregation.getCount(), + statsAggregation.getSum(), + statsAggregation.getMin(), + statsAggregation.getMax() + ); } @Override @@ -120,8 +123,9 @@ public boolean equals(Object obj) { } StatsAccumulator other = (StatsAccumulator) obj; - return Objects.equals(count, other.count) && Objects.equals(total, other.total) && Objects.equals(min, other.min) - && Objects.equals(max, other.max); + return Objects.equals(count, other.count) + && Objects.equals(total, other.total) + && Objects.equals(min, other.min) + && Objects.equals(max, other.max); } } - diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/ExceptionsHelper.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/ExceptionsHelper.java index 03ee5af56f010..fa045953218be 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/ExceptionsHelper.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/ExceptionsHelper.java @@ -12,9 +12,9 @@ import org.elasticsearch.ResourceNotFoundException; import org.elasticsearch.action.search.SearchPhaseExecutionException; import org.elasticsearch.action.search.ShardSearchFailure; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.search.SearchShardTarget; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xpack.core.ml.job.messages.Messages; public class ExceptionsHelper { @@ -86,8 +86,12 @@ public static ElasticsearchStatusException badRequestException(String msg, Objec } public static ElasticsearchStatusException configHasNotBeenMigrated(String verb, String id) { - return new ElasticsearchStatusException("cannot {} as the configuration [{}] is temporarily pending migration", - RestStatus.SERVICE_UNAVAILABLE, verb, id); + return new ElasticsearchStatusException( + "cannot {} as the configuration [{}] is temporarily pending migration", + RestStatus.SERVICE_UNAVAILABLE, + verb, + id + ); } /** @@ -99,9 +103,13 @@ public static String shardFailuresToErrorMsg(String jobId, ShardSearchFailure[] throw new IllegalStateException("Invalid call with null or empty shardFailures"); } SearchShardTarget shardTarget = shardFailures[0].shard(); - return "[" + jobId + "] Search request returned shard failures; first failure: shard [" - + (shardTarget == null ? "_na" : shardTarget) + "], reason [" - + shardFailures[0].reason() + "]; see logs for more info"; + return "[" + + jobId + + "] Search request returned shard failures; first failure: shard [" + + (shardTarget == null ? "_na" : shardTarget) + + "], reason [" + + shardFailures[0].reason() + + "]; see logs for more info"; } /** diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/ExponentialAverageCalculationContext.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/ExponentialAverageCalculationContext.java index b104f7b38ecd6..39d822b843d15 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/ExponentialAverageCalculationContext.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/ExponentialAverageCalculationContext.java @@ -6,14 +6,14 @@ */ package org.elasticsearch.xpack.core.ml.utils; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.common.time.TimeUtils; @@ -56,19 +56,20 @@ public class ExponentialAverageCalculationContext implements Writeable, ToXConte public static final ParseField LATEST_TIMESTAMP = new ParseField("latest_timestamp"); public static final ParseField PREVIOUS_EXPONENTIAL_AVERAGE_MS = new ParseField("previous_exponential_average_ms"); - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>( - "exponential_average_calculation_context", - true, - args -> { - Double incrementalMetricValueMs = (Double) args[0]; - Instant latestTimestamp = (Instant) args[1]; - Double previousExponentialAverageMs = (Double) args[2]; - return new ExponentialAverageCalculationContext( - getOrDefault(incrementalMetricValueMs, 0.0), - latestTimestamp, - previousExponentialAverageMs); - }); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "exponential_average_calculation_context", + true, + args -> { + Double incrementalMetricValueMs = (Double) args[0]; + Instant latestTimestamp = (Instant) args[1]; + Double previousExponentialAverageMs = (Double) args[2]; + return new ExponentialAverageCalculationContext( + getOrDefault(incrementalMetricValueMs, 0.0), + latestTimestamp, + previousExponentialAverageMs + ); + } + ); static { PARSER.declareDouble(optionalConstructorArg(), INCREMENTAL_METRIC_VALUE_MS); @@ -76,7 +77,8 @@ public class ExponentialAverageCalculationContext implements Writeable, ToXConte optionalConstructorArg(), p -> TimeUtils.parseTimeFieldToInstant(p, LATEST_TIMESTAMP.getPreferredName()), LATEST_TIMESTAMP, - ObjectParser.ValueType.VALUE); + ObjectParser.ValueType.VALUE + ); PARSER.declareDouble(optionalConstructorArg(), PREVIOUS_EXPONENTIAL_AVERAGE_MS); } @@ -92,9 +94,10 @@ public ExponentialAverageCalculationContext() { } public ExponentialAverageCalculationContext( - double incrementalMetricValueMs, - @Nullable Instant latestTimestamp, - @Nullable Double previousExponentialAverageMs) { + double incrementalMetricValueMs, + @Nullable Instant latestTimestamp, + @Nullable Double previousExponentialAverageMs + ) { this.incrementalMetricValueMs = incrementalMetricValueMs; this.latestTimestamp = latestTimestamp != null ? Instant.ofEpochMilli(latestTimestamp.toEpochMilli()) : null; this.previousExponentialAverageMs = previousExponentialAverageMs; @@ -129,7 +132,8 @@ public Double getCurrentExponentialAverageMs() { if (previousExponentialAverageMs == null || latestTimestamp == null) return incrementalMetricValueMs; Instant currentWindowStartTimestamp = latestTimestamp.truncatedTo(WINDOW_UNIT); double alpha = Math.exp( - - (double) Duration.between(currentWindowStartTimestamp, latestTimestamp).toMillis() / WINDOW_SIZE.toMillis()); + -(double) Duration.between(currentWindowStartTimestamp, latestTimestamp).toMillis() / WINDOW_SIZE.toMillis() + ); return alpha * previousExponentialAverageMs + (1 - alpha) * incrementalMetricValueMs; } @@ -177,7 +181,8 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.timeField( LATEST_TIMESTAMP.getPreferredName(), LATEST_TIMESTAMP.getPreferredName() + "_string", - latestTimestamp.toEpochMilli()); + latestTimestamp.toEpochMilli() + ); } if (previousExponentialAverageMs != null) { builder.field(PREVIOUS_EXPONENTIAL_AVERAGE_MS.getPreferredName(), previousExponentialAverageMs); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/MapHelper.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/MapHelper.java index 6f91b018e377d..dce1320d182c6 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/MapHelper.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/MapHelper.java @@ -6,8 +6,8 @@ */ package org.elasticsearch.xpack.core.ml.utils; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; +import org.elasticsearch.core.Nullable; import java.util.Collection; import java.util.HashMap; @@ -95,7 +95,7 @@ public static Object dig(String path, Map map) { */ public static Map dotCollapse(Map map, Collection pathsToCollapse) { // default load factor is 0.75 (3/4). - Map collapsed = new HashMap<>(((pathsToCollapse.size() * 4)/3) + 1); + Map collapsed = new HashMap<>(((pathsToCollapse.size() * 4) / 3) + 1); for (String path : pathsToCollapse) { Object dug = dig(path, map); if (dug != null) { @@ -113,14 +113,14 @@ private static Object explore(String[] path, Stack pathStack) { int startPos = potentialPath.pathPosition; Map map = potentialPath.map; String candidateKey = null; - while(endPos <= path.length) { + while (endPos <= path.length) { candidateKey = mergePath(path, startPos, endPos); Object next = map.get(candidateKey); if (endPos == path.length && next != null) { // exit early, we reached the full path and found something return next; } if (next instanceof Map) { // we found another map, continue exploring down this path - pathStack.push(new PotentialPath((Map)next, endPos)); + pathStack.push(new PotentialPath((Map) next, endPos)); } endPos++; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/MlIndexAndAlias.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/MlIndexAndAlias.java index b009bbf7e85b8..cfdcec8bfc8c2 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/MlIndexAndAlias.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/MlIndexAndAlias.java @@ -31,10 +31,10 @@ import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.Index; +import org.elasticsearch.indices.SystemIndexDescriptor; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.indices.SystemIndexDescriptor; import org.elasticsearch.xpack.core.template.IndexTemplateConfig; import java.io.IOException; @@ -87,44 +87,38 @@ private MlIndexAndAlias() {} * If the index is created, the listener is not called until the index is ready to use via the supplied alias, * so that a method that receives a success response from this method can safely use the index immediately. */ - public static void createIndexAndAliasIfNecessary(Client client, - ClusterState clusterState, - IndexNameExpressionResolver resolver, - String indexPatternPrefix, - String alias, - TimeValue masterNodeTimeout, - ActionListener finalListener) { - - final ActionListener loggingListener = ActionListener.wrap( - finalListener::onResponse, - e -> { - logger.error(new ParameterizedMessage( - "Failed to create alias and index with pattern [{}] and alias [{}]", - indexPatternPrefix, - alias), - e); - finalListener.onFailure(e); - } - ); + public static void createIndexAndAliasIfNecessary( + Client client, + ClusterState clusterState, + IndexNameExpressionResolver resolver, + String indexPatternPrefix, + String alias, + TimeValue masterNodeTimeout, + ActionListener finalListener + ) { + + final ActionListener loggingListener = ActionListener.wrap(finalListener::onResponse, e -> { + logger.error( + new ParameterizedMessage("Failed to create alias and index with pattern [{}] and alias [{}]", indexPatternPrefix, alias), + e + ); + finalListener.onFailure(e); + }); // If both the index and alias were successfully created then wait for the shards of the index that the alias points to be ready - ActionListener indexCreatedListener = ActionListener.wrap( - created -> { - if (created) { - waitForShardsReady(client, alias, masterNodeTimeout, loggingListener); - } else { - loggingListener.onResponse(false); - } - }, - loggingListener::onFailure - ); + ActionListener indexCreatedListener = ActionListener.wrap(created -> { + if (created) { + waitForShardsReady(client, alias, masterNodeTimeout, loggingListener); + } else { + loggingListener.onResponse(false); + } + }, loggingListener::onFailure); String legacyIndexWithoutSuffix = indexPatternPrefix; String indexPattern = indexPatternPrefix + "*"; // The initial index name must be suitable for rollover functionality. String firstConcreteIndex = indexPatternPrefix + "-000001"; - String[] concreteIndexNames = - resolver.concreteIndexNames(clusterState, IndicesOptions.lenientExpandHidden(), indexPattern); + String[] concreteIndexNames = resolver.concreteIndexNames(clusterState, IndicesOptions.lenientExpandHidden(), indexPattern); Optional indexPointedByCurrentWriteAlias = clusterState.getMetadata().hasAlias(alias) ? clusterState.getMetadata().getIndicesLookup().get(alias).getIndices().stream().map(Index::getName).findFirst() : Optional.empty(); @@ -136,7 +130,10 @@ public static void createIndexAndAliasIfNecessary(Client client, } logger.error( "There are no indices matching '{}' pattern but '{}' alias points at [{}]. This should never happen.", - indexPattern, alias, indexPointedByCurrentWriteAlias.get()); + indexPattern, + alias, + indexPointedByCurrentWriteAlias.get() + ); } else if (concreteIndexNames.length == 1 && concreteIndexNames[0].equals(legacyIndexWithoutSuffix)) { if (indexPointedByCurrentWriteAlias.isEmpty()) { createFirstConcreteIndex(client, firstConcreteIndex, alias, true, indexCreatedListener); @@ -150,13 +147,18 @@ public static void createIndexAndAliasIfNecessary(Client client, false, ActionListener.wrap( unused -> updateWriteAlias(client, alias, legacyIndexWithoutSuffix, firstConcreteIndex, indexCreatedListener), - loggingListener::onFailure) + loggingListener::onFailure + ) ); return; } logger.error( "There is exactly one index (i.e. '{}') matching '{}' pattern but '{}' alias points at [{}]. This should never happen.", - legacyIndexWithoutSuffix, indexPattern, alias, indexPointedByCurrentWriteAlias.get()); + legacyIndexWithoutSuffix, + indexPattern, + alias, + indexPointedByCurrentWriteAlias.get() + ); } else { if (indexPointedByCurrentWriteAlias.isEmpty()) { assert concreteIndexNames.length > 0; @@ -169,11 +171,13 @@ public static void createIndexAndAliasIfNecessary(Client client, loggingListener.onResponse(false); } - public static void createSystemIndexIfNecessary(Client client, - ClusterState clusterState, - SystemIndexDescriptor descriptor, - TimeValue masterNodeTimeout, - ActionListener finalListener) { + public static void createSystemIndexIfNecessary( + Client client, + ClusterState clusterState, + SystemIndexDescriptor descriptor, + TimeValue masterNodeTimeout, + ActionListener finalListener + ) { final String primaryIndex = descriptor.getPrimaryIndex(); @@ -183,22 +187,19 @@ public static void createSystemIndexIfNecessary(Client client, return; } - ActionListener indexCreatedListener = ActionListener.wrap( - created -> { - if (created) { - waitForShardsReady(client, primaryIndex, masterNodeTimeout, finalListener); - } else { - finalListener.onResponse(false); - } - }, - e -> { - if (ExceptionsHelper.unwrapCause(e) instanceof ResourceAlreadyExistsException) { - finalListener.onResponse(true); - } else { - finalListener.onFailure(e); - } + ActionListener indexCreatedListener = ActionListener.wrap(created -> { + if (created) { + waitForShardsReady(client, primaryIndex, masterNodeTimeout, finalListener); + } else { + finalListener.onResponse(false); } - ); + }, e -> { + if (ExceptionsHelper.unwrapCause(e) instanceof ResourceAlreadyExistsException) { + finalListener.onResponse(true); + } else { + finalListener.onFailure(e); + } + }); CreateIndexRequest createIndexRequest = new CreateIndexRequest(primaryIndex); createIndexRequest.settings(descriptor.getSettings()); @@ -206,11 +207,16 @@ public static void createSystemIndexIfNecessary(Client client, createIndexRequest.origin(ML_ORIGIN); createIndexRequest.masterNodeTimeout(masterNodeTimeout); - executeAsyncWithOrigin(client.threadPool().getThreadContext(), ML_ORIGIN, createIndexRequest, + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + ML_ORIGIN, + createIndexRequest, ActionListener.wrap( r -> indexCreatedListener.onResponse(r.isAcknowledged()), indexCreatedListener::onFailure - ), client.admin().indices()::create); + ), + client.admin().indices()::create + ); } private static void waitForShardsReady(Client client, String index, TimeValue masterNodeTimeout, ActionListener listener) { @@ -225,54 +231,57 @@ private static void waitForShardsReady(Client client, String index, TimeValue ma healthRequest, ActionListener.wrap( response -> listener.onResponse(response.isTimedOut() == false), - listener::onFailure), + listener::onFailure + ), client.admin().cluster()::health ); } - private static void createFirstConcreteIndex(Client client, - String index, - String alias, - boolean addAlias, - ActionListener listener) { + private static void createFirstConcreteIndex( + Client client, + String index, + String alias, + boolean addAlias, + ActionListener listener + ) { logger.info("About to create first concrete index [{}] with alias [{}]", index, alias); - CreateIndexRequestBuilder requestBuilder = client.admin() - .indices() - .prepareCreate(index); + CreateIndexRequestBuilder requestBuilder = client.admin().indices().prepareCreate(index); if (addAlias) { requestBuilder.addAlias(new Alias(alias).isHidden(true)); } CreateIndexRequest request = requestBuilder.request(); - executeAsyncWithOrigin(client.threadPool().getThreadContext(), + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), ML_ORIGIN, request, - ActionListener.wrap( - createIndexResponse -> listener.onResponse(true), - createIndexFailure -> { - if (ExceptionsHelper.unwrapCause(createIndexFailure) instanceof ResourceAlreadyExistsException) { - // If it was created between our last check and this request being handled, we should add the alias - // if we were asked to add it on creation. Adding an alias that already exists is idempotent. So - // no need to double check if the alias exists as well. But if we weren't asked to add the alias - // on creation then we should leave it up to the caller to decide what to do next (some call sites - // already have more advanced alias update logic in their success handlers). - if (addAlias) { - updateWriteAlias(client, alias, null, index, listener); - } else { - listener.onResponse(true); - } + ActionListener.wrap(createIndexResponse -> listener.onResponse(true), createIndexFailure -> { + if (ExceptionsHelper.unwrapCause(createIndexFailure) instanceof ResourceAlreadyExistsException) { + // If it was created between our last check and this request being handled, we should add the alias + // if we were asked to add it on creation. Adding an alias that already exists is idempotent. So + // no need to double check if the alias exists as well. But if we weren't asked to add the alias + // on creation then we should leave it up to the caller to decide what to do next (some call sites + // already have more advanced alias update logic in their success handlers). + if (addAlias) { + updateWriteAlias(client, alias, null, index, listener); } else { - listener.onFailure(createIndexFailure); + listener.onResponse(true); } - }), - client.admin().indices()::create); + } else { + listener.onFailure(createIndexFailure); + } + }), + client.admin().indices()::create + ); } - private static void updateWriteAlias(Client client, - String alias, - @Nullable String currentIndex, - String newIndex, - ActionListener listener) { + private static void updateWriteAlias( + Client client, + String alias, + @Nullable String currentIndex, + String newIndex, + ActionListener listener + ) { logger.info("About to move write alias [{}] from index [{}] to index [{}]", alias, currentIndex, newIndex); IndicesAliasesRequestBuilder requestBuilder = client.admin() .indices() @@ -283,13 +292,13 @@ private static void updateWriteAlias(Client client, } IndicesAliasesRequest request = requestBuilder.request(); - executeAsyncWithOrigin(client.threadPool().getThreadContext(), + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), ML_ORIGIN, request, - ActionListener.wrap( - resp -> listener.onResponse(resp.isAcknowledged()), - listener::onFailure), - client.admin().indices()::aliases); + ActionListener.wrap(resp -> listener.onResponse(resp.isAcknowledged()), listener::onFailure), + client.admin().indices()::aliases + ); } /** @@ -322,10 +331,15 @@ public static void installIndexTemplateIfRequired( PutComposableIndexTemplateAction.Request request; try { - request = new PutComposableIndexTemplateAction.Request(templateConfig.getTemplateName()) - .indexTemplate(ComposableIndexTemplate.parse(JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, templateConfig.loadBytes()))) - .masterNodeTimeout(masterTimeout); + request = new PutComposableIndexTemplateAction.Request(templateConfig.getTemplateName()).indexTemplate( + ComposableIndexTemplate.parse( + JsonXContent.jsonXContent.createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + templateConfig.loadBytes() + ) + ) + ).masterNodeTimeout(masterTimeout); } catch (IOException e) { throw new ElasticsearchParseException("unable to parse composable template " + templateConfig.getTemplateName(), e); } @@ -355,14 +369,12 @@ public static void installIndexTemplateIfRequired( return; } - ActionListener innerListener = ActionListener.wrap( - response -> { - if (response.isAcknowledged() == false) { - logger.warn("error adding template [{}], request was not acknowledged", templateRequest.name()); - } - listener.onResponse(response.isAcknowledged()); - }, - listener::onFailure); + ActionListener innerListener = ActionListener.wrap(response -> { + if (response.isAcknowledged() == false) { + logger.warn("error adding template [{}], request was not acknowledged", templateRequest.name()); + } + listener.onResponse(response.isAcknowledged()); + }, listener::onFailure); executeAsyncWithOrigin(client, ML_ORIGIN, PutComposableIndexTemplateAction.INSTANCE, templateRequest, innerListener); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/MlParserUtils.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/MlParserUtils.java index e13046e213d84..10a76c602137e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/MlParserUtils.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/MlParserUtils.java @@ -28,21 +28,25 @@ private MlParserUtils() {} * @return a list of lists representing the array of arrays * @throws IOException an exception if parsing fails */ - public static List> parseArrayOfArrays(String fieldName, CheckedFunction valueParser, - XContentParser parser) throws IOException { + public static List> parseArrayOfArrays( + String fieldName, + CheckedFunction valueParser, + XContentParser parser + ) throws IOException { if (parser.currentToken() != XContentParser.Token.START_ARRAY) { throw new IllegalArgumentException("unexpected token [" + parser.currentToken() + "] for [" + fieldName + "]"); } List> values = new ArrayList<>(); - while(parser.nextToken() != XContentParser.Token.END_ARRAY) { + while (parser.nextToken() != XContentParser.Token.END_ARRAY) { if (parser.currentToken() != XContentParser.Token.START_ARRAY) { throw new IllegalArgumentException("unexpected token [" + parser.currentToken() + "] for [" + fieldName + "]"); } List innerList = new ArrayList<>(); - while(parser.nextToken() != XContentParser.Token.END_ARRAY) { - if(parser.currentToken().isValue() == false) { - throw new IllegalStateException("expected non-null value but got [" + parser.currentToken() + "] " + - "for [" + fieldName + "]"); + while (parser.nextToken() != XContentParser.Token.END_ARRAY) { + if (parser.currentToken().isValue() == false) { + throw new IllegalStateException( + "expected non-null value but got [" + parser.currentToken() + "] " + "for [" + fieldName + "]" + ); } innerList.add(valueParser.apply(parser)); } @@ -64,14 +68,14 @@ public static double[][][] parse3DArrayOfDoubles(String fieldName, XContentParse throw new IllegalArgumentException("unexpected token [" + parser.currentToken() + "] for [" + fieldName + "]"); } List>> values = new ArrayList<>(); - while(parser.nextToken() != XContentParser.Token.END_ARRAY) { + while (parser.nextToken() != XContentParser.Token.END_ARRAY) { if (parser.currentToken() != XContentParser.Token.START_ARRAY) { throw new IllegalArgumentException("unexpected token [" + parser.currentToken() + "] for [" + fieldName + "]"); } List> innerList = new ArrayList<>(); - while(parser.nextToken() != XContentParser.Token.END_ARRAY) { + while (parser.nextToken() != XContentParser.Token.END_ARRAY) { if (parser.currentToken() != XContentParser.Token.START_ARRAY) { throw new IllegalArgumentException("unexpected token [" + parser.currentToken() + "] for [" + fieldName + "]"); } @@ -83,8 +87,9 @@ public static double[][][] parse3DArrayOfDoubles(String fieldName, XContentParse List innerInner = new ArrayList<>(); while (parser.nextToken() != XContentParser.Token.END_ARRAY) { if (parser.currentToken() != XContentParser.Token.VALUE_NUMBER) { - throw new IllegalStateException("expected non-null numerical value but got [" + parser.currentToken() + "] " + - "for [" + fieldName + "]"); + throw new IllegalStateException( + "expected non-null numerical value but got [" + parser.currentToken() + "] " + "for [" + fieldName + "]" + ); } innerInner.add(parser.doubleValue()); } @@ -93,7 +98,7 @@ public static double[][][] parse3DArrayOfDoubles(String fieldName, XContentParse values.add(innerList); } - double [][][] val = new double[values.size()][values.get(0).size()][values.get(0).get(0).size()]; + double[][][] val = new double[values.size()][values.get(0).size()][values.get(0).get(0).size()]; for (int i = 0; i < val.length; i++) { for (int j = 0; j < val[0].length; j++) { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/MlStrings.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/MlStrings.java index 0035a82ef015e..bb8c110255dee 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/MlStrings.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/MlStrings.java @@ -33,8 +33,7 @@ public final class MlStrings { public static final int ID_LENGTH_LIMIT = 64; - private MlStrings() { - } + private MlStrings() {} /** * Surrounds with double quotes the given {@code input} if it contains @@ -117,7 +116,7 @@ public static Set findMatching(String[] patterns, Set items) { Set matchingItems = new LinkedHashSet<>(); for (String pattern : patterns) { - if (items.contains(pattern)) { + if (items.contains(pattern)) { matchingItems.add(pattern); } else if (Regex.isSimpleMatchPattern(pattern)) { for (String item : items) { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/NameResolver.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/NameResolver.java index b8eb83e70432c..db08eb9ad772a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/NameResolver.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/NameResolver.java @@ -60,10 +60,10 @@ public SortedSet expand(String expression, boolean allowNoMatch) { for (String token : tokens) { if (Regex.isSimpleMatchPattern(token)) { List expanded = keys().stream() - .filter(key -> Regex.simpleMatch(token, key)) - .map(this::lookup) - .flatMap(List::stream) - .collect(Collectors.toList()); + .filter(key -> Regex.simpleMatch(token, key)) + .map(this::lookup) + .flatMap(List::stream) + .collect(Collectors.toList()); if (expanded.isEmpty() && allowNoMatch == false) { throw notFoundExceptionSupplier.apply(token); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/NamedXContentObjectHelper.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/NamedXContentObjectHelper.java index a23bc526dcae6..2519623099980 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/NamedXContentObjectHelper.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/NamedXContentObjectHelper.java @@ -16,11 +16,13 @@ public final class NamedXContentObjectHelper { private NamedXContentObjectHelper() {} - public static XContentBuilder writeNamedObjects(XContentBuilder builder, - ToXContent.Params params, - boolean useExplicitOrder, - String namedObjectsName, - List namedObjects) throws IOException { + public static XContentBuilder writeNamedObjects( + XContentBuilder builder, + ToXContent.Params params, + boolean useExplicitOrder, + String namedObjectsName, + List namedObjects + ) throws IOException { if (useExplicitOrder) { builder.startArray(namedObjectsName); } else { @@ -43,10 +45,12 @@ public static XContentBuilder writeNamedObjects(XContentBuilder builder, return builder; } - public static XContentBuilder writeNamedObject(XContentBuilder builder, - ToXContent.Params params, - String namedObjectName, - NamedXContentObject namedObject) throws IOException { + public static XContentBuilder writeNamedObject( + XContentBuilder builder, + ToXContent.Params params, + String namedObjectName, + NamedXContentObject namedObject + ) throws IOException { builder.startObject(namedObjectName); builder.field(namedObject.getName(), namedObject, params); builder.endObject(); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/PhaseProgress.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/PhaseProgress.java index 734138b27ccf7..1d56f520a3bce 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/PhaseProgress.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/PhaseProgress.java @@ -6,12 +6,12 @@ */ package org.elasticsearch.xpack.core.ml.utils; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -26,8 +26,11 @@ public class PhaseProgress implements ToXContentObject, Writeable { public static final ParseField PHASE = new ParseField("phase"); public static final ParseField PROGRESS_PERCENT = new ParseField("progress_percent"); - public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("phase_progress", - true, a -> new PhaseProgress((String) a[0], (int) a[1])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "phase_progress", + true, + a -> new PhaseProgress((String) a[0], (int) a[1]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), PHASE); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/QueryProvider.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/QueryProvider.java index 29de396317d7e..106e102033fa4 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/QueryProvider.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/QueryProvider.java @@ -11,13 +11,13 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.index.query.MatchAllQueryBuilder; +import org.elasticsearch.index.query.QueryBuilder; +import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.index.query.MatchAllQueryBuilder; -import org.elasticsearch.index.query.QueryBuilder; -import org.elasticsearch.index.query.QueryBuilders; import java.io.IOException; import java.util.Collections; @@ -37,7 +37,8 @@ public static QueryProvider defaultQuery() { return new QueryProvider( Collections.singletonMap(MatchAllQueryBuilder.NAME, Collections.emptyMap()), QueryBuilders.matchAllQuery(), - null); + null + ); } public static QueryProvider fromXContent(XContentParser parser, boolean lenient, String failureMessage) throws IOException { @@ -46,9 +47,9 @@ public static QueryProvider fromXContent(XContentParser parser, boolean lenient, Exception exception = null; try { parsedQuery = XContentObjectTransformer.queryBuilderTransformer(parser.getXContentRegistry()).fromMap(query); - } catch(Exception ex) { + } catch (Exception ex) { if (ex.getCause() instanceof IllegalArgumentException) { - ex = (Exception)ex.getCause(); + ex = (Exception) ex.getCause(); } exception = ex; if (lenient) { @@ -61,12 +62,13 @@ public static QueryProvider fromXContent(XContentParser parser, boolean lenient, } public static QueryProvider fromParsedQuery(QueryBuilder parsedQuery) throws IOException { - return parsedQuery == null ? - null : - new QueryProvider( + return parsedQuery == null + ? null + : new QueryProvider( XContentObjectTransformer.queryBuilderTransformer(NamedXContentRegistry.EMPTY).toMap(parsedQuery), parsedQuery, - null); + null + ); } public static QueryProvider fromStream(StreamInput in) throws IOException { @@ -130,4 +132,3 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws return builder; } } - diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/ToXContentParams.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/ToXContentParams.java index e630c1f2ad72b..ebf2b80366c32 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/ToXContentParams.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/ToXContentParams.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.xpack.core.ml.utils; - /** * Parameters used by machine learning for controlling X Content serialisation. */ @@ -33,6 +32,5 @@ public final class ToXContentParams { */ public static final String INCLUDE_CALCULATED_FIELDS = "include_calculated_fields"; - private ToXContentParams() { - } + private ToXContentParams() {} } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/XContentObjectTransformer.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/XContentObjectTransformer.java index 267b4a25bdc11..55ef79dc2cfc4 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/XContentObjectTransformer.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/XContentObjectTransformer.java @@ -6,19 +6,19 @@ */ package org.elasticsearch.xpack.core.ml.utils; -import org.elasticsearch.core.CheckedFunction; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.core.CheckedFunction; +import org.elasticsearch.index.query.AbstractQueryBuilder; +import org.elasticsearch.index.query.QueryBuilder; +import org.elasticsearch.search.aggregations.AggregatorFactories; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.index.query.AbstractQueryBuilder; -import org.elasticsearch.index.query.QueryBuilder; -import org.elasticsearch.search.aggregations.AggregatorFactories; import org.elasticsearch.xpack.core.deprecation.LoggingDeprecationAccumulationHandler; import java.io.IOException; @@ -41,7 +41,7 @@ public static XContentObjectTransformer aggregatorT return new XContentObjectTransformer<>(registry, (p) -> { // Serializing a map creates an object, need to skip the start object for the aggregation parser XContentParser.Token token = p.nextToken(); - assert(XContentParser.Token.START_OBJECT.equals(token)); + assert (XContentParser.Token.START_OBJECT.equals(token)); return AggregatorFactories.parseAggregators(p); }); } @@ -80,12 +80,11 @@ public T fromMap(Map stringObjectMap, List deprecationWa return null; } LoggingDeprecationAccumulationHandler deprecationLogger = new LoggingDeprecationAccumulationHandler(); - try(XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().map(stringObjectMap); - XContentParser parser = XContentType.JSON - .xContent() - .createParser(registry, - deprecationLogger, - BytesReference.bytes(xContentBuilder).streamInput())) { + try ( + XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().map(stringObjectMap); + XContentParser parser = XContentType.JSON.xContent() + .createParser(registry, deprecationLogger, BytesReference.bytes(xContentBuilder).streamInput()) + ) { T retVal = parserFunction.apply(parser); deprecationWarnings.addAll(deprecationLogger.getDeprecations()); return retVal; @@ -96,7 +95,7 @@ public Map toMap(T object) throws IOException { if (object == null) { return null; } - try(XContentBuilder xContentBuilder = XContentFactory.jsonBuilder()) { + try (XContentBuilder xContentBuilder = XContentFactory.jsonBuilder()) { XContentBuilder content = object.toXContent(xContentBuilder, ToXContent.EMPTY_PARAMS); return XContentHelper.convertToMap(BytesReference.bytes(content), true, XContentType.JSON).v2(); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/time/DateTimeFormatterTimestampConverter.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/time/DateTimeFormatterTimestampConverter.java index 1f86c88f76f15..8bb28dd95597c 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/time/DateTimeFormatterTimestampConverter.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/time/DateTimeFormatterTimestampConverter.java @@ -49,11 +49,10 @@ private DateTimeFormatterTimestampConverter(DateTimeFormatter dateTimeFormatter, * (e.g. contains a date but not a time) */ public static TimestampConverter ofPattern(String pattern, ZoneId defaultTimezone) { - DateTimeFormatter formatter = new DateTimeFormatterBuilder() - .parseLenient() - .appendPattern(pattern) - .parseDefaulting(ChronoField.YEAR_OF_ERA, LocalDate.now(defaultTimezone).getYear()) - .toFormatter(); + DateTimeFormatter formatter = new DateTimeFormatterBuilder().parseLenient() + .appendPattern(pattern) + .parseDefaulting(ChronoField.YEAR_OF_ERA, LocalDate.now(defaultTimezone).getYear()) + .toFormatter(); String formattedTime = formatter.format(ZonedDateTime.ofInstant(Instant.ofEpochSecond(0), ZoneOffset.UTC)); try { @@ -61,13 +60,11 @@ public static TimestampConverter ofPattern(String pattern, ZoneId defaultTimezon boolean hasTimeZone = parsed.isSupported(ChronoField.INSTANT_SECONDS); if (hasTimeZone) { Instant.from(parsed); - } - else { + } else { LocalDateTime.from(parsed); } return new DateTimeFormatterTimestampConverter(formatter, hasTimeZone, defaultTimezone); - } - catch (DateTimeException e) { + } catch (DateTimeException e) { throw new IllegalArgumentException("Timestamp cannot be derived from pattern: " + pattern, e); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/MonitoringDeprecatedSettings.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/MonitoringDeprecatedSettings.java index 1a467a404cf01..13504627e0554 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/MonitoringDeprecatedSettings.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/MonitoringDeprecatedSettings.java @@ -23,15 +23,21 @@ private MonitoringDeprecatedSettings() {} // =================== // Deprecated in 7.16: - public static final Setting.AffixSetting TEMPLATE_CREATE_LEGACY_VERSIONS_SETTING = - Setting.affixKeySetting("xpack.monitoring.exporters.","index.template.create_legacy_templates", - (key) -> Setting.boolSetting(key, true, Property.Dynamic, Property.NodeScope, Property.Deprecated)); - public static final Setting.AffixSetting USE_INGEST_PIPELINE_SETTING = - Setting.affixKeySetting("xpack.monitoring.exporters.","use_ingest", - key -> Setting.boolSetting(key, true, Property.Dynamic, Property.NodeScope, Property.Deprecated)); - public static final Setting.AffixSetting PIPELINE_CHECK_TIMEOUT_SETTING = - Setting.affixKeySetting("xpack.monitoring.exporters.","index.pipeline.master_timeout", - (key) -> Setting.timeSetting(key, TimeValue.MINUS_ONE, Property.Dynamic, Property.NodeScope, Property.Deprecated)); + public static final Setting.AffixSetting TEMPLATE_CREATE_LEGACY_VERSIONS_SETTING = Setting.affixKeySetting( + "xpack.monitoring.exporters.", + "index.template.create_legacy_templates", + (key) -> Setting.boolSetting(key, true, Property.Dynamic, Property.NodeScope, Property.Deprecated) + ); + public static final Setting.AffixSetting USE_INGEST_PIPELINE_SETTING = Setting.affixKeySetting( + "xpack.monitoring.exporters.", + "use_ingest", + key -> Setting.boolSetting(key, true, Property.Dynamic, Property.NodeScope, Property.Deprecated) + ); + public static final Setting.AffixSetting PIPELINE_CHECK_TIMEOUT_SETTING = Setting.affixKeySetting( + "xpack.monitoring.exporters.", + "index.pipeline.master_timeout", + (key) -> Setting.timeSetting(key, TimeValue.MINUS_ONE, Property.Dynamic, Property.NodeScope, Property.Deprecated) + ); // =================== public static List> getSettings() { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/MonitoringFeatureSetUsage.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/MonitoringFeatureSetUsage.java index 127f149c11cdc..6f70423702f71 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/MonitoringFeatureSetUsage.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/MonitoringFeatureSetUsage.java @@ -7,9 +7,9 @@ package org.elasticsearch.xpack.core.monitoring; import org.elasticsearch.Version; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.XPackFeatureSet; import org.elasticsearch.xpack.core.XPackField; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/MonitoringField.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/MonitoringField.java index 934e40b500757..069ab9f23e0a6 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/MonitoringField.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/MonitoringField.java @@ -28,10 +28,14 @@ public final class MonitoringField { * * @see MonitoringField#HISTORY_DURATION_MINIMUM */ - public static final Setting HISTORY_DURATION = timeSetting("xpack.monitoring.history.duration", - TimeValue.timeValueHours(7 * 24), // default value (7 days) - HISTORY_DURATION_MINIMUM, // minimum value - Setting.Property.Dynamic, Setting.Property.NodeScope, Setting.Property.Deprecated); + public static final Setting HISTORY_DURATION = timeSetting( + "xpack.monitoring.history.duration", + TimeValue.timeValueHours(7 * 24), // default value (7 days) + HISTORY_DURATION_MINIMUM, // minimum value + Setting.Property.Dynamic, + Setting.Property.NodeScope, + Setting.Property.Deprecated + ); private MonitoringField() {} } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/action/MonitoringBulkAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/action/MonitoringBulkAction.java index 4d593f88cbfe5..919403f3e09a0 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/action/MonitoringBulkAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/action/MonitoringBulkAction.java @@ -17,4 +17,3 @@ private MonitoringBulkAction() { super(NAME, MonitoringBulkResponse::new); } } - diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/action/MonitoringBulkDoc.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/action/MonitoringBulkDoc.java index 2a3d40d871166..d876466e0e3c4 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/action/MonitoringBulkDoc.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/action/MonitoringBulkDoc.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.xpack.core.monitoring.action; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; @@ -14,6 +13,7 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; @@ -30,13 +30,15 @@ public class MonitoringBulkDoc implements Writeable { private final BytesReference source; private final XContentType xContentType; - public MonitoringBulkDoc(final MonitoredSystem system, - final String type, - @Nullable final String id, - final long timestamp, - final long intervalMillis, - final BytesReference source, - final XContentType xContentType) { + public MonitoringBulkDoc( + final MonitoredSystem system, + final String type, + @Nullable final String id, + final long timestamp, + final long intervalMillis, + final BytesReference source, + final XContentType xContentType + ) { this.system = Objects.requireNonNull(system); this.type = Objects.requireNonNull(type); @@ -48,7 +50,7 @@ public MonitoringBulkDoc(final MonitoredSystem system, this.xContentType = Objects.requireNonNull(xContentType); } - public MonitoringBulkDoc (StreamInput in) throws IOException { + public MonitoringBulkDoc(StreamInput in) throws IOException { this.system = MonitoredSystem.fromSystem(in.readOptionalString()); this.timestamp = in.readVLong(); @@ -111,12 +113,12 @@ public boolean equals(Object o) { } MonitoringBulkDoc that = (MonitoringBulkDoc) o; return timestamp == that.timestamp - && intervalMillis == that.intervalMillis - && system == that.system - && Objects.equals(type, that.type) - && Objects.equals(id, that.id) - && Objects.equals(source, that.source) - && xContentType == that.xContentType; + && intervalMillis == that.intervalMillis + && system == that.system + && Objects.equals(type, that.type) + && Objects.equals(id, that.id) + && Objects.equals(source, that.source) + && xContentType == that.xContentType; } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/action/MonitoringBulkRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/action/MonitoringBulkRequest.java index eafec818b5e8e..9adebf3031248 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/action/MonitoringBulkRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/action/MonitoringBulkRequest.java @@ -9,10 +9,10 @@ import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.bulk.BulkRequestParser; -import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; @@ -74,33 +74,43 @@ public MonitoringBulkRequest add(MonitoringBulkDoc doc) { /** * Parses a monitoring bulk request and builds the list of documents to be indexed. */ - public MonitoringBulkRequest add(final MonitoredSystem system, - final BytesReference content, - final XContentType xContentType, - final long timestamp, - final long intervalMillis) throws IOException { + public MonitoringBulkRequest add( + final MonitoredSystem system, + final BytesReference content, + final XContentType xContentType, + final long timestamp, + final long intervalMillis + ) throws IOException { // MonitoringBulkRequest accepts a body request that has the same format as the BulkRequest - new BulkRequestParser(false, RestApiVersion.current()) - .parse(content, null, null, null, null, null, true, xContentType, + new BulkRequestParser(false, RestApiVersion.current()).parse( + content, + null, + null, + null, + null, + null, + true, + xContentType, (indexRequest, type) -> { - // we no longer accept non-timestamped indexes from Kibana, LS, or Beats because we do not use the data - // and it was duplicated anyway; by simply dropping it, we allow BWC for older clients that still send it - if (MonitoringIndex.from(indexRequest.index()) != MonitoringIndex.TIMESTAMPED) { - return; - } - final BytesReference source = indexRequest.source(); - if (source.length() == 0) { - throw new IllegalArgumentException("source is missing for monitoring document [" - + indexRequest.index() + "][" + type + "][" + indexRequest.id() + "]"); - } + // we no longer accept non-timestamped indexes from Kibana, LS, or Beats because we do not use the data + // and it was duplicated anyway; by simply dropping it, we allow BWC for older clients that still send it + if (MonitoringIndex.from(indexRequest.index()) != MonitoringIndex.TIMESTAMPED) { + return; + } + final BytesReference source = indexRequest.source(); + if (source.length() == 0) { + throw new IllegalArgumentException( + "source is missing for monitoring document [" + indexRequest.index() + "][" + type + "][" + indexRequest.id() + "]" + ); + } - // builds a new monitoring document based on the index request - add(new MonitoringBulkDoc(system, type, indexRequest.id(), timestamp, intervalMillis, source, - xContentType)); - }, - updateRequest -> { throw new IllegalArgumentException("monitoring bulk requests should only contain index requests"); }, - deleteRequest -> { throw new IllegalArgumentException("monitoring bulk requests should only contain index requests"); }); + // builds a new monitoring document based on the index request + add(new MonitoringBulkDoc(system, type, indexRequest.id(), timestamp, intervalMillis, source, xContentType)); + }, + updateRequest -> { throw new IllegalArgumentException("monitoring bulk requests should only contain index requests"); }, + deleteRequest -> { throw new IllegalArgumentException("monitoring bulk requests should only contain index requests"); } + ); return this; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/action/MonitoringBulkRequestBuilder.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/action/MonitoringBulkRequestBuilder.java index b58940fa6a490..0eca2078094fd 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/action/MonitoringBulkRequestBuilder.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/action/MonitoringBulkRequestBuilder.java @@ -14,8 +14,7 @@ import java.io.IOException; -public class MonitoringBulkRequestBuilder - extends ActionRequestBuilder { +public class MonitoringBulkRequestBuilder extends ActionRequestBuilder { public MonitoringBulkRequestBuilder(ElasticsearchClient client) { super(client, MonitoringBulkAction.INSTANCE, new MonitoringBulkRequest()); @@ -26,11 +25,13 @@ public MonitoringBulkRequestBuilder add(MonitoringBulkDoc doc) { return this; } - public MonitoringBulkRequestBuilder add(final MonitoredSystem system, - final BytesReference content, - final XContentType xContentType, - final long timestamp, - final long intervalMillis) throws IOException { + public MonitoringBulkRequestBuilder add( + final MonitoredSystem system, + final BytesReference content, + final XContentType xContentType, + final long timestamp, + final long intervalMillis + ) throws IOException { request.add(system, content, xContentType, timestamp, intervalMillis); return this; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/action/MonitoringBulkResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/action/MonitoringBulkResponse.java index 2c115863457e2..c981c01e807a7 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/action/MonitoringBulkResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/action/MonitoringBulkResponse.java @@ -13,9 +13,9 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.rest.RestStatus; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.rest.RestStatus; import java.io.IOException; import java.util.Objects; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/action/MonitoringIndex.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/action/MonitoringIndex.java index 66bf33cfc1021..f021ab0b1b58e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/action/MonitoringIndex.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/action/MonitoringIndex.java @@ -42,7 +42,7 @@ public boolean matchesIndexName(String indexName) { @Override public void writeTo(StreamOutput out) throws IOException { - out.writeByte((byte)ordinal()); + out.writeByte((byte) ordinal()); } public static MonitoringIndex readFrom(StreamInput in) throws IOException { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/action/MonitoringMigrateAlertsResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/action/MonitoringMigrateAlertsResponse.java index 4776bd3a22645..1e88ee7624a16 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/action/MonitoringMigrateAlertsResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/action/MonitoringMigrateAlertsResponse.java @@ -9,10 +9,10 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.action.ActionResponse; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -41,9 +41,7 @@ public void writeTo(StreamOutput out) throws IOException { @Override public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { - return builder.startObject() - .array("exporters", exporters) - .endObject(); + return builder.startObject().array("exporters", exporters).endObject(); } public List getExporters() { @@ -65,9 +63,7 @@ public int hashCode() { @Override public String toString() { - return "MonitoringMigrateAlertsResponse{" + - "exporters=" + exporters + - '}'; + return "MonitoringMigrateAlertsResponse{" + "exporters=" + exporters + '}'; } public static class ExporterMigrationResult implements Writeable, ToXContentObject { @@ -137,9 +133,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; ExporterMigrationResult that = (ExporterMigrationResult) o; - return migrationComplete == that.migrationComplete && - Objects.equals(name, that.name) && - Objects.equals(type, that.type); + return migrationComplete == that.migrationComplete && Objects.equals(name, that.name) && Objects.equals(type, that.type); } @Override @@ -149,12 +143,18 @@ public int hashCode() { @Override public String toString() { - return "ExporterMigrationResult{" + - "name='" + name + '\'' + - ", type='" + type + '\'' + - ", migrationComplete=" + migrationComplete + - ", reason=" + reason + - '}'; + return "ExporterMigrationResult{" + + "name='" + + name + + '\'' + + ", type='" + + type + + '\'' + + ", migrationComplete=" + + migrationComplete + + ", reason=" + + reason + + '}'; } } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/exporter/MonitoringDoc.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/exporter/MonitoringDoc.java index 5f44456ca9f5d..219d422685e0a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/exporter/MonitoringDoc.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/exporter/MonitoringDoc.java @@ -6,11 +6,11 @@ */ package org.elasticsearch.xpack.core.monitoring.exporter; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.time.DateFormatter; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; @@ -33,13 +33,15 @@ public abstract class MonitoringDoc implements ToXContentObject { private final String type; private final String id; - public MonitoringDoc(final String cluster, - final long timestamp, - final long intervalMillis, - @Nullable final Node node, - final MonitoredSystem system, - final String type, - @Nullable final String id) { + public MonitoringDoc( + final String cluster, + final long timestamp, + final long intervalMillis, + @Nullable final Node node, + final MonitoredSystem system, + final String type, + @Nullable final String id + ) { this.cluster = Objects.requireNonNull(cluster); this.timestamp = timestamp; @@ -88,12 +90,12 @@ public boolean equals(Object o) { } MonitoringDoc that = (MonitoringDoc) o; return timestamp == that.timestamp - && intervalMillis == that.intervalMillis - && Objects.equals(cluster, that.cluster) - && Objects.equals(node, that.node) - && system == that.system - && Objects.equals(type, that.type) - && Objects.equals(id, that.id); + && intervalMillis == that.intervalMillis + && Objects.equals(cluster, that.cluster) + && Objects.equals(node, that.node) + && system == that.system + && Objects.equals(type, that.type) + && Objects.equals(id, that.id); } @Override @@ -140,12 +142,14 @@ public static class Node implements Writeable, ToXContentObject { private final String name; private final long timestamp; - public Node(final String uuid, - final String host, - final String transportAddress, - final String ip, - final String name, - final long timestamp) { + public Node( + final String uuid, + final String host, + final String transportAddress, + final String ip, + final String name, + final long timestamp + ) { this.uuid = uuid; this.host = host; this.transportAddress = transportAddress; @@ -224,11 +228,11 @@ public boolean equals(Object o) { } Node node = (Node) o; return Objects.equals(uuid, node.uuid) - && Objects.equals(host, node.host) - && Objects.equals(transportAddress, node.transportAddress) - && Objects.equals(ip, node.ip) - && Objects.equals(name, node.name) - && Objects.equals(timestamp, node.timestamp); + && Objects.equals(host, node.host) + && Objects.equals(transportAddress, node.transportAddress) + && Objects.equals(ip, node.ip) + && Objects.equals(name, node.name) + && Objects.equals(timestamp, node.timestamp); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/exporter/MonitoringTemplateUtils.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/exporter/MonitoringTemplateUtils.java index 45ecfdc958d97..26f5a2666c946 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/exporter/MonitoringTemplateUtils.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/exporter/MonitoringTemplateUtils.java @@ -35,7 +35,7 @@ public final class MonitoringTemplateUtils { */ public static final String OLD_TEMPLATE_VERSION = "6"; - private MonitoringTemplateUtils() { } + private MonitoringTemplateUtils() {} /** * Get the index name given a specific date format, a monitored system and a timestamp. diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rest/action/RestReloadAnalyzersAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rest/action/RestReloadAnalyzersAction.java index aa6ebf754aa33..68f3bfee5f462 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rest/action/RestReloadAnalyzersAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rest/action/RestReloadAnalyzersAction.java @@ -25,9 +25,7 @@ public class RestReloadAnalyzersAction extends BaseRestHandler { @Override public List routes() { - return List.of( - new Route(GET, "/{index}/_reload_search_analyzers"), - new Route(POST, "/{index}/_reload_search_analyzers")); + return List.of(new Route(GET, "/{index}/_reload_search_analyzers"), new Route(POST, "/{index}/_reload_search_analyzers")); } @Override @@ -38,7 +36,8 @@ public String getName() { @Override public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException { ReloadAnalyzersRequest reloadAnalyzersRequest = new ReloadAnalyzersRequest( - Strings.splitStringByCommaToArray(request.param("index"))); + Strings.splitStringByCommaToArray(request.param("index")) + ); reloadAnalyzersRequest.indicesOptions(IndicesOptions.fromRequest(request, reloadAnalyzersRequest.indicesOptions())); return channel -> client.execute(ReloadAnalyzerAction.INSTANCE, reloadAnalyzersRequest, new RestToXContentListener<>(channel)); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rest/action/RestXPackInfoAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rest/action/RestXPackInfoAction.java index d1ede7294d9e4..c296e7a565429 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rest/action/RestXPackInfoAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rest/action/RestXPackInfoAction.java @@ -30,9 +30,7 @@ public class RestXPackInfoAction extends BaseRestHandler { @Override public List routes() { - return List.of( - new Route(GET, "/_xpack"), - new Route(HEAD, "/_xpack")); + return List.of(new Route(GET, "/_xpack"), new Route(HEAD, "/_xpack")); } @Override @@ -49,21 +47,23 @@ public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client // In 7.x, there was an opt-in flag to show "enterprise" licenses. In 8.0 the flag is deprecated and can only be true // TODO Remove this from 9.0 if (request.hasParam("accept_enterprise")) { - deprecationLogger.critical(DeprecationCategory.API, "get_license_accept_enterprise", - "Including [accept_enterprise] in get license requests is deprecated." + - " The parameter will be removed in the next major version"); + deprecationLogger.critical( + DeprecationCategory.API, + "get_license_accept_enterprise", + "Including [accept_enterprise] in get license requests is deprecated." + + " The parameter will be removed in the next major version" + ); if (request.paramAsBoolean("accept_enterprise", true) == false && request.getRestApiVersion().matches(onOrAfter(RestApiVersion.V_8))) { throw new IllegalArgumentException("The [accept_enterprise] parameters may not be false"); } } - EnumSet categories = XPackInfoRequest.Category - .toSet(request.paramAsStringArray("categories", new String[] { "_all" })); - return channel -> - new XPackInfoRequestBuilder(client) - .setVerbose(verbose) - .setCategories(categories) - .execute(new RestToXContentListener<>(channel)); + EnumSet categories = XPackInfoRequest.Category.toSet( + request.paramAsStringArray("categories", new String[] { "_all" }) + ); + return channel -> new XPackInfoRequestBuilder(client).setVerbose(verbose) + .setCategories(categories) + .execute(new RestToXContentListener<>(channel)); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rest/action/RestXPackUsageAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rest/action/RestXPackUsageAction.java index 8686e4f7a5177..efa508907ba82 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rest/action/RestXPackUsageAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rest/action/RestXPackUsageAction.java @@ -9,7 +9,6 @@ import org.elasticsearch.action.support.master.MasterNodeRequest; import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.http.HttpChannel; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.BytesRestResponse; @@ -17,6 +16,7 @@ import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.action.RestBuilderListener; import org.elasticsearch.rest.action.RestCancellableNodeClient; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.XPackFeatureSet; import org.elasticsearch.xpack.core.action.XPackUsageRequestBuilder; import org.elasticsearch.xpack.core.action.XPackUsageResponse; @@ -43,18 +43,18 @@ public String getName() { public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException { final TimeValue masterTimeout = request.paramAsTime("master_timeout", MasterNodeRequest.DEFAULT_MASTER_NODE_TIMEOUT); final HttpChannel httpChannel = request.getHttpChannel(); - return channel -> new XPackUsageRequestBuilder(new RestCancellableNodeClient(client, httpChannel)) - .setMasterNodeTimeout(masterTimeout) - .execute(new RestBuilderListener<>(channel) { - @Override - public RestResponse buildResponse(XPackUsageResponse response, XContentBuilder builder) throws Exception { - builder.startObject(); - for (XPackFeatureSet.Usage usage : response.getUsages()) { - builder.field(usage.name(), usage); - } - builder.endObject(); - return new BytesRestResponse(OK, builder); - } - }); + return channel -> new XPackUsageRequestBuilder(new RestCancellableNodeClient(client, httpChannel)).setMasterNodeTimeout( + masterTimeout + ).execute(new RestBuilderListener<>(channel) { + @Override + public RestResponse buildResponse(XPackUsageResponse response, XContentBuilder builder) throws Exception { + builder.startObject(); + for (XPackFeatureSet.Usage usage : response.getUsages()) { + builder.field(usage.name(), usage); + } + builder.endObject(); + return new BytesRestResponse(OK, builder); + } + }); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/RollupActionConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/RollupActionConfig.java index 8192614c17d02..315461fb2a93a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/RollupActionConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/RollupActionConfig.java @@ -8,12 +8,12 @@ import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.fieldcaps.FieldCapabilities; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.NamedWriteable; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -49,8 +49,11 @@ public class RollupActionConfig implements NamedWriteable, ToXContentObject { List metricsConfig = (List) args[1]; return new RollupActionConfig(groupConfig, metricsConfig); }); - PARSER.declareObject(optionalConstructorArg(), (p, c) -> RollupActionGroupConfig.fromXContent(p), - new ParseField(RollupActionGroupConfig.NAME)); + PARSER.declareObject( + optionalConstructorArg(), + (p, c) -> RollupActionGroupConfig.fromXContent(p), + new ParseField(RollupActionGroupConfig.NAME) + ); PARSER.declareObjectArray(optionalConstructorArg(), (p, c) -> MetricConfig.fromXContent(p), new ParseField(MetricConfig.NAME)); } @@ -95,8 +98,10 @@ public Set getAllFields() { return Collections.unmodifiableSet(fields); } - public void validateMappings(final Map> fieldCapsResponse, - final ActionRequestValidationException validationException) { + public void validateMappings( + final Map> fieldCapsResponse, + final ActionRequestValidationException validationException + ) { groupConfig.validateMappings(fieldCapsResponse, validationException); for (MetricConfig m : metricsConfig) { m.validateMappings(fieldCapsResponse, validationException); @@ -138,8 +143,7 @@ public boolean equals(Object other) { } final RollupActionConfig that = (RollupActionConfig) other; - return Objects.equals(this.groupConfig, that.groupConfig) - && Objects.equals(this.metricsConfig, that.metricsConfig); + return Objects.equals(this.groupConfig, that.groupConfig) && Objects.equals(this.metricsConfig, that.metricsConfig); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/RollupActionDateHistogramGroupConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/RollupActionDateHistogramGroupConfig.java index a537a6574802b..7ebc30e517a12 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/RollupActionDateHistogramGroupConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/RollupActionDateHistogramGroupConfig.java @@ -8,20 +8,20 @@ import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.fieldcaps.FieldCapabilities; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Rounding; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder; +import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder; -import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; import java.io.IOException; import java.time.ZoneId; @@ -73,10 +73,18 @@ public abstract class RollupActionDateHistogramGroupConfig implements Writeable, } }); PARSER.declareString(constructorArg(), new ParseField(FIELD)); - PARSER.declareField(optionalConstructorArg(), p -> new DateHistogramInterval(p.text()), - new ParseField(CALENDAR_INTERVAL), ValueType.STRING); - PARSER.declareField(optionalConstructorArg(), p -> new DateHistogramInterval(p.text()), - new ParseField(FIXED_INTERVAL), ValueType.STRING); + PARSER.declareField( + optionalConstructorArg(), + p -> new DateHistogramInterval(p.text()), + new ParseField(CALENDAR_INTERVAL), + ValueType.STRING + ); + PARSER.declareField( + optionalConstructorArg(), + p -> new DateHistogramInterval(p.text()), + new ParseField(FIXED_INTERVAL), + ValueType.STRING + ); PARSER.declareString(optionalConstructorArg(), new ParseField(TIME_ZONE)); } @@ -93,6 +101,7 @@ public abstract class RollupActionDateHistogramGroupConfig implements Writeable, */ public static class FixedInterval extends RollupActionDateHistogramGroupConfig { private static final String TYPE_NAME = "fixed_interval"; + public FixedInterval(String field, DateHistogramInterval interval) { this(field, interval, null); } @@ -119,6 +128,7 @@ public String getIntervalTypeName() { */ public static class CalendarInterval extends RollupActionDateHistogramGroupConfig { private static final String TYPE_NAME = "calendar_interval"; + public CalendarInterval(String field, DateHistogramInterval interval) { this(field, interval, null); } @@ -126,8 +136,9 @@ public CalendarInterval(String field, DateHistogramInterval interval) { public CalendarInterval(String field, DateHistogramInterval interval, String timeZone) { super(field, interval, timeZone); if (DateHistogramAggregationBuilder.DATE_FIELD_UNITS.get(interval.toString()) == null) { - throw new IllegalArgumentException("The supplied interval [" + interval +"] could not be parsed " + - "as a calendar interval."); + throw new IllegalArgumentException( + "The supplied interval [" + interval + "] could not be parsed " + "as a calendar interval." + ); } } @@ -155,9 +166,11 @@ protected RollupActionDateHistogramGroupConfig(final String field, final DateHis * @param interval the interval to use for the date histogram (required) * @param timeZone the id of time zone to use to calculate the date histogram (optional). When {@code null}, the UTC timezone is used. */ - protected RollupActionDateHistogramGroupConfig(final String field, - final DateHistogramInterval interval, - final @Nullable String timeZone) { + protected RollupActionDateHistogramGroupConfig( + final String field, + final DateHistogramInterval interval, + final @Nullable String timeZone + ) { if (field == null || field.isEmpty()) { throw new IllegalArgumentException("Field must be a non-null, non-empty string"); } @@ -235,8 +248,10 @@ public Rounding.Prepared createRounding() { public abstract String getIntervalTypeName(); - public void validateMappings(Map> fieldCapsResponse, - ActionRequestValidationException validationException) { + public void validateMappings( + Map> fieldCapsResponse, + ActionRequestValidationException validationException + ) { Map fieldCaps = fieldCapsResponse.get(field); if (fieldCaps != null && fieldCaps.isEmpty() == false) { boolean matchesDateType = false; @@ -246,20 +261,30 @@ public void validateMappings(Map> fieldCa if (fieldCaps.get(dateType).isAggregatable()) { return; } else { - validationException.addValidationError("The field [" + field + "] must be aggregatable, " + - "but is not."); + validationException.addValidationError("The field [" + field + "] must be aggregatable, " + "but is not."); } } } if (matchesDateType == false) { - validationException.addValidationError("The field referenced by a date_histo group must be one of type [" + - Strings.collectionToCommaDelimitedString(RollupField.DATE_FIELD_MAPPER_TYPES) + "]." + - " Found: " + fieldCaps.keySet().toString() + " for field [" + field + "]"); + validationException.addValidationError( + "The field referenced by a date_histo group must be one of type [" + + Strings.collectionToCommaDelimitedString(RollupField.DATE_FIELD_MAPPER_TYPES) + + "]." + + " Found: " + + fieldCaps.keySet().toString() + + " for field [" + + field + + "]" + ); } } else { - validationException.addValidationError("Could not find one of [" + - Strings.collectionToCommaDelimitedString(RollupField.DATE_FIELD_MAPPER_TYPES) + "] fields with name [" + - field + "]."); + validationException.addValidationError( + "Could not find one of [" + + Strings.collectionToCommaDelimitedString(RollupField.DATE_FIELD_MAPPER_TYPES) + + "] fields with name [" + + field + + "]." + ); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/RollupActionGroupConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/RollupActionGroupConfig.java index 8939f15e3380b..48b728199d297 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/RollupActionGroupConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/RollupActionGroupConfig.java @@ -8,13 +8,13 @@ import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.fieldcaps.FieldCapabilities; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -49,15 +49,25 @@ public class RollupActionGroupConfig implements Writeable, ToXContentObject { public static final String NAME = "groups"; private static final ConstructingObjectParser PARSER; static { - PARSER = new ConstructingObjectParser<>(NAME, args -> - new RollupActionGroupConfig((RollupActionDateHistogramGroupConfig) args[0], (HistogramGroupConfig) args[1], - (TermsGroupConfig) args[2])); - PARSER.declareObject(constructorArg(), - (p, c) -> RollupActionDateHistogramGroupConfig.fromXContent(p), new ParseField(RollupActionDateHistogramGroupConfig.NAME)); - PARSER.declareObject(optionalConstructorArg(), - (p, c) -> HistogramGroupConfig.fromXContent(p), new ParseField(HistogramGroupConfig.NAME)); - PARSER.declareObject(optionalConstructorArg(), - (p, c) -> TermsGroupConfig.fromXContent(p), new ParseField(TermsGroupConfig.NAME)); + PARSER = new ConstructingObjectParser<>( + NAME, + args -> new RollupActionGroupConfig( + (RollupActionDateHistogramGroupConfig) args[0], + (HistogramGroupConfig) args[1], + (TermsGroupConfig) args[2] + ) + ); + PARSER.declareObject( + constructorArg(), + (p, c) -> RollupActionDateHistogramGroupConfig.fromXContent(p), + new ParseField(RollupActionDateHistogramGroupConfig.NAME) + ); + PARSER.declareObject( + optionalConstructorArg(), + (p, c) -> HistogramGroupConfig.fromXContent(p), + new ParseField(HistogramGroupConfig.NAME) + ); + PARSER.declareObject(optionalConstructorArg(), (p, c) -> TermsGroupConfig.fromXContent(p), new ParseField(TermsGroupConfig.NAME)); } private final RollupActionDateHistogramGroupConfig dateHistogram; @@ -68,9 +78,11 @@ public RollupActionGroupConfig(final RollupActionDateHistogramGroupConfig dateHi this(dateHistogram, null, null); } - public RollupActionGroupConfig(final RollupActionDateHistogramGroupConfig dateHistogram, - final @Nullable HistogramGroupConfig histogram, - final @Nullable TermsGroupConfig terms) { + public RollupActionGroupConfig( + final RollupActionDateHistogramGroupConfig dateHistogram, + final @Nullable HistogramGroupConfig histogram, + final @Nullable TermsGroupConfig terms + ) { if (dateHistogram == null) { throw new IllegalArgumentException("Date histogram must not be null"); } @@ -120,8 +132,10 @@ public Set getAllFields() { return Collections.unmodifiableSet(fields); } - public void validateMappings(final Map> fieldCapsResponse, - final ActionRequestValidationException validationException) { + public void validateMappings( + final Map> fieldCapsResponse, + final ActionRequestValidationException validationException + ) { dateHistogram.validateMappings(fieldCapsResponse, validationException); if (histogram != null) { histogram.validateMappings(fieldCapsResponse, validationException); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/RollupField.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/RollupField.java index 1da661d2c249a..4f9c7f52594ca 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/RollupField.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/RollupField.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.xpack.core.rollup; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.index.mapper.DateFieldMapper; import org.elasticsearch.index.mapper.NumberFieldMapper; import org.elasticsearch.search.aggregations.metrics.AvgAggregationBuilder; @@ -15,6 +14,7 @@ import org.elasticsearch.search.aggregations.metrics.SumAggregationBuilder; import org.elasticsearch.search.aggregations.metrics.ValueCountAggregationBuilder; import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder; +import org.elasticsearch.xcontent.ParseField; import java.util.Arrays; import java.util.HashSet; @@ -38,11 +38,18 @@ public class RollupField { public static final String TYPE_NAME = "_doc"; public static final String AGG = "agg"; public static final String ROLLUP_MISSING = "ROLLUP_MISSING_40710B25931745D4B0B8B310F6912A69"; - public static final List SUPPORTED_NUMERIC_METRICS = Arrays.asList(MaxAggregationBuilder.NAME, MinAggregationBuilder.NAME, - SumAggregationBuilder.NAME, AvgAggregationBuilder.NAME, ValueCountAggregationBuilder.NAME); - public static final List SUPPORTED_DATE_METRICS = Arrays.asList(MaxAggregationBuilder.NAME, + public static final List SUPPORTED_NUMERIC_METRICS = Arrays.asList( + MaxAggregationBuilder.NAME, + MinAggregationBuilder.NAME, + SumAggregationBuilder.NAME, + AvgAggregationBuilder.NAME, + ValueCountAggregationBuilder.NAME + ); + public static final List SUPPORTED_DATE_METRICS = Arrays.asList( + MaxAggregationBuilder.NAME, MinAggregationBuilder.NAME, - ValueCountAggregationBuilder.NAME); + ValueCountAggregationBuilder.NAME + ); // a set of ALL our supported metrics, to be a union of all other supported metric types (numeric, date, etc.) public static final Set SUPPORTED_METRICS; @@ -62,8 +69,10 @@ public class RollupField { NUMERIC_FIELD_MAPPER_TYPES = types; } - public static final List DATE_FIELD_MAPPER_TYPES = List.of(DateFieldMapper.CONTENT_TYPE, - DateFieldMapper.DATE_NANOS_CONTENT_TYPE); + public static final List DATE_FIELD_MAPPER_TYPES = List.of( + DateFieldMapper.CONTENT_TYPE, + DateFieldMapper.DATE_NANOS_CONTENT_TYPE + ); /** * Format to the appropriate Rollup field name convention diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/DeleteRollupJobAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/DeleteRollupJobAction.java index ee4f9fc9a377b..7da0a6fba289c 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/DeleteRollupJobAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/DeleteRollupJobAction.java @@ -6,10 +6,9 @@ */ package org.elasticsearch.xpack.core.rollup.action; - -import org.elasticsearch.action.ActionType; import org.elasticsearch.action.ActionRequestBuilder; import org.elasticsearch.action.ActionRequestValidationException; +import org.elasticsearch.action.ActionType; import org.elasticsearch.action.FailedNodeException; import org.elasticsearch.action.TaskOperationFailure; import org.elasticsearch.action.support.tasks.BaseTasksRequest; @@ -18,10 +17,10 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.tasks.Task; import org.elasticsearch.xcontent.ToXContentFragment; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.tasks.Task; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; import org.elasticsearch.xpack.core.rollup.RollupField; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/GetRollupCapsAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/GetRollupCapsAction.java index f05acd96d0c38..7ee7b88884456 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/GetRollupCapsAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/GetRollupCapsAction.java @@ -13,11 +13,11 @@ import org.elasticsearch.action.ActionType; import org.elasticsearch.client.ElasticsearchClient; import org.elasticsearch.cluster.metadata.Metadata; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentFragment; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/GetRollupIndexCapsAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/GetRollupIndexCapsAction.java index cadbb0615fad1..56336ce8d5ab0 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/GetRollupIndexCapsAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/GetRollupIndexCapsAction.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.xpack.core.rollup.action; - import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionRequestBuilder; import org.elasticsearch.action.ActionRequestValidationException; @@ -15,11 +14,11 @@ import org.elasticsearch.action.IndicesRequest; import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.client.ElasticsearchClient; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentFragment; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -122,8 +121,7 @@ public boolean equals(Object obj) { return false; } Request other = (Request) obj; - return Arrays.equals(indices, other.indices) - && Objects.equals(options, other.options); + return Arrays.equals(indices, other.indices) && Objects.equals(options, other.options); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/GetRollupJobsAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/GetRollupJobsAction.java index 83bff449c28d2..9e2d611590f5c 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/GetRollupJobsAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/GetRollupJobsAction.java @@ -6,25 +6,24 @@ */ package org.elasticsearch.xpack.core.rollup.action; - -import org.elasticsearch.action.ActionType; import org.elasticsearch.action.ActionRequestBuilder; import org.elasticsearch.action.ActionRequestValidationException; +import org.elasticsearch.action.ActionType; import org.elasticsearch.action.FailedNodeException; import org.elasticsearch.action.TaskOperationFailure; import org.elasticsearch.action.support.tasks.BaseTasksRequest; import org.elasticsearch.action.support.tasks.BaseTasksResponse; import org.elasticsearch.client.ElasticsearchClient; import org.elasticsearch.cluster.metadata.Metadata; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.tasks.Task; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.tasks.Task; import org.elasticsearch.xpack.core.rollup.RollupField; import org.elasticsearch.xpack.core.rollup.job.RollupIndexerJobStats; import org.elasticsearch.xpack.core.rollup.job.RollupJobConfig; @@ -200,9 +199,10 @@ public static class JobWrapper implements Writeable, ToXContentObject { private final RollupIndexerJobStats stats; private final RollupJobStatus status; - public static final ConstructingObjectParser PARSER - = new ConstructingObjectParser<>(NAME, a -> new JobWrapper((RollupJobConfig) a[0], - (RollupIndexerJobStats) a[1], (RollupJobStatus)a[2])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + a -> new JobWrapper((RollupJobConfig) a[0], (RollupIndexerJobStats) a[1], (RollupJobStatus) a[2]) + ); static { PARSER.declareObject(ConstructingObjectParser.constructorArg(), (p, c) -> RollupJobConfig.fromXContent(p, null), CONFIG); @@ -266,9 +266,7 @@ public boolean equals(Object obj) { return false; } JobWrapper other = (JobWrapper) obj; - return Objects.equals(job, other.job) - && Objects.equals(stats, other.stats) - && Objects.equals(status, other.status); + return Objects.equals(job, other.job) && Objects.equals(stats, other.stats) && Objects.equals(status, other.status); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/PutRollupJobAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/PutRollupJobAction.java index d1e7a0ea08066..75d39d7099867 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/PutRollupJobAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/PutRollupJobAction.java @@ -96,7 +96,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws @Override public String[] indices() { - return new String[]{this.config.getIndexPattern()}; + return new String[] { this.config.getIndexPattern() }; } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/RollableIndexCaps.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/RollableIndexCaps.java index 819ff7fa6bf99..d18b196449b4c 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/RollableIndexCaps.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/RollableIndexCaps.java @@ -6,10 +6,10 @@ */ package org.elasticsearch.xpack.core.rollup.action; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -35,10 +35,9 @@ public class RollableIndexCaps implements Writeable, ToXContentObject { public RollableIndexCaps(String indexName, List caps) { this.indexName = indexName; - this.jobCaps = Collections.unmodifiableList(Objects.requireNonNull(caps) - .stream() - .sorted(Comparator.comparing(RollupJobCaps::getJobID)) - .collect(Collectors.toList())); + this.jobCaps = Collections.unmodifiableList( + Objects.requireNonNull(caps).stream().sorted(Comparator.comparing(RollupJobCaps::getJobID)).collect(Collectors.toList()) + ); } public RollableIndexCaps(StreamInput in) throws IOException { @@ -82,8 +81,7 @@ public boolean equals(Object other) { RollableIndexCaps that = (RollableIndexCaps) other; - return Objects.equals(this.jobCaps, that.jobCaps) - && Objects.equals(this.indexName, that.indexName); + return Objects.equals(this.jobCaps, that.jobCaps) && Objects.equals(this.indexName, that.indexName); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/RollupAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/RollupAction.java index fe3ab3c654e41..5b0ebb0bd67ee 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/RollupAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/RollupAction.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.xpack.core.rollup.action; - import org.elasticsearch.action.ActionRequestBuilder; import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.ActionType; @@ -17,10 +16,10 @@ import org.elasticsearch.client.ElasticsearchClient; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.xcontent.ToXContentObject; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.tasks.Task; import org.elasticsearch.tasks.TaskId; +import org.elasticsearch.xcontent.ToXContentObject; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.rollup.RollupActionConfig; import java.io.IOException; @@ -46,8 +45,7 @@ public Request(String sourceIndex, String rollupIndex, RollupActionConfig rollup this.rollupConfig = rollupConfig; } - public Request() { - } + public Request() {} public Request(StreamInput in) throws IOException { super(in); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/RollupActionRequestValidationException.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/RollupActionRequestValidationException.java index 8da6519664967..3401b552929fc 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/RollupActionRequestValidationException.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/RollupActionRequestValidationException.java @@ -8,5 +8,4 @@ import org.elasticsearch.action.ActionRequestValidationException; -public class RollupActionRequestValidationException extends ActionRequestValidationException { -} +public class RollupActionRequestValidationException extends ActionRequestValidationException {} diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/RollupIndexerAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/RollupIndexerAction.java index 49ac479211d31..67e4cfd08f089 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/RollupIndexerAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/RollupIndexerAction.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.xpack.core.rollup.action; - import org.elasticsearch.action.ActionRequestBuilder; import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.ActionType; @@ -20,11 +19,11 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.ToXContentObject; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.tasks.Task; import org.elasticsearch.tasks.TaskId; +import org.elasticsearch.xcontent.ToXContentObject; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.rollup.RollupActionConfig; import java.io.IOException; @@ -46,8 +45,7 @@ public Request(RollupAction.Request rollupRequest) { this.rollupRequest = rollupRequest; } - public Request() { - } + public Request() {} public Request(StreamInput in) throws IOException { super(in); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/RollupJobCaps.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/RollupJobCaps.java index 044841f5e7016..cc241e9b10406 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/RollupJobCaps.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/RollupJobCaps.java @@ -6,16 +6,16 @@ */ package org.elasticsearch.xpack.core.rollup.action; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.ToXContentFragment; -import org.elasticsearch.xcontent.ToXContentObject; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder; import org.elasticsearch.search.aggregations.bucket.histogram.HistogramAggregationBuilder; import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.ToXContentFragment; +import org.elasticsearch.xcontent.ToXContentObject; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.rollup.job.DateHistogramGroupConfig; import org.elasticsearch.xpack.core.rollup.job.GroupConfig; import org.elasticsearch.xpack.core.rollup.job.HistogramGroupConfig; @@ -50,7 +50,7 @@ public class RollupJobCaps implements Writeable, ToXContentObject { private final Map fieldCapLookup; // TODO now that these rollup caps are being used more widely (e.g. search), perhaps we should - // store the RollupJob and translate into FieldCaps on demand for json output. Would make working with + // store the RollupJob and translate into FieldCaps on demand for json output. Would make working with // it internally a lot easier public RollupJobCaps(RollupJobConfig job) { jobID = job.getId(); @@ -185,24 +185,23 @@ private static Map createRollupFieldCaps(final RollupJo // Create RollupFieldCaps for the metrics final List metricsConfig = rollupJobConfig.getMetricsConfig(); - if (metricsConfig.size() > 0) { - rollupJobConfig.getMetricsConfig().forEach(metricConfig -> { - final List> metrics = metricConfig.getMetrics().stream() - .map(metric -> singletonMap("agg", (Object) metric)) - .collect(Collectors.toList()); - metrics.forEach(m -> { - List> caps = tempFieldCaps - .getOrDefault(metricConfig.getField(), new ArrayList<>()); - caps.add(m); - tempFieldCaps.put(metricConfig.getField(), caps); - }); + if (metricsConfig.size() > 0) { + rollupJobConfig.getMetricsConfig().forEach(metricConfig -> { + final List> metrics = metricConfig.getMetrics() + .stream() + .map(metric -> singletonMap("agg", (Object) metric)) + .collect(Collectors.toList()); + metrics.forEach(m -> { + List> caps = tempFieldCaps.getOrDefault(metricConfig.getField(), new ArrayList<>()); + caps.add(m); + tempFieldCaps.put(metricConfig.getField(), caps); }); + }); } - return Collections.unmodifiableMap(tempFieldCaps.entrySet() - .stream() - .collect(Collectors.toMap(Map.Entry::getKey, - e -> new RollupFieldCaps(e.getValue())))); + return Collections.unmodifiableMap( + tempFieldCaps.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> new RollupFieldCaps(e.getValue()))) + ); } public static class RollupFieldCaps implements Writeable, ToXContentFragment { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/RollupTask.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/RollupTask.java index 87d4576da29a1..d4f3b43852831 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/RollupTask.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/RollupTask.java @@ -23,8 +23,15 @@ public class RollupTask extends CancellableTask { private RollupActionConfig config; private RollupJobStatus status; - RollupTask(long id, String type, String action, TaskId parentTask, String rollupIndex, RollupActionConfig config, - Map headers) { + RollupTask( + long id, + String type, + String action, + TaskId parentTask, + String rollupIndex, + RollupActionConfig config, + Map headers + ) { super(id, type, action, RollupField.NAME + "_" + rollupIndex, parentTask, headers); this.rollupIndex = rollupIndex; this.config = config; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/StartRollupJobAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/StartRollupJobAction.java index 3b413fd85f52e..b787cb0b8f972 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/StartRollupJobAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/StartRollupJobAction.java @@ -6,10 +6,9 @@ */ package org.elasticsearch.xpack.core.rollup.action; - -import org.elasticsearch.action.ActionType; import org.elasticsearch.action.ActionRequestBuilder; import org.elasticsearch.action.ActionRequestValidationException; +import org.elasticsearch.action.ActionType; import org.elasticsearch.action.support.tasks.BaseTasksRequest; import org.elasticsearch.action.support.tasks.BaseTasksResponse; import org.elasticsearch.client.ElasticsearchClient; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/StopRollupJobAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/StopRollupJobAction.java index b804bcae78659..d667599dcc515 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/StopRollupJobAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/action/StopRollupJobAction.java @@ -6,18 +6,18 @@ */ package org.elasticsearch.xpack.core.rollup.action; -import org.elasticsearch.action.ActionType; import org.elasticsearch.action.ActionRequestBuilder; import org.elasticsearch.action.ActionRequestValidationException; +import org.elasticsearch.action.ActionType; import org.elasticsearch.action.support.tasks.BaseTasksRequest; import org.elasticsearch.action.support.tasks.BaseTasksResponse; import org.elasticsearch.client.ElasticsearchClient; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; @@ -45,7 +45,7 @@ public static class Request extends BaseTasksRequest implements ToXCont private boolean waitForCompletion = false; private TimeValue timeout = null; - public Request (String id) { + public Request(String id) { this(id, false, null); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/job/DateHistogramGroupConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/job/DateHistogramGroupConfig.java index b9f39abab724a..7a460b61ac9d9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/job/DateHistogramGroupConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/job/DateHistogramGroupConfig.java @@ -8,20 +8,20 @@ import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.fieldcaps.FieldCapabilities; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Rounding; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder; +import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder; -import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; import org.elasticsearch.xpack.core.rollup.RollupField; import java.io.IOException; @@ -70,9 +70,10 @@ public abstract class DateHistogramGroupConfig implements Writeable, ToXContentO DateHistogramInterval fixedInterval = (DateHistogramInterval) a[3]; if (oldInterval != null) { - if (calendarInterval != null || fixedInterval != null) { - throw new IllegalArgumentException("Cannot use [interval] with [fixed_interval] or [calendar_interval] " + - "configuration options."); + if (calendarInterval != null || fixedInterval != null) { + throw new IllegalArgumentException( + "Cannot use [interval] with [fixed_interval] or [calendar_interval] " + "configuration options." + ); } return fromUnknownTimeUnit((String) a[0], oldInterval, (DateHistogramInterval) a[4], (String) a[5]); } else if (calendarInterval != null && fixedInterval == null) { @@ -87,11 +88,19 @@ public abstract class DateHistogramGroupConfig implements Writeable, ToXContentO }); PARSER.declareString(constructorArg(), new ParseField(FIELD)); PARSER.declareField(optionalConstructorArg(), p -> new DateHistogramInterval(p.text()), new ParseField(INTERVAL), ValueType.STRING); - PARSER.declareField(optionalConstructorArg(), p -> new DateHistogramInterval(p.text()), - new ParseField(CALENDAR_INTERVAL), ValueType.STRING); - PARSER.declareField(optionalConstructorArg(), p -> new DateHistogramInterval(p.text()), - new ParseField(FIXED_INTERVAL), ValueType.STRING); - PARSER.declareField(optionalConstructorArg(), p -> new DateHistogramInterval(p.text()), new ParseField(DELAY), ValueType.STRING); + PARSER.declareField( + optionalConstructorArg(), + p -> new DateHistogramInterval(p.text()), + new ParseField(CALENDAR_INTERVAL), + ValueType.STRING + ); + PARSER.declareField( + optionalConstructorArg(), + p -> new DateHistogramInterval(p.text()), + new ParseField(FIXED_INTERVAL), + ValueType.STRING + ); + PARSER.declareField(optionalConstructorArg(), p -> new DateHistogramInterval(p.text()), new ParseField(DELAY), ValueType.STRING); PARSER.declareString(optionalConstructorArg(), new ParseField(TIME_ZONE)); } @@ -109,6 +118,7 @@ public abstract class DateHistogramGroupConfig implements Writeable, ToXContentO */ public static class FixedInterval extends DateHistogramGroupConfig { private static final String TYPE_NAME = "fixed_interval"; + public FixedInterval(String field, DateHistogramInterval interval) { this(field, interval, null, null); } @@ -139,6 +149,7 @@ public String getIntervalTypeName() { */ public static class CalendarInterval extends DateHistogramGroupConfig { private static final String TYPE_NAME = "calendar_interval"; + public CalendarInterval(String field, DateHistogramInterval interval) { this(field, interval, null, null); } @@ -146,8 +157,9 @@ public CalendarInterval(String field, DateHistogramInterval interval) { public CalendarInterval(String field, DateHistogramInterval interval, DateHistogramInterval delay, String timeZone) { super(field, interval, delay, timeZone); if (DateHistogramAggregationBuilder.DATE_FIELD_UNITS.get(interval.toString()) == null) { - throw new IllegalArgumentException("The supplied interval [" + interval +"] could not be parsed " + - "as a calendar interval."); + throw new IllegalArgumentException( + "The supplied interval [" + interval + "] could not be parsed " + "as a calendar interval." + ); } } @@ -166,8 +178,12 @@ public String getIntervalTypeName() { * one of the new Fixed or Calendar intervals. It follows the old behavior where the interval is first * parsed with the calendar logic, and if that fails, it is assumed to be a fixed interval */ - private static DateHistogramGroupConfig fromUnknownTimeUnit(String field, DateHistogramInterval interval, - DateHistogramInterval delay, String timeZone) { + private static DateHistogramGroupConfig fromUnknownTimeUnit( + String field, + DateHistogramInterval interval, + DateHistogramInterval delay, + String timeZone + ) { if (DateHistogramAggregationBuilder.DATE_FIELD_UNITS.get(interval.toString()) != null) { return new CalendarInterval(field, interval, delay, timeZone); } else { @@ -198,10 +214,12 @@ static DateHistogramGroupConfig fromUnknownTimeUnit(StreamInput in) throws IOExc * * @since 7.2.0 */ - protected DateHistogramGroupConfig(final String field, - final DateHistogramInterval interval, - final @Nullable DateHistogramInterval delay, - final @Nullable String timeZone) { + protected DateHistogramGroupConfig( + final String field, + final DateHistogramInterval interval, + final @Nullable DateHistogramInterval delay, + final @Nullable String timeZone + ) { if (field == null || field.isEmpty()) { throw new IllegalArgumentException("Field must be a non-null, non-empty string"); } @@ -290,8 +308,10 @@ public String getIntervalTypeName() { return TYPE_NAME; } - public void validateMappings(Map> fieldCapsResponse, - ActionRequestValidationException validationException) { + public void validateMappings( + Map> fieldCapsResponse, + ActionRequestValidationException validationException + ) { Map fieldCaps = fieldCapsResponse.get(field); if (fieldCaps != null && fieldCaps.isEmpty() == false) { boolean matchesDateType = false; @@ -301,21 +321,33 @@ public void validateMappings(Map> fieldCa if (fieldCaps.get(dateType).isAggregatable()) { return; } else { - validationException.addValidationError("The field [" + field + "] must be aggregatable across all indices, " + - "but is not."); + validationException.addValidationError( + "The field [" + field + "] must be aggregatable across all indices, " + "but is not." + ); } } } if (matchesDateType == false) { - validationException.addValidationError("The field referenced by a date_histo group must be one of type [" + - Strings.collectionToCommaDelimitedString(RollupField.DATE_FIELD_MAPPER_TYPES) + "] across all " + - "indices in the index pattern. Found: " + fieldCaps.keySet().toString() + " for field [" + field + "]"); + validationException.addValidationError( + "The field referenced by a date_histo group must be one of type [" + + Strings.collectionToCommaDelimitedString(RollupField.DATE_FIELD_MAPPER_TYPES) + + "] across all " + + "indices in the index pattern. Found: " + + fieldCaps.keySet().toString() + + " for field [" + + field + + "]" + ); } } else { - validationException.addValidationError("Could not find one of [" + - Strings.collectionToCommaDelimitedString(RollupField.DATE_FIELD_MAPPER_TYPES) + "] fields with name [" + - field + "] in any of the indices matching " + - "the index pattern."); + validationException.addValidationError( + "Could not find one of [" + + Strings.collectionToCommaDelimitedString(RollupField.DATE_FIELD_MAPPER_TYPES) + + "] fields with name [" + + field + + "] in any of the indices matching " + + "the index pattern." + ); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/job/GroupConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/job/GroupConfig.java index bf356638e38fd..e185ac9fb4e61 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/job/GroupConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/job/GroupConfig.java @@ -8,13 +8,13 @@ import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.fieldcaps.FieldCapabilities; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -47,14 +47,21 @@ public class GroupConfig implements Writeable, ToXContentObject { public static final String NAME = "groups"; private static final ConstructingObjectParser PARSER; static { - PARSER = new ConstructingObjectParser<>(NAME, args -> - new GroupConfig((DateHistogramGroupConfig) args[0], (HistogramGroupConfig) args[1], (TermsGroupConfig) args[2])); - PARSER.declareObject(constructorArg(), - (p, c) -> DateHistogramGroupConfig.fromXContent(p), new ParseField(DateHistogramGroupConfig.NAME)); - PARSER.declareObject(optionalConstructorArg(), - (p, c) -> HistogramGroupConfig.fromXContent(p), new ParseField(HistogramGroupConfig.NAME)); - PARSER.declareObject(optionalConstructorArg(), - (p, c) -> TermsGroupConfig.fromXContent(p), new ParseField(TermsGroupConfig.NAME)); + PARSER = new ConstructingObjectParser<>( + NAME, + args -> new GroupConfig((DateHistogramGroupConfig) args[0], (HistogramGroupConfig) args[1], (TermsGroupConfig) args[2]) + ); + PARSER.declareObject( + constructorArg(), + (p, c) -> DateHistogramGroupConfig.fromXContent(p), + new ParseField(DateHistogramGroupConfig.NAME) + ); + PARSER.declareObject( + optionalConstructorArg(), + (p, c) -> HistogramGroupConfig.fromXContent(p), + new ParseField(HistogramGroupConfig.NAME) + ); + PARSER.declareObject(optionalConstructorArg(), (p, c) -> TermsGroupConfig.fromXContent(p), new ParseField(TermsGroupConfig.NAME)); } private final DateHistogramGroupConfig dateHistogram; @@ -65,9 +72,11 @@ public GroupConfig(final DateHistogramGroupConfig dateHistogram) { this(dateHistogram, null, null); } - public GroupConfig(final DateHistogramGroupConfig dateHistogram, - final @Nullable HistogramGroupConfig histogram, - final @Nullable TermsGroupConfig terms) { + public GroupConfig( + final DateHistogramGroupConfig dateHistogram, + final @Nullable HistogramGroupConfig histogram, + final @Nullable TermsGroupConfig terms + ) { if (dateHistogram == null) { throw new IllegalArgumentException("Date histogram must not be null"); } @@ -117,8 +126,10 @@ public Set getAllFields() { return Collections.unmodifiableSet(fields); } - public void validateMappings(final Map> fieldCapsResponse, - final ActionRequestValidationException validationException) { + public void validateMappings( + final Map> fieldCapsResponse, + final ActionRequestValidationException validationException + ) { dateHistogram.validateMappings(fieldCapsResponse, validationException); if (histogram != null) { histogram.validateMappings(fieldCapsResponse, validationException); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/job/HistogramGroupConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/job/HistogramGroupConfig.java index 3c76925f19903..69aeb98faf2c9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/job/HistogramGroupConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/job/HistogramGroupConfig.java @@ -8,12 +8,12 @@ import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.fieldcaps.FieldCapabilities; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -47,7 +47,8 @@ public class HistogramGroupConfig implements Writeable, ToXContentObject { private static final ConstructingObjectParser PARSER; static { PARSER = new ConstructingObjectParser<>(NAME, args -> { - @SuppressWarnings("unchecked") List fields = (List) args[1]; + @SuppressWarnings("unchecked") + List fields = (List) args[1]; return new HistogramGroupConfig((long) args[0], fields != null ? fields.toArray(new String[fields.size()]) : null); }); PARSER.declareLong(constructorArg(), new ParseField(INTERVAL)); @@ -81,8 +82,10 @@ public String[] getFields() { return fields; } - public void validateMappings(Map> fieldCapsResponse, - ActionRequestValidationException validationException) { + public void validateMappings( + Map> fieldCapsResponse, + ActionRequestValidationException validationException + ) { Arrays.stream(fields).forEach(field -> { Map fieldCaps = fieldCapsResponse.get(field); @@ -90,17 +93,25 @@ public void validateMappings(Map> fieldCa fieldCaps.forEach((key, value) -> { if (RollupField.NUMERIC_FIELD_MAPPER_TYPES.contains(key)) { if (value.isAggregatable() == false) { - validationException.addValidationError("The field [" + field + "] must be aggregatable across all indices, " + - "but is not."); + validationException.addValidationError( + "The field [" + field + "] must be aggregatable across all indices, " + "but is not." + ); } } else { - validationException.addValidationError("The field referenced by a histo group must be a [numeric] type, " + - "but found " + fieldCaps.keySet().toString() + " for field [" + field + "]"); + validationException.addValidationError( + "The field referenced by a histo group must be a [numeric] type, " + + "but found " + + fieldCaps.keySet().toString() + + " for field [" + + field + + "]" + ); } }); } else { - validationException.addValidationError("Could not find a [numeric] field with name [" + field - + "] in any of the indices matching the index pattern."); + validationException.addValidationError( + "Could not find a [numeric] field with name [" + field + "] in any of the indices matching the index pattern." + ); } }); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/job/MetricConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/job/MetricConfig.java index eb08396f61b18..48cd2f0b1c204 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/job/MetricConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/job/MetricConfig.java @@ -8,12 +8,12 @@ import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.fieldcaps.FieldCapabilities; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -58,7 +58,8 @@ public class MetricConfig implements Writeable, ToXContentObject { private static final ConstructingObjectParser PARSER; static { PARSER = new ConstructingObjectParser<>(NAME, args -> { - @SuppressWarnings("unchecked") List metrics = (List) args[1]; + @SuppressWarnings("unchecked") + List metrics = (List) args[1]; return new MetricConfig((String) args[0], metrics); }); PARSER.declareString(constructorArg(), new ParseField(FIELD)); @@ -77,8 +78,9 @@ public MetricConfig(final String field, final List metrics) { } metrics.forEach(m -> { if (RollupField.SUPPORTED_METRICS.contains(m) == false) { - throw new IllegalArgumentException("Unsupported metric [" + m + "]. " + - "Supported metrics include: " + RollupField.SUPPORTED_METRICS); + throw new IllegalArgumentException( + "Unsupported metric [" + m + "]. " + "Supported metrics include: " + RollupField.SUPPORTED_METRICS + ); } }); this.field = field; @@ -104,33 +106,47 @@ public List getMetrics() { return metrics; } - public void validateMappings(Map> fieldCapsResponse, - ActionRequestValidationException validationException) { + public void validateMappings( + Map> fieldCapsResponse, + ActionRequestValidationException validationException + ) { Map fieldCaps = fieldCapsResponse.get(field); if (fieldCaps != null && fieldCaps.isEmpty() == false) { fieldCaps.forEach((key, value) -> { if (value.isAggregatable() == false) { - validationException.addValidationError("The field [" + field + "] must be aggregatable across all indices, " + - "but is not."); + validationException.addValidationError( + "The field [" + field + "] must be aggregatable across all indices, " + "but is not." + ); } if (RollupField.NUMERIC_FIELD_MAPPER_TYPES.contains(key)) { // nothing to do as all metrics are supported by SUPPORTED_NUMERIC_METRICS currently } else if (RollupField.DATE_FIELD_MAPPER_TYPES.contains(key)) { if (RollupField.SUPPORTED_DATE_METRICS.containsAll(metrics) == false) { - validationException.addValidationError( - buildSupportedMetricError(key, RollupField.SUPPORTED_DATE_METRICS)); + validationException.addValidationError(buildSupportedMetricError(key, RollupField.SUPPORTED_DATE_METRICS)); } } else { - validationException.addValidationError("The field referenced by a metric group must be a [numeric] or [" + - Strings.collectionToCommaDelimitedString(RollupField.DATE_FIELD_MAPPER_TYPES) + "] type, " + - "but found " + fieldCaps.keySet().toString() + " for field [" + field + "]"); + validationException.addValidationError( + "The field referenced by a metric group must be a [numeric] or [" + + Strings.collectionToCommaDelimitedString(RollupField.DATE_FIELD_MAPPER_TYPES) + + "] type, " + + "but found " + + fieldCaps.keySet().toString() + + " for field [" + + field + + "]" + ); } }); } else { - validationException.addValidationError("Could not find a [numeric] or [" + - Strings.collectionToCommaDelimitedString(RollupField.DATE_FIELD_MAPPER_TYPES) + - "] field with name [" + field + "] in any of the " + "indices matching the index pattern."); + validationException.addValidationError( + "Could not find a [numeric] or [" + + Strings.collectionToCommaDelimitedString(RollupField.DATE_FIELD_MAPPER_TYPES) + + "] field with name [" + + field + + "] in any of the " + + "indices matching the index pattern." + ); } } @@ -180,7 +196,15 @@ public static MetricConfig fromXContent(final XContentParser parser) throws IOEx private String buildSupportedMetricError(String type, List supportedMetrics) { List unsupportedMetrics = new ArrayList<>(metrics); unsupportedMetrics.removeAll(supportedMetrics); - return "Only the metrics " + supportedMetrics + " are supported for [" + type + "] types," + - " but unsupported metrics " + unsupportedMetrics + " supplied for field [" + field + "]"; + return "Only the metrics " + + supportedMetrics + + " are supported for [" + + type + + "] types," + + " but unsupported metrics " + + unsupportedMetrics + + " supplied for field [" + + field + + "]"; } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/job/RollupIndexerJobStats.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/job/RollupIndexerJobStats.java index 04c186ddc94d4..c9b245fa7bc21 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/job/RollupIndexerJobStats.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/job/RollupIndexerJobStats.java @@ -6,9 +6,9 @@ */ package org.elasticsearch.xpack.core.rollup.job; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.indexing.IndexerJobStats; @@ -35,11 +35,23 @@ public class RollupIndexerJobStats extends IndexerJobStats { private static ParseField SEARCH_FAILURES = new ParseField("search_failures"); private static ParseField INDEX_FAILURES = new ParseField("index_failures"); - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(NAME.getPreferredName(), - args -> new RollupIndexerJobStats((long) args[0], (long) args[1], (long) args[2], (long) args[3], - (long) args[4], (long) args[5], (long) args[6], (long) args[7], (long) args[8], (long) args[9], - (long) args[10], (long) args[11])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME.getPreferredName(), + args -> new RollupIndexerJobStats( + (long) args[0], + (long) args[1], + (long) args[2], + (long) args[3], + (long) args[4], + (long) args[5], + (long) args[6], + (long) args[7], + (long) args[8], + (long) args[9], + (long) args[10], + (long) args[11] + ) + ); static { PARSER.declareLong(constructorArg(), NUM_PAGES); @@ -60,11 +72,34 @@ public RollupIndexerJobStats() { super(); } - public RollupIndexerJobStats(long numPages, long numInputDocuments, long numOuputDocuments, long numInvocations, - long indexTime, long searchTime, long processingTime, long indexTotal, long searchTotal, - long processingTotal, long indexFailures, long searchFailures) { - super(numPages, numInputDocuments, numOuputDocuments, numInvocations, indexTime, searchTime, processingTime, - indexTotal, searchTotal, processingTotal, indexFailures, searchFailures); + public RollupIndexerJobStats( + long numPages, + long numInputDocuments, + long numOuputDocuments, + long numInvocations, + long indexTime, + long searchTime, + long processingTime, + long indexTotal, + long searchTotal, + long processingTotal, + long indexFailures, + long searchFailures + ) { + super( + numPages, + numInputDocuments, + numOuputDocuments, + numInvocations, + indexTime, + searchTime, + processingTime, + indexTotal, + searchTotal, + processingTotal, + indexFailures, + searchFailures + ); } public RollupIndexerJobStats(StreamInput in) throws IOException { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/job/RollupJob.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/job/RollupJob.java index 503e4977513ed..90c7fbb701265 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/job/RollupJob.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/job/RollupJob.java @@ -9,13 +9,13 @@ import org.elasticsearch.Version; import org.elasticsearch.cluster.AbstractDiffable; import org.elasticsearch.cluster.Diff; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.persistent.PersistentTaskParams; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.persistent.PersistentTaskParams; import java.io.IOException; import java.util.Collections; @@ -40,8 +40,10 @@ public class RollupJob extends AbstractDiffable implements Persistent private static final ParseField HEADERS = new ParseField("headers"); @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER - = new ConstructingObjectParser<>(NAME, a -> new RollupJob((RollupJobConfig) a[0], (Map) a[1])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + a -> new RollupJob((RollupJobConfig) a[0], (Map) a[1]) + ); static { PARSER.declareObject(ConstructingObjectParser.constructorArg(), (p, c) -> RollupJobConfig.fromXContent(p, null), CONFIG); @@ -107,8 +109,7 @@ public boolean equals(Object other) { RollupJob that = (RollupJob) other; - return Objects.equals(this.config, that.config) - && Objects.equals(this.headers, that.headers); + return Objects.equals(this.config, that.config) && Objects.equals(this.headers, that.headers); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/job/RollupJobConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/job/RollupJobConfig.java index 5a7e7b42bcce5..0309e21699c9f 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/job/RollupJobConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/job/RollupJobConfig.java @@ -8,16 +8,16 @@ import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.fieldcaps.FieldCapabilities; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.NamedWriteable; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.regex.Regex; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -76,20 +76,26 @@ public class RollupJobConfig implements NamedWriteable, ToXContentObject { PARSER.declareString(constructorArg(), new ParseField(ROLLUP_INDEX)); PARSER.declareObject(optionalConstructorArg(), (p, c) -> GroupConfig.fromXContent(p), new ParseField(GroupConfig.NAME)); PARSER.declareObjectArray(optionalConstructorArg(), (p, c) -> MetricConfig.fromXContent(p), new ParseField(MetricConfig.NAME)); - PARSER.declareField(optionalConstructorArg(), (p, c) -> TimeValue.parseTimeValue(p.textOrNull(), TIMEOUT), - new ParseField(TIMEOUT), ObjectParser.ValueType.STRING_OR_NULL); + PARSER.declareField( + optionalConstructorArg(), + (p, c) -> TimeValue.parseTimeValue(p.textOrNull(), TIMEOUT), + new ParseField(TIMEOUT), + ObjectParser.ValueType.STRING_OR_NULL + ); PARSER.declareString(constructorArg(), new ParseField(CRON)); PARSER.declareInt(constructorArg(), new ParseField(PAGE_SIZE)); } - public RollupJobConfig(final String id, - final String indexPattern, - final String rollupIndex, - final String cron, - final int pageSize, - final GroupConfig groupConfig, - final List metricsConfig, - final @Nullable TimeValue timeout) { + public RollupJobConfig( + final String id, + final String indexPattern, + final String rollupIndex, + final String cron, + final int pageSize, + final GroupConfig groupConfig, + final List metricsConfig, + final @Nullable TimeValue timeout + ) { if (id == null || id.isEmpty()) { throw new IllegalArgumentException("Id must be a non-null, non-empty string"); } @@ -191,8 +197,10 @@ public Set getAllFields() { return Collections.unmodifiableSet(fields); } - public void validateMappings(final Map> fieldCapsResponse, - final ActionRequestValidationException validationException) { + public void validateMappings( + final Map> fieldCapsResponse, + final ActionRequestValidationException validationException + ) { groupConfig.validateMappings(fieldCapsResponse, validationException); for (MetricConfig m : metricsConfig) { m.validateMappings(fieldCapsResponse, validationException); @@ -249,13 +257,13 @@ public boolean equals(Object other) { final RollupJobConfig that = (RollupJobConfig) other; return Objects.equals(this.id, that.id) - && Objects.equals(this.indexPattern, that.indexPattern) - && Objects.equals(this.rollupIndex, that.rollupIndex) - && Objects.equals(this.cron, that.cron) - && Objects.equals(this.groupConfig, that.groupConfig) - && Objects.equals(this.metricsConfig, that.metricsConfig) - && Objects.equals(this.timeout, that.timeout) - && Objects.equals(this.pageSize, that.pageSize); + && Objects.equals(this.indexPattern, that.indexPattern) + && Objects.equals(this.rollupIndex, that.rollupIndex) + && Objects.equals(this.cron, that.cron) + && Objects.equals(this.groupConfig, that.groupConfig) + && Objects.equals(this.metricsConfig, that.metricsConfig) + && Objects.equals(this.timeout, that.timeout) + && Objects.equals(this.pageSize, that.pageSize); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/job/RollupJobStatus.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/job/RollupJobStatus.java index 98dbb1fdd5e5b..b362b92f38898 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/job/RollupJobStatus.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/job/RollupJobStatus.java @@ -6,18 +6,17 @@ */ package org.elasticsearch.xpack.core.rollup.job; - import org.elasticsearch.Version; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.persistent.PersistentTaskState; +import org.elasticsearch.tasks.Task; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.persistent.PersistentTaskState; -import org.elasticsearch.tasks.Task; import org.elasticsearch.xpack.core.indexing.IndexerState; import java.io.IOException; @@ -47,9 +46,10 @@ public class RollupJobStatus implements Task.Status, PersistentTaskState { private static final ParseField UPGRADED_DOC_ID = new ParseField("upgraded_doc_id"); // This can be removed in 9.0 @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(NAME, - args -> new RollupJobStatus((IndexerState) args[0], (HashMap) args[1])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + args -> new RollupJobStatus((IndexerState) args[0], (HashMap) args[1]) + ); static { PARSER.declareString(constructorArg(), IndexerState::fromString, STATE); @@ -76,7 +76,7 @@ public RollupJobStatus(StreamInput in) throws IOException { state = IndexerState.fromStream(in); currentPosition = in.readBoolean() ? new TreeMap<>(in.readMap()) : null; if (in.getVersion().before(Version.V_8_0_0)) { - // 7.x nodes serialize `upgradedDocumentID` flag. We don't need it anymore, but + // 7.x nodes serialize `upgradedDocumentID` flag. We don't need it anymore, but // we need to pull it off the stream // This can go away completely in 9.0 in.readBoolean(); @@ -142,8 +142,7 @@ public boolean equals(Object other) { RollupJobStatus that = (RollupJobStatus) other; - return Objects.equals(this.state, that.state) - && Objects.equals(this.currentPosition, that.currentPosition); + return Objects.equals(this.state, that.state) && Objects.equals(this.currentPosition, that.currentPosition); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/job/TermsGroupConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/job/TermsGroupConfig.java index f66e2fb628cbc..93f1bbffa87ac 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/job/TermsGroupConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/job/TermsGroupConfig.java @@ -8,17 +8,17 @@ import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.fieldcaps.FieldCapabilities; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.index.mapper.KeywordFieldMapper; +import org.elasticsearch.index.mapper.TextFieldMapper; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.index.mapper.KeywordFieldMapper; -import org.elasticsearch.index.mapper.TextFieldMapper; import java.io.IOException; import java.util.Arrays; @@ -49,7 +49,8 @@ public class TermsGroupConfig implements Writeable, ToXContentObject { private static final ConstructingObjectParser PARSER; static { PARSER = new ConstructingObjectParser<>(NAME, args -> { - @SuppressWarnings("unchecked") List fields = (List) args[0]; + @SuppressWarnings("unchecked") + List fields = (List) args[0]; return new TermsGroupConfig(fields != null ? fields.toArray(new String[fields.size()]) : null); }); PARSER.declareStringArray(constructorArg(), new ParseField(FIELDS)); @@ -75,8 +76,10 @@ public String[] getFields() { return fields; } - public void validateMappings(Map> fieldCapsResponse, - ActionRequestValidationException validationException) { + public void validateMappings( + Map> fieldCapsResponse, + ActionRequestValidationException validationException + ) { Arrays.stream(fields).forEach(field -> { Map fieldCaps = fieldCapsResponse.get(field); @@ -84,27 +87,39 @@ public void validateMappings(Map> fieldCa fieldCaps.forEach((key, value) -> { if (key.equals(KeywordFieldMapper.CONTENT_TYPE) || key.equals(TextFieldMapper.CONTENT_TYPE)) { if (value.isAggregatable() == false) { - validationException.addValidationError("The field [" + field + "] must be aggregatable across all indices, " + - "but is not."); + validationException.addValidationError( + "The field [" + field + "] must be aggregatable across all indices, " + "but is not." + ); } } else if (FLOAT_TYPES.contains(key)) { if (value.isAggregatable() == false) { - validationException.addValidationError("The field [" + field + "] must be aggregatable across all indices, " + - "but is not."); + validationException.addValidationError( + "The field [" + field + "] must be aggregatable across all indices, " + "but is not." + ); } } else if (NATURAL_TYPES.contains(key)) { if (value.isAggregatable() == false) { - validationException.addValidationError("The field [" + field + "] must be aggregatable across all indices, " + - "but is not."); + validationException.addValidationError( + "The field [" + field + "] must be aggregatable across all indices, " + "but is not." + ); } } else { - validationException.addValidationError("The field referenced by a terms group must be a [numeric] or " + - "[keyword/text] type, but found " + fieldCaps.keySet().toString() + " for field [" + field + "]"); + validationException.addValidationError( + "The field referenced by a terms group must be a [numeric] or " + + "[keyword/text] type, but found " + + fieldCaps.keySet().toString() + + " for field [" + + field + + "]" + ); } }); } else { - validationException.addValidationError("Could not find a [numeric] or [keyword/text] field with name [" + field - + "] in any of the indices matching the index pattern."); + validationException.addValidationError( + "Could not find a [numeric] or [keyword/text] field with name [" + + field + + "] in any of the indices matching the index pattern." + ); } }); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/scheduler/Cron.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/scheduler/Cron.java index 6d97d8e2a9110..935430656a72d 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/scheduler/Cron.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/scheduler/Cron.java @@ -26,7 +26,6 @@ import static java.util.Map.entry; import static org.elasticsearch.xpack.core.watcher.support.Exceptions.illegalArgument; - /** * THIS CLASS IS A FORK OF * @@ -226,16 +225,10 @@ public class Cron implements ToXContentFragment { entry("SEP", 8), entry("OCT", 9), entry("NOV", 10), - entry("DEC", 11)); + entry("DEC", 11) + ); - private static final Map DAY_MAP = Map.of( - "SUN", 1, - "MON", 2, - "TUE", 3, - "WED", 4, - "THU", 5, - "FRI", 6, - "SAT", 7); + private static final Map DAY_MAP = Map.of("SUN", 1, "MON", 2, "TUE", 3, "WED", 4, "THU", 5, "FRI", 6, "SAT", 7); private final String expression; @@ -309,7 +302,7 @@ public long getNextValidTimeAfter(final long time) { // loop until we've computed the next time, or we've past the endTime while (gotOne == false) { - if(cl.get(Calendar.YEAR) > 2999) { // prevent endless loop... + if (cl.get(Calendar.YEAR) > 2999) { // prevent endless loop... return -1; } @@ -386,13 +379,13 @@ public long getNextValidTimeAfter(final long time) { if (dayOfMSpec && dayOfWSpec == false) { // get day by day of month rule st = daysOfMonth.tailSet(day); if (lastdayOfMonth) { - if(nearestWeekday == false) { + if (nearestWeekday == false) { t = day; day = getLastDayOfMonth(mon, cl.get(Calendar.YEAR)); day -= lastdayOffset; - if(t > day) { + if (t > day) { mon++; - if(mon > 12) { + if (mon > 12) { mon = 1; tmon = 3333; // ensure test of mon != tmon further below fails cl.add(Calendar.YEAR, 1); @@ -415,13 +408,13 @@ public long getNextValidTimeAfter(final long time) { int ldom = getLastDayOfMonth(mon, cl.get(Calendar.YEAR)); int dow = tcal.get(Calendar.DAY_OF_WEEK); - if(dow == Calendar.SATURDAY && day == 1) { + if (dow == Calendar.SATURDAY && day == 1) { day += 2; - } else if(dow == Calendar.SATURDAY) { + } else if (dow == Calendar.SATURDAY) { day -= 1; - } else if(dow == Calendar.SUNDAY && day == ldom) { + } else if (dow == Calendar.SUNDAY && day == ldom) { day -= 2; - } else if(dow == Calendar.SUNDAY) { + } else if (dow == Calendar.SUNDAY) { day += 1; } @@ -431,12 +424,12 @@ public long getNextValidTimeAfter(final long time) { tcal.set(Calendar.DAY_OF_MONTH, day); tcal.set(Calendar.MONTH, mon - 1); long nTime = tcal.getTimeInMillis(); - if(nTime < afterTime) { + if (nTime < afterTime) { day = 1; mon++; } } - } else if(nearestWeekday) { + } else if (nearestWeekday) { t = day; day = daysOfMonth.first(); @@ -451,24 +444,23 @@ public long getNextValidTimeAfter(final long time) { int ldom = getLastDayOfMonth(mon, cl.get(Calendar.YEAR)); int dow = tcal.get(Calendar.DAY_OF_WEEK); - if(dow == Calendar.SATURDAY && day == 1) { + if (dow == Calendar.SATURDAY && day == 1) { day += 2; - } else if(dow == Calendar.SATURDAY) { + } else if (dow == Calendar.SATURDAY) { day -= 1; - } else if(dow == Calendar.SUNDAY && day == ldom) { + } else if (dow == Calendar.SUNDAY && day == ldom) { day -= 2; - } else if(dow == Calendar.SUNDAY) { + } else if (dow == Calendar.SUNDAY) { day += 1; } - tcal.set(Calendar.SECOND, sec); tcal.set(Calendar.MINUTE, min); tcal.set(Calendar.HOUR_OF_DAY, hr); tcal.set(Calendar.DAY_OF_MONTH, day); tcal.set(Calendar.MONTH, mon - 1); long nTime = tcal.getTimeInMillis(); - if(nTime < afterTime) { + if (nTime < afterTime) { day = daysOfMonth.first(); mon++; } @@ -565,9 +557,7 @@ public long getNextValidTimeAfter(final long time) { daysToAdd = (nthdayOfWeek - weekOfMonth) * 7; day += daysToAdd; - if (daysToAdd < 0 - || day > getLastDayOfMonth(mon, cl - .get(Calendar.YEAR))) { + if (daysToAdd < 0 || day > getLastDayOfMonth(mon, cl.get(Calendar.YEAR))) { cl.set(Calendar.SECOND, 0); cl.set(Calendar.MINUTE, 0); cl.set(Calendar.HOUR_OF_DAY, 0); @@ -625,8 +615,8 @@ public long getNextValidTimeAfter(final long time) { } } else { // dayOfWSpec && dayOfMSpec == false return -1; -// throw new UnsupportedOperationException( -// "Support for specifying both a day-of-week AND a day-of-month parameter is not implemented."); + // throw new UnsupportedOperationException( + // "Support for specifying both a day-of-week AND a day-of-month parameter is not implemented."); } cl.set(Calendar.DAY_OF_MONTH, day); @@ -640,7 +630,7 @@ public long getNextValidTimeAfter(final long time) { // but keep looping... if (year > MAX_YEAR) { return -1; -// throw new ElasticsearchIllegalArgumentException("given time is not supported by cron [" + formatter.print(time) + "]"); + // throw new ElasticsearchIllegalArgumentException("given time is not supported by cron [" + formatter.print(time) + "]"); } // get month................................................... @@ -677,7 +667,7 @@ public long getNextValidTimeAfter(final long time) { year = st.first(); } else { return -1; -// throw new ElasticsearchIllegalArgumentException("given time is not supported by cron [" + formatter.print(time) + "]"); + // throw new ElasticsearchIllegalArgumentException("given time is not supported by cron [" + formatter.print(time) + "]"); } if (year != t) { @@ -791,7 +781,6 @@ public static void validate(String expression) throws IllegalArgumentException { new Cron(expression); } - //////////////////////////////////////////////////////////////////////////// // // Expression Parsing Functions @@ -826,21 +815,20 @@ private void buildExpression(String expression) { int exprOn = SECOND; - StringTokenizer exprsTok = new StringTokenizer(expression, " \t", - false); + StringTokenizer exprsTok = new StringTokenizer(expression, " \t", false); while (exprsTok.hasMoreTokens() && exprOn <= YEAR) { String expr = exprsTok.nextToken().trim(); // throw an exception if L is used with other days of the month - if(exprOn == DAY_OF_MONTH && expr.indexOf('L') != -1 && expr.length() > 1 && expr.contains(",")) { + if (exprOn == DAY_OF_MONTH && expr.indexOf('L') != -1 && expr.length() > 1 && expr.contains(",")) { throw illegalArgument("support for specifying 'L' and 'LW' with other days of the month is not implemented"); } // throw an exception if L is used with other days of the week - if(exprOn == DAY_OF_WEEK && expr.indexOf('L') != -1 && expr.length() > 1 && expr.contains(",")) { + if (exprOn == DAY_OF_WEEK && expr.indexOf('L') != -1 && expr.length() > 1 && expr.contains(",")) { throw illegalArgument("support for specifying 'L' with other days of the week is not implemented"); } - if(exprOn == DAY_OF_WEEK && expr.indexOf('#') != -1 && expr.indexOf('#', expr.indexOf('#') +1) != -1) { + if (exprOn == DAY_OF_WEEK && expr.indexOf('#') != -1 && expr.indexOf('#', expr.indexOf('#') + 1) != -1) { throw illegalArgument("support for specifying multiple \"nth\" days is not implemented."); } @@ -948,8 +936,7 @@ private int storeExpressionVals(int pos, String s, int type) throws Elasticsearc if (c == '?') { i++; - if ((i + 1) < s.length() - && (s.charAt(i) != ' ' && s.charAt(i + 1) != '\t')) { + if ((i + 1) < s.length() && (s.charAt(i) != ' ' && s.charAt(i + 1) != '\t')) { throw illegalArgument("illegal character [{}] after '?' at pos [{}]", s.charAt(i), i); } if (type != DAY_OF_WEEK && type != DAY_OF_MONTH) { @@ -970,9 +957,7 @@ private int storeExpressionVals(int pos, String s, int type) throws Elasticsearc if (c == '*' && (i + 1) >= s.length()) { addToSet(ALL_SPEC_INT, -1, incr, type); return i + 1; - } else if (c == '/' - && ((i + 1) >= s.length() || s.charAt(i + 1) == ' ' || s - .charAt(i + 1) == '\t')) { + } else if (c == '/' && ((i + 1) >= s.length() || s.charAt(i + 1) == ' ' || s.charAt(i + 1) == '\t')) { throw illegalArgument("'/' must be followed by an integer. at pos [{}]", i); } else if (c == '*') { i++; @@ -1015,18 +1000,17 @@ private int storeExpressionVals(int pos, String s, int type) throws Elasticsearc if (type == DAY_OF_WEEK) { addToSet(7, 7, 0, type); } - if(type == DAY_OF_MONTH && s.length() > i) { + if (type == DAY_OF_MONTH && s.length() > i) { c = s.charAt(i); - if(c == '-') { - ValueSet vs = getValue(0, s, i+1); + if (c == '-') { + ValueSet vs = getValue(0, s, i + 1); lastdayOffset = vs.value; - if(lastdayOffset > 30) - throw illegalArgument("offset from last day must be <= 30 at pos [{}]", i + 1); + if (lastdayOffset > 30) throw illegalArgument("offset from last day must be <= 30 at pos [{}]", i + 1); i = vs.pos; } - if(s.length() > i) { + if (s.length() > i) { c = s.charAt(i); - if(c == 'W') { + if (c == 'W') { nearestWeekday = true; i++; } @@ -1069,8 +1053,7 @@ private int checkNext(int pos, String s, int val, int type) throws Elasticsearch if (c == 'L') { if (type == DAY_OF_WEEK) { - if(val < 1 || val > 7) - throw illegalArgument("Day-of-Week values must be between 1 and 7"); + if (val < 1 || val > 7) throw illegalArgument("Day-of-Week values must be between 1 and 7"); lastdayOfWeek = true; } else { throw illegalArgument("'L' option is not valid here. at pos [{}]", i); @@ -1087,9 +1070,10 @@ private int checkNext(int pos, String s, int val, int type) throws Elasticsearch } else { throw illegalArgument("'W' option is not valid here. at pos [{}]", i); } - if(val > 31) - throw illegalArgument("the 'W' option does not make sense with values larger than 31 (max number of days in a month) at " + - "pos [{}]", i); + if (val > 31) throw illegalArgument( + "the 'W' option does not make sense with values larger than 31 (max number of days in a month) at " + "pos [{}]", + i + ); TreeSet set = getSet(type); set.add(val); i++; @@ -1239,8 +1223,7 @@ private void addToSet(int val, int end, int incr, int type) throws Elasticsearch throw illegalArgument("Hour values must be between 0 and 23"); } } else if (type == DAY_OF_MONTH) { - if ((val < 1 || val > 31 || end > 31) && (val != ALL_SPEC_INT) - && (val != NO_SPEC_INT)) { + if ((val < 1 || val > 31 || end > 31) && (val != ALL_SPEC_INT) && (val != NO_SPEC_INT)) { throw illegalArgument("Day of month values must be between 1 and 31"); } } else if (type == MONTH) { @@ -1248,8 +1231,7 @@ private void addToSet(int val, int end, int incr, int type) throws Elasticsearch throw illegalArgument("Month values must be between 1 and 12"); } } else if (type == DAY_OF_WEEK) { - if ((val == 0 || val > 7 || end > 7) && (val != ALL_SPEC_INT) - && (val != NO_SPEC_INT)) { + if ((val == 0 || val > 7 || end > 7) && (val != ALL_SPEC_INT) && (val != NO_SPEC_INT)) { throw illegalArgument("Day-of-Week values must be between 1 and 7"); } } @@ -1357,7 +1339,7 @@ private void addToSet(int val, int end, int incr, int type) throws Elasticsearch int i2 = i % max; // 1-indexed ranges should not include 0, and should include their max - if (i2 == 0 && (type == MONTH || type == DAY_OF_WEEK || type == DAY_OF_MONTH) ) { + if (i2 == 0 && (type == MONTH || type == DAY_OF_WEEK || type == DAY_OF_MONTH)) { i2 = max; } @@ -1477,8 +1459,7 @@ private int getLastDayOfMonth(int monthNum, int year) { case 12: return 31; default: - throw new IllegalArgumentException("Illegal month number: " - + monthNum); + throw new IllegalArgumentException("Illegal month number: " + monthNum); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/scheduler/SchedulerEngine.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/scheduler/SchedulerEngine.java index 2b02bbde88942..19fa5957c77ed 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/scheduler/SchedulerEngine.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/scheduler/SchedulerEngine.java @@ -76,9 +76,7 @@ public long getScheduledTime() { @Override public String toString() { - return "Event[jobName=" + jobName + "," + - "triggeredTime=" + triggeredTime + "," + - "scheduledTime=" + scheduledTime + "]"; + return "Event[jobName=" + jobName + "," + "triggeredTime=" + triggeredTime + "," + "scheduledTime=" + scheduledTime + "]"; } } @@ -119,7 +117,9 @@ public SchedulerEngine(final Settings settings, final Clock clock) { SchedulerEngine(final Settings settings, final Clock clock, final Logger logger) { this.clock = Objects.requireNonNull(clock, "clock"); this.scheduler = Executors.newScheduledThreadPool( - 1, EsExecutors.daemonThreadFactory(Objects.requireNonNull(settings, "settings"), "trigger_engine_scheduler")); + 1, + EsExecutors.daemonThreadFactory(Objects.requireNonNull(settings, "settings"), "trigger_engine_scheduler") + ); this.logger = Objects.requireNonNull(logger, "logger"); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/search/action/AsyncSearchResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/search/action/AsyncSearchResponse.java index ebb51eb0897d8..22d4fe9644ec4 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/search/action/AsyncSearchResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/search/action/AsyncSearchResponse.java @@ -10,12 +10,12 @@ import org.elasticsearch.ExceptionsHelper; import org.elasticsearch.action.ActionResponse; import org.elasticsearch.action.search.SearchResponse; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.StatusToXContentObject; -import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.core.Nullable; import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.async.AsyncResponse; import java.io.IOException; @@ -41,11 +41,7 @@ public class AsyncSearchResponse extends ActionResponse implements StatusToXCont /** * Creates an {@link AsyncSearchResponse} with meta-information only (not-modified). */ - public AsyncSearchResponse(String id, - boolean isPartial, - boolean isRunning, - long startTimeMillis, - long expirationTimeMillis) { + public AsyncSearchResponse(String id, boolean isPartial, boolean isRunning, long startTimeMillis, long expirationTimeMillis) { this(id, null, null, isPartial, isRunning, startTimeMillis, expirationTimeMillis); } @@ -60,13 +56,15 @@ public AsyncSearchResponse(String id, * @param isRunning Whether the search is running in the cluster. * @param startTimeMillis The start date of the search in milliseconds since epoch. */ - public AsyncSearchResponse(String id, - SearchResponse searchResponse, - Exception error, - boolean isPartial, - boolean isRunning, - long startTimeMillis, - long expirationTimeMillis) { + public AsyncSearchResponse( + String id, + SearchResponse searchResponse, + Exception error, + boolean isPartial, + boolean isRunning, + long startTimeMillis, + long expirationTimeMillis + ) { this.id = id; this.error = error; this.searchResponse = searchResponse; @@ -210,14 +208,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws @Override public AsyncSearchResponse convertToFailure(Exception exc) { exc.setStackTrace(new StackTraceElement[0]); // we don't need to store stack traces - return new AsyncSearchResponse( - id, - null, - exc, - isPartial, - false, - startTimeMillis, - expirationTimeMillis - ); + return new AsyncSearchResponse(id, null, exc, isPartial, false, startTimeMillis, expirationTimeMillis); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/search/action/AsyncStatusResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/search/action/AsyncStatusResponse.java index ef0b4a45c48e6..a0388b048654c 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/search/action/AsyncStatusResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/search/action/AsyncStatusResponse.java @@ -12,9 +12,9 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.StatusToXContentObject; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestActions; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.Objects; @@ -36,16 +36,18 @@ public class AsyncStatusResponse extends ActionResponse implements SearchStatusR private final int failedShards; private final RestStatus completionStatus; - public AsyncStatusResponse(String id, - boolean isRunning, - boolean isPartial, - long startTimeMillis, - long expirationTimeMillis, - int totalShards, - int successfulShards, - int skippedShards, - int failedShards, - RestStatus completionStatus) { + public AsyncStatusResponse( + String id, + boolean isRunning, + boolean isPartial, + long startTimeMillis, + long expirationTimeMillis, + int totalShards, + int successfulShards, + int skippedShards, + int failedShards, + RestStatus completionStatus + ) { this.id = id; this.isRunning = isRunning; this.isPartial = isPartial; @@ -65,8 +67,11 @@ public AsyncStatusResponse(String id, * @param id – encoded async search id * @return status response */ - public static AsyncStatusResponse getStatusFromStoredSearch(AsyncSearchResponse asyncSearchResponse, - long expirationTimeMillis, String id) { + public static AsyncStatusResponse getStatusFromStoredSearch( + AsyncSearchResponse asyncSearchResponse, + long expirationTimeMillis, + String id + ) { int totalShards = 0; int successfulShards = 0; int skippedShards = 0; @@ -172,8 +177,18 @@ public boolean equals(Object obj) { @Override public int hashCode() { - return Objects.hash(id, isRunning, isPartial, startTimeMillis, expirationTimeMillis, totalShards, - successfulShards, skippedShards, failedShards, completionStatus); + return Objects.hash( + id, + isRunning, + isPartial, + startTimeMillis, + expirationTimeMillis, + totalShards, + successfulShards, + skippedShards, + failedShards, + completionStatus + ); } /** diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/search/action/SubmitAsyncSearchRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/search/action/SubmitAsyncSearchRequest.java index 6022efc6f0441..8135be978a6df 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/search/action/SubmitAsyncSearchRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/search/action/SubmitAsyncSearchRequest.java @@ -135,17 +135,22 @@ public ActionRequestValidationException validate() { validationException = addValidationError("suggest-only queries are not supported", validationException); } if (keepAlive.getMillis() < MIN_KEEP_ALIVE) { - validationException = - addValidationError("[keep_alive] must be greater or equals than 1 second, got:" + - keepAlive.toString(), validationException); + validationException = addValidationError( + "[keep_alive] must be greater or equals than 1 second, got:" + keepAlive.toString(), + validationException + ); } if (request.isCcsMinimizeRoundtrips()) { - validationException = - addValidationError("[ccs_minimize_roundtrips] is not supported on async search queries", validationException); + validationException = addValidationError( + "[ccs_minimize_roundtrips] is not supported on async search queries", + validationException + ); } if (request.getPreFilterShardSize() == null || request.getPreFilterShardSize() != 1) { - validationException = - addValidationError("[pre_filter_shard_size] cannot be changed for async search queries", validationException); + validationException = addValidationError( + "[pre_filter_shard_size] cannot be changed for async search queries", + validationException + ); } return validationException; @@ -157,10 +162,14 @@ public Task createTask(long id, String type, String action, TaskId parentTaskId, @Override public String getDescription() { // generating description in a lazy way since source can be quite big - return "waitForCompletionTimeout[" + waitForCompletionTimeout + - "], keepOnCompletion[" + keepOnCompletion + - "] keepAlive[" + keepAlive + - "], request=" + request.buildDescription(); + return "waitForCompletionTimeout[" + + waitForCompletionTimeout + + "], keepOnCompletion[" + + keepOnCompletion + + "] keepAlive[" + + keepAlive + + "], request=" + + request.buildDescription(); } }; } @@ -174,10 +183,10 @@ public boolean equals(Object o) { return false; } SubmitAsyncSearchRequest request1 = (SubmitAsyncSearchRequest) o; - return keepOnCompletion == request1.keepOnCompletion && - waitForCompletionTimeout.equals(request1.waitForCompletionTimeout) && - keepAlive.equals(request1.keepAlive) && - request.equals(request1.request); + return keepOnCompletion == request1.keepOnCompletion + && waitForCompletionTimeout.equals(request1.waitForCompletionTimeout) + && keepAlive.equals(request1.keepAlive) + && request.equals(request1.request); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/searchablesnapshots/MountSearchableSnapshotRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/searchablesnapshots/MountSearchableSnapshotRequest.java index dfc9fff6e1b75..0c2a28e576556 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/searchablesnapshots/MountSearchableSnapshotRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/searchablesnapshots/MountSearchableSnapshotRequest.java @@ -11,17 +11,17 @@ import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.support.master.MasterNodeRequest; import org.elasticsearch.cluster.metadata.IndexMetadata; -import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.cluster.routing.allocation.DataTier; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.rest.RestRequest; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.rest.RestRequest; -import org.elasticsearch.cluster.routing.allocation.DataTier; import java.io.IOException; import java.util.Arrays; @@ -38,16 +38,19 @@ public class MountSearchableSnapshotRequest extends MasterNodeRequest { public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "mount_searchable_snapshot", true, + "mount_searchable_snapshot", + true, (a, request) -> new MountSearchableSnapshotRequest( - Objects.requireNonNullElse((String)a[1], (String)a[0]), + Objects.requireNonNullElse((String) a[1], (String) a[0]), Objects.requireNonNull(request.param("repository")), Objects.requireNonNull(request.param("snapshot")), - (String)a[0], - Objects.requireNonNullElse((Settings)a[2], Settings.EMPTY), - Objects.requireNonNullElse((String[])a[3], Strings.EMPTY_ARRAY), + (String) a[0], + Objects.requireNonNullElse((Settings) a[2], Settings.EMPTY), + Objects.requireNonNullElse((String[]) a[3], Strings.EMPTY_ARRAY), request.paramAsBoolean("wait_for_completion", false), - Storage.valueOf(request.param("storage", Storage.FULL_COPY.toString()).toUpperCase(Locale.ROOT)))); + Storage.valueOf(request.param("storage", Storage.FULL_COPY.toString()).toUpperCase(Locale.ROOT)) + ) + ); private static final ParseField INDEX_FIELD = new ParseField("index"); private static final ParseField RENAMED_INDEX_FIELD = new ParseField("renamed_index"); @@ -58,9 +61,12 @@ public class MountSearchableSnapshotRequest extends MasterNodeRequest p.list().stream().map(s -> (String) s).collect(Collectors.toList()).toArray(Strings.EMPTY_ARRAY), - IGNORE_INDEX_SETTINGS_FIELD, ObjectParser.ValueType.STRING_ARRAY); + IGNORE_INDEX_SETTINGS_FIELD, + ObjectParser.ValueType.STRING_ARRAY + ); } /** @@ -81,14 +87,15 @@ public class MountSearchableSnapshotRequest extends MasterNodeRequest stats) { + public SearchableSnapshotShardStats( + ShardRouting shardRouting, + SnapshotId snapshotId, + IndexId indexId, + List stats + ) { this.shardRouting = Objects.requireNonNull(shardRouting); this.snapshotId = Objects.requireNonNull(snapshotId); this.indexId = Objects.requireNonNull(indexId); @@ -92,7 +96,8 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.startArray("files"); { List stats = inputStats.stream() - .sorted(Comparator.comparing(CacheIndexInputStats::getFileExt)).collect(toList()); + .sorted(Comparator.comparing(CacheIndexInputStats::getFileExt)) + .collect(toList()); for (CacheIndexInputStats stat : stats) { stat.toXContent(builder, params); } @@ -148,15 +153,29 @@ public static class CacheIndexInputStats implements Writeable, ToXContentObject private final Counter luceneBytesRead; private final long currentIndexCacheFills; - public CacheIndexInputStats(String fileExt, long numFiles, ByteSizeValue totalSize, ByteSizeValue minSize, ByteSizeValue maxSize, - long openCount, long closeCount, - Counter forwardSmallSeeks, Counter backwardSmallSeeks, - Counter forwardLargeSeeks, Counter backwardLargeSeeks, - Counter contiguousReads, Counter nonContiguousReads, - Counter cachedBytesRead, Counter indexCacheBytesRead, - TimedCounter cachedBytesWritten, TimedCounter directBytesRead, TimedCounter optimizedBytesRead, - Counter blobStoreBytesRequested, Counter luceneBytesRead, - long currentIndexCacheFills) { + public CacheIndexInputStats( + String fileExt, + long numFiles, + ByteSizeValue totalSize, + ByteSizeValue minSize, + ByteSizeValue maxSize, + long openCount, + long closeCount, + Counter forwardSmallSeeks, + Counter backwardSmallSeeks, + Counter forwardLargeSeeks, + Counter backwardLargeSeeks, + Counter contiguousReads, + Counter nonContiguousReads, + Counter cachedBytesRead, + Counter indexCacheBytesRead, + TimedCounter cachedBytesWritten, + TimedCounter directBytesRead, + TimedCounter optimizedBytesRead, + Counter blobStoreBytesRequested, + Counter luceneBytesRead, + long currentIndexCacheFills + ) { this.fileExt = fileExt; this.numFiles = numFiles; this.totalSize = totalSize; @@ -223,8 +242,9 @@ public CacheIndexInputStats(String fileExt, long numFiles, ByteSizeValue totalSi public static CacheIndexInputStats combine(CacheIndexInputStats cis1, CacheIndexInputStats cis2) { if (cis1.getFileExt().equals(cis2.getFileExt()) == false) { assert false : "can only combine same file extensions"; - throw new IllegalArgumentException("can only combine same file extensions but was " + - cis1.fileExt + " and " + cis2.fileExt); + throw new IllegalArgumentException( + "can only combine same file extensions but was " + cis1.fileExt + " and " + cis2.fileExt + ); } return new CacheIndexInputStats( cis1.fileExt, @@ -310,7 +330,7 @@ public ByteSizeValue getMaxSize() { } public ByteSizeValue getAverageSize() { - final double average = (double) totalSize.getBytes()/ (double) numFiles; + final double average = (double) totalSize.getBytes() / (double) numFiles; return new ByteSizeValue(Math.round(average)); } @@ -454,15 +474,29 @@ public boolean equals(Object other) { @Override public int hashCode() { - return Objects.hash(fileExt, numFiles, totalSize, - minSize, maxSize, - openCount, closeCount, - forwardSmallSeeks, backwardSmallSeeks, - forwardLargeSeeks, backwardLargeSeeks, - contiguousReads, nonContiguousReads, - cachedBytesRead, indexCacheBytesRead, - cachedBytesWritten, directBytesRead, optimizedBytesRead, - blobStoreBytesRequested, luceneBytesRead, currentIndexCacheFills); + return Objects.hash( + fileExt, + numFiles, + totalSize, + minSize, + maxSize, + openCount, + closeCount, + forwardSmallSeeks, + backwardSmallSeeks, + forwardLargeSeeks, + backwardLargeSeeks, + contiguousReads, + nonContiguousReads, + cachedBytesRead, + indexCacheBytesRead, + cachedBytesWritten, + directBytesRead, + optimizedBytesRead, + blobStoreBytesRequested, + luceneBytesRead, + currentIndexCacheFills + ); } } @@ -488,8 +522,7 @@ public Counter(final long count, final long total, final long min, final long ma } public Counter add(Counter counter) { - return new Counter(count + counter.count, total + counter.total, - Math.min(min, counter.min), Math.max(max, counter.max)); + return new Counter(count + counter.count, total + counter.total, Math.min(min, counter.min), Math.max(max, counter.max)); } @Override @@ -514,8 +547,7 @@ public final XContentBuilder toXContent(XContentBuilder builder, Params params) return builder; } - void innerToXContent(XContentBuilder builder, Params params) throws IOException { - } + void innerToXContent(XContentBuilder builder, Params params) throws IOException {} public long getCount() { return count; @@ -542,10 +574,7 @@ public boolean equals(Object other) { return false; } Counter that = (Counter) other; - return count == that.count - && total == that.total - && min == that.min - && max == that.max; + return count == that.count && total == that.total && min == that.min && max == that.max; } @Override @@ -569,8 +598,13 @@ public TimedCounter(long count, long total, long min, long max, long totalNanose } public TimedCounter add(TimedCounter counter) { - return new TimedCounter(count + counter.count, total + counter.total, - Math.min(min, counter.min), Math.max(max, counter.max), totalNanoseconds + counter.totalNanoseconds); + return new TimedCounter( + count + counter.count, + total + counter.total, + Math.min(min, counter.min), + Math.max(max, counter.max), + totalNanoseconds + counter.totalNanoseconds + ); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/searchablesnapshots/SearchableSnapshotsConstants.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/searchablesnapshots/SearchableSnapshotsConstants.java index 8951060576a67..f44f42c241113 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/searchablesnapshots/SearchableSnapshotsConstants.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/searchablesnapshots/SearchableSnapshotsConstants.java @@ -14,7 +14,10 @@ public class SearchableSnapshotsConstants { // This should really be in the searchable-snapshots module, but ILM needs access to it // to short-circuit if not allowed. We should consider making the coupling looser, // perhaps through SPI. - public static final LicensedFeature.Momentary SEARCHABLE_SNAPSHOT_FEATURE = - LicensedFeature.momentary(null, "searchable-snapshots", License.OperationMode.ENTERPRISE); + public static final LicensedFeature.Momentary SEARCHABLE_SNAPSHOT_FEATURE = LicensedFeature.momentary( + null, + "searchable-snapshots", + License.OperationMode.ENTERPRISE + ); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/CommandLineHttpClient.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/CommandLineHttpClient.java index 0be6294d1d239..bb64ff90af63e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/CommandLineHttpClient.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/CommandLineHttpClient.java @@ -6,31 +6,27 @@ */ package org.elasticsearch.xpack.core.security; -import org.elasticsearch.common.hash.MessageDigests; -import org.elasticsearch.common.io.Streams; -import org.elasticsearch.core.CharArrays; -import org.elasticsearch.core.CheckedFunction; import org.elasticsearch.common.CheckedSupplier; import org.elasticsearch.common.Strings; -import org.elasticsearch.core.SuppressForbidden; -import org.elasticsearch.core.Releasables; +import org.elasticsearch.common.hash.MessageDigests; +import org.elasticsearch.common.io.Streams; import org.elasticsearch.common.network.InetAddresses; import org.elasticsearch.common.network.NetworkService; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.ssl.SslConfiguration; -import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.core.CharArrays; +import org.elasticsearch.core.CheckedFunction; +import org.elasticsearch.core.Releasables; +import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.env.Environment; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.xpack.core.common.socket.SocketAccess; +import org.elasticsearch.xpack.core.security.HttpResponse.HttpResponseBuilder; import org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken; import org.elasticsearch.xpack.core.ssl.SSLService; -import org.elasticsearch.xpack.core.security.HttpResponse.HttpResponseBuilder; -import javax.net.ssl.HttpsURLConnection; -import javax.net.ssl.SSLContext; -import javax.net.ssl.TrustManager; -import javax.net.ssl.X509TrustManager; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -54,6 +50,11 @@ import java.util.Map; import java.util.Objects; +import javax.net.ssl.HttpsURLConnection; +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; + import static org.elasticsearch.http.HttpTransportSettings.SETTING_HTTP_PORT; import static org.elasticsearch.http.HttpTransportSettings.SETTING_HTTP_PUBLISH_HOST; import static org.elasticsearch.http.HttpTransportSettings.SETTING_HTTP_PUBLISH_PORT; @@ -98,9 +99,14 @@ public CommandLineHttpClient(Environment env, String pinnedCaCertFingerprint) { * handler of the response Input Stream. * @return HTTP protocol response code. */ - public HttpResponse execute(String method, URL url, String user, SecureString password, - CheckedSupplier requestBodySupplier, - CheckedFunction responseHandler) throws Exception { + public HttpResponse execute( + String method, + URL url, + String user, + SecureString password, + CheckedSupplier requestBodySupplier, + CheckedFunction responseHandler + ) throws Exception { final String authorizationHeader = UsernamePasswordToken.basicAuthHeaderValue(user, password); return execute(method, url, authorizationHeader, requestBodySupplier, responseHandler); @@ -118,17 +124,25 @@ public HttpResponse execute(String method, URL url, String user, SecureString pa * handler of the response Input Stream. * @return HTTP protocol response code. */ - public HttpResponse execute(String method, URL url, SecureString apiKey, + public HttpResponse execute( + String method, + URL url, + SecureString apiKey, CheckedSupplier requestBodySupplier, - CheckedFunction responseHandler) throws Exception { + CheckedFunction responseHandler + ) throws Exception { final String authorizationHeaderValue = apiKeyHeaderValue(apiKey); return execute(method, url, authorizationHeaderValue, requestBodySupplier, responseHandler); } @SuppressForbidden(reason = "We call connect in doPrivileged and provide SocketPermission") - private HttpResponse execute(String method, URL url, String authorizationHeader, + private HttpResponse execute( + String method, + URL url, + String authorizationHeader, CheckedSupplier requestBodySupplier, - CheckedFunction responseHandler) throws Exception { + CheckedFunction responseHandler + ) throws Exception { final HttpURLConnection conn; // If using SSL, need a custom service because it's likely a self-signed certificate if ("https".equalsIgnoreCase(url.getProtocol())) { @@ -214,8 +228,10 @@ public String getDefaultURL() { } return scheme + "://" + InetAddresses.toUriString(publishAddress) + ":" + port; } catch (Exception e) { - throw new IllegalStateException("unable to determine default URL from settings, please use the -u option to explicitly " + - "provide the url", e); + throw new IllegalStateException( + "unable to determine default URL from settings, please use the -u option to explicitly " + "provide the url", + e + ); } } @@ -249,8 +265,7 @@ public static String getErrorCause(HttpResponse httpResponse) { * If cluster is not up yet (connection refused or master is unavailable), we will retry @retries number of times * If status is 'Red', we will wait for 'Yellow' for 30s (default timeout) */ - public void checkClusterHealthWithRetriesWaitingForCluster(String username, SecureString password, int retries) - throws Exception { + public void checkClusterHealthWithRetriesWaitingForCluster(String username, SecureString password, int retries) throws Exception { final URL clusterHealthUrl = createURL(new URL(getDefaultURL()), "_cluster/health", "?wait_for_status=yellow&pretty"); HttpResponse response; try { @@ -274,12 +289,14 @@ public void checkClusterHealthWithRetriesWaitingForCluster(String username, Secu checkClusterHealthWithRetriesWaitingForCluster(username, password, retries); return; } else { - throw new IllegalStateException("Failed to determine the health of the cluster. Unexpected http status [" - + responseStatus + "]"); + throw new IllegalStateException( + "Failed to determine the health of the cluster. Unexpected http status [" + responseStatus + "]" + ); } } - throw new IllegalStateException("Failed to determine the health of the cluster. Unexpected http status [" - + responseStatus + "]"); + throw new IllegalStateException( + "Failed to determine the health of the cluster. Unexpected http status [" + responseStatus + "]" + ); } else { final String clusterStatus = Objects.toString(response.getResponseBody().get("status"), ""); if (clusterStatus.isEmpty()) { @@ -287,8 +304,7 @@ public void checkClusterHealthWithRetriesWaitingForCluster(String username, Secu "Failed to determine the health of the cluster. Cluster health API did not return a status value." ); } else if ("red".equalsIgnoreCase(clusterStatus)) { - throw new IllegalStateException( - "Failed to determine the health of the cluster. Cluster health is currently RED."); + throw new IllegalStateException("Failed to determine the health of the cluster. Cluster health is currently RED."); } // else it is yellow or green so we can continue } @@ -312,7 +328,7 @@ public static String apiKeyHeaderValue(SecureString apiKey) { chars.put(apiKey.getChars()); charBytes = CharArrays.toUtf8Bytes(chars.array()); - //TODO we still have passwords in Strings in headers. Maybe we can look into using a CharSequence? + // TODO we still have passwords in Strings in headers. Maybe we can look into using a CharSequence? String apiKeyToken = Base64.getEncoder().encodeToString(charBytes); return "ApiKey " + apiKeyToken; } finally { @@ -329,8 +345,7 @@ public static String apiKeyHeaderValue(SecureString apiKey) { */ private TrustManager fingerprintTrustingTrustManager(String pinnedCaCertFingerprint) { final TrustManager trustManager = new X509TrustManager() { - public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { - } + public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {} public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { if (chain.length < 2) { @@ -339,7 +354,7 @@ public void checkServerTrusted(X509Certificate[] chain, String authType) throws final Certificate caCertFromChain = chain[1]; MessageDigest sha256 = MessageDigests.sha256(); sha256.update(caCertFromChain.getEncoded()); - if (MessageDigests.toHexString(sha256.digest()).equals(pinnedCaCertFingerprint) == false ) { + if (MessageDigests.toHexString(sha256.digest()).equals(pinnedCaCertFingerprint) == false) { throw new CertificateException(); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/EnrollmentToken.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/EnrollmentToken.java index 67aa083e48b6e..2c68b914807ad 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/EnrollmentToken.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/EnrollmentToken.java @@ -28,12 +28,23 @@ public class EnrollmentToken { private final String apiKey; private final String fingerprint; private final String version; - private final List boundAddress; + private final List boundAddress; - public String getApiKey() { return apiKey; } - public String getFingerprint() { return fingerprint; } - public String getVersion() { return version; } - public List getBoundAddress() { return boundAddress; } + public String getApiKey() { + return apiKey; + } + + public String getFingerprint() { + return fingerprint; + } + + public String getVersion() { + return version; + } + + public List getBoundAddress() { + return boundAddress; + } private static final ParseField API_KEY = new ParseField("key"); private static final ParseField FINGERPRINT = new ParseField("fgr"); @@ -41,8 +52,11 @@ public class EnrollmentToken { private static final ParseField ADDRESS = new ParseField("adr"); @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("enrollment_token", false, - a -> new EnrollmentToken((String) a[0], (String) a[1], (String) a[2], (List) a[3])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "enrollment_token", + false, + a -> new EnrollmentToken((String) a[0], (String) a[1], (String) a[2], (List) a[3]) + ); static { PARSER.declareString(constructorArg(), API_KEY); @@ -50,6 +64,7 @@ public class EnrollmentToken { PARSER.declareString(constructorArg(), VERSION); PARSER.declareStringArray(constructorArg(), ADDRESS); } + /** * Create an EnrollmentToken * @@ -108,8 +123,10 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; EnrollmentToken that = (EnrollmentToken) o; - return apiKey.equals(that.apiKey) && fingerprint.equals(that.fingerprint) && version.equals(that.version) && boundAddress.equals( - that.boundAddress); + return apiKey.equals(that.apiKey) + && fingerprint.equals(that.fingerprint) + && version.equals(that.version) + && boundAddress.equals(that.boundAddress); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/HttpResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/HttpResponse.java index 6201147d99aa9..fc82f8900b129 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/HttpResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/HttpResponse.java @@ -47,11 +47,12 @@ public HttpResponseBuilder withHttpStatus(final int httpStatus) { return this; } - public HttpResponseBuilder withResponseBody(final String responseJson) - throws ElasticsearchParseException, UnsupportedEncodingException { + public HttpResponseBuilder withResponseBody(final String responseJson) throws ElasticsearchParseException, + UnsupportedEncodingException { if (responseJson == null || responseJson.trim().isEmpty()) { throw new ElasticsearchParseException( - "Invalid string provided as http response body, Failed to parse content to form response body."); + "Invalid string provided as http response body, Failed to parse content to form response body." + ); } this.responseBody = XContentHelper.convertToMap(XContentType.JSON.xContent(), responseJson, false); return this; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/ScrollHelper.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/ScrollHelper.java index 230fdb20c469e..7e283e75b21d2 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/ScrollHelper.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/ScrollHelper.java @@ -29,14 +29,19 @@ public final class ScrollHelper { private static final Logger LOGGER = LogManager.getLogger(ScrollHelper.class); + private ScrollHelper() {} /** * This method fetches all results for the given search request, parses them using the given hit parser and calls the * listener once done. */ - public static void fetchAllByEntity(Client client, SearchRequest request, final ActionListener> listener, - Function hitParser) { + public static void fetchAllByEntity( + Client client, + SearchRequest request, + final ActionListener> listener, + Function hitParser + ) { final List results = new ArrayList<>(); if (request.scroll() == null) { // we do scroll by default lets see if we can get rid of this at some point. throw new IllegalArgumentException("request must have scroll set"); @@ -45,67 +50,83 @@ public static void fetchAllByEntity(Client client, SearchRequest request, fi if (response != null && response.getScrollId() != null) { ClearScrollRequest clearScrollRequest = new ClearScrollRequest(); clearScrollRequest.addScrollId(response.getScrollId()); - client.clearScroll(clearScrollRequest, ActionListener.wrap((r) -> {}, e -> - LOGGER.warn(new ParameterizedMessage("clear scroll failed for scroll id [{}]", response.getScrollId()), e) - )); + client.clearScroll( + clearScrollRequest, + ActionListener.wrap( + (r) -> {}, + e -> LOGGER.warn(new ParameterizedMessage("clear scroll failed for scroll id [{}]", response.getScrollId()), e) + ) + ); } }; // This function is MADNESS! But it works, don't think about it too hard... // simon edit: just watch this if you got this far https://www.youtube.com/watch?v=W-lF106Dgk8 - client.search(request, new ContextPreservingActionListener<>(client.threadPool().getThreadContext().newRestorableContext(true), + client.search( + request, + new ContextPreservingActionListener<>( + client.threadPool().getThreadContext().newRestorableContext(true), new ActionListener() { - private volatile SearchResponse lastResponse = null; + private volatile SearchResponse lastResponse = null; + + @Override + public void onResponse(SearchResponse resp) { + try { + lastResponse = resp; + if (resp.getHits().getHits().length > 0) { + for (SearchHit hit : resp.getHits().getHits()) { + final T oneResult = hitParser.apply(hit); + if (oneResult != null) { + results.add(oneResult); + } + } - @Override - public void onResponse(SearchResponse resp) { - try { - lastResponse = resp; - if (resp.getHits().getHits().length > 0) { - for (SearchHit hit : resp.getHits().getHits()) { - final T oneResult = hitParser.apply(hit); - if (oneResult != null) { - results.add(oneResult); + if (results.size() > resp.getHits().getTotalHits().value) { + clearScroll.accept(lastResponse); + listener.onFailure( + new IllegalStateException( + "scrolling returned more hits [" + + results.size() + + "] than expected [" + + resp.getHits().getTotalHits().value + + "] so bailing out to prevent unbounded " + + "memory consumption." + ) + ); + } else if (results.size() == resp.getHits().getTotalHits().value) { + clearScroll.accept(resp); + // Finally, return the list of the entity + listener.onResponse(Collections.unmodifiableList(results)); + } else { + SearchScrollRequest scrollRequest = new SearchScrollRequest(resp.getScrollId()); + scrollRequest.scroll(request.scroll().keepAlive()); + client.searchScroll(scrollRequest, this); + } + } else { + clearScroll.accept(resp); + // Finally, return the list of the entity + listener.onResponse(Collections.unmodifiableList(results)); } + } catch (Exception e) { + onFailure(e); // lets clean up things } + } - if (results.size() > resp.getHits().getTotalHits().value) { + @Override + public void onFailure(Exception t) { + try { + // attempt to clear the scroll request clearScroll.accept(lastResponse); - listener.onFailure(new IllegalStateException("scrolling returned more hits [" + results.size() - + "] than expected [" + resp.getHits().getTotalHits().value + "] so bailing out to prevent unbounded " - + "memory consumption.")); - } else if (results.size() == resp.getHits().getTotalHits().value) { - clearScroll.accept(resp); - // Finally, return the list of the entity - listener.onResponse(Collections.unmodifiableList(results)); - } else { - SearchScrollRequest scrollRequest = new SearchScrollRequest(resp.getScrollId()); - scrollRequest.scroll(request.scroll().keepAlive()); - client.searchScroll(scrollRequest, this); + } finally { + if (t instanceof IndexNotFoundException) { + // since this is expected to happen at times, we just call the listener with an empty list + listener.onResponse(Collections.emptyList()); + } else { + listener.onFailure(t); + } } - } else { - clearScroll.accept(resp); - // Finally, return the list of the entity - listener.onResponse(Collections.unmodifiableList(results)); } - } catch (Exception e){ - onFailure(e); // lets clean up things } - } - - @Override - public void onFailure(Exception t) { - try { - // attempt to clear the scroll request - clearScroll.accept(lastResponse); - } finally { - if (t instanceof IndexNotFoundException) { - // since this is expected to happen at times, we just call the listener with an empty list - listener.onResponse(Collections.emptyList()); - } else { - listener.onFailure(t); - } - } - } - })); + ) + ); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/SecurityContext.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/SecurityContext.java index 3e47b63f3c8ed..59c62f61fe7c2 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/SecurityContext.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/SecurityContext.java @@ -10,15 +10,15 @@ import org.apache.logging.log4j.Logger; import org.elasticsearch.ElasticsearchSecurityException; import org.elasticsearch.Version; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.common.util.concurrent.ThreadContext.StoredContext; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.core.Nullable; import org.elasticsearch.node.Node; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.security.authc.Authentication; import org.elasticsearch.xpack.core.security.authc.Authentication.AuthenticationType; import org.elasticsearch.xpack.core.security.authc.AuthenticationField; @@ -117,7 +117,8 @@ public void setUser(User user, Version version) { lookedUpBy = null; } setAuthentication( - new Authentication(user, authenticatedBy, lookedUpBy, version, AuthenticationType.INTERNAL, Collections.emptyMap())); + new Authentication(user, authenticatedBy, lookedUpBy, version, AuthenticationType.INTERNAL, Collections.emptyMap()) + ); } /** Writes the authentication to the thread context */ @@ -163,9 +164,16 @@ public void executeAfterRewritingAuthentication(Consumer consumer final StoredContext original = threadContext.newStoredContext(true); final Authentication authentication = getAuthentication(); try (ThreadContext.StoredContext ignore = threadContext.stashContext()) { - setAuthentication(new Authentication(authentication.getUser(), authentication.getAuthenticatedBy(), - authentication.getLookedUpBy(), version, authentication.getAuthenticationType(), - rewriteMetadataForApiKeyRoleDescriptors(version, authentication))); + setAuthentication( + new Authentication( + authentication.getUser(), + authentication.getAuthenticatedBy(), + authentication.getLookedUpBy(), + version, + authentication.getAuthenticationType(), + rewriteMetadataForApiKeyRoleDescriptors(version, authentication) + ) + ); existingRequestHeaders.forEach((k, v) -> { if (false == AuthenticationField.AUTHENTICATION_KEY.equals(k)) { threadContext.putHeader(k, v); @@ -182,18 +190,26 @@ private Map rewriteMetadataForApiKeyRoleDescriptors(Version stre if (authentication.getVersion().onOrAfter(VERSION_API_KEY_ROLES_AS_BYTES) && streamVersion.before(VERSION_API_KEY_ROLES_AS_BYTES)) { metadata = new HashMap<>(metadata); - metadata.put(API_KEY_ROLE_DESCRIPTORS_KEY, - convertRoleDescriptorsBytesToMap((BytesReference) metadata.get(API_KEY_ROLE_DESCRIPTORS_KEY))); - metadata.put(API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY, - convertRoleDescriptorsBytesToMap((BytesReference) metadata.get(API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY))); + metadata.put( + API_KEY_ROLE_DESCRIPTORS_KEY, + convertRoleDescriptorsBytesToMap((BytesReference) metadata.get(API_KEY_ROLE_DESCRIPTORS_KEY)) + ); + metadata.put( + API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY, + convertRoleDescriptorsBytesToMap((BytesReference) metadata.get(API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY)) + ); } else if (authentication.getVersion().before(VERSION_API_KEY_ROLES_AS_BYTES) && streamVersion.onOrAfter(VERSION_API_KEY_ROLES_AS_BYTES)) { - metadata = new HashMap<>(metadata); - metadata.put(API_KEY_ROLE_DESCRIPTORS_KEY, - convertRoleDescriptorsMapToBytes((Map)metadata.get(API_KEY_ROLE_DESCRIPTORS_KEY))); - metadata.put(API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY, - convertRoleDescriptorsMapToBytes((Map) metadata.get(API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY))); - } + metadata = new HashMap<>(metadata); + metadata.put( + API_KEY_ROLE_DESCRIPTORS_KEY, + convertRoleDescriptorsMapToBytes((Map) metadata.get(API_KEY_ROLE_DESCRIPTORS_KEY)) + ); + metadata.put( + API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY, + convertRoleDescriptorsMapToBytes((Map) metadata.get(API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY)) + ); + } } return metadata; } @@ -203,7 +219,7 @@ private Map convertRoleDescriptorsBytesToMap(BytesReference role } private BytesReference convertRoleDescriptorsMapToBytes(Map roleDescriptorsMap) { - try(XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent())) { + try (XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent())) { builder.map(roleDescriptorsMap); return BytesReference.bytes(builder); } catch (IOException e) { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/SecurityExtension.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/SecurityExtension.java index 2db295644553e..8233b9991dc87 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/SecurityExtension.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/SecurityExtension.java @@ -38,19 +38,26 @@ public interface SecurityExtension { interface SecurityComponents { /** Global settings for the current node */ Settings settings(); + /** Provides access to key filesystem paths */ Environment environment(); + /** An internal client for retrieving information/data from this cluster */ Client client(); + /** The Elasticsearch thread pools */ ThreadPool threadPool(); + /** Provides the ability to monitor files for changes */ ResourceWatcherService resourceWatcherService(); + /** Access to listen to changes in cluster state and settings */ ClusterService clusterService(); + /** Provides support for mapping users' roles from groups and metadata */ UserRoleMapper roleMapper(); } + /** * Returns authentication realm implementations added by this extension. * @@ -99,8 +106,7 @@ default AuthenticationFailureHandler getAuthenticationFailureHandler(SecurityCom * * @param components Access to components that may be used to build roles */ - default List, ActionListener>> - getRolesProviders(SecurityComponents components) { + default List, ActionListener>> getRolesProviders(SecurityComponents components) { return Collections.emptyList(); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/SecurityFeatureSetUsage.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/SecurityFeatureSetUsage.java index f20b24d2dc287..dcdb2fcc1f1cc 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/SecurityFeatureSetUsage.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/SecurityFeatureSetUsage.java @@ -64,12 +64,20 @@ public SecurityFeatureSetUsage(StreamInput in) throws IOException { } } - public SecurityFeatureSetUsage(boolean enabled, Map realmsUsage, - Map rolesStoreUsage, Map roleMappingStoreUsage, - Map sslUsage, Map auditUsage, - Map ipFilterUsage, Map anonymousUsage, - Map tokenServiceUsage, Map apiKeyServiceUsage, - Map fips140Usage, Map operatorPrivilegesUsage) { + public SecurityFeatureSetUsage( + boolean enabled, + Map realmsUsage, + Map rolesStoreUsage, + Map roleMappingStoreUsage, + Map sslUsage, + Map auditUsage, + Map ipFilterUsage, + Map anonymousUsage, + Map tokenServiceUsage, + Map apiKeyServiceUsage, + Map fips140Usage, + Map operatorPrivilegesUsage + ) { super(XPackField.SECURITY, true, enabled); this.realmsUsage = realmsUsage; this.rolesStoreUsage = rolesStoreUsage; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/SecurityField.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/SecurityField.java index cd12fdcd79972..17bdf338f73ef 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/SecurityField.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/SecurityField.java @@ -17,16 +17,25 @@ public final class SecurityField { public static final String NAME4 = XPackField.SECURITY + "4"; public static final String NIO = XPackField.SECURITY + "-nio"; - public static final Setting> USER_SETTING = - new Setting<>(setting("user"), (String) null, Optional::ofNullable, Setting.Property.NodeScope); + public static final Setting> USER_SETTING = new Setting<>( + setting("user"), + (String) null, + Optional::ofNullable, + Setting.Property.NodeScope + ); // Document and Field Level Security are Platinum+ - private static final String DLS_FLS_FEATURE_FAMILY = "security-dls-fls"; - public static final LicensedFeature.Momentary DOCUMENT_LEVEL_SECURITY_FEATURE = - LicensedFeature.momentary(DLS_FLS_FEATURE_FAMILY, "dls", License.OperationMode.PLATINUM); - public static final LicensedFeature.Momentary FIELD_LEVEL_SECURITY_FEATURE = - LicensedFeature.momentary(DLS_FLS_FEATURE_FAMILY, "fls", License.OperationMode.PLATINUM); - + private static final String DLS_FLS_FEATURE_FAMILY = "security-dls-fls"; + public static final LicensedFeature.Momentary DOCUMENT_LEVEL_SECURITY_FEATURE = LicensedFeature.momentary( + DLS_FLS_FEATURE_FAMILY, + "dls", + License.OperationMode.PLATINUM + ); + public static final LicensedFeature.Momentary FIELD_LEVEL_SECURITY_FEATURE = LicensedFeature.momentary( + DLS_FLS_FEATURE_FAMILY, + "fls", + License.OperationMode.PLATINUM + ); private SecurityField() { // Assert that DLS and FLS have identical license requirement so that license check can be simplified to just diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/SecuritySettings.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/SecuritySettings.java index 96ae20b3b0adc..36f3228b80065 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/SecuritySettings.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/SecuritySettings.java @@ -21,9 +21,18 @@ public static Settings addTransportSettings(final Settings settings) { if (NetworkModule.TRANSPORT_TYPE_SETTING.exists(settings)) { final String transportType = NetworkModule.TRANSPORT_TYPE_SETTING.get(settings); if (SecurityField.NAME4.equals(transportType) == false && SecurityField.NIO.equals(transportType) == false) { - throw new IllegalArgumentException("transport type setting [" + NetworkModule.TRANSPORT_TYPE_KEY - + "] must be [" + SecurityField.NAME4 + "] or [" + SecurityField.NIO + "]" + " but is [" - + transportType + "]"); + throw new IllegalArgumentException( + "transport type setting [" + + NetworkModule.TRANSPORT_TYPE_KEY + + "] must be [" + + SecurityField.NAME4 + + "] or [" + + SecurityField.NIO + + "]" + + " but is [" + + transportType + + "]" + ); } } else { // default to security4 @@ -40,8 +49,9 @@ public static Settings addUserSettings(final Settings settings) { userOptional.ifPresent(userSetting -> { final int i = userSetting.indexOf(":"); if (i < 0 || i == userSetting.length() - 1) { - throw new IllegalArgumentException("invalid [" + SecurityField.USER_SETTING.getKey() - + "] setting. must be in the form of \":\""); + throw new IllegalArgumentException( + "invalid [" + SecurityField.USER_SETTING.getKey() + "] setting. must be in the form of \":\"" + ); } String username = userSetting.substring(0, i); String password = userSetting.substring(i + 1); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/ApiKey.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/ApiKey.java index 491febdbbe57d..7b65a2e8d9306 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/ApiKey.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/ApiKey.java @@ -8,12 +8,12 @@ package org.elasticsearch.xpack.core.security.action; import org.elasticsearch.Version; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -40,15 +40,23 @@ public final class ApiKey implements ToXContentObject, Writeable { private final String realm; private final Map metadata; - public ApiKey(String name, String id, Instant creation, Instant expiration, boolean invalidated, String username, String realm, - @Nullable Map metadata) { + public ApiKey( + String name, + String id, + Instant creation, + Instant expiration, + boolean invalidated, + String username, + String realm, + @Nullable Map metadata + ) { this.name = name; this.id = id; // As we do not yet support the nanosecond precision when we serialize to JSON, // here creating the 'Instant' of milliseconds precision. // This Instant can then be used for date comparison. this.creation = Instant.ofEpochMilli(creation.toEpochMilli()); - this.expiration = (expiration != null) ? Instant.ofEpochMilli(expiration.toEpochMilli()): null; + this.expiration = (expiration != null) ? Instant.ofEpochMilli(expiration.toEpochMilli()) : null; this.invalidated = invalidated; this.username = username; this.realm = realm; @@ -114,22 +122,17 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws } public XContentBuilder innerToXContent(XContentBuilder builder, Params params) throws IOException { - builder - .field("id", id) - .field("name", name) - .field("creation", creation.toEpochMilli()); + builder.field("id", id).field("name", name).field("creation", creation.toEpochMilli()); if (expiration != null) { builder.field("expiration", expiration.toEpochMilli()); } - builder - .field("invalidated", invalidated) + builder.field("invalidated", invalidated) .field("username", username) .field("realm", realm) .field("metadata", (metadata == null ? Map.of() : metadata)); return builder; } - @Override public void writeTo(StreamOutput out) throws IOException { if (out.getVersion().onOrAfter(Version.V_7_5_0)) { @@ -166,20 +169,27 @@ public boolean equals(Object obj) { } ApiKey other = (ApiKey) obj; return Objects.equals(name, other.name) - && Objects.equals(id, other.id) - && Objects.equals(creation, other.creation) - && Objects.equals(expiration, other.expiration) - && Objects.equals(invalidated, other.invalidated) - && Objects.equals(username, other.username) - && Objects.equals(realm, other.realm) - && Objects.equals(metadata, other.metadata); + && Objects.equals(id, other.id) + && Objects.equals(creation, other.creation) + && Objects.equals(expiration, other.expiration) + && Objects.equals(invalidated, other.invalidated) + && Objects.equals(username, other.username) + && Objects.equals(realm, other.realm) + && Objects.equals(metadata, other.metadata); } @SuppressWarnings("unchecked") static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("api_key", args -> { - return new ApiKey((String) args[0], (String) args[1], Instant.ofEpochMilli((Long) args[2]), - (args[3] == null) ? null : Instant.ofEpochMilli((Long) args[3]), (Boolean) args[4], (String) args[5], (String) args[6], - (args[7] == null) ? null : (Map) args[7]); + return new ApiKey( + (String) args[0], + (String) args[1], + Instant.ofEpochMilli((Long) args[2]), + (args[3] == null) ? null : Instant.ofEpochMilli((Long) args[3]), + (Boolean) args[4], + (String) args[5], + (String) args[6], + (args[7] == null) ? null : (Map) args[7] + ); }); static { PARSER.declareString(constructorArg(), new ParseField("name")); @@ -198,8 +208,23 @@ public static ApiKey fromXContent(XContentParser parser) throws IOException { @Override public String toString() { - return "ApiKey [name=" + name + ", id=" + id + ", creation=" + creation + ", expiration=" + expiration + ", invalidated=" - + invalidated + ", username=" + username + ", realm=" + realm + ", metadata=" + metadata + "]"; + return "ApiKey [name=" + + name + + ", id=" + + id + + ", creation=" + + creation + + ", expiration=" + + expiration + + ", invalidated=" + + invalidated + + ", username=" + + username + + ", realm=" + + realm + + ", metadata=" + + metadata + + "]"; } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/ClearSecurityCacheResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/ClearSecurityCacheResponse.java index 89bc598a60a82..41b36deb0f2ae 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/ClearSecurityCacheResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/ClearSecurityCacheResponse.java @@ -20,8 +20,7 @@ import java.io.IOException; import java.util.List; -public class ClearSecurityCacheResponse extends BaseNodesResponse - implements ToXContentFragment { +public class ClearSecurityCacheResponse extends BaseNodesResponse implements ToXContentFragment { public ClearSecurityCacheResponse(StreamInput in) throws IOException { super(in); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/CreateApiKeyRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/CreateApiKeyRequest.java index c78eaee4d4165..716015e7eea34 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/CreateApiKeyRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/CreateApiKeyRequest.java @@ -11,11 +11,11 @@ import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.support.WriteRequest; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.UUIDs; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xpack.core.security.authz.RoleDescriptor; import org.elasticsearch.xpack.core.security.support.MetadataUtils; @@ -58,8 +58,12 @@ public CreateApiKeyRequest(String name, @Nullable List roleDescr this(name, roleDescriptors, expiration, null); } - public CreateApiKeyRequest(String name, @Nullable List roleDescriptors, @Nullable TimeValue expiration, - @Nullable Map metadata) { + public CreateApiKeyRequest( + String name, + @Nullable List roleDescriptors, + @Nullable TimeValue expiration, + @Nullable Map metadata + ) { this(); this.name = name; this.roleDescriptors = (roleDescriptors == null) ? List.of() : List.copyOf(roleDescriptors); @@ -154,8 +158,10 @@ public ActionRequestValidationException validate() { } } if (metadata != null && MetadataUtils.containsReservedMetadata(metadata)) { - validationException = - addValidationError("metadata keys may not start with [" + MetadataUtils.RESERVED_PREFIX + "]", validationException); + validationException = addValidationError( + "metadata keys may not start with [" + MetadataUtils.RESERVED_PREFIX + "]", + validationException + ); } return validationException; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/CreateApiKeyRequestBuilder.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/CreateApiKeyRequestBuilder.java index 1ae34c817a27e..f387296fcc741 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/CreateApiKeyRequestBuilder.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/CreateApiKeyRequestBuilder.java @@ -9,12 +9,12 @@ import org.elasticsearch.action.ActionRequestBuilder; import org.elasticsearch.action.support.WriteRequest; import org.elasticsearch.client.ElasticsearchClient; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; -import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.security.authz.RoleDescriptor; @@ -34,11 +34,17 @@ public final class CreateApiKeyRequestBuilder extends ActionRequestBuilder PARSER = new ConstructingObjectParser<>( - "api_key_request", false, (args, v) -> { - return new CreateApiKeyRequest((String) args[0], (List) args[1], - TimeValue.parseTimeValue((String) args[2], null, "expiration"), - (Map) args[3]); - }); + "api_key_request", + false, + (args, v) -> { + return new CreateApiKeyRequest( + (String) args[0], + (List) args[1], + TimeValue.parseTimeValue((String) args[2], null, "expiration"), + (Map) args[3] + ); + } + ); static { PARSER.declareString(constructorArg(), new ParseField("name")); @@ -81,8 +87,10 @@ public CreateApiKeyRequestBuilder setMetadata(Map metadata) { public CreateApiKeyRequestBuilder source(BytesReference source, XContentType xContentType) throws IOException { final NamedXContentRegistry registry = NamedXContentRegistry.EMPTY; - try (InputStream stream = source.streamInput(); - XContentParser parser = xContentType.xContent().createParser(registry, LoggingDeprecationHandler.INSTANCE, stream)) { + try ( + InputStream stream = source.streamInput(); + XContentParser parser = xContentType.xContent().createParser(registry, LoggingDeprecationHandler.INSTANCE, stream) + ) { CreateApiKeyRequest createApiKeyRequest = parse(parser); setName(createApiKeyRequest.getName()); setRoleDescriptors(createApiKeyRequest.getRoleDescriptors()); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/CreateApiKeyResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/CreateApiKeyResponse.java index 3a0eda0fbd278..a6953c511892d 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/CreateApiKeyResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/CreateApiKeyResponse.java @@ -8,13 +8,13 @@ package org.elasticsearch.xpack.core.security.action; import org.elasticsearch.action.ActionResponse; -import org.elasticsearch.core.CharArrays; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.settings.SecureString; +import org.elasticsearch.core.CharArrays; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -34,9 +34,15 @@ */ public final class CreateApiKeyResponse extends ActionResponse implements ToXContentObject { - static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("create_api_key_response", - args -> new CreateApiKeyResponse((String) args[0], (String) args[1], new SecureString((String) args[2]), - (args[3] == null) ? null : Instant.ofEpochMilli((Long) args[3]))); + static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "create_api_key_response", + args -> new CreateApiKeyResponse( + (String) args[0], + (String) args[1], + new SecureString((String) args[2]), + (args[3] == null) ? null : Instant.ofEpochMilli((Long) args[3]) + ) + ); static { PARSER.declareString(constructorArg(), new ParseField("name")); PARSER.declareString(constructorArg(), new ParseField("id")); @@ -57,7 +63,7 @@ public CreateApiKeyResponse(String name, String id, SecureString key, Instant ex // As we do not yet support the nanosecond precision when we serialize to JSON, // here creating the 'Instant' of milliseconds precision. // This Instant can then be used for date comparison. - this.expiration = (expiration != null) ? Instant.ofEpochMilli(expiration.toEpochMilli()): null; + this.expiration = (expiration != null) ? Instant.ofEpochMilli(expiration.toEpochMilli()) : null; } public CreateApiKeyResponse(StreamInput in) throws IOException { @@ -142,9 +148,7 @@ public static CreateApiKeyResponse fromXContent(XContentParser parser) throws IO @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.startObject() - .field("id", id) - .field("name", name); + builder.startObject().field("id", id).field("name", name); if (expiration != null) { builder.field("expiration", expiration.toEpochMilli()); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/DelegatePkiAuthenticationRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/DelegatePkiAuthenticationRequest.java index 74005301c5f78..42b0892b8d0bc 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/DelegatePkiAuthenticationRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/DelegatePkiAuthenticationRequest.java @@ -9,14 +9,14 @@ import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionRequestValidationException; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.ObjectParser.ValueType; import org.elasticsearch.xpack.core.ssl.CertParsingUtils; import java.io.ByteArrayInputStream; @@ -41,14 +41,17 @@ public final class DelegatePkiAuthenticationRequest extends ActionRequest implem private static final ParseField X509_CERTIFICATE_CHAIN_FIELD = new ParseField("x509_certificate_chain"); public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "delegate_pki_request", false, a -> { - @SuppressWarnings("unchecked") - List certificates = (List) a[0]; - return new DelegatePkiAuthenticationRequest(certificates); - }); + "delegate_pki_request", + false, + a -> { + @SuppressWarnings("unchecked") + List certificates = (List) a[0]; + return new DelegatePkiAuthenticationRequest(certificates); + } + ); static { - PARSER.declareFieldArray(optionalConstructorArg(), (parser,c) -> { + PARSER.declareFieldArray(optionalConstructorArg(), (parser, c) -> { try (ByteArrayInputStream bis = new ByteArrayInputStream(Base64.getDecoder().decode(parser.text()))) { return (X509Certificate) CertificateFactory.getInstance("X.509").generateCertificate(bis); } catch (CertificateException | IOException e) { @@ -128,12 +131,12 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.startObject().startArray(X509_CERTIFICATE_CHAIN_FIELD.getPreferredName()); try { for (X509Certificate cert : certificateChain) { - builder.value(Base64.getEncoder().encodeToString(cert.getEncoded())); - } - } catch (CertificateEncodingException e) { - throw new IOException(e); - } - return builder.endArray().endObject(); + builder.value(Base64.getEncoder().encodeToString(cert.getEncoded())); + } + } catch (CertificateEncodingException e) { + throw new IOException(e); + } + return builder.endArray().endObject(); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/DelegatePkiAuthenticationResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/DelegatePkiAuthenticationResponse.java index 433d959acb652..aa8719410de2a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/DelegatePkiAuthenticationResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/DelegatePkiAuthenticationResponse.java @@ -9,10 +9,10 @@ import org.elasticsearch.Version; import org.elasticsearch.action.ActionResponse; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.security.authc.Authentication; @@ -34,7 +34,7 @@ public final class DelegatePkiAuthenticationResponse extends ActionResponse impl private TimeValue expiresIn; private Authentication authentication; - DelegatePkiAuthenticationResponse() { } + DelegatePkiAuthenticationResponse() {} public DelegatePkiAuthenticationResponse(String accessToken, TimeValue expiresIn, Authentication authentication) { this.accessToken = Objects.requireNonNull(accessToken); @@ -78,9 +78,9 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; DelegatePkiAuthenticationResponse that = (DelegatePkiAuthenticationResponse) o; - return Objects.equals(accessToken, that.accessToken) && - Objects.equals(expiresIn, that.expiresIn) && - Objects.equals(authentication, that.authentication); + return Objects.equals(accessToken, that.accessToken) + && Objects.equals(expiresIn, that.expiresIn) + && Objects.equals(authentication, that.authentication); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/GetApiKeyRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/GetApiKeyRequest.java index 2c76390153323..c9adf9732c9cb 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/GetApiKeyRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/GetApiKeyRequest.java @@ -10,10 +10,10 @@ import org.elasticsearch.Version; import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionRequestValidationException; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; import java.io.IOException; import java.util.Objects; @@ -48,8 +48,13 @@ public GetApiKeyRequest(StreamInput in) throws IOException { } } - public GetApiKeyRequest(@Nullable String realmName, @Nullable String userName, @Nullable String apiKeyId, - @Nullable String apiKeyName, boolean ownedByAuthenticatedUser) { + public GetApiKeyRequest( + @Nullable String realmName, + @Nullable String userName, + @Nullable String apiKeyId, + @Nullable String apiKeyName, + boolean ownedByAuthenticatedUser + ) { this.realmName = textOrNull(realmName); this.userName = textOrNull(userName); this.apiKeyId = textOrNull(apiKeyId); @@ -151,15 +156,17 @@ public ActionRequestValidationException validate() { if (Strings.hasText(apiKeyId) || Strings.hasText(apiKeyName)) { if (Strings.hasText(realmName) || Strings.hasText(userName)) { validationException = addValidationError( - "username or realm name must not be specified when the api key id or api key name is specified", - validationException); + "username or realm name must not be specified when the api key id or api key name is specified", + validationException + ); } } if (ownedByAuthenticatedUser) { if (Strings.hasText(realmName) || Strings.hasText(userName)) { validationException = addValidationError( "neither username nor realm-name may be specified when retrieving owned API keys", - validationException); + validationException + ); } } if (Strings.hasText(apiKeyId) && Strings.hasText(apiKeyName)) { @@ -189,11 +196,11 @@ public boolean equals(Object o) { return false; } GetApiKeyRequest that = (GetApiKeyRequest) o; - return ownedByAuthenticatedUser == that.ownedByAuthenticatedUser && - Objects.equals(realmName, that.realmName) && - Objects.equals(userName, that.userName) && - Objects.equals(apiKeyId, that.apiKeyId) && - Objects.equals(apiKeyName, that.apiKeyName); + return ownedByAuthenticatedUser == that.ownedByAuthenticatedUser + && Objects.equals(realmName, that.realmName) + && Objects.equals(userName, that.userName) + && Objects.equals(apiKeyId, that.apiKeyId) + && Objects.equals(apiKeyName, that.apiKeyName); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/GetApiKeyResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/GetApiKeyResponse.java index 2940f405898f7..f96831f1a8c01 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/GetApiKeyResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/GetApiKeyResponse.java @@ -8,11 +8,11 @@ package org.elasticsearch.xpack.core.security.action; import org.elasticsearch.action.ActionResponse; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -53,8 +53,7 @@ public ApiKey[] getApiKeyInfos() { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.startObject() - .array("api_keys", (Object[]) foundApiKeysInfo); + builder.startObject().array("api_keys", (Object[]) foundApiKeysInfo); return builder.endObject(); } @@ -64,9 +63,10 @@ public void writeTo(StreamOutput out) throws IOException { } @SuppressWarnings("unchecked") - static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("get_api_key_response", args -> { - return (args[0] == null) ? GetApiKeyResponse.emptyResponse() : new GetApiKeyResponse((List) args[0]); - }); + static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "get_api_key_response", + args -> { return (args[0] == null) ? GetApiKeyResponse.emptyResponse() : new GetApiKeyResponse((List) args[0]); } + ); static { PARSER.declareObjectArray(optionalConstructorArg(), (p, c) -> ApiKey.fromXContent(p), new ParseField("api_keys")); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/GrantApiKeyRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/GrantApiKeyRequest.java index d2b7c1c258bc1..35ea240b1fbe0 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/GrantApiKeyRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/GrantApiKeyRequest.java @@ -39,8 +39,7 @@ public static class Grant implements Writeable { private SecureString password; private SecureString accessToken; - public Grant() { - } + public Grant() {} public Grant(StreamInput in) throws IOException { this.type = in.readString(); @@ -149,16 +148,22 @@ public ActionRequestValidationException validate() { return validationException; } - private ActionRequestValidationException validateRequiredField(String fieldName, CharSequence fieldValue, - ActionRequestValidationException validationException) { + private ActionRequestValidationException validateRequiredField( + String fieldName, + CharSequence fieldValue, + ActionRequestValidationException validationException + ) { if (fieldValue == null || fieldValue.length() == 0) { return addValidationError("[" + fieldName + "] is required for grant_type [" + grant.type + "]", validationException); } return validationException; } - private ActionRequestValidationException validateUnsupportedField(String fieldName, CharSequence fieldValue, - ActionRequestValidationException validationException) { + private ActionRequestValidationException validateUnsupportedField( + String fieldName, + CharSequence fieldValue, + ActionRequestValidationException validationException + ) { if (fieldValue != null && fieldValue.length() > 0) { return addValidationError("[" + fieldName + "] is not supported for grant_type [" + grant.type + "]", validationException); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/InvalidateApiKeyRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/InvalidateApiKeyRequest.java index 15043fc0783be..4bc503246c1c1 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/InvalidateApiKeyRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/InvalidateApiKeyRequest.java @@ -10,10 +10,10 @@ import org.elasticsearch.Version; import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionRequestValidationException; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; import java.io.IOException; import java.util.Arrays; @@ -56,8 +56,13 @@ public InvalidateApiKeyRequest(StreamInput in) throws IOException { } } - public InvalidateApiKeyRequest(@Nullable String realmName, @Nullable String userName, - @Nullable String name, boolean ownedByAuthenticatedUser, @Nullable String[] ids) { + public InvalidateApiKeyRequest( + @Nullable String realmName, + @Nullable String userName, + @Nullable String name, + boolean ownedByAuthenticatedUser, + @Nullable String[] ids + ) { validateIds(ids); this.realmName = textOrNull(realmName); this.userName = textOrNull(userName); @@ -130,7 +135,7 @@ public static InvalidateApiKeyRequest usingRealmAndUserName(String realmName, St * @return {@link InvalidateApiKeyRequest} */ public static InvalidateApiKeyRequest usingApiKeyId(String id, boolean ownedByAuthenticatedUser) { - return new InvalidateApiKeyRequest(null, null, null, ownedByAuthenticatedUser, new String[]{ id }); + return new InvalidateApiKeyRequest(null, null, null, ownedByAuthenticatedUser, new String[] { id }); } /** @@ -167,23 +172,30 @@ public static InvalidateApiKeyRequest forOwnedApiKeys() { @Override public ActionRequestValidationException validate() { ActionRequestValidationException validationException = null; - if (Strings.hasText(realmName) == false && Strings.hasText(userName) == false && ids == null - && Strings.hasText(name) == false && ownedByAuthenticatedUser == false) { - validationException = addValidationError("One of [api key id(s), api key name, username, realm name] must be specified if " + - "[owner] flag is false", validationException); + if (Strings.hasText(realmName) == false + && Strings.hasText(userName) == false + && ids == null + && Strings.hasText(name) == false + && ownedByAuthenticatedUser == false) { + validationException = addValidationError( + "One of [api key id(s), api key name, username, realm name] must be specified if " + "[owner] flag is false", + validationException + ); } if (ids != null || Strings.hasText(name)) { if (Strings.hasText(realmName) || Strings.hasText(userName)) { validationException = addValidationError( "username or realm name must not be specified when the api key id(s) or api key name are specified", - validationException); + validationException + ); } } if (ownedByAuthenticatedUser) { if (Strings.hasText(realmName) || Strings.hasText(userName)) { validationException = addValidationError( "neither username nor realm-name may be specified when invalidating owned API keys", - validationException); + validationException + ); } } if (ids != null && Strings.hasText(name)) { @@ -225,11 +237,11 @@ public boolean equals(Object o) { return false; } InvalidateApiKeyRequest that = (InvalidateApiKeyRequest) o; - return ownedByAuthenticatedUser == that.ownedByAuthenticatedUser && - Objects.equals(realmName, that.realmName) && - Objects.equals(userName, that.userName) && - Arrays.equals(ids, that.ids) && - Objects.equals(name, that.name); + return ownedByAuthenticatedUser == that.ownedByAuthenticatedUser + && Objects.equals(realmName, that.realmName) + && Objects.equals(userName, that.userName) + && Arrays.equals(ids, that.ids) + && Objects.equals(name, that.name); } @Override @@ -247,10 +259,14 @@ private void validateIds(@Nullable String[] ids) { final int[] idxOfBlankIds = IntStream.range(0, ids.length).filter(i -> Strings.hasText(ids[i]) == false).toArray(); if (idxOfBlankIds.length > 0) { final ActionRequestValidationException validationException = new ActionRequestValidationException(); - validationException.addValidationError("Field [ids] must not contain blank id, but got blank " - + (idxOfBlankIds.length == 1 ? "id" : "ids") + " at index " - + (idxOfBlankIds.length == 1 ? "position" : "positions") + ": " - + Arrays.toString(idxOfBlankIds)); + validationException.addValidationError( + "Field [ids] must not contain blank id, but got blank " + + (idxOfBlankIds.length == 1 ? "id" : "ids") + + " at index " + + (idxOfBlankIds.length == 1 ? "position" : "positions") + + ": " + + Arrays.toString(idxOfBlankIds) + ); throw validationException; } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/InvalidateApiKeyResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/InvalidateApiKeyResponse.java index a2141ddd2c968..9f26bdc142584 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/InvalidateApiKeyResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/InvalidateApiKeyResponse.java @@ -9,13 +9,13 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.action.ActionResponse; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -56,11 +56,16 @@ public InvalidateApiKeyResponse(StreamInput in) throws IOException { * @param previouslyInvalidatedApiKeys list of previously invalidated API key ids * @param errors list of encountered errors while invalidating API keys */ - public InvalidateApiKeyResponse(List invalidatedApiKeys, List previouslyInvalidatedApiKeys, - @Nullable List errors) { + public InvalidateApiKeyResponse( + List invalidatedApiKeys, + List previouslyInvalidatedApiKeys, + @Nullable List errors + ) { this.invalidatedApiKeys = Objects.requireNonNull(invalidatedApiKeys, "invalidated_api_keys must be provided"); - this.previouslyInvalidatedApiKeys = Objects.requireNonNull(previouslyInvalidatedApiKeys, - "previously_invalidated_api_keys must be provided"); + this.previouslyInvalidatedApiKeys = Objects.requireNonNull( + previouslyInvalidatedApiKeys, + "previously_invalidated_api_keys must be provided" + ); if (null != errors) { this.errors = errors; } else { @@ -122,8 +127,11 @@ public void writeTo(StreamOutput out) throws IOException { PARSER.declareStringArray(constructorArg(), new ParseField("previously_invalidated_api_keys")); // we parse error_count but ignore it while constructing response PARSER.declareInt(constructorArg(), new ParseField("error_count")); - PARSER.declareObjectArray(optionalConstructorArg(), (p, c) -> ElasticsearchException.fromXContent(p), - new ParseField("error_details")); + PARSER.declareObjectArray( + optionalConstructorArg(), + (p, c) -> ElasticsearchException.fromXContent(p), + new ParseField("error_details") + ); } public static InvalidateApiKeyResponse fromXContent(XContentParser parser) throws IOException { @@ -132,8 +140,13 @@ public static InvalidateApiKeyResponse fromXContent(XContentParser parser) throw @Override public String toString() { - return "InvalidateApiKeyResponse [invalidatedApiKeys=" + invalidatedApiKeys + ", previouslyInvalidatedApiKeys=" - + previouslyInvalidatedApiKeys + ", errors=" + errors + "]"; + return "InvalidateApiKeyResponse [invalidatedApiKeys=" + + invalidatedApiKeys + + ", previouslyInvalidatedApiKeys=" + + previouslyInvalidatedApiKeys + + ", errors=" + + errors + + "]"; } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/apikey/QueryApiKeyResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/apikey/QueryApiKeyResponse.java index 257e26850c9a3..9385a0cd9ca52 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/apikey/QueryApiKeyResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/apikey/QueryApiKeyResponse.java @@ -12,9 +12,9 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.lucene.Lucene; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.core.Nullable; import org.elasticsearch.xpack.core.security.action.ApiKey; import java.io.IOException; @@ -62,10 +62,7 @@ public int getCount() { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.startObject() - .field("total", total) - .field("count", items.length) - .array("api_keys", (Object[]) items); + builder.startObject().field("total", total).field("count", items.length).array("api_keys", (Object[]) items); return builder.endObject(); } @@ -77,10 +74,8 @@ public void writeTo(StreamOutput out) throws IOException { @Override public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; QueryApiKeyResponse that = (QueryApiKeyResponse) o; return total == that.total && Arrays.equals(items, that.items); } @@ -139,10 +134,8 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws @Override public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; Item item = (Item) o; return Objects.equals(apiKey, item.apiKey) && Arrays.equals(sortValues, item.sortValues); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/enrollment/KibanaEnrollmentRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/enrollment/KibanaEnrollmentRequest.java index d36ef8fe14c63..10f212b0d9ce0 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/enrollment/KibanaEnrollmentRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/enrollment/KibanaEnrollmentRequest.java @@ -23,7 +23,8 @@ public KibanaEnrollmentRequest(StreamInput in) throws IOException { super(in); } - @Override public ActionRequestValidationException validate() { + @Override + public ActionRequestValidationException validate() { return null; } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/enrollment/KibanaEnrollmentResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/enrollment/KibanaEnrollmentResponse.java index 8bce230731470..58a17bdffcd34 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/enrollment/KibanaEnrollmentResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/enrollment/KibanaEnrollmentResponse.java @@ -13,6 +13,7 @@ import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; + import java.io.IOException; import java.util.Objects; @@ -35,30 +36,40 @@ public KibanaEnrollmentResponse(String tokenName, SecureString tokenValue, Strin this.httpCa = httpCa; } - public String getTokenName() { return tokenName; } - public SecureString getTokenValue() { return tokenValue; } + public String getTokenName() { + return tokenName; + } + + public SecureString getTokenValue() { + return tokenValue; + } + public String getHttpCa() { return httpCa; } - @Override public void writeTo(StreamOutput out) throws IOException { + @Override + public void writeTo(StreamOutput out) throws IOException { out.writeString(tokenName); out.writeSecureString(tokenValue); out.writeString(httpCa); } - @Override public boolean equals(Object o) { + @Override + public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; KibanaEnrollmentResponse that = (KibanaEnrollmentResponse) o; return tokenName.equals(that.tokenName) && tokenValue.equals(that.tokenValue) && httpCa.equals(that.httpCa); } - @Override public int hashCode() { + @Override + public int hashCode() { return Objects.hash(tokenName, tokenValue, httpCa); } - @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + @Override + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject() .startObject("token") .field("name", tokenName) diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/enrollment/NodeEnrollmentRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/enrollment/NodeEnrollmentRequest.java index b1fa2d9248fbc..df82739fc3ff1 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/enrollment/NodeEnrollmentRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/enrollment/NodeEnrollmentRequest.java @@ -15,14 +15,14 @@ public final class NodeEnrollmentRequest extends ActionRequest { - public NodeEnrollmentRequest() { - } + public NodeEnrollmentRequest() {} public NodeEnrollmentRequest(StreamInput in) throws IOException { super(in); } - @Override public ActionRequestValidationException validate() { + @Override + public ActionRequestValidationException validate() { return null; } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/enrollment/NodeEnrollmentResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/enrollment/NodeEnrollmentResponse.java index 594dab1b0f2b0..40545d39e60d1 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/enrollment/NodeEnrollmentResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/enrollment/NodeEnrollmentResponse.java @@ -8,9 +8,9 @@ package org.elasticsearch.xpack.core.security.action.enrollment; import org.elasticsearch.action.ActionResponse; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -45,8 +45,14 @@ public NodeEnrollmentResponse(StreamInput in) throws IOException { nodesAddresses = in.readStringList(); } - public NodeEnrollmentResponse(String httpCaKey, String httpCaCert, String transportCaCert, String transportKey, String transportCert, - List nodesAddresses) { + public NodeEnrollmentResponse( + String httpCaKey, + String httpCaCert, + String transportCaCert, + String transportKey, + String transportCert, + List nodesAddresses + ) { this.httpCaKey = httpCaKey; this.httpCaCert = httpCaCert; this.transportCaCert = transportCaCert; @@ -63,7 +69,9 @@ public String getHttpCaCert() { return httpCaCert; } - public String getTransportCaCert() { return transportCaCert; } + public String getTransportCaCert() { + return transportCaCert; + } public String getTransportKey() { return transportKey; @@ -77,7 +85,8 @@ public List getNodesAddresses() { return nodesAddresses; } - @Override public void writeTo(StreamOutput out) throws IOException { + @Override + public void writeTo(StreamOutput out) throws IOException { out.writeString(httpCaKey); out.writeString(httpCaCert); out.writeString(transportCaCert); @@ -86,7 +95,8 @@ public List getNodesAddresses() { out.writeStringCollection(nodesAddresses); } - @Override public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { + @Override + public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { builder.startObject(); builder.field(HTTP_CA_KEY.getPreferredName(), httpCaKey); builder.field(HTTP_CA_CERT.getPreferredName(), httpCaCert); @@ -97,7 +107,8 @@ public List getNodesAddresses() { return builder.endObject(); } - @Override public boolean equals(Object o) { + @Override + public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; NodeEnrollmentResponse that = (NodeEnrollmentResponse) o; @@ -109,7 +120,8 @@ public List getNodesAddresses() { && nodesAddresses.equals(that.nodesAddresses); } - @Override public int hashCode() { + @Override + public int hashCode() { return Objects.hash(httpCaKey, httpCaCert, transportCaCert, transportKey, transportCert, nodesAddresses); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/oidc/OpenIdConnectAuthenticateRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/oidc/OpenIdConnectAuthenticateRequest.java index a0180d7d0e9d9..7c103d825b17f 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/oidc/OpenIdConnectAuthenticateRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/oidc/OpenIdConnectAuthenticateRequest.java @@ -119,7 +119,6 @@ public void writeTo(StreamOutput out) throws IOException { } public String toString() { - return "{redirectUri=" + redirectUri + ", state=" + state + ", nonce=" + nonce + ", realm=" +realm+"}"; + return "{redirectUri=" + redirectUri + ", state=" + state + ", nonce=" + nonce + ", realm=" + realm + "}"; } } - diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/oidc/OpenIdConnectAuthenticateRequestBuilder.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/oidc/OpenIdConnectAuthenticateRequestBuilder.java index c0bbef817299a..332f5df31c667 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/oidc/OpenIdConnectAuthenticateRequestBuilder.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/oidc/OpenIdConnectAuthenticateRequestBuilder.java @@ -12,8 +12,9 @@ /** * Request builder for populating a {@link OpenIdConnectAuthenticateRequest} */ -public class OpenIdConnectAuthenticateRequestBuilder - extends ActionRequestBuilder { +public class OpenIdConnectAuthenticateRequestBuilder extends ActionRequestBuilder< + OpenIdConnectAuthenticateRequest, + OpenIdConnectAuthenticateResponse> { public OpenIdConnectAuthenticateRequestBuilder(ElasticsearchClient client) { super(client, OpenIdConnectAuthenticateAction.INSTANCE, new OpenIdConnectAuthenticateRequest()); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/oidc/OpenIdConnectAuthenticateResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/oidc/OpenIdConnectAuthenticateResponse.java index cd581e51ac230..c8e63556e5f51 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/oidc/OpenIdConnectAuthenticateResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/oidc/OpenIdConnectAuthenticateResponse.java @@ -22,9 +22,14 @@ public class OpenIdConnectAuthenticateResponse extends ActionResponse { private TimeValue expiresIn; private Authentication authentication; - public OpenIdConnectAuthenticateResponse(Authentication authentication, String accessTokenString, String refreshTokenString, - TimeValue expiresIn) { - this.principal = authentication.getUser().principal();; + public OpenIdConnectAuthenticateResponse( + Authentication authentication, + String accessTokenString, + String refreshTokenString, + TimeValue expiresIn + ) { + this.principal = authentication.getUser().principal(); + ; this.accessTokenString = accessTokenString; this.refreshTokenString = refreshTokenString; this.expiresIn = expiresIn; @@ -58,7 +63,9 @@ public TimeValue getExpiresIn() { return expiresIn; } - public Authentication getAuthentication() { return authentication; } + public Authentication getAuthentication() { + return authentication; + } @Override public void writeTo(StreamOutput out) throws IOException { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/oidc/OpenIdConnectLogoutRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/oidc/OpenIdConnectLogoutRequest.java index 2f020d006e3df..a8050fc3352f9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/oidc/OpenIdConnectLogoutRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/oidc/OpenIdConnectLogoutRequest.java @@ -8,10 +8,10 @@ import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionRequestValidationException; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; import java.io.IOException; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/oidc/OpenIdConnectPrepareAuthenticationRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/oidc/OpenIdConnectPrepareAuthenticationRequest.java index 4edbcaa4ccc79..26388709b084c 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/oidc/OpenIdConnectPrepareAuthenticationRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/oidc/OpenIdConnectPrepareAuthenticationRequest.java @@ -75,8 +75,7 @@ public void setLoginHint(String loginHint) { this.loginHint = loginHint; } - public OpenIdConnectPrepareAuthenticationRequest() { - } + public OpenIdConnectPrepareAuthenticationRequest() {} public OpenIdConnectPrepareAuthenticationRequest(StreamInput in) throws IOException { super(in); @@ -110,8 +109,17 @@ public void writeTo(StreamOutput out) throws IOException { } public String toString() { - return "{realmName=" + realmName + ", issuer=" + issuer + ", login_hint=" + - loginHint + ", state=" + state + ", nonce=" + nonce + "}"; + return "{realmName=" + + realmName + + ", issuer=" + + issuer + + ", login_hint=" + + loginHint + + ", state=" + + state + + ", nonce=" + + nonce + + "}"; } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/oidc/OpenIdConnectPrepareAuthenticationRequestBuilder.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/oidc/OpenIdConnectPrepareAuthenticationRequestBuilder.java index 95acb2aaaa589..ed1fee563a38e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/oidc/OpenIdConnectPrepareAuthenticationRequestBuilder.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/oidc/OpenIdConnectPrepareAuthenticationRequestBuilder.java @@ -12,8 +12,9 @@ /** * Request builder for populating a {@link OpenIdConnectPrepareAuthenticationRequest} */ -public class OpenIdConnectPrepareAuthenticationRequestBuilder - extends ActionRequestBuilder { +public class OpenIdConnectPrepareAuthenticationRequestBuilder extends ActionRequestBuilder< + OpenIdConnectPrepareAuthenticationRequest, + OpenIdConnectPrepareAuthenticationResponse> { public OpenIdConnectPrepareAuthenticationRequestBuilder(ElasticsearchClient client) { super(client, OpenIdConnectPrepareAuthenticationAction.INSTANCE, new OpenIdConnectPrepareAuthenticationRequest()); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/oidc/OpenIdConnectPrepareAuthenticationResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/oidc/OpenIdConnectPrepareAuthenticationResponse.java index 0030d8572e884..a0242876be87a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/oidc/OpenIdConnectPrepareAuthenticationResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/oidc/OpenIdConnectPrepareAuthenticationResponse.java @@ -79,8 +79,15 @@ public void writeTo(StreamOutput out) throws IOException { } public String toString() { - return "{authenticationRequestUrl=" + authenticationRequestUrl + ", state=" + state + ", nonce=" - + nonce + ", realmName" + realmName + "}"; + return "{authenticationRequestUrl=" + + authenticationRequestUrl + + ", state=" + + state + + ", nonce=" + + nonce + + ", realmName" + + realmName + + "}"; } @Override @@ -89,7 +96,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.field("redirect", authenticationRequestUrl); builder.field("state", state); builder.field("nonce", nonce); - if(realmName != null){ + if (realmName != null) { builder.field("realm", realmName); } builder.endObject(); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/privilege/ClearPrivilegesCacheResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/privilege/ClearPrivilegesCacheResponse.java index f9582e1d42c69..9bb11ac8e6893 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/privilege/ClearPrivilegesCacheResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/privilege/ClearPrivilegesCacheResponse.java @@ -20,8 +20,7 @@ import java.io.IOException; import java.util.List; -public class ClearPrivilegesCacheResponse extends BaseNodesResponse - implements ToXContentFragment { +public class ClearPrivilegesCacheResponse extends BaseNodesResponse implements ToXContentFragment { public ClearPrivilegesCacheResponse(StreamInput in) throws IOException { super(in); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/privilege/DeletePrivilegesRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/privilege/DeletePrivilegesRequest.java index 2033caa741b08..6bee6e9bc3ad8 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/privilege/DeletePrivilegesRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/privilege/DeletePrivilegesRequest.java @@ -24,7 +24,9 @@ * A request to delete an application privilege. */ public final class DeletePrivilegesRequest extends ActionRequest - implements ApplicationPrivilegesRequest, WriteRequest { + implements + ApplicationPrivilegesRequest, + WriteRequest { private String application; private String[] privileges; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/privilege/DeletePrivilegesRequestBuilder.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/privilege/DeletePrivilegesRequestBuilder.java index 8bc2bf0ed94a3..3e6cbf5d31b83 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/privilege/DeletePrivilegesRequestBuilder.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/privilege/DeletePrivilegesRequestBuilder.java @@ -14,7 +14,8 @@ * Builder for {@link DeletePrivilegesRequest} */ public final class DeletePrivilegesRequestBuilder extends ActionRequestBuilder - implements WriteRequestBuilder { + implements + WriteRequestBuilder { public DeletePrivilegesRequestBuilder(ElasticsearchClient client) { super(client, DeletePrivilegesAction.INSTANCE, new DeletePrivilegesRequest()); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/privilege/GetBuiltinPrivilegesRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/privilege/GetBuiltinPrivilegesRequest.java index 5f8cac10b82ae..1fdf8ee35d1b6 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/privilege/GetBuiltinPrivilegesRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/privilege/GetBuiltinPrivilegesRequest.java @@ -21,8 +21,7 @@ public GetBuiltinPrivilegesRequest(StreamInput in) throws IOException { super(in); } - public GetBuiltinPrivilegesRequest() { - } + public GetBuiltinPrivilegesRequest() {} @Override public ActionRequestValidationException validate() { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/privilege/GetBuiltinPrivilegesResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/privilege/GetBuiltinPrivilegesResponse.java index 8a6cefe6c7611..d4d99d0b25b7d 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/privilege/GetBuiltinPrivilegesResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/privilege/GetBuiltinPrivilegesResponse.java @@ -26,11 +26,10 @@ public final class GetBuiltinPrivilegesResponse extends ActionResponse { public GetBuiltinPrivilegesResponse(String[] clusterPrivileges, String[] indexPrivileges) { this.clusterPrivileges = Objects.requireNonNull(clusterPrivileges, "Cluster privileges cannot be null"); - this.indexPrivileges = Objects.requireNonNull(indexPrivileges, "Index privileges cannot be null"); + this.indexPrivileges = Objects.requireNonNull(indexPrivileges, "Index privileges cannot be null"); } - public GetBuiltinPrivilegesResponse(Collection clusterPrivileges, - Collection indexPrivileges) { + public GetBuiltinPrivilegesResponse(Collection clusterPrivileges, Collection indexPrivileges) { this(clusterPrivileges.toArray(Strings.EMPTY_ARRAY), indexPrivileges.toArray(Strings.EMPTY_ARRAY)); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/privilege/GetPrivilegesRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/privilege/GetPrivilegesRequest.java index 3a8c316e55ff7..582317c91623c 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/privilege/GetPrivilegesRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/privilege/GetPrivilegesRequest.java @@ -8,10 +8,10 @@ import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionRequestValidationException; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; import java.io.IOException; import java.util.Collection; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/privilege/PutPrivilegesRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/privilege/PutPrivilegesRequest.java index 7436c9a7a76f1..addf296e1d9da 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/privilege/PutPrivilegesRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/privilege/PutPrivilegesRequest.java @@ -64,8 +64,10 @@ public ActionRequestValidationException validate() { } for (String action : privilege.getActions()) { if (action.indexOf('/') == -1 && action.indexOf('*') == -1 && action.indexOf(':') == -1) { - validationException = addValidationError("action [" + action + "] must contain one of [ '/' , '*' , ':' ]", - validationException); + validationException = addValidationError( + "action [" + action + "] must contain one of [ '/' , '*' , ':' ]", + validationException + ); } try { ApplicationPrivilege.validatePrivilegeOrActionName(action); @@ -74,8 +76,16 @@ public ActionRequestValidationException validate() { } } if (MetadataUtils.containsReservedMetadata(privilege.getMetadata())) { - validationException = addValidationError("metadata keys may not start with [" + MetadataUtils.RESERVED_PREFIX - + "] (in privilege " + privilege.getApplication() + ' ' + privilege.getName() + ")", validationException); + validationException = addValidationError( + "metadata keys may not start with [" + + MetadataUtils.RESERVED_PREFIX + + "] (in privilege " + + privilege.getApplication() + + ' ' + + privilege.getName() + + ")", + validationException + ); } } } @@ -107,15 +117,19 @@ public void setPrivileges(Collection privileges) @Override public Collection getApplicationNames() { - return Collections.unmodifiableSet(privileges.stream() - .map(ApplicationPrivilegeDescriptor::getApplication) - .collect(Collectors.toSet())); + return Collections.unmodifiableSet( + privileges.stream().map(ApplicationPrivilegeDescriptor::getApplication).collect(Collectors.toSet()) + ); } @Override public String toString() { - return getClass().getSimpleName() + "{[" + privileges.stream().map(Strings::toString).collect(Collectors.joining(",")) - + "];" + refreshPolicy + "}"; + return getClass().getSimpleName() + + "{[" + + privileges.stream().map(Strings::toString).collect(Collectors.joining(",")) + + "];" + + refreshPolicy + + "}"; } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/privilege/PutPrivilegesRequestBuilder.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/privilege/PutPrivilegesRequestBuilder.java index 3cff84c4bdd95..e3bf1a74d8922 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/privilege/PutPrivilegesRequestBuilder.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/privilege/PutPrivilegesRequestBuilder.java @@ -28,7 +28,8 @@ * Request builder for {@link PutPrivilegesRequest} */ public final class PutPrivilegesRequestBuilder extends ActionRequestBuilder - implements WriteRequestBuilder { + implements + WriteRequestBuilder { public PutPrivilegesRequestBuilder(ElasticsearchClient client) { super(client, PutPrivilegesAction.INSTANCE, new PutPrivilegesRequest()); @@ -46,13 +47,14 @@ ApplicationPrivilegeDescriptor parsePrivilege(XContentParser parser, String appl * The value for each application-name, is an object keyed by privilege name. * The value for each privilege-name is a privilege object which much match the application and privilege names in which it is nested. */ - public PutPrivilegesRequestBuilder source(BytesReference source, XContentType xContentType) - throws IOException { + public PutPrivilegesRequestBuilder source(BytesReference source, XContentType xContentType) throws IOException { Objects.requireNonNull(xContentType); // EMPTY is ok here because we never call namedObject - try (InputStream stream = source.streamInput(); - XContentParser parser = xContentType.xContent() - .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) { + try ( + InputStream stream = source.streamInput(); + XContentParser parser = xContentType.xContent() + .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream) + ) { XContentParser.Token token = parser.currentToken(); if (token == null) { token = parser.nextToken(); @@ -69,8 +71,11 @@ public PutPrivilegesRequestBuilder source(BytesReference source, XContentType xC token = parser.nextToken(); if (token != XContentParser.Token.START_OBJECT) { - throw new ElasticsearchParseException("expected the value for {} to be an object, but found {} instead", - applicationName, token); + throw new ElasticsearchParseException( + "expected the value for {} to be an object, but found {} instead", + applicationName, + token + ); } while (parser.nextToken() != XContentParser.Token.END_OBJECT) { @@ -80,8 +85,11 @@ public PutPrivilegesRequestBuilder source(BytesReference source, XContentType xC token = parser.nextToken(); if (token != XContentParser.Token.START_OBJECT) { - throw new ElasticsearchParseException("expected the value for {} to be an object, but found {} instead", - applicationName, token); + throw new ElasticsearchParseException( + "expected the value for {} to be an object, but found {} instead", + applicationName, + token + ); } privileges.add(parsePrivilege(parser, applicationName, privilegeName)); } @@ -94,12 +102,18 @@ public PutPrivilegesRequestBuilder source(BytesReference source, XContentType xC private void checkPrivilegeName(ApplicationPrivilegeDescriptor privilege, String applicationName, String providedName) { final String privilegeName = privilege.getName(); if (Strings.isNullOrEmpty(applicationName) == false && applicationName.equals(privilege.getApplication()) == false) { - throw new IllegalArgumentException("privilege application [" + privilege.getApplication() - + "] in source does not match the provided application [" + applicationName + "]"); + throw new IllegalArgumentException( + "privilege application [" + + privilege.getApplication() + + "] in source does not match the provided application [" + + applicationName + + "]" + ); } if (Strings.isNullOrEmpty(providedName) == false && providedName.equals(privilegeName) == false) { - throw new IllegalArgumentException("privilege name [" + privilegeName - + "] in source does not match the provided name [" + providedName + "]"); + throw new IllegalArgumentException( + "privilege name [" + privilegeName + "] in source does not match the provided name [" + providedName + "]" + ); } } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/privilege/PutPrivilegesResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/privilege/PutPrivilegesResponse.java index 65e81e4324b7f..d42d6de74b6a7 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/privilege/PutPrivilegesResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/privilege/PutPrivilegesResponse.java @@ -53,4 +53,4 @@ public void writeTo(StreamOutput out) throws IOException { out.writeMap(created, StreamOutput::writeString, StreamOutput::writeStringCollection); } - } +} diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/realm/ClearRealmCacheRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/realm/ClearRealmCacheRequest.java index 98b0632304006..1eb214af7c604 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/realm/ClearRealmCacheRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/realm/ClearRealmCacheRequest.java @@ -18,7 +18,6 @@ public class ClearRealmCacheRequest extends BaseNodesRequest { +public class ClearRealmCacheRequestBuilder extends NodesOperationRequestBuilder< + ClearRealmCacheRequest, + ClearRealmCacheResponse, + ClearRealmCacheRequestBuilder> { public ClearRealmCacheRequestBuilder(ElasticsearchClient client) { this(client, ClearRealmCacheAction.INSTANCE); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/ClearRolesCacheRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/ClearRolesCacheRequest.java index ca25f921566a0..9d24bee7b0c00 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/ClearRolesCacheRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/ClearRolesCacheRequest.java @@ -28,6 +28,7 @@ public ClearRolesCacheRequest(StreamInput in) throws IOException { super(in); names = in.readOptionalStringArray(); } + /** * Sets the roles for which caches will be evicted. When not set all the roles will be evicted from the cache. * @@ -63,7 +64,9 @@ public Node(ClearRolesCacheRequest request) { this.names = request.names(); } - public String[] getNames() { return names; } + public String[] getNames() { + return names; + } @Override public void writeTo(StreamOutput out) throws IOException { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/ClearRolesCacheRequestBuilder.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/ClearRolesCacheRequestBuilder.java index 9ee358429a5b8..99c165a1ddef9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/ClearRolesCacheRequestBuilder.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/ClearRolesCacheRequestBuilder.java @@ -12,8 +12,10 @@ /** * Request builder for the {@link ClearRolesCacheRequest} */ -public class ClearRolesCacheRequestBuilder extends NodesOperationRequestBuilder { +public class ClearRolesCacheRequestBuilder extends NodesOperationRequestBuilder< + ClearRolesCacheRequest, + ClearRolesCacheResponse, + ClearRolesCacheRequestBuilder> { public ClearRolesCacheRequestBuilder(ElasticsearchClient client) { this(client, ClearRolesCacheAction.INSTANCE, new ClearRolesCacheRequest()); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/ClearRolesCacheResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/ClearRolesCacheResponse.java index 91ec8932f9dac..6b5ae85f641f0 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/ClearRolesCacheResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/ClearRolesCacheResponse.java @@ -47,7 +47,7 @@ protected void writeNodesTo(StreamOutput out, List @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject("nodes"); - for (ClearRolesCacheResponse.Node node: getNodes()) { + for (ClearRolesCacheResponse.Node node : getNodes()) { builder.startObject(node.getNode().getId()); builder.field("name", node.getNode().getName()); builder.endObject(); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/DeleteRoleAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/DeleteRoleAction.java index e929f1bd14864..2b4ad261ccae2 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/DeleteRoleAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/DeleteRoleAction.java @@ -16,7 +16,6 @@ public class DeleteRoleAction extends ActionType { public static final DeleteRoleAction INSTANCE = new DeleteRoleAction(); public static final String NAME = "cluster:admin/xpack/security/role/delete"; - protected DeleteRoleAction() { super(NAME, DeleteRoleResponse::new); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/DeleteRoleRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/DeleteRoleRequest.java index 2bcd433446be1..9d7eff847529f 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/DeleteRoleRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/DeleteRoleRequest.java @@ -30,8 +30,7 @@ public DeleteRoleRequest(StreamInput in) throws IOException { refreshPolicy = RefreshPolicy.readFrom(in); } - public DeleteRoleRequest() { - } + public DeleteRoleRequest() {} @Override public DeleteRoleRequest setRefreshPolicy(RefreshPolicy refreshPolicy) { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/DeleteRoleRequestBuilder.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/DeleteRoleRequestBuilder.java index 2bf9acb1a1140..22795288354e3 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/DeleteRoleRequestBuilder.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/DeleteRoleRequestBuilder.java @@ -14,7 +14,8 @@ * A builder for requests to delete a role from the security index */ public class DeleteRoleRequestBuilder extends ActionRequestBuilder - implements WriteRequestBuilder { + implements + WriteRequestBuilder { public DeleteRoleRequestBuilder(ElasticsearchClient client) { this(client, DeleteRoleAction.INSTANCE); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/GetRolesAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/GetRolesAction.java index ed84cc1f57f3d..543b1485b035f 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/GetRolesAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/GetRolesAction.java @@ -16,7 +16,6 @@ public class GetRolesAction extends ActionType { public static final GetRolesAction INSTANCE = new GetRolesAction(); public static final String NAME = "cluster:admin/xpack/security/role/get"; - protected GetRolesAction() { super(NAME, GetRolesResponse::new); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/GetRolesRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/GetRolesRequest.java index 613041e40be50..f5239f18c256a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/GetRolesRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/GetRolesRequest.java @@ -28,8 +28,7 @@ public GetRolesRequest(StreamInput in) throws IOException { names = in.readStringArray(); } - public GetRolesRequest() { - } + public GetRolesRequest() {} @Override public ActionRequestValidationException validate() { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/PutRoleAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/PutRoleAction.java index 1ad78abe7cb9a..548e7e043b1bc 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/PutRoleAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/PutRoleAction.java @@ -16,7 +16,6 @@ public class PutRoleAction extends ActionType { public static final PutRoleAction INSTANCE = new PutRoleAction(); public static final String NAME = "cluster:admin/xpack/security/role/put"; - protected PutRoleAction() { super(NAME, PutRoleResponse::new); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/PutRoleRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/PutRoleRequest.java index 4dab23e5a1c10..26881286c9723 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/PutRoleRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/PutRoleRequest.java @@ -9,11 +9,11 @@ import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.support.WriteRequest; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xpack.core.security.authz.RoleDescriptor; import org.elasticsearch.xpack.core.security.authz.privilege.ApplicationPrivilege; import org.elasticsearch.xpack.core.security.authz.privilege.ClusterPrivilegeResolver; @@ -62,8 +62,7 @@ public PutRoleRequest(StreamInput in) throws IOException { metadata = in.readMap(); } - public PutRoleRequest() { - } + public PutRoleRequest() {} @Override public ActionRequestValidationException validate() { @@ -89,7 +88,7 @@ public ActionRequestValidationException validate() { } } } - if(applicationPrivileges != null) { + if (applicationPrivileges != null) { for (RoleDescriptor.ApplicationResourcePrivileges privilege : applicationPrivileges) { try { ApplicationPrivilege.validateApplicationNameOrWildcard(privilege.getApplication()); @@ -106,8 +105,10 @@ public ActionRequestValidationException validate() { } } if (metadata != null && MetadataUtils.containsReservedMetadata(metadata)) { - validationException = - addValidationError("metadata keys may not start with [" + MetadataUtils.RESERVED_PREFIX + "]", validationException); + validationException = addValidationError( + "metadata keys may not start with [" + MetadataUtils.RESERVED_PREFIX + "]", + validationException + ); } return validationException; } @@ -128,16 +129,24 @@ public void addIndex(RoleDescriptor.IndicesPrivileges... privileges) { this.indicesPrivileges.addAll(Arrays.asList(privileges)); } - public void addIndex(String[] indices, String[] privileges, String[] grantedFields, String[] deniedFields, - @Nullable BytesReference query, boolean allowRestrictedIndices) { - this.indicesPrivileges.add(RoleDescriptor.IndicesPrivileges.builder() + public void addIndex( + String[] indices, + String[] privileges, + String[] grantedFields, + String[] deniedFields, + @Nullable BytesReference query, + boolean allowRestrictedIndices + ) { + this.indicesPrivileges.add( + RoleDescriptor.IndicesPrivileges.builder() .indices(indices) .privileges(privileges) .grantedFields(grantedFields) .deniedFields(deniedFields) .query(query) .allowRestrictedIndices(allowRestrictedIndices) - .build()); + .build() + ); } public void addApplicationPrivileges(RoleDescriptor.ApplicationResourcePrivileges... privileges) { @@ -212,14 +221,16 @@ public void writeTo(StreamOutput out) throws IOException { } public RoleDescriptor roleDescriptor() { - return new RoleDescriptor(name, - clusterPrivileges, - indicesPrivileges.toArray(new RoleDescriptor.IndicesPrivileges[indicesPrivileges.size()]), - applicationPrivileges.toArray(new RoleDescriptor.ApplicationResourcePrivileges[applicationPrivileges.size()]), + return new RoleDescriptor( + name, + clusterPrivileges, + indicesPrivileges.toArray(new RoleDescriptor.IndicesPrivileges[indicesPrivileges.size()]), + applicationPrivileges.toArray(new RoleDescriptor.ApplicationResourcePrivileges[applicationPrivileges.size()]), configurableClusterPrivileges, - runAs, - metadata, - Collections.emptyMap()); + runAs, + metadata, + Collections.emptyMap() + ); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/PutRoleRequestBuilder.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/PutRoleRequestBuilder.java index bfe72f6f72578..0796a257a1697 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/PutRoleRequestBuilder.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/PutRoleRequestBuilder.java @@ -9,8 +9,8 @@ import org.elasticsearch.action.ActionRequestBuilder; import org.elasticsearch.action.support.WriteRequestBuilder; import org.elasticsearch.client.ElasticsearchClient; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.security.authz.RoleDescriptor; @@ -21,7 +21,8 @@ * Builder for requests to add a role to the administrative index */ public class PutRoleRequestBuilder extends ActionRequestBuilder - implements WriteRequestBuilder { + implements + WriteRequestBuilder { public PutRoleRequestBuilder(ElasticsearchClient client) { this(client, PutRoleAction.INSTANCE); @@ -64,8 +65,14 @@ public PutRoleRequestBuilder runAs(String... runAsUsers) { return this; } - public PutRoleRequestBuilder addIndices(String[] indices, String[] privileges, String[] grantedFields, String[] deniedFields, - @Nullable BytesReference query, boolean allowRestrictedIndices) { + public PutRoleRequestBuilder addIndices( + String[] indices, + String[] privileges, + String[] grantedFields, + String[] deniedFields, + @Nullable BytesReference query, + boolean allowRestrictedIndices + ) { request.addIndex(indices, privileges, grantedFields, deniedFields, query, allowRestrictedIndices); return this; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/PutRoleResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/PutRoleResponse.java index 0c033e295dbf8..42b672cca6ad8 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/PutRoleResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/PutRoleResponse.java @@ -46,4 +46,4 @@ public void writeTo(StreamOutput out) throws IOException { out.writeBoolean(created); } - } +} diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/rolemapping/DeleteRoleMappingRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/rolemapping/DeleteRoleMappingRequest.java index a157e21e0aa40..5e75f53f12e80 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/rolemapping/DeleteRoleMappingRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/rolemapping/DeleteRoleMappingRequest.java @@ -30,8 +30,7 @@ public DeleteRoleMappingRequest(StreamInput in) throws IOException { refreshPolicy = RefreshPolicy.readFrom(in); } - public DeleteRoleMappingRequest() { - } + public DeleteRoleMappingRequest() {} @Override public DeleteRoleMappingRequest setRefreshPolicy(RefreshPolicy refreshPolicy) { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/rolemapping/DeleteRoleMappingRequestBuilder.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/rolemapping/DeleteRoleMappingRequestBuilder.java index 37c5207cec57f..d5b5c7c0a07c5 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/rolemapping/DeleteRoleMappingRequestBuilder.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/rolemapping/DeleteRoleMappingRequestBuilder.java @@ -15,7 +15,8 @@ * org.elasticsearch.xpack.security.authc.support.mapper.NativeRoleMappingStore */ public class DeleteRoleMappingRequestBuilder extends ActionRequestBuilder - implements WriteRequestBuilder { + implements + WriteRequestBuilder { public DeleteRoleMappingRequestBuilder(ElasticsearchClient client) { super(client, DeleteRoleMappingAction.INSTANCE, new DeleteRoleMappingRequest()); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/rolemapping/GetRoleMappingsRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/rolemapping/GetRoleMappingsRequest.java index a85e184c8b561..e1b018fdb7d7a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/rolemapping/GetRoleMappingsRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/rolemapping/GetRoleMappingsRequest.java @@ -31,15 +31,13 @@ public GetRoleMappingsRequest(StreamInput in) throws IOException { names = in.readStringArray(); } - public GetRoleMappingsRequest() { - } + public GetRoleMappingsRequest() {} @Override public ActionRequestValidationException validate() { ActionRequestValidationException validationException = null; if (names == null) { - validationException = addValidationError("role-mapping names are missing", - validationException); + validationException = addValidationError("role-mapping names are missing", validationException); } return validationException; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/rolemapping/PutRoleMappingRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/rolemapping/PutRoleMappingRequest.java index e856e64d08590..08d410f7b033a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/rolemapping/PutRoleMappingRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/rolemapping/PutRoleMappingRequest.java @@ -32,8 +32,7 @@ * * see org.elasticsearch.xpack.security.authc.support.mapper.NativeRoleMappingStore */ -public class PutRoleMappingRequest extends ActionRequest - implements WriteRequest { +public class PutRoleMappingRequest extends ActionRequest implements WriteRequest { private String name = null; private boolean enabled = true; @@ -56,8 +55,7 @@ public PutRoleMappingRequest(StreamInput in) throws IOException { this.refreshPolicy = RefreshPolicy.readFrom(in); } - public PutRoleMappingRequest() { - } + public PutRoleMappingRequest() {} @Override public ActionRequestValidationException validate() { @@ -75,8 +73,10 @@ public ActionRequestValidationException validate() { validationException = addValidationError("role-mapping rules are missing", validationException); } if (MetadataUtils.containsReservedMetadata(metadata)) { - validationException = addValidationError("metadata keys may not start with [" + MetadataUtils.RESERVED_PREFIX + "]", - validationException); + validationException = addValidationError( + "metadata keys may not start with [" + MetadataUtils.RESERVED_PREFIX + "]", + validationException + ); } return validationException; } @@ -160,13 +160,6 @@ public void writeTo(StreamOutput out) throws IOException { } public ExpressionRoleMapping getMapping() { - return new ExpressionRoleMapping( - name, - rules, - roles, - roleTemplates, - metadata, - enabled - ); + return new ExpressionRoleMapping(name, rules, roles, roleTemplates, metadata, enabled); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/rolemapping/PutRoleMappingRequestBuilder.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/rolemapping/PutRoleMappingRequestBuilder.java index 3f7ee99181c99..48ec6965e88ed 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/rolemapping/PutRoleMappingRequestBuilder.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/rolemapping/PutRoleMappingRequestBuilder.java @@ -24,7 +24,8 @@ * * see org.elasticsearch.xpack.security.authc.support.mapper.NativeRoleMappingStore */ -public class PutRoleMappingRequestBuilder extends ActionRequestBuilder implements +public class PutRoleMappingRequestBuilder extends ActionRequestBuilder + implements WriteRequestBuilder { public PutRoleMappingRequestBuilder(ElasticsearchClient client) { @@ -34,8 +35,7 @@ public PutRoleMappingRequestBuilder(ElasticsearchClient client) { /** * Populate the put role request from the source and the role's name */ - public PutRoleMappingRequestBuilder source(String name, BytesReference source, - XContentType xContentType) throws IOException { + public PutRoleMappingRequestBuilder source(String name, BytesReference source, XContentType xContentType) throws IOException { ExpressionRoleMapping mapping = ExpressionRoleMapping.parse(name, source, xContentType); request.setName(name); request.setEnabled(mapping.isEnabled()); @@ -55,6 +55,7 @@ public PutRoleMappingRequestBuilder roles(String... roles) { request.setRoles(Arrays.asList(roles)); return this; } + public PutRoleMappingRequestBuilder roleTemplates(TemplateRoleName... templates) { request.setRoleTemplates(Arrays.asList(templates)); return this; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/rolemapping/PutRoleMappingResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/rolemapping/PutRoleMappingResponse.java index b6d220d5e27bf..d04b0bbe1195f 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/rolemapping/PutRoleMappingResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/rolemapping/PutRoleMappingResponse.java @@ -47,4 +47,4 @@ public void writeTo(StreamOutput out) throws IOException { out.writeBoolean(created); } - } +} diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlAuthenticateRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlAuthenticateRequest.java index 0acc73508c8de..1a5041c8d985d 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlAuthenticateRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlAuthenticateRequest.java @@ -8,8 +8,8 @@ import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionRequestValidationException; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.core.Nullable; import java.io.IOException; import java.util.List; @@ -28,8 +28,7 @@ public SamlAuthenticateRequest(StreamInput in) throws IOException { super(in); } - public SamlAuthenticateRequest() { - } + public SamlAuthenticateRequest() {} @Override public ActionRequestValidationException validate() { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlAuthenticateRequestBuilder.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlAuthenticateRequestBuilder.java index eae7621018b4b..666c37a332ddc 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlAuthenticateRequestBuilder.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlAuthenticateRequestBuilder.java @@ -6,16 +6,15 @@ */ package org.elasticsearch.xpack.core.security.action.saml; -import java.util.List; - import org.elasticsearch.action.ActionRequestBuilder; import org.elasticsearch.client.ElasticsearchClient; +import java.util.List; + /** * Request builder used to populate a {@link SamlAuthenticateRequest} */ -public final class SamlAuthenticateRequestBuilder - extends ActionRequestBuilder { +public final class SamlAuthenticateRequestBuilder extends ActionRequestBuilder { public SamlAuthenticateRequestBuilder(ElasticsearchClient client) { super(client, SamlAuthenticateAction.INSTANCE, new SamlAuthenticateRequest()); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlAuthenticateResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlAuthenticateResponse.java index e44854b097b32..a3963d6e7b3c9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlAuthenticateResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlAuthenticateResponse.java @@ -71,7 +71,9 @@ public TimeValue getExpiresIn() { return expiresIn; } - public Authentication getAuthentication() { return authentication; } + public Authentication getAuthentication() { + return authentication; + } @Override public void writeTo(StreamOutput out) throws IOException { @@ -87,4 +89,4 @@ public void writeTo(StreamOutput out) throws IOException { } } - } +} diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlCompleteLogoutRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlCompleteLogoutRequest.java index b19b13a9716a2..b4e0cd5ab90ea 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlCompleteLogoutRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlCompleteLogoutRequest.java @@ -8,9 +8,9 @@ import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionRequestValidationException; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.core.Nullable; import java.io.IOException; import java.util.List; @@ -33,8 +33,7 @@ public SamlCompleteLogoutRequest(StreamInput in) throws IOException { super(in); } - public SamlCompleteLogoutRequest() { - } + public SamlCompleteLogoutRequest() {} @Override public ActionRequestValidationException validate() { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlInvalidateSessionRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlInvalidateSessionRequest.java index 2531589342738..a9314ddcf012e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlInvalidateSessionRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlInvalidateSessionRequest.java @@ -8,9 +8,9 @@ import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionRequestValidationException; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.core.Nullable; import java.io.IOException; @@ -33,8 +33,7 @@ public SamlInvalidateSessionRequest(StreamInput in) throws IOException { super(in); } - public SamlInvalidateSessionRequest() { - } + public SamlInvalidateSessionRequest() {} @Override public ActionRequestValidationException validate() { @@ -75,11 +74,18 @@ public void setAssertionConsumerServiceURL(String assertionConsumerServiceURL) { @Override public String toString() { - return getClass().getSimpleName() + "{" + - "realmName='" + realmName + '\'' + - ", assertionConsumerServiceURL='" + assertionConsumerServiceURL + '\'' + - ", url-query=" + queryString.length() + " chars" + - '}'; + return getClass().getSimpleName() + + "{" + + "realmName='" + + realmName + + '\'' + + ", assertionConsumerServiceURL='" + + assertionConsumerServiceURL + + '\'' + + ", url-query=" + + queryString.length() + + " chars" + + '}'; } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlInvalidateSessionRequestBuilder.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlInvalidateSessionRequestBuilder.java index 792db7ab6aad0..ff0c8d29461cc 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlInvalidateSessionRequestBuilder.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlInvalidateSessionRequestBuilder.java @@ -12,8 +12,9 @@ /** * Request builder used to populate a {@link SamlInvalidateSessionRequest} */ -public final class SamlInvalidateSessionRequestBuilder - extends ActionRequestBuilder { +public final class SamlInvalidateSessionRequestBuilder extends ActionRequestBuilder< + SamlInvalidateSessionRequest, + SamlInvalidateSessionResponse> { public SamlInvalidateSessionRequestBuilder(ElasticsearchClient client) { super(client, SamlInvalidateSessionAction.INSTANCE, new SamlInvalidateSessionRequest()); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlLogoutRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlLogoutRequest.java index 010f8158deb31..fa6dad6215b1e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlLogoutRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlLogoutRequest.java @@ -8,9 +8,9 @@ import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionRequestValidationException; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.core.Nullable; import java.io.IOException; @@ -29,8 +29,7 @@ public SamlLogoutRequest(StreamInput in) throws IOException { super(in); } - public SamlLogoutRequest() { - } + public SamlLogoutRequest() {} @Override public ActionRequestValidationException validate() { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlPrepareAuthenticationRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlPrepareAuthenticationRequest.java index a5f1cda36a783..6981fc582728c 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlPrepareAuthenticationRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlPrepareAuthenticationRequest.java @@ -9,9 +9,9 @@ import org.elasticsearch.Version; import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionRequestValidationException; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; import java.io.IOException; @@ -38,8 +38,7 @@ public SamlPrepareAuthenticationRequest(StreamInput in) throws IOException { } } - public SamlPrepareAuthenticationRequest() { - } + public SamlPrepareAuthenticationRequest() {} @Override public ActionRequestValidationException validate() { @@ -72,11 +71,15 @@ public void setRelayState(String relayState) { @Override public String toString() { - return getClass().getSimpleName() + "{" + - "realmName=" + realmName + - ", assertionConsumerServiceURL=" + assertionConsumerServiceURL + - ", relayState=" + relayState + - '}'; + return getClass().getSimpleName() + + "{" + + "realmName=" + + realmName + + ", assertionConsumerServiceURL=" + + assertionConsumerServiceURL + + ", relayState=" + + relayState + + '}'; } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlPrepareAuthenticationRequestBuilder.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlPrepareAuthenticationRequestBuilder.java index 01c3c4fcf1355..86f112cda81f3 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlPrepareAuthenticationRequestBuilder.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlPrepareAuthenticationRequestBuilder.java @@ -12,7 +12,8 @@ /** * Request builder used to populate a {@link SamlPrepareAuthenticationRequest} */ -public final class SamlPrepareAuthenticationRequestBuilder extends ActionRequestBuilder { public SamlPrepareAuthenticationRequestBuilder(ElasticsearchClient client) { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlSpMetadataRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlSpMetadataRequest.java index 52567c307905c..99b86aa22e3ff 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlSpMetadataRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlSpMetadataRequest.java @@ -49,9 +49,7 @@ public void setRealmName(String realmName) { @Override public String toString() { - return getClass().getSimpleName() + "{" + - "realmName=" + realmName + - '}'; + return getClass().getSimpleName() + "{" + "realmName=" + realmName + '}'; } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/service/CreateServiceAccountTokenRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/service/CreateServiceAccountTokenRequest.java index 7f6405dde10d2..5ec9f2333f8e9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/service/CreateServiceAccountTokenRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/service/CreateServiceAccountTokenRequest.java @@ -63,13 +63,13 @@ public void setRefreshPolicy(WriteRequest.RefreshPolicy refreshPolicy) { @Override public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; CreateServiceAccountTokenRequest that = (CreateServiceAccountTokenRequest) o; - return Objects.equals(namespace, that.namespace) && Objects.equals(serviceName, that.serviceName) - && Objects.equals(tokenName, that.tokenName) && refreshPolicy == that.refreshPolicy; + return Objects.equals(namespace, that.namespace) + && Objects.equals(serviceName, that.serviceName) + && Objects.equals(tokenName, that.tokenName) + && refreshPolicy == that.refreshPolicy; } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/service/CreateServiceAccountTokenResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/service/CreateServiceAccountTokenResponse.java index ea7edb4e4e2ae..897e2ba8aa2ec 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/service/CreateServiceAccountTokenResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/service/CreateServiceAccountTokenResponse.java @@ -62,10 +62,8 @@ public void writeTo(StreamOutput out) throws IOException { @Override public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; CreateServiceAccountTokenResponse that = (CreateServiceAccountTokenResponse) o; return Objects.equals(name, that.name) && Objects.equals(value, that.value); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/service/DeleteServiceAccountTokenRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/service/DeleteServiceAccountTokenRequest.java index ce3a9d5128f46..281541be396b2 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/service/DeleteServiceAccountTokenRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/service/DeleteServiceAccountTokenRequest.java @@ -63,13 +63,13 @@ public void setRefreshPolicy(WriteRequest.RefreshPolicy refreshPolicy) { @Override public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; DeleteServiceAccountTokenRequest that = (DeleteServiceAccountTokenRequest) o; - return Objects.equals(namespace, that.namespace) && Objects.equals(serviceName, that.serviceName) - && Objects.equals(tokenName, that.tokenName) && refreshPolicy == that.refreshPolicy; + return Objects.equals(namespace, that.namespace) + && Objects.equals(serviceName, that.serviceName) + && Objects.equals(tokenName, that.tokenName) + && refreshPolicy == that.refreshPolicy; } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/service/DeleteServiceAccountTokenResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/service/DeleteServiceAccountTokenResponse.java index 3ad8b078b5eba..5443deac03bd9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/service/DeleteServiceAccountTokenResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/service/DeleteServiceAccountTokenResponse.java @@ -35,10 +35,8 @@ public boolean found() { @Override public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; DeleteServiceAccountTokenResponse that = (DeleteServiceAccountTokenResponse) o; return found == that.found; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/service/GetServiceAccountCredentialsNodesResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/service/GetServiceAccountCredentialsNodesResponse.java index ba731caec1ec7..6e81ab8e19250 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/service/GetServiceAccountCredentialsNodesResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/service/GetServiceAccountCredentialsNodesResponse.java @@ -32,9 +32,11 @@ */ public class GetServiceAccountCredentialsNodesResponse extends BaseNodesResponse { - public GetServiceAccountCredentialsNodesResponse(ClusterName clusterName, - List nodes, - List failures) { + public GetServiceAccountCredentialsNodesResponse( + ClusterName clusterName, + List nodes, + List failures + ) { super(clusterName, nodes, failures); } @@ -54,7 +56,7 @@ protected void writeNodesTo(StreamOutput out, List getFileTokenInfos() { final Map> fileTokenDistribution = new HashMap<>(); - for (GetServiceAccountCredentialsNodesResponse.Node node: getNodes()) { + for (GetServiceAccountCredentialsNodesResponse.Node node : getNodes()) { if (node.fileTokenNames == null) { continue; } @@ -63,7 +65,8 @@ public List getFileTokenInfos() { distribution.add(node.getNode().getName()); }); } - return fileTokenDistribution.entrySet().stream() + return fileTokenDistribution.entrySet() + .stream() .map(entry -> TokenInfo.fileToken(entry.getKey(), entry.getValue().stream().sorted().collect(Collectors.toUnmodifiableList()))) .collect(Collectors.toUnmodifiableList()); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/service/GetServiceAccountCredentialsRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/service/GetServiceAccountCredentialsRequest.java index e7eb276c73c84..c0cdffdfd5df3 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/service/GetServiceAccountCredentialsRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/service/GetServiceAccountCredentialsRequest.java @@ -44,10 +44,8 @@ public String getServiceName() { @Override public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; GetServiceAccountCredentialsRequest that = (GetServiceAccountCredentialsRequest) o; return Objects.equals(namespace, that.namespace) && Objects.equals(serviceName, that.serviceName); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/service/GetServiceAccountCredentialsResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/service/GetServiceAccountCredentialsResponse.java index 76d7423d46606..d8634f66f2b59 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/service/GetServiceAccountCredentialsResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/service/GetServiceAccountCredentialsResponse.java @@ -10,9 +10,9 @@ import org.elasticsearch.action.ActionResponse; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.rest.action.RestActions; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.rest.action.RestActions; import java.io.IOException; import java.util.Collection; @@ -26,8 +26,11 @@ public class GetServiceAccountCredentialsResponse extends ActionResponse impleme private final List indexTokenInfos; private final GetServiceAccountCredentialsNodesResponse nodesResponse; - public GetServiceAccountCredentialsResponse(String principal, Collection indexTokenInfos, - GetServiceAccountCredentialsNodesResponse nodesResponse) { + public GetServiceAccountCredentialsResponse( + String principal, + Collection indexTokenInfos, + GetServiceAccountCredentialsNodesResponse nodesResponse + ) { this.principal = principal; this.indexTokenInfos = indexTokenInfos == null ? List.of() : indexTokenInfos.stream().sorted().collect(toUnmodifiableList()); this.nodesResponse = nodesResponse; @@ -66,7 +69,8 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.startObject() .field("service_account", principal) .field("count", indexTokenInfos.size() + fileTokenInfos.size()) - .field("tokens").startObject(); + .field("tokens") + .startObject(); for (TokenInfo info : indexTokenInfos) { info.toXContent(builder, params); } @@ -83,8 +87,14 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws @Override public String toString() { - return "GetServiceAccountCredentialsResponse{" + "principal='" - + principal + '\'' + ", indexTokenInfos=" + indexTokenInfos - + ", nodesResponse=" + nodesResponse + '}'; + return "GetServiceAccountCredentialsResponse{" + + "principal='" + + principal + + '\'' + + ", indexTokenInfos=" + + indexTokenInfos + + ", nodesResponse=" + + nodesResponse + + '}'; } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/service/GetServiceAccountRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/service/GetServiceAccountRequest.java index 9424fb75f9943..1d9c60d33cf07 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/service/GetServiceAccountRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/service/GetServiceAccountRequest.java @@ -9,9 +9,9 @@ import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionRequestValidationException; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; import java.io.IOException; import java.util.Objects; @@ -44,10 +44,8 @@ public String getServiceName() { @Override public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; GetServiceAccountRequest that = (GetServiceAccountRequest) o; return Objects.equals(namespace, that.namespace) && Objects.equals(serviceName, that.serviceName); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/service/GetServiceAccountResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/service/GetServiceAccountResponse.java index 8c1eeb9d67aef..0711b631eca81 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/service/GetServiceAccountResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/service/GetServiceAccountResponse.java @@ -56,10 +56,8 @@ public String toString() { @Override public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; GetServiceAccountResponse that = (GetServiceAccountResponse) o; return Arrays.equals(serviceAccountInfos, that.serviceAccountInfos); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/service/ServiceAccountInfo.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/service/ServiceAccountInfo.java index 02d1d7b33019b..c9bb11b4d1a44 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/service/ServiceAccountInfo.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/service/ServiceAccountInfo.java @@ -62,10 +62,8 @@ public String toString() { @Override public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; ServiceAccountInfo that = (ServiceAccountInfo) o; return principal.equals(that.principal) && roleDescriptor.equals(that.roleDescriptor); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/service/TokenInfo.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/service/TokenInfo.java index e166bb1fc8ad9..44d26f0e315e1 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/service/TokenInfo.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/service/TokenInfo.java @@ -10,9 +10,9 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.core.Nullable; import java.io.IOException; import java.util.Collection; @@ -53,10 +53,8 @@ public Collection getNodeNames() { @Override public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; TokenInfo tokenInfo = (TokenInfo) o; return Objects.equals(name, tokenInfo.name) && Objects.equals(nodeNames, tokenInfo.nodeNames); } @@ -106,6 +104,7 @@ public int compareTo(TokenInfo o) { } public enum TokenSource { - INDEX, FILE; + INDEX, + FILE; } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/token/CreateTokenRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/token/CreateTokenRequest.java index 274f98f0219d2..d8a9debb386f6 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/token/CreateTokenRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/token/CreateTokenRequest.java @@ -8,11 +8,11 @@ import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionRequestValidationException; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.settings.SecureString; +import org.elasticsearch.core.Nullable; import java.io.IOException; import java.util.Collections; @@ -60,7 +60,8 @@ public static GrantType fromString(String grantType) { } private static final Set SUPPORTED_GRANT_TYPES = Collections.unmodifiableSet( - EnumSet.of(GrantType.PASSWORD, GrantType.KERBEROS, GrantType.REFRESH_TOKEN, GrantType.CLIENT_CREDENTIALS)); + EnumSet.of(GrantType.PASSWORD, GrantType.KERBEROS, GrantType.REFRESH_TOKEN, GrantType.CLIENT_CREDENTIALS) + ); private String grantType; private String username; @@ -81,8 +82,14 @@ public CreateTokenRequest(StreamInput in) throws IOException { public CreateTokenRequest() {} - public CreateTokenRequest(String grantType, @Nullable String username, @Nullable SecureString password, - @Nullable SecureString kerberosTicket, @Nullable String scope, @Nullable String refreshToken) { + public CreateTokenRequest( + String grantType, + @Nullable String username, + @Nullable SecureString password, + @Nullable SecureString kerberosTicket, + @Nullable String scope, + @Nullable String refreshToken + ) { this.grantType = grantType; this.username = username; this.password = password; @@ -122,40 +129,57 @@ public ActionRequestValidationException validate() { validationException = validateUnsupportedField(type, "refresh_token", refreshToken, validationException); break; default: - validationException = addValidationError("grant_type only supports the values: [" + - SUPPORTED_GRANT_TYPES.stream().map(GrantType::getValue).collect(Collectors.joining(", ")) + "]", - validationException); + validationException = addValidationError( + "grant_type only supports the values: [" + + SUPPORTED_GRANT_TYPES.stream().map(GrantType::getValue).collect(Collectors.joining(", ")) + + "]", + validationException + ); } } else { - validationException = addValidationError("grant_type only supports the values: [" + - SUPPORTED_GRANT_TYPES.stream().map(GrantType::getValue).collect(Collectors.joining(", ")) + "]", - validationException); + validationException = addValidationError( + "grant_type only supports the values: [" + + SUPPORTED_GRANT_TYPES.stream().map(GrantType::getValue).collect(Collectors.joining(", ")) + + "]", + validationException + ); } return validationException; } - private static ActionRequestValidationException validateRequiredField(String field, String fieldValue, - ActionRequestValidationException validationException) { + private static ActionRequestValidationException validateRequiredField( + String field, + String fieldValue, + ActionRequestValidationException validationException + ) { if (Strings.isNullOrEmpty(fieldValue)) { validationException = addValidationError(String.format(Locale.ROOT, "%s is missing", field), validationException); } return validationException; } - private static ActionRequestValidationException validateRequiredField(String field, SecureString fieldValue, - ActionRequestValidationException validationException) { + private static ActionRequestValidationException validateRequiredField( + String field, + SecureString fieldValue, + ActionRequestValidationException validationException + ) { if (fieldValue == null || fieldValue.getChars() == null || fieldValue.length() == 0) { validationException = addValidationError(String.format(Locale.ROOT, "%s is missing", field), validationException); } return validationException; } - private static ActionRequestValidationException validateUnsupportedField(GrantType grantType, String field, Object fieldValue, - ActionRequestValidationException validationException) { + private static ActionRequestValidationException validateUnsupportedField( + GrantType grantType, + String field, + Object fieldValue, + ActionRequestValidationException validationException + ) { if (fieldValue != null) { validationException = addValidationError( - String.format(Locale.ROOT, "%s is not supported with the %s grant_type", field, grantType.getValue()), - validationException); + String.format(Locale.ROOT, "%s is not supported with the %s grant_type", field, grantType.getValue()), + validationException + ); } return validationException; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/token/CreateTokenRequestBuilder.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/token/CreateTokenRequestBuilder.java index 647a96040cce6..0b9e7e09c9c24 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/token/CreateTokenRequestBuilder.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/token/CreateTokenRequestBuilder.java @@ -6,17 +6,16 @@ */ package org.elasticsearch.xpack.core.security.action.token; -import org.elasticsearch.action.ActionType; import org.elasticsearch.action.ActionRequestBuilder; +import org.elasticsearch.action.ActionType; import org.elasticsearch.client.ElasticsearchClient; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.settings.SecureString; +import org.elasticsearch.core.Nullable; /** * Request builder used to populate a {@link CreateTokenRequest} */ -public final class CreateTokenRequestBuilder - extends ActionRequestBuilder { +public final class CreateTokenRequestBuilder extends ActionRequestBuilder { public CreateTokenRequestBuilder(ElasticsearchClient client, ActionType action) { super(client, action, new CreateTokenRequest()); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/token/CreateTokenResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/token/CreateTokenResponse.java index 15179f8a8aaff..c7c7b8ab1dcc5 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/token/CreateTokenResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/token/CreateTokenResponse.java @@ -46,8 +46,14 @@ public CreateTokenResponse(StreamInput in) throws IOException { } } - public CreateTokenResponse(String tokenString, TimeValue expiresIn, String scope, String refreshToken, - String kerberosAuthenticationResponseToken, Authentication authentication) { + public CreateTokenResponse( + String tokenString, + TimeValue expiresIn, + String scope, + String refreshToken, + String kerberosAuthenticationResponseToken, + Authentication authentication + ) { this.tokenString = Objects.requireNonNull(tokenString); this.expiresIn = Objects.requireNonNull(expiresIn); this.scope = scope; @@ -76,7 +82,9 @@ public String getKerberosAuthenticationResponseToken() { return kerberosAuthenticationResponseToken; } - public Authentication getAuthentication() { return authentication; } + public Authentication getAuthentication() { + return authentication; + } @Override public void writeTo(StreamOutput out) throws IOException { @@ -92,10 +100,7 @@ public void writeTo(StreamOutput out) throws IOException { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.startObject() - .field("access_token", tokenString) - .field("type", "Bearer") - .field("expires_in", expiresIn.seconds()); + builder.startObject().field("access_token", tokenString).field("type", "Bearer").field("expires_in", expiresIn.seconds()); if (refreshToken != null) { builder.field("refresh_token", refreshToken); } @@ -117,17 +122,16 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; CreateTokenResponse that = (CreateTokenResponse) o; - return Objects.equals(tokenString, that.tokenString) && - Objects.equals(expiresIn, that.expiresIn) && - Objects.equals(scope, that.scope) && - Objects.equals(refreshToken, that.refreshToken) && - Objects.equals(kerberosAuthenticationResponseToken, that.kerberosAuthenticationResponseToken) && - Objects.equals(authentication, that.authentication); + return Objects.equals(tokenString, that.tokenString) + && Objects.equals(expiresIn, that.expiresIn) + && Objects.equals(scope, that.scope) + && Objects.equals(refreshToken, that.refreshToken) + && Objects.equals(kerberosAuthenticationResponseToken, that.kerberosAuthenticationResponseToken) + && Objects.equals(authentication, that.authentication); } @Override public int hashCode() { - return Objects.hash(tokenString, expiresIn, scope, refreshToken, kerberosAuthenticationResponseToken, - authentication); + return Objects.hash(tokenString, expiresIn, scope, refreshToken, kerberosAuthenticationResponseToken, authentication); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/token/InvalidateTokenRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/token/InvalidateTokenRequest.java index 62b61583ef6a5..6c4b157dcf1f9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/token/InvalidateTokenRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/token/InvalidateTokenRequest.java @@ -8,10 +8,10 @@ import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionRequestValidationException; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; import java.io.IOException; @@ -70,8 +70,12 @@ public InvalidateTokenRequest() {} * @param realmName the name of the realm for which all tokens will be invalidated * @param userName the principal of the user for which all tokens will be invalidated */ - public InvalidateTokenRequest(@Nullable String tokenString, @Nullable String tokenType, - @Nullable String realmName, @Nullable String userName) { + public InvalidateTokenRequest( + @Nullable String tokenString, + @Nullable String tokenType, + @Nullable String realmName, + @Nullable String userName + ) { this.tokenString = tokenString; this.tokenType = Type.fromString(tokenType); this.realmName = realmName; @@ -94,19 +98,21 @@ public ActionRequestValidationException validate() { ActionRequestValidationException validationException = null; if (Strings.hasText(realmName) || Strings.hasText(userName)) { if (Strings.hasText(tokenString)) { - validationException = - addValidationError("token string must not be provided when realm name or username is specified", null); + validationException = addValidationError( + "token string must not be provided when realm name or username is specified", + null + ); } if (tokenType != null) { - validationException = - addValidationError("token type must not be provided when realm name or username is specified", validationException); + validationException = addValidationError( + "token type must not be provided when realm name or username is specified", + validationException + ); } } else if (Strings.isNullOrEmpty(tokenString)) { - validationException = - addValidationError("token string must be provided when not specifying a realm name or a username", null); + validationException = addValidationError("token string must be provided when not specifying a realm name or a username", null); } else if (tokenType == null) { - validationException = - addValidationError("token type must be provided when a token string is specified", null); + validationException = addValidationError("token type must be provided when a token string is specified", null); } return validationException; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/token/InvalidateTokenRequestBuilder.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/token/InvalidateTokenRequestBuilder.java index 14f398111a667..9f2e5a901ec77 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/token/InvalidateTokenRequestBuilder.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/token/InvalidateTokenRequestBuilder.java @@ -12,8 +12,7 @@ /** * Request builder that is used to populate a {@link InvalidateTokenRequest} */ -public final class InvalidateTokenRequestBuilder - extends ActionRequestBuilder { +public final class InvalidateTokenRequestBuilder extends ActionRequestBuilder { public InvalidateTokenRequestBuilder(ElasticsearchClient client) { super(client, InvalidateTokenAction.INSTANCE, new InvalidateTokenRequest()); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/AuthenticateRequestBuilder.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/AuthenticateRequestBuilder.java index 486ad249ee4cf..1c6e8022ffada 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/AuthenticateRequestBuilder.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/AuthenticateRequestBuilder.java @@ -9,8 +9,7 @@ import org.elasticsearch.action.ActionRequestBuilder; import org.elasticsearch.client.ElasticsearchClient; -public class AuthenticateRequestBuilder - extends ActionRequestBuilder { +public class AuthenticateRequestBuilder extends ActionRequestBuilder { public AuthenticateRequestBuilder(ElasticsearchClient client) { this(client, AuthenticateAction.INSTANCE); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/AuthenticateResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/AuthenticateResponse.java index 8267b7992612a..e9b4a27ea7415 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/AuthenticateResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/AuthenticateResponse.java @@ -22,7 +22,7 @@ public AuthenticateResponse(StreamInput in) throws IOException { authentication = new Authentication(in); } - public AuthenticateResponse(Authentication authentication){ + public AuthenticateResponse(Authentication authentication) { this.authentication = authentication; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/ChangePasswordRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/ChangePasswordRequest.java index e320bb5d8abcc..10c40bb38d2c3 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/ChangePasswordRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/ChangePasswordRequest.java @@ -22,8 +22,7 @@ /** * Request to change a user's password. */ -public class ChangePasswordRequest extends ActionRequest - implements UserRequest, WriteRequest { +public class ChangePasswordRequest extends ActionRequest implements UserRequest, WriteRequest { private String username; private char[] passwordHash; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/ChangePasswordRequestBuilder.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/ChangePasswordRequestBuilder.java index 7b2e8ad5f7142..2087cf48b8ead 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/ChangePasswordRequestBuilder.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/ChangePasswordRequestBuilder.java @@ -30,9 +30,9 @@ /** * Request to change a user's password. */ -public class ChangePasswordRequestBuilder - extends ActionRequestBuilder - implements WriteRequestBuilder { +public class ChangePasswordRequestBuilder extends ActionRequestBuilder + implements + WriteRequestBuilder { public ChangePasswordRequestBuilder(ElasticsearchClient client) { super(client, ChangePasswordAction.INSTANCE, new ChangePasswordRequest()); @@ -71,8 +71,9 @@ public ChangePasswordRequestBuilder password(char[] password, Hasher hasher) { public ChangePasswordRequestBuilder passwordHash(char[] passwordHashChars, Hasher configuredHasher) { final Hasher resolvedHasher = Hasher.resolveFromHash(passwordHashChars); if (resolvedHasher.equals(configuredHasher) == false) { - throw new IllegalArgumentException("Provided password hash uses [" + resolvedHasher - + "] but the configured hashing algorithm is [" + configuredHasher + "]"); + throw new IllegalArgumentException( + "Provided password hash uses [" + resolvedHasher + "] but the configured hashing algorithm is [" + configuredHasher + "]" + ); } if (request.passwordHash() != null) { throw validationException("password_hash has already been set"); @@ -84,12 +85,13 @@ public ChangePasswordRequestBuilder passwordHash(char[] passwordHashChars, Hashe /** * Populate the change password request from the source in the provided content type */ - public ChangePasswordRequestBuilder source(BytesReference source, XContentType xContentType, Hasher hasher) throws - IOException { + public ChangePasswordRequestBuilder source(BytesReference source, XContentType xContentType, Hasher hasher) throws IOException { // EMPTY is ok here because we never call namedObject - try (InputStream stream = source.streamInput(); - XContentParser parser = xContentType.xContent() - .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) { + try ( + InputStream stream = source.streamInput(); + XContentParser parser = xContentType.xContent() + .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream) + ) { XContentUtils.verifyObject(parser); XContentParser.Token token; String currentFieldName = null; @@ -101,23 +103,31 @@ public ChangePasswordRequestBuilder source(BytesReference source, XContentType x String password = parser.text(); final char[] passwordChars = password.toCharArray(); password(passwordChars, hasher); - assert CharBuffer.wrap(passwordChars).chars().noneMatch((i) -> (char) i != (char) 0) : "expected password to " + - "clear the char[] but it did not!"; + assert CharBuffer.wrap(passwordChars).chars().noneMatch((i) -> (char) i != (char) 0) + : "expected password to " + "clear the char[] but it did not!"; } else { throw new ElasticsearchParseException( - "expected field [{}] to be of type string, but found [{}] instead", currentFieldName, token); + "expected field [{}] to be of type string, but found [{}] instead", + currentFieldName, + token + ); } } else if (User.Fields.PASSWORD_HASH.match(currentFieldName, parser.getDeprecationHandler())) { - if (token == XContentParser.Token.VALUE_STRING) { - char[] passwordHashChars = parser.text().toCharArray(); - passwordHash(passwordHashChars, hasher); - } else { - throw new ElasticsearchParseException( - "expected field [{}] to be of type string, but found [{}] instead", currentFieldName, token); - } + if (token == XContentParser.Token.VALUE_STRING) { + char[] passwordHashChars = parser.text().toCharArray(); + passwordHash(passwordHashChars, hasher); + } else { + throw new ElasticsearchParseException( + "expected field [{}] to be of type string, but found [{}] instead", + currentFieldName, + token + ); + } } else { - throw new ElasticsearchParseException("failed to parse change password request. unexpected field [{}]", - currentFieldName); + throw new ElasticsearchParseException( + "failed to parse change password request. unexpected field [{}]", + currentFieldName + ); } } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/DeleteUserRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/DeleteUserRequest.java index 285ca8ae0eed6..7144489ba6ecb 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/DeleteUserRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/DeleteUserRequest.java @@ -30,8 +30,7 @@ public DeleteUserRequest(StreamInput in) throws IOException { refreshPolicy = RefreshPolicy.readFrom(in); } - public DeleteUserRequest() { - } + public DeleteUserRequest() {} public DeleteUserRequest(String username) { this.username = username; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/DeleteUserRequestBuilder.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/DeleteUserRequestBuilder.java index e27f796f3976f..d92dccebc40cb 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/DeleteUserRequestBuilder.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/DeleteUserRequestBuilder.java @@ -11,7 +11,8 @@ import org.elasticsearch.client.ElasticsearchClient; public class DeleteUserRequestBuilder extends ActionRequestBuilder - implements WriteRequestBuilder { + implements + WriteRequestBuilder { public DeleteUserRequestBuilder(ElasticsearchClient client) { this(client, DeleteUserAction.INSTANCE); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/GetUserPrivilegesRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/GetUserPrivilegesRequest.java index a65581746a31a..308fc7e5f3eb1 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/GetUserPrivilegesRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/GetUserPrivilegesRequest.java @@ -23,8 +23,7 @@ public final class GetUserPrivilegesRequest extends ActionRequest implements Use /** * Package level access for {@link GetUserPrivilegesRequestBuilder}. */ - GetUserPrivilegesRequest() { - } + GetUserPrivilegesRequest() {} public GetUserPrivilegesRequest(StreamInput in) throws IOException { super(in); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/GetUserPrivilegesRequestBuilder.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/GetUserPrivilegesRequestBuilder.java index 8adb7c5ad9767..a34b4f87091a3 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/GetUserPrivilegesRequestBuilder.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/GetUserPrivilegesRequestBuilder.java @@ -12,8 +12,7 @@ /** * Request builder for checking a user's privileges */ -public class GetUserPrivilegesRequestBuilder - extends ActionRequestBuilder { +public class GetUserPrivilegesRequestBuilder extends ActionRequestBuilder { public GetUserPrivilegesRequestBuilder(ElasticsearchClient client) { super(client, GetUserPrivilegesAction.INSTANCE, new GetUserPrivilegesRequest()); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/GetUserPrivilegesResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/GetUserPrivilegesResponse.java index fd6d2669e9faf..074e2c0cd544f 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/GetUserPrivilegesResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/GetUserPrivilegesResponse.java @@ -48,10 +48,13 @@ public GetUserPrivilegesResponse(StreamInput in) throws IOException { runAs = Collections.unmodifiableSet(in.readSet(StreamInput::readString)); } - public GetUserPrivilegesResponse(Set cluster, Set conditionalCluster, - Set index, - Set application, - Set runAs) { + public GetUserPrivilegesResponse( + Set cluster, + Set conditionalCluster, + Set index, + Set application, + Set runAs + ) { this.cluster = Collections.unmodifiableSet(cluster); this.configurableClusterPrivileges = Collections.unmodifiableSet(conditionalCluster); this.index = Collections.unmodifiableSet(index); @@ -97,11 +100,11 @@ public boolean equals(Object other) { return false; } final GetUserPrivilegesResponse that = (GetUserPrivilegesResponse) other; - return Objects.equals(cluster, that.cluster) && - Objects.equals(configurableClusterPrivileges, that.configurableClusterPrivileges) && - Objects.equals(index, that.index) && - Objects.equals(application, that.application) && - Objects.equals(runAs, that.runAs); + return Objects.equals(cluster, that.cluster) + && Objects.equals(configurableClusterPrivileges, that.configurableClusterPrivileges) + && Objects.equals(index, that.index) + && Objects.equals(application, that.application) + && Objects.equals(runAs, that.runAs); } @Override @@ -120,9 +123,13 @@ public static class Indices implements ToXContentObject, Writeable { private final Set queries; private final boolean allowRestrictedIndices; - public Indices(Collection indices, Collection privileges, - Set fieldSecurity, Set queries, - boolean allowRestrictedIndices) { + public Indices( + Collection indices, + Collection privileges, + Set fieldSecurity, + Set queries, + boolean allowRestrictedIndices + ) { // The use of TreeSet is to provide a consistent order that can be relied upon in tests this.indices = Collections.unmodifiableSet(new TreeSet<>(Objects.requireNonNull(indices))); this.privileges = Collections.unmodifiableSet(new TreeSet<>(Objects.requireNonNull(privileges))); @@ -166,11 +173,13 @@ public boolean allowRestrictedIndices() { @Override public String toString() { - StringBuilder sb = new StringBuilder(getClass().getSimpleName()) - .append("[") - .append("indices=[").append(Strings.collectionToCommaDelimitedString(indices)) - .append("], allow_restricted_indices=[").append(allowRestrictedIndices) - .append("], privileges=[").append(Strings.collectionToCommaDelimitedString(privileges)) + StringBuilder sb = new StringBuilder(getClass().getSimpleName()).append("[") + .append("indices=[") + .append(Strings.collectionToCommaDelimitedString(indices)) + .append("], allow_restricted_indices=[") + .append(allowRestrictedIndices) + .append("], privileges=[") + .append(Strings.collectionToCommaDelimitedString(privileges)) .append("]"); if (fieldSecurity.isEmpty() == false) { sb.append(", fls=[").append(Strings.collectionToCommaDelimitedString(fieldSecurity)).append("]"); @@ -210,8 +219,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.field(RoleDescriptor.Fields.PRIVILEGES.getPreferredName(), privileges); if (fieldSecurity.stream().anyMatch(g -> nonEmpty(g.getGrantedFields()) || nonEmpty(g.getExcludedFields()))) { builder.startArray(RoleDescriptor.Fields.FIELD_PERMISSIONS.getPreferredName()); - final List sortedFieldSecurity = - this.fieldSecurity.stream().sorted().collect(Collectors.toUnmodifiableList()); + final List sortedFieldSecurity = this.fieldSecurity.stream() + .sorted() + .collect(Collectors.toUnmodifiableList()); for (FieldPermissionsDefinition.FieldGrantExcludeGroup group : sortedFieldSecurity) { builder.startObject(); if (nonEmpty(group.getGrantedFields())) { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/HasPrivilegesRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/HasPrivilegesRequest.java index 0c08cc8aa8d14..ec6c0a8118504 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/HasPrivilegesRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/HasPrivilegesRequest.java @@ -62,9 +62,12 @@ public ActionRequestValidationException validate() { } } } - if (clusterPrivileges != null && clusterPrivileges.length == 0 - && indexPrivileges != null && indexPrivileges.length == 0 - && applicationPrivileges != null && applicationPrivileges.length == 0) { + if (clusterPrivileges != null + && clusterPrivileges.length == 0 + && indexPrivileges != null + && indexPrivileges.length == 0 + && applicationPrivileges != null + && applicationPrivileges.length == 0) { validationException = addValidationError("must specify at least one privilege", validationException); } return validationException; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/HasPrivilegesRequestBuilder.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/HasPrivilegesRequestBuilder.java index 11aeb5eb1a2cc..21979d042f750 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/HasPrivilegesRequestBuilder.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/HasPrivilegesRequestBuilder.java @@ -17,8 +17,7 @@ /** * Request builder for checking a user's privileges */ -public class HasPrivilegesRequestBuilder - extends ActionRequestBuilder { +public class HasPrivilegesRequestBuilder extends ActionRequestBuilder { public HasPrivilegesRequestBuilder(ElasticsearchClient client) { super(client, HasPrivilegesAction.INSTANCE, new HasPrivilegesRequest()); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/HasPrivilegesResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/HasPrivilegesResponse.java index 0b8380c55c5f0..215cb96709875 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/HasPrivilegesResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/HasPrivilegesResponse.java @@ -46,8 +46,13 @@ public HasPrivilegesResponse(StreamInput in) throws IOException { username = in.readString(); } - public HasPrivilegesResponse(String username, boolean completeMatch, Map cluster, Collection index, - Map> application) { + public HasPrivilegesResponse( + String username, + boolean completeMatch, + Map cluster, + Collection index, + Map> application + ) { super(); this.username = username; this.completeMatch = completeMatch; @@ -139,20 +144,28 @@ private static void writeResourcePrivileges(StreamOutput out, Set privileges) - throws IOException { + private void appendResources(XContentBuilder builder, String field, Set privileges) throws IOException { builder.startObject(field); for (ResourcePrivileges privilege : privileges) { builder.field(privilege.getResource()); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/PutUserRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/PutUserRequest.java index eb6015c5a3f71..bb7a549849f03 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/PutUserRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/PutUserRequest.java @@ -10,12 +10,12 @@ import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.support.WriteRequest; -import org.elasticsearch.core.CharArrays; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.CharArrays; +import org.elasticsearch.core.Nullable; import java.io.IOException; import java.util.Arrays; @@ -49,8 +49,7 @@ public PutUserRequest(StreamInput in) throws IOException { enabled = in.readBoolean(); } - public PutUserRequest() { - } + public PutUserRequest() {} @Override public ActionRequestValidationException validate() { @@ -184,15 +183,26 @@ private static void writeCharArrayToStream(StreamOutput out, char[] chars) throw @Override public String toString() { - return "PutUserRequest{" + - "username='" + username + '\'' + - ", roles=" + Arrays.toString(roles) + - ", fullName='" + fullName + '\'' + - ", email='" + email + '\'' + - ", metadata=" + metadata + - ", passwordHash=" + (passwordHash == null ? "" : "") + - ", enabled=" + enabled + - ", refreshPolicy=" + refreshPolicy + - '}'; + return "PutUserRequest{" + + "username='" + + username + + '\'' + + ", roles=" + + Arrays.toString(roles) + + ", fullName='" + + fullName + + '\'' + + ", email='" + + email + + '\'' + + ", metadata=" + + metadata + + ", passwordHash=" + + (passwordHash == null ? "" : "") + + ", enabled=" + + enabled + + ", refreshPolicy=" + + refreshPolicy + + '}'; } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/PutUserRequestBuilder.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/PutUserRequestBuilder.java index df205e8adfe30..db3c6579095f7 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/PutUserRequestBuilder.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/PutUserRequestBuilder.java @@ -30,7 +30,8 @@ import java.util.Objects; public class PutUserRequestBuilder extends ActionRequestBuilder - implements WriteRequestBuilder { + implements + WriteRequestBuilder { public PutUserRequestBuilder(ElasticsearchClient client) { this(client, PutUserAction.INSTANCE); @@ -92,8 +93,9 @@ public PutUserRequestBuilder email(String email) { public PutUserRequestBuilder passwordHash(char[] passwordHash, Hasher configuredHasher) { final Hasher resolvedHasher = Hasher.resolveFromHash(passwordHash); if (resolvedHasher.equals(configuredHasher) == false) { - throw new IllegalArgumentException("Provided password hash uses [" + resolvedHasher - + "] but the configured hashing algorithm is [" + configuredHasher + "]"); + throw new IllegalArgumentException( + "Provided password hash uses [" + resolvedHasher + "] but the configured hashing algorithm is [" + configuredHasher + "]" + ); } if (request.passwordHash() != null) { throw validationException("password_hash has already been set"); @@ -110,14 +112,16 @@ public PutUserRequestBuilder enabled(boolean enabled) { /** * Populate the put user request using the given source and username */ - public PutUserRequestBuilder source(String username, BytesReference source, XContentType xContentType, Hasher hasher) throws - IOException { + public PutUserRequestBuilder source(String username, BytesReference source, XContentType xContentType, Hasher hasher) + throws IOException { Objects.requireNonNull(xContentType); username(username); // EMPTY is ok here because we never call namedObject - try (InputStream stream = source.streamInput(); - XContentParser parser = xContentType.xContent() - .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) { + try ( + InputStream stream = source.streamInput(); + XContentParser parser = xContentType.xContent() + .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream) + ) { XContentUtils.verifyObject(parser); XContentParser.Token token; String currentFieldName = null; @@ -127,12 +131,15 @@ public PutUserRequestBuilder source(String username, BytesReference source, XCon } else if (User.Fields.PASSWORD.match(currentFieldName, parser.getDeprecationHandler())) { if (token == XContentParser.Token.VALUE_STRING) { String password = parser.text(); - try(SecureString securePassword = new SecureString(password.toCharArray())) { + try (SecureString securePassword = new SecureString(password.toCharArray())) { password(securePassword, hasher); } } else { throw new ElasticsearchParseException( - "expected field [{}] to be of type string, but found [{}] instead", currentFieldName, token); + "expected field [{}] to be of type string, but found [{}] instead", + currentFieldName, + token + ); } } else if (User.Fields.PASSWORD_HASH.match(currentFieldName, parser.getDeprecationHandler())) { if (token == XContentParser.Token.VALUE_STRING) { @@ -140,7 +147,10 @@ public PutUserRequestBuilder source(String username, BytesReference source, XCon passwordHash(passwordChars, hasher); } else { throw new ElasticsearchParseException( - "expected field [{}] to be of type string, but found [{}] instead", currentFieldName, token); + "expected field [{}] to be of type string, but found [{}] instead", + currentFieldName, + token + ); } } else if (User.Fields.ROLES.match(currentFieldName, parser.getDeprecationHandler())) { if (token == XContentParser.Token.VALUE_STRING) { @@ -153,38 +163,54 @@ public PutUserRequestBuilder source(String username, BytesReference source, XCon fullName(parser.text()); } else if (token != XContentParser.Token.VALUE_NULL) { throw new ElasticsearchParseException( - "expected field [{}] to be of type string, but found [{}] instead", currentFieldName, token); + "expected field [{}] to be of type string, but found [{}] instead", + currentFieldName, + token + ); } } else if (User.Fields.EMAIL.match(currentFieldName, parser.getDeprecationHandler())) { if (token == XContentParser.Token.VALUE_STRING) { email(parser.text()); } else if (token != XContentParser.Token.VALUE_NULL) { throw new ElasticsearchParseException( - "expected field [{}] to be of type string, but found [{}] instead", currentFieldName, token); + "expected field [{}] to be of type string, but found [{}] instead", + currentFieldName, + token + ); } } else if (User.Fields.METADATA.match(currentFieldName, parser.getDeprecationHandler())) { if (token == XContentParser.Token.START_OBJECT) { metadata(parser.map()); } else { throw new ElasticsearchParseException( - "expected field [{}] to be of type object, but found [{}] instead", currentFieldName, token); + "expected field [{}] to be of type object, but found [{}] instead", + currentFieldName, + token + ); } } else if (User.Fields.ENABLED.match(currentFieldName, parser.getDeprecationHandler())) { if (token == XContentParser.Token.VALUE_BOOLEAN) { enabled(parser.booleanValue()); } else { throw new ElasticsearchParseException( - "expected field [{}] to be of type boolean, but found [{}] instead", currentFieldName, token); + "expected field [{}] to be of type boolean, but found [{}] instead", + currentFieldName, + token + ); } } else if (User.Fields.USERNAME.match(currentFieldName, parser.getDeprecationHandler())) { if (token == Token.VALUE_STRING) { if (username.equals(parser.text()) == false) { - throw new IllegalArgumentException("[username] in source does not match the username provided [" + - username + "]"); + throw new IllegalArgumentException( + "[username] in source does not match the username provided [" + username + "]" + ); } } else { throw new ElasticsearchParseException( - "expected field [{}] to be of type string, but found [{}] instead", currentFieldName, token); + "expected field [{}] to be of type string, but found [{}] instead", + currentFieldName, + token + ); } } else { throw new ElasticsearchParseException("failed to parse add user request. unexpected field [{}]", currentFieldName); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/PutUserResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/PutUserResponse.java index b13ec212cc8bb..86a25f8321176 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/PutUserResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/PutUserResponse.java @@ -43,8 +43,6 @@ public void writeTo(StreamOutput out) throws IOException { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder.startObject() - .field("created", created) - .endObject(); + return builder.startObject().field("created", created).endObject(); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/SetEnabledRequestBuilder.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/SetEnabledRequestBuilder.java index d911539ccae3c..253ba4eb3de93 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/SetEnabledRequestBuilder.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/user/SetEnabledRequestBuilder.java @@ -15,7 +15,8 @@ * Request builder for setting a user as enabled or disabled */ public class SetEnabledRequestBuilder extends ActionRequestBuilder - implements WriteRequestBuilder { + implements + WriteRequestBuilder { public SetEnabledRequestBuilder(ElasticsearchClient client) { super(client, SetEnabledAction.INSTANCE, new SetEnabledRequest()); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/Authentication.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/Authentication.java index 22da39cc71475..04a22501baebe 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/Authentication.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/Authentication.java @@ -52,8 +52,14 @@ public Authentication(User user, RealmRef authenticatedBy, RealmRef lookedUpBy, this(user, authenticatedBy, lookedUpBy, version, AuthenticationType.REALM, Collections.emptyMap()); } - public Authentication(User user, RealmRef authenticatedBy, RealmRef lookedUpBy, Version version, - AuthenticationType type, Map metadata) { + public Authentication( + User user, + RealmRef authenticatedBy, + RealmRef lookedUpBy, + Version version, + AuthenticationType type, + Map metadata + ) { this.user = Objects.requireNonNull(user); this.authenticatedBy = Objects.requireNonNull(authenticatedBy); this.lookedUpBy = lookedUpBy; @@ -159,8 +165,8 @@ public boolean canAccessResourcesOf(Authentication other) { if (AuthenticationType.API_KEY == getAuthenticationType() && AuthenticationType.API_KEY == other.getAuthenticationType()) { final boolean sameKeyId = getMetadata().get(API_KEY_ID_KEY).equals(other.getMetadata().get(API_KEY_ID_KEY)); if (sameKeyId) { - assert getUser().principal().equals(other.getUser().principal()) : - "The same API key ID cannot be attributed to two different usernames"; + assert getUser().principal().equals(other.getUser().principal()) + : "The same API key ID cannot be attributed to two different usernames"; } return sameKeyId; } @@ -186,7 +192,7 @@ assert getUser().principal().equals(other.getUser().principal()) : AuthenticationType.INTERNAL ).containsAll(EnumSet.of(getAuthenticationType(), other.getAuthenticationType())) : "cross AuthenticationType comparison for canAccessResourcesOf is not applicable for: " - + EnumSet.of(getAuthenticationType(), other.getAuthenticationType()); + + EnumSet.of(getAuthenticationType(), other.getAuthenticationType()); return false; } } @@ -196,12 +202,12 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Authentication that = (Authentication) o; - return user.equals(that.user) && - authenticatedBy.equals(that.authenticatedBy) && - Objects.equals(lookedUpBy, that.lookedUpBy) && - version.equals(that.version) && - type == that.type && - metadata.equals(that.metadata); + return user.equals(that.user) + && authenticatedBy.equals(that.authenticatedBy) + && Objects.equals(lookedUpBy, that.lookedUpBy) + && version.equals(that.version) + && type == that.type + && metadata.equals(that.metadata); } @Override @@ -229,8 +235,10 @@ public void toXContentFragment(XContentBuilder builder) throws IOException { assert tokenName != null : "token name cannot be null"; final String tokenSource = (String) getMetadata().get(ServiceAccountSettings.TOKEN_SOURCE_FIELD); assert tokenSource != null : "token source cannot be null"; - builder.field(User.Fields.TOKEN.getPreferredName(), - Map.of("name", tokenName, "type", ServiceAccountSettings.REALM_TYPE + "_" + tokenSource)); + builder.field( + User.Fields.TOKEN.getPreferredName(), + Map.of("name", tokenName, "type", ServiceAccountSettings.REALM_TYPE + "_" + tokenSource) + ); } builder.field(User.Fields.METADATA.getPreferredName(), user.metadata()); builder.field(User.Fields.ENABLED.getPreferredName(), user.enabled()); @@ -252,10 +260,11 @@ public void toXContentFragment(XContentBuilder builder) throws IOException { @Override public String toString() { - StringBuilder builder = new StringBuilder("Authentication[") - .append(user) - .append(",type=").append(type) - .append(",by=").append(authenticatedBy); + StringBuilder builder = new StringBuilder("Authentication[").append(user) + .append(",type=") + .append(type) + .append(",by=") + .append(authenticatedBy); if (lookedUpBy != null) { builder.append(",lookup=").append(lookedUpBy); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/AuthenticationFailureHandler.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/AuthenticationFailureHandler.java index 4a1f6e644625a..1034ce774aa6d 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/AuthenticationFailureHandler.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/AuthenticationFailureHandler.java @@ -50,8 +50,12 @@ public interface AuthenticationFailureHandler { * @param context The context of the request that failed authentication that could not be authenticated * @return ElasticsearchSecurityException with the appropriate headers and message */ - ElasticsearchSecurityException failedAuthentication(TransportMessage message, AuthenticationToken token, String action, - ThreadContext context); + ElasticsearchSecurityException failedAuthentication( + TransportMessage message, + AuthenticationToken token, + String action, + ThreadContext context + ); /** * The method is called when an exception has occurred while processing the REST request. This could be an error that diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/AuthenticationResult.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/AuthenticationResult.java index a247a0531b631..5d144931a906d 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/AuthenticationResult.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/AuthenticationResult.java @@ -52,8 +52,13 @@ public enum Status { private final Exception exception; private final Map metadata; - private AuthenticationResult(Status status, @Nullable T value, @Nullable String message, @Nullable Exception exception, - @Nullable Map metadata) { + private AuthenticationResult( + Status status, + @Nullable T value, + @Nullable String message, + @Nullable Exception exception, + @Nullable Map metadata + ) { this.status = status; this.value = value; this.message = message; @@ -167,12 +172,16 @@ public boolean isAuthenticated() { @Override public String toString() { - return "AuthenticationResult{" + - "status=" + status + - ", value=" + value + - ", message=" + message + - ", exception=" + exception + - '}'; + return "AuthenticationResult{" + + "status=" + + status + + ", value=" + + value + + ", message=" + + message + + ", exception=" + + exception + + '}'; } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/AuthenticationServiceField.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/AuthenticationServiceField.java index 43017efc68d7c..bc290936b3c79 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/AuthenticationServiceField.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/AuthenticationServiceField.java @@ -12,8 +12,11 @@ public final class AuthenticationServiceField { - public static final Setting RUN_AS_ENABLED = - Setting.boolSetting(setting("authc.run_as.enabled"), true, Setting.Property.NodeScope); + public static final Setting RUN_AS_ENABLED = Setting.boolSetting( + setting("authc.run_as.enabled"), + true, + Setting.Property.NodeScope + ); public static final String RUN_AS_USER_HEADER = "es-security-runas-user"; private AuthenticationServiceField() {} diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/DefaultAuthenticationFailureHandler.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/DefaultAuthenticationFailureHandler.java index 7299f1ffd6c5a..4eb5c3f3a989e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/DefaultAuthenticationFailureHandler.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/DefaultAuthenticationFailureHandler.java @@ -41,19 +41,22 @@ public class DefaultAuthenticationFailureHandler implements AuthenticationFailur */ public DefaultAuthenticationFailureHandler(final Map> failureResponseHeaders) { if (failureResponseHeaders == null || failureResponseHeaders.isEmpty()) { - this.defaultFailureResponseHeaders = Collections.singletonMap("WWW-Authenticate", - Collections.singletonList("Basic realm=\"" + XPackField.SECURITY + "\" charset=\"UTF-8\"")); + this.defaultFailureResponseHeaders = Collections.singletonMap( + "WWW-Authenticate", + Collections.singletonList("Basic realm=\"" + XPackField.SECURITY + "\" charset=\"UTF-8\"") + ); } else { - this.defaultFailureResponseHeaders = Collections.unmodifiableMap(failureResponseHeaders.entrySet().stream().collect(Collectors - .toMap(entry -> entry.getKey(), entry -> { - if (entry.getKey().equalsIgnoreCase("WWW-Authenticate")) { - List values = new ArrayList<>(entry.getValue()); - values.sort(Comparator.comparing(DefaultAuthenticationFailureHandler::authSchemePriority)); - return Collections.unmodifiableList(values); - } else { - return Collections.unmodifiableList(entry.getValue()); - } - }))); + this.defaultFailureResponseHeaders = Collections.unmodifiableMap( + failureResponseHeaders.entrySet().stream().collect(Collectors.toMap(entry -> entry.getKey(), entry -> { + if (entry.getKey().equalsIgnoreCase("WWW-Authenticate")) { + List values = new ArrayList<>(entry.getValue()); + values.sort(Comparator.comparing(DefaultAuthenticationFailureHandler::authSchemePriority)); + return Collections.unmodifiableList(values); + } else { + return Collections.unmodifiableList(entry.getValue()); + } + })) + ); } } @@ -62,7 +65,7 @@ public DefaultAuthenticationFailureHandler(final Map> failu * * @param failureResponseHeaders the Map of failure response headers to be set */ - public void setHeaders(Map> failureResponseHeaders){ + public void setHeaders(Map> failureResponseHeaders) { defaultFailureResponseHeaders = failureResponseHeaders; } @@ -94,8 +97,12 @@ public ElasticsearchSecurityException failedAuthentication(RestRequest request, } @Override - public ElasticsearchSecurityException failedAuthentication(TransportMessage message, AuthenticationToken token, String action, - ThreadContext context) { + public ElasticsearchSecurityException failedAuthentication( + TransportMessage message, + AuthenticationToken token, + String action, + ThreadContext context + ) { return createAuthenticationError("unable to authenticate user [{}] for action [{}]", null, token.principal(), action); } @@ -110,8 +117,12 @@ public ElasticsearchSecurityException exceptionProcessingRequest(RestRequest req } @Override - public ElasticsearchSecurityException exceptionProcessingRequest(TransportMessage message, String action, Exception e, - ThreadContext context) { + public ElasticsearchSecurityException exceptionProcessingRequest( + TransportMessage message, + String action, + Exception e, + ThreadContext context + ) { // a couple of authn processing errors can also return {@link RestStatus#INTERNAL_SERVER_ERROR} or // {@link RestStatus#SERVICE_UNAVAILABLE}, besides the obvious {@link RestStatus#UNAUTHORIZED} if (e instanceof ElasticsearchAuthenticationProcessingError) { @@ -165,9 +176,9 @@ private ElasticsearchSecurityException createAuthenticationError(final String me * replace. In case of kerberos spnego mechanism, we use * 'WWW-Authenticate' header value to communicate outToken to peer. */ - containsNegotiateWithToken = - ese.getHeader("WWW-Authenticate").stream() - .anyMatch(s -> s != null && s.regionMatches(true, 0, "Negotiate ", 0, "Negotiate ".length())); + containsNegotiateWithToken = ese.getHeader("WWW-Authenticate") + .stream() + .anyMatch(s -> s != null && s.regionMatches(true, 0, "Negotiate ", 0, "Negotiate ".length())); } else { containsNegotiateWithToken = false; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/InternalRealmsSettings.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/InternalRealmsSettings.java index 8b1c0ca95d902..eaeec9c5570d6 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/InternalRealmsSettings.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/InternalRealmsSettings.java @@ -20,8 +20,7 @@ import java.util.Set; public final class InternalRealmsSettings { - private InternalRealmsSettings() { - } + private InternalRealmsSettings() {} /** * Provides the {@link Setting setting configuration} for each internal realm type. diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/KeyAndTimestamp.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/KeyAndTimestamp.java index 4e854048d1cc6..547b40ad44d24 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/KeyAndTimestamp.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/KeyAndTimestamp.java @@ -33,8 +33,13 @@ public KeyAndTimestamp(SecureString key, long timestamp) { key = new SecureString(Arrays.copyOfRange(ref, 0, len)); } - public long getTimestamp() { return timestamp; } - public SecureString getKey() { return key; } + public long getTimestamp() { + return timestamp; + } + + public SecureString getKey() { + return key; + } @Override public void writeTo(StreamOutput out) throws IOException { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/Realm.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/Realm.java index 8f1a9e14cdf88..682ae8bd349d9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/Realm.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/Realm.java @@ -6,13 +6,13 @@ */ package org.elasticsearch.xpack.core.security.authc; -import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.elasticsearch.action.ActionListener; import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.license.XPackLicenseState; -import org.elasticsearch.xpack.core.security.authc.support.DelegatedAuthorizationSettings; import org.elasticsearch.xpack.core.XPackField; +import org.elasticsearch.xpack.core.security.authc.support.DelegatedAuthorizationSettings; import org.elasticsearch.xpack.core.security.user.User; import java.util.Collections; @@ -64,8 +64,10 @@ public int order() { * @return Map of authentication failure response headers. */ public Map> getAuthenticationFailureHeaders() { - return Collections.singletonMap("WWW-Authenticate", - Collections.singletonList("Basic realm=\"" + XPackField.SECURITY + "\" charset=\"UTF-8\"")); + return Collections.singletonMap( + "WWW-Authenticate", + Collections.singletonList("Basic realm=\"" + XPackField.SECURITY + "\" charset=\"UTF-8\"") + ); } @Override @@ -148,8 +150,7 @@ public String toString() { * * @see DelegatedAuthorizationSettings */ - public void initialize(Iterable realms, XPackLicenseState licenseState) { - } + public void initialize(Iterable realms, XPackLicenseState licenseState) {} /** * A factory interface to construct a security realm. diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/RealmConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/RealmConfig.java index f9a985ef63f2c..4f9a59b1019c6 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/RealmConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/RealmConfig.java @@ -31,9 +31,12 @@ public RealmConfig(RealmIdentifier identifier, Settings settings, Environment en this.threadContext = threadContext; this.enabled = getSetting(RealmSettings.ENABLED_SETTING); if (enabled && false == hasSetting(RealmSettings.ORDER_SETTING.apply(type()))) { - throw new IllegalArgumentException("'order' is a mandatory parameter for realm config. " + - "Found invalid config for realm: '" + identifier.name + "'\n" + - "Please see the breaking changes documentation." + throw new IllegalArgumentException( + "'order' is a mandatory parameter for realm config. " + + "Found invalid config for realm: '" + + identifier.name + + "'\n" + + "Please see the breaking changes documentation." ); } this.order = getSetting(RealmSettings.ORDER_SETTING); @@ -205,8 +208,7 @@ public boolean equals(Object o) { return false; } final RealmIdentifier other = (RealmIdentifier) o; - return Objects.equals(this.type, other.type) && - Objects.equals(this.name, other.name); + return Objects.equals(this.type, other.type) && Objects.equals(this.name, other.name); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/RealmSettings.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/RealmSettings.java index eec297623844b..8e4a156e7f3f7 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/RealmSettings.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/RealmSettings.java @@ -6,12 +6,12 @@ */ package org.elasticsearch.xpack.core.security.authc; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.settings.SecureSetting; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsException; +import org.elasticsearch.core.Tuple; import java.util.Arrays; import java.util.List; @@ -32,10 +32,14 @@ public class RealmSettings { public static final String RESERVED_REALM_NAME_PREFIX = "_"; public static final String PREFIX = "xpack.security.authc.realms."; - public static final Function> ENABLED_SETTING = affixSetting("enabled", - key -> Setting.boolSetting(key, true, Setting.Property.NodeScope)); - public static final Function> ORDER_SETTING = affixSetting("order", - key -> Setting.intSetting(key, Integer.MAX_VALUE, Setting.Property.NodeScope)); + public static final Function> ENABLED_SETTING = affixSetting( + "enabled", + key -> Setting.boolSetting(key, true, Setting.Property.NodeScope) + ); + public static final Function> ORDER_SETTING = affixSetting( + "order", + key -> Setting.intSetting(key, Integer.MAX_VALUE, Setting.Property.NodeScope) + ); public static String realmSettingPrefix(String type) { return PREFIX + type + "."; @@ -88,17 +92,15 @@ public static Function> affixSetting(String */ public static Map getRealmSettings(Settings globalSettings) { Settings settingsByType = globalSettings.getByPrefix(RealmSettings.PREFIX); - return settingsByType.names().stream() - .flatMap(type -> { - final Settings settingsByName = settingsByType.getAsSettings(type); - return settingsByName.names().stream().map(name -> { - final RealmConfig.RealmIdentifier id = new RealmConfig.RealmIdentifier(type, name); - final Settings realmSettings = settingsByName.getAsSettings(name); - verifyRealmSettings(id, realmSettings); - return new Tuple<>(id, realmSettings); - }); - }) - .collect(Collectors.toMap(Tuple::v1, Tuple::v2)); + return settingsByType.names().stream().flatMap(type -> { + final Settings settingsByName = settingsByType.getAsSettings(type); + return settingsByName.names().stream().map(name -> { + final RealmConfig.RealmIdentifier id = new RealmConfig.RealmIdentifier(type, name); + final Settings realmSettings = settingsByName.getAsSettings(name); + verifyRealmSettings(id, realmSettings); + return new Tuple<>(id, realmSettings); + }); + }).collect(Collectors.toMap(Tuple::v1, Tuple::v2)); } /** @@ -109,11 +111,12 @@ private static void verifyRealmSettings(RealmConfig.RealmIdentifier identifier, if (nonSecureSettings.isEmpty()) { final String prefix = realmSettingPrefix(identifier); throw new SettingsException( - "found settings for the realm [{}] (with type [{}]) in the secure settings (elasticsearch.keystore)," + - " but this realm does not have any settings in elasticsearch.yml." + - " Please remove these settings from the keystore, or update their names to match one of the realms that are" + - " defined in elasticsearch.yml - [{}]", - identifier.getName(), identifier.getType(), + "found settings for the realm [{}] (with type [{}]) in the secure settings (elasticsearch.keystore)," + + " but this realm does not have any settings in elasticsearch.yml." + + " Please remove these settings from the keystore, or update their names to match one of the realms that are" + + " defined in elasticsearch.yml - [{}]", + identifier.getName(), + identifier.getType(), realmSettings.keySet().stream().map(k -> prefix + k).collect(Collectors.joining(",")) ); } @@ -139,7 +142,6 @@ public static List> getStandardSettings(String realmType return Arrays.asList(ENABLED_SETTING.apply(realmType), ORDER_SETTING.apply(realmType)); } - private RealmSettings() { - } + private RealmSettings() {} } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/TokenMetadata.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/TokenMetadata.java index 3681364da0d25..017583042e3d6 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/TokenMetadata.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/TokenMetadata.java @@ -63,7 +63,6 @@ public String getWriteableName() { return TYPE; } - @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { // never render this to the user @@ -75,7 +74,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - TokenMetadata that = (TokenMetadata)o; + TokenMetadata that = (TokenMetadata) o; return keys.equals(that.keys) && Arrays.equals(currentKeyHash, that.currentKeyHash); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/kerberos/KerberosRealmSettings.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/kerberos/KerberosRealmSettings.java index 1aae9c9d499d7..7b7e41922d7ea 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/kerberos/KerberosRealmSettings.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/kerberos/KerberosRealmSettings.java @@ -9,8 +9,8 @@ import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Setting.Property; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.set.Sets; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.xpack.core.security.authc.RealmSettings; import org.elasticsearch.xpack.core.security.authc.support.DelegatedAuthorizationSettings; @@ -26,33 +26,52 @@ public final class KerberosRealmSettings { * Kerberos key tab for Elasticsearch service
    * Uses single key tab for multiple service accounts. */ - public static final Setting.AffixSetting HTTP_SERVICE_KEYTAB_PATH = RealmSettings.simpleString(TYPE, - "keytab.path", Property.NodeScope); + public static final Setting.AffixSetting HTTP_SERVICE_KEYTAB_PATH = RealmSettings.simpleString( + TYPE, + "keytab.path", + Property.NodeScope + ); public static final Setting.AffixSetting SETTING_KRB_DEBUG_ENABLE = Setting.affixKeySetting( - RealmSettings.realmSettingPrefix(TYPE), "krb.debug", key -> Setting.boolSetting(key, Boolean.FALSE, Property.NodeScope)); + RealmSettings.realmSettingPrefix(TYPE), + "krb.debug", + key -> Setting.boolSetting(key, Boolean.FALSE, Property.NodeScope) + ); public static final Setting.AffixSetting SETTING_REMOVE_REALM_NAME = Setting.affixKeySetting( - RealmSettings.realmSettingPrefix(TYPE), "remove_realm_name", key -> Setting.boolSetting(key, Boolean.FALSE, Property.NodeScope)); + RealmSettings.realmSettingPrefix(TYPE), + "remove_realm_name", + key -> Setting.boolSetting(key, Boolean.FALSE, Property.NodeScope) + ); // Cache private static final TimeValue DEFAULT_TTL = TimeValue.timeValueMinutes(20); private static final int DEFAULT_MAX_USERS = 100_000; // 100k users public static final Setting.AffixSetting CACHE_TTL_SETTING = Setting.affixKeySetting( - RealmSettings.realmSettingPrefix(TYPE), "cache.ttl", key -> Setting.timeSetting(key, DEFAULT_TTL, Setting.Property.NodeScope)); + RealmSettings.realmSettingPrefix(TYPE), + "cache.ttl", + key -> Setting.timeSetting(key, DEFAULT_TTL, Setting.Property.NodeScope) + ); public static final Setting.AffixSetting CACHE_MAX_USERS_SETTING = Setting.affixKeySetting( - RealmSettings.realmSettingPrefix(TYPE), "cache.max_users", key -> Setting.intSetting(key, DEFAULT_MAX_USERS, Property.NodeScope)); + RealmSettings.realmSettingPrefix(TYPE), + "cache.max_users", + key -> Setting.intSetting(key, DEFAULT_MAX_USERS, Property.NodeScope) + ); - private KerberosRealmSettings() { - } + private KerberosRealmSettings() {} /** * @return the valid set of {@link Setting}s for a {@value #TYPE} realm */ public static Set> getSettings() { - final Set> settings = Sets.newHashSet(HTTP_SERVICE_KEYTAB_PATH, CACHE_TTL_SETTING, CACHE_MAX_USERS_SETTING, - SETTING_KRB_DEBUG_ENABLE, SETTING_REMOVE_REALM_NAME); + final Set> settings = Sets.newHashSet( + HTTP_SERVICE_KEYTAB_PATH, + CACHE_TTL_SETTING, + CACHE_MAX_USERS_SETTING, + SETTING_KRB_DEBUG_ENABLE, + SETTING_REMOVE_REALM_NAME + ); settings.addAll(DelegatedAuthorizationSettings.getSettings(TYPE)); settings.addAll(RealmSettings.getStandardSettings(TYPE)); return settings; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/ldap/ActiveDirectorySessionFactorySettings.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/ldap/ActiveDirectorySessionFactorySettings.java index 6e2d155e44695..7cf9fc683687c 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/ldap/ActiveDirectorySessionFactorySettings.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/ldap/ActiveDirectorySessionFactorySettings.java @@ -19,62 +19,94 @@ public final class ActiveDirectorySessionFactorySettings { private static final String AD_DOMAIN_NAME_SETTING_KEY = "domain_name"; - public static final Function> AD_DOMAIN_NAME_SETTING - = RealmSettings.affixSetting(AD_DOMAIN_NAME_SETTING_KEY, + public static final Function> AD_DOMAIN_NAME_SETTING = RealmSettings.affixSetting( + AD_DOMAIN_NAME_SETTING_KEY, key -> Setting.simpleString(key, v -> { if (Strings.isNullOrEmpty(v)) { throw new IllegalArgumentException("missing [" + key + "] setting for active directory"); } - }, Setting.Property.NodeScope)); + }, Setting.Property.NodeScope) + ); public static final String AD_GROUP_SEARCH_BASEDN_SETTING = "group_search.base_dn"; public static final String AD_GROUP_SEARCH_SCOPE_SETTING = "group_search.scope"; private static final String AD_USER_SEARCH_BASEDN_SETTING_KEY = "user_search.base_dn"; - public static final Setting.AffixSetting AD_USER_SEARCH_BASEDN_SETTING - = RealmSettings.simpleString(AD_TYPE, AD_USER_SEARCH_BASEDN_SETTING_KEY, Setting.Property.NodeScope); + public static final Setting.AffixSetting AD_USER_SEARCH_BASEDN_SETTING = RealmSettings.simpleString( + AD_TYPE, + AD_USER_SEARCH_BASEDN_SETTING_KEY, + Setting.Property.NodeScope + ); private static final String AD_USER_SEARCH_FILTER_SETTING_KEY = "user_search.filter"; - public static final Setting.AffixSetting AD_USER_SEARCH_FILTER_SETTING - = RealmSettings.simpleString(AD_TYPE, AD_USER_SEARCH_FILTER_SETTING_KEY, Setting.Property.NodeScope); + public static final Setting.AffixSetting AD_USER_SEARCH_FILTER_SETTING = RealmSettings.simpleString( + AD_TYPE, + AD_USER_SEARCH_FILTER_SETTING_KEY, + Setting.Property.NodeScope + ); private static final String AD_UPN_USER_SEARCH_FILTER_SETTING_KEY = "user_search.upn_filter"; - public static final Setting.AffixSetting AD_UPN_USER_SEARCH_FILTER_SETTING - = RealmSettings.simpleString(AD_TYPE, AD_UPN_USER_SEARCH_FILTER_SETTING_KEY, Setting.Property.NodeScope); + public static final Setting.AffixSetting AD_UPN_USER_SEARCH_FILTER_SETTING = RealmSettings.simpleString( + AD_TYPE, + AD_UPN_USER_SEARCH_FILTER_SETTING_KEY, + Setting.Property.NodeScope + ); private static final String AD_DOWN_LEVEL_USER_SEARCH_FILTER_SETTING_KEY = "user_search.down_level_filter"; - public static final Setting.AffixSetting AD_DOWN_LEVEL_USER_SEARCH_FILTER_SETTING - = RealmSettings.simpleString(AD_TYPE, AD_DOWN_LEVEL_USER_SEARCH_FILTER_SETTING_KEY, Setting.Property.NodeScope); + public static final Setting.AffixSetting AD_DOWN_LEVEL_USER_SEARCH_FILTER_SETTING = RealmSettings.simpleString( + AD_TYPE, + AD_DOWN_LEVEL_USER_SEARCH_FILTER_SETTING_KEY, + Setting.Property.NodeScope + ); private static final String AD_USER_SEARCH_SCOPE_SETTING_KEY = "user_search.scope"; - public static final Setting.AffixSetting AD_USER_SEARCH_SCOPE_SETTING - = RealmSettings.simpleString(AD_TYPE, AD_USER_SEARCH_SCOPE_SETTING_KEY, Setting.Property.NodeScope); + public static final Setting.AffixSetting AD_USER_SEARCH_SCOPE_SETTING = RealmSettings.simpleString( + AD_TYPE, + AD_USER_SEARCH_SCOPE_SETTING_KEY, + Setting.Property.NodeScope + ); public static final Setting.AffixSetting AD_LDAP_PORT_SETTING = Setting.affixKeySetting( - RealmSettings.realmSettingPrefix(AD_TYPE), "port.ldap", key -> Setting.intSetting(key, 389, Setting.Property.NodeScope)); + RealmSettings.realmSettingPrefix(AD_TYPE), + "port.ldap", + key -> Setting.intSetting(key, 389, Setting.Property.NodeScope) + ); public static final Setting.AffixSetting AD_LDAPS_PORT_SETTING = Setting.affixKeySetting( - RealmSettings.realmSettingPrefix(AD_TYPE), "port.ldaps", key -> Setting.intSetting(key, 636, Setting.Property.NodeScope)); + RealmSettings.realmSettingPrefix(AD_TYPE), + "port.ldaps", + key -> Setting.intSetting(key, 636, Setting.Property.NodeScope) + ); public static final Setting.AffixSetting AD_GC_LDAP_PORT_SETTING = Setting.affixKeySetting( - RealmSettings.realmSettingPrefix(AD_TYPE), "port.gc_ldap", key -> Setting.intSetting(key, 3268, Setting.Property.NodeScope)); + RealmSettings.realmSettingPrefix(AD_TYPE), + "port.gc_ldap", + key -> Setting.intSetting(key, 3268, Setting.Property.NodeScope) + ); public static final Setting.AffixSetting AD_GC_LDAPS_PORT_SETTING = Setting.affixKeySetting( - RealmSettings.realmSettingPrefix(AD_TYPE), "port.gc_ldaps", key -> Setting.intSetting(key, 3269, Setting.Property.NodeScope)); + RealmSettings.realmSettingPrefix(AD_TYPE), + "port.gc_ldaps", + key -> Setting.intSetting(key, 3269, Setting.Property.NodeScope) + ); public static final String POOL_ENABLED_SUFFIX = "user_search.pool.enabled"; public static final Setting.AffixSetting POOL_ENABLED = Setting.affixKeySetting( - RealmSettings.realmSettingPrefix(AD_TYPE), POOL_ENABLED_SUFFIX, - key -> { - if (key.endsWith(POOL_ENABLED_SUFFIX)) { - final String bindDnKey = key.substring(0, key.length() - POOL_ENABLED_SUFFIX.length()) - + PoolingSessionFactorySettings.BIND_DN_SUFFIX; - return Setting.boolSetting(key, settings -> Boolean.toString(settings.keySet().contains(bindDnKey)), - Setting.Property.NodeScope); - } else { - return Setting.boolSetting(key, false, Setting.Property.NodeScope); - } - }); + RealmSettings.realmSettingPrefix(AD_TYPE), + POOL_ENABLED_SUFFIX, + key -> { + if (key.endsWith(POOL_ENABLED_SUFFIX)) { + final String bindDnKey = key.substring(0, key.length() - POOL_ENABLED_SUFFIX.length()) + + PoolingSessionFactorySettings.BIND_DN_SUFFIX; + return Setting.boolSetting( + key, + settings -> Boolean.toString(settings.keySet().contains(bindDnKey)), + Setting.Property.NodeScope + ); + } else { + return Setting.boolSetting(key, false, Setting.Property.NodeScope); + } + } + ); - private ActiveDirectorySessionFactorySettings() { - } + private ActiveDirectorySessionFactorySettings() {} public static Set> getSettings() { Set> settings = new HashSet<>(); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/ldap/LdapRealmSettings.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/ldap/LdapRealmSettings.java index 1eba794ec997c..29ce47d9e8d5f 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/ldap/LdapRealmSettings.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/ldap/LdapRealmSettings.java @@ -23,12 +23,13 @@ public final class LdapRealmSettings { public static final String AD_TYPE = "active_directory"; public static final String TIMEOUT_EXECUTION_SUFFIX = "timeout.execution"; - public static final Function> EXECUTION_TIMEOUT = type -> - Setting.affixKeySetting(RealmSettings.realmSettingPrefix(type), TIMEOUT_EXECUTION_SUFFIX, - key -> Setting.timeSetting(key, TimeValue.timeValueSeconds(30L), Setting.Property.NodeScope)); + public static final Function> EXECUTION_TIMEOUT = type -> Setting.affixKeySetting( + RealmSettings.realmSettingPrefix(type), + TIMEOUT_EXECUTION_SUFFIX, + key -> Setting.timeSetting(key, TimeValue.timeValueSeconds(30L), Setting.Property.NodeScope) + ); - private LdapRealmSettings() { - } + private LdapRealmSettings() {} /** * @param type Either {@link #AD_TYPE} or {@link #LDAP_TYPE} diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/ldap/LdapSessionFactorySettings.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/ldap/LdapSessionFactorySettings.java index b54710f3839d1..8dcf7f8f1c7e6 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/ldap/LdapSessionFactorySettings.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/ldap/LdapSessionFactorySettings.java @@ -21,8 +21,10 @@ public final class LdapSessionFactorySettings { public static final Setting.AffixSetting> USER_DN_TEMPLATES_SETTING = Setting.affixKeySetting( - RealmSettings.realmSettingPrefix(LDAP_TYPE), "user_dn_templates", - key -> Setting.listSetting(key, Collections.emptyList(), Function.identity(), Setting.Property.NodeScope)); + RealmSettings.realmSettingPrefix(LDAP_TYPE), + "user_dn_templates", + key -> Setting.listSetting(key, Collections.emptyList(), Function.identity(), Setting.Property.NodeScope) + ); public static Set> getSettings() { Set> settings = new HashSet<>(); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/ldap/LdapUserSearchSessionFactorySettings.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/ldap/LdapUserSearchSessionFactorySettings.java index 0a155af5b7f99..77733d5a403b7 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/ldap/LdapUserSearchSessionFactorySettings.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/ldap/LdapUserSearchSessionFactorySettings.java @@ -19,27 +19,47 @@ public final class LdapUserSearchSessionFactorySettings { public static final Setting.AffixSetting SEARCH_ATTRIBUTE = Setting.affixKeySetting( - RealmSettings.realmSettingPrefix(LDAP_TYPE), "user_search.attribute", - key -> new Setting<>(key, LdapUserSearchSessionFactorySettings.DEFAULT_USERNAME_ATTRIBUTE, Function.identity(), - Setting.Property.NodeScope, Setting.Property.Deprecated)); + RealmSettings.realmSettingPrefix(LDAP_TYPE), + "user_search.attribute", + key -> new Setting<>( + key, + LdapUserSearchSessionFactorySettings.DEFAULT_USERNAME_ATTRIBUTE, + Function.identity(), + Setting.Property.NodeScope, + Setting.Property.Deprecated + ) + ); - public static final Setting.AffixSetting SEARCH_BASE_DN - = RealmSettings.simpleString(LDAP_TYPE, "user_search.base_dn", Setting.Property.NodeScope); + public static final Setting.AffixSetting SEARCH_BASE_DN = RealmSettings.simpleString( + LDAP_TYPE, + "user_search.base_dn", + Setting.Property.NodeScope + ); - public static final Setting.AffixSetting SEARCH_FILTER - = RealmSettings.simpleString(LDAP_TYPE, "user_search.filter", Setting.Property.NodeScope); + public static final Setting.AffixSetting SEARCH_FILTER = RealmSettings.simpleString( + LDAP_TYPE, + "user_search.filter", + Setting.Property.NodeScope + ); public static final Setting.AffixSetting SEARCH_SCOPE = Setting.affixKeySetting( - RealmSettings.realmSettingPrefix(LDAP_TYPE), "user_search.scope", - key -> new Setting<>(key, (String) null, (String s) -> LdapSearchScope.resolve(s, LdapSearchScope.SUB_TREE), - Setting.Property.NodeScope)); + RealmSettings.realmSettingPrefix(LDAP_TYPE), + "user_search.scope", + key -> new Setting<>( + key, + (String) null, + (String s) -> LdapSearchScope.resolve(s, LdapSearchScope.SUB_TREE), + Setting.Property.NodeScope + ) + ); public static final Setting.AffixSetting POOL_ENABLED = Setting.affixKeySetting( - RealmSettings.realmSettingPrefix(LDAP_TYPE), "user_search.pool.enabled", - key -> Setting.boolSetting(key, true, Setting.Property.NodeScope)); + RealmSettings.realmSettingPrefix(LDAP_TYPE), + "user_search.pool.enabled", + key -> Setting.boolSetting(key, true, Setting.Property.NodeScope) + ); private static final String DEFAULT_USERNAME_ATTRIBUTE = "uid"; - private LdapUserSearchSessionFactorySettings() { - } + private LdapUserSearchSessionFactorySettings() {} public static Set> getSettings() { Set> settings = new HashSet<>(); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/ldap/PoolingSessionFactorySettings.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/ldap/PoolingSessionFactorySettings.java index a1eb5b115e470..d042233e7cbbb 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/ldap/PoolingSessionFactorySettings.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/ldap/PoolingSessionFactorySettings.java @@ -23,48 +23,61 @@ public final class PoolingSessionFactorySettings { public static final TimeValue DEFAULT_HEALTH_CHECK_INTERVAL = TimeValue.timeValueSeconds(60L); public static final String BIND_DN_SUFFIX = "bind_dn"; - public static final Function> BIND_DN = RealmSettings.affixSetting(BIND_DN_SUFFIX, - key -> Setting.simpleString(key, Setting.Property.NodeScope, Setting.Property.Filtered)); + public static final Function> BIND_DN = RealmSettings.affixSetting( + BIND_DN_SUFFIX, + key -> Setting.simpleString(key, Setting.Property.NodeScope, Setting.Property.Filtered) + ); public static final Function> LEGACY_BIND_PASSWORD = RealmSettings.affixSetting( - "bind_password", key -> new Setting<>(key, "", SecureString::new, - Setting.Property.NodeScope, Setting.Property.Filtered, Setting.Property.Deprecated)); + "bind_password", + key -> new Setting<>(key, "", SecureString::new, Setting.Property.NodeScope, Setting.Property.Filtered, Setting.Property.Deprecated) + ); - public static final Function> SECURE_BIND_PASSWORD = realmType -> - Setting.affixKeySetting( - RealmSettings.realmSettingPrefix(realmType), "secure_bind_password", - key -> secureString(key, null) - ); + public static final Function> SECURE_BIND_PASSWORD = realmType -> Setting.affixKeySetting( + RealmSettings.realmSettingPrefix(realmType), + "secure_bind_password", + key -> secureString(key, null) + ); public static final int DEFAULT_CONNECTION_POOL_INITIAL_SIZE = 0; public static final Function> POOL_INITIAL_SIZE = RealmSettings.affixSetting( - "user_search.pool.initial_size", - key -> Setting.intSetting(key, DEFAULT_CONNECTION_POOL_INITIAL_SIZE, 0, Setting.Property.NodeScope)); + "user_search.pool.initial_size", + key -> Setting.intSetting(key, DEFAULT_CONNECTION_POOL_INITIAL_SIZE, 0, Setting.Property.NodeScope) + ); public static final int DEFAULT_CONNECTION_POOL_SIZE = 20; - public static final Function> POOL_SIZE = RealmSettings.affixSetting("user_search.pool.size", - key -> Setting.intSetting(key, DEFAULT_CONNECTION_POOL_SIZE, 1, Setting.Property.NodeScope)); + public static final Function> POOL_SIZE = RealmSettings.affixSetting( + "user_search.pool.size", + key -> Setting.intSetting(key, DEFAULT_CONNECTION_POOL_SIZE, 1, Setting.Property.NodeScope) + ); public static final Function> HEALTH_CHECK_INTERVAL = RealmSettings.affixSetting( - "user_search.pool.health_check.interval", - key -> Setting.timeSetting(key, DEFAULT_HEALTH_CHECK_INTERVAL, Setting.Property.NodeScope)); + "user_search.pool.health_check.interval", + key -> Setting.timeSetting(key, DEFAULT_HEALTH_CHECK_INTERVAL, Setting.Property.NodeScope) + ); public static final Function> HEALTH_CHECK_ENABLED = RealmSettings.affixSetting( - "user_search.pool.health_check.enabled", - key -> Setting.boolSetting(key, true, Setting.Property.NodeScope)); + "user_search.pool.health_check.enabled", + key -> Setting.boolSetting(key, true, Setting.Property.NodeScope) + ); public static final Function>> HEALTH_CHECK_DN = RealmSettings.affixSetting( - "user_search.pool.health_check.dn", - key -> new Setting<>(key, (String) null, - Optional::ofNullable, Setting.Property.NodeScope)); + "user_search.pool.health_check.dn", + key -> new Setting<>(key, (String) null, Optional::ofNullable, Setting.Property.NodeScope) + ); - private PoolingSessionFactorySettings() { - } + private PoolingSessionFactorySettings() {} public static Set> getSettings(String realmType) { return Stream.of( - POOL_INITIAL_SIZE, POOL_SIZE, HEALTH_CHECK_ENABLED, HEALTH_CHECK_INTERVAL, HEALTH_CHECK_DN, BIND_DN, - LEGACY_BIND_PASSWORD, SECURE_BIND_PASSWORD + POOL_INITIAL_SIZE, + POOL_SIZE, + HEALTH_CHECK_ENABLED, + HEALTH_CHECK_INTERVAL, + HEALTH_CHECK_DN, + BIND_DN, + LEGACY_BIND_PASSWORD, + SECURE_BIND_PASSWORD ).map(f -> f.apply(realmType)).collect(Collectors.toSet()); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/ldap/SearchGroupsResolverSettings.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/ldap/SearchGroupsResolverSettings.java index ec873ed88ed86..5cd7c18fff850 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/ldap/SearchGroupsResolverSettings.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/ldap/SearchGroupsResolverSettings.java @@ -19,25 +19,32 @@ public final class SearchGroupsResolverSettings { public static final Function> BASE_DN = RealmSettings.affixSetting( - "group_search.base_dn", key -> Setting.simpleString(key, new Setting.Property[]{Setting.Property.NodeScope})); + "group_search.base_dn", + key -> Setting.simpleString(key, new Setting.Property[] { Setting.Property.NodeScope }) + ); public static final Function> SCOPE = RealmSettings.affixSetting( - "group_search.scope", key -> new Setting<>(key, (String) null, - s -> LdapSearchScope.resolve(s, LdapSearchScope.SUB_TREE), Setting.Property.NodeScope)); - - public static final Setting.AffixSetting USER_ATTRIBUTE = RealmSettings.simpleString(LDAP_TYPE, "group_search.user_attribute", - Setting.Property.NodeScope); - - private static final String GROUP_SEARCH_DEFAULT_FILTER = "(&" + - "(|(objectclass=groupOfNames)(objectclass=groupOfUniqueNames)" + - "(objectclass=group)(objectclass=posixGroup))" + - "(|(uniqueMember={0})(member={0})(memberUid={0})))"; + "group_search.scope", + key -> new Setting<>(key, (String) null, s -> LdapSearchScope.resolve(s, LdapSearchScope.SUB_TREE), Setting.Property.NodeScope) + ); + + public static final Setting.AffixSetting USER_ATTRIBUTE = RealmSettings.simpleString( + LDAP_TYPE, + "group_search.user_attribute", + Setting.Property.NodeScope + ); + + private static final String GROUP_SEARCH_DEFAULT_FILTER = "(&" + + "(|(objectclass=groupOfNames)(objectclass=groupOfUniqueNames)" + + "(objectclass=group)(objectclass=posixGroup))" + + "(|(uniqueMember={0})(member={0})(memberUid={0})))"; public static final Setting.AffixSetting FILTER = Setting.affixKeySetting( - RealmSettings.realmSettingPrefix(LDAP_TYPE), "group_search.filter", - key -> new Setting<>(key, GROUP_SEARCH_DEFAULT_FILTER, Function.identity(), Setting.Property.NodeScope)); + RealmSettings.realmSettingPrefix(LDAP_TYPE), + "group_search.filter", + key -> new Setting<>(key, GROUP_SEARCH_DEFAULT_FILTER, Function.identity(), Setting.Property.NodeScope) + ); - private SearchGroupsResolverSettings() { - } + private SearchGroupsResolverSettings() {} public static Set> getSettings(String realmType) { Set> settings = new HashSet<>(); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/ldap/UserAttributeGroupsResolverSettings.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/ldap/UserAttributeGroupsResolverSettings.java index 35e7d8065d10f..b973dfbaf1409 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/ldap/UserAttributeGroupsResolverSettings.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/ldap/UserAttributeGroupsResolverSettings.java @@ -15,11 +15,12 @@ public final class UserAttributeGroupsResolverSettings { public static final Setting.AffixSetting ATTRIBUTE = Setting.affixKeySetting( - RealmSettings.realmSettingPrefix(LdapRealmSettings.LDAP_TYPE), "user_group_attribute", - key -> new Setting<>(key, "memberOf", Function.identity(), Setting.Property.NodeScope)); + RealmSettings.realmSettingPrefix(LdapRealmSettings.LDAP_TYPE), + "user_group_attribute", + key -> new Setting<>(key, "memberOf", Function.identity(), Setting.Property.NodeScope) + ); - private UserAttributeGroupsResolverSettings() { - } + private UserAttributeGroupsResolverSettings() {} public static Set> getSettings() { return Collections.singleton(ATTRIBUTE); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/ldap/support/LdapLoadBalancingSettings.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/ldap/support/LdapLoadBalancingSettings.java index ea9d16173429c..7772689508f40 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/ldap/support/LdapLoadBalancingSettings.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/ldap/support/LdapLoadBalancingSettings.java @@ -17,14 +17,17 @@ public final class LdapLoadBalancingSettings { public static final Function> LOAD_BALANCE_TYPE_SETTING = RealmSettings.affixSetting( - "load_balance.type", key -> Setting.simpleString(key, Setting.Property.NodeScope)); + "load_balance.type", + key -> Setting.simpleString(key, Setting.Property.NodeScope) + ); private static final TimeValue CACHE_TTL_DEFAULT = TimeValue.timeValueHours(1L); public static final Function> CACHE_TTL_SETTING = RealmSettings.affixSetting( - "load_balance.cache_ttl", key -> Setting.timeSetting(key, CACHE_TTL_DEFAULT, Setting.Property.NodeScope)); + "load_balance.cache_ttl", + key -> Setting.timeSetting(key, CACHE_TTL_DEFAULT, Setting.Property.NodeScope) + ); - private LdapLoadBalancingSettings() { - } + private LdapLoadBalancingSettings() {} public static Set> getSettings(String realmType) { Set> settings = new HashSet<>(); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/ldap/support/LdapMetadataResolverSettings.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/ldap/support/LdapMetadataResolverSettings.java index e367678b77d64..1000c3a874beb 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/ldap/support/LdapMetadataResolverSettings.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/ldap/support/LdapMetadataResolverSettings.java @@ -15,7 +15,9 @@ public final class LdapMetadataResolverSettings { public static final Function>> ADDITIONAL_METADATA_SETTING = RealmSettings.affixSetting( - "metadata", key -> Setting.listSetting(key, Collections.emptyList(), Function.identity(), Setting.Property.NodeScope)); + "metadata", + key -> Setting.listSetting(key, Collections.emptyList(), Function.identity(), Setting.Property.NodeScope) + ); private LdapMetadataResolverSettings() {} diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/ldap/support/LdapSearchScope.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/ldap/support/LdapSearchScope.java index 7ce66f3d6ebb0..808fd5e930830 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/ldap/support/LdapSearchScope.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/ldap/support/LdapSearchScope.java @@ -7,6 +7,7 @@ package org.elasticsearch.xpack.core.security.authc.ldap.support; import com.unboundid.ldap.sdk.SearchScope; + import org.elasticsearch.common.Strings; import java.util.Locale; @@ -33,9 +34,12 @@ public static LdapSearchScope resolve(String scope, LdapSearchScope defaultScope } switch (scope.toLowerCase(Locale.ENGLISH)) { case "base": - case "object": return BASE; - case "one_level" : return ONE_LEVEL; - case "sub_tree" : return SUB_TREE; + case "object": + return BASE; + case "one_level": + return ONE_LEVEL; + case "sub_tree": + return SUB_TREE; default: throw new IllegalArgumentException("unknown search scope [" + scope + "]"); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/ldap/support/SessionFactorySettings.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/ldap/support/SessionFactorySettings.java index 395579b5886a3..5a2664f36bc86 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/ldap/support/SessionFactorySettings.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/ldap/support/SessionFactorySettings.java @@ -20,33 +20,47 @@ public final class SessionFactorySettings { public static final Function>> URLS_SETTING = RealmSettings.affixSetting( - "url", key -> Setting.listSetting(key, Collections.emptyList(), Function.identity(), Setting.Property.NodeScope)); + "url", + key -> Setting.listSetting(key, Collections.emptyList(), Function.identity(), Setting.Property.NodeScope) + ); public static final TimeValue TIMEOUT_DEFAULT = TimeValue.timeValueSeconds(5); public static final Function> TIMEOUT_TCP_CONNECTION_SETTING = RealmSettings.affixSetting( - "timeout.tcp_connect", key -> Setting.timeSetting(key, TIMEOUT_DEFAULT, Setting.Property.NodeScope)); + "timeout.tcp_connect", + key -> Setting.timeSetting(key, TIMEOUT_DEFAULT, Setting.Property.NodeScope) + ); public static final Function> TIMEOUT_LDAP_SETTING = RealmSettings.affixSetting( - "timeout.ldap_search", key -> Setting.timeSetting(key, TIMEOUT_DEFAULT, Setting.Property.NodeScope)); + "timeout.ldap_search", + key -> Setting.timeSetting(key, TIMEOUT_DEFAULT, Setting.Property.NodeScope) + ); public static final Function> TIMEOUT_TCP_READ_SETTING = RealmSettings.affixSetting( - "timeout.tcp_read", key -> Setting.timeSetting(key, TimeValue.MINUS_ONE, Setting.Property.NodeScope, - Setting.Property.Deprecated)); + "timeout.tcp_read", + key -> Setting.timeSetting(key, TimeValue.MINUS_ONE, Setting.Property.NodeScope, Setting.Property.Deprecated) + ); public static final Function> TIMEOUT_RESPONSE_SETTING = RealmSettings.affixSetting( - "timeout.response", key -> Setting.timeSetting(key, TimeValue.MINUS_ONE, Setting.Property.NodeScope)); + "timeout.response", + key -> Setting.timeSetting(key, TimeValue.MINUS_ONE, Setting.Property.NodeScope) + ); public static final Function> HOSTNAME_VERIFICATION_SETTING = RealmSettings.affixSetting( - "hostname_verification", key -> Setting.boolSetting(key, true, Setting.Property.NodeScope, Setting.Property.Filtered)); + "hostname_verification", + key -> Setting.boolSetting(key, true, Setting.Property.NodeScope, Setting.Property.Filtered) + ); public static final Function> FOLLOW_REFERRALS_SETTING = RealmSettings.affixSetting( - "follow_referrals", key -> Setting.boolSetting(key, true, Setting.Property.NodeScope)); + "follow_referrals", + key -> Setting.boolSetting(key, true, Setting.Property.NodeScope) + ); public static final Function> IGNORE_REFERRAL_ERRORS_SETTING = RealmSettings.affixSetting( - "ignore_referral_errors", key -> Setting.boolSetting(key, true, Setting.Property.NodeScope)); + "ignore_referral_errors", + key -> Setting.boolSetting(key, true, Setting.Property.NodeScope) + ); - private SessionFactorySettings() { - } + private SessionFactorySettings() {} public static Set> getSettings(String realmType) { Set> settings = new HashSet<>(); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/oidc/OpenIdConnectRealmSettings.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/oidc/OpenIdConnectRealmSettings.java index dfa80259ed225..8de2c4e7cb14b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/oidc/OpenIdConnectRealmSettings.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/oidc/OpenIdConnectRealmSettings.java @@ -9,8 +9,8 @@ import org.apache.http.HttpHost; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Setting; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.set.Sets; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.xpack.core.security.authc.RealmConfig; import org.elasticsearch.xpack.core.security.authc.RealmSettings; import org.elasticsearch.xpack.core.security.authc.support.DelegatedAuthorizationSettings; @@ -27,141 +27,196 @@ import java.util.Set; import java.util.function.Function; - public class OpenIdConnectRealmSettings { - private OpenIdConnectRealmSettings() { - } + private OpenIdConnectRealmSettings() {} - public static final List SUPPORTED_SIGNATURE_ALGORITHMS = - List.of("HS256", "HS384", "HS512", "RS256", "RS384", "RS512", "ES256", "ES384", "ES512", "PS256", "PS384", "PS512"); + public static final List SUPPORTED_SIGNATURE_ALGORITHMS = List.of( + "HS256", + "HS384", + "HS512", + "RS256", + "RS384", + "RS512", + "ES256", + "ES384", + "ES512", + "PS256", + "PS384", + "PS512" + ); private static final List RESPONSE_TYPES = List.of("code", "id_token", "id_token token"); public static final List CLIENT_AUTH_METHODS = List.of("client_secret_basic", "client_secret_post", "client_secret_jwt"); public static final List SUPPORTED_CLIENT_AUTH_JWT_ALGORITHMS = List.of("HS256", "HS384", "HS512"); public static final String TYPE = "oidc"; - public static final Setting.AffixSetting RP_CLIENT_ID - = RealmSettings.simpleString(TYPE, "rp.client_id", Setting.Property.NodeScope); - public static final Setting.AffixSetting RP_CLIENT_SECRET - = RealmSettings.secureString(TYPE, "rp.client_secret"); - public static final Setting.AffixSetting RP_REDIRECT_URI - = Setting.affixKeySetting(RealmSettings.realmSettingPrefix(TYPE), "rp.redirect_uri", + public static final Setting.AffixSetting RP_CLIENT_ID = RealmSettings.simpleString( + TYPE, + "rp.client_id", + Setting.Property.NodeScope + ); + public static final Setting.AffixSetting RP_CLIENT_SECRET = RealmSettings.secureString(TYPE, "rp.client_secret"); + public static final Setting.AffixSetting RP_REDIRECT_URI = Setting.affixKeySetting( + RealmSettings.realmSettingPrefix(TYPE), + "rp.redirect_uri", key -> Setting.simpleString(key, v -> { try { new URI(v); } catch (URISyntaxException e) { throw new IllegalArgumentException("Invalid value [" + v + "] for [" + key + "]. Not a valid URI.", e); } - }, Setting.Property.NodeScope)); - public static final Setting.AffixSetting RP_POST_LOGOUT_REDIRECT_URI - = Setting.affixKeySetting(RealmSettings.realmSettingPrefix(TYPE), "rp.post_logout_redirect_uri", + }, Setting.Property.NodeScope) + ); + public static final Setting.AffixSetting RP_POST_LOGOUT_REDIRECT_URI = Setting.affixKeySetting( + RealmSettings.realmSettingPrefix(TYPE), + "rp.post_logout_redirect_uri", key -> Setting.simpleString(key, v -> { try { new URI(v); } catch (URISyntaxException e) { throw new IllegalArgumentException("Invalid value [" + v + "] for [" + key + "]. Not a valid URI.", e); } - }, Setting.Property.NodeScope)); - public static final Setting.AffixSetting RP_RESPONSE_TYPE - = Setting.affixKeySetting(RealmSettings.realmSettingPrefix(TYPE), "rp.response_type", + }, Setting.Property.NodeScope) + ); + public static final Setting.AffixSetting RP_RESPONSE_TYPE = Setting.affixKeySetting( + RealmSettings.realmSettingPrefix(TYPE), + "rp.response_type", key -> Setting.simpleString(key, v -> { if (RESPONSE_TYPES.contains(v) == false) { throw new IllegalArgumentException( - "Invalid value [" + v + "] for [" + key + "]. Allowed values are " + RESPONSE_TYPES + ""); + "Invalid value [" + v + "] for [" + key + "]. Allowed values are " + RESPONSE_TYPES + "" + ); } - }, Setting.Property.NodeScope)); - public static final Setting.AffixSetting RP_SIGNATURE_ALGORITHM - = Setting.affixKeySetting(RealmSettings.realmSettingPrefix(TYPE), "rp.signature_algorithm", + }, Setting.Property.NodeScope) + ); + public static final Setting.AffixSetting RP_SIGNATURE_ALGORITHM = Setting.affixKeySetting( + RealmSettings.realmSettingPrefix(TYPE), + "rp.signature_algorithm", key -> new Setting<>(key, "RS256", Function.identity(), v -> { if (SUPPORTED_SIGNATURE_ALGORITHMS.contains(v) == false) { throw new IllegalArgumentException( - "Invalid value [" + v + "] for [" + key + "]. Allowed values are " + SUPPORTED_SIGNATURE_ALGORITHMS + "}]"); + "Invalid value [" + v + "] for [" + key + "]. Allowed values are " + SUPPORTED_SIGNATURE_ALGORITHMS + "}]" + ); } - }, Setting.Property.NodeScope)); + }, Setting.Property.NodeScope) + ); public static final Setting.AffixSetting> RP_REQUESTED_SCOPES = Setting.affixKeySetting( - RealmSettings.realmSettingPrefix(TYPE), "rp.requested_scopes", - key -> Setting.listSetting(key, Collections.singletonList("openid"), Function.identity(), Setting.Property.NodeScope)); - public static final Setting.AffixSetting RP_CLIENT_AUTH_METHOD - = Setting.affixKeySetting(RealmSettings.realmSettingPrefix(TYPE), "rp.client_auth_method", + RealmSettings.realmSettingPrefix(TYPE), + "rp.requested_scopes", + key -> Setting.listSetting(key, Collections.singletonList("openid"), Function.identity(), Setting.Property.NodeScope) + ); + public static final Setting.AffixSetting RP_CLIENT_AUTH_METHOD = Setting.affixKeySetting( + RealmSettings.realmSettingPrefix(TYPE), + "rp.client_auth_method", key -> new Setting<>(key, "client_secret_basic", Function.identity(), v -> { if (CLIENT_AUTH_METHODS.contains(v) == false) { throw new IllegalArgumentException( - "Invalid value [" + v + "] for [" + key + "]. Allowed values are " + CLIENT_AUTH_METHODS + "}]"); + "Invalid value [" + v + "] for [" + key + "]. Allowed values are " + CLIENT_AUTH_METHODS + "}]" + ); } - }, Setting.Property.NodeScope)); - public static final Setting.AffixSetting RP_CLIENT_AUTH_JWT_SIGNATURE_ALGORITHM - = Setting.affixKeySetting(RealmSettings.realmSettingPrefix(TYPE), "rp.client_auth_jwt_signature_algorithm", + }, Setting.Property.NodeScope) + ); + public static final Setting.AffixSetting RP_CLIENT_AUTH_JWT_SIGNATURE_ALGORITHM = Setting.affixKeySetting( + RealmSettings.realmSettingPrefix(TYPE), + "rp.client_auth_jwt_signature_algorithm", key -> new Setting<>(key, "HS384", Function.identity(), v -> { if (SUPPORTED_CLIENT_AUTH_JWT_ALGORITHMS.contains(v) == false) { throw new IllegalArgumentException( - "Invalid value [" + v + "] for [" + key + "]. Allowed values are " + SUPPORTED_CLIENT_AUTH_JWT_ALGORITHMS + "}]"); + "Invalid value [" + v + "] for [" + key + "]. Allowed values are " + SUPPORTED_CLIENT_AUTH_JWT_ALGORITHMS + "}]" + ); } - }, Setting.Property.NodeScope)); - public static final Setting.AffixSetting OP_AUTHORIZATION_ENDPOINT - = Setting.affixKeySetting(RealmSettings.realmSettingPrefix(TYPE), "op.authorization_endpoint", + }, Setting.Property.NodeScope) + ); + public static final Setting.AffixSetting OP_AUTHORIZATION_ENDPOINT = Setting.affixKeySetting( + RealmSettings.realmSettingPrefix(TYPE), + "op.authorization_endpoint", key -> Setting.simpleString(key, v -> { try { new URI(v); } catch (URISyntaxException e) { throw new IllegalArgumentException("Invalid value [" + v + "] for [" + key + "]. Not a valid URI.", e); } - }, Setting.Property.NodeScope)); - public static final Setting.AffixSetting OP_TOKEN_ENDPOINT - = Setting.affixKeySetting(RealmSettings.realmSettingPrefix(TYPE), "op.token_endpoint", + }, Setting.Property.NodeScope) + ); + public static final Setting.AffixSetting OP_TOKEN_ENDPOINT = Setting.affixKeySetting( + RealmSettings.realmSettingPrefix(TYPE), + "op.token_endpoint", key -> Setting.simpleString(key, v -> { try { new URI(v); } catch (URISyntaxException e) { throw new IllegalArgumentException("Invalid value [" + v + "] for [" + key + "]. Not a valid URI.", e); } - }, Setting.Property.NodeScope)); - public static final Setting.AffixSetting OP_USERINFO_ENDPOINT - = Setting.affixKeySetting(RealmSettings.realmSettingPrefix(TYPE), "op.userinfo_endpoint", + }, Setting.Property.NodeScope) + ); + public static final Setting.AffixSetting OP_USERINFO_ENDPOINT = Setting.affixKeySetting( + RealmSettings.realmSettingPrefix(TYPE), + "op.userinfo_endpoint", key -> Setting.simpleString(key, v -> { try { new URI(v); } catch (URISyntaxException e) { throw new IllegalArgumentException("Invalid value [" + v + "] for [" + key + "]. Not a valid URI.", e); } - }, Setting.Property.NodeScope)); - public static final Setting.AffixSetting OP_ENDSESSION_ENDPOINT - = Setting.affixKeySetting(RealmSettings.realmSettingPrefix(TYPE), "op.endsession_endpoint", + }, Setting.Property.NodeScope) + ); + public static final Setting.AffixSetting OP_ENDSESSION_ENDPOINT = Setting.affixKeySetting( + RealmSettings.realmSettingPrefix(TYPE), + "op.endsession_endpoint", key -> Setting.simpleString(key, v -> { try { new URI(v); } catch (URISyntaxException e) { throw new IllegalArgumentException("Invalid value [" + v + "] for [" + key + "]. Not a valid URI.", e); } - }, Setting.Property.NodeScope)); - public static final Setting.AffixSetting OP_ISSUER - = RealmSettings.simpleString(TYPE, "op.issuer", Setting.Property.NodeScope); - public static final Setting.AffixSetting OP_JWKSET_PATH - = RealmSettings.simpleString(TYPE, "op.jwkset_path", Setting.Property.NodeScope); + }, Setting.Property.NodeScope) + ); + public static final Setting.AffixSetting OP_ISSUER = RealmSettings.simpleString(TYPE, "op.issuer", Setting.Property.NodeScope); + public static final Setting.AffixSetting OP_JWKSET_PATH = RealmSettings.simpleString( + TYPE, + "op.jwkset_path", + Setting.Property.NodeScope + ); - public static final Setting.AffixSetting ALLOWED_CLOCK_SKEW - = Setting.affixKeySetting(RealmSettings.realmSettingPrefix(TYPE), "allowed_clock_skew", - key -> Setting.timeSetting(key, TimeValue.timeValueSeconds(60), Setting.Property.NodeScope)); + public static final Setting.AffixSetting ALLOWED_CLOCK_SKEW = Setting.affixKeySetting( + RealmSettings.realmSettingPrefix(TYPE), + "allowed_clock_skew", + key -> Setting.timeSetting(key, TimeValue.timeValueSeconds(60), Setting.Property.NodeScope) + ); public static final Setting.AffixSetting POPULATE_USER_METADATA = Setting.affixKeySetting( - RealmSettings.realmSettingPrefix(TYPE), "populate_user_metadata", - key -> Setting.boolSetting(key, true, Setting.Property.NodeScope)); + RealmSettings.realmSettingPrefix(TYPE), + "populate_user_metadata", + key -> Setting.boolSetting(key, true, Setting.Property.NodeScope) + ); private static final TimeValue DEFAULT_TIMEOUT = TimeValue.timeValueSeconds(5); - public static final Setting.AffixSetting HTTP_CONNECT_TIMEOUT - = Setting.affixKeySetting(RealmSettings.realmSettingPrefix(TYPE), "http.connect_timeout", - key -> Setting.timeSetting(key, DEFAULT_TIMEOUT, Setting.Property.NodeScope)); - public static final Setting.AffixSetting HTTP_CONNECTION_READ_TIMEOUT - = Setting.affixKeySetting(RealmSettings.realmSettingPrefix(TYPE), "http.connection_read_timeout", - key -> Setting.timeSetting(key, DEFAULT_TIMEOUT, Setting.Property.NodeScope)); - public static final Setting.AffixSetting HTTP_SOCKET_TIMEOUT - = Setting.affixKeySetting(RealmSettings.realmSettingPrefix(TYPE), "http.socket_timeout", - key -> Setting.timeSetting(key, DEFAULT_TIMEOUT, Setting.Property.NodeScope)); - public static final Setting.AffixSetting HTTP_MAX_CONNECTIONS - = Setting.affixKeySetting(RealmSettings.realmSettingPrefix(TYPE), "http.max_connections", - key -> Setting.intSetting(key, 200, Setting.Property.NodeScope)); - public static final Setting.AffixSetting HTTP_MAX_ENDPOINT_CONNECTIONS - = Setting.affixKeySetting(RealmSettings.realmSettingPrefix(TYPE), "http.max_endpoint_connections", - key -> Setting.intSetting(key, 200, Setting.Property.NodeScope)); - public static final Setting.AffixSetting HTTP_PROXY_HOST - = Setting.affixKeySetting(RealmSettings.realmSettingPrefix(TYPE), "http.proxy.host", + public static final Setting.AffixSetting HTTP_CONNECT_TIMEOUT = Setting.affixKeySetting( + RealmSettings.realmSettingPrefix(TYPE), + "http.connect_timeout", + key -> Setting.timeSetting(key, DEFAULT_TIMEOUT, Setting.Property.NodeScope) + ); + public static final Setting.AffixSetting HTTP_CONNECTION_READ_TIMEOUT = Setting.affixKeySetting( + RealmSettings.realmSettingPrefix(TYPE), + "http.connection_read_timeout", + key -> Setting.timeSetting(key, DEFAULT_TIMEOUT, Setting.Property.NodeScope) + ); + public static final Setting.AffixSetting HTTP_SOCKET_TIMEOUT = Setting.affixKeySetting( + RealmSettings.realmSettingPrefix(TYPE), + "http.socket_timeout", + key -> Setting.timeSetting(key, DEFAULT_TIMEOUT, Setting.Property.NodeScope) + ); + public static final Setting.AffixSetting HTTP_MAX_CONNECTIONS = Setting.affixKeySetting( + RealmSettings.realmSettingPrefix(TYPE), + "http.max_connections", + key -> Setting.intSetting(key, 200, Setting.Property.NodeScope) + ); + public static final Setting.AffixSetting HTTP_MAX_ENDPOINT_CONNECTIONS = Setting.affixKeySetting( + RealmSettings.realmSettingPrefix(TYPE), + "http.max_endpoint_connections", + key -> Setting.intSetting(key, 200, Setting.Property.NodeScope) + ); + public static final Setting.AffixSetting HTTP_PROXY_HOST = Setting.affixKeySetting( + RealmSettings.realmSettingPrefix(TYPE), + "http.proxy.host", key -> Setting.simpleString(key, new Setting.Validator() { @Override public void validate(String value) { @@ -178,30 +233,52 @@ public void validate(String value, Map, Object> settings) { try { new HttpHost(value, port, scheme); } catch (Exception e) { - throw new IllegalArgumentException("HTTP host for hostname [" + value + "] (from [" + key + "])," + - " port [" + port + "] (from [" + portSetting.getKey() + "]) and " + - "scheme [" + scheme + "] (from ([" + schemeSetting.getKey() + "]) is invalid"); + throw new IllegalArgumentException( + "HTTP host for hostname [" + + value + + "] (from [" + + key + + "])," + + " port [" + + port + + "] (from [" + + portSetting.getKey() + + "]) and " + + "scheme [" + + scheme + + "] (from ([" + + schemeSetting.getKey() + + "]) is invalid" + ); } } @Override public Iterator> settings() { final String namespace = HTTP_PROXY_HOST.getNamespace(HTTP_PROXY_HOST.getConcreteSetting(key)); - final List> settings = List.of(HTTP_PROXY_PORT.getConcreteSettingForNamespace(namespace), - HTTP_PROXY_SCHEME.getConcreteSettingForNamespace(namespace)); + final List> settings = List.of( + HTTP_PROXY_PORT.getConcreteSettingForNamespace(namespace), + HTTP_PROXY_SCHEME.getConcreteSettingForNamespace(namespace) + ); return settings.iterator(); } - }, Setting.Property.NodeScope)); - public static final Setting.AffixSetting HTTP_PROXY_PORT - = Setting.affixKeySetting(RealmSettings.realmSettingPrefix(TYPE), "http.proxy.port", - key -> Setting.intSetting(key, 80, 1, 65535, Setting.Property.NodeScope), () -> HTTP_PROXY_HOST); - public static final Setting.AffixSetting HTTP_PROXY_SCHEME - = Setting.affixKeySetting(RealmSettings.realmSettingPrefix(TYPE), "http.proxy.scheme", + }, Setting.Property.NodeScope) + ); + public static final Setting.AffixSetting HTTP_PROXY_PORT = Setting.affixKeySetting( + RealmSettings.realmSettingPrefix(TYPE), + "http.proxy.port", + key -> Setting.intSetting(key, 80, 1, 65535, Setting.Property.NodeScope), + () -> HTTP_PROXY_HOST + ); + public static final Setting.AffixSetting HTTP_PROXY_SCHEME = Setting.affixKeySetting( + RealmSettings.realmSettingPrefix(TYPE), + "http.proxy.scheme", key -> Setting.simpleString(key, "http", value -> { if (value.equals("http") == false && value.equals("https") == false) { throw new IllegalArgumentException("Invalid value [" + value + "] for [" + key + "]. Only `http` or `https` are allowed."); } - }, Setting.Property.NodeScope)); + }, Setting.Property.NodeScope) + ); public static final ClaimSetting PRINCIPAL_CLAIM = new ClaimSetting("principal"); public static final ClaimSetting GROUPS_CLAIM = new ClaimSetting("groups"); @@ -211,12 +288,32 @@ public Iterator> settings() { public static Set> getSettings() { final Set> set = Sets.newHashSet( - RP_CLIENT_ID, RP_REDIRECT_URI, RP_RESPONSE_TYPE, RP_REQUESTED_SCOPES, RP_CLIENT_SECRET, RP_SIGNATURE_ALGORITHM, - RP_POST_LOGOUT_REDIRECT_URI, RP_CLIENT_AUTH_METHOD, RP_CLIENT_AUTH_JWT_SIGNATURE_ALGORITHM, OP_AUTHORIZATION_ENDPOINT, - OP_TOKEN_ENDPOINT, OP_USERINFO_ENDPOINT, OP_ENDSESSION_ENDPOINT, OP_ISSUER, OP_JWKSET_PATH, - POPULATE_USER_METADATA, HTTP_CONNECT_TIMEOUT, HTTP_CONNECTION_READ_TIMEOUT, - HTTP_SOCKET_TIMEOUT, HTTP_MAX_CONNECTIONS, HTTP_MAX_ENDPOINT_CONNECTIONS, HTTP_PROXY_HOST, HTTP_PROXY_PORT, - HTTP_PROXY_SCHEME, ALLOWED_CLOCK_SKEW); + RP_CLIENT_ID, + RP_REDIRECT_URI, + RP_RESPONSE_TYPE, + RP_REQUESTED_SCOPES, + RP_CLIENT_SECRET, + RP_SIGNATURE_ALGORITHM, + RP_POST_LOGOUT_REDIRECT_URI, + RP_CLIENT_AUTH_METHOD, + RP_CLIENT_AUTH_JWT_SIGNATURE_ALGORITHM, + OP_AUTHORIZATION_ENDPOINT, + OP_TOKEN_ENDPOINT, + OP_USERINFO_ENDPOINT, + OP_ENDSESSION_ENDPOINT, + OP_ISSUER, + OP_JWKSET_PATH, + POPULATE_USER_METADATA, + HTTP_CONNECT_TIMEOUT, + HTTP_CONNECTION_READ_TIMEOUT, + HTTP_SOCKET_TIMEOUT, + HTTP_MAX_CONNECTIONS, + HTTP_MAX_ENDPOINT_CONNECTIONS, + HTTP_PROXY_HOST, + HTTP_PROXY_PORT, + HTTP_PROXY_SCHEME, + ALLOWED_CLOCK_SKEW + ); set.addAll(DelegatedAuthorizationSettings.getSettings(TYPE)); set.addAll(RealmSettings.getStandardSettings(TYPE)); set.addAll(SSLConfigurationSettings.getRealmSettings(TYPE)); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/pki/PkiRealmSettings.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/pki/PkiRealmSettings.java index 589a5202b7ae7..0c9555cfcada1 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/pki/PkiRealmSettings.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/pki/PkiRealmSettings.java @@ -24,23 +24,30 @@ public final class PkiRealmSettings { public static final String TYPE = "pki"; public static final String DEFAULT_USERNAME_PATTERN = "CN=(.*?)(?:,|$)"; public static final Setting.AffixSetting USERNAME_PATTERN_SETTING = Setting.affixKeySetting( - RealmSettings.realmSettingPrefix(TYPE), "username_pattern", - key -> new Setting<>(key, DEFAULT_USERNAME_PATTERN, s -> Pattern.compile(s, Pattern.CASE_INSENSITIVE), - Setting.Property.NodeScope)); + RealmSettings.realmSettingPrefix(TYPE), + "username_pattern", + key -> new Setting<>(key, DEFAULT_USERNAME_PATTERN, s -> Pattern.compile(s, Pattern.CASE_INSENSITIVE), Setting.Property.NodeScope) + ); private static final TimeValue DEFAULT_TTL = TimeValue.timeValueMinutes(20); public static final Setting.AffixSetting CACHE_TTL_SETTING = Setting.affixKeySetting( - RealmSettings.realmSettingPrefix(TYPE), "cache.ttl", - key -> Setting.timeSetting(key, DEFAULT_TTL, Setting.Property.NodeScope)); + RealmSettings.realmSettingPrefix(TYPE), + "cache.ttl", + key -> Setting.timeSetting(key, DEFAULT_TTL, Setting.Property.NodeScope) + ); - private static final int DEFAULT_MAX_USERS = 100_000; //100k users + private static final int DEFAULT_MAX_USERS = 100_000; // 100k users public static final Setting.AffixSetting CACHE_MAX_USERS_SETTING = Setting.affixKeySetting( - RealmSettings.realmSettingPrefix(TYPE), "cache.max_users", - key -> Setting.intSetting(key, DEFAULT_MAX_USERS, Setting.Property.NodeScope)); + RealmSettings.realmSettingPrefix(TYPE), + "cache.max_users", + key -> Setting.intSetting(key, DEFAULT_MAX_USERS, Setting.Property.NodeScope) + ); public static final Setting.AffixSetting DELEGATION_ENABLED_SETTING = Setting.affixKeySetting( - RealmSettings.realmSettingPrefix(TYPE), "delegation.enabled", - key -> Setting.boolSetting(key, false, Setting.Property.NodeScope)); + RealmSettings.realmSettingPrefix(TYPE), + "delegation.enabled", + key -> Setting.boolSetting(key, false, Setting.Property.NodeScope) + ); public static final Setting.AffixSetting> TRUST_STORE_PATH; public static final Setting.AffixSetting> TRUST_STORE_TYPE; @@ -60,8 +67,7 @@ public final class PkiRealmSettings { CAPATH_SETTING = SSLConfigurationSettings.CERT_AUTH_PATH.affixSetting(prefix, ""); } - private PkiRealmSettings() { - } + private PkiRealmSettings() {} /** * @return The {@link Setting setting configuration} for this realm type diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/saml/SamlRealmSettings.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/saml/SamlRealmSettings.java index 84ab45ba0a098..e07a63a3c3d3a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/saml/SamlRealmSettings.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/saml/SamlRealmSettings.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.security.authc.saml; import org.elasticsearch.common.settings.Setting; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.set.Sets; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.xpack.core.security.authc.RealmConfig; import org.elasticsearch.xpack.core.security.authc.RealmSettings; import org.elasticsearch.xpack.core.security.authc.support.DelegatedAuthorizationSettings; @@ -29,42 +29,67 @@ public class SamlRealmSettings { // these settings will be used under the prefix xpack.security.authc.realms.REALM_NAME. private static final String IDP_METADATA_SETTING_PREFIX = "idp.metadata."; - public static final Setting.AffixSetting IDP_ENTITY_ID - = RealmSettings.simpleString(TYPE, "idp.entity_id", Setting.Property.NodeScope); + public static final Setting.AffixSetting IDP_ENTITY_ID = RealmSettings.simpleString( + TYPE, + "idp.entity_id", + Setting.Property.NodeScope + ); - public static final Setting.AffixSetting IDP_METADATA_PATH - = RealmSettings.simpleString(TYPE, IDP_METADATA_SETTING_PREFIX + "path", Setting.Property.NodeScope); + public static final Setting.AffixSetting IDP_METADATA_PATH = RealmSettings.simpleString( + TYPE, + IDP_METADATA_SETTING_PREFIX + "path", + Setting.Property.NodeScope + ); public static final Setting.AffixSetting IDP_METADATA_HTTP_REFRESH = Setting.affixKeySetting( - RealmSettings.realmSettingPrefix(TYPE), IDP_METADATA_SETTING_PREFIX + "http.refresh", - key -> Setting.timeSetting(key, TimeValue.timeValueHours(1), Setting.Property.NodeScope)); + RealmSettings.realmSettingPrefix(TYPE), + IDP_METADATA_SETTING_PREFIX + "http.refresh", + key -> Setting.timeSetting(key, TimeValue.timeValueHours(1), Setting.Property.NodeScope) + ); public static final Setting.AffixSetting IDP_SINGLE_LOGOUT = Setting.affixKeySetting( - RealmSettings.realmSettingPrefix(TYPE), "idp.use_single_logout", - key -> Setting.boolSetting(key, true, Setting.Property.NodeScope)); + RealmSettings.realmSettingPrefix(TYPE), + "idp.use_single_logout", + key -> Setting.boolSetting(key, true, Setting.Property.NodeScope) + ); - public static final Setting.AffixSetting SP_ENTITY_ID - = RealmSettings.simpleString(TYPE, "sp.entity_id", Setting.Property.NodeScope); + public static final Setting.AffixSetting SP_ENTITY_ID = RealmSettings.simpleString( + TYPE, + "sp.entity_id", + Setting.Property.NodeScope + ); public static final Setting.AffixSetting SP_ACS = RealmSettings.simpleString(TYPE, "sp.acs", Setting.Property.NodeScope); public static final Setting.AffixSetting SP_LOGOUT = RealmSettings.simpleString(TYPE, "sp.logout", Setting.Property.NodeScope); - public static final Setting.AffixSetting NAMEID_FORMAT - = RealmSettings.simpleString(TYPE, "nameid_format", Setting.Property.NodeScope); + public static final Setting.AffixSetting NAMEID_FORMAT = RealmSettings.simpleString( + TYPE, + "nameid_format", + Setting.Property.NodeScope + ); public static final Setting.AffixSetting NAMEID_ALLOW_CREATE = Setting.affixKeySetting( - RealmSettings.realmSettingPrefix(TYPE), "nameid.allow_create", - key -> Setting.boolSetting(key, false, Setting.Property.NodeScope)); - public static final Setting.AffixSetting NAMEID_SP_QUALIFIER - = RealmSettings.simpleString(TYPE, "nameid.sp_qualifier", Setting.Property.NodeScope); + RealmSettings.realmSettingPrefix(TYPE), + "nameid.allow_create", + key -> Setting.boolSetting(key, false, Setting.Property.NodeScope) + ); + public static final Setting.AffixSetting NAMEID_SP_QUALIFIER = RealmSettings.simpleString( + TYPE, + "nameid.sp_qualifier", + Setting.Property.NodeScope + ); public static final Setting.AffixSetting FORCE_AUTHN = Setting.affixKeySetting( - RealmSettings.realmSettingPrefix(TYPE), "force_authn", - key -> Setting.boolSetting(key, false, Setting.Property.NodeScope)); + RealmSettings.realmSettingPrefix(TYPE), + "force_authn", + key -> Setting.boolSetting(key, false, Setting.Property.NodeScope) + ); public static final Setting.AffixSetting POPULATE_USER_METADATA = Setting.affixKeySetting( - RealmSettings.realmSettingPrefix(TYPE), "populate_user_metadata", - key -> Setting.boolSetting(key, true, Setting.Property.NodeScope)); + RealmSettings.realmSettingPrefix(TYPE), + "populate_user_metadata", + key -> Setting.boolSetting(key, true, Setting.Property.NodeScope) + ); public static final AttributeSetting PRINCIPAL_ATTRIBUTE = new AttributeSetting("principal"); public static final AttributeSetting GROUPS_ATTRIBUTE = new AttributeSetting("groups"); @@ -74,39 +99,63 @@ public class SamlRealmSettings { public static final String ENCRYPTION_SETTING_KEY = "encryption."; public static final Setting.AffixSetting ENCRYPTION_KEY_ALIAS = RealmSettings.simpleString( - TYPE, ENCRYPTION_SETTING_KEY + "keystore.alias", Setting.Property.NodeScope); + TYPE, + ENCRYPTION_SETTING_KEY + "keystore.alias", + Setting.Property.NodeScope + ); public static final String SIGNING_SETTING_KEY = "signing."; public static final Setting.AffixSetting SIGNING_KEY_ALIAS = RealmSettings.simpleString( - TYPE, SIGNING_SETTING_KEY + "keystore.alias", Setting.Property.NodeScope); + TYPE, + SIGNING_SETTING_KEY + "keystore.alias", + Setting.Property.NodeScope + ); public static final Setting.AffixSetting> SIGNING_MESSAGE_TYPES = Setting.affixKeySetting( - RealmSettings.realmSettingPrefix(TYPE), "signing.saml_messages", - key -> Setting.listSetting(key, Collections.singletonList("*"), Function.identity(), Setting.Property.NodeScope)); + RealmSettings.realmSettingPrefix(TYPE), + "signing.saml_messages", + key -> Setting.listSetting(key, Collections.singletonList("*"), Function.identity(), Setting.Property.NodeScope) + ); public static final Setting.AffixSetting> REQUESTED_AUTHN_CONTEXT_CLASS_REF = Setting.affixKeySetting( - RealmSettings.realmSettingPrefix(TYPE), "req_authn_context_class_ref", - key -> Setting.listSetting(key, Collections.emptyList(), Function.identity(),Setting.Property.NodeScope)); + RealmSettings.realmSettingPrefix(TYPE), + "req_authn_context_class_ref", + key -> Setting.listSetting(key, Collections.emptyList(), Function.identity(), Setting.Property.NodeScope) + ); public static final Setting.AffixSetting CLOCK_SKEW = Setting.affixKeySetting( - RealmSettings.realmSettingPrefix(TYPE), "allowed_clock_skew", - key -> Setting.positiveTimeSetting(key, TimeValue.timeValueMinutes(3), Setting.Property.NodeScope)); + RealmSettings.realmSettingPrefix(TYPE), + "allowed_clock_skew", + key -> Setting.positiveTimeSetting(key, TimeValue.timeValueMinutes(3), Setting.Property.NodeScope) + ); public static final String SSL_PREFIX = "ssl."; - private SamlRealmSettings() { - } + private SamlRealmSettings() {} /** * @return The {@link Setting setting configuration} for this realm type */ public static Set> getSettings() { final Set> set = Sets.newHashSet( - IDP_ENTITY_ID, IDP_METADATA_PATH, IDP_METADATA_HTTP_REFRESH, IDP_SINGLE_LOGOUT, - SP_ENTITY_ID, SP_ACS, SP_LOGOUT, - NAMEID_FORMAT, NAMEID_ALLOW_CREATE, NAMEID_SP_QUALIFIER, FORCE_AUTHN, - POPULATE_USER_METADATA, CLOCK_SKEW, - ENCRYPTION_KEY_ALIAS, SIGNING_KEY_ALIAS, SIGNING_MESSAGE_TYPES, REQUESTED_AUTHN_CONTEXT_CLASS_REF); + IDP_ENTITY_ID, + IDP_METADATA_PATH, + IDP_METADATA_HTTP_REFRESH, + IDP_SINGLE_LOGOUT, + SP_ENTITY_ID, + SP_ACS, + SP_LOGOUT, + NAMEID_FORMAT, + NAMEID_ALLOW_CREATE, + NAMEID_SP_QUALIFIER, + FORCE_AUTHN, + POPULATE_USER_METADATA, + CLOCK_SKEW, + ENCRYPTION_KEY_ALIAS, + SIGNING_KEY_ALIAS, + SIGNING_MESSAGE_TYPES, + REQUESTED_AUTHN_CONTEXT_CLASS_REF + ); set.addAll(X509KeyPairSettings.affix(RealmSettings.realmSettingPrefix(TYPE), ENCRYPTION_SETTING_KEY, false)); set.addAll(X509KeyPairSettings.affix(RealmSettings.realmSettingPrefix(TYPE), SIGNING_SETTING_KEY, false)); set.addAll(SSLConfigurationSettings.getRealmSettings(TYPE)); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/AuthenticationContextSerializer.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/AuthenticationContextSerializer.java index 023de86226d45..b18b6091addef 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/AuthenticationContextSerializer.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/AuthenticationContextSerializer.java @@ -8,9 +8,9 @@ package org.elasticsearch.xpack.core.security.authc.support; import org.elasticsearch.Version; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xpack.core.security.authc.Authentication; import org.elasticsearch.xpack.core.security.authc.AuthenticationField; @@ -48,8 +48,7 @@ public Authentication readFromContext(ThreadContext ctx) throws IOException { return deserializeHeaderAndPutInContext(authenticationHeader, ctx); } - Authentication deserializeHeaderAndPutInContext(String headerValue, ThreadContext ctx) - throws IOException, IllegalArgumentException { + Authentication deserializeHeaderAndPutInContext(String headerValue, ThreadContext ctx) throws IOException, IllegalArgumentException { assert ctx.getTransient(contextKey) == null; Authentication authentication = decode(headerValue); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/BCrypt.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/BCrypt.java index 3846efea84113..5984dd659f5fb 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/BCrypt.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/BCrypt.java @@ -15,8 +15,8 @@ */ package org.elasticsearch.xpack.core.security.authc.support; -import org.elasticsearch.core.CharArrays; import org.elasticsearch.common.settings.SecureString; +import org.elasticsearch.core.CharArrays; import java.security.SecureRandom; @@ -73,305 +73,1252 @@ public class BCrypt { // Initial contents of key schedule private static final int P_orig[] = { - 0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344, - 0xa4093822, 0x299f31d0, 0x082efa98, 0xec4e6c89, - 0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c, - 0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917, - 0x9216d5d9, 0x8979fb1b - }; + 0x243f6a88, + 0x85a308d3, + 0x13198a2e, + 0x03707344, + 0xa4093822, + 0x299f31d0, + 0x082efa98, + 0xec4e6c89, + 0x452821e6, + 0x38d01377, + 0xbe5466cf, + 0x34e90c6c, + 0xc0ac29b7, + 0xc97c50dd, + 0x3f84d5b5, + 0xb5470917, + 0x9216d5d9, + 0x8979fb1b }; private static final int S_orig[] = { - 0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7, - 0xb8e1afed, 0x6a267e96, 0xba7c9045, 0xf12c7f99, - 0x24a19947, 0xb3916cf7, 0x0801f2e2, 0x858efc16, - 0x636920d8, 0x71574e69, 0xa458fea3, 0xf4933d7e, - 0x0d95748f, 0x728eb658, 0x718bcd58, 0x82154aee, - 0x7b54a41d, 0xc25a59b5, 0x9c30d539, 0x2af26013, - 0xc5d1b023, 0x286085f0, 0xca417918, 0xb8db38ef, - 0x8e79dcb0, 0x603a180e, 0x6c9e0e8b, 0xb01e8a3e, - 0xd71577c1, 0xbd314b27, 0x78af2fda, 0x55605c60, - 0xe65525f3, 0xaa55ab94, 0x57489862, 0x63e81440, - 0x55ca396a, 0x2aab10b6, 0xb4cc5c34, 0x1141e8ce, - 0xa15486af, 0x7c72e993, 0xb3ee1411, 0x636fbc2a, - 0x2ba9c55d, 0x741831f6, 0xce5c3e16, 0x9b87931e, - 0xafd6ba33, 0x6c24cf5c, 0x7a325381, 0x28958677, - 0x3b8f4898, 0x6b4bb9af, 0xc4bfe81b, 0x66282193, - 0x61d809cc, 0xfb21a991, 0x487cac60, 0x5dec8032, - 0xef845d5d, 0xe98575b1, 0xdc262302, 0xeb651b88, - 0x23893e81, 0xd396acc5, 0x0f6d6ff3, 0x83f44239, - 0x2e0b4482, 0xa4842004, 0x69c8f04a, 0x9e1f9b5e, - 0x21c66842, 0xf6e96c9a, 0x670c9c61, 0xabd388f0, - 0x6a51a0d2, 0xd8542f68, 0x960fa728, 0xab5133a3, - 0x6eef0b6c, 0x137a3be4, 0xba3bf050, 0x7efb2a98, - 0xa1f1651d, 0x39af0176, 0x66ca593e, 0x82430e88, - 0x8cee8619, 0x456f9fb4, 0x7d84a5c3, 0x3b8b5ebe, - 0xe06f75d8, 0x85c12073, 0x401a449f, 0x56c16aa6, - 0x4ed3aa62, 0x363f7706, 0x1bfedf72, 0x429b023d, - 0x37d0d724, 0xd00a1248, 0xdb0fead3, 0x49f1c09b, - 0x075372c9, 0x80991b7b, 0x25d479d8, 0xf6e8def7, - 0xe3fe501a, 0xb6794c3b, 0x976ce0bd, 0x04c006ba, - 0xc1a94fb6, 0x409f60c4, 0x5e5c9ec2, 0x196a2463, - 0x68fb6faf, 0x3e6c53b5, 0x1339b2eb, 0x3b52ec6f, - 0x6dfc511f, 0x9b30952c, 0xcc814544, 0xaf5ebd09, - 0xbee3d004, 0xde334afd, 0x660f2807, 0x192e4bb3, - 0xc0cba857, 0x45c8740f, 0xd20b5f39, 0xb9d3fbdb, - 0x5579c0bd, 0x1a60320a, 0xd6a100c6, 0x402c7279, - 0x679f25fe, 0xfb1fa3cc, 0x8ea5e9f8, 0xdb3222f8, - 0x3c7516df, 0xfd616b15, 0x2f501ec8, 0xad0552ab, - 0x323db5fa, 0xfd238760, 0x53317b48, 0x3e00df82, - 0x9e5c57bb, 0xca6f8ca0, 0x1a87562e, 0xdf1769db, - 0xd542a8f6, 0x287effc3, 0xac6732c6, 0x8c4f5573, - 0x695b27b0, 0xbbca58c8, 0xe1ffa35d, 0xb8f011a0, - 0x10fa3d98, 0xfd2183b8, 0x4afcb56c, 0x2dd1d35b, - 0x9a53e479, 0xb6f84565, 0xd28e49bc, 0x4bfb9790, - 0xe1ddf2da, 0xa4cb7e33, 0x62fb1341, 0xcee4c6e8, - 0xef20cada, 0x36774c01, 0xd07e9efe, 0x2bf11fb4, - 0x95dbda4d, 0xae909198, 0xeaad8e71, 0x6b93d5a0, - 0xd08ed1d0, 0xafc725e0, 0x8e3c5b2f, 0x8e7594b7, - 0x8ff6e2fb, 0xf2122b64, 0x8888b812, 0x900df01c, - 0x4fad5ea0, 0x688fc31c, 0xd1cff191, 0xb3a8c1ad, - 0x2f2f2218, 0xbe0e1777, 0xea752dfe, 0x8b021fa1, - 0xe5a0cc0f, 0xb56f74e8, 0x18acf3d6, 0xce89e299, - 0xb4a84fe0, 0xfd13e0b7, 0x7cc43b81, 0xd2ada8d9, - 0x165fa266, 0x80957705, 0x93cc7314, 0x211a1477, - 0xe6ad2065, 0x77b5fa86, 0xc75442f5, 0xfb9d35cf, - 0xebcdaf0c, 0x7b3e89a0, 0xd6411bd3, 0xae1e7e49, - 0x00250e2d, 0x2071b35e, 0x226800bb, 0x57b8e0af, - 0x2464369b, 0xf009b91e, 0x5563911d, 0x59dfa6aa, - 0x78c14389, 0xd95a537f, 0x207d5ba2, 0x02e5b9c5, - 0x83260376, 0x6295cfa9, 0x11c81968, 0x4e734a41, - 0xb3472dca, 0x7b14a94a, 0x1b510052, 0x9a532915, - 0xd60f573f, 0xbc9bc6e4, 0x2b60a476, 0x81e67400, - 0x08ba6fb5, 0x571be91f, 0xf296ec6b, 0x2a0dd915, - 0xb6636521, 0xe7b9f9b6, 0xff34052e, 0xc5855664, - 0x53b02d5d, 0xa99f8fa1, 0x08ba4799, 0x6e85076a, - 0x4b7a70e9, 0xb5b32944, 0xdb75092e, 0xc4192623, - 0xad6ea6b0, 0x49a7df7d, 0x9cee60b8, 0x8fedb266, - 0xecaa8c71, 0x699a17ff, 0x5664526c, 0xc2b19ee1, - 0x193602a5, 0x75094c29, 0xa0591340, 0xe4183a3e, - 0x3f54989a, 0x5b429d65, 0x6b8fe4d6, 0x99f73fd6, - 0xa1d29c07, 0xefe830f5, 0x4d2d38e6, 0xf0255dc1, - 0x4cdd2086, 0x8470eb26, 0x6382e9c6, 0x021ecc5e, - 0x09686b3f, 0x3ebaefc9, 0x3c971814, 0x6b6a70a1, - 0x687f3584, 0x52a0e286, 0xb79c5305, 0xaa500737, - 0x3e07841c, 0x7fdeae5c, 0x8e7d44ec, 0x5716f2b8, - 0xb03ada37, 0xf0500c0d, 0xf01c1f04, 0x0200b3ff, - 0xae0cf51a, 0x3cb574b2, 0x25837a58, 0xdc0921bd, - 0xd19113f9, 0x7ca92ff6, 0x94324773, 0x22f54701, - 0x3ae5e581, 0x37c2dadc, 0xc8b57634, 0x9af3dda7, - 0xa9446146, 0x0fd0030e, 0xecc8c73e, 0xa4751e41, - 0xe238cd99, 0x3bea0e2f, 0x3280bba1, 0x183eb331, - 0x4e548b38, 0x4f6db908, 0x6f420d03, 0xf60a04bf, - 0x2cb81290, 0x24977c79, 0x5679b072, 0xbcaf89af, - 0xde9a771f, 0xd9930810, 0xb38bae12, 0xdccf3f2e, - 0x5512721f, 0x2e6b7124, 0x501adde6, 0x9f84cd87, - 0x7a584718, 0x7408da17, 0xbc9f9abc, 0xe94b7d8c, - 0xec7aec3a, 0xdb851dfa, 0x63094366, 0xc464c3d2, - 0xef1c1847, 0x3215d908, 0xdd433b37, 0x24c2ba16, - 0x12a14d43, 0x2a65c451, 0x50940002, 0x133ae4dd, - 0x71dff89e, 0x10314e55, 0x81ac77d6, 0x5f11199b, - 0x043556f1, 0xd7a3c76b, 0x3c11183b, 0x5924a509, - 0xf28fe6ed, 0x97f1fbfa, 0x9ebabf2c, 0x1e153c6e, - 0x86e34570, 0xeae96fb1, 0x860e5e0a, 0x5a3e2ab3, - 0x771fe71c, 0x4e3d06fa, 0x2965dcb9, 0x99e71d0f, - 0x803e89d6, 0x5266c825, 0x2e4cc978, 0x9c10b36a, - 0xc6150eba, 0x94e2ea78, 0xa5fc3c53, 0x1e0a2df4, - 0xf2f74ea7, 0x361d2b3d, 0x1939260f, 0x19c27960, - 0x5223a708, 0xf71312b6, 0xebadfe6e, 0xeac31f66, - 0xe3bc4595, 0xa67bc883, 0xb17f37d1, 0x018cff28, - 0xc332ddef, 0xbe6c5aa5, 0x65582185, 0x68ab9802, - 0xeecea50f, 0xdb2f953b, 0x2aef7dad, 0x5b6e2f84, - 0x1521b628, 0x29076170, 0xecdd4775, 0x619f1510, - 0x13cca830, 0xeb61bd96, 0x0334fe1e, 0xaa0363cf, - 0xb5735c90, 0x4c70a239, 0xd59e9e0b, 0xcbaade14, - 0xeecc86bc, 0x60622ca7, 0x9cab5cab, 0xb2f3846e, - 0x648b1eaf, 0x19bdf0ca, 0xa02369b9, 0x655abb50, - 0x40685a32, 0x3c2ab4b3, 0x319ee9d5, 0xc021b8f7, - 0x9b540b19, 0x875fa099, 0x95f7997e, 0x623d7da8, - 0xf837889a, 0x97e32d77, 0x11ed935f, 0x16681281, - 0x0e358829, 0xc7e61fd6, 0x96dedfa1, 0x7858ba99, - 0x57f584a5, 0x1b227263, 0x9b83c3ff, 0x1ac24696, - 0xcdb30aeb, 0x532e3054, 0x8fd948e4, 0x6dbc3128, - 0x58ebf2ef, 0x34c6ffea, 0xfe28ed61, 0xee7c3c73, - 0x5d4a14d9, 0xe864b7e3, 0x42105d14, 0x203e13e0, - 0x45eee2b6, 0xa3aaabea, 0xdb6c4f15, 0xfacb4fd0, - 0xc742f442, 0xef6abbb5, 0x654f3b1d, 0x41cd2105, - 0xd81e799e, 0x86854dc7, 0xe44b476a, 0x3d816250, - 0xcf62a1f2, 0x5b8d2646, 0xfc8883a0, 0xc1c7b6a3, - 0x7f1524c3, 0x69cb7492, 0x47848a0b, 0x5692b285, - 0x095bbf00, 0xad19489d, 0x1462b174, 0x23820e00, - 0x58428d2a, 0x0c55f5ea, 0x1dadf43e, 0x233f7061, - 0x3372f092, 0x8d937e41, 0xd65fecf1, 0x6c223bdb, - 0x7cde3759, 0xcbee7460, 0x4085f2a7, 0xce77326e, - 0xa6078084, 0x19f8509e, 0xe8efd855, 0x61d99735, - 0xa969a7aa, 0xc50c06c2, 0x5a04abfc, 0x800bcadc, - 0x9e447a2e, 0xc3453484, 0xfdd56705, 0x0e1e9ec9, - 0xdb73dbd3, 0x105588cd, 0x675fda79, 0xe3674340, - 0xc5c43465, 0x713e38d8, 0x3d28f89e, 0xf16dff20, - 0x153e21e7, 0x8fb03d4a, 0xe6e39f2b, 0xdb83adf7, - 0xe93d5a68, 0x948140f7, 0xf64c261c, 0x94692934, - 0x411520f7, 0x7602d4f7, 0xbcf46b2e, 0xd4a20068, - 0xd4082471, 0x3320f46a, 0x43b7d4b7, 0x500061af, - 0x1e39f62e, 0x97244546, 0x14214f74, 0xbf8b8840, - 0x4d95fc1d, 0x96b591af, 0x70f4ddd3, 0x66a02f45, - 0xbfbc09ec, 0x03bd9785, 0x7fac6dd0, 0x31cb8504, - 0x96eb27b3, 0x55fd3941, 0xda2547e6, 0xabca0a9a, - 0x28507825, 0x530429f4, 0x0a2c86da, 0xe9b66dfb, - 0x68dc1462, 0xd7486900, 0x680ec0a4, 0x27a18dee, - 0x4f3ffea2, 0xe887ad8c, 0xb58ce006, 0x7af4d6b6, - 0xaace1e7c, 0xd3375fec, 0xce78a399, 0x406b2a42, - 0x20fe9e35, 0xd9f385b9, 0xee39d7ab, 0x3b124e8b, - 0x1dc9faf7, 0x4b6d1856, 0x26a36631, 0xeae397b2, - 0x3a6efa74, 0xdd5b4332, 0x6841e7f7, 0xca7820fb, - 0xfb0af54e, 0xd8feb397, 0x454056ac, 0xba489527, - 0x55533a3a, 0x20838d87, 0xfe6ba9b7, 0xd096954b, - 0x55a867bc, 0xa1159a58, 0xcca92963, 0x99e1db33, - 0xa62a4a56, 0x3f3125f9, 0x5ef47e1c, 0x9029317c, - 0xfdf8e802, 0x04272f70, 0x80bb155c, 0x05282ce3, - 0x95c11548, 0xe4c66d22, 0x48c1133f, 0xc70f86dc, - 0x07f9c9ee, 0x41041f0f, 0x404779a4, 0x5d886e17, - 0x325f51eb, 0xd59bc0d1, 0xf2bcc18f, 0x41113564, - 0x257b7834, 0x602a9c60, 0xdff8e8a3, 0x1f636c1b, - 0x0e12b4c2, 0x02e1329e, 0xaf664fd1, 0xcad18115, - 0x6b2395e0, 0x333e92e1, 0x3b240b62, 0xeebeb922, - 0x85b2a20e, 0xe6ba0d99, 0xde720c8c, 0x2da2f728, - 0xd0127845, 0x95b794fd, 0x647d0862, 0xe7ccf5f0, - 0x5449a36f, 0x877d48fa, 0xc39dfd27, 0xf33e8d1e, - 0x0a476341, 0x992eff74, 0x3a6f6eab, 0xf4f8fd37, - 0xa812dc60, 0xa1ebddf8, 0x991be14c, 0xdb6e6b0d, - 0xc67b5510, 0x6d672c37, 0x2765d43b, 0xdcd0e804, - 0xf1290dc7, 0xcc00ffa3, 0xb5390f92, 0x690fed0b, - 0x667b9ffb, 0xcedb7d9c, 0xa091cf0b, 0xd9155ea3, - 0xbb132f88, 0x515bad24, 0x7b9479bf, 0x763bd6eb, - 0x37392eb3, 0xcc115979, 0x8026e297, 0xf42e312d, - 0x6842ada7, 0xc66a2b3b, 0x12754ccc, 0x782ef11c, - 0x6a124237, 0xb79251e7, 0x06a1bbe6, 0x4bfb6350, - 0x1a6b1018, 0x11caedfa, 0x3d25bdd8, 0xe2e1c3c9, - 0x44421659, 0x0a121386, 0xd90cec6e, 0xd5abea2a, - 0x64af674e, 0xda86a85f, 0xbebfe988, 0x64e4c3fe, - 0x9dbc8057, 0xf0f7c086, 0x60787bf8, 0x6003604d, - 0xd1fd8346, 0xf6381fb0, 0x7745ae04, 0xd736fccc, - 0x83426b33, 0xf01eab71, 0xb0804187, 0x3c005e5f, - 0x77a057be, 0xbde8ae24, 0x55464299, 0xbf582e61, - 0x4e58f48f, 0xf2ddfda2, 0xf474ef38, 0x8789bdc2, - 0x5366f9c3, 0xc8b38e74, 0xb475f255, 0x46fcd9b9, - 0x7aeb2661, 0x8b1ddf84, 0x846a0e79, 0x915f95e2, - 0x466e598e, 0x20b45770, 0x8cd55591, 0xc902de4c, - 0xb90bace1, 0xbb8205d0, 0x11a86248, 0x7574a99e, - 0xb77f19b6, 0xe0a9dc09, 0x662d09a1, 0xc4324633, - 0xe85a1f02, 0x09f0be8c, 0x4a99a025, 0x1d6efe10, - 0x1ab93d1d, 0x0ba5a4df, 0xa186f20f, 0x2868f169, - 0xdcb7da83, 0x573906fe, 0xa1e2ce9b, 0x4fcd7f52, - 0x50115e01, 0xa70683fa, 0xa002b5c4, 0x0de6d027, - 0x9af88c27, 0x773f8641, 0xc3604c06, 0x61a806b5, - 0xf0177a28, 0xc0f586e0, 0x006058aa, 0x30dc7d62, - 0x11e69ed7, 0x2338ea63, 0x53c2dd94, 0xc2c21634, - 0xbbcbee56, 0x90bcb6de, 0xebfc7da1, 0xce591d76, - 0x6f05e409, 0x4b7c0188, 0x39720a3d, 0x7c927c24, - 0x86e3725f, 0x724d9db9, 0x1ac15bb4, 0xd39eb8fc, - 0xed545578, 0x08fca5b5, 0xd83d7cd3, 0x4dad0fc4, - 0x1e50ef5e, 0xb161e6f8, 0xa28514d9, 0x6c51133c, - 0x6fd5c7e7, 0x56e14ec4, 0x362abfce, 0xddc6c837, - 0xd79a3234, 0x92638212, 0x670efa8e, 0x406000e0, - 0x3a39ce37, 0xd3faf5cf, 0xabc27737, 0x5ac52d1b, - 0x5cb0679e, 0x4fa33742, 0xd3822740, 0x99bc9bbe, - 0xd5118e9d, 0xbf0f7315, 0xd62d1c7e, 0xc700c47b, - 0xb78c1b6b, 0x21a19045, 0xb26eb1be, 0x6a366eb4, - 0x5748ab2f, 0xbc946e79, 0xc6a376d2, 0x6549c2c8, - 0x530ff8ee, 0x468dde7d, 0xd5730a1d, 0x4cd04dc6, - 0x2939bbdb, 0xa9ba4650, 0xac9526e8, 0xbe5ee304, - 0xa1fad5f0, 0x6a2d519a, 0x63ef8ce2, 0x9a86ee22, - 0xc089c2b8, 0x43242ef6, 0xa51e03aa, 0x9cf2d0a4, - 0x83c061ba, 0x9be96a4d, 0x8fe51550, 0xba645bd6, - 0x2826a2f9, 0xa73a3ae1, 0x4ba99586, 0xef5562e9, - 0xc72fefd3, 0xf752f7da, 0x3f046f69, 0x77fa0a59, - 0x80e4a915, 0x87b08601, 0x9b09e6ad, 0x3b3ee593, - 0xe990fd5a, 0x9e34d797, 0x2cf0b7d9, 0x022b8b51, - 0x96d5ac3a, 0x017da67d, 0xd1cf3ed6, 0x7c7d2d28, - 0x1f9f25cf, 0xadf2b89b, 0x5ad6b472, 0x5a88f54c, - 0xe029ac71, 0xe019a5e6, 0x47b0acfd, 0xed93fa9b, - 0xe8d3c48d, 0x283b57cc, 0xf8d56629, 0x79132e28, - 0x785f0191, 0xed756055, 0xf7960e44, 0xe3d35e8c, - 0x15056dd4, 0x88f46dba, 0x03a16125, 0x0564f0bd, - 0xc3eb9e15, 0x3c9057a2, 0x97271aec, 0xa93a072a, - 0x1b3f6d9b, 0x1e6321f5, 0xf59c66fb, 0x26dcf319, - 0x7533d928, 0xb155fdf5, 0x03563482, 0x8aba3cbb, - 0x28517711, 0xc20ad9f8, 0xabcc5167, 0xccad925f, - 0x4de81751, 0x3830dc8e, 0x379d5862, 0x9320f991, - 0xea7a90c2, 0xfb3e7bce, 0x5121ce64, 0x774fbe32, - 0xa8b6e37e, 0xc3293d46, 0x48de5369, 0x6413e680, - 0xa2ae0810, 0xdd6db224, 0x69852dfd, 0x09072166, - 0xb39a460a, 0x6445c0dd, 0x586cdecf, 0x1c20c8ae, - 0x5bbef7dd, 0x1b588d40, 0xccd2017f, 0x6bb4e3bb, - 0xdda26a7e, 0x3a59ff45, 0x3e350a44, 0xbcb4cdd5, - 0x72eacea8, 0xfa6484bb, 0x8d6612ae, 0xbf3c6f47, - 0xd29be463, 0x542f5d9e, 0xaec2771b, 0xf64e6370, - 0x740e0d8d, 0xe75b1357, 0xf8721671, 0xaf537d5d, - 0x4040cb08, 0x4eb4e2cc, 0x34d2466a, 0x0115af84, - 0xe1b00428, 0x95983a1d, 0x06b89fb4, 0xce6ea048, - 0x6f3f3b82, 0x3520ab82, 0x011a1d4b, 0x277227f8, - 0x611560b1, 0xe7933fdc, 0xbb3a792b, 0x344525bd, - 0xa08839e1, 0x51ce794b, 0x2f32c9b7, 0xa01fbac9, - 0xe01cc87e, 0xbcc7d1f6, 0xcf0111c3, 0xa1e8aac7, - 0x1a908749, 0xd44fbd9a, 0xd0dadecb, 0xd50ada38, - 0x0339c32a, 0xc6913667, 0x8df9317c, 0xe0b12b4f, - 0xf79e59b7, 0x43f5bb3a, 0xf2d519ff, 0x27d9459c, - 0xbf97222c, 0x15e6fc2a, 0x0f91fc71, 0x9b941525, - 0xfae59361, 0xceb69ceb, 0xc2a86459, 0x12baa8d1, - 0xb6c1075e, 0xe3056a0c, 0x10d25065, 0xcb03a442, - 0xe0ec6e0e, 0x1698db3b, 0x4c98a0be, 0x3278e964, - 0x9f1f9532, 0xe0d392df, 0xd3a0342b, 0x8971f21e, - 0x1b0a7441, 0x4ba3348c, 0xc5be7120, 0xc37632d8, - 0xdf359f8d, 0x9b992f2e, 0xe60b6f47, 0x0fe3f11d, - 0xe54cda54, 0x1edad891, 0xce6279cf, 0xcd3e7e6f, - 0x1618b166, 0xfd2c1d05, 0x848fd2c5, 0xf6fb2299, - 0xf523f357, 0xa6327623, 0x93a83531, 0x56cccd02, - 0xacf08162, 0x5a75ebb5, 0x6e163697, 0x88d273cc, - 0xde966292, 0x81b949d0, 0x4c50901b, 0x71c65614, - 0xe6c6c7bd, 0x327a140a, 0x45e1d006, 0xc3f27b9a, - 0xc9aa53fd, 0x62a80f00, 0xbb25bfe2, 0x35bdd2f6, - 0x71126905, 0xb2040222, 0xb6cbcf7c, 0xcd769c2b, - 0x53113ec0, 0x1640e3d3, 0x38abbd60, 0x2547adf0, - 0xba38209c, 0xf746ce76, 0x77afa1c5, 0x20756060, - 0x85cbfe4e, 0x8ae88dd8, 0x7aaaf9b0, 0x4cf9aa7e, - 0x1948c25c, 0x02fb8a8c, 0x01c36ae4, 0xd6ebe1f9, - 0x90d4f869, 0xa65cdea0, 0x3f09252d, 0xc208e69f, - 0xb74e6132, 0xce77e25b, 0x578fdfe3, 0x3ac372e6 - }; + 0xd1310ba6, + 0x98dfb5ac, + 0x2ffd72db, + 0xd01adfb7, + 0xb8e1afed, + 0x6a267e96, + 0xba7c9045, + 0xf12c7f99, + 0x24a19947, + 0xb3916cf7, + 0x0801f2e2, + 0x858efc16, + 0x636920d8, + 0x71574e69, + 0xa458fea3, + 0xf4933d7e, + 0x0d95748f, + 0x728eb658, + 0x718bcd58, + 0x82154aee, + 0x7b54a41d, + 0xc25a59b5, + 0x9c30d539, + 0x2af26013, + 0xc5d1b023, + 0x286085f0, + 0xca417918, + 0xb8db38ef, + 0x8e79dcb0, + 0x603a180e, + 0x6c9e0e8b, + 0xb01e8a3e, + 0xd71577c1, + 0xbd314b27, + 0x78af2fda, + 0x55605c60, + 0xe65525f3, + 0xaa55ab94, + 0x57489862, + 0x63e81440, + 0x55ca396a, + 0x2aab10b6, + 0xb4cc5c34, + 0x1141e8ce, + 0xa15486af, + 0x7c72e993, + 0xb3ee1411, + 0x636fbc2a, + 0x2ba9c55d, + 0x741831f6, + 0xce5c3e16, + 0x9b87931e, + 0xafd6ba33, + 0x6c24cf5c, + 0x7a325381, + 0x28958677, + 0x3b8f4898, + 0x6b4bb9af, + 0xc4bfe81b, + 0x66282193, + 0x61d809cc, + 0xfb21a991, + 0x487cac60, + 0x5dec8032, + 0xef845d5d, + 0xe98575b1, + 0xdc262302, + 0xeb651b88, + 0x23893e81, + 0xd396acc5, + 0x0f6d6ff3, + 0x83f44239, + 0x2e0b4482, + 0xa4842004, + 0x69c8f04a, + 0x9e1f9b5e, + 0x21c66842, + 0xf6e96c9a, + 0x670c9c61, + 0xabd388f0, + 0x6a51a0d2, + 0xd8542f68, + 0x960fa728, + 0xab5133a3, + 0x6eef0b6c, + 0x137a3be4, + 0xba3bf050, + 0x7efb2a98, + 0xa1f1651d, + 0x39af0176, + 0x66ca593e, + 0x82430e88, + 0x8cee8619, + 0x456f9fb4, + 0x7d84a5c3, + 0x3b8b5ebe, + 0xe06f75d8, + 0x85c12073, + 0x401a449f, + 0x56c16aa6, + 0x4ed3aa62, + 0x363f7706, + 0x1bfedf72, + 0x429b023d, + 0x37d0d724, + 0xd00a1248, + 0xdb0fead3, + 0x49f1c09b, + 0x075372c9, + 0x80991b7b, + 0x25d479d8, + 0xf6e8def7, + 0xe3fe501a, + 0xb6794c3b, + 0x976ce0bd, + 0x04c006ba, + 0xc1a94fb6, + 0x409f60c4, + 0x5e5c9ec2, + 0x196a2463, + 0x68fb6faf, + 0x3e6c53b5, + 0x1339b2eb, + 0x3b52ec6f, + 0x6dfc511f, + 0x9b30952c, + 0xcc814544, + 0xaf5ebd09, + 0xbee3d004, + 0xde334afd, + 0x660f2807, + 0x192e4bb3, + 0xc0cba857, + 0x45c8740f, + 0xd20b5f39, + 0xb9d3fbdb, + 0x5579c0bd, + 0x1a60320a, + 0xd6a100c6, + 0x402c7279, + 0x679f25fe, + 0xfb1fa3cc, + 0x8ea5e9f8, + 0xdb3222f8, + 0x3c7516df, + 0xfd616b15, + 0x2f501ec8, + 0xad0552ab, + 0x323db5fa, + 0xfd238760, + 0x53317b48, + 0x3e00df82, + 0x9e5c57bb, + 0xca6f8ca0, + 0x1a87562e, + 0xdf1769db, + 0xd542a8f6, + 0x287effc3, + 0xac6732c6, + 0x8c4f5573, + 0x695b27b0, + 0xbbca58c8, + 0xe1ffa35d, + 0xb8f011a0, + 0x10fa3d98, + 0xfd2183b8, + 0x4afcb56c, + 0x2dd1d35b, + 0x9a53e479, + 0xb6f84565, + 0xd28e49bc, + 0x4bfb9790, + 0xe1ddf2da, + 0xa4cb7e33, + 0x62fb1341, + 0xcee4c6e8, + 0xef20cada, + 0x36774c01, + 0xd07e9efe, + 0x2bf11fb4, + 0x95dbda4d, + 0xae909198, + 0xeaad8e71, + 0x6b93d5a0, + 0xd08ed1d0, + 0xafc725e0, + 0x8e3c5b2f, + 0x8e7594b7, + 0x8ff6e2fb, + 0xf2122b64, + 0x8888b812, + 0x900df01c, + 0x4fad5ea0, + 0x688fc31c, + 0xd1cff191, + 0xb3a8c1ad, + 0x2f2f2218, + 0xbe0e1777, + 0xea752dfe, + 0x8b021fa1, + 0xe5a0cc0f, + 0xb56f74e8, + 0x18acf3d6, + 0xce89e299, + 0xb4a84fe0, + 0xfd13e0b7, + 0x7cc43b81, + 0xd2ada8d9, + 0x165fa266, + 0x80957705, + 0x93cc7314, + 0x211a1477, + 0xe6ad2065, + 0x77b5fa86, + 0xc75442f5, + 0xfb9d35cf, + 0xebcdaf0c, + 0x7b3e89a0, + 0xd6411bd3, + 0xae1e7e49, + 0x00250e2d, + 0x2071b35e, + 0x226800bb, + 0x57b8e0af, + 0x2464369b, + 0xf009b91e, + 0x5563911d, + 0x59dfa6aa, + 0x78c14389, + 0xd95a537f, + 0x207d5ba2, + 0x02e5b9c5, + 0x83260376, + 0x6295cfa9, + 0x11c81968, + 0x4e734a41, + 0xb3472dca, + 0x7b14a94a, + 0x1b510052, + 0x9a532915, + 0xd60f573f, + 0xbc9bc6e4, + 0x2b60a476, + 0x81e67400, + 0x08ba6fb5, + 0x571be91f, + 0xf296ec6b, + 0x2a0dd915, + 0xb6636521, + 0xe7b9f9b6, + 0xff34052e, + 0xc5855664, + 0x53b02d5d, + 0xa99f8fa1, + 0x08ba4799, + 0x6e85076a, + 0x4b7a70e9, + 0xb5b32944, + 0xdb75092e, + 0xc4192623, + 0xad6ea6b0, + 0x49a7df7d, + 0x9cee60b8, + 0x8fedb266, + 0xecaa8c71, + 0x699a17ff, + 0x5664526c, + 0xc2b19ee1, + 0x193602a5, + 0x75094c29, + 0xa0591340, + 0xe4183a3e, + 0x3f54989a, + 0x5b429d65, + 0x6b8fe4d6, + 0x99f73fd6, + 0xa1d29c07, + 0xefe830f5, + 0x4d2d38e6, + 0xf0255dc1, + 0x4cdd2086, + 0x8470eb26, + 0x6382e9c6, + 0x021ecc5e, + 0x09686b3f, + 0x3ebaefc9, + 0x3c971814, + 0x6b6a70a1, + 0x687f3584, + 0x52a0e286, + 0xb79c5305, + 0xaa500737, + 0x3e07841c, + 0x7fdeae5c, + 0x8e7d44ec, + 0x5716f2b8, + 0xb03ada37, + 0xf0500c0d, + 0xf01c1f04, + 0x0200b3ff, + 0xae0cf51a, + 0x3cb574b2, + 0x25837a58, + 0xdc0921bd, + 0xd19113f9, + 0x7ca92ff6, + 0x94324773, + 0x22f54701, + 0x3ae5e581, + 0x37c2dadc, + 0xc8b57634, + 0x9af3dda7, + 0xa9446146, + 0x0fd0030e, + 0xecc8c73e, + 0xa4751e41, + 0xe238cd99, + 0x3bea0e2f, + 0x3280bba1, + 0x183eb331, + 0x4e548b38, + 0x4f6db908, + 0x6f420d03, + 0xf60a04bf, + 0x2cb81290, + 0x24977c79, + 0x5679b072, + 0xbcaf89af, + 0xde9a771f, + 0xd9930810, + 0xb38bae12, + 0xdccf3f2e, + 0x5512721f, + 0x2e6b7124, + 0x501adde6, + 0x9f84cd87, + 0x7a584718, + 0x7408da17, + 0xbc9f9abc, + 0xe94b7d8c, + 0xec7aec3a, + 0xdb851dfa, + 0x63094366, + 0xc464c3d2, + 0xef1c1847, + 0x3215d908, + 0xdd433b37, + 0x24c2ba16, + 0x12a14d43, + 0x2a65c451, + 0x50940002, + 0x133ae4dd, + 0x71dff89e, + 0x10314e55, + 0x81ac77d6, + 0x5f11199b, + 0x043556f1, + 0xd7a3c76b, + 0x3c11183b, + 0x5924a509, + 0xf28fe6ed, + 0x97f1fbfa, + 0x9ebabf2c, + 0x1e153c6e, + 0x86e34570, + 0xeae96fb1, + 0x860e5e0a, + 0x5a3e2ab3, + 0x771fe71c, + 0x4e3d06fa, + 0x2965dcb9, + 0x99e71d0f, + 0x803e89d6, + 0x5266c825, + 0x2e4cc978, + 0x9c10b36a, + 0xc6150eba, + 0x94e2ea78, + 0xa5fc3c53, + 0x1e0a2df4, + 0xf2f74ea7, + 0x361d2b3d, + 0x1939260f, + 0x19c27960, + 0x5223a708, + 0xf71312b6, + 0xebadfe6e, + 0xeac31f66, + 0xe3bc4595, + 0xa67bc883, + 0xb17f37d1, + 0x018cff28, + 0xc332ddef, + 0xbe6c5aa5, + 0x65582185, + 0x68ab9802, + 0xeecea50f, + 0xdb2f953b, + 0x2aef7dad, + 0x5b6e2f84, + 0x1521b628, + 0x29076170, + 0xecdd4775, + 0x619f1510, + 0x13cca830, + 0xeb61bd96, + 0x0334fe1e, + 0xaa0363cf, + 0xb5735c90, + 0x4c70a239, + 0xd59e9e0b, + 0xcbaade14, + 0xeecc86bc, + 0x60622ca7, + 0x9cab5cab, + 0xb2f3846e, + 0x648b1eaf, + 0x19bdf0ca, + 0xa02369b9, + 0x655abb50, + 0x40685a32, + 0x3c2ab4b3, + 0x319ee9d5, + 0xc021b8f7, + 0x9b540b19, + 0x875fa099, + 0x95f7997e, + 0x623d7da8, + 0xf837889a, + 0x97e32d77, + 0x11ed935f, + 0x16681281, + 0x0e358829, + 0xc7e61fd6, + 0x96dedfa1, + 0x7858ba99, + 0x57f584a5, + 0x1b227263, + 0x9b83c3ff, + 0x1ac24696, + 0xcdb30aeb, + 0x532e3054, + 0x8fd948e4, + 0x6dbc3128, + 0x58ebf2ef, + 0x34c6ffea, + 0xfe28ed61, + 0xee7c3c73, + 0x5d4a14d9, + 0xe864b7e3, + 0x42105d14, + 0x203e13e0, + 0x45eee2b6, + 0xa3aaabea, + 0xdb6c4f15, + 0xfacb4fd0, + 0xc742f442, + 0xef6abbb5, + 0x654f3b1d, + 0x41cd2105, + 0xd81e799e, + 0x86854dc7, + 0xe44b476a, + 0x3d816250, + 0xcf62a1f2, + 0x5b8d2646, + 0xfc8883a0, + 0xc1c7b6a3, + 0x7f1524c3, + 0x69cb7492, + 0x47848a0b, + 0x5692b285, + 0x095bbf00, + 0xad19489d, + 0x1462b174, + 0x23820e00, + 0x58428d2a, + 0x0c55f5ea, + 0x1dadf43e, + 0x233f7061, + 0x3372f092, + 0x8d937e41, + 0xd65fecf1, + 0x6c223bdb, + 0x7cde3759, + 0xcbee7460, + 0x4085f2a7, + 0xce77326e, + 0xa6078084, + 0x19f8509e, + 0xe8efd855, + 0x61d99735, + 0xa969a7aa, + 0xc50c06c2, + 0x5a04abfc, + 0x800bcadc, + 0x9e447a2e, + 0xc3453484, + 0xfdd56705, + 0x0e1e9ec9, + 0xdb73dbd3, + 0x105588cd, + 0x675fda79, + 0xe3674340, + 0xc5c43465, + 0x713e38d8, + 0x3d28f89e, + 0xf16dff20, + 0x153e21e7, + 0x8fb03d4a, + 0xe6e39f2b, + 0xdb83adf7, + 0xe93d5a68, + 0x948140f7, + 0xf64c261c, + 0x94692934, + 0x411520f7, + 0x7602d4f7, + 0xbcf46b2e, + 0xd4a20068, + 0xd4082471, + 0x3320f46a, + 0x43b7d4b7, + 0x500061af, + 0x1e39f62e, + 0x97244546, + 0x14214f74, + 0xbf8b8840, + 0x4d95fc1d, + 0x96b591af, + 0x70f4ddd3, + 0x66a02f45, + 0xbfbc09ec, + 0x03bd9785, + 0x7fac6dd0, + 0x31cb8504, + 0x96eb27b3, + 0x55fd3941, + 0xda2547e6, + 0xabca0a9a, + 0x28507825, + 0x530429f4, + 0x0a2c86da, + 0xe9b66dfb, + 0x68dc1462, + 0xd7486900, + 0x680ec0a4, + 0x27a18dee, + 0x4f3ffea2, + 0xe887ad8c, + 0xb58ce006, + 0x7af4d6b6, + 0xaace1e7c, + 0xd3375fec, + 0xce78a399, + 0x406b2a42, + 0x20fe9e35, + 0xd9f385b9, + 0xee39d7ab, + 0x3b124e8b, + 0x1dc9faf7, + 0x4b6d1856, + 0x26a36631, + 0xeae397b2, + 0x3a6efa74, + 0xdd5b4332, + 0x6841e7f7, + 0xca7820fb, + 0xfb0af54e, + 0xd8feb397, + 0x454056ac, + 0xba489527, + 0x55533a3a, + 0x20838d87, + 0xfe6ba9b7, + 0xd096954b, + 0x55a867bc, + 0xa1159a58, + 0xcca92963, + 0x99e1db33, + 0xa62a4a56, + 0x3f3125f9, + 0x5ef47e1c, + 0x9029317c, + 0xfdf8e802, + 0x04272f70, + 0x80bb155c, + 0x05282ce3, + 0x95c11548, + 0xe4c66d22, + 0x48c1133f, + 0xc70f86dc, + 0x07f9c9ee, + 0x41041f0f, + 0x404779a4, + 0x5d886e17, + 0x325f51eb, + 0xd59bc0d1, + 0xf2bcc18f, + 0x41113564, + 0x257b7834, + 0x602a9c60, + 0xdff8e8a3, + 0x1f636c1b, + 0x0e12b4c2, + 0x02e1329e, + 0xaf664fd1, + 0xcad18115, + 0x6b2395e0, + 0x333e92e1, + 0x3b240b62, + 0xeebeb922, + 0x85b2a20e, + 0xe6ba0d99, + 0xde720c8c, + 0x2da2f728, + 0xd0127845, + 0x95b794fd, + 0x647d0862, + 0xe7ccf5f0, + 0x5449a36f, + 0x877d48fa, + 0xc39dfd27, + 0xf33e8d1e, + 0x0a476341, + 0x992eff74, + 0x3a6f6eab, + 0xf4f8fd37, + 0xa812dc60, + 0xa1ebddf8, + 0x991be14c, + 0xdb6e6b0d, + 0xc67b5510, + 0x6d672c37, + 0x2765d43b, + 0xdcd0e804, + 0xf1290dc7, + 0xcc00ffa3, + 0xb5390f92, + 0x690fed0b, + 0x667b9ffb, + 0xcedb7d9c, + 0xa091cf0b, + 0xd9155ea3, + 0xbb132f88, + 0x515bad24, + 0x7b9479bf, + 0x763bd6eb, + 0x37392eb3, + 0xcc115979, + 0x8026e297, + 0xf42e312d, + 0x6842ada7, + 0xc66a2b3b, + 0x12754ccc, + 0x782ef11c, + 0x6a124237, + 0xb79251e7, + 0x06a1bbe6, + 0x4bfb6350, + 0x1a6b1018, + 0x11caedfa, + 0x3d25bdd8, + 0xe2e1c3c9, + 0x44421659, + 0x0a121386, + 0xd90cec6e, + 0xd5abea2a, + 0x64af674e, + 0xda86a85f, + 0xbebfe988, + 0x64e4c3fe, + 0x9dbc8057, + 0xf0f7c086, + 0x60787bf8, + 0x6003604d, + 0xd1fd8346, + 0xf6381fb0, + 0x7745ae04, + 0xd736fccc, + 0x83426b33, + 0xf01eab71, + 0xb0804187, + 0x3c005e5f, + 0x77a057be, + 0xbde8ae24, + 0x55464299, + 0xbf582e61, + 0x4e58f48f, + 0xf2ddfda2, + 0xf474ef38, + 0x8789bdc2, + 0x5366f9c3, + 0xc8b38e74, + 0xb475f255, + 0x46fcd9b9, + 0x7aeb2661, + 0x8b1ddf84, + 0x846a0e79, + 0x915f95e2, + 0x466e598e, + 0x20b45770, + 0x8cd55591, + 0xc902de4c, + 0xb90bace1, + 0xbb8205d0, + 0x11a86248, + 0x7574a99e, + 0xb77f19b6, + 0xe0a9dc09, + 0x662d09a1, + 0xc4324633, + 0xe85a1f02, + 0x09f0be8c, + 0x4a99a025, + 0x1d6efe10, + 0x1ab93d1d, + 0x0ba5a4df, + 0xa186f20f, + 0x2868f169, + 0xdcb7da83, + 0x573906fe, + 0xa1e2ce9b, + 0x4fcd7f52, + 0x50115e01, + 0xa70683fa, + 0xa002b5c4, + 0x0de6d027, + 0x9af88c27, + 0x773f8641, + 0xc3604c06, + 0x61a806b5, + 0xf0177a28, + 0xc0f586e0, + 0x006058aa, + 0x30dc7d62, + 0x11e69ed7, + 0x2338ea63, + 0x53c2dd94, + 0xc2c21634, + 0xbbcbee56, + 0x90bcb6de, + 0xebfc7da1, + 0xce591d76, + 0x6f05e409, + 0x4b7c0188, + 0x39720a3d, + 0x7c927c24, + 0x86e3725f, + 0x724d9db9, + 0x1ac15bb4, + 0xd39eb8fc, + 0xed545578, + 0x08fca5b5, + 0xd83d7cd3, + 0x4dad0fc4, + 0x1e50ef5e, + 0xb161e6f8, + 0xa28514d9, + 0x6c51133c, + 0x6fd5c7e7, + 0x56e14ec4, + 0x362abfce, + 0xddc6c837, + 0xd79a3234, + 0x92638212, + 0x670efa8e, + 0x406000e0, + 0x3a39ce37, + 0xd3faf5cf, + 0xabc27737, + 0x5ac52d1b, + 0x5cb0679e, + 0x4fa33742, + 0xd3822740, + 0x99bc9bbe, + 0xd5118e9d, + 0xbf0f7315, + 0xd62d1c7e, + 0xc700c47b, + 0xb78c1b6b, + 0x21a19045, + 0xb26eb1be, + 0x6a366eb4, + 0x5748ab2f, + 0xbc946e79, + 0xc6a376d2, + 0x6549c2c8, + 0x530ff8ee, + 0x468dde7d, + 0xd5730a1d, + 0x4cd04dc6, + 0x2939bbdb, + 0xa9ba4650, + 0xac9526e8, + 0xbe5ee304, + 0xa1fad5f0, + 0x6a2d519a, + 0x63ef8ce2, + 0x9a86ee22, + 0xc089c2b8, + 0x43242ef6, + 0xa51e03aa, + 0x9cf2d0a4, + 0x83c061ba, + 0x9be96a4d, + 0x8fe51550, + 0xba645bd6, + 0x2826a2f9, + 0xa73a3ae1, + 0x4ba99586, + 0xef5562e9, + 0xc72fefd3, + 0xf752f7da, + 0x3f046f69, + 0x77fa0a59, + 0x80e4a915, + 0x87b08601, + 0x9b09e6ad, + 0x3b3ee593, + 0xe990fd5a, + 0x9e34d797, + 0x2cf0b7d9, + 0x022b8b51, + 0x96d5ac3a, + 0x017da67d, + 0xd1cf3ed6, + 0x7c7d2d28, + 0x1f9f25cf, + 0xadf2b89b, + 0x5ad6b472, + 0x5a88f54c, + 0xe029ac71, + 0xe019a5e6, + 0x47b0acfd, + 0xed93fa9b, + 0xe8d3c48d, + 0x283b57cc, + 0xf8d56629, + 0x79132e28, + 0x785f0191, + 0xed756055, + 0xf7960e44, + 0xe3d35e8c, + 0x15056dd4, + 0x88f46dba, + 0x03a16125, + 0x0564f0bd, + 0xc3eb9e15, + 0x3c9057a2, + 0x97271aec, + 0xa93a072a, + 0x1b3f6d9b, + 0x1e6321f5, + 0xf59c66fb, + 0x26dcf319, + 0x7533d928, + 0xb155fdf5, + 0x03563482, + 0x8aba3cbb, + 0x28517711, + 0xc20ad9f8, + 0xabcc5167, + 0xccad925f, + 0x4de81751, + 0x3830dc8e, + 0x379d5862, + 0x9320f991, + 0xea7a90c2, + 0xfb3e7bce, + 0x5121ce64, + 0x774fbe32, + 0xa8b6e37e, + 0xc3293d46, + 0x48de5369, + 0x6413e680, + 0xa2ae0810, + 0xdd6db224, + 0x69852dfd, + 0x09072166, + 0xb39a460a, + 0x6445c0dd, + 0x586cdecf, + 0x1c20c8ae, + 0x5bbef7dd, + 0x1b588d40, + 0xccd2017f, + 0x6bb4e3bb, + 0xdda26a7e, + 0x3a59ff45, + 0x3e350a44, + 0xbcb4cdd5, + 0x72eacea8, + 0xfa6484bb, + 0x8d6612ae, + 0xbf3c6f47, + 0xd29be463, + 0x542f5d9e, + 0xaec2771b, + 0xf64e6370, + 0x740e0d8d, + 0xe75b1357, + 0xf8721671, + 0xaf537d5d, + 0x4040cb08, + 0x4eb4e2cc, + 0x34d2466a, + 0x0115af84, + 0xe1b00428, + 0x95983a1d, + 0x06b89fb4, + 0xce6ea048, + 0x6f3f3b82, + 0x3520ab82, + 0x011a1d4b, + 0x277227f8, + 0x611560b1, + 0xe7933fdc, + 0xbb3a792b, + 0x344525bd, + 0xa08839e1, + 0x51ce794b, + 0x2f32c9b7, + 0xa01fbac9, + 0xe01cc87e, + 0xbcc7d1f6, + 0xcf0111c3, + 0xa1e8aac7, + 0x1a908749, + 0xd44fbd9a, + 0xd0dadecb, + 0xd50ada38, + 0x0339c32a, + 0xc6913667, + 0x8df9317c, + 0xe0b12b4f, + 0xf79e59b7, + 0x43f5bb3a, + 0xf2d519ff, + 0x27d9459c, + 0xbf97222c, + 0x15e6fc2a, + 0x0f91fc71, + 0x9b941525, + 0xfae59361, + 0xceb69ceb, + 0xc2a86459, + 0x12baa8d1, + 0xb6c1075e, + 0xe3056a0c, + 0x10d25065, + 0xcb03a442, + 0xe0ec6e0e, + 0x1698db3b, + 0x4c98a0be, + 0x3278e964, + 0x9f1f9532, + 0xe0d392df, + 0xd3a0342b, + 0x8971f21e, + 0x1b0a7441, + 0x4ba3348c, + 0xc5be7120, + 0xc37632d8, + 0xdf359f8d, + 0x9b992f2e, + 0xe60b6f47, + 0x0fe3f11d, + 0xe54cda54, + 0x1edad891, + 0xce6279cf, + 0xcd3e7e6f, + 0x1618b166, + 0xfd2c1d05, + 0x848fd2c5, + 0xf6fb2299, + 0xf523f357, + 0xa6327623, + 0x93a83531, + 0x56cccd02, + 0xacf08162, + 0x5a75ebb5, + 0x6e163697, + 0x88d273cc, + 0xde966292, + 0x81b949d0, + 0x4c50901b, + 0x71c65614, + 0xe6c6c7bd, + 0x327a140a, + 0x45e1d006, + 0xc3f27b9a, + 0xc9aa53fd, + 0x62a80f00, + 0xbb25bfe2, + 0x35bdd2f6, + 0x71126905, + 0xb2040222, + 0xb6cbcf7c, + 0xcd769c2b, + 0x53113ec0, + 0x1640e3d3, + 0x38abbd60, + 0x2547adf0, + 0xba38209c, + 0xf746ce76, + 0x77afa1c5, + 0x20756060, + 0x85cbfe4e, + 0x8ae88dd8, + 0x7aaaf9b0, + 0x4cf9aa7e, + 0x1948c25c, + 0x02fb8a8c, + 0x01c36ae4, + 0xd6ebe1f9, + 0x90d4f869, + 0xa65cdea0, + 0x3f09252d, + 0xc208e69f, + 0xb74e6132, + 0xce77e25b, + 0x578fdfe3, + 0x3ac372e6 }; // bcrypt IV: "OrpheanBeholderScryDoubt". The C implementation calls // this "ciphertext", but it is really plaintext or an IV. We keep // the name to make code comparison easier. - private static final int bf_crypt_ciphertext[] = { - 0x4f727068, 0x65616e42, 0x65686f6c, - 0x64657253, 0x63727944, 0x6f756274 - }; + private static final int bf_crypt_ciphertext[] = { 0x4f727068, 0x65616e42, 0x65686f6c, 0x64657253, 0x63727944, 0x6f756274 }; // Table for Base64 encoding private static final char base64_code[] = { - '.', '/', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', - 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', - 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', - 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', - 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', - '6', '7', '8', '9' - }; + '.', + '/', + 'A', + 'B', + 'C', + 'D', + 'E', + 'F', + 'G', + 'H', + 'I', + 'J', + 'K', + 'L', + 'M', + 'N', + 'O', + 'P', + 'Q', + 'R', + 'S', + 'T', + 'U', + 'V', + 'W', + 'X', + 'Y', + 'Z', + 'a', + 'b', + 'c', + 'd', + 'e', + 'f', + 'g', + 'h', + 'i', + 'j', + 'k', + 'l', + 'm', + 'n', + 'o', + 'p', + 'q', + 'r', + 's', + 't', + 'u', + 'v', + 'w', + 'x', + 'y', + 'z', + '0', + '1', + '2', + '3', + '4', + '5', + '6', + '7', + '8', + '9' }; // Table for Base64 decoding private static final byte index_64[] = { - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, 0, 1, 54, 55, - 56, 57, 58, 59, 60, 61, 62, 63, -1, -1, - -1, -1, -1, -1, -1, 2, 3, 4, 5, 6, - 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, - 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, - -1, -1, -1, -1, -1, -1, 28, 29, 30, - 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, - 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, - 51, 52, 53, -1, -1, -1, -1, -1 - }; + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + 0, + 1, + 54, + 55, + 56, + 57, + 58, + 59, + 60, + 61, + 62, + 63, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, + 15, + 16, + 17, + 18, + 19, + 20, + 21, + 22, + 23, + 24, + 25, + 26, + 27, + -1, + -1, + -1, + -1, + -1, + -1, + 28, + 29, + 30, + 31, + 32, + 33, + 34, + 35, + 36, + 37, + 38, + 39, + 40, + 41, + 42, + 43, + 44, + 45, + 46, + 47, + 48, + 49, + 50, + 51, + 52, + 53, + -1, + -1, + -1, + -1, + -1 }; // Expanded Blowfish key private int P[]; @@ -387,14 +1334,12 @@ public class BCrypt { * @return base64-encoded string * @exception IllegalArgumentException if the length is invalid */ - private static String encode_base64(byte d[], int len) - throws IllegalArgumentException { + private static String encode_base64(byte d[], int len) throws IllegalArgumentException { int off = 0; StringBuffer rs = new StringBuffer(); int c1, c2; - if (len <= 0 || len > d.length) - throw new IllegalArgumentException ("Invalid len"); + if (len <= 0 || len > d.length) throw new IllegalArgumentException("Invalid len"); while (off < len) { c1 = d[off++] & 0xff; @@ -427,9 +1372,8 @@ private static String encode_base64(byte d[], int len) * @return the decoded value of x */ private static byte char64(char x) { - if ((int)x < 0 || (int)x > index_64.length) - return -1; - return index_64[(int)x]; + if ((int) x < 0 || (int) x > index_64.length) return -1; + return index_64[(int) x]; } /** @@ -441,44 +1385,38 @@ private static byte char64(char x) { * @return an array containing the decoded bytes * @throws IllegalArgumentException if maxolen is invalid */ - private static byte[] decode_base64(String s, int maxolen) - throws IllegalArgumentException { + private static byte[] decode_base64(String s, int maxolen) throws IllegalArgumentException { StringBuffer rs = new StringBuffer(); int off = 0, slen = s.length(), olen = 0; byte ret[]; byte c1, c2, c3, c4, o; - if (maxolen <= 0) - throw new IllegalArgumentException ("Invalid maxolen"); + if (maxolen <= 0) throw new IllegalArgumentException("Invalid maxolen"); while (off < slen - 1 && olen < maxolen) { c1 = char64(s.charAt(off++)); c2 = char64(s.charAt(off++)); - if (c1 == -1 || c2 == -1) - break; - o = (byte)(c1 << 2); + if (c1 == -1 || c2 == -1) break; + o = (byte) (c1 << 2); o |= (c2 & 0x30) >> 4; - rs.append((char)o); - if (++olen >= maxolen || off >= slen) - break; + rs.append((char) o); + if (++olen >= maxolen || off >= slen) break; c3 = char64(s.charAt(off++)); - if (c3 == -1) - break; - o = (byte)((c2 & 0x0f) << 4); + if (c3 == -1) break; + o = (byte) ((c2 & 0x0f) << 4); o |= (c3 & 0x3c) >> 2; - rs.append((char)o); - if (++olen >= maxolen || off >= slen) - break; + rs.append((char) o); + if (++olen >= maxolen || off >= slen) break; c4 = char64(s.charAt(off++)); - o = (byte)((c3 & 0x03) << 6); + o = (byte) ((c3 & 0x03) << 6); o |= c4; - rs.append((char)o); + rs.append((char) o); ++olen; } ret = new byte[olen]; for (off = 0; off < olen; off++) - ret[off] = (byte)rs.charAt(off); + ret[off] = (byte) rs.charAt(off); return ret; } @@ -609,17 +1547,14 @@ private void ekskey(byte data[], byte key[]) { * @param cdata the plaintext to encrypt * @return an array containing the binary hashed password */ - public byte[] crypt_raw(byte password[], byte salt[], int log_rounds, - int cdata[]) { + public byte[] crypt_raw(byte password[], byte salt[], int log_rounds, int cdata[]) { int rounds, i, j; int clen = cdata.length; byte ret[]; - if (log_rounds < 4 || log_rounds > 30) - throw new IllegalArgumentException ("Bad number of rounds"); + if (log_rounds < 4 || log_rounds > 30) throw new IllegalArgumentException("Bad number of rounds"); rounds = 1 << log_rounds; - if (salt.length != BCRYPT_SALT_LEN) - throw new IllegalArgumentException ("Bad salt length"); + if (salt.length != BCRYPT_SALT_LEN) throw new IllegalArgumentException("Bad salt length"); init_key(); ekskey(salt, password); @@ -635,10 +1570,10 @@ public byte[] crypt_raw(byte password[], byte salt[], int log_rounds, ret = new byte[clen * 4]; for (i = 0, j = 0; i < clen; i++) { - ret[j++] = (byte)((cdata[i] >> 24) & 0xff); - ret[j++] = (byte)((cdata[i] >> 16) & 0xff); - ret[j++] = (byte)((cdata[i] >> 8) & 0xff); - ret[j++] = (byte)(cdata[i] & 0xff); + ret[j++] = (byte) ((cdata[i] >> 24) & 0xff); + ret[j++] = (byte) ((cdata[i] >> 16) & 0xff); + ret[j++] = (byte) ((cdata[i] >> 8) & 0xff); + ret[j++] = (byte) (cdata[i] & 0xff); } return ret; } @@ -657,24 +1592,20 @@ public static String hashpw(SecureString password, String salt) { BCrypt B; String real_salt; byte passwordb[], saltb[], hashed[]; - char minor = (char)0; + char minor = (char) 0; int rounds, off = 0; StringBuffer rs = new StringBuffer(); - if (salt.charAt(0) != '$' || salt.charAt(1) != '2') - throw new IllegalArgumentException ("Invalid salt version"); - if (salt.charAt(2) == '$') - off = 3; + if (salt.charAt(0) != '$' || salt.charAt(1) != '2') throw new IllegalArgumentException("Invalid salt version"); + if (salt.charAt(2) == '$') off = 3; else { minor = salt.charAt(2); - if (valid_minor(minor) == false || salt.charAt(3) != '$') - throw new IllegalArgumentException ("Invalid salt revision"); + if (valid_minor(minor) == false || salt.charAt(3) != '$') throw new IllegalArgumentException("Invalid salt revision"); off = 4; } // Extract number of rounds - if (salt.charAt(off + 2) > '$') - throw new IllegalArgumentException ("Missing salt rounds"); + if (salt.charAt(off + 2) > '$') throw new IllegalArgumentException("Missing salt rounds"); rounds = Integer.parseInt(salt.substring(off, off + 2)); real_salt = salt.substring(off + 3, off + 25); @@ -706,24 +1637,19 @@ public static String hashpw(SecureString password, String salt) { saltb = decode_base64(real_salt, BCRYPT_SALT_LEN); B = new BCrypt(); - hashed = B.crypt_raw(passwordb, saltb, rounds, - bf_crypt_ciphertext.clone()); + hashed = B.crypt_raw(passwordb, saltb, rounds, bf_crypt_ciphertext.clone()); rs.append("$2"); - if (minor >= 'a') - rs.append(minor); + if (minor >= 'a') rs.append(minor); rs.append("$"); - if (rounds < 10) - rs.append("0"); + if (rounds < 10) rs.append("0"); if (rounds > 30) { - throw new IllegalArgumentException( - "rounds exceeds maximum (30)"); + throw new IllegalArgumentException("rounds exceeds maximum (30)"); } rs.append(Integer.toString(rounds)); rs.append("$"); rs.append(encode_base64(saltb, saltb.length)); - rs.append(encode_base64(hashed, - bf_crypt_ciphertext.length * 4 - 1)); + rs.append(encode_base64(hashed, bf_crypt_ciphertext.length * 4 - 1)); return rs.toString(); } @@ -753,11 +1679,9 @@ public static String gensalt(int log_rounds, SecureRandom random) { random.nextBytes(rnd); rs.append("$2a$"); - if (log_rounds < 10) - rs.append("0"); + if (log_rounds < 10) rs.append("0"); if (log_rounds > 30) { - throw new IllegalArgumentException( - "log_rounds exceeds maximum (30)"); + throw new IllegalArgumentException("log_rounds exceeds maximum (30)"); } rs.append(Integer.toString(log_rounds)); rs.append("$"); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/CachingUsernamePasswordRealmSettings.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/CachingUsernamePasswordRealmSettings.java index 065ec2bcd6d6b..ecfd6efbcf7bc 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/CachingUsernamePasswordRealmSettings.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/CachingUsernamePasswordRealmSettings.java @@ -19,33 +19,42 @@ public final class CachingUsernamePasswordRealmSettings { private static final String CACHE_HASH_ALGO_SUFFIX = "cache.hash_algo"; public static final Function> CACHE_HASH_ALGO_SETTING = RealmSettings.affixSetting( - CACHE_HASH_ALGO_SUFFIX, key -> Setting.simpleString(key, "ssha256", Setting.Property.NodeScope)); + CACHE_HASH_ALGO_SUFFIX, + key -> Setting.simpleString(key, "ssha256", Setting.Property.NodeScope) + ); private static final TimeValue DEFAULT_TTL = TimeValue.timeValueMinutes(20); private static final String CACHE_TTL_SUFFIX = "cache.ttl"; public static final Function> CACHE_TTL_SETTING = RealmSettings.affixSetting( - CACHE_TTL_SUFFIX, key -> Setting.timeSetting(key, DEFAULT_TTL, Setting.Property.NodeScope)); + CACHE_TTL_SUFFIX, + key -> Setting.timeSetting(key, DEFAULT_TTL, Setting.Property.NodeScope) + ); - private static final int DEFAULT_MAX_USERS = 100_000; //100k users + private static final int DEFAULT_MAX_USERS = 100_000; // 100k users private static final String CACHE_MAX_USERS_SUFFIX = "cache.max_users"; public static final Function> CACHE_MAX_USERS_SETTING = RealmSettings.affixSetting( - CACHE_MAX_USERS_SUFFIX, key -> Setting.intSetting(key, DEFAULT_MAX_USERS, Setting.Property.NodeScope)); + CACHE_MAX_USERS_SUFFIX, + key -> Setting.intSetting(key, DEFAULT_MAX_USERS, Setting.Property.NodeScope) + ); public static final Function> AUTHC_ENABLED_SETTING = RealmSettings.affixSetting( - "authentication.enabled", key -> Setting.boolSetting(key, true, Setting.Property.NodeScope)); + "authentication.enabled", + key -> Setting.boolSetting(key, true, Setting.Property.NodeScope) + ); - private CachingUsernamePasswordRealmSettings() { - } + private CachingUsernamePasswordRealmSettings() {} /** * Returns the {@link Setting setting configuration} that is common for all caching realms */ public static Set> getSettings(String type) { - return new HashSet<>(Arrays.asList( + return new HashSet<>( + Arrays.asList( CACHE_HASH_ALGO_SETTING.apply(type), CACHE_TTL_SETTING.apply(type), CACHE_MAX_USERS_SETTING.apply(type), AUTHC_ENABLED_SETTING.apply(type) - )); + ) + ); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/DelegatedAuthorizationSettings.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/DelegatedAuthorizationSettings.java index 2c1b6a4525a0e..1f669ab171a11 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/DelegatedAuthorizationSettings.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/DelegatedAuthorizationSettings.java @@ -22,7 +22,9 @@ public class DelegatedAuthorizationSettings { public static final String AUTHZ_REALMS_SUFFIX = "authorization_realms"; public static final Function>> AUTHZ_REALMS = RealmSettings.affixSetting( - AUTHZ_REALMS_SUFFIX, key -> Setting.listSetting(key, Collections.emptyList(), Function.identity(), Setting.Property.NodeScope)); + AUTHZ_REALMS_SUFFIX, + key -> Setting.listSetting(key, Collections.emptyList(), Function.identity(), Setting.Property.NodeScope) + ); public static Collection> getSettings(String realmType) { return Collections.singleton(AUTHZ_REALMS.apply(realmType)); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/DnRoleMapperSettings.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/DnRoleMapperSettings.java index 261e4acfa0ebc..9442302c497d2 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/DnRoleMapperSettings.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/DnRoleMapperSettings.java @@ -17,14 +17,19 @@ public final class DnRoleMapperSettings { private static final String DEFAULT_FILE_NAME = "role_mapping.yml"; public static final String FILES_ROLE_MAPPING_SUFFIX = "files.role_mapping"; - public static final Function> ROLE_MAPPING_FILE_SETTING = type -> - Setting.affixKeySetting(RealmSettings.realmSettingPrefix(type), FILES_ROLE_MAPPING_SUFFIX, - key -> new Setting<>(key, DEFAULT_FILE_NAME, Function.identity(), Setting.Property.NodeScope)); + public static final Function> ROLE_MAPPING_FILE_SETTING = type -> Setting.affixKeySetting( + RealmSettings.realmSettingPrefix(type), + FILES_ROLE_MAPPING_SUFFIX, + key -> new Setting<>(key, DEFAULT_FILE_NAME, Function.identity(), Setting.Property.NodeScope) + ); public static final String UNMAPPED_GROUPS_AS_ROLES_SUFFIX = "unmapped_groups_as_roles"; - public static final Function> USE_UNMAPPED_GROUPS_AS_ROLES_SETTING = type -> - Setting.affixKeySetting(RealmSettings.realmSettingPrefix(type), UNMAPPED_GROUPS_AS_ROLES_SUFFIX, - key -> Setting.boolSetting(key, false, Setting.Property.NodeScope)); + public static final Function> USE_UNMAPPED_GROUPS_AS_ROLES_SETTING = type -> Setting + .affixKeySetting( + RealmSettings.realmSettingPrefix(type), + UNMAPPED_GROUPS_AS_ROLES_SUFFIX, + key -> Setting.boolSetting(key, false, Setting.Property.NodeScope) + ); public static Collection> getSettings(String realmType) { return Arrays.asList(USE_UNMAPPED_GROUPS_AS_ROLES_SETTING.apply(realmType), ROLE_MAPPING_FILE_SETTING.apply(realmType)); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/Hasher.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/Hasher.java index 5cd510519d365..b5c8b541226e8 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/Hasher.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/Hasher.java @@ -7,13 +7,11 @@ package org.elasticsearch.xpack.core.security.authc.support; import org.elasticsearch.ElasticsearchException; -import org.elasticsearch.core.CharArrays; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.common.hash.MessageDigests; import org.elasticsearch.common.settings.SecureString; +import org.elasticsearch.core.CharArrays; +import org.elasticsearch.core.SuppressForbidden; -import javax.crypto.SecretKeyFactory; -import javax.crypto.spec.PBEKeySpec; import java.nio.CharBuffer; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -25,6 +23,9 @@ import java.util.Locale; import java.util.stream.Collectors; +import javax.crypto.SecretKeyFactory; +import javax.crypto.spec.PBEKeySpec; + public enum Hasher { BCRYPT() { @@ -656,10 +657,15 @@ private static boolean verifyPbkdf2Hash(SecureString data, char[] hash, String p saltChars = Arrays.copyOfRange(hash, hash.length - (2 * tokenLength + 1), hash.length - (tokenLength + 1)); int cost = Integer.parseInt(new String(Arrays.copyOfRange(hash, prefix.length(), hash.length - (2 * tokenLength + 2)))); SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2withHMACSHA512"); - PBEKeySpec keySpec = new PBEKeySpec(data.getChars(), Base64.getDecoder().decode(CharArrays.toUtf8Bytes(saltChars)), - cost, PBKDF2_KEY_LENGTH); - computedPwdHash = CharArrays.utf8BytesToChars(Base64.getEncoder() - .encode(secretKeyFactory.generateSecret(keySpec).getEncoded())); + PBEKeySpec keySpec = new PBEKeySpec( + data.getChars(), + Base64.getDecoder().decode(CharArrays.toUtf8Bytes(saltChars)), + cost, + PBKDF2_KEY_LENGTH + ); + computedPwdHash = CharArrays.utf8BytesToChars( + Base64.getEncoder().encode(secretKeyFactory.generateSecret(keySpec).getEncoded()) + ); final boolean result = CharArrays.constantTimeEquals(computedPwdHash, hashChars); return result; } catch (InvalidKeySpecException | NoSuchAlgorithmException e) { @@ -696,7 +702,9 @@ private static boolean verifyBcryptHash(SecureString text, char[] hash) { */ @SuppressForbidden(reason = "This is the only allowed way to get available values") public static List getAvailableAlgoStoredHash() { - return Arrays.stream(Hasher.values()).map(Hasher::name).map(name -> name.toLowerCase(Locale.ROOT)) + return Arrays.stream(Hasher.values()) + .map(Hasher::name) + .map(name -> name.toLowerCase(Locale.ROOT)) .filter(name -> (name.startsWith("pbkdf2") || name.startsWith("bcrypt"))) .collect(Collectors.toList()); } @@ -708,7 +716,9 @@ public static List getAvailableAlgoStoredHash() { */ @SuppressForbidden(reason = "This is the only allowed way to get available values") public static List getAvailableAlgoCacheHash() { - return Arrays.stream(Hasher.values()).map(Hasher::name).map(name -> name.toLowerCase(Locale.ROOT)) + return Arrays.stream(Hasher.values()) + .map(Hasher::name) + .map(name -> name.toLowerCase(Locale.ROOT)) .filter(name -> (name.equals("sha256") == false)) .collect(Collectors.toList()); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/SecondaryAuthentication.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/SecondaryAuthentication.java index f65a71df6b398..0fccacb23d049 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/SecondaryAuthentication.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/SecondaryAuthentication.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.security.authc.support; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xpack.core.security.SecurityContext; import org.elasticsearch.xpack.core.security.authc.Authentication; import org.elasticsearch.xpack.core.security.user.User; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/TokensInvalidationResult.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/TokensInvalidationResult.java index 1432d1d5f99c2..96e805f86ad4d 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/TokensInvalidationResult.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/TokensInvalidationResult.java @@ -9,13 +9,13 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.Version; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.rest.RestStatus; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.rest.RestStatus; import java.io.IOException; import java.util.Collections; @@ -37,8 +37,12 @@ public class TokensInvalidationResult implements ToXContentObject, Writeable { private final List errors; private RestStatus restStatus; - public TokensInvalidationResult(List invalidatedTokens, List previouslyInvalidatedTokens, - @Nullable List errors, RestStatus restStatus) { + public TokensInvalidationResult( + List invalidatedTokens, + List previouslyInvalidatedTokens, + @Nullable List errors, + RestStatus restStatus + ) { Objects.requireNonNull(invalidatedTokens, "invalidated_tokens must be provided"); this.invalidatedTokens = invalidatedTokens; Objects.requireNonNull(previouslyInvalidatedTokens, "previously_invalidated_tokens must be provided"); @@ -67,7 +71,6 @@ public static TokensInvalidationResult emptyResult(RestStatus restStatus) { return new TokensInvalidationResult(Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), restStatus); } - public List getInvalidatedTokens() { return invalidatedTokens; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/UserRoleMapper.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/UserRoleMapper.java index 64a131b84145c..56d045d119b97 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/UserRoleMapper.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/UserRoleMapper.java @@ -9,6 +9,7 @@ import com.unboundid.ldap.sdk.DN; import com.unboundid.ldap.sdk.LDAPException; import com.unboundid.util.LDAPSDKUsageException; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.message.ParameterizedMessage; @@ -57,8 +58,7 @@ class UserData { private final Map metadata; private final RealmConfig realm; - public UserData(String username, @Nullable String dn, Collection groups, - Map metadata, RealmConfig realm) { + public UserData(String username, @Nullable String dn, Collection groups, Map metadata, RealmConfig realm) { this.username = username; this.dn = dn; this.groups = Set.copyOf(groups); @@ -80,8 +80,10 @@ public ExpressionModel asModel() { // null dn fields get the default NULL_PREDICATE model.defineField("dn", dn, new DistinguishedNamePredicate(dn)); } - model.defineField("groups", groups, groups.stream() - .>map(DistinguishedNamePredicate::new) + model.defineField( + "groups", + groups, + groups.stream().>map(DistinguishedNamePredicate::new) .reduce(Predicate::or) .orElse(fieldValue -> false) ); @@ -92,13 +94,18 @@ public ExpressionModel asModel() { @Override public String toString() { - return "UserData{" + - "username:" + username + - "; dn:" + dn + - "; groups:" + groups + - "; metadata:" + metadata + - "; realm=" + realm.name() + - '}'; + return "UserData{" + + "username:" + + username + + "; dn:" + + dn + + "; groups:" + + groups + + "; metadata:" + + metadata + + "; realm=" + + realm.name() + + '}'; } /** @@ -203,7 +210,10 @@ public boolean test(FieldExpression.FieldValue fieldValue) { return false; } - assert fieldValue.getValue() instanceof String : "FieldValue " + fieldValue + " has automaton but value is " + assert fieldValue.getValue() instanceof String + : "FieldValue " + + fieldValue + + " has automaton but value is " + (fieldValue.getValue() == null ? "" : fieldValue.getValue().getClass()); String pattern = (String) fieldValue.getValue(); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/UsernamePasswordToken.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/UsernamePasswordToken.java index df9792699ad77..a60a3783f355f 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/UsernamePasswordToken.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/UsernamePasswordToken.java @@ -6,10 +6,10 @@ */ package org.elasticsearch.xpack.core.security.authc.support; -import org.elasticsearch.core.CharArrays; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.CharArrays; import org.elasticsearch.xpack.core.security.authc.AuthenticationToken; import java.nio.CharBuffer; @@ -40,7 +40,7 @@ public static String basicAuthHeaderValue(String username, SecureString passwd) chars.put(username).put(':').put(passwd.getChars()); charBytes = CharArrays.toUtf8Bytes(chars.array()); - //TODO we still have passwords in Strings in headers. Maybe we can look into using a CharSequence? + // TODO we still have passwords in Strings in headers. Maybe we can look into using a CharSequence? String basicToken = Base64.getEncoder().encodeToString(charBytes); return "Basic " + basicToken; } finally { @@ -73,8 +73,7 @@ public boolean equals(Object o) { UsernamePasswordToken that = (UsernamePasswordToken) o; - return Objects.equals(password, that.password) && - Objects.equals(username, that.username); + return Objects.equals(password, that.password) && Objects.equals(username, that.username); } @Override @@ -91,8 +90,7 @@ private static UsernamePasswordToken extractToken(String headerValue) { if (Strings.isNullOrEmpty(headerValue)) { return null; } - if (headerValue.regionMatches(IGNORE_CASE_AUTH_HEADER_MATCH, 0, BASIC_AUTH_PREFIX, 0, - BASIC_AUTH_PREFIX.length()) == false) { + if (headerValue.regionMatches(IGNORE_CASE_AUTH_HEADER_MATCH, 0, BASIC_AUTH_PREFIX, 0, BASIC_AUTH_PREFIX.length()) == false) { // the header does not start with 'Basic ' so we cannot use it, but it may be valid for another realm return null; } @@ -115,8 +113,9 @@ private static UsernamePasswordToken extractToken(String headerValue) { } return new UsernamePasswordToken( - new String(Arrays.copyOfRange(userpasswd, 0, i)), - new SecureString(Arrays.copyOfRange(userpasswd, i + 1, userpasswd.length))); + new String(Arrays.copyOfRange(userpasswd, 0, i)), + new SecureString(Arrays.copyOfRange(userpasswd, i + 1, userpasswd.length)) + ); } public static void putTokenHeader(ThreadContext context, UsernamePasswordToken token) { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/mapper/ExpressionRoleMapping.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/mapper/ExpressionRoleMapping.java index 0042bd0312cd1..8d0e9c972a96a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/mapper/ExpressionRoleMapping.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/mapper/ExpressionRoleMapping.java @@ -7,7 +7,6 @@ package org.elasticsearch.xpack.core.security.authc.support.mapper; import org.elasticsearch.Version; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; @@ -15,14 +14,15 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.script.ScriptService; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ObjectParser; import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.script.ScriptService; import org.elasticsearch.xpack.core.security.authc.support.mapper.expressiondsl.ExpressionModel; import org.elasticsearch.xpack.core.security.authc.support.mapper.expressiondsl.ExpressionParser; import org.elasticsearch.xpack.core.security.authc.support.mapper.expressiondsl.RoleMapperExpression; @@ -64,8 +64,7 @@ public class ExpressionRoleMapping implements ToXContentObject, Writeable { PARSER.declareField(Builder::rules, ExpressionParser::parseObject, Fields.RULES, ValueType.OBJECT); PARSER.declareField(Builder::metadata, XContentParser::map, Fields.METADATA, ValueType.OBJECT); PARSER.declareBoolean(Builder::enabled, Fields.ENABLED); - BiConsumer ignored = (b, v) -> { - }; + BiConsumer ignored = (b, v) -> {}; // skip the doc_type and type fields in case we're parsing directly from the index PARSER.declareString(ignored, new ParseField(NativeRoleMappingStoreField.DOC_TYPE_FIELD)); PARSER.declareString(ignored, new ParseField(UPGRADE_API_TYPE_FIELD)); @@ -74,12 +73,18 @@ public class ExpressionRoleMapping implements ToXContentObject, Writeable { private final String name; private final RoleMapperExpression expression; private final List roles; - private final List roleTemplates ; + private final List roleTemplates; private final Map metadata; private final boolean enabled; - public ExpressionRoleMapping(String name, RoleMapperExpression expr, List roles, List templates, - Map metadata, boolean enabled) { + public ExpressionRoleMapping( + String name, + RoleMapperExpression expr, + List roles, + List templates, + Map metadata, + boolean enabled + ) { this.name = name; this.expression = expr; this.roles = roles == null ? Collections.emptyList() : roles; @@ -169,7 +174,6 @@ public String toString() { return getClass().getSimpleName() + "<" + name + " ; " + roles + "/" + roleTemplates + " = " + Strings.toString(expression) + ">"; } - @Override public boolean equals(Object o) { if (this == o) { @@ -179,12 +183,12 @@ public boolean equals(Object o) { return false; } final ExpressionRoleMapping that = (ExpressionRoleMapping) o; - return this.enabled == that.enabled && - Objects.equals(this.name, that.name) && - Objects.equals(this.expression, that.expression) && - Objects.equals(this.roles, that.roles) && - Objects.equals(this.roleTemplates, that.roleTemplates) && - Objects.equals(this.metadata, that.metadata); + return this.enabled == that.enabled + && Objects.equals(this.name, that.name) + && Objects.equals(this.expression, that.expression) + && Objects.equals(this.roles, that.roles) + && Objects.equals(this.roleTemplates, that.roleTemplates) + && Objects.equals(this.metadata, that.metadata); } @Override @@ -197,9 +201,10 @@ public int hashCode() { */ public static ExpressionRoleMapping parse(String name, BytesReference source, XContentType xContentType) throws IOException { final NamedXContentRegistry registry = NamedXContentRegistry.EMPTY; - try (InputStream stream = source.streamInput(); - XContentParser parser = xContentType.xContent() - .createParser(registry, LoggingDeprecationHandler.INSTANCE, stream)) { + try ( + InputStream stream = source.streamInput(); + XContentParser parser = xContentType.xContent().createParser(registry, LoggingDeprecationHandler.INSTANCE, stream) + ) { return parse(name, parser); } } @@ -254,10 +259,8 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params, boolea } public Set getRoleNames(ScriptService scriptService, ExpressionModel model) { - return Stream.concat(this.roles.stream(), - this.roleTemplates.stream() - .flatMap(r -> r.getRoleNames(scriptService, model).stream()) - ).collect(Collectors.toSet()); + return Stream.concat(this.roles.stream(), this.roleTemplates.stream().flatMap(r -> r.getRoleNames(scriptService, model).stream())) + .collect(Collectors.toSet()); } /** diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/mapper/TemplateRoleName.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/mapper/TemplateRoleName.java index f2d1424888988..efe0e10eb6812 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/mapper/TemplateRoleName.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/mapper/TemplateRoleName.java @@ -7,27 +7,27 @@ package org.elasticsearch.xpack.core.security.authc.support.mapper; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.script.Script; +import org.elasticsearch.script.ScriptService; +import org.elasticsearch.script.TemplateScript; +import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentParseException; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.script.Script; -import org.elasticsearch.script.ScriptService; -import org.elasticsearch.script.TemplateScript; import org.elasticsearch.xpack.core.security.authc.support.mapper.expressiondsl.ExpressionModel; import org.elasticsearch.xpack.core.security.support.MustacheTemplateEvaluator; @@ -50,7 +50,10 @@ public class TemplateRoleName implements ToXContentObject, Writeable { private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "role-mapping-template", false, arr -> new TemplateRoleName((BytesReference) arr[0], (Format) arr[1])); + "role-mapping-template", + false, + arr -> new TemplateRoleName((BytesReference) arr[0], (Format) arr[1]) + ); static { PARSER.declareField(constructorArg(), TemplateRoleName::extractTemplate, Fields.TEMPLATE, ObjectParser.ValueType.OBJECT_OR_STRING); @@ -103,7 +106,11 @@ public List getRoleNames(ScriptService scriptService, ExpressionModel mo public void validate(ScriptService scriptService) { try { final XContentParser parser = XContentHelper.createParser( - NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, template, XContentType.JSON); + NamedXContentRegistry.EMPTY, + LoggingDeprecationHandler.INSTANCE, + template, + XContentType.JSON + ); final Script script = MustacheTemplateEvaluator.parseForScript(parser, Collections.emptyMap()); final TemplateScript compiledTemplate = scriptService.compile(script, TemplateScript.CONTEXT).newInstance(script.getParams()); if ("mustache".equals(script.getLang())) { @@ -119,8 +126,8 @@ public void validate(ScriptService scriptService) { } private List convertJsonToList(String evaluation) throws IOException { - final XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(NamedXContentRegistry.EMPTY, - LoggingDeprecationHandler.INSTANCE, evaluation); + final XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, evaluation); XContentParser.Token token = parser.currentToken(); if (token == null) { token = parser.nextToken(); @@ -128,25 +135,27 @@ private List convertJsonToList(String evaluation) throws IOException { if (token == XContentParser.Token.VALUE_STRING) { return Collections.singletonList(parser.text()); } else if (token == XContentParser.Token.START_ARRAY) { - return parser.list().stream() - .filter(Objects::nonNull) - .map(o -> { - if (o instanceof String) { - return (String) o; - } else { - throw new XContentParseException( - "Roles array may only contain strings but found [" + o.getClass().getName() + "] [" + o + "]"); - } - }).collect(Collectors.toList()); + return parser.list().stream().filter(Objects::nonNull).map(o -> { + if (o instanceof String) { + return (String) o; + } else { + throw new XContentParseException( + "Roles array may only contain strings but found [" + o.getClass().getName() + "] [" + o + "]" + ); + } + }).collect(Collectors.toList()); } else { - throw new XContentParseException( - "Roles template must generate a string or an array of strings, but found [" + token + "]"); + throw new XContentParseException("Roles template must generate a string or an array of strings, but found [" + token + "]"); } } private String parseTemplate(ScriptService scriptService, Map parameters) throws IOException { final XContentParser parser = XContentHelper.createParser( - NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, template, XContentType.JSON); + NamedXContentRegistry.EMPTY, + LoggingDeprecationHandler.INSTANCE, + template, + XContentType.JSON + ); return MustacheTemplateEvaluator.evaluate(scriptService, parser, parameters); } @@ -191,8 +200,7 @@ public boolean equals(Object o) { return false; } final TemplateRoleName that = (TemplateRoleName) o; - return Objects.equals(this.template, that.template) && - this.format == that.format; + return Objects.equals(this.template, that.template) && this.format == that.format; } @Override @@ -206,21 +214,26 @@ private interface Fields { } public enum Format { - JSON, STRING; + JSON, + STRING; private static Format fromXContent(XContentParser parser) throws IOException { final XContentParser.Token token = parser.currentToken(); if (token != XContentParser.Token.VALUE_STRING) { - throw new XContentParseException(parser.getTokenLocation(), - "Expected [" + XContentParser.Token.VALUE_STRING + "] but found [" + token + "]"); + throw new XContentParseException( + parser.getTokenLocation(), + "Expected [" + XContentParser.Token.VALUE_STRING + "] but found [" + token + "]" + ); } final String text = parser.text(); try { return Format.valueOf(text.toUpperCase(Locale.ROOT)); } catch (IllegalArgumentException e) { String valueNames = Stream.of(values()).map(Format::formatName).collect(Collectors.joining(",")); - throw new XContentParseException(parser.getTokenLocation(), - "Invalid format [" + text + "] expected one of [" + valueNames + "]"); + throw new XContentParseException( + parser.getTokenLocation(), + "Invalid format [" + text + "] expected one of [" + valueNames + "]" + ); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/mapper/expressiondsl/AllExpression.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/mapper/expressiondsl/AllExpression.java index f8a8745812eec..001b0a8472d9a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/mapper/expressiondsl/AllExpression.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/mapper/expressiondsl/AllExpression.java @@ -6,14 +6,14 @@ */ package org.elasticsearch.xpack.core.security.authc.support.mapper.expressiondsl; -import java.io.IOException; -import java.util.Collections; -import java.util.List; - import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.XContentBuilder; +import java.io.IOException; +import java.util.Collections; +import java.util.List; + /** * An expression that evaluates to true if-and-only-if all its children * evaluate to true. diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/mapper/expressiondsl/AnyExpression.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/mapper/expressiondsl/AnyExpression.java index 1a409f9bd2d07..97880fc53c12a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/mapper/expressiondsl/AnyExpression.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/mapper/expressiondsl/AnyExpression.java @@ -6,14 +6,14 @@ */ package org.elasticsearch.xpack.core.security.authc.support.mapper.expressiondsl; -import java.io.IOException; -import java.util.Collections; -import java.util.List; - import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.XContentBuilder; +import java.io.IOException; +import java.util.Collections; +import java.util.List; + /** * An expression that evaluates to true if at least one of its children * evaluate to true. diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/mapper/expressiondsl/ExpressionModel.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/mapper/expressiondsl/ExpressionModel.java index c7d10732345a6..acaf10171903b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/mapper/expressiondsl/ExpressionModel.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/mapper/expressiondsl/ExpressionModel.java @@ -63,11 +63,17 @@ public boolean test(String field, List values) { final Predicate predicate = this.fieldPredicates.getOrDefault(field, NULL_PREDICATE); boolean isMatch = values.stream().anyMatch(predicate); if (isMatch == false && predicate == NULL_PREDICATE && fieldPredicates.containsKey(field) == false) { - logger.debug(() -> new ParameterizedMessage("Attempt to test field [{}] against value(s) [{}]," + - " but the field [{}] does not have a value on this object;" + - " known fields are [{}]", - field, Strings.collectionToCommaDelimitedString(values), - field, Strings.collectionToCommaDelimitedString(fieldPredicates.keySet()))); + logger.debug( + () -> new ParameterizedMessage( + "Attempt to test field [{}] against value(s) [{}]," + + " but the field [{}] does not have a value on this object;" + + " known fields are [{}]", + field, + Strings.collectionToCommaDelimitedString(values), + field, + Strings.collectionToCommaDelimitedString(fieldPredicates.keySet()) + ) + ); } return isMatch; @@ -91,9 +97,9 @@ static Predicate buildPredicate(Object object) { } if (object instanceof Collection) { return ((Collection) object).stream() - .map(element -> buildPredicate(element)) - .reduce((a, b) -> a.or(b)) - .orElse(fieldValue -> false); + .map(element -> buildPredicate(element)) + .reduce((a, b) -> a.or(b)) + .orElse(fieldValue -> false); } throw new IllegalArgumentException("Unsupported value type " + object.getClass()); } @@ -110,8 +116,7 @@ private static boolean numberEquals(Number left, Object other) { return false; } Number right = (Number) other; - if (left instanceof Double || left instanceof Float - || right instanceof Double || right instanceof Float) { + if (left instanceof Double || left instanceof Float || right instanceof Double || right instanceof Float) { return Double.compare(left.doubleValue(), right.doubleValue()) == 0; } return Numbers.toLongExact(left) == Numbers.toLongExact(right); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/mapper/expressiondsl/ExpressionParser.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/mapper/expressiondsl/ExpressionParser.java index 31fddcda08bfe..551fcb1706f09 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/mapper/expressiondsl/ExpressionParser.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/mapper/expressiondsl/ExpressionParser.java @@ -7,13 +7,13 @@ package org.elasticsearch.xpack.core.security.authc.support.mapper.expressiondsl; import org.elasticsearch.ElasticsearchParseException; -import org.elasticsearch.core.CheckedFunction; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.CheckedFunction; +import org.elasticsearch.xcontent.ContextParser; import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.ContextParser; import org.elasticsearch.xpack.core.watcher.support.xcontent.XContentSource; import java.io.IOException; @@ -72,8 +72,7 @@ public RoleMapperExpression parse(String name, XContentParser parser) throws IOE return parseRulesObject(name, parser, false); } - private RoleMapperExpression parseRulesObject(String objectName, XContentParser parser, - boolean allowExcept) throws IOException { + private RoleMapperExpression parseRulesObject(String objectName, XContentParser parser, boolean allowExcept) throws IOException { // find the start of the DSL object XContentParser.Token token; if (parser.currentToken() == null) { @@ -82,8 +81,11 @@ private RoleMapperExpression parseRulesObject(String objectName, XContentParser token = parser.currentToken(); } if (token != XContentParser.Token.START_OBJECT) { - throw new ElasticsearchParseException("failed to parse rules expression. expected [{}] to be an object but found [{}] instead", - objectName, token); + throw new ElasticsearchParseException( + "failed to parse rules expression. expected [{}] to be an object but found [{}] instead", + objectName, + token + ); } final String fieldName = readFieldName(objectName, parser); @@ -95,7 +97,7 @@ private RoleMapperExpression parseRulesObject(String objectName, XContentParser } private RoleMapperExpression parseExpression(XContentParser parser, String field, boolean allowExcept, String objectName) - throws IOException { + throws IOException { if (Fields.ANY.match(field, parser.getDeprecationHandler())) { return new AnyExpression(parseExpressionArray(Fields.ANY, parser, false)); @@ -107,12 +109,18 @@ private RoleMapperExpression parseExpression(XContentParser parser, String field if (allowExcept) { return parseExceptExpression(parser); } else { - throw new ElasticsearchParseException("failed to parse rules expression. field [{}] is not allowed within [{}]", - field, objectName); + throw new ElasticsearchParseException( + "failed to parse rules expression. field [{}] is not allowed within [{}]", + field, + objectName + ); } } else { - throw new ElasticsearchParseException("failed to parse rules expression. field [{}] is not recognised in object [{}]", - field, objectName); + throw new ElasticsearchParseException( + "failed to parse rules expression. field [{}] is not recognised in object [{}]", + field, + objectName + ); } } @@ -126,8 +134,10 @@ private RoleMapperExpression parseFieldExpression(XContentParser parser) throws values = Collections.singletonList(parseFieldValue(parser)); } if (parser.nextToken() != XContentParser.Token.END_OBJECT) { - throw new ElasticsearchParseException("failed to parse rules expression. object [{}] contains multiple fields", - Fields.FIELD.getPreferredName()); + throw new ElasticsearchParseException( + "failed to parse rules expression. object [{}] contains multiple fields", + Fields.FIELD.getPreferredName() + ); } return new FieldExpression(fieldName, values); } @@ -152,13 +162,13 @@ private String readFieldName(String objectName, XContentParser parser) throws IO } private List parseExpressionArray(ParseField field, XContentParser parser, boolean allowExcept) - throws IOException { + throws IOException { parser.nextToken(); // parseArray requires that the parser is positioned at the START_ARRAY token return parseArray(field, parser, p -> parseRulesObject(field.getPreferredName(), p, allowExcept)); } private List parseArray(ParseField field, XContentParser parser, CheckedFunction elementParser) - throws IOException { + throws IOException { final XContentParser.Token token = parser.currentToken(); if (token == XContentParser.Token.START_ARRAY) { List list = new ArrayList<>(); @@ -186,8 +196,10 @@ private FieldExpression.FieldValue parseFieldValue(XContentParser parser) throws return new FieldExpression.FieldValue(null); default: - throw new ElasticsearchParseException("failed to parse rules expression. expected a field value but found [{}] instead", - parser.currentToken()); + throw new ElasticsearchParseException( + "failed to parse rules expression. expected a field value but found [{}] instead", + parser.currentToken() + ); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/mapper/expressiondsl/FieldExpression.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/mapper/expressiondsl/FieldExpression.java index 418ceda2dca1e..227857924a2cf 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/mapper/expressiondsl/FieldExpression.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/mapper/expressiondsl/FieldExpression.java @@ -7,11 +7,11 @@ package org.elasticsearch.xpack.core.security.authc.support.mapper.expressiondsl; import org.apache.lucene.util.automaton.CharacterRunAutomaton; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.regex.Regex; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.security.support.Automatons; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/AuthorizationEngine.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/AuthorizationEngine.java index cca370edac975..bcf5285ead9be 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/AuthorizationEngine.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/AuthorizationEngine.java @@ -9,8 +9,8 @@ import org.elasticsearch.action.ActionListener; import org.elasticsearch.cluster.metadata.IndexAbstraction; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; +import org.elasticsearch.core.Nullable; import org.elasticsearch.transport.TransportRequest; import org.elasticsearch.xpack.core.security.action.user.GetUserPrivilegesRequest; import org.elasticsearch.xpack.core.security.action.user.GetUserPrivilegesResponse; @@ -132,9 +132,13 @@ public interface AuthorizationEngine { * alias or index * @param listener the listener to be notified of the authorization result */ - void authorizeIndexAction(RequestInfo requestInfo, AuthorizationInfo authorizationInfo, - AsyncSupplier indicesAsyncSupplier, Map aliasOrIndexLookup, - ActionListener listener); + void authorizeIndexAction( + RequestInfo requestInfo, + AuthorizationInfo authorizationInfo, + AsyncSupplier indicesAsyncSupplier, + Map aliasOrIndexLookup, + ActionListener listener + ); /** * Asynchronously loads a set of alias and index names for which the user is authorized @@ -148,9 +152,12 @@ void authorizeIndexAction(RequestInfo requestInfo, AuthorizationInfo authorizati * alias or index * @param listener the listener to be notified of the authorization result */ - void loadAuthorizedIndices(RequestInfo requestInfo, AuthorizationInfo authorizationInfo, - Map indicesLookup, ActionListener> listener); - + void loadAuthorizedIndices( + RequestInfo requestInfo, + AuthorizationInfo authorizationInfo, + Map indicesLookup, + ActionListener> listener + ); /** * Asynchronously checks that the permissions a user would have for a given list of names do @@ -169,8 +176,12 @@ void loadAuthorizedIndices(RequestInfo requestInfo, AuthorizationInfo authorizat * the name in the key would have. * @param listener the listener to be notified of the authorization result */ - void validateIndexPermissionsAreSubset(RequestInfo requestInfo, AuthorizationInfo authorizationInfo, - Map> indexNameToNewNames, ActionListener listener); + void validateIndexPermissionsAreSubset( + RequestInfo requestInfo, + AuthorizationInfo authorizationInfo, + Map> indexNameToNewNames, + ActionListener listener + ); /** * Checks the current user's privileges against those that being requested to check in the @@ -184,9 +195,13 @@ void validateIndexPermissionsAreSubset(RequestInfo requestInfo, AuthorizationInf * @param applicationPrivilegeDescriptors a collection of application privilege descriptors * @param listener the listener to be notified of the has privileges response */ - void checkPrivileges(Authentication authentication, AuthorizationInfo authorizationInfo, HasPrivilegesRequest hasPrivilegesRequest, - Collection applicationPrivilegeDescriptors, - ActionListener listener); + void checkPrivileges( + Authentication authentication, + AuthorizationInfo authorizationInfo, + HasPrivilegesRequest hasPrivilegesRequest, + Collection applicationPrivilegeDescriptors, + ActionListener listener + ); /** * Retrieve's the current user's privileges in a standard format that can be rendered via an @@ -198,8 +213,12 @@ void checkPrivileges(Authentication authentication, AuthorizationInfo authorizat * @param request the request for retrieving the user's privileges * @param listener the listener to be notified of the has privileges response */ - void getUserPrivileges(Authentication authentication, AuthorizationInfo authorizationInfo, GetUserPrivilegesRequest request, - ActionListener listener); + void getUserPrivileges( + Authentication authentication, + AuthorizationInfo authorizationInfo, + GetUserPrivilegesRequest request, + ActionListener listener + ); /** * Interface for objects that contains the information needed to authorize a request @@ -384,7 +403,6 @@ public IndicesAccessControl getIndicesAccessControl() { } } - final class AuthorizationContext { private final String action; private final AuthorizationInfo authorizationInfo; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/IndicesAndAliasesResolverField.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/IndicesAndAliasesResolverField.java index bd999b471d961..175de20e8bed2 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/IndicesAndAliasesResolverField.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/IndicesAndAliasesResolverField.java @@ -7,7 +7,7 @@ package org.elasticsearch.xpack.core.security.authz; public final class IndicesAndAliasesResolverField { - //placeholder used in the security plugin to indicate that the request is authorized knowing that it will yield an empty response + // placeholder used in the security plugin to indicate that the request is authorized knowing that it will yield an empty response public static final String NO_INDEX_PLACEHOLDER = "-*"; private IndicesAndAliasesResolverField() {} diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/RoleDescriptor.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/RoleDescriptor.java index d680070ae70f1..3c2be30b6e93b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/RoleDescriptor.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/RoleDescriptor.java @@ -8,8 +8,6 @@ import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.ElasticsearchSecurityException; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.ValidationException; import org.elasticsearch.common.bytes.BytesArray; @@ -18,8 +16,10 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -58,10 +58,12 @@ public class RoleDescriptor implements ToXContentObject, Writeable { private final Map metadata; private final Map transientMetadata; - public RoleDescriptor(String name, - @Nullable String[] clusterPrivileges, - @Nullable IndicesPrivileges[] indicesPrivileges, - @Nullable String[] runAs) { + public RoleDescriptor( + String name, + @Nullable String[] clusterPrivileges, + @Nullable IndicesPrivileges[] indicesPrivileges, + @Nullable String[] runAs + ) { this(name, clusterPrivileges, indicesPrivileges, runAs, null); } @@ -70,11 +72,13 @@ public RoleDescriptor(String name, * ConfigurableClusterPrivilege[], String[], Map, Map)} */ @Deprecated - public RoleDescriptor(String name, - @Nullable String[] clusterPrivileges, - @Nullable IndicesPrivileges[] indicesPrivileges, - @Nullable String[] runAs, - @Nullable Map metadata) { + public RoleDescriptor( + String name, + @Nullable String[] clusterPrivileges, + @Nullable IndicesPrivileges[] indicesPrivileges, + @Nullable String[] runAs, + @Nullable Map metadata + ) { this(name, clusterPrivileges, indicesPrivileges, runAs, metadata, null); } @@ -83,33 +87,39 @@ public RoleDescriptor(String name, * ConfigurableClusterPrivilege[], String[], Map, Map)} */ @Deprecated - public RoleDescriptor(String name, - @Nullable String[] clusterPrivileges, - @Nullable IndicesPrivileges[] indicesPrivileges, - @Nullable String[] runAs, - @Nullable Map metadata, - @Nullable Map transientMetadata) { + public RoleDescriptor( + String name, + @Nullable String[] clusterPrivileges, + @Nullable IndicesPrivileges[] indicesPrivileges, + @Nullable String[] runAs, + @Nullable Map metadata, + @Nullable Map transientMetadata + ) { this(name, clusterPrivileges, indicesPrivileges, null, null, runAs, metadata, transientMetadata); } - public RoleDescriptor(String name, - @Nullable String[] clusterPrivileges, - @Nullable IndicesPrivileges[] indicesPrivileges, - @Nullable ApplicationResourcePrivileges[] applicationPrivileges, - @Nullable ConfigurableClusterPrivilege[] configurableClusterPrivileges, - @Nullable String[] runAs, - @Nullable Map metadata, - @Nullable Map transientMetadata) { + public RoleDescriptor( + String name, + @Nullable String[] clusterPrivileges, + @Nullable IndicesPrivileges[] indicesPrivileges, + @Nullable ApplicationResourcePrivileges[] applicationPrivileges, + @Nullable ConfigurableClusterPrivilege[] configurableClusterPrivileges, + @Nullable String[] runAs, + @Nullable Map metadata, + @Nullable Map transientMetadata + ) { this.name = name; this.clusterPrivileges = clusterPrivileges != null ? clusterPrivileges : Strings.EMPTY_ARRAY; this.configurableClusterPrivileges = configurableClusterPrivileges != null - ? configurableClusterPrivileges : ConfigurableClusterPrivileges.EMPTY_ARRAY; + ? configurableClusterPrivileges + : ConfigurableClusterPrivileges.EMPTY_ARRAY; this.indicesPrivileges = indicesPrivileges != null ? indicesPrivileges : IndicesPrivileges.NONE; this.applicationPrivileges = applicationPrivileges != null ? applicationPrivileges : ApplicationResourcePrivileges.NONE; this.runAs = runAs != null ? runAs : Strings.EMPTY_ARRAY; this.metadata = metadata != null ? Collections.unmodifiableMap(metadata) : Collections.emptyMap(); - this.transientMetadata = transientMetadata != null ? Collections.unmodifiableMap(transientMetadata) : - Collections.singletonMap("enabled", true); + this.transientMetadata = transientMetadata != null + ? Collections.unmodifiableMap(transientMetadata) + : Collections.singletonMap("enabled", true); } public RoleDescriptor(StreamInput in) throws IOException { @@ -213,7 +223,6 @@ public int hashCode() { return result; } - public boolean isEmpty() { return clusterPrivileges.length == 0 && configurableClusterPrivileges.length == 0 @@ -276,12 +285,14 @@ public void writeTo(StreamOutput out) throws IOException { } public static RoleDescriptor parse(String name, BytesReference source, boolean allow2xFormat, XContentType xContentType) - throws IOException { + throws IOException { assert name != null; // EMPTY is safe here because we never use namedObject - try (InputStream stream = source.streamInput(); - XContentParser parser = xContentType.xContent() - .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) { + try ( + InputStream stream = source.streamInput(); + XContentParser parser = xContentType.xContent() + .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream) + ) { return parse(name, parser, allow2xFormat); } } @@ -311,39 +322,53 @@ public static RoleDescriptor parse(String name, XContentParser parser, boolean a if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (Fields.INDEX.match(currentFieldName, parser.getDeprecationHandler()) - || Fields.INDICES.match(currentFieldName, parser.getDeprecationHandler())) { - indicesPrivileges = parseIndices(name, parser, allow2xFormat); - } else if (Fields.RUN_AS.match(currentFieldName, parser.getDeprecationHandler())) { - runAsUsers = readStringArray(name, parser, true); - } else if (Fields.CLUSTER.match(currentFieldName, parser.getDeprecationHandler())) { - clusterPrivileges = readStringArray(name, parser, true); - } else if (Fields.APPLICATIONS.match(currentFieldName, parser.getDeprecationHandler()) + || Fields.INDICES.match(currentFieldName, parser.getDeprecationHandler())) { + indicesPrivileges = parseIndices(name, parser, allow2xFormat); + } else if (Fields.RUN_AS.match(currentFieldName, parser.getDeprecationHandler())) { + runAsUsers = readStringArray(name, parser, true); + } else if (Fields.CLUSTER.match(currentFieldName, parser.getDeprecationHandler())) { + clusterPrivileges = readStringArray(name, parser, true); + } else if (Fields.APPLICATIONS.match(currentFieldName, parser.getDeprecationHandler()) || Fields.APPLICATION.match(currentFieldName, parser.getDeprecationHandler())) { - applicationPrivileges = parseApplicationPrivileges(name, parser); - } else if (Fields.GLOBAL.match(currentFieldName, parser.getDeprecationHandler())) { - configurableClusterPrivileges = ConfigurableClusterPrivileges.parse(parser); - } else if (Fields.METADATA.match(currentFieldName, parser.getDeprecationHandler())) { - if (token != XContentParser.Token.START_OBJECT) { - throw new ElasticsearchParseException( - "expected field [{}] to be of type object, but found [{}] instead", currentFieldName, token); - } - metadata = parser.map(); - } else if (Fields.TRANSIENT_METADATA.match(currentFieldName, parser.getDeprecationHandler())) { - if (token == XContentParser.Token.START_OBJECT) { - // consume object but just drop - parser.map(); - } else { - throw new ElasticsearchParseException("failed to parse role [{}]. unexpected field [{}]", name, currentFieldName); - } - } else if (Fields.TYPE.match(currentFieldName, parser.getDeprecationHandler())) { - // don't need it - } else { - throw new ElasticsearchParseException("failed to parse role [{}]. unexpected field [{}]", name, currentFieldName); - } + applicationPrivileges = parseApplicationPrivileges(name, parser); + } else if (Fields.GLOBAL.match(currentFieldName, parser.getDeprecationHandler())) { + configurableClusterPrivileges = ConfigurableClusterPrivileges.parse(parser); + } else if (Fields.METADATA.match(currentFieldName, parser.getDeprecationHandler())) { + if (token != XContentParser.Token.START_OBJECT) { + throw new ElasticsearchParseException( + "expected field [{}] to be of type object, but found [{}] instead", + currentFieldName, + token + ); + } + metadata = parser.map(); + } else if (Fields.TRANSIENT_METADATA.match(currentFieldName, parser.getDeprecationHandler())) { + if (token == XContentParser.Token.START_OBJECT) { + // consume object but just drop + parser.map(); + } else { + throw new ElasticsearchParseException( + "failed to parse role [{}]. unexpected field [{}]", + name, + currentFieldName + ); + } + } else if (Fields.TYPE.match(currentFieldName, parser.getDeprecationHandler())) { + // don't need it + } else { + throw new ElasticsearchParseException("failed to parse role [{}]. unexpected field [{}]", name, currentFieldName); + } } - return new RoleDescriptor(name, clusterPrivileges, indicesPrivileges, applicationPrivileges, - configurableClusterPrivileges.toArray(new ConfigurableClusterPrivilege[configurableClusterPrivileges.size()]), runAsUsers, - metadata, null); + return new RoleDescriptor( + name, + clusterPrivileges, + indicesPrivileges, + applicationPrivileges, + configurableClusterPrivileges.toArray(new ConfigurableClusterPrivilege[configurableClusterPrivileges.size()]), + runAsUsers, + metadata, + null + ); } private static String[] readStringArray(String roleName, XContentParser parser, boolean allowNull) throws IOException { @@ -356,15 +381,20 @@ private static String[] readStringArray(String roleName, XContentParser parser, } public static RoleDescriptor parsePrivilegesCheck(String description, BytesReference source, XContentType xContentType) - throws IOException { - try (InputStream stream = source.streamInput(); - XContentParser parser = xContentType.xContent() - .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) { + throws IOException { + try ( + InputStream stream = source.streamInput(); + XContentParser parser = xContentType.xContent() + .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream) + ) { // advance to the START_OBJECT token XContentParser.Token token = parser.nextToken(); if (token != XContentParser.Token.START_OBJECT) { - throw new ElasticsearchParseException("failed to parse privileges check [{}]. expected an object but found [{}] instead", - description, token); + throw new ElasticsearchParseException( + "failed to parse privileges check [{}]. expected an object but found [{}] instead", + description, + token + ); } String currentFieldName = null; IndicesPrivileges[] indexPrivileges = null; @@ -378,21 +408,31 @@ public static RoleDescriptor parsePrivilegesCheck(String description, BytesRefer } else if (Fields.CLUSTER.match(currentFieldName, parser.getDeprecationHandler())) { clusterPrivileges = readStringArray(description, parser, true); } else if (Fields.APPLICATIONS.match(currentFieldName, parser.getDeprecationHandler()) - || Fields.APPLICATION.match(currentFieldName, parser.getDeprecationHandler())) { - applicationPrivileges = parseApplicationPrivileges(description, parser); - } else { - throw new ElasticsearchParseException("failed to parse privileges check [{}]. unexpected field [{}]", - description, currentFieldName); - } + || Fields.APPLICATION.match(currentFieldName, parser.getDeprecationHandler())) { + applicationPrivileges = parseApplicationPrivileges(description, parser); + } else { + throw new ElasticsearchParseException( + "failed to parse privileges check [{}]. unexpected field [{}]", + description, + currentFieldName + ); + } } if (indexPrivileges == null && clusterPrivileges == null && applicationPrivileges == null) { - throw new ElasticsearchParseException("failed to parse privileges check [{}]. All privilege fields [{},{},{}] are missing", - description, Fields.CLUSTER, Fields.INDEX, Fields.APPLICATIONS); + throw new ElasticsearchParseException( + "failed to parse privileges check [{}]. All privilege fields [{},{},{}] are missing", + description, + Fields.CLUSTER, + Fields.INDEX, + Fields.APPLICATIONS + ); } if (indexPrivileges != null) { if (Arrays.stream(indexPrivileges).anyMatch(IndicesPrivileges::isUsingFieldLevelSecurity)) { - throw new ElasticsearchParseException("Field [{}] is not supported in a has_privileges request", - RoleDescriptor.Fields.FIELD_PERMISSIONS); + throw new ElasticsearchParseException( + "Field [{}] is not supported in a has_privileges request", + RoleDescriptor.Fields.FIELD_PERMISSIONS + ); } if (Arrays.stream(indexPrivileges).anyMatch(IndicesPrivileges::isUsingDocumentLevelSecurity)) { throw new ElasticsearchParseException("Field [{}] is not supported in a has_privileges request", Fields.QUERY); @@ -402,11 +442,15 @@ public static RoleDescriptor parsePrivilegesCheck(String description, BytesRefer } } - private static RoleDescriptor.IndicesPrivileges[] parseIndices(String roleName, XContentParser parser, - boolean allow2xFormat) throws IOException { + private static RoleDescriptor.IndicesPrivileges[] parseIndices(String roleName, XContentParser parser, boolean allow2xFormat) + throws IOException { if (parser.currentToken() != XContentParser.Token.START_ARRAY) { - throw new ElasticsearchParseException("failed to parse indices privileges for role [{}]. expected field [{}] value " + - "to be an array, but found [{}] instead", roleName, parser.currentName(), parser.currentToken()); + throw new ElasticsearchParseException( + "failed to parse indices privileges for role [{}]. expected field [{}] value " + "to be an array, but found [{}] instead", + roleName, + parser.currentName(), + parser.currentToken() + ); } List privileges = new ArrayList<>(); while (parser.nextToken() != XContentParser.Token.END_ARRAY) { @@ -415,12 +459,17 @@ private static RoleDescriptor.IndicesPrivileges[] parseIndices(String roleName, return privileges.toArray(new IndicesPrivileges[privileges.size()]); } - private static RoleDescriptor.IndicesPrivileges parseIndex(String roleName, XContentParser parser, - boolean allow2xFormat) throws IOException { + private static RoleDescriptor.IndicesPrivileges parseIndex(String roleName, XContentParser parser, boolean allow2xFormat) + throws IOException { XContentParser.Token token = parser.currentToken(); if (token != XContentParser.Token.START_OBJECT) { - throw new ElasticsearchParseException("failed to parse indices privileges for role [{}]. expected field [{}] value to " + - "be an array of objects, but found an array element of type [{}]", roleName, parser.currentName(), token); + throw new ElasticsearchParseException( + "failed to parse indices privileges for role [{}]. expected field [{}] value to " + + "be an array of objects, but found an array element of type [{}]", + roleName, + parser.currentName(), + token + ); } String currentFieldName = null; String[] names = null; @@ -434,23 +483,36 @@ private static RoleDescriptor.IndicesPrivileges parseIndex(String roleName, XCon currentFieldName = parser.currentName(); } else if (Fields.NAMES.match(currentFieldName, parser.getDeprecationHandler())) { if (token == XContentParser.Token.VALUE_STRING) { - names = new String[]{parser.text()}; + names = new String[] { parser.text() }; } else if (token == XContentParser.Token.START_ARRAY) { names = readStringArray(roleName, parser, false); if (names.length == 0) { - throw new ElasticsearchParseException("failed to parse indices privileges for role [{}]. [{}] cannot be an empty " + - "array", roleName, currentFieldName); + throw new ElasticsearchParseException( + "failed to parse indices privileges for role [{}]. [{}] cannot be an empty " + "array", + roleName, + currentFieldName + ); } } else { - throw new ElasticsearchParseException("failed to parse indices privileges for role [{}]. expected field [{}] " + - "value to be a string or an array of strings, but found [{}] instead", roleName, currentFieldName, token); + throw new ElasticsearchParseException( + "failed to parse indices privileges for role [{}]. expected field [{}] " + + "value to be a string or an array of strings, but found [{}] instead", + roleName, + currentFieldName, + token + ); } } else if (Fields.ALLOW_RESTRICTED_INDICES.match(currentFieldName, parser.getDeprecationHandler())) { if (token == XContentParser.Token.VALUE_BOOLEAN) { allowRestrictedIndices = parser.booleanValue(); } else { - throw new ElasticsearchParseException("failed to parse indices privileges for role [{}]. expected field [{}] " + - "value to be a boolean, but found [{}] instead", roleName, currentFieldName, token); + throw new ElasticsearchParseException( + "failed to parse indices privileges for role [{}]. expected field [{}] " + + "value to be a boolean, but found [{}] instead", + roleName, + currentFieldName, + token + ); } } else if (Fields.QUERY.match(currentFieldName, parser.getDeprecationHandler())) { if (token == XContentParser.Token.START_OBJECT) { @@ -463,9 +525,13 @@ private static RoleDescriptor.IndicesPrivileges parseIndex(String roleName, XCon query = new BytesArray(text); } } else if (token != XContentParser.Token.VALUE_NULL) { - throw new ElasticsearchParseException("failed to parse indices privileges for role [{}]. expected field [{}] " + - "value to be null, a string, an array, or an object, but found [{}] instead", roleName, currentFieldName, - token); + throw new ElasticsearchParseException( + "failed to parse indices privileges for role [{}]. expected field [{}] " + + "value to be null, a string, an array, or an object, but found [{}] instead", + roleName, + currentFieldName, + token + ); } } else if (Fields.FIELD_PERMISSIONS.match(currentFieldName, parser.getDeprecationHandler())) { if (token == XContentParser.Token.START_OBJECT) { @@ -477,37 +543,59 @@ private static RoleDescriptor.IndicesPrivileges parseIndex(String roleName, XCon parser.nextToken(); grantedFields = readStringArray(roleName, parser, true); if (grantedFields == null) { - throw new ElasticsearchParseException("failed to parse indices privileges for role [{}]. {} must not " + - "be null.", roleName, Fields.GRANT_FIELDS); + throw new ElasticsearchParseException( + "failed to parse indices privileges for role [{}]. {} must not " + "be null.", + roleName, + Fields.GRANT_FIELDS + ); } } else if (Fields.EXCEPT_FIELDS.match(currentFieldName, parser.getDeprecationHandler())) { parser.nextToken(); deniedFields = readStringArray(roleName, parser, true); if (deniedFields == null) { - throw new ElasticsearchParseException("failed to parse indices privileges for role [{}]. {} must not " + - "be null.", roleName, Fields.EXCEPT_FIELDS); + throw new ElasticsearchParseException( + "failed to parse indices privileges for role [{}]. {} must not " + "be null.", + roleName, + Fields.EXCEPT_FIELDS + ); } } else { - throw new ElasticsearchParseException("failed to parse indices privileges for role [{}]. " + - "\"{}\" only accepts options {} and {}, but got: {}", - roleName, Fields.FIELD_PERMISSIONS, Fields.GRANT_FIELDS, Fields.EXCEPT_FIELDS - , parser.currentName()); + throw new ElasticsearchParseException( + "failed to parse indices privileges for role [{}]. " + + "\"{}\" only accepts options {} and {}, but got: {}", + roleName, + Fields.FIELD_PERMISSIONS, + Fields.GRANT_FIELDS, + Fields.EXCEPT_FIELDS, + parser.currentName() + ); } } else { if (token == XContentParser.Token.END_OBJECT) { - throw new ElasticsearchParseException("failed to parse indices privileges for role [{}]. " + - "\"{}\" must not be empty.", roleName, Fields.FIELD_PERMISSIONS); + throw new ElasticsearchParseException( + "failed to parse indices privileges for role [{}]. " + "\"{}\" must not be empty.", + roleName, + Fields.FIELD_PERMISSIONS + ); } else { - throw new ElasticsearchParseException("failed to parse indices privileges for role [{}]. expected {} but " + - "got {}.", roleName, XContentParser.Token.FIELD_NAME, - token); + throw new ElasticsearchParseException( + "failed to parse indices privileges for role [{}]. expected {} but " + "got {}.", + roleName, + XContentParser.Token.FIELD_NAME, + token + ); } } } while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT); } else { - throw new ElasticsearchParseException("failed to parse indices privileges for role [{}]. expected {} or {} but got {}" + - " in \"{}\".", roleName, XContentParser.Token.START_OBJECT, - XContentParser.Token.START_ARRAY, token, Fields.FIELD_PERMISSIONS); + throw new ElasticsearchParseException( + "failed to parse indices privileges for role [{}]. expected {} or {} but got {}" + " in \"{}\".", + roleName, + XContentParser.Token.START_OBJECT, + XContentParser.Token.START_ARRAY, + token, + Fields.FIELD_PERMISSIONS + ); } } else if (Fields.PRIVILEGES.match(currentFieldName, parser.getDeprecationHandler())) { privileges = readStringArray(roleName, parser, true); @@ -515,9 +603,16 @@ private static RoleDescriptor.IndicesPrivileges parseIndex(String roleName, XCon if (allow2xFormat) { grantedFields = readStringArray(roleName, parser, true); } else { - throw new ElasticsearchParseException("[\"fields\": [...]] format has changed for field" + - " permissions in role [{}], use [\"{}\": {\"{}\":[...]," + "\"{}\":[...]}] instead", - roleName, Fields.FIELD_PERMISSIONS, Fields.GRANT_FIELDS, Fields.EXCEPT_FIELDS, roleName); + throw new ElasticsearchParseException( + "[\"fields\": [...]] format has changed for field" + + " permissions in role [{}], use [\"{}\": {\"{}\":[...]," + + "\"{}\":[...]}] instead", + roleName, + Fields.FIELD_PERMISSIONS, + Fields.GRANT_FIELDS, + Fields.EXCEPT_FIELDS, + roleName + ); } } else if (Fields.TRANSIENT_METADATA.match(currentFieldName, parser.getDeprecationHandler())) { if (token == XContentParser.Token.START_OBJECT) { @@ -525,35 +620,54 @@ private static RoleDescriptor.IndicesPrivileges parseIndex(String roleName, XCon // it is transient metadata, skip it } } else { - throw new ElasticsearchParseException("failed to parse transient metadata for role [{}]. expected {} but got {}" + - " in \"{}\".", roleName, XContentParser.Token.START_OBJECT, token, Fields.TRANSIENT_METADATA); + throw new ElasticsearchParseException( + "failed to parse transient metadata for role [{}]. expected {} but got {}" + " in \"{}\".", + roleName, + XContentParser.Token.START_OBJECT, + token, + Fields.TRANSIENT_METADATA + ); } } else { - throw new ElasticsearchParseException("failed to parse indices privileges for role [{}]. unexpected field [{}]", - roleName, currentFieldName); + throw new ElasticsearchParseException( + "failed to parse indices privileges for role [{}]. unexpected field [{}]", + roleName, + currentFieldName + ); } } if (names == null) { - throw new ElasticsearchParseException("failed to parse indices privileges for role [{}]. missing required [{}] field", - roleName, Fields.NAMES.getPreferredName()); + throw new ElasticsearchParseException( + "failed to parse indices privileges for role [{}]. missing required [{}] field", + roleName, + Fields.NAMES.getPreferredName() + ); } if (privileges == null) { - throw new ElasticsearchParseException("failed to parse indices privileges for role [{}]. missing required [{}] field", - roleName, Fields.PRIVILEGES.getPreferredName()); + throw new ElasticsearchParseException( + "failed to parse indices privileges for role [{}]. missing required [{}] field", + roleName, + Fields.PRIVILEGES.getPreferredName() + ); } if (deniedFields != null && grantedFields == null) { - throw new ElasticsearchParseException("failed to parse indices privileges for role [{}]. {} requires {} if {} is given", - roleName, Fields.FIELD_PERMISSIONS, Fields.GRANT_FIELDS, Fields.EXCEPT_FIELDS); + throw new ElasticsearchParseException( + "failed to parse indices privileges for role [{}]. {} requires {} if {} is given", + roleName, + Fields.FIELD_PERMISSIONS, + Fields.GRANT_FIELDS, + Fields.EXCEPT_FIELDS + ); } checkIfExceptFieldsIsSubsetOfGrantedFields(roleName, grantedFields, deniedFields); return RoleDescriptor.IndicesPrivileges.builder() - .indices(names) - .privileges(privileges) - .grantedFields(grantedFields) - .deniedFields(deniedFields) - .query(query) - .allowRestrictedIndices(allowRestrictedIndices) - .build(); + .indices(names) + .privileges(privileges) + .grantedFields(grantedFields) + .deniedFields(deniedFields) + .query(query) + .allowRestrictedIndices(allowRestrictedIndices) + .build(); } private static void checkIfExceptFieldsIsSubsetOfGrantedFields(String roleName, String[] grantedFields, String[] deniedFields) { @@ -564,11 +678,15 @@ private static void checkIfExceptFieldsIsSubsetOfGrantedFields(String roleName, } } - private static ApplicationResourcePrivileges[] parseApplicationPrivileges(String roleName, XContentParser parser) - throws IOException { + private static ApplicationResourcePrivileges[] parseApplicationPrivileges(String roleName, XContentParser parser) throws IOException { if (parser.currentToken() != XContentParser.Token.START_ARRAY) { - throw new ElasticsearchParseException("failed to parse application privileges for role [{}]. expected field [{}] value " + - "to be an array, but found [{}] instead", roleName, parser.currentName(), parser.currentToken()); + throw new ElasticsearchParseException( + "failed to parse application privileges for role [{}]. expected field [{}] value " + + "to be an array, but found [{}] instead", + roleName, + parser.currentName(), + parser.currentToken() + ); } List privileges = new ArrayList<>(); while (parser.nextToken() != XContentParser.Token.END_ARRAY) { @@ -580,17 +698,28 @@ private static ApplicationResourcePrivileges[] parseApplicationPrivileges(String private static ApplicationResourcePrivileges parseApplicationPrivilege(String roleName, XContentParser parser) throws IOException { XContentParser.Token token = parser.currentToken(); if (token != XContentParser.Token.START_OBJECT) { - throw new ElasticsearchParseException("failed to parse application privileges for role [{}]. expected field [{}] value to " + - "be an array of objects, but found an array element of type [{}]", roleName, parser.currentName(), token); + throw new ElasticsearchParseException( + "failed to parse application privileges for role [{}]. expected field [{}] value to " + + "be an array of objects, but found an array element of type [{}]", + roleName, + parser.currentName(), + token + ); } final ApplicationResourcePrivileges.Builder builder = ApplicationResourcePrivileges.PARSER.parse(parser, null); if (builder.hasResources() == false) { - throw new ElasticsearchParseException("failed to parse application privileges for role [{}]. missing required [{}] field", - roleName, Fields.RESOURCES.getPreferredName()); + throw new ElasticsearchParseException( + "failed to parse application privileges for role [{}]. missing required [{}] field", + roleName, + Fields.RESOURCES.getPreferredName() + ); } if (builder.hasPrivileges() == false) { - throw new ElasticsearchParseException("failed to parse application privileges for role [{}]. missing required [{}] field", - roleName, Fields.PRIVILEGES.getPreferredName()); + throw new ElasticsearchParseException( + "failed to parse application privileges for role [{}]. missing required [{}] field", + roleName, + Fields.PRIVILEGES.getPreferredName() + ); } return builder.build(); } @@ -613,8 +742,7 @@ public static class IndicesPrivileges implements ToXContentObject, Writeable { // indices as well. private boolean allowRestrictedIndices = false; - private IndicesPrivileges() { - } + private IndicesPrivileges() {} public IndicesPrivileges(StreamInput in) throws IOException { this.indices = in.readStringArray(); @@ -702,15 +830,16 @@ public String toString() { if (grantedFields == null) { sb.append(RoleDescriptor.Fields.GRANT_FIELDS).append("=null"); } else { - sb.append(RoleDescriptor.Fields.GRANT_FIELDS).append("=[") - .append(Strings.arrayToCommaDelimitedString(grantedFields)); + sb.append(RoleDescriptor.Fields.GRANT_FIELDS).append("=[").append(Strings.arrayToCommaDelimitedString(grantedFields)); sb.append("]"); } if (deniedFields == null) { sb.append(", ").append(RoleDescriptor.Fields.EXCEPT_FIELDS).append("=null"); } else { - sb.append(", ").append(RoleDescriptor.Fields.EXCEPT_FIELDS).append("=[") - .append(Strings.arrayToCommaDelimitedString(deniedFields)); + sb.append(", ") + .append(RoleDescriptor.Fields.EXCEPT_FIELDS) + .append("=[") + .append(Strings.arrayToCommaDelimitedString(deniedFields)); sb.append("]"); } sb.append("]"); @@ -775,8 +904,7 @@ public static class Builder { private IndicesPrivileges indicesPrivileges = new IndicesPrivileges(); - private Builder() { - } + private Builder() {} public Builder indices(String... indices) { indicesPrivileges.indices = indices; @@ -839,8 +967,10 @@ public IndicesPrivileges build() { public static class ApplicationResourcePrivileges implements ToXContentObject, Writeable { private static final ApplicationResourcePrivileges[] NONE = new ApplicationResourcePrivileges[0]; - private static final ObjectParser PARSER = new ObjectParser<>("application", - ApplicationResourcePrivileges::builder); + private static final ObjectParser PARSER = new ObjectParser<>( + "application", + ApplicationResourcePrivileges::builder + ); static { PARSER.declareString(Builder::application, Fields.APPLICATION); @@ -852,8 +982,7 @@ public static class ApplicationResourcePrivileges implements ToXContentObject, W private String[] privileges; private String[] resources; - private ApplicationResourcePrivileges() { - } + private ApplicationResourcePrivileges() {} public ApplicationResourcePrivileges(StreamInput in) throws IOException { this.application = in.readString(); @@ -886,14 +1015,13 @@ public String[] getPrivileges() { @Override public String toString() { - StringBuilder sb = new StringBuilder(getClass().getSimpleName()) - .append("[application=") - .append(application) - .append(", privileges=[") - .append(Strings.arrayToCommaDelimitedString(privileges)) - .append("], resources=[") - .append(Strings.arrayToCommaDelimitedString(resources)) - .append("]]"); + StringBuilder sb = new StringBuilder(getClass().getSimpleName()).append("[application=") + .append(application) + .append(", privileges=[") + .append(Strings.arrayToCommaDelimitedString(privileges)) + .append("], resources=[") + .append(Strings.arrayToCommaDelimitedString(resources)) + .append("]]"); return sb.toString(); } @@ -937,8 +1065,7 @@ public static class Builder { private ApplicationResourcePrivileges applicationPrivileges = new ApplicationResourcePrivileges(); - private Builder() { - } + private Builder() {} public Builder application(String appName) { applicationPrivileges.application = appName; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/DocumentSubsetBitsetCache.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/DocumentSubsetBitsetCache.java index a6da1981e87b1..1f0d9ca8bca8d 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/DocumentSubsetBitsetCache.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/DocumentSubsetBitsetCache.java @@ -22,7 +22,6 @@ import org.apache.lucene.util.Accountable; import org.apache.lucene.util.BitSet; import org.apache.lucene.util.FixedBitSet; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.cache.Cache; import org.elasticsearch.common.cache.CacheBuilder; import org.elasticsearch.common.cache.RemovalNotification; @@ -31,9 +30,10 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.ReleasableLock; import org.elasticsearch.common.util.set.Sets; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.threadpool.ThreadPool; import java.io.Closeable; @@ -82,14 +82,20 @@ public final class DocumentSubsetBitsetCache implements IndexReader.ClosedListen * The TTL defaults to 2 hours. We default to a large cache size ({@link #CACHE_SIZE_SETTING}), and aggressively * expire unused entries so that the cache does not hold on to memory unnecessarily. */ - static final Setting CACHE_TTL_SETTING = - Setting.timeSetting("xpack.security.dls.bitset.cache.ttl", TimeValue.timeValueHours(2), Property.NodeScope); + static final Setting CACHE_TTL_SETTING = Setting.timeSetting( + "xpack.security.dls.bitset.cache.ttl", + TimeValue.timeValueHours(2), + Property.NodeScope + ); /** * The size defaults to 10% of heap so that it automatically scales up with larger node size */ - static final Setting CACHE_SIZE_SETTING = Setting.memorySizeSetting("xpack.security.dls.bitset.cache.size", - "10%", Property.NodeScope); + static final Setting CACHE_SIZE_SETTING = Setting.memorySizeSetting( + "xpack.security.dls.bitset.cache.size", + "10%", + Property.NodeScope + ); private static final BitSet NULL_MARKER = new FixedBitSet(0); @@ -241,10 +247,14 @@ public BitSet getBitSet(final Query query, final LeafReaderContext context) thro } final long bitSetBytes = result.ramBytesUsed(); if (bitSetBytes > this.maxWeightBytes) { - logger.warn("built a DLS BitSet that uses [{}] bytes; the DLS BitSet cache has a maximum size of [{}] bytes;" + - " this object cannot be cached and will need to be rebuilt for each use;" + - " consider increasing the value of [{}]", - bitSetBytes, maxWeightBytes, CACHE_SIZE_SETTING.getKey()); + logger.warn( + "built a DLS BitSet that uses [{}] bytes; the DLS BitSet cache has a maximum size of [{}] bytes;" + + " this object cannot be cached and will need to be rebuilt for each use;" + + " consider increasing the value of [{}]", + bitSetBytes, + maxWeightBytes, + CACHE_SIZE_SETTING.getKey() + ); } else if (bitSetBytes + bitsetCache.weight() > maxWeightBytes) { maybeLogCacheFullWarning(); } @@ -282,7 +292,8 @@ private void maybeLogCacheFullWarning() { if (cacheFullWarningTime.compareAndSet(nextLogTime, nextCheck)) { logger.info( "the Document Level Security BitSet cache is full which may impact performance; consider increasing the value of [{}]", - CACHE_SIZE_SETTING.getKey()); + CACHE_SIZE_SETTING.getKey() + ); } } @@ -292,11 +303,7 @@ public static List> getSettings() { public Map usageStats() { final ByteSizeValue ram = new ByteSizeValue(ramBytesUsed(), ByteSizeUnit.BYTES); - return Map.of( - "count", entryCount(), - "memory", ram.toString(), - "memory_in_bytes", ram.getBytes() - ); + return Map.of("count", entryCount(), "memory", ram.toString(), "memory_in_bytes", ram.getBytes()); } private class BitsetCacheKey { @@ -317,8 +324,7 @@ public boolean equals(Object other) { return false; } final BitsetCacheKey that = (BitsetCacheKey) other; - return Objects.equals(this.index, that.index) && - Objects.equals(this.query, that.query); + return Objects.equals(this.index, that.index) && Objects.equals(this.query, that.query); } @Override @@ -340,12 +346,14 @@ void verifyInternalConsistency() { this.bitsetCache.keys().forEach(bck -> { final Set set = this.keysByIndex.get(bck.index); if (set == null) { - throw new IllegalStateException("Key [" + bck + "] is in the cache, but there is no entry for [" + bck.index + - "] in the lookup map"); + throw new IllegalStateException( + "Key [" + bck + "] is in the cache, but there is no entry for [" + bck.index + "] in the lookup map" + ); } if (set.contains(bck) == false) { - throw new IllegalStateException("Key [" + bck + "] is in the cache, but the lookup entry for [" + bck.index + - "] does not contain that key"); + throw new IllegalStateException( + "Key [" + bck + "] is in the cache, but the lookup entry for [" + bck.index + "] does not contain that key" + ); } }); this.keysByIndex.values().stream().flatMap(Set::stream).forEach(bck -> { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/DocumentSubsetReader.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/DocumentSubsetReader.java index 01dbe0d75e38d..e69dc26370663 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/DocumentSubsetReader.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/DocumentSubsetReader.java @@ -17,13 +17,13 @@ import org.apache.lucene.util.BitSet; import org.apache.lucene.util.BitSetIterator; import org.apache.lucene.util.Bits; -import org.elasticsearch.lucene.util.CombinedBitSet; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ExceptionsHelper; import org.elasticsearch.common.cache.Cache; import org.elasticsearch.common.cache.CacheBuilder; import org.elasticsearch.common.logging.LoggerMessageFormat; import org.elasticsearch.common.lucene.index.SequentialStoredFieldsLeafReader; +import org.elasticsearch.lucene.util.CombinedBitSet; import org.elasticsearch.transport.Transports; import java.io.IOException; @@ -37,8 +37,8 @@ */ public final class DocumentSubsetReader extends SequentialStoredFieldsLeafReader { - public static DocumentSubsetDirectoryReader wrap(DirectoryReader in, DocumentSubsetBitsetCache bitsetCache, - Query roleQuery) throws IOException { + public static DocumentSubsetDirectoryReader wrap(DirectoryReader in, DocumentSubsetBitsetCache bitsetCache, Query roleQuery) + throws IOException { return new DocumentSubsetDirectoryReader(in, bitsetCache, roleQuery); } @@ -90,16 +90,15 @@ private static int getNumDocs(LeafReader reader, Query roleQuery, BitSet roleQue return computeNumDocs(reader, roleQueryBits); } final boolean[] added = new boolean[] { false }; - Cache perReaderCache = NUM_DOCS_CACHE.computeIfAbsent(cacheHelper.getKey(), - key -> { - added[0] = true; - return CacheBuilder.builder() - // Not configurable, this limit only exists so that if a role query is updated - // then we won't risk OOME because of old role queries that are not used anymore - .setMaximumWeight(1000) - .weigher((k, v) -> 1) // just count - .build(); - }); + Cache perReaderCache = NUM_DOCS_CACHE.computeIfAbsent(cacheHelper.getKey(), key -> { + added[0] = true; + return CacheBuilder.builder() + // Not configurable, this limit only exists so that if a role query is updated + // then we won't risk OOME because of old role queries that are not used anymore + .setMaximumWeight(1000) + .weigher((k, v) -> 1) // just count + .build(); + }); if (added[0]) { IndexReader.ClosedListener closedListener = NUM_DOCS_CACHE::remove; try { @@ -117,8 +116,8 @@ public static final class DocumentSubsetDirectoryReader extends FilterDirectoryR private final Query roleQuery; private final DocumentSubsetBitsetCache bitsetCache; - DocumentSubsetDirectoryReader(final DirectoryReader in, final DocumentSubsetBitsetCache bitsetCache, - final Query roleQuery) throws IOException { + DocumentSubsetDirectoryReader(final DirectoryReader in, final DocumentSubsetBitsetCache bitsetCache, final Query roleQuery) + throws IOException { super(in, new SubReaderWrapper() { @Override public LeafReader wrap(LeafReader reader) { @@ -144,8 +143,9 @@ private static void verifyNoOtherDocumentSubsetDirectoryReaderIsWrapped(Director if (reader instanceof FilterDirectoryReader) { FilterDirectoryReader filterDirectoryReader = (FilterDirectoryReader) reader; if (filterDirectoryReader instanceof DocumentSubsetDirectoryReader) { - throw new IllegalArgumentException(LoggerMessageFormat.format("Can't wrap [{}] twice", - DocumentSubsetDirectoryReader.class)); + throw new IllegalArgumentException( + LoggerMessageFormat.format("Can't wrap [{}] twice", DocumentSubsetDirectoryReader.class) + ); } else { verifyNoOtherDocumentSubsetDirectoryReaderIsWrapped(filterDirectoryReader.getDelegate()); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/FieldSubsetReader.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/FieldSubsetReader.java index 24a4b327f848a..74e7efd1e4ab0 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/FieldSubsetReader.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/FieldSubsetReader.java @@ -34,12 +34,12 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.logging.LoggerMessageFormat; import org.elasticsearch.common.lucene.index.SequentialStoredFieldsLeafReader; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.core.Tuple; import org.elasticsearch.index.mapper.FieldNamesFieldMapper; import org.elasticsearch.index.mapper.SourceFieldMapper; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; import java.io.IOException; import java.io.UncheckedIOException; @@ -103,8 +103,9 @@ private static void verifyNoOtherFieldSubsetDirectoryReaderIsWrapped(DirectoryRe if (reader instanceof FilterDirectoryReader) { FilterDirectoryReader filterDirectoryReader = (FilterDirectoryReader) reader; if (filterDirectoryReader instanceof FieldSubsetDirectoryReader) { - throw new IllegalArgumentException(LoggerMessageFormat.format("Can't wrap [{}] twice", - FieldSubsetDirectoryReader.class)); + throw new IllegalArgumentException( + LoggerMessageFormat.format("Can't wrap [{}] twice", FieldSubsetDirectoryReader.class) + ); } else { verifyNoOtherFieldSubsetDirectoryReaderIsWrapped(filterDirectoryReader.getDelegate()); } @@ -210,7 +211,7 @@ private static List filter(Iterable iterable, CharacterRunAutomaton i if (state == -1) { continue; } - Map filteredValue = filter((Map)value, includeAutomaton, state); + Map filteredValue = filter((Map) value, includeAutomaton, state); filtered.add(filteredValue); } else if (value instanceof Iterable) { List filteredValue = filter((Iterable) value, includeAutomaton, initialState); @@ -456,7 +457,7 @@ class FieldNamesTerms extends FilterTerms { final TermsEnum e = iterator(); long size = 0, sumDocFreq = 0, sumTotalFreq = 0; while (e.next() != null) { - size ++; + size++; sumDocFreq += e.docFreq(); sumTotalFreq += e.totalTermFreq(); } @@ -544,12 +545,12 @@ public BytesRef next() throws IOException { @Override public void seekExact(long ord) throws IOException { - throw new UnsupportedOperationException(); + throw new UnsupportedOperationException(); } @Override public long ord() throws IOException { - throw new UnsupportedOperationException(); + throw new UnsupportedOperationException(); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/IndicesAccessControl.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/IndicesAccessControl.java index e99464fe74e63..15ea0ba298038 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/IndicesAccessControl.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/IndicesAccessControl.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.security.authz.accesscontrol; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.util.set.Sets; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xpack.core.security.authz.IndicesAndAliasesResolverField; import org.elasticsearch.xpack.core.security.authz.permission.DocumentPermissions; import org.elasticsearch.xpack.core.security.authz.permission.FieldPermissions; @@ -30,9 +30,13 @@ */ public class IndicesAccessControl { - public static final IndicesAccessControl ALLOW_NO_INDICES = new IndicesAccessControl(true, - Collections.singletonMap(IndicesAndAliasesResolverField.NO_INDEX_PLACEHOLDER, - new IndicesAccessControl.IndexAccessControl(true, new FieldPermissions(), DocumentPermissions.allowAll()))); + public static final IndicesAccessControl ALLOW_NO_INDICES = new IndicesAccessControl( + true, + Collections.singletonMap( + IndicesAndAliasesResolverField.NO_INDEX_PLACEHOLDER, + new IndicesAccessControl.IndexAccessControl(true, new FieldPermissions(), DocumentPermissions.allowAll()) + ) + ); public static final IndicesAccessControl DENIED = new IndicesAccessControl(false, Collections.emptyMap()); private final boolean granted; @@ -60,7 +64,8 @@ public boolean isGranted() { } public Collection getDeniedIndices() { - return this.indexPermissions.entrySet().stream() + return this.indexPermissions.entrySet() + .stream() .filter(e -> e.getValue().granted == false) .map(Map.Entry::getKey) .collect(Collectors.toUnmodifiableSet()); @@ -90,9 +95,12 @@ public DlsFlsUsage getFieldAndDocumentLevelSecurityUsage() { } public List getIndicesWithFieldOrDocumentLevelSecurity() { - return indexPermissions.entrySet().stream() - .filter(entry -> entry.getValue().fieldPermissions.hasFieldLevelSecurity() - || entry.getValue().documentPermissions.hasDocumentLevelPermissions()) + return indexPermissions.entrySet() + .stream() + .filter( + entry -> entry.getValue().fieldPermissions.hasFieldLevelSecurity() + || entry.getValue().documentPermissions.hasDocumentLevelPermissions() + ) .map(Map.Entry::getKey) .collect(Collectors.toUnmodifiableList()); } @@ -191,20 +199,23 @@ public IndexAccessControl limitIndexAccessControl(IndexAccessControl limitedByIn } else { granted = false; } - FieldPermissions fieldPermissions = getFieldPermissions().limitFieldPermissions( - limitedByIndexAccessControl.fieldPermissions); - DocumentPermissions documentPermissions = getDocumentPermissions() - .limitDocumentPermissions(limitedByIndexAccessControl.getDocumentPermissions()); + FieldPermissions fieldPermissions = getFieldPermissions().limitFieldPermissions(limitedByIndexAccessControl.fieldPermissions); + DocumentPermissions documentPermissions = getDocumentPermissions().limitDocumentPermissions( + limitedByIndexAccessControl.getDocumentPermissions() + ); return new IndexAccessControl(granted, fieldPermissions, documentPermissions); } @Override public String toString() { - return "IndexAccessControl{" + - "granted=" + granted + - ", fieldPermissions=" + fieldPermissions + - ", documentPermissions=" + documentPermissions + - '}'; + return "IndexAccessControl{" + + "granted=" + + granted + + ", fieldPermissions=" + + fieldPermissions + + ", documentPermissions=" + + documentPermissions + + '}'; } @Override @@ -225,13 +236,12 @@ public void buildCacheKey(StreamOutput out, DlsQueryEvaluationContext context) t @Override public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; IndexAccessControl that = (IndexAccessControl) o; - return granted == that.granted && Objects.equals(fieldPermissions, that.fieldPermissions) && Objects.equals(documentPermissions, - that.documentPermissions); + return granted == that.granted + && Objects.equals(fieldPermissions, that.fieldPermissions) + && Objects.equals(documentPermissions, that.documentPermissions); } @Override @@ -275,10 +285,7 @@ public IndicesAccessControl limitIndicesAccessControl(IndicesAccessControl limit @Override public String toString() { - return "IndicesAccessControl{" + - "granted=" + granted + - ", indexPermissions=" + indexPermissions + - '}'; + return "IndicesAccessControl{" + "granted=" + granted + ", indexPermissions=" + indexPermissions + '}'; } public static IndicesAccessControl allowAll() { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/SecurityIndexReaderWrapper.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/SecurityIndexReaderWrapper.java index 9d1d25318d8ab..29e6f20aa5ebf 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/SecurityIndexReaderWrapper.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/SecurityIndexReaderWrapper.java @@ -12,9 +12,9 @@ import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.ConstantScoreQuery; import org.elasticsearch.ExceptionsHelper; -import org.elasticsearch.core.CheckedFunction; import org.elasticsearch.common.logging.LoggerMessageFormat; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.CheckedFunction; import org.elasticsearch.index.query.SearchExecutionContext; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.index.shard.ShardUtils; @@ -52,9 +52,13 @@ public class SecurityIndexReaderWrapper implements CheckedFunction searchExecutionContextProvider, - DocumentSubsetBitsetCache bitsetCache, SecurityContext securityContext, - XPackLicenseState licenseState, ScriptService scriptService) { + public SecurityIndexReaderWrapper( + Function searchExecutionContextProvider, + DocumentSubsetBitsetCache bitsetCache, + SecurityContext securityContext, + XPackLicenseState licenseState, + ScriptService scriptService + ) { this.scriptService = scriptService; this.searchExecutionContextProvider = searchExecutionContextProvider; this.bitsetCache = bitsetCache; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/ApplicationPermission.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/ApplicationPermission.java index 3ee73abdf8020..e9bb7e01a1697 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/ApplicationPermission.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/ApplicationPermission.java @@ -10,8 +10,8 @@ import org.apache.logging.log4j.Logger; import org.apache.lucene.util.automaton.Automaton; import org.apache.lucene.util.automaton.Operations; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.util.set.Sets; +import org.elasticsearch.core.Tuple; import org.elasticsearch.xpack.core.security.authz.privilege.ApplicationPrivilege; import org.elasticsearch.xpack.core.security.authz.privilege.ApplicationPrivilegeDescriptor; import org.elasticsearch.xpack.core.security.support.Automatons; @@ -50,8 +50,11 @@ public final class ApplicationPermission { if (existing == null) { return new PermissionEntry(appPriv, resourceNames, patterns); } else { - return new PermissionEntry(appPriv, Sets.union(existing.resourceNames, resourceNames), - Automatons.unionAndMinimize(Arrays.asList(existing.resourceAutomaton, patterns))); + return new PermissionEntry( + appPriv, + Sets.union(existing.resourceNames, resourceNames), + Automatons.unionAndMinimize(Arrays.asList(existing.resourceAutomaton, patterns)) + ); } })); this.permissions = List.copyOf(permissionsByPrivilege.values()); @@ -97,9 +100,12 @@ public boolean grants(ApplicationPrivilege other, String resource) { * performed * @return an instance of {@link ResourcePrivilegesMap} */ - public ResourcePrivilegesMap checkResourcePrivileges(final String applicationName, Set checkForResources, - Set checkForPrivilegeNames, - Collection storedPrivileges) { + public ResourcePrivilegesMap checkResourcePrivileges( + final String applicationName, + Set checkForResources, + Set checkForPrivilegeNames, + Collection storedPrivileges + ) { final ResourcePrivilegesMap.Builder resourcePrivilegesMapBuilder = ResourcePrivilegesMap.builder(); for (String checkResource : checkForResources) { for (String checkPrivilegeName : checkForPrivilegeNames) { @@ -107,8 +113,8 @@ public ResourcePrivilegesMap checkResourcePrivileges(final String applicationNam final Set checkPrivileges = ApplicationPrivilege.get(applicationName, nameSet, storedPrivileges); logger.trace("Resolved privileges [{}] for [{},{}]", checkPrivileges, applicationName, nameSet); for (ApplicationPrivilege checkPrivilege : checkPrivileges) { - assert Automatons.predicate(applicationName).test(checkPrivilege.getApplication()) : "Privilege " + checkPrivilege + - " should have application " + applicationName; + assert Automatons.predicate(applicationName).test(checkPrivilege.getApplication()) + : "Privilege " + checkPrivilege + " should have application " + applicationName; assert checkPrivilege.name().equals(nameSet) : "Privilege " + checkPrivilege + " should have name " + nameSet; if (grants(checkPrivilege, checkResource)) { @@ -128,9 +134,7 @@ public String toString() { } public Set getApplicationNames() { - return permissions.stream() - .map(e -> e.privilege.getApplication()) - .collect(Collectors.toSet()); + return permissions.stream().map(e -> e.privilege.getApplication()).collect(Collectors.toSet()); } public Set getPrivileges(String application) { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/ClusterPermission.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/ClusterPermission.java index 4efc070343778..e65d933f38c0a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/ClusterPermission.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/ClusterPermission.java @@ -28,8 +28,7 @@ public class ClusterPermission { private final Set clusterPrivileges; private final List checks; - private ClusterPermission(final Set clusterPrivileges, - final List checks) { + private ClusterPermission(final Set clusterPrivileges, final List checks) { this.clusterPrivileges = Set.copyOf(clusterPrivileges); this.checks = List.copyOf(checks); } @@ -85,16 +84,22 @@ public static class Builder { private final List actionAutomatons = new ArrayList<>(); private final List permissionChecks = new ArrayList<>(); - public Builder add(final ClusterPrivilege clusterPrivilege, final Set allowedActionPatterns, - final Set excludeActionPatterns) { + public Builder add( + final ClusterPrivilege clusterPrivilege, + final Set allowedActionPatterns, + final Set excludeActionPatterns + ) { this.clusterPrivileges.add(clusterPrivilege); final Automaton actionAutomaton = createAutomaton(allowedActionPatterns, excludeActionPatterns); this.actionAutomatons.add(actionAutomaton); return this; } - public Builder add(final ClusterPrivilege clusterPrivilege, final Set allowedActionPatterns, - final Predicate requestPredicate) { + public Builder add( + final ClusterPrivilege clusterPrivilege, + final Set allowedActionPatterns, + final Predicate requestPredicate + ) { final Automaton actionAutomaton = createAutomaton(allowedActionPatterns, Set.of()); return add(clusterPrivilege, new ActionRequestBasedPermissionCheck(clusterPrivilege, actionAutomaton, requestPredicate)); } @@ -188,8 +193,8 @@ public final boolean check(final String action, final TransportRequest request, @Override public final boolean implies(final PermissionCheck permissionCheck) { if (permissionCheck instanceof ActionBasedPermissionCheck) { - return Operations.subsetOf(((ActionBasedPermissionCheck) permissionCheck).automaton, this.automaton) && - doImplies((ActionBasedPermissionCheck) permissionCheck); + return Operations.subsetOf(((ActionBasedPermissionCheck) permissionCheck).automaton, this.automaton) + && doImplies((ActionBasedPermissionCheck) permissionCheck); } return false; } @@ -226,8 +231,11 @@ private static class ActionRequestBasedPermissionCheck extends ActionBasedPermis private final ClusterPrivilege clusterPrivilege; private final Predicate requestPredicate; - ActionRequestBasedPermissionCheck(ClusterPrivilege clusterPrivilege, final Automaton automaton, - final Predicate requestPredicate) { + ActionRequestBasedPermissionCheck( + ClusterPrivilege clusterPrivilege, + final Automaton automaton, + final Predicate requestPredicate + ) { super(automaton); this.requestPredicate = requestPredicate; this.clusterPrivilege = clusterPrivilege; @@ -241,8 +249,7 @@ protected boolean extendedCheck(String action, TransportRequest request, Authent @Override protected boolean doImplies(final ActionBasedPermissionCheck permissionCheck) { if (permissionCheck instanceof ActionRequestBasedPermissionCheck) { - final ActionRequestBasedPermissionCheck otherCheck = - (ActionRequestBasedPermissionCheck) permissionCheck; + final ActionRequestBasedPermissionCheck otherCheck = (ActionRequestBasedPermissionCheck) permissionCheck; return this.clusterPrivilege.equals(otherCheck.clusterPrivilege); } return false; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/DocumentPermissions.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/DocumentPermissions.java index 8981261cfd7e4..fecb9231e4c9b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/DocumentPermissions.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/DocumentPermissions.java @@ -14,14 +14,14 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.lucene.search.Queries; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryRewriteContext; -import org.elasticsearch.index.query.SearchExecutionContext; import org.elasticsearch.index.query.Rewriteable; +import org.elasticsearch.index.query.SearchExecutionContext; import org.elasticsearch.index.search.NestedHelper; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.script.ScriptService; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.security.authz.support.DLSRoleQueryValidator; import org.elasticsearch.xpack.core.security.authz.support.SecurityQueryTemplateEvaluator; import org.elasticsearch.xpack.core.security.authz.support.SecurityQueryTemplateEvaluator.DlsQueryEvaluationContext; @@ -52,7 +52,6 @@ public final class DocumentPermissions implements CacheKey { private List evaluatedQueries; private List evaluatedLimitedByQueries; - private static DocumentPermissions ALLOW_ALL = new DocumentPermissions(); DocumentPermissions() { @@ -118,8 +117,12 @@ public boolean hasStoredScript() throws IOException { * @return {@link BooleanQuery} for the filter * @throws IOException thrown if there is an exception during parsing */ - public BooleanQuery filter(User user, ScriptService scriptService, ShardId shardId, - Function searchExecutionContextProvider) throws IOException { + public BooleanQuery filter( + User user, + ScriptService scriptService, + ShardId shardId, + Function searchExecutionContextProvider + ) throws IOException { if (hasDocumentLevelPermissions()) { evaluateQueries(SecurityQueryTemplateEvaluator.wrap(user, scriptService)); BooleanQuery.Builder filter; @@ -154,10 +157,12 @@ private void evaluateQueries(DlsQueryEvaluationContext context) { } } - private static void buildRoleQuery(ShardId shardId, - Function searchExecutionContextProvider, - List queries, - BooleanQuery.Builder filter) throws IOException { + private static void buildRoleQuery( + ShardId shardId, + Function searchExecutionContextProvider, + List queries, + BooleanQuery.Builder filter + ) throws IOException { for (String query : queries) { SearchExecutionContext context = searchExecutionContextProvider.apply(shardId); QueryBuilder queryBuilder = DLSRoleQueryValidator.evaluateAndVerifyRoleQuery(query, context.getXContentRegistry()); @@ -168,12 +173,10 @@ private static void buildRoleQuery(ShardId shardId, if (context.hasNested()) { NestedHelper nestedHelper = new NestedHelper(context::getObjectMapper, context::isFieldMapped); if (nestedHelper.mightMatchNestedDocs(roleQuery)) { - roleQuery = new BooleanQuery.Builder().add(roleQuery, FILTER) - .add(Queries.newNonNestedFilter(), FILTER).build(); + roleQuery = new BooleanQuery.Builder().add(roleQuery, FILTER).add(Queries.newNonNestedFilter(), FILTER).build(); } // If access is allowed on root doc then also access is allowed on all nested docs of that root document: - BitSetProducer rootDocs = context - .bitsetFilter(Queries.newNonNestedFilter()); + BitSetProducer rootDocs = context.bitsetFilter(Queries.newNonNestedFilter()); ToChildBlockJoinQuery includeNestedDocs = new ToChildBlockJoinQuery(roleQuery, rootDocs); filter.add(includeNestedDocs, SHOULD); } @@ -191,10 +194,13 @@ private static void buildRoleQuery(ShardId shardId, * the DLS query until the get thread pool has been exhausted: * https://github.com/elastic/x-plugins/issues/3145 */ - static void failIfQueryUsesClient(QueryBuilder queryBuilder, QueryRewriteContext original) - throws IOException { + static void failIfQueryUsesClient(QueryBuilder queryBuilder, QueryRewriteContext original) throws IOException { QueryRewriteContext copy = new QueryRewriteContext( - original.getXContentRegistry(), original.getWriteableRegistry(), null, original::nowInMillis); + original.getXContentRegistry(), + original.getWriteableRegistry(), + null, + original::nowInMillis + ); Rewriteable.rewrite(queryBuilder, copy); if (copy.hasAsyncActions()) { throw new IllegalStateException("role queries are not allowed to execute additional requests"); @@ -230,15 +236,14 @@ public static DocumentPermissions allowAll() { * @param limitedByDocumentPermissions {@link DocumentPermissions} used to limit the document level access * @return instance of {@link DocumentPermissions} */ - public DocumentPermissions limitDocumentPermissions( - DocumentPermissions limitedByDocumentPermissions) { - assert limitedByQueries == null - && limitedByDocumentPermissions.limitedByQueries == null : "nested scoping for document permissions is not permitted"; + public DocumentPermissions limitDocumentPermissions(DocumentPermissions limitedByDocumentPermissions) { + assert limitedByQueries == null && limitedByDocumentPermissions.limitedByQueries == null + : "nested scoping for document permissions is not permitted"; if (queries == null && limitedByDocumentPermissions.queries == null) { return DocumentPermissions.allowAll(); } // TODO: should we apply the same logic here as FieldPermissions#limitFieldPermissions, - // i.e. treat limited-by as queries if original queries is null? + // i.e. treat limited-by as queries if original queries is null? return new DocumentPermissions(queries, limitedByDocumentPermissions.queries); } @@ -267,10 +272,8 @@ public void buildCacheKey(StreamOutput out, DlsQueryEvaluationContext context) t @Override public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; DocumentPermissions that = (DocumentPermissions) o; return Objects.equals(queries, that.queries) && Objects.equals(limitedByQueries, that.limitedByQueries); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/FieldPermissions.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/FieldPermissions.java index 1dec954460ef8..d673fbb801d6b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/FieldPermissions.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/FieldPermissions.java @@ -15,10 +15,10 @@ import org.apache.lucene.util.automaton.MinimizationOperations; import org.apache.lucene.util.automaton.Operations; import org.elasticsearch.ElasticsearchSecurityException; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.regex.Regex; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xpack.core.security.authz.accesscontrol.FieldSubsetReader; import org.elasticsearch.xpack.core.security.authz.permission.FieldPermissionsDefinition.FieldGrantExcludeGroup; import org.elasticsearch.xpack.core.security.authz.support.SecurityQueryTemplateEvaluator.DlsQueryEvaluationContext; @@ -90,9 +90,11 @@ public FieldPermissions(FieldPermissionsDefinition fieldPermissionsDefinition) { /** Constructor that enables field-level security based on include/exclude rules. Exclude rules * have precedence over include rules. */ - private FieldPermissions(FieldPermissionsDefinition fieldPermissionsDefinition, - @Nullable FieldPermissionsDefinition limitedByFieldPermissionsDefinition, - Automaton permittedFieldsAutomaton) { + private FieldPermissions( + FieldPermissionsDefinition fieldPermissionsDefinition, + @Nullable FieldPermissionsDefinition limitedByFieldPermissionsDefinition, + Automaton permittedFieldsAutomaton + ) { if (permittedFieldsAutomaton.isDeterministic() == false && permittedFieldsAutomaton.getNumStates() > 1) { // we only accept deterministic automata so that the CharacterRunAutomaton constructor // directly wraps the provided automaton @@ -140,10 +142,9 @@ private static long runAutomatonRamBytesUsed(Automaton a) { public static Automaton initializePermittedFieldsAutomaton(FieldPermissionsDefinition fieldPermissionsDefinition) { Set groups = fieldPermissionsDefinition.getFieldGrantExcludeGroups(); assert groups.size() > 0 : "there must always be a single group for field inclusion/exclusion"; - List automatonList = - groups.stream() - .map(g -> FieldPermissions.buildPermittedFieldsAutomaton(g.getGrantedFields(), g.getExcludedFields())) - .collect(Collectors.toList()); + List automatonList = groups.stream() + .map(g -> FieldPermissions.buildPermittedFieldsAutomaton(g.getGrantedFields(), g.getExcludedFields())) + .collect(Collectors.toList()); return Automatons.unionAndMinimize(automatonList); } @@ -173,9 +174,13 @@ public static Automaton buildPermittedFieldsAutomaton(final String[] grantedFiel deniedFieldsAutomaton = MinimizationOperations.minimize(deniedFieldsAutomaton, Operations.DEFAULT_DETERMINIZE_WORK_LIMIT); if (subsetOf(deniedFieldsAutomaton, grantedFieldsAutomaton) == false) { - throw new ElasticsearchSecurityException("Exceptions for field permissions must be a subset of the " + - "granted fields but " + Strings.arrayToCommaDelimitedString(deniedFields) + " is not a subset of " + - Strings.arrayToCommaDelimitedString(grantedFields)); + throw new ElasticsearchSecurityException( + "Exceptions for field permissions must be a subset of the " + + "granted fields but " + + Strings.arrayToCommaDelimitedString(deniedFields) + + " is not a subset of " + + Strings.arrayToCommaDelimitedString(grantedFields) + ); } grantedFieldsAutomaton = Automatons.minusAndMinimize(grantedFieldsAutomaton, deniedFieldsAutomaton); @@ -250,10 +255,8 @@ Automaton getIncludeAutomaton() { @Override public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; FieldPermissions that = (FieldPermissions) o; return permittedFieldsAutomatonIsTotal == that.permittedFieldsAutomatonIsTotal && fieldPermissionsDefinition.equals(that.fieldPermissionsDefinition) diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/FieldPermissionsCache.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/FieldPermissionsCache.java index 7dc465dc30298..2845120c5246a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/FieldPermissionsCache.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/FieldPermissionsCache.java @@ -34,14 +34,18 @@ public final class FieldPermissionsCache { public static final Setting CACHE_SIZE_SETTING = Setting.longSetting( - setting("authz.store.roles.field_permissions.cache.max_size_in_bytes"), 100 * 1024 * 1024, -1L, Property.NodeScope); + setting("authz.store.roles.field_permissions.cache.max_size_in_bytes"), + 100 * 1024 * 1024, + -1L, + Property.NodeScope + ); private final Cache cache; public FieldPermissionsCache(Settings settings) { this.cache = CacheBuilder.builder() - .setMaximumWeight(CACHE_SIZE_SETTING.get(settings)) - .weigher((key, fieldPermissions) -> fieldPermissions.ramBytesUsed()) - .build(); + .setMaximumWeight(CACHE_SIZE_SETTING.get(settings)) + .weigher((key, fieldPermissions) -> fieldPermissions.ramBytesUsed()) + .build(); } /** @@ -58,8 +62,10 @@ FieldPermissions getFieldPermissions(String[] granted, String[] denied) { */ public FieldPermissions getFieldPermissions(FieldPermissionsDefinition fieldPermissionsDefinition) { try { - return cache.computeIfAbsent(fieldPermissionsDefinition, - (key) -> new FieldPermissions(key, FieldPermissions.initializePermittedFieldsAutomaton(key))); + return cache.computeIfAbsent( + fieldPermissionsDefinition, + (key) -> new FieldPermissions(key, FieldPermissions.initializePermittedFieldsAutomaton(key)) + ); } catch (ExecutionException e) { throw new ElasticsearchException("unable to compute field permissions", e); } @@ -71,19 +77,19 @@ public FieldPermissions getFieldPermissions(FieldPermissionsDefinition fieldPerm */ FieldPermissions getFieldPermissions(Collection fieldPermissionsCollection) { Optional allowAllFieldPermissions = fieldPermissionsCollection.stream() - .filter(((Predicate) (FieldPermissions::hasFieldLevelSecurity)).negate()) - .findFirst(); + .filter(((Predicate) (FieldPermissions::hasFieldLevelSecurity)).negate()) + .findFirst(); return allowAllFieldPermissions.orElseGet(() -> { final Set fieldGrantExcludeGroups = new HashSet<>(); for (FieldPermissions fieldPermissions : fieldPermissionsCollection) { final FieldPermissionsDefinition definition = fieldPermissions.getFieldPermissionsDefinition(); - final FieldPermissionsDefinition limitedByDefinition = - fieldPermissions.getLimitedByFieldPermissionsDefinition(); + final FieldPermissionsDefinition limitedByDefinition = fieldPermissions.getLimitedByFieldPermissionsDefinition(); if (definition == null) { throw new IllegalArgumentException("Expected field permission definition, but found null"); } else if (limitedByDefinition != null) { - throw new IllegalArgumentException("Expected no limited-by field permission definition, but found [" - + limitedByDefinition + "]"); + throw new IllegalArgumentException( + "Expected no limited-by field permission definition, but found [" + limitedByDefinition + "]" + ); } fieldGrantExcludeGroups.addAll(definition.getFieldGrantExcludeGroups()); } @@ -91,8 +97,8 @@ FieldPermissions getFieldPermissions(Collection fieldPermissio try { return cache.computeIfAbsent(combined, (key) -> { List automatonList = fieldPermissionsCollection.stream() - .map(FieldPermissions::getIncludeAutomaton) - .collect(Collectors.toList()); + .map(FieldPermissions::getIncludeAutomaton) + .collect(Collectors.toList()); return new FieldPermissions(key, Automatons.unionAndMinimize(automatonList)); }); } catch (ExecutionException e) { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/FieldPermissionsDefinition.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/FieldPermissionsDefinition.java index 36094f3c029ce..2c59178779281 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/FieldPermissionsDefinition.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/FieldPermissionsDefinition.java @@ -104,8 +104,10 @@ public int hashCode() { @Override public String toString() { return getClass().getSimpleName() - + "[grant=" + Strings.arrayToCommaDelimitedString(grantedFields) - + "; exclude=" + Strings.arrayToCommaDelimitedString(excludedFields) + + "[grant=" + + Strings.arrayToCommaDelimitedString(grantedFields) + + "; exclude=" + + Strings.arrayToCommaDelimitedString(excludedFields) + "]"; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/IndicesPermission.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/IndicesPermission.java index 9def76ffbd187..646f52d3a5e02 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/IndicesPermission.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/IndicesPermission.java @@ -68,11 +68,13 @@ public Builder(Automaton restrictedNamesAutomaton) { this.restrictedNamesAutomaton = restrictedNamesAutomaton; } - public Builder addGroup(IndexPrivilege privilege, - FieldPermissions fieldPermissions, - @Nullable Set query, - boolean allowRestrictedIndices, - String... indices) { + public Builder addGroup( + IndexPrivilege privilege, + FieldPermissions fieldPermissions, + @Nullable Set query, + boolean allowRestrictedIndices, + String... indices + ) { groups.add(new Group(privilege, fieldPermissions, query, allowRestrictedIndices, restrictedNamesAutomaton, indices)); return this; } @@ -158,11 +160,10 @@ private Predicate buildIndexMatcherPredicateForAction(String a } final StringMatcher nameMatcher = indexMatcher(ordinaryIndices, restrictedIndices); final StringMatcher bwcSpecialCaseMatcher = indexMatcher(grantMappingUpdatesOnIndices, grantMappingUpdatesOnRestrictedIndices); - return indexAbstraction -> - nameMatcher.test(indexAbstraction.getName()) || - (indexAbstraction.getType() != IndexAbstraction.Type.DATA_STREAM && - (indexAbstraction.getParentDataStream() == null) && - bwcSpecialCaseMatcher.test(indexAbstraction.getName())); + return indexAbstraction -> nameMatcher.test(indexAbstraction.getName()) + || (indexAbstraction.getType() != IndexAbstraction.Type.DATA_STREAM + && (indexAbstraction.getParentDataStream() == null) + && bwcSpecialCaseMatcher.test(indexAbstraction.getName())); } /** @@ -191,8 +192,11 @@ public boolean check(String action) { * @param checkForPrivileges check permission grants for the set of index privileges * @return an instance of {@link ResourcePrivilegesMap} */ - public ResourcePrivilegesMap checkResourcePrivileges(Set checkForIndexPatterns, boolean allowRestrictedIndices, - Set checkForPrivileges) { + public ResourcePrivilegesMap checkResourcePrivileges( + Set checkForIndexPatterns, + boolean allowRestrictedIndices, + Set checkForPrivileges + ) { final ResourcePrivilegesMap.Builder resourcePrivilegesMapBuilder = ResourcePrivilegesMap.builder(); final Map predicateCache = new HashMap<>(); for (String forIndexPattern : checkForIndexPatterns) { @@ -206,8 +210,9 @@ public ResourcePrivilegesMap checkResourcePrivileges(Set checkForIndexPa final Automaton groupIndexAutomaton = predicateCache.computeIfAbsent(group, Group::getIndexMatcherAutomaton); if (Operations.subsetOf(checkIndexAutomaton, groupIndexAutomaton)) { if (allowedIndexPrivilegesAutomaton != null) { - allowedIndexPrivilegesAutomaton = Automatons - .unionAndMinimize(Arrays.asList(allowedIndexPrivilegesAutomaton, group.privilege().getAutomaton())); + allowedIndexPrivilegesAutomaton = Automatons.unionAndMinimize( + Arrays.asList(allowedIndexPrivilegesAutomaton, group.privilege().getAutomaton()) + ); } else { allowedIndexPrivilegesAutomaton = group.privilege().getAutomaton(); } @@ -216,7 +221,7 @@ public ResourcePrivilegesMap checkResourcePrivileges(Set checkForIndexPa for (String privilege : checkForPrivileges) { IndexPrivilege indexPrivilege = IndexPrivilege.get(Collections.singleton(privilege)); if (allowedIndexPrivilegesAutomaton != null - && Operations.subsetOf(indexPrivilege.getAutomaton(), allowedIndexPrivilegesAutomaton)) { + && Operations.subsetOf(indexPrivilege.getAutomaton(), allowedIndexPrivilegesAutomaton)) { resourcePrivilegesMapBuilder.addResourcePrivilege(forIndexPattern, privilege, Boolean.TRUE); } else { resourcePrivilegesMapBuilder.addResourcePrivilege(forIndexPattern, privilege, Boolean.FALSE); @@ -266,11 +271,8 @@ private static class IndexResource { private IndexResource(String name, @Nullable IndexAbstraction abstraction) { assert name != null : "Resource name cannot be null"; - assert abstraction == null || abstraction.getName().equals(name) : "Index abstraction has unexpected name [" - + abstraction.getName() - + "] vs [" - + name - + "]"; + assert abstraction == null || abstraction.getName().equals(name) + : "Index abstraction has unexpected name [" + abstraction.getName() + "] vs [" + name + "]"; this.name = name; this.indexAbstraction = abstraction; } @@ -441,13 +443,21 @@ public IndicesAccessControl authorize( if (false == actionCheck) { for (String privilegeName : group.privilege.name()) { if (PRIVILEGE_NAME_SET_BWC_ALLOW_MAPPING_UPDATE.contains(privilegeName)) { - bwcDeprecationLogActions.add(() -> - deprecationLogger.critical(DeprecationCategory.SECURITY, - "[" + resource.name + "] mapping update for ingest privilege [" + - privilegeName + "]", "the index privilege [" + privilegeName + "] allowed the update " + - "mapping action [" + action + "] on index [" + resource.name + "], this privilege " + - "will not permit mapping updates in the next major release - users who require access " + - "to update mappings must be granted explicit privileges") + bwcDeprecationLogActions.add( + () -> deprecationLogger.critical( + DeprecationCategory.SECURITY, + "[" + resource.name + "] mapping update for ingest privilege [" + privilegeName + "]", + "the index privilege [" + + privilegeName + + "] allowed the update " + + "mapping action [" + + action + + "] on index [" + + resource.name + + "], this privilege " + + "will not permit mapping updates in the next major release - users who require access " + + "to update mappings must be granted explicit privileges" + ) ); } } @@ -488,16 +498,23 @@ public IndicesAccessControl authorize( final FieldPermissions fieldPermissions; final Set indexFieldPermissions = fieldPermissionsByIndex.get(index); if (indexFieldPermissions != null && indexFieldPermissions.isEmpty() == false) { - fieldPermissions = indexFieldPermissions.size() == 1 ? indexFieldPermissions.iterator().next() : - fieldPermissionsCache.getFieldPermissions(indexFieldPermissions); + fieldPermissions = indexFieldPermissions.size() == 1 + ? indexFieldPermissions.iterator().next() + : fieldPermissionsCache.getFieldPermissions(indexFieldPermissions); } else { fieldPermissions = FieldPermissions.DEFAULT; } if (entry.getValue() == false) { overallGranted = false; } - indexPermissions.put(index, new IndicesAccessControl.IndexAccessControl(entry.getValue(), fieldPermissions, - (roleQueries != null) ? DocumentPermissions.filteredBy(roleQueries) : DocumentPermissions.allowAll())); + indexPermissions.put( + index, + new IndicesAccessControl.IndexAccessControl( + entry.getValue(), + fieldPermissions, + (roleQueries != null) ? DocumentPermissions.filteredBy(roleQueries) : DocumentPermissions.allowAll() + ) + ); } return new IndicesAccessControl(overallGranted, unmodifiableMap(indexPermissions)); } @@ -532,8 +549,14 @@ public static class Group { // to be covered by the "indices" private final boolean allowRestrictedIndices; - public Group(IndexPrivilege privilege, FieldPermissions fieldPermissions, @Nullable Set query, - boolean allowRestrictedIndices, Automaton restrictedNamesAutomaton, String... indices) { + public Group( + IndexPrivilege privilege, + FieldPermissions fieldPermissions, + @Nullable Set query, + boolean allowRestrictedIndices, + Automaton restrictedNamesAutomaton, + String... indices + ) { assert indices.length != 0; this.privilege = privilege; this.actionMatcher = privilege.predicate(); @@ -545,10 +568,11 @@ public Group(IndexPrivilege privilege, FieldPermissions fieldPermissions, @Nulla this.indexNameAutomaton = () -> indexNameAutomatonMemo.computeIfAbsent(indices, k -> Automatons.patterns(indices)); } else { final CharacterRunAutomaton restrictedNamesRunAutomaton = new CharacterRunAutomaton(restrictedNamesAutomaton); - this.indexNameMatcher = StringMatcher.of(indices) - .and(name -> restrictedNamesRunAutomaton.run(name) == false); - this.indexNameAutomaton = () -> indexNameAutomatonMemo.computeIfAbsent(indices, - k -> Automatons.minusAndMinimize(Automatons.patterns(indices), restrictedNamesAutomaton)); + this.indexNameMatcher = StringMatcher.of(indices).and(name -> restrictedNamesRunAutomaton.run(name) == false); + this.indexNameAutomaton = () -> indexNameAutomatonMemo.computeIfAbsent( + indices, + k -> Automatons.minusAndMinimize(Automatons.patterns(indices), restrictedNamesAutomaton) + ); } this.fieldPermissions = Objects.requireNonNull(fieldPermissions); this.query = query; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/LimitedRole.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/LimitedRole.java index b17704ba15389..43e2e47e78c7b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/LimitedRole.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/LimitedRole.java @@ -30,8 +30,13 @@ public final class LimitedRole extends Role { private final Role limitedBy; - LimitedRole(ClusterPermission cluster, IndicesPermission indices, ApplicationPermission application, RunAsPermission runAs, - Role limitedBy) { + LimitedRole( + ClusterPermission cluster, + IndicesPermission indices, + ApplicationPermission application, + RunAsPermission runAs, + Role limitedBy + ) { super(Objects.requireNonNull(limitedBy, "limiting role is required").names(), cluster, indices, application, runAs); this.limitedBy = limitedBy; } @@ -82,13 +87,24 @@ public int hashCode() { } @Override - public IndicesAccessControl authorize(String action, Set requestedIndicesOrAliases, - Map aliasAndIndexLookup, - FieldPermissionsCache fieldPermissionsCache) { - IndicesAccessControl indicesAccessControl = - super.authorize(action, requestedIndicesOrAliases, aliasAndIndexLookup, fieldPermissionsCache); - IndicesAccessControl limitedByIndicesAccessControl = limitedBy.authorize(action, requestedIndicesOrAliases, aliasAndIndexLookup, - fieldPermissionsCache); + public IndicesAccessControl authorize( + String action, + Set requestedIndicesOrAliases, + Map aliasAndIndexLookup, + FieldPermissionsCache fieldPermissionsCache + ) { + IndicesAccessControl indicesAccessControl = super.authorize( + action, + requestedIndicesOrAliases, + aliasAndIndexLookup, + fieldPermissionsCache + ); + IndicesAccessControl limitedByIndicesAccessControl = limitedBy.authorize( + action, + requestedIndicesOrAliases, + aliasAndIndexLookup, + fieldPermissionsCache + ); return indicesAccessControl.limitIndicesAccessControl(limitedByIndicesAccessControl); } @@ -133,12 +149,18 @@ public boolean checkIndicesAction(String action) { * @return an instance of {@link ResourcePrivilegesMap} */ @Override - public ResourcePrivilegesMap checkIndicesPrivileges(Set checkForIndexPatterns, boolean allowRestrictedIndices, - Set checkForPrivileges) { - ResourcePrivilegesMap resourcePrivilegesMap = super.indices().checkResourcePrivileges(checkForIndexPatterns, allowRestrictedIndices, - checkForPrivileges); - ResourcePrivilegesMap resourcePrivilegesMapForLimitedRole = limitedBy.indices().checkResourcePrivileges(checkForIndexPatterns, - allowRestrictedIndices, checkForPrivileges); + public ResourcePrivilegesMap checkIndicesPrivileges( + Set checkForIndexPatterns, + boolean allowRestrictedIndices, + Set checkForPrivileges + ) { + ResourcePrivilegesMap resourcePrivilegesMap = super.indices().checkResourcePrivileges( + checkForIndexPatterns, + allowRestrictedIndices, + checkForPrivileges + ); + ResourcePrivilegesMap resourcePrivilegesMapForLimitedRole = limitedBy.indices() + .checkResourcePrivileges(checkForIndexPatterns, allowRestrictedIndices, checkForPrivileges); return ResourcePrivilegesMap.intersection(resourcePrivilegesMap, resourcePrivilegesMapForLimitedRole); } @@ -183,13 +205,20 @@ public boolean grants(ClusterPrivilege clusterPrivilege) { * @return an instance of {@link ResourcePrivilegesMap} */ @Override - public ResourcePrivilegesMap checkApplicationResourcePrivileges(final String applicationName, Set checkForResources, - Set checkForPrivilegeNames, - Collection storedPrivileges) { - ResourcePrivilegesMap resourcePrivilegesMap = super.application().checkResourcePrivileges(applicationName, checkForResources, - checkForPrivilegeNames, storedPrivileges); - ResourcePrivilegesMap resourcePrivilegesMapForLimitedRole = limitedBy.application().checkResourcePrivileges(applicationName, - checkForResources, checkForPrivilegeNames, storedPrivileges); + public ResourcePrivilegesMap checkApplicationResourcePrivileges( + final String applicationName, + Set checkForResources, + Set checkForPrivilegeNames, + Collection storedPrivileges + ) { + ResourcePrivilegesMap resourcePrivilegesMap = super.application().checkResourcePrivileges( + applicationName, + checkForResources, + checkForPrivilegeNames, + storedPrivileges + ); + ResourcePrivilegesMap resourcePrivilegesMapForLimitedRole = limitedBy.application() + .checkResourcePrivileges(applicationName, checkForResources, checkForPrivilegeNames, storedPrivileges); return ResourcePrivilegesMap.intersection(resourcePrivilegesMap, resourcePrivilegesMapForLimitedRole); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/ResourcePrivilegesMap.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/ResourcePrivilegesMap.java index 4b1901a9390a6..ac7d18a2a4a85 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/ResourcePrivilegesMap.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/ResourcePrivilegesMap.java @@ -66,8 +66,8 @@ public static final class Builder { private Map resourceToResourcePrivilegesBuilder = new LinkedHashMap<>(); public Builder addResourcePrivilege(String resource, String privilege, Boolean allowed) { - assert resource != null && privilege != null - && allowed != null : "resource, privilege and permission(allowed or denied) are required"; + assert resource != null && privilege != null && allowed != null + : "resource, privilege and permission(allowed or denied) are required"; ResourcePrivileges.Builder builder = resourceToResourcePrivilegesBuilder.computeIfAbsent(resource, ResourcePrivileges::builder); builder.addPrivilege(privilege, allowed); allowAll = allowAll && allowed; @@ -83,14 +83,17 @@ public Builder addResourcePrivilege(String resource, Map privil } public Builder addResourcePrivilegesMap(ResourcePrivilegesMap resourcePrivilegesMap) { - resourcePrivilegesMap.getResourceToResourcePrivileges().entrySet().stream() - .forEach(e -> this.addResourcePrivilege(e.getKey(), e.getValue().getPrivileges())); + resourcePrivilegesMap.getResourceToResourcePrivileges() + .entrySet() + .stream() + .forEach(e -> this.addResourcePrivilege(e.getKey(), e.getValue().getPrivileges())); return this; } public ResourcePrivilegesMap build() { - Map result = resourceToResourcePrivilegesBuilder.entrySet().stream() - .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue().build())); + Map result = resourceToResourcePrivilegesBuilder.entrySet() + .stream() + .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue().build())); return new ResourcePrivilegesMap(allowAll, result); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/Role.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/Role.java index c314dc98ff14f..9fdb2c91f01d6 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/Role.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/Role.java @@ -118,7 +118,6 @@ public boolean checkIndicesAction(String action) { return indices.check(action); } - /** * For given index patterns and index privileges determines allowed privileges and creates an instance of {@link ResourcePrivilegesMap} * holding a map of resource to {@link ResourcePrivileges} where resource is index pattern and the map of index privilege to whether it @@ -129,8 +128,11 @@ public boolean checkIndicesAction(String action) { * @param checkForPrivileges check permission grants for the set of index privileges * @return an instance of {@link ResourcePrivilegesMap} */ - public ResourcePrivilegesMap checkIndicesPrivileges(Set checkForIndexPatterns, boolean allowRestrictedIndices, - Set checkForPrivileges) { + public ResourcePrivilegesMap checkIndicesPrivileges( + Set checkForIndexPatterns, + boolean allowRestrictedIndices, + Set checkForPrivileges + ) { return indices.checkResourcePrivileges(checkForIndexPatterns, allowRestrictedIndices, checkForPrivileges); } @@ -169,9 +171,12 @@ public boolean grants(ClusterPrivilege clusterPrivilege) { * performed * @return an instance of {@link ResourcePrivilegesMap} */ - public ResourcePrivilegesMap checkApplicationResourcePrivileges(final String applicationName, Set checkForResources, - Set checkForPrivilegeNames, - Collection storedPrivileges) { + public ResourcePrivilegesMap checkApplicationResourcePrivileges( + final String applicationName, + Set checkForResources, + Set checkForPrivilegeNames, + Collection storedPrivileges + ) { return application.checkResourcePrivileges(applicationName, checkForResources, checkForPrivilegeNames, storedPrivileges); } @@ -180,9 +185,12 @@ public ResourcePrivilegesMap checkApplicationResourcePrivileges(final String app * specified action with the requested indices/aliases. At the same time if field and/or document level security * is configured for any group also the allowed fields and role queries are resolved. */ - public IndicesAccessControl authorize(String action, Set requestedIndicesOrAliases, - Map aliasAndIndexLookup, - FieldPermissionsCache fieldPermissionsCache) { + public IndicesAccessControl authorize( + String action, + Set requestedIndicesOrAliases, + Map aliasAndIndexLookup, + FieldPermissionsCache fieldPermissionsCache + ) { return indices.authorize(action, requestedIndicesOrAliases, aliasAndIndexLookup, fieldPermissionsCache); } @@ -264,8 +272,13 @@ public Builder add(IndexPrivilege privilege, String... indices) { return this; } - public Builder add(FieldPermissions fieldPermissions, Set query, IndexPrivilege privilege, - boolean allowRestrictedIndices, String... indices) { + public Builder add( + FieldPermissions fieldPermissions, + Set query, + IndexPrivilege privilege, + boolean allowRestrictedIndices, + String... indices + ) { groups.add(new IndicesPermissionGroupDefinition(privilege, fieldPermissions, query, allowRestrictedIndices, indices)); return this; } @@ -282,18 +295,26 @@ public Role build() { } else { IndicesPermission.Builder indicesBuilder = new IndicesPermission.Builder(restrictedNamesAutomaton); for (IndicesPermissionGroupDefinition group : groups) { - indicesBuilder.addGroup(group.privilege, group.fieldPermissions, group.query, group.allowRestrictedIndices, - group.indices); + indicesBuilder.addGroup( + group.privilege, + group.fieldPermissions, + group.query, + group.allowRestrictedIndices, + group.indices + ); } indices = indicesBuilder.build(); } - final ApplicationPermission applicationPermission - = applicationPrivs.isEmpty() ? ApplicationPermission.NONE : new ApplicationPermission(applicationPrivs); + final ApplicationPermission applicationPermission = applicationPrivs.isEmpty() + ? ApplicationPermission.NONE + : new ApplicationPermission(applicationPrivs); return new Role(names, cluster, indices, applicationPermission, runAs); } - static List convertFromIndicesPrivileges(RoleDescriptor.IndicesPrivileges[] indicesPrivileges, - @Nullable FieldPermissionsCache fieldPermissionsCache) { + static List convertFromIndicesPrivileges( + RoleDescriptor.IndicesPrivileges[] indicesPrivileges, + @Nullable FieldPermissionsCache fieldPermissionsCache + ) { List list = new ArrayList<>(indicesPrivileges.length); for (RoleDescriptor.IndicesPrivileges privilege : indicesPrivileges) { final FieldPermissions fieldPermissions; @@ -301,20 +322,28 @@ static List convertFromIndicesPrivileges(RoleD fieldPermissions = fieldPermissionsCache.getFieldPermissions(privilege.getGrantedFields(), privilege.getDeniedFields()); } else { fieldPermissions = new FieldPermissions( - new FieldPermissionsDefinition(privilege.getGrantedFields(), privilege.getDeniedFields())); + new FieldPermissionsDefinition(privilege.getGrantedFields(), privilege.getDeniedFields()) + ); } final Set query = privilege.getQuery() == null ? null : Collections.singleton(privilege.getQuery()); - list.add(new IndicesPermissionGroupDefinition(IndexPrivilege.get(Sets.newHashSet(privilege.getPrivileges())), - fieldPermissions, query, privilege.allowRestrictedIndices(), privilege.getIndices())); + list.add( + new IndicesPermissionGroupDefinition( + IndexPrivilege.get(Sets.newHashSet(privilege.getPrivileges())), + fieldPermissions, + query, + privilege.allowRestrictedIndices(), + privilege.getIndices() + ) + ); } return list; } static Tuple> convertApplicationPrivilege(RoleDescriptor.ApplicationResourcePrivileges arp) { - return new Tuple<>(new ApplicationPrivilege(arp.getApplication(), - Sets.newHashSet(arp.getPrivileges()), - arp.getPrivileges() - ), Sets.newHashSet(arp.getResources())); + return new Tuple<>( + new ApplicationPrivilege(arp.getApplication(), Sets.newHashSet(arp.getPrivileges()), arp.getPrivileges()), + Sets.newHashSet(arp.getResources()) + ); } private static class IndicesPermissionGroupDefinition { @@ -324,11 +353,13 @@ private static class IndicesPermissionGroupDefinition { private final boolean allowRestrictedIndices; private final String[] indices; - private IndicesPermissionGroupDefinition(IndexPrivilege privilege, - FieldPermissions fieldPermissions, - @Nullable Set query, - boolean allowRestrictedIndices, - String... indices) { + private IndicesPermissionGroupDefinition( + IndexPrivilege privilege, + FieldPermissions fieldPermissions, + @Nullable Set query, + boolean allowRestrictedIndices, + String... indices + ) { this.privilege = privilege; this.fieldPermissions = fieldPermissions; this.query = query; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/privilege/ApplicationPrivilege.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/privilege/ApplicationPrivilege.java index 9acb447422542..8544c00163dc9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/privilege/ApplicationPrivilege.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/privilege/ApplicationPrivilege.java @@ -109,8 +109,9 @@ private static void validateApplicationName(String application, boolean allowWil return; } if (asterisk != application.length() - 1) { - throw new IllegalArgumentException("Application name patterns only support trailing wildcards (found '" + application - + "')"); + throw new IllegalArgumentException( + "Application name patterns only support trailing wildcards (found '" + application + "')" + ); } } if (WHITESPACE.matcher(application).find()) { @@ -123,8 +124,9 @@ private static void validateApplicationName(String application, boolean allowWil prefix = prefix.substring(0, prefix.length() - 1); } if (VALID_APPLICATION_PREFIX.matcher(prefix).matches() == false) { - throw new IllegalArgumentException("An application name prefix must match the pattern " + VALID_APPLICATION_PREFIX.pattern() - + " (found '" + prefix + "')"); + throw new IllegalArgumentException( + "An application name prefix must match the pattern " + VALID_APPLICATION_PREFIX.pattern() + " (found '" + prefix + "')" + ); } if (prefix.length() < 3 && asterisk == -1) { throw new IllegalArgumentException("An application name prefix must be at least 3 characters long (found '" + prefix + "')"); @@ -136,8 +138,13 @@ private static void validateApplicationName(String application, boolean allowWil suffix = suffix.substring(0, suffix.length() - 1); } if (Strings.validFileName(suffix) == false) { - throw new IllegalArgumentException("An application name suffix may not contain any of the characters '" + - Strings.collectionToDelimitedString(Strings.INVALID_FILENAME_CHARS, "") + "' (found '" + suffix + "')"); + throw new IllegalArgumentException( + "An application name suffix may not contain any of the characters '" + + Strings.collectionToDelimitedString(Strings.INVALID_FILENAME_CHARS, "") + + "' (found '" + + suffix + + "')" + ); } } } @@ -149,8 +156,9 @@ private static void validateApplicationName(String application, boolean allowWil */ public static void validatePrivilegeName(String name) { if (isValidPrivilegeName(name) == false) { - throw new IllegalArgumentException("Application privilege names must match the pattern " + VALID_NAME.pattern() - + " (found '" + name + "')"); + throw new IllegalArgumentException( + "Application privilege names must match the pattern " + VALID_NAME.pattern() + " (found '" + name + "')" + ); } } @@ -165,8 +173,13 @@ private static boolean isValidPrivilegeName(String name) { */ public static void validatePrivilegeOrActionName(String name) { if (VALID_NAME_OR_ACTION.matcher(name).matches() == false) { - throw new IllegalArgumentException("Application privilege names and actions must match the pattern " - + VALID_NAME_OR_ACTION.pattern() + " (found '" + name + "')"); + throw new IllegalArgumentException( + "Application privilege names and actions must match the pattern " + + VALID_NAME_OR_ACTION.pattern() + + " (found '" + + name + + "')" + ); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/privilege/ApplicationPrivilegeDescriptor.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/privilege/ApplicationPrivilegeDescriptor.java index 6df9c24cf0181..93cc006f04ddc 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/privilege/ApplicationPrivilegeDescriptor.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/privilege/ApplicationPrivilegeDescriptor.java @@ -7,11 +7,11 @@ package org.elasticsearch.xpack.core.security.authz.privilege; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -43,8 +43,11 @@ public class ApplicationPrivilegeDescriptor implements ToXContentObject, Writeab PARSER.declareString(Builder::privilegeName, Fields.NAME); PARSER.declareStringArray(Builder::actions, Fields.ACTIONS); PARSER.declareObject(Builder::metadata, (parser, context) -> parser.map(), Fields.METADATA); - PARSER.declareField((parser, builder, allowType) -> builder.type(parser.text(), allowType), Fields.TYPE, - ObjectParser.ValueType.STRING); + PARSER.declareField( + (parser, builder, allowType) -> builder.type(parser.text(), allowType), + Fields.TYPE, + ObjectParser.ValueType.STRING + ); } private String application; @@ -119,8 +122,12 @@ public String toString() { * @param defaultName The privilege name to use if none is specified in the XContent body * @param allowType If true, accept a "type" field (for which the value must match {@link #DOC_TYPE_VALUE}); */ - public static ApplicationPrivilegeDescriptor parse(XContentParser parser, String defaultApplication, String defaultName, - boolean allowType) throws IOException { + public static ApplicationPrivilegeDescriptor parse( + XContentParser parser, + String defaultApplication, + String defaultName, + boolean allowType + ) throws IOException { final Builder builder = PARSER.parse(parser, allowType); if (builder.applicationName == null) { builder.applicationName(defaultApplication); @@ -140,10 +147,10 @@ public boolean equals(Object o) { return false; } final ApplicationPrivilegeDescriptor that = (ApplicationPrivilegeDescriptor) o; - return Objects.equals(this.application, that.application) && - Objects.equals(this.name, that.name) && - Objects.equals(this.actions, that.actions) && - Objects.equals(this.metadata, that.metadata); + return Objects.equals(this.application, that.application) + && Objects.equals(this.name, that.name) + && Objects.equals(this.actions, that.actions) + && Objects.equals(this.metadata, that.metadata); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/privilege/ClusterPrivilegeResolver.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/privilege/ClusterPrivilegeResolver.java index fce84423e6339..ba6fa1bf79003 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/privilege/ClusterPrivilegeResolver.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/privilege/ClusterPrivilegeResolver.java @@ -53,8 +53,12 @@ public class ClusterPrivilegeResolver { // shared automatons private static final Set ALL_SECURITY_PATTERN = Set.of("cluster:admin/xpack/security/*"); - private static final Set MANAGE_SAML_PATTERN = Set.of("cluster:admin/xpack/security/saml/*", - InvalidateTokenAction.NAME, RefreshTokenAction.NAME, SamlSpMetadataAction.NAME); + private static final Set MANAGE_SAML_PATTERN = Set.of( + "cluster:admin/xpack/security/saml/*", + InvalidateTokenAction.NAME, + RefreshTokenAction.NAME, + SamlSpMetadataAction.NAME + ); private static final Set MANAGE_OIDC_PATTERN = Set.of("cluster:admin/xpack/security/oidc/*"); private static final Set MANAGE_TOKEN_PATTERN = Set.of("cluster:admin/xpack/security/token/*"); private static final Set MANAGE_API_KEY_PATTERN = Set.of("cluster:admin/xpack/security/api_key/*"); @@ -66,29 +70,56 @@ public class ClusterPrivilegeResolver { private static final Set MONITOR_TRANSFORM_PATTERN = Set.of("cluster:monitor/data_frame/*", "cluster:monitor/transform/*"); private static final Set MONITOR_WATCHER_PATTERN = Set.of("cluster:monitor/xpack/watcher/*"); private static final Set MONITOR_ROLLUP_PATTERN = Set.of("cluster:monitor/xpack/rollup/*"); - private static final Set ALL_CLUSTER_PATTERN = Set.of("cluster:*", "indices:admin/template/*", "indices:admin/index_template/*", - "indices:admin/data_stream/*"); + private static final Set ALL_CLUSTER_PATTERN = Set.of( + "cluster:*", + "indices:admin/template/*", + "indices:admin/index_template/*", + "indices:admin/data_stream/*" + ); private static final Set MANAGE_ML_PATTERN = Set.of("cluster:admin/xpack/ml/*", "cluster:monitor/xpack/ml/*"); - private static final Set MANAGE_TRANSFORM_PATTERN = Set.of("cluster:admin/data_frame/*", "cluster:monitor/data_frame/*", - "cluster:monitor/transform/*", "cluster:admin/transform/*"); + private static final Set MANAGE_TRANSFORM_PATTERN = Set.of( + "cluster:admin/data_frame/*", + "cluster:monitor/data_frame/*", + "cluster:monitor/transform/*", + "cluster:admin/transform/*" + ); private static final Set MANAGE_WATCHER_PATTERN = Set.of("cluster:admin/xpack/watcher/*", "cluster:monitor/xpack/watcher/*"); private static final Set TRANSPORT_CLIENT_PATTERN = Set.of("cluster:monitor/nodes/liveness", "cluster:monitor/state"); - private static final Set MANAGE_IDX_TEMPLATE_PATTERN = Set.of("indices:admin/template/*", "indices:admin/index_template/*", - "cluster:admin/component_template/*"); + private static final Set MANAGE_IDX_TEMPLATE_PATTERN = Set.of( + "indices:admin/template/*", + "indices:admin/index_template/*", + "cluster:admin/component_template/*" + ); private static final Set MANAGE_INGEST_PIPELINE_PATTERN = Set.of("cluster:admin/ingest/pipeline/*"); private static final Set READ_PIPELINE_PATTERN = Set.of(GetPipelineAction.NAME, SimulatePipelineAction.NAME); private static final Set MANAGE_ROLLUP_PATTERN = Set.of("cluster:admin/xpack/rollup/*", "cluster:monitor/xpack/rollup/*"); - private static final Set MANAGE_CCR_PATTERN = - Set.of("cluster:admin/xpack/ccr/*", ClusterStateAction.NAME, HasPrivilegesAction.NAME); - private static final Set CREATE_SNAPSHOT_PATTERN = Set.of(CreateSnapshotAction.NAME, SnapshotsStatusAction.NAME + "*", - GetSnapshotsAction.NAME, SnapshotsStatusAction.NAME, GetRepositoriesAction.NAME); - private static final Set MONITOR_SNAPSHOT_PATTERN = Set.of(SnapshotsStatusAction.NAME + "*", GetSnapshotsAction.NAME, - SnapshotsStatusAction.NAME, GetRepositoriesAction.NAME); + private static final Set MANAGE_CCR_PATTERN = Set.of( + "cluster:admin/xpack/ccr/*", + ClusterStateAction.NAME, + HasPrivilegesAction.NAME + ); + private static final Set CREATE_SNAPSHOT_PATTERN = Set.of( + CreateSnapshotAction.NAME, + SnapshotsStatusAction.NAME + "*", + GetSnapshotsAction.NAME, + SnapshotsStatusAction.NAME, + GetRepositoriesAction.NAME + ); + private static final Set MONITOR_SNAPSHOT_PATTERN = Set.of( + SnapshotsStatusAction.NAME + "*", + GetSnapshotsAction.NAME, + SnapshotsStatusAction.NAME, + GetRepositoriesAction.NAME + ); private static final Set READ_CCR_PATTERN = Set.of(ClusterStateAction.NAME, HasPrivilegesAction.NAME); private static final Set MANAGE_ILM_PATTERN = Set.of("cluster:admin/ilm/*"); private static final Set READ_ILM_PATTERN = Set.of(GetLifecycleAction.NAME, GetStatusAction.NAME); - private static final Set MANAGE_SLM_PATTERN = - Set.of("cluster:admin/slm/*", StartILMAction.NAME, StopILMAction.NAME, GetStatusAction.NAME); + private static final Set MANAGE_SLM_PATTERN = Set.of( + "cluster:admin/slm/*", + StartILMAction.NAME, + StopILMAction.NAME, + GetStatusAction.NAME + ); private static final Set READ_SLM_PATTERN = Set.of(GetSnapshotLifecycleAction.NAME, GetStatusAction.NAME); private static final Set MANAGE_ENRICH_AUTOMATON = Set.of("cluster:admin/xpack/enrich/*"); @@ -96,46 +127,62 @@ public class ClusterPrivilegeResolver { public static final NamedClusterPrivilege ALL = new ActionClusterPrivilege("all", ALL_CLUSTER_PATTERN); public static final NamedClusterPrivilege MONITOR = new ActionClusterPrivilege("monitor", MONITOR_PATTERN); public static final NamedClusterPrivilege MONITOR_ML = new ActionClusterPrivilege("monitor_ml", MONITOR_ML_PATTERN); - public static final NamedClusterPrivilege MONITOR_TRANSFORM_DEPRECATED = - new ActionClusterPrivilege("monitor_data_frame_transforms", MONITOR_TRANSFORM_PATTERN); - public static final NamedClusterPrivilege MONITOR_TEXT_STRUCTURE = - new ActionClusterPrivilege("monitor_text_structure", MONITOR_TEXT_STRUCTURE_PATTERN); - public static final NamedClusterPrivilege MONITOR_TRANSFORM = - new ActionClusterPrivilege("monitor_transform", MONITOR_TRANSFORM_PATTERN); + public static final NamedClusterPrivilege MONITOR_TRANSFORM_DEPRECATED = new ActionClusterPrivilege( + "monitor_data_frame_transforms", + MONITOR_TRANSFORM_PATTERN + ); + public static final NamedClusterPrivilege MONITOR_TEXT_STRUCTURE = new ActionClusterPrivilege( + "monitor_text_structure", + MONITOR_TEXT_STRUCTURE_PATTERN + ); + public static final NamedClusterPrivilege MONITOR_TRANSFORM = new ActionClusterPrivilege( + "monitor_transform", + MONITOR_TRANSFORM_PATTERN + ); public static final NamedClusterPrivilege MONITOR_WATCHER = new ActionClusterPrivilege("monitor_watcher", MONITOR_WATCHER_PATTERN); public static final NamedClusterPrivilege MONITOR_ROLLUP = new ActionClusterPrivilege("monitor_rollup", MONITOR_ROLLUP_PATTERN); public static final NamedClusterPrivilege MANAGE = new ActionClusterPrivilege("manage", ALL_CLUSTER_PATTERN, ALL_SECURITY_PATTERN); public static final NamedClusterPrivilege MANAGE_ML = new ActionClusterPrivilege("manage_ml", MANAGE_ML_PATTERN); - public static final NamedClusterPrivilege MANAGE_TRANSFORM_DEPRECATED = - new ActionClusterPrivilege("manage_data_frame_transforms", MANAGE_TRANSFORM_PATTERN); - public static final NamedClusterPrivilege MANAGE_TRANSFORM = - new ActionClusterPrivilege("manage_transform", MANAGE_TRANSFORM_PATTERN); + public static final NamedClusterPrivilege MANAGE_TRANSFORM_DEPRECATED = new ActionClusterPrivilege( + "manage_data_frame_transforms", + MANAGE_TRANSFORM_PATTERN + ); + public static final NamedClusterPrivilege MANAGE_TRANSFORM = new ActionClusterPrivilege("manage_transform", MANAGE_TRANSFORM_PATTERN); public static final NamedClusterPrivilege MANAGE_TOKEN = new ActionClusterPrivilege("manage_token", MANAGE_TOKEN_PATTERN); public static final NamedClusterPrivilege MANAGE_WATCHER = new ActionClusterPrivilege("manage_watcher", MANAGE_WATCHER_PATTERN); public static final NamedClusterPrivilege MANAGE_ROLLUP = new ActionClusterPrivilege("manage_rollup", MANAGE_ROLLUP_PATTERN); - public static final NamedClusterPrivilege MANAGE_IDX_TEMPLATES = - new ActionClusterPrivilege("manage_index_templates", MANAGE_IDX_TEMPLATE_PATTERN); - public static final NamedClusterPrivilege MANAGE_INGEST_PIPELINES = - new ActionClusterPrivilege("manage_ingest_pipelines", MANAGE_INGEST_PIPELINE_PATTERN); - public static final NamedClusterPrivilege READ_PIPELINE = - new ActionClusterPrivilege("read_pipeline", READ_PIPELINE_PATTERN); - public static final NamedClusterPrivilege TRANSPORT_CLIENT = new ActionClusterPrivilege("transport_client", - TRANSPORT_CLIENT_PATTERN); - public static final NamedClusterPrivilege MANAGE_SECURITY = new ActionClusterPrivilege("manage_security", ALL_SECURITY_PATTERN, - Set.of(DelegatePkiAuthenticationAction.NAME)); + public static final NamedClusterPrivilege MANAGE_IDX_TEMPLATES = new ActionClusterPrivilege( + "manage_index_templates", + MANAGE_IDX_TEMPLATE_PATTERN + ); + public static final NamedClusterPrivilege MANAGE_INGEST_PIPELINES = new ActionClusterPrivilege( + "manage_ingest_pipelines", + MANAGE_INGEST_PIPELINE_PATTERN + ); + public static final NamedClusterPrivilege READ_PIPELINE = new ActionClusterPrivilege("read_pipeline", READ_PIPELINE_PATTERN); + public static final NamedClusterPrivilege TRANSPORT_CLIENT = new ActionClusterPrivilege("transport_client", TRANSPORT_CLIENT_PATTERN); + public static final NamedClusterPrivilege MANAGE_SECURITY = new ActionClusterPrivilege( + "manage_security", + ALL_SECURITY_PATTERN, + Set.of(DelegatePkiAuthenticationAction.NAME) + ); public static final NamedClusterPrivilege MANAGE_SAML = new ActionClusterPrivilege("manage_saml", MANAGE_SAML_PATTERN); public static final NamedClusterPrivilege MANAGE_OIDC = new ActionClusterPrivilege("manage_oidc", MANAGE_OIDC_PATTERN); public static final NamedClusterPrivilege MANAGE_API_KEY = new ActionClusterPrivilege("manage_api_key", MANAGE_API_KEY_PATTERN); - public static final NamedClusterPrivilege MANAGE_SERVICE_ACCOUNT = new ActionClusterPrivilege("manage_service_account", - MANAGE_SERVICE_ACCOUNT_PATTERN); + public static final NamedClusterPrivilege MANAGE_SERVICE_ACCOUNT = new ActionClusterPrivilege( + "manage_service_account", + MANAGE_SERVICE_ACCOUNT_PATTERN + ); public static final NamedClusterPrivilege GRANT_API_KEY = new ActionClusterPrivilege("grant_api_key", GRANT_API_KEY_PATTERN); - public static final NamedClusterPrivilege MANAGE_PIPELINE = new ActionClusterPrivilege("manage_pipeline", Set.of("cluster:admin" + - "/ingest/pipeline/*")); + public static final NamedClusterPrivilege MANAGE_PIPELINE = new ActionClusterPrivilege( + "manage_pipeline", + Set.of("cluster:admin" + "/ingest/pipeline/*") + ); public static final NamedClusterPrivilege MANAGE_AUTOSCALING = new ActionClusterPrivilege( "manage_autoscaling", Set.of("cluster:admin/autoscaling/*") ); - public static final NamedClusterPrivilege MANAGE_CCR = new ActionClusterPrivilege("manage_ccr", MANAGE_CCR_PATTERN); + public static final NamedClusterPrivilege MANAGE_CCR = new ActionClusterPrivilege("manage_ccr", MANAGE_CCR_PATTERN); public static final NamedClusterPrivilege READ_CCR = new ActionClusterPrivilege("read_ccr", READ_CCR_PATTERN); public static final NamedClusterPrivilege CREATE_SNAPSHOT = new ActionClusterPrivilege("create_snapshot", CREATE_SNAPSHOT_PATTERN); public static final NamedClusterPrivilege MONITOR_SNAPSHOT = new ActionClusterPrivilege("monitor_snapshot", MONITOR_SNAPSHOT_PATTERN); @@ -143,60 +190,66 @@ public class ClusterPrivilegeResolver { public static final NamedClusterPrivilege READ_ILM = new ActionClusterPrivilege("read_ilm", READ_ILM_PATTERN); public static final NamedClusterPrivilege MANAGE_SLM = new ActionClusterPrivilege("manage_slm", MANAGE_SLM_PATTERN); public static final NamedClusterPrivilege READ_SLM = new ActionClusterPrivilege("read_slm", READ_SLM_PATTERN); - public static final NamedClusterPrivilege DELEGATE_PKI = new ActionClusterPrivilege("delegate_pki", - Set.of(DelegatePkiAuthenticationAction.NAME, InvalidateTokenAction.NAME)); + public static final NamedClusterPrivilege DELEGATE_PKI = new ActionClusterPrivilege( + "delegate_pki", + Set.of(DelegatePkiAuthenticationAction.NAME, InvalidateTokenAction.NAME) + ); public static final NamedClusterPrivilege MANAGE_OWN_API_KEY = ManageOwnApiKeyClusterPrivilege.INSTANCE; public static final NamedClusterPrivilege MANAGE_ENRICH = new ActionClusterPrivilege("manage_enrich", MANAGE_ENRICH_AUTOMATON); - public static final NamedClusterPrivilege MANAGE_LOGSTASH_PIPELINES = new ActionClusterPrivilege("manage_logstash_pipelines", - Set.of("cluster:admin/logstash/pipeline/*")); + public static final NamedClusterPrivilege MANAGE_LOGSTASH_PIPELINES = new ActionClusterPrivilege( + "manage_logstash_pipelines", + Set.of("cluster:admin/logstash/pipeline/*") + ); - public static final NamedClusterPrivilege CANCEL_TASK = new ActionClusterPrivilege("cancel_task", - Set.of(CancelTasksAction.NAME + "*")); + public static final NamedClusterPrivilege CANCEL_TASK = new ActionClusterPrivilege("cancel_task", Set.of(CancelTasksAction.NAME + "*")); - private static final Map VALUES = sortByAccessLevel(List.of( - NONE, - ALL, - MONITOR, - MONITOR_ML, - MONITOR_TEXT_STRUCTURE, - MONITOR_TRANSFORM_DEPRECATED, - MONITOR_TRANSFORM, - MONITOR_WATCHER, - MONITOR_ROLLUP, - MANAGE, - MANAGE_ML, - MANAGE_TRANSFORM_DEPRECATED, - MANAGE_TRANSFORM, - MANAGE_TOKEN, - MANAGE_WATCHER, - MANAGE_IDX_TEMPLATES, - MANAGE_INGEST_PIPELINES, - READ_PIPELINE, - TRANSPORT_CLIENT, - MANAGE_SECURITY, - MANAGE_SAML, - MANAGE_OIDC, - MANAGE_API_KEY, - GRANT_API_KEY, - MANAGE_SERVICE_ACCOUNT, - MANAGE_PIPELINE, - MANAGE_ROLLUP, - MANAGE_AUTOSCALING, - MANAGE_CCR, - READ_CCR, - CREATE_SNAPSHOT, - MONITOR_SNAPSHOT, - MANAGE_ILM, - READ_ILM, - MANAGE_SLM, - READ_SLM, - DELEGATE_PKI, - MANAGE_OWN_API_KEY, - MANAGE_ENRICH, - MANAGE_LOGSTASH_PIPELINES, - CANCEL_TASK)); + private static final Map VALUES = sortByAccessLevel( + List.of( + NONE, + ALL, + MONITOR, + MONITOR_ML, + MONITOR_TEXT_STRUCTURE, + MONITOR_TRANSFORM_DEPRECATED, + MONITOR_TRANSFORM, + MONITOR_WATCHER, + MONITOR_ROLLUP, + MANAGE, + MANAGE_ML, + MANAGE_TRANSFORM_DEPRECATED, + MANAGE_TRANSFORM, + MANAGE_TOKEN, + MANAGE_WATCHER, + MANAGE_IDX_TEMPLATES, + MANAGE_INGEST_PIPELINES, + READ_PIPELINE, + TRANSPORT_CLIENT, + MANAGE_SECURITY, + MANAGE_SAML, + MANAGE_OIDC, + MANAGE_API_KEY, + GRANT_API_KEY, + MANAGE_SERVICE_ACCOUNT, + MANAGE_PIPELINE, + MANAGE_ROLLUP, + MANAGE_AUTOSCALING, + MANAGE_CCR, + READ_CCR, + CREATE_SNAPSHOT, + MONITOR_SNAPSHOT, + MANAGE_ILM, + READ_ILM, + MANAGE_SLM, + READ_SLM, + DELEGATE_PKI, + MANAGE_OWN_API_KEY, + MANAGE_ENRICH, + MANAGE_LOGSTASH_PIPELINES, + CANCEL_TASK + ) + ); /** * Resolves a {@link NamedClusterPrivilege} from a given name if it exists. @@ -214,10 +267,13 @@ public static NamedClusterPrivilege resolve(String name) { if (fixedPrivilege != null) { return fixedPrivilege; } - String errorMessage = "unknown cluster privilege [" + name + "]. a privilege must be either " + - "one of the predefined cluster privilege names [" + - Strings.collectionToCommaDelimitedString(VALUES.keySet()) + "] or a pattern over one of the available " + - "cluster actions"; + String errorMessage = "unknown cluster privilege [" + + name + + "]. a privilege must be either " + + "one of the predefined cluster privilege names [" + + Strings.collectionToCommaDelimitedString(VALUES.keySet()) + + "] or a pattern over one of the available " + + "cluster actions"; logger.debug(errorMessage); throw new IllegalArgumentException(errorMessage); @@ -228,9 +284,9 @@ public static Set names() { } public static boolean isClusterAction(String actionName) { - return actionName.startsWith("cluster:") || - actionName.startsWith("indices:admin/template/") || - actionName.startsWith("indices:admin/index_template/"); + return actionName.startsWith("cluster:") + || actionName.startsWith("indices:admin/template/") + || actionName.startsWith("indices:admin/index_template/"); } private static String actionToPattern(String text) { @@ -245,7 +301,8 @@ private static String actionToPattern(String text) { * @see org.elasticsearch.xpack.core.security.authz.permission.ClusterPermission#check(String, TransportRequest, Authentication) */ public static Collection findPrivilegesThatGrant(String action, TransportRequest request, Authentication authentication) { - return VALUES.entrySet().stream() + return VALUES.entrySet() + .stream() .filter(e -> e.getValue().permission().check(action, request, authentication)) .map(Map.Entry::getKey) .collect(Collectors.toUnmodifiableList()); @@ -258,8 +315,11 @@ public static Collection findPrivilegesThatGrant(String action, Transpor static SortedMap sortByAccessLevel(Collection privileges) { // How many other privileges does this privilege imply. Those with a higher count are considered to be a higher privilege final Map impliesCount = new HashMap<>(privileges.size()); - privileges.forEach(priv -> impliesCount.put(priv.name(), - privileges.stream().filter(p2 -> p2 != priv && priv.permission().implies(p2.permission())).count()) + privileges.forEach( + priv -> impliesCount.put( + priv.name(), + privileges.stream().filter(p2 -> p2 != priv && priv.permission().implies(p2.permission())).count() + ) ); final Comparator compare = Comparator.comparingLong(key -> impliesCount.getOrDefault(key, 0L)) diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/privilege/ConfigurableClusterPrivilege.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/privilege/ConfigurableClusterPrivilege.java index 820bfa7046960..ad5c7c436ac74 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/privilege/ConfigurableClusterPrivilege.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/privilege/ConfigurableClusterPrivilege.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.security.authz.privilege; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.NamedWriteable; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentFragment; import org.elasticsearch.xcontent.XContentBuilder; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/privilege/ConfigurableClusterPrivileges.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/privilege/ConfigurableClusterPrivileges.java index 601daff85948b..75bd0b448ebe8 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/privilege/ConfigurableClusterPrivileges.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/privilege/ConfigurableClusterPrivileges.java @@ -7,16 +7,16 @@ package org.elasticsearch.xpack.core.security.authz.privilege; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.transport.TransportRequest; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParseException; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.transport.TransportRequest; import org.elasticsearch.xpack.core.security.action.privilege.ApplicationPrivilegesRequest; import org.elasticsearch.xpack.core.security.authz.permission.ClusterPermission; import org.elasticsearch.xpack.core.security.authz.privilege.ConfigurableClusterPrivilege.Category; @@ -40,13 +40,12 @@ public final class ConfigurableClusterPrivileges { public static final ConfigurableClusterPrivilege[] EMPTY_ARRAY = new ConfigurableClusterPrivilege[0]; - public static final Writeable.Reader READER = - in1 -> in1.readNamedWriteable(ConfigurableClusterPrivilege.class); - public static final Writeable.Writer WRITER = - (out1, value) -> out1.writeNamedWriteable(value); + public static final Writeable.Reader READER = in1 -> in1.readNamedWriteable( + ConfigurableClusterPrivilege.class + ); + public static final Writeable.Writer WRITER = (out1, value) -> out1.writeNamedWriteable(value); - private ConfigurableClusterPrivileges() { - } + private ConfigurableClusterPrivileges() {} /** * Utility method to read an array of {@link ConfigurableClusterPrivilege} objects from a {@link StreamInput} @@ -66,8 +65,11 @@ public static void writeArray(StreamOutput out, ConfigurableClusterPrivilege[] p * Writes a single object value to the {@code builder} that contains each of the provided privileges. * The privileges are grouped according to their {@link ConfigurableClusterPrivilege#getCategory() categories} */ - public static XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params, - Collection privileges) throws IOException { + public static XContentBuilder toXContent( + XContentBuilder builder, + ToXContent.Params params, + Collection privileges + ) throws IOException { builder.startObject(); for (Category category : Category.values()) { builder.startObject(category.field.getPreferredName()); @@ -106,17 +108,26 @@ public static List parse(XContentParser parser) th private static void expectedToken(XContentParser.Token read, XContentParser parser, XContentParser.Token expected) { if (read != expected) { - throw new XContentParseException(parser.getTokenLocation(), - "failed to parse privilege. expected [" + expected + "] but found [" + read + "] instead"); + throw new XContentParseException( + parser.getTokenLocation(), + "failed to parse privilege. expected [" + expected + "] but found [" + read + "] instead" + ); } } private static void expectFieldName(XContentParser parser, ParseField... fields) throws IOException { final String fieldName = parser.currentName(); if (Arrays.stream(fields).anyMatch(pf -> pf.match(fieldName, parser.getDeprecationHandler())) == false) { - throw new XContentParseException(parser.getTokenLocation(), - "failed to parse privilege. expected " + (fields.length == 1 ? "field name" : "one of") + " [" - + Strings.arrayToCommaDelimitedString(fields) + "] but found [" + fieldName + "] instead"); + throw new XContentParseException( + parser.getTokenLocation(), + "failed to parse privilege. expected " + + (fields.length == 1 ? "field name" : "one of") + + " [" + + Strings.arrayToCommaDelimitedString(fields) + + "] but found [" + + fieldName + + "] instead" + ); } } @@ -139,7 +150,8 @@ public ManageApplicationPrivileges(Set applicationNames) { if (request instanceof ApplicationPrivilegesRequest) { final ApplicationPrivilegesRequest privRequest = (ApplicationPrivilegesRequest) request; final Collection requestApplicationNames = privRequest.getApplicationNames(); - return requestApplicationNames.isEmpty() ? this.applicationNames.contains("*") + return requestApplicationNames.isEmpty() + ? this.applicationNames.contains("*") : requestApplicationNames.stream().allMatch(application -> applicationPredicate.test(application)); } return false; @@ -173,7 +185,8 @@ public static ManageApplicationPrivileges createFrom(StreamInput in) throws IOEx @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder.field(Fields.MANAGE.getPreferredName(), + return builder.field( + Fields.MANAGE.getPreferredName(), Collections.singletonMap(Fields.APPLICATIONS.getPreferredName(), applicationNames) ); } @@ -192,8 +205,15 @@ public static ManageApplicationPrivileges parse(XContentParser parser) throws IO @Override public String toString() { - return "{" + getCategory() + ":" + Fields.MANAGE.getPreferredName() + ":" + Fields.APPLICATIONS.getPreferredName() + "=" - + Strings.collectionToDelimitedString(applicationNames, ",") + "}"; + return "{" + + getCategory() + + ":" + + Fields.MANAGE.getPreferredName() + + ":" + + Fields.APPLICATIONS.getPreferredName() + + "=" + + Strings.collectionToDelimitedString(applicationNames, ",") + + "}"; } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/privilege/HealthAndStatsPrivilege.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/privilege/HealthAndStatsPrivilege.java index e6ce010432ff4..8046ccab8cb13 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/privilege/HealthAndStatsPrivilege.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/privilege/HealthAndStatsPrivilege.java @@ -13,9 +13,6 @@ public final class HealthAndStatsPrivilege extends Privilege { public static final String NAME = "health_and_stats"; private HealthAndStatsPrivilege() { - super(NAME, "cluster:monitor/health*", - "cluster:monitor/stats*", - "indices:monitor/stats*", - "cluster:monitor/nodes/stats*"); + super(NAME, "cluster:monitor/health*", "cluster:monitor/stats*", "indices:monitor/stats*", "cluster:monitor/nodes/stats*"); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/privilege/IndexPrivilege.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/privilege/IndexPrivilege.java index 68f41dd08b409..89cd8bb4d7867 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/privilege/IndexPrivilege.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/privilege/IndexPrivilege.java @@ -56,55 +56,90 @@ public final class IndexPrivilege extends Privilege { private static final Automaton ALL_AUTOMATON = patterns("indices:*", "internal:transport/proxy/indices:*"); private static final Automaton READ_AUTOMATON = patterns("indices:data/read/*"); - private static final Automaton READ_CROSS_CLUSTER_AUTOMATON = patterns("internal:transport/proxy/indices:data/read/*", - ClusterSearchShardsAction.NAME); + private static final Automaton READ_CROSS_CLUSTER_AUTOMATON = patterns( + "internal:transport/proxy/indices:data/read/*", + ClusterSearchShardsAction.NAME + ); private static final Automaton CREATE_AUTOMATON = patterns("indices:data/write/index*", "indices:data/write/bulk*"); - private static final Automaton CREATE_DOC_AUTOMATON = patterns("indices:data/write/index", "indices:data/write/index[*", - "indices:data/write/index:op_type/create", "indices:data/write/bulk*"); - private static final Automaton INDEX_AUTOMATON = patterns("indices:data/write/index*", "indices:data/write/bulk*", - "indices:data/write/update*"); + private static final Automaton CREATE_DOC_AUTOMATON = patterns( + "indices:data/write/index", + "indices:data/write/index[*", + "indices:data/write/index:op_type/create", + "indices:data/write/bulk*" + ); + private static final Automaton INDEX_AUTOMATON = patterns( + "indices:data/write/index*", + "indices:data/write/bulk*", + "indices:data/write/update*" + ); private static final Automaton DELETE_AUTOMATON = patterns("indices:data/write/delete*", "indices:data/write/bulk*"); private static final Automaton WRITE_AUTOMATON = patterns("indices:data/write/*", AutoPutMappingAction.NAME); private static final Automaton MONITOR_AUTOMATON = patterns("indices:monitor/*"); - private static final Automaton MANAGE_AUTOMATON = - unionAndMinimize(Arrays.asList(MONITOR_AUTOMATON, patterns("indices:admin/*", FieldCapabilitiesAction.NAME + "*", - GetRollupIndexCapsAction.NAME + "*"))); - private static final Automaton CREATE_INDEX_AUTOMATON = patterns(CreateIndexAction.NAME, AutoCreateAction.NAME, - CreateDataStreamAction.NAME); + private static final Automaton MANAGE_AUTOMATON = unionAndMinimize( + Arrays.asList( + MONITOR_AUTOMATON, + patterns("indices:admin/*", FieldCapabilitiesAction.NAME + "*", GetRollupIndexCapsAction.NAME + "*") + ) + ); + private static final Automaton CREATE_INDEX_AUTOMATON = patterns( + CreateIndexAction.NAME, + AutoCreateAction.NAME, + CreateDataStreamAction.NAME + ); private static final Automaton DELETE_INDEX_AUTOMATON = patterns(DeleteIndexAction.NAME, DeleteDataStreamAction.NAME); - private static final Automaton VIEW_METADATA_AUTOMATON = patterns(GetAliasesAction.NAME, GetIndexAction.NAME, - GetFieldMappingsAction.NAME + "*", GetMappingsAction.NAME, ClusterSearchShardsAction.NAME, ValidateQueryAction.NAME + "*", - GetSettingsAction.NAME, ExplainLifecycleAction.NAME, GetDataStreamAction.NAME, ResolveIndexAction.NAME, - FieldCapabilitiesAction.NAME + "*", GetRollupIndexCapsAction.NAME + "*"); - private static final Automaton MANAGE_FOLLOW_INDEX_AUTOMATON = patterns(PutFollowAction.NAME, UnfollowAction.NAME, - CloseIndexAction.NAME + "*", PromoteDataStreamAction.NAME, RolloverAction.NAME); + private static final Automaton VIEW_METADATA_AUTOMATON = patterns( + GetAliasesAction.NAME, + GetIndexAction.NAME, + GetFieldMappingsAction.NAME + "*", + GetMappingsAction.NAME, + ClusterSearchShardsAction.NAME, + ValidateQueryAction.NAME + "*", + GetSettingsAction.NAME, + ExplainLifecycleAction.NAME, + GetDataStreamAction.NAME, + ResolveIndexAction.NAME, + FieldCapabilitiesAction.NAME + "*", + GetRollupIndexCapsAction.NAME + "*" + ); + private static final Automaton MANAGE_FOLLOW_INDEX_AUTOMATON = patterns( + PutFollowAction.NAME, + UnfollowAction.NAME, + CloseIndexAction.NAME + "*", + PromoteDataStreamAction.NAME, + RolloverAction.NAME + ); private static final Automaton MANAGE_LEADER_INDEX_AUTOMATON = patterns(ForgetFollowerAction.NAME + "*"); private static final Automaton MANAGE_ILM_AUTOMATON = patterns("indices:admin/ilm/*"); - private static final Automaton MAINTENANCE_AUTOMATON = patterns("indices:admin/refresh*", "indices:admin/flush*", - "indices:admin/synced_flush", "indices:admin/forcemerge*"); + private static final Automaton MAINTENANCE_AUTOMATON = patterns( + "indices:admin/refresh*", + "indices:admin/flush*", + "indices:admin/synced_flush", + "indices:admin/forcemerge*" + ); private static final Automaton AUTO_CONFIGURE_AUTOMATON = patterns(AutoPutMappingAction.NAME, AutoCreateAction.NAME); - public static final IndexPrivilege NONE = new IndexPrivilege("none", Automatons.EMPTY); - public static final IndexPrivilege ALL = new IndexPrivilege("all", ALL_AUTOMATON); - public static final IndexPrivilege READ = new IndexPrivilege("read", READ_AUTOMATON); - public static final IndexPrivilege READ_CROSS_CLUSTER = new IndexPrivilege("read_cross_cluster", READ_CROSS_CLUSTER_AUTOMATON); - public static final IndexPrivilege CREATE = new IndexPrivilege("create", CREATE_AUTOMATON); - public static final IndexPrivilege INDEX = new IndexPrivilege("index", INDEX_AUTOMATON); - public static final IndexPrivilege DELETE = new IndexPrivilege("delete", DELETE_AUTOMATON); - public static final IndexPrivilege WRITE = new IndexPrivilege("write", WRITE_AUTOMATON); - public static final IndexPrivilege CREATE_DOC = new IndexPrivilege("create_doc", CREATE_DOC_AUTOMATON); - public static final IndexPrivilege MONITOR = new IndexPrivilege("monitor", MONITOR_AUTOMATON); - public static final IndexPrivilege MANAGE = new IndexPrivilege("manage", MANAGE_AUTOMATON); - public static final IndexPrivilege DELETE_INDEX = new IndexPrivilege("delete_index", DELETE_INDEX_AUTOMATON); - public static final IndexPrivilege CREATE_INDEX = new IndexPrivilege("create_index", CREATE_INDEX_AUTOMATON); - public static final IndexPrivilege VIEW_METADATA = new IndexPrivilege("view_index_metadata", VIEW_METADATA_AUTOMATON); + public static final IndexPrivilege NONE = new IndexPrivilege("none", Automatons.EMPTY); + public static final IndexPrivilege ALL = new IndexPrivilege("all", ALL_AUTOMATON); + public static final IndexPrivilege READ = new IndexPrivilege("read", READ_AUTOMATON); + public static final IndexPrivilege READ_CROSS_CLUSTER = new IndexPrivilege("read_cross_cluster", READ_CROSS_CLUSTER_AUTOMATON); + public static final IndexPrivilege CREATE = new IndexPrivilege("create", CREATE_AUTOMATON); + public static final IndexPrivilege INDEX = new IndexPrivilege("index", INDEX_AUTOMATON); + public static final IndexPrivilege DELETE = new IndexPrivilege("delete", DELETE_AUTOMATON); + public static final IndexPrivilege WRITE = new IndexPrivilege("write", WRITE_AUTOMATON); + public static final IndexPrivilege CREATE_DOC = new IndexPrivilege("create_doc", CREATE_DOC_AUTOMATON); + public static final IndexPrivilege MONITOR = new IndexPrivilege("monitor", MONITOR_AUTOMATON); + public static final IndexPrivilege MANAGE = new IndexPrivilege("manage", MANAGE_AUTOMATON); + public static final IndexPrivilege DELETE_INDEX = new IndexPrivilege("delete_index", DELETE_INDEX_AUTOMATON); + public static final IndexPrivilege CREATE_INDEX = new IndexPrivilege("create_index", CREATE_INDEX_AUTOMATON); + public static final IndexPrivilege VIEW_METADATA = new IndexPrivilege("view_index_metadata", VIEW_METADATA_AUTOMATON); public static final IndexPrivilege MANAGE_FOLLOW_INDEX = new IndexPrivilege("manage_follow_index", MANAGE_FOLLOW_INDEX_AUTOMATON); public static final IndexPrivilege MANAGE_LEADER_INDEX = new IndexPrivilege("manage_leader_index", MANAGE_LEADER_INDEX_AUTOMATON); - public static final IndexPrivilege MANAGE_ILM = new IndexPrivilege("manage_ilm", MANAGE_ILM_AUTOMATON); - public static final IndexPrivilege MAINTENANCE = new IndexPrivilege("maintenance", MAINTENANCE_AUTOMATON); - public static final IndexPrivilege AUTO_CONFIGURE = new IndexPrivilege("auto_configure", AUTO_CONFIGURE_AUTOMATON); + public static final IndexPrivilege MANAGE_ILM = new IndexPrivilege("manage_ilm", MANAGE_ILM_AUTOMATON); + public static final IndexPrivilege MAINTENANCE = new IndexPrivilege("maintenance", MAINTENANCE_AUTOMATON); + public static final IndexPrivilege AUTO_CONFIGURE = new IndexPrivilege("auto_configure", AUTO_CONFIGURE_AUTOMATON); - private static final Map VALUES = sortByAccessLevel(Map.ofEntries( + private static final Map VALUES = sortByAccessLevel( + Map.ofEntries( entry("none", NONE), entry("all", ALL), entry("manage", MANAGE), @@ -123,7 +158,9 @@ public final class IndexPrivilege extends Privilege { entry("manage_leader_index", MANAGE_LEADER_INDEX), entry("manage_ilm", MANAGE_ILM), entry("maintenance", MAINTENANCE), - entry("auto_configure", AUTO_CONFIGURE))); + entry("auto_configure", AUTO_CONFIGURE) + ) + ); public static final Predicate ACTION_MATCHER = ALL.predicate(); public static final Predicate CREATE_INDEX_MATCHER = CREATE_INDEX.predicate(); @@ -167,10 +204,13 @@ private static IndexPrivilege resolve(Set name) { } else if (indexPrivilege != null) { automata.add(indexPrivilege.automaton); } else { - String errorMessage = "unknown index privilege [" + part + "]. a privilege must be either " + - "one of the predefined fixed indices privileges [" + - Strings.collectionToCommaDelimitedString(VALUES.entrySet()) + "] or a pattern over one of the available index" + - " actions"; + String errorMessage = "unknown index privilege [" + + part + + "]. a privilege must be either " + + "one of the predefined fixed indices privileges [" + + Strings.collectionToCommaDelimitedString(VALUES.entrySet()) + + "] or a pattern over one of the available index" + + " actions"; logger.debug(errorMessage); throw new IllegalArgumentException(errorMessage); } @@ -198,7 +238,8 @@ public static Set names() { * @see Privilege#sortByAccessLevel */ public static Collection findPrivilegesThatGrant(String action) { - return VALUES.entrySet().stream() + return VALUES.entrySet() + .stream() .filter(e -> e.getValue().predicate.test(action)) .map(e -> e.getKey()) .collect(Collectors.toUnmodifiableList()); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/privilege/ManageOwnApiKeyClusterPrivilege.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/privilege/ManageOwnApiKeyClusterPrivilege.java index 5df9ed4e60031..fd885ab3f20f1 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/privilege/ManageOwnApiKeyClusterPrivilege.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/privilege/ManageOwnApiKeyClusterPrivilege.java @@ -62,26 +62,43 @@ protected boolean extendedCheck(String action, TransportRequest request, Authent return true; } else if (request instanceof GetApiKeyRequest) { final GetApiKeyRequest getApiKeyRequest = (GetApiKeyRequest) request; - return checkIfUserIsOwnerOfApiKeys(authentication, getApiKeyRequest.getApiKeyId(), getApiKeyRequest.getUserName(), - getApiKeyRequest.getRealmName(), getApiKeyRequest.ownedByAuthenticatedUser()); + return checkIfUserIsOwnerOfApiKeys( + authentication, + getApiKeyRequest.getApiKeyId(), + getApiKeyRequest.getUserName(), + getApiKeyRequest.getRealmName(), + getApiKeyRequest.ownedByAuthenticatedUser() + ); } else if (request instanceof InvalidateApiKeyRequest) { final InvalidateApiKeyRequest invalidateApiKeyRequest = (InvalidateApiKeyRequest) request; final String[] apiKeyIds = invalidateApiKeyRequest.getIds(); if (apiKeyIds == null) { - return checkIfUserIsOwnerOfApiKeys(authentication, null, - invalidateApiKeyRequest.getUserName(), invalidateApiKeyRequest.getRealmName(), - invalidateApiKeyRequest.ownedByAuthenticatedUser()); + return checkIfUserIsOwnerOfApiKeys( + authentication, + null, + invalidateApiKeyRequest.getUserName(), + invalidateApiKeyRequest.getRealmName(), + invalidateApiKeyRequest.ownedByAuthenticatedUser() + ); } else { - return Arrays.stream(apiKeyIds).allMatch(id -> checkIfUserIsOwnerOfApiKeys(authentication, id, - invalidateApiKeyRequest.getUserName(), invalidateApiKeyRequest.getRealmName(), - invalidateApiKeyRequest.ownedByAuthenticatedUser())); + return Arrays.stream(apiKeyIds) + .allMatch( + id -> checkIfUserIsOwnerOfApiKeys( + authentication, + id, + invalidateApiKeyRequest.getUserName(), + invalidateApiKeyRequest.getRealmName(), + invalidateApiKeyRequest.ownedByAuthenticatedUser() + ) + ); } } else if (request instanceof QueryApiKeyRequest) { final QueryApiKeyRequest queryApiKeyRequest = (QueryApiKeyRequest) request; return queryApiKeyRequest.isFilterForCurrentUser(); } throw new IllegalArgumentException( - "manage own api key privilege only supports API key requests (not " + request.getClass().getName() + ")"); + "manage own api key privilege only supports API key requests (not " + request.getClass().getName() + ")" + ); } @Override @@ -89,8 +106,13 @@ protected boolean doImplies(ClusterPermission.ActionBasedPermissionCheck permiss return permissionCheck instanceof ManageOwnClusterPermissionCheck; } - private boolean checkIfUserIsOwnerOfApiKeys(Authentication authentication, String apiKeyId, String username, String realmName, - boolean ownedByAuthenticatedUser) { + private boolean checkIfUserIsOwnerOfApiKeys( + Authentication authentication, + String apiKeyId, + String username, + String realmName, + boolean ownedByAuthenticatedUser + ) { if (isCurrentAuthenticationUsingSameApiKeyIdFromRequest(authentication, apiKeyId)) { return true; } else { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/privilege/Privilege.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/privilege/Privilege.java index 9934a93031ff0..dd31296747e2f 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/privilege/Privilege.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/privilege/Privilege.java @@ -87,11 +87,15 @@ public Automaton getAutomaton() { static SortedMap sortByAccessLevel(Map privileges) { // How many other privileges is this privilege a subset of. Those with a higher count are considered to be a lower privilege final Map subsetCount = new HashMap<>(privileges.size()); - privileges.forEach((name, priv) -> subsetCount.put(name, - privileges.values().stream().filter(p2 -> p2 != priv && Operations.subsetOf(priv.automaton, p2.automaton)).count()) + privileges.forEach( + (name, priv) -> subsetCount.put( + name, + privileges.values().stream().filter(p2 -> p2 != priv && Operations.subsetOf(priv.automaton, p2.automaton)).count() + ) ); - final Comparator compare = Comparator.comparingLong(key -> subsetCount.getOrDefault(key, 0L)).reversed() + final Comparator compare = Comparator.comparingLong(key -> subsetCount.getOrDefault(key, 0L)) + .reversed() .thenComparing(Comparator.naturalOrder()); final TreeMap tree = new TreeMap<>(compare); tree.putAll(privileges); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/store/ReservedRolesStore.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/store/ReservedRolesStore.java index 86612eb7da1f9..c745f0775e7cd 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/store/ReservedRolesStore.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/store/ReservedRolesStore.java @@ -43,72 +43,120 @@ public class ReservedRolesStore implements BiConsumer, ActionListene public static final String ALERTS_BACKING_INDEX = ".internal.alerts*"; public static final String ALERTS_INDEX_ALIAS = ".alerts*"; - public static final RoleDescriptor SUPERUSER_ROLE_DESCRIPTOR = new RoleDescriptor("superuser", - new String[] { "all" }, - new RoleDescriptor.IndicesPrivileges[] { - RoleDescriptor.IndicesPrivileges.builder().indices("*").privileges("all").allowRestrictedIndices(true).build()}, - new RoleDescriptor.ApplicationResourcePrivileges[] { - RoleDescriptor.ApplicationResourcePrivileges.builder().application("*").privileges("*").resources("*").build() - }, - null, new String[] { "*" }, - MetadataUtils.DEFAULT_RESERVED_METADATA, Collections.emptyMap()); + public static final RoleDescriptor SUPERUSER_ROLE_DESCRIPTOR = new RoleDescriptor( + "superuser", + new String[] { "all" }, + new RoleDescriptor.IndicesPrivileges[] { + RoleDescriptor.IndicesPrivileges.builder().indices("*").privileges("all").allowRestrictedIndices(true).build() }, + new RoleDescriptor.ApplicationResourcePrivileges[] { + RoleDescriptor.ApplicationResourcePrivileges.builder().application("*").privileges("*").resources("*").build() }, + null, + new String[] { "*" }, + MetadataUtils.DEFAULT_RESERVED_METADATA, + Collections.emptyMap() + ); private static final Map RESERVED_ROLES = initializeReservedRoles(); private static Map initializeReservedRoles() { return MapBuilder.newMapBuilder() - .put("superuser", SUPERUSER_ROLE_DESCRIPTOR) - .put("transport_client", new RoleDescriptor("transport_client", new String[] { "transport_client" }, null, null, - MetadataUtils.DEFAULT_RESERVED_METADATA)) - .put("kibana_admin", kibanaAdminUser("kibana_admin", MetadataUtils.DEFAULT_RESERVED_METADATA)) - .put("kibana_user", kibanaAdminUser("kibana_user", - MetadataUtils.getDeprecatedReservedMetadata("Please use the [kibana_admin] role instead"))) - .put("monitoring_user", new RoleDescriptor("monitoring_user", - new String[] { "cluster:monitor/main", "cluster:monitor/xpack/info", RemoteInfoAction.NAME }, - new RoleDescriptor.IndicesPrivileges[] { - RoleDescriptor.IndicesPrivileges.builder() - .indices(".monitoring-*").privileges("read", "read_cross_cluster").build(), - RoleDescriptor.IndicesPrivileges.builder() - .indices("metricbeat-*").privileges("read", "read_cross_cluster").build() }, - new RoleDescriptor.ApplicationResourcePrivileges[] { - RoleDescriptor.ApplicationResourcePrivileges.builder() - .application("kibana-*").resources("*").privileges("reserved_monitoring").build() - }, - null, null, MetadataUtils.DEFAULT_RESERVED_METADATA, null)) - .put("remote_monitoring_agent", new RoleDescriptor("remote_monitoring_agent", - new String[] { - "manage_index_templates", "manage_ingest_pipelines", "monitor", - GetLifecycleAction.NAME, PutLifecycleAction.NAME, - "cluster:monitor/xpack/watcher/watch/get", - "cluster:admin/xpack/watcher/watch/put", - "cluster:admin/xpack/watcher/watch/delete" - }, - new RoleDescriptor.IndicesPrivileges[] { - RoleDescriptor.IndicesPrivileges.builder().indices(".monitoring-*").privileges("all").build(), - RoleDescriptor.IndicesPrivileges.builder() - .indices("metricbeat-*").privileges("index", "create_index", "view_index_metadata", - IndicesAliasesAction.NAME).build() }, - null, MetadataUtils.DEFAULT_RESERVED_METADATA)) - .put("remote_monitoring_collector", new RoleDescriptor( - "remote_monitoring_collector", - new String[] { - "monitor" - }, - new RoleDescriptor.IndicesPrivileges[] { - RoleDescriptor.IndicesPrivileges.builder() - .indices("*").privileges("monitor").allowRestrictedIndices(true).build(), - RoleDescriptor.IndicesPrivileges.builder() - .indices(".kibana*").privileges("read").allowRestrictedIndices(true).build() - }, - null, - null, - null, - MetadataUtils.DEFAULT_RESERVED_METADATA, - null - )) - .put("ingest_admin", new RoleDescriptor("ingest_admin", new String[] { "manage_index_templates", "manage_pipeline" }, - null, null, MetadataUtils.DEFAULT_RESERVED_METADATA)) - // reporting_user doesn't have any privileges in Elasticsearch, and Kibana authorizes privileges based on this role - .put("reporting_user", new RoleDescriptor( + .put("superuser", SUPERUSER_ROLE_DESCRIPTOR) + .put( + "transport_client", + new RoleDescriptor( + "transport_client", + new String[] { "transport_client" }, + null, + null, + MetadataUtils.DEFAULT_RESERVED_METADATA + ) + ) + .put("kibana_admin", kibanaAdminUser("kibana_admin", MetadataUtils.DEFAULT_RESERVED_METADATA)) + .put( + "kibana_user", + kibanaAdminUser("kibana_user", MetadataUtils.getDeprecatedReservedMetadata("Please use the [kibana_admin] role instead")) + ) + .put( + "monitoring_user", + new RoleDescriptor( + "monitoring_user", + new String[] { "cluster:monitor/main", "cluster:monitor/xpack/info", RemoteInfoAction.NAME }, + new RoleDescriptor.IndicesPrivileges[] { + RoleDescriptor.IndicesPrivileges.builder() + .indices(".monitoring-*") + .privileges("read", "read_cross_cluster") + .build(), + RoleDescriptor.IndicesPrivileges.builder() + .indices("metricbeat-*") + .privileges("read", "read_cross_cluster") + .build() }, + new RoleDescriptor.ApplicationResourcePrivileges[] { + RoleDescriptor.ApplicationResourcePrivileges.builder() + .application("kibana-*") + .resources("*") + .privileges("reserved_monitoring") + .build() }, + null, + null, + MetadataUtils.DEFAULT_RESERVED_METADATA, + null + ) + ) + .put( + "remote_monitoring_agent", + new RoleDescriptor( + "remote_monitoring_agent", + new String[] { + "manage_index_templates", + "manage_ingest_pipelines", + "monitor", + GetLifecycleAction.NAME, + PutLifecycleAction.NAME, + "cluster:monitor/xpack/watcher/watch/get", + "cluster:admin/xpack/watcher/watch/put", + "cluster:admin/xpack/watcher/watch/delete" }, + new RoleDescriptor.IndicesPrivileges[] { + RoleDescriptor.IndicesPrivileges.builder().indices(".monitoring-*").privileges("all").build(), + RoleDescriptor.IndicesPrivileges.builder() + .indices("metricbeat-*") + .privileges("index", "create_index", "view_index_metadata", IndicesAliasesAction.NAME) + .build() }, + null, + MetadataUtils.DEFAULT_RESERVED_METADATA + ) + ) + .put( + "remote_monitoring_collector", + new RoleDescriptor( + "remote_monitoring_collector", + new String[] { "monitor" }, + new RoleDescriptor.IndicesPrivileges[] { + RoleDescriptor.IndicesPrivileges.builder().indices("*").privileges("monitor").allowRestrictedIndices(true).build(), + RoleDescriptor.IndicesPrivileges.builder() + .indices(".kibana*") + .privileges("read") + .allowRestrictedIndices(true) + .build() }, + null, + null, + null, + MetadataUtils.DEFAULT_RESERVED_METADATA, + null + ) + ) + .put( + "ingest_admin", + new RoleDescriptor( + "ingest_admin", + new String[] { "manage_index_templates", "manage_pipeline" }, + null, + null, + MetadataUtils.DEFAULT_RESERVED_METADATA + ) + ) + // reporting_user doesn't have any privileges in Elasticsearch, and Kibana authorizes privileges based on this role + .put( + "reporting_user", + new RoleDescriptor( "reporting_user", null, null, @@ -116,188 +164,353 @@ private static Map initializeReservedRoles() { null, null, MetadataUtils.getDeprecatedReservedMetadata("Please use Kibana feature privileges instead"), - null)) - .put(KibanaSystemUser.ROLE_NAME, kibanaSystemRoleDescriptor(KibanaSystemUser.ROLE_NAME)) - .put("logstash_system", new RoleDescriptor("logstash_system", new String[] { "monitor", MonitoringBulkAction.NAME}, - null, null, MetadataUtils.DEFAULT_RESERVED_METADATA)) - .put("beats_admin", new RoleDescriptor("beats_admin", + null + ) + ) + .put(KibanaSystemUser.ROLE_NAME, kibanaSystemRoleDescriptor(KibanaSystemUser.ROLE_NAME)) + .put( + "logstash_system", + new RoleDescriptor( + "logstash_system", + new String[] { "monitor", MonitoringBulkAction.NAME }, + null, + null, + MetadataUtils.DEFAULT_RESERVED_METADATA + ) + ) + .put( + "beats_admin", + new RoleDescriptor( + "beats_admin", null, new RoleDescriptor.IndicesPrivileges[] { - RoleDescriptor.IndicesPrivileges.builder().indices(".management-beats").privileges("all").build() - }, - null, MetadataUtils.DEFAULT_RESERVED_METADATA)) - .put(UsernamesField.BEATS_ROLE, new RoleDescriptor(UsernamesField.BEATS_ROLE, - new String[] { "monitor", MonitoringBulkAction.NAME}, - new RoleDescriptor.IndicesPrivileges[]{ - RoleDescriptor.IndicesPrivileges.builder() - .indices(".monitoring-beats-*").privileges("create_index", "create").build() - }, - null, MetadataUtils.DEFAULT_RESERVED_METADATA)) - .put(UsernamesField.APM_ROLE, new RoleDescriptor(UsernamesField.APM_ROLE, - new String[] { "monitor", MonitoringBulkAction.NAME}, - new RoleDescriptor.IndicesPrivileges[]{ - RoleDescriptor.IndicesPrivileges.builder() - .indices(".monitoring-beats-*").privileges("create_index", "create_doc").build() - }, - null, MetadataUtils.DEFAULT_RESERVED_METADATA)) - .put("apm_user", new RoleDescriptor("apm_user", + RoleDescriptor.IndicesPrivileges.builder().indices(".management-beats").privileges("all").build() }, + null, + MetadataUtils.DEFAULT_RESERVED_METADATA + ) + ) + .put( + UsernamesField.BEATS_ROLE, + new RoleDescriptor( + UsernamesField.BEATS_ROLE, + new String[] { "monitor", MonitoringBulkAction.NAME }, + new RoleDescriptor.IndicesPrivileges[] { + RoleDescriptor.IndicesPrivileges.builder() + .indices(".monitoring-beats-*") + .privileges("create_index", "create") + .build() }, + null, + MetadataUtils.DEFAULT_RESERVED_METADATA + ) + ) + .put( + UsernamesField.APM_ROLE, + new RoleDescriptor( + UsernamesField.APM_ROLE, + new String[] { "monitor", MonitoringBulkAction.NAME }, + new RoleDescriptor.IndicesPrivileges[] { + RoleDescriptor.IndicesPrivileges.builder() + .indices(".monitoring-beats-*") + .privileges("create_index", "create_doc") + .build() }, + null, + MetadataUtils.DEFAULT_RESERVED_METADATA + ) + ) + .put( + "apm_user", + new RoleDescriptor( + "apm_user", null, new RoleDescriptor.IndicesPrivileges[] { // Self managed APM Server // Can be removed in 8.0 - RoleDescriptor.IndicesPrivileges.builder().indices("apm-*") - .privileges("read", "view_index_metadata").build(), + RoleDescriptor.IndicesPrivileges.builder().indices("apm-*").privileges("read", "view_index_metadata").build(), - // APM Server under fleet (data streams) - RoleDescriptor.IndicesPrivileges.builder().indices("logs-apm.*") - .privileges("read", "view_index_metadata").build(), - RoleDescriptor.IndicesPrivileges.builder().indices("logs-apm-*") - .privileges("read", "view_index_metadata").build(), - RoleDescriptor.IndicesPrivileges.builder().indices("metrics-apm.*") - .privileges("read", "view_index_metadata").build(), - RoleDescriptor.IndicesPrivileges.builder().indices("metrics-apm-*") - .privileges("read", "view_index_metadata").build(), - RoleDescriptor.IndicesPrivileges.builder().indices("traces-apm.*") - .privileges("read", "view_index_metadata").build(), - RoleDescriptor.IndicesPrivileges.builder().indices("traces-apm-*") - .privileges("read", "view_index_metadata").build(), + // APM Server under fleet (data streams) + RoleDescriptor.IndicesPrivileges.builder().indices("logs-apm.*").privileges("read", "view_index_metadata").build(), + RoleDescriptor.IndicesPrivileges.builder().indices("logs-apm-*").privileges("read", "view_index_metadata").build(), + RoleDescriptor.IndicesPrivileges.builder() + .indices("metrics-apm.*") + .privileges("read", "view_index_metadata") + .build(), + RoleDescriptor.IndicesPrivileges.builder() + .indices("metrics-apm-*") + .privileges("read", "view_index_metadata") + .build(), + RoleDescriptor.IndicesPrivileges.builder() + .indices("traces-apm.*") + .privileges("read", "view_index_metadata") + .build(), + RoleDescriptor.IndicesPrivileges.builder() + .indices("traces-apm-*") + .privileges("read", "view_index_metadata") + .build(), - // Machine Learning indices. Only needed for legacy reasons - // Can be removed in 8.0 - RoleDescriptor.IndicesPrivileges.builder().indices(".ml-anomalies*") - .privileges("read", "view_index_metadata").build(), + // Machine Learning indices. Only needed for legacy reasons + // Can be removed in 8.0 + RoleDescriptor.IndicesPrivileges.builder() + .indices(".ml-anomalies*") + .privileges("read", "view_index_metadata") + .build(), - // Annotations - RoleDescriptor.IndicesPrivileges.builder().indices("observability-annotations") - .privileges("read", "view_index_metadata").build() - }, + // Annotations + RoleDescriptor.IndicesPrivileges.builder() + .indices("observability-annotations") + .privileges("read", "view_index_metadata") + .build() }, new RoleDescriptor.ApplicationResourcePrivileges[] { - RoleDescriptor - .ApplicationResourcePrivileges - .builder() - .application("kibana-*") - .resources("*") - .privileges("reserved_ml_apm_user") - .build() - }, + RoleDescriptor.ApplicationResourcePrivileges.builder() + .application("kibana-*") + .resources("*") + .privileges("reserved_ml_apm_user") + .build() }, null, null, MetadataUtils.getDeprecatedReservedMetadata("This role will be removed in 8.0"), null - )) - .put("machine_learning_user", new RoleDescriptor("machine_learning_user", new String[] { "monitor_ml" }, - new RoleDescriptor.IndicesPrivileges[] { - RoleDescriptor.IndicesPrivileges.builder().indices(".ml-anomalies*", ".ml-notifications*") - .privileges("view_index_metadata", "read").build(), - RoleDescriptor.IndicesPrivileges.builder().indices(".ml-annotations*") - .privileges("view_index_metadata", "read", "write").build() - }, - // TODO: remove Kibana privileges from ML backend roles in 8.0.0 - new RoleDescriptor.ApplicationResourcePrivileges[] { - RoleDescriptor.ApplicationResourcePrivileges.builder() - .application("kibana-*").resources("*").privileges("reserved_ml_user").build() - }, - null, null, MetadataUtils.DEFAULT_RESERVED_METADATA, null)) - .put("machine_learning_admin", new RoleDescriptor("machine_learning_admin", new String[] { "manage_ml" }, - new RoleDescriptor.IndicesPrivileges[] { - RoleDescriptor.IndicesPrivileges.builder() - .indices(".ml-anomalies*", ".ml-notifications*", ".ml-state*", ".ml-meta*", ".ml-stats-*") - .allowRestrictedIndices(true) // .ml-meta is a restricted index - .privileges("view_index_metadata", "read").build(), - RoleDescriptor.IndicesPrivileges.builder().indices(".ml-annotations*") - .privileges("view_index_metadata", "read", "write").build() - }, - // TODO: remove Kibana privileges from ML backend roles in 8.0.0 - new RoleDescriptor.ApplicationResourcePrivileges[] { - RoleDescriptor.ApplicationResourcePrivileges.builder() - .application("kibana-*").resources("*").privileges("reserved_ml_admin").build() - }, - null, null, MetadataUtils.DEFAULT_RESERVED_METADATA, null)) - // DEPRECATED: to be removed in 9.0.0 - .put("data_frame_transforms_admin", new RoleDescriptor("data_frame_transforms_admin", - new String[] { "manage_data_frame_transforms" }, - new RoleDescriptor.IndicesPrivileges[]{ - RoleDescriptor.IndicesPrivileges.builder() - .indices(TransformInternalIndexConstants.AUDIT_INDEX_PATTERN, - TransformInternalIndexConstants.AUDIT_INDEX_PATTERN_DEPRECATED, - TransformInternalIndexConstants.AUDIT_INDEX_READ_ALIAS) - .privileges("view_index_metadata", "read").build() - }, - new RoleDescriptor.ApplicationResourcePrivileges[] { - RoleDescriptor.ApplicationResourcePrivileges.builder() - .application("kibana-*").resources("*").privileges("reserved_ml_user").build() - }, null, null, MetadataUtils.getDeprecatedReservedMetadata("Please use the [transform_admin] role instead"), null)) - // DEPRECATED: to be removed in 9.0.0 - .put("data_frame_transforms_user", new RoleDescriptor("data_frame_transforms_user", - new String[] { "monitor_data_frame_transforms" }, - new RoleDescriptor.IndicesPrivileges[]{ - RoleDescriptor.IndicesPrivileges.builder() - .indices(TransformInternalIndexConstants.AUDIT_INDEX_PATTERN, - TransformInternalIndexConstants.AUDIT_INDEX_PATTERN_DEPRECATED, - TransformInternalIndexConstants.AUDIT_INDEX_READ_ALIAS) - .privileges("view_index_metadata", "read").build() - }, - new RoleDescriptor.ApplicationResourcePrivileges[] { - RoleDescriptor.ApplicationResourcePrivileges.builder() - .application("kibana-*").resources("*").privileges("reserved_ml_user").build() - }, null, null, MetadataUtils.getDeprecatedReservedMetadata("Please use the [transform_user] role instead"), null)) - .put("transform_admin", new RoleDescriptor("transform_admin", - new String[] { "manage_transform" }, - new RoleDescriptor.IndicesPrivileges[]{ - RoleDescriptor.IndicesPrivileges.builder() - .indices(TransformInternalIndexConstants.AUDIT_INDEX_PATTERN, - TransformInternalIndexConstants.AUDIT_INDEX_PATTERN_DEPRECATED, - TransformInternalIndexConstants.AUDIT_INDEX_READ_ALIAS) - .privileges("view_index_metadata", "read").build() - }, null, null, null, MetadataUtils.DEFAULT_RESERVED_METADATA, null)) - .put("transform_user", new RoleDescriptor("transform_user", - new String[] { "monitor_transform" }, - new RoleDescriptor.IndicesPrivileges[]{ - RoleDescriptor.IndicesPrivileges.builder() - .indices(TransformInternalIndexConstants.AUDIT_INDEX_PATTERN, - TransformInternalIndexConstants.AUDIT_INDEX_PATTERN_DEPRECATED, - TransformInternalIndexConstants.AUDIT_INDEX_READ_ALIAS) - .privileges("view_index_metadata", "read").build() - }, null, null, null, MetadataUtils.DEFAULT_RESERVED_METADATA, null)) - .put("watcher_admin", new RoleDescriptor("watcher_admin", new String[] { "manage_watcher" }, - new RoleDescriptor.IndicesPrivileges[] { - RoleDescriptor.IndicesPrivileges.builder().indices(Watch.INDEX, TriggeredWatchStoreField.INDEX_NAME, - HistoryStoreField.INDEX_PREFIX + "*").privileges("read").allowRestrictedIndices(true).build() }, - null, MetadataUtils.DEFAULT_RESERVED_METADATA)) - .put("watcher_user", new RoleDescriptor("watcher_user", new String[] { "monitor_watcher" }, - new RoleDescriptor.IndicesPrivileges[] { - RoleDescriptor.IndicesPrivileges.builder().indices(Watch.INDEX) - .privileges("read") - .allowRestrictedIndices(true) - .build(), - RoleDescriptor.IndicesPrivileges.builder().indices(HistoryStoreField.INDEX_PREFIX + "*") - .privileges("read") - .build() }, null, MetadataUtils.DEFAULT_RESERVED_METADATA)) - .put("logstash_admin", new RoleDescriptor("logstash_admin", new String[] {"manage_logstash_pipelines"}, + ) + ) + .put( + "machine_learning_user", + new RoleDescriptor( + "machine_learning_user", + new String[] { "monitor_ml" }, new RoleDescriptor.IndicesPrivileges[] { - RoleDescriptor.IndicesPrivileges.builder().indices(".logstash*") - .privileges("create", "delete", "index", "manage", "read") - .allowRestrictedIndices(true) - .build() }, - null, MetadataUtils.DEFAULT_RESERVED_METADATA)) - .put("rollup_user", new RoleDescriptor("rollup_user", new String[] { "monitor_rollup" }, - null, null, MetadataUtils.DEFAULT_RESERVED_METADATA)) - .put("rollup_admin", new RoleDescriptor("rollup_admin", new String[] { "manage_rollup" }, - null, null, MetadataUtils.DEFAULT_RESERVED_METADATA)) - .put("snapshot_user", new RoleDescriptor("snapshot_user", new String[] { "create_snapshot", GetRepositoriesAction.NAME }, - new RoleDescriptor.IndicesPrivileges[] { RoleDescriptor.IndicesPrivileges.builder() - .indices("*") - .privileges("view_index_metadata") - .allowRestrictedIndices(true) - .build() }, null, null, null, MetadataUtils.DEFAULT_RESERVED_METADATA, null)) - .put("enrich_user", new RoleDescriptor("enrich_user", new String[]{ "manage_enrich", "manage_ingest_pipelines", "monitor" }, - new RoleDescriptor.IndicesPrivileges[]{ RoleDescriptor.IndicesPrivileges.builder() - .indices(".enrich-*") - .privileges("manage", "read", "write") - .build() }, null, MetadataUtils.DEFAULT_RESERVED_METADATA)) - .put("viewer", buildViewerRoleDescriptor()) - .put("editor", buildEditorRoleDescriptor()) - .immutableMap(); + RoleDescriptor.IndicesPrivileges.builder() + .indices(".ml-anomalies*", ".ml-notifications*") + .privileges("view_index_metadata", "read") + .build(), + RoleDescriptor.IndicesPrivileges.builder() + .indices(".ml-annotations*") + .privileges("view_index_metadata", "read", "write") + .build() }, + // TODO: remove Kibana privileges from ML backend roles in 8.0.0 + new RoleDescriptor.ApplicationResourcePrivileges[] { + RoleDescriptor.ApplicationResourcePrivileges.builder() + .application("kibana-*") + .resources("*") + .privileges("reserved_ml_user") + .build() }, + null, + null, + MetadataUtils.DEFAULT_RESERVED_METADATA, + null + ) + ) + .put( + "machine_learning_admin", + new RoleDescriptor( + "machine_learning_admin", + new String[] { "manage_ml" }, + new RoleDescriptor.IndicesPrivileges[] { + RoleDescriptor.IndicesPrivileges.builder() + .indices(".ml-anomalies*", ".ml-notifications*", ".ml-state*", ".ml-meta*", ".ml-stats-*") + .allowRestrictedIndices(true) // .ml-meta is a restricted index + .privileges("view_index_metadata", "read") + .build(), + RoleDescriptor.IndicesPrivileges.builder() + .indices(".ml-annotations*") + .privileges("view_index_metadata", "read", "write") + .build() }, + // TODO: remove Kibana privileges from ML backend roles in 8.0.0 + new RoleDescriptor.ApplicationResourcePrivileges[] { + RoleDescriptor.ApplicationResourcePrivileges.builder() + .application("kibana-*") + .resources("*") + .privileges("reserved_ml_admin") + .build() }, + null, + null, + MetadataUtils.DEFAULT_RESERVED_METADATA, + null + ) + ) + // DEPRECATED: to be removed in 9.0.0 + .put( + "data_frame_transforms_admin", + new RoleDescriptor( + "data_frame_transforms_admin", + new String[] { "manage_data_frame_transforms" }, + new RoleDescriptor.IndicesPrivileges[] { + RoleDescriptor.IndicesPrivileges.builder() + .indices( + TransformInternalIndexConstants.AUDIT_INDEX_PATTERN, + TransformInternalIndexConstants.AUDIT_INDEX_PATTERN_DEPRECATED, + TransformInternalIndexConstants.AUDIT_INDEX_READ_ALIAS + ) + .privileges("view_index_metadata", "read") + .build() }, + new RoleDescriptor.ApplicationResourcePrivileges[] { + RoleDescriptor.ApplicationResourcePrivileges.builder() + .application("kibana-*") + .resources("*") + .privileges("reserved_ml_user") + .build() }, + null, + null, + MetadataUtils.getDeprecatedReservedMetadata("Please use the [transform_admin] role instead"), + null + ) + ) + // DEPRECATED: to be removed in 9.0.0 + .put( + "data_frame_transforms_user", + new RoleDescriptor( + "data_frame_transforms_user", + new String[] { "monitor_data_frame_transforms" }, + new RoleDescriptor.IndicesPrivileges[] { + RoleDescriptor.IndicesPrivileges.builder() + .indices( + TransformInternalIndexConstants.AUDIT_INDEX_PATTERN, + TransformInternalIndexConstants.AUDIT_INDEX_PATTERN_DEPRECATED, + TransformInternalIndexConstants.AUDIT_INDEX_READ_ALIAS + ) + .privileges("view_index_metadata", "read") + .build() }, + new RoleDescriptor.ApplicationResourcePrivileges[] { + RoleDescriptor.ApplicationResourcePrivileges.builder() + .application("kibana-*") + .resources("*") + .privileges("reserved_ml_user") + .build() }, + null, + null, + MetadataUtils.getDeprecatedReservedMetadata("Please use the [transform_user] role instead"), + null + ) + ) + .put( + "transform_admin", + new RoleDescriptor( + "transform_admin", + new String[] { "manage_transform" }, + new RoleDescriptor.IndicesPrivileges[] { + RoleDescriptor.IndicesPrivileges.builder() + .indices( + TransformInternalIndexConstants.AUDIT_INDEX_PATTERN, + TransformInternalIndexConstants.AUDIT_INDEX_PATTERN_DEPRECATED, + TransformInternalIndexConstants.AUDIT_INDEX_READ_ALIAS + ) + .privileges("view_index_metadata", "read") + .build() }, + null, + null, + null, + MetadataUtils.DEFAULT_RESERVED_METADATA, + null + ) + ) + .put( + "transform_user", + new RoleDescriptor( + "transform_user", + new String[] { "monitor_transform" }, + new RoleDescriptor.IndicesPrivileges[] { + RoleDescriptor.IndicesPrivileges.builder() + .indices( + TransformInternalIndexConstants.AUDIT_INDEX_PATTERN, + TransformInternalIndexConstants.AUDIT_INDEX_PATTERN_DEPRECATED, + TransformInternalIndexConstants.AUDIT_INDEX_READ_ALIAS + ) + .privileges("view_index_metadata", "read") + .build() }, + null, + null, + null, + MetadataUtils.DEFAULT_RESERVED_METADATA, + null + ) + ) + .put( + "watcher_admin", + new RoleDescriptor( + "watcher_admin", + new String[] { "manage_watcher" }, + new RoleDescriptor.IndicesPrivileges[] { + RoleDescriptor.IndicesPrivileges.builder() + .indices(Watch.INDEX, TriggeredWatchStoreField.INDEX_NAME, HistoryStoreField.INDEX_PREFIX + "*") + .privileges("read") + .allowRestrictedIndices(true) + .build() }, + null, + MetadataUtils.DEFAULT_RESERVED_METADATA + ) + ) + .put( + "watcher_user", + new RoleDescriptor( + "watcher_user", + new String[] { "monitor_watcher" }, + new RoleDescriptor.IndicesPrivileges[] { + RoleDescriptor.IndicesPrivileges.builder() + .indices(Watch.INDEX) + .privileges("read") + .allowRestrictedIndices(true) + .build(), + RoleDescriptor.IndicesPrivileges.builder() + .indices(HistoryStoreField.INDEX_PREFIX + "*") + .privileges("read") + .build() }, + null, + MetadataUtils.DEFAULT_RESERVED_METADATA + ) + ) + .put( + "logstash_admin", + new RoleDescriptor( + "logstash_admin", + new String[] { "manage_logstash_pipelines" }, + new RoleDescriptor.IndicesPrivileges[] { + RoleDescriptor.IndicesPrivileges.builder() + .indices(".logstash*") + .privileges("create", "delete", "index", "manage", "read") + .allowRestrictedIndices(true) + .build() }, + null, + MetadataUtils.DEFAULT_RESERVED_METADATA + ) + ) + .put( + "rollup_user", + new RoleDescriptor("rollup_user", new String[] { "monitor_rollup" }, null, null, MetadataUtils.DEFAULT_RESERVED_METADATA) + ) + .put( + "rollup_admin", + new RoleDescriptor("rollup_admin", new String[] { "manage_rollup" }, null, null, MetadataUtils.DEFAULT_RESERVED_METADATA) + ) + .put( + "snapshot_user", + new RoleDescriptor( + "snapshot_user", + new String[] { "create_snapshot", GetRepositoriesAction.NAME }, + new RoleDescriptor.IndicesPrivileges[] { + RoleDescriptor.IndicesPrivileges.builder() + .indices("*") + .privileges("view_index_metadata") + .allowRestrictedIndices(true) + .build() }, + null, + null, + null, + MetadataUtils.DEFAULT_RESERVED_METADATA, + null + ) + ) + .put( + "enrich_user", + new RoleDescriptor( + "enrich_user", + new String[] { "manage_enrich", "manage_ingest_pipelines", "monitor" }, + new RoleDescriptor.IndicesPrivileges[] { + RoleDescriptor.IndicesPrivileges.builder().indices(".enrich-*").privileges("manage", "read", "write").build() }, + null, + MetadataUtils.DEFAULT_RESERVED_METADATA + ) + ) + .put("viewer", buildViewerRoleDescriptor()) + .put("editor", buildEditorRoleDescriptor()) + .immutableMap(); } private static RoleDescriptor buildViewerRoleDescriptor() { @@ -308,67 +521,94 @@ private static RoleDescriptor buildViewerRoleDescriptor() { // Stack RoleDescriptor.IndicesPrivileges.builder() .indices("/~(([.]|ilm-history-).*)/") - .privileges("read", "view_index_metadata").build(), + .privileges("read", "view_index_metadata") + .build(), // Security RoleDescriptor.IndicesPrivileges.builder().indices(".siem-signals-*").privileges("read", "view_index_metadata").build() }, new RoleDescriptor.ApplicationResourcePrivileges[] { RoleDescriptor.ApplicationResourcePrivileges.builder() .application("kibana-.kibana") .resources("*") - .privileges("read").build() }, + .privileges("read") + .build() }, null, null, MetadataUtils.DEFAULT_RESERVED_METADATA, - null); + null + ); } private static RoleDescriptor buildEditorRoleDescriptor() { - return new RoleDescriptor("editor", + return new RoleDescriptor( + "editor", new String[] {}, new RoleDescriptor.IndicesPrivileges[] { // Stack RoleDescriptor.IndicesPrivileges.builder() .indices("/~(([.]|ilm-history-).*)/") - .privileges("read", "view_index_metadata").build(), + .privileges("read", "view_index_metadata") + .build(), // Observability RoleDescriptor.IndicesPrivileges.builder() .indices("observability-annotations") - .privileges("read", "view_index_metadata", "write").build(), + .privileges("read", "view_index_metadata", "write") + .build(), // Security RoleDescriptor.IndicesPrivileges.builder() .indices(".siem-signals-*", ".lists-*", ".items-*") - .privileges("read", "view_index_metadata", "write", "maintenance").build() }, + .privileges("read", "view_index_metadata", "write", "maintenance") + .build() }, new RoleDescriptor.ApplicationResourcePrivileges[] { RoleDescriptor.ApplicationResourcePrivileges.builder() .application("kibana-.kibana") .resources("*") - .privileges("all").build() }, + .privileges("all") + .build() }, null, null, MetadataUtils.DEFAULT_RESERVED_METADATA, - null); + null + ); } private static RoleDescriptor kibanaAdminUser(String name, Map metadata) { - return new RoleDescriptor(name, null, null, + return new RoleDescriptor( + name, + null, + null, new RoleDescriptor.ApplicationResourcePrivileges[] { RoleDescriptor.ApplicationResourcePrivileges.builder() .application("kibana-.kibana") - .resources("*").privileges("all") + .resources("*") + .privileges("all") .build() }, - null, null, metadata, null); + null, + null, + metadata, + null + ); } public static RoleDescriptor kibanaSystemRoleDescriptor(String name) { - return new RoleDescriptor(name, + return new RoleDescriptor( + name, new String[] { - "monitor", "manage_index_templates", MonitoringBulkAction.NAME, "manage_saml", "manage_token", "manage_oidc", + "monitor", + "manage_index_templates", + MonitoringBulkAction.NAME, + "manage_saml", + "manage_token", + "manage_oidc", // For Fleet package upgrade - "manage_pipeline", "manage_ilm", + "manage_pipeline", + "manage_ilm", // For the endpoint package that ships a transform "manage_transform", - InvalidateApiKeyAction.NAME, "grant_api_key", "manage_own_api_key", - GetBuiltinPrivilegesAction.NAME, "delegate_pki", + InvalidateApiKeyAction.NAME, + "grant_api_key", + "manage_own_api_key", + GetBuiltinPrivilegesAction.NAME, + "delegate_pki", // To facilitate ML UI functionality being controlled using Kibana security privileges "manage_ml", // The symbolic constant for this one is in SecurityActionMapper, so not accessible from X-Pack core @@ -376,73 +616,59 @@ public static RoleDescriptor kibanaSystemRoleDescriptor(String name) { // To facilitate using the file uploader functionality "monitor_text_structure", // To cancel tasks and delete async searches - "cancel_task" - }, + "cancel_task" }, new RoleDescriptor.IndicesPrivileges[] { // System indices defined in KibanaPlugin RoleDescriptor.IndicesPrivileges.builder() - .indices(".kibana*", ".reporting-*").privileges("all").allowRestrictedIndices(true).build(), - RoleDescriptor.IndicesPrivileges.builder() - .indices(".monitoring-*").privileges("read", "read_cross_cluster").build(), - RoleDescriptor.IndicesPrivileges.builder() - .indices(".management-beats").privileges("create_index", "read", "write").build(), + .indices(".kibana*", ".reporting-*") + .privileges("all") + .allowRestrictedIndices(true) + .build(), + RoleDescriptor.IndicesPrivileges.builder().indices(".monitoring-*").privileges("read", "read_cross_cluster").build(), + RoleDescriptor.IndicesPrivileges.builder().indices(".management-beats").privileges("create_index", "read", "write").build(), // To facilitate ML UI functionality being controlled using Kibana security privileges + RoleDescriptor.IndicesPrivileges.builder().indices(".ml-anomalies*", ".ml-stats-*").privileges("read").build(), RoleDescriptor.IndicesPrivileges.builder() - .indices(".ml-anomalies*", ".ml-stats-*") - .privileges("read").build(), - RoleDescriptor.IndicesPrivileges.builder().indices(".ml-annotations*", ".ml-notifications*") - .privileges("read", "write").build(), + .indices(".ml-annotations*", ".ml-notifications*") + .privileges("read", "write") + .build(), // APM agent configuration - system index defined in KibanaPlugin RoleDescriptor.IndicesPrivileges.builder() - .indices(".apm-agent-configuration").privileges("all").allowRestrictedIndices(true).build(), + .indices(".apm-agent-configuration") + .privileges("all") + .allowRestrictedIndices(true) + .build(), // APM custom link index creation - system index defined in KibanaPlugin RoleDescriptor.IndicesPrivileges.builder() - .indices(".apm-custom-link").privileges("all").allowRestrictedIndices(true).build(), + .indices(".apm-custom-link") + .privileges("all") + .allowRestrictedIndices(true) + .build(), // APM telemetry queries APM indices in kibana task runner - RoleDescriptor.IndicesPrivileges.builder() - .indices("apm-*") - .privileges("read", "read_cross_cluster").build(), + RoleDescriptor.IndicesPrivileges.builder().indices("apm-*").privileges("read", "read_cross_cluster").build(), // Data telemetry reads mappings, metadata and stats of indices - RoleDescriptor.IndicesPrivileges.builder() - .indices("*") - .privileges("view_index_metadata", "monitor").build(), + RoleDescriptor.IndicesPrivileges.builder().indices("*").privileges("view_index_metadata", "monitor").build(), // Endpoint diagnostic information. Kibana reads from these indices to send telemetry - RoleDescriptor.IndicesPrivileges.builder() - .indices(".logs-endpoint.diagnostic.collection-*") - .privileges("read").build(), + RoleDescriptor.IndicesPrivileges.builder().indices(".logs-endpoint.diagnostic.collection-*").privileges("read").build(), // Fleet Server indices. Kibana create this indice before Fleet Server use them. // Fleet Server indices. Kibana read and write to this indice to manage Elastic Agents - RoleDescriptor.IndicesPrivileges.builder() - .indices(".fleet*") - .allowRestrictedIndices(true) - .privileges("all").build(), + RoleDescriptor.IndicesPrivileges.builder().indices(".fleet*").allowRestrictedIndices(true).privileges("all").build(), // Legacy "Alerts as data" used in Security Solution. // Kibana user creates these indices; reads / writes to them. - RoleDescriptor.IndicesPrivileges.builder() - .indices(ReservedRolesStore.ALERTS_LEGACY_INDEX) - .privileges("all").build(), + RoleDescriptor.IndicesPrivileges.builder().indices(ReservedRolesStore.ALERTS_LEGACY_INDEX).privileges("all").build(), // "Alerts as data" internal backing indices used in Security Solution, Observability, etc. // Kibana system user creates these indices; reads / writes to them via the aliases (see below). - RoleDescriptor.IndicesPrivileges.builder() - .indices(ReservedRolesStore.ALERTS_BACKING_INDEX) - .privileges("all").build(), + RoleDescriptor.IndicesPrivileges.builder().indices(ReservedRolesStore.ALERTS_BACKING_INDEX).privileges("all").build(), // "Alerts as data" public index aliases used in Security Solution, Observability, etc. // Kibana system user uses them to read / write alerts. - RoleDescriptor.IndicesPrivileges.builder() - .indices(ReservedRolesStore.ALERTS_INDEX_ALIAS) - .privileges("all").build(), + RoleDescriptor.IndicesPrivileges.builder().indices(ReservedRolesStore.ALERTS_INDEX_ALIAS).privileges("all").build(), // Endpoint / Fleet policy responses. Kibana requires read access to send telemetry - RoleDescriptor.IndicesPrivileges.builder() - .indices("metrics-endpoint.policy-*") - .privileges("read").build(), + RoleDescriptor.IndicesPrivileges.builder().indices("metrics-endpoint.policy-*").privileges("read").build(), // Endpoint metrics. Kibana requires read access to send telemetry - RoleDescriptor.IndicesPrivileges.builder() - .indices("metrics-endpoint.metrics-*") - .privileges("read").build(), + RoleDescriptor.IndicesPrivileges.builder().indices("metrics-endpoint.metrics-*").privileges("read").build(), // Fleet package upgrade RoleDescriptor.IndicesPrivileges.builder() - .indices("logs-*", "synthetics-*", "traces-*", - "/metrics-.*&~(metrics-endpoint\\.metadata_current_default)/") + .indices("logs-*", "synthetics-*", "traces-*", "/metrics-.*&~(metrics-endpoint\\.metadata_current_default)/") .privileges(UpdateSettingsAction.NAME, PutMappingAction.NAME, RolloverAction.NAME) .build(), // For src/dest indices of the Endpoint package that ships a transform @@ -451,19 +677,26 @@ public static RoleDescriptor kibanaSystemRoleDescriptor(String name) { .privileges("read", "view_index_metadata") .build(), RoleDescriptor.IndicesPrivileges.builder() - .indices("metrics-endpoint.metadata_current_default", ".metrics-endpoint.metadata_current_default", - ".metrics-endpoint.metadata_united_default") + .indices( + "metrics-endpoint.metadata_current_default", + ".metrics-endpoint.metadata_current_default", + ".metrics-endpoint.metadata_united_default" + ) .privileges("create_index", "delete_index", "read", "index") - .build(), - }, + .build(), }, null, new ConfigurableClusterPrivilege[] { new ManageApplicationPrivileges(Collections.singleton("kibana-*")) }, - null, MetadataUtils.DEFAULT_RESERVED_METADATA, null); + null, + MetadataUtils.DEFAULT_RESERVED_METADATA, + null + ); } public static boolean isReserved(String role) { - return RESERVED_ROLES.containsKey(role) || UsernamesField.SYSTEM_ROLE.equals(role) || - UsernamesField.XPACK_ROLE.equals(role) || UsernamesField.ASYNC_SEARCH_ROLE.equals(role); + return RESERVED_ROLES.containsKey(role) + || UsernamesField.SYSTEM_ROLE.equals(role) + || UsernamesField.XPACK_ROLE.equals(role) + || UsernamesField.ASYNC_SEARCH_ROLE.equals(role); } public Map usageStats() { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/support/DLSRoleQueryValidator.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/support/DLSRoleQueryValidator.java index e51d5d4ae2274..637f0b2a6c120 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/support/DLSRoleQueryValidator.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/support/DLSRoleQueryValidator.java @@ -7,16 +7,11 @@ package org.elasticsearch.xpack.core.security.authz.support; import org.elasticsearch.ElasticsearchParseException; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentParseException; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.core.Nullable; import org.elasticsearch.index.query.AbstractQueryBuilder; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.BoostingQueryBuilder; @@ -28,6 +23,11 @@ import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptService; import org.elasticsearch.script.ScriptType; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentParseException; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.security.authz.RoleDescriptor; import org.elasticsearch.xpack.core.security.user.User; @@ -41,8 +41,7 @@ */ public final class DLSRoleQueryValidator { - private DLSRoleQueryValidator() { - } + private DLSRoleQueryValidator() {} /** * Validates the query field in the {@link RoleDescriptor.IndicesPrivileges} only if it is not a template query.
    @@ -51,8 +50,7 @@ private DLSRoleQueryValidator() { * @param indicesPrivileges {@link RoleDescriptor.IndicesPrivileges} * @param xContentRegistry {@link NamedXContentRegistry} for finding named queries */ - public static void validateQueryField(RoleDescriptor.IndicesPrivileges[] indicesPrivileges, - NamedXContentRegistry xContentRegistry) { + public static void validateQueryField(RoleDescriptor.IndicesPrivileges[] indicesPrivileges, NamedXContentRegistry xContentRegistry) { if (indicesPrivileges != null) { for (int i = 0; i < indicesPrivileges.length; i++) { BytesReference query = indicesPrivileges[i].getQuery(); @@ -65,18 +63,25 @@ public static void validateQueryField(RoleDescriptor.IndicesPrivileges[] indices evaluateAndVerifyRoleQuery(query.utf8ToString(), xContentRegistry); } - } catch (ParsingException | IllegalArgumentException | IOException e) { - throw new ElasticsearchParseException("failed to parse field 'query' for indices [" + - Strings.arrayToCommaDelimitedString(indicesPrivileges[i].getIndices()) + - "] at index privilege [" + i + "] of role descriptor", e); + } catch (ParsingException | IllegalArgumentException | IOException e) { + throw new ElasticsearchParseException( + "failed to parse field 'query' for indices [" + + Strings.arrayToCommaDelimitedString(indicesPrivileges[i].getIndices()) + + "] at index privilege [" + + i + + "] of role descriptor", + e + ); } } } } private static boolean isTemplateQuery(BytesReference query, NamedXContentRegistry xContentRegistry) throws IOException { - try (XContentParser parser = XContentType.JSON.xContent().createParser(xContentRegistry, - LoggingDeprecationHandler.INSTANCE, query.utf8ToString())) { + try ( + XContentParser parser = XContentType.JSON.xContent() + .createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, query.utf8ToString()) + ) { return isTemplateQuery(parser); } } @@ -84,28 +89,40 @@ private static boolean isTemplateQuery(BytesReference query, NamedXContentRegist private static boolean isTemplateQuery(XContentParser parser) throws IOException { XContentParser.Token token = parser.nextToken(); if (token != XContentParser.Token.START_OBJECT) { - throw new XContentParseException(parser.getTokenLocation(), "expected [" + XContentParser.Token.START_OBJECT + "] but " + - "found [" + token + "] instead"); + throw new XContentParseException( + parser.getTokenLocation(), + "expected [" + XContentParser.Token.START_OBJECT + "] but " + "found [" + token + "] instead" + ); } token = parser.nextToken(); if (token != XContentParser.Token.FIELD_NAME) { - throw new XContentParseException(parser.getTokenLocation(), "expected [" + XContentParser.Token.FIELD_NAME + "] with " + - "value a query name or 'template' but found [" + token + "] instead"); + throw new XContentParseException( + parser.getTokenLocation(), + "expected [" + + XContentParser.Token.FIELD_NAME + + "] with " + + "value a query name or 'template' but found [" + + token + + "] instead" + ); } String fieldName = parser.currentName(); return "template".equals(fieldName); } public static boolean hasStoredScript(BytesReference query, NamedXContentRegistry xContentRegistry) throws IOException { - try (XContentParser parser = XContentType.JSON.xContent().createParser(xContentRegistry, - LoggingDeprecationHandler.INSTANCE, query.utf8ToString())) { + try ( + XContentParser parser = XContentType.JSON.xContent() + .createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, query.utf8ToString()) + ) { if (false == isTemplateQuery(parser)) { - return false; + return false; } if (parser.nextToken() != XContentParser.Token.START_OBJECT) { throw new XContentParseException( parser.getTokenLocation(), - "expected [" + XContentParser.Token.START_OBJECT + "] but found [" + parser.currentToken() + "] instead"); + "expected [" + XContentParser.Token.START_OBJECT + "] but found [" + parser.currentToken() + "] instead" + ); } return ScriptType.STORED == Script.parse(parser).getType(); } @@ -124,11 +141,14 @@ public static boolean hasStoredScript(BytesReference query, NamedXContentRegistr * * does not have a query field then it returns {@code null}. */ @Nullable - public static QueryBuilder evaluateAndVerifyRoleQuery(BytesReference query, ScriptService scriptService, - NamedXContentRegistry xContentRegistry, User user) { + public static QueryBuilder evaluateAndVerifyRoleQuery( + BytesReference query, + ScriptService scriptService, + NamedXContentRegistry xContentRegistry, + User user + ) { if (query != null) { - String templateResult = SecurityQueryTemplateEvaluator.evaluateTemplate(query.utf8ToString(), scriptService, - user); + String templateResult = SecurityQueryTemplateEvaluator.evaluateTemplate(query.utf8ToString(), scriptService, user); try { return evaluateAndVerifyRoleQuery(templateResult, xContentRegistry); } catch (ElasticsearchParseException | ParsingException | XContentParseException | IOException e) { @@ -141,8 +161,10 @@ public static QueryBuilder evaluateAndVerifyRoleQuery(BytesReference query, Scri @Nullable public static QueryBuilder evaluateAndVerifyRoleQuery(String query, NamedXContentRegistry xContentRegistry) throws IOException { if (query != null) { - try (XContentParser parser = XContentFactory.xContent(query).createParser(xContentRegistry, - LoggingDeprecationHandler.INSTANCE, query)) { + try ( + XContentParser parser = XContentFactory.xContent(query) + .createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, query) + ) { QueryBuilder queryBuilder = AbstractQueryBuilder.parseInnerQueryBuilder(parser); verifyRoleQuery(queryBuilder); return queryBuilder; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/support/SecurityQueryTemplateEvaluator.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/support/SecurityQueryTemplateEvaluator.java index abc2b2a5df450..07018dfc83b62 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/support/SecurityQueryTemplateEvaluator.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/support/SecurityQueryTemplateEvaluator.java @@ -10,10 +10,10 @@ import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.script.ScriptService; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.script.ScriptService; import org.elasticsearch.xpack.core.security.support.MustacheTemplateEvaluator; import org.elasticsearch.xpack.core.security.user.User; @@ -28,8 +28,7 @@ */ public final class SecurityQueryTemplateEvaluator { - private SecurityQueryTemplateEvaluator() { - } + private SecurityQueryTemplateEvaluator() {} /** * If the query source is a template, then parses the script, compiles the @@ -49,8 +48,10 @@ private SecurityQueryTemplateEvaluator() { */ public static String evaluateTemplate(final String querySource, final ScriptService scriptService, final User user) { // EMPTY is safe here because we never use namedObject - try (XContentParser parser = XContentFactory.xContent(querySource).createParser(NamedXContentRegistry.EMPTY, - LoggingDeprecationHandler.INSTANCE, querySource)) { + try ( + XContentParser parser = XContentFactory.xContent(querySource) + .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, querySource) + ) { XContentParser.Token token = parser.nextToken(); if (token != XContentParser.Token.START_OBJECT) { throw new ElasticsearchParseException("Unexpected token [" + token + "]"); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/index/RestrictedIndicesNames.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/index/RestrictedIndicesNames.java index b7e02a7d43577..e1e9187ed1ce4 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/index/RestrictedIndicesNames.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/index/RestrictedIndicesNames.java @@ -21,9 +21,15 @@ public final class RestrictedIndicesNames { public static final String SECURITY_TOKENS_ALIAS = ".security-tokens"; // public for tests - public static final Set RESTRICTED_NAMES = Collections.unmodifiableSet(Sets.newHashSet(SECURITY_MAIN_ALIAS, - INTERNAL_SECURITY_MAIN_INDEX_6, INTERNAL_SECURITY_MAIN_INDEX_7, INTERNAL_SECURITY_TOKENS_INDEX_7, SECURITY_TOKENS_ALIAS)); + public static final Set RESTRICTED_NAMES = Collections.unmodifiableSet( + Sets.newHashSet( + SECURITY_MAIN_ALIAS, + INTERNAL_SECURITY_MAIN_INDEX_6, + INTERNAL_SECURITY_MAIN_INDEX_7, + INTERNAL_SECURITY_TOKENS_INDEX_7, + SECURITY_TOKENS_ALIAS + ) + ); - private RestrictedIndicesNames() { - } + private RestrictedIndicesNames() {} } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/support/Automatons.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/support/Automatons.java index a52235d51be2c..a364b9cdbb227 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/support/Automatons.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/support/Automatons.java @@ -16,8 +16,8 @@ import org.elasticsearch.common.cache.CacheBuilder; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.set.Sets; +import org.elasticsearch.core.TimeValue; import java.util.ArrayList; import java.util.Arrays; @@ -38,16 +38,24 @@ public final class Automatons { - static final Setting MAX_DETERMINIZED_STATES_SETTING = - Setting.intSetting("xpack.security.automata.max_determinized_states", 100000, DEFAULT_DETERMINIZE_WORK_LIMIT, - Setting.Property.NodeScope); - - static final Setting CACHE_ENABLED = - Setting.boolSetting("xpack.security.automata.cache.enabled", true, Setting.Property.NodeScope); - static final Setting CACHE_SIZE = - Setting.intSetting("xpack.security.automata.cache.size", 10_000, Setting.Property.NodeScope); - static final Setting CACHE_TTL = - Setting.timeSetting("xpack.security.automata.cache.ttl", TimeValue.timeValueHours(48), Setting.Property.NodeScope); + static final Setting MAX_DETERMINIZED_STATES_SETTING = Setting.intSetting( + "xpack.security.automata.max_determinized_states", + 100000, + DEFAULT_DETERMINIZE_WORK_LIMIT, + Setting.Property.NodeScope + ); + + static final Setting CACHE_ENABLED = Setting.boolSetting( + "xpack.security.automata.cache.enabled", + true, + Setting.Property.NodeScope + ); + static final Setting CACHE_SIZE = Setting.intSetting("xpack.security.automata.cache.size", 10_000, Setting.Property.NodeScope); + static final Setting CACHE_TTL = Setting.timeSetting( + "xpack.security.automata.cache.ttl", + TimeValue.timeValueHours(48), + Setting.Property.NodeScope + ); public static final Automaton EMPTY = Automata.makeEmpty(); public static final Automaton MATCH_ALL = Automata.makeAnyString(); @@ -60,8 +68,7 @@ public final class Automatons { static final char WILDCARD_CHAR = '?'; // Char equality with support for wildcards static final char WILDCARD_ESCAPE = '\\'; // Escape character - private Automatons() { - } + private Automatons() {} /** * Builds and returns an automaton that will represent the union of all the given patterns. @@ -105,13 +112,13 @@ private static Automaton buildAutomaton(Collection patterns) { // We originally just compiled each automaton separately and then unioned them all. // However, that approach can be quite slow, and very memory intensive. // It is far more efficient if - // 1. we strip leading/trailing "*" - // 2. union the automaton produced from the remaining text - // 3. append/prepend MatchAnyString automatons as appropriate + // 1. we strip leading/trailing "*" + // 2. union the automaton produced from the remaining text + // 3. append/prepend MatchAnyString automatons as appropriate // That is: - // - `MATCH_ALL + (bullseye|daredevil) + MATCH_ALL` - // can be determinized more efficiently than - // - `(MATCH_ALL + bullseye + MATCH_ALL)|(MATCH_ALL + daredevil + MATCH_ALL)` + // - `MATCH_ALL + (bullseye|daredevil) + MATCH_ALL` + // can be determinized more efficiently than + // - `(MATCH_ALL + bullseye + MATCH_ALL)|(MATCH_ALL + daredevil + MATCH_ALL)` final Set prefix = new HashSet<>(); final Set infix = new HashSet<>(); @@ -145,7 +152,7 @@ private static Automaton buildAutomaton(Collection patterns) { // If we were to handle them here, we would run 2 minimize operations (one for the union of strings, // then another after concatenating MATCH_ANY), which is substantially slower. // However, that's not true if the string has an embedded '*' in it - in that case it is more efficient to determinize - // the set of prefixes (with the embedded MATCH_ANY) and then concatenate another MATCH_ANY and minimize. + // the set of prefixes (with the embedded MATCH_ANY) and then concatenate another MATCH_ANY and minimize. prefix.add(p.substring(0, p.length() - 1)); } else { // something* / some*thing / some?thing / etc @@ -194,9 +201,13 @@ public static boolean isLuceneRegex(String str) { private static Automaton buildAutomaton(String pattern) { if (pattern.startsWith("/")) { // it's a lucene regexp if (pattern.length() == 1 || pattern.endsWith("/") == false) { - throw new IllegalArgumentException("invalid pattern [" + pattern + "]. patterns starting with '/' " + - "indicate regular expression pattern and therefore must also end with '/'." + - " other patterns (those that do not start with '/') will be treated as simple wildcard patterns"); + throw new IllegalArgumentException( + "invalid pattern [" + + pattern + + "]. patterns starting with '/' " + + "indicate regular expression pattern and therefore must also end with '/'." + + " other patterns (those that do not start with '/') will be treated as simple wildcard patterns" + ); } String regex = pattern.substring(1, pattern.length() - 1); return new RegExp(regex).toAutomaton(); @@ -222,7 +233,7 @@ private static RuntimeException unwrapCacheException(ExecutionException e) { @SuppressWarnings("fallthrough") // explicit fallthrough at end of switch static Automaton wildcard(String text) { List automata = new ArrayList<>(); - for (int i = 0; i < text.length(); ) { + for (int i = 0; i < text.length();) { final char c = text.charAt(i); int length = 1; switch (c) { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/support/Exceptions.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/support/Exceptions.java index ed3e0204c6dd5..37f1dce5af7ba 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/support/Exceptions.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/support/Exceptions.java @@ -14,8 +14,7 @@ public class Exceptions { - private Exceptions() { - } + private Exceptions() {} public static ElasticsearchSecurityException authenticationError(String msg, Throwable cause, Object... args) { ElasticsearchSecurityException e = new ElasticsearchSecurityException(msg, RestStatus.UNAUTHORIZED, cause, args); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/support/MetadataUtils.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/support/MetadataUtils.java index f2de13c6511fc..41ebac766cabc 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/support/MetadataUtils.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/support/MetadataUtils.java @@ -16,8 +16,7 @@ public class MetadataUtils { public static final String DEPRECATED_REASON_METADATA_KEY = RESERVED_PREFIX + "deprecated_reason"; public static final Map DEFAULT_RESERVED_METADATA = Map.of(RESERVED_METADATA_KEY, true); - private MetadataUtils() { - } + private MetadataUtils() {} public static boolean containsReservedMetadata(Map metadata) { for (String key : metadata.keySet()) { @@ -29,10 +28,6 @@ public static boolean containsReservedMetadata(Map metadata) { } public static Map getDeprecatedReservedMetadata(String reason) { - return Map.of( - RESERVED_METADATA_KEY, true, - DEPRECATED_METADATA_KEY, true, - DEPRECATED_REASON_METADATA_KEY, reason - ); + return Map.of(RESERVED_METADATA_KEY, true, DEPRECATED_METADATA_KEY, true, DEPRECATED_REASON_METADATA_KEY, reason); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/support/MustacheTemplateEvaluator.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/support/MustacheTemplateEvaluator.java index 23d6f975d13ad..1cb513d1b8ae5 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/support/MustacheTemplateEvaluator.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/support/MustacheTemplateEvaluator.java @@ -7,11 +7,11 @@ package org.elasticsearch.xpack.core.security.support; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptService; import org.elasticsearch.script.ScriptType; import org.elasticsearch.script.TemplateScript; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.HashMap; @@ -35,8 +35,13 @@ public static Script parseForScript(XContentParser parser, Map e } extraParams.forEach(params::put); // Always enforce mustache script lang: - script = new Script(script.getType(), script.getType() == ScriptType.STORED ? null : "mustache", script.getIdOrCode(), - script.getOptions(), params); + script = new Script( + script.getType(), + script.getType() == ScriptType.STORED ? null : "mustache", + script.getIdOrCode(), + script.getOptions(), + params + ); return script; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/support/NoOpLogger.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/support/NoOpLogger.java index a82d751fe6bfe..af1a2726b4f55 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/support/NoOpLogger.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/support/NoOpLogger.java @@ -213,49 +213,52 @@ public void debug(Marker marker, String message, Object p0, Object p1, Object p2 @Override public void debug( - Marker marker, - String message, - Object p0, - Object p1, - Object p2, - Object p3, - Object p4, - Object p5, - Object p6, - Object p7) { + Marker marker, + String message, + Object p0, + Object p1, + Object p2, + Object p3, + Object p4, + Object p5, + Object p6, + Object p7 + ) { } @Override public void debug( - Marker marker, - String message, - Object p0, - Object p1, - Object p2, - Object p3, - Object p4, - Object p5, - Object p6, - Object p7, - Object p8) { + Marker marker, + String message, + Object p0, + Object p1, + Object p2, + Object p3, + Object p4, + Object p5, + Object p6, + Object p7, + Object p8 + ) { } @Override public void debug( - Marker marker, - String message, - Object p0, - Object p1, - Object p2, - Object p3, - Object p4, - Object p5, - Object p6, - Object p7, - Object p8, - Object p9) { + Marker marker, + String message, + Object p0, + Object p1, + Object p2, + Object p3, + Object p4, + Object p5, + Object p6, + Object p7, + Object p8, + Object p9 + ) { } @@ -306,17 +309,18 @@ public void debug(String message, Object p0, Object p1, Object p2, Object p3, Ob @Override public void debug( - String message, - Object p0, - Object p1, - Object p2, - Object p3, - Object p4, - Object p5, - Object p6, - Object p7, - Object p8, - Object p9) { + String message, + Object p0, + Object p1, + Object p2, + Object p3, + Object p4, + Object p5, + Object p6, + Object p7, + Object p8, + Object p9 + ) { } @@ -507,49 +511,52 @@ public void error(Marker marker, String message, Object p0, Object p1, Object p2 @Override public void error( - Marker marker, - String message, - Object p0, - Object p1, - Object p2, - Object p3, - Object p4, - Object p5, - Object p6, - Object p7) { + Marker marker, + String message, + Object p0, + Object p1, + Object p2, + Object p3, + Object p4, + Object p5, + Object p6, + Object p7 + ) { } @Override public void error( - Marker marker, - String message, - Object p0, - Object p1, - Object p2, - Object p3, - Object p4, - Object p5, - Object p6, - Object p7, - Object p8) { + Marker marker, + String message, + Object p0, + Object p1, + Object p2, + Object p3, + Object p4, + Object p5, + Object p6, + Object p7, + Object p8 + ) { } @Override public void error( - Marker marker, - String message, - Object p0, - Object p1, - Object p2, - Object p3, - Object p4, - Object p5, - Object p6, - Object p7, - Object p8, - Object p9) { + Marker marker, + String message, + Object p0, + Object p1, + Object p2, + Object p3, + Object p4, + Object p5, + Object p6, + Object p7, + Object p8, + Object p9 + ) { } @@ -600,17 +607,18 @@ public void error(String message, Object p0, Object p1, Object p2, Object p3, Ob @Override public void error( - String message, - Object p0, - Object p1, - Object p2, - Object p3, - Object p4, - Object p5, - Object p6, - Object p7, - Object p8, - Object p9) { + String message, + Object p0, + Object p1, + Object p2, + Object p3, + Object p4, + Object p5, + Object p6, + Object p7, + Object p8, + Object p9 + ) { } @@ -801,49 +809,52 @@ public void fatal(Marker marker, String message, Object p0, Object p1, Object p2 @Override public void fatal( - Marker marker, - String message, - Object p0, - Object p1, - Object p2, - Object p3, - Object p4, - Object p5, - Object p6, - Object p7) { + Marker marker, + String message, + Object p0, + Object p1, + Object p2, + Object p3, + Object p4, + Object p5, + Object p6, + Object p7 + ) { } @Override public void fatal( - Marker marker, - String message, - Object p0, - Object p1, - Object p2, - Object p3, - Object p4, - Object p5, - Object p6, - Object p7, - Object p8) { + Marker marker, + String message, + Object p0, + Object p1, + Object p2, + Object p3, + Object p4, + Object p5, + Object p6, + Object p7, + Object p8 + ) { } @Override public void fatal( - Marker marker, - String message, - Object p0, - Object p1, - Object p2, - Object p3, - Object p4, - Object p5, - Object p6, - Object p7, - Object p8, - Object p9) { + Marker marker, + String message, + Object p0, + Object p1, + Object p2, + Object p3, + Object p4, + Object p5, + Object p6, + Object p7, + Object p8, + Object p9 + ) { } @@ -894,17 +905,18 @@ public void fatal(String message, Object p0, Object p1, Object p2, Object p3, Ob @Override public void fatal( - String message, - Object p0, - Object p1, - Object p2, - Object p3, - Object p4, - Object p5, - Object p6, - Object p7, - Object p8, - Object p9) { + String message, + Object p0, + Object p1, + Object p2, + Object p3, + Object p4, + Object p5, + Object p6, + Object p7, + Object p8, + Object p9 + ) { } @@ -1100,49 +1112,52 @@ public void info(Marker marker, String message, Object p0, Object p1, Object p2, @Override public void info( - Marker marker, - String message, - Object p0, - Object p1, - Object p2, - Object p3, - Object p4, - Object p5, - Object p6, - Object p7) { + Marker marker, + String message, + Object p0, + Object p1, + Object p2, + Object p3, + Object p4, + Object p5, + Object p6, + Object p7 + ) { } @Override public void info( - Marker marker, - String message, - Object p0, - Object p1, - Object p2, - Object p3, - Object p4, - Object p5, - Object p6, - Object p7, - Object p8) { + Marker marker, + String message, + Object p0, + Object p1, + Object p2, + Object p3, + Object p4, + Object p5, + Object p6, + Object p7, + Object p8 + ) { } @Override public void info( - Marker marker, - String message, - Object p0, - Object p1, - Object p2, - Object p3, - Object p4, - Object p5, - Object p6, - Object p7, - Object p8, - Object p9) { + Marker marker, + String message, + Object p0, + Object p1, + Object p2, + Object p3, + Object p4, + Object p5, + Object p6, + Object p7, + Object p8, + Object p9 + ) { } @@ -1193,17 +1208,18 @@ public void info(String message, Object p0, Object p1, Object p2, Object p3, Obj @Override public void info( - String message, - Object p0, - Object p1, - Object p2, - Object p3, - Object p4, - Object p5, - Object p6, - Object p7, - Object p8, - Object p9) { + String message, + Object p0, + Object p1, + Object p2, + Object p3, + Object p4, + Object p5, + Object p6, + Object p7, + Object p8, + Object p9 + ) { } @@ -1449,67 +1465,71 @@ public void log(Level level, Marker marker, String message, Object p0, Object p1 @Override public void log( - Level level, - Marker marker, - String message, - Object p0, - Object p1, - Object p2, - Object p3, - Object p4, - Object p5, - Object p6) { + Level level, + Marker marker, + String message, + Object p0, + Object p1, + Object p2, + Object p3, + Object p4, + Object p5, + Object p6 + ) { } @Override public void log( - Level level, - Marker marker, - String message, - Object p0, - Object p1, - Object p2, - Object p3, - Object p4, - Object p5, - Object p6, - Object p7) { + Level level, + Marker marker, + String message, + Object p0, + Object p1, + Object p2, + Object p3, + Object p4, + Object p5, + Object p6, + Object p7 + ) { } @Override public void log( - Level level, - Marker marker, - String message, - Object p0, - Object p1, - Object p2, - Object p3, - Object p4, - Object p5, - Object p6, - Object p7, - Object p8) { + Level level, + Marker marker, + String message, + Object p0, + Object p1, + Object p2, + Object p3, + Object p4, + Object p5, + Object p6, + Object p7, + Object p8 + ) { } @Override public void log( - Level level, - Marker marker, - String message, - Object p0, - Object p1, - Object p2, - Object p3, - Object p4, - Object p5, - Object p6, - Object p7, - Object p8, - Object p9) { + Level level, + Marker marker, + String message, + Object p0, + Object p1, + Object p2, + Object p3, + Object p4, + Object p5, + Object p6, + Object p7, + Object p8, + Object p9 + ) { } @@ -1555,34 +1575,36 @@ public void log(Level level, String message, Object p0, Object p1, Object p2, Ob @Override public void log( - Level level, - String message, - Object p0, - Object p1, - Object p2, - Object p3, - Object p4, - Object p5, - Object p6, - Object p7, - Object p8) { + Level level, + String message, + Object p0, + Object p1, + Object p2, + Object p3, + Object p4, + Object p5, + Object p6, + Object p7, + Object p8 + ) { } @Override public void log( - Level level, - String message, - Object p0, - Object p1, - Object p2, - Object p3, - Object p4, - Object p5, - Object p6, - Object p7, - Object p8, - Object p9) { + Level level, + String message, + Object p0, + Object p1, + Object p2, + Object p3, + Object p4, + Object p5, + Object p6, + Object p7, + Object p8, + Object p9 + ) { } @@ -1783,49 +1805,52 @@ public void trace(Marker marker, String message, Object p0, Object p1, Object p2 @Override public void trace( - Marker marker, - String message, - Object p0, - Object p1, - Object p2, - Object p3, - Object p4, - Object p5, - Object p6, - Object p7) { + Marker marker, + String message, + Object p0, + Object p1, + Object p2, + Object p3, + Object p4, + Object p5, + Object p6, + Object p7 + ) { } @Override public void trace( - Marker marker, - String message, - Object p0, - Object p1, - Object p2, - Object p3, - Object p4, - Object p5, - Object p6, - Object p7, - Object p8) { + Marker marker, + String message, + Object p0, + Object p1, + Object p2, + Object p3, + Object p4, + Object p5, + Object p6, + Object p7, + Object p8 + ) { } @Override public void trace( - Marker marker, - String message, - Object p0, - Object p1, - Object p2, - Object p3, - Object p4, - Object p5, - Object p6, - Object p7, - Object p8, - Object p9) { + Marker marker, + String message, + Object p0, + Object p1, + Object p2, + Object p3, + Object p4, + Object p5, + Object p6, + Object p7, + Object p8, + Object p9 + ) { } @@ -1876,17 +1901,18 @@ public void trace(String message, Object p0, Object p1, Object p2, Object p3, Ob @Override public void trace( - String message, - Object p0, - Object p1, - Object p2, - Object p3, - Object p4, - Object p5, - Object p6, - Object p7, - Object p8, - Object p9) { + String message, + Object p0, + Object p1, + Object p2, + Object p3, + Object p4, + Object p5, + Object p6, + Object p7, + Object p8, + Object p9 + ) { } @@ -2122,49 +2148,52 @@ public void warn(Marker marker, String message, Object p0, Object p1, Object p2, @Override public void warn( - Marker marker, - String message, - Object p0, - Object p1, - Object p2, - Object p3, - Object p4, - Object p5, - Object p6, - Object p7) { + Marker marker, + String message, + Object p0, + Object p1, + Object p2, + Object p3, + Object p4, + Object p5, + Object p6, + Object p7 + ) { } @Override public void warn( - Marker marker, - String message, - Object p0, - Object p1, - Object p2, - Object p3, - Object p4, - Object p5, - Object p6, - Object p7, - Object p8) { + Marker marker, + String message, + Object p0, + Object p1, + Object p2, + Object p3, + Object p4, + Object p5, + Object p6, + Object p7, + Object p8 + ) { } @Override public void warn( - Marker marker, - String message, - Object p0, - Object p1, - Object p2, - Object p3, - Object p4, - Object p5, - Object p6, - Object p7, - Object p8, - Object p9) { + Marker marker, + String message, + Object p0, + Object p1, + Object p2, + Object p3, + Object p4, + Object p5, + Object p6, + Object p7, + Object p8, + Object p9 + ) { } @@ -2215,17 +2244,18 @@ public void warn(String message, Object p0, Object p1, Object p2, Object p3, Obj @Override public void warn( - String message, - Object p0, - Object p1, - Object p2, - Object p3, - Object p4, - Object p5, - Object p6, - Object p7, - Object p8, - Object p9) { + String message, + Object p0, + Object p1, + Object p2, + Object p3, + Object p4, + Object p5, + Object p6, + Object p7, + Object p8, + Object p9 + ) { } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/support/RestorableContextClassLoader.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/support/RestorableContextClassLoader.java index 3a8f06bffbf7e..0032e2e9d3b6e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/support/RestorableContextClassLoader.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/support/RestorableContextClassLoader.java @@ -6,12 +6,12 @@ */ package org.elasticsearch.xpack.core.security.support; +import org.elasticsearch.SpecialPermission; + import java.security.AccessController; import java.security.PrivilegedActionException; import java.security.PrivilegedExceptionAction; -import org.elasticsearch.SpecialPermission; - /** * A try-with-resource compatible object for configuring a thread {@link Thread#getContextClassLoader()}. * On construction this class will set the current (or provided) thread's context class loader. diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/support/Validation.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/support/Validation.java index 6283e21e12859..a02d77eb3e1f3 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/support/Validation.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/support/Validation.java @@ -23,25 +23,119 @@ public final class Validation { static final int MIN_NAME_LENGTH = 1; static final int MAX_NAME_LENGTH = 1024; - static final Set VALID_NAME_CHARS = unmodifiableSet(Sets.newHashSet( - ' ', '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/', - '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', - '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', - 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^', '_', - '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', - 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~' - )); - - private static final String INVALID_NAME_MESSAGE = - "%1s names must be at least " + MIN_NAME_LENGTH + " and no more than " + MAX_NAME_LENGTH + " characters. " + - "They can contain alphanumeric characters (a-z, A-Z, 0-9), spaces, punctuation, and printable symbols in the " + - "Basic Latin (ASCII) block. Leading or trailing whitespace is not allowed."; + static final Set VALID_NAME_CHARS = unmodifiableSet( + Sets.newHashSet( + ' ', + '!', + '"', + '#', + '$', + '%', + '&', + '\'', + '(', + ')', + '*', + '+', + ',', + '-', + '.', + '/', + '0', + '1', + '2', + '3', + '4', + '5', + '6', + '7', + '8', + '9', + ':', + ';', + '<', + '=', + '>', + '?', + '@', + 'A', + 'B', + 'C', + 'D', + 'E', + 'F', + 'G', + 'H', + 'I', + 'J', + 'K', + 'L', + 'M', + 'N', + 'O', + 'P', + 'Q', + 'R', + 'S', + 'T', + 'U', + 'V', + 'W', + 'X', + 'Y', + 'Z', + '[', + '\\', + ']', + '^', + '_', + '`', + 'a', + 'b', + 'c', + 'd', + 'e', + 'f', + 'g', + 'h', + 'i', + 'j', + 'k', + 'l', + 'm', + 'n', + 'o', + 'p', + 'q', + 'r', + 's', + 't', + 'u', + 'v', + 'w', + 'x', + 'y', + 'z', + '{', + '|', + '}', + '~' + ) + ); + + private static final String INVALID_NAME_MESSAGE = "%1s names must be at least " + + MIN_NAME_LENGTH + + " and no more than " + + MAX_NAME_LENGTH + + " characters. " + + "They can contain alphanumeric characters (a-z, A-Z, 0-9), spaces, punctuation, and printable symbols in the " + + "Basic Latin (ASCII) block. Leading or trailing whitespace is not allowed."; private static final Pattern VALID_SERVICE_ACCOUNT_TOKEN_NAME = Pattern.compile("^[a-zA-Z0-9-][a-zA-Z0-9_-]{0,255}$"); - public static final String INVALID_SERVICE_ACCOUNT_TOKEN_NAME_MESSAGE = "service account token name must have at least 1 character " + - "and at most 256 characters that are alphanumeric (A-Z, a-z, 0-9) or hyphen (-) or underscore (_). " + - "It must not begin with an underscore (_)."; + public static final String INVALID_SERVICE_ACCOUNT_TOKEN_NAME_MESSAGE = "service account token name must have at least 1 character " + + "and at most 256 characters that are alphanumeric (A-Z, a-z, 0-9) or hyphen (-) or underscore (_). " + + "It must not begin with an underscore (_)."; private static boolean isValidUserOrRoleName(String name) { if (name.length() < MIN_NAME_LENGTH || name.length() > MAX_NAME_LENGTH) { @@ -99,9 +193,9 @@ public static Error validateUsername(String username, boolean allowReserved, Set } public static Error validatePassword(SecureString password) { - return password.length() >= MIN_PASSWD_LENGTH ? - null : - new Error("passwords must be at least [" + MIN_PASSWD_LENGTH + "] characters long"); + return password.length() >= MIN_PASSWD_LENGTH + ? null + : new Error("passwords must be at least [" + MIN_PASSWD_LENGTH + "] characters long"); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/APMSystemUser.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/APMSystemUser.java index 973f6edf48e77..4e4081d23ccda 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/APMSystemUser.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/APMSystemUser.java @@ -17,6 +17,6 @@ public class APMSystemUser extends User { public static final String ROLE_NAME = UsernamesField.APM_ROLE; public APMSystemUser(boolean enabled) { - super(NAME, new String[]{ ROLE_NAME }, null, null, MetadataUtils.DEFAULT_RESERVED_METADATA, enabled); + super(NAME, new String[] { ROLE_NAME }, null, null, MetadataUtils.DEFAULT_RESERVED_METADATA, enabled); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/AnonymousUser.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/AnonymousUser.java index 56eb1a903e8a7..28be093fb32ea 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/AnonymousUser.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/AnonymousUser.java @@ -23,14 +23,28 @@ public class AnonymousUser extends User { public static final String DEFAULT_ANONYMOUS_USERNAME = "_anonymous"; - public static final Setting USERNAME_SETTING = - new Setting<>(setting("authc.anonymous.username"), DEFAULT_ANONYMOUS_USERNAME, s -> s, Property.NodeScope); - public static final Setting> ROLES_SETTING = - Setting.listSetting(setting("authc.anonymous.roles"), Collections.emptyList(), s -> s, Property.NodeScope); + public static final Setting USERNAME_SETTING = new Setting<>( + setting("authc.anonymous.username"), + DEFAULT_ANONYMOUS_USERNAME, + s -> s, + Property.NodeScope + ); + public static final Setting> ROLES_SETTING = Setting.listSetting( + setting("authc.anonymous.roles"), + Collections.emptyList(), + s -> s, + Property.NodeScope + ); public AnonymousUser(Settings settings) { - super(USERNAME_SETTING.get(settings), ROLES_SETTING.get(settings).toArray(Strings.EMPTY_ARRAY), null, null, - MetadataUtils.DEFAULT_RESERVED_METADATA, isAnonymousEnabled(settings)); + super( + USERNAME_SETTING.get(settings), + ROLES_SETTING.get(settings).toArray(Strings.EMPTY_ARRAY), + null, + null, + MetadataUtils.DEFAULT_RESERVED_METADATA, + isAnonymousEnabled(settings) + ); } public static boolean isAnonymousEnabled(Settings settings) { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/AsyncSearchUser.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/AsyncSearchUser.java index ec7e21faccf62..48633e5a56bb4 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/AsyncSearchUser.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/AsyncSearchUser.java @@ -15,19 +15,21 @@ public class AsyncSearchUser extends User { public static final String NAME = UsernamesField.ASYNC_SEARCH_NAME; public static final AsyncSearchUser INSTANCE = new AsyncSearchUser(); public static final String ROLE_NAME = UsernamesField.ASYNC_SEARCH_ROLE; - public static final RoleDescriptor ROLE_DESCRIPTOR = new RoleDescriptor(ROLE_NAME, - new String[] { "cancel_task" }, - new RoleDescriptor.IndicesPrivileges[] { - RoleDescriptor.IndicesPrivileges.builder() - .indices(XPackPlugin.ASYNC_RESULTS_INDEX + "*") - .privileges("all") - .allowRestrictedIndices(true).build(), - }, - null, - null, - null, - MetadataUtils.DEFAULT_RESERVED_METADATA, - null); + public static final RoleDescriptor ROLE_DESCRIPTOR = new RoleDescriptor( + ROLE_NAME, + new String[] { "cancel_task" }, + new RoleDescriptor.IndicesPrivileges[] { + RoleDescriptor.IndicesPrivileges.builder() + .indices(XPackPlugin.ASYNC_RESULTS_INDEX + "*") + .privileges("all") + .allowRestrictedIndices(true) + .build(), }, + null, + null, + null, + MetadataUtils.DEFAULT_RESERVED_METADATA, + null + ); private AsyncSearchUser() { super(NAME, ROLE_NAME); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/BeatsSystemUser.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/BeatsSystemUser.java index 2d998b4cdc981..839dadb0ec5b8 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/BeatsSystemUser.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/BeatsSystemUser.java @@ -17,6 +17,6 @@ public class BeatsSystemUser extends User { public static final String ROLE_NAME = UsernamesField.BEATS_ROLE; public BeatsSystemUser(boolean enabled) { - super(NAME, new String[]{ ROLE_NAME }, null, null, MetadataUtils.DEFAULT_RESERVED_METADATA, enabled); + super(NAME, new String[] { ROLE_NAME }, null, null, MetadataUtils.DEFAULT_RESERVED_METADATA, enabled); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/ElasticUser.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/ElasticUser.java index dbd00c4b71c5c..afd971fb370c5 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/ElasticUser.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/ElasticUser.java @@ -8,7 +8,6 @@ import org.elasticsearch.xpack.core.security.support.MetadataUtils; - /** * The reserved {@code elastic} superuser. Has full permission/access to the cluster/indices and can * run as any other user. @@ -23,4 +22,3 @@ public ElasticUser(boolean enabled) { super(NAME, new String[] { ROLE_NAME }, null, null, MetadataUtils.DEFAULT_RESERVED_METADATA, enabled); } } - diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/InternalUserSerializationHelper.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/InternalUserSerializationHelper.java index cec764f3f9cfd..409773a13d78b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/InternalUserSerializationHelper.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/InternalUserSerializationHelper.java @@ -29,6 +29,7 @@ public static User readFrom(StreamInput input) throws IOException { } return User.partialReadFrom(username, input); } + public static void writeTo(User user, StreamOutput output) throws IOException { if (SystemUser.is(user)) { output.writeBoolean(true); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/KibanaSystemUser.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/KibanaSystemUser.java index a40a3bfc83170..e8da4e40fc519 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/KibanaSystemUser.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/KibanaSystemUser.java @@ -17,6 +17,6 @@ public class KibanaSystemUser extends User { public static final String ROLE_NAME = UsernamesField.KIBANA_ROLE; public KibanaSystemUser(boolean enabled) { - super(NAME, new String[]{ ROLE_NAME }, null, null, MetadataUtils.DEFAULT_RESERVED_METADATA, enabled); + super(NAME, new String[] { ROLE_NAME }, null, null, MetadataUtils.DEFAULT_RESERVED_METADATA, enabled); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/KibanaUser.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/KibanaUser.java index ef7b937925d1a..507a9a408906f 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/KibanaUser.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/KibanaUser.java @@ -19,7 +19,13 @@ public class KibanaUser extends User { public static final String ROLE_NAME = UsernamesField.KIBANA_ROLE; public KibanaUser(boolean enabled) { - super(NAME, new String[]{ ROLE_NAME }, null, null, - MetadataUtils.getDeprecatedReservedMetadata("Please use the [kibana_system] user instead."), enabled); + super( + NAME, + new String[] { ROLE_NAME }, + null, + null, + MetadataUtils.getDeprecatedReservedMetadata("Please use the [kibana_system] user instead."), + enabled + ); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/LogstashSystemUser.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/LogstashSystemUser.java index e9d64f843b61a..8be90564827ed 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/LogstashSystemUser.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/LogstashSystemUser.java @@ -17,6 +17,6 @@ public class LogstashSystemUser extends User { public static final String ROLE_NAME = UsernamesField.LOGSTASH_ROLE; public LogstashSystemUser(boolean enabled) { - super(NAME, new String[]{ ROLE_NAME }, null, null, MetadataUtils.DEFAULT_RESERVED_METADATA, enabled); + super(NAME, new String[] { ROLE_NAME }, null, null, MetadataUtils.DEFAULT_RESERVED_METADATA, enabled); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/RemoteMonitoringUser.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/RemoteMonitoringUser.java index a81b4cf1dec90..4a12f63921c18 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/RemoteMonitoringUser.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/RemoteMonitoringUser.java @@ -18,6 +18,13 @@ public class RemoteMonitoringUser extends User { public static final String INDEXING_ROLE_NAME = UsernamesField.REMOTE_MONITORING_INDEXING_ROLE; public RemoteMonitoringUser(boolean enabled) { - super(NAME, new String[]{ COLLECTION_ROLE_NAME, INDEXING_ROLE_NAME }, null, null, MetadataUtils.DEFAULT_RESERVED_METADATA, enabled); + super( + NAME, + new String[] { COLLECTION_ROLE_NAME, INDEXING_ROLE_NAME }, + null, + null, + MetadataUtils.DEFAULT_RESERVED_METADATA, + enabled + ); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/User.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/User.java index 13f87b5c0e63e..d9ee948badc24 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/User.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/User.java @@ -6,11 +6,11 @@ */ package org.elasticsearch.xpack.core.security.user; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -30,8 +30,10 @@ public class User implements ToXContentObject { private final Map metadata; private final boolean enabled; - @Nullable private final String fullName; - @Nullable private final String email; + @Nullable + private final String fullName; + @Nullable + private final String email; public User(String username, String... roles) { this(username, roles, null, null, Map.of(), true); @@ -49,8 +51,15 @@ public User(String username, String[] roles, String fullName, String email, Map< this(username, roles, fullName, email, metadata, enabled, null); } - private User(String username, String[] roles, String fullName, String email, Map metadata, boolean enabled, - User authenticatedUser) { + private User( + String username, + String[] roles, + String fullName, + String email, + Map metadata, + boolean enabled, + User authenticatedUser + ) { this.username = Objects.requireNonNull(username); this.roles = roles == null ? Strings.EMPTY_ARRAY : roles; this.metadata = metadata == null ? Map.of() : metadata; @@ -195,7 +204,7 @@ public static User partialReadFrom(String username, StreamInput input) throws IO public static User readFrom(StreamInput input) throws IOException { final boolean isInternalUser = input.readBoolean(); - assert isInternalUser == false: "should always return false. Internal users should use the InternalUserSerializationHelper"; + assert isInternalUser == false : "should always return false. Internal users should use the InternalUserSerializationHelper"; final String username = input.readString(); return partialReadFrom(username, input); } @@ -217,7 +226,9 @@ public static boolean isInternal(User user) { } public static boolean isInternalUsername(String username) { - return SystemUser.NAME.equals(username) || XPackUser.NAME.equals(username) || XPackSecurityUser.NAME.equals(username) + return SystemUser.NAME.equals(username) + || XPackUser.NAME.equals(username) + || XPackSecurityUser.NAME.equals(username) || AsyncSearchUser.NAME.equals(username); } @@ -250,4 +261,3 @@ public interface Fields { ParseField TOKEN = new ParseField("token"); } } - diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/UsernamesField.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/UsernamesField.java index 5b5d90d88a0b8..c5e17656658e6 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/UsernamesField.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/UsernamesField.java @@ -17,7 +17,7 @@ public final class UsernamesField { public static final String XPACK_SECURITY_NAME = "_xpack_security"; public static final String XPACK_SECURITY_ROLE = "superuser"; public static final String XPACK_NAME = "_xpack"; - public static final String XPACK_ROLE = "_xpack"; + public static final String XPACK_ROLE = "_xpack"; public static final String LOGSTASH_NAME = "logstash_system"; public static final String LOGSTASH_ROLE = "logstash_system"; public static final String BEATS_NAME = "beats_system"; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/XPackUser.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/XPackUser.java index 0525155707996..6f7f5c9ee92f0 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/XPackUser.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/user/XPackUser.java @@ -17,18 +17,19 @@ public class XPackUser extends User { public static final String NAME = UsernamesField.XPACK_NAME; public static final String ROLE_NAME = UsernamesField.XPACK_ROLE; - public static final RoleDescriptor ROLE_DESCRIPTOR = new RoleDescriptor(ROLE_NAME, new String[] { "all" }, + public static final RoleDescriptor ROLE_DESCRIPTOR = new RoleDescriptor( + ROLE_NAME, + new String[] { "all" }, new RoleDescriptor.IndicesPrivileges[] { RoleDescriptor.IndicesPrivileges.builder() .indices("/@&~(\\.security.*)&~(\\.async-search.*)/") .privileges("all") .allowRestrictedIndices(true) .build(), - RoleDescriptor.IndicesPrivileges.builder().indices(IndexAuditTrailField.INDEX_NAME_PREFIX + "-*") - .privileges("read").build() - }, + RoleDescriptor.IndicesPrivileges.builder().indices(IndexAuditTrailField.INDEX_NAME_PREFIX + "-*").privileges("read").build() }, new String[] { "*" }, - MetadataUtils.DEFAULT_RESERVED_METADATA); + MetadataUtils.DEFAULT_RESERVED_METADATA + ); public static final XPackUser INSTANCE = new XPackUser(); private XPackUser() { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/xcontent/XContentUtils.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/xcontent/XContentUtils.java index a42e4c3f0dddb..91eb5d71fb260 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/xcontent/XContentUtils.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/xcontent/XContentUtils.java @@ -15,8 +15,7 @@ public class XContentUtils { - private XContentUtils() { - } + private XContentUtils() {} /** * Ensures that we're currently on the start of an object, or that the next token is a start of an object. @@ -38,12 +37,17 @@ public static String[] readStringArray(XContentParser parser, boolean allowNull) if (allowNull) { return null; } - throw new ElasticsearchParseException("could not parse [{}] field. expected a string array but found null value instead", - parser.currentName()); + throw new ElasticsearchParseException( + "could not parse [{}] field. expected a string array but found null value instead", + parser.currentName() + ); } if (parser.currentToken() != XContentParser.Token.START_ARRAY) { - throw new ElasticsearchParseException("could not parse [{}] field. expected a string array but found [{}] value instead", - parser.currentName(), parser.currentToken()); + throw new ElasticsearchParseException( + "could not parse [{}] field. expected a string array but found [{}] value instead", + parser.currentName(), + parser.currentToken() + ); } List list = new ArrayList<>(); @@ -52,8 +56,11 @@ public static String[] readStringArray(XContentParser parser, boolean allowNull) if (token == XContentParser.Token.VALUE_STRING) { list.add(parser.text()); } else { - throw new ElasticsearchParseException("could not parse [{}] field. expected a string array but one of the value in the " + - "array is [{}]", parser.currentName(), token); + throw new ElasticsearchParseException( + "could not parse [{}] field. expected a string array but one of the value in the " + "array is [{}]", + parser.currentName(), + token + ); } } return list.toArray(new String[list.size()]); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/SLMFeatureSetUsage.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/SLMFeatureSetUsage.java index 34ad54dfa0332..93f154d088947 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/SLMFeatureSetUsage.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/SLMFeatureSetUsage.java @@ -8,9 +8,9 @@ package org.elasticsearch.xpack.core.slm; import org.elasticsearch.Version; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.XPackFeatureSet; import org.elasticsearch.xpack.core.XPackField; @@ -70,9 +70,9 @@ public boolean equals(Object obj) { return false; } SLMFeatureSetUsage other = (SLMFeatureSetUsage) obj; - return Objects.equals(available, other.available) && - Objects.equals(enabled, other.enabled) && - Objects.equals(slmStats, other.slmStats); + return Objects.equals(available, other.available) + && Objects.equals(enabled, other.enabled) + && Objects.equals(slmStats, other.slmStats); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/SnapshotInvocationRecord.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/SnapshotInvocationRecord.java index 9121d0c610a31..6946d08ed1191 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/SnapshotInvocationRecord.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/SnapshotInvocationRecord.java @@ -12,12 +12,12 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.core.Nullable; import java.io.IOException; import java.util.Objects; @@ -27,7 +27,10 @@ * presented to the user. This class is used for both successes and failures as the structure of the data is very similar. */ public class SnapshotInvocationRecord extends AbstractDiffable - implements Writeable, ToXContentObject, Diffable { + implements + Writeable, + ToXContentObject, + Diffable { static final ParseField SNAPSHOT_NAME = new ParseField("snapshot_name"); static final ParseField START_TIMESTAMP = new ParseField("start_time"); @@ -39,9 +42,11 @@ public class SnapshotInvocationRecord extends AbstractDiffable PARSER = - new ConstructingObjectParser<>("snapshot_policy_invocation_record", true, - a -> new SnapshotInvocationRecord((String) a[0], (Long) a[1], (long) a[2], (String) a[3])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "snapshot_policy_invocation_record", + true, + a -> new SnapshotInvocationRecord((String) a[0], (Long) a[1], (long) a[2], (String) a[3]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), SNAPSHOT_NAME); @@ -115,10 +120,10 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; SnapshotInvocationRecord that = (SnapshotInvocationRecord) o; - return getSnapshotFinishTimestamp() == that.getSnapshotFinishTimestamp() && - Objects.equals(getSnapshotStartTimestamp(), that.getSnapshotStartTimestamp()) && - Objects.equals(getSnapshotName(), that.getSnapshotName()) && - Objects.equals(getDetails(), that.getDetails()); + return getSnapshotFinishTimestamp() == that.getSnapshotFinishTimestamp() + && Objects.equals(getSnapshotStartTimestamp(), that.getSnapshotStartTimestamp()) + && Objects.equals(getSnapshotName(), that.getSnapshotName()) + && Objects.equals(getDetails(), that.getDetails()); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/SnapshotLifecycleMetadata.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/SnapshotLifecycleMetadata.java index c969b294690ab..f0f89523945d2 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/SnapshotLifecycleMetadata.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/SnapshotLifecycleMetadata.java @@ -13,11 +13,11 @@ import org.elasticsearch.cluster.DiffableUtils; import org.elasticsearch.cluster.NamedDiff; import org.elasticsearch.cluster.metadata.Metadata; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ilm.OperationMode; @@ -44,22 +44,30 @@ public class SnapshotLifecycleMetadata implements Metadata.Custom { private static final ParseField POLICIES_FIELD = new ParseField("policies"); private static final ParseField STATS_FIELD = new ParseField("stats"); - public static final SnapshotLifecycleMetadata EMPTY = - new SnapshotLifecycleMetadata(Collections.emptyMap(), OperationMode.RUNNING, new SnapshotLifecycleStats()); + public static final SnapshotLifecycleMetadata EMPTY = new SnapshotLifecycleMetadata( + Collections.emptyMap(), + OperationMode.RUNNING, + new SnapshotLifecycleStats() + ); @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(TYPE, + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + TYPE, a -> new SnapshotLifecycleMetadata( ((List) a[0]).stream() .collect(Collectors.toMap(m -> m.getPolicy().getId(), Function.identity())), OperationMode.valueOf((String) a[1]), - (SnapshotLifecycleStats) a[2])); + (SnapshotLifecycleStats) a[2] + ) + ); static { - PARSER.declareNamedObjects(ConstructingObjectParser.constructorArg(), (p, c, n) -> SnapshotLifecyclePolicyMetadata.parse(p, n), - v -> { - throw new IllegalArgumentException("ordered " + POLICIES_FIELD.getPreferredName() + " are not supported"); - }, POLICIES_FIELD); + PARSER.declareNamedObjects( + ConstructingObjectParser.constructorArg(), + (p, c, n) -> SnapshotLifecyclePolicyMetadata.parse(p, n), + v -> { throw new IllegalArgumentException("ordered " + POLICIES_FIELD.getPreferredName() + " are not supported"); }, + POLICIES_FIELD + ); PARSER.declareString(ConstructingObjectParser.constructorArg(), OPERATION_MODE_FIELD); PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), (v, o) -> SnapshotLifecycleStats.parse(v), STATS_FIELD); } @@ -68,9 +76,11 @@ public class SnapshotLifecycleMetadata implements Metadata.Custom { private final OperationMode operationMode; private final SnapshotLifecycleStats slmStats; - public SnapshotLifecycleMetadata(Map snapshotConfigurations, - OperationMode operationMode, - SnapshotLifecycleStats slmStats) { + public SnapshotLifecycleMetadata( + Map snapshotConfigurations, + OperationMode operationMode, + SnapshotLifecycleStats slmStats + ) { this.snapshotConfigurations = new HashMap<>(snapshotConfigurations); this.operationMode = operationMode; this.slmStats = slmStats != null ? slmStats : new SnapshotLifecycleStats(); @@ -148,9 +158,9 @@ public boolean equals(Object obj) { return false; } SnapshotLifecycleMetadata other = (SnapshotLifecycleMetadata) obj; - return this.snapshotConfigurations.equals(other.snapshotConfigurations) && - this.operationMode.equals(other.operationMode) && - this.slmStats.equals(other.slmStats); + return this.snapshotConfigurations.equals(other.snapshotConfigurations) + && this.operationMode.equals(other.operationMode) + && this.slmStats.equals(other.slmStats); } public static class SnapshotLifecycleMetadataDiff implements NamedDiff { @@ -160,16 +170,22 @@ public static class SnapshotLifecycleMetadataDiff implements NamedDiff newLifecycles = new TreeMap<>( - lifecycles.apply(((SnapshotLifecycleMetadata) part).snapshotConfigurations)); + lifecycles.apply(((SnapshotLifecycleMetadata) part).snapshotConfigurations) + ); return new SnapshotLifecycleMetadata(newLifecycles, this.operationMode, this.slmStats); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/SnapshotLifecyclePolicy.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/SnapshotLifecyclePolicy.java index c72cb916b033f..5b5b3f8abb9f0 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/SnapshotLifecyclePolicy.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/SnapshotLifecyclePolicy.java @@ -12,18 +12,18 @@ import org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotRequest; import org.elasticsearch.cluster.AbstractDiffable; import org.elasticsearch.cluster.Diffable; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.snapshots.SnapshotsService; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.snapshots.SnapshotsService; import org.elasticsearch.xpack.core.scheduler.Cron; import java.io.IOException; @@ -42,7 +42,10 @@ * to, and the configuration for the snapshot itself. */ public class SnapshotLifecyclePolicy extends AbstractDiffable - implements Writeable, Diffable, ToXContentObject { + implements + Writeable, + Diffable, + ToXContentObject { private final String id; private final String name; @@ -59,16 +62,18 @@ public class SnapshotLifecyclePolicy extends AbstractDiffable PARSER = - new ConstructingObjectParser<>("snapshot_lifecycle", true, - (a, id) -> { - String name = (String) a[0]; - String schedule = (String) a[1]; - String repo = (String) a[2]; - Map config = (Map) a[3]; - SnapshotRetentionConfiguration retention = (SnapshotRetentionConfiguration) a[4]; - return new SnapshotLifecyclePolicy(id, name, schedule, repo, config, retention); - }); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "snapshot_lifecycle", + true, + (a, id) -> { + String name = (String) a[0]; + String schedule = (String) a[1]; + String repo = (String) a[2]; + Map config = (Map) a[3]; + SnapshotRetentionConfiguration retention = (SnapshotRetentionConfiguration) a[4]; + return new SnapshotLifecyclePolicy(id, name, schedule, repo, config, retention); + } + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), NAME); @@ -78,9 +83,14 @@ public class SnapshotLifecyclePolicy extends AbstractDiffable configuration, - @Nullable final SnapshotRetentionConfiguration retentionPolicy) { + public SnapshotLifecyclePolicy( + final String id, + final String name, + final String schedule, + final String repository, + @Nullable final Map configuration, + @Nullable final SnapshotRetentionConfiguration retentionPolicy + ) { this.id = Objects.requireNonNull(id, "policy id is required"); this.name = Objects.requireNonNull(name, "policy snapshot name is required"); this.schedule = Objects.requireNonNull(schedule, "policy schedule is required"); @@ -154,23 +164,25 @@ public ActionRequestValidationException validate() { // ID validation if (Strings.validFileName(id) == false) { - err.addValidationError("invalid policy id [" + id + "]: must not contain the following characters " + - Strings.INVALID_FILENAME_CHARS); + err.addValidationError( + "invalid policy id [" + id + "]: must not contain the following characters " + Strings.INVALID_FILENAME_CHARS + ); } if (id.charAt(0) == '_') { err.addValidationError("invalid policy id [" + id + "]: must not start with '_'"); } int byteCount = id.getBytes(StandardCharsets.UTF_8).length; if (byteCount > MAX_INDEX_NAME_BYTES) { - err.addValidationError("invalid policy id [" + id + "]: name is too long, (" + byteCount + " > " + - MAX_INDEX_NAME_BYTES + " bytes)"); + err.addValidationError( + "invalid policy id [" + id + "]: name is too long, (" + byteCount + " > " + MAX_INDEX_NAME_BYTES + " bytes)" + ); } // Snapshot name validation // We generate a snapshot name here to make sure it validates after applying date math final String snapshotName = generateSnapshotName(this.name); ActionRequestValidationException nameValidationErrors = validateGeneratedSnapshotName(name, snapshotName); - if(nameValidationErrors != null) { + if (nameValidationErrors != null) { err.addValidationErrors(nameValidationErrors.validationErrors()); } @@ -181,30 +193,45 @@ public ActionRequestValidationException validate() { try { new Cron(schedule); } catch (IllegalArgumentException e) { - err.addValidationError("invalid schedule: " + - ExceptionsHelper.unwrapCause(e).getMessage()); + err.addValidationError("invalid schedule: " + ExceptionsHelper.unwrapCause(e).getMessage()); } } if (configuration != null && configuration.containsKey(METADATA_FIELD_NAME)) { if (configuration.get(METADATA_FIELD_NAME) instanceof Map == false) { - err.addValidationError("invalid configuration." + METADATA_FIELD_NAME + " [" + configuration.get(METADATA_FIELD_NAME) + - "]: must be an object if present"); + err.addValidationError( + "invalid configuration." + + METADATA_FIELD_NAME + + " [" + + configuration.get(METADATA_FIELD_NAME) + + "]: must be an object if present" + ); } else { @SuppressWarnings("unchecked") Map metadata = (Map) configuration.get(METADATA_FIELD_NAME); if (metadata.containsKey(SnapshotsService.POLICY_ID_METADATA_FIELD)) { - err.addValidationError("invalid configuration." + METADATA_FIELD_NAME + ": field name [" - + SnapshotsService.POLICY_ID_METADATA_FIELD + "] is reserved and will be added automatically"); + err.addValidationError( + "invalid configuration." + + METADATA_FIELD_NAME + + ": field name [" + + SnapshotsService.POLICY_ID_METADATA_FIELD + + "] is reserved and will be added automatically" + ); } else { Map metadataWithPolicyField = addPolicyNameToMetadata(metadata); int serializedSizeOriginal = CreateSnapshotRequest.metadataSize(metadata); int serializedSizeWithMetadata = CreateSnapshotRequest.metadataSize(metadataWithPolicyField); int policyNameAddedBytes = serializedSizeWithMetadata - serializedSizeOriginal; if (serializedSizeWithMetadata > CreateSnapshotRequest.MAXIMUM_METADATA_BYTES) { - err.addValidationError("invalid configuration." + METADATA_FIELD_NAME + ": must be smaller than [" + - (CreateSnapshotRequest.MAXIMUM_METADATA_BYTES - policyNameAddedBytes) + - "] bytes, but is [" + serializedSizeOriginal + "] bytes"); + err.addValidationError( + "invalid configuration." + + METADATA_FIELD_NAME + + ": must be smaller than [" + + (CreateSnapshotRequest.MAXIMUM_METADATA_BYTES - policyNameAddedBytes) + + "] bytes, but is [" + + serializedSizeOriginal + + "] bytes" + ); } } } @@ -291,12 +318,12 @@ public boolean equals(Object obj) { return false; } SnapshotLifecyclePolicy other = (SnapshotLifecyclePolicy) obj; - return Objects.equals(id, other.id) && - Objects.equals(name, other.name) && - Objects.equals(schedule, other.schedule) && - Objects.equals(repository, other.repository) && - Objects.equals(configuration, other.configuration) && - Objects.equals(retentionPolicy, other.retentionPolicy); + return Objects.equals(id, other.id) + && Objects.equals(name, other.name) + && Objects.equals(schedule, other.schedule) + && Objects.equals(repository, other.repository) + && Objects.equals(configuration, other.configuration) + && Objects.equals(retentionPolicy, other.retentionPolicy); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/SnapshotLifecyclePolicyItem.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/SnapshotLifecyclePolicyItem.java index 21ec3bb8e067c..6a352461c2e1e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/SnapshotLifecyclePolicyItem.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/SnapshotLifecyclePolicyItem.java @@ -8,16 +8,16 @@ package org.elasticsearch.xpack.core.slm; import org.elasticsearch.cluster.SnapshotsInProgress; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.snapshots.SnapshotId; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentFragment; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.snapshots.SnapshotId; import java.io.IOException; import java.util.Objects; @@ -44,9 +44,12 @@ public class SnapshotLifecyclePolicyItem implements ToXContentFragment, Writeabl @Nullable private final SnapshotInvocationRecord lastFailure; - public SnapshotLifecyclePolicyItem(SnapshotLifecyclePolicyMetadata policyMetadata, - @Nullable SnapshotInProgress snapshotInProgress, - @Nullable SnapshotLifecycleStats.SnapshotPolicyStats policyStats) { + + public SnapshotLifecyclePolicyItem( + SnapshotLifecyclePolicyMetadata policyMetadata, + @Nullable SnapshotInProgress snapshotInProgress, + @Nullable SnapshotLifecycleStats.SnapshotPolicyStats policyStats + ) { this.policy = policyMetadata.getPolicy(); this.version = policyMetadata.getVersion(); this.modifiedDate = policyMetadata.getModifiedDate(); @@ -68,10 +71,15 @@ public SnapshotLifecyclePolicyItem(StreamInput in) throws IOException { // For testing - SnapshotLifecyclePolicyItem(SnapshotLifecyclePolicy policy, long version, long modifiedDate, - SnapshotInvocationRecord lastSuccess, SnapshotInvocationRecord lastFailure, - @Nullable SnapshotInProgress snapshotInProgress, - SnapshotLifecycleStats.SnapshotPolicyStats policyStats) { + SnapshotLifecyclePolicyItem( + SnapshotLifecyclePolicy policy, + long version, + long modifiedDate, + SnapshotInvocationRecord lastSuccess, + SnapshotInvocationRecord lastFailure, + @Nullable SnapshotInProgress snapshotInProgress, + SnapshotLifecycleStats.SnapshotPolicyStats policyStats + ) { this.policy = policy; this.version = version; this.modifiedDate = modifiedDate; @@ -80,6 +88,7 @@ public SnapshotLifecyclePolicyItem(StreamInput in) throws IOException { this.snapshotInProgress = snapshotInProgress; this.policyStats = policyStats; } + public SnapshotLifecyclePolicy getPolicy() { return policy; } @@ -134,21 +143,24 @@ public boolean equals(Object obj) { return false; } SnapshotLifecyclePolicyItem other = (SnapshotLifecyclePolicyItem) obj; - return policy.equals(other.policy) && - version == other.version && - modifiedDate == other.modifiedDate && - Objects.equals(lastSuccess, other.lastSuccess) && - Objects.equals(lastFailure, other.lastFailure) && - Objects.equals(snapshotInProgress, other.snapshotInProgress) && - Objects.equals(policyStats, other.policyStats); + return policy.equals(other.policy) + && version == other.version + && modifiedDate == other.modifiedDate + && Objects.equals(lastSuccess, other.lastSuccess) + && Objects.equals(lastFailure, other.lastFailure) + && Objects.equals(snapshotInProgress, other.snapshotInProgress) + && Objects.equals(policyStats, other.policyStats); } @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(policy.getId()); builder.field(SnapshotLifecyclePolicyMetadata.VERSION.getPreferredName(), version); - builder.timeField(SnapshotLifecyclePolicyMetadata.MODIFIED_DATE_MILLIS.getPreferredName(), - SnapshotLifecyclePolicyMetadata.MODIFIED_DATE.getPreferredName(), modifiedDate); + builder.timeField( + SnapshotLifecyclePolicyMetadata.MODIFIED_DATE_MILLIS.getPreferredName(), + SnapshotLifecyclePolicyMetadata.MODIFIED_DATE.getPreferredName(), + modifiedDate + ); builder.field(SnapshotLifecyclePolicyMetadata.POLICY.getPreferredName(), policy); if (lastSuccess != null) { builder.field(SnapshotLifecyclePolicyMetadata.LAST_SUCCESS.getPreferredName(), lastSuccess); @@ -156,8 +168,11 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws if (lastFailure != null) { builder.field(SnapshotLifecyclePolicyMetadata.LAST_FAILURE.getPreferredName(), lastFailure); } - builder.timeField(SnapshotLifecyclePolicyMetadata.NEXT_EXECUTION_MILLIS.getPreferredName(), - SnapshotLifecyclePolicyMetadata.NEXT_EXECUTION.getPreferredName(), policy.calculateNextExecution()); + builder.timeField( + SnapshotLifecyclePolicyMetadata.NEXT_EXECUTION_MILLIS.getPreferredName(), + SnapshotLifecyclePolicyMetadata.NEXT_EXECUTION.getPreferredName(), + policy.calculateNextExecution() + ); if (snapshotInProgress != null) { builder.field(SNAPSHOT_IN_PROGRESS.getPreferredName(), snapshotInProgress); } @@ -200,8 +215,7 @@ public SnapshotInProgress(SnapshotId snapshotId, SnapshotsInProgress.State state } public static SnapshotInProgress fromEntry(SnapshotsInProgress.Entry entry) { - return new SnapshotInProgress(entry.snapshot().getSnapshotId(), - entry.state(), entry.startTime(), entry.failure()); + return new SnapshotInProgress(entry.snapshot().getSnapshotId(), entry.state(), entry.startTime(), entry.failure()); } public SnapshotId getSnapshotId() { @@ -257,10 +271,10 @@ public boolean equals(Object obj) { return false; } SnapshotInProgress other = (SnapshotInProgress) obj; - return Objects.equals(snapshotId, other.snapshotId) && - Objects.equals(state, other.state) && - startTime == other.startTime && - Objects.equals(failure, other.failure); + return Objects.equals(snapshotId, other.snapshotId) + && Objects.equals(state, other.state) + && startTime == other.startTime + && Objects.equals(failure, other.failure); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/SnapshotLifecyclePolicyMetadata.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/SnapshotLifecyclePolicyMetadata.java index 78153ec9f06e8..27a6a30e8d1f6 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/SnapshotLifecyclePolicyMetadata.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/SnapshotLifecyclePolicyMetadata.java @@ -9,12 +9,12 @@ import org.elasticsearch.cluster.AbstractDiffable; import org.elasticsearch.cluster.Diffable; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -33,7 +33,9 @@ * incrementing number), and last modified date */ public class SnapshotLifecyclePolicyMetadata extends AbstractDiffable - implements ToXContentObject, Diffable { + implements + ToXContentObject, + Diffable { static final ParseField POLICY = new ParseField("policy"); static final ParseField HEADERS = new ParseField("headers"); @@ -55,22 +57,22 @@ public class SnapshotLifecyclePolicyMetadata extends AbstractDiffable PARSER = - new ConstructingObjectParser<>("snapshot_policy_metadata", - a -> { - SnapshotLifecyclePolicy policy = (SnapshotLifecyclePolicy) a[0]; - SnapshotInvocationRecord lastSuccess = (SnapshotInvocationRecord) a[4]; - SnapshotInvocationRecord lastFailure = (SnapshotInvocationRecord) a[5]; - - return builder() - .setPolicy(policy) - .setHeaders((Map) a[1]) - .setVersion((long) a[2]) - .setModifiedDate((long) a[3]) - .setLastSuccess(lastSuccess) - .setLastFailure(lastFailure) - .build(); - }); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "snapshot_policy_metadata", + a -> { + SnapshotLifecyclePolicy policy = (SnapshotLifecyclePolicy) a[0]; + SnapshotInvocationRecord lastSuccess = (SnapshotInvocationRecord) a[4]; + SnapshotInvocationRecord lastFailure = (SnapshotInvocationRecord) a[5]; + + return builder().setPolicy(policy) + .setHeaders((Map) a[1]) + .setVersion((long) a[2]) + .setModifiedDate((long) a[3]) + .setLastSuccess(lastSuccess) + .setLastFailure(lastFailure) + .build(); + } + ); static { PARSER.declareObject(ConstructingObjectParser.constructorArg(), SnapshotLifecyclePolicy::parse, POLICY); @@ -85,8 +87,14 @@ public static SnapshotLifecyclePolicyMetadata parse(XContentParser parser, Strin return PARSER.apply(parser, name); } - SnapshotLifecyclePolicyMetadata(SnapshotLifecyclePolicy policy, Map headers, long version, long modifiedDate, - SnapshotInvocationRecord lastSuccess, SnapshotInvocationRecord lastFailure) { + SnapshotLifecyclePolicyMetadata( + SnapshotLifecyclePolicy policy, + Map headers, + long version, + long modifiedDate, + SnapshotInvocationRecord lastSuccess, + SnapshotInvocationRecord lastFailure + ) { this.policy = policy; this.headers = headers; assertNoAuthorizationHeader(this.headers); @@ -125,8 +133,7 @@ public static Builder builder(SnapshotLifecyclePolicyMetadata metadata) { if (metadata == null) { return builder(); } - return new Builder() - .setHeaders(metadata.getHeaders()) + return new Builder().setHeaders(metadata.getHeaders()) .setPolicy(metadata.getPolicy()) .setVersion(metadata.getVersion()) .setModifiedDate(metadata.getModifiedDate()) @@ -193,12 +200,12 @@ public boolean equals(Object obj) { return false; } SnapshotLifecyclePolicyMetadata other = (SnapshotLifecyclePolicyMetadata) obj; - return Objects.equals(policy, other.policy) && - Objects.equals(headers, other.headers) && - Objects.equals(version, other.version) && - Objects.equals(modifiedDate, other.modifiedDate) && - Objects.equals(lastSuccess, other.lastSuccess) && - Objects.equals(lastFailure, other.lastFailure); + return Objects.equals(policy, other.policy) + && Objects.equals(headers, other.headers) + && Objects.equals(version, other.version) + && Objects.equals(modifiedDate, other.modifiedDate) + && Objects.equals(lastSuccess, other.lastSuccess) + && Objects.equals(lastFailure, other.lastFailure); } @Override @@ -211,8 +218,7 @@ public String toString() { public static class Builder { - private Builder() { - } + private Builder() {} private SnapshotLifecyclePolicy policy; private Map headers; @@ -258,7 +264,8 @@ public SnapshotLifecyclePolicyMetadata build() { version, Objects.requireNonNull(modifiedDate, "modifiedDate must be set"), lastSuccessDate, - lastFailureDate); + lastFailureDate + ); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/SnapshotLifecycleStats.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/SnapshotLifecycleStats.java index d3347447828ae..f834f25496080 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/SnapshotLifecycleStats.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/SnapshotLifecycleStats.java @@ -7,7 +7,6 @@ package org.elasticsearch.xpack.core.slm; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; @@ -15,6 +14,7 @@ import org.elasticsearch.common.metrics.CounterMetric; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentFragment; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -56,17 +56,19 @@ public class SnapshotLifecycleStats implements Writeable, ToXContentObject { public static final ParseField TOTAL_DELETION_FAILURES = new ParseField("total_snapshot_deletion_failures"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("snapshot_policy_stats", true, - a -> { - long runs = (long) a[0]; - long failed = (long) a[1]; - long timedOut = (long) a[2]; - long timeMs = (long) a[3]; - Map policyStatsMap = ((List) a[4]).stream() - .collect(Collectors.toMap(m -> m.policyId, Function.identity())); - return new SnapshotLifecycleStats(runs, failed, timedOut, timeMs, policyStatsMap); - }); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "snapshot_policy_stats", + true, + a -> { + long runs = (long) a[0]; + long failed = (long) a[1]; + long timedOut = (long) a[2]; + long timeMs = (long) a[3]; + Map policyStatsMap = ((List) a[4]).stream() + .collect(Collectors.toMap(m -> m.policyId, Function.identity())); + return new SnapshotLifecycleStats(runs, failed, timedOut, timeMs, policyStatsMap); + } + ); static { PARSER.declareLong(ConstructingObjectParser.constructorArg(), RETENTION_RUNS); @@ -81,8 +83,13 @@ public SnapshotLifecycleStats() { } // public for testing - public SnapshotLifecycleStats(long retentionRuns, long retentionFailed, long retentionTimedOut, long retentionTimeMs, - Map policyStats) { + public SnapshotLifecycleStats( + long retentionRuns, + long retentionFailed, + long retentionTimedOut, + long retentionTimeMs, + Map policyStats + ) { this.retentionRunCount.inc(retentionRuns); this.retentionFailedCount.inc(retentionFailed); this.retentionTimedOut.inc(retentionTimedOut); @@ -106,30 +113,35 @@ public SnapshotLifecycleStats merge(SnapshotLifecycleStats other) { HashMap newPolicyStats = new HashMap<>(this.policyStats); // Merges the per-run stats (the stats in "other") with the stats already present - other.policyStats - .forEach((policyId, perRunPolicyStats) -> { - newPolicyStats.compute(policyId, (k, existingPolicyMetrics) -> { - if (existingPolicyMetrics == null) { - return perRunPolicyStats; - } else { - return existingPolicyMetrics.merge(perRunPolicyStats); - } - }); + other.policyStats.forEach((policyId, perRunPolicyStats) -> { + newPolicyStats.compute(policyId, (k, existingPolicyMetrics) -> { + if (existingPolicyMetrics == null) { + return perRunPolicyStats; + } else { + return existingPolicyMetrics.merge(perRunPolicyStats); + } }); + }); - return new SnapshotLifecycleStats(this.retentionRunCount.count() + other.retentionRunCount.count(), + return new SnapshotLifecycleStats( + this.retentionRunCount.count() + other.retentionRunCount.count(), this.retentionFailedCount.count() + other.retentionFailedCount.count(), this.retentionTimedOut.count() + other.retentionTimedOut.count(), this.retentionTimeMs.count() + other.retentionTimeMs.count(), - newPolicyStats); + newPolicyStats + ); } public SnapshotLifecycleStats removePolicy(String policyId) { Map policyStats = new HashMap<>(this.policyStats); policyStats.remove(policyId); - return new SnapshotLifecycleStats(this.retentionRunCount.count(), this.retentionFailedCount.count(), - this.retentionTimedOut.count(), this.retentionTimeMs.count(), - policyStats); + return new SnapshotLifecycleStats( + this.retentionRunCount.count(), + this.retentionFailedCount.count(), + this.retentionTimedOut.count(), + this.retentionTimeMs.count(), + policyStats + ); } /** @@ -215,7 +227,8 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.field(RETENTION_TIME.getPreferredName(), retentionTime); builder.field(RETENTION_TIME_MILLIS.getPreferredName(), retentionTime.millis()); - List metrics = getMetrics().values().stream() + List metrics = getMetrics().values() + .stream() .sorted(Comparator.comparing(SnapshotPolicyStats::getPolicyId)) // maintain a consistent order when serializing .collect(Collectors.toList()); long totalTaken = metrics.stream().mapToLong(s -> s.snapshotsTaken.count()).sum(); @@ -240,8 +253,13 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws @Override public int hashCode() { - return Objects.hash(retentionRunCount.count(), retentionFailedCount.count(), - retentionTimedOut.count(), retentionTimeMs.count(), policyStats); + return Objects.hash( + retentionRunCount.count(), + retentionFailedCount.count(), + retentionTimedOut.count(), + retentionTimeMs.count(), + policyStats + ); } @Override @@ -253,11 +271,11 @@ public boolean equals(Object obj) { return false; } SnapshotLifecycleStats other = (SnapshotLifecycleStats) obj; - return Objects.equals(retentionRunCount.count(), other.retentionRunCount.count()) && - Objects.equals(retentionFailedCount.count(), other.retentionFailedCount.count()) && - Objects.equals(retentionTimedOut.count(), other.retentionTimedOut.count()) && - Objects.equals(retentionTimeMs.count(), other.retentionTimeMs.count()) && - Objects.equals(policyStats, other.policyStats); + return Objects.equals(retentionRunCount.count(), other.retentionRunCount.count()) + && Objects.equals(retentionFailedCount.count(), other.retentionFailedCount.count()) + && Objects.equals(retentionTimedOut.count(), other.retentionTimedOut.count()) + && Objects.equals(retentionTimeMs.count(), other.retentionTimeMs.count()) + && Objects.equals(policyStats, other.policyStats); } @Override @@ -278,16 +296,18 @@ public static class SnapshotPolicyStats implements Writeable, ToXContentFragment public static final ParseField SNAPSHOTS_DELETED = new ParseField("snapshots_deleted"); public static final ParseField SNAPSHOT_DELETION_FAILURES = new ParseField("snapshot_deletion_failures"); - static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("snapshot_policy_stats", true, - a -> { - String id = (String) a[0]; - long taken = (long) a[1]; - long failed = (long) a[2]; - long deleted = (long) a[3]; - long deleteFailed = (long) a[4]; - return new SnapshotPolicyStats(id, taken, failed, deleted, deleteFailed); - }); + static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "snapshot_policy_stats", + true, + a -> { + String id = (String) a[0]; + long taken = (long) a[1]; + long failed = (long) a[2]; + long deleted = (long) a[3]; + long deleteFailed = (long) a[4]; + return new SnapshotPolicyStats(id, taken, failed, deleted, deleteFailed); + } + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), POLICY_ID); @@ -327,7 +347,8 @@ public SnapshotPolicyStats merge(SnapshotPolicyStats other) { this.snapshotsTaken.count() + other.snapshotsTaken.count(), this.snapshotsFailed.count() + other.snapshotsFailed.count(), this.snapshotsDeleted.count() + other.snapshotsDeleted.count(), - this.snapshotDeleteFailures.count() + other.snapshotDeleteFailures.count()); + this.snapshotDeleteFailures.count() + other.snapshotDeleteFailures.count() + ); } void snapshotTaken() { @@ -361,8 +382,13 @@ public void writeTo(StreamOutput out) throws IOException { @Override public int hashCode() { - return Objects.hash(policyId, snapshotsTaken.count(), snapshotsFailed.count(), - snapshotsDeleted.count(), snapshotDeleteFailures.count()); + return Objects.hash( + policyId, + snapshotsTaken.count(), + snapshotsFailed.count(), + snapshotsDeleted.count(), + snapshotDeleteFailures.count() + ); } @Override @@ -374,11 +400,11 @@ public boolean equals(Object obj) { return false; } SnapshotPolicyStats other = (SnapshotPolicyStats) obj; - return Objects.equals(policyId, other.policyId) && - Objects.equals(snapshotsTaken.count(), other.snapshotsTaken.count()) && - Objects.equals(snapshotsFailed.count(), other.snapshotsFailed.count()) && - Objects.equals(snapshotsDeleted.count(), other.snapshotsDeleted.count()) && - Objects.equals(snapshotDeleteFailures.count(), other.snapshotDeleteFailures.count()); + return Objects.equals(policyId, other.policyId) + && Objects.equals(snapshotsTaken.count(), other.snapshotsTaken.count()) + && Objects.equals(snapshotsFailed.count(), other.snapshotsFailed.count()) + && Objects.equals(snapshotsDeleted.count(), other.snapshotsDeleted.count()) + && Objects.equals(snapshotDeleteFailures.count(), other.snapshotDeleteFailures.count()); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/SnapshotRetentionConfiguration.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/SnapshotRetentionConfiguration.java index 48adb51aaf51c..58a18791cc707 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/SnapshotRetentionConfiguration.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/SnapshotRetentionConfiguration.java @@ -10,19 +10,19 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.message.ParameterizedMessage; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.snapshots.SnapshotInfo; +import org.elasticsearch.snapshots.SnapshotState; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.snapshots.SnapshotInfo; -import org.elasticsearch.snapshots.SnapshotState; import java.io.IOException; import java.util.Comparator; @@ -45,13 +45,16 @@ public class SnapshotRetentionConfiguration implements ToXContentObject, Writeab private static final Set UNSUCCESSFUL_STATES = EnumSet.of(SnapshotState.FAILED, SnapshotState.PARTIAL); - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("snapshot_retention", true, a -> { + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "snapshot_retention", + true, + a -> { TimeValue expireAfter = a[0] == null ? null : TimeValue.parseTimeValue((String) a[0], EXPIRE_AFTER.getPreferredName()); Integer minCount = (Integer) a[1]; Integer maxCount = (Integer) a[2]; return new SnapshotRetentionConfiguration(expireAfter, minCount, maxCount); - }); + } + ); static { PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), EXPIRE_AFTER); @@ -71,16 +74,20 @@ public class SnapshotRetentionConfiguration implements ToXContentObject, Writeab this.maximumSnapshotCount = in.readOptionalVInt(); } - public SnapshotRetentionConfiguration(@Nullable TimeValue expireAfter, - @Nullable Integer minimumSnapshotCount, - @Nullable Integer maximumSnapshotCount) { + public SnapshotRetentionConfiguration( + @Nullable TimeValue expireAfter, + @Nullable Integer minimumSnapshotCount, + @Nullable Integer maximumSnapshotCount + ) { this(System::currentTimeMillis, expireAfter, minimumSnapshotCount, maximumSnapshotCount); } - public SnapshotRetentionConfiguration(LongSupplier nowSupplier, - @Nullable TimeValue expireAfter, - @Nullable Integer minimumSnapshotCount, - @Nullable Integer maximumSnapshotCount) { + public SnapshotRetentionConfiguration( + LongSupplier nowSupplier, + @Nullable TimeValue expireAfter, + @Nullable Integer minimumSnapshotCount, + @Nullable Integer maximumSnapshotCount + ) { this.nowSupplier = nowSupplier; this.expireAfter = expireAfter; this.minimumSnapshotCount = minimumSnapshotCount; @@ -92,8 +99,12 @@ public SnapshotRetentionConfiguration(LongSupplier nowSupplier, throw new IllegalArgumentException("maximum snapshot count must be at least 1, but was: " + this.maximumSnapshotCount); } if ((maximumSnapshotCount != null && minimumSnapshotCount != null) && this.minimumSnapshotCount > this.maximumSnapshotCount) { - throw new IllegalArgumentException("minimum snapshot count " + this.minimumSnapshotCount + - " cannot be larger than maximum snapshot count " + this.maximumSnapshotCount); + throw new IllegalArgumentException( + "minimum snapshot count " + + this.minimumSnapshotCount + + " cannot be larger than maximum snapshot count " + + this.maximumSnapshotCount + ); } } @@ -163,15 +174,25 @@ public Predicate getSnapshotDeletionPredicate(final List getSnapshotDeletionPredicate(final List getSnapshotDeletionPredicate(final List this.expireAfter.getMillis()) { - logger.trace(() -> new ParameterizedMessage("[{}]: ELIGIBLE as snapshot age of {} is older than {}", - snapName, new TimeValue(snapshotAge).toHumanReadableString(3), this.expireAfter.toHumanReadableString(3))); + logger.trace( + () -> new ParameterizedMessage( + "[{}]: ELIGIBLE as snapshot age of {} is older than {}", + snapName, + new TimeValue(snapshotAge).toHumanReadableString(3), + this.expireAfter.toHumanReadableString(3) + ) + ); return true; } else { - logger.trace(() -> new ParameterizedMessage("[{}]: INELIGIBLE as snapshot age of [{}ms] is newer than {}", - snapName, new TimeValue(snapshotAge).toHumanReadableString(3), this.expireAfter.toHumanReadableString(3))); + logger.trace( + () -> new ParameterizedMessage( + "[{}]: INELIGIBLE as snapshot age of [{}ms] is newer than {}", + snapName, + new TimeValue(snapshotAge).toHumanReadableString(3), + this.expireAfter.toHumanReadableString(3) + ) + ); return false; } } @@ -271,9 +316,9 @@ public boolean equals(Object obj) { return false; } SnapshotRetentionConfiguration other = (SnapshotRetentionConfiguration) obj; - return Objects.equals(this.expireAfter, other.expireAfter) && - Objects.equals(minimumSnapshotCount, other.minimumSnapshotCount) && - Objects.equals(maximumSnapshotCount, other.maximumSnapshotCount); + return Objects.equals(this.expireAfter, other.expireAfter) + && Objects.equals(minimumSnapshotCount, other.minimumSnapshotCount) + && Objects.equals(maximumSnapshotCount, other.maximumSnapshotCount); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/action/DeleteSnapshotLifecycleAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/action/DeleteSnapshotLifecycleAction.java index 738270182222c..3ba7c09af34a3 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/action/DeleteSnapshotLifecycleAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/action/DeleteSnapshotLifecycleAction.java @@ -35,7 +35,7 @@ public Request(StreamInput in) throws IOException { lifecycleId = in.readString(); } - public Request() { } + public Request() {} public Request(String lifecycleId) { this.lifecycleId = Objects.requireNonNull(lifecycleId, "id may not be null"); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/action/ExecuteSnapshotLifecycleAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/action/ExecuteSnapshotLifecycleAction.java index 140065dabac5f..848cca8d9d951 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/action/ExecuteSnapshotLifecycleAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/action/ExecuteSnapshotLifecycleAction.java @@ -45,7 +45,7 @@ public Request(StreamInput in) throws IOException { lifecycleId = in.readString(); } - public Request() { } + public Request() {} public String getLifecycleId() { return this.lifecycleId; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/action/ExecuteSnapshotRetentionAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/action/ExecuteSnapshotRetentionAction.java index ed71dc7d3d77b..2111e35f6cb46 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/action/ExecuteSnapshotRetentionAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/action/ExecuteSnapshotRetentionAction.java @@ -27,7 +27,7 @@ protected ExecuteSnapshotRetentionAction() { public static class Request extends AcknowledgedRequest implements ToXContentObject { - public Request() { } + public Request() {} public Request(StreamInput in) throws IOException { super(in); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/action/GetSLMStatusAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/action/GetSLMStatusAction.java index 65240b202050a..a2a4831cde94b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/action/GetSLMStatusAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/action/GetSLMStatusAction.java @@ -60,8 +60,7 @@ public Request(StreamInput in) throws IOException { super(in); } - public Request() { - } + public Request() {} @Override public ActionRequestValidationException validate() { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/action/GetSnapshotLifecycleAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/action/GetSnapshotLifecycleAction.java index d4c7066ee5f25..0e9fe37127476 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/action/GetSnapshotLifecycleAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/action/GetSnapshotLifecycleAction.java @@ -85,7 +85,7 @@ public static class Response extends ActionResponse implements ToXContentObject private List lifecycles; - public Response() { } + public Response() {} public Response(List lifecycles) { this.lifecycles = lifecycles; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/action/GetSnapshotLifecycleStatsAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/action/GetSnapshotLifecycleStatsAction.java index 5b2586e03a1aa..8c69de784cb20 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/action/GetSnapshotLifecycleStatsAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/action/GetSnapshotLifecycleStatsAction.java @@ -36,7 +36,7 @@ protected GetSnapshotLifecycleStatsAction() { public static class Request extends AcknowledgedRequest { - public Request() { } + public Request() {} public Request(StreamInput in) throws IOException { super(in); @@ -57,7 +57,7 @@ public static class Response extends ActionResponse implements ToXContentObject private SnapshotLifecycleStats slmStats; - public Response() { } + public Response() {} public Response(SnapshotLifecycleStats slmStats) { this.slmStats = slmStats; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/action/PutSnapshotLifecycleAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/action/PutSnapshotLifecycleAction.java index 68c6ac2b1c1df..22d7ce9c916da 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/action/PutSnapshotLifecycleAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/action/PutSnapshotLifecycleAction.java @@ -46,7 +46,7 @@ public Request(StreamInput in) throws IOException { lifecycle = new SnapshotLifecyclePolicy(in); } - public Request() { } + public Request() {} public String getLifecycleId() { return this.lifecycleId; @@ -94,8 +94,7 @@ public boolean equals(Object obj) { return false; } Request other = (Request) obj; - return lifecycleId.equals(other.lifecycleId) && - lifecycle.equals(other.lifecycle); + return lifecycleId.equals(other.lifecycleId) && lifecycle.equals(other.lifecycle); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/action/StartSLMAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/action/StartSLMAction.java index 5ee8df1f27b78..162ba0369506d 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/action/StartSLMAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/action/StartSLMAction.java @@ -29,8 +29,7 @@ public Request(StreamInput in) throws IOException { super(in); } - public Request() { - } + public Request() {} @Override public ActionRequestValidationException validate() { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/action/StopSLMAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/action/StopSLMAction.java index 2acc62deb16e5..7b8d2b9687858 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/action/StopSLMAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/action/StopSLMAction.java @@ -29,8 +29,7 @@ public Request(StreamInput in) throws IOException { super(in); } - public Request() { - } + public Request() {} @Override public ActionRequestValidationException validate() { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/history/SnapshotHistoryItem.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/history/SnapshotHistoryItem.java index e03b05fd5676d..7c4c5ac1b56ad 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/history/SnapshotHistoryItem.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/history/SnapshotHistoryItem.java @@ -8,14 +8,14 @@ package org.elasticsearch.xpack.core.slm.history; import org.elasticsearch.ElasticsearchException; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -59,20 +59,30 @@ public class SnapshotHistoryItem implements Writeable, ToXContentObject { static final ParseField ERROR_DETAILS = new ParseField("error_details"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("snapshot_lifecycle_history_item", true, - (a, id) -> { - final long timestamp = (long) a[0]; - final String policyId = (String) a[1]; - final String repository = (String) a[2]; - final String snapshotName = (String) a[3]; - final String operation = (String) a[4]; - final boolean success = (boolean) a[5]; - final Map snapshotConfiguration = (Map) a[6]; - final String errorDetails = (String) a[7]; - return new SnapshotHistoryItem(timestamp, policyId, repository, snapshotName, operation, success, - snapshotConfiguration, errorDetails); - }); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "snapshot_lifecycle_history_item", + true, + (a, id) -> { + final long timestamp = (long) a[0]; + final String policyId = (String) a[1]; + final String repository = (String) a[2]; + final String snapshotName = (String) a[3]; + final String operation = (String) a[4]; + final boolean success = (boolean) a[5]; + final Map snapshotConfiguration = (Map) a[6]; + final String errorDetails = (String) a[7]; + return new SnapshotHistoryItem( + timestamp, + policyId, + repository, + snapshotName, + operation, + success, + snapshotConfiguration, + errorDetails + ); + } + ); static { PARSER.declareLong(ConstructingObjectParser.constructorArg(), TIMESTAMP); @@ -89,8 +99,16 @@ public static SnapshotHistoryItem parse(XContentParser parser, String name) { return PARSER.apply(parser, name); } - SnapshotHistoryItem(long timestamp, String policyId, String repository, String snapshotName, String operation, - boolean success, Map snapshotConfiguration, String errorDetails) { + SnapshotHistoryItem( + long timestamp, + String policyId, + String repository, + String snapshotName, + String operation, + boolean success, + Map snapshotConfiguration, + String errorDetails + ) { this.timestamp = timestamp; this.policyId = Objects.requireNonNull(policyId); this.repository = Objects.requireNonNull(repository); @@ -102,26 +120,50 @@ public static SnapshotHistoryItem parse(XContentParser parser, String name) { } public static SnapshotHistoryItem creationSuccessRecord(long timestamp, SnapshotLifecyclePolicy policy, String snapshotName) { - return new SnapshotHistoryItem(timestamp, policy.getId(), policy.getRepository(), snapshotName, CREATE_OPERATION, true, - policy.getConfig(), null); + return new SnapshotHistoryItem( + timestamp, + policy.getId(), + policy.getRepository(), + snapshotName, + CREATE_OPERATION, + true, + policy.getConfig(), + null + ); } - public static SnapshotHistoryItem creationFailureRecord(long timeStamp, SnapshotLifecyclePolicy policy, String snapshotName, - Exception exception) throws IOException { + public static SnapshotHistoryItem creationFailureRecord( + long timeStamp, + SnapshotLifecyclePolicy policy, + String snapshotName, + Exception exception + ) throws IOException { String exceptionString = exceptionToString(exception); - return new SnapshotHistoryItem(timeStamp, policy.getId(), policy.getRepository(), snapshotName, CREATE_OPERATION, false, - policy.getConfig(), exceptionString); + return new SnapshotHistoryItem( + timeStamp, + policy.getId(), + policy.getRepository(), + snapshotName, + CREATE_OPERATION, + false, + policy.getConfig(), + exceptionString + ); } public static SnapshotHistoryItem deletionSuccessRecord(long timestamp, String snapshotName, String policyId, String repository) { return new SnapshotHistoryItem(timestamp, policyId, repository, snapshotName, DELETE_OPERATION, true, null, null); } - public static SnapshotHistoryItem deletionFailureRecord(long timestamp, String snapshotName, String policyId, String repository, - Exception exception) throws IOException { + public static SnapshotHistoryItem deletionFailureRecord( + long timestamp, + String snapshotName, + String policyId, + String repository, + Exception exception + ) throws IOException { String exceptionString = exceptionToString(exception); - return new SnapshotHistoryItem(timestamp, policyId, repository, snapshotName, DELETE_OPERATION, false, - null, exceptionString); + return new SnapshotHistoryItem(timestamp, policyId, repository, snapshotName, DELETE_OPERATION, false, null, exceptionString); } public SnapshotHistoryItem(StreamInput in) throws IOException { @@ -205,22 +247,30 @@ public boolean equals(Object o) { if (this == o) result = true; if (o == null || getClass() != o.getClass()) result = false; SnapshotHistoryItem that1 = (SnapshotHistoryItem) o; - result = isSuccess() == that1.isSuccess() && - timestamp == that1.getTimestamp() && - Objects.equals(getPolicyId(), that1.getPolicyId()) && - Objects.equals(getRepository(), that1.getRepository()) && - Objects.equals(getSnapshotName(), that1.getSnapshotName()) && - Objects.equals(getOperation(), that1.getOperation()); + result = isSuccess() == that1.isSuccess() + && timestamp == that1.getTimestamp() + && Objects.equals(getPolicyId(), that1.getPolicyId()) + && Objects.equals(getRepository(), that1.getRepository()) + && Objects.equals(getSnapshotName(), that1.getSnapshotName()) + && Objects.equals(getOperation(), that1.getOperation()); if (result == false) return false; SnapshotHistoryItem that = (SnapshotHistoryItem) o; - return Objects.equals(getSnapshotConfiguration(), that.getSnapshotConfiguration()) && - Objects.equals(getErrorDetails(), that.getErrorDetails()); + return Objects.equals(getSnapshotConfiguration(), that.getSnapshotConfiguration()) + && Objects.equals(getErrorDetails(), that.getErrorDetails()); } @Override public int hashCode() { - return Objects.hash(getTimestamp(), getPolicyId(), getRepository(), getSnapshotName(), getOperation(), isSuccess(), - getSnapshotConfiguration(), getErrorDetails()); + return Objects.hash( + getTimestamp(), + getPolicyId(), + getRepository(), + getSnapshotName(), + getOperation(), + isSuccess(), + getSnapshotConfiguration(), + getErrorDetails() + ); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/history/SnapshotHistoryStore.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/history/SnapshotHistoryStore.java index 70f86a2996951..2b47244b790a2 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/history/SnapshotHistoryStore.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/history/SnapshotHistoryStore.java @@ -53,33 +53,51 @@ public SnapshotHistoryStore(Settings nodeSettings, Client client, ClusterService */ public void putAsync(SnapshotHistoryItem item) { if (slmHistoryEnabled == false) { - logger.trace("not recording snapshot history item because [{}] is [false]: [{}]", - SLM_HISTORY_INDEX_ENABLED_SETTING.getKey(), item); + logger.trace( + "not recording snapshot history item because [{}] is [false]: [{}]", + SLM_HISTORY_INDEX_ENABLED_SETTING.getKey(), + item + ); return; } logger.trace("about to index snapshot history item in data stream [{}]: [{}]", SLM_HISTORY_DATA_STREAM, item); Metadata metadata = clusterService.state().getMetadata(); - if (metadata.dataStreams().containsKey(SLM_HISTORY_DATA_STREAM) == false && - metadata.templatesV2().containsKey(SLM_TEMPLATE_NAME) == false) { - logger.error(new ParameterizedMessage("failed to index snapshot history item, data stream [{}] and template [{}] don't exist", - SLM_HISTORY_DATA_STREAM, SLM_TEMPLATE_NAME)); + if (metadata.dataStreams().containsKey(SLM_HISTORY_DATA_STREAM) == false + && metadata.templatesV2().containsKey(SLM_TEMPLATE_NAME) == false) { + logger.error( + new ParameterizedMessage( + "failed to index snapshot history item, data stream [{}] and template [{}] don't exist", + SLM_HISTORY_DATA_STREAM, + SLM_TEMPLATE_NAME + ) + ); return; } try (XContentBuilder builder = XContentFactory.jsonBuilder()) { item.toXContent(builder, ToXContent.EMPTY_PARAMS); - IndexRequest request = new IndexRequest(SLM_HISTORY_DATA_STREAM) - .opType(DocWriteRequest.OpType.CREATE) - .source(builder); + IndexRequest request = new IndexRequest(SLM_HISTORY_DATA_STREAM).opType(DocWriteRequest.OpType.CREATE).source(builder); client.index(request, ActionListener.wrap(indexResponse -> { - logger.debug("successfully indexed snapshot history item with id [{}] in data stream [{}]: [{}]", - indexResponse.getId(), SLM_HISTORY_DATA_STREAM, item); + logger.debug( + "successfully indexed snapshot history item with id [{}] in data stream [{}]: [{}]", + indexResponse.getId(), + SLM_HISTORY_DATA_STREAM, + item + ); }, exception -> { - logger.error(new ParameterizedMessage("failed to index snapshot history item in data stream [{}]: [{}]", - SLM_HISTORY_DATA_STREAM, item), exception); + logger.error( + new ParameterizedMessage( + "failed to index snapshot history item in data stream [{}]: [{}]", + SLM_HISTORY_DATA_STREAM, + item + ), + exception + ); })); } catch (IOException exception) { - logger.error(new ParameterizedMessage("failed to index snapshot history item in data stream [{}]: [{}]", - SLM_HISTORY_DATA_STREAM, item), exception); + logger.error( + new ParameterizedMessage("failed to index snapshot history item in data stream [{}]: [{}]", SLM_HISTORY_DATA_STREAM, item), + exception + ); } } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/history/SnapshotLifecycleTemplateRegistry.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/history/SnapshotLifecycleTemplateRegistry.java index f0436b00208af..2e29a56295625 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/history/SnapshotLifecycleTemplateRegistry.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/slm/history/SnapshotLifecycleTemplateRegistry.java @@ -11,8 +11,8 @@ import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.ilm.IndexLifecycleMetadata; import org.elasticsearch.xpack.core.ilm.LifecyclePolicy; import org.elasticsearch.xpack.core.template.IndexTemplateConfig; @@ -66,8 +66,13 @@ protected boolean requiresMasterNode() { private final boolean slmHistoryEnabled; - public SnapshotLifecycleTemplateRegistry(Settings nodeSettings, ClusterService clusterService, ThreadPool threadPool, Client client, - NamedXContentRegistry xContentRegistry) { + public SnapshotLifecycleTemplateRegistry( + Settings nodeSettings, + ClusterService clusterService, + ThreadPool threadPool, + Client client, + NamedXContentRegistry xContentRegistry + ) { super(nodeSettings, clusterService, threadPool, client, xContentRegistry); slmHistoryEnabled = SLM_HISTORY_INDEX_ENABLED_SETTING.get(nodeSettings); } @@ -98,17 +103,12 @@ public boolean validate(ClusterState state) { .map(IndexTemplateConfig::getTemplateName) .allMatch(name -> state.metadata().templatesV2().containsKey(name)); - Optional> maybePolicies = Optional - .ofNullable(state.metadata().custom(IndexLifecycleMetadata.TYPE)) - .map(IndexLifecycleMetadata::getPolicies); - Set policyNames = getPolicyConfigs().stream() - .map(LifecyclePolicyConfig::getPolicyName) - .collect(Collectors.toSet()); - - boolean allPoliciesPresent = maybePolicies - .map(policies -> policies.keySet() - .containsAll(policyNames)) - .orElse(false); + Optional> maybePolicies = Optional.ofNullable( + state.metadata().custom(IndexLifecycleMetadata.TYPE) + ).map(IndexLifecycleMetadata::getPolicies); + Set policyNames = getPolicyConfigs().stream().map(LifecyclePolicyConfig::getPolicyName).collect(Collectors.toSet()); + + boolean allPoliciesPresent = maybePolicies.map(policies -> policies.keySet().containsAll(policyNames)).orElse(false); return allTemplatesPresent && allPoliciesPresent; } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/spatial/action/SpatialStatsAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/spatial/action/SpatialStatsAction.java index f0e1fe2b59789..4e51f35838cad 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/spatial/action/SpatialStatsAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/spatial/action/SpatialStatsAction.java @@ -16,9 +16,9 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.transport.TransportRequest; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.transport.TransportRequest; import org.elasticsearch.xpack.core.common.stats.EnumCounters; import java.io.IOException; @@ -107,8 +107,7 @@ protected void writeNodesTo(StreamOutput out, List nodes) throws I } public EnumCounters getStats() { - List> countersPerNode = getNodes() - .stream() + List> countersPerNode = getNodes().stream() .map(SpatialStatsAction.NodeResponse::getStats) .collect(Collectors.toList()); return EnumCounters.merge(Item.class, countersPerNode); @@ -173,8 +172,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; NodeResponse that = (NodeResponse) o; - return counters.equals(that.counters) && - getNode().equals(that.getNode()); + return counters.equals(that.counters) && getNode().equals(that.getNode()); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/CertParsingUtils.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/CertParsingUtils.java index 5dc7236b83d8d..04217165ea93d 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/CertParsingUtils.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/CertParsingUtils.java @@ -34,6 +34,7 @@ import java.util.List; import java.util.Map; import java.util.function.Function; + import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.X509ExtendedKeyManager; import javax.net.ssl.X509ExtendedTrustManager; @@ -51,17 +52,23 @@ private CertParsingUtils() { public static X509Certificate readX509Certificate(Path path) throws CertificateException, IOException { List certificates = PemUtils.readCertificates(List.of(path)); if (certificates.size() != 1) { - throw new IllegalArgumentException("expected a single certificate in file [" + path.toAbsolutePath() + "] but found [" + - certificates.size() + "]"); + throw new IllegalArgumentException( + "expected a single certificate in file [" + path.toAbsolutePath() + "] but found [" + certificates.size() + "]" + ); } final Certificate cert = certificates.get(0); if (cert instanceof X509Certificate) { return (X509Certificate) cert; } else { - throw new IllegalArgumentException("the certificate in " + path.toAbsolutePath() + " is not an X.509 certificate (" - + cert.getType() - + " : " - + cert.getClass() + ")"); + throw new IllegalArgumentException( + "the certificate in " + + path.toAbsolutePath() + + " is not an X.509 certificate (" + + cert.getType() + + " : " + + cert.getClass() + + ")" + ); } } @@ -89,9 +96,12 @@ public static Map readPkcs12KeyPairs(Path path, char[] passwor return readKeyPairsFromKeystore(path, "PKCS12", password, keyPassword); } - public static Map readKeyPairsFromKeystore(Path path, String storeType, char[] password, - Function keyPassword) - throws IOException, GeneralSecurityException { + public static Map readKeyPairsFromKeystore( + Path path, + String storeType, + char[] password, + Function keyPassword + ) throws IOException, GeneralSecurityException { final KeyStore store = KeyStoreUtil.readKeyStore(path, storeType, password); return readKeyPairsFromKeystore(store, keyPassword); } @@ -123,14 +133,18 @@ public static KeyStore getKeyStoreFromPEM(Path certificatePath, Path keyPath, ch /** * Creates a {@link X509ExtendedKeyManager} from a PEM encoded certificate and key file */ - public static X509ExtendedKeyManager getKeyManagerFromPEM(Path certificatePath, Path keyPath, char[] keyPassword) - throws IOException, GeneralSecurityException { + public static X509ExtendedKeyManager getKeyManagerFromPEM(Path certificatePath, Path keyPath, char[] keyPassword) throws IOException, + GeneralSecurityException { final KeyStore keyStore = getKeyStoreFromPEM(certificatePath, keyPath, keyPassword); return KeyStoreUtil.createKeyManager(keyStore, keyPassword, KeyManagerFactory.getDefaultAlgorithm()); } - public static SslKeyConfig createKeyConfig(Settings settings, String prefix, Environment environment, - boolean acceptNonSecurePasswords) { + public static SslKeyConfig createKeyConfig( + Settings settings, + String prefix, + Environment environment, + boolean acceptNonSecurePasswords + ) { final SslSettingsLoader settingsLoader = new SslSettingsLoader(settings, prefix, acceptNonSecurePasswords); return settingsLoader.buildKeyConfig(environment.configFile()); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/RestrictedTrustManager.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/RestrictedTrustManager.java index e0ca46229b054..f4cfff025681e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/RestrictedTrustManager.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/RestrictedTrustManager.java @@ -6,14 +6,11 @@ */ package org.elasticsearch.xpack.core.ssl; -import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.message.ParameterizedMessage; import org.elasticsearch.common.ssl.DerParser; -import javax.net.ssl.SSLEngine; -import javax.net.ssl.X509ExtendedTrustManager; - import java.io.IOException; import java.net.Socket; import java.security.cert.CertificateException; @@ -28,6 +25,9 @@ import java.util.function.Predicate; import java.util.stream.Collectors; +import javax.net.ssl.SSLEngine; +import javax.net.ssl.X509ExtendedTrustManager; + /** * An X509 trust manager that only trusts connections from a restricted set of predefined network entities (nodes, clients, etc). * The trusted entities are defined as a list of predicates on {@link CertificateTrustRestrictions} that are applied to the @@ -97,14 +97,29 @@ private void verifyTrust(X509Certificate[] chain) throws CertificateException { final X509Certificate certificate = chain[0]; Set names = readCommonNames(certificate); if (verifyCertificateNames(names)) { - logger.debug(() -> new ParameterizedMessage("Trusting certificate [{}] [{}] with common-names [{}]", - certificate.getSubjectDN(), certificate.getSerialNumber().toString(16), names)); + logger.debug( + () -> new ParameterizedMessage( + "Trusting certificate [{}] [{}] with common-names [{}]", + certificate.getSubjectDN(), + certificate.getSerialNumber().toString(16), + names + ) + ); } else { - logger.info("Rejecting certificate [{}] [{}] with common-names [{}]", - certificate.getSubjectDN(), certificate.getSerialNumber().toString(16), names); - throw new CertificateException("Certificate for " + certificate.getSubjectDN() + - " with common-names " + names - + " does not match the trusted names " + trustRestrictions.getTrustedNames()); + logger.info( + "Rejecting certificate [{}] [{}] with common-names [{}]", + certificate.getSubjectDN(), + certificate.getSerialNumber().toString(16), + names + ); + throw new CertificateException( + "Certificate for " + + certificate.getSubjectDN() + + " with common-names " + + names + + " does not match the trusted names " + + trustRestrictions.getTrustedNames() + ); } } @@ -121,11 +136,11 @@ private boolean verifyCertificateNames(Set names) { private Set readCommonNames(X509Certificate certificate) throws CertificateParsingException { return getSubjectAlternativeNames(certificate).stream() - .filter(pair -> ((Integer) pair.get(0)).intValue() == SAN_CODE_OTHERNAME) - .map(pair -> pair.get(1)) - .map(value -> decodeDerValue((byte[]) value, certificate)) - .filter(Objects::nonNull) - .collect(Collectors.toSet()); + .filter(pair -> ((Integer) pair.get(0)).intValue() == SAN_CODE_OTHERNAME) + .map(pair -> pair.get(1)) + .map(value -> decodeDerValue((byte[]) value, certificate)) + .filter(Objects::nonNull) + .collect(Collectors.toSet()); } /** @@ -161,13 +176,11 @@ private String decodeDerValue(byte[] value, X509Certificate certificate) { logger.trace("Read cn [{}] from ASN1Sequence [{}]", cn, seq); return cn; } else { - logger.debug("Certificate [{}] has 'otherName' [{}] with unsupported object-id [{}]", - certificate.getSubjectDN(), seq, id); + logger.debug("Certificate [{}] has 'otherName' [{}] with unsupported object-id [{}]", certificate.getSubjectDN(), seq, id); return null; } } catch (IOException e) { - logger.warn("Failed to read 'otherName' from certificate [{}]", - certificate.getSubjectDN()); + logger.warn("Failed to read 'otherName' from certificate [{}]", certificate.getSubjectDN()); return null; } } @@ -178,4 +191,3 @@ private Collection> getSubjectAlternativeNames(X509Certificate certifica return sans == null ? Collections.emptyList() : sans; } } - diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/SSLConfigurationReloader.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/SSLConfigurationReloader.java index e0cd7fa48ef02..ef3bd4b6242df 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/SSLConfigurationReloader.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/SSLConfigurationReloader.java @@ -15,8 +15,6 @@ import org.elasticsearch.watcher.ResourceWatcherService; import org.elasticsearch.watcher.ResourceWatcherService.Frequency; -import javax.net.ssl.SSLContext; - import java.io.IOException; import java.nio.file.Path; import java.security.AccessControlException; @@ -31,6 +29,8 @@ import java.util.concurrent.ExecutionException; import java.util.function.Consumer; +import javax.net.ssl.SSLContext; + /** * Ensures that the files backing an {@link SslConfiguration} are monitored for changes and the underlying key/trust material is reloaded * and the {@link SSLContext} has existing sessions invalidated to force the use of the new key/trust material @@ -41,15 +41,16 @@ public final class SSLConfigurationReloader { private final CompletableFuture sslServiceFuture = new CompletableFuture<>(); - public SSLConfigurationReloader(ResourceWatcherService resourceWatcherService, - Collection sslConfigurations) { + public SSLConfigurationReloader(ResourceWatcherService resourceWatcherService, Collection sslConfigurations) { startWatching(reloadConsumer(sslServiceFuture), resourceWatcherService, sslConfigurations); } // for testing - SSLConfigurationReloader(Consumer reloadConsumer, - ResourceWatcherService resourceWatcherService, - Collection sslConfigurations) { + SSLConfigurationReloader( + Consumer reloadConsumer, + ResourceWatcherService resourceWatcherService, + Collection sslConfigurations + ) { startWatching(reloadConsumer, resourceWatcherService, sslConfigurations); } @@ -78,8 +79,11 @@ private static Consumer reloadConsumer(CompletableFuture reloadConsumer, - ResourceWatcherService resourceWatcherService, Collection sslConfigurations) { + private static void startWatching( + Consumer reloadConsumer, + ResourceWatcherService resourceWatcherService, + Collection sslConfigurations + ) { Map> pathToConfigurationsMap = new HashMap<>(); for (SslConfiguration sslConfiguration : sslConfigurations) { final Collection filesToMonitor = sslConfiguration.getDependentFiles(); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/SSLConfigurationSettings.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/SSLConfigurationSettings.java index 1c4c9f97fd418..a540d42af9137 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/SSLConfigurationSettings.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/SSLConfigurationSettings.java @@ -25,6 +25,7 @@ import java.util.Optional; import java.util.function.Function; import java.util.stream.Collectors; + import javax.net.ssl.TrustManagerFactory; /** diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/SSLService.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/SSLService.java index b51b255d23f4d..9c7d34e03731b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/SSLService.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/SSLService.java @@ -34,19 +34,6 @@ import org.elasticsearch.xpack.core.ssl.cert.CertificateInfo; import org.elasticsearch.xpack.core.watcher.WatcherField; -import javax.net.ssl.HostnameVerifier; -import javax.net.ssl.SSLContext; -import javax.net.ssl.SSLEngine; -import javax.net.ssl.SSLException; -import javax.net.ssl.SSLParameters; -import javax.net.ssl.SSLPeerUnverifiedException; -import javax.net.ssl.SSLSession; -import javax.net.ssl.SSLSessionContext; -import javax.net.ssl.SSLSocket; -import javax.net.ssl.SSLSocketFactory; -import javax.net.ssl.X509ExtendedKeyManager; -import javax.net.ssl.X509ExtendedTrustManager; -import javax.security.auth.x500.X500Principal; import java.io.IOException; import java.net.InetAddress; import java.net.Socket; @@ -76,6 +63,20 @@ import java.util.function.Supplier; import java.util.stream.Collectors; +import javax.net.ssl.HostnameVerifier; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLEngine; +import javax.net.ssl.SSLException; +import javax.net.ssl.SSLParameters; +import javax.net.ssl.SSLPeerUnverifiedException; +import javax.net.ssl.SSLSession; +import javax.net.ssl.SSLSessionContext; +import javax.net.ssl.SSLSocket; +import javax.net.ssl.SSLSocketFactory; +import javax.net.ssl.X509ExtendedKeyManager; +import javax.net.ssl.X509ExtendedTrustManager; +import javax.security.auth.x500.X500Principal; + import static org.elasticsearch.xpack.core.XPackSettings.DEFAULT_SUPPORTED_PROTOCOLS; /** @@ -108,7 +109,10 @@ public class SSLService { } private static final Setting DIAGNOSE_TRUST_EXCEPTIONS_SETTING = Setting.boolSetting( - "xpack.security.ssl.diagnose.trust", true, Setting.Property.NodeScope); + "xpack.security.ssl.diagnose.trust", + true, + Setting.Property.NodeScope + ); private final Environment env; private final Settings settings; @@ -160,8 +164,11 @@ public SSLService(Settings settings, Environment environment) { this.sslContexts = loadSslConfigurations(this.sslConfigurations); } - private SSLService(Environment environment, Map sslConfigurations, - Map sslContexts) { + private SSLService( + Environment environment, + Map sslConfigurations, + Map sslContexts + ) { this.env = environment; this.settings = env.settings(); this.diagnoseTrustExceptions = DIAGNOSE_TRUST_EXCEPTIONS_SETTING.get(environment.settings()); @@ -275,9 +282,13 @@ protected void verifySession(HttpHost host, IOSession iosession, SSLSession sess final X509Certificate x509 = (X509Certificate) certs[0]; final X500Principal x500Principal = x509.getSubjectX500Principal(); final String altNames = Strings.collectionToCommaDelimitedString(SslDiagnostics.describeValidHostnames(x509)); - throw new SSLPeerUnverifiedException(LoggerMessageFormat.format("Expected SSL certificate to be valid for host [{}]," + - " but it is only valid for subject alternative names [{}] and subject [{}]", - new Object[]{host.getHostName(), altNames, x500Principal.toString()})); + throw new SSLPeerUnverifiedException( + LoggerMessageFormat.format( + "Expected SSL certificate to be valid for host [{}]," + + " but it is only valid for subject alternative names [{}] and subject [{}]", + new Object[] { host.getHostName(), altNames, x500Principal.toString() } + ) + ); } } }; @@ -296,7 +307,8 @@ public SSLSocketFactory sslSocketFactory(SslConfiguration configuration) { final SecuritySSLSocketFactory securitySSLSocketFactory = new SecuritySSLSocketFactory( () -> contextHolder.sslContext().getSocketFactory(), configuration.getSupportedProtocols().toArray(Strings.EMPTY_ARRAY), - supportedCiphers(socketFactory.getSupportedCipherSuites(), configuration.getCipherSuites(), false)); + supportedCiphers(socketFactory.getSupportedCipherSuites(), configuration.getCipherSuites(), false) + ); contextHolder.addReloadListener(securitySSLSocketFactory::reload); return securitySSLSocketFactory; } @@ -391,10 +403,9 @@ public SslConfiguration sslConfiguration(Settings settings) { } public Set getTransportProfileContextNames() { - return Collections.unmodifiableSet(this.sslConfigurations - .keySet().stream() - .filter(k -> k.startsWith("transport.profiles.")) - .collect(Collectors.toSet())); + return Collections.unmodifiableSet( + this.sslConfigurations.keySet().stream().filter(k -> k.startsWith("transport.profiles.")).collect(Collectors.toSet()) + ); } /** @@ -431,13 +442,18 @@ String[] supportedCiphers(String[] supportedCiphers, List requestedCiphe if (supportedCiphersList.isEmpty()) { throw new SslConfigException( - "none of the ciphers " + Arrays.toString(requestedCiphers.toArray()) + " are supported by this JVM"); + "none of the ciphers " + Arrays.toString(requestedCiphers.toArray()) + " are supported by this JVM" + ); } if (log && unsupportedCiphers.isEmpty() == false) { - logger.error("unsupported ciphers [{}] were requested but cannot be used in this JVM, however there are supported ciphers " + - "that will be used [{}]. If you are trying to use ciphers with a key length greater than 128 bits on an Oracle JVM, " + - "you will need to install the unlimited strength JCE policy files.", unsupportedCiphers, supportedCiphersList); + logger.error( + "unsupported ciphers [{}] were requested but cannot be used in this JVM, however there are supported ciphers " + + "that will be used [{}]. If you are trying to use ciphers with a key length greater than 128 bits on an Oracle JVM, " + + "you will need to install the unlimited strength JCE policy files.", + unsupportedCiphers, + supportedCiphersList + ); } return supportedCiphersList.toArray(new String[supportedCiphersList.size()]); @@ -466,13 +482,16 @@ private SSLContextHolder createSslContext(SslConfiguration sslConfiguration) { * @param trustManager the trust manager to use * @return the created SSLContext */ - private SSLContextHolder createSslContext(X509ExtendedKeyManager keyManager, X509ExtendedTrustManager trustManager, - SslConfiguration sslConfiguration) { + private SSLContextHolder createSslContext( + X509ExtendedKeyManager keyManager, + X509ExtendedTrustManager trustManager, + SslConfiguration sslConfiguration + ) { trustManager = wrapWithDiagnostics(trustManager, sslConfiguration); // Initialize sslContext try { SSLContext sslContext = SSLContext.getInstance(sslContextAlgorithm(sslConfiguration.getSupportedProtocols())); - sslContext.init(new X509ExtendedKeyManager[]{keyManager}, new X509ExtendedTrustManager[]{trustManager}, null); + sslContext.init(new X509ExtendedKeyManager[] { keyManager }, new X509ExtendedTrustManager[] { trustManager }, null); // check the supported ciphers and log them here to prevent spamming logs on every call supportedCiphers(sslContext.getSupportedSSLParameters().getCipherSuites(), sslConfiguration.getCipherSuites(), true); @@ -491,7 +510,8 @@ X509ExtendedTrustManager wrapWithDiagnostics(X509ExtendedTrustManager trustManag // but listing all of them would be confusing (e.g. some might be the default realms) // This needs to be a supplier (deferred evaluation) because we might load more configurations after this context is built. final Supplier contextName = () -> { - final List names = sslConfigurations.entrySet().stream() + final List names = sslConfigurations.entrySet() + .stream() .filter(e -> e.getValue().equals(configuration)) .limit(2) // we only need to distinguishing between 0/1/many .map(Entry::getKey) @@ -623,19 +643,25 @@ private void validateServerConfiguration(String prefix) { ); } } else if (settings.hasValue(enabledSetting) == false) { - final List sslSettingNames = settings.keySet().stream() + final List sslSettingNames = settings.keySet() + .stream() .filter(s -> s.startsWith(prefix)) .sorted() .collect(Collectors.toUnmodifiableList()); if (sslSettingNames.isEmpty() == false) { - throw new ElasticsearchSecurityException("invalid configuration for " + prefix + " - [" + enabledSetting + - "] is not set, but the following settings have been configured in elasticsearch.yml : [" + - Strings.collectionToCommaDelimitedString(sslSettingNames) + "]"); + throw new ElasticsearchSecurityException( + "invalid configuration for " + + prefix + + " - [" + + enabledSetting + + "] is not set, but the following settings have been configured in elasticsearch.yml : [" + + Strings.collectionToCommaDelimitedString(sslSettingNames) + + "]" + ); } } } - /** * Returns information about each certificate that is referenced by any SSL configuration. * This includes certificates used for identity (with a private key) and those used for trust, but excludes @@ -646,12 +672,13 @@ private void validateServerConfiguration(String prefix) { * @see SslTrustConfig#getConfiguredCertificates() */ public Collection getLoadedCertificates() throws GeneralSecurityException, IOException { - return this.getLoadedSslConfigurations().stream() + return this.getLoadedSslConfigurations() + .stream() .map(SslConfiguration::getConfiguredCertificates) .flatMap(Collection::stream) - .map(cert -> new CertificateInfo( - cert.getPath(), cert.getFormat(), cert.getAlias(), cert.hasPrivateKey(), cert.getCertificate() - )) + .map( + cert -> new CertificateInfo(cert.getPath(), cert.getFormat(), cert.getAlias(), cert.hasPrivateKey(), cert.getCertificate()) + ) .collect(Sets.toUnmodifiableSortedSet()); } @@ -744,7 +771,6 @@ private static SSLSocket createWithPermissions(CheckedSupplier getRealmsSSLSettings(Settings settings) { final String prefix = "xpack.security.authc.realms."; final Map settingsByRealmType = settings.getGroups(prefix); settingsByRealmType.forEach((realmType, typeSettings) -> { - final Optional nonDottedSetting = typeSettings.keySet().stream().filter(k -> k.indexOf('.') == -1).findAny(); - if (nonDottedSetting.isPresent()) { - logger.warn("Skipping any SSL configuration from realm [{}{}] because the key [{}] is not in the correct format", - prefix, realmType, nonDottedSetting.get()); - } else { - typeSettings.getAsGroups().forEach((realmName, realmSettings) -> { - Settings realmSSLSettings = realmSettings.getByPrefix("ssl."); - // Put this even if empty, so that the name will be mapped to the global SSL configuration - sslSettings.put(prefix + realmType + "." + realmName + ".ssl", realmSSLSettings); - }); - } + final Optional nonDottedSetting = typeSettings.keySet().stream().filter(k -> k.indexOf('.') == -1).findAny(); + if (nonDottedSetting.isPresent()) { + logger.warn( + "Skipping any SSL configuration from realm [{}{}] because the key [{}] is not in the correct format", + prefix, + realmType, + nonDottedSetting.get() + ); + } else { + typeSettings.getAsGroups().forEach((realmName, realmSettings) -> { + Settings realmSSLSettings = realmSettings.getByPrefix("ssl."); + // Put this even if empty, so that the name will be mapped to the global SSL configuration + sslSettings.put(prefix + realmType + "." + realmName + ".ssl", realmSSLSettings); + }); } - ); + }); return sslSettings; } @@ -882,8 +914,11 @@ public SslConfiguration getSSLConfiguration(String contextName) { } final SslConfiguration configuration = sslConfigurations.get(contextName); if (configuration == null) { - logger.warn("Cannot find SSL configuration for context {}. Known contexts are: {}", contextName, - Strings.collectionToCommaDelimitedString(sslConfigurations.keySet())); + logger.warn( + "Cannot find SSL configuration for context {}. Known contexts are: {}", + contextName, + Strings.collectionToCommaDelimitedString(sslConfigurations.keySet()) + ); } else { logger.debug("SSL configuration [{}] is [{}]", contextName, configuration); } @@ -905,7 +940,8 @@ private static String sslContextAlgorithm(List supportedProtocols) { return entry.getValue(); } } - throw new IllegalArgumentException("no supported SSL/TLS protocol was found in the configured supported protocols: " - + supportedProtocols); + throw new IllegalArgumentException( + "no supported SSL/TLS protocol was found in the configured supported protocols: " + supportedProtocols + ); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/SslSettingsLoader.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/SslSettingsLoader.java index f9deae94b0367..e46abca986cf5 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/SslSettingsLoader.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/SslSettingsLoader.java @@ -85,8 +85,13 @@ private void checkSetting(String key) { // This triggers deprecation warnings setting.get(settings); } else if (disabledSettings.containsKey(key) == false) { - throw new SslConfigException("The setting [" + key + "] is not supported, valid SSL settings are: [" - + Strings.collectionToCommaDelimitedString(standardSettings.keySet()) + "]"); + throw new SslConfigException( + "The setting [" + + key + + "] is not supported, valid SSL settings are: [" + + Strings.collectionToCommaDelimitedString(standardSettings.keySet()) + + "]" + ); } } @@ -94,8 +99,13 @@ private void checkSetting(String key) { protected char[] getSecureSetting(String key) { final Setting setting = secureSettings.get(key); if (setting == null) { - throw new SslConfigException("The secure setting [" + key + "] is not supported, valid secure SSL settings are: [" - + Strings.collectionToCommaDelimitedString(secureSettings.keySet()) + "]"); + throw new SslConfigException( + "The secure setting [" + + key + + "] is not supported, valid secure SSL settings are: [" + + Strings.collectionToCommaDelimitedString(secureSettings.keySet()) + + "]" + ); } return setting.exists(settings) ? setting.get(settings).getChars() : null; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/TransportTLSBootstrapCheck.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/TransportTLSBootstrapCheck.java index 1d482cf140178..5899736481884 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/TransportTLSBootstrapCheck.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/TransportTLSBootstrapCheck.java @@ -16,7 +16,7 @@ public final class TransportTLSBootstrapCheck implements BootstrapCheck { @Override public BootstrapCheckResult check(BootstrapContext context) { - assert XPackSettings.SECURITY_ENABLED.get(context.settings()) + assert XPackSettings.SECURITY_ENABLED.get(context.settings()) : "Bootstrap check should not be installed unless security is enabled"; if (XPackSettings.TRANSPORT_SSL_ENABLED.get(context.settings()) == false) { return BootstrapCheckResult.failure( diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/X509KeyPairSettings.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/X509KeyPairSettings.java index 9f9ab4eb20ea2..b78b97896d038 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/X509KeyPairSettings.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/X509KeyPairSettings.java @@ -12,7 +12,6 @@ import org.elasticsearch.common.ssl.SslConfigurationKeys; import org.elasticsearch.common.util.CollectionUtils; -import javax.net.ssl.KeyManagerFactory; import java.security.KeyStore; import java.util.Collection; import java.util.List; @@ -20,6 +19,8 @@ import java.util.function.Function; import java.util.stream.Collectors; +import javax.net.ssl.KeyManagerFactory; + /** * An encapsulation of the configuration options for X.509 Key Pair support in X-Pack security. * The most common use is as the private key and associated certificate for SSL/TLS support, but it can also be used for providing @@ -28,43 +29,90 @@ */ public class X509KeyPairSettings { - static final Function>> KEYSTORE_PATH_TEMPLATE = key -> new Setting<>(key, s -> null, - Optional::ofNullable, Setting.Property.NodeScope, Setting.Property.Filtered); - - static final Function> LEGACY_KEYSTORE_PASSWORD_TEMPLATE = key -> new Setting<>(key, "", - SecureString::new, Setting.Property.Deprecated, Setting.Property.Filtered, Setting.Property.NodeScope); - static final Function> KEYSTORE_PASSWORD_TEMPLATE = key -> SecureSetting.secureString(key, - LEGACY_KEYSTORE_PASSWORD_TEMPLATE.apply( - key.replace(SslConfigurationKeys.KEYSTORE_SECURE_PASSWORD, SslConfigurationKeys.KEYSTORE_LEGACY_PASSWORD) - )); - - static final Function> KEY_STORE_ALGORITHM_TEMPLATE = key -> - new Setting<>(key, s -> KeyManagerFactory.getDefaultAlgorithm(), - Function.identity(), Setting.Property.NodeScope, Setting.Property.Filtered); - - static final Function>> KEY_STORE_TYPE_TEMPLATE = key -> - new Setting<>(key, s -> null, Optional::ofNullable, Setting.Property.NodeScope, Setting.Property.Filtered); - - static final Function> LEGACY_KEYSTORE_KEY_PASSWORD_TEMPLATE = key -> new Setting<>(key, "", - SecureString::new, Setting.Property.Deprecated, Setting.Property.Filtered, Setting.Property.NodeScope); - static final Function> KEYSTORE_KEY_PASSWORD_TEMPLATE = key -> - SecureSetting.secureString(key, LEGACY_KEYSTORE_KEY_PASSWORD_TEMPLATE.apply( - key.replace(SslConfigurationKeys.KEYSTORE_SECURE_KEY_PASSWORD, SslConfigurationKeys.KEYSTORE_LEGACY_KEY_PASSWORD) - )); - - static final Function>> KEY_PATH_TEMPLATE = key -> new Setting<>(key, s -> null, - Optional::ofNullable, Setting.Property.NodeScope, Setting.Property.Filtered); - - static final Function>> CERT_TEMPLATE = key -> new Setting<>(key, s -> null, - Optional::ofNullable, Setting.Property.NodeScope, Setting.Property.Filtered); - - static final Function> LEGACY_KEY_PASSWORD_TEMPLATE = key -> new Setting<>(key, "", - SecureString::new, Setting.Property.Deprecated, Setting.Property.Filtered, Setting.Property.NodeScope); - static final Function> KEY_PASSWORD_TEMPLATE = key -> - SecureSetting.secureString(key, LEGACY_KEY_PASSWORD_TEMPLATE.apply( - key.replace(SslConfigurationKeys.KEY_SECURE_PASSPHRASE, SslConfigurationKeys.KEY_LEGACY_PASSPHRASE) - )); - + static final Function>> KEYSTORE_PATH_TEMPLATE = key -> new Setting<>( + key, + s -> null, + Optional::ofNullable, + Setting.Property.NodeScope, + Setting.Property.Filtered + ); + + static final Function> LEGACY_KEYSTORE_PASSWORD_TEMPLATE = key -> new Setting<>( + key, + "", + SecureString::new, + Setting.Property.Deprecated, + Setting.Property.Filtered, + Setting.Property.NodeScope + ); + static final Function> KEYSTORE_PASSWORD_TEMPLATE = key -> SecureSetting.secureString( + key, + LEGACY_KEYSTORE_PASSWORD_TEMPLATE.apply( + key.replace(SslConfigurationKeys.KEYSTORE_SECURE_PASSWORD, SslConfigurationKeys.KEYSTORE_LEGACY_PASSWORD) + ) + ); + + static final Function> KEY_STORE_ALGORITHM_TEMPLATE = key -> new Setting<>( + key, + s -> KeyManagerFactory.getDefaultAlgorithm(), + Function.identity(), + Setting.Property.NodeScope, + Setting.Property.Filtered + ); + + static final Function>> KEY_STORE_TYPE_TEMPLATE = key -> new Setting<>( + key, + s -> null, + Optional::ofNullable, + Setting.Property.NodeScope, + Setting.Property.Filtered + ); + + static final Function> LEGACY_KEYSTORE_KEY_PASSWORD_TEMPLATE = key -> new Setting<>( + key, + "", + SecureString::new, + Setting.Property.Deprecated, + Setting.Property.Filtered, + Setting.Property.NodeScope + ); + static final Function> KEYSTORE_KEY_PASSWORD_TEMPLATE = key -> SecureSetting.secureString( + key, + LEGACY_KEYSTORE_KEY_PASSWORD_TEMPLATE.apply( + key.replace(SslConfigurationKeys.KEYSTORE_SECURE_KEY_PASSWORD, SslConfigurationKeys.KEYSTORE_LEGACY_KEY_PASSWORD) + ) + ); + + static final Function>> KEY_PATH_TEMPLATE = key -> new Setting<>( + key, + s -> null, + Optional::ofNullable, + Setting.Property.NodeScope, + Setting.Property.Filtered + ); + + static final Function>> CERT_TEMPLATE = key -> new Setting<>( + key, + s -> null, + Optional::ofNullable, + Setting.Property.NodeScope, + Setting.Property.Filtered + ); + + static final Function> LEGACY_KEY_PASSWORD_TEMPLATE = key -> new Setting<>( + key, + "", + SecureString::new, + Setting.Property.Deprecated, + Setting.Property.Filtered, + Setting.Property.NodeScope + ); + static final Function> KEY_PASSWORD_TEMPLATE = key -> SecureSetting.secureString( + key, + LEGACY_KEY_PASSWORD_TEMPLATE.apply( + key.replace(SslConfigurationKeys.KEY_SECURE_PASSPHRASE, SslConfigurationKeys.KEY_LEGACY_PASSPHRASE) + ) + ); // Specify private cert/key pair via keystore final Setting> keystorePath; @@ -107,10 +155,17 @@ private X509KeyPairSettings(boolean acceptNonSecurePasswords, SettingFactory fac legacyKeyPassword = factory.apply(SslConfigurationKeys.KEY_LEGACY_PASSPHRASE, LEGACY_KEY_PASSWORD_TEMPLATE); final List> enabled = CollectionUtils.arrayAsArrayList( - keystorePath, keystorePassword, keystoreAlgorithm, keystoreType, keystoreKeyPassword, - keyPath, keyPassword, certificatePath); - - final List> legacySettings = List.of(legacyKeystorePassword,legacyKeystoreKeyPassword,legacyKeyPassword); + keystorePath, + keystorePassword, + keystoreAlgorithm, + keystoreType, + keystoreKeyPassword, + keyPath, + keyPassword, + certificatePath + ); + + final List> legacySettings = List.of(legacyKeystorePassword, legacyKeystoreKeyPassword, legacyKeyPassword); if (acceptNonSecurePasswords) { enabled.addAll(legacySettings); disabledSettings = List.of(); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/action/GetCertificateInfoAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/action/GetCertificateInfoAction.java index b458b6505f291..f759e360d9e78 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/action/GetCertificateInfoAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/action/GetCertificateInfoAction.java @@ -84,7 +84,7 @@ public void writeTo(StreamOutput out) throws IOException { } } - } + } public static class RequestBuilder extends ActionRequestBuilder { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/action/TransportGetCertificateInfoAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/action/TransportGetCertificateInfoAction.java index a1b5ac17b2b54..277efaae357e5 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/action/TransportGetCertificateInfoAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/action/TransportGetCertificateInfoAction.java @@ -19,8 +19,9 @@ import java.security.GeneralSecurityException; import java.util.Collection; -public class TransportGetCertificateInfoAction extends HandledTransportAction { +public class TransportGetCertificateInfoAction extends HandledTransportAction< + GetCertificateInfoAction.Request, + GetCertificateInfoAction.Response> { private final SSLService sslService; @@ -31,8 +32,11 @@ public TransportGetCertificateInfoAction(TransportService transportService, Acti } @Override - protected void doExecute(Task task, GetCertificateInfoAction.Request request, - ActionListener listener) { + protected void doExecute( + Task task, + GetCertificateInfoAction.Request request, + ActionListener listener + ) { try { Collection certificates = sslService.getLoadedCertificates(); listener.onResponse(new GetCertificateInfoAction.Response(certificates)); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/cert/CertificateInfo.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/cert/CertificateInfo.java index 3fe4f083f66b3..e217903dec107 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/cert/CertificateInfo.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/cert/CertificateInfo.java @@ -11,9 +11,9 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.core.Nullable; import java.io.IOException; import java.security.cert.X509Certificate; @@ -28,10 +28,10 @@ */ public class CertificateInfo implements ToXContentObject, Writeable, Comparable { - private static final Comparator COMPARATOR = - Comparator.comparing(CertificateInfo::path, Comparator.nullsLast(Comparator.naturalOrder())) - .thenComparing(CertificateInfo::alias, Comparator.nullsLast(Comparator.naturalOrder())) - .thenComparing(CertificateInfo::serialNumber); + private static final Comparator COMPARATOR = Comparator.comparing( + CertificateInfo::path, + Comparator.nullsLast(Comparator.naturalOrder()) + ).thenComparing(CertificateInfo::alias, Comparator.nullsLast(Comparator.naturalOrder())).thenComparing(CertificateInfo::serialNumber); private final String path; private final String format; @@ -113,14 +113,14 @@ public boolean hasPrivateKey() { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { return builder.startObject() - .field("path", path) - .field("format", format) - .field("alias", alias) - .field("subject_dn", subjectDn) - .field("serial_number", serialNumber) - .field("has_private_key", hasPrivateKey) - .timeField("expiry", expiry) - .endObject(); + .field("path", path) + .field("format", format) + .field("alias", alias) + .field("subject_dn", subjectDn) + .field("serial_number", serialNumber) + .field("has_private_key", hasPrivateKey) + .timeField("expiry", expiry) + .endObject(); } @Override @@ -138,13 +138,13 @@ public boolean equals(Object other) { } final CertificateInfo that = (CertificateInfo) other; - return Objects.equals(this.path, that.path) - && this.format.equals(that.format) - && this.hasPrivateKey == that.hasPrivateKey - && Objects.equals(this.alias, that.alias) - && Objects.equals(this.serialNumber, that.serialNumber) - && Objects.equals(this.subjectDn, that.subjectDn) - && Objects.equals(this.expiry, that.expiry); + return Objects.equals(this.path, that.path) + && this.format.equals(that.format) + && this.hasPrivateKey == that.hasPrivateKey + && Objects.equals(this.alias, that.alias) + && Objects.equals(this.serialNumber, that.serialNumber) + && Objects.equals(this.subjectDn, that.subjectDn) + && Objects.equals(this.expiry, that.expiry); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/rest/RestGetCertificateInfoAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/rest/RestGetCertificateInfoAction.java index 742f101322b56..7a775d8aaf302 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/rest/RestGetCertificateInfoAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/rest/RestGetCertificateInfoAction.java @@ -7,7 +7,6 @@ package org.elasticsearch.xpack.core.ssl.rest; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.BytesRestResponse; @@ -15,6 +14,7 @@ import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ssl.action.GetCertificateInfoAction; import org.elasticsearch.xpack.core.ssl.action.GetCertificateInfoAction.Response; @@ -30,10 +30,7 @@ public class RestGetCertificateInfoAction extends BaseRestHandler { @Override public List routes() { - return List.of( - Route.builder(GET, "/_ssl/certificates") - .replaces(GET, "/_xpack/ssl/certificates", RestApiVersion.V_7).build() - ); + return List.of(Route.builder(GET, "/_ssl/certificates").replaces(GET, "/_xpack/ssl/certificates", RestApiVersion.V_7).build()); } @Override @@ -43,12 +40,13 @@ public String getName() { @Override protected final RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) { - return channel -> new GetCertificateInfoAction.RequestBuilder(client, GetCertificateInfoAction.INSTANCE) - .execute(new RestBuilderListener(channel) { - @Override - public RestResponse buildResponse(Response response, XContentBuilder builder) throws Exception { - return new BytesRestResponse(RestStatus.OK, response.toXContent(builder, request)); - } - }); + return channel -> new GetCertificateInfoAction.RequestBuilder(client, GetCertificateInfoAction.INSTANCE).execute( + new RestBuilderListener(channel) { + @Override + public RestResponse buildResponse(Response response, XContentBuilder builder) throws Exception { + return new BytesRestResponse(RestStatus.OK, response.toXContent(builder, request)); + } + } + ); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/template/IndexTemplateConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/template/IndexTemplateConfig.java index bc04c2bfe8418..f2ba72b259df9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/template/IndexTemplateConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/template/IndexTemplateConfig.java @@ -63,8 +63,7 @@ public IndexTemplateConfig(String templateName, String fileName, int version, St * @param variables A map of additional variable substitutions. The map's keys are the variable names. * The corresponding values will replace the variable names. */ - public IndexTemplateConfig(String templateName, String fileName, int version, String versionProperty, Map variables) - { + public IndexTemplateConfig(String templateName, String fileName, int version, String versionProperty, Map variables) { this.templateName = templateName; this.fileName = fileName; this.version = version; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/template/IndexTemplateRegistry.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/template/IndexTemplateRegistry.java index 096ab52bff322..b4319320afe32 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/template/IndexTemplateRegistry.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/template/IndexTemplateRegistry.java @@ -27,12 +27,12 @@ import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.gateway.GatewayService; +import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.gateway.GatewayService; -import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.xpack.core.ilm.IndexLifecycleMetadata; import org.elasticsearch.xpack.core.ilm.LifecyclePolicy; import org.elasticsearch.xpack.core.ilm.action.PutLifecycleAction; @@ -65,8 +65,13 @@ public abstract class IndexTemplateRegistry implements ClusterStateListener { protected final ConcurrentMap templateCreationsInProgress = new ConcurrentHashMap<>(); protected final ConcurrentMap policyCreationsInProgress = new ConcurrentHashMap<>(); - public IndexTemplateRegistry(Settings nodeSettings, ClusterService clusterService, ThreadPool threadPool, Client client, - NamedXContentRegistry xContentRegistry) { + public IndexTemplateRegistry( + Settings nodeSettings, + ClusterService clusterService, + ThreadPool threadPool, + Client client, + NamedXContentRegistry xContentRegistry + ) { this.settings = nodeSettings; this.client = client; this.threadPool = threadPool; @@ -131,8 +136,15 @@ protected List getPolicyConfigs() { * @param e The exception that caused the failure. */ protected void onPutTemplateFailure(IndexTemplateConfig config, Exception e) { - logger.error(new ParameterizedMessage("error adding index template [{}] from [{}] for [{}]", - config.getTemplateName(), config.getFileName(), getOrigin()), e); + logger.error( + new ParameterizedMessage( + "error adding index template [{}] from [{}] for [{}]", + config.getTemplateName(), + config.getFileName(), + getOrigin() + ), + e + ); } /** @@ -141,8 +153,7 @@ protected void onPutTemplateFailure(IndexTemplateConfig config, Exception e) { * @param e The exception that caused the failure. */ protected void onPutPolicyFailure(LifecyclePolicy policy, Exception e) { - logger.error(new ParameterizedMessage("error adding lifecycle policy [{}] for [{}]", - policy.getName(), getOrigin()), e); + logger.error(new ParameterizedMessage("error adding lifecycle policy [{}] for [{}]", policy.getName(), getOrigin()), e); } @Override @@ -205,17 +216,29 @@ private void addLegacyTemplatesIfMissing(ClusterState state) { } else if (Objects.isNull(currentTemplate.getVersion()) || newTemplate.getVersion() > currentTemplate.getVersion()) { // IndexTemplateConfig now enforces templates contain a `version` property, so if the template doesn't have one we can // safely assume it's an old version of the template. - logger.info("upgrading legacy template [{}] for [{}] from version [{}] to version [{}]", - templateName, getOrigin(), currentTemplate.getVersion(), newTemplate.getVersion()); + logger.info( + "upgrading legacy template [{}] for [{}] from version [{}] to version [{}]", + templateName, + getOrigin(), + currentTemplate.getVersion(), + newTemplate.getVersion() + ); putLegacyTemplate(newTemplate, creationCheck); } else { creationCheck.set(false); - logger.trace("not adding legacy template [{}] for [{}], because it already exists at version [{}]", - templateName, getOrigin(), currentTemplate.getVersion()); + logger.trace( + "not adding legacy template [{}] for [{}], because it already exists at version [{}]", + templateName, + getOrigin(), + currentTemplate.getVersion() + ); } } else { - logger.trace("skipping the creation of legacy template [{}] for [{}], because its creation is in progress", - templateName, getOrigin()); + logger.trace( + "skipping the creation of legacy template [{}] for [{}], because its creation is in progress", + templateName, + getOrigin() + ); } } } @@ -233,17 +256,29 @@ private void addComponentTemplatesIfMissing(ClusterState state) { } else if (Objects.isNull(currentTemplate.version()) || newTemplate.getVersion() > currentTemplate.version()) { // IndexTemplateConfig now enforces templates contain a `version` property, so if the template doesn't have one we can // safely assume it's an old version of the template. - logger.info("upgrading component template [{}] for [{}] from version [{}] to version [{}]", - templateName, getOrigin(), currentTemplate.version(), newTemplate.getVersion()); + logger.info( + "upgrading component template [{}] for [{}] from version [{}] to version [{}]", + templateName, + getOrigin(), + currentTemplate.version(), + newTemplate.getVersion() + ); putComponentTemplate(newTemplate, creationCheck); } else { creationCheck.set(false); - logger.trace("not adding component template [{}] for [{}], because it already exists at version [{}]", - templateName, getOrigin(), currentTemplate.version()); + logger.trace( + "not adding component template [{}] for [{}], because it already exists at version [{}]", + templateName, + getOrigin(), + currentTemplate.version() + ); } } else { - logger.trace("skipping the creation of component template [{}] for [{}], because its creation is in progress", - templateName, getOrigin()); + logger.trace( + "skipping the creation of component template [{}] for [{}], because its creation is in progress", + templateName, + getOrigin() + ); } } } @@ -258,25 +293,40 @@ private void addComposableTemplatesIfMissing(ClusterState state) { boolean componentTemplatesAvailable = componentTemplatesExist(state, newTemplate); if (componentTemplatesAvailable == false) { creationCheck.set(false); - logger.trace("not adding composable template [{}] for [{}] because its required component templates do not exist", - templateName, getOrigin()); + logger.trace( + "not adding composable template [{}] for [{}] because its required component templates do not exist", + templateName, + getOrigin() + ); } else if (Objects.isNull(currentTemplate)) { logger.debug("adding composable template [{}] for [{}], because it doesn't exist", templateName, getOrigin()); putComposableTemplate(newTemplate, creationCheck); } else if (Objects.isNull(currentTemplate.version()) || newTemplate.getVersion() > currentTemplate.version()) { // IndexTemplateConfig now enforces templates contain a `version` property, so if the template doesn't have one we can // safely assume it's an old version of the template. - logger.info("upgrading composable template [{}] for [{}] from version [{}] to version [{}]", - templateName, getOrigin(), currentTemplate.version(), newTemplate.getVersion()); + logger.info( + "upgrading composable template [{}] for [{}] from version [{}] to version [{}]", + templateName, + getOrigin(), + currentTemplate.version(), + newTemplate.getVersion() + ); putComposableTemplate(newTemplate, creationCheck); } else { creationCheck.set(false); - logger.trace("not adding composable template [{}] for [{}], because it already exists at version [{}]", - templateName, getOrigin(), currentTemplate.version()); + logger.trace( + "not adding composable template [{}] for [{}], because it already exists at version [{}]", + templateName, + getOrigin(), + currentTemplate.version() + ); } } else { - logger.trace("skipping the creation of composable template [{}] for [{}], because its creation is in progress", - templateName, getOrigin()); + logger.trace( + "skipping the creation of composable template [{}] for [{}], because its creation is in progress", + templateName, + getOrigin() + ); } } } @@ -287,8 +337,13 @@ private void addComposableTemplatesIfMissing(ClusterState state) { private static boolean componentTemplatesExist(ClusterState state, IndexTemplateConfig composableTemplate) { final ComposableIndexTemplate indexTemplate; try { - indexTemplate = ComposableIndexTemplate.parse(JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, composableTemplate.loadBytes())); + indexTemplate = ComposableIndexTemplate.parse( + JsonXContent.jsonXContent.createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + composableTemplate.loadBytes() + ) + ); } catch (Exception e) { throw new ElasticsearchParseException("unable to parse composable template " + composableTemplate.getTemplateName(), e); } @@ -303,14 +358,20 @@ private void putLegacyTemplate(final IndexTemplateConfig config, final AtomicBoo PutIndexTemplateRequest request = new PutIndexTemplateRequest(templateName).source(config.loadBytes(), XContentType.JSON); request.masterNodeTimeout(TimeValue.timeValueMinutes(1)); - executeAsyncWithOrigin(client.threadPool().getThreadContext(), getOrigin(), request, + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + getOrigin(), + request, new ActionListener() { @Override public void onResponse(AcknowledgedResponse response) { creationCheck.set(false); if (response.isAcknowledged() == false) { - logger.error("error adding legacy template [{}] for [{}], request was not acknowledged", - templateName, getOrigin()); + logger.error( + "error adding legacy template [{}] for [{}], request was not acknowledged", + templateName, + getOrigin() + ); } } @@ -319,7 +380,9 @@ public void onFailure(Exception e) { creationCheck.set(false); onPutTemplateFailure(config, e); } - }, client.admin().indices()::putTemplate); + }, + client.admin().indices()::putTemplate + ); }); } @@ -330,20 +393,33 @@ private void putComponentTemplate(final IndexTemplateConfig config, final Atomic PutComponentTemplateAction.Request request = new PutComponentTemplateAction.Request(templateName); try { - request.componentTemplate(ComponentTemplate.parse(JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, config.loadBytes()))); + request.componentTemplate( + ComponentTemplate.parse( + JsonXContent.jsonXContent.createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + config.loadBytes() + ) + ) + ); } catch (Exception e) { throw new ElasticsearchParseException("unable to parse component template " + config.getTemplateName(), e); } request.masterNodeTimeout(TimeValue.timeValueMinutes(1)); - executeAsyncWithOrigin(client.threadPool().getThreadContext(), getOrigin(), request, + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + getOrigin(), + request, new ActionListener() { @Override public void onResponse(AcknowledgedResponse response) { creationCheck.set(false); if (response.isAcknowledged() == false) { - logger.error("error adding component template [{}] for [{}], request was not acknowledged", - templateName, getOrigin()); + logger.error( + "error adding component template [{}] for [{}], request was not acknowledged", + templateName, + getOrigin() + ); } } @@ -352,7 +428,9 @@ public void onFailure(Exception e) { creationCheck.set(false); onPutTemplateFailure(config, e); } - }, (req, listener) -> client.execute(PutComponentTemplateAction.INSTANCE, req, listener)); + }, + (req, listener) -> client.execute(PutComponentTemplateAction.INSTANCE, req, listener) + ); }); } @@ -363,20 +441,33 @@ private void putComposableTemplate(final IndexTemplateConfig config, final Atomi PutComposableIndexTemplateAction.Request request = new PutComposableIndexTemplateAction.Request(templateName); try { - request.indexTemplate(ComposableIndexTemplate.parse(JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, config.loadBytes()))); + request.indexTemplate( + ComposableIndexTemplate.parse( + JsonXContent.jsonXContent.createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + config.loadBytes() + ) + ) + ); } catch (Exception e) { throw new ElasticsearchParseException("unable to parse composable template " + config.getTemplateName(), e); } request.masterNodeTimeout(TimeValue.timeValueMinutes(1)); - executeAsyncWithOrigin(client.threadPool().getThreadContext(), getOrigin(), request, + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + getOrigin(), + request, new ActionListener() { @Override public void onResponse(AcknowledgedResponse response) { creationCheck.set(false); if (response.isAcknowledged() == false) { - logger.error("error adding composable template [{}] for [{}], request was not acknowledged", - templateName, getOrigin()); + logger.error( + "error adding composable template [{}] for [{}], request was not acknowledged", + templateName, + getOrigin() + ); } } @@ -385,7 +476,9 @@ public void onFailure(Exception e) { creationCheck.set(false); onPutTemplateFailure(config, e); } - }, (req, listener) -> client.execute(PutComposableIndexTemplateAction.INSTANCE, req, listener)); + }, + (req, listener) -> client.execute(PutComposableIndexTemplateAction.INSTANCE, req, listener) + ); }); } @@ -397,18 +490,19 @@ private void addIndexLifecyclePoliciesIfMissing(ClusterState state) { .collect(Collectors.toList()); for (LifecyclePolicy policy : policies) { - final AtomicBoolean creationCheck = policyCreationsInProgress.computeIfAbsent(policy.getName(), - key -> new AtomicBoolean(false)); + final AtomicBoolean creationCheck = policyCreationsInProgress.computeIfAbsent( + policy.getName(), + key -> new AtomicBoolean(false) + ); if (creationCheck.compareAndSet(false, true)) { - final boolean policyNeedsToBeCreated = maybeMeta - .flatMap(ilmMeta -> Optional.ofNullable(ilmMeta.getPolicies().get(policy.getName()))) - .isPresent() == false; + final boolean policyNeedsToBeCreated = maybeMeta.flatMap( + ilmMeta -> Optional.ofNullable(ilmMeta.getPolicies().get(policy.getName())) + ).isPresent() == false; if (policyNeedsToBeCreated) { logger.debug("adding lifecycle policy [{}] for [{}], because it doesn't exist", policy.getName(), getOrigin()); putPolicy(policy, creationCheck); } else { - logger.trace("not adding lifecycle policy [{}] for [{}], because it already exists", - policy.getName(), getOrigin()); + logger.trace("not adding lifecycle policy [{}] for [{}], because it already exists", policy.getName(), getOrigin()); creationCheck.set(false); } } @@ -420,14 +514,20 @@ private void putPolicy(final LifecyclePolicy policy, final AtomicBoolean creatio executor.execute(() -> { PutLifecycleAction.Request request = new PutLifecycleAction.Request(policy); request.masterNodeTimeout(TimeValue.timeValueMinutes(1)); - executeAsyncWithOrigin(client.threadPool().getThreadContext(), getOrigin(), request, + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + getOrigin(), + request, new ActionListener() { @Override public void onResponse(AcknowledgedResponse response) { creationCheck.set(false); if (response.isAcknowledged() == false) { - logger.error("error adding lifecycle policy [{}] for [{}], request was not acknowledged", - policy.getName(), getOrigin()); + logger.error( + "error adding lifecycle policy [{}] for [{}], request was not acknowledged", + policy.getName(), + getOrigin() + ); } } @@ -436,7 +536,9 @@ public void onFailure(Exception e) { creationCheck.set(false); onPutPolicyFailure(policy, e); } - }, (req, listener) -> client.execute(PutLifecycleAction.INSTANCE, req, listener)); + }, + (req, listener) -> client.execute(PutLifecycleAction.INSTANCE, req, listener) + ); }); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/template/TemplateUtils.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/template/TemplateUtils.java index eeb7c323f102c..205b1fd53a09b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/template/TemplateUtils.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/template/TemplateUtils.java @@ -17,9 +17,9 @@ import org.elasticsearch.common.compress.CompressedXContent; import org.elasticsearch.common.io.Streams; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.json.JsonXContent; @@ -42,11 +42,19 @@ private TemplateUtils() {} /** * Loads a JSON template as a resource and puts it into the provided map */ - public static void loadLegacyTemplateIntoMap(String resource, Map map, String templateName, - String version, String versionProperty, Logger logger) { + public static void loadLegacyTemplateIntoMap( + String resource, + Map map, + String templateName, + String version, + String versionProperty, + Logger logger + ) { final String template = loadTemplate(resource, version, versionProperty); - try (XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, template)) { + try ( + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, template) + ) { map.put(templateName, IndexTemplateMetadata.Builder.fromXContent(parser, templateName)); } catch (IOException e) { // TODO: should we handle this with a thrown exception? @@ -112,9 +120,7 @@ private static String replaceVariables(String input, String version, String vers * Replaces all occurrences of given variable with the value */ public static String replaceVariable(String input, String variable, String value) { - return Pattern.compile("${" + variable + "}", Pattern.LITERAL) - .matcher(input) - .replaceAll(value); + return Pattern.compile("${" + variable + "}", Pattern.LITERAL).matcher(input).replaceAll(value); } /** @@ -141,10 +147,21 @@ public static boolean checkTemplateExistsAndVersionIsGTECurrentVersion(String te * null means the template hasn't been switched yet. */ public static boolean checkTemplateExistsAndIsUpToDate( - String templateName, String versionKey, ClusterState state, Logger logger, Version versionComposableTemplateExpected) { - - return checkTemplateExistsAndVersionMatches(templateName, versionKey, state, logger, - Version.CURRENT::equals, versionComposableTemplateExpected); + String templateName, + String versionKey, + ClusterState state, + Logger logger, + Version versionComposableTemplateExpected + ) { + + return checkTemplateExistsAndVersionMatches( + templateName, + versionKey, + state, + logger, + Version.CURRENT::equals, + versionComposableTemplateExpected + ); } /** @@ -158,8 +175,13 @@ public static boolean checkTemplateExistsAndIsUpToDate( * null means the template hasn't been switched yet. */ public static boolean checkTemplateExistsAndVersionMatches( - String templateName, String versionKey, ClusterState state, Logger logger, Predicate predicate, - Version versionComposableTemplateExpected) { + String templateName, + String versionKey, + ClusterState state, + Logger logger, + Predicate predicate, + Version versionComposableTemplateExpected + ) { CompressedXContent mappings; if (versionComposableTemplateExpected != null && state.nodes().getMinNodeVersion().onOrAfter(versionComposableTemplateExpected)) { @@ -191,16 +213,14 @@ public static boolean checkTemplateExistsAndVersionMatches( return false; } } catch (ElasticsearchParseException e) { - logger.error(new ParameterizedMessage( - "Cannot parse the template [{}]", templateName), e); + logger.error(new ParameterizedMessage("Cannot parse the template [{}]", templateName), e); throw new IllegalStateException("Cannot parse the template " + templateName, e); } } return true; } - private static boolean containsCorrectVersion(String versionKey, Map typeMappingMap, - Predicate predicate) { + private static boolean containsCorrectVersion(String versionKey, Map typeMappingMap, Predicate predicate) { @SuppressWarnings("unchecked") Map meta = (Map) typeMappingMap.get("_meta"); if (meta == null) { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/termsenum/action/MultiShardTermsEnum.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/termsenum/action/MultiShardTermsEnum.java index 2a7794df124e6..3d71c14a818d8 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/termsenum/action/MultiShardTermsEnum.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/termsenum/action/MultiShardTermsEnum.java @@ -40,7 +40,7 @@ public final class MultiShardTermsEnum { /** Sole constructor. * @param enums TermsEnums from shards which we should merge - * @throws IOException Errors accessing data + * @throws IOException Errors accessing data **/ public MultiShardTermsEnum(TermsEnum[] enums) throws IOException { queue = new TermMergeQueue(enums.length); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/termsenum/action/NodeTermsEnumRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/termsenum/action/NodeTermsEnumRequest.java index 044968d62da9a..fd206cc9b031f 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/termsenum/action/NodeTermsEnumRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/termsenum/action/NodeTermsEnumRequest.java @@ -42,11 +42,13 @@ public class NodeTermsEnumRequest extends TransportRequest implements IndicesReq private long nodeStartedTimeMillis; - public NodeTermsEnumRequest(OriginalIndices originalIndices, - final String nodeId, - final Set shardIds, - TermsEnumRequest request, - long taskStartTimeMillis) { + public NodeTermsEnumRequest( + OriginalIndices originalIndices, + final String nodeId, + final Set shardIds, + TermsEnumRequest request, + long taskStartTimeMillis + ) { this.originalIndices = originalIndices; this.field = request.field(); this.string = request.string(); @@ -79,10 +81,7 @@ public NodeTermsEnumRequest(StreamInput in) throws IOException { if (in.getVersion().onOrAfter(Version.V_7_15_1)) { originalIndices = OriginalIndices.readOriginalIndices(in); } else { - String[] indicesNames = shardIds.stream() - .map(ShardId::getIndexName) - .distinct() - .toArray(String[]::new); + String[] indicesNames = shardIds.stream().map(ShardId::getIndexName).distinct().toArray(String[]::new); this.originalIndices = new OriginalIndices(indicesNames, null); } } @@ -97,7 +96,7 @@ public void writeTo(StreamOutput out) throws IOException { out.writeVInt(size); // Adjust the amount of permitted time the shard has remaining to gather terms. long timeSpentSoFarInCoordinatingNode = System.currentTimeMillis() - taskStartedTimeMillis; - long remainingTimeForShardToUse = (timeout - timeSpentSoFarInCoordinatingNode); + long remainingTimeForShardToUse = (timeout - timeSpentSoFarInCoordinatingNode); // TODO - if already timed out can we shortcut the trip somehow? Throw exception if remaining time < 0? out.writeVLong(remainingTimeForShardToUse); out.writeVLong(taskStartedTimeMillis); @@ -160,6 +159,7 @@ public int size() { public long timeout() { return timeout; } + public String nodeId() { return nodeId; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/termsenum/action/SimpleTermCountEnum.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/termsenum/action/SimpleTermCountEnum.java index eb4779be52bce..9aa7e74fe0521 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/termsenum/action/SimpleTermCountEnum.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/termsenum/action/SimpleTermCountEnum.java @@ -24,15 +24,15 @@ * but can't return a raw Lucene TermsEnum. */ public class SimpleTermCountEnum extends TermsEnum { - int index =-1; + int index = -1; TermCount[] sortedTerms; TermCount current = null; - + public SimpleTermCountEnum(TermCount[] terms) { sortedTerms = Arrays.copyOf(terms, terms.length); Arrays.sort(sortedTerms, Comparator.comparing(TermCount::getTerm)); } - + public SimpleTermCountEnum(TermCount termCount) { sortedTerms = new TermCount[1]; sortedTerms[0] = termCount; @@ -44,7 +44,7 @@ public BytesRef term() throws IOException { return null; } return new BytesRef(current.getTerm()); - } + } @Override public BytesRef next() throws IOException { @@ -56,18 +56,17 @@ public BytesRef next() throws IOException { } return term(); } - + @Override public int docFreq() throws IOException { if (current == null) { return 0; } return (int) current.getDocCount(); - } + } + + // =============== All other TermsEnum methods not supported ================= - - //=============== All other TermsEnum methods not supported ================= - @Override public AttributeSource attributes() { throw new UnsupportedOperationException(); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/termsenum/action/TermCount.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/termsenum/action/TermCount.java index 52573ac088a06..a580f1d1aca6d 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/termsenum/action/TermCount.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/termsenum/action/TermCount.java @@ -6,11 +6,11 @@ */ package org.elasticsearch.xpack.core.termsenum.action; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentFragment; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/termsenum/action/TermsEnumAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/termsenum/action/TermsEnumAction.java index d01b2525cda8e..0bf422c6853eb 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/termsenum/action/TermsEnumAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/termsenum/action/TermsEnumAction.java @@ -7,9 +7,9 @@ package org.elasticsearch.xpack.core.termsenum.action; import org.elasticsearch.action.ActionType; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -21,7 +21,6 @@ public class TermsEnumAction extends ActionType { public static final TermsEnumAction INSTANCE = new TermsEnumAction(); public static final String NAME = "indices:data/read/xpack/termsenum/list"; - static final ParseField INDEX_FILTER = new ParseField("index_filter"); static final ParseField TIMEOUT = new ParseField("timeout"); @@ -42,9 +41,12 @@ public static TermsEnumRequest fromXContent(XContentParser parser, String... ind PARSER.declareString(TermsEnumRequest::searchAfter, new ParseField("search_after")); PARSER.declareInt(TermsEnumRequest::size, new ParseField("size")); PARSER.declareBoolean(TermsEnumRequest::caseInsensitive, new ParseField("case_insensitive")); - PARSER.declareField(TermsEnumRequest::timeout, + PARSER.declareField( + TermsEnumRequest::timeout, (p, c) -> TimeValue.parseTimeValue(p.text(), TIMEOUT.getPreferredName()), - TIMEOUT, ObjectParser.ValueType.STRING); + TIMEOUT, + ObjectParser.ValueType.STRING + ); PARSER.declareObject(TermsEnumRequest::indexFilter, (p, context) -> parseInnerQueryBuilder(p), INDEX_FILTER); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/termsenum/action/TermsEnumRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/termsenum/action/TermsEnumRequest.java index fcc17a58bc52f..8ba37ab403a2c 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/termsenum/action/TermsEnumRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/termsenum/action/TermsEnumRequest.java @@ -16,15 +16,14 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.index.query.QueryBuilder; import java.io.IOException; import java.util.Arrays; import java.util.Objects; - /** * A request to gather terms for a given field matching a string prefix */ @@ -118,8 +117,7 @@ public ActionRequestValidationException validate() { validationException = ValidateActions.addValidationError("Timeout cannot be null", validationException); } else { if (timeout().getSeconds() > 60) { - validationException = ValidateActions.addValidationError("Timeout cannot be > 1 minute", - validationException); + validationException = ValidateActions.addValidationError("Timeout cannot be > 1 minute", validationException); } } return validationException; @@ -226,10 +224,24 @@ public QueryBuilder indexFilter() { @Override public String toString() { - return "[" + Arrays.toString(indices) + "] field[" + field + "], string[" + string + "] " + " size=" + size + " timeout=" - + timeout().getMillis() + " case_insensitive=" - + caseInsensitive + " indexFilter = "+ indexFilter + - " searchAfter[" + searchAfter + "]" ; + return "[" + + Arrays.toString(indices) + + "] field[" + + field + + "], string[" + + string + + "] " + + " size=" + + size + + " timeout=" + + timeout().getMillis() + + " case_insensitive=" + + caseInsensitive + + " indexFilter = " + + indexFilter + + " searchAfter[" + + searchAfter + + "]"; } @Override @@ -250,8 +262,7 @@ public boolean equals(Object o) { @Override public int hashCode() { - int result = Objects.hash(field, string, searchAfter, size, caseInsensitive, - indexFilter, indicesOptions(), timeout()); + int result = Objects.hash(field, string, searchAfter, size, caseInsensitive, indexFilter, indicesOptions(), timeout()); result = 31 * result + Arrays.hashCode(indices); return result; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/termsenum/action/TermsEnumResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/termsenum/action/TermsEnumResponse.java index 433d213f020dd..3fe45afeb23ad 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/termsenum/action/TermsEnumResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/termsenum/action/TermsEnumResponse.java @@ -8,10 +8,10 @@ import org.elasticsearch.action.support.DefaultShardOperationFailedException; import org.elasticsearch.action.support.broadcast.BroadcastResponse; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -67,7 +67,8 @@ public TermsEnumResponse( int totalShards, int successfulShards, int failedShards, - List shardFailures, boolean complete + List shardFailures, + boolean complete ) { super(totalShards, successfulShards, failedShards, shardFailures); this.terms = terms == null ? Collections.emptyList() : terms; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/termsenum/action/TransportTermsEnumAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/termsenum/action/TransportTermsEnumAction.java index 3770d77efb7d9..b67bbcd251f9f 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/termsenum/action/TransportTermsEnumAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/termsenum/action/TransportTermsEnumAction.java @@ -140,11 +140,13 @@ protected void doExecute(Task task, TermsEnumRequest request, ActionListener shardIds, - TermsEnumRequest request, - long taskStartMillis) { + protected NodeTermsEnumRequest newNodeRequest( + final OriginalIndices originalIndices, + final String nodeId, + final Set shardIds, + TermsEnumRequest request, + long taskStartMillis + ) { // Given we look terms up in the terms dictionary alias filters is another aspect of search (like DLS) that we // currently do not support. // final ClusterState clusterState = clusterService.state(); @@ -254,21 +256,20 @@ protected TermsEnumResponse mergeResponses( successfulShards += rc.resp.getSuccessfulShards(); failedShards += rc.resp.getFailedShards(); for (DefaultShardOperationFailedException exc : rc.resp.getShardFailures()) { - shardFailures.add(new DefaultShardOperationFailedException(rc.clusterAlias + ":" + exc.index(), - exc.shardId(), exc.getCause())); + shardFailures.add( + new DefaultShardOperationFailedException(rc.clusterAlias + ":" + exc.index(), exc.shardId(), exc.getCause()) + ); } - List terms = rc.resp.getTerms().stream() - .map(a -> new TermCount(a, 1)) - .collect(Collectors.toList()); + List terms = rc.resp.getTerms().stream().map(a -> new TermCount(a, 1)).collect(Collectors.toList()); termsList.add(terms); } else { throw new AssertionError("Unknown atomic response type: " + atomicResponse.getClass().getName()); } } - List ans = termsList.size() == 1 ? termsList.get(0).stream() - .map(TermCount::getTerm) - .collect(Collectors.toList()) : mergeResponses(termsList, request.size()); + List ans = termsList.size() == 1 + ? termsList.get(0).stream().map(TermCount::getTerm).collect(Collectors.toList()) + : mergeResponses(termsList, request.size()); return new TermsEnumResponse(ans, (failedShards + successfulShards), successfulShards, failedShards, shardFailures, complete); } @@ -366,7 +367,6 @@ protected NodeTermsEnumResponse dataNodeOperation(NodeTermsEnumRequest request, } MultiShardTermsEnum te = new MultiShardTermsEnum(shardTermsEnums.toArray(new TermsEnum[0])); - int shard_size = request.size(); // All the above prep might take a while - do a timer check now before we continue further. if (System.currentTimeMillis() > scheduledEnd) { @@ -490,8 +490,9 @@ protected AsyncBroadcastAction(Task task, TermsEnumRequest request, ActionListen this.localIndices = remoteClusterIndices.remove(RemoteClusterAware.LOCAL_CLUSTER_GROUP_KEY); // update to concrete indices - String[] concreteIndices = localIndices == null ? new String[0] : - indexNameExpressionResolver.concreteIndexNames(clusterState, localIndices); + String[] concreteIndices = localIndices == null + ? new String[0] + : indexNameExpressionResolver.concreteIndexNames(clusterState, localIndices); blockException = checkRequestBlock(clusterState, request, concreteIndices); if (blockException != null) { throw blockException; @@ -529,12 +530,12 @@ public void start() { // really, no shards active in this group onNodeFailure(nodeId, numOps, null); } - ++ numOps; + ++numOps; } // handle remote clusters for (String clusterAlias : remoteClusterIndices.keySet()) { performRemoteClusterOperation(clusterAlias, remoteClusterIndices.get(clusterAlias), numOps); - ++ numOps; + ++numOps; } } @@ -589,19 +590,19 @@ public void handleException(TransportException exc) { } } - void performRemoteClusterOperation(final String clusterAlias, - final OriginalIndices remoteIndices, - final int opsIndex) { + void performRemoteClusterOperation(final String clusterAlias, final OriginalIndices remoteIndices, final int opsIndex) { try { - TermsEnumRequest req = new TermsEnumRequest(request) - .indices(remoteIndices.indices()); + TermsEnumRequest req = new TermsEnumRequest(request).indices(remoteIndices.indices()); Client remoteClient = remoteClusterService.getRemoteClusterClient(transportService.getThreadPool(), clusterAlias); remoteClient.execute(TermsEnumAction.INSTANCE, req, new ActionListener<>() { @Override public void onResponse(TermsEnumResponse termsEnumResponse) { - onRemoteClusterResponse(clusterAlias, opsIndex, - new RemoteClusterTermsEnumResponse(clusterAlias, termsEnumResponse)); + onRemoteClusterResponse( + clusterAlias, + opsIndex, + new RemoteClusterTermsEnumResponse(clusterAlias, termsEnumResponse) + ); } @Override @@ -624,9 +625,7 @@ private void onNodeResponse(String nodeId, int opsIndex, NodeTermsEnumResponse r } } - private void onRemoteClusterResponse(String clusterAlias, - int opsIndex, - RemoteClusterTermsEnumResponse response) { + private void onRemoteClusterResponse(String clusterAlias, int opsIndex, RemoteClusterTermsEnumResponse response) { logger.trace("received response for cluster {}", clusterAlias); atomicResponses.set(opsIndex, response); if (expectedOps == counterOps.incrementAndGet()) { @@ -698,10 +697,7 @@ private void asyncNodeOperation(NodeTermsEnumRequest request, Task task, ActionL ThreadContext threadContext = transportService.getThreadPool().getThreadContext(); final XPackLicenseState frozenLicenseState = licenseState.copyCurrentLicenseState(); for (ShardId shardId : request.shardIds().toArray(new ShardId[0])) { - if (canAccess(shardId, request, frozenLicenseState, threadContext) == false || canMatchShard( - shardId, - request - ) == false) { + if (canAccess(shardId, request, frozenLicenseState, threadContext) == false || canMatchShard(shardId, request) == false) { // Permission denied or can't match, remove shardID from request request.remove(shardId); } @@ -710,10 +706,8 @@ private void asyncNodeOperation(NodeTermsEnumRequest request, Task task, ActionL listener.onResponse(new NodeTermsEnumResponse(request.nodeId(), Collections.emptyList(), null, true)); } else { // Use the search threadpool if its queue is empty - assert transportService.getThreadPool() - .executor( - ThreadPool.Names.SEARCH - ) instanceof EsThreadPoolExecutor : "SEARCH threadpool must be an instance of ThreadPoolExecutor"; + assert transportService.getThreadPool().executor(ThreadPool.Names.SEARCH) instanceof EsThreadPoolExecutor + : "SEARCH threadpool must be an instance of ThreadPoolExecutor"; EsThreadPoolExecutor ex = (EsThreadPoolExecutor) transportService.getThreadPool().executor(ThreadPool.Names.SEARCH); final String executorName = ex.getQueue().size() == 0 ? ThreadPool.Names.SEARCH : shardExecutor; transportService.getThreadPool() diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/termsenum/action/package-info.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/termsenum/action/package-info.java index 4042ef981827f..74b09c27ff972 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/termsenum/action/package-info.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/termsenum/action/package-info.java @@ -7,4 +7,4 @@ /** * Enumerate a field's terms action. */ -package org.elasticsearch.xpack.core.termsenum.action; \ No newline at end of file +package org.elasticsearch.xpack.core.termsenum.action; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/termsenum/rest/RestTermsEnumAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/termsenum/rest/RestTermsEnumAction.java index bc5dcd3b4d35a..059c3675871b2 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/termsenum/rest/RestTermsEnumAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/termsenum/rest/RestTermsEnumAction.java @@ -8,10 +8,10 @@ import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.common.Strings; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestToXContentListener; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.termsenum.action.TermsEnumAction; import org.elasticsearch.xpack.core.termsenum.action.TermsEnumRequest; @@ -25,9 +25,7 @@ public class RestTermsEnumAction extends BaseRestHandler { @Override public List routes() { - return List.of( - new Route(GET, "/{index}/_terms_enum"), - new Route(POST, "/{index}/_terms_enum")); + return List.of(new Route(GET, "/{index}/_terms_enum"), new Route(POST, "/{index}/_terms_enum")); } @Override @@ -38,10 +36,11 @@ public String getName() { @Override public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException { try (XContentParser parser = request.contentOrSourceParamParser()) { - TermsEnumRequest termEnumRequest = TermsEnumAction.fromXContent(parser, - Strings.splitStringByCommaToArray(request.param("index"))); - return channel -> - client.execute(TermsEnumAction.INSTANCE, termEnumRequest, new RestToXContentListener<>(channel)); + TermsEnumRequest termEnumRequest = TermsEnumAction.fromXContent( + parser, + Strings.splitStringByCommaToArray(request.param("index")) + ); + return channel -> client.execute(TermsEnumAction.INSTANCE, termEnumRequest, new RestToXContentListener<>(channel)); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/textstructure/action/FindStructureAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/textstructure/action/FindStructureAction.java index aa0a7c3dfafd2..e1f4f55ff215b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/textstructure/action/FindStructureAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/textstructure/action/FindStructureAction.java @@ -10,15 +10,15 @@ import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.ActionResponse; import org.elasticsearch.action.ActionType; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.xcontent.StatusToXContentObject; -import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.textstructure.structurefinder.TextStructure; import java.io.IOException; @@ -107,8 +107,9 @@ public static class Request extends ActionRequest { public static final ParseField TIMESTAMP_FORMAT = new ParseField("timestamp_format"); public static final ParseField TIMESTAMP_FIELD = TextStructure.TIMESTAMP_FIELD; - private static final String ARG_INCOMPATIBLE_WITH_FORMAT_TEMPLATE = - "[%s] may only be specified if [" + FORMAT.getPreferredName() + "] is [%s]"; + private static final String ARG_INCOMPATIBLE_WITH_FORMAT_TEMPLATE = "[%s] may only be specified if [" + + FORMAT.getPreferredName() + + "] is [%s]"; private Integer linesToSample; private Integer lineMergeSizeLimit; @@ -125,8 +126,7 @@ public static class Request extends ActionRequest { private String timestampField; private BytesReference sample; - public Request() { - } + public Request() {} public Request(StreamInput in) throws IOException { super(in); @@ -146,7 +146,6 @@ public Request(StreamInput in) throws IOException { sample = in.readBytesReference(); } - public Integer getLinesToSample() { return linesToSample; } @@ -287,10 +286,15 @@ public void setSample(BytesReference sample) { this.sample = sample; } - private static ActionRequestValidationException addIncompatibleArgError(ParseField arg, TextStructure.Format format, - ActionRequestValidationException validationException) { - return addValidationError(String.format(Locale.ROOT, ARG_INCOMPATIBLE_WITH_FORMAT_TEMPLATE, arg.getPreferredName(), format), - validationException); + private static ActionRequestValidationException addIncompatibleArgError( + ParseField arg, + TextStructure.Format format, + ActionRequestValidationException validationException + ) { + return addValidationError( + String.format(Locale.ROOT, ARG_INCOMPATIBLE_WITH_FORMAT_TEMPLATE, arg.getPreferredName(), format), + validationException + ); } @Override @@ -299,11 +303,14 @@ public ActionRequestValidationException validate() { if (linesToSample != null && linesToSample < MIN_SAMPLE_LINE_COUNT) { validationException = addValidationError( "[" + LINES_TO_SAMPLE.getPreferredName() + "] must be at least [" + MIN_SAMPLE_LINE_COUNT + "] if specified", - validationException); + validationException + ); } if (lineMergeSizeLimit != null && lineMergeSizeLimit <= 0) { - validationException = addValidationError("[" + LINE_MERGE_SIZE_LIMIT.getPreferredName() + "] must be positive if specified", - validationException); + validationException = addValidationError( + "[" + LINE_MERGE_SIZE_LIMIT.getPreferredName() + "] must be positive if specified", + validationException + ); } if (format != TextStructure.Format.DELIMITED) { if (columnNames != null) { @@ -324,8 +331,11 @@ public ActionRequestValidationException validate() { } if (format != TextStructure.Format.SEMI_STRUCTURED_TEXT) { if (grokPattern != null) { - validationException = - addIncompatibleArgError(GROK_PATTERN, TextStructure.Format.SEMI_STRUCTURED_TEXT, validationException); + validationException = addIncompatibleArgError( + GROK_PATTERN, + TextStructure.Format.SEMI_STRUCTURED_TEXT, + validationException + ); } } if (sample == null || sample.length() == 0) { @@ -375,8 +385,20 @@ public void writeTo(StreamOutput out) throws IOException { @Override public int hashCode() { - return Objects.hash(linesToSample, lineMergeSizeLimit, timeout, charset, format, columnNames, hasHeaderRow, delimiter, - grokPattern, timestampFormat, timestampField, sample); + return Objects.hash( + linesToSample, + lineMergeSizeLimit, + timeout, + charset, + format, + columnNames, + hasHeaderRow, + delimiter, + grokPattern, + timestampFormat, + timestampField, + sample + ); } @Override @@ -391,18 +413,18 @@ public boolean equals(Object other) { } Request that = (Request) other; - return Objects.equals(this.linesToSample, that.linesToSample) && - Objects.equals(this.lineMergeSizeLimit, that.lineMergeSizeLimit) && - Objects.equals(this.timeout, that.timeout) && - Objects.equals(this.charset, that.charset) && - Objects.equals(this.format, that.format) && - Objects.equals(this.columnNames, that.columnNames) && - Objects.equals(this.hasHeaderRow, that.hasHeaderRow) && - Objects.equals(this.delimiter, that.delimiter) && - Objects.equals(this.grokPattern, that.grokPattern) && - Objects.equals(this.timestampFormat, that.timestampFormat) && - Objects.equals(this.timestampField, that.timestampField) && - Objects.equals(this.sample, that.sample); + return Objects.equals(this.linesToSample, that.linesToSample) + && Objects.equals(this.lineMergeSizeLimit, that.lineMergeSizeLimit) + && Objects.equals(this.timeout, that.timeout) + && Objects.equals(this.charset, that.charset) + && Objects.equals(this.format, that.format) + && Objects.equals(this.columnNames, that.columnNames) + && Objects.equals(this.hasHeaderRow, that.hasHeaderRow) + && Objects.equals(this.delimiter, that.delimiter) + && Objects.equals(this.grokPattern, that.grokPattern) + && Objects.equals(this.timestampFormat, that.timestampFormat) + && Objects.equals(this.timestampField, that.timestampField) + && Objects.equals(this.sample, that.sample); } } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/textstructure/structurefinder/FieldStats.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/textstructure/structurefinder/FieldStats.java index 842296ccb59a7..500c6e590f6e8 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/textstructure/structurefinder/FieldStats.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/textstructure/structurefinder/FieldStats.java @@ -6,12 +6,12 @@ */ package org.elasticsearch.xpack.core.textstructure.structurefinder; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -34,9 +34,21 @@ public class FieldStats implements ToXContentObject, Writeable { static final ParseField TOP_HITS = new ParseField("top_hits"); @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("field_stats", false, - a -> new FieldStats((long) a[0], (int) a[1], (Double) a[2], (Double) a[3], (Double) a[4], (Double) a[5], - (String) a[6], (String) a[7], (List>) a[8])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "field_stats", + false, + a -> new FieldStats( + (long) a[0], + (int) a[1], + (Double) a[2], + (Double) a[3], + (Double) a[4], + (Double) a[5], + (String) a[6], + (String) a[7], + (List>) a[8] + ) + ); static { PARSER.declareLong(ConstructingObjectParser.constructorArg(), COUNT); @@ -68,13 +80,29 @@ public FieldStats(long count, int cardinality, String earliestTimestamp, String this(count, cardinality, null, null, null, null, earliestTimestamp, latestTimestamp, topHits); } - public FieldStats(long count, int cardinality, Double minValue, Double maxValue, Double meanValue, Double medianValue, - List> topHits) { + public FieldStats( + long count, + int cardinality, + Double minValue, + Double maxValue, + Double meanValue, + Double medianValue, + List> topHits + ) { this(count, cardinality, minValue, maxValue, meanValue, medianValue, null, null, topHits); } - FieldStats(long count, int cardinality, Double minValue, Double maxValue, Double meanValue, Double medianValue, - String earliestTimestamp, String latestTimestamp, List> topHits) { + FieldStats( + long count, + int cardinality, + Double minValue, + Double maxValue, + Double meanValue, + Double medianValue, + String earliestTimestamp, + String latestTimestamp, + List> topHits + ) { this.count = count; this.cardinality = cardinality; this.minValue = minValue; @@ -206,15 +234,15 @@ public boolean equals(Object other) { } FieldStats that = (FieldStats) other; - return this.count == that.count && - this.cardinality == that.cardinality && - Objects.equals(this.minValue, that.minValue) && - Objects.equals(this.maxValue, that.maxValue) && - Objects.equals(this.meanValue, that.meanValue) && - Objects.equals(this.medianValue, that.medianValue) && - Objects.equals(this.earliestTimestamp, that.earliestTimestamp) && - Objects.equals(this.latestTimestamp, that.latestTimestamp) && - Objects.equals(this.topHits, that.topHits); + return this.count == that.count + && this.cardinality == that.cardinality + && Objects.equals(this.minValue, that.minValue) + && Objects.equals(this.maxValue, that.maxValue) + && Objects.equals(this.meanValue, that.meanValue) + && Objects.equals(this.medianValue, that.medianValue) + && Objects.equals(this.earliestTimestamp, that.earliestTimestamp) + && Objects.equals(this.latestTimestamp, that.latestTimestamp) + && Objects.equals(this.topHits, that.topHits); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/textstructure/structurefinder/TextStructure.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/textstructure/structurefinder/TextStructure.java index 366d05fb1e064..0b6d798cbfd1b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/textstructure/structurefinder/TextStructure.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/textstructure/structurefinder/TextStructure.java @@ -6,11 +6,11 @@ */ package org.elasticsearch.xpack.core.textstructure.structurefinder; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -32,7 +32,10 @@ public class TextStructure implements ToXContentObject, Writeable { public enum Format { - NDJSON, XML, DELIMITED, SEMI_STRUCTURED_TEXT; + NDJSON, + XML, + DELIMITED, + SEMI_STRUCTURED_TEXT; public boolean supportsNesting() { switch (this) { @@ -164,12 +167,30 @@ public String toString() { private final SortedMap fieldStats; private final List explanation; - public TextStructure(int numLinesAnalyzed, int numMessagesAnalyzed, String sampleStart, String charset, Boolean hasByteOrderMarker, - Format format, String multilineStartPattern, String excludeLinesPattern, List columnNames, - Boolean hasHeaderRow, Character delimiter, Character quote, Boolean shouldTrimFields, String grokPattern, - String timestampField, List jodaTimestampFormats, List javaTimestampFormats, - boolean needClientTimezone, Map mappings, Map ingestPipeline, - Map fieldStats, List explanation) { + public TextStructure( + int numLinesAnalyzed, + int numMessagesAnalyzed, + String sampleStart, + String charset, + Boolean hasByteOrderMarker, + Format format, + String multilineStartPattern, + String excludeLinesPattern, + List columnNames, + Boolean hasHeaderRow, + Character delimiter, + Character quote, + Boolean shouldTrimFields, + String grokPattern, + String timestampField, + List jodaTimestampFormats, + List javaTimestampFormats, + boolean needClientTimezone, + Map mappings, + Map ingestPipeline, + Map fieldStats, + List explanation + ) { this.numLinesAnalyzed = numLinesAnalyzed; this.numMessagesAnalyzed = numMessagesAnalyzed; @@ -432,9 +453,29 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws @Override public int hashCode() { - return Objects.hash(numLinesAnalyzed, numMessagesAnalyzed, sampleStart, charset, hasByteOrderMarker, format, - multilineStartPattern, excludeLinesPattern, columnNames, hasHeaderRow, delimiter, quote, shouldTrimFields, grokPattern, - timestampField, jodaTimestampFormats, javaTimestampFormats, needClientTimezone, mappings, fieldStats, explanation); + return Objects.hash( + numLinesAnalyzed, + numMessagesAnalyzed, + sampleStart, + charset, + hasByteOrderMarker, + format, + multilineStartPattern, + excludeLinesPattern, + columnNames, + hasHeaderRow, + delimiter, + quote, + shouldTrimFields, + grokPattern, + timestampField, + jodaTimestampFormats, + javaTimestampFormats, + needClientTimezone, + mappings, + fieldStats, + explanation + ); } @Override @@ -449,27 +490,27 @@ public boolean equals(Object other) { } TextStructure that = (TextStructure) other; - return this.numLinesAnalyzed == that.numLinesAnalyzed && - this.numMessagesAnalyzed == that.numMessagesAnalyzed && - Objects.equals(this.sampleStart, that.sampleStart) && - Objects.equals(this.charset, that.charset) && - Objects.equals(this.hasByteOrderMarker, that.hasByteOrderMarker) && - Objects.equals(this.format, that.format) && - Objects.equals(this.multilineStartPattern, that.multilineStartPattern) && - Objects.equals(this.excludeLinesPattern, that.excludeLinesPattern) && - Objects.equals(this.columnNames, that.columnNames) && - Objects.equals(this.hasHeaderRow, that.hasHeaderRow) && - Objects.equals(this.delimiter, that.delimiter) && - Objects.equals(this.quote, that.quote) && - Objects.equals(this.shouldTrimFields, that.shouldTrimFields) && - Objects.equals(this.grokPattern, that.grokPattern) && - Objects.equals(this.timestampField, that.timestampField) && - Objects.equals(this.jodaTimestampFormats, that.jodaTimestampFormats) && - Objects.equals(this.javaTimestampFormats, that.javaTimestampFormats) && - this.needClientTimezone == that.needClientTimezone && - Objects.equals(this.mappings, that.mappings) && - Objects.equals(this.fieldStats, that.fieldStats) && - Objects.equals(this.explanation, that.explanation); + return this.numLinesAnalyzed == that.numLinesAnalyzed + && this.numMessagesAnalyzed == that.numMessagesAnalyzed + && Objects.equals(this.sampleStart, that.sampleStart) + && Objects.equals(this.charset, that.charset) + && Objects.equals(this.hasByteOrderMarker, that.hasByteOrderMarker) + && Objects.equals(this.format, that.format) + && Objects.equals(this.multilineStartPattern, that.multilineStartPattern) + && Objects.equals(this.excludeLinesPattern, that.excludeLinesPattern) + && Objects.equals(this.columnNames, that.columnNames) + && Objects.equals(this.hasHeaderRow, that.hasHeaderRow) + && Objects.equals(this.delimiter, that.delimiter) + && Objects.equals(this.quote, that.quote) + && Objects.equals(this.shouldTrimFields, that.shouldTrimFields) + && Objects.equals(this.grokPattern, that.grokPattern) + && Objects.equals(this.timestampField, that.timestampField) + && Objects.equals(this.jodaTimestampFormats, that.jodaTimestampFormats) + && Objects.equals(this.javaTimestampFormats, that.javaTimestampFormats) + && this.needClientTimezone == that.needClientTimezone + && Objects.equals(this.mappings, that.mappings) + && Objects.equals(this.fieldStats, that.fieldStats) + && Objects.equals(this.explanation, that.explanation); } public static class Builder { @@ -709,12 +750,14 @@ public TextStructure build() { if (isTimestampFieldSpecified != isJodaTimestampFormatsSpecified) { throw new IllegalArgumentException( - "Timestamp field and Joda timestamp formats must both be specified or neither be specified."); + "Timestamp field and Joda timestamp formats must both be specified or neither be specified." + ); } if (isTimestampFieldSpecified != isJavaTimestampFormatsSpecified) { throw new IllegalArgumentException( - "Timestamp field and Java timestamp formats must both be specified or neither be specified."); + "Timestamp field and Java timestamp formats must both be specified or neither be specified." + ); } if (needClientTimezone && isTimestampFieldSpecified == false) { @@ -729,10 +772,30 @@ public TextStructure build() { throw new IllegalArgumentException("Explanation must be specified."); } - return new TextStructure(numLinesAnalyzed, numMessagesAnalyzed, sampleStart, charset, hasByteOrderMarker, format, - multilineStartPattern, excludeLinesPattern, columnNames, hasHeaderRow, delimiter, quote, shouldTrimFields, grokPattern, - timestampField, jodaTimestampFormats, javaTimestampFormats, needClientTimezone, mappings, ingestPipeline, fieldStats, - explanation); + return new TextStructure( + numLinesAnalyzed, + numMessagesAnalyzed, + sampleStart, + charset, + hasByteOrderMarker, + format, + multilineStartPattern, + excludeLinesPattern, + columnNames, + hasHeaderRow, + delimiter, + quote, + shouldTrimFields, + grokPattern, + timestampField, + jodaTimestampFormats, + javaTimestampFormats, + needClientTimezone, + mappings, + ingestPipeline, + fieldStats, + explanation + ); } } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/TransformDeprecations.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/TransformDeprecations.java index 308f026331a3d..427d31f7c61a9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/TransformDeprecations.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/TransformDeprecations.java @@ -23,7 +23,8 @@ public class TransformDeprecations { public static final String ACTION_MAX_PAGE_SEARCH_SIZE_IS_DEPRECATED = "[max_page_search_size] is deprecated inside pivot. Use settings instead."; - + public static final String MAX_PAGE_SEARCH_SIZE_BREAKING_CHANGES_URL = "https://ela.st/es-deprecation-7-transform-max-page-search-size"; + private TransformDeprecations() {} } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/TransformFeatureSetUsage.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/TransformFeatureSetUsage.java index dd12bb968f177..b81b173c0d709 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/TransformFeatureSetUsage.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/TransformFeatureSetUsage.java @@ -13,8 +13,8 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.XPackFeatureSet.Usage; -import org.elasticsearch.xpack.core.transform.transforms.TransformIndexerStats; import org.elasticsearch.xpack.core.XPackField; +import org.elasticsearch.xpack.core.transform.transforms.TransformIndexerStats; import java.io.IOException; import java.util.Map; @@ -36,9 +36,11 @@ public TransformFeatureSetUsage(StreamInput in) throws IOException { this.accumulatedStats = new TransformIndexerStats(in); } - public TransformFeatureSetUsage(Map transformCountByState, - Map transformCountByFeature, - TransformIndexerStats accumulatedStats) { + public TransformFeatureSetUsage( + Map transformCountByState, + Map transformCountByFeature, + TransformIndexerStats accumulatedStats + ) { super(XPackField.TRANSFORM, true, true); this.transformCountByState = Objects.requireNonNull(transformCountByState); this.transformCountByFeature = Objects.requireNonNull(transformCountByFeature); @@ -94,9 +96,11 @@ public boolean equals(Object obj) { return false; } TransformFeatureSetUsage other = (TransformFeatureSetUsage) obj; - return Objects.equals(name, other.name) && available == other.available && enabled == other.enabled - && Objects.equals(transformCountByState, other.transformCountByState) - && Objects.equals(transformCountByFeature, other.transformCountByFeature) - && Objects.equals(accumulatedStats, other.accumulatedStats); + return Objects.equals(name, other.name) + && available == other.available + && enabled == other.enabled + && Objects.equals(transformCountByState, other.transformCountByState) + && Objects.equals(transformCountByFeature, other.transformCountByFeature) + && Objects.equals(accumulatedStats, other.accumulatedStats); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/TransformMetadata.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/TransformMetadata.java index 8598dbc12999b..38d71d35f59d0 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/TransformMetadata.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/TransformMetadata.java @@ -12,12 +12,12 @@ import org.elasticsearch.cluster.Diff; import org.elasticsearch.cluster.NamedDiff; import org.elasticsearch.cluster.metadata.Metadata; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; @@ -25,17 +25,17 @@ import java.util.EnumSet; import java.util.Objects; - public class TransformMetadata implements Metadata.Custom { public static final String TYPE = "transform"; public static final ParseField RESET_MODE = new ParseField("reset_mode"); public static final TransformMetadata EMPTY_METADATA = new TransformMetadata(false); // This parser follows the pattern that metadata is parsed leniently (to allow for enhancements) - public static final ObjectParser LENIENT_PARSER = new ObjectParser<>("" + - "transform_metadata", + public static final ObjectParser LENIENT_PARSER = new ObjectParser<>( + "" + "transform_metadata", true, - TransformMetadata.Builder::new); + TransformMetadata.Builder::new + ); static { LENIENT_PARSER.declareBoolean(TransformMetadata.Builder::isResetMode, RESET_MODE); @@ -121,10 +121,8 @@ public String getWriteableName() { @Override public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; TransformMetadata that = (TransformMetadata) o; return resetMode == that.resetMode; } @@ -147,8 +145,7 @@ public static TransformMetadata.Builder from(@Nullable TransformMetadata previou return new TransformMetadata.Builder(previous); } - public Builder() { - } + public Builder() {} public Builder(@Nullable TransformMetadata previous) { if (previous != null) { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/TransformNamedXContentProvider.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/TransformNamedXContentProvider.java index 9d0dd568d2ed0..b508f70bb7aed 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/TransformNamedXContentProvider.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/TransformNamedXContentProvider.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.transform; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.plugins.spi.NamedXContentProvider; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.transform.transforms.RetentionPolicyConfig; import org.elasticsearch.xpack.core.transform.transforms.SyncConfig; import org.elasticsearch.xpack.core.transform.transforms.TimeRetentionPolicyConfig; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/action/GetTransformAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/action/GetTransformAction.java index 736f1f4a13abe..fbedae810d3d3 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/action/GetTransformAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/action/GetTransformAction.java @@ -9,12 +9,12 @@ import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.ActionType; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.ValidationException; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.logging.DeprecationCategory; import org.elasticsearch.common.logging.DeprecationLogger; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.action.AbstractGetResourcesRequest; @@ -125,8 +125,12 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.field(TransformField.COUNT.getPreferredName(), invalidTransforms.size()); builder.field(TransformField.TRANSFORMS.getPreferredName(), invalidTransforms); builder.endObject(); - deprecationLogger.critical(DeprecationCategory.OTHER, "invalid_transforms", - INVALID_TRANSFORMS_DEPRECATION_WARNING, invalidTransforms.size()); + deprecationLogger.critical( + DeprecationCategory.OTHER, + "invalid_transforms", + INVALID_TRANSFORMS_DEPRECATION_WARNING, + invalidTransforms.size() + ); } builder.endObject(); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/action/GetTransformStatsAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/action/GetTransformStatsAction.java index 6154f7582bda8..f7940cd79bdf6 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/action/GetTransformStatsAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/action/GetTransformStatsAction.java @@ -18,9 +18,9 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.tasks.Task; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.tasks.Task; import org.elasticsearch.xpack.core.action.util.PageParams; import org.elasticsearch.xpack.core.action.util.QueryPage; import org.elasticsearch.xpack.core.transform.TransformField; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/action/SetResetModeAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/action/SetResetModeAction.java index 5d688180319a2..8b4b4d438d4e8 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/action/SetResetModeAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/action/SetResetModeAction.java @@ -9,7 +9,6 @@ import org.elasticsearch.action.ActionType; import org.elasticsearch.action.support.master.AcknowledgedResponse; - public class SetResetModeAction extends ActionType { public static final SetResetModeAction INSTANCE = new SetResetModeAction(); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/action/StartTransformAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/action/StartTransformAction.java index 4ea1e7bea824b..d985e2d1bbd85 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/action/StartTransformAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/action/StartTransformAction.java @@ -45,7 +45,7 @@ public Request(String id, TimeValue timeout) { public Request(StreamInput in) throws IOException { super(in); id = in.readString(); - if(in.getVersion().before(Version.V_7_5_0)) { + if (in.getVersion().before(Version.V_7_5_0)) { in.readBoolean(); } } @@ -58,7 +58,7 @@ public String getId() { public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); out.writeString(id); - if(out.getVersion().before(Version.V_7_5_0)) { + if (out.getVersion().before(Version.V_7_5_0)) { out.writeBoolean(false); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/action/StopTransformAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/action/StopTransformAction.java index fe17059eb1caa..1ca8624675189 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/action/StopTransformAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/action/StopTransformAction.java @@ -14,14 +14,14 @@ import org.elasticsearch.action.TaskOperationFailure; import org.elasticsearch.action.support.tasks.BaseTasksRequest; import org.elasticsearch.action.support.tasks.BaseTasksResponse; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.tasks.Task; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.tasks.Task; import org.elasticsearch.xpack.core.transform.TransformField; import org.elasticsearch.xpack.core.transform.utils.ExceptionsHelper; @@ -55,12 +55,14 @@ public static class Request extends BaseTasksRequest { private final boolean waitForCheckpoint; private Set expandedIds; - public Request(String id, - boolean waitForCompletion, - boolean force, - @Nullable TimeValue timeout, - boolean allowNoMatch, - boolean waitForCheckpoint) { + public Request( + String id, + boolean waitForCompletion, + boolean force, + @Nullable TimeValue timeout, + boolean allowNoMatch, + boolean waitForCheckpoint + ) { this.id = ExceptionsHelper.requireNonNull(id, TransformField.ID.getPreferredName()); this.waitForCompletion = waitForCompletion; this.force = force; @@ -107,7 +109,7 @@ public Set getExpandedIds() { return expandedIds; } - public void setExpandedIds(Set expandedIds ) { + public void setExpandedIds(Set expandedIds) { this.expandedIds = expandedIds; } @@ -141,11 +143,14 @@ public void writeTo(StreamOutput out) throws IOException { @Override public ActionRequestValidationException validate() { if (force && waitForCheckpoint) { - return addValidationError(new ParameterizedMessage( - "cannot set both [{}] and [{}] to true", + return addValidationError( + new ParameterizedMessage( + "cannot set both [{}] and [{}] to true", TransformField.FORCE, - TransformField.WAIT_FOR_CHECKPOINT).getFormattedMessage(), - null); + TransformField.WAIT_FOR_CHECKPOINT + ).getFormattedMessage(), + null + ); } return null; } @@ -172,12 +177,12 @@ public boolean equals(Object obj) { return false; } - return Objects.equals(id, other.id) && - Objects.equals(waitForCompletion, other.waitForCompletion) && - Objects.equals(force, other.force) && - Objects.equals(expandedIds, other.expandedIds) && - Objects.equals(waitForCheckpoint, other.waitForCheckpoint) && - allowNoMatch == other.allowNoMatch; + return Objects.equals(id, other.id) + && Objects.equals(waitForCompletion, other.waitForCompletion) + && Objects.equals(force, other.force) + && Objects.equals(expandedIds, other.expandedIds) + && Objects.equals(waitForCheckpoint, other.waitForCheckpoint) + && allowNoMatch == other.allowNoMatch; } @Override @@ -207,9 +212,11 @@ public Response(boolean acknowledged) { this.acknowledged = acknowledged; } - public Response(List taskFailures, - List nodeFailures, - boolean acknowledged) { + public Response( + List taskFailures, + List nodeFailures, + boolean acknowledged + ) { super(taskFailures, nodeFailures); this.acknowledged = acknowledged; } @@ -235,10 +242,8 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws @Override public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; Response response = (Response) o; return acknowledged == response.acknowledged; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/action/compat/PreviewTransformActionDeprecated.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/action/compat/PreviewTransformActionDeprecated.java index 1943470f25f6c..3e1f6251050d9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/action/compat/PreviewTransformActionDeprecated.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/action/compat/PreviewTransformActionDeprecated.java @@ -19,5 +19,4 @@ private PreviewTransformActionDeprecated() { super(NAME, PreviewTransformAction.Response::new); } - } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/action/compat/StartTransformActionDeprecated.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/action/compat/StartTransformActionDeprecated.java index 5b413ef29a27c..8aae058ceacef 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/action/compat/StartTransformActionDeprecated.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/action/compat/StartTransformActionDeprecated.java @@ -10,7 +10,7 @@ import org.elasticsearch.action.ActionType; import org.elasticsearch.xpack.core.transform.action.StartTransformAction; -public class StartTransformActionDeprecated extends ActionType { +public class StartTransformActionDeprecated extends ActionType { public static final StartTransformActionDeprecated INSTANCE = new StartTransformActionDeprecated(); public static final String NAME = "cluster:admin/data_frame/start"; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/notifications/TransformAuditMessage.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/notifications/TransformAuditMessage.java index a7bc15d4d85a2..000efa074d73a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/notifications/TransformAuditMessage.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/notifications/TransformAuditMessage.java @@ -6,8 +6,8 @@ */ package org.elasticsearch.xpack.core.transform.notifications; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xpack.core.common.notifications.AbstractAuditMessage; import org.elasticsearch.xpack.core.common.notifications.Level; import org.elasticsearch.xpack.core.transform.TransformField; @@ -17,8 +17,11 @@ public class TransformAuditMessage extends AbstractAuditMessage { private static final ParseField TRANSFORM_ID = new ParseField(TransformField.TRANSFORM_ID); - public static final ConstructingObjectParser PARSER = - createParser("data_frame_audit_message", TransformAuditMessage::new, TRANSFORM_ID); + public static final ConstructingObjectParser PARSER = createParser( + "data_frame_audit_message", + TransformAuditMessage::new, + TRANSFORM_ID + ); public TransformAuditMessage(String resourceId, String message, Level level, Date timestamp, String nodeName) { super(resourceId, message, level, timestamp, nodeName); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/DestConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/DestConfig.java index 89bcc7403cf70..25d5888ff73dd 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/DestConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/DestConfig.java @@ -38,9 +38,11 @@ public class DestConfig implements Writeable, ToXContentObject { public static final ConstructingObjectParser LENIENT_PARSER = createParser(true); private static ConstructingObjectParser createParser(boolean lenient) { - ConstructingObjectParser parser = new ConstructingObjectParser<>("data_frame_config_dest", + ConstructingObjectParser parser = new ConstructingObjectParser<>( + "data_frame_config_dest", lenient, - args -> new DestConfig((String)args[0], (String) args[1])); + args -> new DestConfig((String) args[0], (String) args[1]) + ); parser.declareString(constructorArg(), INDEX); parser.declareString(optionalConstructorArg(), PIPELINE); return parser; @@ -78,8 +80,7 @@ public ActionRequestValidationException validate(ActionRequestValidationExceptio return validationException; } - public void checkForDeprecations(String id, NamedXContentRegistry namedXContentRegistry, Consumer onDeprecation) { - } + public void checkForDeprecations(String id, NamedXContentRegistry namedXContentRegistry, Consumer onDeprecation) {} @Override public void writeTo(StreamOutput out) throws IOException { @@ -110,12 +111,11 @@ public boolean equals(Object other) { } DestConfig that = (DestConfig) other; - return Objects.equals(index, that.index) && - Objects.equals(pipeline, that.pipeline); + return Objects.equals(index, that.index) && Objects.equals(pipeline, that.pipeline); } @Override - public int hashCode(){ + public int hashCode() { return Objects.hash(index, pipeline); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/NodeAttributes.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/NodeAttributes.java index 52b5eed2f5e6f..cebd0b15c80de 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/NodeAttributes.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/NodeAttributes.java @@ -7,13 +7,13 @@ package org.elasticsearch.xpack.core.transform.transforms; import org.elasticsearch.cluster.node.DiscoveryNode; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.transform.utils.ExceptionsHelper; @@ -35,27 +35,22 @@ public class NodeAttributes implements ToXContentObject, Writeable { public static final ParseField ATTRIBUTES = new ParseField("attributes"); @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("node", true, - (a) -> { - int i = 0; - String id = (String) a[i++]; - String name = (String) a[i++]; - String ephemeralId = (String) a[i++]; - String transportAddress = (String) a[i++]; - Map attributes = (Map) a[i]; - return new NodeAttributes(id, name, ephemeralId, transportAddress, attributes); - }); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("node", true, (a) -> { + int i = 0; + String id = (String) a[i++]; + String name = (String) a[i++]; + String ephemeralId = (String) a[i++]; + String transportAddress = (String) a[i++]; + Map attributes = (Map) a[i]; + return new NodeAttributes(id, name, ephemeralId, transportAddress, attributes); + }); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), ID); PARSER.declareString(ConstructingObjectParser.constructorArg(), NAME); PARSER.declareString(ConstructingObjectParser.constructorArg(), EPHEMERAL_ID); PARSER.declareString(ConstructingObjectParser.constructorArg(), TRANSPORT_ADDRESS); - PARSER.declareField(ConstructingObjectParser.constructorArg(), - (p, c) -> p.mapStrings(), - ATTRIBUTES, - ObjectParser.ValueType.OBJECT); + PARSER.declareField(ConstructingObjectParser.constructorArg(), (p, c) -> p.mapStrings(), ATTRIBUTES, ObjectParser.ValueType.OBJECT); } private final String id; @@ -65,12 +60,14 @@ public class NodeAttributes implements ToXContentObject, Writeable { private final Map attributes; public static NodeAttributes fromDiscoveryNode(DiscoveryNode node) { - return new NodeAttributes(node.getId(), + return new NodeAttributes( + node.getId(), node.getName(), node.getEphemeralId(), node.getAddress().toString(), // TODO add data_frame attributes when/if they are added - Collections.emptyMap()); + Collections.emptyMap() + ); } public NodeAttributes(String id, String name, String ephemeralId, String transportAddress, Map attributes) { @@ -149,11 +146,11 @@ public boolean equals(Object other) { } NodeAttributes that = (NodeAttributes) other; - return Objects.equals(id, that.id) && - Objects.equals(name, that.name) && - Objects.equals(ephemeralId, that.ephemeralId) && - Objects.equals(transportAddress, that.transportAddress) && - Objects.equals(attributes, that.attributes); + return Objects.equals(id, that.id) + && Objects.equals(name, that.name) + && Objects.equals(ephemeralId, that.ephemeralId) + && Objects.equals(transportAddress, that.transportAddress) + && Objects.equals(attributes, that.attributes); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/QueryConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/QueryConfig.java index f383edbe5d60d..24e8faadb7016 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/QueryConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/QueryConfig.java @@ -15,17 +15,17 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.index.query.AbstractQueryBuilder; +import org.elasticsearch.index.query.MatchAllQueryBuilder; +import org.elasticsearch.index.query.QueryBuilder; +import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.index.query.AbstractQueryBuilder; -import org.elasticsearch.index.query.MatchAllQueryBuilder; -import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.xpack.core.deprecation.DeprecationIssue; import org.elasticsearch.xpack.core.deprecation.DeprecationIssue.Level; import org.elasticsearch.xpack.core.deprecation.LoggingDeprecationAccumulationHandler; @@ -50,8 +50,7 @@ public class QueryConfig extends AbstractDiffable implements Writea private final QueryBuilder query; public static QueryConfig matchAll() { - return new QueryConfig(Collections.singletonMap(MatchAllQueryBuilder.NAME, Collections.emptyMap()), - new MatchAllQueryBuilder()); + return new QueryConfig(Collections.singletonMap(MatchAllQueryBuilder.NAME, Collections.emptyMap()), new MatchAllQueryBuilder()); } public QueryConfig(final Map source, final QueryBuilder query) { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/SettingsConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/SettingsConfig.java index 7934be3893737..27cd1caec192b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/SettingsConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/SettingsConfig.java @@ -13,13 +13,13 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.search.aggregations.MultiBucketConsumerService; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ObjectParser.ValueType; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.search.aggregations.MultiBucketConsumerService; import org.elasticsearch.xpack.core.deprecation.DeprecationIssue; import org.elasticsearch.xpack.core.transform.TransformField; @@ -142,8 +142,7 @@ public ActionRequestValidationException validate(ActionRequestValidationExceptio return validationException; } - public void checkForDeprecations(String id, NamedXContentRegistry namedXContentRegistry, Consumer onDeprecation) { - } + public void checkForDeprecations(String id, NamedXContentRegistry namedXContentRegistry, Consumer onDeprecation) {} @Override public void writeTo(StreamOutput out) throws IOException { @@ -309,7 +308,7 @@ public Builder update(SettingsConfig update) { ? null : update.getDatesAsEpochMillisForUpdate(); } - if (update.getAlignCheckpointsForUpdate() != null) { + if (update.getAlignCheckpointsForUpdate() != null) { this.alignCheckpoints = update.getAlignCheckpointsForUpdate().equals(DEFAULT_ALIGN_CHECKPOINTS) ? null : update.getAlignCheckpointsForUpdate(); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/SourceConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/SourceConfig.java index 2228f16e1ebaf..a3a45fecbe7bb 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/SourceConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/SourceConfig.java @@ -13,14 +13,14 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.license.RemoteClusterLicenseChecker; +import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.license.RemoteClusterLicenseChecker; -import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.xpack.core.deprecation.DeprecationIssue; import org.elasticsearch.xpack.core.transform.TransformField; import org.elasticsearch.xpack.core.transform.utils.ExceptionsHelper; @@ -37,7 +37,6 @@ import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg; import static org.elasticsearch.xcontent.ConstructingObjectParser.optionalConstructorArg; - public class SourceConfig implements Writeable, ToXContentObject { public static final ParseField QUERY = new ParseField("query"); @@ -48,15 +47,13 @@ public class SourceConfig implements Writeable, ToXContentObject { @SuppressWarnings("unchecked") private static ConstructingObjectParser createParser(boolean lenient) { - ConstructingObjectParser parser = new ConstructingObjectParser<>("data_frame_config_source", - lenient, - args -> { - String[] index = ((List)args[0]).toArray(new String[0]); - // default handling: if the user does not specify a query, we default to match_all - QueryConfig queryConfig = args[1] == null ? QueryConfig.matchAll() : (QueryConfig) args[1]; - Map runtimeMappings = args[2] == null ? Collections.emptyMap() : (Map) args[2]; - return new SourceConfig(index, queryConfig, runtimeMappings); - }); + ConstructingObjectParser parser = new ConstructingObjectParser<>("data_frame_config_source", lenient, args -> { + String[] index = ((List) args[0]).toArray(new String[0]); + // default handling: if the user does not specify a query, we default to match_all + QueryConfig queryConfig = args[1] == null ? QueryConfig.matchAll() : (QueryConfig) args[1]; + Map runtimeMappings = args[2] == null ? Collections.emptyMap() : (Map) args[2]; + return new SourceConfig(index, queryConfig, runtimeMappings); + }); parser.declareStringArray(constructorArg(), INDEX); parser.declareObject(optionalConstructorArg(), (p, c) -> QueryConfig.fromXContent(p, lenient), QUERY); parser.declareObject(optionalConstructorArg(), (p, c) -> p.map(), SearchSourceBuilder.RUNTIME_MAPPINGS_FIELD); @@ -95,9 +92,9 @@ public SourceConfig(String[] index, QueryConfig queryConfig, Map } this.index = index; this.queryConfig = ExceptionsHelper.requireNonNull(queryConfig, QUERY.getPreferredName()); - this.runtimeMappings = - Collections.unmodifiableMap( - ExceptionsHelper.requireNonNull(runtimeMappings, SearchSourceBuilder.RUNTIME_MAPPINGS_FIELD.getPreferredName())); + this.runtimeMappings = Collections.unmodifiableMap( + ExceptionsHelper.requireNonNull(runtimeMappings, SearchSourceBuilder.RUNTIME_MAPPINGS_FIELD.getPreferredName()) + ); } public SourceConfig(final StreamInput in) throws IOException { @@ -123,7 +120,8 @@ public Map getRuntimeMappings() { } public Map getScriptBasedRuntimeMappings() { - return getRuntimeMappings().entrySet().stream() + return getRuntimeMappings().entrySet() + .stream() .filter(e -> e.getValue() instanceof Map && ((Map) e.getValue()).containsKey("script")) .collect(toMap(Map.Entry::getKey, Map.Entry::getValue)); } @@ -155,7 +153,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.array(INDEX.getPreferredName(), index); if (params.paramAsBoolean(TransformField.EXCLUDE_GENERATED, false) == false) { builder.field(QUERY.getPreferredName(), queryConfig); - } else if(queryConfig.equals(QueryConfig.matchAll()) == false) { + } else if (queryConfig.equals(QueryConfig.matchAll()) == false) { builder.field(QUERY.getPreferredName(), queryConfig); } if (runtimeMappings.isEmpty() == false) { @@ -181,7 +179,7 @@ public boolean equals(Object other) { } @Override - public int hashCode(){ + public int hashCode() { // Using Arrays.hashCode as Objects.hash does not deeply hash nested arrays. Since we are doing Array.equals, this is necessary int indexArrayHash = Arrays.hashCode(index); return Objects.hash(indexArrayHash, queryConfig, runtimeMappings); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/SyncConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/SyncConfig.java index c8b4e47d52060..0666cd2736622 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/SyncConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/SyncConfig.java @@ -8,8 +8,8 @@ package org.elasticsearch.xpack.core.transform.transforms; import org.elasticsearch.common.io.stream.NamedWriteable; -import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.index.query.QueryBuilder; +import org.elasticsearch.xcontent.ToXContentObject; public interface SyncConfig extends ToXContentObject, NamedWriteable { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TimeRetentionPolicyConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TimeRetentionPolicyConfig.java index 1f9e0868e0d64..9e61f66cfc0b4 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TimeRetentionPolicyConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TimeRetentionPolicyConfig.java @@ -11,12 +11,12 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ObjectParser; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.xpack.core.deprecation.DeprecationIssue; import org.elasticsearch.xpack.core.transform.TransformField; import org.elasticsearch.xpack.core.transform.utils.ExceptionsHelper; @@ -94,8 +94,7 @@ public ActionRequestValidationException validate(ActionRequestValidationExceptio } @Override - public void checkForDeprecations(String id, NamedXContentRegistry namedXContentRegistry, Consumer onDeprecation) { - } + public void checkForDeprecations(String id, NamedXContentRegistry namedXContentRegistry, Consumer onDeprecation) {} @Override public void writeTo(final StreamOutput out) throws IOException { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TimeSyncConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TimeSyncConfig.java index 0404ac31e6402..79a667f2d04f1 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TimeSyncConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TimeSyncConfig.java @@ -11,12 +11,12 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.index.query.QueryBuilder; +import org.elasticsearch.index.query.RangeQueryBuilder; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.index.query.QueryBuilder; -import org.elasticsearch.index.query.RangeQueryBuilder; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; import org.elasticsearch.xpack.core.transform.TransformField; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TransformCheckpointingInfo.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TransformCheckpointingInfo.java index c9f6dcaae90d3..995bbcc0e3017 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TransformCheckpointingInfo.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TransformCheckpointingInfo.java @@ -8,13 +8,13 @@ package org.elasticsearch.xpack.core.transform.transforms; import org.elasticsearch.Version; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TransformDestIndexSettings.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TransformDestIndexSettings.java index 6e2f515c251cf..b2fce28b53a7b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TransformDestIndexSettings.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TransformDestIndexSettings.java @@ -9,12 +9,12 @@ import org.elasticsearch.action.admin.indices.alias.Alias; import org.elasticsearch.cluster.AbstractDiffable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TransformIndexerPosition.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TransformIndexerPosition.java index 312616cf632e0..c35f05961fcae 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TransformIndexerPosition.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TransformIndexerPosition.java @@ -7,13 +7,13 @@ package org.elasticsearch.xpack.core.transform.transforms; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -35,9 +35,11 @@ public class TransformIndexerPosition implements Writeable, ToXContentObject { private final Map bucketPosition; @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(NAME, - true, - args -> new TransformIndexerPosition((Map) args[0],(Map) args[1])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + true, + args -> new TransformIndexerPosition((Map) args[0], (Map) args[1]) + ); static { PARSER.declareField(optionalConstructorArg(), XContentParser::mapOrdered, INDEXER_POSITION, ValueType.OBJECT); @@ -95,8 +97,7 @@ public boolean equals(Object other) { TransformIndexerPosition that = (TransformIndexerPosition) other; - return Objects.equals(this.indexerPosition, that.indexerPosition) && - Objects.equals(this.bucketPosition, that.bucketPosition); + return Objects.equals(this.indexerPosition, that.indexerPosition) && Objects.equals(this.bucketPosition, that.bucketPosition); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TransformIndexerStats.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TransformIndexerStats.java index 33656751cfcf8..bdc66d6257ee2 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TransformIndexerStats.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TransformIndexerStats.java @@ -8,11 +8,11 @@ package org.elasticsearch.xpack.core.transform.transforms; import org.elasticsearch.Version; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.indexing.IndexerJobStats; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TransformProgress.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TransformProgress.java index 4027d83636e77..6b6788c550558 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TransformProgress.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TransformProgress.java @@ -8,12 +8,12 @@ package org.elasticsearch.xpack.core.transform.transforms; import org.elasticsearch.Version; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -33,7 +33,8 @@ public class TransformProgress implements Writeable, ToXContentObject { public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( "data_frame_transform_progress", true, - a -> new TransformProgress((Long) a[0], (Long)a[1], (Long)a[2], (Long)a[3])); + a -> new TransformProgress((Long) a[0], (Long) a[1], (Long) a[2], (Long) a[3]) + ); static { PARSER.declareLong(optionalConstructorArg(), TOTAL_DOCS); @@ -52,11 +53,11 @@ public TransformProgress() { // If we are reading from an old document we need to convert docsRemaining to docsProcessed public TransformProgress(Long totalDocs, Long docsRemaining, Long documentsProcessed, Long documentsIndexed) { - this(totalDocs, - documentsProcessed != null ? - documentsProcessed : - docsRemaining != null && totalDocs != null ? totalDocs - docsRemaining : 0L, - documentsIndexed); + this( + totalDocs, + documentsProcessed != null ? documentsProcessed : docsRemaining != null && totalDocs != null ? totalDocs - docsRemaining : 0L, + documentsIndexed + ); } public TransformProgress(Long totalDocs, Long documentsProcessed, Long documentsIndexed) { @@ -101,7 +102,7 @@ public Double getPercentComplete() { if (documentsProcessed >= totalDocs) { return 100.0; } - return 100.0*(double)documentsProcessed/totalDocs; + return 100.0 * (double) documentsProcessed / totalDocs; } public Long getTotalDocs() { @@ -143,7 +144,7 @@ public boolean equals(Object other) { } @Override - public int hashCode(){ + public int hashCode() { return Objects.hash(documentsProcessed, documentsIndexed, totalDocs); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TransformState.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TransformState.java index cb583c7acb421..7cdf4847a65a7 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TransformState.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TransformState.java @@ -8,17 +8,17 @@ package org.elasticsearch.xpack.core.transform.transforms; import org.elasticsearch.Version; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.persistent.PersistentTaskState; +import org.elasticsearch.tasks.Task; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.persistent.PersistentTaskState; -import org.elasticsearch.tasks.Task; import org.elasticsearch.xpack.core.indexing.IndexerState; import org.elasticsearch.xpack.core.transform.TransformField; @@ -49,7 +49,7 @@ public class TransformState implements Task.Status, PersistentTaskState { public static final ParseField TASK_STATE = new ParseField("task_state"); public static final ParseField INDEXER_STATE = new ParseField("indexer_state"); - // 7.3 BWC: current_position only exists in 7.2. In 7.3+ it is replaced by position. + // 7.3 BWC: current_position only exists in 7.2. In 7.3+ it is replaced by position. public static final ParseField CURRENT_POSITION = new ParseField("current_position"); public static final ParseField POSITION = new ParseField("position"); public static final ParseField CHECKPOINT = new ParseField("checkpoint"); @@ -58,36 +58,35 @@ public class TransformState implements Task.Status, PersistentTaskState { public static final ParseField NODE = new ParseField("node"); public static final ParseField SHOULD_STOP_AT_NEXT_CHECKPOINT = new ParseField("should_stop_at_checkpoint"); - @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(NAME, - true, - args -> { - TransformTaskState taskState = (TransformTaskState) args[0]; - IndexerState indexerState = (IndexerState) args[1]; - Map bwcCurrentPosition = (Map) args[2]; - TransformIndexerPosition transformIndexerPosition = (TransformIndexerPosition) args[3]; - - // BWC handling, translate current_position to position iff position isn't set - if (bwcCurrentPosition != null && transformIndexerPosition == null) { - transformIndexerPosition = new TransformIndexerPosition(bwcCurrentPosition, null); - } - - long checkpoint = (long) args[4]; - String reason = (String) args[5]; - TransformProgress progress = (TransformProgress) args[6]; - NodeAttributes node = (NodeAttributes) args[7]; - boolean shouldStopAtNextCheckpoint = args[8] == null ? false : (boolean)args[8]; - - return new TransformState(taskState, - indexerState, - transformIndexerPosition, - checkpoint, - reason, - progress, - node, - shouldStopAtNextCheckpoint); - }); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(NAME, true, args -> { + TransformTaskState taskState = (TransformTaskState) args[0]; + IndexerState indexerState = (IndexerState) args[1]; + Map bwcCurrentPosition = (Map) args[2]; + TransformIndexerPosition transformIndexerPosition = (TransformIndexerPosition) args[3]; + + // BWC handling, translate current_position to position iff position isn't set + if (bwcCurrentPosition != null && transformIndexerPosition == null) { + transformIndexerPosition = new TransformIndexerPosition(bwcCurrentPosition, null); + } + + long checkpoint = (long) args[4]; + String reason = (String) args[5]; + TransformProgress progress = (TransformProgress) args[6]; + NodeAttributes node = (NodeAttributes) args[7]; + boolean shouldStopAtNextCheckpoint = args[8] == null ? false : (boolean) args[8]; + + return new TransformState( + taskState, + indexerState, + transformIndexerPosition, + checkpoint, + reason, + progress, + node, + shouldStopAtNextCheckpoint + ); + }); static { PARSER.declareField(constructorArg(), p -> TransformTaskState.fromString(p.text()), TASK_STATE, ValueType.STRING); @@ -101,14 +100,16 @@ public class TransformState implements Task.Status, PersistentTaskState { PARSER.declareBoolean(optionalConstructorArg(), SHOULD_STOP_AT_NEXT_CHECKPOINT); } - public TransformState(TransformTaskState taskState, - IndexerState indexerState, - @Nullable TransformIndexerPosition position, - long checkpoint, - @Nullable String reason, - @Nullable TransformProgress progress, - @Nullable NodeAttributes node, - boolean shouldStopAtNextCheckpoint) { + public TransformState( + TransformTaskState taskState, + IndexerState indexerState, + @Nullable TransformIndexerPosition position, + long checkpoint, + @Nullable String reason, + @Nullable TransformProgress progress, + @Nullable NodeAttributes node, + boolean shouldStopAtNextCheckpoint + ) { this.taskState = taskState; this.indexerState = indexerState; this.position = position; @@ -119,22 +120,26 @@ public TransformState(TransformTaskState taskState, this.shouldStopAtNextCheckpoint = shouldStopAtNextCheckpoint; } - public TransformState(TransformTaskState taskState, - IndexerState indexerState, - @Nullable TransformIndexerPosition position, - long checkpoint, - @Nullable String reason, - @Nullable TransformProgress progress, - @Nullable NodeAttributes node) { + public TransformState( + TransformTaskState taskState, + IndexerState indexerState, + @Nullable TransformIndexerPosition position, + long checkpoint, + @Nullable String reason, + @Nullable TransformProgress progress, + @Nullable NodeAttributes node + ) { this(taskState, indexerState, position, checkpoint, reason, progress, node, false); } - public TransformState(TransformTaskState taskState, - IndexerState indexerState, - @Nullable TransformIndexerPosition position, - long checkpoint, - @Nullable String reason, - @Nullable TransformProgress progress) { + public TransformState( + TransformTaskState taskState, + IndexerState indexerState, + @Nullable TransformIndexerPosition position, + long checkpoint, + @Nullable String reason, + @Nullable TransformProgress progress + ) { this(taskState, indexerState, position, checkpoint, reason, progress, null); } @@ -267,14 +272,14 @@ public boolean equals(Object other) { TransformState that = (TransformState) other; - return Objects.equals(this.taskState, that.taskState) && - Objects.equals(this.indexerState, that.indexerState) && - Objects.equals(this.position, that.position) && - this.checkpoint == that.checkpoint && - Objects.equals(this.reason, that.reason) && - Objects.equals(this.progress, that.progress) && - Objects.equals(this.shouldStopAtNextCheckpoint, that.shouldStopAtNextCheckpoint) && - Objects.equals(this.node, that.node); + return Objects.equals(this.taskState, that.taskState) + && Objects.equals(this.indexerState, that.indexerState) + && Objects.equals(this.position, that.position) + && this.checkpoint == that.checkpoint + && Objects.equals(this.reason, that.reason) + && Objects.equals(this.progress, that.progress) + && Objects.equals(this.shouldStopAtNextCheckpoint, that.shouldStopAtNextCheckpoint) + && Objects.equals(this.node, that.node); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TransformStats.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TransformStats.java index 55dc2dc35fac6..24ef584e9db2b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TransformStats.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TransformStats.java @@ -8,15 +8,15 @@ package org.elasticsearch.xpack.core.transform.transforms; import org.elasticsearch.Version; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.core.Tuple; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TransformStoredDoc.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TransformStoredDoc.java index 2828a3c2d7ed1..efdc8ef161e19 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TransformStoredDoc.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TransformStoredDoc.java @@ -8,12 +8,12 @@ package org.elasticsearch.xpack.core.transform.transforms; import org.elasticsearch.Version; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -36,16 +36,19 @@ public class TransformStoredDoc implements Writeable, ToXContentObject { private final TransformIndexerStats transformStats; public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - NAME, true, - a -> new TransformStoredDoc((String) a[0], - (TransformState) a[1], - (TransformIndexerStats) a[2])); + NAME, + true, + a -> new TransformStoredDoc((String) a[0], (TransformState) a[1], (TransformIndexerStats) a[2]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), TransformField.ID); PARSER.declareObject(ConstructingObjectParser.constructorArg(), TransformState.PARSER::apply, STATE_FIELD); - PARSER.declareObject(ConstructingObjectParser.constructorArg(), (p, c) -> TransformIndexerStats.fromXContent(p), - TransformField.STATS_FIELD); + PARSER.declareObject( + ConstructingObjectParser.constructorArg(), + (p, c) -> TransformIndexerStats.fromXContent(p), + TransformField.STATS_FIELD + ); } public static TransformStoredDoc fromXContent(XContentParser parser) throws IOException { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TransformTaskParams.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TransformTaskParams.java index 78f0d17846d36..496ccbb67a15b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TransformTaskParams.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TransformTaskParams.java @@ -9,14 +9,14 @@ import org.elasticsearch.Version; import org.elasticsearch.cluster.AbstractDiffable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.persistent.PersistentTaskParams; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.persistent.PersistentTaskParams; import org.elasticsearch.xpack.core.transform.TransformField; import java.io.IOException; @@ -33,8 +33,11 @@ public class TransformTaskParams extends AbstractDiffable i private final TimeValue frequency; private final Boolean requiresRemote; - public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(NAME, true, - a -> new TransformTaskParams((String) a[0], (String) a[1], (String) a[2], (Boolean) a[3])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + NAME, + true, + a -> new TransformTaskParams((String) a[0], (String) a[1], (String) a[2], (Boolean) a[3]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), TransformField.ID); @@ -60,7 +63,7 @@ public TransformTaskParams(String transformId, Version version, TimeValue freque } public TransformTaskParams(StreamInput in) throws IOException { - this.transformId = in.readString(); + this.transformId = in.readString(); if (in.getVersion().onOrAfter(Version.V_7_3_0)) { this.version = Version.readVersion(in); } else { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TransformTaskState.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TransformTaskState.java index 15b59009de6d4..a3990011ba775 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TransformTaskState.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TransformTaskState.java @@ -15,7 +15,9 @@ import java.util.Locale; public enum TransformTaskState implements Writeable { - STOPPED, STARTED, FAILED; + STOPPED, + STARTED, + FAILED; public static TransformTaskState fromString(String name) { return valueOf(name.trim().toUpperCase(Locale.ROOT)); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/latest/LatestConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/latest/LatestConfig.java index ee18b5578b1e3..9f27cf4f9012a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/latest/LatestConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/latest/LatestConfig.java @@ -11,15 +11,15 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.search.sort.SortBuilder; +import org.elasticsearch.search.sort.SortBuilders; +import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.search.sort.SortBuilder; -import org.elasticsearch.search.sort.SortBuilders; -import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.xpack.core.deprecation.DeprecationIssue; import org.elasticsearch.xpack.core.transform.utils.ExceptionsHelper; @@ -49,8 +49,11 @@ public class LatestConfig implements Writeable, ToXContentObject { @SuppressWarnings("unchecked") private static ConstructingObjectParser createParser(boolean lenient) { - ConstructingObjectParser parser = - new ConstructingObjectParser<>(NAME, lenient, args -> new LatestConfig((List) args[0], (String) args[1])); + ConstructingObjectParser parser = new ConstructingObjectParser<>( + NAME, + lenient, + args -> new LatestConfig((List) args[0], (String) args[1]) + ); parser.declareStringArray(constructorArg(), UNIQUE_KEY); parser.declareString(constructorArg(), SORT); @@ -91,13 +94,12 @@ public ActionRequestValidationException validate(ActionRequestValidationExceptio Set uniqueKeyElements = new HashSet<>(); for (int i = 0; i < uniqueKey.size(); ++i) { if (uniqueKey.get(i).isEmpty()) { - validationException = - addValidationError("latest.unique_key[" + i + "] element must be non-empty", validationException); + validationException = addValidationError("latest.unique_key[" + i + "] element must be non-empty", validationException); } else if (uniqueKeyElements.contains(uniqueKey.get(i))) { - validationException = - addValidationError( - "latest.unique_key elements must be unique, found duplicate element [" + uniqueKey.get(i) + "]", - validationException); + validationException = addValidationError( + "latest.unique_key elements must be unique, found duplicate element [" + uniqueKey.get(i) + "]", + validationException + ); } else { uniqueKeyElements.add(uniqueKey.get(i)); } @@ -111,8 +113,7 @@ public ActionRequestValidationException validate(ActionRequestValidationExceptio return validationException; } - public void checkForDeprecations(String id, NamedXContentRegistry namedXContentRegistry, Consumer onDeprecation) { - } + public void checkForDeprecations(String id, NamedXContentRegistry namedXContentRegistry, Consumer onDeprecation) {} @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/pivot/AggregationConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/pivot/AggregationConfig.java index 6026341a85fbb..148cd6917b6f0 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/pivot/AggregationConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/pivot/AggregationConfig.java @@ -14,17 +14,17 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.search.aggregations.AggregationBuilder; +import org.elasticsearch.search.aggregations.AggregatorFactories; +import org.elasticsearch.search.aggregations.PipelineAggregationBuilder; +import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.search.aggregations.AggregationBuilder; -import org.elasticsearch.search.aggregations.AggregatorFactories; -import org.elasticsearch.search.aggregations.PipelineAggregationBuilder; import org.elasticsearch.xpack.core.deprecation.DeprecationIssue; import org.elasticsearch.xpack.core.deprecation.DeprecationIssue.Level; import org.elasticsearch.xpack.core.deprecation.LoggingDeprecationAccumulationHandler; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/pivot/DateHistogramGroupSource.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/pivot/DateHistogramGroupSource.java index 1db3fb562a031..b4876840e2d91 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/pivot/DateHistogramGroupSource.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/pivot/DateHistogramGroupSource.java @@ -7,19 +7,19 @@ package org.elasticsearch.xpack.core.transform.transforms.pivot; import org.elasticsearch.Version; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Rounding; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder; +import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentFragment; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder; -import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; import java.io.IOException; import java.time.ZoneId; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/pivot/GeoTileGroupSource.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/pivot/GeoTileGroupSource.java index 21716567856cd..ad488e48485ae 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/pivot/GeoTileGroupSource.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/pivot/GeoTileGroupSource.java @@ -7,16 +7,16 @@ package org.elasticsearch.xpack.core.transform.transforms.pivot; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.geo.GeoBoundingBox; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.index.mapper.GeoShapeFieldMapper; +import org.elasticsearch.search.aggregations.bucket.geogrid.GeoTileUtils; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.index.mapper.GeoShapeFieldMapper; -import org.elasticsearch.search.aggregations.bucket.geogrid.GeoTileUtils; import java.io.IOException; import java.util.Objects; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/pivot/GroupConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/pivot/GroupConfig.java index 91a30e88cf9a3..461741b72d8a3 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/pivot/GroupConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/pivot/GroupConfig.java @@ -16,13 +16,13 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.search.aggregations.AggregatorFactories; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.search.aggregations.AggregatorFactories; import org.elasticsearch.xpack.core.deprecation.DeprecationIssue; import org.elasticsearch.xpack.core.transform.TransformField; import org.elasticsearch.xpack.core.transform.TransformMessages; @@ -94,8 +94,7 @@ public ActionRequestValidationException validate(ActionRequestValidationExceptio return validationException; } - public void checkForDeprecations(String id, NamedXContentRegistry namedXContentRegistry, Consumer onDeprecation) { - } + public void checkForDeprecations(String id, NamedXContentRegistry namedXContentRegistry, Consumer onDeprecation) {} @Override public void writeTo(StreamOutput out) throws IOException { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/pivot/HistogramGroupSource.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/pivot/HistogramGroupSource.java index 70a5954c02abc..717ba9af098a9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/pivot/HistogramGroupSource.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/pivot/HistogramGroupSource.java @@ -6,10 +6,10 @@ */ package org.elasticsearch.xpack.core.transform.transforms.pivot; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/pivot/ScriptConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/pivot/ScriptConfig.java index e5412a19bfbdf..cec14ea912e1e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/pivot/ScriptConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/pivot/ScriptConfig.java @@ -16,13 +16,13 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.script.Script; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.script.Script; import org.elasticsearch.xpack.core.transform.TransformMessages; import java.io.IOException; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/pivot/SingleGroupSource.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/pivot/SingleGroupSource.java index a291bc421a0fa..6864fb89c242f 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/pivot/SingleGroupSource.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/pivot/SingleGroupSource.java @@ -9,13 +9,13 @@ import org.elasticsearch.Version; import org.elasticsearch.action.ActionRequestValidationException; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.AbstractObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -108,8 +108,10 @@ public SingleGroupSource(StreamInput in) throws IOException { ActionRequestValidationException validate(ActionRequestValidationException validationException) { // either a script or a field must be declared if (field == null && scriptConfig == null) { - validationException = - addValidationError("Required one of fields [field, script], but none were specified.", validationException); + validationException = addValidationError( + "Required one of fields [field, script], but none were specified.", + validationException + ); } return validationException; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/utils/TransformStrings.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/utils/TransformStrings.java index 94ef2f6375300..c5233e8678a3f 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/utils/TransformStrings.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/utils/TransformStrings.java @@ -25,8 +25,7 @@ public final class TransformStrings { public static final int ID_LENGTH_LIMIT = 64; - private TransformStrings() { - } + private TransformStrings() {} public static boolean isValidId(String id) { return id != null && VALID_ID_CHAR_PATTERN.matcher(id).matches() && Metadata.ALL.equals(id) == false; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/WatcherField.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/WatcherField.java index 0b3270b91fc26..74e7d456c77af 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/WatcherField.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/WatcherField.java @@ -15,12 +15,14 @@ public final class WatcherField { - public static final Setting ENCRYPTION_KEY_SETTING = - SecureSetting.secureFile("xpack.watcher.encryption_key", null); + public static final Setting ENCRYPTION_KEY_SETTING = SecureSetting.secureFile("xpack.watcher.encryption_key", null); public static final String EMAIL_NOTIFICATION_SSL_PREFIX = "xpack.notification.email.ssl."; - public static final LicensedFeature.Momentary WATCHER_FEATURE = - LicensedFeature.momentary(null, "watcher", License.OperationMode.STANDARD); + public static final LicensedFeature.Momentary WATCHER_FEATURE = LicensedFeature.momentary( + null, + "watcher", + License.OperationMode.STANDARD + ); private WatcherField() {} } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/WatcherMetadata.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/WatcherMetadata.java index 2179cbafdfff2..ea3e3b4df25bb 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/WatcherMetadata.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/WatcherMetadata.java @@ -10,9 +10,9 @@ import org.elasticsearch.cluster.AbstractNamedDiffable; import org.elasticsearch.cluster.NamedDiff; import org.elasticsearch.cluster.metadata.Metadata; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -64,7 +64,7 @@ public void writeTo(StreamOutput streamOutput) throws IOException { @Override public String toString() { - return "manuallyStopped["+ manuallyStopped +"]"; + return "manuallyStopped[" + manuallyStopped + "]"; } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/WatcherState.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/WatcherState.java index b764120dcd884..403f1cb3b2494 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/WatcherState.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/WatcherState.java @@ -49,7 +49,7 @@ public static WatcherState fromId(byte id) { return STARTING; case 2: return STARTED; - default: //3 + default: // 3 assert id == 3 : "unknown watcher state id [" + id + "]"; return STOPPING; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/actions/Action.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/actions/Action.java index 2560bfedd1518..3d3508def0d00 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/actions/Action.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/actions/Action.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.watcher.actions; import org.elasticsearch.ElasticsearchException; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.logging.LoggerMessageFormat; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentFragment; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/actions/ActionFactory.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/actions/ActionFactory.java index bd51d327e0e66..d99e7438c6fba 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/actions/ActionFactory.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/actions/ActionFactory.java @@ -26,5 +26,5 @@ protected ActionFactory(Logger actionLogger) { * Parses the given xcontent and creates a concrete action */ public abstract ExecutableAction parseExecutable(String watchId, String actionId, XContentParser parser) - throws IOException; + throws IOException; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/actions/ActionRegistry.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/actions/ActionRegistry.java index 6216827a594ac..8a02a225206fd 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/actions/ActionRegistry.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/actions/ActionRegistry.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.watcher.actions; import org.elasticsearch.ElasticsearchParseException; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.license.XPackLicenseState; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.watcher.condition.ConditionRegistry; import org.elasticsearch.xpack.core.watcher.support.WatcherUtils; import org.elasticsearch.xpack.core.watcher.transform.TransformRegistry; @@ -27,10 +27,13 @@ public class ActionRegistry { private final Clock clock; private final XPackLicenseState licenseState; - public ActionRegistry(Map parsers, - ConditionRegistry conditionRegistry, TransformRegistry transformRegistry, - Clock clock, - XPackLicenseState licenseState) { + public ActionRegistry( + Map parsers, + ConditionRegistry conditionRegistry, + TransformRegistry transformRegistry, + Clock clock, + XPackLicenseState licenseState + ) { this.parsers = parsers; this.conditionRegistry = conditionRegistry; this.transformRegistry = transformRegistry; @@ -44,8 +47,11 @@ ActionFactory factory(String type) { public List parseActions(String watchId, XContentParser parser) throws IOException { if (parser.currentToken() != XContentParser.Token.START_OBJECT) { - throw new ElasticsearchParseException("could not parse actions for watch [{}]. expected an object but found [{}] instead", - watchId, parser.currentToken()); + throw new ElasticsearchParseException( + "could not parse actions for watch [{}]. expected an object but found [{}] instead", + watchId, + parser.currentToken() + ); } List actions = new ArrayList<>(); @@ -55,8 +61,11 @@ public List parseActions(String watchId, XContentParser parser) t if (token == XContentParser.Token.FIELD_NAME) { id = parser.currentName(); if (WatcherUtils.isValidId(id) == false) { - throw new ElasticsearchParseException("could not parse action [{}] for watch [{}]. id contains whitespace", id, - watchId); + throw new ElasticsearchParseException( + "could not parse action [{}] for watch [{}]. id contains whitespace", + id, + watchId + ); } } else if (token == XContentParser.Token.START_OBJECT && id != null) { actions.add(ActionWrapper.parse(watchId, id, parser, this, clock, licenseState)); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/actions/ActionStatus.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/actions/ActionStatus.java index 8219b833ad718..8397cb7c6d337 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/actions/ActionStatus.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/actions/ActionStatus.java @@ -7,11 +7,11 @@ package org.elasticsearch.xpack.core.watcher.actions; import org.elasticsearch.ElasticsearchParseException; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.time.DateFormatters; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -29,16 +29,23 @@ public class ActionStatus implements ToXContentObject { private AckStatus ackStatus; - @Nullable private Execution lastExecution; - @Nullable private Execution lastSuccessfulExecution; - @Nullable private Throttle lastThrottle; + @Nullable + private Execution lastExecution; + @Nullable + private Execution lastSuccessfulExecution; + @Nullable + private Throttle lastThrottle; public ActionStatus(ZonedDateTime now) { this(new AckStatus(now, AckStatus.State.AWAITS_SUCCESSFUL_EXECUTION), null, null, null); } - public ActionStatus(AckStatus ackStatus, @Nullable Execution lastExecution, @Nullable Execution lastSuccessfulExecution, - @Nullable Throttle lastThrottle) { + public ActionStatus( + AckStatus ackStatus, + @Nullable Execution lastExecution, + @Nullable Execution lastSuccessfulExecution, + @Nullable Throttle lastThrottle + ) { this.ackStatus = ackStatus; this.lastExecution = lastExecution; this.lastSuccessfulExecution = lastSuccessfulExecution; @@ -68,10 +75,10 @@ public boolean equals(Object o) { ActionStatus that = (ActionStatus) o; - return Objects.equals(ackStatus, that.ackStatus) && - Objects.equals(lastExecution, that.lastExecution) && - Objects.equals(lastSuccessfulExecution, that.lastSuccessfulExecution) && - Objects.equals(lastThrottle, that.lastThrottle); + return Objects.equals(ackStatus, that.ackStatus) + && Objects.equals(lastExecution, that.lastExecution) + && Objects.equals(lastSuccessfulExecution, that.lastSuccessfulExecution) + && Objects.equals(lastThrottle, that.lastThrottle); } @Override @@ -178,13 +185,21 @@ public static ActionStatus parse(String watchId, String actionId, XContentParser } else if (Field.LAST_THROTTLE.match(currentFieldName, parser.getDeprecationHandler())) { lastThrottle = Throttle.parse(watchId, actionId, parser); } else { - throw new ElasticsearchParseException("could not parse action status for [{}/{}]. unexpected field [{}]", watchId, - actionId, currentFieldName); + throw new ElasticsearchParseException( + "could not parse action status for [{}/{}]. unexpected field [{}]", + watchId, + actionId, + currentFieldName + ); } } if (ackStatus == null) { - throw new ElasticsearchParseException("could not parse action status for [{}/{}]. missing required field [{}]", watchId, - actionId, Field.ACK_STATUS.getPreferredName()); + throw new ElasticsearchParseException( + "could not parse action status for [{}/{}]. missing required field [{}]", + watchId, + actionId, + Field.ACK_STATUS.getPreferredName() + ); } return new ActionStatus(ackStatus, lastExecution, lastSuccessfulExecution, lastThrottle); } @@ -204,9 +219,12 @@ public enum State { static State resolve(byte value) { switch (value) { - case 1 : return AWAITS_SUCCESSFUL_EXECUTION; - case 2 : return ACKABLE; - case 3 : return ACKED; + case 1: + return AWAITS_SUCCESSFUL_EXECUTION; + case 2: + return ACKABLE; + case 3: + return ACKED; default: throw illegalArgument("unknown action ack status state value [{}]", value); } @@ -236,7 +254,7 @@ public boolean equals(Object o) { AckStatus ackStatus = (AckStatus) o; - return Objects.equals(timestamp, ackStatus.timestamp) && Objects.equals(state, ackStatus.state); + return Objects.equals(timestamp, ackStatus.timestamp) && Objects.equals(state, ackStatus.state); } @Override @@ -247,9 +265,10 @@ public int hashCode() { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { return builder.startObject() - .field(Field.TIMESTAMP.getPreferredName()).value(dateTimeFormatter.format(timestamp)) - .field(Field.ACK_STATUS_STATE.getPreferredName(), state.name().toLowerCase(Locale.ROOT)) - .endObject(); + .field(Field.TIMESTAMP.getPreferredName()) + .value(dateTimeFormatter.format(timestamp)) + .field(Field.ACK_STATUS_STATE.getPreferredName(), state.name().toLowerCase(Locale.ROOT)) + .endObject(); } public static AckStatus parse(String watchId, String actionId, XContentParser parser) throws IOException { @@ -266,17 +285,32 @@ public static AckStatus parse(String watchId, String actionId, XContentParser pa } else if (Field.ACK_STATUS_STATE.match(currentFieldName, parser.getDeprecationHandler())) { state = State.valueOf(parser.text().toUpperCase(Locale.ROOT)); } else { - throw new ElasticsearchParseException("could not parse action status for [{}/{}]. unexpected field [{}.{}]", watchId, - actionId, Field.ACK_STATUS.getPreferredName(), currentFieldName); + throw new ElasticsearchParseException( + "could not parse action status for [{}/{}]. unexpected field [{}.{}]", + watchId, + actionId, + Field.ACK_STATUS.getPreferredName(), + currentFieldName + ); } } if (timestamp == null) { - throw new ElasticsearchParseException("could not parse action status for [{}/{}]. missing required field [{}.{}]", - watchId, actionId, Field.ACK_STATUS.getPreferredName(), Field.TIMESTAMP.getPreferredName()); + throw new ElasticsearchParseException( + "could not parse action status for [{}/{}]. missing required field [{}.{}]", + watchId, + actionId, + Field.ACK_STATUS.getPreferredName(), + Field.TIMESTAMP.getPreferredName() + ); } if (state == null) { - throw new ElasticsearchParseException("could not parse action status for [{}/{}]. missing required field [{}.{}]", - watchId, actionId, Field.ACK_STATUS.getPreferredName(), Field.ACK_STATUS_STATE.getPreferredName()); + throw new ElasticsearchParseException( + "could not parse action status for [{}/{}]. missing required field [{}.{}]", + watchId, + actionId, + Field.ACK_STATUS.getPreferredName(), + Field.ACK_STATUS_STATE.getPreferredName() + ); } return new AckStatus(timestamp, state); } @@ -332,9 +366,9 @@ public boolean equals(Object o) { Execution execution = (Execution) o; - return Objects.equals(successful, execution.successful) && - Objects.equals(timestamp, execution.timestamp) && - Objects.equals(reason, execution.reason); + return Objects.equals(successful, execution.successful) + && Objects.equals(timestamp, execution.timestamp) + && Objects.equals(reason, execution.reason); } @Override @@ -370,24 +404,44 @@ public static Execution parse(String watchId, String actionId, XContentParser pa } else if (Field.REASON.match(currentFieldName, parser.getDeprecationHandler())) { reason = parser.text(); } else { - throw new ElasticsearchParseException("could not parse action status for [{}/{}]. unexpected field [{}.{}]", watchId, - actionId, Field.LAST_EXECUTION.getPreferredName(), currentFieldName); + throw new ElasticsearchParseException( + "could not parse action status for [{}/{}]. unexpected field [{}.{}]", + watchId, + actionId, + Field.LAST_EXECUTION.getPreferredName(), + currentFieldName + ); } } if (timestamp == null) { - throw new ElasticsearchParseException("could not parse action status for [{}/{}]. missing required field [{}.{}]", - watchId, actionId, Field.LAST_EXECUTION.getPreferredName(), Field.TIMESTAMP.getPreferredName()); + throw new ElasticsearchParseException( + "could not parse action status for [{}/{}]. missing required field [{}.{}]", + watchId, + actionId, + Field.LAST_EXECUTION.getPreferredName(), + Field.TIMESTAMP.getPreferredName() + ); } if (successful == null) { - throw new ElasticsearchParseException("could not parse action status for [{}/{}]. missing required field [{}.{}]", - watchId, actionId, Field.LAST_EXECUTION.getPreferredName(), Field.EXECUTION_SUCCESSFUL.getPreferredName()); + throw new ElasticsearchParseException( + "could not parse action status for [{}/{}]. missing required field [{}.{}]", + watchId, + actionId, + Field.LAST_EXECUTION.getPreferredName(), + Field.EXECUTION_SUCCESSFUL.getPreferredName() + ); } if (successful) { return successful(timestamp); } if (reason == null) { - throw new ElasticsearchParseException("could not parse action status for [{}/{}]. missing required field for unsuccessful" + - " execution [{}.{}]", watchId, actionId, Field.LAST_EXECUTION.getPreferredName(), Field.REASON.getPreferredName()); + throw new ElasticsearchParseException( + "could not parse action status for [{}/{}]. missing required field for unsuccessful" + " execution [{}.{}]", + watchId, + actionId, + Field.LAST_EXECUTION.getPreferredName(), + Field.REASON.getPreferredName() + ); } return failure(timestamp, reason); } @@ -445,9 +499,10 @@ public int hashCode() { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { return builder.startObject() - .field(Field.TIMESTAMP.getPreferredName()).value(dateTimeFormatter.format(timestamp)) - .field(Field.REASON.getPreferredName(), reason) - .endObject(); + .field(Field.TIMESTAMP.getPreferredName()) + .value(dateTimeFormatter.format(timestamp)) + .field(Field.REASON.getPreferredName(), reason) + .endObject(); } public static Throttle parse(String watchId, String actionId, XContentParser parser) throws IOException { @@ -464,17 +519,32 @@ public static Throttle parse(String watchId, String actionId, XContentParser par } else if (Field.REASON.match(currentFieldName, parser.getDeprecationHandler())) { reason = parser.text(); } else { - throw new ElasticsearchParseException("could not parse action status for [{}/{}]. unexpected field [{}.{}]", watchId, - actionId, Field.LAST_THROTTLE.getPreferredName(), currentFieldName); + throw new ElasticsearchParseException( + "could not parse action status for [{}/{}]. unexpected field [{}.{}]", + watchId, + actionId, + Field.LAST_THROTTLE.getPreferredName(), + currentFieldName + ); } } if (timestamp == null) { - throw new ElasticsearchParseException("could not parse action status for [{}/{}]. missing required field [{}.{}]", - watchId, actionId, Field.LAST_THROTTLE.getPreferredName(), Field.TIMESTAMP.getPreferredName()); + throw new ElasticsearchParseException( + "could not parse action status for [{}/{}]. missing required field [{}.{}]", + watchId, + actionId, + Field.LAST_THROTTLE.getPreferredName(), + Field.TIMESTAMP.getPreferredName() + ); } if (reason == null) { - throw new ElasticsearchParseException("could not parse action status for [{}/{}]. missing required field [{}.{}]", - watchId, actionId, Field.LAST_THROTTLE.getPreferredName(), Field.REASON.getPreferredName()); + throw new ElasticsearchParseException( + "could not parse action status for [{}/{}]. missing required field [{}.{}]", + watchId, + actionId, + Field.LAST_THROTTLE.getPreferredName(), + Field.REASON.getPreferredName() + ); } return new Throttle(timestamp, reason); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/actions/ActionWrapper.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/actions/ActionWrapper.java index b560e1ba4e94b..18033f6ce2422 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/actions/ActionWrapper.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/actions/ActionWrapper.java @@ -10,14 +10,14 @@ import org.apache.logging.log4j.util.Supplier; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchParseException; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.xcontent.ObjectPath; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.xpack.core.watcher.actions.throttler.ActionThrottler; import org.elasticsearch.xpack.core.watcher.actions.throttler.Throttler; import org.elasticsearch.xpack.core.watcher.actions.throttler.ThrottlerField; @@ -58,12 +58,15 @@ public class ActionWrapper implements ToXContentObject { private String path; private final Integer maxIterations; - public ActionWrapper(String id, ActionThrottler throttler, - @Nullable ExecutableCondition condition, - @Nullable ExecutableTransform transform, - ExecutableAction action, - @Nullable String path, - @Nullable Integer maxIterations) { + public ActionWrapper( + String id, + ActionThrottler throttler, + @Nullable ExecutableCondition condition, + @Nullable ExecutableTransform transform, + ExecutableAction action, + @Nullable String path, + @Nullable Integer maxIterations + ) { this.id = id; this.condition = condition; this.throttler = throttler; @@ -129,15 +132,27 @@ public ActionWrapperResult execute(WatchExecutionContext ctx) { conditionResult = condition.execute(ctx); if (conditionResult.met() == false) { ctx.watch().status().actionStatus(id).resetAckStatus(ZonedDateTime.now(ZoneOffset.UTC)); - return new ActionWrapperResult(id, conditionResult, null, - new Action.Result.ConditionFailed(action.type(), "condition not met. skipping")); + return new ActionWrapperResult( + id, + conditionResult, + null, + new Action.Result.ConditionFailed(action.type(), "condition not met. skipping") + ); } } catch (RuntimeException e) { - action.logger().error( + action.logger() + .error( (Supplier) () -> new ParameterizedMessage( - "failed to execute action [{}/{}]. failed to execute condition", ctx.watch().id(), id), e); - return new ActionWrapperResult(id, new Action.Result.ConditionFailed(action.type(), - "condition failed. skipping: {}", e.getMessage())); + "failed to execute action [{}/{}]. failed to execute condition", + ctx.watch().id(), + id + ), + e + ); + return new ActionWrapperResult( + id, + new Action.Result.ConditionFailed(action.type(), "condition failed. skipping: {}", e.getMessage()) + ); } } Payload payload = ctx.payload(); @@ -146,16 +161,27 @@ public ActionWrapperResult execute(WatchExecutionContext ctx) { try { transformResult = transform.execute(ctx, payload); if (transformResult.status() == Transform.Result.Status.FAILURE) { - action.logger().error("failed to execute action [{}/{}]. failed to transform payload. {}", ctx.watch().id(), id, - transformResult.reason()); + action.logger() + .error( + "failed to execute action [{}/{}]. failed to transform payload. {}", + ctx.watch().id(), + id, + transformResult.reason() + ); String msg = "Failed to transform payload"; return new ActionWrapperResult(id, conditionResult, transformResult, new Action.Result.Failure(action.type(), msg)); } payload = transformResult.payload(); } catch (Exception e) { - action.logger().error( + action.logger() + .error( (Supplier) () -> new ParameterizedMessage( - "failed to execute action [{}/{}]. failed to transform payload.", ctx.watch().id(), id), e); + "failed to execute action [{}/{}]. failed to transform payload.", + ctx.watch().id(), + id + ), + e + ); return new ActionWrapperResult(id, conditionResult, null, new Action.Result.FailureWithException(action.type(), e)); } } @@ -164,8 +190,8 @@ public ActionWrapperResult execute(WatchExecutionContext ctx) { Action.Result actionResult = action.execute(id, ctx, payload); return new ActionWrapperResult(id, conditionResult, transformResult, actionResult); } catch (Exception e) { - action.logger().error( - (Supplier) () -> new ParameterizedMessage("failed to execute action [{}/{}]", ctx.watch().id(), id), e); + action.logger() + .error((Supplier) () -> new ParameterizedMessage("failed to execute action [{}/{}]", ctx.watch().id(), id), e); return new ActionWrapperResult(id, new Action.Result.FailureWithException(action.type(), e)); } } else { @@ -206,25 +232,24 @@ public ActionWrapperResult execute(WatchExecutionContext ctx) { } final int numberOfActionsExecuted = runs; - return new ActionWrapperResult(id, conditionResult, transformResult, - new Action.Result(action.type(), status) { - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.field("number_of_actions_executed", numberOfActionsExecuted); - builder.startArray(WatchField.FOREACH.getPreferredName()); - for (Action.Result result : results) { - builder.startObject(); - result.toXContent(builder, params); - builder.endObject(); - } - builder.endArray(); - builder.field(WatchField.MAX_ITERATIONS.getPreferredName(), maxIterations); - return builder; + return new ActionWrapperResult(id, conditionResult, transformResult, new Action.Result(action.type(), status) { + @Override + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + builder.field("number_of_actions_executed", numberOfActionsExecuted); + builder.startArray(WatchField.FOREACH.getPreferredName()); + for (Action.Result result : results) { + builder.startObject(); + result.toXContent(builder, params); + builder.endObject(); } + builder.endArray(); + builder.field(WatchField.MAX_ITERATIONS.getPreferredName(), maxIterations); + return builder; + } }); } catch (Exception e) { - action.logger().error( - (Supplier) () -> new ParameterizedMessage("failed to execute action [{}/{}]", ctx.watch().id(), id), e); + action.logger() + .error((Supplier) () -> new ParameterizedMessage("failed to execute action [{}/{}]", ctx.watch().id(), id), e); return new ActionWrapperResult(id, new Action.Result.FailureWithException(action.type(), e)); } } @@ -251,10 +276,10 @@ public boolean equals(Object o) { ActionWrapper that = (ActionWrapper) o; - return Objects.equals(id, that.id) && - Objects.equals(condition, that.condition) && - Objects.equals(transform, that.transform) && - Objects.equals(action, that.action); + return Objects.equals(id, that.id) + && Objects.equals(condition, that.condition) + && Objects.equals(transform, that.transform) + && Objects.equals(action, that.action); } @Override @@ -267,18 +292,17 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.startObject(); TimeValue throttlePeriod = throttler.throttlePeriod(); if (throttlePeriod != null) { - builder.humanReadableField(ThrottlerField.THROTTLE_PERIOD.getPreferredName(), - ThrottlerField.THROTTLE_PERIOD_HUMAN.getPreferredName(), throttlePeriod); + builder.humanReadableField( + ThrottlerField.THROTTLE_PERIOD.getPreferredName(), + ThrottlerField.THROTTLE_PERIOD_HUMAN.getPreferredName(), + throttlePeriod + ); } if (condition != null) { - builder.startObject(WatchField.CONDITION.getPreferredName()) - .field(condition.type(), condition, params) - .endObject(); + builder.startObject(WatchField.CONDITION.getPreferredName()).field(condition.type(), condition, params).endObject(); } if (transform != null) { - builder.startObject(Transform.TRANSFORM.getPreferredName()) - .field(transform.type(), transform, params) - .endObject(); + builder.startObject(Transform.TRANSFORM.getPreferredName()).field(transform.type(), transform, params).endObject(); } if (Strings.isEmpty(path) == false) { builder.field(WatchField.FOREACH.getPreferredName(), path); @@ -289,8 +313,14 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws return builder.endObject(); } - static ActionWrapper parse(String watchId, String actionId, XContentParser parser, ActionRegistry actionRegistry, Clock clock, - XPackLicenseState licenseState) throws IOException { + static ActionWrapper parse( + String watchId, + String actionId, + XContentParser parser, + ActionRegistry actionRegistry, + Clock clock, + XPackLicenseState licenseState + ) throws IOException { assert parser.currentToken() == XContentParser.Token.START_OBJECT; @@ -319,8 +349,13 @@ static ActionWrapper parse(String watchId, String actionId, XContentParser parse try { throttlePeriod = WatcherDateTimeUtils.parseTimeValue(parser, ThrottlerField.THROTTLE_PERIOD_HUMAN.toString()); } catch (ElasticsearchParseException pe) { - throw new ElasticsearchParseException("could not parse action [{}/{}]. failed to parse field [{}] as time value", - pe, watchId, actionId, currentFieldName); + throw new ElasticsearchParseException( + "could not parse action [{}/{}]. failed to parse field [{}] as time value", + pe, + watchId, + actionId, + currentFieldName + ); } } else if (WatchField.MAX_ITERATIONS.match(currentFieldName, parser.getDeprecationHandler())) { maxIterations = parser.intValue(); @@ -328,8 +363,12 @@ static ActionWrapper parse(String watchId, String actionId, XContentParser parse // it's the type of the action ActionFactory actionFactory = actionRegistry.factory(currentFieldName); if (actionFactory == null) { - throw new ElasticsearchParseException("could not parse action [{}/{}]. unknown action type [{}]", watchId, - actionId, currentFieldName); + throw new ElasticsearchParseException( + "could not parse action [{}/{}]. unknown action type [{}]", + watchId, + actionId, + currentFieldName + ); } action = actionFactory.parseExecutable(watchId, actionId, parser); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/actions/ActionWrapperResult.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/actions/ActionWrapperResult.java index ef19a423aa7ec..ddfc31e492e79 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/actions/ActionWrapperResult.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/actions/ActionWrapperResult.java @@ -29,8 +29,12 @@ public ActionWrapperResult(String id, Action.Result action) { this(id, null, null, action); } - public ActionWrapperResult(String id, @Nullable Condition.Result condition, @Nullable Transform.Result transform, - Action.Result action) { + public ActionWrapperResult( + String id, + @Nullable Condition.Result condition, + @Nullable Transform.Result transform, + Action.Result action + ) { this.id = id; this.condition = condition; this.transform = transform; @@ -60,10 +64,10 @@ public boolean equals(Object o) { ActionWrapperResult result = (ActionWrapperResult) o; - return Objects.equals(id, result.id) && - Objects.equals(condition, result.condition) && - Objects.equals(transform, result.transform) && - Objects.equals(action, result.action); + return Objects.equals(id, result.id) + && Objects.equals(condition, result.condition) + && Objects.equals(transform, result.transform) + && Objects.equals(action, result.action); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/actions/throttler/PeriodThrottler.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/actions/throttler/PeriodThrottler.java index 2f87a1f3941a3..efc7274b36f41 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/actions/throttler/PeriodThrottler.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/actions/throttler/PeriodThrottler.java @@ -21,7 +21,8 @@ */ public class PeriodThrottler implements Throttler { - @Nullable private final TimeValue period; + @Nullable + private final TimeValue period; private final Clock clock; public PeriodThrottler(Clock clock, TimeValue period) { @@ -52,8 +53,12 @@ public Result throttle(String actionId, WatchExecutionContext ctx) { long executionTime = status.lastSuccessfulExecution().timestamp().toInstant().toEpochMilli(); TimeValue timeElapsed = TimeValue.timeValueMillis(now - executionTime); if (timeElapsed.getMillis() <= period.getMillis()) { - return Result.throttle(PERIOD, "throttling interval is set to [{}] but time elapsed since last execution is [{}]", - period, timeElapsed); + return Result.throttle( + PERIOD, + "throttling interval is set to [{}] but time elapsed since last execution is [{}]", + period, + timeElapsed + ); } return Result.NO; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/client/WatchSourceBuilder.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/client/WatchSourceBuilder.java index 9030660258c65..e21a490b45f13 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/client/WatchSourceBuilder.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/client/WatchSourceBuilder.java @@ -7,13 +7,13 @@ package org.elasticsearch.xpack.core.watcher.client; import org.elasticsearch.ElasticsearchException; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.watcher.actions.Action; import org.elasticsearch.xpack.core.watcher.actions.throttler.ThrottlerField; @@ -89,8 +89,11 @@ public WatchSourceBuilder addAction(String id, TimeValue throttlePeriod, Action. return addAction(id, throttlePeriod, null, action.build()); } - public WatchSourceBuilder addAction(String id, Transform.Builder transform, - Action.Builder action) { + public WatchSourceBuilder addAction( + String id, + Transform.Builder transform, + Action.Builder action + ) { return addAction(id, null, transform.build(), action.build()); } @@ -98,8 +101,12 @@ public WatchSourceBuilder addAction(String id, Condition condition, Action.Build return addAction(id, null, condition, null, action.build()); } - public WatchSourceBuilder addAction(String id, TimeValue throttlePeriod, Transform.Builder transform, - Action.Builder action) { + public WatchSourceBuilder addAction( + String id, + TimeValue throttlePeriod, + Transform.Builder transform, + Action.Builder action + ) { return addAction(id, throttlePeriod, transform.build(), action.build()); } @@ -108,8 +115,13 @@ public WatchSourceBuilder addAction(String id, TimeValue throttlePeriod, Transfo return this; } - public WatchSourceBuilder addAction(String id, TimeValue throttlePeriod, Condition condition, - Transform.Builder transform, Action.Builder action) { + public WatchSourceBuilder addAction( + String id, + TimeValue throttlePeriod, + Condition condition, + Transform.Builder transform, + Action.Builder action + ) { return addAction(id, throttlePeriod, condition, transform.build(), action.build()); } @@ -118,8 +130,14 @@ public WatchSourceBuilder addAction(String id, TimeValue throttlePeriod, Conditi return this; } - public WatchSourceBuilder addAction(String id, TimeValue throttlePeriod, Condition condition, Transform transform, String path, - Action action) { + public WatchSourceBuilder addAction( + String id, + TimeValue throttlePeriod, + Condition condition, + Transform transform, + String path, + Action action + ) { actions.put(id, new TransformedAction(id, action, throttlePeriod, condition, transform, path)); return this; } @@ -142,27 +160,22 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws if (trigger == null) { throw Exceptions.illegalState("failed to build watch source. no trigger defined"); } - builder.startObject(WatchField.TRIGGER.getPreferredName()) - .field(trigger.type(), trigger, params) - .endObject(); + builder.startObject(WatchField.TRIGGER.getPreferredName()).field(trigger.type(), trigger, params).endObject(); - builder.startObject(WatchField.INPUT.getPreferredName()) - .field(input.type(), input, params) - .endObject(); + builder.startObject(WatchField.INPUT.getPreferredName()).field(input.type(), input, params).endObject(); - builder.startObject(WatchField.CONDITION.getPreferredName()) - .field(condition.type(), condition, params) - .endObject(); + builder.startObject(WatchField.CONDITION.getPreferredName()).field(condition.type(), condition, params).endObject(); if (transform != null) { - builder.startObject(WatchField.TRANSFORM.getPreferredName()) - .field(transform.type(), transform, params) - .endObject(); + builder.startObject(WatchField.TRANSFORM.getPreferredName()).field(transform.type(), transform, params).endObject(); } if (defaultThrottlePeriod != null) { - builder.humanReadableField(WatchField.THROTTLE_PERIOD.getPreferredName(), - WatchField.THROTTLE_PERIOD_HUMAN.getPreferredName(), defaultThrottlePeriod); + builder.humanReadableField( + WatchField.THROTTLE_PERIOD.getPreferredName(), + WatchField.THROTTLE_PERIOD_HUMAN.getPreferredName(), + defaultThrottlePeriod + ); } builder.startObject(WatchField.ACTIONS.getPreferredName()); @@ -186,7 +199,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws public final BytesReference buildAsBytes(XContentType contentType) { try { WatcherParams params = WatcherParams.builder().hideSecrets(false).build(); - return XContentHelper.toXContent(this, contentType, params,false); + return XContentHelper.toXContent(this, contentType, params, false); } catch (Exception e) { throw new ElasticsearchException("Failed to build ToXContent", e); } @@ -195,13 +208,23 @@ public final BytesReference buildAsBytes(XContentType contentType) { static class TransformedAction implements ToXContentObject { private final Action action; - @Nullable private String path; - @Nullable private final TimeValue throttlePeriod; - @Nullable private final Condition condition; - @Nullable private final Transform transform; - - TransformedAction(String id, Action action, @Nullable TimeValue throttlePeriod, - @Nullable Condition condition, @Nullable Transform transform, @Nullable String path) { + @Nullable + private String path; + @Nullable + private final TimeValue throttlePeriod; + @Nullable + private final Condition condition; + @Nullable + private final Transform transform; + + TransformedAction( + String id, + Action action, + @Nullable TimeValue throttlePeriod, + @Nullable Condition condition, + @Nullable Transform transform, + @Nullable String path + ) { this.throttlePeriod = throttlePeriod; this.condition = condition; this.transform = transform; @@ -213,18 +236,17 @@ static class TransformedAction implements ToXContentObject { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); if (throttlePeriod != null) { - builder.humanReadableField(ThrottlerField.THROTTLE_PERIOD.getPreferredName(), - ThrottlerField.THROTTLE_PERIOD_HUMAN.getPreferredName(), throttlePeriod); + builder.humanReadableField( + ThrottlerField.THROTTLE_PERIOD.getPreferredName(), + ThrottlerField.THROTTLE_PERIOD_HUMAN.getPreferredName(), + throttlePeriod + ); } if (condition != null) { - builder.startObject(WatchField.CONDITION.getPreferredName()) - .field(condition.type(), condition, params) - .endObject(); + builder.startObject(WatchField.CONDITION.getPreferredName()).field(condition.type(), condition, params).endObject(); } if (transform != null) { - builder.startObject(Transform.TRANSFORM.getPreferredName()) - .field(transform.type(), transform, params) - .endObject(); + builder.startObject(Transform.TRANSFORM.getPreferredName()).field(transform.type(), transform, params).endObject(); } if (path != null) { builder.field("foreach", path); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/common/stats/Counters.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/common/stats/Counters.java index cb09fb29b56e3..ea3481511feb9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/common/stats/Counters.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/common/stats/Counters.java @@ -8,6 +8,7 @@ import com.carrotsearch.hppc.ObjectLongHashMap; import com.carrotsearch.hppc.cursors.ObjectLongCursor; + import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; @@ -34,7 +35,7 @@ public Counters(StreamInput in) throws IOException { } } - public Counters(String ... names) { + public Counters(String... names) { for (String name : names) { set(name); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/condition/AlwaysCondition.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/condition/AlwaysCondition.java index 35f568850c7c6..3b45d2402cf70 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/condition/AlwaysCondition.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/condition/AlwaysCondition.java @@ -14,7 +14,7 @@ public class AlwaysCondition implements Condition { public static final String TYPE = "always"; public static final Condition INSTANCE = new AlwaysCondition(); - protected AlwaysCondition() { } + protected AlwaysCondition() {} @Override public boolean equals(Object obj) { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/condition/Condition.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/condition/Condition.java index 11e1201843b2f..485e902acc852 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/condition/Condition.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/condition/Condition.java @@ -23,12 +23,13 @@ public interface Condition extends ToXContentObject { class Result implements ToXContentObject { // don't make this final - we can't mock final classes :( - public Map getResolvedValues() { + public Map getResolvedValues() { return resolveValues; } public enum Status { - SUCCESS, FAILURE + SUCCESS, + FAILURE } private final String type; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/condition/ConditionRegistry.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/condition/ConditionRegistry.java index ccd6c53169102..dfca59f09c4ed 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/condition/ConditionRegistry.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/condition/ConditionRegistry.java @@ -46,20 +46,29 @@ public ExecutableCondition parseExecutable(String watchId, XContentParser parser if (token == XContentParser.Token.FIELD_NAME) { type = parser.currentName(); } else if (type == null) { - throw new ElasticsearchParseException("could not parse condition for watch [{}]. invalid definition. expected a field " + - "indicating the condition type, but found", watchId, token); + throw new ElasticsearchParseException( + "could not parse condition for watch [{}]. invalid definition. expected a field " + + "indicating the condition type, but found", + watchId, + token + ); } else { factory = factories.get(type); if (factory == null) { - throw new ElasticsearchParseException("could not parse condition for watch [{}]. unknown condition type [{}]", - watchId, type); + throw new ElasticsearchParseException( + "could not parse condition for watch [{}]. unknown condition type [{}]", + watchId, + type + ); } condition = factory.parse(clock, watchId, parser); } } if (condition == null) { - throw new ElasticsearchParseException("could not parse condition for watch [{}]. missing required condition type field", - watchId); + throw new ElasticsearchParseException( + "could not parse condition for watch [{}]. missing required condition type field", + watchId + ); } return condition; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/crypto/CryptoService.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/crypto/CryptoService.java index 89de62ac2a191..7dde4f7270ad9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/crypto/CryptoService.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/crypto/CryptoService.java @@ -9,20 +9,14 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.ElasticsearchException; -import org.elasticsearch.core.CharArrays; import org.elasticsearch.common.io.Streams; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Setting.Property; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.CharArrays; import org.elasticsearch.xpack.core.security.SecurityField; import org.elasticsearch.xpack.core.watcher.WatcherField; -import javax.crypto.BadPaddingException; -import javax.crypto.Cipher; -import javax.crypto.IllegalBlockSizeException; -import javax.crypto.SecretKey; -import javax.crypto.spec.IvParameterSpec; -import javax.crypto.spec.SecretKeySpec; import java.io.IOException; import java.io.InputStream; import java.security.MessageDigest; @@ -32,6 +26,13 @@ import java.util.Base64; import java.util.List; +import javax.crypto.BadPaddingException; +import javax.crypto.Cipher; +import javax.crypto.IllegalBlockSizeException; +import javax.crypto.SecretKey; +import javax.crypto.spec.IvParameterSpec; +import javax.crypto.spec.SecretKeySpec; + /** * Service that provides cryptographic methods based on a shared system key */ @@ -53,12 +54,23 @@ public class CryptoService { private static final String DEFAULT_KEY_ALGORITH = "AES"; private static final int DEFAULT_KEY_LENGTH = 128; - private static final Setting ENCRYPTION_ALGO_SETTING = - new Setting<>(SecurityField.setting("encryption.algorithm"), s -> DEFAULT_ENCRYPTION_ALGORITHM, s -> s, Property.NodeScope); - private static final Setting ENCRYPTION_KEY_LENGTH_SETTING = - Setting.intSetting(SecurityField.setting("encryption_key.length"), DEFAULT_KEY_LENGTH, Property.NodeScope); - private static final Setting ENCRYPTION_KEY_ALGO_SETTING = - new Setting<>(SecurityField.setting("encryption_key.algorithm"), DEFAULT_KEY_ALGORITH, s -> s, Property.NodeScope); + private static final Setting ENCRYPTION_ALGO_SETTING = new Setting<>( + SecurityField.setting("encryption.algorithm"), + s -> DEFAULT_ENCRYPTION_ALGORITHM, + s -> s, + Property.NodeScope + ); + private static final Setting ENCRYPTION_KEY_LENGTH_SETTING = Setting.intSetting( + SecurityField.setting("encryption_key.length"), + DEFAULT_KEY_LENGTH, + Property.NodeScope + ); + private static final Setting ENCRYPTION_KEY_ALGO_SETTING = new Setting<>( + SecurityField.setting("encryption_key.algorithm"), + DEFAULT_KEY_ALGORITH, + s -> s, + Property.NodeScope + ); private static final Logger logger = LogManager.getLogger(CryptoService.class); private final SecureRandom secureRandom = new SecureRandom(); @@ -99,7 +111,8 @@ private static SecretKey readSystemKey(InputStream in) throws IOException { final int read = Streams.readFully(in, keyBytes); if (read != keySizeBytes) { throw new IllegalArgumentException( - "key size did not match expected value; was the key generated with elasticsearch-syskeygen?"); + "key size did not match expected value; was the key generated with elasticsearch-syskeygen?" + ); } return new SecretKeySpec(keyBytes, KEY_ALGO); } @@ -181,7 +194,6 @@ private byte[] decryptInternal(byte[] bytes, SecretKey key) { } } - private static Cipher cipher(int mode, String encryptionAlgorithm, SecretKey key, byte[] initializationVector) { try { Cipher cipher = Cipher.getInstance(encryptionAlgorithm); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/execution/ActionExecutionMode.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/execution/ActionExecutionMode.java index 84ed04302864c..1c427973a0588 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/execution/ActionExecutionMode.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/execution/ActionExecutionMode.java @@ -61,11 +61,16 @@ public final boolean force() { public static ActionExecutionMode resolve(byte id) { switch (id) { - case 1: return SIMULATE; - case 2: return FORCE_SIMULATE; - case 3: return EXECUTE; - case 4: return FORCE_EXECUTE; - case 5: return SKIP; + case 1: + return SIMULATE; + case 2: + return FORCE_SIMULATE; + case 3: + return EXECUTE; + case 4: + return FORCE_EXECUTE; + case 5: + return SKIP; } throw illegalArgument("unknown action execution mode id [{}]", id); } @@ -75,11 +80,16 @@ public static ActionExecutionMode resolve(String key) { return null; } switch (key.toLowerCase(Locale.ROOT)) { - case "simulate": return SIMULATE; - case "force_simulate": return FORCE_SIMULATE; - case "execute": return EXECUTE; - case "force_execute": return FORCE_EXECUTE; - case "skip": return SKIP; + case "simulate": + return SIMULATE; + case "force_simulate": + return FORCE_SIMULATE; + case "execute": + return EXECUTE; + case "force_execute": + return FORCE_EXECUTE; + case "skip": + return SKIP; } throw illegalArgument("unknown action execution mode [{}]", key); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/execution/WatchExecutionContext.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/execution/WatchExecutionContext.java index 816ce785d7219..a6e6b593309b3 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/execution/WatchExecutionContext.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/execution/WatchExecutionContext.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.watcher.execution; import org.elasticsearch.common.CheckedSupplier; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.ConcurrentCollections; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.xpack.core.security.authc.Authentication; import org.elasticsearch.xpack.core.security.authc.AuthenticationField; import org.elasticsearch.xpack.core.security.authc.support.AuthenticationContextSerializer; @@ -149,7 +149,9 @@ public String getNodeId() { /** * @return The user that executes the watch, which will be stored in the watch history */ - public String getUser() { return user; } + public String getUser() { + return user; + } public void start() { assert phase == ExecutionPhase.AWAITS_EXECUTION; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/execution/WatchExecutionResult.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/execution/WatchExecutionResult.java index 9aecd89940ad8..b8e93c86f1833 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/execution/WatchExecutionResult.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/execution/WatchExecutionResult.java @@ -24,19 +24,33 @@ public class WatchExecutionResult implements ToXContentObject { private final ZonedDateTime executionTime; private final long executionDurationMs; - @Nullable private final Input.Result inputResult; - @Nullable private final Condition.Result conditionResult; - @Nullable private final Transform.Result transformResult; + @Nullable + private final Input.Result inputResult; + @Nullable + private final Condition.Result conditionResult; + @Nullable + private final Transform.Result transformResult; private final Map actionsResults; public WatchExecutionResult(WatchExecutionContext context, long executionDurationMs) { - this(context.executionTime(), executionDurationMs, context.inputResult(), context.conditionResult(), context.transformResult(), - context.actionsResults()); + this( + context.executionTime(), + executionDurationMs, + context.inputResult(), + context.conditionResult(), + context.transformResult(), + context.actionsResults() + ); } - private WatchExecutionResult(ZonedDateTime executionTime, long executionDurationMs, Input.Result inputResult, - Condition.Result conditionResult, @Nullable Transform.Result transformResult, - Map actionsResults) { + private WatchExecutionResult( + ZonedDateTime executionTime, + long executionDurationMs, + Input.Result inputResult, + Condition.Result conditionResult, + @Nullable Transform.Result transformResult, + Map actionsResults + ) { this.executionTime = executionTime; this.inputResult = inputResult; this.conditionResult = conditionResult; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/execution/Wid.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/execution/Wid.java index c9ed53b8f9bd0..39f59aa742690 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/execution/Wid.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/execution/Wid.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.xpack.core.watcher.execution; - import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.util.UUID; @@ -33,7 +32,7 @@ public class Wid { public Wid(String watchId, ZonedDateTime executionTime) { this.watchId = watchId; - this.value = watchId + "_" + UUID.randomUUID().toString() + "-" + formatter.format(executionTime); + this.value = watchId + "_" + UUID.randomUUID().toString() + "-" + formatter.format(executionTime); } public Wid(String value) { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/history/WatchRecord.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/history/WatchRecord.java index 2c6968b6f0a48..8721fa277d6ab 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/history/WatchRecord.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/history/WatchRecord.java @@ -7,9 +7,9 @@ package org.elasticsearch.xpack.core.watcher.history; import org.elasticsearch.ElasticsearchException; +import org.elasticsearch.common.Strings; import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.common.Strings; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.watcher.actions.Action; @@ -56,15 +56,28 @@ public abstract class WatchRecord implements ToXContentObject { // only emitted to xcontent in "debug" mode protected final Map vars; - @Nullable protected final ExecutableInput input; - @Nullable protected final ExecutableCondition condition; - @Nullable protected final Map metadata; - @Nullable protected final WatchExecutionResult executionResult; - - private WatchRecord(Wid id, TriggerEvent triggerEvent, ExecutionState state, Map vars, - ExecutableInput input, ExecutableCondition condition, - Map metadata, Watch watch, WatchExecutionResult executionResult, - String nodeId, String user) { + @Nullable + protected final ExecutableInput input; + @Nullable + protected final ExecutableCondition condition; + @Nullable + protected final Map metadata; + @Nullable + protected final WatchExecutionResult executionResult; + + private WatchRecord( + Wid id, + TriggerEvent triggerEvent, + ExecutionState state, + Map vars, + ExecutableInput input, + ExecutableCondition condition, + Map metadata, + Watch watch, + WatchExecutionResult executionResult, + String nodeId, + String user + ) { this.id = id; this.triggerEvent = triggerEvent; this.state = state; @@ -83,23 +96,51 @@ private WatchRecord(Wid id, TriggerEvent triggerEvent, ExecutionState state, Str } private WatchRecord(WatchRecord record, ExecutionState state) { - this(record.id, record.triggerEvent, state, record.vars, record.input, record.condition, record.metadata, record.watch, - record.executionResult, record.nodeId, record.user); + this( + record.id, + record.triggerEvent, + state, + record.vars, + record.input, + record.condition, + record.metadata, + record.watch, + record.executionResult, + record.nodeId, + record.user + ); } private WatchRecord(WatchExecutionContext context, ExecutionState state) { - this(context.id(), context.triggerEvent(), state, context.vars(), - context.watch() != null ? context.watch().input() : null, - context.watch() != null ? context.watch().condition() : null, - context.watch() != null ? context.watch().metadata() : null, - context.watch(), - null, context.getNodeId(), context.getUser()); + this( + context.id(), + context.triggerEvent(), + state, + context.vars(), + context.watch() != null ? context.watch().input() : null, + context.watch() != null ? context.watch().condition() : null, + context.watch() != null ? context.watch().metadata() : null, + context.watch(), + null, + context.getNodeId(), + context.getUser() + ); } private WatchRecord(WatchExecutionContext context, WatchExecutionResult executionResult) { - this(context.id(), context.triggerEvent(), getState(executionResult), context.vars(), context.watch().input(), - context.watch().condition(), context.watch().metadata(), context.watch(), executionResult, context.getNodeId(), - context.getUser()); + this( + context.id(), + context.triggerEvent(), + getState(executionResult), + context.vars(), + context.watch().input(), + context.watch().condition(), + context.watch().metadata(), + context.watch(), + executionResult, + context.getNodeId(), + context.getUser() + ); } public static ExecutionState getState(WatchExecutionResult executionResult) { @@ -133,7 +174,9 @@ public String watchId() { return id.watchId(); } - public ExecutableInput input() { return input; } + public ExecutableInput input() { + return input; + } public ExecutionState state() { return state; @@ -174,14 +217,10 @@ public final XContentBuilder toXContent(XContentBuilder builder, Params params) } if (input != null) { - builder.startObject(WatchField.INPUT.getPreferredName()) - .field(input.type(), input, params) - .endObject(); + builder.startObject(WatchField.INPUT.getPreferredName()).field(input.type(), input, params).endObject(); } if (condition != null) { - builder.startObject(WatchField.CONDITION.getPreferredName()) - .field(condition.type(), condition, params) - .endObject(); + builder.startObject(WatchField.CONDITION.getPreferredName()).field(condition.type(), condition, params).endObject(); } if (metadata != null) { builder.field(METADATA.getPreferredName(), metadata); @@ -216,7 +255,8 @@ public String toString() { } public static class MessageWatchRecord extends WatchRecord { - @Nullable private final String[] messages; + @Nullable + private final String[] messages; /** * Called when the execution was aborted before it started @@ -256,11 +296,11 @@ public MessageWatchRecord(WatchRecord record, ExecutionState state, String messa this.messages = newMessages; } } else { - messages = new String []{ message }; + messages = new String[] { message }; } } - public String[] messages(){ + public String[] messages() { return messages; } @@ -274,10 +314,13 @@ void innerToXContent(XContentBuilder builder, Params params) throws IOException public static class ExceptionWatchRecord extends WatchRecord { - private static final Map STACK_TRACE_ENABLED_PARAMS = - Map.of(ElasticsearchException.REST_EXCEPTION_SKIP_STACK_TRACE, "false"); + private static final Map STACK_TRACE_ENABLED_PARAMS = Map.of( + ElasticsearchException.REST_EXCEPTION_SKIP_STACK_TRACE, + "false" + ); - @Nullable private final Exception exception; + @Nullable + private final Exception exception; public ExceptionWatchRecord(WatchExecutionContext context, WatchExecutionResult executionResult, Exception exception) { super(context, executionResult); @@ -309,9 +352,9 @@ void innerToXContent(XContentBuilder builder, Params params) throws IOException builder.endObject(); } else { builder.startObject(EXCEPTION.getPreferredName()) - .field("type", ElasticsearchException.getExceptionName(exception)) - .field("reason", exception.getMessage()) - .endObject(); + .field("type", ElasticsearchException.getExceptionName(exception)) + .field("reason", exception.getMessage()) + .endObject(); } } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/input/Input.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/input/Input.java index 47cc99705ced3..10fc92982f400 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/input/Input.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/input/Input.java @@ -27,13 +27,15 @@ abstract class Result implements ToXContentObject { private static final ParseField PAYLOAD = new ParseField("payload"); public enum Status { - SUCCESS, FAILURE + SUCCESS, + FAILURE } protected final String type; protected final Status status; private final Payload payload; - @Nullable private final Exception exception; + @Nullable + private final Exception exception; protected Result(String type, Payload payload) { this.status = Status.SUCCESS; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/input/none/NoneInput.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/input/none/NoneInput.java index c3eddbac3c9bb..84a82c4dd6938 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/input/none/NoneInput.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/input/none/NoneInput.java @@ -19,8 +19,7 @@ public class NoneInput implements Input { public static final String TYPE = "none"; public static final NoneInput INSTANCE = new NoneInput(); - private NoneInput() { - } + private NoneInput() {} @Override public String type() { @@ -66,8 +65,7 @@ public static class Builder implements Input.Builder { private static final Builder INSTANCE = new Builder(); - private Builder() { - } + private Builder() {} @Override public NoneInput build() { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/support/Exceptions.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/support/Exceptions.java index c3bfcf3573cd9..88043782f8a67 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/support/Exceptions.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/support/Exceptions.java @@ -12,8 +12,7 @@ public class Exceptions { - private Exceptions() { - } + private Exceptions() {} public static IllegalArgumentException illegalArgument(String msg, Object... args) { return new IllegalArgumentException(format(msg, args)); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/support/WatcherDateTimeUtils.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/support/WatcherDateTimeUtils.java index 210fa2e02d51f..58b8c2391d8bd 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/support/WatcherDateTimeUtils.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/support/WatcherDateTimeUtils.java @@ -7,16 +7,16 @@ package org.elasticsearch.xpack.core.watcher.support; import org.elasticsearch.ElasticsearchParseException; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.time.DateFormatter; import org.elasticsearch.common.time.DateFormatters; import org.elasticsearch.common.time.DateMathParser; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.index.mapper.DateFieldMapper; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.index.mapper.DateFieldMapper; import java.io.IOException; import java.time.Clock; @@ -27,14 +27,13 @@ import java.util.Locale; import java.util.Objects; import java.util.concurrent.TimeUnit; + public class WatcherDateTimeUtils { public static final DateFormatter dateTimeFormatter = DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER; public static final DateMathParser dateMathParser = dateTimeFormatter.toDateMathParser(); - - private WatcherDateTimeUtils() { - } + private WatcherDateTimeUtils() {} public static ZonedDateTime convertToDate(Object value, Clock clock) { if (value instanceof ZonedDateTime) { @@ -64,14 +63,16 @@ public static String formatDate(ZonedDateTime date) { public static ZonedDateTime parseDateMath(String fieldName, XContentParser parser, ZoneId timeZone, Clock clock) throws IOException { if (parser.currentToken() == XContentParser.Token.VALUE_NULL) { - throw new ElasticsearchParseException("could not parse date/time expected date field [{}] to not be null but was null", - fieldName); + throw new ElasticsearchParseException( + "could not parse date/time expected date field [{}] to not be null but was null", + fieldName + ); } return parseDateMathOrNull(fieldName, parser, timeZone, clock); } - public static ZonedDateTime parseDateMathOrNull(String fieldName, XContentParser parser, ZoneId timeZone, - Clock clock) throws IOException { + public static ZonedDateTime parseDateMathOrNull(String fieldName, XContentParser parser, ZoneId timeZone, Clock clock) + throws IOException { XContentParser.Token token = parser.currentToken(); if (token == XContentParser.Token.VALUE_NUMBER) { return Instant.ofEpochMilli(parser.longValue()).atZone(timeZone); @@ -80,15 +81,23 @@ public static ZonedDateTime parseDateMathOrNull(String fieldName, XContentParser try { return parseDateMath(parser.text(), timeZone, clock); } catch (ElasticsearchParseException epe) { - throw new ElasticsearchParseException("could not parse date/time. expected date field [{}] to be either a number or a " + - "DateMath string but found [{}] instead", epe, fieldName, parser.text()); + throw new ElasticsearchParseException( + "could not parse date/time. expected date field [{}] to be either a number or a " + + "DateMath string but found [{}] instead", + epe, + fieldName, + parser.text() + ); } } if (token == XContentParser.Token.VALUE_NULL) { return null; } - throw new ElasticsearchParseException("could not parse date/time. expected date field [{}] to be either a number or a string but " + - "found [{}] instead", fieldName, token); + throw new ElasticsearchParseException( + "could not parse date/time. expected date field [{}] to be either a number or a string but " + "found [{}] instead", + fieldName, + token + ); } public static ZonedDateTime parseDateMath(String valueString, ZoneId timeZone, final Clock clock) { @@ -106,8 +115,11 @@ public static ZonedDateTime parseDate(String fieldName, XContentParser parser, Z if (token == XContentParser.Token.VALUE_NULL) { return null; } - throw new ElasticsearchParseException("could not parse date/time. expected date field [{}] to be either a number or a string but " + - "found [{}] instead", fieldName, token); + throw new ElasticsearchParseException( + "could not parse date/time. expected date field [{}] to be either a number or a string but " + "found [{}] instead", + fieldName, + token + ); } public static XContentBuilder writeDate(String fieldName, XContentBuilder builder, ZonedDateTime date) throws IOException { @@ -151,8 +163,10 @@ public static TimeValue parseTimeValue(XContentParser parser, String settingName } } - throw new ElasticsearchParseException("could not parse time value. expected either a string or a null value but found [{}] " + - "instead", token); + throw new ElasticsearchParseException( + "could not parse time value. expected either a string or a null value but found [{}] " + "instead", + token + ); } /** @@ -188,8 +202,10 @@ public static TimeValue parseTimeValueSupportingFractional(@Nullable String sVal millis = 0; } else { throw new ElasticsearchParseException( - "Failed to parse setting [{}] with value [{}] as a time value: unit is missing or unrecognized", - settingName, sValue); + "Failed to parse setting [{}] with value [{}] as a time value: unit is missing or unrecognized", + settingName, + sValue + ); } return new TimeValue(millis, TimeUnit.MILLISECONDS); } catch (NumberFormatException e) { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/support/WatcherIndexTemplateRegistryField.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/support/WatcherIndexTemplateRegistryField.java index c7af6cc0f7d3c..144e543507304 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/support/WatcherIndexTemplateRegistryField.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/support/WatcherIndexTemplateRegistryField.java @@ -25,12 +25,8 @@ public final class WatcherIndexTemplateRegistryField { public static final int INDEX_TEMPLATE_VERSION = 14; public static final String HISTORY_TEMPLATE_NAME = ".watch-history-" + INDEX_TEMPLATE_VERSION; public static final String HISTORY_TEMPLATE_NAME_NO_ILM = ".watch-history-no-ilm-" + INDEX_TEMPLATE_VERSION; - public static final String[] TEMPLATE_NAMES = new String[] { - HISTORY_TEMPLATE_NAME - }; - public static final String[] TEMPLATE_NAMES_NO_ILM = new String[] { - HISTORY_TEMPLATE_NAME_NO_ILM - }; + public static final String[] TEMPLATE_NAMES = new String[] { HISTORY_TEMPLATE_NAME }; + public static final String[] TEMPLATE_NAMES_NO_ILM = new String[] { HISTORY_TEMPLATE_NAME_NO_ILM }; private WatcherIndexTemplateRegistryField() {} } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/support/WatcherUtils.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/support/WatcherUtils.java index 0d084b8949c56..796b7bc4565c7 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/support/WatcherUtils.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/support/WatcherUtils.java @@ -7,10 +7,10 @@ package org.elasticsearch.xpack.core.watcher.support; import org.elasticsearch.common.Strings; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentType; import java.io.IOException; @@ -26,12 +26,14 @@ public final class WatcherUtils { private static final Pattern NO_WS_PATTERN = Pattern.compile("\\S+"); - private WatcherUtils() { - } + private WatcherUtils() {} public static Map responseToData(ToXContentObject response, ToXContent.Params params) throws IOException { - return XContentHelper.convertToMap(XContentHelper.toXContent(response, XContentType.SMILE, params, false), false, - XContentType.SMILE).v2(); + return XContentHelper.convertToMap( + XContentHelper.toXContent(response, XContentType.SMILE, params, false), + false, + XContentType.SMILE + ).v2(); } public static Map flattenModel(Map map) { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/support/xcontent/WatcherParams.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/support/xcontent/WatcherParams.java index 6c7e84991f390..aea526523f4ca 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/support/xcontent/WatcherParams.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/support/xcontent/WatcherParams.java @@ -51,9 +51,7 @@ private boolean hideHeaders() { } public static WatcherParams wrap(ToXContent.Params params) { - return params instanceof WatcherParams ? - (WatcherParams) params : - new WatcherParams(emptyMap(), params); + return params instanceof WatcherParams ? (WatcherParams) params : new WatcherParams(emptyMap(), params); } public static Builder builder() { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/support/xcontent/WatcherXContentParser.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/support/xcontent/WatcherXContentParser.java index b235cdccfd5cf..4ddd20b255b3e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/support/xcontent/WatcherXContentParser.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/support/xcontent/WatcherXContentParser.java @@ -59,16 +59,23 @@ public static Secret secretOrNull(XContentParser parser) throws IOException { } private final ZonedDateTime parseTime; - @Nullable private final CryptoService cryptoService; + @Nullable + private final CryptoService cryptoService; private final boolean allowRedactedPasswords; - public WatcherXContentParser(XContentParser parser, ZonedDateTime parseTime, @Nullable CryptoService cryptoService, - boolean allowRedactedPasswords) { + public WatcherXContentParser( + XContentParser parser, + ZonedDateTime parseTime, + @Nullable CryptoService cryptoService, + boolean allowRedactedPasswords + ) { super(parser); this.parseTime = parseTime; this.cryptoService = cryptoService; this.allowRedactedPasswords = allowRedactedPasswords; } - public ZonedDateTime getParseDateTime() { return parseTime; } + public ZonedDateTime getParseDateTime() { + return parseTime; + } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/support/xcontent/XContentSource.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/support/xcontent/XContentSource.java index 53a97a921c092..56e48905b9c83 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/support/xcontent/XContentSource.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/support/xcontent/XContentSource.java @@ -12,11 +12,11 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ObjectPath; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.XContentUtils; @@ -112,8 +112,7 @@ public T getValue(String path) { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { // EMPTY is safe here because we never use namedObject - try (InputStream stream = bytes.streamInput(); - XContentParser parser = parser(NamedXContentRegistry.EMPTY, stream)) { + try (InputStream stream = bytes.streamInput(); XContentParser parser = parser(NamedXContentRegistry.EMPTY, stream)) { parser.nextToken(); builder.generator().copyCurrentStructure(parser); return builder; @@ -136,8 +135,7 @@ public static void writeTo(XContentSource source, StreamOutput out) throws IOExc private Object data() { if (data == null) { // EMPTY is safe here because we never use namedObject - try (InputStream stream = bytes.streamInput(); - XContentParser parser = parser(NamedXContentRegistry.EMPTY, stream)) { + try (InputStream stream = bytes.streamInput(); XContentParser parser = parser(NamedXContentRegistry.EMPTY, stream)) { data = XContentUtils.readValue(parser, parser.nextToken()); } catch (IOException ex) { throw new ElasticsearchException("failed to read value", ex); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transform/Transform.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transform/Transform.java index 9de859b593e7c..1f915e4243bdc 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transform/Transform.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transform/Transform.java @@ -30,14 +30,18 @@ abstract class Result implements ToXContentObject { private static final ParseField REASON = new ParseField("reason"); public enum Status { - SUCCESS, FAILURE + SUCCESS, + FAILURE } protected final String type; protected final Status status; - @Nullable protected final Payload payload; - @Nullable protected final String reason; - @Nullable protected final Exception exception; + @Nullable + protected final Payload payload; + @Nullable + protected final String reason; + @Nullable + protected final Exception exception; public Result(String type, Payload payload) { this.type = type; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transform/TransformRegistry.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transform/TransformRegistry.java index 4bf70962a1744..ab7a4bf5964fc 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transform/TransformRegistry.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transform/TransformRegistry.java @@ -18,13 +18,15 @@ public class TransformRegistry { - private final Map>> factories; + private final Map< + String, + TransformFactory>> factories; - public TransformRegistry(Map>> factories) { - Map>> map - = new HashMap<>(factories); + public TransformRegistry( + Map>> factories + ) { + Map>> map = + new HashMap<>(factories); map.put(ChainTransform.TYPE, new ChainTransformFactory(this)); this.factories = Collections.unmodifiableMap(map); } @@ -50,8 +52,9 @@ public TransformRegistry(Map parse(String watchId, String type, XContentParser parser) throws IOException { - TransformFactory> factory = factories.get(type); + TransformFactory> factory = factories.get( + type + ); if (factory == null) { throw new ElasticsearchParseException("could not parse transform for watch [{}], unknown transform type [{}]", watchId, type); } @@ -59,8 +62,9 @@ public TransformRegistry(Map> factory = factories.get(type); + TransformFactory> factory = factories.get( + type + ); if (factory == null) { throw new ElasticsearchParseException("could not parse transform for watch [{}], unknown transform type [{}]", watchId, type); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transform/chain/ChainTransform.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transform/chain/ChainTransform.java index b0841d9edcaad..d27a255dc0843 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transform/chain/ChainTransform.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transform/chain/ChainTransform.java @@ -62,9 +62,7 @@ public int hashCode() { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startArray(); for (Transform transform : transforms) { - builder.startObject() - .field(transform.type(), transform, params) - .endObject(); + builder.startObject().field(transform.type(), transform, params).endObject(); } return builder.endArray(); } @@ -72,8 +70,12 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws static ChainTransform parse(String watchId, XContentParser parser, TransformRegistry transformRegistry) throws IOException { XContentParser.Token token = parser.currentToken(); if (token != XContentParser.Token.START_ARRAY) { - throw new ElasticsearchParseException("could not parse [{}] transform for watch [{}]. expected an array of transform objects," + - " but found [{}] instead", TYPE, watchId, token); + throw new ElasticsearchParseException( + "could not parse [{}] transform for watch [{}]. expected an array of transform objects," + " but found [{}] instead", + TYPE, + watchId, + token + ); } List transforms = new ArrayList<>(); @@ -81,8 +83,12 @@ static ChainTransform parse(String watchId, XContentParser parser, TransformRegi String currentFieldName = null; while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { if (token != XContentParser.Token.START_OBJECT) { - throw new ElasticsearchParseException("could not parse [{}] transform for watch [{}]. expected a transform object, but " + - "found [{}] instead", TYPE, watchId, token); + throw new ElasticsearchParseException( + "could not parse [{}] transform for watch [{}]. expected a transform object, but " + "found [{}] instead", + TYPE, + watchId, + token + ); } while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { @@ -151,7 +157,7 @@ public Builder add(Transform... transforms) { } public Builder add(Transform.Builder... transforms) { - for (Transform.Builder transform: transforms) { + for (Transform.Builder transform : transforms) { this.transforms.add(transform.build()); } return this; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transform/chain/ChainTransformFactory.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transform/chain/ChainTransformFactory.java index 8415804f0ffb3..403527dd0c1d0 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transform/chain/ChainTransformFactory.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transform/chain/ChainTransformFactory.java @@ -36,7 +36,7 @@ public ChainTransform parseTransform(String watchId, XContentParser parser) thro } @Override - @SuppressWarnings({"rawtypes", "unchecked"}) + @SuppressWarnings({ "rawtypes", "unchecked" }) public ExecutableChainTransform createExecutable(ChainTransform chainTransform) { ArrayList executables = new ArrayList<>(); for (Transform transform : chainTransform.getTransforms()) { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transform/chain/ExecutableChainTransform.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transform/chain/ExecutableChainTransform.java index bc23e1f595d73..3d7000de7ecbc 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transform/chain/ExecutableChainTransform.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transform/chain/ExecutableChainTransform.java @@ -57,8 +57,15 @@ ChainTransform.Result doExecute(WatchExecutionContext ctx, Payload payload, List Transform.Result result = transform.execute(ctx, payload); results.add(result); if (result.status() == Transform.Result.Status.FAILURE) { - return new ChainTransform.Result(format("failed to execute [{}] transform for [{}]. failed to execute sub-transform [{}]", - ChainTransform.TYPE, ctx.id(), transform.type()), results); + return new ChainTransform.Result( + format( + "failed to execute [{}] transform for [{}]. failed to execute sub-transform [{}]", + ChainTransform.TYPE, + ctx.id(), + transform.type() + ), + results + ); } payload = result.payload(); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/QueryWatchesAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/QueryWatchesAction.java index 1099066b0d4c2..cbac378b22f30 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/QueryWatchesAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/QueryWatchesAction.java @@ -10,21 +10,21 @@ import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.ActionResponse; import org.elasticsearch.action.ActionType; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.index.query.QueryBuilder; +import org.elasticsearch.search.searchafter.SearchAfterBuilder; +import org.elasticsearch.search.sort.FieldSortBuilder; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentFragment; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentParser.Token; -import org.elasticsearch.index.query.QueryBuilder; -import org.elasticsearch.search.searchafter.SearchAfterBuilder; -import org.elasticsearch.search.sort.FieldSortBuilder; import org.elasticsearch.xpack.core.watcher.support.xcontent.XContentSource; import org.elasticsearch.xpack.core.watcher.watch.WatchStatus; @@ -32,8 +32,8 @@ import java.util.List; import java.util.Objects; -import static org.elasticsearch.xcontent.ConstructingObjectParser.optionalConstructorArg; import static org.elasticsearch.index.query.AbstractQueryBuilder.parseInnerQueryBuilder; +import static org.elasticsearch.xcontent.ConstructingObjectParser.optionalConstructorArg; public class QueryWatchesAction extends ActionType { @@ -82,8 +82,12 @@ public static class Request extends ActionRequest implements ToXContentObject { } return result; }, SORT_FIELD); - PARSER.declareField(optionalConstructorArg(), (p, c) -> SearchAfterBuilder.fromXContent(p), SEARCH_AFTER_FIELD, - ObjectParser.ValueType.VALUE_ARRAY); + PARSER.declareField( + optionalConstructorArg(), + (p, c) -> SearchAfterBuilder.fromXContent(p), + SEARCH_AFTER_FIELD, + ObjectParser.ValueType.VALUE_ARRAY + ); } public static Request fromXContent(XContentParser parser) throws IOException { @@ -109,11 +113,7 @@ public Request(StreamInput in) throws IOException { searchAfter = in.readOptionalWriteable(SearchAfterBuilder::new); } - public Request(Integer from, - Integer size, - QueryBuilder query, - List sorts, - SearchAfterBuilder searchAfter) { + public Request(Integer from, Integer size, QueryBuilder query, List sorts, SearchAfterBuilder searchAfter) { this.from = from; this.size = size; this.query = query; @@ -191,11 +191,11 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Request request = (Request) o; - return Objects.equals(from, request.from) && - Objects.equals(size, request.size) && - Objects.equals(query, request.query) && - Objects.equals(sorts, request.sorts) && - Objects.equals(searchAfter, request.searchAfter); + return Objects.equals(from, request.from) + && Objects.equals(size, request.size) + && Objects.equals(query, request.query) + && Objects.equals(sorts, request.sorts) + && Objects.equals(searchAfter, request.searchAfter); } @Override @@ -253,8 +253,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Response response = (Response) o; - return watchTotalCount == response.watchTotalCount && - watches.equals(response.watches); + return watchTotalCount == response.watchTotalCount && watches.equals(response.watches); } @Override @@ -319,7 +318,7 @@ public void writeTo(StreamOutput out) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.field("_id", id); builder.field("watch", source, params); - builder.field("status", status, params); + builder.field("status", status, params); builder.field("_seq_no", seqNo); builder.field("_primary_term", primaryTerm); return builder; @@ -330,10 +329,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Item item = (Item) o; - return seqNo == item.seqNo && - primaryTerm == item.primaryTerm && - id.equals(item.id) && - source.equals(item.source); + return seqNo == item.seqNo && primaryTerm == item.primaryTerm && id.equals(item.id) && source.equals(item.source); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/ack/AckWatchRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/ack/AckWatchRequest.java index 73a70f6ae7cc8..2540f29fcc769 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/ack/AckWatchRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/ack/AckWatchRequest.java @@ -71,7 +71,7 @@ public String[] getActionIds() { @Override public ActionRequestValidationException validate() { ActionRequestValidationException validationException = null; - if (watchId == null){ + if (watchId == null) { validationException = ValidateActions.addValidationError("watch id is missing", validationException); } else if (WatcherUtils.isValidId(watchId) == false) { validationException = ValidateActions.addValidationError("watch id contains whitespace", validationException); @@ -80,10 +80,14 @@ public ActionRequestValidationException validate() { for (String actionId : actionIds) { if (actionId == null) { validationException = ValidateActions.addValidationError( - String.format(Locale.ROOT, "action id may not be null"), validationException); + String.format(Locale.ROOT, "action id may not be null"), + validationException + ); } else if (WatcherUtils.isValidId(actionId) == false) { validationException = ValidateActions.addValidationError( - String.format(Locale.ROOT, "action id [%s] contains whitespace", actionId), validationException); + String.format(Locale.ROOT, "action id [%s] contains whitespace", actionId), + validationException + ); } } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/ack/AckWatchResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/ack/AckWatchResponse.java index cea60042c1abb..97f4b7d619191 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/ack/AckWatchResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/ack/AckWatchResponse.java @@ -7,9 +7,9 @@ package org.elasticsearch.xpack.core.watcher.transport.actions.ack; import org.elasticsearch.action.ActionResponse; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xpack.core.watcher.watch.WatchStatus; import java.io.IOException; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/activate/ActivateWatchRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/activate/ActivateWatchRequest.java index ffe136daaf216..669ba097a94d6 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/activate/ActivateWatchRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/activate/ActivateWatchRequest.java @@ -63,7 +63,7 @@ public boolean isActivate() { @Override public ActionRequestValidationException validate() { ActionRequestValidationException validationException = null; - if (watchId == null){ + if (watchId == null) { validationException = ValidateActions.addValidationError("watch id is missing", validationException); } else if (WatcherUtils.isValidId(watchId) == false) { validationException = ValidateActions.addValidationError("watch id contains whitespace", validationException); @@ -73,8 +73,6 @@ public ActionRequestValidationException validate() { @Override public String toString() { - return activate ? - "activate [" + watchId + "]" : - "deactivate [" + watchId + "]"; + return activate ? "activate [" + watchId + "]" : "deactivate [" + watchId + "]"; } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/activate/ActivateWatchResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/activate/ActivateWatchResponse.java index aab8014673b61..8091ba3b5ca26 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/activate/ActivateWatchResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/activate/ActivateWatchResponse.java @@ -7,9 +7,9 @@ package org.elasticsearch.xpack.core.watcher.transport.actions.activate; import org.elasticsearch.action.ActionResponse; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xpack.core.watcher.watch.WatchStatus; import java.io.IOException; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/execute/ExecuteWatchRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/execute/ExecuteWatchRequest.java index 1baade7510974..9fe1a41be2a2a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/execute/ExecuteWatchRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/execute/ExecuteWatchRequest.java @@ -9,11 +9,11 @@ import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.ValidateActions; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.watcher.client.WatchSourceBuilder; import org.elasticsearch.xpack.core.watcher.execution.ActionExecutionMode; @@ -35,16 +35,17 @@ public class ExecuteWatchRequest extends ActionRequest { private String id; private boolean ignoreCondition = false; private boolean recordExecution = false; - @Nullable private Map triggerData = null; - @Nullable private Map alternativeInput = null; + @Nullable + private Map triggerData = null; + @Nullable + private Map alternativeInput = null; private Map actionModes = new HashMap<>(); private BytesReference watchSource; private XContentType xContentType = XContentType.JSON; private boolean debug = false; - public ExecuteWatchRequest() { - } + public ExecuteWatchRequest() {} /** * @param id the id of the watch to execute @@ -58,7 +59,7 @@ public ExecuteWatchRequest(StreamInput in) throws IOException { id = in.readOptionalString(); ignoreCondition = in.readBoolean(); recordExecution = in.readBoolean(); - if (in.readBoolean()){ + if (in.readBoolean()) { alternativeInput = in.readMap(); } if (in.readBoolean()) { @@ -244,9 +245,11 @@ public void setDebug(boolean debug) { @Override public ActionRequestValidationException validate() { ActionRequestValidationException validationException = null; - if (id == null && watchSource == null){ - validationException = ValidateActions.addValidationError("a watch execution request must either have a watch id or an inline " + - "watch source, but both are missing", validationException); + if (id == null && watchSource == null) { + validationException = ValidateActions.addValidationError( + "a watch execution request must either have a watch id or an inline " + "watch source, but both are missing", + validationException + ); } if (id != null && WatcherUtils.isValidId(id) == false) { validationException = ValidateActions.addValidationError("watch id contains whitespace", validationException); @@ -254,19 +257,27 @@ public ActionRequestValidationException validate() { for (String actionId : actionModes.keySet()) { if (actionId == null) { validationException = ValidateActions.addValidationError( - String.format(Locale.ROOT, "action id may not be null"), validationException); + String.format(Locale.ROOT, "action id may not be null"), + validationException + ); } else if (WatcherUtils.isValidId(actionId) == false) { validationException = ValidateActions.addValidationError( - String.format(Locale.ROOT, "action id [%s] contains whitespace", actionId), validationException); + String.format(Locale.ROOT, "action id [%s] contains whitespace", actionId), + validationException + ); } } if (watchSource != null && id != null) { - validationException = ValidateActions.addValidationError("a watch execution request must either have a watch id or an inline " + - "watch source but not both", validationException); + validationException = ValidateActions.addValidationError( + "a watch execution request must either have a watch id or an inline " + "watch source but not both", + validationException + ); } if (watchSource != null && recordExecution) { - validationException = ValidateActions.addValidationError("the execution of an inline watch cannot be recorded", - validationException); + validationException = ValidateActions.addValidationError( + "the execution of an inline watch cannot be recorded", + validationException + ); } return validationException; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/execute/ExecuteWatchResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/execute/ExecuteWatchResponse.java index 231495a273aab..7167759a8c0e5 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/execute/ExecuteWatchResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/execute/ExecuteWatchResponse.java @@ -7,11 +7,11 @@ package org.elasticsearch.xpack.core.watcher.transport.actions.execute; import org.elasticsearch.action.ActionResponse; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; @@ -49,8 +49,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; ExecuteWatchResponse that = (ExecuteWatchResponse) o; - return Objects.equals(recordId, that.recordId) && - Objects.equals(recordSource, that.recordSource); + return Objects.equals(recordId, that.recordId) && Objects.equals(recordSource, that.recordSource); } @Override @@ -93,9 +92,11 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws return builder; } - private static final ConstructingObjectParser PARSER - = new ConstructingObjectParser<>("x_pack_execute_watch_response", false, - (fields) -> new ExecuteWatchResponse((String)fields[0], (BytesReference) fields[1], XContentType.JSON)); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "x_pack_execute_watch_response", + false, + (fields) -> new ExecuteWatchResponse((String) fields[0], (BytesReference) fields[1], XContentType.JSON) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), ID_FIELD); PARSER.declareObject(ConstructingObjectParser.constructorArg(), (p, c) -> readBytesReference(p), WATCH_FIELD); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/get/GetWatchRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/get/GetWatchRequest.java index eb0cf03729643..8a21665f30b9d 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/get/GetWatchRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/get/GetWatchRequest.java @@ -22,8 +22,7 @@ public class GetWatchRequest extends ActionRequest { private String id; - public GetWatchRequest() { - } + public GetWatchRequest() {} /** * @param id name (id) of the watch to retrieve @@ -60,7 +59,6 @@ public ActionRequestValidationException validate() { return validationException; } - /** * @return The name of the watch to retrieve */ @@ -70,6 +68,6 @@ public String getId() { @Override public String toString() { - return "get [" + id +"]"; + return "get [" + id + "]"; } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/get/GetWatchRequestBuilder.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/get/GetWatchRequestBuilder.java index 535c7d0b9bfaf..978e4b9848b55 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/get/GetWatchRequestBuilder.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/get/GetWatchRequestBuilder.java @@ -18,7 +18,6 @@ public GetWatchRequestBuilder(ElasticsearchClient client, String id) { super(client, GetWatchAction.INSTANCE, new GetWatchRequest(id)); } - public GetWatchRequestBuilder(ElasticsearchClient client) { super(client, GetWatchAction.INSTANCE, new GetWatchRequest()); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/get/GetWatchResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/get/GetWatchResponse.java index 1d1bbce5c504f..d1da1cc490f4b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/get/GetWatchResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/get/GetWatchResponse.java @@ -11,9 +11,9 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.lucene.uid.Versions; +import org.elasticsearch.index.seqno.SequenceNumbers; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.index.seqno.SequenceNumbers; import org.elasticsearch.xpack.core.watcher.support.xcontent.XContentSource; import org.elasticsearch.xpack.core.watcher.watch.WatchStatus; @@ -125,7 +125,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.field("_version", version); builder.field("_seq_no", seqNo); builder.field("_primary_term", primaryTerm); - builder.field("status", status, params); + builder.field("status", status, params); builder.field("watch", source, params); } builder.endObject(); @@ -137,10 +137,12 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; GetWatchResponse that = (GetWatchResponse) o; - return version == that.version && seqNo == that.seqNo && primaryTerm == that.primaryTerm && - Objects.equals(id, that.id) && - Objects.equals(status, that.status) && - Objects.equals(source, that.source); + return version == that.version + && seqNo == that.seqNo + && primaryTerm == that.primaryTerm + && Objects.equals(id, that.id) + && Objects.equals(status, that.status) + && Objects.equals(source, that.source); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/put/PutWatchRequestBuilder.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/put/PutWatchRequestBuilder.java index a6433986b1496..3a7ffd61faf3f 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/put/PutWatchRequestBuilder.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/put/PutWatchRequestBuilder.java @@ -9,9 +9,9 @@ import org.elasticsearch.action.ActionRequestBuilder; import org.elasticsearch.client.ElasticsearchClient; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest; import org.elasticsearch.protocol.xpack.watcher.PutWatchResponse; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.watcher.client.WatchSourceBuilder; public class PutWatchRequestBuilder extends ActionRequestBuilder { @@ -28,7 +28,7 @@ public PutWatchRequestBuilder(ElasticsearchClient client, String id) { /** * @param id The watch id to be created */ - public PutWatchRequestBuilder setId(String id){ + public PutWatchRequestBuilder setId(String id) { request.setId(id); return this; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/service/WatcherServiceAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/service/WatcherServiceAction.java index 676120b7bcf8c..80af58d655dda 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/service/WatcherServiceAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/service/WatcherServiceAction.java @@ -9,7 +9,6 @@ import org.elasticsearch.action.ActionType; import org.elasticsearch.action.support.master.AcknowledgedResponse; - public class WatcherServiceAction extends ActionType { public static final WatcherServiceAction INSTANCE = new WatcherServiceAction(); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/service/WatcherServiceRequest.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/service/WatcherServiceRequest.java index 3ccbb7ddebb60..93cc7a18594d6 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/service/WatcherServiceRequest.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/service/WatcherServiceRequest.java @@ -17,7 +17,10 @@ public class WatcherServiceRequest extends MasterNodeRequest { - public enum Command { START, STOP } + public enum Command { + START, + STOP + } private Command command; @@ -26,8 +29,7 @@ public WatcherServiceRequest(StreamInput in) throws IOException { command = Command.valueOf(in.readString().toUpperCase(Locale.ROOT)); } - public WatcherServiceRequest() { - } + public WatcherServiceRequest() {} /** * Starts the watcher service if not already started. diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/service/WatcherServiceRequestBuilder.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/service/WatcherServiceRequestBuilder.java index 80d532e39094c..9f3408acdc64a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/service/WatcherServiceRequestBuilder.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/service/WatcherServiceRequestBuilder.java @@ -10,8 +10,10 @@ import org.elasticsearch.action.support.master.MasterNodeOperationRequestBuilder; import org.elasticsearch.client.ElasticsearchClient; -public class WatcherServiceRequestBuilder extends MasterNodeOperationRequestBuilder { +public class WatcherServiceRequestBuilder extends MasterNodeOperationRequestBuilder< + WatcherServiceRequest, + AcknowledgedResponse, + WatcherServiceRequestBuilder> { public WatcherServiceRequestBuilder(ElasticsearchClient client) { super(client, WatcherServiceAction.INSTANCE, new WatcherServiceRequest()); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/stats/WatcherStatsRequestBuilder.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/stats/WatcherStatsRequestBuilder.java index 6b4eba28bc200..5a4442148dc2a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/stats/WatcherStatsRequestBuilder.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/stats/WatcherStatsRequestBuilder.java @@ -12,8 +12,10 @@ /** * Watcher stats request builder. */ -public class WatcherStatsRequestBuilder extends NodesOperationRequestBuilder { +public class WatcherStatsRequestBuilder extends NodesOperationRequestBuilder< + WatcherStatsRequest, + WatcherStatsResponse, + WatcherStatsRequestBuilder> { public WatcherStatsRequestBuilder(ElasticsearchClient client) { super(client, WatcherStatsAction.INSTANCE, new WatcherStatsRequest()); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/stats/WatcherStatsResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/stats/WatcherStatsResponse.java index f351cde0d2c06..7a12b5803b1da 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/stats/WatcherStatsResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/stats/WatcherStatsResponse.java @@ -11,9 +11,9 @@ import org.elasticsearch.action.support.nodes.BaseNodesResponse; import org.elasticsearch.cluster.ClusterName; import org.elasticsearch.cluster.node.DiscoveryNode; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.watcher.WatcherMetadata; @@ -26,8 +26,7 @@ import java.util.List; import java.util.Locale; -public class WatcherStatsResponse extends BaseNodesResponse - implements ToXContentObject { +public class WatcherStatsResponse extends BaseNodesResponse implements ToXContentObject { private WatcherMetadata watcherMetadata; @@ -36,8 +35,12 @@ public WatcherStatsResponse(StreamInput in) throws IOException { watcherMetadata = new WatcherMetadata(in.readBoolean()); } - public WatcherStatsResponse(ClusterName clusterName, WatcherMetadata watcherMetadata, - List nodes, List failures) { + public WatcherStatsResponse( + ClusterName clusterName, + WatcherMetadata watcherMetadata, + List nodes, + List failures + ) { super(clusterName, nodes, failures); this.watcherMetadata = watcherMetadata; } @@ -207,10 +210,8 @@ public void writeTo(StreamOutput out) throws IOException { } } - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) - throws IOException { + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); builder.field("node_id", getNode().getId()); builder.field("watcher_state", watcherState.toString().toLowerCase(Locale.ROOT)); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/trigger/TriggerEvent.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/trigger/TriggerEvent.java index b6b0fb5461788..00efe462143dc 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/trigger/TriggerEvent.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/trigger/TriggerEvent.java @@ -46,12 +46,17 @@ public final Map data() { @Override public String toString() { - return new StringBuilder("[") - .append("name=[").append(jobName).append("],") - .append("triggered_time=[").append(triggeredTime).append("],") - .append("data=[").append(data).append("]") - .append("]") - .toString(); + return new StringBuilder("[").append("name=[") + .append(jobName) + .append("],") + .append("triggered_time=[") + .append(triggeredTime) + .append("],") + .append("data=[") + .append(data) + .append("]") + .append("]") + .toString(); } public void recordXContent(XContentBuilder builder, Params params) throws IOException { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/watch/Watch.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/watch/Watch.java index 1abccf46e2a20..743f86a3bffa3 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/watch/Watch.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/watch/Watch.java @@ -8,9 +8,9 @@ import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.index.seqno.SequenceNumbers; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.index.seqno.SequenceNumbers; import org.elasticsearch.xpack.core.watcher.actions.ActionStatus; import org.elasticsearch.xpack.core.watcher.actions.ActionWrapper; import org.elasticsearch.xpack.core.watcher.condition.ExecutableCondition; @@ -34,19 +34,31 @@ public class Watch implements ToXContentObject { private final Trigger trigger; private final ExecutableInput input; private final ExecutableCondition condition; - @Nullable private final ExecutableTransform transform; + @Nullable + private final ExecutableTransform transform; private final List actions; - @Nullable private final TimeValue throttlePeriod; - @Nullable private final Map metadata; + @Nullable + private final TimeValue throttlePeriod; + @Nullable + private final Map metadata; private final WatchStatus status; private final long sourceSeqNo; private final long sourcePrimaryTerm; - public Watch(String id, Trigger trigger, ExecutableInputinput, ExecutableCondition condition, - @Nullable ExecutableTransform transform, - @Nullable TimeValue throttlePeriod, List actions, @Nullable Map metadata, - WatchStatus status, long sourceSeqNo, long sourcePrimaryTerm) { + public Watch( + String id, + Trigger trigger, + ExecutableInput input, + ExecutableCondition condition, + @Nullable ExecutableTransform transform, + @Nullable TimeValue throttlePeriod, + List actions, + @Nullable Map metadata, + WatchStatus status, + long sourceSeqNo, + long sourcePrimaryTerm + ) { this.id = id; this.trigger = trigger; this.input = input; @@ -68,7 +80,9 @@ public Trigger trigger() { return trigger; } - public ExecutableInput input() { return input;} + public ExecutableInput input() { + return input; + } public ExecutableCondition condition() { return condition; @@ -157,8 +171,11 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.field(WatchField.TRANSFORM.getPreferredName()).startObject().field(transform.type(), transform, params).endObject(); } if (throttlePeriod != null) { - builder.humanReadableField(WatchField.THROTTLE_PERIOD.getPreferredName(), - WatchField.THROTTLE_PERIOD_HUMAN.getPreferredName(), throttlePeriod); + builder.humanReadableField( + WatchField.THROTTLE_PERIOD.getPreferredName(), + WatchField.THROTTLE_PERIOD_HUMAN.getPreferredName(), + throttlePeriod + ); } builder.startObject(WatchField.ACTIONS.getPreferredName()); for (ActionWrapper action : actions) { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/watch/WatchStatus.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/watch/WatchStatus.java index f186427a0a2d1..b6f48baffb594 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/watch/WatchStatus.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/watch/WatchStatus.java @@ -7,11 +7,11 @@ package org.elasticsearch.xpack.core.watcher.watch; import org.elasticsearch.ElasticsearchParseException; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -43,11 +43,16 @@ public class WatchStatus implements ToXContentObject, Writeable { private State state; - @Nullable private ExecutionState executionState; - @Nullable private ZonedDateTime lastChecked; - @Nullable private ZonedDateTime lastMetCondition; - @Nullable private long version; - @Nullable private Map headers; + @Nullable + private ExecutionState executionState; + @Nullable + private ZonedDateTime lastChecked; + @Nullable + private ZonedDateTime lastMetCondition; + @Nullable + private long version; + @Nullable + private Map headers; private Map actions; public WatchStatus(StreamInput in) throws IOException { @@ -76,8 +81,15 @@ public WatchStatus(ZonedDateTime now, Map actions) { this(-1, new State(true, now), null, null, null, actions, Collections.emptyMap()); } - public WatchStatus(long version, State state, ExecutionState executionState, ZonedDateTime lastChecked, ZonedDateTime lastMetCondition, - Map actions, Map headers) { + public WatchStatus( + long version, + State state, + ExecutionState executionState, + ZonedDateTime lastChecked, + ZonedDateTime lastMetCondition, + Map actions, + Map headers + ) { this.version = version; this.lastChecked = lastChecked; this.lastMetCondition = lastMetCondition; @@ -138,12 +150,12 @@ public boolean equals(Object o) { WatchStatus that = (WatchStatus) o; - return Objects.equals(lastChecked, that.lastChecked) && - Objects.equals(lastMetCondition, that.lastMetCondition) && - Objects.equals(version, that.version) && - Objects.equals(executionState, that.executionState) && - Objects.equals(actions, that.actions) && - Objects.equals(headers, that.headers); + return Objects.equals(lastChecked, that.lastChecked) + && Objects.equals(lastMetCondition, that.lastMetCondition) + && Objects.equals(version, that.version) + && Objects.equals(executionState, that.executionState) + && Objects.equals(actions, that.actions) + && Objects.equals(headers, that.headers); } @Override @@ -286,36 +298,56 @@ public static WatchStatus parse(String watchId, WatcherXContentParser parser) th try { state = State.parse(parser); } catch (ElasticsearchParseException e) { - throw new ElasticsearchParseException("could not parse watch status for [{}]. failed to parse field [{}]", - e, watchId, currentFieldName); + throw new ElasticsearchParseException( + "could not parse watch status for [{}]. failed to parse field [{}]", + e, + watchId, + currentFieldName + ); } } else if (Field.VERSION.match(currentFieldName, parser.getDeprecationHandler())) { if (token.isValue()) { version = parser.longValue(); } else { - throw new ElasticsearchParseException("could not parse watch status for [{}]. expecting field [{}] to hold a long " + - "value, found [{}] instead", watchId, currentFieldName, token); + throw new ElasticsearchParseException( + "could not parse watch status for [{}]. expecting field [{}] to hold a long " + "value, found [{}] instead", + watchId, + currentFieldName, + token + ); } } else if (Field.LAST_CHECKED.match(currentFieldName, parser.getDeprecationHandler())) { if (token.isValue()) { lastChecked = parseDate(currentFieldName, parser, ZoneOffset.UTC); } else { - throw new ElasticsearchParseException("could not parse watch status for [{}]. expecting field [{}] to hold a date " + - "value, found [{}] instead", watchId, currentFieldName, token); + throw new ElasticsearchParseException( + "could not parse watch status for [{}]. expecting field [{}] to hold a date " + "value, found [{}] instead", + watchId, + currentFieldName, + token + ); } } else if (Field.LAST_MET_CONDITION.match(currentFieldName, parser.getDeprecationHandler())) { if (token.isValue()) { lastMetCondition = parseDate(currentFieldName, parser, ZoneOffset.UTC); } else { - throw new ElasticsearchParseException("could not parse watch status for [{}]. expecting field [{}] to hold a date " + - "value, found [{}] instead", watchId, currentFieldName, token); + throw new ElasticsearchParseException( + "could not parse watch status for [{}]. expecting field [{}] to hold a date " + "value, found [{}] instead", + watchId, + currentFieldName, + token + ); } } else if (Field.EXECUTION_STATE.match(currentFieldName, parser.getDeprecationHandler())) { if (token.isValue()) { executionState = ExecutionState.resolve(parser.text()); } else { - throw new ElasticsearchParseException("could not parse watch status for [{}]. expecting field [{}] to hold a string " + - "value, found [{}] instead", watchId, currentFieldName, token); + throw new ElasticsearchParseException( + "could not parse watch status for [{}]. expecting field [{}] to hold a string " + "value, found [{}] instead", + watchId, + currentFieldName, + token + ); } } else if (Field.ACTIONS.match(currentFieldName, parser.getDeprecationHandler())) { actions = new HashMap<>(); @@ -329,8 +361,12 @@ public static WatchStatus parse(String watchId, WatcherXContentParser parser) th } } } else { - throw new ElasticsearchParseException("could not parse watch status for [{}]. expecting field [{}] to be an object, " + - "found [{}] instead", watchId, currentFieldName, token); + throw new ElasticsearchParseException( + "could not parse watch status for [{}]. expecting field [{}] to be an object, " + "found [{}] instead", + watchId, + currentFieldName, + token + ); } } else if (Field.HEADERS.match(currentFieldName, parser.getDeprecationHandler())) { if (token == XContentParser.Token.START_OBJECT) { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/cluster/metadata/IndexMetadataConversionTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/cluster/metadata/IndexMetadataConversionTests.java index fa320d59c5910..116793c1e83f9 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/cluster/metadata/IndexMetadataConversionTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/cluster/metadata/IndexMetadataConversionTests.java @@ -31,35 +31,44 @@ public void testConvertSearchableSnapshotSettings() { assertSame(indexMetadata, src); // A full_copy searchable snapshot (settings should be untouched) - src = newIndexMeta("foo", Settings.builder() - .put(IndexModule.INDEX_STORE_TYPE_SETTING.getKey(), SEARCHABLE_SNAPSHOT_STORE_TYPE) - .put(SearchableSnapshotsSettings.SNAPSHOT_PARTIAL_SETTING.getKey(), false) - .put("index.routing.allocation.include._tier", "data_hot") - .put("index.routing.allocation.exclude._tier", "data_warm") - .put("index.routing.allocation.require._tier", "data_hot") - .put("index.routing.allocation.include._tier_preference", "data_cold") - .build()); + src = newIndexMeta( + "foo", + Settings.builder() + .put(IndexModule.INDEX_STORE_TYPE_SETTING.getKey(), SEARCHABLE_SNAPSHOT_STORE_TYPE) + .put(SearchableSnapshotsSettings.SNAPSHOT_PARTIAL_SETTING.getKey(), false) + .put("index.routing.allocation.include._tier", "data_hot") + .put("index.routing.allocation.exclude._tier", "data_warm") + .put("index.routing.allocation.require._tier", "data_hot") + .put("index.routing.allocation.include._tier_preference", "data_cold") + .build() + ); indexMetadata = service.convertSharedCacheTierPreference(src); assertSame(indexMetadata, src); // A shared_cache searchable snapshot with valid settings (metadata should be untouched) - src = newIndexMeta("foo", Settings.builder() - .put(IndexModule.INDEX_STORE_TYPE_SETTING.getKey(), SEARCHABLE_SNAPSHOT_STORE_TYPE) - .put(SearchableSnapshotsSettings.SNAPSHOT_PARTIAL_SETTING.getKey(), false) - .put("index.routing.allocation.include._tier_preference", "data_frozen") - .build()); + src = newIndexMeta( + "foo", + Settings.builder() + .put(IndexModule.INDEX_STORE_TYPE_SETTING.getKey(), SEARCHABLE_SNAPSHOT_STORE_TYPE) + .put(SearchableSnapshotsSettings.SNAPSHOT_PARTIAL_SETTING.getKey(), false) + .put("index.routing.allocation.include._tier_preference", "data_frozen") + .build() + ); indexMetadata = service.convertSharedCacheTierPreference(src); assertSame(indexMetadata, src); // A shared_cache searchable snapshot (should have its settings converted) - src = newIndexMeta("foo", Settings.builder() - .put(IndexModule.INDEX_STORE_TYPE_SETTING.getKey(), SEARCHABLE_SNAPSHOT_STORE_TYPE) - .put(SearchableSnapshotsSettings.SNAPSHOT_PARTIAL_SETTING.getKey(), true) - .put("index.routing.allocation.include._tier", "data_hot") - .put("index.routing.allocation.exclude._tier", "data_warm") - .put("index.routing.allocation.require._tier", "data_hot") - .put("index.routing.allocation.include._tier_preference", "data_frozen,data_cold") - .build()); + src = newIndexMeta( + "foo", + Settings.builder() + .put(IndexModule.INDEX_STORE_TYPE_SETTING.getKey(), SEARCHABLE_SNAPSHOT_STORE_TYPE) + .put(SearchableSnapshotsSettings.SNAPSHOT_PARTIAL_SETTING.getKey(), true) + .put("index.routing.allocation.include._tier", "data_hot") + .put("index.routing.allocation.exclude._tier", "data_warm") + .put("index.routing.allocation.require._tier", "data_hot") + .put("index.routing.allocation.include._tier_preference", "data_frozen,data_cold") + .build() + ); indexMetadata = service.convertSharedCacheTierPreference(src); assertNotSame(indexMetadata, src); Settings newSettings = indexMetadata.getSettings(); @@ -71,12 +80,15 @@ public void testConvertSearchableSnapshotSettings() { public void testRemoveSingleTierAllocationFilters() { IndexMetadataVerifier service = getIndexMetadataVerifier(); - IndexMetadata src = newIndexMeta("foo", Settings.builder() - .put("index.routing.allocation.include._tier", "data_hot") - .put("index.routing.allocation.exclude._tier", "data_warm") - .put("index.routing.allocation.require._tier", "data_hot") - .put("index.routing.allocation.include._tier_preference", "data_cold") - .build()); + IndexMetadata src = newIndexMeta( + "foo", + Settings.builder() + .put("index.routing.allocation.include._tier", "data_hot") + .put("index.routing.allocation.exclude._tier", "data_warm") + .put("index.routing.allocation.require._tier", "data_hot") + .put("index.routing.allocation.include._tier_preference", "data_cold") + .build() + ); IndexMetadata indexMetadata = service.removeTierFiltering(src); assertNotSame(indexMetadata, src); @@ -86,10 +98,13 @@ public void testRemoveSingleTierAllocationFilters() { assertNull(newSettings.get("index.routing.allocation.require._tier")); assertThat(newSettings.get("index.routing.allocation.include._tier_preference"), equalTo("data_cold")); - src = newIndexMeta("foo", Settings.builder() - .put("index.routing.allocation.include._tier_preference", "data_cold") - .put("index.number_of_shards", randomIntBetween(1, 10)) - .build()); + src = newIndexMeta( + "foo", + Settings.builder() + .put("index.routing.allocation.include._tier_preference", "data_cold") + .put("index.number_of_shards", randomIntBetween(1, 10)) + .build() + ); indexMetadata = service.removeTierFiltering(src); assertSame(indexMetadata, src); } @@ -98,8 +113,8 @@ private IndexMetadataVerifier getIndexMetadataVerifier() { return new IndexMetadataVerifier( Settings.EMPTY, xContentRegistry(), - new MapperRegistry(Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap(), - MapperPlugin.NOOP_FIELD_FILTER), IndexScopedSettings.DEFAULT_SCOPED_SETTINGS, + new MapperRegistry(Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap(), MapperPlugin.NOOP_FIELD_FILTER), + IndexScopedSettings.DEFAULT_SCOPED_SETTINGS, null ); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/AbstractLicenseServiceTestCase.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/AbstractLicenseServiceTestCase.java index efbae3917ade4..eaddb3435ab84 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/AbstractLicenseServiceTestCase.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/AbstractLicenseServiceTestCase.java @@ -90,8 +90,13 @@ protected void setInitialState(License license, XPackLicenseState licenseState, } protected DiscoveryNode getLocalNode() { - return new DiscoveryNode("b", buildNewFakeTransportAddress(), singletonMap(XPackPlugin.XPACK_INSTALLED_NODE_ATTR, "true"), - emptySet(), Version.CURRENT); + return new DiscoveryNode( + "b", + buildNewFakeTransportAddress(), + singletonMap(XPackPlugin.XPACK_INSTALLED_NODE_ATTR, "true"), + emptySet(), + Version.CURRENT + ); } @After diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/AbstractLicensesIntegrationTestCase.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/AbstractLicensesIntegrationTestCase.java index 7f4dcc00b5293..2553bc65790bc 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/AbstractLicensesIntegrationTestCase.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/AbstractLicensesIntegrationTestCase.java @@ -11,8 +11,8 @@ import org.elasticsearch.cluster.ClusterStateUpdateTask; import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.Nullable; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.test.ESIntegTestCase; import org.elasticsearch.xpack.core.LocalStateCompositeXPackPlugin; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/ExpirationCallbackTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/ExpirationCallbackTests.java index 5301b30fc54e3..98af9ab713c36 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/ExpirationCallbackTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/ExpirationCallbackTests.java @@ -23,9 +23,11 @@ public void testPostExpirationDelay() throws Exception { NoopPostExpirationCallback post = new NoopPostExpirationCallback(min, max, frequency); long now = System.currentTimeMillis(); long expiryDate = now + expiryDuration.getMillis(); - assertThat(post.delay(expiryDate, now), + assertThat( + post.delay(expiryDate, now), // before license expiry - equalTo(TimeValue.timeValueMillis(expiryDuration.getMillis() + min.getMillis()))); + equalTo(TimeValue.timeValueMillis(expiryDuration.getMillis() + min.getMillis())) + ); assertThat(post.delay(expiryDate, expiryDate), equalTo(min)); // on license expiry int latestValidTriggerDelay = (int) (expiryDuration.getMillis() + max.getMillis()); int earliestValidTriggerDelay = (int) (expiryDuration.getMillis() + min.getMillis()); @@ -88,50 +90,66 @@ public void testPostExpirationScheduleTime() throws Exception { assertExpirationCallbackScheduleTime(pre, expiryDuration.millis(), latestValidTriggerDelay, earliestValidTriggerDelay); } - private void assertExpirationCallbackDelay(ExpirationCallback expirationCallback, long expiryDuration, - int latestValidTriggerDelay, int earliestValidTriggerDelay) { + private void assertExpirationCallbackDelay( + ExpirationCallback expirationCallback, + long expiryDuration, + int latestValidTriggerDelay, + int earliestValidTriggerDelay + ) { long now = System.currentTimeMillis(); long expiryDate = now + expiryDuration; // bounds assertThat(expirationCallback.delay(expiryDate, now + earliestValidTriggerDelay), equalTo(TimeValue.timeValueMillis(0))); assertThat(expirationCallback.delay(expiryDate, now + latestValidTriggerDelay), equalTo(TimeValue.timeValueMillis(0))); // in match - assertThat(expirationCallback.delay(expiryDate, - now + randomIntBetween(earliestValidTriggerDelay, latestValidTriggerDelay)), - equalTo(TimeValue.timeValueMillis(0))); + assertThat( + expirationCallback.delay(expiryDate, now + randomIntBetween(earliestValidTriggerDelay, latestValidTriggerDelay)), + equalTo(TimeValue.timeValueMillis(0)) + ); // out of bounds int deltaBeforeEarliestMatch = between(1, earliestValidTriggerDelay); - assertThat(expirationCallback.delay(expiryDate, now + deltaBeforeEarliestMatch), - equalTo(TimeValue.timeValueMillis(earliestValidTriggerDelay - deltaBeforeEarliestMatch))); + assertThat( + expirationCallback.delay(expiryDate, now + deltaBeforeEarliestMatch), + equalTo(TimeValue.timeValueMillis(earliestValidTriggerDelay - deltaBeforeEarliestMatch)) + ); int deltaAfterLatestMatch = between(latestValidTriggerDelay + 1, Integer.MAX_VALUE); // after expiry and after max assertThat(expirationCallback.delay(expiryDate, expiryDate + deltaAfterLatestMatch), nullValue()); } - public void assertExpirationCallbackScheduleTime(ExpirationCallback expirationCallback, long expiryDuration, - int latestValidTriggerDelay, int earliestValidTriggerDelay) { + public void assertExpirationCallbackScheduleTime( + ExpirationCallback expirationCallback, + long expiryDuration, + int latestValidTriggerDelay, + int earliestValidTriggerDelay + ) { long now = System.currentTimeMillis(); long expiryDate = now + expiryDuration; int validTriggerInterval = between(earliestValidTriggerDelay, latestValidTriggerDelay); - assertThat(expirationCallback.nextScheduledTimeForExpiry(expiryDate, - now + validTriggerInterval, now + validTriggerInterval), - equalTo(now + validTriggerInterval)); - assertThat(expirationCallback.nextScheduledTimeForExpiry(expiryDate, now, now + validTriggerInterval), - equalTo(now + validTriggerInterval + expirationCallback.getFrequency())); + assertThat( + expirationCallback.nextScheduledTimeForExpiry(expiryDate, now + validTriggerInterval, now + validTriggerInterval), + equalTo(now + validTriggerInterval) + ); + assertThat( + expirationCallback.nextScheduledTimeForExpiry(expiryDate, now, now + validTriggerInterval), + equalTo(now + validTriggerInterval + expirationCallback.getFrequency()) + ); int deltaBeforeEarliestMatch = between(1, earliestValidTriggerDelay - 1); - assertThat(expirationCallback.nextScheduledTimeForExpiry(expiryDate, now, now + deltaBeforeEarliestMatch), - equalTo(now + deltaBeforeEarliestMatch + - expirationCallback.delay(expiryDate, now + deltaBeforeEarliestMatch).getMillis())); - assertThat(expirationCallback.nextScheduledTimeForExpiry(expiryDate, - now + deltaBeforeEarliestMatch, now + deltaBeforeEarliestMatch), - equalTo(now + deltaBeforeEarliestMatch + - expirationCallback.delay(expiryDate, now + deltaBeforeEarliestMatch).getMillis())); + assertThat( + expirationCallback.nextScheduledTimeForExpiry(expiryDate, now, now + deltaBeforeEarliestMatch), + equalTo(now + deltaBeforeEarliestMatch + expirationCallback.delay(expiryDate, now + deltaBeforeEarliestMatch).getMillis()) + ); + assertThat( + expirationCallback.nextScheduledTimeForExpiry(expiryDate, now + deltaBeforeEarliestMatch, now + deltaBeforeEarliestMatch), + equalTo(now + deltaBeforeEarliestMatch + expirationCallback.delay(expiryDate, now + deltaBeforeEarliestMatch).getMillis()) + ); int deltaAfterLatestMatch = between(latestValidTriggerDelay + 1, Integer.MAX_VALUE); // after expiry and after max assertThat(expirationCallback.nextScheduledTimeForExpiry(expiryDate, now, now + deltaAfterLatestMatch), equalTo(-1L)); - assertThat(expirationCallback.nextScheduledTimeForExpiry(expiryDate, - now + deltaAfterLatestMatch, now + deltaAfterLatestMatch), - equalTo(-1L)); + assertThat( + expirationCallback.nextScheduledTimeForExpiry(expiryDate, now + deltaAfterLatestMatch, now + deltaAfterLatestMatch), + equalTo(-1L) + ); } private static class NoopPostExpirationCallback extends ExpirationCallback.Post { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseClusterChangeTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseClusterChangeTests.java index 56c43d66adb98..94b86446138af 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseClusterChangeTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseClusterChangeTests.java @@ -44,7 +44,6 @@ public void teardown() { licenseService.stop(); } - public void testNotificationOnNewLicense() throws Exception { ClusterState oldState = ClusterState.builder(new ClusterName("a")).build(); final License license = TestUtils.generateSignedLicense(TimeValue.timeValueHours(24)); @@ -67,7 +66,8 @@ public void testNoNotificationOnExistingLicense() throws Exception { public void testSelfGeneratedLicenseGeneration() throws Exception { DiscoveryNode master = new DiscoveryNode("b", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT); ClusterState oldState = ClusterState.builder(new ClusterName("a")) - .nodes(DiscoveryNodes.builder().masterNodeId(master.getId()).add(master)).build(); + .nodes(DiscoveryNodes.builder().masterNodeId(master.getId()).add(master)) + .build(); when(discoveryNodes.isLocalNodeElectedMaster()).thenReturn(true); ClusterState newState = ClusterState.builder(oldState).nodes(discoveryNodes).build(); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseFIPSTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseFIPSTests.java index 6a8a25d9226d3..bcf8c1c2e38fe 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseFIPSTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseFIPSTests.java @@ -59,8 +59,10 @@ public void testFIPSCheckWithoutAllowedLicense() throws Exception { licenseService.start(); PlainActionFuture responseFuture = new PlainActionFuture<>(); IllegalStateException e = expectThrows(IllegalStateException.class, () -> licenseService.registerLicense(request, responseFuture)); - assertThat(e.getMessage(), - containsString("Cannot install a [" + newLicense.operationMode() + "] license unless FIPS mode is disabled")); + assertThat( + e.getMessage(), + containsString("Cannot install a [" + newLicense.operationMode() + "] license unless FIPS mode is disabled") + ); licenseService.stop(); settings = Settings.builder() diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseOperationModeTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseOperationModeTests.java index 2ef4cc0f40e59..f8e6b6afe00df 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseOperationModeTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseOperationModeTests.java @@ -62,8 +62,7 @@ public void testResolveUnknown() { OperationMode.resolve(licenseType); fail(String.format(Locale.ROOT, "[%s] should not be recognized as an operation mode", type)); - } - catch (IllegalArgumentException e) { + } catch (IllegalArgumentException e) { assertThat(e.getMessage(), equalTo("unknown license type [" + type + "]")); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseOperationModeUpdateTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseOperationModeUpdateTests.java index 360335202759d..ffb6d76b6a180 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseOperationModeUpdateTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseOperationModeUpdateTests.java @@ -59,14 +59,14 @@ public void testLicenseOperationModeUpdate() throws Exception { public void testCloudInternalLicenseOperationModeUpdate() throws Exception { License license = License.builder() - .uid("id") - .expiryDate(0) - .issueDate(0) - .issuedTo("elasticsearch") - .issuer("issuer") - .type("cloud_internal") - .maxNodes(1) - .build(); + .uid("id") + .expiryDate(0) + .issueDate(0) + .issuedTo("elasticsearch") + .issuer("issuer") + .type("cloud_internal") + .maxNodes(1) + .build(); assertThat(license.operationMode(), equalTo(License.OperationMode.PLATINUM)); OperationModeFileWatcherTests.writeMode("gold", licenseModeFile); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseRegistrationTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseRegistrationTests.java index 9a6154e6aea2a..8170e346224a3 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseRegistrationTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseRegistrationTests.java @@ -38,8 +38,10 @@ public void testSelfGeneratedTrialLicense() throws Exception { assertNotNull(licenseMetadata.getLicense()); assertFalse(licenseMetadata.isEligibleForTrial()); assertEquals("trial", licenseMetadata.getLicense().type()); - assertEquals(clock.millis() + LicenseService.NON_BASIC_SELF_GENERATED_LICENSE_DURATION.millis(), - licenseMetadata.getLicense().expiryDate()); + assertEquals( + clock.millis() + LicenseService.NON_BASIC_SELF_GENERATED_LICENSE_DURATION.millis(), + licenseMetadata.getLicense().expiryDate() + ); } public void testSelfGeneratedBasicLicense() throws Exception { @@ -64,15 +66,15 @@ public void testNonSelfGeneratedBasicLicenseIsReplaced() throws Exception { long now = System.currentTimeMillis(); String uid = UUID.randomUUID().toString(); final License.Builder builder = License.builder() - .uid(uid) - .version(License.VERSION_CURRENT) - .expiryDate(dateMath("now+2h", now)) - .startDate(now) - .issueDate(now) - .type("basic") - .issuedTo("customer") - .issuer("elasticsearch") - .maxNodes(5); + .uid(uid) + .version(License.VERSION_CURRENT) + .expiryDate(dateMath("now+2h", now)) + .startDate(now) + .issueDate(now) + .type("basic") + .issuedTo("customer") + .issuer("elasticsearch") + .maxNodes(5); License license = TestUtils.generateSignedLicense(builder); XPackLicenseState licenseState = TestUtils.newTestLicenseState(); @@ -99,12 +101,12 @@ public void testExpiredSelfGeneratedBasicLicenseIsExtended() throws Exception { long now = System.currentTimeMillis(); String uid = UUID.randomUUID().toString(); License.Builder builder = License.builder() - .uid(uid) - .issuedTo("name") - .maxNodes(1000) - .issueDate(dateMath("now-10h", now)) - .type("basic") - .expiryDate(dateMath("now-2h", now)); + .uid(uid) + .issuedTo("name") + .maxNodes(1000) + .issueDate(dateMath("now-10h", now)) + .type("basic") + .expiryDate(dateMath("now-2h", now)); License license = SelfGeneratedLicense.create(builder, License.VERSION_CURRENT); XPackLicenseState licenseState = TestUtils.newTestLicenseState(); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseScheduleTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseScheduleTests.java index 3d157bfa68043..f1ecb66bb90ab 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseScheduleTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseScheduleTests.java @@ -30,14 +30,12 @@ public void setup() throws Exception { public void testExpiredLicenseSchedule() throws Exception { long triggeredTime = license.expiryDate() + randomIntBetween(1, 1000); - assertThat(schedule.nextScheduledTimeAfter(license.issueDate(), triggeredTime), - equalTo(-1L)); + assertThat(schedule.nextScheduledTimeAfter(license.issueDate(), triggeredTime), equalTo(-1L)); } public void testInvalidLicenseSchedule() throws Exception { long triggeredTime = license.issueDate() - randomIntBetween(1, 1000); - assertThat(schedule.nextScheduledTimeAfter(triggeredTime, triggeredTime), - equalTo(license.issueDate())); + assertThat(schedule.nextScheduledTimeAfter(triggeredTime, triggeredTime), equalTo(license.issueDate())); } public void testDailyWarningPeriod() { @@ -49,12 +47,18 @@ public void testDailyWarningPeriod() { long triggeredTime = nextOffset + randomLongBetween(1, millisInDay); long expectedTime = nextOffset + millisInDay; long scheduledTime = schedule.nextScheduledTimeAfter(triggeredTime, triggeredTime); - assertThat(String.format(Locale.ROOT,"Incorrect schedule:\nexpected [%s]\ngot [%s]\ntriggered [%s]\nexpiry [%s]", - DateTimeFormatter.ISO_INSTANT.format(Instant.ofEpochMilli(expectedTime)), - DateTimeFormatter.ISO_INSTANT.format(Instant.ofEpochMilli(scheduledTime)), - DateTimeFormatter.ISO_INSTANT.format(Instant.ofEpochMilli(triggeredTime)), - DateTimeFormatter.ISO_INSTANT.format(Instant.ofEpochMilli(license.expiryDate()))), - scheduledTime, equalTo(expectedTime)); + assertThat( + String.format( + Locale.ROOT, + "Incorrect schedule:\nexpected [%s]\ngot [%s]\ntriggered [%s]\nexpiry [%s]", + DateTimeFormatter.ISO_INSTANT.format(Instant.ofEpochMilli(expectedTime)), + DateTimeFormatter.ISO_INSTANT.format(Instant.ofEpochMilli(scheduledTime)), + DateTimeFormatter.ISO_INSTANT.format(Instant.ofEpochMilli(triggeredTime)), + DateTimeFormatter.ISO_INSTANT.format(Instant.ofEpochMilli(license.expiryDate())) + ), + scheduledTime, + equalTo(expectedTime) + ); warningOffset -= millisInDay; } while (warningOffset > 0); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseSerializationTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseSerializationTests.java index aaa672509ac15..a89042ed92c76 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseSerializationTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseSerializationTests.java @@ -8,12 +8,12 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; import java.nio.charset.StandardCharsets; import java.util.Collections; @@ -38,8 +38,10 @@ public void testSimpleIssueExpiryDate() throws Exception { public void testLicensesFields() throws Exception { TestUtils.LicenseSpec randomLicenseSpec = TestUtils.generateRandomLicenseSpec(License.VERSION_START); String licenseSpecsSource = TestUtils.generateLicenseSpecString(randomLicenseSpec); - final License fromSource = - License.fromSource(new BytesArray(licenseSpecsSource.getBytes(StandardCharsets.UTF_8)), XContentType.JSON); + final License fromSource = License.fromSource( + new BytesArray(licenseSpecsSource.getBytes(StandardCharsets.UTF_8)), + XContentType.JSON + ); TestUtils.assertLicenseSpec(randomLicenseSpec, fromSource); } @@ -106,12 +108,12 @@ public void testLicenseRestViewNonExpiringBasic() throws Exception { long now = System.currentTimeMillis(); License.Builder specBuilder = License.builder() - .uid(UUID.randomUUID().toString()) - .issuedTo("test") - .maxNodes(1000) - .issueDate(now) - .type("basic") - .expiryDate(LicenseService.BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS); + .uid(UUID.randomUUID().toString()) + .issuedTo("test") + .maxNodes(1000) + .issueDate(now) + .type("basic") + .expiryDate(LicenseService.BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS); License license = SelfGeneratedLicense.create(specBuilder, License.VERSION_CURRENT); XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); license.toXContent(builder, new ToXContent.MapParams(Collections.singletonMap(License.REST_VIEW_MODE, "true"))); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseServiceClusterTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseServiceClusterTests.java index 19787cb580ff7..822601dad2a46 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseServiceClusterTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseServiceClusterTests.java @@ -78,7 +78,6 @@ public void testClusterRestartWithLicense() throws Exception { assertTrue(License.LicenseType.isBasic(licensingClient.prepareGetLicense().get().license().type())); assertOperationMode(License.OperationMode.BASIC); - wipeAllLicenses(); } @@ -92,8 +91,12 @@ public void testCloudInternalLicense() throws Exception { logger.info("--> put signed license"); LicensingClient licensingClient = new LicensingClient(client()); - License license = TestUtils.generateSignedLicense("cloud_internal", License.VERSION_CURRENT, System.currentTimeMillis(), - TimeValue.timeValueMinutes(1)); + License license = TestUtils.generateSignedLicense( + "cloud_internal", + License.VERSION_CURRENT, + System.currentTimeMillis(), + TimeValue.timeValueMinutes(1) + ); putLicense(license); assertThat(licensingClient.prepareGetLicense().get().license(), equalTo(license)); assertOperationMode(License.OperationMode.PLATINUM); @@ -144,7 +147,7 @@ public void testClusterRestartWithOldSignature() throws Exception { logger.info("--> await node for enabled"); assertLicenseActive(true); licensingClient = new LicensingClient(client()); - assertThat(licensingClient.prepareGetLicense().get().license().version(), equalTo(License.VERSION_CURRENT)); //license updated + assertThat(licensingClient.prepareGetLicense().get().license().version(), equalTo(License.VERSION_CURRENT)); // license updated internalCluster().fullRestart(); // restart once more and verify updated license is active ensureYellow(); logger.info("--> await node for enabled"); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseServiceTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseServiceTests.java index cc5b40dc1a120..8736dde481a5d 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseServiceTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseServiceTests.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.license; - import org.elasticsearch.action.support.PlainActionFuture; import org.elasticsearch.cluster.AckedClusterStateUpdateTask; import org.elasticsearch.cluster.ClusterState; @@ -16,10 +15,6 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.env.Environment; import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.license.licensor.LicenseSigner; @@ -29,6 +24,10 @@ import org.elasticsearch.test.TestMatchers; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.watcher.ResourceWatcherService; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentType; import org.hamcrest.Matchers; import org.mockito.ArgumentCaptor; import org.mockito.Mockito; @@ -77,8 +76,10 @@ public void testLogExpirationWarning() { * Tests loading a license when {@link LicenseService#ALLOWED_LICENSE_TYPES_SETTING} is on its default value (all license types) */ public void testRegisterLicenseWithoutTypeRestrictions() throws Exception { - assertRegisterValidLicense(Settings.EMPTY, - randomValueOtherThan(License.LicenseType.BASIC, () -> randomFrom(License.LicenseType.values()))); + assertRegisterValidLicense( + Settings.EMPTY, + randomValueOtherThan(License.LicenseType.BASIC, () -> randomFrom(License.LicenseType.values())) + ); } /** @@ -87,11 +88,11 @@ public void testRegisterLicenseWithoutTypeRestrictions() throws Exception { */ public void testSuccessfullyRegisterLicenseMatchingTypeRestrictions() throws Exception { final List allowed = randomSubsetOf( - randomIntBetween(1, LicenseService.ALLOWABLE_UPLOAD_TYPES.size() - 1), LicenseService.ALLOWABLE_UPLOAD_TYPES); + randomIntBetween(1, LicenseService.ALLOWABLE_UPLOAD_TYPES.size() - 1), + LicenseService.ALLOWABLE_UPLOAD_TYPES + ); final List allowedNames = allowed.stream().map(License.LicenseType::getTypeName).collect(Collectors.toUnmodifiableList()); - final Settings settings = Settings.builder() - .putList("xpack.license.upload.types", allowedNames) - .build(); + final Settings settings = Settings.builder().putList("xpack.license.upload.types", allowedNames).build(); assertRegisterValidLicense(settings, randomFrom(allowed)); } @@ -101,32 +102,39 @@ public void testSuccessfullyRegisterLicenseMatchingTypeRestrictions() throws Exc */ public void testFailToRegisterLicenseNotMatchingTypeRestrictions() throws Exception { final List allowed = randomSubsetOf( - randomIntBetween(1, LicenseService.ALLOWABLE_UPLOAD_TYPES.size() - 2), LicenseService.ALLOWABLE_UPLOAD_TYPES); + randomIntBetween(1, LicenseService.ALLOWABLE_UPLOAD_TYPES.size() - 2), + LicenseService.ALLOWABLE_UPLOAD_TYPES + ); final List allowedNames = allowed.stream().map(License.LicenseType::getTypeName).collect(Collectors.toUnmodifiableList()); - final Settings settings = Settings.builder() - .putList("xpack.license.upload.types", allowedNames) - .build(); + final Settings settings = Settings.builder().putList("xpack.license.upload.types", allowedNames).build(); final License.LicenseType notAllowed = randomValueOtherThanMany( test -> allowed.contains(test), - () -> randomFrom(LicenseService.ALLOWABLE_UPLOAD_TYPES)); + () -> randomFrom(LicenseService.ALLOWABLE_UPLOAD_TYPES) + ); assertRegisterDisallowedLicenseType(settings, notAllowed); } private void assertRegisterValidLicense(Settings baseSettings, License.LicenseType licenseType) throws IOException { - tryRegisterLicense(baseSettings, licenseType, - future -> assertThat(future.actionGet().status(), equalTo(LicensesStatus.VALID))); + tryRegisterLicense(baseSettings, licenseType, future -> assertThat(future.actionGet().status(), equalTo(LicensesStatus.VALID))); } private void assertRegisterDisallowedLicenseType(Settings baseSettings, License.LicenseType licenseType) throws IOException { tryRegisterLicense(baseSettings, licenseType, future -> { final IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, future::actionGet); - assertThat(exception, TestMatchers.throwableWithMessage( - "Registering [" + licenseType.getTypeName() + "] licenses is not allowed on " + "this cluster")); + assertThat( + exception, + TestMatchers.throwableWithMessage( + "Registering [" + licenseType.getTypeName() + "] licenses is not allowed on " + "this cluster" + ) + ); }); } - private void tryRegisterLicense(Settings baseSettings, License.LicenseType licenseType, - Consumer> assertion) throws IOException { + private void tryRegisterLicense( + Settings baseSettings, + License.LicenseType licenseType, + Consumer> assertion + ) throws IOException { final Settings settings = Settings.builder() .put(baseSettings) .put("path.home", createTempDir()) @@ -144,8 +152,15 @@ private void tryRegisterLicense(Settings baseSettings, License.LicenseType licen final ResourceWatcherService resourceWatcherService = mock(ResourceWatcherService.class); final XPackLicenseState licenseState = mock(XPackLicenseState.class); final ThreadPool threadPool = mock(ThreadPool.class); - final LicenseService service = - new LicenseService(settings, threadPool, clusterService, clock, env, resourceWatcherService, licenseState); + final LicenseService service = new LicenseService( + settings, + threadPool, + clusterService, + clock, + env, + resourceWatcherService, + licenseState + ); final PutLicenseRequest request = new PutLicenseRequest(); request.license(spec(licenseType, TimeValue.timeValueDays(randomLongBetween(1, 1000))), XContentType.JSON); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseTLSTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseTLSTests.java index bf12468213b39..5c42a20bb8bc0 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseTLSTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseTLSTests.java @@ -43,10 +43,7 @@ public void testApplyLicenseInDevMode() throws Exception { verify(clusterService).submitStateUpdateTask(any(String.class), any(ClusterStateUpdateTask.class)); inetAddress = TransportAddress.META_ADDRESS; - settings = Settings.builder() - .put("xpack.security.enabled", true) - .put("discovery.type", "single-node") - .build(); + settings = Settings.builder().put("xpack.security.enabled", true).put("discovery.type", "single-node").build(); licenseService.stop(); licenseState = new XPackLicenseState(() -> 0); setInitialState(null, licenseState, settings); @@ -57,7 +54,12 @@ public void testApplyLicenseInDevMode() throws Exception { @Override protected DiscoveryNode getLocalNode() { - return new DiscoveryNode("localnode", new TransportAddress(inetAddress, randomIntBetween(9300, 9399)), - emptyMap(), emptySet(), Version.CURRENT); + return new DiscoveryNode( + "localnode", + new TransportAddress(inetAddress, randomIntBetween(9300, 9399)), + emptyMap(), + emptySet(), + Version.CURRENT + ); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseTests.java index e118eac3a38c1..6ba5d562a35eb 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseTests.java @@ -13,19 +13,19 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.TestMatchers; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.test.TestMatchers; import org.hamcrest.Matchers; import java.nio.BufferUnderflowException; import java.nio.charset.StandardCharsets; import java.util.concurrent.TimeUnit; -import static org.elasticsearch.xcontent.DeprecationHandler.THROW_UNSUPPORTED_OPERATION; import static org.elasticsearch.test.TestMatchers.throwableWithMessage; +import static org.elasticsearch.xcontent.DeprecationHandler.THROW_UNSUPPORTED_OPERATION; import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.instanceOf; @@ -34,23 +34,22 @@ public class LicenseTests extends ESTestCase { public void testFromXContentForGoldLicenseWithVersion2Signature() throws Exception { - String licenseString = "{\"license\":" + - "{\"uid\":\"4056779d-b823-4c12-a9cb-efa4a8d8c422\"," + - "\"type\":\"gold\"," + - "\"issue_date_in_millis\":1546589020459," + - "\"expiry_date_in_millis\":1546596340459," + - "\"max_nodes\":5," + - "\"issued_to\":\"customer\"," + - "\"issuer\":\"elasticsearch\"," + - "\"signature\":\"AAAAAgAAAA34V2kfTJVtvdL2LttwAAABmFJ6NGRnbEM3WVQrZVQwNkdKQmR1VytlMTMyM1J0dTZ1WGwyY2ZCVFhqMGtJU2gzZ3pnNTVpOW" + - "F5Y1NaUkwyN2VsTEtCYnlZR2c5WWtjQ0phaDlhRjlDUXViUmUwMWhjSkE2TFcwSGdneTJHbUV4N2RHUWJxV20ybjRsZHRzV2xkN0ZmdDlYblJmNVcxMlBWeU81" + - "V1hLUm1EK0V1dmF3cFdlSGZzTU5SZE1qUmFra3JkS1hCanBWVmVTaFFwV3BVZERzeG9Sci9rYnlJK2toODZXY09tNmFHUVNUL3IyUHExV3VSTlBneWNJcFQ0bX" + - "l0cmhNNnRwbE1CWE4zWjJ5eGFuWFo0NGhsb3B5WFd1eTdYbFFWQkxFVFFPSlBERlB0eVVJYXVSZ0lsR2JpRS9rN1h4MSsvNUpOcGN6cU1NOHN1cHNtSTFIUGN1" + - "bWNGNEcxekhrblhNOXZ2VEQvYmRzQUFwbytUZEpRR3l6QU5oS2ZFSFdSbGxxNDZyZ0xvUHIwRjdBL2JqcnJnNGFlK09Cek9pYlJ5Umc9PQAAAQAth77fQLF7CC" + - "EL7wA6Z0/UuRm/weECcsjW/50kBnPLO8yEs+9/bPa5LSU0bF6byEXOVeO0ebUQfztpjulbXh8TrBDSG+6VdxGtohPo2IYPBaXzGs3LOOor6An/lhptxBWdwYmf" + - "bcp0m8mnXZh1vN9rmbTsZXnhBIoPTaRDwUBi3vJ3Ms3iLaEm4S8Slrfmtht2jUjgGZ2vAeZ9OHU2YsGtrSpz6f\"}"; - License license = License.fromSource(new BytesArray(licenseString.getBytes(StandardCharsets.UTF_8)), - XContentType.JSON); + String licenseString = "{\"license\":" + + "{\"uid\":\"4056779d-b823-4c12-a9cb-efa4a8d8c422\"," + + "\"type\":\"gold\"," + + "\"issue_date_in_millis\":1546589020459," + + "\"expiry_date_in_millis\":1546596340459," + + "\"max_nodes\":5," + + "\"issued_to\":\"customer\"," + + "\"issuer\":\"elasticsearch\"," + + "\"signature\":\"AAAAAgAAAA34V2kfTJVtvdL2LttwAAABmFJ6NGRnbEM3WVQrZVQwNkdKQmR1VytlMTMyM1J0dTZ1WGwyY2ZCVFhqMGtJU2gzZ3pnNTVpOW" + + "F5Y1NaUkwyN2VsTEtCYnlZR2c5WWtjQ0phaDlhRjlDUXViUmUwMWhjSkE2TFcwSGdneTJHbUV4N2RHUWJxV20ybjRsZHRzV2xkN0ZmdDlYblJmNVcxMlBWeU81" + + "V1hLUm1EK0V1dmF3cFdlSGZzTU5SZE1qUmFra3JkS1hCanBWVmVTaFFwV3BVZERzeG9Sci9rYnlJK2toODZXY09tNmFHUVNUL3IyUHExV3VSTlBneWNJcFQ0bX" + + "l0cmhNNnRwbE1CWE4zWjJ5eGFuWFo0NGhsb3B5WFd1eTdYbFFWQkxFVFFPSlBERlB0eVVJYXVSZ0lsR2JpRS9rN1h4MSsvNUpOcGN6cU1NOHN1cHNtSTFIUGN1" + + "bWNGNEcxekhrblhNOXZ2VEQvYmRzQUFwbytUZEpRR3l6QU5oS2ZFSFdSbGxxNDZyZ0xvUHIwRjdBL2JqcnJnNGFlK09Cek9pYlJ5Umc9PQAAAQAth77fQLF7CC" + + "EL7wA6Z0/UuRm/weECcsjW/50kBnPLO8yEs+9/bPa5LSU0bF6byEXOVeO0ebUQfztpjulbXh8TrBDSG+6VdxGtohPo2IYPBaXzGs3LOOor6An/lhptxBWdwYmf" + + "bcp0m8mnXZh1vN9rmbTsZXnhBIoPTaRDwUBi3vJ3Ms3iLaEm4S8Slrfmtht2jUjgGZ2vAeZ9OHU2YsGtrSpz6f\"}"; + License license = License.fromSource(new BytesArray(licenseString.getBytes(StandardCharsets.UTF_8)), XContentType.JSON); assertThat(license.type(), equalTo("gold")); assertThat(license.uid(), equalTo("4056779d-b823-4c12-a9cb-efa4a8d8c422")); assertThat(license.issuer(), equalTo("elasticsearch")); @@ -63,21 +62,20 @@ public void testFromXContentForGoldLicenseWithVersion2Signature() throws Excepti } public void testFromXContentForGoldLicenseWithVersion4Signature() throws Exception { - String licenseString = "{\"license\":{" + - "\"uid\":\"4056779d-b823-4c12-a9cb-efa4a8d8c422\"," + - "\"type\":\"gold\"," + - "\"issue_date_in_millis\":1546589020459," + - "\"expiry_date_in_millis\":1546596340459," + - "\"max_nodes\":5," + - "\"issued_to\":\"customer\"," + - "\"issuer\":\"elasticsearch\"," + - "\"signature\":\"AAAABAAAAA22vXffI41oM4jLCwZ6AAAAIAo5/x6hrsGh1GqqrJmy4qgmEC7gK0U4zQ6q5ZEMhm4jAAABAH3oL4weubwYGjLGNZsz90" + - "EerX6yOX3Dh6wswG9EfqCiyv6lcjuC7aeKKuOkqhMRTHZ9vHnfMuakHWVlpuGC14WyGqaMwSmgTZ9jVAzt/W3sIotRxM/3rtlCXUc1rOUXNFcii1i3Kkrc" + - "kTzhENTKjdkOmUN3qZlTEmHkp93eYpx8++iIukHYU9K9Vm2VKgydFfxvYaN/Qr+iPfJSbHJB8+DmS2ywdrmdqW+ScE+1ZNouPNhnP3RKTleNvixXPG9l5B" + - "qZ2So1IlCrxVDByA1E6JH5AvjbOucpcGiWCm7IzvfpkzphKHMyxhUaIByoHl9UAf4AdPLhowWAQk0eHMRDDlo=\"," + - "\"start_date_in_millis\":-1}}\n"; - License license = License.fromSource(new BytesArray(licenseString.getBytes(StandardCharsets.UTF_8)), - XContentType.JSON); + String licenseString = "{\"license\":{" + + "\"uid\":\"4056779d-b823-4c12-a9cb-efa4a8d8c422\"," + + "\"type\":\"gold\"," + + "\"issue_date_in_millis\":1546589020459," + + "\"expiry_date_in_millis\":1546596340459," + + "\"max_nodes\":5," + + "\"issued_to\":\"customer\"," + + "\"issuer\":\"elasticsearch\"," + + "\"signature\":\"AAAABAAAAA22vXffI41oM4jLCwZ6AAAAIAo5/x6hrsGh1GqqrJmy4qgmEC7gK0U4zQ6q5ZEMhm4jAAABAH3oL4weubwYGjLGNZsz90" + + "EerX6yOX3Dh6wswG9EfqCiyv6lcjuC7aeKKuOkqhMRTHZ9vHnfMuakHWVlpuGC14WyGqaMwSmgTZ9jVAzt/W3sIotRxM/3rtlCXUc1rOUXNFcii1i3Kkrc" + + "kTzhENTKjdkOmUN3qZlTEmHkp93eYpx8++iIukHYU9K9Vm2VKgydFfxvYaN/Qr+iPfJSbHJB8+DmS2ywdrmdqW+ScE+1ZNouPNhnP3RKTleNvixXPG9l5B" + + "qZ2So1IlCrxVDByA1E6JH5AvjbOucpcGiWCm7IzvfpkzphKHMyxhUaIByoHl9UAf4AdPLhowWAQk0eHMRDDlo=\"," + + "\"start_date_in_millis\":-1}}\n"; + License license = License.fromSource(new BytesArray(licenseString.getBytes(StandardCharsets.UTF_8)), XContentType.JSON); assertThat(license.type(), equalTo("gold")); assertThat(license.uid(), equalTo("4056779d-b823-4c12-a9cb-efa4a8d8c422")); assertThat(license.issuer(), equalTo("elasticsearch")); @@ -90,22 +88,21 @@ public void testFromXContentForGoldLicenseWithVersion4Signature() throws Excepti } public void testFromXContentForEnterpriseLicenseWithV5Signature() throws Exception { - String licenseString = "{\"license\":{" + - "\"uid\":\"4056779d-b823-4c12-a9cb-efa4a8d8c422\"," + - "\"type\":\"enterprise\"," + - "\"issue_date_in_millis\":1546589020459," + - "\"expiry_date_in_millis\":1546596340459," + - "\"max_nodes\":null," + - "\"max_resource_units\":15," + - "\"issued_to\":\"customer\"," + - "\"issuer\":\"elasticsearch\"," + - "\"signature\":\"AAAABQAAAA2MUoEqXb9K9Ie5d6JJAAAAIAo5/x6hrsGh1GqqrJmy4qgmEC7gK0U4zQ6q5ZEMhm4jAAABAAAwVZKGAmDELUlS5PScBkhQsZa" + - "DaQTtJ4ZP5EnZ/nLpmCt9Dj7d/FRsgMtHmSJLrr2CdrIo4Vx5VuhmbwzZvXMttLz2lrJzG7770PX3TnC9e7F9GdnE9ec0FP2U0ZlLOBOtPuirX0q+j6GfB+DLyE" + - "5D+Lo1NQ3eLJGvbd3DBYPWJxkb+EBVHczCH2OrIEVWnN/TafmkdZCPX5PcultkNOs3j7d3s7b51EXHKoye8UTcB/RGmzZwMah+E6I/VJkqu7UHL8bB01wJeqo6W" + - "xI4LC/9+f5kpmHrUu3CHe5pHbmMGDk7O6/cwt1pw/hnJXKIFCi36IGaKcHLgORxQdN0uzE=\"," + - "\"start_date_in_millis\":-1}}"; - License license = License.fromSource(new BytesArray(licenseString.getBytes(StandardCharsets.UTF_8)), - XContentType.JSON); + String licenseString = "{\"license\":{" + + "\"uid\":\"4056779d-b823-4c12-a9cb-efa4a8d8c422\"," + + "\"type\":\"enterprise\"," + + "\"issue_date_in_millis\":1546589020459," + + "\"expiry_date_in_millis\":1546596340459," + + "\"max_nodes\":null," + + "\"max_resource_units\":15," + + "\"issued_to\":\"customer\"," + + "\"issuer\":\"elasticsearch\"," + + "\"signature\":\"AAAABQAAAA2MUoEqXb9K9Ie5d6JJAAAAIAo5/x6hrsGh1GqqrJmy4qgmEC7gK0U4zQ6q5ZEMhm4jAAABAAAwVZKGAmDELUlS5PScBkhQsZa" + + "DaQTtJ4ZP5EnZ/nLpmCt9Dj7d/FRsgMtHmSJLrr2CdrIo4Vx5VuhmbwzZvXMttLz2lrJzG7770PX3TnC9e7F9GdnE9ec0FP2U0ZlLOBOtPuirX0q+j6GfB+DLyE" + + "5D+Lo1NQ3eLJGvbd3DBYPWJxkb+EBVHczCH2OrIEVWnN/TafmkdZCPX5PcultkNOs3j7d3s7b51EXHKoye8UTcB/RGmzZwMah+E6I/VJkqu7UHL8bB01wJeqo6W" + + "xI4LC/9+f5kpmHrUu3CHe5pHbmMGDk7O6/cwt1pw/hnJXKIFCi36IGaKcHLgORxQdN0uzE=\"," + + "\"start_date_in_millis\":-1}}"; + License license = License.fromSource(new BytesArray(licenseString.getBytes(StandardCharsets.UTF_8)), XContentType.JSON); assertThat(license.type(), equalTo("enterprise")); assertThat(license.uid(), equalTo("4056779d-b823-4c12-a9cb-efa4a8d8c422")); assertThat(license.issuer(), equalTo("elasticsearch")); @@ -118,36 +115,35 @@ public void testFromXContentForEnterpriseLicenseWithV5Signature() throws Excepti } public void testThatEnterpriseLicenseMayNotHaveMaxNodes() throws Exception { - License.Builder builder = randomLicense(License.LicenseType.ENTERPRISE) - .maxNodes(randomIntBetween(1, 50)) + License.Builder builder = randomLicense(License.LicenseType.ENTERPRISE).maxNodes(randomIntBetween(1, 50)) .maxResourceUnits(randomIntBetween(10, 500)); final IllegalStateException ex = expectThrows(IllegalStateException.class, builder::build); assertThat(ex, TestMatchers.throwableWithMessage("maxNodes may not be set for enterprise licenses (type=[enterprise])")); } public void testThatEnterpriseLicenseMustHaveMaxResourceUnits() throws Exception { - License.Builder builder = randomLicense(License.LicenseType.ENTERPRISE) - .maxResourceUnits(-1); + License.Builder builder = randomLicense(License.LicenseType.ENTERPRISE).maxResourceUnits(-1); final IllegalStateException ex = expectThrows(IllegalStateException.class, builder::build); assertThat(ex, TestMatchers.throwableWithMessage("maxResourceUnits must be set for enterprise licenses (type=[enterprise])")); } public void testThatRegularLicensesMustHaveMaxNodes() throws Exception { License.LicenseType type = randomValueOtherThan(License.LicenseType.ENTERPRISE, () -> randomFrom(License.LicenseType.values())); - License.Builder builder = randomLicense(type) - .maxNodes(-1); + License.Builder builder = randomLicense(type).maxNodes(-1); final IllegalStateException ex = expectThrows(IllegalStateException.class, builder::build); assertThat(ex, TestMatchers.throwableWithMessage("maxNodes has to be set")); } public void testThatRegularLicensesMayNotHaveMaxResourceUnits() throws Exception { License.LicenseType type = randomValueOtherThan(License.LicenseType.ENTERPRISE, () -> randomFrom(License.LicenseType.values())); - License.Builder builder = randomLicense(type) - .maxResourceUnits(randomIntBetween(10, 500)) - .maxNodes(randomIntBetween(1, 50)); + License.Builder builder = randomLicense(type).maxResourceUnits(randomIntBetween(10, 500)).maxNodes(randomIntBetween(1, 50)); final IllegalStateException ex = expectThrows(IllegalStateException.class, builder::build); - assertThat(ex, TestMatchers.throwableWithMessage("maxResourceUnits may only be set for enterprise licenses (not permitted " + - "for type=[" + type.getTypeName() + "])")); + assertThat( + ex, + TestMatchers.throwableWithMessage( + "maxResourceUnits may only be set for enterprise licenses (not permitted " + "for type=[" + type.getTypeName() + "])" + ) + ); } public void testLicenseToAndFromXContentForEveryLicenseType() throws Exception { @@ -162,8 +158,8 @@ public void testLicenseToAndFromXContentForEveryLicenseType() throws Exception { + "HLgORxQdN0uzE=" ) .build(); - XContentParser parser = XContentType.JSON.xContent().createParser(NamedXContentRegistry.EMPTY, THROW_UNSUPPORTED_OPERATION, - Strings.toString(license1)); + XContentParser parser = XContentType.JSON.xContent() + .createParser(NamedXContentRegistry.EMPTY, THROW_UNSUPPORTED_OPERATION, Strings.toString(license1)); License license2 = License.fromXContent(parser); assertThat(license2, notNullValue()); assertThat(license2.type(), equalTo(type.getTypeName())); @@ -222,44 +218,42 @@ public void testSerializationOfLicenseForEveryLicenseType() throws Exception { public void testNotEnoughBytesFromXContent() throws Exception { - String licenseString = "{\"license\": " + - "{\"uid\":\"4056779d-b823-4c12-a9cb-efa4a8d8c422\"," + - "\"type\":\"gold\"," + - "\"issue_date_in_millis\":1546589020459," + - "\"expiry_date_in_millis\":1546596340459," + - "\"max_nodes\":5," + - "\"issued_to\":\"customer\"," + - "\"issuer\":\"elasticsearch\"," + - "\"signature\":\"AA\"}" + - "}"; - ElasticsearchException exception = - expectThrows(ElasticsearchException.class, - () -> { - License.fromSource(new BytesArray(licenseString.getBytes(StandardCharsets.UTF_8)), - XContentType.JSON); - }); + String licenseString = "{\"license\": " + + "{\"uid\":\"4056779d-b823-4c12-a9cb-efa4a8d8c422\"," + + "\"type\":\"gold\"," + + "\"issue_date_in_millis\":1546589020459," + + "\"expiry_date_in_millis\":1546596340459," + + "\"max_nodes\":5," + + "\"issued_to\":\"customer\"," + + "\"issuer\":\"elasticsearch\"," + + "\"signature\":\"AA\"}" + + "}"; + ElasticsearchException exception = expectThrows( + ElasticsearchException.class, + () -> { License.fromSource(new BytesArray(licenseString.getBytes(StandardCharsets.UTF_8)), XContentType.JSON); } + ); assertThat(exception.getMessage(), containsString("malformed signature for license [4056779d-b823-4c12-a9cb-efa4a8d8c422]")); assertThat(exception.getCause(), instanceOf(BufferUnderflowException.class)); } public void testMalformedSignatureFromXContent() throws Exception { - String licenseString = "{\"license\": " + - "{\"uid\":\"4056779d-b823-4c12-a9cb-efa4a8d8c422\"," + - "\"type\":\"gold\"," + - "\"issue_date_in_millis\":1546589020459," + - "\"expiry_date_in_millis\":1546596340459," + - "\"max_nodes\":5," + - "\"issued_to\":\"customer\"," + - "\"issuer\":\"elasticsearch\"," + - "\"signature\":\"" + randomAlphaOfLength(10) + "\"}" + - "}"; - ElasticsearchException exception = - expectThrows(ElasticsearchException.class, - () -> { - License.fromSource(new BytesArray(licenseString.getBytes(StandardCharsets.UTF_8)), - XContentType.JSON); - }); + String licenseString = "{\"license\": " + + "{\"uid\":\"4056779d-b823-4c12-a9cb-efa4a8d8c422\"," + + "\"type\":\"gold\"," + + "\"issue_date_in_millis\":1546589020459," + + "\"expiry_date_in_millis\":1546596340459," + + "\"max_nodes\":5," + + "\"issued_to\":\"customer\"," + + "\"issuer\":\"elasticsearch\"," + + "\"signature\":\"" + + randomAlphaOfLength(10) + + "\"}" + + "}"; + ElasticsearchException exception = expectThrows( + ElasticsearchException.class, + () -> { License.fromSource(new BytesArray(licenseString.getBytes(StandardCharsets.UTF_8)), XContentType.JSON); } + ); // When parsing a license, we read the signature bytes to verify the _version_. // Random alphabetic sig bytes will generate a bad version assertThat(exception, throwableWithMessage(containsString("Unknown license version found"))); @@ -267,28 +261,26 @@ public void testMalformedSignatureFromXContent() throws Exception { public void testUnableToBase64DecodeFromXContent() throws Exception { - String licenseString = "{\"license\":" + - "{\"uid\":\"4056779d-b823-4c12-a9cb-efa4a8d8c422\"," + - "\"type\":\"gold\"," + - "\"issue_date_in_millis\":1546589020459," + - "\"expiry_date_in_millis\":1546596340459," + - "\"max_nodes\":5," + - "\"issued_to\":\"customer\"," + - "\"issuer\":\"elasticsearch\"," + - "\"signature\":\"AAAAAgAAAA34V2kfTJVtvdL2LttwAAABmFJ6NGRnbEM3WVQrZVQwNkdKQmR1VytlMTMyM1J0dTZ1WGwyY2ZCVFhqMGtJU2gzZ3pnNTVpOW" + - "F5Y1NaUkwyN2VsTEtCYnlZR2c5WWtjQ0phaDlhRjlDUXViUmUwMWhjSkE2TFcwSGdneTJHbUV4N2RHUWJxV20ybjRsZHRzV2xkN0ZmdDlYblJmNVcxMlBWeU81" + - "V1hLUm1EK0V1dmF3cFdlSGZzTU5SZE1qUmFra3JkS1hCanBWVmVTaFFwV3BVZERzeG9Sci9rYnlJK2toODZXY09tNmFHUVNUL3IyUHExV3VSTlBneWNJcFQ0bX" + - "l0cmhNNnRwbE1CWE4zWjJ5eGFuWFo0NGhsb3B5WFd1eTdYbFFWQkxFVFFPSlBERlB0eVVJYXVSZ0lsR2JpRS9rN1h4MSsvNUpOcGN6cU1NOHN1cHNtSTFIUGN1" + - "bWNGNEcxekhrblhNOXZ2VEQvYmRzQUFwbytUZEpRR3l6QU5oS2ZFSFdSbGxxNDZyZ0xvUHIwRjdBL2JqcnJnNGFlK09Cek9pYlJ5Umc9PQAAAQAth77fQLF7CC" + - "EL7wA6Z0/UuRm/weECcsjW/50kBnPLO8yEs+9/bPa5LSU0bF6byEXOVeO0ebUQfztpjulbXh8TrBDSG+6VdxGtohPo2IYPBaXzGs3LOOor6An/lhptxBWdwYmf" + - "+xHAQ8tyvRqP5G+PRU7tiluEwR/eyHGZV2exdJNzmoGzdPSWwueBM5HK2GexORICH+UFI4cuGz444/hL2MMM1RdpVWQkT0SJ6D9x/VuSmHuYPdtX59Pp41LXvl" + - "bcp0m8mnXZh1vN9rmbTsZXnhBIoPTaRDwUBi3vJ3Ms3iLaEm4S8Slrfmtht2jUjgGZ2vAeZ9OHU2YsGtrSpz6fd\"}"; - ElasticsearchException exception = - expectThrows(ElasticsearchException.class, - () -> { - License.fromSource(new BytesArray(licenseString.getBytes(StandardCharsets.UTF_8)), - XContentType.JSON); - }); + String licenseString = "{\"license\":" + + "{\"uid\":\"4056779d-b823-4c12-a9cb-efa4a8d8c422\"," + + "\"type\":\"gold\"," + + "\"issue_date_in_millis\":1546589020459," + + "\"expiry_date_in_millis\":1546596340459," + + "\"max_nodes\":5," + + "\"issued_to\":\"customer\"," + + "\"issuer\":\"elasticsearch\"," + + "\"signature\":\"AAAAAgAAAA34V2kfTJVtvdL2LttwAAABmFJ6NGRnbEM3WVQrZVQwNkdKQmR1VytlMTMyM1J0dTZ1WGwyY2ZCVFhqMGtJU2gzZ3pnNTVpOW" + + "F5Y1NaUkwyN2VsTEtCYnlZR2c5WWtjQ0phaDlhRjlDUXViUmUwMWhjSkE2TFcwSGdneTJHbUV4N2RHUWJxV20ybjRsZHRzV2xkN0ZmdDlYblJmNVcxMlBWeU81" + + "V1hLUm1EK0V1dmF3cFdlSGZzTU5SZE1qUmFra3JkS1hCanBWVmVTaFFwV3BVZERzeG9Sci9rYnlJK2toODZXY09tNmFHUVNUL3IyUHExV3VSTlBneWNJcFQ0bX" + + "l0cmhNNnRwbE1CWE4zWjJ5eGFuWFo0NGhsb3B5WFd1eTdYbFFWQkxFVFFPSlBERlB0eVVJYXVSZ0lsR2JpRS9rN1h4MSsvNUpOcGN6cU1NOHN1cHNtSTFIUGN1" + + "bWNGNEcxekhrblhNOXZ2VEQvYmRzQUFwbytUZEpRR3l6QU5oS2ZFSFdSbGxxNDZyZ0xvUHIwRjdBL2JqcnJnNGFlK09Cek9pYlJ5Umc9PQAAAQAth77fQLF7CC" + + "EL7wA6Z0/UuRm/weECcsjW/50kBnPLO8yEs+9/bPa5LSU0bF6byEXOVeO0ebUQfztpjulbXh8TrBDSG+6VdxGtohPo2IYPBaXzGs3LOOor6An/lhptxBWdwYmf" + + "+xHAQ8tyvRqP5G+PRU7tiluEwR/eyHGZV2exdJNzmoGzdPSWwueBM5HK2GexORICH+UFI4cuGz444/hL2MMM1RdpVWQkT0SJ6D9x/VuSmHuYPdtX59Pp41LXvl" + + "bcp0m8mnXZh1vN9rmbTsZXnhBIoPTaRDwUBi3vJ3Ms3iLaEm4S8Slrfmtht2jUjgGZ2vAeZ9OHU2YsGtrSpz6fd\"}"; + ElasticsearchException exception = expectThrows( + ElasticsearchException.class, + () -> { License.fromSource(new BytesArray(licenseString.getBytes(StandardCharsets.UTF_8)), XContentType.JSON); } + ); assertThat(exception.getMessage(), containsString("malformed signature for license [4056779d-b823-4c12-a9cb-efa4a8d8c422]")); assertThat(exception.getCause(), instanceOf(IllegalArgumentException.class)); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicensedAllocatedPersistentTaskTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicensedAllocatedPersistentTaskTests.java index e2b9b95bc3140..e720640a8a0c3 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicensedAllocatedPersistentTaskTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicensedAllocatedPersistentTaskTests.java @@ -26,8 +26,17 @@ public class LicensedAllocatedPersistentTaskTests extends ESTestCase { void assertTrackingComplete(Consumer method) { XPackLicenseState licenseState = mock(XPackLicenseState.class); LicensedFeature.Persistent feature = LicensedFeature.persistent("family", "somefeature", License.OperationMode.PLATINUM); - var task = new LicensedAllocatedPersistentTask(0, "type", "action", "description", TaskId.EMPTY_TASK_ID, Map.of(), - feature, "context", licenseState); + var task = new LicensedAllocatedPersistentTask( + 0, + "type", + "action", + "description", + TaskId.EMPTY_TASK_ID, + Map.of(), + feature, + "context", + licenseState + ); PersistentTasksService service = mock(PersistentTasksService.class); TaskManager taskManager = mock(TaskManager.class); task.init(service, taskManager, "id", 0); @@ -60,21 +69,33 @@ public void testDoOverrides() { AtomicBoolean cancelledCalled = new AtomicBoolean(); AtomicBoolean failedCalled = new AtomicBoolean(); AtomicBoolean abortedCalled = new AtomicBoolean(); - var task = new LicensedAllocatedPersistentTask(0, "type", "action", "description", TaskId.EMPTY_TASK_ID, Map.of(), - feature, "context", licenseState) { + var task = new LicensedAllocatedPersistentTask( + 0, + "type", + "action", + "description", + TaskId.EMPTY_TASK_ID, + Map.of(), + feature, + "context", + licenseState + ) { @Override protected boolean doMarkAsCancelled() { cancelledCalled.set(true); return true; } + @Override protected void doMarkAsCompleted() { completedCalled.set(true); } + @Override protected void doMarkAsFailed(Exception e) { failedCalled.set(true); } + @Override protected void doMarkAsLocallyAborted(String reason) { abortedCalled.set(true); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicensesAcknowledgementTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicensesAcknowledgementTests.java index 235354ba48927..de0169938edd7 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicensesAcknowledgementTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicensesAcknowledgementTests.java @@ -47,8 +47,7 @@ private static class AssertingLicensesUpdateResponse implements ActionListener

    { try { - final ActionFuture getLicenseFuture = - new GetLicenseRequestBuilder(client().admin().cluster(), GetLicenseAction.INSTANCE).execute(); + final ActionFuture getLicenseFuture = new GetLicenseRequestBuilder( + client().admin().cluster(), + GetLicenseAction.INSTANCE + ).execute(); final GetLicenseResponse getLicenseResponse; getLicenseResponse = getLicenseFuture.get(); assertNotNull(getLicenseResponse.license()); @@ -74,9 +76,10 @@ public void testPutLicense() throws Exception { License signedLicense = generateSignedLicense(TimeValue.timeValueMinutes(2)); // put license - PutLicenseRequestBuilder putLicenseRequestBuilder = - new PutLicenseRequestBuilder(client().admin().cluster(), PutLicenseAction.INSTANCE).setLicense(signedLicense) - .setAcknowledge(true); + PutLicenseRequestBuilder putLicenseRequestBuilder = new PutLicenseRequestBuilder( + client().admin().cluster(), + PutLicenseAction.INSTANCE + ).setLicense(signedLicense).setAcknowledge(true); PutLicenseResponse putLicenseResponse = putLicenseRequestBuilder.get(); assertThat(putLicenseResponse.isAcknowledged(), equalTo(true)); assertThat(putLicenseResponse.status(), equalTo(LicensesStatus.VALID)); @@ -91,10 +94,10 @@ public void testPutLicenseFromString() throws Exception { String licenseString = TestUtils.dumpLicense(signedLicense); // put license source - PutLicenseRequestBuilder putLicenseRequestBuilder = - new PutLicenseRequestBuilder(client().admin().cluster(), PutLicenseAction.INSTANCE) - .setLicense(new BytesArray(licenseString.getBytes(StandardCharsets.UTF_8)), XContentType.JSON) - .setAcknowledge(true); + PutLicenseRequestBuilder putLicenseRequestBuilder = new PutLicenseRequestBuilder( + client().admin().cluster(), + PutLicenseAction.INSTANCE + ).setLicense(new BytesArray(licenseString.getBytes(StandardCharsets.UTF_8)), XContentType.JSON).setAcknowledge(true); PutLicenseResponse putLicenseResponse = putLicenseRequestBuilder.get(); assertThat(putLicenseResponse.isAcknowledged(), equalTo(true)); assertThat(putLicenseResponse.status(), equalTo(LicensesStatus.VALID)); @@ -109,10 +112,10 @@ public void testPutInvalidLicense() throws Exception { // modify content of signed license License tamperedLicense = License.builder() - .fromLicenseSpec(signedLicense, signedLicense.signature()) - .expiryDate(signedLicense.expiryDate() + 10 * 24 * 60 * 60 * 1000L) - .validate() - .build(); + .fromLicenseSpec(signedLicense, signedLicense.signature()) + .expiryDate(signedLicense.expiryDate() + 10 * 24 * 60 * 60 * 1000L) + .validate() + .build(); PutLicenseRequestBuilder builder = new PutLicenseRequestBuilder(client().admin().cluster(), PutLicenseAction.INSTANCE); builder.setLicense(tamperedLicense); @@ -154,9 +157,10 @@ public void testPutExpiredLicense() throws Exception { public void testPutLicensesSimple() throws Exception { License goldSignedLicense = generateSignedLicense("gold", TimeValue.timeValueMinutes(5)); - PutLicenseRequestBuilder putLicenseRequestBuilder = - new PutLicenseRequestBuilder(client().admin().cluster(), PutLicenseAction.INSTANCE).setLicense(goldSignedLicense) - .setAcknowledge(true); + PutLicenseRequestBuilder putLicenseRequestBuilder = new PutLicenseRequestBuilder( + client().admin().cluster(), + PutLicenseAction.INSTANCE + ).setLicense(goldSignedLicense).setAcknowledge(true); PutLicenseResponse putLicenseResponse = putLicenseRequestBuilder.get(); assertThat(putLicenseResponse.status(), equalTo(LicensesStatus.VALID)); GetLicenseResponse getLicenseResponse = new GetLicenseRequestBuilder(client().admin().cluster(), GetLicenseAction.INSTANCE).get(); @@ -173,17 +177,20 @@ public void testPutLicensesSimple() throws Exception { public void testRemoveLicensesSimple() throws Exception { License goldLicense = generateSignedLicense("gold", TimeValue.timeValueMinutes(5)); - PutLicenseRequestBuilder putLicenseRequestBuilder = - new PutLicenseRequestBuilder(client().admin().cluster(), PutLicenseAction.INSTANCE).setLicense(goldLicense) - .setAcknowledge(true); + PutLicenseRequestBuilder putLicenseRequestBuilder = new PutLicenseRequestBuilder( + client().admin().cluster(), + PutLicenseAction.INSTANCE + ).setLicense(goldLicense).setAcknowledge(true); PutLicenseResponse putLicenseResponse = putLicenseRequestBuilder.get(); assertThat(putLicenseResponse.isAcknowledged(), equalTo(true)); assertThat(putLicenseResponse.status(), equalTo(LicensesStatus.VALID)); GetLicenseResponse getLicenseResponse = new GetLicenseRequestBuilder(client().admin().cluster(), GetLicenseAction.INSTANCE).get(); assertThat(getLicenseResponse.license(), equalTo(goldLicense)); // delete all licenses - DeleteLicenseRequestBuilder deleteLicenseRequestBuilder = - new DeleteLicenseRequestBuilder(client().admin().cluster(), DeleteLicenseAction.INSTANCE); + DeleteLicenseRequestBuilder deleteLicenseRequestBuilder = new DeleteLicenseRequestBuilder( + client().admin().cluster(), + DeleteLicenseAction.INSTANCE + ); AcknowledgedResponse deleteLicenseResponse = deleteLicenseRequestBuilder.get(); assertThat(deleteLicenseResponse.isAcknowledged(), equalTo(true)); // get licenses (expected no licenses) @@ -194,20 +201,21 @@ public void testRemoveLicensesSimple() throws Exception { public void testLicenseIsRejectWhenStartDateLaterThanNow() throws Exception { long now = System.currentTimeMillis(); final License.Builder builder = License.builder() - .uid(UUID.randomUUID().toString()) - .version(License.VERSION_CURRENT) - .expiryDate(dateMath("now+2h", now)) - .startDate(dateMath("now+1h", now)) - .issueDate(now) - .type(License.OperationMode.TRIAL.toString()) - .issuedTo("customer") - .issuer("elasticsearch") - .maxNodes(5); + .uid(UUID.randomUUID().toString()) + .version(License.VERSION_CURRENT) + .expiryDate(dateMath("now+2h", now)) + .startDate(dateMath("now+1h", now)) + .issueDate(now) + .type(License.OperationMode.TRIAL.toString()) + .issuedTo("customer") + .issuer("elasticsearch") + .maxNodes(5); License license = TestUtils.generateSignedLicense(builder); - PutLicenseRequestBuilder putLicenseRequestBuilder = - new PutLicenseRequestBuilder(client().admin().cluster(), PutLicenseAction.INSTANCE).setLicense(license) - .setAcknowledge(true); + PutLicenseRequestBuilder putLicenseRequestBuilder = new PutLicenseRequestBuilder( + client().admin().cluster(), + PutLicenseAction.INSTANCE + ).setLicense(license).setAcknowledge(true); PutLicenseResponse putLicenseResponse = putLicenseRequestBuilder.get(); assertThat(putLicenseResponse.isAcknowledged(), equalTo(true)); assertThat(putLicenseResponse.status(), equalTo(LicensesStatus.INVALID)); @@ -216,20 +224,21 @@ public void testLicenseIsRejectWhenStartDateLaterThanNow() throws Exception { public void testLicenseIsAcceptedWhenStartDateBeforeThanNow() throws Exception { long now = System.currentTimeMillis(); final License.Builder builder = License.builder() - .uid(UUID.randomUUID().toString()) - .version(License.VERSION_CURRENT) - .expiryDate(dateMath("now+2h", now)) - .startDate(now) - .issueDate(now) - .type(License.OperationMode.TRIAL.toString()) - .issuedTo("customer") - .issuer("elasticsearch") - .maxNodes(5); + .uid(UUID.randomUUID().toString()) + .version(License.VERSION_CURRENT) + .expiryDate(dateMath("now+2h", now)) + .startDate(now) + .issueDate(now) + .type(License.OperationMode.TRIAL.toString()) + .issuedTo("customer") + .issuer("elasticsearch") + .maxNodes(5); License license = TestUtils.generateSignedLicense(builder); - PutLicenseRequestBuilder putLicenseRequestBuilder = - new PutLicenseRequestBuilder(client().admin().cluster(), PutLicenseAction.INSTANCE).setLicense(license) - .setAcknowledge(true); + PutLicenseRequestBuilder putLicenseRequestBuilder = new PutLicenseRequestBuilder( + client().admin().cluster(), + PutLicenseAction.INSTANCE + ).setLicense(license).setAcknowledge(true); PutLicenseResponse putLicenseResponse = putLicenseRequestBuilder.get(); assertThat(putLicenseResponse.isAcknowledged(), equalTo(true)); assertThat(putLicenseResponse.status(), equalTo(LicensesStatus.VALID)); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/OperationModeFileWatcherTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/OperationModeFileWatcherTests.java index 9236bc28772fb..9cd806d6f8655 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/OperationModeFileWatcherTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/OperationModeFileWatcherTests.java @@ -34,14 +34,16 @@ public class OperationModeFileWatcherTests extends ESTestCase { @Before public void setup() throws Exception { threadPool = new TestThreadPool("license mode file watcher tests"); - Settings settings = Settings.builder() - .put("resource.reload.interval.high", "10ms") - .build(); + Settings settings = Settings.builder().put("resource.reload.interval.high", "10ms").build(); watcherService = new ResourceWatcherService(settings, threadPool); licenseModePath = createTempFile(); onChangeCounter = new AtomicReference<>(new CountDownLatch(1)); - operationModeFileWatcher = new OperationModeFileWatcher(watcherService, licenseModePath, logger, - () -> onChangeCounter.get().countDown()); + operationModeFileWatcher = new OperationModeFileWatcher( + watcherService, + licenseModePath, + logger, + () -> onChangeCounter.get().countDown() + ); } @After diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/RemoteClusterLicenseCheckerTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/RemoteClusterLicenseCheckerTests.java index 82733f1870385..987d3137509d6 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/RemoteClusterLicenseCheckerTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/RemoteClusterLicenseCheckerTests.java @@ -78,8 +78,9 @@ public void testNoRemoteIndices() { public void testRemoteIndices() { final List indices = Arrays.asList("local-index1", "remote-cluster1:index1", "local-index2", "remote-cluster2:index1"); assertThat( - RemoteClusterLicenseChecker.remoteIndices(indices), - containsInAnyOrder("remote-cluster1:index1", "remote-cluster2:index1")); + RemoteClusterLicenseChecker.remoteIndices(indices), + containsInAnyOrder("remote-cluster1:index1", "remote-cluster2:index1") + ); } public void testNoRemoteClusterAliases() { @@ -97,23 +98,33 @@ public void testOneRemoteClusterAlias() { public void testMoreThanOneRemoteClusterAlias() { final Set remoteClusters = Sets.newHashSet("remote-cluster1", "remote-cluster2"); final List indices = Arrays.asList("remote-cluster1:remote-index1", "local-index1", "remote-cluster2:remote-index1"); - assertThat(RemoteClusterLicenseChecker.remoteClusterAliases(remoteClusters, indices), - containsInAnyOrder("remote-cluster1", "remote-cluster2")); + assertThat( + RemoteClusterLicenseChecker.remoteClusterAliases(remoteClusters, indices), + containsInAnyOrder("remote-cluster1", "remote-cluster2") + ); } public void testDuplicateRemoteClusterAlias() { final Set remoteClusters = Sets.newHashSet("remote-cluster1", "remote-cluster2"); final List indices = Arrays.asList( - "remote-cluster1:remote-index1", "local-index1", "remote-cluster2:index1", "remote-cluster2:remote-index2"); - assertThat(RemoteClusterLicenseChecker.remoteClusterAliases(remoteClusters, indices), - containsInAnyOrder("remote-cluster1", "remote-cluster2")); + "remote-cluster1:remote-index1", + "local-index1", + "remote-cluster2:index1", + "remote-cluster2:remote-index2" + ); + assertThat( + RemoteClusterLicenseChecker.remoteClusterAliases(remoteClusters, indices), + containsInAnyOrder("remote-cluster1", "remote-cluster2") + ); } public void testSimpleWildcardRemoteClusterAlias() { final Set remoteClusters = Sets.newHashSet("remote-cluster1", "remote-cluster2"); final List indices = Arrays.asList("*:remote-index1", "local-index1"); - assertThat(RemoteClusterLicenseChecker.remoteClusterAliases(remoteClusters, indices), - containsInAnyOrder("remote-cluster1", "remote-cluster2")); + assertThat( + RemoteClusterLicenseChecker.remoteClusterAliases(remoteClusters, indices), + containsInAnyOrder("remote-cluster1", "remote-cluster2") + ); } public void testPartialWildcardRemoteClusterAlias() { @@ -135,8 +146,8 @@ public void testCheckRemoteClusterLicensesGivenCompatibleLicenses() { final ThreadPool threadPool = createMockThreadPool(); final Client client = createMockClient(threadPool); doAnswer(invocationMock -> { - @SuppressWarnings("unchecked") ActionListener listener = - (ActionListener) invocationMock.getArguments()[2]; + @SuppressWarnings("unchecked") + ActionListener listener = (ActionListener) invocationMock.getArguments()[2]; listener.onResponse(responses.get(index.getAndIncrement())); return null; }).when(client).execute(same(XPackInfoAction.INSTANCE), any(), any()); @@ -146,26 +157,28 @@ public void testCheckRemoteClusterLicensesGivenCompatibleLicenses() { responses.add(new XPackInfoResponse(null, createPlatinumLicenseResponse(), null)); responses.add(new XPackInfoResponse(null, createPlatinumLicenseResponse(), null)); - final RemoteClusterLicenseChecker licenseChecker = - new RemoteClusterLicenseChecker(client, operationMode -> - XPackLicenseState.isAllowedByOperationMode(operationMode, License.OperationMode.PLATINUM)); + final RemoteClusterLicenseChecker licenseChecker = new RemoteClusterLicenseChecker( + client, + operationMode -> XPackLicenseState.isAllowedByOperationMode(operationMode, License.OperationMode.PLATINUM) + ); final AtomicReference licenseCheck = new AtomicReference<>(); licenseChecker.checkRemoteClusterLicenses( - remoteClusterAliases, - doubleInvocationProtectingListener(new ActionListener() { + remoteClusterAliases, + doubleInvocationProtectingListener(new ActionListener() { - @Override - public void onResponse(final RemoteClusterLicenseChecker.LicenseCheck response) { - licenseCheck.set(response); - } + @Override + public void onResponse(final RemoteClusterLicenseChecker.LicenseCheck response) { + licenseCheck.set(response); + } - @Override - public void onFailure(final Exception e) { - fail(e.getMessage()); - } + @Override + public void onFailure(final Exception e) { + fail(e.getMessage()); + } - })); + }) + ); verify(client, times(3)).execute(same(XPackInfoAction.INSTANCE), any(), any()); assertNotNull(licenseCheck.get()); @@ -183,32 +196,34 @@ public void testCheckRemoteClusterLicensesGivenIncompatibleLicense() { final ThreadPool threadPool = createMockThreadPool(); final Client client = createMockClient(threadPool); doAnswer(invocationMock -> { - @SuppressWarnings("unchecked") ActionListener listener = - (ActionListener) invocationMock.getArguments()[2]; + @SuppressWarnings("unchecked") + ActionListener listener = (ActionListener) invocationMock.getArguments()[2]; listener.onResponse(responses.get(index.getAndIncrement())); return null; }).when(client).execute(same(XPackInfoAction.INSTANCE), any(), any()); - final RemoteClusterLicenseChecker licenseChecker = - new RemoteClusterLicenseChecker(client, operationMode -> - XPackLicenseState.isAllowedByOperationMode(operationMode, License.OperationMode.PLATINUM)); + final RemoteClusterLicenseChecker licenseChecker = new RemoteClusterLicenseChecker( + client, + operationMode -> XPackLicenseState.isAllowedByOperationMode(operationMode, License.OperationMode.PLATINUM) + ); final AtomicReference licenseCheck = new AtomicReference<>(); licenseChecker.checkRemoteClusterLicenses( - remoteClusterAliases, - doubleInvocationProtectingListener(new ActionListener() { + remoteClusterAliases, + doubleInvocationProtectingListener(new ActionListener() { - @Override - public void onResponse(final RemoteClusterLicenseChecker.LicenseCheck response) { - licenseCheck.set(response); - } + @Override + public void onResponse(final RemoteClusterLicenseChecker.LicenseCheck response) { + licenseCheck.set(response); + } - @Override - public void onFailure(final Exception e) { - fail(e.getMessage()); - } + @Override + public void onFailure(final Exception e) { + fail(e.getMessage()); + } - })); + }) + ); verify(client, times(2)).execute(same(XPackInfoAction.INSTANCE), any(), any()); assertNotNull(licenseCheck.get()); @@ -226,8 +241,8 @@ public void testCheckRemoteClusterLicencesGivenNonExistentCluster() { final ThreadPool threadPool = createMockThreadPool(); final Client client = createMockClientThatThrowsOnGetRemoteClusterClient(threadPool, failingClusterAlias); doAnswer(invocationMock -> { - @SuppressWarnings("unchecked") ActionListener listener = - (ActionListener) invocationMock.getArguments()[2]; + @SuppressWarnings("unchecked") + ActionListener listener = (ActionListener) invocationMock.getArguments()[2]; listener.onResponse(responses.get(index.getAndIncrement())); return null; }).when(client).execute(same(XPackInfoAction.INSTANCE), any(), any()); @@ -236,26 +251,28 @@ public void testCheckRemoteClusterLicencesGivenNonExistentCluster() { responses.add(new XPackInfoResponse(null, createPlatinumLicenseResponse(), null)); responses.add(new XPackInfoResponse(null, createPlatinumLicenseResponse(), null)); - final RemoteClusterLicenseChecker licenseChecker = - new RemoteClusterLicenseChecker(client, operationMode -> - XPackLicenseState.isAllowedByOperationMode(operationMode, License.OperationMode.PLATINUM)); + final RemoteClusterLicenseChecker licenseChecker = new RemoteClusterLicenseChecker( + client, + operationMode -> XPackLicenseState.isAllowedByOperationMode(operationMode, License.OperationMode.PLATINUM) + ); final AtomicReference exception = new AtomicReference<>(); licenseChecker.checkRemoteClusterLicenses( - remoteClusterAliases, - doubleInvocationProtectingListener(new ActionListener() { + remoteClusterAliases, + doubleInvocationProtectingListener(new ActionListener() { - @Override - public void onResponse(final RemoteClusterLicenseChecker.LicenseCheck response) { - fail(); - } + @Override + public void onResponse(final RemoteClusterLicenseChecker.LicenseCheck response) { + fail(); + } - @Override - public void onFailure(final Exception e) { - exception.set(e); - } + @Override + public void onFailure(final Exception e) { + exception.set(e); + } - })); + }) + ); assertNotNull(exception.get()); assertThat(exception.get(), instanceOf(ElasticsearchException.class)); @@ -271,19 +288,22 @@ public void testRemoteClusterLicenseCallUsesSystemContext() throws InterruptedEx final Client client = createMockClient(threadPool); doAnswer(invocationMock -> { assertTrue(threadPool.getThreadContext().isSystemContext()); - @SuppressWarnings("unchecked") ActionListener listener = - (ActionListener) invocationMock.getArguments()[2]; + @SuppressWarnings("unchecked") + ActionListener listener = (ActionListener) invocationMock.getArguments()[2]; listener.onResponse(new XPackInfoResponse(null, createPlatinumLicenseResponse(), null)); return null; }).when(client).execute(same(XPackInfoAction.INSTANCE), any(), any()); - final RemoteClusterLicenseChecker licenseChecker = - new RemoteClusterLicenseChecker(client, operationMode -> - XPackLicenseState.isAllowedByOperationMode(operationMode, License.OperationMode.PLATINUM)); + final RemoteClusterLicenseChecker licenseChecker = new RemoteClusterLicenseChecker( + client, + operationMode -> XPackLicenseState.isAllowedByOperationMode(operationMode, License.OperationMode.PLATINUM) + ); final List remoteClusterAliases = Collections.singletonList("valid"); licenseChecker.checkRemoteClusterLicenses( - remoteClusterAliases, doubleInvocationProtectingListener(ActionListener.wrap(() -> {}))); + remoteClusterAliases, + doubleInvocationProtectingListener(ActionListener.wrap(() -> {})) + ); verify(client, times(1)).execute(same(XPackInfoAction.INSTANCE), any(), any()); } finally { @@ -307,8 +327,8 @@ public void testListenerIsExecutedWithCallingContext() throws InterruptedExcepti client = createMockClient(threadPool); } doAnswer(invocationMock -> { - @SuppressWarnings("unchecked") ActionListener listener = - (ActionListener) invocationMock.getArguments()[2]; + @SuppressWarnings("unchecked") + ActionListener listener = (ActionListener) invocationMock.getArguments()[2]; listener.onResponse(responses.get(index.getAndIncrement())); return null; }).when(client).execute(same(XPackInfoAction.INSTANCE), any(), any()); @@ -317,37 +337,39 @@ public void testListenerIsExecutedWithCallingContext() throws InterruptedExcepti responses.add(new XPackInfoResponse(null, createPlatinumLicenseResponse(), null)); responses.add(new XPackInfoResponse(null, createPlatinumLicenseResponse(), null)); - final RemoteClusterLicenseChecker licenseChecker = - new RemoteClusterLicenseChecker(client, operationMode -> - XPackLicenseState.isAllowedByOperationMode(operationMode, License.OperationMode.PLATINUM)); + final RemoteClusterLicenseChecker licenseChecker = new RemoteClusterLicenseChecker( + client, + operationMode -> XPackLicenseState.isAllowedByOperationMode(operationMode, License.OperationMode.PLATINUM) + ); final AtomicBoolean listenerInvoked = new AtomicBoolean(); threadPool.getThreadContext().putHeader("key", "value"); licenseChecker.checkRemoteClusterLicenses( - remoteClusterAliases, - doubleInvocationProtectingListener(new ActionListener() { - - @Override - public void onResponse(final RemoteClusterLicenseChecker.LicenseCheck response) { - if (failure) { - fail(); - } - assertThat(threadPool.getThreadContext().getHeader("key"), equalTo("value")); - assertFalse(threadPool.getThreadContext().isSystemContext()); - listenerInvoked.set(true); + remoteClusterAliases, + doubleInvocationProtectingListener(new ActionListener() { + + @Override + public void onResponse(final RemoteClusterLicenseChecker.LicenseCheck response) { + if (failure) { + fail(); } + assertThat(threadPool.getThreadContext().getHeader("key"), equalTo("value")); + assertFalse(threadPool.getThreadContext().isSystemContext()); + listenerInvoked.set(true); + } - @Override - public void onFailure(final Exception e) { - if (failure == false) { - fail(); - } - assertThat(threadPool.getThreadContext().getHeader("key"), equalTo("value")); - assertFalse(threadPool.getThreadContext().isSystemContext()); - listenerInvoked.set(true); + @Override + public void onFailure(final Exception e) { + if (failure == false) { + fail(); } + assertThat(threadPool.getThreadContext().getHeader("key"), equalTo("value")); + assertFalse(threadPool.getThreadContext().isSystemContext()); + listenerInvoked.set(true); + } - })); + }) + ); assertTrue(listenerInvoked.get()); } finally { @@ -357,45 +379,55 @@ public void onFailure(final Exception e) { public void testBuildErrorMessageForActiveCompatibleLicense() { final XPackInfoResponse.LicenseInfo platinumLicence = createPlatinumLicenseResponse(); - final RemoteClusterLicenseChecker.RemoteClusterLicenseInfo info = - new RemoteClusterLicenseChecker.RemoteClusterLicenseInfo("platinum-cluster", platinumLicence); + final RemoteClusterLicenseChecker.RemoteClusterLicenseInfo info = new RemoteClusterLicenseChecker.RemoteClusterLicenseInfo( + "platinum-cluster", + platinumLicence + ); final AssertionError e = expectThrows( - AssertionError.class, - () -> RemoteClusterLicenseChecker.buildErrorMessage("", info, RemoteClusterLicenseChecker::isAllowedByLicense)); + AssertionError.class, + () -> RemoteClusterLicenseChecker.buildErrorMessage("", info, RemoteClusterLicenseChecker::isAllowedByLicense) + ); assertThat(e, hasToString(containsString("license must be incompatible to build error message"))); } public void testBuildErrorMessageForIncompatibleLicense() { final XPackInfoResponse.LicenseInfo basicLicense = createBasicLicenseResponse(); - final RemoteClusterLicenseChecker.RemoteClusterLicenseInfo info = - new RemoteClusterLicenseChecker.RemoteClusterLicenseInfo("basic-cluster", basicLicense); + final RemoteClusterLicenseChecker.RemoteClusterLicenseInfo info = new RemoteClusterLicenseChecker.RemoteClusterLicenseInfo( + "basic-cluster", + basicLicense + ); assertThat( - RemoteClusterLicenseChecker.buildErrorMessage("Feature", info, RemoteClusterLicenseChecker::isAllowedByLicense), - equalTo("the license mode [BASIC] on cluster [basic-cluster] does not enable [Feature]")); + RemoteClusterLicenseChecker.buildErrorMessage("Feature", info, RemoteClusterLicenseChecker::isAllowedByLicense), + equalTo("the license mode [BASIC] on cluster [basic-cluster] does not enable [Feature]") + ); } public void testBuildErrorMessageForInactiveLicense() { final XPackInfoResponse.LicenseInfo expiredLicense = createExpiredLicenseResponse(); - final RemoteClusterLicenseChecker.RemoteClusterLicenseInfo info = - new RemoteClusterLicenseChecker.RemoteClusterLicenseInfo("expired-cluster", expiredLicense); + final RemoteClusterLicenseChecker.RemoteClusterLicenseInfo info = new RemoteClusterLicenseChecker.RemoteClusterLicenseInfo( + "expired-cluster", + expiredLicense + ); assertThat( - RemoteClusterLicenseChecker.buildErrorMessage("Feature", info, RemoteClusterLicenseChecker::isAllowedByLicense), - equalTo("the license on cluster [expired-cluster] is not active")); + RemoteClusterLicenseChecker.buildErrorMessage("Feature", info, RemoteClusterLicenseChecker::isAllowedByLicense), + equalTo("the license on cluster [expired-cluster] is not active") + ); } public void testCheckRemoteClusterLicencesNoLicenseMetadata() { final ThreadPool threadPool = createMockThreadPool(); final Client client = createMockClient(threadPool); doAnswer(invocationMock -> { - @SuppressWarnings("unchecked") ActionListener listener = - (ActionListener) invocationMock.getArguments()[2]; + @SuppressWarnings("unchecked") + ActionListener listener = (ActionListener) invocationMock.getArguments()[2]; listener.onResponse(new XPackInfoResponse(null, null, null)); return null; }).when(client).execute(same(XPackInfoAction.INSTANCE), any(), any()); - final RemoteClusterLicenseChecker licenseChecker = - new RemoteClusterLicenseChecker(client, operationMode -> - XPackLicenseState.isAllowedByOperationMode(operationMode, License.OperationMode.PLATINUM)); + final RemoteClusterLicenseChecker licenseChecker = new RemoteClusterLicenseChecker( + client, + operationMode -> XPackLicenseState.isAllowedByOperationMode(operationMode, License.OperationMode.PLATINUM) + ); final AtomicReference exception = new AtomicReference<>(); licenseChecker.checkRemoteClusterLicenses( @@ -412,7 +444,8 @@ public void onFailure(final Exception e) { exception.set(e); } - })); + }) + ); assertNotNull(exception.get()); assertThat(exception.get(), instanceOf(ResourceNotFoundException.class)); @@ -420,7 +453,8 @@ public void onFailure(final Exception e) { } private ActionListener doubleInvocationProtectingListener( - final ActionListener listener) { + final ActionListener listener + ) { final AtomicBoolean listenerInvoked = new AtomicBoolean(); return new ActionListener() { @@ -454,12 +488,10 @@ private Client createMockClient(final ThreadPool threadPool) { } private Client createMockClientThatThrowsOnGetRemoteClusterClient(final ThreadPool threadPool, final String clusterAlias) { - return createMockClient( - threadPool, - client -> { - when(client.getRemoteClusterClient(clusterAlias)).thenThrow(new IllegalArgumentException()); - when(client.getRemoteClusterClient(argThat(a -> not(clusterAlias).matches(a)))).thenReturn(client); - }); + return createMockClient(threadPool, client -> { + when(client.getRemoteClusterClient(clusterAlias)).thenThrow(new IllegalArgumentException()); + when(client.getRemoteClusterClient(argThat(a -> not(clusterAlias).matches(a)))).thenReturn(client); + }); } private Client createMockClient(final ThreadPool threadPool, final Consumer finish) { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/SelfGeneratedLicenseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/SelfGeneratedLicenseTests.java index a8f7ee9cf177e..09620735219a6 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/SelfGeneratedLicenseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/SelfGeneratedLicenseTests.java @@ -8,11 +8,11 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; import java.io.IOException; import java.nio.ByteBuffer; @@ -23,18 +23,17 @@ import static org.elasticsearch.license.CryptUtils.encryptV3Format; import static org.hamcrest.Matchers.equalTo; - public class SelfGeneratedLicenseTests extends ESTestCase { public void testBasic() throws Exception { long issueDate = System.currentTimeMillis(); License.Builder specBuilder = License.builder() - .uid(UUID.randomUUID().toString()) - .issuedTo("customer") - .maxNodes(5) - .type(randomBoolean() ? "trial" : "basic") - .issueDate(issueDate) - .expiryDate(issueDate + TimeValue.timeValueHours(2).getMillis()); + .uid(UUID.randomUUID().toString()) + .issuedTo("customer") + .maxNodes(5) + .type(randomBoolean() ? "trial" : "basic") + .issueDate(issueDate) + .expiryDate(issueDate + TimeValue.timeValueHours(2).getMillis()); License trialLicense = SelfGeneratedLicense.create(specBuilder, License.VERSION_CURRENT); assertThat(SelfGeneratedLicense.verify(trialLicense), equalTo(true)); } @@ -42,17 +41,18 @@ public void testBasic() throws Exception { public void testTampered() throws Exception { long issueDate = System.currentTimeMillis(); License.Builder specBuilder = License.builder() - .uid(UUID.randomUUID().toString()) - .issuedTo("customer") - .type(randomBoolean() ? "trial" : "basic") - .maxNodes(5) - .issueDate(issueDate) - .expiryDate(issueDate + TimeValue.timeValueHours(2).getMillis()); + .uid(UUID.randomUUID().toString()) + .issuedTo("customer") + .type(randomBoolean() ? "trial" : "basic") + .maxNodes(5) + .issueDate(issueDate) + .expiryDate(issueDate + TimeValue.timeValueHours(2).getMillis()); License trialLicense = SelfGeneratedLicense.create(specBuilder, License.VERSION_CURRENT); final String originalSignature = trialLicense.signature(); - License tamperedLicense = License.builder().fromLicenseSpec(trialLicense, originalSignature) - .expiryDate(System.currentTimeMillis() + TimeValue.timeValueHours(5).getMillis()) - .build(); + License tamperedLicense = License.builder() + .fromLicenseSpec(trialLicense, originalSignature) + .expiryDate(System.currentTimeMillis() + TimeValue.timeValueHours(5).getMillis()) + .build(); assertThat(SelfGeneratedLicense.verify(trialLicense), equalTo(true)); assertThat(SelfGeneratedLicense.verify(tamperedLicense), equalTo(false)); } @@ -60,19 +60,21 @@ public void testTampered() throws Exception { public void testFrom1x() throws Exception { long issueDate = System.currentTimeMillis(); License.Builder specBuilder = License.builder() - .uid(UUID.randomUUID().toString()) - .issuedTo("customer") - .type("subscription") - .subscriptionType("trial") - .issuer("elasticsearch") - .feature("") - .version(License.VERSION_START) - .maxNodes(5) - .issueDate(issueDate) - .expiryDate(issueDate + TimeValue.timeValueHours(2).getMillis()); + .uid(UUID.randomUUID().toString()) + .issuedTo("customer") + .type("subscription") + .subscriptionType("trial") + .issuer("elasticsearch") + .feature("") + .version(License.VERSION_START) + .maxNodes(5) + .issueDate(issueDate) + .expiryDate(issueDate + TimeValue.timeValueHours(2).getMillis()); License pre20TrialLicense = specBuilder.build(); - License license = SelfGeneratedLicense.create(License.builder().fromPre20LicenseSpec(pre20TrialLicense).type("trial"), - License.VERSION_CURRENT); + License license = SelfGeneratedLicense.create( + License.builder().fromPre20LicenseSpec(pre20TrialLicense).type("trial"), + License.VERSION_CURRENT + ); assertThat(SelfGeneratedLicense.verify(license), equalTo(true)); } @@ -80,23 +82,22 @@ public void testTrialLicenseVerifyWithOlderVersion() throws Exception { assumeFalse("Can't run in a FIPS JVM. We can't generate old licenses since PBEWithSHA1AndDESede is not available", inFipsJvm()); long issueDate = System.currentTimeMillis(); License.Builder specBuilder = License.builder() - .issuedTo("customer") - .maxNodes(5) - .issueDate(issueDate) - .expiryDate(issueDate + TimeValue.timeValueHours(2).getMillis()) - .feature("") - .subscriptionType("trial") - .version(1); + .issuedTo("customer") + .maxNodes(5) + .issueDate(issueDate) + .expiryDate(issueDate + TimeValue.timeValueHours(2).getMillis()) + .feature("") + .subscriptionType("trial") + .version(1); License trialLicenseV1 = createTrialLicense(specBuilder); assertThat(SelfGeneratedLicense.verify(trialLicenseV1), equalTo(true)); } private static License createTrialLicense(License.Builder specBuilder) { - License spec = specBuilder - .type(randomBoolean() ? "trial" : "basic") - .issuer("elasticsearch") - .uid(UUID.randomUUID().toString()) - .build(); + License spec = specBuilder.type(randomBoolean() ? "trial" : "basic") + .issuer("elasticsearch") + .uid(UUID.randomUUID().toString()) + .build(); final String signature; try { XContentBuilder contentBuilder = XContentFactory.contentBuilder(XContentType.JSON); @@ -104,9 +105,7 @@ private static License createTrialLicense(License.Builder specBuilder) { byte[] encrypt = encryptV3Format(BytesReference.toBytes(BytesReference.bytes(contentBuilder))); byte[] bytes = new byte[4 + 4 + encrypt.length]; ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); - byteBuffer.putInt(-spec.version()) - .putInt(encrypt.length) - .put(encrypt); + byteBuffer.putInt(-spec.version()).putInt(encrypt.length).put(encrypt); signature = Base64.getEncoder().encodeToString(bytes); } catch (IOException e) { throw new IllegalStateException(e); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/StartBasicLicenseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/StartBasicLicenseTests.java index 068e55a56a792..e74eea86c3665 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/StartBasicLicenseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/StartBasicLicenseTests.java @@ -28,7 +28,8 @@ public class StartBasicLicenseTests extends AbstractLicensesIntegrationTestCase protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { return Settings.builder() .put(addRoles(super.nodeSettings(nodeOrdinal, otherSettings), Set.of(DiscoveryNodeRole.DATA_ROLE))) - .put(LicenseService.SELF_GENERATED_LICENSE_TYPE.getKey(), "basic").build(); + .put(LicenseService.SELF_GENERATED_LICENSE_TYPE.getKey(), "basic") + .build(); } @Override @@ -38,7 +39,7 @@ protected Collection> nodePlugins() { public void testStartBasicLicense() throws Exception { LicensingClient licensingClient = new LicensingClient(client()); - License license = TestUtils.generateSignedLicense("trial", License.VERSION_CURRENT, -1, TimeValue.timeValueHours(24)); + License license = TestUtils.generateSignedLicense("trial", License.VERSION_CURRENT, -1, TimeValue.timeValueHours(24)); licensingClient.preparePutLicense(license).get(); assertBusy(() -> { @@ -68,7 +69,6 @@ public void testStartBasicLicense() throws Exception { GetBasicStatusResponse response4 = licensingClient.prepareGetStartBasic().get(); assertFalse(response4.isEligibleToStartBasic()); - PostStartBasicResponse response5 = licensingClient.preparePostStartBasic().setAcknowledge(true).get(); assertEquals(403, response5.status().getStatus()); assertFalse(response5.getStatus().isBasicStarted()); @@ -78,7 +78,7 @@ public void testStartBasicLicense() throws Exception { public void testUnacknowledgedStartBasicLicense() throws Exception { LicensingClient licensingClient = new LicensingClient(client()); - License license = TestUtils.generateSignedLicense("trial", License.VERSION_CURRENT, -1, TimeValue.timeValueHours(24)); + License license = TestUtils.generateSignedLicense("trial", License.VERSION_CURRENT, -1, TimeValue.timeValueHours(24)); licensingClient.preparePutLicense(license).get(); assertBusy(() -> { @@ -91,8 +91,10 @@ public void testUnacknowledgedStartBasicLicense() throws Exception { assertFalse(response.isAcknowledged()); assertFalse(response.getStatus().isBasicStarted()); assertEquals("Operation failed: Needs acknowledgement.", response.getStatus().getErrorMessage()); - assertEquals("This license update requires acknowledgement. To acknowledge the license, " + - "please read the following messages and call /start_basic again, this time with the \"acknowledge=true\" parameter:", - response.getAcknowledgeMessage()); + assertEquals( + "This license update requires acknowledgement. To acknowledge the license, " + + "please read the following messages and call /start_basic again, this time with the \"acknowledge=true\" parameter:", + response.getAcknowledgeMessage() + ); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/TestUtils.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/TestUtils.java index f39a2e609926b..14c42c8ff167c 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/TestUtils.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/TestUtils.java @@ -66,13 +66,13 @@ public static LicenseSpec generateRandomLicenseSpec(int version) { long now = System.currentTimeMillis(); String uid = UUID.randomUUID().toString(); String feature = "feature__" + randomInt(); - String issuer = "issuer__" + randomInt(); + String issuer = "issuer__" + randomInt(); String issuedTo = "issuedTo__" + randomInt(); final String type; final String subscriptionType; if (version < License.VERSION_NO_FEATURE_TYPE) { subscriptionType = randomFrom("gold", "silver", "platinum"); - type = "subscription";//randomFrom("subscription", "internal", "development"); + type = "subscription";// randomFrom("subscription", "internal", "development"); } else { subscriptionType = null; type = randomFrom("basic", "dev", "gold", "silver", "platinum"); @@ -81,8 +81,18 @@ public static LicenseSpec generateRandomLicenseSpec(int version) { if (datesInMillis) { long issueDateInMillis = dateMath("now", now); long expiryDateInMillis = dateMath("now+10d/d", now); - return new LicenseSpec(version, uid, feature, issueDateInMillis, expiryDateInMillis, type, subscriptionType, issuedTo, issuer, - maxNodes); + return new LicenseSpec( + version, + uid, + feature, + issueDateInMillis, + expiryDateInMillis, + type, + subscriptionType, + issuedTo, + issuer, + maxNodes + ); } else { String issueDate = dateMathString("now", now); String expiryDate = dateMathString("now+10d/d", now); @@ -176,12 +186,32 @@ public static class LicenseSpec { public final int maxNodes; public LicenseSpec(String issueDate, String expiryDate) { - this(License.VERSION_CURRENT, UUID.randomUUID().toString(), "feature", issueDate, expiryDate, "trial", "none", "customer", - "elasticsearch", 5); + this( + License.VERSION_CURRENT, + UUID.randomUUID().toString(), + "feature", + issueDate, + expiryDate, + "trial", + "none", + "customer", + "elasticsearch", + 5 + ); } - public LicenseSpec(int version, String uid, String feature, long issueDateInMillis, long expiryDateInMillis, String type, - String subscriptionType, String issuedTo, String issuer, int maxNodes) { + public LicenseSpec( + int version, + String uid, + String feature, + long issueDateInMillis, + long expiryDateInMillis, + String type, + String subscriptionType, + String issuedTo, + String issuer, + int maxNodes + ) { this.version = version; this.feature = feature; this.issueDateInMillis = issueDateInMillis; @@ -196,8 +226,18 @@ public LicenseSpec(int version, String uid, String feature, long issueDateInMill this.maxNodes = maxNodes; } - public LicenseSpec(int version, String uid, String feature, String issueDate, String expiryDate, String type, - String subscriptionType, String issuedTo, String issuer, int maxNodes) { + public LicenseSpec( + int version, + String uid, + String feature, + String issueDate, + String expiryDate, + String type, + String subscriptionType, + String issuedTo, + String issuer, + int maxNodes + ) { this.version = version; this.feature = feature; this.issueDate = issueDate; @@ -212,6 +252,7 @@ public LicenseSpec(int version, String uid, String feature, String issueDate, St this.maxNodes = maxNodes; } } + private static Path getTestPriKeyPath() throws Exception { return getResourcePath("/private.key"); } @@ -249,13 +290,13 @@ public static License generateSignedLicense(String type, long issueDate, TimeVal public static License generateSignedLicenseOldSignature() { long issueDate = System.currentTimeMillis(); License.Builder specBuilder = License.builder() - .uid(UUID.randomUUID().toString()) - .version(License.VERSION_START_DATE) - .issuedTo("customer") - .maxNodes(5) - .type("trial") - .issueDate(issueDate) - .expiryDate(issueDate + TimeValue.timeValueHours(24).getMillis()); + .uid(UUID.randomUUID().toString()) + .version(License.VERSION_START_DATE) + .issuedTo("customer") + .maxNodes(5) + .type("trial") + .issueDate(issueDate) + .expiryDate(issueDate + TimeValue.timeValueHours(24).getMillis()); return SelfGeneratedLicense.create(specBuilder, License.VERSION_START_DATE); } @@ -272,22 +313,20 @@ public static License generateSignedLicense(String type, int version, long issue licenseType = (type != null) ? type : randomFrom("silver", "dev", "gold", "platinum"); } final License.Builder builder = License.builder() - .uid(UUID.randomUUID().toString()) - .version(version) - .expiryDate(System.currentTimeMillis() + expiryDuration.getMillis()) - .issueDate(issue) - .type(licenseType) - .issuedTo("customer") - .issuer("elasticsearch") - .maxNodes(5); + .uid(UUID.randomUUID().toString()) + .version(version) + .expiryDate(System.currentTimeMillis() + expiryDuration.getMillis()) + .issueDate(issue) + .type(licenseType) + .issuedTo("customer") + .issuer("elasticsearch") + .maxNodes(5); if (version == License.VERSION_START) { builder.subscriptionType((type != null) ? type : randomFrom("dev", "gold", "platinum", "silver")); builder.feature(randomAlphaOfLength(10)); } if ("enterprise".equals(licenseType)) { - builder.version(License.VERSION_ENTERPRISE) - .maxResourceUnits(randomIntBetween(5, 500)) - .maxNodes(-1); + builder.version(License.VERSION_ENTERPRISE).maxResourceUnits(randomIntBetween(5, 500)).maxNodes(-1); } final LicenseSigner signer = new LicenseSigner(getTestPriKeyPath(), getTestPubKeyPath()); return signer.sign(builder.build()); @@ -307,20 +346,22 @@ public static License generateExpiredNonBasicLicense() throws Exception { } public static License generateExpiredNonBasicLicense(String type) throws Exception { - return generateExpiredNonBasicLicense(type, - System.currentTimeMillis() - TimeValue.timeValueHours(randomIntBetween(1, 10)).getMillis()); + return generateExpiredNonBasicLicense( + type, + System.currentTimeMillis() - TimeValue.timeValueHours(randomIntBetween(1, 10)).getMillis() + ); } public static License generateExpiredNonBasicLicense(String type, long expiryDate) throws Exception { final License.Builder builder = License.builder() - .uid(UUID.randomUUID().toString()) - .version(License.VERSION_CURRENT) - .expiryDate(expiryDate) - .issueDate(expiryDate - TimeValue.timeValueMinutes(10).getMillis()) - .type(type) - .issuedTo("customer") - .issuer("elasticsearch") - .maxNodes(5); + .uid(UUID.randomUUID().toString()) + .version(License.VERSION_CURRENT) + .expiryDate(expiryDate) + .issueDate(expiryDate - TimeValue.timeValueMinutes(10).getMillis()) + .type(type) + .issuedTo("customer") + .issuer("elasticsearch") + .maxNodes(5); LicenseSigner signer = new LicenseSigner(getTestPriKeyPath(), getTestPubKeyPath()); return signer.sign(builder.build()); } @@ -333,8 +374,11 @@ private static Path getResourcePath(String resource) throws Exception { return resourceFile; } - public static void registerAndAckSignedLicenses(final LicenseService licenseService, License license, - final LicensesStatus expectedStatus) { + public static void registerAndAckSignedLicenses( + final LicenseService licenseService, + License license, + final LicensesStatus expectedStatus + ) { PutLicenseRequest putLicenseRequest = new PutLicenseRequest().license(license).acknowledge(true); final CountDownLatch latch = new CountDownLatch(1); final AtomicReference status = new AtomicReference<>(); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/XPackInfoRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/XPackInfoRequestTests.java index b08d060e1c0f4..b76d205487ffd 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/XPackInfoRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/XPackInfoRequestTests.java @@ -28,8 +28,10 @@ public void testSerializeUsing7xVersion() throws Exception { // At the time of writing, V7.8.1 is unreleased, so there's no easy way to use VersionUtils to get a random version between // 7.8.1 (inclusive) and 8.0.0 (exclusive), because the "version before 8.0.0" returns 7.8.0 (the most recent released version). // To work around this we accept that 8.0.0 is included in the range, and then filter it out using other-than - final Version version = randomValueOtherThan(Version.V_8_0_0, - () -> VersionUtils.randomVersionBetween(random(), Version.V_7_8_1, Version.V_8_0_0)); + final Version version = randomValueOtherThan( + Version.V_8_0_0, + () -> VersionUtils.randomVersionBetween(random(), Version.V_7_8_1, Version.V_8_0_0) + ); assertSerialization(version); } @@ -39,10 +41,16 @@ private void assertSerialization(Version version) throws java.io.IOException { request.setCategories(categories.isEmpty() ? EnumSet.noneOf(Category.class) : EnumSet.copyOf(categories)); request.setVerbose(randomBoolean()); final XPackInfoRequest read = copyWriteable(request, new NamedWriteableRegistry(List.of()), XPackInfoRequest::new, version); - assertThat("Serialized request with version [" + version + "] does not match original [categories]", - read.getCategories(), equalTo(request.getCategories())); - assertThat("Serialized request with version [" + version + "] does not match original [verbose]", - read.isVerbose(), equalTo(request.isVerbose())); + assertThat( + "Serialized request with version [" + version + "] does not match original [categories]", + read.getCategories(), + equalTo(request.getCategories()) + ); + assertThat( + "Serialized request with version [" + version + "] does not match original [verbose]", + read.isVerbose(), + equalTo(request.isVerbose()) + ); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/common/ProtocolUtilsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/common/ProtocolUtilsTests.java index c7e6326f9ba54..fcd7c9edcfd6a 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/common/ProtocolUtilsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/common/ProtocolUtilsTests.java @@ -11,7 +11,7 @@ import java.util.HashMap; import java.util.Map; -public class ProtocolUtilsTests extends ESTestCase { +public class ProtocolUtilsTests extends ESTestCase { public void testMapStringEqualsAndHash() { assertTrue(ProtocolUtils.equals(null, null)); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/LicenseStatusTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/LicenseStatusTests.java index 4330fbbb5f88e..d0939281991b9 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/LicenseStatusTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/LicenseStatusTests.java @@ -6,10 +6,10 @@ */ package org.elasticsearch.protocol.xpack.license; -import java.io.IOException; - import org.elasticsearch.test.ESTestCase; +import java.io.IOException; + public class LicenseStatusTests extends ESTestCase { public void testSerialization() throws IOException { LicenseStatus status = randomFrom(LicenseStatus.values()); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/script/MockMustacheScriptEngine.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/script/MockMustacheScriptEngine.java index f2e9641ca64b8..5aed2f0754ce9 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/script/MockMustacheScriptEngine.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/script/MockMustacheScriptEngine.java @@ -47,12 +47,11 @@ public T compile(String name, String script, ScriptContext context, Map - new TemplateScript(vars) { - @Override - public String execute() { - return script; - } - }); + return context.factoryClazz.cast((TemplateScript.Factory) vars -> new TemplateScript(vars) { + @Override + public String execute() { + return script; + } + }); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/snapshots/sourceonly/SourceOnlySnapshotShardTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/snapshots/sourceonly/SourceOnlySnapshotShardTests.java index 4c6fbd02baf3a..f79792f8c24ff 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/snapshots/sourceonly/SourceOnlySnapshotShardTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/snapshots/sourceonly/SourceOnlySnapshotShardTests.java @@ -90,16 +90,23 @@ public class SourceOnlySnapshotShardTests extends IndexShardTestCase { public void testSourceIncomplete() throws IOException { - ShardRouting shardRouting = TestShardRouting.newShardRouting(new ShardId("index", "_na_", 0), randomAlphaOfLength(10), true, - ShardRoutingState.INITIALIZING, RecoverySource.EmptyStoreRecoverySource.INSTANCE); - Settings settings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + ShardRouting shardRouting = TestShardRouting.newShardRouting( + new ShardId("index", "_na_", 0), + randomAlphaOfLength(10), + true, + ShardRoutingState.INITIALIZING, + RecoverySource.EmptyStoreRecoverySource.INSTANCE + ); + Settings settings = Settings.builder() + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) .build(); IndexMetadata metadata = IndexMetadata.builder(shardRouting.getIndexName()) .settings(settings) .primaryTerm(0, primaryTerm) - .putMapping("{\"_source\":{\"enabled\": false}}").build(); + .putMapping("{\"_source\":{\"enabled\": false}}") + .build(); IndexShard shard = newShard(shardRouting, metadata, null, new InternalEngineFactory()); recoverShardFromStore(shard); @@ -114,13 +121,28 @@ public void testSourceIncomplete() throws IOException { try (Engine.IndexCommitRef snapshotRef = shard.acquireLastIndexCommit(true)) { IndexShardSnapshotStatus indexShardSnapshotStatus = IndexShardSnapshotStatus.newInitializing(new ShardGeneration(-1L)); final PlainActionFuture future = PlainActionFuture.newFuture(); - runAsSnapshot(shard.getThreadPool(), () -> repository.snapshotShard(new SnapshotShardContext(shard.store(), - shard.mapperService(), snapshotId, indexId, snapshotRef, null, indexShardSnapshotStatus, Version.CURRENT, - Collections.emptyMap(), future))); + runAsSnapshot( + shard.getThreadPool(), + () -> repository.snapshotShard( + new SnapshotShardContext( + shard.store(), + shard.mapperService(), + snapshotId, + indexId, + snapshotRef, + null, + indexShardSnapshotStatus, + Version.CURRENT, + Collections.emptyMap(), + future + ) + ) + ); IllegalStateException illegalStateException = expectThrows(IllegalStateException.class, future::actionGet); assertEquals( "Can't snapshot _source only on an index that has incomplete source ie. has _source disabled or filters the source", - illegalStateException.getMessage()); + illegalStateException.getMessage() + ); } closeShards(shard); } @@ -141,9 +163,23 @@ public void testIncrementalSnapshot() throws IOException { IndexShardSnapshotStatus indexShardSnapshotStatus = IndexShardSnapshotStatus.newInitializing(null); SnapshotId snapshotId = new SnapshotId("test", "test"); final PlainActionFuture future = PlainActionFuture.newFuture(); - runAsSnapshot(shard.getThreadPool(), () -> repository.snapshotShard(new SnapshotShardContext(shard.store(), - shard.mapperService(), snapshotId, indexId, snapshotRef, null, indexShardSnapshotStatus, Version.CURRENT, - Collections.emptyMap(), future))); + runAsSnapshot( + shard.getThreadPool(), + () -> repository.snapshotShard( + new SnapshotShardContext( + shard.store(), + shard.mapperService(), + snapshotId, + indexId, + snapshotRef, + null, + indexShardSnapshotStatus, + Version.CURRENT, + Collections.emptyMap(), + future + ) + ) + ); shardGeneration = future.actionGet().getGeneration(); IndexShardSnapshotStatus.Copy copy = indexShardSnapshotStatus.asCopy(); assertEquals(copy.getTotalFileCount(), copy.getIncrementalFileCount()); @@ -158,15 +194,29 @@ public void testIncrementalSnapshot() throws IOException { IndexShardSnapshotStatus indexShardSnapshotStatus = IndexShardSnapshotStatus.newInitializing(shardGeneration); final PlainActionFuture future = PlainActionFuture.newFuture(); - runAsSnapshot(shard.getThreadPool(), () -> repository.snapshotShard(new SnapshotShardContext(shard.store(), - shard.mapperService(), snapshotId, indexId, snapshotRef, null, indexShardSnapshotStatus, Version.CURRENT, - Collections.emptyMap(), future))); + runAsSnapshot( + shard.getThreadPool(), + () -> repository.snapshotShard( + new SnapshotShardContext( + shard.store(), + shard.mapperService(), + snapshotId, + indexId, + snapshotRef, + null, + indexShardSnapshotStatus, + Version.CURRENT, + Collections.emptyMap(), + future + ) + ) + ); shardGeneration = future.actionGet().getGeneration(); IndexShardSnapshotStatus.Copy copy = indexShardSnapshotStatus.asCopy(); // we processed the segments_N file plus _1.si, _1.fnm, _1.fdx, _1.fdt, _1.fdm assertEquals(6, copy.getIncrementalFileCount()); // in total we have 5 more files than the previous snap since we don't count the segments_N twice - assertEquals(totalFileCount+5, copy.getTotalFileCount()); + assertEquals(totalFileCount + 5, copy.getTotalFileCount()); assertEquals(copy.getStage(), IndexShardSnapshotStatus.Stage.DONE); } deleteDoc(shard, Integer.toString(10)); @@ -175,15 +225,29 @@ public void testIncrementalSnapshot() throws IOException { IndexShardSnapshotStatus indexShardSnapshotStatus = IndexShardSnapshotStatus.newInitializing(shardGeneration); final PlainActionFuture future = PlainActionFuture.newFuture(); - runAsSnapshot(shard.getThreadPool(), () -> repository.snapshotShard(new SnapshotShardContext(shard.store(), - shard.mapperService(), snapshotId, indexId, snapshotRef, null, indexShardSnapshotStatus, Version.CURRENT, - Collections.emptyMap(), future))); + runAsSnapshot( + shard.getThreadPool(), + () -> repository.snapshotShard( + new SnapshotShardContext( + shard.store(), + shard.mapperService(), + snapshotId, + indexId, + snapshotRef, + null, + indexShardSnapshotStatus, + Version.CURRENT, + Collections.emptyMap(), + future + ) + ) + ); future.actionGet(); IndexShardSnapshotStatus.Copy copy = indexShardSnapshotStatus.asCopy(); // we processed the segments_N file plus _1_1.liv assertEquals(2, copy.getIncrementalFileCount()); // in total we have 6 more files than the previous snap since we don't count the segments_N twice - assertEquals(totalFileCount+6, copy.getTotalFileCount()); + assertEquals(totalFileCount + 6, copy.getTotalFileCount()); assertEquals(copy.getStage(), IndexShardSnapshotStatus.Stage.DONE); } closeShards(shard); @@ -224,32 +288,48 @@ public void testRestoreMinmal() throws IOException { IndexShardSnapshotStatus indexShardSnapshotStatus = IndexShardSnapshotStatus.newInitializing(null); final PlainActionFuture future = PlainActionFuture.newFuture(); runAsSnapshot(shard.getThreadPool(), () -> { - repository.snapshotShard(new SnapshotShardContext(shard.store(), shard.mapperService(), snapshotId, indexId, snapshotRef, - null, indexShardSnapshotStatus, Version.CURRENT, Collections.emptyMap(), future)); - future.actionGet(); - final PlainActionFuture> finFuture = PlainActionFuture.newFuture(); - final ShardGenerations shardGenerations = - ShardGenerations.builder().put(indexId, 0, indexShardSnapshotStatus.generation()).build(); - repository.finalizeSnapshot(new FinalizeSnapshotContext( - shardGenerations, - ESBlobStoreRepositoryIntegTestCase.getRepositoryData(repository).getGenId(), - Metadata.builder().put(shard.indexSettings().getIndexMetadata(), false).build(), - new SnapshotInfo( - new Snapshot(repository.getMetadata().name(), snapshotId), - shardGenerations.indices().stream() - .map(IndexId::getName).collect(Collectors.toList()), - Collections.emptyList(), - Collections.emptyList(), + repository.snapshotShard( + new SnapshotShardContext( + shard.store(), + shard.mapperService(), + snapshotId, + indexId, + snapshotRef, null, - 1L, - shardGenerations.totalShards(), - Collections.emptyList(), - true, + indexShardSnapshotStatus, + Version.CURRENT, Collections.emptyMap(), - 0L, - Collections.emptyMap()), - Version.CURRENT, finFuture - )); + future + ) + ); + future.actionGet(); + final PlainActionFuture> finFuture = PlainActionFuture.newFuture(); + final ShardGenerations shardGenerations = ShardGenerations.builder() + .put(indexId, 0, indexShardSnapshotStatus.generation()) + .build(); + repository.finalizeSnapshot( + new FinalizeSnapshotContext( + shardGenerations, + ESBlobStoreRepositoryIntegTestCase.getRepositoryData(repository).getGenId(), + Metadata.builder().put(shard.indexSettings().getIndexMetadata(), false).build(), + new SnapshotInfo( + new Snapshot(repository.getMetadata().name(), snapshotId), + shardGenerations.indices().stream().map(IndexId::getName).collect(Collectors.toList()), + Collections.emptyList(), + Collections.emptyList(), + null, + 1L, + shardGenerations.totalShards(), + Collections.emptyList(), + true, + Collections.emptyMap(), + 0L, + Collections.emptyMap() + ), + Version.CURRENT, + finFuture + ) + ); finFuture.actionGet(); }); IndexShardSnapshotStatus.Copy copy = indexShardSnapshotStatus.asCopy(); @@ -257,14 +337,30 @@ public void testRestoreMinmal() throws IOException { assertEquals(copy.getStage(), IndexShardSnapshotStatus.Stage.DONE); } shard.refresh("test"); - ShardRouting shardRouting = TestShardRouting.newShardRouting(new ShardId("index", "_na_", 0), randomAlphaOfLength(10), true, + ShardRouting shardRouting = TestShardRouting.newShardRouting( + new ShardId("index", "_na_", 0), + randomAlphaOfLength(10), + true, ShardRoutingState.INITIALIZING, new RecoverySource.SnapshotRecoverySource( - UUIDs.randomBase64UUID(), new Snapshot("src_only", snapshotId), Version.CURRENT, indexId)); - IndexMetadata metadata = runAsSnapshot(threadPool, () -> - repository.getSnapshotIndexMetaData(PlainActionFuture.get(repository::getRepositoryData), snapshotId, indexId)); + UUIDs.randomBase64UUID(), + new Snapshot("src_only", snapshotId), + Version.CURRENT, + indexId + ) + ); + IndexMetadata metadata = runAsSnapshot( + threadPool, + () -> repository.getSnapshotIndexMetaData(PlainActionFuture.get(repository::getRepositoryData), snapshotId, indexId) + ); IndexShard restoredShard = newShard( - shardRouting, metadata, null, SourceOnlySnapshotRepository.getEngineFactory(), () -> {}, RetentionLeaseSyncer.EMPTY); + shardRouting, + metadata, + null, + SourceOnlySnapshotRepository.getEngineFactory(), + () -> {}, + RetentionLeaseSyncer.EMPTY + ); DiscoveryNode discoveryNode = new DiscoveryNode("node_g", buildNewFakeTransportAddress(), Version.CURRENT); restoredShard.markAsRecovering("test from snap", new RecoveryState(restoredShard.routingEntry(), discoveryNode, null)); runAsSnapshot(shard.getThreadPool(), () -> { @@ -277,8 +373,10 @@ public void testRestoreMinmal() throws IOException { assertEquals(IndexShardState.POST_RECOVERY, restoredShard.state()); restoredShard.refresh("test"); assertEquals(restoredShard.docStats().getCount(), shard.docStats().getCount()); - EngineException engineException = expectThrows(EngineException.class, () -> restoredShard.get( - new Engine.Get(false, false, Integer.toString(0)))); + EngineException engineException = expectThrows( + EngineException.class, + () -> restoredShard.get(new Engine.Get(false, false, Integer.toString(0))) + ); assertEquals(engineException.getCause().getMessage(), "_source only indices can't be searched or filtered"); SeqNoStats seqNoStats = restoredShard.seqNoStats(); assertEquals(seqNoStats.getMaxSeqNo(), seqNoStats.getLocalCheckpoint()); @@ -287,20 +385,26 @@ public void testRestoreMinmal() throws IOException { assertEquals(searcher.getIndexReader().maxDoc(), seqNoStats.getLocalCheckpoint()); TopDocs search = searcher.search(new MatchAllDocsQuery(), Integer.MAX_VALUE); assertEquals(searcher.getIndexReader().numDocs(), search.totalHits.value); - search = searcher.search(new MatchAllDocsQuery(), Integer.MAX_VALUE, - new Sort(new SortField(SeqNoFieldMapper.NAME, SortField.Type.LONG)), false); + search = searcher.search( + new MatchAllDocsQuery(), + Integer.MAX_VALUE, + new Sort(new SortField(SeqNoFieldMapper.NAME, SortField.Type.LONG)), + false + ); assertEquals(searcher.getIndexReader().numDocs(), search.totalHits.value); long previous = -1; for (ScoreDoc doc : search.scoreDocs) { FieldDoc fieldDoc = (FieldDoc) doc; assertEquals(1, fieldDoc.fields.length); - long current = (Long)fieldDoc.fields[0]; + long current = (Long) fieldDoc.fields[0]; assertThat(previous, Matchers.lessThan(current)); previous = current; } expectThrows(UnsupportedOperationException.class, () -> searcher.search(new TermQuery(new Term("boom", "boom")), 1)); - targetShard = reindex(searcher.getDirectoryReader(), new MappingMetadata("_doc", - restoredShard.mapperService().documentMapper().mapping().getMeta())); + targetShard = reindex( + searcher.getDirectoryReader(), + new MappingMetadata("_doc", restoredShard.mapperService().documentMapper().mapping().getMeta()) + ); } for (int i = 0; i < numInitialDocs; i++) { @@ -323,9 +427,15 @@ public void testRestoreMinmal() throws IOException { } public IndexShard reindex(DirectoryReader reader, MappingMetadata mapping) throws IOException { - ShardRouting targetShardRouting = TestShardRouting.newShardRouting(new ShardId("target", "_na_", 0), randomAlphaOfLength(10), true, - ShardRoutingState.INITIALIZING, RecoverySource.EmptyStoreRecoverySource.INSTANCE); - Settings settings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + ShardRouting targetShardRouting = TestShardRouting.newShardRouting( + new ShardId("target", "_na_", 0), + randomAlphaOfLength(10), + true, + ShardRoutingState.INITIALIZING, + RecoverySource.EmptyStoreRecoverySource.INSTANCE + ); + Settings settings = Settings.builder() + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) .build(); @@ -350,13 +460,27 @@ public IndexShard reindex(DirectoryReader reader, MappingMetadata mapping) throw String id = rootFieldsVisitor.id(); BytesReference source = rootFieldsVisitor.source(); assert source != null : "_source is null but should have been filtered out at snapshot time"; - Engine.Result result = targetShard.applyIndexOperationOnPrimary(Versions.MATCH_ANY, VersionType.INTERNAL, - new SourceToParse(index, id, source, XContentHelper.xContentType(source), - rootFieldsVisitor.routing(), Map.of()), SequenceNumbers.UNASSIGNED_SEQ_NO, 0, - IndexRequest.UNSET_AUTO_GENERATED_TIMESTAMP, false); + Engine.Result result = targetShard.applyIndexOperationOnPrimary( + Versions.MATCH_ANY, + VersionType.INTERNAL, + new SourceToParse( + index, + id, + source, + XContentHelper.xContentType(source), + rootFieldsVisitor.routing(), + Map.of() + ), + SequenceNumbers.UNASSIGNED_SEQ_NO, + 0, + IndexRequest.UNSET_AUTO_GENERATED_TIMESTAMP, + false + ); if (result.getResultType() != Engine.Result.Type.SUCCESS) { - throw new IllegalStateException("failed applying post restore operation result: " + result - .getResultType(), result.getFailure()); + throw new IllegalStateException( + "failed applying post restore operation result: " + result.getResultType(), + result.getFailure() + ); } } } @@ -371,14 +495,15 @@ public IndexShard reindex(DirectoryReader reader, MappingMetadata mapping) throw return targetShard; } - /** Create a {@link Environment} with random path.home and path.repo **/ private Environment createEnvironment() { Path home = createTempDir(); - return TestEnvironment.newEnvironment(Settings.builder() - .put(Environment.PATH_HOME_SETTING.getKey(), home.toAbsolutePath()) - .put(Environment.PATH_REPO_SETTING.getKey(), home.resolve("repo").toAbsolutePath()) - .build()); + return TestEnvironment.newEnvironment( + Settings.builder() + .put(Environment.PATH_HOME_SETTING.getKey(), home.toAbsolutePath()) + .put(Environment.PATH_REPO_SETTING.getKey(), home.resolve("repo").toAbsolutePath()) + .build() + ); } /** Create a {@link Repository} with a random name **/ @@ -386,9 +511,14 @@ private Repository createRepository() { Settings settings = Settings.builder().put("location", randomAlphaOfLength(10)).build(); RepositoryMetadata repositoryMetadata = new RepositoryMetadata(randomAlphaOfLength(10), FsRepository.TYPE, settings); final ClusterService clusterService = BlobStoreTestUtil.mockClusterService(repositoryMetadata); - final Repository repository = new FsRepository(repositoryMetadata, createEnvironment(), xContentRegistry(), clusterService, + final Repository repository = new FsRepository( + repositoryMetadata, + createEnvironment(), + xContentRegistry(), + clusterService, MockBigArrays.NON_RECYCLING_INSTANCE, - new RecoverySettings(settings, new ClusterSettings(settings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS))); + new RecoverySettings(settings, new ClusterSettings(settings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS)) + ); clusterService.addStateApplier(e -> repository.updateState(e.state())); // Apply state once to initialize repo properly like RepositoriesService would repository.updateState(clusterService.state()); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/snapshots/sourceonly/SourceOnlySnapshotTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/snapshots/sourceonly/SourceOnlySnapshotTests.java index 1f7829c2c7bfc..c0ca62441d356 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/snapshots/sourceonly/SourceOnlySnapshotTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/snapshots/sourceonly/SourceOnlySnapshotTests.java @@ -52,16 +52,18 @@ public class SourceOnlySnapshotTests extends ESTestCase { public void testSourceOnlyRandom() throws IOException { try (Directory dir = newDirectory(); BaseDirectoryWrapper targetDir = newDirectory()) { SnapshotDeletionPolicy deletionPolicy = new SnapshotDeletionPolicy(new KeepOnlyLastCommitDeletionPolicy()); - IndexWriterConfig indexWriterConfig = newIndexWriterConfig().setIndexDeletionPolicy - (deletionPolicy).setSoftDeletesField(random().nextBoolean() ? null : Lucene.SOFT_DELETES_FIELD); + IndexWriterConfig indexWriterConfig = newIndexWriterConfig().setIndexDeletionPolicy(deletionPolicy) + .setSoftDeletesField(random().nextBoolean() ? null : Lucene.SOFT_DELETES_FIELD); try (RandomIndexWriter writer = new RandomIndexWriter(random(), dir, indexWriterConfig, false)) { final String softDeletesField = writer.w.getConfig().getSoftDeletesField(); // we either use the soft deletes directly or manually delete them to test the additional delete functionality boolean modifyDeletedDocs = softDeletesField != null && randomBoolean(); targetDir.setCheckIndexOnClose(false); final SourceOnlySnapshot.LinkedFilesDirectory wrappedDir = new SourceOnlySnapshot.LinkedFilesDirectory(targetDir); - SourceOnlySnapshot snapshoter = new SourceOnlySnapshot(wrappedDir, - modifyDeletedDocs ? () -> new DocValuesFieldExistsQuery(softDeletesField) : null) { + SourceOnlySnapshot snapshoter = new SourceOnlySnapshot( + wrappedDir, + modifyDeletedDocs ? () -> new DocValuesFieldExistsQuery(softDeletesField) : null + ) { @Override DirectoryReader wrapReader(DirectoryReader reader) throws IOException { return modifyDeletedDocs ? reader : super.wrapReader(reader); @@ -96,12 +98,14 @@ DirectoryReader wrapReader(DirectoryReader reader) throws IOException { IndexCommit snapshot = deletionPolicy.snapshot(); try { snapshoter.syncSnapshot(snapshot); - try (DirectoryReader snapReader = snapshoter.wrapReader(DirectoryReader.open(wrappedDir)); - DirectoryReader wrappedReader = snapshoter.wrapReader(DirectoryReader.open(snapshot))) { - DirectoryReader reader = modifyDeletedDocs - ? new SoftDeletesDirectoryReaderWrapper(wrappedReader, softDeletesField) : - new DropFullDeletedSegmentsReader(wrappedReader); - logger.warn(snapReader + " " + reader); + try ( + DirectoryReader snapReader = snapshoter.wrapReader(DirectoryReader.open(wrappedDir)); + DirectoryReader wrappedReader = snapshoter.wrapReader(DirectoryReader.open(snapshot)) + ) { + DirectoryReader reader = modifyDeletedDocs + ? new SoftDeletesDirectoryReaderWrapper(wrappedReader, softDeletesField) + : new DropFullDeletedSegmentsReader(wrappedReader); + logger.warn(snapReader + " " + reader); assertEquals(snapReader.maxDoc(), reader.maxDoc()); assertEquals(snapReader.numDocs(), reader.numDocs()); for (int i = 0; i < snapReader.maxDoc(); i++) { @@ -141,14 +145,17 @@ private Document newRandomDocument(int id) { public void testSrcOnlySnap() throws IOException { try (Directory dir = newDirectory()) { SnapshotDeletionPolicy deletionPolicy = new SnapshotDeletionPolicy(new KeepOnlyLastCommitDeletionPolicy()); - IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig() - .setSoftDeletesField(Lucene.SOFT_DELETES_FIELD) - .setIndexDeletionPolicy(deletionPolicy).setMergePolicy(new FilterMergePolicy(NoMergePolicy.INSTANCE) { - @Override - public boolean useCompoundFile(SegmentInfos infos, SegmentCommitInfo mergedInfo, MergeContext mergeContext) { - return randomBoolean(); - } - })); + IndexWriter writer = new IndexWriter( + dir, + newIndexWriterConfig().setSoftDeletesField(Lucene.SOFT_DELETES_FIELD) + .setIndexDeletionPolicy(deletionPolicy) + .setMergePolicy(new FilterMergePolicy(NoMergePolicy.INSTANCE) { + @Override + public boolean useCompoundFile(SegmentInfos infos, SegmentCommitInfo mergedInfo, MergeContext mergeContext) { + return randomBoolean(); + } + }) + ); Document doc = new Document(); doc.add(new StringField("id", "1", Field.Store.YES)); doc.add(new TextField("text", "the quick brown fox", Field.Store.NO)); @@ -231,7 +238,7 @@ public boolean useCompoundFile(SegmentInfos infos, SegmentCommitInfo mergedInfo, fail("unexpected extension: " + extension); } } - try(DirectoryReader snapReader = DirectoryReader.open(wrappedDir)) { + try (DirectoryReader snapReader = DirectoryReader.open(wrappedDir)) { assertEquals(snapReader.maxDoc(), 5); assertEquals(snapReader.numDocs(), 4); } @@ -257,7 +264,7 @@ public boolean useCompoundFile(SegmentInfos infos, SegmentCommitInfo mergedInfo, fail("unexpected extension: " + extension); } } - try(DirectoryReader snapReader = DirectoryReader.open(wrappedDir)) { + try (DirectoryReader snapReader = DirectoryReader.open(wrappedDir)) { assertEquals(snapReader.maxDoc(), 5); assertEquals(snapReader.numDocs(), 3); } @@ -272,19 +279,22 @@ public boolean useCompoundFile(SegmentInfos infos, SegmentCommitInfo mergedInfo, public void testFullyDeletedSegments() throws IOException { try (Directory dir = newDirectory()) { SnapshotDeletionPolicy deletionPolicy = new SnapshotDeletionPolicy(new KeepOnlyLastCommitDeletionPolicy()); - IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig() - .setSoftDeletesField(Lucene.SOFT_DELETES_FIELD) - .setIndexDeletionPolicy(deletionPolicy).setMergePolicy(new FilterMergePolicy(NoMergePolicy.INSTANCE) { - @Override - public boolean useCompoundFile(SegmentInfos infos, SegmentCommitInfo mergedInfo, MergeContext mergeContext) { - return randomBoolean(); - } + IndexWriter writer = new IndexWriter( + dir, + newIndexWriterConfig().setSoftDeletesField(Lucene.SOFT_DELETES_FIELD) + .setIndexDeletionPolicy(deletionPolicy) + .setMergePolicy(new FilterMergePolicy(NoMergePolicy.INSTANCE) { + @Override + public boolean useCompoundFile(SegmentInfos infos, SegmentCommitInfo mergedInfo, MergeContext mergeContext) { + return randomBoolean(); + } - @Override - public boolean keepFullyDeletedSegment(IOSupplier readerIOSupplier) throws IOException { - return true; - } - })); + @Override + public boolean keepFullyDeletedSegment(IOSupplier readerIOSupplier) throws IOException { + return true; + } + }) + ); Document doc = new Document(); doc.add(new StringField("id", "1", Field.Store.YES)); doc.add(new TextField("text", "the quick brown fox", Field.Store.NO)); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/test/http/MockWebServer.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/test/http/MockWebServer.java index 17663e43da066..79daf910d77e2 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/test/http/MockWebServer.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/test/http/MockWebServer.java @@ -11,18 +11,18 @@ import com.sun.net.httpserver.HttpsConfigurator; import com.sun.net.httpserver.HttpsParameters; import com.sun.net.httpserver.HttpsServer; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.message.ParameterizedMessage; import org.apache.logging.log4j.util.Supplier; import org.elasticsearch.common.Strings; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.common.io.Streams; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.ConcurrentCollections; +import org.elasticsearch.core.SuppressForbidden; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.mocksocket.MockHttpServer; -import javax.net.ssl.SSLContext; import java.io.Closeable; import java.io.IOException; import java.io.InputStreamReader; @@ -41,6 +41,8 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.TimeUnit; +import javax.net.ssl.SSLContext; + import static org.elasticsearch.test.ESTestCase.terminate; /** @@ -109,8 +111,15 @@ public void start() throws IOException { requests.add(request); if (logger.isDebugEnabled()) { - logger.debug("[{}:{}] incoming HTTP request [{} {}], returning status [{}] body [{}]", getHostName(), getPort(), - s.getRequestMethod(), s.getRequestURI(), response.getStatusCode(), getStartOfBody(response)); + logger.debug( + "[{}:{}] incoming HTTP request [{} {}], returning status [{}] body [{}]", + getHostName(), + getPort(), + s.getRequestMethod(), + s.getRequestURI(), + response.getStatusCode(), + getStartOfBody(response) + ); } sleepIfNeeded(response.getBeforeReplyDelay()); @@ -130,8 +139,14 @@ public void start() throws IOException { } } } catch (Exception e) { - logger.error((Supplier) () -> new ParameterizedMessage("failed to respond to request [{} {}]", - s.getRequestMethod(), s.getRequestURI()), e); + logger.error( + (Supplier) () -> new ParameterizedMessage( + "failed to respond to request [{} {}]", + s.getRequestMethod(), + s.getRequestURI() + ), + e + ); } finally { s.close(); } @@ -194,10 +209,11 @@ private void sleepIfNeeded(TimeValue timeValue) throws InterruptedException { */ private MockRequest createRequest(HttpExchange exchange) throws IOException { MockRequest request = new MockRequest( - exchange.getRequestMethod(), - exchange.getRequestURI(), - exchange.getRequestHeaders(), - exchange.getRemoteAddress()); + exchange.getRequestMethod(), + exchange.getRequestURI(), + exchange.getRequestHeaders(), + exchange.getRemoteAddress() + ); if (exchange.getRequestBody() != null) { String body = Streams.copyToString(new InputStreamReader(exchange.getRequestBody(), StandardCharsets.UTF_8)); if (Strings.isEmpty(body) == false) { @@ -228,8 +244,14 @@ public int getPort() { */ public void enqueue(MockResponse response) { if (logger.isTraceEnabled()) { - logger.trace("[{}:{}] Enqueueing response [{}], status [{}] body [{}]", getHostName(), getPort(), responses.size(), - response.getStatusCode(), getStartOfBody(response)); + logger.trace( + "[{}:{}] Enqueueing response [{}], status [{}] body [{}]", + getHostName(), + getPort(), + responses.size(), + response.getStatusCode(), + getStartOfBody(response) + ); } responses.add(response); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/cluster/action/MigrateToDataTiersResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/cluster/action/MigrateToDataTiersResponseTests.java index 70e69628a52ec..df7178a786fee 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/cluster/action/MigrateToDataTiersResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/cluster/action/MigrateToDataTiersResponseTests.java @@ -22,8 +22,12 @@ protected Writeable.Reader instanceReader() { @Override protected MigrateToDataTiersResponse createTestInstance() { boolean dryRun = randomBoolean(); - return new MigrateToDataTiersResponse(randomAlphaOfLength(10), randomList(1, 5, () -> randomAlphaOfLengthBetween(5, 50)), - randomList(1, 5, () -> randomAlphaOfLengthBetween(5, 50)), dryRun); + return new MigrateToDataTiersResponse( + randomAlphaOfLength(10), + randomList(1, 5, () -> randomAlphaOfLengthBetween(5, 50)), + randomList(1, 5, () -> randomAlphaOfLengthBetween(5, 50)), + dryRun + ); } @Override @@ -31,18 +35,33 @@ protected MigrateToDataTiersResponse mutateInstance(MigrateToDataTiersResponse i int i = randomIntBetween(0, 3); switch (i) { case 0: - return new MigrateToDataTiersResponse(randomValueOtherThan(instance.getRemovedIndexTemplateName(), - () -> randomAlphaOfLengthBetween(5, 15)), instance.getMigratedPolicies(), instance.getMigratedIndices(), - instance.isDryRun()); + return new MigrateToDataTiersResponse( + randomValueOtherThan(instance.getRemovedIndexTemplateName(), () -> randomAlphaOfLengthBetween(5, 15)), + instance.getMigratedPolicies(), + instance.getMigratedIndices(), + instance.isDryRun() + ); case 1: - return new MigrateToDataTiersResponse(instance.getRemovedIndexTemplateName(), - randomList(6, 10, () -> randomAlphaOfLengthBetween(5, 50)), instance.getMigratedIndices(), instance.isDryRun()); + return new MigrateToDataTiersResponse( + instance.getRemovedIndexTemplateName(), + randomList(6, 10, () -> randomAlphaOfLengthBetween(5, 50)), + instance.getMigratedIndices(), + instance.isDryRun() + ); case 2: - return new MigrateToDataTiersResponse(instance.getRemovedIndexTemplateName(), instance.getMigratedPolicies(), - randomList(6, 10, () -> randomAlphaOfLengthBetween(5, 50)), instance.isDryRun()); + return new MigrateToDataTiersResponse( + instance.getRemovedIndexTemplateName(), + instance.getMigratedPolicies(), + randomList(6, 10, () -> randomAlphaOfLengthBetween(5, 50)), + instance.isDryRun() + ); case 3: - return new MigrateToDataTiersResponse(instance.getRemovedIndexTemplateName(), instance.getMigratedPolicies(), - instance.getMigratedIndices(), instance.isDryRun() ? false : true); + return new MigrateToDataTiersResponse( + instance.getRemovedIndexTemplateName(), + instance.getMigratedPolicies(), + instance.getMigratedIndices(), + instance.isDryRun() ? false : true + ); default: throw new UnsupportedOperationException(); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/cluster/routing/allocation/DataTierAllocationDeciderTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/cluster/routing/allocation/DataTierAllocationDeciderTests.java index 3f1c12c55a39f..b7dea77029521 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/cluster/routing/allocation/DataTierAllocationDeciderTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/cluster/routing/allocation/DataTierAllocationDeciderTests.java @@ -23,6 +23,7 @@ import org.elasticsearch.cluster.routing.ShardRouting; import org.elasticsearch.cluster.routing.UnassignedInfo; import org.elasticsearch.cluster.routing.allocation.AllocationService; +import org.elasticsearch.cluster.routing.allocation.DataTier; import org.elasticsearch.cluster.routing.allocation.RoutingAllocation; import org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator; import org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders; @@ -38,7 +39,6 @@ import org.elasticsearch.snapshots.EmptySnapshotsInfoService; import org.elasticsearch.snapshots.SearchableSnapshotsSettings; import org.elasticsearch.test.gateway.TestGatewayAllocator; -import org.elasticsearch.cluster.routing.allocation.DataTier; import java.util.ArrayList; import java.util.Arrays; @@ -56,38 +56,55 @@ public class DataTierAllocationDeciderTests extends ESAllocationTestCase { private static final DiscoveryNode HOT_NODE = newNode("node-hot", Collections.singleton(DiscoveryNodeRole.DATA_HOT_NODE_ROLE)); private static final DiscoveryNode WARM_NODE = newNode("node-warm", Collections.singleton(DiscoveryNodeRole.DATA_WARM_NODE_ROLE)); private static final DiscoveryNode COLD_NODE = newNode("node-cold", Collections.singleton(DiscoveryNodeRole.DATA_COLD_NODE_ROLE)); - private static final DiscoveryNode CONTENT_NODE = - newNode("node-content", Collections.singleton(DiscoveryNodeRole.DATA_CONTENT_NODE_ROLE)); + private static final DiscoveryNode CONTENT_NODE = newNode( + "node-content", + Collections.singleton(DiscoveryNodeRole.DATA_CONTENT_NODE_ROLE) + ); private static final DiscoveryNode DATA_NODE = newNode("node-data", Collections.singleton(DiscoveryNodeRole.DATA_ROLE)); private final ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); private final DataTierAllocationDecider decider = new DataTierAllocationDecider(); private final AllocationDeciders allocationDeciders = new AllocationDeciders( - Arrays.asList(decider, + Arrays.asList( + decider, new SameShardAllocationDecider(Settings.EMPTY, clusterSettings), - new ReplicaAfterPrimaryActiveAllocationDecider())); - private final AllocationService service = new AllocationService(allocationDeciders, - new TestGatewayAllocator(), new BalancedShardsAllocator(Settings.EMPTY), EmptyClusterInfoService.INSTANCE, - EmptySnapshotsInfoService.INSTANCE); - - private final ShardRouting shard = ShardRouting.newUnassigned(new ShardId("myindex", "myindex", 0), true, - RecoverySource.EmptyStoreRecoverySource.INSTANCE, new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "index created")); + new ReplicaAfterPrimaryActiveAllocationDecider() + ) + ); + private final AllocationService service = new AllocationService( + allocationDeciders, + new TestGatewayAllocator(), + new BalancedShardsAllocator(Settings.EMPTY), + EmptyClusterInfoService.INSTANCE, + EmptySnapshotsInfoService.INSTANCE + ); + + private final ShardRouting shard = ShardRouting.newUnassigned( + new ShardId("myindex", "myindex", 0), + true, + RecoverySource.EmptyStoreRecoverySource.INSTANCE, + new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "index created") + ); public void testIndexPrefer() { ClusterState state = ClusterState.builder(service.reroute(ClusterState.EMPTY_STATE, "initial state")) - .nodes(DiscoveryNodes.builder() - .add(HOT_NODE) - .build()) - .metadata(Metadata.builder() - .put(IndexMetadata.builder("myindex") - .settings(Settings.builder() - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(IndexMetadata.SETTING_INDEX_UUID, "myindex") - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put(DataTier.TIER_PREFERENCE, "data_warm,data_cold") - .build())) - .build()) + .nodes(DiscoveryNodes.builder().add(HOT_NODE).build()) + .metadata( + Metadata.builder() + .put( + IndexMetadata.builder("myindex") + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetadata.SETTING_INDEX_UUID, "myindex") + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put(DataTier.TIER_PREFERENCE, "data_warm,data_cold") + .build() + ) + ) + .build() + ) .build(); RoutingAllocation allocation = new RoutingAllocation(allocationDeciders, state.getRoutingNodes(), state, null, null, 0); allocation.debugDecision(true); @@ -98,31 +115,44 @@ public void testIndexPrefer() { node = new RoutingNode(n.getId(), n, shard); d = decider.canAllocate(shard, node, allocation); assertThat(node.toString(), d.type(), equalTo(Decision.Type.NO)); - assertThat(node.toString(), d.getExplanation(), - containsString("index has a preference for tiers [data_warm,data_cold], " + - "but no nodes for any of those tiers are available in the cluster")); + assertThat( + node.toString(), + d.getExplanation(), + containsString( + "index has a preference for tiers [data_warm,data_cold], " + + "but no nodes for any of those tiers are available in the cluster" + ) + ); d = decider.canRemain(shard, node, allocation); assertThat(node.toString(), d.type(), equalTo(Decision.Type.NO)); - assertThat(node.toString(), d.getExplanation(), - containsString("index has a preference for tiers [data_warm,data_cold], " + - "but no nodes for any of those tiers are available in the cluster")); + assertThat( + node.toString(), + d.getExplanation(), + containsString( + "index has a preference for tiers [data_warm,data_cold], " + + "but no nodes for any of those tiers are available in the cluster" + ) + ); } state = ClusterState.builder(service.reroute(ClusterState.EMPTY_STATE, "initial state")) - .nodes(DiscoveryNodes.builder() - .add(HOT_NODE) - .add(COLD_NODE) - .build()) - .metadata(Metadata.builder() - .put(IndexMetadata.builder("myindex") - .settings(Settings.builder() - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(IndexMetadata.SETTING_INDEX_UUID, "myindex") - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put(DataTier.TIER_PREFERENCE, "data_warm,data_cold") - .build())) - .build()) + .nodes(DiscoveryNodes.builder().add(HOT_NODE).add(COLD_NODE).build()) + .metadata( + Metadata.builder() + .put( + IndexMetadata.builder("myindex") + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetadata.SETTING_INDEX_UUID, "myindex") + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put(DataTier.TIER_PREFERENCE, "data_warm,data_cold") + .build() + ) + ) + .build() + ) .build(); allocation = new RoutingAllocation(allocationDeciders, state.getRoutingNodes(), state, null, null, 0); allocation.debugDecision(true); @@ -131,26 +161,40 @@ public void testIndexPrefer() { node = new RoutingNode(n.getId(), n, shard); d = decider.canAllocate(shard, node, allocation); assertThat(node.toString(), d.type(), equalTo(Decision.Type.NO)); - assertThat(node.toString(), d.getExplanation(), - containsString("index has a preference for tiers [data_warm,data_cold] " + - "and node does not meet the required [data_cold] tier")); + assertThat( + node.toString(), + d.getExplanation(), + containsString( + "index has a preference for tiers [data_warm,data_cold] " + "and node does not meet the required [data_cold] tier" + ) + ); d = decider.canRemain(shard, node, allocation); assertThat(node.toString(), d.type(), equalTo(Decision.Type.NO)); - assertThat(node.toString(), d.getExplanation(), - containsString("index has a preference for tiers [data_warm,data_cold] " + - "and node does not meet the required [data_cold] tier")); + assertThat( + node.toString(), + d.getExplanation(), + containsString( + "index has a preference for tiers [data_warm,data_cold] " + "and node does not meet the required [data_cold] tier" + ) + ); } for (DiscoveryNode n : Arrays.asList(COLD_NODE)) { node = new RoutingNode(n.getId(), n, shard); d = decider.canAllocate(shard, node, allocation); assertThat(node.toString(), d.type(), equalTo(Decision.Type.YES)); - assertThat(node.toString(), d.getExplanation(), - containsString("index has a preference for tiers [data_warm,data_cold] and node has tier [data_cold]")); + assertThat( + node.toString(), + d.getExplanation(), + containsString("index has a preference for tiers [data_warm,data_cold] and node has tier [data_cold]") + ); d = decider.canRemain(shard, node, allocation); assertThat(node.toString(), d.type(), equalTo(Decision.Type.YES)); - assertThat(node.toString(), d.getExplanation(), - containsString("index has a preference for tiers [data_warm,data_cold] and node has tier [data_cold]")); + assertThat( + node.toString(), + d.getExplanation(), + containsString("index has a preference for tiers [data_warm,data_cold] and node has tier [data_cold]") + ); } } @@ -163,10 +207,7 @@ public void testTierNodesPresent() { assertFalse(DataTierAllocationDecider.tierNodesPresent("data_cold", nodes)); assertFalse(DataTierAllocationDecider.tierNodesPresent("data_content", nodes)); - nodes = DiscoveryNodes.builder() - .add(WARM_NODE) - .add(CONTENT_NODE) - .build(); + nodes = DiscoveryNodes.builder().add(WARM_NODE).add(CONTENT_NODE).build(); assertFalse(DataTierAllocationDecider.tierNodesPresent("data", nodes)); assertFalse(DataTierAllocationDecider.tierNodesPresent("data_hot", nodes)); @@ -174,9 +215,7 @@ public void testTierNodesPresent() { assertFalse(DataTierAllocationDecider.tierNodesPresent("data_cold", nodes)); assertTrue(DataTierAllocationDecider.tierNodesPresent("data_content", nodes)); - nodes = DiscoveryNodes.builder() - .add(DATA_NODE) - .build(); + nodes = DiscoveryNodes.builder().add(DATA_NODE).build(); assertTrue(DataTierAllocationDecider.tierNodesPresent("data", nodes)); assertTrue(DataTierAllocationDecider.tierNodesPresent("data_hot", nodes)); @@ -188,32 +227,40 @@ public void testTierNodesPresent() { public void testPreferredTierAvailable() { DiscoveryNodes nodes = DiscoveryNodes.builder().build(); - assertThat(DataTierAllocationDecider.preferredAvailableTier( - DataTier.parseTierList("data"), nodes), equalTo(Optional.empty())); - assertThat(DataTierAllocationDecider.preferredAvailableTier( - DataTier.parseTierList("data_hot,data_warm"), nodes), equalTo(Optional.empty())); - assertThat(DataTierAllocationDecider.preferredAvailableTier( - DataTier.parseTierList("data_warm,data_content"), nodes), equalTo(Optional.empty())); - assertThat(DataTierAllocationDecider.preferredAvailableTier( - DataTier.parseTierList("data_cold"), nodes), equalTo(Optional.empty())); - - nodes = DiscoveryNodes.builder() - .add(WARM_NODE) - .add(CONTENT_NODE) - .build(); - - assertThat(DataTierAllocationDecider.preferredAvailableTier( - DataTier.parseTierList("data"), nodes), equalTo(Optional.empty())); - assertThat(DataTierAllocationDecider.preferredAvailableTier( - DataTier.parseTierList("data_hot,data_warm"), nodes), equalTo(Optional.of("data_warm"))); - assertThat(DataTierAllocationDecider.preferredAvailableTier( - DataTier.parseTierList("data_warm,data_content"), nodes), equalTo(Optional.of("data_warm"))); - assertThat(DataTierAllocationDecider.preferredAvailableTier( - DataTier.parseTierList("data_content,data_warm"), nodes), equalTo(Optional.of("data_content"))); - assertThat(DataTierAllocationDecider.preferredAvailableTier( - DataTier.parseTierList("data_hot,data_content,data_warm"), nodes), equalTo(Optional.of("data_content"))); - assertThat(DataTierAllocationDecider.preferredAvailableTier( - DataTier.parseTierList("data_hot,data_cold,data_warm"), nodes), equalTo(Optional.of("data_warm"))); + assertThat(DataTierAllocationDecider.preferredAvailableTier(DataTier.parseTierList("data"), nodes), equalTo(Optional.empty())); + assertThat( + DataTierAllocationDecider.preferredAvailableTier(DataTier.parseTierList("data_hot,data_warm"), nodes), + equalTo(Optional.empty()) + ); + assertThat( + DataTierAllocationDecider.preferredAvailableTier(DataTier.parseTierList("data_warm,data_content"), nodes), + equalTo(Optional.empty()) + ); + assertThat(DataTierAllocationDecider.preferredAvailableTier(DataTier.parseTierList("data_cold"), nodes), equalTo(Optional.empty())); + + nodes = DiscoveryNodes.builder().add(WARM_NODE).add(CONTENT_NODE).build(); + + assertThat(DataTierAllocationDecider.preferredAvailableTier(DataTier.parseTierList("data"), nodes), equalTo(Optional.empty())); + assertThat( + DataTierAllocationDecider.preferredAvailableTier(DataTier.parseTierList("data_hot,data_warm"), nodes), + equalTo(Optional.of("data_warm")) + ); + assertThat( + DataTierAllocationDecider.preferredAvailableTier(DataTier.parseTierList("data_warm,data_content"), nodes), + equalTo(Optional.of("data_warm")) + ); + assertThat( + DataTierAllocationDecider.preferredAvailableTier(DataTier.parseTierList("data_content,data_warm"), nodes), + equalTo(Optional.of("data_content")) + ); + assertThat( + DataTierAllocationDecider.preferredAvailableTier(DataTier.parseTierList("data_hot,data_content,data_warm"), nodes), + equalTo(Optional.of("data_content")) + ); + assertThat( + DataTierAllocationDecider.preferredAvailableTier(DataTier.parseTierList("data_hot,data_cold,data_warm"), nodes), + equalTo(Optional.of("data_warm")) + ); } public void testFrozenIllegalForRegularIndices() { @@ -263,10 +310,11 @@ public void testNonFrozenIllegalForPartialSnapshot() { Settings settings = builder.build(); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> DataTier.TIER_PREFERENCE_SETTING.get(settings)); - assertThat(e.getMessage(), - containsString("only the [data_frozen] tier preference may be used for partial searchable snapshots")); + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> DataTier.TIER_PREFERENCE_SETTING.get(settings)); + assertThat( + e.getMessage(), + containsString("only the [data_frozen] tier preference may be used for partial searchable snapshots") + ); } { @@ -276,10 +324,11 @@ public void testNonFrozenIllegalForPartialSnapshot() { Settings settings = builder.build(); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> DataTier.TIER_PREFERENCE_SETTING.get(settings)); - assertThat(e.getMessage(), - containsString("only the [data_frozen] tier preference may be used for partial searchable snapshots")); + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> DataTier.TIER_PREFERENCE_SETTING.get(settings)); + assertThat( + e.getMessage(), + containsString("only the [data_frozen] tier preference may be used for partial searchable snapshots") + ); } { @@ -289,10 +338,11 @@ public void testNonFrozenIllegalForPartialSnapshot() { Settings settings = builder.build(); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> DataTier.TIER_PREFERENCE_SETTING.get(settings)); - assertThat(e.getMessage(), - containsString("only the [data_frozen] tier preference may be used for partial searchable snapshots")); + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> DataTier.TIER_PREFERENCE_SETTING.get(settings)); + assertThat( + e.getMessage(), + containsString("only the [data_frozen] tier preference may be used for partial searchable snapshots") + ); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/cluster/routing/allocation/mapper/DataTierFieldTypeTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/cluster/routing/allocation/mapper/DataTierFieldTypeTests.java index 6a0a1efa23f45..0ca05f5269257 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/cluster/routing/allocation/mapper/DataTierFieldTypeTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/cluster/routing/allocation/mapper/DataTierFieldTypeTests.java @@ -137,15 +137,31 @@ private SearchExecutionContext createContext() { private SearchExecutionContext createContextWithoutSetting() { IndexMetadata indexMetadata = IndexMetadata.builder("index") - .settings(Settings.builder() - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .build()) + .settings(Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).build()) .numberOfShards(1) .numberOfReplicas(0) .build(); IndexSettings indexSettings = new IndexSettings(indexMetadata, Settings.EMPTY); - return new SearchExecutionContext(0, 0, indexSettings, null, null, null, null, null, null, - xContentRegistry(), writableRegistry(), null, null, System::currentTimeMillis, null, - value -> true, () -> true, null, emptyMap()); + return new SearchExecutionContext( + 0, + 0, + indexSettings, + null, + null, + null, + null, + null, + null, + xContentRegistry(), + writableRegistry(), + null, + null, + System::currentTimeMillis, + null, + value -> true, + () -> true, + null, + emptyMap() + ); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ClientHelperTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ClientHelperTests.java index 5d1b88cc906ce..50e465a17dff8 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ClientHelperTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ClientHelperTests.java @@ -122,7 +122,6 @@ public void testClientWithOrigin() throws Exception { latch.countDown(); }, e -> fail(e.getMessage())); - doAnswer(invocationOnMock -> { assertEquals(origin, threadContext.getTransient(ClientHelper.ACTION_ORIGIN_TRANSIENT_NAME)); assertNull(threadContext.getHeader(headerName)); @@ -237,8 +236,18 @@ public void testExecuteWithHeadersNoHeaders() { when(client.threadPool()).thenReturn(threadPool); PlainActionFuture searchFuture = PlainActionFuture.newFuture(); - searchFuture.onResponse(new SearchResponse(InternalSearchResponse.empty(), null, 0, 0, 0, 0L, ShardSearchFailure.EMPTY_ARRAY, - SearchResponse.Clusters.EMPTY)); + searchFuture.onResponse( + new SearchResponse( + InternalSearchResponse.empty(), + null, + 0, + 0, + 0, + 0L, + ShardSearchFailure.EMPTY_ARRAY, + SearchResponse.Clusters.EMPTY + ) + ); when(client.search(any())).thenReturn(searchFuture); assertExecutionWithOrigin(Collections.emptyMap(), client); } @@ -251,11 +260,23 @@ public void testExecuteWithHeaders() { when(client.threadPool()).thenReturn(threadPool); PlainActionFuture searchFuture = PlainActionFuture.newFuture(); - searchFuture.onResponse(new SearchResponse(InternalSearchResponse.empty(), null, 0, 0, 0, 0L, ShardSearchFailure.EMPTY_ARRAY, - SearchResponse.Clusters.EMPTY)); + searchFuture.onResponse( + new SearchResponse( + InternalSearchResponse.empty(), + null, + 0, + 0, + 0, + 0L, + ShardSearchFailure.EMPTY_ARRAY, + SearchResponse.Clusters.EMPTY + ) + ); when(client.search(any())).thenReturn(searchFuture); - Map headers = MapBuilder. newMapBuilder().put(AuthenticationField.AUTHENTICATION_KEY, "anything") - .put(AuthenticationServiceField.RUN_AS_USER_HEADER, "anything").map(); + Map headers = MapBuilder.newMapBuilder() + .put(AuthenticationField.AUTHENTICATION_KEY, "anything") + .put(AuthenticationServiceField.RUN_AS_USER_HEADER, "anything") + .map(); assertRunAsExecution(headers, h -> { assertThat(h.keySet(), hasSize(2)); @@ -272,10 +293,20 @@ public void testExecuteWithHeadersNoSecurityHeaders() { when(client.threadPool()).thenReturn(threadPool); PlainActionFuture searchFuture = PlainActionFuture.newFuture(); - searchFuture.onResponse(new SearchResponse(InternalSearchResponse.empty(), null, 0, 0, 0, 0L, ShardSearchFailure.EMPTY_ARRAY, - SearchResponse.Clusters.EMPTY)); + searchFuture.onResponse( + new SearchResponse( + InternalSearchResponse.empty(), + null, + 0, + 0, + 0, + 0L, + ShardSearchFailure.EMPTY_ARRAY, + SearchResponse.Clusters.EMPTY + ) + ); when(client.search(any())).thenReturn(searchFuture); - Map unrelatedHeaders = MapBuilder. newMapBuilder().put(randomAlphaOfLength(10), "anything").map(); + Map unrelatedHeaders = MapBuilder.newMapBuilder().put(randomAlphaOfLength(10), "anything").map(); assertExecutionWithOrigin(unrelatedHeaders, client); } @@ -325,7 +356,8 @@ public void testFilterSecurityHeaders() { { // Singleton map with a security-related header assertThat( ClientHelper.filterSecurityHeaders(Collections.singletonMap(AuthenticationServiceField.RUN_AS_USER_HEADER, "value")), - hasEntry(AuthenticationServiceField.RUN_AS_USER_HEADER, "value")); + hasEntry(AuthenticationServiceField.RUN_AS_USER_HEADER, "value") + ); } { // Map with 3 headers out of which only 1 is security-related Map headers = new HashMap<>(); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/DataTiersFeatureSetUsageTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/DataTiersFeatureSetUsageTests.java index 1a2cc7c3f4980..830d4ef9e0829 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/DataTiersFeatureSetUsageTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/DataTiersFeatureSetUsageTests.java @@ -35,16 +35,22 @@ protected DataTiersFeatureSetUsage createTestInstance() { public static DataTiersFeatureSetUsage randomUsage() { List tiers = randomSubsetOf(DataTier.ALL_DATA_TIERS); Map stats = new HashMap<>(); - tiers.forEach(tier -> - stats.put(tier, new DataTiersFeatureSetUsage.TierSpecificStats(randomIntBetween(1, 10), - randomIntBetween(5, 100), - randomIntBetween(0, 1000), - randomIntBetween(0, 1000), - randomNonNegativeLong(), - randomNonNegativeLong(), - randomNonNegativeLong(), - randomNonNegativeLong(), - randomNonNegativeLong()))); + tiers.forEach( + tier -> stats.put( + tier, + new DataTiersFeatureSetUsage.TierSpecificStats( + randomIntBetween(1, 10), + randomIntBetween(5, 100), + randomIntBetween(0, 1000), + randomIntBetween(0, 1000), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong() + ) + ) + ); return new DataTiersFeatureSetUsage(stats); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/DataTiersUsageTransportActionTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/DataTiersUsageTransportActionTests.java index a40bbb2d9ca0f..91d55dd939fcd 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/DataTiersUsageTransportActionTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/DataTiersUsageTransportActionTests.java @@ -78,12 +78,12 @@ public void testCalculateMAD() { } public void testTierIndices() { - IndexMetadata hotIndex1 = indexMetadata("hot-1", 1, 0, DataTier.DATA_HOT); - IndexMetadata hotIndex2 = indexMetadata("hot-2", 1, 0, DataTier.DATA_HOT); - IndexMetadata warmIndex1 = indexMetadata("warm-1", 1, 0, DataTier.DATA_WARM); - IndexMetadata coldIndex1 = indexMetadata("cold-1", 1, 0, DataTier.DATA_COLD); - IndexMetadata coldIndex2 = indexMetadata("cold-2", 1, 0, DataTier.DATA_COLD, DataTier.DATA_WARM); // Prefers cold over warm - IndexMetadata nonTiered = indexMetadata("non-tier", 1, 0); // No tier + IndexMetadata hotIndex1 = indexMetadata("hot-1", 1, 0, DataTier.DATA_HOT); + IndexMetadata hotIndex2 = indexMetadata("hot-2", 1, 0, DataTier.DATA_HOT); + IndexMetadata warmIndex1 = indexMetadata("warm-1", 1, 0, DataTier.DATA_WARM); + IndexMetadata coldIndex1 = indexMetadata("cold-1", 1, 0, DataTier.DATA_COLD); + IndexMetadata coldIndex2 = indexMetadata("cold-2", 1, 0, DataTier.DATA_COLD, DataTier.DATA_WARM); // Prefers cold over warm + IndexMetadata nonTiered = indexMetadata("non-tier", 1, 0); // No tier ImmutableOpenMap.Builder indicesBuilder = ImmutableOpenMap.builder(); indicesBuilder.put("hot-1", hotIndex1); @@ -142,8 +142,11 @@ public void testCalculateStatsNoTiers() { // Calculate usage Map indexByTier = DataTiersUsageTransportAction.tierIndices(clusterState.metadata().indices()); - Map tierSpecificStats = - DataTiersUsageTransportAction.calculateStats(nodeStatsList, indexByTier, clusterState.getRoutingNodes()); + Map tierSpecificStats = DataTiersUsageTransportAction.calculateStats( + nodeStatsList, + indexByTier, + clusterState.getRoutingNodes() + ); // Verify - No results when no tiers present assertThat(tierSpecificStats.size(), is(0)); @@ -195,8 +198,11 @@ public void testCalculateStatsTieredNodesOnly() { // Calculate usage Map indexByTier = DataTiersUsageTransportAction.tierIndices(clusterState.metadata().indices()); - Map tierSpecificStats = - DataTiersUsageTransportAction.calculateStats(nodeStatsList, indexByTier, clusterState.getRoutingNodes()); + Map tierSpecificStats = DataTiersUsageTransportAction.calculateStats( + nodeStatsList, + indexByTier, + clusterState.getRoutingNodes() + ); // Verify - Results are present but they lack index numbers because none are tiered assertThat(tierSpecificStats.size(), is(4)); @@ -330,8 +336,11 @@ public void testCalculateStatsTieredIndicesOnly() { // Calculate usage Map indexByTier = DataTiersUsageTransportAction.tierIndices(clusterState.metadata().indices()); - Map tierSpecificStats = - DataTiersUsageTransportAction.calculateStats(nodeStatsList, indexByTier, clusterState.getRoutingNodes()); + Map tierSpecificStats = DataTiersUsageTransportAction.calculateStats( + nodeStatsList, + indexByTier, + clusterState.getRoutingNodes() + ); // Verify - Index stats exist for the tiers, but no tiered nodes are found assertThat(tierSpecificStats.size(), is(3)); @@ -341,10 +350,10 @@ public void testCalculateStatsTieredIndicesOnly() { assertThat(hotStats.nodeCount, is(0)); assertThat(hotStats.indexCount, is(1)); assertThat(hotStats.totalShardCount, is(6)); - assertThat(hotStats.docCount, is(6*docCount)); - assertThat(hotStats.totalByteCount, is(6*byteSize)); + assertThat(hotStats.docCount, is(6 * docCount)); + assertThat(hotStats.totalByteCount, is(6 * byteSize)); assertThat(hotStats.primaryShardCount, is(3)); - assertThat(hotStats.primaryByteCount, is(3*byteSize)); + assertThat(hotStats.primaryByteCount, is(3 * byteSize)); assertThat(hotStats.primaryByteCountMedian, is(byteSize)); // All same size assertThat(hotStats.primaryShardBytesMAD, is(0L)); // All same size @@ -353,10 +362,10 @@ public void testCalculateStatsTieredIndicesOnly() { assertThat(warmStats.nodeCount, is(0)); assertThat(warmStats.indexCount, is(2)); assertThat(warmStats.totalShardCount, is(4)); - assertThat(warmStats.docCount, is(4*docCount)); - assertThat(warmStats.totalByteCount, is(4*byteSize)); + assertThat(warmStats.docCount, is(4 * docCount)); + assertThat(warmStats.totalByteCount, is(4 * byteSize)); assertThat(warmStats.primaryShardCount, is(2)); - assertThat(warmStats.primaryByteCount, is(2*byteSize)); + assertThat(warmStats.primaryByteCount, is(2 * byteSize)); assertThat(warmStats.primaryByteCountMedian, is(byteSize)); // All same size assertThat(warmStats.primaryShardBytesMAD, is(0L)); // All same size @@ -365,10 +374,10 @@ public void testCalculateStatsTieredIndicesOnly() { assertThat(coldStats.nodeCount, is(0)); assertThat(coldStats.indexCount, is(3)); assertThat(coldStats.totalShardCount, is(3)); - assertThat(coldStats.docCount, is(3*docCount)); - assertThat(coldStats.totalByteCount, is(3*byteSize)); + assertThat(coldStats.docCount, is(3 * docCount)); + assertThat(coldStats.totalByteCount, is(3 * byteSize)); assertThat(coldStats.primaryShardCount, is(3)); - assertThat(coldStats.primaryByteCount, is(3*byteSize)); + assertThat(coldStats.primaryByteCount, is(3 * byteSize)); assertThat(coldStats.primaryByteCountMedian, is(byteSize)); // All same size assertThat(coldStats.primaryShardBytesMAD, is(0L)); // All same size } @@ -465,8 +474,11 @@ public void testCalculateStatsReasonableCase() { // Calculate usage Map indexByTier = DataTiersUsageTransportAction.tierIndices(clusterState.metadata().indices()); - Map tierSpecificStats = - DataTiersUsageTransportAction.calculateStats(nodeStatsList, indexByTier, clusterState.getRoutingNodes()); + Map tierSpecificStats = DataTiersUsageTransportAction.calculateStats( + nodeStatsList, + indexByTier, + clusterState.getRoutingNodes() + ); // Verify - Node and Index stats are both collected assertThat(tierSpecificStats.size(), is(3)); @@ -476,10 +488,10 @@ public void testCalculateStatsReasonableCase() { assertThat(hotStats.nodeCount, is(3)); assertThat(hotStats.indexCount, is(1)); assertThat(hotStats.totalShardCount, is(6)); - assertThat(hotStats.docCount, is(6*docCount)); - assertThat(hotStats.totalByteCount, is(6*byteSize)); + assertThat(hotStats.docCount, is(6 * docCount)); + assertThat(hotStats.totalByteCount, is(6 * byteSize)); assertThat(hotStats.primaryShardCount, is(3)); - assertThat(hotStats.primaryByteCount, is(3*byteSize)); + assertThat(hotStats.primaryByteCount, is(3 * byteSize)); assertThat(hotStats.primaryByteCountMedian, is(byteSize)); // All same size assertThat(hotStats.primaryShardBytesMAD, is(0L)); // All same size @@ -488,10 +500,10 @@ public void testCalculateStatsReasonableCase() { assertThat(warmStats.nodeCount, is(5)); assertThat(warmStats.indexCount, is(2)); assertThat(warmStats.totalShardCount, is(4)); - assertThat(warmStats.docCount, is(4*docCount)); - assertThat(warmStats.totalByteCount, is(4*byteSize)); + assertThat(warmStats.docCount, is(4 * docCount)); + assertThat(warmStats.totalByteCount, is(4 * byteSize)); assertThat(warmStats.primaryShardCount, is(2)); - assertThat(warmStats.primaryByteCount, is(2*byteSize)); + assertThat(warmStats.primaryByteCount, is(2 * byteSize)); assertThat(warmStats.primaryByteCountMedian, is(byteSize)); // All same size assertThat(warmStats.primaryShardBytesMAD, is(0L)); // All same size @@ -500,10 +512,10 @@ public void testCalculateStatsReasonableCase() { assertThat(coldStats.nodeCount, is(1)); assertThat(coldStats.indexCount, is(3)); assertThat(coldStats.totalShardCount, is(3)); - assertThat(coldStats.docCount, is(3*docCount)); - assertThat(coldStats.totalByteCount, is(3*byteSize)); + assertThat(coldStats.docCount, is(3 * docCount)); + assertThat(coldStats.totalByteCount, is(3 * byteSize)); assertThat(coldStats.primaryShardCount, is(3)); - assertThat(coldStats.primaryByteCount, is(3*byteSize)); + assertThat(coldStats.primaryByteCount, is(3 * byteSize)); assertThat(coldStats.primaryByteCountMedian, is(byteSize)); // All same size assertThat(coldStats.primaryShardBytesMAD, is(0L)); // All same size } @@ -566,8 +578,11 @@ public void testCalculateStatsMixedTiers() { // Calculate usage Map indexByTier = DataTiersUsageTransportAction.tierIndices(clusterState.metadata().indices()); - Map tierSpecificStats = - DataTiersUsageTransportAction.calculateStats(nodeStatsList, indexByTier, clusterState.getRoutingNodes()); + Map tierSpecificStats = DataTiersUsageTransportAction.calculateStats( + nodeStatsList, + indexByTier, + clusterState.getRoutingNodes() + ); // Verify - Index stats are separated by their preferred tier, instead of counted // toward multiple tiers based on their current routing. Nodes are counted for each tier they are in. @@ -578,10 +593,10 @@ public void testCalculateStatsMixedTiers() { assertThat(hotStats.nodeCount, is(3)); assertThat(hotStats.indexCount, is(1)); assertThat(hotStats.totalShardCount, is(6)); - assertThat(hotStats.docCount, is(6*docCount)); - assertThat(hotStats.totalByteCount, is(6*byteSize)); + assertThat(hotStats.docCount, is(6 * docCount)); + assertThat(hotStats.totalByteCount, is(6 * byteSize)); assertThat(hotStats.primaryShardCount, is(3)); - assertThat(hotStats.primaryByteCount, is(3*byteSize)); + assertThat(hotStats.primaryByteCount, is(3 * byteSize)); assertThat(hotStats.primaryByteCountMedian, is(byteSize)); // All same size assertThat(hotStats.primaryShardBytesMAD, is(0L)); // All same size @@ -590,10 +605,10 @@ public void testCalculateStatsMixedTiers() { assertThat(warmStats.nodeCount, is(3)); assertThat(warmStats.indexCount, is(2)); assertThat(warmStats.totalShardCount, is(4)); - assertThat(warmStats.docCount, is(4*docCount)); - assertThat(warmStats.totalByteCount, is(4*byteSize)); + assertThat(warmStats.docCount, is(4 * docCount)); + assertThat(warmStats.totalByteCount, is(4 * byteSize)); assertThat(warmStats.primaryShardCount, is(2)); - assertThat(warmStats.primaryByteCount, is(2*byteSize)); + assertThat(warmStats.primaryByteCount, is(2 * byteSize)); assertThat(warmStats.primaryByteCountMedian, is(byteSize)); // All same size assertThat(warmStats.primaryShardBytesMAD, is(0L)); // All same size } @@ -649,8 +664,11 @@ public void testCalculateStatsStuckInWrongTier() { // Calculate usage Map indexByTier = DataTiersUsageTransportAction.tierIndices(clusterState.metadata().indices()); - Map tierSpecificStats = - DataTiersUsageTransportAction.calculateStats(nodeStatsList, indexByTier, clusterState.getRoutingNodes()); + Map tierSpecificStats = DataTiersUsageTransportAction.calculateStats( + nodeStatsList, + indexByTier, + clusterState.getRoutingNodes() + ); // Verify - Warm indices are still calculated separately from Hot ones, despite Warm nodes missing assertThat(tierSpecificStats.size(), is(2)); @@ -660,10 +678,10 @@ public void testCalculateStatsStuckInWrongTier() { assertThat(hotStats.nodeCount, is(3)); assertThat(hotStats.indexCount, is(1)); assertThat(hotStats.totalShardCount, is(6)); - assertThat(hotStats.docCount, is(6*docCount)); - assertThat(hotStats.totalByteCount, is(6*byteSize)); + assertThat(hotStats.docCount, is(6 * docCount)); + assertThat(hotStats.totalByteCount, is(6 * byteSize)); assertThat(hotStats.primaryShardCount, is(3)); - assertThat(hotStats.primaryByteCount, is(3*byteSize)); + assertThat(hotStats.primaryByteCount, is(3 * byteSize)); assertThat(hotStats.primaryByteCountMedian, is(byteSize)); // All same size assertThat(hotStats.primaryShardBytesMAD, is(0L)); // All same size @@ -672,8 +690,8 @@ public void testCalculateStatsStuckInWrongTier() { assertThat(warmStats.nodeCount, is(0)); assertThat(warmStats.indexCount, is(1)); assertThat(warmStats.totalShardCount, is(2)); - assertThat(warmStats.docCount, is(2*docCount)); - assertThat(warmStats.totalByteCount, is(2*byteSize)); + assertThat(warmStats.docCount, is(2 * docCount)); + assertThat(warmStats.totalByteCount, is(2 * byteSize)); assertThat(warmStats.primaryShardCount, is(1)); assertThat(warmStats.primaryByteCount, is(byteSize)); assertThat(warmStats.primaryByteCountMedian, is(byteSize)); // All same size @@ -681,8 +699,13 @@ public void testCalculateStatsStuckInWrongTier() { } private static DiscoveryNode newNode(int nodeId, DiscoveryNodeRole... roles) { - return new DiscoveryNode("node_" + nodeId, ESTestCase.buildNewFakeTransportAddress(), Collections.emptyMap(), Set.of(roles), - Version.CURRENT); + return new DiscoveryNode( + "node_" + nodeId, + ESTestCase.buildNewFakeTransportAddress(), + Collections.emptyMap(), + Set.of(roles), + Version.CURRENT + ); } private static IndexMetadata indexMetadata(String indexName, int numberOfShards, int numberOfReplicas, String... dataTierPrefs) { @@ -702,14 +725,15 @@ private static IndexMetadata indexMetadata(String indexName, int numberOfShards, settingsBuilder.put(DataTier.TIER_PREFERENCE, dataTierPrefs[0]); } - return IndexMetadata.builder(indexName) - .settings(settingsBuilder.build()) - .timestampRange(IndexLongFieldRange.UNKNOWN) - .build(); + return IndexMetadata.builder(indexName).settings(settingsBuilder.build()).timestampRange(IndexLongFieldRange.UNKNOWN).build(); } - private static void routeTestShardToNodes(IndexMetadata index, int shard, IndexRoutingTable.Builder indexRoutingTableBuilder, - DiscoveryNode... nodes) { + private static void routeTestShardToNodes( + IndexMetadata index, + int shard, + IndexRoutingTable.Builder indexRoutingTableBuilder, + DiscoveryNode... nodes + ) { ShardId shardId = new ShardId(index.getIndex(), shard); IndexShardRoutingTable.Builder indexShardRoutingBuilder = new IndexShardRoutingTable.Builder(shardId); boolean primary = true; @@ -732,7 +756,7 @@ private List buildNodeStats(ClusterState clusterState, long bytesPerS for (ShardRouting shardRouting : routingNode) { ShardId shardId = shardRouting.shardId(); ShardStats shardStat = shardStat(bytesPerShard, docsPerShard, shardRouting); - IndexShardStats shardStats = new IndexShardStats(shardId, new ShardStats[]{shardStat}); + IndexShardStats shardStats = new IndexShardStats(shardId, new ShardStats[] { shardStat }); indexStats.computeIfAbsent(shardId.getIndex(), k -> new ArrayList<>()).add(shardStats); } NodeIndicesStats nodeIndexStats = new NodeIndicesStats(new CommonStats(), indexStats); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/LocalStateCompositeXPackPlugin.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/LocalStateCompositeXPackPlugin.java index 24f6a9d39e5a3..b3cc04c8d9d01 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/LocalStateCompositeXPackPlugin.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/LocalStateCompositeXPackPlugin.java @@ -113,9 +113,22 @@ import static java.util.stream.Collectors.toList; -public class LocalStateCompositeXPackPlugin extends XPackPlugin implements ScriptPlugin, ActionPlugin, IngestPlugin, NetworkPlugin, - ClusterPlugin, DiscoveryPlugin, MapperPlugin, AnalysisPlugin, PersistentTaskPlugin, EnginePlugin, IndexStorePlugin, - SystemIndexPlugin, SearchPlugin, ShutdownAwarePlugin { +public class LocalStateCompositeXPackPlugin extends XPackPlugin + implements + ScriptPlugin, + ActionPlugin, + IngestPlugin, + NetworkPlugin, + ClusterPlugin, + DiscoveryPlugin, + MapperPlugin, + AnalysisPlugin, + PersistentTaskPlugin, + EnginePlugin, + IndexStorePlugin, + SystemIndexPlugin, + SearchPlugin, + ShutdownAwarePlugin { private XPackLicenseState licenseState; private SSLService sslService; @@ -127,7 +140,7 @@ public LocalStateCompositeXPackPlugin(final Settings settings, final Path config super(settings, configPath); } - //Get around all the setOnce nonsense in the plugin + // Get around all the setOnce nonsense in the plugin @Override protected SSLService getSslService() { return sslService; @@ -169,21 +182,54 @@ protected void setEpochMillisSupplier(LongSupplier epochMillisSupplier) { } @Override - public Collection createComponents(Client client, ClusterService clusterService, ThreadPool threadPool, - ResourceWatcherService resourceWatcherService, ScriptService scriptService, - NamedXContentRegistry xContentRegistry, Environment environment, - NodeEnvironment nodeEnvironment, NamedWriteableRegistry namedWriteableRegistry, - IndexNameExpressionResolver expressionResolver, - Supplier repositoriesServiceSupplier) { + public Collection createComponents( + Client client, + ClusterService clusterService, + ThreadPool threadPool, + ResourceWatcherService resourceWatcherService, + ScriptService scriptService, + NamedXContentRegistry xContentRegistry, + Environment environment, + NodeEnvironment nodeEnvironment, + NamedWriteableRegistry namedWriteableRegistry, + IndexNameExpressionResolver expressionResolver, + Supplier repositoriesServiceSupplier + ) { List components = new ArrayList<>(); - components.addAll(super.createComponents(client, clusterService, threadPool, resourceWatcherService, scriptService, - xContentRegistry, environment, nodeEnvironment, namedWriteableRegistry, expressionResolver, repositoriesServiceSupplier)); - - filterPlugins(Plugin.class).stream().forEach(p -> - components.addAll(p.createComponents(client, clusterService, threadPool, resourceWatcherService, scriptService, - xContentRegistry, environment, nodeEnvironment, namedWriteableRegistry, expressionResolver, - repositoriesServiceSupplier)) + components.addAll( + super.createComponents( + client, + clusterService, + threadPool, + resourceWatcherService, + scriptService, + xContentRegistry, + environment, + nodeEnvironment, + namedWriteableRegistry, + expressionResolver, + repositoriesServiceSupplier + ) ); + + filterPlugins(Plugin.class).stream() + .forEach( + p -> components.addAll( + p.createComponents( + client, + clusterService, + threadPool, + resourceWatcherService, + scriptService, + xContentRegistry, + environment, + nodeEnvironment, + namedWriteableRegistry, + expressionResolver, + repositoriesServiceSupplier + ) + ) + ); return components; } @@ -200,9 +246,7 @@ public List> getSettings() { ArrayList> settings = new ArrayList<>(); settings.addAll(super.getSettings()); - filterPlugins(Plugin.class).stream().forEach(p -> - settings.addAll(p.getSettings()) - ); + filterPlugins(Plugin.class).stream().forEach(p -> settings.addAll(p.getSettings())); return settings; } @@ -210,9 +254,7 @@ public List> getSettings() { public List getSettingsFilter() { List filters = new ArrayList<>(); filters.addAll(super.getSettingsFilter()); - filterPlugins(Plugin.class).stream().forEach(p -> - filters.addAll(p.getSettingsFilter()) - ); + filterPlugins(Plugin.class).stream().forEach(p -> filters.addAll(p.getSettingsFilter())); return filters; } @@ -220,9 +262,7 @@ public List getSettingsFilter() { public List> getActions() { List> actions = new ArrayList<>(); actions.addAll(super.getActions()); - filterPlugins(ActionPlugin.class).stream().forEach(p -> - actions.addAll(p.getActions()) - ); + filterPlugins(ActionPlugin.class).stream().forEach(p -> actions.addAll(p.getActions())); return actions; } @@ -230,24 +270,46 @@ public List getSettingsFilter() { public List getActionFilters() { List filters = new ArrayList<>(); filters.addAll(super.getActionFilters()); - filterPlugins(ActionPlugin.class).stream().forEach(p -> - filters.addAll(p.getActionFilters()) - ); + filterPlugins(ActionPlugin.class).stream().forEach(p -> filters.addAll(p.getActionFilters())); return filters; } @Override - public List getRestHandlers(Settings settings, RestController restController, ClusterSettings clusterSettings, - IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter, - IndexNameExpressionResolver indexNameExpressionResolver, - Supplier nodesInCluster) { + public List getRestHandlers( + Settings settings, + RestController restController, + ClusterSettings clusterSettings, + IndexScopedSettings indexScopedSettings, + SettingsFilter settingsFilter, + IndexNameExpressionResolver indexNameExpressionResolver, + Supplier nodesInCluster + ) { List handlers = new ArrayList<>(); - handlers.addAll(super.getRestHandlers(settings, restController, clusterSettings, indexScopedSettings, settingsFilter, - indexNameExpressionResolver, nodesInCluster)); - filterPlugins(ActionPlugin.class).stream().forEach(p -> - handlers.addAll(p.getRestHandlers(settings, restController, clusterSettings, indexScopedSettings, - settingsFilter, indexNameExpressionResolver, nodesInCluster)) + handlers.addAll( + super.getRestHandlers( + settings, + restController, + clusterSettings, + indexScopedSettings, + settingsFilter, + indexNameExpressionResolver, + nodesInCluster + ) ); + filterPlugins(ActionPlugin.class).stream() + .forEach( + p -> handlers.addAll( + p.getRestHandlers( + settings, + restController, + clusterSettings, + indexScopedSettings, + settingsFilter, + indexNameExpressionResolver, + nodesInCluster + ) + ) + ); return handlers; } @@ -277,13 +339,10 @@ public List getNamedXContent() { public Settings additionalSettings() { Settings.Builder builder = Settings.builder(); builder.put(super.additionalSettings()); - filterPlugins(Plugin.class).stream().forEach(p -> - builder.put(p.additionalSettings()) - ); + filterPlugins(Plugin.class).stream().forEach(p -> builder.put(p.additionalSettings())); return builder.build(); } - @Override public List> getContexts() { List> contexts = new ArrayList<>(); @@ -301,36 +360,63 @@ public Map getProcessors(Processor.Parameters paramet @Override public List getTransportInterceptors(NamedWriteableRegistry namedWriteableRegistry, ThreadContext threadContext) { List interceptors = new ArrayList<>(); - filterPlugins(NetworkPlugin.class).stream().forEach(p -> interceptors.addAll(p.getTransportInterceptors(namedWriteableRegistry, - threadContext))); + filterPlugins(NetworkPlugin.class).stream() + .forEach(p -> interceptors.addAll(p.getTransportInterceptors(namedWriteableRegistry, threadContext))); return interceptors; } @Override - public Map> getTransports(Settings settings, ThreadPool threadPool, PageCacheRecycler pageCacheRecycler, - CircuitBreakerService circuitBreakerService, - NamedWriteableRegistry namedWriteableRegistry, NetworkService networkService) { + public Map> getTransports( + Settings settings, + ThreadPool threadPool, + PageCacheRecycler pageCacheRecycler, + CircuitBreakerService circuitBreakerService, + NamedWriteableRegistry namedWriteableRegistry, + NetworkService networkService + ) { Map> transports = new HashMap<>(); - transports.putAll(super.getTransports(settings, threadPool, pageCacheRecycler, circuitBreakerService, namedWriteableRegistry, - networkService)); - filterPlugins(NetworkPlugin.class).stream().forEach(p -> transports.putAll(p.getTransports(settings, threadPool, - pageCacheRecycler, circuitBreakerService, namedWriteableRegistry, networkService))); + transports.putAll( + super.getTransports(settings, threadPool, pageCacheRecycler, circuitBreakerService, namedWriteableRegistry, networkService) + ); + filterPlugins(NetworkPlugin.class).stream() + .forEach( + p -> transports.putAll( + p.getTransports(settings, threadPool, pageCacheRecycler, circuitBreakerService, namedWriteableRegistry, networkService) + ) + ); return transports; - } @Override - public Map> getHttpTransports(Settings settings, ThreadPool threadPool, BigArrays bigArrays, - PageCacheRecycler pageCacheRecycler, - CircuitBreakerService circuitBreakerService, - NamedXContentRegistry xContentRegistry, - NetworkService networkService, - HttpServerTransport.Dispatcher dispatcher, - ClusterSettings clusterSettings) { + public Map> getHttpTransports( + Settings settings, + ThreadPool threadPool, + BigArrays bigArrays, + PageCacheRecycler pageCacheRecycler, + CircuitBreakerService circuitBreakerService, + NamedXContentRegistry xContentRegistry, + NetworkService networkService, + HttpServerTransport.Dispatcher dispatcher, + ClusterSettings clusterSettings + ) { Map> transports = new HashMap<>(); - filterPlugins(NetworkPlugin.class).stream().forEach(p -> transports.putAll(p.getHttpTransports(settings, threadPool, bigArrays, - pageCacheRecycler, circuitBreakerService, xContentRegistry, networkService, dispatcher, clusterSettings))); + filterPlugins(NetworkPlugin.class).stream() + .forEach( + p -> transports.putAll( + p.getHttpTransports( + settings, + threadPool, + bigArrays, + pageCacheRecycler, + circuitBreakerService, + xContentRegistry, + networkService, + dispatcher, + clusterSettings + ) + ) + ); return transports; } @@ -344,9 +430,11 @@ public List getBootstrapChecks() { @Override public UnaryOperator getRestHandlerWrapper(ThreadContext threadContext) { - // There can be only one. - List> items = filterPlugins(ActionPlugin.class).stream().map(p -> - p.getRestHandlerWrapper(threadContext)).filter(Objects::nonNull).collect(Collectors.toList()); + // There can be only one. + List> items = filterPlugins(ActionPlugin.class).stream() + .map(p -> p.getRestHandlerWrapper(threadContext)) + .filter(Objects::nonNull) + .collect(Collectors.toList()); if (items.size() > 1) { throw new UnsupportedOperationException("Only the security ActionPlugin should override this"); @@ -367,7 +455,7 @@ public List> getExecutorBuilders(final Settings settings) { @Override public UnaryOperator> getIndexTemplateMetadataUpgrader() { return templates -> { - for(Plugin p: plugins) { + for (Plugin p : plugins) { templates = p.getIndexTemplateMetadataUpgrader().apply(templates); } return templates; @@ -405,8 +493,9 @@ public void onIndexModule(IndexModule indexModule) { @Override public Function> getFieldFilter() { - List>> items = filterPlugins(MapperPlugin.class).stream().map(p -> - p.getFieldFilter()).collect(Collectors.toList()); + List>> items = filterPlugins(MapperPlugin.class).stream() + .map(p -> p.getFieldFilter()) + .collect(Collectors.toList()); if (items.size() > 1) { throw new UnsupportedOperationException("Only the security MapperPlugin should override this"); } else if (items.size() == 1) { @@ -420,8 +509,9 @@ public Function> getFieldFilter() { @Override public BiConsumer getJoinValidator() { // There can be only one. - List> items = filterPlugins(DiscoveryPlugin.class).stream().map(p -> - p.getJoinValidator()).collect(Collectors.toList()); + List> items = filterPlugins(DiscoveryPlugin.class).stream() + .map(p -> p.getJoinValidator()) + .collect(Collectors.toList()); if (items.size() > 1) { throw new UnsupportedOperationException("Only the security DiscoveryPlugin should override this"); } else if (items.size() == 1) { @@ -432,35 +522,49 @@ public BiConsumer getJoinValidator() { } @Override - public List> getPersistentTasksExecutor(ClusterService clusterService, - ThreadPool threadPool, - Client client, - SettingsModule settingsModule, - IndexNameExpressionResolver expressionResolver) { + public List> getPersistentTasksExecutor( + ClusterService clusterService, + ThreadPool threadPool, + Client client, + SettingsModule settingsModule, + IndexNameExpressionResolver expressionResolver + ) { return filterPlugins(PersistentTaskPlugin.class).stream() - .map(p -> p.getPersistentTasksExecutor(clusterService, threadPool, client, settingsModule, expressionResolver)) - .flatMap(List::stream) - .collect(toList()); + .map(p -> p.getPersistentTasksExecutor(clusterService, threadPool, client, settingsModule, expressionResolver)) + .flatMap(List::stream) + .collect(toList()); } @Override - public Map getRepositories(Environment env, NamedXContentRegistry namedXContentRegistry, - ClusterService clusterService, BigArrays bigArrays, - RecoverySettings recoverySettings) { - HashMap repositories = - new HashMap<>(super.getRepositories(env, namedXContentRegistry, clusterService, bigArrays, recoverySettings)); + public Map getRepositories( + Environment env, + NamedXContentRegistry namedXContentRegistry, + ClusterService clusterService, + BigArrays bigArrays, + RecoverySettings recoverySettings + ) { + HashMap repositories = new HashMap<>( + super.getRepositories(env, namedXContentRegistry, clusterService, bigArrays, recoverySettings) + ); filterPlugins(RepositoryPlugin.class).forEach( - r -> repositories.putAll(r.getRepositories(env, namedXContentRegistry, clusterService, bigArrays, recoverySettings))); + r -> repositories.putAll(r.getRepositories(env, namedXContentRegistry, clusterService, bigArrays, recoverySettings)) + ); return repositories; } @Override - public Map getInternalRepositories(Environment env, NamedXContentRegistry namedXContentRegistry, - ClusterService clusterService, RecoverySettings recoverySettings) { - HashMap internalRepositories = - new HashMap<>(super.getInternalRepositories(env, namedXContentRegistry, clusterService, recoverySettings)); - filterPlugins(RepositoryPlugin.class).forEach(r -> - internalRepositories.putAll(r.getInternalRepositories(env, namedXContentRegistry, clusterService, recoverySettings))); + public Map getInternalRepositories( + Environment env, + NamedXContentRegistry namedXContentRegistry, + ClusterService clusterService, + RecoverySettings recoverySettings + ) { + HashMap internalRepositories = new HashMap<>( + super.getInternalRepositories(env, namedXContentRegistry, clusterService, recoverySettings) + ); + filterPlugins(RepositoryPlugin.class).forEach( + r -> internalRepositories.putAll(r.getInternalRepositories(env, namedXContentRegistry, clusterService, recoverySettings)) + ); return internalRepositories; } @@ -485,27 +589,24 @@ public Optional getEngineFactory(IndexSettings indexSettings) { @Override public Collection> mappingRequestValidators() { - return filterPlugins(ActionPlugin.class) - .stream() - .flatMap(p -> p.mappingRequestValidators().stream()) - .collect(Collectors.toList()); + return filterPlugins(ActionPlugin.class).stream().flatMap(p -> p.mappingRequestValidators().stream()).collect(Collectors.toList()); } @Override public Collection> indicesAliasesRequestValidators() { - return filterPlugins(ActionPlugin.class) - .stream() - .flatMap(p -> p.indicesAliasesRequestValidators().stream()) - .collect(Collectors.toList()); + return filterPlugins(ActionPlugin.class).stream() + .flatMap(p -> p.indicesAliasesRequestValidators().stream()) + .collect(Collectors.toList()); } @Override public Collection createAllocationDeciders(Settings settings, ClusterSettings clusterSettings) { Set deciders = new HashSet<>(); - deciders.addAll(filterPlugins(ClusterPlugin.class) - .stream() - .flatMap(p -> p.createAllocationDeciders(settings, clusterSettings).stream()) - .collect(Collectors.toList())); + deciders.addAll( + filterPlugins(ClusterPlugin.class).stream() + .flatMap(p -> p.createAllocationDeciders(settings, clusterSettings).stream()) + .collect(Collectors.toList()) + ); deciders.addAll(super.createAllocationDeciders(settings, clusterSettings)); return deciders; } @@ -547,8 +648,7 @@ public Map getSnapshotCommitSup @SuppressWarnings("unchecked") private List filterPlugins(Class type) { - return plugins.stream().filter(x -> type.isAssignableFrom(x.getClass())).map(p -> ((T)p)) - .collect(Collectors.toList()); + return plugins.stream().filter(x -> type.isAssignableFrom(x.getClass())).map(p -> ((T) p)).collect(Collectors.toList()); } @Override @@ -563,7 +663,7 @@ public Collection getSystemIndexDescriptors(Settings sett public Map getMetadataMappers() { return filterPlugins(MapperPlugin.class).stream() .map(MapperPlugin::getMetadataMappers) - .flatMap (map -> map.entrySet().stream()) + .flatMap(map -> map.entrySet().stream()) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); } @@ -579,11 +679,11 @@ public String getFeatureDescription() { @Override public CheckedBiConsumer getRequestCacheKeyDifferentiator() { - final List> differentiators = - filterPlugins(SearchPlugin.class).stream() - .map(SearchPlugin::getRequestCacheKeyDifferentiator) - .filter(Objects::nonNull) - .collect(Collectors.toUnmodifiableList()); + final List> differentiators = filterPlugins(SearchPlugin.class) + .stream() + .map(SearchPlugin::getRequestCacheKeyDifferentiator) + .filter(Objects::nonNull) + .collect(Collectors.toUnmodifiableList()); if (differentiators.size() > 1) { throw new UnsupportedOperationException("Only the security SearchPlugin should provide the request cache key differentiator"); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/XPackPluginTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/XPackPluginTests.java index abe17bf6ddcd6..7d6644c5d8bd2 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/XPackPluginTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/XPackPluginTests.java @@ -33,8 +33,10 @@ public void testXPackInstalledAttrClash() throws Exception { } XPackPlugin xpackPlugin = createXPackPlugin(builder.put("path.home", createTempDir()).build()); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, xpackPlugin::additionalSettings); - assertThat(e.getMessage(), - containsString("Directly setting [node.attr." + XPackPlugin.XPACK_INSTALLED_NODE_ATTR + "] is not permitted")); + assertThat( + e.getMessage(), + containsString("Directly setting [node.attr." + XPackPlugin.XPACK_INSTALLED_NODE_ATTR + "] is not permitted") + ); } public void testXPackInstalledAttrExists() throws Exception { @@ -56,8 +58,9 @@ public void testNodesNotReadyForXPackCustomMetadata() { attributes = Collections.emptyMap(); } - discoveryNodes.add(new DiscoveryNode("node_" + i, buildNewFakeTransportAddress(), attributes, Collections.emptySet(), - Version.CURRENT)); + discoveryNodes.add( + new DiscoveryNode("node_" + i, buildNewFakeTransportAddress(), attributes, Collections.emptySet(), Version.CURRENT) + ); } ClusterState.Builder clusterStateBuilder = ClusterState.builder(ClusterName.DEFAULT); @@ -74,14 +77,16 @@ public void testNodesNotReadyForXPackCustomMetadata() { assertEquals(XPackPlugin.isReadyForXPackCustomMetadata(clusterState), compatible); if (compatible == false) { - IllegalStateException e = expectThrows(IllegalStateException.class, - () -> XPackPlugin.checkReadyForXPackCustomMetadata(clusterState)); + IllegalStateException e = expectThrows( + IllegalStateException.class, + () -> XPackPlugin.checkReadyForXPackCustomMetadata(clusterState) + ); assertThat(e.getMessage(), containsString("The following nodes are not ready yet for enabling x-pack custom metadata:")); } } private XPackPlugin createXPackPlugin(Settings settings) throws Exception { - return new XPackPlugin(settings, null){ + return new XPackPlugin(settings, null) { @Override protected void setSslService(SSLService sslService) { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/XPackSettingsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/XPackSettingsTests.java index 67ce5e2e000bf..bf8cd021e0222 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/XPackSettingsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/XPackSettingsTests.java @@ -6,13 +6,14 @@ */ package org.elasticsearch.xpack.core; -import org.elasticsearch.jdk.JavaVersion; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.jdk.JavaVersion; import org.elasticsearch.test.ESTestCase; -import javax.crypto.SecretKeyFactory; import java.security.NoSuchAlgorithmException; +import javax.crypto.SecretKeyFactory; + import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; @@ -47,14 +48,20 @@ public void testPasswordHashingAlgorithmSettingValidation() { if (isPBKDF2Available) { assertEquals(pbkdf2Algo, XPackSettings.PASSWORD_HASHING_ALGORITHM.get(settings)); } else { - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> XPackSettings.PASSWORD_HASHING_ALGORITHM.get(settings)); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> XPackSettings.PASSWORD_HASHING_ALGORITHM.get(settings) + ); assertThat(e.getMessage(), containsString("Support for PBKDF2WithHMACSHA512 must be available")); } final String bcryptAlgo = randomFrom("BCRYPT", "BCRYPT11"); - assertEquals(bcryptAlgo, XPackSettings.PASSWORD_HASHING_ALGORITHM.get( - Settings.builder().put(XPackSettings.PASSWORD_HASHING_ALGORITHM.getKey(), bcryptAlgo).build())); + assertEquals( + bcryptAlgo, + XPackSettings.PASSWORD_HASHING_ALGORITHM.get( + Settings.builder().put(XPackSettings.PASSWORD_HASHING_ALGORITHM.getKey(), bcryptAlgo).build() + ) + ); } public void testDefaultPasswordHashingAlgorithmInFips() { @@ -83,14 +90,20 @@ public void testServiceTokenHashingAlgorithmSettingValidation() { if (isPBKDF2Available) { assertEquals(pbkdf2Algo, XPackSettings.SERVICE_TOKEN_HASHING_ALGORITHM.get(settings)); } else { - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> XPackSettings.SERVICE_TOKEN_HASHING_ALGORITHM.get(settings)); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> XPackSettings.SERVICE_TOKEN_HASHING_ALGORITHM.get(settings) + ); assertThat(e.getMessage(), containsString("Support for PBKDF2WithHMACSHA512 must be available")); } final String bcryptAlgo = randomFrom("BCRYPT", "BCRYPT11"); - assertEquals(bcryptAlgo, XPackSettings.SERVICE_TOKEN_HASHING_ALGORITHM.get( - Settings.builder().put(XPackSettings.SERVICE_TOKEN_HASHING_ALGORITHM.getKey(), bcryptAlgo).build())); + assertEquals( + bcryptAlgo, + XPackSettings.SERVICE_TOKEN_HASHING_ALGORITHM.get( + Settings.builder().put(XPackSettings.SERVICE_TOKEN_HASHING_ALGORITHM.getKey(), bcryptAlgo).build() + ) + ); } public void testDefaultServiceTokenHashingAlgorithm() { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/DelegatePkiAuthenticationRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/DelegatePkiAuthenticationRequestTests.java index 090de8c0bc877..9758e7a74c304 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/DelegatePkiAuthenticationRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/DelegatePkiAuthenticationRequestTests.java @@ -10,8 +10,8 @@ import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.StreamInput; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.security.action.DelegatePkiAuthenticationRequest; import java.io.IOException; @@ -79,8 +79,12 @@ public void testSerialization() throws Exception { private List randomCertificateList() { List certificates = Arrays.asList(randomArray(1, 3, X509Certificate[]::new, () -> { try { - return readCert(getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/" - + randomFrom("testclient.crt", "testnode.crt", "testnode-ip-only.crt", "openldap.crt", "samba4.crt"))); + return readCert( + getDataPath( + "/org/elasticsearch/xpack/security/transport/ssl/certs/simple/" + + randomFrom("testclient.crt", "testnode.crt", "testnode-ip-only.crt", "openldap.crt", "samba4.crt") + ) + ); } catch (Exception e) { throw new RuntimeException(e); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/DelegatePkiAuthenticationResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/DelegatePkiAuthenticationResponseTests.java index 460c90d571aaa..b539d0a46fec6 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/DelegatePkiAuthenticationResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/DelegatePkiAuthenticationResponseTests.java @@ -8,20 +8,20 @@ package org.elasticsearch.xpack.core.action; import org.elasticsearch.Version; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xpack.core.security.action.DelegatePkiAuthenticationResponse; import org.elasticsearch.xpack.core.security.authc.Authentication; import org.elasticsearch.xpack.core.security.user.User; import java.io.IOException; -import java.util.Arrays; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.Locale; import java.util.Map; @@ -47,9 +47,11 @@ public void testSerialization() throws Exception { @Override protected DelegatePkiAuthenticationResponse createTestInstance() { - return new DelegatePkiAuthenticationResponse(randomAlphaOfLengthBetween(0, 10), - TimeValue.parseTimeValue(randomTimeValue(), getClass().getSimpleName() + ".expiresIn"), - createAuthentication()); + return new DelegatePkiAuthenticationResponse( + randomAlphaOfLengthBetween(0, 10), + TimeValue.parseTimeValue(randomTimeValue(), getClass().getSimpleName() + ".expiresIn"), + createAuthentication() + ); } @Override @@ -68,17 +70,20 @@ protected boolean supportsUnknownFields() { private static final ParseField AUTHENTICATION = new ParseField("authentication"); public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "delegate_pki_response", true, a -> { - final String accessToken = (String) a[0]; - final String type = (String) a[1]; - if (false == "Bearer".equals(type)) { - throw new IllegalArgumentException("Unknown token type [" + type + "], only [Bearer] type permitted"); - } - final Long expiresIn = (Long) a[2]; - final Authentication authentication = (Authentication) a[3]; + "delegate_pki_response", + true, + a -> { + final String accessToken = (String) a[0]; + final String type = (String) a[1]; + if (false == "Bearer".equals(type)) { + throw new IllegalArgumentException("Unknown token type [" + type + "], only [Bearer] type permitted"); + } + final Long expiresIn = (Long) a[2]; + final Authentication authentication = (Authentication) a[3]; - return new DelegatePkiAuthenticationResponse(accessToken, TimeValue.timeValueSeconds(expiresIn), authentication); - }); + return new DelegatePkiAuthenticationResponse(accessToken, TimeValue.timeValueSeconds(expiresIn), authentication); + } + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), ACCESS_TOKEN_FIELD); @@ -89,13 +94,30 @@ protected boolean supportsUnknownFields() { @SuppressWarnings("unchecked") private static final ConstructingObjectParser AUTH_PARSER = new ConstructingObjectParser<>( - "authentication", true, - a -> new Authentication(new User((String) a[0], ((ArrayList) a[1]).toArray(new String[0]), (String) a[2], (String) a[3], - (Map) a[4], (boolean) a[5]), (Authentication.RealmRef) a[6], (Authentication.RealmRef) a[7], Version.CURRENT, - Authentication.AuthenticationType.valueOf(a[8].toString().toUpperCase(Locale.ROOT)), (Map) a[4])); + "authentication", + true, + a -> new Authentication( + new User( + (String) a[0], + ((ArrayList) a[1]).toArray(new String[0]), + (String) a[2], + (String) a[3], + (Map) a[4], + (boolean) a[5] + ), + (Authentication.RealmRef) a[6], + (Authentication.RealmRef) a[7], + Version.CURRENT, + Authentication.AuthenticationType.valueOf(a[8].toString().toUpperCase(Locale.ROOT)), + (Map) a[4] + ) + ); static { - final ConstructingObjectParser realmInfoParser = new ConstructingObjectParser<>("realm_info", true, - a -> new Authentication.RealmRef((String) a[0], (String) a[1], "node_name")); + final ConstructingObjectParser realmInfoParser = new ConstructingObjectParser<>( + "realm_info", + true, + a -> new Authentication.RealmRef((String) a[0], (String) a[1], "node_name") + ); realmInfoParser.declareString(ConstructingObjectParser.constructorArg(), User.Fields.REALM_NAME); realmInfoParser.declareString(ConstructingObjectParser.constructorArg(), User.Fields.REALM_TYPE); AUTH_PARSER.declareString(ConstructingObjectParser.constructorArg(), User.Fields.USERNAME); @@ -139,6 +161,10 @@ public static Authentication createAuthentication() { return new Authentication( new User(username, roles, fullName, email, metadata, true), new Authentication.RealmRef(authenticationRealmName, authenticationRealmType, nodeName), - new Authentication.RealmRef(lookupRealmName, lookupRealmType, nodeName), Version.CURRENT, authenticationType, metadata); + new Authentication.RealmRef(lookupRealmName, lookupRealmType, nodeName), + Version.CURRENT, + authenticationType, + metadata + ); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/ReloadAnalyzerTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/ReloadAnalyzerTests.java index ab01e73903f52..d22e8ce366ee8 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/ReloadAnalyzerTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/ReloadAnalyzerTests.java @@ -51,8 +51,12 @@ public void testSynonymsUpdateable() throws IOException { final String indexName = "test"; final String synonymAnalyzerName = "synonym_analyzer"; final String synonymGraphAnalyzerName = "synonym_graph_analyzer"; - assertAcked(client().admin().indices().prepareCreate(indexName) - .setSettings(Settings.builder() + assertAcked( + client().admin() + .indices() + .prepareCreate(indexName) + .setSettings( + Settings.builder() .put("index.number_of_shards", 5) .put("index.number_of_replicas", 0) .put("analysis.analyzer." + synonymAnalyzerName + ".tokenizer", "standard") @@ -64,8 +68,10 @@ public void testSynonymsUpdateable() throws IOException { .put("analysis.filter.synonym_filter.synonyms_path", synonymsFileName) .put("analysis.filter.synonym_graph_filter.type", "synonym_graph") .put("analysis.filter.synonym_graph_filter.updateable", "true") - .put("analysis.filter.synonym_graph_filter.synonyms_path", synonymsFileName)) - .setMapping("field", "type=text,analyzer=standard,search_analyzer=" + synonymAnalyzerName)); + .put("analysis.filter.synonym_graph_filter.synonyms_path", synonymsFileName) + ) + .setMapping("field", "type=text,analyzer=standard,search_analyzer=" + synonymAnalyzerName) + ); client().prepareIndex(indexName).setId("1").setSource("field", "Foo").get(); assertNoFailures(client().admin().indices().prepareRefresh(indexName).execute().actionGet()); @@ -87,12 +93,15 @@ public void testSynonymsUpdateable() throws IOException { } // now update synonyms file and trigger reloading - try (PrintWriter out = new PrintWriter( - new OutputStreamWriter(Files.newOutputStream(synonymsFile, StandardOpenOption.WRITE), StandardCharsets.UTF_8))) { + try ( + PrintWriter out = new PrintWriter( + new OutputStreamWriter(Files.newOutputStream(synonymsFile, StandardOpenOption.WRITE), StandardCharsets.UTF_8) + ) + ) { out.println("foo, baz, buzz"); } ReloadAnalyzersResponse reloadResponse = client().execute(ReloadAnalyzerAction.INSTANCE, new ReloadAnalyzersRequest(indexName)) - .actionGet(); + .actionGet(); assertNoFailures(reloadResponse); Set reloadedAnalyzers = reloadResponse.getReloadDetails().get(indexName).getReloadedAnalyzers(); assertEquals(2, reloadedAnalyzers.size()); @@ -123,8 +132,12 @@ public void testSynonymsInMultiplexerUpdateable() throws FileNotFoundException, final String indexName = "test"; final String synonymAnalyzerName = "synonym_in_multiplexer_analyzer"; - assertAcked(client().admin().indices().prepareCreate(indexName) - .setSettings(Settings.builder() + assertAcked( + client().admin() + .indices() + .prepareCreate(indexName) + .setSettings( + Settings.builder() .put("index.number_of_shards", 5) .put("index.number_of_replicas", 0) .put("analysis.analyzer." + synonymAnalyzerName + ".tokenizer", "whitespace") @@ -133,8 +146,10 @@ public void testSynonymsInMultiplexerUpdateable() throws FileNotFoundException, .put("analysis.filter.synonym_filter.updateable", "true") .put("analysis.filter.synonym_filter.synonyms_path", synonymsFileName) .put("analysis.filter.my_multiplexer.type", "multiplexer") - .putList("analysis.filter.my_multiplexer.filters", "synonym_filter")) - .setMapping("field", "type=text,analyzer=standard,search_analyzer=" + synonymAnalyzerName)); + .putList("analysis.filter.my_multiplexer.filters", "synonym_filter") + ) + .setMapping("field", "type=text,analyzer=standard,search_analyzer=" + synonymAnalyzerName) + ); client().prepareIndex(indexName).setId("1").setSource("field", "foo").get(); assertNoFailures(client().admin().indices().prepareRefresh(indexName).execute().actionGet()); @@ -152,12 +167,15 @@ public void testSynonymsInMultiplexerUpdateable() throws FileNotFoundException, assertTrue(tokens.contains("baz")); // now update synonyms file and trigger reloading - try (PrintWriter out = new PrintWriter( - new OutputStreamWriter(Files.newOutputStream(synonymsFile, StandardOpenOption.WRITE), StandardCharsets.UTF_8))) { + try ( + PrintWriter out = new PrintWriter( + new OutputStreamWriter(Files.newOutputStream(synonymsFile, StandardOpenOption.WRITE), StandardCharsets.UTF_8) + ) + ) { out.println("foo, baz, buzz"); } ReloadAnalyzersResponse reloadResponse = client().execute(ReloadAnalyzerAction.INSTANCE, new ReloadAnalyzersRequest(indexName)) - .actionGet(); + .actionGet(); assertNoFailures(reloadResponse); Set reloadedAnalyzers = reloadResponse.getReloadDetails().get(indexName).getReloadedAnalyzers(); assertEquals(1, reloadedAnalyzers.size()); @@ -188,31 +206,50 @@ public void testUpdateableSynonymsRejectedAtIndexTime() throws FileNotFoundExcep if (Files.exists(synonymsFile) == false) { Files.createFile(synonymsFile); } - try (PrintWriter out = new PrintWriter( - new OutputStreamWriter(Files.newOutputStream(synonymsFile, StandardOpenOption.WRITE), StandardCharsets.UTF_8))) { + try ( + PrintWriter out = new PrintWriter( + new OutputStreamWriter(Files.newOutputStream(synonymsFile, StandardOpenOption.WRITE), StandardCharsets.UTF_8) + ) + ) { out.println("foo, baz"); } final String indexName = "test"; final String analyzerName = "my_synonym_analyzer"; - MapperException ex = expectThrows(MapperException.class, - () -> client().admin().indices().prepareCreate(indexName).setSettings(Settings.builder() + MapperException ex = expectThrows( + MapperException.class, + () -> client().admin() + .indices() + .prepareCreate(indexName) + .setSettings( + Settings.builder() .put("index.number_of_shards", 5) .put("index.number_of_replicas", 0) .put("analysis.analyzer." + analyzerName + ".tokenizer", "standard") .putList("analysis.analyzer." + analyzerName + ".filter", "lowercase", "synonym_filter") .put("analysis.filter.synonym_filter.type", "synonym") .put("analysis.filter.synonym_filter.updateable", "true") - .put("analysis.filter.synonym_filter.synonyms_path", synonymsFileName)) - .setMapping("field", "type=text,analyzer=" + analyzerName).get()); + .put("analysis.filter.synonym_filter.synonyms_path", synonymsFileName) + ) + .setMapping("field", "type=text,analyzer=" + analyzerName) + .get() + ); - assertEquals("Failed to parse mapping: analyzer [my_synonym_analyzer] " - + "contains filters [synonym_filter] that are not allowed to run in index time mode.", ex.getMessage()); + assertEquals( + "Failed to parse mapping: analyzer [my_synonym_analyzer] " + + "contains filters [synonym_filter] that are not allowed to run in index time mode.", + ex.getMessage() + ); // same for synonym filters in multiplexer chain - ex = expectThrows(MapperException.class, - () -> client().admin().indices().prepareCreate(indexName).setSettings(Settings.builder() + ex = expectThrows( + MapperException.class, + () -> client().admin() + .indices() + .prepareCreate(indexName) + .setSettings( + Settings.builder() .put("index.number_of_shards", 5) .put("index.number_of_replicas", 0) .put("analysis.analyzer." + analyzerName + ".tokenizer", "whitespace") @@ -221,11 +258,17 @@ public void testUpdateableSynonymsRejectedAtIndexTime() throws FileNotFoundExcep .put("analysis.filter.synonym_filter.updateable", "true") .put("analysis.filter.synonym_filter.synonyms_path", synonymsFileName) .put("analysis.filter.my_multiplexer.type", "multiplexer") - .putList("analysis.filter.my_multiplexer.filters", "synonym_filter")) - .setMapping("field", "type=text,analyzer=" + analyzerName).get()); - - assertEquals("Failed to parse mapping: analyzer [my_synonym_analyzer] " - + "contains filters [my_multiplexer] that are not allowed to run in index time mode.", ex.getMessage()); + .putList("analysis.filter.my_multiplexer.filters", "synonym_filter") + ) + .setMapping("field", "type=text,analyzer=" + analyzerName) + .get() + ); + + assertEquals( + "Failed to parse mapping: analyzer [my_synonym_analyzer] " + + "contains filters [my_multiplexer] that are not allowed to run in index time mode.", + ex.getMessage() + ); } public void testKeywordMarkerUpdateable() throws IOException { @@ -234,16 +277,22 @@ public void testKeywordMarkerUpdateable() throws IOException { final String indexName = "test"; final String analyzerName = "keyword_maker_analyzer"; - assertAcked(client().admin().indices().prepareCreate(indexName) - .setSettings(Settings.builder() + assertAcked( + client().admin() + .indices() + .prepareCreate(indexName) + .setSettings( + Settings.builder() .put("index.number_of_shards", 5) .put("index.number_of_replicas", 0) .put("analysis.analyzer." + analyzerName + ".tokenizer", "whitespace") .putList("analysis.analyzer." + analyzerName + ".filter", "keyword_marker_filter", "stemmer") .put("analysis.filter.keyword_marker_filter.type", "keyword_marker") .put("analysis.filter.keyword_marker_filter.updateable", "true") - .put("analysis.filter.keyword_marker_filter.keywords_path", fileName)) - .setMapping("field", "type=text,analyzer=standard,search_analyzer=" + analyzerName)); + .put("analysis.filter.keyword_marker_filter.keywords_path", fileName) + ) + .setMapping("field", "type=text,analyzer=standard,search_analyzer=" + analyzerName) + ); AnalyzeAction.Response analysisResponse = client().admin() .indices() @@ -255,14 +304,17 @@ public void testKeywordMarkerUpdateable() throws IOException { assertEquals("jump", tokens.get(1).getTerm()); // now update keyword marker file and trigger reloading - try (PrintWriter out = new PrintWriter( - new OutputStreamWriter(Files.newOutputStream(file, StandardOpenOption.WRITE), StandardCharsets.UTF_8))) { + try ( + PrintWriter out = new PrintWriter( + new OutputStreamWriter(Files.newOutputStream(file, StandardOpenOption.WRITE), StandardCharsets.UTF_8) + ) + ) { out.println("running"); out.println("jumping"); } ReloadAnalyzersResponse reloadResponse = client().execute(ReloadAnalyzerAction.INSTANCE, new ReloadAnalyzersRequest(indexName)) - .actionGet(); + .actionGet(); assertNoFailures(reloadResponse); Set reloadedAnalyzers = reloadResponse.getReloadDetails().get(indexName).getReloadedAnalyzers(); assertEquals(1, reloadedAnalyzers.size()); @@ -283,8 +335,11 @@ private Path setupResourceFile(String fileName, String... content) throws IOExce if (Files.exists(file) == false) { Files.createFile(file); } - try (PrintWriter out = new PrintWriter( - new OutputStreamWriter(Files.newOutputStream(file, StandardOpenOption.WRITE), StandardCharsets.UTF_8))) { + try ( + PrintWriter out = new PrintWriter( + new OutputStreamWriter(Files.newOutputStream(file, StandardOpenOption.WRITE), StandardCharsets.UTF_8) + ) + ) { for (String item : content) { out.println(item); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/ReloadAnalyzersResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/ReloadAnalyzersResponseTests.java index 72d89b7ef41d3..0616ca80ee63c 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/ReloadAnalyzersResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/ReloadAnalyzersResponseTests.java @@ -8,9 +8,9 @@ import org.elasticsearch.action.support.DefaultShardOperationFailedException; import org.elasticsearch.common.Strings; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractBroadcastResponseTestCase; import org.elasticsearch.test.VersionUtils; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.action.ReloadAnalyzersResponse.ReloadDetails; import java.io.IOException; @@ -25,8 +25,12 @@ public class ReloadAnalyzersResponseTests extends AbstractBroadcastResponseTestCase { @Override - protected ReloadAnalyzersResponse createTestInstance(int totalShards, int successfulShards, int failedShards, - List failures) { + protected ReloadAnalyzersResponse createTestInstance( + int totalShards, + int successfulShards, + int failedShards, + List failures + ) { Map reloadedIndicesDetails = new HashMap<>(); int randomIndices = randomIntBetween(0, 5); for (int i = 0; i < randomIndices; i++) { @@ -45,21 +49,28 @@ protected ReloadAnalyzersResponse doParseInstance(XContentParser parser) throws @Override public void testToXContent() { - Map reloadedIndicesNodes = Collections.singletonMap("index", - new ReloadDetails("index", Collections.singleton("nodeId"), Collections.singleton("my_analyzer"))); + Map reloadedIndicesNodes = Collections.singletonMap( + "index", + new ReloadDetails("index", Collections.singleton("nodeId"), Collections.singleton("my_analyzer")) + ); ReloadAnalyzersResponse response = new ReloadAnalyzersResponse(10, 5, 5, null, reloadedIndicesNodes); String output = Strings.toString(response); assertEquals( - "{\"_shards\":{\"total\":10,\"successful\":5,\"failed\":5}," + "{\"_shards\":{\"total\":10,\"successful\":5,\"failed\":5}," + "\"reload_details\":[{\"index\":\"index\",\"reloaded_analyzers\":[\"my_analyzer\"],\"reloaded_node_ids\":[\"nodeId\"]}]" + "}", - output); + output + ); } public void testSerialization() throws IOException { ReloadAnalyzersResponse response = createTestInstance(); - ReloadAnalyzersResponse copy = copyWriteable(response, writableRegistry(), ReloadAnalyzersResponse::new, - VersionUtils.randomVersion(random())); + ReloadAnalyzersResponse copy = copyWriteable( + response, + writableRegistry(), + ReloadAnalyzersResponse::new, + VersionUtils.randomVersion(random()) + ); assertEquals(response.getReloadDetails(), copy.getReloadDetails()); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/ReloadDetailsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/ReloadDetailsTests.java index a2fe7b54d7957..46e6c32edf101 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/ReloadDetailsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/ReloadDetailsTests.java @@ -19,8 +19,11 @@ public class ReloadDetailsTests extends AbstractWireSerializingTestCase reloadedIndicesNodes = new HashSet<>(instance.getReloadedIndicesNodes()); int mutate = randomIntBetween(0, 2); switch (mutate) { - case 0: - indexName = indexName + randomAlphaOfLength(2); - break; - case 1: - reloadedAnalyzers.add(randomAlphaOfLength(10)); - break; - case 2: - reloadedIndicesNodes.add(randomAlphaOfLength(10)); - break; - default: - throw new IllegalStateException("Requested to modify more than available parameters."); + case 0: + indexName = indexName + randomAlphaOfLength(2); + break; + case 1: + reloadedAnalyzers.add(randomAlphaOfLength(10)); + break; + case 2: + reloadedIndicesNodes.add(randomAlphaOfLength(10)); + break; + default: + throw new IllegalStateException("Requested to modify more than available parameters."); } return new ReloadDetails(indexName, reloadedIndicesNodes, reloadedAnalyzers); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/SetResetModeActionRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/SetResetModeActionRequestTests.java index 5e28b413e154d..0b0d28f34d16c 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/SetResetModeActionRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/SetResetModeActionRequestTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.action; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; public class SetResetModeActionRequestTests extends AbstractSerializingTestCase { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/TransportXPackInfoActionTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/TransportXPackInfoActionTests.java index 7c90d6a21a0d5..bd4b7cfb7c6f8 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/TransportXPackInfoActionTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/TransportXPackInfoActionTests.java @@ -54,14 +54,18 @@ public void testDoExecute() throws Exception { FeatureSet featureSet = new FeatureSet(randomAlphaOfLength(5), randomBoolean(), randomBoolean()); featureSets.put(infoAction, featureSet); when(client.executeLocally(eq(infoAction), any(ActionRequest.class), any(ActionListener.class))).thenAnswer(answer -> { - var listener = (ActionListener)answer.getArguments()[2]; + var listener = (ActionListener) answer.getArguments()[2]; listener.onResponse(new XPackInfoFeatureResponse(featureSet)); return null; }); } - TransportXPackInfoAction action = new TransportXPackInfoAction(mock(TransportService.class), mock(ActionFilters.class), - licenseService, client) { + TransportXPackInfoAction action = new TransportXPackInfoAction( + mock(TransportService.class), + mock(ActionFilters.class), + licenseService, + client + ) { @Override protected List infoActions() { return new ArrayList<>(featureSets.keySet()); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/XPackUsageResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/XPackUsageResponseTests.java index 661b180ac96a8..a5501cebfe6db 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/XPackUsageResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/XPackUsageResponseTests.java @@ -35,11 +35,7 @@ public static void setVersion() { VersionUtils.getFirstVersion(), VersionUtils.getPreviousVersion(VersionUtils.getPreviousMinorVersion()) ); - newVersion = VersionUtils.randomVersionBetween( - random(), - VersionUtils.getPreviousMinorVersion(), - Version.CURRENT - ); + newVersion = VersionUtils.randomVersionBetween(random(), VersionUtils.getPreviousMinorVersion(), Version.CURRENT); } public static class OldUsage extends XPackFeatureSet.Usage { @@ -82,10 +78,12 @@ public void testVersionDependentSerializationWriteToOldStream() throws IOExcepti oldStream.setVersion(VersionUtils.randomVersionBetween(random(), oldVersion, VersionUtils.getPreviousVersion(newVersion))); before.writeTo(oldStream); - final NamedWriteableRegistry registry = new NamedWriteableRegistry(List.of( - new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, "old", OldUsage::new), - new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, "new", NewUsage::new) - )); + final NamedWriteableRegistry registry = new NamedWriteableRegistry( + List.of( + new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, "old", OldUsage::new), + new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, "new", NewUsage::new) + ) + ); final StreamInput in = new NamedWriteableAwareStreamInput(oldStream.bytes().streamInput(), registry); final XPackUsageResponse after = new XPackUsageResponse(in); @@ -99,10 +97,12 @@ public void testVersionDependentSerializationWriteToNewStream() throws IOExcepti newStream.setVersion(VersionUtils.randomVersionBetween(random(), newVersion, Version.CURRENT)); before.writeTo(newStream); - final NamedWriteableRegistry registry = new NamedWriteableRegistry(List.of( - new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, "old", OldUsage::new), - new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, "new", NewUsage::new) - )); + final NamedWriteableRegistry registry = new NamedWriteableRegistry( + List.of( + new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, "old", OldUsage::new), + new NamedWriteableRegistry.Entry(XPackFeatureSet.Usage.class, "new", NewUsage::new) + ) + ); final StreamInput in = new NamedWriteableAwareStreamInput(newStream.bytes().streamInput(), registry); final XPackUsageResponse after = new XPackUsageResponse(in); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/util/ExpandedIdsMatcherTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/util/ExpandedIdsMatcherTests.java index ad66c0d863825..e030797dfa4d8 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/util/ExpandedIdsMatcherTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/util/ExpandedIdsMatcherTests.java @@ -20,7 +20,7 @@ public class ExpandedIdsMatcherTests extends ESTestCase { public void testMatchingResourceIds() { - ExpandedIdsMatcher requiredMatches = new ExpandedIdsMatcher(new String[] {"*"}, false); + ExpandedIdsMatcher requiredMatches = new ExpandedIdsMatcher(new String[] { "*" }, false); assertThat(requiredMatches.unmatchedIds(), hasSize(1)); assertTrue(requiredMatches.hasUnmatchedIds()); requiredMatches.filterMatchedIds(Collections.singletonList("foo")); @@ -53,26 +53,26 @@ public void testMatchingResourceIds() { assertThat(requiredMatches.unmatchedIds(), empty()); assertFalse(requiredMatches.isOnlyExact()); - requiredMatches = new ExpandedIdsMatcher(new String[] {"foo*"}, false); + requiredMatches = new ExpandedIdsMatcher(new String[] { "foo*" }, false); assertThat(requiredMatches.unmatchedIds(), hasSize(1)); - requiredMatches.filterMatchedIds(Arrays.asList("foo1","foo2")); + requiredMatches.filterMatchedIds(Arrays.asList("foo1", "foo2")); assertThat(requiredMatches.unmatchedIds(), empty()); assertFalse(requiredMatches.isOnlyExact()); - requiredMatches = new ExpandedIdsMatcher(new String[] {"foo*","bar"}, false); + requiredMatches = new ExpandedIdsMatcher(new String[] { "foo*", "bar" }, false); assertThat(requiredMatches.unmatchedIds(), hasSize(2)); - requiredMatches.filterMatchedIds(Arrays.asList("foo1","foo2")); + requiredMatches.filterMatchedIds(Arrays.asList("foo1", "foo2")); assertThat(requiredMatches.unmatchedIds(), hasSize(1)); assertEquals("bar", requiredMatches.unmatchedIds().get(0)); assertFalse(requiredMatches.isOnlyExact()); - requiredMatches = new ExpandedIdsMatcher(new String[] {"foo*","bar"}, false); + requiredMatches = new ExpandedIdsMatcher(new String[] { "foo*", "bar" }, false); assertThat(requiredMatches.unmatchedIds(), hasSize(2)); - requiredMatches.filterMatchedIds(Arrays.asList("foo1","bar")); + requiredMatches.filterMatchedIds(Arrays.asList("foo1", "bar")); assertFalse(requiredMatches.hasUnmatchedIds()); assertFalse(requiredMatches.isOnlyExact()); - requiredMatches = new ExpandedIdsMatcher(new String[] {"foo*","bar"}, false); + requiredMatches = new ExpandedIdsMatcher(new String[] { "foo*", "bar" }, false); assertThat(requiredMatches.unmatchedIds(), hasSize(2)); requiredMatches.filterMatchedIds(Collections.singletonList("bar")); assertThat(requiredMatches.unmatchedIds(), hasSize(1)); @@ -81,13 +81,13 @@ public void testMatchingResourceIds() { requiredMatches = new ExpandedIdsMatcher(ExpandedIdsMatcher.tokenizeExpression("foo,bar,baz,wild*"), false); assertThat(requiredMatches.unmatchedIds(), hasSize(4)); - requiredMatches.filterMatchedIds(Arrays.asList("foo","baz")); + requiredMatches.filterMatchedIds(Arrays.asList("foo", "baz")); assertThat(requiredMatches.unmatchedIds(), hasSize(2)); assertThat(requiredMatches.unmatchedIds().get(0), is(oneOf("bar", "wild*"))); assertThat(requiredMatches.unmatchedIds().get(1), is(oneOf("bar", "wild*"))); assertFalse(requiredMatches.isOnlyExact()); - requiredMatches = new ExpandedIdsMatcher(new String[] {"foo","bar"}, false); + requiredMatches = new ExpandedIdsMatcher(new String[] { "foo", "bar" }, false); assertThat(requiredMatches.unmatchedIds(), hasSize(2)); requiredMatches.filterMatchedIds(Collections.singletonList("bar")); assertThat(requiredMatches.unmatchedIds(), hasSize(1)); @@ -96,7 +96,7 @@ public void testMatchingResourceIds() { } public void testMatchingResourceIds_allowNoMatch() { - ExpandedIdsMatcher requiredMatches = new ExpandedIdsMatcher(new String[] {"*"}, true); + ExpandedIdsMatcher requiredMatches = new ExpandedIdsMatcher(new String[] { "*" }, true); assertThat(requiredMatches.unmatchedIds(), empty()); assertFalse(requiredMatches.hasUnmatchedIds()); requiredMatches.filterMatchedIds(Collections.emptyList()); @@ -104,7 +104,7 @@ public void testMatchingResourceIds_allowNoMatch() { assertFalse(requiredMatches.hasUnmatchedIds()); assertFalse(requiredMatches.isOnlyExact()); - requiredMatches = new ExpandedIdsMatcher(new String[] {"foo*","bar"}, true); + requiredMatches = new ExpandedIdsMatcher(new String[] { "foo*", "bar" }, true); assertThat(requiredMatches.unmatchedIds(), hasSize(1)); assertTrue(requiredMatches.hasUnmatchedIds()); requiredMatches.filterMatchedIds(Collections.singletonList("bar")); @@ -112,14 +112,14 @@ public void testMatchingResourceIds_allowNoMatch() { assertFalse(requiredMatches.hasUnmatchedIds()); assertFalse(requiredMatches.isOnlyExact()); - requiredMatches = new ExpandedIdsMatcher(new String[] {"foo*","bar"}, true); + requiredMatches = new ExpandedIdsMatcher(new String[] { "foo*", "bar" }, true); assertThat(requiredMatches.unmatchedIds(), hasSize(1)); requiredMatches.filterMatchedIds(Collections.emptyList()); assertThat(requiredMatches.unmatchedIds(), hasSize(1)); assertEquals("bar", requiredMatches.unmatchedIds().get(0)); assertFalse(requiredMatches.isOnlyExact()); - requiredMatches = new ExpandedIdsMatcher(new String[] {"foo","bar"}, true); + requiredMatches = new ExpandedIdsMatcher(new String[] { "foo", "bar" }, true); assertThat(requiredMatches.unmatchedIds(), hasSize(2)); requiredMatches.filterMatchedIds(Collections.singletonList("bar")); assertThat(requiredMatches.unmatchedIds(), hasSize(1)); @@ -129,11 +129,11 @@ public void testMatchingResourceIds_allowNoMatch() { public void testSimpleMatcher() { { - ExpandedIdsMatcher.SimpleIdsMatcher matcher = new ExpandedIdsMatcher.SimpleIdsMatcher(new String[]{"*"}); + ExpandedIdsMatcher.SimpleIdsMatcher matcher = new ExpandedIdsMatcher.SimpleIdsMatcher(new String[] { "*" }); assertTrue(matcher.idMatches(randomAlphaOfLength(5))); } { - ExpandedIdsMatcher.SimpleIdsMatcher matcher = new ExpandedIdsMatcher.SimpleIdsMatcher(new String[]{"foo*","bar"}); + ExpandedIdsMatcher.SimpleIdsMatcher matcher = new ExpandedIdsMatcher.SimpleIdsMatcher(new String[] { "foo*", "bar" }); assertTrue(matcher.idMatches("foo1")); assertTrue(matcher.idMatches("bar")); assertFalse(matcher.idMatches("car")); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/util/PageParamsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/util/PageParamsTests.java index 7fa3a173fc8fb..85b7ae30e4f3f 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/util/PageParamsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/util/PageParamsTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.action.util; import org.elasticsearch.common.io.stream.Writeable.Reader; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; public class PageParamsTests extends AbstractSerializingTestCase { @@ -55,14 +55,14 @@ protected PageParams mutateInstance(PageParams instance) { int size = instance.getSize(); int amountToAdd = between(1, 20); switch (between(0, 1)) { - case 0: - from += amountToAdd; - break; - case 1: - size += amountToAdd; - break; - default: - throw new AssertionError("Illegal randomisation branch"); + case 0: + from += amountToAdd; + break; + case 1: + size += amountToAdd; + break; + default: + throw new AssertionError("Illegal randomisation branch"); } return new PageParams(from, size); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/util/QueryPageTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/util/QueryPageTests.java index c740945d27757..4636b00f3fac4 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/util/QueryPageTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/util/QueryPageTests.java @@ -6,14 +6,14 @@ */ package org.elasticsearch.xpack.core.action.util; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.io.stream.Writeable.Reader; +import org.elasticsearch.test.AbstractWireSerializingTestCase; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.test.AbstractWireSerializingTestCase; import java.io.IOException; import java.util.ArrayList; @@ -94,15 +94,15 @@ protected QueryPage mutateInstance(QueryPage page = instance.results(); long count = instance.count(); switch (between(0, 1)) { - case 0: - page = new ArrayList<>(page); - page.add(new QueryPageTests.QueryPageTester(randomAlphaOfLength(10), randomLong())); - break; - case 1: - count += between(1, 20); - break; - default: - throw new AssertionError("Illegal randomisation branch"); + case 0: + page = new ArrayList<>(page); + page.add(new QueryPageTests.QueryPageTester(randomAlphaOfLength(10), randomLong())); + break; + case 1: + count += between(1, 20); + break; + default: + throw new AssertionError("Illegal randomisation branch"); } return new QueryPage<>(page, count, resultsField); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/async/AsyncExecutionIdTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/async/AsyncExecutionIdTests.java index c1eaa47fc6b09..f2fa5b89916dd 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/async/AsyncExecutionIdTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/async/AsyncExecutionIdTests.java @@ -37,11 +37,13 @@ private static AsyncExecutionId mutate(AsyncExecutionId id) { int rand = randomIntBetween(0, 1); switch (rand) { case 0: - return new AsyncExecutionId(randomAlphaOfLength(id.getDocId().length()+1), id.getTaskId()); + return new AsyncExecutionId(randomAlphaOfLength(id.getDocId().length() + 1), id.getTaskId()); case 1: - return new AsyncExecutionId(id.getDocId(), - new TaskId(randomAlphaOfLength(id.getTaskId().getNodeId().length()), randomNonNegativeLong())); + return new AsyncExecutionId( + id.getDocId(), + new TaskId(randomAlphaOfLength(id.getTaskId().getNodeId().length()), randomNonNegativeLong()) + ); default: throw new AssertionError(); @@ -49,9 +51,11 @@ private static AsyncExecutionId mutate(AsyncExecutionId id) { } public void testEqualsAndHashcode() { - EqualsHashCodeTestUtils.checkEqualsAndHashCode(randomAsyncId(), + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + randomAsyncId(), instance -> new AsyncExecutionId(instance.getDocId(), instance.getTaskId()), - AsyncExecutionIdTests::mutate); + AsyncExecutionIdTests::mutate + ); } public void testDecodeInvalidId() throws IOException { @@ -61,8 +65,10 @@ public void testDecodeInvalidId() throws IOException { assertThat(exc.getCause(), instanceOf(IllegalArgumentException.class)); } { - IllegalArgumentException exc = expectThrows(IllegalArgumentException.class, - () -> AsyncExecutionId.decode("FmhEOGQtRWVpVGplSXRtOVZudXZCOVEaYjFVZjZNWndRa3V0VmJvNV8tQmRpZzoxMzM=?pretty")); + IllegalArgumentException exc = expectThrows( + IllegalArgumentException.class, + () -> AsyncExecutionId.decode("FmhEOGQtRWVpVGplSXRtOVZudXZCOVEaYjFVZjZNWndRa3V0VmJvNV8tQmRpZzoxMzM=?pretty") + ); assertEquals("invalid id: [FmhEOGQtRWVpVGplSXRtOVZudXZCOVEaYjFVZjZNWndRa3V0VmJvNV8tQmRpZzoxMzM=?pretty]", exc.getMessage()); assertThat(exc.getCause(), instanceOf(IllegalArgumentException.class)); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/async/AsyncResultsServiceTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/async/AsyncResultsServiceTests.java index 8af60129696de..c311e9c03ef23 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/async/AsyncResultsServiceTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/async/AsyncResultsServiceTests.java @@ -13,8 +13,8 @@ import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.BigArrays; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.tasks.CancellableTask; import org.elasticsearch.tasks.Task; import org.elasticsearch.tasks.TaskId; @@ -48,8 +48,15 @@ public static class TestTask extends CancellableTask implements AsyncTask { private final Map, TimeValue> listeners = new HashMap<>(); private long expirationTimeMillis; - public TestTask(AsyncExecutionId executionId, long id, String type, String action, String description, TaskId parentTaskId, - Map headers) { + public TestTask( + AsyncExecutionId executionId, + long id, + String type, + String action, + String description, + TaskId parentTaskId, + Map headers + ) { super(id, type, action, description, parentTaskId, headers); this.executionId = executionId; } @@ -114,8 +121,10 @@ public TestRequest(String string) { @Override public Task createTask(long id, String type, String action, TaskId parentTaskId, Map headers) { - AsyncExecutionId asyncExecutionId = new AsyncExecutionId(randomAlphaOfLength(10), - new TaskId(clusterService.localNode().getId(), id)); + AsyncExecutionId asyncExecutionId = new AsyncExecutionId( + randomAlphaOfLength(10), + new TaskId(clusterService.localNode().getId(), id) + ); return new TestTask(asyncExecutionId, id, type, action, string, parentTaskId, headers); } } @@ -126,14 +135,28 @@ public void setup() { TransportService transportService = getInstanceFromNode(TransportService.class); BigArrays bigArrays = getInstanceFromNode(BigArrays.class); taskManager = transportService.getTaskManager(); - indexService = new AsyncTaskIndexService<>("test", clusterService, transportService.getThreadPool().getThreadContext(), - client(), ASYNC_SEARCH_ORIGIN, TestAsyncResponse::new, writableRegistry(), bigArrays); + indexService = new AsyncTaskIndexService<>( + "test", + clusterService, + transportService.getThreadPool().getThreadContext(), + client(), + ASYNC_SEARCH_ORIGIN, + TestAsyncResponse::new, + writableRegistry(), + bigArrays + ); } private AsyncResultsService createResultsService(boolean updateInitialResultsInStore) { - return new AsyncResultsService<>(indexService, updateInitialResultsInStore, TestTask.class, TestTask::addListener, - taskManager, clusterService); + return new AsyncResultsService<>( + indexService, + updateInitialResultsInStore, + TestTask.class, + TestTask::addListener, + taskManager, + clusterService + ); } private DeleteAsyncResultsService createDeleteResultsService() { @@ -163,14 +186,20 @@ public void testRetrieveFromMemoryWithExpiration() throws Exception { if (updateInitialResultsInStore) { // we need to store initial result PlainActionFuture future = new PlainActionFuture<>(); - indexService.createResponse(task.getExecutionId().getDocId(), task.getOriginHeaders(), - new TestAsyncResponse(null, task.getExpirationTime()), future); + indexService.createResponse( + task.getExecutionId().getDocId(), + task.getOriginHeaders(), + new TestAsyncResponse(null, task.getExpirationTime()), + future + ); future.actionGet(TimeValue.timeValueSeconds(10)); } PlainActionFuture listener = new PlainActionFuture<>(); - service.retrieveResult(new GetAsyncResultRequest(task.getExecutionId().getEncoded()) - .setWaitForCompletionTimeout(TimeValue.timeValueSeconds(5)), listener); + service.retrieveResult( + new GetAsyncResultRequest(task.getExecutionId().getEncoded()).setWaitForCompletionTimeout(TimeValue.timeValueSeconds(5)), + listener + ); if (randomBoolean()) { // Test success String expectedResponse = randomAlphaOfLength(10); @@ -205,8 +234,12 @@ public void testAssertExpirationPropagation() throws Exception { if (updateInitialResultsInStore) { // we need to store initial result PlainActionFuture future = new PlainActionFuture<>(); - indexService.createResponse(task.getExecutionId().getDocId(), task.getOriginHeaders(), - new TestAsyncResponse(null, task.getExpirationTime()), future); + indexService.createResponse( + task.getExecutionId().getDocId(), + task.getOriginHeaders(), + new TestAsyncResponse(null, task.getExpirationTime()), + future + ); future.actionGet(TimeValue.timeValueSeconds(10)); } @@ -243,18 +276,30 @@ public void testRetrieveFromDisk() throws Exception { if (updateInitialResultsInStore) { // we need to store initial result PlainActionFuture futureCreate = new PlainActionFuture<>(); - indexService.createResponse(task.getExecutionId().getDocId(), task.getOriginHeaders(), - new TestAsyncResponse(null, task.getExpirationTime()), futureCreate); + indexService.createResponse( + task.getExecutionId().getDocId(), + task.getOriginHeaders(), + new TestAsyncResponse(null, task.getExpirationTime()), + futureCreate + ); futureCreate.actionGet(TimeValue.timeValueSeconds(10)); PlainActionFuture futureUpdate = new PlainActionFuture<>(); - indexService.updateResponse(task.getExecutionId().getDocId(), emptyMap(), - new TestAsyncResponse("final_response", task.getExpirationTime()), futureUpdate); + indexService.updateResponse( + task.getExecutionId().getDocId(), + emptyMap(), + new TestAsyncResponse("final_response", task.getExpirationTime()), + futureUpdate + ); futureUpdate.actionGet(TimeValue.timeValueSeconds(10)); } else { PlainActionFuture futureCreate = new PlainActionFuture<>(); - indexService.createResponse(task.getExecutionId().getDocId(), task.getOriginHeaders(), - new TestAsyncResponse("final_response", task.getExpirationTime()), futureCreate); + indexService.createResponse( + task.getExecutionId().getDocId(), + task.getOriginHeaders(), + new TestAsyncResponse("final_response", task.getExpirationTime()), + futureCreate + ); futureCreate.actionGet(TimeValue.timeValueSeconds(10)); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/async/AsyncSearchIndexServiceTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/async/AsyncSearchIndexServiceTests.java index 34e4ca1589c4d..e99b0739fee8c 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/async/AsyncSearchIndexServiceTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/async/AsyncSearchIndexServiceTests.java @@ -56,7 +56,6 @@ public TestAsyncResponse(String test, long expirationTimeMillis, String failure) this.failure = failure; } - public TestAsyncResponse(StreamInput input) throws IOException { test = input.readOptionalString(); this.expirationTimeMillis = input.readLong(); @@ -85,8 +84,9 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; TestAsyncResponse that = (TestAsyncResponse) o; - return expirationTimeMillis == that.expirationTimeMillis && - Objects.equals(test, that.test) && Objects.equals(failure, that.failure); + return expirationTimeMillis == that.expirationTimeMillis + && Objects.equals(test, that.test) + && Objects.equals(failure, that.failure); } @Override @@ -96,11 +96,16 @@ public int hashCode() { @Override public String toString() { - return "TestAsyncResponse{" + - "test='" + test + '\'' + - "failure='" + failure + '\'' + - ", expirationTimeMillis=" + expirationTimeMillis + - '}'; + return "TestAsyncResponse{" + + "test='" + + test + + '\'' + + "failure='" + + failure + + '\'' + + ", expirationTimeMillis=" + + expirationTimeMillis + + '}'; } @Override @@ -114,8 +119,16 @@ public void setup() { ClusterService clusterService = getInstanceFromNode(ClusterService.class); BigArrays bigArrays = getInstanceFromNode(BigArrays.class); TransportService transportService = getInstanceFromNode(TransportService.class); - indexService = new AsyncTaskIndexService<>("test", clusterService, transportService.getThreadPool().getThreadContext(), - client(), ASYNC_SEARCH_ORIGIN, TestAsyncResponse::new, writableRegistry(), bigArrays); + indexService = new AsyncTaskIndexService<>( + "test", + clusterService, + transportService.getThreadPool().getThreadContext(), + client(), + ASYNC_SEARCH_ORIGIN, + TestAsyncResponse::new, + writableRegistry(), + bigArrays + ); } public void testEncodeSearchResponse() throws IOException { @@ -126,7 +139,8 @@ public void testEncodeSearchResponse() throws IOException { TestAsyncResponse initialResponse = new TestAsyncResponse(testMessage, expirationTime); AsyncExecutionId executionId = new AsyncExecutionId( Long.toString(randomNonNegativeLong()), - new TaskId(randomAlphaOfLength(10), randomNonNegativeLong())); + new TaskId(randomAlphaOfLength(10), randomNonNegativeLong()) + ); PlainActionFuture createFuture = new PlainActionFuture<>(); indexService.createResponse(executionId.getDocId(), Map.of(), initialResponse, createFuture); @@ -176,8 +190,10 @@ public void addEstimateBytesAndMaybeBreak(long bytes, String label) throws Circu addWithoutBreaking(bytes); } else { if (used + bytes > limit) { - throw new CircuitBreakingException("Current used [" + used + "] and requesting bytes [" + bytes + "] " + - "is greater than the limit [" + limit + "]", Durability.TRANSIENT); + throw new CircuitBreakingException( + "Current used [" + used + "] and requesting bytes [" + bytes + "] " + "is greater than the limit [" + limit + "]", + Durability.TRANSIENT + ); } used += bytes; } @@ -228,12 +244,21 @@ public CircuitBreakerStats stats(String name) { BigArrays bigArrays = new BigArrays(null, circuitBreakerService, CircuitBreaker.REQUEST); ClusterService clusterService = getInstanceFromNode(ClusterService.class); TransportService transportService = getInstanceFromNode(TransportService.class); - indexService = new AsyncTaskIndexService<>("test", clusterService, transportService.getThreadPool().getThreadContext(), - client(), ASYNC_SEARCH_ORIGIN, TestAsyncResponse::new, writableRegistry(), bigArrays); + indexService = new AsyncTaskIndexService<>( + "test", + clusterService, + transportService.getThreadPool().getThreadContext(), + client(), + ASYNC_SEARCH_ORIGIN, + TestAsyncResponse::new, + writableRegistry(), + bigArrays + ); AsyncExecutionId executionId = new AsyncExecutionId( Long.toString(randomNonNegativeLong()), - new TaskId(randomAlphaOfLength(10), randomNonNegativeLong())); + new TaskId(randomAlphaOfLength(10), randomNonNegativeLong()) + ); long expirationTime = randomLong(); String testMessage = randomAlphaOfLength(10); { @@ -307,8 +332,10 @@ public CircuitBreakerStats stats(String name) { public void testMaxAsyncSearchResponseSize() throws Exception { try { // successfully create an initial response - AsyncExecutionId executionId1 = new AsyncExecutionId(Long.toString(randomNonNegativeLong()), - new TaskId(randomAlphaOfLength(10), randomNonNegativeLong())); + AsyncExecutionId executionId1 = new AsyncExecutionId( + Long.toString(randomNonNegativeLong()), + new TaskId(randomAlphaOfLength(10), randomNonNegativeLong()) + ); TestAsyncResponse initialResponse = new TestAsyncResponse(randomAlphaOfLength(130), randomLong()); PlainActionFuture createFuture1 = new PlainActionFuture<>(); indexService.createResponse(executionId1.getDocId(), Map.of(), initialResponse, createFuture1); @@ -319,8 +346,12 @@ public void testMaxAsyncSearchResponseSize() throws Exception { ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); updateSettingsRequest.transientSettings(Settings.builder().put("search.max_async_search_response_size", limit + "b")); assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); - String expectedErrMsg = "Can't store an async search response larger than [" + limit + "] bytes. " + - "This limit can be set by changing the [" + MAX_ASYNC_SEARCH_RESPONSE_SIZE_SETTING.getKey() + "] setting."; + String expectedErrMsg = "Can't store an async search response larger than [" + + limit + + "] bytes. " + + "This limit can be set by changing the [" + + MAX_ASYNC_SEARCH_RESPONSE_SIZE_SETTING.getKey() + + "] setting."; // test that an update operation of the initial response fails PlainActionFuture updateFuture = new PlainActionFuture<>(); @@ -334,8 +365,10 @@ public void testMaxAsyncSearchResponseSize() throws Exception { assertEquals(expectedErrMsg, getFuture.actionGet().failure); // test that a create operation fails - AsyncExecutionId executionId2 = new AsyncExecutionId(Long.toString(randomNonNegativeLong()), - new TaskId(randomAlphaOfLength(10), randomNonNegativeLong())); + AsyncExecutionId executionId2 = new AsyncExecutionId( + Long.toString(randomNonNegativeLong()), + new TaskId(randomAlphaOfLength(10), randomNonNegativeLong()) + ); PlainActionFuture createFuture = new PlainActionFuture<>(); TestAsyncResponse initialResponse2 = new TestAsyncResponse(randomAlphaOfLength(130), randomLong()); indexService.createResponse(executionId2.getDocId(), Map.of(), initialResponse2, createFuture); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/async/AsyncTaskServiceTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/async/AsyncTaskServiceTests.java index 00bd76213ef1c..bd955a62c1249 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/async/AsyncTaskServiceTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/async/AsyncTaskServiceTests.java @@ -45,9 +45,16 @@ public void setup() { ClusterService clusterService = getInstanceFromNode(ClusterService.class); TransportService transportService = getInstanceFromNode(TransportService.class); BigArrays bigArrays = getInstanceFromNode(BigArrays.class); - indexService = new AsyncTaskIndexService<>(index, clusterService, + indexService = new AsyncTaskIndexService<>( + index, + clusterService, transportService.getThreadPool().getThreadContext(), - client(), "test_origin", AsyncSearchResponse::new, writableRegistry(), bigArrays); + client(), + "test_origin", + AsyncSearchResponse::new, + writableRegistry(), + bigArrays + ); } @Override @@ -78,10 +85,10 @@ public String getFeatureDescription() { } public void testEnsuredAuthenticatedUserIsSame() throws IOException { - Authentication original = - new Authentication(new User("test", "role"), new Authentication.RealmRef("realm", "file", "node"), null); - Authentication current = randomBoolean() ? original : - new Authentication(new User("test", "role"), new Authentication.RealmRef("realm", "file", "node"), null); + Authentication original = new Authentication(new User("test", "role"), new Authentication.RealmRef("realm", "file", "node"), null); + Authentication current = randomBoolean() + ? original + : new Authentication(new User("test", "role"), new Authentication.RealmRef("realm", "file", "node"), null); assertTrue(original.canAccessResourcesOf(current)); ThreadContext threadContext = new ThreadContext(Settings.EMPTY); original.writeToContext(threadContext); @@ -94,14 +101,20 @@ public void testEnsuredAuthenticatedUserIsSame() throws IOException { // original user being run as User user = new User(new User("test", "role"), new User("authenticated", "runas")); - current = new Authentication(user, new Authentication.RealmRef("realm", "file", "node"), - new Authentication.RealmRef(randomAlphaOfLengthBetween(1, 16), "file", "node")); + current = new Authentication( + user, + new Authentication.RealmRef("realm", "file", "node"), + new Authentication.RealmRef(randomAlphaOfLengthBetween(1, 16), "file", "node") + ); assertTrue(original.canAccessResourcesOf(current)); assertTrue(indexService.ensureAuthenticatedUserIsSame(threadContext.getHeaders(), current)); // both user are run as - current = new Authentication(user, new Authentication.RealmRef("realm", "file", "node"), - new Authentication.RealmRef(randomAlphaOfLengthBetween(1, 16), "file", "node")); + current = new Authentication( + user, + new Authentication.RealmRef("realm", "file", "node"), + new Authentication.RealmRef(randomAlphaOfLengthBetween(1, 16), "file", "node") + ); Authentication runAs = current; assertTrue(runAs.canAccessResourcesOf(current)); threadContext = new ThreadContext(Settings.EMPTY); @@ -109,27 +122,39 @@ public void testEnsuredAuthenticatedUserIsSame() throws IOException { assertTrue(indexService.ensureAuthenticatedUserIsSame(threadContext.getHeaders(), current)); // different authenticated by type - Authentication differentRealmType = - new Authentication(new User("test", "role"), new Authentication.RealmRef("realm", randomAlphaOfLength(5), "node"), null); + Authentication differentRealmType = new Authentication( + new User("test", "role"), + new Authentication.RealmRef("realm", randomAlphaOfLength(5), "node"), + null + ); threadContext = new ThreadContext(Settings.EMPTY); original.writeToContext(threadContext); assertFalse(original.canAccessResourcesOf(differentRealmType)); assertFalse(indexService.ensureAuthenticatedUserIsSame(threadContext.getHeaders(), differentRealmType)); // wrong user - Authentication differentUser = - new Authentication(new User("test2", "role"), new Authentication.RealmRef("realm", "realm", "node"), null); + Authentication differentUser = new Authentication( + new User("test2", "role"), + new Authentication.RealmRef("realm", "realm", "node"), + null + ); assertFalse(original.canAccessResourcesOf(differentUser)); // run as different user - Authentication diffRunAs = new Authentication(new User(new User("test2", "role"), new User("authenticated", "runas")), - new Authentication.RealmRef("realm", "file", "node1"), new Authentication.RealmRef("realm", "file", "node1")); + Authentication diffRunAs = new Authentication( + new User(new User("test2", "role"), new User("authenticated", "runas")), + new Authentication.RealmRef("realm", "file", "node1"), + new Authentication.RealmRef("realm", "file", "node1") + ); assertFalse(original.canAccessResourcesOf(diffRunAs)); assertFalse(indexService.ensureAuthenticatedUserIsSame(threadContext.getHeaders(), diffRunAs)); // run as different looked up by type - Authentication runAsDiffType = new Authentication(user, new Authentication.RealmRef("realm", "file", "node"), - new Authentication.RealmRef(randomAlphaOfLengthBetween(1, 16), randomAlphaOfLengthBetween(5, 12), "node")); + Authentication runAsDiffType = new Authentication( + user, + new Authentication.RealmRef("realm", "file", "node"), + new Authentication.RealmRef(randomAlphaOfLengthBetween(1, 16), randomAlphaOfLengthBetween(5, 12), "node") + ); assertFalse(original.canAccessResourcesOf(runAsDiffType)); assertFalse(indexService.ensureAuthenticatedUserIsSame(threadContext.getHeaders(), runAsDiffType)); } @@ -182,8 +207,7 @@ public void testAutoCreateIndex() throws Exception { } private void assertSettings() { - GetIndexResponse getIndexResponse = client().admin().indices().getIndex( - new GetIndexRequest().indices(index)).actionGet(); + GetIndexResponse getIndexResponse = client().admin().indices().getIndex(new GetIndexRequest().indices(index)).actionGet(); Settings settings = getIndexResponse.getSettings().get(index); Settings expected = AsyncTaskIndexService.settings(); assertEquals(expected, settings.filter(expected::hasValue)); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ccr/action/ShardFollowTaskTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ccr/action/ShardFollowTaskTests.java index 860043dc1a8c6..c252f0d620d91 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ccr/action/ShardFollowTaskTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ccr/action/ShardFollowTaskTests.java @@ -10,9 +10,9 @@ import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.Collections; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/common/IteratingActionListenerTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/common/IteratingActionListenerTests.java index 5d4b4281e2390..8bc85751589a5 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/common/IteratingActionListenerTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/common/IteratingActionListenerTests.java @@ -99,13 +99,14 @@ public void testIterationDoesntAllowThreadContextLeak() { } public void testIterationEmptyList() { - IteratingActionListener listener = new IteratingActionListener<>(ActionListener.wrap(Assert::assertNull, - (e) -> { - logger.error("unexpected exception", e); - fail("exception should not have been thrown"); - }), (listValue, iteratingListener) -> { - fail("consumer should not have been called!!!"); - }, Collections.emptyList(), new ThreadContext(Settings.EMPTY)); + IteratingActionListener listener = new IteratingActionListener<>(ActionListener.wrap(Assert::assertNull, (e) -> { + logger.error("unexpected exception", e); + fail("exception should not have been thrown"); + }), + (listValue, iteratingListener) -> { fail("consumer should not have been called!!!"); }, + Collections.emptyList(), + new ThreadContext(Settings.EMPTY) + ); listener.run(); } @@ -128,12 +129,15 @@ public void testFailure() { }; final AtomicBoolean onFailureCalled = new AtomicBoolean(false); - IteratingActionListener iteratingListener = new IteratingActionListener<>(ActionListener.wrap((object) -> { - fail("onResponse should not have been called, but was called with: " + object); - }, (e) -> { - assertEquals("expected exception", e.getMessage()); - assertTrue(onFailureCalled.compareAndSet(false, true)); - }), consumer, items, new ThreadContext(Settings.EMPTY)); + IteratingActionListener iteratingListener = new IteratingActionListener<>( + ActionListener.wrap((object) -> { fail("onResponse should not have been called, but was called with: " + object); }, (e) -> { + assertEquals("expected exception", e.getMessage()); + assertTrue(onFailureCalled.compareAndSet(false, true)); + }), + consumer, + items, + new ThreadContext(Settings.EMPTY) + ); iteratingListener.run(); // we never really went async, its all chained together so verify this for sanity diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/common/notifications/AbstractAuditMessageTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/common/notifications/AbstractAuditMessageTests.java index 69b58c930874a..6a354f67db283 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/common/notifications/AbstractAuditMessageTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/common/notifications/AbstractAuditMessageTests.java @@ -6,14 +6,14 @@ */ package org.elasticsearch.xpack.core.common.notifications; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; import java.util.Date; @@ -27,8 +27,11 @@ public class AbstractAuditMessageTests extends AbstractXContentTestCase PARSER = - createParser("test_audit_message", TestAuditMessage::new, TEST_ID); + public static final ConstructingObjectParser PARSER = createParser( + "test_audit_message", + TestAuditMessage::new, + TEST_ID + ); TestAuditMessage(String resourceId, String message, Level level, Date timestamp, String nodeName) { super(resourceId, message, level, timestamp, nodeName); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/common/notifications/AbstractAuditorTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/common/notifications/AbstractAuditorTests.java index 682816521c8e4..3017505a63bbd 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/common/notifications/AbstractAuditorTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/common/notifications/AbstractAuditorTests.java @@ -27,16 +27,16 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.collect.ImmutableOpenMap; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.TimeValue; +import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.threadpool.TestThreadPool; +import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.threadpool.TestThreadPool; -import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.xpack.core.ml.notifications.NotificationsIndex; import org.elasticsearch.xpack.core.template.IndexTemplateConfig; import org.junit.After; @@ -96,8 +96,7 @@ public void shutdownThreadPool() { } public void testInfo() throws IOException { - AbstractAuditor auditor = - createTestAuditorWithTemplateInstalled(client); + AbstractAuditor auditor = createTestAuditorWithTemplateInstalled(client); auditor.info("foo", "Here is my info"); verify(client).execute(eq(IndexAction.INSTANCE), indexRequestCaptor.capture(), any()); @@ -108,14 +107,15 @@ public void testInfo() throws IOException { assertThat(auditMessage.getResourceId(), equalTo("foo")); assertThat(auditMessage.getMessage(), equalTo("Here is my info")); assertThat(auditMessage.getLevel(), equalTo(Level.INFO)); - assertThat(auditMessage.getTimestamp().getTime(), - allOf(greaterThanOrEqualTo(startMillis), lessThanOrEqualTo(System.currentTimeMillis()))); + assertThat( + auditMessage.getTimestamp().getTime(), + allOf(greaterThanOrEqualTo(startMillis), lessThanOrEqualTo(System.currentTimeMillis())) + ); assertThat(auditMessage.getNodeName(), equalTo(TEST_NODE_NAME)); } public void testWarning() throws IOException { - AbstractAuditor auditor = - createTestAuditorWithTemplateInstalled(client); + AbstractAuditor auditor = createTestAuditorWithTemplateInstalled(client); auditor.warning("bar", "Here is my warning"); verify(client).execute(eq(IndexAction.INSTANCE), indexRequestCaptor.capture(), any()); @@ -126,14 +126,15 @@ public void testWarning() throws IOException { assertThat(auditMessage.getResourceId(), equalTo("bar")); assertThat(auditMessage.getMessage(), equalTo("Here is my warning")); assertThat(auditMessage.getLevel(), equalTo(Level.WARNING)); - assertThat(auditMessage.getTimestamp().getTime(), - allOf(greaterThanOrEqualTo(startMillis), lessThanOrEqualTo(System.currentTimeMillis()))); + assertThat( + auditMessage.getTimestamp().getTime(), + allOf(greaterThanOrEqualTo(startMillis), lessThanOrEqualTo(System.currentTimeMillis())) + ); assertThat(auditMessage.getNodeName(), equalTo(TEST_NODE_NAME)); } public void testError() throws IOException { - AbstractAuditor auditor = - createTestAuditorWithTemplateInstalled(client); + AbstractAuditor auditor = createTestAuditorWithTemplateInstalled(client); auditor.error("foobar", "Here is my error"); verify(client).execute(eq(IndexAction.INSTANCE), indexRequestCaptor.capture(), any()); @@ -144,15 +145,19 @@ public void testError() throws IOException { assertThat(auditMessage.getResourceId(), equalTo("foobar")); assertThat(auditMessage.getMessage(), equalTo("Here is my error")); assertThat(auditMessage.getLevel(), equalTo(Level.ERROR)); - assertThat(auditMessage.getTimestamp().getTime(), - allOf(greaterThanOrEqualTo(startMillis), lessThanOrEqualTo(System.currentTimeMillis()))); + assertThat( + auditMessage.getTimestamp().getTime(), + allOf(greaterThanOrEqualTo(startMillis), lessThanOrEqualTo(System.currentTimeMillis())) + ); assertThat(auditMessage.getNodeName(), equalTo(TEST_NODE_NAME)); } public void testAuditingBeforeTemplateInstalled() throws Exception { CountDownLatch writeSomeDocsBeforeTemplateLatch = new CountDownLatch(1); - AbstractAuditor auditor = - createTestAuditorWithoutTemplate(client, writeSomeDocsBeforeTemplateLatch); + AbstractAuditor auditor = createTestAuditorWithoutTemplate( + client, + writeSomeDocsBeforeTemplateLatch + ); auditor.error("foobar", "Here is my error to queue"); auditor.warning("foobar", "Here is my warning to queue"); @@ -164,9 +169,7 @@ public void testAuditingBeforeTemplateInstalled() throws Exception { // the back log will be written some point later ArgumentCaptor bulkCaptor = ArgumentCaptor.forClass(BulkRequest.class); - assertBusy(() -> - verify(client, times(1)).execute(eq(BulkAction.INSTANCE), bulkCaptor.capture(), any()) - ); + assertBusy(() -> verify(client, times(1)).execute(eq(BulkAction.INSTANCE), bulkCaptor.capture(), any())); BulkRequest bulkRequest = bulkCaptor.getValue(); assertThat(bulkRequest.numberOfActions(), equalTo(3)); @@ -177,13 +180,15 @@ public void testAuditingBeforeTemplateInstalled() throws Exception { public void testMaxBufferSize() throws Exception { CountDownLatch writeSomeDocsBeforeTemplateLatch = new CountDownLatch(1); - AbstractAuditor auditor = - createTestAuditorWithoutTemplate(client, writeSomeDocsBeforeTemplateLatch); + AbstractAuditor auditor = createTestAuditorWithoutTemplate( + client, + writeSomeDocsBeforeTemplateLatch + ); int numThreads = 2; int numMessagesToWrite = (AbstractAuditor.MAX_BUFFER_SIZE / numThreads) + 10; Runnable messageWrites = () -> { - for (int i=0; i { - ActionListener listener = - (ActionListener)invocationOnMock.getArguments()[2]; + ActionListener listener = (ActionListener) invocationOnMock.getArguments()[2]; Runnable onPutTemplate = () -> { try { @@ -268,12 +272,25 @@ private TestAuditor createTestAuditorWithoutTemplate(Client client, CountDownLat public static class TestAuditor extends AbstractAuditor { TestAuditor(Client client, String nodeName, ClusterService clusterService) { - super(new OriginSettingClient(client, TEST_ORIGIN), TEST_INDEX, - new IndexTemplateConfig(TEST_INDEX, - "/org/elasticsearch/xpack/core/ml/notifications_index_template.json", Version.CURRENT.id, "xpack.ml.version", - Map.of("xpack.ml.version.id", String.valueOf(Version.CURRENT.id), - "xpack.ml.notifications.mappings", NotificationsIndex.mapping())), - nodeName, AbstractAuditMessageTests.TestAuditMessage::new, clusterService); + super( + new OriginSettingClient(client, TEST_ORIGIN), + TEST_INDEX, + new IndexTemplateConfig( + TEST_INDEX, + "/org/elasticsearch/xpack/core/ml/notifications_index_template.json", + Version.CURRENT.id, + "xpack.ml.version", + Map.of( + "xpack.ml.version.id", + String.valueOf(Version.CURRENT.id), + "xpack.ml.notifications.mappings", + NotificationsIndex.mapping() + ) + ), + nodeName, + AbstractAuditMessageTests.TestAuditMessage::new, + clusterService + ); } } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/common/stats/EnumCountersTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/common/stats/EnumCountersTests.java index 214a6e345c4dc..f3304467dad64 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/common/stats/EnumCountersTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/common/stats/EnumCountersTests.java @@ -21,9 +21,18 @@ public class EnumCountersTests extends AbstractWireTestCase> { - enum TestV1 {A, B, C} + enum TestV1 { + A, + B, + C + } - enum TestV2 {A, B, C, D} + enum TestV2 { + A, + B, + C, + D + } @Override protected EnumCounters createTestInstance() { @@ -70,7 +79,6 @@ public void testBackwardCompatibility() throws Exception { assertEquals(counters.get(TestV2.C), oldCounters.get(TestV1.C)); } - public void testForwardCompatibility() throws Exception { EnumCounters counters = new EnumCounters<>(TestV1.class); counters.inc(TestV1.A, 1); @@ -84,7 +92,9 @@ public void testForwardCompatibility() throws Exception { } private , E2 extends Enum> EnumCounters serialize( - EnumCounters source, Writeable.Reader> targetReader) throws IOException { + EnumCounters source, + Writeable.Reader> targetReader + ) throws IOException { try (BytesStreamOutput output = new BytesStreamOutput()) { source.writeTo(output); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/common/time/TimeUtilsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/common/time/TimeUtilsTests.java index da6ae8fc7b150..b80e93166050d 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/common/time/TimeUtilsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/common/time/TimeUtilsTests.java @@ -6,11 +6,11 @@ */ package org.elasticsearch.xpack.core.common.time; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.ESTestCase; import java.io.IOException; import java.time.Instant; @@ -89,23 +89,31 @@ public void testCheckMultiple_GivenMultiples() { } public void testCheckMultiple_GivenNonMultiple() { - expectThrows(IllegalArgumentException.class, () -> - TimeUtils.checkMultiple(TimeValue.timeValueMillis(500), TimeUnit.SECONDS, new ParseField("foo"))); + expectThrows( + IllegalArgumentException.class, + () -> TimeUtils.checkMultiple(TimeValue.timeValueMillis(500), TimeUnit.SECONDS, new ParseField("foo")) + ); } public void testCheckPositiveMultiple_GivenNegative() { - expectThrows(IllegalArgumentException.class, () -> - TimeUtils.checkPositiveMultiple(TimeValue.timeValueMillis(-1), TimeUnit.MILLISECONDS, new ParseField("foo"))); + expectThrows( + IllegalArgumentException.class, + () -> TimeUtils.checkPositiveMultiple(TimeValue.timeValueMillis(-1), TimeUnit.MILLISECONDS, new ParseField("foo")) + ); } public void testCheckPositiveMultiple_GivenZero() { - expectThrows(IllegalArgumentException.class, () -> - TimeUtils.checkPositiveMultiple(TimeValue.ZERO, TimeUnit.SECONDS, new ParseField("foo"))); + expectThrows( + IllegalArgumentException.class, + () -> TimeUtils.checkPositiveMultiple(TimeValue.ZERO, TimeUnit.SECONDS, new ParseField("foo")) + ); } public void testCheckPositiveMultiple_GivenPositiveNonMultiple() { - expectThrows(IllegalArgumentException.class, () -> - TimeUtils.checkPositiveMultiple(TimeValue.timeValueMillis(500), TimeUnit.SECONDS, new ParseField("foo"))); + expectThrows( + IllegalArgumentException.class, + () -> TimeUtils.checkPositiveMultiple(TimeValue.timeValueMillis(500), TimeUnit.SECONDS, new ParseField("foo")) + ); } public void testCheckPositiveMultiple_GivenPositiveMultiple() { @@ -113,8 +121,10 @@ public void testCheckPositiveMultiple_GivenPositiveMultiple() { } public void testCheckNonNegativeMultiple_GivenNegative() { - expectThrows(IllegalArgumentException.class, () -> - TimeUtils.checkNonNegativeMultiple(TimeValue.timeValueMillis(-1), TimeUnit.MILLISECONDS, new ParseField("foo"))); + expectThrows( + IllegalArgumentException.class, + () -> TimeUtils.checkNonNegativeMultiple(TimeValue.timeValueMillis(-1), TimeUnit.MILLISECONDS, new ParseField("foo")) + ); } public void testCheckNonNegativeMultiple_GivenZero() { @@ -122,8 +132,10 @@ public void testCheckNonNegativeMultiple_GivenZero() { } public void testCheckNonNegativeMultiple_GivenPositiveNonMultiple() { - expectThrows(IllegalArgumentException.class, () -> - TimeUtils.checkNonNegativeMultiple(TimeValue.timeValueMillis(500), TimeUnit.SECONDS, new ParseField("foo"))); + expectThrows( + IllegalArgumentException.class, + () -> TimeUtils.checkNonNegativeMultiple(TimeValue.timeValueMillis(500), TimeUnit.SECONDS, new ParseField("foo")) + ); } public void testCheckNonNegativeMultiple_GivenPositiveMultiple() { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/common/validation/RemoteClusterMinimumVersionValidationTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/common/validation/RemoteClusterMinimumVersionValidationTests.java index 846d0186b42e1..9272d210010c6 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/common/validation/RemoteClusterMinimumVersionValidationTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/common/validation/RemoteClusterMinimumVersionValidationTests.java @@ -57,9 +57,8 @@ public void testValidate_NoRemoteClusters() { SourceDestValidation validation = new RemoteClusterMinimumVersionValidation(MIN_EXPECTED_VERSION, REASON); validation.validate( context, - ActionListener.wrap( - ctx -> assertThat(ctx.getValidationException(), is(nullValue())), - e -> fail(e.getMessage()))); + ActionListener.wrap(ctx -> assertThat(ctx.getValidationException(), is(nullValue())), e -> fail(e.getMessage())) + ); } public void testValidate_RemoteClustersVersionsOk() { @@ -68,9 +67,8 @@ public void testValidate_RemoteClustersVersionsOk() { SourceDestValidation validation = new RemoteClusterMinimumVersionValidation(MIN_EXPECTED_VERSION, REASON); validation.validate( context, - ActionListener.wrap( - ctx -> assertThat(ctx.getValidationException(), is(nullValue())), - e -> fail(e.getMessage()))); + ActionListener.wrap(ctx -> assertThat(ctx.getValidationException(), is(nullValue())), e -> fail(e.getMessage())) + ); } public void testValidate_OneRemoteClusterVersionTooLow() { @@ -82,9 +80,14 @@ public void testValidate_OneRemoteClusterVersionTooLow() { ActionListener.wrap( ctx -> assertThat( ctx.getValidationException().validationErrors(), - contains("remote clusters are expected to run at least version [7.11.0] (reason: [some reason]), " - + "but the following clusters were too old: [cluster-A (7.10.2)]")), - e -> fail(e.getMessage()))); + contains( + "remote clusters are expected to run at least version [7.11.0] (reason: [some reason]), " + + "but the following clusters were too old: [cluster-A (7.10.2)]" + ) + ), + e -> fail(e.getMessage()) + ) + ); } public void testValidate_TwoRemoteClusterVersionsTooLow() { @@ -96,9 +99,14 @@ public void testValidate_TwoRemoteClusterVersionsTooLow() { ActionListener.wrap( ctx -> assertThat( ctx.getValidationException().validationErrors(), - contains("remote clusters are expected to run at least version [7.11.2] (reason: [some reason]), " - + "but the following clusters were too old: [cluster-A (7.10.2), cluster-B (7.11.0)]")), - e -> fail(e.getMessage()))); + contains( + "remote clusters are expected to run at least version [7.11.2] (reason: [some reason]), " + + "but the following clusters were too old: [cluster-A (7.10.2), cluster-B (7.11.0)]" + ) + ), + e -> fail(e.getMessage()) + ) + ); } public void testValidate_NoSuchRemoteCluster() { @@ -110,7 +118,9 @@ public void testValidate_NoSuchRemoteCluster() { context, ActionListener.wrap( ctx -> assertThat(ctx.getValidationException().validationErrors(), contains("no such remote cluster: [cluster-D]")), - e -> fail(e.getMessage()))); + e -> fail(e.getMessage()) + ) + ); } public void testValidate_OtherProblem() { @@ -123,7 +133,10 @@ public void testValidate_OtherProblem() { ActionListener.wrap( ctx -> assertThat( ctx.getValidationException().validationErrors(), - contains("Error resolving remote source: some-other-problem")), - e -> fail(e.getMessage()))); + contains("Error resolving remote source: some-other-problem") + ), + e -> fail(e.getMessage()) + ) + ); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/common/validation/SourceDestValidatorTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/common/validation/SourceDestValidatorTests.java index 8486a1201bae8..4f4a29ad7cb80 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/common/validation/SourceDestValidatorTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/common/validation/SourceDestValidatorTests.java @@ -20,9 +20,9 @@ import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; import org.elasticsearch.cluster.metadata.Metadata; -import org.elasticsearch.core.CheckedConsumer; import org.elasticsearch.common.ValidationException; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.CheckedConsumer; import org.elasticsearch.indices.TestIndexNameExpressionResolver; import org.elasticsearch.ingest.ConfigurationUtils; import org.elasticsearch.ingest.IngestService; @@ -62,7 +62,6 @@ import static org.elasticsearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_REPLICAS; import static org.elasticsearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_SHARDS; import static org.elasticsearch.cluster.metadata.IndexMetadata.SETTING_VERSION_CREATED; -import static org.mockito.Mockito.when; import static org.elasticsearch.xpack.core.common.validation.SourceDestValidator.DESTINATION_IN_SOURCE_VALIDATION; import static org.elasticsearch.xpack.core.common.validation.SourceDestValidator.DESTINATION_PIPELINE_MISSING_VALIDATION; import static org.elasticsearch.xpack.core.common.validation.SourceDestValidator.DESTINATION_SINGLE_INDEX_VALIDATION; @@ -70,6 +69,7 @@ import static org.hamcrest.Matchers.equalTo; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; public class SourceDestValidatorTests extends ESTestCase { @@ -105,8 +105,9 @@ public class SourceDestValidatorTests extends ESTestCase { private final TransportService transportService = MockTransportService.createNewService(Settings.EMPTY, Version.CURRENT, threadPool); private final RemoteClusterService remoteClusterService = transportService.getRemoteClusterService(); private final IngestService ingestService = mock(IngestService.class); - private final IndexNameExpressionResolver indexNameExpressionResolver = - TestIndexNameExpressionResolver.newInstance(threadPool.getThreadContext()); + private final IndexNameExpressionResolver indexNameExpressionResolver = TestIndexNameExpressionResolver.newInstance( + threadPool.getThreadContext() + ); private final SourceDestValidator simpleNonRemoteValidator = new SourceDestValidator( indexNameExpressionResolver, @@ -234,14 +235,7 @@ public void testValidate_GivenSimpleSourceIndexAndValidDestIndex() throws Interr public void testCheck_GivenNoSourceIndexAndValidDestIndex() throws InterruptedException { assertValidation( - listener -> simpleNonRemoteValidator.validate( - CLUSTER_STATE, - new String[] {}, - "dest", - null, - TEST_VALIDATIONS, - listener - ), + listener -> simpleNonRemoteValidator.validate(CLUSTER_STATE, new String[] {}, "dest", null, TEST_VALIDATIONS, listener), (Boolean) null, e -> { assertEquals(1, e.validationErrors().size()); @@ -597,10 +591,7 @@ public void testCheck_GivenMissingDestPipeline() throws Exception { (Boolean) null, e -> { assertEquals(1, e.validationErrors().size()); - assertThat( - e.validationErrors().get(0), - equalTo("Pipeline with id [missing-pipeline] could not be found") - ); + assertThat(e.validationErrors().get(0), equalTo("Pipeline with id [missing-pipeline] could not be found")); } ); @@ -611,8 +602,10 @@ public void testCheck_GivenMissingDestPipeline() throws Exception { Map pipelineConfig = new HashMap<>(); pipelineConfig.put(Pipeline.DESCRIPTION_KEY, "_description"); pipelineConfig.put(Pipeline.VERSION_KEY, "1"); - pipelineConfig.put(Pipeline.PROCESSORS_KEY, - Arrays.asList(Collections.singletonMap("test", processorConfig0), Collections.singletonMap("test", processorConfig1))); + pipelineConfig.put( + Pipeline.PROCESSORS_KEY, + Arrays.asList(Collections.singletonMap("test", processorConfig0), Collections.singletonMap("test", processorConfig1)) + ); Map processorRegistry = Collections.singletonMap("test", new TestProcessor.Factory()); Pipeline pipeline = Pipeline.create("missing-pipeline", pipelineConfig, processorRegistry, null); when(ingestService.getPipeline("missing-pipeline")).thenReturn(pipeline); @@ -687,8 +680,10 @@ public void testRemoteSourcePlatinum() throws InterruptedException { CLUSTER_STATE, indexNameExpressionResolver, remoteClusterService, - new RemoteClusterLicenseChecker(clientWithBasicLicense, - operationMode -> XPackLicenseState.isAllowedByOperationMode(operationMode, License.OperationMode.PLATINUM)), + new RemoteClusterLicenseChecker( + clientWithBasicLicense, + operationMode -> XPackLicenseState.isAllowedByOperationMode(operationMode, License.OperationMode.PLATINUM) + ), ingestService, new String[] { REMOTE_BASIC + ":" + "SOURCE_1" }, "dest", @@ -719,8 +714,10 @@ public void testRemoteSourcePlatinum() throws InterruptedException { CLUSTER_STATE, indexNameExpressionResolver, remoteClusterService, - new RemoteClusterLicenseChecker(clientWithPlatinumLicense, - operationMode -> XPackLicenseState.isAllowedByOperationMode(operationMode, License.OperationMode.PLATINUM)), + new RemoteClusterLicenseChecker( + clientWithPlatinumLicense, + operationMode -> XPackLicenseState.isAllowedByOperationMode(operationMode, License.OperationMode.PLATINUM) + ), ingestService, new String[] { REMOTE_PLATINUM + ":" + "SOURCE_1" }, "dest", @@ -742,8 +739,10 @@ public void testRemoteSourcePlatinum() throws InterruptedException { CLUSTER_STATE, indexNameExpressionResolver, remoteClusterService, - new RemoteClusterLicenseChecker(clientWithPlatinumLicense, - operationMode -> XPackLicenseState.isAllowedByOperationMode(operationMode, License.OperationMode.PLATINUM)), + new RemoteClusterLicenseChecker( + clientWithPlatinumLicense, + operationMode -> XPackLicenseState.isAllowedByOperationMode(operationMode, License.OperationMode.PLATINUM) + ), ingestService, new String[] { REMOTE_PLATINUM + ":" + "SOURCE_1" }, "dest", @@ -766,8 +765,10 @@ public void testRemoteSourcePlatinum() throws InterruptedException { CLUSTER_STATE, indexNameExpressionResolver, remoteClusterService, - new RemoteClusterLicenseChecker(clientWithTrialLicense, - operationMode -> XPackLicenseState.isAllowedByOperationMode(operationMode, License.OperationMode.PLATINUM)), + new RemoteClusterLicenseChecker( + clientWithTrialLicense, + operationMode -> XPackLicenseState.isAllowedByOperationMode(operationMode, License.OperationMode.PLATINUM) + ), ingestService, new String[] { REMOTE_PLATINUM + ":" + "SOURCE_1" }, "dest", @@ -792,8 +793,10 @@ public void testRemoteSourceLicenseInActive() throws InterruptedException { CLUSTER_STATE, indexNameExpressionResolver, remoteClusterService, - new RemoteClusterLicenseChecker(clientWithExpiredBasicLicense, - operationMode -> XPackLicenseState.isAllowedByOperationMode(operationMode, License.OperationMode.PLATINUM)), + new RemoteClusterLicenseChecker( + clientWithExpiredBasicLicense, + operationMode -> XPackLicenseState.isAllowedByOperationMode(operationMode, License.OperationMode.PLATINUM) + ), ingestService, new String[] { REMOTE_BASIC + ":" + "SOURCE_1" }, "dest", @@ -821,8 +824,10 @@ public void testRemoteSourceDoesNotExist() throws InterruptedException { CLUSTER_STATE, indexNameExpressionResolver, remoteClusterService, - new RemoteClusterLicenseChecker(clientWithExpiredBasicLicense, - operationMode -> XPackLicenseState.isAllowedByOperationMode(operationMode, License.OperationMode.PLATINUM)), + new RemoteClusterLicenseChecker( + clientWithExpiredBasicLicense, + operationMode -> XPackLicenseState.isAllowedByOperationMode(operationMode, License.OperationMode.PLATINUM) + ), ingestService, new String[] { "non_existing_remote:" + "SOURCE_1" }, "dest", diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/deprecation/DeprecationIssueTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/deprecation/DeprecationIssueTests.java index 4289d6230d70d..2cf6fd20e81db 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/deprecation/DeprecationIssueTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/deprecation/DeprecationIssueTests.java @@ -9,11 +9,11 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.StreamInput; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.core.Tuple; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xpack.core.deprecation.DeprecationIssue.Level; import org.junit.Before; @@ -29,9 +29,14 @@ public class DeprecationIssueTests extends ESTestCase { static DeprecationIssue createTestInstance() { String details = randomBoolean() ? randomAlphaOfLength(10) : null; - return new DeprecationIssue(randomFrom(Level.values()), randomAlphaOfLength(10), - randomAlphaOfLength(10), details, randomBoolean(), - randomMap(1, 5, () -> Tuple.tuple(randomAlphaOfLength(4), randomAlphaOfLength(4)))); + return new DeprecationIssue( + randomFrom(Level.values()), + randomAlphaOfLength(10), + randomAlphaOfLength(10), + details, + randomBoolean(), + randomMap(1, 5, () -> Tuple.tuple(randomAlphaOfLength(4), randomAlphaOfLength(4))) + ); } @Before @@ -40,8 +45,14 @@ public void setup() { } public void testEqualsAndHashCode() { - DeprecationIssue other = new DeprecationIssue(issue.getLevel(), issue.getMessage(), issue.getUrl(), issue.getDetails(), - issue.isResolveDuringRollingUpgrade(), issue.getMeta()); + DeprecationIssue other = new DeprecationIssue( + issue.getLevel(), + issue.getMessage(), + issue.getUrl(), + issue.getDetails(), + issue.isResolveDuringRollingUpgrade(), + issue.getMeta() + ); assertThat(issue, equalTo(other)); assertThat(other, equalTo(issue)); assertThat(issue.hashCode(), equalTo(other.hashCode())); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/AbstractStepTestCase.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/AbstractStepTestCase.java index c35f2d070f90b..4dde09971ce85 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/AbstractStepTestCase.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/AbstractStepTestCase.java @@ -42,7 +42,9 @@ public void setupClient() { protected static final TimeValue MASTER_TIMEOUT = TimeValue.timeValueSeconds(30); protected abstract T createRandomInstance(); + protected abstract T mutateInstance(T instance); + protected abstract T copyInstance(T instance); public void testHashcodeAndEquals() { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ActionConfigStatsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ActionConfigStatsTests.java index 0f5b76297ec2f..4f64155ac8e1e 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ActionConfigStatsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ActionConfigStatsTests.java @@ -38,18 +38,18 @@ public static ActionConfigStats createRandomInstance() { builder.setRolloverMaxDocs(randomLongBetween(0, Long.MAX_VALUE)); } if (randomBoolean()) { - ByteSizeValue randomByteSize = ByteSizeValue.ofBytes(randomLongBetween(0, 1024L*1024L*1024L*50L)); + ByteSizeValue randomByteSize = ByteSizeValue.ofBytes(randomLongBetween(0, 1024L * 1024L * 1024L * 50L)); builder.setRolloverMaxPrimaryShardSize(randomByteSize); } if (randomBoolean()) { - ByteSizeValue randomByteSize = ByteSizeValue.ofBytes(randomLongBetween(0, 1024L*1024L*1024L*50L)); + ByteSizeValue randomByteSize = ByteSizeValue.ofBytes(randomLongBetween(0, 1024L * 1024L * 1024L * 50L)); builder.setRolloverMaxSize(randomByteSize); } if (randomBoolean()) { builder.setPriority(randomIntBetween(0, 50)); } if (randomBoolean()) { - ByteSizeValue randomByteSize = ByteSizeValue.ofBytes(randomLongBetween(0, 1024L*1024L*1024L*50L)); + ByteSizeValue randomByteSize = ByteSizeValue.ofBytes(randomLongBetween(0, 1024L * 1024L * 1024L * 50L)); builder.setShrinkMaxPrimaryShardSize(randomByteSize); } if (randomBoolean()) { @@ -76,26 +76,28 @@ protected ActionConfigStats mutateInstance(ActionConfigStats instance) throws IO builder.setForceMergeMaxNumberOfSegments(numberOfSegments); break; case 2: - TimeValue randomAge = randomValueOtherThan(instance.getRolloverMaxAge(), - () -> TimeValue.parseTimeValue(randomTimeValue(), "action_config_stats_tests")); + TimeValue randomAge = randomValueOtherThan( + instance.getRolloverMaxAge(), + () -> TimeValue.parseTimeValue(randomTimeValue(), "action_config_stats_tests") + ); builder.setRolloverMaxAge(randomAge); break; case 3: builder.setRolloverMaxDocs(randomLongBetween(0, Long.MAX_VALUE)); break; case 4: - ByteSizeValue randomByteSize = ByteSizeValue.ofBytes(randomLongBetween(0, 1024L*1024L*1024L*50L)); + ByteSizeValue randomByteSize = ByteSizeValue.ofBytes(randomLongBetween(0, 1024L * 1024L * 1024L * 50L)); builder.setRolloverMaxPrimaryShardSize(randomByteSize); break; case 5: - ByteSizeValue randomMaxByteSize = ByteSizeValue.ofBytes(randomLongBetween(0, 1024L*1024L*1024L*50L)); + ByteSizeValue randomMaxByteSize = ByteSizeValue.ofBytes(randomLongBetween(0, 1024L * 1024L * 1024L * 50L)); builder.setRolloverMaxSize(randomMaxByteSize); break; case 6: builder.setPriority(randomValueOtherThan(instance.getSetPriorityPriority(), () -> randomIntBetween(0, 50))); break; case 7: - ByteSizeValue randomPrimaryByteSize = ByteSizeValue.ofBytes(randomLongBetween(0, 1024L*1024L*1024L*50L)); + ByteSizeValue randomPrimaryByteSize = ByteSizeValue.ofBytes(randomLongBetween(0, 1024L * 1024L * 1024L * 50L)); builder.setShrinkMaxPrimaryShardSize(randomPrimaryByteSize); break; case 8: diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/AllocateActionTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/AllocateActionTests.java index 222b30ca9d557..6cd891aa129d5 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/AllocateActionTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/AllocateActionTests.java @@ -61,7 +61,6 @@ static AllocateAction randomInstance() { return new AllocateAction(numberOfReplicas, totalShardsPerNode, includes, excludes, requires); } - @Override protected Reader instanceReader() { return AllocateAction::new; @@ -75,23 +74,23 @@ protected AllocateAction mutateInstance(AllocateAction instance) { Integer numberOfReplicas = instance.getNumberOfReplicas(); Integer totalShardsPerNode = instance.getTotalShardsPerNode(); switch (randomIntBetween(0, 3)) { - case 0: - include = new HashMap<>(include); - include.put(randomAlphaOfLengthBetween(11, 15), randomAlphaOfLengthBetween(1, 20)); - break; - case 1: - exclude = new HashMap<>(exclude); - exclude.put(randomAlphaOfLengthBetween(11, 15), randomAlphaOfLengthBetween(1, 20)); - break; - case 2: - require = new HashMap<>(require); - require.put(randomAlphaOfLengthBetween(11, 15), randomAlphaOfLengthBetween(1, 20)); - break; - case 3: - numberOfReplicas = randomIntBetween(11, 20); - break; - default: - throw new AssertionError("Illegal randomisation branch"); + case 0: + include = new HashMap<>(include); + include.put(randomAlphaOfLengthBetween(11, 15), randomAlphaOfLengthBetween(1, 20)); + break; + case 1: + exclude = new HashMap<>(exclude); + exclude.put(randomAlphaOfLengthBetween(11, 15), randomAlphaOfLengthBetween(1, 20)); + break; + case 2: + require = new HashMap<>(require); + require.put(randomAlphaOfLengthBetween(11, 15), randomAlphaOfLengthBetween(1, 20)); + break; + case 3: + numberOfReplicas = randomIntBetween(11, 20); + break; + default: + throw new AssertionError("Illegal randomisation branch"); } return new AllocateAction(numberOfReplicas, totalShardsPerNode, include, exclude, require); } @@ -100,19 +99,31 @@ public void testAllMapsNullOrEmpty() { Map include = randomBoolean() ? null : Collections.emptyMap(); Map exclude = randomBoolean() ? null : Collections.emptyMap(); Map require = randomBoolean() ? null : Collections.emptyMap(); - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, - () -> new AllocateAction(null, null, include, exclude, require)); - assertEquals("At least one of " + AllocateAction.INCLUDE_FIELD.getPreferredName() + ", " - + AllocateAction.EXCLUDE_FIELD.getPreferredName() + " or " + AllocateAction.REQUIRE_FIELD.getPreferredName() - + "must contain attributes for action " + AllocateAction.NAME, exception.getMessage()); + IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> new AllocateAction(null, null, include, exclude, require) + ); + assertEquals( + "At least one of " + + AllocateAction.INCLUDE_FIELD.getPreferredName() + + ", " + + AllocateAction.EXCLUDE_FIELD.getPreferredName() + + " or " + + AllocateAction.REQUIRE_FIELD.getPreferredName() + + "must contain attributes for action " + + AllocateAction.NAME, + exception.getMessage() + ); } public void testInvalidNumberOfReplicas() { Map include = randomAllocationRoutingMap(1, 5); Map exclude = randomBoolean() ? null : Collections.emptyMap(); Map require = randomBoolean() ? null : Collections.emptyMap(); - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, - () -> new AllocateAction(randomIntBetween(-1000, -1), randomIntBetween(0, 300), include, exclude, require)); + IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> new AllocateAction(randomIntBetween(-1000, -1), randomIntBetween(0, 300), include, exclude, require) + ); assertEquals("[" + AllocateAction.NUMBER_OF_REPLICAS_FIELD.getPreferredName() + "] must be >= 0", exception.getMessage()); } @@ -120,8 +131,10 @@ public void testInvalidTotalShardsPerNode() { Map include = randomAllocationRoutingMap(1, 5); Map exclude = randomBoolean() ? null : Collections.emptyMap(); Map require = randomBoolean() ? null : Collections.emptyMap(); - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, - () -> new AllocateAction(randomIntBetween(0, 300), randomIntBetween(-1000, -2), include, exclude, require)); + IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> new AllocateAction(randomIntBetween(0, 300), randomIntBetween(-1000, -2), include, exclude, require) + ); assertEquals("[" + AllocateAction.TOTAL_SHARDS_PER_NODE_FIELD.getPreferredName() + "] must be >= -1", exception.getMessage()); } @@ -129,8 +142,10 @@ public static Map randomAllocationRoutingMap(int minEntries, int Map map = new HashMap<>(); int numIncludes = randomIntBetween(minEntries, maxEntries); for (int i = 0; i < numIncludes; i++) { - String attributeName = randomValueOtherThanMany(DiscoveryNodeRole.roleNames()::contains, - () -> randomAlphaOfLengthBetween(2, 20)); + String attributeName = randomValueOtherThanMany( + DiscoveryNodeRole.roleNames()::contains, + () -> randomAlphaOfLengthBetween(2, 20) + ); map.put(attributeName, randomAlphaOfLengthBetween(2, 20)); } return map; @@ -139,8 +154,11 @@ public static Map randomAllocationRoutingMap(int minEntries, int public void testToSteps() { AllocateAction action = createTestInstance(); String phase = randomAlphaOfLengthBetween(1, 10); - StepKey nextStepKey = new StepKey(randomAlphaOfLengthBetween(1, 10), randomAlphaOfLengthBetween(1, 10), - randomAlphaOfLengthBetween(1, 10)); + StepKey nextStepKey = new StepKey( + randomAlphaOfLengthBetween(1, 10), + randomAlphaOfLengthBetween(1, 10), + randomAlphaOfLengthBetween(1, 10) + ); List steps = action.toSteps(null, phase, nextStepKey); assertNotNull(steps); assertEquals(2, steps.size()); @@ -153,12 +171,12 @@ public void testToSteps() { if (action.getNumberOfReplicas() != null) { expectedSettings.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, action.getNumberOfReplicas()); } - action.getInclude().forEach( - (key, value) -> expectedSettings.put(IndexMetadata.INDEX_ROUTING_INCLUDE_GROUP_SETTING.getKey() + key, value)); - action.getExclude().forEach( - (key, value) -> expectedSettings.put(IndexMetadata.INDEX_ROUTING_EXCLUDE_GROUP_SETTING.getKey() + key, value)); - action.getRequire().forEach( - (key, value) -> expectedSettings.put(IndexMetadata.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + key, value)); + action.getInclude() + .forEach((key, value) -> expectedSettings.put(IndexMetadata.INDEX_ROUTING_INCLUDE_GROUP_SETTING.getKey() + key, value)); + action.getExclude() + .forEach((key, value) -> expectedSettings.put(IndexMetadata.INDEX_ROUTING_EXCLUDE_GROUP_SETTING.getKey() + key, value)); + action.getRequire() + .forEach((key, value) -> expectedSettings.put(IndexMetadata.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + key, value)); if (action.getTotalShardsPerNode() != null) { expectedSettings.put(ShardsLimitAllocationDecider.INDEX_TOTAL_SHARDS_PER_NODE_SETTING.getKey(), action.getTotalShardsPerNode()); } @@ -174,8 +192,11 @@ public void testTotalNumberOfShards() throws Exception { Integer numberOfReplicas = randomIntBetween(0, 4); AllocateAction action = new AllocateAction(numberOfReplicas, totalShardsPerNode, null, null, null); String phase = randomAlphaOfLengthBetween(1, 10); - StepKey nextStepKey = new StepKey(randomAlphaOfLengthBetween(1, 10), randomAlphaOfLengthBetween(1, 10), - randomAlphaOfLengthBetween(1, 10)); + StepKey nextStepKey = new StepKey( + randomAlphaOfLengthBetween(1, 10), + randomAlphaOfLengthBetween(1, 10), + randomAlphaOfLengthBetween(1, 10) + ); List steps = action.toSteps(null, phase, nextStepKey); UpdateSettingsStep firstStep = (UpdateSettingsStep) steps.get(0); assertEquals(totalShardsPerNode, firstStep.getSettings().getAsInt(INDEX_TOTAL_SHARDS_PER_NODE_SETTING.getKey(), null)); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/AllocationRoutedStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/AllocationRoutedStepTests.java index 4cc2a329854b1..f1ad1884e4e5d 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/AllocationRoutedStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/AllocationRoutedStepTests.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.xpack.core.ilm; - import org.elasticsearch.Version; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.IndexMetadata; @@ -51,14 +50,14 @@ public AllocationRoutedStep mutateInstance(AllocationRoutedStep instance) { StepKey nextKey = instance.getNextStepKey(); switch (between(0, 1)) { - case 0: - key = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); - break; - case 1: - nextKey = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); - break; - default: - throw new AssertionError("Illegal randomisation branch"); + case 0: + key = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); + break; + case 1: + nextKey = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); + break; + default: + throw new AssertionError("Illegal randomisation branch"); } return new AllocationRoutedStep(key, nextKey); @@ -74,36 +73,49 @@ public void testConditionMet() { Map includes = AllocateActionTests.randomAllocationRoutingMap(1, 5); Map excludes = AllocateActionTests.randomAllocationRoutingMap(1, 5); Map requires = AllocateActionTests.randomAllocationRoutingMap(1, 5); - Settings.Builder existingSettings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT.id) - .put(IndexMetadata.SETTING_INDEX_UUID, index.getUUID()); + Settings.Builder existingSettings = Settings.builder() + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT.id) + .put(IndexMetadata.SETTING_INDEX_UUID, index.getUUID()); Settings.Builder node1Settings = Settings.builder(); Settings.Builder node2Settings = Settings.builder(); includes.forEach((k, v) -> { existingSettings.put(IndexMetadata.INDEX_ROUTING_INCLUDE_GROUP_SETTING.getKey() + k, v); node1Settings.put(Node.NODE_ATTRIBUTES.getKey() + k, v); }); - excludes.forEach((k, v) -> { - existingSettings.put(IndexMetadata.INDEX_ROUTING_EXCLUDE_GROUP_SETTING.getKey() + k, v); - }); + excludes.forEach((k, v) -> { existingSettings.put(IndexMetadata.INDEX_ROUTING_EXCLUDE_GROUP_SETTING.getKey() + k, v); }); requires.forEach((k, v) -> { existingSettings.put(IndexMetadata.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + k, v); node1Settings.put(Node.NODE_ATTRIBUTES.getKey() + k, v); }); IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index) - .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node1", true, ShardRoutingState.STARTED)); - - logger.info("running test with routing configurations:\n\t includes: [{}]\n\t excludes: [{}]\n\t requires: [{}]", - includes, excludes, requires); + .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node1", true, ShardRoutingState.STARTED)); + + logger.info( + "running test with routing configurations:\n\t includes: [{}]\n\t excludes: [{}]\n\t requires: [{}]", + includes, + excludes, + requires + ); AllocationRoutedStep step = createRandomInstance(); - assertAllocateStatus(index, 1, 0, step, existingSettings, node1Settings, node2Settings, indexRoutingTable, - new ClusterStateWaitStep.Result(true, null)); + assertAllocateStatus( + index, + 1, + 0, + step, + existingSettings, + node1Settings, + node2Settings, + indexRoutingTable, + new ClusterStateWaitStep.Result(true, null) + ); } public void testRequireConditionMetOnlyOneCopyAllocated() { Index index = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20)); Map requires = Collections.singletonMap(IndexMetadata.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + "foo", "bar"); - Settings.Builder existingSettings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT.id) + Settings.Builder existingSettings = Settings.builder() + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT.id) .put(IndexMetadata.SETTING_INDEX_UUID, index.getUUID()); Settings.Builder node1Settings = Settings.builder(); requires.forEach((k, v) -> { @@ -114,43 +126,65 @@ public void testRequireConditionMetOnlyOneCopyAllocated() { boolean primaryOnNode1 = randomBoolean(); IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index) .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node1", primaryOnNode1, ShardRoutingState.STARTED)) - .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node2", primaryOnNode1 == false, - ShardRoutingState.STARTED)); + .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node2", primaryOnNode1 == false, ShardRoutingState.STARTED)); AllocationRoutedStep step = new AllocationRoutedStep(randomStepKey(), randomStepKey()); - assertAllocateStatus(index, 1, 0, step, existingSettings, node1Settings, Settings.builder(), indexRoutingTable, - new ClusterStateWaitStep.Result(false, allShardsActiveAllocationInfo(0, 1))); + assertAllocateStatus( + index, + 1, + 0, + step, + existingSettings, + node1Settings, + Settings.builder(), + indexRoutingTable, + new ClusterStateWaitStep.Result(false, allShardsActiveAllocationInfo(0, 1)) + ); } public void testClusterExcludeFiltersConditionMetOnlyOneCopyAllocated() { Index index = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20)); - Settings.Builder existingSettings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT.id) + Settings.Builder existingSettings = Settings.builder() + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT.id) .put(IndexMetadata.SETTING_INDEX_UUID, index.getUUID()); boolean primaryOnNode1 = randomBoolean(); IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index) .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node1", primaryOnNode1, ShardRoutingState.STARTED)) - .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node2", primaryOnNode1 == false, - ShardRoutingState.STARTED)); + .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node2", primaryOnNode1 == false, ShardRoutingState.STARTED)); AllocationRoutedStep step = new AllocationRoutedStep(randomStepKey(), randomStepKey()); - IndexMetadata indexMetadata = IndexMetadata.builder(index.getName()).settings(existingSettings).numberOfShards(1) - .numberOfReplicas(1).build(); - ImmutableOpenMap.Builder indices = ImmutableOpenMap.builder().fPut(index.getName(), - indexMetadata); - - Settings clusterSettings = Settings.builder() - .put("cluster.routing.allocation.exclude._id", "node1") + IndexMetadata indexMetadata = IndexMetadata.builder(index.getName()) + .settings(existingSettings) + .numberOfShards(1) + .numberOfReplicas(1) .build(); + ImmutableOpenMap.Builder indices = ImmutableOpenMap.builder() + .fPut(index.getName(), indexMetadata); + + Settings clusterSettings = Settings.builder().put("cluster.routing.allocation.exclude._id", "node1").build(); Settings.Builder nodeSettingsBuilder = Settings.builder(); ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE) .metadata(Metadata.builder().indices(indices.build()).transientSettings(clusterSettings)) - .nodes(DiscoveryNodes.builder() - .add(DiscoveryNode.createLocal(nodeSettingsBuilder.build(), new TransportAddress(TransportAddress.META_ADDRESS, 9200), - "node1")) - .add(DiscoveryNode.createLocal(nodeSettingsBuilder.build(), new TransportAddress(TransportAddress.META_ADDRESS, 9201), - "node2"))) - .routingTable(RoutingTable.builder().add(indexRoutingTable).build()).build(); + .nodes( + DiscoveryNodes.builder() + .add( + DiscoveryNode.createLocal( + nodeSettingsBuilder.build(), + new TransportAddress(TransportAddress.META_ADDRESS, 9200), + "node1" + ) + ) + .add( + DiscoveryNode.createLocal( + nodeSettingsBuilder.build(), + new TransportAddress(TransportAddress.META_ADDRESS, 9201), + "node2" + ) + ) + ) + .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) + .build(); Result actualResult = step.isConditionMet(index, clusterState); Result expectedResult = new ClusterStateWaitStep.Result(false, allShardsActiveAllocationInfo(1, 1)); @@ -161,7 +195,8 @@ public void testClusterExcludeFiltersConditionMetOnlyOneCopyAllocated() { public void testExcludeConditionMetOnlyOneCopyAllocated() { Index index = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20)); Map excludes = Collections.singletonMap(IndexMetadata.INDEX_ROUTING_EXCLUDE_GROUP_SETTING.getKey() + "foo", "bar"); - Settings.Builder existingSettings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT.id) + Settings.Builder existingSettings = Settings.builder() + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT.id) .put(IndexMetadata.SETTING_INDEX_UUID, index.getUUID()); Settings.Builder node1Settings = Settings.builder(); excludes.forEach((k, v) -> { @@ -172,18 +207,27 @@ public void testExcludeConditionMetOnlyOneCopyAllocated() { boolean primaryOnNode1 = randomBoolean(); IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index) .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node1", primaryOnNode1, ShardRoutingState.STARTED)) - .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node2", primaryOnNode1 == false, - ShardRoutingState.STARTED)); + .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node2", primaryOnNode1 == false, ShardRoutingState.STARTED)); AllocationRoutedStep step = new AllocationRoutedStep(randomStepKey(), randomStepKey()); - assertAllocateStatus(index, 1, 0, step, existingSettings, node1Settings, Settings.builder(), indexRoutingTable, - new ClusterStateWaitStep.Result(false, allShardsActiveAllocationInfo(0, 1))); + assertAllocateStatus( + index, + 1, + 0, + step, + existingSettings, + node1Settings, + Settings.builder(), + indexRoutingTable, + new ClusterStateWaitStep.Result(false, allShardsActiveAllocationInfo(0, 1)) + ); } public void testIncludeConditionMetOnlyOneCopyAllocated() { Index index = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20)); Map includes = Collections.singletonMap(IndexMetadata.INDEX_ROUTING_INCLUDE_GROUP_SETTING.getKey() + "foo", "bar"); - Settings.Builder existingSettings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT.id) + Settings.Builder existingSettings = Settings.builder() + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT.id) .put(IndexMetadata.SETTING_INDEX_UUID, index.getUUID()); Settings.Builder node1Settings = Settings.builder(); includes.forEach((k, v) -> { @@ -194,12 +238,20 @@ public void testIncludeConditionMetOnlyOneCopyAllocated() { boolean primaryOnNode1 = randomBoolean(); IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index) .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node1", primaryOnNode1, ShardRoutingState.STARTED)) - .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node2", primaryOnNode1 == false, - ShardRoutingState.STARTED)); + .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node2", primaryOnNode1 == false, ShardRoutingState.STARTED)); AllocationRoutedStep step = new AllocationRoutedStep(randomStepKey(), randomStepKey()); - assertAllocateStatus(index, 1, 0, step, existingSettings, node1Settings, Settings.builder(), indexRoutingTable, - new ClusterStateWaitStep.Result(false, allShardsActiveAllocationInfo(0, 1))); + assertAllocateStatus( + index, + 1, + 0, + step, + existingSettings, + node1Settings, + Settings.builder(), + indexRoutingTable, + new ClusterStateWaitStep.Result(false, allShardsActiveAllocationInfo(0, 1)) + ); } public void testConditionNotMetDueToRelocation() { @@ -216,73 +268,102 @@ public void testConditionNotMetDueToRelocation() { node1Settings.put(Node.NODE_ATTRIBUTES.getKey() + k, v); }); boolean primaryOnNode1 = randomBoolean(); - ShardRouting shardOnNode1 = TestShardRouting.newShardRouting(new ShardId(index, 0), - "node1", primaryOnNode1, ShardRoutingState.STARTED); + ShardRouting shardOnNode1 = TestShardRouting.newShardRouting( + new ShardId(index, 0), + "node1", + primaryOnNode1, + ShardRoutingState.STARTED + ); shardOnNode1 = shardOnNode1.relocate("node3", 230); IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index) .addShard(shardOnNode1) - .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node2", primaryOnNode1 == false, - ShardRoutingState.STARTED)); + .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node2", primaryOnNode1 == false, ShardRoutingState.STARTED)); AllocationRoutedStep step = new AllocationRoutedStep(randomStepKey(), randomStepKey()); - assertAllocateStatus(index, 1, 0, step, existingSettings, node1Settings, node2Settings, indexRoutingTable, - new ClusterStateWaitStep.Result(false, allShardsActiveAllocationInfo(0, 2))); + assertAllocateStatus( + index, + 1, + 0, + step, + existingSettings, + node1Settings, + node2Settings, + indexRoutingTable, + new ClusterStateWaitStep.Result(false, allShardsActiveAllocationInfo(0, 2)) + ); } public void testExecuteAllocateNotComplete() throws Exception { Index index = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20)); Map includes = AllocateActionTests.randomAllocationRoutingMap(1, 5); - Map excludes = randomValueOtherThanMany(map -> map.keySet().stream().anyMatch(includes::containsKey), - () -> AllocateActionTests.randomAllocationRoutingMap(1, 5)); - Map requires = randomValueOtherThanMany(map -> map.keySet().stream().anyMatch(includes::containsKey) || - map.keySet().stream().anyMatch(excludes::containsKey), - () -> AllocateActionTests.randomAllocationRoutingMap(1, 5)); - Settings.Builder existingSettings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT.id) - .put(IndexMetadata.SETTING_INDEX_UUID, index.getUUID()); + Map excludes = randomValueOtherThanMany( + map -> map.keySet().stream().anyMatch(includes::containsKey), + () -> AllocateActionTests.randomAllocationRoutingMap(1, 5) + ); + Map requires = randomValueOtherThanMany( + map -> map.keySet().stream().anyMatch(includes::containsKey) || map.keySet().stream().anyMatch(excludes::containsKey), + () -> AllocateActionTests.randomAllocationRoutingMap(1, 5) + ); + Settings.Builder existingSettings = Settings.builder() + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT.id) + .put(IndexMetadata.SETTING_INDEX_UUID, index.getUUID()); Settings.Builder node1Settings = Settings.builder(); Settings.Builder node2Settings = Settings.builder(); includes.forEach((k, v) -> { existingSettings.put(IndexMetadata.INDEX_ROUTING_INCLUDE_GROUP_SETTING.getKey() + k, v); node1Settings.put(Node.NODE_ATTRIBUTES.getKey() + k, v); }); - excludes.forEach((k, v) -> { - existingSettings.put(IndexMetadata.INDEX_ROUTING_EXCLUDE_GROUP_SETTING.getKey() + k, v); - }); + excludes.forEach((k, v) -> { existingSettings.put(IndexMetadata.INDEX_ROUTING_EXCLUDE_GROUP_SETTING.getKey() + k, v); }); requires.forEach((k, v) -> { existingSettings.put(IndexMetadata.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + k, v); node1Settings.put(Node.NODE_ATTRIBUTES.getKey() + k, v); }); IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index) - .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node1", true, ShardRoutingState.STARTED)) - .addShard(TestShardRouting.newShardRouting(new ShardId(index, 1), "node2", true, ShardRoutingState.STARTED)); - - logger.info("running test with routing configurations:\n\t includes: [{}]\n\t excludes: [{}]\n\t requires: [{}]", - includes, excludes, requires); + .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node1", true, ShardRoutingState.STARTED)) + .addShard(TestShardRouting.newShardRouting(new ShardId(index, 1), "node2", true, ShardRoutingState.STARTED)); + + logger.info( + "running test with routing configurations:\n\t includes: [{}]\n\t excludes: [{}]\n\t requires: [{}]", + includes, + excludes, + requires + ); AllocationRoutedStep step = createRandomInstance(); - assertAllocateStatus(index, 2, 0, step, existingSettings, node1Settings, node2Settings, indexRoutingTable, - new ClusterStateWaitStep.Result(false, allShardsActiveAllocationInfo(0, 1))); + assertAllocateStatus( + index, + 2, + 0, + step, + existingSettings, + node1Settings, + node2Settings, + indexRoutingTable, + new ClusterStateWaitStep.Result(false, allShardsActiveAllocationInfo(0, 1)) + ); } public void testExecuteAllocateNotCompleteOnlyOneCopyAllocated() throws Exception { Index index = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20)); Map includes = AllocateActionTests.randomAllocationRoutingMap(1, 5); - Map excludes = randomValueOtherThanMany(map -> map.keySet().stream().anyMatch(includes::containsKey), - () -> AllocateActionTests.randomAllocationRoutingMap(1, 5)); - Map requires = randomValueOtherThanMany(map -> map.keySet().stream().anyMatch(includes::containsKey) || - map.keySet().stream().anyMatch(excludes::containsKey), - () -> AllocateActionTests.randomAllocationRoutingMap(1, 5)); - Settings.Builder existingSettings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT.id) - .put(IndexMetadata.SETTING_INDEX_UUID, index.getUUID()); + Map excludes = randomValueOtherThanMany( + map -> map.keySet().stream().anyMatch(includes::containsKey), + () -> AllocateActionTests.randomAllocationRoutingMap(1, 5) + ); + Map requires = randomValueOtherThanMany( + map -> map.keySet().stream().anyMatch(includes::containsKey) || map.keySet().stream().anyMatch(excludes::containsKey), + () -> AllocateActionTests.randomAllocationRoutingMap(1, 5) + ); + Settings.Builder existingSettings = Settings.builder() + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT.id) + .put(IndexMetadata.SETTING_INDEX_UUID, index.getUUID()); Settings.Builder node1Settings = Settings.builder(); Settings.Builder node2Settings = Settings.builder(); includes.forEach((k, v) -> { existingSettings.put(IndexMetadata.INDEX_ROUTING_INCLUDE_GROUP_SETTING.getKey() + k, v); node1Settings.put(Node.NODE_ATTRIBUTES.getKey() + k, v); }); - excludes.forEach((k, v) -> { - existingSettings.put(IndexMetadata.INDEX_ROUTING_EXCLUDE_GROUP_SETTING.getKey() + k, v); - }); + excludes.forEach((k, v) -> { existingSettings.put(IndexMetadata.INDEX_ROUTING_EXCLUDE_GROUP_SETTING.getKey() + k, v); }); requires.forEach((k, v) -> { existingSettings.put(IndexMetadata.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + k, v); node1Settings.put(Node.NODE_ATTRIBUTES.getKey() + k, v); @@ -290,52 +371,86 @@ public void testExecuteAllocateNotCompleteOnlyOneCopyAllocated() throws Exceptio boolean primaryOnNode1 = randomBoolean(); IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index) - .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node1", primaryOnNode1, ShardRoutingState.STARTED)) - .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node2", primaryOnNode1 == false, - ShardRoutingState.STARTED)); - + .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node1", primaryOnNode1, ShardRoutingState.STARTED)) + .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node2", primaryOnNode1 == false, ShardRoutingState.STARTED)); AllocationRoutedStep step = new AllocationRoutedStep(randomStepKey(), randomStepKey()); - logger.info("running test with routing configurations:\n\t includes: [{}]\n\t excludes: [{}]\n\t requires: [{}]", - includes, excludes, requires); - assertAllocateStatus(index, 2, 0, step, existingSettings, node1Settings, node2Settings, indexRoutingTable, - new ClusterStateWaitStep.Result(false, allShardsActiveAllocationInfo(0, 1))); + logger.info( + "running test with routing configurations:\n\t includes: [{}]\n\t excludes: [{}]\n\t requires: [{}]", + includes, + excludes, + requires + ); + assertAllocateStatus( + index, + 2, + 0, + step, + existingSettings, + node1Settings, + node2Settings, + indexRoutingTable, + new ClusterStateWaitStep.Result(false, allShardsActiveAllocationInfo(0, 1)) + ); } public void testExecuteAllocateUnassigned() throws Exception { Index index = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20)); Map includes = AllocateActionTests.randomAllocationRoutingMap(1, 5); - Map excludes = randomValueOtherThanMany(map -> map.keySet().stream().anyMatch(includes::containsKey), - () -> AllocateActionTests.randomAllocationRoutingMap(1, 5)); - Map requires = randomValueOtherThanMany(map -> map.keySet().stream().anyMatch(includes::containsKey) || - map.keySet().stream().anyMatch(excludes::containsKey), - () -> AllocateActionTests.randomAllocationRoutingMap(1, 5)); - Settings.Builder existingSettings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT.id) - .put(IndexMetadata.SETTING_INDEX_UUID, index.getUUID()); + Map excludes = randomValueOtherThanMany( + map -> map.keySet().stream().anyMatch(includes::containsKey), + () -> AllocateActionTests.randomAllocationRoutingMap(1, 5) + ); + Map requires = randomValueOtherThanMany( + map -> map.keySet().stream().anyMatch(includes::containsKey) || map.keySet().stream().anyMatch(excludes::containsKey), + () -> AllocateActionTests.randomAllocationRoutingMap(1, 5) + ); + Settings.Builder existingSettings = Settings.builder() + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT.id) + .put(IndexMetadata.SETTING_INDEX_UUID, index.getUUID()); Settings.Builder node1Settings = Settings.builder(); Settings.Builder node2Settings = Settings.builder(); includes.forEach((k, v) -> { existingSettings.put(IndexMetadata.INDEX_ROUTING_INCLUDE_GROUP_SETTING.getKey() + k, v); node1Settings.put(Node.NODE_ATTRIBUTES.getKey() + k, v); }); - excludes.forEach((k, v) -> { - existingSettings.put(IndexMetadata.INDEX_ROUTING_EXCLUDE_GROUP_SETTING.getKey() + k, v); - }); + excludes.forEach((k, v) -> { existingSettings.put(IndexMetadata.INDEX_ROUTING_EXCLUDE_GROUP_SETTING.getKey() + k, v); }); requires.forEach((k, v) -> { existingSettings.put(IndexMetadata.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + k, v); node1Settings.put(Node.NODE_ATTRIBUTES.getKey() + k, v); }); IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index) - .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node1", true, ShardRoutingState.STARTED)) - .addShard(TestShardRouting.newShardRouting(new ShardId(index, 1), null, null, true, ShardRoutingState.UNASSIGNED, - TestShardRouting.randomUnassignedInfo("the shard is intentionally unassigned"))); - - logger.info("running test with routing configurations:\n\t includes: [{}]\n\t excludes: [{}]\n\t requires: [{}]", - includes, excludes, requires); + .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node1", true, ShardRoutingState.STARTED)) + .addShard( + TestShardRouting.newShardRouting( + new ShardId(index, 1), + null, + null, + true, + ShardRoutingState.UNASSIGNED, + TestShardRouting.randomUnassignedInfo("the shard is intentionally unassigned") + ) + ); + + logger.info( + "running test with routing configurations:\n\t includes: [{}]\n\t excludes: [{}]\n\t requires: [{}]", + includes, + excludes, + requires + ); AllocationRoutedStep step = createRandomInstance(); - assertAllocateStatus(index, 2, 0, step, existingSettings, node1Settings, node2Settings, indexRoutingTable, - new ClusterStateWaitStep.Result(false, waitingForActiveShardsAllocationInfo(0))); + assertAllocateStatus( + index, + 2, + 0, + step, + existingSettings, + node1Settings, + node2Settings, + indexRoutingTable, + new ClusterStateWaitStep.Result(false, waitingForActiveShardsAllocationInfo(0)) + ); } /** @@ -357,19 +472,37 @@ public void testExecuteAllocateUnassigned() throws Exception { */ public void testExecuteReplicasNotAllocatedOnSingleNode() { Index index = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20)); - Settings.Builder existingSettings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT.id) + Settings.Builder existingSettings = Settings.builder() + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT.id) .put(IndexMetadata.SETTING_INDEX_UUID, index.getUUID()); Settings.Builder node1Settings = Settings.builder(); Settings.Builder node2Settings = Settings.builder(); IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index) .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node1", true, ShardRoutingState.STARTED)) - .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), null, null, false, ShardRoutingState.UNASSIGNED, - new UnassignedInfo(Reason.REPLICA_ADDED, "no attempt"))); + .addShard( + TestShardRouting.newShardRouting( + new ShardId(index, 0), + null, + null, + false, + ShardRoutingState.UNASSIGNED, + new UnassignedInfo(Reason.REPLICA_ADDED, "no attempt") + ) + ); AllocationRoutedStep step = createRandomInstance(); - assertAllocateStatus(index, 1, 1, step, existingSettings, node1Settings, node2Settings, indexRoutingTable, - new ClusterStateWaitStep.Result(false, waitingForActiveShardsAllocationInfo(1))); + assertAllocateStatus( + index, + 1, + 1, + step, + existingSettings, + node1Settings, + node2Settings, + indexRoutingTable, + new ClusterStateWaitStep.Result(false, waitingForActiveShardsAllocationInfo(1)) + ); } public void testExecuteIndexMissing() throws Exception { @@ -383,21 +516,38 @@ public void testExecuteIndexMissing() throws Exception { assertNull(actualResult.getInfomationContext()); } - private void assertAllocateStatus(Index index, int shards, int replicas, AllocationRoutedStep step, Settings.Builder existingSettings, - Settings.Builder node1Settings, Settings.Builder node2Settings, IndexRoutingTable.Builder indexRoutingTable, - ClusterStateWaitStep.Result expectedResult) { - IndexMetadata indexMetadata = IndexMetadata.builder(index.getName()).settings(existingSettings).numberOfShards(shards) - .numberOfReplicas(replicas).build(); - ImmutableOpenMap.Builder indices = ImmutableOpenMap. builder().fPut(index.getName(), - indexMetadata); - - ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE).metadata(Metadata.builder().indices(indices.build())) - .nodes(DiscoveryNodes.builder() - .add(DiscoveryNode.createLocal(node1Settings.build(), new TransportAddress(TransportAddress.META_ADDRESS, 9200), - "node1")) - .add(DiscoveryNode.createLocal(node2Settings.build(), new TransportAddress(TransportAddress.META_ADDRESS, 9201), - "node2"))) - .routingTable(RoutingTable.builder().add(indexRoutingTable).build()).build(); + private void assertAllocateStatus( + Index index, + int shards, + int replicas, + AllocationRoutedStep step, + Settings.Builder existingSettings, + Settings.Builder node1Settings, + Settings.Builder node2Settings, + IndexRoutingTable.Builder indexRoutingTable, + ClusterStateWaitStep.Result expectedResult + ) { + IndexMetadata indexMetadata = IndexMetadata.builder(index.getName()) + .settings(existingSettings) + .numberOfShards(shards) + .numberOfReplicas(replicas) + .build(); + ImmutableOpenMap.Builder indices = ImmutableOpenMap.builder() + .fPut(index.getName(), indexMetadata); + + ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE) + .metadata(Metadata.builder().indices(indices.build())) + .nodes( + DiscoveryNodes.builder() + .add( + DiscoveryNode.createLocal(node1Settings.build(), new TransportAddress(TransportAddress.META_ADDRESS, 9200), "node1") + ) + .add( + DiscoveryNode.createLocal(node2Settings.build(), new TransportAddress(TransportAddress.META_ADDRESS, 9201), "node2") + ) + ) + .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) + .build(); Result actualResult = step.isConditionMet(index, clusterState); assertEquals(expectedResult.isComplete(), actualResult.isComplete()); assertEquals(expectedResult.getInfomationContext(), actualResult.getInfomationContext()); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/BranchingStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/BranchingStepTests.java index 655ecb7ed9213..bb33a4e8f29c1 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/BranchingStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/BranchingStepTests.java @@ -23,9 +23,12 @@ public class BranchingStepTests extends AbstractStepTestCase { public void testPredicateNextStepChange() { String indexName = randomAlphaOfLength(5); - ClusterState state = ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder() - .put(IndexMetadata.builder(indexName).settings(settings(Version.CURRENT)) - .numberOfShards(1).numberOfReplicas(0))).build(); + ClusterState state = ClusterState.builder(ClusterName.DEFAULT) + .metadata( + Metadata.builder() + .put(IndexMetadata.builder(indexName).settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(0)) + ) + .build(); StepKey stepKey = new StepKey(randomAlphaOfLength(5), randomAlphaOfLength(5), BranchingStep.NAME); StepKey nextStepKey = new StepKey(randomAlphaOfLength(6), randomAlphaOfLength(6), BranchingStep.NAME); StepKey nextSkipKey = new StepKey(randomAlphaOfLength(7), randomAlphaOfLength(7), BranchingStep.NAME); @@ -68,8 +71,11 @@ public BranchingStep mutateInstance(BranchingStep instance) { nextStepKey = new StepKey(nextStepKey.getPhase(), nextStepKey.getAction(), nextStepKey.getName() + randomAlphaOfLength(5)); break; case 2: - nextSkipStepKey = new StepKey(nextSkipStepKey.getPhase(), nextSkipStepKey.getAction(), - nextSkipStepKey.getName() + randomAlphaOfLength(5)); + nextSkipStepKey = new StepKey( + nextSkipStepKey.getPhase(), + nextSkipStepKey.getAction(), + nextSkipStepKey.getName() + randomAlphaOfLength(5) + ); break; default: throw new AssertionError("Illegal randomisation branch"); @@ -80,7 +86,11 @@ public BranchingStep mutateInstance(BranchingStep instance) { @Override public BranchingStep copyInstance(BranchingStep instance) { - return new BranchingStep(instance.getKey(), instance.getNextStepKeyOnFalse(), instance.getNextStepKeyOnTrue(), - instance.getPredicate()); + return new BranchingStep( + instance.getKey(), + instance.getNextStepKeyOnFalse(), + instance.getNextStepKeyOnTrue(), + instance.getPredicate() + ); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CheckNoDataStreamWriteIndexStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CheckNoDataStreamWriteIndexStepTests.java index 39e66e89eee44..b923fce86ee5f 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CheckNoDataStreamWriteIndexStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CheckNoDataStreamWriteIndexStepTests.java @@ -52,13 +52,15 @@ protected CheckNotDataStreamWriteIndexStep copyInstance(CheckNotDataStreamWriteI public void testStepCompleteIfIndexIsNotPartOfDataStream() { String indexName = randomAlphaOfLength(10); String policyName = "test-ilm-policy"; - IndexMetadata indexMetadata = - IndexMetadata.builder(indexName).settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + IndexMetadata indexMetadata = IndexMetadata.builder(indexName) + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); - ClusterState clusterState = ClusterState.builder(emptyClusterState()).metadata( - Metadata.builder().put(indexMetadata, true).build() - ).build(); + ClusterState clusterState = ClusterState.builder(emptyClusterState()) + .metadata(Metadata.builder().put(indexMetadata, true).build()) + .build(); ClusterStateWaitStep.Result result = createRandomInstance().isConditionMet(indexMetadata.getIndex(), clusterState); assertThat(result.isComplete(), is(true)); @@ -69,21 +71,38 @@ public void testStepIncompleteIfIndexIsTheDataStreamWriteIndex() { String dataStreamName = randomAlphaOfLength(10); String indexName = DataStream.getDefaultBackingIndexName(dataStreamName, 1); String policyName = "test-ilm-policy"; - IndexMetadata indexMetadata = - IndexMetadata.builder(indexName).settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + IndexMetadata indexMetadata = IndexMetadata.builder(indexName) + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); - ClusterState clusterState = ClusterState.builder(emptyClusterState()).metadata( - Metadata.builder().put(indexMetadata, true).put(new DataStream(dataStreamName, - createTimestampField("@timestamp"), List.of(indexMetadata.getIndex()))).build() - ).build(); + ClusterState clusterState = ClusterState.builder(emptyClusterState()) + .metadata( + Metadata.builder() + .put(indexMetadata, true) + .put(new DataStream(dataStreamName, createTimestampField("@timestamp"), List.of(indexMetadata.getIndex()))) + .build() + ) + .build(); ClusterStateWaitStep.Result result = createRandomInstance().isConditionMet(indexMetadata.getIndex(), clusterState); assertThat(result.isComplete(), is(false)); CheckNotDataStreamWriteIndexStep.Info info = (CheckNotDataStreamWriteIndexStep.Info) result.getInfomationContext(); - assertThat(info.getMessage(), is("index [" + indexName + "] is the write index for data stream [" + dataStreamName + "], " + - "pausing ILM execution of lifecycle [" + policyName + "] until this index is no longer the write index for the data stream " + - "via manual or automated rollover")); + assertThat( + info.getMessage(), + is( + "index [" + + indexName + + "] is the write index for data stream [" + + dataStreamName + + "], " + + "pausing ILM execution of lifecycle [" + + policyName + + "] until this index is no longer the write index for the data stream " + + "via manual or automated rollover" + ) + ); } public void testStepCompleteIfPartOfDataStreamButNotWriteIndex() { @@ -92,23 +111,27 @@ public void testStepCompleteIfPartOfDataStreamButNotWriteIndex() { String policyName = "test-ilm-policy"; IndexMetadata indexMetadata = IndexMetadata.builder(indexName) .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) .build(); String writeIndexName = DataStream.getDefaultBackingIndexName(dataStreamName, 2); IndexMetadata writeIndexMetadata = IndexMetadata.builder(writeIndexName) .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) .build(); List backingIndices = List.of(indexMetadata.getIndex(), writeIndexMetadata.getIndex()); - ClusterState clusterState = ClusterState.builder(emptyClusterState()).metadata( - Metadata.builder() - .put(indexMetadata, true) - .put(writeIndexMetadata, true) - .put(new DataStream(dataStreamName, createTimestampField("@timestamp"), backingIndices)) - .build() - ).build(); + ClusterState clusterState = ClusterState.builder(emptyClusterState()) + .metadata( + Metadata.builder() + .put(indexMetadata, true) + .put(writeIndexMetadata, true) + .put(new DataStream(dataStreamName, createTimestampField("@timestamp"), backingIndices)) + .build() + ) + .build(); ClusterStateWaitStep.Result result = createRandomInstance().isConditionMet(indexMetadata.getIndex(), clusterState); assertThat(result.isComplete(), is(true)); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CheckShrinkReadyStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CheckShrinkReadyStepTests.java index 041f8ef3dfbb0..e5ced493320b4 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CheckShrinkReadyStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CheckShrinkReadyStepTests.java @@ -88,8 +88,17 @@ public void testNoSetting() { CheckShrinkReadyStep step = createRandomInstance(); IllegalStateException e = expectThrows(IllegalStateException.class, () -> { - assertAllocateStatus(index, 1, 0, step, existingSettings, node1Settings, node2Settings, indexRoutingTable, - new ClusterStateWaitStep.Result(true, null)); + assertAllocateStatus( + index, + 1, + 0, + step, + existingSettings, + node1Settings, + node2Settings, + indexRoutingTable, + new ClusterStateWaitStep.Result(true, null) + ); }); assertThat(e.getMessage(), containsString("Cannot check shrink allocation as there are no allocation rules by _id")); } @@ -114,8 +123,17 @@ public void testConditionMet() { .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node1", true, ShardRoutingState.STARTED)); CheckShrinkReadyStep step = createRandomInstance(); - assertAllocateStatus(index, 1, 0, step, existingSettings, node1Settings, node2Settings, indexRoutingTable, - new ClusterStateWaitStep.Result(true, null)); + assertAllocateStatus( + index, + 1, + 0, + step, + existingSettings, + node1Settings, + node2Settings, + indexRoutingTable, + new ClusterStateWaitStep.Result(true, null) + ); } public void testConditionMetOnlyOneCopyAllocated() { @@ -136,12 +154,20 @@ public void testConditionMetOnlyOneCopyAllocated() { boolean primaryOnNode1 = randomBoolean(); IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index) .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node1", primaryOnNode1, ShardRoutingState.STARTED)) - .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node2", primaryOnNode1 == false, - ShardRoutingState.STARTED)); + .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node2", primaryOnNode1 == false, ShardRoutingState.STARTED)); CheckShrinkReadyStep step = new CheckShrinkReadyStep(randomStepKey(), randomStepKey()); - assertAllocateStatus(index, 1, 0, step, existingSettings, node1Settings, node2Settings, indexRoutingTable, - new ClusterStateWaitStep.Result(true, null)); + assertAllocateStatus( + index, + 1, + 0, + step, + existingSettings, + node1Settings, + node2Settings, + indexRoutingTable, + new ClusterStateWaitStep.Result(true, null) + ); } public void testConditionNotMetDueToRelocation() { @@ -160,17 +186,29 @@ public void testConditionNotMetDueToRelocation() { node1Settings.put(Node.NODE_ATTRIBUTES.getKey() + k, v); }); boolean primaryOnNode1 = randomBoolean(); - ShardRouting shardOnNode1 = TestShardRouting.newShardRouting(new ShardId(index, 0), - "node1", primaryOnNode1, ShardRoutingState.STARTED); + ShardRouting shardOnNode1 = TestShardRouting.newShardRouting( + new ShardId(index, 0), + "node1", + primaryOnNode1, + ShardRoutingState.STARTED + ); shardOnNode1 = shardOnNode1.relocate("node3", 230); IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index) .addShard(shardOnNode1) - .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node2", primaryOnNode1 == false, - ShardRoutingState.STARTED)); + .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node2", primaryOnNode1 == false, ShardRoutingState.STARTED)); CheckShrinkReadyStep step = new CheckShrinkReadyStep(randomStepKey(), randomStepKey()); - assertAllocateStatus(index, 1, 0, step, existingSettings, node1Settings, node2Settings, indexRoutingTable, - new ClusterStateWaitStep.Result(false, new CheckShrinkReadyStep.Info("node1", 1, 1))); + assertAllocateStatus( + index, + 1, + 0, + step, + existingSettings, + node1Settings, + node2Settings, + indexRoutingTable, + new ClusterStateWaitStep.Result(false, new CheckShrinkReadyStep.Info("node1", 1, 1)) + ); } public void testExecuteAllocateNotComplete() throws Exception { @@ -194,8 +232,17 @@ public void testExecuteAllocateNotComplete() throws Exception { .addShard(TestShardRouting.newShardRouting(new ShardId(index, 1), "node2", true, ShardRoutingState.STARTED)); CheckShrinkReadyStep step = createRandomInstance(); - assertAllocateStatus(index, 2, 0, step, existingSettings, node1Settings, node2Settings, indexRoutingTable, - new ClusterStateWaitStep.Result(false, new CheckShrinkReadyStep.Info("node1", 2, 1))); + assertAllocateStatus( + index, + 2, + 0, + step, + existingSettings, + node1Settings, + node2Settings, + indexRoutingTable, + new ClusterStateWaitStep.Result(false, new CheckShrinkReadyStep.Info("node1", 2, 1)) + ); } public void testExecuteAllocateNotCompleteOnlyOneCopyAllocated() throws Exception { @@ -217,12 +264,20 @@ public void testExecuteAllocateNotCompleteOnlyOneCopyAllocated() throws Exceptio boolean primaryOnNode1 = randomBoolean(); IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index) .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node1", primaryOnNode1, ShardRoutingState.STARTED)) - .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node2", primaryOnNode1 == false, - ShardRoutingState.STARTED)); + .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node2", primaryOnNode1 == false, ShardRoutingState.STARTED)); CheckShrinkReadyStep step = new CheckShrinkReadyStep(randomStepKey(), randomStepKey()); - assertAllocateStatus(index, 2, 0, step, existingSettings, node1Settings, node2Settings, indexRoutingTable, - new ClusterStateWaitStep.Result(false, new CheckShrinkReadyStep.Info("node1", 2, 1))); + assertAllocateStatus( + index, + 2, + 0, + step, + existingSettings, + node1Settings, + node2Settings, + indexRoutingTable, + new ClusterStateWaitStep.Result(false, new CheckShrinkReadyStep.Info("node1", 2, 1)) + ); } public void testExecuteAllocateReplicaUnassigned() { @@ -243,12 +298,29 @@ public void testExecuteAllocateReplicaUnassigned() { IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index) .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node1", true, ShardRoutingState.STARTED)) - .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), null, null, false, ShardRoutingState.UNASSIGNED, - randomUnassignedInfo("the shard is intentionally unassigned"))); + .addShard( + TestShardRouting.newShardRouting( + new ShardId(index, 0), + null, + null, + false, + ShardRoutingState.UNASSIGNED, + randomUnassignedInfo("the shard is intentionally unassigned") + ) + ); CheckShrinkReadyStep step = createRandomInstance(); - assertAllocateStatus(index, 1, 1, step, existingSettings, node1Settings, node2Settings, indexRoutingTable, - new ClusterStateWaitStep.Result(true, null)); + assertAllocateStatus( + index, + 1, + 1, + step, + existingSettings, + node1Settings, + node2Settings, + indexRoutingTable, + new ClusterStateWaitStep.Result(true, null) + ); } /** @@ -278,20 +350,35 @@ public void testExecuteReplicasNotAllocatedOnSingleNode() { Settings.Builder expectedSettings = Settings.builder(); Settings.Builder node1Settings = Settings.builder(); Settings.Builder node2Settings = Settings.builder(); - requires.forEach((k, v) -> { - expectedSettings.put(IndexMetadata.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + k, v); - }); + requires.forEach((k, v) -> { expectedSettings.put(IndexMetadata.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + k, v); }); IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index) .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node1", true, ShardRoutingState.STARTED)) .addShard(TestShardRouting.newShardRouting(new ShardId(index, 1), "node1", false, ShardRoutingState.STARTED)) .addShard(TestShardRouting.newShardRouting(new ShardId(index, 1), "node2", true, ShardRoutingState.STARTED)) - .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), null, null, false, ShardRoutingState.UNASSIGNED, - new UnassignedInfo(UnassignedInfo.Reason.REPLICA_ADDED, "no attempt"))); + .addShard( + TestShardRouting.newShardRouting( + new ShardId(index, 0), + null, + null, + false, + ShardRoutingState.UNASSIGNED, + new UnassignedInfo(UnassignedInfo.Reason.REPLICA_ADDED, "no attempt") + ) + ); CheckShrinkReadyStep step = createRandomInstance(); - assertAllocateStatus(index, 2, 1, step, existingSettings, node1Settings, node2Settings, indexRoutingTable, - new ClusterStateWaitStep.Result(true, null)); + assertAllocateStatus( + index, + 2, + 1, + step, + existingSettings, + node1Settings, + node2Settings, + indexRoutingTable, + new ClusterStateWaitStep.Result(true, null) + ); } public void testExecuteReplicasButCopiesNotPresent() { @@ -304,20 +391,35 @@ public void testExecuteReplicasButCopiesNotPresent() { Settings.Builder expectedSettings = Settings.builder(); Settings.Builder node1Settings = Settings.builder(); Settings.Builder node2Settings = Settings.builder(); - requires.forEach((k, v) -> { - expectedSettings.put(IndexMetadata.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + k, v); - }); + requires.forEach((k, v) -> { expectedSettings.put(IndexMetadata.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + k, v); }); IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index) .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node1", true, ShardRoutingState.STARTED)) .addShard(TestShardRouting.newShardRouting(new ShardId(index, 1), "node2", false, ShardRoutingState.STARTED)) .addShard(TestShardRouting.newShardRouting(new ShardId(index, 1), "node3", true, ShardRoutingState.STARTED)) - .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), null, null, false, ShardRoutingState.UNASSIGNED, - new UnassignedInfo(UnassignedInfo.Reason.REPLICA_ADDED, "no attempt"))); + .addShard( + TestShardRouting.newShardRouting( + new ShardId(index, 0), + null, + null, + false, + ShardRoutingState.UNASSIGNED, + new UnassignedInfo(UnassignedInfo.Reason.REPLICA_ADDED, "no attempt") + ) + ); CheckShrinkReadyStep step = createRandomInstance(); - assertAllocateStatus(index, 2, 1, step, existingSettings, node1Settings, node2Settings, indexRoutingTable, - new ClusterStateWaitStep.Result(false, new CheckShrinkReadyStep.Info("node1", 2, 1))); + assertAllocateStatus( + index, + 2, + 1, + step, + existingSettings, + node1Settings, + node2Settings, + indexRoutingTable, + new ClusterStateWaitStep.Result(false, new CheckShrinkReadyStep.Info("node1", 2, 1)) + ); } public void testExecuteIndexMissing() throws Exception { @@ -351,10 +453,13 @@ public void testStepCompletableIfAllShardsActive() { .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node1", true, ShardRoutingState.STARTED)); CheckShrinkReadyStep step = createRandomInstance(); - IndexMetadata indexMetadata = IndexMetadata.builder(index.getName()).settings(existingSettings).numberOfShards(1) - .numberOfReplicas(1).build(); - ImmutableOpenMap.Builder indices = ImmutableOpenMap. builder().fPut(index.getName(), - indexMetadata); + IndexMetadata indexMetadata = IndexMetadata.builder(index.getName()) + .settings(existingSettings) + .numberOfShards(1) + .numberOfReplicas(1) + .build(); + ImmutableOpenMap.Builder indices = ImmutableOpenMap.builder() + .fPut(index.getName(), indexMetadata); final SingleNodeShutdownMetadata.Type type = randomFrom( SingleNodeShutdownMetadata.Type.REMOVE, @@ -362,26 +467,44 @@ public void testStepCompletableIfAllShardsActive() { ); final String targetNodeName = type == SingleNodeShutdownMetadata.Type.REPLACE ? randomAlphaOfLengthBetween(10, 20) : null; ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE) - .metadata(Metadata.builder() - .indices(indices.build()) - .putCustom(NodesShutdownMetadata.TYPE, new NodesShutdownMetadata(Collections.singletonMap("node1", - SingleNodeShutdownMetadata.builder() - .setType(type) - .setStartedAtMillis(randomNonNegativeLong()) - .setReason("test") - .setNodeId("node1") - .setTargetNodeName(targetNodeName) - .build())))) - .nodes(DiscoveryNodes.builder() - .add(DiscoveryNode.createLocal(Settings.builder().put(node1Settings.build()) - .put(Node.NODE_NAME_SETTING.getKey(), "node1").build(), - new TransportAddress(TransportAddress.META_ADDRESS, 9200), - "node1")) - .add(DiscoveryNode.createLocal(Settings.builder().put(node2Settings.build()) - .put(Node.NODE_NAME_SETTING.getKey(), "node2").build(), - new TransportAddress(TransportAddress.META_ADDRESS, 9201), - "node2"))) - .routingTable(RoutingTable.builder().add(indexRoutingTable).build()).build(); + .metadata( + Metadata.builder() + .indices(indices.build()) + .putCustom( + NodesShutdownMetadata.TYPE, + new NodesShutdownMetadata( + Collections.singletonMap( + "node1", + SingleNodeShutdownMetadata.builder() + .setType(type) + .setStartedAtMillis(randomNonNegativeLong()) + .setReason("test") + .setNodeId("node1") + .setTargetNodeName(targetNodeName) + .build() + ) + ) + ) + ) + .nodes( + DiscoveryNodes.builder() + .add( + DiscoveryNode.createLocal( + Settings.builder().put(node1Settings.build()).put(Node.NODE_NAME_SETTING.getKey(), "node1").build(), + new TransportAddress(TransportAddress.META_ADDRESS, 9200), + "node1" + ) + ) + .add( + DiscoveryNode.createLocal( + Settings.builder().put(node2Settings.build()).put(Node.NODE_NAME_SETTING.getKey(), "node2").build(), + new TransportAddress(TransportAddress.META_ADDRESS, 9201), + "node2" + ) + ) + ) + .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) + .build(); assertTrue(step.isCompletable()); ClusterStateWaitStep.Result actualResult = step.isConditionMet(index, clusterState); assertTrue(actualResult.isComplete()); @@ -408,10 +531,13 @@ public void testStepBecomesUncompletable() { .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node1", true, ShardRoutingState.INITIALIZING)); CheckShrinkReadyStep step = createRandomInstance(); - IndexMetadata indexMetadata = IndexMetadata.builder(index.getName()).settings(existingSettings).numberOfShards(1) - .numberOfReplicas(1).build(); - ImmutableOpenMap.Builder indices = ImmutableOpenMap. builder().fPut(index.getName(), - indexMetadata); + IndexMetadata indexMetadata = IndexMetadata.builder(index.getName()) + .settings(existingSettings) + .numberOfShards(1) + .numberOfReplicas(1) + .build(); + ImmutableOpenMap.Builder indices = ImmutableOpenMap.builder() + .fPut(index.getName(), indexMetadata); final SingleNodeShutdownMetadata.Type type = randomFrom( SingleNodeShutdownMetadata.Type.REMOVE, @@ -419,53 +545,94 @@ public void testStepBecomesUncompletable() { ); final String targetNodeName = type == SingleNodeShutdownMetadata.Type.REPLACE ? randomAlphaOfLengthBetween(10, 20) : null; ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE) - .metadata(Metadata.builder() - .indices(indices.build()) - .putCustom(NodesShutdownMetadata.TYPE, new NodesShutdownMetadata(Collections.singletonMap("node1", - SingleNodeShutdownMetadata.builder() - .setType(type) - .setStartedAtMillis(randomNonNegativeLong()) - .setReason("test") - .setNodeId("node1") - .setTargetNodeName(targetNodeName) - .build())))) - .nodes(DiscoveryNodes.builder() - .add(DiscoveryNode.createLocal(Settings.builder().put(node1Settings.build()) - .put(Node.NODE_NAME_SETTING.getKey(), "node1").build(), - new TransportAddress(TransportAddress.META_ADDRESS, 9200), - "node1")) - .add(DiscoveryNode.createLocal(Settings.builder().put(node2Settings.build()) - .put(Node.NODE_NAME_SETTING.getKey(), "node2").build(), - new TransportAddress(TransportAddress.META_ADDRESS, 9201), - "node2"))) - .routingTable(RoutingTable.builder().add(indexRoutingTable).build()).build(); + .metadata( + Metadata.builder() + .indices(indices.build()) + .putCustom( + NodesShutdownMetadata.TYPE, + new NodesShutdownMetadata( + Collections.singletonMap( + "node1", + SingleNodeShutdownMetadata.builder() + .setType(type) + .setStartedAtMillis(randomNonNegativeLong()) + .setReason("test") + .setNodeId("node1") + .setTargetNodeName(targetNodeName) + .build() + ) + ) + ) + ) + .nodes( + DiscoveryNodes.builder() + .add( + DiscoveryNode.createLocal( + Settings.builder().put(node1Settings.build()).put(Node.NODE_NAME_SETTING.getKey(), "node1").build(), + new TransportAddress(TransportAddress.META_ADDRESS, 9200), + "node1" + ) + ) + .add( + DiscoveryNode.createLocal( + Settings.builder().put(node2Settings.build()).put(Node.NODE_NAME_SETTING.getKey(), "node2").build(), + new TransportAddress(TransportAddress.META_ADDRESS, 9201), + "node2" + ) + ) + ) + .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) + .build(); assertTrue(step.isCompletable()); ClusterStateWaitStep.Result actualResult = step.isConditionMet(index, clusterState); assertFalse(actualResult.isComplete()); - assertThat(Strings.toString(actualResult.getInfomationContext()), - containsString("node with id [node1] is currently marked as shutting down")); + assertThat( + Strings.toString(actualResult.getInfomationContext()), + containsString("node with id [node1] is currently marked as shutting down") + ); assertFalse(step.isCompletable()); } - private void assertAllocateStatus(Index index, int shards, int replicas, CheckShrinkReadyStep step, Settings.Builder existingSettings, - Settings.Builder node1Settings, Settings.Builder node2Settings, - IndexRoutingTable.Builder indexRoutingTable, ClusterStateWaitStep.Result expectedResult) { - IndexMetadata indexMetadata = IndexMetadata.builder(index.getName()).settings(existingSettings).numberOfShards(shards) - .numberOfReplicas(replicas).build(); - ImmutableOpenMap.Builder indices = ImmutableOpenMap. builder().fPut(index.getName(), - indexMetadata); - - ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE).metadata(Metadata.builder().indices(indices.build())) - .nodes(DiscoveryNodes.builder() - .add(DiscoveryNode.createLocal(Settings.builder().put(node1Settings.build()) - .put(Node.NODE_NAME_SETTING.getKey(), "node1").build(), - new TransportAddress(TransportAddress.META_ADDRESS, 9200), - "node1")) - .add(DiscoveryNode.createLocal(Settings.builder().put(node2Settings.build()) - .put(Node.NODE_NAME_SETTING.getKey(), "node2").build(), - new TransportAddress(TransportAddress.META_ADDRESS, 9201), - "node2"))) - .routingTable(RoutingTable.builder().add(indexRoutingTable).build()).build(); + private void assertAllocateStatus( + Index index, + int shards, + int replicas, + CheckShrinkReadyStep step, + Settings.Builder existingSettings, + Settings.Builder node1Settings, + Settings.Builder node2Settings, + IndexRoutingTable.Builder indexRoutingTable, + ClusterStateWaitStep.Result expectedResult + ) { + IndexMetadata indexMetadata = IndexMetadata.builder(index.getName()) + .settings(existingSettings) + .numberOfShards(shards) + .numberOfReplicas(replicas) + .build(); + ImmutableOpenMap.Builder indices = ImmutableOpenMap.builder() + .fPut(index.getName(), indexMetadata); + + ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE) + .metadata(Metadata.builder().indices(indices.build())) + .nodes( + DiscoveryNodes.builder() + .add( + DiscoveryNode.createLocal( + Settings.builder().put(node1Settings.build()).put(Node.NODE_NAME_SETTING.getKey(), "node1").build(), + new TransportAddress(TransportAddress.META_ADDRESS, 9200), + "node1" + ) + ) + .add( + DiscoveryNode.createLocal( + Settings.builder().put(node2Settings.build()).put(Node.NODE_NAME_SETTING.getKey(), "node2").build(), + new TransportAddress(TransportAddress.META_ADDRESS, 9201), + "node2" + ) + ) + ) + .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) + .build(); ClusterStateWaitStep.Result actualResult = step.isConditionMet(index, clusterState); assertEquals(expectedResult.isComplete(), actualResult.isComplete()); assertEquals(expectedResult.getInfomationContext(), actualResult.getInfomationContext()); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CheckTargetShardsCountStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CheckTargetShardsCountStepTests.java index 9c50e9a20b5cb..13f4456b38b72 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CheckTargetShardsCountStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CheckTargetShardsCountStepTests.java @@ -28,14 +28,14 @@ protected CheckTargetShardsCountStep mutateInstance(CheckTargetShardsCountStep i StepKey nextKey = instance.getNextStepKey(); switch (between(0, 1)) { - case 0: - key = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); - break; - case 1: - nextKey = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); - break; - default: - throw new AssertionError("Illegal randomisation branch"); + case 0: + key = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); + break; + case 1: + nextKey = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); + break; + default: + throw new AssertionError("Illegal randomisation branch"); } return new CheckTargetShardsCountStep(key, nextKey, null); @@ -48,13 +48,15 @@ protected CheckTargetShardsCountStep copyInstance(CheckTargetShardsCountStep ins public void testStepCompleteIfTargetShardsCountIsValid() { String policyName = "test-ilm-policy"; - IndexMetadata indexMetadata = - IndexMetadata.builder(randomAlphaOfLength(10)).settings(settings(Version.CURRENT) - .put(LifecycleSettings.LIFECYCLE_NAME, policyName)) - .numberOfShards(10).numberOfReplicas(randomIntBetween(0, 5)).build(); + IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) + .numberOfShards(10) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); - ClusterState clusterState = ClusterState.builder(emptyClusterState()).metadata( - Metadata.builder().put(indexMetadata, true).build()).build(); + ClusterState clusterState = ClusterState.builder(emptyClusterState()) + .metadata(Metadata.builder().put(indexMetadata, true).build()) + .build(); CheckTargetShardsCountStep checkTargetShardsCountStep = new CheckTargetShardsCountStep(randomStepKey(), randomStepKey(), 2); @@ -65,19 +67,30 @@ public void testStepCompleteIfTargetShardsCountIsValid() { public void testStepIncompleteIfTargetShardsCountNotValid() { String indexName = randomAlphaOfLength(10); String policyName = "test-ilm-policy"; - IndexMetadata indexMetadata = - IndexMetadata.builder(indexName).settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) - .numberOfShards(10).numberOfReplicas(randomIntBetween(0, 5)).build(); + IndexMetadata indexMetadata = IndexMetadata.builder(indexName) + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) + .numberOfShards(10) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); - ClusterState clusterState = ClusterState.builder(emptyClusterState()).metadata( - Metadata.builder().put(indexMetadata, true).build()).build(); + ClusterState clusterState = ClusterState.builder(emptyClusterState()) + .metadata(Metadata.builder().put(indexMetadata, true).build()) + .build(); CheckTargetShardsCountStep checkTargetShardsCountStep = new CheckTargetShardsCountStep(randomStepKey(), randomStepKey(), 3); ClusterStateWaitStep.Result result = checkTargetShardsCountStep.isConditionMet(indexMetadata.getIndex(), clusterState); assertThat(result.isComplete(), is(false)); SingleMessageFieldInfo info = (SingleMessageFieldInfo) result.getInfomationContext(); - assertThat(info.getMessage(), is("lifecycle action of policy [" + policyName + "] for index [" + indexName + - "] cannot make progress because the target shards count [3] must be a factor of the source index's shards count [10]")); + assertThat( + info.getMessage(), + is( + "lifecycle action of policy [" + + policyName + + "] for index [" + + indexName + + "] cannot make progress because the target shards count [3] must be a factor of the source index's shards count [10]" + ) + ); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CleanupShrinkIndexStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CleanupShrinkIndexStepTests.java index a7de483693cee..999b1e5c738da 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CleanupShrinkIndexStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CleanupShrinkIndexStepTests.java @@ -60,25 +60,29 @@ public void testPerformActionDoesntFailIfShrinkingIndexNameIsMissing() { String indexName = randomAlphaOfLength(10); String policyName = "test-ilm-policy"; - IndexMetadata.Builder indexMetadataBuilder = - IndexMetadata.builder(indexName).settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)); + IndexMetadata.Builder indexMetadataBuilder = IndexMetadata.builder(indexName) + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)); IndexMetadata indexMetadata = indexMetadataBuilder.build(); - ClusterState clusterState = - ClusterState.builder(emptyClusterState()).metadata(Metadata.builder().put(indexMetadata, true).build()).build(); + ClusterState clusterState = ClusterState.builder(emptyClusterState()) + .metadata(Metadata.builder().put(indexMetadata, true).build()) + .build(); CleanupShrinkIndexStep cleanupShrinkIndexStep = createRandomInstance(); cleanupShrinkIndexStep.performAction(indexMetadata, clusterState, null, new ActionListener<>() { @Override - public void onResponse(Void unused) { - } + public void onResponse(Void unused) {} @Override public void onFailure(Exception e) { - fail("expecting the step to not report any failure if there isn't any shrink index name stored in the ILM execution " + - "state but got:" + e.getMessage()); + fail( + "expecting the step to not report any failure if there isn't any shrink index name stored in the ILM execution " + + "state but got:" + + e.getMessage() + ); } }); } @@ -89,25 +93,25 @@ public void testPerformAction() { String shrinkIndexName = generateValidIndexName("shrink-", indexName); Map ilmCustom = Map.of("shrink_index_name", shrinkIndexName); - IndexMetadata.Builder indexMetadataBuilder = - IndexMetadata.builder(indexName).settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) - .putCustom(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY, ilmCustom) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)); + IndexMetadata.Builder indexMetadataBuilder = IndexMetadata.builder(indexName) + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) + .putCustom(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY, ilmCustom) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)); IndexMetadata indexMetadata = indexMetadataBuilder.build(); - ClusterState clusterState = - ClusterState.builder(emptyClusterState()).metadata(Metadata.builder().put(indexMetadata, true).build()).build(); + ClusterState clusterState = ClusterState.builder(emptyClusterState()) + .metadata(Metadata.builder().put(indexMetadata, true).build()) + .build(); try (NoOpClient client = getDeleteIndexRequestAssertingClient(shrinkIndexName)) { CleanupShrinkIndexStep step = new CleanupShrinkIndexStep(randomStepKey(), randomStepKey(), client); step.performAction(indexMetadata, clusterState, null, new ActionListener<>() { @Override - public void onResponse(Void complete) { - } + public void onResponse(Void complete) {} @Override - public void onFailure(Exception e) { - } + public void onFailure(Exception e) {} }); } } @@ -120,23 +124,23 @@ public void testDeleteSkippedIfManagedIndexIsShrunkAndSourceDoesntExist() { IndexMetadata.Builder shrunkIndexMetadataBuilder = IndexMetadata.builder(shrinkIndexName) .settings( - settings(Version.CURRENT) - .put(LifecycleSettings.LIFECYCLE_NAME, policyName) + settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName) .put(IndexMetadata.INDEX_RESIZE_SOURCE_NAME_KEY, sourceIndex) ) .putCustom(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY, ilmCustom) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)); + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)); IndexMetadata shrunkIndexMetadata = shrunkIndexMetadataBuilder.build(); - ClusterState clusterState = - ClusterState.builder(emptyClusterState()).metadata(Metadata.builder().put(shrunkIndexMetadata, true).build()).build(); + ClusterState clusterState = ClusterState.builder(emptyClusterState()) + .metadata(Metadata.builder().put(shrunkIndexMetadata, true).build()) + .build(); try (NoOpClient client = getFailingIfCalledClient()) { CleanupShrinkIndexStep step = new CleanupShrinkIndexStep(randomStepKey(), randomStepKey(), client); step.performAction(shrunkIndexMetadata, clusterState, null, new ActionListener<>() { @Override - public void onResponse(Void complete) { - } + public void onResponse(Void complete) {} @Override public void onFailure(Exception e) { @@ -149,9 +153,11 @@ public void onFailure(Exception e) { private NoOpClient getDeleteIndexRequestAssertingClient(String shrinkIndexName) { return new NoOpClient(getTestName()) { @Override - protected void doExecute(ActionType action, - Request request, - ActionListener listener) { + protected void doExecute( + ActionType action, + Request request, + ActionListener listener + ) { assertThat(action.name(), is(DeleteIndexAction.NAME)); assertTrue(request instanceof DeleteIndexRequest); assertThat(((DeleteIndexRequest) request).indices(), arrayContaining(shrinkIndexName)); @@ -162,11 +168,14 @@ protected void private NoOpClient getFailingIfCalledClient() { return new NoOpClient(getTestName()) { @Override - protected void doExecute(ActionType action, - Request request, - ActionListener listener) { - throw new IllegalStateException("not expecting client to be called, but received request [" + request + "] for action [" - + action + "]"); + protected void doExecute( + ActionType action, + Request request, + ActionListener listener + ) { + throw new IllegalStateException( + "not expecting client to be called, but received request [" + request + "] for action [" + action + "]" + ); } }; } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CleanupSnapshotStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CleanupSnapshotStepTests.java index 1ab8723b7d91b..e8660eaa2e049 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CleanupSnapshotStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CleanupSnapshotStepTests.java @@ -61,30 +61,34 @@ public void testPerformActionDoesntFailIfSnapshotInfoIsMissing() throws Exceptio String policyName = "test-ilm-policy"; { - IndexMetadata.Builder indexMetadataBuilder = - IndexMetadata.builder(indexName).settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)); + IndexMetadata.Builder indexMetadataBuilder = IndexMetadata.builder(indexName) + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)); IndexMetadata indexMetadata = indexMetadataBuilder.build(); - ClusterState clusterState = - ClusterState.builder(emptyClusterState()).metadata(Metadata.builder().put(indexMetadata, true).build()).build(); + ClusterState clusterState = ClusterState.builder(emptyClusterState()) + .metadata(Metadata.builder().put(indexMetadata, true).build()) + .build(); CleanupSnapshotStep cleanupSnapshotStep = createRandomInstance(); PlainActionFuture.get(f -> cleanupSnapshotStep.performAction(indexMetadata, clusterState, null, f)); } { - IndexMetadata.Builder indexMetadataBuilder = - IndexMetadata.builder(indexName).settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)); + IndexMetadata.Builder indexMetadataBuilder = IndexMetadata.builder(indexName) + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)); Map ilmCustom = Map.of("snapshot_repository", "repository_name"); indexMetadataBuilder.putCustom(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY, ilmCustom); IndexMetadata indexMetadata = indexMetadataBuilder.build(); - ClusterState clusterState = - ClusterState.builder(emptyClusterState()).metadata(Metadata.builder().put(indexMetadata, true).build()).build(); + ClusterState clusterState = ClusterState.builder(emptyClusterState()) + .metadata(Metadata.builder().put(indexMetadata, true).build()) + .build(); CleanupSnapshotStep cleanupSnapshotStep = createRandomInstance(); PlainActionFuture.get(f -> cleanupSnapshotStep.performAction(indexMetadata, clusterState, null, f)); @@ -97,25 +101,25 @@ public void testPerformAction() { String snapshotName = indexName + "-" + policyName; Map ilmCustom = Map.of("snapshot_name", snapshotName); - IndexMetadata.Builder indexMetadataBuilder = - IndexMetadata.builder(indexName).settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) - .putCustom(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY, ilmCustom) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)); + IndexMetadata.Builder indexMetadataBuilder = IndexMetadata.builder(indexName) + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) + .putCustom(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY, ilmCustom) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)); IndexMetadata indexMetadata = indexMetadataBuilder.build(); - ClusterState clusterState = - ClusterState.builder(emptyClusterState()).metadata(Metadata.builder().put(indexMetadata, true).build()).build(); + ClusterState clusterState = ClusterState.builder(emptyClusterState()) + .metadata(Metadata.builder().put(indexMetadata, true).build()) + .build(); try (NoOpClient client = getDeleteSnapshotRequestAssertingClient(snapshotName)) { CleanupSnapshotStep step = new CleanupSnapshotStep(randomStepKey(), randomStepKey(), client); step.performAction(indexMetadata, clusterState, null, new ActionListener<>() { @Override - public void onResponse(Void complete) { - } + public void onResponse(Void complete) {} @Override - public void onFailure(Exception e) { - } + public void onFailure(Exception e) {} }); } } @@ -123,9 +127,11 @@ public void onFailure(Exception e) { private NoOpClient getDeleteSnapshotRequestAssertingClient(String expectedSnapshotName) { return new NoOpClient(getTestName()) { @Override - protected void doExecute(ActionType action, - Request request, - ActionListener listener) { + protected void doExecute( + ActionType action, + Request request, + ActionListener listener + ) { assertThat(action.name(), is(DeleteSnapshotAction.NAME)); assertTrue(request instanceof DeleteSnapshotRequest); assertThat(((DeleteSnapshotRequest) request).snapshots(), arrayContaining(expectedSnapshotName)); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CloseFollowerIndexStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CloseFollowerIndexStepTests.java index 357dd09c62b9e..1b9d985f93b2e 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CloseFollowerIndexStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CloseFollowerIndexStepTests.java @@ -60,8 +60,10 @@ public void testRequestNotAcknowledged() { }).when(indicesClient).close(Mockito.any(), Mockito.any()); CloseFollowerIndexStep step = new CloseFollowerIndexStep(randomStepKey(), randomStepKey(), client); - Exception e = expectThrows(Exception.class, - () -> PlainActionFuture.get(f -> step.performAction(indexMetadata, emptyClusterState(), null, f))); + Exception e = expectThrows( + Exception.class, + () -> PlainActionFuture.get(f -> step.performAction(indexMetadata, emptyClusterState(), null, f)) + ); assertThat(e.getMessage(), is("close index request failed to be acknowledged")); } @@ -73,14 +75,19 @@ public void testCloseFollowingIndexFailed() { Mockito.doAnswer(invocation -> { CloseIndexRequest closeIndexRequest = (CloseIndexRequest) invocation.getArguments()[0]; assertThat(closeIndexRequest.indices()[0], equalTo("follower-index")); - ActionListenerlistener = (ActionListener) invocation.getArguments()[1]; + ActionListener listener = (ActionListener) invocation.getArguments()[1]; listener.onFailure(error); return null; }).when(indicesClient).close(Mockito.any(), Mockito.any()); CloseFollowerIndexStep step = new CloseFollowerIndexStep(randomStepKey(), randomStepKey(), client); - assertSame(error, expectThrows(Exception.class, - () -> PlainActionFuture.get(f -> step.performAction(indexMetadata, emptyClusterState(), null, f)))); + assertSame( + error, + expectThrows( + Exception.class, + () -> PlainActionFuture.get(f -> step.performAction(indexMetadata, emptyClusterState(), null, f)) + ) + ); Mockito.verify(indicesClient).close(Mockito.any(), Mockito.any()); Mockito.verifyNoMoreInteractions(indicesClient); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CloseIndexStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CloseIndexStepTests.java index e55d2fc21d557..ba2b773e2f675 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CloseIndexStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CloseIndexStepTests.java @@ -64,8 +64,11 @@ protected CloseIndexStep copyInstance(CloseIndexStep instance) { } public void testPerformAction() { - IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)).settings(settings(Version.CURRENT)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) + .settings(settings(Version.CURRENT)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); CloseIndexStep step = createRandomInstance(); @@ -79,9 +82,10 @@ public void testPerformAction() { CloseIndexRequest request = (CloseIndexRequest) invocation.getArguments()[0]; @SuppressWarnings("unchecked") ActionListener listener = (ActionListener) invocation.getArguments()[1]; - assertThat(request.indices(), equalTo(new String[]{indexMetadata.getIndex().getName()})); - listener.onResponse(new CloseIndexResponse(true, true, - Collections.singletonList(new CloseIndexResponse.IndexResult(indexMetadata.getIndex())))); + assertThat(request.indices(), equalTo(new String[] { indexMetadata.getIndex().getName() })); + listener.onResponse( + new CloseIndexResponse(true, true, Collections.singletonList(new CloseIndexResponse.IndexResult(indexMetadata.getIndex()))) + ); return null; }).when(indicesClient).close(Mockito.any(), Mockito.any()); @@ -106,10 +110,12 @@ public void onFailure(Exception e) { Mockito.verify(indicesClient, Mockito.only()).close(Mockito.any(), Mockito.any()); } - public void testPerformActionFailure() { - IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)).settings(settings(Version.CURRENT)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) + .settings(settings(Version.CURRENT)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); CloseIndexStep step = createRandomInstance(); Exception exception = new RuntimeException(); @@ -123,13 +129,18 @@ public void testPerformActionFailure() { CloseIndexRequest request = (CloseIndexRequest) invocation.getArguments()[0]; @SuppressWarnings("unchecked") ActionListener listener = (ActionListener) invocation.getArguments()[1]; - assertThat(request.indices(), equalTo(new String[]{indexMetadata.getIndex().getName()})); + assertThat(request.indices(), equalTo(new String[] { indexMetadata.getIndex().getName() })); listener.onFailure(exception); return null; }).when(indicesClient).close(Mockito.any(), Mockito.any()); - assertSame(exception, expectThrows(Exception.class, () -> PlainActionFuture.get( - f -> step.performAction(indexMetadata, null, null, f)))); + assertSame( + exception, + expectThrows( + Exception.class, + () -> PlainActionFuture.get(f -> step.performAction(indexMetadata, null, null, f)) + ) + ); Mockito.verify(client, Mockito.only()).admin(); Mockito.verify(adminClient, Mockito.only()).indices(); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ClusterStateWaitUntilThresholdStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ClusterStateWaitUntilThresholdStepTests.java index 274899b67fcc1..425f7002a73d1 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ClusterStateWaitUntilThresholdStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ClusterStateWaitUntilThresholdStepTests.java @@ -67,8 +67,10 @@ public ClusterStateWaitUntilThresholdStep copyInstance(ClusterStateWaitUntilThre public void testIndexIsMissingReturnsIncompleteResult() { WaitForIndexingCompleteStep stepToExecute = new WaitForIndexingCompleteStep(randomStepKey(), randomStepKey()); ClusterStateWaitUntilThresholdStep underTest = new ClusterStateWaitUntilThresholdStep(stepToExecute, randomStepKey()); - ClusterStateWaitStep.Result result = underTest.isConditionMet(new Index("testName", UUID.randomUUID().toString()), - ClusterState.EMPTY_STATE); + ClusterStateWaitStep.Result result = underTest.isConditionMet( + new Index("testName", UUID.randomUUID().toString()), + ClusterState.EMPTY_STATE + ); assertThat(result.isComplete(), is(false)); assertThat(result.getInfomationContext(), nullValue()); } @@ -78,8 +80,7 @@ public void testIsConditionMetForUnderlyingStep() { // threshold is not breached and the underlying step condition is met IndexMetadata indexMetadata = IndexMetadata.builder("follower-index") .settings( - settings(Version.CURRENT) - .put(LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE, "true") + settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE, "true") .put(LifecycleSettings.LIFECYCLE_STEP_WAIT_TIME_THRESHOLD, "480h") ) .putCustom(ILM_CUSTOM_METADATA_KEY, Map.of("step_time", String.valueOf(System.currentTimeMillis()))) @@ -103,8 +104,7 @@ public void testIsConditionMetForUnderlyingStep() { // threshold is not breached and the underlying step condition is NOT met IndexMetadata indexMetadata = IndexMetadata.builder("follower-index") .settings( - settings(Version.CURRENT) - .put(LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE, "false") + settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE, "false") .put(LifecycleSettings.LIFECYCLE_STEP_WAIT_TIME_THRESHOLD, "48h") ) .putCustom(ILM_CUSTOM_METADATA_KEY, Map.of("step_time", String.valueOf(System.currentTimeMillis()))) @@ -123,18 +123,22 @@ public void testIsConditionMetForUnderlyingStep() { assertThat(result.isComplete(), is(false)); assertThat(result.getInfomationContext(), notNullValue()); - WaitForIndexingCompleteStep.IndexingNotCompleteInfo info = - (WaitForIndexingCompleteStep.IndexingNotCompleteInfo) result.getInfomationContext(); - assertThat(info.getMessage(), equalTo("waiting for the [index.lifecycle.indexing_complete] setting to be set to " + - "true on the leader index, it is currently [false]")); + WaitForIndexingCompleteStep.IndexingNotCompleteInfo info = (WaitForIndexingCompleteStep.IndexingNotCompleteInfo) result + .getInfomationContext(); + assertThat( + info.getMessage(), + equalTo( + "waiting for the [index.lifecycle.indexing_complete] setting to be set to " + + "true on the leader index, it is currently [false]" + ) + ); } { // underlying step is executed once even if the threshold is breached and the underlying complete result is returned IndexMetadata indexMetadata = IndexMetadata.builder("follower-index") .settings( - settings(Version.CURRENT) - .put(LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE, "true") + settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE, "true") .put(LifecycleSettings.LIFECYCLE_STEP_WAIT_TIME_THRESHOLD, "1s") ) .putCustom(CCR_METADATA_KEY, Collections.emptyMap()) @@ -162,8 +166,7 @@ public void testIsConditionMetForUnderlyingStep() { // step under test will return `complete` (becuase the threshold is breached and we don't want to wait anymore) IndexMetadata indexMetadata = IndexMetadata.builder("follower-index") .settings( - settings(Version.CURRENT) - .put(LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE, "false") + settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE, "false") .put(LifecycleSettings.LIFECYCLE_STEP_WAIT_TIME_THRESHOLD, "1h") ) .putCustom(CCR_METADATA_KEY, Collections.emptyMap()) @@ -185,10 +188,20 @@ public void testIsConditionMetForUnderlyingStep() { assertThat(result.isComplete(), is(true)); assertThat(result.getInfomationContext(), notNullValue()); SingleMessageFieldInfo info = (SingleMessageFieldInfo) result.getInfomationContext(); - assertThat(info.getMessage(), - equalTo("[" + currentStepKey.getName() + "] lifecycle step, as part of [" + currentStepKey.getAction() + "] " + - "action, for index [follower-index] executed for more than [1h]. Abandoning execution and moving to the next " + - "fallback step [" + nextKeyOnThresholdBreach + "]")); + assertThat( + info.getMessage(), + equalTo( + "[" + + currentStepKey.getName() + + "] lifecycle step, as part of [" + + currentStepKey.getAction() + + "] " + + "action, for index [follower-index] executed for more than [1h]. Abandoning execution and moving to the next " + + "fallback step [" + + nextKeyOnThresholdBreach + + "]" + ) + ); // the next step must change to the provided one when the threshold is breached assertThat(underTest.getNextStepKey(), is(nextKeyOnThresholdBreach)); @@ -202,24 +215,27 @@ public void testWaitedMoreThanThresholdLevelMath() { { // step time is "2 hours ago" with a threshold of 1 hour - the threshold level is breached - LifecycleExecutionState executionState = - new LifecycleExecutionState.Builder().setStepTime(epochMillis - TimeValue.timeValueHours(2).millis()).build(); + LifecycleExecutionState executionState = new LifecycleExecutionState.Builder().setStepTime( + epochMillis - TimeValue.timeValueHours(2).millis() + ).build(); boolean thresholdBreached = waitedMoreThanThresholdLevel(retryThreshold, executionState, clock); assertThat(thresholdBreached, is(true)); } { // step time is "10 minutes ago" with a threshold of 1 hour - the threshold level is NOT breached - LifecycleExecutionState executionState = - new LifecycleExecutionState.Builder().setStepTime(epochMillis - TimeValue.timeValueMinutes(10).millis()).build(); + LifecycleExecutionState executionState = new LifecycleExecutionState.Builder().setStepTime( + epochMillis - TimeValue.timeValueMinutes(10).millis() + ).build(); boolean thresholdBreached = waitedMoreThanThresholdLevel(retryThreshold, executionState, clock); assertThat(thresholdBreached, is(false)); } { // if no threshold is configured we'll never report the threshold is breached - LifecycleExecutionState executionState = - new LifecycleExecutionState.Builder().setStepTime(epochMillis - TimeValue.timeValueHours(2).millis()).build(); + LifecycleExecutionState executionState = new LifecycleExecutionState.Builder().setStepTime( + epochMillis - TimeValue.timeValueHours(2).millis() + ).build(); boolean thresholdBreached = waitedMoreThanThresholdLevel(null, executionState, clock); assertThat(thresholdBreached, is(false)); } @@ -238,8 +254,7 @@ public void testIsCompletableBreaches() { .build(); ClusterStateWaitUntilThresholdStep step = new ClusterStateWaitUntilThresholdStep( - new ClusterStateWaitStep(new StepKey("phase" , "action", "key"), - new StepKey("phase", "action", "next-key")) { + new ClusterStateWaitStep(new StepKey("phase", "action", "key"), new StepKey("phase", "action", "next-key")) { @Override public Result isConditionMet(Index index, ClusterState clusterState) { return new Result(false, new SingleMessageFieldInfo("")); @@ -254,15 +269,16 @@ public boolean isCompletable() { public boolean isRetryable() { return true; } - }, new StepKey("phase", "action", "breached")); + }, + new StepKey("phase", "action", "breached") + ); assertFalse(step.isConditionMet(indexMetadata.getIndex(), clusterState).isComplete()); assertThat(step.getNextStepKey().getName(), equalTo("next-key")); step = new ClusterStateWaitUntilThresholdStep( - new ClusterStateWaitStep(new StepKey("phase" , "action", "key"), - new StepKey("phase", "action", "next-key")) { + new ClusterStateWaitStep(new StepKey("phase", "action", "key"), new StepKey("phase", "action", "next-key")) { @Override public Result isConditionMet(Index index, ClusterState clusterState) { return new Result(false, new SingleMessageFieldInfo("")); @@ -277,7 +293,9 @@ public boolean isCompletable() { public boolean isRetryable() { return true; } - }, new StepKey("phase", "action", "breached")); + }, + new StepKey("phase", "action", "breached") + ); assertTrue(step.isConditionMet(indexMetadata.getIndex(), clusterState).isComplete()); assertThat(step.getNextStepKey().getName(), equalTo("breached")); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CopyExecutionStateStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CopyExecutionStateStepTests.java index c232029ec635a..610cde2232402 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CopyExecutionStateStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CopyExecutionStateStepTests.java @@ -50,8 +50,11 @@ protected CopyExecutionStateStep mutateInstance(CopyExecutionStateStep instance) indexNameSupplier = (index, state) -> randomAlphaOfLengthBetween(11, 15) + index; break; case 3: - targetNextStepKey = new StepKey(targetNextStepKey.getPhase(), targetNextStepKey.getAction(), - targetNextStepKey.getName() + randomAlphaOfLength(5)); + targetNextStepKey = new StepKey( + targetNextStepKey.getPhase(), + targetNextStepKey.getAction(), + targetNextStepKey.getName() + randomAlphaOfLength(5) + ); break; default: throw new AssertionError("Illegal randomisation branch"); @@ -62,8 +65,12 @@ protected CopyExecutionStateStep mutateInstance(CopyExecutionStateStep instance) @Override protected CopyExecutionStateStep copyInstance(CopyExecutionStateStep instance) { - return new CopyExecutionStateStep(instance.getKey(), instance.getNextStepKey(), instance.getTargetIndexNameSupplier(), - instance.getTargetNextStepKey()); + return new CopyExecutionStateStep( + instance.getKey(), + instance.getNextStepKey(), + instance.getTargetIndexNameSupplier(), + instance.getTargetNextStepKey() + ); } public void testPerformAction() { @@ -72,27 +79,24 @@ public void testPerformAction() { Map customMetadata = createCustomMetadata(); IndexMetadata originalIndexMetadata = IndexMetadata.builder(indexName) - .settings(settings(Version.CURRENT)).numberOfShards(randomIntBetween(1,5)) - .numberOfReplicas(randomIntBetween(1,5)) + .settings(settings(Version.CURRENT)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(1, 5)) .putCustom(ILM_CUSTOM_METADATA_KEY, customMetadata) .build(); - IndexMetadata shrunkIndexMetadata = - IndexMetadata.builder(step.getTargetIndexNameSupplier().apply(indexName, LifecycleExecutionState.builder().build())) - .settings(settings(Version.CURRENT)).numberOfShards(randomIntBetween(1,5)) - .numberOfReplicas(randomIntBetween(1,5)) - .build(); + IndexMetadata shrunkIndexMetadata = IndexMetadata.builder( + step.getTargetIndexNameSupplier().apply(indexName, LifecycleExecutionState.builder().build()) + ).settings(settings(Version.CURRENT)).numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(1, 5)).build(); ClusterState originalClusterState = ClusterState.builder(ClusterName.DEFAULT) - .metadata(Metadata.builder() - .put(originalIndexMetadata, false) - .put(shrunkIndexMetadata, false)) + .metadata(Metadata.builder().put(originalIndexMetadata, false).put(shrunkIndexMetadata, false)) .build(); ClusterState newClusterState = step.performAction(originalIndexMetadata.getIndex(), originalClusterState); LifecycleExecutionState oldIndexData = LifecycleExecutionState.fromIndexMetadata(originalIndexMetadata); - LifecycleExecutionState newIndexData = LifecycleExecutionState - .fromIndexMetadata(newClusterState.metadata().index(step.getTargetIndexNameSupplier().apply(indexName, - LifecycleExecutionState.builder().build()))); + LifecycleExecutionState newIndexData = LifecycleExecutionState.fromIndexMetadata( + newClusterState.metadata().index(step.getTargetIndexNameSupplier().apply(indexName, LifecycleExecutionState.builder().build())) + ); StepKey targetNextStepKey = step.getTargetNextStepKey(); assertEquals(newIndexData.getLifecycleDate(), oldIndexData.getLifecycleDate()); @@ -108,28 +112,25 @@ public void testAllStateCopied() { String indexName = randomAlphaOfLengthBetween(5, 20); IndexMetadata originalIndexMetadata = IndexMetadata.builder(indexName) - .settings(settings(Version.CURRENT)).numberOfShards(randomIntBetween(1,5)) - .numberOfReplicas(randomIntBetween(1,5)) + .settings(settings(Version.CURRENT)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(1, 5)) .putCustom(ILM_CUSTOM_METADATA_KEY, createCustomMetadata()) .build(); - IndexMetadata shrunkIndexMetadata = - IndexMetadata.builder(step.getTargetIndexNameSupplier().apply(indexName, LifecycleExecutionState.builder().build())) - .settings(settings(Version.CURRENT)).numberOfShards(randomIntBetween(1,5)) - .numberOfReplicas(randomIntBetween(1,5)) - .build(); + IndexMetadata shrunkIndexMetadata = IndexMetadata.builder( + step.getTargetIndexNameSupplier().apply(indexName, LifecycleExecutionState.builder().build()) + ).settings(settings(Version.CURRENT)).numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(1, 5)).build(); ClusterState originalClusterState = ClusterState.builder(ClusterName.DEFAULT) - .metadata(Metadata.builder() - .put(originalIndexMetadata, false) - .put(shrunkIndexMetadata, false)) + .metadata(Metadata.builder().put(originalIndexMetadata, false).put(shrunkIndexMetadata, false)) .build(); ClusterState newClusterState = step.performAction(originalIndexMetadata.getIndex(), originalClusterState); LifecycleExecutionState oldIndexData = LifecycleExecutionState.fromIndexMetadata(originalIndexMetadata); - LifecycleExecutionState newIndexData = LifecycleExecutionState - .fromIndexMetadata(newClusterState.metadata().index(step.getTargetIndexNameSupplier().apply(indexName, - LifecycleExecutionState.builder().build()))); + LifecycleExecutionState newIndexData = LifecycleExecutionState.fromIndexMetadata( + newClusterState.metadata().index(step.getTargetIndexNameSupplier().apply(indexName, LifecycleExecutionState.builder().build())) + ); Map beforeMap = new HashMap<>(oldIndexData.asMap()); // The target step key's StepKey is used in the new metadata, so update the "before" map with the new info so it can be compared @@ -146,21 +147,30 @@ public void testPerformActionWithNoTarget() { Map customMetadata = createCustomMetadata(); IndexMetadata originalIndexMetadata = IndexMetadata.builder(indexName) - .settings(settings(Version.CURRENT)).numberOfShards(randomIntBetween(1,5)) - .numberOfReplicas(randomIntBetween(1,5)) + .settings(settings(Version.CURRENT)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(1, 5)) .putCustom(ILM_CUSTOM_METADATA_KEY, customMetadata) .build(); ClusterState originalClusterState = ClusterState.builder(ClusterName.DEFAULT) - .metadata(Metadata.builder() - .put(originalIndexMetadata, false)) + .metadata(Metadata.builder().put(originalIndexMetadata, false)) .build(); - IllegalStateException e = expectThrows(IllegalStateException.class, - () -> step.performAction(originalIndexMetadata.getIndex(), originalClusterState)); - - assertThat(e.getMessage(), equalTo("unable to copy execution state from [" + - indexName + "] to [" + - step.getTargetIndexNameSupplier().apply(originalIndexMetadata.getIndex().getName(), LifecycleExecutionState.builder().build()) + - "] as target index does not exist")); + IllegalStateException e = expectThrows( + IllegalStateException.class, + () -> step.performAction(originalIndexMetadata.getIndex(), originalClusterState) + ); + + assertThat( + e.getMessage(), + equalTo( + "unable to copy execution state from [" + + indexName + + "] to [" + + step.getTargetIndexNameSupplier() + .apply(originalIndexMetadata.getIndex().getName(), LifecycleExecutionState.builder().build()) + + "] as target index does not exist" + ) + ); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CopySettingsStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CopySettingsStepTests.java index 78a8e69332436..95d9a1af019dc 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CopySettingsStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CopySettingsStepTests.java @@ -17,8 +17,12 @@ public class CopySettingsStepTests extends AbstractStepTestCase randomAlphaOfLengthBetween(1, 10)); break; case 3: - settingsKeys = new String[]{randomAlphaOfLengthBetween(1, 10)}; + settingsKeys = new String[] { randomAlphaOfLengthBetween(1, 10) }; break; default: throw new AssertionError("Illegal randomisation branch"); @@ -55,23 +59,30 @@ protected CopySettingsStep copyInstance(CopySettingsStep instance) { public void testPerformAction() { String indexName = randomAlphaOfLength(10); String policyName = "test-ilm-policy"; - IndexMetadata.Builder sourceIndexMetadataBuilder = - IndexMetadata.builder(indexName).settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)); + IndexMetadata.Builder sourceIndexMetadataBuilder = IndexMetadata.builder(indexName) + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)); String indexPrefix = "test-prefix-"; String targetIndex = indexPrefix + indexName; - IndexMetadata.Builder targetIndexMetadataBuilder = IndexMetadata.builder(targetIndex).settings(settings(Version.CURRENT)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)); + IndexMetadata.Builder targetIndexMetadataBuilder = IndexMetadata.builder(targetIndex) + .settings(settings(Version.CURRENT)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)); final IndexMetadata sourceIndexMetadata = sourceIndexMetadataBuilder.build(); - ClusterState clusterState = ClusterState.builder(emptyClusterState()).metadata( - Metadata.builder().put(sourceIndexMetadata, false).put(targetIndexMetadataBuilder).build() - ).build(); + ClusterState clusterState = ClusterState.builder(emptyClusterState()) + .metadata(Metadata.builder().put(sourceIndexMetadata, false).put(targetIndexMetadataBuilder).build()) + .build(); - CopySettingsStep copySettingsStep = new CopySettingsStep(randomStepKey(), randomStepKey(), indexPrefix, - LifecycleSettings.LIFECYCLE_NAME); + CopySettingsStep copySettingsStep = new CopySettingsStep( + randomStepKey(), + randomStepKey(), + indexPrefix, + LifecycleSettings.LIFECYCLE_NAME + ); ClusterState newClusterState = copySettingsStep.performAction(sourceIndexMetadata.getIndex(), clusterState); IndexMetadata newTargetIndexMetadata = newClusterState.metadata().index(targetIndex); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CreateSnapshotStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CreateSnapshotStepTests.java index 33a82d18938ec..1c4a18c5e4b32 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CreateSnapshotStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CreateSnapshotStepTests.java @@ -37,8 +37,12 @@ public CreateSnapshotStep createRandomInstance() { @Override protected CreateSnapshotStep copyInstance(CreateSnapshotStep instance) { - return new CreateSnapshotStep(instance.getKey(), instance.getNextKeyOnComplete(), instance.getNextKeyOnIncomplete(), - instance.getClient()); + return new CreateSnapshotStep( + instance.getKey(), + instance.getNextKeyOnComplete(), + instance.getNextKeyOnIncomplete(), + instance.getClient() + ); } @Override @@ -63,9 +67,10 @@ public void testPerformActionFailure() { String policyName = "test-ilm-policy"; { - IndexMetadata.Builder indexMetadataBuilder = - IndexMetadata.builder(indexName).settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)); + IndexMetadata.Builder indexMetadataBuilder = IndexMetadata.builder(indexName) + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)); Map ilmCustom = new HashMap<>(); String repository = "repository"; ilmCustom.put("snapshot_repository", repository); @@ -73,30 +78,38 @@ public void testPerformActionFailure() { IndexMetadata indexMetadata = indexMetadataBuilder.build(); - ClusterState clusterState = - ClusterState.builder(emptyClusterState()).metadata(Metadata.builder().put(indexMetadata, true).build()).build(); + ClusterState clusterState = ClusterState.builder(emptyClusterState()) + .metadata(Metadata.builder().put(indexMetadata, true).build()) + .build(); CreateSnapshotStep createSnapshotStep = createRandomInstance(); - Exception e = expectThrows(IllegalStateException.class, () -> PlainActionFuture.get( - f -> createSnapshotStep.performAction(indexMetadata, clusterState, null, f))); - assertThat(e.getMessage(), - is("snapshot name was not generated for policy [" + policyName + "] and index [" + indexName + "]")); + Exception e = expectThrows( + IllegalStateException.class, + () -> PlainActionFuture.get(f -> createSnapshotStep.performAction(indexMetadata, clusterState, null, f)) + ); + assertThat(e.getMessage(), is("snapshot name was not generated for policy [" + policyName + "] and index [" + indexName + "]")); } { - IndexMetadata.Builder indexMetadataBuilder = - IndexMetadata.builder(indexName).settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)); + IndexMetadata.Builder indexMetadataBuilder = IndexMetadata.builder(indexName) + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)); IndexMetadata indexMetadata = indexMetadataBuilder.build(); - ClusterState clusterState = - ClusterState.builder(emptyClusterState()).metadata(Metadata.builder().put(indexMetadata, true).build()).build(); + ClusterState clusterState = ClusterState.builder(emptyClusterState()) + .metadata(Metadata.builder().put(indexMetadata, true).build()) + .build(); CreateSnapshotStep createSnapshotStep = createRandomInstance(); - Exception e = expectThrows(IllegalStateException.class, () -> PlainActionFuture.get( - f -> createSnapshotStep.performAction(indexMetadata, clusterState, null, f))); - assertThat(e.getMessage(), - is("snapshot repository is not present for policy [" + policyName + "] and index [" + indexName + "]")); + Exception e = expectThrows( + IllegalStateException.class, + () -> PlainActionFuture.get(f -> createSnapshotStep.performAction(indexMetadata, clusterState, null, f)) + ); + assertThat( + e.getMessage(), + is("snapshot repository is not present for policy [" + policyName + "] and index [" + indexName + "]") + ); } } @@ -109,25 +122,25 @@ public void testPerformAction() { String repository = "repository"; ilmCustom.put("snapshot_repository", repository); - IndexMetadata.Builder indexMetadataBuilder = - IndexMetadata.builder(indexName).settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) - .putCustom(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY, ilmCustom) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)); + IndexMetadata.Builder indexMetadataBuilder = IndexMetadata.builder(indexName) + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) + .putCustom(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY, ilmCustom) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)); IndexMetadata indexMetadata = indexMetadataBuilder.build(); - ClusterState clusterState = - ClusterState.builder(emptyClusterState()).metadata(Metadata.builder().put(indexMetadata, true).build()).build(); + ClusterState clusterState = ClusterState.builder(emptyClusterState()) + .metadata(Metadata.builder().put(indexMetadata, true).build()) + .build(); try (NoOpClient client = getCreateSnapshotRequestAssertingClient(repository, snapshotName, indexName)) { CreateSnapshotStep step = new CreateSnapshotStep(randomStepKey(), randomStepKey(), randomStepKey(), client); step.performAction(indexMetadata, clusterState, null, new ActionListener<>() { @Override - public void onResponse(Void complete) { - } + public void onResponse(Void complete) {} @Override - public void onFailure(Exception e) { - } + public void onFailure(Exception e) {} }); } } @@ -141,20 +154,21 @@ public void testNextStepKey() { String repository = "repository"; ilmCustom.put("snapshot_repository", repository); - IndexMetadata.Builder indexMetadataBuilder = - IndexMetadata.builder(indexName).settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) - .putCustom(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY, ilmCustom) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)); + IndexMetadata.Builder indexMetadataBuilder = IndexMetadata.builder(indexName) + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) + .putCustom(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY, ilmCustom) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)); IndexMetadata indexMetadata = indexMetadataBuilder.build(); - ClusterState clusterState = - ClusterState.builder(emptyClusterState()).metadata(Metadata.builder().put(indexMetadata, true).build()).build(); + ClusterState clusterState = ClusterState.builder(emptyClusterState()) + .metadata(Metadata.builder().put(indexMetadata, true).build()) + .build(); { try (NoOpClient client = new NoOpClient(getTestName())) { StepKey nextKeyOnComplete = randomStepKey(); StepKey nextKeyOnIncomplete = randomStepKey(); - CreateSnapshotStep completeStep = new CreateSnapshotStep(randomStepKey(), nextKeyOnComplete, nextKeyOnIncomplete, - client) { + CreateSnapshotStep completeStep = new CreateSnapshotStep(randomStepKey(), nextKeyOnComplete, nextKeyOnIncomplete, client) { @Override void createSnapshot(IndexMetadata indexMetadata, ActionListener listener) { listener.onResponse(true); @@ -179,8 +193,12 @@ public void onFailure(Exception e) { try (NoOpClient client = new NoOpClient(getTestName())) { StepKey nextKeyOnComplete = randomStepKey(); StepKey nextKeyOnIncomplete = randomStepKey(); - CreateSnapshotStep incompleteStep = new CreateSnapshotStep(randomStepKey(), nextKeyOnComplete, nextKeyOnIncomplete, - client) { + CreateSnapshotStep incompleteStep = new CreateSnapshotStep( + randomStepKey(), + nextKeyOnComplete, + nextKeyOnIncomplete, + client + ) { @Override void createSnapshot(IndexMetadata indexMetadata, ActionListener listener) { listener.onResponse(false); @@ -205,9 +223,11 @@ public void onFailure(Exception e) { private NoOpClient getCreateSnapshotRequestAssertingClient(String expectedRepoName, String expectedSnapshotName, String indexName) { return new NoOpClient(getTestName()) { @Override - protected void doExecute(ActionType action, - Request request, - ActionListener listener) { + protected void doExecute( + ActionType action, + Request request, + ActionListener listener + ) { assertThat(action.name(), is(CreateSnapshotAction.NAME)); assertTrue(request instanceof CreateSnapshotRequest); CreateSnapshotRequest createSnapshotRequest = (CreateSnapshotRequest) request; @@ -215,10 +235,16 @@ protected void assertThat(createSnapshotRequest.indices()[0], is(indexName)); assertThat(createSnapshotRequest.repository(), is(expectedRepoName)); assertThat(createSnapshotRequest.snapshot(), is(expectedSnapshotName)); - assertThat(CreateSnapshotStep.NAME + " waits for the create snapshot request to complete", - createSnapshotRequest.waitForCompletion(), is(true)); - assertThat("ILM generated snapshots should not include global state", createSnapshotRequest.includeGlobalState(), - is(false)); + assertThat( + CreateSnapshotStep.NAME + " waits for the create snapshot request to complete", + createSnapshotRequest.waitForCompletion(), + is(true) + ); + assertThat( + "ILM generated snapshots should not include global state", + createSnapshotRequest.includeGlobalState(), + is(false) + ); } }; } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/DataTierMigrationRoutedStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/DataTierMigrationRoutedStepTests.java index 6add879cba1b0..432840eec0bbf 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/DataTierMigrationRoutedStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/DataTierMigrationRoutedStepTests.java @@ -17,9 +17,9 @@ import org.elasticsearch.cluster.routing.RoutingTable; import org.elasticsearch.cluster.routing.ShardRoutingState; import org.elasticsearch.cluster.routing.TestShardRouting; +import org.elasticsearch.cluster.routing.allocation.DataTier; import org.elasticsearch.index.Index; import org.elasticsearch.index.shard.ShardId; -import org.elasticsearch.cluster.routing.allocation.DataTier; import org.elasticsearch.xpack.core.ilm.ClusterStateWaitStep.Result; import org.elasticsearch.xpack.core.ilm.Step.StepKey; import org.elasticsearch.xpack.core.ilm.step.info.AllocationInfo; @@ -50,14 +50,14 @@ public DataTierMigrationRoutedStep mutateInstance(DataTierMigrationRoutedStep in StepKey nextKey = instance.getNextStepKey(); switch (between(0, 1)) { - case 0: - key = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); - break; - case 1: - nextKey = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); - break; - default: - throw new AssertionError("Illegal randomisation branch"); + case 0: + key = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); + break; + case 1: + nextKey = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); + break; + default: + throw new AssertionError("Illegal randomisation branch"); } return new DataTierMigrationRoutedStep(key, nextKey); @@ -69,21 +69,30 @@ public DataTierMigrationRoutedStep copyInstance(DataTierMigrationRoutedStep inst } public void testExecuteWithUnassignedShard() { - IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLengthBetween(5, 10)).settings(settings(Version.CURRENT)) - .numberOfShards(1).numberOfReplicas(1).build(); + IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLengthBetween(5, 10)) + .settings(settings(Version.CURRENT)) + .numberOfShards(1) + .numberOfReplicas(1) + .build(); Index index = indexMetadata.getIndex(); IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index) - .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node1", true, ShardRoutingState.STARTED)) - .addShard(TestShardRouting.newShardRouting(new ShardId(index, 1), null, null, true, ShardRoutingState.UNASSIGNED, - randomUnassignedInfo("the shard is intentionally unassigned"))); - - ClusterState clusterState = - ClusterState.builder(ClusterState.EMPTY_STATE).metadata(Metadata.builder().put(indexMetadata, true).build()) - .nodes(DiscoveryNodes.builder() - .add(newNode("node1", Collections.singleton(DiscoveryNodeRole.DATA_HOT_NODE_ROLE))) + .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node1", true, ShardRoutingState.STARTED)) + .addShard( + TestShardRouting.newShardRouting( + new ShardId(index, 1), + null, + null, + true, + ShardRoutingState.UNASSIGNED, + randomUnassignedInfo("the shard is intentionally unassigned") ) - .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) - .build(); + ); + + ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE) + .metadata(Metadata.builder().put(indexMetadata, true).build()) + .nodes(DiscoveryNodes.builder().add(newNode("node1", Collections.singleton(DiscoveryNodeRole.DATA_HOT_NODE_ROLE)))) + .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) + .build(); DataTierMigrationRoutedStep step = createRandomInstance(); Result expectedResult = new Result(false, waitingForActiveShardsAllocationInfo(1)); @@ -95,23 +104,36 @@ public void testExecuteWithUnassignedShard() { public void testExecuteWithPendingShards() { IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLengthBetween(5, 10)) .settings(settings(Version.CURRENT).put(TIER_PREFERENCE, DataTier.DATA_WARM)) - .numberOfShards(1).numberOfReplicas(0).build(); + .numberOfShards(1) + .numberOfReplicas(0) + .build(); Index index = indexMetadata.getIndex(); IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index) .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node1", true, ShardRoutingState.STARTED)); - ClusterState clusterState = - ClusterState.builder(ClusterState.EMPTY_STATE).metadata(Metadata.builder().put(indexMetadata, true).build()) - .nodes(DiscoveryNodes.builder() + ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE) + .metadata(Metadata.builder().put(indexMetadata, true).build()) + .nodes( + DiscoveryNodes.builder() .add(newNode("node1", Collections.singleton(DiscoveryNodeRole.DATA_HOT_NODE_ROLE))) .add(newNode("node2", Collections.singleton(DiscoveryNodeRole.DATA_WARM_NODE_ROLE))) - ) - .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) - .build(); + ) + .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) + .build(); DataTierMigrationRoutedStep step = createRandomInstance(); - Result expectedResult = new Result(false, new AllocationInfo(0, 1, true, - "[" + index.getName() + "] lifecycle action [" + step.getKey().getAction() + "] waiting for " + - "[1] shards to be moved to the [data_warm] tier (tier migration preference configuration is [data_warm])") + Result expectedResult = new Result( + false, + new AllocationInfo( + 0, + 1, + true, + "[" + + index.getName() + + "] lifecycle action [" + + step.getKey().getAction() + + "] waiting for " + + "[1] shards to be moved to the [data_warm] tier (tier migration preference configuration is [data_warm])" + ) ); Result actualResult = step.isConditionMet(index, clusterState); @@ -122,22 +144,31 @@ public void testExecuteWithPendingShards() { public void testExecuteWithPendingShardsAndTargetRoleNotPresentInCluster() { IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLengthBetween(5, 10)) .settings(settings(Version.CURRENT).put(TIER_PREFERENCE, DataTier.DATA_WARM)) - .numberOfShards(1).numberOfReplicas(0).build(); + .numberOfShards(1) + .numberOfReplicas(0) + .build(); Index index = indexMetadata.getIndex(); IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index) .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node1", true, ShardRoutingState.STARTED)); - ClusterState clusterState = - ClusterState.builder(ClusterState.EMPTY_STATE).metadata(Metadata.builder().put(indexMetadata, true).build()) - .nodes(DiscoveryNodes.builder() - .add(newNode("node1", Collections.singleton(DiscoveryNodeRole.DATA_HOT_NODE_ROLE))) - ) - .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) - .build(); + ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE) + .metadata(Metadata.builder().put(indexMetadata, true).build()) + .nodes(DiscoveryNodes.builder().add(newNode("node1", Collections.singleton(DiscoveryNodeRole.DATA_HOT_NODE_ROLE)))) + .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) + .build(); DataTierMigrationRoutedStep step = createRandomInstance(); - Result expectedResult = new Result(false, new AllocationInfo(0, 1, true, - "index [" + index.getName() + "] has a preference for tiers [data_warm], but no nodes for any of those tiers are available " + - "in the cluster")); + Result expectedResult = new Result( + false, + new AllocationInfo( + 0, + 1, + true, + "index [" + + index.getName() + + "] has a preference for tiers [data_warm], but no nodes for any of those tiers are available " + + "in the cluster" + ) + ); Result actualResult = step.isConditionMet(index, clusterState); assertThat(actualResult.isComplete(), is(false)); @@ -158,19 +189,22 @@ public void testExecuteIndexMissing() { public void testExecuteIsComplete() { IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLengthBetween(5, 10)) .settings(settings(Version.CURRENT).put(TIER_PREFERENCE, DataTier.DATA_WARM)) - .numberOfShards(1).numberOfReplicas(0).build(); + .numberOfShards(1) + .numberOfReplicas(0) + .build(); Index index = indexMetadata.getIndex(); IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index) .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node2", true, ShardRoutingState.STARTED)); - ClusterState clusterState = - ClusterState.builder(ClusterState.EMPTY_STATE).metadata(Metadata.builder().put(indexMetadata, true).build()) - .nodes(DiscoveryNodes.builder() + ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE) + .metadata(Metadata.builder().put(indexMetadata, true).build()) + .nodes( + DiscoveryNodes.builder() .add(newNode("node1", Collections.singleton(DiscoveryNodeRole.DATA_HOT_NODE_ROLE))) .add(newNode("node2", Collections.singleton(DiscoveryNodeRole.DATA_WARM_NODE_ROLE))) - ) - .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) - .build(); + ) + .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) + .build(); DataTierMigrationRoutedStep step = createRandomInstance(); Result result = step.isConditionMet(index, clusterState); assertThat(result.isComplete(), is(true)); @@ -180,18 +214,18 @@ public void testExecuteIsComplete() { public void testExecuteWithGenericDataNodes() { IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLengthBetween(5, 10)) .settings(settings(Version.CURRENT).put(TIER_PREFERENCE, DataTier.DATA_WARM)) - .numberOfShards(1).numberOfReplicas(0).build(); + .numberOfShards(1) + .numberOfReplicas(0) + .build(); Index index = indexMetadata.getIndex(); IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index) .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node1", true, ShardRoutingState.STARTED)); - ClusterState clusterState = - ClusterState.builder(ClusterState.EMPTY_STATE).metadata(Metadata.builder().put(indexMetadata, true).build()) - .nodes(DiscoveryNodes.builder() - .add(newNode("node1", Collections.singleton(DiscoveryNodeRole.DATA_ROLE))) - ) - .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) - .build(); + ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE) + .metadata(Metadata.builder().put(indexMetadata, true).build()) + .nodes(DiscoveryNodes.builder().add(newNode("node1", Collections.singleton(DiscoveryNodeRole.DATA_ROLE)))) + .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) + .build(); DataTierMigrationRoutedStep step = createRandomInstance(); Result result = step.isConditionMet(index, clusterState); assertThat(result.isComplete(), is(true)); @@ -201,20 +235,20 @@ public void testExecuteWithGenericDataNodes() { public void testExecuteForIndexWithoutTierRoutingInformationWaitsForReplicasToBeActive() { IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLengthBetween(5, 10)) .settings(settings(Version.CURRENT)) - .numberOfShards(1).numberOfReplicas(1).build(); + .numberOfShards(1) + .numberOfReplicas(1) + .build(); Index index = indexMetadata.getIndex(); { IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index) .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node1", true, ShardRoutingState.STARTED)) .addReplica(); - ClusterState clusterState = - ClusterState.builder(ClusterState.EMPTY_STATE).metadata(Metadata.builder().put(indexMetadata, true).build()) - .nodes(DiscoveryNodes.builder() - .add(newNode("node1", Collections.singleton(DiscoveryNodeRole.DATA_HOT_NODE_ROLE))) - ) - .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) - .build(); + ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE) + .metadata(Metadata.builder().put(indexMetadata, true).build()) + .nodes(DiscoveryNodes.builder().add(newNode("node1", Collections.singleton(DiscoveryNodeRole.DATA_HOT_NODE_ROLE)))) + .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) + .build(); DataTierMigrationRoutedStep step = createRandomInstance(); Result expectedResult = new Result(false, waitingForActiveShardsAllocationInfo(1)); @@ -228,14 +262,15 @@ public void testExecuteForIndexWithoutTierRoutingInformationWaitsForReplicasToBe .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node1", true, ShardRoutingState.STARTED)) .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node2", false, ShardRoutingState.STARTED)); - ClusterState clusterState = - ClusterState.builder(ClusterState.EMPTY_STATE).metadata(Metadata.builder().put(indexMetadata, true).build()) - .nodes(DiscoveryNodes.builder() + ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE) + .metadata(Metadata.builder().put(indexMetadata, true).build()) + .nodes( + DiscoveryNodes.builder() .add(newNode("node1", Collections.singleton(DiscoveryNodeRole.DATA_HOT_NODE_ROLE))) .add(newNode("node2", Collections.singleton(DiscoveryNodeRole.DATA_WARM_NODE_ROLE))) - ) - .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) - .build(); + ) + .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) + .build(); DataTierMigrationRoutedStep step = createRandomInstance(); Result result = step.isConditionMet(index, clusterState); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/DeleteActionTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/DeleteActionTests.java index 350bedb081a29..096723c2c9968 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/DeleteActionTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/DeleteActionTests.java @@ -32,8 +32,11 @@ protected Reader instanceReader() { public void testToSteps() { String phase = randomAlphaOfLengthBetween(1, 10); - StepKey nextStepKey = new StepKey(randomAlphaOfLengthBetween(1, 10), randomAlphaOfLengthBetween(1, 10), - randomAlphaOfLengthBetween(1, 10)); + StepKey nextStepKey = new StepKey( + randomAlphaOfLengthBetween(1, 10), + randomAlphaOfLengthBetween(1, 10), + randomAlphaOfLengthBetween(1, 10) + ); { DeleteAction action = new DeleteAction(true); List steps = action.toSteps(null, phase, nextStepKey); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/DeleteStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/DeleteStepTests.java index 3c3fb9c639509..bd9d3c42a77f8 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/DeleteStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/DeleteStepTests.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.xpack.core.ilm; - import org.elasticsearch.Version; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; @@ -40,14 +39,14 @@ public DeleteStep mutateInstance(DeleteStep instance) { StepKey nextKey = instance.getNextStepKey(); switch (between(0, 1)) { - case 0: - key = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); - break; - case 1: - nextKey = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); - break; - default: - throw new AssertionError("Illegal randomisation branch"); + case 0: + key = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); + break; + case 1: + nextKey = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); + break; + default: + throw new AssertionError("Illegal randomisation branch"); } return new DeleteStep(key, nextKey, instance.getClient()); @@ -59,8 +58,11 @@ public DeleteStep copyInstance(DeleteStep instance) { } private static IndexMetadata getIndexMetadata() { - return IndexMetadata.builder(randomAlphaOfLength(10)).settings(settings(Version.CURRENT)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + return IndexMetadata.builder(randomAlphaOfLength(10)) + .settings(settings(Version.CURRENT)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); } public void testIndexSurvives() { @@ -71,20 +73,20 @@ public void testDeleted() throws Exception { IndexMetadata indexMetadata = getIndexMetadata(); Mockito.doAnswer(invocation -> { - DeleteIndexRequest request = (DeleteIndexRequest) invocation.getArguments()[0]; - @SuppressWarnings("unchecked") - ActionListener listener = (ActionListener) invocation.getArguments()[1]; - assertNotNull(request); - assertEquals(1, request.indices().length); - assertEquals(indexMetadata.getIndex().getName(), request.indices()[0]); - listener.onResponse(null); - return null; + DeleteIndexRequest request = (DeleteIndexRequest) invocation.getArguments()[0]; + @SuppressWarnings("unchecked") + ActionListener listener = (ActionListener) invocation.getArguments()[1]; + assertNotNull(request); + assertEquals(1, request.indices().length); + assertEquals(indexMetadata.getIndex().getName(), request.indices()[0]); + listener.onResponse(null); + return null; }).when(indicesClient).delete(Mockito.any(), Mockito.any()); DeleteStep step = createRandomInstance(); - ClusterState clusterState = ClusterState.builder(emptyClusterState()).metadata( - Metadata.builder().put(indexMetadata, true).build() - ).build(); + ClusterState clusterState = ClusterState.builder(emptyClusterState()) + .metadata(Metadata.builder().put(indexMetadata, true).build()) + .build(); PlainActionFuture.get(f -> step.performAction(indexMetadata, clusterState, null, f)); Mockito.verify(client, Mockito.only()).admin(); @@ -108,11 +110,16 @@ public void testExceptionThrown() { }).when(indicesClient).delete(Mockito.any(), Mockito.any()); DeleteStep step = createRandomInstance(); - ClusterState clusterState = ClusterState.builder(emptyClusterState()).metadata( - Metadata.builder().put(indexMetadata, true).build() - ).build(); - assertSame(exception, expectThrows(Exception.class, () -> PlainActionFuture.get( - f -> step.performAction(indexMetadata, clusterState, null, f)))); + ClusterState clusterState = ClusterState.builder(emptyClusterState()) + .metadata(Metadata.builder().put(indexMetadata, true).build()) + .build(); + assertSame( + exception, + expectThrows( + Exception.class, + () -> PlainActionFuture.get(f -> step.performAction(indexMetadata, clusterState, null, f)) + ) + ); } public void testPerformActionThrowsExceptionIfIndexIsTheDataStreamWriteIndex() { @@ -139,13 +146,17 @@ public void testPerformActionThrowsExceptionIfIndexIsTheDataStreamWriteIndex() { .build(); } - DataStream dataStream = - new DataStream(dataStreamName, createTimestampField("@timestamp"), List.of(index1.getIndex(), sourceIndexMetadata.getIndex())); - ClusterState clusterState = ClusterState.builder(emptyClusterState()).metadata( - Metadata.builder().put(index1, false).put(sourceIndexMetadata, false).put(dataStream).build() - ).build(); + DataStream dataStream = new DataStream( + dataStreamName, + createTimestampField("@timestamp"), + List.of(index1.getIndex(), sourceIndexMetadata.getIndex()) + ); + ClusterState clusterState = ClusterState.builder(emptyClusterState()) + .metadata(Metadata.builder().put(index1, false).put(sourceIndexMetadata, false).put(dataStream).build()) + .build(); - IllegalStateException illegalStateException = expectThrows(IllegalStateException.class, + IllegalStateException illegalStateException = expectThrows( + IllegalStateException.class, () -> createRandomInstance().performDuringNoSnapshot(sourceIndexMetadata, clusterState, new ActionListener<>() { @Override public void onResponse(Void complete) { @@ -156,7 +167,8 @@ public void onResponse(Void complete) { public void onFailure(Exception e) { fail("unexpected listener callback"); } - })); + }) + ); assertThat( illegalStateException.getMessage(), is( diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ExplainLifecycleRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ExplainLifecycleRequestTests.java index d88cbabcf727a..b94b65b8c8073 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ExplainLifecycleRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ExplainLifecycleRequestTests.java @@ -23,8 +23,16 @@ protected ExplainLifecycleRequest createTestInstance() { request.indices(generateRandomStringArray(20, 20, false, false)); } if (randomBoolean()) { - IndicesOptions indicesOptions = IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), - randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean()); + IndicesOptions indicesOptions = IndicesOptions.fromOptions( + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean() + ); request.indicesOptions(indicesOptions); } if (randomBoolean()) { @@ -44,12 +52,25 @@ protected ExplainLifecycleRequest mutateInstance(ExplainLifecycleRequest instanc boolean onlyManaged = instance.onlyManaged(); switch (between(0, 3)) { case 0: - indices = randomValueOtherThanMany(i -> Arrays.equals(i, instance.indices()), - () -> generateRandomStringArray(20, 10, false, false)); + indices = randomValueOtherThanMany( + i -> Arrays.equals(i, instance.indices()), + () -> generateRandomStringArray(20, 10, false, false) + ); break; case 1: - indicesOptions = randomValueOtherThan(indicesOptions, () -> IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), - randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean())); + indicesOptions = randomValueOtherThan( + indicesOptions, + () -> IndicesOptions.fromOptions( + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean() + ) + ); break; case 2: onlyErrors = onlyErrors == false; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ExplainLifecycleResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ExplainLifecycleResponseTests.java index cc9924c26bae8..a98abe3edb93d 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ExplainLifecycleResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ExplainLifecycleResponseTests.java @@ -8,13 +8,13 @@ package org.elasticsearch.xpack.core.ilm; import org.elasticsearch.cluster.ClusterModule; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.util.CollectionUtils; +import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractSerializingTestCase; import java.io.IOException; import java.util.Arrays; @@ -62,13 +62,18 @@ protected boolean assertToXContentEquivalence() { } protected NamedWriteableRegistry getNamedWriteableRegistry() { - return new NamedWriteableRegistry(Arrays - .asList(new NamedWriteableRegistry.Entry(LifecycleAction.class, MockAction.NAME, MockAction::new))); + return new NamedWriteableRegistry( + Arrays.asList(new NamedWriteableRegistry.Entry(LifecycleAction.class, MockAction.NAME, MockAction::new)) + ); } @Override protected NamedXContentRegistry xContentRegistry() { - return new NamedXContentRegistry(CollectionUtils.appendToCopy(ClusterModule.getNamedXWriteables(), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(MockAction.NAME), MockAction::parse))); + return new NamedXContentRegistry( + CollectionUtils.appendToCopy( + ClusterModule.getNamedXWriteables(), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(MockAction.NAME), MockAction::parse) + ) + ); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ForceMergeActionTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ForceMergeActionTests.java index 11c4264f5e400..ed50b12495253 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ForceMergeActionTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ForceMergeActionTests.java @@ -8,15 +8,15 @@ import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.io.stream.Writeable.Reader; -import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.core.Tuple; +import org.elasticsearch.index.codec.CodecService; +import org.elasticsearch.index.engine.EngineConfig; +import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.index.codec.CodecService; -import org.elasticsearch.index.engine.EngineConfig; import org.elasticsearch.xpack.core.ilm.Step.StepKey; import java.io.IOException; @@ -74,8 +74,10 @@ private void assertNonBestCompression(ForceMergeAction instance) { ForceMergeStep fourthStep = (ForceMergeStep) steps.get(3); SegmentCountStep fifthStep = (SegmentCountStep) steps.get(4); - assertThat(firstStep.getKey(), - equalTo(new StepKey(phase, ForceMergeAction.NAME, ForceMergeAction.CONDITIONAL_SKIP_FORCE_MERGE_STEP))); + assertThat( + firstStep.getKey(), + equalTo(new StepKey(phase, ForceMergeAction.NAME, ForceMergeAction.CONDITIONAL_SKIP_FORCE_MERGE_STEP)) + ); assertThat(secondStep.getKey(), equalTo(new StepKey(phase, ForceMergeAction.NAME, CheckNotDataStreamWriteIndexStep.NAME))); assertThat(secondStep.getNextStepKey(), equalTo(new StepKey(phase, ForceMergeAction.NAME, ReadOnlyAction.NAME))); assertThat(thirdStep.getKey(), equalTo(new StepKey(phase, ForceMergeAction.NAME, ReadOnlyAction.NAME))); @@ -107,17 +109,23 @@ private void assertBestCompression(ForceMergeAction instance) { StepKey waitForGreen = new StepKey(phase, ForceMergeAction.NAME, WaitForIndexColorStep.NAME); StepKey forceMerge = new StepKey(phase, ForceMergeAction.NAME, ForceMergeStep.NAME); StepKey segmentCount = new StepKey(phase, ForceMergeAction.NAME, SegmentCountStep.NAME); - assertThat(steps.get(0).getKey(), is(new StepKey(phase, ForceMergeAction.NAME, - ForceMergeAction.CONDITIONAL_SKIP_FORCE_MERGE_STEP))); - assertThat(stepKeys, contains( - new Tuple<>(checkNotWriteIndex, readOnly), - new Tuple<>(readOnly, closeIndex), - new Tuple<>(closeIndex, updateCodec), - new Tuple<>(updateCodec, openIndex), - new Tuple<>(openIndex, waitForGreen), - new Tuple<>(waitForGreen, forceMerge), - new Tuple<>(forceMerge, segmentCount), - new Tuple<>(segmentCount, nextStepKey))); + assertThat( + steps.get(0).getKey(), + is(new StepKey(phase, ForceMergeAction.NAME, ForceMergeAction.CONDITIONAL_SKIP_FORCE_MERGE_STEP)) + ); + assertThat( + stepKeys, + contains( + new Tuple<>(checkNotWriteIndex, readOnly), + new Tuple<>(readOnly, closeIndex), + new Tuple<>(closeIndex, updateCodec), + new Tuple<>(updateCodec, openIndex), + new Tuple<>(openIndex, waitForGreen), + new Tuple<>(waitForGreen, forceMerge), + new Tuple<>(forceMerge, segmentCount), + new Tuple<>(segmentCount, nextStepKey) + ) + ); UpdateSettingsStep thirdStep = (UpdateSettingsStep) steps.get(2); UpdateSettingsStep fifthStep = (UpdateSettingsStep) steps.get(4); @@ -128,21 +136,26 @@ private void assertBestCompression(ForceMergeAction instance) { public void testMissingMaxNumSegments() throws IOException { BytesReference emptyObject = BytesReference.bytes(JsonXContent.contentBuilder().startObject().endObject()); - XContentParser parser = XContentHelper.createParser(null, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, - emptyObject, XContentType.JSON); + XContentParser parser = XContentHelper.createParser( + null, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + emptyObject, + XContentType.JSON + ); Exception e = expectThrows(IllegalArgumentException.class, () -> ForceMergeAction.parse(parser)); assertThat(e.getMessage(), equalTo("Required [max_num_segments]")); } public void testInvalidNegativeSegmentNumber() { - Exception r = expectThrows(IllegalArgumentException.class, () -> new - ForceMergeAction(randomIntBetween(-10, 0), null)); + Exception r = expectThrows(IllegalArgumentException.class, () -> new ForceMergeAction(randomIntBetween(-10, 0), null)); assertThat(r.getMessage(), equalTo("[max_num_segments] must be a positive integer")); } public void testInvalidCodec() { - Exception r = expectThrows(IllegalArgumentException.class, () -> new - ForceMergeAction(randomIntBetween(1, 10), "DummyCompressingStoredFields")); + Exception r = expectThrows( + IllegalArgumentException.class, + () -> new ForceMergeAction(randomIntBetween(1, 10), "DummyCompressingStoredFields") + ); assertThat(r.getMessage(), equalTo("unknown index codec: [DummyCompressingStoredFields]")); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ForceMergeStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ForceMergeStepTests.java index 30e047a752ea5..f7c4c52d28263 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ForceMergeStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ForceMergeStepTests.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.xpack.core.ilm; - import org.apache.lucene.util.SetOnce; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.Version; @@ -65,13 +64,15 @@ public ForceMergeStep mutateInstance(ForceMergeStep instance) { @Override public ForceMergeStep copyInstance(ForceMergeStep instance) { - return new ForceMergeStep(instance.getKey(), instance.getNextStepKey(), - instance.getClient(), instance.getMaxNumSegments()); + return new ForceMergeStep(instance.getKey(), instance.getNextStepKey(), instance.getClient(), instance.getMaxNumSegments()); } public void testPerformActionComplete() throws Exception { - IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)).settings(settings(Version.CURRENT)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) + .settings(settings(Version.CURRENT)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); Step.StepKey stepKey = randomStepKey(); StepKey nextStepKey = randomStepKey(); int maxNumSegments = randomIntBetween(1, 10); @@ -91,8 +92,11 @@ public void testPerformActionComplete() throws Exception { } public void testPerformActionThrowsException() { - IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)).settings(settings(Version.CURRENT)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) + .settings(settings(Version.CURRENT)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); Exception exception = new RuntimeException("error"); Step.StepKey stepKey = randomStepKey(); StepKey nextStepKey = randomStepKey(); @@ -111,24 +115,34 @@ public void testPerformActionThrowsException() { }).when(indicesClient).forceMerge(any(), any()); ForceMergeStep step = new ForceMergeStep(stepKey, nextStepKey, client, maxNumSegments); - assertSame(exception, expectThrows(Exception.class, () -> PlainActionFuture.get( - f -> step.performAction(indexMetadata, null, null, f)))); + assertSame( + exception, + expectThrows( + Exception.class, + () -> PlainActionFuture.get(f -> step.performAction(indexMetadata, null, null, f)) + ) + ); } public void testForcemergeFailsOnSomeShards() { int numberOfShards = randomIntBetween(2, 5); IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, "ilmPolicy")) - .numberOfShards(numberOfShards).numberOfReplicas(randomIntBetween(0, 5)).build(); + .numberOfShards(numberOfShards) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); Index index = indexMetadata.getIndex(); ForceMergeResponse forceMergeResponse = Mockito.mock(ForceMergeResponse.class); Mockito.when(forceMergeResponse.getTotalShards()).thenReturn(numberOfShards); Mockito.when(forceMergeResponse.getFailedShards()).thenReturn(numberOfShards - 1); Mockito.when(forceMergeResponse.getStatus()).thenReturn(RestStatus.BAD_REQUEST); Mockito.when(forceMergeResponse.getSuccessfulShards()).thenReturn(1); - DefaultShardOperationFailedException cause = - new DefaultShardOperationFailedException(index.getName(), 0, new IllegalArgumentException("couldn't merge")); - Mockito.when(forceMergeResponse.getShardFailures()).thenReturn(new DefaultShardOperationFailedException[]{cause}); + DefaultShardOperationFailedException cause = new DefaultShardOperationFailedException( + index.getName(), + 0, + new IllegalArgumentException("couldn't merge") + ); + Mockito.when(forceMergeResponse.getShardFailures()).thenReturn(new DefaultShardOperationFailedException[] { cause }); Step.StepKey stepKey = randomStepKey(); StepKey nextStepKey = randomStepKey(); @@ -142,8 +156,9 @@ public void testForcemergeFailsOnSomeShards() { SetOnce failedStep = new SetOnce<>(); - ClusterState state = - ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder().put(indexMetadata, true).build()).build(); + ClusterState state = ClusterState.builder(ClusterName.DEFAULT) + .metadata(Metadata.builder().put(indexMetadata, true).build()) + .build(); ForceMergeStep step = new ForceMergeStep(stepKey, nextStepKey, client, 1); step.performAction(indexMetadata, state, null, new ActionListener<>() { @Override @@ -153,19 +168,23 @@ public void onResponse(Void aBoolean) { @Override public void onFailure(Exception e) { - assert e instanceof ElasticsearchException : "step must report " + ElasticsearchException.class.getSimpleName() + - " but was " + e; + assert e instanceof ElasticsearchException + : "step must report " + ElasticsearchException.class.getSimpleName() + " but was " + e; failedStep.set((ElasticsearchException) e); } }); ElasticsearchException stepException = failedStep.get(); assertThat(stepException, notNullValue()); - assertThat(stepException.getMessage(), + assertThat( + stepException.getMessage(), is( - "index [" + index.getName() + "] in policy [ilmPolicy] encountered failures [{\"shard\":0,\"index\":\"" + - index.getName() + "\",\"status\":\"BAD_REQUEST\",\"reason\":{\"type\":\"illegal_argument_exception\"," + - "\"reason\":\"couldn't merge\"}}] on step [forcemerge]" + "index [" + + index.getName() + + "] in policy [ilmPolicy] encountered failures [{\"shard\":0,\"index\":\"" + + index.getName() + + "\",\"status\":\"BAD_REQUEST\",\"reason\":{\"type\":\"illegal_argument_exception\"," + + "\"reason\":\"couldn't merge\"}}] on step [forcemerge]" ) ); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/FreezeActionTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/FreezeActionTests.java index 7e48a6fa01dd6..2e460e8559af5 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/FreezeActionTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/FreezeActionTests.java @@ -35,8 +35,11 @@ protected Reader instanceReader() { public void testToSteps() { FreezeAction action = createTestInstance(); String phase = randomAlphaOfLengthBetween(1, 10); - StepKey nextStepKey = new StepKey(randomAlphaOfLengthBetween(1, 10), randomAlphaOfLengthBetween(1, 10), - randomAlphaOfLengthBetween(1, 10)); + StepKey nextStepKey = new StepKey( + randomAlphaOfLengthBetween(1, 10), + randomAlphaOfLengthBetween(1, 10), + randomAlphaOfLengthBetween(1, 10) + ); List steps = action.toSteps(null, phase, nextStepKey); assertNotNull(steps); assertEquals(3, steps.size()); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/FreezeStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/FreezeStepTests.java index f8aaf2c2de09d..f4b36c5948382 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/FreezeStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/FreezeStepTests.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.xpack.core.ilm; - import org.elasticsearch.Version; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.xpack.core.ilm.Step.StepKey; @@ -27,14 +26,14 @@ public FreezeStep mutateInstance(FreezeStep instance) { StepKey nextKey = instance.getNextStepKey(); switch (between(0, 1)) { - case 0: - key = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); - break; - case 1: - nextKey = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); - break; - default: - throw new AssertionError("Illegal randomisation branch"); + case 0: + key = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); + break; + case 1: + nextKey = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); + break; + default: + throw new AssertionError("Illegal randomisation branch"); } return new FreezeStep(key, nextKey, instance.getClient()); @@ -46,8 +45,11 @@ public FreezeStep copyInstance(FreezeStep instance) { } private static IndexMetadata getIndexMetadata() { - return IndexMetadata.builder(randomAlphaOfLength(10)).settings(settings(Version.CURRENT)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + return IndexMetadata.builder(randomAlphaOfLength(10)) + .settings(settings(Version.CURRENT)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); } public void testIndexSurvives() { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/GenerateSnapshotNameStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/GenerateSnapshotNameStepTests.java index a93647ec5c312..5d8d810320f67 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/GenerateSnapshotNameStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/GenerateSnapshotNameStepTests.java @@ -80,10 +80,13 @@ public void testPerformAction() { RepositoryMetadata repo = new RepositoryMetadata(generateSnapshotNameStep.getSnapshotRepository(), "fs", Settings.EMPTY); ClusterState clusterState = ClusterState.builder(emptyClusterState()) - .metadata(Metadata.builder() - .put(indexMetadata, false) - .putCustom(RepositoriesMetadata.TYPE, new RepositoriesMetadata(Collections.singletonList(repo))) - .build()).build(); + .metadata( + Metadata.builder() + .put(indexMetadata, false) + .putCustom(RepositoriesMetadata.TYPE, new RepositoriesMetadata(Collections.singletonList(repo))) + .build() + ) + .build(); ClusterState newClusterState; @@ -91,8 +94,11 @@ public void testPerformAction() { newClusterState = generateSnapshotNameStep.performAction(indexMetadata.getIndex(), clusterState); LifecycleExecutionState executionState = LifecycleExecutionState.fromIndexMetadata(newClusterState.metadata().index(indexName)); assertThat(executionState.getSnapshotIndexName(), is(indexName)); - assertThat("the " + GenerateSnapshotNameStep.NAME + " step must generate a snapshot name", executionState.getSnapshotName(), - notNullValue()); + assertThat( + "the " + GenerateSnapshotNameStep.NAME + " step must generate a snapshot name", + executionState.getSnapshotName(), + notNullValue() + ); assertThat(executionState.getSnapshotRepository(), is(generateSnapshotNameStep.getSnapshotRepository())); assertThat(executionState.getSnapshotName(), containsString(indexName.toLowerCase(Locale.ROOT))); assertThat(executionState.getSnapshotName(), containsString(policyName.toLowerCase(Locale.ROOT))); @@ -117,16 +123,25 @@ public void testPerformActionRejectsNonexistentRepository() { GenerateSnapshotNameStep generateSnapshotNameStep = createRandomInstance(); ClusterState clusterState = ClusterState.builder(emptyClusterState()) - .metadata(Metadata.builder() - .put(indexMetadata, false) - .putCustom(RepositoriesMetadata.TYPE, RepositoriesMetadata.EMPTY) - .build()).build(); - - IllegalStateException illegalStateException = expectThrows(IllegalStateException.class, - () -> generateSnapshotNameStep.performAction(indexMetadata.getIndex(), clusterState)); - assertThat(illegalStateException.getMessage(), is("repository [" + generateSnapshotNameStep.getSnapshotRepository() + "] " + - "is missing. [test-ilm-policy] policy for index [" + indexName + "] cannot continue until the repository " + - "is created or the policy is changed")); + .metadata(Metadata.builder().put(indexMetadata, false).putCustom(RepositoriesMetadata.TYPE, RepositoriesMetadata.EMPTY).build()) + .build(); + + IllegalStateException illegalStateException = expectThrows( + IllegalStateException.class, + () -> generateSnapshotNameStep.performAction(indexMetadata.getIndex(), clusterState) + ); + assertThat( + illegalStateException.getMessage(), + is( + "repository [" + + generateSnapshotNameStep.getSnapshotRepository() + + "] " + + "is missing. [test-ilm-policy] policy for index [" + + indexName + + "] cannot continue until the repository " + + "is created or the policy is changed" + ) + ); } public void testPerformActionWillOverwriteCachedRepository() { @@ -150,10 +165,13 @@ public void testPerformActionWillOverwriteCachedRepository() { RepositoryMetadata repo = new RepositoryMetadata(generateSnapshotNameStep.getSnapshotRepository(), "fs", Settings.EMPTY); ClusterState clusterState = ClusterState.builder(emptyClusterState()) - .metadata(Metadata.builder() - .put(indexMetadata, false) - .putCustom(RepositoriesMetadata.TYPE, new RepositoriesMetadata(Collections.singletonList(repo))) - .build()).build(); + .metadata( + Metadata.builder() + .put(indexMetadata, false) + .putCustom(RepositoriesMetadata.TYPE, new RepositoriesMetadata(Collections.singletonList(repo))) + .build() + ) + .build(); ClusterState newClusterState = generateSnapshotNameStep.performAction(indexMetadata.getIndex(), clusterState); @@ -193,8 +211,10 @@ public void testNameValidation() { { ActionRequestValidationException validationException = validateGeneratedSnapshotName("_start", generateSnapshotName("_start")); assertThat(validationException, notNullValue()); - assertThat(validationException.validationErrors(), containsInAnyOrder("invalid snapshot name [_start]: must not start with " + - "'_'")); + assertThat( + validationException.validationErrors(), + containsInAnyOrder("invalid snapshot name [_start]: must not start with " + "'_'") + ); } { ActionRequestValidationException validationException = validateGeneratedSnapshotName("aBcD", generateSnapshotName("aBcD")); @@ -204,8 +224,14 @@ public void testNameValidation() { { ActionRequestValidationException validationException = validateGeneratedSnapshotName("na>me", generateSnapshotName("na>me")); assertThat(validationException, notNullValue()); - assertThat(validationException.validationErrors(), containsInAnyOrder("invalid snapshot name [na>me]: must not contain " + - "contain the following characters " + Strings.INVALID_FILENAME_CHARS)); + assertThat( + validationException.validationErrors(), + containsInAnyOrder( + "invalid snapshot name [na>me]: must not contain " + + "contain the following characters " + + Strings.INVALID_FILENAME_CHARS + ) + ); } } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/GenerateUniqueIndexNameStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/GenerateUniqueIndexNameStepTests.java index 56bf5a934b18c..5061a04836248 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/GenerateUniqueIndexNameStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/GenerateUniqueIndexNameStepTests.java @@ -70,27 +70,36 @@ protected GenerateUniqueIndexNameStep mutateInstance(GenerateUniqueIndexNameStep @Override protected GenerateUniqueIndexNameStep copyInstance(GenerateUniqueIndexNameStep instance) { - return new GenerateUniqueIndexNameStep(instance.getKey(), instance.getNextStepKey(), instance.prefix(), - instance.lifecycleStateSetter()); + return new GenerateUniqueIndexNameStep( + instance.getKey(), + instance.getNextStepKey(), + instance.prefix(), + instance.lifecycleStateSetter() + ); } public void testPerformAction() { String indexName = randomAlphaOfLength(10); String policyName = "test-ilm-policy"; - IndexMetadata.Builder indexMetadataBuilder = - IndexMetadata.builder(indexName).settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)); + IndexMetadata.Builder indexMetadataBuilder = IndexMetadata.builder(indexName) + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)); final IndexMetadata indexMetadata = indexMetadataBuilder.build(); ClusterState clusterState = ClusterState.builder(emptyClusterState()) - .metadata(Metadata.builder().put(indexMetadata, false).build()).build(); + .metadata(Metadata.builder().put(indexMetadata, false).build()) + .build(); GenerateUniqueIndexNameStep generateUniqueIndexNameStep = createRandomInstance(); ClusterState newClusterState = generateUniqueIndexNameStep.performAction(indexMetadata.getIndex(), clusterState); LifecycleExecutionState executionState = LifecycleExecutionState.fromIndexMetadata(newClusterState.metadata().index(indexName)); - assertThat("the " + GenerateUniqueIndexNameStep.NAME + " step must generate an index name", executionState.getShrinkIndexName(), - notNullValue()); + assertThat( + "the " + GenerateUniqueIndexNameStep.NAME + " step must generate an index name", + executionState.getShrinkIndexName(), + notNullValue() + ); assertThat(executionState.getShrinkIndexName(), containsString(indexName)); assertThat(executionState.getShrinkIndexName(), startsWith(generateUniqueIndexNameStep.prefix())); } @@ -118,8 +127,10 @@ public void testGenerateValidIndexSuffix() { } { - IllegalArgumentException illegalArgumentException = expectThrows(IllegalArgumentException.class, - () -> generateValidIndexSuffix(() -> "****???><><>,# \\/:||")); + IllegalArgumentException illegalArgumentException = expectThrows( + IllegalArgumentException.class, + () -> generateValidIndexSuffix(() -> "****???><><>,# \\/:||") + ); assertThat(illegalArgumentException.getMessage(), is("unable to generate random index name suffix")); } @@ -130,48 +141,59 @@ public void testGenerateValidIndexSuffix() { public void testValidateGeneratedIndexName() { { - assertThat(validateGeneratedIndexName( - generateValidIndexName(randomAlphaOfLengthBetween(5, 10), randomAlphaOfLengthBetween(5, 150)), ClusterState.EMPTY_STATE - ), nullValue()); + assertThat( + validateGeneratedIndexName( + generateValidIndexName(randomAlphaOfLengthBetween(5, 10), randomAlphaOfLengthBetween(5, 150)), + ClusterState.EMPTY_STATE + ), + nullValue() + ); } { // index name is validated (invalid chars etc) String generatedIndexName = generateValidIndexName("_prefix-", randomAlphaOfLengthBetween(5, 150)); - assertThat(validateGeneratedIndexName(generatedIndexName, ClusterState.EMPTY_STATE).validationErrors(), containsInAnyOrder( - "Invalid index name [" + generatedIndexName + "], must not start with '_', '-', or '+'")); + assertThat( + validateGeneratedIndexName(generatedIndexName, ClusterState.EMPTY_STATE).validationErrors(), + containsInAnyOrder("Invalid index name [" + generatedIndexName + "], must not start with '_', '-', or '+'") + ); } { // index name is validated (invalid chars etc) String generatedIndexName = generateValidIndexName("shrink-", "shrink-indexName-random###"); - assertThat(validateGeneratedIndexName(generatedIndexName, ClusterState.EMPTY_STATE).validationErrors(), containsInAnyOrder( - "Invalid index name [" + generatedIndexName + "], must not contain '#'")); + assertThat( + validateGeneratedIndexName(generatedIndexName, ClusterState.EMPTY_STATE).validationErrors(), + containsInAnyOrder("Invalid index name [" + generatedIndexName + "], must not contain '#'") + ); } { // generated index already exists as a standalone index String generatedIndexName = generateValidIndexName(randomAlphaOfLengthBetween(5, 10), randomAlphaOfLengthBetween(5, 150)); IndexMetadata indexMetadata = IndexMetadata.builder(generatedIndexName) - .settings(settings(Version.CURRENT)).numberOfShards(randomIntBetween(1,5)) - .numberOfReplicas(randomIntBetween(1,5)) + .settings(settings(Version.CURRENT)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(1, 5)) .build(); ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) - .metadata(Metadata.builder() - .put(indexMetadata, false)) + .metadata(Metadata.builder().put(indexMetadata, false)) .build(); ActionRequestValidationException validationException = validateGeneratedIndexName(generatedIndexName, clusterState); assertThat(validationException, notNullValue()); - assertThat(validationException.validationErrors(), containsInAnyOrder("the index name we generated [" + generatedIndexName - + "] already exists")); + assertThat( + validationException.validationErrors(), + containsInAnyOrder("the index name we generated [" + generatedIndexName + "] already exists") + ); } { // generated index name already exists as an index (cluster state routing table is also populated) String generatedIndexName = generateValidIndexName(randomAlphaOfLengthBetween(5, 10), randomAlphaOfLengthBetween(5, 150)); IndexMetadata indexMetadata = IndexMetadata.builder(generatedIndexName) - .settings(settings(Version.CURRENT)).numberOfShards(randomIntBetween(1, 5)) + .settings(settings(Version.CURRENT)) + .numberOfShards(randomIntBetween(1, 5)) .numberOfReplicas(randomIntBetween(1, 5)) .build(); ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) @@ -181,27 +203,32 @@ public void testValidateGeneratedIndexName() { ActionRequestValidationException validationException = validateGeneratedIndexName(generatedIndexName, clusterState); assertThat(validationException, notNullValue()); - assertThat(validationException.validationErrors(), containsInAnyOrder("the index name we generated [" + generatedIndexName - + "] already exists"));; + assertThat( + validationException.validationErrors(), + containsInAnyOrder("the index name we generated [" + generatedIndexName + "] already exists") + ); + ; } { // generated index name already exists as an alias to another index String generatedIndexName = generateValidIndexName(randomAlphaOfLengthBetween(5, 10), randomAlphaOfLengthBetween(5, 150)); IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLengthBetween(10, 30)) - .settings(settings(Version.CURRENT)).numberOfShards(randomIntBetween(1, 5)) + .settings(settings(Version.CURRENT)) + .numberOfShards(randomIntBetween(1, 5)) .numberOfReplicas(randomIntBetween(1, 5)) .putAlias(AliasMetadata.builder(generatedIndexName).build()) .build(); ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) - .metadata(Metadata.builder() - .put(indexMetadata, false)) + .metadata(Metadata.builder().put(indexMetadata, false)) .build(); ActionRequestValidationException validationException = validateGeneratedIndexName(generatedIndexName, clusterState); assertThat(validationException, notNullValue()); - assertThat(validationException.validationErrors(), containsInAnyOrder("the index name we generated [" + generatedIndexName - + "] already exists as alias")); + assertThat( + validationException.validationErrors(), + containsInAnyOrder("the index name we generated [" + generatedIndexName + "] already exists as alias") + ); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/IndexLifecycleExplainResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/IndexLifecycleExplainResponseTests.java index 71d40f4038d42..1365dd728cc0e 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/IndexLifecycleExplainResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/IndexLifecycleExplainResponseTests.java @@ -8,18 +8,18 @@ package org.elasticsearch.xpack.core.ilm; import org.elasticsearch.cluster.ClusterModule; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.Writeable.Reader; import org.elasticsearch.common.util.CollectionUtils; +import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractSerializingTestCase; import java.io.IOException; import java.util.Arrays; @@ -45,7 +45,8 @@ private static IndexLifecycleExplainResponse randomUnmanagedIndexExplainResponse private static IndexLifecycleExplainResponse randomManagedIndexExplainResponse() { boolean stepNull = randomBoolean(); - return IndexLifecycleExplainResponse.newManagedIndexResponse(randomAlphaOfLength(10), + return IndexLifecycleExplainResponse.newManagedIndexResponse( + randomAlphaOfLength(10), randomAlphaOfLength(10), randomBoolean() ? null : randomLongBetween(0, System.currentTimeMillis()), stepNull ? null : randomAlphaOfLength(10), @@ -61,13 +62,16 @@ private static IndexLifecycleExplainResponse randomManagedIndexExplainResponse() stepNull ? null : randomAlphaOfLength(10), stepNull ? null : randomAlphaOfLength(10), randomBoolean() ? null : new BytesArray(new RandomStepInfo(() -> randomAlphaOfLength(10)).toString()), - randomBoolean() ? null : PhaseExecutionInfoTests.randomPhaseExecutionInfo("")); + randomBoolean() ? null : PhaseExecutionInfoTests.randomPhaseExecutionInfo("") + ); } public void testInvalidStepDetails() { final int numNull = randomIntBetween(1, 3); - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> - IndexLifecycleExplainResponse.newManagedIndexResponse(randomAlphaOfLength(10), + IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> IndexLifecycleExplainResponse.newManagedIndexResponse( + randomAlphaOfLength(10), randomAlphaOfLength(10), randomBoolean() ? null : randomNonNegativeLong(), (numNull == 1) ? null : randomAlphaOfLength(10), @@ -83,7 +87,9 @@ public void testInvalidStepDetails() { randomBoolean() ? null : randomAlphaOfLength(10), randomBoolean() ? null : randomAlphaOfLength(10), randomBoolean() ? null : new BytesArray(new RandomStepInfo(() -> randomAlphaOfLength(10)).toString()), - randomBoolean() ? null : PhaseExecutionInfoTests.randomPhaseExecutionInfo(""))); + randomBoolean() ? null : PhaseExecutionInfoTests.randomPhaseExecutionInfo("") + ) + ); assertThat(exception.getMessage(), startsWith("managed index response must have complete step details")); assertThat(exception.getMessage(), containsString("=null")); } @@ -130,93 +136,119 @@ protected IndexLifecycleExplainResponse mutateInstance(IndexLifecycleExplainResp PhaseExecutionInfo phaseExecutionInfo = instance.getPhaseExecutionInfo(); if (managed) { switch (between(0, 14)) { - case 0: - index = index + randomAlphaOfLengthBetween(1, 5); - break; - case 1: - policy = policy + randomAlphaOfLengthBetween(1, 5); - break; - case 2: - phase = randomAlphaOfLengthBetween(1, 5); - action = randomAlphaOfLengthBetween(1, 5); - step = randomAlphaOfLengthBetween(1, 5); - break; - case 3: - phaseTime = randomValueOtherThan(phaseTime, () -> randomLongBetween(0, 100000)); - break; - case 4: - actionTime = randomValueOtherThan(actionTime, () -> randomLongBetween(0, 100000)); - break; - case 5: - stepTime = randomValueOtherThan(stepTime, () -> randomLongBetween(0, 100000)); - break; - case 6: - if (Strings.hasLength(failedStep) == false) { - failedStep = randomAlphaOfLength(10); - } else if (randomBoolean()) { - failedStep = failedStep + randomAlphaOfLengthBetween(1, 5); - } else { - failedStep = null; - } - break; - case 7: - policyTime = randomValueOtherThan(policyTime, () -> randomLongBetween(0, 100000)); - break; - case 8: - if (Strings.hasLength(stepInfo) == false) { - stepInfo = new BytesArray(randomByteArrayOfLength(100)); - } else if (randomBoolean()) { - stepInfo = randomValueOtherThan(stepInfo, - () -> new BytesArray(new RandomStepInfo(() -> randomAlphaOfLength(10)).toString())); - } else { - stepInfo = null; - } - break; - case 9: - phaseExecutionInfo = randomValueOtherThan(phaseExecutionInfo, () -> PhaseExecutionInfoTests.randomPhaseExecutionInfo("")); - break; - case 10: - return IndexLifecycleExplainResponse.newUnmanagedIndexResponse(index); - case 11: - isAutoRetryableError = true; - failedStepRetryCount = randomValueOtherThan(failedStepRetryCount, () -> randomInt(10)); - break; - case 12: - repositoryName = randomValueOtherThan(repositoryName, () -> randomAlphaOfLengthBetween(5, 10)); - break; - case 13: - snapshotName = randomValueOtherThan(snapshotName, () -> randomAlphaOfLengthBetween(5, 10)); - break; - case 14: - shrinkIndexName = randomValueOtherThan(shrinkIndexName, () -> randomAlphaOfLengthBetween(5, 10)); - break; - default: - throw new AssertionError("Illegal randomisation branch"); + case 0: + index = index + randomAlphaOfLengthBetween(1, 5); + break; + case 1: + policy = policy + randomAlphaOfLengthBetween(1, 5); + break; + case 2: + phase = randomAlphaOfLengthBetween(1, 5); + action = randomAlphaOfLengthBetween(1, 5); + step = randomAlphaOfLengthBetween(1, 5); + break; + case 3: + phaseTime = randomValueOtherThan(phaseTime, () -> randomLongBetween(0, 100000)); + break; + case 4: + actionTime = randomValueOtherThan(actionTime, () -> randomLongBetween(0, 100000)); + break; + case 5: + stepTime = randomValueOtherThan(stepTime, () -> randomLongBetween(0, 100000)); + break; + case 6: + if (Strings.hasLength(failedStep) == false) { + failedStep = randomAlphaOfLength(10); + } else if (randomBoolean()) { + failedStep = failedStep + randomAlphaOfLengthBetween(1, 5); + } else { + failedStep = null; + } + break; + case 7: + policyTime = randomValueOtherThan(policyTime, () -> randomLongBetween(0, 100000)); + break; + case 8: + if (Strings.hasLength(stepInfo) == false) { + stepInfo = new BytesArray(randomByteArrayOfLength(100)); + } else if (randomBoolean()) { + stepInfo = randomValueOtherThan( + stepInfo, + () -> new BytesArray(new RandomStepInfo(() -> randomAlphaOfLength(10)).toString()) + ); + } else { + stepInfo = null; + } + break; + case 9: + phaseExecutionInfo = randomValueOtherThan( + phaseExecutionInfo, + () -> PhaseExecutionInfoTests.randomPhaseExecutionInfo("") + ); + break; + case 10: + return IndexLifecycleExplainResponse.newUnmanagedIndexResponse(index); + case 11: + isAutoRetryableError = true; + failedStepRetryCount = randomValueOtherThan(failedStepRetryCount, () -> randomInt(10)); + break; + case 12: + repositoryName = randomValueOtherThan(repositoryName, () -> randomAlphaOfLengthBetween(5, 10)); + break; + case 13: + snapshotName = randomValueOtherThan(snapshotName, () -> randomAlphaOfLengthBetween(5, 10)); + break; + case 14: + shrinkIndexName = randomValueOtherThan(shrinkIndexName, () -> randomAlphaOfLengthBetween(5, 10)); + break; + default: + throw new AssertionError("Illegal randomisation branch"); } - return IndexLifecycleExplainResponse.newManagedIndexResponse(index, policy, policyTime, phase, action, step, failedStep, - isAutoRetryableError, failedStepRetryCount, phaseTime, actionTime, stepTime, repositoryName, snapshotName, - shrinkIndexName, stepInfo, phaseExecutionInfo); + return IndexLifecycleExplainResponse.newManagedIndexResponse( + index, + policy, + policyTime, + phase, + action, + step, + failedStep, + isAutoRetryableError, + failedStepRetryCount, + phaseTime, + actionTime, + stepTime, + repositoryName, + snapshotName, + shrinkIndexName, + stepInfo, + phaseExecutionInfo + ); } else { switch (between(0, 1)) { - case 0: - return IndexLifecycleExplainResponse.newUnmanagedIndexResponse(index + randomAlphaOfLengthBetween(1, 5)); - case 1: - return randomManagedIndexExplainResponse(); - default: - throw new AssertionError("Illegal randomisation branch"); + case 0: + return IndexLifecycleExplainResponse.newUnmanagedIndexResponse(index + randomAlphaOfLengthBetween(1, 5)); + case 1: + return randomManagedIndexExplainResponse(); + default: + throw new AssertionError("Illegal randomisation branch"); } } } protected NamedWriteableRegistry getNamedWriteableRegistry() { - return new NamedWriteableRegistry(Arrays - .asList(new NamedWriteableRegistry.Entry(LifecycleAction.class, MockAction.NAME, MockAction::new))); + return new NamedWriteableRegistry( + Arrays.asList(new NamedWriteableRegistry.Entry(LifecycleAction.class, MockAction.NAME, MockAction::new)) + ); } @Override protected NamedXContentRegistry xContentRegistry() { - return new NamedXContentRegistry(CollectionUtils.appendToCopy(ClusterModule.getNamedXWriteables(), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(MockAction.NAME), MockAction::parse))); + return new NamedXContentRegistry( + CollectionUtils.appendToCopy( + ClusterModule.getNamedXWriteables(), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(MockAction.NAME), MockAction::parse) + ) + ); } private static class RandomStepInfo implements ToXContentObject { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/IndexLifecycleOriginationDateParserTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/IndexLifecycleOriginationDateParserTests.java index 247bd81ef64bd..beb58e6f9bb93 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/IndexLifecycleOriginationDateParserTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/IndexLifecycleOriginationDateParserTests.java @@ -21,23 +21,17 @@ public class IndexLifecycleOriginationDateParserTests extends ESTestCase { private static final DateFormatter dateFormatter = DateFormatter.forPattern("uuuu.MM.dd"); public void testShouldParseIndexNameReturnsFalseWhenOriginationDateIsSet() { - Settings settings = Settings.builder() - .put(LifecycleSettings.LIFECYCLE_ORIGINATION_DATE, 1L) - .build(); + Settings settings = Settings.builder().put(LifecycleSettings.LIFECYCLE_ORIGINATION_DATE, 1L).build(); assertThat(shouldParseIndexName(settings), is(false)); } public void testShouldParseIndexNameReturnsFalseIfParseOriginationDateIsDisabled() { - Settings settings = Settings.builder() - .put(LifecycleSettings.LIFECYCLE_PARSE_ORIGINATION_DATE, false) - .build(); + Settings settings = Settings.builder().put(LifecycleSettings.LIFECYCLE_PARSE_ORIGINATION_DATE, false).build(); assertThat(shouldParseIndexName(settings), is(false)); } public void testShouldParseIndexNameReturnsTrueIfParseOriginationDateIsTrueAndOriginationDateIsNotSet() { - Settings settings = Settings.builder() - .put(LifecycleSettings.LIFECYCLE_PARSE_ORIGINATION_DATE, true) - .build(); + Settings settings = Settings.builder().put(LifecycleSettings.LIFECYCLE_PARSE_ORIGINATION_DATE, true).build(); assertThat(shouldParseIndexName(settings), is(true)); } @@ -56,15 +50,21 @@ public void testParseIndexNameThatMatchesExpectedFormat() throws ParseException { long parsedDate = parseIndexNameAndExtractDate("indexName-2019.09.04-2019.09.24"); long secondDateInIndexName = dateFormatter.parseMillis("2019.09.24"); - assertThat("indexName-yyyy.MM.dd-yyyy.MM.dd is a valid index format and the second date should be parsed", - parsedDate, is(secondDateInIndexName)); + assertThat( + "indexName-yyyy.MM.dd-yyyy.MM.dd is a valid index format and the second date should be parsed", + parsedDate, + is(secondDateInIndexName) + ); } { long parsedDate = parseIndexNameAndExtractDate("index-2019.09.04-2019.09.24-00002"); long secondDateInIndexName = dateFormatter.parseMillis("2019.09.24"); - assertThat("indexName-yyyy.MM.dd-yyyy.MM.dd-digits is a valid index format and the second date should be parsed", - parsedDate, is(secondDateInIndexName)); + assertThat( + "indexName-yyyy.MM.dd-yyyy.MM.dd-digits is a valid index format and the second date should be parsed", + parsedDate, + is(secondDateInIndexName) + ); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/InitializePolicyContextStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/InitializePolicyContextStepTests.java index ad554d45c5285..4bcd17b52a179 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/InitializePolicyContextStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/InitializePolicyContextStepTests.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.xpack.core.ilm; - import org.elasticsearch.Version; import org.elasticsearch.cluster.ClusterName; import org.elasticsearch.cluster.ClusterState; @@ -57,7 +56,9 @@ public void testAddCreationDate() { IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(5)) .settings(settings(Version.CURRENT)) .creationDate(creationDate) - .numberOfShards(1).numberOfReplicas(0).build(); + .numberOfShards(1) + .numberOfReplicas(0) + .build(); Metadata metadata = Metadata.builder() .persistentSettings(settings(Version.CURRENT).build()) .put(IndexMetadata.builder(indexMetadata)) @@ -77,7 +78,9 @@ public void testDoNothing() { .settings(settings(Version.CURRENT)) .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()) .creationDate(creationDate) - .numberOfShards(1).numberOfReplicas(0).build(); + .numberOfShards(1) + .numberOfReplicas(0) + .build(); Metadata metadata = Metadata.builder() .persistentSettings(settings(Version.CURRENT).build()) .put(IndexMetadata.builder(indexMetadata)) diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/LifecycleExecutionStateTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/LifecycleExecutionStateTests.java index ecf300124d166..14802c75827b1 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/LifecycleExecutionStateTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/LifecycleExecutionStateTests.java @@ -69,7 +69,8 @@ public void testEqualsAndHashcode() { EqualsHashCodeTestUtils.checkEqualsAndHashCode( original, toCopy -> LifecycleExecutionState.builder(toCopy).build(), - LifecycleExecutionStateTests::mutate); + LifecycleExecutionStateTests::mutate + ); } public void testGetCurrentStepKey() { @@ -97,8 +98,10 @@ public void testGetCurrentStepKey() { lifecycleState3.setPhase(phase); lifecycleState3.setAction(action); lifecycleState3.setStep(step); - AssertionError error3 = expectThrows(AssertionError.class, - () -> LifecycleExecutionState.getCurrentStepKey(lifecycleState3.build())); + AssertionError error3 = expectThrows( + AssertionError.class, + () -> LifecycleExecutionState.getCurrentStepKey(lifecycleState3.build()) + ); assertEquals("Current phase is not empty: " + phase, error3.getMessage()); phase = null; @@ -108,8 +111,10 @@ public void testGetCurrentStepKey() { lifecycleState4.setPhase(phase); lifecycleState4.setAction(action); lifecycleState4.setStep(step); - AssertionError error4 = expectThrows(AssertionError.class, - () -> LifecycleExecutionState.getCurrentStepKey(lifecycleState4.build())); + AssertionError error4 = expectThrows( + AssertionError.class, + () -> LifecycleExecutionState.getCurrentStepKey(lifecycleState4.build()) + ); assertEquals("Current action is not empty: " + action, error4.getMessage()); phase = null; @@ -119,8 +124,10 @@ public void testGetCurrentStepKey() { lifecycleState5.setPhase(phase); lifecycleState5.setAction(action); lifecycleState5.setStep(step); - AssertionError error5 = expectThrows(AssertionError.class, - () -> LifecycleExecutionState.getCurrentStepKey(lifecycleState5.build())); + AssertionError error5 = expectThrows( + AssertionError.class, + () -> LifecycleExecutionState.getCurrentStepKey(lifecycleState5.build()) + ); assertNull(error5.getMessage()); phase = null; @@ -130,8 +137,10 @@ public void testGetCurrentStepKey() { lifecycleState6.setPhase(phase); lifecycleState6.setAction(action); lifecycleState6.setStep(step); - AssertionError error6 = expectThrows(AssertionError.class, - () -> LifecycleExecutionState.getCurrentStepKey(lifecycleState6.build())); + AssertionError error6 = expectThrows( + AssertionError.class, + () -> LifecycleExecutionState.getCurrentStepKey(lifecycleState6.build()) + ); assertNull(error6.getMessage()); } @@ -172,12 +181,14 @@ private static LifecycleExecutionState mutate(LifecycleExecutionState toMutate) newState.setShrinkIndexName(randomValueOtherThan(toMutate.getShrinkIndexName(), () -> randomAlphaOfLengthBetween(5, 20))); break; case 11: - newState.setSnapshotRepository(randomValueOtherThan(toMutate.getSnapshotRepository(), - () -> randomAlphaOfLengthBetween(5, 20))); + newState.setSnapshotRepository( + randomValueOtherThan(toMutate.getSnapshotRepository(), () -> randomAlphaOfLengthBetween(5, 20)) + ); break; case 12: - newState.setSnapshotIndexName(randomValueOtherThan(toMutate.getSnapshotIndexName(), - () -> randomAlphaOfLengthBetween(5, 20))); + newState.setSnapshotIndexName( + randomValueOtherThan(toMutate.getSnapshotIndexName(), () -> randomAlphaOfLengthBetween(5, 20)) + ); break; case 13: newState.setSnapshotName(randomValueOtherThan(toMutate.getSnapshotName(), () -> randomAlphaOfLengthBetween(5, 20))); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/LifecyclePolicyMetadataTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/LifecyclePolicyMetadataTests.java index 6081f82ac6635..59d541d9002f6 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/LifecyclePolicyMetadataTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/LifecyclePolicyMetadataTests.java @@ -8,12 +8,12 @@ package org.elasticsearch.xpack.core.ilm; import org.elasticsearch.cluster.ClusterModule; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.Writeable.Reader; +import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractSerializingTestCase; import org.junit.Before; import java.io.IOException; @@ -39,8 +39,11 @@ protected NamedWriteableRegistry getNamedWriteableRegistry() { return new NamedWriteableRegistry( Arrays.asList( new NamedWriteableRegistry.Entry(LifecycleAction.class, MockAction.NAME, MockAction::new), - new NamedWriteableRegistry.Entry(LifecycleType.class, TimeseriesLifecycleType.TYPE, - (in) -> TimeseriesLifecycleType.INSTANCE), + new NamedWriteableRegistry.Entry( + LifecycleType.class, + TimeseriesLifecycleType.TYPE, + (in) -> TimeseriesLifecycleType.INSTANCE + ), new NamedWriteableRegistry.Entry(LifecycleAction.class, AllocateAction.NAME, AllocateAction::new), new NamedWriteableRegistry.Entry(LifecycleAction.class, WaitForSnapshotAction.NAME, WaitForSnapshotAction::new), new NamedWriteableRegistry.Entry(LifecycleAction.class, SearchableSnapshotAction.NAME, SearchableSnapshotAction::new), @@ -54,31 +57,43 @@ protected NamedWriteableRegistry getNamedWriteableRegistry() { new NamedWriteableRegistry.Entry(LifecycleAction.class, MigrateAction.NAME, MigrateAction::new), new NamedWriteableRegistry.Entry(LifecycleAction.class, UnfollowAction.NAME, UnfollowAction::new), new NamedWriteableRegistry.Entry(LifecycleAction.class, RollupILMAction.NAME, RollupILMAction::new) - )); + ) + ); } @Override protected NamedXContentRegistry xContentRegistry() { List entries = new ArrayList<>(ClusterModule.getNamedXWriteables()); - entries.addAll(Arrays.asList( - new NamedXContentRegistry.Entry(LifecycleType.class, new ParseField(TimeseriesLifecycleType.TYPE), - (p) -> TimeseriesLifecycleType.INSTANCE), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(AllocateAction.NAME), AllocateAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, - new ParseField(WaitForSnapshotAction.NAME), WaitForSnapshotAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(SearchableSnapshotAction.NAME), - SearchableSnapshotAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ForceMergeAction.NAME), ForceMergeAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ReadOnlyAction.NAME), ReadOnlyAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(RolloverAction.NAME), RolloverAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ShrinkAction.NAME), ShrinkAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(FreezeAction.NAME), FreezeAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(SetPriorityAction.NAME), SetPriorityAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(MigrateAction.NAME), MigrateAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(UnfollowAction.NAME), UnfollowAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(RollupILMAction.NAME), RollupILMAction::parse) - )); + entries.addAll( + Arrays.asList( + new NamedXContentRegistry.Entry( + LifecycleType.class, + new ParseField(TimeseriesLifecycleType.TYPE), + (p) -> TimeseriesLifecycleType.INSTANCE + ), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(AllocateAction.NAME), AllocateAction::parse), + new NamedXContentRegistry.Entry( + LifecycleAction.class, + new ParseField(WaitForSnapshotAction.NAME), + WaitForSnapshotAction::parse + ), + new NamedXContentRegistry.Entry( + LifecycleAction.class, + new ParseField(SearchableSnapshotAction.NAME), + SearchableSnapshotAction::parse + ), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ForceMergeAction.NAME), ForceMergeAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ReadOnlyAction.NAME), ReadOnlyAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(RolloverAction.NAME), RolloverAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ShrinkAction.NAME), ShrinkAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(FreezeAction.NAME), FreezeAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(SetPriorityAction.NAME), SetPriorityAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(MigrateAction.NAME), MigrateAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(UnfollowAction.NAME), UnfollowAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(RollupILMAction.NAME), RollupILMAction::parse) + ) + ); return new NamedXContentRegistry(entries); } @@ -98,8 +113,12 @@ public static LifecyclePolicyMetadata createRandomPolicyMetadata(String lifecycl for (int i = 0; i < numberHeaders; i++) { headers.put(randomAlphaOfLength(10), randomAlphaOfLength(10)); } - return new LifecyclePolicyMetadata(LifecyclePolicyTests.randomTimeseriesLifecyclePolicy(lifecycleName), headers, - randomNonNegativeLong(), randomNonNegativeLong()); + return new LifecyclePolicyMetadata( + LifecyclePolicyTests.randomTimeseriesLifecyclePolicy(lifecycleName), + headers, + randomNonNegativeLong(), + randomNonNegativeLong() + ); } @Override @@ -114,22 +133,26 @@ protected LifecyclePolicyMetadata mutateInstance(LifecyclePolicyMetadata instanc long version = instance.getVersion(); long creationDate = instance.getModifiedDate(); switch (between(0, 3)) { - case 0: - policy = new LifecyclePolicy(TimeseriesLifecycleType.INSTANCE, policy.getName() + randomAlphaOfLengthBetween(1, 5), - policy.getPhases(), randomMeta()); - break; - case 1: - headers = new HashMap<>(headers); - headers.put(randomAlphaOfLength(11), randomAlphaOfLength(11)); - break; - case 2: - version++; - break; - case 3: - creationDate++; - break; - default: - throw new AssertionError("Illegal randomisation branch"); + case 0: + policy = new LifecyclePolicy( + TimeseriesLifecycleType.INSTANCE, + policy.getName() + randomAlphaOfLengthBetween(1, 5), + policy.getPhases(), + randomMeta() + ); + break; + case 1: + headers = new HashMap<>(headers); + headers.put(randomAlphaOfLength(11), randomAlphaOfLength(11)); + break; + case 2: + version++; + break; + case 3: + creationDate++; + break; + default: + throw new AssertionError("Illegal randomisation branch"); } return new LifecyclePolicyMetadata(policy, headers, version, creationDate); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/LifecyclePolicyTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/LifecyclePolicyTests.java index 1623526c9d979..62f9df970559c 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/LifecyclePolicyTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/LifecyclePolicyTests.java @@ -8,14 +8,14 @@ import org.elasticsearch.client.Client; import org.elasticsearch.cluster.ClusterModule; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.Writeable.Reader; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xpack.core.ilm.Step.StepKey; import java.io.IOException; @@ -48,8 +48,11 @@ protected LifecyclePolicy doParseInstance(XContentParser parser) { protected NamedWriteableRegistry getNamedWriteableRegistry() { return new NamedWriteableRegistry( Arrays.asList( - new NamedWriteableRegistry.Entry(LifecycleType.class, TimeseriesLifecycleType.TYPE, - (in) -> TimeseriesLifecycleType.INSTANCE), + new NamedWriteableRegistry.Entry( + LifecycleType.class, + TimeseriesLifecycleType.TYPE, + (in) -> TimeseriesLifecycleType.INSTANCE + ), new NamedWriteableRegistry.Entry(LifecycleAction.class, AllocateAction.NAME, AllocateAction::new), new NamedWriteableRegistry.Entry(LifecycleAction.class, WaitForSnapshotAction.NAME, WaitForSnapshotAction::new), new NamedWriteableRegistry.Entry(LifecycleAction.class, DeleteAction.NAME, DeleteAction::new), @@ -63,31 +66,43 @@ protected NamedWriteableRegistry getNamedWriteableRegistry() { new NamedWriteableRegistry.Entry(LifecycleAction.class, MigrateAction.NAME, MigrateAction::new), new NamedWriteableRegistry.Entry(LifecycleAction.class, SearchableSnapshotAction.NAME, SearchableSnapshotAction::new), new NamedWriteableRegistry.Entry(LifecycleAction.class, RollupILMAction.NAME, RollupILMAction::new) - )); + ) + ); } @Override protected NamedXContentRegistry xContentRegistry() { List entries = new ArrayList<>(ClusterModule.getNamedXWriteables()); - entries.addAll(Arrays.asList( - new NamedXContentRegistry.Entry(LifecycleType.class, new ParseField(TimeseriesLifecycleType.TYPE), - (p) -> TimeseriesLifecycleType.INSTANCE), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(AllocateAction.NAME), AllocateAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, - new ParseField(WaitForSnapshotAction.NAME), WaitForSnapshotAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ForceMergeAction.NAME), ForceMergeAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ReadOnlyAction.NAME), ReadOnlyAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(RolloverAction.NAME), RolloverAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ShrinkAction.NAME), ShrinkAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(FreezeAction.NAME), FreezeAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(SetPriorityAction.NAME), SetPriorityAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(UnfollowAction.NAME), UnfollowAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(MigrateAction.NAME), MigrateAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(SearchableSnapshotAction.NAME), - SearchableSnapshotAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(RollupILMAction.NAME), RollupILMAction::parse) - )); + entries.addAll( + Arrays.asList( + new NamedXContentRegistry.Entry( + LifecycleType.class, + new ParseField(TimeseriesLifecycleType.TYPE), + (p) -> TimeseriesLifecycleType.INSTANCE + ), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(AllocateAction.NAME), AllocateAction::parse), + new NamedXContentRegistry.Entry( + LifecycleAction.class, + new ParseField(WaitForSnapshotAction.NAME), + WaitForSnapshotAction::parse + ), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ForceMergeAction.NAME), ForceMergeAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ReadOnlyAction.NAME), ReadOnlyAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(RolloverAction.NAME), RolloverAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ShrinkAction.NAME), ShrinkAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(FreezeAction.NAME), FreezeAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(SetPriorityAction.NAME), SetPriorityAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(UnfollowAction.NAME), UnfollowAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(MigrateAction.NAME), MigrateAction::parse), + new NamedXContentRegistry.Entry( + LifecycleAction.class, + new ParseField(SearchableSnapshotAction.NAME), + SearchableSnapshotAction::parse + ), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(RollupILMAction.NAME), RollupILMAction::parse) + ) + ); return new NamedXContentRegistry(entries); } @@ -108,8 +123,9 @@ public static LifecyclePolicy randomTimeseriesLifecyclePolicyWithAllPhases(@Null Function randomAction = getNameToActionFunction(); TimeValue prev = null; for (String phase : phaseNames) { - TimeValue after = prev == null ? TimeValue.parseTimeValue(randomTimeValue(0, 100000, "s", "m", "h", "d"), "test_after") : - TimeValue.timeValueSeconds(prev.seconds() + randomIntBetween(60, 600)); + TimeValue after = prev == null + ? TimeValue.parseTimeValue(randomTimeValue(0, 100000, "s", "m", "h", "d"), "test_after") + : TimeValue.timeValueSeconds(prev.seconds() + randomIntBetween(60, 600)); prev = after; Map actions = new HashMap<>(); Set actionNames = validActions.apply(phase); @@ -128,7 +144,9 @@ public static LifecyclePolicy randomTimeseriesLifecyclePolicyWithAllPhases(@Null public static LifecyclePolicy randomTimeseriesLifecyclePolicy(@Nullable String lifecycleName) { List phaseNames = randomSubsetOf( - between(0, TimeseriesLifecycleType.ORDERED_VALID_PHASES.size() - 1), TimeseriesLifecycleType.ORDERED_VALID_PHASES).stream() + between(0, TimeseriesLifecycleType.ORDERED_VALID_PHASES.size() - 1), + TimeseriesLifecycleType.ORDERED_VALID_PHASES + ).stream() // Remove the frozen phase, we'll randomly re-add it later .filter(pn -> TimeseriesLifecycleType.FROZEN_PHASE.equals(pn) == false) .collect(Collectors.toList()); @@ -153,8 +171,9 @@ public static LifecyclePolicy randomTimeseriesLifecyclePolicy(@Nullable String l TimeValue prev = null; for (String phase : orderedPhases) { - TimeValue after = prev == null ? TimeValue.parseTimeValue(randomTimeValue(0, 100000, "s", "m", "h", "d"), "test_after") : - TimeValue.timeValueSeconds(prev.seconds() + randomIntBetween(60, 600)); + TimeValue after = prev == null + ? TimeValue.parseTimeValue(randomTimeValue(0, 100000, "s", "m", "h", "d"), "test_after") + : TimeValue.timeValueSeconds(prev.seconds() + randomIntBetween(60, 600)); prev = after; Map actions = new HashMap<>(); List actionNames = randomSubsetOf(validActions.apply(phase)); @@ -194,12 +213,20 @@ public static LifecyclePolicy randomTimeseriesLifecyclePolicy(@Nullable String l } // Add a frozen phase if neither the hot nor cold phase contains a searchable snapshot action if (hotPhaseContainsSearchableSnap == false && coldPhaseContainsSearchableSnap == false && randomBoolean()) { - TimeValue frozenTime = prev == null ? TimeValue.parseTimeValue(randomTimeValue(0, 100000, "s", "m", "h", "d"), "test") : - TimeValue.timeValueSeconds(prev.seconds() + randomIntBetween(60, 600)); - phases.put(TimeseriesLifecycleType.FROZEN_PHASE, - new Phase(TimeseriesLifecycleType.FROZEN_PHASE, frozenTime, - Collections.singletonMap(SearchableSnapshotAction.NAME, - new SearchableSnapshotAction(randomAlphaOfLength(10), randomBoolean())))); + TimeValue frozenTime = prev == null + ? TimeValue.parseTimeValue(randomTimeValue(0, 100000, "s", "m", "h", "d"), "test") + : TimeValue.timeValueSeconds(prev.seconds() + randomIntBetween(60, 600)); + phases.put( + TimeseriesLifecycleType.FROZEN_PHASE, + new Phase( + TimeseriesLifecycleType.FROZEN_PHASE, + frozenTime, + Collections.singletonMap( + SearchableSnapshotAction.NAME, + new SearchableSnapshotAction(randomAlphaOfLength(10), randomBoolean()) + ) + ) + ); } else { phases.remove(TimeseriesLifecycleType.FROZEN_PHASE); } @@ -221,41 +248,43 @@ private static Function> getPhaseToValidActions() { return new HashSet<>(TimeseriesLifecycleType.VALID_DELETE_ACTIONS); default: throw new IllegalArgumentException("invalid phase [" + phase + "]"); - }}; + } + }; } private static Function getNameToActionFunction() { return (action) -> { - switch (action) { - case AllocateAction.NAME: - return AllocateActionTests.randomInstance(); - case WaitForSnapshotAction.NAME: - return WaitForSnapshotActionTests.randomInstance(); - case DeleteAction.NAME: - return new DeleteAction(); - case ForceMergeAction.NAME: - return ForceMergeActionTests.randomInstance(); - case ReadOnlyAction.NAME: - return new ReadOnlyAction(); - case RolloverAction.NAME: - return RolloverActionTests.randomInstance(); - case ShrinkAction.NAME: - return ShrinkActionTests.randomInstance(); - case FreezeAction.NAME: - return new FreezeAction(); - case SetPriorityAction.NAME: - return SetPriorityActionTests.randomInstance(); - case UnfollowAction.NAME: - return new UnfollowAction(); - case SearchableSnapshotAction.NAME: - return new SearchableSnapshotAction("repo", randomBoolean()); - case MigrateAction.NAME: - return new MigrateAction(false); - case RollupILMAction.NAME: - return RollupILMActionTests.randomInstance(); - default: - throw new IllegalArgumentException("invalid action [" + action + "]"); - }}; + switch (action) { + case AllocateAction.NAME: + return AllocateActionTests.randomInstance(); + case WaitForSnapshotAction.NAME: + return WaitForSnapshotActionTests.randomInstance(); + case DeleteAction.NAME: + return new DeleteAction(); + case ForceMergeAction.NAME: + return ForceMergeActionTests.randomInstance(); + case ReadOnlyAction.NAME: + return new ReadOnlyAction(); + case RolloverAction.NAME: + return RolloverActionTests.randomInstance(); + case ShrinkAction.NAME: + return ShrinkActionTests.randomInstance(); + case FreezeAction.NAME: + return new FreezeAction(); + case SetPriorityAction.NAME: + return SetPriorityActionTests.randomInstance(); + case UnfollowAction.NAME: + return new UnfollowAction(); + case SearchableSnapshotAction.NAME: + return new SearchableSnapshotAction("repo", randomBoolean()); + case MigrateAction.NAME: + return new MigrateAction(false); + case RollupILMAction.NAME: + return RollupILMActionTests.randomInstance(); + default: + throw new IllegalArgumentException("invalid action [" + action + "]"); + } + }; } public static LifecyclePolicy randomTestLifecyclePolicy(@Nullable String lifecycleName) { @@ -289,10 +318,14 @@ protected LifecyclePolicy mutateInstance(LifecyclePolicy instance) throws IOExce if (phases.size() > 0) { phases.remove(new ArrayList<>(phases.keySet()).remove(randomIntBetween(0, phases.size() - 1))); } - String phaseName = randomValueOtherThanMany(phases::containsKey, - () -> randomFrom(TimeseriesLifecycleType.ORDERED_VALID_PHASES.stream() + String phaseName = randomValueOtherThanMany( + phases::containsKey, + () -> randomFrom( + TimeseriesLifecycleType.ORDERED_VALID_PHASES.stream() .filter(pn -> TimeseriesLifecycleType.FROZEN_PHASE.equals(pn) == false) - .collect(Collectors.toList()))); + .collect(Collectors.toList()) + ) + ); phases = new LinkedHashMap<>(phases); phases.put(phaseName, new Phase(phaseName, null, Collections.emptyMap())); break; @@ -322,8 +355,7 @@ public void testFirstAndLastSteps() { public void testToStepsWithOneStep() { Client client = mock(Client.class); - MockStep mockStep = new MockStep( - new Step.StepKey("test", "test", "test"), PhaseCompleteStep.finalStep("test").getKey()); + MockStep mockStep = new MockStep(new Step.StepKey("test", "test", "test"), PhaseCompleteStep.finalStep("test").getKey()); lifecycleName = randomAlphaOfLengthBetween(1, 20); Map phases = new LinkedHashMap<>(); @@ -347,10 +379,14 @@ public void testToStepsWithOneStep() { public void testToStepsWithTwoPhases() { Client client = mock(Client.class); - MockStep secondActionStep = new MockStep(new StepKey("second_phase", "test2", "test"), - PhaseCompleteStep.finalStep("second_phase").getKey()); - MockStep secondAfter = new MockStep(new StepKey("first_phase", PhaseCompleteStep.NAME, PhaseCompleteStep.NAME), - secondActionStep.getKey()); + MockStep secondActionStep = new MockStep( + new StepKey("second_phase", "test2", "test"), + PhaseCompleteStep.finalStep("second_phase").getKey() + ); + MockStep secondAfter = new MockStep( + new StepKey("first_phase", PhaseCompleteStep.NAME, PhaseCompleteStep.NAME), + secondActionStep.getKey() + ); MockStep firstActionAnotherStep = new MockStep(new StepKey("first_phase", "test", "bar"), secondAfter.getKey()); MockStep firstActionStep = new MockStep(new StepKey("first_phase", "test", "foo"), firstActionAnotherStep.getKey()); MockStep firstAfter = new MockStep(new StepKey("new", PhaseCompleteStep.NAME, PhaseCompleteStep.NAME), firstActionStep.getKey()); @@ -401,23 +437,33 @@ public void testIsActionSafe() { assertFalse(policy.isActionSafe(new StepKey("second_phase", MockAction.NAME, randomAlphaOfLength(10)))); - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, - () -> policy.isActionSafe(new StepKey("non_existant_phase", MockAction.NAME, randomAlphaOfLength(10)))); + IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> policy.isActionSafe(new StepKey("non_existant_phase", MockAction.NAME, randomAlphaOfLength(10))) + ); assertEquals("Phase [non_existant_phase] does not exist in policy [" + policy.getName() + "]", exception.getMessage()); - exception = expectThrows(IllegalArgumentException.class, - () -> policy.isActionSafe(new StepKey("first_phase", "non_existant_action", randomAlphaOfLength(10)))); - assertEquals("Action [non_existant_action] in phase [first_phase] does not exist in policy [" + policy.getName() + "]", - exception.getMessage()); + exception = expectThrows( + IllegalArgumentException.class, + () -> policy.isActionSafe(new StepKey("first_phase", "non_existant_action", randomAlphaOfLength(10))) + ); + assertEquals( + "Action [non_existant_action] in phase [first_phase] does not exist in policy [" + policy.getName() + "]", + exception.getMessage() + ); assertTrue(policy.isActionSafe(new StepKey("new", randomAlphaOfLength(10), randomAlphaOfLength(10)))); } public void testValidatePolicyName() { - expectThrows(IllegalArgumentException.class, () -> LifecyclePolicy.validatePolicyName(randomAlphaOfLengthBetween(0, 10) + - "," + randomAlphaOfLengthBetween(0, 10))); - expectThrows(IllegalArgumentException.class, () -> LifecyclePolicy.validatePolicyName(randomAlphaOfLengthBetween(0, 10) + - " " + randomAlphaOfLengthBetween(0, 10))); + expectThrows( + IllegalArgumentException.class, + () -> LifecyclePolicy.validatePolicyName(randomAlphaOfLengthBetween(0, 10) + "," + randomAlphaOfLengthBetween(0, 10)) + ); + expectThrows( + IllegalArgumentException.class, + () -> LifecyclePolicy.validatePolicyName(randomAlphaOfLengthBetween(0, 10) + " " + randomAlphaOfLengthBetween(0, 10)) + ); expectThrows(IllegalArgumentException.class, () -> LifecyclePolicy.validatePolicyName("_" + randomAlphaOfLengthBetween(1, 20))); expectThrows(IllegalArgumentException.class, () -> LifecyclePolicy.validatePolicyName(randomAlphaOfLengthBetween(256, 1000))); @@ -434,8 +480,10 @@ public static Map randomMeta() { if (randomBoolean()) { return Collections.singletonMap(randomAlphaOfLength(4), randomAlphaOfLength(4)); } else { - return Collections.singletonMap(randomAlphaOfLength(5), - Collections.singletonMap(randomAlphaOfLength(4), randomAlphaOfLength(4))); + return Collections.singletonMap( + randomAlphaOfLength(5), + Collections.singletonMap(randomAlphaOfLength(4), randomAlphaOfLength(4)) + ); } } else { return null; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/LifecyclePolicyUtilsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/LifecyclePolicyUtilsTests.java index 7df0f75a5a4e2..c268a4183a7b0 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/LifecyclePolicyUtilsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/LifecyclePolicyUtilsTests.java @@ -30,118 +30,203 @@ public class LifecyclePolicyUtilsTests extends ESTestCase { public void testCalculateUsage() { - final IndexNameExpressionResolver iner = - new IndexNameExpressionResolver(new ThreadContext(Settings.EMPTY), EmptySystemIndices.INSTANCE); + final IndexNameExpressionResolver iner = new IndexNameExpressionResolver( + new ThreadContext(Settings.EMPTY), + EmptySystemIndices.INSTANCE + ); { // Test where policy does not exist ClusterState state = ClusterState.builder(new ClusterName("mycluster")).build(); - assertThat(LifecyclePolicyUtils.calculateUsage(iner, state, "mypolicy"), - equalTo(new ItemUsage(Collections.emptyList(), Collections.emptyList(), Collections.emptyList()))); + assertThat( + LifecyclePolicyUtils.calculateUsage(iner, state, "mypolicy"), + equalTo(new ItemUsage(Collections.emptyList(), Collections.emptyList(), Collections.emptyList())) + ); } { // Test where policy is not used by anything ClusterState state = ClusterState.builder(new ClusterName("mycluster")) - .metadata(Metadata.builder() - .putCustom(IndexLifecycleMetadata.TYPE, - new IndexLifecycleMetadata(Collections.singletonMap("mypolicy", - LifecyclePolicyMetadataTests.createRandomPolicyMetadata("mypolicy")), OperationMode.RUNNING)) - .build()) + .metadata( + Metadata.builder() + .putCustom( + IndexLifecycleMetadata.TYPE, + new IndexLifecycleMetadata( + Collections.singletonMap("mypolicy", LifecyclePolicyMetadataTests.createRandomPolicyMetadata("mypolicy")), + OperationMode.RUNNING + ) + ) + .build() + ) .build(); - assertThat(LifecyclePolicyUtils.calculateUsage(iner, state, "mypolicy"), - equalTo(new ItemUsage(Collections.emptyList(), Collections.emptyList(), Collections.emptyList()))); + assertThat( + LifecyclePolicyUtils.calculateUsage(iner, state, "mypolicy"), + equalTo(new ItemUsage(Collections.emptyList(), Collections.emptyList(), Collections.emptyList())) + ); } { // Test where policy exists and is used by an index ClusterState state = ClusterState.builder(new ClusterName("mycluster")) - .metadata(Metadata.builder() - .putCustom(IndexLifecycleMetadata.TYPE, - new IndexLifecycleMetadata(Collections.singletonMap("mypolicy", - LifecyclePolicyMetadataTests.createRandomPolicyMetadata("mypolicy")), OperationMode.RUNNING)) - .put(IndexMetadata.builder("myindex") - .settings(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(LifecycleSettings.LIFECYCLE_NAME, "mypolicy") - .build())) - .build()) + .metadata( + Metadata.builder() + .putCustom( + IndexLifecycleMetadata.TYPE, + new IndexLifecycleMetadata( + Collections.singletonMap("mypolicy", LifecyclePolicyMetadataTests.createRandomPolicyMetadata("mypolicy")), + OperationMode.RUNNING + ) + ) + .put( + IndexMetadata.builder("myindex") + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(LifecycleSettings.LIFECYCLE_NAME, "mypolicy") + .build() + ) + ) + .build() + ) .build(); - assertThat(LifecyclePolicyUtils.calculateUsage(iner, state, "mypolicy"), - equalTo(new ItemUsage(Collections.singleton("myindex"), Collections.emptyList(), Collections.emptyList()))); + assertThat( + LifecyclePolicyUtils.calculateUsage(iner, state, "mypolicy"), + equalTo(new ItemUsage(Collections.singleton("myindex"), Collections.emptyList(), Collections.emptyList())) + ); } { // Test where policy exists and is used by an index, and template ClusterState state = ClusterState.builder(new ClusterName("mycluster")) - .metadata(Metadata.builder() - .putCustom(IndexLifecycleMetadata.TYPE, - new IndexLifecycleMetadata(Collections.singletonMap("mypolicy", - LifecyclePolicyMetadataTests.createRandomPolicyMetadata("mypolicy")), OperationMode.RUNNING)) - .put(IndexMetadata.builder("myindex") - .settings(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(LifecycleSettings.LIFECYCLE_NAME, "mypolicy") - .build())) - .putCustom(ComposableIndexTemplateMetadata.TYPE, - new ComposableIndexTemplateMetadata(Collections.singletonMap("mytemplate", - new ComposableIndexTemplate(Collections.singletonList("myds"), - new Template(Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, "mypolicy").build(), null, null), - null, null, null, null, new ComposableIndexTemplate.DataStreamTemplate(false, false))))) - .build()) + .metadata( + Metadata.builder() + .putCustom( + IndexLifecycleMetadata.TYPE, + new IndexLifecycleMetadata( + Collections.singletonMap("mypolicy", LifecyclePolicyMetadataTests.createRandomPolicyMetadata("mypolicy")), + OperationMode.RUNNING + ) + ) + .put( + IndexMetadata.builder("myindex") + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(LifecycleSettings.LIFECYCLE_NAME, "mypolicy") + .build() + ) + ) + .putCustom( + ComposableIndexTemplateMetadata.TYPE, + new ComposableIndexTemplateMetadata( + Collections.singletonMap( + "mytemplate", + new ComposableIndexTemplate( + Collections.singletonList("myds"), + new Template( + Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, "mypolicy").build(), + null, + null + ), + null, + null, + null, + null, + new ComposableIndexTemplate.DataStreamTemplate(false, false) + ) + ) + ) + ) + .build() + ) .build(); - assertThat(LifecyclePolicyUtils.calculateUsage(iner, state, "mypolicy"), - equalTo(new ItemUsage(Collections.singleton("myindex"), Collections.emptyList(), Collections.singleton("mytemplate")))); + assertThat( + LifecyclePolicyUtils.calculateUsage(iner, state, "mypolicy"), + equalTo(new ItemUsage(Collections.singleton("myindex"), Collections.emptyList(), Collections.singleton("mytemplate"))) + ); } { Metadata.Builder mBuilder = Metadata.builder() - .putCustom(IndexLifecycleMetadata.TYPE, - new IndexLifecycleMetadata(Collections.singletonMap("mypolicy", - LifecyclePolicyMetadataTests.createRandomPolicyMetadata("mypolicy")), OperationMode.RUNNING)) - .put(IndexMetadata.builder("myindex") - .settings(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(LifecycleSettings.LIFECYCLE_NAME, "mypolicy") - .build())) - .put(IndexMetadata.builder("another") - .settings(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(LifecycleSettings.LIFECYCLE_NAME, "mypolicy") - .build())) - .put(IndexMetadata.builder("other") - .settings(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(LifecycleSettings.LIFECYCLE_NAME, "otherpolicy") - .build())) + .putCustom( + IndexLifecycleMetadata.TYPE, + new IndexLifecycleMetadata( + Collections.singletonMap("mypolicy", LifecyclePolicyMetadataTests.createRandomPolicyMetadata("mypolicy")), + OperationMode.RUNNING + ) + ) + .put( + IndexMetadata.builder("myindex") + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(LifecycleSettings.LIFECYCLE_NAME, "mypolicy") + .build() + ) + ) + .put( + IndexMetadata.builder("another") + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(LifecycleSettings.LIFECYCLE_NAME, "mypolicy") + .build() + ) + ) + .put( + IndexMetadata.builder("other") + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(LifecycleSettings.LIFECYCLE_NAME, "otherpolicy") + .build() + ) + ) - .putCustom(ComposableIndexTemplateMetadata.TYPE, - new ComposableIndexTemplateMetadata(Collections.singletonMap("mytemplate", - new ComposableIndexTemplate(Collections.singletonList("myds"), - new Template(Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, "mypolicy").build(), null, null), - null, null, null, null, new ComposableIndexTemplate.DataStreamTemplate(false, false))))); + .putCustom( + ComposableIndexTemplateMetadata.TYPE, + new ComposableIndexTemplateMetadata( + Collections.singletonMap( + "mytemplate", + new ComposableIndexTemplate( + Collections.singletonList("myds"), + new Template(Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, "mypolicy").build(), null, null), + null, + null, + null, + null, + new ComposableIndexTemplate.DataStreamTemplate(false, false) + ) + ) + ) + ); // Need to get the real Index instance of myindex: - mBuilder.put(new DataStream("myds", new DataStream.TimestampField("@timestamp"), - Collections.singletonList(mBuilder.get("myindex").getIndex()))); + mBuilder.put( + new DataStream( + "myds", + new DataStream.TimestampField("@timestamp"), + Collections.singletonList(mBuilder.get("myindex").getIndex()) + ) + ); // Test where policy exists and is used by an index, datastream, and template - ClusterState state = ClusterState.builder(new ClusterName("mycluster")) - .metadata(mBuilder.build()) - .build(); - assertThat(LifecyclePolicyUtils.calculateUsage(iner, state, "mypolicy"), - equalTo(new ItemUsage(Arrays.asList("myindex", "another"), - Collections.singleton("myds"), - Collections.singleton("mytemplate")))); + ClusterState state = ClusterState.builder(new ClusterName("mycluster")).metadata(mBuilder.build()).build(); + assertThat( + LifecyclePolicyUtils.calculateUsage(iner, state, "mypolicy"), + equalTo( + new ItemUsage(Arrays.asList("myindex", "another"), Collections.singleton("myds"), Collections.singleton("mytemplate")) + ) + ); } } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/MigrateActionTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/MigrateActionTests.java index 550265f63e2c3..fc795b24418c6 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/MigrateActionTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/MigrateActionTests.java @@ -19,16 +19,16 @@ import java.io.IOException; import java.util.List; -import static org.elasticsearch.index.IndexModule.INDEX_STORE_TYPE_SETTING; -import static org.elasticsearch.snapshots.SearchableSnapshotsSettings.SEARCHABLE_SNAPSHOT_STORE_TYPE; import static org.elasticsearch.cluster.routing.allocation.DataTier.DATA_COLD; import static org.elasticsearch.cluster.routing.allocation.DataTier.DATA_HOT; import static org.elasticsearch.cluster.routing.allocation.DataTier.DATA_WARM; +import static org.elasticsearch.index.IndexModule.INDEX_STORE_TYPE_SETTING; +import static org.elasticsearch.snapshots.SearchableSnapshotsSettings.SEARCHABLE_SNAPSHOT_STORE_TYPE; +import static org.elasticsearch.snapshots.SearchableSnapshotsSettings.SNAPSHOT_PARTIAL_SETTING; import static org.elasticsearch.xpack.core.ilm.TimeseriesLifecycleType.COLD_PHASE; import static org.elasticsearch.xpack.core.ilm.TimeseriesLifecycleType.DELETE_PHASE; import static org.elasticsearch.xpack.core.ilm.TimeseriesLifecycleType.HOT_PHASE; import static org.elasticsearch.xpack.core.ilm.TimeseriesLifecycleType.WARM_PHASE; -import static org.elasticsearch.snapshots.SearchableSnapshotsSettings.SNAPSHOT_PARTIAL_SETTING; import static org.hamcrest.CoreMatchers.is; public class MigrateActionTests extends AbstractActionTestCase { @@ -55,8 +55,11 @@ protected Reader instanceReader() { public void testToSteps() { String phase = randomValueOtherThan(DELETE_PHASE, () -> randomFrom(TimeseriesLifecycleType.ORDERED_VALID_PHASES)); - StepKey nextStepKey = new StepKey(randomAlphaOfLengthBetween(1, 10), randomAlphaOfLengthBetween(1, 10), - randomAlphaOfLengthBetween(1, 10)); + StepKey nextStepKey = new StepKey( + randomAlphaOfLengthBetween(1, 10), + randomAlphaOfLengthBetween(1, 10), + randomAlphaOfLengthBetween(1, 10) + ); { MigrateAction action = new MigrateAction(); List steps = action.toSteps(null, phase, nextStepKey); @@ -83,32 +86,35 @@ public void testToSteps() { } public void testMigrateActionsConfiguresTierPreference() { - StepKey nextStepKey = new StepKey(randomAlphaOfLengthBetween(1, 10), randomAlphaOfLengthBetween(1, 10), - randomAlphaOfLengthBetween(1, 10)); + StepKey nextStepKey = new StepKey( + randomAlphaOfLengthBetween(1, 10), + randomAlphaOfLengthBetween(1, 10), + randomAlphaOfLengthBetween(1, 10) + ); MigrateAction action = new MigrateAction(); { List steps = action.toSteps(null, HOT_PHASE, nextStepKey); UpdateSettingsStep firstStep = (UpdateSettingsStep) steps.get(1); - assertThat(DataTier.TIER_PREFERENCE_SETTING.get(firstStep.getSettings()), - is(DATA_HOT)); + assertThat(DataTier.TIER_PREFERENCE_SETTING.get(firstStep.getSettings()), is(DATA_HOT)); } { List steps = action.toSteps(null, WARM_PHASE, nextStepKey); UpdateSettingsStep firstStep = (UpdateSettingsStep) steps.get(1); - assertThat(DataTier.TIER_PREFERENCE_SETTING.get(firstStep.getSettings()), - is(DATA_WARM + "," + DATA_HOT)); + assertThat(DataTier.TIER_PREFERENCE_SETTING.get(firstStep.getSettings()), is(DATA_WARM + "," + DATA_HOT)); } { List steps = action.toSteps(null, COLD_PHASE, nextStepKey); UpdateSettingsStep firstStep = (UpdateSettingsStep) steps.get(1); - assertThat(DataTier.TIER_PREFERENCE_SETTING.get(firstStep.getSettings()), - is(DATA_COLD + "," + DATA_WARM + "," + DATA_HOT)); + assertThat(DataTier.TIER_PREFERENCE_SETTING.get(firstStep.getSettings()), is(DATA_COLD + "," + DATA_WARM + "," + DATA_HOT)); } } public void testMigrateActionWillSkipAPartiallyMountedIndex() { - StepKey nextStepKey = new StepKey(randomAlphaOfLengthBetween(1, 10), randomAlphaOfLengthBetween(1, 10), - randomAlphaOfLengthBetween(1, 10)); + StepKey nextStepKey = new StepKey( + randomAlphaOfLengthBetween(1, 10), + randomAlphaOfLengthBetween(1, 10), + randomAlphaOfLengthBetween(1, 10) + ); MigrateAction action = new MigrateAction(); // does not skip an ordinary index @@ -134,9 +140,10 @@ public void testMigrateActionWillSkipAPartiallyMountedIndex() { // does skip a partially mounted { IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(5)) - .settings(settings(Version.CURRENT) - .put(INDEX_STORE_TYPE_SETTING.getKey(), SEARCHABLE_SNAPSHOT_STORE_TYPE) - .put(SNAPSHOT_PARTIAL_SETTING.getKey(), true)) + .settings( + settings(Version.CURRENT).put(INDEX_STORE_TYPE_SETTING.getKey(), SEARCHABLE_SNAPSHOT_STORE_TYPE) + .put(SNAPSHOT_PARTIAL_SETTING.getKey(), true) + ) .numberOfShards(1) .numberOfReplicas(2) .build(); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/MockAction.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/MockAction.java index c3f903d9ec2e1..0679365039fb1 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/MockAction.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/MockAction.java @@ -95,7 +95,6 @@ public boolean equals(Object obj) { return false; } MockAction other = (MockAction) obj; - return Objects.equals(steps, other.steps) && - Objects.equals(safe, other.safe); + return Objects.equals(steps, other.steps) && Objects.equals(safe, other.safe); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/MockActionTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/MockActionTests.java index c95a1bcfdee07..b8ac0ec2d44b3 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/MockActionTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/MockActionTests.java @@ -53,8 +53,7 @@ protected MockAction mutateInstance(MockAction instance) throws IOException { } private static Step.StepKey randomStepKey() { - return new Step.StepKey(randomAlphaOfLength(5), - randomAlphaOfLength(5), randomAlphaOfLength(5)); + return new Step.StepKey(randomAlphaOfLength(5), randomAlphaOfLength(5), randomAlphaOfLength(5)); } @Override @@ -68,4 +67,3 @@ public void testToSteps() { assertEquals(action.getSteps(), action.toSteps(null, null, null)); } } - diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/MountSnapshotStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/MountSnapshotStepTests.java index 506a4b27d12e7..f901a2586ebe9 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/MountSnapshotStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/MountSnapshotStepTests.java @@ -53,8 +53,13 @@ public static MountSearchableSnapshotRequest.Storage randomStorageType() { @Override protected MountSnapshotStep copyInstance(MountSnapshotStep instance) { - return new MountSnapshotStep(instance.getKey(), instance.getNextStepKey(), instance.getClient(), instance.getRestoredIndexPrefix(), - instance.getStorage()); + return new MountSnapshotStep( + instance.getKey(), + instance.getNextStepKey(), + instance.getClient(), + instance.getRestoredIndexPrefix(), + instance.getStorage() + ); } @Override @@ -93,39 +98,48 @@ public void testPerformActionFailure() { String policyName = "test-ilm-policy"; { - IndexMetadata.Builder indexMetadataBuilder = - IndexMetadata.builder(indexName).settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)); + IndexMetadata.Builder indexMetadataBuilder = IndexMetadata.builder(indexName) + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)); IndexMetadata indexMetadata = indexMetadataBuilder.build(); - ClusterState clusterState = - ClusterState.builder(emptyClusterState()).metadata(Metadata.builder().put(indexMetadata, true).build()).build(); + ClusterState clusterState = ClusterState.builder(emptyClusterState()) + .metadata(Metadata.builder().put(indexMetadata, true).build()) + .build(); MountSnapshotStep mountSnapshotStep = createRandomInstance(); - Exception e = expectThrows(IllegalStateException.class, () -> PlainActionFuture.get( - f -> mountSnapshotStep.performAction(indexMetadata, clusterState, null, f))); - assertThat(e.getMessage(), - is("snapshot repository is not present for policy [" + policyName + "] and index [" + indexName + "]")); + Exception e = expectThrows( + IllegalStateException.class, + () -> PlainActionFuture.get(f -> mountSnapshotStep.performAction(indexMetadata, clusterState, null, f)) + ); + assertThat( + e.getMessage(), + is("snapshot repository is not present for policy [" + policyName + "] and index [" + indexName + "]") + ); } { - IndexMetadata.Builder indexMetadataBuilder = - IndexMetadata.builder(indexName).settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)); + IndexMetadata.Builder indexMetadataBuilder = IndexMetadata.builder(indexName) + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)); Map ilmCustom = new HashMap<>(); String repository = "repository"; ilmCustom.put("snapshot_repository", repository); indexMetadataBuilder.putCustom(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY, ilmCustom); IndexMetadata indexMetadata = indexMetadataBuilder.build(); - ClusterState clusterState = - ClusterState.builder(emptyClusterState()).metadata(Metadata.builder().put(indexMetadata, true).build()).build(); + ClusterState clusterState = ClusterState.builder(emptyClusterState()) + .metadata(Metadata.builder().put(indexMetadata, true).build()) + .build(); MountSnapshotStep mountSnapshotStep = createRandomInstance(); - Exception e = expectThrows(IllegalStateException.class, () -> PlainActionFuture.get( - f -> mountSnapshotStep.performAction(indexMetadata, clusterState, null, f))); - assertThat(e.getMessage(), - is("snapshot name was not generated for policy [" + policyName + "] and index [" + indexName + "]")); + Exception e = expectThrows( + IllegalStateException.class, + () -> PlainActionFuture.get(f -> mountSnapshotStep.performAction(indexMetadata, clusterState, null, f)) + ); + assertThat(e.getMessage(), is("snapshot name was not generated for policy [" + policyName + "] and index [" + indexName + "]")); } } @@ -138,19 +152,33 @@ public void testPerformAction() throws Exception { String repository = "repository"; ilmCustom.put("snapshot_repository", repository); - IndexMetadata.Builder indexMetadataBuilder = - IndexMetadata.builder(indexName).settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) - .putCustom(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY, ilmCustom) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)); + IndexMetadata.Builder indexMetadataBuilder = IndexMetadata.builder(indexName) + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) + .putCustom(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY, ilmCustom) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)); IndexMetadata indexMetadata = indexMetadataBuilder.build(); - ClusterState clusterState = - ClusterState.builder(emptyClusterState()).metadata(Metadata.builder().put(indexMetadata, true).build()).build(); - - try (NoOpClient client = - getRestoreSnapshotRequestAssertingClient(repository, snapshotName, indexName, RESTORED_INDEX_PREFIX, indexName)) { - MountSnapshotStep step = - new MountSnapshotStep(randomStepKey(), randomStepKey(), client, RESTORED_INDEX_PREFIX, randomStorageType()); + ClusterState clusterState = ClusterState.builder(emptyClusterState()) + .metadata(Metadata.builder().put(indexMetadata, true).build()) + .build(); + + try ( + NoOpClient client = getRestoreSnapshotRequestAssertingClient( + repository, + snapshotName, + indexName, + RESTORED_INDEX_PREFIX, + indexName + ) + ) { + MountSnapshotStep step = new MountSnapshotStep( + randomStepKey(), + randomStepKey(), + client, + RESTORED_INDEX_PREFIX, + randomStorageType() + ); PlainActionFuture.get(f -> step.performAction(indexMetadata, clusterState, null, f)); } } @@ -164,20 +192,27 @@ public void testResponseStatusHandling() throws Exception { String repository = "repository"; ilmCustom.put("snapshot_repository", repository); - IndexMetadata.Builder indexMetadataBuilder = - IndexMetadata.builder(indexName).settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) - .putCustom(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY, ilmCustom) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)); + IndexMetadata.Builder indexMetadataBuilder = IndexMetadata.builder(indexName) + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) + .putCustom(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY, ilmCustom) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)); IndexMetadata indexMetadata = indexMetadataBuilder.build(); - ClusterState clusterState = - ClusterState.builder(emptyClusterState()).metadata(Metadata.builder().put(indexMetadata, true).build()).build(); + ClusterState clusterState = ClusterState.builder(emptyClusterState()) + .metadata(Metadata.builder().put(indexMetadata, true).build()) + .build(); { RestoreSnapshotResponse responseWithOKStatus = new RestoreSnapshotResponse(new RestoreInfo("test", List.of(), 1, 1)); try (NoOpClient clientPropagatingOKResponse = getClientTriggeringResponse(responseWithOKStatus)) { - MountSnapshotStep step = new MountSnapshotStep(randomStepKey(), randomStepKey(), clientPropagatingOKResponse, - RESTORED_INDEX_PREFIX, randomStorageType()); + MountSnapshotStep step = new MountSnapshotStep( + randomStepKey(), + randomStepKey(), + clientPropagatingOKResponse, + RESTORED_INDEX_PREFIX, + randomStorageType() + ); PlainActionFuture.get(f -> step.performAction(indexMetadata, clusterState, null, f)); } } @@ -185,8 +220,13 @@ public void testResponseStatusHandling() throws Exception { { RestoreSnapshotResponse responseWithACCEPTEDStatus = new RestoreSnapshotResponse((RestoreInfo) null); try (NoOpClient clientPropagatingACCEPTEDResponse = getClientTriggeringResponse(responseWithACCEPTEDStatus)) { - MountSnapshotStep step = new MountSnapshotStep(randomStepKey(), randomStepKey(), clientPropagatingACCEPTEDResponse, - RESTORED_INDEX_PREFIX, randomStorageType()); + MountSnapshotStep step = new MountSnapshotStep( + randomStepKey(), + randomStepKey(), + clientPropagatingACCEPTEDResponse, + RESTORED_INDEX_PREFIX, + randomStorageType() + ); PlainActionFuture.get(f -> step.performAction(indexMetadata, clusterState, null, f)); } } @@ -217,8 +257,9 @@ public void testMountWithPartialPrefix() throws Exception { } public void testMountWithPartialAndRestoredPrefix() throws Exception { - doTestMountWithoutSnapshotIndexNameInState(SearchableSnapshotAction.PARTIAL_RESTORED_INDEX_PREFIX + - SearchableSnapshotAction.FULL_RESTORED_INDEX_PREFIX); + doTestMountWithoutSnapshotIndexNameInState( + SearchableSnapshotAction.PARTIAL_RESTORED_INDEX_PREFIX + SearchableSnapshotAction.FULL_RESTORED_INDEX_PREFIX + ); } public void doTestMountWithoutSnapshotIndexNameInState(String prefix) throws Exception { @@ -232,20 +273,33 @@ public void doTestMountWithoutSnapshotIndexNameInState(String prefix) throws Exc String repository = "repository"; ilmCustom.put("snapshot_repository", repository); - IndexMetadata.Builder indexMetadataBuilder = - IndexMetadata.builder(indexName).settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) - .putCustom(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY, ilmCustom) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)); + IndexMetadata.Builder indexMetadataBuilder = IndexMetadata.builder(indexName) + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) + .putCustom(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY, ilmCustom) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)); IndexMetadata indexMetadata = indexMetadataBuilder.build(); - ClusterState clusterState = - ClusterState.builder(emptyClusterState()).metadata(Metadata.builder().put(indexMetadata, true).build()).build(); - - try (NoOpClient client = - getRestoreSnapshotRequestAssertingClient(repository, snapshotName, - indexName, RESTORED_INDEX_PREFIX, indexNameSnippet)) { - MountSnapshotStep step = - new MountSnapshotStep(randomStepKey(), randomStepKey(), client, RESTORED_INDEX_PREFIX, randomStorageType()); + ClusterState clusterState = ClusterState.builder(emptyClusterState()) + .metadata(Metadata.builder().put(indexMetadata, true).build()) + .build(); + + try ( + NoOpClient client = getRestoreSnapshotRequestAssertingClient( + repository, + snapshotName, + indexName, + RESTORED_INDEX_PREFIX, + indexNameSnippet + ) + ) { + MountSnapshotStep step = new MountSnapshotStep( + randomStepKey(), + randomStepKey(), + client, + RESTORED_INDEX_PREFIX, + randomStorageType() + ); PlainActionFuture.get(f -> step.performAction(indexMetadata, clusterState, null, f)); } } @@ -255,29 +309,41 @@ public void doTestMountWithoutSnapshotIndexNameInState(String prefix) throws Exc private NoOpClient getClientTriggeringResponse(RestoreSnapshotResponse response) { return new NoOpClient(getTestName()) { @Override - protected void doExecute(ActionType action, - Request request, - ActionListener listener) { + protected void doExecute( + ActionType action, + Request request, + ActionListener listener + ) { listener.onResponse((Response) response); } }; } @SuppressWarnings("unchecked") - private NoOpClient getRestoreSnapshotRequestAssertingClient(String expectedRepoName, String expectedSnapshotName, String indexName, - String restoredIndexPrefix, String expectedSnapshotIndexName) { + private NoOpClient getRestoreSnapshotRequestAssertingClient( + String expectedRepoName, + String expectedSnapshotName, + String indexName, + String restoredIndexPrefix, + String expectedSnapshotIndexName + ) { return new NoOpClient(getTestName()) { @Override - protected void doExecute(ActionType action, - Request request, - ActionListener listener) { + protected void doExecute( + ActionType action, + Request request, + ActionListener listener + ) { assertThat(action.name(), is(MountSearchableSnapshotAction.NAME)); assertTrue(request instanceof MountSearchableSnapshotRequest); MountSearchableSnapshotRequest mountSearchableSnapshotRequest = (MountSearchableSnapshotRequest) request; assertThat(mountSearchableSnapshotRequest.repositoryName(), is(expectedRepoName)); assertThat(mountSearchableSnapshotRequest.snapshotName(), is(expectedSnapshotName)); - assertThat("another ILM step will wait for the restore to complete. the " + MountSnapshotStep.NAME + " step should not", - mountSearchableSnapshotRequest.waitForCompletion(), is(false)); + assertThat( + "another ILM step will wait for the restore to complete. the " + MountSnapshotStep.NAME + " step should not", + mountSearchableSnapshotRequest.waitForCompletion(), + is(false) + ); assertThat(mountSearchableSnapshotRequest.ignoreIndexSettings(), is(notNullValue())); assertThat(mountSearchableSnapshotRequest.ignoreIndexSettings()[0], is(LifecycleSettings.LIFECYCLE_NAME)); assertThat(mountSearchableSnapshotRequest.mountedIndexName(), is(restoredIndexPrefix + indexName)); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/OpenIndexStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/OpenIndexStepTests.java index 4c522a34571b3..d8ce33838fb15 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/OpenIndexStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/OpenIndexStepTests.java @@ -61,7 +61,8 @@ protected OpenIndexStep copyInstance(OpenIndexStep instance) { } public void testPerformAction() throws Exception { - IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)).settings(settings(Version.CURRENT)) + IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) + .settings(settings(Version.CURRENT)) .numberOfShards(randomIntBetween(1, 5)) .numberOfReplicas(randomIntBetween(0, 5)) .state(IndexMetadata.State.CLOSE) @@ -79,7 +80,7 @@ public void testPerformAction() throws Exception { OpenIndexRequest request = (OpenIndexRequest) invocation.getArguments()[0]; @SuppressWarnings("unchecked") ActionListener listener = (ActionListener) invocation.getArguments()[1]; - assertThat(request.indices(), equalTo(new String[]{indexMetadata.getIndex().getName()})); + assertThat(request.indices(), equalTo(new String[] { indexMetadata.getIndex().getName() })); listener.onResponse(new OpenIndexResponse(true, true)); return null; }).when(indicesClient).open(Mockito.any(), Mockito.any()); @@ -91,9 +92,9 @@ public void testPerformAction() throws Exception { Mockito.verify(indicesClient, Mockito.only()).open(Mockito.any(), Mockito.any()); } - public void testPerformActionFailure() { - IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)).settings(settings(Version.CURRENT)) + IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) + .settings(settings(Version.CURRENT)) .numberOfShards(randomIntBetween(1, 5)) .numberOfReplicas(randomIntBetween(0, 5)) .state(IndexMetadata.State.CLOSE) @@ -111,13 +112,18 @@ public void testPerformActionFailure() { OpenIndexRequest request = (OpenIndexRequest) invocation.getArguments()[0]; @SuppressWarnings("unchecked") ActionListener listener = (ActionListener) invocation.getArguments()[1]; - assertThat(request.indices(), equalTo(new String[]{indexMetadata.getIndex().getName()})); + assertThat(request.indices(), equalTo(new String[] { indexMetadata.getIndex().getName() })); listener.onFailure(exception); return null; }).when(indicesClient).open(Mockito.any(), Mockito.any()); - assertSame(exception, expectThrows(Exception.class, () -> PlainActionFuture.get( - f -> step.performAction(indexMetadata, null, null, f)))); + assertSame( + exception, + expectThrows( + Exception.class, + () -> PlainActionFuture.get(f -> step.performAction(indexMetadata, null, null, f)) + ) + ); Mockito.verify(client, Mockito.only()).admin(); Mockito.verify(adminClient, Mockito.only()).indices(); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/PauseFollowerIndexStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/PauseFollowerIndexStepTests.java index 7b847eeec65b8..48bfc3bfe55c8 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/PauseFollowerIndexStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/PauseFollowerIndexStepTests.java @@ -75,8 +75,10 @@ public void testRequestNotAcknowledged() { }).when(client).execute(Mockito.same(PauseFollowAction.INSTANCE), Mockito.any(), Mockito.any()); PauseFollowerIndexStep step = new PauseFollowerIndexStep(randomStepKey(), randomStepKey(), client); - Exception e = expectThrows(Exception.class, - () -> PlainActionFuture.get(f -> step.performAction(indexMetadata, clusterState, null, f))); + Exception e = expectThrows( + Exception.class, + () -> PlainActionFuture.get(f -> step.performAction(indexMetadata, clusterState, null, f)) + ); assertThat(e.getMessage(), is("pause follow request failed to be acknowledged")); } @@ -100,8 +102,13 @@ public void testPauseFollowingIndexFailed() { }).when(client).execute(Mockito.same(PauseFollowAction.INSTANCE), Mockito.any(), Mockito.any()); PauseFollowerIndexStep step = new PauseFollowerIndexStep(randomStepKey(), randomStepKey(), client); - assertSame(error, expectThrows(Exception.class, - () -> PlainActionFuture.get(f -> step.performAction(indexMetadata, clusterState, null, f)))); + assertSame( + error, + expectThrows( + Exception.class, + () -> PlainActionFuture.get(f -> step.performAction(indexMetadata, clusterState, null, f)) + ) + ); Mockito.verify(client).execute(Mockito.same(PauseFollowAction.INSTANCE), Mockito.any(), Mockito.any()); Mockito.verifyNoMoreInteractions(client); @@ -117,10 +124,12 @@ public final void testNoShardFollowPersistentTasks() throws Exception { PersistentTasksCustomMetadata.Builder emptyPersistentTasks = PersistentTasksCustomMetadata.builder(); ClusterState clusterState = ClusterState.builder(new ClusterName("_cluster")) - .metadata(Metadata.builder() - .putCustom(PersistentTasksCustomMetadata.TYPE, emptyPersistentTasks.build()) - .put(indexMetadata, false) - .build()) + .metadata( + Metadata.builder() + .putCustom(PersistentTasksCustomMetadata.TYPE, emptyPersistentTasks.build()) + .put(indexMetadata, false) + .build() + ) .build(); PauseFollowerIndexStep step = newInstance(randomStepKey(), randomStepKey()); @@ -143,8 +152,9 @@ public final void testNoShardFollowTasksForManagedIndex() throws Exception { .numberOfShards(1) .numberOfReplicas(0) .build(); - final ClusterState clusterState = ClusterState.builder( - setupClusterStateWithFollowingIndex(followerIndex)).metadata(Metadata.builder().put(managedIndex, false).build()).build(); + final ClusterState clusterState = ClusterState.builder(setupClusterStateWithFollowingIndex(followerIndex)) + .metadata(Metadata.builder().put(managedIndex, false).build()) + .build(); PauseFollowerIndexStep step = newInstance(randomStepKey(), randomStepKey()); PlainActionFuture.get(f -> step.performAction(managedIndex, clusterState, null, f)); @@ -154,28 +164,32 @@ public final void testNoShardFollowTasksForManagedIndex() throws Exception { private static ClusterState setupClusterStateWithFollowingIndex(IndexMetadata followerIndex) { PersistentTasksCustomMetadata.Builder persistentTasks = PersistentTasksCustomMetadata.builder() - .addTask("1", ShardFollowTask.NAME, new ShardFollowTask( - null, - new ShardId(followerIndex.getIndex(), 0), - new ShardId("leader_index", "", 0), - 1024, - 1024, - 1, - 1, - new ByteSizeValue(32, ByteSizeUnit.MB), - new ByteSizeValue(Long.MAX_VALUE, ByteSizeUnit.BYTES), - 10240, - new ByteSizeValue(512, ByteSizeUnit.MB), - TimeValue.timeValueMillis(10), - TimeValue.timeValueMillis(10), - Collections.emptyMap() - ), null); + .addTask( + "1", + ShardFollowTask.NAME, + new ShardFollowTask( + null, + new ShardId(followerIndex.getIndex(), 0), + new ShardId("leader_index", "", 0), + 1024, + 1024, + 1, + 1, + new ByteSizeValue(32, ByteSizeUnit.MB), + new ByteSizeValue(Long.MAX_VALUE, ByteSizeUnit.BYTES), + 10240, + new ByteSizeValue(512, ByteSizeUnit.MB), + TimeValue.timeValueMillis(10), + TimeValue.timeValueMillis(10), + Collections.emptyMap() + ), + null + ); return ClusterState.builder(new ClusterName("_cluster")) - .metadata(Metadata.builder() - .putCustom(PersistentTasksCustomMetadata.TYPE, persistentTasks.build()) - .put(followerIndex, false) - .build()) + .metadata( + Metadata.builder().putCustom(PersistentTasksCustomMetadata.TYPE, persistentTasks.build()).put(followerIndex, false).build() + ) .build(); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/PhaseCacheManagementTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/PhaseCacheManagementTests.java index df0b6ee75b67c..3edb0f4a342e8 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/PhaseCacheManagementTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/PhaseCacheManagementTests.java @@ -14,10 +14,10 @@ import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.core.TimeValue; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import java.util.Collections; import java.util.HashMap; @@ -42,11 +42,13 @@ public class PhaseCacheManagementTests extends ESTestCase { private static final String index = "eggplant"; static { - REGISTRY = new NamedXContentRegistry(List.of( - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(RolloverAction.NAME), RolloverAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(SetPriorityAction.NAME), SetPriorityAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ForceMergeAction.NAME), ForceMergeAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(AllocateAction.NAME), AllocateAction::parse)) + REGISTRY = new NamedXContentRegistry( + List.of( + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(RolloverAction.NAME), RolloverAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(SetPriorityAction.NAME), SetPriorityAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ForceMergeAction.NAME), ForceMergeAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(AllocateAction.NAME), AllocateAction::parse) + ) ); } @@ -55,22 +57,24 @@ public void testRefreshPhaseJson() { .setPhase("hot") .setAction("rollover") .setStep("check-rollover-ready") - .setPhaseDefinition("{\n" + - " \"policy\" : \"my-policy\",\n" + - " \"phase_definition\" : {\n" + - " \"min_age\" : \"20m\",\n" + - " \"actions\" : {\n" + - " \"rollover\" : {\n" + - " \"max_age\" : \"5s\"\n" + - " },\n" + - " \"set_priority\" : {\n" + - " \"priority\" : 150\n" + - " }\n" + - " }\n" + - " },\n" + - " \"version\" : 1,\n" + - " \"modified_date_in_millis\" : 1578521007076\n" + - " }"); + .setPhaseDefinition( + "{\n" + + " \"policy\" : \"my-policy\",\n" + + " \"phase_definition\" : {\n" + + " \"min_age\" : \"20m\",\n" + + " \"actions\" : {\n" + + " \"rollover\" : {\n" + + " \"max_age\" : \"5s\"\n" + + " },\n" + + " \"set_priority\" : {\n" + + " \"priority\" : 150\n" + + " }\n" + + " }\n" + + " },\n" + + " \"version\" : 1,\n" + + " \"modified_date_in_millis\" : 1578521007076\n" + + " }" + ); IndexMetadata meta = buildIndexMetadata("my-policy", exState); String index = meta.getIndex().getName(); @@ -84,9 +88,7 @@ public void testRefreshPhaseJson() { LifecyclePolicyMetadata policyMetadata = new LifecyclePolicyMetadata(newPolicy, Collections.emptyMap(), 2L, 2L); ClusterState existingState = ClusterState.builder(ClusterState.EMPTY_STATE) - .metadata(Metadata.builder(Metadata.EMPTY_METADATA) - .put(meta, false) - .build()) + .metadata(Metadata.builder(Metadata.EMPTY_METADATA).put(meta, false).build()) .build(); ClusterState changedState = refreshPhaseDefinition(existingState, index, policyMetadata); @@ -101,56 +103,62 @@ public void testRefreshPhaseJson() { assertThat(beforeState, equalTo(afterState)); // Check that the phase definition has been refreshed - assertThat(afterExState.getPhaseDefinition(), - equalTo("{\"policy\":\"my-policy\",\"phase_definition\":{\"min_age\":\"0ms\",\"actions\":{\"rollover\":{\"max_docs\":1}," + - "\"set_priority\":{\"priority\":100}}},\"version\":2,\"modified_date_in_millis\":2}")); + assertThat( + afterExState.getPhaseDefinition(), + equalTo( + "{\"policy\":\"my-policy\",\"phase_definition\":{\"min_age\":\"0ms\",\"actions\":{\"rollover\":{\"max_docs\":1}," + + "\"set_priority\":{\"priority\":100}}},\"version\":2,\"modified_date_in_millis\":2}" + ) + ); } public void testEligibleForRefresh() { IndexMetadata meta = IndexMetadata.builder("index") - .settings(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 10)) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, randomIntBetween(0, 5)) - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(IndexMetadata.SETTING_INDEX_UUID, randomAlphaOfLength(5))) + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 10)) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, randomIntBetween(0, 5)) + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetadata.SETTING_INDEX_UUID, randomAlphaOfLength(5)) + ) .build(); assertFalse(eligibleToCheckForRefresh(meta)); LifecycleExecutionState state = LifecycleExecutionState.builder().build(); meta = IndexMetadata.builder("index") - .settings(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 10)) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, randomIntBetween(0, 5)) - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(IndexMetadata.SETTING_INDEX_UUID, randomAlphaOfLength(5))) + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 10)) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, randomIntBetween(0, 5)) + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetadata.SETTING_INDEX_UUID, randomAlphaOfLength(5)) + ) .putCustom(ILM_CUSTOM_METADATA_KEY, state.asMap()) .build(); assertFalse(eligibleToCheckForRefresh(meta)); - state = LifecycleExecutionState.builder() - .setPhase("phase") - .setAction("action") - .setStep("step") - .build(); + state = LifecycleExecutionState.builder().setPhase("phase").setAction("action").setStep("step").build(); meta = IndexMetadata.builder("index") - .settings(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 10)) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, randomIntBetween(0, 5)) - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(IndexMetadata.SETTING_INDEX_UUID, randomAlphaOfLength(5))) + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 10)) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, randomIntBetween(0, 5)) + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetadata.SETTING_INDEX_UUID, randomAlphaOfLength(5)) + ) .putCustom(ILM_CUSTOM_METADATA_KEY, state.asMap()) .build(); assertFalse(eligibleToCheckForRefresh(meta)); - state = LifecycleExecutionState.builder() - .setPhaseDefinition("{}") - .build(); + state = LifecycleExecutionState.builder().setPhaseDefinition("{}").build(); meta = IndexMetadata.builder("index") - .settings(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 10)) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, randomIntBetween(0, 5)) - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(IndexMetadata.SETTING_INDEX_UUID, randomAlphaOfLength(5))) + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 10)) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, randomIntBetween(0, 5)) + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetadata.SETTING_INDEX_UUID, randomAlphaOfLength(5)) + ) .putCustom(ILM_CUSTOM_METADATA_KEY, state.asMap()) .build(); assertFalse(eligibleToCheckForRefresh(meta)); @@ -162,27 +170,26 @@ public void testEligibleForRefresh() { .setPhaseDefinition("{}") .build(); meta = IndexMetadata.builder("index") - .settings(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 10)) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, randomIntBetween(0, 5)) - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(IndexMetadata.SETTING_INDEX_UUID, randomAlphaOfLength(5))) + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 10)) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, randomIntBetween(0, 5)) + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetadata.SETTING_INDEX_UUID, randomAlphaOfLength(5)) + ) .putCustom(ILM_CUSTOM_METADATA_KEY, state.asMap()) .build(); assertFalse(eligibleToCheckForRefresh(meta)); - state = LifecycleExecutionState.builder() - .setPhase("phase") - .setAction("action") - .setStep("step") - .setPhaseDefinition("{}") - .build(); + state = LifecycleExecutionState.builder().setPhase("phase").setAction("action").setStep("step").setPhaseDefinition("{}").build(); meta = IndexMetadata.builder("index") - .settings(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 10)) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, randomIntBetween(0, 5)) - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(IndexMetadata.SETTING_INDEX_UUID, randomAlphaOfLength(5))) + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 10)) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, randomIntBetween(0, 5)) + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetadata.SETTING_INDEX_UUID, randomAlphaOfLength(5)) + ) .putCustom(ILM_CUSTOM_METADATA_KEY, state.asMap()) .build(); assertTrue(eligibleToCheckForRefresh(meta)); @@ -194,47 +201,67 @@ public void testReadStepKeys() { assertNull(readStepKeys(REGISTRY, client, "aoeu", "phase", null)); assertNull(readStepKeys(REGISTRY, client, "", "phase", null)); - assertThat(readStepKeys(REGISTRY, client, "{\n" + - " \"policy\": \"my_lifecycle3\",\n" + - " \"phase_definition\": { \n" + - " \"min_age\": \"0ms\",\n" + - " \"actions\": {\n" + - " \"rollover\": {\n" + - " \"max_age\": \"30s\"\n" + - " }\n" + - " }\n" + - " },\n" + - " \"version\": 3, \n" + - " \"modified_date_in_millis\": 1539609701576 \n" + - " }", "phase", null), - contains(new Step.StepKey("phase", "rollover", WaitForRolloverReadyStep.NAME), + assertThat( + readStepKeys( + REGISTRY, + client, + "{\n" + + " \"policy\": \"my_lifecycle3\",\n" + + " \"phase_definition\": { \n" + + " \"min_age\": \"0ms\",\n" + + " \"actions\": {\n" + + " \"rollover\": {\n" + + " \"max_age\": \"30s\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"version\": 3, \n" + + " \"modified_date_in_millis\": 1539609701576 \n" + + " }", + "phase", + null + ), + contains( + new Step.StepKey("phase", "rollover", WaitForRolloverReadyStep.NAME), new Step.StepKey("phase", "rollover", RolloverStep.NAME), new Step.StepKey("phase", "rollover", WaitForActiveShardsStep.NAME), new Step.StepKey("phase", "rollover", UpdateRolloverLifecycleDateStep.NAME), - new Step.StepKey("phase", "rollover", RolloverAction.INDEXING_COMPLETE_STEP_NAME))); - - assertThat(readStepKeys(REGISTRY, client, "{\n" + - " \"policy\" : \"my_lifecycle3\",\n" + - " \"phase_definition\" : {\n" + - " \"min_age\" : \"20m\",\n" + - " \"actions\" : {\n" + - " \"rollover\" : {\n" + - " \"max_age\" : \"5s\"\n" + - " },\n" + - " \"set_priority\" : {\n" + - " \"priority\" : 150\n" + - " }\n" + - " }\n" + - " },\n" + - " \"version\" : 1,\n" + - " \"modified_date_in_millis\" : 1578521007076\n" + - " }", "phase", null), - containsInAnyOrder(new Step.StepKey("phase", "rollover", WaitForRolloverReadyStep.NAME), + new Step.StepKey("phase", "rollover", RolloverAction.INDEXING_COMPLETE_STEP_NAME) + ) + ); + + assertThat( + readStepKeys( + REGISTRY, + client, + "{\n" + + " \"policy\" : \"my_lifecycle3\",\n" + + " \"phase_definition\" : {\n" + + " \"min_age\" : \"20m\",\n" + + " \"actions\" : {\n" + + " \"rollover\" : {\n" + + " \"max_age\" : \"5s\"\n" + + " },\n" + + " \"set_priority\" : {\n" + + " \"priority\" : 150\n" + + " }\n" + + " }\n" + + " },\n" + + " \"version\" : 1,\n" + + " \"modified_date_in_millis\" : 1578521007076\n" + + " }", + "phase", + null + ), + containsInAnyOrder( + new Step.StepKey("phase", "rollover", WaitForRolloverReadyStep.NAME), new Step.StepKey("phase", "rollover", RolloverStep.NAME), new Step.StepKey("phase", "rollover", WaitForActiveShardsStep.NAME), new Step.StepKey("phase", "rollover", UpdateRolloverLifecycleDateStep.NAME), new Step.StepKey("phase", "rollover", RolloverAction.INDEXING_COMPLETE_STEP_NAME), - new Step.StepKey("phase", "set_priority", SetPriorityAction.NAME))); + new Step.StepKey("phase", "set_priority", SetPriorityAction.NAME) + ) + ); Map actions = new HashMap<>(); actions.put("forcemerge", new ForceMergeAction(5, null)); @@ -243,7 +270,8 @@ public void testReadStepKeys() { String phaseDef = Strings.toString(pei); logger.info("--> phaseDef: {}", phaseDef); - assertThat(readStepKeys(REGISTRY, client, phaseDef, "phase", null), + assertThat( + readStepKeys(REGISTRY, client, phaseDef, "phase", null), contains( new Step.StepKey("phase", "allocate", AllocateAction.NAME), new Step.StepKey("phase", "allocate", AllocationRoutedStep.NAME), @@ -251,7 +279,9 @@ public void testReadStepKeys() { new Step.StepKey("phase", "forcemerge", CheckNotDataStreamWriteIndexStep.NAME), new Step.StepKey("phase", "forcemerge", ReadOnlyAction.NAME), new Step.StepKey("phase", "forcemerge", ForceMergeAction.NAME), - new Step.StepKey("phase", "forcemerge", SegmentCountStep.NAME))); + new Step.StepKey("phase", "forcemerge", SegmentCountStep.NAME) + ) + ); } public void testIndexCanBeSafelyUpdated() { @@ -263,27 +293,27 @@ public void testIndexCanBeSafelyUpdated() { .setPhase("hot") .setAction("rollover") .setStep("check-rollover-ready") - .setPhaseDefinition("{\n" + - " \"policy\" : \"my-policy\",\n" + - " \"phase_definition\" : {\n" + - " \"min_age\" : \"20m\",\n" + - " \"actions\" : {\n" + - " \"rollover\" : {\n" + - " \"max_age\" : \"5s\"\n" + - " },\n" + - " \"set_priority\" : {\n" + - " \"priority\" : 150\n" + - " }\n" + - " }\n" + - " },\n" + - " \"version\" : 1,\n" + - " \"modified_date_in_millis\" : 1578521007076\n" + - " }") + .setPhaseDefinition( + "{\n" + + " \"policy\" : \"my-policy\",\n" + + " \"phase_definition\" : {\n" + + " \"min_age\" : \"20m\",\n" + + " \"actions\" : {\n" + + " \"rollover\" : {\n" + + " \"max_age\" : \"5s\"\n" + + " },\n" + + " \"set_priority\" : {\n" + + " \"priority\" : 150\n" + + " }\n" + + " }\n" + + " },\n" + + " \"version\" : 1,\n" + + " \"modified_date_in_millis\" : 1578521007076\n" + + " }" + ) .build(); - IndexMetadata meta = mkMeta() - .putCustom(ILM_CUSTOM_METADATA_KEY, exState.asMap()) - .build(); + IndexMetadata meta = mkMeta().putCustom(ILM_CUSTOM_METADATA_KEY, exState.asMap()).build(); Map actions = new HashMap<>(); actions.put("rollover", new RolloverAction(null, null, null, 1L)); @@ -301,27 +331,27 @@ public void testIndexCanBeSafelyUpdated() { .setPhase("hot") .setAction("rollover") .setStep("check-rollover-ready") - .setPhaseDefinition("{\n" + - " \"policy\" : \"my-policy\",\n" + - " \"phase_definition\" : {\n" + - " \"min_age\" : \"20m\",\n" + - " \"actions\" : {\n" + - " \"rollover\" : {\n" + - " \"max_age\" : \"5s\"\n" + - " },\n" + - " \"set_priority\" : {\n" + - " \"priority\" : 150\n" + - " }\n" + - " }\n" + - " },\n" + - " \"version\" : 1,\n" + - " \"modified_date_in_millis\" : 1578521007076\n" + - " }") + .setPhaseDefinition( + "{\n" + + " \"policy\" : \"my-policy\",\n" + + " \"phase_definition\" : {\n" + + " \"min_age\" : \"20m\",\n" + + " \"actions\" : {\n" + + " \"rollover\" : {\n" + + " \"max_age\" : \"5s\"\n" + + " },\n" + + " \"set_priority\" : {\n" + + " \"priority\" : 150\n" + + " }\n" + + " }\n" + + " },\n" + + " \"version\" : 1,\n" + + " \"modified_date_in_millis\" : 1578521007076\n" + + " }" + ) .build(); - IndexMetadata meta = mkMeta() - .putCustom(ILM_CUSTOM_METADATA_KEY, exState.asMap()) - .build(); + IndexMetadata meta = mkMeta().putCustom(ILM_CUSTOM_METADATA_KEY, exState.asMap()).build(); Map actions = new HashMap<>(); actions.put("set_priority", new SetPriorityAction(150)); @@ -338,27 +368,27 @@ public void testIndexCanBeSafelyUpdated() { .setPhase("hot") .setAction("rollover") .setStep("check-rollover-ready") - .setPhaseDefinition("{\n" + - " \"policy\" : \"my-policy\",\n" + - " \"phase_definition\" : {\n" + - " \"min_age\" : \"20m\",\n" + - " \"actions\" : {\n" + - " \"rollover\" : {\n" + - " \"max_age\" : \"5s\"\n" + - " },\n" + - " \"set_priority\" : {\n" + - " \"priority\" : 150\n" + - " }\n" + - " }\n" + - " },\n" + - " \"version\" : 1,\n" + - " \"modified_date_in_millis\" : 1578521007076\n" + - " }") + .setPhaseDefinition( + "{\n" + + " \"policy\" : \"my-policy\",\n" + + " \"phase_definition\" : {\n" + + " \"min_age\" : \"20m\",\n" + + " \"actions\" : {\n" + + " \"rollover\" : {\n" + + " \"max_age\" : \"5s\"\n" + + " },\n" + + " \"set_priority\" : {\n" + + " \"priority\" : 150\n" + + " }\n" + + " }\n" + + " },\n" + + " \"version\" : 1,\n" + + " \"modified_date_in_millis\" : 1578521007076\n" + + " }" + ) .build(); - IndexMetadata meta = mkMeta() - .putCustom(ILM_CUSTOM_METADATA_KEY, exState.asMap()) - .build(); + IndexMetadata meta = mkMeta().putCustom(ILM_CUSTOM_METADATA_KEY, exState.asMap()).build(); Map actions = new HashMap<>(); actions.put("rollover", new RolloverAction(null, null, TimeValue.timeValueSeconds(5), null)); @@ -372,27 +402,27 @@ public void testIndexCanBeSafelyUpdated() { // Failure case, index doesn't have enough info to check { LifecycleExecutionState exState = LifecycleExecutionState.builder() - .setPhaseDefinition("{\n" + - " \"policy\" : \"my-policy\",\n" + - " \"phase_definition\" : {\n" + - " \"min_age\" : \"20m\",\n" + - " \"actions\" : {\n" + - " \"rollover\" : {\n" + - " \"max_age\" : \"5s\"\n" + - " },\n" + - " \"set_priority\" : {\n" + - " \"priority\" : 150\n" + - " }\n" + - " }\n" + - " },\n" + - " \"version\" : 1,\n" + - " \"modified_date_in_millis\" : 1578521007076\n" + - " }") + .setPhaseDefinition( + "{\n" + + " \"policy\" : \"my-policy\",\n" + + " \"phase_definition\" : {\n" + + " \"min_age\" : \"20m\",\n" + + " \"actions\" : {\n" + + " \"rollover\" : {\n" + + " \"max_age\" : \"5s\"\n" + + " },\n" + + " \"set_priority\" : {\n" + + " \"priority\" : 150\n" + + " }\n" + + " }\n" + + " },\n" + + " \"version\" : 1,\n" + + " \"modified_date_in_millis\" : 1578521007076\n" + + " }" + ) .build(); - IndexMetadata meta = mkMeta() - .putCustom(ILM_CUSTOM_METADATA_KEY, exState.asMap()) - .build(); + IndexMetadata meta = mkMeta().putCustom(ILM_CUSTOM_METADATA_KEY, exState.asMap()).build(); Map actions = new HashMap<>(); actions.put("rollover", new RolloverAction(null, null, null, 1L)); @@ -413,9 +443,7 @@ public void testIndexCanBeSafelyUpdated() { .setPhaseDefinition("potato") .build(); - IndexMetadata meta = mkMeta() - .putCustom(ILM_CUSTOM_METADATA_KEY, exState.asMap()) - .build(); + IndexMetadata meta = mkMeta().putCustom(ILM_CUSTOM_METADATA_KEY, exState.asMap()).build(); Map actions = new HashMap<>(); actions.put("rollover", new RolloverAction(null, null, null, 1L)); @@ -433,13 +461,13 @@ public void testUpdateIndicesForPolicy() { .setPhase("hot") .setAction("rollover") .setStep("check-rollover-ready") - .setPhaseDefinition("{\"policy\":\"my-policy\",\"phase_definition\":{\"min_age\":\"0ms\",\"actions\":{\"rollover\":" + - "{\"max_docs\":1},\"set_priority\":{\"priority\":100}}},\"version\":1,\"modified_date_in_millis\":1578521007076}") + .setPhaseDefinition( + "{\"policy\":\"my-policy\",\"phase_definition\":{\"min_age\":\"0ms\",\"actions\":{\"rollover\":" + + "{\"max_docs\":1},\"set_priority\":{\"priority\":100}}},\"version\":1,\"modified_date_in_millis\":1578521007076}" + ) .build(); - IndexMetadata meta = mkMeta() - .putCustom(ILM_CUSTOM_METADATA_KEY, exState.asMap()) - .build(); + IndexMetadata meta = mkMeta().putCustom(ILM_CUSTOM_METADATA_KEY, exState.asMap()).build(); assertTrue(eligibleToCheckForRefresh(meta)); @@ -461,9 +489,7 @@ public void testUpdateIndicesForPolicy() { assertTrue(isIndexPhaseDefinitionUpdatable(REGISTRY, client, meta, newPolicy, null)); ClusterState existingState = ClusterState.builder(ClusterState.EMPTY_STATE) - .metadata(Metadata.builder(Metadata.EMPTY_METADATA) - .put(meta, false) - .build()) + .metadata(Metadata.builder(Metadata.EMPTY_METADATA).put(meta, false).build()) .build(); logger.info("--> update for unchanged policy"); @@ -487,18 +513,18 @@ public void testUpdateIndicesForPolicy() { assertThat(updatedState, equalTo(existingState)); meta = IndexMetadata.builder(index) - .settings(Settings.builder() - .put(LifecycleSettings.LIFECYCLE_NAME, "my-policy") - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 10)) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, randomIntBetween(0, 5)) - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(IndexMetadata.SETTING_INDEX_UUID, randomAlphaOfLength(5))) + .settings( + Settings.builder() + .put(LifecycleSettings.LIFECYCLE_NAME, "my-policy") + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 10)) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, randomIntBetween(0, 5)) + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetadata.SETTING_INDEX_UUID, randomAlphaOfLength(5)) + ) .putCustom(ILM_CUSTOM_METADATA_KEY, exState.asMap()) .build(); existingState = ClusterState.builder(ClusterState.EMPTY_STATE) - .metadata(Metadata.builder(Metadata.EMPTY_METADATA) - .put(meta, false) - .build()) + .metadata(Metadata.builder(Metadata.EMPTY_METADATA).put(meta, false).build()) .build(); logger.info("--> update with changed policy and this index has the policy"); @@ -514,9 +540,13 @@ public void testUpdateIndicesForPolicy() { assertThat(beforeState, equalTo(afterState)); // Check that the phase definition has been refreshed - assertThat(afterExState.getPhaseDefinition(), - equalTo("{\"policy\":\"my-policy\",\"phase_definition\":{\"min_age\":\"0ms\",\"actions\":{\"rollover\":{\"max_docs\":2}," + - "\"set_priority\":{\"priority\":150}}},\"version\":2,\"modified_date_in_millis\":2}")); + assertThat( + afterExState.getPhaseDefinition(), + equalTo( + "{\"policy\":\"my-policy\",\"phase_definition\":{\"min_age\":\"0ms\",\"actions\":{\"rollover\":{\"max_docs\":2}," + + "\"set_priority\":{\"priority\":150}}},\"version\":2,\"modified_date_in_millis\":2}" + ) + ); } private IndexMetadata buildIndexMetadata(String policy, LifecycleExecutionState.Builder lifecycleState) { @@ -530,11 +560,13 @@ private IndexMetadata buildIndexMetadata(String policy, LifecycleExecutionState. private static IndexMetadata.Builder mkMeta() { return IndexMetadata.builder(index) - .settings(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 10)) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, randomIntBetween(0, 5)) - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(IndexMetadata.SETTING_INDEX_UUID, randomAlphaOfLength(5))); + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 10)) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, randomIntBetween(0, 5)) + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetadata.SETTING_INDEX_UUID, randomAlphaOfLength(5)) + ); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/PhaseExecutionInfoTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/PhaseExecutionInfoTests.java index 73d3d4a45b530..259513af15afc 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/PhaseExecutionInfoTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/PhaseExecutionInfoTests.java @@ -8,13 +8,13 @@ package org.elasticsearch.xpack.core.ilm; import org.elasticsearch.cluster.ClusterModule; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.Writeable.Reader; import org.elasticsearch.common.util.CollectionUtils; +import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractSerializingTestCase; import org.junit.Before; import java.io.IOException; @@ -23,8 +23,12 @@ public class PhaseExecutionInfoTests extends AbstractSerializingTestCase { static PhaseExecutionInfo randomPhaseExecutionInfo(String phaseName) { - return new PhaseExecutionInfo(randomAlphaOfLength(5), PhaseTests.randomTestPhase(phaseName), - randomNonNegativeLong(), randomNonNegativeLong()); + return new PhaseExecutionInfo( + randomAlphaOfLength(5), + PhaseTests.randomTestPhase(phaseName), + randomNonNegativeLong(), + randomNonNegativeLong() + ); } String phaseName; @@ -75,13 +79,18 @@ protected PhaseExecutionInfo mutateInstance(PhaseExecutionInfo instance) throws } protected NamedWriteableRegistry getNamedWriteableRegistry() { - return new NamedWriteableRegistry(Arrays - .asList(new NamedWriteableRegistry.Entry(LifecycleAction.class, MockAction.NAME, MockAction::new))); + return new NamedWriteableRegistry( + Arrays.asList(new NamedWriteableRegistry.Entry(LifecycleAction.class, MockAction.NAME, MockAction::new)) + ); } @Override protected NamedXContentRegistry xContentRegistry() { - return new NamedXContentRegistry(CollectionUtils.appendToCopy(ClusterModule.getNamedXWriteables(), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(MockAction.NAME), MockAction::parse))); + return new NamedXContentRegistry( + CollectionUtils.appendToCopy( + ClusterModule.getNamedXWriteables(), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(MockAction.NAME), MockAction::parse) + ) + ); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/PhaseStatsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/PhaseStatsTests.java index 0ba422dd883f0..cfc7b1ae64313 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/PhaseStatsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/PhaseStatsTests.java @@ -33,15 +33,17 @@ protected PhaseStats mutateInstance(PhaseStats instance) throws IOException { TimeValue after = instance.getAfter(); String[] actionNames = instance.getActionNames(); switch (between(0, 1)) { - case 0: - after = randomValueOtherThan(after, () -> TimeValue.parseTimeValue(randomPositiveTimeValue(), "rollover_action_test")); - break; - case 1: - actionNames = randomValueOtherThanMany(a -> Arrays.equals(a, instance.getActionNames()), - () -> randomArray(0, 20, size -> new String[size], () -> randomAlphaOfLengthBetween(1, 20))); - break; - default: - throw new AssertionError("Illegal randomisation branch"); + case 0: + after = randomValueOtherThan(after, () -> TimeValue.parseTimeValue(randomPositiveTimeValue(), "rollover_action_test")); + break; + case 1: + actionNames = randomValueOtherThanMany( + a -> Arrays.equals(a, instance.getActionNames()), + () -> randomArray(0, 20, size -> new String[size], () -> randomAlphaOfLengthBetween(1, 20)) + ); + break; + default: + throw new AssertionError("Illegal randomisation branch"); } return new PhaseStats(after, actionNames, instance.getConfigurations()); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/PhaseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/PhaseTests.java index a3f6374903eea..a6bb513022f78 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/PhaseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/PhaseTests.java @@ -7,14 +7,14 @@ package org.elasticsearch.xpack.core.ilm; import org.elasticsearch.cluster.ClusterModule; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.Writeable.Reader; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.CollectionUtils; +import org.elasticsearch.core.TimeValue; +import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractSerializingTestCase; import org.junit.Before; import java.io.IOException; @@ -59,14 +59,19 @@ protected Reader instanceReader() { } protected NamedWriteableRegistry getNamedWriteableRegistry() { - return new NamedWriteableRegistry(Arrays - .asList(new NamedWriteableRegistry.Entry(LifecycleAction.class, MockAction.NAME, MockAction::new))); + return new NamedWriteableRegistry( + Arrays.asList(new NamedWriteableRegistry.Entry(LifecycleAction.class, MockAction.NAME, MockAction::new)) + ); } @Override protected NamedXContentRegistry xContentRegistry() { - return new NamedXContentRegistry(CollectionUtils.appendToCopy(ClusterModule.getNamedXWriteables(), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(MockAction.NAME), MockAction::parse))); + return new NamedXContentRegistry( + CollectionUtils.appendToCopy( + ClusterModule.getNamedXWriteables(), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(MockAction.NAME), MockAction::parse) + ) + ); } @Override @@ -75,18 +80,18 @@ protected Phase mutateInstance(Phase instance) throws IOException { TimeValue after = instance.getMinimumAge(); Map actions = instance.getActions(); switch (between(0, 2)) { - case 0: - name = name + randomAlphaOfLengthBetween(1, 5); - break; - case 1: - after = TimeValue.timeValueSeconds(after.getSeconds() + randomIntBetween(1, 1000)); - break; - case 2: - actions = new HashMap<>(actions); - actions.put(MockAction.NAME + "another", new MockAction(Collections.emptyList())); - break; - default: - throw new AssertionError("Illegal randomisation branch"); + case 0: + name = name + randomAlphaOfLengthBetween(1, 5); + break; + case 1: + after = TimeValue.timeValueSeconds(after.getSeconds() + randomIntBetween(1, 1000)); + break; + case 2: + actions = new HashMap<>(actions); + actions.put(MockAction.NAME + "another", new MockAction(Collections.emptyList())); + break; + default: + throw new AssertionError("Illegal randomisation branch"); } return new Phase(name, after, actions); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/PolicyStatsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/PolicyStatsTests.java index 2fd262844ab36..a59eb4d2cf210 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/PolicyStatsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/PolicyStatsTests.java @@ -37,15 +37,15 @@ protected PolicyStats mutateInstance(PolicyStats instance) throws IOException { Map phaseStats = instance.getPhaseStats(); int indicesManaged = instance.getIndicesManaged(); switch (between(0, 1)) { - case 0: - phaseStats = new HashMap<>(instance.getPhaseStats()); - phaseStats.put(randomAlphaOfLengthBetween(21, 25), PhaseStatsTests.createRandomInstance()); - break; - case 1: - indicesManaged += randomIntBetween(1, 10); - break; - default: - throw new AssertionError("Illegal randomisation branch"); + case 0: + phaseStats = new HashMap<>(instance.getPhaseStats()); + phaseStats.put(randomAlphaOfLengthBetween(21, 25), PhaseStatsTests.createRandomInstance()); + break; + case 1: + indicesManaged += randomIntBetween(1, 10); + break; + default: + throw new AssertionError("Illegal randomisation branch"); } return new PolicyStats(phaseStats, indicesManaged); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ReadOnlyActionTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ReadOnlyActionTests.java index 0986389072ec0..b2180773e9d69 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ReadOnlyActionTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ReadOnlyActionTests.java @@ -34,8 +34,11 @@ protected Reader instanceReader() { public void testToSteps() { ReadOnlyAction action = createTestInstance(); String phase = randomAlphaOfLengthBetween(1, 10); - StepKey nextStepKey = new StepKey(randomAlphaOfLengthBetween(1, 10), randomAlphaOfLengthBetween(1, 10), - randomAlphaOfLengthBetween(1, 10)); + StepKey nextStepKey = new StepKey( + randomAlphaOfLengthBetween(1, 10), + randomAlphaOfLengthBetween(1, 10), + randomAlphaOfLengthBetween(1, 10) + ); List steps = action.toSteps(null, phase, nextStepKey); assertNotNull(steps); assertEquals(2, steps.size()); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ReplaceDataStreamBackingIndexStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ReplaceDataStreamBackingIndexStepTests.java index 2afaa68413158..75991641b2ab1 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ReplaceDataStreamBackingIndexStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ReplaceDataStreamBackingIndexStepTests.java @@ -59,35 +59,39 @@ protected ReplaceDataStreamBackingIndexStep copyInstance(ReplaceDataStreamBackin public void testPerformActionThrowsExceptionIfIndexIsNotPartOfDataStream() { String indexName = randomAlphaOfLength(10); String policyName = "test-ilm-policy"; - IndexMetadata.Builder sourceIndexMetadataBuilder = - IndexMetadata.builder(indexName).settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)); + IndexMetadata.Builder sourceIndexMetadataBuilder = IndexMetadata.builder(indexName) + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)); final IndexMetadata sourceIndexMetadata = sourceIndexMetadataBuilder.build(); - ClusterState clusterState = ClusterState.builder(emptyClusterState()).metadata( - Metadata.builder().put(sourceIndexMetadata, false).build() - ).build(); + ClusterState clusterState = ClusterState.builder(emptyClusterState()) + .metadata(Metadata.builder().put(sourceIndexMetadata, false).build()) + .build(); - expectThrows(IllegalStateException.class, - () -> createRandomInstance().performAction(sourceIndexMetadata.getIndex(), clusterState)); + expectThrows(IllegalStateException.class, () -> createRandomInstance().performAction(sourceIndexMetadata.getIndex(), clusterState)); } public void testPerformActionThrowsExceptionIfIndexIsTheDataStreamWriteIndex() { String dataStreamName = randomAlphaOfLength(10); String indexName = DataStream.getDefaultBackingIndexName(dataStreamName, 1); String policyName = "test-ilm-policy"; - IndexMetadata sourceIndexMetadata = - IndexMetadata.builder(indexName).settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); - - ClusterState clusterState = ClusterState.builder(emptyClusterState()).metadata( - Metadata.builder().put(sourceIndexMetadata, true) - .put(new DataStream(dataStreamName, createTimestampField("@timestamp"), - List.of(sourceIndexMetadata.getIndex()))).build() - ).build(); - - expectThrows(IllegalStateException.class, - () -> createRandomInstance().performAction(sourceIndexMetadata.getIndex(), clusterState)); + IndexMetadata sourceIndexMetadata = IndexMetadata.builder(indexName) + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); + + ClusterState clusterState = ClusterState.builder(emptyClusterState()) + .metadata( + Metadata.builder() + .put(sourceIndexMetadata, true) + .put(new DataStream(dataStreamName, createTimestampField("@timestamp"), List.of(sourceIndexMetadata.getIndex()))) + .build() + ) + .build(); + + expectThrows(IllegalStateException.class, () -> createRandomInstance().performAction(sourceIndexMetadata.getIndex(), clusterState)); } public void testPerformActionThrowsExceptionIfTargetIndexIsMissing() { @@ -96,26 +100,29 @@ public void testPerformActionThrowsExceptionIfTargetIndexIsMissing() { String policyName = "test-ilm-policy"; IndexMetadata sourceIndexMetadata = IndexMetadata.builder(indexName) .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) .build(); String writeIndexName = DataStream.getDefaultBackingIndexName(dataStreamName, 2); IndexMetadata writeIndexMetadata = IndexMetadata.builder(writeIndexName) .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) .build(); List backingIndices = List.of(sourceIndexMetadata.getIndex(), writeIndexMetadata.getIndex()); - ClusterState clusterState = ClusterState.builder(emptyClusterState()).metadata( - Metadata.builder() - .put(sourceIndexMetadata, true) - .put(writeIndexMetadata, true) - .put(new DataStream(dataStreamName, createTimestampField("@timestamp"), backingIndices)) - .build() - ).build(); - - expectThrows(IllegalStateException.class, - () -> createRandomInstance().performAction(sourceIndexMetadata.getIndex(), clusterState)); + ClusterState clusterState = ClusterState.builder(emptyClusterState()) + .metadata( + Metadata.builder() + .put(sourceIndexMetadata, true) + .put(writeIndexMetadata, true) + .put(new DataStream(dataStreamName, createTimestampField("@timestamp"), backingIndices)) + .build() + ) + .build(); + + expectThrows(IllegalStateException.class, () -> createRandomInstance().performAction(sourceIndexMetadata.getIndex(), clusterState)); } public void testPerformActionIsNoOpIfIndexIsMissing() { @@ -132,33 +139,43 @@ public void testPerformAction() { String policyName = "test-ilm-policy"; IndexMetadata sourceIndexMetadata = IndexMetadata.builder(indexName) .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) .build(); String writeIndexName = DataStream.getDefaultBackingIndexName(dataStreamName, 2); IndexMetadata writeIndexMetadata = IndexMetadata.builder(writeIndexName) .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) .build(); String indexPrefix = "test-prefix-"; String targetIndex = indexPrefix + indexName; - IndexMetadata targetIndexMetadata = IndexMetadata.builder(targetIndex).settings(settings(Version.CURRENT)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + IndexMetadata targetIndexMetadata = IndexMetadata.builder(targetIndex) + .settings(settings(Version.CURRENT)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); List backingIndices = List.of(sourceIndexMetadata.getIndex(), writeIndexMetadata.getIndex()); - ClusterState clusterState = ClusterState.builder(emptyClusterState()).metadata( - Metadata.builder() - .put(sourceIndexMetadata, true) - .put(writeIndexMetadata, true) - .put(new DataStream(dataStreamName, createTimestampField("@timestamp"), backingIndices)) - .put(targetIndexMetadata, true) - .build() - ).build(); - - ReplaceDataStreamBackingIndexStep replaceSourceIndexStep = - new ReplaceDataStreamBackingIndexStep(randomStepKey(), randomStepKey(), (index, state) -> indexPrefix + index); + ClusterState clusterState = ClusterState.builder(emptyClusterState()) + .metadata( + Metadata.builder() + .put(sourceIndexMetadata, true) + .put(writeIndexMetadata, true) + .put(new DataStream(dataStreamName, createTimestampField("@timestamp"), backingIndices)) + .put(targetIndexMetadata, true) + .build() + ) + .build(); + + ReplaceDataStreamBackingIndexStep replaceSourceIndexStep = new ReplaceDataStreamBackingIndexStep( + randomStepKey(), + randomStepKey(), + (index, state) -> indexPrefix + index + ); ClusterState newState = replaceSourceIndexStep.performAction(sourceIndexMetadata.getIndex(), clusterState); DataStream updatedDataStream = newState.metadata().dataStreams().get(dataStreamName); assertThat(updatedDataStream.getIndices().size(), is(2)); @@ -175,32 +192,42 @@ public void testPerformActionSameOriginalTargetError() { String policyName = "test-ilm-policy"; IndexMetadata sourceIndexMetadata = IndexMetadata.builder(indexName) .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) .build(); IndexMetadata writeIndexMetadata = IndexMetadata.builder(writeIndexName) .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) .build(); String indexPrefix = "test-prefix-"; String targetIndex = indexPrefix + indexName; - IndexMetadata targetIndexMetadata = IndexMetadata.builder(targetIndex).settings(settings(Version.CURRENT)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + IndexMetadata targetIndexMetadata = IndexMetadata.builder(targetIndex) + .settings(settings(Version.CURRENT)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); List backingIndices = List.of(sourceIndexMetadata.getIndex(), writeIndexMetadata.getIndex()); - ClusterState clusterState = ClusterState.builder(emptyClusterState()).metadata( - Metadata.builder() - .put(sourceIndexMetadata, true) - .put(writeIndexMetadata, true) - .put(new DataStream(dataStreamName, createTimestampField("@timestamp"), backingIndices)) - .put(targetIndexMetadata, true) - .build() - ).build(); - - ReplaceDataStreamBackingIndexStep replaceSourceIndexStep = - new ReplaceDataStreamBackingIndexStep(randomStepKey(), randomStepKey(), (index, state) -> indexPrefix + index); + ClusterState clusterState = ClusterState.builder(emptyClusterState()) + .metadata( + Metadata.builder() + .put(sourceIndexMetadata, true) + .put(writeIndexMetadata, true) + .put(new DataStream(dataStreamName, createTimestampField("@timestamp"), backingIndices)) + .put(targetIndexMetadata, true) + .build() + ) + .build(); + + ReplaceDataStreamBackingIndexStep replaceSourceIndexStep = new ReplaceDataStreamBackingIndexStep( + randomStepKey(), + randomStepKey(), + (index, state) -> indexPrefix + index + ); IllegalStateException ex = expectThrows( IllegalStateException.class, () -> replaceSourceIndexStep.performAction(sourceIndexMetadata.getIndex(), clusterState) diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/RolloverActionTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/RolloverActionTests.java index 0f0f8c5176d27..e6d291dba0c9e 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/RolloverActionTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/RolloverActionTests.java @@ -30,11 +30,11 @@ protected RolloverAction createTestInstance() { public static RolloverAction randomInstance() { ByteSizeUnit maxSizeUnit = randomFrom(ByteSizeUnit.values()); - ByteSizeValue maxSize = randomBoolean() ? null : - new ByteSizeValue(randomNonNegativeLong() / maxSizeUnit.toBytes(1), maxSizeUnit); + ByteSizeValue maxSize = randomBoolean() ? null : new ByteSizeValue(randomNonNegativeLong() / maxSizeUnit.toBytes(1), maxSizeUnit); ByteSizeUnit maxPrimaryShardSizeUnit = randomFrom(ByteSizeUnit.values()); - ByteSizeValue maxPrimaryShardSize = randomBoolean() ? null : - new ByteSizeValue(randomNonNegativeLong() / maxPrimaryShardSizeUnit.toBytes(1), maxPrimaryShardSizeUnit); + ByteSizeValue maxPrimaryShardSize = randomBoolean() + ? null + : new ByteSizeValue(randomNonNegativeLong() / maxPrimaryShardSizeUnit.toBytes(1), maxPrimaryShardSizeUnit); Long maxDocs = randomBoolean() ? null : randomNonNegativeLong(); TimeValue maxAge = (maxDocs == null && maxSize == null || randomBoolean()) ? TimeValue.parseTimeValue(randomPositiveTimeValue(), "rollover_action_test") @@ -67,8 +67,7 @@ protected RolloverAction mutateInstance(RolloverAction instance) throws IOExcept }); break; case 2: - maxAge = randomValueOtherThan(maxAge, - () -> TimeValue.parseTimeValue(randomPositiveTimeValue(), "rollover_action_test")); + maxAge = randomValueOtherThan(maxAge, () -> TimeValue.parseTimeValue(randomPositiveTimeValue(), "rollover_action_test")); break; case 3: maxDocs = maxDocs == null ? randomNonNegativeLong() : maxDocs + 1; @@ -80,16 +79,18 @@ protected RolloverAction mutateInstance(RolloverAction instance) throws IOExcept } public void testNoConditions() { - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, - () -> new RolloverAction(null, null, null, null)); + IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> new RolloverAction(null, null, null, null)); assertEquals("At least one rollover condition must be set.", exception.getMessage()); } public void testToSteps() { RolloverAction action = createTestInstance(); String phase = randomAlphaOfLengthBetween(1, 10); - StepKey nextStepKey = new StepKey(randomAlphaOfLengthBetween(1, 10), randomAlphaOfLengthBetween(1, 10), - randomAlphaOfLengthBetween(1, 10)); + StepKey nextStepKey = new StepKey( + randomAlphaOfLengthBetween(1, 10), + randomAlphaOfLengthBetween(1, 10), + randomAlphaOfLengthBetween(1, 10) + ); List steps = action.toSteps(null, phase, nextStepKey); assertNotNull(steps); assertEquals(5, steps.size()); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/RolloverStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/RolloverStepTests.java index 2d642a0fe16c1..c56aeb1ea5069 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/RolloverStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/RolloverStepTests.java @@ -46,16 +46,15 @@ public RolloverStep mutateInstance(RolloverStep instance) { StepKey key = instance.getKey(); StepKey nextKey = instance.getNextStepKey(); - switch (between(0, 1)) { - case 0: - key = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); - break; - case 1: - nextKey = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); - break; - default: - throw new AssertionError("Illegal randomisation branch"); + case 0: + key = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); + break; + case 1: + nextKey = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); + break; + default: + throw new AssertionError("Illegal randomisation branch"); } return new RolloverStep(key, nextKey, instance.getClient()); @@ -70,7 +69,9 @@ private IndexMetadata getIndexMetadata(String alias) { return IndexMetadata.builder(randomAlphaOfLength(10)) .putAlias(AliasMetadata.builder(alias)) .settings(settings(Version.CURRENT).put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); } private static void assertRolloverIndexRequest(RolloverRequest request, String rolloverTarget) { @@ -90,12 +91,7 @@ public void testPerformAction() throws Exception { mockClientRolloverCall(alias); - ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) - .metadata( - Metadata.builder() - .put(indexMetadata, true) - ) - .build(); + ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder().put(indexMetadata, true)).build(); PlainActionFuture.get(f -> step.performAction(indexMetadata, clusterState, null, f)); Mockito.verify(client, Mockito.only()).admin(); @@ -107,7 +103,9 @@ public void testPerformActionOnDataStream() throws Exception { String dataStreamName = "test-datastream"; IndexMetadata indexMetadata = IndexMetadata.builder(DataStream.getDefaultBackingIndexName(dataStreamName, 1)) .settings(settings(Version.CURRENT)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); RolloverStep step = createRandomInstance(); @@ -116,8 +114,7 @@ public void testPerformActionOnDataStream() throws Exception { ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) .metadata( Metadata.builder() - .put(new DataStream(dataStreamName, createTimestampField("@timestamp"), - List.of(indexMetadata.getIndex()))) + .put(new DataStream(dataStreamName, createTimestampField("@timestamp"), List.of(indexMetadata.getIndex()))) .put(indexMetadata, true) ) .build(); @@ -132,19 +129,29 @@ public void testSkipRolloverIfDataStreamIsAlreadyRolledOver() throws Exception { String dataStreamName = "test-datastream"; IndexMetadata firstGenerationIndex = IndexMetadata.builder(DataStream.getDefaultBackingIndexName(dataStreamName, 1)) .settings(settings(Version.CURRENT)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); IndexMetadata writeIndex = IndexMetadata.builder(DataStream.getDefaultBackingIndexName(dataStreamName, 2)) .settings(settings(Version.CURRENT)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); RolloverStep step = createRandomInstance(); ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) .metadata( - Metadata.builder().put(firstGenerationIndex, true) + Metadata.builder() + .put(firstGenerationIndex, true) .put(writeIndex, true) - .put(new DataStream(dataStreamName, createTimestampField("@timestamp"), - List.of(firstGenerationIndex.getIndex(), writeIndex.getIndex()))) + .put( + new DataStream( + dataStreamName, + createTimestampField("@timestamp"), + List.of(firstGenerationIndex.getIndex(), writeIndex.getIndex()) + ) + ) ) .build(); PlainActionFuture.get(f -> step.performAction(firstGenerationIndex, clusterState, null, f)); @@ -169,19 +176,17 @@ public void testPerformActionWithIndexingComplete() throws Exception { String alias = randomAlphaOfLength(5); IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) .putAlias(AliasMetadata.builder(alias)) - .settings(settings(Version.CURRENT) - .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias) - .put(LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE, true)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + .settings( + settings(Version.CURRENT).put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias) + .put(LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE, true) + ) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); RolloverStep step = createRandomInstance(); - ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) - .metadata( - Metadata.builder() - .put(indexMetadata, true) - ) - .build(); + ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder().put(indexMetadata, true)).build(); PlainActionFuture.get(f -> step.performAction(indexMetadata, clusterState, null, f)); } @@ -190,19 +195,19 @@ public void testPerformActionSkipsRolloverForAlreadyRolledIndex() throws Excepti IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) .putAlias(AliasMetadata.builder(rolloverAlias)) .settings(settings(Version.CURRENT).put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, rolloverAlias)) - .putRolloverInfo(new RolloverInfo(rolloverAlias, - Collections.singletonList(new MaxSizeCondition(new ByteSizeValue(2L))), - System.currentTimeMillis()) + .putRolloverInfo( + new RolloverInfo( + rolloverAlias, + Collections.singletonList(new MaxSizeCondition(new ByteSizeValue(2L))), + System.currentTimeMillis() + ) ) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); RolloverStep step = createRandomInstance(); - ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) - .metadata( - Metadata.builder() - .put(indexMetadata, true) - ) - .build(); + ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder().put(indexMetadata, true)).build(); PlainActionFuture.get(f -> step.performAction(indexMetadata, clusterState, null, f)); Mockito.verify(indicesClient, Mockito.never()).rolloverIndex(Mockito.any(), Mockito.any()); @@ -223,14 +228,14 @@ public void testPerformActionFailure() { return null; }).when(indicesClient).rolloverIndex(Mockito.any(), Mockito.any()); - ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) - .metadata( - Metadata.builder() - .put(indexMetadata, true) + ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder().put(indexMetadata, true)).build(); + assertSame( + exception, + expectThrows( + Exception.class, + () -> PlainActionFuture.get(f -> step.performAction(indexMetadata, clusterState, null, f)) ) - .build(); - assertSame(exception, expectThrows(Exception.class, () -> PlainActionFuture.get( - f -> step.performAction(indexMetadata, clusterState, null, f)))); + ); Mockito.verify(client, Mockito.only()).admin(); Mockito.verify(adminClient, Mockito.only()).indices(); @@ -241,39 +246,55 @@ public void testPerformActionInvalidNullOrEmptyAlias() { String alias = randomBoolean() ? "" : null; IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) .settings(settings(Version.CURRENT).put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); RolloverStep step = createRandomInstance(); - ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) - .metadata( - Metadata.builder() - .put(indexMetadata, true) + ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder().put(indexMetadata, true)).build(); + Exception e = expectThrows( + IllegalArgumentException.class, + () -> PlainActionFuture.get(f -> step.performAction(indexMetadata, clusterState, null, f)) + ); + assertThat( + e.getMessage(), + Matchers.is( + String.format( + Locale.ROOT, + "setting [%s] for index [%s] is empty or not defined, it must be set to the name of the alias pointing to the group of " + + "indices being rolled over", + RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, + indexMetadata.getIndex().getName() + ) ) - .build(); - Exception e = expectThrows(IllegalArgumentException.class, - () -> PlainActionFuture.get(f -> step.performAction(indexMetadata, clusterState, null, f))); - assertThat(e.getMessage(), Matchers.is(String.format(Locale.ROOT, - "setting [%s] for index [%s] is empty or not defined, it must be set to the name of the alias pointing to the group of " + - "indices being rolled over", RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, indexMetadata.getIndex().getName()))); + ); } public void testPerformActionAliasDoesNotPointToIndex() { String alias = randomAlphaOfLength(5); IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) .settings(settings(Version.CURRENT).put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); RolloverStep step = createRandomInstance(); - ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) - .metadata( - Metadata.builder() - .put(indexMetadata, true) + ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder().put(indexMetadata, true)).build(); + Exception e = expectThrows( + IllegalArgumentException.class, + () -> PlainActionFuture.get(f -> step.performAction(indexMetadata, clusterState, null, f)) + ); + assertThat( + e.getMessage(), + Matchers.is( + String.format( + Locale.ROOT, + "%s [%s] does not point to index [%s]", + RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, + alias, + indexMetadata.getIndex().getName() + ) ) - .build(); - Exception e = expectThrows(IllegalArgumentException.class, - () -> PlainActionFuture.get(f -> step.performAction(indexMetadata, clusterState, null, f))); - assertThat(e.getMessage(), Matchers.is(String.format(Locale.ROOT, - "%s [%s] does not point to index [%s]", RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias, - indexMetadata.getIndex().getName()))); + ); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/RollupILMActionTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/RollupILMActionTests.java index 0570fb108cfe2..a84bbb34b1782 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/RollupILMActionTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/RollupILMActionTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ilm; import org.elasticsearch.common.io.stream.Writeable.Reader; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.EqualsHashCodeTestUtils; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ilm.Step.StepKey; import org.elasticsearch.xpack.core.rollup.RollupActionConfig; import org.elasticsearch.xpack.core.rollup.RollupActionConfigTests; @@ -24,8 +24,7 @@ public class RollupILMActionTests extends AbstractActionTestCase { static RollupILMAction randomInstance() { - return new RollupILMAction(RollupActionConfigTests.randomConfig(random()), - randomBoolean() ? randomAlphaOfLength(5) : null); + return new RollupILMAction(RollupActionConfigTests.randomConfig(random()), randomBoolean() ? randomAlphaOfLength(5) : null); } @Override @@ -52,8 +51,11 @@ public boolean isSafeAction() { public void testToSteps() { RollupILMAction action = new RollupILMAction(RollupActionConfigTests.randomConfig(random()), null); String phase = randomAlphaOfLengthBetween(1, 10); - StepKey nextStepKey = new StepKey(randomAlphaOfLengthBetween(1, 10), randomAlphaOfLengthBetween(1, 10), - randomAlphaOfLengthBetween(1, 10)); + StepKey nextStepKey = new StepKey( + randomAlphaOfLengthBetween(1, 10), + randomAlphaOfLengthBetween(1, 10), + randomAlphaOfLengthBetween(1, 10) + ); List steps = action.toSteps(null, phase, nextStepKey); assertNotNull(steps); assertEquals(4, steps.size()); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/RollupStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/RollupStepTests.java index 7a9b2242e8d90..da58cc031e976 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/RollupStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/RollupStepTests.java @@ -66,9 +66,10 @@ public RollupStep copyInstance(RollupStep instance) { private IndexMetadata getIndexMetadata(String index) { Map ilmCustom = Collections.singletonMap("rollup_index_name", "rollup-index"); - return IndexMetadata.builder(index).settings( - settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, "test-ilm-policy")) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)) + return IndexMetadata.builder(index) + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, "test-ilm-policy")) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) .putCustom(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY, ilmCustom) .build(); } @@ -87,19 +88,15 @@ public void testPerformAction() throws Exception { mockClientRollupCall(index); - ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) - .metadata( - Metadata.builder() - .put(indexMetadata, true) - ) - .build(); + ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder().put(indexMetadata, true)).build(); PlainActionFuture.get(f -> step.performAction(indexMetadata, clusterState, null, f)); } public void testPerformActionFailureInvalidExecutionState() { - IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)).settings( - settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, "test-ilm-policy")) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)) + IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, "test-ilm-policy")) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) .build(); String policyName = indexMetadata.getSettings().get(LifecycleSettings.LIFECYCLE_NAME); String indexName = indexMetadata.getIndex().getName(); @@ -113,8 +110,10 @@ public void onResponse(Void unused) { @Override public void onFailure(Exception e) { assertThat(e, instanceOf(IllegalStateException.class)); - assertThat(e.getMessage(), - is("rollup index name was not generated for policy [" + policyName + "] and index [" + indexName + "]")); + assertThat( + e.getMessage(), + is("rollup index name was not generated for policy [" + policyName + "] and index [" + indexName + "]") + ); } }); } @@ -131,8 +130,7 @@ public void testPerformActionOnDataStream() throws Exception { ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) .metadata( Metadata.builder() - .put(new DataStream(dataStreamName, createTimestampField("@timestamp"), - List.of(indexMetadata.getIndex()))) + .put(new DataStream(dataStreamName, createTimestampField("@timestamp"), List.of(indexMetadata.getIndex()))) .put(indexMetadata, true) ) .build(); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SearchableSnapshotActionTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SearchableSnapshotActionTests.java index 2b39fe6f18ecd..31646ffc929bd 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SearchableSnapshotActionTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SearchableSnapshotActionTests.java @@ -6,8 +6,8 @@ */ package org.elasticsearch.xpack.core.ilm; -import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.cluster.routing.allocation.DataTier; +import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ilm.Step.StepKey; import org.elasticsearch.xpack.core.searchablesnapshots.MountSearchableSnapshotRequest; @@ -30,8 +30,9 @@ public void testToSteps() { List steps = action.toSteps(null, phase, nextStepKey, null); assertThat(steps.size(), is(action.isForceMergeIndex() ? 18 : 16)); - List expectedSteps = action.isForceMergeIndex() ? expectedStepKeysWithForceMerge(phase) : - expectedStepKeysNoForceMerge(phase); + List expectedSteps = action.isForceMergeIndex() + ? expectedStepKeysWithForceMerge(phase) + : expectedStepKeysNoForceMerge(phase); assertThat(steps.get(0).getKey(), is(expectedSteps.get(0))); assertThat(steps.get(1).getKey(), is(expectedSteps.get(1))); @@ -104,7 +105,8 @@ private List expectedStepKeysWithForceMerge(String phase) { new StepKey(phase, NAME, SearchableSnapshotAction.CONDITIONAL_DATASTREAM_CHECK_KEY), new StepKey(phase, NAME, ReplaceDataStreamBackingIndexStep.NAME), new StepKey(phase, NAME, DeleteStep.NAME), - new StepKey(phase, NAME, SwapAliasesAndDeleteSourceIndexStep.NAME)); + new StepKey(phase, NAME, SwapAliasesAndDeleteSourceIndexStep.NAME) + ); } private List expectedStepKeysNoForceMerge(String phase) { @@ -124,7 +126,8 @@ private List expectedStepKeysNoForceMerge(String phase) { new StepKey(phase, NAME, SearchableSnapshotAction.CONDITIONAL_DATASTREAM_CHECK_KEY), new StepKey(phase, NAME, ReplaceDataStreamBackingIndexStep.NAME), new StepKey(phase, NAME, DeleteStep.NAME), - new StepKey(phase, NAME, SwapAliasesAndDeleteSourceIndexStep.NAME)); + new StepKey(phase, NAME, SwapAliasesAndDeleteSourceIndexStep.NAME) + ); } @Override diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SegmentCountStepInfoTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SegmentCountStepInfoTests.java index 037f2d32b875e..7aeeba557ee54 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SegmentCountStepInfoTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SegmentCountStepInfoTests.java @@ -7,9 +7,9 @@ package org.elasticsearch.xpack.core.ilm; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.test.EqualsHashCodeTestUtils; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ilm.SegmentCountStep.Info; import java.io.IOException; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SegmentCountStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SegmentCountStepTests.java index d1edfe873e8d9..d947f05bdd42c 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SegmentCountStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SegmentCountStepTests.java @@ -17,10 +17,10 @@ import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.index.Index; import org.elasticsearch.index.engine.Segment; import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xpack.core.ilm.Step.StepKey; import org.mockito.Mockito; @@ -46,10 +46,12 @@ public SegmentCountStep createRandomInstance() { private IndexMetadata makeMeta(Index index) { return IndexMetadata.builder(index.getName()) - .settings(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT)) + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + ) .build(); } @@ -204,8 +206,10 @@ public void testFailedToRetrieveSomeSegments() { Mockito.when(indicesSegmentResponse.getStatus()).thenReturn(RestStatus.OK); Mockito.when(indicesSegmentResponse.getIndices()).thenReturn(Collections.singletonMap(index.getName(), null)); Mockito.when(indicesSegmentResponse.getShardFailures()) - .thenReturn(new DefaultShardOperationFailedException[]{new DefaultShardOperationFailedException(index.getName(), - 0, new IllegalArgumentException("fake"))}); + .thenReturn( + new DefaultShardOperationFailedException[] { + new DefaultShardOperationFailedException(index.getName(), 0, new IllegalArgumentException("fake")) } + ); Mockito.when(indexSegments.spliterator()).thenReturn(iss); Mockito.when(indexShardSegments.getShards()).thenReturn(shardSegmentsArray); Mockito.when(shardSegmentsOne.getSegments()).thenReturn(segments); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SetPriorityActionTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SetPriorityActionTests.java index 52f2c781323c9..adde6a736cd7f 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SetPriorityActionTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SetPriorityActionTests.java @@ -8,8 +8,8 @@ import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.common.io.stream.Writeable.Reader; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.EqualsHashCodeTestUtils; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ilm.Step.StepKey; import java.util.List; @@ -44,7 +44,7 @@ public void testNonPositivePriority() { assertThat(e.getMessage(), equalTo("[priority] must be 0 or greater")); } - public void testNullPriorityAllowed(){ + public void testNullPriorityAllowed() { SetPriorityAction nullPriority = new SetPriorityAction((Integer) null); assertNull(nullPriority.recoveryPriority); } @@ -52,8 +52,11 @@ public void testNullPriorityAllowed(){ public void testToSteps() { SetPriorityAction action = createTestInstance(); String phase = randomAlphaOfLengthBetween(1, 10); - StepKey nextStepKey = new StepKey(randomAlphaOfLengthBetween(1, 10), randomAlphaOfLengthBetween(1, 10), - randomAlphaOfLengthBetween(1, 10)); + StepKey nextStepKey = new StepKey( + randomAlphaOfLengthBetween(1, 10), + randomAlphaOfLengthBetween(1, 10), + randomAlphaOfLengthBetween(1, 10) + ); List steps = action.toSteps(null, phase, nextStepKey); assertNotNull(steps); assertEquals(1, steps.size()); @@ -66,10 +69,13 @@ public void testToSteps() { } public void testNullPriorityStep() { - SetPriorityAction action = new SetPriorityAction((Integer)null); + SetPriorityAction action = new SetPriorityAction((Integer) null); String phase = randomAlphaOfLengthBetween(1, 10); - StepKey nextStepKey = new StepKey(randomAlphaOfLengthBetween(1, 10), randomAlphaOfLengthBetween(1, 10), - randomAlphaOfLengthBetween(1, 10)); + StepKey nextStepKey = new StepKey( + randomAlphaOfLengthBetween(1, 10), + randomAlphaOfLengthBetween(1, 10), + randomAlphaOfLengthBetween(1, 10) + ); List steps = action.toSteps(null, phase, nextStepKey); assertNotNull(steps); assertEquals(1, steps.size()); @@ -78,8 +84,10 @@ public void testNullPriorityStep() { assertThat(firstStep.getKey(), equalTo(expectedFirstStepKey)); assertThat(firstStep.getNextStepKey(), equalTo(nextStepKey)); assertThat(firstStep.getSettings().size(), equalTo(1)); - assertThat(IndexMetadata.INDEX_PRIORITY_SETTING.get(firstStep.getSettings()), - equalTo(IndexMetadata.INDEX_PRIORITY_SETTING.getDefault(firstStep.getSettings()))); + assertThat( + IndexMetadata.INDEX_PRIORITY_SETTING.get(firstStep.getSettings()), + equalTo(IndexMetadata.INDEX_PRIORITY_SETTING.getDefault(firstStep.getSettings())) + ); } public void testEqualsAndHashCode() { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SetSingleNodeAllocateStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SetSingleNodeAllocateStepTests.java index 4927e8857413d..d930beecff5aa 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SetSingleNodeAllocateStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SetSingleNodeAllocateStepTests.java @@ -60,14 +60,14 @@ protected SetSingleNodeAllocateStep mutateInstance(SetSingleNodeAllocateStep ins StepKey nextKey = instance.getNextStepKey(); switch (between(0, 1)) { - case 0: - key = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); - break; - case 1: - nextKey = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); - break; - default: - throw new AssertionError("Illegal randomisation branch"); + case 0: + key = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); + break; + case 1: + nextKey = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); + break; + default: + throw new AssertionError("Illegal randomisation branch"); } return new SetSingleNodeAllocateStep(key, nextKey, instance.getClient()); @@ -78,9 +78,13 @@ protected SetSingleNodeAllocateStep copyInstance(SetSingleNodeAllocateStep insta return new SetSingleNodeAllocateStep(instance.getKey(), instance.getNextStepKey(), client); } - public static void assertSettingsRequestContainsValueFrom(UpdateSettingsRequest request, String settingsKey, - Set acceptableValues, boolean assertOnlyKeyInSettings, - String... expectedIndices) { + public static void assertSettingsRequestContainsValueFrom( + UpdateSettingsRequest request, + String settingsKey, + Set acceptableValues, + boolean assertOnlyKeyInSettings, + String... expectedIndices + ) { assertNotNull(request); assertArrayEquals(expectedIndices, request.indices()); assertThat(request.settings().get(settingsKey), anyOf(acceptableValues.stream().map(e -> equalTo(e)).collect(Collectors.toList()))); @@ -91,8 +95,11 @@ public static void assertSettingsRequestContainsValueFrom(UpdateSettingsRequest public void testPerformActionNoAttrs() throws Exception { final int numNodes = randomIntBetween(1, 20); - IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)).settings(settings(Version.CURRENT)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, numNodes - 1)).build(); + IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) + .settings(settings(Version.CURRENT)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, numNodes - 1)) + .build(); Index index = indexMetadata.getIndex(); Set validNodeIds = new HashSet<>(); Settings validNodeSettings = Settings.EMPTY; @@ -102,8 +109,7 @@ public void testPerformActionNoAttrs() throws Exception { String nodeName = "node_" + i; int nodePort = 9300 + i; Settings nodeSettings = Settings.builder().put(validNodeSettings).put(Node.NODE_NAME_SETTING.getKey(), nodeName).build(); - nodes.add( - DiscoveryNode.createLocal(nodeSettings, new TransportAddress(TransportAddress.META_ADDRESS, nodePort), nodeId)); + nodes.add(DiscoveryNode.createLocal(nodeSettings, new TransportAddress(TransportAddress.META_ADDRESS, nodePort), nodeId)); validNodeIds.add(nodeId); } @@ -121,8 +127,11 @@ public void testPerformActionAttrsAllNodesValid() throws Exception { for (String[] attr : validAttrs) { indexSettings.put(IndexMetadata.INDEX_ROUTING_INCLUDE_GROUP_SETTING.getKey() + attr[0], attr[1]); } - IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)).settings(indexSettings) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, numNodes - 1)).build(); + IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) + .settings(indexSettings) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, numNodes - 1)) + .build(); Index index = indexMetadata.getIndex(); Set validNodeIds = new HashSet<>(); Settings validNodeSettings = Settings.EMPTY; @@ -132,8 +141,11 @@ public void testPerformActionAttrsAllNodesValid() throws Exception { String nodeName = "node_" + i; int nodePort = 9300 + i; String[] nodeAttr = randomFrom(validAttrs); - Settings nodeSettings = Settings.builder().put(validNodeSettings).put(Node.NODE_NAME_SETTING.getKey(), nodeName) - .put(Node.NODE_ATTRIBUTES.getKey() + nodeAttr[0], nodeAttr[1]).build(); + Settings nodeSettings = Settings.builder() + .put(validNodeSettings) + .put(Node.NODE_NAME_SETTING.getKey(), nodeName) + .put(Node.NODE_ATTRIBUTES.getKey() + nodeAttr[0], nodeAttr[1]) + .build(); nodes.add(DiscoveryNode.createLocal(nodeSettings, new TransportAddress(TransportAddress.META_ADDRESS, nodePort), nodeId)); validNodeIds.add(nodeId); } @@ -147,8 +159,11 @@ public void testPerformActionAttrsSomeNodesValid() throws Exception { String[] invalidAttr = new String[] { "box_type", "not_valid" }; Settings.Builder indexSettings = settings(Version.CURRENT); indexSettings.put(IndexMetadata.INDEX_ROUTING_INCLUDE_GROUP_SETTING.getKey() + validAttr[0], validAttr[1]); - IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)).settings(indexSettings) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, numNodes - 1)).build(); + IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) + .settings(indexSettings) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, numNodes - 1)) + .build(); Index index = indexMetadata.getIndex(); Set validNodeIds = new HashSet<>(); Settings validNodeSettings = Settings.builder().put(Node.NODE_ATTRIBUTES.getKey() + validAttr[0], validAttr[1]).build(); @@ -166,8 +181,13 @@ public void testPerformActionAttrsSomeNodesValid() throws Exception { } else { nodeSettingsBuilder.put(invalidNodeSettings).put(Node.NODE_NAME_SETTING.getKey(), nodeName); } - nodes.add(DiscoveryNode.createLocal(nodeSettingsBuilder.build(), new TransportAddress(TransportAddress.META_ADDRESS, nodePort), - nodeId)); + nodes.add( + DiscoveryNode.createLocal( + nodeSettingsBuilder.build(), + new TransportAddress(TransportAddress.META_ADDRESS, nodePort), + nodeId + ) + ); } assertNodeSelected(indexMetadata, index, validNodeIds, nodes); @@ -175,32 +195,38 @@ public void testPerformActionAttrsSomeNodesValid() throws Exception { public void testPerformActionWithClusterExcludeFilters() throws IOException { Settings.Builder indexSettings = settings(Version.CURRENT); - IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)).settings(indexSettings) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 1)).build(); + IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) + .settings(indexSettings) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 1)) + .build(); Index index = indexMetadata.getIndex(); DiscoveryNodes.Builder nodes = DiscoveryNodes.builder(); String nodeId = "node_id_0"; int nodePort = 9300; Builder nodeSettingsBuilder = Settings.builder(); - nodes.add(DiscoveryNode.createLocal(nodeSettingsBuilder.build(), new TransportAddress(TransportAddress.META_ADDRESS, nodePort), - nodeId)); + nodes.add( + DiscoveryNode.createLocal(nodeSettingsBuilder.build(), new TransportAddress(TransportAddress.META_ADDRESS, nodePort), nodeId) + ); - Settings clusterSettings = Settings.builder() - .put("cluster.routing.allocation.exclude._id", "node_id_0") - .build(); - ImmutableOpenMap.Builder indices = ImmutableOpenMap.builder().fPut(index.getName(), - indexMetadata); + Settings clusterSettings = Settings.builder().put("cluster.routing.allocation.exclude._id", "node_id_0").build(); + ImmutableOpenMap.Builder indices = ImmutableOpenMap.builder() + .fPut(index.getName(), indexMetadata); IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index) .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node_id_0", true, ShardRoutingState.STARTED)); ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE) .metadata(Metadata.builder().indices(indices.build()).transientSettings(clusterSettings)) - .nodes(nodes).routingTable(RoutingTable.builder().add(indexRoutingTable).build()).build(); + .nodes(nodes) + .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) + .build(); SetSingleNodeAllocateStep step = createRandomInstance(); - expectThrows(NoNodeAvailableException.class, - () -> PlainActionFuture.get(f -> step.performAction(indexMetadata, clusterState, null, f))); + expectThrows( + NoNodeAvailableException.class, + () -> PlainActionFuture.get(f -> step.performAction(indexMetadata, clusterState, null, f)) + ); Mockito.verifyZeroInteractions(client); } @@ -211,8 +237,11 @@ public void testPerformActionAttrsNoNodesValid() { String[] invalidAttr = new String[] { "box_type", "not_valid" }; Settings.Builder indexSettings = settings(Version.CURRENT); indexSettings.put(IndexMetadata.INDEX_ROUTING_INCLUDE_GROUP_SETTING.getKey() + validAttr[0], validAttr[1]); - IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)).settings(indexSettings) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, numNodes - 1)).build(); + IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) + .settings(indexSettings) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, numNodes - 1)) + .build(); Index index = indexMetadata.getIndex(); Settings invalidNodeSettings = Settings.builder().put(Node.NODE_ATTRIBUTES.getKey() + invalidAttr[0], invalidAttr[1]).build(); DiscoveryNodes.Builder nodes = DiscoveryNodes.builder(); @@ -221,8 +250,13 @@ public void testPerformActionAttrsNoNodesValid() { String nodeName = "node_" + i; int nodePort = 9300 + i; Builder nodeSettingsBuilder = Settings.builder().put(invalidNodeSettings).put(Node.NODE_NAME_SETTING.getKey(), nodeName); - nodes.add(DiscoveryNode.createLocal(nodeSettingsBuilder.build(), new TransportAddress(TransportAddress.META_ADDRESS, nodePort), - nodeId)); + nodes.add( + DiscoveryNode.createLocal( + nodeSettingsBuilder.build(), + new TransportAddress(TransportAddress.META_ADDRESS, nodePort), + nodeId + ) + ); } assertNoValidNode(indexMetadata, index, nodes); @@ -233,16 +267,21 @@ public void testPerformActionAttrsRequestFails() { int numAttrs = randomIntBetween(1, 10); Map validAttributes = new HashMap<>(); for (int i = 0; i < numAttrs; i++) { - validAttributes.put(randomValueOtherThanMany(validAttributes::containsKey, - () -> randomAlphaOfLengthBetween(1,20)), randomAlphaOfLengthBetween(1,20)); + validAttributes.put( + randomValueOtherThanMany(validAttributes::containsKey, () -> randomAlphaOfLengthBetween(1, 20)), + randomAlphaOfLengthBetween(1, 20) + ); } Settings.Builder indexSettings = settings(Version.CURRENT); validAttributes.forEach((k, v) -> { indexSettings.put(IndexMetadata.INDEX_ROUTING_INCLUDE_GROUP_SETTING.getKey() + k, v); }); - IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)).settings(indexSettings) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, numNodes - 1)).build(); + IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) + .settings(indexSettings) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, numNodes - 1)) + .build(); Index index = indexMetadata.getIndex(); Set validNodeIds = new HashSet<>(); Settings validNodeSettings = Settings.EMPTY; @@ -252,18 +291,24 @@ public void testPerformActionAttrsRequestFails() { String nodeName = "node_" + i; int nodePort = 9300 + i; Map.Entry nodeAttr = randomFrom(validAttributes.entrySet()); - Settings nodeSettings = Settings.builder().put(validNodeSettings).put(Node.NODE_NAME_SETTING.getKey(), nodeName) - .put(Node.NODE_ATTRIBUTES.getKey() + nodeAttr.getKey(), nodeAttr.getValue()).build(); + Settings nodeSettings = Settings.builder() + .put(validNodeSettings) + .put(Node.NODE_NAME_SETTING.getKey(), nodeName) + .put(Node.NODE_ATTRIBUTES.getKey() + nodeAttr.getKey(), nodeAttr.getValue()) + .build(); nodes.add(DiscoveryNode.createLocal(nodeSettings, new TransportAddress(TransportAddress.META_ADDRESS, nodePort), nodeId)); validNodeIds.add(nodeId); } - ImmutableOpenMap.Builder indices = ImmutableOpenMap. builder().fPut(index.getName(), - indexMetadata); + ImmutableOpenMap.Builder indices = ImmutableOpenMap.builder() + .fPut(index.getName(), indexMetadata); IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index) .addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node_id_0", true, ShardRoutingState.STARTED)); - ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE).metadata(Metadata.builder().indices(indices.build())) - .nodes(nodes).routingTable(RoutingTable.builder().add(indexRoutingTable).build()).build(); + ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE) + .metadata(Metadata.builder().indices(indices.build())) + .nodes(nodes) + .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) + .build(); SetSingleNodeAllocateStep step = createRandomInstance(); Exception exception = new RuntimeException(); @@ -272,15 +317,24 @@ public void testPerformActionAttrsRequestFails() { UpdateSettingsRequest request = (UpdateSettingsRequest) invocation.getArguments()[0]; @SuppressWarnings("unchecked") ActionListener listener = (ActionListener) invocation.getArguments()[1]; - assertSettingsRequestContainsValueFrom(request, - IndexMetadata.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + "_id", validNodeIds, true, - indexMetadata.getIndex().getName()); + assertSettingsRequestContainsValueFrom( + request, + IndexMetadata.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + "_id", + validNodeIds, + true, + indexMetadata.getIndex().getName() + ); listener.onFailure(exception); return null; }).when(indicesClient).updateSettings(Mockito.any(), Mockito.any()); - assertSame(exception, expectThrows(Exception.class, () -> PlainActionFuture.get( - f -> step.performAction(indexMetadata, clusterState, null, f)))); + assertSame( + exception, + expectThrows( + Exception.class, + () -> PlainActionFuture.get(f -> step.performAction(indexMetadata, clusterState, null, f)) + ) + ); Mockito.verify(client, Mockito.only()).admin(); Mockito.verify(adminClient, Mockito.only()).indices(); @@ -298,8 +352,11 @@ public void testPerformActionAttrsNoShard() { for (String[] attr : validAttrs) { indexSettings.put(IndexMetadata.INDEX_ROUTING_INCLUDE_GROUP_SETTING.getKey() + attr[0], attr[1]); } - IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)).settings(indexSettings) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, numNodes - 1)).build(); + IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) + .settings(indexSettings) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, numNodes - 1)) + .build(); Index index = indexMetadata.getIndex(); Settings validNodeSettings = Settings.EMPTY; DiscoveryNodes.Builder nodes = DiscoveryNodes.builder(); @@ -308,21 +365,29 @@ public void testPerformActionAttrsNoShard() { String nodeName = "node_" + i; int nodePort = 9300 + i; String[] nodeAttr = randomFrom(validAttrs); - Settings nodeSettings = Settings.builder().put(validNodeSettings).put(Node.NODE_NAME_SETTING.getKey(), nodeName) - .put(Node.NODE_ATTRIBUTES.getKey() + nodeAttr[0], nodeAttr[1]).build(); + Settings nodeSettings = Settings.builder() + .put(validNodeSettings) + .put(Node.NODE_NAME_SETTING.getKey(), nodeName) + .put(Node.NODE_ATTRIBUTES.getKey() + nodeAttr[0], nodeAttr[1]) + .build(); nodes.add(DiscoveryNode.createLocal(nodeSettings, new TransportAddress(TransportAddress.META_ADDRESS, nodePort), nodeId)); } - ImmutableOpenMap.Builder indices = ImmutableOpenMap. builder().fPut(index.getName(), - indexMetadata); + ImmutableOpenMap.Builder indices = ImmutableOpenMap.builder() + .fPut(index.getName(), indexMetadata); IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index); - ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE).metadata(Metadata.builder().indices(indices.build())) - .nodes(nodes).routingTable(RoutingTable.builder().add(indexRoutingTable).build()).build(); + ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE) + .metadata(Metadata.builder().indices(indices.build())) + .nodes(nodes) + .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) + .build(); SetSingleNodeAllocateStep step = createRandomInstance(); - IndexNotFoundException e = expectThrows(IndexNotFoundException.class, - () -> PlainActionFuture.get(f -> step.performAction(indexMetadata, clusterState, null, f))); + IndexNotFoundException e = expectThrows( + IndexNotFoundException.class, + () -> PlainActionFuture.get(f -> step.performAction(indexMetadata, clusterState, null, f)) + ); assertEquals(indexMetadata.getIndex(), e.getIndex()); Mockito.verifyZeroInteractions(client); @@ -335,8 +400,11 @@ public void testPerformActionSomeShardsOnlyOnNewNodes() throws Exception { final int numOldNodes = numNodes - numNewNodes; final int numberOfShards = randomIntBetween(1, 5); - IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)).settings(settings(oldVersion)) - .numberOfShards(numberOfShards).numberOfReplicas(randomIntBetween(0, numNewNodes - 1)).build(); + IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) + .settings(settings(oldVersion)) + .numberOfShards(numberOfShards) + .numberOfReplicas(randomIntBetween(0, numNewNodes - 1)) + .build(); DiscoveryNodes.Builder nodes = DiscoveryNodes.builder(); Set newNodeIds = new HashSet<>(); @@ -346,13 +414,16 @@ public void testPerformActionSomeShardsOnlyOnNewNodes() throws Exception { int nodePort = 9300 + i; Settings nodeSettings = Settings.builder().put(Node.NODE_NAME_SETTING.getKey(), nodeName).build(); newNodeIds.add(nodeId); - nodes.add(new DiscoveryNode( - Node.NODE_NAME_SETTING.get(nodeSettings), - nodeId, - new TransportAddress(TransportAddress.META_ADDRESS, nodePort), - Node.NODE_ATTRIBUTES.getAsMap(nodeSettings), - DiscoveryNode.getRolesFromSettings(nodeSettings), - Version.CURRENT)); + nodes.add( + new DiscoveryNode( + Node.NODE_NAME_SETTING.get(nodeSettings), + nodeId, + new TransportAddress(TransportAddress.META_ADDRESS, nodePort), + Node.NODE_ATTRIBUTES.getAsMap(nodeSettings), + DiscoveryNode.getRolesFromSettings(nodeSettings), + Version.CURRENT + ) + ); } Set oldNodeIds = new HashSet<>(); @@ -362,13 +433,16 @@ public void testPerformActionSomeShardsOnlyOnNewNodes() throws Exception { int nodePort = 9300 + numNewNodes + i; Settings nodeSettings = Settings.builder().put(Node.NODE_NAME_SETTING.getKey(), nodeName).build(); oldNodeIds.add(nodeId); - nodes.add(new DiscoveryNode( - Node.NODE_NAME_SETTING.get(nodeSettings), - nodeId, - new TransportAddress(TransportAddress.META_ADDRESS, nodePort), - Node.NODE_ATTRIBUTES.getAsMap(nodeSettings), - DiscoveryNode.getRolesFromSettings(nodeSettings), - oldVersion)); + nodes.add( + new DiscoveryNode( + Node.NODE_NAME_SETTING.get(nodeSettings), + nodeId, + new TransportAddress(TransportAddress.META_ADDRESS, nodePort), + Node.NODE_ATTRIBUTES.getAsMap(nodeSettings), + DiscoveryNode.getRolesFromSettings(nodeSettings), + oldVersion + ) + ); } Set nodeIds = new HashSet<>(); @@ -393,8 +467,11 @@ public void testPerformActionSomeShardsOnlyOnNewNodesButNewNodesInvalidAttrs() { final String invalidAttr = "not_valid"; Settings.Builder indexSettings = settings(oldVersion); indexSettings.put(IndexMetadata.INDEX_ROUTING_INCLUDE_GROUP_SETTING.getKey() + attribute, validAttr); - IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)).settings(indexSettings) - .numberOfShards(numberOfShards).numberOfReplicas(randomIntBetween(0, numNewNodes - 1)).build(); + IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) + .settings(indexSettings) + .numberOfShards(numberOfShards) + .numberOfReplicas(randomIntBetween(0, numNewNodes - 1)) + .build(); DiscoveryNodes.Builder nodes = DiscoveryNodes.builder(); Set newNodeIds = new HashSet<>(); @@ -404,15 +481,19 @@ public void testPerformActionSomeShardsOnlyOnNewNodesButNewNodesInvalidAttrs() { int nodePort = 9300 + i; Settings nodeSettings = Settings.builder() .put(Node.NODE_NAME_SETTING.getKey(), nodeName) - .put(Node.NODE_ATTRIBUTES.getKey() + attribute, invalidAttr).build(); + .put(Node.NODE_ATTRIBUTES.getKey() + attribute, invalidAttr) + .build(); newNodeIds.add(nodeId); - nodes.add(new DiscoveryNode( - Node.NODE_NAME_SETTING.get(nodeSettings), - nodeId, - new TransportAddress(TransportAddress.META_ADDRESS, nodePort), - Node.NODE_ATTRIBUTES.getAsMap(nodeSettings), - DiscoveryNode.getRolesFromSettings(nodeSettings), - Version.CURRENT)); + nodes.add( + new DiscoveryNode( + Node.NODE_NAME_SETTING.get(nodeSettings), + nodeId, + new TransportAddress(TransportAddress.META_ADDRESS, nodePort), + Node.NODE_ATTRIBUTES.getAsMap(nodeSettings), + DiscoveryNode.getRolesFromSettings(nodeSettings), + Version.CURRENT + ) + ); } Set oldNodeIds = new HashSet<>(); @@ -422,15 +503,19 @@ public void testPerformActionSomeShardsOnlyOnNewNodesButNewNodesInvalidAttrs() { int nodePort = 9300 + numNewNodes + i; Settings nodeSettings = Settings.builder() .put(Node.NODE_NAME_SETTING.getKey(), nodeName) - .put(Node.NODE_ATTRIBUTES.getKey() + attribute, validAttr).build(); + .put(Node.NODE_ATTRIBUTES.getKey() + attribute, validAttr) + .build(); oldNodeIds.add(nodeId); - nodes.add(new DiscoveryNode( - Node.NODE_NAME_SETTING.get(nodeSettings), - nodeId, - new TransportAddress(TransportAddress.META_ADDRESS, nodePort), - Node.NODE_ATTRIBUTES.getAsMap(nodeSettings), - DiscoveryNode.getRolesFromSettings(nodeSettings), - oldVersion)); + nodes.add( + new DiscoveryNode( + Node.NODE_NAME_SETTING.get(nodeSettings), + nodeId, + new TransportAddress(TransportAddress.META_ADDRESS, nodePort), + Node.NODE_ATTRIBUTES.getAsMap(nodeSettings), + DiscoveryNode.getRolesFromSettings(nodeSettings), + oldVersion + ) + ); } Set nodeIds = new HashSet<>(); nodeIds.addAll(newNodeIds); @@ -453,8 +538,11 @@ public void testPerformActionNewShardsExistButWithInvalidAttributes() throws Exc final String invalidAttr = "not_valid"; Settings.Builder indexSettings = settings(oldVersion); indexSettings.put(IndexMetadata.INDEX_ROUTING_INCLUDE_GROUP_SETTING.getKey() + attribute, validAttr); - IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)).settings(indexSettings) - .numberOfShards(numberOfShards).numberOfReplicas(randomIntBetween(0, numOldNodes - 1)).build(); + IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) + .settings(indexSettings) + .numberOfShards(numberOfShards) + .numberOfReplicas(randomIntBetween(0, numOldNodes - 1)) + .build(); DiscoveryNodes.Builder nodes = DiscoveryNodes.builder(); Set newNodeIds = new HashSet<>(); @@ -464,15 +552,19 @@ public void testPerformActionNewShardsExistButWithInvalidAttributes() throws Exc int nodePort = 9300 + i; Settings nodeSettings = Settings.builder() .put(Node.NODE_NAME_SETTING.getKey(), nodeName) - .put(Node.NODE_ATTRIBUTES.getKey() + attribute, invalidAttr).build(); + .put(Node.NODE_ATTRIBUTES.getKey() + attribute, invalidAttr) + .build(); newNodeIds.add(nodeId); - nodes.add(new DiscoveryNode( - Node.NODE_NAME_SETTING.get(nodeSettings), - nodeId, - new TransportAddress(TransportAddress.META_ADDRESS, nodePort), - Node.NODE_ATTRIBUTES.getAsMap(nodeSettings), - DiscoveryNode.getRolesFromSettings(nodeSettings), - Version.CURRENT)); + nodes.add( + new DiscoveryNode( + Node.NODE_NAME_SETTING.get(nodeSettings), + nodeId, + new TransportAddress(TransportAddress.META_ADDRESS, nodePort), + Node.NODE_ATTRIBUTES.getAsMap(nodeSettings), + DiscoveryNode.getRolesFromSettings(nodeSettings), + Version.CURRENT + ) + ); } Set oldNodeIds = new HashSet<>(); @@ -482,15 +574,19 @@ public void testPerformActionNewShardsExistButWithInvalidAttributes() throws Exc int nodePort = 9300 + numNewNodes + i; Settings nodeSettings = Settings.builder() .put(Node.NODE_NAME_SETTING.getKey(), nodeName) - .put(Node.NODE_ATTRIBUTES.getKey() + attribute, validAttr).build(); + .put(Node.NODE_ATTRIBUTES.getKey() + attribute, validAttr) + .build(); oldNodeIds.add(nodeId); - nodes.add(new DiscoveryNode( - Node.NODE_NAME_SETTING.get(nodeSettings), - nodeId, - new TransportAddress(TransportAddress.META_ADDRESS, nodePort), - Node.NODE_ATTRIBUTES.getAsMap(nodeSettings), - DiscoveryNode.getRolesFromSettings(nodeSettings), - oldVersion)); + nodes.add( + new DiscoveryNode( + Node.NODE_NAME_SETTING.get(nodeSettings), + nodeId, + new TransportAddress(TransportAddress.META_ADDRESS, nodePort), + Node.NODE_ATTRIBUTES.getAsMap(nodeSettings), + DiscoveryNode.getRolesFromSettings(nodeSettings), + oldVersion + ) + ); } Set nodeIds = new HashSet<>(); nodeIds.addAll(newNodeIds); @@ -502,19 +598,27 @@ public void testPerformActionNewShardsExistButWithInvalidAttributes() throws Exc assertNodeSelected(indexMetadata, indexMetadata.getIndex(), oldNodeIds, discoveryNodes, indexRoutingTable.build()); } - private void assertNodeSelected(IndexMetadata indexMetadata, Index index, - Set validNodeIds, DiscoveryNodes.Builder nodes) throws Exception { + private void assertNodeSelected(IndexMetadata indexMetadata, Index index, Set validNodeIds, DiscoveryNodes.Builder nodes) + throws Exception { DiscoveryNodes discoveryNodes = nodes.build(); IndexRoutingTable.Builder indexRoutingTable = createRoutingTable(indexMetadata, index, discoveryNodes); assertNodeSelected(indexMetadata, index, validNodeIds, discoveryNodes, indexRoutingTable.build()); } - private void assertNodeSelected(IndexMetadata indexMetadata, Index index, Set validNodeIds, DiscoveryNodes nodes, - IndexRoutingTable indexRoutingTable) throws Exception { - ImmutableOpenMap.Builder indices = ImmutableOpenMap. builder().fPut(index.getName(), - indexMetadata); - ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE).metadata(Metadata.builder().indices(indices.build())) - .nodes(nodes).routingTable(RoutingTable.builder().add(indexRoutingTable).build()).build(); + private void assertNodeSelected( + IndexMetadata indexMetadata, + Index index, + Set validNodeIds, + DiscoveryNodes nodes, + IndexRoutingTable indexRoutingTable + ) throws Exception { + ImmutableOpenMap.Builder indices = ImmutableOpenMap.builder() + .fPut(index.getName(), indexMetadata); + ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE) + .metadata(Metadata.builder().indices(indices.build())) + .nodes(nodes) + .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) + .build(); SetSingleNodeAllocateStep step = createRandomInstance(); @@ -522,9 +626,13 @@ private void assertNodeSelected(IndexMetadata indexMetadata, Index index, Set listener = (ActionListener) invocation.getArguments()[1]; - assertSettingsRequestContainsValueFrom(request, - IndexMetadata.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + "_id", validNodeIds, true, - indexMetadata.getIndex().getName()); + assertSettingsRequestContainsValueFrom( + request, + IndexMetadata.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + "_id", + validNodeIds, + true, + indexMetadata.getIndex().getName() + ); listener.onResponse(AcknowledgedResponse.TRUE); return null; }).when(indicesClient).updateSettings(Mockito.any(), Mockito.any()); @@ -545,15 +653,20 @@ private void assertNoValidNode(IndexMetadata indexMetadata, Index index, Discove private void assertNoValidNode(IndexMetadata indexMetadata, Index index, DiscoveryNodes nodes, IndexRoutingTable indexRoutingTable) { - ImmutableOpenMap.Builder indices = ImmutableOpenMap.builder().fPut(index.getName(), - indexMetadata); - ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE).metadata(Metadata.builder().indices(indices.build())) - .nodes(nodes).routingTable(RoutingTable.builder().add(indexRoutingTable).build()).build(); + ImmutableOpenMap.Builder indices = ImmutableOpenMap.builder() + .fPut(index.getName(), indexMetadata); + ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE) + .metadata(Metadata.builder().indices(indices.build())) + .nodes(nodes) + .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) + .build(); SetSingleNodeAllocateStep step = createRandomInstance(); - expectThrows(NoNodeAvailableException.class, - () -> PlainActionFuture.get(f -> step.performAction(indexMetadata, clusterState, null, f))); + expectThrows( + NoNodeAvailableException.class, + () -> PlainActionFuture.get(f -> step.performAction(indexMetadata, clusterState, null, f)) + ); Mockito.verifyZeroInteractions(client); } @@ -570,21 +683,26 @@ private IndexRoutingTable.Builder createRoutingTable(IndexMetadata indexMetadata Set nodesThisShardCanBePutOn = new HashSet<>(nodeIds); String currentNode = randomFrom(nodesThisShardCanBePutOn); nodesThisShardCanBePutOn.remove(currentNode); - indexRoutingTable.addShard(TestShardRouting.newShardRouting(new ShardId(index, primary), currentNode, - true, ShardRoutingState.STARTED)); + indexRoutingTable.addShard( + TestShardRouting.newShardRouting(new ShardId(index, primary), currentNode, true, ShardRoutingState.STARTED) + ); for (int replica = 0; replica < indexMetadata.getNumberOfReplicas(); replica++) { assertThat("not enough nodes to allocate all initial shards", nodesThisShardCanBePutOn.size(), greaterThan(0)); String replicaNode = randomFrom(nodesThisShardCanBePutOn); nodesThisShardCanBePutOn.remove(replicaNode); - indexRoutingTable.addShard(TestShardRouting.newShardRouting(new ShardId(index, primary), replicaNode, - false, ShardRoutingState.STARTED)); + indexRoutingTable.addShard( + TestShardRouting.newShardRouting(new ShardId(index, primary), replicaNode, false, ShardRoutingState.STARTED) + ); } } return indexRoutingTable; } - private IndexRoutingTable.Builder createRoutingTableWithOneShardOnSubset(IndexMetadata indexMetadata, Set subset, - Set allNodeIds) { + private IndexRoutingTable.Builder createRoutingTableWithOneShardOnSubset( + IndexMetadata indexMetadata, + Set subset, + Set allNodeIds + ) { IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(indexMetadata.getIndex()); final int numberOfShards = indexMetadata.getNumberOfShards(); final int shardOnlyOnNewNodes = randomIntBetween(0, numberOfShards - 1); @@ -598,14 +716,26 @@ private IndexRoutingTable.Builder createRoutingTableWithOneShardOnSubset(IndexMe } String currentNode = randomFrom(nodesThisShardCanBePutOn); nodesThisShardCanBePutOn.remove(currentNode); - indexRoutingTable.addShard(TestShardRouting.newShardRouting(new ShardId(indexMetadata.getIndex(), primary), currentNode, - true, ShardRoutingState.STARTED)); + indexRoutingTable.addShard( + TestShardRouting.newShardRouting( + new ShardId(indexMetadata.getIndex(), primary), + currentNode, + true, + ShardRoutingState.STARTED + ) + ); for (int replica = 0; replica < indexMetadata.getNumberOfReplicas(); replica++) { assertThat("not enough nodes to allocate all initial shards", nodesThisShardCanBePutOn.size(), greaterThan(0)); String replicaNode = randomFrom(nodesThisShardCanBePutOn); nodesThisShardCanBePutOn.remove(replicaNode); - indexRoutingTable.addShard(TestShardRouting.newShardRouting(new ShardId(indexMetadata.getIndex(), primary), replicaNode, - false, ShardRoutingState.STARTED)); + indexRoutingTable.addShard( + TestShardRouting.newShardRouting( + new ShardId(indexMetadata.getIndex(), primary), + replicaNode, + false, + ShardRoutingState.STARTED + ) + ); } } return indexRoutingTable; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkActionTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkActionTests.java index 3381350c6de7d..83de669a01089 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkActionTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkActionTests.java @@ -79,30 +79,55 @@ public void testPerformActionWithSkip() { int numberOfShards = randomIntBetween(1, 10); ShrinkAction action = new ShrinkAction(numberOfShards, null); String phase = randomAlphaOfLengthBetween(1, 10); - StepKey nextStepKey = new StepKey(randomAlphaOfLengthBetween(1, 10), randomAlphaOfLengthBetween(1, 10), - randomAlphaOfLengthBetween(1, 10)); + StepKey nextStepKey = new StepKey( + randomAlphaOfLengthBetween(1, 10), + randomAlphaOfLengthBetween(1, 10), + randomAlphaOfLengthBetween(1, 10) + ); List steps = action.toSteps(null, phase, nextStepKey); BranchingStep step = ((BranchingStep) steps.get(0)); - LifecyclePolicy policy = new LifecyclePolicy(lifecycleName, Collections.singletonMap("warm", - new Phase("warm", TimeValue.ZERO, Collections.singletonMap(action.getWriteableName(), action)))); - LifecyclePolicyMetadata policyMetadata = new LifecyclePolicyMetadata(policy, Collections.emptyMap(), - randomNonNegativeLong(), randomNonNegativeLong()); + LifecyclePolicy policy = new LifecyclePolicy( + lifecycleName, + Collections.singletonMap("warm", new Phase("warm", TimeValue.ZERO, Collections.singletonMap(action.getWriteableName(), action))) + ); + LifecyclePolicyMetadata policyMetadata = new LifecyclePolicyMetadata( + policy, + Collections.emptyMap(), + randomNonNegativeLong(), + randomNonNegativeLong() + ); String indexName = randomAlphaOfLength(5); - ClusterState state = ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder() - .putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata( - Collections.singletonMap(policyMetadata.getName(), policyMetadata), OperationMode.RUNNING)) - .put(IndexMetadata.builder(indexName).settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, lifecycleName)) - .putCustom(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY, - LifecycleExecutionState.builder() - .setPhase(step.getKey().getPhase()) - .setPhaseTime(0L) - .setAction(step.getKey().getAction()) - .setActionTime(0L) - .setStep(step.getKey().getName()) - .setStepTime(0L) - .build().asMap()) - .numberOfShards(numberOfShards).numberOfReplicas(0))).build(); + ClusterState state = ClusterState.builder(ClusterName.DEFAULT) + .metadata( + Metadata.builder() + .putCustom( + IndexLifecycleMetadata.TYPE, + new IndexLifecycleMetadata( + Collections.singletonMap(policyMetadata.getName(), policyMetadata), + OperationMode.RUNNING + ) + ) + .put( + IndexMetadata.builder(indexName) + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, lifecycleName)) + .putCustom( + LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY, + LifecycleExecutionState.builder() + .setPhase(step.getKey().getPhase()) + .setPhaseTime(0L) + .setAction(step.getKey().getAction()) + .setActionTime(0L) + .setStep(step.getKey().getName()) + .setStepTime(0L) + .build() + .asMap() + ) + .numberOfShards(numberOfShards) + .numberOfReplicas(0) + ) + ) + .build(); step.performAction(state.metadata().index(indexName).getIndex(), state); assertThat(step.getNextStepKey(), equalTo(nextStepKey)); } @@ -114,30 +139,55 @@ public void testPerformActionWithoutSkip() { String lifecycleName = randomAlphaOfLengthBetween(4, 10); ShrinkAction action = new ShrinkAction(expectedFinalShards, null); String phase = randomAlphaOfLengthBetween(1, 10); - StepKey nextStepKey = new StepKey(randomAlphaOfLengthBetween(1, 10), randomAlphaOfLengthBetween(1, 10), - randomAlphaOfLengthBetween(1, 10)); + StepKey nextStepKey = new StepKey( + randomAlphaOfLengthBetween(1, 10), + randomAlphaOfLengthBetween(1, 10), + randomAlphaOfLengthBetween(1, 10) + ); List steps = action.toSteps(null, phase, nextStepKey); BranchingStep step = ((BranchingStep) steps.get(0)); - LifecyclePolicy policy = new LifecyclePolicy(lifecycleName, Collections.singletonMap("warm", - new Phase("warm", TimeValue.ZERO, Collections.singletonMap(action.getWriteableName(), action)))); - LifecyclePolicyMetadata policyMetadata = new LifecyclePolicyMetadata(policy, Collections.emptyMap(), - randomNonNegativeLong(), randomNonNegativeLong()); + LifecyclePolicy policy = new LifecyclePolicy( + lifecycleName, + Collections.singletonMap("warm", new Phase("warm", TimeValue.ZERO, Collections.singletonMap(action.getWriteableName(), action))) + ); + LifecyclePolicyMetadata policyMetadata = new LifecyclePolicyMetadata( + policy, + Collections.emptyMap(), + randomNonNegativeLong(), + randomNonNegativeLong() + ); String indexName = randomAlphaOfLength(5); - ClusterState state = ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder() - .putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata( - Collections.singletonMap(policyMetadata.getName(), policyMetadata), OperationMode.RUNNING)) - .put(IndexMetadata.builder(indexName).settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, lifecycleName)) - .putCustom(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY, - LifecycleExecutionState.builder() - .setPhase(step.getKey().getPhase()) - .setPhaseTime(0L) - .setAction(step.getKey().getAction()) - .setActionTime(0L) - .setStep(step.getKey().getName()) - .setStepTime(0L) - .build().asMap()) - .numberOfShards(numShards).numberOfReplicas(0))).build(); + ClusterState state = ClusterState.builder(ClusterName.DEFAULT) + .metadata( + Metadata.builder() + .putCustom( + IndexLifecycleMetadata.TYPE, + new IndexLifecycleMetadata( + Collections.singletonMap(policyMetadata.getName(), policyMetadata), + OperationMode.RUNNING + ) + ) + .put( + IndexMetadata.builder(indexName) + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, lifecycleName)) + .putCustom( + LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY, + LifecycleExecutionState.builder() + .setPhase(step.getKey().getPhase()) + .setPhaseTime(0L) + .setAction(step.getKey().getAction()) + .setActionTime(0L) + .setStep(step.getKey().getName()) + .setStepTime(0L) + .build() + .asMap() + ) + .numberOfShards(numShards) + .numberOfReplicas(0) + ) + ) + .build(); step.performAction(state.metadata().index(indexName).getIndex(), state); assertThat(step.getNextStepKey(), equalTo(steps.get(1).getKey())); } @@ -145,8 +195,11 @@ public void testPerformActionWithoutSkip() { public void testToSteps() { ShrinkAction action = createTestInstance(); String phase = randomAlphaOfLengthBetween(1, 10); - StepKey nextStepKey = new StepKey(randomAlphaOfLengthBetween(1, 10), randomAlphaOfLengthBetween(1, 10), - randomAlphaOfLengthBetween(1, 10)); + StepKey nextStepKey = new StepKey( + randomAlphaOfLengthBetween(1, 10), + randomAlphaOfLengthBetween(1, 10), + randomAlphaOfLengthBetween(1, 10) + ); List steps = action.toSteps(null, phase, nextStepKey); assertThat(steps.size(), equalTo(17)); StepKey expectedFirstKey = new StepKey(phase, ShrinkAction.NAME, ShrinkAction.CONDITIONAL_SKIP_SHRINK_STEP); @@ -215,8 +268,10 @@ public void testToSteps() { assertTrue(steps.get(10) instanceof ClusterStateWaitUntilThresholdStep); assertThat(steps.get(10).getKey(), equalTo(expectedEleventhKey)); assertThat(steps.get(10).getNextStepKey(), equalTo(expectedTwelveKey)); - assertThat(((ClusterStateWaitUntilThresholdStep) steps.get(10)).getStepToExecute(), - is(instanceOf(ShrunkShardsAllocatedStep.class))); + assertThat( + ((ClusterStateWaitUntilThresholdStep) steps.get(10)).getStepToExecute(), + is(instanceOf(ShrunkShardsAllocatedStep.class)) + ); // assert in case the threshold is breached we go back to the "cleanup shrunk index" step assertThat(((ClusterStateWaitUntilThresholdStep) steps.get(10)).getNextKeyOnThreshold(), is(expectedSixthKey)); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkIndexNameSupplierTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkIndexNameSupplierTests.java index 02660ce136b77..889391953e0ec 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkIndexNameSupplierTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkIndexNameSupplierTests.java @@ -20,16 +20,17 @@ public void testGetShrinkIndexName() { { // if the lifecycle execution state contains a `shrink_index_name`, that one will be returned String shrinkIndexName = "the-shrink-index"; - LifecycleExecutionState lifecycleExecutionState = - LifecycleExecutionState.builder().setShrinkIndexName(shrinkIndexName).build(); + LifecycleExecutionState lifecycleExecutionState = LifecycleExecutionState.builder().setShrinkIndexName(shrinkIndexName).build(); assertThat(getShrinkIndexName(sourceIndexName, lifecycleExecutionState), is(shrinkIndexName)); } { // if the lifecycle execution state does NOT contain a `shrink_index_name`, `shrink-` will be prefixed to the index name - assertThat(getShrinkIndexName(sourceIndexName, LifecycleExecutionState.builder().build()), - is(SHRUNKEN_INDEX_PREFIX + sourceIndexName)); + assertThat( + getShrinkIndexName(sourceIndexName, LifecycleExecutionState.builder().build()), + is(SHRUNKEN_INDEX_PREFIX + sourceIndexName) + ); } } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkSetAliasStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkSetAliasStepTests.java index 2e9366a9f08ce..261fe9114a2a1 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkSetAliasStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkSetAliasStepTests.java @@ -38,14 +38,14 @@ public ShrinkSetAliasStep mutateInstance(ShrinkSetAliasStep instance) { StepKey key = instance.getKey(); StepKey nextKey = instance.getNextStepKey(); switch (between(0, 1)) { - case 0: - key = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); - break; - case 1: - nextKey = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); - break; - default: - throw new AssertionError("Illegal randomisation branch"); + case 0: + key = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); + break; + case 1: + nextKey = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); + break; + default: + throw new AssertionError("Illegal randomisation branch"); } return new ShrinkSetAliasStep(key, nextKey, instance.getClient()); } @@ -56,8 +56,10 @@ public ShrinkSetAliasStep copyInstance(ShrinkSetAliasStep instance) { } public void testPerformAction() throws Exception { - IndexMetadata.Builder indexMetadataBuilder = IndexMetadata.builder(randomAlphaOfLength(10)).settings(settings(Version.CURRENT)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)); + IndexMetadata.Builder indexMetadataBuilder = IndexMetadata.builder(randomAlphaOfLength(10)) + .settings(settings(Version.CURRENT)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)); AliasMetadata.Builder aliasBuilder = AliasMetadata.builder(randomAlphaOfLengthBetween(3, 10)); if (randomBoolean()) { aliasBuilder.routing(randomAlphaOfLengthBetween(3, 10)); @@ -80,11 +82,16 @@ public void testPerformAction() throws Exception { List expectedAliasActions = Arrays.asList( IndicesAliasesRequest.AliasActions.removeIndex().index(sourceIndex), IndicesAliasesRequest.AliasActions.add().index(shrunkenIndex).alias(sourceIndex), - IndicesAliasesRequest.AliasActions.add().index(shrunkenIndex).alias(aliasMetadata.alias()) - .searchRouting(aliasMetadata.searchRouting()).indexRouting(aliasMetadata.indexRouting()) - .filter(aliasMetadataFilter).writeIndex(null)); - - Mockito.doAnswer( invocation -> { + IndicesAliasesRequest.AliasActions.add() + .index(shrunkenIndex) + .alias(aliasMetadata.alias()) + .searchRouting(aliasMetadata.searchRouting()) + .indexRouting(aliasMetadata.indexRouting()) + .filter(aliasMetadataFilter) + .writeIndex(null) + ); + + Mockito.doAnswer(invocation -> { IndicesAliasesRequest request = (IndicesAliasesRequest) invocation.getArguments()[0]; assertThat(request.getAliasActions(), equalTo(expectedAliasActions)); @SuppressWarnings("unchecked") @@ -101,8 +108,11 @@ public void testPerformAction() throws Exception { } public void testPerformActionFailure() { - IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)).settings(settings(Version.CURRENT)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) + .settings(settings(Version.CURRENT)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); Exception exception = new RuntimeException(); ShrinkSetAliasStep step = createRandomInstance(); @@ -113,8 +123,13 @@ public void testPerformActionFailure() { return null; }).when(indicesClient).aliases(Mockito.any(), Mockito.any()); - assertSame(exception, expectThrows(Exception.class, () -> PlainActionFuture.get( - f -> step.performAction(indexMetadata, emptyClusterState(), null, f)))); + assertSame( + exception, + expectThrows( + Exception.class, + () -> PlainActionFuture.get(f -> step.performAction(indexMetadata, emptyClusterState(), null, f)) + ) + ); Mockito.verify(client, Mockito.only()).admin(); Mockito.verify(adminClient, Mockito.only()).indices(); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkStepTests.java index ae51b70a0d7ab..76167b502f2ef 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkStepTests.java @@ -39,7 +39,7 @@ public ShrinkStep createRandomInstance() { if (randomBoolean()) { numberOfShards = randomIntBetween(1, 20); } else { - maxPrimaryShardSize = new ByteSizeValue(between(1,100)); + maxPrimaryShardSize = new ByteSizeValue(between(1, 100)); } return new ShrinkStep(stepKey, nextStepKey, client, numberOfShards, maxPrimaryShardSize); } @@ -52,22 +52,22 @@ public ShrinkStep mutateInstance(ShrinkStep instance) { ByteSizeValue maxPrimaryShardSize = instance.getMaxPrimaryShardSize(); switch (between(0, 2)) { - case 0: - key = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); - break; - case 1: - nextKey = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); - break; - case 2: - if (numberOfShards != null) { - numberOfShards = numberOfShards + 1; - } - if (maxPrimaryShardSize != null) { - maxPrimaryShardSize = new ByteSizeValue(maxPrimaryShardSize.getBytes() + 1); - } - break; - default: - throw new AssertionError("Illegal randomisation branch"); + case 0: + key = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); + break; + case 1: + nextKey = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); + break; + case 2: + if (numberOfShards != null) { + numberOfShards = numberOfShards + 1; + } + if (maxPrimaryShardSize != null) { + maxPrimaryShardSize = new ByteSizeValue(maxPrimaryShardSize.getBytes() + 1); + } + break; + default: + throw new AssertionError("Illegal randomisation branch"); } return new ShrinkStep(key, nextKey, instance.getClient(), numberOfShards, maxPrimaryShardSize); @@ -75,8 +75,13 @@ public ShrinkStep mutateInstance(ShrinkStep instance) { @Override public ShrinkStep copyInstance(ShrinkStep instance) { - return new ShrinkStep(instance.getKey(), instance.getNextStepKey(), instance.getClient(), instance.getNumberOfShards(), - instance.getMaxPrimaryShardSize()); + return new ShrinkStep( + instance.getKey(), + instance.getNextStepKey(), + instance.getClient(), + instance.getNumberOfShards(), + instance.getMaxPrimaryShardSize() + ); } public void testPerformAction() throws Exception { @@ -88,11 +93,10 @@ public void testPerformAction() throws Exception { lifecycleState.setStep(step.getKey().getName()); lifecycleState.setIndexCreationDate(randomNonNegativeLong()); IndexMetadata sourceIndexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) - .settings(settings(Version.CURRENT) - .put(LifecycleSettings.LIFECYCLE_NAME, lifecycleName) - ) + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, lifecycleName)) .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) .putAlias(AliasMetadata.builder("my_alias")) .build(); @@ -112,8 +116,10 @@ public void testPerformAction() throws Exception { } assertThat(request.getTargetIndexRequest().settings(), equalTo(builder.build())); if (step.getNumberOfShards() != null) { - assertThat(request.getTargetIndexRequest().settings() - .getAsInt(IndexMetadata.SETTING_NUMBER_OF_SHARDS, -1), equalTo(step.getNumberOfShards())); + assertThat( + request.getTargetIndexRequest().settings().getAsInt(IndexMetadata.SETTING_NUMBER_OF_SHARDS, -1), + equalTo(step.getNumberOfShards()) + ); } request.setMaxPrimaryShardSize(step.getMaxPrimaryShardSize()); listener.onResponse(new ResizeResponse(true, true, sourceIndexMetadata.getIndex().getName())); @@ -139,25 +145,27 @@ public void testPerformActionShrunkenIndexExists() throws Exception { String generatedShrunkenIndexName = GenerateUniqueIndexNameStep.generateValidIndexName(SHRUNKEN_INDEX_PREFIX, sourceIndexName); lifecycleState.setShrinkIndexName(generatedShrunkenIndexName); IndexMetadata sourceIndexMetadata = IndexMetadata.builder(sourceIndexName) - .settings(settings(Version.CURRENT) - .put(LifecycleSettings.LIFECYCLE_NAME, lifecycleName) - ) + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, lifecycleName)) .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) .putAlias(AliasMetadata.builder("my_alias")) .build(); - IndexMetadata indexMetadata = IndexMetadata.builder(generatedShrunkenIndexName).settings(settings(Version.CURRENT)) - .numberOfShards(1).numberOfReplicas(0).build(); - ImmutableOpenMap.Builder indices = ImmutableOpenMap.builder().fPut( - generatedShrunkenIndexName, indexMetadata); - ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE).metadata(Metadata.builder().indices(indices.build())) + IndexMetadata indexMetadata = IndexMetadata.builder(generatedShrunkenIndexName) + .settings(settings(Version.CURRENT)) + .numberOfShards(1) + .numberOfReplicas(0) + .build(); + ImmutableOpenMap.Builder indices = ImmutableOpenMap.builder() + .fPut(generatedShrunkenIndexName, indexMetadata); + ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE) + .metadata(Metadata.builder().indices(indices.build())) .build(); step.performAction(sourceIndexMetadata, clusterState, null, new ActionListener<>() { @Override - public void onResponse(Void unused) { - } + public void onResponse(Void unused) {} @Override public void onFailure(Exception e) { @@ -169,9 +177,12 @@ public void onFailure(Exception e) { public void testPerformActionIsCompleteForUnAckedRequests() throws Exception { LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); lifecycleState.setIndexCreationDate(randomNonNegativeLong()); - IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)).settings(settings(Version.CURRENT)) + IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) + .settings(settings(Version.CURRENT)) .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); ShrinkStep step = createRandomInstance(); Mockito.doAnswer(invocation -> { @@ -191,9 +202,12 @@ public void testPerformActionIsCompleteForUnAckedRequests() throws Exception { public void testPerformActionFailure() throws Exception { LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); lifecycleState.setIndexCreationDate(randomNonNegativeLong()); - IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)).settings(settings(Version.CURRENT)) + IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) + .settings(settings(Version.CURRENT)) .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); Exception exception = new RuntimeException(); ShrinkStep step = createRandomInstance(); @@ -204,8 +218,13 @@ public void testPerformActionFailure() throws Exception { return null; }).when(indicesClient).resizeIndex(Mockito.any(), Mockito.any()); - assertSame(exception, expectThrows(Exception.class, () -> PlainActionFuture.get( - f -> step.performAction(indexMetadata, emptyClusterState(), null, f)))); + assertSame( + exception, + expectThrows( + Exception.class, + () -> PlainActionFuture.get(f -> step.performAction(indexMetadata, emptyClusterState(), null, f)) + ) + ); Mockito.verify(client, Mockito.only()).admin(); Mockito.verify(adminClient, Mockito.only()).indices(); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrunkShardsAllocatedStepInfoTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrunkShardsAllocatedStepInfoTests.java index 6a12ac90859ce..e67c1d59f0fa6 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrunkShardsAllocatedStepInfoTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrunkShardsAllocatedStepInfoTests.java @@ -7,9 +7,9 @@ package org.elasticsearch.xpack.core.ilm; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.test.EqualsHashCodeTestUtils; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ilm.ShrunkShardsAllocatedStep.Info; import java.io.IOException; @@ -46,17 +46,17 @@ protected Info mutateInstance(Info instance) throws IOException { int actualShards = instance.getActualShards(); boolean allShardsActive = instance.allShardsActive(); switch (between(0, 2)) { - case 0: - shrunkIndexExists = shrunkIndexExists == false; - break; - case 1: - actualShards += between(1, 20); - break; - case 2: - allShardsActive = allShardsActive == false; - break; - default: - throw new AssertionError("Illegal randomisation branch"); + case 0: + shrunkIndexExists = shrunkIndexExists == false; + break; + case 1: + actualShards += between(1, 20); + break; + case 2: + allShardsActive = allShardsActive == false; + break; + default: + throw new AssertionError("Illegal randomisation branch"); } return new Info(shrunkIndexExists, actualShards, allShardsActive); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrunkShardsAllocatedStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrunkShardsAllocatedStepTests.java index ec0ed77dd816e..74f74996bd7da 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrunkShardsAllocatedStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrunkShardsAllocatedStepTests.java @@ -67,11 +67,13 @@ public void testConditionMet() { IndexMetadata originalIndexMetadata = IndexMetadata.builder(originalIndexName) .settings(settings(Version.CURRENT)) .numberOfShards(originalNumberOfShards) - .numberOfReplicas(0).build(); + .numberOfReplicas(0) + .build(); IndexMetadata shrunkIndexMetadata = IndexMetadata.builder(SHRUNKEN_INDEX_PREFIX + originalIndexName) - .settings(settings(Version.CURRENT)) - .numberOfShards(shrinkNumberOfShards) - .numberOfReplicas(0).build(); + .settings(settings(Version.CURRENT)) + .numberOfShards(shrinkNumberOfShards) + .numberOfReplicas(0) + .build(); Metadata metadata = Metadata.builder() .persistentSettings(settings(Version.CURRENT).build()) .put(IndexMetadata.builder(originalIndexMetadata)) @@ -83,17 +85,18 @@ public void testConditionMet() { DiscoveryNode masterNode = DiscoveryNode.createLocal( NodeRoles.masterNode(settings(Version.CURRENT).build()), new TransportAddress(TransportAddress.META_ADDRESS, 9300), - nodeId); + nodeId + ); IndexRoutingTable.Builder builder = IndexRoutingTable.builder(shrinkIndex); for (int i = 0; i < shrinkNumberOfShards; i++) { - builder.addShard(TestShardRouting.newShardRouting(new ShardId(shrinkIndex, i), - nodeId, true, ShardRoutingState.STARTED)); + builder.addShard(TestShardRouting.newShardRouting(new ShardId(shrinkIndex, i), nodeId, true, ShardRoutingState.STARTED)); } ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) .metadata(metadata) .nodes(DiscoveryNodes.builder().localNodeId(nodeId).masterNodeId(nodeId).add(masterNode).build()) - .routingTable(RoutingTable.builder().add(builder.build()).build()).build(); + .routingTable(RoutingTable.builder().add(builder.build()).build()) + .build(); Result result = step.isConditionMet(originalIndexMetadata.getIndex(), clusterState); assertTrue(result.isComplete()); @@ -108,11 +111,13 @@ public void testConditionNotMetBecauseOfActive() { IndexMetadata originalIndexMetadata = IndexMetadata.builder(originalIndexName) .settings(settings(Version.CURRENT)) .numberOfShards(originalNumberOfShards) - .numberOfReplicas(0).build(); + .numberOfReplicas(0) + .build(); IndexMetadata shrunkIndexMetadata = IndexMetadata.builder(SHRUNKEN_INDEX_PREFIX + originalIndexName) - .settings(settings(Version.CURRENT)) - .numberOfShards(shrinkNumberOfShards) - .numberOfReplicas(0).build(); + .settings(settings(Version.CURRENT)) + .numberOfShards(shrinkNumberOfShards) + .numberOfReplicas(0) + .build(); Metadata metadata = Metadata.builder() .persistentSettings(settings(Version.CURRENT).build()) .put(IndexMetadata.builder(originalIndexMetadata)) @@ -124,22 +129,22 @@ public void testConditionNotMetBecauseOfActive() { DiscoveryNode masterNode = DiscoveryNode.createLocal( NodeRoles.masterNode(settings(Version.CURRENT).build()), new TransportAddress(TransportAddress.META_ADDRESS, 9300), - nodeId); + nodeId + ); IndexRoutingTable.Builder builder = IndexRoutingTable.builder(shrinkIndex); for (int i = 0; i < shrinkNumberOfShards; i++) { - builder.addShard(TestShardRouting.newShardRouting(new ShardId(shrinkIndex, i), - nodeId, true, ShardRoutingState.INITIALIZING)); + builder.addShard(TestShardRouting.newShardRouting(new ShardId(shrinkIndex, i), nodeId, true, ShardRoutingState.INITIALIZING)); } ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) .metadata(metadata) .nodes(DiscoveryNodes.builder().localNodeId(nodeId).masterNodeId(nodeId).add(masterNode).build()) - .routingTable(RoutingTable.builder().add(builder.build()).build()).build(); + .routingTable(RoutingTable.builder().add(builder.build()).build()) + .build(); Result result = step.isConditionMet(originalIndexMetadata.getIndex(), clusterState); assertFalse(result.isComplete()); - assertEquals(new ShrunkShardsAllocatedStep.Info(true, shrinkNumberOfShards, false), - result.getInfomationContext()); + assertEquals(new ShrunkShardsAllocatedStep.Info(true, shrinkNumberOfShards, false), result.getInfomationContext()); } public void testConditionNotMetBecauseOfShrunkIndexDoesntExistYet() { @@ -149,7 +154,8 @@ public void testConditionNotMetBecauseOfShrunkIndexDoesntExistYet() { IndexMetadata originalIndexMetadata = IndexMetadata.builder(originalIndexName) .settings(settings(Version.CURRENT)) .numberOfShards(originalNumberOfShards) - .numberOfReplicas(0).build(); + .numberOfReplicas(0) + .build(); Metadata metadata = Metadata.builder() .persistentSettings(settings(Version.CURRENT).build()) .put(IndexMetadata.builder(originalIndexMetadata)) @@ -159,7 +165,8 @@ public void testConditionNotMetBecauseOfShrunkIndexDoesntExistYet() { DiscoveryNode masterNode = DiscoveryNode.createLocal( NodeRoles.masterNode(settings(Version.CURRENT).build()), new TransportAddress(TransportAddress.META_ADDRESS, 9300), - nodeId); + nodeId + ); ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) .metadata(metadata) .nodes(DiscoveryNodes.builder().localNodeId(nodeId).masterNodeId(nodeId).add(masterNode).build()) diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrunkenIndexCheckStepInfoTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrunkenIndexCheckStepInfoTests.java index d8b9b76637528..f21fc3f1f1502 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrunkenIndexCheckStepInfoTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrunkenIndexCheckStepInfoTests.java @@ -7,9 +7,9 @@ package org.elasticsearch.xpack.core.ilm; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.test.EqualsHashCodeTestUtils; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ilm.ShrunkenIndexCheckStep.Info; import java.io.IOException; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrunkenIndexCheckStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrunkenIndexCheckStepTests.java index 16ae32526204e..6ab24d9182363 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrunkenIndexCheckStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrunkenIndexCheckStepTests.java @@ -32,14 +32,14 @@ public ShrunkenIndexCheckStep mutateInstance(ShrunkenIndexCheckStep instance) { StepKey key = instance.getKey(); StepKey nextKey = instance.getNextStepKey(); switch (between(0, 1)) { - case 0: - key = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); - break; - case 1: - nextKey = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); - break; - default: - throw new AssertionError("Illegal randomisation branch"); + case 0: + key = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); + break; + case 1: + nextKey = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); + break; + default: + throw new AssertionError("Illegal randomisation branch"); } return new ShrunkenIndexCheckStep(key, nextKey); } @@ -55,7 +55,8 @@ public void testConditionMet() { IndexMetadata indexMetadata = IndexMetadata.builder(SHRUNKEN_INDEX_PREFIX + sourceIndex) .settings(settings(Version.CURRENT).put(IndexMetadata.INDEX_RESIZE_SOURCE_NAME_KEY, sourceIndex)) .numberOfShards(1) - .numberOfReplicas(0).build(); + .numberOfReplicas(0) + .build(); Metadata metadata = Metadata.builder() .persistentSettings(settings(Version.CURRENT).build()) .put(IndexMetadata.builder(indexMetadata)) @@ -73,7 +74,8 @@ public void testConditionNotMetBecauseNotSameShrunkenIndex() { IndexMetadata shrinkIndexMetadata = IndexMetadata.builder(sourceIndex + "hello") .settings(settings(Version.CURRENT).put(IndexMetadata.INDEX_RESIZE_SOURCE_NAME_KEY, sourceIndex)) .numberOfShards(1) - .numberOfReplicas(0).build(); + .numberOfReplicas(0) + .build(); Metadata metadata = Metadata.builder() .persistentSettings(settings(Version.CURRENT).build()) .put(IndexMetadata.builder(shrinkIndexMetadata)) @@ -90,11 +92,13 @@ public void testConditionNotMetBecauseSourceIndexExists() { IndexMetadata originalIndexMetadata = IndexMetadata.builder(sourceIndex) .settings(settings(Version.CURRENT)) .numberOfShards(100) - .numberOfReplicas(0).build(); + .numberOfReplicas(0) + .build(); IndexMetadata shrinkIndexMetadata = IndexMetadata.builder(SHRUNKEN_INDEX_PREFIX + sourceIndex) .settings(settings(Version.CURRENT).put(IndexMetadata.INDEX_RESIZE_SOURCE_NAME_KEY, sourceIndex)) .numberOfShards(1) - .numberOfReplicas(0).build(); + .numberOfReplicas(0) + .build(); Metadata metadata = Metadata.builder() .persistentSettings(settings(Version.CURRENT).build()) .put(IndexMetadata.builder(originalIndexMetadata)) @@ -111,15 +115,20 @@ public void testIllegalState() { IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(5)) .settings(settings(Version.CURRENT)) .numberOfShards(1) - .numberOfReplicas(0).build(); + .numberOfReplicas(0) + .build(); Metadata metadata = Metadata.builder() .persistentSettings(settings(Version.CURRENT).build()) .put(IndexMetadata.builder(indexMetadata)) .build(); ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metadata(metadata).build(); - IllegalStateException exception = expectThrows(IllegalStateException.class, - () -> step.isConditionMet(indexMetadata.getIndex(), clusterState)); - assertThat(exception.getMessage(), - equalTo("step[is-shrunken-index] is checking an un-shrunken index[" + indexMetadata.getIndex().getName() + "]")); + IllegalStateException exception = expectThrows( + IllegalStateException.class, + () -> step.isConditionMet(indexMetadata.getIndex(), clusterState) + ); + assertThat( + exception.getMessage(), + equalTo("step[is-shrunken-index] is checking an un-shrunken index[" + indexMetadata.getIndex().getName() + "]") + ); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/StepKeyTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/StepKeyTests.java index 84ee38d342e39..ae2b0b0d4de44 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/StepKeyTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/StepKeyTests.java @@ -6,10 +6,9 @@ */ package org.elasticsearch.xpack.core.ilm; - import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ilm.Step.StepKey; public class StepKeyTests extends AbstractSerializingTestCase { @@ -40,17 +39,17 @@ public StepKey mutateInstance(StepKey instance) { String step = instance.getName(); switch (between(0, 2)) { - case 0: - phase += randomAlphaOfLength(5); - break; - case 1: - action += randomAlphaOfLength(5); - break; - case 2: - step += randomAlphaOfLength(5); - break; - default: - throw new AssertionError("Illegal randomisation branch"); + case 0: + phase += randomAlphaOfLength(5); + break; + case 1: + action += randomAlphaOfLength(5); + break; + case 2: + step += randomAlphaOfLength(5); + break; + default: + throw new AssertionError("Illegal randomisation branch"); } return new StepKey(phase, action, step); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SwapAliasesAndDeleteSourceIndexStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SwapAliasesAndDeleteSourceIndexStepTests.java index d2af828084591..59587fcb3ca0e 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SwapAliasesAndDeleteSourceIndexStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SwapAliasesAndDeleteSourceIndexStepTests.java @@ -39,8 +39,12 @@ public SwapAliasesAndDeleteSourceIndexStep createRandomInstance() { @Override protected SwapAliasesAndDeleteSourceIndexStep copyInstance(SwapAliasesAndDeleteSourceIndexStep instance) { - return new SwapAliasesAndDeleteSourceIndexStep(instance.getKey(), instance.getNextStepKey(), instance.getClient(), - instance.getTargetIndexPrefix()); + return new SwapAliasesAndDeleteSourceIndexStep( + instance.getKey(), + instance.getNextStepKey(), + instance.getClient(), + instance.getTargetIndexPrefix() + ); } @Override @@ -66,8 +70,10 @@ public SwapAliasesAndDeleteSourceIndexStep mutateInstance(SwapAliasesAndDeleteSo public void testPerformAction() { String sourceIndexName = randomAlphaOfLength(10); - IndexMetadata.Builder sourceIndexMetadataBuilder = IndexMetadata.builder(sourceIndexName).settings(settings(Version.CURRENT)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)); + IndexMetadata.Builder sourceIndexMetadataBuilder = IndexMetadata.builder(sourceIndexName) + .settings(settings(Version.CURRENT)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)); AliasMetadata.Builder aliasBuilder = AliasMetadata.builder(randomAlphaOfLengthBetween(3, 10)); if (randomBoolean()) { aliasBuilder.routing(randomAlphaOfLengthBetween(1, 10)); @@ -88,33 +94,37 @@ public void testPerformAction() { List expectedAliasActions = Arrays.asList( AliasActions.removeIndex().index(sourceIndexName), AliasActions.add().index(targetIndexName).alias(sourceIndexName), - AliasActions.add().index(targetIndexName).alias(aliasMetadata.alias()) - .searchRouting(aliasMetadata.searchRouting()).indexRouting(aliasMetadata.indexRouting()) - .writeIndex(null)); + AliasActions.add() + .index(targetIndexName) + .alias(aliasMetadata.alias()) + .searchRouting(aliasMetadata.searchRouting()) + .indexRouting(aliasMetadata.indexRouting()) + .writeIndex(null) + ); try (NoOpClient client = getIndicesAliasAssertingClient(expectedAliasActions)) { - SwapAliasesAndDeleteSourceIndexStep step = new SwapAliasesAndDeleteSourceIndexStep(randomStepKey(), randomStepKey(), - client, targetIndexPrefix); - - IndexMetadata.Builder targetIndexMetadataBuilder = IndexMetadata.builder(targetIndexName).settings(settings(Version.CURRENT)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)); + SwapAliasesAndDeleteSourceIndexStep step = new SwapAliasesAndDeleteSourceIndexStep( + randomStepKey(), + randomStepKey(), + client, + targetIndexPrefix + ); + + IndexMetadata.Builder targetIndexMetadataBuilder = IndexMetadata.builder(targetIndexName) + .settings(settings(Version.CURRENT)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)); ClusterState clusterState = ClusterState.builder(emptyClusterState()) - .metadata( - Metadata.builder() - .put(sourceIndexMetadata, true) - .put(targetIndexMetadataBuilder) - .build() - ).build(); + .metadata(Metadata.builder().put(sourceIndexMetadata, true).put(targetIndexMetadataBuilder).build()) + .build(); step.performAction(sourceIndexMetadata, clusterState, null, new ActionListener<>() { @Override - public void onResponse(Void complete) { - } + public void onResponse(Void complete) {} @Override - public void onFailure(Exception e) { - } + public void onFailure(Exception e) {} }); } } @@ -122,9 +132,11 @@ public void onFailure(Exception e) { private NoOpClient getIndicesAliasAssertingClient(List expectedAliasActions) { return new NoOpClient(getTestName()) { @Override - protected void doExecute(ActionType action, - Request request, - ActionListener listener) { + protected void doExecute( + ActionType action, + Request request, + ActionListener listener + ) { assertThat(action.name(), is(IndicesAliasesAction.NAME)); assertTrue(request instanceof IndicesAliasesRequest); assertThat(((IndicesAliasesRequest) request).getAliasActions(), equalTo(expectedAliasActions)); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/TerminalPolicyStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/TerminalPolicyStepTests.java index 7262caa543a01..c0c47836928a1 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/TerminalPolicyStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/TerminalPolicyStepTests.java @@ -35,6 +35,7 @@ public TerminalPolicyStep mutateInstance(TerminalPolicyStep instance) { public TerminalPolicyStep copyInstance(TerminalPolicyStep instance) { return new TerminalPolicyStep(instance.getKey(), instance.getNextStepKey()); } + public void testInstance() { assertEquals(new Step.StepKey("completed", "completed", "completed"), TerminalPolicyStep.INSTANCE.getKey()); assertNull(TerminalPolicyStep.INSTANCE.getNextStepKey()); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/TestLifecycleType.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/TestLifecycleType.java index 4a7f8b05ed63d..c8c3fb867810f 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/TestLifecycleType.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/TestLifecycleType.java @@ -20,12 +20,10 @@ public class TestLifecycleType implements LifecycleType { public static final String TYPE = "test"; - private TestLifecycleType() { - } + private TestLifecycleType() {} @Override - public void writeTo(StreamOutput out) throws IOException { - } + public void writeTo(StreamOutput out) throws IOException {} @Override public String getWriteableName() { @@ -75,12 +73,14 @@ public List getOrderedActions(Phase phase) { @Override public String getNextActionName(String currentActionName, Phase phase) { - List orderedActionNames = getOrderedActions(phase).stream().map(LifecycleAction::getWriteableName) - .collect(Collectors.toList()); + List orderedActionNames = getOrderedActions(phase).stream() + .map(LifecycleAction::getWriteableName) + .collect(Collectors.toList()); int index = orderedActionNames.indexOf(currentActionName); if (index < 0) { - throw new IllegalArgumentException("[" + currentActionName + "] is not a valid action for phase [" + phase.getName() - + "] in lifecycle type [" + TYPE + "]"); + throw new IllegalArgumentException( + "[" + currentActionName + "] is not a valid action for phase [" + phase.getName() + "] in lifecycle type [" + TYPE + "]" + ); } else if (index == orderedActionNames.size() - 1) { return null; } else { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/TimeseriesLifecycleTypeTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/TimeseriesLifecycleTypeTests.java index 5abfdc3914fff..e76dad1db7312 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/TimeseriesLifecycleTypeTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/TimeseriesLifecycleTypeTests.java @@ -36,16 +36,16 @@ import static org.elasticsearch.xpack.core.ilm.TimeseriesLifecycleType.ORDERED_VALID_COLD_ACTIONS; import static org.elasticsearch.xpack.core.ilm.TimeseriesLifecycleType.ORDERED_VALID_DELETE_ACTIONS; import static org.elasticsearch.xpack.core.ilm.TimeseriesLifecycleType.ORDERED_VALID_HOT_ACTIONS; +import static org.elasticsearch.xpack.core.ilm.TimeseriesLifecycleType.ORDERED_VALID_PHASES; import static org.elasticsearch.xpack.core.ilm.TimeseriesLifecycleType.ORDERED_VALID_WARM_ACTIONS; import static org.elasticsearch.xpack.core.ilm.TimeseriesLifecycleType.VALID_COLD_ACTIONS; import static org.elasticsearch.xpack.core.ilm.TimeseriesLifecycleType.VALID_DELETE_ACTIONS; import static org.elasticsearch.xpack.core.ilm.TimeseriesLifecycleType.VALID_HOT_ACTIONS; -import static org.elasticsearch.xpack.core.ilm.TimeseriesLifecycleType.ORDERED_VALID_PHASES; import static org.elasticsearch.xpack.core.ilm.TimeseriesLifecycleType.VALID_WARM_ACTIONS; import static org.elasticsearch.xpack.core.ilm.TimeseriesLifecycleType.WARM_PHASE; import static org.elasticsearch.xpack.core.ilm.TimeseriesLifecycleType.validateAllSearchableSnapshotActionsUseSameRepository; -import static org.elasticsearch.xpack.core.ilm.TimeseriesLifecycleType.validateMonotonicallyIncreasingPhaseTimings; import static org.elasticsearch.xpack.core.ilm.TimeseriesLifecycleType.validateFrozenPhaseHasSearchableSnapshotAction; +import static org.elasticsearch.xpack.core.ilm.TimeseriesLifecycleType.validateMonotonicallyIncreasingPhaseTimings; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; @@ -54,8 +54,13 @@ public class TimeseriesLifecycleTypeTests extends ESTestCase { - private static final AllocateAction TEST_ALLOCATE_ACTION = - new AllocateAction(2, 20, Collections.singletonMap("node", "node1"),null, null); + private static final AllocateAction TEST_ALLOCATE_ACTION = new AllocateAction( + 2, + 20, + Collections.singletonMap("node", "node1"), + null, + null + ); private static final DeleteAction TEST_DELETE_ACTION = new DeleteAction(); private static final WaitForSnapshotAction TEST_WAIT_FOR_SNAPSHOT_ACTION = new WaitForSnapshotAction("policy"); private static final ForceMergeAction TEST_FORCE_MERGE_ACTION = new ForceMergeAction(1, null); @@ -64,14 +69,18 @@ public class TimeseriesLifecycleTypeTests extends ESTestCase { private static final ReadOnlyAction TEST_READ_ONLY_ACTION = new ReadOnlyAction(); private static final FreezeAction TEST_FREEZE_ACTION = new FreezeAction(); private static final SetPriorityAction TEST_PRIORITY_ACTION = new SetPriorityAction(0); - private static final UnfollowAction TEST_UNFOLLOW_ACTION = new UnfollowAction(); + private static final UnfollowAction TEST_UNFOLLOW_ACTION = new UnfollowAction(); private static final SearchableSnapshotAction TEST_SEARCHABLE_SNAPSHOT_ACTION = new SearchableSnapshotAction("repo"); // keeping the migrate action disabled as otherwise it could conflict with the allocate action if both are randomly selected for the // same phase private static final MigrateAction TEST_MIGRATE_ACTION = new MigrateAction(false); - private static final RollupILMAction TEST_ROLLUP_ACTION =new RollupILMAction(new RollupActionConfig( - new RollupActionGroupConfig(new RollupActionDateHistogramGroupConfig.FixedInterval("field", DateHistogramInterval.DAY)), - Collections.singletonList(new MetricConfig("field", Collections.singletonList("max")))), null); + private static final RollupILMAction TEST_ROLLUP_ACTION = new RollupILMAction( + new RollupActionConfig( + new RollupActionGroupConfig(new RollupActionDateHistogramGroupConfig.FixedInterval("field", DateHistogramInterval.DAY)), + Collections.singletonList(new MetricConfig("field", Collections.singletonList("max"))) + ), + null + ); public void testValidatePhases() { boolean invalid = randomBoolean(); @@ -79,8 +88,7 @@ public void testValidatePhases() { if (invalid) { phaseName += randomAlphaOfLength(5); } - Map phases = Collections.singletonMap(phaseName, - new Phase(phaseName, TimeValue.ZERO, Collections.emptyMap())); + Map phases = Collections.singletonMap(phaseName, new Phase(phaseName, TimeValue.ZERO, Collections.emptyMap())); if (invalid) { Exception e = expectThrows(IllegalArgumentException.class, () -> TimeseriesLifecycleType.INSTANCE.validate(phases.values())); assertThat(e.getMessage(), equalTo("Timeseries lifecycle does not support phase [" + phaseName + "]")); @@ -91,20 +99,18 @@ public void testValidatePhases() { public void testValidateHotPhase() { LifecycleAction invalidAction = null; - Map actions = VALID_HOT_ACTIONS - .stream().map(this::getTestAction).collect(Collectors.toMap(LifecycleAction::getWriteableName, Function.identity())); + Map actions = VALID_HOT_ACTIONS.stream() + .map(this::getTestAction) + .collect(Collectors.toMap(LifecycleAction::getWriteableName, Function.identity())); if (randomBoolean()) { invalidAction = getTestAction(randomFrom("allocate", "delete", "freeze")); actions.put(invalidAction.getWriteableName(), invalidAction); } - Map hotPhase = Collections.singletonMap("hot", - new Phase("hot", TimeValue.ZERO, actions)); + Map hotPhase = Collections.singletonMap("hot", new Phase("hot", TimeValue.ZERO, actions)); if (invalidAction != null) { - Exception e = expectThrows(IllegalArgumentException.class, - () -> TimeseriesLifecycleType.INSTANCE.validate(hotPhase.values())); - assertThat(e.getMessage(), - equalTo("invalid action [" + invalidAction.getWriteableName() + "] defined in phase [hot]")); + Exception e = expectThrows(IllegalArgumentException.class, () -> TimeseriesLifecycleType.INSTANCE.validate(hotPhase.values())); + assertThat(e.getMessage(), equalTo("invalid action [" + invalidAction.getWriteableName() + "] defined in phase [hot]")); } else { TimeseriesLifecycleType.INSTANCE.validate(hotPhase.values()); } @@ -119,29 +125,31 @@ public void testValidateHotPhase() { validateHotActions.accept(Arrays.asList(RolloverAction.NAME)); validateHotActions.accept(Arrays.asList(RolloverAction.NAME, ForceMergeAction.NAME)); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> validateHotActions.accept(Arrays.asList(ForceMergeAction.NAME))); - assertThat(e.getMessage(), - containsString("the [forcemerge] action(s) may not be used in the [hot] phase without an accompanying [rollover] action")); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> validateHotActions.accept(Arrays.asList(ForceMergeAction.NAME)) + ); + assertThat( + e.getMessage(), + containsString("the [forcemerge] action(s) may not be used in the [hot] phase without an accompanying [rollover] action") + ); } } public void testValidateWarmPhase() { LifecycleAction invalidAction = null; - Map actions = randomSubsetOf(VALID_WARM_ACTIONS) - .stream().map(this::getTestAction).collect(Collectors.toMap(LifecycleAction::getWriteableName, Function.identity())); + Map actions = randomSubsetOf(VALID_WARM_ACTIONS).stream() + .map(this::getTestAction) + .collect(Collectors.toMap(LifecycleAction::getWriteableName, Function.identity())); if (randomBoolean()) { invalidAction = getTestAction(randomFrom("rollover", "delete", "freeze")); actions.put(invalidAction.getWriteableName(), invalidAction); } - Map warmPhase = Collections.singletonMap("warm", - new Phase("warm", TimeValue.ZERO, actions)); + Map warmPhase = Collections.singletonMap("warm", new Phase("warm", TimeValue.ZERO, actions)); if (invalidAction != null) { - Exception e = expectThrows(IllegalArgumentException.class, - () -> TimeseriesLifecycleType.INSTANCE.validate(warmPhase.values())); - assertThat(e.getMessage(), - equalTo("invalid action [" + invalidAction.getWriteableName() + "] defined in phase [warm]")); + Exception e = expectThrows(IllegalArgumentException.class, () -> TimeseriesLifecycleType.INSTANCE.validate(warmPhase.values())); + assertThat(e.getMessage(), equalTo("invalid action [" + invalidAction.getWriteableName() + "] defined in phase [warm]")); } else { TimeseriesLifecycleType.INSTANCE.validate(warmPhase.values()); } @@ -149,20 +157,18 @@ public void testValidateWarmPhase() { public void testValidateColdPhase() { LifecycleAction invalidAction = null; - Map actions = randomSubsetOf(VALID_COLD_ACTIONS) - .stream().map(this::getTestAction).collect(Collectors.toMap(LifecycleAction::getWriteableName, Function.identity())); + Map actions = randomSubsetOf(VALID_COLD_ACTIONS).stream() + .map(this::getTestAction) + .collect(Collectors.toMap(LifecycleAction::getWriteableName, Function.identity())); if (randomBoolean()) { invalidAction = getTestAction(randomFrom("rollover", "delete", "forcemerge", "shrink")); actions.put(invalidAction.getWriteableName(), invalidAction); } - Map coldPhase = Collections.singletonMap("cold", - new Phase("cold", TimeValue.ZERO, actions)); + Map coldPhase = Collections.singletonMap("cold", new Phase("cold", TimeValue.ZERO, actions)); if (invalidAction != null) { - Exception e = expectThrows(IllegalArgumentException.class, - () -> TimeseriesLifecycleType.INSTANCE.validate(coldPhase.values())); - assertThat(e.getMessage(), - equalTo("invalid action [" + invalidAction.getWriteableName() + "] defined in phase [cold]")); + Exception e = expectThrows(IllegalArgumentException.class, () -> TimeseriesLifecycleType.INSTANCE.validate(coldPhase.values())); + assertThat(e.getMessage(), equalTo("invalid action [" + invalidAction.getWriteableName() + "] defined in phase [cold]")); } else { TimeseriesLifecycleType.INSTANCE.validate(coldPhase.values()); } @@ -170,20 +176,21 @@ public void testValidateColdPhase() { public void testValidateDeletePhase() { LifecycleAction invalidAction = null; - Map actions = VALID_DELETE_ACTIONS - .stream().map(this::getTestAction).collect(Collectors.toMap(LifecycleAction::getWriteableName, Function.identity())); + Map actions = VALID_DELETE_ACTIONS.stream() + .map(this::getTestAction) + .collect(Collectors.toMap(LifecycleAction::getWriteableName, Function.identity())); if (randomBoolean()) { invalidAction = getTestAction(randomFrom("allocate", "rollover", "forcemerge", "shrink", "freeze", "set_priority")); actions.put(invalidAction.getWriteableName(), invalidAction); } - Map deletePhase = Collections.singletonMap("delete", - new Phase("delete", TimeValue.ZERO, actions)); + Map deletePhase = Collections.singletonMap("delete", new Phase("delete", TimeValue.ZERO, actions)); if (invalidAction != null) { - Exception e = expectThrows(IllegalArgumentException.class, - () -> TimeseriesLifecycleType.INSTANCE.validate(deletePhase.values())); - assertThat(e.getMessage(), - equalTo("invalid action [" + invalidAction.getWriteableName() + "] defined in phase [delete]")); + Exception e = expectThrows( + IllegalArgumentException.class, + () -> TimeseriesLifecycleType.INSTANCE.validate(deletePhase.values()) + ); + assertThat(e.getMessage(), equalTo("invalid action [" + invalidAction.getWriteableName() + "] defined in phase [delete]")); } else { TimeseriesLifecycleType.INSTANCE.validate(deletePhase.values()); } @@ -195,10 +202,17 @@ public void testValidateConflictingDataMigrationConfigurations() { actions.put(TEST_ALLOCATE_ACTION.getWriteableName(), TEST_ALLOCATE_ACTION); List phases = List.of(new Phase(WARM_PHASE, TimeValue.ZERO, actions), new Phase(COLD_PHASE, TimeValue.ZERO, actions)); - Exception validationException = expectThrows(IllegalArgumentException.class, - () -> TimeseriesLifecycleType.INSTANCE.validate(phases)); - assertThat(validationException.getMessage(), equalTo("phases [warm,cold] specify an enabled migrate action and an allocate " + - "action with allocation rules. specify only a single data migration in each phase")); + Exception validationException = expectThrows( + IllegalArgumentException.class, + () -> TimeseriesLifecycleType.INSTANCE.validate(phases) + ); + assertThat( + validationException.getMessage(), + equalTo( + "phases [warm,cold] specify an enabled migrate action and an allocate " + + "action with allocation rules. specify only a single data migration in each phase" + ) + ); // disabling the migrate action makes the phases definition valid as only the allocate action will perform data migration actions.put(TEST_MIGRATE_ACTION.getWriteableName(), new MigrateAction(false)); @@ -211,8 +225,10 @@ public void testValidateConflictingDataMigrationConfigurations() { public void testActionsThatCannotFollowSearchableSnapshot() { assertThat(ACTIONS_CANNOT_FOLLOW_SEARCHABLE_SNAPSHOT.size(), is(4)); - assertThat(ACTIONS_CANNOT_FOLLOW_SEARCHABLE_SNAPSHOT, containsInAnyOrder(ShrinkAction.NAME, FreezeAction.NAME, - ForceMergeAction.NAME, RollupILMAction.NAME)); + assertThat( + ACTIONS_CANNOT_FOLLOW_SEARCHABLE_SNAPSHOT, + containsInAnyOrder(ShrinkAction.NAME, FreezeAction.NAME, ForceMergeAction.NAME, RollupILMAction.NAME) + ); } public void testValidateActionsFollowingSearchableSnapshot() { @@ -220,63 +236,100 @@ public void testValidateActionsFollowingSearchableSnapshot() { Phase hotPhase = new Phase("hot", TimeValue.ZERO, Map.of(SearchableSnapshotAction.NAME, new SearchableSnapshotAction("repo"))); Phase warmPhase = new Phase("warm", TimeValue.ZERO, Map.of(ShrinkAction.NAME, new ShrinkAction(1, null))); Phase coldPhase = new Phase("cold", TimeValue.ZERO, Map.of(FreezeAction.NAME, new FreezeAction())); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> TimeseriesLifecycleType.validateActionsFollowingSearchableSnapshot(List.of(hotPhase, warmPhase, coldPhase))); - assertThat(e.getMessage(), is( - "phases [warm,cold] define one or more of [forcemerge, freeze, shrink, rollup] actions" + - " which are not allowed after a managed index is mounted as a searchable snapshot")); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> TimeseriesLifecycleType.validateActionsFollowingSearchableSnapshot(List.of(hotPhase, warmPhase, coldPhase)) + ); + assertThat( + e.getMessage(), + is( + "phases [warm,cold] define one or more of [forcemerge, freeze, shrink, rollup] actions" + + " which are not allowed after a managed index is mounted as a searchable snapshot" + ) + ); } { - Phase warmPhase = new Phase("warm", TimeValue.ZERO, - Map.of(ShrinkAction.NAME, new ShrinkAction(1, null))); - Phase coldPhase = new Phase("cold", TimeValue.ZERO, - Map.of(SearchableSnapshotAction.NAME, new SearchableSnapshotAction("repo"))); - Phase frozenPhase = new Phase("frozen", TimeValue.ZERO, - Map.of(FreezeAction.NAME, new FreezeAction())); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> TimeseriesLifecycleType.validateActionsFollowingSearchableSnapshot(List.of(warmPhase, coldPhase, frozenPhase))); - assertThat(e.getMessage(), is( - "phases [frozen] define one or more of [forcemerge, freeze, shrink, rollup] actions" + - " which are not allowed after a managed index is mounted as a searchable snapshot")); + Phase warmPhase = new Phase("warm", TimeValue.ZERO, Map.of(ShrinkAction.NAME, new ShrinkAction(1, null))); + Phase coldPhase = new Phase( + "cold", + TimeValue.ZERO, + Map.of(SearchableSnapshotAction.NAME, new SearchableSnapshotAction("repo")) + ); + Phase frozenPhase = new Phase("frozen", TimeValue.ZERO, Map.of(FreezeAction.NAME, new FreezeAction())); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> TimeseriesLifecycleType.validateActionsFollowingSearchableSnapshot(List.of(warmPhase, coldPhase, frozenPhase)) + ); + assertThat( + e.getMessage(), + is( + "phases [frozen] define one or more of [forcemerge, freeze, shrink, rollup] actions" + + " which are not allowed after a managed index is mounted as a searchable snapshot" + ) + ); } { - Phase hotPhase = new Phase("hot", TimeValue.ZERO, - Map.of(SearchableSnapshotAction.NAME, new SearchableSnapshotAction("repo"))); - Phase warmPhase = new Phase("warm", TimeValue.ZERO, - Map.of(ShrinkAction.NAME, new ShrinkAction(1, null))); - Phase coldPhase = new Phase("cold", TimeValue.ZERO, - Map.of(SearchableSnapshotAction.NAME, new SearchableSnapshotAction("repo"))); - Phase frozenPhase = new Phase("frozen", TimeValue.ZERO, - Map.of(FreezeAction.NAME, new FreezeAction())); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> TimeseriesLifecycleType.validateActionsFollowingSearchableSnapshot(List.of(hotPhase, warmPhase, coldPhase, - frozenPhase))); - assertThat(e.getMessage(), is( - "phases [warm,frozen] define one or more of [forcemerge, freeze, shrink, rollup] actions" + - " which are not allowed after a managed index is mounted as a searchable snapshot")); + Phase hotPhase = new Phase("hot", TimeValue.ZERO, Map.of(SearchableSnapshotAction.NAME, new SearchableSnapshotAction("repo"))); + Phase warmPhase = new Phase("warm", TimeValue.ZERO, Map.of(ShrinkAction.NAME, new ShrinkAction(1, null))); + Phase coldPhase = new Phase( + "cold", + TimeValue.ZERO, + Map.of(SearchableSnapshotAction.NAME, new SearchableSnapshotAction("repo")) + ); + Phase frozenPhase = new Phase("frozen", TimeValue.ZERO, Map.of(FreezeAction.NAME, new FreezeAction())); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> TimeseriesLifecycleType.validateActionsFollowingSearchableSnapshot( + List.of(hotPhase, warmPhase, coldPhase, frozenPhase) + ) + ); + assertThat( + e.getMessage(), + is( + "phases [warm,frozen] define one or more of [forcemerge, freeze, shrink, rollup] actions" + + " which are not allowed after a managed index is mounted as a searchable snapshot" + ) + ); } { - Phase hot = new Phase("hot", TimeValue.ZERO, Map.of(RolloverAction.NAME, new RolloverAction(null, null, null, 1L), - SearchableSnapshotAction.NAME, new SearchableSnapshotAction(randomAlphaOfLengthBetween(4, 10)))); + Phase hot = new Phase( + "hot", + TimeValue.ZERO, + Map.of( + RolloverAction.NAME, + new RolloverAction(null, null, null, 1L), + SearchableSnapshotAction.NAME, + new SearchableSnapshotAction(randomAlphaOfLengthBetween(4, 10)) + ) + ); Phase warm = new Phase("warm", TimeValue.ZERO, Map.of(ForceMergeAction.NAME, new ForceMergeAction(1, null))); Phase cold = new Phase("cold", TimeValue.ZERO, Map.of(FreezeAction.NAME, new FreezeAction())); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> TimeseriesLifecycleType.validateActionsFollowingSearchableSnapshot(List.of(warm, hot, cold))); - assertThat(e.getMessage(), is( - "phases [warm,cold] define one or more of [forcemerge, freeze, shrink, rollup] actions" + - " which are not allowed after a managed index is mounted as a searchable snapshot")); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> TimeseriesLifecycleType.validateActionsFollowingSearchableSnapshot(List.of(warm, hot, cold)) + ); + assertThat( + e.getMessage(), + is( + "phases [warm,cold] define one or more of [forcemerge, freeze, shrink, rollup] actions" + + " which are not allowed after a managed index is mounted as a searchable snapshot" + ) + ); } { - Phase frozenPhase = new Phase("frozen", TimeValue.ZERO, Map.of(FreezeAction.NAME, new FreezeAction(), - SearchableSnapshotAction.NAME, new SearchableSnapshotAction("repo"))); + Phase frozenPhase = new Phase( + "frozen", + TimeValue.ZERO, + Map.of(FreezeAction.NAME, new FreezeAction(), SearchableSnapshotAction.NAME, new SearchableSnapshotAction("repo")) + ); try { TimeseriesLifecycleType.validateActionsFollowingSearchableSnapshot(List.of(frozenPhase)); } catch (Exception e) { - fail("unexpected exception while validating phase [ "+ frozenPhase +" ] but got [" + e.getMessage()+ "]"); + fail("unexpected exception while validating phase [ " + frozenPhase + " ] but got [" + e.getMessage() + "]"); } } } @@ -287,7 +340,6 @@ public void testGetOrderedPhases() { phaseMap.put(phaseName, new Phase(phaseName, TimeValue.ZERO, Collections.emptyMap())); } - assertTrue(isSorted(TimeseriesLifecycleType.INSTANCE.getOrderedPhases(phaseMap), Phase::getName, ORDERED_VALID_PHASES)); } @@ -331,14 +383,17 @@ private boolean isUnfollowInjected(String phaseName, String actionName) { } public void testGetOrderedActionsInvalidPhase() { - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> TimeseriesLifecycleType.INSTANCE - .getOrderedActions(new Phase("invalid", TimeValue.ZERO, Collections.emptyMap()))); + IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> TimeseriesLifecycleType.INSTANCE.getOrderedActions(new Phase("invalid", TimeValue.ZERO, Collections.emptyMap())) + ); assertThat(exception.getMessage(), equalTo("lifecycle type [timeseries] does not support phase [invalid]")); } public void testGetOrderedActionsHot() { - Map actions = VALID_HOT_ACTIONS - .stream().map(this::getTestAction).collect(Collectors.toMap(LifecycleAction::getWriteableName, Function.identity())); + Map actions = VALID_HOT_ACTIONS.stream() + .map(this::getTestAction) + .collect(Collectors.toMap(LifecycleAction::getWriteableName, Function.identity())); Phase hotPhase = new Phase("hot", TimeValue.ZERO, actions); List orderedActions = TimeseriesLifecycleType.INSTANCE.getOrderedActions(hotPhase); assertTrue(isSorted(orderedActions, LifecycleAction::getWriteableName, ORDERED_VALID_HOT_ACTIONS)); @@ -346,8 +401,9 @@ public void testGetOrderedActionsHot() { } public void testGetOrderedActionsWarm() { - Map actions = VALID_WARM_ACTIONS - .stream().map(this::getTestAction).collect(Collectors.toMap(LifecycleAction::getWriteableName, Function.identity())); + Map actions = VALID_WARM_ACTIONS.stream() + .map(this::getTestAction) + .collect(Collectors.toMap(LifecycleAction::getWriteableName, Function.identity())); Phase warmPhase = new Phase("warm", TimeValue.ZERO, actions); List orderedActions = TimeseriesLifecycleType.INSTANCE.getOrderedActions(warmPhase); assertTrue(isSorted(orderedActions, LifecycleAction::getWriteableName, ORDERED_VALID_WARM_ACTIONS)); @@ -355,8 +411,9 @@ public void testGetOrderedActionsWarm() { } public void testGetOrderedActionsCold() { - Map actions = VALID_COLD_ACTIONS - .stream().map(this::getTestAction).collect(Collectors.toMap(LifecycleAction::getWriteableName, Function.identity())); + Map actions = VALID_COLD_ACTIONS.stream() + .map(this::getTestAction) + .collect(Collectors.toMap(LifecycleAction::getWriteableName, Function.identity())); Phase coldPhase = new Phase("cold", TimeValue.ZERO, actions); List orderedActions = TimeseriesLifecycleType.INSTANCE.getOrderedActions(coldPhase); assertTrue(isSorted(orderedActions, LifecycleAction::getWriteableName, ORDERED_VALID_COLD_ACTIONS)); @@ -364,8 +421,9 @@ public void testGetOrderedActionsCold() { } public void testGetOrderedActionsDelete() { - Map actions = VALID_DELETE_ACTIONS - .stream().map(this::getTestAction).collect(Collectors.toMap(LifecycleAction::getWriteableName, Function.identity())); + Map actions = VALID_DELETE_ACTIONS.stream() + .map(this::getTestAction) + .collect(Collectors.toMap(LifecycleAction::getWriteableName, Function.identity())); Phase deletePhase = new Phase("delete", TimeValue.ZERO, actions); List orderedActions = TimeseriesLifecycleType.INSTANCE.getOrderedActions(deletePhase); assertTrue(isSorted(orderedActions, LifecycleAction::getWriteableName, ORDERED_VALID_DELETE_ACTIONS)); @@ -374,7 +432,7 @@ public void testGetOrderedActionsDelete() { public void testGetNextPhaseName() { assertNextPhaseName("hot", "warm", new String[] { "hot", "warm" }); assertNextPhaseName("hot", "warm", new String[] { "hot", "warm", "cold" }); - assertNextPhaseName("hot", "warm", new String[] { "hot", "warm", "cold", "delete"}); + assertNextPhaseName("hot", "warm", new String[] { "hot", "warm", "cold", "delete" }); assertNextPhaseName("hot", "warm", new String[] { "warm", "cold", "delete" }); assertNextPhaseName("hot", "warm", new String[] { "warm", "cold", "delete" }); assertNextPhaseName("hot", "warm", new String[] { "warm", "delete" }); @@ -417,11 +475,18 @@ public void testGetNextPhaseName() { assertNextPhaseName("delete", null, new String[] { "delete" }); assertNextPhaseName("delete", null, new String[] {}); - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, - () -> TimeseriesLifecycleType.INSTANCE.getNextPhaseName("foo", Collections.emptyMap())); + IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> TimeseriesLifecycleType.INSTANCE.getNextPhaseName("foo", Collections.emptyMap()) + ); assertEquals("[foo] is not a valid phase for lifecycle type [" + TimeseriesLifecycleType.TYPE + "]", exception.getMessage()); - exception = expectThrows(IllegalArgumentException.class, () -> TimeseriesLifecycleType.INSTANCE - .getNextPhaseName("foo", Collections.singletonMap("foo", new Phase("foo", TimeValue.ZERO, Collections.emptyMap())))); + exception = expectThrows( + IllegalArgumentException.class, + () -> TimeseriesLifecycleType.INSTANCE.getNextPhaseName( + "foo", + Collections.singletonMap("foo", new Phase("foo", TimeValue.ZERO, Collections.emptyMap())) + ) + ); assertEquals("[foo] is not a valid phase for lifecycle type [" + TimeseriesLifecycleType.TYPE + "]", exception.getMessage()); } @@ -473,19 +538,25 @@ public void testGetPreviousPhaseName() { assertPreviousPhaseName("delete", "cold", new String[] { "cold" }); assertPreviousPhaseName("delete", null, new String[] {}); - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, - () -> TimeseriesLifecycleType.INSTANCE.getPreviousPhaseName("foo", Collections.emptyMap())); + IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> TimeseriesLifecycleType.INSTANCE.getPreviousPhaseName("foo", Collections.emptyMap()) + ); assertEquals("[foo] is not a valid phase for lifecycle type [" + TimeseriesLifecycleType.TYPE + "]", exception.getMessage()); - exception = expectThrows(IllegalArgumentException.class, () -> TimeseriesLifecycleType.INSTANCE - .getPreviousPhaseName("foo", Collections.singletonMap("foo", new Phase("foo", TimeValue.ZERO, Collections.emptyMap())))); + exception = expectThrows( + IllegalArgumentException.class, + () -> TimeseriesLifecycleType.INSTANCE.getPreviousPhaseName( + "foo", + Collections.singletonMap("foo", new Phase("foo", TimeValue.ZERO, Collections.emptyMap())) + ) + ); assertEquals("[foo] is not a valid phase for lifecycle type [" + TimeseriesLifecycleType.TYPE + "]", exception.getMessage()); } public void testGetNextActionName() { // Hot Phase - assertNextActionName("hot", SetPriorityAction.NAME, UnfollowAction.NAME, - new String[] {UnfollowAction.NAME, RolloverAction.NAME}); - assertNextActionName("hot", SetPriorityAction.NAME, RolloverAction.NAME, new String[]{RolloverAction.NAME}); + assertNextActionName("hot", SetPriorityAction.NAME, UnfollowAction.NAME, new String[] { UnfollowAction.NAME, RolloverAction.NAME }); + assertNextActionName("hot", SetPriorityAction.NAME, RolloverAction.NAME, new String[] { RolloverAction.NAME }); assertNextActionName("hot", SetPriorityAction.NAME, null, new String[] {}); assertNextActionName("hot", RolloverAction.NAME, null, new String[] {}); @@ -498,92 +569,183 @@ public void testGetNextActionName() { assertInvalidAction("hot", DeleteAction.NAME, new String[] { RolloverAction.NAME }); // Warm Phase - assertNextActionName("warm", SetPriorityAction.NAME, UnfollowAction.NAME, - new String[]{SetPriorityAction.NAME, UnfollowAction.NAME, ReadOnlyAction.NAME, AllocateAction.NAME, - ShrinkAction.NAME, ForceMergeAction.NAME}); - assertNextActionName("warm", SetPriorityAction.NAME, ReadOnlyAction.NAME, - new String[]{SetPriorityAction.NAME, ReadOnlyAction.NAME, AllocateAction.NAME, ShrinkAction.NAME, ForceMergeAction.NAME}); - assertNextActionName("warm", SetPriorityAction.NAME, AllocateAction.NAME, - new String[]{SetPriorityAction.NAME, AllocateAction.NAME, ShrinkAction.NAME, ForceMergeAction.NAME}); - assertNextActionName("warm", SetPriorityAction.NAME, ShrinkAction.NAME, - new String[]{SetPriorityAction.NAME, ShrinkAction.NAME, ForceMergeAction.NAME}); - assertNextActionName("warm", SetPriorityAction.NAME, ForceMergeAction.NAME, - new String[]{SetPriorityAction.NAME, ForceMergeAction.NAME}); - assertNextActionName("warm", SetPriorityAction.NAME, null, new String[]{SetPriorityAction.NAME}); - - assertNextActionName("warm", UnfollowAction.NAME, ReadOnlyAction.NAME, - new String[] { SetPriorityAction.NAME, ReadOnlyAction.NAME, AllocateAction.NAME, ShrinkAction.NAME, ForceMergeAction.NAME }); - assertNextActionName("warm", UnfollowAction.NAME, ReadOnlyAction.NAME, - new String[] { ReadOnlyAction.NAME, AllocateAction.NAME, ShrinkAction.NAME, ForceMergeAction.NAME }); - assertNextActionName("warm", UnfollowAction.NAME, AllocateAction.NAME, - new String[] { AllocateAction.NAME, ShrinkAction.NAME, ForceMergeAction.NAME }); - assertNextActionName("warm", UnfollowAction.NAME, ShrinkAction.NAME, - new String[] { ShrinkAction.NAME, ForceMergeAction.NAME }); + assertNextActionName( + "warm", + SetPriorityAction.NAME, + UnfollowAction.NAME, + new String[] { + SetPriorityAction.NAME, + UnfollowAction.NAME, + ReadOnlyAction.NAME, + AllocateAction.NAME, + ShrinkAction.NAME, + ForceMergeAction.NAME } + ); + assertNextActionName( + "warm", + SetPriorityAction.NAME, + ReadOnlyAction.NAME, + new String[] { SetPriorityAction.NAME, ReadOnlyAction.NAME, AllocateAction.NAME, ShrinkAction.NAME, ForceMergeAction.NAME } + ); + assertNextActionName( + "warm", + SetPriorityAction.NAME, + AllocateAction.NAME, + new String[] { SetPriorityAction.NAME, AllocateAction.NAME, ShrinkAction.NAME, ForceMergeAction.NAME } + ); + assertNextActionName( + "warm", + SetPriorityAction.NAME, + ShrinkAction.NAME, + new String[] { SetPriorityAction.NAME, ShrinkAction.NAME, ForceMergeAction.NAME } + ); + assertNextActionName( + "warm", + SetPriorityAction.NAME, + ForceMergeAction.NAME, + new String[] { SetPriorityAction.NAME, ForceMergeAction.NAME } + ); + assertNextActionName("warm", SetPriorityAction.NAME, null, new String[] { SetPriorityAction.NAME }); + + assertNextActionName( + "warm", + UnfollowAction.NAME, + ReadOnlyAction.NAME, + new String[] { SetPriorityAction.NAME, ReadOnlyAction.NAME, AllocateAction.NAME, ShrinkAction.NAME, ForceMergeAction.NAME } + ); + assertNextActionName( + "warm", + UnfollowAction.NAME, + ReadOnlyAction.NAME, + new String[] { ReadOnlyAction.NAME, AllocateAction.NAME, ShrinkAction.NAME, ForceMergeAction.NAME } + ); + assertNextActionName( + "warm", + UnfollowAction.NAME, + AllocateAction.NAME, + new String[] { AllocateAction.NAME, ShrinkAction.NAME, ForceMergeAction.NAME } + ); + assertNextActionName("warm", UnfollowAction.NAME, ShrinkAction.NAME, new String[] { ShrinkAction.NAME, ForceMergeAction.NAME }); assertNextActionName("warm", UnfollowAction.NAME, ForceMergeAction.NAME, new String[] { ForceMergeAction.NAME }); assertNextActionName("warm", UnfollowAction.NAME, null, new String[] {}); - assertNextActionName("warm", ReadOnlyAction.NAME, AllocateAction.NAME, - new String[] { ReadOnlyAction.NAME, AllocateAction.NAME, ShrinkAction.NAME, ForceMergeAction.NAME }); - assertNextActionName("warm", ReadOnlyAction.NAME, ShrinkAction.NAME, - new String[] { ReadOnlyAction.NAME, ShrinkAction.NAME, ForceMergeAction.NAME }); - assertNextActionName("warm", ReadOnlyAction.NAME, ForceMergeAction.NAME, - new String[] { ReadOnlyAction.NAME, ForceMergeAction.NAME }); + assertNextActionName( + "warm", + ReadOnlyAction.NAME, + AllocateAction.NAME, + new String[] { ReadOnlyAction.NAME, AllocateAction.NAME, ShrinkAction.NAME, ForceMergeAction.NAME } + ); + assertNextActionName( + "warm", + ReadOnlyAction.NAME, + ShrinkAction.NAME, + new String[] { ReadOnlyAction.NAME, ShrinkAction.NAME, ForceMergeAction.NAME } + ); + assertNextActionName( + "warm", + ReadOnlyAction.NAME, + ForceMergeAction.NAME, + new String[] { ReadOnlyAction.NAME, ForceMergeAction.NAME } + ); assertNextActionName("warm", ReadOnlyAction.NAME, null, new String[] { ReadOnlyAction.NAME }); - assertNextActionName("warm", ReadOnlyAction.NAME, AllocateAction.NAME, - new String[] { AllocateAction.NAME, ShrinkAction.NAME, ForceMergeAction.NAME }); + assertNextActionName( + "warm", + ReadOnlyAction.NAME, + AllocateAction.NAME, + new String[] { AllocateAction.NAME, ShrinkAction.NAME, ForceMergeAction.NAME } + ); assertNextActionName("warm", ReadOnlyAction.NAME, ShrinkAction.NAME, new String[] { ShrinkAction.NAME, ForceMergeAction.NAME }); assertNextActionName("warm", ReadOnlyAction.NAME, ForceMergeAction.NAME, new String[] { ForceMergeAction.NAME }); assertNextActionName("warm", ReadOnlyAction.NAME, null, new String[] {}); - assertNextActionName("warm", AllocateAction.NAME, ShrinkAction.NAME, - new String[] { ReadOnlyAction.NAME, AllocateAction.NAME, ShrinkAction.NAME, ForceMergeAction.NAME }); - assertNextActionName("warm", AllocateAction.NAME, ForceMergeAction.NAME, - new String[] { ReadOnlyAction.NAME, AllocateAction.NAME, ForceMergeAction.NAME }); + assertNextActionName( + "warm", + AllocateAction.NAME, + ShrinkAction.NAME, + new String[] { ReadOnlyAction.NAME, AllocateAction.NAME, ShrinkAction.NAME, ForceMergeAction.NAME } + ); + assertNextActionName( + "warm", + AllocateAction.NAME, + ForceMergeAction.NAME, + new String[] { ReadOnlyAction.NAME, AllocateAction.NAME, ForceMergeAction.NAME } + ); assertNextActionName("warm", AllocateAction.NAME, null, new String[] { ReadOnlyAction.NAME, AllocateAction.NAME }); assertNextActionName("warm", AllocateAction.NAME, ShrinkAction.NAME, new String[] { ShrinkAction.NAME, ForceMergeAction.NAME }); assertNextActionName("warm", AllocateAction.NAME, ForceMergeAction.NAME, new String[] { ForceMergeAction.NAME }); assertNextActionName("warm", AllocateAction.NAME, null, new String[] {}); - assertNextActionName("warm", ShrinkAction.NAME, ForceMergeAction.NAME, - new String[] { ReadOnlyAction.NAME, AllocateAction.NAME, ShrinkAction.NAME, ForceMergeAction.NAME }); - assertNextActionName("warm", ShrinkAction.NAME, null, - new String[] { ReadOnlyAction.NAME, AllocateAction.NAME, ShrinkAction.NAME }); + assertNextActionName( + "warm", + ShrinkAction.NAME, + ForceMergeAction.NAME, + new String[] { ReadOnlyAction.NAME, AllocateAction.NAME, ShrinkAction.NAME, ForceMergeAction.NAME } + ); + assertNextActionName("warm", ShrinkAction.NAME, null, new String[] { ReadOnlyAction.NAME, AllocateAction.NAME, ShrinkAction.NAME }); assertNextActionName("warm", ShrinkAction.NAME, ForceMergeAction.NAME, new String[] { ForceMergeAction.NAME }); assertNextActionName("warm", ShrinkAction.NAME, null, new String[] {}); - assertNextActionName("warm", ForceMergeAction.NAME, null, - new String[] { ReadOnlyAction.NAME, AllocateAction.NAME, ShrinkAction.NAME, ForceMergeAction.NAME }); + assertNextActionName( + "warm", + ForceMergeAction.NAME, + null, + new String[] { ReadOnlyAction.NAME, AllocateAction.NAME, ShrinkAction.NAME, ForceMergeAction.NAME } + ); assertNextActionName("warm", ForceMergeAction.NAME, null, new String[] {}); assertInvalidAction("warm", "foo", new String[] { RolloverAction.NAME }); - assertInvalidAction("warm", DeleteAction.NAME, - new String[] { ReadOnlyAction.NAME, AllocateAction.NAME, ShrinkAction.NAME, ForceMergeAction.NAME }); - assertInvalidAction("warm", RolloverAction.NAME, - new String[] { ReadOnlyAction.NAME, AllocateAction.NAME, ShrinkAction.NAME, ForceMergeAction.NAME }); + assertInvalidAction( + "warm", + DeleteAction.NAME, + new String[] { ReadOnlyAction.NAME, AllocateAction.NAME, ShrinkAction.NAME, ForceMergeAction.NAME } + ); + assertInvalidAction( + "warm", + RolloverAction.NAME, + new String[] { ReadOnlyAction.NAME, AllocateAction.NAME, ShrinkAction.NAME, ForceMergeAction.NAME } + ); // Cold Phase - assertNextActionName("cold", SetPriorityAction.NAME, UnfollowAction.NAME, - new String[]{UnfollowAction.NAME, SetPriorityAction.NAME, FreezeAction.NAME}); - assertNextActionName("cold", SetPriorityAction.NAME, FreezeAction.NAME, - new String[]{SetPriorityAction.NAME, FreezeAction.NAME}); - assertNextActionName("cold", SetPriorityAction.NAME, AllocateAction.NAME, - new String[]{SetPriorityAction.NAME, AllocateAction.NAME}); + assertNextActionName( + "cold", + SetPriorityAction.NAME, + UnfollowAction.NAME, + new String[] { UnfollowAction.NAME, SetPriorityAction.NAME, FreezeAction.NAME } + ); + assertNextActionName("cold", SetPriorityAction.NAME, FreezeAction.NAME, new String[] { SetPriorityAction.NAME, FreezeAction.NAME }); + assertNextActionName( + "cold", + SetPriorityAction.NAME, + AllocateAction.NAME, + new String[] { SetPriorityAction.NAME, AllocateAction.NAME } + ); assertNextActionName("cold", SetPriorityAction.NAME, null, new String[] { SetPriorityAction.NAME }); assertNextActionName("cold", SetPriorityAction.NAME, null, new String[] {}); - assertNextActionName("cold", UnfollowAction.NAME, ReadOnlyAction.NAME, - new String[] {ReadOnlyAction.NAME, SearchableSnapshotAction.NAME, SetPriorityAction.NAME, AllocateAction.NAME}); - assertNextActionName("cold", UnfollowAction.NAME, SearchableSnapshotAction.NAME, - new String[] {SearchableSnapshotAction.NAME, AllocateAction.NAME, FreezeAction.NAME}); - assertNextActionName("cold", UnfollowAction.NAME, AllocateAction.NAME, - new String[] {SetPriorityAction.NAME, AllocateAction.NAME, FreezeAction.NAME}); - assertNextActionName("cold", UnfollowAction.NAME, AllocateAction.NAME, - new String[] {AllocateAction.NAME, FreezeAction.NAME}); - assertNextActionName("cold", UnfollowAction.NAME, FreezeAction.NAME, new String[] {FreezeAction.NAME}); + assertNextActionName( + "cold", + UnfollowAction.NAME, + ReadOnlyAction.NAME, + new String[] { ReadOnlyAction.NAME, SearchableSnapshotAction.NAME, SetPriorityAction.NAME, AllocateAction.NAME } + ); + assertNextActionName( + "cold", + UnfollowAction.NAME, + SearchableSnapshotAction.NAME, + new String[] { SearchableSnapshotAction.NAME, AllocateAction.NAME, FreezeAction.NAME } + ); + assertNextActionName( + "cold", + UnfollowAction.NAME, + AllocateAction.NAME, + new String[] { SetPriorityAction.NAME, AllocateAction.NAME, FreezeAction.NAME } + ); + assertNextActionName("cold", UnfollowAction.NAME, AllocateAction.NAME, new String[] { AllocateAction.NAME, FreezeAction.NAME }); + assertNextActionName("cold", UnfollowAction.NAME, FreezeAction.NAME, new String[] { FreezeAction.NAME }); assertNextActionName("cold", UnfollowAction.NAME, null, new String[] {}); assertNextActionName("cold", AllocateAction.NAME, null, new String[] { AllocateAction.NAME }); @@ -615,18 +777,24 @@ public void testGetNextActionName() { assertInvalidAction("delete", UnfollowAction.NAME, new String[] { DeleteAction.NAME }); Phase phase = new Phase("foo", TimeValue.ZERO, Collections.emptyMap()); - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, - () -> TimeseriesLifecycleType.INSTANCE.getNextActionName(ShrinkAction.NAME, phase)); - assertEquals("lifecycle type [" + TimeseriesLifecycleType.TYPE + "] does not support phase [" + phase.getName() + "]", - exception.getMessage()); + IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> TimeseriesLifecycleType.INSTANCE.getNextActionName(ShrinkAction.NAME, phase) + ); + assertEquals( + "lifecycle type [" + TimeseriesLifecycleType.TYPE + "] does not support phase [" + phase.getName() + "]", + exception.getMessage() + ); } public void testShouldMigrateDataToTiers() { { // there's an allocate action Map actions = new HashMap<>(); - actions.put(TEST_ALLOCATE_ACTION.getWriteableName(), - randomFrom(TEST_ALLOCATE_ACTION, new AllocateAction(2, 20, null, null, null))); + actions.put( + TEST_ALLOCATE_ACTION.getWriteableName(), + randomFrom(TEST_ALLOCATE_ACTION, new AllocateAction(2, 20, null, null, null)) + ); Phase phase = new Phase(WARM_PHASE, TimeValue.ZERO, actions); assertThat(TimeseriesLifecycleType.shouldInjectMigrateStepForPhase(phase), is(true)); } @@ -675,11 +843,18 @@ public void testValidatingSearchableSnapshotRepos() { Phase coldPhase = new Phase(HOT_PHASE, TimeValue.ZERO, coldActions); Phase frozenPhase = new Phase(HOT_PHASE, TimeValue.ZERO, frozenActions); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> validateAllSearchableSnapshotActionsUseSameRepository(Arrays.asList(hotPhase, coldPhase, frozenPhase))); - assertThat(e.getMessage(), containsString("policy specifies [searchable_snapshot]" + - " action multiple times with differing repositories [repo2, repo1]," + - " the same repository must be used for all searchable snapshot actions")); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> validateAllSearchableSnapshotActionsUseSameRepository(Arrays.asList(hotPhase, coldPhase, frozenPhase)) + ); + assertThat( + e.getMessage(), + containsString( + "policy specifies [searchable_snapshot]" + + " action multiple times with differing repositories [repo2, repo1]," + + " the same repository must be used for all searchable snapshot actions" + ) + ); } } @@ -691,8 +866,11 @@ public void testValidatingIncreasingAges() { Phase frozenPhase = new Phase(FROZEN_PHASE, TimeValue.ZERO, Collections.emptyMap()); Phase deletePhase = new Phase(DELETE_PHASE, TimeValue.ZERO, Collections.emptyMap()); - assertFalse(Strings.hasText(validateMonotonicallyIncreasingPhaseTimings(Arrays.asList(hotPhase, - warmPhase, coldPhase, frozenPhase, deletePhase)))); + assertFalse( + Strings.hasText( + validateMonotonicallyIncreasingPhaseTimings(Arrays.asList(hotPhase, warmPhase, coldPhase, frozenPhase, deletePhase)) + ) + ); } { @@ -726,13 +904,18 @@ public void testValidatingIncreasingAges() { Phase frozenPhase = new Phase(FROZEN_PHASE, TimeValue.ZERO, Collections.emptyMap()); Phase deletePhase = new Phase(DELETE_PHASE, TimeValue.ZERO, Collections.emptyMap()); - String err = - validateMonotonicallyIncreasingPhaseTimings(Arrays.asList(hotPhase, warmPhase, coldPhase, frozenPhase, deletePhase)); + String err = validateMonotonicallyIncreasingPhaseTimings( + Arrays.asList(hotPhase, warmPhase, coldPhase, frozenPhase, deletePhase) + ); - assertThat(err, - containsString("Your policy is configured to run the cold phase "+ - "(min_age: 12h) before the hot phase (min_age: 1d). You should change "+ - "the phase timing so that the phases will execute in the order of hot, warm, then cold.")); + assertThat( + err, + containsString( + "Your policy is configured to run the cold phase " + + "(min_age: 12h) before the hot phase (min_age: 1d). You should change " + + "the phase timing so that the phases will execute in the order of hot, warm, then cold." + ) + ); } { @@ -742,13 +925,18 @@ public void testValidatingIncreasingAges() { Phase frozenPhase = new Phase(FROZEN_PHASE, TimeValue.timeValueDays(1), Collections.emptyMap()); Phase deletePhase = new Phase(DELETE_PHASE, TimeValue.timeValueDays(2), Collections.emptyMap()); - String err = - validateMonotonicallyIncreasingPhaseTimings(Arrays.asList(hotPhase, warmPhase, coldPhase, frozenPhase, deletePhase)); + String err = validateMonotonicallyIncreasingPhaseTimings( + Arrays.asList(hotPhase, warmPhase, coldPhase, frozenPhase, deletePhase) + ); - assertThat(err, - containsString("Your policy is configured to run the frozen phase "+ - "(min_age: 1d) and the delete phase (min_age: 2d) before the warm phase (min_age: 3d)."+ - " You should change the phase timing so that the phases will execute in the order of hot, warm, then cold.")); + assertThat( + err, + containsString( + "Your policy is configured to run the frozen phase " + + "(min_age: 1d) and the delete phase (min_age: 2d) before the warm phase (min_age: 3d)." + + " You should change the phase timing so that the phases will execute in the order of hot, warm, then cold." + ) + ); } { @@ -758,13 +946,18 @@ public void testValidatingIncreasingAges() { Phase frozenPhase = new Phase(FROZEN_PHASE, TimeValue.timeValueDays(2), Collections.emptyMap()); Phase deletePhase = new Phase(DELETE_PHASE, TimeValue.timeValueDays(1), Collections.emptyMap()); - String err = - validateMonotonicallyIncreasingPhaseTimings(Arrays.asList(hotPhase, warmPhase, coldPhase, frozenPhase, deletePhase)); + String err = validateMonotonicallyIncreasingPhaseTimings( + Arrays.asList(hotPhase, warmPhase, coldPhase, frozenPhase, deletePhase) + ); - assertThat(err, - containsString("Your policy is configured to run the frozen phase "+ - "(min_age: 2d) and the delete phase (min_age: 1d) before the warm phase (min_age: 3d)."+ - " You should change the phase timing so that the phases will execute in the order of hot, warm, then cold.")); + assertThat( + err, + containsString( + "Your policy is configured to run the frozen phase " + + "(min_age: 2d) and the delete phase (min_age: 1d) before the warm phase (min_age: 3d)." + + " You should change the phase timing so that the phases will execute in the order of hot, warm, then cold." + ) + ); } { @@ -774,8 +967,9 @@ public void testValidatingIncreasingAges() { Phase frozenPhase = new Phase(FROZEN_PHASE, TimeValue.timeValueDays(2), Collections.emptyMap()); Phase deletePhase = new Phase(DELETE_PHASE, TimeValue.timeValueDays(1), Collections.emptyMap()); - String err = - validateMonotonicallyIncreasingPhaseTimings(Arrays.asList(hotPhase, warmPhase, coldPhase, frozenPhase, deletePhase)); + String err = validateMonotonicallyIncreasingPhaseTimings( + Arrays.asList(hotPhase, warmPhase, coldPhase, frozenPhase, deletePhase) + ); assertThat( err, @@ -795,8 +989,9 @@ public void testValidatingIncreasingAges() { Phase frozenPhase = new Phase(FROZEN_PHASE, TimeValue.timeValueDays(2), Collections.emptyMap()); Phase deletePhase = new Phase(DELETE_PHASE, TimeValue.timeValueDays(1), Collections.emptyMap()); - String err = - validateMonotonicallyIncreasingPhaseTimings(Arrays.asList(hotPhase, warmPhase, coldPhase, frozenPhase, deletePhase)); + String err = validateMonotonicallyIncreasingPhaseTimings( + Arrays.asList(hotPhase, warmPhase, coldPhase, frozenPhase, deletePhase) + ); assertThat( err, @@ -821,10 +1016,17 @@ public void testValidateFrozenPhaseHasSearchableSnapshot() { { Map frozenActions = new HashMap<>(); Phase frozenPhase = new Phase(FROZEN_PHASE, TimeValue.ZERO, frozenActions); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> validateFrozenPhaseHasSearchableSnapshotAction(Collections.singleton(frozenPhase))); - assertThat(e.getMessage(), containsString("policy specifies the [frozen] phase without a corresponding " + - "[searchable_snapshot] action, but a searchable snapshot action is required in the frozen phase")); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> validateFrozenPhaseHasSearchableSnapshotAction(Collections.singleton(frozenPhase)) + ); + assertThat( + e.getMessage(), + containsString( + "policy specifies the [frozen] phase without a corresponding " + + "[searchable_snapshot] action, but a searchable snapshot action is required in the frozen phase" + ) + ); } } @@ -838,61 +1040,79 @@ private void assertNextActionName(String phaseName, String currentAction, String private void assertInvalidAction(String phaseName, String currentAction, String... availableActionNames) { Map availableActions = convertActionNamesToActions(availableActionNames); Phase phase = new Phase(phaseName, TimeValue.ZERO, availableActions); - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, - () -> TimeseriesLifecycleType.INSTANCE.getNextActionName(currentAction, phase)); - assertEquals("[" + currentAction + "] is not a valid action for phase [" + phaseName + "] in lifecycle type [" - + TimeseriesLifecycleType.TYPE + "]", exception.getMessage()); + IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> TimeseriesLifecycleType.INSTANCE.getNextActionName(currentAction, phase) + ); + assertEquals( + "[" + + currentAction + + "] is not a valid action for phase [" + + phaseName + + "] in lifecycle type [" + + TimeseriesLifecycleType.TYPE + + "]", + exception.getMessage() + ); } private ConcurrentMap convertActionNamesToActions(String... availableActionNames) { return Arrays.asList(availableActionNames).stream().map(n -> { switch (n) { - case AllocateAction.NAME: - return new AllocateAction(null, null, Collections.singletonMap("foo", "bar"), Collections.emptyMap(), - Collections.emptyMap()); - case DeleteAction.NAME: - return new DeleteAction(); - case ForceMergeAction.NAME: - return new ForceMergeAction(1, null); - case ReadOnlyAction.NAME: - return new ReadOnlyAction(); - case RolloverAction.NAME: - return new RolloverAction( - ByteSizeValue.parseBytesSizeValue("0b", "test"), - ByteSizeValue.parseBytesSizeValue("0b", "test"), - TimeValue.ZERO, - 1L); - case ShrinkAction.NAME: - return new ShrinkAction(1, null); - case FreezeAction.NAME: - return new FreezeAction(); - case SetPriorityAction.NAME: - return new SetPriorityAction(0); - case UnfollowAction.NAME: - return new UnfollowAction(); - case MigrateAction.NAME: - return new MigrateAction(true); - case RollupILMAction.NAME: - return TEST_ROLLUP_ACTION; - case SearchableSnapshotAction.NAME: - return TEST_SEARCHABLE_SNAPSHOT_ACTION; + case AllocateAction.NAME: + return new AllocateAction( + null, + null, + Collections.singletonMap("foo", "bar"), + Collections.emptyMap(), + Collections.emptyMap() + ); + case DeleteAction.NAME: + return new DeleteAction(); + case ForceMergeAction.NAME: + return new ForceMergeAction(1, null); + case ReadOnlyAction.NAME: + return new ReadOnlyAction(); + case RolloverAction.NAME: + return new RolloverAction( + ByteSizeValue.parseBytesSizeValue("0b", "test"), + ByteSizeValue.parseBytesSizeValue("0b", "test"), + TimeValue.ZERO, + 1L + ); + case ShrinkAction.NAME: + return new ShrinkAction(1, null); + case FreezeAction.NAME: + return new FreezeAction(); + case SetPriorityAction.NAME: + return new SetPriorityAction(0); + case UnfollowAction.NAME: + return new UnfollowAction(); + case MigrateAction.NAME: + return new MigrateAction(true); + case RollupILMAction.NAME: + return TEST_ROLLUP_ACTION; + case SearchableSnapshotAction.NAME: + return TEST_SEARCHABLE_SNAPSHOT_ACTION; } return new DeleteAction(); }).collect(Collectors.toConcurrentMap(LifecycleAction::getWriteableName, Function.identity())); } private void assertNextPhaseName(String currentPhase, String expectedNextPhase, String... availablePhaseNames) { - Map availablePhases = Arrays.asList(availablePhaseNames).stream() - .map(n -> new Phase(n, TimeValue.ZERO, Collections.emptyMap())) - .collect(Collectors.toMap(Phase::getName, Function.identity())); + Map availablePhases = Arrays.asList(availablePhaseNames) + .stream() + .map(n -> new Phase(n, TimeValue.ZERO, Collections.emptyMap())) + .collect(Collectors.toMap(Phase::getName, Function.identity())); String nextPhase = TimeseriesLifecycleType.INSTANCE.getNextPhaseName(currentPhase, availablePhases); assertEquals(expectedNextPhase, nextPhase); } private void assertPreviousPhaseName(String currentPhase, String expectedNextPhase, String... availablePhaseNames) { - Map availablePhases = Arrays.asList(availablePhaseNames).stream() - .map(n -> new Phase(n, TimeValue.ZERO, Collections.emptyMap())) - .collect(Collectors.toMap(Phase::getName, Function.identity())); + Map availablePhases = Arrays.asList(availablePhaseNames) + .stream() + .map(n -> new Phase(n, TimeValue.ZERO, Collections.emptyMap())) + .collect(Collectors.toMap(Phase::getName, Function.identity())); String nextPhase = TimeseriesLifecycleType.INSTANCE.getPreviousPhaseName(currentPhase, availablePhases); assertEquals(expectedNextPhase, nextPhase); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/UnfollowActionTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/UnfollowActionTests.java index 916d06f1cbc24..fd4c71aaa91eb 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/UnfollowActionTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/UnfollowActionTests.java @@ -39,8 +39,11 @@ protected Reader instanceReader() { public void testToSteps() { UnfollowAction action = createTestInstance(); String phase = randomAlphaOfLengthBetween(1, 10); - StepKey nextStepKey = new StepKey(randomAlphaOfLengthBetween(1, 10), randomAlphaOfLengthBetween(1, 10), - randomAlphaOfLengthBetween(1, 10)); + StepKey nextStepKey = new StepKey( + randomAlphaOfLengthBetween(1, 10), + randomAlphaOfLengthBetween(1, 10), + randomAlphaOfLengthBetween(1, 10) + ); List steps = action.toSteps(null, phase, nextStepKey); assertThat(steps, notNullValue()); assertThat(steps.size(), equalTo(8)); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/UnfollowFollowerIndexStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/UnfollowFollowerIndexStepTests.java index 78a447a6f80a0..de934345c13fe 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/UnfollowFollowerIndexStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/UnfollowFollowerIndexStepTests.java @@ -65,8 +65,10 @@ public void testRequestNotAcknowledged() { }).when(client).execute(Mockito.same(UnfollowAction.INSTANCE), Mockito.any(), Mockito.any()); UnfollowFollowerIndexStep step = new UnfollowFollowerIndexStep(randomStepKey(), randomStepKey(), client); - Exception e = expectThrows(Exception.class, - () -> PlainActionFuture.get(f -> step.performAction(indexMetadata, null, null, f))); + Exception e = expectThrows( + Exception.class, + () -> PlainActionFuture.get(f -> step.performAction(indexMetadata, null, null, f)) + ); assertThat(e.getMessage(), is("unfollow request failed to be acknowledged")); } @@ -89,8 +91,13 @@ public void testUnFollowUnfollowFailed() { }).when(client).execute(Mockito.same(UnfollowAction.INSTANCE), Mockito.any(), Mockito.any()); UnfollowFollowerIndexStep step = new UnfollowFollowerIndexStep(randomStepKey(), randomStepKey(), client); - assertSame(error, expectThrows(RuntimeException.class, - () -> PlainActionFuture.get(f -> step.performAction(indexMetadata, null, null, f)))); + assertSame( + error, + expectThrows( + RuntimeException.class, + () -> PlainActionFuture.get(f -> step.performAction(indexMetadata, null, null, f)) + ) + ); } public void testFailureToReleaseRetentionLeases() throws Exception { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/UpdateRolloverLifecycleDateStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/UpdateRolloverLifecycleDateStepTests.java index 6dbed6eaeedbd..712390e066e6c 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/UpdateRolloverLifecycleDateStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/UpdateRolloverLifecycleDateStepTests.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.xpack.core.ilm; - import org.elasticsearch.Version; import org.elasticsearch.action.admin.indices.rollover.RolloverInfo; import org.elasticsearch.cluster.ClusterName; @@ -62,22 +61,25 @@ public void testPerformAction() { long creationDate = randomLongBetween(0, 1000000); long rolloverTime = randomValueOtherThan(creationDate, () -> randomNonNegativeLong()); IndexMetadata newIndexMetadata = IndexMetadata.builder(randomAlphaOfLength(11)) - .settings(settings(Version.CURRENT)).creationDate(creationDate) - .putAlias(AliasMetadata.builder(alias)).numberOfShards(randomIntBetween(1, 5)) - .numberOfReplicas(randomIntBetween(0, 5)).build(); + .settings(settings(Version.CURRENT)) + .creationDate(creationDate) + .putAlias(AliasMetadata.builder(alias)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) .putRolloverInfo(new RolloverInfo(alias, Collections.emptyList(), rolloverTime)) .settings(settings(Version.CURRENT).put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) - .metadata(Metadata.builder() - .put(indexMetadata, false) - .put(newIndexMetadata, false)).build(); + .metadata(Metadata.builder().put(indexMetadata, false).put(newIndexMetadata, false)) + .build(); UpdateRolloverLifecycleDateStep step = createRandomInstance(); ClusterState newState = step.performAction(indexMetadata.getIndex(), clusterState); - long actualRolloverTime = LifecycleExecutionState - .fromIndexMetadata(newState.metadata().index(indexMetadata.getIndex())) + long actualRolloverTime = LifecycleExecutionState.fromIndexMetadata(newState.metadata().index(indexMetadata.getIndex())) .getLifecycleDate(); assertThat(actualRolloverTime, equalTo(rolloverTime)); } @@ -89,25 +91,34 @@ public void testPerformActionOnDataStream() { IndexMetadata originalIndexMeta = IndexMetadata.builder(DataStream.getDefaultBackingIndexName(dataStreamName, 1)) .putRolloverInfo(new RolloverInfo(dataStreamName, Collections.emptyList(), rolloverTime)) .settings(settings(Version.CURRENT)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); - IndexMetadata rolledIndexMeta= IndexMetadata.builder(DataStream.getDefaultBackingIndexName(dataStreamName, 2)) + IndexMetadata rolledIndexMeta = IndexMetadata.builder(DataStream.getDefaultBackingIndexName(dataStreamName, 2)) .settings(settings(Version.CURRENT)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) .metadata( Metadata.builder() - .put(new DataStream(dataStreamName, createTimestampField("@timestamp"), - List.of(originalIndexMeta.getIndex(), rolledIndexMeta.getIndex()))) + .put( + new DataStream( + dataStreamName, + createTimestampField("@timestamp"), + List.of(originalIndexMeta.getIndex(), rolledIndexMeta.getIndex()) + ) + ) .put(originalIndexMeta, true) .put(rolledIndexMeta, true) - ).build(); + ) + .build(); UpdateRolloverLifecycleDateStep step = createRandomInstance(); ClusterState newState = step.performAction(originalIndexMeta.getIndex(), clusterState); - long actualRolloverTime = LifecycleExecutionState - .fromIndexMetadata(newState.metadata().index(originalIndexMeta.getIndex())) + long actualRolloverTime = LifecycleExecutionState.fromIndexMetadata(newState.metadata().index(originalIndexMeta.getIndex())) .getLifecycleDate(); assertThat(actualRolloverTime, equalTo(rolloverTime)); } @@ -117,32 +128,54 @@ public void testPerformActionBeforeRolloverHappened() { long creationDate = randomLongBetween(0, 1000000); IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(11)) .settings(settings(Version.CURRENT).put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias)) - .creationDate(creationDate).putAlias(AliasMetadata.builder(alias)).numberOfShards(randomIntBetween(1, 5)) - .numberOfReplicas(randomIntBetween(0, 5)).build(); + .creationDate(creationDate) + .putAlias(AliasMetadata.builder(alias)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) - .metadata(Metadata.builder().put(indexMetadata, false)).build(); + .metadata(Metadata.builder().put(indexMetadata, false)) + .build(); UpdateRolloverLifecycleDateStep step = createRandomInstance(); - IllegalStateException exceptionThrown = expectThrows(IllegalStateException.class, - () -> step.performAction(indexMetadata.getIndex(), clusterState)); - assertThat(exceptionThrown.getMessage(), - equalTo("no rollover info found for [" + indexMetadata.getIndex().getName() + "] with rollover target [" + alias + "], the " + - "index has not yet rolled over with that target")); + IllegalStateException exceptionThrown = expectThrows( + IllegalStateException.class, + () -> step.performAction(indexMetadata.getIndex(), clusterState) + ); + assertThat( + exceptionThrown.getMessage(), + equalTo( + "no rollover info found for [" + + indexMetadata.getIndex().getName() + + "] with rollover target [" + + alias + + "], the " + + "index has not yet rolled over with that target" + ) + ); } public void testPerformActionWithNoRolloverAliasSetting() { long creationDate = randomLongBetween(0, 1000000); IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(11)) - .settings(settings(Version.CURRENT)).creationDate(creationDate).numberOfShards(randomIntBetween(1, 5)) - .numberOfReplicas(randomIntBetween(0, 5)).build(); + .settings(settings(Version.CURRENT)) + .creationDate(creationDate) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) - .metadata(Metadata.builder().put(indexMetadata, false)).build(); + .metadata(Metadata.builder().put(indexMetadata, false)) + .build(); UpdateRolloverLifecycleDateStep step = createRandomInstance(); - IllegalStateException exceptionThrown = expectThrows(IllegalStateException.class, - () -> step.performAction(indexMetadata.getIndex(), clusterState)); - assertThat(exceptionThrown.getMessage(), - equalTo("setting [index.lifecycle.rollover_alias] is not set on index [" + indexMetadata.getIndex().getName() +"]")); + IllegalStateException exceptionThrown = expectThrows( + IllegalStateException.class, + () -> step.performAction(indexMetadata.getIndex(), clusterState) + ); + assertThat( + exceptionThrown.getMessage(), + equalTo("setting [index.lifecycle.rollover_alias] is not set on index [" + indexMetadata.getIndex().getName() + "]") + ); } public void testPerformActionWithIndexingComplete() { @@ -151,18 +184,20 @@ public void testPerformActionWithIndexingComplete() { long rolloverTime = randomValueOtherThan(creationDate, () -> randomNonNegativeLong()); IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) - .settings(settings(Version.CURRENT) - .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias) - .put(LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE, true)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + .settings( + settings(Version.CURRENT).put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias) + .put(LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE, true) + ) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) - .metadata(Metadata.builder() - .put(indexMetadata, false)).build(); + .metadata(Metadata.builder().put(indexMetadata, false)) + .build(); UpdateRolloverLifecycleDateStep step = createRandomInstanceWithFallbackTime(() -> rolloverTime); ClusterState newState = step.performAction(indexMetadata.getIndex(), clusterState); - long actualRolloverTime = LifecycleExecutionState - .fromIndexMetadata(newState.metadata().index(indexMetadata.getIndex())) + long actualRolloverTime = LifecycleExecutionState.fromIndexMetadata(newState.metadata().index(indexMetadata.getIndex())) .getLifecycleDate(); assertThat(actualRolloverTime, equalTo(rolloverTime)); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/UpdateRollupIndexPolicyStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/UpdateRollupIndexPolicyStepTests.java index d10b4763f12ad..338597deed37a 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/UpdateRollupIndexPolicyStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/UpdateRollupIndexPolicyStepTests.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.xpack.core.ilm; - import org.elasticsearch.Version; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest; @@ -44,17 +43,17 @@ public UpdateRollupIndexPolicyStep mutateInstance(UpdateRollupIndexPolicyStep in String rollupPolicy = instance.getRollupPolicy(); switch (between(0, 2)) { - case 0: - key = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); - break; - case 1: - nextKey = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); - break; - case 2: - rollupPolicy = randomAlphaOfLength(5); - break; - default: - throw new AssertionError("Illegal randomisation branch"); + case 0: + key = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); + break; + case 1: + nextKey = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); + break; + case 2: + rollupPolicy = randomAlphaOfLength(5); + break; + default: + throw new AssertionError("Illegal randomisation branch"); } return new UpdateRollupIndexPolicyStep(key, nextKey, client, rollupPolicy); @@ -62,15 +61,20 @@ public UpdateRollupIndexPolicyStep mutateInstance(UpdateRollupIndexPolicyStep in @Override public UpdateRollupIndexPolicyStep copyInstance(UpdateRollupIndexPolicyStep instance) { - return new UpdateRollupIndexPolicyStep(instance.getKey(), instance.getNextStepKey(), instance.getClient(), - instance.getRollupPolicy()); + return new UpdateRollupIndexPolicyStep( + instance.getKey(), + instance.getNextStepKey(), + instance.getClient(), + instance.getRollupPolicy() + ); } private static IndexMetadata getIndexMetadata() { Map ilmCustom = Collections.singletonMap("rollup_index_name", "rollup-index"); - return IndexMetadata.builder(randomAlphaOfLength(10)).settings( - settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, "test-ilm-policy")) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)) + return IndexMetadata.builder(randomAlphaOfLength(10)) + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, "test-ilm-policy")) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) .putCustom(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY, ilmCustom) .build(); } @@ -99,8 +103,9 @@ public void testPerformAction() throws Exception { public void testPerformActionFailure() { IndexMetadata indexMetadata = getIndexMetadata(); - ClusterState clusterState = - ClusterState.builder(emptyClusterState()).metadata(Metadata.builder().put(indexMetadata, true).build()).build(); + ClusterState clusterState = ClusterState.builder(emptyClusterState()) + .metadata(Metadata.builder().put(indexMetadata, true).build()) + .build(); Exception exception = new RuntimeException(); UpdateRollupIndexPolicyStep step = createRandomInstance(); Settings settings = Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, step.getRollupPolicy()).build(); @@ -115,8 +120,13 @@ public void testPerformActionFailure() { return null; }).when(indicesClient).updateSettings(Mockito.any(), Mockito.any()); - assertSame(exception, expectThrows(Exception.class, - () -> PlainActionFuture.get(f -> step.performAction(indexMetadata, clusterState, null, f)))); + assertSame( + exception, + expectThrows( + Exception.class, + () -> PlainActionFuture.get(f -> step.performAction(indexMetadata, clusterState, null, f)) + ) + ); Mockito.verify(client, Mockito.only()).admin(); Mockito.verify(adminClient, Mockito.only()).indices(); @@ -124,9 +134,10 @@ public void testPerformActionFailure() { } public void testPerformActionFailureIllegalExecutionState() { - IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)).settings( - settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, "test-ilm-policy")) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)) + IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, "test-ilm-policy")) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) .build(); String policyName = indexMetadata.getSettings().get(LifecycleSettings.LIFECYCLE_NAME); String indexName = indexMetadata.getIndex().getName(); @@ -140,8 +151,10 @@ public void onResponse(Void unused) { @Override public void onFailure(Exception e) { assertThat(e, instanceOf(IllegalStateException.class)); - assertThat(e.getMessage(), - is("rollup index name was not generated for policy [" + policyName + "] and index [" + indexName + "]")); + assertThat( + e.getMessage(), + is("rollup index name was not generated for policy [" + policyName + "] and index [" + indexName + "]") + ); } }); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/UpdateSettingsStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/UpdateSettingsStepTests.java index 673e02c8cdaf0..07d3d46920e2d 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/UpdateSettingsStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/UpdateSettingsStepTests.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.xpack.core.ilm; - import org.elasticsearch.Version; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest; @@ -37,17 +36,17 @@ public UpdateSettingsStep mutateInstance(UpdateSettingsStep instance) { Settings settings = instance.getSettings(); switch (between(0, 2)) { - case 0: - key = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); - break; - case 1: - nextKey = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); - break; - case 2: - settings = Settings.builder().put(settings).put(randomAlphaOfLength(10), randomInt()).build(); - break; - default: - throw new AssertionError("Illegal randomisation branch"); + case 0: + key = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); + break; + case 1: + nextKey = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); + break; + case 2: + settings = Settings.builder().put(settings).put(randomAlphaOfLength(10), randomInt()).build(); + break; + default: + throw new AssertionError("Illegal randomisation branch"); } return new UpdateSettingsStep(key, nextKey, client, settings); @@ -59,8 +58,11 @@ public UpdateSettingsStep copyInstance(UpdateSettingsStep instance) { } private static IndexMetadata getIndexMetadata() { - return IndexMetadata.builder(randomAlphaOfLength(10)).settings(settings(Version.CURRENT)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + return IndexMetadata.builder(randomAlphaOfLength(10)) + .settings(settings(Version.CURRENT)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); } public void testPerformAction() { @@ -73,7 +75,7 @@ public void testPerformAction() { @SuppressWarnings("unchecked") ActionListener listener = (ActionListener) invocation.getArguments()[1]; assertThat(request.settings(), equalTo(step.getSettings())); - assertThat(request.indices(), equalTo(new String[] {indexMetadata.getIndex().getName()})); + assertThat(request.indices(), equalTo(new String[] { indexMetadata.getIndex().getName() })); listener.onResponse(AcknowledgedResponse.TRUE); return null; }).when(indicesClient).updateSettings(Mockito.any(), Mockito.any()); @@ -95,13 +97,18 @@ public void testPerformActionFailure() { @SuppressWarnings("unchecked") ActionListener listener = (ActionListener) invocation.getArguments()[1]; assertThat(request.settings(), equalTo(step.getSettings())); - assertThat(request.indices(), equalTo(new String[] {indexMetadata.getIndex().getName()})); + assertThat(request.indices(), equalTo(new String[] { indexMetadata.getIndex().getName() })); listener.onFailure(exception); return null; }).when(indicesClient).updateSettings(Mockito.any(), Mockito.any()); - assertSame(exception, expectThrows(Exception.class, - () -> PlainActionFuture.get(f -> step.performAction(indexMetadata, emptyClusterState(), null, f)))); + assertSame( + exception, + expectThrows( + Exception.class, + () -> PlainActionFuture.get(f -> step.performAction(indexMetadata, emptyClusterState(), null, f)) + ) + ); Mockito.verify(client, Mockito.only()).admin(); Mockito.verify(adminClient, Mockito.only()).indices(); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForActiveShardsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForActiveShardsTests.java index 61a81afda1173..cbfe42624dfe5 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForActiveShardsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForActiveShardsTests.java @@ -18,10 +18,10 @@ import org.elasticsearch.cluster.routing.ShardRoutingState; import org.elasticsearch.cluster.routing.TestShardRouting; import org.elasticsearch.common.Strings; +import org.elasticsearch.index.Index; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.index.Index; import org.elasticsearch.xpack.core.ilm.Step.StepKey; import java.io.IOException; @@ -71,7 +71,9 @@ public void testIsConditionMetThrowsExceptionWhenRolloverAliasIsNotSet() { IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) .putAlias(AliasMetadata.builder(alias)) .settings(settings(Version.CURRENT)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) .metadata(Metadata.builder().put(indexMetadata, true).build()) .build(); @@ -80,8 +82,16 @@ public void testIsConditionMetThrowsExceptionWhenRolloverAliasIsNotSet() { createRandomInstance().isConditionMet(indexMetadata.getIndex(), clusterState); fail("expected the invocation to fail"); } catch (IllegalStateException e) { - assertThat(e.getMessage(), is("setting [" + RolloverAction.LIFECYCLE_ROLLOVER_ALIAS - + "] is not set on index [" + indexMetadata.getIndex().getName() + "]")); + assertThat( + e.getMessage(), + is( + "setting [" + + RolloverAction.LIFECYCLE_ROLLOVER_ALIAS + + "] is not set on index [" + + indexMetadata.getIndex().getName() + + "]" + ) + ); } } @@ -95,27 +105,30 @@ public void testResultEvaluatedOnWriteIndexAliasWhenExists() { .build(); IndexMetadata rolledIndex = IndexMetadata.builder("index-000001") .putAlias(AliasMetadata.builder(alias).writeIndex(true)) - .settings(settings(Version.CURRENT) - .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias) - .put("index.write.wait_for_active_shards", "all") + .settings( + settings(Version.CURRENT).put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias) + .put("index.write.wait_for_active_shards", "all") ) .numberOfShards(1) .numberOfReplicas(1) .build(); IndexRoutingTable.Builder routingTable = new IndexRoutingTable.Builder(rolledIndex.getIndex()); - routingTable.addShard(TestShardRouting.newShardRouting(rolledIndex.getIndex().getName(), 0, "node", null, true, - ShardRoutingState.STARTED)); - routingTable.addShard(TestShardRouting.newShardRouting(rolledIndex.getIndex().getName(), 0, "node2", null, false, - ShardRoutingState.STARTED)); + routingTable.addShard( + TestShardRouting.newShardRouting(rolledIndex.getIndex().getName(), 0, "node", null, true, ShardRoutingState.STARTED) + ); + routingTable.addShard( + TestShardRouting.newShardRouting(rolledIndex.getIndex().getName(), 0, "node2", null, false, ShardRoutingState.STARTED) + ); ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) - .metadata(Metadata.builder().put(originalIndex, true) - .put(rolledIndex, true) - .build()) + .metadata(Metadata.builder().put(originalIndex, true).put(rolledIndex, true).build()) .routingTable(RoutingTable.builder().add(routingTable.build()).build()) .build(); - assertThat("the rolled index has both the primary and the replica shards started so the condition should be met", - createRandomInstance().isConditionMet(originalIndex.getIndex(), clusterState).isComplete(), is(true)); + assertThat( + "the rolled index has both the primary and the replica shards started so the condition should be met", + createRandomInstance().isConditionMet(originalIndex.getIndex(), clusterState).isComplete(), + is(true) + ); } public void testResultEvaluatedOnOnlyIndexTheAliasPointsToIfWriteIndexIsNull() { @@ -127,50 +140,64 @@ public void testResultEvaluatedOnOnlyIndexTheAliasPointsToIfWriteIndexIsNull() { .build(); IndexMetadata rolledIndex = IndexMetadata.builder("index-000001") .putAlias(AliasMetadata.builder(alias).writeIndex(false)) - .settings(settings(Version.CURRENT) - .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias) - .put("index.write.wait_for_active_shards", "all") + .settings( + settings(Version.CURRENT).put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias) + .put("index.write.wait_for_active_shards", "all") ) .numberOfShards(1) .numberOfReplicas(1) .build(); IndexRoutingTable.Builder routingTable = new IndexRoutingTable.Builder(rolledIndex.getIndex()); - routingTable.addShard(TestShardRouting.newShardRouting(rolledIndex.getIndex().getName(), 0, "node", null, true, - ShardRoutingState.STARTED)); - routingTable.addShard(TestShardRouting.newShardRouting(rolledIndex.getIndex().getName(), 0, "node2", null, false, - ShardRoutingState.STARTED)); + routingTable.addShard( + TestShardRouting.newShardRouting(rolledIndex.getIndex().getName(), 0, "node", null, true, ShardRoutingState.STARTED) + ); + routingTable.addShard( + TestShardRouting.newShardRouting(rolledIndex.getIndex().getName(), 0, "node2", null, false, ShardRoutingState.STARTED) + ); ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) - .metadata(Metadata.builder().put(originalIndex, true) - .put(rolledIndex, true) - .build()) + .metadata(Metadata.builder().put(originalIndex, true).put(rolledIndex, true).build()) .routingTable(RoutingTable.builder().add(routingTable.build()).build()) .build(); - assertThat("the index the alias is pointing to has both the primary and the replica shards started so the condition should be" + - " met", createRandomInstance().isConditionMet(originalIndex.getIndex(), clusterState).isComplete(), is(true)); + assertThat( + "the index the alias is pointing to has both the primary and the replica shards started so the condition should be" + " met", + createRandomInstance().isConditionMet(originalIndex.getIndex(), clusterState).isComplete(), + is(true) + ); } public void testResultEvaluatedOnDataStream() throws IOException { String dataStreamName = "test-datastream"; IndexMetadata originalIndexMeta = IndexMetadata.builder(DataStream.getDefaultBackingIndexName(dataStreamName, 1)) .settings(settings(Version.CURRENT)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); - IndexMetadata rolledIndexMeta= IndexMetadata.builder(DataStream.getDefaultBackingIndexName(dataStreamName, 2)) + IndexMetadata rolledIndexMeta = IndexMetadata.builder(DataStream.getDefaultBackingIndexName(dataStreamName, 2)) .settings(settings(Version.CURRENT).put("index.write.wait_for_active_shards", "3")) - .numberOfShards(1).numberOfReplicas(3).build(); + .numberOfShards(1) + .numberOfReplicas(3) + .build(); IndexRoutingTable.Builder routingTable = new IndexRoutingTable.Builder(rolledIndexMeta.getIndex()); - routingTable.addShard(TestShardRouting.newShardRouting(rolledIndexMeta.getIndex().getName(), 0, "node", null, true, - ShardRoutingState.STARTED)); - routingTable.addShard(TestShardRouting.newShardRouting(rolledIndexMeta.getIndex().getName(), 0, "node2", null, false, - ShardRoutingState.STARTED)); + routingTable.addShard( + TestShardRouting.newShardRouting(rolledIndexMeta.getIndex().getName(), 0, "node", null, true, ShardRoutingState.STARTED) + ); + routingTable.addShard( + TestShardRouting.newShardRouting(rolledIndexMeta.getIndex().getName(), 0, "node2", null, false, ShardRoutingState.STARTED) + ); ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) .metadata( Metadata.builder() - .put(new DataStream(dataStreamName, createTimestampField("@timestamp"), - List.of(originalIndexMeta.getIndex(), rolledIndexMeta.getIndex()))) + .put( + new DataStream( + dataStreamName, + createTimestampField("@timestamp"), + List.of(originalIndexMeta.getIndex(), rolledIndexMeta.getIndex()) + ) + ) .put(originalIndexMeta, true) .put(rolledIndexMeta, true) ) @@ -182,8 +209,10 @@ public void testResultEvaluatedOnDataStream() throws IOException { ClusterStateWaitStep.Result result = waitForActiveShardsStep.isConditionMet(originalIndexMeta.getIndex(), clusterState); assertThat(result.isComplete(), is(false)); - XContentBuilder expected = new WaitForActiveShardsStep.ActiveShardsInfo(2, "3", false).toXContent(JsonXContent.contentBuilder(), - ToXContent.EMPTY_PARAMS); + XContentBuilder expected = new WaitForActiveShardsStep.ActiveShardsInfo(2, "3", false).toXContent( + JsonXContent.contentBuilder(), + ToXContent.EMPTY_PARAMS + ); String actualResultAsString = Strings.toString(result.getInfomationContext()); assertThat(actualResultAsString, is(Strings.toString(expected))); assertThat(actualResultAsString, containsString("waiting for [3] shards to become active, but only [2] are active")); @@ -199,30 +228,31 @@ public void testResultReportsMeaningfulMessage() throws IOException { .build(); IndexMetadata rolledIndex = IndexMetadata.builder("index-000001") .putAlias(AliasMetadata.builder(alias).writeIndex(true)) - .settings(settings(Version.CURRENT) - .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias) - .put("index.write.wait_for_active_shards", "3") + .settings( + settings(Version.CURRENT).put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias).put("index.write.wait_for_active_shards", "3") ) .numberOfShards(1) .numberOfReplicas(2) .build(); IndexRoutingTable.Builder routingTable = new IndexRoutingTable.Builder(rolledIndex.getIndex()); - routingTable.addShard(TestShardRouting.newShardRouting(rolledIndex.getIndex().getName(), 0, "node", null, true, - ShardRoutingState.STARTED)); - routingTable.addShard(TestShardRouting.newShardRouting(rolledIndex.getIndex().getName(), 0, "node2", null, false, - ShardRoutingState.STARTED)); + routingTable.addShard( + TestShardRouting.newShardRouting(rolledIndex.getIndex().getName(), 0, "node", null, true, ShardRoutingState.STARTED) + ); + routingTable.addShard( + TestShardRouting.newShardRouting(rolledIndex.getIndex().getName(), 0, "node2", null, false, ShardRoutingState.STARTED) + ); ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) - .metadata(Metadata.builder().put(originalIndex, true) - .put(rolledIndex, true) - .build()) + .metadata(Metadata.builder().put(originalIndex, true).put(rolledIndex, true).build()) .routingTable(RoutingTable.builder().add(routingTable.build()).build()) .build(); ClusterStateWaitStep.Result result = createRandomInstance().isConditionMet(originalIndex.getIndex(), clusterState); assertThat(result.isComplete(), is(false)); - XContentBuilder expected = new WaitForActiveShardsStep.ActiveShardsInfo(2, "3", false).toXContent(JsonXContent.contentBuilder(), - ToXContent.EMPTY_PARAMS); + XContentBuilder expected = new WaitForActiveShardsStep.ActiveShardsInfo(2, "3", false).toXContent( + JsonXContent.contentBuilder(), + ToXContent.EMPTY_PARAMS + ); String actualResultAsString = Strings.toString(result.getInfomationContext()); assertThat(actualResultAsString, is(Strings.toString(expected))); assertThat(actualResultAsString, containsString("waiting for [3] shards to become active, but only [2] are active")); @@ -232,9 +262,8 @@ public void testResultReportsErrorMessage() { String alias = randomAlphaOfLength(5); IndexMetadata rolledIndex = IndexMetadata.builder("index-000001") .putAlias(AliasMetadata.builder(alias).writeIndex(true)) - .settings(settings(Version.CURRENT) - .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias) - .put("index.write.wait_for_active_shards", "3") + .settings( + settings(Version.CURRENT).put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias).put("index.write.wait_for_active_shards", "3") ) .numberOfShards(1) .numberOfReplicas(2) @@ -244,13 +273,15 @@ public void testResultReportsErrorMessage() { .build(); WaitForActiveShardsStep step = createRandomInstance(); - ClusterStateWaitStep.Result result = step.isConditionMet(new Index("index-000000", UUID.randomUUID().toString()), - clusterState); + ClusterStateWaitStep.Result result = step.isConditionMet(new Index("index-000000", UUID.randomUUID().toString()), clusterState); assertThat(result.isComplete(), is(false)); String actualResultAsString = Strings.toString(result.getInfomationContext()); - assertThat(actualResultAsString, - containsString("[" + step.getKey().getAction() + "] lifecycle action for index [index-000000] executed but " + - "index no longer exists")); + assertThat( + actualResultAsString, + containsString( + "[" + step.getKey().getAction() + "] lifecycle action for index [index-000000] executed but " + "index no longer exists" + ) + ); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForDataTierStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForDataTierStepTests.java index 502c925f2bd77..c0fb29be28aec 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForDataTierStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForDataTierStepTests.java @@ -13,9 +13,9 @@ import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.node.DiscoveryNodeRole; import org.elasticsearch.cluster.node.DiscoveryNodes; +import org.elasticsearch.cluster.routing.allocation.DataTier; import org.elasticsearch.common.Strings; import org.elasticsearch.common.UUIDs; -import org.elasticsearch.cluster.routing.allocation.DataTier; import java.util.Collection; import java.util.List; @@ -38,16 +38,23 @@ protected WaitForDataTierStep createRandomInstance() { protected WaitForDataTierStep mutateInstance(WaitForDataTierStep instance) { switch (between(0, 2)) { case 0: - return new WaitForDataTierStep(randomValueOtherThan(instance.getKey(), AbstractStepTestCase::randomStepKey), + return new WaitForDataTierStep( + randomValueOtherThan(instance.getKey(), AbstractStepTestCase::randomStepKey), instance.getNextStepKey(), - instance.tierPreference()); + instance.tierPreference() + ); case 1: - return new WaitForDataTierStep(instance.getKey(), + return new WaitForDataTierStep( + instance.getKey(), randomValueOtherThan(instance.getNextStepKey(), AbstractStepTestCase::randomStepKey), - instance.tierPreference()); + instance.tierPreference() + ); case 2: - return new WaitForDataTierStep(instance.getKey(), instance.getNextStepKey(), - randomValueOtherThan(instance.tierPreference(), () -> randomAlphaOfLength(5))); + return new WaitForDataTierStep( + instance.getKey(), + instance.getNextStepKey(), + randomValueOtherThan(instance.tierPreference(), () -> randomAlphaOfLength(5)) + ); } throw new AssertionError(); } @@ -60,12 +67,13 @@ protected WaitForDataTierStep copyInstance(WaitForDataTierStep instance) { public void testConditionMet() { String notIncludedTier = randomFrom(DataTier.ALL_DATA_TIERS); List otherTiers = DataTier.ALL_DATA_TIERS.stream() - .filter(tier -> notIncludedTier.equals(tier) == false).collect(Collectors.toList()); + .filter(tier -> notIncludedTier.equals(tier) == false) + .collect(Collectors.toList()); List includedTiers = randomSubsetOf(between(1, otherTiers.size()), otherTiers); String tierPreference = String.join(",", includedTiers); WaitForDataTierStep step = new WaitForDataTierStep(randomStepKey(), randomStepKey(), tierPreference); - verify (step, ClusterState.EMPTY_STATE, false, "no nodes for tiers [" + tierPreference + "] available"); + verify(step, ClusterState.EMPTY_STATE, false, "no nodes for tiers [" + tierPreference + "] available"); verify(step, state(List.of(notIncludedTier)), false, "no nodes for tiers [" + tierPreference + "] available"); verify(step, state(includedTiers), true, null); verify(step, state(List.of(DiscoveryNodeRole.DATA_ROLE.roleName())), true, null); @@ -84,17 +92,19 @@ private void verify(WaitForDataTierStep step, ClusterState state, boolean comple private ClusterState state(Collection roles) { DiscoveryNodes.Builder builder = DiscoveryNodes.builder(); IntStream.range(0, between(1, 5)) - .mapToObj(i -> - new DiscoveryNode( + .mapToObj( + i -> new DiscoveryNode( "node_" + i, UUIDs.randomBase64UUID(), buildNewFakeTransportAddress(), Map.of(), randomSubsetOf(between(1, roles.size()), roles).stream() - .map(DiscoveryNodeRole::getRoleFromRoleName).collect(Collectors.toSet()), + .map(DiscoveryNodeRole::getRoleFromRoleName) + .collect(Collectors.toSet()), Version.CURRENT ) - ).forEach(builder::add); + ) + .forEach(builder::add); return ClusterState.builder(ClusterName.DEFAULT).nodes(builder).build(); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForFollowShardTasksStepInfoTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForFollowShardTasksStepInfoTests.java index ea8664ddd1259..62c12e272ef59 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForFollowShardTasksStepInfoTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForFollowShardTasksStepInfoTests.java @@ -6,9 +6,9 @@ */ package org.elasticsearch.xpack.core.ilm; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xpack.core.ilm.WaitForFollowShardTasksStep.Info; import org.elasticsearch.xpack.core.ilm.WaitForFollowShardTasksStep.Info.ShardFollowTaskInfo; @@ -18,33 +18,32 @@ public class WaitForFollowShardTasksStepInfoTests extends AbstractXContentTestCase { - private static final ConstructingObjectParser SHARD_FOLLOW_TASK_INFO_PARSER = - new ConstructingObjectParser<>( - "shard_follow_task_info_parser", - args -> new ShardFollowTaskInfo((String) args[0], (Integer) args[1], (Long) args[2], (Long) args[3]) - ); + private static final ConstructingObjectParser SHARD_FOLLOW_TASK_INFO_PARSER = new ConstructingObjectParser<>( + "shard_follow_task_info_parser", + args -> new ShardFollowTaskInfo((String) args[0], (Integer) args[1], (Long) args[2], (Long) args[3]) + ); static { SHARD_FOLLOW_TASK_INFO_PARSER.declareString(ConstructingObjectParser.constructorArg(), ShardFollowTaskInfo.FOLLOWER_INDEX_FIELD); SHARD_FOLLOW_TASK_INFO_PARSER.declareInt(ConstructingObjectParser.constructorArg(), ShardFollowTaskInfo.SHARD_ID_FIELD); - SHARD_FOLLOW_TASK_INFO_PARSER.declareLong(ConstructingObjectParser.constructorArg(), - ShardFollowTaskInfo.LEADER_GLOBAL_CHECKPOINT_FIELD); - SHARD_FOLLOW_TASK_INFO_PARSER.declareLong(ConstructingObjectParser.constructorArg(), - ShardFollowTaskInfo.FOLLOWER_GLOBAL_CHECKPOINT_FIELD); + SHARD_FOLLOW_TASK_INFO_PARSER.declareLong( + ConstructingObjectParser.constructorArg(), + ShardFollowTaskInfo.LEADER_GLOBAL_CHECKPOINT_FIELD + ); + SHARD_FOLLOW_TASK_INFO_PARSER.declareLong( + ConstructingObjectParser.constructorArg(), + ShardFollowTaskInfo.FOLLOWER_GLOBAL_CHECKPOINT_FIELD + ); } - private static final ConstructingObjectParser INFO_PARSER = new ConstructingObjectParser<>( - "info_parser", - args -> { - @SuppressWarnings("unchecked") - Info info = new Info((List) args[0]); - return info; - } - ); + private static final ConstructingObjectParser INFO_PARSER = new ConstructingObjectParser<>("info_parser", args -> { + @SuppressWarnings("unchecked") + Info info = new Info((List) args[0]); + return info; + }); static { - INFO_PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), SHARD_FOLLOW_TASK_INFO_PARSER, - Info.SHARD_FOLLOW_TASKS); + INFO_PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), SHARD_FOLLOW_TASK_INFO_PARSER, Info.SHARD_FOLLOW_TASKS); INFO_PARSER.declareString((i, s) -> {}, Info.MESSAGE); } @@ -53,8 +52,9 @@ protected Info createTestInstance() { int numInfos = randomIntBetween(0, 32); List shardFollowTaskInfos = new ArrayList<>(numInfos); for (int i = 0; i < numInfos; i++) { - shardFollowTaskInfos.add(new ShardFollowTaskInfo(randomAlphaOfLength(3), randomIntBetween(0, 10), - randomNonNegativeLong(), randomNonNegativeLong())); + shardFollowTaskInfos.add( + new ShardFollowTaskInfo(randomAlphaOfLength(3), randomIntBetween(0, 10), randomNonNegativeLong(), randomNonNegativeLong()) + ); } return new Info(shardFollowTaskInfos); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForFollowShardTasksStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForFollowShardTasksStepTests.java index 71eb7f8a75237..96ccfad0282a6 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForFollowShardTasksStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForFollowShardTasksStepTests.java @@ -70,7 +70,9 @@ public void testConditionMet() { final boolean[] conditionMetHolder = new boolean[1]; final ToXContentObject[] informationContextHolder = new ToXContentObject[1]; final Exception[] exceptionHolder = new Exception[1]; - createRandomInstance().evaluateCondition(Metadata.builder().put(indexMetadata, true).build(), indexMetadata.getIndex(), + createRandomInstance().evaluateCondition( + Metadata.builder().put(indexMetadata, true).build(), + indexMetadata.getIndex(), new AsyncWaitStep.Listener() { @Override public void onResponse(boolean conditionMet, ToXContentObject informationContext) { @@ -82,7 +84,9 @@ public void onResponse(boolean conditionMet, ToXContentObject informationContext public void onFailure(Exception e) { exceptionHolder[0] = e; } - }, MASTER_TIMEOUT); + }, + MASTER_TIMEOUT + ); assertThat(conditionMetHolder[0], is(true)); assertThat(informationContextHolder[0], nullValue()); @@ -105,7 +109,9 @@ public void testConditionNotMetShardsNotInSync() { final boolean[] conditionMetHolder = new boolean[1]; final ToXContentObject[] informationContextHolder = new ToXContentObject[1]; final Exception[] exceptionHolder = new Exception[1]; - createRandomInstance().evaluateCondition(Metadata.builder().put(indexMetadata, true).build(), indexMetadata.getIndex(), + createRandomInstance().evaluateCondition( + Metadata.builder().put(indexMetadata, true).build(), + indexMetadata.getIndex(), new AsyncWaitStep.Listener() { @Override public void onResponse(boolean conditionMet, ToXContentObject informationContext) { @@ -117,7 +123,9 @@ public void onResponse(boolean conditionMet, ToXContentObject informationContext public void onFailure(Exception e) { exceptionHolder[0] = e; } - }, MASTER_TIMEOUT); + }, + MASTER_TIMEOUT + ); assertThat(conditionMetHolder[0], is(false)); assertThat(informationContextHolder[0], notNullValue()); @@ -139,7 +147,9 @@ public void testConditionNotMetNotAFollowerIndex() { final boolean[] conditionMetHolder = new boolean[1]; final ToXContentObject[] informationContextHolder = new ToXContentObject[1]; final Exception[] exceptionHolder = new Exception[1]; - createRandomInstance().evaluateCondition(Metadata.builder().put(indexMetadata, true).build(), indexMetadata.getIndex(), + createRandomInstance().evaluateCondition( + Metadata.builder().put(indexMetadata, true).build(), + indexMetadata.getIndex(), new AsyncWaitStep.Listener() { @Override public void onResponse(boolean conditionMet, ToXContentObject informationContext) { @@ -151,7 +161,9 @@ public void onResponse(boolean conditionMet, ToXContentObject informationContext public void onFailure(Exception e) { exceptionHolder[0] = e; } - }, MASTER_TIMEOUT); + }, + MASTER_TIMEOUT + ); assertThat(conditionMetHolder[0], is(true)); assertThat(informationContextHolder[0], nullValue()); @@ -200,8 +212,8 @@ private void mockFollowStatsCall(String expectedIndexName, List listener = - (ActionListener) invocationOnMock.getArguments()[2]; + ActionListener listener = (ActionListener) invocationOnMock + .getArguments()[2]; listener.onResponse(new FollowStatsAction.StatsResponses(Collections.emptyList(), Collections.emptyList(), statsResponses)); return null; }).when(client).execute(Mockito.eq(FollowStatsAction.INSTANCE), Mockito.any(), Mockito.any()); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForIndexColorStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForIndexColorStepTests.java index 86e7392b01b9f..10752b7bb610f 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForIndexColorStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForIndexColorStepTests.java @@ -28,7 +28,7 @@ public class WaitForIndexColorStepTests extends AbstractStepTestCase { private static ClusterHealthStatus randomColor() { - String[] colors = new String[]{"green", "yellow", "red"}; + String[] colors = new String[] { "green", "yellow", "red" }; int randomColor = randomIntBetween(0, colors.length - 1); return ClusterHealthStatus.fromString(colors[randomColor]); } @@ -83,10 +83,8 @@ public void testConditionMetForGreen() { .numberOfReplicas(2) .build(); - ShardRouting shardRouting = - TestShardRouting.newShardRouting("test_index", 0, "1", true, ShardRoutingState.STARTED); - IndexRoutingTable indexRoutingTable = IndexRoutingTable.builder(indexMetadata.getIndex()) - .addShard(shardRouting).build(); + ShardRouting shardRouting = TestShardRouting.newShardRouting("test_index", 0, "1", true, ShardRoutingState.STARTED); + IndexRoutingTable indexRoutingTable = IndexRoutingTable.builder(indexMetadata.getIndex()).addShard(shardRouting).build(); ClusterState clusterState = ClusterState.builder(new ClusterName("_name")) .metadata(Metadata.builder().put(indexMetadata, true).build()) @@ -106,10 +104,8 @@ public void testConditionNotMetForGreen() { .numberOfReplicas(0) .build(); - ShardRouting shardRouting = - TestShardRouting.newShardRouting("test_index", 0, "1", true, ShardRoutingState.INITIALIZING); - IndexRoutingTable indexRoutingTable = IndexRoutingTable.builder(indexMetadata.getIndex()) - .addShard(shardRouting).build(); + ShardRouting shardRouting = TestShardRouting.newShardRouting("test_index", 0, "1", true, ShardRoutingState.INITIALIZING); + IndexRoutingTable indexRoutingTable = IndexRoutingTable.builder(indexMetadata.getIndex()).addShard(shardRouting).build(); ClusterState clusterState = ClusterState.builder(new ClusterName("_name")) .metadata(Metadata.builder().put(indexMetadata, true).build()) @@ -151,10 +147,8 @@ public void testConditionMetForYellow() { .numberOfReplicas(0) .build(); - ShardRouting shardRouting = - TestShardRouting.newShardRouting("index2", 0, "1", true, ShardRoutingState.STARTED); - IndexRoutingTable indexRoutingTable = IndexRoutingTable.builder(indexMetadata.getIndex()) - .addShard(shardRouting).build(); + ShardRouting shardRouting = TestShardRouting.newShardRouting("index2", 0, "1", true, ShardRoutingState.STARTED); + IndexRoutingTable indexRoutingTable = IndexRoutingTable.builder(indexMetadata.getIndex()).addShard(shardRouting).build(); ClusterState clusterState = ClusterState.builder(new ClusterName("_name")) .metadata(Metadata.builder().put(indexMetadata, true).build()) @@ -174,10 +168,8 @@ public void testConditionNotMetForYellow() { .numberOfReplicas(0) .build(); - ShardRouting shardRouting = - TestShardRouting.newShardRouting("index2", 0, "1", true, ShardRoutingState.INITIALIZING); - IndexRoutingTable indexRoutingTable = IndexRoutingTable.builder(indexMetadata.getIndex()) - .addShard(shardRouting).build(); + ShardRouting shardRouting = TestShardRouting.newShardRouting("index2", 0, "1", true, ShardRoutingState.INITIALIZING); + IndexRoutingTable indexRoutingTable = IndexRoutingTable.builder(indexMetadata.getIndex()).addShard(shardRouting).build(); ClusterState clusterState = ClusterState.builder(new ClusterName("_name")) .metadata(Metadata.builder().put(indexMetadata, true).build()) @@ -220,10 +212,14 @@ public void testStepReturnsFalseIfTargetIndexIsMissing() { .build(); String indexPrefix = randomAlphaOfLengthBetween(5, 10) + "-"; - ShardRouting shardRouting = - TestShardRouting.newShardRouting(originalIndex.getIndex().getName(), 0, "1", true, ShardRoutingState.STARTED); - IndexRoutingTable indexRoutingTable = IndexRoutingTable.builder(originalIndex.getIndex()) - .addShard(shardRouting).build(); + ShardRouting shardRouting = TestShardRouting.newShardRouting( + originalIndex.getIndex().getName(), + 0, + "1", + true, + ShardRoutingState.STARTED + ); + IndexRoutingTable indexRoutingTable = IndexRoutingTable.builder(originalIndex.getIndex()).addShard(shardRouting).build(); ClusterState clusterState = ClusterState.builder(new ClusterName("_name")) .metadata(Metadata.builder().put(originalIndex, true).build()) @@ -235,8 +231,18 @@ public void testStepReturnsFalseIfTargetIndexIsMissing() { assertThat(result.isComplete(), is(false)); WaitForIndexColorStep.Info info = (WaitForIndexColorStep.Info) result.getInfomationContext(); String targetIndex = indexPrefix + originalIndex.getIndex().getName(); - assertThat(info.getMessage(), is("[" + step.getKey().getAction() + "] lifecycle action for index [" + - originalIndex.getIndex().getName() + "] executed but the target index [" + targetIndex + "] does not exist")); + assertThat( + info.getMessage(), + is( + "[" + + step.getKey().getAction() + + "] lifecycle action for index [" + + originalIndex.getIndex().getName() + + "] executed but the target index [" + + targetIndex + + "] does not exist" + ) + ); } public void testStepWaitsForTargetIndexHealthWhenPrefixConfigured() { @@ -245,10 +251,16 @@ public void testStepWaitsForTargetIndexHealthWhenPrefixConfigured() { .numberOfShards(1) .numberOfReplicas(2) .build(); - ShardRouting originalShardRouting = - TestShardRouting.newShardRouting(originalIndex.getIndex().getName(), 0, "1", true, ShardRoutingState.STARTED); + ShardRouting originalShardRouting = TestShardRouting.newShardRouting( + originalIndex.getIndex().getName(), + 0, + "1", + true, + ShardRoutingState.STARTED + ); IndexRoutingTable originalIndexRoutingTable = IndexRoutingTable.builder(originalIndex.getIndex()) - .addShard(originalShardRouting).build(); + .addShard(originalShardRouting) + .build(); String indexPrefix = randomAlphaOfLengthBetween(5, 10) + "-"; String targetIndexName = indexPrefix + originalIndex.getIndex().getName(); @@ -259,10 +271,16 @@ public void testStepWaitsForTargetIndexHealthWhenPrefixConfigured() { .build(); { - ShardRouting targetShardRouting = - TestShardRouting.newShardRouting(targetIndexName, 0, "1", true, ShardRoutingState.INITIALIZING); + ShardRouting targetShardRouting = TestShardRouting.newShardRouting( + targetIndexName, + 0, + "1", + true, + ShardRoutingState.INITIALIZING + ); IndexRoutingTable targetIndexRoutingTable = IndexRoutingTable.builder(originalIndex.getIndex()) - .addShard(targetShardRouting).build(); + .addShard(targetShardRouting) + .build(); ClusterState clusterTargetInitializing = ClusterState.builder(new ClusterName("_name")) .metadata(Metadata.builder().put(originalIndex, true).put(targetIndex, true).build()) @@ -277,10 +295,10 @@ public void testStepWaitsForTargetIndexHealthWhenPrefixConfigured() { } { - ShardRouting targetShardRouting = - TestShardRouting.newShardRouting(targetIndexName, 0, "1", true, ShardRoutingState.STARTED); + ShardRouting targetShardRouting = TestShardRouting.newShardRouting(targetIndexName, 0, "1", true, ShardRoutingState.STARTED); IndexRoutingTable targetIndexRoutingTable = IndexRoutingTable.builder(originalIndex.getIndex()) - .addShard(targetShardRouting).build(); + .addShard(targetShardRouting) + .build(); ClusterState clusterTargetInitializing = ClusterState.builder(new ClusterName("_name")) .metadata(Metadata.builder().put(originalIndex, true).put(targetIndex, true).build()) @@ -294,4 +312,3 @@ public void testStepWaitsForTargetIndexHealthWhenPrefixConfigured() { } } } - diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForIndexingCompleteStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForIndexingCompleteStepTests.java index a7764db2e74f9..2b3d203849d53 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForIndexingCompleteStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForIndexingCompleteStepTests.java @@ -106,16 +106,19 @@ public void testConditionNotMet() { ClusterStateWaitStep.Result result = step.isConditionMet(indexMetadata.getIndex(), clusterState); assertThat(result.isComplete(), is(false)); assertThat(result.getInfomationContext(), notNullValue()); - WaitForIndexingCompleteStep.IndexingNotCompleteInfo info = - (WaitForIndexingCompleteStep.IndexingNotCompleteInfo) result.getInfomationContext(); - assertThat(info.getMessage(), equalTo("waiting for the [index.lifecycle.indexing_complete] setting to be set to " + - "true on the leader index, it is currently [false]")); + WaitForIndexingCompleteStep.IndexingNotCompleteInfo info = (WaitForIndexingCompleteStep.IndexingNotCompleteInfo) result + .getInfomationContext(); + assertThat( + info.getMessage(), + equalTo( + "waiting for the [index.lifecycle.indexing_complete] setting to be set to " + + "true on the leader index, it is currently [false]" + ) + ); } public void testIndexDeleted() { - ClusterState clusterState = ClusterState.builder(new ClusterName("cluster")) - .metadata(Metadata.builder().build()) - .build(); + ClusterState clusterState = ClusterState.builder(new ClusterName("cluster")).metadata(Metadata.builder().build()).build(); WaitForIndexingCompleteStep step = createRandomInstance(); ClusterStateWaitStep.Result result = step.isConditionMet(new Index("this-index-doesnt-exist", "uuid"), clusterState); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForNoFollowersStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForNoFollowersStepTests.java index 18b0b1b2b0642..7ee0f0060c2fa 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForNoFollowersStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForNoFollowersStepTests.java @@ -17,12 +17,12 @@ import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.common.Strings; -import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.index.seqno.RetentionLease; import org.elasticsearch.index.seqno.RetentionLeaseStats; import org.elasticsearch.index.seqno.RetentionLeases; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.index.shard.ShardPath; +import org.elasticsearch.xcontent.ToXContentObject; import org.mockito.Mockito; import java.nio.file.Path; @@ -37,7 +37,6 @@ public class WaitForNoFollowersStepTests extends AbstractStepTestCase { - @Override protected WaitForNoFollowersStep createRandomInstance() { Step.StepKey stepKey = randomStepKey(); @@ -67,7 +66,7 @@ protected WaitForNoFollowersStep copyInstance(WaitForNoFollowersStep instance) { public void testConditionMet() { WaitForNoFollowersStep step = createRandomInstance(); - String indexName = randomAlphaOfLengthBetween(5,10); + String indexName = randomAlphaOfLengthBetween(5, 10); int numberOfShards = randomIntBetween(1, 100); final IndexMetadata indexMetadata = IndexMetadata.builder(indexName) @@ -100,7 +99,7 @@ public void onFailure(Exception e) { public void testConditionNotMet() { WaitForNoFollowersStep step = createRandomInstance(); - String indexName = randomAlphaOfLengthBetween(5,10); + String indexName = randomAlphaOfLengthBetween(5, 10); int numberOfShards = randomIntBetween(1, 100); final IndexMetadata indexMetadata = IndexMetadata.builder(indexName) @@ -127,14 +126,16 @@ public void onFailure(Exception e) { }, MASTER_TIMEOUT); assertFalse(conditionMetHolder.get()); - assertThat(Strings.toString(stepInfoHolder.get()), - containsString("this index is a leader index; waiting for all following indices to cease following before proceeding")); + assertThat( + Strings.toString(stepInfoHolder.get()), + containsString("this index is a leader index; waiting for all following indices to cease following before proceeding") + ); } public void testNoShardStats() { WaitForNoFollowersStep step = createRandomInstance(); - String indexName = randomAlphaOfLengthBetween(5,10); + String indexName = randomAlphaOfLengthBetween(5, 10); int numberOfShards = randomIntBetween(1, 100); final IndexMetadata indexMetadata = IndexMetadata.builder(indexName) @@ -170,7 +171,7 @@ public void onFailure(Exception e) { public void testFailure() { WaitForNoFollowersStep step = createRandomInstance(); - String indexName = randomAlphaOfLengthBetween(5,10); + String indexName = randomAlphaOfLengthBetween(5, 10); int numberOfShards = randomIntBetween(1, 100); IndexMetadata indexMetadata = IndexMetadata.builder(indexName) @@ -192,8 +193,12 @@ public void testFailure() { step.evaluateCondition(Metadata.builder().put(indexMetadata, true).build(), indexMetadata.getIndex(), new AsyncWaitStep.Listener() { @Override public void onResponse(boolean conditionMet, ToXContentObject infomationContext) { - fail("onResponse should not be called in this test, called with conditionMet: " + conditionMet - + " and stepInfo: " + Strings.toString(infomationContext)); + fail( + "onResponse should not be called in this test, called with conditionMet: " + + conditionMet + + " and stepInfo: " + + Strings.toString(infomationContext) + ); } @Override @@ -232,24 +237,26 @@ private IndexStats randomIndexStats(boolean isLeaderIndex, int numOfShards) { } private ShardStats randomShardStats(boolean isLeaderIndex) { - return new ShardStats(null, - mockShardPath(), - null, - null, - null, - randomRetentionLeaseStats(isLeaderIndex)); + return new ShardStats(null, mockShardPath(), null, null, null, randomRetentionLeaseStats(isLeaderIndex)); } private RetentionLeaseStats randomRetentionLeaseStats(boolean isLeaderIndex) { int numOfLeases = randomIntBetween(1, 10); ArrayList leases = new ArrayList<>(); - for (int i=0; i < numOfLeases; i++) { - leases.add(new RetentionLease(randomAlphaOfLength(5), randomNonNegativeLong(), randomNonNegativeLong(), - isLeaderIndex ? CCR_LEASE_KEY : randomAlphaOfLength(5))); + for (int i = 0; i < numOfLeases; i++) { + leases.add( + new RetentionLease( + randomAlphaOfLength(5), + randomNonNegativeLong(), + randomNonNegativeLong(), + isLeaderIndex ? CCR_LEASE_KEY : randomAlphaOfLength(5) + ) + ); } return new RetentionLeaseStats( - new RetentionLeases(randomLongBetween(1, Long.MAX_VALUE), randomLongBetween(1, Long.MAX_VALUE), leases)); + new RetentionLeases(randomLongBetween(1, Long.MAX_VALUE), randomLongBetween(1, Long.MAX_VALUE), leases) + ); } private ShardPath mockShardPath() { @@ -258,7 +265,7 @@ private ShardPath mockShardPath() { final Path getFileNameShardId = mock(Path.class); when(getFileNameShardId.toString()).thenReturn(Integer.toString(shardId)); - final String shardUuid = randomAlphaOfLength(5); + final String shardUuid = randomAlphaOfLength(5); final Path getFileNameShardUuid = mock(Path.class); when(getFileNameShardUuid.toString()).thenReturn(shardUuid); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForRolloverReadyStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForRolloverReadyStepTests.java index b811470fca3d0..c9e230582e0ea 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForRolloverReadyStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForRolloverReadyStepTests.java @@ -50,11 +50,11 @@ protected WaitForRolloverReadyStep createRandomInstance() { Step.StepKey stepKey = randomStepKey(); Step.StepKey nextStepKey = randomStepKey(); ByteSizeUnit maxSizeUnit = randomFrom(ByteSizeUnit.values()); - ByteSizeValue maxSize = randomBoolean() ? null : - new ByteSizeValue(randomNonNegativeLong() / maxSizeUnit.toBytes(1), maxSizeUnit); + ByteSizeValue maxSize = randomBoolean() ? null : new ByteSizeValue(randomNonNegativeLong() / maxSizeUnit.toBytes(1), maxSizeUnit); ByteSizeUnit maxPrimaryShardSizeUnit = randomFrom(ByteSizeUnit.values()); - ByteSizeValue maxPrimaryShardSize = randomBoolean() ? null : - new ByteSizeValue(randomNonNegativeLong() / maxPrimaryShardSizeUnit.toBytes(1), maxPrimaryShardSizeUnit); + ByteSizeValue maxPrimaryShardSize = randomBoolean() + ? null + : new ByteSizeValue(randomNonNegativeLong() / maxPrimaryShardSizeUnit.toBytes(1), maxPrimaryShardSizeUnit); Long maxDocs = randomBoolean() ? null : randomNonNegativeLong(); TimeValue maxAge = (maxDocs == null && maxSize == null || randomBoolean()) ? TimeValue.parseTimeValue(randomPositiveTimeValue(), "rollover_action_test") @@ -104,8 +104,15 @@ protected WaitForRolloverReadyStep mutateInstance(WaitForRolloverReadyStep insta @Override protected WaitForRolloverReadyStep copyInstance(WaitForRolloverReadyStep instance) { - return new WaitForRolloverReadyStep(instance.getKey(), instance.getNextStepKey(), instance.getClient(), - instance.getMaxSize(), instance.getMaxPrimaryShardSize(), instance.getMaxAge(), instance.getMaxDocs()); + return new WaitForRolloverReadyStep( + instance.getKey(), + instance.getNextStepKey(), + instance.getClient(), + instance.getMaxSize(), + instance.getMaxPrimaryShardSize(), + instance.getMaxAge(), + instance.getMaxDocs() + ); } private static void assertRolloverIndexRequest(RolloverRequest request, String rolloverTarget, Set> expectedConditions) { @@ -116,8 +123,7 @@ private static void assertRolloverIndexRequest(RolloverRequest request, String r assertEquals(expectedConditions.size(), request.getConditions().size()); assertTrue(request.isDryRun()); Set expectedConditionValues = expectedConditions.stream().map(Condition::value).collect(Collectors.toSet()); - Set actualConditionValues = request.getConditions().values().stream() - .map(Condition::value).collect(Collectors.toSet()); + Set actualConditionValues = request.getConditions().values().stream().map(Condition::value).collect(Collectors.toSet()); assertEquals(expectedConditionValues, actualConditionValues); } @@ -126,7 +132,9 @@ public void testEvaluateCondition() { IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) .putAlias(AliasMetadata.builder(alias).writeIndex(randomFrom(true, null))) .settings(settings(Version.CURRENT).put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); WaitForRolloverReadyStep step = createRandomInstance(); @@ -157,16 +165,18 @@ public void testEvaluateConditionOnDataStreamTarget() { String dataStreamName = "test-datastream"; IndexMetadata indexMetadata = IndexMetadata.builder(DataStream.getDefaultBackingIndexName(dataStreamName, 1)) .settings(settings(Version.CURRENT)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); WaitForRolloverReadyStep step = createRandomInstance(); mockRolloverIndexCall(dataStreamName, step); SetOnce conditionsMet = new SetOnce<>(); - Metadata metadata = Metadata.builder().put(indexMetadata, true) - .put(new DataStream(dataStreamName, createTimestampField("@timestamp"), - List.of(indexMetadata.getIndex()))) + Metadata metadata = Metadata.builder() + .put(indexMetadata, true) + .put(new DataStream(dataStreamName, createTimestampField("@timestamp"), List.of(indexMetadata.getIndex()))) .build(); step.evaluateCondition(metadata, indexMetadata.getIndex(), new AsyncWaitStep.Listener() { @@ -192,18 +202,28 @@ public void testSkipRolloverIfDataStreamIsAlreadyRolledOver() { String dataStreamName = "test-datastream"; IndexMetadata firstGenerationIndex = IndexMetadata.builder(DataStream.getDefaultBackingIndexName(dataStreamName, 1)) .settings(settings(Version.CURRENT)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); IndexMetadata writeIndex = IndexMetadata.builder(DataStream.getDefaultBackingIndexName(dataStreamName, 2)) .settings(settings(Version.CURRENT)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); WaitForRolloverReadyStep step = createRandomInstance(); SetOnce conditionsMet = new SetOnce<>(); - Metadata metadata = Metadata.builder().put(firstGenerationIndex, true) + Metadata metadata = Metadata.builder() + .put(firstGenerationIndex, true) .put(writeIndex, true) - .put(new DataStream(dataStreamName, createTimestampField("@timestamp"), - List.of(firstGenerationIndex.getIndex(), writeIndex.getIndex()))) + .put( + new DataStream( + dataStreamName, + createTimestampField("@timestamp"), + List.of(firstGenerationIndex.getIndex(), writeIndex.getIndex()) + ) + ) .build(); step.evaluateCondition(metadata, firstGenerationIndex.getIndex(), new AsyncWaitStep.Listener() { @@ -256,9 +276,16 @@ public void testEvaluateDoesntTriggerRolloverForIndexManuallyRolledOnLifecycleRo IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) .putAlias(AliasMetadata.builder(rolloverAlias)) .settings(settings(Version.CURRENT).put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, rolloverAlias)) - .putRolloverInfo(new RolloverInfo(rolloverAlias, Collections.singletonList(new MaxSizeCondition(new ByteSizeValue(2L))), - System.currentTimeMillis())) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + .putRolloverInfo( + new RolloverInfo( + rolloverAlias, + Collections.singletonList(new MaxSizeCondition(new ByteSizeValue(2L))), + System.currentTimeMillis() + ) + ) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); WaitForRolloverReadyStep step = createRandomInstance(); @@ -283,11 +310,16 @@ public void testEvaluateTriggersRolloverForIndexManuallyRolledOnDifferentAlias() IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) .putAlias(AliasMetadata.builder(rolloverAlias)) .settings(settings(Version.CURRENT).put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, rolloverAlias)) - .putRolloverInfo(new RolloverInfo(randomAlphaOfLength(5), - Collections.singletonList(new MaxSizeCondition(new ByteSizeValue(2L))), - System.currentTimeMillis()) + .putRolloverInfo( + new RolloverInfo( + randomAlphaOfLength(5), + Collections.singletonList(new MaxSizeCondition(new ByteSizeValue(2L))), + System.currentTimeMillis() + ) ) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); WaitForRolloverReadyStep step = createRandomInstance(); @@ -311,9 +343,10 @@ public void testPerformActionWriteIndexIsFalse() { String alias = randomAlphaOfLength(5); IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) .putAlias(AliasMetadata.builder(alias).writeIndex(false)) - .settings(settings(Version.CURRENT) - .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + .settings(settings(Version.CURRENT).put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); WaitForRolloverReadyStep step = createRandomInstance(); step.evaluateCondition(Metadata.builder().put(indexMetadata, true).build(), indexMetadata.getIndex(), new AsyncWaitStep.Listener() { @@ -325,8 +358,17 @@ public void onResponse(boolean complete, ToXContentObject infomationContext) { @Override public void onFailure(Exception e) { - assertThat(e.getMessage(), is(String.format(Locale.ROOT, "index [%s] is not the write index for alias [%s]", - indexMetadata.getIndex().getName(), alias))); + assertThat( + e.getMessage(), + is( + String.format( + Locale.ROOT, + "index [%s] is not the write index for alias [%s]", + indexMetadata.getIndex().getName(), + alias + ) + ) + ); } }, MASTER_TIMEOUT); @@ -337,10 +379,13 @@ public void testPerformActionWithIndexingComplete() { String alias = randomAlphaOfLength(5); IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) .putAlias(AliasMetadata.builder(alias).writeIndex(randomFrom(false, null))) - .settings(settings(Version.CURRENT) - .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias) - .put(LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE, true)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + .settings( + settings(Version.CURRENT).put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias) + .put(LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE, true) + ) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); WaitForRolloverReadyStep step = createRandomInstance(); @@ -365,10 +410,13 @@ public void testPerformActionWithIndexingCompleteStillWriteIndex() { String alias = randomAlphaOfLength(5); IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) .putAlias(AliasMetadata.builder(alias).writeIndex(true)) - .settings(settings(Version.CURRENT) - .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias) - .put(LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE, true)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + .settings( + settings(Version.CURRENT).put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias) + .put(LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE, true) + ) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); WaitForRolloverReadyStep step = createRandomInstance(); @@ -395,7 +443,9 @@ public void testPerformActionNotComplete() { IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) .putAlias(AliasMetadata.builder(alias)) .settings(settings(Version.CURRENT).put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); WaitForRolloverReadyStep step = createRandomInstance(); Mockito.doAnswer(invocation -> { @@ -448,7 +498,9 @@ public void testPerformActionFailure() { IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) .putAlias(AliasMetadata.builder(alias)) .settings(settings(Version.CURRENT).put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); Exception exception = new RuntimeException(); WaitForRolloverReadyStep step = createRandomInstance(); @@ -500,7 +552,9 @@ public void testPerformActionInvalidNullOrEmptyAlias() { String alias = randomBoolean() ? "" : null; IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) .settings(settings(Version.CURRENT).put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); WaitForRolloverReadyStep step = createRandomInstance(); SetOnce exceptionThrown = new SetOnce<>(); @@ -516,16 +570,26 @@ public void onFailure(Exception e) { } }, MASTER_TIMEOUT); assertThat(exceptionThrown.get().getClass(), equalTo(IllegalArgumentException.class)); - assertThat(exceptionThrown.get().getMessage(), equalTo(String.format(Locale.ROOT, - "setting [%s] for index [%s] is empty or not defined", RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, - indexMetadata.getIndex().getName()))); + assertThat( + exceptionThrown.get().getMessage(), + equalTo( + String.format( + Locale.ROOT, + "setting [%s] for index [%s] is empty or not defined", + RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, + indexMetadata.getIndex().getName() + ) + ) + ); } public void testPerformActionAliasDoesNotPointToIndex() { String alias = randomAlphaOfLength(5); IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) .settings(settings(Version.CURRENT).put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); WaitForRolloverReadyStep step = createRandomInstance(); SetOnce exceptionThrown = new SetOnce<>(); @@ -541,8 +605,17 @@ public void onFailure(Exception e) { } }, MASTER_TIMEOUT); assertThat(exceptionThrown.get().getClass(), equalTo(IllegalArgumentException.class)); - assertThat(exceptionThrown.get().getMessage(), equalTo(String.format(Locale.ROOT, - "%s [%s] does not point to index [%s]", RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias, - indexMetadata.getIndex().getName()))); + assertThat( + exceptionThrown.get().getMessage(), + equalTo( + String.format( + Locale.ROOT, + "%s [%s] does not point to index [%s]", + RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, + alias, + indexMetadata.getIndex().getName() + ) + ) + ); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForSnapshotStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForSnapshotStepTests.java index c3f0648ac43ef..0302d11d38139 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForSnapshotStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForSnapshotStepTests.java @@ -60,14 +60,18 @@ public void testNoSlmPolicies() { IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) .putCustom(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY, Map.of("action_time", Long.toString(randomLong()))) .settings(settings(Version.CURRENT)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); - ImmutableOpenMap.Builder indices = - ImmutableOpenMap.builder().fPut(indexMetadata.getIndex().getName(), indexMetadata); + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); + ImmutableOpenMap.Builder indices = ImmutableOpenMap.builder() + .fPut(indexMetadata.getIndex().getName(), indexMetadata); Metadata.Builder meta = Metadata.builder().indices(indices.build()); ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metadata(meta).build(); WaitForSnapshotStep instance = createRandomInstance(); - IllegalStateException e = expectThrows(IllegalStateException.class, () -> instance.isConditionMet(indexMetadata.getIndex(), - clusterState)); + IllegalStateException e = expectThrows( + IllegalStateException.class, + () -> instance.isConditionMet(indexMetadata.getIndex(), clusterState) + ); assertTrue(e.getMessage().contains(instance.getPolicy())); } @@ -77,16 +81,20 @@ public void testSlmPolicyNotExecuted() throws IOException { .setModifiedDate(randomLong()) .setPolicy(new SnapshotLifecyclePolicy("", "", "", "", null, null)) .build(); - SnapshotLifecycleMetadata smlMetadata = new SnapshotLifecycleMetadata(Map.of(instance.getPolicy(), slmPolicy), - OperationMode.RUNNING, null); - + SnapshotLifecycleMetadata smlMetadata = new SnapshotLifecycleMetadata( + Map.of(instance.getPolicy(), slmPolicy), + OperationMode.RUNNING, + null + ); IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) .putCustom(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY, Map.of("action_time", Long.toString(randomLong()))) .settings(settings(Version.CURRENT)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); - ImmutableOpenMap.Builder indices = - ImmutableOpenMap.builder().fPut(indexMetadata.getIndex().getName(), indexMetadata); + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); + ImmutableOpenMap.Builder indices = ImmutableOpenMap.builder() + .fPut(indexMetadata.getIndex().getName(), indexMetadata); Metadata.Builder meta = Metadata.builder().indices(indices.build()).putCustom(SnapshotLifecycleMetadata.TYPE, smlMetadata); ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metadata(meta).build(); ClusterStateWaitStep.Result result = instance.isConditionMet(indexMetadata.getIndex(), clusterState); @@ -116,19 +124,29 @@ private void assertSlmPolicyExecuted(boolean startTimeAfterPhaseTime, boolean fi SnapshotLifecyclePolicyMetadata slmPolicy = SnapshotLifecyclePolicyMetadata.builder() .setModifiedDate(randomLong()) .setPolicy(new SnapshotLifecyclePolicy("", "", "", "", null, null)) - .setLastSuccess(new SnapshotInvocationRecord("", - phaseTime + (startTimeAfterPhaseTime ? 10 : -100), - phaseTime + (finishTimeAfterPhaseTime ? 100 : -10), "")) + .setLastSuccess( + new SnapshotInvocationRecord( + "", + phaseTime + (startTimeAfterPhaseTime ? 10 : -100), + phaseTime + (finishTimeAfterPhaseTime ? 100 : -10), + "" + ) + ) .build(); - SnapshotLifecycleMetadata smlMetadata = new SnapshotLifecycleMetadata(Map.of(instance.getPolicy(), slmPolicy), - OperationMode.RUNNING, null); + SnapshotLifecycleMetadata smlMetadata = new SnapshotLifecycleMetadata( + Map.of(instance.getPolicy(), slmPolicy), + OperationMode.RUNNING, + null + ); IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) .putCustom(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY, Map.of("action_time", Long.toString(phaseTime))) .settings(settings(Version.CURRENT)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); - ImmutableOpenMap.Builder indices = - ImmutableOpenMap.builder().fPut(indexMetadata.getIndex().getName(), indexMetadata); + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); + ImmutableOpenMap.Builder indices = ImmutableOpenMap.builder() + .fPut(indexMetadata.getIndex().getName(), indexMetadata); Metadata.Builder meta = Metadata.builder().indices(indices.build()).putCustom(SnapshotLifecycleMetadata.TYPE, smlMetadata); ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metadata(meta).build(); ClusterStateWaitStep.Result result = instance.isConditionMet(indexMetadata.getIndex(), clusterState); @@ -148,23 +166,28 @@ public void testNullStartTime() throws IOException { SnapshotLifecyclePolicyMetadata slmPolicy = SnapshotLifecyclePolicyMetadata.builder() .setModifiedDate(randomLong()) .setPolicy(new SnapshotLifecyclePolicy("", "", "", "", null, null)) - .setLastSuccess(new SnapshotInvocationRecord("", - null, - phaseTime + 100, "")) + .setLastSuccess(new SnapshotInvocationRecord("", null, phaseTime + 100, "")) .build(); - SnapshotLifecycleMetadata smlMetadata = new SnapshotLifecycleMetadata(Map.of(instance.getPolicy(), slmPolicy), - OperationMode.RUNNING, null); + SnapshotLifecycleMetadata smlMetadata = new SnapshotLifecycleMetadata( + Map.of(instance.getPolicy(), slmPolicy), + OperationMode.RUNNING, + null + ); IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(10)) .putCustom(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY, Map.of("phase_time", Long.toString(phaseTime))) .settings(settings(Version.CURRENT)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); - ImmutableOpenMap.Builder indices = - ImmutableOpenMap.builder().fPut(indexMetadata.getIndex().getName(), indexMetadata); + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); + ImmutableOpenMap.Builder indices = ImmutableOpenMap.builder() + .fPut(indexMetadata.getIndex().getName(), indexMetadata); Metadata.Builder meta = Metadata.builder().indices(indices.build()).putCustom(SnapshotLifecycleMetadata.TYPE, smlMetadata); ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metadata(meta).build(); - IllegalStateException e = expectThrows(IllegalStateException.class, () -> instance.isConditionMet(indexMetadata.getIndex(), - clusterState)); + IllegalStateException e = expectThrows( + IllegalStateException.class, + () -> instance.isConditionMet(indexMetadata.getIndex(), clusterState) + ); assertTrue(e.getMessage().contains("no information about ILM action start")); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/action/GetLifecycleResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/action/GetLifecycleResponseTests.java index 4eca6e9a49048..32355b0d4abcc 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/action/GetLifecycleResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/action/GetLifecycleResponseTests.java @@ -31,8 +31,14 @@ protected Response createTestInstance() { String randomPrefix = randomAlphaOfLength(5); List responseItems = new ArrayList<>(); for (int i = 0; i < randomIntBetween(0, 2); i++) { - responseItems.add(new LifecyclePolicyResponseItem(randomTestLifecyclePolicy(randomPrefix + i), - randomNonNegativeLong(), randomAlphaOfLength(8), randomUsage())); + responseItems.add( + new LifecyclePolicyResponseItem( + randomTestLifecyclePolicy(randomPrefix + i), + randomNonNegativeLong(), + randomAlphaOfLength(8), + randomUsage() + ) + ); } return new Response(responseItems); } @@ -44,8 +50,11 @@ protected Writeable.Reader instanceReader() { protected NamedWriteableRegistry getNamedWriteableRegistry() { return new NamedWriteableRegistry( - Arrays.asList(new NamedWriteableRegistry.Entry(LifecycleAction.class, MockAction.NAME, MockAction::new), - new NamedWriteableRegistry.Entry(LifecycleType.class, TestLifecycleType.TYPE, in -> TestLifecycleType.INSTANCE))); + Arrays.asList( + new NamedWriteableRegistry.Entry(LifecycleAction.class, MockAction.NAME, MockAction::new), + new NamedWriteableRegistry.Entry(LifecycleType.class, TestLifecycleType.TYPE, in -> TestLifecycleType.INSTANCE) + ) + ); } @Override @@ -53,14 +62,26 @@ protected Response mutateInstance(Response response) { List responseItems = new ArrayList<>(response.getPolicies()); if (responseItems.size() > 0) { if (randomBoolean()) { - responseItems.add(new LifecyclePolicyResponseItem(randomTestLifecyclePolicy(randomAlphaOfLength(5)), - randomNonNegativeLong(), randomAlphaOfLength(4), randomUsage())); + responseItems.add( + new LifecyclePolicyResponseItem( + randomTestLifecyclePolicy(randomAlphaOfLength(5)), + randomNonNegativeLong(), + randomAlphaOfLength(4), + randomUsage() + ) + ); } else { responseItems.remove(0); } } else { - responseItems.add(new LifecyclePolicyResponseItem(randomTestLifecyclePolicy(randomAlphaOfLength(2)), - randomNonNegativeLong(), randomAlphaOfLength(4), randomUsage())); + responseItems.add( + new LifecyclePolicyResponseItem( + randomTestLifecyclePolicy(randomAlphaOfLength(2)), + randomNonNegativeLong(), + randomAlphaOfLength(4), + randomUsage() + ) + ); } return new Response(responseItems); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/action/MoveToStepRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/action/MoveToStepRequestTests.java index cc3d19ed55d75..3e2b4ddea0c56 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/action/MoveToStepRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/action/MoveToStepRequestTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.xpack.core.ilm.action; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ilm.Step.StepKey; import org.elasticsearch.xpack.core.ilm.StepKeyTests; import org.elasticsearch.xpack.core.ilm.action.MoveToStepAction.Request; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/action/PutLifecycleRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/action/PutLifecycleRequestTests.java index cf556cd62b6a1..cc23926f3545d 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/action/PutLifecycleRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/action/PutLifecycleRequestTests.java @@ -7,12 +7,12 @@ package org.elasticsearch.xpack.core.ilm.action; import org.elasticsearch.cluster.ClusterModule; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xpack.core.ilm.AllocateAction; import org.elasticsearch.xpack.core.ilm.DeleteAction; import org.elasticsearch.xpack.core.ilm.ForceMergeAction; @@ -66,8 +66,11 @@ protected Request doParseInstance(XContentParser parser) { protected NamedWriteableRegistry getNamedWriteableRegistry() { return new NamedWriteableRegistry( Arrays.asList( - new NamedWriteableRegistry.Entry(LifecycleType.class, TimeseriesLifecycleType.TYPE, - (in) -> TimeseriesLifecycleType.INSTANCE), + new NamedWriteableRegistry.Entry( + LifecycleType.class, + TimeseriesLifecycleType.TYPE, + (in) -> TimeseriesLifecycleType.INSTANCE + ), new NamedWriteableRegistry.Entry(LifecycleAction.class, AllocateAction.NAME, AllocateAction::new), new NamedWriteableRegistry.Entry(LifecycleAction.class, WaitForSnapshotAction.NAME, WaitForSnapshotAction::new), new NamedWriteableRegistry.Entry(LifecycleAction.class, DeleteAction.NAME, DeleteAction::new), @@ -81,31 +84,43 @@ protected NamedWriteableRegistry getNamedWriteableRegistry() { new NamedWriteableRegistry.Entry(LifecycleAction.class, MigrateAction.NAME, MigrateAction::new), new NamedWriteableRegistry.Entry(LifecycleAction.class, SearchableSnapshotAction.NAME, SearchableSnapshotAction::new), new NamedWriteableRegistry.Entry(LifecycleAction.class, RollupILMAction.NAME, RollupILMAction::new) - )); + ) + ); } @Override protected NamedXContentRegistry xContentRegistry() { List entries = new ArrayList<>(ClusterModule.getNamedXWriteables()); - entries.addAll(Arrays.asList( - new NamedXContentRegistry.Entry(LifecycleType.class, new ParseField(TimeseriesLifecycleType.TYPE), - (p) -> TimeseriesLifecycleType.INSTANCE), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(AllocateAction.NAME), AllocateAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, - new ParseField(WaitForSnapshotAction.NAME), WaitForSnapshotAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ForceMergeAction.NAME), ForceMergeAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ReadOnlyAction.NAME), ReadOnlyAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(RolloverAction.NAME), RolloverAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ShrinkAction.NAME), ShrinkAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(FreezeAction.NAME), FreezeAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(SetPriorityAction.NAME), SetPriorityAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(SearchableSnapshotAction.NAME), - SearchableSnapshotAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(MigrateAction.NAME), MigrateAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(UnfollowAction.NAME), UnfollowAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(RollupILMAction.NAME), RollupILMAction::parse) - )); + entries.addAll( + Arrays.asList( + new NamedXContentRegistry.Entry( + LifecycleType.class, + new ParseField(TimeseriesLifecycleType.TYPE), + (p) -> TimeseriesLifecycleType.INSTANCE + ), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(AllocateAction.NAME), AllocateAction::parse), + new NamedXContentRegistry.Entry( + LifecycleAction.class, + new ParseField(WaitForSnapshotAction.NAME), + WaitForSnapshotAction::parse + ), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ForceMergeAction.NAME), ForceMergeAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ReadOnlyAction.NAME), ReadOnlyAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(RolloverAction.NAME), RolloverAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ShrinkAction.NAME), ShrinkAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(FreezeAction.NAME), FreezeAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(SetPriorityAction.NAME), SetPriorityAction::parse), + new NamedXContentRegistry.Entry( + LifecycleAction.class, + new ParseField(SearchableSnapshotAction.NAME), + SearchableSnapshotAction::parse + ), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(MigrateAction.NAME), MigrateAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(UnfollowAction.NAME), UnfollowAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(RollupILMAction.NAME), RollupILMAction::parse) + ) + ); return new NamedXContentRegistry(entries); } @@ -116,8 +131,10 @@ protected boolean supportsUnknownFields() { @Override protected Request mutateInstance(Request request) { String name = randomBoolean() ? lifecycleName : randomAlphaOfLength(5); - LifecyclePolicy policy = randomValueOtherThan(request.getPolicy(), - () -> LifecyclePolicyTests.randomTimeseriesLifecyclePolicy(name)); + LifecyclePolicy policy = randomValueOtherThan( + request.getPolicy(), + () -> LifecyclePolicyTests.randomTimeseriesLifecyclePolicy(name) + ); return new Request(policy); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/action/RemoveIndexLifecyclePolicyRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/action/RemoveIndexLifecyclePolicyRequestTests.java index bb4dbc37c0731..9e1750c8b22b9 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/action/RemoveIndexLifecyclePolicyRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/action/RemoveIndexLifecyclePolicyRequestTests.java @@ -21,8 +21,16 @@ public class RemoveIndexLifecyclePolicyRequestTests extends AbstractWireSerializ protected Request createTestInstance() { Request request = new Request(generateRandomStringArray(20, 20, false)); if (randomBoolean()) { - IndicesOptions indicesOptions = IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), - randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean()); + IndicesOptions indicesOptions = IndicesOptions.fromOptions( + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean() + ); request.indicesOptions(indicesOptions); } if (randomBoolean()) { @@ -41,16 +49,29 @@ protected Request mutateInstance(Request instance) throws IOException { String[] indices = instance.indices(); IndicesOptions indicesOptions = instance.indicesOptions(); switch (between(0, 1)) { - case 0: - indices = randomValueOtherThanMany(i -> Arrays.equals(i, instance.indices()), - () -> generateRandomStringArray(20, 20, false)); - break; - case 1: - indicesOptions = randomValueOtherThan(indicesOptions, () -> IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), - randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean())); - break; - default: - throw new AssertionError("Illegal randomisation branch"); + case 0: + indices = randomValueOtherThanMany( + i -> Arrays.equals(i, instance.indices()), + () -> generateRandomStringArray(20, 20, false) + ); + break; + case 1: + indicesOptions = randomValueOtherThan( + indicesOptions, + () -> IndicesOptions.fromOptions( + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean() + ) + ); + break; + default: + throw new AssertionError("Illegal randomisation branch"); } Request newRequest = new Request(indices); newRequest.indicesOptions(indicesOptions); @@ -58,8 +79,7 @@ protected Request mutateInstance(Request instance) throws IOException { } public void testNullIndices() { - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, - () -> new Request((String[]) null)); + IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> new Request((String[]) null)); assertEquals("indices cannot be null", exception.getMessage()); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/action/RemoveIndexLifecyclePolicyResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/action/RemoveIndexLifecyclePolicyResponseTests.java index d5e05ea966555..0b387e525ddca 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/action/RemoveIndexLifecyclePolicyResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/action/RemoveIndexLifecyclePolicyResponseTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.xpack.core.ilm.action; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ilm.action.RemoveIndexLifecyclePolicyAction.Response; import java.io.IOException; @@ -33,8 +33,10 @@ protected Writeable.Reader instanceReader() { @Override protected Response mutateInstance(Response instance) throws IOException { - List failedIndices = randomValueOtherThan(instance.getFailedIndexes(), - () -> Arrays.asList(generateRandomStringArray(20, 20, false))); + List failedIndices = randomValueOtherThan( + instance.getFailedIndexes(), + () -> Arrays.asList(generateRandomStringArray(20, 20, false)) + ); return new Response(failedIndices); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/action/RetryRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/action/RetryRequestTests.java index 016ac32a7d66b..a853d2caf0b0d 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/action/RetryRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/action/RetryRequestTests.java @@ -24,8 +24,16 @@ protected Request createTestInstance() { request.indices(generateRandomStringArray(20, 20, false)); } if (randomBoolean()) { - IndicesOptions indicesOptions = IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), - randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean()); + IndicesOptions indicesOptions = IndicesOptions.fromOptions( + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean() + ); request.indicesOptions(indicesOptions); } return request; @@ -42,12 +50,25 @@ protected Request mutateInstance(Request instance) throws IOException { IndicesOptions indicesOptions = instance.indicesOptions(); switch (between(0, 1)) { case 0: - indices = randomValueOtherThanMany(i -> Arrays.equals(i, instance.indices()), - () -> generateRandomStringArray(20, 10, false, true)); + indices = randomValueOtherThanMany( + i -> Arrays.equals(i, instance.indices()), + () -> generateRandomStringArray(20, 10, false, true) + ); break; case 1: - indicesOptions = randomValueOtherThan(indicesOptions, () -> IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), - randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean())); + indicesOptions = randomValueOtherThan( + indicesOptions, + () -> IndicesOptions.fromOptions( + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean() + ) + ); break; default: throw new AssertionError("Illegal randomisation branch"); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/step/info/AllocationRoutedStepInfoTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/step/info/AllocationRoutedStepInfoTests.java index 4eb69a6f2e47a..43f6b73d4389a 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/step/info/AllocationRoutedStepInfoTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/step/info/AllocationRoutedStepInfoTests.java @@ -7,9 +7,9 @@ package org.elasticsearch.xpack.core.ilm.step.info; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.test.EqualsHashCodeTestUtils; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -37,8 +37,12 @@ public final void testEqualsAndHashcode() { } protected final AllocationInfo copyInstance(AllocationInfo instance) { - return new AllocationInfo(instance.getNumberOfReplicas(), instance.getNumberShardsLeftToAllocate(), instance.allShardsActive(), - instance.getMessage()); + return new AllocationInfo( + instance.getNumberOfReplicas(), + instance.getNumberShardsLeftToAllocate(), + instance.allShardsActive(), + instance.getMessage() + ); } protected AllocationInfo mutateInstance(AllocationInfo instance) throws IOException { @@ -47,20 +51,20 @@ protected AllocationInfo mutateInstance(AllocationInfo instance) throws IOExcept boolean allShardsActive = instance.allShardsActive(); var message = instance.getMessage(); switch (between(0, 2)) { - case 0: - shardsToAllocate += between(1, 20); - break; - case 1: - allShardsActive = allShardsActive == false; - break; - case 2: - actualReplicas += between(1, 20); - break; - case 3: - message = randomValueOtherThan(message, () -> randomAlphaOfLengthBetween(5, 10)); - break; - default: - throw new AssertionError("Illegal randomisation branch"); + case 0: + shardsToAllocate += between(1, 20); + break; + case 1: + allShardsActive = allShardsActive == false; + break; + case 2: + actualReplicas += between(1, 20); + break; + case 3: + message = randomValueOtherThan(message, () -> randomAlphaOfLengthBetween(5, 10)); + break; + default: + throw new AssertionError("Illegal randomisation branch"); } return new AllocationInfo(actualReplicas, shardsToAllocate, allShardsActive, message); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexing/AsyncTwoPhaseIndexerTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexing/AsyncTwoPhaseIndexerTests.java index 2f93e77e52d1e..56009e8bf9587 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexing/AsyncTwoPhaseIndexerTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexing/AsyncTwoPhaseIndexerTests.java @@ -17,7 +17,6 @@ import org.elasticsearch.action.search.SearchResponseSections; import org.elasticsearch.action.search.ShardSearchFailure; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.core.TimeValue; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; @@ -25,6 +24,7 @@ import org.elasticsearch.threadpool.ExecutorBuilder; import org.elasticsearch.threadpool.TestThreadPool; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.XContentBuilder; import org.junit.Before; import java.io.IOException; @@ -59,8 +59,14 @@ private class MockIndexer extends AsyncTwoPhaseIndexer { private final boolean stoppedBeforeFinished; private final boolean noIndices; - protected MockIndexer(ThreadPool threadPool, AtomicReference initialState, - Integer initialPosition, CountDownLatch latch, boolean stoppedBeforeFinished, boolean noIndices) { + protected MockIndexer( + ThreadPool threadPool, + AtomicReference initialState, + Integer initialPosition, + CountDownLatch latch, + boolean stoppedBeforeFinished, + boolean noIndices + ) { super(threadPool, initialState, initialPosition, new MockJobStats()); this.latch = latch; this.stoppedBeforeFinished = stoppedBeforeFinished; @@ -110,8 +116,14 @@ protected void doNextSearch(long waitTimeInNanos, ActionListener } final SearchResponseSections sections = new SearchResponseSections( - new SearchHits(new SearchHit[0], new TotalHits(0, TotalHits.Relation.EQUAL_TO), 0), null, - null, false, null, null, 1); + new SearchHits(new SearchHit[0], new TotalHits(0, TotalHits.Relation.EQUAL_TO), 0), + null, + null, + false, + null, + null, + 1 + ); nextPhase.onResponse(new SearchResponse(sections, null, 1, 1, 0, 0, ShardSearchFailure.EMPTY_ARRAY, null)); } @@ -149,8 +161,7 @@ protected void onStop() { } @Override - protected void onAbort() { - } + protected void onAbort() {} public int getStep() { return step; @@ -171,8 +182,13 @@ private class MockIndexerFiveRuns extends AsyncTwoPhaseIndexer initialState, - Integer initialPosition, float maxDocsPerSecond, CountDownLatch latch) { + protected MockIndexerFiveRuns( + ThreadPool threadPool, + AtomicReference initialState, + Integer initialPosition, + float maxDocsPerSecond, + CountDownLatch latch + ) { super(threadPool, initialState, initialPosition, new MockJobStats()); startTime = System.nanoTime(); this.latch = latch; @@ -202,8 +218,7 @@ protected IterationResult doProcess(SearchResponse searchResponse) { ++processOps; if (processOps == 5) { return new IterationResult<>(Stream.of(new IndexRequest()), processOps, true); - } - else if (processOps % 2 == 0) { + } else if (processOps % 2 == 0) { return new IterationResult<>(Stream.empty(), processOps, false); } @@ -237,8 +252,14 @@ public boolean waitingForLatchCountDown() { protected void doNextSearch(long waitTimeInNanos, ActionListener nextPhase) { ++searchOps; final SearchResponseSections sections = new SearchResponseSections( - new SearchHits(new SearchHit[0], new TotalHits(0, TotalHits.Relation.EQUAL_TO), 0), null, - null, false, null, null, 1); + new SearchHits(new SearchHit[0], new TotalHits(0, TotalHits.Relation.EQUAL_TO), 0), + null, + null, + false, + null, + null, + 1 + ); if (processOps == 3) { awaitForLatch(); @@ -275,8 +296,7 @@ protected void onStop() { } @Override - protected void onAbort() { - } + protected void onAbort() {} @Override protected long getTimeNanos() { @@ -297,8 +317,12 @@ private class MockIndexerThrowsFirstSearch extends AsyncTwoPhaseIndexer initialState, - Integer initialPosition) { + protected MockIndexerThrowsFirstSearch( + ThreadPool threadPool, + String executorName, + AtomicReference initialState, + Integer initialPosition + ) { super(threadPool, initialState, initialPosition, new MockJobStats()); } @@ -504,8 +528,7 @@ public void doTestFiveRuns(float docsPerSecond, Collection expectedDe AtomicReference state = new AtomicReference<>(IndexerState.STOPPED); final MockThreadPool threadPool = new MockThreadPool(getTestName()); try { - MockIndexerFiveRuns indexer = new MockIndexerFiveRuns (threadPool, state, 2, docsPerSecond, - null); + MockIndexerFiveRuns indexer = new MockIndexerFiveRuns(threadPool, state, 2, docsPerSecond, null); indexer.start(); assertThat(indexer.getState(), equalTo(IndexerState.STARTED)); assertTrue(indexer.maybeTriggerAsyncJob(System.currentTimeMillis())); @@ -533,18 +556,14 @@ public void testFiveRunsRethrottle1000_100() throws Exception { doTestFiveRunsRethrottle(1000, 100, timeValueCollectionFromMilliseconds(950L, 950L, 950L, 9950L)); } - public void doTestFiveRunsRethrottle( - float docsPerSecond, - float docsPerSecondRethrottle, - Collection expectedDelays - ) throws Exception { + public void doTestFiveRunsRethrottle(float docsPerSecond, float docsPerSecondRethrottle, Collection expectedDelays) + throws Exception { AtomicReference state = new AtomicReference<>(IndexerState.STOPPED); final MockThreadPool threadPool = new MockThreadPool(getTestName()); try { CountDownLatch latch = new CountDownLatch(1); - MockIndexerFiveRuns indexer = new MockIndexerFiveRuns (threadPool, state, 2, docsPerSecond, - latch); + MockIndexerFiveRuns indexer = new MockIndexerFiveRuns(threadPool, state, 2, docsPerSecond, latch); indexer.start(); assertThat(indexer.getState(), equalTo(IndexerState.STARTED)); assertTrue(indexer.maybeTriggerAsyncJob(System.currentTimeMillis())); @@ -597,7 +616,7 @@ public void testCalculateThrottlingDelay() { private static Collection timeValueCollectionFromMilliseconds(Long... milliseconds) { List timeValues = new ArrayList<>(); - for (Long m: milliseconds) { + for (Long m : milliseconds) { timeValues.add(TimeValue.timeValueMillis(m)); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexing/IndexerStateEnumTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexing/IndexerStateEnumTests.java index 6b2750e01c69e..52ec11402e136 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexing/IndexerStateEnumTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexing/IndexerStateEnumTests.java @@ -101,7 +101,7 @@ public void testInvalidReadFrom() throws Exception { try (StreamInput in = out.bytes().streamInput()) { IndexerState.fromStream(in); fail("Expected IOException"); - } catch(IOException e) { + } catch (IOException e) { assertThat(e.getMessage(), containsString("Unknown IndexerState ordinal [")); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/AbstractBWCSerializationTestCase.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/AbstractBWCSerializationTestCase.java index ae1863930d3a7..56364bb346997 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/AbstractBWCSerializationTestCase.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/AbstractBWCSerializationTestCase.java @@ -8,8 +8,8 @@ import org.elasticsearch.Version; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.ToXContent; import java.io.IOException; import java.util.List; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/MlTasksTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/MlTasksTests.java index 287b678a10c94..88e2231760a48 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/MlTasksTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/MlTasksTests.java @@ -37,8 +37,12 @@ public void testGetJobState() { // A missing task is a closed job assertEquals(JobState.CLOSED, MlTasks.getJobState("foo", tasksBuilder.build())); // A task with no status is opening - tasksBuilder.addTask(MlTasks.jobTaskId("foo"), MlTasks.JOB_TASK_NAME, new OpenJobAction.JobParams("foo"), - new PersistentTasksCustomMetadata.Assignment("bar", "test assignment")); + tasksBuilder.addTask( + MlTasks.jobTaskId("foo"), + MlTasks.JOB_TASK_NAME, + new OpenJobAction.JobParams("foo"), + new PersistentTasksCustomMetadata.Assignment("bar", "test assignment") + ); assertEquals(JobState.OPENING, MlTasks.getJobState("foo", tasksBuilder.build())); tasksBuilder.updateTaskState(MlTasks.jobTaskId("foo"), new JobTaskState(JobState.OPENED, tasksBuilder.getLastAllocationId(), null)); @@ -54,9 +58,12 @@ public void testGetDatefeedState() { // A missing task is a stopped datafeed assertEquals(DatafeedState.STOPPED, MlTasks.getDatafeedState("foo", tasksBuilder.build())); - tasksBuilder.addTask(MlTasks.datafeedTaskId("foo"), MlTasks.DATAFEED_TASK_NAME, - new StartDatafeedAction.DatafeedParams("foo", 0L), - new PersistentTasksCustomMetadata.Assignment("bar", "test assignment")); + tasksBuilder.addTask( + MlTasks.datafeedTaskId("foo"), + MlTasks.DATAFEED_TASK_NAME, + new StartDatafeedAction.DatafeedParams("foo", 0L), + new PersistentTasksCustomMetadata.Assignment("bar", "test assignment") + ); // A task with no state means the datafeed is starting assertEquals(DatafeedState.STARTING, MlTasks.getDatafeedState("foo", tasksBuilder.build())); @@ -68,8 +75,12 @@ public void testGetJobTask() { assertNull(MlTasks.getJobTask("foo", null)); PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); - tasksBuilder.addTask(MlTasks.jobTaskId("foo"), MlTasks.JOB_TASK_NAME, new OpenJobAction.JobParams("foo"), - new PersistentTasksCustomMetadata.Assignment("bar", "test assignment")); + tasksBuilder.addTask( + MlTasks.jobTaskId("foo"), + MlTasks.JOB_TASK_NAME, + new OpenJobAction.JobParams("foo"), + new PersistentTasksCustomMetadata.Assignment("bar", "test assignment") + ); assertNotNull(MlTasks.getJobTask("foo", tasksBuilder.build())); assertNull(MlTasks.getJobTask("other", tasksBuilder.build())); @@ -79,9 +90,12 @@ public void testGetDatafeedTask() { assertNull(MlTasks.getDatafeedTask("foo", null)); PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); - tasksBuilder.addTask(MlTasks.datafeedTaskId("foo"), MlTasks.DATAFEED_TASK_NAME, - new StartDatafeedAction.DatafeedParams("foo", 0L), - new PersistentTasksCustomMetadata.Assignment("bar", "test assignment")); + tasksBuilder.addTask( + MlTasks.datafeedTaskId("foo"), + MlTasks.DATAFEED_TASK_NAME, + new StartDatafeedAction.DatafeedParams("foo", 0L), + new PersistentTasksCustomMetadata.Assignment("bar", "test assignment") + ); assertNotNull(MlTasks.getDatafeedTask("foo", tasksBuilder.build())); assertNull(MlTasks.getDatafeedTask("other", tasksBuilder.build())); @@ -91,13 +105,24 @@ public void testOpenJobIds() { PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); assertThat(MlTasks.openJobIds(tasksBuilder.build()), empty()); - tasksBuilder.addTask(MlTasks.jobTaskId("foo-1"), MlTasks.JOB_TASK_NAME, new OpenJobAction.JobParams("foo-1"), - new PersistentTasksCustomMetadata.Assignment("node-1", "test assignment")); - tasksBuilder.addTask(MlTasks.jobTaskId("bar"), MlTasks.JOB_TASK_NAME, new OpenJobAction.JobParams("bar"), - new PersistentTasksCustomMetadata.Assignment("node-1", "test assignment")); - tasksBuilder.addTask(MlTasks.datafeedTaskId("df"), MlTasks.DATAFEED_TASK_NAME, - new StartDatafeedAction.DatafeedParams("df", 0L), - new PersistentTasksCustomMetadata.Assignment("node-1", "test assignment")); + tasksBuilder.addTask( + MlTasks.jobTaskId("foo-1"), + MlTasks.JOB_TASK_NAME, + new OpenJobAction.JobParams("foo-1"), + new PersistentTasksCustomMetadata.Assignment("node-1", "test assignment") + ); + tasksBuilder.addTask( + MlTasks.jobTaskId("bar"), + MlTasks.JOB_TASK_NAME, + new OpenJobAction.JobParams("bar"), + new PersistentTasksCustomMetadata.Assignment("node-1", "test assignment") + ); + tasksBuilder.addTask( + MlTasks.datafeedTaskId("df"), + MlTasks.DATAFEED_TASK_NAME, + new StartDatafeedAction.DatafeedParams("df", 0L), + new PersistentTasksCustomMetadata.Assignment("node-1", "test assignment") + ); assertThat(MlTasks.openJobIds(tasksBuilder.build()), containsInAnyOrder("foo-1", "bar")); } @@ -110,14 +135,24 @@ public void testStartedDatafeedIds() { PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); assertThat(MlTasks.openJobIds(tasksBuilder.build()), empty()); - tasksBuilder.addTask(MlTasks.jobTaskId("job-1"), MlTasks.JOB_TASK_NAME, new OpenJobAction.JobParams("foo-1"), - new PersistentTasksCustomMetadata.Assignment("node-1", "test assignment")); - tasksBuilder.addTask(MlTasks.datafeedTaskId("df1"), MlTasks.DATAFEED_TASK_NAME, - new StartDatafeedAction.DatafeedParams("df1", 0L), - new PersistentTasksCustomMetadata.Assignment("node-1", "test assignment")); - tasksBuilder.addTask(MlTasks.datafeedTaskId("df2"), MlTasks.DATAFEED_TASK_NAME, - new StartDatafeedAction.DatafeedParams("df2", 0L), - new PersistentTasksCustomMetadata.Assignment("node-2", "test assignment")); + tasksBuilder.addTask( + MlTasks.jobTaskId("job-1"), + MlTasks.JOB_TASK_NAME, + new OpenJobAction.JobParams("foo-1"), + new PersistentTasksCustomMetadata.Assignment("node-1", "test assignment") + ); + tasksBuilder.addTask( + MlTasks.datafeedTaskId("df1"), + MlTasks.DATAFEED_TASK_NAME, + new StartDatafeedAction.DatafeedParams("df1", 0L), + new PersistentTasksCustomMetadata.Assignment("node-1", "test assignment") + ); + tasksBuilder.addTask( + MlTasks.datafeedTaskId("df2"), + MlTasks.DATAFEED_TASK_NAME, + new StartDatafeedAction.DatafeedParams("df2", 0L), + new PersistentTasksCustomMetadata.Assignment("node-2", "test assignment") + ); assertThat(MlTasks.startedDatafeedIds(tasksBuilder.build()), containsInAnyOrder("df1", "df2")); } @@ -128,61 +163,89 @@ public void testStartedDatafeedIds_GivenNull() { public void testUnallocatedJobIds() { PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); - tasksBuilder.addTask(MlTasks.jobTaskId("job_with_assignment"), MlTasks.JOB_TASK_NAME, - new OpenJobAction.JobParams("job_with_assignment"), - new PersistentTasksCustomMetadata.Assignment("node-1", "test assignment")); - tasksBuilder.addTask(MlTasks.jobTaskId("job_without_assignment"), MlTasks.JOB_TASK_NAME, - new OpenJobAction.JobParams("job_without_assignment"), - new PersistentTasksCustomMetadata.Assignment(null, "test assignment")); - tasksBuilder.addTask(MlTasks.jobTaskId("job_without_node"), MlTasks.JOB_TASK_NAME, - new OpenJobAction.JobParams("job_without_node"), - new PersistentTasksCustomMetadata.Assignment("dead-node", "expired node")); + tasksBuilder.addTask( + MlTasks.jobTaskId("job_with_assignment"), + MlTasks.JOB_TASK_NAME, + new OpenJobAction.JobParams("job_with_assignment"), + new PersistentTasksCustomMetadata.Assignment("node-1", "test assignment") + ); + tasksBuilder.addTask( + MlTasks.jobTaskId("job_without_assignment"), + MlTasks.JOB_TASK_NAME, + new OpenJobAction.JobParams("job_without_assignment"), + new PersistentTasksCustomMetadata.Assignment(null, "test assignment") + ); + tasksBuilder.addTask( + MlTasks.jobTaskId("job_without_node"), + MlTasks.JOB_TASK_NAME, + new OpenJobAction.JobParams("job_without_node"), + new PersistentTasksCustomMetadata.Assignment("dead-node", "expired node") + ); DiscoveryNodes nodes = DiscoveryNodes.builder() - .add(new DiscoveryNode("node-1", new TransportAddress(InetAddress.getLoopbackAddress(), 9300), Version.CURRENT)) - .localNodeId("node-1") - .masterNodeId("node-1") - .build(); + .add(new DiscoveryNode("node-1", new TransportAddress(InetAddress.getLoopbackAddress(), 9300), Version.CURRENT)) + .localNodeId("node-1") + .masterNodeId("node-1") + .build(); - assertThat(MlTasks.unassignedJobIds(tasksBuilder.build(), nodes), - containsInAnyOrder("job_without_assignment", "job_without_node")); + assertThat(MlTasks.unassignedJobIds(tasksBuilder.build(), nodes), containsInAnyOrder("job_without_assignment", "job_without_node")); } public void testUnallocatedDatafeedIds() { PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); - tasksBuilder.addTask(MlTasks.datafeedTaskId("datafeed_with_assignment"), MlTasks.DATAFEED_TASK_NAME, - new StartDatafeedAction.DatafeedParams("datafeed_with_assignment", 0L), - new PersistentTasksCustomMetadata.Assignment("node-1", "test assignment")); - tasksBuilder.addTask(MlTasks.datafeedTaskId("datafeed_without_assignment"), MlTasks.DATAFEED_TASK_NAME, - new StartDatafeedAction.DatafeedParams("datafeed_without_assignment", 0L), - new PersistentTasksCustomMetadata.Assignment(null, "test assignment")); - tasksBuilder.addTask(MlTasks.datafeedTaskId("datafeed_without_node"), MlTasks.DATAFEED_TASK_NAME, - new StartDatafeedAction.DatafeedParams("datafeed_without_node", 0L), - new PersistentTasksCustomMetadata.Assignment("dead_node", "expired node")); - + tasksBuilder.addTask( + MlTasks.datafeedTaskId("datafeed_with_assignment"), + MlTasks.DATAFEED_TASK_NAME, + new StartDatafeedAction.DatafeedParams("datafeed_with_assignment", 0L), + new PersistentTasksCustomMetadata.Assignment("node-1", "test assignment") + ); + tasksBuilder.addTask( + MlTasks.datafeedTaskId("datafeed_without_assignment"), + MlTasks.DATAFEED_TASK_NAME, + new StartDatafeedAction.DatafeedParams("datafeed_without_assignment", 0L), + new PersistentTasksCustomMetadata.Assignment(null, "test assignment") + ); + tasksBuilder.addTask( + MlTasks.datafeedTaskId("datafeed_without_node"), + MlTasks.DATAFEED_TASK_NAME, + new StartDatafeedAction.DatafeedParams("datafeed_without_node", 0L), + new PersistentTasksCustomMetadata.Assignment("dead_node", "expired node") + ); DiscoveryNodes nodes = DiscoveryNodes.builder() - .add(new DiscoveryNode("node-1", new TransportAddress(InetAddress.getLoopbackAddress(), 9300), Version.CURRENT)) - .localNodeId("node-1") - .masterNodeId("node-1") - .build(); - - assertThat(MlTasks.unassignedDatafeedIds(tasksBuilder.build(), nodes), - containsInAnyOrder("datafeed_without_assignment", "datafeed_without_node")); + .add(new DiscoveryNode("node-1", new TransportAddress(InetAddress.getLoopbackAddress(), 9300), Version.CURRENT)) + .localNodeId("node-1") + .masterNodeId("node-1") + .build(); + + assertThat( + MlTasks.unassignedDatafeedIds(tasksBuilder.build(), nodes), + containsInAnyOrder("datafeed_without_assignment", "datafeed_without_node") + ); } public void testDatafeedTasksOnNode() { PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); assertThat(MlTasks.openJobIds(tasksBuilder.build()), empty()); - tasksBuilder.addTask(MlTasks.datafeedTaskId("df1"), MlTasks.DATAFEED_TASK_NAME, + tasksBuilder.addTask( + MlTasks.datafeedTaskId("df1"), + MlTasks.DATAFEED_TASK_NAME, new StartDatafeedAction.DatafeedParams("df1", 0L), - new PersistentTasksCustomMetadata.Assignment("node-1", "test assignment")); - tasksBuilder.addTask(MlTasks.jobTaskId("job-2"), MlTasks.JOB_TASK_NAME, new OpenJobAction.JobParams("foo-2"), - new PersistentTasksCustomMetadata.Assignment("node-2", "test assignment")); - tasksBuilder.addTask(MlTasks.datafeedTaskId("df2"), MlTasks.DATAFEED_TASK_NAME, + new PersistentTasksCustomMetadata.Assignment("node-1", "test assignment") + ); + tasksBuilder.addTask( + MlTasks.jobTaskId("job-2"), + MlTasks.JOB_TASK_NAME, + new OpenJobAction.JobParams("foo-2"), + new PersistentTasksCustomMetadata.Assignment("node-2", "test assignment") + ); + tasksBuilder.addTask( + MlTasks.datafeedTaskId("df2"), + MlTasks.DATAFEED_TASK_NAME, new StartDatafeedAction.DatafeedParams("df2", 0L), - new PersistentTasksCustomMetadata.Assignment("node-2", "test assignment")); + new PersistentTasksCustomMetadata.Assignment("node-2", "test assignment") + ); assertThat(MlTasks.datafeedTasksOnNode(tasksBuilder.build(), "node-2"), contains(hasProperty("id", equalTo("datafeed-df2")))); } @@ -191,43 +254,74 @@ public void testJobTasksOnNode() { PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); assertThat(MlTasks.openJobIds(tasksBuilder.build()), empty()); - tasksBuilder.addTask(MlTasks.jobTaskId("job-1"), MlTasks.JOB_TASK_NAME, new OpenJobAction.JobParams("foo-1"), - new PersistentTasksCustomMetadata.Assignment("node-1", "test assignment")); - tasksBuilder.addTask(MlTasks.datafeedTaskId("df1"), MlTasks.DATAFEED_TASK_NAME, + tasksBuilder.addTask( + MlTasks.jobTaskId("job-1"), + MlTasks.JOB_TASK_NAME, + new OpenJobAction.JobParams("foo-1"), + new PersistentTasksCustomMetadata.Assignment("node-1", "test assignment") + ); + tasksBuilder.addTask( + MlTasks.datafeedTaskId("df1"), + MlTasks.DATAFEED_TASK_NAME, new StartDatafeedAction.DatafeedParams("df1", 0L), - new PersistentTasksCustomMetadata.Assignment("node-1", "test assignment")); - tasksBuilder.addTask(MlTasks.jobTaskId("job-2"), MlTasks.JOB_TASK_NAME, new OpenJobAction.JobParams("foo-2"), - new PersistentTasksCustomMetadata.Assignment("node-2", "test assignment")); - tasksBuilder.addTask(MlTasks.datafeedTaskId("df2"), MlTasks.DATAFEED_TASK_NAME, + new PersistentTasksCustomMetadata.Assignment("node-1", "test assignment") + ); + tasksBuilder.addTask( + MlTasks.jobTaskId("job-2"), + MlTasks.JOB_TASK_NAME, + new OpenJobAction.JobParams("foo-2"), + new PersistentTasksCustomMetadata.Assignment("node-2", "test assignment") + ); + tasksBuilder.addTask( + MlTasks.datafeedTaskId("df2"), + MlTasks.DATAFEED_TASK_NAME, new StartDatafeedAction.DatafeedParams("df2", 0L), - new PersistentTasksCustomMetadata.Assignment("node-2", "test assignment")); - tasksBuilder.addTask(MlTasks.jobTaskId("job-3"), MlTasks.JOB_TASK_NAME, new OpenJobAction.JobParams("foo-3"), - new PersistentTasksCustomMetadata.Assignment("node-2", "test assignment")); - - assertThat(MlTasks.jobTasksOnNode(tasksBuilder.build(), "node-2"), - containsInAnyOrder(hasProperty("id", equalTo("job-job-2")), hasProperty("id", equalTo("job-job-3")))); + new PersistentTasksCustomMetadata.Assignment("node-2", "test assignment") + ); + tasksBuilder.addTask( + MlTasks.jobTaskId("job-3"), + MlTasks.JOB_TASK_NAME, + new OpenJobAction.JobParams("foo-3"), + new PersistentTasksCustomMetadata.Assignment("node-2", "test assignment") + ); + + assertThat( + MlTasks.jobTasksOnNode(tasksBuilder.build(), "node-2"), + containsInAnyOrder(hasProperty("id", equalTo("job-job-2")), hasProperty("id", equalTo("job-job-3"))) + ); } public void testNonFailedJobTasksOnNode() { PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); assertThat(MlTasks.openJobIds(tasksBuilder.build()), empty()); - tasksBuilder.addTask(MlTasks.jobTaskId("job-1"), MlTasks.JOB_TASK_NAME, new OpenJobAction.JobParams("foo-1"), - new PersistentTasksCustomMetadata.Assignment("node-1", "test assignment")); + tasksBuilder.addTask( + MlTasks.jobTaskId("job-1"), + MlTasks.JOB_TASK_NAME, + new OpenJobAction.JobParams("foo-1"), + new PersistentTasksCustomMetadata.Assignment("node-1", "test assignment") + ); tasksBuilder.updateTaskState(MlTasks.jobTaskId("job-1"), new JobTaskState(JobState.FAILED, 1, "testing")); - tasksBuilder.addTask(MlTasks.jobTaskId("job-2"), MlTasks.JOB_TASK_NAME, new OpenJobAction.JobParams("foo-2"), - new PersistentTasksCustomMetadata.Assignment("node-1", "test assignment")); + tasksBuilder.addTask( + MlTasks.jobTaskId("job-2"), + MlTasks.JOB_TASK_NAME, + new OpenJobAction.JobParams("foo-2"), + new PersistentTasksCustomMetadata.Assignment("node-1", "test assignment") + ); if (randomBoolean()) { tasksBuilder.updateTaskState(MlTasks.jobTaskId("job-2"), new JobTaskState(JobState.OPENED, 2, "testing")); } - tasksBuilder.addTask(MlTasks.jobTaskId("job-3"), MlTasks.JOB_TASK_NAME, new OpenJobAction.JobParams("foo-3"), - new PersistentTasksCustomMetadata.Assignment("node-2", "test assignment")); + tasksBuilder.addTask( + MlTasks.jobTaskId("job-3"), + MlTasks.JOB_TASK_NAME, + new OpenJobAction.JobParams("foo-3"), + new PersistentTasksCustomMetadata.Assignment("node-2", "test assignment") + ); if (randomBoolean()) { tasksBuilder.updateTaskState(MlTasks.jobTaskId("job-3"), new JobTaskState(JobState.FAILED, 3, "testing")); } - assertThat(MlTasks.nonFailedJobTasksOnNode(tasksBuilder.build(), "node-1"), - contains(hasProperty("id", equalTo("job-job-2")))); + assertThat(MlTasks.nonFailedJobTasksOnNode(tasksBuilder.build(), "node-1"), contains(hasProperty("id", equalTo("job-job-2")))); } public void testGetDataFrameAnalyticsState_GivenNullTask() { @@ -246,8 +340,12 @@ public void testGetDataFrameAnalyticsState_GivenTaskWithNullState() { public void testGetDataFrameAnalyticsState_GivenTaskWithStartedState() { String jobId = "foo"; - PersistentTasksCustomMetadata.PersistentTask task = createDataFrameAnalyticsTask(jobId, "test_node", - DataFrameAnalyticsState.STARTED, false); + PersistentTasksCustomMetadata.PersistentTask task = createDataFrameAnalyticsTask( + jobId, + "test_node", + DataFrameAnalyticsState.STARTED, + false + ); DataFrameAnalyticsState state = MlTasks.getDataFrameAnalyticsState(task); @@ -256,8 +354,12 @@ public void testGetDataFrameAnalyticsState_GivenTaskWithStartedState() { public void testGetDataFrameAnalyticsState_GivenStaleTaskWithStartedState() { String jobId = "foo"; - PersistentTasksCustomMetadata.PersistentTask task = createDataFrameAnalyticsTask(jobId, "test_node", - DataFrameAnalyticsState.STARTED, true); + PersistentTasksCustomMetadata.PersistentTask task = createDataFrameAnalyticsTask( + jobId, + "test_node", + DataFrameAnalyticsState.STARTED, + true + ); DataFrameAnalyticsState state = MlTasks.getDataFrameAnalyticsState(task); @@ -266,8 +368,12 @@ public void testGetDataFrameAnalyticsState_GivenStaleTaskWithStartedState() { public void testGetDataFrameAnalyticsState_GivenTaskWithStoppingState() { String jobId = "foo"; - PersistentTasksCustomMetadata.PersistentTask task = createDataFrameAnalyticsTask(jobId, "test_node", - DataFrameAnalyticsState.STOPPING, false); + PersistentTasksCustomMetadata.PersistentTask task = createDataFrameAnalyticsTask( + jobId, + "test_node", + DataFrameAnalyticsState.STOPPING, + false + ); DataFrameAnalyticsState state = MlTasks.getDataFrameAnalyticsState(task); @@ -276,8 +382,12 @@ public void testGetDataFrameAnalyticsState_GivenTaskWithStoppingState() { public void testGetDataFrameAnalyticsState_GivenStaleTaskWithStoppingState() { String jobId = "foo"; - PersistentTasksCustomMetadata.PersistentTask task = createDataFrameAnalyticsTask(jobId, "test_node", - DataFrameAnalyticsState.STOPPING, true); + PersistentTasksCustomMetadata.PersistentTask task = createDataFrameAnalyticsTask( + jobId, + "test_node", + DataFrameAnalyticsState.STOPPING, + true + ); DataFrameAnalyticsState state = MlTasks.getDataFrameAnalyticsState(task); @@ -286,8 +396,12 @@ public void testGetDataFrameAnalyticsState_GivenStaleTaskWithStoppingState() { public void testGetDataFrameAnalyticsState_GivenTaskWithFailedState() { String jobId = "foo"; - PersistentTasksCustomMetadata.PersistentTask task = createDataFrameAnalyticsTask(jobId, "test_node", - DataFrameAnalyticsState.FAILED, false); + PersistentTasksCustomMetadata.PersistentTask task = createDataFrameAnalyticsTask( + jobId, + "test_node", + DataFrameAnalyticsState.FAILED, + false + ); DataFrameAnalyticsState state = MlTasks.getDataFrameAnalyticsState(task); @@ -296,24 +410,36 @@ public void testGetDataFrameAnalyticsState_GivenTaskWithFailedState() { public void testGetDataFrameAnalyticsState_GivenStaleTaskWithFailedState() { String jobId = "foo"; - PersistentTasksCustomMetadata.PersistentTask task = createDataFrameAnalyticsTask(jobId, "test_node", - DataFrameAnalyticsState.FAILED, true); + PersistentTasksCustomMetadata.PersistentTask task = createDataFrameAnalyticsTask( + jobId, + "test_node", + DataFrameAnalyticsState.FAILED, + true + ); DataFrameAnalyticsState state = MlTasks.getDataFrameAnalyticsState(task); assertThat(state, equalTo(DataFrameAnalyticsState.FAILED)); } - private static PersistentTasksCustomMetadata.PersistentTask createDataFrameAnalyticsTask(String jobId, String nodeId, - DataFrameAnalyticsState state, - boolean isStale) { + private static PersistentTasksCustomMetadata.PersistentTask createDataFrameAnalyticsTask( + String jobId, + String nodeId, + DataFrameAnalyticsState state, + boolean isStale + ) { PersistentTasksCustomMetadata.Builder builder = PersistentTasksCustomMetadata.builder(); - builder.addTask(MlTasks.dataFrameAnalyticsTaskId(jobId), MlTasks.DATA_FRAME_ANALYTICS_TASK_NAME, + builder.addTask( + MlTasks.dataFrameAnalyticsTaskId(jobId), + MlTasks.DATA_FRAME_ANALYTICS_TASK_NAME, new StartDataFrameAnalyticsAction.TaskParams(jobId, Version.CURRENT, false), - new PersistentTasksCustomMetadata.Assignment(nodeId, "test assignment")); + new PersistentTasksCustomMetadata.Assignment(nodeId, "test assignment") + ); if (state != null) { - builder.updateTaskState(MlTasks.dataFrameAnalyticsTaskId(jobId), - new DataFrameAnalyticsTaskState(state, builder.getLastAllocationId() - (isStale ? 1 : 0), null)); + builder.updateTaskState( + MlTasks.dataFrameAnalyticsTaskId(jobId), + new DataFrameAnalyticsTaskState(state, builder.getLastAllocationId() - (isStale ? 1 : 0), null) + ); } PersistentTasksCustomMetadata tasks = builder.build(); return tasks.getTask(MlTasks.dataFrameAnalyticsTaskId(jobId)); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/CloseJobActionRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/CloseJobActionRequestTests.java index b5cfe0cbea19b..4e3591c2e8f29 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/CloseJobActionRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/CloseJobActionRequestTests.java @@ -8,8 +8,8 @@ import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.CloseJobAction.Request; public class CloseJobActionRequestTests extends AbstractSerializingTestCase { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/CreateTrainedModelAllocationActionResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/CreateTrainedModelAllocationActionResponseTests.java index 3fcd6e89c79d3..86e5d8763a6c4 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/CreateTrainedModelAllocationActionResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/CreateTrainedModelAllocationActionResponseTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ml.action; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.CreateTrainedModelAllocationAction.Response; import org.elasticsearch.xpack.core.ml.inference.allocation.TrainedModelAllocationTests; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/DatafeedParamsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/DatafeedParamsTests.java index 8c4586a17df09..ed5606b254f49 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/DatafeedParamsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/DatafeedParamsTests.java @@ -9,8 +9,8 @@ import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.Arrays; @@ -22,8 +22,10 @@ protected StartDatafeedAction.DatafeedParams doParseInstance(XContentParser pars } public static StartDatafeedAction.DatafeedParams createDatafeedParams() { - StartDatafeedAction.DatafeedParams params = - new StartDatafeedAction.DatafeedParams(randomAlphaOfLength(10), randomNonNegativeLong()); + StartDatafeedAction.DatafeedParams params = new StartDatafeedAction.DatafeedParams( + randomAlphaOfLength(10), + randomNonNegativeLong() + ); if (randomBoolean()) { params.setEndTime(randomNonNegativeLong()); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/DeleteTrainedModelAliasActionRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/DeleteTrainedModelAliasActionRequestTests.java index cf45cde927c9b..c668881ded5ec 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/DeleteTrainedModelAliasActionRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/DeleteTrainedModelAliasActionRequestTests.java @@ -10,7 +10,6 @@ import org.elasticsearch.test.AbstractWireSerializingTestCase; import org.elasticsearch.xpack.core.ml.action.DeleteTrainedModelAliasAction.Request; - public class DeleteTrainedModelAliasActionRequestTests extends AbstractWireSerializingTestCase { @Override diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/EvaluateDataFrameActionRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/EvaluateDataFrameActionRequestTests.java index 53222d3dfffeb..e64229e9c2880 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/EvaluateDataFrameActionRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/EvaluateDataFrameActionRequestTests.java @@ -9,17 +9,17 @@ import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchModule; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.EvaluateDataFrameAction.Request; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.Evaluation; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.MlEvaluationNamedXContentProvider; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.classification.ClassificationTests; -import org.elasticsearch.xpack.core.ml.dataframe.evaluation.regression.RegressionTests; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.outlierdetection.OutlierDetectionTests; +import org.elasticsearch.xpack.core.ml.dataframe.evaluation.regression.RegressionTests; import org.elasticsearch.xpack.core.ml.utils.QueryProvider; import java.io.IOException; @@ -62,12 +62,12 @@ protected Request createTestInstance() { throw new UncheckedIOException(e); } } - Evaluation evaluation = - randomFrom(OutlierDetectionTests.createRandom(), ClassificationTests.createRandom(), RegressionTests.createRandom()); - return new Request() - .setIndices(indices) - .setQueryProvider(queryProvider) - .setEvaluation(evaluation); + Evaluation evaluation = randomFrom( + OutlierDetectionTests.createRandom(), + ClassificationTests.createRandom(), + RegressionTests.createRandom() + ); + return new Request().setIndices(indices).setQueryProvider(queryProvider).setEvaluation(evaluation); } @Override diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/EvaluateDataFrameActionResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/EvaluateDataFrameActionResponseTests.java index 7151ea75eee4e..163e57f23f6ff 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/EvaluateDataFrameActionResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/EvaluateDataFrameActionResponseTests.java @@ -12,8 +12,8 @@ import org.elasticsearch.xpack.core.ml.action.EvaluateDataFrameAction.Response; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationMetricResult; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.MlEvaluationNamedXContentProvider; -import org.elasticsearch.xpack.core.ml.dataframe.evaluation.classification.AucRocResultTests; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.classification.AccuracyResultTests; +import org.elasticsearch.xpack.core.ml.dataframe.evaluation.classification.AucRocResultTests; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.classification.MulticlassConfusionMatrixResultTests; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.classification.PrecisionResultTests; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.classification.RecallResultTests; @@ -41,9 +41,7 @@ protected Response createTestInstance() { List metrics; switch (evaluationName) { case OUTLIER_DETECTION: - metrics = randomSubsetOf( - List.of( - AucRocResultTests.createRandom())); + metrics = randomSubsetOf(List.of(AucRocResultTests.createRandom())); break; case CLASSIFICATION: metrics = randomSubsetOf( @@ -52,15 +50,19 @@ protected Response createTestInstance() { AccuracyResultTests.createRandom(), PrecisionResultTests.createRandom(), RecallResultTests.createRandom(), - MulticlassConfusionMatrixResultTests.createRandom())); - break; + MulticlassConfusionMatrixResultTests.createRandom() + ) + ); + break; case REGRESSION: metrics = randomSubsetOf( List.of( new MeanSquaredError.Result(randomDouble()), new MeanSquaredLogarithmicError.Result(randomDouble()), new Huber.Result(randomDouble()), - new RSquared.Result(randomDouble()))); + new RSquared.Result(randomDouble()) + ) + ); break; default: throw new AssertionError("Please add missing \"case\" variant to the \"switch\" statement"); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/ExplainDataFrameAnalyticsActionResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/ExplainDataFrameAnalyticsActionResponseTests.java index 91f3fecdb3cfd..2c7261247ffbd 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/ExplainDataFrameAnalyticsActionResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/ExplainDataFrameAnalyticsActionResponseTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ml.action; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.ExplainDataFrameAnalyticsAction.Response; import org.elasticsearch.xpack.core.ml.dataframe.explain.FieldSelection; import org.elasticsearch.xpack.core.ml.dataframe.explain.FieldSelectionTests; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/ForecastJobActionRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/ForecastJobActionRequestTests.java index b38e06cf05ee7..062707f39f2f0 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/ForecastJobActionRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/ForecastJobActionRequestTests.java @@ -10,8 +10,8 @@ import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.ForecastJobAction.Request; import static org.hamcrest.Matchers.equalTo; @@ -38,9 +38,9 @@ protected Request createTestInstance() { request.setExpiresIn(TimeValue.timeValueSeconds(randomIntBetween(0, 1_000_000)).getStringRep()); } if (randomBoolean()) { - request.setMaxModelMemory(randomLongBetween( - new ByteSizeValue(1, ByteSizeUnit.MB).getBytes(), - new ByteSizeValue(499, ByteSizeUnit.MB).getBytes())); + request.setMaxModelMemory( + randomLongBetween(new ByteSizeValue(1, ByteSizeUnit.MB).getBytes(), new ByteSizeValue(499, ByteSizeUnit.MB).getBytes()) + ); } return request; } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetBucketActionRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetBucketActionRequestTests.java index 6f1deaa2513e9..c7d2d3792d7bf 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetBucketActionRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetBucketActionRequestTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ml.action; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.action.util.PageParams; import org.elasticsearch.xpack.core.ml.action.GetBucketsAction.Request; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetBucketActionResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetBucketActionResponseTests.java index 2d7ca46413b49..79480c19e997b 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetBucketActionResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetBucketActionResponseTests.java @@ -8,8 +8,8 @@ import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.test.AbstractWireSerializingTestCase; -import org.elasticsearch.xpack.core.ml.action.GetBucketsAction.Response; import org.elasticsearch.xpack.core.action.util.QueryPage; +import org.elasticsearch.xpack.core.ml.action.GetBucketsAction.Response; import org.elasticsearch.xpack.core.ml.job.results.AnomalyRecord; import org.elasticsearch.xpack.core.ml.job.results.Bucket; import org.elasticsearch.xpack.core.ml.job.results.BucketInfluencer; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetCalendarEventsActionRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetCalendarEventsActionRequestTests.java index b2a8e180a96ba..d6e69d1f911b2 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetCalendarEventsActionRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetCalendarEventsActionRequestTests.java @@ -8,8 +8,8 @@ import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.action.util.PageParams; public class GetCalendarEventsActionRequestTests extends AbstractSerializingTestCase { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetCalendarsActionRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetCalendarsActionRequestTests.java index 81512b6f977ae..c33198a68cd0a 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetCalendarsActionRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetCalendarsActionRequestTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ml.action; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.action.util.PageParams; public class GetCalendarsActionRequestTests extends AbstractSerializingTestCase { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetCategoriesRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetCategoriesRequestTests.java index 7c1bcb9619095..f3825205434c0 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetCategoriesRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetCategoriesRequestTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ml.action; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.action.util.PageParams; public class GetCategoriesRequestTests extends AbstractSerializingTestCase { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetCategoriesResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetCategoriesResponseTests.java index 327a0f5d9064f..8aa0229eec96f 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetCategoriesResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetCategoriesResponseTests.java @@ -18,8 +18,11 @@ public class GetCategoriesResponseTests extends AbstractWireSerializingTestCase< @Override protected GetCategoriesAction.Response createTestInstance() { CategoryDefinition definition = new CategoryDefinition(randomAlphaOfLength(10)); - QueryPage queryPage = - new QueryPage<>(Collections.singletonList(definition), 1L, CategoryDefinition.RESULTS_FIELD); + QueryPage queryPage = new QueryPage<>( + Collections.singletonList(definition), + 1L, + CategoryDefinition.RESULTS_FIELD + ); return new GetCategoriesAction.Response(queryPage); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetDataFrameAnalyticsActionResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetDataFrameAnalyticsActionResponseTests.java index 32c0ff3f30604..d3531fef674b5 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetDataFrameAnalyticsActionResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetDataFrameAnalyticsActionResponseTests.java @@ -9,9 +9,9 @@ import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.search.SearchModule; import org.elasticsearch.test.AbstractWireSerializingTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.action.util.QueryPage; import org.elasticsearch.xpack.core.ml.action.GetDataFrameAnalyticsAction.Response; import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsConfig; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetDataFrameAnalyticsStatsActionResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetDataFrameAnalyticsStatsActionResponseTests.java index cb2886c59c871..5ceb9a1f33835 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetDataFrameAnalyticsStatsActionResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetDataFrameAnalyticsStatsActionResponseTests.java @@ -9,20 +9,20 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.test.AbstractWireSerializingTestCase; +import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xpack.core.action.util.QueryPage; import org.elasticsearch.xpack.core.ml.action.GetDataFrameAnalyticsStatsAction.Response; import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsConfigTests; import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsState; import org.elasticsearch.xpack.core.ml.dataframe.stats.AnalysisStats; import org.elasticsearch.xpack.core.ml.dataframe.stats.AnalysisStatsNamedWriteablesProvider; -import org.elasticsearch.xpack.core.ml.dataframe.stats.classification.ValidationLoss; -import org.elasticsearch.xpack.core.ml.dataframe.stats.common.MemoryUsage; -import org.elasticsearch.xpack.core.ml.dataframe.stats.common.MemoryUsageTests; import org.elasticsearch.xpack.core.ml.dataframe.stats.classification.ClassificationStatsTests; +import org.elasticsearch.xpack.core.ml.dataframe.stats.classification.ValidationLoss; import org.elasticsearch.xpack.core.ml.dataframe.stats.common.DataCounts; import org.elasticsearch.xpack.core.ml.dataframe.stats.common.DataCountsTests; +import org.elasticsearch.xpack.core.ml.dataframe.stats.common.MemoryUsage; +import org.elasticsearch.xpack.core.ml.dataframe.stats.common.MemoryUsageTests; import org.elasticsearch.xpack.core.ml.dataframe.stats.outlierdetection.OutlierDetectionStatsTests; import org.elasticsearch.xpack.core.ml.dataframe.stats.regression.RegressionStatsTests; import org.elasticsearch.xpack.core.ml.utils.PhaseProgress; @@ -54,7 +54,9 @@ public static Response randomResponse(int listSize) { : randomFrom( ClassificationStatsTests.createRandom(), OutlierDetectionStatsTests.createRandom(), - RegressionStatsTests.createRandom())); + RegressionStatsTests.createRandom() + ) + ); } private static Response randomResponse(int listSize, Supplier analysisStatsSupplier) { @@ -63,14 +65,22 @@ private static Response randomResponse(int listSize, Supplier ana String failureReason = randomBoolean() ? null : randomAlphaOfLength(10); int progressSize = randomIntBetween(2, 5); List progress = new ArrayList<>(progressSize); - IntStream.of(progressSize).forEach(progressIndex -> progress.add( - new PhaseProgress(randomAlphaOfLength(10), randomIntBetween(0, 100)))); + IntStream.of(progressSize) + .forEach(progressIndex -> progress.add(new PhaseProgress(randomAlphaOfLength(10), randomIntBetween(0, 100)))); DataCounts dataCounts = randomBoolean() ? null : DataCountsTests.createRandom(); MemoryUsage memoryUsage = randomBoolean() ? null : MemoryUsageTests.createRandom(); AnalysisStats analysisStats = analysisStatsSupplier.get(); - Response.Stats stats = new Response.Stats(DataFrameAnalyticsConfigTests.randomValidId(), - randomFrom(DataFrameAnalyticsState.values()), failureReason, progress, dataCounts, memoryUsage, analysisStats, null, - randomAlphaOfLength(20)); + Response.Stats stats = new Response.Stats( + DataFrameAnalyticsConfigTests.randomValidId(), + randomFrom(DataFrameAnalyticsState.values()), + failureReason, + progress, + dataCounts, + memoryUsage, + analysisStats, + null, + randomAlphaOfLength(20) + ); analytics.add(stats); } return new Response(new QueryPage<>(analytics, analytics.size(), GetDataFrameAnalyticsAction.Response.RESULTS_FIELD)); @@ -87,7 +97,8 @@ protected Writeable.Reader instanceReader() { } public void testStats_GivenNulls() { - Response.Stats stats = new Response.Stats(randomAlphaOfLength(10), + Response.Stats stats = new Response.Stats( + randomAlphaOfLength(10), randomFrom(DataFrameAnalyticsState.values()), null, Collections.emptyList(), @@ -105,8 +116,10 @@ public void testStats_GivenNulls() { public void testVerbose() { String foldValuesFieldName = ValidationLoss.FOLD_VALUES.getPreferredName(); // Create response for supervised analysis that is certain to contain fold_values field - Response response = - randomResponse(1, () -> randomFrom(ClassificationStatsTests.createRandom(), RegressionStatsTests.createRandom())); + Response response = randomResponse( + 1, + () -> randomFrom(ClassificationStatsTests.createRandom(), RegressionStatsTests.createRandom()) + ); // VERBOSE param defaults to "false", fold values *not* outputted assertThat(Strings.toString(response), not(containsString(foldValuesFieldName))); @@ -114,11 +127,13 @@ public void testVerbose() { // VERBOSE param explicitly set to "false", fold values *not* outputted assertThat( Strings.toString(response, new ToXContent.MapParams(Collections.singletonMap(Response.VERBOSE, "false"))), - not(containsString(foldValuesFieldName))); + not(containsString(foldValuesFieldName)) + ); // VERBOSE param explicitly set to "true", fold values outputted assertThat( Strings.toString(response, new ToXContent.MapParams(Collections.singletonMap(Response.VERBOSE, "true"))), - containsString(foldValuesFieldName)); + containsString(foldValuesFieldName) + ); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetDatafeedRunningStateActionResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetDatafeedRunningStateActionResponseTests.java index 8146db9fd350d..d9ca604c14fe3 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetDatafeedRunningStateActionResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetDatafeedRunningStateActionResponseTests.java @@ -23,9 +23,11 @@ static Response.RunningState randomRunningState() { @Override protected Response createTestInstance() { int listSize = randomInt(10); - return new Response(Stream.generate(() -> randomAlphaOfLength(10)) - .limit(listSize) - .collect(Collectors.toMap(Function.identity(), _unused -> randomRunningState()))); + return new Response( + Stream.generate(() -> randomAlphaOfLength(10)) + .limit(listSize) + .collect(Collectors.toMap(Function.identity(), _unused -> randomRunningState())) + ); } @Override diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetDatafeedStatsActionResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetDatafeedStatsActionResponseTests.java index fbde8f3f83bc2..68ed8d19e83c8 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetDatafeedStatsActionResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetDatafeedStatsActionResponseTests.java @@ -11,11 +11,11 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.transport.TransportAddress; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.test.AbstractWireSerializingTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.AbstractWireSerializingTestCase; import org.elasticsearch.xpack.core.action.util.QueryPage; import org.elasticsearch.xpack.core.ml.action.GetDatafeedsStatsAction.Response; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfig; @@ -49,10 +49,9 @@ protected Response createTestInstance() { for (int j = 0; j < listSize; j++) { String datafeedId = randomAlphaOfLength(10); DatafeedState datafeedState = randomFrom(DatafeedState.values()); - DiscoveryNode node = - randomBoolean() - ? null - : new DiscoveryNode("_id", new TransportAddress(InetAddress.getLoopbackAddress(), 9300), Version.CURRENT); + DiscoveryNode node = randomBoolean() + ? null + : new DiscoveryNode("_id", new TransportAddress(InetAddress.getLoopbackAddress(), 9300), Version.CURRENT); String explanation = randomBoolean() ? null : randomAlphaOfLength(3); DatafeedTimingStats timingStats = randomBoolean() ? null : DatafeedTimingStatsTests.createRandom(); Response.DatafeedStats datafeedStats = new Response.DatafeedStats( @@ -84,12 +83,15 @@ public void testDatafeedStatsToXContent() throws IOException { attributes.put("non-ml-attribute", "should be filtered out"); TransportAddress transportAddress = new TransportAddress(TransportAddress.META_ADDRESS, 9000); - DiscoveryNode node = new DiscoveryNode("df-node-name", "df-node-id", transportAddress, attributes, - Set.of(), - Version.CURRENT); + DiscoveryNode node = new DiscoveryNode("df-node-name", "df-node-id", transportAddress, attributes, Set.of(), Version.CURRENT); - DatafeedTimingStats timingStats = - new DatafeedTimingStats("my-job-id", 5, 10, 100.0, new ExponentialAverageCalculationContext(50.0, null, null)); + DatafeedTimingStats timingStats = new DatafeedTimingStats( + "my-job-id", + 5, + 10, + 100.0, + new ExponentialAverageCalculationContext(50.0, null, null) + ); Response.DatafeedStats stats = new Response.DatafeedStats( "df-id", diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetDatafeedsActionResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetDatafeedsActionResponseTests.java index 99252c201c408..3e5916c8abf6b 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetDatafeedsActionResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetDatafeedsActionResponseTests.java @@ -11,8 +11,8 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.search.SearchModule; import org.elasticsearch.test.AbstractWireSerializingTestCase; -import org.elasticsearch.xpack.core.ml.action.GetDatafeedsAction.Response; import org.elasticsearch.xpack.core.action.util.QueryPage; +import org.elasticsearch.xpack.core.ml.action.GetDatafeedsAction.Response; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfig; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfigTests; @@ -43,5 +43,4 @@ protected NamedWriteableRegistry getNamedWriteableRegistry() { return new NamedWriteableRegistry(searchModule.getNamedWriteables()); } - } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetDeploymentStatsActionResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetDeploymentStatsActionResponseTests.java index 66e99f754b8ee..405d799f627fa 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetDeploymentStatsActionResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetDeploymentStatsActionResponseTests.java @@ -39,7 +39,7 @@ protected Writeable.Reader instanceReader() { protected GetDeploymentStatsAction.Response createTestInstance() { int numStats = randomIntBetween(0, 2); var stats = new ArrayList(numStats); - for (var i=0; i s.getModelId())); @@ -48,20 +48,22 @@ protected GetDeploymentStatsAction.Response createTestInstance() { public void testAddFailedRoutes_GivenNoFailures() throws UnknownHostException { var response = createTestInstance(); - var modifed = GetDeploymentStatsAction.Response.addFailedRoutes(response, - Collections.emptyMap(), - buildNodes("node_foo")); + var modifed = GetDeploymentStatsAction.Response.addFailedRoutes(response, Collections.emptyMap(), buildNodes("node_foo")); assertEquals(response, modifed); } public void testAddFailedRoutes_GivenNoTaskResponses() throws UnknownHostException { - var emptyResponse = new GetDeploymentStatsAction.Response(Collections.emptyList(), - Collections.emptyList(), Collections.emptyList(), 0); + var emptyResponse = new GetDeploymentStatsAction.Response( + Collections.emptyList(), + Collections.emptyList(), + Collections.emptyList(), + 0 + ); Map> badRoutes = new HashMap<>(); - for (var modelId : new String[]{"model1", "model2"}) { + for (var modelId : new String[] { "model1", "model2" }) { Map nodeRoutes = new HashMap<>(); - for (var nodeId : new String[]{"nodeA", "nodeB"}) { + for (var nodeId : new String[] { "nodeA", "nodeB" }) { nodeRoutes.put(nodeId, new RoutingStateAndReason(RoutingState.FAILED, "failure reason")); } badRoutes.put(modelId, nodeRoutes); @@ -83,18 +85,22 @@ public void testAddFailedRoutes_GivenMixedResponses() throws UnknownHostExceptio DiscoveryNodes nodes = buildNodes("node1", "node2", "node3"); List nodeStatsList = new ArrayList<>(); - nodeStatsList.add(GetDeploymentStatsAction.Response.AllocationStats.NodeStats.forStartedState( - nodes.get("node1"), - randomNonNegativeLong(), - randomDoubleBetween(0.0, 100.0, true), - Instant.now() - )); - nodeStatsList.add(GetDeploymentStatsAction.Response.AllocationStats.NodeStats.forStartedState( - nodes.get("node2"), - randomNonNegativeLong(), - randomDoubleBetween(0.0, 100.0, true), - Instant.now() - )); + nodeStatsList.add( + GetDeploymentStatsAction.Response.AllocationStats.NodeStats.forStartedState( + nodes.get("node1"), + randomNonNegativeLong(), + randomDoubleBetween(0.0, 100.0, true), + Instant.now() + ) + ); + nodeStatsList.add( + GetDeploymentStatsAction.Response.AllocationStats.NodeStats.forStartedState( + nodes.get("node2"), + randomNonNegativeLong(), + randomDoubleBetween(0.0, 100.0, true), + Instant.now() + ) + ); var model1 = new GetDeploymentStatsAction.Response.AllocationStats( "model1", @@ -102,15 +108,15 @@ public void testAddFailedRoutes_GivenMixedResponses() throws UnknownHostExceptio randomBoolean() ? null : randomIntBetween(1, 8), randomBoolean() ? null : randomIntBetween(1, 8), randomBoolean() ? null : randomIntBetween(1, 10000), - nodeStatsList); + nodeStatsList + ); Map> badRoutes = new HashMap<>(); Map nodeRoutes = new HashMap<>(); nodeRoutes.put("node3", new RoutingStateAndReason(RoutingState.FAILED, "failed on node3")); badRoutes.put("model1", nodeRoutes); - var response = new GetDeploymentStatsAction.Response(Collections.emptyList(), Collections.emptyList(), - List.of(model1), 1); + var response = new GetDeploymentStatsAction.Response(Collections.emptyList(), Collections.emptyList(), List.of(model1), 1); var modified = GetDeploymentStatsAction.Response.addFailedRoutes(response, badRoutes, nodes); List results = modified.getStats().results(); @@ -128,18 +134,22 @@ public void testAddFailedRoutes_TaskResultIsOverwritten() throws UnknownHostExce DiscoveryNodes nodes = buildNodes("node1", "node2"); List nodeStatsList = new ArrayList<>(); - nodeStatsList.add(GetDeploymentStatsAction.Response.AllocationStats.NodeStats.forStartedState( - nodes.get("node1"), - randomNonNegativeLong(), - randomDoubleBetween(0.0, 100.0, true), - Instant.now() - )); - nodeStatsList.add(GetDeploymentStatsAction.Response.AllocationStats.NodeStats.forStartedState( - nodes.get("node2"), - randomNonNegativeLong(), - randomDoubleBetween(0.0, 100.0, true), - Instant.now() - )); + nodeStatsList.add( + GetDeploymentStatsAction.Response.AllocationStats.NodeStats.forStartedState( + nodes.get("node1"), + randomNonNegativeLong(), + randomDoubleBetween(0.0, 100.0, true), + Instant.now() + ) + ); + nodeStatsList.add( + GetDeploymentStatsAction.Response.AllocationStats.NodeStats.forStartedState( + nodes.get("node2"), + randomNonNegativeLong(), + randomDoubleBetween(0.0, 100.0, true), + Instant.now() + ) + ); var model1 = new GetDeploymentStatsAction.Response.AllocationStats( "model1", @@ -147,9 +157,9 @@ public void testAddFailedRoutes_TaskResultIsOverwritten() throws UnknownHostExce randomBoolean() ? null : randomIntBetween(1, 8), randomBoolean() ? null : randomIntBetween(1, 8), randomBoolean() ? null : randomIntBetween(1, 10000), - nodeStatsList); - var response = new GetDeploymentStatsAction.Response(Collections.emptyList(), Collections.emptyList(), - List.of(model1), 1); + nodeStatsList + ); + var response = new GetDeploymentStatsAction.Response(Collections.emptyList(), Collections.emptyList(), List.of(model1), 1); // failed state for node 2 conflicts with the task response Map> badRoutes = new HashMap<>(); @@ -168,8 +178,8 @@ public void testAddFailedRoutes_TaskResultIsOverwritten() throws UnknownHostExce assertEquals(RoutingState.FAILED, results.get(0).getNodeStats().get(1).getRoutingState().getState()); } - private DiscoveryNodes buildNodes(String ... nodeIds) throws UnknownHostException { - InetAddress inetAddress = InetAddress.getByAddress(new byte[]{(byte) 192, (byte) 168, (byte) 0, (byte) 1}); + private DiscoveryNodes buildNodes(String... nodeIds) throws UnknownHostException { + InetAddress inetAddress = InetAddress.getByAddress(new byte[] { (byte) 192, (byte) 168, (byte) 0, (byte) 1 }); DiscoveryNodes.Builder builder = DiscoveryNodes.builder(); int port = 9200; for (String nodeId : nodeIds) { @@ -184,16 +194,22 @@ private GetDeploymentStatsAction.Response.AllocationStats randomDeploymentStats( for (int i = 0; i < numNodes; i++) { var node = new DiscoveryNode("node_" + i, new TransportAddress(InetAddress.getLoopbackAddress(), 9300), Version.CURRENT); if (randomBoolean()) { - nodeStatsList.add(GetDeploymentStatsAction.Response.AllocationStats.NodeStats.forStartedState( - node, - randomNonNegativeLong(), - randomDoubleBetween(0.0, 100.0, true), - Instant.now() - )); + nodeStatsList.add( + GetDeploymentStatsAction.Response.AllocationStats.NodeStats.forStartedState( + node, + randomNonNegativeLong(), + randomDoubleBetween(0.0, 100.0, true), + Instant.now() + ) + ); } else { - nodeStatsList.add(GetDeploymentStatsAction.Response.AllocationStats.NodeStats.forNotStartedState( - node, randomFrom(RoutingState.values()), randomBoolean() ? null : "a good reason" - )); + nodeStatsList.add( + GetDeploymentStatsAction.Response.AllocationStats.NodeStats.forNotStartedState( + node, + randomFrom(RoutingState.values()), + randomBoolean() ? null : "a good reason" + ) + ); } } @@ -205,6 +221,7 @@ node, randomFrom(RoutingState.values()), randomBoolean() ? null : "a good reason randomBoolean() ? null : randomIntBetween(1, 8), randomBoolean() ? null : randomIntBetween(1, 8), randomBoolean() ? null : randomIntBetween(1, 10000), - nodeStatsList); + nodeStatsList + ); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetFiltersActionRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetFiltersActionRequestTests.java index 467001648e456..be05071abd492 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetFiltersActionRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetFiltersActionRequestTests.java @@ -13,7 +13,6 @@ public class GetFiltersActionRequestTests extends AbstractWireSerializingTestCase { - @Override protected Request createTestInstance() { if (randomBoolean()) { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetFiltersActionResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetFiltersActionResponseTests.java index 26a9f0b953498..b5c63df46c0db 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetFiltersActionResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetFiltersActionResponseTests.java @@ -8,8 +8,8 @@ import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.test.AbstractWireSerializingTestCase; -import org.elasticsearch.xpack.core.ml.action.GetFiltersAction.Response; import org.elasticsearch.xpack.core.action.util.QueryPage; +import org.elasticsearch.xpack.core.ml.action.GetFiltersAction.Response; import org.elasticsearch.xpack.core.ml.job.config.MlFilter; import org.elasticsearch.xpack.core.ml.job.config.MlFilterTests; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetInfluencersActionRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetInfluencersActionRequestTests.java index 1f70e1057a60f..0831eccac3b86 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetInfluencersActionRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetInfluencersActionRequestTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ml.action; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.action.util.PageParams; import org.elasticsearch.xpack.core.ml.action.GetInfluencersAction.Request; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetInfluencersActionResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetInfluencersActionResponseTests.java index 78872b37d02de..8590a0de81b9a 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetInfluencersActionResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetInfluencersActionResponseTests.java @@ -23,8 +23,13 @@ protected Response createTestInstance() { int listSize = randomInt(10); List hits = new ArrayList<>(listSize); for (int j = 0; j < listSize; j++) { - Influencer influencer = new Influencer(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20), - randomAlphaOfLengthBetween(1, 20), new Date(randomNonNegativeLong()), randomNonNegativeLong()); + Influencer influencer = new Influencer( + randomAlphaOfLengthBetween(1, 20), + randomAlphaOfLengthBetween(1, 20), + randomAlphaOfLengthBetween(1, 20), + new Date(randomNonNegativeLong()), + randomNonNegativeLong() + ); influencer.setInfluencerScore(randomDouble()); influencer.setInitialInfluencerScore(randomDouble()); influencer.setProbability(randomDouble()); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetJobStatsActionResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetJobStatsActionResponseTests.java index 46f55fa4fc863..a432482b8c0d3 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetJobStatsActionResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetJobStatsActionResponseTests.java @@ -12,8 +12,8 @@ import org.elasticsearch.common.transport.TransportAddress; import org.elasticsearch.core.TimeValue; import org.elasticsearch.test.AbstractWireSerializingTestCase; -import org.elasticsearch.xpack.core.ml.action.GetJobsStatsAction.Response; import org.elasticsearch.xpack.core.action.util.QueryPage; +import org.elasticsearch.xpack.core.ml.action.GetJobsStatsAction.Response; import org.elasticsearch.xpack.core.ml.job.config.Job; import org.elasticsearch.xpack.core.ml.job.config.JobState; import org.elasticsearch.xpack.core.ml.job.process.autodetect.state.DataCounts; @@ -45,15 +45,23 @@ protected Response createTestInstance() { ModelSizeStats sizeStats = randomBoolean() ? null : new ModelSizeStats.Builder("foo").build(); ForecastStats forecastStats = randomBoolean() ? null : new ForecastStatsTests().createTestInstance(); JobState jobState = randomFrom(EnumSet.allOf(JobState.class)); - DiscoveryNode node = - randomBoolean() - ? null - : new DiscoveryNode("_id", new TransportAddress(InetAddress.getLoopbackAddress(), 9300), Version.CURRENT); + DiscoveryNode node = randomBoolean() + ? null + : new DiscoveryNode("_id", new TransportAddress(InetAddress.getLoopbackAddress(), 9300), Version.CURRENT); String explanation = randomBoolean() ? null : randomAlphaOfLength(3); TimeValue openTime = randomBoolean() ? null : parseTimeValue(randomPositiveTimeValue(), "open_time-Test"); TimingStats timingStats = randomBoolean() ? null : TimingStatsTests.createTestInstance("foo"); - Response.JobStats jobStats = - new Response.JobStats(jobId, dataCounts, sizeStats, forecastStats, jobState, node, explanation, openTime, timingStats); + Response.JobStats jobStats = new Response.JobStats( + jobId, + dataCounts, + sizeStats, + forecastStats, + jobState, + node, + explanation, + openTime, + timingStats + ); jobStatsList.add(jobStats); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetJobsActionResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetJobsActionResponseTests.java index a7922db247a09..46407321d7a47 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetJobsActionResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetJobsActionResponseTests.java @@ -8,8 +8,8 @@ import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.test.AbstractWireSerializingTestCase; -import org.elasticsearch.xpack.core.ml.action.GetJobsAction.Response; import org.elasticsearch.xpack.core.action.util.QueryPage; +import org.elasticsearch.xpack.core.ml.action.GetJobsAction.Response; import org.elasticsearch.xpack.core.ml.job.config.Job; import org.elasticsearch.xpack.core.ml.job.config.JobTests; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetModelSnapshotsActionRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetModelSnapshotsActionRequestTests.java index 36b16bc82767b..0ae9ad0d50805 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetModelSnapshotsActionRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetModelSnapshotsActionRequestTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ml.action; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.action.util.PageParams; import org.elasticsearch.xpack.core.ml.action.GetModelSnapshotsAction.Request; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetModelSnapshotsActionResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetModelSnapshotsActionResponseTests.java index 91cf5d2fc7033..a0cf005428eaf 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetModelSnapshotsActionResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetModelSnapshotsActionResponseTests.java @@ -8,8 +8,8 @@ import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.test.AbstractWireSerializingTestCase; -import org.elasticsearch.xpack.core.ml.action.GetModelSnapshotsAction.Response; import org.elasticsearch.xpack.core.action.util.QueryPage; +import org.elasticsearch.xpack.core.ml.action.GetModelSnapshotsAction.Response; import org.elasticsearch.xpack.core.ml.job.process.autodetect.state.ModelSnapshot; import org.elasticsearch.xpack.core.ml.job.process.autodetect.state.ModelSnapshotTests; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetOverallBucketsActionRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetOverallBucketsActionRequestTests.java index aea852a96405e..c353917515cc1 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetOverallBucketsActionRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetOverallBucketsActionRequestTests.java @@ -8,8 +8,8 @@ import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.GetOverallBucketsAction.Request; public class GetOverallBucketsActionRequestTests extends AbstractSerializingTestCase { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetOverallBucketsActionResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetOverallBucketsActionResponseTests.java index 9976a0a0a90c0..1d8a85e36c8a5 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetOverallBucketsActionResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetOverallBucketsActionResponseTests.java @@ -8,8 +8,8 @@ import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.test.AbstractWireSerializingTestCase; -import org.elasticsearch.xpack.core.ml.action.GetOverallBucketsAction.Response; import org.elasticsearch.xpack.core.action.util.QueryPage; +import org.elasticsearch.xpack.core.ml.action.GetOverallBucketsAction.Response; import org.elasticsearch.xpack.core.ml.job.results.OverallBucket; import java.util.ArrayList; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetRecordsActionRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetRecordsActionRequestTests.java index f3dbfe21f2ec7..ba90ddb295122 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetRecordsActionRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetRecordsActionRequestTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ml.action; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.action.util.PageParams; import org.elasticsearch.xpack.core.ml.action.GetRecordsAction.Request; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetRecordsActionResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetRecordsActionResponseTests.java index 0c918435fd710..a96ce6b5e33b7 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetRecordsActionResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetRecordsActionResponseTests.java @@ -8,8 +8,8 @@ import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.test.AbstractWireSerializingTestCase; -import org.elasticsearch.xpack.core.ml.action.GetRecordsAction.Response; import org.elasticsearch.xpack.core.action.util.QueryPage; +import org.elasticsearch.xpack.core.ml.action.GetRecordsAction.Response; import org.elasticsearch.xpack.core.ml.job.results.AnomalyRecord; import java.util.ArrayList; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetTrainedModelsRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetTrainedModelsRequestTests.java index ee04c322fbdd2..469c37bf61346 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetTrainedModelsRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetTrainedModelsRequestTests.java @@ -10,8 +10,8 @@ import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xpack.core.action.util.PageParams; import org.elasticsearch.xpack.core.ml.AbstractBWCWireSerializationTestCase; -import org.elasticsearch.xpack.core.ml.action.GetTrainedModelsAction.Request; import org.elasticsearch.xpack.core.ml.action.GetTrainedModelsAction.Includes; +import org.elasticsearch.xpack.core.ml.action.GetTrainedModelsAction.Request; import java.util.HashSet; import java.util.Set; @@ -22,16 +22,20 @@ public class GetTrainedModelsRequestTests extends AbstractBWCWireSerializationTe @Override protected Request createTestInstance() { - Request request = new Request(randomAlphaOfLength(20), - randomBoolean() ? null : - randomList(10, () -> randomAlphaOfLength(10)), - randomBoolean() ? null : - Stream.generate(() -> randomFrom(Includes.DEFINITION, - Includes.TOTAL_FEATURE_IMPORTANCE, - Includes.FEATURE_IMPORTANCE_BASELINE, - Includes.HYPERPARAMETERS)) - .limit(4) - .collect(Collectors.toSet())); + Request request = new Request( + randomAlphaOfLength(20), + randomBoolean() ? null : randomList(10, () -> randomAlphaOfLength(10)), + randomBoolean() + ? null + : Stream.generate( + () -> randomFrom( + Includes.DEFINITION, + Includes.TOTAL_FEATURE_IMPORTANCE, + Includes.FEATURE_IMPORTANCE_BASELINE, + Includes.HYPERPARAMETERS + ) + ).limit(4).collect(Collectors.toSet()) + ); request.setPageParams(new PageParams(randomIntBetween(0, 100), randomIntBetween(0, 100))); return request; } @@ -48,10 +52,7 @@ protected Request mutateInstanceForVersion(Request instance, Version version) { if (instance.getIncludes().isIncludeModelDefinition()) { includes.add(Includes.DEFINITION); } - Request request = new Request( - instance.getResourceId(), - instance.getTags(), - includes); + Request request = new Request(instance.getResourceId(), instance.getTags(), includes); request.setPageParams(instance.getPageParams()); request.setAllowNoResources(instance.isAllowNoResources()); return request; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetTrainedModelsStatsActionResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetTrainedModelsStatsActionResponseTests.java index 21e3f5ff57f7a..40c29be287a1e 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetTrainedModelsStatsActionResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/GetTrainedModelsStatsActionResponseTests.java @@ -27,27 +27,31 @@ public class GetTrainedModelsStatsActionResponseTests extends AbstractBWCWireSer protected Response createTestInstance() { int listSize = randomInt(10); List trainedModelStats = Stream.generate(() -> randomAlphaOfLength(10)) - .limit(listSize).map(id -> - new Response.TrainedModelStats(id, + .limit(listSize) + .map( + id -> new Response.TrainedModelStats( + id, randomBoolean() ? randomIngestStats() : null, randomIntBetween(0, 10), - randomBoolean() ? InferenceStatsTests.createTestInstance(id, null) : null) + randomBoolean() ? InferenceStatsTests.createTestInstance(id, null) : null + ) ) .collect(Collectors.toList()); return new Response(new QueryPage<>(trainedModelStats, randomLongBetween(listSize, 1000), RESULTS_FIELD)); } private IngestStats randomIngestStats() { - List pipelineIds = Stream.generate(()-> randomAlphaOfLength(10)) + List pipelineIds = Stream.generate(() -> randomAlphaOfLength(10)) .limit(randomIntBetween(0, 10)) .collect(Collectors.toList()); return new IngestStats( new IngestStats.Stats(randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong()), pipelineIds.stream().map(id -> new IngestStats.PipelineStat(id, randomStats())).collect(Collectors.toList()), - pipelineIds.stream().collect(Collectors.toMap(Function.identity(), (v) -> randomProcessorStats()))); + pipelineIds.stream().collect(Collectors.toMap(Function.identity(), (v) -> randomProcessorStats())) + ); } - private IngestStats.Stats randomStats(){ + private IngestStats.Stats randomStats() { return new IngestStats.Stats(randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong()); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/InferTrainedModelDeploymentRequestsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/InferTrainedModelDeploymentRequestsTests.java index 3d3fa6c2f6425..9d4013fc8d06c 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/InferTrainedModelDeploymentRequestsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/InferTrainedModelDeploymentRequestsTests.java @@ -23,7 +23,6 @@ public class InferTrainedModelDeploymentRequestsTests extends AbstractWireSerializingTestCase { - private static InferenceConfigUpdate randomInferenceConfigUpdate() { return randomFrom(ZeroShotClassificationConfigUpdateTests.createRandom(), EmptyConfigUpdateTests.testInstance()); } @@ -35,8 +34,10 @@ protected Writeable.Reader instanceRe @Override protected InferTrainedModelDeploymentAction.Request createTestInstance() { - List> docs = randomList(5, () -> randomMap(1, 3, - () -> Tuple.tuple(randomAlphaOfLength(7), randomAlphaOfLength(7)))); + List> docs = randomList( + 5, + () -> randomMap(1, 3, () -> Tuple.tuple(randomAlphaOfLength(7), randomAlphaOfLength(7))) + ); return new InferTrainedModelDeploymentAction.Request( randomAlphaOfLength(4), diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/InternalInferModelActionRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/InternalInferModelActionRequestTests.java index abe8ef7ad2352..90c2aa7412935 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/InternalInferModelActionRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/InternalInferModelActionRequestTests.java @@ -25,33 +25,31 @@ import java.util.stream.Collectors; import java.util.stream.Stream; - public class InternalInferModelActionRequestTests extends AbstractBWCWireSerializationTestCase { @Override protected Request createTestInstance() { - return randomBoolean() ? - new Request( + return randomBoolean() + ? new Request( randomAlphaOfLength(10), Stream.generate(InternalInferModelActionRequestTests::randomMap).limit(randomInt(10)).collect(Collectors.toList()), randomInferenceConfigUpdate(), - randomBoolean()) : - new Request( - randomAlphaOfLength(10), - randomMap(), - randomInferenceConfigUpdate(), - randomBoolean()); + randomBoolean() + ) + : new Request(randomAlphaOfLength(10), randomMap(), randomInferenceConfigUpdate(), randomBoolean()); } private static InferenceConfigUpdate randomInferenceConfigUpdate() { - return randomFrom(RegressionConfigUpdateTests.randomRegressionConfigUpdate(), + return randomFrom( + RegressionConfigUpdateTests.randomRegressionConfigUpdate(), ClassificationConfigUpdateTests.randomClassificationConfigUpdate(), ResultsFieldUpdateTests.randomUpdate(), - EmptyConfigUpdateTests.testInstance()); + EmptyConfigUpdateTests.testInstance() + ); } private static Map randomMap() { - return Stream.generate(()-> randomAlphaOfLength(10)) + return Stream.generate(() -> randomAlphaOfLength(10)) .limit(randomInt(10)) .collect(Collectors.toMap(Function.identity(), (v) -> randomAlphaOfLength(10))); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/InternalInferModelActionResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/InternalInferModelActionResponseTests.java index 9732ff14963b0..11024bc98aab0 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/InternalInferModelActionResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/InternalInferModelActionResponseTests.java @@ -26,11 +26,10 @@ public class InternalInferModelActionResponseTests extends AbstractWireSerializi protected Response createTestInstance() { String resultType = randomFrom(ClassificationInferenceResults.NAME, RegressionInferenceResults.NAME); return new Response( - Stream.generate(() -> randomInferenceResult(resultType)) - .limit(randomIntBetween(0, 10)) - .collect(Collectors.toList()), + Stream.generate(() -> randomInferenceResult(resultType)).limit(randomIntBetween(0, 10)).collect(Collectors.toList()), randomAlphaOfLength(10), - randomBoolean()); + randomBoolean() + ); } private static InferenceResults randomInferenceResult(String resultType) { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/JobParamsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/JobParamsTests.java index d9b720c7f323a..1b87c9ee998a3 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/JobParamsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/JobParamsTests.java @@ -9,8 +9,8 @@ import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.job.config.JobTests; import java.io.IOException; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/OpenJobActionRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/OpenJobActionRequestTests.java index b8310fc15d5ac..ac5ddb500d003 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/OpenJobActionRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/OpenJobActionRequestTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ml.action; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.OpenJobAction.Request; public class OpenJobActionRequestTests extends AbstractSerializingTestCase { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PersistJobActionRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PersistJobActionRequestTests.java index 7c96918d3edf8..2830abf077f0c 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PersistJobActionRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PersistJobActionRequestTests.java @@ -9,7 +9,7 @@ import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.test.AbstractWireSerializingTestCase; -public class PersistJobActionRequestTests extends AbstractWireSerializingTestCase { +public class PersistJobActionRequestTests extends AbstractWireSerializingTestCase { @Override protected Writeable.Reader instanceReader() { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PostCalendarEventActionRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PostCalendarEventActionRequestTests.java index b522e52261b74..d86ac5c7f66f3 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PostCalendarEventActionRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PostCalendarEventActionRequestTests.java @@ -9,9 +9,9 @@ import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.test.AbstractWireSerializingTestCase; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.AbstractWireSerializingTestCase; import org.elasticsearch.xpack.core.ml.calendars.ScheduledEvent; import org.elasticsearch.xpack.core.ml.calendars.ScheduledEventTests; @@ -35,7 +35,7 @@ protected Writeable.Reader instanceReader() { private PostCalendarEventsAction.Request createTestInstance(String calendarId) { int numEvents = randomIntBetween(1, 10); List events = new ArrayList<>(); - for (int i=0; i PostCalendarEventsAction.Request.parseRequest("bar", parser)); - assertEquals("Inconsistent calendar_id; 'foo' specified in the body differs from 'bar' specified as a URL argument", - e.getMessage()); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> PostCalendarEventsAction.Request.parseRequest("bar", parser) + ); + assertEquals( + "Inconsistent calendar_id; 'foo' specified in the body differs from 'bar' specified as a URL argument", + e.getMessage() + ); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PostDataActionRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PostDataActionRequestTests.java index 702dbc13bbf08..aaf4c123a64b3 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PostDataActionRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PostDataActionRequestTests.java @@ -8,8 +8,8 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.test.AbstractWireSerializingTestCase; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.ml.job.config.DataDescription; public class PostDataActionRequestTests extends AbstractWireSerializingTestCase { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PreviewDataFrameAnalyticsActionRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PreviewDataFrameAnalyticsActionRequestTests.java index 2ed63e158fb4f..dd34dbfb7a221 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PreviewDataFrameAnalyticsActionRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PreviewDataFrameAnalyticsActionRequestTests.java @@ -21,7 +21,6 @@ import java.util.Collections; import java.util.List; - public class PreviewDataFrameAnalyticsActionRequestTests extends AbstractWireSerializingTestCase { @Override diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PreviewDataFrameAnalyticsActionResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PreviewDataFrameAnalyticsActionResponseTests.java index ac3629cb363a7..4fff8daff671a 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PreviewDataFrameAnalyticsActionResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PreviewDataFrameAnalyticsActionResponseTests.java @@ -21,9 +21,7 @@ public class PreviewDataFrameAnalyticsActionResponseTests extends AbstractWireSe @Override protected Response createTestInstance() { return new Response( - Stream.generate(() -> randomHashMap("foo", "bar", "baz")) - .limit(randomIntBetween(1, 10)) - .collect(Collectors.toList()) + Stream.generate(() -> randomHashMap("foo", "bar", "baz")).limit(randomIntBetween(1, 10)).collect(Collectors.toList()) ); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PreviewDatafeedActionRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PreviewDatafeedActionRequestTests.java index 1d33361863133..f0180dbc8cb6f 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PreviewDatafeedActionRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PreviewDatafeedActionRequestTests.java @@ -9,9 +9,9 @@ import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.search.SearchModule; import org.elasticsearch.test.AbstractWireSerializingTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.ml.action.PreviewDatafeedAction.Request; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfig; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfigBuilderTests; @@ -37,13 +37,10 @@ protected Request createTestInstance() { randomBoolean() ? JobTests.buildJobBuilder(jobId) : null ); case 2: - return new Request.Builder() - .setJobBuilder( - JobTests.buildJobBuilder(jobId).setDatafeed(DatafeedConfigBuilderTests.createRandomizedDatafeedConfigBuilder( - null, - null, - 3600000 - ))).build(); + return new Request.Builder().setJobBuilder( + JobTests.buildJobBuilder(jobId) + .setDatafeed(DatafeedConfigBuilderTests.createRandomizedDatafeedConfigBuilder(null, null, 3600000)) + ).build(); default: throw new IllegalArgumentException("Unexpected test state"); } @@ -61,40 +58,30 @@ public void testCtor() { public void testValidation() { String jobId = randomAlphaOfLength(10); - Request.Builder requestBuilder = new Request.Builder() - .setDatafeedId(randomAlphaOfLength(10)) + Request.Builder requestBuilder = new Request.Builder().setDatafeedId(randomAlphaOfLength(10)) .setDatafeedBuilder(new DatafeedConfig.Builder(DatafeedConfigTests.createRandomizedDatafeedConfig(jobId))) .setJobBuilder(randomBoolean() ? JobTests.buildJobBuilder(jobId) : null); IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, requestBuilder::build); - assertThat(ex.getMessage(), - containsString("[datafeed_id] cannot be supplied when either [job_config] or [datafeed_config] is present")); + assertThat( + ex.getMessage(), + containsString("[datafeed_id] cannot be supplied when either [job_config] or [datafeed_config] is present") + ); - requestBuilder.setJobBuilder(null) - .setDatafeedId(null) - .setDatafeedBuilder(new DatafeedConfig.Builder()); + requestBuilder.setJobBuilder(null).setDatafeedId(null).setDatafeedBuilder(new DatafeedConfig.Builder()); ex = expectThrows(IllegalArgumentException.class, requestBuilder::build); - assertThat(ex.getMessage(), - containsString("[datafeed_config.job_id] must be set or a [job_config] must be provided")); + assertThat(ex.getMessage(), containsString("[datafeed_config.job_id] must be set or a [job_config] must be provided")); - requestBuilder - .setJobBuilder( - JobTests.buildJobBuilder(jobId) - .setDatafeed(new DatafeedConfig.Builder(DatafeedConfigTests.createRandomizedDatafeedConfig(jobId))) - ) - .setDatafeedId(null) - .setDatafeedBuilder(new DatafeedConfig.Builder()); + requestBuilder.setJobBuilder( + JobTests.buildJobBuilder(jobId) + .setDatafeed(new DatafeedConfig.Builder(DatafeedConfigTests.createRandomizedDatafeedConfig(jobId))) + ).setDatafeedId(null).setDatafeedBuilder(new DatafeedConfig.Builder()); ex = expectThrows(IllegalArgumentException.class, requestBuilder::build); - assertThat(ex.getMessage(), - containsString("[datafeed_config] must not be present when a [job_config.datafeed_config] is present")); + assertThat(ex.getMessage(), containsString("[datafeed_config] must not be present when a [job_config.datafeed_config] is present")); - requestBuilder - .setJobBuilder(JobTests.buildJobBuilder(jobId)) - .setDatafeedId(null) - .setDatafeedBuilder(null); + requestBuilder.setJobBuilder(JobTests.buildJobBuilder(jobId)).setDatafeedId(null).setDatafeedBuilder(null); ex = expectThrows(IllegalArgumentException.class, requestBuilder::build); - assertThat(ex.getMessage(), - containsString("[datafeed_config] must be present when a [job_config.datafeed_config] is not present")); + assertThat(ex.getMessage(), containsString("[datafeed_config] must be present when a [job_config.datafeed_config] is not present")); } @Override diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PutCalendarActionRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PutCalendarActionRequestTests.java index a8f3e97d8cf1c..bf0372ab46e59 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PutCalendarActionRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PutCalendarActionRequestTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ml.action; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.calendars.CalendarTests; import org.elasticsearch.xpack.core.ml.job.config.JobTests; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PutDataFrameAnalyticsActionRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PutDataFrameAnalyticsActionRequestTests.java index 50ee59e3ba81a..69e4c04f9d07c 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PutDataFrameAnalyticsActionRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PutDataFrameAnalyticsActionRequestTests.java @@ -9,11 +9,11 @@ import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.SearchModule; import org.elasticsearch.search.fetch.subphase.FetchSourceContext; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.PutDataFrameAnalyticsAction.Request; import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsConfig; import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsConfigTests; @@ -80,11 +80,14 @@ protected Request doParseInstance(XContentParser parser) { } public void testValidate_GivenRequestWithIncludedAnalyzedFieldThatIsExcludedInSourceFiltering() { - DataFrameAnalyticsSource source = new DataFrameAnalyticsSource(new String[] {"index"}, null, - new FetchSourceContext(true, null, new String[] {"excluded"}), null); - FetchSourceContext analyzedFields = new FetchSourceContext(true, new String[] {"excluded"}, null); - DataFrameAnalyticsConfig config = new DataFrameAnalyticsConfig.Builder() - .setId("foo") + DataFrameAnalyticsSource source = new DataFrameAnalyticsSource( + new String[] { "index" }, + null, + new FetchSourceContext(true, null, new String[] { "excluded" }), + null + ); + FetchSourceContext analyzedFields = new FetchSourceContext(true, new String[] { "excluded" }, null); + DataFrameAnalyticsConfig config = new DataFrameAnalyticsConfig.Builder().setId("foo") .setSource(source) .setAnalysis(OutlierDetectionTests.createRandom()) .setAnalyzedFields(analyzedFields) @@ -98,11 +101,14 @@ public void testValidate_GivenRequestWithIncludedAnalyzedFieldThatIsExcludedInSo } public void testValidate_GivenRequestWithIncludedAnalyzedFieldThatIsIncludedInSourceFiltering() { - DataFrameAnalyticsSource source = new DataFrameAnalyticsSource(new String[] {"index"}, null, - new FetchSourceContext(true, new String[] {"included"}, null), null); - FetchSourceContext analyzedFields = new FetchSourceContext(true, new String[] {"included"}, null); - DataFrameAnalyticsConfig config = new DataFrameAnalyticsConfig.Builder() - .setId("foo") + DataFrameAnalyticsSource source = new DataFrameAnalyticsSource( + new String[] { "index" }, + null, + new FetchSourceContext(true, new String[] { "included" }, null), + null + ); + FetchSourceContext analyzedFields = new FetchSourceContext(true, new String[] { "included" }, null); + DataFrameAnalyticsConfig config = new DataFrameAnalyticsConfig.Builder().setId("foo") .setSource(source) .setAnalysis(OutlierDetectionTests.createRandom()) .setAnalyzedFields(analyzedFields) diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PutDataFrameAnalyticsActionResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PutDataFrameAnalyticsActionResponseTests.java index 95f0b7271f5bc..6542d92594f21 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PutDataFrameAnalyticsActionResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PutDataFrameAnalyticsActionResponseTests.java @@ -26,7 +26,7 @@ public class PutDataFrameAnalyticsActionResponseTests extends AbstractWireSerial protected NamedWriteableRegistry getNamedWriteableRegistry() { List namedWriteables = new ArrayList<>(); namedWriteables.addAll(new MlDataFrameAnalysisNamedXContentProvider().getNamedWriteables()); - namedWriteables.addAll(new MlInferenceNamedXContentProvider() .getNamedWriteables()); + namedWriteables.addAll(new MlInferenceNamedXContentProvider().getNamedWriteables()); namedWriteables.addAll(new SearchModule(Settings.EMPTY, Collections.emptyList()).getNamedWriteables()); return new NamedWriteableRegistry(namedWriteables); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PutDatafeedActionRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PutDatafeedActionRequestTests.java index ea012a1d966de..054d7c763b78e 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PutDatafeedActionRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PutDatafeedActionRequestTests.java @@ -10,10 +10,10 @@ import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.SearchModule; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.PutDatafeedAction.Request; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfigTests; import org.junit.Before; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PutDatafeedActionResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PutDatafeedActionResponseTests.java index 653befe288f6c..2a661263c6c22 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PutDatafeedActionResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PutDatafeedActionResponseTests.java @@ -23,7 +23,9 @@ public class PutDatafeedActionResponseTests extends AbstractWireSerializingTestC @Override protected Response createTestInstance() { DatafeedConfig.Builder datafeedConfig = new DatafeedConfig.Builder( - DatafeedConfigTests.randomValidDatafeedId(), randomAlphaOfLength(10)); + DatafeedConfigTests.randomValidDatafeedId(), + randomAlphaOfLength(10) + ); datafeedConfig.setIndices(Arrays.asList(randomAlphaOfLength(10))); return new Response(datafeedConfig.build()); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PutFilterActionRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PutFilterActionRequestTests.java index 03520de25a27a..dae8d2b39bf1f 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PutFilterActionRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PutFilterActionRequestTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ml.action; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.PutFilterAction.Request; import org.elasticsearch.xpack.core.ml.job.config.MlFilterTests; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PutFilterActionResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PutFilterActionResponseTests.java index a6bafdfed2c68..1d2df8c2a0fe8 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PutFilterActionResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PutFilterActionResponseTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ml.action; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.job.config.MlFilter; import org.elasticsearch.xpack.core.ml.job.config.MlFilterTests; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PutTrainedModelActionRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PutTrainedModelActionRequestTests.java index 272ec0a06a256..90eb05a53a50a 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PutTrainedModelActionRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PutTrainedModelActionRequestTests.java @@ -8,8 +8,8 @@ import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.test.AbstractWireSerializingTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.ml.action.PutTrainedModelAction.Request; import org.elasticsearch.xpack.core.ml.inference.MlInferenceNamedXContentProvider; import org.elasticsearch.xpack.core.ml.inference.TrainedModelConfigTests; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PutTrainedModelActionResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PutTrainedModelActionResponseTests.java index 01f4bda52e66e..b1bde5a3d2703 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PutTrainedModelActionResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PutTrainedModelActionResponseTests.java @@ -8,8 +8,8 @@ import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.test.AbstractWireSerializingTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.ml.action.PutTrainedModelAction.Response; import org.elasticsearch.xpack.core.ml.inference.MlInferenceNamedXContentProvider; import org.elasticsearch.xpack.core.ml.inference.TrainedModelConfigTests; @@ -20,9 +20,11 @@ public class PutTrainedModelActionResponseTests extends AbstractWireSerializingT @Override protected Response createTestInstance() { String modelId = randomAlphaOfLength(10); - return new Response(TrainedModelConfigTests.createTestInstance(modelId, randomBoolean()) - .setParsedDefinition(TrainedModelDefinitionTests.createRandomBuilder()) - .build()); + return new Response( + TrainedModelConfigTests.createTestInstance(modelId, randomBoolean()) + .setParsedDefinition(TrainedModelDefinitionTests.createRandomBuilder()) + .build() + ); } @Override diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PutTrainedModelAliasActionRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PutTrainedModelAliasActionRequestTests.java index 8f77b116d7e96..4cdb3d49cdc33 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PutTrainedModelAliasActionRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PutTrainedModelAliasActionRequestTests.java @@ -27,11 +27,7 @@ public void setupModelAlias() { @Override protected Request createTestInstance() { - return new Request( - modelAlias, - randomAlphaOfLength(10), - randomBoolean() - ); + return new Request(modelAlias, randomAlphaOfLength(10), randomBoolean()); } @Override @@ -46,7 +42,7 @@ public void testCtor() { public void testValidate() { - { // model_alias equal to model Id + { // model_alias equal to model Id ActionRequestValidationException ex = new Request("foo", "foo", randomBoolean()).validate(); assertThat(ex, not(nullValue())); assertThat(ex.getMessage(), containsString("model_alias [foo] cannot equal model_id [foo]")); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PutTrainedModelDefinitionPartActionRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PutTrainedModelDefinitionPartActionRequestTests.java index 566754703a069..9f39f1b7bba4b 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PutTrainedModelDefinitionPartActionRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/PutTrainedModelDefinitionPartActionRequestTests.java @@ -15,7 +15,6 @@ import static org.hamcrest.Matchers.containsString; - public class PutTrainedModelDefinitionPartActionRequestTests extends AbstractBWCWireSerializationTestCase { @Override @@ -30,14 +29,14 @@ protected Request createTestInstance() { } public void testValidate() { - Request badRequest = new Request(randomAlphaOfLength(10), new BytesArray(randomAlphaOfLength(10)), -1, -1 , -1); + Request badRequest = new Request(randomAlphaOfLength(10), new BytesArray(randomAlphaOfLength(10)), -1, -1, -1); ValidationException exception = badRequest.validate(); assertThat(exception.getMessage(), containsString("[part] must be greater or equal to 0")); assertThat(exception.getMessage(), containsString("[total_parts] must be greater than 0")); assertThat(exception.getMessage(), containsString("[total_definition_length] must be greater than 0")); - badRequest = new Request(randomAlphaOfLength(10), new BytesArray(randomAlphaOfLength(10)), 5, 10 , 5); + badRequest = new Request(randomAlphaOfLength(10), new BytesArray(randomAlphaOfLength(10)), 5, 10, 5); exception = badRequest.validate(); assertThat(exception.getMessage(), containsString("[part] must be less than total_parts")); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/RevertModelSnapshotActionRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/RevertModelSnapshotActionRequestTests.java index fbf2b53bb0c13..a41c484a4ef3f 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/RevertModelSnapshotActionRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/RevertModelSnapshotActionRequestTests.java @@ -7,16 +7,18 @@ package org.elasticsearch.xpack.core.ml.action; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.RevertModelSnapshotAction.Request; public class RevertModelSnapshotActionRequestTests extends AbstractSerializingTestCase { @Override protected Request createTestInstance() { - RevertModelSnapshotAction.Request request = - new RevertModelSnapshotAction.Request(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20)); + RevertModelSnapshotAction.Request request = new RevertModelSnapshotAction.Request( + randomAlphaOfLengthBetween(1, 20), + randomAlphaOfLengthBetween(1, 20) + ); if (randomBoolean()) { request.setDeleteInterveningResults(randomBoolean()); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/SetUpgradeModeActionRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/SetUpgradeModeActionRequestTests.java index 2711ff38b8a33..b38afc1fa21c1 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/SetUpgradeModeActionRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/SetUpgradeModeActionRequestTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ml.action; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.SetUpgradeModeAction.Request; public class SetUpgradeModeActionRequestTests extends AbstractSerializingTestCase { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/StartDataFrameAnalyticsActionTaskParamsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/StartDataFrameAnalyticsActionTaskParamsTests.java index 8886d43b8d3ff..d4e0f1ffe9df4 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/StartDataFrameAnalyticsActionTaskParamsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/StartDataFrameAnalyticsActionTaskParamsTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.xpack.core.ml.action; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -24,10 +24,7 @@ protected StartDataFrameAnalyticsAction.TaskParams doParseInstance(XContentParse @Override protected StartDataFrameAnalyticsAction.TaskParams createTestInstance() { - return new StartDataFrameAnalyticsAction.TaskParams( - randomAlphaOfLength(10), - randomVersion(random()), - randomBoolean()); + return new StartDataFrameAnalyticsAction.TaskParams(randomAlphaOfLength(10), randomVersion(random()), randomBoolean()); } @Override diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/StartDataFrameAnalyticsRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/StartDataFrameAnalyticsRequestTests.java index 2046a84ad3e97..cef0cec8b32e0 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/StartDataFrameAnalyticsRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/StartDataFrameAnalyticsRequestTests.java @@ -8,9 +8,9 @@ import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.test.AbstractWireSerializingTestCase; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.AbstractWireSerializingTestCase; import org.elasticsearch.xpack.core.ml.action.StartDataFrameAnalyticsAction.Request; import java.io.IOException; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/StartDatafeedActionRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/StartDatafeedActionRequestTests.java index 1ed3c636e3268..f3e0ef4eee33b 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/StartDatafeedActionRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/StartDatafeedActionRequestTests.java @@ -8,8 +8,8 @@ import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.StartDatafeedAction.Request; import static org.hamcrest.Matchers.equalTo; @@ -37,17 +37,34 @@ protected Request doParseInstance(XContentParser parser) { } public void testParseDateOrThrow() { - assertEquals(0L, StartDatafeedAction.DatafeedParams.parseDateOrThrow("0", - StartDatafeedAction.START_TIME, () -> System.currentTimeMillis())); - assertEquals(0L, StartDatafeedAction.DatafeedParams.parseDateOrThrow("1970-01-01T00:00:00Z", - StartDatafeedAction.START_TIME, () -> System.currentTimeMillis())); - assertThat(StartDatafeedAction.DatafeedParams.parseDateOrThrow("now", - StartDatafeedAction.START_TIME, () -> 123456789L), equalTo(123456789L)); - - Exception e = expectThrows(ElasticsearchParseException.class, - () -> StartDatafeedAction.DatafeedParams.parseDateOrThrow("not-a-date", - StartDatafeedAction.START_TIME, () -> System.currentTimeMillis())); - assertEquals("Query param [start] with value [not-a-date] cannot be parsed as a date or converted to a number (epoch).", - e.getMessage()); + assertEquals( + 0L, + StartDatafeedAction.DatafeedParams.parseDateOrThrow("0", StartDatafeedAction.START_TIME, () -> System.currentTimeMillis()) + ); + assertEquals( + 0L, + StartDatafeedAction.DatafeedParams.parseDateOrThrow( + "1970-01-01T00:00:00Z", + StartDatafeedAction.START_TIME, + () -> System.currentTimeMillis() + ) + ); + assertThat( + StartDatafeedAction.DatafeedParams.parseDateOrThrow("now", StartDatafeedAction.START_TIME, () -> 123456789L), + equalTo(123456789L) + ); + + Exception e = expectThrows( + ElasticsearchParseException.class, + () -> StartDatafeedAction.DatafeedParams.parseDateOrThrow( + "not-a-date", + StartDatafeedAction.START_TIME, + () -> System.currentTimeMillis() + ) + ); + assertEquals( + "Query param [start] with value [not-a-date] cannot be parsed as a date or converted to a number (epoch).", + e.getMessage() + ); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/StartTrainedModelDeploymentRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/StartTrainedModelDeploymentRequestTests.java index 4c17f35025e7a..8baf9d1399af0 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/StartTrainedModelDeploymentRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/StartTrainedModelDeploymentRequestTests.java @@ -9,9 +9,9 @@ import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.core.TimeValue; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.StartTrainedModelDeploymentAction.Request; import org.elasticsearch.xpack.core.ml.inference.allocation.AllocationStatus; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/StartTrainedModelDeploymentTaskParamsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/StartTrainedModelDeploymentTaskParamsTests.java index c5160f96663a3..dc9e3e34d42a2 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/StartTrainedModelDeploymentTaskParamsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/StartTrainedModelDeploymentTaskParamsTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.xpack.core.ml.action; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.StartTrainedModelDeploymentAction.TaskParams; import java.io.IOException; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/StopDataFrameAnalyticsRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/StopDataFrameAnalyticsRequestTests.java index f8d195c593675..921c2821ece9e 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/StopDataFrameAnalyticsRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/StopDataFrameAnalyticsRequestTests.java @@ -8,9 +8,9 @@ import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.test.AbstractWireSerializingTestCase; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.AbstractWireSerializingTestCase; import org.elasticsearch.xpack.core.ml.action.StopDataFrameAnalyticsAction.Request; import java.io.IOException; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/StopDatafeedActionRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/StopDatafeedActionRequestTests.java index a6f5b064e57f4..f1b3b2572a383 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/StopDatafeedActionRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/StopDatafeedActionRequestTests.java @@ -8,8 +8,8 @@ import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.StopDatafeedAction.Request; public class StopDatafeedActionRequestTests extends AbstractSerializingTestCase { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/UpdateCalendarJobActionResquestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/UpdateCalendarJobActionResquestTests.java index 797a0c81da42b..b49abe4900ac6 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/UpdateCalendarJobActionResquestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/UpdateCalendarJobActionResquestTests.java @@ -13,9 +13,11 @@ public class UpdateCalendarJobActionResquestTests extends AbstractWireSerializin @Override protected UpdateCalendarJobAction.Request createTestInstance() { - return new UpdateCalendarJobAction.Request(randomAlphaOfLength(10), - randomBoolean() ? null : randomAlphaOfLength(10), - randomBoolean() ? null : randomAlphaOfLength(10)); + return new UpdateCalendarJobAction.Request( + randomAlphaOfLength(10), + randomBoolean() ? null : randomAlphaOfLength(10), + randomBoolean() ? null : randomAlphaOfLength(10) + ); } @Override diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/UpdateDataFrameAnalyticsActionRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/UpdateDataFrameAnalyticsActionRequestTests.java index 493eb803d6c5f..0789bdc206812 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/UpdateDataFrameAnalyticsActionRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/UpdateDataFrameAnalyticsActionRequestTests.java @@ -7,9 +7,9 @@ package org.elasticsearch.xpack.core.ml.action; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xpack.core.ml.action.UpdateDataFrameAnalyticsAction.Request; import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsConfigUpdate; import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsConfigUpdateTests; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/UpdateDatafeedActionRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/UpdateDatafeedActionRequestTests.java index 27728b2aee22d..a58b1dfce620b 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/UpdateDatafeedActionRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/UpdateDatafeedActionRequestTests.java @@ -9,10 +9,10 @@ import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.SearchModule; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.UpdateDatafeedAction.Request; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfigTests; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedUpdateTests; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/UpdateFilterActionRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/UpdateFilterActionRequestTests.java index c42864a0f3479..a3ab2963da53f 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/UpdateFilterActionRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/UpdateFilterActionRequestTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ml.action; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.UpdateFilterAction.Request; import java.util.ArrayList; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/UpdateModelSnapshotActionRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/UpdateModelSnapshotActionRequestTests.java index ec01f8352a849..e02784129a955 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/UpdateModelSnapshotActionRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/UpdateModelSnapshotActionRequestTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ml.action; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.UpdateModelSnapshotAction.Request; public class UpdateModelSnapshotActionRequestTests extends AbstractSerializingTestCase { @@ -20,8 +20,7 @@ protected Request doParseInstance(XContentParser parser) { @Override protected Request createTestInstance() { - Request request = new Request(randomAlphaOfLengthBetween(1, 20), - randomAlphaOfLengthBetween(1, 20)); + Request request = new Request(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20)); if (randomBoolean()) { request.setDescription(randomAlphaOfLengthBetween(1, 20)); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/UpdateModelSnapshotActionResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/UpdateModelSnapshotActionResponseTests.java index 1d67616d5a187..c4325d82195fb 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/UpdateModelSnapshotActionResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/UpdateModelSnapshotActionResponseTests.java @@ -11,8 +11,7 @@ import org.elasticsearch.xpack.core.ml.action.UpdateModelSnapshotAction.Response; import org.elasticsearch.xpack.core.ml.job.process.autodetect.state.ModelSnapshotTests; -public class UpdateModelSnapshotActionResponseTests - extends AbstractWireSerializingTestCase { +public class UpdateModelSnapshotActionResponseTests extends AbstractWireSerializingTestCase { @Override protected Response createTestInstance() { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/UpdateProcessActionRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/UpdateProcessActionRequestTests.java index bda6e8afe33a1..6a7864636643c 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/UpdateProcessActionRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/UpdateProcessActionRequestTests.java @@ -42,8 +42,14 @@ protected UpdateProcessAction.Request createTestInstance() { if (randomBoolean()) { filter = MlFilterTests.createTestFilter(); } - return new UpdateProcessAction.Request(randomAlphaOfLength(10), modelPlotConfig, perPartitionCategorizationConfig, updates, - filter, randomBoolean()); + return new UpdateProcessAction.Request( + randomAlphaOfLength(10), + modelPlotConfig, + perPartitionCategorizationConfig, + updates, + filter, + randomBoolean() + ); } @Override diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/UpgradeJobModelSnapshotRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/UpgradeJobModelSnapshotRequestTests.java index 1875f95fd96ee..685077daaa1f4 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/UpgradeJobModelSnapshotRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/UpgradeJobModelSnapshotRequestTests.java @@ -7,18 +7,20 @@ package org.elasticsearch.xpack.core.ml.action; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.UpgradeJobModelSnapshotAction.Request; public class UpgradeJobModelSnapshotRequestTests extends AbstractSerializingTestCase { @Override protected Request createTestInstance() { - return new Request(randomAlphaOfLength(10), + return new Request( + randomAlphaOfLength(10), randomAlphaOfLength(10), randomBoolean() ? null : randomTimeValue(), - randomBoolean() ? null : randomBoolean()); + randomBoolean() ? null : randomBoolean() + ); } @Override diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/UpgradeJobModelSnapshotResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/UpgradeJobModelSnapshotResponseTests.java index 86c70d28d22e7..e694794b0e8f3 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/UpgradeJobModelSnapshotResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/UpgradeJobModelSnapshotResponseTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ml.action; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.UpgradeJobModelSnapshotAction.Response; public class UpgradeJobModelSnapshotResponseTests extends AbstractSerializingTestCase { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/ValidateDetectorActionRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/ValidateDetectorActionRequestTests.java index 8148ed6a8845b..65e0e469efde0 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/ValidateDetectorActionRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/ValidateDetectorActionRequestTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ml.action; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.ValidateDetectorAction.Request; import org.elasticsearch.xpack.core.ml.job.config.Detector; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/ValidateJobConfigActionRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/ValidateJobConfigActionRequestTests.java index 3dcf770e5a125..b16c63f95db08 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/ValidateJobConfigActionRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/action/ValidateJobConfigActionRequestTests.java @@ -8,6 +8,7 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.test.AbstractWireSerializingTestCase; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ToXContent; @@ -15,7 +16,6 @@ import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.AbstractWireSerializingTestCase; import org.elasticsearch.xpack.core.ml.action.ValidateJobConfigAction.Request; import org.elasticsearch.xpack.core.ml.job.config.Job; @@ -45,8 +45,11 @@ public void testParseRequest_InvalidCreateSetting() throws IOException { XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); XContentBuilder xContentBuilder = jobConfiguration.build(new Date()).toXContent(builder, ToXContent.EMPTY_PARAMS); XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, - BytesReference.bytes(xContentBuilder).streamInput()); + .createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + BytesReference.bytes(xContentBuilder).streamInput() + ); expectThrows(IllegalArgumentException.class, () -> Request.parseRequest(parser)); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/annotations/AnnotationTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/annotations/AnnotationTests.java index 68bf809a6fac7..56d6160b9225a 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/annotations/AnnotationTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/annotations/AnnotationTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ml.annotations; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.util.Date; @@ -27,8 +27,7 @@ protected Annotation createTestInstance() { } public static Annotation randomAnnotation(String jobId) { - return new Annotation.Builder() - .setAnnotation(randomAlphaOfLengthBetween(100, 1000)) + return new Annotation.Builder().setAnnotation(randomAlphaOfLengthBetween(100, 1000)) .setCreateTime(new Date(randomNonNegativeLong())) .setCreateUsername(randomAlphaOfLengthBetween(5, 20)) .setTimestamp(new Date(randomNonNegativeLong())) diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/calendars/CalendarTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/calendars/CalendarTests.java index 6b748e86f063c..4a568452d48aa 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/calendars/CalendarTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/calendars/CalendarTests.java @@ -7,9 +7,9 @@ package org.elasticsearch.xpack.core.ml.calendars; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xpack.core.ml.job.config.JobTests; import java.io.IOException; @@ -66,8 +66,7 @@ public void testDocumentId() { public void testStrictParser() throws IOException { String json = "{\"foo\":\"bar\"}"; try (XContentParser parser = createParser(JsonXContent.jsonXContent, json)) { - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> Calendar.STRICT_PARSER.apply(parser, null)); + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> Calendar.STRICT_PARSER.apply(parser, null)); assertThat(e.getMessage(), containsString("unknown field [foo]")); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/calendars/ScheduledEventTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/calendars/ScheduledEventTests.java index 31071526fb8d3..edf1358517b93 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/calendars/ScheduledEventTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/calendars/ScheduledEventTests.java @@ -9,9 +9,9 @@ import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xpack.core.ml.job.config.DetectionRule; import org.elasticsearch.xpack.core.ml.job.config.Operator; import org.elasticsearch.xpack.core.ml.job.config.RuleAction; @@ -28,8 +28,7 @@ public class ScheduledEventTests extends AbstractSerializingTestCase ScheduledEvent.STRICT_PARSER.apply(parser, null)); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> ScheduledEvent.STRICT_PARSER.apply(parser, null) + ); assertThat(e.getMessage(), containsString("unknown field [foo]")); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/AggProviderTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/AggProviderTests.java index 64de65066c688..efcdb58d4f1a4 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/AggProviderTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/AggProviderTests.java @@ -10,15 +10,15 @@ import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.search.SearchModule; +import org.elasticsearch.search.aggregations.AggregatorFactories; +import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.rest.RestStatus; -import org.elasticsearch.search.SearchModule; -import org.elasticsearch.search.aggregations.AggregatorFactories; -import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xpack.core.ml.utils.XContentObjectTransformer; import java.io.IOException; @@ -28,7 +28,6 @@ import static org.hamcrest.Matchers.equalTo; - public class AggProviderTests extends AbstractSerializingTestCase { @Override @@ -68,13 +67,12 @@ public static AggProvider createRandomValidAggProvider() { } public static AggProvider createRandomValidAggProvider(String name, String field) { - Map agg = Collections.singletonMap(name, - Collections.singletonMap("avg", Collections.singletonMap("field", field))); + Map agg = Collections.singletonMap(name, Collections.singletonMap("avg", Collections.singletonMap("field", field))); try { SearchModule searchModule = new SearchModule(Settings.EMPTY, Collections.emptyList()); - AggregatorFactories.Builder aggs = - XContentObjectTransformer.aggregatorTransformer(new NamedXContentRegistry(searchModule.getNamedXContents())) - .fromMap(agg); + AggregatorFactories.Builder aggs = XContentObjectTransformer.aggregatorTransformer( + new NamedXContentRegistry(searchModule.getNamedXContents()) + ).fromMap(agg); return new AggProvider(agg, aggs, null, false); } catch (IOException ex) { fail(ex.getMessage()); @@ -85,8 +83,7 @@ public static AggProvider createRandomValidAggProvider(String name, String field public void testEmptyAggMap() throws IOException { XContentParser parser = XContentFactory.xContent(XContentType.JSON) .createParser(xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, "{}"); - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> AggProvider.fromXContent(parser, false)); + ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, () -> AggProvider.fromXContent(parser, false)); assertThat(e.status(), equalTo(RestStatus.BAD_REQUEST)); assertThat(e.getMessage(), equalTo("Datafeed aggregations are not parsable")); } @@ -94,77 +91,92 @@ public void testEmptyAggMap() throws IOException { public void testRewriteBadNumericInterval() { long numericInterval = randomNonNegativeLong(); Map maxTime = Collections.singletonMap("max", Collections.singletonMap("field", "time")); - Map numericDeprecated = new HashMap<>(){{ - put("interval", numericInterval); - put("field", "foo"); - put("aggs", Collections.singletonMap("time", maxTime)); - }}; - Map expected = new HashMap<>() {{ - put("fixed_interval", numericInterval + "ms"); - put("field", "foo"); - put("aggs", Collections.singletonMap("time", maxTime)); - }}; + Map numericDeprecated = new HashMap<>() { + { + put("interval", numericInterval); + put("field", "foo"); + put("aggs", Collections.singletonMap("time", maxTime)); + } + }; + Map expected = new HashMap<>() { + { + put("fixed_interval", numericInterval + "ms"); + put("field", "foo"); + put("aggs", Collections.singletonMap("time", maxTime)); + } + }; Map deprecated = Collections.singletonMap("buckets", Collections.singletonMap("date_histogram", numericDeprecated)); assertTrue(AggProvider.rewriteDateHistogramInterval(deprecated, false)); assertThat(deprecated, equalTo(Collections.singletonMap("buckets", Collections.singletonMap("date_histogram", expected)))); - numericDeprecated = new HashMap<>(){{ - put("interval", numericInterval + "ms"); - put("field", "foo"); - put("aggs", Collections.singletonMap("time", maxTime)); - }}; + numericDeprecated = new HashMap<>() { + { + put("interval", numericInterval + "ms"); + put("field", "foo"); + put("aggs", Collections.singletonMap("time", maxTime)); + } + }; deprecated = Collections.singletonMap("date_histogram", Collections.singletonMap("date_histogram", numericDeprecated)); assertTrue(AggProvider.rewriteDateHistogramInterval(deprecated, false)); - assertThat(deprecated, - equalTo(Collections.singletonMap("date_histogram", Collections.singletonMap("date_histogram", expected)))); + assertThat(deprecated, equalTo(Collections.singletonMap("date_histogram", Collections.singletonMap("date_histogram", expected)))); } public void testRewriteBadCalendarInterval() { String calendarInterval = "1w"; Map maxTime = Collections.singletonMap("max", Collections.singletonMap("field", "time")); - Map calendarDeprecated = new HashMap<>(){{ - put("interval", calendarInterval); - put("field", "foo"); - put("aggs", Collections.singletonMap("time", maxTime)); - }}; - Map expected = new HashMap<>() {{ - put("calendar_interval", calendarInterval); - put("field", "foo"); - put("aggs", Collections.singletonMap("time", maxTime)); - }}; - Map deprecated = Collections.singletonMap("buckets", - Collections.singletonMap("date_histogram", calendarDeprecated)); + Map calendarDeprecated = new HashMap<>() { + { + put("interval", calendarInterval); + put("field", "foo"); + put("aggs", Collections.singletonMap("time", maxTime)); + } + }; + Map expected = new HashMap<>() { + { + put("calendar_interval", calendarInterval); + put("field", "foo"); + put("aggs", Collections.singletonMap("time", maxTime)); + } + }; + Map deprecated = Collections.singletonMap( + "buckets", + Collections.singletonMap("date_histogram", calendarDeprecated) + ); assertTrue(AggProvider.rewriteDateHistogramInterval(deprecated, false)); assertThat(deprecated, equalTo(Collections.singletonMap("buckets", Collections.singletonMap("date_histogram", expected)))); - calendarDeprecated = new HashMap<>(){{ - put("interval", calendarInterval); - put("field", "foo"); - put("aggs", Collections.singletonMap("time", maxTime)); - }}; + calendarDeprecated = new HashMap<>() { + { + put("interval", calendarInterval); + put("field", "foo"); + put("aggs", Collections.singletonMap("time", maxTime)); + } + }; deprecated = Collections.singletonMap("date_histogram", Collections.singletonMap("date_histogram", calendarDeprecated)); assertTrue(AggProvider.rewriteDateHistogramInterval(deprecated, false)); - assertThat(deprecated, - equalTo(Collections.singletonMap("date_histogram", Collections.singletonMap("date_histogram", expected)))); + assertThat(deprecated, equalTo(Collections.singletonMap("date_histogram", Collections.singletonMap("date_histogram", expected)))); } public void testRewriteWhenNoneMustOccur() { String calendarInterval = "1w"; Map maxTime = Collections.singletonMap("max", Collections.singletonMap("field", "time")); - Map calendarDeprecated = new HashMap<>(){{ - put("calendar_interval", calendarInterval); - put("field", "foo"); - put("aggs", Collections.singletonMap("time", maxTime)); - }}; - Map expected = new HashMap<>() {{ - put("calendar_interval", calendarInterval); - put("field", "foo"); - put("aggs", Collections.singletonMap("time", maxTime)); - }}; + Map calendarDeprecated = new HashMap<>() { + { + put("calendar_interval", calendarInterval); + put("field", "foo"); + put("aggs", Collections.singletonMap("time", maxTime)); + } + }; + Map expected = new HashMap<>() { + { + put("calendar_interval", calendarInterval); + put("field", "foo"); + put("aggs", Collections.singletonMap("time", maxTime)); + } + }; Map current = Collections.singletonMap("buckets", Collections.singletonMap("date_histogram", calendarDeprecated)); assertFalse(AggProvider.rewriteDateHistogramInterval(current, false)); - assertThat(current, - equalTo(Collections.singletonMap("buckets", Collections.singletonMap("date_histogram", expected)))); + assertThat(current, equalTo(Collections.singletonMap("buckets", Collections.singletonMap("date_histogram", expected)))); } @Override @@ -176,9 +188,9 @@ protected AggProvider mutateInstance(AggProvider instance) throws IOException { parsingException = parsingException == null ? new IOException("failed parsing") : null; break; case 1: - parsedAggs = parsedAggs == null ? - XContentObjectTransformer.aggregatorTransformer(xContentRegistry()).fromMap(instance.getAggs()) : - null; + parsedAggs = parsedAggs == null + ? XContentObjectTransformer.aggregatorTransformer(xContentRegistry()).fromMap(instance.getAggs()) + : null; break; default: throw new AssertionError("Illegal randomisation branch"); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/AggProviderWireSerializationTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/AggProviderWireSerializationTests.java index 35d9149da4590..389efa3f0d9a6 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/AggProviderWireSerializationTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/AggProviderWireSerializationTests.java @@ -11,9 +11,9 @@ import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.search.SearchModule; import org.elasticsearch.search.aggregations.AggregatorFactories; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.ml.AbstractBWCWireSerializationTestCase; import org.elasticsearch.xpack.core.ml.utils.XContentObjectTransformer; @@ -45,13 +45,15 @@ protected Writeable.Reader instanceReader() { } public static AggProvider createRandomValidAggProvider() { - Map agg = Collections.singletonMap(randomAlphaOfLengthBetween(1, 10), - Collections.singletonMap("avg", Collections.singletonMap("field", randomAlphaOfLengthBetween(1, 10)))); + Map agg = Collections.singletonMap( + randomAlphaOfLengthBetween(1, 10), + Collections.singletonMap("avg", Collections.singletonMap("field", randomAlphaOfLengthBetween(1, 10))) + ); try { SearchModule searchModule = new SearchModule(Settings.EMPTY, Collections.emptyList()); - AggregatorFactories.Builder aggs = - XContentObjectTransformer.aggregatorTransformer(new NamedXContentRegistry(searchModule.getNamedXContents())) - .fromMap(agg); + AggregatorFactories.Builder aggs = XContentObjectTransformer.aggregatorTransformer( + new NamedXContentRegistry(searchModule.getNamedXContents()) + ).fromMap(agg); Exception parsingException = null; if (randomBoolean()) { aggs = null; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/ChunkingConfigTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/ChunkingConfigTests.java index cf1803bb3ea7c..9fc66707e3cf1 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/ChunkingConfigTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/ChunkingConfigTests.java @@ -8,8 +8,8 @@ import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.ArrayList; @@ -62,7 +62,7 @@ public static ChunkingConfig createRandomizedChunk() { timeSpan = randomPositiveSecondsMinutesHours(); } return new ChunkingConfig(mode, timeSpan); - } + } private static TimeValue randomPositiveSecondsMinutesHours() { return new TimeValue(randomIntBetween(1, 1000), randomFrom(Arrays.asList(TimeUnit.SECONDS, TimeUnit.MINUTES, TimeUnit.HOURS))); @@ -73,27 +73,27 @@ protected ChunkingConfig mutateInstance(ChunkingConfig instance) throws IOExcept ChunkingConfig.Mode mode = instance.getMode(); TimeValue timeSpan = instance.getTimeSpan(); switch (between(0, 1)) { - case 0: - List modes = new ArrayList<>(Arrays.asList(ChunkingConfig.Mode.values())); - modes.remove(mode); - mode = randomFrom(modes); - if (mode == ChunkingConfig.Mode.MANUAL) { - timeSpan = randomPositiveSecondsMinutesHours(); - } else { - timeSpan = null; - } - break; - case 1: - if (timeSpan == null) { - timeSpan = randomPositiveSecondsMinutesHours(); - } else { - timeSpan = new TimeValue(timeSpan.getMillis() + between(10, 10000)); - } - // only manual mode allows a timespan - mode = ChunkingConfig.Mode.MANUAL; - break; - default: - throw new AssertionError("Illegal randomisation branch"); + case 0: + List modes = new ArrayList<>(Arrays.asList(ChunkingConfig.Mode.values())); + modes.remove(mode); + mode = randomFrom(modes); + if (mode == ChunkingConfig.Mode.MANUAL) { + timeSpan = randomPositiveSecondsMinutesHours(); + } else { + timeSpan = null; + } + break; + case 1: + if (timeSpan == null) { + timeSpan = randomPositiveSecondsMinutesHours(); + } else { + timeSpan = new TimeValue(timeSpan.getMillis() + between(10, 10000)); + } + // only manual mode allows a timespan + mode = ChunkingConfig.Mode.MANUAL; + break; + default: + throw new AssertionError("Illegal randomisation branch"); } return new ChunkingConfig(mode, timeSpan); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedConfigBuilderTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedConfigBuilderTests.java index 166b68fec4341..2430a95c12288 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedConfigBuilderTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedConfigBuilderTests.java @@ -51,8 +51,9 @@ public static DatafeedConfig.Builder createRandomizedDatafeedConfigBuilder(Strin int scriptsSize = randomInt(3); List scriptFields = new ArrayList<>(scriptsSize); for (int scriptIndex = 0; scriptIndex < scriptsSize; scriptIndex++) { - scriptFields.add(new SearchSourceBuilder.ScriptField(randomAlphaOfLength(10), mockScript(randomAlphaOfLength(10)), - randomBoolean())); + scriptFields.add( + new SearchSourceBuilder.ScriptField(randomAlphaOfLength(10), mockScript(randomAlphaOfLength(10)), randomBoolean()) + ); } builder.setScriptFields(scriptFields); } @@ -63,18 +64,17 @@ public static DatafeedConfig.Builder createRandomizedDatafeedConfigBuilder(Strin // Testing with a single agg is ok as we don't have special list writeable / xcontent logic AggregatorFactories.Builder aggs = new AggregatorFactories.Builder(); aggHistogramInterval = randomNonNegativeLong(); - aggHistogramInterval = aggHistogramInterval> bucketSpanMillis ? bucketSpanMillis : aggHistogramInterval; + aggHistogramInterval = aggHistogramInterval > bucketSpanMillis ? bucketSpanMillis : aggHistogramInterval; aggHistogramInterval = aggHistogramInterval <= 0 ? 1 : aggHistogramInterval; MaxAggregationBuilder maxTime = AggregationBuilders.max("time").field("time"); - AggregationBuilder topAgg = randomBoolean() ? - AggregationBuilders.dateHistogram("buckets") + AggregationBuilder topAgg = randomBoolean() + ? AggregationBuilders.dateHistogram("buckets") .field("time") - .fixedInterval(new DateHistogramInterval(aggHistogramInterval + "ms")) : - AggregationBuilders.composite( + .fixedInterval(new DateHistogramInterval(aggHistogramInterval + "ms")) + : AggregationBuilders.composite( "buckets", Collections.singletonList( - new DateHistogramValuesSourceBuilder("time") - .field("time") + new DateHistogramValuesSourceBuilder("time").field("time") .fixedInterval(new DateHistogramInterval(aggHistogramInterval + "ms")) ) ); @@ -103,12 +103,15 @@ public static DatafeedConfig.Builder createRandomizedDatafeedConfigBuilder(Strin if (randomBoolean()) { builder.setMaxEmptySearches(randomIntBetween(10, 100)); } - builder.setIndicesOptions(IndicesOptions.fromParameters( - randomFrom(IndicesOptions.WildcardStates.values()).name().toLowerCase(Locale.ROOT), - Boolean.toString(randomBoolean()), - Boolean.toString(randomBoolean()), - Boolean.toString(randomBoolean()), - SearchRequest.DEFAULT_INDICES_OPTIONS)); + builder.setIndicesOptions( + IndicesOptions.fromParameters( + randomFrom(IndicesOptions.WildcardStates.values()).name().toLowerCase(Locale.ROOT), + Boolean.toString(randomBoolean()), + Boolean.toString(randomBoolean()), + Boolean.toString(randomBoolean()), + SearchRequest.DEFAULT_INDICES_OPTIONS + ) + ); if (randomBoolean()) { Map settings = new HashMap<>(); settings.put("type", "keyword"); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedConfigTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedConfigTests.java index 1e7a08a861f8c..43fe7a217b408 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedConfigTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedConfigTests.java @@ -7,6 +7,7 @@ package org.elasticsearch.xpack.core.ml.datafeed; import com.carrotsearch.randomizedtesting.generators.CodepointSetGenerator; + import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.Version; @@ -19,17 +20,10 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.CollectionUtils; -import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentParseException; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.TermQueryBuilder; @@ -52,6 +46,13 @@ import org.elasticsearch.search.builder.SearchSourceBuilder.ScriptField; import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.DeprecationHandler; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentParseException; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.ml.datafeed.ChunkingConfig.Mode; import org.elasticsearch.xpack.core.ml.job.messages.Messages; import org.elasticsearch.xpack.core.ml.utils.QueryProvider; @@ -131,191 +132,217 @@ protected DatafeedConfig doParseInstance(XContentParser parser) { return DatafeedConfig.STRICT_PARSER.apply(parser, null).build(); } - private static final String FUTURE_DATAFEED = "{\n" + - " \"datafeed_id\": \"farequote-datafeed\",\n" + - " \"job_id\": \"farequote\",\n" + - " \"frequency\": \"1h\",\n" + - " \"indices\": [\"farequote1\", \"farequote2\"],\n" + - " \"tomorrows_technology_today\": \"amazing\",\n" + - " \"scroll_size\": 1234\n" + - "}"; - - private static final String ANACHRONISTIC_QUERY_DATAFEED = "{\n" + - " \"datafeed_id\": \"farequote-datafeed\",\n" + - " \"job_id\": \"farequote\",\n" + - " \"frequency\": \"1h\",\n" + - " \"indices\": [\"farequote1\", \"farequote2\"],\n" + - //query:match:type stopped being supported in 6.x - " \"query\": {\"match\" : {\"query\":\"fieldName\", \"type\": \"phrase\"}},\n" + - " \"scroll_size\": 1234\n" + - "}"; - - private static final String ANACHRONISTIC_AGG_DATAFEED = "{\n" + - " \"datafeed_id\": \"farequote-datafeed\",\n" + - " \"job_id\": \"farequote\",\n" + - " \"frequency\": \"1h\",\n" + - " \"indices\": [\"farequote1\", \"farequote2\"],\n" + - " \"aggregations\": {\n" + - " \"buckets\": {\n" + - " \"date_histogram\": {\n" + - " \"field\": \"time\",\n" + - " \"fixed_interval\": \"360s\",\n" + - " \"time_zone\": \"UTC\"\n" + - " },\n" + - " \"aggregations\": {\n" + - " \"time\": {\n" + - " \"max\": {\"field\": \"time\"}\n" + - " },\n" + - " \"airline\": {\n" + - " \"terms\": {\n" + - " \"field\": \"airline\",\n" + - " \"size\": 0\n" + //size: 0 stopped being supported in 6.x - " }\n" + - " }\n" + - " }\n" + - " }\n" + - " }\n" + - "}"; - - private static final String AGG_WITH_OLD_DATE_HISTOGRAM_INTERVAL = "{\n" + - " \"datafeed_id\": \"farequote-datafeed\",\n" + - " \"job_id\": \"farequote\",\n" + - " \"frequency\": \"1h\",\n" + - " \"indices\": [\"farequote1\", \"farequote2\"],\n" + - " \"aggregations\": {\n" + - " \"buckets\": {\n" + - " \"date_histogram\": {\n" + - " \"field\": \"time\",\n" + - " \"interval\": \"360s\",\n" + - " \"time_zone\": \"UTC\"\n" + - " },\n" + - " \"aggregations\": {\n" + - " \"time\": {\n" + - " \"max\": {\"field\": \"time\"}\n" + - " }\n" + - " }\n" + - " }\n" + - " }\n" + - "}"; - - private static final String MULTIPLE_AGG_DEF_DATAFEED = "{\n" + - " \"datafeed_id\": \"farequote-datafeed\",\n" + - " \"job_id\": \"farequote\",\n" + - " \"frequency\": \"1h\",\n" + - " \"indices\": [\"farequote1\", \"farequote2\"],\n" + - " \"aggregations\": {\n" + - " \"buckets\": {\n" + - " \"date_histogram\": {\n" + - " \"field\": \"time\",\n" + - " \"fixed_interval\": \"360s\",\n" + - " \"time_zone\": \"UTC\"\n" + - " },\n" + - " \"aggregations\": {\n" + - " \"time\": {\n" + - " \"max\": {\"field\": \"time\"}\n" + - " }\n" + - " }\n" + - " }\n" + - " }," + - " \"aggs\": {\n" + - " \"buckets2\": {\n" + - " \"date_histogram\": {\n" + - " \"field\": \"time\",\n" + - " \"fixed_interval\": \"360s\",\n" + - " \"time_zone\": \"UTC\"\n" + - " },\n" + - " \"aggregations\": {\n" + - " \"time\": {\n" + - " \"max\": {\"field\": \"time\"}\n" + - " }\n" + - " }\n" + - " }\n" + - " }\n" + - "}"; + private static final String FUTURE_DATAFEED = "{\n" + + " \"datafeed_id\": \"farequote-datafeed\",\n" + + " \"job_id\": \"farequote\",\n" + + " \"frequency\": \"1h\",\n" + + " \"indices\": [\"farequote1\", \"farequote2\"],\n" + + " \"tomorrows_technology_today\": \"amazing\",\n" + + " \"scroll_size\": 1234\n" + + "}"; + + private static final String ANACHRONISTIC_QUERY_DATAFEED = "{\n" + + " \"datafeed_id\": \"farequote-datafeed\",\n" + + " \"job_id\": \"farequote\",\n" + + " \"frequency\": \"1h\",\n" + + " \"indices\": [\"farequote1\", \"farequote2\"],\n" + + + // query:match:type stopped being supported in 6.x + " \"query\": {\"match\" : {\"query\":\"fieldName\", \"type\": \"phrase\"}},\n" + + " \"scroll_size\": 1234\n" + + "}"; + + private static final String ANACHRONISTIC_AGG_DATAFEED = "{\n" + + " \"datafeed_id\": \"farequote-datafeed\",\n" + + " \"job_id\": \"farequote\",\n" + + " \"frequency\": \"1h\",\n" + + " \"indices\": [\"farequote1\", \"farequote2\"],\n" + + " \"aggregations\": {\n" + + " \"buckets\": {\n" + + " \"date_histogram\": {\n" + + " \"field\": \"time\",\n" + + " \"fixed_interval\": \"360s\",\n" + + " \"time_zone\": \"UTC\"\n" + + " },\n" + + " \"aggregations\": {\n" + + " \"time\": {\n" + + " \"max\": {\"field\": \"time\"}\n" + + " },\n" + + " \"airline\": {\n" + + " \"terms\": {\n" + + " \"field\": \"airline\",\n" + + " \"size\": 0\n" + + // size: 0 stopped being supported in 6.x + " }\n" + + " }\n" + + " }\n" + + " }\n" + + " }\n" + + "}"; + + private static final String AGG_WITH_OLD_DATE_HISTOGRAM_INTERVAL = "{\n" + + " \"datafeed_id\": \"farequote-datafeed\",\n" + + " \"job_id\": \"farequote\",\n" + + " \"frequency\": \"1h\",\n" + + " \"indices\": [\"farequote1\", \"farequote2\"],\n" + + " \"aggregations\": {\n" + + " \"buckets\": {\n" + + " \"date_histogram\": {\n" + + " \"field\": \"time\",\n" + + " \"interval\": \"360s\",\n" + + " \"time_zone\": \"UTC\"\n" + + " },\n" + + " \"aggregations\": {\n" + + " \"time\": {\n" + + " \"max\": {\"field\": \"time\"}\n" + + " }\n" + + " }\n" + + " }\n" + + " }\n" + + "}"; + + private static final String MULTIPLE_AGG_DEF_DATAFEED = "{\n" + + " \"datafeed_id\": \"farequote-datafeed\",\n" + + " \"job_id\": \"farequote\",\n" + + " \"frequency\": \"1h\",\n" + + " \"indices\": [\"farequote1\", \"farequote2\"],\n" + + " \"aggregations\": {\n" + + " \"buckets\": {\n" + + " \"date_histogram\": {\n" + + " \"field\": \"time\",\n" + + " \"fixed_interval\": \"360s\",\n" + + " \"time_zone\": \"UTC\"\n" + + " },\n" + + " \"aggregations\": {\n" + + " \"time\": {\n" + + " \"max\": {\"field\": \"time\"}\n" + + " }\n" + + " }\n" + + " }\n" + + " }," + + " \"aggs\": {\n" + + " \"buckets2\": {\n" + + " \"date_histogram\": {\n" + + " \"field\": \"time\",\n" + + " \"fixed_interval\": \"360s\",\n" + + " \"time_zone\": \"UTC\"\n" + + " },\n" + + " \"aggregations\": {\n" + + " \"time\": {\n" + + " \"max\": {\"field\": \"time\"}\n" + + " }\n" + + " }\n" + + " }\n" + + " }\n" + + "}"; public void testFutureConfigParse() throws IOException { XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, FUTURE_DATAFEED); - XContentParseException e = expectThrows(XContentParseException.class, - () -> DatafeedConfig.STRICT_PARSER.apply(parser, null).build()); + .createParser(xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, FUTURE_DATAFEED); + XContentParseException e = expectThrows( + XContentParseException.class, + () -> DatafeedConfig.STRICT_PARSER.apply(parser, null).build() + ); assertEquals("[6:5] [datafeed_config] unknown field [tomorrows_technology_today]", e.getMessage()); } public void testPastQueryConfigParse() throws IOException { - try(XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, ANACHRONISTIC_QUERY_DATAFEED)) { + try ( + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, ANACHRONISTIC_QUERY_DATAFEED) + ) { DatafeedConfig config = DatafeedConfig.LENIENT_PARSER.apply(parser, null).build(); - assertThat(config.getQueryParsingException().getMessage(), - equalTo("[match] query doesn't support multiple fields, found [query] and [type]")); + assertThat( + config.getQueryParsingException().getMessage(), + equalTo("[match] query doesn't support multiple fields, found [query] and [type]") + ); } - try(XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, ANACHRONISTIC_QUERY_DATAFEED)) { + try ( + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, ANACHRONISTIC_QUERY_DATAFEED) + ) { - XContentParseException e = expectThrows(XContentParseException.class, - () -> DatafeedConfig.STRICT_PARSER.apply(parser, null).build()); + XContentParseException e = expectThrows( + XContentParseException.class, + () -> DatafeedConfig.STRICT_PARSER.apply(parser, null).build() + ); assertEquals("[6:64] [datafeed_config] failed to parse field [query]", e.getMessage()); } } public void testPastAggConfigParse() throws IOException { - try(XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, ANACHRONISTIC_AGG_DATAFEED)) { + try ( + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, ANACHRONISTIC_AGG_DATAFEED) + ) { DatafeedConfig datafeedConfig = DatafeedConfig.LENIENT_PARSER.apply(parser, null).build(); - assertThat(datafeedConfig.getAggParsingException().getMessage(), - equalTo("[size] must be greater than 0. Found [0] in [airline]")); + assertThat( + datafeedConfig.getAggParsingException().getMessage(), + equalTo("[size] must be greater than 0. Found [0] in [airline]") + ); } - try(XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, ANACHRONISTIC_AGG_DATAFEED)) { + try ( + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, ANACHRONISTIC_AGG_DATAFEED) + ) { - XContentParseException e = expectThrows(XContentParseException.class, - () -> DatafeedConfig.STRICT_PARSER.apply(parser, null).build()); + XContentParseException e = expectThrows( + XContentParseException.class, + () -> DatafeedConfig.STRICT_PARSER.apply(parser, null).build() + ); assertEquals("[25:3] [datafeed_config] failed to parse field [aggregations]", e.getMessage()); } } public void testPastAggConfigOldDateHistogramParse() throws IOException { - try(XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(xContentRegistry(), - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, - AGG_WITH_OLD_DATE_HISTOGRAM_INTERVAL)) { + try ( + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, AGG_WITH_OLD_DATE_HISTOGRAM_INTERVAL) + ) { DatafeedConfig datafeedConfig = DatafeedConfig.LENIENT_PARSER.apply(parser, null).build(); assertThat(datafeedConfig.getParsedAggregations(xContentRegistry()), is(not(nullValue()))); } - try(XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, ANACHRONISTIC_AGG_DATAFEED)) { + try ( + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, ANACHRONISTIC_AGG_DATAFEED) + ) { - XContentParseException e = expectThrows(XContentParseException.class, - () -> DatafeedConfig.STRICT_PARSER.apply(parser, null).build()); + XContentParseException e = expectThrows( + XContentParseException.class, + () -> DatafeedConfig.STRICT_PARSER.apply(parser, null).build() + ); assertEquals("[25:3] [datafeed_config] failed to parse field [aggregations]", e.getMessage()); } } public void testFutureMetadataParse() throws IOException { XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, FUTURE_DATAFEED); + .createParser(xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, FUTURE_DATAFEED); // Unlike the config version of this test, the metadata parser should tolerate the unknown future field assertNotNull(DatafeedConfig.LENIENT_PARSER.apply(parser, null).build()); } public void testMultipleDefinedAggParse() throws IOException { - try(XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, MULTIPLE_AGG_DEF_DATAFEED)) { - XContentParseException ex = expectThrows(XContentParseException.class, - () -> DatafeedConfig.LENIENT_PARSER.apply(parser, null)); + try ( + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, MULTIPLE_AGG_DEF_DATAFEED) + ) { + XContentParseException ex = expectThrows(XContentParseException.class, () -> DatafeedConfig.LENIENT_PARSER.apply(parser, null)); assertThat(ex.getMessage(), equalTo("[32:3] [datafeed_config] failed to parse field [aggs]")); assertNotNull(ex.getCause()); assertThat(ex.getCause().getMessage(), equalTo("Found two aggregation definitions: [aggs] and [aggregations]")); } - try(XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, MULTIPLE_AGG_DEF_DATAFEED)) { - XContentParseException ex = expectThrows(XContentParseException.class, - () -> DatafeedConfig.STRICT_PARSER.apply(parser, null)); + try ( + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, MULTIPLE_AGG_DEF_DATAFEED) + ) { + XContentParseException ex = expectThrows(XContentParseException.class, () -> DatafeedConfig.STRICT_PARSER.apply(parser, null)); assertThat(ex.getMessage(), equalTo("[32:3] [datafeed_config] failed to parse field [aggs]")); assertNotNull(ex.getCause()); assertThat(ex.getCause().getMessage(), equalTo("Found two aggregation definitions: [aggs] and [aggregations]")); @@ -335,7 +362,7 @@ public void testToXContentForInternalStorage() throws IOException { BytesReference forClusterstateXContent = XContentHelper.toXContent(config, XContentType.JSON, params, false); XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(xContentRegistry(), LoggingDeprecationHandler.INSTANCE, forClusterstateXContent.streamInput()); + .createParser(xContentRegistry(), LoggingDeprecationHandler.INSTANCE, forClusterstateXContent.streamInput()); DatafeedConfig parsedConfig = DatafeedConfig.LENIENT_PARSER.apply(parser, null).build(); assertThat(parsedConfig.getHeaders(), hasEntry("header-name", "header-value")); @@ -343,7 +370,7 @@ public void testToXContentForInternalStorage() throws IOException { // headers are not written without the FOR_INTERNAL_STORAGE param BytesReference nonClusterstateXContent = XContentHelper.toXContent(config, XContentType.JSON, ToXContent.EMPTY_PARAMS, false); parser = XContentFactory.xContent(XContentType.JSON) - .createParser(xContentRegistry(), LoggingDeprecationHandler.INSTANCE, nonClusterstateXContent.streamInput()); + .createParser(xContentRegistry(), LoggingDeprecationHandler.INSTANCE, nonClusterstateXContent.streamInput()); parsedConfig = DatafeedConfig.LENIENT_PARSER.apply(parser, null).build(); assertThat(parsedConfig.getHeaders().entrySet(), hasSize(0)); @@ -396,8 +423,10 @@ public void testCheckValid_GivenNullIndices() { public void testCheckValid_GivenInvalidMaxEmptySearches() { DatafeedConfig.Builder conf = new DatafeedConfig.Builder("datafeed1", "job1"); - ElasticsearchStatusException e = - expectThrows(ElasticsearchStatusException.class, () -> conf.setMaxEmptySearches(randomFrom(-2, 0))); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> conf.setMaxEmptySearches(randomFrom(-2, 0)) + ); assertThat(e.getMessage(), containsString("Invalid max_empty_searches value")); } @@ -437,8 +466,10 @@ public void testCheckValid_GivenIndicesContainsOnlyEmptyStrings() { public void testCheckValid_GivenNegativeQueryDelay() { DatafeedConfig.Builder conf = new DatafeedConfig.Builder("datafeed1", "job1"); - IllegalArgumentException e = ESTestCase.expectThrows(IllegalArgumentException.class, - () -> conf.setQueryDelay(TimeValue.timeValueMillis(-1))); + IllegalArgumentException e = ESTestCase.expectThrows( + IllegalArgumentException.class, + () -> conf.setQueryDelay(TimeValue.timeValueMillis(-1)) + ); assertEquals("query_delay cannot be less than 0. Value = -1", e.getMessage()); } @@ -450,8 +481,10 @@ public void testCheckValid_GivenZeroFrequency() { public void testCheckValid_GivenNegativeFrequency() { DatafeedConfig.Builder conf = new DatafeedConfig.Builder("datafeed1", "job1"); - IllegalArgumentException e = ESTestCase.expectThrows(IllegalArgumentException.class, - () -> conf.setFrequency(TimeValue.timeValueMinutes(-1))); + IllegalArgumentException e = ESTestCase.expectThrows( + IllegalArgumentException.class, + () -> conf.setFrequency(TimeValue.timeValueMinutes(-1)) + ); assertEquals("frequency cannot be less or equal than 0. Value = -1", e.getMessage()); } @@ -464,8 +497,11 @@ public void testCheckValid_GivenNegativeScrollSize() { public void testBuild_GivenScriptFieldsAndAggregations() { DatafeedConfig.Builder datafeed = new DatafeedConfig.Builder("datafeed1", "job1"); datafeed.setIndices(Collections.singletonList("my_index")); - datafeed.setScriptFields(Collections.singletonList(new SearchSourceBuilder.ScriptField(randomAlphaOfLength(10), - mockScript(randomAlphaOfLength(10)), randomBoolean()))); + datafeed.setScriptFields( + Collections.singletonList( + new SearchSourceBuilder.ScriptField(randomAlphaOfLength(10), mockScript(randomAlphaOfLength(10)), randomBoolean()) + ) + ); datafeed.setParsedAggregations(new AggregatorFactories.Builder().addAggregator(AggregationBuilders.avg("foo"))); ElasticsearchException e = expectThrows(ElasticsearchException.class, datafeed::build); @@ -508,10 +544,14 @@ public void testHasAggregations_NonEmpty() { DatafeedConfig.Builder builder = new DatafeedConfig.Builder("datafeed1", "job1"); builder.setIndices(Collections.singletonList("myIndex")); MaxAggregationBuilder maxTime = AggregationBuilders.max("time").field("time"); - builder.setParsedAggregations(new AggregatorFactories.Builder().addAggregator( + builder.setParsedAggregations( + new AggregatorFactories.Builder().addAggregator( AggregationBuilders.dateHistogram("time") .fixedInterval(new DateHistogramInterval(300000 + "ms")) - .subAggregation(maxTime).field("time"))); + .subAggregation(maxTime) + .field("time") + ) + ); DatafeedConfig datafeedConfig = builder.build(); assertThat(datafeedConfig.hasAggregations(), is(true)); @@ -531,8 +571,8 @@ public void testBuild_GivenHistogramWithDefaultInterval() { DatafeedConfig.Builder builder = new DatafeedConfig.Builder("datafeed1", "job1"); builder.setIndices(Collections.singletonList("myIndex")); MaxAggregationBuilder maxTime = AggregationBuilders.max("time").field("time"); - builder.setParsedAggregations(new AggregatorFactories.Builder().addAggregator( - AggregationBuilders.histogram("time").subAggregation(maxTime).field("time")) + builder.setParsedAggregations( + new AggregatorFactories.Builder().addAggregator(AggregationBuilders.histogram("time").subAggregation(maxTime).field("time")) ); ElasticsearchException e = expectThrows(ElasticsearchException.class, builder::build); @@ -542,10 +582,12 @@ public void testBuild_GivenHistogramWithDefaultInterval() { public void testBuild_GivenDateHistogramWithInvalidTimeZone() { MaxAggregationBuilder maxTime = AggregationBuilders.max("time").field("time"); - DateHistogramAggregationBuilder dateHistogram = AggregationBuilders.dateHistogram("bucket").field("time") - .fixedInterval(new DateHistogramInterval("30000ms")).timeZone(ZoneId.of("CET")).subAggregation(maxTime); - ElasticsearchException e = expectThrows(ElasticsearchException.class, - () -> createDatafeedWithDateHistogram(dateHistogram)); + DateHistogramAggregationBuilder dateHistogram = AggregationBuilders.dateHistogram("bucket") + .field("time") + .fixedInterval(new DateHistogramInterval("30000ms")) + .timeZone(ZoneId.of("CET")) + .subAggregation(maxTime); + ElasticsearchException e = expectThrows(ElasticsearchException.class, () -> createDatafeedWithDateHistogram(dateHistogram)); assertThat(e.getMessage(), equalTo("ML requires date_histogram.time_zone to be UTC")); } @@ -562,22 +604,27 @@ public void testBuild_GivenValidDateHistogram() { assertThat(createDatafeedWithDateHistogram("1d").getHistogramIntervalMillis(xContentRegistry()), equalTo(millisInDay)); assertThat(createDatafeedWithDateHistogram("7d").getHistogramIntervalMillis(xContentRegistry()), equalTo(7 * millisInDay)); - assertThat(createDatafeedWithDateHistogram(7 * millisInDay + 1).getHistogramIntervalMillis(xContentRegistry()), - equalTo(7 * millisInDay + 1)); + assertThat( + createDatafeedWithDateHistogram(7 * millisInDay + 1).getHistogramIntervalMillis(xContentRegistry()), + equalTo(7 * millisInDay + 1) + ); } public void testBuild_GivenDateHistogramWithMoreThanCalendarWeek() { - ElasticsearchException e = expectThrows(ElasticsearchException.class, - () -> createDatafeedWithDateHistogram("month")); + ElasticsearchException e = expectThrows(ElasticsearchException.class, () -> createDatafeedWithDateHistogram("month")); assertThat(e.getMessage(), containsString("When specifying a date_histogram calendar interval [month]")); } public void testDefaultChunkingConfig_GivenAggregations() { - assertThat(createDatafeedWithDateHistogram("1s").getChunkingConfig(), - equalTo(ChunkingConfig.newManual(TimeValue.timeValueSeconds(1000)))); - assertThat(createDatafeedWithDateHistogram("2h").getChunkingConfig(), - equalTo(ChunkingConfig.newManual(TimeValue.timeValueHours(2000)))); + assertThat( + createDatafeedWithDateHistogram("1s").getChunkingConfig(), + equalTo(ChunkingConfig.newManual(TimeValue.timeValueSeconds(1000))) + ); + assertThat( + createDatafeedWithDateHistogram("2h").getChunkingConfig(), + equalTo(ChunkingConfig.newManual(TimeValue.timeValueHours(2000))) + ); } public void testChunkingConfig_GivenExplicitSetting() { @@ -589,8 +636,10 @@ public void testChunkingConfig_GivenExplicitSetting() { public void testCheckHistogramAggregationHasChildMaxTimeAgg() { DateHistogramAggregationBuilder dateHistogram = AggregationBuilders.dateHistogram("time_agg").field("max_time"); - ElasticsearchException e = expectThrows(ElasticsearchException.class, - () -> DatafeedConfig.Builder.checkHistogramAggregationHasChildMaxTimeAgg(dateHistogram)); + ElasticsearchException e = expectThrows( + ElasticsearchException.class, + () -> DatafeedConfig.Builder.checkHistogramAggregationHasChildMaxTimeAgg(dateHistogram) + ); assertThat(e.getMessage(), containsString("Date histogram must have nested max aggregation for time_field [max_time]")); } @@ -600,8 +649,10 @@ public void testValidateCompositeAggValueSources_MustHaveExactlyOneDateValue() { "buckets", Arrays.asList(new TermsValuesSourceBuilder("foo").field("bar")) ); - ElasticsearchStatusException ex = expectThrows(ElasticsearchStatusException.class, - () -> DatafeedConfig.Builder.validateCompositeAggregationSources(aggregationBuilder)); + ElasticsearchStatusException ex = expectThrows( + ElasticsearchStatusException.class, + () -> DatafeedConfig.Builder.validateCompositeAggregationSources(aggregationBuilder) + ); assertThat(ex.getMessage(), containsString("must have exactly one date_histogram source")); CompositeAggregationBuilder aggregationBuilderWithMoreDateHisto = AggregationBuilders.composite( @@ -612,23 +663,25 @@ public void testValidateCompositeAggValueSources_MustHaveExactlyOneDateValue() { new DateHistogramValuesSourceBuilder("date2").field("time").fixedInterval(DateHistogramInterval.days(1)) ) ); - ex = expectThrows(ElasticsearchStatusException.class, - () -> DatafeedConfig.Builder.validateCompositeAggregationSources(aggregationBuilderWithMoreDateHisto)); + ex = expectThrows( + ElasticsearchStatusException.class, + () -> DatafeedConfig.Builder.validateCompositeAggregationSources(aggregationBuilderWithMoreDateHisto) + ); assertThat(ex.getMessage(), containsString("must have exactly one date_histogram source")); } + public void testValidateCompositeAggValueSources_DateHistoWithMissingBucket() { CompositeAggregationBuilder aggregationBuilder = AggregationBuilders.composite( "buckets", Arrays.asList( new TermsValuesSourceBuilder("foo").field("bar"), - new DateHistogramValuesSourceBuilder("date1") - .field("time") - .fixedInterval(DateHistogramInterval.days(1)) - .missingBucket(true) + new DateHistogramValuesSourceBuilder("date1").field("time").fixedInterval(DateHistogramInterval.days(1)).missingBucket(true) ) ); - ElasticsearchStatusException ex = expectThrows(ElasticsearchStatusException.class, - () -> DatafeedConfig.Builder.validateCompositeAggregationSources(aggregationBuilder)); + ElasticsearchStatusException ex = expectThrows( + ElasticsearchStatusException.class, + () -> DatafeedConfig.Builder.validateCompositeAggregationSources(aggregationBuilder) + ); assertThat(ex.getMessage(), containsString("does not support missing_buckets")); } @@ -637,14 +690,13 @@ public void testValidateCompositeAggValueSources_DateHistoBadOrder() { "buckets", Arrays.asList( new TermsValuesSourceBuilder("foo").field("bar"), - new DateHistogramValuesSourceBuilder("date1") - .field("time") - .fixedInterval(DateHistogramInterval.days(1)) - .order("desc") + new DateHistogramValuesSourceBuilder("date1").field("time").fixedInterval(DateHistogramInterval.days(1)).order("desc") ) ); - ElasticsearchStatusException ex = expectThrows(ElasticsearchStatusException.class, - () -> DatafeedConfig.Builder.validateCompositeAggregationSources(aggregationBuilder)); + ElasticsearchStatusException ex = expectThrows( + ElasticsearchStatusException.class, + () -> DatafeedConfig.Builder.validateCompositeAggregationSources(aggregationBuilder) + ); assertThat(ex.getMessage(), containsString("must be sorted in ascending order")); } @@ -661,16 +713,20 @@ public void testValidateAggregations_GivenMulitpleHistogramAggs() { TermsAggregationBuilder toplevelTerms = AggregationBuilders.terms("top_level"); toplevelTerms.subAggregation(dateHistogram); - ElasticsearchException e = expectThrows(ElasticsearchException.class, - () -> DatafeedConfig.validateAggregations(new AggregatorFactories.Builder().addAggregator(toplevelTerms))); + ElasticsearchException e = expectThrows( + ElasticsearchException.class, + () -> DatafeedConfig.validateAggregations(new AggregatorFactories.Builder().addAggregator(toplevelTerms)) + ); assertEquals("Aggregations can only have 1 date_histogram or histogram aggregation", e.getMessage()); } public void testDefaultFrequency_GivenNegative() { DatafeedConfig datafeed = createTestInstance(); - ESTestCase.expectThrows(IllegalArgumentException.class, - () -> datafeed.defaultFrequency(TimeValue.timeValueSeconds(-1), xContentRegistry())); + ESTestCase.expectThrows( + IllegalArgumentException.class, + () -> datafeed.defaultFrequency(TimeValue.timeValueSeconds(-1), xContentRegistry()) + ); } public void testDefaultFrequency_GivenNoAggregations() { @@ -703,9 +759,7 @@ public void testDefaultFrequency_GivenNoAggregations() { } public void testDefaultFrequency_GivenAggregationsWithHistogramOrCompositeInterval_1_Second() { - DatafeedConfig datafeed = randomBoolean() ? - createDatafeedWithDateHistogram("1s") : - createDatafeedWithCompositeAgg("1s"); + DatafeedConfig datafeed = randomBoolean() ? createDatafeedWithDateHistogram("1s") : createDatafeedWithCompositeAgg("1s"); assertEquals(TimeValue.timeValueMinutes(1), datafeed.defaultFrequency(TimeValue.timeValueSeconds(60), xContentRegistry())); assertEquals(TimeValue.timeValueMinutes(1), datafeed.defaultFrequency(TimeValue.timeValueSeconds(90), xContentRegistry())); @@ -718,9 +772,7 @@ public void testDefaultFrequency_GivenAggregationsWithHistogramOrCompositeInterv } public void testDefaultFrequency_GivenAggregationsWithHistogramOrCompositeInterval_1_Minute() { - DatafeedConfig datafeed = randomBoolean() ? - createDatafeedWithDateHistogram("1m") : - createDatafeedWithCompositeAgg("1m"); + DatafeedConfig datafeed = randomBoolean() ? createDatafeedWithDateHistogram("1m") : createDatafeedWithCompositeAgg("1m"); assertEquals(TimeValue.timeValueMinutes(1), datafeed.defaultFrequency(TimeValue.timeValueSeconds(60), xContentRegistry())); assertEquals(TimeValue.timeValueMinutes(1), datafeed.defaultFrequency(TimeValue.timeValueSeconds(90), xContentRegistry())); @@ -729,8 +781,10 @@ public void testDefaultFrequency_GivenAggregationsWithHistogramOrCompositeInterv assertEquals(TimeValue.timeValueMinutes(2), datafeed.defaultFrequency(TimeValue.timeValueSeconds(240), xContentRegistry())); assertEquals(TimeValue.timeValueMinutes(10), datafeed.defaultFrequency(TimeValue.timeValueMinutes(20), xContentRegistry())); - assertEquals(TimeValue.timeValueMinutes(10), - datafeed.defaultFrequency(TimeValue.timeValueSeconds(20 * 60 + 1), xContentRegistry())); + assertEquals( + TimeValue.timeValueMinutes(10), + datafeed.defaultFrequency(TimeValue.timeValueSeconds(20 * 60 + 1), xContentRegistry()) + ); assertEquals(TimeValue.timeValueMinutes(10), datafeed.defaultFrequency(TimeValue.timeValueHours(6), xContentRegistry())); assertEquals(TimeValue.timeValueMinutes(10), datafeed.defaultFrequency(TimeValue.timeValueHours(12), xContentRegistry())); @@ -739,9 +793,7 @@ public void testDefaultFrequency_GivenAggregationsWithHistogramOrCompositeInterv } public void testDefaultFrequency_GivenAggregationsWithHistogramOrCompositeInterval_10_Minutes() { - DatafeedConfig datafeed = randomBoolean() ? - createDatafeedWithDateHistogram("10m") : - createDatafeedWithCompositeAgg("10m"); + DatafeedConfig datafeed = randomBoolean() ? createDatafeedWithDateHistogram("10m") : createDatafeedWithCompositeAgg("10m"); assertEquals(TimeValue.timeValueMinutes(10), datafeed.defaultFrequency(TimeValue.timeValueMinutes(10), xContentRegistry())); assertEquals(TimeValue.timeValueMinutes(10), datafeed.defaultFrequency(TimeValue.timeValueMinutes(20), xContentRegistry())); assertEquals(TimeValue.timeValueMinutes(10), datafeed.defaultFrequency(TimeValue.timeValueMinutes(30), xContentRegistry())); @@ -750,9 +802,7 @@ public void testDefaultFrequency_GivenAggregationsWithHistogramOrCompositeInterv } public void testDefaultFrequency_GivenAggregationsWithHistogramOrCompositeInterval_1_Hour() { - DatafeedConfig datafeed = randomBoolean() ? - createDatafeedWithDateHistogram("1h") : - createDatafeedWithCompositeAgg("1h"); + DatafeedConfig datafeed = randomBoolean() ? createDatafeedWithDateHistogram("1h") : createDatafeedWithCompositeAgg("1h"); assertEquals(TimeValue.timeValueHours(1), datafeed.defaultFrequency(TimeValue.timeValueHours(1), xContentRegistry())); assertEquals(TimeValue.timeValueHours(1), datafeed.defaultFrequency(TimeValue.timeValueSeconds(3601), xContentRegistry())); assertEquals(TimeValue.timeValueHours(1), datafeed.defaultFrequency(TimeValue.timeValueHours(2), xContentRegistry())); @@ -762,32 +812,38 @@ public void testDefaultFrequency_GivenAggregationsWithHistogramOrCompositeInterv public void testSerializationOfComplexAggs() throws IOException { MaxAggregationBuilder maxTime = AggregationBuilders.max("timestamp").field("timestamp"); AvgAggregationBuilder avgAggregationBuilder = AggregationBuilders.avg("bytes_in_avg").field("system.network.in.bytes"); - DerivativePipelineAggregationBuilder derivativePipelineAggregationBuilder = - PipelineAggregatorBuilders.derivative("bytes_in_derivative", "bytes_in_avg"); - BucketScriptPipelineAggregationBuilder bucketScriptPipelineAggregationBuilder = - PipelineAggregatorBuilders.bucketScript("non_negative_bytes", - Collections.singletonMap("bytes", "bytes_in_derivative"), - new Script("params.bytes > 0 ? params.bytes : null")); - DateHistogramAggregationBuilder dateHistogram = - AggregationBuilders.dateHistogram("histogram_buckets") - .field("timestamp").fixedInterval(new DateHistogramInterval("300000ms")).timeZone(ZoneOffset.UTC) - .subAggregation(maxTime) - .subAggregation(avgAggregationBuilder) - .subAggregation(derivativePipelineAggregationBuilder) - .subAggregation(bucketScriptPipelineAggregationBuilder); + DerivativePipelineAggregationBuilder derivativePipelineAggregationBuilder = PipelineAggregatorBuilders.derivative( + "bytes_in_derivative", + "bytes_in_avg" + ); + BucketScriptPipelineAggregationBuilder bucketScriptPipelineAggregationBuilder = PipelineAggregatorBuilders.bucketScript( + "non_negative_bytes", + Collections.singletonMap("bytes", "bytes_in_derivative"), + new Script("params.bytes > 0 ? params.bytes : null") + ); + DateHistogramAggregationBuilder dateHistogram = AggregationBuilders.dateHistogram("histogram_buckets") + .field("timestamp") + .fixedInterval(new DateHistogramInterval("300000ms")) + .timeZone(ZoneOffset.UTC) + .subAggregation(maxTime) + .subAggregation(avgAggregationBuilder) + .subAggregation(derivativePipelineAggregationBuilder) + .subAggregation(bucketScriptPipelineAggregationBuilder); DatafeedConfig.Builder datafeedConfigBuilder = createDatafeedBuilderWithDateHistogram(dateHistogram); datafeedConfigBuilder.setQueryProvider( - createRandomValidQueryProvider(randomAlphaOfLengthBetween(1, 10), randomAlphaOfLengthBetween(1, 10))); + createRandomValidQueryProvider(randomAlphaOfLengthBetween(1, 10), randomAlphaOfLengthBetween(1, 10)) + ); DatafeedConfig datafeedConfig = datafeedConfigBuilder.build(); AggregatorFactories.Builder aggBuilder = new AggregatorFactories.Builder().addAggregator(dateHistogram); - XContentType xContentType = XContentType.JSON; BytesReference bytes = XContentHelper.toXContent(datafeedConfig, xContentType, false); - XContentParser parser = XContentHelper.createParser(xContentRegistry(), + XContentParser parser = XContentHelper.createParser( + xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, bytes, - xContentType); + xContentType + ); DatafeedConfig parsedDatafeedConfig = doParseInstance(parser); assertEquals(datafeedConfig, parsedDatafeedConfig); @@ -798,9 +854,9 @@ public void testSerializationOfComplexAggs() throws IOException { SearchModule searchModule = new SearchModule(Settings.EMPTY, Collections.emptyList()); NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(searchModule.getNamedWriteables()); - try(BytesStreamOutput output = new BytesStreamOutput()) { + try (BytesStreamOutput output = new BytesStreamOutput()) { datafeedConfig.writeTo(output); - try(StreamInput streamInput = new NamedWriteableAwareStreamInput(output.bytes().streamInput(), namedWriteableRegistry)) { + try (StreamInput streamInput = new NamedWriteableAwareStreamInput(output.bytes().streamInput(), namedWriteableRegistry)) { DatafeedConfig streamedDatafeedConfig = new DatafeedConfig(streamInput); assertEquals(datafeedConfig, streamedDatafeedConfig); @@ -814,27 +870,33 @@ public void testSerializationOfComplexAggs() throws IOException { public void testSerializationOfComplexAggsBetweenVersions() throws IOException { MaxAggregationBuilder maxTime = AggregationBuilders.max("timestamp").field("timestamp"); AvgAggregationBuilder avgAggregationBuilder = AggregationBuilders.avg("bytes_in_avg").field("system.network.in.bytes"); - DerivativePipelineAggregationBuilder derivativePipelineAggregationBuilder = - PipelineAggregatorBuilders.derivative("bytes_in_derivative", "bytes_in_avg"); - BucketScriptPipelineAggregationBuilder bucketScriptPipelineAggregationBuilder = - PipelineAggregatorBuilders.bucketScript("non_negative_bytes", - Collections.singletonMap("bytes", "bytes_in_derivative"), - new Script("params.bytes > 0 ? params.bytes : null")); - DateHistogramAggregationBuilder dateHistogram = - AggregationBuilders.dateHistogram("histogram_buckets") - .field("timestamp").fixedInterval(new DateHistogramInterval("30000ms")).timeZone(ZoneOffset.UTC) - .subAggregation(maxTime) - .subAggregation(avgAggregationBuilder) - .subAggregation(derivativePipelineAggregationBuilder) - .subAggregation(bucketScriptPipelineAggregationBuilder); + DerivativePipelineAggregationBuilder derivativePipelineAggregationBuilder = PipelineAggregatorBuilders.derivative( + "bytes_in_derivative", + "bytes_in_avg" + ); + BucketScriptPipelineAggregationBuilder bucketScriptPipelineAggregationBuilder = PipelineAggregatorBuilders.bucketScript( + "non_negative_bytes", + Collections.singletonMap("bytes", "bytes_in_derivative"), + new Script("params.bytes > 0 ? params.bytes : null") + ); + DateHistogramAggregationBuilder dateHistogram = AggregationBuilders.dateHistogram("histogram_buckets") + .field("timestamp") + .fixedInterval(new DateHistogramInterval("30000ms")) + .timeZone(ZoneOffset.UTC) + .subAggregation(maxTime) + .subAggregation(avgAggregationBuilder) + .subAggregation(derivativePipelineAggregationBuilder) + .subAggregation(bucketScriptPipelineAggregationBuilder); DatafeedConfig.Builder datafeedConfigBuilder = createDatafeedBuilderWithDateHistogram(dateHistogram); // So equality check between the streamed and current passes // Streamed DatafeedConfigs when they are before 6.6.0 require a parsed object for aggs and queries, consequently all the default // values are added between them datafeedConfigBuilder.setQueryProvider( - QueryProvider - .fromParsedQuery(QueryBuilders.boolQuery() - .filter(QueryBuilders.termQuery(randomAlphaOfLengthBetween(1, 10), randomAlphaOfLengthBetween(1, 10))))); + QueryProvider.fromParsedQuery( + QueryBuilders.boolQuery() + .filter(QueryBuilders.termQuery(randomAlphaOfLengthBetween(1, 10), randomAlphaOfLengthBetween(1, 10))) + ) + ); DatafeedConfig datafeedConfig = datafeedConfigBuilder.build(); SearchModule searchModule = new SearchModule(Settings.EMPTY, Collections.emptyList()); @@ -849,8 +911,10 @@ public void testSerializationOfComplexAggsBetweenVersions() throws IOException { assertEquals(datafeedConfig, streamedDatafeedConfig); // Assert that the parsed versions of our aggs and queries work as well - assertEquals(new AggregatorFactories.Builder().addAggregator(dateHistogram), - streamedDatafeedConfig.getParsedAggregations(xContentRegistry())); + assertEquals( + new AggregatorFactories.Builder().addAggregator(dateHistogram), + streamedDatafeedConfig.getParsedAggregations(xContentRegistry()) + ); assertEquals(datafeedConfig.getParsedQuery(xContentRegistry()), streamedDatafeedConfig.getParsedQuery(xContentRegistry())); } } @@ -864,7 +928,7 @@ public void testCopyingDatafeedDoesNotCauseStackOverflow() { } public static String randomValidDatafeedId() { - CodepointSetGenerator generator = new CodepointSetGenerator("abcdefghijklmnopqrstuvwxyz".toCharArray()); + CodepointSetGenerator generator = new CodepointSetGenerator("abcdefghijklmnopqrstuvwxyz".toCharArray()); return generator.ofCodePointsLength(random(), 10, 10); } @@ -879,10 +943,8 @@ private static DatafeedConfig createDatafeedWithCompositeAgg(String interval) { sourceBuilder.fixedInterval(new DateHistogramInterval(interval)); } } - CompositeAggregationBuilder composite = AggregationBuilders.composite( - "buckets", - Arrays.asList(sourceBuilder) - ).subAggregation(maxTime); + CompositeAggregationBuilder composite = AggregationBuilders.composite("buckets", Arrays.asList(sourceBuilder)) + .subAggregation(maxTime); return createDatafeedWithComposite(composite); } @@ -938,85 +1000,93 @@ private static DatafeedConfig createDatafeedWithComposite(CompositeAggregationBu protected DatafeedConfig mutateInstance(DatafeedConfig instance) throws IOException { DatafeedConfig.Builder builder = new DatafeedConfig.Builder(instance); switch (between(0, 12)) { - case 0: - builder.setId(instance.getId() + randomValidDatafeedId()); - break; - case 1: - builder.setJobId(instance.getJobId() + randomAlphaOfLength(5)); - break; - case 2: - builder.setQueryDelay(new TimeValue(instance.getQueryDelay().millis() + between(100, 100000))); - break; - case 3: - if (instance.getFrequency() == null) { - builder.setFrequency(new TimeValue(between(1, 10) * 1000)); - } else { - builder.setFrequency(new TimeValue(instance.getFrequency().millis() + between(1, 10) * 1000)); - } - break; - case 4: - List indices = new ArrayList<>(instance.getIndices()); - indices.add(randomAlphaOfLengthBetween(1, 20)); - builder.setIndices(indices); - break; - case 5: - BoolQueryBuilder query = new BoolQueryBuilder(); - if (instance.getParsedQuery(xContentRegistry()) != null) { - query.must(instance.getParsedQuery(xContentRegistry())); - } - query.filter(new TermQueryBuilder(randomAlphaOfLengthBetween(1, 10), randomAlphaOfLengthBetween(1, 10))); - builder.setParsedQuery(query); - break; - case 6: - if (instance.hasAggregations()) { - builder.setAggProvider(null); - } else { - AggregatorFactories.Builder aggBuilder = new AggregatorFactories.Builder(); - String timeField = randomAlphaOfLength(10); - long fixedInterval = between(10000, 3600000); + case 0: + builder.setId(instance.getId() + randomValidDatafeedId()); + break; + case 1: + builder.setJobId(instance.getJobId() + randomAlphaOfLength(5)); + break; + case 2: + builder.setQueryDelay(new TimeValue(instance.getQueryDelay().millis() + between(100, 100000))); + break; + case 3: + if (instance.getFrequency() == null) { + builder.setFrequency(new TimeValue(between(1, 10) * 1000)); + } else { + builder.setFrequency(new TimeValue(instance.getFrequency().millis() + between(1, 10) * 1000)); + } + break; + case 4: + List indices = new ArrayList<>(instance.getIndices()); + indices.add(randomAlphaOfLengthBetween(1, 20)); + builder.setIndices(indices); + break; + case 5: + BoolQueryBuilder query = new BoolQueryBuilder(); + if (instance.getParsedQuery(xContentRegistry()) != null) { + query.must(instance.getParsedQuery(xContentRegistry())); + } + query.filter(new TermQueryBuilder(randomAlphaOfLengthBetween(1, 10), randomAlphaOfLengthBetween(1, 10))); + builder.setParsedQuery(query); + break; + case 6: + if (instance.hasAggregations()) { + builder.setAggProvider(null); + } else { + AggregatorFactories.Builder aggBuilder = new AggregatorFactories.Builder(); + String timeField = randomAlphaOfLength(10); + long fixedInterval = between(10000, 3600000); - aggBuilder - .addAggregator(new DateHistogramAggregationBuilder(timeField).field(timeField) + aggBuilder.addAggregator( + new DateHistogramAggregationBuilder(timeField).field(timeField) .fixedInterval(new DateHistogramInterval(fixedInterval + "ms")) - .subAggregation(new MaxAggregationBuilder(timeField).field(timeField))); - builder.setParsedAggregations(aggBuilder); - if (instance.getScriptFields().isEmpty() == false) { - builder.setScriptFields(Collections.emptyList()); + .subAggregation(new MaxAggregationBuilder(timeField).field(timeField)) + ); + builder.setParsedAggregations(aggBuilder); + if (instance.getScriptFields().isEmpty() == false) { + builder.setScriptFields(Collections.emptyList()); + } } - } - break; - case 7: - builder.setScriptFields(CollectionUtils.appendToCopy( - instance.getScriptFields(), new ScriptField(randomAlphaOfLengthBetween(1, 10), new Script("foo"), true))); - builder.setAggProvider(null); - break; - case 8: - builder.setScrollSize(instance.getScrollSize() + between(1, 100)); - break; - case 9: - if (instance.getChunkingConfig() == null || instance.getChunkingConfig().getMode() == Mode.AUTO) { - ChunkingConfig newChunkingConfig = ChunkingConfig.newManual(new TimeValue(randomNonNegativeLong())); - builder.setChunkingConfig(newChunkingConfig); - } else { - builder.setChunkingConfig(ChunkingConfig.newAuto()); - } - break; - case 10: - if (instance.getMaxEmptySearches() == null) { - builder.setMaxEmptySearches(randomIntBetween(10, 100)); - } else { - builder.setMaxEmptySearches(instance.getMaxEmptySearches() + 1); - } - break; - case 11: - builder.setIndicesOptions(IndicesOptions.fromParameters( - randomFrom(IndicesOptions.WildcardStates.values()).name().toLowerCase(Locale.ROOT), - Boolean.toString(instance.getIndicesOptions().ignoreUnavailable() == false), - Boolean.toString(instance.getIndicesOptions().allowNoIndices() == false), - Boolean.toString(instance.getIndicesOptions().ignoreThrottled() == false), - SearchRequest.DEFAULT_INDICES_OPTIONS)); - break; - case 12: + break; + case 7: + builder.setScriptFields( + CollectionUtils.appendToCopy( + instance.getScriptFields(), + new ScriptField(randomAlphaOfLengthBetween(1, 10), new Script("foo"), true) + ) + ); + builder.setAggProvider(null); + break; + case 8: + builder.setScrollSize(instance.getScrollSize() + between(1, 100)); + break; + case 9: + if (instance.getChunkingConfig() == null || instance.getChunkingConfig().getMode() == Mode.AUTO) { + ChunkingConfig newChunkingConfig = ChunkingConfig.newManual(new TimeValue(randomNonNegativeLong())); + builder.setChunkingConfig(newChunkingConfig); + } else { + builder.setChunkingConfig(ChunkingConfig.newAuto()); + } + break; + case 10: + if (instance.getMaxEmptySearches() == null) { + builder.setMaxEmptySearches(randomIntBetween(10, 100)); + } else { + builder.setMaxEmptySearches(instance.getMaxEmptySearches() + 1); + } + break; + case 11: + builder.setIndicesOptions( + IndicesOptions.fromParameters( + randomFrom(IndicesOptions.WildcardStates.values()).name().toLowerCase(Locale.ROOT), + Boolean.toString(instance.getIndicesOptions().ignoreUnavailable() == false), + Boolean.toString(instance.getIndicesOptions().allowNoIndices() == false), + Boolean.toString(instance.getIndicesOptions().ignoreThrottled() == false), + SearchRequest.DEFAULT_INDICES_OPTIONS + ) + ); + break; + case 12: if (instance.getRuntimeMappings() != null && instance.getRuntimeMappings().isEmpty() == false) { builder.setRuntimeMappings(Collections.emptyMap()); } else { @@ -1027,9 +1097,9 @@ protected DatafeedConfig mutateInstance(DatafeedConfig instance) throws IOExcept field.put("runtime_field_foo", settings); builder.setRuntimeMappings(field); } - break; - default: - throw new AssertionError("Illegal randomisation branch"); + break; + default: + throw new AssertionError("Illegal randomisation branch"); } return builder.build(); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedTimingStatsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedTimingStatsTests.java index 8af6cf3b5124c..8d7dd0c931161 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedTimingStatsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedTimingStatsTests.java @@ -7,12 +7,12 @@ package org.elasticsearch.xpack.core.ml.datafeed; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xpack.core.ml.utils.ExponentialAverageCalculationContext; import org.elasticsearch.xpack.core.ml.utils.ExponentialAverageCalculationContextTests; import org.elasticsearch.xpack.core.ml.utils.ToXContentParams; @@ -35,11 +35,12 @@ public static DatafeedTimingStats createRandom() { randomLong(), randomLong(), randomDouble(), - ExponentialAverageCalculationContextTests.createRandom()); + ExponentialAverageCalculationContextTests.createRandom() + ); } @Override - protected DatafeedTimingStats createTestInstance(){ + protected DatafeedTimingStats createTestInstance() { return createRandom(); } @@ -70,14 +71,16 @@ protected DatafeedTimingStats mutateInstance(DatafeedTimingStats instance) throw searchCount + 2, bucketCount + 1, totalSearchTimeMs + randomDoubleBetween(1.0, 100.0, true), - new ExponentialAverageCalculationContext(incrementalSearchTimeMs + randomDoubleBetween(1.0, 100.0, true), null, null)); + new ExponentialAverageCalculationContext(incrementalSearchTimeMs + randomDoubleBetween(1.0, 100.0, true), null, null) + ); } public void testParse_OptionalFieldsAbsent() throws IOException { String json = "{\"job_id\": \"my-job-id\"}"; - try (XContentParser parser = - XContentFactory.xContent(XContentType.JSON).createParser( - xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json)) { + try ( + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json) + ) { DatafeedTimingStats stats = DatafeedTimingStats.PARSER.apply(parser, null); assertThat(stats.getJobId(), equalTo(JOB_ID)); assertThat(stats.getSearchCount(), equalTo(0L)); @@ -89,8 +92,11 @@ public void testParse_OptionalFieldsAbsent() throws IOException { } public void testConstructor() { - ExponentialAverageCalculationContext context = - new ExponentialAverageCalculationContext(78.9, Instant.ofEpochMilli(123456789), 987.0); + ExponentialAverageCalculationContext context = new ExponentialAverageCalculationContext( + 78.9, + Instant.ofEpochMilli(123456789), + 987.0 + ); DatafeedTimingStats stats = new DatafeedTimingStats(JOB_ID, 5, 10, 123.456, context); assertThat(stats.getJobId(), equalTo(JOB_ID)); assertThat(stats.getSearchCount(), equalTo(5L)); @@ -111,8 +117,11 @@ public void testDefaultConstructor() { } public void testCopyConstructor() { - ExponentialAverageCalculationContext context = - new ExponentialAverageCalculationContext(78.9, Instant.ofEpochMilli(123456789), 987.0); + ExponentialAverageCalculationContext context = new ExponentialAverageCalculationContext( + 78.9, + Instant.ofEpochMilli(123456789), + 987.0 + ); DatafeedTimingStats stats1 = new DatafeedTimingStats(JOB_ID, 5, 10, 123.456, context); DatafeedTimingStats stats2 = new DatafeedTimingStats(stats1); @@ -125,8 +134,13 @@ public void testCopyConstructor() { } public void testIncrementTotalSearchTimeMs() { - DatafeedTimingStats stats = - new DatafeedTimingStats(JOB_ID, 5, 10, 100.0, new ExponentialAverageCalculationContext(50.0, null, null)); + DatafeedTimingStats stats = new DatafeedTimingStats( + JOB_ID, + 5, + 10, + 100.0, + new ExponentialAverageCalculationContext(50.0, null, null) + ); stats.incrementSearchTimeMs(200.0); assertThat(stats.getJobId(), equalTo(JOB_ID)); assertThat(stats.getSearchCount(), equalTo(6L)); @@ -137,8 +151,13 @@ public void testIncrementTotalSearchTimeMs() { } public void testIncrementBucketCount() { - DatafeedTimingStats stats = - new DatafeedTimingStats(JOB_ID, 5, 10, 100.0, new ExponentialAverageCalculationContext(50.0, null, null)); + DatafeedTimingStats stats = new DatafeedTimingStats( + JOB_ID, + 5, + 10, + 100.0, + new ExponentialAverageCalculationContext(50.0, null, null) + ); stats.incrementBucketCount(10); assertThat(stats.getJobId(), equalTo(JOB_ID)); assertThat(stats.getSearchCount(), equalTo(5L)); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedUpdateTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedUpdateTests.java index 642ff8979f7f3..68b021bcce63c 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedUpdateTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedUpdateTests.java @@ -10,21 +10,15 @@ import org.elasticsearch.Version; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.support.IndicesOptions; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.CollectionUtils; -import org.elasticsearch.xcontent.DeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentParseException; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.TermQueryBuilder; @@ -43,6 +37,12 @@ import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.builder.SearchSourceBuilder.ScriptField; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.DeprecationHandler; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentParseException; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.ml.datafeed.ChunkingConfig.Mode; import org.elasticsearch.xpack.core.ml.job.config.JobTests; import org.elasticsearch.xpack.core.ml.utils.QueryProvider; @@ -93,8 +93,9 @@ public static DatafeedUpdate createRandomized(String datafeedId, @Nullable Dataf int scriptsSize = randomInt(3); List scriptFields = new ArrayList<>(scriptsSize); for (int scriptIndex = 0; scriptIndex < scriptsSize; scriptIndex++) { - scriptFields.add(new SearchSourceBuilder.ScriptField(randomAlphaOfLength(10), mockScript(randomAlphaOfLength(10)), - randomBoolean())); + scriptFields.add( + new SearchSourceBuilder.ScriptField(randomAlphaOfLength(10), mockScript(randomAlphaOfLength(10)), randomBoolean()) + ); } builder.setScriptFields(scriptFields); } @@ -117,12 +118,15 @@ public static DatafeedUpdate createRandomized(String datafeedId, @Nullable Dataf builder.setMaxEmptySearches(randomBoolean() ? -1 : randomIntBetween(10, 100)); } if (randomBoolean()) { - builder.setIndicesOptions(IndicesOptions.fromParameters( - randomFrom(IndicesOptions.WildcardStates.values()).name().toLowerCase(Locale.ROOT), - Boolean.toString(randomBoolean()), - Boolean.toString(randomBoolean()), - Boolean.toString(randomBoolean()), - SearchRequest.DEFAULT_INDICES_OPTIONS)); + builder.setIndicesOptions( + IndicesOptions.fromParameters( + randomFrom(IndicesOptions.WildcardStates.values()).name().toLowerCase(Locale.ROOT), + Boolean.toString(randomBoolean()), + Boolean.toString(randomBoolean()), + Boolean.toString(randomBoolean()), + SearchRequest.DEFAULT_INDICES_OPTIONS + ) + ); } if (randomBoolean()) { Map settings = new HashMap<>(); @@ -157,46 +161,47 @@ protected NamedXContentRegistry xContentRegistry() { return new NamedXContentRegistry(searchModule.getNamedXContents()); } - private static final String MULTIPLE_AGG_DEF_DATAFEED = "{\n" + - " \"datafeed_id\": \"farequote-datafeed\",\n" + - " \"job_id\": \"farequote\",\n" + - " \"frequency\": \"1h\",\n" + - " \"indices\": [\"farequote1\", \"farequote2\"],\n" + - " \"aggregations\": {\n" + - " \"buckets\": {\n" + - " \"date_histogram\": {\n" + - " \"field\": \"time\",\n" + - " \"fixed_interval\": \"360s\",\n" + - " \"time_zone\": \"UTC\"\n" + - " },\n" + - " \"aggregations\": {\n" + - " \"time\": {\n" + - " \"max\": {\"field\": \"time\"}\n" + - " }\n" + - " }\n" + - " }\n" + - " }," + - " \"aggs\": {\n" + - " \"buckets2\": {\n" + - " \"date_histogram\": {\n" + - " \"field\": \"time\",\n" + - " \"fixed_interval\": \"360s\",\n" + - " \"time_zone\": \"UTC\"\n" + - " },\n" + - " \"aggregations\": {\n" + - " \"time\": {\n" + - " \"max\": {\"field\": \"time\"}\n" + - " }\n" + - " }\n" + - " }\n" + - " }\n" + - "}"; + private static final String MULTIPLE_AGG_DEF_DATAFEED = "{\n" + + " \"datafeed_id\": \"farequote-datafeed\",\n" + + " \"job_id\": \"farequote\",\n" + + " \"frequency\": \"1h\",\n" + + " \"indices\": [\"farequote1\", \"farequote2\"],\n" + + " \"aggregations\": {\n" + + " \"buckets\": {\n" + + " \"date_histogram\": {\n" + + " \"field\": \"time\",\n" + + " \"fixed_interval\": \"360s\",\n" + + " \"time_zone\": \"UTC\"\n" + + " },\n" + + " \"aggregations\": {\n" + + " \"time\": {\n" + + " \"max\": {\"field\": \"time\"}\n" + + " }\n" + + " }\n" + + " }\n" + + " }," + + " \"aggs\": {\n" + + " \"buckets2\": {\n" + + " \"date_histogram\": {\n" + + " \"field\": \"time\",\n" + + " \"fixed_interval\": \"360s\",\n" + + " \"time_zone\": \"UTC\"\n" + + " },\n" + + " \"aggregations\": {\n" + + " \"time\": {\n" + + " \"max\": {\"field\": \"time\"}\n" + + " }\n" + + " }\n" + + " }\n" + + " }\n" + + "}"; public void testMultipleDefinedAggParse() throws IOException { - try(XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, MULTIPLE_AGG_DEF_DATAFEED)) { - XContentParseException ex = expectThrows(XContentParseException.class, - () -> DatafeedUpdate.PARSER.apply(parser, null)); + try ( + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, MULTIPLE_AGG_DEF_DATAFEED) + ) { + XContentParseException ex = expectThrows(XContentParseException.class, () -> DatafeedUpdate.PARSER.apply(parser, null)); assertThat(ex.getMessage(), equalTo("[32:3] [datafeed_update] failed to parse field [aggs]")); assertNotNull(ex.getCause()); assertThat(ex.getCause().getMessage(), equalTo("Found two aggregation definitions: [aggs] and [aggregations]")); @@ -205,33 +210,28 @@ public void testMultipleDefinedAggParse() throws IOException { public void testApply_failBecauseTargetDatafeedHasDifferentId() { DatafeedConfig datafeed = DatafeedConfigTests.createRandomizedDatafeedConfig("foo"); - expectThrows(IllegalArgumentException.class, () -> createRandomized(datafeed.getId() + "_2") - .apply(datafeed, null)); + expectThrows(IllegalArgumentException.class, () -> createRandomized(datafeed.getId() + "_2").apply(datafeed, null)); } public void testApply_failBecauseJobIdChanged() { DatafeedConfig datafeed = DatafeedConfigTests.createRandomizedDatafeedConfig("foo"); - DatafeedUpdate datafeedUpdateWithUnchangedJobId = new DatafeedUpdate.Builder(datafeed.getId()) - .setJobId("foo") - .build(); + DatafeedUpdate datafeedUpdateWithUnchangedJobId = new DatafeedUpdate.Builder(datafeed.getId()).setJobId("foo").build(); DatafeedConfig updatedDatafeed = datafeedUpdateWithUnchangedJobId.apply(datafeed, Collections.emptyMap()); assertThat(updatedDatafeed, equalTo(datafeed)); - DatafeedUpdate datafeedUpdateWithChangedJobId = new DatafeedUpdate.Builder(datafeed.getId()) - .setJobId("bar") - .build(); + DatafeedUpdate datafeedUpdateWithChangedJobId = new DatafeedUpdate.Builder(datafeed.getId()).setJobId("bar").build(); ElasticsearchStatusException ex = expectThrows( ElasticsearchStatusException.class, - () -> datafeedUpdateWithChangedJobId.apply(datafeed, Collections.emptyMap())); + () -> datafeedUpdateWithChangedJobId.apply(datafeed, Collections.emptyMap()) + ); assertThat(ex.status(), equalTo(RestStatus.BAD_REQUEST)); assertThat(ex.getMessage(), equalTo(DatafeedUpdate.ERROR_MESSAGE_ON_JOB_ID_UPDATE)); } public void testApply_givenEmptyUpdate() { DatafeedConfig datafeed = DatafeedConfigTests.createRandomizedDatafeedConfig("foo"); - DatafeedConfig updatedDatafeed = new DatafeedUpdate.Builder(datafeed.getId()).build() - .apply(datafeed, Collections.emptyMap()); + DatafeedConfig updatedDatafeed = new DatafeedUpdate.Builder(datafeed.getId()).build().apply(datafeed, Collections.emptyMap()); assertThat(datafeed, equalTo(updatedDatafeed)); } @@ -278,8 +278,10 @@ public void testApply_givenFullUpdateNoAggregations() { assertThat(updatedDatafeed.getFrequency(), equalTo(TimeValue.timeValueSeconds(142))); assertThat(updatedDatafeed.getQuery(), equalTo(queryProvider.getQuery())); assertThat(updatedDatafeed.hasAggregations(), is(false)); - assertThat(updatedDatafeed.getScriptFields(), - equalTo(Collections.singletonList(new SearchSourceBuilder.ScriptField("a", mockScript("b"), false)))); + assertThat( + updatedDatafeed.getScriptFields(), + equalTo(Collections.singletonList(new SearchSourceBuilder.ScriptField("a", mockScript("b"), false))) + ); assertThat(updatedDatafeed.getScrollSize(), equalTo(8000)); assertThat(updatedDatafeed.getChunkingConfig(), equalTo(ChunkingConfig.newManual(TimeValue.timeValueHours(1)))); assertThat(updatedDatafeed.getDelayedDataCheckConfig().isEnabled(), equalTo(true)); @@ -294,11 +296,13 @@ public void testApply_givenAggregations() throws IOException { DatafeedUpdate.Builder update = new DatafeedUpdate.Builder(datafeed.getId()); MaxAggregationBuilder maxTime = AggregationBuilders.max("time").field("time"); - AggProvider aggProvider = AggProvider.fromParsedAggs(new AggregatorFactories.Builder().addAggregator( - AggregationBuilders.histogram("a").interval(300000).field("time").subAggregation(maxTime))); + AggProvider aggProvider = AggProvider.fromParsedAggs( + new AggregatorFactories.Builder().addAggregator( + AggregationBuilders.histogram("a").interval(300000).field("time").subAggregation(maxTime) + ) + ); update.setAggregations(aggProvider); - DatafeedConfig updatedDatafeed = update.build().apply(datafeed, Collections.emptyMap()); assertThat(updatedDatafeed.getIndices(), equalTo(Collections.singletonList("i_1"))); @@ -308,10 +312,9 @@ public void testApply_givenAggregations() throws IOException { public void testApply_givenIndicesOptions() { DatafeedConfig datafeed = DatafeedConfigTests.createRandomizedDatafeedConfig("foo"); - DatafeedConfig updatedDatafeed = new DatafeedUpdate.Builder(datafeed.getId()) - .setIndicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN_HIDDEN) - .build() - .apply(datafeed, Collections.emptyMap()); + DatafeedConfig updatedDatafeed = new DatafeedUpdate.Builder(datafeed.getId()).setIndicesOptions( + IndicesOptions.LENIENT_EXPAND_OPEN_HIDDEN + ).build().apply(datafeed, Collections.emptyMap()); assertThat(datafeed.getIndicesOptions(), is(not(equalTo(updatedDatafeed.getIndicesOptions())))); assertThat(updatedDatafeed.getIndicesOptions(), equalTo(IndicesOptions.LENIENT_EXPAND_OPEN_HIDDEN)); } @@ -338,33 +341,37 @@ public void testApply_GivenRandomUpdates_AssertImmutability() { public void testSerializationOfComplexAggsBetweenVersions() throws IOException { MaxAggregationBuilder maxTime = AggregationBuilders.max("timestamp").field("timestamp"); AvgAggregationBuilder avgAggregationBuilder = AggregationBuilders.avg("bytes_in_avg").field("system.network.in.bytes"); - DerivativePipelineAggregationBuilder derivativePipelineAggregationBuilder = - PipelineAggregatorBuilders.derivative("bytes_in_derivative", "bytes_in_avg"); - BucketScriptPipelineAggregationBuilder bucketScriptPipelineAggregationBuilder = - PipelineAggregatorBuilders.bucketScript("non_negative_bytes", - Collections.singletonMap("bytes", "bytes_in_derivative"), - new Script("params.bytes > 0 ? params.bytes : null")); - DateHistogramAggregationBuilder dateHistogram = - AggregationBuilders.dateHistogram("histogram_buckets") - .field("timestamp").fixedInterval(new DateHistogramInterval("300000ms")).timeZone(ZoneOffset.UTC) - .subAggregation(maxTime) - .subAggregation(avgAggregationBuilder) - .subAggregation(derivativePipelineAggregationBuilder) - .subAggregation(bucketScriptPipelineAggregationBuilder); + DerivativePipelineAggregationBuilder derivativePipelineAggregationBuilder = PipelineAggregatorBuilders.derivative( + "bytes_in_derivative", + "bytes_in_avg" + ); + BucketScriptPipelineAggregationBuilder bucketScriptPipelineAggregationBuilder = PipelineAggregatorBuilders.bucketScript( + "non_negative_bytes", + Collections.singletonMap("bytes", "bytes_in_derivative"), + new Script("params.bytes > 0 ? params.bytes : null") + ); + DateHistogramAggregationBuilder dateHistogram = AggregationBuilders.dateHistogram("histogram_buckets") + .field("timestamp") + .fixedInterval(new DateHistogramInterval("300000ms")) + .timeZone(ZoneOffset.UTC) + .subAggregation(maxTime) + .subAggregation(avgAggregationBuilder) + .subAggregation(derivativePipelineAggregationBuilder) + .subAggregation(bucketScriptPipelineAggregationBuilder); AggregatorFactories.Builder aggs = new AggregatorFactories.Builder().addAggregator(dateHistogram); DatafeedUpdate.Builder datafeedUpdateBuilder = new DatafeedUpdate.Builder("df-update-past-serialization-test"); - datafeedUpdateBuilder.setAggregations(new AggProvider( - XContentObjectTransformer.aggregatorTransformer(xContentRegistry()).toMap(aggs), - aggs, - null, - false)); + datafeedUpdateBuilder.setAggregations( + new AggProvider(XContentObjectTransformer.aggregatorTransformer(xContentRegistry()).toMap(aggs), aggs, null, false) + ); // So equality check between the streamed and current passes // Streamed DatafeedConfigs when they are before 6.6.0 require a parsed object for aggs and queries, consequently all the default // values are added between them datafeedUpdateBuilder.setQuery( - QueryProvider - .fromParsedQuery(QueryBuilders.boolQuery() - .filter(QueryBuilders.termQuery(randomAlphaOfLengthBetween(1, 10), randomAlphaOfLengthBetween(1, 10))))); + QueryProvider.fromParsedQuery( + QueryBuilders.boolQuery() + .filter(QueryBuilders.termQuery(randomAlphaOfLengthBetween(1, 10), randomAlphaOfLengthBetween(1, 10))) + ) + ); DatafeedUpdate datafeedUpdate = datafeedUpdateBuilder.build(); SearchModule searchModule = new SearchModule(Settings.EMPTY, Collections.emptyList()); @@ -389,101 +396,114 @@ public void testSerializationOfComplexAggsBetweenVersions() throws IOException { protected DatafeedUpdate mutateInstance(DatafeedUpdate instance) throws IOException { DatafeedUpdate.Builder builder = new DatafeedUpdate.Builder(instance); switch (between(1, 12)) { - case 1: - builder.setId(instance.getId() + DatafeedConfigTests.randomValidDatafeedId()); - break; - case 2: - if (instance.getQueryDelay() == null) { - builder.setQueryDelay(new TimeValue(between(100, 100000))); - } else { - builder.setQueryDelay(new TimeValue(instance.getQueryDelay().millis() + between(100, 100000))); - } - break; - case 3: - if (instance.getFrequency() == null) { - builder.setFrequency(new TimeValue(between(1, 10) * 1000)); - } else { - builder.setFrequency(new TimeValue(instance.getFrequency().millis() + between(1, 10) * 1000)); - } - break; - case 4: - List indices; - if (instance.getIndices() == null) { - indices = new ArrayList<>(); - } else { - indices = new ArrayList<>(instance.getIndices()); - } - indices.add(randomAlphaOfLengthBetween(1, 20)); - builder.setIndices(indices); - break; - case 5: - BoolQueryBuilder query = new BoolQueryBuilder(); - if (instance.getQuery() != null) { - query.must(instance.getParsedQuery(xContentRegistry())); - } - query.filter(new TermQueryBuilder(randomAlphaOfLengthBetween(1, 10), randomAlphaOfLengthBetween(1, 10))); - builder.setQuery(QueryProvider.fromParsedQuery(query)); - break; - case 6: - if (instance.hasAggregations()) { + case 1: + builder.setId(instance.getId() + DatafeedConfigTests.randomValidDatafeedId()); + break; + case 2: + if (instance.getQueryDelay() == null) { + builder.setQueryDelay(new TimeValue(between(100, 100000))); + } else { + builder.setQueryDelay(new TimeValue(instance.getQueryDelay().millis() + between(100, 100000))); + } + break; + case 3: + if (instance.getFrequency() == null) { + builder.setFrequency(new TimeValue(between(1, 10) * 1000)); + } else { + builder.setFrequency(new TimeValue(instance.getFrequency().millis() + between(1, 10) * 1000)); + } + break; + case 4: + List indices; + if (instance.getIndices() == null) { + indices = new ArrayList<>(); + } else { + indices = new ArrayList<>(instance.getIndices()); + } + indices.add(randomAlphaOfLengthBetween(1, 20)); + builder.setIndices(indices); + break; + case 5: + BoolQueryBuilder query = new BoolQueryBuilder(); + if (instance.getQuery() != null) { + query.must(instance.getParsedQuery(xContentRegistry())); + } + query.filter(new TermQueryBuilder(randomAlphaOfLengthBetween(1, 10), randomAlphaOfLengthBetween(1, 10))); + builder.setQuery(QueryProvider.fromParsedQuery(query)); + break; + case 6: + if (instance.hasAggregations()) { + builder.setAggregations(null); + } else { + AggregatorFactories.Builder aggBuilder = new AggregatorFactories.Builder(); + String timeField = randomAlphaOfLength(10); + DateHistogramInterval interval = new DateHistogramInterval(between(10000, 3600000) + "ms"); + aggBuilder.addAggregator( + new DateHistogramAggregationBuilder(timeField).field(timeField) + .fixedInterval(interval) + .subAggregation(new MaxAggregationBuilder(timeField).field(timeField)) + ); + builder.setAggregations(AggProvider.fromParsedAggs(aggBuilder)); + if (instance.getScriptFields().isEmpty() == false) { + builder.setScriptFields(Collections.emptyList()); + } + } + break; + case 7: + builder.setScriptFields( + CollectionUtils.appendToCopy( + instance.getScriptFields(), + new ScriptField(randomAlphaOfLengthBetween(1, 10), new Script("foo"), true) + ) + ); builder.setAggregations(null); - } else { - AggregatorFactories.Builder aggBuilder = new AggregatorFactories.Builder(); - String timeField = randomAlphaOfLength(10); - DateHistogramInterval interval = new DateHistogramInterval(between(10000, 3600000) + "ms"); - aggBuilder.addAggregator(new DateHistogramAggregationBuilder(timeField).field(timeField).fixedInterval(interval) - .subAggregation(new MaxAggregationBuilder(timeField).field(timeField))); - builder.setAggregations(AggProvider.fromParsedAggs(aggBuilder)); - if (instance.getScriptFields().isEmpty() == false) { - builder.setScriptFields(Collections.emptyList()); + break; + case 8: + if (instance.getScrollSize() == null) { + builder.setScrollSize(between(1, 100)); + } else { + builder.setScrollSize(instance.getScrollSize() + between(1, 100)); } - } - break; - case 7: - builder.setScriptFields(CollectionUtils.appendToCopy(instance.getScriptFields(), - new ScriptField(randomAlphaOfLengthBetween(1, 10), new Script("foo"), true))); - builder.setAggregations(null); - break; - case 8: - if (instance.getScrollSize() == null) { - builder.setScrollSize(between(1, 100)); - } else { - builder.setScrollSize(instance.getScrollSize() + between(1, 100)); - } - break; - case 9: - if (instance.getChunkingConfig() == null || instance.getChunkingConfig().getMode() == Mode.AUTO) { - ChunkingConfig newChunkingConfig = ChunkingConfig.newManual(new TimeValue(randomNonNegativeLong())); - builder.setChunkingConfig(newChunkingConfig); - } else { - builder.setChunkingConfig(null); - } - break; - case 10: - if (instance.getMaxEmptySearches() == null) { - builder.setMaxEmptySearches(randomFrom(-1, 10)); - } else { - builder.setMaxEmptySearches(instance.getMaxEmptySearches() + 100); - } - break; - case 11: - if (instance.getIndicesOptions() != null) { - builder.setIndicesOptions(IndicesOptions.fromParameters( - randomFrom(IndicesOptions.WildcardStates.values()).name().toLowerCase(Locale.ROOT), - Boolean.toString(instance.getIndicesOptions().ignoreUnavailable() == false), - Boolean.toString(instance.getIndicesOptions().allowNoIndices() == false), - Boolean.toString(instance.getIndicesOptions().ignoreThrottled() == false), - SearchRequest.DEFAULT_INDICES_OPTIONS)); - } else { - builder.setIndicesOptions(IndicesOptions.fromParameters( - randomFrom(IndicesOptions.WildcardStates.values()).name().toLowerCase(Locale.ROOT), - Boolean.toString(randomBoolean()), - Boolean.toString(randomBoolean()), - Boolean.toString(randomBoolean()), - SearchRequest.DEFAULT_INDICES_OPTIONS)); - } - break; - case 12: + break; + case 9: + if (instance.getChunkingConfig() == null || instance.getChunkingConfig().getMode() == Mode.AUTO) { + ChunkingConfig newChunkingConfig = ChunkingConfig.newManual(new TimeValue(randomNonNegativeLong())); + builder.setChunkingConfig(newChunkingConfig); + } else { + builder.setChunkingConfig(null); + } + break; + case 10: + if (instance.getMaxEmptySearches() == null) { + builder.setMaxEmptySearches(randomFrom(-1, 10)); + } else { + builder.setMaxEmptySearches(instance.getMaxEmptySearches() + 100); + } + break; + case 11: + if (instance.getIndicesOptions() != null) { + builder.setIndicesOptions( + IndicesOptions.fromParameters( + randomFrom(IndicesOptions.WildcardStates.values()).name().toLowerCase(Locale.ROOT), + Boolean.toString(instance.getIndicesOptions().ignoreUnavailable() == false), + Boolean.toString(instance.getIndicesOptions().allowNoIndices() == false), + Boolean.toString(instance.getIndicesOptions().ignoreThrottled() == false), + SearchRequest.DEFAULT_INDICES_OPTIONS + ) + ); + } else { + builder.setIndicesOptions( + IndicesOptions.fromParameters( + randomFrom(IndicesOptions.WildcardStates.values()).name().toLowerCase(Locale.ROOT), + Boolean.toString(randomBoolean()), + Boolean.toString(randomBoolean()), + Boolean.toString(randomBoolean()), + SearchRequest.DEFAULT_INDICES_OPTIONS + ) + ); + } + break; + case 12: if (instance.getRuntimeMappings() != null) { builder.setRuntimeMappings(null); } else { @@ -494,9 +514,9 @@ protected DatafeedUpdate mutateInstance(DatafeedUpdate instance) throws IOExcept field.put("runtime_field_foo", settings); builder.setRuntimeMappings(field); } - break; - default: - throw new AssertionError("Illegal randomisation branch"); + break; + default: + throw new AssertionError("Illegal randomisation branch"); } return builder.build(); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/DelayedDataCheckConfigTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/DelayedDataCheckConfigTests.java index b7eff99e5e494..0a64df1965f86 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/DelayedDataCheckConfigTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/DelayedDataCheckConfigTests.java @@ -8,8 +8,8 @@ import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -20,7 +20,7 @@ public class DelayedDataCheckConfigTests extends AbstractSerializingTestCase { @Override - protected DelayedDataCheckConfig createTestInstance(){ + protected DelayedDataCheckConfig createTestInstance() { return createRandomizedConfig(100); } @@ -62,7 +62,7 @@ public static DelayedDataCheckConfig createRandomizedConfig(long bucketSpanMilli TimeValue timeWindow = null; if (enabled || randomBoolean()) { // time span is required to be at least 1 millis, so we use a custom method to generate a time value here - timeWindow = new TimeValue(randomLongBetween(bucketSpanMillis,bucketSpanMillis*2)); + timeWindow = new TimeValue(randomLongBetween(bucketSpanMillis, bucketSpanMillis * 2)); } return new DelayedDataCheckConfig(enabled, timeWindow); } @@ -72,24 +72,24 @@ protected DelayedDataCheckConfig mutateInstance(DelayedDataCheckConfig instance) boolean enabled = instance.isEnabled(); TimeValue timeWindow = instance.getCheckWindow(); switch (between(0, 1)) { - case 0: - enabled = enabled == false; - if (randomBoolean()) { - timeWindow = TimeValue.timeValueMillis(randomLongBetween(1, 1000)); - } else { - timeWindow = null; - } - break; - case 1: - if (timeWindow == null) { - timeWindow = TimeValue.timeValueMillis(randomLongBetween(1, 1000)); - } else { - timeWindow = new TimeValue(timeWindow.getMillis() + between(10, 100)); - } - enabled = true; - break; - default: - throw new AssertionError("Illegal randomisation branch"); + case 0: + enabled = enabled == false; + if (randomBoolean()) { + timeWindow = TimeValue.timeValueMillis(randomLongBetween(1, 1000)); + } else { + timeWindow = null; + } + break; + case 1: + if (timeWindow == null) { + timeWindow = TimeValue.timeValueMillis(randomLongBetween(1, 1000)); + } else { + timeWindow = new TimeValue(timeWindow.getMillis() + between(10, 100)); + } + enabled = true; + break; + default: + throw new AssertionError("Illegal randomisation branch"); } return new DelayedDataCheckConfig(enabled, timeWindow); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/extractor/ExtractorUtilsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/extractor/ExtractorUtilsTests.java index 003c5a5b2be66..23b64aa8be431 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/extractor/ExtractorUtilsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/extractor/ExtractorUtilsTests.java @@ -29,17 +29,21 @@ public void testGetHistogramAggregation_DateHistogramHasSibling() { AvgAggregationBuilder avg = AggregationBuilders.avg("avg"); DateHistogramAggregationBuilder dateHistogram = AggregationBuilders.dateHistogram("time"); - ElasticsearchException e = expectThrows(ElasticsearchException.class, - () -> ExtractorUtils.getHistogramAggregation( - new AggregatorFactories.Builder().addAggregator(avg).addAggregator(dateHistogram).getAggregatorFactories())); + ElasticsearchException e = expectThrows( + ElasticsearchException.class, + () -> ExtractorUtils.getHistogramAggregation( + new AggregatorFactories.Builder().addAggregator(avg).addAggregator(dateHistogram).getAggregatorFactories() + ) + ); assertEquals("The date_histogram (or histogram) aggregation cannot have sibling aggregations", e.getMessage()); TermsAggregationBuilder terms = AggregationBuilders.terms("terms"); terms.subAggregation(dateHistogram); terms.subAggregation(avg); - e = expectThrows(ElasticsearchException.class, - () -> ExtractorUtils.getHistogramAggregation( - new AggregatorFactories.Builder().addAggregator(terms).getAggregatorFactories())); + e = expectThrows( + ElasticsearchException.class, + () -> ExtractorUtils.getHistogramAggregation(new AggregatorFactories.Builder().addAggregator(terms).getAggregatorFactories()) + ); assertEquals("The date_histogram (or histogram) aggregation cannot have sibling aggregations", e.getMessage()); } @@ -49,36 +53,45 @@ public void testGetHistogramAggregation() { DateHistogramAggregationBuilder dateHistogram = AggregationBuilders.dateHistogram("time"); AggregationBuilder histogramAggregationBuilder = ExtractorUtils.getHistogramAggregation( - new AggregatorFactories.Builder().addAggregator(dateHistogram).getAggregatorFactories()); + new AggregatorFactories.Builder().addAggregator(dateHistogram).getAggregatorFactories() + ); assertEquals(dateHistogram, histogramAggregationBuilder); dateHistogram.subAggregation(avg).subAggregation(nestedTerms); histogramAggregationBuilder = ExtractorUtils.getHistogramAggregation( - new AggregatorFactories.Builder().addAggregator(dateHistogram).getAggregatorFactories()); + new AggregatorFactories.Builder().addAggregator(dateHistogram).getAggregatorFactories() + ); assertEquals(dateHistogram, histogramAggregationBuilder); TermsAggregationBuilder toplevelTerms = AggregationBuilders.terms("top_level"); toplevelTerms.subAggregation(dateHistogram); histogramAggregationBuilder = ExtractorUtils.getHistogramAggregation( - new AggregatorFactories.Builder().addAggregator(toplevelTerms).getAggregatorFactories()); + new AggregatorFactories.Builder().addAggregator(toplevelTerms).getAggregatorFactories() + ); assertEquals(dateHistogram, histogramAggregationBuilder); } public void testGetHistogramAggregation_MissingHistogramAgg() { TermsAggregationBuilder terms = AggregationBuilders.terms("top_level"); - ElasticsearchException e = expectThrows(ElasticsearchException.class, - () -> ExtractorUtils.getHistogramAggregation( - new AggregatorFactories.Builder().addAggregator(terms).getAggregatorFactories())); + ElasticsearchException e = expectThrows( + ElasticsearchException.class, + () -> ExtractorUtils.getHistogramAggregation(new AggregatorFactories.Builder().addAggregator(terms).getAggregatorFactories()) + ); assertEquals("A date_histogram (or histogram) aggregation is required", e.getMessage()); } public void testGetHistogramIntervalMillis_GivenDateHistogramWithInvalidTimeZone() { MaxAggregationBuilder maxTime = AggregationBuilders.max("time").field("time"); - DateHistogramAggregationBuilder dateHistogram = AggregationBuilders.dateHistogram("bucket").field("time") - .fixedInterval(new DateHistogramInterval(300000 + "ms")).timeZone(ZoneId.of("CET")).subAggregation(maxTime); - ElasticsearchException e = expectThrows(ElasticsearchException.class, - () -> ExtractorUtils.getHistogramIntervalMillis(dateHistogram)); + DateHistogramAggregationBuilder dateHistogram = AggregationBuilders.dateHistogram("bucket") + .field("time") + .fixedInterval(new DateHistogramInterval(300000 + "ms")) + .timeZone(ZoneId.of("CET")) + .subAggregation(maxTime); + ElasticsearchException e = expectThrows( + ElasticsearchException.class, + () -> ExtractorUtils.getHistogramIntervalMillis(dateHistogram) + ); assertThat(e.getMessage(), equalTo("ML requires date_histogram.time_zone to be UTC")); } @@ -86,16 +99,22 @@ public void testGetHistogramIntervalMillis_GivenDateHistogramWithInvalidTimeZone public void testGetHistogramIntervalMillis_GivenUtcTimeZonesDeprecated() { MaxAggregationBuilder maxTime = AggregationBuilders.max("time").field("time"); ZoneId zone = randomFrom(ZoneOffset.UTC, ZoneId.of("UTC")); - DateHistogramAggregationBuilder dateHistogram = AggregationBuilders.dateHistogram("bucket").field("time") - .fixedInterval(new DateHistogramInterval(300000L + "ms")).timeZone(zone).subAggregation(maxTime); + DateHistogramAggregationBuilder dateHistogram = AggregationBuilders.dateHistogram("bucket") + .field("time") + .fixedInterval(new DateHistogramInterval(300000L + "ms")) + .timeZone(zone) + .subAggregation(maxTime); assertThat(ExtractorUtils.getHistogramIntervalMillis(dateHistogram), is(300_000L)); } public void testGetHistogramIntervalMillis_GivenUtcTimeZones() { MaxAggregationBuilder maxTime = AggregationBuilders.max("time").field("time"); ZoneId zone = randomFrom(ZoneOffset.UTC, ZoneId.of("UTC")); - DateHistogramAggregationBuilder dateHistogram = AggregationBuilders.dateHistogram("bucket").field("time") - .fixedInterval(new DateHistogramInterval("300000ms")).timeZone(zone).subAggregation(maxTime); + DateHistogramAggregationBuilder dateHistogram = AggregationBuilders.dateHistogram("bucket") + .field("time") + .fixedInterval(new DateHistogramInterval("300000ms")) + .timeZone(zone) + .subAggregation(maxTime); assertThat(ExtractorUtils.getHistogramIntervalMillis(dateHistogram), is(300_000L)); } @@ -112,7 +131,6 @@ public void testValidateAndGetCalendarInterval() { } public void testValidateAndGetCalendarInterval_intervalIsLongerThanAWeek() { - expectThrows(ElasticsearchException.class, - () -> ExtractorUtils.validateAndGetCalendarInterval("8d")); + expectThrows(ElasticsearchException.class, () -> ExtractorUtils.validateAndGetCalendarInterval("8d")); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsConfigTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsConfigTests.java index 8acabb0c0a93f..1df171e7528fe 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsConfigTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsConfigTests.java @@ -7,6 +7,7 @@ package org.elasticsearch.xpack.core.ml.dataframe; import com.carrotsearch.randomizedtesting.generators.CodepointSetGenerator; + import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.Version; @@ -17,22 +18,22 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; -import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.index.query.MatchAllQueryBuilder; +import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.search.SearchModule; +import org.elasticsearch.search.fetch.subphase.FetchSourceContext; +import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ObjectParser; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentParseException; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.index.query.MatchAllQueryBuilder; -import org.elasticsearch.rest.RestStatus; -import org.elasticsearch.search.SearchModule; -import org.elasticsearch.search.fetch.subphase.FetchSourceContext; import org.elasticsearch.xpack.core.ml.AbstractBWCSerializationTestCase; import org.elasticsearch.xpack.core.ml.AbstractBWCWireSerializationTestCase; import org.elasticsearch.xpack.core.ml.dataframe.analyses.Classification; @@ -69,10 +70,9 @@ public class DataFrameAnalyticsConfigTests extends AbstractBWCSerializationTestC @Override protected DataFrameAnalyticsConfig doParseInstance(XContentParser parser) throws IOException { - ObjectParser dataFrameAnalyticsConfigParser = - lenient - ? DataFrameAnalyticsConfig.LENIENT_PARSER - : DataFrameAnalyticsConfig.STRICT_PARSER; + ObjectParser dataFrameAnalyticsConfigParser = lenient + ? DataFrameAnalyticsConfig.LENIENT_PARSER + : DataFrameAnalyticsConfig.STRICT_PARSER; return dataFrameAnalyticsConfigParser.apply(parser, null).build(); } @@ -106,17 +106,17 @@ protected List bwcVersions() { @Override protected DataFrameAnalyticsConfig mutateInstanceForVersion(DataFrameAnalyticsConfig instance, Version version) { - DataFrameAnalyticsConfig.Builder builder = new DataFrameAnalyticsConfig.Builder(instance) - .setSource(DataFrameAnalyticsSourceTests.mutateForVersion(instance.getSource(), version)) - .setDest(DataFrameAnalyticsDestTests.mutateForVersion(instance.getDest(), version)); + DataFrameAnalyticsConfig.Builder builder = new DataFrameAnalyticsConfig.Builder(instance).setSource( + DataFrameAnalyticsSourceTests.mutateForVersion(instance.getSource(), version) + ).setDest(DataFrameAnalyticsDestTests.mutateForVersion(instance.getDest(), version)); if (instance.getAnalysis() instanceof OutlierDetection) { - builder.setAnalysis(OutlierDetectionTests.mutateForVersion((OutlierDetection)instance.getAnalysis(), version)); + builder.setAnalysis(OutlierDetectionTests.mutateForVersion((OutlierDetection) instance.getAnalysis(), version)); } if (instance.getAnalysis() instanceof Regression) { - builder.setAnalysis(RegressionTests.mutateForVersion((Regression)instance.getAnalysis(), version)); + builder.setAnalysis(RegressionTests.mutateForVersion((Regression) instance.getAnalysis(), version)); } if (instance.getAnalysis() instanceof Classification) { - builder.setAnalysis(ClassificationTests.mutateForVersion((Classification)instance.getAnalysis(), version)); + builder.setAnalysis(ClassificationTests.mutateForVersion((Classification) instance.getAnalysis(), version)); } if (version.before(Version.V_7_5_0)) { builder.setAllowLazyStart(false); @@ -142,10 +142,11 @@ protected void assertOnBWCObject(DataFrameAnalyticsConfig bwcSerializedObject, D DataFrameAnalysis bwcAnalysis; DataFrameAnalysis testAnalysis; if (testInstance.getAnalysis() instanceof Regression) { - Regression testRegression = (Regression)testInstance.getAnalysis(); - Regression bwcRegression = (Regression)bwcSerializedObject.getAnalysis(); + Regression testRegression = (Regression) testInstance.getAnalysis(); + Regression bwcRegression = (Regression) bwcSerializedObject.getAnalysis(); - bwcAnalysis = new Regression(bwcRegression.getDependentVariable(), + bwcAnalysis = new Regression( + bwcRegression.getDependentVariable(), bwcRegression.getBoostedTreeParams(), bwcRegression.getPredictionFieldName(), bwcRegression.getTrainingPercent(), @@ -153,8 +154,10 @@ protected void assertOnBWCObject(DataFrameAnalyticsConfig bwcSerializedObject, D bwcRegression.getLossFunction(), bwcRegression.getLossFunctionParameter(), bwcRegression.getFeatureProcessors(), - bwcRegression.getEarlyStoppingEnabled()); - testAnalysis = new Regression(testRegression.getDependentVariable(), + bwcRegression.getEarlyStoppingEnabled() + ); + testAnalysis = new Regression( + testRegression.getDependentVariable(), testRegression.getBoostedTreeParams(), testRegression.getPredictionFieldName(), testRegression.getTrainingPercent(), @@ -162,11 +165,13 @@ protected void assertOnBWCObject(DataFrameAnalyticsConfig bwcSerializedObject, D testRegression.getLossFunction(), testRegression.getLossFunctionParameter(), testRegression.getFeatureProcessors(), - testRegression.getEarlyStoppingEnabled()); + testRegression.getEarlyStoppingEnabled() + ); } else { - Classification testClassification = (Classification)testInstance.getAnalysis(); - Classification bwcClassification = (Classification)bwcSerializedObject.getAnalysis(); - bwcAnalysis = new Classification(bwcClassification.getDependentVariable(), + Classification testClassification = (Classification) testInstance.getAnalysis(); + Classification bwcClassification = (Classification) bwcSerializedObject.getAnalysis(); + bwcAnalysis = new Classification( + bwcClassification.getDependentVariable(), bwcClassification.getBoostedTreeParams(), bwcClassification.getPredictionFieldName(), bwcClassification.getClassAssignmentObjective(), @@ -174,8 +179,10 @@ protected void assertOnBWCObject(DataFrameAnalyticsConfig bwcSerializedObject, D bwcClassification.getTrainingPercent(), 42L, bwcClassification.getFeatureProcessors(), - bwcClassification.getEarlyStoppingEnabled()); - testAnalysis = new Classification(testClassification.getDependentVariable(), + bwcClassification.getEarlyStoppingEnabled() + ); + testAnalysis = new Classification( + testClassification.getDependentVariable(), testClassification.getBoostedTreeParams(), testClassification.getPredictionFieldName(), testClassification.getClassAssignmentObjective(), @@ -183,18 +190,16 @@ protected void assertOnBWCObject(DataFrameAnalyticsConfig bwcSerializedObject, D testClassification.getTrainingPercent(), 42L, testClassification.getFeatureProcessors(), - testClassification.getEarlyStoppingEnabled()); + testClassification.getEarlyStoppingEnabled() + ); } - super.assertOnBWCObject(new DataFrameAnalyticsConfig.Builder(bwcSerializedObject) - .setAnalysis(bwcAnalysis) - .build(), - new DataFrameAnalyticsConfig.Builder(testInstance) - .setAnalysis(testAnalysis) - .build(), - version); + super.assertOnBWCObject( + new DataFrameAnalyticsConfig.Builder(bwcSerializedObject).setAnalysis(bwcAnalysis).build(), + new DataFrameAnalyticsConfig.Builder(testInstance).setAnalysis(testAnalysis).build(), + version + ); } - @Override protected Writeable.Reader instanceReader() { return DataFrameAnalyticsConfig::new; @@ -205,29 +210,36 @@ public static DataFrameAnalyticsConfig createRandom(String id) { } public static DataFrameAnalyticsConfig createRandom(String id, boolean withGeneratedFields) { - return createRandomBuilder(id, withGeneratedFields, randomFrom(OutlierDetectionTests.createRandom(), - RegressionTests.createRandom(), - ClassificationTests.createRandom())).build(); + return createRandomBuilder( + id, + withGeneratedFields, + randomFrom(OutlierDetectionTests.createRandom(), RegressionTests.createRandom(), ClassificationTests.createRandom()) + ).build(); } public static DataFrameAnalyticsConfig.Builder createRandomBuilder(String id) { - return createRandomBuilder(id, false, randomFrom(OutlierDetectionTests.createRandom(), - RegressionTests.createRandom(), - ClassificationTests.createRandom())); + return createRandomBuilder( + id, + false, + randomFrom(OutlierDetectionTests.createRandom(), RegressionTests.createRandom(), ClassificationTests.createRandom()) + ); } public static DataFrameAnalyticsConfig.Builder createRandomBuilder(String id, boolean withGeneratedFields, DataFrameAnalysis analysis) { DataFrameAnalyticsSource source = DataFrameAnalyticsSourceTests.createRandom(); DataFrameAnalyticsDest dest = DataFrameAnalyticsDestTests.createRandom(); - DataFrameAnalyticsConfig.Builder builder = new DataFrameAnalyticsConfig.Builder() - .setId(id) + DataFrameAnalyticsConfig.Builder builder = new DataFrameAnalyticsConfig.Builder().setId(id) .setAnalysis(analysis) .setSource(source) .setDest(dest); if (randomBoolean()) { - builder.setAnalyzedFields(new FetchSourceContext(true, - generateRandomStringArray(10, 10, false, false), - generateRandomStringArray(10, 10, false, false))); + builder.setAnalyzedFields( + new FetchSourceContext( + true, + generateRandomStringArray(10, 10, false, false), + generateRandomStringArray(10, 10, false, false) + ) + ); } if (randomBoolean()) { builder.setModelMemoryLimit(new ByteSizeValue(randomIntBetween(1, 16), randomFrom(ByteSizeUnit.MB, ByteSizeUnit.GB))); @@ -257,21 +269,19 @@ public static String randomValidId() { return generator.ofCodePointsLength(random(), 10, 10); } - private static final String ANACHRONISTIC_QUERY_DATA_FRAME_ANALYTICS = "{\n" + - " \"id\": \"old-data-frame\",\n" + - //query:match:type stopped being supported in 6.x - " \"source\": {\"index\":\"my-index\", \"query\": {\"match\" : {\"query\":\"fieldName\", \"type\": \"phrase\"}}},\n" + - " \"dest\": {\"index\":\"dest-index\"},\n" + - " \"analysis\": {\"outlier_detection\": {\"n_neighbors\": 10}}\n" + - "}"; + private static final String ANACHRONISTIC_QUERY_DATA_FRAME_ANALYTICS = "{\n" + " \"id\": \"old-data-frame\",\n" + + // query:match:type stopped being supported in 6.x + " \"source\": {\"index\":\"my-index\", \"query\": {\"match\" : {\"query\":\"fieldName\", \"type\": \"phrase\"}}},\n" + + " \"dest\": {\"index\":\"dest-index\"},\n" + + " \"analysis\": {\"outlier_detection\": {\"n_neighbors\": 10}}\n" + + "}"; - private static final String MODERN_QUERY_DATA_FRAME_ANALYTICS = "{\n" + - " \"id\": \"data-frame\",\n" + - // match_all if parsed, adds default values in the options - " \"source\": {\"index\":\"my-index\", \"query\": {\"match_all\" : {}}},\n" + - " \"dest\": {\"index\":\"dest-index\"},\n" + - " \"analysis\": {\"outlier_detection\": {\"n_neighbors\": 10}}\n" + - "}"; + private static final String MODERN_QUERY_DATA_FRAME_ANALYTICS = "{\n" + " \"id\": \"data-frame\",\n" + + // match_all if parsed, adds default values in the options + " \"source\": {\"index\":\"my-index\", \"query\": {\"match_all\" : {}}},\n" + + " \"dest\": {\"index\":\"dest-index\"},\n" + + " \"analysis\": {\"outlier_detection\": {\"n_neighbors\": 10}}\n" + + "}"; private boolean lenient; @@ -281,19 +291,19 @@ public void chooseStrictOrLenient() { } public void testQueryConfigStoresUserInputOnly() throws IOException { - try (XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(xContentRegistry(), - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, - MODERN_QUERY_DATA_FRAME_ANALYTICS)) { + try ( + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, MODERN_QUERY_DATA_FRAME_ANALYTICS) + ) { DataFrameAnalyticsConfig config = DataFrameAnalyticsConfig.LENIENT_PARSER.apply(parser, null).build(); assertThat(config.getSource().getQuery(), equalTo(Collections.singletonMap(MatchAllQueryBuilder.NAME, Collections.emptyMap()))); } - try (XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(xContentRegistry(), - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, - MODERN_QUERY_DATA_FRAME_ANALYTICS)) { + try ( + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, MODERN_QUERY_DATA_FRAME_ANALYTICS) + ) { DataFrameAnalyticsConfig config = DataFrameAnalyticsConfig.STRICT_PARSER.apply(parser, null).build(); assertThat(config.getSource().getQuery(), equalTo(Collections.singletonMap(MatchAllQueryBuilder.NAME, Collections.emptyMap()))); @@ -301,23 +311,25 @@ public void testQueryConfigStoresUserInputOnly() throws IOException { } public void testPastQueryConfigParse() throws IOException { - try (XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(xContentRegistry(), - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, - ANACHRONISTIC_QUERY_DATA_FRAME_ANALYTICS)) { + try ( + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, ANACHRONISTIC_QUERY_DATA_FRAME_ANALYTICS) + ) { DataFrameAnalyticsConfig config = DataFrameAnalyticsConfig.LENIENT_PARSER.apply(parser, null).build(); ElasticsearchException e = expectThrows(ElasticsearchException.class, () -> config.getSource().getParsedQuery()); assertEquals("[match] query doesn't support multiple fields, found [query] and [type]", e.getMessage()); } - try (XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(xContentRegistry(), - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, - ANACHRONISTIC_QUERY_DATA_FRAME_ANALYTICS)) { + try ( + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, ANACHRONISTIC_QUERY_DATA_FRAME_ANALYTICS) + ) { - XContentParseException e = expectThrows(XContentParseException.class, - () -> DataFrameAnalyticsConfig.STRICT_PARSER.apply(parser, null).build()); + XContentParseException e = expectThrows( + XContentParseException.class, + () -> DataFrameAnalyticsConfig.STRICT_PARSER.apply(parser, null).build() + ); assertThat(e.getMessage(), containsString("[data_frame_analytics_config] failed to parse field [source]")); } } @@ -354,16 +366,36 @@ public void testInvalidModelMemoryLimits() { DataFrameAnalyticsConfig.Builder builder = new DataFrameAnalyticsConfig.Builder(); // All these are different ways of specifying a limit that is lower than the minimum - assertTooSmall(expectThrows(ElasticsearchStatusException.class, - () -> builder.setModelMemoryLimit(new ByteSizeValue(-1, ByteSizeUnit.BYTES)).build())); - assertTooSmall(expectThrows(ElasticsearchStatusException.class, - () -> builder.setModelMemoryLimit(new ByteSizeValue(0, ByteSizeUnit.BYTES)).build())); - assertTooSmall(expectThrows(ElasticsearchStatusException.class, - () -> builder.setModelMemoryLimit(new ByteSizeValue(0, ByteSizeUnit.KB)).build())); - assertTooSmall(expectThrows(ElasticsearchStatusException.class, - () -> builder.setModelMemoryLimit(new ByteSizeValue(0, ByteSizeUnit.MB)).build())); - assertTooSmall(expectThrows(ElasticsearchStatusException.class, - () -> builder.setModelMemoryLimit(new ByteSizeValue(1023, ByteSizeUnit.BYTES)).build())); + assertTooSmall( + expectThrows( + ElasticsearchStatusException.class, + () -> builder.setModelMemoryLimit(new ByteSizeValue(-1, ByteSizeUnit.BYTES)).build() + ) + ); + assertTooSmall( + expectThrows( + ElasticsearchStatusException.class, + () -> builder.setModelMemoryLimit(new ByteSizeValue(0, ByteSizeUnit.BYTES)).build() + ) + ); + assertTooSmall( + expectThrows( + ElasticsearchStatusException.class, + () -> builder.setModelMemoryLimit(new ByteSizeValue(0, ByteSizeUnit.KB)).build() + ) + ); + assertTooSmall( + expectThrows( + ElasticsearchStatusException.class, + () -> builder.setModelMemoryLimit(new ByteSizeValue(0, ByteSizeUnit.MB)).build() + ) + ); + assertTooSmall( + expectThrows( + ElasticsearchStatusException.class, + () -> builder.setModelMemoryLimit(new ByteSizeValue(1023, ByteSizeUnit.BYTES)).build() + ) + ); } public void testNoMemoryCapping() { @@ -371,8 +403,10 @@ public void testNoMemoryCapping() { DataFrameAnalyticsConfig uncapped = createRandom("foo"); ByteSizeValue unlimited = randomBoolean() ? null : ByteSizeValue.ZERO; - assertThat(uncapped.getModelMemoryLimit(), - equalTo(new DataFrameAnalyticsConfig.Builder(uncapped, unlimited).build().getModelMemoryLimit())); + assertThat( + uncapped.getModelMemoryLimit(), + equalTo(new DataFrameAnalyticsConfig.Builder(uncapped, unlimited).build().getModelMemoryLimit()) + ); } public void testMemoryCapping() { @@ -381,11 +415,12 @@ public void testMemoryCapping() { ByteSizeValue maxLimit = new ByteSizeValue(randomIntBetween(500, 1000), ByteSizeUnit.MB); if (maxLimit.compareTo(defaultLimitConfig.getModelMemoryLimit()) < 0) { - assertThat(maxLimit, - equalTo(new DataFrameAnalyticsConfig.Builder(defaultLimitConfig, maxLimit).build().getModelMemoryLimit())); + assertThat(maxLimit, equalTo(new DataFrameAnalyticsConfig.Builder(defaultLimitConfig, maxLimit).build().getModelMemoryLimit())); } else { - assertThat(defaultLimitConfig.getModelMemoryLimit(), - equalTo(new DataFrameAnalyticsConfig.Builder(defaultLimitConfig, maxLimit).build().getModelMemoryLimit())); + assertThat( + defaultLimitConfig.getModelMemoryLimit(), + equalTo(new DataFrameAnalyticsConfig.Builder(defaultLimitConfig, maxLimit).build().getModelMemoryLimit()) + ); } } @@ -395,8 +430,10 @@ public void testExplicitModelMemoryLimitTooHigh() { DataFrameAnalyticsConfig explicitLimitConfig = createRandomBuilder("foo").setModelMemoryLimit(configuredLimit).build(); ByteSizeValue maxLimit = new ByteSizeValue(randomIntBetween(500, 1000), ByteSizeUnit.MB); - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> new DataFrameAnalyticsConfig.Builder(explicitLimitConfig, maxLimit).build()); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> new DataFrameAnalyticsConfig.Builder(explicitLimitConfig, maxLimit).build() + ); assertThat(e.getMessage(), startsWith("model_memory_limit")); assertThat(e.getMessage(), containsString("must be less than the value of the xpack.ml.max_model_memory_limit setting")); } @@ -410,8 +447,7 @@ public void testBuildForExplain() { } public void testBuildForExplain_MissingId() { - DataFrameAnalyticsConfig.Builder builder = new DataFrameAnalyticsConfig.Builder() - .setAnalysis(OutlierDetectionTests.createRandom()) + DataFrameAnalyticsConfig.Builder builder = new DataFrameAnalyticsConfig.Builder().setAnalysis(OutlierDetectionTests.createRandom()) .setSource(DataFrameAnalyticsSourceTests.createRandom()) .setDest(DataFrameAnalyticsDestTests.createRandom()); @@ -421,8 +457,7 @@ public void testBuildForExplain_MissingId() { } public void testBuildForExplain_MissingDest() { - DataFrameAnalyticsConfig.Builder builder = new DataFrameAnalyticsConfig.Builder() - .setId("foo") + DataFrameAnalyticsConfig.Builder builder = new DataFrameAnalyticsConfig.Builder().setId("foo") .setAnalysis(OutlierDetectionTests.createRandom()) .setSource(DataFrameAnalyticsSourceTests.createRandom()); @@ -438,24 +473,22 @@ public void testPreventCreateTimeInjection() throws IOException { + " \"dest\" : {\"index\": \"dest\"}," + "}"; - try (XContentParser parser = - XContentFactory.xContent(XContentType.JSON).createParser( - xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json)) { + try ( + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json) + ) { Exception e = expectThrows(IllegalArgumentException.class, () -> DataFrameAnalyticsConfig.STRICT_PARSER.apply(parser, null)); assertThat(e.getMessage(), containsString("unknown field [create_time]")); } } public void testPreventVersionInjection() throws IOException { - String json = "{" - + " \"version\" : \"7.3.0\"," - + " \"source\" : {\"index\":\"src\"}," - + " \"dest\" : {\"index\": \"dest\"}," - + "}"; + String json = "{" + " \"version\" : \"7.3.0\"," + " \"source\" : {\"index\":\"src\"}," + " \"dest\" : {\"index\": \"dest\"}," + "}"; - try (XContentParser parser = - XContentFactory.xContent(XContentType.JSON).createParser( - xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json)) { + try ( + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json) + ) { Exception e = expectThrows(IllegalArgumentException.class, () -> DataFrameAnalyticsConfig.STRICT_PARSER.apply(parser, null)); assertThat(e.getMessage(), containsString("unknown field [version]")); } @@ -465,10 +498,9 @@ public void testToXContent_GivenAnalysisWithRandomizeSeedAndVersionIsCurrent() t Regression regression = new Regression("foo"); assertThat(regression.getRandomizeSeed(), is(notNullValue())); - DataFrameAnalyticsConfig config = new DataFrameAnalyticsConfig.Builder() - .setVersion(Version.CURRENT) + DataFrameAnalyticsConfig config = new DataFrameAnalyticsConfig.Builder().setVersion(Version.CURRENT) .setId("test_config") - .setSource(new DataFrameAnalyticsSource(new String[] {"source_index"}, null, null, null)) + .setSource(new DataFrameAnalyticsSource(new String[] { "source_index" }, null, null, null)) .setDest(new DataFrameAnalyticsDest("dest_index", null)) .setAnalysis(regression) .build(); @@ -484,10 +516,9 @@ public void testToXContent_GivenAnalysisWithRandomizeSeedAndVersionIsBeforeItWas Regression regression = new Regression("foo"); assertThat(regression.getRandomizeSeed(), is(notNullValue())); - DataFrameAnalyticsConfig config = new DataFrameAnalyticsConfig.Builder() - .setVersion(Version.V_7_5_0) + DataFrameAnalyticsConfig config = new DataFrameAnalyticsConfig.Builder().setVersion(Version.V_7_5_0) .setId("test_config") - .setSource(new DataFrameAnalyticsSource(new String[] {"source_index"}, null, null, null)) + .setSource(new DataFrameAnalyticsSource(new String[] { "source_index" }, null, null, null)) .setDest(new DataFrameAnalyticsDest("dest_index", null)) .setAnalysis(regression) .build(); @@ -501,32 +532,38 @@ public void testToXContent_GivenAnalysisWithRandomizeSeedAndVersionIsBeforeItWas public void testExtractJobIdFromDocId() { assertThat(DataFrameAnalyticsConfig.extractJobIdFromDocId("data_frame_analytics_config-foo"), equalTo("foo")); - assertThat(DataFrameAnalyticsConfig.extractJobIdFromDocId("data_frame_analytics_config-data_frame_analytics_config-foo"), - equalTo("data_frame_analytics_config-foo")); + assertThat( + DataFrameAnalyticsConfig.extractJobIdFromDocId("data_frame_analytics_config-data_frame_analytics_config-foo"), + equalTo("data_frame_analytics_config-foo") + ); assertThat(DataFrameAnalyticsConfig.extractJobIdFromDocId("foo"), is(nullValue())); } public void testCtor_GivenMaxNumThreadsIsZero() { - ElasticsearchException e = expectThrows(ElasticsearchException.class, () -> new DataFrameAnalyticsConfig.Builder() - .setId("test_config") - .setSource(new DataFrameAnalyticsSource(new String[] {"source_index"}, null, null, null)) - .setDest(new DataFrameAnalyticsDest("dest_index", null)) - .setAnalysis(new Regression("foo")) - .setMaxNumThreads(0) - .build()); + ElasticsearchException e = expectThrows( + ElasticsearchException.class, + () -> new DataFrameAnalyticsConfig.Builder().setId("test_config") + .setSource(new DataFrameAnalyticsSource(new String[] { "source_index" }, null, null, null)) + .setDest(new DataFrameAnalyticsDest("dest_index", null)) + .setAnalysis(new Regression("foo")) + .setMaxNumThreads(0) + .build() + ); assertThat(e.status(), equalTo(RestStatus.BAD_REQUEST)); assertThat(e.getMessage(), equalTo("[max_num_threads] must be a positive integer")); } public void testCtor_GivenMaxNumThreadsIsNegative() { - ElasticsearchException e = expectThrows(ElasticsearchException.class, () -> new DataFrameAnalyticsConfig.Builder() - .setId("test_config") - .setSource(new DataFrameAnalyticsSource(new String[] {"source_index"}, null, null, null)) - .setDest(new DataFrameAnalyticsDest("dest_index", null)) - .setAnalysis(new Regression("foo")) - .setMaxNumThreads(randomIntBetween(Integer.MIN_VALUE, 0)) - .build()); + ElasticsearchException e = expectThrows( + ElasticsearchException.class, + () -> new DataFrameAnalyticsConfig.Builder().setId("test_config") + .setSource(new DataFrameAnalyticsSource(new String[] { "source_index" }, null, null, null)) + .setDest(new DataFrameAnalyticsDest("dest_index", null)) + .setAnalysis(new Regression("foo")) + .setMaxNumThreads(randomIntBetween(Integer.MIN_VALUE, 0)) + .build() + ); assertThat(e.status(), equalTo(RestStatus.BAD_REQUEST)); assertThat(e.getMessage(), equalTo("[max_num_threads] must be a positive integer")); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsConfigUpdateTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsConfigUpdateTests.java index 1b295070003b6..2be92ff5d5f9f 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsConfigUpdateTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsConfigUpdateTests.java @@ -9,9 +9,9 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.unit.ByteSizeValue; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.Objects; @@ -59,24 +59,25 @@ public static DataFrameAnalyticsConfigUpdate randomUpdate(String id) { public void testMergeWithConfig_UpdatedDescription() { String id = randomValidId(); - DataFrameAnalyticsConfig config = - DataFrameAnalyticsConfigTests.createRandomBuilder(id).setDescription("old description").build(); - DataFrameAnalyticsConfigUpdate update = - new DataFrameAnalyticsConfigUpdate.Builder(id).setDescription("new description").build(); + DataFrameAnalyticsConfig config = DataFrameAnalyticsConfigTests.createRandomBuilder(id).setDescription("old description").build(); + DataFrameAnalyticsConfigUpdate update = new DataFrameAnalyticsConfigUpdate.Builder(id).setDescription("new description").build(); assertThat( update.mergeWithConfig(config).build(), - is(equalTo(new DataFrameAnalyticsConfig.Builder(config).setDescription("new description").build()))); + is(equalTo(new DataFrameAnalyticsConfig.Builder(config).setDescription("new description").build())) + ); } public void testMergeWithConfig_UpdatedModelMemoryLimit() { String id = randomValidId(); - DataFrameAnalyticsConfig config = - DataFrameAnalyticsConfigTests.createRandomBuilder(id).setModelMemoryLimit(new ByteSizeValue(1024)).build(); - DataFrameAnalyticsConfigUpdate update = - new DataFrameAnalyticsConfigUpdate.Builder(id).setModelMemoryLimit(new ByteSizeValue(2048)).build(); + DataFrameAnalyticsConfig config = DataFrameAnalyticsConfigTests.createRandomBuilder(id) + .setModelMemoryLimit(new ByteSizeValue(1024)) + .build(); + DataFrameAnalyticsConfigUpdate update = new DataFrameAnalyticsConfigUpdate.Builder(id).setModelMemoryLimit(new ByteSizeValue(2048)) + .build(); assertThat( update.mergeWithConfig(config).build(), - is(equalTo(new DataFrameAnalyticsConfig.Builder(config).setModelMemoryLimit(new ByteSizeValue(2048)).build()))); + is(equalTo(new DataFrameAnalyticsConfig.Builder(config).setModelMemoryLimit(new ByteSizeValue(2048)).build())) + ); } public void testMergeWithConfig_UpdatedAllowLazyStart() { @@ -85,7 +86,8 @@ public void testMergeWithConfig_UpdatedAllowLazyStart() { DataFrameAnalyticsConfigUpdate update = new DataFrameAnalyticsConfigUpdate.Builder(id).setAllowLazyStart(true).build(); assertThat( update.mergeWithConfig(config).build(), - is(equalTo(new DataFrameAnalyticsConfig.Builder(config).setAllowLazyStart(true).build()))); + is(equalTo(new DataFrameAnalyticsConfig.Builder(config).setAllowLazyStart(true).build())) + ); } public void testMergeWithConfig_UpdatedMaxNumThreads() { @@ -94,34 +96,35 @@ public void testMergeWithConfig_UpdatedMaxNumThreads() { DataFrameAnalyticsConfigUpdate update = new DataFrameAnalyticsConfigUpdate.Builder(id).setMaxNumThreads(5).build(); assertThat( update.mergeWithConfig(config).build(), - is(equalTo(new DataFrameAnalyticsConfig.Builder(config).setMaxNumThreads(5).build()))); + is(equalTo(new DataFrameAnalyticsConfig.Builder(config).setMaxNumThreads(5).build())) + ); } public void testMergeWithConfig_UpdatedAllUpdatableProperties() { String id = randomValidId(); - DataFrameAnalyticsConfig config = - DataFrameAnalyticsConfigTests.createRandomBuilder(id) - .setDescription("old description") - .setModelMemoryLimit(new ByteSizeValue(1024)) - .setAllowLazyStart(false) - .setMaxNumThreads(1) - .build(); - DataFrameAnalyticsConfigUpdate update = - new DataFrameAnalyticsConfigUpdate.Builder(id) - .setDescription("new description") - .setModelMemoryLimit(new ByteSizeValue(2048)) - .setAllowLazyStart(true) - .setMaxNumThreads(4) - .build(); + DataFrameAnalyticsConfig config = DataFrameAnalyticsConfigTests.createRandomBuilder(id) + .setDescription("old description") + .setModelMemoryLimit(new ByteSizeValue(1024)) + .setAllowLazyStart(false) + .setMaxNumThreads(1) + .build(); + DataFrameAnalyticsConfigUpdate update = new DataFrameAnalyticsConfigUpdate.Builder(id).setDescription("new description") + .setModelMemoryLimit(new ByteSizeValue(2048)) + .setAllowLazyStart(true) + .setMaxNumThreads(4) + .build(); assertThat( update.mergeWithConfig(config).build(), - is(equalTo( - new DataFrameAnalyticsConfig.Builder(config) - .setDescription("new description") - .setModelMemoryLimit(new ByteSizeValue(2048)) - .setAllowLazyStart(true) - .setMaxNumThreads(4) - .build()))); + is( + equalTo( + new DataFrameAnalyticsConfig.Builder(config).setDescription("new description") + .setModelMemoryLimit(new ByteSizeValue(2048)) + .setAllowLazyStart(true) + .setMaxNumThreads(4) + .build() + ) + ) + ); } public void testMergeWithConfig_NoopUpdate() { @@ -156,52 +159,53 @@ public void testMergeWithConfig_failBecauseTargetConfigHasDifferentId() { public void testRequiresRestart_DescriptionUpdateDoesNotRequireRestart() { String id = randomValidId(); - DataFrameAnalyticsConfig config = - DataFrameAnalyticsConfigTests.createRandomBuilder(id).setDescription("old description").build(); - DataFrameAnalyticsConfigUpdate update = - new DataFrameAnalyticsConfigUpdate.Builder(id).setDescription("new description").build(); + DataFrameAnalyticsConfig config = DataFrameAnalyticsConfigTests.createRandomBuilder(id).setDescription("old description").build(); + DataFrameAnalyticsConfigUpdate update = new DataFrameAnalyticsConfigUpdate.Builder(id).setDescription("new description").build(); assertThat(update.requiresRestart(config), is(false)); } public void testRequiresRestart_ModelMemoryLimitUpdateRequiresRestart() { String id = randomValidId(); - DataFrameAnalyticsConfig config = - DataFrameAnalyticsConfigTests.createRandomBuilder(id).setModelMemoryLimit(new ByteSizeValue(1024)).build(); - DataFrameAnalyticsConfigUpdate update = - new DataFrameAnalyticsConfigUpdate.Builder(id).setModelMemoryLimit(new ByteSizeValue(2048)).build(); + DataFrameAnalyticsConfig config = DataFrameAnalyticsConfigTests.createRandomBuilder(id) + .setModelMemoryLimit(new ByteSizeValue(1024)) + .build(); + DataFrameAnalyticsConfigUpdate update = new DataFrameAnalyticsConfigUpdate.Builder(id).setModelMemoryLimit(new ByteSizeValue(2048)) + .build(); assertThat(update.requiresRestart(config), is(true)); } public void testRequiresRestart_MaxNumThreadsUpdateRequiresRestart() { String id = randomValidId(); - DataFrameAnalyticsConfig config = - DataFrameAnalyticsConfigTests.createRandomBuilder(id).setMaxNumThreads(1).build(); + DataFrameAnalyticsConfig config = DataFrameAnalyticsConfigTests.createRandomBuilder(id).setMaxNumThreads(1).build(); DataFrameAnalyticsConfigUpdate update = new DataFrameAnalyticsConfigUpdate.Builder(id).setMaxNumThreads(8).build(); assertThat(update.requiresRestart(config), is(true)); } public void testCtor_GivenMaxNumberThreadsIsZero() { - ElasticsearchException e = expectThrows(ElasticsearchException.class, - () -> new DataFrameAnalyticsConfigUpdate.Builder("test").setMaxNumThreads(0).build()); + ElasticsearchException e = expectThrows( + ElasticsearchException.class, + () -> new DataFrameAnalyticsConfigUpdate.Builder("test").setMaxNumThreads(0).build() + ); assertThat(e.status(), equalTo(RestStatus.BAD_REQUEST)); assertThat(e.getMessage(), equalTo("[max_num_threads] must be a positive integer")); } public void testCtor_GivenMaxNumberThreadsIsNegative() { - ElasticsearchException e = expectThrows(ElasticsearchException.class, - () -> new DataFrameAnalyticsConfigUpdate.Builder("test").setMaxNumThreads(randomIntBetween(Integer.MIN_VALUE, 0)).build()); + ElasticsearchException e = expectThrows( + ElasticsearchException.class, + () -> new DataFrameAnalyticsConfigUpdate.Builder("test").setMaxNumThreads(randomIntBetween(Integer.MIN_VALUE, 0)).build() + ); assertThat(e.status(), equalTo(RestStatus.BAD_REQUEST)); assertThat(e.getMessage(), equalTo("[max_num_threads] must be a positive integer")); } public void testGetUpdatedFields_GivenAll() { - DataFrameAnalyticsConfigUpdate update = new DataFrameAnalyticsConfigUpdate.Builder("test_job") - .setDescription("new description") + DataFrameAnalyticsConfigUpdate update = new DataFrameAnalyticsConfigUpdate.Builder("test_job").setDescription("new description") .setModelMemoryLimit(new ByteSizeValue(1024)) .setAllowLazyStart(true) .setMaxNumThreads(8) @@ -211,33 +215,28 @@ public void testGetUpdatedFields_GivenAll() { } public void testGetUpdatedFields_GivenAllowLazyStart() { - DataFrameAnalyticsConfigUpdate update = new DataFrameAnalyticsConfigUpdate.Builder("test_job") - .setAllowLazyStart(false) - .build(); + DataFrameAnalyticsConfigUpdate update = new DataFrameAnalyticsConfigUpdate.Builder("test_job").setAllowLazyStart(false).build(); assertThat(update.getUpdatedFields(), contains("allow_lazy_start")); } public void testGetUpdatedFields_GivenDescription() { - DataFrameAnalyticsConfigUpdate update = new DataFrameAnalyticsConfigUpdate.Builder("test_job") - .setDescription("new description") + DataFrameAnalyticsConfigUpdate update = new DataFrameAnalyticsConfigUpdate.Builder("test_job").setDescription("new description") .build(); assertThat(update.getUpdatedFields(), contains("description")); } public void testGetUpdatedFields_GivenMaxNumThreads() { - DataFrameAnalyticsConfigUpdate update = new DataFrameAnalyticsConfigUpdate.Builder("test_job") - .setMaxNumThreads(3) - .build(); + DataFrameAnalyticsConfigUpdate update = new DataFrameAnalyticsConfigUpdate.Builder("test_job").setMaxNumThreads(3).build(); assertThat(update.getUpdatedFields(), contains("max_num_threads")); } public void testGetUpdatedFields_GivenModelMemoryLimit() { - DataFrameAnalyticsConfigUpdate update = new DataFrameAnalyticsConfigUpdate.Builder("test_job") - .setModelMemoryLimit(new ByteSizeValue(1024)) - .build(); + DataFrameAnalyticsConfigUpdate update = new DataFrameAnalyticsConfigUpdate.Builder("test_job").setModelMemoryLimit( + new ByteSizeValue(1024) + ).build(); assertThat(update.getUpdatedFields(), contains("model_memory_limit")); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsSourceTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsSourceTests.java index 88d671d669d95..fa3c8a204e31e 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsSourceTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsSourceTests.java @@ -11,11 +11,11 @@ import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchModule; import org.elasticsearch.search.fetch.subphase.FetchSourceContext; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.AbstractBWCSerializationTestCase; import org.elasticsearch.xpack.core.ml.utils.QueryProvider; @@ -67,9 +67,11 @@ public static DataFrameAnalyticsSource createRandom() { } } if (randomBoolean()) { - sourceFiltering = new FetchSourceContext(true, + sourceFiltering = new FetchSourceContext( + true, generateRandomStringArray(10, 10, false, false), - generateRandomStringArray(10, 10, false, false)); + generateRandomStringArray(10, 10, false, false) + ); } Map runtimeMappings = null; if (randomBoolean()) { @@ -93,14 +95,23 @@ protected Writeable.Reader instanceReader() { } public void testConstructor_GivenDisabledSource() { - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> new DataFrameAnalyticsSource( - new String[] {"index"}, null, new FetchSourceContext(false, null, null), null)); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> new DataFrameAnalyticsSource(new String[] { "index" }, null, new FetchSourceContext(false, null, null), null) + ); assertThat(e.getMessage(), equalTo("source._source cannot be disabled")); } public void testConstructor_GivenInvalidRuntimeMappings() { - ElasticsearchException e = expectThrows(ElasticsearchException.class, () -> new DataFrameAnalyticsSource( - new String[] {"index"}, null, null, Collections.singletonMap("typeless", Collections.singletonMap("not a type", "42")))); + ElasticsearchException e = expectThrows( + ElasticsearchException.class, + () -> new DataFrameAnalyticsSource( + new String[] { "index" }, + null, + null, + Collections.singletonMap("typeless", Collections.singletonMap("not a type", "42")) + ) + ); assertThat(e.getMessage(), equalTo("No type specified for runtime field [typeless]")); } @@ -110,8 +121,12 @@ public void testIsFieldExcluded_GivenNoSourceFiltering() { } public void testIsFieldExcluded_GivenSourceFilteringWithNulls() { - DataFrameAnalyticsSource source = new DataFrameAnalyticsSource(new String[] { "index" }, null, - new FetchSourceContext(true, null, null), null); + DataFrameAnalyticsSource source = new DataFrameAnalyticsSource( + new String[] { "index" }, + null, + new FetchSourceContext(true, null, null), + null + ); assertThat(source.isFieldExcluded(randomAlphaOfLength(10)), is(false)); } @@ -139,8 +154,10 @@ public void testIsFieldExcluded_GivenIncludes() { public void testIsFieldExcluded_GivenIncludesAndExcludes() { // Excludes take precedence - assertThat(newSourceWithIncludesExcludes(Collections.singletonList("foo"), Collections.singletonList("foo")) - .isFieldExcluded("foo"), is(true)); + assertThat( + newSourceWithIncludesExcludes(Collections.singletonList("foo"), Collections.singletonList("foo")).isFieldExcluded("foo"), + is(true) + ); } private static DataFrameAnalyticsSource newSourceWithIncludes(String... includes) { @@ -152,9 +169,8 @@ private static DataFrameAnalyticsSource newSourceWithExcludes(String... excludes } private static DataFrameAnalyticsSource newSourceWithIncludesExcludes(List includes, List excludes) { - FetchSourceContext sourceFiltering = new FetchSourceContext(true, - includes.toArray(new String[0]), excludes.toArray(new String[0])); - return new DataFrameAnalyticsSource(new String[] { "index" } , null, sourceFiltering, null); + FetchSourceContext sourceFiltering = new FetchSourceContext(true, includes.toArray(new String[0]), excludes.toArray(new String[0])); + return new DataFrameAnalyticsSource(new String[] { "index" }, null, sourceFiltering, null); } @Override diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsTaskStateTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsTaskStateTests.java index c3cc06e15a6e3..68b8520bbba73 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsTaskStateTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsTaskStateTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ml.dataframe; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/BoostedTreeParamsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/BoostedTreeParamsTests.java index fb55296d59696..04b26555b2365 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/BoostedTreeParamsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/BoostedTreeParamsTests.java @@ -25,12 +25,24 @@ public class BoostedTreeParamsTests extends AbstractBWCSerializationTestCase objParser = - new ConstructingObjectParser<>( - BoostedTreeParams.NAME, - true, - a -> new BoostedTreeParams((Double) a[0], (Double) a[1], (Double) a[2], (Integer) a[3], (Double) a[4], (Integer) a[5], - (Double) a[6], (Double) a[7], (Double) a[8], (Double) a[9], (Double) a[10], (Integer) a[11])); + ConstructingObjectParser objParser = new ConstructingObjectParser<>( + BoostedTreeParams.NAME, + true, + a -> new BoostedTreeParams( + (Double) a[0], + (Double) a[1], + (Double) a[2], + (Integer) a[3], + (Double) a[4], + (Integer) a[5], + (Double) a[6], + (Double) a[7], + (Double) a[8], + (Double) a[9], + (Double) a[10], + (Integer) a[11] + ) + ); BoostedTreeParams.declareFields(objParser); return objParser.apply(parser, null); } @@ -67,71 +79,91 @@ protected Writeable.Reader instanceReader() { } public void testConstructor_GivenNegativeLambda() { - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> BoostedTreeParams.builder().setLambda(-0.00001).build()); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> BoostedTreeParams.builder().setLambda(-0.00001).build() + ); assertThat(e.getMessage(), equalTo("[lambda] must be a non-negative double")); } public void testConstructor_GivenNegativeGamma() { - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> BoostedTreeParams.builder().setGamma(-0.00001).build()); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> BoostedTreeParams.builder().setGamma(-0.00001).build() + ); assertThat(e.getMessage(), equalTo("[gamma] must be a non-negative double")); } public void testConstructor_GivenEtaIsZero() { - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> BoostedTreeParams.builder().setEta(0.0).build()); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> BoostedTreeParams.builder().setEta(0.0).build() + ); assertThat(e.getMessage(), equalTo("[eta] must be a double in [0.001, 1]")); } public void testConstructor_GivenEtaIsGreaterThanOne() { - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> BoostedTreeParams.builder().setEta(1.00001).build()); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> BoostedTreeParams.builder().setEta(1.00001).build() + ); assertThat(e.getMessage(), equalTo("[eta] must be a double in [0.001, 1]")); } public void testConstructor_GivenMaximumNumberTreesIsZero() { - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> BoostedTreeParams.builder().setMaxTrees(0).build()); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> BoostedTreeParams.builder().setMaxTrees(0).build() + ); assertThat(e.getMessage(), equalTo("[max_trees] must be an integer in [1, 2000]")); } public void testConstructor_GivenMaximumNumberTreesIsGreaterThan2k() { - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> BoostedTreeParams.builder().setMaxTrees(2001).build()); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> BoostedTreeParams.builder().setMaxTrees(2001).build() + ); assertThat(e.getMessage(), equalTo("[max_trees] must be an integer in [1, 2000]")); } public void testConstructor_GivenFeatureBagFractionIsLessThanZero() { - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> BoostedTreeParams.builder().setFeatureBagFraction(-0.00001).build()); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> BoostedTreeParams.builder().setFeatureBagFraction(-0.00001).build() + ); assertThat(e.getMessage(), equalTo("[feature_bag_fraction] must be a double in (0, 1]")); } public void testConstructor_GivenFeatureBagFractionIsGreaterThanOne() { - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> BoostedTreeParams.builder().setFeatureBagFraction(1.00001).build()); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> BoostedTreeParams.builder().setFeatureBagFraction(1.00001).build() + ); assertThat(e.getMessage(), equalTo("[feature_bag_fraction] must be a double in (0, 1]")); } public void testConstructor_GivenTopFeatureImportanceValuesIsNegative() { - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> BoostedTreeParams.builder().setNumTopFeatureImportanceValues(-1).build()); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> BoostedTreeParams.builder().setNumTopFeatureImportanceValues(-1).build() + ); assertThat(e.getMessage(), equalTo("[num_top_feature_importance_values] must be a non-negative integer")); } public void testConstructor_GivenAlphaIsNegative() { - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> BoostedTreeParams.builder().setAlpha(-0.001).build()); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> BoostedTreeParams.builder().setAlpha(-0.001).build() + ); assertThat(e.getMessage(), equalTo("[alpha] must be a non-negative double")); } @@ -146,22 +178,28 @@ public void testConstructor_GivenEtaGrowthRatePerTreeIsOnRangeLimit() { } public void testConstructor_GivenEtaGrowthRatePerTreeIsLessThanMin() { - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> BoostedTreeParams.builder().setEtaGrowthRatePerTree(0.49999).build()); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> BoostedTreeParams.builder().setEtaGrowthRatePerTree(0.49999).build() + ); assertThat(e.getMessage(), equalTo("[eta_growth_rate_per_tree] must be a double in [0.5, 2.0]")); } public void testConstructor_GivenEtaGrowthRatePerTreeIsGreaterThanMax() { - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> BoostedTreeParams.builder().setEtaGrowthRatePerTree(2.00001).build()); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> BoostedTreeParams.builder().setEtaGrowthRatePerTree(2.00001).build() + ); assertThat(e.getMessage(), equalTo("[eta_growth_rate_per_tree] must be a double in [0.5, 2.0]")); } public void testConstructor_GivenSoftTreeDepthLimitIsNegative() { - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> BoostedTreeParams.builder().setSoftTreeDepthLimit(-0.001).build()); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> BoostedTreeParams.builder().setSoftTreeDepthLimit(-0.001).build() + ); assertThat(e.getMessage(), equalTo("[soft_tree_depth_limit] must be a non-negative double")); } @@ -171,8 +209,10 @@ public void testConstructor_GivenSoftTreeDepthLimitIsZero() { } public void testConstructor_GivenSoftTreeDepthToleranceIsLessThanMin() { - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> BoostedTreeParams.builder().setSoftTreeDepthTolerance(0.001).build()); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> BoostedTreeParams.builder().setSoftTreeDepthTolerance(0.001).build() + ); assertThat(e.getMessage(), equalTo("[soft_tree_depth_tolerance] must be a double greater than or equal to 0.01")); } @@ -182,15 +222,19 @@ public void testConstructor_GivenSoftTreeDepthToleranceIsMin() { } public void testConstructor_GivenDownsampleFactorIsZero() { - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> BoostedTreeParams.builder().setDownsampleFactor(0.0).build()); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> BoostedTreeParams.builder().setDownsampleFactor(0.0).build() + ); assertThat(e.getMessage(), equalTo("[downsample_factor] must be a double in (0, 1]")); } public void testConstructor_GivenDownsampleFactorIsNegative() { - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> BoostedTreeParams.builder().setDownsampleFactor(-42.0).build()); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> BoostedTreeParams.builder().setDownsampleFactor(-42.0).build() + ); assertThat(e.getMessage(), equalTo("[downsample_factor] must be a double in (0, 1]")); } @@ -200,32 +244,42 @@ public void testConstructor_GivenDownsampleFactorIsOne() { } public void testConstructor_GivenDownsampleFactorIsGreaterThanOne() { - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> BoostedTreeParams.builder().setDownsampleFactor(1.00001).build()); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> BoostedTreeParams.builder().setDownsampleFactor(1.00001).build() + ); assertThat(e.getMessage(), equalTo("[downsample_factor] must be a double in (0, 1]")); } public void testConstructor_GivenMaxOptimizationRoundsPerHyperparameterIsZero() { - assertThat(BoostedTreeParams.builder().setMaxOptimizationRoundsPerHyperparameter(0).build() - .getMaxOptimizationRoundsPerHyperparameter(), equalTo(0)); + assertThat( + BoostedTreeParams.builder().setMaxOptimizationRoundsPerHyperparameter(0).build().getMaxOptimizationRoundsPerHyperparameter(), + equalTo(0) + ); } public void testConstructor_GivenMaxOptimizationRoundsPerHyperparameterIsNegative() { - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> BoostedTreeParams.builder().setMaxOptimizationRoundsPerHyperparameter(-1).build()); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> BoostedTreeParams.builder().setMaxOptimizationRoundsPerHyperparameter(-1).build() + ); assertThat(e.getMessage(), equalTo("[max_optimization_rounds_per_hyperparameter] must be an integer in [0, 20]")); } public void testConstructor_GivenMaxOptimizationRoundsPerHyperparameterIsMax() { - assertThat(BoostedTreeParams.builder().setMaxOptimizationRoundsPerHyperparameter(20).build() - .getMaxOptimizationRoundsPerHyperparameter(), equalTo(20)); + assertThat( + BoostedTreeParams.builder().setMaxOptimizationRoundsPerHyperparameter(20).build().getMaxOptimizationRoundsPerHyperparameter(), + equalTo(20) + ); } public void testConstructor_GivenMaxOptimizationRoundsPerHyperparameterIsGreaterThanMax() { - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> BoostedTreeParams.builder().setMaxOptimizationRoundsPerHyperparameter(21).build()); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> BoostedTreeParams.builder().setMaxOptimizationRoundsPerHyperparameter(21).build() + ); assertThat(e.getMessage(), equalTo("[max_optimization_rounds_per_hyperparameter] must be an integer in [0, 20]")); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/ClassificationTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/ClassificationTests.java index c735fb2f9923f..19ea9ef455a53 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/ClassificationTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/ClassificationTests.java @@ -15,18 +15,18 @@ import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.index.mapper.BooleanFieldMapper; +import org.elasticsearch.index.mapper.KeywordFieldMapper; +import org.elasticsearch.index.mapper.NumberFieldMapper; +import org.elasticsearch.search.SearchModule; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.index.mapper.BooleanFieldMapper; -import org.elasticsearch.index.mapper.KeywordFieldMapper; -import org.elasticsearch.index.mapper.NumberFieldMapper; -import org.elasticsearch.search.SearchModule; import org.elasticsearch.xpack.core.ml.AbstractBWCSerializationTestCase; import org.elasticsearch.xpack.core.ml.inference.MlInferenceNamedXContentProvider; import org.elasticsearch.xpack.core.ml.inference.preprocessing.FrequencyEncodingTests; @@ -93,26 +93,37 @@ public static Classification createRandom() { String dependentVariableName = randomAlphaOfLength(10); BoostedTreeParams boostedTreeParams = BoostedTreeParamsTests.createRandom(); String predictionFieldName = randomBoolean() ? null : randomAlphaOfLength(10); - Classification.ClassAssignmentObjective classAssignmentObjective = randomBoolean() ? - null : randomFrom(Classification.ClassAssignmentObjective.values()); + Classification.ClassAssignmentObjective classAssignmentObjective = randomBoolean() + ? null + : randomFrom(Classification.ClassAssignmentObjective.values()); Integer numTopClasses = randomBoolean() ? null : randomIntBetween(-1, 1000); Double trainingPercent = randomBoolean() ? null : randomDoubleBetween(0.0, 100.0, false); Long randomizeSeed = randomBoolean() ? null : randomLong(); Boolean earlyStoppingEnabled = randomBoolean() ? null : randomBoolean(); - return new Classification(dependentVariableName, boostedTreeParams, predictionFieldName, classAssignmentObjective, - numTopClasses, trainingPercent, randomizeSeed, - randomBoolean() ? - null : - Stream.generate(() -> randomFrom(FrequencyEncodingTests.createRandom(true), - OneHotEncodingTests.createRandom(true), - TargetMeanEncodingTests.createRandom(true))) - .limit(randomIntBetween(0, 5)) - .collect(Collectors.toList()), - earlyStoppingEnabled); + return new Classification( + dependentVariableName, + boostedTreeParams, + predictionFieldName, + classAssignmentObjective, + numTopClasses, + trainingPercent, + randomizeSeed, + randomBoolean() + ? null + : Stream.generate( + () -> randomFrom( + FrequencyEncodingTests.createRandom(true), + OneHotEncodingTests.createRandom(true), + TargetMeanEncodingTests.createRandom(true) + ) + ).limit(randomIntBetween(0, 5)).collect(Collectors.toList()), + earlyStoppingEnabled + ); } public static Classification mutateForVersion(Classification instance, Version version) { - return new Classification(instance.getDependentVariable(), + return new Classification( + instance.getDependentVariable(), BoostedTreeParamsTests.mutateForVersion(instance.getBoostedTreeParams(), version), instance.getPredictionFieldName(), version.onOrAfter(Version.V_7_7_0) ? instance.getClassAssignmentObjective() : null, @@ -120,7 +131,8 @@ public static Classification mutateForVersion(Classification instance, Version v instance.getTrainingPercent(), instance.getRandomizeSeed(), version.onOrAfter(Version.V_7_10_0) ? instance.getFeatureProcessors() : Collections.emptyList(), - instance.getEarlyStoppingEnabled()); + instance.getEarlyStoppingEnabled() + ); } @Override @@ -130,7 +142,8 @@ protected void assertOnBWCObject(Classification bwcSerializedObject, Classificat return; } - Classification newBwc = new Classification(bwcSerializedObject.getDependentVariable(), + Classification newBwc = new Classification( + bwcSerializedObject.getDependentVariable(), bwcSerializedObject.getBoostedTreeParams(), bwcSerializedObject.getPredictionFieldName(), bwcSerializedObject.getClassAssignmentObjective(), @@ -138,8 +151,10 @@ protected void assertOnBWCObject(Classification bwcSerializedObject, Classificat bwcSerializedObject.getTrainingPercent(), 42L, bwcSerializedObject.getFeatureProcessors(), - bwcSerializedObject.getEarlyStoppingEnabled()); - Classification newInstance = new Classification(testInstance.getDependentVariable(), + bwcSerializedObject.getEarlyStoppingEnabled() + ); + Classification newInstance = new Classification( + testInstance.getDependentVariable(), testInstance.getBoostedTreeParams(), testInstance.getPredictionFieldName(), testInstance.getClassAssignmentObjective(), @@ -147,7 +162,8 @@ protected void assertOnBWCObject(Classification bwcSerializedObject, Classificat testInstance.getTrainingPercent(), 42L, testInstance.getFeatureProcessors(), - testInstance.getEarlyStoppingEnabled()); + testInstance.getEarlyStoppingEnabled() + ); super.assertOnBWCObject(newBwc, newInstance, version); } @@ -157,47 +173,51 @@ protected Writeable.Reader instanceReader() { } public void testDeserialization() throws IOException { - String toDeserialize = "{\n" + - " \"dependent_variable\": \"FlightDelayMin\",\n" + - " \"feature_processors\": [\n" + - " {\n" + - " \"one_hot_encoding\": {\n" + - " \"field\": \"OriginWeather\",\n" + - " \"hot_map\": {\n" + - " \"sunny_col\": \"Sunny\",\n" + - " \"clear_col\": \"Clear\",\n" + - " \"rainy_col\": \"Rain\"\n" + - " }\n" + - " }\n" + - " },\n" + - " {\n" + - " \"one_hot_encoding\": {\n" + - " \"field\": \"DestWeather\",\n" + - " \"hot_map\": {\n" + - " \"dest_sunny_col\": \"Sunny\",\n" + - " \"dest_clear_col\": \"Clear\",\n" + - " \"dest_rainy_col\": \"Rain\"\n" + - " }\n" + - " }\n" + - " },\n" + - " {\n" + - " \"frequency_encoding\": {\n" + - " \"field\": \"OriginWeather\",\n" + - " \"feature_name\": \"mean\",\n" + - " \"frequency_map\": {\n" + - " \"Sunny\": 0.8,\n" + - " \"Rain\": 0.2\n" + - " }\n" + - " }\n" + - " }\n" + - " ]\n" + - " }" + - ""; - - try(XContentParser parser = XContentHelper.createParser(xContentRegistry(), - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, - new BytesArray(toDeserialize), - XContentType.JSON)) { + String toDeserialize = "{\n" + + " \"dependent_variable\": \"FlightDelayMin\",\n" + + " \"feature_processors\": [\n" + + " {\n" + + " \"one_hot_encoding\": {\n" + + " \"field\": \"OriginWeather\",\n" + + " \"hot_map\": {\n" + + " \"sunny_col\": \"Sunny\",\n" + + " \"clear_col\": \"Clear\",\n" + + " \"rainy_col\": \"Rain\"\n" + + " }\n" + + " }\n" + + " },\n" + + " {\n" + + " \"one_hot_encoding\": {\n" + + " \"field\": \"DestWeather\",\n" + + " \"hot_map\": {\n" + + " \"dest_sunny_col\": \"Sunny\",\n" + + " \"dest_clear_col\": \"Clear\",\n" + + " \"dest_rainy_col\": \"Rain\"\n" + + " }\n" + + " }\n" + + " },\n" + + " {\n" + + " \"frequency_encoding\": {\n" + + " \"field\": \"OriginWeather\",\n" + + " \"feature_name\": \"mean\",\n" + + " \"frequency_map\": {\n" + + " \"Sunny\": 0.8,\n" + + " \"Rain\": 0.2\n" + + " }\n" + + " }\n" + + " }\n" + + " ]\n" + + " }" + + ""; + + try ( + XContentParser parser = XContentHelper.createParser( + xContentRegistry(), + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + new BytesArray(toDeserialize), + XContentType.JSON + ) + ) { Classification parsed = Classification.fromXContent(parser, false); assertThat(parsed.getDependentVariable(), equalTo("FlightDelayMin")); for (PreProcessor preProcessor : parsed.getFeatureProcessors()) { @@ -207,36 +227,46 @@ public void testDeserialization() throws IOException { } public void testConstructor_GivenTrainingPercentIsZero() { - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> new Classification("foo", BOOSTED_TREE_PARAMS, "result", null, 3, 0.0, randomLong(), null, null)); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> new Classification("foo", BOOSTED_TREE_PARAMS, "result", null, 3, 0.0, randomLong(), null, null) + ); assertThat(e.getMessage(), equalTo("[training_percent] must be a positive double in (0, 100]")); } public void testConstructor_GivenTrainingPercentIsLessThanZero() { - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> new Classification("foo", BOOSTED_TREE_PARAMS, "result", null, 3, -1.0, randomLong(), null, null)); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> new Classification("foo", BOOSTED_TREE_PARAMS, "result", null, 3, -1.0, randomLong(), null, null) + ); assertThat(e.getMessage(), equalTo("[training_percent] must be a positive double in (0, 100]")); } public void testConstructor_GivenTrainingPercentIsGreaterThan100() { - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> new Classification("foo", BOOSTED_TREE_PARAMS, "result", null, 3, 100.0001, randomLong(), null, null)); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> new Classification("foo", BOOSTED_TREE_PARAMS, "result", null, 3, 100.0001, randomLong(), null, null) + ); assertThat(e.getMessage(), equalTo("[training_percent] must be a positive double in (0, 100]")); } public void testConstructor_GivenNumTopClassesIsLessThanMinusOne() { - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> new Classification("foo", BOOSTED_TREE_PARAMS, "result", null, -2, 1.0, randomLong(), null, null)); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> new Classification("foo", BOOSTED_TREE_PARAMS, "result", null, -2, 1.0, randomLong(), null, null) + ); assertThat(e.getMessage(), equalTo("[num_top_classes] must be an integer in [0, 1000] or a special value -1")); } public void testConstructor_GivenNumTopClassesIsGreaterThan1000() { - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> new Classification("foo", BOOSTED_TREE_PARAMS, "result", null, 1001, 1.0, randomLong(), null, null)); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> new Classification("foo", BOOSTED_TREE_PARAMS, "result", null, 1001, 1.0, randomLong(), null, null) + ); assertThat(e.getMessage(), equalTo("[num_top_classes] must be an integer in [0, 1000] or a special value -1")); } @@ -250,12 +280,30 @@ public void testGetPredictionFieldName() { } public void testClassAssignmentObjective() { - Classification classification = new Classification("foo", BOOSTED_TREE_PARAMS, "result", - Classification.ClassAssignmentObjective.MAXIMIZE_ACCURACY, 7, 1.0, randomLong(), null, null); + Classification classification = new Classification( + "foo", + BOOSTED_TREE_PARAMS, + "result", + Classification.ClassAssignmentObjective.MAXIMIZE_ACCURACY, + 7, + 1.0, + randomLong(), + null, + null + ); assertThat(classification.getClassAssignmentObjective(), equalTo(Classification.ClassAssignmentObjective.MAXIMIZE_ACCURACY)); - classification = new Classification("foo", BOOSTED_TREE_PARAMS, "result", - Classification.ClassAssignmentObjective.MAXIMIZE_MINIMUM_RECALL, 7, 1.0, randomLong(), null, null); + classification = new Classification( + "foo", + BOOSTED_TREE_PARAMS, + "result", + Classification.ClassAssignmentObjective.MAXIMIZE_MINIMUM_RECALL, + 7, + 1.0, + randomLong(), + null, + null + ); assertThat(classification.getClassAssignmentObjective(), equalTo(Classification.ClassAssignmentObjective.MAXIMIZE_MINIMUM_RECALL)); // class_assignment_objective == null, default applied @@ -304,58 +352,84 @@ public void testGetTrainingPercent() { public void testGetParams() { DataFrameAnalysis.FieldInfo fieldInfo = new TestFieldInfo( Map.of( - "foo", Set.of(BooleanFieldMapper.CONTENT_TYPE), - "bar", Set.of(NumberFieldMapper.NumberType.LONG.typeName()), - "baz", Set.of(KeywordFieldMapper.CONTENT_TYPE)), - Map.of( - "foo", 10L, - "bar", 20L, - "baz", 30L) + "foo", + Set.of(BooleanFieldMapper.CONTENT_TYPE), + "bar", + Set.of(NumberFieldMapper.NumberType.LONG.typeName()), + "baz", + Set.of(KeywordFieldMapper.CONTENT_TYPE) + ), + Map.of("foo", 10L, "bar", 20L, "baz", 30L) ); assertThat( new Classification("foo").getParams(fieldInfo), equalTo( Map.of( - "dependent_variable", "foo", - "class_assignment_objective", Classification.ClassAssignmentObjective.MAXIMIZE_MINIMUM_RECALL, - "num_top_classes", 2, - "prediction_field_name", "foo_prediction", - "prediction_field_type", "bool", - "num_classes", 10L, - "training_percent", 100.0, - "early_stopping_enabled", true))); + "dependent_variable", + "foo", + "class_assignment_objective", + Classification.ClassAssignmentObjective.MAXIMIZE_MINIMUM_RECALL, + "num_top_classes", + 2, + "prediction_field_name", + "foo_prediction", + "prediction_field_type", + "bool", + "num_classes", + 10L, + "training_percent", + 100.0, + "early_stopping_enabled", + true + ) + ) + ); assertThat( new Classification("bar").getParams(fieldInfo), equalTo( Map.of( - "dependent_variable", "bar", - "class_assignment_objective", Classification.ClassAssignmentObjective.MAXIMIZE_MINIMUM_RECALL, - "num_top_classes", 2, - "prediction_field_name", "bar_prediction", - "prediction_field_type", "int", - "num_classes", 20L, - "training_percent", 100.0, - "early_stopping_enabled", true))); + "dependent_variable", + "bar", + "class_assignment_objective", + Classification.ClassAssignmentObjective.MAXIMIZE_MINIMUM_RECALL, + "num_top_classes", + 2, + "prediction_field_name", + "bar_prediction", + "prediction_field_type", + "int", + "num_classes", + 20L, + "training_percent", + 100.0, + "early_stopping_enabled", + true + ) + ) + ); assertThat( - new Classification("baz", - BoostedTreeParams.builder().build() , - null, - null, - null, - 50.0, - null, - null, - null).getParams(fieldInfo), + new Classification("baz", BoostedTreeParams.builder().build(), null, null, null, 50.0, null, null, null).getParams(fieldInfo), equalTo( Map.of( - "dependent_variable", "baz", - "class_assignment_objective", Classification.ClassAssignmentObjective.MAXIMIZE_MINIMUM_RECALL, - "num_top_classes", 2, - "prediction_field_name", "baz_prediction", - "prediction_field_type", "string", - "num_classes", 30L, - "training_percent", 50.0, - "early_stopping_enabled", true))); + "dependent_variable", + "baz", + "class_assignment_objective", + Classification.ClassAssignmentObjective.MAXIMIZE_MINIMUM_RECALL, + "num_top_classes", + 2, + "prediction_field_name", + "baz_prediction", + "prediction_field_type", + "string", + "num_classes", + 30L, + "training_percent", + 50.0, + "early_stopping_enabled", + true + ) + ) + ); } public void testRequiredFieldsIsNonEmpty() { @@ -374,33 +448,42 @@ public void testFieldCardinalityLimitsIsNonEmpty() { public void testGetResultMappings_DependentVariableMappingIsAbsent() { FieldCapabilitiesResponse fieldCapabilitiesResponse = new FieldCapabilitiesResponse(new String[0], Collections.emptyMap()); - expectThrows(ElasticsearchStatusException.class, - () -> new Classification("foo").getResultMappings("results", fieldCapabilitiesResponse)); + expectThrows( + ElasticsearchStatusException.class, + () -> new Classification("foo").getResultMappings("results", fieldCapabilitiesResponse) + ); } public void testGetResultMappings_DependentVariableMappingHasNoTypes() { - FieldCapabilitiesResponse fieldCapabilitiesResponse = - new FieldCapabilitiesResponse(new String[0], Collections.singletonMap("foo", Collections.emptyMap())); - expectThrows(ElasticsearchStatusException.class, - () -> new Classification("foo").getResultMappings("results", fieldCapabilitiesResponse)); + FieldCapabilitiesResponse fieldCapabilitiesResponse = new FieldCapabilitiesResponse( + new String[0], + Collections.singletonMap("foo", Collections.emptyMap()) + ); + expectThrows( + ElasticsearchStatusException.class, + () -> new Classification("foo").getResultMappings("results", fieldCapabilitiesResponse) + ); } public void testGetResultMappings_DependentVariableMappingIsPresent() { - Map expectedTopClassesMapping = new HashMap<>() {{ - put("type", "nested"); - put("properties", new HashMap<>() {{ - put("class_name", singletonMap("type", "dummy")); - put("class_probability", singletonMap("type", "double")); - put("class_score", singletonMap("type", "double")); - }}); - }}; - FieldCapabilitiesResponse fieldCapabilitiesResponse = - new FieldCapabilitiesResponse( - new String[0], - Collections.singletonMap("foo", Collections.singletonMap("dummy", createFieldCapabilities("foo", "dummy")))); - - Map resultMappings = - new Classification("foo").getResultMappings("results", fieldCapabilitiesResponse); + Map expectedTopClassesMapping = new HashMap<>() { + { + put("type", "nested"); + put("properties", new HashMap<>() { + { + put("class_name", singletonMap("type", "dummy")); + put("class_probability", singletonMap("type", "double")); + put("class_score", singletonMap("type", "double")); + } + }); + } + }; + FieldCapabilitiesResponse fieldCapabilitiesResponse = new FieldCapabilitiesResponse( + new String[0], + Collections.singletonMap("foo", Collections.singletonMap("dummy", createFieldCapabilities("foo", "dummy"))) + ); + + Map resultMappings = new Classification("foo").getResultMappings("results", fieldCapabilitiesResponse); assertThat(resultMappings, hasEntry("results.foo_prediction", singletonMap("type", "dummy"))); assertThat(resultMappings, hasEntry("results.prediction_probability", singletonMap("type", "double"))); @@ -485,8 +568,9 @@ public void testInferenceConfig() { ClassificationConfig classificationConfig = (ClassificationConfig) inferenceConfig; assertThat(classificationConfig.getResultsField(), equalTo(classification.getPredictionFieldName())); assertThat(classificationConfig.getNumTopClasses(), equalTo(classification.getNumTopClasses())); - Integer expectedNumTopFeatureImportanceValues = classification.getBoostedTreeParams().getNumTopFeatureImportanceValues() == null ? - 0 : classification.getBoostedTreeParams().getNumTopFeatureImportanceValues(); + Integer expectedNumTopFeatureImportanceValues = classification.getBoostedTreeParams().getNumTopFeatureImportanceValues() == null + ? 0 + : classification.getBoostedTreeParams().getNumTopFeatureImportanceValues(); assertThat(classificationConfig.getNumTopFeatureImportanceValues(), equalTo(expectedNumTopFeatureImportanceValues)); assertThat(classificationConfig.getPredictionFieldType(), equalTo(PredictionFieldType.STRING)); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/OutlierDetectionTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/OutlierDetectionTests.java index 395bb6d8a51e8..dbbd6317baa91 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/OutlierDetectionTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/OutlierDetectionTests.java @@ -8,8 +8,8 @@ import org.elasticsearch.Version; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.index.mapper.NumberFieldMapper; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.AbstractBWCSerializationTestCase; import java.io.IOException; @@ -39,8 +39,7 @@ public static OutlierDetection createRandom() { Integer numberNeighbors = randomBoolean() ? null : randomIntBetween(1, 20); OutlierDetection.Method method = randomBoolean() ? null : randomFrom(OutlierDetection.Method.values()); Double minScoreToWriteFeatureInfluence = randomBoolean() ? null : randomDoubleBetween(0.0, 1.0, true); - return new OutlierDetection.Builder() - .setNNeighbors(numberNeighbors) + return new OutlierDetection.Builder().setNNeighbors(numberNeighbors) .setMethod(method) .setFeatureInfluenceThreshold(minScoreToWriteFeatureInfluence) .setComputeFeatureInfluence(randomBoolean()) @@ -51,8 +50,7 @@ public static OutlierDetection createRandom() { public static OutlierDetection mutateForVersion(OutlierDetection instance, Version version) { if (version.before(Version.V_7_5_0)) { - return new OutlierDetection.Builder(instance) - .setComputeFeatureInfluence(true) + return new OutlierDetection.Builder(instance).setComputeFeatureInfluence(true) .setOutlierFraction(0.05) .setStandardizationEnabled(true) .build(); @@ -78,8 +76,7 @@ public void testGetParams_GivenDefaults() { } public void testGetParams_GivenExplicitValues() { - OutlierDetection outlierDetection = new OutlierDetection.Builder() - .setNNeighbors(42) + OutlierDetection outlierDetection = new OutlierDetection.Builder().setNNeighbors(42) .setMethod(OutlierDetection.Method.LDOF) .setFeatureInfluenceThreshold(0.42) .setComputeFeatureInfluence(false) @@ -92,11 +89,9 @@ public void testGetParams_GivenExplicitValues() { assertThat(params.size(), equalTo(6)); assertThat(params.get(OutlierDetection.N_NEIGHBORS.getPreferredName()), equalTo(42)); assertThat(params.get(OutlierDetection.METHOD.getPreferredName()), equalTo(OutlierDetection.Method.LDOF)); - assertThat((Double) params.get(OutlierDetection.FEATURE_INFLUENCE_THRESHOLD.getPreferredName()), - is(closeTo(0.42, 1E-9))); + assertThat((Double) params.get(OutlierDetection.FEATURE_INFLUENCE_THRESHOLD.getPreferredName()), is(closeTo(0.42, 1E-9))); assertThat(params.get(OutlierDetection.COMPUTE_FEATURE_INFLUENCE.getPreferredName()), is(false)); - assertThat((Double) params.get(OutlierDetection.OUTLIER_FRACTION.getPreferredName()), - is(closeTo(0.9, 1E-9))); + assertThat((Double) params.get(OutlierDetection.OUTLIER_FRACTION.getPreferredName()), is(closeTo(0.9, 1E-9))); assertThat(params.get(OutlierDetection.STANDARDIZATION_ENABLED.getPreferredName()), is(false)); } @@ -112,8 +107,10 @@ public void testGetResultMappings() { Map mappedFields = createTestInstance().getResultMappings("test", null); assertThat(mappedFields.size(), equalTo(2)); assertThat(mappedFields, hasKey("test.outlier_score")); - assertThat(mappedFields.get("test.outlier_score"), - equalTo(Collections.singletonMap("type", NumberFieldMapper.NumberType.DOUBLE.typeName()))); + assertThat( + mappedFields.get("test.outlier_score"), + equalTo(Collections.singletonMap("type", NumberFieldMapper.NumberType.DOUBLE.typeName())) + ); assertThat(mappedFields, hasKey("test.feature_influence")); assertThat(mappedFields.get("test.feature_influence"), equalTo(OutlierDetection.FEATURE_INFLUENCE_MAPPING)); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/RegressionTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/RegressionTests.java index be8e95c5b96e9..ac2fb220857dc 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/RegressionTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/RegressionTests.java @@ -13,15 +13,15 @@ import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.search.SearchModule; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.search.SearchModule; import org.elasticsearch.xpack.core.ml.AbstractBWCSerializationTestCase; import org.elasticsearch.xpack.core.ml.inference.MlInferenceNamedXContentProvider; import org.elasticsearch.xpack.core.ml.inference.preprocessing.FrequencyEncodingTests; @@ -90,20 +90,30 @@ private static Regression createRandom(BoostedTreeParams boostedTreeParams) { Regression.LossFunction lossFunction = randomBoolean() ? null : randomFrom(Regression.LossFunction.values()); Double lossFunctionParameter = randomBoolean() ? null : randomDoubleBetween(0.0, Double.MAX_VALUE, false); Boolean earlyStoppingEnabled = randomBoolean() ? null : randomBoolean(); - return new Regression(dependentVariableName, boostedTreeParams, predictionFieldName, trainingPercent, randomizeSeed, lossFunction, + return new Regression( + dependentVariableName, + boostedTreeParams, + predictionFieldName, + trainingPercent, + randomizeSeed, + lossFunction, lossFunctionParameter, - randomBoolean() ? - null : - Stream.generate(() -> randomFrom(FrequencyEncodingTests.createRandom(true), - OneHotEncodingTests.createRandom(true), - TargetMeanEncodingTests.createRandom(true))) - .limit(randomIntBetween(0, 5)) - .collect(Collectors.toList()), - earlyStoppingEnabled); + randomBoolean() + ? null + : Stream.generate( + () -> randomFrom( + FrequencyEncodingTests.createRandom(true), + OneHotEncodingTests.createRandom(true), + TargetMeanEncodingTests.createRandom(true) + ) + ).limit(randomIntBetween(0, 5)).collect(Collectors.toList()), + earlyStoppingEnabled + ); } public static Regression mutateForVersion(Regression instance, Version version) { - return new Regression(instance.getDependentVariable(), + return new Regression( + instance.getDependentVariable(), BoostedTreeParamsTests.mutateForVersion(instance.getBoostedTreeParams(), version), instance.getPredictionFieldName(), instance.getTrainingPercent(), @@ -111,7 +121,8 @@ public static Regression mutateForVersion(Regression instance, Version version) instance.getLossFunction(), instance.getLossFunctionParameter(), version.onOrAfter(Version.V_7_10_0) ? instance.getFeatureProcessors() : Collections.emptyList(), - instance.getEarlyStoppingEnabled()); + instance.getEarlyStoppingEnabled() + ); } @Override @@ -121,7 +132,8 @@ protected void assertOnBWCObject(Regression bwcSerializedObject, Regression test return; } - Regression newBwc = new Regression(bwcSerializedObject.getDependentVariable(), + Regression newBwc = new Regression( + bwcSerializedObject.getDependentVariable(), bwcSerializedObject.getBoostedTreeParams(), bwcSerializedObject.getPredictionFieldName(), bwcSerializedObject.getTrainingPercent(), @@ -129,8 +141,10 @@ protected void assertOnBWCObject(Regression bwcSerializedObject, Regression test bwcSerializedObject.getLossFunction(), bwcSerializedObject.getLossFunctionParameter(), bwcSerializedObject.getFeatureProcessors(), - bwcSerializedObject.getEarlyStoppingEnabled()); - Regression newInstance = new Regression(testInstance.getDependentVariable(), + bwcSerializedObject.getEarlyStoppingEnabled() + ); + Regression newInstance = new Regression( + testInstance.getDependentVariable(), testInstance.getBoostedTreeParams(), testInstance.getPredictionFieldName(), testInstance.getTrainingPercent(), @@ -138,7 +152,8 @@ protected void assertOnBWCObject(Regression bwcSerializedObject, Regression test testInstance.getLossFunction(), testInstance.getLossFunctionParameter(), testInstance.getFeatureProcessors(), - testInstance.getEarlyStoppingEnabled()); + testInstance.getEarlyStoppingEnabled() + ); super.assertOnBWCObject(newBwc, newInstance, version); } @@ -153,47 +168,51 @@ protected Writeable.Reader instanceReader() { } public void testDeserialization() throws IOException { - String toDeserialize = "{\n" + - " \"dependent_variable\": \"FlightDelayMin\",\n" + - " \"feature_processors\": [\n" + - " {\n" + - " \"one_hot_encoding\": {\n" + - " \"field\": \"OriginWeather\",\n" + - " \"hot_map\": {\n" + - " \"sunny_col\": \"Sunny\",\n" + - " \"clear_col\": \"Clear\",\n" + - " \"rainy_col\": \"Rain\"\n" + - " }\n" + - " }\n" + - " },\n" + - " {\n" + - " \"one_hot_encoding\": {\n" + - " \"field\": \"DestWeather\",\n" + - " \"hot_map\": {\n" + - " \"dest_sunny_col\": \"Sunny\",\n" + - " \"dest_clear_col\": \"Clear\",\n" + - " \"dest_rainy_col\": \"Rain\"\n" + - " }\n" + - " }\n" + - " },\n" + - " {\n" + - " \"frequency_encoding\": {\n" + - " \"field\": \"OriginWeather\",\n" + - " \"feature_name\": \"mean\",\n" + - " \"frequency_map\": {\n" + - " \"Sunny\": 0.8,\n" + - " \"Rain\": 0.2\n" + - " }\n" + - " }\n" + - " }\n" + - " ]\n" + - " }" + - ""; - - try(XContentParser parser = XContentHelper.createParser(xContentRegistry(), - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, - new BytesArray(toDeserialize), - XContentType.JSON)) { + String toDeserialize = "{\n" + + " \"dependent_variable\": \"FlightDelayMin\",\n" + + " \"feature_processors\": [\n" + + " {\n" + + " \"one_hot_encoding\": {\n" + + " \"field\": \"OriginWeather\",\n" + + " \"hot_map\": {\n" + + " \"sunny_col\": \"Sunny\",\n" + + " \"clear_col\": \"Clear\",\n" + + " \"rainy_col\": \"Rain\"\n" + + " }\n" + + " }\n" + + " },\n" + + " {\n" + + " \"one_hot_encoding\": {\n" + + " \"field\": \"DestWeather\",\n" + + " \"hot_map\": {\n" + + " \"dest_sunny_col\": \"Sunny\",\n" + + " \"dest_clear_col\": \"Clear\",\n" + + " \"dest_rainy_col\": \"Rain\"\n" + + " }\n" + + " }\n" + + " },\n" + + " {\n" + + " \"frequency_encoding\": {\n" + + " \"field\": \"OriginWeather\",\n" + + " \"feature_name\": \"mean\",\n" + + " \"frequency_map\": {\n" + + " \"Sunny\": 0.8,\n" + + " \"Rain\": 0.2\n" + + " }\n" + + " }\n" + + " }\n" + + " ]\n" + + " }" + + ""; + + try ( + XContentParser parser = XContentHelper.createParser( + xContentRegistry(), + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + new BytesArray(toDeserialize), + XContentType.JSON + ) + ) { Regression parsed = Regression.fromXContent(parser, false); assertThat(parsed.getDependentVariable(), equalTo("FlightDelayMin")); for (PreProcessor preProcessor : parsed.getFeatureProcessors()) { @@ -203,82 +222,138 @@ public void testDeserialization() throws IOException { } public void testConstructor_GivenTrainingPercentIsZero() { - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> new Regression("foo", BOOSTED_TREE_PARAMS, "result", 0.0, randomLong(), - Regression.LossFunction.MSE, null, null, null)); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> new Regression("foo", BOOSTED_TREE_PARAMS, "result", 0.0, randomLong(), Regression.LossFunction.MSE, null, null, null) + ); assertThat(e.getMessage(), equalTo("[training_percent] must be a positive double in (0, 100]")); } public void testConstructor_GivenTrainingPercentIsLessThanZero() { - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> new Regression("foo", BOOSTED_TREE_PARAMS, "result", -0.01, randomLong(), - Regression.LossFunction.MSE, null, null, null)); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> new Regression("foo", BOOSTED_TREE_PARAMS, "result", -0.01, randomLong(), Regression.LossFunction.MSE, null, null, null) + ); assertThat(e.getMessage(), equalTo("[training_percent] must be a positive double in (0, 100]")); } public void testConstructor_GivenTrainingPercentIsGreaterThan100() { - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> new Regression("foo", BOOSTED_TREE_PARAMS, "result", 100.0001, randomLong(), - Regression.LossFunction.MSE, null, null, null)); - + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> new Regression( + "foo", + BOOSTED_TREE_PARAMS, + "result", + 100.0001, + randomLong(), + Regression.LossFunction.MSE, + null, + null, + null + ) + ); assertThat(e.getMessage(), equalTo("[training_percent] must be a positive double in (0, 100]")); } public void testConstructor_GivenLossFunctionParameterIsZero() { - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> new Regression("foo", BOOSTED_TREE_PARAMS, "result", 100.0, randomLong(), - Regression.LossFunction.MSE, 0.0, null, null)); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> new Regression("foo", BOOSTED_TREE_PARAMS, "result", 100.0, randomLong(), Regression.LossFunction.MSE, 0.0, null, null) + ); assertThat(e.getMessage(), equalTo("[loss_function_parameter] must be a positive double")); } public void testConstructor_GivenLossFunctionParameterIsNegative() { - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> new Regression("foo", BOOSTED_TREE_PARAMS, "result", 100.0, randomLong(), - Regression.LossFunction.MSE, -1.0, null, null)); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> new Regression("foo", BOOSTED_TREE_PARAMS, "result", 100.0, randomLong(), Regression.LossFunction.MSE, -1.0, null, null) + ); assertThat(e.getMessage(), equalTo("[loss_function_parameter] must be a positive double")); } public void testGetPredictionFieldName() { - Regression regression = new Regression("foo", BOOSTED_TREE_PARAMS, "result", 50.0, randomLong(), - Regression.LossFunction.MSE, 1.0, null, null); + Regression regression = new Regression( + "foo", + BOOSTED_TREE_PARAMS, + "result", + 50.0, + randomLong(), + Regression.LossFunction.MSE, + 1.0, + null, + null + ); assertThat(regression.getPredictionFieldName(), equalTo("result")); - regression = new Regression("foo", BOOSTED_TREE_PARAMS, null, 50.0, randomLong(), - Regression.LossFunction.MSE, null, null, null); + regression = new Regression("foo", BOOSTED_TREE_PARAMS, null, 50.0, randomLong(), Regression.LossFunction.MSE, null, null, null); assertThat(regression.getPredictionFieldName(), equalTo("foo_prediction")); } public void testGetTrainingPercent() { - Regression regression = new Regression("foo", BOOSTED_TREE_PARAMS, "result", 50.0, randomLong(), - Regression.LossFunction.MSE, 1.0, null, null); + Regression regression = new Regression( + "foo", + BOOSTED_TREE_PARAMS, + "result", + 50.0, + randomLong(), + Regression.LossFunction.MSE, + 1.0, + null, + null + ); assertThat(regression.getTrainingPercent(), equalTo(50.0)); // Boundary condition: training_percent == 1.0 - regression = new Regression("foo", BOOSTED_TREE_PARAMS, "result", 1.0, randomLong(), - Regression.LossFunction.MSE, null, null, null); + regression = new Regression("foo", BOOSTED_TREE_PARAMS, "result", 1.0, randomLong(), Regression.LossFunction.MSE, null, null, null); assertThat(regression.getTrainingPercent(), equalTo(1.0)); // Boundary condition: training_percent == 100.0 - regression = new Regression("foo", BOOSTED_TREE_PARAMS, "result", 100.0, randomLong(), - Regression.LossFunction.MSE, null, null, null); + regression = new Regression( + "foo", + BOOSTED_TREE_PARAMS, + "result", + 100.0, + randomLong(), + Regression.LossFunction.MSE, + null, + null, + null + ); assertThat(regression.getTrainingPercent(), equalTo(100.0)); // training_percent == null, default applied - regression = new Regression("foo", BOOSTED_TREE_PARAMS, "result", null, randomLong(), - Regression.LossFunction.MSE, null, null, null); + regression = new Regression( + "foo", + BOOSTED_TREE_PARAMS, + "result", + null, + randomLong(), + Regression.LossFunction.MSE, + null, + null, + null + ); assertThat(regression.getTrainingPercent(), equalTo(100.0)); } public void testGetParams_ShouldIncludeBoostedTreeParams() { int maxTrees = randomIntBetween(1, 100); - Regression regression = new Regression("foo", + Regression regression = new Regression( + "foo", BoostedTreeParams.builder().setMaxTrees(maxTrees).build(), - null, 100.0, 0L, Regression.LossFunction.MSE, null, null, null); + null, + 100.0, + 0L, + Regression.LossFunction.MSE, + null, + null, + null + ); Map params = regression.getParams(null); @@ -296,9 +371,9 @@ public void testGetParams_GivenRandomWithoutBoostedTreeParams() { Map params = regression.getParams(null); - int expectedParamsCount = 5 - + (regression.getLossFunctionParameter() == null ? 0 : 1) - + (regression.getFeatureProcessors().isEmpty() ? 0 : 1); + int expectedParamsCount = 5 + (regression.getLossFunctionParameter() == null ? 0 : 1) + (regression.getFeatureProcessors().isEmpty() + ? 0 + : 1); assertThat(params.size(), equalTo(expectedParamsCount)); assertThat(params.get("dependent_variable"), equalTo(regression.getDependentVariable())); assertThat(params.get("prediction_field_name"), equalTo(regression.getPredictionFieldName())); @@ -393,8 +468,9 @@ public void testInferenceConfig() { RegressionConfig regressionConfig = (RegressionConfig) inferenceConfig; assertThat(regressionConfig.getResultsField(), equalTo(regression.getPredictionFieldName())); - Integer expectedNumTopFeatureImportanceValues = regression.getBoostedTreeParams().getNumTopFeatureImportanceValues() == null ? - 0 : regression.getBoostedTreeParams().getNumTopFeatureImportanceValues(); + Integer expectedNumTopFeatureImportanceValues = regression.getBoostedTreeParams().getNumTopFeatureImportanceValues() == null + ? 0 + : regression.getBoostedTreeParams().getNumTopFeatureImportanceValues(); assertThat(regressionConfig.getNumTopFeatureImportanceValues(), equalTo(expectedNumTopFeatureImportanceValues)); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/AccuracyTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/AccuracyTests.java index 8121d64f99b69..591b1bea2dd2f 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/AccuracyTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/AccuracyTests.java @@ -8,9 +8,9 @@ import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.aggregations.Aggregations; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationFields; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationParameters; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.classification.Accuracy.Result; @@ -59,30 +59,54 @@ public static Accuracy createRandom() { } public void testProcess() { - Aggregations aggs = new Aggregations(List.of( - mockTerms( - "accuracy_" + MulticlassConfusionMatrix.STEP_1_AGGREGATE_BY_ACTUAL_CLASS, - List.of( - mockTermsBucket("dog", new Aggregations(List.of())), - mockTermsBucket("cat", new Aggregations(List.of()))), - 100L), - mockCardinality("accuracy_" + MulticlassConfusionMatrix.STEP_1_CARDINALITY_OF_ACTUAL_CLASS, 1000L), - mockFilters( - "accuracy_" + MulticlassConfusionMatrix.STEP_2_AGGREGATE_BY_ACTUAL_CLASS, - List.of( - mockFiltersBucket( - "dog", - 30, - new Aggregations(List.of(mockFilters( - "accuracy_" + MulticlassConfusionMatrix.STEP_2_AGGREGATE_BY_PREDICTED_CLASS, - List.of(mockFiltersBucket("cat", 10L), mockFiltersBucket("dog", 20L), mockFiltersBucket("_other_", 0L)))))), - mockFiltersBucket( - "cat", - 70, - new Aggregations(List.of(mockFilters( - "accuracy_" + MulticlassConfusionMatrix.STEP_2_AGGREGATE_BY_PREDICTED_CLASS, - List.of(mockFiltersBucket("cat", 30L), mockFiltersBucket("dog", 40L), mockFiltersBucket("_other_", 0L)))))))), - mockSingleValue(Accuracy.OVERALL_ACCURACY_AGG_NAME, 0.5))); + Aggregations aggs = new Aggregations( + List.of( + mockTerms( + "accuracy_" + MulticlassConfusionMatrix.STEP_1_AGGREGATE_BY_ACTUAL_CLASS, + List.of(mockTermsBucket("dog", new Aggregations(List.of())), mockTermsBucket("cat", new Aggregations(List.of()))), + 100L + ), + mockCardinality("accuracy_" + MulticlassConfusionMatrix.STEP_1_CARDINALITY_OF_ACTUAL_CLASS, 1000L), + mockFilters( + "accuracy_" + MulticlassConfusionMatrix.STEP_2_AGGREGATE_BY_ACTUAL_CLASS, + List.of( + mockFiltersBucket( + "dog", + 30, + new Aggregations( + List.of( + mockFilters( + "accuracy_" + MulticlassConfusionMatrix.STEP_2_AGGREGATE_BY_PREDICTED_CLASS, + List.of( + mockFiltersBucket("cat", 10L), + mockFiltersBucket("dog", 20L), + mockFiltersBucket("_other_", 0L) + ) + ) + ) + ) + ), + mockFiltersBucket( + "cat", + 70, + new Aggregations( + List.of( + mockFilters( + "accuracy_" + MulticlassConfusionMatrix.STEP_2_AGGREGATE_BY_PREDICTED_CLASS, + List.of( + mockFiltersBucket("cat", 30L), + mockFiltersBucket("dog", 40L), + mockFiltersBucket("_other_", 0L) + ) + ) + ) + ) + ) + ) + ), + mockSingleValue(Accuracy.OVERALL_ACCURACY_AGG_NAME, 0.5) + ) + ); Accuracy accuracy = new Accuracy(); accuracy.process(aggs); @@ -91,40 +115,59 @@ public void testProcess() { Result result = accuracy.getResult().get(); assertThat(result.getMetricName(), equalTo(Accuracy.NAME.getPreferredName())); - assertThat( - result.getClasses(), - equalTo( - List.of( - new PerClassSingleValue("dog", 0.5), - new PerClassSingleValue("cat", 0.5)))); + assertThat(result.getClasses(), equalTo(List.of(new PerClassSingleValue("dog", 0.5), new PerClassSingleValue("cat", 0.5)))); assertThat(result.getOverallAccuracy(), equalTo(0.5)); } public void testProcess_GivenCardinalityTooHigh() { - Aggregations aggs = new Aggregations(List.of( - mockTerms( - "accuracy_" + MulticlassConfusionMatrix.STEP_1_AGGREGATE_BY_ACTUAL_CLASS, - List.of( - mockTermsBucket("dog", new Aggregations(List.of())), - mockTermsBucket("cat", new Aggregations(List.of()))), - 100L), - mockCardinality("accuracy_" + MulticlassConfusionMatrix.STEP_1_CARDINALITY_OF_ACTUAL_CLASS, 1001L), - mockFilters( - "accuracy_" + MulticlassConfusionMatrix.STEP_2_AGGREGATE_BY_ACTUAL_CLASS, - List.of( - mockFiltersBucket( - "dog", - 30, - new Aggregations(List.of(mockFilters( - "accuracy_" + MulticlassConfusionMatrix.STEP_2_AGGREGATE_BY_PREDICTED_CLASS, - List.of(mockFiltersBucket("cat", 10L), mockFiltersBucket("dog", 20L), mockFiltersBucket("_other_", 0L)))))), - mockFiltersBucket( - "cat", - 70, - new Aggregations(List.of(mockFilters( - "accuracy_" + MulticlassConfusionMatrix.STEP_2_AGGREGATE_BY_PREDICTED_CLASS, - List.of(mockFiltersBucket("cat", 30L), mockFiltersBucket("dog", 40L), mockFiltersBucket("_other_", 0L)))))))), - mockSingleValue(Accuracy.OVERALL_ACCURACY_AGG_NAME, 0.5))); + Aggregations aggs = new Aggregations( + List.of( + mockTerms( + "accuracy_" + MulticlassConfusionMatrix.STEP_1_AGGREGATE_BY_ACTUAL_CLASS, + List.of(mockTermsBucket("dog", new Aggregations(List.of())), mockTermsBucket("cat", new Aggregations(List.of()))), + 100L + ), + mockCardinality("accuracy_" + MulticlassConfusionMatrix.STEP_1_CARDINALITY_OF_ACTUAL_CLASS, 1001L), + mockFilters( + "accuracy_" + MulticlassConfusionMatrix.STEP_2_AGGREGATE_BY_ACTUAL_CLASS, + List.of( + mockFiltersBucket( + "dog", + 30, + new Aggregations( + List.of( + mockFilters( + "accuracy_" + MulticlassConfusionMatrix.STEP_2_AGGREGATE_BY_PREDICTED_CLASS, + List.of( + mockFiltersBucket("cat", 10L), + mockFiltersBucket("dog", 20L), + mockFiltersBucket("_other_", 0L) + ) + ) + ) + ) + ), + mockFiltersBucket( + "cat", + 70, + new Aggregations( + List.of( + mockFilters( + "accuracy_" + MulticlassConfusionMatrix.STEP_2_AGGREGATE_BY_PREDICTED_CLASS, + List.of( + mockFiltersBucket("cat", 30L), + mockFiltersBucket("dog", 40L), + mockFiltersBucket("_other_", 0L) + ) + ) + ) + ) + ) + ) + ), + mockSingleValue(Accuracy.OVERALL_ACCURACY_AGG_NAME, 0.5) + ) + ); Accuracy accuracy = new Accuracy(); accuracy.aggs(EVALUATION_PARAMETERS, EVALUATION_FIELDS); @@ -137,27 +180,47 @@ public void testComputePerClassAccuracy() { Accuracy.computePerClassAccuracy( new MulticlassConfusionMatrix.Result( List.of( - new MulticlassConfusionMatrix.ActualClass("A", 14, List.of( - new MulticlassConfusionMatrix.PredictedClass("A", 1), - new MulticlassConfusionMatrix.PredictedClass("B", 6), - new MulticlassConfusionMatrix.PredictedClass("C", 4) - ), 3L), - new MulticlassConfusionMatrix.ActualClass("B", 20, List.of( - new MulticlassConfusionMatrix.PredictedClass("A", 5), - new MulticlassConfusionMatrix.PredictedClass("B", 3), - new MulticlassConfusionMatrix.PredictedClass("C", 9) - ), 3L), - new MulticlassConfusionMatrix.ActualClass("C", 17, List.of( - new MulticlassConfusionMatrix.PredictedClass("A", 8), - new MulticlassConfusionMatrix.PredictedClass("B", 2), - new MulticlassConfusionMatrix.PredictedClass("C", 7) - ), 0L)), - 0)), + new MulticlassConfusionMatrix.ActualClass( + "A", + 14, + List.of( + new MulticlassConfusionMatrix.PredictedClass("A", 1), + new MulticlassConfusionMatrix.PredictedClass("B", 6), + new MulticlassConfusionMatrix.PredictedClass("C", 4) + ), + 3L + ), + new MulticlassConfusionMatrix.ActualClass( + "B", + 20, + List.of( + new MulticlassConfusionMatrix.PredictedClass("A", 5), + new MulticlassConfusionMatrix.PredictedClass("B", 3), + new MulticlassConfusionMatrix.PredictedClass("C", 9) + ), + 3L + ), + new MulticlassConfusionMatrix.ActualClass( + "C", + 17, + List.of( + new MulticlassConfusionMatrix.PredictedClass("A", 8), + new MulticlassConfusionMatrix.PredictedClass("B", 2), + new MulticlassConfusionMatrix.PredictedClass("C", 7) + ), + 0L + ) + ), + 0 + ) + ), equalTo( List.of( new PerClassSingleValue("A", 25.0 / 51), // 13 false positives, 13 false negatives - new PerClassSingleValue("B", 26.0 / 51), // 8 false positives, 17 false negatives - new PerClassSingleValue("C", 28.0 / 51))) // 13 false positives, 10 false negatives + new PerClassSingleValue("B", 26.0 / 51), // 8 false positives, 17 false negatives + new PerClassSingleValue("C", 28.0 / 51) + ) + ) // 13 false positives, 10 false negatives ); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/AucRocResultTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/AucRocResultTests.java index dfbda18e14aea..376b7c975113a 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/AucRocResultTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/AucRocResultTests.java @@ -21,11 +21,9 @@ public class AucRocResultTests extends AbstractWireSerializingTestCase { public static Result createRandom() { double score = randomDoubleBetween(0.0, 1.0, true); - List curve = - Stream - .generate(() -> new AucRocPoint(randomDouble(), randomDouble(), randomDouble())) - .limit(randomIntBetween(0, 20)) - .collect(Collectors.toList()); + List curve = Stream.generate(() -> new AucRocPoint(randomDouble(), randomDouble(), randomDouble())) + .limit(randomIntBetween(0, 20)) + .collect(Collectors.toList()); return new Result(score, curve); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/AucRocTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/AucRocTests.java index f79bd5fd6495a..9cbe668aa1924 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/AucRocTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/AucRocTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ml.dataframe.evaluation.classification; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/ClassificationTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/ClassificationTests.java index 2eebdd905a4dc..a414340393a3c 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/ClassificationTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/ClassificationTests.java @@ -10,14 +10,11 @@ import org.apache.lucene.search.join.ScoreMode; import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.action.search.SearchResponse; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.util.set.Sets; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.core.Tuple; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHits; @@ -26,6 +23,9 @@ import org.elasticsearch.search.aggregations.PipelineAggregationBuilder; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationFields; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationMetric; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationMetricResult; @@ -63,21 +63,23 @@ protected NamedXContentRegistry xContentRegistry() { } public static Classification createRandom() { - List metrics = - randomSubsetOf( - Arrays.asList( - AccuracyTests.createRandom(), - AucRocTests.createRandom(), - PrecisionTests.createRandom(), - RecallTests.createRandom(), - MulticlassConfusionMatrixTests.createRandom())); + List metrics = randomSubsetOf( + Arrays.asList( + AccuracyTests.createRandom(), + AucRocTests.createRandom(), + PrecisionTests.createRandom(), + RecallTests.createRandom(), + MulticlassConfusionMatrixTests.createRandom() + ) + ); boolean usesAucRoc = metrics.stream().map(EvaluationMetric::getName).anyMatch(n -> AucRoc.NAME.getPreferredName().equals(n)); return new Classification( randomAlphaOfLength(10), randomAlphaOfLength(10), // If AucRoc is to be calculated, the top_classes field is required (usesAucRoc || randomBoolean()) ? randomAlphaOfLength(10) : null, - metrics.isEmpty() ? null : metrics); + metrics.isEmpty() ? null : metrics + ); } @Override @@ -97,18 +99,21 @@ protected Writeable.Reader instanceReader() { public void testConstructor_GivenMissingField() { FakeClassificationMetric metric = new FakeClassificationMetric("fake"); - ElasticsearchStatusException e = - expectThrows( - ElasticsearchStatusException.class, - () -> new Classification("foo", null, null, Collections.singletonList(metric))); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> new Classification("foo", null, null, Collections.singletonList(metric)) + ); assertThat( e.getMessage(), - is(equalTo("[classification] must define [predicted_field] as required by the following metrics [fake]"))); + is(equalTo("[classification] must define [predicted_field] as required by the following metrics [fake]")) + ); } public void testConstructor_GivenEmptyMetrics() { - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> new Classification("foo", "bar", "results", Collections.emptyList())); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> new Classification("foo", "bar", "results", Collections.emptyList()) + ); assertThat(e.getMessage(), equalTo("[classification] must have one or more metrics")); } @@ -132,17 +137,17 @@ public void testGetFields() { } public void testBuildSearch_WithDefaultNonRequiredNestedFields() { - QueryBuilder userProvidedQuery = - QueryBuilders.boolQuery() - .filter(QueryBuilders.termQuery("field_A", "some-value")) - .filter(QueryBuilders.termQuery("field_B", "some-other-value")); - QueryBuilder expectedSearchQuery = - QueryBuilders.boolQuery() - .filter(QueryBuilders.existsQuery("act")) - .filter(QueryBuilders.existsQuery("pred")) - .filter(QueryBuilders.boolQuery() + QueryBuilder userProvidedQuery = QueryBuilders.boolQuery() + .filter(QueryBuilders.termQuery("field_A", "some-value")) + .filter(QueryBuilders.termQuery("field_B", "some-other-value")); + QueryBuilder expectedSearchQuery = QueryBuilders.boolQuery() + .filter(QueryBuilders.existsQuery("act")) + .filter(QueryBuilders.existsQuery("pred")) + .filter( + QueryBuilders.boolQuery() .filter(QueryBuilders.termQuery("field_A", "some-value")) - .filter(QueryBuilders.termQuery("field_B", "some-other-value"))); + .filter(QueryBuilders.termQuery("field_B", "some-other-value")) + ); Classification evaluation = new Classification("act", "pred", null, Arrays.asList(new MulticlassConfusionMatrix())); @@ -152,17 +157,17 @@ public void testBuildSearch_WithDefaultNonRequiredNestedFields() { } public void testBuildSearch_WithExplicitNonRequiredNestedFields() { - QueryBuilder userProvidedQuery = - QueryBuilders.boolQuery() - .filter(QueryBuilders.termQuery("field_A", "some-value")) - .filter(QueryBuilders.termQuery("field_B", "some-other-value")); - QueryBuilder expectedSearchQuery = - QueryBuilders.boolQuery() - .filter(QueryBuilders.existsQuery("act")) - .filter(QueryBuilders.existsQuery("pred")) - .filter(QueryBuilders.boolQuery() + QueryBuilder userProvidedQuery = QueryBuilders.boolQuery() + .filter(QueryBuilders.termQuery("field_A", "some-value")) + .filter(QueryBuilders.termQuery("field_B", "some-other-value")); + QueryBuilder expectedSearchQuery = QueryBuilders.boolQuery() + .filter(QueryBuilders.existsQuery("act")) + .filter(QueryBuilders.existsQuery("pred")) + .filter( + QueryBuilders.boolQuery() .filter(QueryBuilders.termQuery("field_A", "some-value")) - .filter(QueryBuilders.termQuery("field_B", "some-other-value"))); + .filter(QueryBuilders.termQuery("field_B", "some-other-value")) + ); Classification evaluation = new Classification("act", "pred", "results", Arrays.asList(new MulticlassConfusionMatrix())); @@ -172,23 +177,24 @@ public void testBuildSearch_WithExplicitNonRequiredNestedFields() { } public void testBuildSearch_WithDefaultRequiredNestedFields() { - QueryBuilder userProvidedQuery = - QueryBuilders.boolQuery() - .filter(QueryBuilders.termQuery("field_A", "some-value")) - .filter(QueryBuilders.termQuery("field_B", "some-other-value")); - QueryBuilder expectedSearchQuery = - QueryBuilders.boolQuery() - .filter(QueryBuilders.existsQuery("act")) - .filter( - QueryBuilders.nestedQuery("ml.top_classes", QueryBuilders.existsQuery("ml.top_classes.class_name"), ScoreMode.None) - .ignoreUnmapped(true)) - .filter( - QueryBuilders.nestedQuery( - "ml.top_classes", QueryBuilders.existsQuery("ml.top_classes.class_probability"), ScoreMode.None) - .ignoreUnmapped(true)) - .filter(QueryBuilders.boolQuery() + QueryBuilder userProvidedQuery = QueryBuilders.boolQuery() + .filter(QueryBuilders.termQuery("field_A", "some-value")) + .filter(QueryBuilders.termQuery("field_B", "some-other-value")); + QueryBuilder expectedSearchQuery = QueryBuilders.boolQuery() + .filter(QueryBuilders.existsQuery("act")) + .filter( + QueryBuilders.nestedQuery("ml.top_classes", QueryBuilders.existsQuery("ml.top_classes.class_name"), ScoreMode.None) + .ignoreUnmapped(true) + ) + .filter( + QueryBuilders.nestedQuery("ml.top_classes", QueryBuilders.existsQuery("ml.top_classes.class_probability"), ScoreMode.None) + .ignoreUnmapped(true) + ) + .filter( + QueryBuilders.boolQuery() .filter(QueryBuilders.termQuery("field_A", "some-value")) - .filter(QueryBuilders.termQuery("field_B", "some-other-value"))); + .filter(QueryBuilders.termQuery("field_B", "some-other-value")) + ); Classification evaluation = new Classification("act", "pred", null, Arrays.asList(new AucRoc(false, "some-value"))); @@ -198,22 +204,23 @@ public void testBuildSearch_WithDefaultRequiredNestedFields() { } public void testBuildSearch_WithExplicitRequiredNestedFields() { - QueryBuilder userProvidedQuery = - QueryBuilders.boolQuery() - .filter(QueryBuilders.termQuery("field_A", "some-value")) - .filter(QueryBuilders.termQuery("field_B", "some-other-value")); - QueryBuilder expectedSearchQuery = - QueryBuilders.boolQuery() - .filter(QueryBuilders.existsQuery("act")) - .filter( - QueryBuilders.nestedQuery("results", QueryBuilders.existsQuery("results.class_name"), ScoreMode.None) - .ignoreUnmapped(true)) - .filter( - QueryBuilders.nestedQuery("results", QueryBuilders.existsQuery("results.class_probability"), ScoreMode.None) - .ignoreUnmapped(true)) - .filter(QueryBuilders.boolQuery() + QueryBuilder userProvidedQuery = QueryBuilders.boolQuery() + .filter(QueryBuilders.termQuery("field_A", "some-value")) + .filter(QueryBuilders.termQuery("field_B", "some-other-value")); + QueryBuilder expectedSearchQuery = QueryBuilders.boolQuery() + .filter(QueryBuilders.existsQuery("act")) + .filter( + QueryBuilders.nestedQuery("results", QueryBuilders.existsQuery("results.class_name"), ScoreMode.None).ignoreUnmapped(true) + ) + .filter( + QueryBuilders.nestedQuery("results", QueryBuilders.existsQuery("results.class_probability"), ScoreMode.None) + .ignoreUnmapped(true) + ) + .filter( + QueryBuilders.boolQuery() .filter(QueryBuilders.termQuery("field_A", "some-value")) - .filter(QueryBuilders.termQuery("field_B", "some-other-value"))); + .filter(QueryBuilders.termQuery("field_B", "some-other-value")) + ); Classification evaluation = new Classification("act", "pred", "results", Arrays.asList(new AucRoc(false, "some-value"))); @@ -322,8 +329,10 @@ public Set getRequiredFields() { } @Override - public Tuple, List> aggs(EvaluationParameters parameters, - EvaluationFields fields) { + public Tuple, List> aggs( + EvaluationParameters parameters, + EvaluationFields fields + ) { return Tuple.tuple(List.of(), List.of()); } @@ -350,7 +359,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) { } @Override - public void writeTo(StreamOutput out) { - } + public void writeTo(StreamOutput out) {} } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/MulticlassConfusionMatrixResultTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/MulticlassConfusionMatrixResultTests.java index b2842fe802288..ea6febafc8908 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/MulticlassConfusionMatrixResultTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/MulticlassConfusionMatrixResultTests.java @@ -8,8 +8,8 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.classification.MulticlassConfusionMatrix.ActualClass; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.classification.MulticlassConfusionMatrix.PredictedClass; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.classification.MulticlassConfusionMatrix.Result; @@ -77,53 +77,57 @@ public void testConstructor_ValidationFailures() { assertThat(e.getMessage(), equalTo("[other_actual_class_count] must be >= 0, was: -1")); } { - IllegalArgumentException e = - expectThrows( - IllegalArgumentException.class, - () -> new Result(Collections.singletonList(new ActualClass(null, 0, Collections.emptyList(), 0)), 0)); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> new Result(Collections.singletonList(new ActualClass(null, 0, Collections.emptyList(), 0)), 0) + ); assertThat(e.getMessage(), equalTo("[actual_class] must not be null.")); } { - ElasticsearchException e = - expectThrows( - ElasticsearchException.class, - () -> new Result(Collections.singletonList(new ActualClass("actual_class", -1, Collections.emptyList(), 0)), 0)); + ElasticsearchException e = expectThrows( + ElasticsearchException.class, + () -> new Result(Collections.singletonList(new ActualClass("actual_class", -1, Collections.emptyList(), 0)), 0) + ); assertThat(e.status().getStatus(), equalTo(500)); assertThat(e.getMessage(), equalTo("[actual_class_doc_count] must be >= 0, was: -1")); } { - IllegalArgumentException e = - expectThrows( - IllegalArgumentException.class, - () -> new Result(Collections.singletonList(new ActualClass("actual_class", 0, null, 0)), 0)); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> new Result(Collections.singletonList(new ActualClass("actual_class", 0, null, 0)), 0) + ); assertThat(e.getMessage(), equalTo("[predicted_classes] must not be null.")); } { - ElasticsearchException e = - expectThrows( - ElasticsearchException.class, - () -> new Result(Collections.singletonList(new ActualClass("actual_class", 0, Collections.emptyList(), -1)), 0)); + ElasticsearchException e = expectThrows( + ElasticsearchException.class, + () -> new Result(Collections.singletonList(new ActualClass("actual_class", 0, Collections.emptyList(), -1)), 0) + ); assertThat(e.status().getStatus(), equalTo(500)); assertThat(e.getMessage(), equalTo("[other_predicted_class_doc_count] must be >= 0, was: -1")); } { - IllegalArgumentException e = - expectThrows( - IllegalArgumentException.class, - () -> new Result( - Collections.singletonList( - new ActualClass("actual_class", 0, Collections.singletonList(new PredictedClass(null, 0)), 0)), - 0)); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> new Result( + Collections.singletonList( + new ActualClass("actual_class", 0, Collections.singletonList(new PredictedClass(null, 0)), 0) + ), + 0 + ) + ); assertThat(e.getMessage(), equalTo("[predicted_class] must not be null.")); } { - ElasticsearchException e = - expectThrows( - ElasticsearchException.class, - () -> new Result( - Collections.singletonList( - new ActualClass("actual_class", 0, Collections.singletonList(new PredictedClass("predicted_class", -1)), 0)), - 0)); + ElasticsearchException e = expectThrows( + ElasticsearchException.class, + () -> new Result( + Collections.singletonList( + new ActualClass("actual_class", 0, Collections.singletonList(new PredictedClass("predicted_class", -1)), 0) + ), + 0 + ) + ); assertThat(e.status().getStatus(), equalTo(500)); assertThat(e.getMessage(), equalTo("[count] must be >= 0, was: -1")); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/MulticlassConfusionMatrixTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/MulticlassConfusionMatrixTests.java index e148ffc9e2653..b6e7b63699803 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/MulticlassConfusionMatrixTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/MulticlassConfusionMatrixTests.java @@ -7,13 +7,13 @@ package org.elasticsearch.xpack.core.ml.dataframe.evaluation.classification; import org.elasticsearch.ElasticsearchStatusException; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.core.Tuple; import org.elasticsearch.search.aggregations.AggregationBuilder; import org.elasticsearch.search.aggregations.Aggregations; import org.elasticsearch.search.aggregations.PipelineAggregationBuilder; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationFields; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationParameters; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.classification.MulticlassConfusionMatrix.ActualClass; @@ -66,55 +66,83 @@ public static MulticlassConfusionMatrix createRandom() { public void testConstructor_SizeValidationFailures() { { - ElasticsearchStatusException e = - expectThrows(ElasticsearchStatusException.class, () -> new MulticlassConfusionMatrix(-1, null)); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> new MulticlassConfusionMatrix(-1, null) + ); assertThat(e.getMessage(), equalTo("[size] must be an integer in [1, 1000]")); } { - ElasticsearchStatusException e = - expectThrows(ElasticsearchStatusException.class, () -> new MulticlassConfusionMatrix(0, null)); + ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, () -> new MulticlassConfusionMatrix(0, null)); assertThat(e.getMessage(), equalTo("[size] must be an integer in [1, 1000]")); } { - ElasticsearchStatusException e = - expectThrows(ElasticsearchStatusException.class, () -> new MulticlassConfusionMatrix(1001, null)); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> new MulticlassConfusionMatrix(1001, null) + ); assertThat(e.getMessage(), equalTo("[size] must be an integer in [1, 1000]")); } } public void testAggs() { MulticlassConfusionMatrix confusionMatrix = new MulticlassConfusionMatrix(); - Tuple, List> aggs = - confusionMatrix.aggs(EVALUATION_PARAMETERS, EVALUATION_FIELDS); + Tuple, List> aggs = confusionMatrix.aggs( + EVALUATION_PARAMETERS, + EVALUATION_FIELDS + ); assertThat(aggs, isTuple(not(empty()), empty())); assertThat(confusionMatrix.getResult(), isEmpty()); } public void testProcess() { - Aggregations aggs = new Aggregations(List.of( - mockTerms( - MulticlassConfusionMatrix.STEP_1_AGGREGATE_BY_ACTUAL_CLASS, - List.of( - mockTermsBucket("dog", new Aggregations(List.of())), - mockTermsBucket("cat", new Aggregations(List.of()))), - 0L), - mockCardinality(MulticlassConfusionMatrix.STEP_1_CARDINALITY_OF_ACTUAL_CLASS, 2L), - mockFilters( - MulticlassConfusionMatrix.STEP_2_AGGREGATE_BY_ACTUAL_CLASS, - List.of( - mockFiltersBucket( - "dog", - 30, - new Aggregations(List.of(mockFilters( - MulticlassConfusionMatrix.STEP_2_AGGREGATE_BY_PREDICTED_CLASS, - List.of(mockFiltersBucket("cat", 10L), mockFiltersBucket("dog", 20L), mockFiltersBucket("_other_", 0L)))))), - mockFiltersBucket( - "cat", - 70, - new Aggregations(List.of(mockFilters( - MulticlassConfusionMatrix.STEP_2_AGGREGATE_BY_PREDICTED_CLASS, - List.of(mockFiltersBucket("cat", 30L), mockFiltersBucket("dog", 40L), mockFiltersBucket("_other_", 0L)))))))) - )); + Aggregations aggs = new Aggregations( + List.of( + mockTerms( + MulticlassConfusionMatrix.STEP_1_AGGREGATE_BY_ACTUAL_CLASS, + List.of(mockTermsBucket("dog", new Aggregations(List.of())), mockTermsBucket("cat", new Aggregations(List.of()))), + 0L + ), + mockCardinality(MulticlassConfusionMatrix.STEP_1_CARDINALITY_OF_ACTUAL_CLASS, 2L), + mockFilters( + MulticlassConfusionMatrix.STEP_2_AGGREGATE_BY_ACTUAL_CLASS, + List.of( + mockFiltersBucket( + "dog", + 30, + new Aggregations( + List.of( + mockFilters( + MulticlassConfusionMatrix.STEP_2_AGGREGATE_BY_PREDICTED_CLASS, + List.of( + mockFiltersBucket("cat", 10L), + mockFiltersBucket("dog", 20L), + mockFiltersBucket("_other_", 0L) + ) + ) + ) + ) + ), + mockFiltersBucket( + "cat", + 70, + new Aggregations( + List.of( + mockFilters( + MulticlassConfusionMatrix.STEP_2_AGGREGATE_BY_PREDICTED_CLASS, + List.of( + mockFiltersBucket("cat", 30L), + mockFiltersBucket("dog", 40L), + mockFiltersBucket("_other_", 0L) + ) + ) + ) + ) + ) + ) + ) + ) + ); MulticlassConfusionMatrix confusionMatrix = new MulticlassConfusionMatrix(2, null); confusionMatrix.process(aggs); @@ -127,35 +155,61 @@ public void testProcess() { equalTo( List.of( new ActualClass("dog", 30, List.of(new PredictedClass("cat", 10L), new PredictedClass("dog", 20L)), 0), - new ActualClass("cat", 70, List.of(new PredictedClass("cat", 30L), new PredictedClass("dog", 40L)), 0)))); + new ActualClass("cat", 70, List.of(new PredictedClass("cat", 30L), new PredictedClass("dog", 40L)), 0) + ) + ) + ); assertThat(result.getOtherActualClassCount(), equalTo(0L)); } public void testProcess_OtherClassesCountGreaterThanZero() { - Aggregations aggs = new Aggregations(List.of( - mockTerms( - MulticlassConfusionMatrix.STEP_1_AGGREGATE_BY_ACTUAL_CLASS, - List.of( - mockTermsBucket("dog", new Aggregations(List.of())), - mockTermsBucket("cat", new Aggregations(List.of()))), - 100L), - mockCardinality(MulticlassConfusionMatrix.STEP_1_CARDINALITY_OF_ACTUAL_CLASS, 5L), - mockFilters( - MulticlassConfusionMatrix.STEP_2_AGGREGATE_BY_ACTUAL_CLASS, - List.of( - mockFiltersBucket( - "dog", - 30, - new Aggregations(List.of(mockFilters( - MulticlassConfusionMatrix.STEP_2_AGGREGATE_BY_PREDICTED_CLASS, - List.of(mockFiltersBucket("cat", 10L), mockFiltersBucket("dog", 20L), mockFiltersBucket("_other_", 0L)))))), - mockFiltersBucket( - "cat", - 85, - new Aggregations(List.of(mockFilters( - MulticlassConfusionMatrix.STEP_2_AGGREGATE_BY_PREDICTED_CLASS, - List.of(mockFiltersBucket("cat", 30L), mockFiltersBucket("dog", 40L), mockFiltersBucket("_other_", 15L)))))))) - )); + Aggregations aggs = new Aggregations( + List.of( + mockTerms( + MulticlassConfusionMatrix.STEP_1_AGGREGATE_BY_ACTUAL_CLASS, + List.of(mockTermsBucket("dog", new Aggregations(List.of())), mockTermsBucket("cat", new Aggregations(List.of()))), + 100L + ), + mockCardinality(MulticlassConfusionMatrix.STEP_1_CARDINALITY_OF_ACTUAL_CLASS, 5L), + mockFilters( + MulticlassConfusionMatrix.STEP_2_AGGREGATE_BY_ACTUAL_CLASS, + List.of( + mockFiltersBucket( + "dog", + 30, + new Aggregations( + List.of( + mockFilters( + MulticlassConfusionMatrix.STEP_2_AGGREGATE_BY_PREDICTED_CLASS, + List.of( + mockFiltersBucket("cat", 10L), + mockFiltersBucket("dog", 20L), + mockFiltersBucket("_other_", 0L) + ) + ) + ) + ) + ), + mockFiltersBucket( + "cat", + 85, + new Aggregations( + List.of( + mockFilters( + MulticlassConfusionMatrix.STEP_2_AGGREGATE_BY_PREDICTED_CLASS, + List.of( + mockFiltersBucket("cat", 30L), + mockFiltersBucket("dog", 40L), + mockFiltersBucket("_other_", 15L) + ) + ) + ) + ) + ) + ) + ) + ) + ); MulticlassConfusionMatrix confusionMatrix = new MulticlassConfusionMatrix(2, null); confusionMatrix.process(aggs); @@ -168,76 +222,119 @@ public void testProcess_OtherClassesCountGreaterThanZero() { equalTo( List.of( new ActualClass("dog", 30, List.of(new PredictedClass("cat", 10L), new PredictedClass("dog", 20L)), 0), - new ActualClass("cat", 85, List.of(new PredictedClass("cat", 30L), new PredictedClass("dog", 40L)), 15)))); + new ActualClass("cat", 85, List.of(new PredictedClass("cat", 30L), new PredictedClass("dog", 40L)), 15) + ) + ) + ); assertThat(result.getOtherActualClassCount(), equalTo(3L)); } public void testProcess_MoreThanTwoStepsNeeded() { - Aggregations aggsStep1 = new Aggregations(List.of( - mockTerms( - MulticlassConfusionMatrix.STEP_1_AGGREGATE_BY_ACTUAL_CLASS, - List.of( - mockTermsBucket("ant", new Aggregations(List.of())), - mockTermsBucket("cat", new Aggregations(List.of())), - mockTermsBucket("dog", new Aggregations(List.of())), - mockTermsBucket("fox", new Aggregations(List.of()))), - 0L), - mockCardinality(MulticlassConfusionMatrix.STEP_1_CARDINALITY_OF_ACTUAL_CLASS, 2L) - )); - Aggregations aggsStep2 = new Aggregations(List.of( - mockFilters( - MulticlassConfusionMatrix.STEP_2_AGGREGATE_BY_ACTUAL_CLASS, - List.of( - mockFiltersBucket( - "ant", - 46, - new Aggregations(List.of(mockFilters( - MulticlassConfusionMatrix.STEP_2_AGGREGATE_BY_PREDICTED_CLASS, - List.of( - mockFiltersBucket("ant", 10L), - mockFiltersBucket("cat", 11L), - mockFiltersBucket("dog", 12L), - mockFiltersBucket("fox", 13L), - mockFiltersBucket("_other_", 0L)))))), - mockFiltersBucket( - "cat", - 86, - new Aggregations(List.of(mockFilters( - MulticlassConfusionMatrix.STEP_2_AGGREGATE_BY_PREDICTED_CLASS, - List.of( - mockFiltersBucket("ant", 20L), - mockFiltersBucket("cat", 21L), - mockFiltersBucket("dog", 22L), - mockFiltersBucket("fox", 23L), - mockFiltersBucket("_other_", 0L)))))))) - )); - Aggregations aggsStep3 = new Aggregations(List.of( - mockFilters( - MulticlassConfusionMatrix.STEP_2_AGGREGATE_BY_ACTUAL_CLASS, - List.of( - mockFiltersBucket( - "dog", - 126, - new Aggregations(List.of(mockFilters( - MulticlassConfusionMatrix.STEP_2_AGGREGATE_BY_PREDICTED_CLASS, - List.of( - mockFiltersBucket("ant", 30L), - mockFiltersBucket("cat", 31L), - mockFiltersBucket("dog", 32L), - mockFiltersBucket("fox", 33L), - mockFiltersBucket("_other_", 0L)))))), - mockFiltersBucket( - "fox", - 166, - new Aggregations(List.of(mockFilters( - MulticlassConfusionMatrix.STEP_2_AGGREGATE_BY_PREDICTED_CLASS, - List.of( - mockFiltersBucket("ant", 40L), - mockFiltersBucket("cat", 41L), - mockFiltersBucket("dog", 42L), - mockFiltersBucket("fox", 43L), - mockFiltersBucket("_other_", 0L)))))))) - )); + Aggregations aggsStep1 = new Aggregations( + List.of( + mockTerms( + MulticlassConfusionMatrix.STEP_1_AGGREGATE_BY_ACTUAL_CLASS, + List.of( + mockTermsBucket("ant", new Aggregations(List.of())), + mockTermsBucket("cat", new Aggregations(List.of())), + mockTermsBucket("dog", new Aggregations(List.of())), + mockTermsBucket("fox", new Aggregations(List.of())) + ), + 0L + ), + mockCardinality(MulticlassConfusionMatrix.STEP_1_CARDINALITY_OF_ACTUAL_CLASS, 2L) + ) + ); + Aggregations aggsStep2 = new Aggregations( + List.of( + mockFilters( + MulticlassConfusionMatrix.STEP_2_AGGREGATE_BY_ACTUAL_CLASS, + List.of( + mockFiltersBucket( + "ant", + 46, + new Aggregations( + List.of( + mockFilters( + MulticlassConfusionMatrix.STEP_2_AGGREGATE_BY_PREDICTED_CLASS, + List.of( + mockFiltersBucket("ant", 10L), + mockFiltersBucket("cat", 11L), + mockFiltersBucket("dog", 12L), + mockFiltersBucket("fox", 13L), + mockFiltersBucket("_other_", 0L) + ) + ) + ) + ) + ), + mockFiltersBucket( + "cat", + 86, + new Aggregations( + List.of( + mockFilters( + MulticlassConfusionMatrix.STEP_2_AGGREGATE_BY_PREDICTED_CLASS, + List.of( + mockFiltersBucket("ant", 20L), + mockFiltersBucket("cat", 21L), + mockFiltersBucket("dog", 22L), + mockFiltersBucket("fox", 23L), + mockFiltersBucket("_other_", 0L) + ) + ) + ) + ) + ) + ) + ) + ) + ); + Aggregations aggsStep3 = new Aggregations( + List.of( + mockFilters( + MulticlassConfusionMatrix.STEP_2_AGGREGATE_BY_ACTUAL_CLASS, + List.of( + mockFiltersBucket( + "dog", + 126, + new Aggregations( + List.of( + mockFilters( + MulticlassConfusionMatrix.STEP_2_AGGREGATE_BY_PREDICTED_CLASS, + List.of( + mockFiltersBucket("ant", 30L), + mockFiltersBucket("cat", 31L), + mockFiltersBucket("dog", 32L), + mockFiltersBucket("fox", 33L), + mockFiltersBucket("_other_", 0L) + ) + ) + ) + ) + ), + mockFiltersBucket( + "fox", + 166, + new Aggregations( + List.of( + mockFilters( + MulticlassConfusionMatrix.STEP_2_AGGREGATE_BY_PREDICTED_CLASS, + List.of( + mockFiltersBucket("ant", 40L), + mockFiltersBucket("cat", 41L), + mockFiltersBucket("dog", 42L), + mockFiltersBucket("fox", 43L), + mockFiltersBucket("_other_", 0L) + ) + ) + ) + ) + ) + ) + ) + ) + ); MulticlassConfusionMatrix confusionMatrix = new MulticlassConfusionMatrix(4, null); confusionMatrix.process(aggsStep1); @@ -251,26 +348,53 @@ public void testProcess_MoreThanTwoStepsNeeded() { result.getConfusionMatrix(), equalTo( List.of( - new ActualClass("ant", 46, List.of( - new PredictedClass("ant", 10L), - new PredictedClass("cat", 11L), - new PredictedClass("dog", 12L), - new PredictedClass("fox", 13L)), 0), - new ActualClass("cat", 86, List.of( - new PredictedClass("ant", 20L), - new PredictedClass("cat", 21L), - new PredictedClass("dog", 22L), - new PredictedClass("fox", 23L)), 0), - new ActualClass("dog", 126, List.of( - new PredictedClass("ant", 30L), - new PredictedClass("cat", 31L), - new PredictedClass("dog", 32L), - new PredictedClass("fox", 33L)), 0), - new ActualClass("fox", 166, List.of( - new PredictedClass("ant", 40L), - new PredictedClass("cat", 41L), - new PredictedClass("dog", 42L), - new PredictedClass("fox", 43L)), 0)))); + new ActualClass( + "ant", + 46, + List.of( + new PredictedClass("ant", 10L), + new PredictedClass("cat", 11L), + new PredictedClass("dog", 12L), + new PredictedClass("fox", 13L) + ), + 0 + ), + new ActualClass( + "cat", + 86, + List.of( + new PredictedClass("ant", 20L), + new PredictedClass("cat", 21L), + new PredictedClass("dog", 22L), + new PredictedClass("fox", 23L) + ), + 0 + ), + new ActualClass( + "dog", + 126, + List.of( + new PredictedClass("ant", 30L), + new PredictedClass("cat", 31L), + new PredictedClass("dog", 32L), + new PredictedClass("fox", 33L) + ), + 0 + ), + new ActualClass( + "fox", + 166, + List.of( + new PredictedClass("ant", 40L), + new PredictedClass("cat", 41L), + new PredictedClass("dog", 42L), + new PredictedClass("fox", 43L) + ), + 0 + ) + ) + ) + ); assertThat(result.getOtherActualClassCount(), equalTo(0L)); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/PerClassSingleValueTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/PerClassSingleValueTests.java index 9d3ec91f9e81d..3d460841d4ec5 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/PerClassSingleValueTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/PerClassSingleValueTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ml.dataframe.evaluation.classification; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/PrecisionTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/PrecisionTests.java index dbc51ad679ff3..cb7f2f7e3235d 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/PrecisionTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/PrecisionTests.java @@ -8,9 +8,9 @@ import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.aggregations.Aggregations; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationFields; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationParameters; @@ -58,12 +58,14 @@ public static Precision createRandom() { } public void testProcess() { - Aggregations aggs = new Aggregations(Arrays.asList( - mockTerms(Precision.ACTUAL_CLASSES_NAMES_AGG_NAME), - mockFilters(Precision.BY_PREDICTED_CLASS_AGG_NAME), - mockSingleValue(Precision.AVG_PRECISION_AGG_NAME, 0.8123), - mockSingleValue("some_other_single_metric_agg", 0.2377) - )); + Aggregations aggs = new Aggregations( + Arrays.asList( + mockTerms(Precision.ACTUAL_CLASSES_NAMES_AGG_NAME), + mockFilters(Precision.BY_PREDICTED_CLASS_AGG_NAME), + mockSingleValue(Precision.AVG_PRECISION_AGG_NAME, 0.8123), + mockSingleValue("some_other_single_metric_agg", 0.2377) + ) + ); Precision precision = new Precision(); precision.process(aggs); @@ -74,19 +76,20 @@ public void testProcess() { public void testProcess_GivenMissingAgg() { { - Aggregations aggs = new Aggregations(Arrays.asList( - mockFilters(Precision.BY_PREDICTED_CLASS_AGG_NAME), - mockSingleValue("some_other_single_metric_agg", 0.2377) - )); + Aggregations aggs = new Aggregations( + Arrays.asList(mockFilters(Precision.BY_PREDICTED_CLASS_AGG_NAME), mockSingleValue("some_other_single_metric_agg", 0.2377)) + ); Precision precision = new Precision(); precision.process(aggs); assertThat(precision.getResult(), isEmpty()); } { - Aggregations aggs = new Aggregations(Arrays.asList( - mockSingleValue(Precision.AVG_PRECISION_AGG_NAME, 0.8123), - mockSingleValue("some_other_single_metric_agg", 0.2377) - )); + Aggregations aggs = new Aggregations( + Arrays.asList( + mockSingleValue(Precision.AVG_PRECISION_AGG_NAME, 0.8123), + mockSingleValue("some_other_single_metric_agg", 0.2377) + ) + ); Precision precision = new Precision(); precision.process(aggs); assertThat(precision.getResult(), isEmpty()); @@ -95,19 +98,20 @@ public void testProcess_GivenMissingAgg() { public void testProcess_GivenAggOfWrongType() { { - Aggregations aggs = new Aggregations(Arrays.asList( - mockFilters(Precision.BY_PREDICTED_CLASS_AGG_NAME), - mockFilters(Precision.AVG_PRECISION_AGG_NAME) - )); + Aggregations aggs = new Aggregations( + Arrays.asList(mockFilters(Precision.BY_PREDICTED_CLASS_AGG_NAME), mockFilters(Precision.AVG_PRECISION_AGG_NAME)) + ); Precision precision = new Precision(); precision.process(aggs); assertThat(precision.getResult(), isEmpty()); } { - Aggregations aggs = new Aggregations(Arrays.asList( - mockSingleValue(Precision.BY_PREDICTED_CLASS_AGG_NAME, 1.0), - mockSingleValue(Precision.AVG_PRECISION_AGG_NAME, 0.8123) - )); + Aggregations aggs = new Aggregations( + Arrays.asList( + mockSingleValue(Precision.BY_PREDICTED_CLASS_AGG_NAME, 1.0), + mockSingleValue(Precision.AVG_PRECISION_AGG_NAME, 0.8123) + ) + ); Precision precision = new Precision(); precision.process(aggs); assertThat(precision.getResult(), isEmpty()); @@ -115,8 +119,9 @@ public void testProcess_GivenAggOfWrongType() { } public void testProcess_GivenCardinalityTooHigh() { - Aggregations aggs = - new Aggregations(Collections.singletonList(mockTerms(Precision.ACTUAL_CLASSES_NAMES_AGG_NAME, Collections.emptyList(), 1))); + Aggregations aggs = new Aggregations( + Collections.singletonList(mockTerms(Precision.ACTUAL_CLASSES_NAMES_AGG_NAME, Collections.emptyList(), 1)) + ); Precision precision = new Precision(); precision.aggs(EVALUATION_PARAMETERS, EVALUATION_FIELDS); ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, () -> precision.process(aggs)); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/RecallTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/RecallTests.java index e1ab0a15264a5..1f7411d0e46bb 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/RecallTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/classification/RecallTests.java @@ -8,9 +8,9 @@ import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.aggregations.Aggregations; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationFields; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationParameters; @@ -57,11 +57,13 @@ public static Recall createRandom() { } public void testProcess() { - Aggregations aggs = new Aggregations(Arrays.asList( - mockTerms(Recall.BY_ACTUAL_CLASS_AGG_NAME), - mockSingleValue(Recall.AVG_RECALL_AGG_NAME, 0.8123), - mockSingleValue("some_other_single_metric_agg", 0.2377) - )); + Aggregations aggs = new Aggregations( + Arrays.asList( + mockTerms(Recall.BY_ACTUAL_CLASS_AGG_NAME), + mockSingleValue(Recall.AVG_RECALL_AGG_NAME, 0.8123), + mockSingleValue("some_other_single_metric_agg", 0.2377) + ) + ); Recall recall = new Recall(); recall.process(aggs); @@ -72,19 +74,17 @@ public void testProcess() { public void testProcess_GivenMissingAgg() { { - Aggregations aggs = new Aggregations(Arrays.asList( - mockTerms(Recall.BY_ACTUAL_CLASS_AGG_NAME), - mockSingleValue("some_other_single_metric_agg", 0.2377) - )); + Aggregations aggs = new Aggregations( + Arrays.asList(mockTerms(Recall.BY_ACTUAL_CLASS_AGG_NAME), mockSingleValue("some_other_single_metric_agg", 0.2377)) + ); Recall recall = new Recall(); recall.process(aggs); assertThat(recall.getResult(), isEmpty()); } { - Aggregations aggs = new Aggregations(Arrays.asList( - mockSingleValue(Recall.AVG_RECALL_AGG_NAME, 0.8123), - mockSingleValue("some_other_single_metric_agg", 0.2377) - )); + Aggregations aggs = new Aggregations( + Arrays.asList(mockSingleValue(Recall.AVG_RECALL_AGG_NAME, 0.8123), mockSingleValue("some_other_single_metric_agg", 0.2377)) + ); Recall recall = new Recall(); recall.process(aggs); assertThat(recall.getResult(), isEmpty()); @@ -93,19 +93,17 @@ public void testProcess_GivenMissingAgg() { public void testProcess_GivenAggOfWrongType() { { - Aggregations aggs = new Aggregations(Arrays.asList( - mockTerms(Recall.BY_ACTUAL_CLASS_AGG_NAME), - mockTerms(Recall.AVG_RECALL_AGG_NAME) - )); + Aggregations aggs = new Aggregations( + Arrays.asList(mockTerms(Recall.BY_ACTUAL_CLASS_AGG_NAME), mockTerms(Recall.AVG_RECALL_AGG_NAME)) + ); Recall recall = new Recall(); recall.process(aggs); assertThat(recall.getResult(), isEmpty()); } { - Aggregations aggs = new Aggregations(Arrays.asList( - mockSingleValue(Recall.BY_ACTUAL_CLASS_AGG_NAME, 1.0), - mockSingleValue(Recall.AVG_RECALL_AGG_NAME, 0.8123) - )); + Aggregations aggs = new Aggregations( + Arrays.asList(mockSingleValue(Recall.BY_ACTUAL_CLASS_AGG_NAME, 1.0), mockSingleValue(Recall.AVG_RECALL_AGG_NAME, 0.8123)) + ); Recall recall = new Recall(); recall.process(aggs); assertThat(recall.getResult(), isEmpty()); @@ -113,9 +111,12 @@ public void testProcess_GivenAggOfWrongType() { } public void testProcess_GivenCardinalityTooHigh() { - Aggregations aggs = new Aggregations(Arrays.asList( - mockTerms(Recall.BY_ACTUAL_CLASS_AGG_NAME, Collections.emptyList(), 1), - mockSingleValue(Recall.AVG_RECALL_AGG_NAME, 0.8123))); + Aggregations aggs = new Aggregations( + Arrays.asList( + mockTerms(Recall.BY_ACTUAL_CLASS_AGG_NAME, Collections.emptyList(), 1), + mockSingleValue(Recall.AVG_RECALL_AGG_NAME, 0.8123) + ) + ); Recall recall = new Recall(); recall.aggs(EVALUATION_PARAMETERS, EVALUATION_FIELDS); ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, () -> recall.process(aggs)); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/common/AbstractAucRocTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/common/AbstractAucRocTests.java index d7d66930784a7..8f4180b30b834 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/common/AbstractAucRocTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/common/AbstractAucRocTests.java @@ -76,8 +76,8 @@ public void testCalculateAucScore_GivenPrecalculated() { double[] tpPercentiles = new double[99]; double[] fpPercentiles = new double[99]; - double[] tpSimplified = new double[] { 0.3, 0.6, 0.5 , 0.8 }; - double[] fpSimplified = new double[] { 0.1, 0.3, 0.5 , 0.5 }; + double[] tpSimplified = new double[] { 0.3, 0.6, 0.5, 0.8 }; + double[] fpSimplified = new double[] { 0.1, 0.3, 0.5, 0.5 }; for (int i = 0; i < tpPercentiles.length; i++) { int simplifiedIndex = i / 25; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/AucRocTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/AucRocTests.java index cbbd0ed40fdaa..2797080c12967 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/AucRocTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/AucRocTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ml.dataframe.evaluation.outlierdetection; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/ConfusionMatrixTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/ConfusionMatrixTests.java index f30cd27ca5cf8..b5c321768d27d 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/ConfusionMatrixTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/ConfusionMatrixTests.java @@ -8,9 +8,9 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.aggregations.Aggregations; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationMetricResult; import java.io.IOException; @@ -48,16 +48,18 @@ public static ConfusionMatrix createRandom() { } public void testEvaluate() { - Aggregations aggs = new Aggregations(Arrays.asList( - mockFilter("confusion_matrix_at_0.25_TP", 1L), - mockFilter("confusion_matrix_at_0.25_FP", 2L), - mockFilter("confusion_matrix_at_0.25_TN", 3L), - mockFilter("confusion_matrix_at_0.25_FN", 4L), - mockFilter("confusion_matrix_at_0.5_TP", 5L), - mockFilter("confusion_matrix_at_0.5_FP", 6L), - mockFilter("confusion_matrix_at_0.5_TN", 7L), - mockFilter("confusion_matrix_at_0.5_FN", 8L) - )); + Aggregations aggs = new Aggregations( + Arrays.asList( + mockFilter("confusion_matrix_at_0.25_TP", 1L), + mockFilter("confusion_matrix_at_0.25_FP", 2L), + mockFilter("confusion_matrix_at_0.25_TN", 3L), + mockFilter("confusion_matrix_at_0.25_FN", 4L), + mockFilter("confusion_matrix_at_0.5_TP", 5L), + mockFilter("confusion_matrix_at_0.5_FP", 6L), + mockFilter("confusion_matrix_at_0.5_TN", 7L), + mockFilter("confusion_matrix_at_0.5_FN", 8L) + ) + ); ConfusionMatrix confusionMatrix = new ConfusionMatrix(Arrays.asList(0.25, 0.5)); EvaluationMetricResult result = confusionMatrix.evaluate(aggs); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/OutlierDetectionTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/OutlierDetectionTests.java index b965954967953..e80a543e66e7d 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/OutlierDetectionTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/OutlierDetectionTests.java @@ -9,12 +9,12 @@ import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationFields; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationMetric; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationParameters; @@ -86,8 +86,10 @@ protected Writeable.Reader instanceReader() { } public void testConstructor_GivenEmptyMetrics() { - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> new OutlierDetection("foo", "bar", Collections.emptyList())); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> new OutlierDetection("foo", "bar", Collections.emptyList()) + ); assertThat(e.getMessage(), equalTo("[outlier_detection] must have one or more metrics")); } @@ -96,10 +98,15 @@ public void testConstructor_GivenDefaultMetrics() { List metrics = outlierDetection.getMetrics(); - assertThat(metrics, containsInAnyOrder(new AucRoc(false), - new Precision(Arrays.asList(0.25, 0.5, 0.75)), - new Recall(Arrays.asList(0.25, 0.5, 0.75)), - new ConfusionMatrix(Arrays.asList(0.25, 0.5, 0.75)))); + assertThat( + metrics, + containsInAnyOrder( + new AucRoc(false), + new Precision(Arrays.asList(0.25, 0.5, 0.75)), + new Recall(Arrays.asList(0.25, 0.5, 0.75)), + new ConfusionMatrix(Arrays.asList(0.25, 0.5, 0.75)) + ) + ); } public void testGetFields() { @@ -114,17 +121,17 @@ public void testGetFields() { } public void testBuildSearch() { - QueryBuilder userProvidedQuery = - QueryBuilders.boolQuery() - .filter(QueryBuilders.termQuery("field_A", "some-value")) - .filter(QueryBuilders.termQuery("field_B", "some-other-value")); - QueryBuilder expectedSearchQuery = - QueryBuilders.boolQuery() - .filter(QueryBuilders.existsQuery("act")) - .filter(QueryBuilders.existsQuery("prob")) - .filter(QueryBuilders.boolQuery() + QueryBuilder userProvidedQuery = QueryBuilders.boolQuery() + .filter(QueryBuilders.termQuery("field_A", "some-value")) + .filter(QueryBuilders.termQuery("field_B", "some-other-value")); + QueryBuilder expectedSearchQuery = QueryBuilders.boolQuery() + .filter(QueryBuilders.existsQuery("act")) + .filter(QueryBuilders.existsQuery("prob")) + .filter( + QueryBuilders.boolQuery() .filter(QueryBuilders.termQuery("field_A", "some-value")) - .filter(QueryBuilders.termQuery("field_B", "some-other-value"))); + .filter(QueryBuilders.termQuery("field_B", "some-other-value")) + ); OutlierDetection evaluation = new OutlierDetection("act", "prob", Arrays.asList(new Precision(Arrays.asList(0.7)))); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/PrecisionTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/PrecisionTests.java index bd4c81b119c5d..1dfff1f7baf04 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/PrecisionTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/PrecisionTests.java @@ -8,9 +8,9 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.aggregations.Aggregations; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationMetricResult; import java.io.IOException; @@ -48,14 +48,16 @@ public static Precision createRandom() { } public void testEvaluate() { - Aggregations aggs = new Aggregations(Arrays.asList( - mockFilter("precision_at_0.25_TP", 1L), - mockFilter("precision_at_0.25_FP", 4L), - mockFilter("precision_at_0.5_TP", 3L), - mockFilter("precision_at_0.5_FP", 1L), - mockFilter("precision_at_0.75_TP", 5L), - mockFilter("precision_at_0.75_FP", 0L) - )); + Aggregations aggs = new Aggregations( + Arrays.asList( + mockFilter("precision_at_0.25_TP", 1L), + mockFilter("precision_at_0.25_FP", 4L), + mockFilter("precision_at_0.5_TP", 3L), + mockFilter("precision_at_0.5_FP", 1L), + mockFilter("precision_at_0.75_TP", 5L), + mockFilter("precision_at_0.75_FP", 0L) + ) + ); Precision precision = new Precision(Arrays.asList(0.25, 0.5, 0.75)); EvaluationMetricResult result = precision.evaluate(aggs); @@ -65,10 +67,7 @@ public void testEvaluate() { } public void testEvaluate_GivenZeroTpAndFp() { - Aggregations aggs = new Aggregations(Arrays.asList( - mockFilter("precision_at_1.0_TP", 0L), - mockFilter("precision_at_1.0_FP", 0L) - )); + Aggregations aggs = new Aggregations(Arrays.asList(mockFilter("precision_at_1.0_TP", 0L), mockFilter("precision_at_1.0_FP", 0L))); Precision precision = new Precision(Arrays.asList(1.0)); EvaluationMetricResult result = precision.evaluate(aggs); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/RecallTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/RecallTests.java index be6ca623f6e6f..5c2c782a2c268 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/RecallTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/RecallTests.java @@ -8,9 +8,9 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.aggregations.Aggregations; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationMetricResult; import java.io.IOException; @@ -48,14 +48,16 @@ public static Recall createRandom() { } public void testEvaluate() { - Aggregations aggs = new Aggregations(Arrays.asList( - mockFilter("recall_at_0.25_TP", 1L), - mockFilter("recall_at_0.25_FN", 4L), - mockFilter("recall_at_0.5_TP", 3L), - mockFilter("recall_at_0.5_FN", 1L), - mockFilter("recall_at_0.75_TP", 5L), - mockFilter("recall_at_0.75_FN", 0L) - )); + Aggregations aggs = new Aggregations( + Arrays.asList( + mockFilter("recall_at_0.25_TP", 1L), + mockFilter("recall_at_0.25_FN", 4L), + mockFilter("recall_at_0.5_TP", 3L), + mockFilter("recall_at_0.5_FN", 1L), + mockFilter("recall_at_0.75_TP", 5L), + mockFilter("recall_at_0.75_FN", 0L) + ) + ); Recall recall = new Recall(Arrays.asList(0.25, 0.5, 0.75)); EvaluationMetricResult result = recall.evaluate(aggs); @@ -65,10 +67,7 @@ public void testEvaluate() { } public void testEvaluate_GivenZeroTpAndFp() { - Aggregations aggs = new Aggregations(Arrays.asList( - mockFilter("recall_at_1.0_TP", 0L), - mockFilter("recall_at_1.0_FN", 0L) - )); + Aggregations aggs = new Aggregations(Arrays.asList(mockFilter("recall_at_1.0_TP", 0L), mockFilter("recall_at_1.0_FN", 0L))); Recall recall = new Recall(Arrays.asList(1.0)); EvaluationMetricResult result = recall.evaluate(aggs); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/regression/HuberTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/regression/HuberTests.java index ef5abedc0a61c..97fca1a01d7b4 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/regression/HuberTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/regression/HuberTests.java @@ -8,9 +8,9 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.aggregations.Aggregations; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationMetricResult; import java.io.IOException; @@ -42,10 +42,9 @@ public static Huber createRandom() { } public void testEvaluate() { - Aggregations aggs = new Aggregations(Arrays.asList( - mockSingleValue("regression_huber", 0.8123), - mockSingleValue("some_other_single_metric_agg", 0.2377) - )); + Aggregations aggs = new Aggregations( + Arrays.asList(mockSingleValue("regression_huber", 0.8123), mockSingleValue("some_other_single_metric_agg", 0.2377)) + ); Huber huber = new Huber((Double) null); huber.process(aggs); @@ -56,9 +55,7 @@ public void testEvaluate() { } public void testEvaluate_GivenMissingAggs() { - Aggregations aggs = new Aggregations(Collections.singletonList( - mockSingleValue("some_other_single_metric_agg", 0.2377) - )); + Aggregations aggs = new Aggregations(Collections.singletonList(mockSingleValue("some_other_single_metric_agg", 0.2377))); Huber huber = new Huber((Double) null); huber.process(aggs); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/regression/MeanSquaredErrorTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/regression/MeanSquaredErrorTests.java index ea3f4cc9b5dd4..7d4852f5188dc 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/regression/MeanSquaredErrorTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/regression/MeanSquaredErrorTests.java @@ -8,9 +8,9 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.aggregations.Aggregations; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationMetricResult; import java.io.IOException; @@ -42,10 +42,9 @@ public static MeanSquaredError createRandom() { } public void testEvaluate() { - Aggregations aggs = new Aggregations(Arrays.asList( - mockSingleValue("regression_mse", 0.8123), - mockSingleValue("some_other_single_metric_agg", 0.2377) - )); + Aggregations aggs = new Aggregations( + Arrays.asList(mockSingleValue("regression_mse", 0.8123), mockSingleValue("some_other_single_metric_agg", 0.2377)) + ); MeanSquaredError mse = new MeanSquaredError(); mse.process(aggs); @@ -56,9 +55,7 @@ public void testEvaluate() { } public void testEvaluate_GivenMissingAggs() { - Aggregations aggs = new Aggregations(Collections.singletonList( - mockSingleValue("some_other_single_metric_agg", 0.2377) - )); + Aggregations aggs = new Aggregations(Collections.singletonList(mockSingleValue("some_other_single_metric_agg", 0.2377))); MeanSquaredError mse = new MeanSquaredError(); mse.process(aggs); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/regression/MeanSquaredLogarithmicErrorTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/regression/MeanSquaredLogarithmicErrorTests.java index 009fd1dd4a6e0..c6493a45dba82 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/regression/MeanSquaredLogarithmicErrorTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/regression/MeanSquaredLogarithmicErrorTests.java @@ -8,9 +8,9 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.aggregations.Aggregations; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationMetricResult; import java.io.IOException; @@ -42,10 +42,9 @@ public static MeanSquaredLogarithmicError createRandom() { } public void testEvaluate() { - Aggregations aggs = new Aggregations(Arrays.asList( - mockSingleValue("regression_msle", 0.8123), - mockSingleValue("some_other_single_metric_agg", 0.2377) - )); + Aggregations aggs = new Aggregations( + Arrays.asList(mockSingleValue("regression_msle", 0.8123), mockSingleValue("some_other_single_metric_agg", 0.2377)) + ); MeanSquaredLogarithmicError msle = new MeanSquaredLogarithmicError((Double) null); msle.process(aggs); @@ -56,9 +55,7 @@ public void testEvaluate() { } public void testEvaluate_GivenMissingAggs() { - Aggregations aggs = new Aggregations(Collections.singletonList( - mockSingleValue("some_other_single_metric_agg", 0.2377) - )); + Aggregations aggs = new Aggregations(Collections.singletonList(mockSingleValue("some_other_single_metric_agg", 0.2377))); MeanSquaredLogarithmicError msle = new MeanSquaredLogarithmicError((Double) null); msle.process(aggs); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/regression/RSquaredTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/regression/RSquaredTests.java index 485ded8ee6185..979c02c0e34c0 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/regression/RSquaredTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/regression/RSquaredTests.java @@ -8,9 +8,9 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.aggregations.Aggregations; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationMetricResult; import java.io.IOException; @@ -43,12 +43,14 @@ public static RSquared createRandom() { } public void testEvaluate() { - Aggregations aggs = new Aggregations(Arrays.asList( - mockSingleValue("residual_sum_of_squares", 10_111), - mockExtendedStats("extended_stats_actual", 155.23, 1000), - mockExtendedStats("some_other_extended_stats",99.1, 10_000), - mockSingleValue("some_other_single_metric_agg", 0.2377) - )); + Aggregations aggs = new Aggregations( + Arrays.asList( + mockSingleValue("residual_sum_of_squares", 10_111), + mockExtendedStats("extended_stats_actual", 155.23, 1000), + mockExtendedStats("some_other_extended_stats", 99.1, 10_000), + mockSingleValue("some_other_single_metric_agg", 0.2377) + ) + ); RSquared rSquared = new RSquared(); rSquared.process(aggs); @@ -59,12 +61,14 @@ public void testEvaluate() { } public void testEvaluateWithZeroCount() { - Aggregations aggs = new Aggregations(Arrays.asList( - mockSingleValue("residual_sum_of_squares", 0), - mockExtendedStats("extended_stats_actual", 0.0, 0), - mockExtendedStats("some_other_extended_stats",99.1, 10_000), - mockSingleValue("some_other_single_metric_agg", 0.2377) - )); + Aggregations aggs = new Aggregations( + Arrays.asList( + mockSingleValue("residual_sum_of_squares", 0), + mockExtendedStats("extended_stats_actual", 0.0, 0), + mockExtendedStats("some_other_extended_stats", 99.1, 10_000), + mockSingleValue("some_other_single_metric_agg", 0.2377) + ) + ); RSquared rSquared = new RSquared(); rSquared.process(aggs); @@ -74,12 +78,14 @@ public void testEvaluateWithZeroCount() { } public void testEvaluateWithSingleCountZeroVariance() { - Aggregations aggs = new Aggregations(Arrays.asList( - mockSingleValue("residual_sum_of_squares", 1), - mockExtendedStats("extended_stats_actual", 0.0, 1), - mockExtendedStats("some_other_extended_stats",99.1, 10_000), - mockSingleValue("some_other_single_metric_agg", 0.2377) - )); + Aggregations aggs = new Aggregations( + Arrays.asList( + mockSingleValue("residual_sum_of_squares", 1), + mockExtendedStats("extended_stats_actual", 0.0, 1), + mockExtendedStats("some_other_extended_stats", 99.1, 10_000), + mockSingleValue("some_other_single_metric_agg", 0.2377) + ) + ); RSquared rSquared = new RSquared(); rSquared.process(aggs); @@ -89,9 +95,7 @@ public void testEvaluateWithSingleCountZeroVariance() { } public void testEvaluate_GivenMissingAggs() { - Aggregations aggs = new Aggregations(Collections.singletonList( - mockSingleValue("some_other_single_metric_agg", 0.2377) - )); + Aggregations aggs = new Aggregations(Collections.singletonList(mockSingleValue("some_other_single_metric_agg", 0.2377))); RSquared rSquared = new RSquared(); rSquared.process(aggs); @@ -101,10 +105,9 @@ public void testEvaluate_GivenMissingAggs() { } public void testEvaluate_GivenMissingExtendedStatsAgg() { - Aggregations aggs = new Aggregations(Arrays.asList( - mockSingleValue("some_other_single_metric_agg", 0.2377), - mockSingleValue("residual_sum_of_squares", 0.2377) - )); + Aggregations aggs = new Aggregations( + Arrays.asList(mockSingleValue("some_other_single_metric_agg", 0.2377), mockSingleValue("residual_sum_of_squares", 0.2377)) + ); RSquared rSquared = new RSquared(); rSquared.process(aggs); @@ -114,10 +117,9 @@ public void testEvaluate_GivenMissingExtendedStatsAgg() { } public void testEvaluate_GivenMissingResidualSumOfSquaresAgg() { - Aggregations aggs = new Aggregations(Arrays.asList( - mockSingleValue("some_other_single_metric_agg", 0.2377), - mockExtendedStats("extended_stats_actual",100, 50) - )); + Aggregations aggs = new Aggregations( + Arrays.asList(mockSingleValue("some_other_single_metric_agg", 0.2377), mockExtendedStats("extended_stats_actual", 100, 50)) + ); RSquared rSquared = new RSquared(); rSquared.process(aggs); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/regression/RegressionTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/regression/RegressionTests.java index ac3f2901e1514..eab69abd165a5 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/regression/RegressionTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/regression/RegressionTests.java @@ -9,12 +9,12 @@ import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationFields; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationMetric; import org.elasticsearch.xpack.core.ml.dataframe.evaluation.EvaluationParameters; @@ -73,8 +73,10 @@ protected Writeable.Reader instanceReader() { } public void testConstructor_GivenEmptyMetrics() { - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> new Regression("foo", "bar", Collections.emptyList())); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> new Regression("foo", "bar", Collections.emptyList()) + ); assertThat(e.getMessage(), equalTo("[regression] must have one or more metrics")); } @@ -98,17 +100,17 @@ public void testGetFields() { } public void testBuildSearch() { - QueryBuilder userProvidedQuery = - QueryBuilders.boolQuery() - .filter(QueryBuilders.termQuery("field_A", "some-value")) - .filter(QueryBuilders.termQuery("field_B", "some-other-value")); - QueryBuilder expectedSearchQuery = - QueryBuilders.boolQuery() - .filter(QueryBuilders.existsQuery("act")) - .filter(QueryBuilders.existsQuery("pred")) - .filter(QueryBuilders.boolQuery() + QueryBuilder userProvidedQuery = QueryBuilders.boolQuery() + .filter(QueryBuilders.termQuery("field_A", "some-value")) + .filter(QueryBuilders.termQuery("field_B", "some-other-value")); + QueryBuilder expectedSearchQuery = QueryBuilders.boolQuery() + .filter(QueryBuilders.existsQuery("act")) + .filter(QueryBuilders.existsQuery("pred")) + .filter( + QueryBuilders.boolQuery() .filter(QueryBuilders.termQuery("field_A", "some-value")) - .filter(QueryBuilders.termQuery("field_B", "some-other-value"))); + .filter(QueryBuilders.termQuery("field_B", "some-other-value")) + ); Regression evaluation = new Regression("act", "pred", Arrays.asList(new MeanSquaredError())); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/explain/FieldSelectionTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/explain/FieldSelectionTests.java index d007da5363b96..6a601c4805768 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/explain/FieldSelectionTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/explain/FieldSelectionTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ml.dataframe.explain; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.Set; @@ -17,16 +17,11 @@ public class FieldSelectionTests extends AbstractSerializingTestCase { public static FieldSelection createRandom() { - Set mappingTypes = randomSubsetOf(randomIntBetween(1, 3), "int", "float", "double", "text", "keyword", "ip") - .stream().collect(Collectors.toSet()); + Set mappingTypes = randomSubsetOf(randomIntBetween(1, 3), "int", "float", "double", "text", "keyword", "ip").stream() + .collect(Collectors.toSet()); FieldSelection.FeatureType featureType = randomBoolean() ? null : randomFrom(FieldSelection.FeatureType.values()); String reason = randomBoolean() ? null : randomAlphaOfLength(20); - return new FieldSelection(randomAlphaOfLength(10), - mappingTypes, - randomBoolean(), - randomBoolean(), - featureType, - reason); + return new FieldSelection(randomAlphaOfLength(10), mappingTypes, randomBoolean(), randomBoolean(), featureType, reason); } @Override diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/explain/MemoryEstimationTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/explain/MemoryEstimationTests.java index 0abdfd6791d18..cc11f77c66637 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/explain/MemoryEstimationTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/explain/MemoryEstimationTests.java @@ -9,8 +9,8 @@ import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -22,7 +22,8 @@ public class MemoryEstimationTests extends AbstractSerializingTestCase TrainedModelDefinition.fromXContent(parser, false).build(), - xContentRegistry()); + xContentRegistry() + ); // Did we inflate to the same object? assertThat(inflatedDefinition, equalTo(definition)); @@ -40,44 +42,62 @@ public void testInflateAndDeflate() throws IOException { public void testInflateTooLargeStream() throws IOException { TrainedModelDefinition definition = TrainedModelDefinitionTests.createRandomBuilder() - .setPreProcessors(Stream.generate(() -> randomFrom(FrequencyEncodingTests.createRandom(), - OneHotEncodingTests.createRandom(), - TargetMeanEncodingTests.createRandom())) - .limit(100) - .collect(Collectors.toList())) + .setPreProcessors( + Stream.generate( + () -> randomFrom( + FrequencyEncodingTests.createRandom(), + OneHotEncodingTests.createRandom(), + TargetMeanEncodingTests.createRandom() + ) + ).limit(100).collect(Collectors.toList()) + ) .build(); BytesReference firstDeflate = InferenceToXContentCompressor.deflate(definition); int max = firstDeflate.length() + 10; - IOException ex = expectThrows(IOException.class, - () -> Streams.readFully(InferenceToXContentCompressor.inflate(firstDeflate, max))); - assertThat(ex.getMessage(), equalTo("" + - "input stream exceeded maximum bytes of [" + max + "]")); + IOException ex = expectThrows(IOException.class, () -> Streams.readFully(InferenceToXContentCompressor.inflate(firstDeflate, max))); + assertThat(ex.getMessage(), equalTo("" + "input stream exceeded maximum bytes of [" + max + "]")); } public void testInflateGarbage() { - expectThrows(IOException.class, () -> Streams.readFully( - InferenceToXContentCompressor.inflate(new BytesArray(randomByteArrayOfLength(10)), 100L))); + expectThrows( + IOException.class, + () -> Streams.readFully(InferenceToXContentCompressor.inflate(new BytesArray(randomByteArrayOfLength(10)), 100L)) + ); } public void testInflateParsingTooLargeStream() throws IOException { TrainedModelDefinition definition = TrainedModelDefinitionTests.createRandomBuilder() - .setPreProcessors(Stream.generate(() -> randomFrom(FrequencyEncodingTests.createRandom(), - OneHotEncodingTests.createRandom(), - TargetMeanEncodingTests.createRandom())) - .limit(100) - .collect(Collectors.toList())) + .setPreProcessors( + Stream.generate( + () -> randomFrom( + FrequencyEncodingTests.createRandom(), + OneHotEncodingTests.createRandom(), + TargetMeanEncodingTests.createRandom() + ) + ).limit(100).collect(Collectors.toList()) + ) .build(); BytesReference compressedString = InferenceToXContentCompressor.deflate(definition); int max = compressedString.length() + 10; - CircuitBreakingException e = expectThrows(CircuitBreakingException.class, ()-> InferenceToXContentCompressor.inflate( - compressedString, - parser -> TrainedModelDefinition.fromXContent(parser, true).build(), - xContentRegistry(), - max)); + CircuitBreakingException e = expectThrows( + CircuitBreakingException.class, + () -> InferenceToXContentCompressor.inflate( + compressedString, + parser -> TrainedModelDefinition.fromXContent(parser, true).build(), + xContentRegistry(), + max + ) + ); - assertThat(e.getMessage(), equalTo("Cannot parse model definition as the content is larger than the maximum stream size of [" - + max + "] bytes. Max stream size is 10% of the JVM heap or 1GB whichever is smallest")); + assertThat( + e.getMessage(), + equalTo( + "Cannot parse model definition as the content is larger than the maximum stream size of [" + + max + + "] bytes. Max stream size is 10% of the JVM heap or 1GB whichever is smallest" + ) + ); assertThat(e.getDurability(), equalTo(CircuitBreaker.Durability.PERMANENT)); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/NamedXContentObjectsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/NamedXContentObjectsTests.java index 666f297047f54..343ed89f9ac7d 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/NamedXContentObjectsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/NamedXContentObjectsTests.java @@ -6,26 +6,26 @@ */ package org.elasticsearch.xpack.core.ml.inference; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.search.SearchModule; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.search.SearchModule; -import org.elasticsearch.test.AbstractXContentTestCase; -import org.elasticsearch.xpack.core.ml.inference.trainedmodel.LenientlyParsedTrainedModel; -import org.elasticsearch.xpack.core.ml.inference.trainedmodel.TrainedModel; -import org.elasticsearch.xpack.core.ml.inference.trainedmodel.StrictlyParsedTrainedModel; -import org.elasticsearch.xpack.core.ml.inference.trainedmodel.ensemble.EnsembleTests; -import org.elasticsearch.xpack.core.ml.inference.trainedmodel.tree.TreeTests; import org.elasticsearch.xpack.core.ml.inference.preprocessing.FrequencyEncodingTests; import org.elasticsearch.xpack.core.ml.inference.preprocessing.LenientlyParsedPreProcessor; import org.elasticsearch.xpack.core.ml.inference.preprocessing.OneHotEncodingTests; import org.elasticsearch.xpack.core.ml.inference.preprocessing.PreProcessor; import org.elasticsearch.xpack.core.ml.inference.preprocessing.StrictlyParsedPreProcessor; import org.elasticsearch.xpack.core.ml.inference.preprocessing.TargetMeanEncodingTests; +import org.elasticsearch.xpack.core.ml.inference.trainedmodel.LenientlyParsedTrainedModel; +import org.elasticsearch.xpack.core.ml.inference.trainedmodel.StrictlyParsedTrainedModel; +import org.elasticsearch.xpack.core.ml.inference.trainedmodel.TrainedModel; +import org.elasticsearch.xpack.core.ml.inference.trainedmodel.ensemble.EnsembleTests; +import org.elasticsearch.xpack.core.ml.inference.trainedmodel.tree.TreeTests; import org.elasticsearch.xpack.core.ml.utils.NamedXContentObject; import org.junit.Before; @@ -52,17 +52,23 @@ private static ObjectParser createParser(boolean len ObjectParser parser = new ObjectParser<>( "named_xcontent_object_container_test", lenient, - NamedObjectContainer::new); - parser.declareNamedObjects(NamedObjectContainer::setPreProcessors, - (p, c, n) -> - lenient ? p.namedObject(LenientlyParsedPreProcessor.class, n, null) : - p.namedObject(StrictlyParsedPreProcessor.class, n, null), - (noc) -> noc.setUseExplicitPreprocessorOrder(true), PRE_PROCESSORS); - parser.declareNamedObjects(NamedObjectContainer::setTrainedModel, - (p, c, n) -> - lenient ? p.namedObject(LenientlyParsedTrainedModel.class, n, null) : - p.namedObject(StrictlyParsedTrainedModel.class, n, null), - TRAINED_MODEL); + NamedObjectContainer::new + ); + parser.declareNamedObjects( + NamedObjectContainer::setPreProcessors, + (p, c, n) -> lenient + ? p.namedObject(LenientlyParsedPreProcessor.class, n, null) + : p.namedObject(StrictlyParsedPreProcessor.class, n, null), + (noc) -> noc.setUseExplicitPreprocessorOrder(true), + PRE_PROCESSORS + ); + parser.declareNamedObjects( + NamedObjectContainer::setTrainedModel, + (p, c, n) -> lenient + ? p.namedObject(LenientlyParsedTrainedModel.class, n, null) + : p.namedObject(StrictlyParsedTrainedModel.class, n, null), + TRAINED_MODEL + ); return parser; } @@ -99,11 +105,13 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws return builder; } - XContentBuilder writeNamedObjects(XContentBuilder builder, - Params params, - boolean useExplicitOrder, - String namedObjectsName, - List namedObjects) throws IOException { + XContentBuilder writeNamedObjects( + XContentBuilder builder, + Params params, + boolean useExplicitOrder, + String namedObjectsName, + List namedObjects + ) throws IOException { if (useExplicitOrder) { builder.startArray(namedObjectsName); } else { @@ -152,9 +160,13 @@ public NamedObjectContainer createTestInstance() { int max = randomIntBetween(1, 10); List preProcessors = new ArrayList<>(max); for (int i = 0; i < max; i++) { - preProcessors.add(randomFrom(FrequencyEncodingTests.createRandom(), - OneHotEncodingTests.createRandom(), - TargetMeanEncodingTests.createRandom())); + preProcessors.add( + randomFrom( + FrequencyEncodingTests.createRandom(), + OneHotEncodingTests.createRandom(), + TargetMeanEncodingTests.createRandom() + ) + ); } NamedObjectContainer container = new NamedObjectContainer(); container.setPreProcessors(preProcessors); @@ -176,12 +188,11 @@ protected boolean supportsUnknownFields() { @Override protected Predicate getRandomFieldsExcludeFilter() { // We only want to add random fields to the root, or the root of the named objects - return field -> - (field.endsWith("frequency_encoding") || - field.endsWith("one_hot_encoding") || - field.endsWith("target_mean_encoding") || - field.startsWith("tree.tree_structure") || - field.isEmpty()) == false; + return field -> (field.endsWith("frequency_encoding") + || field.endsWith("one_hot_encoding") + || field.endsWith("target_mean_encoding") + || field.startsWith("tree.tree_structure") + || field.isEmpty()) == false; } @Override @@ -192,4 +203,3 @@ protected NamedXContentRegistry xContentRegistry() { return new NamedXContentRegistry(namedXContent); } } - diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/TrainedModelConfigTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/TrainedModelConfigTests.java index c2911a51ebf6a..11fff7737eab9 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/TrainedModelConfigTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/TrainedModelConfigTests.java @@ -12,16 +12,16 @@ import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.license.License; +import org.elasticsearch.search.SearchModule; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.license.License; -import org.elasticsearch.search.SearchModule; import org.elasticsearch.xpack.core.ml.AbstractBWCSerializationTestCase; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.ClassificationConfigTests; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.FillMaskConfigTests; @@ -59,6 +59,7 @@ public class TrainedModelConfigTests extends AbstractBWCSerializationTestCase { private boolean lenient; + public static TrainedModelConfig.Builder createTestInstance(String modelId) { return createTestInstance(modelId, false); } @@ -66,7 +67,7 @@ public static TrainedModelConfig.Builder createTestInstance(String modelId) { public static TrainedModelConfig.Builder createTestInstance(String modelId, boolean lenient) { InferenceConfig[] inferenceConfigs = lenient ? - // Because of vocab config validations on parse, only test on lenient + // Because of vocab config validations on parse, only test on lenient new InferenceConfig[] { ClassificationConfigTests.randomClassificationConfig(), RegressionConfigTests.randomRegressionConfig(), @@ -74,11 +75,10 @@ public static TrainedModelConfig.Builder createTestInstance(String modelId, bool PassThroughConfigTests.createRandom(), TextClassificationConfigTests.createRandom(), FillMaskConfigTests.createRandom(), - TextEmbeddingConfigTests.createRandom() - } : new InferenceConfig[] { + TextEmbeddingConfigTests.createRandom() } + : new InferenceConfig[] { ClassificationConfigTests.randomClassificationConfig(), - RegressionConfigTests.randomRegressionConfig() - }; + RegressionConfigTests.randomRegressionConfig() }; List tags = Arrays.asList(generateRandomStringArray(randomIntBetween(0, 5), 15, false)); return TrainedModelConfig.builder() .setInput(TrainedModelInputTests.createRandomInput()) @@ -91,8 +91,7 @@ public static TrainedModelConfig.Builder createTestInstance(String modelId, bool .setDescription(randomBoolean() ? null : randomAlphaOfLength(10)) .setEstimatedHeapMemory(randomNonNegativeLong()) .setEstimatedOperations(randomNonNegativeLong()) - .setLicenseLevel(randomFrom(License.OperationMode.PLATINUM.description(), - License.OperationMode.BASIC.description())) + .setLicenseLevel(randomFrom(License.OperationMode.PLATINUM.description(), License.OperationMode.BASIC.description())) .setInferenceConfig(randomFrom(inferenceConfigs)) .setTags(tags) .setLocation(randomBoolean() ? null : IndexLocationTests.randomInstance()); @@ -153,8 +152,9 @@ protected boolean assertToXContentEquivalence() { } public void testToXContentWithParams() throws IOException { - TrainedModelConfig.LazyModelDefinition lazyModelDefinition = TrainedModelConfig.LazyModelDefinition - .fromParsedDefinition(TrainedModelDefinitionTests.createRandomBuilder().build()); + TrainedModelConfig.LazyModelDefinition lazyModelDefinition = TrainedModelConfig.LazyModelDefinition.fromParsedDefinition( + TrainedModelDefinitionTests.createRandomBuilder().build() + ); TrainedModelConfig config = new TrainedModelConfig( randomAlphaOfLength(10), TrainedModelType.TREE_ENSEMBLE, @@ -169,34 +169,41 @@ public void testToXContentWithParams() throws IOException { randomNonNegativeLong(), randomNonNegativeLong(), "platinum", - randomBoolean() ? null : - Stream.generate(() -> randomAlphaOfLength(10)) + randomBoolean() + ? null + : Stream.generate(() -> randomAlphaOfLength(10)) .limit(randomIntBetween(1, 10)) .collect(Collectors.toMap(Function.identity(), (k) -> randomAlphaOfLength(10))), randomFrom(ClassificationConfigTests.randomClassificationConfig(), RegressionConfigTests.randomRegressionConfig()), - null); + null + ); BytesReference reference = XContentHelper.toXContent(config, XContentType.JSON, ToXContent.EMPTY_PARAMS, false); assertThat(reference.utf8ToString(), containsString("\"compressed_definition\"")); - reference = XContentHelper.toXContent(config, + reference = XContentHelper.toXContent( + config, XContentType.JSON, new ToXContent.MapParams(Collections.singletonMap(ToXContentParams.FOR_INTERNAL_STORAGE, "true")), - false); + false + ); assertThat(reference.utf8ToString(), not(containsString("definition"))); assertThat(reference.utf8ToString(), not(containsString("compressed_definition"))); - reference = XContentHelper.toXContent(config, + reference = XContentHelper.toXContent( + config, XContentType.JSON, new ToXContent.MapParams(Collections.singletonMap(TrainedModelConfig.DECOMPRESS_DEFINITION, "true")), - false); + false + ); assertThat(reference.utf8ToString(), containsString("\"definition\"")); assertThat(reference.utf8ToString(), not(containsString("compressed_definition"))); } public void testParseWithBothDefinitionAndCompressedSupplied() throws IOException { - TrainedModelConfig.LazyModelDefinition lazyModelDefinition = TrainedModelConfig.LazyModelDefinition - .fromParsedDefinition(TrainedModelDefinitionTests.createRandomBuilder().build()); + TrainedModelConfig.LazyModelDefinition lazyModelDefinition = TrainedModelConfig.LazyModelDefinition.fromParsedDefinition( + TrainedModelDefinitionTests.createRandomBuilder().build() + ); TrainedModelConfig config = new TrainedModelConfig( randomAlphaOfLength(10), TrainedModelType.TREE_ENSEMBLE, @@ -211,114 +218,134 @@ public void testParseWithBothDefinitionAndCompressedSupplied() throws IOExceptio randomNonNegativeLong(), randomNonNegativeLong(), "platinum", - randomBoolean() ? null : - Stream.generate(() -> randomAlphaOfLength(10)) + randomBoolean() + ? null + : Stream.generate(() -> randomAlphaOfLength(10)) .limit(randomIntBetween(1, 10)) .collect(Collectors.toMap(Function.identity(), (k) -> randomAlphaOfLength(10))), randomFrom(ClassificationConfigTests.randomClassificationConfig(), RegressionConfigTests.randomRegressionConfig()), - null); + null + ); BytesReference reference = XContentHelper.toXContent(config, XContentType.JSON, ToXContent.EMPTY_PARAMS, false); Map objectMap = XContentHelper.convertToMap(reference, true, XContentType.JSON).v2(); objectMap.put(TrainedModelConfig.DEFINITION.getPreferredName(), config.getModelDefinition()); - try(XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().map(objectMap); - XContentParser parser = XContentType.JSON - .xContent() - .createParser(xContentRegistry(), + try ( + XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().map(objectMap); + XContentParser parser = XContentType.JSON.xContent() + .createParser( + xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, - BytesReference.bytes(xContentBuilder).streamInput())) { + BytesReference.bytes(xContentBuilder).streamInput() + ) + ) { IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> TrainedModelConfig.fromXContent(parser, true)); assertThat(ex.getCause().getMessage(), equalTo("both [compressed_definition] and [definition] cannot be set.")); } } public void testValidateWithBothDefinitionAndLocation() { - ActionRequestValidationException ex = expectThrows(ActionRequestValidationException.class, + ActionRequestValidationException ex = expectThrows( + ActionRequestValidationException.class, () -> TrainedModelConfig.builder() .setLocation(IndexLocationTests.randomInstance()) .setParsedDefinition(TrainedModelDefinitionTests.createRandomBuilder()) .setModelType(TrainedModelType.PYTORCH) - .validate()); + .validate() + ); assertThat(ex.getMessage(), containsString("[definition] and [location] are both defined but only one can be used.")); } public void testValidateWithWithMissingTypeAndDefinition() { - ActionRequestValidationException ex = expectThrows(ActionRequestValidationException.class, - () -> TrainedModelConfig.builder() - .setLocation(IndexLocationTests.randomInstance()) - .validate()); + ActionRequestValidationException ex = expectThrows( + ActionRequestValidationException.class, + () -> TrainedModelConfig.builder().setLocation(IndexLocationTests.randomInstance()).validate() + ); assertThat(ex.getMessage(), containsString("[model_type] must be set if [definition] is not defined")); } public void testValidateWithInvalidID() { String modelId = "InvalidID-"; - ActionRequestValidationException ex = expectThrows(ActionRequestValidationException.class, + ActionRequestValidationException ex = expectThrows( + ActionRequestValidationException.class, () -> TrainedModelConfig.builder() .setParsedDefinition(TrainedModelDefinitionTests.createRandomBuilder()) - .setModelId(modelId).validate()); + .setModelId(modelId) + .validate() + ); assertThat(ex.getMessage(), containsString(Messages.getMessage(Messages.INVALID_ID, "model_id", modelId))); } public void testValidateWithLongID() { String modelId = IntStream.range(0, 100).mapToObj(x -> "a").collect(Collectors.joining()); - ActionRequestValidationException ex = expectThrows(ActionRequestValidationException.class, + ActionRequestValidationException ex = expectThrows( + ActionRequestValidationException.class, () -> TrainedModelConfig.builder() .setParsedDefinition(TrainedModelDefinitionTests.createRandomBuilder()) - .setModelId(modelId).validate()); - assertThat(ex.getMessage(), - containsString(Messages.getMessage(Messages.ID_TOO_LONG, "model_id", modelId, MlStrings.ID_LENGTH_LIMIT))); + .setModelId(modelId) + .validate() + ); + assertThat( + ex.getMessage(), + containsString(Messages.getMessage(Messages.ID_TOO_LONG, "model_id", modelId, MlStrings.ID_LENGTH_LIMIT)) + ); } public void testValidateWithIllegallyUserProvidedFields() { String modelId = "simplemodel"; - ActionRequestValidationException ex = expectThrows(ActionRequestValidationException.class, + ActionRequestValidationException ex = expectThrows( + ActionRequestValidationException.class, () -> TrainedModelConfig.builder() .setParsedDefinition(TrainedModelDefinitionTests.createRandomBuilder()) .setCreateTime(Instant.now()) - .setModelId(modelId).validate(true)); + .setModelId(modelId) + .validate(true) + ); assertThat(ex.getMessage(), containsString("illegal to set [create_time] at inference model creation")); - ex = expectThrows(ActionRequestValidationException.class, + ex = expectThrows( + ActionRequestValidationException.class, () -> TrainedModelConfig.builder() .setParsedDefinition(TrainedModelDefinitionTests.createRandomBuilder()) .setVersion(Version.CURRENT) - .setModelId(modelId).validate(true)); + .setModelId(modelId) + .validate(true) + ); assertThat(ex.getMessage(), containsString("illegal to set [version] at inference model creation")); - ex = expectThrows(ActionRequestValidationException.class, + ex = expectThrows( + ActionRequestValidationException.class, () -> TrainedModelConfig.builder() .setParsedDefinition(TrainedModelDefinitionTests.createRandomBuilder()) .setCreatedBy("ml_user") - .setModelId(modelId).validate(true)); + .setModelId(modelId) + .validate(true) + ); assertThat(ex.getMessage(), containsString("illegal to set [created_by] at inference model creation")); } public void testSerializationWithLazyDefinition() throws IOException { - xContentTester(this::createParser, - () -> { + xContentTester(this::createParser, () -> { try { BytesReference bytes = InferenceToXContentCompressor.deflate(TrainedModelDefinitionTests.createRandomBuilder().build()); - return createTestInstance(randomAlphaOfLength(10), lenient) - .setDefinitionFromBytes(bytes) - .build(); + return createTestInstance(randomAlphaOfLength(10), lenient).setDefinitionFromBytes(bytes).build(); } catch (IOException ex) { fail(ex.getMessage()); return null; } - }, - ToXContent.EMPTY_PARAMS, - (p) -> TrainedModelConfig.fromXContent(p, true).build()) - .numberOfTestRuns(NUMBER_OF_TEST_RUNS) + }, ToXContent.EMPTY_PARAMS, (p) -> TrainedModelConfig.fromXContent(p, true).build()).numberOfTestRuns(NUMBER_OF_TEST_RUNS) .supportsUnknownFields(false) .shuffleFieldsExceptions(getShuffleFieldsExceptions()) .randomFieldsExcludeFilter(getRandomFieldsExcludeFilter()) .assertEqualsConsumer((def1, def2) -> { try { - assertThat(def1.ensureParsedDefinition(xContentRegistry()).getModelDefinition(), - equalTo(def2.ensureParsedDefinition(xContentRegistry()).getModelDefinition())); - } catch(IOException ex) { + assertThat( + def1.ensureParsedDefinition(xContentRegistry()).getModelDefinition(), + equalTo(def2.ensureParsedDefinition(xContentRegistry()).getModelDefinition()) + ); + } catch (IOException ex) { fail(ex.getMessage()); } }) @@ -327,30 +354,28 @@ public void testSerializationWithLazyDefinition() throws IOException { } public void testSerializationWithCompressedLazyDefinition() throws IOException { - xContentTester(this::createParser, - () -> { - try { - BytesReference bytes = - InferenceToXContentCompressor.deflate(TrainedModelDefinitionTests.createRandomBuilder().build()); - return createTestInstance(randomAlphaOfLength(10), lenient) - .setDefinitionFromBytes(bytes) - .build(); - } catch (IOException ex) { - fail(ex.getMessage()); - return null; - } - }, + xContentTester(this::createParser, () -> { + try { + BytesReference bytes = InferenceToXContentCompressor.deflate(TrainedModelDefinitionTests.createRandomBuilder().build()); + return createTestInstance(randomAlphaOfLength(10), lenient).setDefinitionFromBytes(bytes).build(); + } catch (IOException ex) { + fail(ex.getMessage()); + return null; + } + }, new ToXContent.MapParams(Collections.singletonMap(TrainedModelConfig.DECOMPRESS_DEFINITION, "false")), - (p) -> TrainedModelConfig.fromXContent(p, true).build()) - .numberOfTestRuns(NUMBER_OF_TEST_RUNS) + (p) -> TrainedModelConfig.fromXContent(p, true).build() + ).numberOfTestRuns(NUMBER_OF_TEST_RUNS) .supportsUnknownFields(false) .shuffleFieldsExceptions(getShuffleFieldsExceptions()) .randomFieldsExcludeFilter(getRandomFieldsExcludeFilter()) .assertEqualsConsumer((def1, def2) -> { try { - assertThat(def1.ensureParsedDefinition(xContentRegistry()).getModelDefinition(), - equalTo(def2.ensureParsedDefinition(xContentRegistry()).getModelDefinition())); - } catch(IOException ex) { + assertThat( + def1.ensureParsedDefinition(xContentRegistry()).getModelDefinition(), + equalTo(def2.ensureParsedDefinition(xContentRegistry()).getModelDefinition()) + ); + } catch (IOException ex) { fail(ex.getMessage()); } }) @@ -368,7 +393,7 @@ protected TrainedModelConfig mutateInstanceForVersion(TrainedModelConfig instanc builder.setInferenceConfig(null); } if (version.before(TrainedModelConfig.VERSION_3RD_PARTY_CONFIG_ADDED)) { - builder.setModelType((TrainedModelType)null); + builder.setModelType((TrainedModelType) null); builder.setLocation(null); } return builder.build(); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/TrainedModelDefinitionTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/TrainedModelDefinitionTests.java index a2cc7e915c19a..12324cb9c08ed 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/TrainedModelDefinitionTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/TrainedModelDefinitionTests.java @@ -9,13 +9,13 @@ import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.search.SearchModule; +import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.search.SearchModule; -import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xpack.core.ml.inference.preprocessing.FrequencyEncodingTests; import org.elasticsearch.xpack.core.ml.inference.preprocessing.OneHotEncodingTests; import org.elasticsearch.xpack.core.ml.inference.preprocessing.TargetMeanEncodingTests; @@ -36,7 +36,6 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.greaterThan; - public class TrainedModelDefinitionTests extends AbstractSerializingTestCase { @Override @@ -61,208 +60,210 @@ protected boolean assertToXContentEquivalence() { public static TrainedModelDefinition.Builder createRandomBuilder(TargetType targetType) { int numberOfProcessors = randomIntBetween(1, 10); - return new TrainedModelDefinition.Builder() - .setPreProcessors( - randomBoolean() ? null : - Stream.generate(() -> randomFrom(FrequencyEncodingTests.createRandom(), + return new TrainedModelDefinition.Builder().setPreProcessors( + randomBoolean() + ? null + : Stream.generate( + () -> randomFrom( + FrequencyEncodingTests.createRandom(), OneHotEncodingTests.createRandom(), - TargetMeanEncodingTests.createRandom())) - .limit(numberOfProcessors) - .collect(Collectors.toList())) - .setTrainedModel(randomFrom(TreeTests.createRandom(targetType), EnsembleTests.createRandom(targetType))); + TargetMeanEncodingTests.createRandom() + ) + ).limit(numberOfProcessors).collect(Collectors.toList()) + ).setTrainedModel(randomFrom(TreeTests.createRandom(targetType), EnsembleTests.createRandom(targetType))); } public static TrainedModelDefinition.Builder createRandomBuilder() { return createRandomBuilder(randomFrom(TargetType.values())); } - public static final String ENSEMBLE_MODEL = "" + - "{\n" + - " \"preprocessors\": [\n" + - " {\n" + - " \"one_hot_encoding\": {\n" + - " \"field\": \"col1\",\n" + - " \"hot_map\": {\n" + - " \"male\": \"col1_male\",\n" + - " \"female\": \"col1_female\"\n" + - " }\n" + - " }\n" + - " },\n" + - " {\n" + - " \"target_mean_encoding\": {\n" + - " \"field\": \"col2\",\n" + - " \"feature_name\": \"col2_encoded\",\n" + - " \"target_map\": {\n" + - " \"S\": 5.0,\n" + - " \"M\": 10.0,\n" + - " \"L\": 20\n" + - " },\n" + - " \"default_value\": 5.0\n" + - " }\n" + - " },\n" + - " {\n" + - " \"frequency_encoding\": {\n" + - " \"field\": \"col3\",\n" + - " \"feature_name\": \"col3_encoded\",\n" + - " \"frequency_map\": {\n" + - " \"none\": 0.75,\n" + - " \"true\": 0.10,\n" + - " \"false\": 0.15\n" + - " }\n" + - " }\n" + - " }\n" + - " ],\n" + - " \"trained_model\": {\n" + - " \"ensemble\": {\n" + - " \"feature_names\": [\n" + - " \"col1_male\",\n" + - " \"col1_female\",\n" + - " \"col2_encoded\",\n" + - " \"col3_encoded\",\n" + - " \"col4\"\n" + - " ],\n" + - " \"aggregate_output\": {\n" + - " \"weighted_sum\": {\n" + - " \"weights\": [\n" + - " 0.5,\n" + - " 0.5\n" + - " ]\n" + - " }\n" + - " },\n" + - " \"target_type\": \"regression\",\n" + - " \"trained_models\": [\n" + - " {\n" + - " \"tree\": {\n" + - " \"feature_names\": [\n" + - " \"col1_male\",\n" + - " \"col1_female\",\n" + - " \"col4\"\n" + - " ],\n" + - " \"tree_structure\": [\n" + - " {\n" + - " \"node_index\": 0,\n" + - " \"split_feature\": 0,\n" + - " \"split_gain\": 12.0,\n" + - " \"threshold\": 10.0,\n" + - " \"decision_type\": \"lte\",\n" + - " \"default_left\": true,\n" + - " \"left_child\": 1,\n" + - " \"right_child\": 2\n" + - " },\n" + - " {\n" + - " \"node_index\": 1,\n" + - " \"leaf_value\": 1\n" + - " },\n" + - " {\n" + - " \"node_index\": 2,\n" + - " \"leaf_value\": 2\n" + - " }\n" + - " ],\n" + - " \"target_type\": \"regression\"\n" + - " }\n" + - " },\n" + - " {\n" + - " \"tree\": {\n" + - " \"feature_names\": [\n" + - " \"col2_encoded\",\n" + - " \"col3_encoded\",\n" + - " \"col4\"\n" + - " ],\n" + - " \"tree_structure\": [\n" + - " {\n" + - " \"node_index\": 0,\n" + - " \"split_feature\": 0,\n" + - " \"split_gain\": 12.0,\n" + - " \"threshold\": 10.0,\n" + - " \"decision_type\": \"lte\",\n" + - " \"default_left\": true,\n" + - " \"left_child\": 1,\n" + - " \"right_child\": 2\n" + - " },\n" + - " {\n" + - " \"node_index\": 1,\n" + - " \"leaf_value\": 1\n" + - " },\n" + - " {\n" + - " \"node_index\": 2,\n" + - " \"leaf_value\": 2\n" + - " }\n" + - " ],\n" + - " \"target_type\": \"regression\"\n" + - " }\n" + - " }\n" + - " ]\n" + - " }\n" + - " }\n" + - "}"; + public static final String ENSEMBLE_MODEL = "" + + "{\n" + + " \"preprocessors\": [\n" + + " {\n" + + " \"one_hot_encoding\": {\n" + + " \"field\": \"col1\",\n" + + " \"hot_map\": {\n" + + " \"male\": \"col1_male\",\n" + + " \"female\": \"col1_female\"\n" + + " }\n" + + " }\n" + + " },\n" + + " {\n" + + " \"target_mean_encoding\": {\n" + + " \"field\": \"col2\",\n" + + " \"feature_name\": \"col2_encoded\",\n" + + " \"target_map\": {\n" + + " \"S\": 5.0,\n" + + " \"M\": 10.0,\n" + + " \"L\": 20\n" + + " },\n" + + " \"default_value\": 5.0\n" + + " }\n" + + " },\n" + + " {\n" + + " \"frequency_encoding\": {\n" + + " \"field\": \"col3\",\n" + + " \"feature_name\": \"col3_encoded\",\n" + + " \"frequency_map\": {\n" + + " \"none\": 0.75,\n" + + " \"true\": 0.10,\n" + + " \"false\": 0.15\n" + + " }\n" + + " }\n" + + " }\n" + + " ],\n" + + " \"trained_model\": {\n" + + " \"ensemble\": {\n" + + " \"feature_names\": [\n" + + " \"col1_male\",\n" + + " \"col1_female\",\n" + + " \"col2_encoded\",\n" + + " \"col3_encoded\",\n" + + " \"col4\"\n" + + " ],\n" + + " \"aggregate_output\": {\n" + + " \"weighted_sum\": {\n" + + " \"weights\": [\n" + + " 0.5,\n" + + " 0.5\n" + + " ]\n" + + " }\n" + + " },\n" + + " \"target_type\": \"regression\",\n" + + " \"trained_models\": [\n" + + " {\n" + + " \"tree\": {\n" + + " \"feature_names\": [\n" + + " \"col1_male\",\n" + + " \"col1_female\",\n" + + " \"col4\"\n" + + " ],\n" + + " \"tree_structure\": [\n" + + " {\n" + + " \"node_index\": 0,\n" + + " \"split_feature\": 0,\n" + + " \"split_gain\": 12.0,\n" + + " \"threshold\": 10.0,\n" + + " \"decision_type\": \"lte\",\n" + + " \"default_left\": true,\n" + + " \"left_child\": 1,\n" + + " \"right_child\": 2\n" + + " },\n" + + " {\n" + + " \"node_index\": 1,\n" + + " \"leaf_value\": 1\n" + + " },\n" + + " {\n" + + " \"node_index\": 2,\n" + + " \"leaf_value\": 2\n" + + " }\n" + + " ],\n" + + " \"target_type\": \"regression\"\n" + + " }\n" + + " },\n" + + " {\n" + + " \"tree\": {\n" + + " \"feature_names\": [\n" + + " \"col2_encoded\",\n" + + " \"col3_encoded\",\n" + + " \"col4\"\n" + + " ],\n" + + " \"tree_structure\": [\n" + + " {\n" + + " \"node_index\": 0,\n" + + " \"split_feature\": 0,\n" + + " \"split_gain\": 12.0,\n" + + " \"threshold\": 10.0,\n" + + " \"decision_type\": \"lte\",\n" + + " \"default_left\": true,\n" + + " \"left_child\": 1,\n" + + " \"right_child\": 2\n" + + " },\n" + + " {\n" + + " \"node_index\": 1,\n" + + " \"leaf_value\": 1\n" + + " },\n" + + " {\n" + + " \"node_index\": 2,\n" + + " \"leaf_value\": 2\n" + + " }\n" + + " ],\n" + + " \"target_type\": \"regression\"\n" + + " }\n" + + " }\n" + + " ]\n" + + " }\n" + + " }\n" + + "}"; - public static final String TREE_MODEL = "" + - "{\n" + - " \"preprocessors\": [\n" + - " {\n" + - " \"one_hot_encoding\": {\n" + - " \"field\": \"col1\",\n" + - " \"hot_map\": {\n" + - " \"male\": \"col1_male\",\n" + - " \"female\": \"col1_female\"\n" + - " }\n" + - " }\n" + - " },\n" + - " {\n" + - " \"target_mean_encoding\": {\n" + - " \"field\": \"col2\",\n" + - " \"feature_name\": \"col2_encoded\",\n" + - " \"target_map\": {\n" + - " \"S\": 5.0,\n" + - " \"M\": 10.0,\n" + - " \"L\": 20\n" + - " },\n" + - " \"default_value\": 5.0\n" + - " }\n" + - " },\n" + - " {\n" + - " \"frequency_encoding\": {\n" + - " \"field\": \"col3\",\n" + - " \"feature_name\": \"col3_encoded\",\n" + - " \"frequency_map\": {\n" + - " \"none\": 0.75,\n" + - " \"true\": 0.10,\n" + - " \"false\": 0.15\n" + - " }\n" + - " }\n" + - " }\n" + - " ],\n" + - " \"trained_model\": {\n" + - " \"tree\": {\n" + - " \"feature_names\": [\n" + - " \"col1_male\",\n" + - " \"col1_female\",\n" + - " \"col4\"\n" + - " ],\n" + - " \"tree_structure\": [\n" + - " {\n" + - " \"node_index\": 0,\n" + - " \"split_feature\": 0,\n" + - " \"split_gain\": 12.0,\n" + - " \"threshold\": 10.0,\n" + - " \"decision_type\": \"lte\",\n" + - " \"default_left\": true,\n" + - " \"left_child\": 1,\n" + - " \"right_child\": 2\n" + - " },\n" + - " {\n" + - " \"node_index\": 1,\n" + - " \"leaf_value\": 1\n" + - " },\n" + - " {\n" + - " \"node_index\": 2,\n" + - " \"leaf_value\": 2\n" + - " }\n" + - " ],\n" + - " \"target_type\": \"regression\"\n" + - " }\n" + - " }\n" + - "}"; + public static final String TREE_MODEL = "" + + "{\n" + + " \"preprocessors\": [\n" + + " {\n" + + " \"one_hot_encoding\": {\n" + + " \"field\": \"col1\",\n" + + " \"hot_map\": {\n" + + " \"male\": \"col1_male\",\n" + + " \"female\": \"col1_female\"\n" + + " }\n" + + " }\n" + + " },\n" + + " {\n" + + " \"target_mean_encoding\": {\n" + + " \"field\": \"col2\",\n" + + " \"feature_name\": \"col2_encoded\",\n" + + " \"target_map\": {\n" + + " \"S\": 5.0,\n" + + " \"M\": 10.0,\n" + + " \"L\": 20\n" + + " },\n" + + " \"default_value\": 5.0\n" + + " }\n" + + " },\n" + + " {\n" + + " \"frequency_encoding\": {\n" + + " \"field\": \"col3\",\n" + + " \"feature_name\": \"col3_encoded\",\n" + + " \"frequency_map\": {\n" + + " \"none\": 0.75,\n" + + " \"true\": 0.10,\n" + + " \"false\": 0.15\n" + + " }\n" + + " }\n" + + " }\n" + + " ],\n" + + " \"trained_model\": {\n" + + " \"tree\": {\n" + + " \"feature_names\": [\n" + + " \"col1_male\",\n" + + " \"col1_female\",\n" + + " \"col4\"\n" + + " ],\n" + + " \"tree_structure\": [\n" + + " {\n" + + " \"node_index\": 0,\n" + + " \"split_feature\": 0,\n" + + " \"split_gain\": 12.0,\n" + + " \"threshold\": 10.0,\n" + + " \"decision_type\": \"lte\",\n" + + " \"default_left\": true,\n" + + " \"left_child\": 1,\n" + + " \"right_child\": 2\n" + + " },\n" + + " {\n" + + " \"node_index\": 1,\n" + + " \"leaf_value\": 1\n" + + " },\n" + + " {\n" + + " \"node_index\": 2,\n" + + " \"leaf_value\": 2\n" + + " }\n" + + " ],\n" + + " \"target_type\": \"regression\"\n" + + " }\n" + + " }\n" + + "}"; public void testEnsembleSchemaDeserialization() throws IOException { XContentParser parser = XContentFactory.xContent(XContentType.JSON) diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/TrainedModelInputTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/TrainedModelInputTests.java index d72017d596bd8..3b01dc331e7a4 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/TrainedModelInputTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/TrainedModelInputTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ml.inference; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.junit.Before; import java.io.IOException; @@ -16,7 +16,6 @@ import java.util.stream.Collectors; import java.util.stream.Stream; - public class TrainedModelInputTests extends AbstractSerializingTestCase { private boolean lenient; @@ -42,9 +41,7 @@ protected Predicate getRandomFieldsExcludeFilter() { } public static TrainedModelInput createRandomInput() { - return new TrainedModelInput(Stream.generate(() -> randomAlphaOfLength(10)) - .limit(randomInt(10)) - .collect(Collectors.toList())); + return new TrainedModelInput(Stream.generate(() -> randomAlphaOfLength(10)).limit(randomInt(10)).collect(Collectors.toList())); } @Override diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/TrainedModelTypeTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/TrainedModelTypeTests.java index 009dcf5da794b..dc77455433cd4 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/TrainedModelTypeTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/TrainedModelTypeTests.java @@ -18,8 +18,10 @@ public class TrainedModelTypeTests extends ESTestCase { public void testTypeFromTrainedModel() { { - TrainedModel tm = randomFrom(TreeTests.createRandom(TargetType.CLASSIFICATION), - EnsembleTests.createRandom(TargetType.CLASSIFICATION)); + TrainedModel tm = randomFrom( + TreeTests.createRandom(TargetType.CLASSIFICATION), + EnsembleTests.createRandom(TargetType.CLASSIFICATION) + ); assertEquals(TrainedModelType.TREE_ENSEMBLE, TrainedModelType.typeFromTrainedModel(tm)); } { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/allocation/AllocationStatusTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/allocation/AllocationStatusTests.java index e00b5aa981eff..f13827a51bf90 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/allocation/AllocationStatusTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/allocation/AllocationStatusTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.xpack.core.ml.inference.allocation; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -44,10 +44,7 @@ public void testCalculateState() { equalTo(AllocationStatus.State.STARTED) ); - assertThat( - new AllocationStatus(0, targetAllocation).calculateState(), - equalTo(AllocationStatus.State.STARTING) - ); + assertThat(new AllocationStatus(0, targetAllocation).calculateState(), equalTo(AllocationStatus.State.STARTING)); assertThat( new AllocationStatus(targetAllocation, targetAllocation).calculateState(), diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/allocation/RoutingStateAndReasonTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/allocation/RoutingStateAndReasonTests.java index e803fe98204c5..ca92a976317d8 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/allocation/RoutingStateAndReasonTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/allocation/RoutingStateAndReasonTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.xpack.core.ml.inference.allocation; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/allocation/TrainedModelAllocationTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/allocation/TrainedModelAllocationTests.java index 82ca307f0e024..e8f08ff0e557d 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/allocation/TrainedModelAllocationTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/allocation/TrainedModelAllocationTests.java @@ -190,14 +190,9 @@ public void testCalculateAllocationStatus() { public void testCalculateAllocationState() { List nodes = Stream.generate(TrainedModelAllocationTests::buildNode).limit(5).collect(Collectors.toList()); + assertThat(TrainedModelAllocation.Builder.empty(randomParams()).calculateAllocationState(), equalTo(AllocationState.STARTING)); assertThat( - TrainedModelAllocation.Builder.empty(randomParams()).calculateAllocationState(), - equalTo(AllocationState.STARTING) - ); - assertThat( - TrainedModelAllocation.Builder.empty(randomParams()) - .stopAllocation("test") - .calculateAllocationState(), + TrainedModelAllocation.Builder.empty(randomParams()).stopAllocation("test").calculateAllocationState(), equalTo(AllocationState.STOPPING) ); @@ -235,7 +230,6 @@ public void testCalculateAllocationState() { } } - private static DiscoveryNode buildNode() { return new DiscoveryNode( randomAlphaOfLength(10), diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/CustomWordEmbeddingTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/CustomWordEmbeddingTests.java index 48cae048a57e6..27876b8b3ce9a 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/CustomWordEmbeddingTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/CustomWordEmbeddingTests.java @@ -11,7 +11,6 @@ import java.io.IOException; - public class CustomWordEmbeddingTests extends PreProcessingTests { @Override diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/FrequencyEncodingTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/FrequencyEncodingTests.java index 08c778edaad51..b4821dad4a5dd 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/FrequencyEncodingTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/FrequencyEncodingTests.java @@ -25,9 +25,9 @@ public class FrequencyEncodingTests extends PreProcessingTests instanceReader() { public void testProcessWithFieldPresent() { String field = "categorical"; List values = Arrays.asList("foo", "bar", "foobar", "baz", "farequote", 1.5); - Map valueMap = values.stream().collect(Collectors.toMap(Object::toString, - v -> randomDoubleBetween(0.0, 1.0, false))); + Map valueMap = values.stream() + .collect(Collectors.toMap(Object::toString, v -> randomDoubleBetween(0.0, 1.0, false))); String encodedFeatureName = "encoded"; FrequencyEncoding encoding = new FrequencyEncoding(field, encodedFeatureName, valueMap, false); Object fieldValue = randomFrom(values); - Map> matchers = Collections.singletonMap(encodedFeatureName, - equalTo(valueMap.get(fieldValue.toString()))); + Map> matchers = Collections.singletonMap( + encodedFeatureName, + equalTo(valueMap.get(fieldValue.toString())) + ); Map fieldValues = randomFieldValues(field, fieldValue); testProcess(encoding, fieldValues, matchers); @@ -83,8 +82,8 @@ public void testProcessWithFieldPresent() { public void testInputOutputFields() { String field = randomAlphaOfLength(10); List values = Arrays.asList("foo", "bar", "foobar", "baz", "farequote", 1.5); - Map valueMap = values.stream().collect(Collectors.toMap(Object::toString, - v -> randomDoubleBetween(0.0, 1.0, false))); + Map valueMap = values.stream() + .collect(Collectors.toMap(Object::toString, v -> randomDoubleBetween(0.0, 1.0, false))); String encodedFeatureName = randomAlphaOfLength(10); FrequencyEncoding encoding = new FrequencyEncoding(field, encodedFeatureName, valueMap, false); assertThat(encoding.inputFields(), containsInAnyOrder(field)); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/MultiTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/MultiTests.java index f480cedc9b23b..05abebe37fb7b 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/MultiTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/MultiTests.java @@ -27,9 +27,9 @@ public class MultiTests extends PreProcessingTests { @Override protected Multi doParseInstance(XContentParser parser) throws IOException { - return lenient ? - Multi.fromXContentLenient(parser, PreProcessor.PreProcessorParseContext.DEFAULT) : - Multi.fromXContentStrict(parser, PreProcessor.PreProcessorParseContext.DEFAULT); + return lenient + ? Multi.fromXContentLenient(parser, PreProcessor.PreProcessorParseContext.DEFAULT) + : Multi.fromXContentStrict(parser, PreProcessor.PreProcessorParseContext.DEFAULT); } @Override @@ -52,11 +52,13 @@ public static Multi createRandom(Boolean isCustom) { NGram nGram = NGramTests.createRandom(isCustom); List preProcessorList = new ArrayList<>(); preProcessorList.add(nGram); - Stream.generate(() -> randomFrom( - FrequencyEncodingTests.createRandom(isCustom, randomFrom(nGram.outputFields())), - TargetMeanEncodingTests.createRandom(isCustom, randomFrom(nGram.outputFields())), - OneHotEncodingTests.createRandom(isCustom, randomFrom(nGram.outputFields())) - )).limit(randomIntBetween(1, 10)).forEach(preProcessorList::add); + Stream.generate( + () -> randomFrom( + FrequencyEncodingTests.createRandom(isCustom, randomFrom(nGram.outputFields())), + TargetMeanEncodingTests.createRandom(isCustom, randomFrom(nGram.outputFields())), + OneHotEncodingTests.createRandom(isCustom, randomFrom(nGram.outputFields())) + ) + ).limit(randomIntBetween(1, 10)).forEach(preProcessorList::add); processors = preProcessorList.toArray(PreProcessor[]::new); } else { processors = randomArray( @@ -82,41 +84,37 @@ protected Writeable.Reader instanceReader() { public void testReverseLookup() { String field = "text"; NGram nGram = new NGram(field, Collections.singletonList(1), 0, 2, null, "f"); - OneHotEncoding oneHotEncoding = new OneHotEncoding("f.10", - MapBuilder.newMapBuilder() - .put("a", "has_a") - .put("b", "has_b") - .map(), - true); - Multi multi = new Multi(new PreProcessor[]{nGram, oneHotEncoding}, true); + OneHotEncoding oneHotEncoding = new OneHotEncoding( + "f.10", + MapBuilder.newMapBuilder().put("a", "has_a").put("b", "has_b").map(), + true + ); + Multi multi = new Multi(new PreProcessor[] { nGram, oneHotEncoding }, true); assertThat(multi.reverseLookup(), allOf(hasEntry("has_a", field), hasEntry("has_b", field), hasEntry("f.11", field))); - OneHotEncoding oneHotEncodingOutside = new OneHotEncoding("some_other", - MapBuilder.newMapBuilder() - .put("a", "has_3_a") - .put("b", "has_3_b") - .map(), - true); - multi = new Multi(new PreProcessor[]{nGram, oneHotEncoding, oneHotEncodingOutside}, true); + OneHotEncoding oneHotEncodingOutside = new OneHotEncoding( + "some_other", + MapBuilder.newMapBuilder().put("a", "has_3_a").put("b", "has_3_b").map(), + true + ); + multi = new Multi(new PreProcessor[] { nGram, oneHotEncoding, oneHotEncodingOutside }, true); expectThrows(IllegalArgumentException.class, multi::reverseLookup); } public void testProcessWithFieldPresent() { String field = "text"; NGram nGram = new NGram(field, Collections.singletonList(1), 0, 2, null, "f"); - OneHotEncoding oneHotEncoding1 = new OneHotEncoding("f.10", - MapBuilder.newMapBuilder() - .put("a", "has_a") - .put("b", "has_b") - .map(), - true); - OneHotEncoding oneHotEncoding2 = new OneHotEncoding("f.11", - MapBuilder.newMapBuilder() - .put("a", "has_2_a") - .put("b", "has_2_b") - .map(), - true); - Multi multi = new Multi(new PreProcessor[]{nGram, oneHotEncoding1, oneHotEncoding2}, true); + OneHotEncoding oneHotEncoding1 = new OneHotEncoding( + "f.10", + MapBuilder.newMapBuilder().put("a", "has_a").put("b", "has_b").map(), + true + ); + OneHotEncoding oneHotEncoding2 = new OneHotEncoding( + "f.11", + MapBuilder.newMapBuilder().put("a", "has_2_a").put("b", "has_2_b").map(), + true + ); + Multi multi = new Multi(new PreProcessor[] { nGram, oneHotEncoding1, oneHotEncoding2 }, true); Map fields = randomFieldValues("text", "cat"); multi.process(fields); assertThat(fields, hasEntry("has_a", 0)); @@ -128,38 +126,26 @@ public void testProcessWithFieldPresent() { public void testInputOutputFields() { String field = "text"; NGram nGram = new NGram(field, Collections.singletonList(1), 0, 3, null, "f"); - OneHotEncoding oneHotEncoding1 = new OneHotEncoding("f.10", - MapBuilder.newMapBuilder() - .put("a", "has_a") - .put("b", "has_b") - .map(), - true); - OneHotEncoding oneHotEncoding2 = new OneHotEncoding("f.11", - MapBuilder.newMapBuilder() - .put("a", "has_2_a") - .put("b", "has_2_b") - .map(), - true); - OneHotEncoding oneHotEncoding3 = new OneHotEncoding("some_other", - MapBuilder.newMapBuilder() - .put("a", "has_3_a") - .put("b", "has_3_b") - .map(), - true); - Multi multi = new Multi(new PreProcessor[]{nGram, oneHotEncoding1, oneHotEncoding2, oneHotEncoding3}, true); - assertThat(multi.inputFields(), contains(field, "some_other")); - assertThat(multi.outputFields(), - contains( - "f.12", - "has_a", - "has_b", - "has_2_a", - "has_2_b", - "has_3_a", - "has_3_b") + OneHotEncoding oneHotEncoding1 = new OneHotEncoding( + "f.10", + MapBuilder.newMapBuilder().put("a", "has_a").put("b", "has_b").map(), + true + ); + OneHotEncoding oneHotEncoding2 = new OneHotEncoding( + "f.11", + MapBuilder.newMapBuilder().put("a", "has_2_a").put("b", "has_2_b").map(), + true ); + OneHotEncoding oneHotEncoding3 = new OneHotEncoding( + "some_other", + MapBuilder.newMapBuilder().put("a", "has_3_a").put("b", "has_3_b").map(), + true + ); + Multi multi = new Multi(new PreProcessor[] { nGram, oneHotEncoding1, oneHotEncoding2, oneHotEncoding3 }, true); + assertThat(multi.inputFields(), contains(field, "some_other")); + assertThat(multi.outputFields(), contains("f.12", "has_a", "has_b", "has_2_a", "has_2_b", "has_3_a", "has_3_b")); assertThat(multi.getOutputFieldType("f.12"), equalTo("text")); - for (String fieldName : new String[]{"has_a", "has_b", "has_2_a", "has_2_b", "has_3_a", "has_3_b"}) { + for (String fieldName : new String[] { "has_a", "has_b", "has_2_a", "has_2_b", "has_3_a", "has_3_b" }) { assertThat(multi.getOutputFieldType(fieldName), equalTo("integer")); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/NGramTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/NGramTests.java index 3f1e5bec212f7..932aee55932bd 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/NGramTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/NGramTests.java @@ -17,20 +17,19 @@ import java.util.stream.Collectors; import java.util.stream.IntStream; +import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.nullValue; -import static org.hamcrest.Matchers.contains; - public class NGramTests extends PreProcessingTests { @Override protected NGram doParseInstance(XContentParser parser) throws IOException { - return lenient ? - NGram.fromXContentLenient(parser, PreProcessor.PreProcessorParseContext.DEFAULT) : - NGram.fromXContentStrict(parser, PreProcessor.PreProcessorParseContext.DEFAULT); + return lenient + ? NGram.fromXContentLenient(parser, PreProcessor.PreProcessorParseContext.DEFAULT) + : NGram.fromXContentStrict(parser, PreProcessor.PreProcessorParseContext.DEFAULT); } @Override @@ -50,7 +49,8 @@ public static NGram createRandom(Boolean isCustom) { randomBoolean() ? null : randomIntBetween(0, 10), randomBoolean() ? null : possibleLength, isCustom, - randomBoolean() ? null : randomAlphaOfLength(10)); + randomBoolean() ? null : randomAlphaOfLength(10) + ); } @Override @@ -61,7 +61,7 @@ protected Writeable.Reader instanceReader() { public void testProcessNGramPrefix() { String field = "text"; String fieldValue = "this is the value"; - NGram encoding = new NGram(field, "f", new int[]{1, 4}, 0, 5, false); + NGram encoding = new NGram(field, "f", new int[] { 1, 4 }, 0, 5, false); Map fieldValues = randomFieldValues(field, fieldValue); Map> matchers = new HashMap<>(); @@ -79,7 +79,7 @@ public void testProcessNGramSuffix() { String field = "text"; String fieldValue = "this is the value"; - NGram encoding = new NGram(field, "f", new int[]{1, 3}, -3, 3, false); + NGram encoding = new NGram(field, "f", new int[] { 1, 3 }, -3, 3, false); Map fieldValues = randomFieldValues(field, fieldValue); Map> matchers = new HashMap<>(); matchers.put("f.10", equalTo("l")); @@ -94,7 +94,7 @@ public void testProcessNGramInfix() { String field = "text"; String fieldValue = "this is the value"; - NGram encoding = new NGram(field, "f", new int[]{1, 3}, 3, 3, false); + NGram encoding = new NGram(field, "f", new int[] { 1, 3 }, 3, 3, false); Map fieldValues = randomFieldValues(field, fieldValue); Map> matchers = new HashMap<>(); matchers.put("f.10", equalTo("s")); @@ -109,7 +109,7 @@ public void testProcessNGramLengthOverrun() { String field = "text"; String fieldValue = "this is the value"; - NGram encoding = new NGram(field, "f", new int[]{1, 3}, 12, 10, false); + NGram encoding = new NGram(field, "f", new int[] { 1, 3 }, 12, 10, false); Map fieldValues = randomFieldValues(field, fieldValue); Map> matchers = new HashMap<>(); matchers.put("f.10", equalTo("v")); @@ -125,22 +125,16 @@ public void testProcessNGramLengthOverrun() { public void testInputOutputFields() { String field = randomAlphaOfLength(10); - NGram encoding = new NGram(field, "f", new int[]{1, 4}, 0, 5, false); + NGram encoding = new NGram(field, "f", new int[] { 1, 4 }, 0, 5, false); assertThat(encoding.inputFields(), containsInAnyOrder(field)); - assertThat(encoding.outputFields(), - contains("f.10", "f.11","f.12","f.13","f.14","f.40", "f.41")); + assertThat(encoding.outputFields(), contains("f.10", "f.11", "f.12", "f.13", "f.14", "f.40", "f.41")); encoding = new NGram(field, Arrays.asList(1, 4), 0, 5, false, null); assertThat(encoding.inputFields(), containsInAnyOrder(field)); - assertThat(encoding.outputFields(), - contains( - "ngram_0_5.10", - "ngram_0_5.11", - "ngram_0_5.12", - "ngram_0_5.13", - "ngram_0_5.14", - "ngram_0_5.40", - "ngram_0_5.41")); + assertThat( + encoding.outputFields(), + contains("ngram_0_5.10", "ngram_0_5.11", "ngram_0_5.12", "ngram_0_5.13", "ngram_0_5.14", "ngram_0_5.40", "ngram_0_5.41") + ); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/OneHotEncodingTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/OneHotEncodingTests.java index da75608e46d50..c0c6256831185 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/OneHotEncodingTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/OneHotEncodingTests.java @@ -25,9 +25,9 @@ public class OneHotEncodingTests extends PreProcessingTests { @Override protected OneHotEncoding doParseInstance(XContentParser parser) throws IOException { - return lenient ? - OneHotEncoding.fromXContentLenient(parser, PreProcessor.PreProcessorParseContext.DEFAULT) : - OneHotEncoding.fromXContentStrict(parser, PreProcessor.PreProcessorParseContext.DEFAULT); + return lenient + ? OneHotEncoding.fromXContentLenient(parser, PreProcessor.PreProcessorParseContext.DEFAULT) + : OneHotEncoding.fromXContentStrict(parser, PreProcessor.PreProcessorParseContext.DEFAULT); } @Override @@ -49,9 +49,7 @@ public static OneHotEncoding createRandom(Boolean isCustom, String inputField) { for (int i = 0; i < valuesSize; i++) { valueMap.put(randomAlphaOfLength(10), randomAlphaOfLength(10)); } - return new OneHotEncoding(inputField, - valueMap, - isCustom); + return new OneHotEncoding(inputField, valueMap, isCustom); } @Override @@ -67,10 +65,9 @@ public void testProcessWithFieldPresent() { Object fieldValue = randomFrom(values); Map fieldValues = randomFieldValues(field, fieldValue); - Map> matchers = values.stream().map(v -> "Column_" + v) - .collect(Collectors.toMap( - Function.identity(), - v -> v.equals("Column_" + fieldValue) ? equalTo(1) : equalTo(0))); + Map> matchers = values.stream() + .map(v -> "Column_" + v) + .collect(Collectors.toMap(Function.identity(), v -> v.equals("Column_" + fieldValue) ? equalTo(1) : equalTo(0))); fieldValues.put(field, fieldValue); testProcess(encoding, fieldValues, matchers); @@ -87,8 +84,7 @@ public void testInputOutputFields() { Map valueMap = values.stream().collect(Collectors.toMap(Object::toString, v -> "Column_" + v.toString())); OneHotEncoding encoding = new OneHotEncoding(field, valueMap, false); assertThat(encoding.inputFields(), containsInAnyOrder(field)); - assertThat(encoding.outputFields(), - containsInAnyOrder(values.stream().map(v -> "Column_" + v.toString()).toArray(String[]::new))); + assertThat(encoding.outputFields(), containsInAnyOrder(values.stream().map(v -> "Column_" + v.toString()).toArray(String[]::new))); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/PreProcessingTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/PreProcessingTests.java index 01141e637dc0c..fa23d66b3a4dd 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/PreProcessingTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/PreProcessingTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ml.inference.preprocessing; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.ml.inference.MlInferenceNamedXContentProvider; import org.hamcrest.Matcher; import org.junit.Before; @@ -56,14 +56,13 @@ protected Predicate getRandomFieldsExcludeFilter() { void testProcess(PreProcessor preProcessor, Map fieldValues, Map> assertions) { preProcessor.process(fieldValues); - assertions.forEach((fieldName, matcher) -> - assertThat(fieldValues.get(fieldName), matcher) - ); + assertions.forEach((fieldName, matcher) -> assertThat(fieldValues.get(fieldName), matcher)); } public void testInputOutputFieldOrderConsistency() throws IOException { - xContentTester(this::createParser, this::createXContextTestInstance, getToXContentParams(), this::doParseInstance) - .numberOfTestRuns(NUMBER_OF_TEST_RUNS) + xContentTester(this::createParser, this::createXContextTestInstance, getToXContentParams(), this::doParseInstance).numberOfTestRuns( + NUMBER_OF_TEST_RUNS + ) .supportsUnknownFields(supportsUnknownFields()) .shuffleFieldsExceptions(getShuffleFieldsExceptions()) .randomFieldsExcludeFilter(getRandomFieldsExcludeFilter()) diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/TargetMeanEncodingTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/TargetMeanEncodingTests.java index 500f025711a96..3a581b4b61d0a 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/TargetMeanEncodingTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/TargetMeanEncodingTests.java @@ -25,9 +25,9 @@ public class TargetMeanEncodingTests extends PreProcessingTests instanceReader() { public void testProcessWithFieldPresent() { String field = "categorical"; List values = Arrays.asList("foo", "bar", "foobar", "baz", "farequote", 1.0); - Map valueMap = values.stream().collect(Collectors.toMap(Object::toString, - v -> randomDoubleBetween(0.0, 1.0, false))); + Map valueMap = values.stream() + .collect(Collectors.toMap(Object::toString, v -> randomDoubleBetween(0.0, 1.0, false))); String encodedFeatureName = "encoded"; Double defaultvalue = randomDouble(); TargetMeanEncoding encoding = new TargetMeanEncoding(field, encodedFeatureName, valueMap, defaultvalue, false); Object fieldValue = randomFrom(values); - Map> matchers = Collections.singletonMap(encodedFeatureName, - equalTo(valueMap.get(fieldValue.toString()))); + Map> matchers = Collections.singletonMap( + encodedFeatureName, + equalTo(valueMap.get(fieldValue.toString())) + ); Map fieldValues = randomFieldValues(field, fieldValue); testProcess(encoding, fieldValues, matchers); @@ -86,8 +84,8 @@ public void testInputOutputFields() { String encodedFeatureName = randomAlphaOfLength(10); Double defaultvalue = randomDouble(); List values = Arrays.asList("foo", "bar", "foobar", "baz", "farequote", 1.0); - Map valueMap = values.stream().collect(Collectors.toMap(Object::toString, - v -> randomDoubleBetween(0.0, 1.0, false))); + Map valueMap = values.stream() + .collect(Collectors.toMap(Object::toString, v -> randomDoubleBetween(0.0, 1.0, false))); TargetMeanEncoding encoding = new TargetMeanEncoding(field, encodedFeatureName, valueMap, defaultvalue, false); assertThat(encoding.inputFields(), containsInAnyOrder(field)); assertThat(encoding.outputFields(), containsInAnyOrder(encodedFeatureName)); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/customwordembedding/FeatureUtilsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/customwordembedding/FeatureUtilsTests.java index a7903104b6dbe..61d6e58971540 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/customwordembedding/FeatureUtilsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/customwordembedding/FeatureUtilsTests.java @@ -15,21 +15,23 @@ public class FeatureUtilsTests extends ESTestCase { public void testValidUTF8Length() { { // Truncate to UTF8 boundary (no cut) - String strAZ = " a az qalıb breyn rinq intellektual oyunu üzrə yarışın zona mərhələləri " + - "keçirilib miq un qalıqlarının dənizdən çıxarılması davam edir məhəmməd " + - "peyğəmbərin karikaturalarını"; + String strAZ = " a az qalıb breyn rinq intellektual oyunu üzrə yarışın zona mərhələləri " + + "keçirilib miq un qalıqlarının dənizdən çıxarılması davam edir məhəmməd " + + "peyğəmbərin karikaturalarını"; String truncated = FeatureUtils.truncateToNumValidBytes(strAZ, 200); assertThat(truncated, equalTo(strAZ)); } { // Truncate to UTF8 boundary (cuts) - String strBE = " а друкаваць іх не было тэхнічна магчыма бліжэй за вільню тым самым часам " + - "нямецкае кіраўніцтва прапаноўвала апроч ўвядзення лацінкі яе"; + String strBE = " а друкаваць іх не было тэхнічна магчыма бліжэй за вільню тым самым часам " + + "нямецкае кіраўніцтва прапаноўвала апроч ўвядзення лацінкі яе"; String truncated = FeatureUtils.truncateToNumValidBytes(strBE, 200); - assertThat(truncated, equalTo(" а друкаваць іх не было тэхнічна магчыма бліжэй за вільню тым самым часам " + - "нямецкае кіраўніцтва прапаноўвала ")); + assertThat( + truncated, + equalTo(" а друкаваць іх не было тэхнічна магчыма бліжэй за вільню тым самым часам " + "нямецкае кіраўніцтва прапаноўвала ") + ); } { // Don't truncate @@ -40,35 +42,40 @@ public void testValidUTF8Length() { } { // Truncate to UTF8 boundary (cuts) - String strZH = "产品的简报和公告 提交该申请后无法进行更改 请确认您的选择是正确的 " + - "对于要提交的图书 我确认 我是版权所有者或已得到版权所有者的授权 " + - "要更改您的国家 地区 请在此表的最上端更改您的"; + String strZH = "产品的简报和公告 提交该申请后无法进行更改 请确认您的选择是正确的 " + "对于要提交的图书 我确认 我是版权所有者或已得到版权所有者的授权 " + "要更改您的国家 地区 请在此表的最上端更改您的"; String truncated = FeatureUtils.truncateToNumValidBytes(strZH, 200); - assertThat(truncated, equalTo("产品的简报和公告 提交该申请后无法进行更改 请确认您的选择是正确的 " + - "对于要提交的图书 我确认 我是版权所有者或已得到版权所有者的授权 " + - "要更改")); + assertThat(truncated, equalTo("产品的简报和公告 提交该申请后无法进行更改 请确认您的选择是正确的 " + "对于要提交的图书 我确认 我是版权所有者或已得到版权所有者的授权 " + "要更改")); } } public void testCleanText() { - assertThat(FeatureUtils.cleanAndLowerText("This has a tag in
    it &..///1/2@@3winter"), - equalTo(" this has a tag in br it ssss winter ")); + assertThat( + FeatureUtils.cleanAndLowerText("This has a tag in
    it &..///1/2@@3winter"), + equalTo(" this has a tag in br it ssss winter ") + ); - assertThat(FeatureUtils.cleanAndLowerText(" This has a tag in
    it &..///1/2@@3winter "), - equalTo(" this has a tag in br it ssss winter ")); + assertThat( + FeatureUtils.cleanAndLowerText(" This has a tag in
    it &..///1/2@@3winter "), + equalTo(" this has a tag in br it ssss winter ") + ); - assertThat(FeatureUtils.cleanAndLowerText(" This has a tag in

    it

    &..///1/2@@3winter "), - equalTo(" this has a tag in p it p ssss winter ")); + assertThat( + FeatureUtils.cleanAndLowerText(" This has a tag in

    it

    &..///1/2@@3winter "), + equalTo(" this has a tag in p it p ssss winter ") + ); - assertThat(FeatureUtils.cleanAndLowerText(" This has a tag in \n

    it \r\n

    &..///1/2@@3winter "), - equalTo(" this has a tag in p it p ssss winter ")); + assertThat( + FeatureUtils.cleanAndLowerText(" This has a tag in \n

    it \r\n

    &..///1/2@@3winter "), + equalTo(" this has a tag in p it p ssss winter ") + ); - assertThat(FeatureUtils.cleanAndLowerText(" !This has a tag.in\n+|iW£#

    hello\nit

    &..///1/2@@3winter "), - equalTo(" this has a tag in iw p hello it p ssss winter ")); + assertThat( + FeatureUtils.cleanAndLowerText(" !This has a tag.in\n+|iW£#

    hello\nit

    &..///1/2@@3winter "), + equalTo(" this has a tag in iw p hello it p ssss winter ") + ); assertThat(FeatureUtils.cleanAndLowerText("北京——。"), equalTo(" 北京 ")); - assertThat(FeatureUtils.cleanAndLowerText("北京——中国共产党已为国家主席习近平或许无限期地继续执政扫清了道路。"), - equalTo(" 北京 中国共产党已为国家主席习近平或许无限期地继续执政扫清了道路 ")); + assertThat(FeatureUtils.cleanAndLowerText("北京——中国共产党已为国家主席习近平或许无限期地继续执政扫清了道路。"), equalTo(" 北京 中国共产党已为国家主席习近平或许无限期地继续执政扫清了道路 ")); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/customwordembedding/NGramFeatureExtractorTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/customwordembedding/NGramFeatureExtractorTests.java index afcf4d9e51830..bb17b242832c8 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/customwordembedding/NGramFeatureExtractorTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/customwordembedding/NGramFeatureExtractorTests.java @@ -7,15 +7,15 @@ */ package org.elasticsearch.xpack.core.ml.inference.preprocessing.customwordembedding; -import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.langident.LanguageExamples; import java.io.IOException; @@ -62,74 +62,103 @@ public class NGramFeatureExtractorTests extends ESTestCase { */ private static final Map KNOWN_PROCESSING_FAILURE_LANGUAGES = new HashMap<>(); static { - KNOWN_PROCESSING_FAILURE_LANGUAGES.put("bn", - "এনপির ওয়াক আউট তপন চৌধুরী হারবাল অ্যাসোসিয়েশনের সভাপতি " + - "আন্তর্জাতিক পরামর ালিকপক্ষের কান্না শ্রমিকের অনিশ্চয়তা মতিঝিলে সমাবেশ " + - "নিষিদ্ধ এফবিসিসিআইয় ের গ্র্যান্ডমাস্টার সিজন ব্রাজিলে বিশ্বকাপ ফুটবল" + - " আয়োজনবিরোধী বিক্ষোভ দেশ ের দক্ষতা ও যোগ্যতার পাশাপাশি তারা " + - "জাতীয় ইস্যুগুলোতে প্রাধান্য দিয়েছেন প া যাবে কি একজন দর্শকের " + - "এমন প্রশ্নে জবাবে আব্দুল্লাহ আল নোমান বলেন এই ্রতিন "); - KNOWN_PROCESSING_FAILURE_LANGUAGES.put("bs", - " novi predsjednik mešihata islamske zajednice u srbiji izus i muftija dr mevlud ef dudić izjavio je u intervjuu za anadolu" + - " agency aa kako je uvjeren da će doći do vraćanja jedinstva među muslimanima i unutar islamske zajednice na " + - "prostoru sandžaka te da je njegova ruka pružena za povratak svih u okrilje islamske zajednice u srbiji nakon " + - "skoro sedam godina podjela u tom u srbiji izabran januara a zvanična inauguracija će biti obavljena u prvoj polovini " + - "februara kako se očekuje prisustvovat će joj i reisu l koji će i zvanično promovirati dudića boraviti u prvoj" + - " zvaničnoj posjeti reisu kavazoviću što je njegov privi simbolični potez nakon imenovanja "); - KNOWN_PROCESSING_FAILURE_LANGUAGES.put("fr", - " a accès aux collections et aux frontaux qui lui ont été attribués il peut consulter et modifier ses collections et" + - " exporter des configurations de collection toutefois il ne peut pas aux fonctions "); + KNOWN_PROCESSING_FAILURE_LANGUAGES.put( + "bn", + "এনপির ওয়াক আউট তপন চৌধুরী হারবাল অ্যাসোসিয়েশনের সভাপতি " + + "আন্তর্জাতিক পরামর ালিকপক্ষের কান্না শ্রমিকের অনিশ্চয়তা মতিঝিলে সমাবেশ " + + "নিষিদ্ধ এফবিসিসিআইয় ের গ্র্যান্ডমাস্টার সিজন ব্রাজিলে বিশ্বকাপ ফুটবল" + + " আয়োজনবিরোধী বিক্ষোভ দেশ ের দক্ষতা ও যোগ্যতার পাশাপাশি তারা " + + "জাতীয় ইস্যুগুলোতে প্রাধান্য দিয়েছেন প া যাবে কি একজন দর্শকের " + + "এমন প্রশ্নে জবাবে আব্দুল্লাহ আল নোমান বলেন এই ্রতিন " + ); + KNOWN_PROCESSING_FAILURE_LANGUAGES.put( + "bs", + " novi predsjednik mešihata islamske zajednice u srbiji izus i muftija dr mevlud ef dudić izjavio je u intervjuu za anadolu" + + " agency aa kako je uvjeren da će doći do vraćanja jedinstva među muslimanima i unutar islamske zajednice na " + + "prostoru sandžaka te da je njegova ruka pružena za povratak svih u okrilje islamske zajednice u srbiji nakon " + + "skoro sedam godina podjela u tom u srbiji izabran januara a zvanična inauguracija će biti obavljena u prvoj polovini " + + "februara kako se očekuje prisustvovat će joj i reisu l koji će i zvanično promovirati dudića boraviti u prvoj" + + " zvaničnoj posjeti reisu kavazoviću što je njegov privi simbolični potez nakon imenovanja " + ); + KNOWN_PROCESSING_FAILURE_LANGUAGES.put( + "fr", + " a accès aux collections et aux frontaux qui lui ont été attribués il peut consulter et modifier ses collections et" + + " exporter des configurations de collection toutefois il ne peut pas aux fonctions " + ); KNOWN_PROCESSING_FAILURE_LANGUAGES.put("ha", " a cikin a kan sakamako daga sakwannin a kan kafar "); KNOWN_PROCESSING_FAILURE_LANGUAGES.put("hi", " ं ऐडवर्ड्स विज्ञापनों के अनुभव पर आधारित हैं और इनकी मदद से आपको अपने "); - KNOWN_PROCESSING_FAILURE_LANGUAGES.put("hr", - " posljednja dva vladara su kijaksar κυαξαρης prije krista fraortov sin koji će proširiti teritorij medije i " + - "astijag kijaksar je imao kćer ili unuku koja se zvala amitis a postala je ženom nabukodonosora ii kojoj je " + - "ovaj izgradio viseće vrtove babilona kijaksar je modernizirao svoju vojsku i uništio ninivu prije krista naslijedio " + - "ga je njegov sin posljednji medijski kralj astijag kojega je detronizirao srušio sa vlasti njegov unuk kir veliki" + - " zemljom su zavladali perzijanci hrvatska je zemlja situacija u europi ona ima bogatu kulturu i ukusna jela "); - KNOWN_PROCESSING_FAILURE_LANGUAGES.put("ht", - " ak pitit tout sosyete a chita se pou sa leta dwe pwoteje yo nimewo leta fèt pou li pwoteje tout paran ak pitit nan " + - "peyi a menm jan kit paran yo marye kit yo pa marye tout manman ki fè piti ak pou "); - KNOWN_PROCESSING_FAILURE_LANGUAGES.put("hu", - " a felhasználóim a google azonosító szöveget fogják látni minden tranzakció után ha a vásárlását regisztrációját oldalunk "); - KNOWN_PROCESSING_FAILURE_LANGUAGES.put("ig", " chineke bụ aha ọzọ ndï omenala igbo kpọro chukwu mgbe ndị bekee bịara ha " + - "mee ya nke ndi christian n echiche ndi ekpere chi omenala ndi igbo christianity judaism ma islam chineke nwere ọtụtụ " + - "utu aha ma nwee nanị otu aha ụzọ abụọ e si akpọ aha ahụ bụ jehovah ma ọ bụ yahweh na ọtụtụ akwụkwọ nsọ e wepụla aha" + - " chineke ma jiri utu aha bụ onyenwe anyị ma ọ bụ chineke dochie ya pụtara n ime ya ihe dị ka ugboro pụkụ asaa "); - KNOWN_PROCESSING_FAILURE_LANGUAGES.put("is", - " a afköst leitarorða þinna leitarorð neikvæð leitarorð auglýsingahópa byggja upp og skoða ítarleg gögn um árangur " + - "leitarorða eins og samkeppni auglýsenda og leitarmagn er krafist notkun "); - KNOWN_PROCESSING_FAILURE_LANGUAGES.put("mi", - " haere ki te kainga o o haere ki te kainga o o kainga o ka tangohia he ki to rapunga kaore au mohio te tikanga" + - " whakatiki o te ra he nga awhina o te "); - KNOWN_PROCESSING_FAILURE_LANGUAGES.put("mn", - " а боловсронгуй болгох орон нутгийн ажил үйлсийг уялдуулж зохицуулах дүрэм өмч хөрөнгө санхүүгийн "); - KNOWN_PROCESSING_FAILURE_LANGUAGES.put("mr", - " हैदराबाद उच्चार ऐका सहाय्य माहिती तेलुगू హైదరాబాదు उर्दू حیدر آباد हे भारतातील" + - " आंध्र प्रदेश राज्याच्या राजधानीचे शहर आहे हैदराबादची लोकसंख्या " + - "लाख हजार आहे मोत्यांचे शहर अशी एकेकाळी ओळख असलेल्या या शहराला ऐतिहासिक " + - "सांस्कृतिक आणि स्थापत्यशास्त्रीय वारसा लाभला आहे नंतर त्याचप्रमाणे " + - "औषधनिर्मिती आणि उद्योगधंद्यांची वाढ शहरात झाली दक्षिण मध्य तेलुगू केंद्र आहे "); - KNOWN_PROCESSING_FAILURE_LANGUAGES.put("ms", - " pengampunan beramai ramai supaya mereka pulang ke rumah masing masing orang orang besarnya enggan " + - "mengiktiraf sultan yang dilantik oleh belanda sebagai yang dipertuan selangor orang ramai pula " + - "tidak mahu menjalankan perniagaan bijih timah dengan belanda selagi raja yang berhak tidak ditabalkan " + - "perdagang yang lain dibekukan terus kerana untuk membalas jasa beliau yang membantu belanda menentang " + - "riau johor dan selangor di antara tiga orang sultan juga dipandang oleh rakyat ganti sultan ibrahim ditabalkan" + - " raja muhammad iaitu raja muda walaupun baginda bukan anak isteri pertama bergelar sultan muhammad " + - "ioleh cina di lukut tidak diambil tindakan sedangkan baginda sendiri banyak berhutang kepada "); - KNOWN_PROCESSING_FAILURE_LANGUAGES.put("ne", - " अरू ठाऊँबाटपनि खुलेको छ यो खाता अर "); - KNOWN_PROCESSING_FAILURE_LANGUAGES.put("pa", - " ਂ ਦਿਨਾਂ ਵਿਚ ਭਾਈ ਸਾਹਿਬ ਦੀ ਬੁੱਚੜ ਗੋਬਿੰਦ ਰਾਮ ਨਾਲ ਅੜਫਸ ਚੱਲ "); - KNOWN_PROCESSING_FAILURE_LANGUAGES.put("tg", - " адолат ва инсондӯстиро бар фашизм нажодпарастӣ ва адоват тарҷеҳ додааст чоп кунед ба дигарон фиристед "); - KNOWN_PROCESSING_FAILURE_LANGUAGES.put("tr", - " a ayarlarınızı görmeniz ve yönetmeniz içindir eğer kampanyanız için günlük bütçenizi gözden geçirebileceğiniz " + - "yeri ve kampanya ayarlarını düzenle yi tıklayın sunumu "); - KNOWN_PROCESSING_FAILURE_LANGUAGES.put("zu", - " ana engu uma inkinga iqhubeka siza ubike kwi isexwayiso ngenxa yephutha lomlekeleli sikwazi ukubuyisela " + - "emuva kuphela imiphumela engaqediwe ukuthola imiphumela eqediwe zama ukulayisha siza uthumele "); + KNOWN_PROCESSING_FAILURE_LANGUAGES.put( + "hr", + " posljednja dva vladara su kijaksar κυαξαρης prije krista fraortov sin koji će proširiti teritorij medije i " + + "astijag kijaksar je imao kćer ili unuku koja se zvala amitis a postala je ženom nabukodonosora ii kojoj je " + + "ovaj izgradio viseće vrtove babilona kijaksar je modernizirao svoju vojsku i uništio ninivu prije krista naslijedio " + + "ga je njegov sin posljednji medijski kralj astijag kojega je detronizirao srušio sa vlasti njegov unuk kir veliki" + + " zemljom su zavladali perzijanci hrvatska je zemlja situacija u europi ona ima bogatu kulturu i ukusna jela " + ); + KNOWN_PROCESSING_FAILURE_LANGUAGES.put( + "ht", + " ak pitit tout sosyete a chita se pou sa leta dwe pwoteje yo nimewo leta fèt pou li pwoteje tout paran ak pitit nan " + + "peyi a menm jan kit paran yo marye kit yo pa marye tout manman ki fè piti ak pou " + ); + KNOWN_PROCESSING_FAILURE_LANGUAGES.put( + "hu", + " a felhasználóim a google azonosító szöveget fogják látni minden tranzakció után ha a vásárlását regisztrációját oldalunk " + ); + KNOWN_PROCESSING_FAILURE_LANGUAGES.put( + "ig", + " chineke bụ aha ọzọ ndï omenala igbo kpọro chukwu mgbe ndị bekee bịara ha " + + "mee ya nke ndi christian n echiche ndi ekpere chi omenala ndi igbo christianity judaism ma islam chineke nwere ọtụtụ " + + "utu aha ma nwee nanị otu aha ụzọ abụọ e si akpọ aha ahụ bụ jehovah ma ọ bụ yahweh na ọtụtụ akwụkwọ nsọ e wepụla aha" + + " chineke ma jiri utu aha bụ onyenwe anyị ma ọ bụ chineke dochie ya pụtara n ime ya ihe dị ka ugboro pụkụ asaa " + ); + KNOWN_PROCESSING_FAILURE_LANGUAGES.put( + "is", + " a afköst leitarorða þinna leitarorð neikvæð leitarorð auglýsingahópa byggja upp og skoða ítarleg gögn um árangur " + + "leitarorða eins og samkeppni auglýsenda og leitarmagn er krafist notkun " + ); + KNOWN_PROCESSING_FAILURE_LANGUAGES.put( + "mi", + " haere ki te kainga o o haere ki te kainga o o kainga o ka tangohia he ki to rapunga kaore au mohio te tikanga" + + " whakatiki o te ra he nga awhina o te " + ); + KNOWN_PROCESSING_FAILURE_LANGUAGES.put( + "mn", + " а боловсронгуй болгох орон нутгийн ажил үйлсийг уялдуулж зохицуулах дүрэм өмч хөрөнгө санхүүгийн " + ); + KNOWN_PROCESSING_FAILURE_LANGUAGES.put( + "mr", + " हैदराबाद उच्चार ऐका सहाय्य माहिती तेलुगू హైదరాబాదు उर्दू حیدر آباد हे भारतातील" + + " आंध्र प्रदेश राज्याच्या राजधानीचे शहर आहे हैदराबादची लोकसंख्या " + + "लाख हजार आहे मोत्यांचे शहर अशी एकेकाळी ओळख असलेल्या या शहराला ऐतिहासिक " + + "सांस्कृतिक आणि स्थापत्यशास्त्रीय वारसा लाभला आहे नंतर त्याचप्रमाणे " + + "औषधनिर्मिती आणि उद्योगधंद्यांची वाढ शहरात झाली दक्षिण मध्य तेलुगू केंद्र आहे " + ); + KNOWN_PROCESSING_FAILURE_LANGUAGES.put( + "ms", + " pengampunan beramai ramai supaya mereka pulang ke rumah masing masing orang orang besarnya enggan " + + "mengiktiraf sultan yang dilantik oleh belanda sebagai yang dipertuan selangor orang ramai pula " + + "tidak mahu menjalankan perniagaan bijih timah dengan belanda selagi raja yang berhak tidak ditabalkan " + + "perdagang yang lain dibekukan terus kerana untuk membalas jasa beliau yang membantu belanda menentang " + + "riau johor dan selangor di antara tiga orang sultan juga dipandang oleh rakyat ganti sultan ibrahim ditabalkan" + + " raja muhammad iaitu raja muda walaupun baginda bukan anak isteri pertama bergelar sultan muhammad " + + "ioleh cina di lukut tidak diambil tindakan sedangkan baginda sendiri banyak berhutang kepada " + ); + KNOWN_PROCESSING_FAILURE_LANGUAGES.put("ne", " अरू ठाऊँबाटपनि खुलेको छ यो खाता अर "); + KNOWN_PROCESSING_FAILURE_LANGUAGES.put("pa", " ਂ ਦਿਨਾਂ ਵਿਚ ਭਾਈ ਸਾਹਿਬ ਦੀ ਬੁੱਚੜ ਗੋਬਿੰਦ ਰਾਮ ਨਾਲ ਅੜਫਸ ਚੱਲ "); + KNOWN_PROCESSING_FAILURE_LANGUAGES.put( + "tg", + " адолат ва инсондӯстиро бар фашизм нажодпарастӣ ва адоват тарҷеҳ додааст чоп кунед ба дигарон фиристед " + ); + KNOWN_PROCESSING_FAILURE_LANGUAGES.put( + "tr", + " a ayarlarınızı görmeniz ve yönetmeniz içindir eğer kampanyanız için günlük bütçenizi gözden geçirebileceğiniz " + + "yeri ve kampanya ayarlarını düzenle yi tıklayın sunumu " + ); + KNOWN_PROCESSING_FAILURE_LANGUAGES.put( + "zu", + " ana engu uma inkinga iqhubeka siza ubike kwi isexwayiso ngenxa yephutha lomlekeleli sikwazi ukubuyisela " + + "emuva kuphela imiphumela engaqediwe ukuthola imiphumela eqediwe zama ukulayisha siza uthumele " + ); } private String processedText(String language, List languages) { @@ -161,7 +190,7 @@ public void testExtractor() throws Exception { .toArray(Integer[]::new); double[] orderedWeights = new double[entry.weights.length]; int[] orderedIds = new int[entry.ids.length]; - for(int i = 0; i < entry.nGrams.length; i++) { + for (int i = 0; i < entry.nGrams.length; i++) { orderedIds[i] = entry.ids[sortedIndices[i]]; orderedWeights[i] = entry.weights[sortedIndices[i]]; } @@ -173,19 +202,21 @@ public void testExtractor() throws Exception { String msg = "for language [" + entry.language + "] dimension [" + entry.dimension + "] ngrams [" + entry.nGramSize + "]"; assertThat("weights length mismatch " + msg, extractedWeights.length, equalTo(entry.weights.length)); assertThat("ids length mismatch " + msg, extractedIds.length, equalTo(entry.ids.length)); - for(int i = 0; i < extractedIds.length; i++) { + for (int i = 0; i < extractedIds.length; i++) { String assertMessage = "ids mismatch for id [" + i + "] and " + msg; assertThat(assertMessage, extractedIds[i], equalTo(orderedIds[i])); } double eps = 0.00001; - for(int i = 0; i < extractedWeights.length; i++) { - String assertMessage = " Difference [" + Math.abs(extractedWeights[i] - entry.weights[i]) + "]" + - "weights mismatch for weight [" + i + "] and " + msg; - assertThat( - assertMessage, - extractedWeights[i], - closeTo(orderedWeights[i], eps)); + for (int i = 0; i < extractedWeights.length; i++) { + String assertMessage = " Difference [" + + Math.abs(extractedWeights[i] - entry.weights[i]) + + "]" + + "weights mismatch for weight [" + + i + + "] and " + + msg; + assertThat(assertMessage, extractedWeights[i], closeTo(orderedWeights[i], eps)); } } @@ -196,12 +227,16 @@ public void testExtractor() throws Exception { */ public List getGoldenNGrams() throws Exception { String path = "/org/elasticsearch/xpack/core/ml/inference/ngram_examples.json"; - try(XContentParser parser = XContentType.JSON.xContent().createParser( - NamedXContentRegistry.EMPTY, - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, - Files.newInputStream(getDataPath(path))) ) { + try ( + XContentParser parser = XContentType.JSON.xContent() + .createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + Files.newInputStream(getDataPath(path)) + ) + ) { List entries = new ArrayList<>(); - while(parser.nextToken() != XContentParser.Token.END_ARRAY) { + while (parser.nextToken() != XContentParser.Token.END_ARRAY) { entries.add(NGramFeatureExtractorTests.NGramExampleEntry.PARSER.apply(parser, null)); } return entries; @@ -220,7 +255,8 @@ public static class NGramExampleEntry implements ToXContentObject { public static ObjectParser PARSER = new ObjectParser<>( "ngram_example_entry", true, - NGramExampleEntry::new); + NGramExampleEntry::new + ); static { PARSER.declareString(NGramExampleEntry::setLanguage, LANGUAGE); @@ -277,7 +313,6 @@ public void setnGrams(List nGrams) { setnGrams(nGrams.toArray(new String[0])); } - @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/customwordembedding/RelevantScriptFeatureExtractorTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/customwordembedding/RelevantScriptFeatureExtractorTests.java index 1494c42bfbcb9..5867c2886f3f5 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/customwordembedding/RelevantScriptFeatureExtractorTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/customwordembedding/RelevantScriptFeatureExtractorTests.java @@ -94,7 +94,8 @@ public void testRelevantScriptFeatureCornerCases() { results = extractor.extractFeatures("8*1ゟ12----"); assertThat(results.length, equalTo(1)); assertThat(results[0].getRow(), equalTo(ScriptDetector.Script.kScriptHiragana.toInt())); - assertThat(results[0].getWeight(), closeTo(1.0, eps));; + assertThat(results[0].getWeight(), closeTo(1.0, eps)); + ; } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/customwordembedding/ScriptDetectorTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/customwordembedding/ScriptDetectorTests.java index ad4e88472cdd1..e4f0c8b3ba78a 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/customwordembedding/ScriptDetectorTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/customwordembedding/ScriptDetectorTests.java @@ -75,9 +75,9 @@ public void testOtherOneByte() { } public void testOtherTwoBytes() { - // Unrecognized 2-byte scripts. For info on the scripts mentioned below, see + // Unrecognized 2-byte scripts. For info on the scripts mentioned below, see // http://www.unicode.org/charts/#scripts Note: the scripts below are uniquely - // associated with a language. Still, the number of queries in those + // associated with a language. Still, the number of queries in those // languages is small and we didn't want to increase the code size and // latency, so (at least for now) we do not treat them specially. // The following three tests are, respectively, for Armenian, Syriac and diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/customwordembedding/ScriptFeatureExtractorTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/customwordembedding/ScriptFeatureExtractorTests.java index aaca58405af6b..a4af37eaf212c 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/customwordembedding/ScriptFeatureExtractorTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/preprocessing/customwordembedding/ScriptFeatureExtractorTests.java @@ -35,9 +35,97 @@ public void testSimpleJa() { public void testAllExamples() throws IOException { // compare against cld3 expected text type - long[] expected = new long[]{1, 6, 1, 3, 3, 10, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 6, 1, 1, 1, 1, 1, 12, 1, 9, 1, 1, 1, 1, - 4, 1, 1, 1, 1, 5, 24, 1, 23, 3, 30, 16, 102, 1, 20, 1, 1, 1, 1, 3, 17, 3, 9, 1, 1, 22, 9, 1, 1, 1, 11, 1, 1, 1, 3, 18, 1, - 1, 1, 1, 3, 1, 1, 1, 1, 14, 15, 3, 19, 1, 3, 6, 1, 1, 5, 1, 24, 1}; + long[] expected = new long[] { + 1, + 6, + 1, + 3, + 3, + 10, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 2, + 1, + 1, + 1, + 1, + 1, + 6, + 1, + 1, + 1, + 1, + 1, + 12, + 1, + 9, + 1, + 1, + 1, + 1, + 4, + 1, + 1, + 1, + 1, + 5, + 24, + 1, + 23, + 3, + 30, + 16, + 102, + 1, + 20, + 1, + 1, + 1, + 1, + 3, + 17, + 3, + 9, + 1, + 1, + 22, + 9, + 1, + 1, + 1, + 11, + 1, + 1, + 1, + 3, + 18, + 1, + 1, + 1, + 1, + 3, + 1, + 1, + 1, + 1, + 14, + 15, + 3, + 19, + 1, + 3, + 6, + 1, + 1, + 5, + 1, + 24, + 1 }; List entries = new LanguageExamples().getLanguageExamples(); assertEquals(expected.length, entries.size()); @@ -49,4 +137,3 @@ public void testAllExamples() throws IOException { } } - diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/results/ClassificationFeatureImportanceTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/results/ClassificationFeatureImportanceTests.java index a47a4a5bcf0cc..60859132ce56f 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/results/ClassificationFeatureImportanceTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/results/ClassificationFeatureImportanceTests.java @@ -7,9 +7,9 @@ package org.elasticsearch.xpack.core.ml.inference.results; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.Arrays; @@ -49,7 +49,8 @@ public static ClassificationFeatureImportance createRandomInstance() { Stream.generate(classNameGenerator) .limit(randomLongBetween(2, 10)) .map(name -> new ClassificationFeatureImportance.ClassImportance(name, randomDoubleBetween(-10, 10, false))) - .collect(Collectors.toList())); + .collect(Collectors.toList()) + ); } public void testGetTotalImportance_GivenBinary() { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/results/ClassificationInferenceResultsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/results/ClassificationInferenceResultsTests.java index 5bc9295ec01a7..8b0b2ee995173 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/results/ClassificationInferenceResultsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/results/ClassificationInferenceResultsTests.java @@ -35,19 +35,23 @@ public static ClassificationInferenceResults createRandomResults() { value = randomBoolean() ? 0.0 : 1.0; } - return new ClassificationInferenceResults(value, + return new ClassificationInferenceResults( + value, randomBoolean() ? null : randomAlphaOfLength(10), - randomBoolean() ? null : - Stream.generate(TopClassEntryTests::createRandomTopClassEntry) + randomBoolean() + ? null + : Stream.generate(TopClassEntryTests::createRandomTopClassEntry) .limit(randomIntBetween(0, 10)) .collect(Collectors.toList()), - randomBoolean() ? null : - Stream.generate(ClassificationFeatureImportanceTests::createRandomInstance) + randomBoolean() + ? null + : Stream.generate(ClassificationFeatureImportanceTests::createRandomInstance) .limit(randomIntBetween(1, 10)) .collect(Collectors.toList()), config, randomBoolean() ? null : randomDoubleBetween(0.0, 1.0, false), - randomBoolean() ? null : randomDoubleBetween(0.0, 1.0, false)); + randomBoolean() ? null : randomDoubleBetween(0.0, 1.0, false) + ); } @SuppressWarnings("unchecked") @@ -55,14 +59,17 @@ public void testWriteResultsWithTopClasses() { List entries = Arrays.asList( new TopClassEntry("foo", 0.7, 0.7), new TopClassEntry("bar", 0.2, 0.2), - new TopClassEntry("baz", 0.1, 0.1)); - ClassificationInferenceResults result = new ClassificationInferenceResults(1.0, + new TopClassEntry("baz", 0.1, 0.1) + ); + ClassificationInferenceResults result = new ClassificationInferenceResults( + 1.0, "foo", entries, Collections.emptyList(), new ClassificationConfig(3, "my_results", "bar", null, PredictionFieldType.STRING), 0.7, - 0.7); + 0.7 + ); IngestDocument document = new IngestDocument(new HashMap<>(), new HashMap<>()); writeResult(result, document, "result_field", "test"); @@ -81,13 +88,15 @@ public void testWriteResultsWithImportance() { List importanceList = Stream.generate(ClassificationFeatureImportanceTests::createRandomInstance) .limit(5) .collect(Collectors.toList()); - ClassificationInferenceResults result = new ClassificationInferenceResults(0.0, + ClassificationInferenceResults result = new ClassificationInferenceResults( + 0.0, "foo", Collections.emptyList(), importanceList, new ClassificationConfig(0, "predicted_value", "top_classes", 3, PredictionFieldType.STRING), 1.0, - 1.0); + 1.0 + ); IngestDocument document = new IngestDocument(new HashMap<>(), new HashMap<>()); writeResult(result, document, "result_field", "test"); @@ -95,7 +104,8 @@ public void testWriteResultsWithImportance() { @SuppressWarnings("unchecked") List> writtenImportance = (List>) document.getFieldValue( "result_field.feature_importance", - List.class); + List.class + ); assertThat(writtenImportance, hasSize(3)); importanceList.sort((l, r) -> Double.compare(Math.abs(r.getTotalImportance()), Math.abs(l.getTotalImportance()))); for (int i = 0; i < 3; i++) { @@ -103,7 +113,7 @@ public void testWriteResultsWithImportance() { ClassificationFeatureImportance importance = importanceList.get(i); assertThat(objectMap.get("feature_name"), equalTo(importance.getFeatureName())); @SuppressWarnings("unchecked") - List> classImportances = (List>)objectMap.get("classes"); + List> classImportances = (List>) objectMap.get("classes"); if (importance.getClassImportance() != null) { for (int j = 0; j < importance.getClassImportance().size(); j++) { Map classMap = classImportances.get(j); @@ -127,62 +137,56 @@ protected Writeable.Reader instanceReader() { public void testToXContent() { ClassificationConfig toStringConfig = new ClassificationConfig(1, null, null, null, PredictionFieldType.STRING); - ClassificationInferenceResults result = new ClassificationInferenceResults(1.0, + ClassificationInferenceResults result = new ClassificationInferenceResults( + 1.0, null, null, Collections.emptyList(), toStringConfig, 1.0, - 1.0); + 1.0 + ); String stringRep = Strings.toString(result); String expected = "{\"predicted_value\":\"1.0\",\"prediction_probability\":1.0,\"prediction_score\":1.0}"; assertEquals(expected, stringRep); ClassificationConfig toDoubleConfig = new ClassificationConfig(1, null, null, null, PredictionFieldType.NUMBER); - result = new ClassificationInferenceResults(1.0, null, null, Collections.emptyList(), toDoubleConfig, - 1.0, - 1.0); + result = new ClassificationInferenceResults(1.0, null, null, Collections.emptyList(), toDoubleConfig, 1.0, 1.0); stringRep = Strings.toString(result); expected = "{\"predicted_value\":1.0,\"prediction_probability\":1.0,\"prediction_score\":1.0}"; assertEquals(expected, stringRep); ClassificationConfig boolFieldConfig = new ClassificationConfig(1, null, null, null, PredictionFieldType.BOOLEAN); - result = new ClassificationInferenceResults(1.0, null, null, Collections.emptyList(), boolFieldConfig, - 1.0, - 1.0); + result = new ClassificationInferenceResults(1.0, null, null, Collections.emptyList(), boolFieldConfig, 1.0, 1.0); stringRep = Strings.toString(result); expected = "{\"predicted_value\":true,\"prediction_probability\":1.0,\"prediction_score\":1.0}"; assertEquals(expected, stringRep); ClassificationConfig config = new ClassificationConfig(1); - result = new ClassificationInferenceResults(1.0, "label1", null, Collections.emptyList(), config, - 1.0, - 1.0); + result = new ClassificationInferenceResults(1.0, "label1", null, Collections.emptyList(), config, 1.0, 1.0); stringRep = Strings.toString(result); expected = "{\"predicted_value\":\"label1\",\"prediction_probability\":1.0,\"prediction_score\":1.0}"; assertEquals(expected, stringRep); ClassificationFeatureImportance fi = new ClassificationFeatureImportance("foo", Collections.emptyList()); TopClassEntry tp = new TopClassEntry("class", 1.0, 1.0); - result = new ClassificationInferenceResults(1.0, "label1", Collections.singletonList(tp), - Collections.singletonList(fi), config, + result = new ClassificationInferenceResults( + 1.0, + "label1", + Collections.singletonList(tp), + Collections.singletonList(fi), + config, 1.0, - 1.0); + 1.0 + ); stringRep = Strings.toString(result); - expected = "{\"predicted_value\":\"label1\"," + - "\"top_classes\":[{\"class_name\":\"class\",\"class_probability\":1.0,\"class_score\":1.0}]," + - "\"prediction_probability\":1.0,\"prediction_score\":1.0}"; + expected = "{\"predicted_value\":\"label1\"," + + "\"top_classes\":[{\"class_name\":\"class\",\"class_probability\":1.0,\"class_score\":1.0}]," + + "\"prediction_probability\":1.0,\"prediction_score\":1.0}"; assertEquals(expected, stringRep); - config = new ClassificationConfig(0); - result = new ClassificationInferenceResults(1.0, - "label1", - Collections.emptyList(), - Collections.emptyList(), - config, - 1.0, - 1.0); + result = new ClassificationInferenceResults(1.0, "label1", Collections.emptyList(), Collections.emptyList(), config, 1.0, 1.0); stringRep = Strings.toString(result); expected = "{\"predicted_value\":\"label1\",\"prediction_probability\":1.0,\"prediction_score\":1.0}"; assertEquals(expected, stringRep); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/results/FillMaskResultsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/results/FillMaskResultsTests.java index d976072ada224..b87c9d7bc9efb 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/results/FillMaskResultsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/results/FillMaskResultsTests.java @@ -32,7 +32,7 @@ protected Writeable.Reader instanceReader() { protected FillMaskResults createTestInstance() { int numResults = randomIntBetween(0, 3); List resultList = new ArrayList<>(); - for (int i=0; i> resultList = (List>)asMap.get(DEFAULT_TOP_CLASSES_RESULTS_FIELD); + List> resultList = (List>) asMap.get(DEFAULT_TOP_CLASSES_RESULTS_FIELD); if (testInstance.getTopClasses().size() == 0) { assertThat(resultList, is(nullValue())); } else { assertThat(resultList, hasSize(testInstance.getTopClasses().size())); - for (int i = 0; i map = resultList.get(i); assertThat(map.get("class_score"), equalTo(result.getScore())); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/results/InferenceResultsTestCase.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/results/InferenceResultsTestCase.java index a6eadd1bbd0ad..2a11c2f8445d2 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/results/InferenceResultsTestCase.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/results/InferenceResultsTestCase.java @@ -8,10 +8,10 @@ package org.elasticsearch.xpack.core.ml.inference.results; import org.elasticsearch.Version; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.ingest.IngestDocument; import org.elasticsearch.test.AbstractWireSerializingTestCase; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; import java.io.IOException; import java.util.HashMap; @@ -20,7 +20,7 @@ abstract class InferenceResultsTestCase extends AbstractWireSerializingTestCase { public void testWriteToIngestDoc() throws IOException { - for (int i = 0 ; i < NUMBER_OF_TEST_RUNS; ++i) { + for (int i = 0; i < NUMBER_OF_TEST_RUNS; ++i) { T inferenceResult = createTestInstance(); if (randomBoolean()) { inferenceResult = copyInstance(inferenceResult, Version.CURRENT); @@ -40,7 +40,7 @@ public void testWriteToIngestDoc() throws IOException { abstract void assertFieldValues(T createdInstance, IngestDocument document, String resultsField); public void testWriteToDocAndSerialize() throws IOException { - for (int i = 0 ; i < NUMBER_OF_TEST_RUNS; ++i) { + for (int i = 0; i < NUMBER_OF_TEST_RUNS; ++i) { T inferenceResult = createTestInstance(); if (randomBoolean()) { inferenceResult = copyInstance(inferenceResult, Version.CURRENT); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/results/NerResultsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/results/NerResultsTests.java index a5ab1147fd520..a3ce28f3d46a9 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/results/NerResultsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/results/NerResultsTests.java @@ -40,8 +40,7 @@ protected NerResults createTestInstance() { randomIntBetween(-1, 5), randomIntBetween(5, 10) ) - ).limit(numEntities) - .collect(Collectors.toList()) + ).limit(numEntities).collect(Collectors.toList()) ); } @@ -49,7 +48,7 @@ protected NerResults createTestInstance() { public void testAsMap() { NerResults testInstance = createTestInstance(); Map asMap = testInstance.asMap(); - List> resultList = (List>)asMap.get(ENTITY_FIELD); + List> resultList = (List>) asMap.get(ENTITY_FIELD); if (resultList == null) { return; } @@ -61,8 +60,8 @@ public void testAsMap() { assertThat(map.get(NerResults.EntityGroup.CLASS_NAME), equalTo(entity.getClassName())); assertThat(map.get("entity"), equalTo(entity.getEntity())); assertThat(map.get(NerResults.EntityGroup.CLASS_PROBABILITY), equalTo(entity.getClassProbability())); - Integer startPos = (Integer)map.get(NerResults.EntityGroup.START_POS); - Integer endPos = (Integer)map.get(NerResults.EntityGroup.END_POS); + Integer startPos = (Integer) map.get(NerResults.EntityGroup.START_POS); + Integer endPos = (Integer) map.get(NerResults.EntityGroup.END_POS); if (startPos != null) { assertThat(startPos, equalTo(entity.getStartPos())); } @@ -92,8 +91,8 @@ void assertFieldValues(NerResults createdInstance, IngestDocument document, Stri assertThat(map.get(NerResults.EntityGroup.CLASS_NAME), equalTo(entity.getClassName())); assertThat(map.get("entity"), equalTo(entity.getEntity())); assertThat(map.get(NerResults.EntityGroup.CLASS_PROBABILITY), equalTo(entity.getClassProbability())); - Integer startPos = (Integer)map.get(NerResults.EntityGroup.START_POS); - Integer endPos = (Integer)map.get(NerResults.EntityGroup.END_POS); + Integer startPos = (Integer) map.get(NerResults.EntityGroup.START_POS); + Integer endPos = (Integer) map.get(NerResults.EntityGroup.END_POS); if (startPos != null) { assertThat(startPos, equalTo(entity.getStartPos())); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/results/PyTorchPassThroughResultsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/results/PyTorchPassThroughResultsTests.java index 9a48cf72c0f4b..70590dd6d8ee4 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/results/PyTorchPassThroughResultsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/results/PyTorchPassThroughResultsTests.java @@ -25,9 +25,9 @@ protected Writeable.Reader instanceReader() { protected PyTorchPassThroughResults createTestInstance() { int rows = randomIntBetween(1, 10); int columns = randomIntBetween(1, 10); - double [][] arr = new double[rows][columns]; - for (int i=0; i { public static RegressionInferenceResults createRandomResults() { - return new RegressionInferenceResults(randomDouble(), + return new RegressionInferenceResults( + randomDouble(), RegressionConfigTests.randomRegressionConfig(), - randomBoolean() ? Collections.emptyList() : - Stream.generate(RegressionFeatureImportanceTests::createRandomInstance) + randomBoolean() + ? Collections.emptyList() + : Stream.generate(RegressionFeatureImportanceTests::createRandomInstance) .limit(randomIntBetween(1, 10)) - .collect(Collectors.toList())); + .collect(Collectors.toList()) + ); } public void testWriteResultsWithImportance() { List importanceList = Stream.generate(RegressionFeatureImportanceTests::createRandomInstance) .limit(5) .collect(Collectors.toList()); - RegressionInferenceResults result = new RegressionInferenceResults(0.3, - new RegressionConfig("predicted_value", 3), - importanceList); + RegressionInferenceResults result = new RegressionInferenceResults(0.3, new RegressionConfig("predicted_value", 3), importanceList); IngestDocument document = new IngestDocument(new HashMap<>(), new HashMap<>()); writeResult(result, document, "result_field", "test"); assertThat(document.getFieldValue("result_field.predicted_value", Double.class), equalTo(0.3)); @SuppressWarnings("unchecked") - List> writtenImportance = (List>)document.getFieldValue( + List> writtenImportance = (List>) document.getFieldValue( "result_field.feature_importance", - List.class); + List.class + ); assertThat(writtenImportance, hasSize(3)); - importanceList.sort((l, r)-> Double.compare(Math.abs(r.getImportance()), Math.abs(l.getImportance()))); + importanceList.sort((l, r) -> Double.compare(Math.abs(r.getImportance()), Math.abs(l.getImportance()))); for (int i = 0; i < 3; i++) { Map objectMap = writtenImportance.get(i); RegressionFeatureImportance importance = importanceList.get(i); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/results/TextEmbeddingResultsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/results/TextEmbeddingResultsTests.java index 8d9e0834d81b0..3c27af0790ea7 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/results/TextEmbeddingResultsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/results/TextEmbeddingResultsTests.java @@ -25,7 +25,7 @@ protected Writeable.Reader instanceReader() { protected TextEmbeddingResults createTestInstance() { int columns = randomIntBetween(1, 10); double[] arr = new double[columns]; - for (int i=0; i instanceReader() { @Override void assertFieldValues(WarningInferenceResults createdInstance, IngestDocument document, String resultsField) { - assertThat( - document.getFieldValue(resultsField + ".warning", String.class), - equalTo(createdInstance.getWarning()) - ); + assertThat(document.getFieldValue(resultsField + ".warning", String.class), equalTo(createdInstance.getWarning())); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ClassificationConfigTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ClassificationConfigTests.java index b03e9a2cf7809..2880e4952addb 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ClassificationConfigTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ClassificationConfigTests.java @@ -14,18 +14,18 @@ import java.io.IOException; - public class ClassificationConfigTests extends AbstractBWCSerializationTestCase { private boolean lenient; public static ClassificationConfig randomClassificationConfig() { - return new ClassificationConfig(randomBoolean() ? null : randomIntBetween(-1, 10), + return new ClassificationConfig( + randomBoolean() ? null : randomIntBetween(-1, 10), randomBoolean() ? null : randomAlphaOfLength(10), randomBoolean() ? null : randomAlphaOfLength(10), randomBoolean() ? null : randomIntBetween(0, 10), randomFrom(PredictionFieldType.values()) - ); + ); } @Before diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ClassificationConfigUpdateTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ClassificationConfigUpdateTests.java index eedfc153aa7be..5f35e674646b6 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ClassificationConfigUpdateTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ClassificationConfigUpdateTests.java @@ -25,12 +25,13 @@ public class ClassificationConfigUpdateTests extends AbstractBWCSerializationTestCase { public static ClassificationConfigUpdate randomClassificationConfigUpdate() { - return new ClassificationConfigUpdate(randomBoolean() ? null : randomIntBetween(-1, 10), + return new ClassificationConfigUpdate( + randomBoolean() ? null : randomIntBetween(-1, 10), randomBoolean() ? null : randomAlphaOfLength(10), randomBoolean() ? null : randomAlphaOfLength(10), randomBoolean() ? null : randomIntBetween(0, 10), randomBoolean() ? null : randomFrom(PredictionFieldType.values()) - ); + ); } public void testFromMap() { @@ -48,8 +49,10 @@ public void testFromMap() { } public void testFromMapWithUnknownField() { - ElasticsearchException ex = expectThrows(ElasticsearchException.class, - () -> ClassificationConfigUpdate.fromMap(Collections.singletonMap("some_key", 1))); + ElasticsearchException ex = expectThrows( + ElasticsearchException.class, + () -> ClassificationConfigUpdate.fromMap(Collections.singletonMap("some_key", 1)) + ); assertThat(ex.getMessage(), equalTo("Unrecognized fields [some_key].")); } @@ -58,28 +61,34 @@ public void testApply() { assertThat(originalConfig, equalTo(ClassificationConfigUpdate.EMPTY_PARAMS.apply(originalConfig))); - assertThat(new ClassificationConfig.Builder(originalConfig).setNumTopClasses(5).build(), - equalTo(new ClassificationConfigUpdate.Builder().setNumTopClasses(5).build().apply(originalConfig))); - assertThat(new ClassificationConfig.Builder() - .setNumTopClasses(5) - .setNumTopFeatureImportanceValues(1) - .setPredictionFieldType(PredictionFieldType.BOOLEAN) - .setResultsField("foo") - .setTopClassesResultsField("bar").build(), - equalTo(new ClassificationConfigUpdate.Builder() - .setNumTopClasses(5) + assertThat( + new ClassificationConfig.Builder(originalConfig).setNumTopClasses(5).build(), + equalTo(new ClassificationConfigUpdate.Builder().setNumTopClasses(5).build().apply(originalConfig)) + ); + assertThat( + new ClassificationConfig.Builder().setNumTopClasses(5) .setNumTopFeatureImportanceValues(1) .setPredictionFieldType(PredictionFieldType.BOOLEAN) .setResultsField("foo") .setTopClassesResultsField("bar") - .build() - .apply(originalConfig) - )); + .build(), + equalTo( + new ClassificationConfigUpdate.Builder().setNumTopClasses(5) + .setNumTopFeatureImportanceValues(1) + .setPredictionFieldType(PredictionFieldType.BOOLEAN) + .setResultsField("foo") + .setTopClassesResultsField("bar") + .build() + .apply(originalConfig) + ) + ); } public void testDuplicateFieldNamesThrow() { - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> new ClassificationConfigUpdate(5, "foo", "foo", 1, PredictionFieldType.BOOLEAN)); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> new ClassificationConfigUpdate(5, "foo", "foo", 1, PredictionFieldType.BOOLEAN) + ); assertEquals("Invalid inference config. More than one field is configured as [foo]", e.getMessage()); } @@ -94,7 +103,7 @@ public void testDuplicateWithResultsField() { assertEquals(newFieldName, updateWithField.getResultsField()); // other fields are the same assertThat(updateWithField, instanceOf(ClassificationConfigUpdate.class)); - ClassificationConfigUpdate classUpdate = (ClassificationConfigUpdate)updateWithField; + ClassificationConfigUpdate classUpdate = (ClassificationConfigUpdate) updateWithField; assertEquals(update.getTopClassesResultsField(), classUpdate.getTopClassesResultsField()); assertEquals(update.getNumTopClasses(), classUpdate.getNumTopClasses()); assertEquals(update.getPredictionFieldType(), classUpdate.getPredictionFieldType()); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/FillMaskConfigUpdateTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/FillMaskConfigUpdateTests.java index 2f791fc3809bd..cc8bd0857c5cf 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/FillMaskConfigUpdateTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/FillMaskConfigUpdateTests.java @@ -24,31 +24,35 @@ public class FillMaskConfigUpdateTests extends AbstractBWCSerializationTestCase< public void testFromMap() { FillMaskConfigUpdate expected = new FillMaskConfigUpdate(3, "ml-results"); - Map config = new HashMap<>(){{ - put(NlpConfig.RESULTS_FIELD.getPreferredName(), "ml-results"); - put(NlpConfig.NUM_TOP_CLASSES.getPreferredName(), 3); - }}; + Map config = new HashMap<>() { + { + put(NlpConfig.RESULTS_FIELD.getPreferredName(), "ml-results"); + put(NlpConfig.NUM_TOP_CLASSES.getPreferredName(), 3); + } + }; assertThat(FillMaskConfigUpdate.fromMap(config), equalTo(expected)); } public void testFromMapWithUnknownField() { - ElasticsearchException ex = expectThrows(ElasticsearchException.class, - () -> FillMaskConfigUpdate.fromMap(Collections.singletonMap("some_key", 1))); + ElasticsearchException ex = expectThrows( + ElasticsearchException.class, + () -> FillMaskConfigUpdate.fromMap(Collections.singletonMap("some_key", 1)) + ); assertThat(ex.getMessage(), equalTo("Unrecognized fields [some_key].")); } public void testIsNoop() { assertTrue(new FillMaskConfigUpdate.Builder().build().isNoop(FillMaskConfigTests.createRandom())); - assertFalse(new FillMaskConfigUpdate.Builder() - .setResultsField("foo") - .build() - .isNoop(new FillMaskConfig.Builder().setResultsField("bar").build())); + assertFalse( + new FillMaskConfigUpdate.Builder().setResultsField("foo") + .build() + .isNoop(new FillMaskConfig.Builder().setResultsField("bar").build()) + ); - assertTrue(new FillMaskConfigUpdate.Builder() - .setNumTopClasses(3) - .build() - .isNoop(new FillMaskConfig.Builder().setNumTopClasses(3).build())); + assertTrue( + new FillMaskConfigUpdate.Builder().setNumTopClasses(3).build().isNoop(new FillMaskConfig.Builder().setNumTopClasses(3).build()) + ); } public void testApply() { @@ -56,22 +60,16 @@ public void testApply() { assertThat(originalConfig, equalTo(new FillMaskConfigUpdate.Builder().build().apply(originalConfig))); - assertThat(new FillMaskConfig.Builder(originalConfig) - .setResultsField("ml-results") - .build(), - equalTo(new FillMaskConfigUpdate.Builder() - .setResultsField("ml-results") - .build() - .apply(originalConfig) - )); - assertThat(new FillMaskConfig.Builder(originalConfig) - .setNumTopClasses(originalConfig.getNumTopClasses() +1) - .build(), - equalTo(new FillMaskConfigUpdate.Builder() - .setNumTopClasses(originalConfig.getNumTopClasses() +1) - .build() - .apply(originalConfig) - )); + assertThat( + new FillMaskConfig.Builder(originalConfig).setResultsField("ml-results").build(), + equalTo(new FillMaskConfigUpdate.Builder().setResultsField("ml-results").build().apply(originalConfig)) + ); + assertThat( + new FillMaskConfig.Builder(originalConfig).setNumTopClasses(originalConfig.getNumTopClasses() + 1).build(), + equalTo( + new FillMaskConfigUpdate.Builder().setNumTopClasses(originalConfig.getNumTopClasses() + 1).build().apply(originalConfig) + ) + ); } @Override diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/IndexLocationTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/IndexLocationTests.java index 52668fc145bb8..7d427b21e7eac 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/IndexLocationTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/IndexLocationTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.xpack.core.ml.inference.trainedmodel; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/InferenceHelpersTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/InferenceHelpersTests.java index 5ad10b5f75dfa..151e08b0100b3 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/InferenceHelpersTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/InferenceHelpersTests.java @@ -14,7 +14,6 @@ import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.nullValue; - public class InferenceHelpersTests extends ESTestCase { public void testToDoubleFromNumbers() { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/InferenceStatsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/InferenceStatsTests.java index 92d54765e8247..8eefb6fcb5b2c 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/InferenceStatsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/InferenceStatsTests.java @@ -6,11 +6,11 @@ */ package org.elasticsearch.xpack.core.ml.inference.trainedmodel; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractSerializingTestCase; import java.io.IOException; import java.time.Instant; @@ -21,14 +21,15 @@ public class InferenceStatsTests extends AbstractSerializingTestCase { public static InferenceStats createTestInstance(String modelId, @Nullable String nodeId) { - return new InferenceStats(randomNonNegativeLong(), + return new InferenceStats( + randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), modelId, nodeId, Instant.now() - ); + ); } @Override diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/NerConfigUpdateTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/NerConfigUpdateTests.java index 42b35842c11d8..62542d67375fa 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/NerConfigUpdateTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/NerConfigUpdateTests.java @@ -25,34 +25,36 @@ public class NerConfigUpdateTests extends AbstractBWCSerializationTestCase config = new HashMap<>(){{ - put(NlpConfig.RESULTS_FIELD.getPreferredName(), "ml-results"); - }}; + Map config = new HashMap<>() { + { + put(NlpConfig.RESULTS_FIELD.getPreferredName(), "ml-results"); + } + }; assertThat(NerConfigUpdate.fromMap(config), equalTo(expected)); } public void testFromMapWithUnknownField() { - ElasticsearchException ex = expectThrows(ElasticsearchException.class, - () -> NerConfigUpdate.fromMap(Collections.singletonMap("some_key", 1))); + ElasticsearchException ex = expectThrows( + ElasticsearchException.class, + () -> NerConfigUpdate.fromMap(Collections.singletonMap("some_key", 1)) + ); assertThat(ex.getMessage(), equalTo("Unrecognized fields [some_key].")); } - public void testApply() { NerConfig originalConfig = NerConfigTests.createRandom(); assertThat(originalConfig, sameInstance(new NerConfigUpdate.Builder().build().apply(originalConfig))); - assertThat(new NerConfig( + assertThat( + new NerConfig( originalConfig.getVocabularyConfig(), originalConfig.getTokenization(), originalConfig.getClassificationLabels(), - "ml-results"), - equalTo(new NerConfigUpdate.Builder() - .setResultsField("ml-results") - .build() - .apply(originalConfig) - )); + "ml-results" + ), + equalTo(new NerConfigUpdate.Builder().setResultsField("ml-results").build().apply(originalConfig)) + ); } @Override @@ -79,4 +81,3 @@ protected NerConfigUpdate mutateInstanceForVersion(NerConfigUpdate instance, Ver return instance; } } - diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/PassThroughConfigUpdateTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/PassThroughConfigUpdateTests.java index 265b2d6ee15fb..676741e667061 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/PassThroughConfigUpdateTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/PassThroughConfigUpdateTests.java @@ -25,33 +25,31 @@ public class PassThroughConfigUpdateTests extends AbstractBWCSerializationTestCa public void testFromMap() { PassThroughConfigUpdate expected = new PassThroughConfigUpdate("ml-results"); - Map config = new HashMap<>(){{ - put(NlpConfig.RESULTS_FIELD.getPreferredName(), "ml-results"); - }}; + Map config = new HashMap<>() { + { + put(NlpConfig.RESULTS_FIELD.getPreferredName(), "ml-results"); + } + }; assertThat(PassThroughConfigUpdate.fromMap(config), equalTo(expected)); } public void testFromMapWithUnknownField() { - ElasticsearchException ex = expectThrows(ElasticsearchException.class, - () -> PassThroughConfigUpdate.fromMap(Collections.singletonMap("some_key", 1))); + ElasticsearchException ex = expectThrows( + ElasticsearchException.class, + () -> PassThroughConfigUpdate.fromMap(Collections.singletonMap("some_key", 1)) + ); assertThat(ex.getMessage(), equalTo("Unrecognized fields [some_key].")); } - public void testApply() { PassThroughConfig originalConfig = PassThroughConfigTests.createRandom(); assertThat(originalConfig, sameInstance(new PassThroughConfigUpdate.Builder().build().apply(originalConfig))); - assertThat(new PassThroughConfig( - originalConfig.getVocabularyConfig(), - originalConfig.getTokenization(), - "ml-results"), - equalTo(new PassThroughConfigUpdate.Builder() - .setResultsField("ml-results") - .build() - .apply(originalConfig) - )); + assertThat( + new PassThroughConfig(originalConfig.getVocabularyConfig(), originalConfig.getTokenization(), "ml-results"), + equalTo(new PassThroughConfigUpdate.Builder().setResultsField("ml-results").build().apply(originalConfig)) + ); } @Override diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/PredictionFieldTypeTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/PredictionFieldTypeTests.java index b736d80be6d19..422bffdd36a5c 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/PredictionFieldTypeTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/PredictionFieldTypeTests.java @@ -17,12 +17,9 @@ public class PredictionFieldTypeTests extends ESTestCase { private static final String NOT_BOOLEAN = "not_boolean"; public void testTransformPredictedValueBoolean() { - assertThat(PredictionFieldType.BOOLEAN.transformPredictedValue(null, randomBoolean() ? null : NOT_BOOLEAN), - is(nullValue())); - assertThat(PredictionFieldType.BOOLEAN.transformPredictedValue(1.0, randomBoolean() ? null : NOT_BOOLEAN), - is(true)); - assertThat(PredictionFieldType.BOOLEAN.transformPredictedValue(0.0, randomBoolean() ? null : NOT_BOOLEAN), - is(false)); + assertThat(PredictionFieldType.BOOLEAN.transformPredictedValue(null, randomBoolean() ? null : NOT_BOOLEAN), is(nullValue())); + assertThat(PredictionFieldType.BOOLEAN.transformPredictedValue(1.0, randomBoolean() ? null : NOT_BOOLEAN), is(true)); + assertThat(PredictionFieldType.BOOLEAN.transformPredictedValue(0.0, randomBoolean() ? null : NOT_BOOLEAN), is(false)); assertThat(PredictionFieldType.BOOLEAN.transformPredictedValue(0.0, "1"), is(true)); assertThat(PredictionFieldType.BOOLEAN.transformPredictedValue(0.0, "0"), is(false)); assertThat(PredictionFieldType.BOOLEAN.transformPredictedValue(0.0, "TruE"), is(true)); @@ -31,22 +28,30 @@ public void testTransformPredictedValueBoolean() { assertThat(PredictionFieldType.BOOLEAN.transformPredictedValue(1.0, "1"), is(true)); assertThat(PredictionFieldType.BOOLEAN.transformPredictedValue(1.0, "TruE"), is(true)); assertThat(PredictionFieldType.BOOLEAN.transformPredictedValue(1.0, "fAlse"), is(false)); - expectThrows(IllegalArgumentException.class, - () -> PredictionFieldType.BOOLEAN.transformPredictedValue(0.1, randomBoolean() ? null : NOT_BOOLEAN)); - expectThrows(IllegalArgumentException.class, - () -> PredictionFieldType.BOOLEAN.transformPredictedValue(1.1, randomBoolean() ? null : NOT_BOOLEAN)); + expectThrows( + IllegalArgumentException.class, + () -> PredictionFieldType.BOOLEAN.transformPredictedValue(0.1, randomBoolean() ? null : NOT_BOOLEAN) + ); + expectThrows( + IllegalArgumentException.class, + () -> PredictionFieldType.BOOLEAN.transformPredictedValue(1.1, randomBoolean() ? null : NOT_BOOLEAN) + ); } public void testTransformPredictedValueString() { - assertThat(PredictionFieldType.STRING.transformPredictedValue(null, randomBoolean() ? null : randomAlphaOfLength(10)), - is(nullValue())); + assertThat( + PredictionFieldType.STRING.transformPredictedValue(null, randomBoolean() ? null : randomAlphaOfLength(10)), + is(nullValue()) + ); assertThat(PredictionFieldType.STRING.transformPredictedValue(1.0, "foo"), equalTo("foo")); assertThat(PredictionFieldType.STRING.transformPredictedValue(1.0, null), equalTo("1.0")); } public void testTransformPredictedValueNumber() { - assertThat(PredictionFieldType.NUMBER.transformPredictedValue(null, randomBoolean() ? null : randomAlphaOfLength(10)), - is(nullValue())); + assertThat( + PredictionFieldType.NUMBER.transformPredictedValue(null, randomBoolean() ? null : randomAlphaOfLength(10)), + is(nullValue()) + ); assertThat(PredictionFieldType.NUMBER.transformPredictedValue(1.0, "foo"), equalTo(1.0)); assertThat(PredictionFieldType.NUMBER.transformPredictedValue(1.0, null), equalTo(1.0)); assertThat(PredictionFieldType.NUMBER.transformPredictedValue(1.0, ""), equalTo(1.0)); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/RegressionConfigUpdateTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/RegressionConfigUpdateTests.java index b513e0cfc5090..3918ddbf4f7e3 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/RegressionConfigUpdateTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/RegressionConfigUpdateTests.java @@ -25,22 +25,28 @@ public class RegressionConfigUpdateTests extends AbstractBWCSerializationTestCase { public static RegressionConfigUpdate randomRegressionConfigUpdate() { - return new RegressionConfigUpdate(randomBoolean() ? null : randomAlphaOfLength(10), - randomBoolean() ? null : randomIntBetween(0, 10)); + return new RegressionConfigUpdate( + randomBoolean() ? null : randomAlphaOfLength(10), + randomBoolean() ? null : randomIntBetween(0, 10) + ); } public void testFromMap() { RegressionConfigUpdate expected = new RegressionConfigUpdate("foo", 3); - Map config = new HashMap<>(){{ - put(RegressionConfig.RESULTS_FIELD.getPreferredName(), "foo"); - put(RegressionConfig.NUM_TOP_FEATURE_IMPORTANCE_VALUES.getPreferredName(), 3); - }}; + Map config = new HashMap<>() { + { + put(RegressionConfig.RESULTS_FIELD.getPreferredName(), "foo"); + put(RegressionConfig.NUM_TOP_FEATURE_IMPORTANCE_VALUES.getPreferredName(), 3); + } + }; assertThat(RegressionConfigUpdate.fromMap(config), equalTo(expected)); } public void testFromMapWithUnknownField() { - ElasticsearchException ex = expectThrows(ElasticsearchException.class, - () -> RegressionConfigUpdate.fromMap(Collections.singletonMap("some_key", 1))); + ElasticsearchException ex = expectThrows( + ElasticsearchException.class, + () -> RegressionConfigUpdate.fromMap(Collections.singletonMap("some_key", 1)) + ); assertThat(ex.getMessage(), equalTo("Unrecognized fields [some_key].")); } @@ -49,23 +55,23 @@ public void testApply() { assertThat(originalConfig, equalTo(RegressionConfigUpdate.EMPTY_PARAMS.apply(originalConfig))); - assertThat(new RegressionConfig.Builder(originalConfig).setNumTopFeatureImportanceValues(5).build(), - equalTo(new RegressionConfigUpdate.Builder().setNumTopFeatureImportanceValues(5).build().apply(originalConfig))); - assertThat(new RegressionConfig.Builder() - .setNumTopFeatureImportanceValues(1) - .setResultsField("foo") - .build(), - equalTo(new RegressionConfigUpdate.Builder() - .setNumTopFeatureImportanceValues(1) - .setResultsField("foo") - .build() - .apply(originalConfig) - )); + assertThat( + new RegressionConfig.Builder(originalConfig).setNumTopFeatureImportanceValues(5).build(), + equalTo(new RegressionConfigUpdate.Builder().setNumTopFeatureImportanceValues(5).build().apply(originalConfig)) + ); + assertThat( + new RegressionConfig.Builder().setNumTopFeatureImportanceValues(1).setResultsField("foo").build(), + equalTo( + new RegressionConfigUpdate.Builder().setNumTopFeatureImportanceValues(1) + .setResultsField("foo") + .build() + .apply(originalConfig) + ) + ); } public void testInvalidResultFieldNotUnique() { - ElasticsearchStatusException e = - expectThrows(ElasticsearchStatusException.class, () -> new RegressionConfigUpdate("warning", 0)); + ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, () -> new RegressionConfigUpdate("warning", 0)); assertEquals("Invalid inference config. More than one field is configured as [warning]", e.getMessage()); } @@ -79,8 +85,10 @@ public void testNewBuilder() { assertEquals(newFieldName, updateWithField.getResultsField()); // other fields are the same assertThat(updateWithField, instanceOf(RegressionConfigUpdate.class)); - assertEquals(update.getNumTopFeatureImportanceValues(), - ((RegressionConfigUpdate)updateWithField).getNumTopFeatureImportanceValues()); + assertEquals( + update.getNumTopFeatureImportanceValues(), + ((RegressionConfigUpdate) updateWithField).getNumTopFeatureImportanceValues() + ); } @Override diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ResultsFieldUpdateTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ResultsFieldUpdateTests.java index 483559b89735a..78d305a06153c 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ResultsFieldUpdateTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ResultsFieldUpdateTests.java @@ -42,7 +42,7 @@ public void testApply_OnlyTheResultsFieldIsChanged() { InferenceConfig applied = update.apply(config); assertThat(applied, instanceOf(ClassificationConfig.class)); - ClassificationConfig appliedConfig = (ClassificationConfig)applied; + ClassificationConfig appliedConfig = (ClassificationConfig) applied; assertEquals(newResultsField, appliedConfig.getResultsField()); assertEquals(appliedConfig, new ClassificationConfig.Builder(config).setResultsField(newResultsField).build()); @@ -53,7 +53,7 @@ public void testApply_OnlyTheResultsFieldIsChanged() { InferenceConfig applied = update.apply(config); assertThat(applied, instanceOf(RegressionConfig.class)); - RegressionConfig appliedConfig = (RegressionConfig)applied; + RegressionConfig appliedConfig = (RegressionConfig) applied; assertEquals(newResultsField, appliedConfig.getResultsField()); assertEquals(appliedConfig, new RegressionConfig.Builder(config).setResultsField(newResultsField).build()); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/TextClassificationConfigTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/TextClassificationConfigTests.java index ec9a8b6102cf5..6e7190a355cad 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/TextClassificationConfigTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/TextClassificationConfigTests.java @@ -52,16 +52,18 @@ protected TextClassificationConfig mutateInstanceForVersion(TextClassificationCo } public void testInvalidClassificationLabels() { - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> new TextClassificationConfig(null, null, null, null, null)); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> new TextClassificationConfig(null, null, null, null, null) + ); - assertThat(e.getMessage(), - containsString("[text_classification] requires at least 2 [classification_labels]; provided null")); + assertThat(e.getMessage(), containsString("[text_classification] requires at least 2 [classification_labels]; provided null")); - e = expectThrows(ElasticsearchStatusException.class, - () -> new TextClassificationConfig(null, null, List.of("too-few"), null, null)); - assertThat(e.getMessage(), - containsString("[text_classification] requires at least 2 [classification_labels]; provided [too-few]")); + e = expectThrows( + ElasticsearchStatusException.class, + () -> new TextClassificationConfig(null, null, List.of("too-few"), null, null) + ); + assertThat(e.getMessage(), containsString("[text_classification] requires at least 2 [classification_labels]; provided [too-few]")); } public static TextClassificationConfig createRandom() { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/TextClassificationConfigUpdateTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/TextClassificationConfigUpdateTests.java index f9003e6afd3da..4658bdb06b7ba 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/TextClassificationConfigUpdateTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/TextClassificationConfigUpdateTests.java @@ -27,39 +27,48 @@ public class TextClassificationConfigUpdateTests extends AbstractBWCSerializatio public void testFromMap() { TextClassificationConfigUpdate expected = new TextClassificationConfigUpdate(List.of("foo", "bar"), 3, "ml-results"); - Map config = new HashMap<>(){{ - put(NlpConfig.RESULTS_FIELD.getPreferredName(), "ml-results"); - put(NlpConfig.CLASSIFICATION_LABELS.getPreferredName(), List.of("foo", "bar")); - put(NlpConfig.NUM_TOP_CLASSES.getPreferredName(), 3); - }}; + Map config = new HashMap<>() { + { + put(NlpConfig.RESULTS_FIELD.getPreferredName(), "ml-results"); + put(NlpConfig.CLASSIFICATION_LABELS.getPreferredName(), List.of("foo", "bar")); + put(NlpConfig.NUM_TOP_CLASSES.getPreferredName(), 3); + } + }; assertThat(TextClassificationConfigUpdate.fromMap(config), equalTo(expected)); } public void testFromMapWithUnknownField() { - ElasticsearchException ex = expectThrows(ElasticsearchException.class, - () -> TextClassificationConfigUpdate.fromMap(Collections.singletonMap("some_key", 1))); + ElasticsearchException ex = expectThrows( + ElasticsearchException.class, + () -> TextClassificationConfigUpdate.fromMap(Collections.singletonMap("some_key", 1)) + ); assertThat(ex.getMessage(), equalTo("Unrecognized fields [some_key].")); } public void testIsNoop() { assertTrue(new TextClassificationConfigUpdate.Builder().build().isNoop(TextClassificationConfigTests.createRandom())); - assertFalse(new TextClassificationConfigUpdate.Builder() - .setResultsField("foo") - .build() - .isNoop(new TextClassificationConfig.Builder() - .setClassificationLabels(List.of("a", "b")) - .setNumTopClasses(-1) - .setResultsField("bar").build())); - - assertTrue(new TextClassificationConfigUpdate.Builder() - .setNumTopClasses(3) - .build() - .isNoop(new TextClassificationConfig.Builder().setClassificationLabels(List.of("a", "b")).setNumTopClasses(3).build())); - assertFalse(new TextClassificationConfigUpdate.Builder() - .setClassificationLabels(List.of("a", "b")) - .build() - .isNoop(new TextClassificationConfig.Builder().setClassificationLabels(List.of("c", "d")).setNumTopClasses(3).build())); + assertFalse( + new TextClassificationConfigUpdate.Builder().setResultsField("foo") + .build() + .isNoop( + new TextClassificationConfig.Builder().setClassificationLabels(List.of("a", "b")) + .setNumTopClasses(-1) + .setResultsField("bar") + .build() + ) + ); + + assertTrue( + new TextClassificationConfigUpdate.Builder().setNumTopClasses(3) + .build() + .isNoop(new TextClassificationConfig.Builder().setClassificationLabels(List.of("a", "b")).setNumTopClasses(3).build()) + ); + assertFalse( + new TextClassificationConfigUpdate.Builder().setClassificationLabels(List.of("a", "b")) + .build() + .isNoop(new TextClassificationConfig.Builder().setClassificationLabels(List.of("c", "d")).setNumTopClasses(3).build()) + ); } public void testApply() { @@ -73,29 +82,24 @@ public void testApply() { assertThat(originalConfig, equalTo(new TextClassificationConfigUpdate.Builder().build().apply(originalConfig))); - assertThat(new TextClassificationConfig.Builder(originalConfig) - .setClassificationLabels(List.of("foo", "bar")) - .build(), - equalTo(new TextClassificationConfigUpdate.Builder() - .setClassificationLabels(List.of("foo", "bar")) - .build() - .apply(originalConfig))); - assertThat(new TextClassificationConfig.Builder(originalConfig) - .setResultsField("ml-results") - .build(), - equalTo(new TextClassificationConfigUpdate.Builder() - .setResultsField("ml-results") - .build() - .apply(originalConfig) - )); - assertThat(new TextClassificationConfig.Builder(originalConfig) - .setNumTopClasses(originalConfig.getNumTopClasses() + 2) - .build(), - equalTo(new TextClassificationConfigUpdate.Builder() - .setNumTopClasses(originalConfig.getNumTopClasses() + 2) - .build() - .apply(originalConfig) - )); + assertThat( + new TextClassificationConfig.Builder(originalConfig).setClassificationLabels(List.of("foo", "bar")).build(), + equalTo( + new TextClassificationConfigUpdate.Builder().setClassificationLabels(List.of("foo", "bar")).build().apply(originalConfig) + ) + ); + assertThat( + new TextClassificationConfig.Builder(originalConfig).setResultsField("ml-results").build(), + equalTo(new TextClassificationConfigUpdate.Builder().setResultsField("ml-results").build().apply(originalConfig)) + ); + assertThat( + new TextClassificationConfig.Builder(originalConfig).setNumTopClasses(originalConfig.getNumTopClasses() + 2).build(), + equalTo( + new TextClassificationConfigUpdate.Builder().setNumTopClasses(originalConfig.getNumTopClasses() + 2) + .build() + .apply(originalConfig) + ) + ); } public void testApplyWithInvalidLabels() { @@ -104,16 +108,19 @@ public void testApplyWithInvalidLabels() { int numberNewLabels = originalConfig.getClassificationLabels().size() + 2; List newLabels = randomList(numberNewLabels, numberNewLabels, () -> randomAlphaOfLength(6)); - var update = new TextClassificationConfigUpdate.Builder() - .setClassificationLabels(newLabels) - .build(); - - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> update.apply(originalConfig)); - assertThat(e.getMessage(), - containsString("The number of [classification_labels] the model is defined with [" - + originalConfig.getClassificationLabels().size() + - "] does not match the number in the update [" + numberNewLabels + "]")); + var update = new TextClassificationConfigUpdate.Builder().setClassificationLabels(newLabels).build(); + + ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, () -> update.apply(originalConfig)); + assertThat( + e.getMessage(), + containsString( + "The number of [classification_labels] the model is defined with [" + + originalConfig.getClassificationLabels().size() + + "] does not match the number in the update [" + + numberNewLabels + + "]" + ) + ); } @Override diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/TextEmbeddingConfigUpdateTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/TextEmbeddingConfigUpdateTests.java index 1f56f7fc6f9d3..a5e2e06c4fb59 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/TextEmbeddingConfigUpdateTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/TextEmbeddingConfigUpdateTests.java @@ -25,33 +25,31 @@ public class TextEmbeddingConfigUpdateTests extends AbstractBWCSerializationTest public void testFromMap() { TextEmbeddingConfigUpdate expected = new TextEmbeddingConfigUpdate("ml-results"); - Map config = new HashMap<>(){{ - put(NlpConfig.RESULTS_FIELD.getPreferredName(), "ml-results"); - }}; + Map config = new HashMap<>() { + { + put(NlpConfig.RESULTS_FIELD.getPreferredName(), "ml-results"); + } + }; assertThat(TextEmbeddingConfigUpdate.fromMap(config), equalTo(expected)); } public void testFromMapWithUnknownField() { - ElasticsearchException ex = expectThrows(ElasticsearchException.class, - () -> TextEmbeddingConfigUpdate.fromMap(Collections.singletonMap("some_key", 1))); + ElasticsearchException ex = expectThrows( + ElasticsearchException.class, + () -> TextEmbeddingConfigUpdate.fromMap(Collections.singletonMap("some_key", 1)) + ); assertThat(ex.getMessage(), equalTo("Unrecognized fields [some_key].")); } - public void testApply() { TextEmbeddingConfig originalConfig = TextEmbeddingConfigTests.createRandom(); assertThat(originalConfig, sameInstance(new TextEmbeddingConfigUpdate.Builder().build().apply(originalConfig))); - assertThat(new TextEmbeddingConfig( - originalConfig.getVocabularyConfig(), - originalConfig.getTokenization(), - "ml-results"), - equalTo(new TextEmbeddingConfigUpdate.Builder() - .setResultsField("ml-results") - .build() - .apply(originalConfig) - )); + assertThat( + new TextEmbeddingConfig(originalConfig.getVocabularyConfig(), originalConfig.getTokenization(), "ml-results"), + equalTo(new TextEmbeddingConfigUpdate.Builder().setResultsField("ml-results").build().apply(originalConfig)) + ); } @Override diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ZeroShotClassificationConfigUpdateTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ZeroShotClassificationConfigUpdateTests.java index 93244a5f42cc5..51832c5655805 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ZeroShotClassificationConfigUpdateTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ZeroShotClassificationConfigUpdateTests.java @@ -51,17 +51,21 @@ protected ZeroShotClassificationConfigUpdate mutateInstanceForVersion(ZeroShotCl public void testFromMap() { ZeroShotClassificationConfigUpdate expected = new ZeroShotClassificationConfigUpdate(List.of("foo", "bar"), false, "ml-results"); - Map config = new HashMap<>(){{ - put(ZeroShotClassificationConfig.LABELS.getPreferredName(), List.of("foo", "bar")); - put(ZeroShotClassificationConfig.MULTI_LABEL.getPreferredName(), false); - put(ZeroShotClassificationConfig.RESULTS_FIELD.getPreferredName(), "ml-results"); - }}; + Map config = new HashMap<>() { + { + put(ZeroShotClassificationConfig.LABELS.getPreferredName(), List.of("foo", "bar")); + put(ZeroShotClassificationConfig.MULTI_LABEL.getPreferredName(), false); + put(ZeroShotClassificationConfig.RESULTS_FIELD.getPreferredName(), "ml-results"); + } + }; assertThat(ZeroShotClassificationConfigUpdate.fromMap(config), equalTo(expected)); } public void testFromMapWithUnknownField() { - ElasticsearchException ex = expectThrows(ElasticsearchException.class, - () -> ZeroShotClassificationConfigUpdate.fromMap(Collections.singletonMap("some_key", 1))); + ElasticsearchException ex = expectThrows( + ElasticsearchException.class, + () -> ZeroShotClassificationConfigUpdate.fromMap(Collections.singletonMap("some_key", 1)) + ); assertThat(ex.getMessage(), equalTo("Unrecognized fields [some_key].")); } @@ -88,11 +92,7 @@ public void testApply() { List.of("foo", "bar"), originalConfig.getResultsField() ), - equalTo( - new ZeroShotClassificationConfigUpdate.Builder() - .setLabels(List.of("foo", "bar")).build() - .apply(originalConfig) - ) + equalTo(new ZeroShotClassificationConfigUpdate.Builder().setLabels(List.of("foo", "bar")).build().apply(originalConfig)) ); assertThat( new ZeroShotClassificationConfig( @@ -104,11 +104,7 @@ public void testApply() { originalConfig.getLabels(), originalConfig.getResultsField() ), - equalTo( - new ZeroShotClassificationConfigUpdate.Builder() - .setMultiLabel(true).build() - .apply(originalConfig) - ) + equalTo(new ZeroShotClassificationConfigUpdate.Builder().setMultiLabel(true).build().apply(originalConfig)) ); assertThat( new ZeroShotClassificationConfig( @@ -120,11 +116,7 @@ public void testApply() { originalConfig.getLabels(), "updated-field" ), - equalTo( - new ZeroShotClassificationConfigUpdate.Builder() - .setResultsField("updated-field").build() - .apply(originalConfig) - ) + equalTo(new ZeroShotClassificationConfigUpdate.Builder().setResultsField("updated-field").build().apply(originalConfig)) ); } @@ -148,7 +140,7 @@ public void testApplyWithEmptyLabelsInConfigAndUpdate() { public static ZeroShotClassificationConfigUpdate createRandom() { return new ZeroShotClassificationConfigUpdate( - randomBoolean() ? null : randomList(1,5, () -> randomAlphaOfLength(10)), + randomBoolean() ? null : randomList(1, 5, () -> randomAlphaOfLength(10)), randomBoolean() ? null : randomBoolean(), randomBoolean() ? null : randomAlphaOfLength(5) ); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/EnsembleTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/EnsembleTests.java index 9923daa8d7c31..395edee638f7a 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/EnsembleTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/EnsembleTests.java @@ -9,10 +9,10 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.inference.MlInferenceNamedXContentProvider; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.TargetType; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.TrainedModel; @@ -20,6 +20,7 @@ import org.elasticsearch.xpack.core.ml.inference.trainedmodel.tree.TreeNode; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.tree.TreeTests; import org.junit.Before; + import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; @@ -67,41 +68,35 @@ public static Ensemble createRandom(TargetType targetType) { public static Ensemble createRandom(TargetType targetType, List featureNames) { int numberOfModels = randomIntBetween(1, 10); - List treeFeatureNames = featureNames.isEmpty() ? - Stream.generate(() -> randomAlphaOfLength(10)).limit(5).collect(Collectors.toList()) : - featureNames; + List treeFeatureNames = featureNames.isEmpty() + ? Stream.generate(() -> randomAlphaOfLength(10)).limit(5).collect(Collectors.toList()) + : featureNames; List models = Stream.generate(() -> TreeTests.buildRandomTree(treeFeatureNames, 6)) .limit(numberOfModels) .collect(Collectors.toList()); - double[] weights = randomBoolean() ? - null : - Stream.generate(ESTestCase::randomDouble).limit(numberOfModels).mapToDouble(Double::valueOf).toArray(); + double[] weights = randomBoolean() + ? null + : Stream.generate(ESTestCase::randomDouble).limit(numberOfModels).mapToDouble(Double::valueOf).toArray(); List categoryLabels = null; if (randomBoolean() && targetType == TargetType.CLASSIFICATION) { categoryLabels = randomList(2, randomIntBetween(3, 10), () -> randomAlphaOfLength(10)); } - OutputAggregator outputAggregator = targetType == TargetType.REGRESSION ? - randomFrom(new WeightedSum(weights), new Exponent(weights)) : - randomFrom( - new WeightedMode( - weights, - categoryLabels != null ? categoryLabels.size() : randomIntBetween(2, 10)), - new LogisticRegression(weights)); + OutputAggregator outputAggregator = targetType == TargetType.REGRESSION + ? randomFrom(new WeightedSum(weights), new Exponent(weights)) + : randomFrom( + new WeightedMode(weights, categoryLabels != null ? categoryLabels.size() : randomIntBetween(2, 10)), + new LogisticRegression(weights) + ); - double[] thresholds = randomBoolean() && targetType == TargetType.CLASSIFICATION ? - Stream.generate(ESTestCase::randomDouble) + double[] thresholds = randomBoolean() && targetType == TargetType.CLASSIFICATION + ? Stream.generate(ESTestCase::randomDouble) .limit(categoryLabels == null ? randomIntBetween(1, 10) : categoryLabels.size()) .mapToDouble(Double::valueOf) - .toArray() : - null; + .toArray() + : null; - return new Ensemble(featureNames, - models, - outputAggregator, - targetType, - categoryLabels, - thresholds); + return new Ensemble(featureNames, models, outputAggregator, targetType, categoryLabels, thresholds); } @Override @@ -137,14 +132,17 @@ public void testEnsembleWithAggregatedOutputDifferingFromTrainedModels() { for (int i = 0; i < numberOfModels; i++) { models.add(TreeTests.buildRandomTree(featureNames, 6)); } - ElasticsearchException ex = expectThrows(ElasticsearchException.class, () -> { - Ensemble.builder() - .setTrainedModels(models) - .setOutputAggregator(outputAggregator) - .setFeatureNames(featureNames) - .build() - .validate(); - }); + ElasticsearchException ex = expectThrows( + ElasticsearchException.class, + () -> { + Ensemble.builder() + .setTrainedModels(models) + .setOutputAggregator(outputAggregator) + .setFeatureNames(featureNames) + .build() + .validate(); + } + ); assertThat(ex.getMessage(), equalTo("[aggregate_output] expects value array of size [7] but number of models is [5]")); } @@ -153,19 +151,18 @@ public void testEnsembleWithInvalidModel() { expectThrows(ElasticsearchException.class, () -> { Ensemble.builder() .setFeatureNames(featureNames) - .setTrainedModels(Arrays.asList( - // Tree with loop - Tree.builder() - .setNodes(TreeNode.builder(0) - .setLeftChild(1) - .setSplitFeature(1) - .setThreshold(randomDouble()), - TreeNode.builder(0) - .setLeftChild(0) - .setSplitFeature(1) - .setThreshold(randomDouble())) - .setFeatureNames(featureNames) - .build())) + .setTrainedModels( + Arrays.asList( + // Tree with loop + Tree.builder() + .setNodes( + TreeNode.builder(0).setLeftChild(1).setSplitFeature(1).setThreshold(randomDouble()), + TreeNode.builder(0).setLeftChild(0).setSplitFeature(1).setThreshold(randomDouble()) + ) + .setFeatureNames(featureNames) + .build() + ) + ) .build() .validate(); }); @@ -176,14 +173,14 @@ public void testEnsembleWithAggregatorOutputNotSupportingTargetType() { ElasticsearchException ex = expectThrows(ElasticsearchException.class, () -> { Ensemble.builder() .setFeatureNames(featureNames) - .setTrainedModels(Arrays.asList( - Tree.builder() - .setNodes(TreeNode.builder(0) - .setLeftChild(1) - .setSplitFeature(1) - .setThreshold(randomDouble())) - .setFeatureNames(featureNames) - .build())) + .setTrainedModels( + Arrays.asList( + Tree.builder() + .setNodes(TreeNode.builder(0).setLeftChild(1).setSplitFeature(1).setThreshold(randomDouble())) + .setFeatureNames(featureNames) + .build() + ) + ) .setClassificationLabels(Arrays.asList("label1", "label2")) .setTargetType(TargetType.CLASSIFICATION) .setOutputAggregator(new WeightedSum()) @@ -194,17 +191,15 @@ public void testEnsembleWithAggregatorOutputNotSupportingTargetType() { public void testEnsembleWithTargetTypeAndLabelsMismatch() { List featureNames = Arrays.asList("foo", "bar"); - String msg = "[target_type] should be [classification] if " + - "[classification_labels] or [classification_weights] are provided"; + String msg = "[target_type] should be [classification] if " + "[classification_labels] or [classification_weights] are provided"; ElasticsearchException ex = expectThrows(ElasticsearchException.class, () -> { Ensemble.builder() .setFeatureNames(featureNames) - .setTrainedModels(Arrays.asList( - Tree.builder() - .setNodes(TreeNode.builder(0) - .setLeafValue(randomDouble())) - .setFeatureNames(featureNames) - .build())) + .setTrainedModels( + Arrays.asList( + Tree.builder().setNodes(TreeNode.builder(0).setLeafValue(randomDouble())).setFeatureNames(featureNames).build() + ) + ) .setClassificationLabels(Arrays.asList("label1", "label2")) .build() .validate(); @@ -214,9 +209,10 @@ public void testEnsembleWithTargetTypeAndLabelsMismatch() { public void testEnsembleWithEmptyModels() { List featureNames = Arrays.asList("foo", "bar"); - ElasticsearchException ex = expectThrows(ElasticsearchException.class, () -> { - Ensemble.builder().setTrainedModels(Collections.emptyList()).setFeatureNames(featureNames).build().validate(); - }); + ElasticsearchException ex = expectThrows( + ElasticsearchException.class, + () -> { Ensemble.builder().setTrainedModels(Collections.emptyList()).setFeatureNames(featureNames).build().validate(); } + ); assertThat(ex.getMessage(), equalTo("[trained_models] must not be empty")); } @@ -224,10 +220,11 @@ public void testOperationsEstimations() { Tree tree1 = TreeTests.buildRandomTree(Arrays.asList("foo", "bar"), 2); Tree tree2 = TreeTests.buildRandomTree(Arrays.asList("foo", "bar", "baz"), 5); Tree tree3 = TreeTests.buildRandomTree(Arrays.asList("foo", "baz"), 3); - Ensemble ensemble = Ensemble.builder().setTrainedModels(Arrays.asList(tree1, tree2, tree3)) + Ensemble ensemble = Ensemble.builder() + .setTrainedModels(Arrays.asList(tree1, tree2, tree3)) .setTargetType(TargetType.CLASSIFICATION) .setFeatureNames(Arrays.asList("foo", "bar", "baz")) - .setOutputAggregator(new LogisticRegression(new double[]{0.1, 0.4, 1.0})) + .setOutputAggregator(new LogisticRegression(new double[] { 0.1, 0.4, 1.0 })) .build(); assertThat(ensemble.estimatedNumOperations(), equalTo(9L)); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/ExponentTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/ExponentTests.java index 1583dbebccbe5..0488f0c083f2c 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/ExponentTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/ExponentTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ml.inference.trainedmodel.ensemble; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.TargetType; import java.io.IOException; @@ -41,19 +41,18 @@ protected Writeable.Reader instanceReader() { } public void testAggregate() { - double[] ones = new double[]{1.0, 1.0, 1.0, 1.0, 1.0}; - double[][] values = new double[][]{ - new double[] {.01}, - new double[] {.2}, - new double[] {.002}, - new double[] {-.01}, - new double[] {.1} - }; + double[] ones = new double[] { 1.0, 1.0, 1.0, 1.0, 1.0 }; + double[][] values = new double[][] { + new double[] { .01 }, + new double[] { .2 }, + new double[] { .002 }, + new double[] { -.01 }, + new double[] { .1 } }; Exponent exponent = new Exponent(ones); assertThat(exponent.aggregate(exponent.processValues(values)), closeTo(1.35256, 0.00001)); - double[] variedWeights = new double[]{.01, -1.0, .1, 0.0, 0.0}; + double[] variedWeights = new double[] { .01, -1.0, .1, 0.0, 0.0 }; exponent = new Exponent(variedWeights); assertThat(exponent.aggregate(exponent.processValues(values)), closeTo(0.81897, 0.00001)); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/LogisticRegressionTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/LogisticRegressionTests.java index fe8eb4f9f05f6..af8e366997976 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/LogisticRegressionTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/LogisticRegressionTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ml.inference.trainedmodel.ensemble; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.TargetType; import java.io.IOException; @@ -42,19 +42,18 @@ protected Writeable.Reader instanceReader() { } public void testAggregate() { - double[] ones = new double[]{1.0, 1.0, 1.0, 1.0, 1.0}; - double[][] values = new double[][]{ - new double[] {1.0}, - new double[] {2.0}, - new double[] {2.0}, - new double[] {3.0}, - new double[] {5.0} - }; + double[] ones = new double[] { 1.0, 1.0, 1.0, 1.0, 1.0 }; + double[][] values = new double[][] { + new double[] { 1.0 }, + new double[] { 2.0 }, + new double[] { 2.0 }, + new double[] { 3.0 }, + new double[] { 5.0 } }; LogisticRegression logisticRegression = new LogisticRegression(ones); assertThat(logisticRegression.aggregate(logisticRegression.processValues(values)), equalTo(1.0)); - double[] variedWeights = new double[]{.01, -1.0, .1, 0.0, 0.0}; + double[] variedWeights = new double[] { .01, -1.0, .1, 0.0, 0.0 }; logisticRegression = new LogisticRegression(variedWeights); assertThat(logisticRegression.aggregate(logisticRegression.processValues(values)), equalTo(0.0)); @@ -64,14 +63,13 @@ public void testAggregate() { } public void testAggregateMultiValueArrays() { - double[] ones = new double[]{1.0, 1.0, 1.0, 1.0, 1.0}; - double[][] values = new double[][]{ - new double[] {1.0, 0.0, 1.0}, - new double[] {2.0, 0.0, 0.0}, - new double[] {2.0, 3.0, 1.0}, - new double[] {3.0, 3.0, 1.0}, - new double[] {1.0, 1.0, 5.0} - }; + double[] ones = new double[] { 1.0, 1.0, 1.0, 1.0, 1.0 }; + double[][] values = new double[][] { + new double[] { 1.0, 0.0, 1.0 }, + new double[] { 2.0, 0.0, 0.0 }, + new double[] { 2.0, 3.0, 1.0 }, + new double[] { 3.0, 3.0, 1.0 }, + new double[] { 1.0, 1.0, 5.0 } }; LogisticRegression logisticRegression = new LogisticRegression(ones); double[] processedValues = logisticRegression.processValues(values); @@ -81,7 +79,7 @@ public void testAggregateMultiValueArrays() { assertThat(processedValues[2], closeTo(0.244728471, 0.00001)); assertThat(logisticRegression.aggregate(logisticRegression.processValues(values)), equalTo(0.0)); - double[] variedWeights = new double[]{1.0, -1.0, .5, 1.0, 5.0}; + double[] variedWeights = new double[] { 1.0, -1.0, .5, 1.0, 5.0 }; logisticRegression = new LogisticRegression(variedWeights); processedValues = logisticRegression.processValues(values); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/WeightedAggregatorTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/WeightedAggregatorTests.java index a8627f21df634..4538c65307c8e 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/WeightedAggregatorTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/WeightedAggregatorTests.java @@ -35,7 +35,7 @@ public void testWithValuesOfWrongLength() { int numberOfValues = randomIntBetween(5, 10); double[][] values = new double[numberOfValues][]; for (int i = 0; i < numberOfValues; i++) { - values[i] = new double[] {randomDouble()}; + values[i] = new double[] { randomDouble() }; } OutputAggregator outputAggregatorWithTooFewWeights = createTestInstance(randomIntBetween(1, numberOfValues - 1)); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/WeightedModeTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/WeightedModeTests.java index 109eb4ef4ea4e..3d59d49f73fe0 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/WeightedModeTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/WeightedModeTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ml.inference.trainedmodel.ensemble; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.TargetType; import java.io.IOException; @@ -42,19 +42,18 @@ protected Writeable.Reader instanceReader() { } public void testAggregate() { - double[] ones = new double[]{1.0, 1.0, 1.0, 1.0, 1.0}; - double[][] values = new double[][]{ - new double[] {1.0}, - new double[] {2.0}, - new double[] {2.0}, - new double[] {3.0}, - new double[] {5.0} - }; + double[] ones = new double[] { 1.0, 1.0, 1.0, 1.0, 1.0 }; + double[][] values = new double[][] { + new double[] { 1.0 }, + new double[] { 2.0 }, + new double[] { 2.0 }, + new double[] { 3.0 }, + new double[] { 5.0 } }; WeightedMode weightedMode = new WeightedMode(ones, 6); assertThat(weightedMode.aggregate(weightedMode.processValues(values)), equalTo(2.0)); - double[] variedWeights = new double[]{1.0, -1.0, .5, 1.0, 5.0}; + double[] variedWeights = new double[] { 1.0, -1.0, .5, 1.0, 5.0 }; weightedMode = new WeightedMode(variedWeights, 6); assertThat(weightedMode.aggregate(weightedMode.processValues(values)), equalTo(5.0)); @@ -62,13 +61,12 @@ public void testAggregate() { weightedMode = new WeightedMode(6); assertThat(weightedMode.aggregate(weightedMode.processValues(values)), equalTo(2.0)); - values = new double[][]{ - new double[] {1.0}, - new double[] {1.0}, - new double[] {1.0}, - new double[] {1.0}, - new double[] {2.0} - }; + values = new double[][] { + new double[] { 1.0 }, + new double[] { 1.0 }, + new double[] { 1.0 }, + new double[] { 1.0 }, + new double[] { 2.0 } }; weightedMode = new WeightedMode(6); double[] processedValues = weightedMode.processValues(values); assertThat(processedValues.length, equalTo(6)); @@ -82,14 +80,13 @@ public void testAggregate() { } public void testAggregateMultiValueArrays() { - double[] ones = new double[]{1.0, 1.0, 1.0, 1.0, 1.0}; - double[][] values = new double[][]{ - new double[] {1.0, 0.0, 1.0}, - new double[] {2.0, 0.0, 0.0}, - new double[] {2.0, 3.0, 1.0}, - new double[] {3.0, 3.0, 1.0}, - new double[] {1.0, 1.0, 5.0} - }; + double[] ones = new double[] { 1.0, 1.0, 1.0, 1.0, 1.0 }; + double[][] values = new double[][] { + new double[] { 1.0, 0.0, 1.0 }, + new double[] { 2.0, 0.0, 0.0 }, + new double[] { 2.0, 3.0, 1.0 }, + new double[] { 3.0, 3.0, 1.0 }, + new double[] { 1.0, 1.0, 5.0 } }; WeightedMode weightedMode = new WeightedMode(ones, 3); double[] processedValues = weightedMode.processValues(values); @@ -99,7 +96,7 @@ public void testAggregateMultiValueArrays() { assertThat(processedValues[2], closeTo(0.244728471, 0.00001)); assertThat(weightedMode.aggregate(weightedMode.processValues(values)), equalTo(0.0)); - double[] variedWeights = new double[]{1.0, -1.0, .5, 1.0, 5.0}; + double[] variedWeights = new double[] { 1.0, -1.0, .5, 1.0, 5.0 }; weightedMode = new WeightedMode(variedWeights, 3); processedValues = weightedMode.processValues(values); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/WeightedSumTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/WeightedSumTests.java index b1a7f6a6df0cc..5e496931c4756 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/WeightedSumTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/ensemble/WeightedSumTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ml.inference.trainedmodel.ensemble; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.TargetType; import java.io.IOException; @@ -41,19 +41,18 @@ protected Writeable.Reader instanceReader() { } public void testAggregate() { - double[] ones = new double[]{1.0, 1.0, 1.0, 1.0, 1.0}; - double[][] values = new double[][]{ - new double[] {1.0}, - new double[] {2.0}, - new double[] {2.0}, - new double[] {3.0}, - new double[] {5.0} - }; + double[] ones = new double[] { 1.0, 1.0, 1.0, 1.0, 1.0 }; + double[][] values = new double[][] { + new double[] { 1.0 }, + new double[] { 2.0 }, + new double[] { 2.0 }, + new double[] { 3.0 }, + new double[] { 5.0 } }; WeightedSum weightedSum = new WeightedSum(ones); assertThat(weightedSum.aggregate(weightedSum.processValues(values)), equalTo(13.0)); - double[] variedWeights = new double[]{1.0, -1.0, .5, 1.0, 5.0}; + double[] variedWeights = new double[] { 1.0, -1.0, .5, 1.0, 5.0 }; weightedSum = new WeightedSum(variedWeights); assertThat(weightedSum.aggregate(weightedSum.processValues(values)), equalTo(28.0)); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/inference/EnsembleInferenceModelTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/inference/EnsembleInferenceModelTests.java index d304fcd35e42d..581d3af7d1e60 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/inference/EnsembleInferenceModelTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/inference/EnsembleInferenceModelTests.java @@ -9,9 +9,9 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.search.SearchModule; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.ml.inference.MlInferenceNamedXContentProvider; import org.elasticsearch.xpack.core.ml.inference.results.ClassificationInferenceResults; import org.elasticsearch.xpack.core.ml.inference.results.SingleValueInferenceResults; @@ -51,9 +51,7 @@ public class EnsembleInferenceModelTests extends ESTestCase { public static EnsembleInferenceModel serializeFromTrainedModel(Ensemble ensemble) throws IOException { NamedXContentRegistry registry = new NamedXContentRegistry(new MlInferenceNamedXContentProvider().getNamedXContentParsers()); - EnsembleInferenceModel model = deserializeFromTrainedModel(ensemble, - registry, - EnsembleInferenceModel::fromXContent); + EnsembleInferenceModel model = deserializeFromTrainedModel(ensemble, registry, EnsembleInferenceModel::fromXContent); model.rewriteFeatureIndices(Collections.emptyMap()); return model; } @@ -69,21 +67,23 @@ protected NamedXContentRegistry xContentRegistry() { public void testSerializationFromEnsemble() throws Exception { for (int i = 0; i < NUMBER_OF_TEST_RUNS; ++i) { int numberOfFeatures = randomIntBetween(1, 10); - Ensemble ensemble = EnsembleTests.createRandom(randomFrom(TargetType.values()), - randomBoolean() ? - Collections.emptyList() : - Stream.generate(() -> randomAlphaOfLength(10)).limit(numberOfFeatures).collect(Collectors.toList())); + Ensemble ensemble = EnsembleTests.createRandom( + randomFrom(TargetType.values()), + randomBoolean() + ? Collections.emptyList() + : Stream.generate(() -> randomAlphaOfLength(10)).limit(numberOfFeatures).collect(Collectors.toList()) + ); assertThat(serializeFromTrainedModel(ensemble), is(not(nullValue()))); } } public void testInferenceWithoutPreparing() throws IOException { - Ensemble ensemble = EnsembleTests.createRandom(TargetType.REGRESSION, - Stream.generate(() -> randomAlphaOfLength(10)).limit(4).collect(Collectors.toList())); + Ensemble ensemble = EnsembleTests.createRandom( + TargetType.REGRESSION, + Stream.generate(() -> randomAlphaOfLength(10)).limit(4).collect(Collectors.toList()) + ); - EnsembleInferenceModel model = deserializeFromTrainedModel(ensemble, - xContentRegistry(), - EnsembleInferenceModel::fromXContent); + EnsembleInferenceModel model = deserializeFromTrainedModel(ensemble, xContentRegistry(), EnsembleInferenceModel::fromXContent); expectThrows(ElasticsearchException.class, () -> model.infer(Collections.emptyMap(), RegressionConfig.EMPTY_PARAMS, null)); } @@ -91,36 +91,21 @@ public void testClassificationProbability() throws IOException { List featureNames = Arrays.asList("foo", "bar"); Tree tree1 = Tree.builder() .setFeatureNames(featureNames) - .setRoot(TreeNode.builder(0) - .setLeftChild(1) - .setRightChild(2) - .setSplitFeature(0) - .setThreshold(0.5)) + .setRoot(TreeNode.builder(0).setLeftChild(1).setRightChild(2).setSplitFeature(0).setThreshold(0.5)) .addNode(TreeNode.builder(1).setLeafValue(1.0)) - .addNode(TreeNode.builder(2) - .setThreshold(0.8) - .setSplitFeature(1) - .setLeftChild(3) - .setRightChild(4)) + .addNode(TreeNode.builder(2).setThreshold(0.8).setSplitFeature(1).setLeftChild(3).setRightChild(4)) .addNode(TreeNode.builder(3).setLeafValue(0.0)) - .addNode(TreeNode.builder(4).setLeafValue(1.0)).build(); + .addNode(TreeNode.builder(4).setLeafValue(1.0)) + .build(); Tree tree2 = Tree.builder() .setFeatureNames(featureNames) - .setRoot(TreeNode.builder(0) - .setLeftChild(1) - .setRightChild(2) - .setSplitFeature(0) - .setThreshold(0.5)) + .setRoot(TreeNode.builder(0).setLeftChild(1).setRightChild(2).setSplitFeature(0).setThreshold(0.5)) .addNode(TreeNode.builder(1).setLeafValue(0.0)) .addNode(TreeNode.builder(2).setLeafValue(1.0)) .build(); Tree tree3 = Tree.builder() .setFeatureNames(featureNames) - .setRoot(TreeNode.builder(0) - .setLeftChild(1) - .setRightChild(2) - .setSplitFeature(1) - .setThreshold(1.0)) + .setRoot(TreeNode.builder(0).setLeftChild(1).setRightChild(2).setSplitFeature(1).setThreshold(1.0)) .addNode(TreeNode.builder(1).setLeafValue(1.0)) .addNode(TreeNode.builder(2).setLeafValue(0.0)) .build(); @@ -128,24 +113,28 @@ public void testClassificationProbability() throws IOException { .setTargetType(TargetType.CLASSIFICATION) .setFeatureNames(featureNames) .setTrainedModels(Arrays.asList(tree1, tree2, tree3)) - .setOutputAggregator(new WeightedMode(new double[]{0.7, 0.5, 1.0}, 2)) + .setOutputAggregator(new WeightedMode(new double[] { 0.7, 0.5, 1.0 }, 2)) .setClassificationWeights(Arrays.asList(0.7, 0.3)) .build(); - EnsembleInferenceModel ensemble = deserializeFromTrainedModel(ensembleObject, + EnsembleInferenceModel ensemble = deserializeFromTrainedModel( + ensembleObject, xContentRegistry(), - EnsembleInferenceModel::fromXContent); + EnsembleInferenceModel::fromXContent + ); ensemble.rewriteFeatureIndices(Collections.emptyMap()); List featureVector = Arrays.asList(0.4, 0.0); Map featureMap = zipObjMap(featureNames, featureVector); List expected = Arrays.asList(0.768524783, 0.231475216); - List scores = Arrays.asList(0.230557435, 0.162032651); + List scores = Arrays.asList(0.230557435, 0.162032651); double eps = 0.000001; - List probabilities = - ((ClassificationInferenceResults)ensemble.infer(featureMap, new ClassificationConfig(2), Collections.emptyMap())) - .getTopClasses(); - for(int i = 0; i < expected.size(); i++) { + List probabilities = ((ClassificationInferenceResults) ensemble.infer( + featureMap, + new ClassificationConfig(2), + Collections.emptyMap() + )).getTopClasses(); + for (int i = 0; i < expected.size(); i++) { assertThat(probabilities.get(i).getProbability(), closeTo(expected.get(i), eps)); assertThat(probabilities.get(i).getScore(), closeTo(scores.get(i), eps)); } @@ -153,11 +142,10 @@ public void testClassificationProbability() throws IOException { featureVector = Arrays.asList(2.0, 0.7); featureMap = zipObjMap(featureNames, featureVector); expected = Arrays.asList(0.310025518, 0.6899744811); - scores = Arrays.asList(0.217017863, 0.2069923443); - probabilities = - ((ClassificationInferenceResults)ensemble.infer(featureMap, new ClassificationConfig(2), Collections.emptyMap())) - .getTopClasses(); - for(int i = 0; i < expected.size(); i++) { + scores = Arrays.asList(0.217017863, 0.2069923443); + probabilities = ((ClassificationInferenceResults) ensemble.infer(featureMap, new ClassificationConfig(2), Collections.emptyMap())) + .getTopClasses(); + for (int i = 0; i < expected.size(); i++) { assertThat(probabilities.get(i).getProbability(), closeTo(expected.get(i), eps)); assertThat(probabilities.get(i).getScore(), closeTo(scores.get(i), eps)); } @@ -165,26 +153,26 @@ public void testClassificationProbability() throws IOException { featureVector = Arrays.asList(0.0, 1.0); featureMap = zipObjMap(featureNames, featureVector); expected = Arrays.asList(0.768524783, 0.231475216); - scores = Arrays.asList(0.230557435, 0.162032651); - probabilities = - ((ClassificationInferenceResults)ensemble.infer(featureMap, new ClassificationConfig(2), Collections.emptyMap())) - .getTopClasses(); - for(int i = 0; i < expected.size(); i++) { + scores = Arrays.asList(0.230557435, 0.162032651); + probabilities = ((ClassificationInferenceResults) ensemble.infer(featureMap, new ClassificationConfig(2), Collections.emptyMap())) + .getTopClasses(); + for (int i = 0; i < expected.size(); i++) { assertThat(probabilities.get(i).getProbability(), closeTo(expected.get(i), eps)); assertThat(probabilities.get(i).getScore(), closeTo(scores.get(i), eps)); } // This should handle missing values and take the default_left path - featureMap = new HashMap<>(2) {{ - put("foo", 0.3); - put("bar", null); - }}; + featureMap = new HashMap<>(2) { + { + put("foo", 0.3); + put("bar", null); + } + }; expected = Arrays.asList(0.6899744811, 0.3100255188); - scores = Arrays.asList(0.482982136, 0.0930076556); - probabilities = - ((ClassificationInferenceResults)ensemble.infer(featureMap, new ClassificationConfig(2), Collections.emptyMap())) - .getTopClasses(); - for(int i = 0; i < expected.size(); i++) { + scores = Arrays.asList(0.482982136, 0.0930076556); + probabilities = ((ClassificationInferenceResults) ensemble.infer(featureMap, new ClassificationConfig(2), Collections.emptyMap())) + .getTopClasses(); + for (int i = 0; i < expected.size(); i++) { assertThat(probabilities.get(i).getProbability(), closeTo(expected.get(i), eps)); assertThat(probabilities.get(i).getScore(), closeTo(scores.get(i), eps)); } @@ -194,39 +182,23 @@ public void testClassificationInference() throws IOException { List featureNames = Arrays.asList("foo", "bar"); Tree tree1 = Tree.builder() .setFeatureNames(featureNames) - .setRoot(TreeNode.builder(0) - .setLeftChild(1) - .setRightChild(2) - .setSplitFeature(0) - .setThreshold(0.5)) + .setRoot(TreeNode.builder(0).setLeftChild(1).setRightChild(2).setSplitFeature(0).setThreshold(0.5)) .addNode(TreeNode.builder(1).setLeafValue(1.0)) - .addNode(TreeNode.builder(2) - .setThreshold(0.8) - .setSplitFeature(1) - .setLeftChild(3) - .setRightChild(4)) + .addNode(TreeNode.builder(2).setThreshold(0.8).setSplitFeature(1).setLeftChild(3).setRightChild(4)) .addNode(TreeNode.builder(3).setLeafValue(0.0)) .addNode(TreeNode.builder(4).setLeafValue(1.0)) .setTargetType(randomFrom(TargetType.CLASSIFICATION, TargetType.REGRESSION)) .build(); Tree tree2 = Tree.builder() .setFeatureNames(featureNames) - .setRoot(TreeNode.builder(0) - .setLeftChild(1) - .setRightChild(2) - .setSplitFeature(0) - .setThreshold(0.5)) + .setRoot(TreeNode.builder(0).setLeftChild(1).setRightChild(2).setSplitFeature(0).setThreshold(0.5)) .addNode(TreeNode.builder(1).setLeafValue(0.0)) .addNode(TreeNode.builder(2).setLeafValue(1.0)) .setTargetType(randomFrom(TargetType.CLASSIFICATION, TargetType.REGRESSION)) .build(); Tree tree3 = Tree.builder() .setFeatureNames(featureNames) - .setRoot(TreeNode.builder(0) - .setLeftChild(1) - .setRightChild(2) - .setSplitFeature(1) - .setThreshold(1.0)) + .setRoot(TreeNode.builder(0).setLeftChild(1).setRightChild(2).setSplitFeature(1).setThreshold(1.0)) .addNode(TreeNode.builder(1).setLeafValue(1.0)) .addNode(TreeNode.builder(2).setLeafValue(0.0)) .setTargetType(randomFrom(TargetType.CLASSIFICATION, TargetType.REGRESSION)) @@ -235,78 +207,82 @@ public void testClassificationInference() throws IOException { .setTargetType(TargetType.CLASSIFICATION) .setFeatureNames(featureNames) .setTrainedModels(Arrays.asList(tree1, tree2, tree3)) - .setOutputAggregator(new WeightedMode(new double[]{0.7, 0.5, 1.0}, 2)) + .setOutputAggregator(new WeightedMode(new double[] { 0.7, 0.5, 1.0 }, 2)) .build(); - EnsembleInferenceModel ensemble = deserializeFromTrainedModel(ensembleObject, + EnsembleInferenceModel ensemble = deserializeFromTrainedModel( + ensembleObject, xContentRegistry(), - EnsembleInferenceModel::fromXContent); + EnsembleInferenceModel::fromXContent + ); ensemble.rewriteFeatureIndices(Collections.emptyMap()); List featureVector = Arrays.asList(0.4, 0.0); Map featureMap = zipObjMap(featureNames, featureVector); - assertThat(1.0, - closeTo(((SingleValueInferenceResults)ensemble.infer(featureMap, new ClassificationConfig(0), Collections.emptyMap())).value(), - 0.00001)); + assertThat( + 1.0, + closeTo( + ((SingleValueInferenceResults) ensemble.infer(featureMap, new ClassificationConfig(0), Collections.emptyMap())).value(), + 0.00001 + ) + ); featureVector = Arrays.asList(2.0, 0.7); featureMap = zipObjMap(featureNames, featureVector); - assertThat(1.0, - closeTo(((SingleValueInferenceResults)ensemble.infer(featureMap, new ClassificationConfig(0), Collections.emptyMap())).value(), - 0.00001)); + assertThat( + 1.0, + closeTo( + ((SingleValueInferenceResults) ensemble.infer(featureMap, new ClassificationConfig(0), Collections.emptyMap())).value(), + 0.00001 + ) + ); featureVector = Arrays.asList(0.0, 1.0); featureMap = zipObjMap(featureNames, featureVector); - assertThat(1.0, - closeTo(((SingleValueInferenceResults)ensemble.infer(featureMap, new ClassificationConfig(0), Collections.emptyMap())).value(), - 0.00001)); - - featureMap = new HashMap<>(2) {{ - put("foo", 0.3); - put("bar", null); - }}; - assertThat(0.0, - closeTo(((SingleValueInferenceResults)ensemble.infer(featureMap, new ClassificationConfig(0), Collections.emptyMap())).value(), - 0.00001)); + assertThat( + 1.0, + closeTo( + ((SingleValueInferenceResults) ensemble.infer(featureMap, new ClassificationConfig(0), Collections.emptyMap())).value(), + 0.00001 + ) + ); + + featureMap = new HashMap<>(2) { + { + put("foo", 0.3); + put("bar", null); + } + }; + assertThat( + 0.0, + closeTo( + ((SingleValueInferenceResults) ensemble.infer(featureMap, new ClassificationConfig(0), Collections.emptyMap())).value(), + 0.00001 + ) + ); } public void testMultiClassClassificationInference() throws IOException { List featureNames = Arrays.asList("foo", "bar"); Tree tree1 = Tree.builder() .setFeatureNames(featureNames) - .setRoot(TreeNode.builder(0) - .setLeftChild(1) - .setRightChild(2) - .setSplitFeature(0) - .setThreshold(0.5)) + .setRoot(TreeNode.builder(0).setLeftChild(1).setRightChild(2).setSplitFeature(0).setThreshold(0.5)) .addNode(TreeNode.builder(1).setLeafValue(2.0)) - .addNode(TreeNode.builder(2) - .setThreshold(0.8) - .setSplitFeature(1) - .setLeftChild(3) - .setRightChild(4)) + .addNode(TreeNode.builder(2).setThreshold(0.8).setSplitFeature(1).setLeftChild(3).setRightChild(4)) .addNode(TreeNode.builder(3).setLeafValue(0.0)) .addNode(TreeNode.builder(4).setLeafValue(1.0)) .setTargetType(randomFrom(TargetType.CLASSIFICATION, TargetType.REGRESSION)) .build(); Tree tree2 = Tree.builder() .setFeatureNames(featureNames) - .setRoot(TreeNode.builder(0) - .setLeftChild(1) - .setRightChild(2) - .setSplitFeature(1) - .setThreshold(0.5)) + .setRoot(TreeNode.builder(0).setLeftChild(1).setRightChild(2).setSplitFeature(1).setThreshold(0.5)) .addNode(TreeNode.builder(1).setLeafValue(2.0)) .addNode(TreeNode.builder(2).setLeafValue(1.0)) .setTargetType(randomFrom(TargetType.CLASSIFICATION, TargetType.REGRESSION)) .build(); Tree tree3 = Tree.builder() .setFeatureNames(featureNames) - .setRoot(TreeNode.builder(0) - .setLeftChild(1) - .setRightChild(2) - .setSplitFeature(1) - .setThreshold(2.0)) + .setRoot(TreeNode.builder(0).setLeftChild(1).setRightChild(2).setSplitFeature(1).setThreshold(2.0)) .addNode(TreeNode.builder(1).setLeafValue(1.0)) .addNode(TreeNode.builder(2).setLeafValue(0.0)) .setTargetType(randomFrom(TargetType.CLASSIFICATION, TargetType.REGRESSION)) @@ -315,65 +291,74 @@ public void testMultiClassClassificationInference() throws IOException { .setTargetType(TargetType.CLASSIFICATION) .setFeatureNames(featureNames) .setTrainedModels(Arrays.asList(tree1, tree2, tree3)) - .setOutputAggregator(new WeightedMode(new double[]{0.7, 0.5, 1.0}, 3)) + .setOutputAggregator(new WeightedMode(new double[] { 0.7, 0.5, 1.0 }, 3)) .build(); - EnsembleInferenceModel ensemble = deserializeFromTrainedModel(ensembleObject, + EnsembleInferenceModel ensemble = deserializeFromTrainedModel( + ensembleObject, xContentRegistry(), - EnsembleInferenceModel::fromXContent); + EnsembleInferenceModel::fromXContent + ); ensemble.rewriteFeatureIndices(Collections.emptyMap()); List featureVector = Arrays.asList(0.4, 0.0); Map featureMap = zipObjMap(featureNames, featureVector); - assertThat(2.0, - closeTo(((SingleValueInferenceResults)ensemble.infer(featureMap, new ClassificationConfig(0), Collections.emptyMap())).value(), - 0.00001)); + assertThat( + 2.0, + closeTo( + ((SingleValueInferenceResults) ensemble.infer(featureMap, new ClassificationConfig(0), Collections.emptyMap())).value(), + 0.00001 + ) + ); featureVector = Arrays.asList(2.0, 0.7); featureMap = zipObjMap(featureNames, featureVector); - assertThat(1.0, - closeTo(((SingleValueInferenceResults)ensemble.infer(featureMap, new ClassificationConfig(0), Collections.emptyMap())).value(), - 0.00001)); + assertThat( + 1.0, + closeTo( + ((SingleValueInferenceResults) ensemble.infer(featureMap, new ClassificationConfig(0), Collections.emptyMap())).value(), + 0.00001 + ) + ); featureVector = Arrays.asList(0.0, 1.0); featureMap = zipObjMap(featureNames, featureVector); - assertThat(1.0, - closeTo(((SingleValueInferenceResults)ensemble.infer(featureMap, new ClassificationConfig(0), Collections.emptyMap())).value(), - 0.00001)); - - featureMap = new HashMap<>(2) {{ - put("foo", 0.6); - put("bar", null); - }}; - assertThat(1.0, - closeTo(((SingleValueInferenceResults)ensemble.infer(featureMap, new ClassificationConfig(0), Collections.emptyMap())).value(), - 0.00001)); + assertThat( + 1.0, + closeTo( + ((SingleValueInferenceResults) ensemble.infer(featureMap, new ClassificationConfig(0), Collections.emptyMap())).value(), + 0.00001 + ) + ); + + featureMap = new HashMap<>(2) { + { + put("foo", 0.6); + put("bar", null); + } + }; + assertThat( + 1.0, + closeTo( + ((SingleValueInferenceResults) ensemble.infer(featureMap, new ClassificationConfig(0), Collections.emptyMap())).value(), + 0.00001 + ) + ); } public void testRegressionInference() throws IOException { List featureNames = Arrays.asList("foo", "bar"); Tree tree1 = Tree.builder() .setFeatureNames(featureNames) - .setRoot(TreeNode.builder(0) - .setLeftChild(1) - .setRightChild(2) - .setSplitFeature(0) - .setThreshold(0.5)) + .setRoot(TreeNode.builder(0).setLeftChild(1).setRightChild(2).setSplitFeature(0).setThreshold(0.5)) .addNode(TreeNode.builder(1).setLeafValue(0.3)) - .addNode(TreeNode.builder(2) - .setThreshold(0.8) - .setSplitFeature(1) - .setLeftChild(3) - .setRightChild(4)) + .addNode(TreeNode.builder(2).setThreshold(0.8).setSplitFeature(1).setLeftChild(3).setRightChild(4)) .addNode(TreeNode.builder(3).setLeafValue(0.1)) - .addNode(TreeNode.builder(4).setLeafValue(0.2)).build(); + .addNode(TreeNode.builder(4).setLeafValue(0.2)) + .build(); Tree tree2 = Tree.builder() .setFeatureNames(featureNames) - .setRoot(TreeNode.builder(0) - .setLeftChild(1) - .setRightChild(2) - .setSplitFeature(0) - .setThreshold(0.5)) + .setRoot(TreeNode.builder(0).setLeftChild(1).setRightChild(2).setSplitFeature(0).setThreshold(0.5)) .addNode(TreeNode.builder(1).setLeafValue(1.5)) .addNode(TreeNode.builder(2).setLeafValue(0.9)) .build(); @@ -381,27 +366,35 @@ public void testRegressionInference() throws IOException { .setTargetType(TargetType.REGRESSION) .setFeatureNames(featureNames) .setTrainedModels(Arrays.asList(tree1, tree2)) - .setOutputAggregator(new WeightedSum(new double[]{0.5, 0.5})) + .setOutputAggregator(new WeightedSum(new double[] { 0.5, 0.5 })) .build(); - EnsembleInferenceModel ensemble = deserializeFromTrainedModel(ensembleObject, + EnsembleInferenceModel ensemble = deserializeFromTrainedModel( + ensembleObject, xContentRegistry(), - EnsembleInferenceModel::fromXContent); + EnsembleInferenceModel::fromXContent + ); ensemble.rewriteFeatureIndices(Collections.emptyMap()); List featureVector = Arrays.asList(0.4, 0.0); Map featureMap = zipObjMap(featureNames, featureVector); - assertThat(0.9, - closeTo(((SingleValueInferenceResults)ensemble.infer(featureMap, RegressionConfig.EMPTY_PARAMS, Collections.emptyMap())) - .value(), - 0.00001)); + assertThat( + 0.9, + closeTo( + ((SingleValueInferenceResults) ensemble.infer(featureMap, RegressionConfig.EMPTY_PARAMS, Collections.emptyMap())).value(), + 0.00001 + ) + ); featureVector = Arrays.asList(2.0, 0.7); featureMap = zipObjMap(featureNames, featureVector); - assertThat(0.5, - closeTo(((SingleValueInferenceResults)ensemble.infer(featureMap, RegressionConfig.EMPTY_PARAMS, Collections.emptyMap())) - .value(), - 0.00001)); + assertThat( + 0.5, + closeTo( + ((SingleValueInferenceResults) ensemble.infer(featureMap, RegressionConfig.EMPTY_PARAMS, Collections.emptyMap())).value(), + 0.00001 + ) + ); // Test with NO aggregator supplied, verifies default behavior of non-weighted sum ensembleObject = Ensemble.builder() @@ -409,33 +402,42 @@ public void testRegressionInference() throws IOException { .setFeatureNames(featureNames) .setTrainedModels(Arrays.asList(tree1, tree2)) .build(); - ensemble = deserializeFromTrainedModel(ensembleObject, - xContentRegistry(), - EnsembleInferenceModel::fromXContent); + ensemble = deserializeFromTrainedModel(ensembleObject, xContentRegistry(), EnsembleInferenceModel::fromXContent); ensemble.rewriteFeatureIndices(Collections.emptyMap()); featureVector = Arrays.asList(0.4, 0.0); featureMap = zipObjMap(featureNames, featureVector); - assertThat(1.8, - closeTo(((SingleValueInferenceResults)ensemble.infer(featureMap, RegressionConfig.EMPTY_PARAMS, Collections.emptyMap())) - .value(), - 0.00001)); + assertThat( + 1.8, + closeTo( + ((SingleValueInferenceResults) ensemble.infer(featureMap, RegressionConfig.EMPTY_PARAMS, Collections.emptyMap())).value(), + 0.00001 + ) + ); featureVector = Arrays.asList(2.0, 0.7); featureMap = zipObjMap(featureNames, featureVector); - assertThat(1.0, - closeTo(((SingleValueInferenceResults)ensemble.infer(featureMap, RegressionConfig.EMPTY_PARAMS, Collections.emptyMap())) - .value(), - 0.00001)); - - featureMap = new HashMap<>(2) {{ - put("foo", 0.3); - put("bar", null); - }}; - assertThat(1.8, - closeTo(((SingleValueInferenceResults)ensemble.infer(featureMap, RegressionConfig.EMPTY_PARAMS, Collections.emptyMap())) - .value(), - 0.00001)); + assertThat( + 1.0, + closeTo( + ((SingleValueInferenceResults) ensemble.infer(featureMap, RegressionConfig.EMPTY_PARAMS, Collections.emptyMap())).value(), + 0.00001 + ) + ); + + featureMap = new HashMap<>(2) { + { + put("foo", 0.3); + put("bar", null); + } + }; + assertThat( + 1.8, + closeTo( + ((SingleValueInferenceResults) ensemble.infer(featureMap, RegressionConfig.EMPTY_PARAMS, Collections.emptyMap())).value(), + 0.00001 + ) + ); } public void testFeatureImportance() throws IOException { @@ -467,7 +469,9 @@ public void testFeatureImportance() throws IOException { TreeNode.builder(3).setLeafValue(1.18230136).setNumberSamples(5L), TreeNode.builder(4).setLeafValue(1.98006658).setNumberSamples(1L), TreeNode.builder(5).setLeafValue(3.25350885).setNumberSamples(3L), - TreeNode.builder(6).setLeafValue(2.42384369).setNumberSamples(1L)).build(); + TreeNode.builder(6).setLeafValue(2.42384369).setNumberSamples(1L) + ) + .build(); Tree tree2 = Tree.builder() .setFeatureNames(featureNames) @@ -496,55 +500,60 @@ public void testFeatureImportance() throws IOException { TreeNode.builder(3).setLeafValue(1.04476388).setNumberSamples(3L), TreeNode.builder(4).setLeafValue(1.52799228).setNumberSamples(2L), TreeNode.builder(5).setLeafValue(1.98006658).setNumberSamples(1L), - TreeNode.builder(6).setLeafValue(2.950216).setNumberSamples(4L)).build(); + TreeNode.builder(6).setLeafValue(2.950216).setNumberSamples(4L) + ) + .build(); - Ensemble ensembleObject = Ensemble.builder().setOutputAggregator(new WeightedSum((double[])null)) + Ensemble ensembleObject = Ensemble.builder() + .setOutputAggregator(new WeightedSum((double[]) null)) .setTrainedModels(Arrays.asList(tree1, tree2)) .setFeatureNames(featureNames) .build(); - EnsembleInferenceModel ensemble = deserializeFromTrainedModel(ensembleObject, + EnsembleInferenceModel ensemble = deserializeFromTrainedModel( + ensembleObject, xContentRegistry(), - EnsembleInferenceModel::fromXContent); + EnsembleInferenceModel::fromXContent + ); ensemble.rewriteFeatureIndices(Collections.emptyMap()); - double[][] featureImportance = ensemble.featureImportance(new double[]{0.0, 0.9}); + double[][] featureImportance = ensemble.featureImportance(new double[] { 0.0, 0.9 }); assertThat(featureImportance[0][0], closeTo(-1.653200025, eps)); - assertThat(featureImportance[1][0], closeTo( -0.12444978, eps)); + assertThat(featureImportance[1][0], closeTo(-0.12444978, eps)); - featureImportance = ensemble.featureImportance(new double[]{0.1, 0.8}); + featureImportance = ensemble.featureImportance(new double[] { 0.1, 0.8 }); assertThat(featureImportance[0][0], closeTo(-1.653200025, eps)); - assertThat(featureImportance[1][0], closeTo( -0.12444978, eps)); + assertThat(featureImportance[1][0], closeTo(-0.12444978, eps)); - featureImportance = ensemble.featureImportance(new double[]{0.2, 0.7}); + featureImportance = ensemble.featureImportance(new double[] { 0.2, 0.7 }); assertThat(featureImportance[0][0], closeTo(-1.653200025, eps)); - assertThat(featureImportance[1][0], closeTo( -0.12444978, eps)); + assertThat(featureImportance[1][0], closeTo(-0.12444978, eps)); - featureImportance = ensemble.featureImportance(new double[]{0.3, 0.6}); + featureImportance = ensemble.featureImportance(new double[] { 0.3, 0.6 }); assertThat(featureImportance[0][0], closeTo(-1.16997162, eps)); - assertThat(featureImportance[1][0], closeTo( -0.12444978, eps)); + assertThat(featureImportance[1][0], closeTo(-0.12444978, eps)); - featureImportance = ensemble.featureImportance(new double[]{0.4, 0.5}); + featureImportance = ensemble.featureImportance(new double[] { 0.4, 0.5 }); assertThat(featureImportance[0][0], closeTo(-1.16997162, eps)); - assertThat(featureImportance[1][0], closeTo( -0.12444978, eps)); + assertThat(featureImportance[1][0], closeTo(-0.12444978, eps)); - featureImportance = ensemble.featureImportance(new double[]{0.5, 0.4}); + featureImportance = ensemble.featureImportance(new double[] { 0.5, 0.4 }); assertThat(featureImportance[0][0], closeTo(0.0798679, eps)); - assertThat(featureImportance[1][0], closeTo( -0.12444978, eps)); + assertThat(featureImportance[1][0], closeTo(-0.12444978, eps)); - featureImportance = ensemble.featureImportance(new double[]{0.6, 0.3}); + featureImportance = ensemble.featureImportance(new double[] { 0.6, 0.3 }); assertThat(featureImportance[0][0], closeTo(1.80491886, eps)); assertThat(featureImportance[1][0], closeTo(-0.4355742, eps)); - featureImportance = ensemble.featureImportance(new double[]{0.7, 0.2}); + featureImportance = ensemble.featureImportance(new double[] { 0.7, 0.2 }); assertThat(featureImportance[0][0], closeTo(2.0538184, eps)); assertThat(featureImportance[1][0], closeTo(0.1451914, eps)); - featureImportance = ensemble.featureImportance(new double[]{0.8, 0.1}); + featureImportance = ensemble.featureImportance(new double[] { 0.8, 0.1 }); assertThat(featureImportance[0][0], closeTo(2.0538184, eps)); assertThat(featureImportance[1][0], closeTo(0.1451914, eps)); - featureImportance = ensemble.featureImportance(new double[]{0.9, 0.0}); + featureImportance = ensemble.featureImportance(new double[] { 0.9, 0.0 }); assertThat(featureImportance[0][0], closeTo(2.0538184, eps)); assertThat(featureImportance[1][0], closeTo(0.1451914, eps)); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/inference/InferenceDefinitionTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/inference/InferenceDefinitionTests.java index 01f9c7261558f..aedd95b95bb8e 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/inference/InferenceDefinitionTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/inference/InferenceDefinitionTests.java @@ -8,16 +8,17 @@ package org.elasticsearch.xpack.core.ml.inference.trainedmodel.inference; import com.unboundid.util.Base64; + import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.search.SearchModule; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.search.SearchModule; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.ml.inference.InferenceToXContentCompressor; import org.elasticsearch.xpack.core.ml.inference.MlInferenceNamedXContentProvider; import org.elasticsearch.xpack.core.ml.inference.results.ClassificationFeatureImportance; @@ -64,72 +65,82 @@ public void testTreeSchemaDeserialization() throws IOException { public void testMultiClassIrisInference() throws IOException, ParseException { // Fairly simple, random forest classification model built to fit in our format // Trained on the well known Iris dataset - String compressedDef = "H4sIAPbiMl4C/+1b246bMBD9lVWet8jjG3b/oN9QVYgmToLEkghIL6r23wukl90" + - "YxRMGlt2WPKwEC/gYe2bOnBl+rOoyzQq3SR4OG5ev3t/9WLmicg+fc9cd1Gm5c3VSfz+2x6t1nlZVts3Wa" + - "Z0ditX93Wrr0vpUuqRIH1zVXPJxVbljmie5K3b1vr3ifPw125wPj65+9u/z8fnfn+4vh0jy9LPLzw/+UGb" + - "Vu8rVhyptb+wOv7iyytaH/FD+PZWVu6xo7u8e92x+3XOaSZVurtm1QydVXZ7W7XPPcIoGWpIVG/etOWbNR" + - "Ru3zqp28r+B5bVrH5a7bZ2s91m+aU5Cc6LMdvu/Z3gL55hndfILdnNOtGPuS1ftD901LDKs+wFYziy3j/d" + - "3FwjgKoJ0m3xJ81N7kvn3cix64aEH1gOfX8CXkVEtemFAahvz2IcgsBCkB0GhEMTKH1Ri3xn49yosYO0Bj" + - "hErDpGy3Y9JLbjSRvoQNAF+jIVvPPi2Bz67gK8iK1v0ptmsWoHoWXFDQG+x9/IeQ8Hbqm+swBGT15dr1wM" + - "CKDNA2yv0GKxE7b4+cwFBWDKQ+BlfDSgsat43tH94xD49diMtoeEVhgaN2mi6iwzMKqFjKUDPEBqCrmq6O" + - "HHd0PViMreajEEFJxlaccAi4B4CgdhzHBHdOcFqCSYTI14g2WS2z0007DfAe4Hy7DdkrI2I+9yGIhitJhh" + - "tTBjXYN+axcX1Ab7Oom2P+RgAtffDLj/A0a5vfkAbL/jWCwJHj9jT3afMzSQtQJYEhR6ibQ984+McsYQqg" + - "m4baTBKMB6LHhDo/Aj8BInDcI6q0ePG/rgMx+57hkXnU+AnVGBxCWH3zq3ijclwI/tW3lC2jSVsWM4oN1O" + - "SIc4XkjRGXjGEosylOUkUQ7AhhkBgSXYc1YvAksw4PG1kGWsAT5tOxbruOKbTnwIkSYxD1MbXsWAIUwMKz" + - "eGUeDUbRwI9Fkek5CiwqAM3Bz6NUgdUt+vBslhIo8UM6kDQac4kDiicpHfe+FwY2SQI5q3oadvnoQ3hMHE" + - "pCaHUgkqoVcRCG5aiKzCUCN03cUtJ4ikJxZTVlcWvDvarL626DiiVLH71pf0qG1y9H7mEPSQBNoTtQpFba" + - "NzfDFfXSNJqPFJBkFb/1iiNLxhSAW3u4Ns7qHHi+i1F9fmyj1vV0sDIZonP0wh+waxjLr1vOPcmxORe7n3" + - "pKOKIhVp9Rtb4+Owa3xCX/TpFPnrig6nKTNisNl8aNEKQRfQITh9kG/NhTzcvpwRZoARZvkh8S6h7Oz1zI" + - "atZeuYWk5nvC4TJ2aFFJXBCTkcO9UuQQ0qb3FXdx4xTPH6dBeApP0CQ43QejN8kd7l64jI1krMVgJfPEf7" + - "h3uq3o/K/ztZqP1QKFagz/G+t1XxwjeIFuqkRbXoTdlOTGnwCIoKZ6ku1AbrBoN6oCdX56w3UEOO0y2B9g" + - "aLbAYWcAdpeweKa2IfIT2jz5QzXxD6AoP+DrdXtxeluV7pdWrvkcKqPp7rjS19d+wp/fff/5Ez3FPjzFNy" + - "fdpTi9JB0sDp2JR7b309mn5HuPkEAAA=="; + String compressedDef = "H4sIAPbiMl4C/+1b246bMBD9lVWet8jjG3b/oN9QVYgmToLEkghIL6r23wukl90" + + "YxRMGlt2WPKwEC/gYe2bOnBl+rOoyzQq3SR4OG5ev3t/9WLmicg+fc9cd1Gm5c3VSfz+2x6t1nlZVts3Wa" + + "Z0ditX93Wrr0vpUuqRIH1zVXPJxVbljmie5K3b1vr3ifPw125wPj65+9u/z8fnfn+4vh0jy9LPLzw/+UGb" + + "Vu8rVhyptb+wOv7iyytaH/FD+PZWVu6xo7u8e92x+3XOaSZVurtm1QydVXZ7W7XPPcIoGWpIVG/etOWbNR" + + "Ru3zqp28r+B5bVrH5a7bZ2s91m+aU5Cc6LMdvu/Z3gL55hndfILdnNOtGPuS1ftD901LDKs+wFYziy3j/d" + + "3FwjgKoJ0m3xJ81N7kvn3cix64aEH1gOfX8CXkVEtemFAahvz2IcgsBCkB0GhEMTKH1Ri3xn49yosYO0Bj" + + "hErDpGy3Y9JLbjSRvoQNAF+jIVvPPi2Bz67gK8iK1v0ptmsWoHoWXFDQG+x9/IeQ8Hbqm+swBGT15dr1wM" + + "CKDNA2yv0GKxE7b4+cwFBWDKQ+BlfDSgsat43tH94xD49diMtoeEVhgaN2mi6iwzMKqFjKUDPEBqCrmq6O" + + "HHd0PViMreajEEFJxlaccAi4B4CgdhzHBHdOcFqCSYTI14g2WS2z0007DfAe4Hy7DdkrI2I+9yGIhitJhh" + + "tTBjXYN+axcX1Ab7Oom2P+RgAtffDLj/A0a5vfkAbL/jWCwJHj9jT3afMzSQtQJYEhR6ibQ984+McsYQqg" + + "m4baTBKMB6LHhDo/Aj8BInDcI6q0ePG/rgMx+57hkXnU+AnVGBxCWH3zq3ijclwI/tW3lC2jSVsWM4oN1O" + + "SIc4XkjRGXjGEosylOUkUQ7AhhkBgSXYc1YvAksw4PG1kGWsAT5tOxbruOKbTnwIkSYxD1MbXsWAIUwMKz" + + "eGUeDUbRwI9Fkek5CiwqAM3Bz6NUgdUt+vBslhIo8UM6kDQac4kDiicpHfe+FwY2SQI5q3oadvnoQ3hMHE" + + "pCaHUgkqoVcRCG5aiKzCUCN03cUtJ4ikJxZTVlcWvDvarL626DiiVLH71pf0qG1y9H7mEPSQBNoTtQpFba" + + "NzfDFfXSNJqPFJBkFb/1iiNLxhSAW3u4Ns7qHHi+i1F9fmyj1vV0sDIZonP0wh+waxjLr1vOPcmxORe7n3" + + "pKOKIhVp9Rtb4+Owa3xCX/TpFPnrig6nKTNisNl8aNEKQRfQITh9kG/NhTzcvpwRZoARZvkh8S6h7Oz1zI" + + "atZeuYWk5nvC4TJ2aFFJXBCTkcO9UuQQ0qb3FXdx4xTPH6dBeApP0CQ43QejN8kd7l64jI1krMVgJfPEf7" + + "h3uq3o/K/ztZqP1QKFagz/G+t1XxwjeIFuqkRbXoTdlOTGnwCIoKZ6ku1AbrBoN6oCdX56w3UEOO0y2B9g" + + "aLbAYWcAdpeweKa2IfIT2jz5QzXxD6AoP+DrdXtxeluV7pdWrvkcKqPp7rjS19d+wp/fff/5Ez3FPjzFNy" + + "fdpTi9JB0sDp2JR7b309mn5HuPkEAAA=="; byte[] bytes = Base64.decode(compressedDef); - InferenceDefinition definition = InferenceToXContentCompressor.inflate(new BytesArray(bytes), + InferenceDefinition definition = InferenceToXContentCompressor.inflate( + new BytesArray(bytes), InferenceDefinition::fromXContent, - xContentRegistry()); + xContentRegistry() + ); - Map fields = new HashMap<>(){{ - put("sepal_length", 5.1); - put("sepal_width", 3.5); - put("petal_length", 1.4); - put("petal_width", 0.2); - }}; + Map fields = new HashMap<>() { + { + put("sepal_length", 5.1); + put("sepal_width", 3.5); + put("petal_length", 1.4); + put("petal_width", 0.2); + } + }; assertThat( - ((ClassificationInferenceResults)definition.infer(fields, ClassificationConfig.EMPTY_PARAMS)) - .getClassificationLabel(), - equalTo("Iris-setosa")); + ((ClassificationInferenceResults) definition.infer(fields, ClassificationConfig.EMPTY_PARAMS)).getClassificationLabel(), + equalTo("Iris-setosa") + ); - fields = new HashMap<>(){{ - put("sepal_length", 7.0); - put("sepal_width", 3.2); - put("petal_length", 4.7); - put("petal_width", 1.4); - }}; + fields = new HashMap<>() { + { + put("sepal_length", 7.0); + put("sepal_width", 3.2); + put("petal_length", 4.7); + put("petal_width", 1.4); + } + }; assertThat( - ((ClassificationInferenceResults)definition.infer(fields, ClassificationConfig.EMPTY_PARAMS)) - .getClassificationLabel(), - equalTo("Iris-versicolor")); + ((ClassificationInferenceResults) definition.infer(fields, ClassificationConfig.EMPTY_PARAMS)).getClassificationLabel(), + equalTo("Iris-versicolor") + ); - fields = new HashMap<>(){{ - put("sepal_length", 6.5); - put("sepal_width", 3.0); - put("petal_length", 5.2); - put("petal_width", 2.0); - }}; + fields = new HashMap<>() { + { + put("sepal_length", 6.5); + put("sepal_width", 3.0); + put("petal_length", 5.2); + put("petal_width", 2.0); + } + }; assertThat( - ((ClassificationInferenceResults)definition.infer(fields, ClassificationConfig.EMPTY_PARAMS)) - .getClassificationLabel(), - equalTo("Iris-virginica")); + ((ClassificationInferenceResults) definition.infer(fields, ClassificationConfig.EMPTY_PARAMS)).getClassificationLabel(), + equalTo("Iris-virginica") + ); } public void testComplexInferenceDefinitionInfer() throws IOException { - XContentParser parser = XContentHelper.createParser(xContentRegistry(), + XContentParser parser = XContentHelper.createParser( + xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, new BytesArray(getClassificationDefinition(false)), - XContentType.JSON); + XContentType.JSON + ); InferenceDefinition inferenceDefinition = InferenceDefinition.fromXContent(parser); ClassificationConfig config = new ClassificationConfig(2, null, null, 2, null); @@ -148,10 +159,12 @@ public void testComplexInferenceDefinitionInfer() throws IOException { } public void testComplexInferenceDefinitionInferWithCustomPreProcessor() throws IOException { - XContentParser parser = XContentHelper.createParser(xContentRegistry(), + XContentParser parser = XContentHelper.createParser( + xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, new BytesArray(getClassificationDefinition(true)), - XContentType.JSON); + XContentType.JSON + ); InferenceDefinition inferenceDefinition = InferenceDefinition.fromXContent(parser); ClassificationConfig config = new ClassificationConfig(2, null, null, 2, null); @@ -186,132 +199,134 @@ public void testComplexInferenceDefinitionInferWithCustomPreProcessor() throws I } public static String getClassificationDefinition(boolean customPreprocessor) { - return "{" + - " \"preprocessors\": [\n" + - " {\n" + - " \"one_hot_encoding\": {\n" + - " \"field\": \"col1\",\n" + - " \"custom\": " + customPreprocessor + ",\n" + - " \"hot_map\": {\n" + - " \"male\": \"col1_male\",\n" + - " \"female\": \"col1_female\"\n" + - " }\n" + - " }\n" + - " },\n" + - " {\n" + - " \"target_mean_encoding\": {\n" + - " \"field\": \"col2\",\n" + - " \"feature_name\": \"col2_encoded\",\n" + - " \"target_map\": {\n" + - " \"S\": 5.0,\n" + - " \"M\": 10.0,\n" + - " \"L\": 20\n" + - " },\n" + - " \"default_value\": 5.0\n" + - " }\n" + - " },\n" + - " {\n" + - " \"frequency_encoding\": {\n" + - " \"field\": \"col3\",\n" + - " \"feature_name\": \"col3_encoded\",\n" + - " \"frequency_map\": {\n" + - " \"none\": 0.75,\n" + - " \"true\": 0.10,\n" + - " \"false\": 0.15\n" + - " }\n" + - " }\n" + - " }\n" + - " ],\n" + - " \"trained_model\": {\n" + - " \"ensemble\": {\n" + - " \"feature_names\": [\n" + - " \"col1_male\",\n" + - " \"col1_female\",\n" + - " \"col2_encoded\",\n" + - " \"col3_encoded\",\n" + - " \"col4\"\n" + - " ],\n" + - " \"aggregate_output\": {\n" + - " \"weighted_mode\": {\n" + - " \"num_classes\": \"2\",\n" + - " \"weights\": [\n" + - " 0.5,\n" + - " 0.5\n" + - " ]\n" + - " }\n" + - " },\n" + - " \"target_type\": \"classification\",\n" + - " \"classification_labels\": [\"first\", \"second\"],\n" + - " \"trained_models\": [\n" + - " {\n" + - " \"tree\": {\n" + - " \"feature_names\": [\n" + - " \"col1_male\",\n" + - " \"col1_female\",\n" + - " \"col4\"\n" + - " ],\n" + - " \"tree_structure\": [\n" + - " {\n" + - " \"node_index\": 0,\n" + - " \"split_feature\": 0,\n" + - " \"number_samples\": 100,\n" + - " \"split_gain\": 12.0,\n" + - " \"threshold\": 10.0,\n" + - " \"decision_type\": \"lte\",\n" + - " \"default_left\": true,\n" + - " \"left_child\": 1,\n" + - " \"right_child\": 2\n" + - " },\n" + - " {\n" + - " \"node_index\": 1,\n" + - " \"number_samples\": 80,\n" + - " \"leaf_value\": 1\n" + - " },\n" + - " {\n" + - " \"node_index\": 2,\n" + - " \"number_samples\": 20,\n" + - " \"leaf_value\": 0\n" + - " }\n" + - " ],\n" + - " \"target_type\": \"regression\"\n" + - " }\n" + - " },\n" + - " {\n" + - " \"tree\": {\n" + - " \"feature_names\": [\n" + - " \"col2_encoded\",\n" + - " \"col3_encoded\",\n" + - " \"col4\"\n" + - " ],\n" + - " \"tree_structure\": [\n" + - " {\n" + - " \"node_index\": 0,\n" + - " \"split_feature\": 0,\n" + - " \"split_gain\": 12.0,\n" + - " \"number_samples\": 180,\n" + - " \"threshold\": 10.0,\n" + - " \"decision_type\": \"lte\",\n" + - " \"default_left\": true,\n" + - " \"left_child\": 1,\n" + - " \"right_child\": 2\n" + - " },\n" + - " {\n" + - " \"node_index\": 1,\n" + - " \"number_samples\": 10,\n" + - " \"leaf_value\": 1\n" + - " },\n" + - " {\n" + - " \"node_index\": 2,\n" + - " \"number_samples\": 170,\n" + - " \"leaf_value\": 0\n" + - " }\n" + - " ],\n" + - " \"target_type\": \"regression\"\n" + - " }\n" + - " }\n" + - " ]\n" + - " }\n" + - " }\n" + - "}"; + return "{" + + " \"preprocessors\": [\n" + + " {\n" + + " \"one_hot_encoding\": {\n" + + " \"field\": \"col1\",\n" + + " \"custom\": " + + customPreprocessor + + ",\n" + + " \"hot_map\": {\n" + + " \"male\": \"col1_male\",\n" + + " \"female\": \"col1_female\"\n" + + " }\n" + + " }\n" + + " },\n" + + " {\n" + + " \"target_mean_encoding\": {\n" + + " \"field\": \"col2\",\n" + + " \"feature_name\": \"col2_encoded\",\n" + + " \"target_map\": {\n" + + " \"S\": 5.0,\n" + + " \"M\": 10.0,\n" + + " \"L\": 20\n" + + " },\n" + + " \"default_value\": 5.0\n" + + " }\n" + + " },\n" + + " {\n" + + " \"frequency_encoding\": {\n" + + " \"field\": \"col3\",\n" + + " \"feature_name\": \"col3_encoded\",\n" + + " \"frequency_map\": {\n" + + " \"none\": 0.75,\n" + + " \"true\": 0.10,\n" + + " \"false\": 0.15\n" + + " }\n" + + " }\n" + + " }\n" + + " ],\n" + + " \"trained_model\": {\n" + + " \"ensemble\": {\n" + + " \"feature_names\": [\n" + + " \"col1_male\",\n" + + " \"col1_female\",\n" + + " \"col2_encoded\",\n" + + " \"col3_encoded\",\n" + + " \"col4\"\n" + + " ],\n" + + " \"aggregate_output\": {\n" + + " \"weighted_mode\": {\n" + + " \"num_classes\": \"2\",\n" + + " \"weights\": [\n" + + " 0.5,\n" + + " 0.5\n" + + " ]\n" + + " }\n" + + " },\n" + + " \"target_type\": \"classification\",\n" + + " \"classification_labels\": [\"first\", \"second\"],\n" + + " \"trained_models\": [\n" + + " {\n" + + " \"tree\": {\n" + + " \"feature_names\": [\n" + + " \"col1_male\",\n" + + " \"col1_female\",\n" + + " \"col4\"\n" + + " ],\n" + + " \"tree_structure\": [\n" + + " {\n" + + " \"node_index\": 0,\n" + + " \"split_feature\": 0,\n" + + " \"number_samples\": 100,\n" + + " \"split_gain\": 12.0,\n" + + " \"threshold\": 10.0,\n" + + " \"decision_type\": \"lte\",\n" + + " \"default_left\": true,\n" + + " \"left_child\": 1,\n" + + " \"right_child\": 2\n" + + " },\n" + + " {\n" + + " \"node_index\": 1,\n" + + " \"number_samples\": 80,\n" + + " \"leaf_value\": 1\n" + + " },\n" + + " {\n" + + " \"node_index\": 2,\n" + + " \"number_samples\": 20,\n" + + " \"leaf_value\": 0\n" + + " }\n" + + " ],\n" + + " \"target_type\": \"regression\"\n" + + " }\n" + + " },\n" + + " {\n" + + " \"tree\": {\n" + + " \"feature_names\": [\n" + + " \"col2_encoded\",\n" + + " \"col3_encoded\",\n" + + " \"col4\"\n" + + " ],\n" + + " \"tree_structure\": [\n" + + " {\n" + + " \"node_index\": 0,\n" + + " \"split_feature\": 0,\n" + + " \"split_gain\": 12.0,\n" + + " \"number_samples\": 180,\n" + + " \"threshold\": 10.0,\n" + + " \"decision_type\": \"lte\",\n" + + " \"default_left\": true,\n" + + " \"left_child\": 1,\n" + + " \"right_child\": 2\n" + + " },\n" + + " {\n" + + " \"node_index\": 1,\n" + + " \"number_samples\": 10,\n" + + " \"leaf_value\": 1\n" + + " },\n" + + " {\n" + + " \"node_index\": 2,\n" + + " \"number_samples\": 170,\n" + + " \"leaf_value\": 0\n" + + " }\n" + + " ],\n" + + " \"target_type\": \"regression\"\n" + + " }\n" + + " }\n" + + " ]\n" + + " }\n" + + " }\n" + + "}"; } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/inference/InferenceModelTestUtils.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/inference/InferenceModelTestUtils.java index 93c0ac999bb22..af837809279e3 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/inference/InferenceModelTestUtils.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/inference/InferenceModelTestUtils.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ml.inference.trainedmodel.inference; -import org.elasticsearch.core.CheckedFunction; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.core.CheckedFunction; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentBuilder; @@ -26,16 +26,15 @@ final class InferenceModelTestUtils { static U deserializeFromTrainedModel( T trainedModel, NamedXContentRegistry registry, - CheckedFunction parser) throws IOException { - try(XContentBuilder builder = trainedModel.toXContent(XContentFactory.jsonBuilder(), EMPTY_PARAMS); - XContentParser xContentParser = XContentType.JSON - .xContent() - .createParser(registry, - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, - BytesReference.bytes(builder).streamInput())) { + CheckedFunction parser + ) throws IOException { + try ( + XContentBuilder builder = trainedModel.toXContent(XContentFactory.jsonBuilder(), EMPTY_PARAMS); + XContentParser xContentParser = XContentType.JSON.xContent() + .createParser(registry, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, BytesReference.bytes(builder).streamInput()) + ) { return parser.apply(xContentParser); } } - } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/inference/TreeInferenceModelTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/inference/TreeInferenceModelTests.java index 480a938d1264a..dd67246480de5 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/inference/TreeInferenceModelTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/inference/TreeInferenceModelTests.java @@ -9,9 +9,9 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.search.SearchModule; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.ml.inference.MlInferenceNamedXContentProvider; import org.elasticsearch.xpack.core.ml.inference.results.ClassificationInferenceResults; import org.elasticsearch.xpack.core.ml.inference.results.SingleValueInferenceResults; @@ -48,9 +48,7 @@ public class TreeInferenceModelTests extends ESTestCase { public static TreeInferenceModel serializeFromTrainedModel(Tree tree) throws IOException { NamedXContentRegistry registry = new NamedXContentRegistry(new MlInferenceNamedXContentProvider().getNamedXContentParsers()); - TreeInferenceModel model = deserializeFromTrainedModel(tree, - registry, - TreeInferenceModel::fromXContent); + TreeInferenceModel model = deserializeFromTrainedModel(tree, registry, TreeInferenceModel::fromXContent); model.rewriteFeatureIndices(Collections.emptyMap()); return model; } @@ -66,9 +64,10 @@ protected NamedXContentRegistry xContentRegistry() { public void testCtorWithNullTargetType() { TreeInferenceModel treeInferenceModel = new TreeInferenceModel( Collections.emptyList(), - Collections.singletonList(new TreeInferenceModel.NodeBuilder().setLeafValue(new double[]{1.0}).setNumberSamples(100L)), + Collections.singletonList(new TreeInferenceModel.NodeBuilder().setLeafValue(new double[] { 1.0 }).setNumberSamples(100L)), null, - Collections.emptyList()); + Collections.emptyList() + ); assertThat(treeInferenceModel.targetType(), equalTo(TargetType.REGRESSION)); } @@ -82,9 +81,7 @@ public void testSerializationFromEnsemble() throws Exception { public void testInferenceWithoutPreparing() throws IOException { Tree tree = TreeTests.createRandom(randomFrom(TargetType.values())); - TreeInferenceModel model = deserializeFromTrainedModel(tree, - xContentRegistry(), - TreeInferenceModel::fromXContent); + TreeInferenceModel model = deserializeFromTrainedModel(tree, xContentRegistry(), TreeInferenceModel::fromXContent); expectThrows(ElasticsearchException.class, () -> model.infer(Collections.emptyMap(), RegressionConfig.EMPTY_PARAMS, null)); } @@ -94,16 +91,18 @@ public void testInferWithStump() throws IOException { builder.setFeatureNames(Collections.emptyList()); Tree treeObject = builder.build(); - TreeInferenceModel tree = deserializeFromTrainedModel(treeObject, - xContentRegistry(), - TreeInferenceModel::fromXContent); + TreeInferenceModel tree = deserializeFromTrainedModel(treeObject, xContentRegistry(), TreeInferenceModel::fromXContent); tree.rewriteFeatureIndices(Collections.emptyMap()); List featureNames = Arrays.asList("foo", "bar"); List featureVector = Arrays.asList(0.6, 0.0); Map featureMap = zipObjMap(featureNames, featureVector); // does not really matter as this is a stump - assertThat(42.0, - closeTo(((SingleValueInferenceResults)tree.infer(featureMap, RegressionConfig.EMPTY_PARAMS, Collections.emptyMap())).value(), - 0.00001)); + assertThat( + 42.0, + closeTo( + ((SingleValueInferenceResults) tree.infer(featureMap, RegressionConfig.EMPTY_PARAMS, Collections.emptyMap())).value(), + 0.00001 + ) + ); } public void testInfer() throws IOException { @@ -118,48 +117,68 @@ public void testInfer() throws IOException { List featureNames = Arrays.asList("foo", "bar"); Tree treeObject = builder.setFeatureNames(featureNames).build(); - TreeInferenceModel tree = deserializeFromTrainedModel(treeObject, - xContentRegistry(), - TreeInferenceModel::fromXContent); + TreeInferenceModel tree = deserializeFromTrainedModel(treeObject, xContentRegistry(), TreeInferenceModel::fromXContent); tree.rewriteFeatureIndices(Collections.emptyMap()); // This feature vector should hit the right child of the root node List featureVector = Arrays.asList(0.6, 0.0); Map featureMap = zipObjMap(featureNames, featureVector); - assertThat(0.3, - closeTo(((SingleValueInferenceResults)tree.infer(featureMap, RegressionConfig.EMPTY_PARAMS, Collections.emptyMap())).value(), - 0.00001)); + assertThat( + 0.3, + closeTo( + ((SingleValueInferenceResults) tree.infer(featureMap, RegressionConfig.EMPTY_PARAMS, Collections.emptyMap())).value(), + 0.00001 + ) + ); // This should hit the left child of the left child of the root node // i.e. it takes the path left, left featureVector = Arrays.asList(0.3, 0.7); featureMap = zipObjMap(featureNames, featureVector); - assertThat(0.1, - closeTo(((SingleValueInferenceResults)tree.infer(featureMap, RegressionConfig.EMPTY_PARAMS, Collections.emptyMap())).value(), - 0.00001)); + assertThat( + 0.1, + closeTo( + ((SingleValueInferenceResults) tree.infer(featureMap, RegressionConfig.EMPTY_PARAMS, Collections.emptyMap())).value(), + 0.00001 + ) + ); // This should hit the right child of the left child of the root node // i.e. it takes the path left, right featureVector = Arrays.asList(0.3, 0.9); featureMap = zipObjMap(featureNames, featureVector); - assertThat(0.2, - closeTo(((SingleValueInferenceResults)tree.infer(featureMap, RegressionConfig.EMPTY_PARAMS, Collections.emptyMap())).value(), - 0.00001)); + assertThat( + 0.2, + closeTo( + ((SingleValueInferenceResults) tree.infer(featureMap, RegressionConfig.EMPTY_PARAMS, Collections.emptyMap())).value(), + 0.00001 + ) + ); // This should still work if the internal values are strings List featureVectorStrings = Arrays.asList("0.3", "0.9"); featureMap = zipObjMap(featureNames, featureVectorStrings); - assertThat(0.2, - closeTo(((SingleValueInferenceResults)tree.infer(featureMap, RegressionConfig.EMPTY_PARAMS, Collections.emptyMap())).value(), - 0.00001)); + assertThat( + 0.2, + closeTo( + ((SingleValueInferenceResults) tree.infer(featureMap, RegressionConfig.EMPTY_PARAMS, Collections.emptyMap())).value(), + 0.00001 + ) + ); // This should handle missing values and take the default_left path - featureMap = new HashMap<>(2, 1.0f) {{ - put("foo", 0.3); - put("bar", null); - }}; - assertThat(0.1, - closeTo(((SingleValueInferenceResults)tree.infer(featureMap, RegressionConfig.EMPTY_PARAMS, Collections.emptyMap())).value(), - 0.00001)); + featureMap = new HashMap<>(2, 1.0f) { + { + put("foo", 0.3); + put("bar", null); + } + }; + assertThat( + 0.1, + closeTo( + ((SingleValueInferenceResults) tree.infer(featureMap, RegressionConfig.EMPTY_PARAMS, Collections.emptyMap())).value(), + 0.00001 + ) + ); } public void testTreeClassificationProbability() throws IOException { @@ -174,9 +193,7 @@ public void testTreeClassificationProbability() throws IOException { List featureNames = Arrays.asList("foo", "bar"); Tree treeObject = builder.setFeatureNames(featureNames).setClassificationLabels(Arrays.asList("cat", "dog")).build(); - TreeInferenceModel tree = deserializeFromTrainedModel(treeObject, - xContentRegistry(), - TreeInferenceModel::fromXContent); + TreeInferenceModel tree = deserializeFromTrainedModel(treeObject, xContentRegistry(), TreeInferenceModel::fromXContent); tree.rewriteFeatureIndices(Collections.emptyMap()); double eps = 0.000001; // This feature vector should hit the right child of the root node @@ -184,10 +201,12 @@ public void testTreeClassificationProbability() throws IOException { List expectedProbs = Arrays.asList(1.0, 0.0); List expectedFields = Arrays.asList("dog", "cat"); Map featureMap = zipObjMap(featureNames, featureVector); - List probabilities = - ((ClassificationInferenceResults)tree.infer(featureMap, new ClassificationConfig(2), Collections.emptyMap())) - .getTopClasses(); - for(int i = 0; i < expectedProbs.size(); i++) { + List probabilities = ((ClassificationInferenceResults) tree.infer( + featureMap, + new ClassificationConfig(2), + Collections.emptyMap() + )).getTopClasses(); + for (int i = 0; i < expectedProbs.size(); i++) { assertThat(probabilities.get(i).getProbability(), closeTo(expectedProbs.get(i), eps)); assertThat(probabilities.get(i).getClassification(), equalTo(expectedFields.get(i))); } @@ -196,23 +215,23 @@ public void testTreeClassificationProbability() throws IOException { // i.e. it takes the path left, left featureVector = Arrays.asList(0.3, 0.7); featureMap = zipObjMap(featureNames, featureVector); - probabilities = - ((ClassificationInferenceResults)tree.infer(featureMap, new ClassificationConfig(2), Collections.emptyMap())) - .getTopClasses(); - for(int i = 0; i < expectedProbs.size(); i++) { + probabilities = ((ClassificationInferenceResults) tree.infer(featureMap, new ClassificationConfig(2), Collections.emptyMap())) + .getTopClasses(); + for (int i = 0; i < expectedProbs.size(); i++) { assertThat(probabilities.get(i).getProbability(), closeTo(expectedProbs.get(i), eps)); assertThat(probabilities.get(i).getClassification(), equalTo(expectedFields.get(i))); } // This should handle missing values and take the default_left path - featureMap = new HashMap<>(2) {{ - put("foo", 0.3); - put("bar", null); - }}; - probabilities = - ((ClassificationInferenceResults)tree.infer(featureMap, new ClassificationConfig(2), Collections.emptyMap())) - .getTopClasses(); - for(int i = 0; i < expectedProbs.size(); i++) { + featureMap = new HashMap<>(2) { + { + put("foo", 0.3); + put("bar", null); + } + }; + probabilities = ((ClassificationInferenceResults) tree.infer(featureMap, new ClassificationConfig(2), Collections.emptyMap())) + .getTopClasses(); + for (int i = 0; i < expectedProbs.size(); i++) { assertThat(probabilities.get(i).getProbability(), closeTo(expectedProbs.get(i), eps)); assertThat(probabilities.get(i).getClassification(), equalTo(expectedFields.get(i))); } @@ -247,26 +266,26 @@ public void testFeatureImportance() throws IOException { TreeNode.builder(3).setLeafValue(3.0).setNumberSamples(1L), TreeNode.builder(4).setLeafValue(8.0).setNumberSamples(1L), TreeNode.builder(5).setLeafValue(13.0).setNumberSamples(1L), - TreeNode.builder(6).setLeafValue(18.0).setNumberSamples(1L)).build(); + TreeNode.builder(6).setLeafValue(18.0).setNumberSamples(1L) + ) + .build(); - TreeInferenceModel tree = deserializeFromTrainedModel(treeObject, - xContentRegistry(), - TreeInferenceModel::fromXContent); + TreeInferenceModel tree = deserializeFromTrainedModel(treeObject, xContentRegistry(), TreeInferenceModel::fromXContent); tree.rewriteFeatureIndices(Collections.emptyMap()); - double[][] featureImportance = tree.featureImportance(new double[]{0.25, 0.25}); + double[][] featureImportance = tree.featureImportance(new double[] { 0.25, 0.25 }); assertThat(featureImportance[0][0], closeTo(-5.0, eps)); assertThat(featureImportance[1][0], closeTo(-2.5, eps)); - featureImportance = tree.featureImportance(new double[]{0.25, 0.75}); + featureImportance = tree.featureImportance(new double[] { 0.25, 0.75 }); assertThat(featureImportance[0][0], closeTo(-5.0, eps)); assertThat(featureImportance[1][0], closeTo(2.5, eps)); - featureImportance = tree.featureImportance(new double[]{0.75, 0.25}); + featureImportance = tree.featureImportance(new double[] { 0.75, 0.25 }); assertThat(featureImportance[0][0], closeTo(5.0, eps)); assertThat(featureImportance[1][0], closeTo(-2.5, eps)); - featureImportance = tree.featureImportance(new double[]{0.75, 0.75}); + featureImportance = tree.featureImportance(new double[] { 0.75, 0.75 }); assertThat(featureImportance[0][0], closeTo(5.0, eps)); assertThat(featureImportance[1][0], closeTo(2.5, eps)); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/langident/LangIdentNeuralNetworkTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/langident/LangIdentNeuralNetworkTests.java index 7525132c066bf..b857fb994bad6 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/langident/LangIdentNeuralNetworkTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/langident/LangIdentNeuralNetworkTests.java @@ -8,10 +8,10 @@ import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.SearchModule; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.inference.MlInferenceNamedXContentProvider; import org.junit.Before; @@ -20,7 +20,6 @@ import java.util.Collections; import java.util.List; - public class LangIdentNeuralNetworkTests extends AbstractSerializingTestCase { private boolean lenient; @@ -46,9 +45,7 @@ protected LangIdentNeuralNetwork createTestInstance() { } public static LangIdentNeuralNetwork createRandom() { - return new LangIdentNeuralNetwork(randomAlphaOfLength(10), - LangNetLayerTests.createRandom(), - LangNetLayerTests.createRandom()); + return new LangIdentNeuralNetwork(randomAlphaOfLength(10), LangNetLayerTests.createRandom(), LangNetLayerTests.createRandom()); } @Override diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/langident/LangNetLayerTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/langident/LangNetLayerTests.java index d21de4858e5f7..df0a4316ec8f6 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/langident/LangNetLayerTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/langident/LangNetLayerTests.java @@ -7,15 +7,14 @@ package org.elasticsearch.xpack.core.ml.inference.trainedmodel.langident; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.junit.Before; import java.io.IOException; import java.util.stream.Stream; - public class LangNetLayerTests extends AbstractSerializingTestCase { private boolean lenient; @@ -46,7 +45,8 @@ public static LangNetLayer createRandom() { Stream.generate(ESTestCase::randomDouble).limit(numWeights).mapToDouble(Double::doubleValue).toArray(), numWeights, 1, - Stream.generate(ESTestCase::randomDouble).limit(numWeights).mapToDouble(Double::doubleValue).toArray()); + Stream.generate(ESTestCase::randomDouble).limit(numWeights).mapToDouble(Double::doubleValue).toArray() + ); } @Override diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/langident/LanguageExamples.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/langident/LanguageExamples.java index b332599f27c5c..7cd96a72b0d64 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/langident/LanguageExamples.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/langident/LanguageExamples.java @@ -9,10 +9,10 @@ package org.elasticsearch.xpack.core.ml.inference.trainedmodel.langident; import org.elasticsearch.ElasticsearchException; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -30,19 +30,22 @@ public final class LanguageExamples { public LanguageExamples() {} - public List getLanguageExamples() throws IOException { + public List getLanguageExamples() throws IOException { String path = "/org/elasticsearch/xpack/core/ml/inference/language_examples.json"; URL resource = getClass().getResource(path); if (resource == null) { throw new ElasticsearchException("Could not find resource stored at [" + path + "]"); } - try(XContentParser parser = - XContentType.JSON.xContent().createParser( + try ( + XContentParser parser = XContentType.JSON.xContent() + .createParser( NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, - getClass().getResourceAsStream(path))) { + getClass().getResourceAsStream(path) + ) + ) { List entries = new ArrayList<>(); - while(parser.nextToken() != XContentParser.Token.END_ARRAY) { + while (parser.nextToken() != XContentParser.Token.END_ARRAY) { entries.add(LanguageExampleEntry.PARSER.apply(parser, null)); } return entries; @@ -58,7 +61,8 @@ public static class LanguageExampleEntry implements ToXContentObject { public static ObjectParser PARSER = new ObjectParser<>( "language_example_entry", true, - LanguageExampleEntry::new); + LanguageExampleEntry::new + ); static { PARSER.declareString(LanguageExampleEntry::setLanguage, LANGUAGE); @@ -75,8 +79,7 @@ public static class LanguageExampleEntry implements ToXContentObject { // The raw text on which the prediction is based String text; - private LanguageExampleEntry() { - } + private LanguageExampleEntry() {} private void setLanguage(String language) { this.language = language; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/metadata/FeatureImportanceBaselineTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/metadata/FeatureImportanceBaselineTests.java index a6c56c5c01ea6..0f2ff7954c101 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/metadata/FeatureImportanceBaselineTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/metadata/FeatureImportanceBaselineTests.java @@ -8,8 +8,8 @@ import org.elasticsearch.Version; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.AbstractBWCSerializationTestCase; import org.junit.Before; @@ -18,7 +18,6 @@ import java.util.stream.Collectors; import java.util.stream.Stream; - public class FeatureImportanceBaselineTests extends AbstractBWCSerializationTestCase { private boolean lenient; @@ -32,12 +31,12 @@ public static FeatureImportanceBaseline randomInstance() { ); return new FeatureImportanceBaseline( randomBoolean() ? null : randomDouble(), - randomBoolean() ? - null : - Stream.generate(() -> new FeatureImportanceBaseline.ClassBaseline(classNameGenerator.get(), randomDouble())) + randomBoolean() + ? null + : Stream.generate(() -> new FeatureImportanceBaseline.ClassBaseline(classNameGenerator.get(), randomDouble())) .limit(randomIntBetween(1, 10)) .collect(Collectors.toList()) - ); + ); } @Before diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/metadata/HyperparametersTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/metadata/HyperparametersTests.java index 43edc4442b5e7..72eaa83ee4c59 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/metadata/HyperparametersTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/metadata/HyperparametersTests.java @@ -14,7 +14,6 @@ import java.io.IOException; - public class HyperparametersTests extends AbstractBWCSerializationTestCase { private boolean lenient; @@ -28,7 +27,8 @@ public static Hyperparameters randomInstance() { // If supplied, the importance values are possibly nullable supplied && randomBoolean() ? null : randomDoubleBetween(0.0, 100.0, true), supplied && randomBoolean() ? null : randomDoubleBetween(0.0, 1.0, true), - supplied); + supplied + ); } @Before diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/metadata/TotalFeatureImportanceTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/metadata/TotalFeatureImportanceTests.java index a5ef7344a43eb..b14623627189d 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/metadata/TotalFeatureImportanceTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/metadata/TotalFeatureImportanceTests.java @@ -8,8 +8,8 @@ import org.elasticsearch.Version; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.AbstractBWCSerializationTestCase; import org.junit.Before; @@ -18,7 +18,6 @@ import java.util.stream.Collectors; import java.util.stream.Stream; - public class TotalFeatureImportanceTests extends AbstractBWCSerializationTestCase { private boolean lenient; @@ -33,12 +32,12 @@ public static TotalFeatureImportance randomInstance() { return new TotalFeatureImportance( randomAlphaOfLength(10), randomBoolean() ? null : randomImportance(), - randomBoolean() ? - null : - Stream.generate(() -> new TotalFeatureImportance.ClassImportance(classNameGenerator.get(), randomImportance())) + randomBoolean() + ? null + : Stream.generate(() -> new TotalFeatureImportance.ClassImportance(classNameGenerator.get(), randomImportance())) .limit(randomIntBetween(1, 10)) .collect(Collectors.toList()) - ); + ); } private static TotalFeatureImportance.Importance randomImportance() { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/metadata/TrainedModelMetadataTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/metadata/TrainedModelMetadataTests.java index 95057d9d12120..98199c57698b6 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/metadata/TrainedModelMetadataTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/metadata/TrainedModelMetadataTests.java @@ -16,7 +16,6 @@ import java.util.stream.Collectors; import java.util.stream.Stream; - public class TrainedModelMetadataTests extends AbstractBWCSerializationTestCase { private boolean lenient; @@ -26,8 +25,10 @@ public static TrainedModelMetadata randomInstance() { randomAlphaOfLength(10), Stream.generate(TotalFeatureImportanceTests::randomInstance).limit(randomIntBetween(1, 10)).collect(Collectors.toList()), randomBoolean() ? null : FeatureImportanceBaselineTests.randomInstance(), - randomBoolean() ? null : Stream.generate(HyperparametersTests::randomInstance).limit(randomIntBetween(1, 10)) - .collect(Collectors.toList())); + randomBoolean() + ? null + : Stream.generate(HyperparametersTests::randomInstance).limit(randomIntBetween(1, 10)).collect(Collectors.toList()) + ); } @Before diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/tree/TreeNodeTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/tree/TreeNodeTests.java index 525c444112264..ddf40ca4e24b2 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/tree/TreeNodeTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/tree/TreeNodeTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ml.inference.trainedmodel.tree; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.job.config.Operator; import org.junit.Before; @@ -40,12 +40,8 @@ protected TreeNode createTestInstance() { Integer rgt = randomBoolean() ? randomInt(100) : null; Double threshold = lft != null || randomBoolean() ? randomDouble() : null; Integer featureIndex = lft != null || randomBoolean() ? randomInt(100) : null; - return createRandom(randomInt(100), - lft, - rgt, - threshold, - featureIndex, - randomBoolean() ? null : randomFrom(Operator.values())).build(); + return createRandom(randomInt(100), lft, rgt, threshold, featureIndex, randomBoolean() ? null : randomFrom(Operator.values())) + .build(); } public static TreeNode createRandomLeafNode(double internalValue) { @@ -56,12 +52,14 @@ public static TreeNode createRandomLeafNode(double internalValue) { .build(); } - public static TreeNode.Builder createRandom(int nodeId, - Integer left, - Integer right, - Double threshold, - Integer featureIndex, - Operator operator) { + public static TreeNode.Builder createRandom( + int nodeId, + Integer left, + Integer right, + Double threshold, + Integer featureIndex, + Operator operator + ) { return TreeNode.builder(nodeId) .setLeafValue(left == null ? Collections.singletonList(randomDouble()) : null) .setDefaultLeft(randomBoolean() ? null : randomBoolean()) diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/tree/TreeTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/tree/TreeTests.java index e63cb28b70f2e..f710d2b84e675 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/tree/TreeTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/tree/TreeTests.java @@ -9,8 +9,8 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.TargetType; import org.junit.Before; @@ -24,7 +24,6 @@ import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; - public class TreeTests extends AbstractSerializingTestCase { private boolean lenient; @@ -60,7 +59,7 @@ public static Tree createRandom(TargetType targetType) { for (int i = 0; i < numberOfFeatures; i++) { featureNames.add(randomAlphaOfLength(10)); } - return buildRandomTree(targetType, featureNames, 6); + return buildRandomTree(targetType, featureNames, 6); } public static Tree createRandom() { @@ -75,15 +74,14 @@ public static Tree buildRandomTree(TargetType targetType, List featureNa TreeNode.Builder node = builder.addJunction(0, randomInt(maxFeatureIndex), true, randomDouble()); List childNodes = List.of(node.getLeftChild(), node.getRightChild()); - for (int i = 0; i < depth -1; i++) { + for (int i = 0; i < depth - 1; i++) { List nextNodes = new ArrayList<>(); for (int nodeId : childNodes) { - if (i == depth -2) { + if (i == depth - 2) { builder.addLeaf(nodeId, randomDouble()); } else { - TreeNode.Builder childNode = - builder.addJunction(nodeId, randomInt(maxFeatureIndex), true, randomDouble()); + TreeNode.Builder childNode = builder.addJunction(nodeId, randomInt(maxFeatureIndex), true, randomDouble()); nextNodes.add(childNode.getLeftChild()); nextNodes.add(childNode.getRightChild()); } @@ -108,54 +106,49 @@ protected Writeable.Reader instanceReader() { } public void testTreeWithNullRoot() { - ElasticsearchStatusException ex = expectThrows(ElasticsearchStatusException.class, - () -> Tree.builder() - .setNodes(Collections.singletonList(null)) - .setFeatureNames(Arrays.asList("foo", "bar")) - .build()); + ElasticsearchStatusException ex = expectThrows( + ElasticsearchStatusException.class, + () -> Tree.builder().setNodes(Collections.singletonList(null)).setFeatureNames(Arrays.asList("foo", "bar")).build() + ); assertThat(ex.getMessage(), equalTo("[tree] cannot contain null nodes")); } public void testTreeWithInvalidNode() { - ElasticsearchStatusException ex = expectThrows(ElasticsearchStatusException.class, + ElasticsearchStatusException ex = expectThrows( + ElasticsearchStatusException.class, () -> Tree.builder() - .setNodes(TreeNode.builder(0) - .setLeftChild(1) - .setSplitFeature(1) - .setThreshold(randomDouble())) + .setNodes(TreeNode.builder(0).setLeftChild(1).setSplitFeature(1).setThreshold(randomDouble())) .setFeatureNames(Arrays.asList("foo", "bar")) - .build().validate()); + .build() + .validate() + ); assertThat(ex.getMessage(), equalTo("[tree] contains missing nodes [1]")); } public void testTreeWithNullNode() { - ElasticsearchStatusException ex = expectThrows(ElasticsearchStatusException.class, + ElasticsearchStatusException ex = expectThrows( + ElasticsearchStatusException.class, () -> Tree.builder() - .setNodes(TreeNode.builder(0) - .setLeftChild(1) - .setSplitFeature(1) - .setThreshold(randomDouble()), - null) + .setNodes(TreeNode.builder(0).setLeftChild(1).setSplitFeature(1).setThreshold(randomDouble()), null) .setFeatureNames(Arrays.asList("foo", "bar")) .build() - .validate()); + .validate() + ); assertThat(ex.getMessage(), equalTo("[tree] cannot contain null nodes")); } public void testTreeWithCycle() { - ElasticsearchStatusException ex = expectThrows(ElasticsearchStatusException.class, + ElasticsearchStatusException ex = expectThrows( + ElasticsearchStatusException.class, () -> Tree.builder() - .setNodes(TreeNode.builder(0) - .setLeftChild(1) - .setSplitFeature(1) - .setThreshold(randomDouble()), - TreeNode.builder(0) - .setLeftChild(0) - .setSplitFeature(1) - .setThreshold(randomDouble())) + .setNodes( + TreeNode.builder(0).setLeftChild(1).setSplitFeature(1).setThreshold(randomDouble()), + TreeNode.builder(0).setLeftChild(0).setSplitFeature(1).setThreshold(randomDouble()) + ) .setFeatureNames(Arrays.asList("foo", "bar")) .build() - .validate()); + .validate() + ); assertThat(ex.getMessage(), equalTo("[tree] contains cycle at node 0")); } @@ -164,10 +157,7 @@ public void testTreeWithTargetTypeAndLabelsMismatch() { String msg = "[target_type] should be [classification] if [classification_labels] are provided"; ElasticsearchException ex = expectThrows(ElasticsearchException.class, () -> { Tree.builder() - .setRoot(TreeNode.builder(0) - .setLeftChild(1) - .setSplitFeature(1) - .setThreshold(randomDouble())) + .setRoot(TreeNode.builder(0).setLeftChild(1).setSplitFeature(1).setThreshold(randomDouble())) .setFeatureNames(featureNames) .setClassificationLabels(Arrays.asList("label1", "label2")) .build() @@ -178,26 +168,17 @@ public void testTreeWithTargetTypeAndLabelsMismatch() { public void testTreeWithEmptyFeaturesAndOneNode() { // Shouldn't throw - Tree.builder() - .setRoot(TreeNode.builder(0).setLeafValue(10.0)) - .setFeatureNames(Collections.emptyList()) - .build() - .validate(); + Tree.builder().setRoot(TreeNode.builder(0).setLeafValue(10.0)).setFeatureNames(Collections.emptyList()).build().validate(); } public void testTreeWithEmptyFeaturesAndThreeNodes() { - String msg = "[feature_names] is empty and the tree has > 1 nodes; num nodes [3]. " + - "The model Must have features if tree is not a stump"; + String msg = "[feature_names] is empty and the tree has > 1 nodes; num nodes [3]. " + + "The model Must have features if tree is not a stump"; ElasticsearchException ex = expectThrows(ElasticsearchException.class, () -> { Tree.builder() - .setRoot(TreeNode.builder(0) - .setLeftChild(1) - .setRightChild(2) - .setThreshold(randomDouble())) - .addNode(TreeNode.builder(1) - .setLeafValue(randomDouble())) - .addNode(TreeNode.builder(2) - .setLeafValue(randomDouble())) + .setRoot(TreeNode.builder(0).setLeftChild(1).setRightChild(2).setThreshold(randomDouble())) + .addNode(TreeNode.builder(1).setLeafValue(randomDouble())) + .addNode(TreeNode.builder(2).setLeafValue(randomDouble())) .setFeatureNames(Collections.emptyList()) .build() .validate(); @@ -215,9 +196,9 @@ public void testMaxFeatureIndex() { int numFeatures = randomIntBetween(1, 15); // We need a tree where every feature is used, choose a depth big enough to // accommodate those non-leave nodes (leaf nodes don't have a feature index) - int depth = (int) Math.ceil(Math.log(numFeatures +1) / Math.log(2)) + 1; + int depth = (int) Math.ceil(Math.log(numFeatures + 1) / Math.log(2)) + 1; List featureNames = new ArrayList<>(numFeatures); - for (int i=0; i childNodes = List.of(node.getLeftChild(), node.getRightChild()); - for (int i = 0; i < depth -1; i++) { + for (int i = 0; i < depth - 1; i++) { List nextNodes = new ArrayList<>(); for (int nodeId : childNodes) { - if (i == depth -2) { + if (i == depth - 2) { builder.addLeaf(nodeId, randomDouble()); } else { - TreeNode.Builder childNode = - builder.addJunction(nodeId, featureIndex++ % numFeatures, true, randomDouble()); + TreeNode.Builder childNode = builder.addJunction(nodeId, featureIndex++ % numFeatures, true, randomDouble()); nextNodes.add(childNode.getLeftChild()); nextNodes.add(childNode.getRightChild()); } @@ -245,14 +225,11 @@ public void testMaxFeatureIndex() { Tree tree = builder.build(); - assertEquals(numFeatures, tree.maxFeatureIndex() +1); + assertEquals(numFeatures, tree.maxFeatureIndex() + 1); } public void testMaxFeatureIndexSingleNodeTree() { - Tree tree = Tree.builder() - .setRoot(TreeNode.builder(0).setLeafValue(10.0)) - .setFeatureNames(Collections.emptyList()) - .build(); + Tree tree = Tree.builder().setRoot(TreeNode.builder(0).setLeafValue(10.0)).setFeatureNames(Collections.emptyList()).build(); assertEquals(-1, tree.maxFeatureIndex()); } @@ -275,12 +252,7 @@ public void testValidateGivenMissingFeatures() { } public void testValidateGivenTreeWithNoFeatures() { - Tree.builder() - .setRoot(TreeNode.builder(0).setLeafValue(10.0)) - .setFeatureNames(Collections.emptyList()) - .build() - .validate(); + Tree.builder().setRoot(TreeNode.builder(0).setLeafValue(10.0)).setFeatureNames(Collections.emptyList()).build().validate(); } - } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/utils/StatisticsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/utils/StatisticsTests.java index a12b3ddea7727..a3d71a9ec0deb 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/utils/StatisticsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/utils/StatisticsTests.java @@ -17,18 +17,18 @@ public class StatisticsTests extends ESTestCase { public void testSoftMax() { - double[] values = new double[] {Double.NEGATIVE_INFINITY, 1.0, -0.5, Double.NaN, Double.NaN, Double.POSITIVE_INFINITY, 1.0, 5.0}; + double[] values = new double[] { Double.NEGATIVE_INFINITY, 1.0, -0.5, Double.NaN, Double.NaN, Double.POSITIVE_INFINITY, 1.0, 5.0 }; double[] softMax = Statistics.softMax(values); - double[] expected = new double[] {0.0, 0.017599040, 0.003926876, 0.0, 0.0, 0.0, 0.017599040, 0.960875042}; + double[] expected = new double[] { 0.0, 0.017599040, 0.003926876, 0.0, 0.0, 0.0, 0.017599040, 0.960875042 }; - for(int i = 0; i < expected.length; i++) { + for (int i = 0; i < expected.length; i++) { assertThat(softMax[i], closeTo(expected[i], 0.000001)); } } public void testSoftMaxWithNoValidValues() { - double[] values = new double[] {Double.NEGATIVE_INFINITY, Double.NaN, Double.POSITIVE_INFINITY}; + double[] values = new double[] { Double.NEGATIVE_INFINITY, Double.NaN, Double.POSITIVE_INFINITY }; expectThrows(IllegalArgumentException.class, () -> Statistics.softMax(values)); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/AnalysisConfigTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/AnalysisConfigTests.java index 4211ea7defcdd..f72bfef27d516 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/AnalysisConfigTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/AnalysisConfigTests.java @@ -8,11 +8,11 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.CollectionUtils; -import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.job.messages.Messages; import org.elasticsearch.xpack.core.ml.process.writer.RecordWriter; @@ -95,8 +95,7 @@ public static AnalysisConfig.Builder createRandomized() { } if (randomBoolean()) { boolean enabled = randomBoolean(); - builder.setPerPartitionCategorizationConfig( - new PerPartitionCategorizationConfig(enabled, enabled && randomBoolean())); + builder.setPerPartitionCategorizationConfig(new PerPartitionCategorizationConfig(enabled, enabled && randomBoolean())); } } if (randomBoolean()) { @@ -203,12 +202,12 @@ public void testFieldConfiguration_multipleDetectors_NotPreSummarised() { builder.setInfluencers(Collections.singletonList("Influencer_Field")); AnalysisConfig ac = builder.build(); - Set termFields = new TreeSet<>(Arrays.asList( - "by_one", "by_two", "over_field", - "partition_one", "partition_two", "Influencer_Field")); - Set analysisFields = new TreeSet<>(Arrays.asList( - "metric1", "metric2", "by_one", "by_two", "over_field", - "partition_one", "partition_two", "Influencer_Field")); + Set termFields = new TreeSet<>( + Arrays.asList("by_one", "by_two", "over_field", "partition_one", "partition_two", "Influencer_Field") + ); + Set analysisFields = new TreeSet<>( + Arrays.asList("metric1", "metric2", "by_one", "by_two", "over_field", "partition_one", "partition_two", "Influencer_Field") + ); assertEquals(termFields.size(), ac.termFields().size()); assertEquals(analysisFields.size(), ac.analysisFields().size()); @@ -294,8 +293,10 @@ public void testBuild_GivenCategorizationFieldNameButNoUseOfMlCategory() { ac.setCategorizationFieldName("msg"); ElasticsearchException e = expectThrows(ElasticsearchException.class, ac::build); - assertThat(e.getMessage(), equalTo("categorization_field_name is set but mlcategory is " + - "not used in any detector by/over/partition field")); + assertThat( + e.getMessage(), + equalTo("categorization_field_name is set but mlcategory is " + "not used in any detector by/over/partition field") + ); } public void testBuild_GivenMlCategoryUsedAsByFieldAndCategorizationFieldName() { @@ -485,7 +486,8 @@ public void testExtractReferencedFilters() { detector2.setRules(Collections.singletonList(rule2)); detector2.setByFieldName("foo"); AnalysisConfig config = new AnalysisConfig.Builder( - Arrays.asList(detector1.build(), detector2.build(), new Detector.Builder("count", null).build())).build(); + Arrays.asList(detector1.build(), detector2.build(), new Detector.Builder("count", null).build()) + ).build(); assertEquals(new HashSet<>(Arrays.asList("filter1", "filter2")), config.extractReferencedFilters()); } @@ -493,12 +495,12 @@ public void testExtractReferencedFilters() { private static AnalysisConfig createFullyPopulatedNonRandomConfig() { Detector.Builder detector = new Detector.Builder("min", "count"); detector.setOverFieldName("mlcategory"); - AnalysisConfig.Builder builder = new AnalysisConfig.Builder( - Collections.singletonList(detector.build())); + AnalysisConfig.Builder builder = new AnalysisConfig.Builder(Collections.singletonList(detector.build())); builder.setBucketSpan(TimeValue.timeValueHours(1)); builder.setCategorizationFieldName("cat"); builder.setCategorizationAnalyzerConfig( - CategorizationAnalyzerConfig.buildDefaultCategorizationAnalyzer(Collections.singletonList("foo"))); + CategorizationAnalyzerConfig.buildDefaultCategorizationAnalyzer(Collections.singletonList("foo")) + ); builder.setInfluencers(Collections.singletonList("myInfluencer")); builder.setLatency(TimeValue.timeValueSeconds(3600)); builder.setModelPruneWindow(TimeValue.timeValueDays(30)); @@ -556,7 +558,8 @@ public void testVerify_GivenValidConfigWithCategorizationFieldNameAndCategorizat public void testVerify_GivenValidConfigWithCategorizationFieldNameAndCategorizationAnalyzerConfig() { AnalysisConfig.Builder analysisConfig = createValidCategorizationConfig(); analysisConfig.setCategorizationAnalyzerConfig( - CategorizationAnalyzerConfig.buildDefaultCategorizationAnalyzer(Arrays.asList("foo", "bar"))); + CategorizationAnalyzerConfig.buildDefaultCategorizationAnalyzer(Arrays.asList("foo", "bar")) + ); analysisConfig.build(); } @@ -565,12 +568,15 @@ public void testVerify_GivenBothCategorizationFiltersAndCategorizationAnalyzerCo AnalysisConfig.Builder analysisConfig = createValidCategorizationConfig(); analysisConfig.setCategorizationFilters(Arrays.asList("foo", "bar")); analysisConfig.setCategorizationAnalyzerConfig( - CategorizationAnalyzerConfig.buildDefaultCategorizationAnalyzer(Collections.singletonList("baz"))); + CategorizationAnalyzerConfig.buildDefaultCategorizationAnalyzer(Collections.singletonList("baz")) + ); ElasticsearchException e = ESTestCase.expectThrows(ElasticsearchException.class, analysisConfig::build); - assertEquals(Messages.getMessage(Messages.JOB_CONFIG_CATEGORIZATION_FILTERS_INCOMPATIBLE_WITH_CATEGORIZATION_ANALYZER), - e.getMessage()); + assertEquals( + Messages.getMessage(Messages.JOB_CONFIG_CATEGORIZATION_FILTERS_INCOMPATIBLE_WITH_CATEGORIZATION_ANALYZER), + e.getMessage() + ); } public void testVerify_GivenFieldIsControlField() { @@ -583,8 +589,10 @@ public void testVerify_GivenFieldIsControlField() { ElasticsearchException e = ESTestCase.expectThrows(ElasticsearchException.class, analysisConfig::build); - assertEquals(Messages.getMessage(Messages.JOB_CONFIG_INVALID_FIELDNAME, RecordWriter.CONTROL_FIELD_NAME, - RecordWriter.CONTROL_FIELD_NAME), e.getMessage()); + assertEquals( + Messages.getMessage(Messages.JOB_CONFIG_INVALID_FIELDNAME, RecordWriter.CONTROL_FIELD_NAME, RecordWriter.CONTROL_FIELD_NAME), + e.getMessage() + ); } public void testVerify_GivenMetricAndSummaryCountField() { @@ -648,7 +656,8 @@ public void testVerify_GivenPerPartitionCategorizationAndNoPartitions() { assertEquals( "partition_field_name must be set for detectors that reference mlcategory when per-partition categorization is enabled", - e.getMessage()); + e.getMessage() + ); } public void testVerify_GivenPerPartitionCategorizationAndMultiplePartitionFields() { @@ -668,7 +677,8 @@ public void testVerify_GivenPerPartitionCategorizationAndMultiplePartitionFields assertEquals( "partition_field_name cannot vary between detectors when per-partition categorization is enabled: [part1] and [part2] are used", - e.getMessage()); + e.getMessage() + ); } public void testVerify_GivenPerPartitionCategorizationAndNoPartitionFieldOnCategorizationDetector() { @@ -688,7 +698,8 @@ public void testVerify_GivenPerPartitionCategorizationAndNoPartitionFieldOnCateg assertEquals( "partition_field_name must be set for detectors that reference mlcategory when per-partition categorization is enabled", - e.getMessage()); + e.getMessage() + ); } public void testVerify_GivenComplexPerPartitionCategorizationConfig() { @@ -739,94 +750,97 @@ private static AnalysisConfig.Builder createValidCategorizationConfig() { protected AnalysisConfig mutateInstance(AnalysisConfig instance) { AnalysisConfig.Builder builder = new AnalysisConfig.Builder(instance); switch (between(0, 8)) { - case 0: - List detectors = new ArrayList<>(instance.getDetectors()); - Detector.Builder detector = new Detector.Builder(); - detector.setFunction("mean"); - detector.setFieldName(randomAlphaOfLengthBetween(10, 20)); - detectors.add(detector.build()); - builder.setDetectors(detectors); - break; - case 1: - TimeValue bucketSpan = new TimeValue(instance.getBucketSpan().millis() + (between(1, 1000) * 1000)); - builder.setBucketSpan(bucketSpan); - - // There is a dependency between model_prune_window and bucket_span: model_prune window must be - // at least twice the size of bucket_span. - builder.setModelPruneWindow(new TimeValue(between(2, 1000) * bucketSpan.millis())); - break; - case 2: - if (instance.getLatency() == null) { - builder.setLatency(new TimeValue(between(1, 1000) * 1000)); - } else { - builder.setLatency(new TimeValue(instance.getLatency().millis() + (between(1, 1000) * 1000))); - } - break; - case 3: - if (instance.getCategorizationFieldName() == null) { - String categorizationFieldName = instance.getCategorizationFieldName() + randomAlphaOfLengthBetween(1, 10); - builder.setCategorizationFieldName(categorizationFieldName); - List newDetectors = new ArrayList<>(instance.getDetectors()); - Detector.Builder catDetector = new Detector.Builder(); - catDetector.setFunction("mean"); - catDetector.setFieldName(randomAlphaOfLengthBetween(10, 20)); - catDetector.setPartitionFieldName("mlcategory"); - newDetectors.add(catDetector.build()); - builder.setDetectors(newDetectors); - } else { - builder.setCategorizationFieldName(instance.getCategorizationFieldName() + randomAlphaOfLengthBetween(1, 10)); - } - break; - case 4: - List filters; - if (instance.getCategorizationFilters() == null) { - filters = new ArrayList<>(); - } else { - filters = new ArrayList<>(instance.getCategorizationFilters()); - } - filters.add(randomAlphaOfLengthBetween(1, 20)); - builder.setCategorizationFilters(filters); - builder.setCategorizationAnalyzerConfig(null); - if (instance.getCategorizationFieldName() == null) { - builder.setCategorizationFieldName(randomAlphaOfLengthBetween(1, 10)); - List newDetectors = new ArrayList<>(instance.getDetectors()); - Detector.Builder catDetector = new Detector.Builder(); - catDetector.setFunction("mean"); - catDetector.setFieldName(randomAlphaOfLengthBetween(10, 20)); - catDetector.setPartitionFieldName("mlcategory"); - newDetectors.add(catDetector.build()); - builder.setDetectors(newDetectors); - } - break; - case 5: - builder.setCategorizationFilters(null); - builder.setCategorizationAnalyzerConfig(CategorizationAnalyzerConfig.buildDefaultCategorizationAnalyzer( - Collections.singletonList(randomAlphaOfLengthBetween(1, 20)))); - if (instance.getCategorizationFieldName() == null) { - builder.setCategorizationFieldName(randomAlphaOfLengthBetween(1, 10)); - List newDetectors = new ArrayList<>(instance.getDetectors()); - Detector.Builder catDetector = new Detector.Builder(); - catDetector.setFunction("count"); - catDetector.setByFieldName("mlcategory"); - newDetectors.add(catDetector.build()); - builder.setDetectors(newDetectors); - } - break; - case 6: - builder.setSummaryCountFieldName(instance.getSummaryCountFieldName() + randomAlphaOfLengthBetween(1, 5)); - break; - case 7: - builder.setInfluencers(CollectionUtils.appendToCopy(instance.getInfluencers(), randomAlphaOfLengthBetween(5, 10))); - break; - case 8: - if (instance.getMultivariateByFields() == null) { - builder.setMultivariateByFields(randomBoolean()); - } else { - builder.setMultivariateByFields(instance.getMultivariateByFields() == false); - } - break; - default: - throw new AssertionError("Illegal randomisation branch"); + case 0: + List detectors = new ArrayList<>(instance.getDetectors()); + Detector.Builder detector = new Detector.Builder(); + detector.setFunction("mean"); + detector.setFieldName(randomAlphaOfLengthBetween(10, 20)); + detectors.add(detector.build()); + builder.setDetectors(detectors); + break; + case 1: + TimeValue bucketSpan = new TimeValue(instance.getBucketSpan().millis() + (between(1, 1000) * 1000)); + builder.setBucketSpan(bucketSpan); + + // There is a dependency between model_prune_window and bucket_span: model_prune window must be + // at least twice the size of bucket_span. + builder.setModelPruneWindow(new TimeValue(between(2, 1000) * bucketSpan.millis())); + break; + case 2: + if (instance.getLatency() == null) { + builder.setLatency(new TimeValue(between(1, 1000) * 1000)); + } else { + builder.setLatency(new TimeValue(instance.getLatency().millis() + (between(1, 1000) * 1000))); + } + break; + case 3: + if (instance.getCategorizationFieldName() == null) { + String categorizationFieldName = instance.getCategorizationFieldName() + randomAlphaOfLengthBetween(1, 10); + builder.setCategorizationFieldName(categorizationFieldName); + List newDetectors = new ArrayList<>(instance.getDetectors()); + Detector.Builder catDetector = new Detector.Builder(); + catDetector.setFunction("mean"); + catDetector.setFieldName(randomAlphaOfLengthBetween(10, 20)); + catDetector.setPartitionFieldName("mlcategory"); + newDetectors.add(catDetector.build()); + builder.setDetectors(newDetectors); + } else { + builder.setCategorizationFieldName(instance.getCategorizationFieldName() + randomAlphaOfLengthBetween(1, 10)); + } + break; + case 4: + List filters; + if (instance.getCategorizationFilters() == null) { + filters = new ArrayList<>(); + } else { + filters = new ArrayList<>(instance.getCategorizationFilters()); + } + filters.add(randomAlphaOfLengthBetween(1, 20)); + builder.setCategorizationFilters(filters); + builder.setCategorizationAnalyzerConfig(null); + if (instance.getCategorizationFieldName() == null) { + builder.setCategorizationFieldName(randomAlphaOfLengthBetween(1, 10)); + List newDetectors = new ArrayList<>(instance.getDetectors()); + Detector.Builder catDetector = new Detector.Builder(); + catDetector.setFunction("mean"); + catDetector.setFieldName(randomAlphaOfLengthBetween(10, 20)); + catDetector.setPartitionFieldName("mlcategory"); + newDetectors.add(catDetector.build()); + builder.setDetectors(newDetectors); + } + break; + case 5: + builder.setCategorizationFilters(null); + builder.setCategorizationAnalyzerConfig( + CategorizationAnalyzerConfig.buildDefaultCategorizationAnalyzer( + Collections.singletonList(randomAlphaOfLengthBetween(1, 20)) + ) + ); + if (instance.getCategorizationFieldName() == null) { + builder.setCategorizationFieldName(randomAlphaOfLengthBetween(1, 10)); + List newDetectors = new ArrayList<>(instance.getDetectors()); + Detector.Builder catDetector = new Detector.Builder(); + catDetector.setFunction("count"); + catDetector.setByFieldName("mlcategory"); + newDetectors.add(catDetector.build()); + builder.setDetectors(newDetectors); + } + break; + case 6: + builder.setSummaryCountFieldName(instance.getSummaryCountFieldName() + randomAlphaOfLengthBetween(1, 5)); + break; + case 7: + builder.setInfluencers(CollectionUtils.appendToCopy(instance.getInfluencers(), randomAlphaOfLengthBetween(5, 10))); + break; + case 8: + if (instance.getMultivariateByFields() == null) { + builder.setMultivariateByFields(randomBoolean()); + } else { + builder.setMultivariateByFields(instance.getMultivariateByFields() == false); + } + break; + default: + throw new AssertionError("Illegal randomisation branch"); } return builder.build(); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/AnalysisLimitsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/AnalysisLimitsTests.java index 24d182ca8f767..313bf4cc8c7f8 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/AnalysisLimitsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/AnalysisLimitsTests.java @@ -9,13 +9,13 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParseException; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xpack.core.ml.job.messages.Messages; import java.io.IOException; @@ -33,8 +33,10 @@ protected AnalysisLimits createTestInstance() { } public static AnalysisLimits createRandomized() { - return new AnalysisLimits(randomBoolean() ? (long) randomIntBetween(1, 1000000) : null, - randomBoolean() ? randomNonNegativeLong() : null); + return new AnalysisLimits( + randomBoolean() ? (long) randomIntBetween(1, 1000000) : null, + randomBoolean() ? randomNonNegativeLong() : null + ); } @Override @@ -50,7 +52,7 @@ protected AnalysisLimits doParseInstance(XContentParser parser) { public void testParseModelMemoryLimitGivenNegativeNumber() throws IOException { String json = "{\"model_memory_limit\": -1}"; XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json); + .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json); XContentParseException e = expectThrows(XContentParseException.class, () -> AnalysisLimits.STRICT_PARSER.apply(parser, null)); assertThat(e.getCause(), notNullValue()); assertThat(e.getCause().getMessage(), containsString("model_memory_limit must be at least 1 MiB. Value = -1")); @@ -59,7 +61,7 @@ public void testParseModelMemoryLimitGivenNegativeNumber() throws IOException { public void testParseModelMemoryLimitGivenZero() throws IOException { String json = "{\"model_memory_limit\": 0}"; XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json); + .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json); XContentParseException e = expectThrows(XContentParseException.class, () -> AnalysisLimits.STRICT_PARSER.apply(parser, null)); assertThat(e.getCause(), notNullValue()); assertThat(e.getCause().getMessage(), containsString("model_memory_limit must be at least 1 MiB. Value = 0")); @@ -68,7 +70,7 @@ public void testParseModelMemoryLimitGivenZero() throws IOException { public void testParseModelMemoryLimitGivenPositiveNumber() throws IOException { String json = "{\"model_memory_limit\": 2048}"; XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json); + .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json); AnalysisLimits limits = AnalysisLimits.STRICT_PARSER.apply(parser, null); @@ -78,7 +80,7 @@ public void testParseModelMemoryLimitGivenPositiveNumber() throws IOException { public void testParseModelMemoryLimitGivenNegativeString() throws IOException { String json = "{\"model_memory_limit\":\"-4MB\"}"; XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json); + .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json); XContentParseException e = expectThrows(XContentParseException.class, () -> AnalysisLimits.STRICT_PARSER.apply(parser, null)); // the root cause is wrapped in an intermediate ElasticsearchParseException assertThat(e.getCause(), instanceOf(ElasticsearchParseException.class)); @@ -89,7 +91,7 @@ public void testParseModelMemoryLimitGivenNegativeString() throws IOException { public void testParseModelMemoryLimitGivenZeroString() throws IOException { String json = "{\"model_memory_limit\":\"0MB\"}"; XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json); + .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json); XContentParseException e = expectThrows(XContentParseException.class, () -> AnalysisLimits.STRICT_PARSER.apply(parser, null)); assertThat(e.getCause(), notNullValue()); assertThat(e.getCause().getMessage(), containsString("model_memory_limit must be at least 1 MiB. Value = 0")); @@ -98,7 +100,7 @@ public void testParseModelMemoryLimitGivenZeroString() throws IOException { public void testParseModelMemoryLimitGivenLessThanOneMBString() throws IOException { String json = "{\"model_memory_limit\":\"1000Kb\"}"; XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json); + .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json); XContentParseException e = expectThrows(XContentParseException.class, () -> AnalysisLimits.STRICT_PARSER.apply(parser, null)); assertThat(e.getCause(), notNullValue()); assertThat(e.getCause().getMessage(), containsString("model_memory_limit must be at least 1 MiB. Value = 0")); @@ -107,7 +109,7 @@ public void testParseModelMemoryLimitGivenLessThanOneMBString() throws IOExcepti public void testParseModelMemoryLimitGivenStringMultipleOfMBs() throws IOException { String json = "{\"model_memory_limit\":\"4g\"}"; XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json); + .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json); AnalysisLimits limits = AnalysisLimits.STRICT_PARSER.apply(parser, null); @@ -117,7 +119,7 @@ public void testParseModelMemoryLimitGivenStringMultipleOfMBs() throws IOExcepti public void testParseModelMemoryLimitGivenStringNonMultipleOfMBs() throws IOException { String json = "{\"model_memory_limit\":\"1300kb\"}"; XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json); + .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json); AnalysisLimits limits = AnalysisLimits.STRICT_PARSER.apply(parser, null); @@ -138,7 +140,6 @@ public void testEquals_GivenEqual() { assertTrue(analysisLimits2.equals(analysisLimits1)); } - public void testEquals_GivenDifferentModelMemoryLimit() { AnalysisLimits analysisLimits1 = new AnalysisLimits(10L, 20L); AnalysisLimits analysisLimits2 = new AnalysisLimits(11L, 20L); @@ -147,7 +148,6 @@ public void testEquals_GivenDifferentModelMemoryLimit() { assertFalse(analysisLimits2.equals(analysisLimits1)); } - public void testEquals_GivenDifferentCategorizationExamplesLimit() { AnalysisLimits analysisLimits1 = new AnalysisLimits(10L, 20L); AnalysisLimits analysisLimits2 = new AnalysisLimits(10L, 21L); @@ -156,7 +156,6 @@ public void testEquals_GivenDifferentCategorizationExamplesLimit() { assertFalse(analysisLimits2.equals(analysisLimits1)); } - public void testHashCode_GivenEqual() { AnalysisLimits analysisLimits1 = new AnalysisLimits(5555L, 3L); AnalysisLimits analysisLimits2 = new AnalysisLimits(5555L, 3L); @@ -166,8 +165,12 @@ public void testHashCode_GivenEqual() { public void testVerify_GivenNegativeCategorizationExamplesLimit() { ElasticsearchException e = expectThrows(ElasticsearchException.class, () -> new AnalysisLimits(1L, -1L)); - String errorMessage = Messages.getMessage(Messages.JOB_CONFIG_FIELD_VALUE_TOO_LOW, - AnalysisLimits.CATEGORIZATION_EXAMPLES_LIMIT, 0, -1L); + String errorMessage = Messages.getMessage( + Messages.JOB_CONFIG_FIELD_VALUE_TOO_LOW, + AnalysisLimits.CATEGORIZATION_EXAMPLES_LIMIT, + 0, + -1L + ); assertEquals(errorMessage, e.getMessage()); } @@ -182,30 +185,30 @@ protected AnalysisLimits mutateInstance(AnalysisLimits instance) throws IOExcept Long memoryModelLimit = instance.getModelMemoryLimit(); Long categorizationExamplesLimit = instance.getCategorizationExamplesLimit(); switch (between(0, 1)) { - case 0: - if (memoryModelLimit == null) { - memoryModelLimit = randomNonNegativeLong(); - } else { - if (randomBoolean()) { - memoryModelLimit = null; + case 0: + if (memoryModelLimit == null) { + memoryModelLimit = randomNonNegativeLong(); } else { - memoryModelLimit += between(1, 10000); + if (randomBoolean()) { + memoryModelLimit = null; + } else { + memoryModelLimit += between(1, 10000); + } } - } - break; - case 1: - if (categorizationExamplesLimit == null) { - categorizationExamplesLimit = randomNonNegativeLong(); - } else { - if (randomBoolean()) { - categorizationExamplesLimit = null; + break; + case 1: + if (categorizationExamplesLimit == null) { + categorizationExamplesLimit = randomNonNegativeLong(); } else { - categorizationExamplesLimit += between(1, 10000); + if (randomBoolean()) { + categorizationExamplesLimit = null; + } else { + categorizationExamplesLimit += between(1, 10000); + } } - } - break; - default: - throw new AssertionError("Illegal randomisation branch"); + break; + default: + throw new AssertionError("Illegal randomisation branch"); } return new AnalysisLimits(memoryModelLimit, categorizationExamplesLimit); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/BlockedTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/BlockedTests.java index 8248df5c29746..3234d10f12e80 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/BlockedTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/BlockedTests.java @@ -8,9 +8,9 @@ package org.elasticsearch.xpack.core.ml.job.config; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.tasks.TaskId; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.Arrays; @@ -39,8 +39,9 @@ protected Blocked createTestInstance() { public static Blocked createRandom() { Blocked.Reason reason = randomFrom(Blocked.Reason.values()); - TaskId taskId = (reason != Blocked.Reason.NONE && randomBoolean()) ? - new TaskId(randomAlphaOfLength(10) + ":" + randomNonNegativeLong()) : null; + TaskId taskId = (reason != Blocked.Reason.NONE && randomBoolean()) + ? new TaskId(randomAlphaOfLength(10) + ":" + randomNonNegativeLong()) + : null; return new Blocked(reason, taskId); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/DataDescriptionTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/DataDescriptionTests.java index 5e2067cebba1a..a449b724c95e7 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/DataDescriptionTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/DataDescriptionTests.java @@ -9,12 +9,12 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.io.stream.Writeable.Reader; +import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParseException; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xpack.core.ml.job.messages.Messages; import java.time.DateTimeException; @@ -78,16 +78,20 @@ public void testIsTransformTime_GivenTimeFormatPattern() { public void testInvalidDataFormat() throws Exception { BytesArray json = new BytesArray("{ \"format\":\"INEXISTENT_FORMAT\" }"); - XContentParser parser = JsonXContent.jsonXContent - .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json.streamInput()); - XContentParseException ex = expectThrows(XContentParseException.class, - () -> DataDescription.STRICT_PARSER.apply(parser, null)); + XContentParser parser = JsonXContent.jsonXContent.createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + json.streamInput() + ); + XContentParseException ex = expectThrows(XContentParseException.class, () -> DataDescription.STRICT_PARSER.apply(parser, null)); assertThat(ex.getMessage(), containsString("[data_description] failed to parse field [format]")); Throwable cause = ex.getCause(); assertNotNull(cause); assertThat(cause, instanceOf(IllegalArgumentException.class)); - assertThat(cause.getMessage(), - containsString("No enum constant org.elasticsearch.xpack.core.ml.job.config.DataDescription.DataFormat.INEXISTENT_FORMAT")); + assertThat( + cause.getMessage(), + containsString("No enum constant org.elasticsearch.xpack.core.ml.job.config.DataDescription.DataFormat.INEXISTENT_FORMAT") + ); } @Override @@ -125,14 +129,14 @@ protected DataDescription mutateInstance(DataDescription instance) throws java.i String timeField = instance.getTimeField(); String timeFormat = instance.getTimeFormat(); switch (between(0, 1)) { - case 0: - timeField += randomAlphaOfLengthBetween(1, 10); - break; - case 1: - timeFormat = "yyyy-MM-dd-HH-mm-ss"; - break; - default: - throw new AssertionError("Illegal randomisation branch"); + case 0: + timeField += randomAlphaOfLengthBetween(1, 10); + break; + case 1: + timeFormat = "yyyy-MM-dd-HH-mm-ss"; + break; + default: + throw new AssertionError("Illegal randomisation branch"); } return new DataDescription(timeField, timeFormat); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/DetectionRuleTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/DetectionRuleTests.java index 8279d408de8b9..e5e8a07e12a5b 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/DetectionRuleTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/DetectionRuleTests.java @@ -8,8 +8,8 @@ import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.common.io.stream.Writeable.Reader; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.util.ArrayList; import java.util.Arrays; @@ -30,9 +30,7 @@ public void testBuildWithNeitherScopeNorCondition() { } public void testExtractReferencedLists() { - DetectionRule rule = new DetectionRule.Builder(RuleScope.builder() - .exclude("foo", "filter1").include("bar", "filter2")) - .build(); + DetectionRule rule = new DetectionRule.Builder(RuleScope.builder().exclude("foo", "filter1").include("bar", "filter2")).build(); assertEquals(new HashSet<>(Arrays.asList("filter1", "filter2")), rule.extractReferencedFilters()); } @@ -101,22 +99,22 @@ protected DetectionRule mutateInstance(DetectionRule instance) { EnumSet actions = instance.getActions(); switch (between(0, 2)) { - case 0: - if (actions.size() == RuleAction.values().length) { - actions = EnumSet.of(randomFrom(RuleAction.values())); - } else { - actions = EnumSet.allOf(RuleAction.class); - } - break; - case 1: - conditions = new ArrayList<>(conditions); - conditions.addAll(createCondition(randomDouble())); - break; - case 2: - scope = new RuleScope.Builder(scope).include("another_field", "another_filter").build(); - break; - default: - throw new AssertionError("Illegal randomisation branch"); + case 0: + if (actions.size() == RuleAction.values().length) { + actions = EnumSet.of(randomFrom(RuleAction.values())); + } else { + actions = EnumSet.allOf(RuleAction.class); + } + break; + case 1: + conditions = new ArrayList<>(conditions); + conditions.addAll(createCondition(randomDouble())); + break; + case 2: + scope = new RuleScope.Builder(scope).include("another_field", "another_filter").build(); + break; + default: + throw new AssertionError("Illegal randomisation branch"); } return new DetectionRule.Builder(conditions).setActions(actions).setScope(scope).build(); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/DetectorTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/DetectorTests.java index 4345ffd4e15eb..f1eba7bb5e280 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/DetectorTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/DetectorTests.java @@ -9,9 +9,9 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.common.io.stream.Writeable.Reader; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.job.messages.Messages; import org.elasticsearch.xpack.core.ml.process.writer.RecordWriter; @@ -70,9 +70,8 @@ public void testEquals_GivenDifferentByFieldName() { public void testExtractAnalysisFields() { DetectionRule rule = new DetectionRule.Builder( - Collections.singletonList(new RuleCondition(RuleCondition.AppliesTo.ACTUAL, Operator.GT, 5))) - .setActions(RuleAction.SKIP_RESULT) - .build(); + Collections.singletonList(new RuleCondition(RuleCondition.AppliesTo.ACTUAL, Operator.GT, 5)) + ).setActions(RuleAction.SKIP_RESULT).build(); Detector.Builder builder = createDetector(); builder.setRules(Collections.singletonList(rule)); Detector detector = builder.build(); @@ -101,9 +100,12 @@ public void testExtractAnalysisFields() { public void testExtractReferencedLists() { Detector.Builder builder = createDetector(); - builder.setRules(Arrays.asList( + builder.setRules( + Arrays.asList( new DetectionRule.Builder(RuleScope.builder().exclude("by_field", "list1")).build(), - new DetectionRule.Builder(RuleScope.builder().exclude("by_field", "list2")).build())); + new DetectionRule.Builder(RuleScope.builder().exclude("by_field", "list2")).build() + ) + ); Detector detector = builder.build(); assertEquals(new HashSet<>(Arrays.asList("list1", "list2")), detector.extractReferencedFilters()); @@ -119,10 +121,12 @@ public void testInvalid_GivenFieldIsControlField() { detector.setPartitionFieldName(RecordWriter.CONTROL_FIELD_NAME); } - ElasticsearchException e = expectThrows(ElasticsearchException.class , detector::build); + ElasticsearchException e = expectThrows(ElasticsearchException.class, detector::build); - assertEquals(Messages.getMessage(Messages.JOB_CONFIG_INVALID_FIELDNAME, RecordWriter.CONTROL_FIELD_NAME, - RecordWriter.CONTROL_FIELD_NAME), e.getMessage()); + assertEquals( + Messages.getMessage(Messages.JOB_CONFIG_INVALID_FIELDNAME, RecordWriter.CONTROL_FIELD_NAME, RecordWriter.CONTROL_FIELD_NAME), + e.getMessage() + ); } private Detector.Builder createDetector() { @@ -131,9 +135,9 @@ private Detector.Builder createDetector() { detector.setOverFieldName("over_field"); detector.setPartitionFieldName("partition"); detector.setUseNull(true); - DetectionRule rule = new DetectionRule.Builder(RuleScope.builder().exclude("partition", "partition_filter")) - .setActions(RuleAction.SKIP_RESULT) - .build(); + DetectionRule rule = new DetectionRule.Builder(RuleScope.builder().exclude("partition", "partition_filter")).setActions( + RuleAction.SKIP_RESULT + ).build(); detector.setRules(Collections.singletonList(rule)); return detector; } @@ -188,7 +192,7 @@ protected Detector doParseInstance(XContentParser parser) { public void testVerifyFieldNames_givenInvalidChars() { Collection testCaseArguments = getCharactersAndValidity(); - for (Object [] args : testCaseArguments) { + for (Object[] args : testCaseArguments) { String character = (String) args[0]; boolean valid = (boolean) args[1]; Detector.Builder detector = createDetectorWithValidFieldNames(); @@ -204,7 +208,7 @@ public void testVerifyFieldNames_givenInvalidChars() { public void testVerifyFunctionForPreSummariedInput() { Collection testCaseArguments = getCharactersAndValidity(); - for (Object [] args : testCaseArguments) { + for (Object[] args : testCaseArguments) { String character = (String) args[0]; boolean valid = (boolean) args[1]; Detector.Builder detector = createDetectorWithValidFieldNames(); @@ -220,28 +224,28 @@ public void testVerifyFunctionForPreSummariedInput() { private static void verifyFieldName(Detector.Builder detector, String character, boolean valid) { Detector.Builder updated = createDetectorWithSpecificFieldName(detector.build().getFieldName() + character); if (valid == false) { - expectThrows(ElasticsearchException.class , updated::build); + expectThrows(ElasticsearchException.class, updated::build); } } private static void verifyByFieldName(Detector.Builder detector, String character, boolean valid) { detector.setByFieldName(detector.build().getByFieldName() + character); if (valid == false) { - expectThrows(ElasticsearchException.class , detector::build); + expectThrows(ElasticsearchException.class, detector::build); } } private static void verifyOverFieldName(Detector.Builder detector, String character, boolean valid) { detector.setOverFieldName(detector.build().getOverFieldName() + character); if (valid == false) { - expectThrows(ElasticsearchException.class , detector::build); + expectThrows(ElasticsearchException.class, detector::build); } } private static void verifyPartitionFieldName(Detector.Builder detector, String character, boolean valid) { detector.setPartitionFieldName(detector.build().getPartitionFieldName() + character); if (valid == false) { - expectThrows(ElasticsearchException.class , detector::build); + expectThrows(ElasticsearchException.class, detector::build); } } @@ -262,21 +266,22 @@ private static Detector.Builder createDetectorWithSpecificFieldName(String field } private static Collection getCharactersAndValidity() { - return Arrays.asList(new Object[][]{ + return Arrays.asList( + new Object[][] { // char, isValid? - {"a", true}, - {"[", true}, - {"]", true}, - {"(", true}, - {")", true}, - {"=", true}, - {"-", true}, - {" ", true}, - {"\"", false}, - {"\\", false}, - {"\t", false}, - {"\n", false}, - }); + { "a", true }, + { "[", true }, + { "]", true }, + { "(", true }, + { ")", true }, + { "=", true }, + { "-", true }, + { " ", true }, + { "\"", false }, + { "\\", false }, + { "\t", false }, + { "\n", false }, } + ); } public void testVerify_GivenFunctionOnly() { @@ -294,20 +299,21 @@ public void testVerify_GivenFunctionOnly() { difference.remove(DetectorFunction.TIME_OF_DAY); difference.remove(DetectorFunction.TIME_OF_WEEK); for (DetectorFunction f : difference) { - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> new Detector.Builder(f, null).build()); - assertThat(e.getMessage(), equalTo("Unless a count or temporal function is used one of field_name," + - " by_field_name or over_field_name must be set")); + ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, () -> new Detector.Builder(f, null).build()); + assertThat( + e.getMessage(), + equalTo("Unless a count or temporal function is used one of field_name," + " by_field_name or over_field_name must be set") + ); } } public void testVerify_GivenFunctionsNotSupportingOverField() { EnumSet noOverFieldFunctions = EnumSet.of( - DetectorFunction.NON_ZERO_COUNT, - DetectorFunction.LOW_NON_ZERO_COUNT, - DetectorFunction.HIGH_NON_ZERO_COUNT + DetectorFunction.NON_ZERO_COUNT, + DetectorFunction.LOW_NON_ZERO_COUNT, + DetectorFunction.HIGH_NON_ZERO_COUNT ); - for (DetectorFunction f: noOverFieldFunctions) { + for (DetectorFunction f : noOverFieldFunctions) { Detector.Builder builder = new Detector.Builder(f, null); builder.setOverFieldName("over_field"); ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, () -> builder.build()); @@ -322,7 +328,7 @@ public void testVerify_GivenFunctionsCannotHaveJustOverField() { difference.remove(DetectorFunction.HIGH_COUNT); difference.remove(DetectorFunction.TIME_OF_DAY); difference.remove(DetectorFunction.TIME_OF_WEEK); - for (DetectorFunction f: difference) { + for (DetectorFunction f : difference) { Detector.Builder builder = new Detector.Builder(f, null); builder.setOverFieldName("over_field"); expectThrows(ElasticsearchStatusException.class, () -> builder.build()); @@ -331,11 +337,11 @@ public void testVerify_GivenFunctionsCannotHaveJustOverField() { public void testVerify_GivenFunctionsCanHaveJustOverField() { EnumSet noOverFieldFunctions = EnumSet.of( - DetectorFunction.COUNT, - DetectorFunction.LOW_COUNT, - DetectorFunction.HIGH_COUNT + DetectorFunction.COUNT, + DetectorFunction.LOW_COUNT, + DetectorFunction.HIGH_COUNT ); - for (DetectorFunction f: noOverFieldFunctions) { + for (DetectorFunction f : noOverFieldFunctions) { Detector.Builder builder = new Detector.Builder(f, null); builder.setOverFieldName("over_field"); builder.build(); @@ -435,9 +441,15 @@ public void testVerify_GivenRareAndFreqRareWithByAndOverFields() { } public void testVerify_GivenFunctionsThatCanHaveByField() { - for (DetectorFunction f : EnumSet.of(DetectorFunction.COUNT, DetectorFunction.HIGH_COUNT, DetectorFunction.LOW_COUNT, - DetectorFunction.RARE, DetectorFunction.NON_ZERO_COUNT, DetectorFunction.LOW_NON_ZERO_COUNT, - DetectorFunction.HIGH_NON_ZERO_COUNT)) { + for (DetectorFunction f : EnumSet.of( + DetectorFunction.COUNT, + DetectorFunction.HIGH_COUNT, + DetectorFunction.LOW_COUNT, + DetectorFunction.RARE, + DetectorFunction.NON_ZERO_COUNT, + DetectorFunction.LOW_NON_ZERO_COUNT, + DetectorFunction.HIGH_NON_ZERO_COUNT + )) { Detector.Builder builder = new Detector.Builder(f, null); builder.setByFieldName("b"); builder.build(); @@ -450,11 +462,9 @@ public void testVerify_GivenAllPartitioningFieldsAreScoped() { detector.setOverFieldName("my_over"); detector.setByFieldName("my_by"); - DetectionRule rule = new DetectionRule.Builder(RuleScope.builder() - .exclude("my_partition", "my_filter_id") - .exclude("my_over", "my_filter_id") - .exclude("my_by", "my_filter_id")) - .build(); + DetectionRule rule = new DetectionRule.Builder( + RuleScope.builder().exclude("my_partition", "my_filter_id").exclude("my_over", "my_filter_id").exclude("my_by", "my_filter_id") + ).build(); detector.setRules(Collections.singletonList(rule)); detector.build(); @@ -537,8 +547,12 @@ public void testVerify_GivenOverIsOver() { public void testVerify_GivenRulesAndFunctionIsLatLong() { Detector.Builder detector = new Detector.Builder("lat_long", "geo"); - detector.setRules(Collections.singletonList(new DetectionRule.Builder(Collections.singletonList( - new RuleCondition(RuleCondition.AppliesTo.ACTUAL, Operator.GT, 42.0))).build())); + detector.setRules( + Collections.singletonList( + new DetectionRule.Builder(Collections.singletonList(new RuleCondition(RuleCondition.AppliesTo.ACTUAL, Operator.GT, 42.0))) + .build() + ) + ); ElasticsearchException e = ESTestCase.expectThrows(ElasticsearchException.class, detector::build); @@ -547,8 +561,12 @@ public void testVerify_GivenRulesAndFunctionIsLatLong() { public void testVerify_GivenRulesAndFunctionIsMetric() { Detector.Builder detector = new Detector.Builder("metric", "some_metric"); - detector.setRules(Collections.singletonList(new DetectionRule.Builder(Collections.singletonList( - new RuleCondition(RuleCondition.AppliesTo.TYPICAL, Operator.GT, 42.0))).build())); + detector.setRules( + Collections.singletonList( + new DetectionRule.Builder(Collections.singletonList(new RuleCondition(RuleCondition.AppliesTo.TYPICAL, Operator.GT, 42.0))) + .build() + ) + ); ElasticsearchException e = ESTestCase.expectThrows(ElasticsearchException.class, detector::build); @@ -558,8 +576,13 @@ public void testVerify_GivenRulesAndFunctionIsMetric() { public void testVerify_GivenRulesAndFunctionIsRare() { Detector.Builder detector = new Detector.Builder("rare", null); detector.setByFieldName("some_field"); - detector.setRules(Collections.singletonList(new DetectionRule.Builder(Collections.singletonList( - new RuleCondition(RuleCondition.AppliesTo.DIFF_FROM_TYPICAL, Operator.GT, 42.0))).build())); + detector.setRules( + Collections.singletonList( + new DetectionRule.Builder( + Collections.singletonList(new RuleCondition(RuleCondition.AppliesTo.DIFF_FROM_TYPICAL, Operator.GT, 42.0)) + ).build() + ) + ); ElasticsearchException e = ESTestCase.expectThrows(ElasticsearchException.class, detector::build); @@ -570,8 +593,12 @@ public void testVerify_GivenRulesAndFunctionIsFreqRare() { Detector.Builder detector = new Detector.Builder("freq_rare", null); detector.setByFieldName("some_field"); detector.setOverFieldName("some_field2"); - detector.setRules(Collections.singletonList(new DetectionRule.Builder(Collections.singletonList( - new RuleCondition(RuleCondition.AppliesTo.ACTUAL, Operator.GT, 42.0))).build())); + detector.setRules( + Collections.singletonList( + new DetectionRule.Builder(Collections.singletonList(new RuleCondition(RuleCondition.AppliesTo.ACTUAL, Operator.GT, 42.0))) + .build() + ) + ); ElasticsearchException e = ESTestCase.expectThrows(ElasticsearchException.class, detector::build); @@ -580,8 +607,12 @@ public void testVerify_GivenRulesAndFunctionIsFreqRare() { public void testVerify_GivenTimeConditionRuleAndFunctionIsLatLong() { Detector.Builder detector = new Detector.Builder("lat_long", "geo"); - detector.setRules(Collections.singletonList(new DetectionRule.Builder(Collections.singletonList( - new RuleCondition(RuleCondition.AppliesTo.TIME, Operator.GT, 42.0))).build())); + detector.setRules( + Collections.singletonList( + new DetectionRule.Builder(Collections.singletonList(new RuleCondition(RuleCondition.AppliesTo.TIME, Operator.GT, 42.0))) + .build() + ) + ); detector.build(); } @@ -596,20 +627,19 @@ public void testVerify_GivenScopeRuleOnInvalidField() { ElasticsearchException e = ESTestCase.expectThrows(ElasticsearchException.class, detector::build); - assertEquals(Messages.getMessage(Messages.JOB_CONFIG_DETECTION_RULE_SCOPE_HAS_INVALID_FIELD, - "my_metric", "[my_by, my_over, my_partition]"), e.getMessage()); + assertEquals( + Messages.getMessage(Messages.JOB_CONFIG_DETECTION_RULE_SCOPE_HAS_INVALID_FIELD, "my_metric", "[my_by, my_over, my_partition]"), + e.getMessage() + ); } public void testVerify_GivenValidRule() { Detector.Builder detector = new Detector.Builder("mean", "metricVale"); detector.setByFieldName("metricName"); detector.setPartitionFieldName("instance"); - DetectionRule rule = new DetectionRule.Builder(Collections.singletonList(RuleConditionTests.createRandom())) - .setScope(RuleScope.builder() - .include("metricName", "f1") - .exclude("instance", "f2") - .build()) - .build(); + DetectionRule rule = new DetectionRule.Builder(Collections.singletonList(RuleConditionTests.createRandom())).setScope( + RuleScope.builder().include("metricName", "f1").exclude("instance", "f2").build() + ).build(); detector.setRules(Collections.singletonList(rule)); detector.build(); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/FilterRefTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/FilterRefTests.java index acb403a4c2126..366ee882f2792 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/FilterRefTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/FilterRefTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ml.job.config; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/JobTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/JobTests.java index 809978fe7a7a9..39e9e467b59ce 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/JobTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/JobTests.java @@ -7,6 +7,7 @@ package org.elasticsearch.xpack.core.ml.job.config; import com.carrotsearch.randomizedtesting.generators.CodepointSetGenerator; + import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.ResourceAlreadyExistsException; import org.elasticsearch.Version; @@ -17,19 +18,19 @@ import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; -import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.search.SearchModule; +import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentParseException; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.search.SearchModule; -import org.elasticsearch.test.AbstractSerializingTestCase; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.ml.MachineLearningField; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfig; import org.elasticsearch.xpack.core.ml.job.messages.Messages; @@ -55,20 +56,20 @@ public class JobTests extends AbstractSerializingTestCase { - private static final String FUTURE_JOB = "{\n" + - " \"job_id\": \"farequote\",\n" + - " \"create_time\": 1234567890000,\n" + - " \"tomorrows_technology_today\": \"wow\",\n" + - " \"analysis_config\": {\n" + - " \"bucket_span\": \"1h\",\n" + - " \"something_new\": \"gasp\",\n" + - " \"detectors\": [{\"function\": \"metric\", \"field_name\": \"responsetime\", \"by_field_name\": \"airline\"}]\n" + - " },\n" + - " \"data_description\": {\n" + - " \"time_field\": \"time\",\n" + - " \"the_future\": 123\n" + - " }\n" + - "}"; + private static final String FUTURE_JOB = "{\n" + + " \"job_id\": \"farequote\",\n" + + " \"create_time\": 1234567890000,\n" + + " \"tomorrows_technology_today\": \"wow\",\n" + + " \"analysis_config\": {\n" + + " \"bucket_span\": \"1h\",\n" + + " \"something_new\": \"gasp\",\n" + + " \"detectors\": [{\"function\": \"metric\", \"field_name\": \"responsetime\", \"by_field_name\": \"airline\"}]\n" + + " },\n" + + " \"data_description\": {\n" + + " \"time_field\": \"time\",\n" + + " \"the_future\": 123\n" + + " }\n" + + "}"; @Override protected Job createTestInstance() { @@ -112,15 +113,14 @@ public void testToXContentForInternalStorage() throws IOException { public void testFutureConfigParse() throws IOException { XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, FUTURE_JOB); - XContentParseException e = expectThrows(XContentParseException.class, - () -> Job.STRICT_PARSER.apply(parser, null).build()); + .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, FUTURE_JOB); + XContentParseException e = expectThrows(XContentParseException.class, () -> Job.STRICT_PARSER.apply(parser, null).build()); assertEquals("[4:5] [job_details] unknown field [tomorrows_technology_today]", e.getMessage()); } public void testFutureMetadataParse() throws IOException { XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, FUTURE_JOB); + .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, FUTURE_JOB); // Unlike the config version of this test, the metadata parser should tolerate the unknown future field assertNotNull(Job.LENIENT_PARSER.apply(parser, null).build()); } @@ -182,10 +182,16 @@ public void testValidateAnalysisLimitsAndSetDefaults_whenMaxIsLessThanTheDefault public void testValidateAnalysisLimitsAndSetDefaults_throwsWhenMaxLimitIsExceeded() { Job.Builder builder = buildJobBuilder("foo"); builder.setAnalysisLimits(new AnalysisLimits(4096L, null)); - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> builder.validateAnalysisLimitsAndSetDefaults(new ByteSizeValue(1000L, ByteSizeUnit.MB))); - assertEquals("model_memory_limit [4gb] must be less than the value of the " + - MachineLearningField.MAX_MODEL_MEMORY_LIMIT.getKey() + " setting [1000mb]", e.getMessage()); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> builder.validateAnalysisLimitsAndSetDefaults(new ByteSizeValue(1000L, ByteSizeUnit.MB)) + ); + assertEquals( + "model_memory_limit [4gb] must be less than the value of the " + + MachineLearningField.MAX_MODEL_MEMORY_LIMIT.getKey() + + " setting [1000mb]", + e.getMessage() + ); builder.validateAnalysisLimitsAndSetDefaults(new ByteSizeValue(8192L, ByteSizeUnit.MB)); } @@ -338,7 +344,7 @@ public void testCopyConstructor() { } } - public void testCheckValidId_IdTooLong() { + public void testCheckValidId_IdTooLong() { Job.Builder builder = buildJobBuilder("foo"); builder.setId("averyveryveryaveryveryveryaveryveryveryaveryveryveryaveryveryveryaveryveryverylongid"); expectThrows(IllegalArgumentException.class, builder::build); @@ -414,8 +420,7 @@ public void jobConfigurationTest() { } public void testVerify_GivenNegativeRenormalizationWindowDays() { - String errorMessage = Messages.getMessage(Messages.JOB_CONFIG_FIELD_VALUE_TOO_LOW, - "renormalization_window_days", 0, -1); + String errorMessage = Messages.getMessage(Messages.JOB_CONFIG_FIELD_VALUE_TOO_LOW, "renormalization_window_days", 0, -1); Job.Builder builder = buildJobBuilder("foo"); builder.setRenormalizationWindowDays(-1L); IllegalArgumentException e = ESTestCase.expectThrows(IllegalArgumentException.class, builder::build); @@ -432,8 +437,12 @@ public void testVerify_GivenNegativeModelSnapshotRetentionDays() { } public void testVerify_GivenNegativeDailyModelSnapshotRetentionAfterDays() { - String errorMessage = - Messages.getMessage(Messages.JOB_CONFIG_FIELD_VALUE_TOO_LOW, "daily_model_snapshot_retention_after_days", 0, -1); + String errorMessage = Messages.getMessage( + Messages.JOB_CONFIG_FIELD_VALUE_TOO_LOW, + "daily_model_snapshot_retention_after_days", + 0, + -1 + ); Job.Builder builder = buildJobBuilder("foo"); builder.setDailyModelSnapshotRetentionAfterDays(-1L); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, builder::build); @@ -444,9 +453,11 @@ public void testVerify_GivenNegativeDailyModelSnapshotRetentionAfterDays() { public void testVerify_GivenInconsistentModelSnapshotRetentionSettings() { long dailyModelSnapshotRetentionAfterDays = randomLongBetween(1, Long.MAX_VALUE); long modelSnapshotRetentionDays = randomLongBetween(0, dailyModelSnapshotRetentionAfterDays - 1); - String errorMessage = - Messages.getMessage(Messages.JOB_CONFIG_MODEL_SNAPSHOT_RETENTION_SETTINGS_INCONSISTENT, - dailyModelSnapshotRetentionAfterDays, modelSnapshotRetentionDays); + String errorMessage = Messages.getMessage( + Messages.JOB_CONFIG_MODEL_SNAPSHOT_RETENTION_SETTINGS_INCONSISTENT, + dailyModelSnapshotRetentionAfterDays, + modelSnapshotRetentionDays + ); Job.Builder builder = buildJobBuilder("foo"); builder.setDailyModelSnapshotRetentionAfterDays(dailyModelSnapshotRetentionAfterDays); builder.setModelSnapshotRetentionDays(modelSnapshotRetentionDays); @@ -474,8 +485,10 @@ public void testVerify_GivenNegativeResultsRetentionDays() { public void testBuilder_setsDefaultIndexName() { Job.Builder builder = buildJobBuilder("foo"); Job job = builder.build(); - assertEquals(AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + AnomalyDetectorsIndexFields.RESULTS_INDEX_DEFAULT, - job.getInitialResultsIndexName()); + assertEquals( + AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + AnomalyDetectorsIndexFields.RESULTS_INDEX_DEFAULT, + job.getInitialResultsIndexName() + ); } public void testBuilder_setsIndexName() { @@ -508,7 +521,7 @@ public void testJobWithoutVersion() throws IOException { // Assert parsing a job without version works as expected XContentType xContentType = randomFrom(XContentType.values()); BytesReference bytes = XContentHelper.toXContent(job, xContentType, false); - try(XContentParser parser = createParser(xContentType.xContent(), bytes)) { + try (XContentParser parser = createParser(xContentType.xContent(), bytes)) { Job parsed = doParseInstance(parser); assertThat(parsed, equalTo(job)); } @@ -573,14 +586,16 @@ public void testInvalidGroup_matchesJobId() { } public void testInvalidAnalysisConfig_duplicateDetectors() throws Exception { - Job.Builder builder = - new Job.Builder("job_with_duplicate_detectors") - .setCreateTime(new Date()) - .setDataDescription(new DataDescription.Builder()) - .setAnalysisConfig(new AnalysisConfig.Builder(Arrays.asList( - new Detector.Builder("mean", "responsetime").build(), - new Detector.Builder("mean", "responsetime").build() - ))); + Job.Builder builder = new Job.Builder("job_with_duplicate_detectors").setCreateTime(new Date()) + .setDataDescription(new DataDescription.Builder()) + .setAnalysisConfig( + new AnalysisConfig.Builder( + Arrays.asList( + new Detector.Builder("mean", "responsetime").build(), + new Detector.Builder("mean", "responsetime").build() + ) + ) + ); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, builder::validateDetectorsAreUnique); assertThat(e.getMessage(), containsString("Duplicate detectors are not allowed: [mean(responsetime)]")); @@ -620,43 +635,42 @@ public void testCopyingJobDoesNotCauseStackOverflow() { public void testDocumentId() { String jobFoo = "foo"; assertEquals("anomaly_detector-" + jobFoo, Job.documentId(jobFoo)); - assertEquals(jobFoo, Job.extractJobIdFromDocumentId( - Job.documentId(jobFoo) - )); + assertEquals(jobFoo, Job.extractJobIdFromDocumentId(Job.documentId(jobFoo))); assertNull(Job.extractJobIdFromDocumentId("some_other_type-foo")); } public void testDeletingAndBlockReasonAreSynced() { { - Job job = buildJobBuilder(randomValidJobId()) - .setDeleting(true) - .build(); + Job job = buildJobBuilder(randomValidJobId()).setDeleting(true).build(); assertThat(job.getBlocked().getReason(), equalTo(Blocked.Reason.DELETE)); } { - Job job = buildJobBuilder(randomValidJobId()) - .setBlocked(new Blocked(Blocked.Reason.DELETE, null)) - .build(); + Job job = buildJobBuilder(randomValidJobId()).setBlocked(new Blocked(Blocked.Reason.DELETE, null)).build(); assertThat(job.isDeleting(), is(true)); } } public void testParseJobWithDeletingButWithoutBlockReason() throws IOException { - String jobWithDeleting = "{\n" + - " \"job_id\": \"deleting_job\",\n" + - " \"create_time\": 1234567890000,\n" + - " \"analysis_config\": {\n" + - " \"bucket_span\": \"1h\",\n" + - " \"detectors\": [{\"function\": \"count\"}]\n" + - " },\n" + - " \"data_description\": {\n" + - " \"time_field\": \"time\"\n" + - " },\n" + - " \"deleting\": true\n" + - "}"; - - try (XContentParser parser = JsonXContent.jsonXContent.createParser( - NamedXContentRegistry.EMPTY, DeprecationHandler.IGNORE_DEPRECATIONS, jobWithDeleting)) { + String jobWithDeleting = "{\n" + + " \"job_id\": \"deleting_job\",\n" + + " \"create_time\": 1234567890000,\n" + + " \"analysis_config\": {\n" + + " \"bucket_span\": \"1h\",\n" + + " \"detectors\": [{\"function\": \"count\"}]\n" + + " },\n" + + " \"data_description\": {\n" + + " \"time_field\": \"time\"\n" + + " },\n" + + " \"deleting\": true\n" + + "}"; + + try ( + XContentParser parser = JsonXContent.jsonXContent.createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.IGNORE_DEPRECATIONS, + jobWithDeleting + ) + ) { Job job = doParseInstance(parser); assertThat(job.getBlocked().getReason(), equalTo(Blocked.Reason.DELETE)); } @@ -677,7 +691,7 @@ public static Job.Builder buildJobBuilder(String id) { } public static String randomValidJobId() { - CodepointSetGenerator generator = new CodepointSetGenerator("abcdefghijklmnopqrstuvwxyz".toCharArray()); + CodepointSetGenerator generator = new CodepointSetGenerator("abcdefghijklmnopqrstuvwxyz".toCharArray()); return generator.ofCodePointsLength(random(), 10, 10); } @@ -714,8 +728,13 @@ public static Job createRandomizedJob(DatafeedConfig.Builder datafeedBuilder) { builder.setFinishedTime(new Date(randomNonNegativeLong())); } builder.setAnalysisConfig(AnalysisConfigTests.createRandomized()); - builder.setAnalysisLimits(AnalysisLimits.validateAndSetDefaults(AnalysisLimitsTests.createRandomized(), null, - AnalysisLimits.DEFAULT_MODEL_MEMORY_LIMIT_MB)); + builder.setAnalysisLimits( + AnalysisLimits.validateAndSetDefaults( + AnalysisLimitsTests.createRandomized(), + null, + AnalysisLimits.DEFAULT_MODEL_MEMORY_LIMIT_MB + ) + ); DataDescription.Builder dataDescription = new DataDescription.Builder(); builder.setDataDescription(dataDescription); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/JobUpdateTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/JobUpdateTests.java index a08253377d7e6..7caf9d066b9e1 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/JobUpdateTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/JobUpdateTests.java @@ -8,14 +8,14 @@ import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.Version; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.test.VersionUtils; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.job.process.autodetect.state.ModelSnapshot; import java.util.ArrayList; @@ -60,16 +60,22 @@ public JobUpdate createRandom(String jobId, @Nullable Job job) { update.setDescription(randomAlphaOfLength(20)); } if (randomBoolean()) { - List detectorUpdates = job == null ? createRandomDetectorUpdates() - : createRandomDetectorUpdatesForJob(job); + List detectorUpdates = job == null + ? createRandomDetectorUpdates() + : createRandomDetectorUpdatesForJob(job); update.setDetectorUpdates(detectorUpdates); } if (randomBoolean()) { update.setModelPlotConfig(ModelPlotConfigTests.createRandomized()); } if (randomBoolean()) { - update.setAnalysisLimits(AnalysisLimits.validateAndSetDefaults(AnalysisLimitsTests.createRandomized(), null, - AnalysisLimits.DEFAULT_MODEL_MEMORY_LIMIT_MB)); + update.setAnalysisLimits( + AnalysisLimits.validateAndSetDefaults( + AnalysisLimitsTests.createRandomized(), + null, + AnalysisLimits.DEFAULT_MODEL_MEMORY_LIMIT_MB + ) + ); } if (randomBoolean()) { update.setRenormalizationWindowDays(randomNonNegativeLong()); @@ -134,8 +140,11 @@ public JobUpdate createRandom(String jobId, @Nullable Job job) { update.setBlocked(BlockedTests.createRandom()); } if (randomBoolean() && job != null) { - update.setModelPruneWindow(TimeValue.timeValueSeconds(TimeValue.timeValueSeconds(randomIntBetween(2, 100)).seconds() - * job.getAnalysisConfig().getBucketSpan().seconds())); + update.setModelPruneWindow( + TimeValue.timeValueSeconds( + TimeValue.timeValueSeconds(randomIntBetween(2, 100)).seconds() * job.getAnalysisConfig().getBucketSpan().seconds() + ) + ); } return update.build(); @@ -172,8 +181,10 @@ private static List createRandomDetectorUpdates() { List detectionRules = null; if (randomBoolean()) { detectionRules = new ArrayList<>(); - detectionRules.add(new DetectionRule.Builder( - Collections.singletonList(new RuleCondition(RuleCondition.AppliesTo.ACTUAL, Operator.GT, 5))).build()); + detectionRules.add( + new DetectionRule.Builder(Collections.singletonList(new RuleCondition(RuleCondition.AppliesTo.ACTUAL, Operator.GT, 5))) + .build() + ); } detectorUpdates.add(new JobUpdate.DetectorUpdate(i, detectorDescription, detectionRules)); } @@ -196,8 +207,17 @@ private static List createRandomDetectorUpdatesForJob( Detector detector = analysisConfig.getDetectors().get(detectorIndex); List analysisFields = detector.extractAnalysisFields(); if (randomBoolean() || analysisFields.isEmpty()) { - detectionRules.add(new DetectionRule.Builder(Collections.singletonList(new RuleCondition( - randomFrom(RuleCondition.AppliesTo.values()), randomFrom(Operator.values()), randomDouble()))).build()); + detectionRules.add( + new DetectionRule.Builder( + Collections.singletonList( + new RuleCondition( + randomFrom(RuleCondition.AppliesTo.values()), + randomFrom(Operator.values()), + randomDouble() + ) + ) + ).build() + ); } else { RuleScope.Builder ruleScope = RuleScope.builder(); int scopeSize = randomIntBetween(1, analysisFields.size()); @@ -235,12 +255,13 @@ protected JobUpdate doParseInstance(XContentParser parser) { public void testMergeWithJob() { List detectorUpdates = new ArrayList<>(); - List detectionRules1 = Collections.singletonList(new DetectionRule.Builder( - Collections.singletonList(new RuleCondition(RuleCondition.AppliesTo.ACTUAL, Operator.GT, 5))) - .build()); + List detectionRules1 = Collections.singletonList( + new DetectionRule.Builder(Collections.singletonList(new RuleCondition(RuleCondition.AppliesTo.ACTUAL, Operator.GT, 5))).build() + ); detectorUpdates.add(new JobUpdate.DetectorUpdate(0, "description-1", detectionRules1)); - List detectionRules2 = Collections.singletonList(new DetectionRule.Builder(Collections.singletonList( - new RuleCondition(RuleCondition.AppliesTo.ACTUAL, Operator.GT, 5))).build()); + List detectionRules2 = Collections.singletonList( + new DetectionRule.Builder(Collections.singletonList(new RuleCondition(RuleCondition.AppliesTo.ACTUAL, Operator.GT, 5))).build() + ); detectorUpdates.add(new JobUpdate.DetectorUpdate(1, "description-2", detectionRules2)); ModelPlotConfig modelPlotConfig = ModelPlotConfigTests.createRandomized(); @@ -295,8 +316,10 @@ public void testMergeWithJob() { assertEquals(update.getModelSnapshotRetentionDays(), updatedJob.getModelSnapshotRetentionDays()); assertEquals(update.getResultsRetentionDays(), updatedJob.getResultsRetentionDays()); assertEquals(update.getCategorizationFilters(), updatedJob.getAnalysisConfig().getCategorizationFilters()); - assertEquals(update.getPerPartitionCategorizationConfig().isEnabled(), - updatedJob.getAnalysisConfig().getPerPartitionCategorizationConfig().isEnabled()); + assertEquals( + update.getPerPartitionCategorizationConfig().isEnabled(), + updatedJob.getAnalysisConfig().getPerPartitionCategorizationConfig().isEnabled() + ); assertEquals(update.getCustomSettings(), updatedJob.getCustomSettings()); assertEquals(update.getModelSnapshotId(), updatedJob.getModelSnapshotId()); assertEquals(update.getJobVersion(), updatedJob.getJobVersion()); @@ -333,8 +356,7 @@ public void testIsAutodetectProcessUpdate() { assertTrue(update.isAutodetectProcessUpdate()); update = new JobUpdate.Builder("foo").setGroups(Collections.singletonList("bar")).build(); assertTrue(update.isAutodetectProcessUpdate()); - update = new JobUpdate.Builder("foo") - .setPerPartitionCategorizationConfig(new PerPartitionCategorizationConfig(true, true)).build(); + update = new JobUpdate.Builder("foo").setPerPartitionCategorizationConfig(new PerPartitionCategorizationConfig(true, true)).build(); assertTrue(update.isAutodetectProcessUpdate()); } @@ -353,10 +375,14 @@ public void testUpdateAnalysisLimitWithValueGreaterThanMax() { JobUpdate update = new JobUpdate.Builder("foo").setAnalysisLimits(new AnalysisLimits(1024L, null)).build(); - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> update.mergeWithJob(jobBuilder.build(), new ByteSizeValue(512L, ByteSizeUnit.MB))); - assertEquals("model_memory_limit [1gb] must be less than the value of the xpack.ml.max_model_memory_limit setting [512mb]", - e.getMessage()); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> update.mergeWithJob(jobBuilder.build(), new ByteSizeValue(512L, ByteSizeUnit.MB)) + ); + assertEquals( + "model_memory_limit [1gb] must be less than the value of the xpack.ml.max_model_memory_limit setting [512mb]", + e.getMessage() + ); } public void testUpdate_withAnalysisLimitsPreviouslyUndefined() { @@ -375,10 +401,14 @@ public void testUpdate_withAnalysisLimitsPreviouslyUndefined() { JobUpdate updateAboveMaxLimit = new JobUpdate.Builder("foo").setAnalysisLimits(new AnalysisLimits(8000L, null)).build(); - Exception e = expectThrows(ElasticsearchStatusException.class, - () -> updateAboveMaxLimit.mergeWithJob(jobBuilder.build(), new ByteSizeValue(5000L, ByteSizeUnit.MB))); - assertEquals("model_memory_limit [7.8gb] must be less than the value of the xpack.ml.max_model_memory_limit setting [4.8gb]", - e.getMessage()); + Exception e = expectThrows( + ElasticsearchStatusException.class, + () -> updateAboveMaxLimit.mergeWithJob(jobBuilder.build(), new ByteSizeValue(5000L, ByteSizeUnit.MB)) + ); + assertEquals( + "model_memory_limit [7.8gb] must be less than the value of the xpack.ml.max_model_memory_limit setting [4.8gb]", + e.getMessage() + ); updateAboveMaxLimit.mergeWithJob(jobBuilder.build(), new ByteSizeValue(10000L, ByteSizeUnit.MB)); } @@ -394,8 +424,7 @@ public void testUpdate_givenEmptySnapshot() { Job job = jobBuilder.build(); assertThat(job.getModelSnapshotId(), equalTo("some_snapshot_id")); - JobUpdate update = new JobUpdate.Builder(job.getId()) - .setModelSnapshotId(ModelSnapshot.emptySnapshot(job.getId()).getSnapshotId()) + JobUpdate update = new JobUpdate.Builder(job.getId()).setModelSnapshotId(ModelSnapshot.emptySnapshot(job.getId()).getSnapshotId()) .build(); Job updatedJob = update.mergeWithJob(job, ByteSizeValue.ofMb(100)); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/MlFilterTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/MlFilterTests.java index 0f6b20d863b78..48948e5fead4f 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/MlFilterTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/MlFilterTests.java @@ -7,11 +7,12 @@ package org.elasticsearch.xpack.core.ml.job.config; import com.carrotsearch.randomizedtesting.generators.CodepointSetGenerator; + import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.common.io.stream.Writeable.Reader; +import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.AbstractSerializingTestCase; import java.io.IOException; import java.util.ArrayList; @@ -40,7 +41,7 @@ public static MlFilter createRandom() { } public static String randomValidFilterId() { - CodepointSetGenerator generator = new CodepointSetGenerator("abcdefghijklmnopqrstuvwxyz".toCharArray()); + CodepointSetGenerator generator = new CodepointSetGenerator("abcdefghijklmnopqrstuvwxyz".toCharArray()); return generator.ofCodePointsLength(random(), 10, 10); } @@ -74,8 +75,10 @@ public void testNullId() { } public void testNullItems() { - Exception ex = expectThrows(IllegalArgumentException.class, - () -> MlFilter.builder(randomValidFilterId()).setItems((SortedSet) null).build()); + Exception ex = expectThrows( + IllegalArgumentException.class, + () -> MlFilter.builder(randomValidFilterId()).setItems((SortedSet) null).build() + ); assertEquals("[items] must not be null.", ex.getMessage()); } @@ -86,8 +89,7 @@ public void testDocumentId() { public void testStrictParser() throws IOException { String json = "{\"filter_id\":\"filter_1\", \"items\": [], \"foo\":\"bar\"}"; try (XContentParser parser = createParser(JsonXContent.jsonXContent, json)) { - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> MlFilter.STRICT_PARSER.apply(parser, null)); + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> MlFilter.STRICT_PARSER.apply(parser, null)); assertThat(e.getMessage(), containsString("unknown field [foo]")); } @@ -110,8 +112,10 @@ public void testTooManyItems() { for (int i = 0; i < 10001; ++i) { items.add("item_" + i); } - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> MlFilter.builder("huge").setItems(items).build()); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> MlFilter.builder("huge").setItems(items).build() + ); assertThat(e.getMessage(), startsWith("Filter [huge] contains too many items")); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/ModelPlotConfigTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/ModelPlotConfigTests.java index 04c18eef9efd1..039f86cba1db2 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/ModelPlotConfigTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/ModelPlotConfigTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ml.job.config; import org.elasticsearch.common.io.stream.Writeable.Reader; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.nullValue; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/PerPartitionCategorizationConfigTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/PerPartitionCategorizationConfigTests.java index c50f9c2b67b16..40ba131b6a422 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/PerPartitionCategorizationConfigTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/PerPartitionCategorizationConfigTests.java @@ -9,8 +9,8 @@ import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import static org.hamcrest.Matchers.is; @@ -22,8 +22,10 @@ public void testConstructorDefaults() { } public void testValidation() { - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> new PerPartitionCategorizationConfig(false, true)); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> new PerPartitionCategorizationConfig(false, true) + ); assertThat(e.getMessage(), is("stop_on_warn cannot be true in per_partition_categorization when enabled is false")); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/RuleConditionTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/RuleConditionTests.java index 9a498e2821c5c..49007d7ec16e4 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/RuleConditionTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/RuleConditionTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ml.job.config; import org.elasticsearch.common.io.stream.Writeable.Reader; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; public class RuleConditionTests extends AbstractSerializingTestCase { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/RuleScopeTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/RuleScopeTests.java index 5168212ea0489..da90cdfd70b18 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/RuleScopeTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/RuleScopeTests.java @@ -48,37 +48,31 @@ public void testValidate_GivenEmpty() { } public void testValidate_GivenMultipleValidFields() { - RuleScope scope = RuleScope.builder() - .include("foo", "filter1") - .exclude("bar", "filter2") - .include("foobar", "filter3") - .build(); + RuleScope scope = RuleScope.builder().include("foo", "filter1").exclude("bar", "filter2").include("foobar", "filter3").build(); assertThat(scope.isEmpty(), is(false)); scope.validate(Sets.newHashSet("foo", "bar", "foobar")); } public void testValidate_GivenNoAvailableFieldsForScope() { - RuleScope scope = RuleScope.builder() - .include("foo", "filter1") - .build(); + RuleScope scope = RuleScope.builder().include("foo", "filter1").build(); assertThat(scope.isEmpty(), is(false)); ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, () -> scope.validate(Collections.emptySet())); - assertThat(e.getMessage(), equalTo("Invalid detector rule: scope field 'foo' is invalid; " + - "detector has no available fields for scoping")); + assertThat( + e.getMessage(), + equalTo("Invalid detector rule: scope field 'foo' is invalid; " + "detector has no available fields for scoping") + ); } public void testValidate_GivenMultipleFieldsIncludingInvalid() { - RuleScope scope = RuleScope.builder() - .include("foo", "filter1") - .exclude("bar", "filter2") - .include("foobar", "filter3") - .build(); + RuleScope scope = RuleScope.builder().include("foo", "filter1").exclude("bar", "filter2").include("foobar", "filter3").build(); assertThat(scope.isEmpty(), is(false)); - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> scope.validate(new LinkedHashSet<>(Arrays.asList("foo", "foobar")))); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> scope.validate(new LinkedHashSet<>(Arrays.asList("foo", "foobar"))) + ); assertThat(e.getMessage(), equalTo("Invalid detector rule: scope field 'bar' is invalid; select from [foo, foobar]")); } @@ -87,11 +81,7 @@ public void testGetReferencedFilters_GivenEmpty() { } public void testGetReferencedFilters_GivenMultipleFields() { - RuleScope scope = RuleScope.builder() - .include("foo", "filter1") - .exclude("bar", "filter2") - .include("foobar", "filter3") - .build(); + RuleScope scope = RuleScope.builder().include("foo", "filter1").exclude("bar", "filter2").include("foobar", "filter3").build(); assertThat(scope.getReferencedFilters(), contains("filter1", "filter2", "filter3")); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/groups/GroupOrJobLookupTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/groups/GroupOrJobLookupTests.java index 37f0805d81d08..a6c1f47f107f2 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/groups/GroupOrJobLookupTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/groups/GroupOrJobLookupTests.java @@ -53,8 +53,10 @@ public void testConstructor_GivenJobWithSameIdAsPreviousGroupName() { jobs.add(mockJob("foo", Arrays.asList("foo-group"))); jobs.add(mockJob("foo-group", Collections.emptyList())); ResourceAlreadyExistsException e = expectThrows(ResourceAlreadyExistsException.class, () -> new GroupOrJobLookup(jobs)); - assertThat(e.getMessage(), - equalTo("job and group names must be unique but job [foo-group] and group [foo-group] have the same name")); + assertThat( + e.getMessage(), + equalTo("job and group names must be unique but job [foo-group] and group [foo-group] have the same name") + ); } public void testConstructor_GivenGroupWithSameNameAsPreviousJobId() { @@ -62,8 +64,7 @@ public void testConstructor_GivenGroupWithSameNameAsPreviousJobId() { jobs.add(mockJob("foo", Collections.emptyList())); jobs.add(mockJob("foo-2", Arrays.asList("foo"))); ResourceAlreadyExistsException e = expectThrows(ResourceAlreadyExistsException.class, () -> new GroupOrJobLookup(jobs)); - assertThat(e.getMessage(), - equalTo("job and group names must be unique but job [foo] and group [foo] have the same name")); + assertThat(e.getMessage(), equalTo("job and group names must be unique but job [foo] and group [foo] have the same name")); } public void testLookup() { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/groups/GroupOrJobTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/groups/GroupOrJobTests.java index 896d284231cae..5377ab119e551 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/groups/GroupOrJobTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/groups/GroupOrJobTests.java @@ -8,7 +8,6 @@ import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.ml.job.config.Job; -import org.elasticsearch.xpack.core.ml.job.groups.GroupOrJob; import java.util.Arrays; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/persistence/ElasticsearchMappingsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/persistence/ElasticsearchMappingsTests.java index 559c0f1696245..207c6436e4e1b 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/persistence/ElasticsearchMappingsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/persistence/ElasticsearchMappingsTests.java @@ -10,6 +10,7 @@ import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonToken; + import org.elasticsearch.Version; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.admin.indices.mapping.put.PutMappingAction; @@ -61,27 +62,23 @@ import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; - public class ElasticsearchMappingsTests extends ESTestCase { // These are not reserved because they're Elasticsearch keywords, not // field names private static final List KEYWORDS = Arrays.asList( - ElasticsearchMappings.ANALYZER, - ElasticsearchMappings.COPY_TO, - ElasticsearchMappings.DYNAMIC, - ElasticsearchMappings.ENABLED, - ElasticsearchMappings.NESTED, - ElasticsearchMappings.PROPERTIES, - ElasticsearchMappings.TYPE, - ElasticsearchMappings.WHITESPACE, - SearchSourceBuilder.RUNTIME_MAPPINGS_FIELD.getPreferredName() + ElasticsearchMappings.ANALYZER, + ElasticsearchMappings.COPY_TO, + ElasticsearchMappings.DYNAMIC, + ElasticsearchMappings.ENABLED, + ElasticsearchMappings.NESTED, + ElasticsearchMappings.PROPERTIES, + ElasticsearchMappings.TYPE, + ElasticsearchMappings.WHITESPACE, + SearchSourceBuilder.RUNTIME_MAPPINGS_FIELD.getPreferredName() ); - private static final List INTERNAL_FIELDS = Arrays.asList( - GetResult._ID, - GetResult._INDEX - ); + private static final List INTERNAL_FIELDS = Arrays.asList(GetResult._ID, GetResult._INDEX); public void testResultsMappingReservedFields() throws Exception { Set overridden = new HashSet<>(KEYWORDS); @@ -154,7 +151,8 @@ public void testMappingRequiresUpdateRecentMappingVersion() { public void testMappingRequiresUpdateMaliciousMappingVersion() { ClusterState cs = getClusterStateWithMappingsWithMetadata( - Collections.singletonMap("version_current", Collections.singletonMap("nested", "1.0"))); + Collections.singletonMap("version_current", Collections.singletonMap("nested", "1.0")) + ); String[] indices = new String[] { "version_nested" }; assertArrayEquals(indices, ElasticsearchMappings.mappingRequiresUpdate(cs, indices, Version.CURRENT)); } @@ -174,23 +172,23 @@ public void testMappingRequiresUpdateNewerMappingVersion() { public void testMappingRequiresUpdateNewerMappingVersionMinor() { ClusterState cs = getClusterStateWithMappingsWithMetadata(Collections.singletonMap("version_newer_minor", Version.CURRENT)); String[] indices = new String[] { "version_newer_minor" }; - assertArrayEquals(new String[] {}, - ElasticsearchMappings.mappingRequiresUpdate(cs, indices, VersionUtils.getPreviousMinorVersion())); + assertArrayEquals( + new String[] {}, + ElasticsearchMappings.mappingRequiresUpdate(cs, indices, VersionUtils.getPreviousMinorVersion()) + ); } - @SuppressWarnings({"unchecked"}) + @SuppressWarnings({ "unchecked" }) public void testAddDocMappingIfMissing() { ThreadPool threadPool = mock(ThreadPool.class); when(threadPool.getThreadContext()).thenReturn(new ThreadContext(Settings.EMPTY)); Client client = mock(Client.class); when(client.threadPool()).thenReturn(threadPool); - doAnswer( - invocationOnMock -> { - ActionListener listener = (ActionListener) invocationOnMock.getArguments()[2]; - listener.onResponse(AcknowledgedResponse.TRUE); - return null; - }) - .when(client).execute(eq(PutMappingAction.INSTANCE), any(), any(ActionListener.class)); + doAnswer(invocationOnMock -> { + ActionListener listener = (ActionListener) invocationOnMock.getArguments()[2]; + listener.onResponse(AcknowledgedResponse.TRUE); + return null; + }).when(client).execute(eq(PutMappingAction.INSTANCE), any(), any(ActionListener.class)); ClusterState clusterState = getClusterStateWithMappingsWithMetadata(Collections.singletonMap("index-name", "0.0")); ElasticsearchMappings.addDocMappingIfMissing( @@ -199,10 +197,7 @@ public void testAddDocMappingIfMissing() { client, clusterState, MasterNodeRequest.DEFAULT_MASTER_NODE_TIMEOUT, - ActionListener.wrap( - ok -> assertTrue(ok), - e -> fail(e.toString()) - ) + ActionListener.wrap(ok -> assertTrue(ok), e -> fail(e.toString())) ); ArgumentCaptor requestCaptor = ArgumentCaptor.forClass(PutMappingRequest.class); @@ -224,8 +219,12 @@ private ClusterState getClusterStateWithMappingsWithMetadata(Map Object version = entry.getValue(); IndexMetadata.Builder indexMetadata = IndexMetadata.builder(indexName); - indexMetadata.settings(Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)); + indexMetadata.settings( + Settings.builder() + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + ); Map mapping = new HashMap<>(); Map properties = new HashMap<>(); @@ -252,13 +251,12 @@ private ClusterState getClusterStateWithMappingsWithMetadata(Map } private Set collectResultsDocFieldNames() throws IOException { - // Only the mappings for the results index should be added below. Do NOT add mappings for other indexes here. + // Only the mappings for the results index should be added below. Do NOT add mappings for other indexes here. return collectFieldNames(AnomalyDetectorsIndex.resultsMapping()); } private Set collectFieldNames(String mapping) throws IOException { - BufferedInputStream inputStream = - new BufferedInputStream(new ByteArrayInputStream(mapping.getBytes(StandardCharsets.UTF_8))); + BufferedInputStream inputStream = new BufferedInputStream(new ByteArrayInputStream(mapping.getBytes(StandardCharsets.UTF_8))); JsonParser parser = new JsonFactory().createParser(inputStream); Set fieldNames = new HashSet<>(); boolean isAfterPropertiesStart = false; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/CategorizerStatsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/CategorizerStatsTests.java index 4e8db35ca15af..1786d572bc36f 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/CategorizerStatsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/CategorizerStatsTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.xpack.core.ml.job.process.autodetect.state; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; public class CategorizerStatsTests extends AbstractSerializingTestCase { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/DataCountsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/DataCountsTests.java index ec717388d2e1c..a279d1e4566dd 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/DataCountsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/DataCountsTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ml.job.process.autodetect.state; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.time.Instant; import java.time.ZonedDateTime; @@ -19,13 +19,25 @@ public class DataCountsTests extends AbstractSerializingTestCase { public static DataCounts createTestInstance(String jobId) { - return new DataCounts(jobId, randomIntBetween(1, 1_000_000), - randomIntBetween(1, 1_000_000), randomIntBetween(1, 1_000_000), randomIntBetween(1, 1_000_000), - randomIntBetween(1, 1_000_000), randomIntBetween(1, 1_000_000), randomIntBetween(1, 1_000_000), - randomIntBetween(1, 1_000_000), randomIntBetween(1, 1_000_000), randomIntBetween(1, 1_000_000), - dateWithRandomTimeZone(), dateWithRandomTimeZone(), - dateWithRandomTimeZone(), dateWithRandomTimeZone(), - dateWithRandomTimeZone(), randomBoolean() ? Instant.now() : null); + return new DataCounts( + jobId, + randomIntBetween(1, 1_000_000), + randomIntBetween(1, 1_000_000), + randomIntBetween(1, 1_000_000), + randomIntBetween(1, 1_000_000), + randomIntBetween(1, 1_000_000), + randomIntBetween(1, 1_000_000), + randomIntBetween(1, 1_000_000), + randomIntBetween(1, 1_000_000), + randomIntBetween(1, 1_000_000), + randomIntBetween(1, 1_000_000), + dateWithRandomTimeZone(), + dateWithRandomTimeZone(), + dateWithRandomTimeZone(), + dateWithRandomTimeZone(), + dateWithRandomTimeZone(), + randomBoolean() ? Instant.now() : null + ); } private static Date dateWithRandomTimeZone() { @@ -113,22 +125,72 @@ public void testGetInputRecordCount() { } public void testCalcProcessedFieldCount() { - DataCounts counts = new DataCounts(randomAlphaOfLength(16), 10L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, new Date(), new Date(), - new Date(), new Date(), new Date(), Instant.now()); + DataCounts counts = new DataCounts( + randomAlphaOfLength(16), + 10L, + 0L, + 0L, + 0L, + 0L, + 0L, + 0L, + 0L, + 0L, + 0L, + new Date(), + new Date(), + new Date(), + new Date(), + new Date(), + Instant.now() + ); counts.calcProcessedFieldCount(3); assertEquals(30, counts.getProcessedFieldCount()); - counts = new DataCounts(randomAlphaOfLength(16), 10L, 0L, 0L, 0L, 0L, 5L, 0L, 0L, 0L, 0L, new Date(), new Date(), - new Date(), new Date(), new Date(), Instant.now()); + counts = new DataCounts( + randomAlphaOfLength(16), + 10L, + 0L, + 0L, + 0L, + 0L, + 5L, + 0L, + 0L, + 0L, + 0L, + new Date(), + new Date(), + new Date(), + new Date(), + new Date(), + Instant.now() + ); counts.calcProcessedFieldCount(3); assertEquals(25, counts.getProcessedFieldCount()); } public void testEquals() { DataCounts counts1 = new DataCounts( - randomAlphaOfLength(16), 10L, 5000L, 2000L, 300L, 6L, 15L, 0L, 0L, 0L, 0L, new Date(), new Date(1435000000L), - new Date(), new Date(), new Date(), Instant.now()); + randomAlphaOfLength(16), + 10L, + 5000L, + 2000L, + 300L, + 6L, + 15L, + 0L, + 0L, + 0L, + 0L, + new Date(), + new Date(1435000000L), + new Date(), + new Date(), + new Date(), + Instant.now() + ); DataCounts counts2 = new DataCounts(counts1); assertEquals(counts1, counts2); @@ -175,18 +237,43 @@ private void assertAllFieldsGreaterThanZero(DataCounts stats) throws Exception { } private static DataCounts createCounts( - long processedRecordCount, long processedFieldCount, long inputBytes, long inputFieldCount, - long invalidDateCount, long missingFieldCount, long outOfOrderTimeStampCount, - long emptyBucketCount, long sparseBucketCount, long bucketCount, - long earliestRecordTime, long latestRecordTime, long lastDataTimeStamp, long latestEmptyBucketTimeStamp, - long latestSparseBucketTimeStamp, long logTime) { - - DataCounts counts = new DataCounts("foo", processedRecordCount, processedFieldCount, inputBytes, - inputFieldCount, invalidDateCount, missingFieldCount, outOfOrderTimeStampCount, - emptyBucketCount, sparseBucketCount, bucketCount, - new Date(earliestRecordTime), new Date(latestRecordTime), - new Date(lastDataTimeStamp), new Date(latestEmptyBucketTimeStamp), new Date(latestSparseBucketTimeStamp), - Instant.ofEpochMilli(logTime)); + long processedRecordCount, + long processedFieldCount, + long inputBytes, + long inputFieldCount, + long invalidDateCount, + long missingFieldCount, + long outOfOrderTimeStampCount, + long emptyBucketCount, + long sparseBucketCount, + long bucketCount, + long earliestRecordTime, + long latestRecordTime, + long lastDataTimeStamp, + long latestEmptyBucketTimeStamp, + long latestSparseBucketTimeStamp, + long logTime + ) { + + DataCounts counts = new DataCounts( + "foo", + processedRecordCount, + processedFieldCount, + inputBytes, + inputFieldCount, + invalidDateCount, + missingFieldCount, + outOfOrderTimeStampCount, + emptyBucketCount, + sparseBucketCount, + bucketCount, + new Date(earliestRecordTime), + new Date(latestRecordTime), + new Date(lastDataTimeStamp), + new Date(latestEmptyBucketTimeStamp), + new Date(latestSparseBucketTimeStamp), + Instant.ofEpochMilli(logTime) + ); return counts; } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/ModelSizeStatsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/ModelSizeStatsTests.java index 1ddf260952ed3..4f1ad2b99f96b 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/ModelSizeStatsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/ModelSizeStatsTests.java @@ -8,9 +8,9 @@ import org.elasticsearch.common.io.stream.Writeable.Reader; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xpack.core.ml.job.process.autodetect.state.ModelSizeStats.MemoryStatus; import java.io.IOException; @@ -142,8 +142,10 @@ public void testId() { public void testStrictParser() throws IOException { String json = "{\"job_id\":\"job_1\", \"foo\":\"bar\"}"; try (XContentParser parser = createParser(JsonXContent.jsonXContent, json)) { - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> ModelSizeStats.STRICT_PARSER.apply(parser, null)); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> ModelSizeStats.STRICT_PARSER.apply(parser, null) + ); assertThat(e.getMessage(), containsString("unknown field [foo]")); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/ModelSnapshotTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/ModelSnapshotTests.java index f692b73950d5e..cfb4c43613b42 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/ModelSnapshotTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/ModelSnapshotTests.java @@ -9,9 +9,9 @@ import org.elasticsearch.Version; import org.elasticsearch.common.io.stream.Writeable.Reader; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.AbstractSerializingTestCase; import java.io.IOException; import java.util.Arrays; @@ -58,8 +58,7 @@ public void testEquals_GivenEqualModelSnapshots() { public void testEquals_GivenDifferentTimestamp() { ModelSnapshot modelSnapshot1 = createFullyPopulated().build(); - ModelSnapshot modelSnapshot2 = createFullyPopulated().setTimestamp( - new Date(modelSnapshot1.getTimestamp().getTime() + 1)).build(); + ModelSnapshot modelSnapshot2 = createFullyPopulated().setTimestamp(new Date(modelSnapshot1.getTimestamp().getTime() + 1)).build(); assertFalse(modelSnapshot1.equals(modelSnapshot2)); assertFalse(modelSnapshot2.equals(modelSnapshot1)); @@ -67,8 +66,7 @@ public void testEquals_GivenDifferentTimestamp() { public void testEquals_GivenDifferentDescription() { ModelSnapshot modelSnapshot1 = createFullyPopulated().build(); - ModelSnapshot modelSnapshot2 = createFullyPopulated() - .setDescription(modelSnapshot1.getDescription() + " blah").build(); + ModelSnapshot modelSnapshot2 = createFullyPopulated().setDescription(modelSnapshot1.getDescription() + " blah").build(); assertFalse(modelSnapshot1.equals(modelSnapshot2)); assertFalse(modelSnapshot2.equals(modelSnapshot1)); @@ -76,8 +74,7 @@ public void testEquals_GivenDifferentDescription() { public void testEquals_GivenDifferentId() { ModelSnapshot modelSnapshot1 = createFullyPopulated().build(); - ModelSnapshot modelSnapshot2 = createFullyPopulated() - .setSnapshotId(modelSnapshot1.getSnapshotId() + "_2").build(); + ModelSnapshot modelSnapshot2 = createFullyPopulated().setSnapshotId(modelSnapshot1.getSnapshotId() + "_2").build(); assertFalse(modelSnapshot1.equals(modelSnapshot2)); assertFalse(modelSnapshot2.equals(modelSnapshot1)); @@ -85,8 +82,7 @@ public void testEquals_GivenDifferentId() { public void testEquals_GivenDifferentDocCount() { ModelSnapshot modelSnapshot1 = createFullyPopulated().build(); - ModelSnapshot modelSnapshot2 = createFullyPopulated() - .setSnapshotDocCount(modelSnapshot1.getSnapshotDocCount() + 1).build(); + ModelSnapshot modelSnapshot2 = createFullyPopulated().setSnapshotDocCount(modelSnapshot1.getSnapshotDocCount() + 1).build(); assertFalse(modelSnapshot1.equals(modelSnapshot2)); assertFalse(modelSnapshot2.equals(modelSnapshot1)); @@ -104,9 +100,9 @@ public void testEquals_GivenDifferentModelSizeStats() { public void testEquals_GivenDifferentQuantiles() { ModelSnapshot modelSnapshot1 = createFullyPopulated().build(); - ModelSnapshot modelSnapshot2 = createFullyPopulated() - .setQuantiles(new Quantiles("foo", modelSnapshot1.getQuantiles().getTimestamp(), - "different state")).build(); + ModelSnapshot modelSnapshot2 = createFullyPopulated().setQuantiles( + new Quantiles("foo", modelSnapshot1.getQuantiles().getTimestamp(), "different state") + ).build(); assertFalse(modelSnapshot1.equals(modelSnapshot2)); assertFalse(modelSnapshot2.equals(modelSnapshot1)); @@ -115,7 +111,8 @@ public void testEquals_GivenDifferentQuantiles() { public void testEquals_GivenDifferentLatestResultTimestamp() { ModelSnapshot modelSnapshot1 = createFullyPopulated().build(); ModelSnapshot modelSnapshot2 = createFullyPopulated().setLatestResultTimeStamp( - new Date(modelSnapshot1.getLatestResultTimeStamp().getTime() + 1)).build(); + new Date(modelSnapshot1.getLatestResultTimeStamp().getTime() + 1) + ).build(); assertFalse(modelSnapshot1.equals(modelSnapshot2)); assertFalse(modelSnapshot2.equals(modelSnapshot1)); @@ -124,7 +121,8 @@ public void testEquals_GivenDifferentLatestResultTimestamp() { public void testEquals_GivenDifferentLatestRecordTimestamp() { ModelSnapshot modelSnapshot1 = createFullyPopulated().build(); ModelSnapshot modelSnapshot2 = createFullyPopulated().setLatestRecordTimeStamp( - new Date(modelSnapshot1.getLatestRecordTimeStamp().getTime() + 1)).build(); + new Date(modelSnapshot1.getLatestRecordTimeStamp().getTime() + 1) + ).build(); assertFalse(modelSnapshot1.equals(modelSnapshot2)); assertFalse(modelSnapshot2.equals(modelSnapshot1)); @@ -161,10 +159,8 @@ public static ModelSnapshot createRandomized() { modelSnapshot.setSnapshotId(randomAlphaOfLength(10)); modelSnapshot.setSnapshotDocCount(randomInt()); modelSnapshot.setModelSizeStats(ModelSizeStatsTests.createRandomized()); - modelSnapshot.setLatestResultTimeStamp( - new Date(TimeValue.parseTimeValue(randomTimeValue(), "test").millis())); - modelSnapshot.setLatestRecordTimeStamp( - new Date(TimeValue.parseTimeValue(randomTimeValue(), "test").millis())); + modelSnapshot.setLatestResultTimeStamp(new Date(TimeValue.parseTimeValue(randomTimeValue(), "test").millis())); + modelSnapshot.setLatestRecordTimeStamp(new Date(TimeValue.parseTimeValue(randomTimeValue(), "test").millis())); modelSnapshot.setQuantiles(QuantilesTests.createRandomized()); modelSnapshot.setRetain(randomBoolean()); return modelSnapshot.build(); @@ -197,15 +193,19 @@ public void testStateDocumentIds_GivenDocCountIsOne() { public void testStateDocumentIds_GivenDocCountIsThree() { ModelSnapshot snapshot = new ModelSnapshot.Builder("foo").setSnapshotId("123456789").setSnapshotDocCount(3).build(); - assertThat(snapshot.stateDocumentIds(), - equalTo(Arrays.asList("foo_model_state_123456789#1", "foo_model_state_123456789#2", "foo_model_state_123456789#3"))); + assertThat( + snapshot.stateDocumentIds(), + equalTo(Arrays.asList("foo_model_state_123456789#1", "foo_model_state_123456789#2", "foo_model_state_123456789#3")) + ); } public void testStrictParser() throws IOException { String json = "{\"foo\":\"bar\"}"; try (XContentParser parser = createParser(JsonXContent.jsonXContent, json)) { - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> ModelSnapshot.STRICT_PARSER.apply(parser, null)); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> ModelSnapshot.STRICT_PARSER.apply(parser, null) + ); assertThat(e.getMessage(), containsString("unknown field [foo]")); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/QuantilesTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/QuantilesTests.java index 027b80a107a89..9ce49efa68668 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/QuantilesTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/QuantilesTests.java @@ -8,9 +8,9 @@ import org.elasticsearch.common.io.stream.Writeable.Reader; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.AbstractSerializingTestCase; import java.io.IOException; import java.util.Date; @@ -48,13 +48,11 @@ public void testEquals_GivenSameObject() { assertTrue(quantiles.equals(quantiles)); } - public void testEquals_GivenDifferentClassObject() { Quantiles quantiles = new Quantiles("foo", new Date(0L), "foo"); assertFalse(quantiles.equals("not a quantiles object")); } - public void testEquals_GivenEqualQuantilesObject() { Quantiles quantiles1 = new Quantiles("foo", new Date(0L), "foo"); @@ -64,7 +62,6 @@ public void testEquals_GivenEqualQuantilesObject() { assertTrue(quantiles2.equals(quantiles1)); } - public void testEquals_GivenDifferentState() { Quantiles quantiles1 = new Quantiles("foo", new Date(0L), "bar1"); @@ -74,7 +71,6 @@ public void testEquals_GivenDifferentState() { assertFalse(quantiles2.equals(quantiles1)); } - public void testHashCode_GivenEqualObject() { Quantiles quantiles1 = new Quantiles("foo", new Date(0L), "foo"); @@ -95,9 +91,11 @@ protected Quantiles createTestInstance() { } public static Quantiles createRandomized() { - return new Quantiles(randomAlphaOfLengthBetween(1, 20), - new Date(TimeValue.parseTimeValue(randomTimeValue(), "test").millis()), - randomAlphaOfLengthBetween(0, 1000)); + return new Quantiles( + randomAlphaOfLengthBetween(1, 20), + new Date(TimeValue.parseTimeValue(randomTimeValue(), "test").millis()), + randomAlphaOfLengthBetween(0, 1000) + ); } @Override @@ -113,8 +111,7 @@ protected Quantiles doParseInstance(XContentParser parser) { public void testStrictParser() throws IOException { String json = "{\"job_id\":\"job_1\", \"timestamp\": 123456789, \"quantile_state\":\"...\", \"foo\":\"bar\"}"; try (XContentParser parser = createParser(JsonXContent.jsonXContent, json)) { - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> Quantiles.STRICT_PARSER.apply(parser, null)); + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> Quantiles.STRICT_PARSER.apply(parser, null)); assertThat(e.getMessage(), containsString("unknown field [foo]")); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/TimingStatsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/TimingStatsTests.java index bf65556a7ee91..3e9184fc852ba 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/TimingStatsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/TimingStatsTests.java @@ -7,9 +7,9 @@ package org.elasticsearch.xpack.core.ml.job.process.autodetect.state; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xpack.core.ml.utils.ExponentialAverageCalculationContext; import org.elasticsearch.xpack.core.ml.utils.ExponentialAverageCalculationContextTests; import org.elasticsearch.xpack.core.ml.utils.ToXContentParams; @@ -35,7 +35,8 @@ public static TimingStats createTestInstance(String jobId) { randomBoolean() ? null : randomDouble(), randomBoolean() ? null : randomDouble(), randomBoolean() ? null : randomDouble(), - ExponentialAverageCalculationContextTests.createRandom()); + ExponentialAverageCalculationContextTests.createRandom() + ); } @Override @@ -72,8 +73,11 @@ public void testDefaultConstructor() { } public void testConstructor() { - ExponentialAverageCalculationContext context = - new ExponentialAverageCalculationContext(78.9, Instant.ofEpochMilli(123456789), 987.0); + ExponentialAverageCalculationContext context = new ExponentialAverageCalculationContext( + 78.9, + Instant.ofEpochMilli(123456789), + 987.0 + ); TimingStats stats = new TimingStats(JOB_ID, 7, 1.0, 2.0, 1.23, 7.89, context); assertThat(stats.getJobId(), equalTo(JOB_ID)); @@ -87,8 +91,11 @@ public void testConstructor() { } public void testCopyConstructor() { - ExponentialAverageCalculationContext context = - new ExponentialAverageCalculationContext(78.9, Instant.ofEpochMilli(123456789), 987.0); + ExponentialAverageCalculationContext context = new ExponentialAverageCalculationContext( + 78.9, + Instant.ofEpochMilli(123456789), + 987.0 + ); TimingStats stats1 = new TimingStats(JOB_ID, 7, 1.0, 2.0, 1.23, 7.89, context); TimingStats stats2 = new TimingStats(stats1); @@ -168,10 +175,12 @@ && closeTo(operand.getTotalBucketProcessingTimeMs(), error).matches(item.getTota && closeTo(operand.getMinBucketProcessingTimeMs(), error).matches(item.getMinBucketProcessingTimeMs()) && closeTo(operand.getMaxBucketProcessingTimeMs(), error).matches(item.getMaxBucketProcessingTimeMs()) && closeTo(operand.getAvgBucketProcessingTimeMs(), error).matches(item.getAvgBucketProcessingTimeMs()) - && closeTo(operand.getExponentialAvgBucketProcessingTimeMs(), error) - .matches(item.getExponentialAvgBucketProcessingTimeMs()) - && closeTo(operand.getExponentialAvgBucketProcessingTimePerHourMs(), error) - .matches(item.getExponentialAvgBucketProcessingTimePerHourMs()); + && closeTo(operand.getExponentialAvgBucketProcessingTimeMs(), error).matches( + item.getExponentialAvgBucketProcessingTimeMs() + ) + && closeTo(operand.getExponentialAvgBucketProcessingTimePerHourMs(), error).matches( + item.getExponentialAvgBucketProcessingTimePerHourMs() + ); } }; } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/results/AnomalyCauseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/results/AnomalyCauseTests.java index f12a7e9c5d28a..41b4be1276783 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/results/AnomalyCauseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/results/AnomalyCauseTests.java @@ -8,9 +8,9 @@ import org.elasticsearch.client.ml.job.config.DetectorFunction; import org.elasticsearch.common.io.stream.Writeable.Reader; +import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.AbstractSerializingTestCase; import java.io.IOException; import java.util.ArrayList; @@ -102,8 +102,7 @@ protected AnomalyCause doParseInstance(XContentParser parser) { public void testStrictParser() throws IOException { String json = "{\"foo\":\"bar\"}"; try (XContentParser parser = createParser(JsonXContent.jsonXContent, json)) { - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> AnomalyCause.STRICT_PARSER.apply(parser, null)); + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> AnomalyCause.STRICT_PARSER.apply(parser, null)); assertThat(e.getMessage(), containsString("unknown field [foo]")); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/results/AnomalyRecordTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/results/AnomalyRecordTests.java index c4477b21bf032..26206c29aa0e3 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/results/AnomalyRecordTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/results/AnomalyRecordTests.java @@ -10,11 +10,11 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xcontent.XContentParseException; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xpack.core.ml.MachineLearningField; import org.elasticsearch.xpack.core.ml.utils.MlStrings; @@ -75,16 +75,16 @@ public AnomalyRecord createTestInstance(String jobId) { } if (randomBoolean()) { int count = randomIntBetween(0, 9); - List influences = new ArrayList<>(); - for (int i=0; i influences = new ArrayList<>(); + for (int i = 0; i < count; i++) { influences.add(new Influence(randomAlphaOfLength(8), Collections.singletonList(randomAlphaOfLengthBetween(1, 28)))); } anomalyRecord.setInfluencers(influences); } if (randomBoolean()) { int count = randomIntBetween(0, 9); - List causes = new ArrayList<>(); - for (int i=0; i causes = new ArrayList<>(); + for (int i = 0; i < count; i++) { causes.add(new AnomalyCauseTests().createTestInstance()); } anomalyRecord.setCauses(causes); @@ -133,7 +133,7 @@ public void testToXContentIncludesInputFields() throws IOException { assertEquals(influence2.getInfluencerFieldValues(), serialisedInfFieldValues2); } - @SuppressWarnings({"unchecked"}) + @SuppressWarnings({ "unchecked" }) public void testToXContentOrdersDuplicateInputFields() throws IOException { AnomalyRecord record = createTestInstance(); record.setByFieldName("car-make"); @@ -206,18 +206,17 @@ public void testStrictParser_IsLenientOnTopLevelFields() throws IOException { } public void testStrictParser_IsStrictOnNestedFields() throws IOException { - String json = "{\"job_id\":\"job_1\", \"timestamp\": 123544456, \"bucket_span\": 3600, \"foo\":\"bar\"," + - " \"causes\":[{\"cause_foo\":\"bar\"}]}"; + String json = "{\"job_id\":\"job_1\", \"timestamp\": 123544456, \"bucket_span\": 3600, \"foo\":\"bar\"," + + " \"causes\":[{\"cause_foo\":\"bar\"}]}"; try (XContentParser parser = createParser(JsonXContent.jsonXContent, json)) { - XContentParseException e = expectThrows(XContentParseException.class, - () -> AnomalyRecord.STRICT_PARSER.apply(parser, null)); + XContentParseException e = expectThrows(XContentParseException.class, () -> AnomalyRecord.STRICT_PARSER.apply(parser, null)); assertThat(e.getCause().getMessage(), containsString("[anomaly_cause] unknown field [cause_foo]")); } } public void testLenientParser() throws IOException { - String json = "{\"job_id\":\"job_1\", \"timestamp\": 123544456, \"bucket_span\": 3600, \"foo\":\"bar\"," + - " \"causes\":[{\"cause_foo\":\"bar\"}]}"; + String json = "{\"job_id\":\"job_1\", \"timestamp\": 123544456, \"bucket_span\": 3600, \"foo\":\"bar\"," + + " \"causes\":[{\"cause_foo\":\"bar\"}]}"; try (XContentParser parser = createParser(JsonXContent.jsonXContent, json)) { AnomalyRecord.LENIENT_PARSER.apply(parser, null); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/results/BucketInfluencerTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/results/BucketInfluencerTests.java index 600753675807c..738fbb76f469e 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/results/BucketInfluencerTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/results/BucketInfluencerTests.java @@ -7,9 +7,9 @@ package org.elasticsearch.xpack.core.ml.job.results; import org.elasticsearch.common.io.stream.Writeable.Reader; +import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.AbstractSerializingTestCase; import java.io.IOException; import java.util.Date; @@ -20,8 +20,11 @@ public class BucketInfluencerTests extends AbstractSerializingTestCase BucketInfluencer.STRICT_PARSER.apply(parser, null)); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> BucketInfluencer.STRICT_PARSER.apply(parser, null) + ); assertThat(e.getMessage(), containsString("unknown field [foo]")); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/results/GeoResultsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/results/GeoResultsTests.java index 4f26a36a71dec..806a8eb315d2f 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/results/GeoResultsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/results/GeoResultsTests.java @@ -7,9 +7,9 @@ package org.elasticsearch.xpack.core.ml.job.results; import org.elasticsearch.common.io.stream.Writeable.Reader; +import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.AbstractSerializingTestCase; import org.junit.Before; import java.io.IOException; @@ -28,12 +28,10 @@ public void setLenient() { static GeoResults createTestGeoResults() { GeoResults geoResults = new GeoResults(); if (randomBoolean()) { - geoResults.setActualPoint(randomDoubleBetween(-90.0, 90.0, true) + "," + - randomDoubleBetween(-90.0, 90.0, true)); + geoResults.setActualPoint(randomDoubleBetween(-90.0, 90.0, true) + "," + randomDoubleBetween(-90.0, 90.0, true)); } if (randomBoolean()) { - geoResults.setTypicalPoint(randomDoubleBetween(-90.0, 90.0, true) + "," + - randomDoubleBetween(-90.0, 90.0, true)); + geoResults.setTypicalPoint(randomDoubleBetween(-90.0, 90.0, true) + "," + randomDoubleBetween(-90.0, 90.0, true)); } return geoResults; } @@ -56,8 +54,7 @@ protected GeoResults doParseInstance(XContentParser parser) { public void testStrictParser() throws IOException { String json = "{\"foo\":\"bar\"}"; try (XContentParser parser = createParser(JsonXContent.jsonXContent, json)) { - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> GeoResults.STRICT_PARSER.apply(parser, null)); + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> GeoResults.STRICT_PARSER.apply(parser, null)); assertThat(e.getMessage(), containsString("unknown field [foo]")); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/results/InfluencerTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/results/InfluencerTests.java index 2501169c348f6..0326bb204bce0 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/results/InfluencerTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/results/InfluencerTests.java @@ -9,10 +9,10 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.Writeable.Reader; import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xpack.core.ml.MachineLearningField; import java.io.IOException; @@ -21,14 +21,20 @@ public class InfluencerTests extends AbstractSerializingTestCase { public Influencer createTestInstance(String jobId) { - Influencer influencer = new Influencer(jobId, randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20), - new Date(randomNonNegativeLong()), randomNonNegativeLong()); + Influencer influencer = new Influencer( + jobId, + randomAlphaOfLengthBetween(1, 20), + randomAlphaOfLengthBetween(1, 20), + new Date(randomNonNegativeLong()), + randomNonNegativeLong() + ); influencer.setInterim(randomBoolean()); influencer.setInfluencerScore(randomDouble()); influencer.setInitialInfluencerScore(randomDouble()); influencer.setProbability(randomDouble()); return influencer; } + @Override protected Influencer createTestInstance() { return createTestInstance(randomAlphaOfLengthBetween(1, 20)); @@ -70,8 +76,8 @@ public void testId() { } public void testLenientParser() throws IOException { - String json = "{\"job_id\":\"job_1\", \"timestamp\": 123544456, \"bucket_span\": 3600," + - "\"influencer_field_name\":\"foo_1\", \"influencer_field_value\": \"foo_2\", \"foo\":\"bar\"}"; + String json = "{\"job_id\":\"job_1\", \"timestamp\": 123544456, \"bucket_span\": 3600," + + "\"influencer_field_name\":\"foo_1\", \"influencer_field_value\": \"foo_2\", \"foo\":\"bar\"}"; try (XContentParser parser = createParser(JsonXContent.jsonXContent, json)) { Influencer.LENIENT_PARSER.apply(parser, null); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/notifications/AuditMessageTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/notifications/AuditMessageTests.java index 20b1439a9c647..d2a8f552d245c 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/notifications/AuditMessageTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/notifications/AuditMessageTests.java @@ -9,7 +9,6 @@ import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xpack.core.common.notifications.AbstractAuditMessage; - import static org.hamcrest.Matchers.equalTo; public abstract class AuditMessageTests extends AbstractXContentTestCase { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/stats/CountAccumulatorTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/stats/CountAccumulatorTests.java index 69c5f74f94ed7..bdfbe4b121784 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/stats/CountAccumulatorTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/stats/CountAccumulatorTests.java @@ -73,7 +73,7 @@ public void testFromTermsAggregation() { when(bucket2.getKeyAsString()).thenReturn("b"); when(bucket2.getDocCount()).thenReturn(33L); - List buckets = Arrays.asList(bucket1, bucket2); + List buckets = Arrays.asList(bucket1, bucket2); when(termsAggregation.getBuckets()).thenReturn(buckets); CountAccumulator accumulator = CountAccumulator.fromTermsAggregation(termsAggregation); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/stats/ForecastStatsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/stats/ForecastStatsTests.java index ceedd0c756022..67e053c9cb9cb 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/stats/ForecastStatsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/stats/ForecastStatsTests.java @@ -8,11 +8,11 @@ package org.elasticsearch.xpack.core.ml.stats; import org.elasticsearch.common.io.stream.Writeable.Reader; +import org.elasticsearch.test.AbstractWireSerializingTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.AbstractWireSerializingTestCase; import org.elasticsearch.xpack.core.ml.stats.ForecastStats.Fields; import java.io.IOException; @@ -237,8 +237,13 @@ protected Reader instanceReader() { } public ForecastStats createForecastStats(long minTotal, long maxTotal) { - ForecastStats forecastStats = new ForecastStats(randomLongBetween(minTotal, maxTotal), createStatsAccumulator(), - createStatsAccumulator(), createStatsAccumulator(), createCountAccumulator()); + ForecastStats forecastStats = new ForecastStats( + randomLongBetween(minTotal, maxTotal), + createStatsAccumulator(), + createStatsAccumulator(), + createStatsAccumulator(), + createCountAccumulator() + ); return forecastStats; } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/stats/StatsAccumulatorTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/stats/StatsAccumulatorTests.java index 69c0e74e75ec7..f265ac4300d96 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/stats/StatsAccumulatorTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/stats/StatsAccumulatorTests.java @@ -9,6 +9,7 @@ import org.elasticsearch.common.io.stream.Writeable.Reader; import org.elasticsearch.search.aggregations.metrics.Stats; import org.elasticsearch.test.AbstractWireSerializingTestCase; + import java.util.HashMap; import java.util.Map; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/utils/ExceptionsHelperTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/utils/ExceptionsHelperTests.java index ed691e7cd1c07..e5bd6672ae2a7 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/utils/ExceptionsHelperTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/utils/ExceptionsHelperTests.java @@ -19,11 +19,15 @@ public class ExceptionsHelperTests extends ESTestCase { public void testFindSearchExceptionRootCause_GivenWrappedSearchPhaseException() { - SearchPhaseExecutionException searchPhaseExecutionException = new SearchPhaseExecutionException("test-phase", - "partial shards failure", new ShardSearchFailure[] { new ShardSearchFailure(new ElasticsearchException("for the cause!")) }); + SearchPhaseExecutionException searchPhaseExecutionException = new SearchPhaseExecutionException( + "test-phase", + "partial shards failure", + new ShardSearchFailure[] { new ShardSearchFailure(new ElasticsearchException("for the cause!")) } + ); Throwable rootCauseException = ExceptionsHelper.findSearchExceptionRootCause( - new IndexCreationException("test-index", searchPhaseExecutionException)); + new IndexCreationException("test-index", searchPhaseExecutionException) + ); assertThat(rootCauseException.getMessage(), equalTo("for the cause!")); } @@ -37,7 +41,8 @@ public void testFindSearchExceptionRootCause_GivenWrapperException() { RuntimeException runtimeException = new RuntimeException("cause"); Throwable rootCauseException = ExceptionsHelper.findSearchExceptionRootCause( - new IndexCreationException("test-index", runtimeException)); + new IndexCreationException("test-index", runtimeException) + ); assertThat(rootCauseException.getMessage(), equalTo("cause")); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/utils/ExponentialAverageCalculationContextTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/utils/ExponentialAverageCalculationContextTests.java index 1c156ae8a0886..ee8cbb93ec315 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/utils/ExponentialAverageCalculationContextTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/utils/ExponentialAverageCalculationContextTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ml.utils; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.time.Instant; @@ -22,7 +22,8 @@ public static ExponentialAverageCalculationContext createRandom() { return new ExponentialAverageCalculationContext( randomDouble(), randomBoolean() ? Instant.now() : null, - randomBoolean() ? randomDouble() : null); + randomBoolean() ? randomDouble() : null + ); } @Override @@ -49,8 +50,11 @@ public void testDefaultConstructor() { } public void testConstructor() { - ExponentialAverageCalculationContext context = - new ExponentialAverageCalculationContext(1.23, Instant.ofEpochMilli(123456789), 4.56); + ExponentialAverageCalculationContext context = new ExponentialAverageCalculationContext( + 1.23, + Instant.ofEpochMilli(123456789), + 4.56 + ); assertThat(context.getIncrementalMetricValueMs(), equalTo(1.23)); assertThat(context.getLatestTimestamp(), equalTo(Instant.ofEpochMilli(123456789))); @@ -58,8 +62,11 @@ public void testConstructor() { } public void testCopyConstructor() { - ExponentialAverageCalculationContext context1 = - new ExponentialAverageCalculationContext(1.23, Instant.ofEpochMilli(123456789), 4.56); + ExponentialAverageCalculationContext context1 = new ExponentialAverageCalculationContext( + 1.23, + Instant.ofEpochMilli(123456789), + 4.56 + ); ExponentialAverageCalculationContext context2 = new ExponentialAverageCalculationContext(context1); assertThat(context2.getIncrementalMetricValueMs(), equalTo(1.23)); @@ -110,8 +117,11 @@ public void testExponentialAverageCalculation() { } public void testExponentialAverageCalculationOnWindowBoundary() { - ExponentialAverageCalculationContext context = - new ExponentialAverageCalculationContext(500.0, Instant.parse("2019-07-19T04:25:06.00Z"), 200.0); + ExponentialAverageCalculationContext context = new ExponentialAverageCalculationContext( + 500.0, + Instant.parse("2019-07-19T04:25:06.00Z"), + 200.0 + ); assertThat(context.getIncrementalMetricValueMs(), equalTo(500.0)); assertThat(context.getLatestTimestamp(), equalTo(Instant.parse("2019-07-19T04:25:06.00Z"))); assertThat(context.getPreviousExponentialAverageMs(), equalTo(200.0)); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/utils/MapHelperTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/utils/MapHelperTests.java index 3b4e7dcc395ad..d124a5ee23770 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/utils/MapHelperTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/utils/MapHelperTests.java @@ -30,7 +30,7 @@ public void testCollapseFields() { map.put("d", Collections.singletonMap("e", Collections.singletonMap("f", 2))); map.put("g.h.i", 3); { - assertThat(MapHelper.dotCollapse(map, Collections.emptyList()), is(anEmptyMap())); + assertThat(MapHelper.dotCollapse(map, Collections.emptyList()), is(anEmptyMap())); } { Map collapsed = MapHelper.dotCollapse(map, Arrays.asList("a.b.c", "d.e.f", "g.h.i", "m.i.s.s.i.n.g")); @@ -50,166 +50,155 @@ public void testAbsolutePathStringAsKey() { public void testSimplePath() { String path = "a.b.c.d"; - Map map = Collections.singletonMap("a", - Collections.singletonMap("b", - Collections.singletonMap("c", - Collections.singletonMap("d", 2)))); + Map map = Collections.singletonMap( + "a", + Collections.singletonMap("b", Collections.singletonMap("c", Collections.singletonMap("d", 2))) + ); assertThat(MapHelper.dig(path, map), equalTo(2)); - map = Collections.singletonMap("a", - Collections.singletonMap("b", - Collections.singletonMap("e", // Not part of path - Collections.singletonMap("d", 2)))); + map = Collections.singletonMap( + "a", + Collections.singletonMap( + "b", + Collections.singletonMap( + "e", // Not part of path + Collections.singletonMap("d", 2) + ) + ) + ); assertThat(MapHelper.dig(path, map), is(nullValue())); } public void testSimplePathReturningMap() { String path = "a.b.c"; - Map map = Collections.singletonMap("a", - Collections.singletonMap("b", - Collections.singletonMap("c", - Collections.singletonMap("d", 2)))); + Map map = Collections.singletonMap( + "a", + Collections.singletonMap("b", Collections.singletonMap("c", Collections.singletonMap("d", 2))) + ); assertThat(MapHelper.dig(path, map), equalTo(Collections.singletonMap("d", 2))); } public void testSimpleMixedPath() { String path = "a.b.c.d"; - Map map = Collections.singletonMap("a", - Collections.singletonMap("b.c", - Collections.singletonMap("d", 2))); + Map map = Collections.singletonMap("a", Collections.singletonMap("b.c", Collections.singletonMap("d", 2))); assertThat(MapHelper.dig(path, map), equalTo(2)); - map = Collections.singletonMap("a.b", - Collections.singletonMap("c", - Collections.singletonMap("d", 2))); + map = Collections.singletonMap("a.b", Collections.singletonMap("c", Collections.singletonMap("d", 2))); assertThat(MapHelper.dig(path, map), equalTo(2)); - map = Collections.singletonMap("a.b.c", - Collections.singletonMap("d", 2)); + map = Collections.singletonMap("a.b.c", Collections.singletonMap("d", 2)); assertThat(MapHelper.dig(path, map), equalTo(2)); - map = Collections.singletonMap("a", - Collections.singletonMap("b", - Collections.singletonMap("c.d", 2))); + map = Collections.singletonMap("a", Collections.singletonMap("b", Collections.singletonMap("c.d", 2))); assertThat(MapHelper.dig(path, map), equalTo(2)); - map = Collections.singletonMap("a", - Collections.singletonMap("b.c.d", 2)); + map = Collections.singletonMap("a", Collections.singletonMap("b.c.d", 2)); assertThat(MapHelper.dig(path, map), equalTo(2)); - map = Collections.singletonMap("a.b", - Collections.singletonMap("c.d", 2)); + map = Collections.singletonMap("a.b", Collections.singletonMap("c.d", 2)); assertThat(MapHelper.dig(path, map), equalTo(2)); - map = Collections.singletonMap("a", - Collections.singletonMap("b.foo", - Collections.singletonMap("d", 2))); + map = Collections.singletonMap("a", Collections.singletonMap("b.foo", Collections.singletonMap("d", 2))); assertThat(MapHelper.dig(path, map), is(nullValue())); - map = Collections.singletonMap("a", - Collections.singletonMap("b.c", - Collections.singletonMap("foo", 2))); + map = Collections.singletonMap("a", Collections.singletonMap("b.c", Collections.singletonMap("foo", 2))); assertThat(MapHelper.dig(path, map), is(nullValue())); - map = Collections.singletonMap("x", - Collections.singletonMap("b.c", - Collections.singletonMap("d", 2))); + map = Collections.singletonMap("x", Collections.singletonMap("b.c", Collections.singletonMap("d", 2))); assertThat(MapHelper.dig(path, map), is(nullValue())); } public void testSimpleMixedPathReturningMap() { String path = "a.b.c"; - Map map = Collections.singletonMap("a", - Collections.singletonMap("b.c", - Collections.singletonMap("d", 2))); + Map map = Collections.singletonMap("a", Collections.singletonMap("b.c", Collections.singletonMap("d", 2))); assertThat(MapHelper.dig(path, map), equalTo(Collections.singletonMap("d", 2))); - map = Collections.singletonMap("a", - Collections.singletonMap("b.foo", - Collections.singletonMap("d", 2))); + map = Collections.singletonMap("a", Collections.singletonMap("b.foo", Collections.singletonMap("d", 2))); assertThat(MapHelper.dig(path, map), is(nullValue())); - map = Collections.singletonMap("a", - Collections.singletonMap("b.not_c", - Collections.singletonMap("foo", 2))); + map = Collections.singletonMap("a", Collections.singletonMap("b.not_c", Collections.singletonMap("foo", 2))); assertThat(MapHelper.dig(path, map), is(nullValue())); - map = Collections.singletonMap("x", - Collections.singletonMap("b.c", - Collections.singletonMap("d", 2))); + map = Collections.singletonMap("x", Collections.singletonMap("b.c", Collections.singletonMap("d", 2))); assertThat(MapHelper.dig(path, map), is(nullValue())); } public void testMultiplePotentialPaths() { String path = "a.b.c.d"; - Map map = new LinkedHashMap<>() {{ - put("a", Collections.singletonMap("b", - Collections.singletonMap("c", - Collections.singletonMap("not_d", 5)))); - put("a.b", Collections.singletonMap("c", Collections.singletonMap("d", 2))); - }}; + Map map = new LinkedHashMap<>() { + { + put("a", Collections.singletonMap("b", Collections.singletonMap("c", Collections.singletonMap("not_d", 5)))); + put("a.b", Collections.singletonMap("c", Collections.singletonMap("d", 2))); + } + }; assertThat(MapHelper.dig(path, map), equalTo(2)); - map = new LinkedHashMap<>() {{ - put("a", Collections.singletonMap("b", - Collections.singletonMap("c", - Collections.singletonMap("d", 2)))); - put("a.b", Collections.singletonMap("c", Collections.singletonMap("not_d", 5))); - }}; + map = new LinkedHashMap<>() { + { + put("a", Collections.singletonMap("b", Collections.singletonMap("c", Collections.singletonMap("d", 2)))); + put("a.b", Collections.singletonMap("c", Collections.singletonMap("not_d", 5))); + } + }; assertThat(MapHelper.dig(path, map), equalTo(2)); - map = new LinkedHashMap<>() {{ - put("a", Collections.singletonMap("b", - new HashMap<>() {{ - put("c", Collections.singletonMap("not_d", 5)); - put("c.d", 2); - }})); - }}; + map = new LinkedHashMap<>() { + { + put("a", Collections.singletonMap("b", new HashMap<>() { + { + put("c", Collections.singletonMap("not_d", 5)); + put("c.d", 2); + } + })); + } + }; assertThat(MapHelper.dig(path, map), equalTo(2)); - map = new LinkedHashMap<>() {{ - put("a", Collections.singletonMap("b", - new HashMap<>() {{ - put("c", Collections.singletonMap("d", 2)); - put("c.not_d", 5); - }})); - }}; + map = new LinkedHashMap<>() { + { + put("a", Collections.singletonMap("b", new HashMap<>() { + { + put("c", Collections.singletonMap("d", 2)); + put("c.not_d", 5); + } + })); + } + }; assertThat(MapHelper.dig(path, map), equalTo(2)); - map = new LinkedHashMap<>() {{ - put("a", Collections.singletonMap("b", - Collections.singletonMap("c", - Collections.singletonMap("not_d", 5)))); - put("a.b", Collections.singletonMap("c", Collections.singletonMap("not_d", 2))); - }}; + map = new LinkedHashMap<>() { + { + put("a", Collections.singletonMap("b", Collections.singletonMap("c", Collections.singletonMap("not_d", 5)))); + put("a.b", Collections.singletonMap("c", Collections.singletonMap("not_d", 2))); + } + }; assertThat(MapHelper.dig(path, map), is(nullValue())); } public void testMultiplePotentialPathsReturningMap() { String path = "a.b.c"; - Map map = new LinkedHashMap<>() {{ - put("a", Collections.singletonMap("b", - Collections.singletonMap("c", - Collections.singletonMap("d", 2)))); - put("a.b", Collections.singletonMap("not_c", Collections.singletonMap("d", 2))); - }}; + Map map = new LinkedHashMap<>() { + { + put("a", Collections.singletonMap("b", Collections.singletonMap("c", Collections.singletonMap("d", 2)))); + put("a.b", Collections.singletonMap("not_c", Collections.singletonMap("d", 2))); + } + }; assertThat(MapHelper.dig(path, map), equalTo(Collections.singletonMap("d", 2))); - map = new LinkedHashMap<>() {{ - put("a", Collections.singletonMap("b", - Collections.singletonMap("not_c", - Collections.singletonMap("d", 2)))); - put("a.b", Collections.singletonMap("c", Collections.singletonMap("d", 2))); - }}; + map = new LinkedHashMap<>() { + { + put("a", Collections.singletonMap("b", Collections.singletonMap("not_c", Collections.singletonMap("d", 2)))); + put("a.b", Collections.singletonMap("c", Collections.singletonMap("d", 2))); + } + }; assertThat(MapHelper.dig(path, map), equalTo(Collections.singletonMap("d", 2))); - map = new LinkedHashMap<>() {{ - put("a", Collections.singletonMap("b", - Collections.singletonMap("not_c", - Collections.singletonMap("d", 2)))); - put("a.b", Collections.singletonMap("not_c", Collections.singletonMap("d", 2))); - }}; + map = new LinkedHashMap<>() { + { + put("a", Collections.singletonMap("b", Collections.singletonMap("not_c", Collections.singletonMap("d", 2)))); + put("a.b", Collections.singletonMap("not_c", Collections.singletonMap("d", 2))); + } + }; assertThat(MapHelper.dig(path, map), is(nullValue())); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/utils/MlIndexAndAliasTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/utils/MlIndexAndAliasTests.java index b1e9b2c2c797c..b6f5e43f4e089 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/utils/MlIndexAndAliasTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/utils/MlIndexAndAliasTests.java @@ -99,8 +99,9 @@ public void setUpMocks() { when(threadPool.getThreadContext()).thenReturn(new ThreadContext(Settings.EMPTY)); indicesAdminClient = mock(IndicesAdminClient.class); - when(indicesAdminClient.prepareCreate(FIRST_CONCRETE_INDEX)) - .thenReturn(new CreateIndexRequestBuilder(client, CreateIndexAction.INSTANCE, FIRST_CONCRETE_INDEX)); + when(indicesAdminClient.prepareCreate(FIRST_CONCRETE_INDEX)).thenReturn( + new CreateIndexRequestBuilder(client, CreateIndexAction.INSTANCE, FIRST_CONCRETE_INDEX) + ); doAnswer(withResponse(new CreateIndexResponse(true, true, FIRST_CONCRETE_INDEX))).when(indicesAdminClient).create(any(), any()); when(indicesAdminClient.prepareAliases()).thenReturn(new IndicesAliasesRequestBuilder(client, IndicesAliasesAction.INSTANCE)); doAnswer(withResponse(AcknowledgedResponse.TRUE)).when(indicesAdminClient).aliases(any(), any()); @@ -124,8 +125,12 @@ public void setUpMocks() { ActionListener listener = (ActionListener) invocationOnMock.getArguments()[2]; listener.onResponse(AcknowledgedResponse.TRUE); return null; - }).when(client).execute(any(PutComposableIndexTemplateAction.class), any(PutComposableIndexTemplateAction.Request.class), - any(ActionListener.class)); + }).when(client) + .execute( + any(PutComposableIndexTemplateAction.class), + any(PutComposableIndexTemplateAction.Request.class), + any(ActionListener.class) + ); listener = mock(ActionListener.class); @@ -139,37 +144,78 @@ public void verifyNoMoreInteractionsWithMocks() { } public void testInstallIndexTemplateIfRequired_GivenLegacyTemplateExistsAndModernCluster() throws UnknownHostException { - ClusterState clusterState = createClusterState(Version.CURRENT, Collections.emptyMap(), - Collections.singletonMap(NotificationsIndex.NOTIFICATIONS_INDEX, - createLegacyIndexTemplateMetaData(NotificationsIndex.NOTIFICATIONS_INDEX, - Collections.singletonList(NotificationsIndex.NOTIFICATIONS_INDEX))), - Collections.emptyMap()); - - IndexTemplateConfig notificationsTemplate = new IndexTemplateConfig(NotificationsIndex.NOTIFICATIONS_INDEX, - "/org/elasticsearch/xpack/core/ml/notifications_index_template.json", Version.CURRENT.id, "xpack.ml.version", - Map.of("xpack.ml.version.id", String.valueOf(Version.CURRENT.id), - "xpack.ml.notifications.mappings", NotificationsIndex.mapping())); - - MlIndexAndAlias.installIndexTemplateIfRequired(clusterState, client, - notificationsTemplate, TimeValue.timeValueMinutes(1), listener); + ClusterState clusterState = createClusterState( + Version.CURRENT, + Collections.emptyMap(), + Collections.singletonMap( + NotificationsIndex.NOTIFICATIONS_INDEX, + createLegacyIndexTemplateMetaData( + NotificationsIndex.NOTIFICATIONS_INDEX, + Collections.singletonList(NotificationsIndex.NOTIFICATIONS_INDEX) + ) + ), + Collections.emptyMap() + ); + + IndexTemplateConfig notificationsTemplate = new IndexTemplateConfig( + NotificationsIndex.NOTIFICATIONS_INDEX, + "/org/elasticsearch/xpack/core/ml/notifications_index_template.json", + Version.CURRENT.id, + "xpack.ml.version", + Map.of( + "xpack.ml.version.id", + String.valueOf(Version.CURRENT.id), + "xpack.ml.notifications.mappings", + NotificationsIndex.mapping() + ) + ); + + MlIndexAndAlias.installIndexTemplateIfRequired( + clusterState, + client, + notificationsTemplate, + TimeValue.timeValueMinutes(1), + listener + ); InOrder inOrder = inOrder(client, listener); inOrder.verify(client).execute(same(PutComposableIndexTemplateAction.INSTANCE), any(), any()); inOrder.verify(listener).onResponse(true); } public void testInstallIndexTemplateIfRequired_GivenComposableTemplateExists() throws UnknownHostException { - ClusterState clusterState = createClusterState(Version.CURRENT, Collections.emptyMap(), Collections.emptyMap(), - Collections.singletonMap(NotificationsIndex.NOTIFICATIONS_INDEX, - createComposableIndexTemplateMetaData(NotificationsIndex.NOTIFICATIONS_INDEX, - Collections.singletonList(NotificationsIndex.NOTIFICATIONS_INDEX)))); - - IndexTemplateConfig notificationsTemplate = new IndexTemplateConfig(NotificationsIndex.NOTIFICATIONS_INDEX, - "/org/elasticsearch/xpack/core/ml/notifications_index_template.json", Version.CURRENT.id, "xpack.ml.version", - Map.of("xpack.ml.version.id", String.valueOf(Version.CURRENT.id), - "xpack.ml.notifications.mappings", NotificationsIndex.mapping())); - - MlIndexAndAlias.installIndexTemplateIfRequired(clusterState, client, - notificationsTemplate, TimeValue.timeValueMinutes(1), listener); + ClusterState clusterState = createClusterState( + Version.CURRENT, + Collections.emptyMap(), + Collections.emptyMap(), + Collections.singletonMap( + NotificationsIndex.NOTIFICATIONS_INDEX, + createComposableIndexTemplateMetaData( + NotificationsIndex.NOTIFICATIONS_INDEX, + Collections.singletonList(NotificationsIndex.NOTIFICATIONS_INDEX) + ) + ) + ); + + IndexTemplateConfig notificationsTemplate = new IndexTemplateConfig( + NotificationsIndex.NOTIFICATIONS_INDEX, + "/org/elasticsearch/xpack/core/ml/notifications_index_template.json", + Version.CURRENT.id, + "xpack.ml.version", + Map.of( + "xpack.ml.version.id", + String.valueOf(Version.CURRENT.id), + "xpack.ml.notifications.mappings", + NotificationsIndex.mapping() + ) + ); + + MlIndexAndAlias.installIndexTemplateIfRequired( + clusterState, + client, + notificationsTemplate, + TimeValue.timeValueMinutes(1), + listener + ); verify(listener).onResponse(true); verifyNoMoreInteractions(client); } @@ -177,13 +223,26 @@ public void testInstallIndexTemplateIfRequired_GivenComposableTemplateExists() t public void testInstallIndexTemplateIfRequired() throws UnknownHostException { ClusterState clusterState = createClusterState(Collections.emptyMap()); - IndexTemplateConfig notificationsTemplate = new IndexTemplateConfig(NotificationsIndex.NOTIFICATIONS_INDEX, - "/org/elasticsearch/xpack/core/ml/notifications_index_template.json", Version.CURRENT.id, "xpack.ml.version", - Map.of("xpack.ml.version.id", String.valueOf(Version.CURRENT.id), - "xpack.ml.notifications.mappings", NotificationsIndex.mapping())); - - MlIndexAndAlias.installIndexTemplateIfRequired(clusterState, client, - notificationsTemplate, TimeValue.timeValueMinutes(1), listener); + IndexTemplateConfig notificationsTemplate = new IndexTemplateConfig( + NotificationsIndex.NOTIFICATIONS_INDEX, + "/org/elasticsearch/xpack/core/ml/notifications_index_template.json", + Version.CURRENT.id, + "xpack.ml.version", + Map.of( + "xpack.ml.version.id", + String.valueOf(Version.CURRENT.id), + "xpack.ml.notifications.mappings", + NotificationsIndex.mapping() + ) + ); + + MlIndexAndAlias.installIndexTemplateIfRequired( + clusterState, + client, + notificationsTemplate, + TimeValue.timeValueMinutes(1), + listener + ); InOrder inOrder = inOrder(client, listener); inOrder.verify(client).execute(same(PutComposableIndexTemplateAction.INSTANCE), any(), any()); inOrder.verify(listener).onResponse(true); @@ -223,11 +282,10 @@ public void testCreateStateIndexAndAliasIfNecessary_WriteAliasAlreadyExistsAndPo assertNoClientInteractionsWhenWriteAliasAlreadyExists("dummy-index"); } - public void testCreateStateIndexAndAliasIfNecessary_WriteAliasAlreadyExistsAndPointsAtLegacyStateIndex() - throws UnknownHostException { - ClusterState clusterState = - createClusterState( - Collections.singletonMap(LEGACY_INDEX_WITHOUT_SUFFIX, createIndexMetadataWithAlias(LEGACY_INDEX_WITHOUT_SUFFIX))); + public void testCreateStateIndexAndAliasIfNecessary_WriteAliasAlreadyExistsAndPointsAtLegacyStateIndex() throws UnknownHostException { + ClusterState clusterState = createClusterState( + Collections.singletonMap(LEGACY_INDEX_WITHOUT_SUFFIX, createIndexMetadataWithAlias(LEGACY_INDEX_WITHOUT_SUFFIX)) + ); createIndexAndAliasIfNecessary(clusterState); InOrder inOrder = inOrder(indicesAdminClient, listener); @@ -246,14 +304,16 @@ public void testCreateStateIndexAndAliasIfNecessary_WriteAliasAlreadyExistsAndPo indicesAliasesRequest.getAliasActions(), contains( AliasActions.add().alias(TEST_INDEX_ALIAS).index(FIRST_CONCRETE_INDEX).isHidden(true), - AliasActions.remove().alias(TEST_INDEX_ALIAS).index(LEGACY_INDEX_WITHOUT_SUFFIX))); + AliasActions.remove().alias(TEST_INDEX_ALIAS).index(LEGACY_INDEX_WITHOUT_SUFFIX) + ) + ); } private void assertMlStateWriteAliasAddedToMostRecentMlStateIndex(List existingIndexNames, String expectedWriteIndexName) throws UnknownHostException { - ClusterState clusterState = - createClusterState( - existingIndexNames.stream().collect(toMap(Function.identity(), MlIndexAndAliasTests::createIndexMetadata))); + ClusterState clusterState = createClusterState( + existingIndexNames.stream().collect(toMap(Function.identity(), MlIndexAndAliasTests::createIndexMetadata)) + ); createIndexAndAliasIfNecessary(clusterState); InOrder inOrder = inOrder(indicesAdminClient, listener); @@ -264,28 +324,30 @@ private void assertMlStateWriteAliasAddedToMostRecentMlStateIndex(List e IndicesAliasesRequest indicesAliasesRequest = aliasesRequestCaptor.getValue(); assertThat( indicesAliasesRequest.getAliasActions(), - contains(AliasActions.add().alias(TEST_INDEX_ALIAS).index(expectedWriteIndexName).isHidden(true))); + contains(AliasActions.add().alias(TEST_INDEX_ALIAS).index(expectedWriteIndexName).isHidden(true)) + ); } public void testCreateStateIndexAndAliasIfNecessary_WriteAliasDoesNotExistButInitialStateIndexExists() throws UnknownHostException { - assertMlStateWriteAliasAddedToMostRecentMlStateIndex( - Arrays.asList(FIRST_CONCRETE_INDEX), FIRST_CONCRETE_INDEX); + assertMlStateWriteAliasAddedToMostRecentMlStateIndex(Arrays.asList(FIRST_CONCRETE_INDEX), FIRST_CONCRETE_INDEX); } public void testCreateStateIndexAndAliasIfNecessary_WriteAliasDoesNotExistButSubsequentStateIndicesExist() throws UnknownHostException { - assertMlStateWriteAliasAddedToMostRecentMlStateIndex( - Arrays.asList("test-000003", "test-000040", "test-000500"), "test-000500"); + assertMlStateWriteAliasAddedToMostRecentMlStateIndex(Arrays.asList("test-000003", "test-000040", "test-000500"), "test-000500"); } public void testCreateStateIndexAndAliasIfNecessary_WriteAliasDoesNotExistButBothLegacyAndNewIndicesExist() throws UnknownHostException { assertMlStateWriteAliasAddedToMostRecentMlStateIndex( - Arrays.asList(LEGACY_INDEX_WITHOUT_SUFFIX, "test-000003", "test-000040", "test-000500"), "test-000500"); + Arrays.asList(LEGACY_INDEX_WITHOUT_SUFFIX, "test-000003", "test-000040", "test-000500"), + "test-000500" + ); } public void testCreateStateIndexAndAliasIfNecessary_WriteAliasDoesNotExistButLegacyStateIndexExists() throws UnknownHostException { - ClusterState clusterState = - createClusterState(Collections.singletonMap(LEGACY_INDEX_WITHOUT_SUFFIX, createIndexMetadata(LEGACY_INDEX_WITHOUT_SUFFIX))); + ClusterState clusterState = createClusterState( + Collections.singletonMap(LEGACY_INDEX_WITHOUT_SUFFIX, createIndexMetadata(LEGACY_INDEX_WITHOUT_SUFFIX)) + ); createIndexAndAliasIfNecessary(clusterState); InOrder inOrder = inOrder(indicesAdminClient, listener); @@ -300,33 +362,25 @@ public void testCreateStateIndexAndAliasIfNecessary_WriteAliasDoesNotExistButLeg public void testIndexNameComparator() { Comparator comparator = MlIndexAndAlias.INDEX_NAME_COMPARATOR; - assertThat( - Stream.of("test-000001").max(comparator).get(), - equalTo("test-000001")); - assertThat( - Stream.of("test-000002", "test-000001").max(comparator).get(), - equalTo("test-000002")); - assertThat( - Stream.of("test-000003", "test-000040", "test-000500").max(comparator).get(), - equalTo("test-000500")); - assertThat( - Stream.of("test-000042", "test-000049", "test-000038").max(comparator).get(), - equalTo("test-000049")); - assertThat( - Stream.of("test", "test-000003", "test-000040", "test-000500").max(comparator).get(), - equalTo("test-000500")); - assertThat( - Stream.of(".reindexed-6-test", "test-000042").max(comparator).get(), - equalTo("test-000042")); - assertThat( - Stream.of(".a-000002", ".b-000001").max(comparator).get(), - equalTo(".a-000002")); + assertThat(Stream.of("test-000001").max(comparator).get(), equalTo("test-000001")); + assertThat(Stream.of("test-000002", "test-000001").max(comparator).get(), equalTo("test-000002")); + assertThat(Stream.of("test-000003", "test-000040", "test-000500").max(comparator).get(), equalTo("test-000500")); + assertThat(Stream.of("test-000042", "test-000049", "test-000038").max(comparator).get(), equalTo("test-000049")); + assertThat(Stream.of("test", "test-000003", "test-000040", "test-000500").max(comparator).get(), equalTo("test-000500")); + assertThat(Stream.of(".reindexed-6-test", "test-000042").max(comparator).get(), equalTo("test-000042")); + assertThat(Stream.of(".a-000002", ".b-000001").max(comparator).get(), equalTo(".a-000002")); } private void createIndexAndAliasIfNecessary(ClusterState clusterState) { MlIndexAndAlias.createIndexAndAliasIfNecessary( - client, clusterState, TestIndexNameExpressionResolver.newInstance(), - TEST_INDEX_PREFIX, TEST_INDEX_ALIAS, MasterNodeRequest.DEFAULT_MASTER_NODE_TIMEOUT, listener); + client, + clusterState, + TestIndexNameExpressionResolver.newInstance(), + TEST_INDEX_PREFIX, + TEST_INDEX_ALIAS, + MasterNodeRequest.DEFAULT_MASTER_NODE_TIMEOUT, + listener + ); } @SuppressWarnings("unchecked") @@ -342,22 +396,28 @@ private static ClusterState createClusterState(Map indice return createClusterState(Version.CURRENT, indices, Collections.emptyMap(), Collections.emptyMap()); } - private static ClusterState createClusterState(Version minNodeVersion, - Map indices, - Map legacyTemplates, - Map composableTemplates) throws UnknownHostException { - InetAddress inetAddress1 = InetAddress.getByAddress(new byte[]{(byte) 192, (byte) 168, (byte) 0, (byte) 1}); - InetAddress inetAddress2 = InetAddress.getByAddress(new byte[]{(byte) 192, (byte) 168, (byte) 0, (byte) 2}); + private static ClusterState createClusterState( + Version minNodeVersion, + Map indices, + Map legacyTemplates, + Map composableTemplates + ) throws UnknownHostException { + InetAddress inetAddress1 = InetAddress.getByAddress(new byte[] { (byte) 192, (byte) 168, (byte) 0, (byte) 1 }); + InetAddress inetAddress2 = InetAddress.getByAddress(new byte[] { (byte) 192, (byte) 168, (byte) 0, (byte) 2 }); return ClusterState.builder(ClusterName.DEFAULT) - .nodes(DiscoveryNodes.builder() - .add(new DiscoveryNode("foo", new TransportAddress(inetAddress1, 9201), Version.CURRENT)) - .add(new DiscoveryNode("bar", new TransportAddress(inetAddress2, 9202), minNodeVersion)) - .build()) - .metadata(Metadata.builder() - .indices(ImmutableOpenMap.builder().putAll(indices).build()) - .templates(ImmutableOpenMap.builder().putAll(legacyTemplates).build()) - .indexTemplates(composableTemplates) - .build()) + .nodes( + DiscoveryNodes.builder() + .add(new DiscoveryNode("foo", new TransportAddress(inetAddress1, 9201), Version.CURRENT)) + .add(new DiscoveryNode("bar", new TransportAddress(inetAddress2, 9202), minNodeVersion)) + .build() + ) + .metadata( + Metadata.builder() + .indices(ImmutableOpenMap.builder().putAll(indices).build()) + .templates(ImmutableOpenMap.builder().putAll(legacyTemplates).build()) + .indexTemplates(composableTemplates) + .build() + ) .build(); } @@ -378,14 +438,12 @@ private static ComposableIndexTemplate createComposableIndexTemplateMetaData(Str } private static IndexMetadata createIndexMetadata(String indexName, boolean withAlias) { - Settings settings = - Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .build(); - IndexMetadata.Builder builder = IndexMetadata.builder(indexName) - .settings(settings); + Settings settings = Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .build(); + IndexMetadata.Builder builder = IndexMetadata.builder(indexName).settings(settings); if (withAlias) { builder.putAlias(AliasMetadata.builder(TEST_INDEX_ALIAS).build()); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/utils/NamedXContentObjectHelperTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/utils/NamedXContentObjectHelperTests.java index fcb566913a1f7..baa4f59d27d1a 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/utils/NamedXContentObjectHelperTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/utils/NamedXContentObjectHelperTests.java @@ -8,16 +8,16 @@ import org.elasticsearch.client.ml.inference.NamedXContentObject; import org.elasticsearch.client.ml.inference.NamedXContentObjectHelper; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.search.SearchModule; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.search.SearchModule; -import org.elasticsearch.test.ESTestCase; import java.io.IOException; import java.util.ArrayList; @@ -32,8 +32,7 @@ public class NamedXContentObjectHelperTests extends ESTestCase { static class NamedTestObject implements NamedXContentObject { private String fieldValue; - public static final ObjectParser PARSER = - new ObjectParser<>("my_named_object", true, NamedTestObject::new); + public static final ObjectParser PARSER = new ObjectParser<>("my_named_object", true, NamedTestObject::new); static { PARSER.declareString(NamedTestObject::setFieldValue, new ParseField("my_field")); } @@ -92,9 +91,15 @@ public void testSerialize() throws IOException { @Override protected NamedXContentRegistry xContentRegistry() { List namedXContent = new ArrayList<>(); - namedXContent.addAll(Collections.singletonList(new NamedXContentRegistry.Entry(NamedXContentObject.class, - new ParseField("my_named_object"), - (p, c) -> NamedTestObject.PARSER.apply(p, null)))); + namedXContent.addAll( + Collections.singletonList( + new NamedXContentRegistry.Entry( + NamedXContentObject.class, + new ParseField("my_named_object"), + (p, c) -> NamedTestObject.PARSER.apply(p, null) + ) + ) + ); namedXContent.addAll(new SearchModule(Settings.EMPTY, Collections.emptyList()).getNamedXContents()); return new NamedXContentRegistry(namedXContent); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/utils/PhaseProgressTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/utils/PhaseProgressTests.java index ed7db06095edf..489490dd2305b 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/utils/PhaseProgressTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/utils/PhaseProgressTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.ml.utils; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/utils/QueryProviderTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/utils/QueryProviderTests.java index 413180b29d121..3bc49b9137eb1 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/utils/QueryProviderTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/utils/QueryProviderTests.java @@ -10,11 +10,6 @@ import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.DeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; @@ -22,6 +17,11 @@ import org.elasticsearch.rest.RestStatus; import org.elasticsearch.search.SearchModule; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.DeprecationHandler; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.ml.job.messages.Messages; import java.io.IOException; @@ -30,7 +30,6 @@ import static org.hamcrest.Matchers.equalTo; - public class QueryProviderTests extends AbstractSerializingTestCase { @Override @@ -70,22 +69,23 @@ public static QueryProvider createRandomValidQueryProvider() { } public static QueryProvider createRandomValidQueryProvider(String field, String value) { - Map terms = Collections.singletonMap(BoolQueryBuilder.NAME, - Collections.singletonMap("filter", - Collections.singletonList( - Collections.singletonMap(TermQueryBuilder.NAME, - Collections.singletonMap(field, value))))); - return new QueryProvider( - terms, - QueryBuilders.boolQuery().filter(QueryBuilders.termQuery(field, value)), - null); + Map terms = Collections.singletonMap( + BoolQueryBuilder.NAME, + Collections.singletonMap( + "filter", + Collections.singletonList(Collections.singletonMap(TermQueryBuilder.NAME, Collections.singletonMap(field, value))) + ) + ); + return new QueryProvider(terms, QueryBuilders.boolQuery().filter(QueryBuilders.termQuery(field, value)), null); } public void testEmptyQueryMap() throws IOException { XContentParser parser = XContentFactory.xContent(XContentType.JSON) .createParser(xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, "{}"); - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> QueryProvider.fromXContent(parser, false, Messages.DATAFEED_CONFIG_QUERY_BAD_FORMAT)); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> QueryProvider.fromXContent(parser, false, Messages.DATAFEED_CONFIG_QUERY_BAD_FORMAT) + ); assertThat(e.status(), equalTo(RestStatus.BAD_REQUEST)); assertThat(e.getMessage(), equalTo("Datafeed query is not parsable")); } @@ -99,9 +99,9 @@ protected QueryProvider mutateInstance(QueryProvider instance) throws IOExceptio parsingException = parsingException == null ? new IOException("failed parsing") : null; break; case 1: - parsedQuery = parsedQuery == null ? - XContentObjectTransformer.queryBuilderTransformer(xContentRegistry()).fromMap(instance.getQuery()) : - null; + parsedQuery = parsedQuery == null + ? XContentObjectTransformer.queryBuilderTransformer(xContentRegistry()).fromMap(instance.getQuery()) + : null; break; default: throw new AssertionError("Illegal randomisation branch"); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/utils/XContentObjectTransformerTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/utils/XContentObjectTransformerTests.java index 1298285b49f80..38a1ab4f45c79 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/utils/XContentObjectTransformerTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/utils/XContentObjectTransformerTests.java @@ -9,12 +9,7 @@ import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.ToXContentObject; -import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentParseException; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; @@ -24,6 +19,11 @@ import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; import org.elasticsearch.search.aggregations.metrics.MaxAggregationBuilder; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ToXContentObject; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentParseException; +import org.elasticsearch.xcontent.XContentType; import java.io.IOException; import java.util.ArrayList; @@ -32,8 +32,8 @@ import java.util.List; import java.util.Map; -import static org.elasticsearch.xcontent.ToXContent.EMPTY_PARAMS; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertToXContentEquivalent; +import static org.elasticsearch.xcontent.ToXContent.EMPTY_PARAMS; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasItem; @@ -48,73 +48,91 @@ public NamedXContentRegistry xContentRegistry() { } public void testFromMap() throws IOException { - Map aggMap = Collections.singletonMap("fieldName", - Collections.singletonMap("max", - Collections.singletonMap("field", "fieldName"))); - - XContentObjectTransformer aggTransformer = - XContentObjectTransformer.aggregatorTransformer(xContentRegistry()); + Map aggMap = Collections.singletonMap( + "fieldName", + Collections.singletonMap("max", Collections.singletonMap("field", "fieldName")) + ); + + XContentObjectTransformer aggTransformer = XContentObjectTransformer.aggregatorTransformer( + xContentRegistry() + ); assertXContentAreEqual(aggTransformer.fromMap(aggMap), aggMap); assertXContentAreEqual(aggTransformer.fromMap(aggMap), aggTransformer.toMap(aggTransformer.fromMap(aggMap))); - Map queryMap = Collections.singletonMap("match", - Collections.singletonMap("fieldName", new HashMap(){{ - // Add all the default fields so they are not added dynamically when the object is parsed - put("query","fieldValue"); - put("operator","OR"); - put("prefix_length",0); - put("max_expansions",50); - put("fuzzy_transpositions",true); - put("lenient",false); - put("zero_terms_query","NONE"); - put("auto_generate_synonyms_phrase_query",true); - put("boost",1.0); - }})); - - XContentObjectTransformer queryBuilderTransformer = - XContentObjectTransformer.queryBuilderTransformer(xContentRegistry()); + Map queryMap = Collections.singletonMap( + "match", + Collections.singletonMap("fieldName", new HashMap() { + { + // Add all the default fields so they are not added dynamically when the object is parsed + put("query", "fieldValue"); + put("operator", "OR"); + put("prefix_length", 0); + put("max_expansions", 50); + put("fuzzy_transpositions", true); + put("lenient", false); + put("zero_terms_query", "NONE"); + put("auto_generate_synonyms_phrase_query", true); + put("boost", 1.0); + } + }) + ); + + XContentObjectTransformer queryBuilderTransformer = XContentObjectTransformer.queryBuilderTransformer( + xContentRegistry() + ); assertXContentAreEqual(queryBuilderTransformer.fromMap(queryMap), queryMap); - assertXContentAreEqual(queryBuilderTransformer.fromMap(queryMap), - queryBuilderTransformer.toMap(queryBuilderTransformer.fromMap(queryMap))); + assertXContentAreEqual( + queryBuilderTransformer.fromMap(queryMap), + queryBuilderTransformer.toMap(queryBuilderTransformer.fromMap(queryMap)) + ); } public void testFromMapWithBadMaps() { - Map queryMap = Collections.singletonMap("match", - Collections.singletonMap("airline", new HashMap() {{ + Map queryMap = Collections.singletonMap("match", Collections.singletonMap("airline", new HashMap() { + { put("query", "notSupported"); - put("type", "phrase"); //phrase stopped being supported for match in 6.x - }})); + put("type", "phrase"); // phrase stopped being supported for match in 6.x + } + })); - XContentObjectTransformer queryBuilderTransformer = - XContentObjectTransformer.queryBuilderTransformer(xContentRegistry()); - ParsingException exception = expectThrows(ParsingException.class, - () -> queryBuilderTransformer.fromMap(queryMap)); + XContentObjectTransformer queryBuilderTransformer = XContentObjectTransformer.queryBuilderTransformer( + xContentRegistry() + ); + ParsingException exception = expectThrows(ParsingException.class, () -> queryBuilderTransformer.fromMap(queryMap)); assertThat(exception.getMessage(), equalTo("[match] query does not support [type]")); - Map aggMap = Collections.singletonMap("badTerms", - Collections.singletonMap("terms", new HashMap() {{ - put("size", 0); //size being 0 in terms agg stopped being supported in 6.x + Map aggMap = Collections.singletonMap("badTerms", Collections.singletonMap("terms", new HashMap() { + { + put("size", 0); // size being 0 in terms agg stopped being supported in 6.x put("field", "myField"); - }})); + } + })); - XContentObjectTransformer aggTransformer = - XContentObjectTransformer.aggregatorTransformer(xContentRegistry()); + XContentObjectTransformer aggTransformer = XContentObjectTransformer.aggregatorTransformer( + xContentRegistry() + ); XContentParseException xContentParseException = expectThrows(XContentParseException.class, () -> aggTransformer.fromMap(aggMap)); assertThat(xContentParseException.getMessage(), containsString("[terms] failed to parse field [size]")); } public void testToMap() throws IOException { - XContentObjectTransformer aggTransformer = - XContentObjectTransformer.aggregatorTransformer(xContentRegistry()); - XContentObjectTransformer queryBuilderTransformer = - XContentObjectTransformer.queryBuilderTransformer(xContentRegistry()); + XContentObjectTransformer aggTransformer = XContentObjectTransformer.aggregatorTransformer( + xContentRegistry() + ); + XContentObjectTransformer queryBuilderTransformer = XContentObjectTransformer.queryBuilderTransformer( + xContentRegistry() + ); AggregatorFactories.Builder aggs = new AggregatorFactories.Builder(); long aggHistogramInterval = randomNonNegativeLong(); MaxAggregationBuilder maxTime = AggregationBuilders.max("time").field("time"); - aggs.addAggregator(AggregationBuilders.dateHistogram("buckets") - .fixedInterval(new DateHistogramInterval(aggHistogramInterval + "ms")).subAggregation(maxTime).field("time")); + aggs.addAggregator( + AggregationBuilders.dateHistogram("buckets") + .fixedInterval(new DateHistogramInterval(aggHistogramInterval + "ms")) + .subAggregation(maxTime) + .field("time") + ); assertXContentAreEqual(aggs, aggTransformer.toMap(aggs)); assertXContentAreEqual(aggTransformer.fromMap(aggTransformer.toMap(aggs)), aggTransformer.toMap(aggs)); @@ -122,17 +140,21 @@ public void testToMap() throws IOException { QueryBuilder queryBuilder = QueryBuilders.termQuery(randomAlphaOfLength(10), randomAlphaOfLength(10)); assertXContentAreEqual(queryBuilder, queryBuilderTransformer.toMap(queryBuilder)); - assertXContentAreEqual(queryBuilderTransformer.fromMap(queryBuilderTransformer.toMap(queryBuilder)), - queryBuilderTransformer.toMap(queryBuilder)); + assertXContentAreEqual( + queryBuilderTransformer.fromMap(queryBuilderTransformer.toMap(queryBuilder)), + queryBuilderTransformer.toMap(queryBuilder) + ); } public void testDeprecationWarnings() throws IOException { - XContentObjectTransformer queryBuilderTransformer = new XContentObjectTransformer<>(NamedXContentRegistry.EMPTY, - (p)-> { - p.getDeprecationHandler().logReplacedField(null, null, "oldField", "newField"); - p.getDeprecationHandler().logRenamedField(null, null, "oldName", "modernName"); - return new BoolQueryBuilder(); - }); + XContentObjectTransformer queryBuilderTransformer = new XContentObjectTransformer<>( + NamedXContentRegistry.EMPTY, + (p) -> { + p.getDeprecationHandler().logReplacedField(null, null, "oldField", "newField"); + p.getDeprecationHandler().logRenamedField(null, null, "oldName", "modernName"); + return new BoolQueryBuilder(); + } + ); List deprecations = new ArrayList<>(); queryBuilderTransformer.fromMap(Collections.singletonMap("bool", "match"), deprecations); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/ConfigTestHelpers.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/ConfigTestHelpers.java index 076f0a35354e1..efa50937619d0 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/ConfigTestHelpers.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/ConfigTestHelpers.java @@ -7,6 +7,7 @@ package org.elasticsearch.xpack.core.rollup; import com.carrotsearch.randomizedtesting.generators.RandomNumbers; + import org.elasticsearch.core.TimeValue; import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder; import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; @@ -36,14 +37,14 @@ public class ConfigTestHelpers { - private static final String[] TIME_SUFFIXES = new String[]{"d", "h", "ms", "s", "m"}; + private static final String[] TIME_SUFFIXES = new String[] { "d", "h", "ms", "s", "m" }; - private ConfigTestHelpers() { - } + private ConfigTestHelpers() {} public static RollupJobConfig randomRollupJobConfig(final Random random) { return randomRollupJobConfig(random, randomAsciiAlphanumOfLengthBetween(random, 5, 20)); } + public static RollupJobConfig randomRollupJobConfig(final Random random, final String id) { return randomRollupJobConfig(random, id, randomAsciiAlphanumOfLengthBetween(random, 5, 20)); } @@ -52,10 +53,12 @@ public static RollupJobConfig randomRollupJobConfig(final Random random, final S return randomRollupJobConfig(random, id, indexPattern, "rollup_" + indexPattern); } - public static RollupJobConfig randomRollupJobConfig(final Random random, - final String id, - final String indexPattern, - final String rollupIndex) { + public static RollupJobConfig randomRollupJobConfig( + final Random random, + final String id, + final String indexPattern, + final String rollupIndex + ) { final String cron = randomCron(); final int pageSize = randomIntBetween(random, 1, 10); final TimeValue timeout = random.nextBoolean() ? null : randomTimeout(random); @@ -107,20 +110,20 @@ public static DateHistogramGroupConfig randomDateHistogramGroupConfigWithField(f } } - public static List getFields() { + public static List getFields() { return IntStream.range(0, ESTestCase.randomIntBetween(1, 10)) - .mapToObj(n -> ESTestCase.randomAlphaOfLengthBetween(5, 10)) - .collect(Collectors.toList()); + .mapToObj(n -> ESTestCase.randomAlphaOfLengthBetween(5, 10)) + .collect(Collectors.toList()); } public static String randomCron() { - return (ESTestCase.randomBoolean() ? "*" : String.valueOf(ESTestCase.randomIntBetween(0, 59))) + //second - " " + (ESTestCase.randomBoolean() ? "*" : String.valueOf(ESTestCase.randomIntBetween(0, 59))) + //minute - " " + (ESTestCase.randomBoolean() ? "*" : String.valueOf(ESTestCase.randomIntBetween(0, 23))) + //hour - " " + (ESTestCase.randomBoolean() ? "*" : String.valueOf(ESTestCase.randomIntBetween(1, 31))) + //day of month - " " + (ESTestCase.randomBoolean() ? "*" : String.valueOf(ESTestCase.randomIntBetween(1, 12))) + //month - " ?" + //day of week - " " + (ESTestCase.randomBoolean() ? "*" : String.valueOf(ESTestCase.randomIntBetween(1970, 2199))); //year + return (ESTestCase.randomBoolean() ? "*" : String.valueOf(ESTestCase.randomIntBetween(0, 59))) + // second + " " + (ESTestCase.randomBoolean() ? "*" : String.valueOf(ESTestCase.randomIntBetween(0, 59))) + // minute + " " + (ESTestCase.randomBoolean() ? "*" : String.valueOf(ESTestCase.randomIntBetween(0, 23))) + // hour + " " + (ESTestCase.randomBoolean() ? "*" : String.valueOf(ESTestCase.randomIntBetween(1, 31))) + // day of month + " " + (ESTestCase.randomBoolean() ? "*" : String.valueOf(ESTestCase.randomIntBetween(1, 12))) + // month + " ?" + // day of week + " " + (ESTestCase.randomBoolean() ? "*" : String.valueOf(ESTestCase.randomIntBetween(1970, 2199))); // year } public static HistogramGroupConfig randomHistogramGroupConfig(final Random random) { @@ -175,8 +178,10 @@ private static long randomInterval(final Random random) { } public static TimeValue randomTimeout(final Random random) { - return new TimeValue(randomIntBetween(random, 0, 60), - randomFrom(random, Arrays.asList(TimeUnit.MILLISECONDS, TimeUnit.SECONDS, TimeUnit.MINUTES))); + return new TimeValue( + randomIntBetween(random, 0, 60), + randomFrom(random, Arrays.asList(TimeUnit.MILLISECONDS, TimeUnit.SECONDS, TimeUnit.MINUTES)) + ); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/RollupActionConfigTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/RollupActionConfigTests.java index f64b99f38864a..d57bd9355a954 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/RollupActionConfigTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/RollupActionConfigTests.java @@ -9,9 +9,9 @@ import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.fieldcaps.FieldCapabilities; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.rollup.job.MetricConfig; import org.elasticsearch.xpack.core.rollup.job.TermsGroupConfig; @@ -27,7 +27,6 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; - public class RollupActionConfigTests extends AbstractSerializingTestCase { @Override @@ -52,15 +51,19 @@ protected RollupActionConfig doParseInstance(final XContentParser parser) throws } public void testEmptyGroupAndMetrics() { - Exception e = expectThrows(IllegalArgumentException.class, () -> - new RollupActionConfig(null, randomBoolean() ? null : emptyList())); + Exception e = expectThrows( + IllegalArgumentException.class, + () -> new RollupActionConfig(null, randomBoolean() ? null : emptyList()) + ); assertThat(e.getMessage(), equalTo("At least one grouping or metric must be configured")); } public void testEmptyMetrics() { final RollupActionGroupConfig groupConfig = ConfigTestHelpers.randomRollupActionGroupConfig(random()); - Exception e = expectThrows(IllegalArgumentException.class, () -> - new RollupActionConfig(groupConfig, randomBoolean() ? null : emptyList())); + Exception e = expectThrows( + IllegalArgumentException.class, + () -> new RollupActionConfig(groupConfig, randomBoolean() ? null : emptyList()) + ); assertThat(e.getMessage(), equalTo("At least one metric must be configured")); } @@ -77,16 +80,20 @@ public void testValidateMapping() { responseMap.put("group_field", Collections.singletonMap("keyword", myFieldCaps)); responseMap.put("metric_field", Collections.singletonMap("short", myFieldCaps)); - RollupActionConfig config = new RollupActionConfig(new RollupActionGroupConfig( - new RollupActionDateHistogramGroupConfig.FixedInterval("date_field", DateHistogramInterval.DAY), - null, new TermsGroupConfig("group_field")), - List.of(new MetricConfig("metric_field", List.of("max")))); + RollupActionConfig config = new RollupActionConfig( + new RollupActionGroupConfig( + new RollupActionDateHistogramGroupConfig.FixedInterval("date_field", DateHistogramInterval.DAY), + null, + new TermsGroupConfig("group_field") + ), + List.of(new MetricConfig("metric_field", List.of("max"))) + ); config.validateMappings(responseMap, e); assertThat(e.validationErrors().size(), equalTo(0)); } private String getRandomType() { - int n = randomIntBetween(0,8); + int n = randomIntBetween(0, 8); if (n == 0) { return "keyword"; } else if (n == 1) { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/RollupActionDateHistogramGroupConfigSerializingTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/RollupActionDateHistogramGroupConfigSerializingTests.java index 937cc916a532d..35575024575b0 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/RollupActionDateHistogramGroupConfigSerializingTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/RollupActionDateHistogramGroupConfigSerializingTests.java @@ -9,9 +9,9 @@ import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.fieldcaps.FieldCapabilities; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.Collections; @@ -23,8 +23,8 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -public class RollupActionDateHistogramGroupConfigSerializingTests - extends AbstractSerializingTestCase { +public class RollupActionDateHistogramGroupConfigSerializingTests extends AbstractSerializingTestCase< + RollupActionDateHistogramGroupConfig> { @Override protected RollupActionDateHistogramGroupConfig doParseInstance(final XContentParser parser) throws IOException { @@ -45,8 +45,10 @@ public void testValidateNoMapping() { ActionRequestValidationException e = new ActionRequestValidationException(); Map> responseMap = new HashMap<>(); - RollupActionDateHistogramGroupConfig config = new RollupActionDateHistogramGroupConfig.CalendarInterval("my_field", - new DateHistogramInterval("1d")); + RollupActionDateHistogramGroupConfig config = new RollupActionDateHistogramGroupConfig.CalendarInterval( + "my_field", + new DateHistogramInterval("1d") + ); config.validateMappings(responseMap, e); assertThat(e.validationErrors().get(0), equalTo("Could not find one of [date,date_nanos] fields with name [my_field].")); } @@ -59,8 +61,10 @@ public void testValidateNomatchingField() { FieldCapabilities fieldCaps = mock(FieldCapabilities.class); responseMap.put("some_other_field", Collections.singletonMap("date", fieldCaps)); - RollupActionDateHistogramGroupConfig config = new RollupActionDateHistogramGroupConfig.CalendarInterval("my_field", - new DateHistogramInterval("1d")); + RollupActionDateHistogramGroupConfig config = new RollupActionDateHistogramGroupConfig.CalendarInterval( + "my_field", + new DateHistogramInterval("1d") + ); config.validateMappings(responseMap, e); assertThat(e.validationErrors().get(0), equalTo("Could not find one of [date,date_nanos] fields with name [my_field].")); } @@ -73,11 +77,18 @@ public void testValidateFieldWrongType() { FieldCapabilities fieldCaps = mock(FieldCapabilities.class); responseMap.put("my_field", Collections.singletonMap("keyword", fieldCaps)); - RollupActionDateHistogramGroupConfig config = new RollupActionDateHistogramGroupConfig.CalendarInterval("my_field", - new DateHistogramInterval("1d")); + RollupActionDateHistogramGroupConfig config = new RollupActionDateHistogramGroupConfig.CalendarInterval( + "my_field", + new DateHistogramInterval("1d") + ); config.validateMappings(responseMap, e); - assertThat(e.validationErrors().get(0), equalTo("The field referenced by a date_histo group must be one of type " + - "[date,date_nanos]. Found: [keyword] for field [my_field]")); + assertThat( + e.validationErrors().get(0), + equalTo( + "The field referenced by a date_histo group must be one of type " + + "[date,date_nanos]. Found: [keyword] for field [my_field]" + ) + ); } public void testValidateFieldMixtureTypes() { @@ -91,11 +102,18 @@ public void testValidateFieldMixtureTypes() { types.put("keyword", fieldCaps); responseMap.put("my_field", types); - RollupActionDateHistogramGroupConfig config = new RollupActionDateHistogramGroupConfig.CalendarInterval("my_field", - new DateHistogramInterval("1d")); + RollupActionDateHistogramGroupConfig config = new RollupActionDateHistogramGroupConfig.CalendarInterval( + "my_field", + new DateHistogramInterval("1d") + ); config.validateMappings(responseMap, e); - assertThat(e.validationErrors().get(0), equalTo("The field referenced by a date_histo group must be one of type " + - "[date,date_nanos]. Found: [date, keyword] for field [my_field]")); + assertThat( + e.validationErrors().get(0), + equalTo( + "The field referenced by a date_histo group must be one of type " + + "[date,date_nanos]. Found: [date, keyword] for field [my_field]" + ) + ); } public void testValidateFieldMatchingNotAggregatable() { @@ -107,8 +125,10 @@ public void testValidateFieldMatchingNotAggregatable() { when(fieldCaps.isAggregatable()).thenReturn(false); responseMap.put("my_field", Collections.singletonMap("date", fieldCaps)); - RollupActionDateHistogramGroupConfig config =new RollupActionDateHistogramGroupConfig.CalendarInterval("my_field", - new DateHistogramInterval("1d")); + RollupActionDateHistogramGroupConfig config = new RollupActionDateHistogramGroupConfig.CalendarInterval( + "my_field", + new DateHistogramInterval("1d") + ); config.validateMappings(responseMap, e); assertThat(e.validationErrors().get(0), equalTo("The field [my_field] must be aggregatable, but is not.")); } @@ -122,8 +142,10 @@ public void testValidateMatchingField() { when(fieldCaps.isAggregatable()).thenReturn(true); responseMap.put("my_field", Collections.singletonMap("date", fieldCaps)); - RollupActionDateHistogramGroupConfig config = new RollupActionDateHistogramGroupConfig.CalendarInterval("my_field", - new DateHistogramInterval("1d")); + RollupActionDateHistogramGroupConfig config = new RollupActionDateHistogramGroupConfig.CalendarInterval( + "my_field", + new DateHistogramInterval("1d") + ); config.validateMappings(responseMap, e); assertThat(e.validationErrors().size(), equalTo(0)); } @@ -137,8 +159,10 @@ public void testValidateWeek() { when(fieldCaps.isAggregatable()).thenReturn(true); responseMap.put("my_field", Collections.singletonMap("date", fieldCaps)); - RollupActionDateHistogramGroupConfig config = new RollupActionDateHistogramGroupConfig.CalendarInterval("my_field", - new DateHistogramInterval("1w")); + RollupActionDateHistogramGroupConfig config = new RollupActionDateHistogramGroupConfig.CalendarInterval( + "my_field", + new DateHistogramInterval("1w") + ); config.validateMappings(responseMap, e); assertThat(e.validationErrors().size(), equalTo(0)); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/RollupActionGroupConfigSerializingTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/RollupActionGroupConfigSerializingTests.java index 86e985adbce92..475a65a4efa46 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/RollupActionGroupConfigSerializingTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/RollupActionGroupConfigSerializingTests.java @@ -9,9 +9,9 @@ import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.fieldcaps.FieldCapabilities; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.rollup.job.HistogramGroupConfig; import org.elasticsearch.xpack.core.rollup.job.TermsGroupConfig; @@ -50,7 +50,8 @@ public void testValidatesDateHistogramConfig() { when(fieldCaps.isAggregatable()).thenReturn(true); responseMap.put("date_field", Collections.singletonMap("not_date", fieldCaps)); RollupActionGroupConfig config = new RollupActionGroupConfig( - new RollupActionDateHistogramGroupConfig.FixedInterval("date_field", DateHistogramInterval.DAY)); + new RollupActionDateHistogramGroupConfig.FixedInterval("date_field", DateHistogramInterval.DAY) + ); config.validateMappings(responseMap, e); assertThat(e.validationErrors().size(), equalTo(1)); } @@ -66,16 +67,17 @@ public void testValidatesAllSubConfigs() { responseMap.put("histogram_field", Collections.singletonMap("keyword", fieldCaps)); RollupActionGroupConfig config = new RollupActionGroupConfig( new RollupActionDateHistogramGroupConfig.FixedInterval("date_field", DateHistogramInterval.DAY), - new HistogramGroupConfig(132, "histogram_field"), new TermsGroupConfig("terms_field")); + new HistogramGroupConfig(132, "histogram_field"), + new TermsGroupConfig("terms_field") + ); config.validateMappings(responseMap, e); // all fields are non-aggregatable assertThat(e.validationErrors().size(), equalTo(3)); - assertThat(e.validationErrors().get(0), - equalTo("The field [date_field] must be aggregatable, but is not.")); - assertThat(e.validationErrors().get(1), - equalTo("The field referenced by a histo group must be a [numeric] type, " + - "but found [keyword] for field [histogram_field]")); - assertThat(e.validationErrors().get(2), - equalTo("The field [terms_field] must be aggregatable across all indices, but is not.")); + assertThat(e.validationErrors().get(0), equalTo("The field [date_field] must be aggregatable, but is not.")); + assertThat( + e.validationErrors().get(1), + equalTo("The field referenced by a histo group must be a [numeric] type, " + "but found [keyword] for field [histogram_field]") + ); + assertThat(e.validationErrors().get(2), equalTo("The field [terms_field] must be aggregatable across all indices, but is not.")); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/job/DateHistogramGroupConfigSerializingTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/job/DateHistogramGroupConfigSerializingTests.java index 330cea0460f71..737261166eed6 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/job/DateHistogramGroupConfigSerializingTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/job/DateHistogramGroupConfigSerializingTests.java @@ -11,9 +11,9 @@ import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.rollup.ConfigTestHelpers; import java.io.IOException; @@ -29,7 +29,11 @@ public class DateHistogramGroupConfigSerializingTests extends AbstractSerializingTestCase { - private enum DateHistoType { FIXED, CALENDAR } + private enum DateHistoType { + FIXED, + CALENDAR + } + private static DateHistoType type; @Override @@ -64,11 +68,19 @@ public void testValidateNoMapping() { ActionRequestValidationException e = new ActionRequestValidationException(); Map> responseMap = new HashMap<>(); - DateHistogramGroupConfig config = new DateHistogramGroupConfig.CalendarInterval("my_field", - new DateHistogramInterval("1d"), null, null); + DateHistogramGroupConfig config = new DateHistogramGroupConfig.CalendarInterval( + "my_field", + new DateHistogramInterval("1d"), + null, + null + ); config.validateMappings(responseMap, e); - assertThat(e.validationErrors().get(0), equalTo("Could not find one of [date,date_nanos] fields with name [my_field] in " + - "any of the indices matching the index pattern.")); + assertThat( + e.validationErrors().get(0), + equalTo( + "Could not find one of [date,date_nanos] fields with name [my_field] in " + "any of the indices matching the index pattern." + ) + ); } public void testValidateNomatchingField() { @@ -79,11 +91,19 @@ public void testValidateNomatchingField() { FieldCapabilities fieldCaps = mock(FieldCapabilities.class); responseMap.put("some_other_field", Collections.singletonMap("date", fieldCaps)); - DateHistogramGroupConfig config = new DateHistogramGroupConfig.CalendarInterval("my_field", - new DateHistogramInterval("1d"), null, null); + DateHistogramGroupConfig config = new DateHistogramGroupConfig.CalendarInterval( + "my_field", + new DateHistogramInterval("1d"), + null, + null + ); config.validateMappings(responseMap, e); - assertThat(e.validationErrors().get(0), equalTo("Could not find one of [date,date_nanos] fields with name [my_field] in " + - "any of the indices matching the index pattern.")); + assertThat( + e.validationErrors().get(0), + equalTo( + "Could not find one of [date,date_nanos] fields with name [my_field] in " + "any of the indices matching the index pattern." + ) + ); } public void testValidateFieldWrongType() { @@ -94,11 +114,20 @@ public void testValidateFieldWrongType() { FieldCapabilities fieldCaps = mock(FieldCapabilities.class); responseMap.put("my_field", Collections.singletonMap("keyword", fieldCaps)); - DateHistogramGroupConfig config = new DateHistogramGroupConfig.CalendarInterval("my_field", - new DateHistogramInterval("1d"), null, null); + DateHistogramGroupConfig config = new DateHistogramGroupConfig.CalendarInterval( + "my_field", + new DateHistogramInterval("1d"), + null, + null + ); config.validateMappings(responseMap, e); - assertThat(e.validationErrors().get(0), equalTo("The field referenced by a date_histo group must be one of type " + - "[date,date_nanos] across all indices in the index pattern. Found: [keyword] for field [my_field]")); + assertThat( + e.validationErrors().get(0), + equalTo( + "The field referenced by a date_histo group must be one of type " + + "[date,date_nanos] across all indices in the index pattern. Found: [keyword] for field [my_field]" + ) + ); } public void testValidateFieldMixtureTypes() { @@ -112,11 +141,20 @@ public void testValidateFieldMixtureTypes() { types.put("keyword", fieldCaps); responseMap.put("my_field", types); - DateHistogramGroupConfig config = new DateHistogramGroupConfig.CalendarInterval("my_field", - new DateHistogramInterval("1d"), null, null); + DateHistogramGroupConfig config = new DateHistogramGroupConfig.CalendarInterval( + "my_field", + new DateHistogramInterval("1d"), + null, + null + ); config.validateMappings(responseMap, e); - assertThat(e.validationErrors().get(0), equalTo("The field referenced by a date_histo group must be one of type " + - "[date,date_nanos] across all indices in the index pattern. Found: [date, keyword] for field [my_field]")); + assertThat( + e.validationErrors().get(0), + equalTo( + "The field referenced by a date_histo group must be one of type " + + "[date,date_nanos] across all indices in the index pattern. Found: [date, keyword] for field [my_field]" + ) + ); } public void testValidateFieldMatchingNotAggregatable() { @@ -128,8 +166,12 @@ public void testValidateFieldMatchingNotAggregatable() { when(fieldCaps.isAggregatable()).thenReturn(false); responseMap.put("my_field", Collections.singletonMap("date", fieldCaps)); - DateHistogramGroupConfig config =new DateHistogramGroupConfig.CalendarInterval("my_field", - new DateHistogramInterval("1d"), null, null); + DateHistogramGroupConfig config = new DateHistogramGroupConfig.CalendarInterval( + "my_field", + new DateHistogramInterval("1d"), + null, + null + ); config.validateMappings(responseMap, e); assertThat(e.validationErrors().get(0), equalTo("The field [my_field] must be aggregatable across all indices, but is not.")); } @@ -143,8 +185,12 @@ public void testValidateMatchingField() { when(fieldCaps.isAggregatable()).thenReturn(true); responseMap.put("my_field", Collections.singletonMap("date", fieldCaps)); - DateHistogramGroupConfig config = new DateHistogramGroupConfig.CalendarInterval("my_field", - new DateHistogramInterval("1d"), null, null); + DateHistogramGroupConfig config = new DateHistogramGroupConfig.CalendarInterval( + "my_field", + new DateHistogramInterval("1d"), + null, + null + ); config.validateMappings(responseMap, e); assertThat(e.validationErrors().size(), equalTo(0)); } @@ -158,8 +204,12 @@ public void testValidateWeek() { when(fieldCaps.isAggregatable()).thenReturn(true); responseMap.put("my_field", Collections.singletonMap("date", fieldCaps)); - DateHistogramGroupConfig config = new DateHistogramGroupConfig.CalendarInterval("my_field", - new DateHistogramInterval("1w"), null, null); + DateHistogramGroupConfig config = new DateHistogramGroupConfig.CalendarInterval( + "my_field", + new DateHistogramInterval("1w"), + null, + null + ); config.validateMappings(responseMap, e); assertThat(e.validationErrors().size(), equalTo(0)); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/job/GroupConfigSerializingTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/job/GroupConfigSerializingTests.java index 888bf857005b5..685f3553f7b5d 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/job/GroupConfigSerializingTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/job/GroupConfigSerializingTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.rollup.job; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/job/HistogramGroupConfigSerializingTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/job/HistogramGroupConfigSerializingTests.java index 5387d656fd6c2..4de9badb73fcf 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/job/HistogramGroupConfigSerializingTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/job/HistogramGroupConfigSerializingTests.java @@ -9,8 +9,8 @@ import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.fieldcaps.FieldCapabilities; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.rollup.RollupField; import java.io.IOException; @@ -46,8 +46,10 @@ public void testValidateNoMapping() throws IOException { HistogramGroupConfig config = new HistogramGroupConfig(132, "my_field"); config.validateMappings(responseMap, e); - assertThat(e.validationErrors().get(0), equalTo("Could not find a [numeric] field with name [my_field] in any of the " + - "indices matching the index pattern.")); + assertThat( + e.validationErrors().get(0), + equalTo("Could not find a [numeric] field with name [my_field] in any of the " + "indices matching the index pattern.") + ); } public void testValidateNomatchingField() throws IOException { @@ -61,8 +63,10 @@ public void testValidateNomatchingField() throws IOException { HistogramGroupConfig config = new HistogramGroupConfig(132, "my_field"); config.validateMappings(responseMap, e); - assertThat(e.validationErrors().get(0), equalTo("Could not find a [numeric] field with name [my_field] in any of the " + - "indices matching the index pattern.")); + assertThat( + e.validationErrors().get(0), + equalTo("Could not find a [numeric] field with name [my_field] in any of the " + "indices matching the index pattern.") + ); } public void testValidateFieldWrongType() throws IOException { @@ -76,8 +80,10 @@ public void testValidateFieldWrongType() throws IOException { HistogramGroupConfig config = new HistogramGroupConfig(132, "my_field"); config.validateMappings(responseMap, e); - assertThat(e.validationErrors().get(0), equalTo("The field referenced by a histo group must be a [numeric] type, but " + - "found [keyword] for field [my_field]")); + assertThat( + e.validationErrors().get(0), + equalTo("The field referenced by a histo group must be a [numeric] type, but " + "found [keyword] for field [my_field]") + ); } public void testValidateFieldMatchingNotAggregatable() throws IOException { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/job/JobWrapperSerializingTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/job/JobWrapperSerializingTests.java index 91c835133f38a..bb7e2424c8bd2 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/job/JobWrapperSerializingTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/job/JobWrapperSerializingTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.rollup.job; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.indexing.IndexerState; import org.elasticsearch.xpack.core.rollup.ConfigTestHelpers; import org.elasticsearch.xpack.core.rollup.action.GetRollupJobsAction; @@ -30,7 +30,7 @@ protected Writeable.Reader instanceReader() { @Override protected GetRollupJobsAction.JobWrapper createTestInstance() { IndexerState state = null; - int num = randomIntBetween(0,3); + int num = randomIntBetween(0, 3); if (num == 0) { state = IndexerState.STOPPED; } else if (num == 1) { @@ -41,11 +41,23 @@ protected GetRollupJobsAction.JobWrapper createTestInstance() { state = IndexerState.ABORTING; } - return new GetRollupJobsAction.JobWrapper(ConfigTestHelpers.randomRollupJobConfig(random()), - new RollupIndexerJobStats(randomNonNegativeLong(), randomNonNegativeLong(), - randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), - randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), - randomNonNegativeLong(), randomNonNegativeLong()), - new RollupJobStatus(state, Collections.emptyMap())); + return new GetRollupJobsAction.JobWrapper( + ConfigTestHelpers.randomRollupJobConfig(random()), + new RollupIndexerJobStats( + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong() + ), + new RollupJobStatus(state, Collections.emptyMap()) + ); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/job/MetricConfigSerializingTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/job/MetricConfigSerializingTests.java index 735b5ee2c32f7..fa8c1bf27d174 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/job/MetricConfigSerializingTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/job/MetricConfigSerializingTests.java @@ -9,8 +9,8 @@ import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.fieldcaps.FieldCapabilities; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.rollup.ConfigTestHelpers; import org.elasticsearch.xpack.core.rollup.RollupField; @@ -51,8 +51,13 @@ public void testValidateNoMapping() { MetricConfig config = new MetricConfig("my_field", singletonList("max")); config.validateMappings(responseMap, e); - assertThat(e.validationErrors().get(0), equalTo("Could not find a [numeric] or [date,date_nanos] field with name [my_field] " + - "in any of the indices matching the index pattern.")); + assertThat( + e.validationErrors().get(0), + equalTo( + "Could not find a [numeric] or [date,date_nanos] field with name [my_field] " + + "in any of the indices matching the index pattern." + ) + ); } public void testValidateNoMatchingField() { @@ -65,8 +70,13 @@ public void testValidateNoMatchingField() { MetricConfig config = new MetricConfig("my_field", singletonList("max")); config.validateMappings(responseMap, e); - assertThat(e.validationErrors().get(0), equalTo("Could not find a [numeric] or [date,date_nanos] field with name [my_field] " + - "in any of the indices matching the index pattern.")); + assertThat( + e.validationErrors().get(0), + equalTo( + "Could not find a [numeric] or [date,date_nanos] field with name [my_field] " + + "in any of the indices matching the index pattern." + ) + ); } public void testValidateFieldWrongType() { @@ -79,8 +89,11 @@ public void testValidateFieldWrongType() { MetricConfig config = new MetricConfig("my_field", singletonList("max")); config.validateMappings(responseMap, e); - assertThat("The field referenced by a metric group must be a [numeric] or [date,date_nanos] type," + - " but found [keyword] for field [my_field]", is(in(e.validationErrors()))); + assertThat( + "The field referenced by a metric group must be a [numeric] or [date,date_nanos] type," + + " but found [keyword] for field [my_field]", + is(in(e.validationErrors())) + ); } public void testValidateFieldMatchingNotAggregatable() { @@ -112,9 +125,18 @@ public void testValidateDateFieldsUnsupportedMetric() { MetricConfig config = new MetricConfig("my_field", Collections.singletonList(unsupportedMetric)); ActionRequestValidationException e = new ActionRequestValidationException(); config.validateMappings(responseMap, e); - assertThat(e.validationErrors().get(0), equalTo("Only the metrics " + RollupField.SUPPORTED_DATE_METRICS.toString() + - " are supported for [" + mappingType + "] types, but unsupported metrics [" + unsupportedMetric + - "] supplied for field [my_field]")); + assertThat( + e.validationErrors().get(0), + equalTo( + "Only the metrics " + + RollupField.SUPPORTED_DATE_METRICS.toString() + + " are supported for [" + + mappingType + + "] types, but unsupported metrics [" + + unsupportedMetric + + "] supplied for field [my_field]" + ) + ); } } @@ -129,8 +151,11 @@ public void testValidateMatchingField() { FieldCapabilities fieldCaps = mock(FieldCapabilities.class); when(fieldCaps.isAggregatable()).thenReturn(true); responseMap.put("my_field", Collections.singletonMap(numericType, fieldCaps)); - MetricConfig config = ConfigTestHelpers - .randomMetricConfigWithFieldAndMetrics(random(), "my_field", RollupField.SUPPORTED_NUMERIC_METRICS); + MetricConfig config = ConfigTestHelpers.randomMetricConfigWithFieldAndMetrics( + random(), + "my_field", + RollupField.SUPPORTED_NUMERIC_METRICS + ); config.validateMappings(responseMap, e); assertThat(e.validationErrors().size(), equalTo(0)); } @@ -140,8 +165,11 @@ public void testValidateMatchingField() { FieldCapabilities fieldCaps = mock(FieldCapabilities.class); when(fieldCaps.isAggregatable()).thenReturn(true); responseMap.put("my_field", Collections.singletonMap(dateType, fieldCaps)); - MetricConfig config = ConfigTestHelpers - .randomMetricConfigWithFieldAndMetrics(random(), "my_field", RollupField.SUPPORTED_DATE_METRICS); + MetricConfig config = ConfigTestHelpers.randomMetricConfigWithFieldAndMetrics( + random(), + "my_field", + RollupField.SUPPORTED_DATE_METRICS + ); config.validateMappings(responseMap, e); assertThat(e.validationErrors().size(), equalTo(0)); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/job/RollupIndexerJobStatsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/job/RollupIndexerJobStatsTests.java index b74970ae0c520..9712282abf980 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/job/RollupIndexerJobStatsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/job/RollupIndexerJobStatsTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.rollup.job; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; public class RollupIndexerJobStatsTests extends AbstractSerializingTestCase { @@ -28,10 +28,20 @@ protected RollupIndexerJobStats doParseInstance(XContentParser parser) { } public static RollupIndexerJobStats randomStats() { - return new RollupIndexerJobStats(randomNonNegativeLong(), randomNonNegativeLong(), - randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), - randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), - randomNonNegativeLong(), randomNonNegativeLong()); + return new RollupIndexerJobStats( + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong() + ); } @Override diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/job/RollupJobConfigTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/job/RollupJobConfigTests.java index daf938eb47ed7..8a4322178dda0 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/job/RollupJobConfigTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/job/RollupJobConfigTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.rollup.job; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.junit.Before; import java.io.IOException; @@ -17,7 +17,6 @@ import static org.elasticsearch.xpack.core.rollup.ConfigTestHelpers.randomRollupJobConfig; import static org.hamcrest.Matchers.equalTo; - public class RollupJobConfigTests extends AbstractSerializingTestCase { private String jobId; @@ -49,119 +48,279 @@ protected RollupJobConfig doParseInstance(final XContentParser parser) throws IO public void testEmptyIndexPattern() { final RollupJobConfig sample = randomRollupJobConfig(random()); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> - new RollupJobConfig(sample.getId(), null, sample.getRollupIndex(), sample.getCron(), sample.getPageSize(), - sample.getGroupConfig(), sample.getMetricsConfig(), sample.getTimeout())); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> new RollupJobConfig( + sample.getId(), + null, + sample.getRollupIndex(), + sample.getCron(), + sample.getPageSize(), + sample.getGroupConfig(), + sample.getMetricsConfig(), + sample.getTimeout() + ) + ); assertThat(e.getMessage(), equalTo("Index pattern must be a non-null, non-empty string")); - e = expectThrows(IllegalArgumentException.class, () -> - new RollupJobConfig(sample.getId(), "", sample.getRollupIndex(), sample.getCron(), sample.getPageSize(), - sample.getGroupConfig(), sample.getMetricsConfig(), sample.getTimeout())); + e = expectThrows( + IllegalArgumentException.class, + () -> new RollupJobConfig( + sample.getId(), + "", + sample.getRollupIndex(), + sample.getCron(), + sample.getPageSize(), + sample.getGroupConfig(), + sample.getMetricsConfig(), + sample.getTimeout() + ) + ); assertThat(e.getMessage(), equalTo("Index pattern must be a non-null, non-empty string")); } public void testEmptyCron() { final RollupJobConfig sample = randomRollupJobConfig(random()); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> - new RollupJobConfig(sample.getId(), sample.getIndexPattern(), sample.getRollupIndex(), null, sample.getPageSize(), - sample.getGroupConfig(), sample.getMetricsConfig(), sample.getTimeout())); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> new RollupJobConfig( + sample.getId(), + sample.getIndexPattern(), + sample.getRollupIndex(), + null, + sample.getPageSize(), + sample.getGroupConfig(), + sample.getMetricsConfig(), + sample.getTimeout() + ) + ); assertThat(e.getMessage(), equalTo("Cron schedule must be a non-null, non-empty string")); - e = expectThrows(IllegalArgumentException.class, () -> - new RollupJobConfig(sample.getId(), sample.getIndexPattern(), sample.getRollupIndex(), "", sample.getPageSize(), - sample.getGroupConfig(), sample.getMetricsConfig(), sample.getTimeout())); + e = expectThrows( + IllegalArgumentException.class, + () -> new RollupJobConfig( + sample.getId(), + sample.getIndexPattern(), + sample.getRollupIndex(), + "", + sample.getPageSize(), + sample.getGroupConfig(), + sample.getMetricsConfig(), + sample.getTimeout() + ) + ); assertThat(e.getMessage(), equalTo("Cron schedule must be a non-null, non-empty string")); } public void testEmptyID() { final RollupJobConfig sample = randomRollupJobConfig(random()); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> - new RollupJobConfig(null, sample.getIndexPattern(), sample.getRollupIndex(), sample.getCron(), sample.getPageSize(), - sample.getGroupConfig(), sample.getMetricsConfig(), sample.getTimeout())); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> new RollupJobConfig( + null, + sample.getIndexPattern(), + sample.getRollupIndex(), + sample.getCron(), + sample.getPageSize(), + sample.getGroupConfig(), + sample.getMetricsConfig(), + sample.getTimeout() + ) + ); assertThat(e.getMessage(), equalTo("Id must be a non-null, non-empty string")); - e = expectThrows(IllegalArgumentException.class, () -> - new RollupJobConfig("", sample.getIndexPattern(), sample.getRollupIndex(), sample.getCron(), sample.getPageSize(), - sample.getGroupConfig(), sample.getMetricsConfig(), sample.getTimeout())); + e = expectThrows( + IllegalArgumentException.class, + () -> new RollupJobConfig( + "", + sample.getIndexPattern(), + sample.getRollupIndex(), + sample.getCron(), + sample.getPageSize(), + sample.getGroupConfig(), + sample.getMetricsConfig(), + sample.getTimeout() + ) + ); assertThat(e.getMessage(), equalTo("Id must be a non-null, non-empty string")); } public void testMatchAllIndexPattern() { final RollupJobConfig sample = randomRollupJobConfig(random()); - Exception e = expectThrows(IllegalArgumentException.class, () -> - new RollupJobConfig(sample.getId(), "*", sample.getRollupIndex(), sample.getCron(), sample.getPageSize(), - sample.getGroupConfig(), sample.getMetricsConfig(), sample.getTimeout())); + Exception e = expectThrows( + IllegalArgumentException.class, + () -> new RollupJobConfig( + sample.getId(), + "*", + sample.getRollupIndex(), + sample.getCron(), + sample.getPageSize(), + sample.getGroupConfig(), + sample.getMetricsConfig(), + sample.getTimeout() + ) + ); assertThat(e.getMessage(), equalTo("Index pattern must not match all indices (as it would match it's own rollup index")); } public void testMatchOwnRollupPatternPrefix() { final RollupJobConfig sample = randomRollupJobConfig(random()); - Exception e = expectThrows(IllegalArgumentException.class, () -> - new RollupJobConfig(sample.getId(), "foo-*", "foo-rollup", sample.getCron(), sample.getPageSize(), - sample.getGroupConfig(), sample.getMetricsConfig(), sample.getTimeout())); + Exception e = expectThrows( + IllegalArgumentException.class, + () -> new RollupJobConfig( + sample.getId(), + "foo-*", + "foo-rollup", + sample.getCron(), + sample.getPageSize(), + sample.getGroupConfig(), + sample.getMetricsConfig(), + sample.getTimeout() + ) + ); assertThat(e.getMessage(), equalTo("Index pattern would match rollup index name which is not allowed")); } public void testMatchOwnRollupPatternSuffix() { final RollupJobConfig sample = randomRollupJobConfig(random()); - Exception e = expectThrows(IllegalArgumentException.class, () -> - new RollupJobConfig(sample.getId(), "*-rollup", "foo-rollup", sample.getCron(), sample.getPageSize(), - sample.getGroupConfig(), sample.getMetricsConfig(), sample.getTimeout())); + Exception e = expectThrows( + IllegalArgumentException.class, + () -> new RollupJobConfig( + sample.getId(), + "*-rollup", + "foo-rollup", + sample.getCron(), + sample.getPageSize(), + sample.getGroupConfig(), + sample.getMetricsConfig(), + sample.getTimeout() + ) + ); assertThat(e.getMessage(), equalTo("Index pattern would match rollup index name which is not allowed")); } public void testIndexPatternIdenticalToRollup() { final RollupJobConfig sample = randomRollupJobConfig(random()); - Exception e = expectThrows(IllegalArgumentException.class, () -> - new RollupJobConfig(sample.getId(), "foo", "foo", sample.getCron(), sample.getPageSize(), - sample.getGroupConfig(), sample.getMetricsConfig(), sample.getTimeout())); + Exception e = expectThrows( + IllegalArgumentException.class, + () -> new RollupJobConfig( + sample.getId(), + "foo", + "foo", + sample.getCron(), + sample.getPageSize(), + sample.getGroupConfig(), + sample.getMetricsConfig(), + sample.getTimeout() + ) + ); assertThat(e.getMessage(), equalTo("Rollup index may not be the same as the index pattern")); } public void testEmptyRollupIndex() { final RollupJobConfig sample = randomRollupJobConfig(random()); - Exception e = expectThrows(IllegalArgumentException.class, () -> - new RollupJobConfig(sample.getId(), sample.getIndexPattern(), "", sample.getCron(), sample.getPageSize(), - sample.getGroupConfig(), sample.getMetricsConfig(), sample.getTimeout())); + Exception e = expectThrows( + IllegalArgumentException.class, + () -> new RollupJobConfig( + sample.getId(), + sample.getIndexPattern(), + "", + sample.getCron(), + sample.getPageSize(), + sample.getGroupConfig(), + sample.getMetricsConfig(), + sample.getTimeout() + ) + ); assertThat(e.getMessage(), equalTo("Rollup index must be a non-null, non-empty string")); - e = expectThrows(IllegalArgumentException.class, () -> - new RollupJobConfig(sample.getId(), sample.getIndexPattern(), null, sample.getCron(), sample.getPageSize(), - sample.getGroupConfig(), sample.getMetricsConfig(), sample.getTimeout())); + e = expectThrows( + IllegalArgumentException.class, + () -> new RollupJobConfig( + sample.getId(), + sample.getIndexPattern(), + null, + sample.getCron(), + sample.getPageSize(), + sample.getGroupConfig(), + sample.getMetricsConfig(), + sample.getTimeout() + ) + ); assertThat(e.getMessage(), equalTo("Rollup index must be a non-null, non-empty string")); } public void testBadSize() { final RollupJobConfig sample = randomRollupJobConfig(random()); - Exception e = expectThrows(IllegalArgumentException.class, () -> - new RollupJobConfig(sample.getId(), sample.getIndexPattern(), sample.getRollupIndex(), sample.getCron(), -1, - sample.getGroupConfig(), sample.getMetricsConfig(), sample.getTimeout())); + Exception e = expectThrows( + IllegalArgumentException.class, + () -> new RollupJobConfig( + sample.getId(), + sample.getIndexPattern(), + sample.getRollupIndex(), + sample.getCron(), + -1, + sample.getGroupConfig(), + sample.getMetricsConfig(), + sample.getTimeout() + ) + ); assertThat(e.getMessage(), equalTo("Page size is mandatory and must be a positive long")); - e = expectThrows(IllegalArgumentException.class, () -> - new RollupJobConfig(sample.getId(), sample.getIndexPattern(), sample.getRollupIndex(), sample.getCron(), 0, - sample.getGroupConfig(), sample.getMetricsConfig(), sample.getTimeout())); + e = expectThrows( + IllegalArgumentException.class, + () -> new RollupJobConfig( + sample.getId(), + sample.getIndexPattern(), + sample.getRollupIndex(), + sample.getCron(), + 0, + sample.getGroupConfig(), + sample.getMetricsConfig(), + sample.getTimeout() + ) + ); assertThat(e.getMessage(), equalTo("Page size is mandatory and must be a positive long")); } public void testEmptyGroupAndMetrics() { final RollupJobConfig sample = randomRollupJobConfig(random()); - Exception e = expectThrows(IllegalArgumentException.class, () -> - new RollupJobConfig(sample.getId(), sample.getIndexPattern(), sample.getRollupIndex(), sample.getCron(), sample.getPageSize(), - null, null, sample.getTimeout())); + Exception e = expectThrows( + IllegalArgumentException.class, + () -> new RollupJobConfig( + sample.getId(), + sample.getIndexPattern(), + sample.getRollupIndex(), + sample.getCron(), + sample.getPageSize(), + null, + null, + sample.getTimeout() + ) + ); assertThat(e.getMessage(), equalTo("At least one grouping or metric must be configured")); - e = expectThrows(IllegalArgumentException.class, () -> - new RollupJobConfig(sample.getId(), sample.getIndexPattern(), sample.getRollupIndex(), sample.getCron(), sample.getPageSize(), - null, emptyList(), sample.getTimeout())); + e = expectThrows( + IllegalArgumentException.class, + () -> new RollupJobConfig( + sample.getId(), + sample.getIndexPattern(), + sample.getRollupIndex(), + sample.getCron(), + sample.getPageSize(), + null, + emptyList(), + sample.getTimeout() + ) + ); assertThat(e.getMessage(), equalTo("At least one grouping or metric must be configured")); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/job/RollupJobStatusTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/job/RollupJobStatusTests.java index 1d250eab87cf1..1f515f0a8a776 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/job/RollupJobStatusTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/job/RollupJobStatusTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.rollup.job; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.indexing.IndexerState; import java.util.HashMap; @@ -49,4 +49,3 @@ protected RollupJobStatus doParseInstance(XContentParser parser) { } } - diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/job/RollupJobTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/job/RollupJobTests.java index 0e8df2338bbff..83561d9012632 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/job/RollupJobTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/job/RollupJobTests.java @@ -8,8 +8,8 @@ import org.elasticsearch.cluster.Diff; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractDiffableSerializationTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.rollup.ConfigTestHelpers; import java.io.IOException; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/job/TermsGroupConfigSerializingTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/job/TermsGroupConfigSerializingTests.java index cbb3b8dde5ebd..2d373359ab976 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/job/TermsGroupConfigSerializingTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/job/TermsGroupConfigSerializingTests.java @@ -9,8 +9,8 @@ import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.fieldcaps.FieldCapabilities; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.Collections; @@ -45,8 +45,13 @@ public void testValidateNoMapping() { TermsGroupConfig config = new TermsGroupConfig("my_field"); config.validateMappings(responseMap, e); - assertThat(e.validationErrors().get(0), equalTo("Could not find a [numeric] or [keyword/text] field with name " + - "[my_field] in any of the indices matching the index pattern.")); + assertThat( + e.validationErrors().get(0), + equalTo( + "Could not find a [numeric] or [keyword/text] field with name " + + "[my_field] in any of the indices matching the index pattern." + ) + ); } public void testValidateNomatchingField() { @@ -59,8 +64,13 @@ public void testValidateNomatchingField() { TermsGroupConfig config = new TermsGroupConfig("my_field"); config.validateMappings(responseMap, e); - assertThat(e.validationErrors().get(0), equalTo("Could not find a [numeric] or [keyword/text] field with name " + - "[my_field] in any of the indices matching the index pattern.")); + assertThat( + e.validationErrors().get(0), + equalTo( + "Could not find a [numeric] or [keyword/text] field with name " + + "[my_field] in any of the indices matching the index pattern." + ) + ); } public void testValidateFieldWrongType() { @@ -73,8 +83,13 @@ public void testValidateFieldWrongType() { TermsGroupConfig config = new TermsGroupConfig("my_field"); config.validateMappings(responseMap, e); - assertThat(e.validationErrors().get(0), equalTo("The field referenced by a terms group must be a [numeric] or " + - "[keyword/text] type, but found [geo_point] for field [my_field]")); + assertThat( + e.validationErrors().get(0), + equalTo( + "The field referenced by a terms group must be a [numeric] or " + + "[keyword/text] type, but found [geo_point] for field [my_field]" + ) + ); } public void testValidateFieldMatchingNotAggregatable() { @@ -109,7 +124,7 @@ public void testValidateMatchingField() { } private String getRandomType() { - int n = randomIntBetween(0,8); + int n = randomIntBetween(0, 8); if (n == 0) { return "keyword"; } else if (n == 1) { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/scheduler/SchedulerEngineTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/scheduler/SchedulerEngineTests.java index 06e54195167de..b0cd5e9e1a4d4 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/scheduler/SchedulerEngineTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/scheduler/SchedulerEngineTests.java @@ -9,8 +9,8 @@ import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.message.ParameterizedMessage; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.Tuple; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.scheduler.SchedulerEngine.ActiveSchedule; import org.elasticsearch.xpack.core.scheduler.SchedulerEngine.Job; @@ -83,16 +83,14 @@ public void testListenersThrowingExceptionsDoNotCauseOtherListenersToBeSkipped() listeners.stream().map(Tuple::v1).forEach(engine::register); final AtomicBoolean scheduled = new AtomicBoolean(); - engine.add(new SchedulerEngine.Job( - getTestName(), - (startTime, now) -> { - // only allow one triggering of the listeners - if (scheduled.compareAndSet(false, true)) { - return 0; - } else { - return -1; - } - })); + engine.add(new SchedulerEngine.Job(getTestName(), (startTime, now) -> { + // only allow one triggering of the listeners + if (scheduled.compareAndSet(false, true)) { + return 0; + } else { + return -1; + } + })); latch.await(); @@ -131,19 +129,17 @@ public void testListenersThrowingExceptionsDoNotCauseNextScheduledTaskToBeSkippe // latch for each invocation of nextScheduledTimeAfter, once for each scheduled run, and then a final time when we disable final CountDownLatch latch = new CountDownLatch(1 + numberOfSchedules); - engine.add(new SchedulerEngine.Job( - getTestName(), - (startTime, now) -> { - if (latch.getCount() >= 2) { - latch.countDown(); - return 0; - } else if (latch.getCount() == 1) { - latch.countDown(); - return -1; - } else { - throw new AssertionError("nextScheduledTimeAfter invoked more than the expected number of times"); - } - })); + engine.add(new SchedulerEngine.Job(getTestName(), (startTime, now) -> { + if (latch.getCount() >= 2) { + latch.countDown(); + return 0; + } else if (latch.getCount() == 1) { + latch.countDown(); + return -1; + } else { + throw new AssertionError("nextScheduledTimeAfter invoked more than the expected number of times"); + } + })); listenersLatch.await(); assertTrue(listeners.stream().map(Tuple::v2).allMatch(count -> count.get() == numberOfSchedules)); @@ -198,10 +194,14 @@ public void testNextScheduledTimeAfterCurrentScheduledTime() throws Exception { assertNotNull(activeSchedule); assertEquals(clock.millis() + oneHourMillis, activeSchedule.getScheduledTime()); - assertEquals(clock.millis() + oneHourMillis + oneHourMillis, - activeSchedule.computeNextScheduledTime(clock.millis() - randomIntBetween(1, 999))); - assertEquals(clock.millis() + oneHourMillis + oneHourMillis, - activeSchedule.computeNextScheduledTime(clock.millis() + TimeUnit.SECONDS.toMillis(10L))); + assertEquals( + clock.millis() + oneHourMillis + oneHourMillis, + activeSchedule.computeNextScheduledTime(clock.millis() - randomIntBetween(1, 999)) + ); + assertEquals( + clock.millis() + oneHourMillis + oneHourMillis, + activeSchedule.computeNextScheduledTime(clock.millis() + TimeUnit.SECONDS.toMillis(10L)) + ); } finally { engine.stop(); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/searchablesnapshots/DataStreamFeatureSetUsageTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/searchablesnapshots/DataStreamFeatureSetUsageTests.java index cca17fd7ff14d..a2b6884a5b753 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/searchablesnapshots/DataStreamFeatureSetUsageTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/searchablesnapshots/DataStreamFeatureSetUsageTests.java @@ -17,8 +17,9 @@ public class DataStreamFeatureSetUsageTests extends AbstractWireSerializingTestC @Override protected DataStreamFeatureSetUsage createTestInstance() { - return new DataStreamFeatureSetUsage(new DataStreamFeatureSetUsage.DataStreamStats(randomNonNegativeLong(), - randomNonNegativeLong())); + return new DataStreamFeatureSetUsage( + new DataStreamFeatureSetUsage.DataStreamStats(randomNonNegativeLong(), randomNonNegativeLong()) + ); } @Override diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/searchablesnapshots/SearchableSnapshotShardStatsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/searchablesnapshots/SearchableSnapshotShardStatsTests.java index ae804a7e9823f..208bed83426b3 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/searchablesnapshots/SearchableSnapshotShardStatsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/searchablesnapshots/SearchableSnapshotShardStatsTests.java @@ -32,8 +32,13 @@ protected Writeable.Reader instanceReader() { protected SearchableSnapshotShardStats createTestInstance() { SnapshotId snapshotId = new SnapshotId(randomAlphaOfLength(5), randomAlphaOfLength(5)); IndexId indexId = new IndexId(randomAlphaOfLength(5), randomAlphaOfLength(5)); - ShardRouting shardRouting = TestShardRouting.newShardRouting(randomAlphaOfLength(5), randomInt(10), randomAlphaOfLength(5), - randomBoolean(), ShardRoutingState.STARTED); + ShardRouting shardRouting = TestShardRouting.newShardRouting( + randomAlphaOfLength(5), + randomInt(10), + randomAlphaOfLength(5), + randomBoolean(), + ShardRoutingState.STARTED + ); final List inputStats = new ArrayList<>(); for (int j = 0; j < randomInt(20); j++) { @@ -43,15 +48,29 @@ protected SearchableSnapshotShardStats createTestInstance() { } private CacheIndexInputStats randomCacheIndexInputStats() { - return new CacheIndexInputStats(randomAlphaOfLength(10), randomNonNegativeLong(), new ByteSizeValue(randomNonNegativeLong()), - new ByteSizeValue(randomNonNegativeLong()), new ByteSizeValue(randomNonNegativeLong()), - randomNonNegativeLong(), randomNonNegativeLong(), - randomCounter(), randomCounter(), - randomCounter(), randomCounter(), - randomCounter(), randomCounter(), - randomCounter(), randomCounter(), randomTimedCounter(), - randomTimedCounter(), randomTimedCounter(), - randomCounter(), randomCounter(), randomNonNegativeLong()); + return new CacheIndexInputStats( + randomAlphaOfLength(10), + randomNonNegativeLong(), + new ByteSizeValue(randomNonNegativeLong()), + new ByteSizeValue(randomNonNegativeLong()), + new ByteSizeValue(randomNonNegativeLong()), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomCounter(), + randomCounter(), + randomCounter(), + randomCounter(), + randomCounter(), + randomCounter(), + randomCounter(), + randomCounter(), + randomTimedCounter(), + randomTimedCounter(), + randomTimedCounter(), + randomCounter(), + randomCounter(), + randomNonNegativeLong() + ); } private Counter randomCounter() { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/searchablesnapshots/SearchableSnapshotsConstantsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/searchablesnapshots/SearchableSnapshotsConstantsTests.java index 26e3516d23571..b7982ff572925 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/searchablesnapshots/SearchableSnapshotsConstantsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/searchablesnapshots/SearchableSnapshotsConstantsTests.java @@ -18,19 +18,35 @@ public class SearchableSnapshotsConstantsTests extends ESTestCase { public void testIsPartialSearchableSnapshotIndex() { - assertThat(SearchableSnapshotsSettings.isPartialSearchableSnapshotIndex( - Map.of(IndexModule.INDEX_STORE_TYPE_SETTING, SearchableSnapshotsSettings.SEARCHABLE_SNAPSHOT_STORE_TYPE, - SearchableSnapshotsSettings.SNAPSHOT_PARTIAL_SETTING, false)), - is(false)); - - assertThat(SearchableSnapshotsSettings.isPartialSearchableSnapshotIndex( - Map.of(IndexModule.INDEX_STORE_TYPE_SETTING, "abc", - SearchableSnapshotsSettings.SNAPSHOT_PARTIAL_SETTING, randomBoolean())), - is(false)); - - assertThat(SearchableSnapshotsSettings.isPartialSearchableSnapshotIndex( - Map.of(IndexModule.INDEX_STORE_TYPE_SETTING, SearchableSnapshotsSettings.SEARCHABLE_SNAPSHOT_STORE_TYPE, - SearchableSnapshotsSettings.SNAPSHOT_PARTIAL_SETTING, true)), - is(true)); + assertThat( + SearchableSnapshotsSettings.isPartialSearchableSnapshotIndex( + Map.of( + IndexModule.INDEX_STORE_TYPE_SETTING, + SearchableSnapshotsSettings.SEARCHABLE_SNAPSHOT_STORE_TYPE, + SearchableSnapshotsSettings.SNAPSHOT_PARTIAL_SETTING, + false + ) + ), + is(false) + ); + + assertThat( + SearchableSnapshotsSettings.isPartialSearchableSnapshotIndex( + Map.of(IndexModule.INDEX_STORE_TYPE_SETTING, "abc", SearchableSnapshotsSettings.SNAPSHOT_PARTIAL_SETTING, randomBoolean()) + ), + is(false) + ); + + assertThat( + SearchableSnapshotsSettings.isPartialSearchableSnapshotIndex( + Map.of( + IndexModule.INDEX_STORE_TYPE_SETTING, + SearchableSnapshotsSettings.SEARCHABLE_SNAPSHOT_STORE_TYPE, + SearchableSnapshotsSettings.SNAPSHOT_PARTIAL_SETTING, + true + ) + ), + is(true) + ); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/searchablesnapshots/SearchableSnapshotsFeatureSetUsageTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/searchablesnapshots/SearchableSnapshotsFeatureSetUsageTests.java index ac36887de0f4d..ab76c5f279d31 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/searchablesnapshots/SearchableSnapshotsFeatureSetUsageTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/searchablesnapshots/SearchableSnapshotsFeatureSetUsageTests.java @@ -29,18 +29,25 @@ protected SearchableSnapshotFeatureSetUsage mutateInstance(SearchableSnapshotFea available = available == false; break; case 1: - numFullCopySearchableSnapshotIndices = randomValueOtherThan(numFullCopySearchableSnapshotIndices, - () -> randomIntBetween(0, 100000)); + numFullCopySearchableSnapshotIndices = randomValueOtherThan( + numFullCopySearchableSnapshotIndices, + () -> randomIntBetween(0, 100000) + ); break; case 2: - numSharedCacheSearchableSnapshotIndices = randomValueOtherThan(numSharedCacheSearchableSnapshotIndices, - () -> randomIntBetween(0, 100000)); + numSharedCacheSearchableSnapshotIndices = randomValueOtherThan( + numSharedCacheSearchableSnapshotIndices, + () -> randomIntBetween(0, 100000) + ); break; default: throw new AssertionError("Illegal randomisation branch"); } - return new SearchableSnapshotFeatureSetUsage(available, numFullCopySearchableSnapshotIndices, - numSharedCacheSearchableSnapshotIndices); + return new SearchableSnapshotFeatureSetUsage( + available, + numFullCopySearchableSnapshotIndices, + numSharedCacheSearchableSnapshotIndices + ); } @Override diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/EnrollmentTokenTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/EnrollmentTokenTests.java index c5df55977fba3..b796e2c6b2afb 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/EnrollmentTokenTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/EnrollmentTokenTests.java @@ -7,9 +7,9 @@ package org.elasticsearch.xpack.core.security; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.ESTestCase; import java.nio.charset.StandardCharsets; import java.util.Arrays; @@ -34,8 +34,7 @@ public void testEnrollmentToken() throws Exception { try (XContentParser parser = createParser(JsonXContent.jsonXContent, jsonString)) { final Map info = parser.map(); assertNotEquals(info, null); - enrollmentMap = info.entrySet().stream() - .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue().toString())); + enrollmentMap = info.entrySet().stream().collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue().toString())); } assertEquals(enrollmentMap.get("key"), apiKey); assertEquals(enrollmentMap.get("fgr"), fingerprint); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/ApiKeyTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/ApiKeyTests.java index 673b98d3fb23e..4bd4b59a657fa 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/ApiKeyTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/ApiKeyTests.java @@ -8,11 +8,11 @@ package org.elasticsearch.xpack.core.security.action; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.test.ESTestCase; import java.io.IOException; import java.time.Instant; @@ -31,7 +31,8 @@ public void testXContent() throws IOException { final String id = randomAlphaOfLength(20); // between 1970 and 2065 final Instant creation = Instant.ofEpochSecond(randomLongBetween(0, 3000000000L), randomLongBetween(0, 999999999)); - final Instant expiration = randomBoolean() ? null + final Instant expiration = randomBoolean() + ? null : Instant.ofEpochSecond(randomLongBetween(0, 3000000000L), randomLongBetween(0, 999999999)); final boolean invalidated = randomBoolean(); final String username = randomAlphaOfLengthBetween(4, 10); @@ -63,13 +64,19 @@ public void testXContent() throws IOException { @SuppressWarnings("unchecked") public static Map randomMetadata() { return randomFrom( - Map.of("application", randomAlphaOfLength(5), - "number", 1, - "numbers", List.of(1, 3, 5), - "environment", Map.of("os", "linux", "level", 42, "category", "trusted") + Map.of( + "application", + randomAlphaOfLength(5), + "number", + 1, + "numbers", + List.of(1, 3, 5), + "environment", + Map.of("os", "linux", "level", 42, "category", "trusted") ), Map.of(randomAlphaOfLengthBetween(3, 8), randomAlphaOfLengthBetween(3, 8)), Map.of(), - null); + null + ); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/CreateApiKeyRequestBuilderTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/CreateApiKeyRequestBuilderTests.java index fd25bfd34457a..5aeaab4ff7875 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/CreateApiKeyRequestBuilderTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/CreateApiKeyRequestBuilderTests.java @@ -10,8 +10,8 @@ import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.security.authz.RoleDescriptor; import org.elasticsearch.xpack.core.security.authz.RoleDescriptor.IndicesPrivileges; @@ -28,12 +28,12 @@ public class CreateApiKeyRequestBuilderTests extends ESTestCase { public void testParserAndCreateApiRequestBuilder() throws IOException { boolean withExpiration = randomBoolean(); final String json = "{ \"name\" : \"my-api-key\", " - + ((withExpiration) ? " \"expiration\": \"1d\", " : "") - +" \"role_descriptors\": { \"role-a\": {\"cluster\":[\"a-1\", \"a-2\"]," - + " \"index\": [{\"names\": [\"indx-a\"], \"privileges\": [\"read\"] }] }, " - + " \"role-b\": {\"cluster\":[\"b\"]," - + " \"index\": [{\"names\": [\"indx-b\"], \"privileges\": [\"read\"] }] } " - + "} }"; + + ((withExpiration) ? " \"expiration\": \"1d\", " : "") + + " \"role_descriptors\": { \"role-a\": {\"cluster\":[\"a-1\", \"a-2\"]," + + " \"index\": [{\"names\": [\"indx-a\"], \"privileges\": [\"read\"] }] }, " + + " \"role-b\": {\"cluster\":[\"b\"]," + + " \"index\": [{\"names\": [\"indx-b\"], \"privileges\": [\"read\"] }] } " + + "} }"; final BytesArray source = new BytesArray(json); final NodeClient mockClient = mock(NodeClient.class); final CreateApiKeyRequest request = new CreateApiKeyRequestBuilder(mockClient).source(source, XContentType.JSON).request(); @@ -46,15 +46,14 @@ public void testParserAndCreateApiRequestBuilder() throws IOException { if (rd.getName().equals("role-a")) { clusters = new String[] { "a-1", "a-2" }; indicesPrivileges = RoleDescriptor.IndicesPrivileges.builder().indices("indx-a").privileges("read").build(); - } else if (rd.getName().equals("role-b")){ + } else if (rd.getName().equals("role-b")) { clusters = new String[] { "b" }; indicesPrivileges = RoleDescriptor.IndicesPrivileges.builder().indices("indx-b").privileges("read").build(); } else { fail("unexpected role name"); } assertThat(rd.getClusterPrivileges(), arrayContainingInAnyOrder(clusters)); - assertThat(rd.getIndicesPrivileges(), - arrayContainingInAnyOrder(indicesPrivileges)); + assertThat(rd.getIndicesPrivileges(), arrayContainingInAnyOrder(indicesPrivileges)); } if (withExpiration) { assertThat(request.getExpiration(), equalTo(TimeValue.parseTimeValue("1d", "expiration"))); @@ -65,9 +64,9 @@ public void testParserAndCreateApiRequestBuilderWithNullOrEmptyRoleDescriptors() boolean withExpiration = randomBoolean(); boolean noRoleDescriptorsField = randomBoolean(); final String json = "{ \"name\" : \"my-api-key\"" - + ((withExpiration) ? ", \"expiration\": \"1d\"" : "") - + ((noRoleDescriptorsField) ? "" : ", \"role_descriptors\": {}") - + "}"; + + ((withExpiration) ? ", \"expiration\": \"1d\"" : "") + + ((noRoleDescriptorsField) ? "" : ", \"role_descriptors\": {}") + + "}"; final BytesArray source = new BytesArray(json); final NodeClient mockClient = mock(NodeClient.class); final CreateApiKeyRequest request = new CreateApiKeyRequestBuilder(mockClient).source(source, XContentType.JSON).request(); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/CreateApiKeyRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/CreateApiKeyRequestTests.java index d34fc28b5ab64..bba492e70553b 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/CreateApiKeyRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/CreateApiKeyRequestTests.java @@ -87,8 +87,9 @@ public void testMetadataKeyValidation() { public void testSerialization() throws IOException { final String name = randomAlphaOfLengthBetween(1, 256); - final TimeValue expiration = randomBoolean() ? null : - TimeValue.parseTimeValue(randomTimeValue(), "test serialization of create api key"); + final TimeValue expiration = randomBoolean() + ? null + : TimeValue.parseTimeValue(randomTimeValue(), "test serialization of create api key"); final WriteRequest.RefreshPolicy refreshPolicy = randomFrom(WriteRequest.RefreshPolicy.values()); boolean nullOrEmptyRoleDescriptors = randomBoolean(); final List descriptorList; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/CreateApiKeyResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/CreateApiKeyResponseTests.java index b5b7a613f4a54..2381256dfc13a 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/CreateApiKeyResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/CreateApiKeyResponseTests.java @@ -11,9 +11,9 @@ import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.settings.SecureString; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.test.EqualsHashCodeTestUtils; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.time.Instant; @@ -46,7 +46,7 @@ public void testSerialization() throws IOException { final CreateApiKeyResponse response = createTestInstance(); try (BytesStreamOutput out = new BytesStreamOutput()) { response.writeTo(out); - try (StreamInput in = out.bytes().streamInput()) { + try (StreamInput in = out.bytes().streamInput()) { CreateApiKeyResponse serialized = new CreateApiKeyResponse(in); assertThat(serialized, equalTo(response)); } @@ -56,27 +56,38 @@ public void testSerialization() throws IOException { public void testEqualsHashCode() { CreateApiKeyResponse createApiKeyResponse = createTestInstance(); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(createApiKeyResponse, (original) -> { - return new CreateApiKeyResponse(original.getName(), original.getId(), original.getKey(), original.getExpiration()); - }); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(createApiKeyResponse, (original) -> { - return new CreateApiKeyResponse(original.getName(), original.getId(), original.getKey(), original.getExpiration()); - }, CreateApiKeyResponseTests::mutateTestItem); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + createApiKeyResponse, + (original) -> { + return new CreateApiKeyResponse(original.getName(), original.getId(), original.getKey(), original.getExpiration()); + } + ); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + createApiKeyResponse, + (original) -> { + return new CreateApiKeyResponse(original.getName(), original.getId(), original.getKey(), original.getExpiration()); + }, + CreateApiKeyResponseTests::mutateTestItem + ); } private static CreateApiKeyResponse mutateTestItem(CreateApiKeyResponse original) { switch (randomIntBetween(0, 3)) { - case 0: - return new CreateApiKeyResponse(randomAlphaOfLength(5), original.getId(), original.getKey(), original.getExpiration()); - case 1: - return new CreateApiKeyResponse(original.getName(), randomAlphaOfLength(5), original.getKey(), original.getExpiration()); - case 2: - return new CreateApiKeyResponse(original.getName(), original.getId(), new SecureString(UUIDs.randomBase64UUID().toCharArray()), - original.getExpiration()); - case 3: - return new CreateApiKeyResponse(original.getName(), original.getId(), original.getKey(), Instant.now()); - default: - return new CreateApiKeyResponse(randomAlphaOfLength(5), original.getId(), original.getKey(), original.getExpiration()); + case 0: + return new CreateApiKeyResponse(randomAlphaOfLength(5), original.getId(), original.getKey(), original.getExpiration()); + case 1: + return new CreateApiKeyResponse(original.getName(), randomAlphaOfLength(5), original.getKey(), original.getExpiration()); + case 2: + return new CreateApiKeyResponse( + original.getName(), + original.getId(), + new SecureString(UUIDs.randomBase64UUID().toCharArray()), + original.getExpiration() + ); + case 3: + return new CreateApiKeyResponse(original.getName(), original.getId(), original.getKey(), Instant.now()); + default: + return new CreateApiKeyResponse(randomAlphaOfLength(5), original.getId(), original.getKey(), original.getExpiration()); } } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/GetApiKeyRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/GetApiKeyRequestTests.java index 7e96e05b06513..ecb937368f362 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/GetApiKeyRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/GetApiKeyRequestTests.java @@ -78,28 +78,30 @@ public void writeTo(StreamOutput out) throws IOException { } } - String[][] inputs = new String[][]{ - {randomNullOrEmptyString(), "user", "api-kid", "api-kname", "false"}, - {"realm", randomNullOrEmptyString(), "api-kid", "api-kname", "false"}, - {"realm", "user", "api-kid", randomNullOrEmptyString(), "false"}, - {randomNullOrEmptyString(), randomNullOrEmptyString(), "api-kid", "api-kname", "false"}, - {"realm", randomNullOrEmptyString(), randomNullOrEmptyString(), randomNullOrEmptyString(), "true"}, - {randomNullOrEmptyString(), "user", randomNullOrEmptyString(), randomNullOrEmptyString(), "true"} - }; - String[][] expectedErrorMessages = new String[][]{ - {"username or realm name must not be specified when the api key id or api key name is specified", - "only one of [api key id, api key name] can be specified"}, - {"username or realm name must not be specified when the api key id or api key name is specified", - "only one of [api key id, api key name] can be specified"}, - {"username or realm name must not be specified when the api key id or api key name is specified"}, - {"only one of [api key id, api key name] can be specified"}, - {"neither username nor realm-name may be specified when retrieving owned API keys"}, - {"neither username nor realm-name may be specified when retrieving owned API keys"} - }; + String[][] inputs = new String[][] { + { randomNullOrEmptyString(), "user", "api-kid", "api-kname", "false" }, + { "realm", randomNullOrEmptyString(), "api-kid", "api-kname", "false" }, + { "realm", "user", "api-kid", randomNullOrEmptyString(), "false" }, + { randomNullOrEmptyString(), randomNullOrEmptyString(), "api-kid", "api-kname", "false" }, + { "realm", randomNullOrEmptyString(), randomNullOrEmptyString(), randomNullOrEmptyString(), "true" }, + { randomNullOrEmptyString(), "user", randomNullOrEmptyString(), randomNullOrEmptyString(), "true" } }; + String[][] expectedErrorMessages = new String[][] { + { + "username or realm name must not be specified when the api key id or api key name is specified", + "only one of [api key id, api key name] can be specified" }, + { + "username or realm name must not be specified when the api key id or api key name is specified", + "only one of [api key id, api key name] can be specified" }, + { "username or realm name must not be specified when the api key id or api key name is specified" }, + { "only one of [api key id, api key name] can be specified" }, + { "neither username nor realm-name may be specified when retrieving owned API keys" }, + { "neither username nor realm-name may be specified when retrieving owned API keys" } }; for (int caseNo = 0; caseNo < inputs.length; caseNo++) { - try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); - OutputStreamStreamOutput osso = new OutputStreamStreamOutput(bos)) { + try ( + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + OutputStreamStreamOutput osso = new OutputStreamStreamOutput(bos) + ) { Dummy d = new Dummy(inputs[caseNo]); d.writeTo(osso); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/GetApiKeyResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/GetApiKeyResponseTests.java index 5434c7c7eaac2..028b83342b1d3 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/GetApiKeyResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/GetApiKeyResponseTests.java @@ -10,10 +10,10 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.test.ESTestCase; import java.io.IOException; import java.time.Instant; @@ -28,9 +28,16 @@ public class GetApiKeyResponseTests extends ESTestCase { public void testSerialization() throws IOException { boolean withApiKeyName = randomBoolean(); boolean withExpiration = randomBoolean(); - ApiKey apiKeyInfo = createApiKeyInfo((withApiKeyName) ? randomAlphaOfLength(4) : null, randomAlphaOfLength(5), Instant.now(), - (withExpiration) ? Instant.now() : null, false, randomAlphaOfLength(4), randomAlphaOfLength(5), - randomBoolean() ? null : Map.of(randomAlphaOfLengthBetween(3, 8), randomAlphaOfLengthBetween(3, 8))); + ApiKey apiKeyInfo = createApiKeyInfo( + (withApiKeyName) ? randomAlphaOfLength(4) : null, + randomAlphaOfLength(5), + Instant.now(), + (withExpiration) ? Instant.now() : null, + false, + randomAlphaOfLength(4), + randomAlphaOfLength(5), + randomBoolean() ? null : Map.of(randomAlphaOfLengthBetween(3, 8), randomAlphaOfLengthBetween(3, 8)) + ); GetApiKeyResponse response = new GetApiKeyResponse(Collections.singletonList(apiKeyInfo)); try (BytesStreamOutput output = new BytesStreamOutput()) { response.writeTo(output); @@ -42,31 +49,66 @@ public void testSerialization() throws IOException { } public void testToXContent() throws IOException { - ApiKey apiKeyInfo1 = createApiKeyInfo("name1", "id-1", Instant.ofEpochMilli(100000L), Instant.ofEpochMilli(10000000L), false, - "user-a", "realm-x", null); - ApiKey apiKeyInfo2 = createApiKeyInfo("name2", "id-2", Instant.ofEpochMilli(100000L), Instant.ofEpochMilli(10000000L), true, - "user-b", "realm-y", Map.of()); - ApiKey apiKeyInfo3 = createApiKeyInfo(null, "id-3", Instant.ofEpochMilli(100000L), null, true, - "user-c", "realm-z", Map.of("foo", "bar")); + ApiKey apiKeyInfo1 = createApiKeyInfo( + "name1", + "id-1", + Instant.ofEpochMilli(100000L), + Instant.ofEpochMilli(10000000L), + false, + "user-a", + "realm-x", + null + ); + ApiKey apiKeyInfo2 = createApiKeyInfo( + "name2", + "id-2", + Instant.ofEpochMilli(100000L), + Instant.ofEpochMilli(10000000L), + true, + "user-b", + "realm-y", + Map.of() + ); + ApiKey apiKeyInfo3 = createApiKeyInfo( + null, + "id-3", + Instant.ofEpochMilli(100000L), + null, + true, + "user-c", + "realm-z", + Map.of("foo", "bar") + ); GetApiKeyResponse response = new GetApiKeyResponse(Arrays.asList(apiKeyInfo1, apiKeyInfo2, apiKeyInfo3)); XContentBuilder builder = XContentFactory.jsonBuilder(); response.toXContent(builder, ToXContent.EMPTY_PARAMS); - assertThat(Strings.toString(builder), equalTo( + assertThat( + Strings.toString(builder), + equalTo( "{" - + "\"api_keys\":[" - + "{\"id\":\"id-1\",\"name\":\"name1\",\"creation\":100000,\"expiration\":10000000,\"invalidated\":false," - + "\"username\":\"user-a\",\"realm\":\"realm-x\",\"metadata\":{}}," - + "{\"id\":\"id-2\",\"name\":\"name2\",\"creation\":100000,\"expiration\":10000000,\"invalidated\":true," - + "\"username\":\"user-b\",\"realm\":\"realm-y\",\"metadata\":{}}," - + "{\"id\":\"id-3\",\"name\":null,\"creation\":100000,\"invalidated\":true," - + "\"username\":\"user-c\",\"realm\":\"realm-z\",\"metadata\":{\"foo\":\"bar\"}}" - + "]" - + "}")); + + "\"api_keys\":[" + + "{\"id\":\"id-1\",\"name\":\"name1\",\"creation\":100000,\"expiration\":10000000,\"invalidated\":false," + + "\"username\":\"user-a\",\"realm\":\"realm-x\",\"metadata\":{}}," + + "{\"id\":\"id-2\",\"name\":\"name2\",\"creation\":100000,\"expiration\":10000000,\"invalidated\":true," + + "\"username\":\"user-b\",\"realm\":\"realm-y\",\"metadata\":{}}," + + "{\"id\":\"id-3\",\"name\":null,\"creation\":100000,\"invalidated\":true," + + "\"username\":\"user-c\",\"realm\":\"realm-z\",\"metadata\":{\"foo\":\"bar\"}}" + + "]" + + "}" + ) + ); } - private ApiKey createApiKeyInfo(String name, String id, Instant creation, Instant expiration, boolean invalidated, String username, - String realm, Map metadata) { + private ApiKey createApiKeyInfo( + String name, + String id, + Instant creation, + Instant expiration, + boolean invalidated, + String username, + String realm, + Map metadata + ) { return new ApiKey(name, id, creation, expiration, invalidated, username, realm, metadata); } } - diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/InvalidateApiKeyRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/InvalidateApiKeyRequestTests.java index a3e481d043819..4ac16715bb0aa 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/InvalidateApiKeyRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/InvalidateApiKeyRequestTests.java @@ -32,24 +32,32 @@ public class InvalidateApiKeyRequestTests extends ESTestCase { public void testNonNullIdsCannotBeEmptyNorContainBlankId() { - ActionRequestValidationException validationException = - expectThrows(ActionRequestValidationException.class, () -> new InvalidateApiKeyRequest( + ActionRequestValidationException validationException = expectThrows( + ActionRequestValidationException.class, + () -> new InvalidateApiKeyRequest( randomFrom(randomNullOrEmptyString(), randomAlphaOfLength(8)), randomFrom(randomNullOrEmptyString(), randomAlphaOfLength(8)), randomFrom(randomNullOrEmptyString(), randomAlphaOfLength(8)), false, - new String[] {})); + new String[] {} + ) + ); assertThat(validationException.getMessage(), containsString("Field [ids] cannot be an empty array")); - validationException = - expectThrows(ActionRequestValidationException.class, () -> new InvalidateApiKeyRequest( + validationException = expectThrows( + ActionRequestValidationException.class, + () -> new InvalidateApiKeyRequest( randomFrom(randomNullOrEmptyString(), randomAlphaOfLength(8)), randomFrom(randomNullOrEmptyString(), randomAlphaOfLength(8)), randomFrom(randomNullOrEmptyString(), randomAlphaOfLength(8)), false, - new String[] { randomAlphaOfLength(12), null })); - assertThat(validationException.getMessage(), containsString("Field [ids] must not contain blank id, " - + "but got blank id at index position: [1]")); + new String[] { randomAlphaOfLength(12), null } + ) + ); + assertThat( + validationException.getMessage(), + containsString("Field [ids] must not contain blank id, " + "but got blank id at index position: [1]") + ); } public void testEmptyStringsAreCoercedToNull() { @@ -113,7 +121,7 @@ public void writeTo(StreamOutput out) throws IOException { out.writeOptionalString(user); if (out.getVersion().onOrAfter(Version.V_7_10_0)) { if (Strings.hasText(apiKeyId)) { - out.writeOptionalStringArray(new String[]{apiKeyId}); + out.writeOptionalStringArray(new String[] { apiKeyId }); } else { out.writeOptionalStringArray(null); } @@ -125,31 +133,32 @@ public void writeTo(StreamOutput out) throws IOException { } } - String[][] inputs = new String[][]{ - {randomNullOrEmptyString(), randomNullOrEmptyString(), randomNullOrEmptyString(), - randomNullOrEmptyString(), "false"}, - {randomNullOrEmptyString(), "user", "api-kid", "api-kname", "false"}, - {"realm", randomNullOrEmptyString(), "api-kid", "api-kname", "false"}, - {"realm", "user", "api-kid", randomNullOrEmptyString(), "false"}, - {randomNullOrEmptyString(), randomNullOrEmptyString(), "api-kid", "api-kname", "false"}, - {"realm", randomNullOrEmptyString(), randomNullOrEmptyString(), randomNullOrEmptyString(), "true"}, - {randomNullOrEmptyString(), "user", randomNullOrEmptyString(), randomNullOrEmptyString(), "true"}, - }; - String[][] expectedErrorMessages = new String[][]{ - {"One of [api key id(s), api key name, username, realm name] must be specified if [owner] flag is false"}, - {"username or realm name must not be specified when the api key id(s) or api key name are specified", - "only one of [api key id(s), api key name] can be specified"}, - {"username or realm name must not be specified when the api key id(s) or api key name are specified", - "only one of [api key id(s), api key name] can be specified"}, - {"username or realm name must not be specified when the api key id(s) or api key name are specified"}, - {"only one of [api key id(s), api key name] can be specified"}, - {"neither username nor realm-name may be specified when invalidating owned API keys"}, - {"neither username nor realm-name may be specified when invalidating owned API keys"} - }; + String[][] inputs = new String[][] { + { randomNullOrEmptyString(), randomNullOrEmptyString(), randomNullOrEmptyString(), randomNullOrEmptyString(), "false" }, + { randomNullOrEmptyString(), "user", "api-kid", "api-kname", "false" }, + { "realm", randomNullOrEmptyString(), "api-kid", "api-kname", "false" }, + { "realm", "user", "api-kid", randomNullOrEmptyString(), "false" }, + { randomNullOrEmptyString(), randomNullOrEmptyString(), "api-kid", "api-kname", "false" }, + { "realm", randomNullOrEmptyString(), randomNullOrEmptyString(), randomNullOrEmptyString(), "true" }, + { randomNullOrEmptyString(), "user", randomNullOrEmptyString(), randomNullOrEmptyString(), "true" }, }; + String[][] expectedErrorMessages = new String[][] { + { "One of [api key id(s), api key name, username, realm name] must be specified if [owner] flag is false" }, + { + "username or realm name must not be specified when the api key id(s) or api key name are specified", + "only one of [api key id(s), api key name] can be specified" }, + { + "username or realm name must not be specified when the api key id(s) or api key name are specified", + "only one of [api key id(s), api key name] can be specified" }, + { "username or realm name must not be specified when the api key id(s) or api key name are specified" }, + { "only one of [api key id(s), api key name] can be specified" }, + { "neither username nor realm-name may be specified when invalidating owned API keys" }, + { "neither username nor realm-name may be specified when invalidating owned API keys" } }; for (int caseNo = 0; caseNo < inputs.length; caseNo++) { - try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); - OutputStreamStreamOutput osso = new OutputStreamStreamOutput(bos)) { + try ( + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + OutputStreamStreamOutput osso = new OutputStreamStreamOutput(bos) + ) { final Version streamVersion = randomVersionBetween(random(), Version.V_7_4_0, getPreviousVersion(Version.V_7_10_0)); Dummy d = new Dummy(inputs[caseNo]); osso.setVersion(streamVersion); @@ -218,7 +227,8 @@ public void testSerializationWillThrowWhenMultipleIdsAndOldVersionStream() { randomFrom(randomNullOrEmptyString(), randomAlphaOfLength(8)), randomFrom(randomNullOrEmptyString(), randomAlphaOfLength(8)), false, - new String[]{randomAlphaOfLength(12), randomAlphaOfLength(12)}); + new String[] { randomAlphaOfLength(12), randomAlphaOfLength(12) } + ); ByteArrayOutputStream outBuffer = new ByteArrayOutputStream(); OutputStreamStreamOutput out = new OutputStreamStreamOutput(outBuffer); out.setVersion(randomVersionBetween(random(), Version.V_7_4_0, getPreviousVersion(Version.V_7_10_0))); @@ -227,7 +237,7 @@ public void testSerializationWillThrowWhenMultipleIdsAndOldVersionStream() { } private static String randomNullOrEmptyString() { - return randomFrom(new String[]{"", null}); + return randomFrom(new String[] { "", null }); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/InvalidateApiKeyResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/InvalidateApiKeyResponseTests.java index db544fc40743a..60c56f375c966 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/InvalidateApiKeyResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/InvalidateApiKeyResponseTests.java @@ -11,10 +11,10 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.test.ESTestCase; import java.io.IOException; import java.util.Arrays; @@ -26,64 +26,73 @@ public class InvalidateApiKeyResponseTests extends ESTestCase { public void testSerialization() throws IOException { - InvalidateApiKeyResponse response = new InvalidateApiKeyResponse(Arrays.asList("api-key-id-1"), - Arrays.asList("api-key-id-2", "api-key-id-3"), - Arrays.asList(new ElasticsearchException("error1"), - new ElasticsearchException("error2"))); + InvalidateApiKeyResponse response = new InvalidateApiKeyResponse( + Arrays.asList("api-key-id-1"), + Arrays.asList("api-key-id-2", "api-key-id-3"), + Arrays.asList(new ElasticsearchException("error1"), new ElasticsearchException("error2")) + ); try (BytesStreamOutput output = new BytesStreamOutput()) { response.writeTo(output); try (StreamInput input = output.bytes().streamInput()) { InvalidateApiKeyResponse serialized = new InvalidateApiKeyResponse(input); assertThat(serialized.getInvalidatedApiKeys(), equalTo(response.getInvalidatedApiKeys())); - assertThat(serialized.getPreviouslyInvalidatedApiKeys(), - equalTo(response.getPreviouslyInvalidatedApiKeys())); + assertThat(serialized.getPreviouslyInvalidatedApiKeys(), equalTo(response.getPreviouslyInvalidatedApiKeys())); assertThat(serialized.getErrors().size(), equalTo(response.getErrors().size())); assertThat(serialized.getErrors().get(0).toString(), containsString("error1")); assertThat(serialized.getErrors().get(1).toString(), containsString("error2")); } } - response = new InvalidateApiKeyResponse(Arrays.asList(generateRandomStringArray(20, 15, false)), + response = new InvalidateApiKeyResponse( + Arrays.asList(generateRandomStringArray(20, 15, false)), Arrays.asList(generateRandomStringArray(20, 15, false)), - Collections.emptyList()); + Collections.emptyList() + ); try (BytesStreamOutput output = new BytesStreamOutput()) { response.writeTo(output); try (StreamInput input = output.bytes().streamInput()) { InvalidateApiKeyResponse serialized = new InvalidateApiKeyResponse(input); assertThat(serialized.getInvalidatedApiKeys(), equalTo(response.getInvalidatedApiKeys())); - assertThat(serialized.getPreviouslyInvalidatedApiKeys(), - equalTo(response.getPreviouslyInvalidatedApiKeys())); + assertThat(serialized.getPreviouslyInvalidatedApiKeys(), equalTo(response.getPreviouslyInvalidatedApiKeys())); assertThat(serialized.getErrors().size(), equalTo(response.getErrors().size())); } } } public void testToXContent() throws IOException { - InvalidateApiKeyResponse response = new InvalidateApiKeyResponse(Arrays.asList("api-key-id-1"), - Arrays.asList("api-key-id-2", "api-key-id-3"), - Arrays.asList(new ElasticsearchException("error1", new IllegalArgumentException("msg - 1")), - new ElasticsearchException("error2", new IllegalArgumentException("msg - 2")))); + InvalidateApiKeyResponse response = new InvalidateApiKeyResponse( + Arrays.asList("api-key-id-1"), + Arrays.asList("api-key-id-2", "api-key-id-3"), + Arrays.asList( + new ElasticsearchException("error1", new IllegalArgumentException("msg - 1")), + new ElasticsearchException("error2", new IllegalArgumentException("msg - 2")) + ) + ); XContentBuilder builder = XContentFactory.jsonBuilder(); response.toXContent(builder, ToXContent.EMPTY_PARAMS); - assertThat(Strings.toString(builder), - equalTo("{" + - "\"invalidated_api_keys\":[\"api-key-id-1\"]," + - "\"previously_invalidated_api_keys\":[\"api-key-id-2\",\"api-key-id-3\"]," + - "\"error_count\":2," + - "\"error_details\":[" + - "{\"type\":\"exception\"," + - "\"reason\":\"error1\"," + - "\"caused_by\":{" + - "\"type\":\"illegal_argument_exception\"," + - "\"reason\":\"msg - 1\"}" + - "}," + - "{\"type\":\"exception\"," + - "\"reason\":\"error2\"," + - "\"caused_by\":" + - "{\"type\":\"illegal_argument_exception\"," + - "\"reason\":\"msg - 2\"}" + - "}" + - "]" + - "}")); + assertThat( + Strings.toString(builder), + equalTo( + "{" + + "\"invalidated_api_keys\":[\"api-key-id-1\"]," + + "\"previously_invalidated_api_keys\":[\"api-key-id-2\",\"api-key-id-3\"]," + + "\"error_count\":2," + + "\"error_details\":[" + + "{\"type\":\"exception\"," + + "\"reason\":\"error1\"," + + "\"caused_by\":{" + + "\"type\":\"illegal_argument_exception\"," + + "\"reason\":\"msg - 1\"}" + + "}," + + "{\"type\":\"exception\"," + + "\"reason\":\"error2\"," + + "\"caused_by\":" + + "{\"type\":\"illegal_argument_exception\"," + + "\"reason\":\"msg - 2\"}" + + "}" + + "]" + + "}" + ) + ); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/apikey/QueryApiKeyRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/apikey/QueryApiKeyRequestTests.java index d40fc2cca3c64..1d89166c3570c 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/apikey/QueryApiKeyRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/apikey/QueryApiKeyRequestTests.java @@ -67,10 +67,13 @@ public void testReadWrite() throws IOException { QueryBuilders.matchAllQuery(), 42, 20, - List.of(new FieldSortBuilder("name"), + List.of( + new FieldSortBuilder("name"), new FieldSortBuilder("creation_time").setFormat("strict_date_time").order(SortOrder.DESC), - new FieldSortBuilder("username")), - new SearchAfterBuilder().setSortValues(new String[] { "key-2048", "2021-07-01T00:00:59.000Z" })); + new FieldSortBuilder("username") + ), + new SearchAfterBuilder().setSortValues(new String[] { "key-2048", "2021-07-01T00:00:59.000Z" }) + ); try (BytesStreamOutput out = new BytesStreamOutput()) { request3.writeTo(out); try (StreamInput in = new NamedWriteableAwareStreamInput(out.bytes().streamInput(), writableRegistry())) { @@ -85,16 +88,31 @@ public void testReadWrite() throws IOException { } public void testValidate() { - final QueryApiKeyRequest request1 = - new QueryApiKeyRequest(null, randomIntBetween(0, Integer.MAX_VALUE), randomIntBetween(0, Integer.MAX_VALUE), null, null); + final QueryApiKeyRequest request1 = new QueryApiKeyRequest( + null, + randomIntBetween(0, Integer.MAX_VALUE), + randomIntBetween(0, Integer.MAX_VALUE), + null, + null + ); assertThat(request1.validate(), nullValue()); - final QueryApiKeyRequest request2 = - new QueryApiKeyRequest(null, randomIntBetween(Integer.MIN_VALUE, -1), randomIntBetween(0, Integer.MAX_VALUE), null, null); + final QueryApiKeyRequest request2 = new QueryApiKeyRequest( + null, + randomIntBetween(Integer.MIN_VALUE, -1), + randomIntBetween(0, Integer.MAX_VALUE), + null, + null + ); assertThat(request2.validate().getMessage(), containsString("[from] parameter cannot be negative")); - final QueryApiKeyRequest request3 = - new QueryApiKeyRequest(null, randomIntBetween(0, Integer.MAX_VALUE), randomIntBetween(Integer.MIN_VALUE, -1), null, null); + final QueryApiKeyRequest request3 = new QueryApiKeyRequest( + null, + randomIntBetween(0, Integer.MAX_VALUE), + randomIntBetween(Integer.MIN_VALUE, -1), + null, + null + ); assertThat(request3.validate().getMessage(), containsString("[size] parameter cannot be negative")); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/apikey/QueryApiKeyResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/apikey/QueryApiKeyResponseTests.java index 14170e355e349..088e7fa7f35a7 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/apikey/QueryApiKeyResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/apikey/QueryApiKeyResponseTests.java @@ -35,8 +35,7 @@ protected QueryApiKeyResponse createTestInstance() { @Override protected QueryApiKeyResponse mutateInstance(QueryApiKeyResponse instance) throws IOException { - final List items = - Arrays.stream(instance.getItems()).collect(Collectors.toCollection(ArrayList::new)); + final List items = Arrays.stream(instance.getItems()).collect(Collectors.toCollection(ArrayList::new)); switch (randomIntBetween(0, 3)) { case 0: items.add(randomItem()); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/enrollment/KibanaEnrollmentResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/enrollment/KibanaEnrollmentResponseTests.java index 969502510c1ef..54d498b0e5122 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/enrollment/KibanaEnrollmentResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/enrollment/KibanaEnrollmentResponseTests.java @@ -10,11 +10,11 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.settings.SecureString; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.test.AbstractWireSerializingTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.test.AbstractWireSerializingTestCase; import java.io.IOException; import java.util.Map; @@ -23,7 +23,6 @@ public class KibanaEnrollmentResponseTests extends AbstractWireSerializingTestCase { - @Override protected Writeable.Reader instanceReader() { return KibanaEnrollmentResponse::new; @@ -54,11 +53,7 @@ protected KibanaEnrollmentResponse mutateInstance(KibanaEnrollmentResponse insta randomAlphaOfLength(52) ); case 2: - return new KibanaEnrollmentResponse( - randomAlphaOfLengthBetween(14, 20), - instance.getTokenValue(), - randomAlphaOfLength(52) - ); + return new KibanaEnrollmentResponse(randomAlphaOfLengthBetween(14, 20), instance.getTokenValue(), randomAlphaOfLength(52)); case 3: return new KibanaEnrollmentResponse( randomAlphaOfLengthBetween(14, 20), @@ -76,12 +71,21 @@ public void testToXContent() throws IOException { response.toXContent(jsonBuilder, ToXContent.EMPTY_PARAMS); final Map responseMap = XContentHelper.convertToMap( BytesReference.bytes(jsonBuilder), - false, jsonBuilder.contentType()).v2(); + false, + jsonBuilder.contentType() + ).v2(); - assertThat(responseMap, equalTo(Map.of( - "token", Map.of("name", response.getTokenName(), "value", response.getTokenValue().toString()), - "http_ca", response.getHttpCa() - ))); + assertThat( + responseMap, + equalTo( + Map.of( + "token", + Map.of("name", response.getTokenName(), "value", response.getTokenValue().toString()), + "http_ca", + response.getHttpCa() + ) + ) + ); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/enrollment/NodeEnrollementResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/enrollment/NodeEnrollementResponseTests.java index ed0fd62e27cdd..780780390b331 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/enrollment/NodeEnrollementResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/enrollment/NodeEnrollementResponseTests.java @@ -7,12 +7,12 @@ package org.elasticsearch.xpack.core.security.action.enrollment; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; import java.util.List; @@ -37,21 +37,25 @@ public void testSerialization() throws Exception { } } - @Override protected NodeEnrollmentResponse createTestInstance() { + @Override + protected NodeEnrollmentResponse createTestInstance() { return new NodeEnrollmentResponse( randomAlphaOfLengthBetween(50, 100), randomAlphaOfLengthBetween(50, 100), randomAlphaOfLengthBetween(50, 100), randomAlphaOfLengthBetween(50, 100), randomAlphaOfLengthBetween(50, 100), - randomList(10, () -> buildNewFakeTransportAddress().toString())); + randomList(10, () -> buildNewFakeTransportAddress().toString()) + ); } - @Override protected NodeEnrollmentResponse doParseInstance(XContentParser parser) throws IOException { + @Override + protected NodeEnrollmentResponse doParseInstance(XContentParser parser) throws IOException { return PARSER.apply(parser, null); } - @Override protected boolean supportsUnknownFields() { + @Override + protected boolean supportsUnknownFields() { return false; } @@ -63,9 +67,10 @@ public void testSerialization() throws Exception { private static final ParseField NODES_ADDRESSES = new ParseField("nodes_addresses"); @SuppressWarnings("unchecked") - public static final ConstructingObjectParser - PARSER = - new ConstructingObjectParser<>("node_enrollment_response", true, a -> { + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "node_enrollment_response", + true, + a -> { final String httpCaKey = (String) a[0]; final String httpCaCert = (String) a[1]; final String transportCaCert = (String) a[2]; @@ -73,7 +78,8 @@ public void testSerialization() throws Exception { final String transportCert = (String) a[4]; final List nodesAddresses = (List) a[5]; return new NodeEnrollmentResponse(httpCaKey, httpCaCert, transportCaCert, transportKey, transportCert, nodesAddresses); - }); + } + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), HTTP_CA_KEY); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/privilege/DeletePrivilegesRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/privilege/DeletePrivilegesRequestTests.java index 3bda21ca7cb03..b2eddb9b48d0a 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/privilege/DeletePrivilegesRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/privilege/DeletePrivilegesRequestTests.java @@ -24,7 +24,9 @@ public class DeletePrivilegesRequestTests extends ESTestCase { public void testSerialization() throws IOException { final DeletePrivilegesRequest original = new DeletePrivilegesRequest( - randomAlphaOfLengthBetween(3, 8), generateRandomStringArray(5, randomIntBetween(3, 8), false, false)); + randomAlphaOfLengthBetween(3, 8), + generateRandomStringArray(5, randomIntBetween(3, 8), false, false) + ); original.setRefreshPolicy(randomFrom(WriteRequest.RefreshPolicy.values())); final BytesStreamOutput output = new BytesStreamOutput(); @@ -41,14 +43,14 @@ public void testValidation() { assertValidationFailure(new DeletePrivilegesRequest("", null), "application name", "privileges"); assertValidationFailure(new DeletePrivilegesRequest(null, new String[0]), "application name", "privileges"); assertValidationFailure(new DeletePrivilegesRequest("", new String[0]), "application name", "privileges"); - assertValidationFailure(new DeletePrivilegesRequest(null, new String[]{"all"}), "application name"); - assertValidationFailure(new DeletePrivilegesRequest("", new String[]{"all"}), "application name"); + assertValidationFailure(new DeletePrivilegesRequest(null, new String[] { "all" }), "application name"); + assertValidationFailure(new DeletePrivilegesRequest("", new String[] { "all" }), "application name"); assertValidationFailure(new DeletePrivilegesRequest("app", null), "privileges"); assertValidationFailure(new DeletePrivilegesRequest("app", new String[0]), "privileges"); - assertValidationFailure(new DeletePrivilegesRequest("app", new String[]{""}), "privileges"); + assertValidationFailure(new DeletePrivilegesRequest("app", new String[] { "" }), "privileges"); - assertThat(new DeletePrivilegesRequest("app", new String[]{"all"}).validate(), nullValue()); - assertThat(new DeletePrivilegesRequest("app", new String[]{"all", "some"}).validate(), nullValue()); + assertThat(new DeletePrivilegesRequest("app", new String[] { "all" }).validate(), nullValue()); + assertThat(new DeletePrivilegesRequest("app", new String[] { "all", "some" }).validate(), nullValue()); } private void assertValidationFailure(DeletePrivilegesRequest request, String... messages) { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/privilege/DeletePrivilegesResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/privilege/DeletePrivilegesResponseTests.java index d8e9f191346c6..228db267dee49 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/privilege/DeletePrivilegesResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/privilege/DeletePrivilegesResponseTests.java @@ -19,7 +19,8 @@ public class DeletePrivilegesResponseTests extends ESTestCase { public void testSerialization() throws IOException { final DeletePrivilegesResponse original = new DeletePrivilegesResponse( - Arrays.asList(generateRandomStringArray(5, randomIntBetween(3, 8), false, true))); + Arrays.asList(generateRandomStringArray(5, randomIntBetween(3, 8), false, true)) + ); final BytesStreamOutput output = new BytesStreamOutput(); original.writeTo(output); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/privilege/GetPrivilegesRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/privilege/GetPrivilegesRequestTests.java index f9af855b888fb..532f6c01689a7 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/privilege/GetPrivilegesRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/privilege/GetPrivilegesRequestTests.java @@ -45,8 +45,7 @@ public void testValidation() { assertThat(request("my_app", "read", "write").validate(), nullValue()); final ActionRequestValidationException exception = request("my_app", ((String[]) null)).validate(); assertThat(exception, notNullValue()); - assertThat(exception.validationErrors(), - containsInAnyOrder("privileges cannot be null")); + assertThat(exception.validationErrors(), containsInAnyOrder("privileges cannot be null")); } private GetPrivilegesRequest request(String application, String... privileges) { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/privilege/GetPrivilegesResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/privilege/GetPrivilegesResponseTests.java index 1b44f8bf7cbcb..7b01d955bfd0a 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/privilege/GetPrivilegesResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/privilege/GetPrivilegesResponseTests.java @@ -31,8 +31,10 @@ public void testSerialization() throws IOException { } private static GetPrivilegesResponse randomResponse() { - ApplicationPrivilegeDescriptor[] application = randomArray(6, ApplicationPrivilegeDescriptor[]::new, () -> - new ApplicationPrivilegeDescriptor( + ApplicationPrivilegeDescriptor[] application = randomArray( + 6, + ApplicationPrivilegeDescriptor[]::new, + () -> new ApplicationPrivilegeDescriptor( randomAlphaOfLengthBetween(3, 8).toLowerCase(Locale.ROOT), randomAlphaOfLengthBetween(3, 8).toLowerCase(Locale.ROOT), Sets.newHashSet(randomArray(3, String[]::new, () -> randomAlphaOfLength(3).toLowerCase(Locale.ROOT) + "/*")), diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/privilege/PutPrivilegesRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/privilege/PutPrivilegesRequestTests.java index ceca95b5d1c4e..2452aacdc7c52 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/privilege/PutPrivilegesRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/privilege/PutPrivilegesRequestTests.java @@ -27,14 +27,18 @@ public class PutPrivilegesRequestTests extends ESTestCase { public void testSerialization() throws IOException { - final PutPrivilegesRequest original = request(randomArray(8, ApplicationPrivilegeDescriptor[]::new, - () -> new ApplicationPrivilegeDescriptor( - randomAlphaOfLengthBetween(3, 8).toLowerCase(Locale.ROOT), - randomAlphaOfLengthBetween(3, 8).toLowerCase(Locale.ROOT), - Sets.newHashSet(randomArray(3, String[]::new, () -> randomAlphaOfLength(3).toLowerCase(Locale.ROOT) + "/*")), - Collections.emptyMap() + final PutPrivilegesRequest original = request( + randomArray( + 8, + ApplicationPrivilegeDescriptor[]::new, + () -> new ApplicationPrivilegeDescriptor( + randomAlphaOfLengthBetween(3, 8).toLowerCase(Locale.ROOT), + randomAlphaOfLengthBetween(3, 8).toLowerCase(Locale.ROOT), + Sets.newHashSet(randomArray(3, String[]::new, () -> randomAlphaOfLength(3).toLowerCase(Locale.ROOT) + "/*")), + Collections.emptyMap() + ) ) - )); + ); original.setRefreshPolicy(randomFrom(WriteRequest.RefreshPolicy.values())); final BytesStreamOutput out = new BytesStreamOutput(); @@ -62,8 +66,11 @@ public void testValidation() { assertValidationFailure(request(nothing), "Application privileges must have at least one action"); // reserved metadata - final ApplicationPrivilegeDescriptor reservedMetadata = new ApplicationPrivilegeDescriptor("app", "all", - Collections.emptySet(), Collections.singletonMap("_notAllowed", true) + final ApplicationPrivilegeDescriptor reservedMetadata = new ApplicationPrivilegeDescriptor( + "app", + "all", + Collections.emptySet(), + Collections.singletonMap("_notAllowed", true) ); assertValidationFailure(request(reservedMetadata), "metadata keys may not start"); @@ -71,9 +78,13 @@ public void testValidation() { assertValidationFailure(request(badAction), "must contain one of"); // mixed - assertValidationFailure(request(wildcardApp, numericName, reservedMetadata, badAction), - "Application names may not contain", "Application privilege names must match", "metadata keys may not start", - "must contain one of"); + assertValidationFailure( + request(wildcardApp, numericName, reservedMetadata, badAction), + "Application names may not contain", + "Application privilege names must match", + "metadata keys may not start", + "must contain one of" + ); // Empty request assertValidationFailure(new PutPrivilegesRequest(), "At least one application privilege must be provided"); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/privilege/PutPrivilegesResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/privilege/PutPrivilegesResponseTests.java index ece5496e4eec1..9e63fd0a52945 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/privilege/PutPrivilegesResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/privilege/PutPrivilegesResponseTests.java @@ -25,9 +25,7 @@ public void testSerialization() throws IOException { final int applicationCount = randomInt(3); final Map> map = new HashMap<>(applicationCount); for (int i = 0; i < applicationCount; i++) { - map.put(randomAlphaOfLengthBetween(3, 8), - Arrays.asList(generateRandomStringArray(5, 6, false, true)) - ); + map.put(randomAlphaOfLengthBetween(3, 8), Arrays.asList(generateRandomStringArray(5, 6, false, true))); } final PutPrivilegesResponse original = new PutPrivilegesResponse(map); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/role/PutRoleRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/role/PutRoleRequestTests.java index 458cb91929a11..dc77aabbdc075 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/role/PutRoleRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/role/PutRoleRequestTests.java @@ -42,7 +42,7 @@ public class PutRoleRequestTests extends ESTestCase { public void testValidationErrorWithUnknownClusterPrivilegeName() { final PutRoleRequest request = new PutRoleRequest(); request.name(randomAlphaOfLengthBetween(4, 9)); - String unknownClusterPrivilegeName = "unknown_" + randomAlphaOfLengthBetween(3,9); + String unknownClusterPrivilegeName = "unknown_" + randomAlphaOfLengthBetween(3, 9); request.cluster("manage_security", unknownClusterPrivilegeName); // Fail @@ -59,9 +59,15 @@ public void testValidationSuccessWithCorrectClusterPrivilegeName() { public void testValidationErrorWithUnknownIndexPrivilegeName() { final PutRoleRequest request = new PutRoleRequest(); request.name(randomAlphaOfLengthBetween(4, 9)); - String unknownIndexPrivilegeName = "unknown_" + randomAlphaOfLengthBetween(3,9); - request.addIndex(new String[]{randomAlphaOfLength(5)}, new String[]{"index", unknownIndexPrivilegeName}, null, - null, null, randomBoolean()); + String unknownIndexPrivilegeName = "unknown_" + randomAlphaOfLengthBetween(3, 9); + request.addIndex( + new String[] { randomAlphaOfLength(5) }, + new String[] { "index", unknownIndexPrivilegeName }, + null, + null, + null, + randomBoolean() + ); // Fail assertValidationError("unknown index privilege [" + unknownIndexPrivilegeName.toLowerCase(Locale.ROOT) + "]", request); @@ -70,23 +76,37 @@ public void testValidationErrorWithUnknownIndexPrivilegeName() { public void testValidationSuccessWithCorrectIndexPrivilegeName() { final PutRoleRequest request = new PutRoleRequest(); request.name(randomAlphaOfLengthBetween(4, 9)); - request.addIndex(new String[]{randomAlphaOfLength(5)}, new String[]{"index", "write", "indices:data/read"}, null, - null, null, randomBoolean()); + request.addIndex( + new String[] { randomAlphaOfLength(5) }, + new String[] { "index", "write", "indices:data/read" }, + null, + null, + null, + randomBoolean() + ); assertSuccessfulValidation(request); } public void testValidationOfApplicationPrivileges() { - assertSuccessfulValidation(buildRequestWithApplicationPrivilege("app", new String[]{"read"}, new String[]{"*"})); - assertSuccessfulValidation(buildRequestWithApplicationPrivilege("app", new String[]{"action:login"}, new String[]{"/"})); - assertSuccessfulValidation(buildRequestWithApplicationPrivilege("*", new String[]{"data/read:user"}, new String[]{"user/123"})); + assertSuccessfulValidation(buildRequestWithApplicationPrivilege("app", new String[] { "read" }, new String[] { "*" })); + assertSuccessfulValidation(buildRequestWithApplicationPrivilege("app", new String[] { "action:login" }, new String[] { "/" })); + assertSuccessfulValidation( + buildRequestWithApplicationPrivilege("*", new String[] { "data/read:user" }, new String[] { "user/123" }) + ); // Fail - assertValidationError("privilege names and actions must match the pattern", - buildRequestWithApplicationPrivilege("app", new String[]{"in valid"}, new String[]{"*"})); - assertValidationError("An application name prefix must match the pattern", - buildRequestWithApplicationPrivilege("000", new String[]{"all"}, new String[]{"*"})); - assertValidationError("An application name prefix must match the pattern", - buildRequestWithApplicationPrivilege("%*", new String[]{"all"}, new String[]{"*"})); + assertValidationError( + "privilege names and actions must match the pattern", + buildRequestWithApplicationPrivilege("app", new String[] { "in valid" }, new String[] { "*" }) + ); + assertValidationError( + "An application name prefix must match the pattern", + buildRequestWithApplicationPrivilege("000", new String[] { "all" }, new String[] { "*" }) + ); + assertValidationError( + "An application name prefix must match the pattern", + buildRequestWithApplicationPrivilege("%*", new String[] { "all" }, new String[] { "*" }) + ); } public void testSerialization() throws IOException { @@ -127,7 +147,7 @@ private PutRoleRequest buildRequestWithApplicationPrivilege(String appName, Stri .privileges(privileges) .resources(resources) .build(); - request.addApplicationPrivileges(new ApplicationResourcePrivileges[]{privilege}); + request.addApplicationPrivileges(new ApplicationResourcePrivileges[] { privilege }); return request; } @@ -136,8 +156,11 @@ private PutRoleRequest buildRandomRequest() { final PutRoleRequest request = new PutRoleRequest(); request.name(randomAlphaOfLengthBetween(4, 9)); - request.cluster(randomSubsetOf(Arrays.asList("monitor", "manage", "all", "manage_security", "manage_ml", "monitor_watcher")) - .toArray(Strings.EMPTY_ARRAY)); + request.cluster( + randomSubsetOf(Arrays.asList("monitor", "manage", "all", "manage_security", "manage_ml", "monitor_watcher")).toArray( + Strings.EMPTY_ARRAY + ) + ); for (int i = randomIntBetween(0, 4); i > 0; i--) { request.addIndex( @@ -150,8 +173,8 @@ private PutRoleRequest buildRandomRequest() { ); } - final Supplier stringWithInitialLowercase = () - -> randomAlphaOfLength(1).toLowerCase(Locale.ROOT) + randomAlphaOfLengthBetween(3, 12); + final Supplier stringWithInitialLowercase = () -> randomAlphaOfLength(1).toLowerCase(Locale.ROOT) + + randomAlphaOfLengthBetween(3, 12); final ApplicationResourcePrivileges[] applicationPrivileges = new ApplicationResourcePrivileges[randomIntBetween(0, 5)]; for (int i = 0; i < applicationPrivileges.length; i++) { applicationPrivileges[i] = ApplicationResourcePrivileges.builder() diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/saml/SamlCompleteLogoutRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/saml/SamlCompleteLogoutRequestTests.java index b80a6ee6618d9..23ed491021510 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/saml/SamlCompleteLogoutRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/saml/SamlCompleteLogoutRequestTests.java @@ -40,8 +40,10 @@ public void testValidateFailsWhenRealmIsNotSet() { public void testCannotSetQueryStringTwice() { final SamlCompleteLogoutRequest samlCompleteLogoutRequest = new SamlCompleteLogoutRequest(); samlCompleteLogoutRequest.setQueryString("query_string"); - final IllegalArgumentException e = - expectThrows(IllegalArgumentException.class, () -> samlCompleteLogoutRequest.setQueryString("queryString")); + final IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> samlCompleteLogoutRequest.setQueryString("queryString") + ); assertThat(e.getMessage(), containsString("Must use either [query_string] or [queryString], not both at the same time")); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/saml/SamlInvalidateSessionRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/saml/SamlInvalidateSessionRequestTests.java index 0331880b2bce4..adb7364ab092d 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/saml/SamlInvalidateSessionRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/saml/SamlInvalidateSessionRequestTests.java @@ -16,8 +16,10 @@ public class SamlInvalidateSessionRequestTests extends ESTestCase { public void testCannotSetQueryStringTwice() { final SamlInvalidateSessionRequest samlInvalidateSessionRequest = new SamlInvalidateSessionRequest(); samlInvalidateSessionRequest.setQueryString("query_string"); - final IllegalArgumentException e = - expectThrows(IllegalArgumentException.class, () -> samlInvalidateSessionRequest.setQueryString("queryString")); + final IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> samlInvalidateSessionRequest.setQueryString("queryString") + ); assertThat(e.getMessage(), containsString("Must use either [query_string] or [queryString], not both at the same time")); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/saml/SamlSpMetadataRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/saml/SamlSpMetadataRequestTests.java index d3fe6205d8d63..b825e070208ad 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/saml/SamlSpMetadataRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/saml/SamlSpMetadataRequestTests.java @@ -24,7 +24,7 @@ public void testValidateFailsWhenRealmEmpty() { assertThat(validationException.getMessage(), containsString("Realm name may not be empty")); } - public void testValidateSerialization() throws IOException { + public void testValidateSerialization() throws IOException { final SamlSpMetadataRequest samlSPMetadataRequest = new SamlSpMetadataRequest("saml1"); try (BytesStreamOutput out = new BytesStreamOutput()) { samlSPMetadataRequest.writeTo(out); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/service/CreateServiceAccountTokenRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/service/CreateServiceAccountTokenRequestTests.java index 85e66dc0b94fc..5b88dc86ba4fc 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/service/CreateServiceAccountTokenRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/service/CreateServiceAccountTokenRequestTests.java @@ -29,7 +29,8 @@ public void testReadWrite() throws IOException { final CreateServiceAccountTokenRequest request = new CreateServiceAccountTokenRequest( randomAlphaOfLengthBetween(3, 8), randomAlphaOfLengthBetween(3, 8), - randomAlphaOfLengthBetween(3, 8)); + randomAlphaOfLengthBetween(3, 8) + ); try (BytesStreamOutput out = new BytesStreamOutput()) { request.writeTo(out); try (StreamInput in = new InputStreamStreamInput(new ByteArrayInputStream(out.bytes().array()))) { @@ -43,22 +44,29 @@ public void testValidation() { final String serviceName = randomAlphaOfLengthBetween(3, 8); final String tokenName = ValidationTests.randomTokenName(); - final CreateServiceAccountTokenRequest request1 = - new CreateServiceAccountTokenRequest(randomFrom("", null), serviceName, tokenName); + final CreateServiceAccountTokenRequest request1 = new CreateServiceAccountTokenRequest( + randomFrom("", null), + serviceName, + tokenName + ); final ActionRequestValidationException validation1 = request1.validate(); assertThat(validation1.validationErrors(), contains(containsString("namespace is required"))); - final CreateServiceAccountTokenRequest request2 = - new CreateServiceAccountTokenRequest(namespace, randomFrom("", null), tokenName); + final CreateServiceAccountTokenRequest request2 = new CreateServiceAccountTokenRequest(namespace, randomFrom("", null), tokenName); final ActionRequestValidationException validation2 = request2.validate(); assertThat(validation2.validationErrors(), contains(containsString("service-name is required"))); - final CreateServiceAccountTokenRequest request3 = - new CreateServiceAccountTokenRequest(namespace, serviceName, ValidationTests.randomInvalidTokenName()); + final CreateServiceAccountTokenRequest request3 = new CreateServiceAccountTokenRequest( + namespace, + serviceName, + ValidationTests.randomInvalidTokenName() + ); final ActionRequestValidationException validation3 = request3.validate(); assertThat(validation3.validationErrors(), contains(containsString(Validation.INVALID_SERVICE_ACCOUNT_TOKEN_NAME_MESSAGE))); - assertThat(validation3.validationErrors(), - contains(containsString("invalid service token name [" + request3.getTokenName() + "]"))); + assertThat( + validation3.validationErrors(), + contains(containsString("invalid service token name [" + request3.getTokenName() + "]")) + ); final CreateServiceAccountTokenRequest request4 = new CreateServiceAccountTokenRequest(namespace, serviceName, tokenName); final ActionRequestValidationException validation4 = request4.validate(); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/service/CreateServiceAccountTokenResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/service/CreateServiceAccountTokenResponseTests.java index 3b5554c5460b9..a43c47fcb132b 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/service/CreateServiceAccountTokenResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/service/CreateServiceAccountTokenResponseTests.java @@ -10,11 +10,11 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.settings.SecureString; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.test.AbstractWireSerializingTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.test.AbstractWireSerializingTestCase; import java.io.IOException; import java.util.Map; @@ -31,17 +31,23 @@ protected Writeable.Reader instanceReader() { @Override protected CreateServiceAccountTokenResponse createTestInstance() { return CreateServiceAccountTokenResponse.created( - randomAlphaOfLengthBetween(3, 8), new SecureString(randomAlphaOfLength(20).toCharArray())); + randomAlphaOfLengthBetween(3, 8), + new SecureString(randomAlphaOfLength(20).toCharArray()) + ); } @Override protected CreateServiceAccountTokenResponse mutateInstance(CreateServiceAccountTokenResponse instance) throws IOException { if (randomBoolean()) { return CreateServiceAccountTokenResponse.created( - randomValueOtherThan(instance.getName(), () -> randomAlphaOfLengthBetween(3, 8)), instance.getValue()); + randomValueOtherThan(instance.getName(), () -> randomAlphaOfLengthBetween(3, 8)), + instance.getValue() + ); } else { - return CreateServiceAccountTokenResponse.created(instance.getName(), - randomValueOtherThan(instance.getValue(), () -> new SecureString(randomAlphaOfLength(22).toCharArray()))); + return CreateServiceAccountTokenResponse.created( + instance.getName(), + randomValueOtherThan(instance.getValue(), () -> new SecureString(randomAlphaOfLength(22).toCharArray())) + ); } } @@ -49,13 +55,12 @@ public void testToXContent() throws IOException { final CreateServiceAccountTokenResponse response = createTestInstance(); XContentBuilder builder = XContentFactory.jsonBuilder(); response.toXContent(builder, ToXContent.EMPTY_PARAMS); - final Map responseMap = XContentHelper.convertToMap( - BytesReference.bytes(builder), - false, builder.contentType()).v2(); - - assertThat(responseMap, equalTo(Map.of( - "created", true, - "token", Map.of("name", response.getName(), "value", response.getValue().toString()) - ))); + final Map responseMap = XContentHelper.convertToMap(BytesReference.bytes(builder), false, builder.contentType()) + .v2(); + + assertThat( + responseMap, + equalTo(Map.of("created", true, "token", Map.of("name", response.getName(), "value", response.getValue().toString()))) + ); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/service/DeleteServiceAccountTokenRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/service/DeleteServiceAccountTokenRequestTests.java index 68c7198d566b2..29b0eaaedd77c 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/service/DeleteServiceAccountTokenRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/service/DeleteServiceAccountTokenRequestTests.java @@ -29,7 +29,10 @@ protected Writeable.Reader instanceReader() { @Override protected DeleteServiceAccountTokenRequest createTestInstance() { return new DeleteServiceAccountTokenRequest( - randomAlphaOfLengthBetween(3, 8), randomAlphaOfLengthBetween(3, 8), randomAlphaOfLengthBetween(3, 8)); + randomAlphaOfLengthBetween(3, 8), + randomAlphaOfLengthBetween(3, 8), + randomAlphaOfLengthBetween(3, 8) + ); } @Override @@ -38,18 +41,23 @@ protected DeleteServiceAccountTokenRequest mutateInstance(DeleteServiceAccountTo if (randomBoolean()) { newInstance = new DeleteServiceAccountTokenRequest( randomValueOtherThan(newInstance.getNamespace(), () -> randomAlphaOfLengthBetween(3, 8)), - newInstance.getServiceName(), newInstance.getTokenName()); + newInstance.getServiceName(), + newInstance.getTokenName() + ); } if (randomBoolean()) { newInstance = new DeleteServiceAccountTokenRequest( newInstance.getNamespace(), randomValueOtherThan(newInstance.getServiceName(), () -> randomAlphaOfLengthBetween(3, 8)), - newInstance.getTokenName()); + newInstance.getTokenName() + ); } if (newInstance == instance || randomBoolean()) { newInstance = new DeleteServiceAccountTokenRequest( - newInstance.getNamespace(), newInstance.getServiceName(), - randomValueOtherThan(newInstance.getTokenName(), () -> randomAlphaOfLengthBetween(3, 8))); + newInstance.getNamespace(), + newInstance.getServiceName(), + randomValueOtherThan(newInstance.getTokenName(), () -> randomAlphaOfLengthBetween(3, 8)) + ); } return newInstance; } @@ -59,22 +67,29 @@ public void testValidation() { final String serviceName = randomAlphaOfLengthBetween(3, 8); final String tokenName = ValidationTests.randomTokenName(); - final CreateServiceAccountTokenRequest request1 = - new CreateServiceAccountTokenRequest(randomFrom("", null), serviceName, tokenName); + final CreateServiceAccountTokenRequest request1 = new CreateServiceAccountTokenRequest( + randomFrom("", null), + serviceName, + tokenName + ); final ActionRequestValidationException validation1 = request1.validate(); assertThat(validation1.validationErrors(), contains(containsString("namespace is required"))); - final CreateServiceAccountTokenRequest request2 = - new CreateServiceAccountTokenRequest(namespace, randomFrom("", null), tokenName); + final CreateServiceAccountTokenRequest request2 = new CreateServiceAccountTokenRequest(namespace, randomFrom("", null), tokenName); final ActionRequestValidationException validation2 = request2.validate(); assertThat(validation2.validationErrors(), contains(containsString("service-name is required"))); - final CreateServiceAccountTokenRequest request3 = - new CreateServiceAccountTokenRequest(namespace, serviceName, ValidationTests.randomInvalidTokenName()); + final CreateServiceAccountTokenRequest request3 = new CreateServiceAccountTokenRequest( + namespace, + serviceName, + ValidationTests.randomInvalidTokenName() + ); final ActionRequestValidationException validation3 = request3.validate(); assertThat(validation3.validationErrors(), contains(containsString(Validation.INVALID_SERVICE_ACCOUNT_TOKEN_NAME_MESSAGE))); - assertThat(validation3.validationErrors(), - contains(containsString("invalid service token name [" + request3.getTokenName() + "]"))); + assertThat( + validation3.validationErrors(), + contains(containsString("invalid service token name [" + request3.getTokenName() + "]")) + ); final CreateServiceAccountTokenRequest request4 = new CreateServiceAccountTokenRequest(namespace, serviceName, tokenName); final ActionRequestValidationException validation4 = request4.validate(); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/service/DeleteServiceAccountTokenResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/service/DeleteServiceAccountTokenResponseTests.java index aa3123eb620f6..8a0b1db5a4672 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/service/DeleteServiceAccountTokenResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/service/DeleteServiceAccountTokenResponseTests.java @@ -9,11 +9,11 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.test.AbstractWireSerializingTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.test.AbstractWireSerializingTestCase; import java.io.IOException; import java.util.Map; @@ -41,9 +41,8 @@ public void testToXContent() throws IOException { final DeleteServiceAccountTokenResponse response = createTestInstance(); XContentBuilder builder = XContentFactory.jsonBuilder(); response.toXContent(builder, ToXContent.EMPTY_PARAMS); - final Map responseMap = XContentHelper.convertToMap( - BytesReference.bytes(builder), - false, builder.contentType()).v2(); + final Map responseMap = XContentHelper.convertToMap(BytesReference.bytes(builder), false, builder.contentType()) + .v2(); assertThat(responseMap, equalTo(Map.of("found", response.found()))); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/service/GetServiceAccountCredentialsRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/service/GetServiceAccountCredentialsRequestTests.java index 989e966b1afa0..a1ecb0a45e5b3 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/service/GetServiceAccountCredentialsRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/service/GetServiceAccountCredentialsRequestTests.java @@ -34,27 +34,36 @@ protected GetServiceAccountCredentialsRequest mutateInstance(GetServiceAccountCr switch (randomIntBetween(0, 2)) { case 0: return new GetServiceAccountCredentialsRequest( - randomValueOtherThan(instance.getNamespace(), () -> randomAlphaOfLengthBetween(3, 8)), instance.getServiceName()); + randomValueOtherThan(instance.getNamespace(), () -> randomAlphaOfLengthBetween(3, 8)), + instance.getServiceName() + ); case 1: return new GetServiceAccountCredentialsRequest( - instance.getNamespace(), randomValueOtherThan(instance.getServiceName(), () -> randomAlphaOfLengthBetween(3, 8))); + instance.getNamespace(), + randomValueOtherThan(instance.getServiceName(), () -> randomAlphaOfLengthBetween(3, 8)) + ); default: return new GetServiceAccountCredentialsRequest( randomValueOtherThan(instance.getNamespace(), () -> randomAlphaOfLengthBetween(3, 8)), - randomValueOtherThan(instance.getServiceName(), () -> randomAlphaOfLengthBetween(3, 8))); + randomValueOtherThan(instance.getServiceName(), () -> randomAlphaOfLengthBetween(3, 8)) + ); } } public void testValidate() { assertNull(createTestInstance().validate()); - final GetServiceAccountCredentialsRequest request1 = - new GetServiceAccountCredentialsRequest(randomFrom("", null), randomAlphaOfLengthBetween(3, 8)); + final GetServiceAccountCredentialsRequest request1 = new GetServiceAccountCredentialsRequest( + randomFrom("", null), + randomAlphaOfLengthBetween(3, 8) + ); final ActionRequestValidationException e1 = request1.validate(); assertThat(e1.getMessage(), containsString("service account namespace is required")); - final GetServiceAccountCredentialsRequest request2 = - new GetServiceAccountCredentialsRequest(randomAlphaOfLengthBetween(3, 8), randomFrom("", null)); + final GetServiceAccountCredentialsRequest request2 = new GetServiceAccountCredentialsRequest( + randomAlphaOfLengthBetween(3, 8), + randomFrom("", null) + ); final ActionRequestValidationException e2 = request2.validate(); assertThat(e2.getMessage(), containsString("service account service-name is required")); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/service/GetServiceAccountCredentialsResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/service/GetServiceAccountCredentialsResponseTests.java index 747b7d73df29e..7f4d781b85397 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/service/GetServiceAccountCredentialsResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/service/GetServiceAccountCredentialsResponseTests.java @@ -14,11 +14,11 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.transport.TransportAddress; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.test.ESTestCase; import java.io.IOException; import java.nio.file.NoSuchFileException; @@ -65,8 +65,8 @@ public void testToXContent() throws IOException { XContentBuilder builder = XContentFactory.jsonBuilder(); response.toXContent(builder, ToXContent.EMPTY_PARAMS); - final Map responseMap = XContentHelper.convertToMap(BytesReference.bytes(builder), - false, builder.contentType()).v2(); + final Map responseMap = XContentHelper.convertToMap(BytesReference.bytes(builder), false, builder.contentType()) + .v2(); assertThat(responseMap.get("service_account"), equalTo(response.getPrincipal())); assertThat(responseMap.get("count"), equalTo(tokenInfos.size())); @@ -121,10 +121,12 @@ private GetServiceAccountCredentialsNodesResponse.Node randomNodeResponse(String final DiscoveryNode discoveryNode = new DiscoveryNode( randomAlphaOfLength(8) + i, new TransportAddress(TransportAddress.META_ADDRESS, 9300), - Version.CURRENT); + Version.CURRENT + ); return new GetServiceAccountCredentialsNodesResponse.Node( discoveryNode, - randomSubsetOf(randomIntBetween(0, tokenNames.length), tokenNames).toArray(String[]::new)); + randomSubsetOf(randomIntBetween(0, tokenNames.length), tokenNames).toArray(String[]::new) + ); } private List getAllTokenInfos(GetServiceAccountCredentialsResponse response) { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/service/GetServiceAccountRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/service/GetServiceAccountRequestTests.java index 08f6cc4ee730b..fa3fe264575bd 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/service/GetServiceAccountRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/service/GetServiceAccountRequestTests.java @@ -21,8 +21,10 @@ protected Writeable.Reader instanceReader() { @Override protected GetServiceAccountRequest createTestInstance() { - return new GetServiceAccountRequest(randomFrom(randomAlphaOfLengthBetween(3, 8), null), - randomFrom(randomAlphaOfLengthBetween(3, 8), null)); + return new GetServiceAccountRequest( + randomFrom(randomAlphaOfLengthBetween(3, 8), null), + randomFrom(randomAlphaOfLengthBetween(3, 8), null) + ); } @Override @@ -30,11 +32,13 @@ protected GetServiceAccountRequest mutateInstance(GetServiceAccountRequest insta if (randomBoolean()) { return new GetServiceAccountRequest( randomValueOtherThan(instance.getNamespace(), () -> randomFrom(randomAlphaOfLengthBetween(3, 8), null)), - instance.getServiceName()); + instance.getServiceName() + ); } else { return new GetServiceAccountRequest( instance.getNamespace(), - randomValueOtherThan(instance.getServiceName(), () -> randomFrom(randomAlphaOfLengthBetween(3, 8), null))); + randomValueOtherThan(instance.getServiceName(), () -> randomFrom(randomAlphaOfLengthBetween(3, 8), null)) + ); } } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/service/GetServiceAccountResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/service/GetServiceAccountResponseTests.java index 7520631a6ef67..06ae2384fc911 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/service/GetServiceAccountResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/service/GetServiceAccountResponseTests.java @@ -9,13 +9,13 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.test.AbstractWireSerializingTestCase; +import org.elasticsearch.test.XContentTestUtils; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.AbstractWireSerializingTestCase; -import org.elasticsearch.test.XContentTestUtils; import org.elasticsearch.xpack.core.security.authz.RoleDescriptor; import java.io.IOException; @@ -34,17 +34,20 @@ protected Writeable.Reader instanceReader() { @Override protected GetServiceAccountResponse createTestInstance() { final String principal = randomPrincipal(); - return new GetServiceAccountResponse(randomBoolean() - ? new ServiceAccountInfo[]{new ServiceAccountInfo(principal, getRoleDescriptorFor(principal))} - : new ServiceAccountInfo[0]); + return new GetServiceAccountResponse( + randomBoolean() + ? new ServiceAccountInfo[] { new ServiceAccountInfo(principal, getRoleDescriptorFor(principal)) } + : new ServiceAccountInfo[0] + ); } @Override protected GetServiceAccountResponse mutateInstance(GetServiceAccountResponse instance) throws IOException { if (instance.getServiceAccountInfos().length == 0) { final String principal = randomPrincipal(); - return new GetServiceAccountResponse(new ServiceAccountInfo[]{ - new ServiceAccountInfo(principal, getRoleDescriptorFor(principal))}); + return new GetServiceAccountResponse( + new ServiceAccountInfo[] { new ServiceAccountInfo(principal, getRoleDescriptorFor(principal)) } + ); } else { return new GetServiceAccountResponse(new ServiceAccountInfo[0]); } @@ -55,9 +58,8 @@ public void testToXContent() throws IOException { final GetServiceAccountResponse response = createTestInstance(); XContentBuilder builder = XContentFactory.jsonBuilder(); response.toXContent(builder, ToXContent.EMPTY_PARAMS); - final Map responseMap = XContentHelper.convertToMap( - BytesReference.bytes(builder), - false, builder.contentType()).v2(); + final Map responseMap = XContentHelper.convertToMap(BytesReference.bytes(builder), false, builder.contentType()) + .v2(); final ServiceAccountInfo[] serviceAccountInfos = response.getServiceAccountInfos(); if (serviceAccountInfos.length == 0) { assertThat(responseMap, anEmptyMap()); @@ -75,24 +77,33 @@ private String randomPrincipal() { } private RoleDescriptor getRoleDescriptorFor(String name) { - return new RoleDescriptor(name, + return new RoleDescriptor( + name, new String[] { "monitor", "manage_own_api_key" }, new RoleDescriptor.IndicesPrivileges[] { - RoleDescriptor.IndicesPrivileges.builder() - .indices("logs-*", "metrics-*", "traces-*") - .privileges("write", "create_index", "auto_configure").build() }, + RoleDescriptor.IndicesPrivileges.builder() + .indices("logs-*", "metrics-*", "traces-*") + .privileges("write", "create_index", "auto_configure") + .build() }, null, null, null, null, - null); + null + ); } private void assertRoleDescriptorEquals(Map responseFragment, RoleDescriptor roleDescriptor) throws IOException { @SuppressWarnings("unchecked") final Map descriptorMap = (Map) responseFragment.get("role_descriptor"); - assertThat(RoleDescriptor.parse(roleDescriptor.getName(), - XContentTestUtils.convertToXContent(descriptorMap, XContentType.JSON), false, XContentType.JSON), - equalTo(roleDescriptor)); + assertThat( + RoleDescriptor.parse( + roleDescriptor.getName(), + XContentTestUtils.convertToXContent(descriptorMap, XContentType.JSON), + false, + XContentType.JSON + ), + equalTo(roleDescriptor) + ); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/token/CreateTokenResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/token/CreateTokenResponseTests.java index b9231e9674429..790d497a3e6dd 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/token/CreateTokenResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/token/CreateTokenResponseTests.java @@ -16,10 +16,18 @@ public class CreateTokenResponseTests extends ESTestCase { public void testSerialization() throws Exception { - CreateTokenResponse response = new CreateTokenResponse(randomAlphaOfLengthBetween(1, 10), TimeValue.timeValueMinutes(20L), - randomBoolean() ? null : "FULL", randomAlphaOfLengthBetween(1, 10), randomBoolean() ? null :randomAlphaOfLengthBetween(1, 10), - new Authentication(new User("joe", new String[]{"custom_superuser"}, new User("bar", "not_superuser")), - new Authentication.RealmRef("test", "test", "node"), new Authentication.RealmRef("test", "test", "node"))); + CreateTokenResponse response = new CreateTokenResponse( + randomAlphaOfLengthBetween(1, 10), + TimeValue.timeValueMinutes(20L), + randomBoolean() ? null : "FULL", + randomAlphaOfLengthBetween(1, 10), + randomBoolean() ? null : randomAlphaOfLengthBetween(1, 10), + new Authentication( + new User("joe", new String[] { "custom_superuser" }, new User("bar", "not_superuser")), + new Authentication.RealmRef("test", "test", "node"), + new Authentication.RealmRef("test", "test", "node") + ) + ); try (BytesStreamOutput output = new BytesStreamOutput()) { response.writeTo(output); try (StreamInput input = output.bytes().streamInput()) { @@ -28,10 +36,18 @@ public void testSerialization() throws Exception { } } - response = new CreateTokenResponse(randomAlphaOfLengthBetween(1, 10), TimeValue.timeValueMinutes(20L), - randomBoolean() ? null : "FULL", null, null, - new Authentication(new User("joe", new String[]{"custom_superuser"}, new User("bar", "not_superuser")), - new Authentication.RealmRef("test", "test", "node"), new Authentication.RealmRef("test", "test", "node"))); + response = new CreateTokenResponse( + randomAlphaOfLengthBetween(1, 10), + TimeValue.timeValueMinutes(20L), + randomBoolean() ? null : "FULL", + null, + null, + new Authentication( + new User("joe", new String[] { "custom_superuser" }, new User("bar", "not_superuser")), + new Authentication.RealmRef("test", "test", "node"), + new Authentication.RealmRef("test", "test", "node") + ) + ); try (BytesStreamOutput output = new BytesStreamOutput()) { response.writeTo(output); try (StreamInput input = output.bytes().streamInput()) { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/token/InvalidateTokenRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/token/InvalidateTokenRequestTests.java index 3c771c7773a32..b9408a4d8fbf7 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/token/InvalidateTokenRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/token/InvalidateTokenRequestTests.java @@ -36,48 +36,67 @@ public void testValidation() { ve = request.validate(); assertNull(ve); - request = - new InvalidateTokenRequest(randomAlphaOfLength(4), randomFrom("", null), randomAlphaOfLength(4), randomAlphaOfLength(8)); + request = new InvalidateTokenRequest(randomAlphaOfLength(4), randomFrom("", null), randomAlphaOfLength(4), randomAlphaOfLength(8)); ve = request.validate(); assertNotNull(ve); assertEquals(1, ve.validationErrors().size()); - assertThat(ve.validationErrors().get(0), - containsString("token string must not be provided when realm name or username is specified")); + assertThat( + ve.validationErrors().get(0), + containsString("token string must not be provided when realm name or username is specified") + ); - request = new InvalidateTokenRequest(randomAlphaOfLength(4), randomFrom("token", "refresh_token"), - randomAlphaOfLength(4), randomAlphaOfLength(8)); + request = new InvalidateTokenRequest( + randomAlphaOfLength(4), + randomFrom("token", "refresh_token"), + randomAlphaOfLength(4), + randomAlphaOfLength(8) + ); ve = request.validate(); assertNotNull(ve); assertEquals(2, ve.validationErrors().size()); - assertThat(ve.validationErrors().get(0), - containsString("token string must not be provided when realm name or username is specified")); - assertThat(ve.validationErrors().get(1), - containsString("token type must not be provided when realm name or username is specified")); + assertThat( + ve.validationErrors().get(0), + containsString("token string must not be provided when realm name or username is specified") + ); + assertThat( + ve.validationErrors().get(1), + containsString("token type must not be provided when realm name or username is specified") + ); - request = - new InvalidateTokenRequest(randomAlphaOfLength(4), randomFrom("", null), randomAlphaOfLength(4), randomAlphaOfLength(8)); + request = new InvalidateTokenRequest(randomAlphaOfLength(4), randomFrom("", null), randomAlphaOfLength(4), randomAlphaOfLength(8)); ve = request.validate(); assertNotNull(ve); assertEquals(1, ve.validationErrors().size()); - assertThat(ve.validationErrors().get(0), - containsString("token string must not be provided when realm name or username is specified")); + assertThat( + ve.validationErrors().get(0), + containsString("token string must not be provided when realm name or username is specified") + ); - request = - new InvalidateTokenRequest(randomAlphaOfLength(4), randomFrom("token", "refresh_token"), randomFrom("", null), - randomAlphaOfLength(8)); + request = new InvalidateTokenRequest( + randomAlphaOfLength(4), + randomFrom("token", "refresh_token"), + randomFrom("", null), + randomAlphaOfLength(8) + ); ve = request.validate(); assertNotNull(ve); assertEquals(2, ve.validationErrors().size()); - assertThat(ve.validationErrors().get(0), - containsString("token string must not be provided when realm name or username is specified")); - assertThat(ve.validationErrors().get(1), - containsString("token type must not be provided when realm name or username is specified")); + assertThat( + ve.validationErrors().get(0), + containsString("token string must not be provided when realm name or username is specified") + ); + assertThat( + ve.validationErrors().get(1), + containsString("token type must not be provided when realm name or username is specified") + ); request = new InvalidateTokenRequest(randomAlphaOfLength(4), randomFrom("", null), randomFrom("", null), randomAlphaOfLength(8)); ve = request.validate(); assertNotNull(ve); assertEquals(1, ve.validationErrors().size()); - assertThat(ve.validationErrors().get(0), - containsString("token string must not be provided when realm name or username is specified")); + assertThat( + ve.validationErrors().get(0), + containsString("token string must not be provided when realm name or username is specified") + ); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/token/InvalidateTokenResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/token/InvalidateTokenResponseTests.java index 66800b531aa76..fcfac6cc297fc 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/token/InvalidateTokenResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/token/InvalidateTokenResponseTests.java @@ -10,11 +10,11 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.rest.RestStatus; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.security.authc.support.TokensInvalidationResult; import java.io.IOException; @@ -28,35 +28,47 @@ public class InvalidateTokenResponseTests extends ESTestCase { public void testSerialization() throws IOException { - TokensInvalidationResult result = new TokensInvalidationResult(Arrays.asList(generateRandomStringArray(20, 15, false)), + TokensInvalidationResult result = new TokensInvalidationResult( Arrays.asList(generateRandomStringArray(20, 15, false)), - Arrays.asList(new ElasticsearchException("foo", new IllegalArgumentException("this is an error message")), - new ElasticsearchException("bar", new IllegalArgumentException("this is an error message2"))), - RestStatus.OK); + Arrays.asList(generateRandomStringArray(20, 15, false)), + Arrays.asList( + new ElasticsearchException("foo", new IllegalArgumentException("this is an error message")), + new ElasticsearchException("bar", new IllegalArgumentException("this is an error message2")) + ), + RestStatus.OK + ); InvalidateTokenResponse response = new InvalidateTokenResponse(result); try (BytesStreamOutput output = new BytesStreamOutput()) { response.writeTo(output); try (StreamInput input = output.bytes().streamInput()) { InvalidateTokenResponse serialized = new InvalidateTokenResponse(input); assertThat(serialized.getResult().getInvalidatedTokens(), equalTo(response.getResult().getInvalidatedTokens())); - assertThat(serialized.getResult().getPreviouslyInvalidatedTokens(), - equalTo(response.getResult().getPreviouslyInvalidatedTokens())); + assertThat( + serialized.getResult().getPreviouslyInvalidatedTokens(), + equalTo(response.getResult().getPreviouslyInvalidatedTokens()) + ); assertThat(serialized.getResult().getErrors().size(), equalTo(response.getResult().getErrors().size())); assertThat(serialized.getResult().getErrors().get(0).getCause().getMessage(), containsString("this is an error message")); assertThat(serialized.getResult().getErrors().get(1).getCause().getMessage(), containsString("this is an error message2")); } } - result = new TokensInvalidationResult(Arrays.asList(generateRandomStringArray(20, 15, false)), - Arrays.asList(generateRandomStringArray(20, 15, false)), Collections.emptyList(), RestStatus.OK); + result = new TokensInvalidationResult( + Arrays.asList(generateRandomStringArray(20, 15, false)), + Arrays.asList(generateRandomStringArray(20, 15, false)), + Collections.emptyList(), + RestStatus.OK + ); response = new InvalidateTokenResponse(result); try (BytesStreamOutput output = new BytesStreamOutput()) { response.writeTo(output); try (StreamInput input = output.bytes().streamInput()) { InvalidateTokenResponse serialized = new InvalidateTokenResponse(input); assertThat(serialized.getResult().getInvalidatedTokens(), equalTo(response.getResult().getInvalidatedTokens())); - assertThat(serialized.getResult().getPreviouslyInvalidatedTokens(), - equalTo(response.getResult().getPreviouslyInvalidatedTokens())); + assertThat( + serialized.getResult().getPreviouslyInvalidatedTokens(), + equalTo(response.getResult().getPreviouslyInvalidatedTokens()) + ); assertThat(serialized.getResult().getErrors().size(), equalTo(response.getResult().getErrors().size())); } } @@ -65,31 +77,45 @@ public void testSerialization() throws IOException { public void testToXContent() throws IOException { List invalidatedTokens = Arrays.asList(generateRandomStringArray(20, 15, false)); List previouslyInvalidatedTokens = Arrays.asList(generateRandomStringArray(20, 15, false)); - TokensInvalidationResult result = new TokensInvalidationResult(invalidatedTokens, previouslyInvalidatedTokens, - Arrays.asList(new ElasticsearchException("foo", new IllegalArgumentException("this is an error message")), - new ElasticsearchException("bar", new IllegalArgumentException("this is an error message2"))), RestStatus.OK); + TokensInvalidationResult result = new TokensInvalidationResult( + invalidatedTokens, + previouslyInvalidatedTokens, + Arrays.asList( + new ElasticsearchException("foo", new IllegalArgumentException("this is an error message")), + new ElasticsearchException("bar", new IllegalArgumentException("this is an error message2")) + ), + RestStatus.OK + ); InvalidateTokenResponse response = new InvalidateTokenResponse(result); XContentBuilder builder = XContentFactory.jsonBuilder(); response.toXContent(builder, ToXContent.EMPTY_PARAMS); - assertThat(Strings.toString(builder), - equalTo("{" + - "\"invalidated_tokens\":" + invalidatedTokens.size() + "," + - "\"previously_invalidated_tokens\":" + previouslyInvalidatedTokens.size() + "," + - "\"error_count\":2," + - "\"error_details\":[" + - "{\"type\":\"exception\"," + - "\"reason\":\"foo\"," + - "\"caused_by\":{" + - "\"type\":\"illegal_argument_exception\"," + - "\"reason\":\"this is an error message\"}" + - "}," + - "{\"type\":\"exception\"," + - "\"reason\":\"bar\"," + - "\"caused_by\":" + - "{\"type\":\"illegal_argument_exception\"," + - "\"reason\":\"this is an error message2\"}" + - "}" + - "]" + - "}")); + assertThat( + Strings.toString(builder), + equalTo( + "{" + + "\"invalidated_tokens\":" + + invalidatedTokens.size() + + "," + + "\"previously_invalidated_tokens\":" + + previouslyInvalidatedTokens.size() + + "," + + "\"error_count\":2," + + "\"error_details\":[" + + "{\"type\":\"exception\"," + + "\"reason\":\"foo\"," + + "\"caused_by\":{" + + "\"type\":\"illegal_argument_exception\"," + + "\"reason\":\"this is an error message\"}" + + "}," + + "{\"type\":\"exception\"," + + "\"reason\":\"bar\"," + + "\"caused_by\":" + + "{\"type\":\"illegal_argument_exception\"," + + "\"reason\":\"this is an error message2\"}" + + "}" + + "]" + + "}" + ) + ); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/user/GetUserPrivilegesResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/user/GetUserPrivilegesResponseTests.java index 8550ee9514103..14f9e8d7a9c7c 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/user/GetUserPrivilegesResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/user/GetUserPrivilegesResponseTests.java @@ -49,7 +49,6 @@ public void testSerialization() throws IOException { final BytesStreamOutput out = new BytesStreamOutput(); original.writeTo(out); - final NamedWriteableRegistry registry = new NamedWriteableRegistry(new XPackClientPlugin(Settings.EMPTY).getNamedWriteables()); StreamInput in = new NamedWriteableAwareStreamInput(ByteBufferStreamInput.wrap(BytesReference.toBytes(out.bytes())), registry); final GetUserPrivilegesResponse copy = new GetUserPrivilegesResponse(in); @@ -70,55 +69,96 @@ public void testEqualsAndHashCode() throws IOException { original.getApplicationPrivileges(), original.getRunAs() ); - final EqualsHashCodeTestUtils.MutateFunction mutate = - new EqualsHashCodeTestUtils.MutateFunction() { - @Override - public GetUserPrivilegesResponse mutate(GetUserPrivilegesResponse original) { - final int random = randomIntBetween(1, 0b11111); - final Set cluster = maybeMutate(random, 0, original.getClusterPrivileges(), () -> randomAlphaOfLength(5)); - final Set conditionalCluster = maybeMutate(random, 1, - original.getConditionalClusterPrivileges(), () -> new ManageApplicationPrivileges(randomStringSet(3))); - final Set index = maybeMutate(random, 2, original.getIndexPrivileges(), - () -> new GetUserPrivilegesResponse.Indices(randomStringSet(1), randomStringSet(1), emptySet(), emptySet(), - randomBoolean())); - final Set application = maybeMutate(random, 3, original.getApplicationPrivileges(), - () -> ApplicationResourcePrivileges.builder().resources(generateRandomStringArray(3, 3, false, false)) - .application(randomAlphaOfLength(5)).privileges(generateRandomStringArray(3, 5, false, false)).build()); - final Set runAs = maybeMutate(random, 4, original.getRunAs(), () -> randomAlphaOfLength(8)); - return new GetUserPrivilegesResponse(cluster, conditionalCluster, index, application, runAs); - } + final EqualsHashCodeTestUtils.MutateFunction mutate = new EqualsHashCodeTestUtils.MutateFunction< + GetUserPrivilegesResponse>() { + @Override + public GetUserPrivilegesResponse mutate(GetUserPrivilegesResponse original) { + final int random = randomIntBetween(1, 0b11111); + final Set cluster = maybeMutate(random, 0, original.getClusterPrivileges(), () -> randomAlphaOfLength(5)); + final Set conditionalCluster = maybeMutate( + random, + 1, + original.getConditionalClusterPrivileges(), + () -> new ManageApplicationPrivileges(randomStringSet(3)) + ); + final Set index = maybeMutate( + random, + 2, + original.getIndexPrivileges(), + () -> new GetUserPrivilegesResponse.Indices( + randomStringSet(1), + randomStringSet(1), + emptySet(), + emptySet(), + randomBoolean() + ) + ); + final Set application = maybeMutate( + random, + 3, + original.getApplicationPrivileges(), + () -> ApplicationResourcePrivileges.builder() + .resources(generateRandomStringArray(3, 3, false, false)) + .application(randomAlphaOfLength(5)) + .privileges(generateRandomStringArray(3, 5, false, false)) + .build() + ); + final Set runAs = maybeMutate(random, 4, original.getRunAs(), () -> randomAlphaOfLength(8)); + return new GetUserPrivilegesResponse(cluster, conditionalCluster, index, application, runAs); + } - private Set maybeMutate(int random, int index, Set original, Supplier supplier) { - if ((random & (1 << index)) == 0) { - return original; - } - if (original.isEmpty()) { - return Collections.singleton(supplier.get()); - } else { - return emptySet(); - } + private Set maybeMutate(int random, int index, Set original, Supplier supplier) { + if ((random & (1 << index)) == 0) { + return original; + } + if (original.isEmpty()) { + return Collections.singleton(supplier.get()); + } else { + return emptySet(); } - }; + } + }; EqualsHashCodeTestUtils.checkEqualsAndHashCode(response, copy, mutate); } private GetUserPrivilegesResponse randomResponse() { final Set cluster = randomStringSet(5); - final Set conditionalCluster = Sets.newHashSet(randomArray(3, ConfigurableClusterPrivilege[]::new, - () -> new ManageApplicationPrivileges( - randomStringSet(3) - ))); - final Set index = Sets.newHashSet(randomArray(5, GetUserPrivilegesResponse.Indices[]::new, - () -> new GetUserPrivilegesResponse.Indices(randomStringSet(6), randomStringSet(8), - Sets.newHashSet(randomArray(3, FieldGrantExcludeGroup[]::new, () -> new FieldGrantExcludeGroup( - generateRandomStringArray(3, 5, false, false), generateRandomStringArray(3, 5, false, false)))), - randomStringSet(3).stream().map(BytesArray::new).collect(Collectors.toSet()), randomBoolean() - )) + final Set conditionalCluster = Sets.newHashSet( + randomArray(3, ConfigurableClusterPrivilege[]::new, () -> new ManageApplicationPrivileges(randomStringSet(3))) + ); + final Set index = Sets.newHashSet( + randomArray( + 5, + GetUserPrivilegesResponse.Indices[]::new, + () -> new GetUserPrivilegesResponse.Indices( + randomStringSet(6), + randomStringSet(8), + Sets.newHashSet( + randomArray( + 3, + FieldGrantExcludeGroup[]::new, + () -> new FieldGrantExcludeGroup( + generateRandomStringArray(3, 5, false, false), + generateRandomStringArray(3, 5, false, false) + ) + ) + ), + randomStringSet(3).stream().map(BytesArray::new).collect(Collectors.toSet()), + randomBoolean() + ) + ) + ); + final Set application = Sets.newHashSet( + randomArray( + 5, + ApplicationResourcePrivileges[]::new, + () -> ApplicationResourcePrivileges.builder() + .resources(generateRandomStringArray(3, 3, false, false)) + .application(randomAlphaOfLength(5)) + .privileges(generateRandomStringArray(3, 5, false, false)) + .build() + ) ); - final Set application = Sets.newHashSet(randomArray(5, ApplicationResourcePrivileges[]::new, - () -> ApplicationResourcePrivileges.builder().resources(generateRandomStringArray(3, 3, false, false)) - .application(randomAlphaOfLength(5)).privileges(generateRandomStringArray(3, 5, false, false)).build() - )); final Set runAs = randomStringSet(3); return new GetUserPrivilegesResponse(cluster, conditionalCluster, index, application, runAs); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/user/HasPrivilegesRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/user/HasPrivilegesRequestTests.java index e4deeebf1c325..a912423241177 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/user/HasPrivilegesRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/user/HasPrivilegesRequestTests.java @@ -63,9 +63,10 @@ public void testValidateNoWildcardApplicationPrivileges() { final HasPrivilegesRequest request = new HasPrivilegesRequest(); request.clusterPrivileges(new String[0]); request.indexPrivileges(new IndicesPrivileges[0]); - request.applicationPrivileges(new ApplicationResourcePrivileges[] { - ApplicationResourcePrivileges.builder().privileges("read").application("*").resources("item/1").build() - }); + request.applicationPrivileges( + new ApplicationResourcePrivileges[] { + ApplicationResourcePrivileges.builder().privileges("read").application("*").resources("item/1").build() } + ); final ActionRequestValidationException exception = request.validate(); assertThat(exception, notNullValue()); assertThat(exception.validationErrors(), hasItem("Application names may not contain '*' (found '*')")); @@ -76,7 +77,6 @@ private HasPrivilegesRequest serializeAndDeserialize(HasPrivilegesRequest origin out.setVersion(version); original.writeTo(out); - final StreamInput in = out.bytes().streamInput(); in.setVersion(version); final HasPrivilegesRequest copy = new HasPrivilegesRequest(in); @@ -88,11 +88,16 @@ private HasPrivilegesRequest randomRequest() { final HasPrivilegesRequest request = new HasPrivilegesRequest(); request.username(randomAlphaOfLength(8)); - final List clusterPrivileges = randomSubsetOf(Arrays.asList(ClusterPrivilegeResolver.MONITOR, - ClusterPrivilegeResolver.MANAGE, - ClusterPrivilegeResolver.MANAGE_ML, ClusterPrivilegeResolver.MANAGE_SECURITY, ClusterPrivilegeResolver.MANAGE_PIPELINE, - ClusterPrivilegeResolver.ALL)) - .stream().map(p -> p.name()).collect(Collectors.toList()); + final List clusterPrivileges = randomSubsetOf( + Arrays.asList( + ClusterPrivilegeResolver.MONITOR, + ClusterPrivilegeResolver.MANAGE, + ClusterPrivilegeResolver.MANAGE_ML, + ClusterPrivilegeResolver.MANAGE_SECURITY, + ClusterPrivilegeResolver.MANAGE_PIPELINE, + ClusterPrivilegeResolver.ALL + ) + ).stream().map(p -> p.name()).collect(Collectors.toList()); request.clusterPrivileges(clusterPrivileges.toArray(Strings.EMPTY_ARRAY)); IndicesPrivileges[] indicesPrivileges = new IndicesPrivileges[randomInt(5)]; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/audit/logfile/CapturingLogger.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/audit/logfile/CapturingLogger.java index f28c9b7e39e7b..d533e4a476d99 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/audit/logfile/CapturingLogger.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/audit/logfile/CapturingLogger.java @@ -17,8 +17,8 @@ import org.apache.logging.log4j.core.config.Configuration; import org.apache.logging.log4j.core.config.LoggerConfig; import org.apache.logging.log4j.core.filter.RegexFilter; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.logging.Loggers; +import org.elasticsearch.core.Nullable; import java.util.ArrayList; import java.util.List; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/AuthenticationTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/AuthenticationTests.java index 1ad0a8a5e238b..362fe09c8ab1a 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/AuthenticationTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/AuthenticationTests.java @@ -31,16 +31,14 @@ public class AuthenticationTests extends ESTestCase { public void testWillGetLookedUpByWhenItExists() { final RealmRef authenticatedBy = new RealmRef("auth_by", "auth_by_type", "node"); final RealmRef lookedUpBy = new RealmRef("lookup_by", "lookup_by_type", "node"); - final Authentication authentication = new Authentication( - new User("user"), authenticatedBy, lookedUpBy); + final Authentication authentication = new Authentication(new User("user"), authenticatedBy, lookedUpBy); assertEquals(lookedUpBy, authentication.getSourceRealm()); } public void testWillGetAuthenticateByWhenLookupIsNull() { final RealmRef authenticatedBy = new RealmRef("auth_by", "auth_by_type", "node"); - final Authentication authentication = new Authentication( - new User("user"), authenticatedBy, null); + final Authentication authentication = new Authentication(new User("user"), authenticatedBy, null); assertEquals(authenticatedBy, authentication.getSourceRealm()); } @@ -81,8 +79,10 @@ public void testCanAccessResourcesOf() { } // User and its API key are not the same owner - assertCannotAccessResources(randomAuthentication(user1, realm1), - randomApiKeyAuthentication(user1, randomAlphaOfLengthBetween(10, 20))); + assertCannotAccessResources( + randomAuthentication(user1, realm1), + randomApiKeyAuthentication(user1, randomAlphaOfLengthBetween(10, 20)) + ); // Same API key ID are the same owner final String apiKeyId1 = randomAlphaOfLengthBetween(10, 20); @@ -90,27 +90,40 @@ public void testCanAccessResourcesOf() { // Two API keys (2 API key IDs) are not the same owner final String apiKeyId2 = randomValueOtherThan(apiKeyId1, () -> randomAlphaOfLengthBetween(10, 20)); - assertCannotAccessResources(randomApiKeyAuthentication(randomFrom(user1, user2), apiKeyId1), - randomApiKeyAuthentication(randomFrom(user1, user2), apiKeyId2)); + assertCannotAccessResources( + randomApiKeyAuthentication(randomFrom(user1, user2), apiKeyId1), + randomApiKeyAuthentication(randomFrom(user1, user2), apiKeyId2) + ); } public void testIsServiceAccount() { - final User user = - new User(randomAlphaOfLengthBetween(3, 8), randomArray(0, 3, String[]::new, () -> randomAlphaOfLengthBetween(3, 8))); + final User user = new User( + randomAlphaOfLengthBetween(3, 8), + randomArray(0, 3, String[]::new, () -> randomAlphaOfLengthBetween(3, 8)) + ); final Authentication.RealmRef authRealm; final boolean authRealmIsForServiceAccount = randomBoolean(); if (authRealmIsForServiceAccount) { authRealm = new Authentication.RealmRef( ServiceAccountSettings.REALM_NAME, ServiceAccountSettings.REALM_TYPE, - randomAlphaOfLengthBetween(3, 8)); + randomAlphaOfLengthBetween(3, 8) + ); } else { - authRealm = new Authentication.RealmRef(randomAlphaOfLengthBetween(3, 8), randomAlphaOfLengthBetween(3, 8), - randomAlphaOfLengthBetween(3, 8)); + authRealm = new Authentication.RealmRef( + randomAlphaOfLengthBetween(3, 8), + randomAlphaOfLengthBetween(3, 8), + randomAlphaOfLengthBetween(3, 8) + ); } final Authentication.RealmRef lookupRealm = randomFrom( - new Authentication.RealmRef(randomAlphaOfLengthBetween(3, 8), randomAlphaOfLengthBetween(3, 8), - randomAlphaOfLengthBetween(3, 8)), null); + new Authentication.RealmRef( + randomAlphaOfLengthBetween(3, 8), + randomAlphaOfLengthBetween(3, 8), + randomAlphaOfLengthBetween(3, 8) + ), + null + ); final Authentication authentication = new Authentication(user, authRealm, lookupRealm); if (authRealmIsForServiceAccount && lookupRealm == null) { @@ -122,8 +135,8 @@ public void testIsServiceAccount() { private void checkCanAccessResources(Authentication authentication0, Authentication authentication1) { if (authentication0.getAuthenticationType() == authentication1.getAuthenticationType() - || EnumSet.of(AuthenticationType.REALM, AuthenticationType.TOKEN).equals( - EnumSet.of(authentication0.getAuthenticationType(), authentication1.getAuthenticationType()))) { + || EnumSet.of(AuthenticationType.REALM, AuthenticationType.TOKEN) + .equals(EnumSet.of(authentication0.getAuthenticationType(), authentication1.getAuthenticationType()))) { assertTrue(authentication0.canAccessResourcesOf(authentication1)); assertTrue(authentication1.canAccessResourcesOf(authentication0)); } else { @@ -137,22 +150,23 @@ private void assertCannotAccessResources(Authentication authentication0, Authent } public static User randomUser() { - return new User(randomAlphaOfLengthBetween(3, 8), - randomArray(1, 3, String[]::new, () -> randomAlphaOfLengthBetween(3, 8))); + return new User(randomAlphaOfLengthBetween(3, 8), randomArray(1, 3, String[]::new, () -> randomAlphaOfLengthBetween(3, 8))); } public static RealmRef randomRealm() { return new RealmRef( randomAlphaOfLengthBetween(3, 8), randomFrom(FileRealmSettings.TYPE, NativeRealmSettings.TYPE, randomAlphaOfLengthBetween(3, 8)), - randomAlphaOfLengthBetween(3, 8)); + randomAlphaOfLengthBetween(3, 8) + ); } private RealmRef mutateRealm(RealmRef original, String name, String type) { return new RealmRef( name == null ? original.getName() : name, type == null ? original.getType() : type, - randomBoolean() ? original.getNodeName() : randomAlphaOfLengthBetween(3, 8)); + randomBoolean() ? original.getNodeName() : randomAlphaOfLengthBetween(3, 8) + ); } public static Authentication randomAuthentication(User user, RealmRef realmRef) { @@ -163,18 +177,27 @@ public static Authentication randomAuthentication(User user, RealmRef realmRef) realmRef = randomRealm(); } final Version version = VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, Version.CURRENT); - final AuthenticationType authenticationType = - randomValueOtherThan(AuthenticationType.API_KEY, () -> randomFrom(AuthenticationType.values())); + final AuthenticationType authenticationType = randomValueOtherThan( + AuthenticationType.API_KEY, + () -> randomFrom(AuthenticationType.values()) + ); final Map metadata; if (randomBoolean()) { metadata = Map.of(randomAlphaOfLengthBetween(3, 8), randomAlphaOfLengthBetween(3, 8)); } else { metadata = Arrays.stream(randomArray(1, 5, String[]::new, () -> randomAlphaOfLengthBetween(3, 8))) - .distinct().collect(Collectors.toMap(s -> s, s -> randomAlphaOfLengthBetween(3, 8))); + .distinct() + .collect(Collectors.toMap(s -> s, s -> randomAlphaOfLengthBetween(3, 8))); } if (randomBoolean()) { // run-as - return new Authentication(new User(user.principal(), user.roles(), randomUser()), - randomRealm(), realmRef, version, authenticationType, metadata); + return new Authentication( + new User(user.principal(), user.roles(), randomUser()), + randomRealm(), + realmRef, + version, + authenticationType, + metadata + ); } else { return new Authentication(user, realmRef, null, version, authenticationType, metadata); } @@ -182,12 +205,14 @@ public static Authentication randomAuthentication(User user, RealmRef realmRef) public static Authentication randomApiKeyAuthentication(User user, String apiKeyId) { final RealmRef apiKeyRealm = new RealmRef("_es_api_key", "_es_api_key", randomAlphaOfLengthBetween(3, 8)); - return new Authentication(user, + return new Authentication( + user, apiKeyRealm, null, VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, Version.CURRENT), AuthenticationType.API_KEY, - Map.of(API_KEY_ID_KEY, apiKeyId)); + Map.of(API_KEY_ID_KEY, apiKeyId) + ); } private boolean realmIsSingleton(RealmRef realmRef) { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/DefaultAuthenticationFailureHandlerTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/DefaultAuthenticationFailureHandlerTests.java index b075b03e7098a..fdce936d73ffa 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/DefaultAuthenticationFailureHandlerTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/DefaultAuthenticationFailureHandlerTests.java @@ -44,8 +44,10 @@ public void testAuthenticationRequired() { failureHandler = new DefaultAuthenticationFailureHandler(failureResponseHeaders); } assertThat(failureHandler, is(notNullValue())); - final ElasticsearchSecurityException ese = - failureHandler.authenticationRequired("someaction", new ThreadContext(Settings.builder().build())); + final ElasticsearchSecurityException ese = failureHandler.authenticationRequired( + "someaction", + new ThreadContext(Settings.builder().build()) + ); assertThat(ese, is(notNullValue())); assertThat(ese.getMessage(), equalTo("action [someaction] requires authentication")); assertThat(ese.getHeader("WWW-Authenticate"), is(notNullValue())); @@ -78,8 +80,8 @@ public void testExceptionProcessingRequest() { final boolean causeIsElasticsearchSecurityException = randomBoolean(); final boolean causeIsEseAndUnauthorized = causeIsElasticsearchSecurityException && randomBoolean(); final ElasticsearchSecurityException eseCause = (causeIsEseAndUnauthorized) - ? new ElasticsearchSecurityException("unauthorized", RestStatus.UNAUTHORIZED, null, (Object[]) null) - : new ElasticsearchSecurityException("different error", RestStatus.BAD_REQUEST, null, (Object[]) null); + ? new ElasticsearchSecurityException("unauthorized", RestStatus.UNAUTHORIZED, null, (Object[]) null) + : new ElasticsearchSecurityException("different error", RestStatus.BAD_REQUEST, null, (Object[]) null); final Exception cause = causeIsElasticsearchSecurityException ? eseCause : new Exception("other error"); final boolean withAuthenticateHeader = randomBoolean(); final String selectedScheme = randomFrom(bearerAuthScheme, basicAuthScheme, negotiateAuthScheme); @@ -89,8 +91,11 @@ public void testExceptionProcessingRequest() { if (causeIsElasticsearchSecurityException) { if (causeIsEseAndUnauthorized) { - final ElasticsearchSecurityException ese = failureHandler.exceptionProcessingRequest(mock(RestRequest.class), cause, - new ThreadContext(Settings.builder().build())); + final ElasticsearchSecurityException ese = failureHandler.exceptionProcessingRequest( + mock(RestRequest.class), + cause, + new ThreadContext(Settings.builder().build()) + ); assertThat(ese, is(notNullValue())); assertThat(ese.getHeader("WWW-Authenticate"), is(notNullValue())); assertThat(ese, is(sameInstance(cause))); @@ -105,12 +110,21 @@ public void testExceptionProcessingRequest() { } assertThat(ese.getMessage(), equalTo("unauthorized")); } else { - expectThrows(AssertionError.class, () -> failureHandler.exceptionProcessingRequest(mock(RestRequest.class), cause, - new ThreadContext(Settings.builder().build()))); + expectThrows( + AssertionError.class, + () -> failureHandler.exceptionProcessingRequest( + mock(RestRequest.class), + cause, + new ThreadContext(Settings.builder().build()) + ) + ); } } else { - final ElasticsearchSecurityException ese = failureHandler.exceptionProcessingRequest(mock(RestRequest.class), cause, - new ThreadContext(Settings.builder().build())); + final ElasticsearchSecurityException ese = failureHandler.exceptionProcessingRequest( + mock(RestRequest.class), + cause, + new ThreadContext(Settings.builder().build()) + ); assertThat(ese, is(notNullValue())); assertThat(ese.getHeader("WWW-Authenticate"), is(notNullValue())); assertThat(ese.getMessage(), equalTo("error attempting to authenticate request")); @@ -130,8 +144,11 @@ public void testSortsWWWAuthenticateHeaderValues() { failureResponeHeaders.put("WWW-Authenticate", supportedSchemes); final DefaultAuthenticationFailureHandler failuerHandler = new DefaultAuthenticationFailureHandler(failureResponeHeaders); - final ElasticsearchSecurityException ese = failuerHandler.exceptionProcessingRequest(mock(RestRequest.class), null, - new ThreadContext(Settings.builder().build())); + final ElasticsearchSecurityException ese = failuerHandler.exceptionProcessingRequest( + mock(RestRequest.class), + null, + new ThreadContext(Settings.builder().build()) + ); assertThat(ese, is(notNullValue())); assertThat(ese.getHeader("WWW-Authenticate"), is(notNullValue())); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/RealmConfigTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/RealmConfigTests.java index 16b5d225078d9..c14cd571d66a4 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/RealmConfigTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/RealmConfigTests.java @@ -26,7 +26,7 @@ public class RealmConfigTests extends ESTestCase { @Before public void setUp() throws Exception { - realmIdentifier = new RealmConfig.RealmIdentifier(randomAlphaOfLengthBetween(4, 12), randomAlphaOfLengthBetween(4,12)); + realmIdentifier = new RealmConfig.RealmIdentifier(randomAlphaOfLengthBetween(4, 12), randomAlphaOfLengthBetween(4, 12)); environment = Mockito.mock(Environment.class); globalSettings = Settings.builder().put("path.home", createTempDir()).build(); threadContext = new ThreadContext(globalSettings); @@ -35,9 +35,9 @@ public void setUp() throws Exception { public void testWillPassWhenOrderSettingIsConfigured() { Settings settings = Settings.builder() - .put(globalSettings) - .put(RealmSettings.realmSettingPrefix(realmIdentifier) + "order", 0) - .build(); + .put(globalSettings) + .put(RealmSettings.realmSettingPrefix(realmIdentifier) + "order", 0) + .build(); RealmConfig realmConfig = new RealmConfig(realmIdentifier, settings, environment, threadContext); assertEquals(0, realmConfig.order); @@ -50,8 +50,10 @@ public void testWillFailWhenOrderSettingIsMissing() { } public void testWillNotFailWhenOrderIsMissingAndDisabled() { - Settings settings = Settings.builder().put(globalSettings) - .put(RealmSettings.getFullSettingKey(realmIdentifier, RealmSettings.ENABLED_SETTING), false).build(); + Settings settings = Settings.builder() + .put(globalSettings) + .put(RealmSettings.getFullSettingKey(realmIdentifier, RealmSettings.ENABLED_SETTING), false) + .build(); final RealmConfig realmConfig = new RealmConfig(realmIdentifier, settings, environment, threadContext); assertThat(realmConfig.enabled(), is(false)); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/TokenMetadataTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/TokenMetadataTests.java index f7db2d375116a..210e4ca1e853f 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/TokenMetadataTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/TokenMetadataTests.java @@ -46,7 +46,8 @@ private List generateKeyAndTimestampListOfSize(int size) { final List keyAndTimestampList = new ArrayList<>(size); for (int i = 0; i < size; i++) { keyAndTimestampList.add( - new KeyAndTimestamp(new SecureString(randomAlphaOfLengthBetween(1, 12).toCharArray()), randomNonNegativeLong())); + new KeyAndTimestamp(new SecureString(randomAlphaOfLengthBetween(1, 12).toCharArray()), randomNonNegativeLong()) + ); } return keyAndTimestampList; } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/support/SecondaryAuthenticationTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/support/SecondaryAuthenticationTests.java index 126193529c7f7..eef3b2e4993e3 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/support/SecondaryAuthenticationTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/support/SecondaryAuthenticationTests.java @@ -18,7 +18,6 @@ import org.elasticsearch.xpack.core.security.SecurityContext; import org.elasticsearch.xpack.core.security.authc.Authentication; import org.elasticsearch.xpack.core.security.user.User; -import org.elasticsearch.xpack.core.security.authc.support.SecondaryAuthentication; import org.junit.After; import org.junit.Before; @@ -140,8 +139,10 @@ public void testPreserveSecondaryContextAcrossThreads() throws Exception { final ThreadContext threadContext = threadPool.getThreadContext(); secondaryAuth.execute(originalContext -> { assertThat(securityContext.getUser().principal(), equalTo("u2")); - ActionListener listener = new ContextPreservingActionListener<>(threadContext.newRestorableContext(false), - ActionListener.wrap(() -> listenerUser.set(securityContext.getUser()))); + ActionListener listener = new ContextPreservingActionListener<>( + threadContext.newRestorableContext(false), + ActionListener.wrap(() -> listenerUser.set(securityContext.getUser())) + ); originalContext.restore(); threadPool.generic().execute(() -> { threadUser.set(securityContext.getUser()); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/support/mapper/TemplateRoleNameTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/support/mapper/TemplateRoleNameTests.java index 0ff92b5fa8fc0..5c6fa57269de3 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/support/mapper/TemplateRoleNameTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/support/mapper/TemplateRoleNameTests.java @@ -17,10 +17,6 @@ import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.DeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.script.ScriptEngine; import org.elasticsearch.script.ScriptException; import org.elasticsearch.script.ScriptMetadata; @@ -31,6 +27,10 @@ import org.elasticsearch.script.mustache.MustacheScriptEngine; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.EqualsHashCodeTestUtils; +import org.elasticsearch.xcontent.DeprecationHandler; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.security.authc.support.mapper.TemplateRoleName.Format; import org.elasticsearch.xpack.core.security.authc.support.mapper.expressiondsl.ExpressionModel; import org.hamcrest.Matchers; @@ -63,18 +63,22 @@ public void testParseRoles() throws Exception { assertThat(role1.getFormat(), equalTo(Format.STRING)); final TemplateRoleName role2 = parse( - "{ \"template\": \"{\\\"source\\\":\\\"{{#tojson}}groups{{/tojson}}\\\"}\", \"format\":\"json\" }"); + "{ \"template\": \"{\\\"source\\\":\\\"{{#tojson}}groups{{/tojson}}\\\"}\", \"format\":\"json\" }" + ); assertThat(role2, Matchers.instanceOf(TemplateRoleName.class)); - assertThat(role2.getTemplate().utf8ToString(), - equalTo("{\"source\":\"{{#tojson}}groups{{/tojson}}\"}")); + assertThat(role2.getTemplate().utf8ToString(), equalTo("{\"source\":\"{{#tojson}}groups{{/tojson}}\"}")); assertThat(role2.getFormat(), equalTo(Format.JSON)); } public void testToXContent() throws Exception { - final String json = "{" + - "\"template\":\"{\\\"source\\\":\\\"" + randomAlphaOfLengthBetween(8, 24) + "\\\"}\"," + - "\"format\":\"" + randomFrom(Format.values()).formatName() + "\"" + - "}"; + final String json = "{" + + "\"template\":\"{\\\"source\\\":\\\"" + + randomAlphaOfLengthBetween(8, 24) + + "\\\"}\"," + + "\"format\":\"" + + randomFrom(Format.values()).formatName() + + "\"" + + "}"; assertThat(Strings.toString(parse(json)), equalTo(json)); } @@ -87,8 +91,11 @@ public void testEqualsAndHashCode() throws Exception { } public void testEvaluateRoles() throws Exception { - final ScriptService scriptService = new ScriptService(Settings.EMPTY, - Collections.singletonMap(MustacheScriptEngine.NAME, new MustacheScriptEngine()), ScriptModule.CORE_CONTEXTS); + final ScriptService scriptService = new ScriptService( + Settings.EMPTY, + Collections.singletonMap(MustacheScriptEngine.NAME, new MustacheScriptEngine()), + ScriptModule.CORE_CONTEXTS + ); final ExpressionModel model = new ExpressionModel(); model.defineField("username", "hulk"); model.defineField("groups", Arrays.asList("avengers", "defenders", "panthenon")); @@ -99,8 +106,10 @@ public void testEvaluateRoles() throws Exception { final TemplateRoleName user = new TemplateRoleName(new BytesArray("{ \"source\":\"_user_{{username}}\" }"), Format.STRING); assertThat(user.getRoleNames(scriptService, model), contains("_user_hulk")); - final TemplateRoleName groups = new TemplateRoleName(new BytesArray("{ \"source\":\"{{#tojson}}groups{{/tojson}}\" }"), - Format.JSON); + final TemplateRoleName groups = new TemplateRoleName( + new BytesArray("{ \"source\":\"{{#tojson}}groups{{/tojson}}\" }"), + Format.JSON + ); assertThat(groups.getRoleNames(scriptService, model), contains("avengers", "defenders", "panthenon")); } @@ -122,24 +131,27 @@ public void trySerialize(TemplateRoleName original) throws Exception { } public void tryEquals(TemplateRoleName original) { - final EqualsHashCodeTestUtils.CopyFunction copy = - rmt -> new TemplateRoleName(rmt.getTemplate(), rmt.getFormat()); + final EqualsHashCodeTestUtils.CopyFunction copy = rmt -> new TemplateRoleName(rmt.getTemplate(), rmt.getFormat()); final EqualsHashCodeTestUtils.MutateFunction mutate = rmt -> { if (randomBoolean()) { - return new TemplateRoleName(rmt.getTemplate(), - randomValueOtherThan(rmt.getFormat(), () -> randomFrom(Format.values()))); + return new TemplateRoleName(rmt.getTemplate(), randomValueOtherThan(rmt.getFormat(), () -> randomFrom(Format.values()))); } else { final String templateStr = rmt.getTemplate().utf8ToString(); - return new TemplateRoleName(new BytesArray(templateStr.substring(randomIntBetween(1, templateStr.length() / 2))), - rmt.getFormat()); + return new TemplateRoleName( + new BytesArray(templateStr.substring(randomIntBetween(1, templateStr.length() / 2))), + rmt.getFormat() + ); } }; EqualsHashCodeTestUtils.checkEqualsAndHashCode(original, copy, mutate); } public void testValidate() { - final ScriptService scriptService = new ScriptService(Settings.EMPTY, - Collections.singletonMap(MustacheScriptEngine.NAME, new MustacheScriptEngine()), ScriptModule.CORE_CONTEXTS); + final ScriptService scriptService = new ScriptService( + Settings.EMPTY, + Collections.singletonMap(MustacheScriptEngine.NAME, new MustacheScriptEngine()), + ScriptModule.CORE_CONTEXTS + ); final TemplateRoleName plainString = new TemplateRoleName(new BytesArray("{ \"source\":\"heroes\" }"), Format.STRING); plainString.validate(scriptService); @@ -147,8 +159,10 @@ public void testValidate() { final TemplateRoleName user = new TemplateRoleName(new BytesArray("{ \"source\":\"_user_{{username}}\" }"), Format.STRING); user.validate(scriptService); - final TemplateRoleName groups = new TemplateRoleName(new BytesArray("{ \"source\":\"{{#tojson}}groups{{/tojson}}\" }"), - Format.JSON); + final TemplateRoleName groups = new TemplateRoleName( + new BytesArray("{ \"source\":\"{{#tojson}}groups{{/tojson}}\" }"), + Format.JSON + ); groups.validate(scriptService); final TemplateRoleName notObject = new TemplateRoleName(new BytesArray("heroes"), Format.STRING); @@ -159,38 +173,48 @@ public void testValidate() { } public void testValidateWillPassWithEmptyContext() { - final ScriptService scriptService = new ScriptService(Settings.EMPTY, - Collections.singletonMap(MustacheScriptEngine.NAME, new MustacheScriptEngine()), ScriptModule.CORE_CONTEXTS); - - final BytesReference template = new BytesArray("{ \"source\":\"" + - "{{username}}/{{dn}}/{{realm}}/{{metadata}}" + - "{{#realm}}" + - " {{name}}/{{type}}" + - "{{/realm}}" + - "{{#toJson}}groups{{/toJson}}" + - "{{^groups}}{{.}}{{/groups}}" + - "{{#metadata}}" + - " {{#first}}" + - "
  • {{name}}
  • " + - " {{/first}}" + - " {{#link}}" + - "
  • {{name}}
  • " + - " {{/link}}" + - " {{#toJson}}subgroups{{/toJson}}" + - " {{something-else}}" + - "{{/metadata}}\" }"); + final ScriptService scriptService = new ScriptService( + Settings.EMPTY, + Collections.singletonMap(MustacheScriptEngine.NAME, new MustacheScriptEngine()), + ScriptModule.CORE_CONTEXTS + ); + + final BytesReference template = new BytesArray( + "{ \"source\":\"" + + "{{username}}/{{dn}}/{{realm}}/{{metadata}}" + + "{{#realm}}" + + " {{name}}/{{type}}" + + "{{/realm}}" + + "{{#toJson}}groups{{/toJson}}" + + "{{^groups}}{{.}}{{/groups}}" + + "{{#metadata}}" + + " {{#first}}" + + "
  • {{name}}
  • " + + " {{/first}}" + + " {{#link}}" + + "
  • {{name}}
  • " + + " {{/link}}" + + " {{#toJson}}subgroups{{/toJson}}" + + " {{something-else}}" + + "{{/metadata}}\" }" + ); final TemplateRoleName templateRoleName = new TemplateRoleName(template, Format.STRING); templateRoleName.validate(scriptService); } public void testValidateWillFailForSyntaxError() { - final ScriptService scriptService = new ScriptService(Settings.EMPTY, - Collections.singletonMap(MustacheScriptEngine.NAME, new MustacheScriptEngine()), ScriptModule.CORE_CONTEXTS); + final ScriptService scriptService = new ScriptService( + Settings.EMPTY, + Collections.singletonMap(MustacheScriptEngine.NAME, new MustacheScriptEngine()), + ScriptModule.CORE_CONTEXTS + ); final BytesReference template = new BytesArray("{ \"source\":\" {{#not-closed}} {{other-variable}} \" }"); - final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> new TemplateRoleName(template, Format.STRING).validate(scriptService)); + final IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> new TemplateRoleName(template, Format.STRING).validate(scriptService) + ); assertTrue(e.getCause() instanceof ScriptException); } @@ -202,15 +226,21 @@ public void testValidateWillCompileButNotExecutePainlessScript() { final ScriptEngine scriptEngine = mock(ScriptEngine.class); when(scriptEngine.getType()).thenReturn("painless"); - when(scriptEngine.compile(eq("valid"), eq("params.metedata.group"), any(), eq(Map.of()))) - .thenReturn(scriptFactory); - final ScriptException scriptException = - new ScriptException("exception", new IllegalStateException(), List.of(), "bad syntax", "painless"); - doThrow(scriptException) - .when(scriptEngine).compile(eq("invalid"), eq("bad syntax"), any(), eq(Map.of())); - - final ScriptService scriptService = new ScriptService(Settings.EMPTY, - Map.of("painless", scriptEngine), ScriptModule.CORE_CONTEXTS) { + when(scriptEngine.compile(eq("valid"), eq("params.metedata.group"), any(), eq(Map.of()))).thenReturn(scriptFactory); + final ScriptException scriptException = new ScriptException( + "exception", + new IllegalStateException(), + List.of(), + "bad syntax", + "painless" + ); + doThrow(scriptException).when(scriptEngine).compile(eq("invalid"), eq("bad syntax"), any(), eq(Map.of())); + + final ScriptService scriptService = new ScriptService( + Settings.EMPTY, + Map.of("painless", scriptEngine), + ScriptModule.CORE_CONTEXTS + ) { @Override protected StoredScriptSource getScriptFromClusterState(String id) { if ("valid".equals(id)) { @@ -222,30 +252,39 @@ protected StoredScriptSource getScriptFromClusterState(String id) { }; // Validation succeeds if compilation is successful new TemplateRoleName(new BytesArray("{ \"id\":\"valid\" }"), Format.STRING).validate(scriptService); - verify(scriptEngine, times(1)) - .compile(eq("valid"), eq("params.metedata.group"), any(), eq(Map.of())); + verify(scriptEngine, times(1)).compile(eq("valid"), eq("params.metedata.group"), any(), eq(Map.of())); verify(compiledScript, never()).execute(); // Validation fails if compilation fails - final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> new TemplateRoleName(new BytesArray("{ \"id\":\"invalid\" }"), Format.STRING).validate(scriptService)); + final IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> new TemplateRoleName(new BytesArray("{ \"id\":\"invalid\" }"), Format.STRING).validate(scriptService) + ); assertSame(scriptException, e.getCause()); } public void testValidationWillFailWhenInlineScriptIsNotEnabled() { final Settings settings = Settings.builder().put("script.allowed_types", ScriptService.ALLOW_NONE).build(); - final ScriptService scriptService = new ScriptService(settings, - Collections.singletonMap(MustacheScriptEngine.NAME, new MustacheScriptEngine()), ScriptModule.CORE_CONTEXTS); + final ScriptService scriptService = new ScriptService( + settings, + Collections.singletonMap(MustacheScriptEngine.NAME, new MustacheScriptEngine()), + ScriptModule.CORE_CONTEXTS + ); final BytesReference inlineScript = new BytesArray("{ \"source\":\"\" }"); - final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> new TemplateRoleName(inlineScript, Format.STRING).validate(scriptService)); + final IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> new TemplateRoleName(inlineScript, Format.STRING).validate(scriptService) + ); assertThat(e.getMessage(), containsString("[inline]")); } public void testValidateWillFailWhenStoredScriptIsNotEnabled() { final Settings settings = Settings.builder().put("script.allowed_types", ScriptService.ALLOW_NONE).build(); - final ScriptService scriptService = new ScriptService(settings, - Collections.singletonMap(MustacheScriptEngine.NAME, new MustacheScriptEngine()), ScriptModule.CORE_CONTEXTS); + final ScriptService scriptService = new ScriptService( + settings, + Collections.singletonMap(MustacheScriptEngine.NAME, new MustacheScriptEngine()), + ScriptModule.CORE_CONTEXTS + ); final ClusterChangedEvent clusterChangedEvent = mock(ClusterChangedEvent.class); final ClusterState clusterState = mock(ClusterState.class); final Metadata metadata = mock(Metadata.class); @@ -260,14 +299,19 @@ public void testValidateWillFailWhenStoredScriptIsNotEnabled() { scriptService.applyClusterState(clusterChangedEvent); final BytesReference storedScript = new BytesArray("{ \"id\":\"foo\" }"); - final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> new TemplateRoleName(storedScript, Format.STRING).validate(scriptService)); + final IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> new TemplateRoleName(storedScript, Format.STRING).validate(scriptService) + ); assertThat(e.getMessage(), containsString("[stored]")); } public void testValidateWillFailWhenStoredScriptIsNotFound() { - final ScriptService scriptService = new ScriptService(Settings.EMPTY, - Collections.singletonMap(MustacheScriptEngine.NAME, new MustacheScriptEngine()), ScriptModule.CORE_CONTEXTS); + final ScriptService scriptService = new ScriptService( + Settings.EMPTY, + Collections.singletonMap(MustacheScriptEngine.NAME, new MustacheScriptEngine()), + ScriptModule.CORE_CONTEXTS + ); final ClusterChangedEvent clusterChangedEvent = mock(ClusterChangedEvent.class); final ClusterState clusterState = mock(ClusterState.class); final Metadata metadata = mock(Metadata.class); @@ -278,8 +322,10 @@ public void testValidateWillFailWhenStoredScriptIsNotFound() { scriptService.applyClusterState(clusterChangedEvent); final BytesReference storedScript = new BytesArray("{ \"id\":\"foo\" }"); - final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> new TemplateRoleName(storedScript, Format.STRING).validate(scriptService)); + final IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> new TemplateRoleName(storedScript, Format.STRING).validate(scriptService) + ); assertThat(e.getMessage(), containsString("unable to find script")); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/support/mapper/expressiondsl/ExpressionModelPredicateTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/support/mapper/expressiondsl/ExpressionModelPredicateTests.java index fa9d639fc6969..d6798d1b16c3d 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/support/mapper/expressiondsl/ExpressionModelPredicateTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/support/mapper/expressiondsl/ExpressionModelPredicateTests.java @@ -85,8 +85,11 @@ public void testRegexAutomatonValue() throws Exception { final FieldValue fieldValue = new FieldValue("/.*" + substring + ".*/"); assertThat(ExpressionModel.buildPredicate(substring).test(fieldValue), is(true)); - assertThat(ExpressionModel.buildPredicate(randomAlphaOfLengthBetween(2, 4) + substring + randomAlphaOfLengthBetween(1, 5)) - .test(fieldValue), is(true)); + assertThat( + ExpressionModel.buildPredicate(randomAlphaOfLengthBetween(2, 4) + substring + randomAlphaOfLengthBetween(1, 5)) + .test(fieldValue), + is(true) + ); assertThat(ExpressionModel.buildPredicate(substring.substring(1, 3)).test(fieldValue), is(false)); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/support/mapper/expressiondsl/ExpressionModelTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/support/mapper/expressiondsl/ExpressionModelTests.java index 637b026684f9d..0c13bbc1d6f79 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/support/mapper/expressiondsl/ExpressionModelTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/support/mapper/expressiondsl/ExpressionModelTests.java @@ -12,8 +12,8 @@ import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.core.LogEvent; import org.apache.logging.log4j.message.Message; -import org.elasticsearch.core.CheckedRunnable; import org.elasticsearch.common.logging.Loggers; +import org.elasticsearch.core.CheckedRunnable; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.MockLogAppender; import org.elasticsearch.xpack.core.security.authc.support.mapper.expressiondsl.FieldExpression.FieldValue; @@ -37,15 +37,16 @@ public void testCheckFailureAgainstUndefinedFieldLogsMessage() throws Exception model.defineField("some_int", randomIntBetween(1, 99)); doWithLoggingExpectations( - List.of(new MockLogAppender.SeenEventExpectation( - "undefined field", - model.getClass().getName(), - Level.DEBUG, - "Attempt to test field [another_field] against value(s) [bork,bork!]," + - " but the field [another_field] does not have a value on this object; known fields are [some_int]")), - () -> assertThat( - model.test("another_field", List.of(new FieldValue("bork"), new FieldValue("bork!"))), - is(false)) + List.of( + new MockLogAppender.SeenEventExpectation( + "undefined field", + model.getClass().getName(), + Level.DEBUG, + "Attempt to test field [another_field] against value(s) [bork,bork!]," + + " but the field [another_field] does not have a value on this object; known fields are [some_int]" + ) + ), + () -> assertThat(model.test("another_field", List.of(new FieldValue("bork"), new FieldValue("bork!"))), is(false)) ); } @@ -69,8 +70,8 @@ public void testCheckAgainstDefinedFieldDoesNotLog() throws Exception { ); } - private void doWithLoggingExpectations(List expectations, - CheckedRunnable body) throws Exception { + private void doWithLoggingExpectations(List expectations, CheckedRunnable body) + throws Exception { final Logger modelLogger = LogManager.getLogger(ExpressionModel.class); final MockLogAppender mockAppender = new MockLogAppender(); mockAppender.start(); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/support/mapper/expressiondsl/ExpressionParserTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/support/mapper/expressiondsl/ExpressionParserTests.java index 83df8c7656da2..1e00f0e7c0a60 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/support/mapper/expressiondsl/ExpressionParserTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/support/mapper/expressiondsl/ExpressionParserTests.java @@ -7,16 +7,17 @@ package org.elasticsearch.xpack.core.security.authc.support.mapper.expressiondsl; import com.carrotsearch.randomizedtesting.WriterOutputStream; + import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.XPackClientPlugin; import org.elasticsearch.xpack.core.security.authc.support.mapper.expressiondsl.FieldExpression.FieldValue; import org.elasticsearch.xpack.core.watcher.support.xcontent.XContentSource; @@ -46,16 +47,16 @@ public void testParseSimpleFieldExpression() throws Exception { } public void testParseComplexExpression() throws Exception { - String json = "{ \"any\": [" + - " { \"field\": { \"username\" : \"*@shield.gov\" } }, " + - " { \"all\": [" + - " { \"field\": { \"username\" : \"/.*\\\\@avengers\\\\.(net|org)/\" } }, " + - " { \"field\": { \"groups\" : [ \"admin\", \"operators\" ] } }, " + - " { \"except\":" + - " { \"field\": { \"groups\" : \"disavowed\" } }" + - " }" + - " ] }" + - "] }"; + String json = "{ \"any\": [" + + " { \"field\": { \"username\" : \"*@shield.gov\" } }, " + + " { \"all\": [" + + " { \"field\": { \"username\" : \"/.*\\\\@avengers\\\\.(net|org)/\" } }, " + + " { \"field\": { \"groups\" : [ \"admin\", \"operators\" ] } }, " + + " { \"except\":" + + " { \"field\": { \"groups\" : \"disavowed\" } }" + + " }" + + " ] }" + + "] }"; final RoleMapperExpression expr = parse(json); assertThat(expr, instanceOf(AnyExpression.class)); @@ -63,8 +64,7 @@ public void testParseComplexExpression() throws Exception { assertThat(any.getElements(), iterableWithSize(2)); - final FieldExpression fieldShield = checkExpressionType(any.getElements().get(0), - FieldExpression.class); + final FieldExpression fieldShield = checkExpressionType(any.getElements().get(0), FieldExpression.class); assertThat(fieldShield.getField(), equalTo("username")); assertThat(fieldShield.getValues(), iterableWithSize(1)); final FieldValue valueShield = fieldShield.getValues().get(0); @@ -73,12 +73,10 @@ public void testParseComplexExpression() throws Exception { assertThat(valueShield.getAutomaton().run("fury@shield.gov"), equalTo(true)); assertThat(valueShield.getAutomaton().run("fury@shield.net"), equalTo(false)); - final AllExpression all = checkExpressionType(any.getElements().get(1), - AllExpression.class); + final AllExpression all = checkExpressionType(any.getElements().get(1), AllExpression.class); assertThat(all.getElements(), iterableWithSize(3)); - final FieldExpression fieldAvengers = checkExpressionType(all.getElements().get(0), - FieldExpression.class); + final FieldExpression fieldAvengers = checkExpressionType(all.getElements().get(0), FieldExpression.class); assertThat(fieldAvengers.getField(), equalTo("username")); assertThat(fieldAvengers.getValues(), iterableWithSize(1)); final FieldValue valueAvengers = fieldAvengers.getValues().get(0); @@ -86,17 +84,14 @@ public void testParseComplexExpression() throws Exception { assertThat(valueAvengers.getAutomaton().run("romanov@avengers.org"), equalTo(true)); assertThat(valueAvengers.getAutomaton().run("fury@shield.gov"), equalTo(false)); - final FieldExpression fieldGroupsAdmin = checkExpressionType(all.getElements().get(1), - FieldExpression.class); + final FieldExpression fieldGroupsAdmin = checkExpressionType(all.getElements().get(1), FieldExpression.class); assertThat(fieldGroupsAdmin.getField(), equalTo("groups")); assertThat(fieldGroupsAdmin.getValues(), iterableWithSize(2)); assertThat(fieldGroupsAdmin.getValues().get(0).getValue(), equalTo("admin")); assertThat(fieldGroupsAdmin.getValues().get(1).getValue(), equalTo("operators")); - final ExceptExpression except = checkExpressionType(all.getElements().get(2), - ExceptExpression.class); - final FieldExpression fieldDisavowed = checkExpressionType(except.getInnerExpression(), - FieldExpression.class); + final ExceptExpression except = checkExpressionType(all.getElements().get(2), ExceptExpression.class); + final FieldExpression fieldDisavowed = checkExpressionType(except.getInnerExpression(), FieldExpression.class); assertThat(fieldDisavowed.getField(), equalTo("groups")); assertThat(fieldDisavowed.getValues(), iterableWithSize(1)); assertThat(fieldDisavowed.getValues().get(0).getValue(), equalTo("disavowed")); @@ -124,16 +119,16 @@ public void testParseComplexExpression() throws Exception { } public void testWriteAndReadFromStream() throws Exception { - String json = "{ \"any\": [" + - " { \"field\": { \"username\" : \"*@shield.gov\" } }, " + - " { \"all\": [" + - " { \"field\": { \"username\" : \"/.*\\\\@avengers\\\\.(net|org)/\" } }, " + - " { \"field\": { \"groups\" : [ \"admin\", \"operators\" ] } }, " + - " { \"except\":" + - " { \"field\": { \"groups\" : \"disavowed\" } }" + - " }" + - " ] }" + - "] }"; + String json = "{ \"any\": [" + + " { \"field\": { \"username\" : \"*@shield.gov\" } }, " + + " { \"all\": [" + + " { \"field\": { \"username\" : \"/.*\\\\@avengers\\\\.(net|org)/\" } }, " + + " { \"field\": { \"groups\" : [ \"admin\", \"operators\" ] } }, " + + " { \"except\":" + + " { \"field\": { \"groups\" : \"disavowed\" } }" + + " }" + + " ] }" + + "] }"; final RoleMapperExpression exprSource = parse(json); final BytesStreamOutput out = new BytesStreamOutput(); ExpressionParser.writeExpression(exprSource, out); @@ -151,8 +146,7 @@ private T checkExpressionType(RoleMapperExpression expr, Class type) { } private RoleMapperExpression parse(String json) throws IOException { - return new ExpressionParser().parse("rules", new XContentSource(new BytesArray(json), - XContentType.JSON)); + return new ExpressionParser().parse("rules", new XContentSource(new BytesArray(json), XContentType.JSON)); } private String json(RoleMapperExpression node) throws IOException { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/DocumentSubsetBitsetCacheTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/DocumentSubsetBitsetCacheTests.java index a3590bd4f7a23..ac99e492088a2 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/DocumentSubsetBitsetCacheTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/DocumentSubsetBitsetCacheTests.java @@ -28,9 +28,9 @@ import org.apache.lucene.util.FixedBitSet; import org.elasticsearch.client.Client; import org.elasticsearch.common.CheckedBiConsumer; -import org.elasticsearch.core.CheckedConsumer; import org.elasticsearch.common.logging.Loggers; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.CheckedConsumer; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.mapper.KeywordFieldMapper; @@ -191,7 +191,7 @@ public void testLogWarningIfBitSetExceedsCacheSize() throws Exception { final long expectedBytesPerBitSet = 56; // Enough to hold less than 1 bit-sets in the cache - final long maxCacheBytes = expectedBytesPerBitSet - expectedBytesPerBitSet/3; + final long maxCacheBytes = expectedBytesPerBitSet - expectedBytesPerBitSet / 3; final Settings settings = Settings.builder() .put(DocumentSubsetBitsetCache.CACHE_SIZE_SETTING.getKey(), maxCacheBytes + "b") .build(); @@ -204,14 +204,19 @@ public void testLogWarningIfBitSetExceedsCacheSize() throws Exception { mockAppender.start(); try { Loggers.addAppender(cacheLogger, mockAppender); - mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation( - "[bitset too big]", - cache.getClass().getName(), - Level.WARN, - "built a DLS BitSet that uses [" + expectedBytesPerBitSet + "] bytes; the DLS BitSet cache has a maximum size of [" + - maxCacheBytes + "] bytes; this object cannot be cached and will need to be rebuilt for each use;" + - " consider increasing the value of [xpack.security.dls.bitset.cache.size]" - )); + mockAppender.addExpectation( + new MockLogAppender.SeenEventExpectation( + "[bitset too big]", + cache.getClass().getName(), + Level.WARN, + "built a DLS BitSet that uses [" + + expectedBytesPerBitSet + + "] bytes; the DLS BitSet cache has a maximum size of [" + + maxCacheBytes + + "] bytes; this object cannot be cached and will need to be rebuilt for each use;" + + " consider increasing the value of [xpack.security.dls.bitset.cache.size]" + ) + ); runTestOnIndex((searchExecutionContext, leafContext) -> { final TermQueryBuilder queryBuilder = QueryBuilders.termQuery("field-1", "value-1"); @@ -234,7 +239,7 @@ public void testLogMessageIfCacheFull() throws Exception { final long expectedBytesPerBitSet = 56; // Enough to hold slightly more than 1 bit-sets in the cache - final long maxCacheBytes = expectedBytesPerBitSet + expectedBytesPerBitSet/3; + final long maxCacheBytes = expectedBytesPerBitSet + expectedBytesPerBitSet / 3; final Settings settings = Settings.builder() .put(DocumentSubsetBitsetCache.CACHE_SIZE_SETTING.getKey(), maxCacheBytes + "b") .build(); @@ -247,13 +252,15 @@ public void testLogMessageIfCacheFull() throws Exception { mockAppender.start(); try { Loggers.addAppender(cacheLogger, mockAppender); - mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation( - "[cache full]", - cache.getClass().getName(), - Level.INFO, - "the Document Level Security BitSet cache is full which may impact performance;" + - " consider increasing the value of [xpack.security.dls.bitset.cache.size]" - )); + mockAppender.addExpectation( + new MockLogAppender.SeenEventExpectation( + "[cache full]", + cache.getClass().getName(), + Level.INFO, + "the Document Level Security BitSet cache is full which may impact performance;" + + " consider increasing the value of [xpack.security.dls.bitset.cache.size]" + ) + ); runTestOnIndex((searchExecutionContext, leafContext) -> { for (int i = 1; i <= 3; i++) { @@ -273,9 +280,7 @@ public void testLogMessageIfCacheFull() throws Exception { } public void testCacheRespectsAccessTimeExpiry() throws Exception { - final Settings settings = Settings.builder() - .put(DocumentSubsetBitsetCache.CACHE_TTL_SETTING.getKey(), "10ms") - .build(); + final Settings settings = Settings.builder().put(DocumentSubsetBitsetCache.CACHE_TTL_SETTING.getKey(), "10ms").build(); final DocumentSubsetBitsetCache cache = newCache(settings); assertThat(cache.entryCount(), equalTo(0)); assertThat(cache.ramBytesUsed(), equalTo(0L)); @@ -307,7 +312,7 @@ public void testIndexLookupIsClearedWhenBitSetIsEvicted() throws Exception { final long expectedBytesPerBitSet = 56; // Enough to hold slightly more than 1 bit-set in the cache - final long maxCacheBytes = expectedBytesPerBitSet + expectedBytesPerBitSet/2; + final long maxCacheBytes = expectedBytesPerBitSet + expectedBytesPerBitSet / 2; final Settings settings = Settings.builder() .put(DocumentSubsetBitsetCache.CACHE_SIZE_SETTING.getKey(), maxCacheBytes + "b") .build(); @@ -479,7 +484,7 @@ public void testRoleBitSets() throws Exception { FixedBitSet matches = new FixedBitSet(maxDocs); for (int i = 0; i < maxDocs; i++) { if (numDocs < maxDocs && randomBoolean()) { - numDocs ++; + numDocs++; matches.set(i); } } @@ -524,8 +529,13 @@ private static final class TestIndexContext implements Closeable { private final SearchExecutionContext searchExecutionContext; private final LeafReaderContext leafReaderContext; - private TestIndexContext(Directory directory, IndexWriter indexWriter, DirectoryReader directoryReader, - SearchExecutionContext searchExecutionContext, LeafReaderContext leafReaderContext) { + private TestIndexContext( + Directory directory, + IndexWriter indexWriter, + DirectoryReader directoryReader, + SearchExecutionContext searchExecutionContext, + LeafReaderContext leafReaderContext + ) { this.directory = directory; this.indexWriter = indexWriter; this.directoryReader = directoryReader; @@ -568,9 +578,27 @@ private TestIndexContext testIndex(MappingLookup mappingLookup, Client client) t directoryReader = DirectoryReader.open(directory); final LeafReaderContext leaf = directoryReader.leaves().get(0); - final SearchExecutionContext searchExecutionContext = new SearchExecutionContext(shardId.id(), 0, indexSettings, - null, null, null, mappingLookup, null, null, xContentRegistry(), writableRegistry(), - client, new IndexSearcher(directoryReader), () -> nowInMillis, null, null, () -> true, null, emptyMap()); + final SearchExecutionContext searchExecutionContext = new SearchExecutionContext( + shardId.id(), + 0, + indexSettings, + null, + null, + null, + mappingLookup, + null, + null, + xContentRegistry(), + writableRegistry(), + client, + new IndexSearcher(directoryReader), + () -> nowInMillis, + null, + null, + () -> true, + null, + emptyMap() + ); context = new TestIndexContext(directory, iw, directoryReader, searchExecutionContext, leaf); return context; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/DocumentSubsetReaderTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/DocumentSubsetReaderTests.java index 3559ba5cbcf1f..2115a65033750 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/DocumentSubsetReaderTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/DocumentSubsetReaderTests.java @@ -24,11 +24,11 @@ import org.apache.lucene.search.TopDocs; import org.apache.lucene.store.Directory; import org.apache.lucene.util.Bits; -import org.elasticsearch.common.lucene.index.SequentialStoredFieldsLeafReader; -import org.elasticsearch.core.internal.io.IOUtils; import org.apache.lucene.util.TestUtil; import org.elasticsearch.common.lucene.index.ElasticsearchDirectoryReader; +import org.elasticsearch.common.lucene.index.SequentialStoredFieldsLeafReader; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.test.ESTestCase; import org.hamcrest.Matchers; @@ -52,8 +52,7 @@ public void setUpDirectory() { // We check it is empty at the end of the test, so make sure it is empty in the // beginning as well so that we can easily distinguish from garbage added by // this test and garbage not cleaned up by other tests. - assertTrue(DocumentSubsetReader.NUM_DOCS_CACHE.toString(), - DocumentSubsetReader.NUM_DOCS_CACHE.isEmpty()); + assertTrue(DocumentSubsetReader.NUM_DOCS_CACHE.toString(), DocumentSubsetReader.NUM_DOCS_CACHE.isEmpty()); directory = newDirectory(); bitsetCache = new DocumentSubsetBitsetCache(Settings.EMPTY, Executors.newSingleThreadExecutor()); } @@ -63,8 +62,7 @@ public void cleanDirectory() throws Exception { if (directoryReader != null) { directoryReader.close(); } - assertTrue(DocumentSubsetReader.NUM_DOCS_CACHE.toString(), - DocumentSubsetReader.NUM_DOCS_CACHE.isEmpty()); + assertTrue(DocumentSubsetReader.NUM_DOCS_CACHE.toString(), DocumentSubsetReader.NUM_DOCS_CACHE.isEmpty()); directory.close(); bitsetCache.close(); } @@ -93,29 +91,33 @@ public void testSearch() throws Exception { iw.close(); openDirectoryReader(); - IndexSearcher indexSearcher = new IndexSearcher(DocumentSubsetReader.wrap(directoryReader, bitsetCache, - new TermQuery(new Term("field", "value1")))); + IndexSearcher indexSearcher = new IndexSearcher( + DocumentSubsetReader.wrap(directoryReader, bitsetCache, new TermQuery(new Term("field", "value1"))) + ); assertThat(indexSearcher.getIndexReader().numDocs(), equalTo(1)); TopDocs result = indexSearcher.search(new MatchAllDocsQuery(), 1); assertThat(result.totalHits.value, equalTo(1L)); assertThat(result.scoreDocs[0].doc, equalTo(0)); - indexSearcher = new IndexSearcher(DocumentSubsetReader.wrap(directoryReader, bitsetCache, - new TermQuery(new Term("field", "value2")))); + indexSearcher = new IndexSearcher( + DocumentSubsetReader.wrap(directoryReader, bitsetCache, new TermQuery(new Term("field", "value2"))) + ); assertThat(indexSearcher.getIndexReader().numDocs(), equalTo(1)); result = indexSearcher.search(new MatchAllDocsQuery(), 1); assertThat(result.totalHits.value, equalTo(1L)); assertThat(result.scoreDocs[0].doc, equalTo(1)); // this doc has been marked as deleted: - indexSearcher = new IndexSearcher(DocumentSubsetReader.wrap(directoryReader, bitsetCache, - new TermQuery(new Term("field", "value3")))); + indexSearcher = new IndexSearcher( + DocumentSubsetReader.wrap(directoryReader, bitsetCache, new TermQuery(new Term("field", "value3"))) + ); assertThat(indexSearcher.getIndexReader().numDocs(), equalTo(0)); result = indexSearcher.search(new MatchAllDocsQuery(), 1); assertThat(result.totalHits.value, equalTo(0L)); - indexSearcher = new IndexSearcher(DocumentSubsetReader.wrap(directoryReader, bitsetCache, - new TermQuery(new Term("field", "value4")))); + indexSearcher = new IndexSearcher( + DocumentSubsetReader.wrap(directoryReader, bitsetCache, new TermQuery(new Term("field", "value4"))) + ); assertThat(indexSearcher.getIndexReader().numDocs(), equalTo(1)); result = indexSearcher.search(new MatchAllDocsQuery(), 1); assertThat(result.totalHits.value, equalTo(1L)); @@ -124,10 +126,7 @@ public void testSearch() throws Exception { public void testLiveDocs() throws Exception { int numDocs = scaledRandomIntBetween(16, 128); - IndexWriter iw = new IndexWriter( - directory, - new IndexWriterConfig(new StandardAnalyzer()).setMergePolicy(NoMergePolicy.INSTANCE) - ); + IndexWriter iw = new IndexWriter(directory, new IndexWriterConfig(new StandardAnalyzer()).setMergePolicy(NoMergePolicy.INSTANCE)); for (int i = 0; i < numDocs; i++) { Document document = new Document(); @@ -152,9 +151,9 @@ public void testLiveDocs() throws Exception { assertThat(liveDocs.length(), equalTo(numDocs)); for (int docId = 0; docId < numDocs; docId++) { if (docId == i) { - assertThat("docId [" + docId +"] should match", liveDocs.get(docId), is(true)); + assertThat("docId [" + docId + "] should match", liveDocs.get(docId), is(true)); } else { - assertThat("docId [" + docId +"] should not match", liveDocs.get(docId), is(false)); + assertThat("docId [" + docId + "] should not match", liveDocs.get(docId), is(false)); } } } @@ -170,8 +169,13 @@ public void testWrapTwice() throws Exception { DocumentSubsetReader.wrap(directoryReader, bitsetCache, new MatchAllDocsQuery()); fail("shouldn't be able to wrap DocumentSubsetDirectoryReader twice"); } catch (IllegalArgumentException e) { - assertThat(e.getMessage(), equalTo("Can't wrap [class org.elasticsearch.xpack.core.security.authz.accesscontrol" + - ".DocumentSubsetReader$DocumentSubsetDirectoryReader] twice")); + assertThat( + e.getMessage(), + equalTo( + "Can't wrap [class org.elasticsearch.xpack.core.security.authz.accesscontrol" + + ".DocumentSubsetReader$DocumentSubsetDirectoryReader] twice" + ) + ); } bitsetCache.close(); @@ -209,8 +213,7 @@ public void testCoreCacheKey() throws Exception { // we should have the same cache key as before assertEquals(1, ir2.numDocs()); assertEquals(1, ir2.leaves().size()); - assertSame(ir.leaves().get(0).reader().getCoreCacheHelper().getKey(), - ir2.leaves().get(0).reader().getCoreCacheHelper().getKey()); + assertSame(ir.leaves().get(0).reader().getCoreCacheHelper().getKey(), ir2.leaves().get(0).reader().getCoreCacheHelper().getKey()); // However we don't support caching on the reader cache key since we override deletes assertNull(ir.leaves().get(0).reader().getReaderCacheHelper()); assertNull(ir2.leaves().get(0).reader().getReaderCacheHelper()); @@ -246,7 +249,7 @@ public void testProducesStoredFieldsReader() throws Exception { TestUtil.checkReader(reader); assertThat(reader.leaves().size(), Matchers.greaterThanOrEqualTo(1)); - for (LeafReaderContext context: reader.leaves()) { + for (LeafReaderContext context : reader.leaves()) { assertThat(context.reader(), Matchers.instanceOf(SequentialStoredFieldsLeafReader.class)); SequentialStoredFieldsLeafReader lf = (SequentialStoredFieldsLeafReader) context.reader(); assertNotNull(lf.getSequentialStoredFieldsReader()); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/FieldSubsetReaderTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/FieldSubsetReaderTests.java index bc8a6c583449a..dfeb31e1b9580 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/FieldSubsetReaderTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/FieldSubsetReaderTests.java @@ -189,8 +189,8 @@ public void testKnnVectors() throws Exception { IndexWriter iw = new IndexWriter(dir, iwc); Document doc = new Document(); - doc.add(new KnnVectorField("fieldA", new float[] {0.1f, 0.2f, 0.3f})); - doc.add(new KnnVectorField("fieldB", new float[] {3.0f, 2.0f, 1.0f})); + doc.add(new KnnVectorField("fieldA", new float[] { 0.1f, 0.2f, 0.3f })); + doc.add(new KnnVectorField("fieldB", new float[] { 3.0f, 2.0f, 1.0f })); iw.addDocument(doc); DirectoryReader ir = FieldSubsetReader.wrap(DirectoryReader.open(iw), new CharacterRunAutomaton(Automata.makeString("fieldA"))); @@ -203,13 +203,13 @@ public void testKnnVectors() throws Exception { assertEquals(0, vectorValues.nextDoc()); assertNotNull(vectorValues.binaryValue()); - TopDocs topDocs = leafReader.searchNearestVectors("fieldA", new float[] {1.0f, 1.0f, 1.0f}, 5, null); + TopDocs topDocs = leafReader.searchNearestVectors("fieldA", new float[] { 1.0f, 1.0f, 1.0f }, 5, null); assertNotNull(topDocs); assertEquals(1, topDocs.scoreDocs.length); // Check that we can't see fieldB assertNull(leafReader.getVectorValues("fieldB")); - assertNull(leafReader.searchNearestVectors("fieldB", new float[] {1.0f, 1.0f, 1.0f}, 5, null)); + assertNull(leafReader.searchNearestVectors("fieldB", new float[] { 1.0f, 1.0f, 1.0f }, 5, null)); TestUtil.checkReader(ir); IOUtils.close(ir, iw, dir); @@ -678,9 +678,9 @@ public void testSourceFiltering() { assertEquals(expected, filtered); // exclude on exact value - include = new CharacterRunAutomaton(Operations.minus( - Automata.makeAnyString(), Automatons.patterns("foo.bar"), - Operations.DEFAULT_DETERMINIZE_WORK_LIMIT)); + include = new CharacterRunAutomaton( + Operations.minus(Automata.makeAnyString(), Automatons.patterns("foo.bar"), Operations.DEFAULT_DETERMINIZE_WORK_LIMIT) + ); filtered = FieldSubsetReader.filter(map, include, 0); expected = new HashMap<>(); expected.put("bar", "baz"); @@ -689,9 +689,9 @@ public void testSourceFiltering() { assertEquals(expected, filtered); // exclude on wildcard - include = new CharacterRunAutomaton(Operations.minus( - Automata.makeAnyString(), Automatons.patterns("foo.*"), - Operations.DEFAULT_DETERMINIZE_WORK_LIMIT)); + include = new CharacterRunAutomaton( + Operations.minus(Automata.makeAnyString(), Automatons.patterns("foo.*"), Operations.DEFAULT_DETERMINIZE_WORK_LIMIT) + ); filtered = FieldSubsetReader.filter(map, include, 0); expected = Collections.singletonMap("bar", "baz"); @@ -729,9 +729,9 @@ public void testSourceFiltering() { assertEquals(expected, filtered); // exclude on inner array - include = new CharacterRunAutomaton(Operations.minus( - Automata.makeAnyString(), Automatons.patterns("foo.baz"), - Operations.DEFAULT_DETERMINIZE_WORK_LIMIT)); + include = new CharacterRunAutomaton( + Operations.minus(Automata.makeAnyString(), Automatons.patterns("foo.baz"), Operations.DEFAULT_DETERMINIZE_WORK_LIMIT) + ); filtered = FieldSubsetReader.filter(map, include, 0); expected = new HashMap<>(); subArray = new ArrayList<>(); @@ -744,9 +744,9 @@ public void testSourceFiltering() { assertEquals(expected, filtered); // exclude on inner array 2 - include = new CharacterRunAutomaton(Operations.minus( - Automata.makeAnyString(), Automatons.patterns("foo"), - Operations.DEFAULT_DETERMINIZE_WORK_LIMIT)); + include = new CharacterRunAutomaton( + Operations.minus(Automata.makeAnyString(), Automatons.patterns("foo"), Operations.DEFAULT_DETERMINIZE_WORK_LIMIT) + ); filtered = FieldSubsetReader.filter(map, include, 0); expected = new HashMap<>(); subArray = new ArrayList<>(); @@ -971,8 +971,7 @@ public void testCoreCacheKey() throws Exception { // we should have the same cache key as before assertEquals(1, ir2.numDocs()); assertEquals(1, ir2.leaves().size()); - assertSame(ir.leaves().get(0).reader().getCoreCacheHelper().getKey(), - ir2.leaves().get(0).reader().getCoreCacheHelper().getKey()); + assertSame(ir.leaves().get(0).reader().getCoreCacheHelper().getKey(), ir2.leaves().get(0).reader().getCoreCacheHelper().getKey()); TestUtil.checkReader(ir); IOUtils.close(ir, ir2, iw, dir); @@ -1042,12 +1041,21 @@ public void testWrapTwice() throws Exception { IndexWriter iw = new IndexWriter(dir, iwc); iw.close(); - final DirectoryReader directoryReader = FieldSubsetReader.wrap(DirectoryReader.open(dir), - new CharacterRunAutomaton(Automata.makeString("fieldA"))); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> FieldSubsetReader.wrap(directoryReader, - new CharacterRunAutomaton(Automata.makeString("fieldA")))); - assertThat(e.getMessage(), equalTo("Can't wrap [class org.elasticsearch.xpack.core.security.authz.accesscontrol" + - ".FieldSubsetReader$FieldSubsetDirectoryReader] twice")); + final DirectoryReader directoryReader = FieldSubsetReader.wrap( + DirectoryReader.open(dir), + new CharacterRunAutomaton(Automata.makeString("fieldA")) + ); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> FieldSubsetReader.wrap(directoryReader, new CharacterRunAutomaton(Automata.makeString("fieldA"))) + ); + assertThat( + e.getMessage(), + equalTo( + "Can't wrap [class org.elasticsearch.xpack.core.security.authz.accesscontrol" + + ".FieldSubsetReader$FieldSubsetDirectoryReader] twice" + ) + ); directoryReader.close(); dir.close(); } @@ -1055,16 +1063,26 @@ public void testWrapTwice() throws Exception { @SuppressWarnings("unchecked") public void testMappingsFilteringDuelWithSourceFiltering() throws Exception { Metadata metadata = Metadata.builder() - .put(IndexMetadata.builder("index") - .settings(Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)) - .putMapping(MAPPING_TEST_ITEM)).build(); + .put( + IndexMetadata.builder("index") + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + ) + .putMapping(MAPPING_TEST_ITEM) + ) + .build(); { - FieldPermissionsDefinition definition = new FieldPermissionsDefinition(new String[]{"*inner1"}, Strings.EMPTY_ARRAY); + FieldPermissionsDefinition definition = new FieldPermissionsDefinition(new String[] { "*inner1" }, Strings.EMPTY_ARRAY); FieldPermissions fieldPermissions = new FieldPermissions(definition); - ImmutableOpenMap mappings = metadata.findMappings(new String[]{"index"}, - index -> fieldPermissions::grantsAccessTo, Metadata.ON_NEXT_INDEX_FIND_MAPPINGS_NOOP); + ImmutableOpenMap mappings = metadata.findMappings( + new String[] { "index" }, + index -> fieldPermissions::grantsAccessTo, + Metadata.ON_NEXT_INDEX_FIND_MAPPINGS_NOOP + ); MappingMetadata index = mappings.get("index"); Map sourceAsMap = index.getSourceAsMap(); assertEquals(1, sourceAsMap.size()); @@ -1087,10 +1105,10 @@ public void testMappingsFilteringDuelWithSourceFiltering() throws Exception { Map stringObjectMap = XContentHelper.convertToMap(XContentType.JSON.xContent(), DOC_TEST_ITEM, false); Map filtered = FieldSubsetReader.filter(stringObjectMap, include, 0); assertEquals(2, filtered.size()); - Map object = (Map)filtered.get("object"); + Map object = (Map) filtered.get("object"); assertEquals(1, object.size()); assertTrue(object.containsKey("inner1")); - List> nested = (List>)filtered.get("nested"); + List> nested = (List>) filtered.get("nested"); assertEquals(2, nested.size()); for (Map objectMap : nested) { assertEquals(1, objectMap.size()); @@ -1098,10 +1116,13 @@ public void testMappingsFilteringDuelWithSourceFiltering() throws Exception { } } { - FieldPermissionsDefinition definition = new FieldPermissionsDefinition(new String[]{"object*"}, Strings.EMPTY_ARRAY); + FieldPermissionsDefinition definition = new FieldPermissionsDefinition(new String[] { "object*" }, Strings.EMPTY_ARRAY); FieldPermissions fieldPermissions = new FieldPermissions(definition); - ImmutableOpenMap mappings = metadata.findMappings(new String[]{"index"}, - index -> fieldPermissions::grantsAccessTo, Metadata.ON_NEXT_INDEX_FIND_MAPPINGS_NOOP); + ImmutableOpenMap mappings = metadata.findMappings( + new String[] { "index" }, + index -> fieldPermissions::grantsAccessTo, + Metadata.ON_NEXT_INDEX_FIND_MAPPINGS_NOOP + ); MappingMetadata index = mappings.get("index"); Map sourceAsMap = index.getSourceAsMap(); assertEquals(1, sourceAsMap.size()); @@ -1128,16 +1149,19 @@ public void testMappingsFilteringDuelWithSourceFiltering() throws Exception { Map stringObjectMap = XContentHelper.convertToMap(XContentType.JSON.xContent(), DOC_TEST_ITEM, false); Map filtered = FieldSubsetReader.filter(stringObjectMap, include, 0); assertEquals(1, filtered.size()); - Map object = (Map)filtered.get("object"); + Map object = (Map) filtered.get("object"); assertEquals(2, object.size()); assertTrue(object.containsKey("inner1")); assertTrue(object.containsKey("inner2")); } { - FieldPermissionsDefinition definition = new FieldPermissionsDefinition(new String[]{"object"}, Strings.EMPTY_ARRAY); + FieldPermissionsDefinition definition = new FieldPermissionsDefinition(new String[] { "object" }, Strings.EMPTY_ARRAY); FieldPermissions fieldPermissions = new FieldPermissions(definition); - ImmutableOpenMap mappings = metadata.findMappings(new String[]{"index"}, - index -> fieldPermissions::grantsAccessTo, Metadata.ON_NEXT_INDEX_FIND_MAPPINGS_NOOP); + ImmutableOpenMap mappings = metadata.findMappings( + new String[] { "index" }, + index -> fieldPermissions::grantsAccessTo, + Metadata.ON_NEXT_INDEX_FIND_MAPPINGS_NOOP + ); MappingMetadata index = mappings.get("index"); Map sourceAsMap = index.getSourceAsMap(); assertEquals(1, sourceAsMap.size()); @@ -1152,18 +1176,21 @@ public void testMappingsFilteringDuelWithSourceFiltering() throws Exception { CharacterRunAutomaton include = new CharacterRunAutomaton(automaton); Map stringObjectMap = XContentHelper.convertToMap(XContentType.JSON.xContent(), DOC_TEST_ITEM, false); Map filtered = FieldSubsetReader.filter(stringObjectMap, include, 0); - //TODO FLS filters out empty objects from source, although they are granted access. - //When filtering mappings though we keep them. + // TODO FLS filters out empty objects from source, although they are granted access. + // When filtering mappings though we keep them. assertEquals(0, filtered.size()); /*assertEquals(1, filtered.size()); Map object = (Map)filtered.get("object"); assertEquals(0, object.size());*/ } { - FieldPermissionsDefinition definition = new FieldPermissionsDefinition(new String[]{"nested.inner2"}, Strings.EMPTY_ARRAY); + FieldPermissionsDefinition definition = new FieldPermissionsDefinition(new String[] { "nested.inner2" }, Strings.EMPTY_ARRAY); FieldPermissions fieldPermissions = new FieldPermissions(definition); - ImmutableOpenMap mappings = metadata.findMappings(new String[]{"index"}, - index -> fieldPermissions::grantsAccessTo, Metadata.ON_NEXT_INDEX_FIND_MAPPINGS_NOOP); + ImmutableOpenMap mappings = metadata.findMappings( + new String[] { "index" }, + index -> fieldPermissions::grantsAccessTo, + Metadata.ON_NEXT_INDEX_FIND_MAPPINGS_NOOP + ); MappingMetadata index = mappings.get("index"); Map sourceAsMap = index.getSourceAsMap(); assertEquals(1, sourceAsMap.size()); @@ -1181,7 +1208,7 @@ public void testMappingsFilteringDuelWithSourceFiltering() throws Exception { Map stringObjectMap = XContentHelper.convertToMap(XContentType.JSON.xContent(), DOC_TEST_ITEM, false); Map filtered = FieldSubsetReader.filter(stringObjectMap, include, 0); assertEquals(1, filtered.size()); - List> nested = (List>)filtered.get("nested"); + List> nested = (List>) filtered.get("nested"); assertEquals(2, nested.size()); for (Map objectMap : nested) { assertEquals(1, objectMap.size()); @@ -1215,7 +1242,7 @@ public void testProducesStoredFieldsReader() throws Exception { TestUtil.checkReader(ir); assertThat(ir.leaves().size(), Matchers.greaterThanOrEqualTo(1)); - for (LeafReaderContext context: ir.leaves()) { + for (LeafReaderContext context : ir.leaves()) { assertThat(context.reader(), Matchers.instanceOf(SequentialStoredFieldsLeafReader.class)); SequentialStoredFieldsLeafReader lf = (SequentialStoredFieldsLeafReader) context.reader(); assertNotNull(lf.getSequentialStoredFieldsReader()); @@ -1223,57 +1250,57 @@ public void testProducesStoredFieldsReader() throws Exception { IOUtils.close(ir, iw, dir); } - private static final String DOC_TEST_ITEM = "{\n" + - " \"field_text\" : \"text\",\n" + - " \"object\" : {\n" + - " \"inner1\" : \"text\",\n" + - " \"inner2\" : \"keyword\"\n" + - " },\n" + - " \"nested\" : [\n" + - " {\n" + - " \"inner1\" : 1,\n" + - " \"inner2\" : \"2017/12/12\"\n" + - " },\n" + - " {\n" + - " \"inner1\" : 2,\n" + - " \"inner2\" : \"2017/11/11\"\n" + - " }\n" + - " ]\n" + - "}"; - - private static final String MAPPING_TEST_ITEM = "{\n" + - " \"_doc\": {\n" + - " \"properties\" : {\n" + - " \"field_text\" : {\n" + - " \"type\":\"text\"\n" + - " },\n" + - " \"object\" : {\n" + - " \"properties\" : {\n" + - " \"inner1\" : {\n" + - " \"type\": \"text\",\n" + - " \"fields\" : {\n" + - " \"keyword\" : {\n" + - " \"type\" : \"keyword\"\n" + - " }\n" + - " }\n" + - " },\n" + - " \"inner2\" : {\n" + - " \"type\": \"keyword\"\n" + - " }\n" + - " }\n" + - " },\n" + - " \"nested\" : {\n" + - " \"type\":\"nested\",\n" + - " \"properties\" : {\n" + - " \"inner1\" : {\n" + - " \"type\": \"integer\"\n" + - " },\n" + - " \"inner2\" : {\n" + - " \"type\": \"date\"\n" + - " }\n" + - " }\n" + - " }\n" + - " }\n" + - " }\n" + - "}"; + private static final String DOC_TEST_ITEM = "{\n" + + " \"field_text\" : \"text\",\n" + + " \"object\" : {\n" + + " \"inner1\" : \"text\",\n" + + " \"inner2\" : \"keyword\"\n" + + " },\n" + + " \"nested\" : [\n" + + " {\n" + + " \"inner1\" : 1,\n" + + " \"inner2\" : \"2017/12/12\"\n" + + " },\n" + + " {\n" + + " \"inner1\" : 2,\n" + + " \"inner2\" : \"2017/11/11\"\n" + + " }\n" + + " ]\n" + + "}"; + + private static final String MAPPING_TEST_ITEM = "{\n" + + " \"_doc\": {\n" + + " \"properties\" : {\n" + + " \"field_text\" : {\n" + + " \"type\":\"text\"\n" + + " },\n" + + " \"object\" : {\n" + + " \"properties\" : {\n" + + " \"inner1\" : {\n" + + " \"type\": \"text\",\n" + + " \"fields\" : {\n" + + " \"keyword\" : {\n" + + " \"type\" : \"keyword\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"inner2\" : {\n" + + " \"type\": \"keyword\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"nested\" : {\n" + + " \"type\":\"nested\",\n" + + " \"properties\" : {\n" + + " \"inner1\" : {\n" + + " \"type\": \"integer\"\n" + + " },\n" + + " \"inner2\" : {\n" + + " \"type\": \"date\"\n" + + " }\n" + + " }\n" + + " }\n" + + " }\n" + + " }\n" + + "}"; } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/SecurityIndexReaderWrapperIntegrationTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/SecurityIndexReaderWrapperIntegrationTests.java index ec32e37511e38..0e09a09646686 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/SecurityIndexReaderWrapperIntegrationTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/SecurityIndexReaderWrapperIntegrationTests.java @@ -87,19 +87,34 @@ public void testDLS() throws Exception { Client client = mock(Client.class); when(client.settings()).thenReturn(Settings.EMPTY); final long nowInMillis = randomNonNegativeLong(); - SearchExecutionContext realSearchExecutionContext = new SearchExecutionContext(shardId.id(), 0, indexSettings, - null, null, null, mappingLookup, null, null, xContentRegistry(), writableRegistry(), - client, null, () -> nowInMillis, null, null, () -> true, null, emptyMap()); + SearchExecutionContext realSearchExecutionContext = new SearchExecutionContext( + shardId.id(), + 0, + indexSettings, + null, + null, + null, + mappingLookup, + null, + null, + xContentRegistry(), + writableRegistry(), + client, + null, + () -> nowInMillis, + null, + null, + () -> true, + null, + emptyMap() + ); SearchExecutionContext searchExecutionContext = spy(realSearchExecutionContext); DocumentSubsetBitsetCache bitsetCache = new DocumentSubsetBitsetCache(Settings.EMPTY, Executors.newSingleThreadExecutor()); final MockLicenseState licenseState = mock(MockLicenseState.class); when(licenseState.isAllowed(DOCUMENT_LEVEL_SECURITY_FEATURE)).thenReturn(true); Directory directory = newDirectory(); - IndexWriter iw = new IndexWriter( - directory, - new IndexWriterConfig(new StandardAnalyzer()).setMergePolicy(NoMergePolicy.INSTANCE) - ); + IndexWriter iw = new IndexWriter(directory, new IndexWriterConfig(new StandardAnalyzer()).setMergePolicy(NoMergePolicy.INSTANCE)); int numValues = scaledRandomIntBetween(2, 16); String[] values = new String[numValues]; @@ -110,8 +125,12 @@ null, null, null, mappingLookup, null, null, xContentRegistry(), writableRegistr int numDocs = scaledRandomIntBetween(32, 128); int commitAfter = scaledRandomIntBetween(1, numDocs); - logger.info("Going to index [{}] documents with [{}] unique values and commit after [{}] documents have been indexed", - numDocs, numValues, commitAfter); + logger.info( + "Going to index [{}] documents with [{}] unique values and commit after [{}] documents have been indexed", + numDocs, + numValues, + commitAfter + ); for (int doc = 1; doc <= numDocs; doc++) { int valueIndex = (numValues - 1) % doc; @@ -140,12 +159,19 @@ null, null, null, mappingLookup, null, null, xContentRegistry(), writableRegistr DirectoryReader directoryReader = ElasticsearchDirectoryReader.wrap(DirectoryReader.open(directory), shardId); for (int i = 0; i < numValues; i++) { - String termQuery = "{\"term\": {\"field\": \""+ values[i] + "\"} }"; - IndicesAccessControl.IndexAccessControl indexAccessControl = new IndicesAccessControl.IndexAccessControl(true, new - FieldPermissions(), - DocumentPermissions.filteredBy(singleton(new BytesArray(termQuery)))); - SecurityIndexReaderWrapper wrapper = new SecurityIndexReaderWrapper(s -> searchExecutionContext, - bitsetCache, securityContext, licenseState, scriptService) { + String termQuery = "{\"term\": {\"field\": \"" + values[i] + "\"} }"; + IndicesAccessControl.IndexAccessControl indexAccessControl = new IndicesAccessControl.IndexAccessControl( + true, + new FieldPermissions(), + DocumentPermissions.filteredBy(singleton(new BytesArray(termQuery))) + ); + SecurityIndexReaderWrapper wrapper = new SecurityIndexReaderWrapper( + s -> searchExecutionContext, + bitsetCache, + securityContext, + licenseState, + scriptService + ) { @Override protected IndicesAccessControl getIndicesAccessControl() { @@ -158,8 +184,12 @@ protected IndicesAccessControl getIndicesAccessControl() { DirectoryReader wrappedDirectoryReader = wrapper.apply(directoryReader); IndexSearcher indexSearcher = new ContextIndexSearcher( - wrappedDirectoryReader, IndexSearcher.getDefaultSimilarity(), IndexSearcher.getDefaultQueryCache(), - IndexSearcher.getDefaultQueryCachingPolicy(), true); + wrappedDirectoryReader, + IndexSearcher.getDefaultSimilarity(), + IndexSearcher.getDefaultQueryCache(), + IndexSearcher.getDefaultQueryCachingPolicy(), + true + ); int expectedHitCount = valuesHitCount[i]; logger.info("Going to verify hit count with query [{}] with expected total hits [{}]", parsedQuery.query(), expectedHitCount); @@ -178,10 +208,8 @@ protected IndicesAccessControl getIndicesAccessControl() { public void testDLSWithLimitedPermissions() throws Exception { ShardId shardId = new ShardId("_index", "_na_", 0); - MappingLookup mappingLookup = createMappingLookup(List.of( - new KeywordFieldType("field"), - new KeywordFieldType("f1"), - new KeywordFieldType("f2")) + MappingLookup mappingLookup = createMappingLookup( + List.of(new KeywordFieldType("field"), new KeywordFieldType("f1"), new KeywordFieldType("f2")) ); ScriptService scriptService = mock(ScriptService.class); @@ -200,31 +228,60 @@ public void testDLSWithLimitedPermissions() throws Exception { Set queries = new HashSet<>(); queries.add(new BytesArray("{\"terms\" : { \"f2\" : [\"fv22\"] } }")); queries.add(new BytesArray("{\"terms\" : { \"f2\" : [\"fv32\"] } }")); - IndicesAccessControl.IndexAccessControl indexAccessControl = new IndicesAccessControl.IndexAccessControl(true, new - FieldPermissions(), - DocumentPermissions.filteredBy(queries)); + IndicesAccessControl.IndexAccessControl indexAccessControl = new IndicesAccessControl.IndexAccessControl( + true, + new FieldPermissions(), + DocumentPermissions.filteredBy(queries) + ); queries = singleton(new BytesArray("{\"terms\" : { \"f1\" : [\"fv11\", \"fv21\", \"fv31\"] } }")); if (restrictiveLimitedIndexPermissions) { queries = singleton(new BytesArray("{\"terms\" : { \"f1\" : [\"fv11\", \"fv31\"] } }")); } - IndicesAccessControl.IndexAccessControl limitedIndexAccessControl = new IndicesAccessControl.IndexAccessControl(true, new - FieldPermissions(), - DocumentPermissions.filteredBy(queries)); - IndexSettings indexSettings = IndexSettingsModule.newIndexSettings(shardId.getIndex(), - Settings.builder().put(IndexSettings.ALLOW_UNMAPPED.getKey(), false).build()); + IndicesAccessControl.IndexAccessControl limitedIndexAccessControl = new IndicesAccessControl.IndexAccessControl( + true, + new FieldPermissions(), + DocumentPermissions.filteredBy(queries) + ); + IndexSettings indexSettings = IndexSettingsModule.newIndexSettings( + shardId.getIndex(), + Settings.builder().put(IndexSettings.ALLOW_UNMAPPED.getKey(), false).build() + ); Client client = mock(Client.class); when(client.settings()).thenReturn(Settings.EMPTY); final long nowInMillis = randomNonNegativeLong(); - SearchExecutionContext realSearchExecutionContext = new SearchExecutionContext(shardId.id(), 0, indexSettings, - null, null, null, mappingLookup, null, null, xContentRegistry(), writableRegistry(), - client, null, () -> nowInMillis, null, null, () -> true, null, emptyMap()); + SearchExecutionContext realSearchExecutionContext = new SearchExecutionContext( + shardId.id(), + 0, + indexSettings, + null, + null, + null, + mappingLookup, + null, + null, + xContentRegistry(), + writableRegistry(), + client, + null, + () -> nowInMillis, + null, + null, + () -> true, + null, + emptyMap() + ); SearchExecutionContext searchExecutionContext = spy(realSearchExecutionContext); DocumentSubsetBitsetCache bitsetCache = new DocumentSubsetBitsetCache(Settings.EMPTY, Executors.newSingleThreadExecutor()); final MockLicenseState licenseState = mock(MockLicenseState.class); when(licenseState.isAllowed(DOCUMENT_LEVEL_SECURITY_FEATURE)).thenReturn(true); - SecurityIndexReaderWrapper wrapper = new SecurityIndexReaderWrapper(s -> searchExecutionContext, - bitsetCache, securityContext, licenseState, scriptService) { + SecurityIndexReaderWrapper wrapper = new SecurityIndexReaderWrapper( + s -> searchExecutionContext, + bitsetCache, + securityContext, + licenseState, + scriptService + ) { @Override protected IndicesAccessControl getIndicesAccessControl() { @@ -232,17 +289,16 @@ protected IndicesAccessControl getIndicesAccessControl() { if (noFilteredIndexPermissions) { return indicesAccessControl; } - IndicesAccessControl limitedByIndicesAccessControl = new IndicesAccessControl(true, - singletonMap("_index", limitedIndexAccessControl)); + IndicesAccessControl limitedByIndicesAccessControl = new IndicesAccessControl( + true, + singletonMap("_index", limitedIndexAccessControl) + ); return indicesAccessControl.limitIndicesAccessControl(limitedByIndicesAccessControl); } }; Directory directory = newDirectory(); - IndexWriter iw = new IndexWriter( - directory, - new IndexWriterConfig(new StandardAnalyzer()).setMergePolicy(NoMergePolicy.INSTANCE) - ); + IndexWriter iw = new IndexWriter(directory, new IndexWriterConfig(new StandardAnalyzer()).setMergePolicy(NoMergePolicy.INSTANCE)); Document doc1 = new Document(); doc1.add(new StringField("f1", "fv11", Store.NO)); @@ -262,8 +318,12 @@ protected IndicesAccessControl getIndicesAccessControl() { DirectoryReader directoryReader = ElasticsearchDirectoryReader.wrap(DirectoryReader.open(directory), shardId); DirectoryReader wrappedDirectoryReader = wrapper.apply(directoryReader); IndexSearcher indexSearcher = new ContextIndexSearcher( - wrappedDirectoryReader, IndexSearcher.getDefaultSimilarity(), IndexSearcher.getDefaultQueryCache(), - IndexSearcher.getDefaultQueryCachingPolicy(), true); + wrappedDirectoryReader, + IndexSearcher.getDefaultSimilarity(), + IndexSearcher.getDefaultQueryCache(), + IndexSearcher.getDefaultQueryCachingPolicy(), + true + ); ScoreDoc[] hits = indexSearcher.search(new MatchAllDocsQuery(), 1000).scoreDocs; Set actualDocIds = new HashSet<>(); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/SecurityIndexReaderWrapperUnitTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/SecurityIndexReaderWrapperUnitTests.java index 0e49fa50de762..b4644fbc52fe2 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/SecurityIndexReaderWrapperUnitTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/SecurityIndexReaderWrapperUnitTests.java @@ -86,18 +86,20 @@ public void tearDown() throws Exception { } public void testDefaultMetaFields() throws Exception { - securityIndexReaderWrapper = - new SecurityIndexReaderWrapper(null, null, securityContext, licenseState, scriptService) { + securityIndexReaderWrapper = new SecurityIndexReaderWrapper(null, null, securityContext, licenseState, scriptService) { @Override protected IndicesAccessControl getIndicesAccessControl() { - IndicesAccessControl.IndexAccessControl indexAccessControl = new IndicesAccessControl.IndexAccessControl(true, - new FieldPermissions(fieldPermissionDef(new String[]{}, null)), DocumentPermissions.allowAll()); + IndicesAccessControl.IndexAccessControl indexAccessControl = new IndicesAccessControl.IndexAccessControl( + true, + new FieldPermissions(fieldPermissionDef(new String[] {}, null)), + DocumentPermissions.allowAll() + ); return new IndicesAccessControl(true, singletonMap("_index", indexAccessControl)); } }; - FieldSubsetReader.FieldSubsetDirectoryReader result = - (FieldSubsetReader.FieldSubsetDirectoryReader) securityIndexReaderWrapper.apply(esIn); + FieldSubsetReader.FieldSubsetDirectoryReader result = (FieldSubsetReader.FieldSubsetDirectoryReader) securityIndexReaderWrapper + .apply(esIn); assertThat(result.getFilter().run("_uid"), is(true)); assertThat(result.getFilter().run("_id"), is(true)); assertThat(result.getFilter().run("_version"), is(true)); @@ -116,8 +118,7 @@ protected IndicesAccessControl getIndicesAccessControl() { public void testWrapReaderWhenFeatureDisabled() throws Exception { when(licenseState.isAllowed(DOCUMENT_LEVEL_SECURITY_FEATURE)).thenReturn(false); - securityIndexReaderWrapper = - new SecurityIndexReaderWrapper(null, null, securityContext, licenseState, scriptService); + securityIndexReaderWrapper = new SecurityIndexReaderWrapper(null, null, securityContext, licenseState, scriptService); DirectoryReader reader = securityIndexReaderWrapper.apply(esIn); assertThat(reader, sameInstance(esIn)); } @@ -127,17 +128,17 @@ public void testWildcards() throws Exception { expected.add("field1_a"); expected.add("field1_b"); expected.add("field1_c"); - assertResolved(new FieldPermissions(fieldPermissionDef(new String[] {"field1*"}, null)), expected, "field", "field2"); + assertResolved(new FieldPermissions(fieldPermissionDef(new String[] { "field1*" }, null)), expected, "field", "field2"); } public void testDotNotion() throws Exception { Set expected = new HashSet<>(META_FIELDS); expected.add("foo.bar"); - assertResolved(new FieldPermissions(fieldPermissionDef(new String[] {"foo.bar"}, null)), expected, "foo", "foo.baz", "bar.foo"); + assertResolved(new FieldPermissions(fieldPermissionDef(new String[] { "foo.bar" }, null)), expected, "foo", "foo.baz", "bar.foo"); expected = new HashSet<>(META_FIELDS); expected.add("foo.bar"); - assertResolved(new FieldPermissions(fieldPermissionDef(new String[] {"foo.*"}, null)), expected, "foo", "bar"); + assertResolved(new FieldPermissions(fieldPermissionDef(new String[] { "foo.*" }, null)), expected, "foo", "bar"); } private void assertResolved(FieldPermissions permissions, Set expected, String... fieldsToTest) { @@ -150,33 +151,38 @@ private void assertResolved(FieldPermissions permissions, Set expected, } public void testFieldPermissionsWithFieldExceptions() throws Exception { - securityIndexReaderWrapper = - new SecurityIndexReaderWrapper(null, null, securityContext, licenseState, null); - String[] grantedFields = new String[]{}; + securityIndexReaderWrapper = new SecurityIndexReaderWrapper(null, null, securityContext, licenseState, null); + String[] grantedFields = new String[] {}; String[] deniedFields; Set expected = new HashSet<>(META_FIELDS); // Presence of fields in a role with an empty array implies access to no fields except the meta fields - assertResolved(new FieldPermissions(fieldPermissionDef(grantedFields, randomBoolean() ? null : new String[]{})), - expected, "foo", "bar"); + assertResolved( + new FieldPermissions(fieldPermissionDef(grantedFields, randomBoolean() ? null : new String[] {})), + expected, + "foo", + "bar" + ); // make sure meta fields cannot be denied access to deniedFields = META_FIELDS.toArray(new String[0]); - assertResolved(new FieldPermissions(fieldPermissionDef(null, deniedFields)), - new HashSet<>(Arrays.asList("foo", "bar", "_some_plugin_meta_field"))); + assertResolved( + new FieldPermissions(fieldPermissionDef(null, deniedFields)), + new HashSet<>(Arrays.asList("foo", "bar", "_some_plugin_meta_field")) + ); // check we can add all fields with * - grantedFields = new String[]{"*"}; + grantedFields = new String[] { "*" }; expected = new HashSet<>(META_FIELDS); expected.add("foo"); - assertResolved(new FieldPermissions(fieldPermissionDef(grantedFields, randomBoolean() ? null : new String[]{})), expected); + assertResolved(new FieldPermissions(fieldPermissionDef(grantedFields, randomBoolean() ? null : new String[] {})), expected); // same with null grantedFields = null; - assertResolved(new FieldPermissions(fieldPermissionDef(grantedFields, randomBoolean() ? null : new String[]{})), expected); + assertResolved(new FieldPermissions(fieldPermissionDef(grantedFields, randomBoolean() ? null : new String[] {})), expected); // check we remove only excluded fields - grantedFields = new String[]{"*"}; - deniedFields = new String[]{"xfield"}; + grantedFields = new String[] { "*" }; + deniedFields = new String[] { "xfield" }; expected = new HashSet<>(META_FIELDS); expected.add("foo"); assertResolved(new FieldPermissions(fieldPermissionDef(grantedFields, deniedFields)), expected, "xfield"); @@ -186,37 +192,35 @@ public void testFieldPermissionsWithFieldExceptions() throws Exception { assertResolved(new FieldPermissions(fieldPermissionDef(grantedFields, deniedFields)), expected, "xfield"); // some other checks - grantedFields = new String[]{"field*"}; - deniedFields = new String[]{"field1", "field2"}; + grantedFields = new String[] { "field*" }; + deniedFields = new String[] { "field1", "field2" }; expected = new HashSet<>(META_FIELDS); expected.add("field3"); assertResolved(new FieldPermissions(fieldPermissionDef(grantedFields, deniedFields)), expected, "field1", "field2"); - grantedFields = new String[]{"field1", "field2"}; - deniedFields = new String[]{"field2"}; + grantedFields = new String[] { "field1", "field2" }; + deniedFields = new String[] { "field2" }; expected = new HashSet<>(META_FIELDS); expected.add("field1"); assertResolved(new FieldPermissions(fieldPermissionDef(grantedFields, deniedFields)), expected, "field1", "field2"); - grantedFields = new String[]{"field*"}; - deniedFields = new String[]{"field2"}; + grantedFields = new String[] { "field*" }; + deniedFields = new String[] { "field2" }; expected = new HashSet<>(META_FIELDS); expected.add("field1"); assertResolved(new FieldPermissions(fieldPermissionDef(grantedFields, deniedFields)), expected, "field2"); - deniedFields = new String[]{"field*"}; - assertResolved(new FieldPermissions(fieldPermissionDef(grantedFields, deniedFields)), - META_FIELDS, "field1", "field2"); + deniedFields = new String[] { "field*" }; + assertResolved(new FieldPermissions(fieldPermissionDef(grantedFields, deniedFields)), META_FIELDS, "field1", "field2"); // empty array for allowed fields always means no field is allowed - grantedFields = new String[]{}; - deniedFields = new String[]{}; - assertResolved(new FieldPermissions(fieldPermissionDef(grantedFields, deniedFields)), - META_FIELDS, "field1", "field2"); + grantedFields = new String[] {}; + deniedFields = new String[] {}; + assertResolved(new FieldPermissions(fieldPermissionDef(grantedFields, deniedFields)), META_FIELDS, "field1", "field2"); // make sure all field can be explicitly allowed - grantedFields = new String[]{"*"}; - deniedFields = randomBoolean() ? null : new String[]{}; + grantedFields = new String[] { "*" }; + deniedFields = randomBoolean() ? null : new String[] {}; expected = new HashSet<>(META_FIELDS); expected.add("field1"); assertResolved(new FieldPermissions(fieldPermissionDef(grantedFields, deniedFields)), expected); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/permission/ApplicationPermissionTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/permission/ApplicationPermissionTests.java index d1c6e58009bcd..729f2dc2e148e 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/permission/ApplicationPermissionTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/permission/ApplicationPermissionTests.java @@ -6,8 +6,8 @@ */ package org.elasticsearch.xpack.core.security.authz.permission; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.util.set.Sets; +import org.elasticsearch.core.Tuple; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.security.authz.privilege.ApplicationPrivilege; import org.elasticsearch.xpack.core.security.authz.privilege.ApplicationPrivilegeDescriptor; @@ -133,13 +133,15 @@ public void testMergedPermissionChecking() { public void testInspectPermissionContents() { final ApplicationPrivilege app1ReadWrite = compositePrivilege("app1", app1Read, app1Write); - ApplicationPermission perm = new ApplicationPermission(Arrays.asList( - new Tuple<>(app1Read, Sets.newHashSet("obj/1", "obj/2")), - new Tuple<>(app1Write, Sets.newHashSet("obj/3", "obj/4")), - new Tuple<>(app1ReadWrite, Sets.newHashSet("obj/5")), - new Tuple<>(app1All, Sets.newHashSet("obj/6", "obj/7")), - new Tuple<>(app2Read, Sets.newHashSet("obj/1", "obj/8")) - )); + ApplicationPermission perm = new ApplicationPermission( + Arrays.asList( + new Tuple<>(app1Read, Sets.newHashSet("obj/1", "obj/2")), + new Tuple<>(app1Write, Sets.newHashSet("obj/3", "obj/4")), + new Tuple<>(app1ReadWrite, Sets.newHashSet("obj/5")), + new Tuple<>(app1All, Sets.newHashSet("obj/6", "obj/7")), + new Tuple<>(app2Read, Sets.newHashSet("obj/1", "obj/8")) + ) + ); assertThat(perm.getApplicationNames(), containsInAnyOrder("app1", "app2")); assertThat(perm.getPrivileges("app1"), containsInAnyOrder(app1Read, app1Write, app1ReadWrite, app1All)); assertThat(perm.getPrivileges("app2"), containsInAnyOrder(app2Read)); @@ -169,7 +171,7 @@ private ApplicationPermission buildPermission(ApplicationPrivilege privilege, St private ApplicationPermission buildPermission(Collection privileges, String... resources) { final Set resourceSet = Sets.newHashSet(resources); - final List>> privilegesAndResources = privileges.stream() + final List>> privilegesAndResources = privileges.stream() .map(p -> new Tuple<>(p, resourceSet)) .collect(Collectors.toList()); return new ApplicationPermission(privilegesAndResources); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/permission/ClusterPermissionTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/permission/ClusterPermissionTests.java index a6eea750958cf..e106cab1dd28d 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/permission/ClusterPermissionTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/permission/ClusterPermissionTests.java @@ -8,9 +8,9 @@ package org.elasticsearch.xpack.core.security.authz.permission; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.transport.TransportRequest; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.security.authc.Authentication; import org.elasticsearch.xpack.core.security.authz.privilege.ClusterPrivilege; import org.elasticsearch.xpack.core.security.authz.privilege.ClusterPrivilegeResolver; @@ -52,10 +52,10 @@ public void testClusterPermissionBuilder() { builder = ClusterPrivilegeResolver.MANAGE_SECURITY.buildPermission(builder); builder = ClusterPrivilegeResolver.MANAGE_ILM.buildPermission(builder); - final MockConfigurableClusterPrivilege mockConfigurableClusterPrivilege1 = - new MockConfigurableClusterPrivilege(r -> r == mockTransportRequest); - final MockConfigurableClusterPrivilege mockConfigurableClusterPrivilege2 = - new MockConfigurableClusterPrivilege(r -> false); + final MockConfigurableClusterPrivilege mockConfigurableClusterPrivilege1 = new MockConfigurableClusterPrivilege( + r -> r == mockTransportRequest + ); + final MockConfigurableClusterPrivilege mockConfigurableClusterPrivilege2 = new MockConfigurableClusterPrivilege(r -> false); builder = mockConfigurableClusterPrivilege1.buildPermission(builder); builder = mockConfigurableClusterPrivilege2.buildPermission(builder); final ClusterPermission clusterPermission = builder.build(); @@ -65,8 +65,15 @@ public void testClusterPermissionBuilder() { final Set privileges = clusterPermission.privileges(); assertNotNull(privileges); assertThat(privileges.size(), is(4)); - assertThat(privileges, containsInAnyOrder(ClusterPrivilegeResolver.MANAGE_SECURITY, ClusterPrivilegeResolver.MANAGE_ILM, - mockConfigurableClusterPrivilege1, mockConfigurableClusterPrivilege2)); + assertThat( + privileges, + containsInAnyOrder( + ClusterPrivilegeResolver.MANAGE_SECURITY, + ClusterPrivilegeResolver.MANAGE_ILM, + mockConfigurableClusterPrivilege1, + mockConfigurableClusterPrivilege2 + ) + ); } public void testClusterPermissionCheck() { @@ -74,19 +81,23 @@ public void testClusterPermissionCheck() { builder = ClusterPrivilegeResolver.MANAGE_SECURITY.buildPermission(builder); builder = ClusterPrivilegeResolver.MANAGE_ILM.buildPermission(builder); - final MockConfigurableClusterPrivilege mockConfigurableClusterPrivilege1 = - new MockConfigurableClusterPrivilege(r -> r == mockTransportRequest); - final MockConfigurableClusterPrivilege mockConfigurableClusterPrivilege2 = - new MockConfigurableClusterPrivilege(r -> false); + final MockConfigurableClusterPrivilege mockConfigurableClusterPrivilege1 = new MockConfigurableClusterPrivilege( + r -> r == mockTransportRequest + ); + final MockConfigurableClusterPrivilege mockConfigurableClusterPrivilege2 = new MockConfigurableClusterPrivilege(r -> false); builder = mockConfigurableClusterPrivilege1.buildPermission(builder); builder = mockConfigurableClusterPrivilege2.buildPermission(builder); final ClusterPermission clusterPermission = builder.build(); - assertThat(clusterPermission.check("cluster:admin/xpack/security/token/invalidate", mockTransportRequest, mockAuthentication), - is(true)); + assertThat( + clusterPermission.check("cluster:admin/xpack/security/token/invalidate", mockTransportRequest, mockAuthentication), + is(true) + ); assertThat(clusterPermission.check("cluster:admin/ilm/stop", mockTransportRequest, mockAuthentication), is(true)); - assertThat(clusterPermission.check("cluster:admin/xpack/security/privilege/get", mockTransportRequest, mockAuthentication), - is(true)); + assertThat( + clusterPermission.check("cluster:admin/xpack/security/privilege/get", mockTransportRequest, mockAuthentication), + is(true) + ); assertThat(clusterPermission.check("cluster:admin/snapshot/status", mockTransportRequest, mockAuthentication), is(false)); } @@ -96,8 +107,10 @@ public void testClusterPermissionCheckWithEmptyActionPatterns() { final ClusterPermission clusterPermission = builder.build(); assertThat(clusterPermission.check("cluster:admin/ilm/start", mockTransportRequest, mockAuthentication), is(false)); - assertThat(clusterPermission.check("cluster:admin/xpack/security/token/invalidate", mockTransportRequest, mockAuthentication), - is(false)); + assertThat( + clusterPermission.check("cluster:admin/xpack/security/token/invalidate", mockTransportRequest, mockAuthentication), + is(false) + ); } public void testClusterPermissionCheckWithExcludeOnlyActionPatterns() { @@ -106,8 +119,10 @@ public void testClusterPermissionCheckWithExcludeOnlyActionPatterns() { final ClusterPermission clusterPermission = builder.build(); assertThat(clusterPermission.check("cluster:admin/ilm/start", mockTransportRequest, mockAuthentication), is(false)); - assertThat(clusterPermission.check("cluster:admin/xpack/security/token/invalidate", mockTransportRequest, mockAuthentication), - is(false)); + assertThat( + clusterPermission.check("cluster:admin/xpack/security/token/invalidate", mockTransportRequest, mockAuthentication), + is(false) + ); } public void testClusterPermissionCheckWithActionPatterns() { @@ -116,8 +131,10 @@ public void testClusterPermissionCheckWithActionPatterns() { final ClusterPermission clusterPermission = builder.build(); assertThat(clusterPermission.check("cluster:admin/ilm/start", mockTransportRequest, mockAuthentication), is(false)); - assertThat(clusterPermission.check("cluster:admin/xpack/security/token/invalidate", mockTransportRequest, mockAuthentication), - is(true)); + assertThat( + clusterPermission.check("cluster:admin/xpack/security/token/invalidate", mockTransportRequest, mockAuthentication), + is(true) + ); } public void testClusterPermissionCheckWithActionPatternsAndNoExludePatterns() { @@ -126,8 +143,10 @@ public void testClusterPermissionCheckWithActionPatternsAndNoExludePatterns() { final ClusterPermission clusterPermission = builder.build(); assertThat(clusterPermission.check("cluster:admin/ilm/start", mockTransportRequest, mockAuthentication), is(true)); - assertThat(clusterPermission.check("cluster:admin/xpack/security/token/invalidate", mockTransportRequest, mockAuthentication), - is(true)); + assertThat( + clusterPermission.check("cluster:admin/xpack/security/token/invalidate", mockTransportRequest, mockAuthentication), + is(true) + ); } public void testNoneClusterPermissionIsImpliedByNone() { @@ -138,10 +157,10 @@ public void testNoneClusterPermissionIsImpliedByAny() { ClusterPermission.Builder builder = ClusterPermission.builder(); builder = ClusterPrivilegeResolver.MANAGE_SECURITY.buildPermission(builder); builder = ClusterPrivilegeResolver.MANAGE_ILM.buildPermission(builder); - final MockConfigurableClusterPrivilege mockConfigurableClusterPrivilege1 = - new MockConfigurableClusterPrivilege(r -> r == mockTransportRequest); - final MockConfigurableClusterPrivilege mockConfigurableClusterPrivilege2 = - new MockConfigurableClusterPrivilege(r -> false); + final MockConfigurableClusterPrivilege mockConfigurableClusterPrivilege1 = new MockConfigurableClusterPrivilege( + r -> r == mockTransportRequest + ); + final MockConfigurableClusterPrivilege mockConfigurableClusterPrivilege2 = new MockConfigurableClusterPrivilege(r -> false); builder = mockConfigurableClusterPrivilege1.buildPermission(builder); builder = mockConfigurableClusterPrivilege2.buildPermission(builder); final ClusterPermission clusterPermission = builder.build(); @@ -153,8 +172,9 @@ public void testClusterPermissionSubsetWithConfigurableClusterPrivilegeIsImplied ClusterPermission.Builder builder = ClusterPermission.builder(); builder = ClusterPrivilegeResolver.MANAGE_ML.buildPermission(builder); builder = ClusterPrivilegeResolver.MANAGE_ILM.buildPermission(builder); - final MockConfigurableClusterPrivilege mockConfigurableClusterPrivilege1 = - new MockConfigurableClusterPrivilege(r -> r == mockTransportRequest); + final MockConfigurableClusterPrivilege mockConfigurableClusterPrivilege1 = new MockConfigurableClusterPrivilege( + r -> r == mockTransportRequest + ); builder = mockConfigurableClusterPrivilege1.buildPermission(builder); final ClusterPermission clusterPermission = builder.build(); @@ -170,16 +190,16 @@ public void testClusterPermissionNonSubsetWithConfigurableClusterPrivilegeIsImpl ClusterPermission.Builder builder = ClusterPermission.builder(); builder = ClusterPrivilegeResolver.MANAGE_ML.buildPermission(builder); builder = ClusterPrivilegeResolver.MANAGE_ILM.buildPermission(builder); - final MockConfigurableClusterPrivilege mockConfigurableClusterPrivilege1 = - new MockConfigurableClusterPrivilege(r -> r == mockTransportRequest); + final MockConfigurableClusterPrivilege mockConfigurableClusterPrivilege1 = new MockConfigurableClusterPrivilege( + r -> r == mockTransportRequest + ); builder = mockConfigurableClusterPrivilege1.buildPermission(builder); final ClusterPermission clusterPermission = builder.build(); ClusterPermission.Builder builder1 = ClusterPermission.builder(); builder1 = ClusterPrivilegeResolver.MANAGE_ML.buildPermission(builder1); builder1 = mockConfigurableClusterPrivilege1.buildPermission(builder1); - final MockConfigurableClusterPrivilege mockConfigurableClusterPrivilege2 = - new MockConfigurableClusterPrivilege(r -> false); + final MockConfigurableClusterPrivilege mockConfigurableClusterPrivilege2 = new MockConfigurableClusterPrivilege(r -> false); builder1 = mockConfigurableClusterPrivilege2.buildPermission(builder1); final ClusterPermission otherClusterPermission = builder1.build(); @@ -216,8 +236,9 @@ public void testClusterPermissionIsImpliedBySameClusterPermission() { ClusterPermission.Builder builder = ClusterPermission.builder(); builder = ClusterPrivilegeResolver.MANAGE_ML.buildPermission(builder); builder = ClusterPrivilegeResolver.MANAGE_ILM.buildPermission(builder); - final MockConfigurableClusterPrivilege mockConfigurableClusterPrivilege1 = - new MockConfigurableClusterPrivilege(r -> r == mockTransportRequest); + final MockConfigurableClusterPrivilege mockConfigurableClusterPrivilege1 = new MockConfigurableClusterPrivilege( + r -> r == mockTransportRequest + ); builder = mockConfigurableClusterPrivilege1.buildPermission(builder); final ClusterPermission clusterPermission = builder.build(); @@ -226,8 +247,7 @@ public void testClusterPermissionIsImpliedBySameClusterPermission() { public void testClusterPermissionSubsetIsImpliedByAllClusterPermission() { final ClusterPermission allClusterPermission = ClusterPrivilegeResolver.ALL.buildPermission(ClusterPermission.builder()).build(); - ClusterPermission otherClusterPermission = - ClusterPrivilegeResolver.MANAGE_ILM.buildPermission(ClusterPermission.builder()).build(); + ClusterPermission otherClusterPermission = ClusterPrivilegeResolver.MANAGE_ILM.buildPermission(ClusterPermission.builder()).build(); assertThat(allClusterPermission.implies(otherClusterPermission), is(true)); } @@ -282,8 +302,7 @@ public String getWriteableName() { } @Override - public void writeTo(StreamOutput out) throws IOException { - } + public void writeTo(StreamOutput out) throws IOException {} @Override public boolean equals(Object o) { @@ -304,9 +323,7 @@ public int hashCode() { @Override public String toString() { - return "MockConfigurableClusterPrivilege{" + - "requestPredicate=" + requestPredicate + - '}'; + return "MockConfigurableClusterPrivilege{" + "requestPredicate=" + requestPredicate + '}'; } @Override diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/permission/DocumentPermissionsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/permission/DocumentPermissionsTests.java index 9601d2979389a..7278879413804 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/permission/DocumentPermissionsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/permission/DocumentPermissionsTests.java @@ -41,8 +41,7 @@ public void testHasDocumentPermissions() throws IOException { assertThat(documentPermissions1.filter(null, null, null, null), is(nullValue())); Set queries = Collections.singleton(new BytesArray("{\"match_all\" : {}}")); - final DocumentPermissions documentPermissions2 = DocumentPermissions - .filteredBy(queries); + final DocumentPermissions documentPermissions2 = DocumentPermissions.filteredBy(queries); assertThat(documentPermissions2, is(notNullValue())); assertThat(documentPermissions2.hasDocumentLevelPermissions(), is(true)); assertThat(documentPermissions2.getQueries(), equalTo(queries)); @@ -54,12 +53,14 @@ public void testHasDocumentPermissions() throws IOException { assertThat(documentPermissions3.getLimitedByQueries(), equalTo(queries)); final DocumentPermissions documentPermissions4 = DocumentPermissions.allowAll() - .limitDocumentPermissions(DocumentPermissions.allowAll()); + .limitDocumentPermissions(DocumentPermissions.allowAll()); assertThat(documentPermissions4, is(notNullValue())); assertThat(documentPermissions4.hasDocumentLevelPermissions(), is(false)); - AssertionError ae = expectThrows(AssertionError.class, - () -> DocumentPermissions.allowAll().limitDocumentPermissions(documentPermissions3)); + AssertionError ae = expectThrows( + AssertionError.class, + () -> DocumentPermissions.allowAll().limitDocumentPermissions(documentPermissions3) + ); assertThat(ae.getMessage(), containsString("nested scoping for document permissions is not permitted")); } @@ -67,46 +68,50 @@ public void testFailIfQueryUsesClient() throws Exception { Client client = mock(Client.class); when(client.settings()).thenReturn(Settings.EMPTY); final long nowInMillis = randomNonNegativeLong(); - QueryRewriteContext context = new QueryRewriteContext(xContentRegistry(), writableRegistry(), client, - () -> nowInMillis); + QueryRewriteContext context = new QueryRewriteContext(xContentRegistry(), writableRegistry(), client, () -> nowInMillis); QueryBuilder queryBuilder1 = new TermsQueryBuilder("field", "val1", "val2"); DocumentPermissions.failIfQueryUsesClient(queryBuilder1, context); QueryBuilder queryBuilder2 = new TermsQueryBuilder("field", new TermsLookup("_index", "_id", "_path")); - Exception e = expectThrows(IllegalStateException.class, - () -> DocumentPermissions.failIfQueryUsesClient(queryBuilder2, context)); + Exception e = expectThrows(IllegalStateException.class, () -> DocumentPermissions.failIfQueryUsesClient(queryBuilder2, context)); assertThat(e.getMessage(), equalTo("role queries are not allowed to execute additional requests")); } public void testWriteCacheKeyWillDistinguishBetweenQueriesAndLimitedByQueries() throws IOException { final BytesStreamOutput out0 = new BytesStreamOutput(); - final DocumentPermissions documentPermissions0 = - new DocumentPermissions( - Set.of(new BytesArray("{\"term\":{\"q1\":\"v1\"}}"), - new BytesArray("{\"term\":{\"q2\":\"v2\"}}"), new BytesArray("{\"term\":{\"q3\":\"v3\"}}")), - null); + final DocumentPermissions documentPermissions0 = new DocumentPermissions( + Set.of( + new BytesArray("{\"term\":{\"q1\":\"v1\"}}"), + new BytesArray("{\"term\":{\"q2\":\"v2\"}}"), + new BytesArray("{\"term\":{\"q3\":\"v3\"}}") + ), + null + ); documentPermissions0.buildCacheKey(out0, BytesReference::utf8ToString); final BytesStreamOutput out1 = new BytesStreamOutput(); - final DocumentPermissions documentPermissions1 = - new DocumentPermissions( - Set.of(new BytesArray("{\"term\":{\"q1\":\"v1\"}}"), new BytesArray("{\"term\":{\"q2\":\"v2\"}}")), - Set.of(new BytesArray("{\"term\":{\"q3\":\"v3\"}}"))); + final DocumentPermissions documentPermissions1 = new DocumentPermissions( + Set.of(new BytesArray("{\"term\":{\"q1\":\"v1\"}}"), new BytesArray("{\"term\":{\"q2\":\"v2\"}}")), + Set.of(new BytesArray("{\"term\":{\"q3\":\"v3\"}}")) + ); documentPermissions1.buildCacheKey(out1, BytesReference::utf8ToString); final BytesStreamOutput out2 = new BytesStreamOutput(); - final DocumentPermissions documentPermissions2 = - new DocumentPermissions( - Set.of(new BytesArray("{\"term\":{\"q1\":\"v1\"}}")), - Set.of(new BytesArray("{\"term\":{\"q2\":\"v2\"}}"), new BytesArray("{\"term\":{\"q3\":\"v3\"}}"))); + final DocumentPermissions documentPermissions2 = new DocumentPermissions( + Set.of(new BytesArray("{\"term\":{\"q1\":\"v1\"}}")), + Set.of(new BytesArray("{\"term\":{\"q2\":\"v2\"}}"), new BytesArray("{\"term\":{\"q3\":\"v3\"}}")) + ); documentPermissions2.buildCacheKey(out2, BytesReference::utf8ToString); final BytesStreamOutput out3 = new BytesStreamOutput(); - final DocumentPermissions documentPermissions3 = - new DocumentPermissions( - null, - Set.of(new BytesArray("{\"term\":{\"q1\":\"v1\"}}"), - new BytesArray("{\"term\":{\"q2\":\"v2\"}}"), new BytesArray("{\"term\":{\"q3\":\"v3\"}}"))); + final DocumentPermissions documentPermissions3 = new DocumentPermissions( + null, + Set.of( + new BytesArray("{\"term\":{\"q1\":\"v1\"}}"), + new BytesArray("{\"term\":{\"q2\":\"v2\"}}"), + new BytesArray("{\"term\":{\"q3\":\"v3\"}}") + ) + ); documentPermissions3.buildCacheKey(out3, BytesReference::utf8ToString); assertThat(Arrays.equals(BytesReference.toBytes(out0.bytes()), BytesReference.toBytes(out1.bytes())), is(false)); @@ -126,8 +131,9 @@ public void testHasStoredScript() throws IOException { if (hasStoredScript) { queries.add(new BytesArray("{\"template\":{\"id\":\"my-script\"}}")); } - final DocumentPermissions documentPermissions0 = - randomBoolean() ? new DocumentPermissions(queries, null) : new DocumentPermissions(null, queries); + final DocumentPermissions documentPermissions0 = randomBoolean() + ? new DocumentPermissions(queries, null) + : new DocumentPermissions(null, queries); assertThat(documentPermissions0.hasStoredScript(), is(hasStoredScript)); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/permission/FieldPermissionsCacheTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/permission/FieldPermissionsCacheTests.java index cb67dd690fda9..4a3ecab2b3d0c 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/permission/FieldPermissionsCacheTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/permission/FieldPermissionsCacheTests.java @@ -18,9 +18,11 @@ public class FieldPermissionsCacheTests extends ESTestCase { public void testFieldPermissionsCaching() { FieldPermissionsCache fieldPermissionsCache = new FieldPermissionsCache(Settings.EMPTY); - String[] allowed = new String[]{randomAlphaOfLength(5) + "*", randomAlphaOfLength(5) + "*", randomAlphaOfLength(5) + "*"}; - String[] denied = new String[]{allowed[0] + randomAlphaOfLength(5), allowed[1] + randomAlphaOfLength(5), - allowed[2] + randomAlphaOfLength(5)}; + String[] allowed = new String[] { randomAlphaOfLength(5) + "*", randomAlphaOfLength(5) + "*", randomAlphaOfLength(5) + "*" }; + String[] denied = new String[] { + allowed[0] + randomAlphaOfLength(5), + allowed[1] + randomAlphaOfLength(5), + allowed[2] + randomAlphaOfLength(5) }; FieldPermissions fieldPermissions = fieldPermissionsCache.getFieldPermissions(allowed, denied); assertNotNull(fieldPermissions); final String[] allowed2 = randomBoolean() ? allowed : Arrays.copyOf(allowed, allowed.length); @@ -32,43 +34,48 @@ public void testMergeFieldPermissions() { FieldPermissionsCache fieldPermissionsCache = new FieldPermissionsCache(Settings.EMPTY); String allowedPrefix1 = randomAlphaOfLength(5); String allowedPrefix2 = randomAlphaOfLength(5); - String[] allowed1 = new String[]{allowedPrefix1 + "*"}; - String[] allowed2 = new String[]{allowedPrefix2 + "*"}; - String[] denied1 = new String[]{allowedPrefix1 + "a"}; - String[] denied2 = new String[]{allowedPrefix2 + "a"}; - FieldPermissions fieldPermissions1 = randomBoolean() ? fieldPermissionsCache.getFieldPermissions(allowed1, denied1) : - new FieldPermissions(fieldPermissionDef(allowed1, denied1)); - FieldPermissions fieldPermissions2 = randomBoolean() ? fieldPermissionsCache.getFieldPermissions(allowed2, denied2) : - new FieldPermissions(fieldPermissionDef(allowed2, denied2)); - FieldPermissions mergedFieldPermissions = - fieldPermissionsCache.getFieldPermissions(Arrays.asList(fieldPermissions1, fieldPermissions2)); + String[] allowed1 = new String[] { allowedPrefix1 + "*" }; + String[] allowed2 = new String[] { allowedPrefix2 + "*" }; + String[] denied1 = new String[] { allowedPrefix1 + "a" }; + String[] denied2 = new String[] { allowedPrefix2 + "a" }; + FieldPermissions fieldPermissions1 = randomBoolean() + ? fieldPermissionsCache.getFieldPermissions(allowed1, denied1) + : new FieldPermissions(fieldPermissionDef(allowed1, denied1)); + FieldPermissions fieldPermissions2 = randomBoolean() + ? fieldPermissionsCache.getFieldPermissions(allowed2, denied2) + : new FieldPermissions(fieldPermissionDef(allowed2, denied2)); + FieldPermissions mergedFieldPermissions = fieldPermissionsCache.getFieldPermissions( + Arrays.asList(fieldPermissions1, fieldPermissions2) + ); assertTrue(mergedFieldPermissions.grantsAccessTo(allowedPrefix1 + "b")); assertTrue(mergedFieldPermissions.grantsAccessTo(allowedPrefix2 + "b")); assertFalse(mergedFieldPermissions.grantsAccessTo(denied1[0])); assertFalse(mergedFieldPermissions.grantsAccessTo(denied2[0])); - allowed1 = new String[]{randomAlphaOfLength(5) + "*", randomAlphaOfLength(5) + "*"}; + allowed1 = new String[] { randomAlphaOfLength(5) + "*", randomAlphaOfLength(5) + "*" }; allowed2 = null; - denied1 = new String[]{allowed1[0] + "a", allowed1[1] + "a"}; + denied1 = new String[] { allowed1[0] + "a", allowed1[1] + "a" }; denied2 = null; - fieldPermissions1 = randomBoolean() ? fieldPermissionsCache.getFieldPermissions(allowed1, denied1) : - new FieldPermissions(fieldPermissionDef(allowed1, denied1)); - fieldPermissions2 = randomBoolean() ? fieldPermissionsCache.getFieldPermissions(allowed2, denied2) : - new FieldPermissions(fieldPermissionDef(allowed2, denied2)); - mergedFieldPermissions = - fieldPermissionsCache.getFieldPermissions(Arrays.asList(fieldPermissions1, fieldPermissions2)); + fieldPermissions1 = randomBoolean() + ? fieldPermissionsCache.getFieldPermissions(allowed1, denied1) + : new FieldPermissions(fieldPermissionDef(allowed1, denied1)); + fieldPermissions2 = randomBoolean() + ? fieldPermissionsCache.getFieldPermissions(allowed2, denied2) + : new FieldPermissions(fieldPermissionDef(allowed2, denied2)); + mergedFieldPermissions = fieldPermissionsCache.getFieldPermissions(Arrays.asList(fieldPermissions1, fieldPermissions2)); assertFalse(mergedFieldPermissions.hasFieldLevelSecurity()); - allowed1 = new String[]{}; - allowed2 = new String[]{randomAlphaOfLength(5) + "*", randomAlphaOfLength(5) + "*"}; - denied1 = new String[]{}; - denied2 = new String[]{allowed2[0] + "a", allowed2[1] + "a"}; - fieldPermissions1 = randomBoolean() ? fieldPermissionsCache.getFieldPermissions(allowed1, denied1) : - new FieldPermissions(fieldPermissionDef(allowed1, denied1)); - fieldPermissions2 = randomBoolean() ? fieldPermissionsCache.getFieldPermissions(allowed2, denied2) : - new FieldPermissions(fieldPermissionDef(allowed2, denied2)); - mergedFieldPermissions = - fieldPermissionsCache.getFieldPermissions(Arrays.asList(fieldPermissions1, fieldPermissions2)); + allowed1 = new String[] {}; + allowed2 = new String[] { randomAlphaOfLength(5) + "*", randomAlphaOfLength(5) + "*" }; + denied1 = new String[] {}; + denied2 = new String[] { allowed2[0] + "a", allowed2[1] + "a" }; + fieldPermissions1 = randomBoolean() + ? fieldPermissionsCache.getFieldPermissions(allowed1, denied1) + : new FieldPermissions(fieldPermissionDef(allowed1, denied1)); + fieldPermissions2 = randomBoolean() + ? fieldPermissionsCache.getFieldPermissions(allowed2, denied2) + : new FieldPermissions(fieldPermissionDef(allowed2, denied2)); + mergedFieldPermissions = fieldPermissionsCache.getFieldPermissions(Arrays.asList(fieldPermissions1, fieldPermissions2)); for (String field : allowed2) { assertTrue(mergedFieldPermissions.grantsAccessTo(field)); } @@ -76,16 +83,17 @@ public void testMergeFieldPermissions() { assertFalse(mergedFieldPermissions.grantsAccessTo(field)); } - allowed1 = randomBoolean() ? null : new String[]{"*"}; - allowed2 = randomBoolean() ? null : new String[]{"*"}; - denied1 = new String[]{"a"}; - denied2 = new String[]{"b"}; - fieldPermissions1 = randomBoolean() ? fieldPermissionsCache.getFieldPermissions(allowed1, denied1) : - new FieldPermissions(fieldPermissionDef(allowed1, denied1)); - fieldPermissions2 = randomBoolean() ? fieldPermissionsCache.getFieldPermissions(allowed2, denied2) : - new FieldPermissions(fieldPermissionDef(allowed2, denied2)); - mergedFieldPermissions = - fieldPermissionsCache.getFieldPermissions(Arrays.asList(fieldPermissions1, fieldPermissions2)); + allowed1 = randomBoolean() ? null : new String[] { "*" }; + allowed2 = randomBoolean() ? null : new String[] { "*" }; + denied1 = new String[] { "a" }; + denied2 = new String[] { "b" }; + fieldPermissions1 = randomBoolean() + ? fieldPermissionsCache.getFieldPermissions(allowed1, denied1) + : new FieldPermissions(fieldPermissionDef(allowed1, denied1)); + fieldPermissions2 = randomBoolean() + ? fieldPermissionsCache.getFieldPermissions(allowed2, denied2) + : new FieldPermissions(fieldPermissionDef(allowed2, denied2)); + mergedFieldPermissions = fieldPermissionsCache.getFieldPermissions(Arrays.asList(fieldPermissions1, fieldPermissions2)); assertTrue(mergedFieldPermissions.grantsAccessTo("a")); assertTrue(mergedFieldPermissions.grantsAccessTo("b")); @@ -93,12 +101,13 @@ public void testMergeFieldPermissions() { allowed2 = new String[] { "b*" }; denied1 = new String[] { "aa*" }; denied2 = null; - fieldPermissions1 = randomBoolean() ? fieldPermissionsCache.getFieldPermissions(allowed1, denied1) : - new FieldPermissions(fieldPermissionDef(allowed1, denied1)); - fieldPermissions2 = randomBoolean() ? fieldPermissionsCache.getFieldPermissions(allowed2, denied2) : - new FieldPermissions(fieldPermissionDef(allowed2, denied2)); - mergedFieldPermissions = - fieldPermissionsCache.getFieldPermissions(Arrays.asList(fieldPermissions1, fieldPermissions2)); + fieldPermissions1 = randomBoolean() + ? fieldPermissionsCache.getFieldPermissions(allowed1, denied1) + : new FieldPermissions(fieldPermissionDef(allowed1, denied1)); + fieldPermissions2 = randomBoolean() + ? fieldPermissionsCache.getFieldPermissions(allowed2, denied2) + : new FieldPermissions(fieldPermissionDef(allowed2, denied2)); + mergedFieldPermissions = fieldPermissionsCache.getFieldPermissions(Arrays.asList(fieldPermissions1, fieldPermissions2)); assertTrue(mergedFieldPermissions.grantsAccessTo("a")); assertTrue(mergedFieldPermissions.grantsAccessTo("b")); assertFalse(mergedFieldPermissions.grantsAccessTo("aa")); @@ -108,9 +117,9 @@ public void testMergeFieldPermissions() { public void testNonFlsAndFlsMerging() { List permissionsList = new ArrayList<>(); - permissionsList.add(new FieldPermissions(fieldPermissionDef(new String[] {"field1"}, null))); - permissionsList.add(new FieldPermissions(fieldPermissionDef(new String[] {"field2", "query*"}, null))); - permissionsList.add(new FieldPermissions(fieldPermissionDef(new String[] {"field1", "field2"}, null))); + permissionsList.add(new FieldPermissions(fieldPermissionDef(new String[] { "field1" }, null))); + permissionsList.add(new FieldPermissions(fieldPermissionDef(new String[] { "field2", "query*" }, null))); + permissionsList.add(new FieldPermissions(fieldPermissionDef(new String[] { "field1", "field2" }, null))); permissionsList.add(new FieldPermissions(fieldPermissionDef(new String[] {}, null))); permissionsList.add(new FieldPermissions(fieldPermissionDef(null, null))); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/permission/FieldPermissionsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/permission/FieldPermissionsTests.java index 80d0e46ace52e..70709525f9951 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/permission/FieldPermissionsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/permission/FieldPermissionsTests.java @@ -29,9 +29,11 @@ public void testFieldPermissionsIntersection() { final FieldPermissions fieldPermissions = FieldPermissions.DEFAULT; final FieldPermissions fieldPermissions1 = new FieldPermissions( - fieldPermissionDef(new String[] { "f1", "f2", "f3*" }, new String[] { "f3" })); + fieldPermissionDef(new String[] { "f1", "f2", "f3*" }, new String[] { "f3" }) + ); final FieldPermissions fieldPermissions2 = new FieldPermissions( - fieldPermissionDef(new String[] { "f1", "f3*", "f4" }, new String[] { "f3" })); + fieldPermissionDef(new String[] { "f1", "f3*", "f4" }, new String[] { "f3" }) + ); { FieldPermissions result = fieldPermissions.limitFieldPermissions(randomFrom(new FieldPermissions(), null)); @@ -88,58 +90,81 @@ public void testMustHaveNonNullFieldPermissionsDefinition() { final FieldPermissions fieldPermissions03 = randomFrom( FieldPermissions.DEFAULT, - new FieldPermissions(fieldPermissionDef(new String[] { "f1", "f2", "f3*" }, new String[] { "f3" }))); + new FieldPermissions(fieldPermissionDef(new String[] { "f1", "f2", "f3*" }, new String[] { "f3" })) + ); assertThat(fieldPermissions03.limitFieldPermissions(null).getFieldPermissionsDefinition(), notNullValue()); assertThat(fieldPermissions03.limitFieldPermissions(FieldPermissions.DEFAULT).getFieldPermissionsDefinition(), notNullValue()); - assertThat(fieldPermissions03.limitFieldPermissions( - new FieldPermissions(fieldPermissionDef(new String[] { "f1", "f3*", "f4" }, new String[] { "f3" })) - ).getFieldPermissionsDefinition(), notNullValue()); + assertThat( + fieldPermissions03.limitFieldPermissions( + new FieldPermissions(fieldPermissionDef(new String[] { "f1", "f3*", "f4" }, new String[] { "f3" })) + ).getFieldPermissionsDefinition(), + notNullValue() + ); } public void testWriteCacheKeyWillDistinguishBetweenDefinitionAndLimitedByDefinition() throws IOException { // The overall same grant/except sets but are come from either: - // 1. Just the definition - // 2. Just the limited-by definition - // 3. both + // 1. Just the definition + // 2. Just the limited-by definition + // 3. both // The cache key should differentiate between them // Just definition final BytesStreamOutput out0 = new BytesStreamOutput(); final FieldPermissions fieldPermissions0 = new FieldPermissions( - new FieldPermissionsDefinition(Set.of( - new FieldPermissionsDefinition.FieldGrantExcludeGroup(new String[] { "x*" }, new String[] { "x2" }), - new FieldPermissionsDefinition.FieldGrantExcludeGroup(new String[] { "y*" }, new String[] { "y2" }), - new FieldPermissionsDefinition.FieldGrantExcludeGroup(new String[] { "z*" }, new String[] { "z2" })))); + new FieldPermissionsDefinition( + Set.of( + new FieldPermissionsDefinition.FieldGrantExcludeGroup(new String[] { "x*" }, new String[] { "x2" }), + new FieldPermissionsDefinition.FieldGrantExcludeGroup(new String[] { "y*" }, new String[] { "y2" }), + new FieldPermissionsDefinition.FieldGrantExcludeGroup(new String[] { "z*" }, new String[] { "z2" }) + ) + ) + ); fieldPermissions0.buildCacheKey(out0, BytesReference::utf8ToString); // Mixed definition final BytesStreamOutput out1 = new BytesStreamOutput(); final FieldPermissions fieldPermissions1 = new FieldPermissions( - new FieldPermissionsDefinition(Set.of( - new FieldPermissionsDefinition.FieldGrantExcludeGroup(new String[] { "x*" }, new String[] { "x2" }), - new FieldPermissionsDefinition.FieldGrantExcludeGroup(new String[] { "y*" }, new String[] { "y2" })))) - .limitFieldPermissions(new FieldPermissions(fieldPermissionDef(new String[] { "z*" }, new String[] { "z2" }))); + new FieldPermissionsDefinition( + Set.of( + new FieldPermissionsDefinition.FieldGrantExcludeGroup(new String[] { "x*" }, new String[] { "x2" }), + new FieldPermissionsDefinition.FieldGrantExcludeGroup(new String[] { "y*" }, new String[] { "y2" }) + ) + ) + ).limitFieldPermissions(new FieldPermissions(fieldPermissionDef(new String[] { "z*" }, new String[] { "z2" }))); fieldPermissions1.buildCacheKey(out1, BytesReference::utf8ToString); // Another mixed definition final BytesStreamOutput out2 = new BytesStreamOutput(); final FieldPermissions fieldPermissions2 = new FieldPermissions( - new FieldPermissionsDefinition(Set.of( - new FieldPermissionsDefinition.FieldGrantExcludeGroup(new String[] { "x*" }, new String[] { "x2" })))) - .limitFieldPermissions(new FieldPermissions(new FieldPermissionsDefinition(Set.of( - new FieldPermissionsDefinition.FieldGrantExcludeGroup(new String[] { "y*" }, new String[] { "y2" }), - new FieldPermissionsDefinition.FieldGrantExcludeGroup(new String[] { "z*" }, new String[] { "z2" })) - ))); + new FieldPermissionsDefinition( + Set.of(new FieldPermissionsDefinition.FieldGrantExcludeGroup(new String[] { "x*" }, new String[] { "x2" })) + ) + ).limitFieldPermissions( + new FieldPermissions( + new FieldPermissionsDefinition( + Set.of( + new FieldPermissionsDefinition.FieldGrantExcludeGroup(new String[] { "y*" }, new String[] { "y2" }), + new FieldPermissionsDefinition.FieldGrantExcludeGroup(new String[] { "z*" }, new String[] { "z2" }) + ) + ) + ) + ); fieldPermissions2.buildCacheKey(out2, BytesReference::utf8ToString); // Just limited by final BytesStreamOutput out3 = new BytesStreamOutput(); final FieldPermissions fieldPermissions3 = new FieldPermissions().limitFieldPermissions( new FieldPermissions( - new FieldPermissionsDefinition(Set.of( - new FieldPermissionsDefinition.FieldGrantExcludeGroup(new String[] { "x*" }, new String[] { "x2" }), - new FieldPermissionsDefinition.FieldGrantExcludeGroup(new String[] { "y*" }, new String[] { "y2" }), - new FieldPermissionsDefinition.FieldGrantExcludeGroup(new String[] { "z*" }, new String[] { "z2" }))))); + new FieldPermissionsDefinition( + Set.of( + new FieldPermissionsDefinition.FieldGrantExcludeGroup(new String[] { "x*" }, new String[] { "x2" }), + new FieldPermissionsDefinition.FieldGrantExcludeGroup(new String[] { "y*" }, new String[] { "y2" }), + new FieldPermissionsDefinition.FieldGrantExcludeGroup(new String[] { "z*" }, new String[] { "z2" }) + ) + ) + ) + ); fieldPermissions3.buildCacheKey(out3, BytesReference::utf8ToString); assertThat(Arrays.equals(BytesReference.toBytes(out0.bytes()), BytesReference.toBytes(out1.bytes())), is(false)); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/permission/LimitedRoleTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/permission/LimitedRoleTests.java index 0f6d107b85fbd..0a97fe68d12d8 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/permission/LimitedRoleTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/permission/LimitedRoleTests.java @@ -64,27 +64,46 @@ public void testRoleConstructorWithLimitedRole() { } public void testAuthorize() { - IndexMetadata.Builder imbBuilder = IndexMetadata - .builder("_index").settings(Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1).put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT)) - .putAlias(AliasMetadata.builder("_alias")); - IndexMetadata.Builder imbBuilder1 = IndexMetadata - .builder("_index1").settings(Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1).put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT)) - .putAlias(AliasMetadata.builder("_alias1")); + IndexMetadata.Builder imbBuilder = IndexMetadata.builder("_index") + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + ) + .putAlias(AliasMetadata.builder("_alias")); + IndexMetadata.Builder imbBuilder1 = IndexMetadata.builder("_index1") + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + ) + .putAlias(AliasMetadata.builder("_alias1")); Metadata md = Metadata.builder().put(imbBuilder).put(imbBuilder1).build(); FieldPermissionsCache fieldPermissionsCache = new FieldPermissionsCache(Settings.EMPTY); - Role fromRole = Role.builder(Automatons.EMPTY, "a-role").cluster(Collections.singleton("manage_security"), Collections.emptyList()) - .add(IndexPrivilege.ALL, "_index").add(IndexPrivilege.CREATE_INDEX, "_index1").build(); + Role fromRole = Role.builder(Automatons.EMPTY, "a-role") + .cluster(Collections.singleton("manage_security"), Collections.emptyList()) + .add(IndexPrivilege.ALL, "_index") + .add(IndexPrivilege.CREATE_INDEX, "_index1") + .build(); - IndicesAccessControl iac = fromRole.authorize(SearchAction.NAME, Sets.newHashSet("_index", "_alias1"), md.getIndicesLookup(), - fieldPermissionsCache); + IndicesAccessControl iac = fromRole.authorize( + SearchAction.NAME, + Sets.newHashSet("_index", "_alias1"), + md.getIndicesLookup(), + fieldPermissionsCache + ); assertThat(iac.getIndexPermissions("_index"), is(notNullValue())); assertThat(iac.getIndexPermissions("_index").isGranted(), is(true)); assertThat(iac.getIndexPermissions("_index1"), is(notNullValue())); assertThat(iac.getIndexPermissions("_index1").isGranted(), is(false)); - iac = fromRole.authorize(CreateIndexAction.NAME, Sets.newHashSet("_index", "_index1"), md.getIndicesLookup(), - fieldPermissionsCache); + iac = fromRole.authorize( + CreateIndexAction.NAME, + Sets.newHashSet("_index", "_index1"), + md.getIndicesLookup(), + fieldPermissionsCache + ); assertThat(iac.getIndexPermissions("_index"), is(notNullValue())); assertThat(iac.getIndexPermissions("_index").isGranted(), is(true)); assertThat(iac.getIndexPermissions("_index1"), is(notNullValue())); @@ -92,42 +111,63 @@ public void testAuthorize() { { Role limitedByRole = Role.builder(Automatons.EMPTY, "limited-role") - .cluster(Collections.singleton("all"), Collections.emptyList()).add(IndexPrivilege.READ, "_index") - .add(IndexPrivilege.NONE, "_index1").build(); - iac = limitedByRole.authorize(SearchAction.NAME, Sets.newHashSet("_index", "_alias1"), md.getIndicesLookup(), - fieldPermissionsCache); + .cluster(Collections.singleton("all"), Collections.emptyList()) + .add(IndexPrivilege.READ, "_index") + .add(IndexPrivilege.NONE, "_index1") + .build(); + iac = limitedByRole.authorize( + SearchAction.NAME, + Sets.newHashSet("_index", "_alias1"), + md.getIndicesLookup(), + fieldPermissionsCache + ); assertThat(iac.getIndexPermissions("_index"), is(notNullValue())); assertThat(iac.getIndexPermissions("_index").isGranted(), is(true)); assertThat(iac.getIndexPermissions("_index1"), is(notNullValue())); assertThat(iac.getIndexPermissions("_index1").isGranted(), is(false)); - iac = limitedByRole.authorize(DeleteIndexAction.NAME, Sets.newHashSet("_index", "_alias1"), md.getIndicesLookup(), - fieldPermissionsCache); + iac = limitedByRole.authorize( + DeleteIndexAction.NAME, + Sets.newHashSet("_index", "_alias1"), + md.getIndicesLookup(), + fieldPermissionsCache + ); assertThat(iac.getIndexPermissions("_index"), is(notNullValue())); assertThat(iac.getIndexPermissions("_index").isGranted(), is(false)); assertThat(iac.getIndexPermissions("_index1"), is(notNullValue())); assertThat(iac.getIndexPermissions("_index1").isGranted(), is(false)); - iac = limitedByRole.authorize(CreateIndexAction.NAME, Sets.newHashSet("_index", "_alias1"), md.getIndicesLookup(), - fieldPermissionsCache); + iac = limitedByRole.authorize( + CreateIndexAction.NAME, + Sets.newHashSet("_index", "_alias1"), + md.getIndicesLookup(), + fieldPermissionsCache + ); assertThat(iac.getIndexPermissions("_index"), is(notNullValue())); assertThat(iac.getIndexPermissions("_index").isGranted(), is(false)); assertThat(iac.getIndexPermissions("_index1"), is(notNullValue())); assertThat(iac.getIndexPermissions("_index1").isGranted(), is(false)); Role role = LimitedRole.createLimitedRole(fromRole, limitedByRole); - iac = role.authorize(SearchAction.NAME, Sets.newHashSet("_index", "_alias1"), md.getIndicesLookup(), - fieldPermissionsCache); + iac = role.authorize(SearchAction.NAME, Sets.newHashSet("_index", "_alias1"), md.getIndicesLookup(), fieldPermissionsCache); assertThat(iac.getIndexPermissions("_index"), is(notNullValue())); assertThat(iac.getIndexPermissions("_index").isGranted(), is(true)); assertThat(iac.getIndexPermissions("_index1"), is(notNullValue())); assertThat(iac.getIndexPermissions("_index1").isGranted(), is(false)); - iac = role.authorize(DeleteIndexAction.NAME, Sets.newHashSet("_index", "_alias1"), md.getIndicesLookup(), - fieldPermissionsCache); + iac = role.authorize( + DeleteIndexAction.NAME, + Sets.newHashSet("_index", "_alias1"), + md.getIndicesLookup(), + fieldPermissionsCache + ); assertThat(iac.getIndexPermissions("_index"), is(notNullValue())); assertThat(iac.getIndexPermissions("_index").isGranted(), is(false)); assertThat(iac.getIndexPermissions("_index1"), is(notNullValue())); assertThat(iac.getIndexPermissions("_index1").isGranted(), is(false)); - iac = role.authorize(CreateIndexAction.NAME, Sets.newHashSet("_index", "_index1"), md.getIndicesLookup(), - fieldPermissionsCache); + iac = role.authorize( + CreateIndexAction.NAME, + Sets.newHashSet("_index", "_index1"), + md.getIndicesLookup(), + fieldPermissionsCache + ); assertThat(iac.getIndexPermissions("_index"), is(notNullValue())); assertThat(iac.getIndexPermissions("_index").isGranted(), is(false)); assertThat(iac.getIndexPermissions("_index1"), is(notNullValue())); @@ -136,15 +176,19 @@ public void testAuthorize() { } public void testCheckClusterAction() { - Role fromRole = Role.builder(Automatons.EMPTY, "a-role").cluster(Collections.singleton("manage_security"), Collections.emptyList()) + Role fromRole = Role.builder(Automatons.EMPTY, "a-role") + .cluster(Collections.singleton("manage_security"), Collections.emptyList()) .build(); Authentication authentication = mock(Authentication.class); assertThat(fromRole.checkClusterAction("cluster:admin/xpack/security/x", mock(TransportRequest.class), authentication), is(true)); { Role limitedByRole = Role.builder(Automatons.EMPTY, "limited-role") - .cluster(Collections.singleton("all"), Collections.emptyList()).build(); - assertThat(limitedByRole.checkClusterAction("cluster:admin/xpack/security/x", mock(TransportRequest.class), authentication), - is(true)); + .cluster(Collections.singleton("all"), Collections.emptyList()) + .build(); + assertThat( + limitedByRole.checkClusterAction("cluster:admin/xpack/security/x", mock(TransportRequest.class), authentication), + is(true) + ); assertThat(limitedByRole.checkClusterAction("cluster:other-action", mock(TransportRequest.class), authentication), is(true)); Role role = LimitedRole.createLimitedRole(fromRole, limitedByRole); assertThat(role.checkClusterAction("cluster:admin/xpack/security/x", mock(TransportRequest.class), authentication), is(true)); @@ -152,7 +196,8 @@ public void testCheckClusterAction() { } { Role limitedByRole = Role.builder(Automatons.EMPTY, "limited-role") - .cluster(Collections.singleton("monitor"), Collections.emptyList()).build(); + .cluster(Collections.singleton("monitor"), Collections.emptyList()) + .build(); assertThat(limitedByRole.checkClusterAction("cluster:monitor/me", mock(TransportRequest.class), authentication), is(true)); Role role = LimitedRole.createLimitedRole(fromRole, limitedByRole); assertThat(role.checkClusterAction("cluster:monitor/me", mock(TransportRequest.class), authentication), is(false)); @@ -210,18 +255,16 @@ public void testAllowedIndicesMatcher() { public void testAllowedActionsMatcher() { Role fromRole = Role.builder(Automatons.EMPTY, "fromRole") - .add(IndexPrivilege.WRITE, "ind*") - .add(IndexPrivilege.READ, "ind*") - .add(IndexPrivilege.READ, "other*") - .build(); + .add(IndexPrivilege.WRITE, "ind*") + .add(IndexPrivilege.READ, "ind*") + .add(IndexPrivilege.READ, "other*") + .build(); Automaton fromRoleAutomaton = fromRole.allowedActionsMatcher("index1"); Predicate fromRolePredicate = Automatons.predicate(fromRoleAutomaton); assertThat(fromRolePredicate.test(SearchAction.NAME), is(true)); assertThat(fromRolePredicate.test(BulkAction.NAME), is(true)); - Role limitedByRole = Role.builder(Automatons.EMPTY, "limitedRole") - .add(IndexPrivilege.READ, "index1", "index2") - .build(); + Role limitedByRole = Role.builder(Automatons.EMPTY, "limitedRole").add(IndexPrivilege.READ, "index1", "index2").build(); Automaton limitedByRoleAutomaton = limitedByRole.allowedActionsMatcher("index1"); Predicate limitedByRolePredicated = Automatons.predicate(limitedByRoleAutomaton); assertThat(limitedByRolePredicated.test(SearchAction.NAME), is(true)); @@ -245,14 +288,16 @@ public void testAllowedActionsMatcher() { } public void testCheckClusterPrivilege() { - Role fromRole = Role.builder(Automatons.EMPTY, "a-role").cluster(Collections.singleton("manage_security"), Collections.emptyList()) - .build(); + Role fromRole = Role.builder(Automatons.EMPTY, "a-role") + .cluster(Collections.singleton("manage_security"), Collections.emptyList()) + .build(); assertThat(fromRole.grants(ClusterPrivilegeResolver.ALL), is(false)); assertThat(fromRole.grants(ClusterPrivilegeResolver.MANAGE_SECURITY), is(true)); { Role limitedByRole = Role.builder(Automatons.EMPTY, "scoped-role") - .cluster(Collections.singleton("all"), Collections.emptyList()).build(); + .cluster(Collections.singleton("all"), Collections.emptyList()) + .build(); assertThat(limitedByRole.grants(ClusterPrivilegeResolver.ALL), is(true)); assertThat(limitedByRole.grants(ClusterPrivilegeResolver.MANAGE_SECURITY), is(true)); Role role = LimitedRole.createLimitedRole(fromRole, limitedByRole); @@ -261,7 +306,8 @@ public void testCheckClusterPrivilege() { } { Role limitedByRole = Role.builder(Automatons.EMPTY, "scoped-role") - .cluster(Collections.singleton("monitor"), Collections.emptyList()).build(); + .cluster(Collections.singleton("monitor"), Collections.emptyList()) + .build(); assertThat(limitedByRole.grants(ClusterPrivilegeResolver.ALL), is(false)); assertThat(limitedByRole.grants(ClusterPrivilegeResolver.MONITOR), is(true)); Role role = LimitedRole.createLimitedRole(fromRole, limitedByRole); @@ -273,77 +319,123 @@ public void testCheckClusterPrivilege() { public void testGetPrivilegesForIndexPatterns() { Role fromRole = Role.builder(Automatons.EMPTY, "a-role").add(IndexPrivilege.READ, "ind-1*").build(); - ResourcePrivilegesMap resourcePrivileges = fromRole.checkIndicesPrivileges(Collections.singleton("ind-1-1-*"), true, - Sets.newHashSet("read", "write")); - ResourcePrivilegesMap expectedAppPrivsByResource = new ResourcePrivilegesMap(false, Collections.singletonMap("ind-1-1-*", - ResourcePrivileges.builder("ind-1-1-*").addPrivilege("read", true).addPrivilege("write", false).build())); + ResourcePrivilegesMap resourcePrivileges = fromRole.checkIndicesPrivileges( + Collections.singleton("ind-1-1-*"), + true, + Sets.newHashSet("read", "write") + ); + ResourcePrivilegesMap expectedAppPrivsByResource = new ResourcePrivilegesMap( + false, + Collections.singletonMap( + "ind-1-1-*", + ResourcePrivileges.builder("ind-1-1-*").addPrivilege("read", true).addPrivilege("write", false).build() + ) + ); verifyResourcesPrivileges(resourcePrivileges, expectedAppPrivsByResource); resourcePrivileges = fromRole.checkIndicesPrivileges(Collections.singleton("ind-*"), true, Sets.newHashSet("read", "write")); - expectedAppPrivsByResource = new ResourcePrivilegesMap(false, Collections.singletonMap("ind-*", - ResourcePrivileges.builder("ind-*").addPrivilege("read", false).addPrivilege("write", false).build())); + expectedAppPrivsByResource = new ResourcePrivilegesMap( + false, + Collections.singletonMap( + "ind-*", + ResourcePrivileges.builder("ind-*").addPrivilege("read", false).addPrivilege("write", false).build() + ) + ); verifyResourcesPrivileges(resourcePrivileges, expectedAppPrivsByResource); { Role limitedByRole = Role.builder(Automatons.EMPTY, "limited-role").add(IndexPrivilege.READ, "ind-1", "ind-2").build(); resourcePrivileges = limitedByRole.checkIndicesPrivileges(Collections.singleton("ind-1"), true, Collections.singleton("read")); - expectedAppPrivsByResource = new ResourcePrivilegesMap(true, - Collections.singletonMap("ind-1", ResourcePrivileges.builder("ind-1").addPrivilege("read", true).build())); + expectedAppPrivsByResource = new ResourcePrivilegesMap( + true, + Collections.singletonMap("ind-1", ResourcePrivileges.builder("ind-1").addPrivilege("read", true).build()) + ); verifyResourcesPrivileges(resourcePrivileges, expectedAppPrivsByResource); - resourcePrivileges = limitedByRole.checkIndicesPrivileges(Collections.singleton("ind-1-1-*"), true, - Collections.singleton("read")); - expectedAppPrivsByResource = new ResourcePrivilegesMap(false, - Collections.singletonMap("ind-1-1-*", ResourcePrivileges.builder("ind-1-1-*").addPrivilege("read", false).build())); + resourcePrivileges = limitedByRole.checkIndicesPrivileges( + Collections.singleton("ind-1-1-*"), + true, + Collections.singleton("read") + ); + expectedAppPrivsByResource = new ResourcePrivilegesMap( + false, + Collections.singletonMap("ind-1-1-*", ResourcePrivileges.builder("ind-1-1-*").addPrivilege("read", false).build()) + ); verifyResourcesPrivileges(resourcePrivileges, expectedAppPrivsByResource); resourcePrivileges = limitedByRole.checkIndicesPrivileges(Collections.singleton("ind-*"), true, Collections.singleton("read")); - expectedAppPrivsByResource = new ResourcePrivilegesMap(false, - Collections.singletonMap("ind-*", ResourcePrivileges.builder("ind-*").addPrivilege("read", false).build())); + expectedAppPrivsByResource = new ResourcePrivilegesMap( + false, + Collections.singletonMap("ind-*", ResourcePrivileges.builder("ind-*").addPrivilege("read", false).build()) + ); verifyResourcesPrivileges(resourcePrivileges, expectedAppPrivsByResource); Role role = LimitedRole.createLimitedRole(fromRole, limitedByRole); resourcePrivileges = role.checkIndicesPrivileges(Collections.singleton("ind-1"), true, Collections.singleton("read")); - expectedAppPrivsByResource = new ResourcePrivilegesMap(true, - Collections.singletonMap("ind-1", ResourcePrivileges.builder("ind-1").addPrivilege("read", true).build())); + expectedAppPrivsByResource = new ResourcePrivilegesMap( + true, + Collections.singletonMap("ind-1", ResourcePrivileges.builder("ind-1").addPrivilege("read", true).build()) + ); verifyResourcesPrivileges(resourcePrivileges, expectedAppPrivsByResource); resourcePrivileges = role.checkIndicesPrivileges(Sets.newHashSet("ind-1-1-*", "ind-1"), true, Collections.singleton("read")); - expectedAppPrivsByResource = new ResourcePrivilegesMap(false, - mapBuilder().put("ind-1-1-*", ResourcePrivileges.builder("ind-1-1-*").addPrivilege("read", false).build()) - .put("ind-1", ResourcePrivileges.builder("ind-1").addPrivilege("read", true).build()).map()); + expectedAppPrivsByResource = new ResourcePrivilegesMap( + false, + mapBuilder().put("ind-1-1-*", ResourcePrivileges.builder("ind-1-1-*").addPrivilege("read", false).build()) + .put("ind-1", ResourcePrivileges.builder("ind-1").addPrivilege("read", true).build()) + .map() + ); verifyResourcesPrivileges(resourcePrivileges, expectedAppPrivsByResource); } { fromRole = Role.builder(Automatons.EMPTY, "a-role") - .add(FieldPermissions.DEFAULT, Collections.emptySet(), IndexPrivilege.READ, true, "ind-1*", ".security").build(); - resourcePrivileges = fromRole.checkIndicesPrivileges(Sets.newHashSet("ind-1", ".security"), true, - Collections.singleton("read")); + .add(FieldPermissions.DEFAULT, Collections.emptySet(), IndexPrivilege.READ, true, "ind-1*", ".security") + .build(); + resourcePrivileges = fromRole.checkIndicesPrivileges( + Sets.newHashSet("ind-1", ".security"), + true, + Collections.singleton("read") + ); // Map expectedResourceToResourcePrivs = new HashMap<>(); ; - expectedAppPrivsByResource = new ResourcePrivilegesMap(true, - mapBuilder().put("ind-1", ResourcePrivileges.builder("ind-1").addPrivilege("read", true).build()) - .put(".security", ResourcePrivileges.builder(".security").addPrivilege("read", true).build()).map()); + expectedAppPrivsByResource = new ResourcePrivilegesMap( + true, + mapBuilder().put("ind-1", ResourcePrivileges.builder("ind-1").addPrivilege("read", true).build()) + .put(".security", ResourcePrivileges.builder(".security").addPrivilege("read", true).build()) + .map() + ); verifyResourcesPrivileges(resourcePrivileges, expectedAppPrivsByResource); Role limitedByRole = Role.builder(Automatons.EMPTY, "limited-role").add(IndexPrivilege.READ, "ind-1", "ind-2").build(); - resourcePrivileges = limitedByRole.checkIndicesPrivileges(Sets.newHashSet("ind-1", "ind-2", ".security"), true, - Collections.singleton("read")); - - expectedAppPrivsByResource = new ResourcePrivilegesMap(false, - mapBuilder().put("ind-1", ResourcePrivileges.builder("ind-1").addPrivilege("read", true).build()) - .put("ind-2", ResourcePrivileges.builder("ind-2").addPrivilege("read", true).build()) - .put(".security", ResourcePrivileges.builder(".security").addPrivilege("read", false).build()).map()); + resourcePrivileges = limitedByRole.checkIndicesPrivileges( + Sets.newHashSet("ind-1", "ind-2", ".security"), + true, + Collections.singleton("read") + ); + + expectedAppPrivsByResource = new ResourcePrivilegesMap( + false, + mapBuilder().put("ind-1", ResourcePrivileges.builder("ind-1").addPrivilege("read", true).build()) + .put("ind-2", ResourcePrivileges.builder("ind-2").addPrivilege("read", true).build()) + .put(".security", ResourcePrivileges.builder(".security").addPrivilege("read", false).build()) + .map() + ); verifyResourcesPrivileges(resourcePrivileges, expectedAppPrivsByResource); Role role = LimitedRole.createLimitedRole(fromRole, limitedByRole); - resourcePrivileges = role.checkIndicesPrivileges(Sets.newHashSet("ind-1", "ind-2", ".security"), true, - Collections.singleton("read")); - - expectedAppPrivsByResource = new ResourcePrivilegesMap(false, - mapBuilder().put("ind-1", ResourcePrivileges.builder("ind-1").addPrivilege("read", true).build()) - .put("ind-2", ResourcePrivileges.builder("ind-2").addPrivilege("read", false).build()) - .put(".security", ResourcePrivileges.builder(".security").addPrivilege("read", false).build()).map()); + resourcePrivileges = role.checkIndicesPrivileges( + Sets.newHashSet("ind-1", "ind-2", ".security"), + true, + Collections.singleton("read") + ); + + expectedAppPrivsByResource = new ResourcePrivilegesMap( + false, + mapBuilder().put("ind-1", ResourcePrivileges.builder("ind-1").addPrivilege("read", true).build()) + .put("ind-2", ResourcePrivileges.builder("ind-2").addPrivilege("read", false).build()) + .put(".security", ResourcePrivileges.builder(".security").addPrivilege("read", false).build()) + .map() + ); verifyResourcesPrivileges(resourcePrivileges, expectedAppPrivsByResource); } } @@ -354,89 +446,244 @@ public void testGetApplicationPrivilegesByResource() { final ApplicationPrivilege app2Read = defineApplicationPrivilege("app2", "read", "data:read/*"); final ApplicationPrivilege app2Write = defineApplicationPrivilege("app2", "write", "data:write/*"); - Role fromRole = Role.builder(Automatons.EMPTY, "test-role").addApplicationPrivilege(app1Read, Collections.singleton("foo/*")) - .addApplicationPrivilege(app1All, Collections.singleton("foo/bar/baz")) - .addApplicationPrivilege(app2Read, Collections.singleton("foo/bar/*")) - .addApplicationPrivilege(app2Write, Collections.singleton("*/bar/*")).build(); + Role fromRole = Role.builder(Automatons.EMPTY, "test-role") + .addApplicationPrivilege(app1Read, Collections.singleton("foo/*")) + .addApplicationPrivilege(app1All, Collections.singleton("foo/bar/baz")) + .addApplicationPrivilege(app2Read, Collections.singleton("foo/bar/*")) + .addApplicationPrivilege(app2Write, Collections.singleton("*/bar/*")) + .build(); Set forPrivilegeNames = Sets.newHashSet("read", "write", "all"); - ResourcePrivilegesMap appPrivsByResource = fromRole.checkApplicationResourcePrivileges("app1", Collections.singleton("*"), - forPrivilegeNames, applicationPrivilegeDescriptors); - ResourcePrivilegesMap expectedAppPrivsByResource = new ResourcePrivilegesMap(false, Collections.singletonMap("*", ResourcePrivileges - .builder("*").addPrivilege("read", false).addPrivilege("write", false).addPrivilege("all", false).build())); + ResourcePrivilegesMap appPrivsByResource = fromRole.checkApplicationResourcePrivileges( + "app1", + Collections.singleton("*"), + forPrivilegeNames, + applicationPrivilegeDescriptors + ); + ResourcePrivilegesMap expectedAppPrivsByResource = new ResourcePrivilegesMap( + false, + Collections.singletonMap( + "*", + ResourcePrivileges.builder("*").addPrivilege("read", false).addPrivilege("write", false).addPrivilege("all", false).build() + ) + ); verifyResourcesPrivileges(appPrivsByResource, expectedAppPrivsByResource); - appPrivsByResource = fromRole.checkApplicationResourcePrivileges("app1", Collections.singleton("foo/x/y"), forPrivilegeNames, - applicationPrivilegeDescriptors); - expectedAppPrivsByResource = new ResourcePrivilegesMap(false, Collections.singletonMap("foo/x/y", ResourcePrivileges - .builder("foo/x/y").addPrivilege("read", true).addPrivilege("write", false).addPrivilege("all", false).build())); + appPrivsByResource = fromRole.checkApplicationResourcePrivileges( + "app1", + Collections.singleton("foo/x/y"), + forPrivilegeNames, + applicationPrivilegeDescriptors + ); + expectedAppPrivsByResource = new ResourcePrivilegesMap( + false, + Collections.singletonMap( + "foo/x/y", + ResourcePrivileges.builder("foo/x/y") + .addPrivilege("read", true) + .addPrivilege("write", false) + .addPrivilege("all", false) + .build() + ) + ); verifyResourcesPrivileges(appPrivsByResource, expectedAppPrivsByResource); - appPrivsByResource = fromRole.checkApplicationResourcePrivileges("app2", Collections.singleton("foo/bar/a"), forPrivilegeNames, - applicationPrivilegeDescriptors); - expectedAppPrivsByResource = new ResourcePrivilegesMap(false, Collections.singletonMap("foo/bar/a", ResourcePrivileges - .builder("foo/bar/a").addPrivilege("read", true).addPrivilege("write", true).addPrivilege("all", false).build())); + appPrivsByResource = fromRole.checkApplicationResourcePrivileges( + "app2", + Collections.singleton("foo/bar/a"), + forPrivilegeNames, + applicationPrivilegeDescriptors + ); + expectedAppPrivsByResource = new ResourcePrivilegesMap( + false, + Collections.singletonMap( + "foo/bar/a", + ResourcePrivileges.builder("foo/bar/a") + .addPrivilege("read", true) + .addPrivilege("write", true) + .addPrivilege("all", false) + .build() + ) + ); verifyResourcesPrivileges(appPrivsByResource, expectedAppPrivsByResource); - appPrivsByResource = fromRole.checkApplicationResourcePrivileges("app2", Collections.singleton("moon/bar/a"), forPrivilegeNames, - applicationPrivilegeDescriptors); - expectedAppPrivsByResource = new ResourcePrivilegesMap(false, Collections.singletonMap("moon/bar/a", ResourcePrivileges - .builder("moon/bar/a").addPrivilege("read", false).addPrivilege("write", true).addPrivilege("all", false).build())); + appPrivsByResource = fromRole.checkApplicationResourcePrivileges( + "app2", + Collections.singleton("moon/bar/a"), + forPrivilegeNames, + applicationPrivilegeDescriptors + ); + expectedAppPrivsByResource = new ResourcePrivilegesMap( + false, + Collections.singletonMap( + "moon/bar/a", + ResourcePrivileges.builder("moon/bar/a") + .addPrivilege("read", false) + .addPrivilege("write", true) + .addPrivilege("all", false) + .build() + ) + ); verifyResourcesPrivileges(appPrivsByResource, expectedAppPrivsByResource); { Role limitedByRole = Role.builder(Automatons.EMPTY, "test-role-scoped") .addApplicationPrivilege(app1Read, Collections.singleton("foo/scoped/*")) .addApplicationPrivilege(app2Read, Collections.singleton("foo/bar/*")) - .addApplicationPrivilege(app2Write, Collections.singleton("moo/bar/*")).build(); - appPrivsByResource = limitedByRole.checkApplicationResourcePrivileges("app1", Collections.singleton("*"), forPrivilegeNames, - applicationPrivilegeDescriptors); - expectedAppPrivsByResource = new ResourcePrivilegesMap(false, Collections.singletonMap("*", ResourcePrivileges.builder("*") - .addPrivilege("read", false).addPrivilege("write", false).addPrivilege("all", false).build())); + .addApplicationPrivilege(app2Write, Collections.singleton("moo/bar/*")) + .build(); + appPrivsByResource = limitedByRole.checkApplicationResourcePrivileges( + "app1", + Collections.singleton("*"), + forPrivilegeNames, + applicationPrivilegeDescriptors + ); + expectedAppPrivsByResource = new ResourcePrivilegesMap( + false, + Collections.singletonMap( + "*", + ResourcePrivileges.builder("*") + .addPrivilege("read", false) + .addPrivilege("write", false) + .addPrivilege("all", false) + .build() + ) + ); verifyResourcesPrivileges(appPrivsByResource, expectedAppPrivsByResource); - appPrivsByResource = limitedByRole.checkApplicationResourcePrivileges("app1", Collections.singleton("foo/x/y"), - forPrivilegeNames, applicationPrivilegeDescriptors); - expectedAppPrivsByResource = new ResourcePrivilegesMap(false, Collections.singletonMap("foo/x/y", ResourcePrivileges - .builder("foo/x/y").addPrivilege("read", false).addPrivilege("write", false).addPrivilege("all", false).build())); + appPrivsByResource = limitedByRole.checkApplicationResourcePrivileges( + "app1", + Collections.singleton("foo/x/y"), + forPrivilegeNames, + applicationPrivilegeDescriptors + ); + expectedAppPrivsByResource = new ResourcePrivilegesMap( + false, + Collections.singletonMap( + "foo/x/y", + ResourcePrivileges.builder("foo/x/y") + .addPrivilege("read", false) + .addPrivilege("write", false) + .addPrivilege("all", false) + .build() + ) + ); verifyResourcesPrivileges(appPrivsByResource, expectedAppPrivsByResource); - appPrivsByResource = limitedByRole.checkApplicationResourcePrivileges("app2", Collections.singleton("foo/bar/a"), - forPrivilegeNames, applicationPrivilegeDescriptors); - expectedAppPrivsByResource = new ResourcePrivilegesMap(false, Collections.singletonMap("foo/bar/a", ResourcePrivileges - .builder("foo/bar/a").addPrivilege("read", true).addPrivilege("write", false).addPrivilege("all", false).build())); + appPrivsByResource = limitedByRole.checkApplicationResourcePrivileges( + "app2", + Collections.singleton("foo/bar/a"), + forPrivilegeNames, + applicationPrivilegeDescriptors + ); + expectedAppPrivsByResource = new ResourcePrivilegesMap( + false, + Collections.singletonMap( + "foo/bar/a", + ResourcePrivileges.builder("foo/bar/a") + .addPrivilege("read", true) + .addPrivilege("write", false) + .addPrivilege("all", false) + .build() + ) + ); verifyResourcesPrivileges(appPrivsByResource, expectedAppPrivsByResource); - appPrivsByResource = limitedByRole.checkApplicationResourcePrivileges("app2", Collections.singleton("moon/bar/a"), - forPrivilegeNames, applicationPrivilegeDescriptors); - expectedAppPrivsByResource = new ResourcePrivilegesMap(false, Collections.singletonMap("moon/bar/a", ResourcePrivileges - .builder("moon/bar/a").addPrivilege("read", false).addPrivilege("write", false).addPrivilege("all", false).build())); + appPrivsByResource = limitedByRole.checkApplicationResourcePrivileges( + "app2", + Collections.singleton("moon/bar/a"), + forPrivilegeNames, + applicationPrivilegeDescriptors + ); + expectedAppPrivsByResource = new ResourcePrivilegesMap( + false, + Collections.singletonMap( + "moon/bar/a", + ResourcePrivileges.builder("moon/bar/a") + .addPrivilege("read", false) + .addPrivilege("write", false) + .addPrivilege("all", false) + .build() + ) + ); verifyResourcesPrivileges(appPrivsByResource, expectedAppPrivsByResource); Role role = LimitedRole.createLimitedRole(fromRole, limitedByRole); - appPrivsByResource = role.checkApplicationResourcePrivileges("app2", Collections.singleton("foo/bar/a"), forPrivilegeNames, - applicationPrivilegeDescriptors); - expectedAppPrivsByResource = new ResourcePrivilegesMap(false, Collections.singletonMap("foo/bar/a", ResourcePrivileges - .builder("foo/bar/a").addPrivilege("read", true).addPrivilege("write", false).addPrivilege("all", false).build())); + appPrivsByResource = role.checkApplicationResourcePrivileges( + "app2", + Collections.singleton("foo/bar/a"), + forPrivilegeNames, + applicationPrivilegeDescriptors + ); + expectedAppPrivsByResource = new ResourcePrivilegesMap( + false, + Collections.singletonMap( + "foo/bar/a", + ResourcePrivileges.builder("foo/bar/a") + .addPrivilege("read", true) + .addPrivilege("write", false) + .addPrivilege("all", false) + .build() + ) + ); verifyResourcesPrivileges(appPrivsByResource, expectedAppPrivsByResource); - appPrivsByResource = role.checkApplicationResourcePrivileges("app2", Collections.singleton("moon/bar/a"), forPrivilegeNames, - applicationPrivilegeDescriptors); - expectedAppPrivsByResource = new ResourcePrivilegesMap(false, Collections.singletonMap("moon/bar/a", ResourcePrivileges - .builder("moon/bar/a").addPrivilege("read", false).addPrivilege("write", false).addPrivilege("all", false).build())); + appPrivsByResource = role.checkApplicationResourcePrivileges( + "app2", + Collections.singleton("moon/bar/a"), + forPrivilegeNames, + applicationPrivilegeDescriptors + ); + expectedAppPrivsByResource = new ResourcePrivilegesMap( + false, + Collections.singletonMap( + "moon/bar/a", + ResourcePrivileges.builder("moon/bar/a") + .addPrivilege("read", false) + .addPrivilege("write", false) + .addPrivilege("all", false) + .build() + ) + ); verifyResourcesPrivileges(appPrivsByResource, expectedAppPrivsByResource); - appPrivsByResource = role.checkApplicationResourcePrivileges("unknown", Collections.singleton("moon/bar/a"), forPrivilegeNames, - applicationPrivilegeDescriptors); - expectedAppPrivsByResource = new ResourcePrivilegesMap(false, Collections.singletonMap("moon/bar/a", ResourcePrivileges - .builder("moon/bar/a").addPrivilege("read", false).addPrivilege("write", false).addPrivilege("all", false).build())); + appPrivsByResource = role.checkApplicationResourcePrivileges( + "unknown", + Collections.singleton("moon/bar/a"), + forPrivilegeNames, + applicationPrivilegeDescriptors + ); + expectedAppPrivsByResource = new ResourcePrivilegesMap( + false, + Collections.singletonMap( + "moon/bar/a", + ResourcePrivileges.builder("moon/bar/a") + .addPrivilege("read", false) + .addPrivilege("write", false) + .addPrivilege("all", false) + .build() + ) + ); verifyResourcesPrivileges(appPrivsByResource, expectedAppPrivsByResource); - appPrivsByResource = role.checkApplicationResourcePrivileges("app2", Collections.singleton("moo/bar/a"), - Sets.newHashSet("read", "write", "all", "unknown"), applicationPrivilegeDescriptors); - expectedAppPrivsByResource = new ResourcePrivilegesMap(false, - Collections.singletonMap("moo/bar/a", ResourcePrivileges.builder("moo/bar/a").addPrivilege("read", false) - .addPrivilege("write", true).addPrivilege("all", false).addPrivilege("unknown", false).build())); + appPrivsByResource = role.checkApplicationResourcePrivileges( + "app2", + Collections.singleton("moo/bar/a"), + Sets.newHashSet("read", "write", "all", "unknown"), + applicationPrivilegeDescriptors + ); + expectedAppPrivsByResource = new ResourcePrivilegesMap( + false, + Collections.singletonMap( + "moo/bar/a", + ResourcePrivileges.builder("moo/bar/a") + .addPrivilege("read", false) + .addPrivilege("write", true) + .addPrivilege("all", false) + .addPrivilege("unknown", false) + .build() + ) + ); verifyResourcesPrivileges(appPrivsByResource, expectedAppPrivsByResource); } } @@ -448,14 +695,16 @@ private void verifyResourcesPrivileges(ResourcePrivilegesMap resourcePrivileges, private IndexAbstraction mockIndexAbstraction(String name) { IndexAbstraction mock = mock(IndexAbstraction.class); when(mock.getName()).thenReturn(name); - when(mock.getType()).thenReturn(randomFrom(IndexAbstraction.Type.CONCRETE_INDEX, - IndexAbstraction.Type.ALIAS, IndexAbstraction.Type.DATA_STREAM)); + when(mock.getType()).thenReturn( + randomFrom(IndexAbstraction.Type.CONCRETE_INDEX, IndexAbstraction.Type.ALIAS, IndexAbstraction.Type.DATA_STREAM) + ); return mock; } private ApplicationPrivilege defineApplicationPrivilege(String app, String name, String... actions) { - applicationPrivilegeDescriptors - .add(new ApplicationPrivilegeDescriptor(app, name, Sets.newHashSet(actions), Collections.emptyMap())); + applicationPrivilegeDescriptors.add( + new ApplicationPrivilegeDescriptor(app, name, Sets.newHashSet(actions), Collections.emptyMap()) + ); return new ApplicationPrivilege(app, name, actions); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/permission/ResourcePrivilegesMapTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/permission/ResourcePrivilegesMapTests.java index c1f7a0f25ba81..a9edb87a90cad 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/permission/ResourcePrivilegesMapTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/permission/ResourcePrivilegesMapTests.java @@ -18,22 +18,25 @@ public class ResourcePrivilegesMapTests extends ESTestCase { public void testBuilder() { ResourcePrivilegesMap instance = ResourcePrivilegesMap.builder() - .addResourcePrivilege("*", mapBuilder().put("read", true).put("write", true).map()).build(); + .addResourcePrivilege("*", mapBuilder().put("read", true).put("write", true).map()) + .build(); assertThat(instance.allAllowed(), is(true)); assertThat(instance.getResourceToResourcePrivileges().size(), is(1)); assertThat(instance.getResourceToResourcePrivileges().get("*").isAllowed("read"), is(true)); assertThat(instance.getResourceToResourcePrivileges().get("*").isAllowed("write"), is(true)); - instance = ResourcePrivilegesMap.builder().addResourcePrivilege("*", mapBuilder().put("read", true).put("write", false).map()) - .build(); + instance = ResourcePrivilegesMap.builder() + .addResourcePrivilege("*", mapBuilder().put("read", true).put("write", false).map()) + .build(); assertThat(instance.allAllowed(), is(false)); assertThat(instance.getResourceToResourcePrivileges().size(), is(1)); assertThat(instance.getResourceToResourcePrivileges().get("*").isAllowed("read"), is(true)); assertThat(instance.getResourceToResourcePrivileges().get("*").isAllowed("write"), is(false)); instance = ResourcePrivilegesMap.builder() - .addResourcePrivilege("some-other", mapBuilder().put("index", true).put("write", true).map()) - .addResourcePrivilegesMap(instance).build(); + .addResourcePrivilege("some-other", mapBuilder().put("index", true).put("write", true).map()) + .addResourcePrivilegesMap(instance) + .build(); assertThat(instance.allAllowed(), is(false)); assertThat(instance.getResourceToResourcePrivileges().size(), is(2)); assertThat(instance.getResourceToResourcePrivileges().get("*").isAllowed("read"), is(true)); @@ -44,12 +47,14 @@ public void testBuilder() { public void testIntersection() { ResourcePrivilegesMap instance = ResourcePrivilegesMap.builder() - .addResourcePrivilege("*", mapBuilder().put("read", true).put("write", true).map()) - .addResourcePrivilege("index-*", mapBuilder().put("read", true).put("write", true).map()).build(); + .addResourcePrivilege("*", mapBuilder().put("read", true).put("write", true).map()) + .addResourcePrivilege("index-*", mapBuilder().put("read", true).put("write", true).map()) + .build(); ResourcePrivilegesMap otherInstance = ResourcePrivilegesMap.builder() - .addResourcePrivilege("*", mapBuilder().put("read", true).put("write", false).map()) - .addResourcePrivilege("index-*", mapBuilder().put("read", false).put("write", true).map()) - .addResourcePrivilege("index-uncommon", mapBuilder().put("read", false).put("write", true).map()).build(); + .addResourcePrivilege("*", mapBuilder().put("read", true).put("write", false).map()) + .addResourcePrivilege("index-*", mapBuilder().put("read", false).put("write", true).map()) + .addResourcePrivilege("index-uncommon", mapBuilder().put("read", false).put("write", true).map()) + .build(); ResourcePrivilegesMap result = ResourcePrivilegesMap.intersection(instance, otherInstance); assertThat(result.allAllowed(), is(false)); assertThat(result.getResourceToResourcePrivileges().size(), is(2)); @@ -62,27 +67,34 @@ public void testIntersection() { public void testEqualsHashCode() { ResourcePrivilegesMap instance = ResourcePrivilegesMap.builder() - .addResourcePrivilege("*", mapBuilder().put("read", true).put("write", true).map()).build(); + .addResourcePrivilege("*", mapBuilder().put("read", true).put("write", true).map()) + .build(); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(instance, (original) -> { - return ResourcePrivilegesMap.builder().addResourcePrivilegesMap(original).build(); - }); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(instance, (original) -> { - return ResourcePrivilegesMap.builder().addResourcePrivilegesMap(original).build(); - }, ResourcePrivilegesMapTests::mutateTestItem); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + instance, + (original) -> { return ResourcePrivilegesMap.builder().addResourcePrivilegesMap(original).build(); } + ); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + instance, + (original) -> { return ResourcePrivilegesMap.builder().addResourcePrivilegesMap(original).build(); }, + ResourcePrivilegesMapTests::mutateTestItem + ); } private static ResourcePrivilegesMap mutateTestItem(ResourcePrivilegesMap original) { switch (randomIntBetween(0, 1)) { - case 0: - return ResourcePrivilegesMap.builder() - .addResourcePrivilege(randomAlphaOfLength(6), mapBuilder().put("read", true).put("write", true).map()).build(); - case 1: - return ResourcePrivilegesMap.builder().addResourcePrivilege("*", mapBuilder().put("read", false).put("write", false).map()) + case 0: + return ResourcePrivilegesMap.builder() + .addResourcePrivilege(randomAlphaOfLength(6), mapBuilder().put("read", true).put("write", true).map()) + .build(); + case 1: + return ResourcePrivilegesMap.builder() + .addResourcePrivilege("*", mapBuilder().put("read", false).put("write", false).map()) + .build(); + default: + return ResourcePrivilegesMap.builder() + .addResourcePrivilege(randomAlphaOfLength(6), mapBuilder().put("read", true).put("write", true).map()) .build(); - default: - return ResourcePrivilegesMap.builder() - .addResourcePrivilege(randomAlphaOfLength(6), mapBuilder().put("read", true).put("write", true).map()).build(); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/permission/ResourcePrivilegesTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/permission/ResourcePrivilegesTests.java index 8bf7aa35a3885..db155e424a2bc 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/permission/ResourcePrivilegesTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/permission/ResourcePrivilegesTests.java @@ -40,28 +40,33 @@ public void testWhenSamePrivilegeExists() { public void testEqualsHashCode() { ResourcePrivileges instance = createInstance(); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(instance, (original) -> { - return ResourcePrivileges.builder(original.getResource()).addPrivileges(original.getPrivileges()).build(); - }); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(instance, (original) -> { - return ResourcePrivileges.builder(original.getResource()).addPrivileges(original.getPrivileges()).build(); - }, ResourcePrivilegesTests::mutateTestItem); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + instance, + (original) -> { return ResourcePrivileges.builder(original.getResource()).addPrivileges(original.getPrivileges()).build(); } + ); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + instance, + (original) -> { return ResourcePrivileges.builder(original.getResource()).addPrivileges(original.getPrivileges()).build(); }, + ResourcePrivilegesTests::mutateTestItem + ); } private ResourcePrivileges createInstance() { - ResourcePrivileges instance = ResourcePrivileges.builder("*").addPrivilege("read", true) - .addPrivileges(Collections.singletonMap("write", false)).build(); + ResourcePrivileges instance = ResourcePrivileges.builder("*") + .addPrivilege("read", true) + .addPrivileges(Collections.singletonMap("write", false)) + .build(); return instance; } private static ResourcePrivileges mutateTestItem(ResourcePrivileges original) { switch (randomIntBetween(0, 1)) { - case 0: - return ResourcePrivileges.builder(randomAlphaOfLength(6)).addPrivileges(original.getPrivileges()).build(); - case 1: - return ResourcePrivileges.builder(original.getResource()).addPrivileges(Collections.emptyMap()).build(); - default: - return ResourcePrivileges.builder(randomAlphaOfLength(6)).addPrivileges(Collections.emptyMap()).build(); + case 0: + return ResourcePrivileges.builder(randomAlphaOfLength(6)).addPrivileges(original.getPrivileges()).build(); + case 1: + return ResourcePrivileges.builder(original.getResource()).addPrivileges(Collections.emptyMap()).build(); + default: + return ResourcePrivileges.builder(randomAlphaOfLength(6)).addPrivileges(Collections.emptyMap()).build(); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/privilege/ApplicationPrivilegeDescriptorTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/privilege/ApplicationPrivilegeDescriptorTests.java index 2dd752ead277c..3e204988033c0 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/privilege/ApplicationPrivilegeDescriptorTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/privilege/ApplicationPrivilegeDescriptorTests.java @@ -8,14 +8,14 @@ import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.util.set.Sets; +import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.EqualsHashCodeTestUtils; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.test.EqualsHashCodeTestUtils; import org.hamcrest.Matchers; import java.io.ByteArrayOutputStream; @@ -35,18 +35,29 @@ public class ApplicationPrivilegeDescriptorTests extends ESTestCase { public void testEqualsAndHashCode() { final ApplicationPrivilegeDescriptor privilege = randomPrivilege(); final EqualsHashCodeTestUtils.MutateFunction mutate = randomFrom( + orig -> new ApplicationPrivilegeDescriptor("x" + orig.getApplication(), orig.getName(), orig.getActions(), orig.getMetadata()), + orig -> new ApplicationPrivilegeDescriptor(orig.getApplication(), "x" + orig.getName(), orig.getActions(), orig.getMetadata()), orig -> new ApplicationPrivilegeDescriptor( - "x" + orig.getApplication(), orig.getName(), orig.getActions(), orig.getMetadata()), - orig -> new ApplicationPrivilegeDescriptor( - orig.getApplication(), "x" + orig.getName(), orig.getActions(), orig.getMetadata()), - orig -> new ApplicationPrivilegeDescriptor( - orig.getApplication(), orig.getName(), Collections.singleton("*"), orig.getMetadata()), + orig.getApplication(), + orig.getName(), + Collections.singleton("*"), + orig.getMetadata() + ), orig -> new ApplicationPrivilegeDescriptor( - orig.getApplication(), orig.getName(), orig.getActions(), Collections.singletonMap("mutate", -1L)) + orig.getApplication(), + orig.getName(), + orig.getActions(), + Collections.singletonMap("mutate", -1L) + ) ); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(privilege, + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + privilege, original -> new ApplicationPrivilegeDescriptor( - original.getApplication(), original.getName(), original.getActions(), original.getMetadata()), + original.getApplication(), + original.getName(), + original.getActions(), + original.getMetadata() + ), mutate ); } @@ -80,10 +91,12 @@ public void testXContentGenerationAndParsing() throws IOException { final byte[] bytes = out.toByteArray(); try (XContentParser parser = xContent.createParser(NamedXContentRegistry.EMPTY, THROW_UNSUPPORTED_OPERATION, bytes)) { - final ApplicationPrivilegeDescriptor clone = ApplicationPrivilegeDescriptor.parse(parser, + final ApplicationPrivilegeDescriptor clone = ApplicationPrivilegeDescriptor.parse( + parser, randomBoolean() ? randomAlphaOfLength(3) : null, randomBoolean() ? randomAlphaOfLength(3) : null, - includeTypeField); + includeTypeField + ); assertThat(clone, Matchers.equalTo(original)); assertThat(original, Matchers.equalTo(clone)); } @@ -105,11 +118,7 @@ public void testParseXContentWithDefaultNames() throws IOException { } public void testParseXContentWithoutUsingDefaultNames() throws IOException { - final String json = "{" + - " \"application\": \"your_app\"," + - " \"name\": \"write\"," + - " \"actions\": [ \"data:write\" ]" + - "}"; + final String json = "{" + " \"application\": \"your_app\"," + " \"name\": \"write\"," + " \"actions\": [ \"data:write\" ]" + "}"; final XContent xContent = XContentType.JSON.xContent(); try (XContentParser parser = xContent.createParser(NamedXContentRegistry.EMPTY, THROW_UNSUPPORTED_OPERATION, json)) { final ApplicationPrivilegeDescriptor privilege = ApplicationPrivilegeDescriptor.parse(parser, "my_app", "read", false); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/privilege/ApplicationPrivilegeTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/privilege/ApplicationPrivilegeTests.java index 04e5077f6108d..eb62ab5fd1eb4 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/privilege/ApplicationPrivilegeTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/privilege/ApplicationPrivilegeTests.java @@ -7,6 +7,7 @@ package org.elasticsearch.xpack.core.security.authz.privilege; import junit.framework.AssertionFailedError; + import org.apache.lucene.util.automaton.CharacterRunAutomaton; import org.elasticsearch.common.util.set.Sets; import org.elasticsearch.test.ESTestCase; @@ -51,8 +52,11 @@ public void testValidationOfApplicationName() { assertValidationFailure("app*", "application names", () -> ApplicationPrivilege.validateApplicationName("app*")); // no special characters with wildcards final String appNameWithSpecialCharAndWildcard = "app" + specialCharacter.get() + "*"; - assertValidationFailure(appNameWithSpecialCharAndWildcard, "application name", - () -> ApplicationPrivilege.validateApplicationNameOrWildcard(appNameWithSpecialCharAndWildcard)); + assertValidationFailure( + appNameWithSpecialCharAndWildcard, + "application name", + () -> ApplicationPrivilege.validateApplicationNameOrWildcard(appNameWithSpecialCharAndWildcard) + ); String appNameWithSpecialChars = "myapp" + randomFrom('-', '_'); for (int i = randomIntBetween(1, 12); i > 0; i--) { @@ -78,7 +82,7 @@ public void testValidationOfPrivilegeName() { assertValidationFailure("1read", "privilege names", () -> ApplicationPrivilege.validatePrivilegeName("1read")); // cannot contain special characters final String specialChars = ":;$#%()+=/',"; - final String withSpecialChar = "read" + specialChars.charAt(randomInt(specialChars.length()-1)); + final String withSpecialChar = "read" + specialChars.charAt(randomInt(specialChars.length() - 1)); assertValidationFailure(withSpecialChar, "privilege names", () -> ApplicationPrivilege.validatePrivilegeName(withSpecialChar)); // these should all be OK @@ -158,7 +162,8 @@ public void testEqualsAndHashCode() { orig -> createPrivilege(orig.getApplication(), "x" + getPrivilegeName(orig), orig.getPatterns()), orig -> new ApplicationPrivilege(orig.getApplication(), getPrivilegeName(orig), "*") ); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(privilege, + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + privilege, original -> createPrivilege(original.getApplication(), getPrivilegeName(original), original.getPatterns()), mutate ); @@ -176,7 +181,7 @@ private String getPrivilegeName(ApplicationPrivilege privilege) { } } - private void assertValidationFailure(String reason,String messageContent, ThrowingRunnable body) { + private void assertValidationFailure(String reason, String messageContent, ThrowingRunnable body) { final IllegalArgumentException exception; try { exception = expectThrows(IllegalArgumentException.class, body); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/privilege/ClusterPrivilegeResolverTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/privilege/ClusterPrivilegeResolverTests.java index 621e8605dbb53..17d75427dbb8c 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/privilege/ClusterPrivilegeResolverTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/privilege/ClusterPrivilegeResolverTests.java @@ -19,14 +19,16 @@ public class ClusterPrivilegeResolverTests extends ESTestCase { public void testSortByAccessLevel() throws Exception { - final List privileges = new ArrayList<>(List.of( - ClusterPrivilegeResolver.ALL, - ClusterPrivilegeResolver.MONITOR, - ClusterPrivilegeResolver.MANAGE, - ClusterPrivilegeResolver.MANAGE_OWN_API_KEY, - ClusterPrivilegeResolver.MANAGE_API_KEY, - ClusterPrivilegeResolver.MANAGE_SECURITY - )); + final List privileges = new ArrayList<>( + List.of( + ClusterPrivilegeResolver.ALL, + ClusterPrivilegeResolver.MONITOR, + ClusterPrivilegeResolver.MANAGE, + ClusterPrivilegeResolver.MANAGE_OWN_API_KEY, + ClusterPrivilegeResolver.MANAGE_API_KEY, + ClusterPrivilegeResolver.MANAGE_SECURITY + ) + ); Collections.shuffle(privileges, random()); final SortedMap sorted = ClusterPrivilegeResolver.sortByAccessLevel(privileges); // This is: diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/privilege/ConfigurableClusterPrivilegesTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/privilege/ConfigurableClusterPrivilegesTests.java index 0c69796013642..509d8fecf7780 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/privilege/ConfigurableClusterPrivilegesTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/privilege/ConfigurableClusterPrivilegesTests.java @@ -12,13 +12,13 @@ import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.XPackClientPlugin; import java.io.ByteArrayOutputStream; @@ -67,8 +67,6 @@ private ConfigurableClusterPrivilege[] buildSecurityPrivileges() { } private ConfigurableClusterPrivilege[] buildSecurityPrivileges(int applicationNameLength) { - return new ConfigurableClusterPrivilege[] { - ManageApplicationPrivilegesTests.buildPrivileges(applicationNameLength) - }; + return new ConfigurableClusterPrivilege[] { ManageApplicationPrivilegesTests.buildPrivileges(applicationNameLength) }; } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/privilege/ManageApplicationPrivilegesTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/privilege/ManageApplicationPrivilegesTests.java index b6ce496b14c80..8e3936a96fc56 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/privilege/ManageApplicationPrivilegesTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/privilege/ManageApplicationPrivilegesTests.java @@ -13,14 +13,14 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.set.Sets; +import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.EqualsHashCodeTestUtils; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.test.EqualsHashCodeTestUtils; import org.elasticsearch.xpack.core.XPackClientPlugin; import org.elasticsearch.xpack.core.security.action.privilege.DeletePrivilegesRequest; import org.elasticsearch.xpack.core.security.action.privilege.GetPrivilegesRequest; @@ -87,8 +87,9 @@ public void testGenerateAndParseXContent() throws Exception { public void testEqualsAndHashCode() { final int applicationNameLength = randomIntBetween(4, 7); final ManageApplicationPrivileges privileges = buildPrivileges(applicationNameLength); - final EqualsHashCodeTestUtils.MutateFunction mutate - = orig -> buildPrivileges(applicationNameLength + randomIntBetween(1, 3)); + final EqualsHashCodeTestUtils.MutateFunction mutate = orig -> buildPrivileges( + applicationNameLength + randomIntBetween(1, 3) + ); EqualsHashCodeTestUtils.checkEqualsAndHashCode(privileges, this::clone, mutate); } @@ -106,7 +107,7 @@ public void testActionAndRequestPredicate() { assertTrue(kibanaAndLogstashPermission.check("cluster:admin/xpack/security/privilege/get", getKibana1, authentication)); assertFalse(cloudAndSwiftypePermission.check("cluster:admin/xpack/security/privilege/get", getKibana1, authentication)); - final DeletePrivilegesRequest deleteLogstash = new DeletePrivilegesRequest("logstash", new String[]{"all"}); + final DeletePrivilegesRequest deleteLogstash = new DeletePrivilegesRequest("logstash", new String[] { "all" }); assertTrue(kibanaAndLogstashPermission.check("cluster:admin/xpack/security/privilege/get", deleteLogstash, authentication)); assertFalse(cloudAndSwiftypePermission.check("cluster:admin/xpack/security/privilege/get", deleteLogstash, authentication)); @@ -114,8 +115,14 @@ public void testActionAndRequestPredicate() { final List kibanaPrivileges = new ArrayList<>(); for (int i = randomIntBetween(2, 6); i > 0; i--) { - kibanaPrivileges.add(new ApplicationPrivilegeDescriptor("kibana-" + i, - randomAlphaOfLengthBetween(3, 6).toLowerCase(Locale.ROOT), Collections.emptySet(), Collections.emptyMap())); + kibanaPrivileges.add( + new ApplicationPrivilegeDescriptor( + "kibana-" + i, + randomAlphaOfLengthBetween(3, 6).toLowerCase(Locale.ROOT), + Collections.emptySet(), + Collections.emptyMap() + ) + ); } putKibana.setPrivileges(kibanaPrivileges); assertTrue(kibanaAndLogstashPermission.check("cluster:admin/xpack/security/privilege/get", putKibana, authentication)); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/privilege/ManageOwnApiKeyClusterPrivilegeTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/privilege/ManageOwnApiKeyClusterPrivilegeTests.java index e5afe013e6b7c..eda0e4e6b455c 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/privilege/ManageOwnApiKeyClusterPrivilegeTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/privilege/ManageOwnApiKeyClusterPrivilegeTests.java @@ -29,12 +29,16 @@ public class ManageOwnApiKeyClusterPrivilegeTests extends ESTestCase { public void testAuthenticationWithApiKeyAllowsAccessToApiKeyActionsWhenItIsOwner() { - final ClusterPermission clusterPermission = - ManageOwnApiKeyClusterPrivilege.INSTANCE.buildPermission(ClusterPermission.builder()).build(); + final ClusterPermission clusterPermission = ManageOwnApiKeyClusterPrivilege.INSTANCE.buildPermission(ClusterPermission.builder()) + .build(); final String apiKeyId = randomAlphaOfLengthBetween(4, 7); - final Authentication authentication = createMockAuthentication("joe","_es_api_key", - AuthenticationType.API_KEY, Map.of("_security_api_key_id", apiKeyId)); + final Authentication authentication = createMockAuthentication( + "joe", + "_es_api_key", + AuthenticationType.API_KEY, + Map.of("_security_api_key_id", apiKeyId) + ); final TransportRequest getApiKeyRequest = GetApiKeyRequest.usingApiKeyId(apiKeyId, randomBoolean()); final TransportRequest invalidateApiKeyRequest = InvalidateApiKeyRequest.usingApiKeyId(apiKeyId, randomBoolean()); @@ -44,12 +48,16 @@ public void testAuthenticationWithApiKeyAllowsAccessToApiKeyActionsWhenItIsOwner } public void testAuthenticationWithApiKeyDeniesAccessToApiKeyActionsWhenItIsNotOwner() { - final ClusterPermission clusterPermission = - ManageOwnApiKeyClusterPrivilege.INSTANCE.buildPermission(ClusterPermission.builder()).build(); + final ClusterPermission clusterPermission = ManageOwnApiKeyClusterPrivilege.INSTANCE.buildPermission(ClusterPermission.builder()) + .build(); final String apiKeyId = randomAlphaOfLengthBetween(4, 7); - final Authentication authentication = createMockAuthentication("joe","_es_api_key", - AuthenticationType.API_KEY, Map.of("_security_api_key_id", randomAlphaOfLength(7))); + final Authentication authentication = createMockAuthentication( + "joe", + "_es_api_key", + AuthenticationType.API_KEY, + Map.of("_security_api_key_id", randomAlphaOfLength(7)) + ); final TransportRequest getApiKeyRequest = GetApiKeyRequest.usingApiKeyId(apiKeyId, randomBoolean()); final TransportRequest invalidateApiKeyRequest = InvalidateApiKeyRequest.usingApiKeyId(apiKeyId, randomBoolean()); @@ -58,11 +66,10 @@ public void testAuthenticationWithApiKeyDeniesAccessToApiKeyActionsWhenItIsNotOw } public void testAuthenticationWithUserAllowsAccessToApiKeyActionsWhenItIsOwner() { - final ClusterPermission clusterPermission = - ManageOwnApiKeyClusterPrivilege.INSTANCE.buildPermission(ClusterPermission.builder()).build(); + final ClusterPermission clusterPermission = ManageOwnApiKeyClusterPrivilege.INSTANCE.buildPermission(ClusterPermission.builder()) + .build(); - final Authentication authentication = createMockAuthentication("joe","realm1", - AuthenticationType.REALM, Map.of()); + final Authentication authentication = createMockAuthentication("joe", "realm1", AuthenticationType.REALM, Map.of()); final TransportRequest getApiKeyRequest = GetApiKeyRequest.usingRealmAndUserName("realm1", "joe"); final TransportRequest invalidateApiKeyRequest = InvalidateApiKeyRequest.usingRealmAndUserName("realm1", "joe"); @@ -72,11 +79,10 @@ public void testAuthenticationWithUserAllowsAccessToApiKeyActionsWhenItIsOwner() } public void testAuthenticationWithUserAllowsAccessToApiKeyActionsWhenItIsOwner_WithOwnerFlagOnly() { - final ClusterPermission clusterPermission = - ManageOwnApiKeyClusterPrivilege.INSTANCE.buildPermission(ClusterPermission.builder()).build(); + final ClusterPermission clusterPermission = ManageOwnApiKeyClusterPrivilege.INSTANCE.buildPermission(ClusterPermission.builder()) + .build(); - final Authentication authentication = createMockAuthentication("joe","realm1", - AuthenticationType.REALM, Map.of()); + final Authentication authentication = createMockAuthentication("joe", "realm1", AuthenticationType.REALM, Map.of()); final TransportRequest getApiKeyRequest = GetApiKeyRequest.forOwnedApiKeys(); final TransportRequest invalidateApiKeyRequest = InvalidateApiKeyRequest.forOwnedApiKeys(); @@ -86,41 +92,57 @@ public void testAuthenticationWithUserAllowsAccessToApiKeyActionsWhenItIsOwner_W } public void testAuthenticationWithUserDeniesAccessToApiKeyActionsWhenItIsNotOwner() { - final ClusterPermission clusterPermission = - ManageOwnApiKeyClusterPrivilege.INSTANCE.buildPermission(ClusterPermission.builder()).build(); + final ClusterPermission clusterPermission = ManageOwnApiKeyClusterPrivilege.INSTANCE.buildPermission(ClusterPermission.builder()) + .build(); - final Authentication authentication = createMockAuthentication("joe", "realm1", - AuthenticationType.REALM, Map.of()); + final Authentication authentication = createMockAuthentication("joe", "realm1", AuthenticationType.REALM, Map.of()); final TransportRequest getApiKeyRequest = randomFrom( GetApiKeyRequest.usingRealmAndUserName("realm1", randomAlphaOfLength(7)), GetApiKeyRequest.usingRealmAndUserName(randomAlphaOfLength(5), "joe"), - new GetApiKeyRequest(randomAlphaOfLength(5), randomAlphaOfLength(7), null, null, false)); + new GetApiKeyRequest(randomAlphaOfLength(5), randomAlphaOfLength(7), null, null, false) + ); final TransportRequest invalidateApiKeyRequest = randomFrom( InvalidateApiKeyRequest.usingRealmAndUserName("realm1", randomAlphaOfLength(7)), InvalidateApiKeyRequest.usingRealmAndUserName(randomAlphaOfLength(5), "joe"), - new InvalidateApiKeyRequest(randomAlphaOfLength(5), randomAlphaOfLength(7), null, false, null)); + new InvalidateApiKeyRequest(randomAlphaOfLength(5), randomAlphaOfLength(7), null, false, null) + ); assertFalse(clusterPermission.check("cluster:admin/xpack/security/api_key/get", getApiKeyRequest, authentication)); assertFalse(clusterPermission.check("cluster:admin/xpack/security/api_key/invalidate", invalidateApiKeyRequest, authentication)); } public void testGetAndInvalidateApiKeyWillRespectRunAsUser() { - final ClusterPermission clusterPermission = - ManageOwnApiKeyClusterPrivilege.INSTANCE.buildPermission(ClusterPermission.builder()).build(); + final ClusterPermission clusterPermission = ManageOwnApiKeyClusterPrivilege.INSTANCE.buildPermission(ClusterPermission.builder()) + .build(); final Authentication authentication = createMockRunAsAuthentication( - "user_a", "realm_a", "realm_a_type", - "user_b", "realm_b", "realm_b_type"); - - assertTrue(clusterPermission.check("cluster:admin/xpack/security/api_key/get", - GetApiKeyRequest.usingRealmAndUserName("realm_b", "user_b"), authentication)); - assertTrue(clusterPermission.check("cluster:admin/xpack/security/api_key/invalidate", - InvalidateApiKeyRequest.usingRealmAndUserName("realm_b", "user_b"), authentication)); + "user_a", + "realm_a", + "realm_a_type", + "user_b", + "realm_b", + "realm_b_type" + ); + + assertTrue( + clusterPermission.check( + "cluster:admin/xpack/security/api_key/get", + GetApiKeyRequest.usingRealmAndUserName("realm_b", "user_b"), + authentication + ) + ); + assertTrue( + clusterPermission.check( + "cluster:admin/xpack/security/api_key/invalidate", + InvalidateApiKeyRequest.usingRealmAndUserName("realm_b", "user_b"), + authentication + ) + ); } public void testCheckQueryApiKeyRequest() { - final ClusterPermission clusterPermission = - ManageOwnApiKeyClusterPrivilege.INSTANCE.buildPermission(ClusterPermission.builder()).build(); + final ClusterPermission clusterPermission = ManageOwnApiKeyClusterPrivilege.INSTANCE.buildPermission(ClusterPermission.builder()) + .build(); final QueryApiKeyRequest queryApiKeyRequest = new QueryApiKeyRequest(); if (randomBoolean()) { @@ -128,11 +150,16 @@ public void testCheckQueryApiKeyRequest() { } assertThat( clusterPermission.check(QueryApiKeyAction.NAME, queryApiKeyRequest, mock(Authentication.class)), - is(queryApiKeyRequest.isFilterForCurrentUser())); + is(queryApiKeyRequest.isFilterForCurrentUser()) + ); } - private Authentication createMockAuthentication(String username, String realmName, - AuthenticationType authenticationType, Map metadata) { + private Authentication createMockAuthentication( + String username, + String realmName, + AuthenticationType authenticationType, + Map metadata + ) { final User user = new User(username); final Authentication authentication = mock(Authentication.class); final Authentication.RealmRef authenticatedBy = mock(Authentication.RealmRef.class); @@ -144,8 +171,14 @@ private Authentication createMockAuthentication(String username, String realmNam return authentication; } - private Authentication createMockRunAsAuthentication(String username, String realmName, String realmType, - String runAsUsername, String runAsRealmName, String runAsRealmType) { + private Authentication createMockRunAsAuthentication( + String username, + String realmName, + String realmType, + String runAsUsername, + String runAsRealmName, + String runAsRealmType + ) { final Authentication.RealmRef authenticatedBy = mock(Authentication.RealmRef.class); when(authenticatedBy.getName()).thenReturn(realmName); when(authenticatedBy.getType()).thenReturn(realmType); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/privilege/PrivilegeTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/privilege/PrivilegeTests.java index b79dbb5a4a1c2..1c0167bcc15c0 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/privilege/PrivilegeTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/privilege/PrivilegeTests.java @@ -39,18 +39,21 @@ public void testSubActionPattern() throws Exception { assertThat(predicate.test("bar[n][nodes]"), is(false)); assertThat(predicate.test("[n][nodes]"), is(false)); } + private void verifyClusterActionAllowed(ClusterPrivilege clusterPrivilege, String... actions) { ClusterPermission clusterPermission = clusterPrivilege.buildPermission(ClusterPermission.builder()).build(); - for (String action: actions) { + for (String action : actions) { assertTrue(clusterPermission.check(action, mock(TransportRequest.class), mock(Authentication.class))); } } + private void verifyClusterActionDenied(ClusterPrivilege clusterPrivilege, String... actions) { ClusterPermission clusterPermission = clusterPrivilege.buildPermission(ClusterPermission.builder()).build(); - for (String action: actions) { + for (String action : actions) { assertFalse(clusterPermission.check(action, mock(TransportRequest.class), mock(Authentication.class))); } } + public void testCluster() throws Exception { ClusterPrivilege allClusterPrivilege = ClusterPrivilegeResolver.resolve("all"); assertThat(allClusterPrivilege, is(ClusterPrivilegeResolver.ALL)); @@ -107,10 +110,18 @@ public void testClusterAction() throws Exception { // ClusterPrivilegeResolver.resolve() for a cluster action converts action name into a pattern by adding "*" ClusterPrivilege clusterPrivilegeSnapshotDelete = ClusterPrivilegeResolver.resolve("cluster:admin/snapshot/delete"); assertThat(clusterPrivilegeSnapshotDelete, notNullValue()); - verifyClusterActionAllowed(clusterPrivilegeSnapshotDelete, "cluster:admin/snapshot/delete", "cluster:admin/snapshot/delete[n]", - "cluster:admin/snapshot/delete/non-existing"); - verifyClusterActionDenied(clusterPrivilegeSnapshotDelete, "cluster:admin/snapshot/dele", "cluster:admin/snapshot/dele[n]", - "cluster:admin/snapshot/dele/non-existing"); + verifyClusterActionAllowed( + clusterPrivilegeSnapshotDelete, + "cluster:admin/snapshot/delete", + "cluster:admin/snapshot/delete[n]", + "cluster:admin/snapshot/delete/non-existing" + ); + verifyClusterActionDenied( + clusterPrivilegeSnapshotDelete, + "cluster:admin/snapshot/dele", + "cluster:admin/snapshot/dele[n]", + "cluster:admin/snapshot/dele/non-existing" + ); } public void testIndexAction() throws Exception { @@ -119,14 +130,13 @@ public void testIndexAction() throws Exception { assertThat(index, notNullValue()); assertThat(index.predicate().test("indices:admin/mapping/delete"), is(true)); assertThat(index.predicate().test("indices:admin/mapping/dele"), is(false)); - assertThat(IndexPrivilege.READ_CROSS_CLUSTER.predicate() - .test("internal:transport/proxy/indices:data/read/query"), is(true)); + assertThat(IndexPrivilege.READ_CROSS_CLUSTER.predicate().test("internal:transport/proxy/indices:data/read/query"), is(true)); } public void testIndexCollapse() throws Exception { IndexPrivilege[] values = IndexPrivilege.values().values().toArray(new IndexPrivilege[IndexPrivilege.values().size()]); - IndexPrivilege first = values[randomIntBetween(0, values.length-1)]; - IndexPrivilege second = values[randomIntBetween(0, values.length-1)]; + IndexPrivilege first = values[randomIntBetween(0, values.length - 1)]; + IndexPrivilege second = values[randomIntBetween(0, values.length - 1)]; Set name = Sets.newHashSet(first.name().iterator().next(), second.name().iterator().next()); IndexPrivilege index = IndexPrivilege.get(name); @@ -181,8 +191,12 @@ public void testManageAutoscalingPrivilege() { } public void testManageCcrPrivilege() { - verifyClusterActionAllowed(ClusterPrivilegeResolver.MANAGE_CCR, "cluster:admin/xpack/ccr/follow_index", - "cluster:admin/xpack/ccr/unfollow_index", "cluster:admin/xpack/ccr/brand_new_api"); + verifyClusterActionAllowed( + ClusterPrivilegeResolver.MANAGE_CCR, + "cluster:admin/xpack/ccr/follow_index", + "cluster:admin/xpack/ccr/unfollow_index", + "cluster:admin/xpack/ccr/brand_new_api" + ); verifyClusterActionDenied(ClusterPrivilegeResolver.MANAGE_CCR, "cluster:admin/xpack/whatever"); } @@ -198,9 +212,15 @@ public void testManageEnrichPrivilege() { public void testIlmPrivileges() { { - verifyClusterActionAllowed(ClusterPrivilegeResolver.MANAGE_ILM, "cluster:admin/ilm/delete", - "cluster:admin/ilm/_move/post", "cluster:admin/ilm/put", "cluster:admin/ilm/start", - "cluster:admin/ilm/stop", "cluster:admin/ilm/brand_new_api", "cluster:admin/ilm/get", + verifyClusterActionAllowed( + ClusterPrivilegeResolver.MANAGE_ILM, + "cluster:admin/ilm/delete", + "cluster:admin/ilm/_move/post", + "cluster:admin/ilm/put", + "cluster:admin/ilm/start", + "cluster:admin/ilm/stop", + "cluster:admin/ilm/brand_new_api", + "cluster:admin/ilm/get", "cluster:admin/ilm/operation_mode/get" ); verifyClusterActionDenied(ClusterPrivilegeResolver.MANAGE_ILM, "cluster:admin/whatever"); @@ -209,9 +229,16 @@ public void testIlmPrivileges() { { verifyClusterActionAllowed(ClusterPrivilegeResolver.READ_ILM, "cluster:admin/ilm/get", "cluster:admin/ilm/operation_mode/get"); - verifyClusterActionDenied(ClusterPrivilegeResolver.READ_ILM, "cluster:admin/ilm/delete", "cluster:admin/ilm/_move/post", - "cluster:admin/ilm/put", "cluster:admin/ilm/start", "cluster:admin/ilm/stop", - "cluster:admin/ilm/brand_new_api", "cluster:admin/whatever"); + verifyClusterActionDenied( + ClusterPrivilegeResolver.READ_ILM, + "cluster:admin/ilm/delete", + "cluster:admin/ilm/_move/post", + "cluster:admin/ilm/put", + "cluster:admin/ilm/start", + "cluster:admin/ilm/stop", + "cluster:admin/ilm/brand_new_api", + "cluster:admin/whatever" + ); } { @@ -239,46 +266,58 @@ public void testIlmPrivileges() { public void testSlmPrivileges() { { - verifyClusterActionAllowed(ClusterPrivilegeResolver.MANAGE_SLM, "cluster:admin/slm/delete", + verifyClusterActionAllowed( + ClusterPrivilegeResolver.MANAGE_SLM, + "cluster:admin/slm/delete", "cluster:admin/slm/put", "cluster:admin/slm/get", "cluster:admin/ilm/start", "cluster:admin/ilm/stop", "cluster:admin/slm/execute", - "cluster:admin/ilm/operation_mode/get"); + "cluster:admin/ilm/operation_mode/get" + ); verifyClusterActionDenied(ClusterPrivilegeResolver.MANAGE_SLM, "cluster:admin/whatever"); } { - verifyClusterActionAllowed(ClusterPrivilegeResolver.READ_SLM, - "cluster:admin/slm/get", - "cluster:admin/ilm/operation_mode/get"); - verifyClusterActionDenied(ClusterPrivilegeResolver.READ_SLM,"cluster:admin/slm/delete", + verifyClusterActionAllowed(ClusterPrivilegeResolver.READ_SLM, "cluster:admin/slm/get", "cluster:admin/ilm/operation_mode/get"); + verifyClusterActionDenied( + ClusterPrivilegeResolver.READ_SLM, + "cluster:admin/slm/delete", "cluster:admin/slm/put", "cluster:admin/ilm/start", "cluster:admin/ilm/stop", "cluster:admin/slm/execute", - "cluster:admin/whatever"); + "cluster:admin/whatever" + ); } } public void testIngestPipelinePrivileges() { { - verifyClusterActionAllowed(ClusterPrivilegeResolver.MANAGE_INGEST_PIPELINES, "cluster:admin/ingest/pipeline/get", + verifyClusterActionAllowed( + ClusterPrivilegeResolver.MANAGE_INGEST_PIPELINES, + "cluster:admin/ingest/pipeline/get", "cluster:admin/ingest/pipeline/put", "cluster:admin/ingest/pipeline/delete", - "cluster:admin/ingest/pipeline/simulate"); + "cluster:admin/ingest/pipeline/simulate" + ); verifyClusterActionDenied(ClusterPrivilegeResolver.MANAGE_INGEST_PIPELINES, "cluster:admin/whatever"); } { - verifyClusterActionAllowed(ClusterPrivilegeResolver.READ_PIPELINE, + verifyClusterActionAllowed( + ClusterPrivilegeResolver.READ_PIPELINE, "cluster:admin/ingest/pipeline/get", - "cluster:admin/ingest/pipeline/simulate"); - verifyClusterActionDenied(ClusterPrivilegeResolver.READ_PIPELINE,"cluster:admin/ingest/pipeline/put", + "cluster:admin/ingest/pipeline/simulate" + ); + verifyClusterActionDenied( + ClusterPrivilegeResolver.READ_PIPELINE, + "cluster:admin/ingest/pipeline/put", "cluster:admin/ingest/pipeline/delete", - "cluster:admin/whatever"); + "cluster:admin/whatever" + ); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/store/ReservedRolesStoreTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/store/ReservedRolesStoreTests.java index b1d272378a0a8..0df7ef733f692 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/store/ReservedRolesStoreTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/store/ReservedRolesStoreTests.java @@ -91,13 +91,6 @@ import org.elasticsearch.xpack.core.ml.action.EvaluateDataFrameAction; import org.elasticsearch.xpack.core.ml.action.ExplainDataFrameAnalyticsAction; import org.elasticsearch.xpack.core.ml.action.FinalizeJobExecutionAction; -import org.elasticsearch.xpack.core.rollup.action.GetRollupIndexCapsAction; -import org.elasticsearch.xpack.core.security.action.CreateApiKeyAction; -import org.elasticsearch.xpack.core.security.action.CreateApiKeyRequest; -import org.elasticsearch.xpack.core.security.action.GetApiKeyRequest; -import org.elasticsearch.xpack.core.security.action.apikey.QueryApiKeyRequest; -import org.elasticsearch.xpack.core.security.authz.accesscontrol.IndicesAccessControl; -import org.elasticsearch.xpack.core.textstructure.action.FindStructureAction; import org.elasticsearch.xpack.core.ml.action.FlushJobAction; import org.elasticsearch.xpack.core.ml.action.ForecastJobAction; import org.elasticsearch.xpack.core.ml.action.GetBucketsAction; @@ -150,9 +143,14 @@ import org.elasticsearch.xpack.core.ml.job.persistence.AnomalyDetectorsIndexFields; import org.elasticsearch.xpack.core.ml.notifications.NotificationsIndex; import org.elasticsearch.xpack.core.monitoring.action.MonitoringBulkAction; +import org.elasticsearch.xpack.core.rollup.action.GetRollupIndexCapsAction; +import org.elasticsearch.xpack.core.security.action.CreateApiKeyAction; +import org.elasticsearch.xpack.core.security.action.CreateApiKeyRequest; import org.elasticsearch.xpack.core.security.action.DelegatePkiAuthenticationAction; +import org.elasticsearch.xpack.core.security.action.GetApiKeyRequest; import org.elasticsearch.xpack.core.security.action.GrantApiKeyAction; import org.elasticsearch.xpack.core.security.action.InvalidateApiKeyAction; +import org.elasticsearch.xpack.core.security.action.apikey.QueryApiKeyRequest; import org.elasticsearch.xpack.core.security.action.privilege.DeletePrivilegesAction; import org.elasticsearch.xpack.core.security.action.privilege.DeletePrivilegesRequest; import org.elasticsearch.xpack.core.security.action.privilege.GetBuiltinPrivilegesAction; @@ -168,6 +166,7 @@ import org.elasticsearch.xpack.core.security.action.user.PutUserAction; import org.elasticsearch.xpack.core.security.authc.Authentication; import org.elasticsearch.xpack.core.security.authz.RoleDescriptor; +import org.elasticsearch.xpack.core.security.authz.accesscontrol.IndicesAccessControl; import org.elasticsearch.xpack.core.security.authz.permission.FieldPermissionsCache; import org.elasticsearch.xpack.core.security.authz.permission.Role; import org.elasticsearch.xpack.core.security.authz.privilege.ApplicationPrivilege; @@ -180,6 +179,7 @@ import org.elasticsearch.xpack.core.security.user.RemoteMonitoringUser; import org.elasticsearch.xpack.core.security.user.SystemUser; import org.elasticsearch.xpack.core.security.user.XPackUser; +import org.elasticsearch.xpack.core.textstructure.action.FindStructureAction; import org.elasticsearch.xpack.core.transform.action.DeleteTransformAction; import org.elasticsearch.xpack.core.transform.action.GetTransformAction; import org.elasticsearch.xpack.core.transform.action.GetTransformStatsAction; @@ -296,26 +296,43 @@ public void testSnapshotUserRole() { assertThat(snapshotUserRole.cluster().check(WatcherServiceAction.NAME, request, authentication), is(false)); assertThat(snapshotUserRole.cluster().check(DelegatePkiAuthenticationAction.NAME, request, authentication), is(false)); - assertThat(snapshotUserRole.indices().allowedIndicesMatcher(IndexAction.NAME).test( - mockIndexAbstraction(randomAlphaOfLengthBetween(8, 24))), is(false)); - assertThat(snapshotUserRole.indices().allowedIndicesMatcher("indices:foo").test( - mockIndexAbstraction(randomAlphaOfLengthBetween(8, 24))), is(false)); - assertThat(snapshotUserRole.indices().allowedIndicesMatcher(GetAction.NAME).test( - mockIndexAbstraction(randomAlphaOfLengthBetween(8, 24))), is(false)); - assertThat(snapshotUserRole.indices().allowedIndicesMatcher(GetAction.NAME).test( - mockIndexAbstraction(randomAlphaOfLengthBetween(8, 24))), is(false)); + assertThat( + snapshotUserRole.indices() + .allowedIndicesMatcher(IndexAction.NAME) + .test(mockIndexAbstraction(randomAlphaOfLengthBetween(8, 24))), + is(false) + ); + assertThat( + snapshotUserRole.indices().allowedIndicesMatcher("indices:foo").test(mockIndexAbstraction(randomAlphaOfLengthBetween(8, 24))), + is(false) + ); + assertThat( + snapshotUserRole.indices().allowedIndicesMatcher(GetAction.NAME).test(mockIndexAbstraction(randomAlphaOfLengthBetween(8, 24))), + is(false) + ); + assertThat( + snapshotUserRole.indices().allowedIndicesMatcher(GetAction.NAME).test(mockIndexAbstraction(randomAlphaOfLengthBetween(8, 24))), + is(false) + ); - assertThat(snapshotUserRole.indices().allowedIndicesMatcher(GetIndexAction.NAME).test( - mockIndexAbstraction(randomAlphaOfLengthBetween(8, 24))), is(true)); + assertThat( + snapshotUserRole.indices() + .allowedIndicesMatcher(GetIndexAction.NAME) + .test(mockIndexAbstraction(randomAlphaOfLengthBetween(8, 24))), + is(true) + ); for (String index : RestrictedIndicesNames.RESTRICTED_NAMES) { // This test might cease to be true if we ever have non-security restricted names // but that depends on how users are supposed to perform snapshots of those new indices. - assertThat(snapshotUserRole.indices().allowedIndicesMatcher(GetIndexAction.NAME).test( - mockIndexAbstraction(index)), is(true)); + assertThat(snapshotUserRole.indices().allowedIndicesMatcher(GetIndexAction.NAME).test(mockIndexAbstraction(index)), is(true)); } - assertThat(snapshotUserRole.indices().allowedIndicesMatcher(GetIndexAction.NAME).test( - mockIndexAbstraction(XPackPlugin.ASYNC_RESULTS_INDEX + randomAlphaOfLengthBetween(0, 2))), is(true)); + assertThat( + snapshotUserRole.indices() + .allowedIndicesMatcher(GetIndexAction.NAME) + .test(mockIndexAbstraction(XPackPlugin.ASYNC_RESULTS_INDEX + randomAlphaOfLengthBetween(0, 2))), + is(true) + ); assertNoAccessAllowed(snapshotUserRole, RestrictedIndicesNames.RESTRICTED_NAMES); assertNoAccessAllowed(snapshotUserRole, XPackPlugin.ASYNC_RESULTS_INDEX + randomAlphaOfLengthBetween(0, 2)); @@ -342,10 +359,14 @@ public void testIngestAdminRole() { assertThat(ingestAdminRole.cluster().check(DelegatePkiAuthenticationAction.NAME, request, authentication), is(false)); assertThat(ingestAdminRole.indices().allowedIndicesMatcher(IndexAction.NAME).test(mockIndexAbstraction("foo")), is(false)); - assertThat(ingestAdminRole.indices().allowedIndicesMatcher("indices:foo").test( - mockIndexAbstraction(randomAlphaOfLengthBetween(8, 24))), is(false)); - assertThat(ingestAdminRole.indices().allowedIndicesMatcher(GetAction.NAME).test( - mockIndexAbstraction(randomAlphaOfLengthBetween(8, 24))), is(false)); + assertThat( + ingestAdminRole.indices().allowedIndicesMatcher("indices:foo").test(mockIndexAbstraction(randomAlphaOfLengthBetween(8, 24))), + is(false) + ); + assertThat( + ingestAdminRole.indices().allowedIndicesMatcher(GetAction.NAME).test(mockIndexAbstraction(randomAlphaOfLengthBetween(8, 24))), + is(false) + ); assertNoAccessAllowed(ingestAdminRole, RestrictedIndicesNames.RESTRICTED_NAMES); assertNoAccessAllowed(ingestAdminRole, XPackPlugin.ASYNC_RESULTS_INDEX + randomAlphaOfLengthBetween(0, 2)); @@ -382,9 +403,10 @@ public void testKibanaSystemRole() { assertThat(kibanaRole.cluster().check(CreateApiKeyAction.NAME, createApiKeyRequest, authentication), is(true)); // Can only get and query its own API keys assertThat(kibanaRole.cluster().check(CreateApiKeyAction.NAME, new GetApiKeyRequest(), authentication), is(false)); - assertThat(kibanaRole.cluster().check(CreateApiKeyAction.NAME, - new GetApiKeyRequest(null, null, null, null, true), authentication), - is(true)); + assertThat( + kibanaRole.cluster().check(CreateApiKeyAction.NAME, new GetApiKeyRequest(null, null, null, null, true), authentication), + is(true) + ); final QueryApiKeyRequest queryApiKeyRequest = new QueryApiKeyRequest(); assertThat(kibanaRole.cluster().check(CreateApiKeyAction.NAME, queryApiKeyRequest, authentication), is(false)); queryApiKeyRequest.setFilterForCurrentUser(); @@ -397,8 +419,8 @@ public void testKibanaSystemRole() { assertThat(kibanaRole.cluster().check(FindStructureAction.NAME, request, authentication), is(true)); // Application Privileges - DeletePrivilegesRequest deleteKibanaPrivileges = new DeletePrivilegesRequest("kibana-.kibana", new String[]{ "all", "read" }); - DeletePrivilegesRequest deleteLogstashPrivileges = new DeletePrivilegesRequest("logstash", new String[]{ "all", "read" }); + DeletePrivilegesRequest deleteKibanaPrivileges = new DeletePrivilegesRequest("kibana-.kibana", new String[] { "all", "read" }); + DeletePrivilegesRequest deleteLogstashPrivileges = new DeletePrivilegesRequest("logstash", new String[] { "all", "read" }); assertThat(kibanaRole.cluster().check(DeletePrivilegesAction.NAME, deleteKibanaPrivileges, authentication), is(true)); assertThat(kibanaRole.cluster().check(DeletePrivilegesAction.NAME, deleteLogstashPrivileges, authentication), is(false)); @@ -410,11 +432,22 @@ public void testKibanaSystemRole() { assertThat(kibanaRole.cluster().check(GetPrivilegesAction.NAME, getApmPrivileges, authentication), is(false)); PutPrivilegesRequest putKibanaPrivileges = new PutPrivilegesRequest(); - putKibanaPrivileges.setPrivileges(Collections.singletonList(new ApplicationPrivilegeDescriptor( - "kibana-.kibana-" + randomAlphaOfLengthBetween(2,6), "all", Collections.emptySet(), Collections.emptyMap()))); + putKibanaPrivileges.setPrivileges( + Collections.singletonList( + new ApplicationPrivilegeDescriptor( + "kibana-.kibana-" + randomAlphaOfLengthBetween(2, 6), + "all", + Collections.emptySet(), + Collections.emptyMap() + ) + ) + ); PutPrivilegesRequest putSwiftypePrivileges = new PutPrivilegesRequest(); - putSwiftypePrivileges.setPrivileges(Collections.singletonList(new ApplicationPrivilegeDescriptor( - "swiftype-kibana" , "all", Collections.emptySet(), Collections.emptyMap()))); + putSwiftypePrivileges.setPrivileges( + Collections.singletonList( + new ApplicationPrivilegeDescriptor("swiftype-kibana", "all", Collections.emptySet(), Collections.emptyMap()) + ) + ); assertThat(kibanaRole.cluster().check(PutPrivilegesAction.NAME, putKibanaPrivileges, authentication), is(true)); assertThat(kibanaRole.cluster().check(PutPrivilegesAction.NAME, putSwiftypePrivileges, authentication), is(false)); @@ -426,8 +459,10 @@ public void testKibanaSystemRole() { assertThat(kibanaRole.indices().allowedIndicesMatcher(IndexAction.NAME).test(mockIndexAbstraction("foo")), is(false)); assertThat(kibanaRole.indices().allowedIndicesMatcher(IndexAction.NAME).test(mockIndexAbstraction(".reporting")), is(false)); - assertThat(kibanaRole.indices().allowedIndicesMatcher("indices:foo").test(mockIndexAbstraction(randomAlphaOfLengthBetween(8, 24))), - is(false)); + assertThat( + kibanaRole.indices().allowedIndicesMatcher("indices:foo").test(mockIndexAbstraction(randomAlphaOfLengthBetween(8, 24))), + is(false) + ); Arrays.asList( ".kibana", @@ -536,7 +571,6 @@ public void testKibanaSystemRole() { ".fleet-servers" ).forEach(index -> assertAllIndicesAccessAllowed(kibanaRole, index)); - // Data telemetry reads mappings, metadata and stats of indices Arrays.asList(randomAlphaOfLengthBetween(8, 24), "packetbeat-*").forEach((index) -> { logger.info("index name [{}]", index); @@ -672,7 +706,8 @@ public void testKibanaSystemRole() { assertViewIndexMetadata(kibanaRole, indexName); assertThat( kibanaRole.indices().allowedIndicesMatcher("indices:monitor/" + randomAlphaOfLengthBetween(3, 8)).test(indexAbstraction), - is(true)); + is(true) + ); assertThat(kibanaRole.indices().allowedIndicesMatcher(CreateIndexAction.NAME).test(indexAbstraction), is(false)); assertThat(kibanaRole.indices().allowedIndicesMatcher(AutoCreateAction.NAME).test(indexAbstraction), is(false)); @@ -734,11 +769,14 @@ public void testKibanaSystemRole() { assertViewIndexMetadata(kibanaRole, indexName); assertThat( kibanaRole.indices().allowedIndicesMatcher("indices:monitor/" + randomAlphaOfLengthBetween(3, 8)).test(indexAbstraction), - is(true)); + is(true) + ); // Granted by bwc for index privilege - assertThat(kibanaRole.indices().allowedIndicesMatcher(PutMappingAction.NAME).test(indexAbstraction), - is(indexAbstraction.getType() != IndexAbstraction.Type.DATA_STREAM)); + assertThat( + kibanaRole.indices().allowedIndicesMatcher(PutMappingAction.NAME).test(indexAbstraction), + is(indexAbstraction.getType() != IndexAbstraction.Type.DATA_STREAM) + ); // Deny deleting documents and modifying the index settings assertThat(kibanaRole.indices().allowedIndicesMatcher(DeleteAction.NAME).test(indexAbstraction), is(false)); @@ -762,34 +800,31 @@ public void testKibanaAdminRole() { assertThat(kibanaAdminRole.cluster().check(ClusterStatsAction.NAME, request, authentication), is(false)); assertThat(kibanaAdminRole.cluster().check(PutIndexTemplateAction.NAME, request, authentication), is(false)); assertThat(kibanaAdminRole.cluster().check(ClusterRerouteAction.NAME, request, authentication), is(false)); - assertThat(kibanaAdminRole.cluster().check(ClusterUpdateSettingsAction.NAME, request, authentication), - is(false)); + assertThat(kibanaAdminRole.cluster().check(ClusterUpdateSettingsAction.NAME, request, authentication), is(false)); assertThat(kibanaAdminRole.cluster().check(MonitoringBulkAction.NAME, request, authentication), is(false)); - assertThat(kibanaAdminRole.cluster().check(DelegatePkiAuthenticationAction.NAME, request, authentication), - is(false)); + assertThat(kibanaAdminRole.cluster().check(DelegatePkiAuthenticationAction.NAME, request, authentication), is(false)); assertThat(kibanaAdminRole.runAs().check(randomAlphaOfLengthBetween(1, 12)), is(false)); assertThat(kibanaAdminRole.indices().allowedIndicesMatcher(IndexAction.NAME).test(mockIndexAbstraction("foo")), is(false)); assertThat(kibanaAdminRole.indices().allowedIndicesMatcher(IndexAction.NAME).test(mockIndexAbstraction(".reporting")), is(false)); - assertThat(kibanaAdminRole.indices().allowedIndicesMatcher("indices:foo").test( - mockIndexAbstraction(randomAlphaOfLengthBetween(8, 24))), is(false)); + assertThat( + kibanaAdminRole.indices().allowedIndicesMatcher("indices:foo").test(mockIndexAbstraction(randomAlphaOfLengthBetween(8, 24))), + is(false) + ); final String randomApplication = "kibana-" + randomAlphaOfLengthBetween(8, 24); - assertThat(kibanaAdminRole.application().grants(new ApplicationPrivilege(randomApplication, "app-random", "all"), - "*"), is(false)); + assertThat(kibanaAdminRole.application().grants(new ApplicationPrivilege(randomApplication, "app-random", "all"), "*"), is(false)); final String application = "kibana-.kibana"; - assertThat(kibanaAdminRole.application().grants(new ApplicationPrivilege(application, "app-foo", "foo"), "*"), - is(false)); - assertThat(kibanaAdminRole.application().grants(new ApplicationPrivilege(application, "app-all", "all"), "*"), - is(true)); + assertThat(kibanaAdminRole.application().grants(new ApplicationPrivilege(application, "app-foo", "foo"), "*"), is(false)); + assertThat(kibanaAdminRole.application().grants(new ApplicationPrivilege(application, "app-all", "all"), "*"), is(true)); final String applicationWithRandomIndex = "kibana-.kibana_" + randomAlphaOfLengthBetween(8, 24); assertThat( - kibanaAdminRole.application() - .grants(new ApplicationPrivilege(applicationWithRandomIndex, "app-random-index", "all"), "*"), - is(false)); + kibanaAdminRole.application().grants(new ApplicationPrivilege(applicationWithRandomIndex, "app-random-index", "all"), "*"), + is(false) + ); assertNoAccessAllowed(kibanaAdminRole, RestrictedIndicesNames.RESTRICTED_NAMES); } @@ -817,8 +852,10 @@ public void testKibanaUserRole() { assertThat(kibanaUserRole.indices().allowedIndicesMatcher(IndexAction.NAME).test(mockIndexAbstraction("foo")), is(false)); assertThat(kibanaUserRole.indices().allowedIndicesMatcher(IndexAction.NAME).test(mockIndexAbstraction(".reporting")), is(false)); - assertThat(kibanaUserRole.indices().allowedIndicesMatcher("indices:foo") - .test(mockIndexAbstraction(randomAlphaOfLengthBetween(8, 24))), is(false)); + assertThat( + kibanaUserRole.indices().allowedIndicesMatcher("indices:foo").test(mockIndexAbstraction(randomAlphaOfLengthBetween(8, 24))), + is(false) + ); final String randomApplication = "kibana-" + randomAlphaOfLengthBetween(8, 24); assertThat(kibanaUserRole.application().grants(new ApplicationPrivilege(randomApplication, "app-random", "all"), "*"), is(false)); @@ -828,8 +865,10 @@ public void testKibanaUserRole() { assertThat(kibanaUserRole.application().grants(new ApplicationPrivilege(application, "app-all", "all"), "*"), is(true)); final String applicationWithRandomIndex = "kibana-.kibana_" + randomAlphaOfLengthBetween(8, 24); - assertThat(kibanaUserRole.application().grants(new ApplicationPrivilege(applicationWithRandomIndex, "app-random-index", "all"), - "*"), is(false)); + assertThat( + kibanaUserRole.application().grants(new ApplicationPrivilege(applicationWithRandomIndex, "app-random-index", "all"), "*"), + is(false) + ); assertNoAccessAllowed(kibanaUserRole, RestrictedIndicesNames.RESTRICTED_NAMES); assertNoAccessAllowed(kibanaUserRole, XPackPlugin.ASYNC_RESULTS_INDEX + randomAlphaOfLengthBetween(0, 2)); @@ -859,18 +898,27 @@ public void testMonitoringUserRole() { assertThat(monitoringUserRole.runAs().check(randomAlphaOfLengthBetween(1, 12)), is(false)); assertThat(monitoringUserRole.indices().allowedIndicesMatcher(SearchAction.NAME).test(mockIndexAbstraction("foo")), is(false)); - assertThat(monitoringUserRole.indices().allowedIndicesMatcher(SearchAction.NAME).test(mockIndexAbstraction(".reporting")), - is(false)); - assertThat(monitoringUserRole.indices().allowedIndicesMatcher(SearchAction.NAME).test(mockIndexAbstraction(".kibana")), - is(false)); - assertThat(monitoringUserRole.indices().allowedIndicesMatcher("indices:foo").test( - mockIndexAbstraction(randomAlphaOfLengthBetween(8, 24))), is(false)); - assertThat(monitoringUserRole.indices().allowedIndicesMatcher(READ_CROSS_CLUSTER_NAME).test(mockIndexAbstraction("foo")), - is(false)); - assertThat(monitoringUserRole.indices().allowedIndicesMatcher(READ_CROSS_CLUSTER_NAME).test(mockIndexAbstraction(".reporting")), - is(false)); - assertThat(monitoringUserRole.indices().allowedIndicesMatcher(READ_CROSS_CLUSTER_NAME).test(mockIndexAbstraction(".kibana")), - is(false)); + assertThat( + monitoringUserRole.indices().allowedIndicesMatcher(SearchAction.NAME).test(mockIndexAbstraction(".reporting")), + is(false) + ); + assertThat(monitoringUserRole.indices().allowedIndicesMatcher(SearchAction.NAME).test(mockIndexAbstraction(".kibana")), is(false)); + assertThat( + monitoringUserRole.indices().allowedIndicesMatcher("indices:foo").test(mockIndexAbstraction(randomAlphaOfLengthBetween(8, 24))), + is(false) + ); + assertThat( + monitoringUserRole.indices().allowedIndicesMatcher(READ_CROSS_CLUSTER_NAME).test(mockIndexAbstraction("foo")), + is(false) + ); + assertThat( + monitoringUserRole.indices().allowedIndicesMatcher(READ_CROSS_CLUSTER_NAME).test(mockIndexAbstraction(".reporting")), + is(false) + ); + assertThat( + monitoringUserRole.indices().allowedIndicesMatcher(READ_CROSS_CLUSTER_NAME).test(mockIndexAbstraction(".kibana")), + is(false) + ); final String index = ".monitoring-" + randomAlphaOfLength(randomIntBetween(0, 13)); assertThat(monitoringUserRole.indices().allowedIndicesMatcher("indices:foo").test(mockIndexAbstraction(index)), is(false)); @@ -879,8 +927,10 @@ public void testMonitoringUserRole() { assertThat(monitoringUserRole.indices().allowedIndicesMatcher(CreateIndexAction.NAME).test(mockIndexAbstraction(index)), is(false)); assertThat(monitoringUserRole.indices().allowedIndicesMatcher(IndexAction.NAME).test(mockIndexAbstraction(index)), is(false)); assertThat(monitoringUserRole.indices().allowedIndicesMatcher(DeleteAction.NAME).test(mockIndexAbstraction(index)), is(false)); - assertThat(monitoringUserRole.indices().allowedIndicesMatcher(UpdateSettingsAction.NAME) - .test(mockIndexAbstraction(index)), is(false)); + assertThat( + monitoringUserRole.indices().allowedIndicesMatcher(UpdateSettingsAction.NAME).test(mockIndexAbstraction(index)), + is(false) + ); assertThat(monitoringUserRole.indices().allowedIndicesMatcher(SearchAction.NAME).test(mockIndexAbstraction(index)), is(true)); assertThat(monitoringUserRole.indices().allowedIndicesMatcher(GetAction.NAME).test(mockIndexAbstraction(index)), is(true)); assertThat(monitoringUserRole.indices().allowedIndicesMatcher(READ_CROSS_CLUSTER_NAME).test(mockIndexAbstraction(index)), is(true)); @@ -889,16 +939,23 @@ public void testMonitoringUserRole() { assertNoAccessAllowed(monitoringUserRole, XPackPlugin.ASYNC_RESULTS_INDEX + randomAlphaOfLengthBetween(0, 2)); final String kibanaApplicationWithRandomIndex = "kibana-" + randomFrom(randomAlphaOfLengthBetween(8, 24), ".kibana"); - assertThat(monitoringUserRole.application().grants( - new ApplicationPrivilege(kibanaApplicationWithRandomIndex, "app-foo", "foo"), "*"), is(false)); - assertThat(monitoringUserRole.application().grants( - new ApplicationPrivilege(kibanaApplicationWithRandomIndex, "app-reserved_monitoring", "reserved_monitoring"), "*"), is(true)); + assertThat( + monitoringUserRole.application().grants(new ApplicationPrivilege(kibanaApplicationWithRandomIndex, "app-foo", "foo"), "*"), + is(false) + ); + assertThat( + monitoringUserRole.application() + .grants(new ApplicationPrivilege(kibanaApplicationWithRandomIndex, "app-reserved_monitoring", "reserved_monitoring"), "*"), + is(true) + ); final String otherApplication = "logstash-" + randomAlphaOfLengthBetween(8, 24); - assertThat(monitoringUserRole.application().grants( - new ApplicationPrivilege(otherApplication, "app-foo", "foo"), "*"), is(false)); - assertThat(monitoringUserRole.application().grants( - new ApplicationPrivilege(otherApplication, "app-reserved_monitoring", "reserved_monitoring"), "*"), is(false)); + assertThat(monitoringUserRole.application().grants(new ApplicationPrivilege(otherApplication, "app-foo", "foo"), "*"), is(false)); + assertThat( + monitoringUserRole.application() + .grants(new ApplicationPrivilege(otherApplication, "app-reserved_monitoring", "reserved_monitoring"), "*"), + is(false) + ); } public void testRemoteMonitoringAgentRole() { @@ -934,66 +991,134 @@ public void testRemoteMonitoringAgentRole() { assertThat(remoteMonitoringAgentRole.runAs().check(randomAlphaOfLengthBetween(1, 12)), is(false)); - assertThat(remoteMonitoringAgentRole.indices().allowedIndicesMatcher(SearchAction.NAME).test(mockIndexAbstraction("foo")), - is(false)); - assertThat(remoteMonitoringAgentRole.indices().allowedIndicesMatcher(SearchAction.NAME).test(mockIndexAbstraction(".reporting")), - is(false)); - assertThat(remoteMonitoringAgentRole.indices().allowedIndicesMatcher(SearchAction.NAME).test(mockIndexAbstraction(".kibana")), - is(false)); - assertThat(remoteMonitoringAgentRole.indices().allowedIndicesMatcher("indices:foo") - .test(mockIndexAbstraction(randomAlphaOfLengthBetween(8, 24))), is(false)); + assertThat( + remoteMonitoringAgentRole.indices().allowedIndicesMatcher(SearchAction.NAME).test(mockIndexAbstraction("foo")), + is(false) + ); + assertThat( + remoteMonitoringAgentRole.indices().allowedIndicesMatcher(SearchAction.NAME).test(mockIndexAbstraction(".reporting")), + is(false) + ); + assertThat( + remoteMonitoringAgentRole.indices().allowedIndicesMatcher(SearchAction.NAME).test(mockIndexAbstraction(".kibana")), + is(false) + ); + assertThat( + remoteMonitoringAgentRole.indices() + .allowedIndicesMatcher("indices:foo") + .test(mockIndexAbstraction(randomAlphaOfLengthBetween(8, 24))), + is(false) + ); final String monitoringIndex = ".monitoring-" + randomAlphaOfLength(randomIntBetween(0, 13)); - assertThat(remoteMonitoringAgentRole.indices().allowedIndicesMatcher("indices:foo").test(mockIndexAbstraction(monitoringIndex)), - is(true)); - assertThat(remoteMonitoringAgentRole.indices().allowedIndicesMatcher("indices:bar").test(mockIndexAbstraction(monitoringIndex)), - is(true)); - assertThat(remoteMonitoringAgentRole.indices().allowedIndicesMatcher(DeleteIndexAction.NAME) - .test(mockIndexAbstraction(monitoringIndex)), is(true)); - assertThat(remoteMonitoringAgentRole.indices().allowedIndicesMatcher(CreateIndexAction.NAME) - .test(mockIndexAbstraction(monitoringIndex)), is(true)); - assertThat(remoteMonitoringAgentRole.indices().allowedIndicesMatcher(IndexAction.NAME) - .test(mockIndexAbstraction(monitoringIndex)), is(true)); - assertThat(remoteMonitoringAgentRole.indices().allowedIndicesMatcher(DeleteAction.NAME) - .test(mockIndexAbstraction(monitoringIndex)), is(true)); - assertThat(remoteMonitoringAgentRole.indices().allowedIndicesMatcher(UpdateSettingsAction.NAME) - .test(mockIndexAbstraction(monitoringIndex)), is(true)); - assertThat(remoteMonitoringAgentRole.indices().allowedIndicesMatcher(SearchAction.NAME) - .test(mockIndexAbstraction(monitoringIndex)), is(true)); - assertThat(remoteMonitoringAgentRole.indices().allowedIndicesMatcher(GetAction.NAME) - .test(mockIndexAbstraction(monitoringIndex)), is(true)); - assertThat(remoteMonitoringAgentRole.indices().allowedIndicesMatcher(GetIndexAction.NAME) - .test(mockIndexAbstraction(monitoringIndex)), is(true)); + assertThat( + remoteMonitoringAgentRole.indices().allowedIndicesMatcher("indices:foo").test(mockIndexAbstraction(monitoringIndex)), + is(true) + ); + assertThat( + remoteMonitoringAgentRole.indices().allowedIndicesMatcher("indices:bar").test(mockIndexAbstraction(monitoringIndex)), + is(true) + ); + assertThat( + remoteMonitoringAgentRole.indices().allowedIndicesMatcher(DeleteIndexAction.NAME).test(mockIndexAbstraction(monitoringIndex)), + is(true) + ); + assertThat( + remoteMonitoringAgentRole.indices().allowedIndicesMatcher(CreateIndexAction.NAME).test(mockIndexAbstraction(monitoringIndex)), + is(true) + ); + assertThat( + remoteMonitoringAgentRole.indices().allowedIndicesMatcher(IndexAction.NAME).test(mockIndexAbstraction(monitoringIndex)), + is(true) + ); + assertThat( + remoteMonitoringAgentRole.indices().allowedIndicesMatcher(DeleteAction.NAME).test(mockIndexAbstraction(monitoringIndex)), + is(true) + ); + assertThat( + remoteMonitoringAgentRole.indices() + .allowedIndicesMatcher(UpdateSettingsAction.NAME) + .test(mockIndexAbstraction(monitoringIndex)), + is(true) + ); + assertThat( + remoteMonitoringAgentRole.indices().allowedIndicesMatcher(SearchAction.NAME).test(mockIndexAbstraction(monitoringIndex)), + is(true) + ); + assertThat( + remoteMonitoringAgentRole.indices().allowedIndicesMatcher(GetAction.NAME).test(mockIndexAbstraction(monitoringIndex)), + is(true) + ); + assertThat( + remoteMonitoringAgentRole.indices().allowedIndicesMatcher(GetIndexAction.NAME).test(mockIndexAbstraction(monitoringIndex)), + is(true) + ); final String metricbeatIndex = "metricbeat-" + randomAlphaOfLength(randomIntBetween(0, 13)); - assertThat(remoteMonitoringAgentRole.indices().allowedIndicesMatcher("indices:foo") - .test(mockIndexAbstraction(metricbeatIndex)), is(false)); - assertThat(remoteMonitoringAgentRole.indices().allowedIndicesMatcher("indices:bar") - .test(mockIndexAbstraction(metricbeatIndex)), is(false)); - assertThat(remoteMonitoringAgentRole.indices().allowedIndicesMatcher(DeleteIndexAction.NAME) - .test(mockIndexAbstraction(metricbeatIndex)), is(false)); - assertThat(remoteMonitoringAgentRole.indices().allowedIndicesMatcher(CreateIndexAction.NAME) - .test(mockIndexAbstraction(metricbeatIndex)), is(true)); - assertThat(remoteMonitoringAgentRole.indices().allowedIndicesMatcher(IndexAction.NAME) - .test(mockIndexAbstraction(metricbeatIndex)), is(true)); - assertThat(remoteMonitoringAgentRole.indices().allowedIndicesMatcher(GetIndexAction.NAME) - .test(mockIndexAbstraction(metricbeatIndex)), is(true)); - assertThat(remoteMonitoringAgentRole.indices().allowedIndicesMatcher(GetAliasesAction.NAME) - .test(mockIndexAbstraction(metricbeatIndex)), is(true)); - assertThat(remoteMonitoringAgentRole.indices().allowedIndicesMatcher(IndicesAliasesAction.NAME) - .test(mockIndexAbstraction(metricbeatIndex)), is(true)); - assertThat(remoteMonitoringAgentRole.indices().allowedIndicesMatcher(IndicesSegmentsAction.NAME) - .test(mockIndexAbstraction(metricbeatIndex)), is(false)); - assertThat(remoteMonitoringAgentRole.indices().allowedIndicesMatcher(RemoveIndexLifecyclePolicyAction.NAME) - .test(mockIndexAbstraction(metricbeatIndex)), is(false)); - assertThat(remoteMonitoringAgentRole.indices().allowedIndicesMatcher(DeleteAction.NAME) - .test(mockIndexAbstraction(metricbeatIndex)), is(false)); - assertThat(remoteMonitoringAgentRole.indices().allowedIndicesMatcher(UpdateSettingsAction.NAME) - .test(mockIndexAbstraction(metricbeatIndex)), is(false)); - assertThat(remoteMonitoringAgentRole.indices().allowedIndicesMatcher(SearchAction.NAME) - .test(mockIndexAbstraction(metricbeatIndex)), is(false)); - assertThat(remoteMonitoringAgentRole.indices().allowedIndicesMatcher(GetAction.NAME) - .test(mockIndexAbstraction(metricbeatIndex)), is(false)); + assertThat( + remoteMonitoringAgentRole.indices().allowedIndicesMatcher("indices:foo").test(mockIndexAbstraction(metricbeatIndex)), + is(false) + ); + assertThat( + remoteMonitoringAgentRole.indices().allowedIndicesMatcher("indices:bar").test(mockIndexAbstraction(metricbeatIndex)), + is(false) + ); + assertThat( + remoteMonitoringAgentRole.indices().allowedIndicesMatcher(DeleteIndexAction.NAME).test(mockIndexAbstraction(metricbeatIndex)), + is(false) + ); + assertThat( + remoteMonitoringAgentRole.indices().allowedIndicesMatcher(CreateIndexAction.NAME).test(mockIndexAbstraction(metricbeatIndex)), + is(true) + ); + assertThat( + remoteMonitoringAgentRole.indices().allowedIndicesMatcher(IndexAction.NAME).test(mockIndexAbstraction(metricbeatIndex)), + is(true) + ); + assertThat( + remoteMonitoringAgentRole.indices().allowedIndicesMatcher(GetIndexAction.NAME).test(mockIndexAbstraction(metricbeatIndex)), + is(true) + ); + assertThat( + remoteMonitoringAgentRole.indices().allowedIndicesMatcher(GetAliasesAction.NAME).test(mockIndexAbstraction(metricbeatIndex)), + is(true) + ); + assertThat( + remoteMonitoringAgentRole.indices() + .allowedIndicesMatcher(IndicesAliasesAction.NAME) + .test(mockIndexAbstraction(metricbeatIndex)), + is(true) + ); + assertThat( + remoteMonitoringAgentRole.indices() + .allowedIndicesMatcher(IndicesSegmentsAction.NAME) + .test(mockIndexAbstraction(metricbeatIndex)), + is(false) + ); + assertThat( + remoteMonitoringAgentRole.indices() + .allowedIndicesMatcher(RemoveIndexLifecyclePolicyAction.NAME) + .test(mockIndexAbstraction(metricbeatIndex)), + is(false) + ); + assertThat( + remoteMonitoringAgentRole.indices().allowedIndicesMatcher(DeleteAction.NAME).test(mockIndexAbstraction(metricbeatIndex)), + is(false) + ); + assertThat( + remoteMonitoringAgentRole.indices() + .allowedIndicesMatcher(UpdateSettingsAction.NAME) + .test(mockIndexAbstraction(metricbeatIndex)), + is(false) + ); + assertThat( + remoteMonitoringAgentRole.indices().allowedIndicesMatcher(SearchAction.NAME).test(mockIndexAbstraction(metricbeatIndex)), + is(false) + ); + assertThat( + remoteMonitoringAgentRole.indices().allowedIndicesMatcher(GetAction.NAME).test(mockIndexAbstraction(metricbeatIndex)), + is(false) + ); assertNoAccessAllowed(remoteMonitoringAgentRole, RestrictedIndicesNames.RESTRICTED_NAMES); assertNoAccessAllowed(remoteMonitoringAgentRole, XPackPlugin.ASYNC_RESULTS_INDEX + randomAlphaOfLengthBetween(0, 2)); @@ -1021,91 +1146,205 @@ public void testRemoteMonitoringCollectorRole() { assertThat(remoteMonitoringCollectorRole.runAs().check(randomAlphaOfLengthBetween(1, 12)), is(false)); - assertThat(remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(RecoveryAction.NAME) - .test(mockIndexAbstraction("foo")), is(true)); - assertThat(remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(SearchAction.NAME) - .test(mockIndexAbstraction("foo")), is(false)); - assertThat(remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(SearchAction.NAME) - .test(mockIndexAbstraction(".reporting")), is(false)); - assertThat(remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(SearchAction.NAME) - .test(mockIndexAbstraction(".kibana")), is(true)); - assertThat(remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(GetAction.NAME) - .test(mockIndexAbstraction(".kibana")), is(true)); - assertThat(remoteMonitoringCollectorRole.indices().allowedIndicesMatcher("indices:foo") - .test(mockIndexAbstraction(randomAlphaOfLengthBetween(8, 24))), is(false)); + assertThat( + remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(RecoveryAction.NAME).test(mockIndexAbstraction("foo")), + is(true) + ); + assertThat( + remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(SearchAction.NAME).test(mockIndexAbstraction("foo")), + is(false) + ); + assertThat( + remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(SearchAction.NAME).test(mockIndexAbstraction(".reporting")), + is(false) + ); + assertThat( + remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(SearchAction.NAME).test(mockIndexAbstraction(".kibana")), + is(true) + ); + assertThat( + remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(GetAction.NAME).test(mockIndexAbstraction(".kibana")), + is(true) + ); + assertThat( + remoteMonitoringCollectorRole.indices() + .allowedIndicesMatcher("indices:foo") + .test(mockIndexAbstraction(randomAlphaOfLengthBetween(8, 24))), + is(false) + ); Arrays.asList( ".monitoring-" + randomAlphaOfLength(randomIntBetween(0, 13)), "metricbeat-" + randomAlphaOfLength(randomIntBetween(0, 13)) ).forEach((index) -> { logger.info("index name [{}]", index); - assertThat(remoteMonitoringCollectorRole.indices().allowedIndicesMatcher("indices:foo") - .test(mockIndexAbstraction(index)), is(false)); - assertThat(remoteMonitoringCollectorRole.indices().allowedIndicesMatcher("indices:bar") - .test(mockIndexAbstraction(index)), is(false)); - assertThat(remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(DeleteIndexAction.NAME) - .test(mockIndexAbstraction(index)), is(false)); - assertThat(remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(CreateIndexAction.NAME) - .test(mockIndexAbstraction(index)), is(false)); - assertThat(remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(IndexAction.NAME) - .test(mockIndexAbstraction(index)), is(false)); - assertThat(remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(GetIndexAction.NAME) - .test(mockIndexAbstraction(index)), is(false)); - assertThat(remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(GetAliasesAction.NAME) - .test(mockIndexAbstraction(index)), is(false)); - assertThat(remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(RemoveIndexLifecyclePolicyAction.NAME) - .test(mockIndexAbstraction(index)), is(false)); - assertThat(remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(DeleteAction.NAME) - .test(mockIndexAbstraction(index)), is(false)); - assertThat(remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(UpdateSettingsAction.NAME) - .test(mockIndexAbstraction(index)), is(false)); - assertThat(remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(SearchAction.NAME) - .test(mockIndexAbstraction(index)), is(false)); - assertThat(remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(GetAction.NAME) - .test(mockIndexAbstraction(index)), is(false)); - assertThat(remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(GetIndexAction.NAME) - .test(mockIndexAbstraction(index)), is(false)); + assertThat( + remoteMonitoringCollectorRole.indices().allowedIndicesMatcher("indices:foo").test(mockIndexAbstraction(index)), + is(false) + ); + assertThat( + remoteMonitoringCollectorRole.indices().allowedIndicesMatcher("indices:bar").test(mockIndexAbstraction(index)), + is(false) + ); + assertThat( + remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(DeleteIndexAction.NAME).test(mockIndexAbstraction(index)), + is(false) + ); + assertThat( + remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(CreateIndexAction.NAME).test(mockIndexAbstraction(index)), + is(false) + ); + assertThat( + remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(IndexAction.NAME).test(mockIndexAbstraction(index)), + is(false) + ); + assertThat( + remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(GetIndexAction.NAME).test(mockIndexAbstraction(index)), + is(false) + ); + assertThat( + remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(GetAliasesAction.NAME).test(mockIndexAbstraction(index)), + is(false) + ); + assertThat( + remoteMonitoringCollectorRole.indices() + .allowedIndicesMatcher(RemoveIndexLifecyclePolicyAction.NAME) + .test(mockIndexAbstraction(index)), + is(false) + ); + assertThat( + remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(DeleteAction.NAME).test(mockIndexAbstraction(index)), + is(false) + ); + assertThat( + remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(UpdateSettingsAction.NAME).test(mockIndexAbstraction(index)), + is(false) + ); + assertThat( + remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(SearchAction.NAME).test(mockIndexAbstraction(index)), + is(false) + ); + assertThat( + remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(GetAction.NAME).test(mockIndexAbstraction(index)), + is(false) + ); + assertThat( + remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(GetIndexAction.NAME).test(mockIndexAbstraction(index)), + is(false) + ); }); // These tests might need to change if we add new non-security restricted indices that the monitoring user isn't supposed to see // (but ideally, the monitoring user should see all indices). - assertThat(remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(GetSettingsAction.NAME) - .test(mockIndexAbstraction(randomFrom(RestrictedIndicesNames.RESTRICTED_NAMES))), is(true)); - assertThat(remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(GetSettingsAction.NAME) - .test(mockIndexAbstraction(XPackPlugin.ASYNC_RESULTS_INDEX + randomAlphaOfLengthBetween(0, 2))), is(true)); - assertThat(remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(IndicesShardStoresAction.NAME) - .test(mockIndexAbstraction(randomFrom(RestrictedIndicesNames.RESTRICTED_NAMES))), is(true)); - assertThat(remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(IndicesShardStoresAction.NAME) - .test(mockIndexAbstraction(XPackPlugin.ASYNC_RESULTS_INDEX + randomAlphaOfLengthBetween(0, 2))), is(true)); - assertThat(remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(RecoveryAction.NAME) - .test(mockIndexAbstraction(randomFrom(RestrictedIndicesNames.RESTRICTED_NAMES))), is(true)); - assertThat(remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(RecoveryAction.NAME) - .test(mockIndexAbstraction(XPackPlugin.ASYNC_RESULTS_INDEX + randomAlphaOfLengthBetween(0, 2))), is(true)); - assertThat(remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(IndicesStatsAction.NAME) - .test(mockIndexAbstraction(randomFrom(RestrictedIndicesNames.RESTRICTED_NAMES))), is(true)); - assertThat(remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(IndicesStatsAction.NAME) - .test(mockIndexAbstraction(XPackPlugin.ASYNC_RESULTS_INDEX + randomAlphaOfLengthBetween(0, 2))), is(true)); - assertThat(remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(IndicesSegmentsAction.NAME) - .test(mockIndexAbstraction(randomFrom(RestrictedIndicesNames.RESTRICTED_NAMES))), is(true)); - assertThat(remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(IndicesSegmentsAction.NAME) - .test(mockIndexAbstraction(XPackPlugin.ASYNC_RESULTS_INDEX + randomAlphaOfLengthBetween(0, 2))), is(true)); - - assertThat(remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(SearchAction.NAME) - .test(mockIndexAbstraction(randomFrom(RestrictedIndicesNames.RESTRICTED_NAMES))), is(false)); - assertThat(remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(SearchAction.NAME) - .test(mockIndexAbstraction(XPackPlugin.ASYNC_RESULTS_INDEX + randomAlphaOfLengthBetween(0, 2))), is(false)); - assertThat(remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(GetAction.NAME) - .test(mockIndexAbstraction(randomFrom(RestrictedIndicesNames.RESTRICTED_NAMES))), is(false)); - assertThat(remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(GetAction.NAME) - .test(mockIndexAbstraction(XPackPlugin.ASYNC_RESULTS_INDEX + randomAlphaOfLengthBetween(0, 2))), is(false)); - assertThat(remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(DeleteAction.NAME) - .test(mockIndexAbstraction(randomFrom(RestrictedIndicesNames.RESTRICTED_NAMES))), is(false)); - assertThat(remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(DeleteAction.NAME) - .test(mockIndexAbstraction(XPackPlugin.ASYNC_RESULTS_INDEX + randomAlphaOfLengthBetween(0, 2))), is(false)); - assertThat(remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(IndexAction.NAME) - .test(mockIndexAbstraction(randomFrom(RestrictedIndicesNames.RESTRICTED_NAMES))), is(false)); - assertThat(remoteMonitoringCollectorRole.indices().allowedIndicesMatcher(IndexAction.NAME) - .test(mockIndexAbstraction(XPackPlugin.ASYNC_RESULTS_INDEX + randomAlphaOfLengthBetween(0, 2))), is(false)); + assertThat( + remoteMonitoringCollectorRole.indices() + .allowedIndicesMatcher(GetSettingsAction.NAME) + .test(mockIndexAbstraction(randomFrom(RestrictedIndicesNames.RESTRICTED_NAMES))), + is(true) + ); + assertThat( + remoteMonitoringCollectorRole.indices() + .allowedIndicesMatcher(GetSettingsAction.NAME) + .test(mockIndexAbstraction(XPackPlugin.ASYNC_RESULTS_INDEX + randomAlphaOfLengthBetween(0, 2))), + is(true) + ); + assertThat( + remoteMonitoringCollectorRole.indices() + .allowedIndicesMatcher(IndicesShardStoresAction.NAME) + .test(mockIndexAbstraction(randomFrom(RestrictedIndicesNames.RESTRICTED_NAMES))), + is(true) + ); + assertThat( + remoteMonitoringCollectorRole.indices() + .allowedIndicesMatcher(IndicesShardStoresAction.NAME) + .test(mockIndexAbstraction(XPackPlugin.ASYNC_RESULTS_INDEX + randomAlphaOfLengthBetween(0, 2))), + is(true) + ); + assertThat( + remoteMonitoringCollectorRole.indices() + .allowedIndicesMatcher(RecoveryAction.NAME) + .test(mockIndexAbstraction(randomFrom(RestrictedIndicesNames.RESTRICTED_NAMES))), + is(true) + ); + assertThat( + remoteMonitoringCollectorRole.indices() + .allowedIndicesMatcher(RecoveryAction.NAME) + .test(mockIndexAbstraction(XPackPlugin.ASYNC_RESULTS_INDEX + randomAlphaOfLengthBetween(0, 2))), + is(true) + ); + assertThat( + remoteMonitoringCollectorRole.indices() + .allowedIndicesMatcher(IndicesStatsAction.NAME) + .test(mockIndexAbstraction(randomFrom(RestrictedIndicesNames.RESTRICTED_NAMES))), + is(true) + ); + assertThat( + remoteMonitoringCollectorRole.indices() + .allowedIndicesMatcher(IndicesStatsAction.NAME) + .test(mockIndexAbstraction(XPackPlugin.ASYNC_RESULTS_INDEX + randomAlphaOfLengthBetween(0, 2))), + is(true) + ); + assertThat( + remoteMonitoringCollectorRole.indices() + .allowedIndicesMatcher(IndicesSegmentsAction.NAME) + .test(mockIndexAbstraction(randomFrom(RestrictedIndicesNames.RESTRICTED_NAMES))), + is(true) + ); + assertThat( + remoteMonitoringCollectorRole.indices() + .allowedIndicesMatcher(IndicesSegmentsAction.NAME) + .test(mockIndexAbstraction(XPackPlugin.ASYNC_RESULTS_INDEX + randomAlphaOfLengthBetween(0, 2))), + is(true) + ); + + assertThat( + remoteMonitoringCollectorRole.indices() + .allowedIndicesMatcher(SearchAction.NAME) + .test(mockIndexAbstraction(randomFrom(RestrictedIndicesNames.RESTRICTED_NAMES))), + is(false) + ); + assertThat( + remoteMonitoringCollectorRole.indices() + .allowedIndicesMatcher(SearchAction.NAME) + .test(mockIndexAbstraction(XPackPlugin.ASYNC_RESULTS_INDEX + randomAlphaOfLengthBetween(0, 2))), + is(false) + ); + assertThat( + remoteMonitoringCollectorRole.indices() + .allowedIndicesMatcher(GetAction.NAME) + .test(mockIndexAbstraction(randomFrom(RestrictedIndicesNames.RESTRICTED_NAMES))), + is(false) + ); + assertThat( + remoteMonitoringCollectorRole.indices() + .allowedIndicesMatcher(GetAction.NAME) + .test(mockIndexAbstraction(XPackPlugin.ASYNC_RESULTS_INDEX + randomAlphaOfLengthBetween(0, 2))), + is(false) + ); + assertThat( + remoteMonitoringCollectorRole.indices() + .allowedIndicesMatcher(DeleteAction.NAME) + .test(mockIndexAbstraction(randomFrom(RestrictedIndicesNames.RESTRICTED_NAMES))), + is(false) + ); + assertThat( + remoteMonitoringCollectorRole.indices() + .allowedIndicesMatcher(DeleteAction.NAME) + .test(mockIndexAbstraction(XPackPlugin.ASYNC_RESULTS_INDEX + randomAlphaOfLengthBetween(0, 2))), + is(false) + ); + assertThat( + remoteMonitoringCollectorRole.indices() + .allowedIndicesMatcher(IndexAction.NAME) + .test(mockIndexAbstraction(randomFrom(RestrictedIndicesNames.RESTRICTED_NAMES))), + is(false) + ); + assertThat( + remoteMonitoringCollectorRole.indices() + .allowedIndicesMatcher(IndexAction.NAME) + .test(mockIndexAbstraction(XPackPlugin.ASYNC_RESULTS_INDEX + randomAlphaOfLengthBetween(0, 2))), + is(false) + ); assertMonitoringOnRestrictedIndices(remoteMonitoringCollectorRole); @@ -1115,24 +1354,35 @@ public void testRemoteMonitoringCollectorRole() { private void assertMonitoringOnRestrictedIndices(Role role) { final Settings indexSettings = Settings.builder().put("index.version.created", Version.CURRENT).build(); - final String internalSecurityIndex = randomFrom(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_6, - RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7); - final Metadata metadata = new Metadata.Builder() - .put(new IndexMetadata.Builder(internalSecurityIndex) - .settings(indexSettings) - .numberOfShards(1) - .numberOfReplicas(0) - .putAlias(new AliasMetadata.Builder(RestrictedIndicesNames.SECURITY_MAIN_ALIAS).build()) - .build(), true) - .build(); + final String internalSecurityIndex = randomFrom( + RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_6, + RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7 + ); + final Metadata metadata = new Metadata.Builder().put( + new IndexMetadata.Builder(internalSecurityIndex).settings(indexSettings) + .numberOfShards(1) + .numberOfReplicas(0) + .putAlias(new AliasMetadata.Builder(RestrictedIndicesNames.SECURITY_MAIN_ALIAS).build()) + .build(), + true + ).build(); final FieldPermissionsCache fieldPermissionsCache = new FieldPermissionsCache(Settings.EMPTY); - final List indexMonitoringActionNamesList = Arrays.asList(IndicesStatsAction.NAME, IndicesSegmentsAction.NAME, - GetSettingsAction.NAME, IndicesShardStoresAction.NAME, RecoveryAction.NAME); + final List indexMonitoringActionNamesList = Arrays.asList( + IndicesStatsAction.NAME, + IndicesSegmentsAction.NAME, + GetSettingsAction.NAME, + IndicesShardStoresAction.NAME, + RecoveryAction.NAME + ); for (final String indexMonitoringActionName : indexMonitoringActionNamesList) { String asyncSearchIndex = XPackPlugin.ASYNC_RESULTS_INDEX + randomAlphaOfLengthBetween(0, 2); - final IndicesAccessControl iac = role.indices().authorize(indexMonitoringActionName, - Sets.newHashSet(internalSecurityIndex, RestrictedIndicesNames.SECURITY_MAIN_ALIAS, asyncSearchIndex), - metadata.getIndicesLookup(), fieldPermissionsCache); + final IndicesAccessControl iac = role.indices() + .authorize( + indexMonitoringActionName, + Sets.newHashSet(internalSecurityIndex, RestrictedIndicesNames.SECURITY_MAIN_ALIAS, asyncSearchIndex), + metadata.getIndicesLookup(), + fieldPermissionsCache + ); assertThat(iac.getIndexPermissions(internalSecurityIndex).isGranted(), is(true)); assertThat(iac.getIndexPermissions(RestrictedIndicesNames.SECURITY_MAIN_ALIAS).isGranted(), is(true)); assertThat(iac.getIndexPermissions(asyncSearchIndex).isGranted(), is(true)); @@ -1161,20 +1411,25 @@ public void testReportingUserRole() { assertThat(reportingUserRole.runAs().check(randomAlphaOfLengthBetween(1, 12)), is(false)); assertThat(reportingUserRole.indices().allowedIndicesMatcher(SearchAction.NAME).test(mockIndexAbstraction("foo")), is(false)); - assertThat(reportingUserRole.indices().allowedIndicesMatcher(SearchAction.NAME) - .test(mockIndexAbstraction(".reporting")), is(false)); - assertThat(reportingUserRole.indices().allowedIndicesMatcher(SearchAction.NAME) - .test(mockIndexAbstraction(".kibana")), is(false)); - assertThat(reportingUserRole.indices().allowedIndicesMatcher("indices:foo") - .test(mockIndexAbstraction(randomAlphaOfLengthBetween(8, 24))), is(false)); + assertThat( + reportingUserRole.indices().allowedIndicesMatcher(SearchAction.NAME).test(mockIndexAbstraction(".reporting")), + is(false) + ); + assertThat(reportingUserRole.indices().allowedIndicesMatcher(SearchAction.NAME).test(mockIndexAbstraction(".kibana")), is(false)); + assertThat( + reportingUserRole.indices().allowedIndicesMatcher("indices:foo").test(mockIndexAbstraction(randomAlphaOfLengthBetween(8, 24))), + is(false) + ); final String index = ".reporting-" + randomAlphaOfLength(randomIntBetween(0, 13)); assertThat(reportingUserRole.indices().allowedIndicesMatcher("indices:foo").test(mockIndexAbstraction(index)), is(false)); assertThat(reportingUserRole.indices().allowedIndicesMatcher("indices:bar").test(mockIndexAbstraction(index)), is(false)); assertThat(reportingUserRole.indices().allowedIndicesMatcher(DeleteIndexAction.NAME).test(mockIndexAbstraction(index)), is(false)); assertThat(reportingUserRole.indices().allowedIndicesMatcher(CreateIndexAction.NAME).test(mockIndexAbstraction(index)), is(false)); - assertThat(reportingUserRole.indices().allowedIndicesMatcher(UpdateSettingsAction.NAME) - .test(mockIndexAbstraction(index)), is(false)); + assertThat( + reportingUserRole.indices().allowedIndicesMatcher(UpdateSettingsAction.NAME).test(mockIndexAbstraction(index)), + is(false) + ); assertThat(reportingUserRole.indices().allowedIndicesMatcher(SearchAction.NAME).test(mockIndexAbstraction(index)), is(false)); assertThat(reportingUserRole.indices().allowedIndicesMatcher(GetAction.NAME).test(mockIndexAbstraction(index)), is(false)); assertThat(reportingUserRole.indices().allowedIndicesMatcher(IndexAction.NAME).test(mockIndexAbstraction(index)), is(false)); @@ -1204,47 +1459,58 @@ public void testSuperuserRole() { assertThat(superuserRole.cluster().check("internal:admin/foo", request, authentication), is(false)); final Settings indexSettings = Settings.builder().put("index.version.created", Version.CURRENT).build(); - final String internalSecurityIndex = randomFrom(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_6, - RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7); - final Metadata metadata = new Metadata.Builder() - .put(new IndexMetadata.Builder("a1").settings(indexSettings).numberOfShards(1).numberOfReplicas(0).build(), true) - .put(new IndexMetadata.Builder("a2").settings(indexSettings).numberOfShards(1).numberOfReplicas(0).build(), true) - .put(new IndexMetadata.Builder("aaaaaa").settings(indexSettings).numberOfShards(1).numberOfReplicas(0).build(), true) - .put(new IndexMetadata.Builder("bbbbb").settings(indexSettings).numberOfShards(1).numberOfReplicas(0).build(), true) - .put(new IndexMetadata.Builder("b") - .settings(indexSettings) - .numberOfShards(1) - .numberOfReplicas(0) - .putAlias(new AliasMetadata.Builder("ab").build()) - .putAlias(new AliasMetadata.Builder("ba").build()) - .build(), true) - .put(new IndexMetadata.Builder(internalSecurityIndex) - .settings(indexSettings) - .numberOfShards(1) - .numberOfReplicas(0) - .putAlias(new AliasMetadata.Builder(RestrictedIndicesNames.SECURITY_MAIN_ALIAS).build()) - .build(), true) - .build(); + final String internalSecurityIndex = randomFrom( + RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_6, + RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7 + ); + final Metadata metadata = new Metadata.Builder().put( + new IndexMetadata.Builder("a1").settings(indexSettings).numberOfShards(1).numberOfReplicas(0).build(), + true + ) + .put(new IndexMetadata.Builder("a2").settings(indexSettings).numberOfShards(1).numberOfReplicas(0).build(), true) + .put(new IndexMetadata.Builder("aaaaaa").settings(indexSettings).numberOfShards(1).numberOfReplicas(0).build(), true) + .put(new IndexMetadata.Builder("bbbbb").settings(indexSettings).numberOfShards(1).numberOfReplicas(0).build(), true) + .put( + new IndexMetadata.Builder("b").settings(indexSettings) + .numberOfShards(1) + .numberOfReplicas(0) + .putAlias(new AliasMetadata.Builder("ab").build()) + .putAlias(new AliasMetadata.Builder("ba").build()) + .build(), + true + ) + .put( + new IndexMetadata.Builder(internalSecurityIndex).settings(indexSettings) + .numberOfShards(1) + .numberOfReplicas(0) + .putAlias(new AliasMetadata.Builder(RestrictedIndicesNames.SECURITY_MAIN_ALIAS).build()) + .build(), + true + ) + .build(); FieldPermissionsCache fieldPermissionsCache = new FieldPermissionsCache(Settings.EMPTY); SortedMap lookup = metadata.getIndicesLookup(); - IndicesAccessControl iac = - superuserRole.indices().authorize(SearchAction.NAME, Sets.newHashSet("a1", "ba"), lookup, fieldPermissionsCache); + IndicesAccessControl iac = superuserRole.indices() + .authorize(SearchAction.NAME, Sets.newHashSet("a1", "ba"), lookup, fieldPermissionsCache); assertThat(iac.getIndexPermissions("a1").isGranted(), is(true)); assertThat(iac.getIndexPermissions("b").isGranted(), is(true)); - iac = - superuserRole.indices().authorize(DeleteIndexAction.NAME, Sets.newHashSet("a1", "ba"), lookup, fieldPermissionsCache); + iac = superuserRole.indices().authorize(DeleteIndexAction.NAME, Sets.newHashSet("a1", "ba"), lookup, fieldPermissionsCache); assertThat(iac.getIndexPermissions("a1").isGranted(), is(true)); assertThat(iac.getIndexPermissions("b").isGranted(), is(true)); iac = superuserRole.indices().authorize(IndexAction.NAME, Sets.newHashSet("a2", "ba"), lookup, fieldPermissionsCache); assertThat(iac.getIndexPermissions("a2").isGranted(), is(true)); assertThat(iac.getIndexPermissions("b").isGranted(), is(true)); - iac = superuserRole.indices() - .authorize(UpdateSettingsAction.NAME, Sets.newHashSet("aaaaaa", "ba"), lookup, fieldPermissionsCache); + iac = superuserRole.indices().authorize(UpdateSettingsAction.NAME, Sets.newHashSet("aaaaaa", "ba"), lookup, fieldPermissionsCache); assertThat(iac.getIndexPermissions("aaaaaa").isGranted(), is(true)); assertThat(iac.getIndexPermissions("b").isGranted(), is(true)); - iac = superuserRole.indices().authorize(randomFrom(IndexAction.NAME, DeleteIndexAction.NAME, SearchAction.NAME), - Sets.newHashSet(RestrictedIndicesNames.SECURITY_MAIN_ALIAS), lookup, fieldPermissionsCache); + iac = superuserRole.indices() + .authorize( + randomFrom(IndexAction.NAME, DeleteIndexAction.NAME, SearchAction.NAME), + Sets.newHashSet(RestrictedIndicesNames.SECURITY_MAIN_ALIAS), + lookup, + fieldPermissionsCache + ); assertThat(iac.getIndexPermissions(RestrictedIndicesNames.SECURITY_MAIN_ALIAS).isGranted(), is(true)); assertThat(iac.getIndexPermissions(internalSecurityIndex).isGranted(), is(true)); assertTrue(superuserRole.indices().check(SearchAction.NAME)); @@ -1252,10 +1518,18 @@ public void testSuperuserRole() { assertThat(superuserRole.runAs().check(randomAlphaOfLengthBetween(1, 30)), is(true)); - assertThat(superuserRole.indices().allowedIndicesMatcher(randomFrom(IndexAction.NAME, DeleteIndexAction.NAME, SearchAction.NAME)) - .test(mockIndexAbstraction(RestrictedIndicesNames.SECURITY_MAIN_ALIAS)), is(true)); - assertThat(superuserRole.indices().allowedIndicesMatcher(randomFrom(IndexAction.NAME, DeleteIndexAction.NAME, SearchAction.NAME)) - .test(mockIndexAbstraction(internalSecurityIndex)), is(true)); + assertThat( + superuserRole.indices() + .allowedIndicesMatcher(randomFrom(IndexAction.NAME, DeleteIndexAction.NAME, SearchAction.NAME)) + .test(mockIndexAbstraction(RestrictedIndicesNames.SECURITY_MAIN_ALIAS)), + is(true) + ); + assertThat( + superuserRole.indices() + .allowedIndicesMatcher(randomFrom(IndexAction.NAME, DeleteIndexAction.NAME, SearchAction.NAME)) + .test(mockIndexAbstraction(internalSecurityIndex)), + is(true) + ); } public void testLogstashSystemRole() { @@ -1279,10 +1553,14 @@ public void testLogstashSystemRole() { assertThat(logstashSystemRole.runAs().check(randomAlphaOfLengthBetween(1, 30)), is(false)); assertThat(logstashSystemRole.indices().allowedIndicesMatcher(IndexAction.NAME).test(mockIndexAbstraction("foo")), is(false)); - assertThat(logstashSystemRole.indices().allowedIndicesMatcher(IndexAction.NAME) - .test(mockIndexAbstraction(".reporting")), is(false)); - assertThat(logstashSystemRole.indices().allowedIndicesMatcher("indices:foo") - .test(mockIndexAbstraction(randomAlphaOfLengthBetween(8, 24))), is(false)); + assertThat( + logstashSystemRole.indices().allowedIndicesMatcher(IndexAction.NAME).test(mockIndexAbstraction(".reporting")), + is(false) + ); + assertThat( + logstashSystemRole.indices().allowedIndicesMatcher("indices:foo").test(mockIndexAbstraction(randomAlphaOfLengthBetween(8, 24))), + is(false) + ); assertNoAccessAllowed(logstashSystemRole, RestrictedIndicesNames.RESTRICTED_NAMES); assertNoAccessAllowed(logstashSystemRole, XPackPlugin.ASYNC_RESULTS_INDEX + randomAlphaOfLengthBetween(0, 2)); @@ -1296,7 +1574,6 @@ public void testBeatsAdminRole() { assertNotNull(roleDescriptor); assertThat(roleDescriptor.getMetadata(), hasEntry("_reserved", true)); - final Role beatsAdminRole = Role.builder(roleDescriptor, null, RESTRICTED_INDICES_AUTOMATON).build(); assertThat(beatsAdminRole.cluster().check(ClusterHealthAction.NAME, request, authentication), is(false)); assertThat(beatsAdminRole.cluster().check(ClusterStateAction.NAME, request, authentication), is(false)); @@ -1309,8 +1586,10 @@ public void testBeatsAdminRole() { assertThat(beatsAdminRole.runAs().check(randomAlphaOfLengthBetween(1, 30)), is(false)); - assertThat(beatsAdminRole.indices().allowedIndicesMatcher("indices:foo") - .test(mockIndexAbstraction(randomAlphaOfLengthBetween(8, 24))), is(false)); + assertThat( + beatsAdminRole.indices().allowedIndicesMatcher("indices:foo").test(mockIndexAbstraction(randomAlphaOfLengthBetween(8, 24))), + is(false) + ); final String index = ".management-beats"; logger.info("index name [{}]", index); @@ -1349,13 +1628,15 @@ public void testBeatsSystemRole() { assertThat(beatsSystemRole.runAs().check(randomAlphaOfLengthBetween(1, 30)), is(false)); - - final String index = ".monitoring-beats-" + randomIntBetween(0, 5);; + final String index = ".monitoring-beats-" + randomIntBetween(0, 5); + ; logger.info("beats monitoring index name [{}]", index); assertThat(beatsSystemRole.indices().allowedIndicesMatcher(IndexAction.NAME).test(mockIndexAbstraction("foo")), is(false)); assertThat(beatsSystemRole.indices().allowedIndicesMatcher(IndexAction.NAME).test(mockIndexAbstraction(".reporting")), is(false)); - assertThat(beatsSystemRole.indices().allowedIndicesMatcher("indices:foo") - .test(mockIndexAbstraction(randomAlphaOfLengthBetween(8, 24))), is(false)); + assertThat( + beatsSystemRole.indices().allowedIndicesMatcher("indices:foo").test(mockIndexAbstraction(randomAlphaOfLengthBetween(8, 24))), + is(false) + ); assertThat(beatsSystemRole.indices().allowedIndicesMatcher(CreateIndexAction.NAME).test(mockIndexAbstraction(index)), is(true)); assertThat(beatsSystemRole.indices().allowedIndicesMatcher(IndexAction.NAME).test(mockIndexAbstraction(index)), is(true)); assertThat(beatsSystemRole.indices().allowedIndicesMatcher(DeleteAction.NAME).test(mockIndexAbstraction(index)), is(false)); @@ -1387,22 +1668,32 @@ public void testAPMSystemRole() { assertThat(APMSystemRole.indices().allowedIndicesMatcher(IndexAction.NAME).test(mockIndexAbstraction("foo")), is(false)); assertThat(APMSystemRole.indices().allowedIndicesMatcher(IndexAction.NAME).test(mockIndexAbstraction(".reporting")), is(false)); - assertThat(APMSystemRole.indices().allowedIndicesMatcher("indices:foo") - .test(mockIndexAbstraction(randomAlphaOfLengthBetween(8, 24))), is(false)); + assertThat( + APMSystemRole.indices().allowedIndicesMatcher("indices:foo").test(mockIndexAbstraction(randomAlphaOfLengthBetween(8, 24))), + is(false) + ); final String index = ".monitoring-beats-" + randomIntBetween(10, 15); logger.info("APM beats monitoring index name [{}]", index); assertThat(APMSystemRole.indices().allowedIndicesMatcher(CreateIndexAction.NAME).test(mockIndexAbstraction(index)), is(true)); - assertThat(APMSystemRole.indices().allowedIndicesMatcher("indices:data/write/index:op_type/create") - .test(mockIndexAbstraction(index)), is(true)); + assertThat( + APMSystemRole.indices().allowedIndicesMatcher("indices:data/write/index:op_type/create").test(mockIndexAbstraction(index)), + is(true) + ); assertThat(APMSystemRole.indices().allowedIndicesMatcher(DeleteAction.NAME).test(mockIndexAbstraction(index)), is(false)); assertThat(APMSystemRole.indices().allowedIndicesMatcher(BulkAction.NAME).test(mockIndexAbstraction(index)), is(true)); - assertThat(APMSystemRole.indices().allowedIndicesMatcher("indices:data/write/index:op_type/index") - .test(mockIndexAbstraction(index)), is(false)); - assertThat(APMSystemRole.indices().allowedIndicesMatcher( - "indices:data/write/index:op_type/" + randomAlphaOfLengthBetween(3,5)).test(mockIndexAbstraction(index)), is(false)); + assertThat( + APMSystemRole.indices().allowedIndicesMatcher("indices:data/write/index:op_type/index").test(mockIndexAbstraction(index)), + is(false) + ); + assertThat( + APMSystemRole.indices() + .allowedIndicesMatcher("indices:data/write/index:op_type/" + randomAlphaOfLengthBetween(3, 5)) + .test(mockIndexAbstraction(index)), + is(false) + ); assertNoAccessAllowed(APMSystemRole, RestrictedIndicesNames.RESTRICTED_NAMES); assertNoAccessAllowed(APMSystemRole, XPackPlugin.ASYNC_RESULTS_INDEX + randomAlphaOfLengthBetween(0, 2)); @@ -1442,16 +1733,22 @@ public void testAPMUserRole() { assertOnlyReadAllowed(role, "observability-annotations"); final String kibanaApplicationWithRandomIndex = "kibana-" + randomFrom(randomAlphaOfLengthBetween(8, 24), ".kibana"); - assertThat(role.application().grants( - new ApplicationPrivilege(kibanaApplicationWithRandomIndex, "app-foo", "foo"), "*"), is(false)); - assertThat(role.application().grants( - new ApplicationPrivilege(kibanaApplicationWithRandomIndex, "app-reserved_ml_apm_user", "reserved_ml_apm_user"), "*"), is(true)); + assertThat(role.application().grants(new ApplicationPrivilege(kibanaApplicationWithRandomIndex, "app-foo", "foo"), "*"), is(false)); + assertThat( + role.application() + .grants( + new ApplicationPrivilege(kibanaApplicationWithRandomIndex, "app-reserved_ml_apm_user", "reserved_ml_apm_user"), + "*" + ), + is(true) + ); final String otherApplication = "logstash-" + randomAlphaOfLengthBetween(8, 24); - assertThat(role.application().grants( - new ApplicationPrivilege(otherApplication, "app-foo", "foo"), "*"), is(false)); - assertThat(role.application().grants( - new ApplicationPrivilege(otherApplication, "app-reserved_ml_apm_user", "reserved_ml_apm_user"), "*"), is(false)); + assertThat(role.application().grants(new ApplicationPrivilege(otherApplication, "app-foo", "foo"), "*"), is(false)); + assertThat( + role.application().grants(new ApplicationPrivilege(otherApplication, "app-reserved_ml_apm_user", "reserved_ml_apm_user"), "*"), + is(false) + ); } public void testMachineLearningAdminRole() { @@ -1480,16 +1777,19 @@ public void testMachineLearningAdminRole() { assertNoAccessAllowed(role, XPackPlugin.ASYNC_RESULTS_INDEX + randomAlphaOfLengthBetween(0, 2)); final String kibanaApplicationWithRandomIndex = "kibana-" + randomFrom(randomAlphaOfLengthBetween(8, 24), ".kibana"); - assertThat(role.application().grants( - new ApplicationPrivilege(kibanaApplicationWithRandomIndex, "app-foo", "foo"), "*"), is(false)); - assertThat(role.application().grants( - new ApplicationPrivilege(kibanaApplicationWithRandomIndex, "app-reserved_ml", "reserved_ml_admin"), "*"), is(true)); + assertThat(role.application().grants(new ApplicationPrivilege(kibanaApplicationWithRandomIndex, "app-foo", "foo"), "*"), is(false)); + assertThat( + role.application() + .grants(new ApplicationPrivilege(kibanaApplicationWithRandomIndex, "app-reserved_ml", "reserved_ml_admin"), "*"), + is(true) + ); final String otherApplication = "logstash-" + randomAlphaOfLengthBetween(8, 24); - assertThat(role.application().grants( - new ApplicationPrivilege(otherApplication, "app-foo", "foo"), "*"), is(false)); - assertThat(role.application().grants( - new ApplicationPrivilege(otherApplication, "app-reserved_ml", "reserved_ml_admin"), "*"), is(false)); + assertThat(role.application().grants(new ApplicationPrivilege(otherApplication, "app-foo", "foo"), "*"), is(false)); + assertThat( + role.application().grants(new ApplicationPrivilege(otherApplication, "app-reserved_ml", "reserved_ml_admin"), "*"), + is(false) + ); } private void assertRoleHasManageMl(Role role) { @@ -1633,18 +1933,20 @@ public void testMachineLearningUserRole() { assertNoAccessAllowed(role, RestrictedIndicesNames.RESTRICTED_NAMES); assertNoAccessAllowed(role, XPackPlugin.ASYNC_RESULTS_INDEX + randomAlphaOfLengthBetween(0, 2)); - final String kibanaApplicationWithRandomIndex = "kibana-" + randomFrom(randomAlphaOfLengthBetween(8, 24), ".kibana"); - assertThat(role.application().grants( - new ApplicationPrivilege(kibanaApplicationWithRandomIndex, "app-foo", "foo"), "*"), is(false)); - assertThat(role.application().grants( - new ApplicationPrivilege(kibanaApplicationWithRandomIndex, "app-reserved_ml", "reserved_ml_user"), "*"), is(true)); + assertThat(role.application().grants(new ApplicationPrivilege(kibanaApplicationWithRandomIndex, "app-foo", "foo"), "*"), is(false)); + assertThat( + role.application() + .grants(new ApplicationPrivilege(kibanaApplicationWithRandomIndex, "app-reserved_ml", "reserved_ml_user"), "*"), + is(true) + ); final String otherApplication = "logstash-" + randomAlphaOfLengthBetween(8, 24); - assertThat(role.application().grants( - new ApplicationPrivilege(otherApplication, "app-foo", "foo"), "*"), is(false)); - assertThat(role.application().grants( - new ApplicationPrivilege(otherApplication, "app-reserved_ml", "reserved_ml_user"), "*"), is(false)); + assertThat(role.application().grants(new ApplicationPrivilege(otherApplication, "app-foo", "foo"), "*"), is(false)); + assertThat( + role.application().grants(new ApplicationPrivilege(otherApplication, "app-reserved_ml", "reserved_ml_user"), "*"), + is(false) + ); } public void testTransformAdminRole() { @@ -1653,8 +1955,7 @@ public void testTransformAdminRole() { RoleDescriptor[] roleDescriptors = { new ReservedRolesStore().roleDescriptor("data_frame_transforms_admin"), - new ReservedRolesStore().roleDescriptor("transform_admin") - }; + new ReservedRolesStore().roleDescriptor("transform_admin") }; for (RoleDescriptor roleDescriptor : roleDescriptors) { assertNotNull(roleDescriptor); @@ -1687,20 +1988,26 @@ public void testTransformAdminRole() { assertNoAccessAllowed(role, XPackPlugin.ASYNC_RESULTS_INDEX + randomAlphaOfLengthBetween(0, 2)); final String kibanaApplicationWithRandomIndex = "kibana-" + randomFrom(randomAlphaOfLengthBetween(8, 24), ".kibana"); - assertThat(role.application().grants( - new ApplicationPrivilege(kibanaApplicationWithRandomIndex, "app-foo", "foo"), "*"), is(false)); + assertThat( + role.application().grants(new ApplicationPrivilege(kibanaApplicationWithRandomIndex, "app-foo", "foo"), "*"), + is(false) + ); if (roleDescriptor.getName().equals("data_frame_transforms_admin")) { - assertThat(role.application().grants( - new ApplicationPrivilege(kibanaApplicationWithRandomIndex, "app-reserved_ml", "reserved_ml_user"), "*"), is(true)); + assertThat( + role.application() + .grants(new ApplicationPrivilege(kibanaApplicationWithRandomIndex, "app-reserved_ml", "reserved_ml_user"), "*"), + is(true) + ); } final String otherApplication = "logstash-" + randomAlphaOfLengthBetween(8, 24); - assertThat(role.application().grants( - new ApplicationPrivilege(otherApplication, "app-foo", "foo"), "*"), is(false)); + assertThat(role.application().grants(new ApplicationPrivilege(otherApplication, "app-foo", "foo"), "*"), is(false)); if (roleDescriptor.getName().equals("data_frame_transforms_admin")) { - assertThat(role.application().grants( - new ApplicationPrivilege(otherApplication, "app-reserved_ml", "reserved_ml_user"), "*"), is(false)); + assertThat( + role.application().grants(new ApplicationPrivilege(otherApplication, "app-reserved_ml", "reserved_ml_user"), "*"), + is(false) + ); } } } @@ -1711,8 +2018,7 @@ public void testTransformUserRole() { RoleDescriptor[] roleDescriptors = { new ReservedRolesStore().roleDescriptor("data_frame_transforms_user"), - new ReservedRolesStore().roleDescriptor("transform_user") - }; + new ReservedRolesStore().roleDescriptor("transform_user") }; for (RoleDescriptor roleDescriptor : roleDescriptors) { assertNotNull(roleDescriptor); @@ -1745,20 +2051,26 @@ public void testTransformUserRole() { assertNoAccessAllowed(role, XPackPlugin.ASYNC_RESULTS_INDEX + randomAlphaOfLengthBetween(0, 2)); final String kibanaApplicationWithRandomIndex = "kibana-" + randomFrom(randomAlphaOfLengthBetween(8, 24), ".kibana"); - assertThat(role.application().grants( - new ApplicationPrivilege(kibanaApplicationWithRandomIndex, "app-foo", "foo"), "*"), is(false)); + assertThat( + role.application().grants(new ApplicationPrivilege(kibanaApplicationWithRandomIndex, "app-foo", "foo"), "*"), + is(false) + ); if (roleDescriptor.getName().equals("data_frame_transforms_user")) { - assertThat(role.application().grants( - new ApplicationPrivilege(kibanaApplicationWithRandomIndex, "app-reserved_ml", "reserved_ml_user"), "*"), is(true)); + assertThat( + role.application() + .grants(new ApplicationPrivilege(kibanaApplicationWithRandomIndex, "app-reserved_ml", "reserved_ml_user"), "*"), + is(true) + ); } final String otherApplication = "logstash-" + randomAlphaOfLengthBetween(8, 24); - assertThat(role.application().grants( - new ApplicationPrivilege(otherApplication, "app-foo", "foo"), "*"), is(false)); + assertThat(role.application().grants(new ApplicationPrivilege(otherApplication, "app-foo", "foo"), "*"), is(false)); if (roleDescriptor.getName().equals("data_frame_transforms_user")) { - assertThat(role.application().grants( - new ApplicationPrivilege(otherApplication, "app-reserved_ml", "reserved_ml_user"), "*"), is(false)); + assertThat( + role.application().grants(new ApplicationPrivilege(otherApplication, "app-reserved_ml", "reserved_ml_user"), "*"), + is(false) + ); } } } @@ -1786,7 +2098,7 @@ public void testWatcherAdminRole() { assertThat(role.indices().allowedIndicesMatcher(IndexAction.NAME).test(mockIndexAbstraction("foo")), is(false)); - for (String index : new String[]{ Watch.INDEX, HistoryStoreField.DATA_STREAM, TriggeredWatchStoreField.INDEX_NAME }) { + for (String index : new String[] { Watch.INDEX, HistoryStoreField.DATA_STREAM, TriggeredWatchStoreField.INDEX_NAME }) { assertOnlyReadAllowed(role, index); } @@ -1816,10 +2128,12 @@ public void testWatcherUserRole() { assertThat(role.runAs().check(randomAlphaOfLengthBetween(1, 30)), is(false)); assertThat(role.indices().allowedIndicesMatcher(IndexAction.NAME).test(mockIndexAbstraction("foo")), is(false)); - assertThat(role.indices().allowedIndicesMatcher(IndexAction.NAME).test(mockIndexAbstraction(TriggeredWatchStoreField.INDEX_NAME)), - is(false)); + assertThat( + role.indices().allowedIndicesMatcher(IndexAction.NAME).test(mockIndexAbstraction(TriggeredWatchStoreField.INDEX_NAME)), + is(false) + ); - for (String index : new String[]{ Watch.INDEX, HistoryStoreField.DATA_STREAM }) { + for (String index : new String[] { Watch.INDEX, HistoryStoreField.DATA_STREAM }) { assertOnlyReadAllowed(role, index); } @@ -2045,8 +2359,10 @@ public void testLogstashAdminRole() { assertThat(logstashAdminRole.indices().allowedIndicesMatcher(IndexAction.NAME).test(mockIndexAbstraction("foo")), is(false)); assertThat(logstashAdminRole.indices().allowedIndicesMatcher(IndexAction.NAME).test(mockIndexAbstraction(".reporting")), is(false)); assertThat(logstashAdminRole.indices().allowedIndicesMatcher(IndexAction.NAME).test(mockIndexAbstraction(".logstash")), is(true)); - assertThat(logstashAdminRole.indices().allowedIndicesMatcher("indices:foo").test( - mockIndexAbstraction(randomAlphaOfLengthBetween(8, 24))), is(false)); + assertThat( + logstashAdminRole.indices().allowedIndicesMatcher("indices:foo").test(mockIndexAbstraction(randomAlphaOfLengthBetween(8, 24))), + is(false) + ); final String index = ".logstash-" + randomIntBetween(0, 5); @@ -2057,15 +2373,18 @@ public void testLogstashAdminRole() { assertThat(logstashAdminRole.indices().allowedIndicesMatcher(GetAction.NAME).test(mockIndexAbstraction(index)), is(true)); assertThat(logstashAdminRole.indices().allowedIndicesMatcher(SearchAction.NAME).test(mockIndexAbstraction(index)), is(true)); assertThat(logstashAdminRole.indices().allowedIndicesMatcher(MultiSearchAction.NAME).test(mockIndexAbstraction(index)), is(true)); - assertThat(logstashAdminRole.indices().allowedIndicesMatcher(UpdateSettingsAction.NAME).test(mockIndexAbstraction(index)), - is(true)); + assertThat( + logstashAdminRole.indices().allowedIndicesMatcher(UpdateSettingsAction.NAME).test(mockIndexAbstraction(index)), + is(true) + ); } private IndexAbstraction mockIndexAbstraction(String name) { IndexAbstraction mock = mock(IndexAbstraction.class); when(mock.getName()).thenReturn(name); - when(mock.getType()).thenReturn(randomFrom(IndexAbstraction.Type.CONCRETE_INDEX, - IndexAbstraction.Type.ALIAS, IndexAbstraction.Type.DATA_STREAM)); + when(mock.getType()).thenReturn( + randomFrom(IndexAbstraction.Type.CONCRETE_INDEX, IndexAbstraction.Type.ALIAS, IndexAbstraction.Type.DATA_STREAM) + ); return mock; } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/support/DLSRoleQueryValidatorTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/support/DLSRoleQueryValidatorTests.java index 4a9f74d92235b..1ec6847ca0d7d 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/support/DLSRoleQueryValidatorTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/support/DLSRoleQueryValidatorTests.java @@ -8,7 +8,6 @@ import org.apache.lucene.search.join.ScoreMode; import org.elasticsearch.common.bytes.BytesArray; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.BoostingQueryBuilder; import org.elasticsearch.index.query.ConstantScoreQueryBuilder; @@ -21,6 +20,7 @@ import org.elasticsearch.join.query.HasChildQueryBuilder; import org.elasticsearch.join.query.HasParentQueryBuilder; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; import java.io.IOException; @@ -61,17 +61,20 @@ public void testVerifyRoleQuery() throws Exception { e = expectThrows(IllegalArgumentException.class, () -> DLSRoleQueryValidator.verifyRoleQuery(queryBuilder8)); assertThat(e.getMessage(), equalTo("geoshape query referring to indexed shapes isn't supported as part of a role query")); - QueryBuilder queryBuilder9 = new BoostingQueryBuilder(new GeoShapeQueryBuilder("field", "_id"), - new MatchAllQueryBuilder()); + QueryBuilder queryBuilder9 = new BoostingQueryBuilder(new GeoShapeQueryBuilder("field", "_id"), new MatchAllQueryBuilder()); e = expectThrows(IllegalArgumentException.class, () -> DLSRoleQueryValidator.verifyRoleQuery(queryBuilder9)); assertThat(e.getMessage(), equalTo("geoshape query referring to indexed shapes isn't supported as part of a role query")); } public void testHasStoredScript() throws IOException { - assertThat(DLSRoleQueryValidator.hasStoredScript( - new BytesArray("{\"template\":{\"id\":\"my-script\"}}"), NamedXContentRegistry.EMPTY), is(true)); - assertThat(DLSRoleQueryValidator.hasStoredScript( - new BytesArray("{\"template\":{\"source\":\"{}\"}}"), NamedXContentRegistry.EMPTY), is(false)); + assertThat( + DLSRoleQueryValidator.hasStoredScript(new BytesArray("{\"template\":{\"id\":\"my-script\"}}"), NamedXContentRegistry.EMPTY), + is(true) + ); + assertThat( + DLSRoleQueryValidator.hasStoredScript(new BytesArray("{\"template\":{\"source\":\"{}\"}}"), NamedXContentRegistry.EMPTY), + is(false) + ); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/support/SecurityQueryTemplateEvaluatorTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/support/SecurityQueryTemplateEvaluatorTests.java index 82726e6ba0174..af8163db00a62 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/support/SecurityQueryTemplateEvaluatorTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/support/SecurityQueryTemplateEvaluatorTests.java @@ -8,8 +8,6 @@ package org.elasticsearch.xpack.core.security.authz.support; import org.elasticsearch.common.Strings; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.index.query.TermQueryBuilder; import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptService; @@ -17,6 +15,8 @@ import org.elasticsearch.script.TemplateScript; import org.elasticsearch.script.mustache.MustacheScriptEngine; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.security.user.User; import org.junit.Before; import org.mockito.ArgumentCaptor; @@ -46,8 +46,7 @@ public void setup() throws Exception { } public void testTemplating() throws Exception { - User user = new User("_username", new String[] { "role1", "role2" }, "_full_name", "_email", - Map.of("key", "value"), true); + User user = new User("_username", new String[] { "role1", "role2" }, "_full_name", "_email", Map.of("key", "value"), true); TemplateScript.Factory compiledTemplate = templateParams -> new TemplateScript(templateParams) { @Override @@ -86,8 +85,14 @@ public String execute() { } public void testDocLevelSecurityTemplateWithOpenIdConnectStyleMetadata() throws Exception { - User user = new User(randomAlphaOfLength(8), generateRandomStringArray(5, 5, false), randomAlphaOfLength(9), "sample@example.com", - Map.of("oidc(email)", "sample@example.com"), true); + User user = new User( + randomAlphaOfLength(8), + generateRandomStringArray(5, 5, false), + randomAlphaOfLength(9), + "sample@example.com", + Map.of("oidc(email)", "sample@example.com"), + true + ); final MustacheScriptEngine mustache = new MustacheScriptEngine(); @@ -95,7 +100,11 @@ public void testDocLevelSecurityTemplateWithOpenIdConnectStyleMetadata() throws assertThat(inv.getArguments(), arrayWithSize(2)); Script script = (Script) inv.getArguments()[0]; TemplateScript.Factory factory = mustache.compile( - script.getIdOrCode(), script.getIdOrCode(), TemplateScript.CONTEXT, script.getOptions()); + script.getIdOrCode(), + script.getIdOrCode(), + TemplateScript.CONTEXT, + script.getOptions() + ); return factory; }); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/support/AutomatonsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/support/AutomatonsTests.java index c41c37a5d132f..09923c937cfc3 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/support/AutomatonsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/support/AutomatonsTests.java @@ -82,7 +82,9 @@ public void testPredicateToString() throws Exception { } public void testPatternComplexity() { - List patterns = Arrays.asList("*", "filebeat*de-tst-chatclassification*", + List patterns = Arrays.asList( + "*", + "filebeat*de-tst-chatclassification*", "metricbeat*de-tst-chatclassification*", "packetbeat*de-tst-chatclassification*", "heartbeat*de-tst-chatclassification*", @@ -105,7 +107,8 @@ public void testPatternComplexity() { "filebeat*bender-minio-test-1*", "metricbeat*bender-minio-test-1*", "packetbeat*bender-minio-test-1*", - "heartbeat*bender-minio-test-1*"); + "heartbeat*bender-minio-test-1*" + ); final Automaton automaton = Automatons.patterns(patterns); assertTrue(Operations.isTotal(automaton)); assertTrue(automaton.isDeterministic()); @@ -183,9 +186,7 @@ public void testCachingOfAutomatons() { } public void testConfigurationOfCacheSize() { - final Settings settings = Settings.builder() - .put(Automatons.CACHE_SIZE.getKey(), 2) - .build(); + final Settings settings = Settings.builder().put(Automatons.CACHE_SIZE.getKey(), 2).build(); Automatons.updateConfiguration(settings); String pattern1 = "a"; @@ -210,9 +211,7 @@ public void testConfigurationOfCacheSize() { } public void testDisableCache() { - final Settings settings = Settings.builder() - .put(Automatons.CACHE_ENABLED.getKey(), false) - .build(); + final Settings settings = Settings.builder().put(Automatons.CACHE_ENABLED.getKey(), false).build(); Automatons.updateConfiguration(settings); final String pattern = randomAlphaOfLengthBetween(5, 10); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/support/StringMatcherTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/support/StringMatcherTests.java index 12e5b585ddfbb..fe7155504401a 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/support/StringMatcherTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/support/StringMatcherTests.java @@ -76,7 +76,8 @@ public void testUnicodeWildcard() throws Exception { // Lucene automatons don't work correctly on strings with high surrogates final String prefix = randomValueOtherThanMany( s -> StringMatcherTests.hasHighSurrogate(s) || s.contains("\\") || s.startsWith("/"), - () -> randomRealisticUnicodeOfLengthBetween(3, 5)); + () -> randomRealisticUnicodeOfLengthBetween(3, 5) + ); final StringMatcher matcher = StringMatcher.of(prefix + "*"); for (int i = 0; i < 10; i++) { assertMatch(matcher, prefix + randomRealisticUnicodeOfLengthBetween(i, 20)); @@ -116,17 +117,20 @@ public void testMultiplePatterns() throws Exception { final String suffix2 = randomAlphaOfLengthBetween(8, 12); final String exact1 = randomValueOtherThanMany( s -> s.startsWith(prefix1) || s.startsWith(prefix2) || s.startsWith(prefix3) || s.endsWith(suffix1) || s.endsWith(suffix2), - () -> randomAlphaOfLengthBetween(5, 9)); + () -> randomAlphaOfLengthBetween(5, 9) + ); final String exact2 = randomValueOtherThanMany( s -> s.startsWith(prefix1) || s.startsWith(prefix2) || s.startsWith(prefix3) || s.endsWith(suffix1) || s.endsWith(suffix2), - () -> randomAlphaOfLengthBetween(10, 12)); + () -> randomAlphaOfLengthBetween(10, 12) + ); final String exact3 = randomValueOtherThanMany( s -> s.startsWith(prefix1) || s.startsWith(prefix2) || s.startsWith(prefix3) || s.endsWith(suffix1) || s.endsWith(suffix2), - () -> randomAlphaOfLengthBetween(15, 20)); + () -> randomAlphaOfLengthBetween(15, 20) + ); - final StringMatcher matcher = StringMatcher.of(List.of( - prefix1 + "*", prefix2 + "?", "/" + prefix3 + "@/", "*" + suffix1, "/@" + suffix2 + "/", exact1, exact2, exact3 - )); + final StringMatcher matcher = StringMatcher.of( + List.of(prefix1 + "*", prefix2 + "?", "/" + prefix3 + "@/", "*" + suffix1, "/@" + suffix2 + "/", exact1, exact2, exact3) + ); assertMatch(matcher, exact1); assertMatch(matcher, exact2); @@ -187,5 +191,4 @@ static boolean hasHighSurrogate(String s) { return false; } - } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/support/ValidationTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/support/ValidationTests.java index ff852a45d8f50..cb0de4f46af5a 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/support/ValidationTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/support/ValidationTests.java @@ -28,22 +28,110 @@ public class ValidationTests extends ESTestCase { - private static final Character[] ALLOWED_CHARS = Validation.VALID_NAME_CHARS.toArray( - new Character[Validation.VALID_NAME_CHARS.size()] - ); + private static final Character[] ALLOWED_CHARS = Validation.VALID_NAME_CHARS.toArray(new Character[Validation.VALID_NAME_CHARS.size()]); private static final Set VALID_SERVICE_ACCOUNT_TOKEN_NAME_CHARS = Set.of( - '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', - 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', - 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', - 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', - 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', - '-', '_' + '0', + '1', + '2', + '3', + '4', + '5', + '6', + '7', + '8', + '9', + 'A', + 'B', + 'C', + 'D', + 'E', + 'F', + 'G', + 'H', + 'I', + 'J', + 'K', + 'L', + 'M', + 'N', + 'O', + 'P', + 'Q', + 'R', + 'S', + 'T', + 'U', + 'V', + 'W', + 'X', + 'Y', + 'Z', + 'a', + 'b', + 'c', + 'd', + 'e', + 'f', + 'g', + 'h', + 'i', + 'j', + 'k', + 'l', + 'm', + 'n', + 'o', + 'p', + 'q', + 'r', + 's', + 't', + 'u', + 'v', + 'w', + 'x', + 'y', + 'z', + '-', + '_' ); private static final Set INVALID_SERVICE_ACCOUNT_TOKEN_NAME_CHARS = Set.of( - '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '.', '/', ';', '<', '=', '>', '?', '@', '[', - '\\', ']', '^', '`', '{', '|', '}', '~', ' ', '\t', '\n', '\r'); + '!', + '"', + '#', + '$', + '%', + '&', + '\'', + '(', + ')', + '*', + '+', + ',', + '.', + '/', + ';', + '<', + '=', + '>', + '?', + '@', + '[', + '\\', + ']', + '^', + '`', + '{', + '|', + '}', + '~', + ' ', + '\t', + '\n', + '\r' + ); public void testUsernameValid() throws Exception { int length = randomIntBetween(Validation.MIN_NAME_LENGTH, Validation.MAX_NAME_LENGTH); @@ -191,11 +279,7 @@ private static char[] generateNameInvalidWhitespace(int length) { } public static String randomTokenName() { - final Character[] chars = randomArray( - 1, - 256, - Character[]::new, - () -> randomFrom(VALID_SERVICE_ACCOUNT_TOKEN_NAME_CHARS)); + final Character[] chars = randomArray(1, 256, Character[]::new, () -> randomFrom(VALID_SERVICE_ACCOUNT_TOKEN_NAME_CHARS)); final String name = Arrays.stream(chars).map(String::valueOf).collect(Collectors.joining()); return name.startsWith("_") ? randomAlphaOfLength(1) + name.substring(1) : name; } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/test/TestRestrictedIndices.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/test/TestRestrictedIndices.java index cbc76a25c7b12..8f2b5b70326a1 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/test/TestRestrictedIndices.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/test/TestRestrictedIndices.java @@ -12,12 +12,12 @@ import org.elasticsearch.Version; import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.indices.ExecutorNames; import org.elasticsearch.indices.SystemIndexDescriptor; import org.elasticsearch.indices.SystemIndices; import org.elasticsearch.indices.SystemIndices.Feature; import org.elasticsearch.indices.TestIndexNameExpressionResolver; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.XPackPlugin; import org.elasticsearch.xpack.core.security.index.RestrictedIndicesNames; @@ -27,8 +27,8 @@ import java.util.List; import java.util.Map; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.index.mapper.MapperService.SINGLE_MAPPING_NAME; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.xpack.core.ClientHelper.ASYNC_SEARCH_ORIGIN; import static org.elasticsearch.xpack.core.ClientHelper.SECURITY_ORIGIN; import static org.elasticsearch.xpack.core.security.index.RestrictedIndicesNames.SECURITY_MAIN_ALIAS; @@ -41,55 +41,113 @@ public class TestRestrictedIndices { static { Map featureMap = new HashMap<>(); - featureMap.put("security-mock", - new Feature("security-mock", "fake security for test restricted indices", List.of( - getMainSecurityDescriptor(), - getSecurityTokensDescriptor()))); - featureMap.put("async-search-mock", - new Feature("async search mock", "fake async search for restricted indices", List.of( - getAsyncSearchDescriptor()))); - featureMap.put("kibana-mock", - new Feature("kibana-mock", "fake kibana for testing restricted indices", List.of( - getKibanaSavedObjectsDescriptor(), - getReportingIndexDescriptor(), - getApmAgentConfigDescriptor(), - getApmCustomLinkDescriptor()))); + featureMap.put( + "security-mock", + new Feature( + "security-mock", + "fake security for test restricted indices", + List.of(getMainSecurityDescriptor(), getSecurityTokensDescriptor()) + ) + ); + featureMap.put( + "async-search-mock", + new Feature("async search mock", "fake async search for restricted indices", List.of(getAsyncSearchDescriptor())) + ); + featureMap.put( + "kibana-mock", + new Feature( + "kibana-mock", + "fake kibana for testing restricted indices", + List.of( + getKibanaSavedObjectsDescriptor(), + getReportingIndexDescriptor(), + getApmAgentConfigDescriptor(), + getApmCustomLinkDescriptor() + ) + ) + ); // From here, we have very minimal mock features that only supply system index patterns, // not settings or mock mappings. - featureMap.put("enrich-mock", - new Feature("enrich-mock", "fake enrich for restricted indices tests", List.of( - new SystemIndexDescriptor(".enrich-*", "enrich pattern")))); - featureMap.put("fleet-mock", - new Feature("fleet-mock", "fake fleet for restricted indices tests", List.of( - new SystemIndexDescriptor(".fleet-actions~(-results*)", "fleet actions"), - new SystemIndexDescriptor(".fleet-agents*", "fleet agents"), - new SystemIndexDescriptor(".fleet-enrollment-api-keys*", "fleet enrollment"), - new SystemIndexDescriptor(".fleet-policies-[0-9]+*", "fleet policies"), - new SystemIndexDescriptor(".fleet-policies-leader*", "fleet policies leader"), - new SystemIndexDescriptor(".fleet-servers*", "fleet servers"), - new SystemIndexDescriptor(".fleet-artifacts*", "fleet artifacts")))); - featureMap.put("ingest-geoip-mock", - new Feature("ingest-geoip-mock", "fake geoip for restricted indices tests", List.of( - new SystemIndexDescriptor(".geoip_databases*", "geoip databases")))); - featureMap.put("logstash-mock", - new Feature("logstash-mock", "fake logstash for restricted indices tests", List.of( - new SystemIndexDescriptor(".logstash*", "logstash")))); - featureMap.put("machine-learning-mock", - new Feature("machine-learning-mock", "fake machine learning for restricted indices tests", List.of( - new SystemIndexDescriptor(".ml-meta*", "machine learning meta"), - new SystemIndexDescriptor(".ml-config*", "machine learning config"), - new SystemIndexDescriptor(".ml-inference*", "machine learning inference")))); - featureMap.put("searchable-snapshots-mock", - new Feature("searchable-snapshots-mock", "fake searchable snapshots for restricted indices tests", List.of( - new SystemIndexDescriptor(".snapshot-blob-cache*", "snapshot blob cache")))); - featureMap.put("transform-mock", - new Feature("transform-mock", "fake transform for restricted indices tests", List.of( - new SystemIndexDescriptor(".transform-internal-*", "transform internal")))); - featureMap.put("watcher-mock", - new Feature("watcher-mock", "fake watcher for restricted indices tests", List.of( - new SystemIndexDescriptor(".watches*", "watches"), - new SystemIndexDescriptor(".triggered-watches*", "triggered watches")))); + featureMap.put( + "enrich-mock", + new Feature( + "enrich-mock", + "fake enrich for restricted indices tests", + List.of(new SystemIndexDescriptor(".enrich-*", "enrich pattern")) + ) + ); + featureMap.put( + "fleet-mock", + new Feature( + "fleet-mock", + "fake fleet for restricted indices tests", + List.of( + new SystemIndexDescriptor(".fleet-actions~(-results*)", "fleet actions"), + new SystemIndexDescriptor(".fleet-agents*", "fleet agents"), + new SystemIndexDescriptor(".fleet-enrollment-api-keys*", "fleet enrollment"), + new SystemIndexDescriptor(".fleet-policies-[0-9]+*", "fleet policies"), + new SystemIndexDescriptor(".fleet-policies-leader*", "fleet policies leader"), + new SystemIndexDescriptor(".fleet-servers*", "fleet servers"), + new SystemIndexDescriptor(".fleet-artifacts*", "fleet artifacts") + ) + ) + ); + featureMap.put( + "ingest-geoip-mock", + new Feature( + "ingest-geoip-mock", + "fake geoip for restricted indices tests", + List.of(new SystemIndexDescriptor(".geoip_databases*", "geoip databases")) + ) + ); + featureMap.put( + "logstash-mock", + new Feature( + "logstash-mock", + "fake logstash for restricted indices tests", + List.of(new SystemIndexDescriptor(".logstash*", "logstash")) + ) + ); + featureMap.put( + "machine-learning-mock", + new Feature( + "machine-learning-mock", + "fake machine learning for restricted indices tests", + List.of( + new SystemIndexDescriptor(".ml-meta*", "machine learning meta"), + new SystemIndexDescriptor(".ml-config*", "machine learning config"), + new SystemIndexDescriptor(".ml-inference*", "machine learning inference") + ) + ) + ); + featureMap.put( + "searchable-snapshots-mock", + new Feature( + "searchable-snapshots-mock", + "fake searchable snapshots for restricted indices tests", + List.of(new SystemIndexDescriptor(".snapshot-blob-cache*", "snapshot blob cache")) + ) + ); + featureMap.put( + "transform-mock", + new Feature( + "transform-mock", + "fake transform for restricted indices tests", + List.of(new SystemIndexDescriptor(".transform-internal-*", "transform internal")) + ) + ); + featureMap.put( + "watcher-mock", + new Feature( + "watcher-mock", + "fake watcher for restricted indices tests", + List.of( + new SystemIndexDescriptor(".watches*", "watches"), + new SystemIndexDescriptor(".triggered-watches*", "triggered watches") + ) + ) + ); SystemIndices systemIndices = new SystemIndices(featureMap); RESTRICTED_INDICES_AUTOMATON = systemIndices.getSystemNameAutomaton(); @@ -97,10 +155,7 @@ public class TestRestrictedIndices { } private static SystemIndexDescriptor.Builder getInitializedDescriptorBuilder() { - return SystemIndexDescriptor.builder() - .setMappings(mockMappings()) - .setSettings(Settings.EMPTY) - .setVersionMetaKey("version"); + return SystemIndexDescriptor.builder().setMappings(mockMappings()).setSettings(Settings.EMPTY).setVersionMetaKey("version"); } private static SystemIndexDescriptor getMainSecurityDescriptor() { @@ -117,8 +172,7 @@ private static SystemIndexDescriptor getMainSecurityDescriptor() { } private static SystemIndexDescriptor getSecurityTokensDescriptor() { - return getInitializedDescriptorBuilder() - .setIndexPattern(".security-tokens-[0-9]+*") + return getInitializedDescriptorBuilder().setIndexPattern(".security-tokens-[0-9]+*") .setPrimaryIndex(RestrictedIndicesNames.INTERNAL_SECURITY_TOKENS_INDEX_7) .setDescription("Contains auth token data") .setAliasName(SECURITY_TOKENS_ALIAS) @@ -129,8 +183,7 @@ private static SystemIndexDescriptor getSecurityTokensDescriptor() { } private static SystemIndexDescriptor getAsyncSearchDescriptor() { - return getInitializedDescriptorBuilder() - .setIndexPattern(XPackPlugin.ASYNC_RESULTS_INDEX + "*") + return getInitializedDescriptorBuilder().setIndexPattern(XPackPlugin.ASYNC_RESULTS_INDEX + "*") .setDescription("Async search results") .setPrimaryIndex(XPackPlugin.ASYNC_RESULTS_INDEX) .setOrigin(ASYNC_SEARCH_ORIGIN) @@ -143,7 +196,7 @@ private static SystemIndexDescriptor getKibanaSavedObjectsDescriptor() { .setDescription("Kibana saved objects system index") .setAliasName(".kibana") .setType(SystemIndexDescriptor.Type.EXTERNAL_UNMANAGED) - .setAllowedElasticProductOrigins( List.of("kibana")) + .setAllowedElasticProductOrigins(List.of("kibana")) .build(); } @@ -178,16 +231,15 @@ private TestRestrictedIndices() {} private static XContentBuilder mockMappings() { try { - XContentBuilder builder = jsonBuilder() - .startObject() - .startObject(SINGLE_MAPPING_NAME) - .startObject("_meta") - .field("version", Version.CURRENT) - .endObject() - .field("dynamic", "strict") - .startObject("properties") - .endObject() - .endObject() + XContentBuilder builder = jsonBuilder().startObject() + .startObject(SINGLE_MAPPING_NAME) + .startObject("_meta") + .field("version", Version.CURRENT) + .endObject() + .field("dynamic", "strict") + .startObject("properties") + .endObject() + .endObject() .endObject(); return builder; } catch (IOException e) { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/user/UserTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/user/UserTests.java index f24232dc0ca7c..45d8899800337 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/user/UserTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/user/UserTests.java @@ -19,8 +19,13 @@ public void testUserToString() { assertThat(user.toString(), is("User[username=u1,roles=[r1],fullName=null,email=null,metadata={}]")); user = new User("u1", new String[] { "r1", "r2" }, "user1", "user1@domain.com", Map.of("key", "val"), true); assertThat(user.toString(), is("User[username=u1,roles=[r1,r2],fullName=user1,email=user1@domain.com,metadata={key=val}]")); - user = new User("u1", new String[] {"r1"}, new User("u2", "r2", "r3")); - assertThat(user.toString(), is("User[username=u1,roles=[r1],fullName=null,email=null,metadata={}," + - "authenticatedUser=[User[username=u2,roles=[r2,r3],fullName=null,email=null,metadata={}]]]")); + user = new User("u1", new String[] { "r1" }, new User("u2", "r2", "r3")); + assertThat( + user.toString(), + is( + "User[username=u1,roles=[r1],fullName=null,email=null,metadata={}," + + "authenticatedUser=[User[username=u2,roles=[r2,r3],fullName=null,email=null,metadata={}]]]" + ) + ); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/slm/SnapshotInvocationRecordTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/slm/SnapshotInvocationRecordTests.java index 40c51ab0ed909..6146d7343666f 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/slm/SnapshotInvocationRecordTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/slm/SnapshotInvocationRecordTests.java @@ -8,9 +8,9 @@ package org.elasticsearch.xpack.core.slm; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -36,19 +36,21 @@ protected SnapshotInvocationRecord mutateInstance(SnapshotInvocationRecord insta switch (between(0, 2)) { case 0: return new SnapshotInvocationRecord( - randomValueOtherThan(instance.getSnapshotName(), () -> randomAlphaOfLengthBetween(2,10)), + randomValueOtherThan(instance.getSnapshotName(), () -> randomAlphaOfLengthBetween(2, 10)), instance.getSnapshotFinishTimestamp() - 100, instance.getSnapshotFinishTimestamp(), - instance.getDetails()); + instance.getDetails() + ); case 1: long timestamp = randomValueOtherThan(instance.getSnapshotFinishTimestamp(), ESTestCase::randomNonNegativeLong); - return new SnapshotInvocationRecord(instance.getSnapshotName(), - timestamp - 100, timestamp, - instance.getDetails()); + return new SnapshotInvocationRecord(instance.getSnapshotName(), timestamp - 100, timestamp, instance.getDetails()); case 2: - return new SnapshotInvocationRecord(instance.getSnapshotName(), - instance.getSnapshotFinishTimestamp() - 100, instance.getSnapshotFinishTimestamp(), - randomValueOtherThan(instance.getDetails(), () -> randomAlphaOfLengthBetween(2,10))); + return new SnapshotInvocationRecord( + instance.getSnapshotName(), + instance.getSnapshotFinishTimestamp() - 100, + instance.getSnapshotFinishTimestamp(), + randomValueOtherThan(instance.getDetails(), () -> randomAlphaOfLengthBetween(2, 10)) + ); default: throw new AssertionError("failure, got illegal switch case"); } @@ -56,10 +58,11 @@ protected SnapshotInvocationRecord mutateInstance(SnapshotInvocationRecord insta public static SnapshotInvocationRecord randomSnapshotInvocationRecord() { return new SnapshotInvocationRecord( - randomAlphaOfLengthBetween(5,10), + randomAlphaOfLengthBetween(5, 10), randomNonNegativeNullableLong(), randomNonNegativeLong(), - randomBoolean() ? null : randomAlphaOfLengthBetween(5, 10)); + randomBoolean() ? null : randomAlphaOfLengthBetween(5, 10) + ); } private static Long randomNonNegativeNullableLong() { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/slm/SnapshotLifecycleMetadataTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/slm/SnapshotLifecycleMetadataTests.java index 395a27922cf72..d95d7b51994e2 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/slm/SnapshotLifecycleMetadataTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/slm/SnapshotLifecycleMetadataTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.xpack.core.slm; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ilm.OperationMode; import java.io.IOException; @@ -30,8 +30,11 @@ protected SnapshotLifecycleMetadata createTestInstance() { String id = "policy-" + randomAlphaOfLength(3); policies.put(id, SnapshotLifecyclePolicyMetadataTests.createRandomPolicyMetadata(id)); } - return new SnapshotLifecycleMetadata(policies, randomFrom(OperationMode.values()), - SnapshotLifecycleStatsTests.randomLifecycleStats()); + return new SnapshotLifecycleMetadata( + policies, + randomFrom(OperationMode.values()), + SnapshotLifecycleStatsTests.randomLifecycleStats() + ); } @Override diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/slm/SnapshotLifecyclePolicyItemTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/slm/SnapshotLifecyclePolicyItemTests.java index ea090b846a740..3eeaa18f0a81e 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/slm/SnapshotLifecyclePolicyItemTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/slm/SnapshotLifecyclePolicyItemTests.java @@ -13,24 +13,30 @@ import org.elasticsearch.test.AbstractWireSerializingTestCase; import org.elasticsearch.test.ESTestCase; -import static org.elasticsearch.xpack.core.slm.SnapshotLifecyclePolicyMetadataTests.randomSnapshotLifecyclePolicy; import static org.elasticsearch.xpack.core.slm.SnapshotLifecyclePolicyMetadataTests.createRandomPolicyMetadata; +import static org.elasticsearch.xpack.core.slm.SnapshotLifecyclePolicyMetadataTests.randomSnapshotLifecyclePolicy; public class SnapshotLifecyclePolicyItemTests extends AbstractWireSerializingTestCase { public static SnapshotLifecyclePolicyItem.SnapshotInProgress randomSnapshotInProgress() { - return rarely() ? null : new SnapshotLifecyclePolicyItem.SnapshotInProgress( - new SnapshotId("name-" + randomAlphaOfLength(3), "uuid-" + randomAlphaOfLength(3)), - randomFrom(SnapshotsInProgress.State.values()), - randomNonNegativeLong(), - randomBoolean() ? null : "failure!"); + return rarely() + ? null + : new SnapshotLifecyclePolicyItem.SnapshotInProgress( + new SnapshotId("name-" + randomAlphaOfLength(3), "uuid-" + randomAlphaOfLength(3)), + randomFrom(SnapshotsInProgress.State.values()), + randomNonNegativeLong(), + randomBoolean() ? null : "failure!" + ); } @Override protected SnapshotLifecyclePolicyItem createTestInstance() { String policyId = randomAlphaOfLengthBetween(5, 10); - return new SnapshotLifecyclePolicyItem(createRandomPolicyMetadata(policyId), randomSnapshotInProgress(), - SnapshotLifecycleStatsTests.randomPolicyStats(policyId)); + return new SnapshotLifecyclePolicyItem( + createRandomPolicyMetadata(policyId), + randomSnapshotInProgress(), + SnapshotLifecycleStatsTests.randomPolicyStats(policyId) + ); } @Override @@ -38,65 +44,78 @@ protected SnapshotLifecyclePolicyItem mutateInstance(SnapshotLifecyclePolicyItem switch (between(0, 6)) { case 0: String newPolicyId = randomValueOtherThan(instance.getPolicy().getId(), () -> randomAlphaOfLengthBetween(5, 10)); - return new SnapshotLifecyclePolicyItem(randomSnapshotLifecyclePolicy(newPolicyId), + return new SnapshotLifecyclePolicyItem( + randomSnapshotLifecyclePolicy(newPolicyId), instance.getVersion(), instance.getModifiedDate(), instance.getLastSuccess(), instance.getLastFailure(), instance.getSnapshotInProgress(), - instance.getPolicyStats()); + instance.getPolicyStats() + ); case 1: - return new SnapshotLifecyclePolicyItem(instance.getPolicy(), + return new SnapshotLifecyclePolicyItem( + instance.getPolicy(), randomValueOtherThan(instance.getVersion(), ESTestCase::randomNonNegativeLong), instance.getModifiedDate(), instance.getLastSuccess(), instance.getLastFailure(), instance.getSnapshotInProgress(), - instance.getPolicyStats()); + instance.getPolicyStats() + ); case 2: - return new SnapshotLifecyclePolicyItem(instance.getPolicy(), + return new SnapshotLifecyclePolicyItem( + instance.getPolicy(), instance.getVersion(), randomValueOtherThan(instance.getModifiedDate(), ESTestCase::randomNonNegativeLong), instance.getLastSuccess(), instance.getLastFailure(), instance.getSnapshotInProgress(), - instance.getPolicyStats()); + instance.getPolicyStats() + ); case 3: - return new SnapshotLifecyclePolicyItem(instance.getPolicy(), + return new SnapshotLifecyclePolicyItem( + instance.getPolicy(), instance.getVersion(), instance.getModifiedDate(), - randomValueOtherThan(instance.getLastSuccess(), - SnapshotInvocationRecordTests::randomSnapshotInvocationRecord), + randomValueOtherThan(instance.getLastSuccess(), SnapshotInvocationRecordTests::randomSnapshotInvocationRecord), instance.getLastFailure(), instance.getSnapshotInProgress(), - instance.getPolicyStats()); + instance.getPolicyStats() + ); case 4: - return new SnapshotLifecyclePolicyItem(instance.getPolicy(), + return new SnapshotLifecyclePolicyItem( + instance.getPolicy(), instance.getVersion(), instance.getModifiedDate(), instance.getLastSuccess(), - randomValueOtherThan(instance.getLastFailure(), - SnapshotInvocationRecordTests::randomSnapshotInvocationRecord), + randomValueOtherThan(instance.getLastFailure(), SnapshotInvocationRecordTests::randomSnapshotInvocationRecord), instance.getSnapshotInProgress(), - instance.getPolicyStats()); + instance.getPolicyStats() + ); case 5: - return new SnapshotLifecyclePolicyItem(instance.getPolicy(), + return new SnapshotLifecyclePolicyItem( + instance.getPolicy(), instance.getVersion(), instance.getModifiedDate(), instance.getLastSuccess(), instance.getLastFailure(), - randomValueOtherThan(instance.getSnapshotInProgress(), - SnapshotLifecyclePolicyItemTests::randomSnapshotInProgress), - instance.getPolicyStats()); + randomValueOtherThan(instance.getSnapshotInProgress(), SnapshotLifecyclePolicyItemTests::randomSnapshotInProgress), + instance.getPolicyStats() + ); case 6: - return new SnapshotLifecyclePolicyItem(instance.getPolicy(), + return new SnapshotLifecyclePolicyItem( + instance.getPolicy(), instance.getVersion(), instance.getModifiedDate(), instance.getLastSuccess(), instance.getLastFailure(), instance.getSnapshotInProgress(), - randomValueOtherThan(instance.getPolicyStats(), - () -> SnapshotLifecycleStatsTests.randomPolicyStats(instance.getPolicy().getId()))); + randomValueOtherThan( + instance.getPolicyStats(), + () -> SnapshotLifecycleStatsTests.randomPolicyStats(instance.getPolicy().getId()) + ) + ); default: throw new AssertionError("failure, got illegal switch case"); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/slm/SnapshotLifecyclePolicyMetadataTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/slm/SnapshotLifecyclePolicyMetadataTests.java index bbdf63227f59b..c05ba4409242f 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/slm/SnapshotLifecyclePolicyMetadataTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/slm/SnapshotLifecyclePolicyMetadataTests.java @@ -9,9 +9,9 @@ import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.HashMap; @@ -35,9 +35,9 @@ protected SnapshotLifecyclePolicyMetadata createTestInstance() { private static Map randomHeaders() { Map headers = new HashMap<>(); - int headerCount = randomIntBetween(1,10); + int headerCount = randomIntBetween(1, 10); for (int i = 0; i < headerCount; i++) { - headers.put(randomAlphaOfLengthBetween(5,10), randomAlphaOfLengthBetween(5,10)); + headers.put(randomAlphaOfLengthBetween(5, 10), randomAlphaOfLengthBetween(5, 10)); } return headers; } @@ -68,13 +68,15 @@ protected SnapshotLifecyclePolicyMetadata mutateInstance(SnapshotLifecyclePolicy .build(); case 4: return SnapshotLifecyclePolicyMetadata.builder(instance) - .setLastSuccess(randomValueOtherThan(instance.getLastSuccess(), - SnapshotInvocationRecordTests::randomSnapshotInvocationRecord)) + .setLastSuccess( + randomValueOtherThan(instance.getLastSuccess(), SnapshotInvocationRecordTests::randomSnapshotInvocationRecord) + ) .build(); case 5: return SnapshotLifecyclePolicyMetadata.builder(instance) - .setLastFailure(randomValueOtherThan(instance.getLastFailure(), - SnapshotInvocationRecordTests::randomSnapshotInvocationRecord)) + .setLastFailure( + randomValueOtherThan(instance.getLastFailure(), SnapshotInvocationRecordTests::randomSnapshotInvocationRecord) + ) .build(); default: throw new AssertionError("failure, got illegal switch case"); @@ -103,24 +105,27 @@ public static SnapshotLifecyclePolicy randomSnapshotLifecyclePolicy(String polic for (int i = 0; i < randomIntBetween(2, 5); i++) { config.put(randomAlphaOfLength(4), randomAlphaOfLength(4)); } - return new SnapshotLifecyclePolicy(policyId, + return new SnapshotLifecyclePolicy( + policyId, randomAlphaOfLength(4), randomSchedule(), randomAlphaOfLength(4), config, - randomRetention()); + randomRetention() + ); } public static SnapshotRetentionConfiguration randomRetention() { - return rarely() ? null : new SnapshotRetentionConfiguration( - rarely() ? null : TimeValue.parseTimeValue(randomTimeValue(), "random retention generation"), - rarely() ? null : randomIntBetween(1, 10), - rarely() ? null : randomIntBetween(15, 30)); + return rarely() + ? null + : new SnapshotRetentionConfiguration( + rarely() ? null : TimeValue.parseTimeValue(randomTimeValue(), "random retention generation"), + rarely() ? null : randomIntBetween(1, 10), + rarely() ? null : randomIntBetween(15, 30) + ); } public static String randomSchedule() { - return randomIntBetween(0, 59) + " " + - randomIntBetween(0, 59) + " " + - randomIntBetween(0, 12) + " * * ?"; + return randomIntBetween(0, 59) + " " + randomIntBetween(0, 59) + " " + randomIntBetween(0, 12) + " * * ?"; } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/slm/SnapshotLifecycleStatsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/slm/SnapshotLifecycleStatsTests.java index 72104731077c4..ecca501b5391d 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/slm/SnapshotLifecycleStatsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/slm/SnapshotLifecycleStatsTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.xpack.core.slm; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.HashMap; @@ -22,11 +22,13 @@ protected SnapshotLifecycleStats doParseInstance(XContentParser parser) throws I } public static SnapshotLifecycleStats.SnapshotPolicyStats randomPolicyStats(String policyId) { - return new SnapshotLifecycleStats.SnapshotPolicyStats(policyId, + return new SnapshotLifecycleStats.SnapshotPolicyStats( + policyId, randomBoolean() ? 0 : randomIntBetween(0, Integer.MAX_VALUE), randomBoolean() ? 0 : randomIntBetween(0, Integer.MAX_VALUE), randomBoolean() ? 0 : randomIntBetween(0, Integer.MAX_VALUE), - randomBoolean() ? 0 : randomIntBetween(0, Integer.MAX_VALUE)); + randomBoolean() ? 0 : randomIntBetween(0, Integer.MAX_VALUE) + ); } public static SnapshotLifecycleStats randomLifecycleStats() { @@ -41,7 +43,8 @@ public static SnapshotLifecycleStats randomLifecycleStats() { randomBoolean() ? 0 : randomIntBetween(0, Integer.MAX_VALUE), randomBoolean() ? 0 : randomIntBetween(0, Integer.MAX_VALUE), randomBoolean() ? 0 : randomIntBetween(0, Integer.MAX_VALUE), - policyStats); + policyStats + ); } @Override diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/slm/SnapshotRetentionConfigurationTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/slm/SnapshotRetentionConfigurationTests.java index 6a92b00298a7e..02034eb6bd2da 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/slm/SnapshotRetentionConfigurationTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/slm/SnapshotRetentionConfigurationTests.java @@ -48,7 +48,10 @@ public void testConflictingSettings() { public void testExpireAfter() { SnapshotRetentionConfiguration conf = new SnapshotRetentionConfiguration( () -> TimeValue.timeValueDays(1).millis() + 1, - TimeValue.timeValueDays(1), null, null); + TimeValue.timeValueDays(1), + null, + null + ); SnapshotInfo oldInfo = makeInfo(0); assertThat(conf.getSnapshotDeletionPredicate(Collections.singletonList(oldInfo)).test(oldInfo), equalTo(true)); @@ -63,8 +66,12 @@ public void testExpireAfter() { } public void testExpiredWithMinimum() { - SnapshotRetentionConfiguration conf = new SnapshotRetentionConfiguration(() -> TimeValue.timeValueDays(1).millis() + 1, - TimeValue.timeValueDays(1), 2, null); + SnapshotRetentionConfiguration conf = new SnapshotRetentionConfiguration( + () -> TimeValue.timeValueDays(1).millis() + 1, + TimeValue.timeValueDays(1), + 2, + null + ); SnapshotInfo oldInfo = makeInfo(0); SnapshotInfo newInfo = makeInfo(1); @@ -74,8 +81,7 @@ public void testExpiredWithMinimum() { assertThat(conf.getSnapshotDeletionPredicate(infos).test(newInfo), equalTo(false)); assertThat(conf.getSnapshotDeletionPredicate(infos).test(oldInfo), equalTo(false)); - conf = new SnapshotRetentionConfiguration(() -> TimeValue.timeValueDays(1).millis() + 1, - TimeValue.timeValueDays(1), 1, null); + conf = new SnapshotRetentionConfiguration(() -> TimeValue.timeValueDays(1).millis() + 1, TimeValue.timeValueDays(1), 1, null); assertThat(conf.getSnapshotDeletionPredicate(infos).test(newInfo), equalTo(false)); assertThat(conf.getSnapshotDeletionPredicate(infos).test(oldInfo), equalTo(true)); } @@ -92,7 +98,7 @@ public void testMaximum() { SnapshotInfo s8 = makeInfo(8); SnapshotInfo s9 = makeInfo(9); - List infos = Arrays.asList(s1 , s2, s3, s4, s5, s6, s7, s8, s9); + List infos = Arrays.asList(s1, s2, s3, s4, s5, s6, s7, s8, s9); assertThat(conf.getSnapshotDeletionPredicate(infos).test(s1), equalTo(true)); assertThat(conf.getSnapshotDeletionPredicate(infos).test(s2), equalTo(true)); assertThat(conf.getSnapshotDeletionPredicate(infos).test(s3), equalTo(true)); @@ -107,12 +113,15 @@ public void testMaximum() { public void testMaximumWithExpireAfter() { SnapshotRetentionConfiguration conf = new SnapshotRetentionConfiguration( () -> TimeValue.timeValueDays(1).millis() + 2, - TimeValue.timeValueDays(1), null, 2); + TimeValue.timeValueDays(1), + null, + 2 + ); SnapshotInfo old1 = makeInfo(0); SnapshotInfo old2 = makeInfo(1); SnapshotInfo new1 = makeInfo(2); - List infos = Arrays.asList(old1, old2 , new1); + List infos = Arrays.asList(old1, old2, new1); assertThat(conf.getSnapshotDeletionPredicate(infos).test(old1), equalTo(true)); assertThat(conf.getSnapshotDeletionPredicate(infos).test(old2), equalTo(true)); assertThat(conf.getSnapshotDeletionPredicate(infos).test(new1), equalTo(false)); @@ -125,7 +134,7 @@ public void testMaximumWithFailedOrPartial() { SnapshotInfo s3 = makeInfo(3); SnapshotInfo s4 = makeInfo(4); - List infos = Arrays.asList(s1 , s2, s3, s4); + List infos = Arrays.asList(s1, s2, s3, s4); assertThat(conf.getSnapshotDeletionPredicate(infos).test(s1), equalTo(true)); assertThat(conf.getSnapshotDeletionPredicate(infos).test(s2), equalTo(true)); assertThat(conf.getSnapshotDeletionPredicate(infos).test(s3), equalTo(true)); @@ -143,7 +152,10 @@ public void testPartialsDeletedIfExpired() { private void assertUnsuccessfulDeletedIfExpired(boolean failure) { SnapshotRetentionConfiguration conf = new SnapshotRetentionConfiguration( () -> TimeValue.timeValueDays(1).millis() + 1, - TimeValue.timeValueDays(1), null, null); + TimeValue.timeValueDays(1), + null, + null + ); SnapshotInfo oldInfo = makeFailureOrPartial(0, failure); assertThat(conf.getSnapshotDeletionPredicate(Collections.singletonList(oldInfo)).test(oldInfo), equalTo(true)); @@ -172,7 +184,7 @@ private void assertUnsuccessfulDeletedIfNoExpiryAndMoreRecentSuccessExists(boole SnapshotInfo s3 = makeFailureOrPartial(3, failure); SnapshotInfo s4 = makeInfo(4); - List infos = Arrays.asList(s1 , s2, s3, s4); + List infos = Arrays.asList(s1, s2, s3, s4); assertThat(conf.getSnapshotDeletionPredicate(infos).test(s1), equalTo(false)); assertThat(conf.getSnapshotDeletionPredicate(infos).test(s2), equalTo(false)); assertThat(conf.getSnapshotDeletionPredicate(infos).test(s3), equalTo(true)); @@ -195,7 +207,7 @@ private void assertUnsuccessfulKeptIfNoExpiryAndNoMoreRecentSuccess(boolean fail SnapshotInfo s3 = makeInfo(3); SnapshotInfo s4 = makeFailureOrPartial(4, failure); - List infos = Arrays.asList(s1 , s2, s3, s4); + List infos = Arrays.asList(s1, s2, s3, s4); assertThat(conf.getSnapshotDeletionPredicate(infos).test(s1), equalTo(false)); assertThat(conf.getSnapshotDeletionPredicate(infos).test(s2), equalTo(false)); assertThat(conf.getSnapshotDeletionPredicate(infos).test(s3), equalTo(false)); @@ -218,7 +230,7 @@ private void assertUnsuccessfulNotCountedTowardsMaximum(boolean failure) { SnapshotInfo s4 = makeFailureOrPartial(4, failure); SnapshotInfo s5 = makeInfo(5); - List infos = Arrays.asList(s1 , s2, s3, s4, s5); + List infos = Arrays.asList(s1, s2, s3, s4, s5); assertThat(conf.getSnapshotDeletionPredicate(infos).test(s1), equalTo(false)); assertThat(conf.getSnapshotDeletionPredicate(infos).test(s2), equalTo(false)); assertThat(conf.getSnapshotDeletionPredicate(infos).test(s3), equalTo(false)); @@ -235,8 +247,12 @@ public void testPartialsNotCountedTowardsMinimum() { } private void assertUnsuccessfulNotCountedTowardsMinimum(boolean failure) { - SnapshotRetentionConfiguration conf = new SnapshotRetentionConfiguration(() -> TimeValue.timeValueDays(1).millis() + 1, - TimeValue.timeValueDays(1), 2, null); + SnapshotRetentionConfiguration conf = new SnapshotRetentionConfiguration( + () -> TimeValue.timeValueDays(1).millis() + 1, + TimeValue.timeValueDays(1), + 2, + null + ); SnapshotInfo oldInfo = makeInfo(0); SnapshotInfo failureInfo = makeFailureOrPartial(1, failure); SnapshotInfo newInfo = makeInfo(2); @@ -249,8 +265,7 @@ private void assertUnsuccessfulNotCountedTowardsMinimum(boolean failure) { assertThat(conf.getSnapshotDeletionPredicate(infos).test(failureInfo), equalTo(false)); assertThat(conf.getSnapshotDeletionPredicate(infos).test(oldInfo), equalTo(false)); - conf = new SnapshotRetentionConfiguration(() -> TimeValue.timeValueDays(1).millis() + 2, - TimeValue.timeValueDays(1), 1, null); + conf = new SnapshotRetentionConfiguration(() -> TimeValue.timeValueDays(1).millis() + 2, TimeValue.timeValueDays(1), 1, null); assertThat(conf.getSnapshotDeletionPredicate(infos).test(newInfo), equalTo(false)); assertThat(conf.getSnapshotDeletionPredicate(infos).test(failureInfo), equalTo(true)); assertThat(conf.getSnapshotDeletionPredicate(infos).test(oldInfo), equalTo(true)); @@ -264,7 +279,7 @@ public void testMostRecentSuccessfulTimestampIsUsed() { SnapshotInfo s3 = makeFailureOrPartial(3, failureBeforePartial); SnapshotInfo s4 = makeFailureOrPartial(4, failureBeforePartial == false); - List infos = Arrays.asList(s1 , s2, s3, s4); + List infos = Arrays.asList(s1, s2, s3, s4); assertThat(conf.getSnapshotDeletionPredicate(infos).test(s1), equalTo(false)); assertThat(conf.getSnapshotDeletionPredicate(infos).test(s2), equalTo(false)); assertThat(conf.getSnapshotDeletionPredicate(infos).test(s3), equalTo(false)); @@ -277,7 +292,7 @@ public void testFewerSuccessesThanMinWithPartial() { SnapshotInfo sP = makePartialInfo(2); SnapshotInfo s2 = makeInfo(3); - List infos = Arrays.asList(s1 , sP, s2); + List infos = Arrays.asList(s1, sP, s2); assertThat(conf.getSnapshotDeletionPredicate(infos).test(s1), equalTo(false)); assertThat(conf.getSnapshotDeletionPredicate(infos).test(sP), equalTo(false)); assertThat(conf.getSnapshotDeletionPredicate(infos).test(s2), equalTo(false)); @@ -286,7 +301,7 @@ public void testFewerSuccessesThanMinWithPartial() { private SnapshotInfo makeInfo(long startTime) { final Map meta = new HashMap<>(); meta.put(SnapshotsService.POLICY_ID_METADATA_FIELD, REPO); - final int totalShards = between(1,20); + final int totalShards = between(1, 20); SnapshotInfo snapInfo = new SnapshotInfo( new Snapshot(REPO, new SnapshotId("snap-" + randomAlphaOfLength(3), "uuid")), Collections.singletonList("foo"), @@ -316,9 +331,9 @@ private SnapshotInfo makeFailureOrPartial(long startTime, boolean failure) { private SnapshotInfo makeFailureInfo(long startTime) { final Map meta = new HashMap<>(); meta.put(SnapshotsService.POLICY_ID_METADATA_FIELD, REPO); - final int totalShards = between(1,20); + final int totalShards = between(1, 20); final List failures = new ArrayList<>(); - final int failureCount = between(1,totalShards); + final int failureCount = between(1, totalShards); for (int i = 0; i < failureCount; i++) { failures.add(new SnapshotShardFailure("nodeId", new ShardId("index-name", "index-uuid", i), "failed")); } @@ -344,9 +359,9 @@ private SnapshotInfo makeFailureInfo(long startTime) { private SnapshotInfo makePartialInfo(long startTime) { final Map meta = new HashMap<>(); meta.put(SnapshotsService.POLICY_ID_METADATA_FIELD, REPO); - final int totalShards = between(2,20); + final int totalShards = between(2, 20); final List failures = new ArrayList<>(); - final int failureCount = between(1,totalShards - 1); + final int failureCount = between(1, totalShards - 1); for (int i = 0; i < failureCount; i++) { failures.add(new SnapshotShardFailure("nodeId", new ShardId("index-name", "index-uuid", i), "failed")); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/slm/history/SnapshotHistoryItemTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/slm/history/SnapshotHistoryItemTests.java index 49973d16ddec4..e34a8612e5526 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/slm/history/SnapshotHistoryItemTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/slm/history/SnapshotHistoryItemTests.java @@ -8,9 +8,9 @@ package org.elasticsearch.xpack.core.slm.history; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.Arrays; @@ -40,8 +40,7 @@ protected SnapshotHistoryItem createTestInstance() { Map snapshotConfig = randomSnapshotConfiguration(); String errorDetails = randomBoolean() ? null : randomAlphaOfLengthBetween(10, 20); - return new SnapshotHistoryItem(timestamp, policyId, repository, snapshotName, operation, success, snapshotConfig, - errorDetails); + return new SnapshotHistoryItem(timestamp, policyId, repository, snapshotName, operation, success, snapshotConfig, errorDetails); } @Override @@ -51,42 +50,91 @@ protected SnapshotHistoryItem mutateInstance(SnapshotHistoryItem instance) { case 0: // New timestamp return new SnapshotHistoryItem( randomValueOtherThan(instance.getTimestamp(), ESTestCase::randomNonNegativeLong), - instance.getPolicyId(), instance.getRepository(), instance.getSnapshotName(), instance.getOperation(), - instance.isSuccess(), instance.getSnapshotConfiguration(), instance.getErrorDetails()); + instance.getPolicyId(), + instance.getRepository(), + instance.getSnapshotName(), + instance.getOperation(), + instance.isSuccess(), + instance.getSnapshotConfiguration(), + instance.getErrorDetails() + ); case 1: // new policyId - return new SnapshotHistoryItem(instance.getTimestamp(), + return new SnapshotHistoryItem( + instance.getTimestamp(), randomValueOtherThan(instance.getPolicyId(), () -> randomAlphaOfLengthBetween(5, 10)), - instance.getSnapshotName(), instance.getRepository(), instance.getOperation(), instance.isSuccess(), - instance.getSnapshotConfiguration(), instance.getErrorDetails()); + instance.getSnapshotName(), + instance.getRepository(), + instance.getOperation(), + instance.isSuccess(), + instance.getSnapshotConfiguration(), + instance.getErrorDetails() + ); case 2: // new repo name - return new SnapshotHistoryItem(instance.getTimestamp(), instance.getPolicyId(), instance.getSnapshotName(), + return new SnapshotHistoryItem( + instance.getTimestamp(), + instance.getPolicyId(), + instance.getSnapshotName(), randomValueOtherThan(instance.getRepository(), () -> randomAlphaOfLengthBetween(5, 10)), - instance.getOperation(), instance.isSuccess(), instance.getSnapshotConfiguration(), instance.getErrorDetails()); + instance.getOperation(), + instance.isSuccess(), + instance.getSnapshotConfiguration(), + instance.getErrorDetails() + ); case 3: - return new SnapshotHistoryItem(instance.getTimestamp(), instance.getPolicyId(), instance.getRepository(), + return new SnapshotHistoryItem( + instance.getTimestamp(), + instance.getPolicyId(), + instance.getRepository(), randomValueOtherThan(instance.getSnapshotName(), () -> randomAlphaOfLengthBetween(5, 10)), - instance.getOperation(), instance.isSuccess(), instance.getSnapshotConfiguration(), instance.getErrorDetails()); + instance.getOperation(), + instance.isSuccess(), + instance.getSnapshotConfiguration(), + instance.getErrorDetails() + ); case 4: - return new SnapshotHistoryItem(instance.getTimestamp(), instance.getPolicyId(), instance.getRepository(), + return new SnapshotHistoryItem( + instance.getTimestamp(), + instance.getPolicyId(), + instance.getRepository(), instance.getSnapshotName(), randomValueOtherThan(instance.getOperation(), () -> randomAlphaOfLengthBetween(5, 10)), - instance.isSuccess(), instance.getSnapshotConfiguration(), instance.getErrorDetails()); + instance.isSuccess(), + instance.getSnapshotConfiguration(), + instance.getErrorDetails() + ); case 5: - return new SnapshotHistoryItem(instance.getTimestamp(), instance.getPolicyId(), instance.getRepository(), + return new SnapshotHistoryItem( + instance.getTimestamp(), + instance.getPolicyId(), + instance.getRepository(), instance.getSnapshotName(), instance.getOperation(), instance.isSuccess() == false, - instance.getSnapshotConfiguration(), instance.getErrorDetails()); + instance.getSnapshotConfiguration(), + instance.getErrorDetails() + ); case 6: - return new SnapshotHistoryItem(instance.getTimestamp(), instance.getPolicyId(), instance.getRepository(), - instance.getSnapshotName(), instance.getOperation(), instance.isSuccess(), - randomValueOtherThan(instance.getSnapshotConfiguration(), - SnapshotHistoryItemTests::randomSnapshotConfiguration), - instance.getErrorDetails()); + return new SnapshotHistoryItem( + instance.getTimestamp(), + instance.getPolicyId(), + instance.getRepository(), + instance.getSnapshotName(), + instance.getOperation(), + instance.isSuccess(), + randomValueOtherThan(instance.getSnapshotConfiguration(), SnapshotHistoryItemTests::randomSnapshotConfiguration), + instance.getErrorDetails() + ); case 7: - return new SnapshotHistoryItem(instance.getTimestamp(), instance.getPolicyId(), instance.getRepository(), - instance.getSnapshotName(), instance.getOperation(), instance.isSuccess(), instance.getSnapshotConfiguration(), - randomValueOtherThan(instance.getErrorDetails(), () -> randomAlphaOfLengthBetween(10, 20))); + return new SnapshotHistoryItem( + instance.getTimestamp(), + instance.getPolicyId(), + instance.getRepository(), + instance.getSnapshotName(), + instance.getOperation(), + instance.isSuccess(), + instance.getSnapshotConfiguration(), + randomValueOtherThan(instance.getErrorDetails(), () -> randomAlphaOfLengthBetween(10, 20)) + ); default: throw new IllegalArgumentException("illegal randomization: " + branch); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/slm/history/SnapshotHistoryStoreTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/slm/history/SnapshotHistoryStoreTests.java index 58aa4efd169a4..65d0d4681d59a 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/slm/history/SnapshotHistoryStoreTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/slm/history/SnapshotHistoryStoreTests.java @@ -18,14 +18,14 @@ import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.DeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.test.ClusterServiceUtils; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.threadpool.TestThreadPool; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.DeprecationHandler; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.slm.SnapshotLifecyclePolicy; import org.junit.After; import org.junit.Before; @@ -55,12 +55,16 @@ public void setup() throws IOException { threadPool = new TestThreadPool(this.getClass().getName()); client = new SnapshotLifecycleTemplateRegistryTests.VerifyingClient(threadPool); clusterService = ClusterServiceUtils.createClusterService(threadPool); - ComposableIndexTemplate template = - ComposableIndexTemplate.parse(JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, TEMPLATE_SLM_HISTORY.loadBytes())); + ComposableIndexTemplate template = ComposableIndexTemplate.parse( + JsonXContent.jsonXContent.createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + TEMPLATE_SLM_HISTORY.loadBytes() + ) + ); ClusterState state = clusterService.state(); - Metadata.Builder metadataBuilder = - Metadata.builder(state.getMetadata()).indexTemplates(Map.of(TEMPLATE_SLM_HISTORY.getTemplateName(), template)); + Metadata.Builder metadataBuilder = Metadata.builder(state.getMetadata()) + .indexTemplates(Map.of(TEMPLATE_SLM_HISTORY.getTemplateName(), template)); ClusterServiceUtils.setState(clusterService, ClusterState.builder(state).metadata(metadataBuilder).build()); historyStore = new SnapshotHistoryStore(Settings.EMPTY, client, clusterService); clusterService.stop(); @@ -121,7 +125,8 @@ public void testPut() throws Exception { randomLongBetween(1, 1000), randomLongBetween(1, 1000), randomLongBetween(1, 1000), - randomBoolean()); + randomBoolean() + ); }); historyStore.putAsync(record); @@ -160,7 +165,8 @@ public void testPut() throws Exception { randomLongBetween(1, 1000), randomLongBetween(1, 1000), randomLongBetween(1, 1000), - randomBoolean()); + randomBoolean() + ); }); historyStore.putAsync(record); @@ -176,9 +182,7 @@ private void assertContainsMap(String indexedDocument, Map map) assertContainsMap(indexedDocument, (Map) v); } if (v instanceof Iterable) { - ((Iterable) v).forEach(elem -> { - assertThat(indexedDocument, containsString(elem.toString())); - }); + ((Iterable) v).forEach(elem -> { assertThat(indexedDocument, containsString(elem.toString())); }); } else { assertThat(indexedDocument, containsString(v.toString())); } @@ -193,17 +197,10 @@ public static SnapshotLifecyclePolicy randomSnapshotLifecyclePolicy(String id) { config.put(randomAlphaOfLength(4), randomAlphaOfLength(4)); } } - return new SnapshotLifecyclePolicy(id, - randomAlphaOfLength(4), - randomSchedule(), - randomAlphaOfLength(4), - config, - null); + return new SnapshotLifecyclePolicy(id, randomAlphaOfLength(4), randomSchedule(), randomAlphaOfLength(4), config, null); } private static String randomSchedule() { - return randomIntBetween(0, 59) + " " + - randomIntBetween(0, 59) + " " + - randomIntBetween(0, 12) + " * * ?"; + return randomIntBetween(0, 59) + " " + randomIntBetween(0, 59) + " " + randomIntBetween(0, 12) + " * * ?"; } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/slm/history/SnapshotLifecycleTemplateRegistryTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/slm/history/SnapshotLifecycleTemplateRegistryTests.java index 6da9c38881456..8146a990de481 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/slm/history/SnapshotLifecycleTemplateRegistryTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/slm/history/SnapshotLifecycleTemplateRegistryTests.java @@ -24,18 +24,18 @@ import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.node.DiscoveryNodes; import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.TriFunction; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.test.ClusterServiceUtils; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.client.NoOpClient; import org.elasticsearch.threadpool.TestThreadPool; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.ilm.DeleteAction; import org.elasticsearch.xpack.core.ilm.IndexLifecycleMetadata; import org.elasticsearch.xpack.core.ilm.LifecycleAction; @@ -59,7 +59,6 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; -import static org.mockito.Mockito.when; import static org.elasticsearch.xpack.core.ilm.LifecycleSettings.SLM_HISTORY_INDEX_ENABLED_SETTING; import static org.elasticsearch.xpack.core.slm.history.SnapshotLifecycleTemplateRegistry.INDEX_TEMPLATE_VERSION; import static org.elasticsearch.xpack.core.slm.history.SnapshotLifecycleTemplateRegistry.SLM_POLICY_NAME; @@ -70,6 +69,7 @@ import static org.hamcrest.Matchers.instanceOf; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; public class SnapshotLifecycleTemplateRegistryTests extends ESTestCase { private SnapshotLifecycleTemplateRegistry registry; @@ -84,11 +84,17 @@ public void createRegistryAndClient() { client = new VerifyingClient(threadPool); clusterService = ClusterServiceUtils.createClusterService(threadPool); List entries = new ArrayList<>(ClusterModule.getNamedXWriteables()); - entries.addAll(Arrays.asList( - new NamedXContentRegistry.Entry(LifecycleType.class, new ParseField(TimeseriesLifecycleType.TYPE), - (p) -> TimeseriesLifecycleType.INSTANCE), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(RolloverAction.NAME), RolloverAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse))); + entries.addAll( + Arrays.asList( + new NamedXContentRegistry.Entry( + LifecycleType.class, + new ParseField(TimeseriesLifecycleType.TYPE), + (p) -> TimeseriesLifecycleType.INSTANCE + ), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(RolloverAction.NAME), RolloverAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse) + ) + ); xContentRegistry = new NamedXContentRegistry(entries); registry = new SnapshotLifecycleTemplateRegistry(Settings.EMPTY, clusterService, threadPool, client, xContentRegistry); } @@ -102,8 +108,13 @@ public void tearDown() throws Exception { public void testDisabledDoesNotAddTemplates() { Settings settings = Settings.builder().put(SLM_HISTORY_INDEX_ENABLED_SETTING.getKey(), false).build(); - SnapshotLifecycleTemplateRegistry disabledRegistry = new SnapshotLifecycleTemplateRegistry(settings, clusterService, threadPool, - client, xContentRegistry); + SnapshotLifecycleTemplateRegistry disabledRegistry = new SnapshotLifecycleTemplateRegistry( + settings, + clusterService, + threadPool, + client, + xContentRegistry + ); assertThat(disabledRegistry.getComposableTemplateConfigs(), hasSize(0)); assertThat(disabledRegistry.getPolicyConfigs(), hasSize(0)); } @@ -166,7 +177,8 @@ public void testPolicyAlreadyExists() { DiscoveryNodes nodes = DiscoveryNodes.builder().localNodeId("node").masterNodeId("node").add(node).build(); Map policyMap = new HashMap<>(); - List policies = registry.getPolicyConfigs().stream() + List policies = registry.getPolicyConfigs() + .stream() .map(policyConfig -> policyConfig.load(xContentRegistry)) .collect(Collectors.toList()); assertThat(policies, hasSize(1)); @@ -195,7 +207,8 @@ public void testPolicyAlreadyExistsButDiffers() throws IOException { Map policyMap = new HashMap<>(); String policyStr = "{\"phases\":{\"delete\":{\"min_age\":\"1m\",\"actions\":{\"delete\":{}}}}}"; - List policies = registry.getPolicyConfigs().stream() + List policies = registry.getPolicyConfigs() + .stream() .map(policyConfig -> policyConfig.load(xContentRegistry)) .collect(Collectors.toList()); assertThat(policies, hasSize(1)); @@ -213,8 +226,10 @@ public void testPolicyAlreadyExistsButDiffers() throws IOException { return null; }); - try (XContentParser parser = XContentType.JSON.xContent() - .createParser(xContentRegistry, LoggingDeprecationHandler.THROW_UNSUPPORTED_OPERATION, policyStr)) { + try ( + XContentParser parser = XContentType.JSON.xContent() + .createParser(xContentRegistry, LoggingDeprecationHandler.THROW_UNSUPPORTED_OPERATION, policyStr) + ) { LifecyclePolicy different = LifecyclePolicy.parse(parser, policy.getName()); policyMap.put(policy.getName(), different); ClusterChangedEvent event = createClusterChangedEvent(Collections.emptyMap(), policyMap, nodes); @@ -226,8 +241,10 @@ public void testThatVersionedOldTemplatesAreUpgraded() throws Exception { DiscoveryNode node = new DiscoveryNode("node", ESTestCase.buildNewFakeTransportAddress(), Version.CURRENT); DiscoveryNodes nodes = DiscoveryNodes.builder().localNodeId("node").masterNodeId("node").add(node).build(); - ClusterChangedEvent event = createClusterChangedEvent(Collections.singletonMap(SLM_TEMPLATE_NAME, INDEX_TEMPLATE_VERSION - 1), - nodes); + ClusterChangedEvent event = createClusterChangedEvent( + Collections.singletonMap(SLM_TEMPLATE_NAME, INDEX_TEMPLATE_VERSION - 1), + nodes + ); AtomicInteger calledTimes = new AtomicInteger(0); client.setVerifier((action, request, listener) -> verifyTemplateInstalled(calledTimes, action, request, listener)); registry.clusterChanged(event); @@ -245,13 +262,14 @@ public void testThatUnversionedOldTemplatesAreUpgraded() throws Exception { assertBusy(() -> assertThat(calledTimes.get(), equalTo(registry.getComposableTemplateConfigs().size()))); } - public void testSameOrHigherVersionTemplateNotUpgraded() throws Exception { DiscoveryNode node = new DiscoveryNode("node", ESTestCase.buildNewFakeTransportAddress(), Version.CURRENT); DiscoveryNodes nodes = DiscoveryNodes.builder().localNodeId("node").masterNodeId("node").add(node).build(); ClusterChangedEvent sameVersionEvent = createClusterChangedEvent( - Collections.singletonMap(SLM_TEMPLATE_NAME, INDEX_TEMPLATE_VERSION), nodes); + Collections.singletonMap(SLM_TEMPLATE_NAME, INDEX_TEMPLATE_VERSION), + nodes + ); AtomicInteger calledTimes = new AtomicInteger(0); client.setVerifier((action, request, listener) -> { if (action instanceof PutComposableIndexTemplateAction) { @@ -268,7 +286,9 @@ public void testSameOrHigherVersionTemplateNotUpgraded() throws Exception { registry.clusterChanged(sameVersionEvent); ClusterChangedEvent higherVersionEvent = createClusterChangedEvent( - Collections.singletonMap(SLM_TEMPLATE_NAME, INDEX_TEMPLATE_VERSION + randomIntBetween(1, 1000)), nodes); + Collections.singletonMap(SLM_TEMPLATE_NAME, INDEX_TEMPLATE_VERSION + randomIntBetween(1, 1000)), + nodes + ); registry.clusterChanged(higherVersionEvent); } @@ -276,7 +296,7 @@ public void testThatMissingMasterNodeDoesNothing() { DiscoveryNode localNode = new DiscoveryNode("node", ESTestCase.buildNewFakeTransportAddress(), Version.CURRENT); DiscoveryNodes nodes = DiscoveryNodes.builder().localNodeId("node").add(localNode).build(); - client.setVerifier((a,r,l) -> { + client.setVerifier((a, r, l) -> { fail("if the master is missing nothing should happen"); return null; }); @@ -287,19 +307,19 @@ public void testThatMissingMasterNodeDoesNothing() { public void testValidate() { assertFalse(registry.validate(createClusterState(Settings.EMPTY, Collections.emptyMap(), Collections.emptyMap(), null))); - assertFalse(registry.validate(createClusterState(Settings.EMPTY, - Collections.singletonMap(SLM_TEMPLATE_NAME, null), - Collections.emptyMap(), - null))); + assertFalse( + registry.validate( + createClusterState(Settings.EMPTY, Collections.singletonMap(SLM_TEMPLATE_NAME, null), Collections.emptyMap(), null) + ) + ); Map policyMap = new HashMap<>(); policyMap.put(SLM_POLICY_NAME, new LifecyclePolicy(SLM_POLICY_NAME, new HashMap<>())); assertFalse(registry.validate(createClusterState(Settings.EMPTY, Collections.emptyMap(), policyMap, null))); - assertTrue(registry.validate(createClusterState(Settings.EMPTY, - Collections.singletonMap(SLM_TEMPLATE_NAME, null), - policyMap, - null))); + assertTrue( + registry.validate(createClusterState(Settings.EMPTY, Collections.singletonMap(SLM_TEMPLATE_NAME, null), policyMap, null)) + ); } // ------------- @@ -320,9 +340,11 @@ public static class VerifyingClient extends NoOpClient { @Override @SuppressWarnings("unchecked") - protected void doExecute(ActionType action, - Request request, - ActionListener listener) { + protected void doExecute( + ActionType action, + Request request, + ActionListener listener + ) { try { listener.onResponse((Response) verifier.apply(action, request, listener)); } catch (Exception e) { @@ -337,7 +359,11 @@ public VerifyingClient setVerifier(TriFunction, ActionRequest, Act } private ActionResponse verifyTemplateInstalled( - AtomicInteger calledTimes, ActionType action, ActionRequest request, ActionListener listener) { + AtomicInteger calledTimes, + ActionType action, + ActionRequest request, + ActionListener listener + ) { if (action instanceof PutComposableIndexTemplateAction) { calledTimes.incrementAndGet(); assertThat(action, instanceOf(PutComposableIndexTemplateAction.class)); @@ -361,20 +387,29 @@ private ClusterChangedEvent createClusterChangedEvent(Map exist return createClusterChangedEvent(existingTemplates, Collections.emptyMap(), nodes); } - private ClusterChangedEvent createClusterChangedEvent(Map existingTemplates, - Map existingPolicies, - DiscoveryNodes nodes) { + private ClusterChangedEvent createClusterChangedEvent( + Map existingTemplates, + Map existingPolicies, + DiscoveryNodes nodes + ) { ClusterState cs = createClusterState(Settings.EMPTY, existingTemplates, existingPolicies, nodes); - ClusterChangedEvent realEvent = new ClusterChangedEvent("created-from-test", cs, - ClusterState.builder(new ClusterName("test")).build()); + ClusterChangedEvent realEvent = new ClusterChangedEvent( + "created-from-test", + cs, + ClusterState.builder(new ClusterName("test")).build() + ); ClusterChangedEvent event = spy(realEvent); when(event.localNodeMaster()).thenReturn(nodes.isLocalNodeElectedMaster()); return event; } - private ClusterState createClusterState(Settings nodeSettings, Map existingTemplates, - Map existingPolicies, DiscoveryNodes nodes) { + private ClusterState createClusterState( + Settings nodeSettings, + Map existingTemplates, + Map existingPolicies, + DiscoveryNodes nodes + ) { Map indexTemplates = new HashMap<>(); for (Map.Entry template : existingTemplates.entrySet()) { final ComposableIndexTemplate mockTemplate = mock(ComposableIndexTemplate.class); @@ -384,16 +419,19 @@ private ClusterState createClusterState(Settings nodeSettings, Map existingILMMeta = existingPolicies.entrySet().stream() + Map existingILMMeta = existingPolicies.entrySet() + .stream() .collect(Collectors.toMap(Map.Entry::getKey, e -> new LifecyclePolicyMetadata(e.getValue(), Collections.emptyMap(), 1, 1))); IndexLifecycleMetadata ilmMeta = new IndexLifecycleMetadata(existingILMMeta, OperationMode.RUNNING); return ClusterState.builder(new ClusterName("test")) - .metadata(Metadata.builder() - .indexTemplates(indexTemplates) - .transientSettings(nodeSettings) - .putCustom(IndexLifecycleMetadata.TYPE, ilmMeta) - .build()) + .metadata( + Metadata.builder() + .indexTemplates(indexTemplates) + .transientSettings(nodeSettings) + .putCustom(IndexLifecycleMetadata.TYPE, ilmMeta) + .build() + ) .blocks(new ClusterBlocks.Builder().build()) .nodes(nodes) .build(); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/spatial/SpatialFeatureSetUsageTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/spatial/SpatialFeatureSetUsageTests.java index f1a87e98fbf42..5313c3b44135a 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/spatial/SpatialFeatureSetUsageTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/spatial/SpatialFeatureSetUsageTests.java @@ -40,8 +40,7 @@ protected Writeable.Reader instanceReader() { } private SpatialStatsAction.Response randomStatsResponse() { - DiscoveryNode node = new DiscoveryNode("_node_id", - new TransportAddress(InetAddress.getLoopbackAddress(), 9300), Version.CURRENT); + DiscoveryNode node = new DiscoveryNode("_node_id", new TransportAddress(InetAddress.getLoopbackAddress(), 9300), Version.CURRENT); EnumCounters counters = new EnumCounters<>(SpatialStatsAction.Item.class); SpatialStatsAction.NodeResponse nodeResponse = new SpatialStatsAction.NodeResponse(node, counters); return new SpatialStatsAction.Response(new ClusterName("cluster_name"), List.of(nodeResponse), emptyList()); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ssl/CertParsingUtilsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ssl/CertParsingUtilsTests.java index dbc9ed50910ab..08f7a3fbaf889 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ssl/CertParsingUtilsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ssl/CertParsingUtilsTests.java @@ -41,8 +41,10 @@ public void testReadKeysCorrectly() throws Exception { assertThat(key, notNullValue()); assertThat(key, instanceOf(PrivateKey.class)); - PrivateKey privateKey = org.elasticsearch.common.ssl.PemUtils.readPrivateKey(getDataPath - ("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.pem"), "testnode"::toCharArray); + PrivateKey privateKey = org.elasticsearch.common.ssl.PemUtils.readPrivateKey( + getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.pem"), + "testnode"::toCharArray + ); assertThat(privateKey, notNullValue()); assertThat(privateKey, equalTo(key)); @@ -61,8 +63,11 @@ public void testReadCertsCorrectly() throws Exception { assertThat(certificate, instanceOf(X509Certificate.class)); Certificate pemCert; - try (InputStream input = - Files.newInputStream(getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt"))) { + try ( + InputStream input = Files.newInputStream( + getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt") + ) + ) { List certificateList = CertParsingUtils.readCertificates(input); assertThat(certificateList.size(), is(1)); pemCert = certificateList.get(0); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ssl/RestrictedTrustConfigTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ssl/RestrictedTrustConfigTests.java index 71760c81525b8..090ad015620f0 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ssl/RestrictedTrustConfigTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ssl/RestrictedTrustConfigTests.java @@ -14,13 +14,14 @@ import org.elasticsearch.test.ESTestCase; import org.hamcrest.Matchers; -import javax.net.ssl.X509ExtendedTrustManager; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.Collection; import java.util.List; +import javax.net.ssl.X509ExtendedTrustManager; + public class RestrictedTrustConfigTests extends ESTestCase { public void testDelegationOfFilesToMonitor() throws Exception { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ssl/RestrictedTrustManagerTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ssl/RestrictedTrustManagerTests.java index d529e4c123a9c..30d99a43ac49e 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ssl/RestrictedTrustManagerTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ssl/RestrictedTrustManagerTests.java @@ -12,7 +12,6 @@ import org.junit.Assert; import org.junit.Before; -import javax.net.ssl.X509ExtendedTrustManager; import java.io.IOException; import java.nio.file.FileVisitResult; import java.nio.file.Files; @@ -32,6 +31,7 @@ import java.util.regex.Pattern; import java.util.stream.Collectors; +import javax.net.ssl.X509ExtendedTrustManager; public class RestrictedTrustManagerTests extends ESTestCase { @@ -43,45 +43,52 @@ public class RestrictedTrustManagerTests extends ESTestCase { @Before public void readCertificates() throws GeneralSecurityException, IOException { - final Path caPath = getDataPath - ("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/nodes/ca.crt"); + final Path caPath = getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/nodes/ca.crt"); baseTrustManager = CertParsingUtils.getTrustManagerFromPEM(List.of(caPath)); certificates = new HashMap<>(); - Files.walkFileTree(getDataPath - ("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/nodes/self-signed"), new SimpleFileVisitor() { - - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { - try { - String fileName = file.getFileName().toString(); - if (fileName.endsWith(".crt")) { - certificates.put(fileName.replace(".crt", "/self"), CertParsingUtils - .readX509Certificates(Collections.singletonList(file))); + Files.walkFileTree( + getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/nodes/self-signed"), + new SimpleFileVisitor() { + + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + try { + String fileName = file.getFileName().toString(); + if (fileName.endsWith(".crt")) { + certificates.put( + fileName.replace(".crt", "/self"), + CertParsingUtils.readX509Certificates(Collections.singletonList(file)) + ); + } + return FileVisitResult.CONTINUE; + } catch (CertificateException e) { + throw new IOException("Failed to read X.509 Certificate from: " + file.toAbsolutePath().toString()); } - return FileVisitResult.CONTINUE; - } catch (CertificateException e) { - throw new IOException("Failed to read X.509 Certificate from: " + file.toAbsolutePath().toString()); } } - }); - - Files.walkFileTree(getDataPath - ("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/nodes/ca-signed"), new SimpleFileVisitor() { - - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { - try { - String fileName = file.getFileName().toString(); - if (fileName.endsWith(".crt")) { - certificates.put(fileName.replace(".crt", "/ca"), CertParsingUtils - .readX509Certificates(Collections.singletonList(file))); + ); + + Files.walkFileTree( + getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/nodes/ca-signed"), + new SimpleFileVisitor() { + + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + try { + String fileName = file.getFileName().toString(); + if (fileName.endsWith(".crt")) { + certificates.put( + fileName.replace(".crt", "/ca"), + CertParsingUtils.readX509Certificates(Collections.singletonList(file)) + ); + } + return FileVisitResult.CONTINUE; + } catch (CertificateException e) { + throw new IOException("Failed to read X.509 Certificate from: " + file.toAbsolutePath().toString()); } - return FileVisitResult.CONTINUE; - } catch (CertificateException e) { - throw new IOException("Failed to read X.509 Certificate from: " + file.toAbsolutePath().toString()); } } - }); + ); numberOfClusters = scaledRandomIntBetween(2, 8); numberOfNodes = scaledRandomIntBetween(2, 8); @@ -109,9 +116,7 @@ public void testTrustsWildcardCertificateName() throws Exception { public void testTrustWithRegexCertificateName() throws Exception { final int trustedNode = randomIntBetween(1, numberOfNodes); final List trustedNames = Collections.singletonList("/node" + trustedNode + ".cluster[0-9].elasticsearch/"); - final CertificateTrustRestrictions restrictions = new CertificateTrustRestrictions( - trustedNames - ); + final CertificateTrustRestrictions restrictions = new CertificateTrustRestrictions(trustedNames); final RestrictedTrustManager trustManager = new RestrictedTrustManager(baseTrustManager, restrictions); for (int cluster = 1; cluster <= numberOfClusters; cluster++) { for (int node = 1; node <= numberOfNodes; node++) { @@ -131,14 +136,17 @@ public void testThatDelegateTrustManagerIsRespected() throws Exception { if (cert.endsWith("/ca")) { assertTrusted(trustManager, cert); } else { - assertNotValid(trustManager, cert, inFipsJvm() ? "unable to process certificates: Unable to find certificate chain.": - "PKIX path building failed.*"); + assertNotValid( + trustManager, + cert, + inFipsJvm() ? "unable to process certificates: Unable to find certificate chain." : "PKIX path building failed.*" + ); } } } private void assertSingleClusterIsTrusted(int trustedCluster, RestrictedTrustManager trustManager, List trustedNames) - throws Exception { + throws Exception { for (int cluster = 1; cluster <= numberOfClusters; cluster++) { for (int node = 1; node <= numberOfNodes; node++) { final String certAlias = "n" + node + ".c" + cluster + "/ca"; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ssl/SSLConfigurationReloaderTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ssl/SSLConfigurationReloaderTests.java index 5dc5929d7543c..02f9a58d7947d 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ssl/SSLConfigurationReloaderTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ssl/SSLConfigurationReloaderTests.java @@ -71,6 +71,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Consumer; + import javax.net.ssl.SSLContext; import javax.net.ssl.SSLHandshakeException; import javax.net.ssl.SSLSession; @@ -91,8 +92,10 @@ public class SSLConfigurationReloaderTests extends ESTestCase { @Before public void setup() { threadPool = new TestThreadPool("reload tests"); - resourceWatcherService = - new ResourceWatcherService(Settings.builder().put("resource.reload.interval.high", "1s").build(), threadPool); + resourceWatcherService = new ResourceWatcherService( + Settings.builder().put("resource.reload.interval.high", "1s").build(), + threadPool + ); } @After @@ -124,7 +127,7 @@ public void testReloadingKeyStore() throws Exception { .setSecureSettings(secureSettings) .build(); final Environment env = TestEnvironment.newEnvironment(settings); - //Load HTTPClient only once. Client uses the same store as a truststore + // Load HTTPClient only once. Client uses the same store as a truststore try (CloseableHttpClient client = getSSLClient(keystorePath, "testnode")) { final Consumer keyMaterialPreChecks = (context) -> { try (MockWebServer server = new MockWebServer(context, true)) { @@ -149,8 +152,10 @@ public void testReloadingKeyStore() throws Exception { try (MockWebServer server = new MockWebServer(updatedContext, true)) { server.enqueue(new MockResponse().setResponseCode(200).setBody("body")); server.start(); - SSLHandshakeException sslException = expectThrows(SSLHandshakeException.class, () -> - privilegedConnect(() -> client.execute(new HttpGet("https://localhost:" + server.getPort())).close())); + SSLHandshakeException sslException = expectThrows( + SSLHandshakeException.class, + () -> privilegedConnect(() -> client.execute(new HttpGet("https://localhost:" + server.getPort())).close()) + ); assertThat(sslException.getCause().getMessage(), containsString("PKIX path validation failed")); } catch (Exception e) { throw new RuntimeException("Exception starting or connecting to the mock server", e); @@ -159,6 +164,7 @@ public void testReloadingKeyStore() throws Exception { validateSSLConfigurationIsReloaded(env, keyMaterialPreChecks, modifier, keyMaterialPostChecks); } } + /** * Tests the reloading of SSLContext when a PEM key and certificate are used. */ @@ -209,8 +215,10 @@ public void testPEMKeyConfigReloading() throws Exception { try (MockWebServer server = new MockWebServer(updatedContext, false)) { server.enqueue(new MockResponse().setResponseCode(200).setBody("body")); server.start(); - SSLHandshakeException sslException = expectThrows(SSLHandshakeException.class, () -> - privilegedConnect(() -> client.execute(new HttpGet("https://localhost:" + server.getPort())).close())); + SSLHandshakeException sslException = expectThrows( + SSLHandshakeException.class, + () -> privilegedConnect(() -> client.execute(new HttpGet("https://localhost:" + server.getPort())).close()) + ); assertThat(sslException.getCause().getMessage(), containsString("PKIX path validation failed")); } catch (Exception e) { throw new RuntimeException("Exception starting or connecting to the mock server", e); @@ -230,12 +238,10 @@ public void testReloadingTrustStore() throws Exception { Path trustStorePath = tempDir.resolve("testnode.jks"); Path updatedTruststorePath = tempDir.resolve("testnode_updated.jks"); Files.copy(getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.jks"), trustStorePath); - Files.copy(getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode_updated.jks"), - updatedTruststorePath); + Files.copy(getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode_updated.jks"), updatedTruststorePath); MockSecureSettings secureSettings = new MockSecureSettings(); secureSettings.setString("xpack.security.transport.ssl.truststore.secure_password", "testnode"); - final Settings settings = baseKeystoreSettings(tempDir, secureSettings) - .put("xpack.security.transport.ssl.enabled", true) + final Settings settings = baseKeystoreSettings(tempDir, secureSettings).put("xpack.security.transport.ssl.enabled", true) .put("xpack.security.transport.ssl.truststore.path", trustStorePath) .put("path.home", createTempDir()) .build(); @@ -261,8 +267,10 @@ public void testReloadingTrustStore() throws Exception { // Client's truststore doesn't contain the server's certificate anymore so SSLHandshake should fail final Consumer trustMaterialPostChecks = (updatedContext) -> { try (CloseableHttpClient client = createHttpClient(updatedContext)) { - SSLHandshakeException sslException = expectThrows(SSLHandshakeException.class, () -> - privilegedConnect(() -> client.execute(new HttpGet("https://localhost:" + server.getPort())).close())); + SSLHandshakeException sslException = expectThrows( + SSLHandshakeException.class, + () -> privilegedConnect(() -> client.execute(new HttpGet("https://localhost:" + server.getPort())).close()) + ); assertThat(sslException.getCause().getMessage(), containsString("PKIX path building failed")); } catch (Exception e) { throw new RuntimeException("Error closing CloseableHttpClient", e); @@ -284,8 +292,7 @@ public void testReloadingPEMTrustConfig() throws Exception { Files.copy(getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt"), serverCertPath); Files.copy(getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.pem"), serverKeyPath); Files.copy(getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode_updated.crt"), updatedCertPath); - Settings settings = baseKeystoreSettings(tempDir, null) - .put("xpack.security.transport.ssl.enabled", true) + Settings settings = baseKeystoreSettings(tempDir, null).put("xpack.security.transport.ssl.enabled", true) .putList("xpack.security.transport.ssl.certificate_authorities", serverCertPath.toString()) .put("path.home", createTempDir()) .build(); @@ -294,7 +301,7 @@ public void testReloadingPEMTrustConfig() throws Exception { try (MockWebServer server = getSslServer(serverKeyPath, serverCertPath, "testnode")) { final Consumer trustMaterialPreChecks = (context) -> { try (CloseableHttpClient client = createHttpClient(context)) { - privilegedConnect(() -> client.execute(new HttpGet("https://localhost:" + server.getPort())));//.close()); + privilegedConnect(() -> client.execute(new HttpGet("https://localhost:" + server.getPort())));// .close()); } catch (Exception e) { throw new RuntimeException("Exception connecting to the mock server", e); } @@ -311,8 +318,10 @@ public void testReloadingPEMTrustConfig() throws Exception { // Client doesn't trust the Server certificate anymore so SSLHandshake should fail final Consumer trustMaterialPostChecks = (updatedContext) -> { try (CloseableHttpClient client = createHttpClient(updatedContext)) { - SSLHandshakeException sslException = expectThrows(SSLHandshakeException.class, () -> - privilegedConnect(() -> client.execute(new HttpGet("https://localhost:" + server.getPort())).close())); + SSLHandshakeException sslException = expectThrows( + SSLHandshakeException.class, + () -> privilegedConnect(() -> client.execute(new HttpGet("https://localhost:" + server.getPort())).close()) + ); assertThat(sslException.getCause().getMessage(), containsString("PKIX path validation failed")); } catch (Exception e) { throw new RuntimeException("Error closing CloseableHttpClient", e); @@ -411,8 +420,7 @@ public void testReloadingPEMKeyConfigException() throws Exception { final SSLContext context = sslService.sslContextHolder(config).sslContext(); // truncate the file - try (OutputStream ignore = Files.newOutputStream(keyPath, StandardOpenOption.TRUNCATE_EXISTING)) { - } + try (OutputStream ignore = Files.newOutputStream(keyPath, StandardOpenOption.TRUNCATE_EXISTING)) {} latch.await(); assertNotNull(exceptionRef.get()); @@ -430,8 +438,7 @@ public void testTrustStoreReloadException() throws Exception { Files.copy(getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.jks"), trustStorePath); MockSecureSettings secureSettings = new MockSecureSettings(); secureSettings.setString("xpack.security.transport.ssl.truststore.secure_password", "testnode"); - Settings settings = baseKeystoreSettings(tempDir, secureSettings) - .put("xpack.security.transport.ssl.enabled", true) + Settings settings = baseKeystoreSettings(tempDir, secureSettings).put("xpack.security.transport.ssl.enabled", true) .put("xpack.security.transport.ssl.truststore.path", trustStorePath) .put("path.home", createTempDir()) .build(); @@ -455,8 +462,7 @@ public void testTrustStoreReloadException() throws Exception { final SSLContext context = sslService.sslContextHolder(config).sslContext(); // truncate the truststore - try (OutputStream ignore = Files.newOutputStream(trustStorePath, StandardOpenOption.TRUNCATE_EXISTING)) { - } + try (OutputStream ignore = Files.newOutputStream(trustStorePath, StandardOpenOption.TRUNCATE_EXISTING)) {} latch.await(); assertNotNull(exceptionRef.get()); @@ -472,8 +478,7 @@ public void testPEMTrustReloadException() throws Exception { Path tempDir = createTempDir(); Path clientCertPath = tempDir.resolve("testclient.crt"); Files.copy(getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testclient.crt"), clientCertPath); - Settings settings = baseKeystoreSettings(tempDir, null) - .put("xpack.security.transport.ssl.enabled", true) + Settings settings = baseKeystoreSettings(tempDir, null).put("xpack.security.transport.ssl.enabled", true) .putList("xpack.security.transport.ssl.certificate_authorities", clientCertPath.toString()) .put("path.home", createTempDir()) .build(); @@ -517,10 +522,10 @@ public void testPEMTrustReloadException() throws Exception { public void testFailureToReadFileDoesntFail() throws Exception { Path tempDir = createTempDir(); Path clientCertPath = tempDir.resolve("testclient.crt"); - Settings settings = baseKeystoreSettings(tempDir, null) - .putList("xpack.security.transport.ssl.certificate_authorities", clientCertPath.toString()) - .put("path.home", createTempDir()) - .build(); + Settings settings = baseKeystoreSettings(tempDir, null).putList( + "xpack.security.transport.ssl.certificate_authorities", + clientCertPath.toString() + ).put("path.home", createTempDir()).build(); Environment env = TestEnvironment.newEnvironment(settings); final ResourceWatcherService mockResourceWatcher = Mockito.mock(ResourceWatcherService.class); @@ -551,8 +556,12 @@ private Settings.Builder baseKeystoreSettings(Path tempDir, MockSecureSettings s .setSecureSettings(secureSettings); } - private void validateSSLConfigurationIsReloaded(Environment env, Consumer preChecks, - Runnable modificationFunction, Consumer postChecks) throws Exception { + private void validateSSLConfigurationIsReloaded( + Environment env, + Consumer preChecks, + Runnable modificationFunction, + Consumer postChecks + ) throws Exception { final CountDownLatch reloadLatch = new CountDownLatch(1); final SSLService sslService = new SSLService(env); final SslConfiguration config = sslService.getSSLConfiguration("xpack.security.transport.ssl"); @@ -590,9 +599,7 @@ private static MockWebServer getSslServer(Path keyStorePath, String keyStorePass try (InputStream is = Files.newInputStream(keyStorePath)) { keyStore.load(is, keyStorePass.toCharArray()); } - final SSLContext sslContext = new SSLContextBuilder() - .loadKeyMaterial(keyStore, keyStorePass.toCharArray()) - .build(); + final SSLContext sslContext = new SSLContextBuilder().loadKeyMaterial(keyStore, keyStorePass.toCharArray()).build(); MockWebServer server = new MockWebServer(sslContext, false); server.enqueue(new MockResponse().setResponseCode(200).setBody("body")); server.start(); @@ -602,11 +609,13 @@ private static MockWebServer getSslServer(Path keyStorePath, String keyStorePass private static MockWebServer getSslServer(Path keyPath, Path certPath, String password) throws GeneralSecurityException, IOException { KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); keyStore.load(null, password.toCharArray()); - keyStore.setKeyEntry("testnode_ec", PemUtils.readPrivateKey(keyPath, password::toCharArray), password.toCharArray(), - CertParsingUtils.readX509Certificates(Collections.singletonList(certPath))); - final SSLContext sslContext = new SSLContextBuilder() - .loadKeyMaterial(keyStore, password.toCharArray()) - .build(); + keyStore.setKeyEntry( + "testnode_ec", + PemUtils.readPrivateKey(keyPath, password::toCharArray), + password.toCharArray(), + CertParsingUtils.readX509Certificates(Collections.singletonList(certPath)) + ); + final SSLContext sslContext = new SSLContextBuilder().loadKeyMaterial(keyStore, password.toCharArray()).build(); MockWebServer server = new MockWebServer(sslContext, false); server.enqueue(new MockResponse().setResponseCode(200).setBody("body")); server.start(); @@ -614,15 +623,12 @@ private static MockWebServer getSslServer(Path keyPath, Path certPath, String pa } private static CloseableHttpClient getSSLClient(Path trustStorePath, String trustStorePass) throws KeyStoreException, - NoSuchAlgorithmException, - KeyManagementException, IOException, CertificateException { + NoSuchAlgorithmException, KeyManagementException, IOException, CertificateException { KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); try (InputStream is = Files.newInputStream(trustStorePath)) { trustStore.load(is, trustStorePass.toCharArray()); } - final SSLContext sslContext = new SSLContextBuilder() - .loadTrustMaterial(trustStore, null) - .build(); + final SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(trustStore, null).build(); return createHttpClient(sslContext); } @@ -631,27 +637,32 @@ private static CloseableHttpClient getSSLClient(Path trustStorePath, String trus * * @param trustedCertificatePaths The certificates this client trusts **/ - private static CloseableHttpClient getSSLClient(List trustedCertificatePaths) throws KeyStoreException, - NoSuchAlgorithmException, + private static CloseableHttpClient getSSLClient(List trustedCertificatePaths) throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException, IOException, CertificateException { KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); trustStore.load(null, null); for (Certificate cert : CertParsingUtils.readX509Certificates(trustedCertificatePaths)) { trustStore.setCertificateEntry(cert.toString(), cert); } - final SSLContext sslContext = new SSLContextBuilder() - .loadTrustMaterial(trustStore, null) - .build(); + final SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(trustStore, null).build(); return createHttpClient(sslContext); } private static CloseableHttpClient createHttpClient(SSLContext sslContext) { return HttpClients.custom() - .setConnectionManager(new PoolingHttpClientConnectionManager( - RegistryBuilder.create() - .register("http", PlainConnectionSocketFactory.getSocketFactory()) - .register("https", new SSLConnectionSocketFactory(sslContext, null, null, new DefaultHostnameVerifier())) - .build(), getHttpClientConnectionFactory(), null, null, -1, TimeUnit.MILLISECONDS)) + .setConnectionManager( + new PoolingHttpClientConnectionManager( + RegistryBuilder.create() + .register("http", PlainConnectionSocketFactory.getSocketFactory()) + .register("https", new SSLConnectionSocketFactory(sslContext, null, null, new DefaultHostnameVerifier())) + .build(), + getHttpClientConnectionFactory(), + null, + null, + -1, + TimeUnit.MILLISECONDS + ) + ) .build(); } @@ -736,8 +747,7 @@ public int getRemotePort() { @Override public void close() throws IOException { if (delegate.getSocket() instanceof SSLSocket) { - try (SSLSocket socket = (SSLSocket) delegate.getSocket()) { - } + try (SSLSocket socket = (SSLSocket) delegate.getSocket()) {} } delegate.close(); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ssl/SSLConfigurationSettingsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ssl/SSLConfigurationSettingsTests.java index 003663ccf3f17..305ad60217a75 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ssl/SSLConfigurationSettingsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ssl/SSLConfigurationSettingsTests.java @@ -11,9 +11,10 @@ import org.elasticsearch.common.ssl.SslClientAuthenticationMode; import org.elasticsearch.test.ESTestCase; +import java.util.Arrays; + import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.TrustManagerFactory; -import java.util.Arrays; import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.is; @@ -28,13 +29,20 @@ public void testParseCipherSettingsWithoutPrefix() { assertThat(ssl.ciphers.match("xpack.transport.security.ssl.cipher_suites"), is(false)); final Settings settings = Settings.builder() - .put("cipher_suites.0", "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256") - .put("cipher_suites.1", "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256") - .put("cipher_suites.2", "TLS_RSA_WITH_AES_128_CBC_SHA256") - .build(); - assertThat(ssl.ciphers.get(settings), is(Arrays.asList( - "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256", "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256", "TLS_RSA_WITH_AES_128_CBC_SHA256" - ))); + .put("cipher_suites.0", "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256") + .put("cipher_suites.1", "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256") + .put("cipher_suites.2", "TLS_RSA_WITH_AES_128_CBC_SHA256") + .build(); + assertThat( + ssl.ciphers.get(settings), + is( + Arrays.asList( + "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256", + "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256", + "TLS_RSA_WITH_AES_128_CBC_SHA256" + ) + ) + ); } public void testParseClientAuthWithPrefix() { @@ -43,8 +51,8 @@ public void testParseClientAuthWithPrefix() { assertThat(ssl.clientAuth.match("client_authentication"), is(false)); final Settings settings = Settings.builder() - .put("xpack.security.http.ssl.client_authentication", SslClientAuthenticationMode.OPTIONAL.name()) - .build(); + .put("xpack.security.http.ssl.client_authentication", SslClientAuthenticationMode.OPTIONAL.name()) + .build(); assertThat(ssl.clientAuth.get(settings).get(), is(SslClientAuthenticationMode.OPTIONAL)); } @@ -53,9 +61,7 @@ public void testParseKeystoreAlgorithmWithPrefix() { assertThat(ssl.x509KeyPair.keystoreAlgorithm.match("xpack.security.authc.realms.ldap1.ssl.keystore.algorithm"), is(true)); final String algo = randomAlphaOfLength(16); - final Settings settings = Settings.builder() - .put("xpack.security.authc.realms.ldap1.ssl.keystore.algorithm", algo) - .build(); + final Settings settings = Settings.builder().put("xpack.security.authc.realms.ldap1.ssl.keystore.algorithm", algo).build(); assertThat(ssl.x509KeyPair.keystoreAlgorithm.get(settings), is(algo)); } @@ -63,9 +69,7 @@ public void testParseProtocolsListWithPrefix() { final SSLConfigurationSettings ssl = SSLConfigurationSettings.withPrefix("ssl.", true); assertThat(ssl.supportedProtocols.match("ssl.supported_protocols"), is(true)); - final Settings settings = Settings.builder() - .putList("ssl.supported_protocols", "SSLv3", "SSLv2Hello", "SSLv2") - .build(); + final Settings settings = Settings.builder().putList("ssl.supported_protocols", "SSLv3", "SSLv2Hello", "SSLv2").build(); assertThat(ssl.supportedProtocols.get(settings), is(Arrays.asList("SSLv3", "SSLv2Hello", "SSLv2"))); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ssl/SSLServiceTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ssl/SSLServiceTests.java index d184ecfcf6f97..919e3ce709940 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ssl/SSLServiceTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ssl/SSLServiceTests.java @@ -38,19 +38,6 @@ import org.elasticsearch.xpack.core.ssl.cert.CertificateInfo; import org.junit.Before; -import javax.net.ssl.HostnameVerifier; -import javax.net.ssl.SSLContext; -import javax.net.ssl.SSLEngine; -import javax.net.ssl.SSLParameters; -import javax.net.ssl.SSLPeerUnverifiedException; -import javax.net.ssl.SSLSession; -import javax.net.ssl.SSLSessionContext; -import javax.net.ssl.SSLSocket; -import javax.net.ssl.SSLSocketFactory; -import javax.net.ssl.TrustManagerFactory; -import javax.net.ssl.X509ExtendedTrustManager; -import javax.net.ssl.X509TrustManager; - import java.nio.file.Path; import java.security.AccessController; import java.security.KeyStore; @@ -73,6 +60,19 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; +import javax.net.ssl.HostnameVerifier; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLEngine; +import javax.net.ssl.SSLParameters; +import javax.net.ssl.SSLPeerUnverifiedException; +import javax.net.ssl.SSLSession; +import javax.net.ssl.SSLSessionContext; +import javax.net.ssl.SSLSocket; +import javax.net.ssl.SSLSocketFactory; +import javax.net.ssl.TrustManagerFactory; +import javax.net.ssl.X509ExtendedTrustManager; +import javax.net.ssl.X509TrustManager; + import static org.elasticsearch.test.TestMatchers.throwableWithMessage; import static org.hamcrest.Matchers.arrayContainingInAnyOrder; import static org.hamcrest.Matchers.arrayWithSize; @@ -196,8 +196,9 @@ public void testThatSslContextCachingWorks() throws Exception { public void testThatKeyStoreAndKeyCanHaveDifferentPasswords() throws Exception { assumeFalse("Can't run in a FIPS JVM", inFipsJvm()); - Path differentPasswordsStore = - getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode-different-passwords.jks"); + Path differentPasswordsStore = getDataPath( + "/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode-different-passwords.jks" + ); MockSecureSettings secureSettings = new MockSecureSettings(); secureSettings.setString("xpack.security.transport.ssl.keystore.secure_password", "testnode"); secureSettings.setString("xpack.security.transport.ssl.keystore.secure_key_password", "testnode1"); @@ -214,8 +215,9 @@ public void testThatKeyStoreAndKeyCanHaveDifferentPasswords() throws Exception { public void testIncorrectKeyPasswordThrowsException() throws Exception { assumeFalse("Can't run in a FIPS JVM", inFipsJvm()); - Path differentPasswordsStore = - getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode-different-passwords.jks"); + Path differentPasswordsStore = getDataPath( + "/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode-different-passwords.jks" + ); try { MockSecureSettings secureSettings = new MockSecureSettings(); secureSettings.setString("xpack.security.transport.ssl.keystore.secure_password", "testnode"); @@ -269,7 +271,6 @@ public void testThatCreateSSLEngineWithOnlyTruststoreWorks() throws Exception { assertThat(sslEngine, notNullValue()); } - public void testCreateWithKeystoreIsValidForServer() throws Exception { assumeFalse("Can't run in a FIPS JVM, JKS keystores can't be used", inFipsJvm()); MockSecureSettings secureSettings = new MockSecureSettings(); @@ -349,8 +350,10 @@ public void testValidForServer() throws Exception { public void testGetVerificationMode() throws Exception { assumeFalse("Can't run in a FIPS JVM, TrustAllConfig is not a SunJSSE TrustManagers", inFipsJvm()); SSLService sslService = new SSLService(env); - assertThat(sslService.getSSLConfiguration("xpack.security.transport.ssl").getVerificationMode(), - is(XPackSettings.VERIFICATION_MODE_DEFAULT)); + assertThat( + sslService.getSSLConfiguration("xpack.security.transport.ssl").getVerificationMode(), + is(XPackSettings.VERIFICATION_MODE_DEFAULT) + ); Settings settings = Settings.builder() .put("xpack.security.transport.ssl.enabled", false) @@ -417,8 +420,10 @@ public void testThatTruststorePasswordIsRequired() throws Exception { .put("xpack.security.transport.ssl.truststore.path", testnodeStore) .put("xpack.security.transport.ssl.truststore.type", testnodeStoreType) .build(); - ElasticsearchException e = - expectThrows(ElasticsearchException.class, () -> new SSLService(TestEnvironment.newEnvironment(buildEnvSettings(settings)))); + ElasticsearchException e = expectThrows( + ElasticsearchException.class, + () -> new SSLService(TestEnvironment.newEnvironment(buildEnvSettings(settings))) + ); assertThat(e, throwableWithMessage(startsWith("failed to load SSL configuration [xpack.security.transport.ssl] - "))); assertThat(e.getCause(), throwableWithMessage(containsString("incorrect password"))); } @@ -429,8 +434,10 @@ public void testThatKeystorePasswordIsRequired() throws Exception { .put("xpack.security.transport.ssl.keystore.path", testnodeStore) .put("xpack.security.transport.ssl.keystore.type", testnodeStoreType) .build(); - ElasticsearchException e = - expectThrows(ElasticsearchException.class, () -> new SSLService(TestEnvironment.newEnvironment(buildEnvSettings(settings)))); + ElasticsearchException e = expectThrows( + ElasticsearchException.class, + () -> new SSLService(TestEnvironment.newEnvironment(buildEnvSettings(settings))) + ); assertThat(e, throwableWithMessage(startsWith("failed to load SSL configuration [xpack.security.transport.ssl] - "))); assertThat(e.getCause(), throwableWithMessage(containsString("incorrect password"))); } @@ -464,12 +471,18 @@ public void testInvalidCiphersOnlyThrowsException() throws Exception { .put("xpack.security.transport.ssl.certificate", testnodeCert) .put("xpack.security.transport.ssl.key", testnodeKey) .setSecureSettings(secureSettings) - .putList("xpack.security.transport.ssl.cipher_suites", new String[]{"foo", "bar"}) + .putList("xpack.security.transport.ssl.cipher_suites", new String[] { "foo", "bar" }) .build(); - ElasticsearchException e = - expectThrows(ElasticsearchException.class, () -> new SSLService(TestEnvironment.newEnvironment(buildEnvSettings(settings)))); - assertThat(e, throwableWithMessage( - "failed to load SSL configuration [xpack.security.transport.ssl] - none of the ciphers [foo, bar] are supported by this JVM")); + ElasticsearchException e = expectThrows( + ElasticsearchException.class, + () -> new SSLService(TestEnvironment.newEnvironment(buildEnvSettings(settings))) + ); + assertThat( + e, + throwableWithMessage( + "failed to load SSL configuration [xpack.security.transport.ssl] - none of the ciphers [foo, bar] are supported by this JVM" + ) + ); assertThat(e.getCause(), throwableWithMessage("none of the ciphers [foo, bar] are supported by this JVM")); } @@ -548,9 +561,9 @@ public void testSSLStrategy() { SslConfiguration sslConfig = SslSettingsLoader.load(settings, null, env); SSLParameters sslParameters = mock(SSLParameters.class); SSLContext sslContext = mock(SSLContext.class); - String[] protocols = new String[]{"protocols"}; - String[] ciphers = new String[]{"ciphers!!!"}; - String[] supportedCiphers = new String[]{"supported ciphers"}; + String[] protocols = new String[] { "protocols" }; + String[] ciphers = new String[] { "ciphers!!!" }; + String[] supportedCiphers = new String[] { "supported ciphers" }; List requestedCiphers = List.of("INVALID_CIPHER"); SSLIOSessionStrategy sslStrategy = mock(SSLIOSessionStrategy.class); @@ -604,8 +617,7 @@ public void testGetConfigurationByContextName() throws Exception { "xpack.security.authc.realms.ldap.realm1.ssl", "xpack.security.authc.realms.saml.realm2.ssl", "xpack.monitoring.exporters.mon1.ssl", - "xpack.monitoring.exporters.mon2.ssl" - }; + "xpack.monitoring.exporters.mon2.ssl" }; assumeTrue("Not enough cipher suites are available to support this test", getCipherSuites.length >= contextNames.length); @@ -620,13 +632,10 @@ public void testGetConfigurationByContextName() throws Exception { builder.put(prefix + ".enabled", true); } secureSettings.setString(prefix + ".keystore.secure_password", "testnode"); - builder.put(prefix + ".keystore.path", testnodeStore) - .putList(prefix + ".cipher_suites", cipher.next()); + builder.put(prefix + ".keystore.path", testnodeStore).putList(prefix + ".cipher_suites", cipher.next()); } - final Settings settings = builder - .setSecureSettings(secureSettings) - .build(); + final Settings settings = builder.setSecureSettings(secureSettings).build(); SSLService sslService = new SSLService(TestEnvironment.newEnvironment(buildEnvSettings(settings))); for (int i = 0; i < contextNames.length; i++) { @@ -663,8 +672,10 @@ public void testReadCertificateInformation() throws Exception { final SSLService sslService = new SSLService(TestEnvironment.newEnvironment(buildEnvSettings(settings))); final List certificates = new ArrayList<>(sslService.getLoadedCertificates()); assertThat(certificates, iterableWithSize(18)); - Collections.sort(certificates, - Comparator.comparing((CertificateInfo c) -> c.alias() == null ? "" : c.alias()).thenComparing(CertificateInfo::path)); + Collections.sort( + certificates, + Comparator.comparing((CertificateInfo c) -> c.alias() == null ? "" : c.alias()).thenComparing(CertificateInfo::path) + ); final Iterator iterator = certificates.iterator(); @@ -862,8 +873,7 @@ public Enumeration getIds() { } @Override - public void setSessionTimeout(int seconds) throws IllegalArgumentException { - } + public void setSessionTimeout(int seconds) throws IllegalArgumentException {} @Override public int getSessionTimeout() { @@ -871,8 +881,7 @@ public int getSessionTimeout() { } @Override - public void setSessionCacheSize(int size) throws IllegalArgumentException { - } + public void setSessionCacheSize(int size) throws IllegalArgumentException {} @Override public int getSessionCacheSize() { @@ -992,8 +1001,11 @@ public void cancelled() { private CloseableHttpAsyncClient getAsyncHttpClient(SSLIOSessionStrategy sslStrategy) throws Exception { try { - return AccessController.doPrivileged((PrivilegedExceptionAction) - () -> HttpAsyncClientBuilder.create().setSSLStrategy(sslStrategy).build()); + return AccessController.doPrivileged( + (PrivilegedExceptionAction) () -> HttpAsyncClientBuilder.create() + .setSSLStrategy(sslStrategy) + .build() + ); } catch (PrivilegedActionException e) { throw (Exception) e.getCause(); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ssl/SslSettingsLoaderTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ssl/SslSettingsLoaderTests.java index a0349c85b23ad..e21a91772c809 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ssl/SslSettingsLoaderTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ssl/SslSettingsLoaderTests.java @@ -28,6 +28,7 @@ import java.security.cert.X509Certificate; import java.util.Arrays; import java.util.stream.Collectors; + import javax.net.ssl.KeyManager; import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.TrustManager; @@ -73,10 +74,7 @@ public void testThatOnlyKeystoreInSettingsSetsTruststoreSettings() { final Path path = getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.jks"); MockSecureSettings secureSettings = new MockSecureSettings(); secureSettings.setString("keystore.secure_password", "testnode"); - Settings settings = Settings.builder() - .put("keystore.path", path) - .setSecureSettings(secureSettings) - .build(); + Settings settings = Settings.builder().put("keystore.path", path).setSecureSettings(secureSettings).build(); // Pass settings in as component settings SslConfiguration sslConfiguration = getSslConfiguration(settings); assertThat(sslConfiguration.getKeyConfig(), instanceOf(StoreKeyConfig.class)); @@ -99,10 +97,7 @@ public void testFilterAppliedToKeystore() { final Path path = getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.p12"); MockSecureSettings secureSettings = new MockSecureSettings(); secureSettings.setString("keystore.secure_password", "testnode"); - Settings settings = Settings.builder() - .put("keystore.path", path) - .setSecureSettings(secureSettings) - .build(); + Settings settings = Settings.builder().put("keystore.path", path).setSecureSettings(secureSettings).build(); final SslConfiguration sslConfiguration = SslSettingsLoader.load( settings, null, @@ -144,9 +139,7 @@ public void testKeystorePassword() { StoreKeyConfig ksKeyInfo = (StoreKeyConfig) sslConfiguration.getKeyConfig(); assertThat( ksKeyInfo, - equalTo( - new StoreKeyConfig("path", PASSWORD, "type", null, PASSWORD, KEY_MGR_ALGORITHM, environment.configFile()) - ) + equalTo(new StoreKeyConfig("path", PASSWORD, "type", null, PASSWORD, KEY_MGR_ALGORITHM, environment.configFile())) ); } @@ -161,12 +154,9 @@ public void testKeystorePasswordBackcompat() { StoreKeyConfig ksKeyInfo = (StoreKeyConfig) sslConfiguration.getKeyConfig(); assertThat( ksKeyInfo, - equalTo( - new StoreKeyConfig("path", PASSWORD, "type", null, PASSWORD, KEY_MGR_ALGORITHM, environment.configFile()) - ) + equalTo(new StoreKeyConfig("path", PASSWORD, "type", null, PASSWORD, KEY_MGR_ALGORITHM, environment.configFile())) ); - assertSettingDeprecationsAndWarnings(new Setting[]{ - configurationSettings.x509KeyPair.legacyKeystorePassword}); + assertSettingDeprecationsAndWarnings(new Setting[] { configurationSettings.x509KeyPair.legacyKeystorePassword }); } public void testKeystoreKeyPassword() { @@ -183,9 +173,7 @@ public void testKeystoreKeyPassword() { StoreKeyConfig ksKeyInfo = (StoreKeyConfig) sslConfiguration.getKeyConfig(); assertThat( ksKeyInfo, - equalTo( - new StoreKeyConfig("path", PASSWORD, "type", null, KEYPASS, KEY_MGR_ALGORITHM, environment.configFile()) - ) + equalTo(new StoreKeyConfig("path", PASSWORD, "type", null, KEYPASS, KEY_MGR_ALGORITHM, environment.configFile())) ); } @@ -201,32 +189,26 @@ public void testKeystoreKeyPasswordBackcompat() { StoreKeyConfig ksKeyInfo = (StoreKeyConfig) sslConfiguration.getKeyConfig(); assertThat( ksKeyInfo, - equalTo( - new StoreKeyConfig("path", PASSWORD, "type", null, KEYPASS, KEY_MGR_ALGORITHM, environment.configFile()) - ) + equalTo(new StoreKeyConfig("path", PASSWORD, "type", null, KEYPASS, KEY_MGR_ALGORITHM, environment.configFile())) + ); + assertSettingDeprecationsAndWarnings( + new Setting[] { + configurationSettings.x509KeyPair.legacyKeystorePassword, + configurationSettings.x509KeyPair.legacyKeystoreKeyPassword } ); - assertSettingDeprecationsAndWarnings(new Setting[]{ - configurationSettings.x509KeyPair.legacyKeystorePassword, - configurationSettings.x509KeyPair.legacyKeystoreKeyPassword - }); } public void testInferKeystoreTypeFromJksFile() { MockSecureSettings secureSettings = new MockSecureSettings(); secureSettings.setString("keystore.secure_password", "password"); secureSettings.setString("keystore.secure_key_password", "keypass"); - Settings settings = Settings.builder() - .put("keystore.path", "xpack/tls/path.jks") - .setSecureSettings(secureSettings) - .build(); + Settings settings = Settings.builder().put("keystore.path", "xpack/tls/path.jks").setSecureSettings(secureSettings).build(); SslConfiguration sslConfiguration = getSslConfiguration(settings); assertThat(sslConfiguration.getKeyConfig(), instanceOf(StoreKeyConfig.class)); StoreKeyConfig ksKeyInfo = (StoreKeyConfig) sslConfiguration.getKeyConfig(); assertThat( ksKeyInfo, - equalTo( - new StoreKeyConfig("xpack/tls/path.jks", PASSWORD, "jks", null, KEYPASS, KEY_MGR_ALGORITHM, environment.configFile()) - ) + equalTo(new StoreKeyConfig("xpack/tls/path.jks", PASSWORD, "jks", null, KEYPASS, KEY_MGR_ALGORITHM, environment.configFile())) ); } @@ -236,18 +218,13 @@ public void testInferKeystoreTypeFromPkcs12File() { secureSettings.setString("keystore.secure_password", "password"); secureSettings.setString("keystore.secure_key_password", "keypass"); final String path = "xpack/tls/path." + ext; - Settings settings = Settings.builder() - .put("keystore.path", path) - .setSecureSettings(secureSettings) - .build(); + Settings settings = Settings.builder().put("keystore.path", path).setSecureSettings(secureSettings).build(); SslConfiguration sslConfiguration = getSslConfiguration(settings); assertThat(sslConfiguration.getKeyConfig(), instanceOf(StoreKeyConfig.class)); StoreKeyConfig ksKeyInfo = (StoreKeyConfig) sslConfiguration.getKeyConfig(); assertThat( ksKeyInfo, - equalTo( - new StoreKeyConfig(path, PASSWORD, "PKCS12", null, KEYPASS, KEY_MGR_ALGORITHM, environment.configFile()) - ) + equalTo(new StoreKeyConfig(path, PASSWORD, "PKCS12", null, KEYPASS, KEY_MGR_ALGORITHM, environment.configFile())) ); } @@ -255,10 +232,7 @@ public void testInferKeystoreTypeFromUnrecognised() { MockSecureSettings secureSettings = new MockSecureSettings(); secureSettings.setString("keystore.secure_password", "password"); secureSettings.setString("keystore.secure_key_password", "keypass"); - Settings settings = Settings.builder() - .put("keystore.path", "xpack/tls/path.foo") - .setSecureSettings(secureSettings) - .build(); + Settings settings = Settings.builder().put("keystore.path", "xpack/tls/path.foo").setSecureSettings(secureSettings).build(); SslConfiguration sslConfiguration = getSslConfiguration(settings); assertThat(sslConfiguration.getKeyConfig(), instanceOf(StoreKeyConfig.class)); StoreKeyConfig ksKeyInfo = (StoreKeyConfig) sslConfiguration.getKeyConfig(); @@ -299,12 +273,8 @@ public void testThatEmptySettingsAreEqual() { } public void testThatSettingsWithDifferentKeystoresAreNotEqual() { - SslConfiguration sslConfiguration = getSslConfiguration(Settings.builder() - .put("keystore.path", "path") - .build()); - SslConfiguration sslConfiguration1 = getSslConfiguration(Settings.builder() - .put("keystore.path", "path1") - .build()); + SslConfiguration sslConfiguration = getSslConfiguration(Settings.builder().put("keystore.path", "path").build()); + SslConfiguration sslConfiguration1 = getSslConfiguration(Settings.builder().put("keystore.path", "path1").build()); assertThat(sslConfiguration.equals(sslConfiguration1), is(equalTo(false))); assertThat(sslConfiguration1.equals(sslConfiguration), is(equalTo(false))); assertThat(sslConfiguration.equals(sslConfiguration), is(equalTo(true))); @@ -312,12 +282,8 @@ public void testThatSettingsWithDifferentKeystoresAreNotEqual() { } public void testThatSettingsWithDifferentTruststoresAreNotEqual() { - SslConfiguration sslConfiguration = getSslConfiguration(Settings.builder() - .put("truststore.path", "/trust") - .build()); - SslConfiguration sslConfiguration1 = getSslConfiguration(Settings.builder() - .put("truststore.path", "/truststore") - .build()); + SslConfiguration sslConfiguration = getSslConfiguration(Settings.builder().put("truststore.path", "/trust").build()); + SslConfiguration sslConfiguration1 = getSslConfiguration(Settings.builder().put("truststore.path", "/truststore").build()); assertThat(sslConfiguration.equals(sslConfiguration1), is(equalTo(false))); assertThat(sslConfiguration1.equals(sslConfiguration), is(equalTo(false))); assertThat(sslConfiguration.equals(sslConfiguration), is(equalTo(true))); @@ -331,22 +297,14 @@ public void testThatEmptySettingsHaveSameHashCode() { } public void testThatSettingsWithDifferentKeystoresHaveDifferentHashCode() { - SslConfiguration sslConfiguration = getSslConfiguration(Settings.builder() - .put("keystore.path", "path") - .build()); - SslConfiguration sslConfiguration1 = getSslConfiguration(Settings.builder() - .put("keystore.path", "path1") - .build()); + SslConfiguration sslConfiguration = getSslConfiguration(Settings.builder().put("keystore.path", "path").build()); + SslConfiguration sslConfiguration1 = getSslConfiguration(Settings.builder().put("keystore.path", "path1").build()); assertThat(sslConfiguration.hashCode(), is(not(equalTo(sslConfiguration1.hashCode())))); } public void testThatSettingsWithDifferentTruststoresHaveDifferentHashCode() { - SslConfiguration sslConfiguration = getSslConfiguration(Settings.builder() - .put("truststore.path", "/trust") - .build()); - SslConfiguration sslConfiguration1 = getSslConfiguration(Settings.builder() - .put("truststore.path", "/truststore") - .build()); + SslConfiguration sslConfiguration = getSslConfiguration(Settings.builder().put("truststore.path", "/trust").build()); + SslConfiguration sslConfiguration1 = getSslConfiguration(Settings.builder().put("truststore.path", "/truststore").build()); assertThat(sslConfiguration.hashCode(), is(not(equalTo(sslConfiguration1.hashCode())))); } @@ -369,11 +327,9 @@ public void testPEMFile() { public void testPEMFileBackcompat() { Settings settings = Settings.builder() - .put("key", - getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.pem")) + .put("key", getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.pem")) .put("key_passphrase", "testnode") - .put("certificate", - getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt")) + .put("certificate", getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt")) .build(); SslConfiguration config = getSslConfiguration(settings); @@ -382,7 +338,7 @@ public void testPEMFileBackcompat() { KeyManager keyManager = keyConfig.createKeyManager(); assertNotNull(keyManager); assertCombiningTrustConfigContainsCorrectIssuers(config); - assertSettingDeprecationsAndWarnings(new Setting[]{configurationSettings.x509KeyPair.legacyKeyPassword}); + assertSettingDeprecationsAndWarnings(new Setting[] { configurationSettings.x509KeyPair.legacyKeyPassword }); } public void testPEMKeyAndTrustFiles() { @@ -391,9 +347,11 @@ public void testPEMKeyAndTrustFiles() { Settings settings = Settings.builder() .put("key", getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.pem")) .put("certificate", getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt")) - .putList("certificate_authorities", + .putList( + "certificate_authorities", getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt").toString(), - getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testclient.crt").toString()) + getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testclient.crt").toString() + ) .setSecureSettings(secureSettings) .build(); @@ -413,9 +371,11 @@ public void testPEMKeyAndTrustFilesBackcompat() { .put("key", getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.pem")) .put("key_passphrase", "testnode") .put("certificate", getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt")) - .putList("certificate_authorities", + .putList( + "certificate_authorities", getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt").toString(), - getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testclient.crt").toString()) + getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testclient.crt").toString() + ) .build(); SslConfiguration config = getSslConfiguration(settings); @@ -427,32 +387,41 @@ public void testPEMKeyAndTrustFilesBackcompat() { assertThat(config.getTrustConfig(), instanceOf(PemTrustConfig.class)); TrustManager trustManager = keyConfig.asTrustConfig().createTrustManager(); assertNotNull(trustManager); - assertSettingDeprecationsAndWarnings(new Setting[]{configurationSettings.x509KeyPair.legacyKeyPassword}); + assertSettingDeprecationsAndWarnings(new Setting[] { configurationSettings.x509KeyPair.legacyKeyPassword }); } public void testExplicitlyConfigured() { assertThat(SslSettingsLoader.load(Settings.EMPTY, null, environment).isExplicitlyConfigured(), is(false)); - assertThat(SslSettingsLoader.load( - Settings.builder() - .put("cluster.name", randomAlphaOfLength(8)) - .put("xpack.security.transport.ssl.certificate", randomAlphaOfLength(12)) - .put("xpack.security.transport.ssl.key", randomAlphaOfLength(12)) - .build(), - "xpack.http.ssl.", - environment - ).isExplicitlyConfigured(), is(false)); - - assertThat(SslSettingsLoader.load( - Settings.builder().put("verification_mode", randomFrom(SslVerificationMode.values()).name()).build(), - null, - environment - ).isExplicitlyConfigured(), is(true)); - - assertThat(SslSettingsLoader.load( - Settings.builder().putList("xpack.security.transport.ssl.truststore.path", "truststore.p12").build(), - "xpack.security.transport.ssl.", - environment - ).isExplicitlyConfigured(), is(true)); + assertThat( + SslSettingsLoader.load( + Settings.builder() + .put("cluster.name", randomAlphaOfLength(8)) + .put("xpack.security.transport.ssl.certificate", randomAlphaOfLength(12)) + .put("xpack.security.transport.ssl.key", randomAlphaOfLength(12)) + .build(), + "xpack.http.ssl.", + environment + ).isExplicitlyConfigured(), + is(false) + ); + + assertThat( + SslSettingsLoader.load( + Settings.builder().put("verification_mode", randomFrom(SslVerificationMode.values()).name()).build(), + null, + environment + ).isExplicitlyConfigured(), + is(true) + ); + + assertThat( + SslSettingsLoader.load( + Settings.builder().putList("xpack.security.transport.ssl.truststore.path", "truststore.p12").build(), + "xpack.security.transport.ssl.", + environment + ).isExplicitlyConfigured(), + is(true) + ); } private void assertCombiningTrustConfigContainsCorrectIssuers(SslConfiguration sslConfig) { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ssl/TransportTLSBootstrapCheckTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ssl/TransportTLSBootstrapCheckTests.java index bb335b16eed17..0851078b52e12 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ssl/TransportTLSBootstrapCheckTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ssl/TransportTLSBootstrapCheckTests.java @@ -19,10 +19,16 @@ public class TransportTLSBootstrapCheckTests extends AbstractBootstrapCheckTestCase { public void testBootstrapCheckOnEmptyMetadata() { assertTrue(new TransportTLSBootstrapCheck().check(emptyContext).isFailure()); - assertTrue(new TransportTLSBootstrapCheck().check(createTestContext(Settings.builder().put("xpack.security.transport.ssl.enabled" - , false).build(), Metadata.EMPTY_METADATA)).isFailure()); - assertTrue(new TransportTLSBootstrapCheck().check(createTestContext(Settings.builder().put("xpack.security.transport.ssl.enabled" - , true).build(), Metadata.EMPTY_METADATA)).isSuccess()); + assertTrue( + new TransportTLSBootstrapCheck().check( + createTestContext(Settings.builder().put("xpack.security.transport.ssl.enabled", false).build(), Metadata.EMPTY_METADATA) + ).isFailure() + ); + assertTrue( + new TransportTLSBootstrapCheck().check( + createTestContext(Settings.builder().put("xpack.security.transport.ssl.enabled", true).build(), Metadata.EMPTY_METADATA) + ).isSuccess() + ); } public void testBootstrapCheckFailureOnAnyLicense() throws Exception { @@ -46,10 +52,12 @@ public void testBootstrapCheckFailureOnAnyLicense() throws Exception { final BootstrapCheck.BootstrapCheckResult result = runBootstrapCheck(mode, settings); assertTrue("Expected bootstrap failure", result.isFailure()); - assertEquals("Transport SSL must be enabled if security is enabled. Please set " + - "[xpack.security.transport.ssl.enabled] to [true] or disable security by setting " + - "[xpack.security.enabled] to [false]", - result.getMessage()); + assertEquals( + "Transport SSL must be enabled if security is enabled. Please set " + + "[xpack.security.transport.ssl.enabled] to [true] or disable security by setting " + + "[xpack.security.enabled] to [false]", + result.getMessage() + ); } public void testBootstrapCheckSucceedsWithTlsEnabledOnAnyLicense() throws Exception { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ssl/cert/CertificateInfoTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ssl/cert/CertificateInfoTests.java index d96be36ea6ae3..29f00e99a7486 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ssl/cert/CertificateInfoTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ssl/cert/CertificateInfoTests.java @@ -10,7 +10,6 @@ import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.ssl.CertParsingUtils; - import java.io.IOException; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; @@ -50,9 +49,9 @@ public void testCompareTo() throws Exception { } private X509Certificate readSampleCertificate() throws CertificateException, IOException { - return CertParsingUtils. - readX509Certificates(Collections.singletonList(getDataPath - ("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt")))[0]; + return CertParsingUtils.readX509Certificates( + Collections.singletonList(getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt")) + )[0]; } private CertificateInfo serializeAndDeserialize(CertificateInfo cert1) throws IOException { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/template/TemplateUtilsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/template/TemplateUtilsTests.java index ec00e3d0ef4dd..9d867381559e0 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/template/TemplateUtilsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/template/TemplateUtilsTests.java @@ -34,16 +34,25 @@ public void testLoadTemplate() { assertThat(source, notNullValue()); assertThat(source.length(), greaterThan(0)); - assertTemplate(source, equalTo("{\n" + - " \"index_patterns\": \".monitoring-data-" + version + "\",\n" + - " \"mappings\": {\n" + - " \"doc\": {\n" + - " \"_meta\": {\n" + - " \"template.version\": \"" + version + "\"\n" + - " }\n" + - " }\n" + - " }\n" + - "}\n")); + assertTemplate( + source, + equalTo( + "{\n" + + " \"index_patterns\": \".monitoring-data-" + + version + + "\",\n" + + " \"mappings\": {\n" + + " \"doc\": {\n" + + " \"_meta\": {\n" + + " \"template.version\": \"" + + version + + "\"\n" + + " }\n" + + " }\n" + + " }\n" + + "}\n" + ) + ); } public void testLoadTemplate_GivenTemplateWithVariables() { @@ -52,25 +61,38 @@ public void testLoadTemplate_GivenTemplateWithVariables() { variables.put("test.template.field_1", "test_field_1"); variables.put("test.template.field_2", "\"test_field_2\": {\"type\": \"long\"}"); - String source = TemplateUtils.loadTemplate(TEST_TEMPLATE_WITH_VARIABLES, String.valueOf(version), - "test.template.version", variables); + String source = TemplateUtils.loadTemplate( + TEST_TEMPLATE_WITH_VARIABLES, + String.valueOf(version), + "test.template.version", + variables + ); assertThat(source, notNullValue()); assertThat(source.length(), greaterThan(0)); - assertTemplate(source, equalTo("{\n" + - " \"index_patterns\": \".test-" + version + "\",\n" + - " \"mappings\": {\n" + - " \"doc\": {\n" + - " \"_meta\": {\n" + - " \"template.version\": \"" + version + "\"\n" + - " },\n" + - " \"properties\": {\n" + - " \"test_field_1\": {\"type\": \"keyword\"},\n" + - " \"test_field_2\": {\"type\": \"long\"}\n" + - " }\n" + - " }\n" + - " }\n" + - "}\n")); + assertTemplate( + source, + equalTo( + "{\n" + + " \"index_patterns\": \".test-" + + version + + "\",\n" + + " \"mappings\": {\n" + + " \"doc\": {\n" + + " \"_meta\": {\n" + + " \"template.version\": \"" + + version + + "\"\n" + + " },\n" + + " \"properties\": {\n" + + " \"test_field_1\": {\"type\": \"keyword\"},\n" + + " \"test_field_2\": {\"type\": \"long\"}\n" + + " }\n" + + " }\n" + + " }\n" + + "}\n" + ) + ); } public void testLoad() throws IOException { @@ -86,14 +108,15 @@ public void testValidateNullSource() { } public void testValidateEmptySource() { - ElasticsearchParseException exception = expectThrows(ElasticsearchParseException.class, - () -> TemplateUtils.validate("")); + ElasticsearchParseException exception = expectThrows(ElasticsearchParseException.class, () -> TemplateUtils.validate("")); assertThat(exception.getMessage(), is("Template must not be empty")); } public void testValidateInvalidSource() { - ElasticsearchParseException exception = expectThrows(ElasticsearchParseException.class, - () -> TemplateUtils.validate("{\"foo\": \"bar")); + ElasticsearchParseException exception = expectThrows( + ElasticsearchParseException.class, + () -> TemplateUtils.validate("{\"foo\": \"bar") + ); assertThat(exception.getMessage(), is("Invalid template")); } @@ -103,19 +126,33 @@ public void testValidate() throws IOException { } public void testReplaceVariable() { - assertTemplate(TemplateUtils.replaceVariable("${monitoring.template.version}", - "monitoring.template.version", "0"), equalTo("0")); - assertTemplate(TemplateUtils.replaceVariable("{\"template\": \"test-${monitoring.template.version}\"}", - "monitoring.template.version", "1"), equalTo("{\"template\": \"test-1\"}")); - assertTemplate(TemplateUtils.replaceVariable("{\"template\": \"${monitoring.template.version}-test\"}", - "monitoring.template.version", "2"), equalTo("{\"template\": \"2-test\"}")); - assertTemplate(TemplateUtils.replaceVariable("{\"template\": \"test-${monitoring.template.version}-test\"}", - "monitoring.template.version", "3"), equalTo("{\"template\": \"test-3-test\"}")); + assertTemplate(TemplateUtils.replaceVariable("${monitoring.template.version}", "monitoring.template.version", "0"), equalTo("0")); + assertTemplate( + TemplateUtils.replaceVariable("{\"template\": \"test-${monitoring.template.version}\"}", "monitoring.template.version", "1"), + equalTo("{\"template\": \"test-1\"}") + ); + assertTemplate( + TemplateUtils.replaceVariable("{\"template\": \"${monitoring.template.version}-test\"}", "monitoring.template.version", "2"), + equalTo("{\"template\": \"2-test\"}") + ); + assertTemplate( + TemplateUtils.replaceVariable( + "{\"template\": \"test-${monitoring.template.version}-test\"}", + "monitoring.template.version", + "3" + ), + equalTo("{\"template\": \"test-3-test\"}") + ); final int version = randomIntBetween(0, 100); - assertTemplate(TemplateUtils.replaceVariable("{\"foo-${monitoring.template.version}\": " + - "\"bar-${monitoring.template.version}\"}", "monitoring.template.version", String.valueOf(version)), - equalTo("{\"foo-" + version + "\": \"bar-" + version + "\"}")); + assertTemplate( + TemplateUtils.replaceVariable( + "{\"foo-${monitoring.template.version}\": " + "\"bar-${monitoring.template.version}\"}", + "monitoring.template.version", + String.valueOf(version) + ), + equalTo("{\"foo-" + version + "\": \"bar-" + version + "\"}") + ); } public static void assertTemplate(String actual, Matcher matcher) { @@ -126,5 +163,4 @@ public static void assertTemplate(String actual, Matcher matcher assertThat(actual, matcher); } - } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/termsenum/MultiShardTermsEnumTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/termsenum/MultiShardTermsEnumTests.java index 248bc102c4e64..80d171240aa2e 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/termsenum/MultiShardTermsEnumTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/termsenum/MultiShardTermsEnumTests.java @@ -38,7 +38,7 @@ import java.util.Map.Entry; public class MultiShardTermsEnumTests extends ESTestCase { - + public void testRandomIndexFusion() throws Exception { String fieldName = "foo"; Map globalTermCounts = new HashMap<>(); @@ -53,10 +53,10 @@ public void testRandomIndexFusion() throws Exception { Directory directory = new ByteBuffersDirectory(); IndexWriter writer = new IndexWriter(directory, new IndexWriterConfig(new MockAnalyzer(random()))); - int numDocs = randomIntBetween(10,200); + int numDocs = randomIntBetween(10, 200); for (int i = 0; i < numDocs; i++) { Document document = new Document(); - String term = randomAlphaOfLengthBetween(1,3).toLowerCase(Locale.ROOT); + String term = randomAlphaOfLengthBetween(1, 3).toLowerCase(Locale.ROOT); document.add(new StringField(fieldName, term, Field.Store.YES)); writer.addDocument(document); int count = 0; @@ -107,7 +107,7 @@ public void testRandomIndexFusion() throws Exception { expecteds.put(termCount.getKey(), termCount.getValue()); } } - + while (mte.next() != null) { String teString = mte.term().utf8ToString(); long actual = mte.docFreq(); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/termsenum/TermCountTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/termsenum/TermCountTests.java index 964fd7fc32b16..2ad887ae49e4f 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/termsenum/TermCountTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/termsenum/TermCountTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.termsenum; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.termsenum.action.TermCount; import java.io.IOException; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/termsenum/TermsEnumRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/termsenum/TermsEnumRequestTests.java index 8b46be3f6cb6b..634b04ac458ea 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/termsenum/TermsEnumRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/termsenum/TermsEnumRequestTests.java @@ -12,13 +12,13 @@ import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.ArrayUtils; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.indices.IndicesModule; import org.elasticsearch.search.SearchModule; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.termsenum.action.TermsEnumAction; import org.elasticsearch.xpack.core.termsenum.action.TermsEnumRequest; @@ -44,7 +44,7 @@ public void setUp() throws Exception { @Override protected TermsEnumRequest createTestInstance() { - TermsEnumRequest request = new TermsEnumRequest(); + TermsEnumRequest request = new TermsEnumRequest(); request.size(randomIntBetween(1, 20)); request.field(randomAlphaOfLengthBetween(3, 10)); request.caseInsensitive(randomBoolean()); @@ -93,16 +93,16 @@ protected TermsEnumRequest doParseInstance(XContentParser parser) throws IOExcep @Override protected TermsEnumRequest mutateInstance(TermsEnumRequest instance) throws IOException { List> mutators = new ArrayList<>(); - mutators.add(request -> { - request.field(randomAlphaOfLengthBetween(3, 10)); - }); + mutators.add(request -> { request.field(randomAlphaOfLengthBetween(3, 10)); }); mutators.add(request -> { String[] indices = ArrayUtils.concat(instance.indices(), generateRandomStringArray(5, 10, false, false)); request.indices(indices); }); mutators.add(request -> { - IndicesOptions indicesOptions = randomValueOtherThan(request.indicesOptions(), - () -> IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean())); + IndicesOptions indicesOptions = randomValueOtherThan( + request.indicesOptions(), + () -> IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean()) + ); request.indicesOptions(indicesOptions); }); mutators.add( diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/termsenum/TermsEnumResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/termsenum/TermsEnumResponseTests.java index 14714c1fefacd..b979db884834b 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/termsenum/TermsEnumResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/termsenum/TermsEnumResponseTests.java @@ -9,8 +9,8 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.action.support.DefaultShardOperationFailedException; import org.elasticsearch.common.Strings; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractBroadcastResponseTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.termsenum.action.TermsEnumResponse; import java.io.IOException; @@ -37,13 +37,11 @@ private static TermsEnumResponse createRandomTermEnumResponse() { int successfulShards = randomIntBetween(0, totalShards); int failedShards = totalShards - successfulShards; List shardFailures = new ArrayList<>(failedShards); - for (int i=0; i failures) { + protected TermsEnumResponse createTestInstance( + int totalShards, + int successfulShards, + int failedShards, + List failures + ) { return new TermsEnumResponse(getRandomTerms(), totalShards, successfulShards, failedShards, failures, randomBoolean()); } @@ -80,8 +82,9 @@ public void testToXContent() { TermsEnumResponse response = new TermsEnumResponse(terms, 10, 10, 0, new ArrayList<>(), true); String output = Strings.toString(response); - assertEquals("{\"_shards\":{\"total\":10,\"successful\":10,\"failed\":0},\"terms\":[" + - "\""+ s +"\""+ - "],\"complete\":true}", output); + assertEquals( + "{\"_shards\":{\"total\":10,\"successful\":10,\"failed\":0},\"terms\":[" + "\"" + s + "\"" + "],\"complete\":true}", + output + ); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/termsenum/TransportTermsEnumActionTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/termsenum/TransportTermsEnumActionTests.java index ca9c46e9f6e7d..27b87b66316d1 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/termsenum/TransportTermsEnumActionTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/termsenum/TransportTermsEnumActionTests.java @@ -38,7 +38,7 @@ public void onFailure(final Exception e) { } }; - client().execute(TermsEnumAction.INSTANCE, new TermsEnumRequest("non-existent-index"),listener); + client().execute(TermsEnumAction.INSTANCE, new TermsEnumRequest("non-existent-index"), listener); assertThat(invoked.get(), equalTo(true)); // ensure that onFailure was invoked } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/termsenum/action/RestTermsEnumActionTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/termsenum/action/RestTermsEnumActionTests.java index 086db3a99a3ee..68a798d2bd1fa 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/termsenum/action/RestTermsEnumActionTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/termsenum/action/RestTermsEnumActionTests.java @@ -16,8 +16,6 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.indices.breaker.NoneCircuitBreakerService; import org.elasticsearch.rest.RestController; import org.elasticsearch.rest.RestRequest; @@ -31,6 +29,8 @@ import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.Transport; import org.elasticsearch.usage.UsageService; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.termsenum.rest.RestTermsEnumAction; import org.junit.AfterClass; import org.junit.BeforeClass; @@ -52,8 +52,7 @@ public class RestTermsEnumActionTests extends ESTestCase { private static NodeClient client = new NodeClient(Settings.EMPTY, threadPool); private static UsageService usageService = new UsageService(); - private static RestController controller = new RestController(emptySet(), null, client, - new NoneCircuitBreakerService(), usageService); + private static RestController controller = new RestController(emptySet(), null, client, new NoneCircuitBreakerService(), usageService); private static RestTermsEnumAction action = new RestTermsEnumAction(); /** @@ -78,8 +77,14 @@ protected void doExecute(Task task, ActionRequest request, ActionListener(); actions.put(TermsEnumAction.INSTANCE, transportAction); - client.initialize(actions, taskManager, () -> "local", - mock(Transport.Connection.class), null, new NamedWriteableRegistry(List.of())); + client.initialize( + actions, + taskManager, + () -> "local", + mock(Transport.Connection.class), + null, + new NamedWriteableRegistry(List.of()) + ); controller.registerHandler(action); } @@ -124,7 +129,7 @@ public void testRestTermEnumAction() throws Exception { public void testRestTermEnumActionMissingField() throws Exception { // GIVEN an invalid query final String content = "{" -// + "\"field\":\"a\", " + // + "\"field\":\"a\", " + "\"string\":\"foo\", " + "\"index_filter\":{\"bool\":{\"must\":{\"term\":{\"user\":\"kimchy\"}}}}}"; @@ -140,10 +145,8 @@ public void testRestTermEnumActionMissingField() throws Exception { assertThat(channel.capturedResponse().content().utf8ToString(), containsString("field cannot be null")); } - private RestRequest createRestRequest(String content) { - return new FakeRestRequest.Builder(xContentRegistry()) - .withPath("index1/_terms_enum") + return new FakeRestRequest.Builder(xContentRegistry()).withPath("index1/_terms_enum") .withParams(emptyMap()) .withContent(new BytesArray(content), XContentType.JSON) .build(); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/textstructure/structurefinder/FieldStatsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/textstructure/structurefinder/FieldStatsTests.java index a6936bd5faafa..55bbb78df88ab 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/textstructure/structurefinder/FieldStatsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/textstructure/structurefinder/FieldStatsTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.core.textstructure.structurefinder; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.util.ArrayList; import java.util.LinkedHashMap; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/textstructure/structurefinder/TextStructureTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/textstructure/structurefinder/TextStructureTests.java index f4fa81816a187..84b99a960b64d 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/textstructure/structurefinder/TextStructureTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/textstructure/structurefinder/TextStructureTests.java @@ -7,9 +7,9 @@ package org.elasticsearch.xpack.core.textstructure.structurefinder; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractSerializingTestCase; import java.nio.charset.Charset; import java.util.Arrays; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/AbstractSerializingTransformTestCase.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/AbstractSerializingTransformTestCase.java index 1b2b10e3961f6..2f4d045f52364 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/AbstractSerializingTransformTestCase.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/AbstractSerializingTransformTestCase.java @@ -8,21 +8,21 @@ package org.elasticsearch.xpack.core.transform; import org.elasticsearch.Version; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.ToXContent.Params; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.search.SearchModule; import org.elasticsearch.search.aggregations.AggregationBuilder; import org.elasticsearch.search.aggregations.BaseAggregationBuilder; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.ToXContent.Params; import org.elasticsearch.xpack.core.transform.transforms.RetentionPolicyConfig; import org.elasticsearch.xpack.core.transform.transforms.SyncConfig; import org.elasticsearch.xpack.core.transform.transforms.TimeRetentionPolicyConfig; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/MockDeprecatedAggregationBuilder.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/MockDeprecatedAggregationBuilder.java index 8e8bfd639df14..5ef5fad9e52ee 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/MockDeprecatedAggregationBuilder.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/MockDeprecatedAggregationBuilder.java @@ -11,8 +11,6 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.logging.DeprecationCategory; import org.elasticsearch.common.logging.DeprecationLogger; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.aggregations.AggregationBuilder; import org.elasticsearch.search.aggregations.AggregatorFactories.Builder; import org.elasticsearch.search.aggregations.AggregatorFactory; @@ -23,6 +21,8 @@ import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry; import org.elasticsearch.search.aggregations.support.ValuesSourceType; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.Map; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/MockDeprecatedQueryBuilder.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/MockDeprecatedQueryBuilder.java index 09e610f7cae00..88ab32525bc65 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/MockDeprecatedQueryBuilder.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/MockDeprecatedQueryBuilder.java @@ -14,11 +14,11 @@ import org.elasticsearch.common.logging.DeprecationCategory; import org.elasticsearch.common.logging.DeprecationLogger; import org.elasticsearch.common.lucene.search.Queries; +import org.elasticsearch.index.query.AbstractQueryBuilder; +import org.elasticsearch.index.query.SearchExecutionContext; import org.elasticsearch.xcontent.ObjectParser; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.index.query.AbstractQueryBuilder; -import org.elasticsearch.index.query.SearchExecutionContext; import java.io.IOException; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/TransformFeatureSetUsageTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/TransformFeatureSetUsageTests.java index 3b5620ba0b9d5..5747dfd4dfa76 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/TransformFeatureSetUsageTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/TransformFeatureSetUsageTests.java @@ -22,12 +22,10 @@ public class TransformFeatureSetUsageTests extends AbstractWireSerializingTestCa @Override protected TransformFeatureSetUsage createTestInstance() { - Map transformCountByState = - randomSubsetOf(Arrays.asList(IndexerState.values())).stream() - .collect(toMap(state -> state.value(), state -> randomLong())); - Map transformCountByFeature = - randomList(10, () -> randomAlphaOfLength(10)).stream() - .collect(toMap(f -> f, f -> randomLong())); + Map transformCountByState = randomSubsetOf(Arrays.asList(IndexerState.values())).stream() + .collect(toMap(state -> state.value(), state -> randomLong())); + Map transformCountByFeature = randomList(10, () -> randomAlphaOfLength(10)).stream() + .collect(toMap(f -> f, f -> randomLong())); TransformIndexerStats accumulatedStats = TransformIndexerStatsTests.randomStats(); return new TransformFeatureSetUsage(transformCountByState, transformCountByFeature, accumulatedStats); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/TransformMessagesTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/TransformMessagesTests.java index 04c87fe5bef75..a84427744a0d1 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/TransformMessagesTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/TransformMessagesTests.java @@ -18,8 +18,11 @@ public class TransformMessagesTests extends ESTestCase { public void testGetMessage_WithFormatStrings() { - String formattedMessage = TransformMessages.getMessage(TransformMessages.REST_STOP_TRANSFORM_WAIT_FOR_COMPLETION_TIMEOUT, "30s", - "my_transform"); + String formattedMessage = TransformMessages.getMessage( + TransformMessages.REST_STOP_TRANSFORM_WAIT_FOR_COMPLETION_TIMEOUT, + "30s", + "my_transform" + ); assertEquals("Timed out after [30s] while waiting for transform [my_transform] to stop", formattedMessage); } @@ -29,8 +32,9 @@ public void testMessageProperFormat() throws IllegalArgumentException, IllegalAc for (Field field : declaredFields) { int modifiers = field.getModifiers(); - if (java.lang.reflect.Modifier.isStatic(modifiers) && java.lang.reflect.Modifier.isFinal(modifiers) - && field.getType().isAssignableFrom(String.class)) { + if (java.lang.reflect.Modifier.isStatic(modifiers) + && java.lang.reflect.Modifier.isFinal(modifiers) + && field.getType().isAssignableFrom(String.class)) { assertSingleMessage((String) field.get(TransformMessages.class)); ++checkedMessages; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/action/AbstractWireSerializingTransformTestCase.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/action/AbstractWireSerializingTransformTestCase.java index 68cdaa87a8a6a..07ce6ee52893d 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/action/AbstractWireSerializingTransformTestCase.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/action/AbstractWireSerializingTransformTestCase.java @@ -14,9 +14,9 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.search.SearchModule; import org.elasticsearch.test.AbstractWireSerializingTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.transform.TransformField; import org.elasticsearch.xpack.core.transform.TransformNamedXContentProvider; import org.elasticsearch.xpack.core.transform.transforms.RetentionPolicyConfig; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/action/GetTransformActionResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/action/GetTransformActionResponseTests.java index 00989174c7985..37ced39a5effc 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/action/GetTransformActionResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/action/GetTransformActionResponseTests.java @@ -9,10 +9,10 @@ import org.elasticsearch.common.io.stream.Writeable.Reader; import org.elasticsearch.common.logging.LoggerMessageFormat; +import org.elasticsearch.common.xcontent.support.XContentMapValues; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.common.xcontent.support.XContentMapValues; import org.elasticsearch.xpack.core.transform.action.GetTransformAction.Response; import org.elasticsearch.xpack.core.transform.transforms.TransformConfig; import org.elasticsearch.xpack.core.transform.transforms.TransformConfigTests; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/action/GetTransformStatsActionRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/action/GetTransformStatsActionRequestTests.java index 4af4cdd9b64f2..7c320c00f0d21 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/action/GetTransformStatsActionRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/action/GetTransformStatsActionRequestTests.java @@ -15,11 +15,7 @@ public class GetTransformStatsActionRequestTests extends AbstractWireSerializingTestCase { @Override protected Request createTestInstance() { - return new Request( - randomBoolean() - ? randomAlphaOfLengthBetween(1, 20) - : randomBoolean() ? Metadata.ALL : null - ); + return new Request(randomBoolean() ? randomAlphaOfLengthBetween(1, 20) : randomBoolean() ? Metadata.ALL : null); } @Override diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/action/PreviewTransformActionRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/action/PreviewTransformActionRequestTests.java index 2758ff9919d94..81504e0d1755b 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/action/PreviewTransformActionRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/action/PreviewTransformActionRequestTests.java @@ -31,7 +31,7 @@ public class PreviewTransformActionRequestTests extends AbstractSerializingTrans @Override protected Request doParseInstance(XContentParser parser) throws IOException { - return Request.fromXContent(parser, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT); + return Request.fromXContent(parser, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT); } @Override @@ -68,10 +68,7 @@ protected Request createTestInstance() { public void testParsingOverwritesIdField() throws IOException { testParsingOverwrites( "", - "\"dest\": {" - + "\"index\": \"bar\"," - + "\"pipeline\": \"baz\"" - + "},", + "\"dest\": {" + "\"index\": \"bar\"," + "\"pipeline\": \"baz\"" + "},", "transform-preview", "bar", "baz" @@ -79,21 +76,13 @@ public void testParsingOverwritesIdField() throws IOException { } public void testParsingOverwritesDestField() throws IOException { - testParsingOverwrites( - "\"id\": \"bar\",", - "", - "bar", - "unused-transform-preview-index", - null - ); + testParsingOverwrites("\"id\": \"bar\",", "", "bar", "unused-transform-preview-index", null); } public void testParsingOverwritesIdAndDestIndexFields() throws IOException { testParsingOverwrites( "", - "\"dest\": {" - + "\"pipeline\": \"baz\"" - + "},", + "\"dest\": {" + "\"pipeline\": \"baz\"" + "},", "transform-preview", "unused-transform-preview-index", "baz" @@ -101,13 +90,7 @@ public void testParsingOverwritesIdAndDestIndexFields() throws IOException { } public void testParsingOverwritesIdAndDestFields() throws IOException { - testParsingOverwrites( - "", - "", - "transform-preview", - "unused-transform-preview-index", - null - ); + testParsingOverwrites("", "", "transform-preview", "unused-transform-preview-index", null); } private void testParsingOverwrites( @@ -139,7 +122,7 @@ private void testParsingOverwrites( ) ) { - Request request = Request.fromXContent(parser, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT); + Request request = Request.fromXContent(parser, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT); assertThat(request.getConfig().getId(), is(equalTo(expectedTransformId))); assertThat(request.getConfig().getDestination().getIndex(), is(equalTo(expectedDestIndex))); assertThat(request.getConfig().getDestination().getPipeline(), is(equalTo(expectedDestPipeline))); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/action/PreviewTransformsActionResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/action/PreviewTransformsActionResponseTests.java index c34c693d0c94f..83575af98c521 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/action/PreviewTransformsActionResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/action/PreviewTransformsActionResponseTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.xpack.core.transform.action; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.transform.action.PreviewTransformAction.Response; import org.elasticsearch.xpack.core.transform.transforms.TransformDestIndexSettingsTests; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/action/UpgradeTransformsActionResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/action/UpgradeTransformsActionResponseTests.java index a92ce693ec11c..5bd856720f2b0 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/action/UpgradeTransformsActionResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/action/UpgradeTransformsActionResponseTests.java @@ -13,11 +13,7 @@ public class UpgradeTransformsActionResponseTests extends AbstractWireSerializingTransformTestCase { public static Response randomUpgradeResponse() { - return new Response( - randomNonNegativeLong(), - randomNonNegativeLong(), - randomNonNegativeLong() - ); + return new Response(randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong()); } @Override diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/notifications/TransformAuditMessageTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/notifications/TransformAuditMessageTests.java index 117924fed8719..a98bc0dceb888 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/notifications/TransformAuditMessageTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/notifications/TransformAuditMessageTests.java @@ -6,8 +6,8 @@ */ package org.elasticsearch.xpack.core.transform.notifications; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.common.notifications.Level; import java.util.Date; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/NodeAttributeTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/NodeAttributeTests.java index fe8a197da81e0..b24902264f0ac 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/NodeAttributeTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/NodeAttributeTests.java @@ -8,22 +8,23 @@ package org.elasticsearch.xpack.core.transform.transforms; import org.elasticsearch.common.io.stream.Writeable.Reader; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.Collections; import java.util.function.Predicate; - public class NodeAttributeTests extends AbstractSerializingTestCase { public static NodeAttributes randomNodeAttributes() { - return new NodeAttributes(randomAlphaOfLength(10), + return new NodeAttributes( + randomAlphaOfLength(10), randomAlphaOfLength(10), randomAlphaOfLength(10), randomAlphaOfLength(10), - randomBoolean() ? Collections.emptyMap() : Collections.singletonMap(randomAlphaOfLength(10), randomAlphaOfLength(10))); + randomBoolean() ? Collections.emptyMap() : Collections.singletonMap(randomAlphaOfLength(10), randomAlphaOfLength(10)) + ); } @Override diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/QueryConfigTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/QueryConfigTests.java index 33a66397e342f..223e0eb98ea7c 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/QueryConfigTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/QueryConfigTests.java @@ -11,17 +11,17 @@ import org.elasticsearch.common.ValidationException; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.Writeable.Reader; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.index.query.MatchAllQueryBuilder; +import org.elasticsearch.index.query.MatchNoneQueryBuilder; +import org.elasticsearch.index.query.MatchQueryBuilder; +import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.index.query.MatchAllQueryBuilder; -import org.elasticsearch.index.query.MatchNoneQueryBuilder; -import org.elasticsearch.index.query.MatchQueryBuilder; -import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.xpack.core.transform.AbstractSerializingTransformTestCase; import org.elasticsearch.xpack.core.transform.MockDeprecatedQueryBuilder; import org.junit.Before; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/SettingsConfigTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/SettingsConfigTests.java index aef8eb2351f9b..8103ef5ac7174 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/SettingsConfigTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/SettingsConfigTests.java @@ -85,11 +85,12 @@ public void testExplicitNullParsing() throws IOException { } public void testUpdateUsingBuilder() throws IOException { - SettingsConfig config = - fromString("{\"max_page_search_size\" : 10000, " + SettingsConfig config = fromString( + "{\"max_page_search_size\" : 10000, " + "\"docs_per_second\" :42, " + "\"dates_as_epoch_millis\": true, " - + "\"align_checkpoints\": false}"); + + "\"align_checkpoints\": false}" + ); SettingsConfig.Builder builder = new SettingsConfig.Builder(config); builder.update(fromString("{\"max_page_search_size\" : 100}")); @@ -106,10 +107,13 @@ public void testUpdateUsingBuilder() throws IOException { assertThat(builder.build().getAlignCheckpointsForUpdate(), equalTo(0)); builder.update( - fromString("{\"max_page_search_size\" : 77, " - + "\"docs_per_second\" :null, " - + "\"dates_as_epoch_millis\": null, " - + "\"align_checkpoints\": null}")); + fromString( + "{\"max_page_search_size\" : 77, " + + "\"docs_per_second\" :null, " + + "\"dates_as_epoch_millis\": null, " + + "\"align_checkpoints\": null}" + ) + ); assertThat(builder.build().getMaxPageSearchSize(), equalTo(77)); assertNull(builder.build().getDocsPerSecond()); assertNull(builder.build().getDatesAsEpochMillisForUpdate()); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/TimeRetentionPolicyConfigTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/TimeRetentionPolicyConfigTests.java index 88d64464f4d9a..f70a113a98112 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/TimeRetentionPolicyConfigTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/TimeRetentionPolicyConfigTests.java @@ -10,18 +10,15 @@ import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.common.io.stream.Writeable.Reader; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; public class TimeRetentionPolicyConfigTests extends AbstractSerializingTestCase { public static TimeRetentionPolicyConfig randomTimeRetentionPolicyConfig() { - return new TimeRetentionPolicyConfig( - randomAlphaOfLengthBetween(1, 10), - new TimeValue(randomLongBetween(60000, 1_000_000_000L)) - ); + return new TimeRetentionPolicyConfig(randomAlphaOfLengthBetween(1, 10), new TimeValue(randomLongBetween(60000, 1_000_000_000L))); } @Override diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/TimeSyncConfigTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/TimeSyncConfigTests.java index b87a927a244a1..8d559693dd60c 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/TimeSyncConfigTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/TimeSyncConfigTests.java @@ -9,8 +9,8 @@ import org.elasticsearch.common.io.stream.Writeable.Reader; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/TransformConfigUpdateTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/TransformConfigUpdateTests.java index ddf69eb966dd9..68b9848ffbddf 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/TransformConfigUpdateTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/TransformConfigUpdateTests.java @@ -11,9 +11,9 @@ import org.elasticsearch.Version; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable.Reader; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.query.QueryBuilder; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.transform.TransformField; import org.elasticsearch.xpack.core.transform.action.AbstractWireSerializingTransformTestCase; import org.elasticsearch.xpack.core.transform.transforms.pivot.PivotConfigTests; @@ -162,8 +162,16 @@ public void testApplySettings() { randomBoolean() ? null : Version.V_7_2_0.toString() ); - TransformConfigUpdate update = - new TransformConfigUpdate(null, null, null, null, null, new SettingsConfig(4_000, null, (Boolean) null, null), null, null); + TransformConfigUpdate update = new TransformConfigUpdate( + null, + null, + null, + null, + null, + new SettingsConfig(4_000, null, (Boolean) null, null), + null, + null + ); TransformConfig updatedConfig = update.apply(config); // for settings we allow partial updates, so changing 1 setting should not overwrite the other @@ -173,8 +181,16 @@ public void testApplySettings() { assertThat(updatedConfig.getSettings().getDatesAsEpochMillis(), equalTo(config.getSettings().getDatesAsEpochMillis())); assertThat(updatedConfig.getSettings().getAlignCheckpoints(), equalTo(config.getSettings().getAlignCheckpoints())); - update = - new TransformConfigUpdate(null, null, null, null, null, new SettingsConfig(null, 43.244F, (Boolean) null, null), null, null); + update = new TransformConfigUpdate( + null, + null, + null, + null, + null, + new SettingsConfig(null, 43.244F, (Boolean) null, null), + null, + null + ); updatedConfig = update.apply(updatedConfig); assertThat(updatedConfig.getSettings().getMaxPageSearchSize(), equalTo(4_000)); assertThat(updatedConfig.getSettings().getDocsPerSecond(), equalTo(43.244F)); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/TransformIndexerPositionTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/TransformIndexerPositionTests.java index 30f6feeca18cc..8b38d8c645874 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/TransformIndexerPositionTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/TransformIndexerPositionTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.xpack.core.transform.transforms; import org.elasticsearch.common.io.stream.Writeable.Reader; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.HashMap; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/TransformIndexerStatsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/TransformIndexerStatsTests.java index 3a74ece4f5d21..ad7d033848c03 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/TransformIndexerStatsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/TransformIndexerStatsTests.java @@ -8,9 +8,9 @@ package org.elasticsearch.xpack.core.transform.transforms; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractSerializingTestCase; import java.io.IOException; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/TransformStateTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/TransformStateTests.java index 5486c0ff2fc3e..6107fb163005b 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/TransformStateTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/TransformStateTests.java @@ -11,15 +11,15 @@ import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.Writeable.Reader; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.indexing.IndexerState; import java.io.IOException; import java.util.function.Predicate; -import static org.elasticsearch.xpack.core.transform.transforms.TransformProgressTests.randomTransformProgress; import static org.elasticsearch.xpack.core.transform.transforms.NodeAttributeTests.randomNodeAttributes; +import static org.elasticsearch.xpack.core.transform.transforms.TransformProgressTests.randomTransformProgress; public class TransformStateTests extends AbstractSerializingTestCase { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/TransformStatsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/TransformStatsTests.java index 75b80b3840a7f..3fb9798b700b1 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/TransformStatsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/TransformStatsTests.java @@ -11,8 +11,8 @@ import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.Writeable.Reader; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.function.Predicate; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/TransformTaskStateTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/TransformTaskStateTests.java index 32c0ef3271f54..26bc5ca1635c7 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/TransformTaskStateTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/TransformTaskStateTests.java @@ -73,7 +73,7 @@ public void testInvalidReadFrom() throws Exception { try (StreamInput in = out.bytes().streamInput()) { TransformTaskState.fromStream(in); fail("Expected IOException"); - } catch(IOException e) { + } catch (IOException e) { assertThat(e.getMessage(), containsString("Unknown TransformTaskState ordinal [")); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/latest/LatestConfigTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/latest/LatestConfigTests.java index 72c4e60d156e9..c320120cf8ee6 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/latest/LatestConfigTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/latest/LatestConfigTests.java @@ -8,11 +8,11 @@ package org.elasticsearch.xpack.core.transform.transforms.latest; import org.elasticsearch.common.io.stream.Writeable.Reader; +import org.elasticsearch.search.sort.SortBuilders; +import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.search.sort.SortBuilders; -import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.xpack.core.transform.AbstractSerializingTransformTestCase; import java.io.IOException; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/pivot/AggregationConfigTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/pivot/AggregationConfigTests.java index 25f49dd9e718b..73d42e2618cdf 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/pivot/AggregationConfigTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/pivot/AggregationConfigTests.java @@ -11,16 +11,16 @@ import org.elasticsearch.common.ValidationException; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.Writeable.Reader; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.search.aggregations.AggregationBuilder; +import org.elasticsearch.search.aggregations.AggregationBuilders; +import org.elasticsearch.search.aggregations.AggregatorFactories; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.search.aggregations.AggregationBuilder; -import org.elasticsearch.search.aggregations.AggregationBuilders; -import org.elasticsearch.search.aggregations.AggregatorFactories; import org.elasticsearch.xpack.core.transform.AbstractSerializingTransformTestCase; import org.elasticsearch.xpack.core.transform.MockDeprecatedAggregationBuilder; import org.junit.Before; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/pivot/DateHistogramGroupSourceTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/pivot/DateHistogramGroupSourceTests.java index 68803199d5a0b..608608b56f0c5 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/pivot/DateHistogramGroupSourceTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/pivot/DateHistogramGroupSourceTests.java @@ -13,10 +13,10 @@ import org.elasticsearch.common.io.stream.Writeable.Reader; import org.elasticsearch.common.time.DateFormatter; import org.elasticsearch.common.time.DateFormatters; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.test.VersionUtils; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.time.ZoneOffset; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/pivot/GeoTileGroupSourceTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/pivot/GeoTileGroupSourceTests.java index 3b0e091651c2d..77b7e038eee07 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/pivot/GeoTileGroupSourceTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/pivot/GeoTileGroupSourceTests.java @@ -11,11 +11,11 @@ import org.elasticsearch.common.geo.GeoBoundingBox; import org.elasticsearch.common.geo.GeoPoint; import org.elasticsearch.common.io.stream.Writeable.Reader; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.geo.GeometryTestUtils; import org.elasticsearch.geometry.Rectangle; import org.elasticsearch.search.aggregations.bucket.geogrid.GeoTileUtils; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/pivot/GroupConfigTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/pivot/GroupConfigTests.java index c177d3926c240..fe0a3655d2ebc 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/pivot/GroupConfigTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/pivot/GroupConfigTests.java @@ -12,14 +12,14 @@ import org.elasticsearch.common.ValidationException; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.Writeable.Reader; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xpack.core.transform.transforms.pivot.SingleGroupSource.Type; import java.io.IOException; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/pivot/HistogramGroupSourceTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/pivot/HistogramGroupSourceTests.java index c409f0f22b159..2e1bf1488ef2f 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/pivot/HistogramGroupSourceTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/pivot/HistogramGroupSourceTests.java @@ -9,8 +9,8 @@ import org.elasticsearch.Version; import org.elasticsearch.common.io.stream.Writeable.Reader; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/pivot/ScriptConfigTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/pivot/ScriptConfigTests.java index 228fba539a839..2a7fb20c85467 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/pivot/ScriptConfigTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/pivot/ScriptConfigTests.java @@ -10,16 +10,16 @@ import org.elasticsearch.common.ValidationException; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.Writeable.Reader; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.script.Script; +import org.elasticsearch.script.ScriptType; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentParseException; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.script.Script; -import org.elasticsearch.script.ScriptType; import org.elasticsearch.xpack.core.transform.AbstractSerializingTransformTestCase; import org.junit.Before; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/pivot/TermsGroupSourceTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/pivot/TermsGroupSourceTests.java index 774181cea4af3..65b9b6e977b59 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/pivot/TermsGroupSourceTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/pivot/TermsGroupSourceTests.java @@ -10,9 +10,9 @@ import org.elasticsearch.Version; import org.elasticsearch.common.ValidationException; import org.elasticsearch.common.io.stream.Writeable.Reader; +import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.AbstractSerializingTestCase; import java.io.IOException; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/schema/TransformConfigTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/schema/TransformConfigTests.java index 168a93ad67198..21dd928f1c4ce 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/schema/TransformConfigTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/schema/TransformConfigTests.java @@ -7,9 +7,9 @@ package org.elasticsearch.xpack.core.transform.transforms.schema; +import org.elasticsearch.test.AbstractSchemaValidationTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContent.Params; -import org.elasticsearch.test.AbstractSchemaValidationTestCase; import org.elasticsearch.xpack.core.transform.TransformField; import org.elasticsearch.xpack.core.transform.transforms.TransformConfig; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/utils/TransformStringsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/utils/TransformStringsTests.java index d891f98264957..52f6294291759 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/utils/TransformStringsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/utils/TransformStringsTests.java @@ -24,8 +24,7 @@ public void testValidId_givenStartsWithUnderScore() { } public void testKasValidLengthForId_givenTooLong() { - assertTrue(TransformStrings.hasValidLengthForId("#".repeat(TransformStrings.ID_LENGTH_LIMIT ))); + assertTrue(TransformStrings.hasValidLengthForId("#".repeat(TransformStrings.ID_LENGTH_LIMIT))); assertFalse(TransformStrings.hasValidLengthForId("#".repeat(TransformStrings.ID_LENGTH_LIMIT + 1))); } } - diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/watcher/crypto/CryptoServiceTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/watcher/crypto/CryptoServiceTests.java index 53d2c272ce476..d23c21f88c9c3 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/watcher/crypto/CryptoServiceTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/watcher/crypto/CryptoServiceTests.java @@ -13,11 +13,11 @@ import org.elasticsearch.xpack.core.watcher.WatcherField; import org.junit.Before; -import javax.crypto.KeyGenerator; - import java.security.NoSuchAlgorithmException; import java.util.Arrays; +import javax.crypto.KeyGenerator; + import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; @@ -28,9 +28,7 @@ public class CryptoServiceTests extends ESTestCase { public void init() throws Exception { MockSecureSettings mockSecureSettings = new MockSecureSettings(); mockSecureSettings.setFile(WatcherField.ENCRYPTION_KEY_SETTING.getKey(), generateKey()); - settings = Settings.builder() - .setSecureSettings(mockSecureSettings) - .build(); + settings = Settings.builder().setSecureSettings(mockSecureSettings).build(); } public void testEncryptionAndDecryptionChars() throws Exception { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/watcher/watch/ClockMock.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/watcher/watch/ClockMock.java index a7d2fc25e2c24..c3befa6d6711c 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/watcher/watch/ClockMock.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/watcher/watch/ClockMock.java @@ -35,7 +35,6 @@ private ClockMock(Clock wrappedClock) { this.wrappedClock = wrappedClock; } - @Override public ZoneId getZone() { return wrappedClock.getZone(); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/watcher/support/xcontent/WatcherXContentParserTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/watcher/support/xcontent/WatcherXContentParserTests.java index 28424d1df0da8..ad6d2b508d78d 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/watcher/support/xcontent/WatcherXContentParserTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/watcher/support/xcontent/WatcherXContentParserTests.java @@ -9,11 +9,11 @@ import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.watcher.support.xcontent.WatcherXContentParser; import java.time.ZoneOffset; @@ -29,8 +29,10 @@ public void testThatRedactedSecretsThrowException() throws Exception { try (XContentBuilder builder = jsonBuilder()) { builder.startObject().field(fieldName, "::es_redacted::").endObject(); - try (XContentParser xContentParser = XContentType.JSON.xContent().createParser(NamedXContentRegistry.EMPTY, - LoggingDeprecationHandler.INSTANCE, Strings.toString(builder))) { + try ( + XContentParser xContentParser = XContentType.JSON.xContent() + .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, Strings.toString(builder)) + ) { xContentParser.nextToken(); xContentParser.nextToken(); assertThat(xContentParser.currentName(), is(fieldName)); @@ -39,8 +41,10 @@ public void testThatRedactedSecretsThrowException() throws Exception { ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC); WatcherXContentParser parser = new WatcherXContentParser(xContentParser, now, null, false); - ElasticsearchParseException e = expectThrows(ElasticsearchParseException.class, - () -> WatcherXContentParser.secretOrNull(parser)); + ElasticsearchParseException e = expectThrows( + ElasticsearchParseException.class, + () -> WatcherXContentParser.secretOrNull(parser) + ); assertThat(e.getMessage(), is("found redacted password in field [" + fieldName + "]")); } } diff --git a/x-pack/plugin/core/src/yamlRestTest/java/org/elasticsearch/license/XPackCoreClientYamlTestSuiteIT.java b/x-pack/plugin/core/src/yamlRestTest/java/org/elasticsearch/license/XPackCoreClientYamlTestSuiteIT.java index b010e6c1eb133..105df6dbeca4a 100644 --- a/x-pack/plugin/core/src/yamlRestTest/java/org/elasticsearch/license/XPackCoreClientYamlTestSuiteIT.java +++ b/x-pack/plugin/core/src/yamlRestTest/java/org/elasticsearch/license/XPackCoreClientYamlTestSuiteIT.java @@ -9,6 +9,7 @@ import com.carrotsearch.randomizedtesting.annotations.Name; import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; + import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; @@ -17,8 +18,7 @@ public class XPackCoreClientYamlTestSuiteIT extends ESClientYamlSuiteTestCase { - private static final String BASIC_AUTH_VALUE = - basicAuthHeaderValue("x_pack_rest_user", new SecureString("x-pack-test-password")); + private static final String BASIC_AUTH_VALUE = basicAuthHeaderValue("x_pack_rest_user", new SecureString("x-pack-test-password")); public XPackCoreClientYamlTestSuiteIT(@Name("yaml") ClientYamlTestCandidate testCandidate) { super(testCandidate); @@ -31,8 +31,6 @@ public static Iterable parameters() throws Exception { @Override protected Settings restClientSettings() { - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", BASIC_AUTH_VALUE) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", BASIC_AUTH_VALUE).build(); } } diff --git a/x-pack/plugin/data-streams/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/datastreams/DataStreamRestIT.java b/x-pack/plugin/data-streams/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/datastreams/DataStreamRestIT.java index e4de4763c6968..3b774b8da0b43 100644 --- a/x-pack/plugin/data-streams/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/datastreams/DataStreamRestIT.java +++ b/x-pack/plugin/data-streams/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/datastreams/DataStreamRestIT.java @@ -10,11 +10,11 @@ import org.apache.http.util.EntityUtils; import org.elasticsearch.client.Request; import org.elasticsearch.client.Response; +import org.elasticsearch.test.rest.ESRestTestCase; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.rest.ESRestTestCase; import java.util.Map; diff --git a/x-pack/plugin/data-streams/qa/rest/src/javaRestTest/java/org/elasticsearch/xpack/datastreams/DataStreamUpgradeRestIT.java b/x-pack/plugin/data-streams/qa/rest/src/javaRestTest/java/org/elasticsearch/xpack/datastreams/DataStreamUpgradeRestIT.java index 1f420c9fd034f..5e8ff688d98f5 100644 --- a/x-pack/plugin/data-streams/qa/rest/src/javaRestTest/java/org/elasticsearch/xpack/datastreams/DataStreamUpgradeRestIT.java +++ b/x-pack/plugin/data-streams/qa/rest/src/javaRestTest/java/org/elasticsearch/xpack/datastreams/DataStreamUpgradeRestIT.java @@ -12,9 +12,9 @@ import org.elasticsearch.client.Response; import org.elasticsearch.client.ResponseException; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.common.xcontent.support.XContentMapValues; import org.elasticsearch.test.rest.ESRestTestCase; +import org.elasticsearch.xcontent.json.JsonXContent; import java.io.IOException; import java.util.List; diff --git a/x-pack/plugin/data-streams/qa/rest/src/javaRestTest/java/org/elasticsearch/xpack/datastreams/DataStreamsRestIT.java b/x-pack/plugin/data-streams/qa/rest/src/javaRestTest/java/org/elasticsearch/xpack/datastreams/DataStreamsRestIT.java index 142b3241d637e..a6bff6f32b0e8 100644 --- a/x-pack/plugin/data-streams/qa/rest/src/javaRestTest/java/org/elasticsearch/xpack/datastreams/DataStreamsRestIT.java +++ b/x-pack/plugin/data-streams/qa/rest/src/javaRestTest/java/org/elasticsearch/xpack/datastreams/DataStreamsRestIT.java @@ -102,8 +102,10 @@ public void testDataStreamAliases() throws Exception { // Add logs-myapp1 -> logs & logs-myapp2 -> logs Request updateAliasesRequest = new Request("POST", "/_aliases"); - updateAliasesRequest.setJsonEntity("{\"actions\":[{\"add\":{\"index\":\"logs-myapp1\",\"alias\":\"logs\"}}," + - "{\"add\":{\"index\":\"logs-myapp2\",\"alias\":\"logs\"}}]}"); + updateAliasesRequest.setJsonEntity( + "{\"actions\":[{\"add\":{\"index\":\"logs-myapp1\",\"alias\":\"logs\"}}," + + "{\"add\":{\"index\":\"logs-myapp2\",\"alias\":\"logs\"}}]}" + ); assertOK(client().performRequest(updateAliasesRequest)); Request getAliasesRequest = new Request("GET", "/_aliases"); @@ -118,8 +120,10 @@ public void testDataStreamAliases() throws Exception { // Remove logs-myapp1 -> logs & logs-myapp2 -> logs updateAliasesRequest = new Request("POST", "/_aliases"); - updateAliasesRequest.setJsonEntity("{\"actions\":[{\"remove\":{\"index\":\"logs-myapp1\",\"alias\":\"logs\"}}," + - "{\"remove\":{\"index\":\"logs-myapp2\",\"alias\":\"logs\"}}]}"); + updateAliasesRequest.setJsonEntity( + "{\"actions\":[{\"remove\":{\"index\":\"logs-myapp1\",\"alias\":\"logs\"}}," + + "{\"remove\":{\"index\":\"logs-myapp2\",\"alias\":\"logs\"}}]}" + ); assertOK(client().performRequest(updateAliasesRequest)); getAliasesRequest = new Request("GET", "/_aliases"); @@ -168,8 +172,9 @@ public void testDeleteDataStreamApiWithAliasFails() throws IOException { Request updateAliasesRequest = new Request("POST", "/_aliases"); updateAliasesRequest.setJsonEntity( - "{\"actions\":[{\"add\":{\"index\":\"logs-emea\",\"alias\":\"logs\"}}," + - "{\"add\":{\"index\":\"logs-nasa\",\"alias\":\"logs\"}}]}"); + "{\"actions\":[{\"add\":{\"index\":\"logs-emea\",\"alias\":\"logs\"}}," + + "{\"add\":{\"index\":\"logs-nasa\",\"alias\":\"logs\"}}]}" + ); assertOK(client().performRequest(updateAliasesRequest)); Request getAliasesRequest = new Request("GET", "/logs-*/_alias"); @@ -178,8 +183,10 @@ public void testDeleteDataStreamApiWithAliasFails() throws IOException { assertEquals(Map.of("logs", Map.of()), XContentMapValues.extractValue("logs-nasa.aliases", getAliasesResponse)); Exception e = expectThrows(ResponseException.class, () -> client().performRequest(new Request("DELETE", "/_data_stream/logs"))); - assertThat(e.getMessage(), containsString("The provided expression [logs] matches an alias, " + - "specify the corresponding concrete indices instead")); + assertThat( + e.getMessage(), + containsString("The provided expression [logs] matches an alias, " + "specify the corresponding concrete indices instead") + ); assertOK(client().performRequest(new Request("DELETE", "/_data_stream/logs-emea"))); assertOK(client().performRequest(new Request("DELETE", "/_data_stream/logs-nasa"))); @@ -203,8 +210,9 @@ public void testGetAliasApiFilterByDataStreamAlias() throws Exception { Request updateAliasesRequest = new Request("POST", "/_aliases"); updateAliasesRequest.setJsonEntity( - "{\"actions\":[{\"add\":{\"index\":\"logs-emea\",\"alias\":\"emea\"}}," + - "{\"add\":{\"index\":\"logs-nasa\",\"alias\":\"nasa\"}}]}"); + "{\"actions\":[{\"add\":{\"index\":\"logs-emea\",\"alias\":\"emea\"}}," + + "{\"add\":{\"index\":\"logs-nasa\",\"alias\":\"nasa\"}}]}" + ); assertOK(client().performRequest(updateAliasesRequest)); Response response = client().performRequest(new Request("GET", "/_alias")); @@ -220,8 +228,10 @@ public void testGetAliasApiFilterByDataStreamAlias() throws Exception { assertThat(getAliasesResponse.size(), equalTo(1)); assertEquals(Map.of("emea", Map.of()), XContentMapValues.extractValue("logs-emea.aliases", getAliasesResponse)); - ResponseException exception = - expectThrows(ResponseException.class, () -> client().performRequest(new Request("GET", "/_alias/wrong_name"))); + ResponseException exception = expectThrows( + ResponseException.class, + () -> client().performRequest(new Request("GET", "/_alias/wrong_name")) + ); response = exception.getResponse(); assertThat(response.getStatusLine().getStatusCode(), equalTo(404)); getAliasesResponse = entityAsMap(response); @@ -234,7 +244,8 @@ public void testDataStreamWithAliasFromTemplate() throws IOException { // Create a template Request putComposableIndexTemplateRequest = new Request("POST", "/_index_template/1"); putComposableIndexTemplateRequest.setJsonEntity( - "{\"index_patterns\": [\"logs-*\"], \"template\": { \"aliases\": { \"logs\": {} } }, \"data_stream\": {}}"); + "{\"index_patterns\": [\"logs-*\"], \"template\": { \"aliases\": { \"logs\": {} } }, \"data_stream\": {}}" + ); assertOK(client().performRequest(putComposableIndexTemplateRequest)); Request createDocRequest = new Request("POST", "/logs-emea/_doc?refresh=true"); @@ -272,8 +283,9 @@ public void testDataStreamWriteAlias() throws IOException { Request updateAliasesRequest = new Request("POST", "/_aliases"); updateAliasesRequest.setJsonEntity( - "{\"actions\":[{\"add\":{\"index\":\"logs-emea\",\"alias\":\"logs\",\"is_write_index\":true}}," + - "{\"add\":{\"index\":\"logs-nasa\",\"alias\":\"logs\"}}]}"); + "{\"actions\":[{\"add\":{\"index\":\"logs-emea\",\"alias\":\"logs\",\"is_write_index\":true}}," + + "{\"add\":{\"index\":\"logs-nasa\",\"alias\":\"logs\"}}]}" + ); assertOK(client().performRequest(updateAliasesRequest)); Request getAliasesRequest = new Request("GET", "/_aliases"); @@ -295,10 +307,12 @@ public void testDataStreamWriteAlias() throws IOException { assertThat((String) entityAsMap(createDocResponse).get("_index"), startsWith(".ds-logs-emea")); updateAliasesRequest = new Request("POST", "/_aliases"); - updateAliasesRequest.setJsonEntity("{\"actions\":[" + - "{\"add\":{\"index\":\"logs-emea\",\"alias\":\"logs\",\"is_write_index\":false}}," + - "{\"add\":{\"index\":\"logs-nasa\",\"alias\":\"logs\",\"is_write_index\":true}}" + - "]}"); + updateAliasesRequest.setJsonEntity( + "{\"actions\":[" + + "{\"add\":{\"index\":\"logs-emea\",\"alias\":\"logs\",\"is_write_index\":false}}," + + "{\"add\":{\"index\":\"logs-nasa\",\"alias\":\"logs\",\"is_write_index\":true}}" + + "]}" + ); assertOK(client().performRequest(updateAliasesRequest)); createDocRequest = new Request("POST", "/logs/_doc?refresh=true"); diff --git a/x-pack/plugin/data-streams/qa/rest/src/yamlRestTest/java/org/elasticsearch/xpack/datastreams/AutoCreateDataStreamIT.java b/x-pack/plugin/data-streams/qa/rest/src/yamlRestTest/java/org/elasticsearch/xpack/datastreams/AutoCreateDataStreamIT.java index d5eca4cae81db..ed8106b3c119c 100644 --- a/x-pack/plugin/data-streams/qa/rest/src/yamlRestTest/java/org/elasticsearch/xpack/datastreams/AutoCreateDataStreamIT.java +++ b/x-pack/plugin/data-streams/qa/rest/src/yamlRestTest/java/org/elasticsearch/xpack/datastreams/AutoCreateDataStreamIT.java @@ -15,9 +15,9 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.Streams; import org.elasticsearch.common.time.DateFormatter; +import org.elasticsearch.test.rest.ESRestTestCase; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.rest.ESRestTestCase; import java.io.IOException; import java.io.InputStreamReader; diff --git a/x-pack/plugin/data-streams/qa/rest/src/yamlRestTest/java/org/elasticsearch/xpack/datastreams/DataStreamsClientYamlTestSuiteIT.java b/x-pack/plugin/data-streams/qa/rest/src/yamlRestTest/java/org/elasticsearch/xpack/datastreams/DataStreamsClientYamlTestSuiteIT.java index 1c59fe60052dc..44c2760ad80ec 100644 --- a/x-pack/plugin/data-streams/qa/rest/src/yamlRestTest/java/org/elasticsearch/xpack/datastreams/DataStreamsClientYamlTestSuiteIT.java +++ b/x-pack/plugin/data-streams/qa/rest/src/yamlRestTest/java/org/elasticsearch/xpack/datastreams/DataStreamsClientYamlTestSuiteIT.java @@ -8,6 +8,7 @@ package org.elasticsearch.xpack.datastreams; import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; + import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate; import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase; diff --git a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecks.java b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecks.java index 63e3ee50187a7..453f72ea6d6da 100644 --- a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecks.java +++ b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecks.java @@ -13,12 +13,14 @@ public class ClusterDeprecationChecks { static DeprecationIssue checkTransientSettingsExistence(ClusterState state) { if (state.metadata().transientSettings().isEmpty() == false) { - return new DeprecationIssue(DeprecationIssue.Level.WARNING, + return new DeprecationIssue( + DeprecationIssue.Level.WARNING, "Transient cluster settings are deprecated", "https://ela.st/es-deprecation-7-transient-cluster-settings", "Use persistent settings to configure your cluster.", false, - null); + null + ); } return null; } diff --git a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/Deprecation.java b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/Deprecation.java index 0851e567789f1..9b594ceec47de 100644 --- a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/Deprecation.java +++ b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/Deprecation.java @@ -19,7 +19,6 @@ import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsFilter; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.env.Environment; import org.elasticsearch.env.NodeEnvironment; import org.elasticsearch.plugins.ActionPlugin; @@ -30,6 +29,7 @@ import org.elasticsearch.script.ScriptService; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.watcher.ResourceWatcherService; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.deprecation.logging.DeprecationCacheResetAction; import org.elasticsearch.xpack.deprecation.logging.DeprecationIndexingComponent; import org.elasticsearch.xpack.deprecation.logging.DeprecationIndexingTemplateRegistry; @@ -64,17 +64,22 @@ public class Deprecation extends Plugin implements ActionPlugin { @Override public List> getActions() { return List.of( - new ActionHandler<>(DeprecationInfoAction.INSTANCE, TransportDeprecationInfoAction.class), - new ActionHandler<>(NodesDeprecationCheckAction.INSTANCE, TransportNodeDeprecationCheckAction.class), - new ActionHandler<>(DeprecationCacheResetAction.INSTANCE, TransportDeprecationCacheResetAction.class)); + new ActionHandler<>(DeprecationInfoAction.INSTANCE, TransportDeprecationInfoAction.class), + new ActionHandler<>(NodesDeprecationCheckAction.INSTANCE, TransportNodeDeprecationCheckAction.class), + new ActionHandler<>(DeprecationCacheResetAction.INSTANCE, TransportDeprecationCacheResetAction.class) + ); } @Override - public List getRestHandlers(Settings settings, RestController restController, ClusterSettings clusterSettings, - IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter, - IndexNameExpressionResolver indexNameExpressionResolver, - Supplier nodesInCluster) { - + public List getRestHandlers( + Settings settings, + RestController restController, + ClusterSettings clusterSettings, + IndexScopedSettings indexScopedSettings, + SettingsFilter settingsFilter, + IndexNameExpressionResolver indexNameExpressionResolver, + Supplier nodesInCluster + ) { return List.of(new RestDeprecationInfoAction(), new RestDeprecationCacheResetAction()); } @@ -93,33 +98,36 @@ public Collection createComponents( IndexNameExpressionResolver indexNameExpressionResolver, Supplier repositoriesServiceSupplier ) { - final DeprecationIndexingTemplateRegistry templateRegistry = - new DeprecationIndexingTemplateRegistry(environment.settings(), clusterService, threadPool, client, xContentRegistry); + final DeprecationIndexingTemplateRegistry templateRegistry = new DeprecationIndexingTemplateRegistry( + environment.settings(), + clusterService, + threadPool, + client, + xContentRegistry + ); templateRegistry.initialize(); final RateLimitingFilter rateLimitingFilterForIndexing = new RateLimitingFilter(); // enable on start. rateLimitingFilterForIndexing.setUseXOpaqueId(USE_X_OPAQUE_ID_IN_FILTERING.get(environment.settings())); - final DeprecationIndexingComponent component = new DeprecationIndexingComponent(client, + final DeprecationIndexingComponent component = new DeprecationIndexingComponent( + client, environment.settings(), rateLimitingFilterForIndexing, - WRITE_DEPRECATION_LOGS_TO_INDEX.get(environment.settings()) //pass the default on startup + WRITE_DEPRECATION_LOGS_TO_INDEX.get(environment.settings()) // pass the default on startup ); - clusterService.getClusterSettings().addSettingsUpdateConsumer(USE_X_OPAQUE_ID_IN_FILTERING, - rateLimitingFilterForIndexing::setUseXOpaqueId); - clusterService.getClusterSettings().addSettingsUpdateConsumer(WRITE_DEPRECATION_LOGS_TO_INDEX, - component::enableDeprecationLogIndexing); + clusterService.getClusterSettings() + .addSettingsUpdateConsumer(USE_X_OPAQUE_ID_IN_FILTERING, rateLimitingFilterForIndexing::setUseXOpaqueId); + clusterService.getClusterSettings() + .addSettingsUpdateConsumer(WRITE_DEPRECATION_LOGS_TO_INDEX, component::enableDeprecationLogIndexing); return List.of(component, rateLimitingFilterForIndexing); } @Override public List> getSettings() { - return List.of( - USE_X_OPAQUE_ID_IN_FILTERING, - WRITE_DEPRECATION_LOGS_TO_INDEX, - SKIP_DEPRECATIONS_SETTING); + return List.of(USE_X_OPAQUE_ID_IN_FILTERING, WRITE_DEPRECATION_LOGS_TO_INDEX, SKIP_DEPRECATIONS_SETTING); } } diff --git a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/DeprecationChecks.java b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/DeprecationChecks.java index a028e1558c5c4..5cdecc87d5c9e 100644 --- a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/DeprecationChecks.java +++ b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/DeprecationChecks.java @@ -26,17 +26,15 @@ */ public class DeprecationChecks { - public static final Setting> SKIP_DEPRECATIONS_SETTING = - Setting.listSetting( - "deprecation.skip_deprecated_settings", - Collections.emptyList(), - Function.identity(), - Setting.Property.NodeScope, - Setting.Property.Dynamic - ); + public static final Setting> SKIP_DEPRECATIONS_SETTING = Setting.listSetting( + "deprecation.skip_deprecated_settings", + Collections.emptyList(), + Function.identity(), + Setting.Property.NodeScope, + Setting.Property.Dynamic + ); - private DeprecationChecks() { - } + private DeprecationChecks() {} static List> CLUSTER_SETTINGS_CHECKS = List.of( ClusterDeprecationChecks::checkTransientSettingsExistence diff --git a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/DeprecationInfoAction.java b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/DeprecationInfoAction.java index 5ec3c7d84cb6b..c8818e32ce19a 100644 --- a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/DeprecationInfoAction.java +++ b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/DeprecationInfoAction.java @@ -25,9 +25,9 @@ import org.elasticsearch.common.regex.Regex; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.set.Sets; +import org.elasticsearch.rest.RestStatus; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.rest.RestStatus; import org.elasticsearch.xpack.core.deprecation.DeprecationIssue; import java.io.IOException; @@ -73,14 +73,18 @@ private static List mergeNodeIssues(NodesDeprecationCheckRespo } } - return issueListMap.entrySet().stream() - .map(entry -> { - DeprecationIssue issue = entry.getKey(); - String details = issue.getDetails() != null ? issue.getDetails() + " " : ""; - return new DeprecationIssue(issue.getLevel(), issue.getMessage(), issue.getUrl(), - details + "(nodes impacted: " + entry.getValue() + ")", issue.isResolveDuringRollingUpgrade(), - issue.getMeta()); - }).collect(Collectors.toList()); + return issueListMap.entrySet().stream().map(entry -> { + DeprecationIssue issue = entry.getKey(); + String details = issue.getDetails() != null ? issue.getDetails() + " " : ""; + return new DeprecationIssue( + issue.getLevel(), + issue.getMessage(), + issue.getUrl(), + details + "(nodes impacted: " + entry.getValue() + ")", + issue.isResolveDuringRollingUpgrade(), + issue.getMeta() + ); + }).collect(Collectors.toList()); } public static class Response extends ActionResponse implements ToXContentObject { @@ -104,10 +108,12 @@ public Response(StreamInput in) throws IOException { } } - public Response(List clusterSettingsIssues, - List nodeSettingsIssues, - Map> indexSettingsIssues, - Map> pluginSettingsIssues) { + public Response( + List clusterSettingsIssues, + List nodeSettingsIssues, + Map> indexSettingsIssues, + Map> pluginSettingsIssues + ) { this.clusterSettingsIssues = clusterSettingsIssues; this.nodeSettingsIssues = nodeSettingsIssues; this.indexSettingsIssues = indexSettingsIssues; @@ -166,10 +172,10 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Response response = (Response) o; - return Objects.equals(clusterSettingsIssues, response.clusterSettingsIssues) && - Objects.equals(nodeSettingsIssues, response.nodeSettingsIssues) && - Objects.equals(indexSettingsIssues, response.indexSettingsIssues) && - Objects.equals(pluginSettingsIssues, response.pluginSettingsIssues); + return Objects.equals(clusterSettingsIssues, response.clusterSettingsIssues) + && Objects.equals(nodeSettingsIssues, response.nodeSettingsIssues) + && Objects.equals(indexSettingsIssues, response.indexSettingsIssues) + && Objects.equals(pluginSettingsIssues, response.pluginSettingsIssues); } @Override @@ -192,26 +198,29 @@ public int hashCode() { * @param clusterSettingsChecks The list of cluster-level checks * @return The list of deprecation issues found in the cluster */ - public static DeprecationInfoAction.Response from(ClusterState state, - IndexNameExpressionResolver indexNameExpressionResolver, - Request request, - NodesDeprecationCheckResponse nodeDeprecationResponse, - List> indexSettingsChecks, - List> clusterSettingsChecks, - Map> pluginSettingIssues, - List skipTheseDeprecatedSettings) { + public static DeprecationInfoAction.Response from( + ClusterState state, + IndexNameExpressionResolver indexNameExpressionResolver, + Request request, + NodesDeprecationCheckResponse nodeDeprecationResponse, + List> indexSettingsChecks, + List> clusterSettingsChecks, + Map> pluginSettingIssues, + List skipTheseDeprecatedSettings + ) { // Allow system index access here to prevent deprecation warnings when we call this API String[] concreteIndexNames = indexNameExpressionResolver.concreteIndexNames(state, request); ClusterState stateWithSkippedSettingsRemoved = removeSkippedSettings(state, concreteIndexNames, skipTheseDeprecatedSettings); - List clusterSettingsIssues = filterChecks(clusterSettingsChecks, - (c) -> c.apply(stateWithSkippedSettingsRemoved)); + List clusterSettingsIssues = filterChecks( + clusterSettingsChecks, + (c) -> c.apply(stateWithSkippedSettingsRemoved) + ); List nodeSettingsIssues = mergeNodeIssues(nodeDeprecationResponse); Map> indexSettingsIssues = new HashMap<>(); for (String concreteIndex : concreteIndexNames) { IndexMetadata indexMetadata = stateWithSkippedSettingsRemoved.getMetadata().index(concreteIndex); - List singleIndexIssues = filterChecks(indexSettingsChecks, - c -> c.apply(indexMetadata)); + List singleIndexIssues = filterChecks(indexSettingsChecks, c -> c.apply(indexMetadata)); if (singleIndexIssues.size() > 0) { indexSettingsIssues.put(concreteIndex, singleIndexIssues); } @@ -241,15 +250,17 @@ private static ClusterState removeSkippedSettings(ClusterState state, String[] i ClusterState.Builder clusterStateBuilder = new ClusterState.Builder(state); Metadata.Builder metadataBuilder = Metadata.builder(state.metadata()); metadataBuilder.transientSettings( - metadataBuilder.transientSettings().filter(setting -> Regex.simpleMatch(skipTheseDeprecatedSettings, setting) == false)); + metadataBuilder.transientSettings().filter(setting -> Regex.simpleMatch(skipTheseDeprecatedSettings, setting) == false) + ); metadataBuilder.persistentSettings( - metadataBuilder.persistentSettings().filter(setting -> Regex.simpleMatch(skipTheseDeprecatedSettings, setting) == false)); + metadataBuilder.persistentSettings().filter(setting -> Regex.simpleMatch(skipTheseDeprecatedSettings, setting) == false) + ); ImmutableOpenMap.Builder indicesBuilder = ImmutableOpenMap.builder(state.getMetadata().indices()); for (String indexName : indexNames) { IndexMetadata indexMetadata = state.getMetadata().index(indexName); IndexMetadata.Builder filteredIndexMetadataBuilder = new IndexMetadata.Builder(indexMetadata); - Settings filteredSettings = - indexMetadata.getSettings().filter(setting -> Regex.simpleMatch(skipTheseDeprecatedSettings, setting) == false); + Settings filteredSettings = indexMetadata.getSettings() + .filter(setting -> Regex.simpleMatch(skipTheseDeprecatedSettings, setting) == false); filteredIndexMetadataBuilder.settings(filteredSettings); indicesBuilder.put(indexName, filteredIndexMetadataBuilder.build()); } diff --git a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/IndexDeprecationChecks.java b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/IndexDeprecationChecks.java index 3bd4e037e4833..1e24bddd4dd54 100644 --- a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/IndexDeprecationChecks.java +++ b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/IndexDeprecationChecks.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.xpack.deprecation; - import org.elasticsearch.Version; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.index.IndexModule; @@ -24,13 +23,15 @@ public class IndexDeprecationChecks { static DeprecationIssue oldIndicesCheck(IndexMetadata indexMetadata) { Version createdWith = indexMetadata.getCreationVersion(); if (createdWith.before(Version.V_7_0_0)) { - return new DeprecationIssue(DeprecationIssue.Level.CRITICAL, - "Index created before 7.0", - "https://www.elastic.co/guide/en/elasticsearch/reference/master/" + - "breaking-changes-8.0.html", - "This index was created using version: " + createdWith, - false, null); - } + return new DeprecationIssue( + DeprecationIssue.Level.CRITICAL, + "Index created before 7.0", + "https://www.elastic.co/guide/en/elasticsearch/reference/master/" + "breaking-changes-8.0.html", + "This index was created using version: " + createdWith, + false, + null + ); + } return null; } @@ -39,12 +40,15 @@ static DeprecationIssue translogRetentionSettingCheck(IndexMetadata indexMetadat if (softDeletesEnabled) { if (IndexSettings.INDEX_TRANSLOG_RETENTION_SIZE_SETTING.exists(indexMetadata.getSettings()) || IndexSettings.INDEX_TRANSLOG_RETENTION_AGE_SETTING.exists(indexMetadata.getSettings())) { - return new DeprecationIssue(DeprecationIssue.Level.WARNING, + return new DeprecationIssue( + DeprecationIssue.Level.WARNING, "translog retention settings are ignored", "https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules-translog.html", - "translog retention settings [index.translog.retention.size] and [index.translog.retention.age] are ignored " + - "because translog is no longer used in peer recoveries with soft-deletes enabled (default in 7.0 or later)", - false, null); + "translog retention settings [index.translog.retention.size] and [index.translog.retention.age] are ignored " + + "because translog is no longer used in peer recoveries with soft-deletes enabled (default in 7.0 or later)", + false, + null + ); } } return null; @@ -52,10 +56,13 @@ static DeprecationIssue translogRetentionSettingCheck(IndexMetadata indexMetadat static DeprecationIssue checkIndexDataPath(IndexMetadata indexMetadata) { if (IndexMetadata.INDEX_DATA_PATH_SETTING.exists(indexMetadata.getSettings())) { - final String message = String.format(Locale.ROOT, - "setting [%s] is deprecated and will be removed in a future version", IndexMetadata.INDEX_DATA_PATH_SETTING.getKey()); - final String url = "https://www.elastic.co/guide/en/elasticsearch/reference/7.13/" + - "breaking-changes-7.13.html#deprecate-shared-data-path-setting"; + final String message = String.format( + Locale.ROOT, + "setting [%s] is deprecated and will be removed in a future version", + IndexMetadata.INDEX_DATA_PATH_SETTING.getKey() + ); + final String url = "https://www.elastic.co/guide/en/elasticsearch/reference/7.13/" + + "breaking-changes-7.13.html#deprecate-shared-data-path-setting"; final String details = "Found index data path configured. Discontinue use of this setting."; return new DeprecationIssue(DeprecationIssue.Level.CRITICAL, message, url, details, false, null); } @@ -65,12 +72,16 @@ static DeprecationIssue checkIndexDataPath(IndexMetadata indexMetadata) { static DeprecationIssue storeTypeSettingCheck(IndexMetadata indexMetadata) { final String storeType = IndexModule.INDEX_STORE_TYPE_SETTING.get(indexMetadata.getSettings()); if (IndexModule.Type.SIMPLEFS.match(storeType)) { - return new DeprecationIssue(DeprecationIssue.Level.WARNING, + return new DeprecationIssue( + DeprecationIssue.Level.WARNING, "[simplefs] is deprecated and will be removed in future versions", "https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules-store.html", - "[simplefs] is deprecated and will be removed in 8.0. Use [niofs] or other file systems instead. " + - "Elasticsearch 7.15 or later uses [niofs] for the [simplefs] store type " + - "as it offers superior or equivalent performance to [simplefs].", false, null); + "[simplefs] is deprecated and will be removed in 8.0. Use [niofs] or other file systems instead. " + + "Elasticsearch 7.15 or later uses [niofs] for the [simplefs] store type " + + "as it offers superior or equivalent performance to [simplefs].", + false, + null + ); } return null; } @@ -81,8 +92,9 @@ static DeprecationIssue frozenIndexSettingCheck(IndexMetadata indexMetadata) { String indexName = indexMetadata.getIndex().getName(); return new DeprecationIssue( DeprecationIssue.Level.WARNING, - "index [" + indexName + - "] is a frozen index. The frozen indices feature is deprecated and will be removed in a future version", + "index [" + + indexName + + "] is a frozen index. The frozen indices feature is deprecated and will be removed in a future version", "https://www.elastic.co/guide/en/elasticsearch/reference/master/frozen-indices.html", "Frozen indices no longer offer any advantages. Consider cold or frozen tiers in place of frozen indices.", false, diff --git a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/MlDeprecationChecker.java b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/MlDeprecationChecker.java index 1e188bc7c8820..f04aa8c582367 100644 --- a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/MlDeprecationChecker.java +++ b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/MlDeprecationChecker.java @@ -10,8 +10,8 @@ import org.elasticsearch.Version; import org.elasticsearch.action.ActionListener; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.common.xcontent.XContentElasticsearchExtension; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.xpack.core.action.util.PageParams; import org.elasticsearch.xpack.core.deprecation.DeprecationIssue; @@ -34,10 +34,16 @@ static Optional checkDataFeedQuery(DatafeedConfig datafeedConf if (deprecations.isEmpty()) { return Optional.empty(); } else { - return Optional.of(new DeprecationIssue(DeprecationIssue.Level.WARNING, - "Datafeed [" + datafeedConfig.getId() + "] uses deprecated query options", - "https://www.elastic.co/guide/en/elasticsearch/reference/master/breaking-changes-7.0.html#breaking_70_search_changes", - deprecations.toString(), false, null)); + return Optional.of( + new DeprecationIssue( + DeprecationIssue.Level.WARNING, + "Datafeed [" + datafeedConfig.getId() + "] uses deprecated query options", + "https://www.elastic.co/guide/en/elasticsearch/reference/master/breaking-changes-7.0.html#breaking_70_search_changes", + deprecations.toString(), + false, + null + ) + ); } } @@ -46,40 +52,55 @@ static Optional checkDataFeedAggregations(DatafeedConfig dataf if (deprecations.isEmpty()) { return Optional.empty(); } else { - return Optional.of(new DeprecationIssue(DeprecationIssue.Level.WARNING, - "Datafeed [" + datafeedConfig.getId() + "] uses deprecated aggregation options", - "https://www.elastic.co/guide/en/elasticsearch/reference/master/breaking-changes-7.0.html" + - "#breaking_70_aggregations_changes", deprecations.toString(), false, null)); + return Optional.of( + new DeprecationIssue( + DeprecationIssue.Level.WARNING, + "Datafeed [" + datafeedConfig.getId() + "] uses deprecated aggregation options", + "https://www.elastic.co/guide/en/elasticsearch/reference/master/breaking-changes-7.0.html" + + "#breaking_70_aggregations_changes", + deprecations.toString(), + false, + null + ) + ); } } static Optional checkModelSnapshot(ModelSnapshot modelSnapshot) { if (modelSnapshot.getMinVersion().before(Version.V_7_0_0)) { - StringBuilder details = new StringBuilder(String.format( - Locale.ROOT, - "model snapshot [%s] for job [%s] supports minimum version [%s] and needs to be at least [%s].", - modelSnapshot.getSnapshotId(), - modelSnapshot.getJobId(), - modelSnapshot.getMinVersion(), - Version.V_7_0_0)); - if (modelSnapshot.getLatestRecordTimeStamp() != null) { - details.append(String.format( - Locale.ROOT, - " The model snapshot's latest record timestamp is [%s]", - XContentElasticsearchExtension.DEFAULT_FORMATTER.format(modelSnapshot.getLatestRecordTimeStamp().toInstant()) - )); - } - return Optional.of(new DeprecationIssue(DeprecationIssue.Level.CRITICAL, + StringBuilder details = new StringBuilder( String.format( Locale.ROOT, - "model snapshot [%s] for job [%s] needs to be deleted or upgraded", + "model snapshot [%s] for job [%s] supports minimum version [%s] and needs to be at least [%s].", modelSnapshot.getSnapshotId(), - modelSnapshot.getJobId() - ), - "https://www.elastic.co/guide/en/elasticsearch/reference/master/ml-upgrade-job-model-snapshot.html", - details.toString(), - false, - Map.of("job_id", modelSnapshot.getJobId(), "snapshot_id", modelSnapshot.getSnapshotId())) + modelSnapshot.getJobId(), + modelSnapshot.getMinVersion(), + Version.V_7_0_0 + ) + ); + if (modelSnapshot.getLatestRecordTimeStamp() != null) { + details.append( + String.format( + Locale.ROOT, + " The model snapshot's latest record timestamp is [%s]", + XContentElasticsearchExtension.DEFAULT_FORMATTER.format(modelSnapshot.getLatestRecordTimeStamp().toInstant()) + ) + ); + } + return Optional.of( + new DeprecationIssue( + DeprecationIssue.Level.CRITICAL, + String.format( + Locale.ROOT, + "model snapshot [%s] for job [%s] needs to be deleted or upgraded", + modelSnapshot.getSnapshotId(), + modelSnapshot.getJobId() + ), + "https://www.elastic.co/guide/en/elasticsearch/reference/master/ml-upgrade-job-model-snapshot.html", + details.toString(), + false, + Map.of("job_id", modelSnapshot.getJobId(), "snapshot_id", modelSnapshot.getSnapshotId()) + ) ); } return Optional.empty(); @@ -102,34 +123,28 @@ public void check(Components components, ActionListener deprecation getModelSnapshots.setSort(ModelSnapshot.MIN_VERSION.getPreferredName()); ActionListener getModelSnaphots = ActionListener.wrap( - _unused -> components.client().execute( - GetModelSnapshotsAction.INSTANCE, - getModelSnapshots, - ActionListener.wrap( - modelSnapshots -> { - modelSnapshots.getResources() - .results() - .forEach(modelSnapshot -> checkModelSnapshot(modelSnapshot) - .ifPresent(issues::add)); - deprecationIssueListener.onResponse(new CheckResult(getName(), issues)); - }, - deprecationIssueListener::onFailure) - ), - deprecationIssueListener::onFailure); + _unused -> components.client() + .execute(GetModelSnapshotsAction.INSTANCE, getModelSnapshots, ActionListener.wrap(modelSnapshots -> { + modelSnapshots.getResources() + .results() + .forEach(modelSnapshot -> checkModelSnapshot(modelSnapshot).ifPresent(issues::add)); + deprecationIssueListener.onResponse(new CheckResult(getName(), issues)); + }, deprecationIssueListener::onFailure)), + deprecationIssueListener::onFailure + ); - components.client().execute( - GetDatafeedsAction.INSTANCE, - new GetDatafeedsAction.Request(GetDatafeedsAction.ALL), ActionListener.wrap( - datafeedsResponse -> { + components.client() + .execute( + GetDatafeedsAction.INSTANCE, + new GetDatafeedsAction.Request(GetDatafeedsAction.ALL), + ActionListener.wrap(datafeedsResponse -> { for (DatafeedConfig df : datafeedsResponse.getResponse().results()) { checkDataFeedAggregations(df, components.xContentRegistry()).ifPresent(issues::add); checkDataFeedQuery(df, components.xContentRegistry()).ifPresent(issues::add); } getModelSnaphots.onResponse(null); - }, - deprecationIssueListener::onFailure - ) - ); + }, deprecationIssueListener::onFailure) + ); } @Override diff --git a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/NodeDeprecationChecks.java b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/NodeDeprecationChecks.java index 5d69db09bc595..9d4ca136bd448 100644 --- a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/NodeDeprecationChecks.java +++ b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/NodeDeprecationChecks.java @@ -33,17 +33,28 @@ public class NodeDeprecationChecks { - static DeprecationIssue checkDeprecatedSetting(final Settings settings, final Setting deprecatedSetting, final String url, - final String whenRemoved) { + static DeprecationIssue checkDeprecatedSetting( + final Settings settings, + final Setting deprecatedSetting, + final String url, + final String whenRemoved + ) { if (deprecatedSetting.exists(settings) == false) { return null; } final String deprecatedSettingKey = deprecatedSetting.getKey(); final String value = deprecatedSetting.get(settings).toString(); - final String message = - String.format(Locale.ROOT, "setting [%s] is deprecated and will be removed " + whenRemoved, deprecatedSettingKey); - final String details = - String.format(Locale.ROOT, "the setting [%s] is currently set to [%s], remove this setting", deprecatedSettingKey, value); + final String message = String.format( + Locale.ROOT, + "setting [%s] is deprecated and will be removed " + whenRemoved, + deprecatedSettingKey + ); + final String details = String.format( + Locale.ROOT, + "the setting [%s] is currently set to [%s], remove this setting", + deprecatedSettingKey, + value + ); return new DeprecationIssue(DeprecationIssue.Level.WARNING, message, url, details, false, null); } @@ -53,19 +64,29 @@ static DeprecationIssue checkRemovedSetting(final Settings settings, final Setti } final String removedSettingKey = removedSetting.getKey(); final String value = removedSetting.get(settings).toString(); - final String message = - String.format(Locale.ROOT, "setting [%s] is deprecated and will be removed in the next major version", removedSettingKey); - final String details = - String.format(Locale.ROOT, "the setting [%s] is currently set to [%s], remove this setting", removedSettingKey, value); + final String message = String.format( + Locale.ROOT, + "setting [%s] is deprecated and will be removed in the next major version", + removedSettingKey + ); + final String details = String.format( + Locale.ROOT, + "the setting [%s] is currently set to [%s], remove this setting", + removedSettingKey, + value + ); return new DeprecationIssue(DeprecationIssue.Level.CRITICAL, message, url, details, false, null); } static DeprecationIssue checkSharedDataPathSetting(final Settings settings, final PluginsAndModules pluginsAndModules) { if (Environment.PATH_SHARED_DATA_SETTING.exists(settings)) { - final String message = String.format(Locale.ROOT, - "setting [%s] is deprecated and will be removed in a future version", Environment.PATH_SHARED_DATA_SETTING.getKey()); - final String url = "https://www.elastic.co/guide/en/elasticsearch/reference/7.13/" + - "breaking-changes-7.13.html#deprecate-shared-data-path-setting"; + final String message = String.format( + Locale.ROOT, + "setting [%s] is deprecated and will be removed in a future version", + Environment.PATH_SHARED_DATA_SETTING.getKey() + ); + final String url = "https://www.elastic.co/guide/en/elasticsearch/reference/7.13/" + + "breaking-changes-7.13.html#deprecate-shared-data-path-setting"; final String details = "Found shared data path configured. Discontinue use of this setting."; return new DeprecationIssue(DeprecationIssue.Level.CRITICAL, message, url, details, false, null); } @@ -86,39 +107,55 @@ static DeprecationIssue checkReservedPrefixedRealmNames(final Settings settings, if (reservedPrefixedRealmIdentifiers.isEmpty()) { return null; } else { - return new DeprecationIssue(DeprecationIssue.Level.CRITICAL, + return new DeprecationIssue( + DeprecationIssue.Level.CRITICAL, "Realm that start with [" + RESERVED_REALM_NAME_PREFIX + "] will not be permitted in a future major release.", "https://www.elastic.co/guide/en/elasticsearch/reference/7.14/deprecated-7.14.html#reserved-prefixed-realm-names", - String.format(Locale.ROOT, - "Found realm " + (reservedPrefixedRealmIdentifiers.size() == 1 ? "name" : "names") + String.format( + Locale.ROOT, + "Found realm " + + (reservedPrefixedRealmIdentifiers.size() == 1 ? "name" : "names") + " with reserved prefix [%s]: [%s]. " + "In a future major release, node will fail to start if any realm names start with reserved prefix.", RESERVED_REALM_NAME_PREFIX, reservedPrefixedRealmIdentifiers.stream() .map(rid -> RealmSettings.PREFIX + rid.getType() + "." + rid.getName()) .sorted() - .collect(Collectors.joining("; "))), false, null); + .collect(Collectors.joining("; ")) + ), + false, + null + ); } } static DeprecationIssue checkSingleDataNodeWatermarkSetting(final Settings settings, final PluginsAndModules pluginsAndModules) { if (DiskThresholdDecider.ENABLE_FOR_SINGLE_DATA_NODE.exists(settings)) { String key = DiskThresholdDecider.ENABLE_FOR_SINGLE_DATA_NODE.getKey(); - return new DeprecationIssue(DeprecationIssue.Level.CRITICAL, + return new DeprecationIssue( + DeprecationIssue.Level.CRITICAL, String.format(Locale.ROOT, "setting [%s] is deprecated and will not be available in a future version", key), - "https://www.elastic.co/guide/en/elasticsearch/reference/7.14/" + - "breaking-changes-7.14.html#deprecate-single-data-node-watermark", + "https://www.elastic.co/guide/en/elasticsearch/reference/7.14/" + + "breaking-changes-7.14.html#deprecate-single-data-node-watermark", String.format(Locale.ROOT, "found [%s] configured. Discontinue use of this setting.", key), - false, null); + false, + null + ); } return null; } - private static DeprecationIssue deprecatedAffixSetting(Setting.AffixSetting deprecatedAffixSetting, String detailPattern, - String url, DeprecationIssue.Level warningLevel, Settings settings) { + private static DeprecationIssue deprecatedAffixSetting( + Setting.AffixSetting deprecatedAffixSetting, + String detailPattern, + String url, + DeprecationIssue.Level warningLevel, + Settings settings + ) { List> deprecatedConcreteSettings = deprecatedAffixSetting.getAllConcreteSettings(settings) - .sorted(Comparator.comparing(Setting::getKey)).collect(Collectors.toList()); + .sorted(Comparator.comparing(Setting::getKey)) + .collect(Collectors.toList()); if (deprecatedConcreteSettings.isEmpty()) { return null; @@ -135,11 +172,16 @@ private static DeprecationIssue deprecatedAffixSetting(Setting.AffixSetting d return new DeprecationIssue(warningLevel, message, url, details, false, null); } - private static DeprecationIssue deprecatedAffixGroupedSetting(Setting.AffixSetting deprecatedAffixSetting, - String detailPattern, String url, DeprecationIssue.Level warningLevel, - Settings settings) { + private static DeprecationIssue deprecatedAffixGroupedSetting( + Setting.AffixSetting deprecatedAffixSetting, + String detailPattern, + String url, + DeprecationIssue.Level warningLevel, + Settings settings + ) { List> deprecatedConcreteSettings = deprecatedAffixSetting.getAllConcreteSettings(settings) - .sorted(Comparator.comparing(Setting::getKey)).collect(Collectors.toList()); + .sorted(Comparator.comparing(Setting::getKey)) + .collect(Collectors.toList()); if (deprecatedConcreteSettings.isEmpty()) { return null; @@ -177,22 +219,26 @@ static DeprecationIssue genericMonitoringSetting(final Settings settings, final static DeprecationIssue genericMonitoringAffixSetting(final Settings settings, final String deprecatedSuffix) { return deprecatedAffixSetting( - Setting.affixKeySetting("xpack.monitoring.exporters.", deprecatedSuffix, - (Function>) Setting::simpleString), + Setting.affixKeySetting( + "xpack.monitoring.exporters.", + deprecatedSuffix, + (Function>) Setting::simpleString + ), "Remove the following settings from elasticsearch.yml: [%s]", MONITORING_SETTING_DEPRECATION_LINK, DeprecationIssue.Level.WARNING, - settings); + settings + ); } static DeprecationIssue genericMonitoringAffixSecureSetting(final Settings settings, final String deprecatedSuffix) { return deprecatedAffixSetting( - Setting.affixKeySetting("xpack.monitoring.exporters.", deprecatedSuffix, - k -> SecureSetting.secureString(k, null)), + Setting.affixKeySetting("xpack.monitoring.exporters.", deprecatedSuffix, k -> SecureSetting.secureString(k, null)), "Remove the following settings from the keystore: [%s]", MONITORING_SETTING_DEPRECATION_LINK, DeprecationIssue.Level.WARNING, - settings); + settings + ); } static DeprecationIssue genericMonitoringAffixGroupedSetting(final Settings settings, final String deprecatedSuffix) { @@ -201,7 +247,8 @@ static DeprecationIssue genericMonitoringAffixGroupedSetting(final Settings sett "Remove the following settings from elasticsearch.yml: [%s]", MONITORING_SETTING_DEPRECATION_LINK, DeprecationIssue.Level.WARNING, - settings); + settings + ); } static DeprecationIssue checkMonitoringSettingHistoryDuration(final Settings settings, final PluginsAndModules pluginsAndModules) { @@ -220,33 +267,45 @@ static DeprecationIssue checkMonitoringSettingCollectCcrTimeout(final Settings s return genericMonitoringSetting(settings, Setting.simpleString("xpack.monitoring.collection.ccr.stats.timeout")); } - static DeprecationIssue checkMonitoringSettingCollectEnrichStatsTimeout(final Settings settings, - final PluginsAndModules pluginsAndModules) { + static DeprecationIssue checkMonitoringSettingCollectEnrichStatsTimeout( + final Settings settings, + final PluginsAndModules pluginsAndModules + ) { return genericMonitoringSetting(settings, Setting.simpleString("xpack.monitoring.collection.enrich.stats.timeout")); } - static DeprecationIssue checkMonitoringSettingCollectIndexRecoveryStatsTimeout(final Settings settings, - final PluginsAndModules pluginsAndModules) { + static DeprecationIssue checkMonitoringSettingCollectIndexRecoveryStatsTimeout( + final Settings settings, + final PluginsAndModules pluginsAndModules + ) { return genericMonitoringSetting(settings, Setting.simpleString("xpack.monitoring.collection.index.recovery.timeout")); } - static DeprecationIssue checkMonitoringSettingCollectIndexStatsTimeout(final Settings settings, - final PluginsAndModules pluginsAndModules) { + static DeprecationIssue checkMonitoringSettingCollectIndexStatsTimeout( + final Settings settings, + final PluginsAndModules pluginsAndModules + ) { return genericMonitoringSetting(settings, Setting.simpleString("xpack.monitoring.collection.index.stats.timeout")); } - static DeprecationIssue checkMonitoringSettingCollectMlJobStatsTimeout(final Settings settings, - final PluginsAndModules pluginsAndModules) { + static DeprecationIssue checkMonitoringSettingCollectMlJobStatsTimeout( + final Settings settings, + final PluginsAndModules pluginsAndModules + ) { return genericMonitoringSetting(settings, Setting.simpleString("xpack.monitoring.collection.ml.job.stats.timeout")); } - static DeprecationIssue checkMonitoringSettingCollectNodeStatsTimeout(final Settings settings, - final PluginsAndModules pluginsAndModules) { + static DeprecationIssue checkMonitoringSettingCollectNodeStatsTimeout( + final Settings settings, + final PluginsAndModules pluginsAndModules + ) { return genericMonitoringSetting(settings, Setting.simpleString("xpack.monitoring.collection.node.stats.timeout")); } - static DeprecationIssue checkMonitoringSettingCollectClusterStatsTimeout(final Settings settings, - final PluginsAndModules pluginsAndModules) { + static DeprecationIssue checkMonitoringSettingCollectClusterStatsTimeout( + final Settings settings, + final PluginsAndModules pluginsAndModules + ) { return genericMonitoringSetting(settings, Setting.simpleString("xpack.monitoring.collection.cluster.stats.timeout")); } @@ -258,18 +317,24 @@ static DeprecationIssue checkMonitoringSettingExportersBulkTimeout(final Setting return genericMonitoringAffixSetting(settings, "bulk.timeout"); } - static DeprecationIssue checkMonitoringSettingExportersConnectionTimeout(final Settings settings, - final PluginsAndModules pluginsAndModules) { + static DeprecationIssue checkMonitoringSettingExportersConnectionTimeout( + final Settings settings, + final PluginsAndModules pluginsAndModules + ) { return genericMonitoringAffixSetting(settings, "connection.timeout"); } - static DeprecationIssue checkMonitoringSettingExportersConnectionReadTimeout(final Settings settings, - final PluginsAndModules pluginsAndModules) { + static DeprecationIssue checkMonitoringSettingExportersConnectionReadTimeout( + final Settings settings, + final PluginsAndModules pluginsAndModules + ) { return genericMonitoringAffixSetting(settings, "connection.read_timeout"); } - static DeprecationIssue checkMonitoringSettingExportersAuthUsername(final Settings settings, - final PluginsAndModules pluginsAndModules) { + static DeprecationIssue checkMonitoringSettingExportersAuthUsername( + final Settings settings, + final PluginsAndModules pluginsAndModules + ) { return genericMonitoringAffixSetting(settings, "auth.username"); } @@ -285,8 +350,10 @@ static DeprecationIssue checkMonitoringSettingExportersProxyBase(final Settings return genericMonitoringAffixSetting(settings, "proxy.base_path"); } - static DeprecationIssue checkMonitoringSettingExportersSniffEnabled(final Settings settings, - final PluginsAndModules pluginsAndModules) { + static DeprecationIssue checkMonitoringSettingExportersSniffEnabled( + final Settings settings, + final PluginsAndModules pluginsAndModules + ) { return genericMonitoringAffixSetting(settings, "sniff.enabled"); } @@ -294,13 +361,17 @@ static DeprecationIssue checkMonitoringSettingExportersHeaders(final Settings se return genericMonitoringAffixGroupedSetting(settings, "headers"); } - static DeprecationIssue checkMonitoringSettingExportersTemplateTimeout(final Settings settings, - final PluginsAndModules pluginsAndModules) { + static DeprecationIssue checkMonitoringSettingExportersTemplateTimeout( + final Settings settings, + final PluginsAndModules pluginsAndModules + ) { return genericMonitoringAffixSetting(settings, "index.template.master_timeout"); } - static DeprecationIssue checkMonitoringSettingExportersMasterTimeout(final Settings settings, - final PluginsAndModules pluginsAndModules) { + static DeprecationIssue checkMonitoringSettingExportersMasterTimeout( + final Settings settings, + final PluginsAndModules pluginsAndModules + ) { return genericMonitoringAffixSetting(settings, "wait_master.timeout"); } @@ -312,18 +383,24 @@ static DeprecationIssue checkMonitoringSettingExportersType(final Settings setti return genericMonitoringAffixSetting(settings, "type"); } - static DeprecationIssue checkMonitoringSettingExportersAlertsEnabled(final Settings settings, - final PluginsAndModules pluginsAndModules) { + static DeprecationIssue checkMonitoringSettingExportersAlertsEnabled( + final Settings settings, + final PluginsAndModules pluginsAndModules + ) { return genericMonitoringAffixSetting(settings, "cluster_alerts.management.enabled"); } - static DeprecationIssue checkMonitoringSettingExportersAlertsBlacklist(final Settings settings, - final PluginsAndModules pluginsAndModules) { + static DeprecationIssue checkMonitoringSettingExportersAlertsBlacklist( + final Settings settings, + final PluginsAndModules pluginsAndModules + ) { return genericMonitoringAffixSetting(settings, "cluster_alerts.management.blacklist"); } - static DeprecationIssue checkMonitoringSettingExportersIndexNameTimeFormat(final Settings settings, - final PluginsAndModules pluginsAndModules) { + static DeprecationIssue checkMonitoringSettingExportersIndexNameTimeFormat( + final Settings settings, + final PluginsAndModules pluginsAndModules + ) { return genericMonitoringAffixSetting(settings, "index.name.time_format"); } @@ -344,101 +421,130 @@ static DeprecationIssue checkMonitoringSettingCollectionInterval(final Settings } static DeprecationIssue checkExporterUseIngestPipelineSettings(final Settings settings, final PluginsAndModules pluginsAndModules) { - return deprecatedAffixSetting(MonitoringDeprecatedSettings.USE_INGEST_PIPELINE_SETTING, + return deprecatedAffixSetting( + MonitoringDeprecatedSettings.USE_INGEST_PIPELINE_SETTING, "Remove the following settings from elasticsearch.yml: [%s]", "https://ela.st/es-deprecation-7-monitoring-exporter-use-ingest-setting", DeprecationIssue.Level.WARNING, - settings); + settings + ); } static DeprecationIssue checkExporterPipelineMasterTimeoutSetting(final Settings settings, final PluginsAndModules pluginsAndModules) { - return deprecatedAffixSetting(MonitoringDeprecatedSettings.PIPELINE_CHECK_TIMEOUT_SETTING, + return deprecatedAffixSetting( + MonitoringDeprecatedSettings.PIPELINE_CHECK_TIMEOUT_SETTING, "Remove the following settings from elasticsearch.yml: [%s]", "https://ela.st/es-deprecation-7-monitoring-exporter-pipeline-timeout-setting", DeprecationIssue.Level.WARNING, - settings); + settings + ); } static DeprecationIssue checkExporterCreateLegacyTemplateSetting(final Settings settings, final PluginsAndModules pluginsAndModules) { - return deprecatedAffixSetting(MonitoringDeprecatedSettings.TEMPLATE_CREATE_LEGACY_VERSIONS_SETTING, + return deprecatedAffixSetting( + MonitoringDeprecatedSettings.TEMPLATE_CREATE_LEGACY_VERSIONS_SETTING, "Remove the following settings from elasticsearch.yml: [%s]", "https://ela.st/es-deprecation-7-monitoring-exporter-create-legacy-template-setting", DeprecationIssue.Level.WARNING, - settings); + settings + ); } - static DeprecationIssue checkScriptContextCache(final Settings settings, - final PluginsAndModules pluginsAndModules) { + static DeprecationIssue checkScriptContextCache(final Settings settings, final PluginsAndModules pluginsAndModules) { if (ScriptService.isUseContextCacheSet(settings)) { - return new DeprecationIssue(DeprecationIssue.Level.WARNING, + return new DeprecationIssue( + DeprecationIssue.Level.WARNING, ScriptService.USE_CONTEXT_RATE_KEY_DEPRECATION_MESSAGE, "https://ela.st/es-deprecation-7-script-context-cache", - "found deprecated script context caches in use, change setting to compilation rate or remove " + - "setting to use the default", - false, null); + "found deprecated script context caches in use, change setting to compilation rate or remove " + + "setting to use the default", + false, + null + ); } return null; } - static DeprecationIssue checkScriptContextCompilationsRateLimitSetting(final Settings settings, - final PluginsAndModules pluginsAndModules) { + static DeprecationIssue checkScriptContextCompilationsRateLimitSetting( + final Settings settings, + final PluginsAndModules pluginsAndModules + ) { Setting.AffixSetting maxSetting = ScriptService.SCRIPT_MAX_COMPILATIONS_RATE_SETTING; Set contextCompilationRates = maxSetting.getAsMap(settings).keySet(); if (contextCompilationRates.isEmpty() == false) { - String maxSettings = contextCompilationRates.stream().sorted().map( - c -> maxSetting.getConcreteSettingForNamespace(c).getKey() - ).collect(Collectors.joining(",")); - return new DeprecationIssue(DeprecationIssue.Level.WARNING, - String.format(Locale.ROOT, + String maxSettings = contextCompilationRates.stream() + .sorted() + .map(c -> maxSetting.getConcreteSettingForNamespace(c).getKey()) + .collect(Collectors.joining(",")); + return new DeprecationIssue( + DeprecationIssue.Level.WARNING, + String.format( + Locale.ROOT, "Setting context-specific rate limits [%s] is deprecated." - + " Use [%s] to rate limit the compilation of user scripts." - + " Context-specific caches are no longer needed to prevent system scripts from triggering rate limits.", - maxSettings, ScriptService.SCRIPT_GENERAL_MAX_COMPILATIONS_RATE_SETTING.getKey()), + + " Use [%s] to rate limit the compilation of user scripts." + + " Context-specific caches are no longer needed to prevent system scripts from triggering rate limits.", + maxSettings, + ScriptService.SCRIPT_GENERAL_MAX_COMPILATIONS_RATE_SETTING.getKey() + ), "https://ela.st/es-deprecation-7-script-context-cache", String.format(Locale.ROOT, "[%s] is deprecated and will be removed in a future release", maxSettings), - false, null); + false, + null + ); } return null; } - static DeprecationIssue checkScriptContextCacheSizeSetting(final Settings settings, - final PluginsAndModules pluginsAndModules) { + static DeprecationIssue checkScriptContextCacheSizeSetting(final Settings settings, final PluginsAndModules pluginsAndModules) { Setting.AffixSetting cacheSizeSetting = ScriptService.SCRIPT_CACHE_SIZE_SETTING; Set contextCacheSizes = cacheSizeSetting.getAsMap(settings).keySet(); if (contextCacheSizes.isEmpty() == false) { - String cacheSizeSettings = contextCacheSizes.stream().sorted().map( - c -> cacheSizeSetting.getConcreteSettingForNamespace(c).getKey() - ).collect(Collectors.joining(",")); - return new DeprecationIssue(DeprecationIssue.Level.WARNING, - String.format(Locale.ROOT, + String cacheSizeSettings = contextCacheSizes.stream() + .sorted() + .map(c -> cacheSizeSetting.getConcreteSettingForNamespace(c).getKey()) + .collect(Collectors.joining(",")); + return new DeprecationIssue( + DeprecationIssue.Level.WARNING, + String.format( + Locale.ROOT, "Setting a context-specific cache size [%s] is deprecated." - + " Use [%s] to configure the size of the general cache for scripts." - + " Context-specific caches are no longer needed to prevent system scripts from triggering rate limits.", - cacheSizeSettings, ScriptService.SCRIPT_GENERAL_CACHE_SIZE_SETTING.getKey()), + + " Use [%s] to configure the size of the general cache for scripts." + + " Context-specific caches are no longer needed to prevent system scripts from triggering rate limits.", + cacheSizeSettings, + ScriptService.SCRIPT_GENERAL_CACHE_SIZE_SETTING.getKey() + ), "https://ela.st/es-deprecation-7-script-context-cache", String.format(Locale.ROOT, "[%s] is deprecated and will be removed in a future release", cacheSizeSettings), - false, null); + false, + null + ); } return null; } - static DeprecationIssue checkScriptContextCacheExpirationSetting(final Settings settings, - final PluginsAndModules pluginsAndModules) { + static DeprecationIssue checkScriptContextCacheExpirationSetting(final Settings settings, final PluginsAndModules pluginsAndModules) { Setting.AffixSetting cacheExpireSetting = ScriptService.SCRIPT_CACHE_EXPIRE_SETTING; Set contextCacheExpires = cacheExpireSetting.getAsMap(settings).keySet(); if (contextCacheExpires.isEmpty() == false) { - String cacheExpireSettings = contextCacheExpires.stream().sorted().map( - c -> cacheExpireSetting.getConcreteSettingForNamespace(c).getKey() - ).collect(Collectors.joining(",")); - return new DeprecationIssue(DeprecationIssue.Level.WARNING, - String.format(Locale.ROOT, - "Setting a context-specific cache expiration [%s] is deprecated." + String cacheExpireSettings = contextCacheExpires.stream() + .sorted() + .map(c -> cacheExpireSetting.getConcreteSettingForNamespace(c).getKey()) + .collect(Collectors.joining(",")); + return new DeprecationIssue( + DeprecationIssue.Level.WARNING, + String.format( + Locale.ROOT, + "Setting a context-specific cache expiration [%s] is deprecated." + " Use [%s] to configure the expiration of the general cache." + " Context-specific caches are no longer needed to prevent system scripts from triggering rate limits.", - cacheExpireSettings, ScriptService.SCRIPT_GENERAL_CACHE_EXPIRE_SETTING.getKey()), + cacheExpireSettings, + ScriptService.SCRIPT_GENERAL_CACHE_EXPIRE_SETTING.getKey() + ), "https://ela.st/es-deprecation-7-script-context-cache", String.format(Locale.ROOT, "[%s] is deprecated and will be removed in a future release", cacheExpireSettings), - false, null); + false, + null + ); } return null; } @@ -446,11 +552,14 @@ static DeprecationIssue checkScriptContextCacheExpirationSetting(final Settings static DeprecationIssue checkEnforceDefaultTierPreferenceSetting(final Settings settings, final PluginsAndModules pluginsAndModules) { if (DataTier.ENFORCE_DEFAULT_TIER_PREFERENCE_SETTING.exists(settings)) { String key = DataTier.ENFORCE_DEFAULT_TIER_PREFERENCE_SETTING.getKey(); - return new DeprecationIssue(DeprecationIssue.Level.CRITICAL, + return new DeprecationIssue( + DeprecationIssue.Level.CRITICAL, String.format(Locale.ROOT, "setting [%s] is deprecated and will not be available in a future version", key), "https://www.elastic.co/guide/en/elasticsearch/reference/current/data-tiers.html", String.format(Locale.ROOT, "found [%s] configured. Discontinue use of this setting.", key), - false, null); + false, + null + ); } return null; diff --git a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/NodesDeprecationCheckAction.java b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/NodesDeprecationCheckAction.java index 590868f5578ea..b19e810676995 100644 --- a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/NodesDeprecationCheckAction.java +++ b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/NodesDeprecationCheckAction.java @@ -41,6 +41,7 @@ public NodeRequest(StreamInput in) throws IOException { super(in); request = new NodesDeprecationCheckRequest(in); } + public NodeRequest(NodesDeprecationCheckRequest request) { this.request = request; } @@ -80,8 +81,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; NodeResponse that = (NodeResponse) o; - return Objects.equals(getDeprecationIssues(), that.getDeprecationIssues()) - && Objects.equals(getNode(), that.getNode()); + return Objects.equals(getDeprecationIssues(), that.getDeprecationIssues()) && Objects.equals(getNode(), that.getNode()); } @Override @@ -90,12 +90,16 @@ public int hashCode() { } } - public static class RequestBuilder extends NodesOperationRequestBuilder { + public static class RequestBuilder extends NodesOperationRequestBuilder< + NodesDeprecationCheckRequest, + NodesDeprecationCheckResponse, + RequestBuilder> { - protected RequestBuilder(ElasticsearchClient client, - ActionType action, - NodesDeprecationCheckRequest request) { + protected RequestBuilder( + ElasticsearchClient client, + ActionType action, + NodesDeprecationCheckRequest request + ) { super(client, action, request); } } diff --git a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/NodesDeprecationCheckResponse.java b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/NodesDeprecationCheckResponse.java index 41c9c0cf84a7c..4708ae8eb90c5 100644 --- a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/NodesDeprecationCheckResponse.java +++ b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/NodesDeprecationCheckResponse.java @@ -23,9 +23,11 @@ public NodesDeprecationCheckResponse(StreamInput in) throws IOException { super(in); } - public NodesDeprecationCheckResponse(ClusterName clusterName, - List nodes, - List failures) { + public NodesDeprecationCheckResponse( + ClusterName clusterName, + List nodes, + List failures + ) { super(clusterName, nodes, failures); } diff --git a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/RestDeprecationInfoAction.java b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/RestDeprecationInfoAction.java index 6eb912185414a..5270625871897 100644 --- a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/RestDeprecationInfoAction.java +++ b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/RestDeprecationInfoAction.java @@ -24,10 +24,10 @@ public class RestDeprecationInfoAction extends BaseRestHandler { @Override public List routes() { return List.of( - Route.builder(GET, "/_migration/deprecations") - .replaces(GET, "/_xpack/migration/deprecations", RestApiVersion.V_7).build(), + Route.builder(GET, "/_migration/deprecations").replaces(GET, "/_xpack/migration/deprecations", RestApiVersion.V_7).build(), Route.builder(GET, "/{index}/_migration/deprecations") - .replaces(GET, "/{index}/_xpack/migration/deprecations", RestApiVersion.V_7).build() + .replaces(GET, "/{index}/_xpack/migration/deprecations", RestApiVersion.V_7) + .build() ); } diff --git a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/TransportDeprecationInfoAction.java b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/TransportDeprecationInfoAction.java index 0f5080af79850..3e8ef3f649f07 100644 --- a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/TransportDeprecationInfoAction.java +++ b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/TransportDeprecationInfoAction.java @@ -22,10 +22,10 @@ import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.tasks.Task; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.ClientHelper; import org.elasticsearch.xpack.core.deprecation.DeprecationIssue; @@ -37,7 +37,8 @@ import static org.elasticsearch.xpack.deprecation.DeprecationChecks.CLUSTER_SETTINGS_CHECKS; import static org.elasticsearch.xpack.deprecation.DeprecationChecks.INDEX_SETTINGS_CHECKS; -public class TransportDeprecationInfoAction extends TransportMasterNodeReadAction { private static final List PLUGIN_CHECKERS = List.of(new MlDeprecationChecker(), new TransformDeprecationChecker()); private static final Logger logger = LogManager.getLogger(TransportDeprecationInfoAction.class); @@ -49,20 +50,35 @@ public class TransportDeprecationInfoAction extends TransportMasterNodeReadActio private volatile List skipTheseDeprecations; @Inject - public TransportDeprecationInfoAction(Settings settings, TransportService transportService, ClusterService clusterService, - ThreadPool threadPool, ActionFilters actionFilters, - IndexNameExpressionResolver indexNameExpressionResolver, - NodeClient client, NamedXContentRegistry xContentRegistry) { - super(DeprecationInfoAction.NAME, transportService, clusterService, threadPool, actionFilters, DeprecationInfoAction.Request::new, - indexNameExpressionResolver, DeprecationInfoAction.Response::new, ThreadPool.Names.GENERIC); + public TransportDeprecationInfoAction( + Settings settings, + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver, + NodeClient client, + NamedXContentRegistry xContentRegistry + ) { + super( + DeprecationInfoAction.NAME, + transportService, + clusterService, + threadPool, + actionFilters, + DeprecationInfoAction.Request::new, + indexNameExpressionResolver, + DeprecationInfoAction.Response::new, + ThreadPool.Names.GENERIC + ); this.client = client; this.indexNameExpressionResolver = indexNameExpressionResolver; this.settings = settings; this.xContentRegistry = xContentRegistry; skipTheseDeprecations = DeprecationChecks.SKIP_DEPRECATIONS_SETTING.get(settings); // Safe to register this here because it happens synchronously before the cluster service is started: - clusterService.getClusterSettings().addSettingsUpdateConsumer(DeprecationChecks.SKIP_DEPRECATIONS_SETTING, - this::setSkipDeprecations); + clusterService.getClusterSettings() + .addSettingsUpdateConsumer(DeprecationChecks.SKIP_DEPRECATIONS_SETTING, this::setSkipDeprecations); } private void setSkipDeprecations(List skipDeprecations) { @@ -76,29 +92,36 @@ protected ClusterBlockException checkBlock(DeprecationInfoAction.Request request } @Override - protected final void masterOperation(Task task, final DeprecationInfoAction.Request request, ClusterState state, - final ActionListener listener) { + protected final void masterOperation( + Task task, + final DeprecationInfoAction.Request request, + ClusterState state, + final ActionListener listener + ) { NodesDeprecationCheckRequest nodeDepReq = new NodesDeprecationCheckRequest("_all"); - ClientHelper.executeAsyncWithOrigin(client, ClientHelper.DEPRECATION_ORIGIN, - NodesDeprecationCheckAction.INSTANCE, nodeDepReq, + ClientHelper.executeAsyncWithOrigin( + client, + ClientHelper.DEPRECATION_ORIGIN, + NodesDeprecationCheckAction.INSTANCE, + nodeDepReq, ActionListener.wrap(response -> { - if (response.hasFailures()) { - List failedNodeIds = response.failures().stream() - .map(failure -> failure.nodeId() + ": " + failure.getMessage()) - .collect(Collectors.toList()); - logger.warn("nodes failed to run deprecation checks: {}", failedNodeIds); - for (FailedNodeException failure : response.failures()) { - logger.debug("node {} failed to run deprecation checks: {}", failure.nodeId(), failure); + if (response.hasFailures()) { + List failedNodeIds = response.failures() + .stream() + .map(failure -> failure.nodeId() + ": " + failure.getMessage()) + .collect(Collectors.toList()); + logger.warn("nodes failed to run deprecation checks: {}", failedNodeIds); + for (FailedNodeException failure : response.failures()) { + logger.debug("node {} failed to run deprecation checks: {}", failure.nodeId(), failure); + } } - } - DeprecationChecker.Components components = new DeprecationChecker.Components( - xContentRegistry, - settings, - new OriginSettingClient(client, ClientHelper.DEPRECATION_ORIGIN) - ); - pluginSettingIssues(PLUGIN_CHECKERS, components, ActionListener.wrap( - deprecationIssues -> { + DeprecationChecker.Components components = new DeprecationChecker.Components( + xContentRegistry, + settings, + new OriginSettingClient(client, ClientHelper.DEPRECATION_ORIGIN) + ); + pluginSettingIssues(PLUGIN_CHECKERS, components, ActionListener.wrap(deprecationIssues -> { listener.onResponse( DeprecationInfoAction.Response.from( state, @@ -111,31 +134,37 @@ protected final void masterOperation(Task task, final DeprecationInfoAction.Requ skipTheseDeprecations ) ); - }, - listener::onFailure - )); + }, listener::onFailure)); - }, listener::onFailure)); + }, listener::onFailure) + ); } - static void pluginSettingIssues(List checkers, - DeprecationChecker.Components components, - ActionListener>> listener) { - List enabledCheckers = checkers - .stream() + static void pluginSettingIssues( + List checkers, + DeprecationChecker.Components components, + ActionListener>> listener + ) { + List enabledCheckers = checkers.stream() .filter(c -> c.enabled(components.settings())) .collect(Collectors.toList()); if (enabledCheckers.isEmpty()) { listener.onResponse(Collections.emptyMap()); return; } - GroupedActionListener groupedActionListener = new GroupedActionListener<>(ActionListener.wrap( - checkResults -> listener.onResponse(checkResults - .stream() - .collect(Collectors.toMap(DeprecationChecker.CheckResult::getCheckerName, DeprecationChecker.CheckResult::getIssues))), - listener::onFailure - ), enabledCheckers.size()); - for(DeprecationChecker checker : checkers) { + GroupedActionListener groupedActionListener = new GroupedActionListener<>( + ActionListener.wrap( + checkResults -> listener.onResponse( + checkResults.stream() + .collect( + Collectors.toMap(DeprecationChecker.CheckResult::getCheckerName, DeprecationChecker.CheckResult::getIssues) + ) + ), + listener::onFailure + ), + enabledCheckers.size() + ); + for (DeprecationChecker checker : checkers) { checker.check(components, groupedActionListener); } } diff --git a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/TransportNodeDeprecationCheckAction.java b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/TransportNodeDeprecationCheckAction.java index 80b05450bcb44..f1302c8ddec84 100644 --- a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/TransportNodeDeprecationCheckAction.java +++ b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/TransportNodeDeprecationCheckAction.java @@ -28,7 +28,8 @@ import java.util.List; import java.util.function.BiFunction; -public class TransportNodeDeprecationCheckAction extends TransportNodesAction { @@ -38,20 +39,31 @@ public class TransportNodeDeprecationCheckAction extends TransportNodesAction skipTheseDeprecations; @Inject - public TransportNodeDeprecationCheckAction(Settings settings, ThreadPool threadPool, - ClusterService clusterService, TransportService transportService, - PluginsService pluginsService, ActionFilters actionFilters) { - super(NodesDeprecationCheckAction.NAME, threadPool, clusterService, transportService, actionFilters, + public TransportNodeDeprecationCheckAction( + Settings settings, + ThreadPool threadPool, + ClusterService clusterService, + TransportService transportService, + PluginsService pluginsService, + ActionFilters actionFilters + ) { + super( + NodesDeprecationCheckAction.NAME, + threadPool, + clusterService, + transportService, + actionFilters, NodesDeprecationCheckRequest::new, NodesDeprecationCheckAction.NodeRequest::new, ThreadPool.Names.GENERIC, - NodesDeprecationCheckAction.NodeResponse.class); + NodesDeprecationCheckAction.NodeResponse.class + ); this.settings = settings; this.pluginsService = pluginsService; skipTheseDeprecations = DeprecationChecks.SKIP_DEPRECATIONS_SETTING.get(settings); // Safe to register this here because it happens synchronously before the cluster service is started: - clusterService.getClusterSettings().addSettingsUpdateConsumer(DeprecationChecks.SKIP_DEPRECATIONS_SETTING, - this::setSkipDeprecations); + clusterService.getClusterSettings() + .addSettingsUpdateConsumer(DeprecationChecks.SKIP_DEPRECATIONS_SETTING, this::setSkipDeprecations); } private void setSkipDeprecations(List skipDeprecations) { @@ -59,9 +71,11 @@ private void setSkipDeprecations(List skipDeprecations) { } @Override - protected NodesDeprecationCheckResponse newResponse(NodesDeprecationCheckRequest request, - List nodeResponses, - List failures) { + protected NodesDeprecationCheckResponse newResponse( + NodesDeprecationCheckRequest request, + List nodeResponses, + List failures + ) { return new NodesDeprecationCheckResponse(clusterService.getClusterName(), nodeResponses, failures); } @@ -80,12 +94,15 @@ protected NodesDeprecationCheckAction.NodeResponse nodeOperation(NodesDeprecatio return nodeOperation(request, DeprecationChecks.NODE_SETTINGS_CHECKS); } - NodesDeprecationCheckAction.NodeResponse nodeOperation(NodesDeprecationCheckAction.NodeRequest request, - List> nodeSettingsChecks) { + NodesDeprecationCheckAction.NodeResponse nodeOperation( + NodesDeprecationCheckAction.NodeRequest request, + List> nodeSettingsChecks + ) { Settings filteredSettings = settings.filter(setting -> Regex.simpleMatch(skipTheseDeprecations, setting) == false); - List issues = DeprecationInfoAction.filterChecks(nodeSettingsChecks, - (c) -> c.apply(filteredSettings, pluginsService.info())); + List issues = DeprecationInfoAction.filterChecks( + nodeSettingsChecks, + (c) -> c.apply(filteredSettings, pluginsService.info()) + ); return new NodesDeprecationCheckAction.NodeResponse(transportService.getLocalNode(), issues); } diff --git a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/logging/DeprecationCacheResetAction.java b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/logging/DeprecationCacheResetAction.java index 596a3c77fe9ca..c69e4e2903124 100644 --- a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/logging/DeprecationCacheResetAction.java +++ b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/logging/DeprecationCacheResetAction.java @@ -17,9 +17,9 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.transport.TransportRequest; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.transport.TransportRequest; import java.io.IOException; import java.util.List; @@ -113,18 +113,16 @@ public NodeRequest(StreamInput in) throws IOException { super(in); } - public NodeRequest(Request request) { - } + public NodeRequest(Request request) {} } public static class NodeResponse extends BaseNodeResponse { - protected NodeResponse(StreamInput in) throws IOException { super(in); } - protected NodeResponse(DiscoveryNode node ) { + protected NodeResponse(DiscoveryNode node) { super(node); } diff --git a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/logging/DeprecationIndexingComponent.java b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/logging/DeprecationIndexingComponent.java index f9d8e39181b1d..245c77aa41927 100644 --- a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/logging/DeprecationIndexingComponent.java +++ b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/logging/DeprecationIndexingComponent.java @@ -48,8 +48,12 @@ public class DeprecationIndexingComponent extends AbstractLifecycleComponent { private final BulkProcessor processor; private final RateLimitingFilter rateLimitingFilterForIndexing; - public DeprecationIndexingComponent(Client client, Settings settings, RateLimitingFilter rateLimitingFilterForIndexing, - boolean enableDeprecationLogIndexingDefault) { + public DeprecationIndexingComponent( + Client client, + Settings settings, + RateLimitingFilter rateLimitingFilterForIndexing, + boolean enableDeprecationLogIndexingDefault + ) { this.rateLimitingFilterForIndexing = rateLimitingFilterForIndexing; this.processor = getBulkProcessor(new OriginSettingClient(client, ClientHelper.DEPRECATION_ORIGIN), settings); @@ -63,8 +67,12 @@ public DeprecationIndexingComponent(Client client, Settings settings, RateLimiti .setConfiguration(configuration) .build(); - this.appender = new DeprecationIndexingAppender("deprecation_indexing_appender", - rateLimitingFilterForIndexing, ecsLayout, consumer); + this.appender = new DeprecationIndexingAppender( + "deprecation_indexing_appender", + rateLimitingFilterForIndexing, + ecsLayout, + consumer + ); enableDeprecationLogIndexing(enableDeprecationLogIndexingDefault); } @@ -85,7 +93,6 @@ protected void doClose() { this.processor.close(); } - public void enableDeprecationLogIndexing(boolean newEnabled) { if (appender.isEnabled() != newEnabled) { appender.setEnabled(newEnabled); @@ -141,7 +148,7 @@ public void afterBulk(long executionId, BulkRequest request, BulkResponse respon if (response.hasFailures()) { List failures = Arrays.stream(response.getItems()) .filter(BulkItemResponse::isFailed) - .map(r -> r.getId() + " " + r.getFailureMessage() ) + .map(r -> r.getId() + " " + r.getFailureMessage()) .collect(Collectors.toList()); logger.error("Bulk write of deprecation logs encountered some failures: [{}]", failures); } diff --git a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/logging/DeprecationIndexingTemplateRegistry.java b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/logging/DeprecationIndexingTemplateRegistry.java index 9740324da0a79..89d2b9cc99ca0 100644 --- a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/logging/DeprecationIndexingTemplateRegistry.java +++ b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/logging/DeprecationIndexingTemplateRegistry.java @@ -10,8 +10,8 @@ import org.elasticsearch.client.Client; import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.template.IndexTemplateConfig; import org.elasticsearch.xpack.core.template.IndexTemplateRegistry; import org.elasticsearch.xpack.core.template.LifecyclePolicyConfig; diff --git a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/logging/TransportDeprecationCacheResetAction.java b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/logging/TransportDeprecationCacheResetAction.java index 6bb68bf262008..48c4d5648eab3 100644 --- a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/logging/TransportDeprecationCacheResetAction.java +++ b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/logging/TransportDeprecationCacheResetAction.java @@ -24,8 +24,8 @@ import java.io.IOException; import java.util.List; -public class TransportDeprecationCacheResetAction - extends TransportNodesAction { @@ -35,23 +35,33 @@ public class TransportDeprecationCacheResetAction private final RateLimitingFilter rateLimitingFilterForIndexing; @Inject - public TransportDeprecationCacheResetAction(ThreadPool threadPool, - ClusterService clusterService, - TransportService transportService, - ActionFilters actionFilters, - RateLimitingFilter rateLimitingFilterForIndexing) { - super(DeprecationCacheResetAction.NAME, threadPool, clusterService, transportService, actionFilters, + public TransportDeprecationCacheResetAction( + ThreadPool threadPool, + ClusterService clusterService, + TransportService transportService, + ActionFilters actionFilters, + RateLimitingFilter rateLimitingFilterForIndexing + ) { + super( + DeprecationCacheResetAction.NAME, + threadPool, + clusterService, + transportService, + actionFilters, DeprecationCacheResetAction.Request::new, DeprecationCacheResetAction.NodeRequest::new, ThreadPool.Names.MANAGEMENT, - DeprecationCacheResetAction.NodeResponse.class); + DeprecationCacheResetAction.NodeResponse.class + ); this.rateLimitingFilterForIndexing = rateLimitingFilterForIndexing; } @Override - protected DeprecationCacheResetAction.Response newResponse(DeprecationCacheResetAction.Request request, - List nodeResponses, - List failures) { + protected DeprecationCacheResetAction.Response newResponse( + DeprecationCacheResetAction.Request request, + List nodeResponses, + List failures + ) { return new DeprecationCacheResetAction.Response(clusterService.getClusterName(), nodeResponses, failures); } @@ -68,7 +78,7 @@ protected DeprecationCacheResetAction.NodeResponse newNodeResponse(StreamInput i @Override protected DeprecationCacheResetAction.NodeResponse nodeOperation(DeprecationCacheResetAction.NodeRequest request, Task task) { rateLimitingFilterForIndexing.reset(); - logger.debug( "Deprecation cache was reset"); + logger.debug("Deprecation cache was reset"); return new DeprecationCacheResetAction.NodeResponse(transportService.getLocalNode()); } } diff --git a/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecksTests.java b/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecksTests.java index 730bf691ddc10..e4ea0216f25a2 100644 --- a/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecksTests.java +++ b/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecksTests.java @@ -18,9 +18,7 @@ public class ClusterDeprecationChecksTests extends ESTestCase { public void testCheckTransientSettingsExistence() { - Settings persistentSettings = Settings.builder() - .put("xpack.monitoring.collection.enabled", true) - .build(); + Settings persistentSettings = Settings.builder().put("xpack.monitoring.collection.enabled", true).build(); Settings transientSettings = Settings.builder() .put("indices.recovery.max_bytes_per_sec", "20mb") @@ -34,20 +32,22 @@ public void testCheckTransientSettingsExistence() { ClusterState badState = ClusterState.builder(new ClusterName("test")).metadata(metadataWithTransientSettings).build(); DeprecationIssue issue = ClusterDeprecationChecks.checkTransientSettingsExistence(badState); - assertThat(issue, equalTo( - new DeprecationIssue(DeprecationIssue.Level.WARNING, - "Transient cluster settings are deprecated", - "https://ela.st/es-deprecation-7-transient-cluster-settings", - "Use persistent settings to configure your cluster.", - false, null) - )); - - persistentSettings = Settings.builder() - .put("indices.recovery.max_bytes_per_sec", "20mb") - .build(); - Metadata metadataWithoutTransientSettings = Metadata.builder() - .persistentSettings(persistentSettings) - .build(); + assertThat( + issue, + equalTo( + new DeprecationIssue( + DeprecationIssue.Level.WARNING, + "Transient cluster settings are deprecated", + "https://ela.st/es-deprecation-7-transient-cluster-settings", + "Use persistent settings to configure your cluster.", + false, + null + ) + ) + ); + + persistentSettings = Settings.builder().put("indices.recovery.max_bytes_per_sec", "20mb").build(); + Metadata metadataWithoutTransientSettings = Metadata.builder().persistentSettings(persistentSettings).build(); ClusterState okState = ClusterState.builder(new ClusterName("test")).metadata(metadataWithoutTransientSettings).build(); issue = ClusterDeprecationChecks.checkTransientSettingsExistence(okState); diff --git a/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/DeprecationChecksTests.java b/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/DeprecationChecksTests.java index db2b270376536..9a57450b7fad7 100644 --- a/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/DeprecationChecksTests.java +++ b/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/DeprecationChecksTests.java @@ -36,8 +36,13 @@ public void testFilterChecks() { private static DeprecationIssue createRandomDeprecationIssue() { String details = randomBoolean() ? randomAlphaOfLength(10) : null; - return new DeprecationIssue(randomFrom(DeprecationIssue.Level.values()), randomAlphaOfLength(10), - randomAlphaOfLength(10), details, randomBoolean(), - randomMap(1, 5, () -> Tuple.tuple(randomAlphaOfLength(4), randomAlphaOfLength(4)))); + return new DeprecationIssue( + randomFrom(DeprecationIssue.Level.values()), + randomAlphaOfLength(10), + randomAlphaOfLength(10), + details, + randomBoolean(), + randomMap(1, 5, () -> Tuple.tuple(randomAlphaOfLength(4), randomAlphaOfLength(4))) + ); } } diff --git a/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/DeprecationInfoActionResponseTests.java b/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/DeprecationInfoActionResponseTests.java index aedcc788ff032..707e767b7cf10 100644 --- a/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/DeprecationInfoActionResponseTests.java +++ b/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/DeprecationInfoActionResponseTests.java @@ -18,11 +18,11 @@ import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.TransportAddress; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.core.Tuple; import org.elasticsearch.indices.TestIndexNameExpressionResolver; import org.elasticsearch.test.AbstractWireSerializingTestCase; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xpack.core.deprecation.DeprecationIssue; import org.elasticsearch.xpack.core.deprecation.DeprecationIssue.Level; import org.junit.Assert; @@ -50,19 +50,23 @@ public class DeprecationInfoActionResponseTests extends AbstractWireSerializingT @Override protected DeprecationInfoAction.Response createTestInstance() { List clusterIssues = Stream.generate(DeprecationInfoActionResponseTests::createTestDeprecationIssue) - .limit(randomIntBetween(0, 10)).collect(Collectors.toList()); + .limit(randomIntBetween(0, 10)) + .collect(Collectors.toList()); List nodeIssues = Stream.generate(DeprecationInfoActionResponseTests::createTestDeprecationIssue) - .limit(randomIntBetween(0, 10)).collect(Collectors.toList()); + .limit(randomIntBetween(0, 10)) + .collect(Collectors.toList()); Map> indexIssues = new HashMap<>(); for (int i = 0; i < randomIntBetween(0, 10); i++) { List perIndexIssues = Stream.generate(DeprecationInfoActionResponseTests::createTestDeprecationIssue) - .limit(randomIntBetween(0, 10)).collect(Collectors.toList()); + .limit(randomIntBetween(0, 10)) + .collect(Collectors.toList()); indexIssues.put(randomAlphaOfLength(10), perIndexIssues); } Map> pluginIssues = new HashMap<>(); for (int i = 0; i < randomIntBetween(0, 10); i++) { List perPluginIssues = Stream.generate(DeprecationInfoActionResponseTests::createTestDeprecationIssue) - .limit(randomIntBetween(0, 10)).collect(Collectors.toList()); + .limit(randomIntBetween(0, 10)) + .collect(Collectors.toList()); pluginIssues.put(randomAlphaOfLength(10), perPluginIssues); } return new DeprecationInfoAction.Response(clusterIssues, nodeIssues, indexIssues, pluginIssues); @@ -78,15 +82,21 @@ public void testFrom() throws IOException { mapping.field("enabled", false); mapping.endObject().endObject(); - Metadata metadata = Metadata.builder().put(IndexMetadata.builder("test") - .putMapping(Strings.toString(mapping)) - .settings(settings(Version.CURRENT)) - .numberOfShards(1) - .numberOfReplicas(0)) + Metadata metadata = Metadata.builder() + .put( + IndexMetadata.builder("test") + .putMapping(Strings.toString(mapping)) + .settings(settings(Version.CURRENT)) + .numberOfShards(1) + .numberOfReplicas(0) + ) .build(); - DiscoveryNode discoveryNode = DiscoveryNode.createLocal(Settings.EMPTY, - new TransportAddress(TransportAddress.META_ADDRESS, 9300), "test"); + DiscoveryNode discoveryNode = DiscoveryNode.createLocal( + Settings.EMPTY, + new TransportAddress(TransportAddress.META_ADDRESS, 9300), + "test" + ); ClusterState state = ClusterState.builder(ClusterName.DEFAULT).metadata(metadata).build(); IndexNameExpressionResolver resolver = TestIndexNameExpressionResolver.newInstance(); boolean clusterIssueFound = randomBoolean(); @@ -100,19 +110,23 @@ public void testFrom() throws IOException { new ClusterName(randomAlphaOfLength(5)), nodeIssueFound ? Collections.singletonList( - new NodesDeprecationCheckAction.NodeResponse(discoveryNode, Collections.singletonList(foundIssue))) + new NodesDeprecationCheckAction.NodeResponse(discoveryNode, Collections.singletonList(foundIssue)) + ) : emptyList(), - emptyList()); + emptyList() + ); DeprecationInfoAction.Request request = new DeprecationInfoAction.Request(Strings.EMPTY_ARRAY); - DeprecationInfoAction.Response response = DeprecationInfoAction.Response.from(state, + DeprecationInfoAction.Response response = DeprecationInfoAction.Response.from( + state, resolver, request, nodeDeprecationIssues, indexSettingsChecks, clusterSettingsChecks, Collections.emptyMap(), - Collections.emptyList()); + Collections.emptyList() + ); if (clusterIssueFound) { assertThat(response.getClusterSettingsIssues(), equalTo(Collections.singletonList(foundIssue))); @@ -122,17 +136,21 @@ public void testFrom() throws IOException { if (nodeIssueFound) { String details = foundIssue.getDetails() != null ? foundIssue.getDetails() + " " : ""; - DeprecationIssue mergedFoundIssue = new DeprecationIssue(foundIssue.getLevel(), foundIssue.getMessage(), foundIssue.getUrl(), - details + "(nodes impacted: [" + discoveryNode.getName() + "])", foundIssue.isResolveDuringRollingUpgrade(), - foundIssue.getMeta()); + DeprecationIssue mergedFoundIssue = new DeprecationIssue( + foundIssue.getLevel(), + foundIssue.getMessage(), + foundIssue.getUrl(), + details + "(nodes impacted: [" + discoveryNode.getName() + "])", + foundIssue.isResolveDuringRollingUpgrade(), + foundIssue.getMeta() + ); assertThat(response.getNodeSettingsIssues(), equalTo(Collections.singletonList(mergedFoundIssue))); } else { assertTrue(response.getNodeSettingsIssues().isEmpty()); } if (indexIssueFound) { - assertThat(response.getIndexSettingsIssues(), equalTo(Collections.singletonMap("test", - Collections.singletonList(foundIssue)))); + assertThat(response.getIndexSettingsIssues(), equalTo(Collections.singletonMap("test", Collections.singletonList(foundIssue)))); } else { assertTrue(response.getIndexSettingsIssues().isEmpty()); } @@ -146,46 +164,41 @@ public void testRemoveSkippedSettings() throws IOException { settingsBuilder.put("some.undeprecated.property", "someValue3"); settingsBuilder.putList("some.undeprecated.list.property", List.of("someValue4", "someValue5")); Settings inputSettings = settingsBuilder.build(); - Metadata metadata = Metadata.builder().put(IndexMetadata.builder("test") - .settings(inputSettings) - .numberOfShards(1) - .numberOfReplicas(0)) + Metadata metadata = Metadata.builder() + .put(IndexMetadata.builder("test").settings(inputSettings).numberOfShards(1).numberOfReplicas(0)) .persistentSettings(inputSettings) .build(); ClusterState state = ClusterState.builder(ClusterName.DEFAULT).metadata(metadata).build(); IndexNameExpressionResolver resolver = TestIndexNameExpressionResolver.newInstance(); AtomicReference visibleClusterSettings = new AtomicReference<>(); - List> clusterSettingsChecks = - Collections.unmodifiableList(Arrays.asList( - (s) -> { - visibleClusterSettings.set(s.getMetadata().settings()); - return null; - } - )); + List> clusterSettingsChecks = Collections.unmodifiableList(Arrays.asList((s) -> { + visibleClusterSettings.set(s.getMetadata().settings()); + return null; + })); AtomicReference visibleIndexSettings = new AtomicReference<>(); - List> indexSettingsChecks = - Collections.unmodifiableList(Arrays.asList( - (idx) -> { - visibleIndexSettings.set(idx.getSettings()); - return null; - } - )); + List> indexSettingsChecks = Collections.unmodifiableList(Arrays.asList((idx) -> { + visibleIndexSettings.set(idx.getSettings()); + return null; + })); NodesDeprecationCheckResponse nodeDeprecationIssues = new NodesDeprecationCheckResponse( new ClusterName(randomAlphaOfLength(5)), emptyList(), - emptyList()); + emptyList() + ); DeprecationInfoAction.Request request = new DeprecationInfoAction.Request(Strings.EMPTY_ARRAY); - DeprecationInfoAction.Response.from(state, + DeprecationInfoAction.Response.from( + state, resolver, request, nodeDeprecationIssues, indexSettingsChecks, clusterSettingsChecks, Collections.emptyMap(), - List.of("some.deprecated.property", "some.other.*.deprecated.property")); + List.of("some.deprecated.property", "some.other.*.deprecated.property") + ); settingsBuilder = settings(Version.CURRENT); settingsBuilder.put("some.undeprecated.property", "someValue3"); @@ -197,8 +210,7 @@ public void testRemoveSkippedSettings() throws IOException { Settings resultIndexSettings = visibleIndexSettings.get(); Assert.assertNotNull(resultIndexSettings); Assert.assertTrue(resultIndexSettings.get("some.undeprecated.property").equals("someValue3")); - Assert.assertTrue(resultIndexSettings.getAsList("some.undeprecated.list.property") - .equals(List.of("someValue4", "someValue5"))); + Assert.assertTrue(resultIndexSettings.getAsList("some.undeprecated.list.property").equals(List.of("someValue4", "someValue5"))); Assert.assertFalse(resultIndexSettings.hasValue("some.deprecated.property")); Assert.assertFalse(resultIndexSettings.hasValue("some.other.bad.deprecated.property")); } @@ -208,9 +220,8 @@ public void testCtorFailure() { .limit(10) .collect(Collectors.toMap(Function.identity(), (_k) -> Collections.emptyList())); Set shouldCauseFailure = new HashSet<>(RESERVED_NAMES); - for(int i = 0; i < NUMBER_OF_TEST_RUNS; i++) { - Map> pluginSettingsIssues = randomSubsetOf(3, shouldCauseFailure) - .stream() + for (int i = 0; i < NUMBER_OF_TEST_RUNS; i++) { + Map> pluginSettingsIssues = randomSubsetOf(3, shouldCauseFailure).stream() .collect(Collectors.toMap(Function.identity(), (_k) -> Collections.emptyList())); expectThrows( ElasticsearchStatusException.class, diff --git a/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/IndexDeprecationChecksTests.java b/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/IndexDeprecationChecksTests.java index ff679ba82b712..ecb8ec1559465 100644 --- a/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/IndexDeprecationChecksTests.java +++ b/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/IndexDeprecationChecksTests.java @@ -31,11 +31,14 @@ public void testOldIndicesCheck() { .numberOfShards(1) .numberOfReplicas(0) .build(); - DeprecationIssue expected = new DeprecationIssue(DeprecationIssue.Level.CRITICAL, + DeprecationIssue expected = new DeprecationIssue( + DeprecationIssue.Level.CRITICAL, "Index created before 7.0", - "https://www.elastic.co/guide/en/elasticsearch/reference/master/" + - "breaking-changes-8.0.html", - "This index was created using version: " + createdWith, false, null); + "https://www.elastic.co/guide/en/elasticsearch/reference/master/" + "breaking-changes-8.0.html", + "This index was created using version: " + createdWith, + false, + null + ); List issues = DeprecationChecks.filterChecks(INDEX_SETTINGS_CHECKS, c -> c.apply(indexMetadata)); assertEquals(singletonList(expected), issues); } @@ -46,14 +49,20 @@ public void testTranslogRetentionSettings() { settings.put(IndexSettings.INDEX_TRANSLOG_RETENTION_SIZE_SETTING.getKey(), between(1, 1024) + "b"); IndexMetadata indexMetadata = IndexMetadata.builder("test").settings(settings).numberOfShards(1).numberOfReplicas(0).build(); List issues = DeprecationChecks.filterChecks(INDEX_SETTINGS_CHECKS, c -> c.apply(indexMetadata)); - assertThat(issues, contains( - new DeprecationIssue(DeprecationIssue.Level.WARNING, - "translog retention settings are ignored", - "https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules-translog.html", - "translog retention settings [index.translog.retention.size] and [index.translog.retention.age] are ignored " + - "because translog is no longer used in peer recoveries with soft-deletes enabled (default in 7.0 or later)", - false, null) - )); + assertThat( + issues, + contains( + new DeprecationIssue( + DeprecationIssue.Level.WARNING, + "translog retention settings are ignored", + "https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules-translog.html", + "translog retention settings [index.translog.retention.size] and [index.translog.retention.age] are ignored " + + "because translog is no longer used in peer recoveries with soft-deletes enabled (default in 7.0 or later)", + false, + null + ) + ) + ); } public void testDefaultTranslogRetentionSettings() { @@ -75,12 +84,19 @@ public void testIndexDataPathSetting() { List issues = DeprecationChecks.filterChecks(INDEX_SETTINGS_CHECKS, c -> c.apply(indexMetadata)); final String expectedUrl = "https://www.elastic.co/guide/en/elasticsearch/reference/7.13/breaking-changes-7.13.html#deprecate-shared-data-path-setting"; - assertThat(issues, contains( - new DeprecationIssue(DeprecationIssue.Level.CRITICAL, - "setting [index.data_path] is deprecated and will be removed in a future version", - expectedUrl, - "Found index data path configured. Discontinue use of this setting.", - false, null))); + assertThat( + issues, + contains( + new DeprecationIssue( + DeprecationIssue.Level.CRITICAL, + "setting [index.data_path] is deprecated and will be removed in a future version", + expectedUrl, + "Found index data path configured. Discontinue use of this setting.", + false, + null + ) + ) + ); } public void testSimpleFSSetting() { @@ -88,14 +104,21 @@ public void testSimpleFSSetting() { settings.put(IndexModule.INDEX_STORE_TYPE_SETTING.getKey(), "simplefs"); IndexMetadata indexMetadata = IndexMetadata.builder("test").settings(settings).numberOfShards(1).numberOfReplicas(0).build(); List issues = DeprecationChecks.filterChecks(INDEX_SETTINGS_CHECKS, c -> c.apply(indexMetadata)); - assertThat(issues, contains( - new DeprecationIssue(DeprecationIssue.Level.WARNING, - "[simplefs] is deprecated and will be removed in future versions", - "https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules-store.html", - "[simplefs] is deprecated and will be removed in 8.0. Use [niofs] or other file systems instead. " + - "Elasticsearch 7.15 or later uses [niofs] for the [simplefs] store type " + - "as it offers superior or equivalent performance to [simplefs].", false, null) - )); + assertThat( + issues, + contains( + new DeprecationIssue( + DeprecationIssue.Level.WARNING, + "[simplefs] is deprecated and will be removed in future versions", + "https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules-store.html", + "[simplefs] is deprecated and will be removed in 8.0. Use [niofs] or other file systems instead. " + + "Elasticsearch 7.15 or later uses [niofs] for the [simplefs] store type " + + "as it offers superior or equivalent performance to [simplefs].", + false, + null + ) + ) + ); } public void testFrozenIndex() { @@ -103,14 +126,18 @@ public void testFrozenIndex() { settings.put(FrozenEngine.INDEX_FROZEN.getKey(), true); IndexMetadata indexMetadata = IndexMetadata.builder("test").settings(settings).numberOfShards(1).numberOfReplicas(0).build(); List issues = DeprecationChecks.filterChecks(INDEX_SETTINGS_CHECKS, c -> c.apply(indexMetadata)); - assertThat(issues, contains( - new DeprecationIssue(DeprecationIssue.Level.WARNING, - "index [test] is a frozen index. The frozen indices feature is deprecated and will be removed in a future version", - "https://www.elastic.co/guide/en/elasticsearch/reference/master/frozen-indices.html", - "Frozen indices no longer offer any advantages. Consider cold or frozen tiers in place of frozen indices.", - false, - null + assertThat( + issues, + contains( + new DeprecationIssue( + DeprecationIssue.Level.WARNING, + "index [test] is a frozen index. The frozen indices feature is deprecated and will be removed in a future version", + "https://www.elastic.co/guide/en/elasticsearch/reference/master/frozen-indices.html", + "Frozen indices no longer offer any advantages. Consider cold or frozen tiers in place of frozen indices.", + false, + null + ) ) - )); + ); } } diff --git a/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/MlDeprecationCheckerTests.java b/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/MlDeprecationCheckerTests.java index d4059a5bebd8e..84d39d9a02070 100644 --- a/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/MlDeprecationCheckerTests.java +++ b/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/MlDeprecationCheckerTests.java @@ -8,10 +8,10 @@ package org.elasticsearch.xpack.deprecation; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchModule; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfig; @@ -35,9 +35,12 @@ protected boolean enableWarningsCheck() { public void testEnabled() { MlDeprecationChecker mlDeprecationChecker = new MlDeprecationChecker(); assertThat(mlDeprecationChecker.enabled(Settings.EMPTY), is(true)); - assertThat(mlDeprecationChecker.enabled(Settings.builder() - .put(XPackSettings.MACHINE_LEARNING_ENABLED.getKey(), Boolean.toString(false)) - .build()), is(false)); + assertThat( + mlDeprecationChecker.enabled( + Settings.builder().put(XPackSettings.MACHINE_LEARNING_ENABLED.getKey(), Boolean.toString(false)).build() + ), + is(false) + ); } public void testCheckDataFeedQuery() { diff --git a/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/NodeDeprecationChecksTests.java b/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/NodeDeprecationChecksTests.java index 157742e0d13a3..d66cafc8aec8e 100644 --- a/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/NodeDeprecationChecksTests.java +++ b/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/NodeDeprecationChecksTests.java @@ -35,41 +35,54 @@ public class NodeDeprecationChecksTests extends ESTestCase { public void testRemovedSettingNotSet() { final Settings settings = Settings.EMPTY; final Setting removedSetting = Setting.simpleString("node.removed_setting"); - final DeprecationIssue issue = - NodeDeprecationChecks.checkRemovedSetting(settings, removedSetting, "http://removed-setting.example.com"); + final DeprecationIssue issue = NodeDeprecationChecks.checkRemovedSetting( + settings, + removedSetting, + "http://removed-setting.example.com" + ); assertThat(issue, nullValue()); } public void testRemovedSetting() { final Settings settings = Settings.builder().put("node.removed_setting", "value").build(); final Setting removedSetting = Setting.simpleString("node.removed_setting"); - final DeprecationIssue issue = - NodeDeprecationChecks.checkRemovedSetting(settings, removedSetting, "https://removed-setting.example.com"); + final DeprecationIssue issue = NodeDeprecationChecks.checkRemovedSetting( + settings, + removedSetting, + "https://removed-setting.example.com" + ); assertThat(issue, not(nullValue())); assertThat(issue.getLevel(), equalTo(DeprecationIssue.Level.CRITICAL)); assertThat( issue.getMessage(), - equalTo("setting [node.removed_setting] is deprecated and will be removed in the next major version")); - assertThat( - issue.getDetails(), - equalTo("the setting [node.removed_setting] is currently set to [value], remove this setting")); + equalTo("setting [node.removed_setting] is deprecated and will be removed in the next major version") + ); + assertThat(issue.getDetails(), equalTo("the setting [node.removed_setting] is currently set to [value], remove this setting")); assertThat(issue.getUrl(), equalTo("https://removed-setting.example.com")); } public void testSharedDataPathSetting() { Settings settings = Settings.builder() .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()) - .put(Environment.PATH_SHARED_DATA_SETTING.getKey(), createTempDir()).build(); + .put(Environment.PATH_SHARED_DATA_SETTING.getKey(), createTempDir()) + .build(); List issues = DeprecationChecks.filterChecks(NODE_SETTINGS_CHECKS, c -> c.apply(settings, null)); final String expectedUrl = "https://www.elastic.co/guide/en/elasticsearch/reference/7.13/breaking-changes-7.13.html#deprecate-shared-data-path-setting"; - assertThat(issues, contains( - new DeprecationIssue(DeprecationIssue.Level.CRITICAL, - "setting [path.shared_data] is deprecated and will be removed in a future version", - expectedUrl, - "Found shared data path configured. Discontinue use of this setting.", - false, null))); + assertThat( + issues, + contains( + new DeprecationIssue( + DeprecationIssue.Level.CRITICAL, + "setting [path.shared_data] is deprecated and will be removed in a future version", + expectedUrl, + "Found shared data path configured. Discontinue use of this setting.", + false, + null + ) + ) + ); } public void testCheckReservedPrefixedRealmNames() { @@ -114,206 +127,268 @@ public void testCheckReservedPrefixedRealmNames() { assertEquals("Realm that start with [_] will not be permitted in a future major release.", deprecationIssue.getMessage()); assertEquals( "https://www.elastic.co/guide/en/elasticsearch/reference" + "/7.14/deprecated-7.14.html#reserved-prefixed-realm-names", - deprecationIssue.getUrl()); + deprecationIssue.getUrl() + ); assertEquals( - "Found realm " + (invalidRealmNames.size() == 1 ? "name" : "names") + "Found realm " + + (invalidRealmNames.size() == 1 ? "name" : "names") + " with reserved prefix [_]: [" + Strings.collectionToDelimitedString(invalidRealmNames.stream().sorted().collect(Collectors.toList()), "; ") - + "]. " + "In a future major release, node will fail to start if any realm names start with reserved prefix.", - deprecationIssue.getDetails()); + + "]. " + + "In a future major release, node will fail to start if any realm names start with reserved prefix.", + deprecationIssue.getDetails() + ); } public void testSingleDataNodeWatermarkSetting() { - Settings settings = Settings.builder() - .put(DiskThresholdDecider.ENABLE_FOR_SINGLE_DATA_NODE.getKey(), true) - .build(); + Settings settings = Settings.builder().put(DiskThresholdDecider.ENABLE_FOR_SINGLE_DATA_NODE.getKey(), true).build(); List issues = DeprecationChecks.filterChecks(NODE_SETTINGS_CHECKS, c -> c.apply(settings, null)); - final String expectedUrl = - "https://www.elastic.co/guide/en/elasticsearch/reference/7.14/" + - "breaking-changes-7.14.html#deprecate-single-data-node-watermark"; - assertThat(issues, hasItem( - new DeprecationIssue(DeprecationIssue.Level.CRITICAL, - "setting [cluster.routing.allocation.disk.watermark.enable_for_single_data_node] is deprecated and" + - " will not be available in a future version", - expectedUrl, - "found [cluster.routing.allocation.disk.watermark.enable_for_single_data_node] configured." + - " Discontinue use of this setting.", - false, null))); + final String expectedUrl = "https://www.elastic.co/guide/en/elasticsearch/reference/7.14/" + + "breaking-changes-7.14.html#deprecate-single-data-node-watermark"; + assertThat( + issues, + hasItem( + new DeprecationIssue( + DeprecationIssue.Level.CRITICAL, + "setting [cluster.routing.allocation.disk.watermark.enable_for_single_data_node] is deprecated and" + + " will not be available in a future version", + expectedUrl, + "found [cluster.routing.allocation.disk.watermark.enable_for_single_data_node] configured." + + " Discontinue use of this setting.", + false, + null + ) + ) + ); } void monitoringSetting(String settingKey, String value) { - Settings settings = Settings.builder() - .put(settingKey, value) - .build(); + Settings settings = Settings.builder().put(settingKey, value).build(); List issues = DeprecationChecks.filterChecks(NODE_SETTINGS_CHECKS, c -> c.apply(settings, null)); final String expectedUrl = "https://ela.st/es-deprecation-7-monitoring-settings"; - assertThat(issues, hasItem( - new DeprecationIssue(DeprecationIssue.Level.WARNING, - "setting ["+settingKey+"] is deprecated and will be removed after 8.0", - expectedUrl, - "the setting ["+settingKey+"] is currently set to ["+value+"], remove this setting", - false, null))); + assertThat( + issues, + hasItem( + new DeprecationIssue( + DeprecationIssue.Level.WARNING, + "setting [" + settingKey + "] is deprecated and will be removed after 8.0", + expectedUrl, + "the setting [" + settingKey + "] is currently set to [" + value + "], remove this setting", + false, + null + ) + ) + ); } void monitoringExporterSetting(String suffix, String value) { String settingKey = "xpack.monitoring.exporters.test." + suffix; - Settings settings = Settings.builder() - .put(settingKey, value) - .build(); + Settings settings = Settings.builder().put(settingKey, value).build(); List issues = DeprecationChecks.filterChecks(NODE_SETTINGS_CHECKS, c -> c.apply(settings, null)); final String expectedUrl = "https://ela.st/es-deprecation-7-monitoring-settings"; - assertThat(issues, hasItem( - new DeprecationIssue(DeprecationIssue.Level.WARNING, - "The ["+settingKey+"] settings are deprecated and will be removed after 8.0", - expectedUrl, - "Remove the following settings from elasticsearch.yml: ["+settingKey+"]", - false, null))); + assertThat( + issues, + hasItem( + new DeprecationIssue( + DeprecationIssue.Level.WARNING, + "The [" + settingKey + "] settings are deprecated and will be removed after 8.0", + expectedUrl, + "Remove the following settings from elasticsearch.yml: [" + settingKey + "]", + false, + null + ) + ) + ); } void monitoringExporterGroupedSetting(String suffix, String value) { String settingKey = "xpack.monitoring.exporters.test." + suffix; String subSettingKey = settingKey + ".subsetting"; - Settings settings = Settings.builder() - .put(subSettingKey, value) - .build(); + Settings settings = Settings.builder().put(subSettingKey, value).build(); List issues = DeprecationChecks.filterChecks(NODE_SETTINGS_CHECKS, c -> c.apply(settings, null)); final String expectedUrl = "https://ela.st/es-deprecation-7-monitoring-settings"; - assertThat(issues, hasItem( - new DeprecationIssue(DeprecationIssue.Level.WARNING, - "The ["+settingKey+".*] settings are deprecated and will be removed after 8.0", - expectedUrl, - "Remove the following settings from elasticsearch.yml: ["+subSettingKey+"]", - false, null))); + assertThat( + issues, + hasItem( + new DeprecationIssue( + DeprecationIssue.Level.WARNING, + "The [" + settingKey + ".*] settings are deprecated and will be removed after 8.0", + expectedUrl, + "Remove the following settings from elasticsearch.yml: [" + subSettingKey + "]", + false, + null + ) + ) + ); } void monitoringExporterSecureSetting(String suffix, String value) { String settingKey = "xpack.monitoring.exporters.test." + suffix; MockSecureSettings secureSettings = new MockSecureSettings(); secureSettings.setString(settingKey, value); - Settings settings = Settings.builder() - .setSecureSettings(secureSettings) - .build(); + Settings settings = Settings.builder().setSecureSettings(secureSettings).build(); List issues = DeprecationChecks.filterChecks(NODE_SETTINGS_CHECKS, c -> c.apply(settings, null)); final String expectedUrl = "https://ela.st/es-deprecation-7-monitoring-settings"; - assertThat(issues, hasItem( - new DeprecationIssue(DeprecationIssue.Level.WARNING, - "The ["+settingKey+"] settings are deprecated and will be removed after 8.0", - expectedUrl, - "Remove the following settings from the keystore: ["+settingKey+"]", - false, null))); + assertThat( + issues, + hasItem( + new DeprecationIssue( + DeprecationIssue.Level.WARNING, + "The [" + settingKey + "] settings are deprecated and will be removed after 8.0", + expectedUrl, + "Remove the following settings from the keystore: [" + settingKey + "]", + false, + null + ) + ) + ); } public void testCheckMonitoringSettingHistoryDuration() { monitoringSetting("xpack.monitoring.history.duration", "7d"); } + public void testCheckMonitoringSettingCollectIndexRecovery() { monitoringSetting("xpack.monitoring.collection.index.recovery.active_only", "true"); } + public void testCheckMonitoringSettingCollectIndices() { monitoringSetting("xpack.monitoring.collection.indices", "[test1,test2]"); } + public void testCheckMonitoringSettingCollectCcrTimeout() { monitoringSetting("xpack.monitoring.collection.ccr.stats.timeout", "10s"); } + public void testCheckMonitoringSettingCollectEnrichStatsTimeout() { monitoringSetting("xpack.monitoring.collection.enrich.stats.timeout", "10s"); } + public void testCheckMonitoringSettingCollectIndexRecoveryStatsTimeout() { monitoringSetting("xpack.monitoring.collection.index.recovery.timeout", "10s"); } + public void testCheckMonitoringSettingCollectIndexStatsTimeout() { monitoringSetting("xpack.monitoring.collection.index.stats.timeout", "10s"); } + public void testCheckMonitoringSettingCollectMlJobStatsTimeout() { monitoringSetting("xpack.monitoring.collection.ml.job.stats.timeout", "10s"); } + public void testCheckMonitoringSettingCollectNodeStatsTimeout() { monitoringSetting("xpack.monitoring.collection.node.stats.timeout", "10s"); } + public void testCheckMonitoringSettingCollectClusterStatsTimeout() { monitoringSetting("xpack.monitoring.collection.cluster.stats.timeout", "10s"); } + public void testCheckMonitoringSettingExportersHost() { monitoringExporterSetting("host", "abcdef"); } + public void testCheckMonitoringSettingExportersBulkTimeout() { monitoringExporterSetting("bulk.timeout", "10s"); } + public void testCheckMonitoringSettingExportersConnectionTimeout() { monitoringExporterSetting("connection.timeout", "10s"); } + public void testCheckMonitoringSettingExportersConnectionReadTimeout() { monitoringExporterSetting("connection.read_timeout", "10s"); } + public void testCheckMonitoringSettingExportersAuthUsername() { monitoringExporterSetting("auth.username", "abcdef"); } + public void testCheckMonitoringSettingExportersAuthPass() { monitoringExporterSecureSetting("auth.secure_password", "abcdef"); } + public void testCheckMonitoringSettingExportersSSL() { monitoringExporterGroupedSetting("ssl", "abcdef"); } + public void testCheckMonitoringSettingExportersProxyBase() { monitoringExporterSetting("proxy.base_path", "abcdef"); } + public void testCheckMonitoringSettingExportersSniffEnabled() { monitoringExporterSetting("sniff.enabled", "true"); } + public void testCheckMonitoringSettingExportersHeaders() { monitoringExporterGroupedSetting("headers", "abcdef"); } + public void testCheckMonitoringSettingExportersTemplateTimeout() { monitoringExporterSetting("index.template.master_timeout", "10s"); } + public void testCheckMonitoringSettingExportersMasterTimeout() { monitoringExporterSetting("wait_master.timeout", "10s"); } + public void testCheckMonitoringSettingExportersEnabled() { monitoringExporterSetting("enabled", "true"); } + public void testCheckMonitoringSettingExportersType() { monitoringExporterSetting("type", "local"); } + public void testCheckMonitoringSettingExportersAlertsEnabled() { monitoringExporterSetting("cluster_alerts.management.enabled", "true"); } + public void testCheckMonitoringSettingExportersAlertsBlacklist() { monitoringExporterSetting("cluster_alerts.management.blacklist", "[abcdef,ghijkl]"); } + public void testCheckMonitoringSettingExportersIndexNameTimeFormat() { monitoringExporterSetting("index.name.time_format", "yyyy-mm-dd"); } + public void testCheckMonitoringSettingDecomissionAlerts() { monitoringSetting("xpack.monitoring.migration.decommission_alerts", "true"); } + public void testCheckMonitoringSettingEsCollectionEnabled() { monitoringSetting("xpack.monitoring.elasticsearch.collection.enabled", "true"); } + public void testCheckMonitoringSettingCollectionEnabled() { monitoringSetting("xpack.monitoring.collection.enabled", "true"); } + public void testCheckMonitoringSettingCollectionInterval() { monitoringSetting("xpack.monitoring.collection.interval", "10s"); } public void testExporterUseIngestPipelineSettings() { - Settings settings = Settings.builder() - .put("xpack.monitoring.exporters.test.use_ingest", true) - .build(); + Settings settings = Settings.builder().put("xpack.monitoring.exporters.test.use_ingest", true).build(); List issues = DeprecationChecks.filterChecks(NODE_SETTINGS_CHECKS, c -> c.apply(settings, null)); - final String expectedUrl = - "https://ela.st/es-deprecation-7-monitoring-exporter-use-ingest-setting"; - assertThat(issues, hasItem( - new DeprecationIssue(DeprecationIssue.Level.WARNING, - "The [xpack.monitoring.exporters.test.use_ingest] settings are deprecated and will be removed after 8.0", - expectedUrl, - "Remove the following settings from elasticsearch.yml: [xpack.monitoring.exporters.test.use_ingest]", - false, null))); + final String expectedUrl = "https://ela.st/es-deprecation-7-monitoring-exporter-use-ingest-setting"; + assertThat( + issues, + hasItem( + new DeprecationIssue( + DeprecationIssue.Level.WARNING, + "The [xpack.monitoring.exporters.test.use_ingest] settings are deprecated and will be removed after 8.0", + expectedUrl, + "Remove the following settings from elasticsearch.yml: [xpack.monitoring.exporters.test.use_ingest]", + false, + null + ) + ) + ); } public void testExporterPipelineMasterTimeoutSetting() { @@ -323,36 +398,45 @@ public void testExporterPipelineMasterTimeoutSetting() { List issues = DeprecationChecks.filterChecks(NODE_SETTINGS_CHECKS, c -> c.apply(settings, null)); - final String expectedUrl = - "https://ela.st/es-deprecation-7-monitoring-exporter-pipeline-timeout-setting"; - assertThat(issues, hasItem( - new DeprecationIssue(DeprecationIssue.Level.WARNING, - "The [xpack.monitoring.exporters.test.index.pipeline.master_timeout] settings are deprecated and will be removed after 8.0", - expectedUrl, - "Remove the following settings from elasticsearch.yml: [xpack.monitoring.exporters.test.index.pipeline.master_timeout]", - false, null))); + final String expectedUrl = "https://ela.st/es-deprecation-7-monitoring-exporter-pipeline-timeout-setting"; + assertThat( + issues, + hasItem( + new DeprecationIssue( + DeprecationIssue.Level.WARNING, + "The [xpack.monitoring.exporters.test.index.pipeline.master_timeout] settings are deprecated and will be removed after 8.0", + expectedUrl, + "Remove the following settings from elasticsearch.yml: [xpack.monitoring.exporters.test.index.pipeline.master_timeout]", + false, + null + ) + ) + ); } public void testExporterCreateLegacyTemplateSetting() { - Settings settings = Settings.builder() - .put("xpack.monitoring.exporters.test.index.template.create_legacy_templates", true) - .build(); + Settings settings = Settings.builder().put("xpack.monitoring.exporters.test.index.template.create_legacy_templates", true).build(); List issues = DeprecationChecks.filterChecks(NODE_SETTINGS_CHECKS, c -> c.apply(settings, null)); - final String expectedUrl = - "https://ela.st/es-deprecation-7-monitoring-exporter-create-legacy-template-setting"; - assertThat(issues, hasItem( - new DeprecationIssue(DeprecationIssue.Level.WARNING, - "The [xpack.monitoring.exporters.test.index.template.create_legacy_templates] settings are deprecated and will be " + - "removed after 8.0", - expectedUrl, - "Remove the following settings from elasticsearch.yml: " + - "[xpack.monitoring.exporters.test.index.template.create_legacy_templates]", - false, null))); + final String expectedUrl = "https://ela.st/es-deprecation-7-monitoring-exporter-create-legacy-template-setting"; + assertThat( + issues, + hasItem( + new DeprecationIssue( + DeprecationIssue.Level.WARNING, + "The [xpack.monitoring.exporters.test.index.template.create_legacy_templates] settings are deprecated and will be " + + "removed after 8.0", + expectedUrl, + "Remove the following settings from elasticsearch.yml: " + + "[xpack.monitoring.exporters.test.index.template.create_legacy_templates]", + false, + null + ) + ) + ); } - public void testScriptContextCacheSetting() { Settings settings = Settings.builder() .put(ScriptService.SCRIPT_GENERAL_MAX_COMPILATIONS_RATE_SETTING.getKey(), "use-context") @@ -363,12 +447,16 @@ public void testScriptContextCacheSetting() { assertThat( issues, hasItem( - new DeprecationIssue(DeprecationIssue.Level.WARNING, + new DeprecationIssue( + DeprecationIssue.Level.WARNING, ScriptService.USE_CONTEXT_RATE_KEY_DEPRECATION_MESSAGE, "https://ela.st/es-deprecation-7-script-context-cache", - "found deprecated script context caches in use, change setting to compilation rate or remove " + - "setting to use the default", - false, null)) + "found deprecated script context caches in use, change setting to compilation rate or remove " + + "setting to use the default", + false, + null + ) + ) ); } @@ -385,7 +473,8 @@ public void testScriptContextCompilationsRateLimitSetting() { assertThat( issues, hasItem( - new DeprecationIssue(DeprecationIssue.Level.WARNING, + new DeprecationIssue( + DeprecationIssue.Level.WARNING, "Setting context-specific rate limits" + " [script.context.field.max_compilations_rate,script.context.score.max_compilations_rate] is deprecated." + " Use [script.max_compilations_rate] to rate limit the compilation of user scripts." @@ -393,13 +482,18 @@ public void testScriptContextCompilationsRateLimitSetting() { "https://ela.st/es-deprecation-7-script-context-cache", "[script.context.field.max_compilations_rate,script.context.score.max_compilations_rate] is deprecated and" + " will be removed in a future release", - false, null))); + false, + null + ) + ) + ); assertWarnings( "[script.context.field.max_compilations_rate] setting was deprecated in Elasticsearch and will be" + " removed in a future release! See the breaking changes documentation for the next major version.", "[script.context.score.max_compilations_rate] setting was deprecated in Elasticsearch and will be removed in a future" - + " release! See the breaking changes documentation for the next major version."); + + " release! See the breaking changes documentation for the next major version." + ); } public void testScriptContextCacheSizeSetting() { @@ -415,20 +509,27 @@ public void testScriptContextCacheSizeSetting() { assertThat( issues, hasItem( - new DeprecationIssue(DeprecationIssue.Level.WARNING, + new DeprecationIssue( + DeprecationIssue.Level.WARNING, "Setting a context-specific cache size" + " [script.context.filter.cache_max_size,script.context.update.cache_max_size] is deprecated." + " Use [script.cache.max_size] to configure the size of the general cache for scripts." + " Context-specific caches are no longer needed to prevent system scripts from triggering rate limits.", "https://ela.st/es-deprecation-7-script-context-cache", - "[script.context.filter.cache_max_size,script.context.update.cache_max_size] is deprecated and will be" + - " removed in a future release", - false, null))); + "[script.context.filter.cache_max_size,script.context.update.cache_max_size] is deprecated and will be" + + " removed in a future release", + false, + null + ) + ) + ); - assertWarnings("[script.context.update.cache_max_size] setting was deprecated in Elasticsearch and will be" + assertWarnings( + "[script.context.update.cache_max_size] setting was deprecated in Elasticsearch and will be" + " removed in a future release! See the breaking changes documentation for the next major version.", - "[script.context.filter.cache_max_size] setting was deprecated in Elasticsearch and will be removed in a future" + - " release! See the breaking changes documentation for the next major version."); + "[script.context.filter.cache_max_size] setting was deprecated in Elasticsearch and will be removed in a future" + + " release! See the breaking changes documentation for the next major version." + ); } public void testScriptContextCacheExpirationSetting() { @@ -444,7 +545,8 @@ public void testScriptContextCacheExpirationSetting() { assertThat( issues, hasItem( - new DeprecationIssue(DeprecationIssue.Level.WARNING, + new DeprecationIssue( + DeprecationIssue.Level.WARNING, "Setting a context-specific cache expiration" + " [script.context.interval.cache_expire,script.context.moving-function.cache_expire] is deprecated." + " Use [script.cache.expire] to configure the expiration of the general cache." @@ -452,31 +554,40 @@ public void testScriptContextCacheExpirationSetting() { "https://ela.st/es-deprecation-7-script-context-cache", "[script.context.interval.cache_expire,script.context.moving-function.cache_expire] is deprecated and will be" + " removed in a future release", - false, null))); - + false, + null + ) + ) + ); - assertWarnings("[script.context.interval.cache_expire] setting was deprecated in Elasticsearch and will be" + assertWarnings( + "[script.context.interval.cache_expire] setting was deprecated in Elasticsearch and will be" + " removed in a future release! See the breaking changes documentation for the next major version.", - "[script.context.moving-function.cache_expire] setting was deprecated in Elasticsearch and will be removed in a future" + - " release! See the breaking changes documentation for the next major version."); + "[script.context.moving-function.cache_expire] setting was deprecated in Elasticsearch and will be removed in a future" + + " release! See the breaking changes documentation for the next major version." + ); } public void testEnforceDefaultTierPreferenceSetting() { - Settings settings = Settings.builder() - .put(DataTier.ENFORCE_DEFAULT_TIER_PREFERENCE_SETTING.getKey(), randomBoolean()) - .build(); + Settings settings = Settings.builder().put(DataTier.ENFORCE_DEFAULT_TIER_PREFERENCE_SETTING.getKey(), randomBoolean()).build(); List issues = DeprecationChecks.filterChecks(NODE_SETTINGS_CHECKS, c -> c.apply(settings, null)); final String expectedUrl = "https://www.elastic.co/guide/en/elasticsearch/reference/current/data-tiers.html"; - assertThat(issues, hasItem( - new DeprecationIssue(DeprecationIssue.Level.CRITICAL, - "setting [cluster.routing.allocation.enforce_default_tier_preference] is deprecated and" + - " will not be available in a future version", - expectedUrl, - "found [cluster.routing.allocation.enforce_default_tier_preference] configured." + - " Discontinue use of this setting.", - false, null))); + assertThat( + issues, + hasItem( + new DeprecationIssue( + DeprecationIssue.Level.CRITICAL, + "setting [cluster.routing.allocation.enforce_default_tier_preference] is deprecated and" + + " will not be available in a future version", + expectedUrl, + "found [cluster.routing.allocation.enforce_default_tier_preference] configured." + " Discontinue use of this setting.", + false, + null + ) + ) + ); } } diff --git a/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/NodesDeprecationCheckRequestTests.java b/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/NodesDeprecationCheckRequestTests.java index d073637b548d3..ea3d5dd4d0f6e 100644 --- a/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/NodesDeprecationCheckRequestTests.java +++ b/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/NodesDeprecationCheckRequestTests.java @@ -12,8 +12,7 @@ import java.io.IOException; -public class NodesDeprecationCheckRequestTests - extends AbstractWireSerializingTestCase { +public class NodesDeprecationCheckRequestTests extends AbstractWireSerializingTestCase { @Override protected Writeable.Reader instanceReader() { @@ -22,14 +21,13 @@ protected Writeable.Reader instanceReader() { @Override protected NodesDeprecationCheckRequest mutateInstance(NodesDeprecationCheckRequest instance) throws IOException { - int newSize = randomValueOtherThan(instance.nodesIds().length, () -> randomIntBetween(0,10)); + int newSize = randomValueOtherThan(instance.nodesIds().length, () -> randomIntBetween(0, 10)); String[] newNodeIds = randomArray(newSize, newSize, String[]::new, () -> randomAlphaOfLengthBetween(5, 10)); return new NodesDeprecationCheckRequest(newNodeIds); } @Override protected NodesDeprecationCheckRequest createTestInstance() { - return new NodesDeprecationCheckRequest(randomArray(0, 10, String[]::new, - ()-> randomAlphaOfLengthBetween(5,10))); + return new NodesDeprecationCheckRequest(randomArray(0, 10, String[]::new, () -> randomAlphaOfLengthBetween(5, 10))); } } diff --git a/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/NodesDeprecationCheckResponseTests.java b/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/NodesDeprecationCheckResponseTests.java index 5728aa516a6b8..b7875c199894b 100644 --- a/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/NodesDeprecationCheckResponseTests.java +++ b/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/NodesDeprecationCheckResponseTests.java @@ -25,8 +25,7 @@ import java.util.Collections; import java.util.List; -public class NodesDeprecationCheckResponseTests - extends AbstractWireSerializingTestCase { +public class NodesDeprecationCheckResponseTests extends AbstractWireSerializingTestCase { @Override protected Writeable.Reader instanceReader() { @@ -36,17 +35,15 @@ protected Writeable.Reader instanceReader() { @Override protected NodesDeprecationCheckResponse createTestInstance() { - List responses = - Arrays.asList(randomArray(1, 10, NodesDeprecationCheckAction.NodeResponse[]::new, - NodesDeprecationCheckResponseTests::randomNodeResponse)); - return new NodesDeprecationCheckResponse(new ClusterName(randomAlphaOfLength(10)), - responses, - Collections.emptyList()); + List responses = Arrays.asList( + randomArray(1, 10, NodesDeprecationCheckAction.NodeResponse[]::new, NodesDeprecationCheckResponseTests::randomNodeResponse) + ); + return new NodesDeprecationCheckResponse(new ClusterName(randomAlphaOfLength(10)), responses, Collections.emptyList()); } @Override protected NodesDeprecationCheckResponse mutateInstance(NodesDeprecationCheckResponse instance) throws IOException { - int mutate = randomIntBetween(1,3); + int mutate = randomIntBetween(1, 3); switch (mutate) { case 1: List responses = new ArrayList<>(instance.getNodes()); @@ -57,7 +54,7 @@ protected NodesDeprecationCheckResponse mutateInstance(NodesDeprecationCheckResp failures.add(new FailedNodeException("test node", "test failure", new RuntimeException(randomAlphaOfLength(10)))); return new NodesDeprecationCheckResponse(instance.getClusterName(), instance.getNodes(), failures); case 3: - String clusterName = randomValueOtherThan(instance.getClusterName().value(), () -> randomAlphaOfLengthBetween(5,15)); + String clusterName = randomValueOtherThan(instance.getClusterName().value(), () -> randomAlphaOfLengthBetween(5, 15)); return new NodesDeprecationCheckResponse(new ClusterName(clusterName), instance.getNodes(), instance.failures()); default: fail("invalid mutation"); @@ -67,12 +64,20 @@ protected NodesDeprecationCheckResponse mutateInstance(NodesDeprecationCheckResp } private static DiscoveryNode randomDiscoveryNode() throws Exception { - InetAddress inetAddress = InetAddress.getByAddress(randomAlphaOfLength(5), - new byte[] { (byte) 192, (byte) 168, (byte) 0, (byte) 1}); + InetAddress inetAddress = InetAddress.getByAddress( + randomAlphaOfLength(5), + new byte[] { (byte) 192, (byte) 168, (byte) 0, (byte) 1 } + ); TransportAddress transportAddress = new TransportAddress(inetAddress, randomIntBetween(0, 65535)); - return new DiscoveryNode(randomAlphaOfLength(5), randomAlphaOfLength(5), transportAddress, - Collections.emptyMap(), Collections.emptySet(), Version.CURRENT); + return new DiscoveryNode( + randomAlphaOfLength(5), + randomAlphaOfLength(5), + transportAddress, + Collections.emptyMap(), + Collections.emptySet(), + Version.CURRENT + ); } private static NodesDeprecationCheckAction.NodeResponse randomNodeResponse() { diff --git a/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/TransportDeprecationInfoActionTests.java b/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/TransportDeprecationInfoActionTests.java index 118cfd6cb8c64..85fa375c09c5f 100644 --- a/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/TransportDeprecationInfoActionTests.java +++ b/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/TransportDeprecationInfoActionTests.java @@ -25,14 +25,18 @@ public class TransportDeprecationInfoActionTests extends ESTestCase { public void testPluginSettingIssues() { DeprecationChecker.Components components = new DeprecationChecker.Components(null, Settings.EMPTY, null); PlainActionFuture>> future = new PlainActionFuture<>(); - TransportDeprecationInfoAction.pluginSettingIssues(List.of( - new NamedChecker("foo", List.of(), false), - new NamedChecker("bar", - List.of(new DeprecationIssue(DeprecationIssue.Level.WARNING, "bar msg", "", "details", false, Map.of("key", "value"))), - false)), + TransportDeprecationInfoAction.pluginSettingIssues( + List.of( + new NamedChecker("foo", List.of(), false), + new NamedChecker( + "bar", + List.of(new DeprecationIssue(DeprecationIssue.Level.WARNING, "bar msg", "", "details", false, Map.of("key", "value"))), + false + ) + ), components, future - ); + ); Map> issueMap = future.actionGet(); assertThat(issueMap.size(), equalTo(2)); assertThat(issueMap.get("foo"), is(empty())); @@ -45,11 +49,15 @@ public void testPluginSettingIssues() { public void testPluginSettingIssuesWithFailures() { DeprecationChecker.Components components = new DeprecationChecker.Components(null, Settings.EMPTY, null); PlainActionFuture>> future = new PlainActionFuture<>(); - TransportDeprecationInfoAction.pluginSettingIssues(List.of( - new NamedChecker("foo", List.of(), false), - new NamedChecker("bar", - List.of(new DeprecationIssue(DeprecationIssue.Level.WARNING, "bar msg", "", null, false, null)), - true)), + TransportDeprecationInfoAction.pluginSettingIssues( + List.of( + new NamedChecker("foo", List.of(), false), + new NamedChecker( + "bar", + List.of(new DeprecationIssue(DeprecationIssue.Level.WARNING, "bar msg", "", null, false, null)), + true + ) + ), components, future ); diff --git a/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/TransportNodeDeprecationCheckActionTests.java b/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/TransportNodeDeprecationCheckActionTests.java index d6eb8f9e39cc3..b880a3634afc3 100644 --- a/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/TransportNodeDeprecationCheckActionTests.java +++ b/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/TransportNodeDeprecationCheckActionTests.java @@ -37,8 +37,10 @@ public void testNodeOperation() { settingsBuilder.put("some.other.bad.deprecated.property", "someValue2"); settingsBuilder.put("some.undeprecated.property", "someValue3"); settingsBuilder.putList("some.undeprecated.list.property", List.of("someValue4", "someValue5")); - settingsBuilder.putList(DeprecationChecks.SKIP_DEPRECATIONS_SETTING.getKey(), - List.of("some.deprecated.property", "some.other.*.deprecated.property")); + settingsBuilder.putList( + DeprecationChecks.SKIP_DEPRECATIONS_SETTING.getKey(), + List.of("some.deprecated.property", "some.other.*.deprecated.property") + ); Settings inputSettings = settingsBuilder.build(); ThreadPool threadPool = null; final XPackLicenseState licenseState = null; @@ -73,15 +75,18 @@ public void testNodeOperation() { settingsBuilder = Settings.builder(); settingsBuilder.put("some.undeprecated.property", "someValue3"); settingsBuilder.putList("some.undeprecated.list.property", List.of("someValue4", "someValue5")); - settingsBuilder.putList(DeprecationChecks.SKIP_DEPRECATIONS_SETTING.getKey(), - List.of("some.deprecated.property", "some.other.*.deprecated.property")); + settingsBuilder.putList( + DeprecationChecks.SKIP_DEPRECATIONS_SETTING.getKey(), + List.of("some.deprecated.property", "some.other.*.deprecated.property") + ); Settings expectedSettings = settingsBuilder.build(); Assert.assertNotNull(visibleSettings.get()); Assert.assertEquals(expectedSettings, visibleSettings.get()); // Testing that the setting is dynamically updatable: - Settings newSettings = Settings.builder().putList(DeprecationChecks.SKIP_DEPRECATIONS_SETTING.getKey(), - List.of("some.undeprecated.property")).build(); + Settings newSettings = Settings.builder() + .putList(DeprecationChecks.SKIP_DEPRECATIONS_SETTING.getKey(), List.of("some.undeprecated.property")) + .build(); clusterSettings.applySettings(newSettings); transportNodeDeprecationCheckAction.nodeOperation(nodeRequest, nodeSettingsChecks); settingsBuilder = Settings.builder(); @@ -89,8 +94,10 @@ public void testNodeOperation() { settingsBuilder.put("some.other.bad.deprecated.property", "someValue2"); settingsBuilder.putList("some.undeprecated.list.property", List.of("someValue4", "someValue5")); // This is the node setting (since this is the node deprecation check), not the cluster setting: - settingsBuilder.putList(DeprecationChecks.SKIP_DEPRECATIONS_SETTING.getKey(), - List.of("some.deprecated.property", "some.other.*.deprecated.property")); + settingsBuilder.putList( + DeprecationChecks.SKIP_DEPRECATIONS_SETTING.getKey(), + List.of("some.deprecated.property", "some.other.*.deprecated.property") + ); expectedSettings = settingsBuilder.build(); Assert.assertNotNull(visibleSettings.get()); Assert.assertEquals(expectedSettings, visibleSettings.get()); diff --git a/x-pack/plugin/enrich/qa/common/src/main/java/org/elasticsearch/test/enrich/CommonEnrichRestTestCase.java b/x-pack/plugin/enrich/qa/common/src/main/java/org/elasticsearch/test/enrich/CommonEnrichRestTestCase.java index d8bf1b8301709..e64d9e6652132 100644 --- a/x-pack/plugin/enrich/qa/common/src/main/java/org/elasticsearch/test/enrich/CommonEnrichRestTestCase.java +++ b/x-pack/plugin/enrich/qa/common/src/main/java/org/elasticsearch/test/enrich/CommonEnrichRestTestCase.java @@ -12,12 +12,12 @@ import org.elasticsearch.client.ResponseException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.common.xcontent.support.XContentMapValues; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.test.rest.ESRestTestCase; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.json.JsonXContent; import org.junit.After; import java.io.IOException; @@ -70,33 +70,35 @@ private void setupGenericLifecycleTest(boolean deletePipeilne, String field, Str // Add entry to source index and then refresh: Request indexRequest = new Request("PUT", "/my-source-index/_doc/elastic.co"); - indexRequest.setJsonEntity("{" + - "\"host\": \"elastic.co\"," + - "\"globalRank\": 25," + - "\"tldRank\": 7," + - "\"tld\": \"co\", " + - "\"date\": {" + - "\"gte\" : \"2021-09-05\"," + - "\"lt\" : \"2021-09-07\"" + - "}, " + - "\"integer\": {" + - "\"gte\" : 40," + - "\"lt\" : 42" + - "}, " + - "\"long\": {" + - "\"gte\" : 8000000," + - "\"lt\" : 9000000" + - "}, " + - "\"double\": {" + - "\"gte\" : 10.10," + - "\"lt\" : 20.20" + - "}, " + - "\"float\": {" + - "\"gte\" : 10000.5," + - "\"lt\" : 10000.7" + - "}, " + - "\"ip\": \"100.0.0.0/4\"" + - "}"); + indexRequest.setJsonEntity( + "{" + + "\"host\": \"elastic.co\"," + + "\"globalRank\": 25," + + "\"tldRank\": 7," + + "\"tld\": \"co\", " + + "\"date\": {" + + "\"gte\" : \"2021-09-05\"," + + "\"lt\" : \"2021-09-07\"" + + "}, " + + "\"integer\": {" + + "\"gte\" : 40," + + "\"lt\" : 42" + + "}, " + + "\"long\": {" + + "\"gte\" : 8000000," + + "\"lt\" : 9000000" + + "}, " + + "\"double\": {" + + "\"gte\" : 10.10," + + "\"lt\" : 20.20" + + "}, " + + "\"float\": {" + + "\"gte\" : 10000.5," + + "\"lt\" : 10000.7" + + "}, " + + "\"ip\": \"100.0.0.0/4\"" + + "}" + ); assertOK(client().performRequest(indexRequest)); Request refreshRequest = new Request("POST", "/my-source-index/_refresh"); assertOK(client().performRequest(refreshRequest)); @@ -108,14 +110,18 @@ private void setupGenericLifecycleTest(boolean deletePipeilne, String field, Str // Create pipeline Request putPipelineRequest = new Request("PUT", "/_ingest/pipeline/my_pipeline"); putPipelineRequest.setJsonEntity( - "{\"processors\":[" + "{\"enrich\":{\"policy_name\":\"my_policy\",\"field\":\""+field+"\",\"target_field\":\"entry\"}}" + "]}" + "{\"processors\":[" + + "{\"enrich\":{\"policy_name\":\"my_policy\",\"field\":\"" + + field + + "\",\"target_field\":\"entry\"}}" + + "]}" ); assertOK(client().performRequest(putPipelineRequest)); // Index document using pipeline with enrich processor: indexRequest = new Request("PUT", "/my-index/_doc/1"); indexRequest.addParameter("pipeline", "my_pipeline"); - indexRequest.setJsonEntity("{\""+field+"\": \""+value+"\"}"); + indexRequest.setJsonEntity("{\"" + field + "\": \"" + value + "\"}"); assertOK(client().performRequest(indexRequest)); // Check if document has been enriched @@ -208,10 +214,7 @@ public void testDeleteExistingPipeline() throws Exception { ResponseException.class, () -> client().performRequest(new Request("DELETE", "/_enrich/policy/my_policy")) ); - assertTrue( - exc.getMessage() - .contains("Could not delete policy [my_policy] because a pipeline is referencing it [") - ); + assertTrue(exc.getMessage().contains("Could not delete policy [my_policy] because a pipeline is referencing it [")); assertTrue(exc.getMessage().contains("another_pipeline")); assertTrue(exc.getMessage().contains("my_pipeline")); @@ -236,7 +239,7 @@ public static String generatePolicySource(String index, String field, String typ source.field("query", QueryBuilders.matchAllQuery()); } source.field("match_field", field); - source.field("enrich_fields", new String[]{"globalRank", "tldRank", "tld"}); + source.field("enrich_fields", new String[] { "globalRank", "tldRank", "tld" }); } source.endObject().endObject(); return Strings.toString(source); @@ -253,7 +256,9 @@ public static String createSourceIndexMapping() { + "\"globalRank\":{\"type\":\"keyword\"}," + "\"tldRank\":{\"type\":\"keyword\"}," + "\"tld\":{\"type\":\"keyword\"}," - + "\"date\":{\"type\":\"date_range\"" + (randomBoolean() ? "" : ", \"format\": \"yyyy-MM-dd\"") + "}," + + "\"date\":{\"type\":\"date_range\"" + + (randomBoolean() ? "" : ", \"format\": \"yyyy-MM-dd\"") + + "}," + "\"integer\":{\"type\":\"integer_range\"}," + "\"long\":{\"type\":\"long_range\"}," + "\"double\":{\"type\":\"double_range\"}," diff --git a/x-pack/plugin/enrich/qa/rest-with-advanced-security/src/javaRestTest/java/org/elasticsearch/xpack/enrich/EnrichAdvancedSecurityIT.java b/x-pack/plugin/enrich/qa/rest-with-advanced-security/src/javaRestTest/java/org/elasticsearch/xpack/enrich/EnrichAdvancedSecurityIT.java index f0cb70b80f4a6..8b466f6c439a7 100644 --- a/x-pack/plugin/enrich/qa/rest-with-advanced-security/src/javaRestTest/java/org/elasticsearch/xpack/enrich/EnrichAdvancedSecurityIT.java +++ b/x-pack/plugin/enrich/qa/rest-with-advanced-security/src/javaRestTest/java/org/elasticsearch/xpack/enrich/EnrichAdvancedSecurityIT.java @@ -6,18 +6,18 @@ */ package org.elasticsearch.xpack.enrich; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.nullValue; - -import java.io.IOException; -import java.util.Map; - import org.elasticsearch.client.Request; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.test.enrich.CommonEnrichRestTestCase; +import java.io.IOException; +import java.util.Map; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.nullValue; + public class EnrichAdvancedSecurityIT extends CommonEnrichRestTestCase { @Override diff --git a/x-pack/plugin/enrich/qa/rest/src/yamlRestTest/java/org/elasticsearch/xpack/enrich/EnrichRestIT.java b/x-pack/plugin/enrich/qa/rest/src/yamlRestTest/java/org/elasticsearch/xpack/enrich/EnrichRestIT.java index 01dbf6c3d6303..98b4ad024639e 100644 --- a/x-pack/plugin/enrich/qa/rest/src/yamlRestTest/java/org/elasticsearch/xpack/enrich/EnrichRestIT.java +++ b/x-pack/plugin/enrich/qa/rest/src/yamlRestTest/java/org/elasticsearch/xpack/enrich/EnrichRestIT.java @@ -8,6 +8,7 @@ package org.elasticsearch.xpack.enrich; import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; + import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate; import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase; diff --git a/x-pack/plugin/eql/qa/common/src/main/java/org/elasticsearch/test/eql/BaseEqlSpecTestCase.java b/x-pack/plugin/eql/qa/common/src/main/java/org/elasticsearch/test/eql/BaseEqlSpecTestCase.java index 947a553c21d2c..1a2b84209fcd1 100644 --- a/x-pack/plugin/eql/qa/common/src/main/java/org/elasticsearch/test/eql/BaseEqlSpecTestCase.java +++ b/x-pack/plugin/eql/qa/common/src/main/java/org/elasticsearch/test/eql/BaseEqlSpecTestCase.java @@ -107,11 +107,9 @@ protected void assertResponse(EqlSearchResponse response) { Hits hits = response.hits(); if (hits.events() != null) { assertEvents(hits.events()); - } - else if (hits.sequences() != null) { + } else if (hits.sequences() != null) { assertSequences(hits.sequences()); - } - else { + } else { fail("No events or sequences found"); } } @@ -131,7 +129,7 @@ protected EqlSearchResponse runQuery(String index, String query) throws Exceptio return runRequest(eqlClient(), request); } - protected EqlSearchResponse runRequest(EqlClient eqlClient, EqlSearchRequest request) throws IOException { + protected EqlSearchResponse runRequest(EqlClient eqlClient, EqlSearchRequest request) throws IOException { int timeout = Math.toIntExact(timeout().millis()); RequestConfig config = RequestConfig.copy(RequestConfig.DEFAULT) @@ -168,9 +166,18 @@ public String toString() { long[] expected = eventIds; long[] actual = extractIds(events); - assertArrayEquals(LoggerMessageFormat.format(null, "unexpected result for spec[{}] [{}] -> {} vs {}", name, query, Arrays.toString( - expected), Arrays.toString(actual)), - expected, actual); + assertArrayEquals( + LoggerMessageFormat.format( + null, + "unexpected result for spec[{}] [{}] -> {} vs {}", + name, + query, + Arrays.toString(expected), + Arrays.toString(actual) + ), + expected, + actual + ); } private String eventsToString(List events) { @@ -194,13 +201,9 @@ private long[] extractIds(List events) { } protected void assertSequences(List sequences) { - List events = sequences.stream() - .flatMap(s -> s.events().stream()) - .collect(toList()); + List events = sequences.stream().flatMap(s -> s.events().stream()).collect(toList()); assertEvents(events); - List keys = sequences.stream() - .flatMap(s -> s.joinKeys().stream()) - .collect(toList()); + List keys = sequences.stream().flatMap(s -> s.joinKeys().stream()).collect(toList()); assertEvents(events); assertJoinKeys(keys); } @@ -225,8 +228,18 @@ public String toString() { } i++; } - assertArrayEquals(LoggerMessageFormat.format(null, "unexpected result for spec[{}] [{}] -> {} vs {}", name, query, - Arrays.toString(joinKeys), Arrays.toString(actual)), joinKeys, actual); + assertArrayEquals( + LoggerMessageFormat.format( + null, + "unexpected result for spec[{}] [{}] -> {} vs {}", + name, + query, + Arrays.toString(joinKeys), + Arrays.toString(actual) + ), + joinKeys, + actual + ); } private String keysToString(List keys) { diff --git a/x-pack/plugin/eql/qa/common/src/main/java/org/elasticsearch/test/eql/DataLoader.java b/x-pack/plugin/eql/qa/common/src/main/java/org/elasticsearch/test/eql/DataLoader.java index 3cb0324d11131..e2cc067363512 100644 --- a/x-pack/plugin/eql/qa/common/src/main/java/org/elasticsearch/test/eql/DataLoader.java +++ b/x-pack/plugin/eql/qa/common/src/main/java/org/elasticsearch/test/eql/DataLoader.java @@ -6,21 +6,6 @@ */ package org.elasticsearch.test.eql; -import static org.hamcrest.Matchers.greaterThan; -import static org.hamcrest.Matchers.instanceOf; -import static org.junit.Assert.assertThat; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.function.Consumer; - import org.apache.http.HttpHost; import org.apache.logging.log4j.LogManager; import org.elasticsearch.action.bulk.BulkRequest; @@ -34,14 +19,29 @@ import org.elasticsearch.cluster.ClusterModule; import org.elasticsearch.common.CheckedBiFunction; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.test.rest.ESRestTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContent; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.rest.ESRestTestCase; import org.elasticsearch.xpack.ql.TestUtils; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.function.Consumer; + +import static org.hamcrest.Matchers.greaterThan; +import static org.hamcrest.Matchers.instanceOf; +import static org.junit.Assert.assertThat; + /** * Loads EQL dataset into ES. * @@ -68,24 +68,22 @@ public class DataLoader { private static Map getReplacementPatterns() { final Map map = new HashMap<>(1); - map.put("[runtime_random_keyword_type]", new String[] {"keyword", "wildcard"}); + map.put("[runtime_random_keyword_type]", new String[] { "keyword", "wildcard" }); return map; } public static void main(String[] args) throws IOException { main = true; try (RestClient client = RestClient.builder(new HttpHost("localhost", 9200)).build()) { - loadDatasetIntoEs(new RestHighLevelClient( - client, - ignore -> { - }, - List.of()) { + loadDatasetIntoEs(new RestHighLevelClient(client, ignore -> {}, List.of()) { }, DataLoader::createParser); } } - public static void loadDatasetIntoEs(RestHighLevelClient client, - CheckedBiFunction p) throws IOException { + public static void loadDatasetIntoEs( + RestHighLevelClient client, + CheckedBiFunction p + ) throws IOException { // // Main Index @@ -104,8 +102,13 @@ public static void loadDatasetIntoEs(RestHighLevelClient client, load(client, TEST_NANOS_INDEX, TEST_INDEX, DataLoader::timestampToUnixNanos, p); } - private static void load(RestHighLevelClient client, String indexName, String dataName, Consumer> datasetTransform, - CheckedBiFunction p) throws IOException { + private static void load( + RestHighLevelClient client, + String indexName, + String dataName, + Consumer> datasetTransform, + CheckedBiFunction p + ) throws IOException { String name = "/data/" + indexName + ".mapping"; URL mapping = DataLoader.class.getResource(name); if (mapping == null) { @@ -147,14 +150,18 @@ private static String readMapping(URL resource) throws IOException { } } - private static CharSequence randomOf(String...values) { + private static CharSequence randomOf(String... values) { return main ? values[0] : ESRestTestCase.randomFrom(values); } @SuppressWarnings("unchecked") - private static void loadData(RestHighLevelClient client, String indexName, Consumer> datasetTransform, - URL resource, CheckedBiFunction p) - throws IOException { + private static void loadData( + RestHighLevelClient client, + String indexName, + Consumer> datasetTransform, + URL resource, + CheckedBiFunction p + ) throws IOException { BulkRequest bulk = new BulkRequest(); bulk.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); @@ -199,7 +206,7 @@ private static void timestampToUnixNanos(Map entry) { String milliFraction = timestamp.substring(12); // strip the fractions right away if not actually present entry.put("@timestamp", milliFraction.equals("000000") ? millis : millis + "." + milliFraction); - entry.put("timestamp", ((long) object)/1_000_000L); + entry.put("timestamp", ((long) object) / 1_000_000L); } public static long winFileTimeToUnix(final long filetime) { diff --git a/x-pack/plugin/eql/qa/common/src/main/java/org/elasticsearch/test/eql/EqlRestTestCase.java b/x-pack/plugin/eql/qa/common/src/main/java/org/elasticsearch/test/eql/EqlRestTestCase.java index 8888b9e0a872a..d0cfb45dad3aa 100644 --- a/x-pack/plugin/eql/qa/common/src/main/java/org/elasticsearch/test/eql/EqlRestTestCase.java +++ b/x-pack/plugin/eql/qa/common/src/main/java/org/elasticsearch/test/eql/EqlRestTestCase.java @@ -36,15 +36,14 @@ public void checkSearchContent() throws Exception { } private static final String[][] testBadRequests = { - {null, "request body or source parameter is required"}, - {"{}", "query is null or empty"}, - {"{\"query\": \"\"}", "query is null or empty"}, - {"{\"query\": \"" + validQuery + "\", \"timestamp_field\": \"\"}", "timestamp field is null or empty"}, - {"{\"query\": \"" + validQuery + "\", \"event_category_field\": \"\"}", "event category field is null or empty"}, - {"{\"query\": \"" + validQuery + "\", \"size\": -1}", "size must be greater than or equal to 0"}, - {"{\"query\": \"" + validQuery + "\", \"filter\": null}", "filter doesn't support values of type: VALUE_NULL"}, - {"{\"query\": \"" + validQuery + "\", \"filter\": {}}", "query malformed, empty clause found"} - }; + { null, "request body or source parameter is required" }, + { "{}", "query is null or empty" }, + { "{\"query\": \"\"}", "query is null or empty" }, + { "{\"query\": \"" + validQuery + "\", \"timestamp_field\": \"\"}", "timestamp field is null or empty" }, + { "{\"query\": \"" + validQuery + "\", \"event_category_field\": \"\"}", "event category field is null or empty" }, + { "{\"query\": \"" + validQuery + "\", \"size\": -1}", "size must be greater than or equal to 0" }, + { "{\"query\": \"" + validQuery + "\", \"filter\": null}", "filter doesn't support values of type: VALUE_NULL" }, + { "{\"query\": \"" + validQuery + "\", \"filter\": {}}", "query malformed, empty clause found" } }; public void testBadRequests() throws Exception { createIndex(defaultValidationIndexName, (String) null); @@ -79,9 +78,16 @@ public void testIndexWildcardPatterns() throws Exception { bulkIndex(bulk.toString()); String[] wildcardRequests = { - "test1,test2","test1*,test2","test1,test2*","test1*,test2*","test*","test1,test2,inexistent","my_alias","my_alias,test*", - "test2,my_alias,test1","my_al*" - }; + "test1,test2", + "test1*,test2", + "test1,test2*", + "test1*,test2*", + "test*", + "test1,test2,inexistent", + "my_alias", + "my_alias,test*", + "test2,my_alias,test1", + "my_al*" }; for (String indexPattern : wildcardRequests) { String endpoint = "/" + indexPattern(indexPattern) + "/_eql/search"; diff --git a/x-pack/plugin/eql/qa/common/src/main/java/org/elasticsearch/test/eql/EqlRestValidationTestCase.java b/x-pack/plugin/eql/qa/common/src/main/java/org/elasticsearch/test/eql/EqlRestValidationTestCase.java index 254e9c34b44c4..ad509c262ab49 100644 --- a/x-pack/plugin/eql/qa/common/src/main/java/org/elasticsearch/test/eql/EqlRestValidationTestCase.java +++ b/x-pack/plugin/eql/qa/common/src/main/java/org/elasticsearch/test/eql/EqlRestValidationTestCase.java @@ -25,11 +25,13 @@ public abstract class EqlRestValidationTestCase extends RemoteClusterAwareEqlRestTestCase { private static final String indexName = "test_eql"; - protected static final String[] existentIndexWithWildcard = new String[] {indexName + ",inexistent*", indexName + "*,inexistent*", - "inexistent*," + indexName}; - private static final String[] existentIndexWithoutWildcard = new String[] {indexName + ",inexistent", "inexistent," + indexName}; - protected static final String[] inexistentIndexNameWithWildcard = new String[] {"inexistent*", "inexistent1*,inexistent2*"}; - protected static final String[] inexistentIndexNameWithoutWildcard = new String[] {"inexistent", "inexistent1,inexistent2"}; + protected static final String[] existentIndexWithWildcard = new String[] { + indexName + ",inexistent*", + indexName + "*,inexistent*", + "inexistent*," + indexName }; + private static final String[] existentIndexWithoutWildcard = new String[] { indexName + ",inexistent", "inexistent," + indexName }; + protected static final String[] inexistentIndexNameWithWildcard = new String[] { "inexistent*", "inexistent1*,inexistent2*" }; + protected static final String[] inexistentIndexNameWithoutWildcard = new String[] { "inexistent", "inexistent1,inexistent2" }; @Before public void prepareIndices() throws IOException { @@ -37,7 +39,7 @@ public void prepareIndices() throws IOException { createIndex(indexName, (String) null); } - Object[] fieldsAndValues = new Object[] {"event_type", "my_event", "@timestamp", "2020-10-08T12:35:48Z", "val", 0}; + Object[] fieldsAndValues = new Object[] { "event_type", "my_event", "@timestamp", "2020-10-08T12:35:48Z", "val", 0 }; XContentBuilder document = jsonBuilder().startObject(); for (int i = 0; i < fieldsAndValues.length; i += 2) { document.field((String) fieldsAndValues[i], fieldsAndValues[i + 1]); @@ -86,7 +88,7 @@ protected void assertErrorMessages(String[] indices, String reqParameter, String protected void assertErrorMessage(String indexName, String reqParameter, String errorMessage) throws IOException { final Request request = createRequest(indexName, reqParameter); - ResponseException exc = expectThrows(ResponseException.class, () -> client().performRequest(request)); + ResponseException exc = expectThrows(ResponseException.class, () -> client().performRequest(request)); assertThat(exc.getResponse().getStatusLine().getStatusCode(), equalTo(404)); assertThat(exc.getMessage(), containsString(errorMessage)); @@ -94,11 +96,15 @@ protected void assertErrorMessage(String indexName, String reqParameter, String private Request createRequest(String indexName, String reqParameter) throws IOException { final Request request = new Request("POST", "/" + indexPattern(indexName) + "/_eql/search" + reqParameter); - request.setJsonEntity(Strings.toString(JsonXContent.contentBuilder() - .startObject() - .field("event_category_field", "event_type") - .field("query", "my_event where true") - .endObject())); + request.setJsonEntity( + Strings.toString( + JsonXContent.contentBuilder() + .startObject() + .field("event_category_field", "event_type") + .field("query", "my_event where true") + .endObject() + ) + ); return request; } diff --git a/x-pack/plugin/eql/qa/common/src/main/java/org/elasticsearch/test/eql/EqlSpecLoader.java b/x-pack/plugin/eql/qa/common/src/main/java/org/elasticsearch/test/eql/EqlSpecLoader.java index 70c4eb214219f..5237e3a628c21 100644 --- a/x-pack/plugin/eql/qa/common/src/main/java/org/elasticsearch/test/eql/EqlSpecLoader.java +++ b/x-pack/plugin/eql/qa/common/src/main/java/org/elasticsearch/test/eql/EqlSpecLoader.java @@ -30,10 +30,10 @@ public static List load(String path, Set uniqueTestNames) throw } } - public static List load(String ...paths) throws Exception { + public static List load(String... paths) throws Exception { Set uniqueTestNames = new HashSet<>(); List specs = new ArrayList<>(); - for (String path: paths) { + for (String path : paths) { specs.addAll(load(path, uniqueTestNames)); } return specs; diff --git a/x-pack/plugin/eql/qa/common/src/main/java/org/elasticsearch/test/eql/RemoteClusterAwareEqlRestTestCase.java b/x-pack/plugin/eql/qa/common/src/main/java/org/elasticsearch/test/eql/RemoteClusterAwareEqlRestTestCase.java index 87f9e80523465..e0ea99ddca3aa 100644 --- a/x-pack/plugin/eql/qa/common/src/main/java/org/elasticsearch/test/eql/RemoteClusterAwareEqlRestTestCase.java +++ b/x-pack/plugin/eql/qa/common/src/main/java/org/elasticsearch/test/eql/RemoteClusterAwareEqlRestTestCase.java @@ -62,11 +62,7 @@ public static void closeRemoteClients() throws IOException { } protected static RestHighLevelClient highLevelClient(RestClient client) { - return new RestHighLevelClient( - client, - ignore -> { - }, - Collections.emptyList()) { + return new RestHighLevelClient(client, ignore -> {}, Collections.emptyList()) { }; } @@ -131,9 +127,7 @@ protected static Settings secureRemoteClientSettings() { String pass = System.getProperty("tests.rest.cluster.remote.password"); if (hasText(user) && hasText(pass)) { String token = basicAuthHeaderValue(user, new SecureString(pass.toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } return Settings.EMPTY; } diff --git a/x-pack/plugin/eql/qa/common/src/main/java/org/elasticsearch/test/eql/stats/EqlUsageRestTestCase.java b/x-pack/plugin/eql/qa/common/src/main/java/org/elasticsearch/test/eql/stats/EqlUsageRestTestCase.java index 15fd7a6e625a3..7c1804f2a5a32 100644 --- a/x-pack/plugin/eql/qa/common/src/main/java/org/elasticsearch/test/eql/stats/EqlUsageRestTestCase.java +++ b/x-pack/plugin/eql/qa/common/src/main/java/org/elasticsearch/test/eql/stats/EqlUsageRestTestCase.java @@ -13,9 +13,9 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.test.eql.DataLoader; import org.elasticsearch.test.rest.ESRestTestCase; +import org.elasticsearch.xcontent.json.JsonXContent; import org.junit.Before; import java.io.IOException; @@ -26,7 +26,6 @@ import java.util.Map; import java.util.Set; - /** * Tests a random number of queries that increase various (most of the times, one query will "touch" multiple metrics values) metrics. */ @@ -172,18 +171,27 @@ public void testEqlRestUsage() throws IOException { int randomMaxspanExecutions = randomIntBetween(1, 15); allTotalQueries += randomMaxspanExecutions; for (int i = 0; i < randomMaxspanExecutions; i++) { - runEql("sequence with maxspan=1d" + - " [process where serial_event_id < 4] by exit_code" + - " [process where opcode == 1] by pid" + - " [process where opcode == 2] by pid" + - " [file where parent_process_name == \\\"file_delete_event\\\"] by exit_code" + - " until [process where opcode==1] by ppid" + - " | head 4" + - " | tail 2"); + runEql( + "sequence with maxspan=1d" + + " [process where serial_event_id < 4] by exit_code" + + " [process where opcode == 1] by pid" + + " [process where opcode == 2] by pid" + + " [file where parent_process_name == \\\"file_delete_event\\\"] by exit_code" + + " until [process where opcode==1] by ppid" + + " | head 4" + + " | tail 2" + ); } responseAsMap = getStats(); - metricsToCheck = Set.of("sequence", "sequence_maxspan", "sequence_queries_four", "pipe_head", "pipe_tail", "join_keys_one", - "sequence_until"); + metricsToCheck = Set.of( + "sequence", + "sequence_maxspan", + "sequence_queries_four", + "pipe_head", + "pipe_tail", + "join_keys_one", + "sequence_until" + ); assertFeaturesMetrics(randomMaxspanExecutions, responseAsMap, metricsToCheck); assertFeaturesMetricsExcept(responseAsMap, metricsToCheck); assertAllQueryMetrics(allTotalQueries, responseAsMap); @@ -194,10 +202,12 @@ public void testEqlRestUsage() throws IOException { int randomThreeQueriesSequences = randomIntBetween(1, 15); allTotalQueries += randomThreeQueriesSequences; for (int i = 0; i < randomThreeQueriesSequences; i++) { - runEql("sequence with maxspan=1d" + - " [process where serial_event_id < 4] by user" + - " [process where opcode == 1] by user" + - " [process where opcode == 2] by user"); + runEql( + "sequence with maxspan=1d" + + " [process where serial_event_id < 4] by user" + + " [process where opcode == 1] by user" + + " [process where opcode == 2] by user" + ); } responseAsMap = getStats(); metricsToCheck = Set.of("sequence", "sequence_queries_three", "join_keys_one", "sequence_maxspan", defaultPipe); @@ -211,13 +221,15 @@ public void testEqlRestUsage() throws IOException { int randomFiveQueriesSequences = randomIntBetween(1, 15); allTotalQueries += randomFiveQueriesSequences; for (int i = 0; i < randomFiveQueriesSequences; i++) { - runEql("sequence by user, ppid, exit_code with maxspan=1m" + - " [process where serial_event_id < 4]" + - " [process where opcode == 1]" + - " [file where parent_process_name == \\\"file_delete_event\\\"]" + - " [process where serial_event_id < 4]" + - " [process where opcode == 1]" + - "| tail 4"); + runEql( + "sequence by user, ppid, exit_code with maxspan=1m" + + " [process where serial_event_id < 4]" + + " [process where opcode == 1]" + + " [file where parent_process_name == \\\"file_delete_event\\\"]" + + " [process where serial_event_id < 4]" + + " [process where opcode == 1]" + + "| tail 4" + ); } responseAsMap = getStats(); metricsToCheck = Set.of("sequence", "sequence_queries_five_or_more", "pipe_tail", "join_keys_three", "sequence_maxspan"); @@ -231,9 +243,11 @@ public void testEqlRestUsage() throws IOException { int randomFourJoinKeysExecutions = randomIntBetween(1, 15); allTotalQueries += randomFourJoinKeysExecutions; for (int i = 0; i < randomFourJoinKeysExecutions; i++) { - runEql("sequence by exit_code, user, serial_event_id, pid" + - " [process where serial_event_id < 4]" + - " [process where opcode == 1]"); + runEql( + "sequence by exit_code, user, serial_event_id, pid" + + " [process where serial_event_id < 4]" + + " [process where opcode == 1]" + ); } responseAsMap = getStats(); metricsToCheck = Set.of("sequence", "sequence_queries_two", "join_keys_four", defaultPipe); @@ -247,9 +261,11 @@ public void testEqlRestUsage() throws IOException { int randomFiveJoinKeysExecutions = randomIntBetween(1, 15); allTotalQueries += randomFiveJoinKeysExecutions; for (int i = 0; i < randomFiveJoinKeysExecutions; i++) { - runEql("sequence by exit_code, user, serial_event_id, pid, ppid" + - " [process where serial_event_id < 4]" + - " [process where opcode == 1]"); + runEql( + "sequence by exit_code, user, serial_event_id, pid, ppid" + + " [process where serial_event_id < 4]" + + " [process where opcode == 1]" + ); } responseAsMap = getStats(); metricsToCheck = Set.of("sequence", "sequence_queries_two", "join_keys_five_or_more", defaultPipe); @@ -302,7 +318,7 @@ private Map getStats() throws UnsupportedOperationException, IOE private void runEql(String eql) throws IOException { Request request = new Request("POST", DataLoader.TEST_INDEX + "/_eql/search"); - request.setJsonEntity("{\"query\":\"" + eql +"\"}"); + request.setJsonEntity("{\"query\":\"" + eql + "\"}"); runRequest(request); } @@ -311,7 +327,7 @@ protected void runRequest(Request request) throws IOException { } private void assertFeaturesMetrics(int expected, Map responseAsMap, Set metricsToCheck) { - for(String metricName : metricsToCheck) { + for (String metricName : metricsToCheck) { assertFeatureMetric(expected, responseAsMap, metricName); } } @@ -368,11 +384,7 @@ private void assertFeaturesMetricsExcept(Map responseAsMap, Set< private RestHighLevelClient highLevelClient() { if (highLevelClient == null) { - highLevelClient = new RestHighLevelClient( - client(), - ignore -> { - }, - Collections.emptyList()) { + highLevelClient = new RestHighLevelClient(client(), ignore -> {}, Collections.emptyList()) { }; } return highLevelClient; @@ -381,8 +393,6 @@ private RestHighLevelClient highLevelClient() { @Override protected Settings restClientSettings() { String token = basicAuthHeaderValue("admin", new SecureString("admin-password".toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } } diff --git a/x-pack/plugin/eql/qa/mixed-node/src/test/java/org/elasticsearch/xpack/eql/qa/mixed_node/EqlSearchIT.java b/x-pack/plugin/eql/qa/mixed-node/src/test/java/org/elasticsearch/xpack/eql/qa/mixed_node/EqlSearchIT.java index 034b66af07630..767c6073a7c15 100644 --- a/x-pack/plugin/eql/qa/mixed-node/src/test/java/org/elasticsearch/xpack/eql/qa/mixed_node/EqlSearchIT.java +++ b/x-pack/plugin/eql/qa/mixed-node/src/test/java/org/elasticsearch/xpack/eql/qa/mixed_node/EqlSearchIT.java @@ -14,9 +14,9 @@ import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.test.NotEqualMessageBuilder; import org.elasticsearch.test.rest.ESRestTestCase; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.eql.execution.search.RuntimeUtils; import org.elasticsearch.xpack.eql.expression.function.EqlFunctionRegistry; import org.elasticsearch.xpack.ql.TestNode; @@ -120,61 +120,159 @@ public void testMultiValueFields() throws Exception { Set testedFunctions = new HashSet<>(); boolean multiValued = nodes.getBWCVersion().onOrAfter(RuntimeUtils.SWITCH_TO_MULTI_VALUE_FIELDS_VERSION); try ( - RestClient client = buildClient(restClientSettings(), - newNodes.stream().map(TestNode::getPublishAddress).toArray(HttpHost[]::new)) + RestClient client = buildClient( + restClientSettings(), + newNodes.stream().map(TestNode::getPublishAddress).toArray(HttpHost[]::new) + ) ) { // filter only the relevant bits of the response String filterPath = "filter_path=hits.events._id"; Request request = new Request("POST", index + "/_eql/search?" + filterPath); - assertMultiValueFunctionQuery(availableFunctions, testedFunctions, request, client, "between", + assertMultiValueFunctionQuery( + availableFunctions, + testedFunctions, + request, + client, + "between", "PROCESS where between(process_name, \\\"w\\\", \\\"s\\\") : \\\"indow\\\"", - multiValued ? new int[] {120, 121} : new int[] {121}); - assertMultiValueFunctionQuery(availableFunctions, testedFunctions, request, client, "cidrmatch", + multiValued ? new int[] { 120, 121 } : new int[] { 121 } + ); + assertMultiValueFunctionQuery( + availableFunctions, + testedFunctions, + request, + client, + "cidrmatch", "PROCESS where string(cidrmatch(source_address, \\\"10.6.48.157/24\\\")) : \\\"true\\\"", - multiValued ? new int[] {121, 122} : new int[] {122}); - assertMultiValueFunctionQuery(availableFunctions, testedFunctions, request, client, "concat", + multiValued ? new int[] { 121, 122 } : new int[] { 122 } + ); + assertMultiValueFunctionQuery( + availableFunctions, + testedFunctions, + request, + client, + "concat", "PROCESS where concat(file_name, process_name) == \\\"foo\\\" or add(pid, ppid) > 100", - multiValued ? new int[] {116, 117, 120, 121, 122} : new int[] {120, 121}); - assertMultiValueFunctionQuery(availableFunctions, testedFunctions, request, client, "endswith", + multiValued ? new int[] { 116, 117, 120, 121, 122 } : new int[] { 120, 121 } + ); + assertMultiValueFunctionQuery( + availableFunctions, + testedFunctions, + request, + client, + "endswith", "PROCESS where string(endswith(process_name, \\\"s\\\")) : \\\"true\\\"", - multiValued ? new int[] {120, 121} : new int[] {121}); - assertMultiValueFunctionQuery(availableFunctions, testedFunctions, request, client, "indexof", + multiValued ? new int[] { 120, 121 } : new int[] { 121 } + ); + assertMultiValueFunctionQuery( + availableFunctions, + testedFunctions, + request, + client, + "indexof", "PROCESS where indexof(file_name, \\\"x\\\", 2) > 0", - multiValued ? new int[] {116, 117} : new int[] {117}); - assertMultiValueFunctionQuery(availableFunctions, testedFunctions, request, client, "length", + multiValued ? new int[] { 116, 117 } : new int[] { 117 } + ); + assertMultiValueFunctionQuery( + availableFunctions, + testedFunctions, + request, + client, + "length", "PROCESS where length(file_name) >= 3 and length(file_name) == 1", - multiValued ? new int[] {116} : new int[] {}); - assertMultiValueFunctionQuery(availableFunctions, testedFunctions, request, client, "startswith", + multiValued ? new int[] { 116 } : new int[] {} + ); + assertMultiValueFunctionQuery( + availableFunctions, + testedFunctions, + request, + client, + "startswith", "PROCESS where string(startswith~(file_name, \\\"F\\\")) : \\\"true\\\"", - multiValued ? new int[] {116, 117, 120, 121} : new int[] {116, 120, 121}); - assertMultiValueFunctionQuery(availableFunctions, testedFunctions, request, client, "string", + multiValued ? new int[] { 116, 117, 120, 121 } : new int[] { 116, 120, 121 } + ); + assertMultiValueFunctionQuery( + availableFunctions, + testedFunctions, + request, + client, + "string", "PROCESS where string(concat(file_name, process_name) == \\\"foo\\\") : \\\"true\\\"", - multiValued ? new int[] {116, 120} : new int[] {120}); - assertMultiValueFunctionQuery(availableFunctions, testedFunctions, request, client, "stringcontains", + multiValued ? new int[] { 116, 120 } : new int[] { 120 } + ); + assertMultiValueFunctionQuery( + availableFunctions, + testedFunctions, + request, + client, + "stringcontains", "PROCESS where string(stringcontains(file_name, \\\"txt\\\")) : \\\"true\\\"", - multiValued ? new int[] {117} : new int[] {}); - assertMultiValueFunctionQuery(availableFunctions, testedFunctions, request, client, "substring", + multiValued ? new int[] { 117 } : new int[] {} + ); + assertMultiValueFunctionQuery( + availableFunctions, + testedFunctions, + request, + client, + "substring", "PROCESS where substring(file_name, -4) : \\\".txt\\\"", - multiValued ? new int[] {117} : new int[] {}); - assertMultiValueFunctionQuery(availableFunctions, testedFunctions, request, client, "add", + multiValued ? new int[] { 117 } : new int[] {} + ); + assertMultiValueFunctionQuery( + availableFunctions, + testedFunctions, + request, + client, + "add", "PROCESS where add(pid, 1) == 2", - multiValued ? new int[] {120, 121, 122} : new int[] {120, 121, 122}); - assertMultiValueFunctionQuery(availableFunctions, testedFunctions, request, client, "divide", + multiValued ? new int[] { 120, 121, 122 } : new int[] { 120, 121, 122 } + ); + assertMultiValueFunctionQuery( + availableFunctions, + testedFunctions, + request, + client, + "divide", "PROCESS where divide(pid, 12) == 1", - multiValued ? new int[] {116, 117, 118, 119, 120, 122} : new int[] {116, 117, 118, 119}); - assertMultiValueFunctionQuery(availableFunctions, testedFunctions, request, client, "modulo", + multiValued ? new int[] { 116, 117, 118, 119, 120, 122 } : new int[] { 116, 117, 118, 119 } + ); + assertMultiValueFunctionQuery( + availableFunctions, + testedFunctions, + request, + client, + "modulo", "PROCESS where modulo(ppid, 10) == 0", - multiValued ? new int[] {121, 122} : new int[] {121}); - assertMultiValueFunctionQuery(availableFunctions, testedFunctions, request, client, "multiply", + multiValued ? new int[] { 121, 122 } : new int[] { 121 } + ); + assertMultiValueFunctionQuery( + availableFunctions, + testedFunctions, + request, + client, + "multiply", "PROCESS where string(multiply(pid, 10) == 120) == \\\"true\\\"", - multiValued ? new int[] {116, 117, 118, 119, 120, 122} : new int[] {116, 117, 118, 119}); - assertMultiValueFunctionQuery(availableFunctions, testedFunctions, request, client, "number", + multiValued ? new int[] { 116, 117, 118, 119, 120, 122 } : new int[] { 116, 117, 118, 119 } + ); + assertMultiValueFunctionQuery( + availableFunctions, + testedFunctions, + request, + client, + "number", "PROCESS where number(command_line) + pid >= 360", - multiValued ? new int[] {122, 123} : new int[] {123}); - assertMultiValueFunctionQuery(availableFunctions, testedFunctions, request, client, "subtract", + multiValued ? new int[] { 122, 123 } : new int[] { 123 } + ); + assertMultiValueFunctionQuery( + availableFunctions, + testedFunctions, + request, + client, + "subtract", "PROCESS where subtract(pid, 1) == 0", - multiValued ? new int[] {120, 121, 122} : new int[] {120, 121, 122}); + multiValued ? new int[] { 120, 121, 122 } : new int[] { 120, 121, 122 } + ); } // check that ALL functions from the function registry have a test query. We don't want to miss any of the functions, since this @@ -186,8 +284,10 @@ private void assertEventsQueryOnNodes(List nodesList) throws Exception final String event = randomEvent(); Map expectedResponse = prepareEventsTestData(event); try ( - RestClient client = buildClient(restClientSettings(), - nodesList.stream().map(TestNode::getPublishAddress).toArray(HttpHost[]::new)) + RestClient client = buildClient( + restClientSettings(), + nodesList.stream().map(TestNode::getPublishAddress).toArray(HttpHost[]::new) + ) ) { // filter only the relevant bits of the response String filterPath = "filter_path=hits.events._source.@timestamp,hits.events._source.event_type,hits.events._source.sequence"; @@ -201,8 +301,10 @@ private void assertEventsQueryOnNodes(List nodesList) throws Exception private void assertSequncesQueryOnNodes(List nodesList) throws Exception { Map expectedResponse = prepareSequencesTestData(); try ( - RestClient client = buildClient(restClientSettings(), - nodesList.stream().map(TestNode::getPublishAddress).toArray(HttpHost[]::new)) + RestClient client = buildClient( + restClientSettings(), + nodesList.stream().map(TestNode::getPublishAddress).toArray(HttpHost[]::new) + ) ) { String filterPath = "filter_path=hits.sequences.join_keys,hits.sequences.events._id,hits.sequences.events._source"; String query = "sequence by `sequence` with maxspan=100ms [success where true] by correlation_success1, correlation_success2 " @@ -310,8 +412,15 @@ private Map prepareSequencesTestData() throws IOException { return expectedResponse; } - private void assertMultiValueFunctionQuery(Set availableFunctions, Set testedFunctions, Request request, - RestClient client, String functionName, String query, int[] ids) throws IOException { + private void assertMultiValueFunctionQuery( + Set availableFunctions, + Set testedFunctions, + Request request, + RestClient client, + String functionName, + String query, + int[] ids + ) throws IOException { List eventIds = new ArrayList<>(); for (int id : ids) { eventIds.add(String.valueOf(id)); diff --git a/x-pack/plugin/eql/qa/multi-cluster-with-security/src/test/java/org/elasticsearch/xpack/eql/EqlRestValidationIT.java b/x-pack/plugin/eql/qa/multi-cluster-with-security/src/test/java/org/elasticsearch/xpack/eql/EqlRestValidationIT.java index ad08863c97eae..66617341aa27e 100644 --- a/x-pack/plugin/eql/qa/multi-cluster-with-security/src/test/java/org/elasticsearch/xpack/eql/EqlRestValidationIT.java +++ b/x-pack/plugin/eql/qa/multi-cluster-with-security/src/test/java/org/elasticsearch/xpack/eql/EqlRestValidationIT.java @@ -22,14 +22,23 @@ protected String getInexistentIndexErrorMessage() { protected void assertErrorMessageWhenAllowNoIndicesIsFalse(String reqParameter) throws IOException { assertErrorMessage("inexistent1*", reqParameter, getInexistentIndexErrorMessage() + "[" + indexPattern("inexistent1*") + "]\""); - assertErrorMessage("inexistent1*,inexistent2*", reqParameter, getInexistentIndexErrorMessage() + - "[" + indexPattern("inexistent1*") + "]\""); - assertErrorMessage("test_eql,inexistent*", reqParameter, getInexistentIndexErrorMessage() + - "[" + indexPattern("inexistent*") + "]\""); - //TODO: revisit the next two tests when https://github.com/elastic/elasticsearch/issues/64190 is closed + assertErrorMessage( + "inexistent1*,inexistent2*", + reqParameter, + getInexistentIndexErrorMessage() + "[" + indexPattern("inexistent1*") + "]\"" + ); + assertErrorMessage( + "test_eql,inexistent*", + reqParameter, + getInexistentIndexErrorMessage() + "[" + indexPattern("inexistent*") + "]\"" + ); + // TODO: revisit the next two tests when https://github.com/elastic/elasticsearch/issues/64190 is closed assertErrorMessage("inexistent", reqParameter, getInexistentIndexErrorMessage() + "[" + indexPattern("inexistent") + "]\""); - assertErrorMessage("inexistent1,inexistent2", reqParameter, getInexistentIndexErrorMessage() + - "[" + indexPattern("inexistent1") + "," + indexPattern("inexistent2") + "]\""); + assertErrorMessage( + "inexistent1,inexistent2", + reqParameter, + getInexistentIndexErrorMessage() + "[" + indexPattern("inexistent1") + "," + indexPattern("inexistent2") + "]\"" + ); } @Override diff --git a/x-pack/plugin/eql/qa/multi-cluster-with-security/src/test/java/org/elasticsearch/xpack/eql/RemoteClusterTestUtils.java b/x-pack/plugin/eql/qa/multi-cluster-with-security/src/test/java/org/elasticsearch/xpack/eql/RemoteClusterTestUtils.java index ea255e771b6f6..004b0d0683732 100644 --- a/x-pack/plugin/eql/qa/multi-cluster-with-security/src/test/java/org/elasticsearch/xpack/eql/RemoteClusterTestUtils.java +++ b/x-pack/plugin/eql/qa/multi-cluster-with-security/src/test/java/org/elasticsearch/xpack/eql/RemoteClusterTestUtils.java @@ -18,7 +18,7 @@ public static String remoteClusterIndex(String indexName) { public static String remoteClusterPattern(String pattern) { StringJoiner sj = new StringJoiner(","); - for (String index: pattern.split(",")) { + for (String index : pattern.split(",")) { sj.add(remoteClusterIndex(index)); } return sj.toString(); diff --git a/x-pack/plugin/eql/qa/rest/src/javaRestTest/java/org/elasticsearch/xpack/eql/EqlRestIT.java b/x-pack/plugin/eql/qa/rest/src/javaRestTest/java/org/elasticsearch/xpack/eql/EqlRestIT.java index 63158dcc501d9..4aa8d9332fcf5 100644 --- a/x-pack/plugin/eql/qa/rest/src/javaRestTest/java/org/elasticsearch/xpack/eql/EqlRestIT.java +++ b/x-pack/plugin/eql/qa/rest/src/javaRestTest/java/org/elasticsearch/xpack/eql/EqlRestIT.java @@ -17,8 +17,6 @@ public class EqlRestIT extends EqlRestTestCase { @Override protected Settings restClientSettings() { String token = basicAuthHeaderValue("admin", new SecureString("admin-password".toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } } diff --git a/x-pack/plugin/eql/qa/rest/src/javaRestTest/java/org/elasticsearch/xpack/eql/EqlRestValidationIT.java b/x-pack/plugin/eql/qa/rest/src/javaRestTest/java/org/elasticsearch/xpack/eql/EqlRestValidationIT.java index b5fbaa8fbd779..f0dafda705619 100644 --- a/x-pack/plugin/eql/qa/rest/src/javaRestTest/java/org/elasticsearch/xpack/eql/EqlRestValidationIT.java +++ b/x-pack/plugin/eql/qa/rest/src/javaRestTest/java/org/elasticsearch/xpack/eql/EqlRestValidationIT.java @@ -19,16 +19,31 @@ protected String getInexistentIndexErrorMessage() { } protected void assertErrorMessageWhenAllowNoIndicesIsFalse(String reqParameter) throws IOException { - assertErrorMessage("inexistent1*", reqParameter, "\"root_cause\":[{\"type\":\"index_not_found_exception\"," - + "\"reason\":\"no such index [inexistent1*]\""); - assertErrorMessage("inexistent1*,inexistent2*", reqParameter, "\"root_cause\":[{\"type\":\"index_not_found_exception\"," - + "\"reason\":\"no such index [inexistent1*]\""); - assertErrorMessage("test_eql,inexistent*", reqParameter, "\"root_cause\":[{\"type\":\"index_not_found_exception\"," - + "\"reason\":\"no such index [inexistent*]\""); - assertErrorMessage("inexistent", reqParameter, "\"root_cause\":[{\"type\":\"index_not_found_exception\"," - + "\"reason\":\"no such index [inexistent]\""); - //TODO: revisit after https://github.com/elastic/elasticsearch/issues/64197 is closed - assertErrorMessage("inexistent1,inexistent2", reqParameter, "\"root_cause\":[{\"type\":\"index_not_found_exception\"," - + "\"reason\":\"no such index [null]\""); + assertErrorMessage( + "inexistent1*", + reqParameter, + "\"root_cause\":[{\"type\":\"index_not_found_exception\"," + "\"reason\":\"no such index [inexistent1*]\"" + ); + assertErrorMessage( + "inexistent1*,inexistent2*", + reqParameter, + "\"root_cause\":[{\"type\":\"index_not_found_exception\"," + "\"reason\":\"no such index [inexistent1*]\"" + ); + assertErrorMessage( + "test_eql,inexistent*", + reqParameter, + "\"root_cause\":[{\"type\":\"index_not_found_exception\"," + "\"reason\":\"no such index [inexistent*]\"" + ); + assertErrorMessage( + "inexistent", + reqParameter, + "\"root_cause\":[{\"type\":\"index_not_found_exception\"," + "\"reason\":\"no such index [inexistent]\"" + ); + // TODO: revisit after https://github.com/elastic/elasticsearch/issues/64197 is closed + assertErrorMessage( + "inexistent1,inexistent2", + reqParameter, + "\"root_cause\":[{\"type\":\"index_not_found_exception\"," + "\"reason\":\"no such index [null]\"" + ); } } diff --git a/x-pack/plugin/eql/qa/security/src/javaRestTest/java/org/elasticsearch/xpack/eql/AsyncEqlSecurityIT.java b/x-pack/plugin/eql/qa/security/src/javaRestTest/java/org/elasticsearch/xpack/eql/AsyncEqlSecurityIT.java index 08f211623a2cf..43f4d27868f15 100644 --- a/x-pack/plugin/eql/qa/security/src/javaRestTest/java/org/elasticsearch/xpack/eql/AsyncEqlSecurityIT.java +++ b/x-pack/plugin/eql/qa/security/src/javaRestTest/java/org/elasticsearch/xpack/eql/AsyncEqlSecurityIT.java @@ -13,11 +13,11 @@ import org.elasticsearch.client.ResponseException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.test.rest.ESRestTestCase; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.rest.ESRestTestCase; import org.elasticsearch.xpack.core.XPackPlugin; import org.elasticsearch.xpack.core.async.AsyncExecutionId; import org.junit.Before; @@ -62,7 +62,7 @@ public void testWithUsers() throws Exception { } private void testCase(String user, String other) throws Exception { - for (String indexName : new String[] {"index", "index-" + user}) { + for (String indexName : new String[] { "index", "index-" + user }) { Response submitResp = submitAsyncEqlSearch(indexName, "my_event where val==0", TimeValue.timeValueSeconds(10), user); assertOK(submitResp); String id = extractResponseId(submitResp); @@ -79,17 +79,19 @@ private void testCase(String user, String other) throws Exception { // other and user cannot access the result from direct get calls AsyncExecutionId searchId = AsyncExecutionId.decode(id); - for (String runAs : new String[] {user, other}) { + for (String runAs : new String[] { user, other }) { exc = expectThrows(ResponseException.class, () -> get(XPackPlugin.ASYNC_RESULTS_INDEX, searchId.getDocId(), runAs)); assertThat(exc.getResponse().getStatusLine().getStatusCode(), equalTo(403)); assertThat(exc.getMessage(), containsString("unauthorized")); } - Response delResp = deleteAsyncEqlSearch(id, user); - assertOK(delResp); + Response delResp = deleteAsyncEqlSearch(id, user); + assertOK(delResp); } - ResponseException exc = expectThrows(ResponseException.class, - () -> submitAsyncEqlSearch("index-" + other, "*", TimeValue.timeValueSeconds(10), user)); + ResponseException exc = expectThrows( + ResponseException.class, + () -> submitAsyncEqlSearch("index-" + other, "*", TimeValue.timeValueSeconds(10), user) + ); assertThat(exc.getResponse().getStatusLine().getStatusCode(), equalTo(400)); } @@ -122,11 +124,11 @@ static Response get(String index, String id, String user) throws IOException { static Response submitAsyncEqlSearch(String indexName, String query, TimeValue waitForCompletion, String user) throws IOException { final Request request = new Request("POST", indexName + "/_eql/search"); setRunAsHeader(request, user); - request.setJsonEntity(Strings.toString(JsonXContent.contentBuilder() - .startObject() - .field("event_category_field", "event_type") - .field("query", query) - .endObject())); + request.setJsonEntity( + Strings.toString( + JsonXContent.contentBuilder().startObject().field("event_category_field", "event_type").field("query", query).endObject() + ) + ); request.addParameter("wait_for_completion_timeout", waitForCompletion.toString()); // we do the cleanup explicitly request.addParameter("keep_on_completion", "true"); @@ -134,14 +136,14 @@ static Response submitAsyncEqlSearch(String indexName, String query, TimeValue w } static Response getAsyncEqlSearch(String id, String user) throws IOException { - final Request request = new Request("GET", "/_eql/search/" + id); + final Request request = new Request("GET", "/_eql/search/" + id); setRunAsHeader(request, user); request.addParameter("wait_for_completion_timeout", "0ms"); return client().performRequest(request); } static Response deleteAsyncEqlSearch(String id, String user) throws IOException { - final Request request = new Request("DELETE", "/_eql/search/" + id); + final Request request = new Request("DELETE", "/_eql/search/" + id); setRunAsHeader(request, user); return client().performRequest(request); } diff --git a/x-pack/plugin/eql/qa/security/src/javaRestTest/java/org/elasticsearch/xpack/eql/EqlRestValidationIT.java b/x-pack/plugin/eql/qa/security/src/javaRestTest/java/org/elasticsearch/xpack/eql/EqlRestValidationIT.java index 431cb6d95fe1c..066c76fe3a9dd 100644 --- a/x-pack/plugin/eql/qa/security/src/javaRestTest/java/org/elasticsearch/xpack/eql/EqlRestValidationIT.java +++ b/x-pack/plugin/eql/qa/security/src/javaRestTest/java/org/elasticsearch/xpack/eql/EqlRestValidationIT.java @@ -22,17 +22,32 @@ protected String getInexistentIndexErrorMessage() { @Override protected void assertErrorMessageWhenAllowNoIndicesIsFalse(String reqParameter) throws IOException { - assertErrorMessage("inexistent1*", reqParameter, "\"root_cause\":[{\"type\":\"index_not_found_exception\"," - + "\"reason\":\"no such index [inexistent1*]\""); - assertErrorMessage("inexistent1*,inexistent2*", reqParameter, "\"root_cause\":[{\"type\":\"index_not_found_exception\"," - + "\"reason\":\"no such index [inexistent1*]\""); - assertErrorMessage("test_eql,inexistent*", reqParameter, "\"root_cause\":[{\"type\":\"index_not_found_exception\"," - + "\"reason\":\"no such index [inexistent*]\""); - //TODO: revisit the next two tests when https://github.com/elastic/elasticsearch/issues/64190 is closed - assertErrorMessage("inexistent", reqParameter, "\"root_cause\":[{\"type\":\"index_not_found_exception\"," - + "\"reason\":\"no such index [[inexistent]]\""); - assertErrorMessage("inexistent1,inexistent2", reqParameter, "\"root_cause\":[{\"type\":\"index_not_found_exception\"," - + "\"reason\":\"no such index [[inexistent1, inexistent2]]\""); + assertErrorMessage( + "inexistent1*", + reqParameter, + "\"root_cause\":[{\"type\":\"index_not_found_exception\"," + "\"reason\":\"no such index [inexistent1*]\"" + ); + assertErrorMessage( + "inexistent1*,inexistent2*", + reqParameter, + "\"root_cause\":[{\"type\":\"index_not_found_exception\"," + "\"reason\":\"no such index [inexistent1*]\"" + ); + assertErrorMessage( + "test_eql,inexistent*", + reqParameter, + "\"root_cause\":[{\"type\":\"index_not_found_exception\"," + "\"reason\":\"no such index [inexistent*]\"" + ); + // TODO: revisit the next two tests when https://github.com/elastic/elasticsearch/issues/64190 is closed + assertErrorMessage( + "inexistent", + reqParameter, + "\"root_cause\":[{\"type\":\"index_not_found_exception\"," + "\"reason\":\"no such index [[inexistent]]\"" + ); + assertErrorMessage( + "inexistent1,inexistent2", + reqParameter, + "\"root_cause\":[{\"type\":\"index_not_found_exception\"," + "\"reason\":\"no such index [[inexistent1, inexistent2]]\"" + ); } } diff --git a/x-pack/plugin/eql/qa/security/src/javaRestTest/java/org/elasticsearch/xpack/eql/EqlStatsIT.java b/x-pack/plugin/eql/qa/security/src/javaRestTest/java/org/elasticsearch/xpack/eql/EqlStatsIT.java index 99f1cf43ed349..4449ff94eb8b5 100644 --- a/x-pack/plugin/eql/qa/security/src/javaRestTest/java/org/elasticsearch/xpack/eql/EqlStatsIT.java +++ b/x-pack/plugin/eql/qa/security/src/javaRestTest/java/org/elasticsearch/xpack/eql/EqlStatsIT.java @@ -27,7 +27,7 @@ protected Settings restClientSettings() { @Override protected void runRequest(Request request) throws IOException { - SecurityUtils.setRunAsHeader(request,"test-admin"); + SecurityUtils.setRunAsHeader(request, "test-admin"); super.runRequest(request); } } diff --git a/x-pack/plugin/eql/qa/security/src/javaRestTest/java/org/elasticsearch/xpack/eql/SecurityUtils.java b/x-pack/plugin/eql/qa/security/src/javaRestTest/java/org/elasticsearch/xpack/eql/SecurityUtils.java index aa019f02c9bcd..ae8ce28f19570 100644 --- a/x-pack/plugin/eql/qa/security/src/javaRestTest/java/org/elasticsearch/xpack/eql/SecurityUtils.java +++ b/x-pack/plugin/eql/qa/security/src/javaRestTest/java/org/elasticsearch/xpack/eql/SecurityUtils.java @@ -20,9 +20,7 @@ public class SecurityUtils { static Settings secureClientSettings() { String token = basicAuthHeaderValue("test-admin", new SecureString("x-pack-test-password".toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } static void setRunAsHeader(Request request, String user) { diff --git a/x-pack/plugin/eql/src/internalClusterTest/java/org/elasticsearch/xpack/eql/action/AbstractEqlBlockingIntegTestCase.java b/x-pack/plugin/eql/src/internalClusterTest/java/org/elasticsearch/xpack/eql/action/AbstractEqlBlockingIntegTestCase.java index f68784623286f..0c3361baf843b 100644 --- a/x-pack/plugin/eql/src/internalClusterTest/java/org/elasticsearch/xpack/eql/action/AbstractEqlBlockingIntegTestCase.java +++ b/x-pack/plugin/eql/src/internalClusterTest/java/org/elasticsearch/xpack/eql/action/AbstractEqlBlockingIntegTestCase.java @@ -147,7 +147,6 @@ public void enableSearchBlock() { shouldBlockOnSearch.set(true); } - public void disableFieldCapBlock() { shouldBlockOnFieldCapabilities.set(false); } @@ -193,7 +192,8 @@ public void app String action, Request request, ActionListener listener, - ActionFilterChain chain) { + ActionFilterChain chain + ) { if (action.equals(FieldCapabilitiesAction.NAME)) { final Consumer actionWrapper = resp -> { @@ -209,7 +209,10 @@ public void app } logger.trace("unblocking field caps on " + nodeId); }; - chain.proceed(task, action, request, + chain.proceed( + task, + action, + request, ActionListener.wrap(resp -> executorService.execute(() -> actionWrapper.accept(resp)), listener::onFailure) ); } else { @@ -237,7 +240,7 @@ protected TaskId findTaskWithXOpaqueId(String id, String action) { if (taskInfo != null) { return taskInfo.getTaskId(); } else { - return null; + return null; } } diff --git a/x-pack/plugin/eql/src/internalClusterTest/java/org/elasticsearch/xpack/eql/action/AsyncEqlSearchActionIT.java b/x-pack/plugin/eql/src/internalClusterTest/java/org/elasticsearch/xpack/eql/action/AsyncEqlSearchActionIT.java index 817bf03a1186d..49c6cf7dca68d 100644 --- a/x-pack/plugin/eql/src/internalClusterTest/java/org/elasticsearch/xpack/eql/action/AsyncEqlSearchActionIT.java +++ b/x-pack/plugin/eql/src/internalClusterTest/java/org/elasticsearch/xpack/eql/action/AsyncEqlSearchActionIT.java @@ -22,8 +22,8 @@ import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.CollectionUtils; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.IndexNotFoundException; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.script.MockScriptPlugin; @@ -55,9 +55,9 @@ import java.util.concurrent.Executors; import java.util.function.Function; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertFutureThrows; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; @@ -69,8 +69,9 @@ public class AsyncEqlSearchActionIT extends AbstractEqlBlockingIntegTestCase { private final ExecutorService executorService = Executors.newFixedThreadPool(1); - NamedWriteableRegistry registry = new NamedWriteableRegistry(new SearchModule(Settings.EMPTY, - Collections.emptyList()).getNamedWriteables()); + NamedWriteableRegistry registry = new NamedWriteableRegistry( + new SearchModule(Settings.EMPTY, Collections.emptyList()).getNamedWriteables() + ); /** * Shutdown the executor so we don't leak threads into other test runs. @@ -81,9 +82,13 @@ public void shutdownExec() { } private void prepareIndex() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test") - .setMapping("val", "type=integer", "event_type", "type=keyword", "@timestamp", "type=date", "i", "type=integer") - .get()); + assertAcked( + client().admin() + .indices() + .prepareCreate("test") + .setMapping("val", "type=integer", "event_type", "type=keyword", "@timestamp", "type=date", "i", "type=integer") + .get() + ); createIndex("idx_unmapped"); int numDocs = randomIntBetween(6, 20); @@ -92,13 +97,17 @@ private void prepareIndex() throws Exception { for (int i = 0; i < numDocs; i++) { int fieldValue = randomIntBetween(0, 10); - builders.add(client().prepareIndex("test").setSource( - jsonBuilder().startObject() - .field("val", fieldValue) - .field("event_type", "my_event") - .field("@timestamp", "2020-04-09T12:35:48Z") - .field("i", i) - .endObject())); + builders.add( + client().prepareIndex("test") + .setSource( + jsonBuilder().startObject() + .field("val", fieldValue) + .field("event_type", "my_event") + .field("@timestamp", "2020-04-09T12:35:48Z") + .field("i", i) + .endObject() + ) + ); } indexRandom(true, builders); } @@ -108,7 +117,9 @@ public void testBasicAsyncExecution() throws Exception { boolean success = randomBoolean(); String query = success ? "my_event where i==1" : "my_event where 10/i==1"; - EqlSearchRequest request = new EqlSearchRequest().indices("test").query(query).eventCategoryField("event_type") + EqlSearchRequest request = new EqlSearchRequest().indices("test") + .query(query) + .eventCategoryField("event_type") .waitForCompletionTimeout(TimeValue.timeValueMillis(1)); List plugins = initBlockFactory(true, false); @@ -125,8 +136,7 @@ public void testBasicAsyncExecution() throws Exception { if (randomBoolean()) { // let's timeout first - GetAsyncResultRequest getResultsRequest = new GetAsyncResultRequest(response.id()) - .setKeepAlive(TimeValue.timeValueMinutes(10)) + GetAsyncResultRequest getResultsRequest = new GetAsyncResultRequest(response.id()).setKeepAlive(TimeValue.timeValueMinutes(10)) .setWaitForCompletionTimeout(TimeValue.timeValueMillis(10)); EqlSearchResponse responseWithTimeout = client().execute(EqlAsyncGetResultAction.INSTANCE, getResultsRequest).get(); assertThat(responseWithTimeout.isRunning(), is(true)); @@ -135,8 +145,7 @@ public void testBasicAsyncExecution() throws Exception { } // Now we wait - GetAsyncResultRequest getResultsRequest = new GetAsyncResultRequest(response.id()) - .setKeepAlive(TimeValue.timeValueMinutes(10)) + GetAsyncResultRequest getResultsRequest = new GetAsyncResultRequest(response.id()).setKeepAlive(TimeValue.timeValueMinutes(10)) .setWaitForCompletionTimeout(TimeValue.timeValueSeconds(10)); ActionFuture future = client().execute(EqlAsyncGetResultAction.INSTANCE, getResultsRequest); disableBlocks(plugins); @@ -148,8 +157,10 @@ public void testBasicAsyncExecution() throws Exception { Exception ex = expectThrows(Exception.class, future::actionGet); assertThat(ex.getCause().getMessage(), containsString("by zero")); } - AcknowledgedResponse deleteResponse = - client().execute(DeleteAsyncResultAction.INSTANCE, new DeleteAsyncResultRequest(response.id())).actionGet(); + AcknowledgedResponse deleteResponse = client().execute( + DeleteAsyncResultAction.INSTANCE, + new DeleteAsyncResultRequest(response.id()) + ).actionGet(); assertThat(deleteResponse.isAcknowledged(), equalTo(true)); } @@ -158,7 +169,9 @@ public void testGoingAsync() throws Exception { boolean success = randomBoolean(); String query = success ? "my_event where i==1" : "my_event where 10/i==1"; - EqlSearchRequest request = new EqlSearchRequest().indices("test").query(query).eventCategoryField("event_type") + EqlSearchRequest request = new EqlSearchRequest().indices("test") + .query(query) + .eventCategoryField("event_type") .waitForCompletionTimeout(TimeValue.timeValueMillis(1)); boolean customKeepAlive = randomBoolean(); @@ -175,7 +188,8 @@ public void testGoingAsync() throws Exception { String opaqueId = randomAlphaOfLength(10); logger.trace("Starting async search"); EqlSearchResponse response = client().filterWithHeader(Collections.singletonMap(Task.X_OPAQUE_ID, opaqueId)) - .execute(EqlSearchAction.INSTANCE, request).get(); + .execute(EqlSearchAction.INSTANCE, request) + .get(); assertThat(response.isRunning(), is(true)); assertThat(response.isPartial(), is(true)); assertThat(response.id(), notNullValue()); @@ -193,8 +207,10 @@ public void testGoingAsync() throws Exception { assertBusy(() -> assertThat(findTaskWithXOpaqueId(opaqueId, EqlSearchAction.NAME + "[a]"), nullValue())); StoredAsyncResponse doc = getStoredRecord(id); // Make sure that the expiration time is not more than 1 min different from the current time + keep alive - assertThat(System.currentTimeMillis() + keepAliveValue.getMillis() - doc.getExpirationTime(), - lessThan(doc.getExpirationTime() + TimeValue.timeValueMinutes(1).getMillis())); + assertThat( + System.currentTimeMillis() + keepAliveValue.getMillis() - doc.getExpirationTime(), + lessThan(doc.getExpirationTime() + TimeValue.timeValueMinutes(1).getMillis()) + ); if (success) { assertThat(doc.getException(), nullValue()); assertThat(doc.getResponse(), notNullValue()); @@ -211,7 +227,9 @@ public void testAsyncCancellation() throws Exception { boolean success = randomBoolean(); String query = success ? "my_event where i==1" : "my_event where 10/i==1"; - EqlSearchRequest request = new EqlSearchRequest().indices("test").query(query).eventCategoryField("event_type") + EqlSearchRequest request = new EqlSearchRequest().indices("test") + .query(query) + .eventCategoryField("event_type") .waitForCompletionTimeout(TimeValue.timeValueMillis(1)); boolean customKeepAlive = randomBoolean(); @@ -226,7 +244,8 @@ public void testAsyncCancellation() throws Exception { String opaqueId = randomAlphaOfLength(10); logger.trace("Starting async search"); EqlSearchResponse response = client().filterWithHeader(Collections.singletonMap(Task.X_OPAQUE_ID, opaqueId)) - .execute(EqlSearchAction.INSTANCE, request).get(); + .execute(EqlSearchAction.INSTANCE, request) + .get(); assertThat(response.isRunning(), is(true)); assertThat(response.isPartial(), is(true)); assertThat(response.id(), notNullValue()); @@ -235,8 +254,10 @@ public void testAsyncCancellation() throws Exception { awaitForBlockedSearches(plugins, "test"); logger.trace("Block is established"); - ActionFuture deleteResponse = - client().execute(DeleteAsyncResultAction.INSTANCE, new DeleteAsyncResultRequest(response.id())); + ActionFuture deleteResponse = client().execute( + DeleteAsyncResultAction.INSTANCE, + new DeleteAsyncResultRequest(response.id()) + ); disableBlocks(plugins); assertThat(deleteResponse.actionGet().isAcknowledged(), equalTo(true)); @@ -250,7 +271,9 @@ public void testFinishingBeforeTimeout() throws Exception { boolean success = randomBoolean(); boolean keepOnCompletion = randomBoolean(); String query = success ? "my_event where i==1" : "my_event where 10/i==1"; - EqlSearchRequest request = new EqlSearchRequest().indices("test").query(query).eventCategoryField("event_type") + EqlSearchRequest request = new EqlSearchRequest().indices("test") + .query(query) + .eventCategoryField("event_type") .waitForCompletionTimeout(TimeValue.timeValueSeconds(10)); if (keepOnCompletion || randomBoolean()) { request.keepOnCompletion(keepOnCompletion); @@ -268,17 +291,20 @@ public void testFinishingBeforeTimeout() throws Exception { assertThat(doc.getException(), nullValue()); assertThat(doc.getResponse(), notNullValue()); assertThat(doc.getResponse().hits().events().size(), equalTo(1)); - EqlSearchResponse storedResponse = client().execute(EqlAsyncGetResultAction.INSTANCE, - new GetAsyncResultRequest(response.id())).actionGet(); + EqlSearchResponse storedResponse = client().execute( + EqlAsyncGetResultAction.INSTANCE, + new GetAsyncResultRequest(response.id()) + ).actionGet(); assertThat(storedResponse, equalTo(response)); - AcknowledgedResponse deleteResponse = - client().execute(DeleteAsyncResultAction.INSTANCE, new DeleteAsyncResultRequest(response.id())).actionGet(); + AcknowledgedResponse deleteResponse = client().execute( + DeleteAsyncResultAction.INSTANCE, + new DeleteAsyncResultRequest(response.id()) + ).actionGet(); assertThat(deleteResponse.isAcknowledged(), equalTo(true)); } } else { - Exception ex = expectThrows(Exception.class, - () -> client().execute(EqlSearchAction.INSTANCE, request).get()); + Exception ex = expectThrows(Exception.class, () -> client().execute(EqlSearchAction.INSTANCE, request).get()); assertThat(ex.getMessage(), containsString("by zero")); } } @@ -332,8 +358,11 @@ public static class FakePainlessScriptPlugin extends MockScriptPlugin { @Override protected Map, Object>> pluginScripts() { Map, Object>> scripts = new HashMap<>(); - scripts.put("InternalEqlScriptUtils.multiValueDocValues(doc,params.v0,X0 -> InternalQlScriptUtils.nullSafeFilter(" - + "InternalQlScriptUtils.eq(InternalQlScriptUtils.div(params.v1,X0),params.v2)))", FakePainlessScriptPlugin::fail); + scripts.put( + "InternalEqlScriptUtils.multiValueDocValues(doc,params.v0,X0 -> InternalQlScriptUtils.nullSafeFilter(" + + "InternalQlScriptUtils.eq(InternalQlScriptUtils.div(params.v1,X0),params.v2)))", + FakePainlessScriptPlugin::fail + ); return scripts; } diff --git a/x-pack/plugin/eql/src/internalClusterTest/java/org/elasticsearch/xpack/eql/action/EqlCancellationIT.java b/x-pack/plugin/eql/src/internalClusterTest/java/org/elasticsearch/xpack/eql/action/EqlCancellationIT.java index 7eac83d912ce8..755e3cffbf53a 100644 --- a/x-pack/plugin/eql/src/internalClusterTest/java/org/elasticsearch/xpack/eql/action/EqlCancellationIT.java +++ b/x-pack/plugin/eql/src/internalClusterTest/java/org/elasticsearch/xpack/eql/action/EqlCancellationIT.java @@ -21,8 +21,8 @@ import java.util.concurrent.Executors; import java.util.concurrent.Future; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.instanceOf; @@ -39,9 +39,13 @@ public void shutdownExec() { } public void testCancellation() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test") - .setMapping("val", "type=integer", "event_type", "type=keyword", "@timestamp", "type=date") - .get()); + assertAcked( + client().admin() + .indices() + .prepareCreate("test") + .setMapping("val", "type=integer", "event_type", "type=keyword", "@timestamp", "type=date") + .get() + ); createIndex("idx_unmapped"); int numDocs = randomIntBetween(6, 20); @@ -50,10 +54,16 @@ public void testCancellation() throws Exception { for (int i = 0; i < numDocs; i++) { int fieldValue = randomIntBetween(0, 10); - builders.add(client().prepareIndex("test").setSource( - jsonBuilder().startObject() - .field("val", fieldValue).field("event_type", "my_event").field("@timestamp", "2020-04-09T12:35:48Z") - .endObject())); + builders.add( + client().prepareIndex("test") + .setSource( + jsonBuilder().startObject() + .field("val", fieldValue) + .field("event_type", "my_event") + .field("@timestamp", "2020-04-09T12:35:48Z") + .endObject() + ) + ); } indexRandom(true, builders); @@ -63,8 +73,8 @@ public void testCancellation() throws Exception { String id = randomAlphaOfLength(10); logger.trace("Preparing search"); // We might perform field caps on the same thread if it is local client, so we cannot use the standard mechanism - Future future = executorService.submit(() -> - client().filterWithHeader(Collections.singletonMap(Task.X_OPAQUE_ID, id)).execute(EqlSearchAction.INSTANCE, request).get() + Future future = executorService.submit( + () -> client().filterWithHeader(Collections.singletonMap(Task.X_OPAQUE_ID, id)).execute(EqlSearchAction.INSTANCE, request).get() ); logger.trace("Waiting for block to be established"); if (cancelDuringSearch) { diff --git a/x-pack/plugin/eql/src/internalClusterTest/java/org/elasticsearch/xpack/eql/action/RestEqlCancellationIT.java b/x-pack/plugin/eql/src/internalClusterTest/java/org/elasticsearch/xpack/eql/action/RestEqlCancellationIT.java index b9cde24239a19..0e216f9f8d334 100644 --- a/x-pack/plugin/eql/src/internalClusterTest/java/org/elasticsearch/xpack/eql/action/RestEqlCancellationIT.java +++ b/x-pack/plugin/eql/src/internalClusterTest/java/org/elasticsearch/xpack/eql/action/RestEqlCancellationIT.java @@ -19,8 +19,8 @@ import org.elasticsearch.plugins.Plugin; import org.elasticsearch.tasks.Task; import org.elasticsearch.tasks.TaskInfo; -import org.elasticsearch.transport.netty4.Netty4Plugin; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.transport.netty4.Netty4Plugin; import org.elasticsearch.transport.nio.NioTransportPlugin; import org.junit.BeforeClass; @@ -30,8 +30,8 @@ import java.util.concurrent.CancellationException; import static org.elasticsearch.action.support.ActionTestUtils.wrapAsRestResponseListener; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.notNullValue; @@ -55,7 +55,8 @@ protected boolean addMockHttpTransport() { protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { return Settings.builder() .put(super.nodeSettings(nodeOrdinal, otherSettings)) - .put(NetworkModule.HTTP_TYPE_KEY, nodeHttpTypeKey).build(); + .put(NetworkModule.HTTP_TYPE_KEY, nodeHttpTypeKey) + .build(); } private static String getHttpTypeKey(Class clazz) { @@ -77,9 +78,13 @@ protected Collection> nodePlugins() { } public void testRestCancellation() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test") - .setMapping("val", "type=integer", "event_type", "type=keyword", "@timestamp", "type=date") - .get()); + assertAcked( + client().admin() + .indices() + .prepareCreate("test") + .setMapping("val", "type=integer", "event_type", "type=keyword", "@timestamp", "type=date") + .get() + ); createIndex("idx_unmapped"); int numDocs = randomIntBetween(6, 20); @@ -88,18 +93,26 @@ public void testRestCancellation() throws Exception { for (int i = 0; i < numDocs; i++) { int fieldValue = randomIntBetween(0, 10); - builders.add(client().prepareIndex("test").setSource( - jsonBuilder().startObject() - .field("val", fieldValue).field("event_type", "my_event").field("@timestamp", "2020-04-09T12:35:48Z") - .endObject())); + builders.add( + client().prepareIndex("test") + .setSource( + jsonBuilder().startObject() + .field("val", fieldValue) + .field("event_type", "my_event") + .field("@timestamp", "2020-04-09T12:35:48Z") + .endObject() + ) + ); } indexRandom(true, builders); // We are cancelling during both mapping and searching but we cancel during mapping so we should never reach the second block List plugins = initBlockFactory(true, true); - org.elasticsearch.client.eql.EqlSearchRequest eqlSearchRequest = - new org.elasticsearch.client.eql.EqlSearchRequest("test", "my_event where val==1").eventCategoryField("event_type"); + org.elasticsearch.client.eql.EqlSearchRequest eqlSearchRequest = new org.elasticsearch.client.eql.EqlSearchRequest( + "test", + "my_event where val==1" + ).eventCategoryField("event_type"); String id = randomAlphaOfLength(10); Request request = new Request("GET", "/test/_eql/search"); @@ -137,9 +150,7 @@ public void testRestCancellation() throws Exception { logger.trace("Disabling field cap blocks"); disableFieldCapBlocks(plugins); // The task should be cancelled before ever reaching search blocks - assertBusy(() -> { - assertThat(getTaskInfoWithXOpaqueId(id, EqlSearchAction.NAME), nullValue()); - }); + assertBusy(() -> { assertThat(getTaskInfoWithXOpaqueId(id, EqlSearchAction.NAME), nullValue()); }); // Make sure it didn't reach search blocks assertThat(getNumberOfContexts(plugins), equalTo(0)); disableSearchBlocks(plugins); diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/EqlUsageTransportAction.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/EqlUsageTransportAction.java index d3e48c72d4f5f..84e590fc145ce 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/EqlUsageTransportAction.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/EqlUsageTransportAction.java @@ -35,17 +35,25 @@ public class EqlUsageTransportAction extends XPackUsageFeatureTransportAction { private final Client client; @Inject - public EqlUsageTransportAction(TransportService transportService, ClusterService clusterService, ThreadPool threadPool, - ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver, - Client client) { - super(XPackUsageFeatureAction.EQL.name(), transportService, clusterService, threadPool, actionFilters, - indexNameExpressionResolver); + public EqlUsageTransportAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver, + Client client + ) { + super(XPackUsageFeatureAction.EQL.name(), transportService, clusterService, threadPool, actionFilters, indexNameExpressionResolver); this.client = client; } @Override - protected void masterOperation(Task task, XPackUsageRequest request, ClusterState state, - ActionListener listener) { + protected void masterOperation( + Task task, + XPackUsageRequest request, + ClusterState state, + ActionListener listener + ) { EqlStatsRequest eqlRequest = new EqlStatsRequest(); eqlRequest.includeStats(true); diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/action/EqlSearchRequest.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/action/EqlSearchRequest.java index 92826a800554d..7dc4d94d3e6a3 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/action/EqlSearchRequest.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/action/EqlSearchRequest.java @@ -11,23 +11,23 @@ import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.IndicesRequest; import org.elasticsearch.action.support.IndicesOptions; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.ObjectParser; -import org.elasticsearch.xcontent.ObjectParser.ValueType; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentParser.Token; import org.elasticsearch.index.query.AbstractQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.fetch.subphase.FieldAndFormat; import org.elasticsearch.tasks.Task; import org.elasticsearch.tasks.TaskId; +import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentParser.Token; import java.io.IOException; import java.util.ArrayList; @@ -172,9 +172,11 @@ public ActionRequestValidationException validate() { validationException = addValidationError("fetch size must be greater than 1", validationException); } - if (keepAlive != null && keepAlive.getMillis() < MIN_KEEP_ALIVE) { - validationException = - addValidationError("[keep_alive] must be greater than 1 minute, got:" + keepAlive.toString(), validationException); + if (keepAlive != null && keepAlive.getMillis() < MIN_KEEP_ALIVE) { + validationException = addValidationError( + "[keep_alive] must be greater than 1 minute, got:" + keepAlive.toString(), + validationException + ); } if (runtimeMappings != null) { @@ -184,8 +186,10 @@ public ActionRequestValidationException validate() { return validationException; } - private static ActionRequestValidationException validateRuntimeMappings(Map runtimeMappings, - ActionRequestValidationException validationException) { + private static ActionRequestValidationException validateRuntimeMappings( + Map runtimeMappings, + ActionRequestValidationException validationException + ) { for (Map.Entry entry : runtimeMappings.entrySet()) { // top level objects are fields String fieldName = entry.getKey(); @@ -196,8 +200,10 @@ private static ActionRequestValidationException validateRuntimeMappings(Map ObjectParser objectParser(Supplier supplier) { ObjectParser parser = new ObjectParser<>("eql/search", false, supplier); - parser.declareObject(EqlSearchRequest::filter, - (p, c) -> AbstractQueryBuilder.parseInnerQueryBuilder(p), FILTER); + parser.declareObject(EqlSearchRequest::filter, (p, c) -> AbstractQueryBuilder.parseInnerQueryBuilder(p), FILTER); parser.declareString(EqlSearchRequest::timestampField, TIMESTAMP_FIELD); parser.declareString(EqlSearchRequest::tiebreakerField, TIEBREAKER_FIELD); parser.declareString(EqlSearchRequest::eventCategoryField, EVENT_CATEGORY_FIELD); parser.declareInt(EqlSearchRequest::size, SIZE); parser.declareInt(EqlSearchRequest::fetchSize, FETCH_SIZE); parser.declareString(EqlSearchRequest::query, QUERY); - parser.declareField(EqlSearchRequest::waitForCompletionTimeout, - (p, c) -> TimeValue.parseTimeValue(p.text(), KEY_WAIT_FOR_COMPLETION_TIMEOUT), WAIT_FOR_COMPLETION_TIMEOUT, - ObjectParser.ValueType.VALUE); - parser.declareField(EqlSearchRequest::keepAlive, - (p, c) -> TimeValue.parseTimeValue(p.text(), KEY_KEEP_ALIVE), KEEP_ALIVE, ObjectParser.ValueType.VALUE); + parser.declareField( + EqlSearchRequest::waitForCompletionTimeout, + (p, c) -> TimeValue.parseTimeValue(p.text(), KEY_WAIT_FOR_COMPLETION_TIMEOUT), + WAIT_FOR_COMPLETION_TIMEOUT, + ObjectParser.ValueType.VALUE + ); + parser.declareField( + EqlSearchRequest::keepAlive, + (p, c) -> TimeValue.parseTimeValue(p.text(), KEY_KEEP_ALIVE), + KEEP_ALIVE, + ObjectParser.ValueType.VALUE + ); parser.declareBoolean(EqlSearchRequest::keepOnCompletion, KEEP_ON_COMPLETION); parser.declareString(EqlSearchRequest::resultPosition, RESULT_POSITION); parser.declareField(EqlSearchRequest::fetchFields, EqlSearchRequest::parseFetchFields, FETCH_FIELDS_FIELD, ValueType.VALUE_ARRAY); @@ -266,35 +278,45 @@ public EqlSearchRequest indices(String... indices) { return this; } - public QueryBuilder filter() { return this.filter; } + public QueryBuilder filter() { + return this.filter; + } public EqlSearchRequest filter(QueryBuilder filter) { this.filter = filter; return this; } - public Map runtimeMappings() { return this.runtimeMappings; } + public Map runtimeMappings() { + return this.runtimeMappings; + } public EqlSearchRequest runtimeMappings(Map runtimeMappings) { this.runtimeMappings = runtimeMappings; return this; } - public String timestampField() { return this.timestampField; } + public String timestampField() { + return this.timestampField; + } public EqlSearchRequest timestampField(String timestampField) { this.timestampField = timestampField; return this; } - public String tiebreakerField() { return this.tiebreakerField; } + public String tiebreakerField() { + return this.tiebreakerField; + } public EqlSearchRequest tiebreakerField(String tiebreakerField) { this.tiebreakerField = tiebreakerField; return this; } - public String eventCategoryField() { return this.eventCategoryField; } + public String eventCategoryField() { + return this.eventCategoryField; + } public EqlSearchRequest eventCategoryField(String eventCategoryField) { this.eventCategoryField = eventCategoryField; @@ -319,7 +341,9 @@ public EqlSearchRequest fetchSize(int fetchSize) { return this; } - public String query() { return this.query; } + public String query() { + return this.query; + } public EqlSearchRequest query(String query) { this.query = query; @@ -438,24 +462,23 @@ public boolean equals(Object o) { return false; } EqlSearchRequest that = (EqlSearchRequest) o; - return size == that.size && - fetchSize == that.fetchSize && - Arrays.equals(indices, that.indices) && - Objects.equals(indicesOptions, that.indicesOptions) && - Objects.equals(filter, that.filter) && - Objects.equals(timestampField, that.timestampField) && - Objects.equals(tiebreakerField, that.tiebreakerField) && - Objects.equals(eventCategoryField, that.eventCategoryField) && - Objects.equals(query, that.query) && - Objects.equals(ccsMinimizeRoundtrips, that.ccsMinimizeRoundtrips) && - Objects.equals(waitForCompletionTimeout, that.waitForCompletionTimeout) && - Objects.equals(keepAlive, that.keepAlive) && - Objects.equals(resultPosition, that.resultPosition) && - Objects.equals(fetchFields, that.fetchFields) && - Objects.equals(runtimeMappings, that.runtimeMappings); + return size == that.size + && fetchSize == that.fetchSize + && Arrays.equals(indices, that.indices) + && Objects.equals(indicesOptions, that.indicesOptions) + && Objects.equals(filter, that.filter) + && Objects.equals(timestampField, that.timestampField) + && Objects.equals(tiebreakerField, that.tiebreakerField) + && Objects.equals(eventCategoryField, that.eventCategoryField) + && Objects.equals(query, that.query) + && Objects.equals(ccsMinimizeRoundtrips, that.ccsMinimizeRoundtrips) + && Objects.equals(waitForCompletionTimeout, that.waitForCompletionTimeout) + && Objects.equals(keepAlive, that.keepAlive) + && Objects.equals(resultPosition, that.resultPosition) + && Objects.equals(fetchFields, that.fetchFields) + && Objects.equals(runtimeMappings, that.runtimeMappings); } - @Override public int hashCode() { return Objects.hash( @@ -473,7 +496,8 @@ public int hashCode() { keepAlive, resultPosition, fetchFields, - runtimeMappings); + runtimeMappings + ); } @Override diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/action/EqlSearchResponse.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/action/EqlSearchResponse.java index 1078c2abc09a7..cedcb20bcd205 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/action/EqlSearchResponse.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/action/EqlSearchResponse.java @@ -9,8 +9,6 @@ import org.apache.lucene.search.TotalHits; import org.elasticsearch.Version; import org.elasticsearch.action.ActionResponse; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.document.DocumentField; @@ -18,18 +16,20 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.lucene.Lucene; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.common.xcontent.XContentParserUtils; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.index.get.GetResult; +import org.elasticsearch.index.mapper.SourceFieldMapper; +import org.elasticsearch.search.SearchHits; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.InstantiatingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentFragment; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.XContentParserUtils; -import org.elasticsearch.index.get.GetResult; -import org.elasticsearch.index.mapper.SourceFieldMapper; -import org.elasticsearch.search.SearchHits; import org.elasticsearch.xpack.ql.async.QlStatusResponse; import java.io.IOException; @@ -51,7 +51,6 @@ public class EqlSearchResponse extends ActionResponse implements ToXContentObjec private final boolean isRunning; private final boolean isPartial; - private static final class Fields { static final String TOOK = "took"; static final String TIMED_OUT = "timed_out"; @@ -70,8 +69,11 @@ private static final class Fields { private static final InstantiatingObjectParser PARSER; static { - InstantiatingObjectParser.Builder parser = - InstantiatingObjectParser.builder("eql/search_response", true, EqlSearchResponse.class); + InstantiatingObjectParser.Builder parser = InstantiatingObjectParser.builder( + "eql/search_response", + true, + EqlSearchResponse.class + ); parser.declareObject(constructorArg(), (p, c) -> Hits.fromXContent(p), HITS); parser.declareLong(constructorArg(), TOOK); parser.declareBoolean(constructorArg(), TIMED_OUT); @@ -85,8 +87,14 @@ public EqlSearchResponse(Hits hits, long tookInMillis, boolean isTimeout) { this(hits, tookInMillis, isTimeout, null, false, false); } - public EqlSearchResponse(Hits hits, long tookInMillis, boolean isTimeout, String asyncExecutionId, - boolean isRunning, boolean isPartial) { + public EqlSearchResponse( + Hits hits, + long tookInMillis, + boolean isTimeout, + String asyncExecutionId, + boolean isRunning, + boolean isPartial + ) { super(); this.hits = hits == null ? Hits.EMPTY : hits; this.tookInMillis = tookInMillis; @@ -207,9 +215,11 @@ private static final class Fields { private static final ParseField FIELDS = new ParseField(Fields.FIELDS); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("eql/search_response_event", true, - args -> new Event((String) args[0], (String) args[1], (BytesReference) args[2], (Map) args[3])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "eql/search_response_event", + true, + args -> new Event((String) args[0], (String) args[1], (BytesReference) args[2], (Map) args[3]) + ); static { PARSER.declareString(constructorArg(), INDEX); @@ -274,7 +284,8 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws // We have to use the deprecated version since we don't know the content type of the original source XContentHelper.writeRawField(Fields.SOURCE, source, builder, params); // ignore fields all together if they are all empty - if (fetchFields != null && fetchFields.isEmpty() == false + if (fetchFields != null + && fetchFields.isEmpty() == false && fetchFields.values().stream().anyMatch(df -> df.getValues().size() > 0)) { builder.startObject(Fields.FIELDS); for (DocumentField field : fetchFields.values()) { @@ -329,9 +340,9 @@ public boolean equals(Object obj) { EqlSearchResponse.Event other = (EqlSearchResponse.Event) obj; return Objects.equals(index, other.index) - && Objects.equals(id, other.id) - && Objects.equals(source, other.source) - && Objects.equals(fetchFields, other.fetchFields); + && Objects.equals(id, other.id) + && Objects.equals(source, other.source) + && Objects.equals(fetchFields, other.fetchFields); } @Override @@ -350,18 +361,26 @@ private static final class Fields { private static final ParseField JOIN_KEYS = new ParseField(Fields.JOIN_KEYS); private static final ParseField EVENTS = new ParseField(Fields.EVENTS); - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("eql/search_response_sequence", true, - args -> { - int i = 0; - @SuppressWarnings("unchecked") List joinKeys = (List) args[i++]; - @SuppressWarnings("unchecked") List events = (List) args[i]; - return new EqlSearchResponse.Sequence(joinKeys, events); - }); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "eql/search_response_sequence", + true, + args -> { + int i = 0; + @SuppressWarnings("unchecked") + List joinKeys = (List) args[i++]; + @SuppressWarnings("unchecked") + List events = (List) args[i]; + return new EqlSearchResponse.Sequence(joinKeys, events); + } + ); static { - PARSER.declareFieldArray(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> XContentParserUtils.parseFieldsValue(p), - JOIN_KEYS, ObjectParser.ValueType.VALUE_ARRAY); + PARSER.declareFieldArray( + ConstructingObjectParser.optionalConstructorArg(), + (p, c) -> XContentParserUtils.parseFieldsValue(p), + JOIN_KEYS, + ObjectParser.ValueType.VALUE_ARRAY + ); PARSER.declareObjectArray(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> Event.fromXContent(p), EVENTS); } @@ -415,8 +434,7 @@ public boolean equals(Object o) { return false; } Sequence that = (Sequence) o; - return Objects.equals(joinKeys, that.joinKeys) - && Objects.equals(events, that.events); + return Objects.equals(joinKeys, that.joinKeys) && Objects.equals(events, that.events); } @Override @@ -453,7 +471,6 @@ public Hits(@Nullable List events, @Nullable List sequences, @N this.totalHits = totalHits; } - public Hits(StreamInput in) throws IOException { if (in.readBoolean()) { totalHits = Lucene.readTotalHits(in); @@ -485,23 +502,32 @@ public void writeTo(StreamOutput out) throws IOException { } } - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("eql/search_response_hits", true, - args -> { - int i = 0; - @SuppressWarnings("unchecked") List events = (List) args[i++]; - @SuppressWarnings("unchecked") List sequences = (List) args[i++]; - TotalHits totalHits = (TotalHits) args[i]; - return new EqlSearchResponse.Hits(events, sequences, totalHits); - }); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "eql/search_response_hits", + true, + args -> { + int i = 0; + @SuppressWarnings("unchecked") + List events = (List) args[i++]; + @SuppressWarnings("unchecked") + List sequences = (List) args[i++]; + TotalHits totalHits = (TotalHits) args[i]; + return new EqlSearchResponse.Hits(events, sequences, totalHits); + } + ); static { - PARSER.declareObjectArray(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> Event.fromXContent(p), - new ParseField(Fields.EVENTS)); - PARSER.declareObjectArray(ConstructingObjectParser.optionalConstructorArg(), Sequence.PARSER, - new ParseField(Fields.SEQUENCES)); - PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> SearchHits.parseTotalHitsFragment(p), - new ParseField(Fields.TOTAL)); + PARSER.declareObjectArray( + ConstructingObjectParser.optionalConstructorArg(), + (p, c) -> Event.fromXContent(p), + new ParseField(Fields.EVENTS) + ); + PARSER.declareObjectArray(ConstructingObjectParser.optionalConstructorArg(), Sequence.PARSER, new ParseField(Fields.SEQUENCES)); + PARSER.declareObject( + ConstructingObjectParser.optionalConstructorArg(), + (p, c) -> SearchHits.parseTotalHitsFragment(p), + new ParseField(Fields.TOTAL) + ); } public static Hits fromXContent(XContentParser parser) throws IOException { diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/action/EqlSearchTask.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/action/EqlSearchTask.java index 41c715c950c32..2a1bc3b7adb67 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/action/EqlSearchTask.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/action/EqlSearchTask.java @@ -15,8 +15,17 @@ import java.util.Map; public class EqlSearchTask extends StoredAsyncTask { - public EqlSearchTask(long id, String type, String action, String description, TaskId parentTaskId, Map headers, - Map originHeaders, AsyncExecutionId asyncExecutionId, TimeValue keepAlive) { + public EqlSearchTask( + long id, + String type, + String action, + String description, + TaskId parentTaskId, + Map headers, + Map originHeaders, + AsyncExecutionId asyncExecutionId, + TimeValue keepAlive + ) { super(id, type, action, description, parentTaskId, headers, originHeaders, asyncExecutionId, keepAlive); } @@ -24,7 +33,13 @@ public EqlSearchTask(long id, String type, String action, String description, Ta public EqlSearchResponse getCurrentResult() { // for eql searches we never store a search response in the task (neither partial, nor final) // we kill the task on final response, so if the task is still present, it means the search is still running - return new EqlSearchResponse(EqlSearchResponse.Hits.EMPTY, System.currentTimeMillis() - getStartTime(), false, - getExecutionId().getEncoded(), true, true); + return new EqlSearchResponse( + EqlSearchResponse.Hits.EMPTY, + System.currentTimeMillis() - getStartTime(), + false, + getExecutionId().getEncoded(), + true, + true + ); } } diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/analysis/AnalysisUtils.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/analysis/AnalysisUtils.java index 3dd82bf0c250a..cbd288b829a60 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/analysis/AnalysisUtils.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/analysis/AnalysisUtils.java @@ -45,9 +45,9 @@ static Attribute resolveAgainstList(UnresolvedAttribute u, Collection boolean match = qualified ? Objects.equals(u.qualifiedName(), attribute.qualifiedName()) : // if the field is unqualified // first check the names directly - (Objects.equals(u.name(), attribute.name()) - // but also if the qualifier might not be quoted and if there's any ambiguity with nested fields - || Objects.equals(u.name(), attribute.qualifiedName())); + (Objects.equals(u.name(), attribute.name()) + // but also if the qualifier might not be quoted and if there's any ambiguity with nested fields + || Objects.equals(u.name(), attribute.qualifiedName())); if (match) { matches.add(attribute.withLocation(u.source())); } @@ -64,8 +64,11 @@ static Attribute resolveAgainstList(UnresolvedAttribute u, Collection } return u.withUnresolvedMessage( - "Reference [" + u.qualifiedName() + "] is ambiguous (to disambiguate use quotes or qualifiers); matches any of " - + matches.stream().map(a -> "\"" + a.qualifier() + "\".\"" + a.name() + "\"").sorted().collect(toList())); + "Reference [" + + u.qualifiedName() + + "] is ambiguous (to disambiguate use quotes or qualifiers); matches any of " + + matches.stream().map(a -> "\"" + a.qualifier() + "\".\"" + a.name() + "\"").sorted().collect(toList()) + ); } private static Attribute handleSpecialFields(UnresolvedAttribute u, Attribute named, boolean allowCompound) { @@ -75,34 +78,59 @@ private static Attribute handleSpecialFields(UnresolvedAttribute u, Attribute na // incompatible mappings if (fa.field() instanceof InvalidMappedField) { - named = u.withUnresolvedMessage("Cannot use field [" + fa.name() + "] due to ambiguities being " - + ((InvalidMappedField) fa.field()).errorMessage()); + named = u.withUnresolvedMessage( + "Cannot use field [" + fa.name() + "] due to ambiguities being " + ((InvalidMappedField) fa.field()).errorMessage() + ); } // unsupported types else if (DataTypes.isUnsupported(fa.dataType())) { UnsupportedEsField unsupportedField = (UnsupportedEsField) fa.field(); if (unsupportedField.hasInherited()) { - named = u.withUnresolvedMessage("Cannot use field [" + fa.name() + "] with unsupported type [" - + unsupportedField.getOriginalType() + "] " + "in hierarchy (field [" + unsupportedField.getInherited() + "])"); + named = u.withUnresolvedMessage( + "Cannot use field [" + + fa.name() + + "] with unsupported type [" + + unsupportedField.getOriginalType() + + "] " + + "in hierarchy (field [" + + unsupportedField.getInherited() + + "])" + ); } else { named = u.withUnresolvedMessage( - "Cannot use field [" + fa.name() + "] with unsupported type [" + unsupportedField.getOriginalType() + "]"); + "Cannot use field [" + fa.name() + "] with unsupported type [" + unsupportedField.getOriginalType() + "]" + ); } } // compound fields that are not of "nested" type else if (allowCompound == false && DataTypes.isPrimitive(fa.dataType()) == false && fa.dataType() != DataTypes.NESTED) { named = u.withUnresolvedMessage( - "Cannot use field [" + fa.name() + "] type [" + fa.dataType().typeName() + "] only its subfields"); + "Cannot use field [" + fa.name() + "] type [" + fa.dataType().typeName() + "] only its subfields" + ); } // "nested" fields else if (fa.dataType() == DataTypes.NESTED) { - named = u.withUnresolvedMessage("Cannot use field [" + fa.name() + "] type [" + fa.dataType().typeName() + "] " - + "due to nested fields not being supported yet"); + named = u.withUnresolvedMessage( + "Cannot use field [" + + fa.name() + + "] type [" + + fa.dataType().typeName() + + "] " + + "due to nested fields not being supported yet" + ); } // fields having nested parents else if (fa.isNested()) { - named = u.withUnresolvedMessage("Cannot use field [" + fa.name() + "] type [" + fa.dataType().typeName() + "] " - + "with unsupported nested type in hierarchy (field [" + fa.nestedParent().name() +"])"); + named = u.withUnresolvedMessage( + "Cannot use field [" + + fa.name() + + "] type [" + + fa.dataType().typeName() + + "] " + + "with unsupported nested type in hierarchy (field [" + + fa.nestedParent().name() + + "])" + ); } // remember if the field was optional - needed when generating the runtime query else if (u instanceof OptionalUnresolvedAttribute) { diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/analysis/Analyzer.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/analysis/Analyzer.java index d02d4442466af..0d9ae3b13cf23 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/analysis/Analyzer.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/analysis/Analyzer.java @@ -48,14 +48,9 @@ public Analyzer(Configuration configuration, FunctionRegistry functionRegistry, protected Iterable.Batch> batches() { Batch optional = new Batch("Optional", Limiter.ONCE, new ResolveOrReplaceOptionalRefs()); - Batch resolution = new Batch("Resolution", - new ResolveRefs(), - new ResolveFunctions() - ); - - Batch cleanup = new Batch("Finish Analysis", Limiter.ONCE, - new AddMissingEqualsToBoolField() - ); + Batch resolution = new Batch("Resolution", new ResolveRefs(), new ResolveFunctions()); + + Batch cleanup = new Batch("Finish Analysis", Limiter.ONCE, new AddMissingEqualsToBoolField()); return asList(optional, resolution, cleanup); } diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/analysis/PostAnalyzer.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/analysis/PostAnalyzer.java index 135774dcf6919..7ab275de20dda 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/analysis/PostAnalyzer.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/analysis/PostAnalyzer.java @@ -53,9 +53,11 @@ public LogicalPlan postAnalyze(LogicalPlan plan, EqlConfiguration configuration) Project p = new Project(projectCtx, k.child(), k.extractionAttributes()); // TODO: this could be incorporated into the query generation - LogicalPlan fetchSize = new LimitWithOffset(synthetic(""), + LogicalPlan fetchSize = new LimitWithOffset( + synthetic(""), new Literal(synthetic(""), configuration.fetchSize(), DataTypes.INTEGER), - p); + p + ); return new KeyedFilter(k.source(), fetchSize, k.keys(), k.timestamp(), k.tiebreaker()); }); diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/analysis/Verifier.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/analysis/Verifier.java index 2f5936bad1906..40b7741389916 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/analysis/Verifier.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/analysis/Verifier.java @@ -184,14 +184,18 @@ Collection verify(LogicalPlan plan, Function int queriesCount = s.queries().size(); switch (queriesCount) { - case 2: b.set(SEQUENCE_QUERIES_TWO.ordinal()); - break; - case 3: b.set(SEQUENCE_QUERIES_THREE.ordinal()); - break; - case 4: b.set(SEQUENCE_QUERIES_FOUR.ordinal()); - break; - default: b.set(SEQUENCE_QUERIES_FIVE_OR_MORE.ordinal()); - break; + case 2: + b.set(SEQUENCE_QUERIES_TWO.ordinal()); + break; + case 3: + b.set(SEQUENCE_QUERIES_THREE.ordinal()); + break; + case 4: + b.set(SEQUENCE_QUERIES_FOUR.ordinal()); + break; + default: + b.set(SEQUENCE_QUERIES_FIVE_OR_MORE.ordinal()); + break; } if (j.until().keys().isEmpty() == false) { b.set(SEQUENCE_UNTIL.ordinal()); @@ -200,14 +204,18 @@ Collection verify(LogicalPlan plan, Function b.set(FeatureMetric.JOIN.ordinal()); int queriesCount = j.queries().size(); switch (queriesCount) { - case 2: b.set(JOIN_QUERIES_TWO.ordinal()); - break; - case 3: b.set(JOIN_QUERIES_THREE.ordinal()); - break; - case 4: b.set(JOIN_QUERIES_FOUR.ordinal()); - break; - default: b.set(JOIN_QUERIES_FIVE_OR_MORE.ordinal()); - break; + case 2: + b.set(JOIN_QUERIES_TWO.ordinal()); + break; + case 3: + b.set(JOIN_QUERIES_THREE.ordinal()); + break; + case 4: + b.set(JOIN_QUERIES_FOUR.ordinal()); + break; + default: + b.set(JOIN_QUERIES_FIVE_OR_MORE.ordinal()); + break; } if (j.until().keys().isEmpty() == false) { b.set(JOIN_UNTIL.ordinal()); @@ -216,18 +224,23 @@ Collection verify(LogicalPlan plan, Function int joinKeysCount = j.queries().get(0).keys().size(); switch (joinKeysCount) { - case 1: b.set(JOIN_KEYS_ONE.ordinal()); - break; - case 2: b.set(JOIN_KEYS_TWO.ordinal()); - break; - case 3: b.set(JOIN_KEYS_THREE.ordinal()); - break; - case 4: b.set(JOIN_KEYS_FOUR.ordinal()); - break; - default: if (joinKeysCount >= 5) { - b.set(JOIN_KEYS_FIVE_OR_MORE.ordinal()); - } - break; + case 1: + b.set(JOIN_KEYS_ONE.ordinal()); + break; + case 2: + b.set(JOIN_KEYS_TWO.ordinal()); + break; + case 3: + b.set(JOIN_KEYS_THREE.ordinal()); + break; + case 4: + b.set(JOIN_KEYS_FOUR.ordinal()); + break; + default: + if (joinKeysCount >= 5) { + b.set(JOIN_KEYS_FIVE_OR_MORE.ordinal()); + } + break; } } }); @@ -267,23 +280,38 @@ private void checkJoinKeyTypes(LogicalPlan plan, Set localFailures) { private static void doCheckKeyTypes(Join join, Set localFailures, NamedExpression expectedKey, NamedExpression currentKey) { if (DataTypes.areCompatible(expectedKey.dataType(), currentKey.dataType()) == false) { - localFailures.add(fail(currentKey, "{} key [{}] type [{}] is incompatible with key [{}] type [{}]", - join.nodeName(), - currentKey.name(), currentKey.dataType().esType(), - expectedKey.name(), expectedKey.dataType().esType() - )); + localFailures.add( + fail( + currentKey, + "{} key [{}] type [{}] is incompatible with key [{}] type [{}]", + join.nodeName(), + currentKey.name(), + currentKey.dataType().esType(), + expectedKey.name(), + expectedKey.dataType().esType() + ) + ); } } - private void checkRemoteClusterOnSameVersion(LogicalPlan plan, Function> versionIncompatibleClusters, - Collection localFailures) { + private void checkRemoteClusterOnSameVersion( + LogicalPlan plan, + Function> versionIncompatibleClusters, + Collection localFailures + ) { if (plan instanceof EsRelation) { EsRelation esRelation = (EsRelation) plan; Collection incompatibleClusters = versionIncompatibleClusters.apply(esRelation.index().name()); if (incompatibleClusters.size() > 0) { - localFailures.add(fail(esRelation, "the following remote cluster{} incompatible, being on a version different than local " - + "cluster's [{}]: {}", incompatibleClusters.size() > 1 ? "s are" : " is", Version.CURRENT, - incompatibleClusters)); + localFailures.add( + fail( + esRelation, + "the following remote cluster{} incompatible, being on a version different than local " + "cluster's [{}]: {}", + incompatibleClusters.size() > 1 ? "s are" : " is", + Version.CURRENT, + incompatibleClusters + ) + ); } } } diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/PlanExecutor.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/PlanExecutor.java index 3b7e59b646fcc..88a223b829faf 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/PlanExecutor.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/PlanExecutor.java @@ -42,7 +42,6 @@ public class PlanExecutor { private final Metrics metrics; - public PlanExecutor(Client client, IndexResolver indexResolver, CircuitBreaker circuitBreaker) { this.client = client; this.indexResolver = indexResolver; diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/assembler/Criterion.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/assembler/Criterion.java index ffadccafc3755..53779d6382897 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/assembler/Criterion.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/assembler/Criterion.java @@ -28,13 +28,15 @@ public class Criterion { private final boolean descending; private final int keySize; - public Criterion(int stage, - Q queryRequest, - List keys, - HitExtractor timestamp, - HitExtractor tiebreaker, - HitExtractor implicitTiebreaker, - boolean descending) { + public Criterion( + int stage, + Q queryRequest, + List keys, + HitExtractor timestamp, + HitExtractor tiebreaker, + HitExtractor implicitTiebreaker, + boolean descending + ) { this.stage = stage; this.queryRequest = queryRequest; this.keys = keys; diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/assembler/ExecutionManager.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/assembler/ExecutionManager.java index 44fc7be6144a0..bd519d9ae754a 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/assembler/ExecutionManager.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/assembler/ExecutionManager.java @@ -51,13 +51,15 @@ public ExecutionManager(EqlSession eqlSession) { this.cfg = eqlSession.configuration(); } - public Executable assemble(List> listOfKeys, - List plans, - Attribute timestamp, - Attribute tiebreaker, - OrderDirection direction, - TimeValue maxSpan, - Limit limit) { + public Executable assemble( + List> listOfKeys, + List plans, + Attribute timestamp, + Attribute tiebreaker, + OrderDirection direction, + TimeValue maxSpan, + Limit limit + ) { FieldExtractorRegistry extractorRegistry = new FieldExtractorRegistry(); boolean descending = direction == OrderDirection.DESC; @@ -101,7 +103,7 @@ public Executable assemble(List> listOfKeys, keyFields = emptyList(); break; } - // optional field + // optional field } else if (extractor instanceof ComputingExtractor) { keyFields.add(((ComputingExtractor) extractor).hitName()); } @@ -113,8 +115,15 @@ public Executable assemble(List> listOfKeys, SearchSourceBuilder source = ((EsQueryExec) query).source(session, false); QueryRequest original = () -> source; BoxedQueryRequest boxedRequest = new BoxedQueryRequest(original, timestampName, keyFields, optionalKeys); - Criterion criterion = - new Criterion<>(i, boxedRequest, keyExtractors, tsExtractor, tbExtractor, itbExtractor, i == 0 && descending); + Criterion criterion = new Criterion<>( + i, + boxedRequest, + keyExtractors, + tsExtractor, + tbExtractor, + itbExtractor, + i == 0 && descending + ); criteria.add(criterion); } else { // until @@ -129,10 +138,12 @@ public Executable assemble(List> listOfKeys, int completionStage = criteria.size() - 1; SequenceMatcher matcher = new SequenceMatcher(completionStage, descending, maxSpan, limit, session.circuitBreaker()); - TumblingWindow w = new TumblingWindow(new PITAwareQueryClient(session), - criteria.subList(0, completionStage), - criteria.get(completionStage), - matcher); + TumblingWindow w = new TumblingWindow( + new PITAwareQueryClient(session), + criteria.subList(0, completionStage), + criteria.get(completionStage), + matcher + ); return w; } diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/BasicQueryClient.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/BasicQueryClient.java index 1b5325cb8c870..d5ba7366b03e2 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/BasicQueryClient.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/BasicQueryClient.java @@ -120,7 +120,7 @@ public void fetchHits(Iterable> refs, ActionListener[] hits = new List[topListSize]; for (int i = 0; i < hits.length; i++) { hits[i] = Arrays.asList(new SearchHit[listSize]); @@ -167,8 +167,11 @@ public void fetchHits(Iterable> refs, ActionListener { SearchHit previous = seq.get(pos / listSize).set(pos % listSize, doc); if (previous != null) { - throw new EqlIllegalArgumentException("Overriding sequence match [{}] with [{}]", - new HitReference(previous), docRef); + throw new EqlIllegalArgumentException( + "Overriding sequence match [{}] with [{}]", + new HitReference(previous), + docRef + ); } }); } diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/HitReference.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/HitReference.java index 211415f367091..5f0fbf23c8eeb 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/HitReference.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/HitReference.java @@ -61,8 +61,7 @@ public boolean equals(Object obj) { } HitReference other = (HitReference) obj; - return Objects.equals(index, other.index) - && Objects.equals(id, other.id); + return Objects.equals(index, other.index) && Objects.equals(id, other.id); } @Override diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/Ordinal.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/Ordinal.java index d1e8e3f97c254..c934fdc6d34e0 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/Ordinal.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/Ordinal.java @@ -60,8 +60,8 @@ public boolean equals(Object obj) { Ordinal other = (Ordinal) obj; return Objects.equals(timestamp, other.timestamp) - && Objects.equals(tiebreaker, other.tiebreaker) - && Objects.equals(implicitTiebreaker, other.implicitTiebreaker); + && Objects.equals(tiebreaker, other.tiebreaker) + && Objects.equals(implicitTiebreaker, other.implicitTiebreaker); } @Override @@ -119,8 +119,8 @@ public boolean afterOrAt(Ordinal other) { } public Object[] toArray() { - return tiebreaker != null ? - new Object[] { timestamp.toString(), tiebreaker, implicitTiebreaker } + return tiebreaker != null + ? new Object[] { timestamp.toString(), tiebreaker, implicitTiebreaker } : new Object[] { timestamp.toString(), implicitTiebreaker }; } } diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/PITAwareQueryClient.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/PITAwareQueryClient.java index b1a158c07026a..18d8a2fb316a7 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/PITAwareQueryClient.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/PITAwareQueryClient.java @@ -8,22 +8,22 @@ package org.elasticsearch.xpack.eql.execution.search; import org.elasticsearch.action.ActionListener; +import org.elasticsearch.action.search.ClosePointInTimeAction; +import org.elasticsearch.action.search.ClosePointInTimeRequest; +import org.elasticsearch.action.search.ClosePointInTimeResponse; import org.elasticsearch.action.search.MultiSearchRequest; import org.elasticsearch.action.search.MultiSearchResponse; +import org.elasticsearch.action.search.OpenPointInTimeAction; +import org.elasticsearch.action.search.OpenPointInTimeRequest; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.common.Strings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.CollectionUtils; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.get.GetResult; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.search.builder.PointInTimeBuilder; import org.elasticsearch.search.builder.SearchSourceBuilder; -import org.elasticsearch.action.search.ClosePointInTimeAction; -import org.elasticsearch.action.search.ClosePointInTimeRequest; -import org.elasticsearch.action.search.ClosePointInTimeResponse; -import org.elasticsearch.action.search.OpenPointInTimeAction; -import org.elasticsearch.action.search.OpenPointInTimeRequest; import org.elasticsearch.xpack.eql.session.EqlSession; import org.elasticsearch.xpack.ql.index.IndexResolver; @@ -113,10 +113,10 @@ private void makeRequestPITCompatible(SearchRequest request) { // listener handing the extraction of new PIT and closing in case of exceptions private ActionListener pitListener(Function pitIdExtractor, ActionListener listener) { return wrap(r -> { - // get pid - pitId = pitIdExtractor.apply(r); - listener.onResponse(r); - }, + // get pid + pitId = pitIdExtractor.apply(r); + listener.onResponse(r); + }, // always close PIT in case of exceptions e -> { listener.onFailure(e); @@ -124,24 +124,26 @@ private ActionListener pitListener(Function {}, ex -> {})); } - }); + } + ); } private void openPIT(ActionListener listener, Runnable runnable) { - OpenPointInTimeRequest request = new OpenPointInTimeRequest(indices) - .indicesOptions(IndexResolver.FIELD_CAPS_INDICES_OPTIONS) + OpenPointInTimeRequest request = new OpenPointInTimeRequest(indices).indicesOptions(IndexResolver.FIELD_CAPS_INDICES_OPTIONS) .keepAlive(keepAlive); client.execute(OpenPointInTimeAction.INSTANCE, request, wrap(r -> { - pitId = r.getPointInTimeId(); - runnable.run(); - }, - listener::onFailure)); + pitId = r.getPointInTimeId(); + runnable.run(); + }, listener::onFailure)); } @Override public void close(ActionListener listener) { - client.execute(ClosePointInTimeAction.INSTANCE, new ClosePointInTimeRequest(pitId), - map(listener, ClosePointInTimeResponse::isSucceeded)); + client.execute( + ClosePointInTimeAction.INSTANCE, + new ClosePointInTimeRequest(pitId), + map(listener, ClosePointInTimeResponse::isSucceeded) + ); pitId = null; } } diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/RuntimeUtils.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/RuntimeUtils.java index 9baa70c95b51a..2fce5d2a0aacb 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/RuntimeUtils.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/RuntimeUtils.java @@ -100,10 +100,19 @@ private static void logSearchResponse(SearchResponse response, Logger logger) { SearchHit[] hits = response.getHits().getHits(); int count = hits != null ? hits.length : 0; - logger.trace("Got search response [hits {}, {} aggregations: [{}], {} failed shards, {} skipped shards, " - + "{} successful shards, {} total shards, took {}, timed out [{}]]", count, aggs.size(), - aggsNames, response.getFailedShards(), response.getSkippedShards(), response.getSuccessfulShards(), - response.getTotalShards(), response.getTook(), response.isTimedOut()); + logger.trace( + "Got search response [hits {}, {} aggregations: [{}], {} failed shards, {} skipped shards, " + + "{} successful shards, {} total shards, took {}, timed out [{}]]", + count, + aggs.size(), + aggsNames, + response.getFailedShards(), + response.getSkippedShards(), + response.getSuccessfulShards(), + response.getTotalShards(), + response.getTook(), + response.isTimedOut() + ); } public static List createExtractor(List fields, EqlConfiguration cfg) { @@ -145,16 +154,14 @@ public static HitExtractor createExtractor(FieldExtraction ref, EqlConfiguration throw new EqlIllegalArgumentException("Unexpected value reference {}", ref.getClass()); } - - public static SearchRequest prepareRequest(SearchSourceBuilder source, - boolean includeFrozen, - String... indices) { + public static SearchRequest prepareRequest(SearchSourceBuilder source, boolean includeFrozen, String... indices) { SearchRequest searchRequest = new SearchRequest(SWITCH_TO_MULTI_VALUE_FIELDS_VERSION); searchRequest.indices(indices); searchRequest.source(source); searchRequest.allowPartialSearchResults(false); searchRequest.indicesOptions( - includeFrozen ? IndexResolver.FIELD_CAPS_FROZEN_INDICES_OPTIONS : IndexResolver.FIELD_CAPS_INDICES_OPTIONS); + includeFrozen ? IndexResolver.FIELD_CAPS_FROZEN_INDICES_OPTIONS : IndexResolver.FIELD_CAPS_INDICES_OPTIONS + ); return searchRequest; } @@ -173,8 +180,7 @@ public static SearchSourceBuilder addFilter(QueryBuilder filter, SearchSourceBui if (filter != null && bool.filter().contains(filter) == false) { bool.filter(filter); } - } - else { + } else { bool = boolQuery(); if (query != null) { bool.filter(query); @@ -188,9 +194,11 @@ public static SearchSourceBuilder addFilter(QueryBuilder filter, SearchSourceBui return source; } - public static SearchSourceBuilder replaceFilter(List oldFilters, - List newFilters, - SearchSourceBuilder source) { + public static SearchSourceBuilder replaceFilter( + List oldFilters, + List newFilters, + SearchSourceBuilder source + ) { BoolQueryBuilder bool = null; QueryBuilder query = source.query(); diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/SourceGenerator.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/SourceGenerator.java index f1e3ea2a9c411..941a94c3d164c 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/SourceGenerator.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/SourceGenerator.java @@ -32,8 +32,12 @@ public abstract class SourceGenerator { private SourceGenerator() {} - public static SearchSourceBuilder sourceBuilder(QueryContainer container, QueryBuilder filter, List fetchFields, - Map runtimeMappings) { + public static SearchSourceBuilder sourceBuilder( + QueryContainer container, + QueryBuilder filter, + List fetchFields, + Map runtimeMappings + ) { QueryBuilder finalQuery = null; // add the source if (container.query() != null) { @@ -102,14 +106,12 @@ private static void sorting(QueryContainer container, SearchSourceBuilder source if (attr instanceof FieldAttribute) { FieldAttribute fa = ((FieldAttribute) attr).exactAttribute(); - sortBuilder = fieldSort(fa.name()) - .missing(as.missing().searchOrder(as.direction())) - .unmappedType(fa.dataType().esType()); + sortBuilder = fieldSort(fa.name()).missing(as.missing().searchOrder(as.direction())) + .unmappedType(fa.dataType().esType()); if (fa.isNested()) { - FieldSortBuilder fieldSort = fieldSort(fa.name()) - .missing(as.missing().searchOrder(as.direction())) - .unmappedType(fa.dataType().esType()); + FieldSortBuilder fieldSort = fieldSort(fa.name()).missing(as.missing().searchOrder(as.direction())) + .unmappedType(fa.dataType().esType()); NestedSortBuilder newSort = new NestedSortBuilder(fa.nestedParent().name()); NestedSortBuilder nestedSort = fieldSort.getNestedSort(); @@ -133,8 +135,10 @@ private static void sorting(QueryContainer container, SearchSourceBuilder source } } else if (sortable instanceof ScriptSort) { ScriptSort ss = (ScriptSort) sortable; - sortBuilder = scriptSort(ss.script().toPainless(), - ss.script().outputType().isNumeric() ? ScriptSortType.NUMBER : ScriptSortType.STRING); + sortBuilder = scriptSort( + ss.script().toPainless(), + ss.script().outputType().isNumeric() ? ScriptSortType.NUMBER : ScriptSortType.STRING + ); } if (sortBuilder != null) { diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/Timestamp.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/Timestamp.java index 3eae288724042..5e1082dcb64f9 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/Timestamp.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/Timestamp.java @@ -15,7 +15,7 @@ public abstract class Timestamp { static final long MILLIS_PER_SECOND = 1_000L; static final long NANOS_PER_MILLI = 1_000_000L; - private static final long[] MICROS_MULTIPLIER = {0L, 100_000L, 10_000L, 1_000L, 1_00L, 10L}; + private static final long[] MICROS_MULTIPLIER = { 0L, 100_000L, 10_000L, 1_000L, 1_00L, 10L }; private String source; @@ -32,8 +32,9 @@ public static Timestamp of(String milliseconds) { if (dotIndex > 0) { long millis = Long.parseLong(milliseconds.substring(0, dotIndex)); int digits = milliseconds.length() - dotIndex - 1; - long micros = (digits >= 6) ? Long.parseLong(milliseconds.substring(dotIndex + 1, dotIndex + 1 + 6)) : - Long.parseLong(milliseconds.substring(dotIndex + 1)) * MICROS_MULTIPLIER[digits]; + long micros = (digits >= 6) + ? Long.parseLong(milliseconds.substring(dotIndex + 1, dotIndex + 1 + 6)) + : Long.parseLong(milliseconds.substring(dotIndex + 1)) * MICROS_MULTIPLIER[digits]; timestamp = new NanosTimestamp(millis, micros); } else { timestamp = new MillisTimestamp(Long.parseLong(milliseconds)); diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/extractor/FieldHitExtractor.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/extractor/FieldHitExtractor.java index 69b32768bdf9a..80e3ed8f5dd9a 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/extractor/FieldHitExtractor.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/extractor/FieldHitExtractor.java @@ -11,6 +11,7 @@ import org.elasticsearch.xpack.ql.execution.search.extractor.AbstractFieldHitExtractor; import org.elasticsearch.xpack.ql.type.DataType; import org.elasticsearch.xpack.ql.util.DateUtils; + import java.io.IOException; import java.time.Instant; import java.time.ZoneId; @@ -27,8 +28,7 @@ public FieldHitExtractor(StreamInput in) throws IOException { super(in); } - public FieldHitExtractor(String name, DataType dataType, ZoneId zoneId, String hitName, - boolean arrayLeniency) { + public FieldHitExtractor(String name, DataType dataType, ZoneId zoneId, String hitName, boolean arrayLeniency) { super(name, dataType, zoneId, hitName, arrayLeniency); } diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/extractor/ImplicitTiebreakerHitExtractor.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/extractor/ImplicitTiebreakerHitExtractor.java index ee789145f701d..7c5f9205daf19 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/extractor/ImplicitTiebreakerHitExtractor.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/extractor/ImplicitTiebreakerHitExtractor.java @@ -24,7 +24,7 @@ public class ImplicitTiebreakerHitExtractor implements HitExtractor { private ImplicitTiebreakerHitExtractor() {} @Override - public void writeTo(StreamOutput out) throws IOException { } + public void writeTo(StreamOutput out) throws IOException {} @Override public String getWriteableName() { diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/extractor/TimestampFieldHitExtractor.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/extractor/TimestampFieldHitExtractor.java index f6b1ffd01ded6..eceadbf7c7461 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/extractor/TimestampFieldHitExtractor.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/extractor/TimestampFieldHitExtractor.java @@ -12,8 +12,7 @@ public class TimestampFieldHitExtractor extends FieldHitExtractor { public TimestampFieldHitExtractor(FieldHitExtractor target) { - super(target.fieldName(), target.dataType(), target.zoneId(), target.hitName(), - target.arrayLeniency()); + super(target.fieldName(), target.dataType(), target.zoneId(), target.hitName(), target.arrayLeniency()); } @Override diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/sequence/KeyAndOrdinal.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/sequence/KeyAndOrdinal.java index 7d55b226ee67b..27dd0bb6e12c0 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/sequence/KeyAndOrdinal.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/sequence/KeyAndOrdinal.java @@ -44,8 +44,7 @@ public boolean equals(Object obj) { } KeyAndOrdinal other = (KeyAndOrdinal) obj; - return Objects.equals(key, other.key) - && Objects.equals(ordinal, other.ordinal); + return Objects.equals(key, other.key) && Objects.equals(ordinal, other.ordinal); } @Override diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/sequence/KeyToSequences.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/sequence/KeyToSequences.java index b06849570dd8c..7ad81f39659a1 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/sequence/KeyToSequences.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/sequence/KeyToSequences.java @@ -113,8 +113,8 @@ void remove(int stage, SequenceKey key) { * Remove all matches except the latest occurring _before_ the given ordinal. */ void trimToTail(Ordinal ordinal) { - for (Iterator it = keyToSequences.values().iterator(); it.hasNext(); ) { - SequenceEntry seqs = it.next(); + for (Iterator it = keyToSequences.values().iterator(); it.hasNext();) { + SequenceEntry seqs = it.next(); // remember the last item found (will be ascending) // to trim unneeded until that occur before it Sequence firstTail = null; diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/sequence/Match.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/sequence/Match.java index 52e07530bd160..ecf92fcd46a89 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/sequence/Match.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/sequence/Match.java @@ -58,8 +58,7 @@ public boolean equals(Object obj) { } Match other = (Match) obj; - return Objects.equals(ordinal, other.ordinal) - && Objects.equals(hit, other.hit); + return Objects.equals(ordinal, other.ordinal) && Objects.equals(hit, other.hit); } @Override diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/sequence/Sequence.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/sequence/Sequence.java index 38c347e17a29f..a013df58b090c 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/sequence/Sequence.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/sequence/Sequence.java @@ -100,8 +100,7 @@ public boolean equals(Object obj) { } Sequence other = (Sequence) obj; - return Objects.equals(currentStage, other.currentStage) - && Objects.equals(key, other.key); + return Objects.equals(currentStage, other.currentStage) && Objects.equals(key, other.key); } @Override @@ -111,10 +110,7 @@ public String toString() { nf.setMinimumIntegerDigits(numberOfDigits); StringBuilder sb = new StringBuilder(); - sb.append(format(null, "[Seq<{}>[{}/{}]]", - key, - nf.format(currentStage), - nf.format(stages - 1))); + sb.append(format(null, "[Seq<{}>[{}/{}]]", key, nf.format(currentStage), nf.format(stages - 1))); for (int i = 0; i < matches.length; i++) { sb.append(format(null, "\n [{}]={{}}", nf.format(i), matches[i])); diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/sequence/SequenceMatcher.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/sequence/SequenceMatcher.java index 0bced10cb822b..550c65da64d3a 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/sequence/SequenceMatcher.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/sequence/SequenceMatcher.java @@ -42,11 +42,14 @@ static class Stats { @Override public String toString() { - return LoggerMessageFormat.format(null, "Stats: Seen [{}]/Ignored [{}]/Rejected {Maxspan [{}]/Until [{}]}", - seen, - ignored, - rejectionMaxspan, - rejectionUntil); + return LoggerMessageFormat.format( + null, + "Stats: Seen [{}]/Ignored [{}]/Rejected {Maxspan [{}]/Until [{}]}", + seen, + ignored, + rejectionMaxspan, + rejectionUntil + ); } public void clear() { @@ -334,9 +337,12 @@ private void trackMemory(long prevRamBytesUsedInflight, long prevRamBytesUsedCom @Override public String toString() { - return LoggerMessageFormat.format(null, "Tracking [{}] keys with [{}] completed and {} in-flight", - keyToSequences, - completed.size(), - stageToKeys); + return LoggerMessageFormat.format( + null, + "Tracking [{}] keys with [{}] completed and {} in-flight", + keyToSequences, + completed.size(), + stageToKeys + ); } } diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/sequence/TumblingWindow.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/sequence/TumblingWindow.java index bd95f8b34307c..6a2173cc62564 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/sequence/TumblingWindow.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/sequence/TumblingWindow.java @@ -11,8 +11,8 @@ import org.apache.logging.log4j.Logger; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.search.SearchResponse; -import org.elasticsearch.core.Tuple; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.core.Tuple; import org.elasticsearch.search.SearchHit; import org.elasticsearch.xpack.eql.execution.assembler.BoxedQueryRequest; import org.elasticsearch.xpack.eql.execution.assembler.Criterion; @@ -99,10 +99,12 @@ private static class WindowInfo { } } - public TumblingWindow(QueryClient client, - List> criteria, - Criterion until, - SequenceMatcher matcher) { + public TumblingWindow( + QueryClient client, + List> criteria, + Criterion until, + SequenceMatcher matcher + ) { this.client = client; this.until = until; @@ -145,8 +147,7 @@ private void tumbleWindow(int currentStage, ActionListener listener) { if (currentStage == 0) { matcher.trim(null); } - } - else { + } else { // trim to last until the current window // that's because some stages can be sparse, other dense // and results from the sparse stage can be after those in the dense one @@ -274,7 +275,7 @@ private void completeBaseCriterion(int baseStage, List hits, WindowIn } } // for ASC queries continue if there are still matches available - else { + else { if (matcher.hasFollowingCandidates(baseStage)) { next = () -> rebaseWindow(nextStage, listener); } @@ -451,7 +452,7 @@ private void secondaryCriterion(WindowInfo window, int currentStage, ActionListe private List trim(List searchHits, Criterion criterion, Ordinal boundary) { int offset = 0; - for (int i = searchHits.size() - 1; i >= 0 ; i--) { + for (int i = searchHits.size() - 1; i >= 0; i--) { Ordinal ordinal = criterion.ordinal(searchHits.get(i)); if (ordinal.after(boundary)) { offset++; @@ -501,14 +502,11 @@ private void setupWindowFromTail(Ordinal from) { // check if it hasn't been set before if (from.equals(request.from()) == false) { // initialize the next request - request.from(from) - .nextAfter(from); + request.from(from).nextAfter(from); // initialize until (if available) if (until != null) { - until.queryRequest() - .from(from) - .nextAfter(from); + until.queryRequest().from(from).nextAfter(from); } // reset all sub queries for (int i = 2; i < maxStages; i++) { @@ -599,9 +597,9 @@ private static Ordinal tailOrdinal(List hits, Criterion> hits(List sequences) { return () -> { - Iterator delegate = criteria.get(0).descending() != criteria.get(1).descending() ? - new ReversedIterator<>(sequences) : - sequences.iterator(); + Iterator delegate = criteria.get(0).descending() != criteria.get(1).descending() + ? new ReversedIterator<>(sequences) + : sequences.iterator(); return new Iterator<>() { diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/OptionalMissingAttribute.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/OptionalMissingAttribute.java index fc7549f43802a..fbf4c810074c9 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/OptionalMissingAttribute.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/OptionalMissingAttribute.java @@ -30,12 +30,15 @@ public OptionalMissingAttribute(Source source, String name, String qualifier) { } @Override - protected Attribute clone(Source source, - String name, - DataType type, - String qualifier, - Nullability nullability, - NameId id, boolean synthetic) { + protected Attribute clone( + Source source, + String name, + DataType type, + String qualifier, + Nullability nullability, + NameId id, + boolean synthetic + ) { return new OptionalMissingAttribute(source, name, qualifier); } diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/EqlFunctionDefinition.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/EqlFunctionDefinition.java index 6b57ea67217cc..d4e38243f10b3 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/EqlFunctionDefinition.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/EqlFunctionDefinition.java @@ -20,11 +20,13 @@ class EqlFunctionDefinition extends FunctionDefinition { private final boolean caseAware; - protected EqlFunctionDefinition(String name, - List aliases, - Class clazz, - boolean caseAware, - Builder builder) { + protected EqlFunctionDefinition( + String name, + List aliases, + Class clazz, + boolean caseAware, + Builder builder + ) { super(name, aliases, clazz, builder); this.caseAware = caseAware; } diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/EqlFunctionRegistry.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/EqlFunctionRegistry.java index 11395aa4f6f5f..bf183a5b11919 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/EqlFunctionRegistry.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/EqlFunctionRegistry.java @@ -51,10 +51,10 @@ public EqlFunctionRegistry() { } private FunctionDefinition[][] functions() { - return new FunctionDefinition[][]{ + return new FunctionDefinition[][] { // Scalar functions // String - new FunctionDefinition[]{ + new FunctionDefinition[] { def(Between.class, Between::new, "between"), def(CIDRMatch.class, CIDRMatch::new, "cidrmatch"), def(Concat.class, Concat::new, "concat"), @@ -64,18 +64,15 @@ private FunctionDefinition[][] functions() { def(StartsWith.class, StartsWith::new, "startswith"), def(ToString.class, ToString::new, "string"), def(StringContains.class, StringContains::new, "stringcontains"), - def(Substring.class, Substring::new, "substring"), - }, + def(Substring.class, Substring::new, "substring"), }, // Arithmetic - new FunctionDefinition[]{ + new FunctionDefinition[] { def(Add.class, Add::new, "add"), def(Div.class, Div::new, "divide"), def(Mod.class, Mod::new, "modulo"), def(Mul.class, Mul::new, "multiply"), def(ToNumber.class, ToNumber::new, "number"), - def(Sub.class, Sub::new, "subtract"), - } - }; + def(Sub.class, Sub::new, "subtract"), } }; } @Override @@ -145,11 +142,13 @@ protected static FunctionDefinition def(Class function, } else if (hasMinimumTwo == false && children.size() != 3) { throw new QlIllegalArgumentException("expects exactly three arguments"); } - return ctorRef.build(source, + return ctorRef.build( + source, children.get(0), children.get(1), children.size() == 3 ? children.get(2) : null, - defaultSensitivityIfNotSet(caseInsensitive)); + defaultSensitivityIfNotSet(caseInsensitive) + ); }; return def(function, builder, names); } @@ -162,9 +161,11 @@ protected interface QuaternaryBuilderCaseAwareBuilder { * Build a {@linkplain FunctionDefinition} for a quaternary function. */ @SuppressWarnings("overloads") // These are ambiguous if you aren't using ctor references but we always do - protected static FunctionDefinition def(Class function, - QuaternaryBuilderCaseAwareBuilder ctorRef, - String... names) { + protected static FunctionDefinition def( + Class function, + QuaternaryBuilderCaseAwareBuilder ctorRef, + String... names + ) { EqlFunctionBuilder builder = (source, children, caseInsensitive) -> { boolean hasMinimumThree = OptionalArgument.class.isAssignableFrom(function); if (hasMinimumThree && (children.size() > 4 || children.size() < 3)) { @@ -172,12 +173,14 @@ protected static FunctionDefinition def(Class function, } else if (hasMinimumThree == false && children.size() != 4) { throw new QlIllegalArgumentException("expects exactly four arguments"); } - return ctorRef.build(source, + return ctorRef.build( + source, children.get(0), children.get(1), children.get(2), children.size() == 4 ? children.get(3) : null, - defaultSensitivityIfNotSet(caseInsensitive)); + defaultSensitivityIfNotSet(caseInsensitive) + ); }; return def(function, builder, names); } @@ -190,17 +193,21 @@ protected interface UnaryVariadicCaseAwareBuilder { * Build a {@linkplain FunctionDefinition} for a quaternary function. */ @SuppressWarnings("overloads") // These are ambiguous if you aren't using ctor references but we always do - protected static FunctionDefinition def(Class function, - UnaryVariadicCaseAwareBuilder ctorRef, - String... names) { + protected static FunctionDefinition def( + Class function, + UnaryVariadicCaseAwareBuilder ctorRef, + String... names + ) { EqlFunctionBuilder builder = (source, children, caseInsensitive) -> { if (children.size() < 2) { throw new QlIllegalArgumentException("expects at least two arguments"); } - return ctorRef.build(source, + return ctorRef.build( + source, children.get(0), children.subList(1, children.size()), - defaultSensitivityIfNotSet(caseInsensitive)); + defaultSensitivityIfNotSet(caseInsensitive) + ); }; return def(function, builder, names); } diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/math/ToNumber.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/math/ToNumber.java index 604e577ec2b4b..2a501c5ae6e0f 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/math/ToNumber.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/math/ToNumber.java @@ -85,21 +85,20 @@ public ScriptTemplate asScript() { ScriptTemplate valueScript = asScript(value); ScriptTemplate baseScript = asScript(base); - return new ScriptTemplate(format(Locale.ROOT, formatTemplate("{eql}.%s(%s,%s)"), - "number", - valueScript.template(), - baseScript.template()), - paramsBuilder() - .script(valueScript.params()) - .script(baseScript.params()) - .build(), dataType()); + return new ScriptTemplate( + format(Locale.ROOT, formatTemplate("{eql}.%s(%s,%s)"), "number", valueScript.template(), baseScript.template()), + paramsBuilder().script(valueScript.params()).script(baseScript.params()).build(), + dataType() + ); } @Override public ScriptTemplate scriptWithField(FieldAttribute field) { - return new ScriptTemplate(processScript(Scripts.DOC_VALUE), - paramsBuilder().variable(field.exactAttribute().name()).build(), - dataType()); + return new ScriptTemplate( + processScript(Scripts.DOC_VALUE), + paramsBuilder().variable(field.exactAttribute().name()).build(), + dataType() + ); } @Override diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/math/ToNumberFunctionPipe.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/math/ToNumberFunctionPipe.java index 011bbbb0a7b7f..bbb0546ebb1a2 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/math/ToNumberFunctionPipe.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/math/ToNumberFunctionPipe.java @@ -71,7 +71,6 @@ public ToNumberFunctionProcessor asProcessor() { return new ToNumberFunctionProcessor(value.asProcessor(), base.asProcessor()); } - public Pipe value() { return value; } diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/math/ToNumberFunctionProcessor.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/math/ToNumberFunctionProcessor.java index 3add3d560f10a..214600c69b6cf 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/math/ToNumberFunctionProcessor.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/math/ToNumberFunctionProcessor.java @@ -111,7 +111,6 @@ public int hashCode() { return Objects.hash(value, base); } - @Override public String getWriteableName() { return NAME; diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/Between.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/Between.java index 92292289f370a..b8bc877b614c3 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/Between.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/Between.java @@ -98,9 +98,15 @@ public Expression greedy() { @Override protected Pipe makePipe() { - return new BetweenFunctionPipe(source(), this, Expressions.pipe(input), - Expressions.pipe(left), Expressions.pipe(right), - Expressions.pipe(greedy), isCaseInsensitive()); + return new BetweenFunctionPipe( + source(), + this, + Expressions.pipe(input), + Expressions.pipe(left), + Expressions.pipe(right), + Expressions.pipe(greedy), + isCaseInsensitive() + ); } @Override @@ -128,29 +134,40 @@ public ScriptTemplate asScript() { return asScriptFrom(inputScript, leftScript, rightScript, greedyScript); } - protected ScriptTemplate asScriptFrom(ScriptTemplate inputScript, ScriptTemplate leftScript, - ScriptTemplate rightScript, ScriptTemplate greedyScript) { - return new ScriptTemplate(format(Locale.ROOT, formatTemplate("{eql}.%s(%s,%s,%s,%s,%s)"), - "between", - inputScript.template(), - leftScript.template(), - rightScript.template(), - greedyScript.template(), - "{}"), - paramsBuilder() - .script(inputScript.params()) + protected ScriptTemplate asScriptFrom( + ScriptTemplate inputScript, + ScriptTemplate leftScript, + ScriptTemplate rightScript, + ScriptTemplate greedyScript + ) { + return new ScriptTemplate( + format( + Locale.ROOT, + formatTemplate("{eql}.%s(%s,%s,%s,%s,%s)"), + "between", + inputScript.template(), + leftScript.template(), + rightScript.template(), + greedyScript.template(), + "{}" + ), + paramsBuilder().script(inputScript.params()) .script(leftScript.params()) .script(rightScript.params()) .script(greedyScript.params()) .variable(isCaseInsensitive()) - .build(), dataType()); + .build(), + dataType() + ); } @Override public ScriptTemplate scriptWithField(FieldAttribute field) { - return new ScriptTemplate(processScript(Scripts.DOC_VALUE), - paramsBuilder().variable(field.exactAttribute().name()).build(), - dataType()); + return new ScriptTemplate( + processScript(Scripts.DOC_VALUE), + paramsBuilder().variable(field.exactAttribute().name()).build(), + dataType() + ); } @Override diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/BetweenFunctionPipe.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/BetweenFunctionPipe.java index ff139f166743b..9d8a4327780c6 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/BetweenFunctionPipe.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/BetweenFunctionPipe.java @@ -21,13 +21,15 @@ public class BetweenFunctionPipe extends Pipe { private final Pipe input, left, right, greedy; private final boolean caseInsensitive; - public BetweenFunctionPipe(Source source, - Expression expression, - Pipe input, - Pipe left, - Pipe right, - Pipe greedy, - boolean caseInsensitive) { + public BetweenFunctionPipe( + Source source, + Expression expression, + Pipe input, + Pipe left, + Pipe right, + Pipe greedy, + boolean caseInsensitive + ) { super(source, expression, Arrays.asList(input, left, right, greedy)); this.input = input; this.left = left; @@ -55,7 +57,9 @@ public final Pipe resolveAttributes(AttributeResolver resolver) { @Override public boolean supportedByAggsOnlyQuery() { - return input.supportedByAggsOnlyQuery() && left.supportedByAggsOnlyQuery() && right.supportedByAggsOnlyQuery() + return input.supportedByAggsOnlyQuery() + && left.supportedByAggsOnlyQuery() + && right.supportedByAggsOnlyQuery() && greedy.supportedByAggsOnlyQuery(); } @@ -83,8 +87,13 @@ protected NodeInfo info() { @Override public BetweenFunctionProcessor asProcessor() { - return new BetweenFunctionProcessor(input.asProcessor(), left.asProcessor(), right.asProcessor(), - greedy.asProcessor(), caseInsensitive); + return new BetweenFunctionProcessor( + input.asProcessor(), + left.asProcessor(), + right.asProcessor(), + greedy.asProcessor(), + caseInsensitive + ); } public Pipe input() { diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/CIDRMatch.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/CIDRMatch.java index c25adb210886b..888d0b479f221 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/CIDRMatch.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/CIDRMatch.java @@ -114,19 +114,19 @@ public ScriptTemplate asScript() { List values = new ArrayList<>(new LinkedHashSet<>(Expressions.fold(addresses))); return new ScriptTemplate( - formatTemplate(LoggerMessageFormat.format("{eql}.","cidrMatch({}, {})", leftScript.template())), - paramsBuilder() - .script(leftScript.params()) - .variable(values) - .build(), - dataType()); + formatTemplate(LoggerMessageFormat.format("{eql}.", "cidrMatch({}, {})", leftScript.template())), + paramsBuilder().script(leftScript.params()).variable(values).build(), + dataType() + ); } @Override public ScriptTemplate scriptWithField(FieldAttribute field) { - return new ScriptTemplate(processScript(Scripts.DOC_VALUE), - paramsBuilder().variable(field.exactAttribute().name()).build(), - dataType()); + return new ScriptTemplate( + processScript(Scripts.DOC_VALUE), + paramsBuilder().variable(field.exactAttribute().name()).build(), + dataType() + ); } @Override diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/CIDRMatchFunctionPipe.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/CIDRMatchFunctionPipe.java index f663875ccc99b..036e1eed0e1de 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/CIDRMatchFunctionPipe.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/CIDRMatchFunctionPipe.java @@ -95,7 +95,7 @@ protected NodeInfo info() { @Override public CIDRMatchFunctionProcessor asProcessor() { ArrayList processors = new ArrayList<>(addresses.size()); - for (Pipe address: addresses) { + for (Pipe address : addresses) { processors.add(address.asProcessor()); } return new CIDRMatchFunctionProcessor(input.asProcessor(), processors); diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/CIDRMatchFunctionProcessor.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/CIDRMatchFunctionProcessor.java index fa5106e1ccb7e..2eb9d7630effb 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/CIDRMatchFunctionProcessor.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/CIDRMatchFunctionProcessor.java @@ -8,9 +8,9 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.common.network.CIDRUtils; import org.elasticsearch.xpack.eql.EqlIllegalArgumentException; import org.elasticsearch.xpack.ql.expression.gen.processor.Processor; -import org.elasticsearch.common.network.CIDRUtils; import org.elasticsearch.xpack.ql.util.Check; import java.io.IOException; @@ -65,12 +65,12 @@ public static Object doProcess(Object source, List addresses) { String[] arr = new String[addresses.size()]; int i = 0; - for (Object address: addresses) { + for (Object address : addresses) { Check.isString(address); - arr[i++] = (String)address; + arr[i++] = (String) address; } try { - return CIDRUtils.isInRange((String)source, arr); + return CIDRUtils.isInRange((String) source, arr); } catch (IllegalArgumentException e) { throw new EqlIllegalArgumentException(e.getMessage()); } @@ -84,7 +84,6 @@ public List addresses() { return addresses; } - @Override public int hashCode() { return Objects.hash(source(), addresses()); @@ -101,7 +100,6 @@ public boolean equals(Object obj) { } CIDRMatchFunctionProcessor other = (CIDRMatchFunctionProcessor) obj; - return Objects.equals(source(), other.source()) - && Objects.equals(addresses(), other.addresses()); + return Objects.equals(source(), other.source()) && Objects.equals(addresses(), other.addresses()); } } diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/ConcatFunctionPipe.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/ConcatFunctionPipe.java index eec4a0802b326..b9f8f7dfc4330 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/ConcatFunctionPipe.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/ConcatFunctionPipe.java @@ -80,7 +80,7 @@ protected NodeInfo info() { @Override public ConcatFunctionProcessor asProcessor() { List processors = new ArrayList<>(values.size()); - for (Pipe p: values) { + for (Pipe p : values) { processors.add(p.asProcessor()); } return new ConcatFunctionProcessor(processors); diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/ConcatFunctionProcessor.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/ConcatFunctionProcessor.java index a90d444f2db89..e4c00001e3650 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/ConcatFunctionProcessor.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/ConcatFunctionProcessor.java @@ -26,7 +26,7 @@ public ConcatFunctionProcessor(List values) { @Override public final void writeTo(StreamOutput out) throws IOException { - for (Processor v: values) { + for (Processor v : values) { out.writeNamedWriteable(v); } } @@ -34,7 +34,7 @@ public final void writeTo(StreamOutput out) throws IOException { @Override public Object process(Object input) { List processed = new ArrayList<>(values.size()); - for (Processor v: values) { + for (Processor v : values) { processed.add(v.process(input)); } return doProcess(processed); @@ -47,7 +47,7 @@ public static Object doProcess(List inputs) { StringBuilder str = new StringBuilder(); - for (Object input: inputs) { + for (Object input : inputs) { if (input == null) { return null; } @@ -76,7 +76,6 @@ public int hashCode() { return Objects.hash(values); } - @Override public String getWriteableName() { return NAME; diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/EndsWithFunctionProcessor.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/EndsWithFunctionProcessor.java index 0ee7f2c6e8679..d69aa41baf147 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/EndsWithFunctionProcessor.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/EndsWithFunctionProcessor.java @@ -101,7 +101,6 @@ public int hashCode() { return Objects.hash(input(), pattern(), isCaseInsensitive()); } - @Override public String getWriteableName() { return NAME; diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/IndexOf.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/IndexOf.java index b80e1ef7f0390..e7ead20b407cb 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/IndexOf.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/IndexOf.java @@ -68,8 +68,14 @@ protected TypeResolution resolveType() { @Override protected Pipe makePipe() { - return new IndexOfFunctionPipe(source(), this, Expressions.pipe(input), Expressions.pipe(substring), Expressions.pipe(start), - isCaseInsensitive()); + return new IndexOfFunctionPipe( + source(), + this, + Expressions.pipe(input), + Expressions.pipe(substring), + Expressions.pipe(start), + isCaseInsensitive() + ); } @Override @@ -97,18 +103,23 @@ public ScriptTemplate asScript() { } protected ScriptTemplate asScriptFrom(ScriptTemplate inputScript, ScriptTemplate substringScript, ScriptTemplate startScript) { - return new ScriptTemplate(format(Locale.ROOT, formatTemplate("{eql}.%s(%s,%s,%s,%s)"), - "indexOf", - inputScript.template(), - substringScript.template(), - startScript.template(), - "{}"), - paramsBuilder() - .script(inputScript.params()) + return new ScriptTemplate( + format( + Locale.ROOT, + formatTemplate("{eql}.%s(%s,%s,%s,%s)"), + "indexOf", + inputScript.template(), + substringScript.template(), + startScript.template(), + "{}" + ), + paramsBuilder().script(inputScript.params()) .script(substringScript.params()) .script(startScript.params()) .variable(isCaseInsensitive()) - .build(), dataType()); + .build(), + dataType() + ); } @Override diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/IndexOfFunctionProcessor.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/IndexOfFunctionProcessor.java index 29eeabc545923..19b8584106584 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/IndexOfFunctionProcessor.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/IndexOfFunctionProcessor.java @@ -119,7 +119,6 @@ public int hashCode() { return Objects.hash(input(), substring(), start(), isCaseInsensitive()); } - @Override public String getWriteableName() { return NAME; diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/Length.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/Length.java index 612cdeb0b0675..184c16680e703 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/Length.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/Length.java @@ -75,19 +75,20 @@ protected NodeInfo info() { public ScriptTemplate asScript() { ScriptTemplate inputScript = asScript(input); - return new ScriptTemplate(format(Locale.ROOT, formatTemplate("{eql}.%s(%s)"), - "length", - inputScript.template()), - paramsBuilder() - .script(inputScript.params()) - .build(), dataType()); + return new ScriptTemplate( + format(Locale.ROOT, formatTemplate("{eql}.%s(%s)"), "length", inputScript.template()), + paramsBuilder().script(inputScript.params()).build(), + dataType() + ); } @Override public ScriptTemplate scriptWithField(FieldAttribute field) { - return new ScriptTemplate(processScript(Scripts.DOC_VALUE), - paramsBuilder().variable(field.exactAttribute().name()).build(), - dataType()); + return new ScriptTemplate( + processScript(Scripts.DOC_VALUE), + paramsBuilder().variable(field.exactAttribute().name()).build(), + dataType() + ); } @Override diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/LengthFunctionProcessor.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/LengthFunctionProcessor.java index ce21bc6454338..d7788d4b3739f 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/LengthFunctionProcessor.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/LengthFunctionProcessor.java @@ -71,7 +71,6 @@ public int hashCode() { return Objects.hash(input()); } - @Override public String getWriteableName() { return NAME; diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/StringContainsFunctionPipe.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/StringContainsFunctionPipe.java index bf69962c7780a..b1ab0854e5a46 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/StringContainsFunctionPipe.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/StringContainsFunctionPipe.java @@ -102,7 +102,6 @@ public boolean equals(Object obj) { } StringContainsFunctionPipe other = (StringContainsFunctionPipe) obj; - return Objects.equals(string(), other.string()) - && Objects.equals(substring(), other.substring()); + return Objects.equals(string(), other.string()) && Objects.equals(substring(), other.substring()); } } diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/StringContainsFunctionProcessor.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/StringContainsFunctionProcessor.java index 4b352f892d9be..61b5bb6c0271a 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/StringContainsFunctionProcessor.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/StringContainsFunctionProcessor.java @@ -85,8 +85,7 @@ public boolean equals(Object obj) { } StringContainsFunctionProcessor other = (StringContainsFunctionProcessor) obj; - return Objects.equals(string(), other.string()) - && Objects.equals(substring(), other.substring()); + return Objects.equals(string(), other.string()) && Objects.equals(substring(), other.substring()); } @Override @@ -94,7 +93,6 @@ public int hashCode() { return Objects.hash(string(), substring()); } - @Override public String getWriteableName() { return NAME; diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/Substring.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/Substring.java index d9fdf1867bc3f..4fe946b5cd1c5 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/Substring.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/Substring.java @@ -98,23 +98,27 @@ public ScriptTemplate asScript() { } protected ScriptTemplate asScriptFrom(ScriptTemplate inputScript, ScriptTemplate startScript, ScriptTemplate endScript) { - return new ScriptTemplate(format(Locale.ROOT, formatTemplate("{eql}.%s(%s,%s,%s)"), + return new ScriptTemplate( + format( + Locale.ROOT, + formatTemplate("{eql}.%s(%s,%s,%s)"), "substring", inputScript.template(), startScript.template(), - endScript.template()), - paramsBuilder() - .script(inputScript.params()) - .script(startScript.params()) - .script(endScript.params()) - .build(), dataType()); + endScript.template() + ), + paramsBuilder().script(inputScript.params()).script(startScript.params()).script(endScript.params()).build(), + dataType() + ); } @Override public ScriptTemplate scriptWithField(FieldAttribute field) { - return new ScriptTemplate(processScript(Scripts.DOC_VALUE), - paramsBuilder().variable(field.exactAttribute().name()).build(), - dataType()); + return new ScriptTemplate( + processScript(Scripts.DOC_VALUE), + paramsBuilder().variable(field.exactAttribute().name()).build(), + dataType() + ); } @Override diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/SubstringFunctionPipe.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/SubstringFunctionPipe.java index 1c6e814f0c492..e06e80e5c8f45 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/SubstringFunctionPipe.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/SubstringFunctionPipe.java @@ -102,8 +102,6 @@ public boolean equals(Object obj) { } SubstringFunctionPipe other = (SubstringFunctionPipe) obj; - return Objects.equals(input(), other.input()) - && Objects.equals(start(), other.start()) - && Objects.equals(end(), other.end()); + return Objects.equals(input(), other.input()) && Objects.equals(start(), other.start()) && Objects.equals(end(), other.end()); } } diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/SubstringFunctionProcessor.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/SubstringFunctionProcessor.java index 12b237be54010..06f201765fe3a 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/SubstringFunctionProcessor.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/SubstringFunctionProcessor.java @@ -91,9 +91,7 @@ public boolean equals(Object obj) { } SubstringFunctionProcessor other = (SubstringFunctionProcessor) obj; - return Objects.equals(input(), other.input()) - && Objects.equals(start(), other.start()) - && Objects.equals(end(), other.end()); + return Objects.equals(input(), other.input()) && Objects.equals(start(), other.start()) && Objects.equals(end(), other.end()); } @Override @@ -101,7 +99,6 @@ public int hashCode() { return Objects.hash(input(), start(), end()); } - @Override public String getWriteableName() { return NAME; diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/ToString.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/ToString.java index 82a1e7699cf5e..c91f3219dc223 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/ToString.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/ToString.java @@ -78,19 +78,20 @@ protected NodeInfo info() { public ScriptTemplate asScript() { ScriptTemplate sourceScript = asScript(value); - return new ScriptTemplate(format(Locale.ROOT, formatTemplate("{eql}.%s(%s)"), - "string", - sourceScript.template()), - paramsBuilder() - .script(sourceScript.params()) - .build(), dataType()); + return new ScriptTemplate( + format(Locale.ROOT, formatTemplate("{eql}.%s(%s)"), "string", sourceScript.template()), + paramsBuilder().script(sourceScript.params()).build(), + dataType() + ); } @Override public ScriptTemplate scriptWithField(FieldAttribute field) { - return new ScriptTemplate(processScript(Scripts.DOC_VALUE), - paramsBuilder().variable(field.exactAttribute().name()).build(), - dataType()); + return new ScriptTemplate( + processScript(Scripts.DOC_VALUE), + paramsBuilder().variable(field.exactAttribute().name()).build(), + dataType() + ); } @Override diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/ToStringFunctionProcessor.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/ToStringFunctionProcessor.java index a6a7d2ee71040..8ea0730e9c820 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/ToStringFunctionProcessor.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/ToStringFunctionProcessor.java @@ -64,7 +64,6 @@ public int hashCode() { return Objects.hash(input()); } - @Override public String getWriteableName() { return NAME; diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/whitelist/InternalEqlScriptUtils.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/whitelist/InternalEqlScriptUtils.java index d76be851a1c88..d296102c66326 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/whitelist/InternalEqlScriptUtils.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/function/scalar/whitelist/InternalEqlScriptUtils.java @@ -33,8 +33,7 @@ */ public class InternalEqlScriptUtils extends InternalQlScriptUtils { - InternalEqlScriptUtils() { - } + InternalEqlScriptUtils() {} public static Boolean multiValueDocValues(Map> doc, String fieldName, Predicate script) { ScriptDocValues docValues = doc.get(fieldName); diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/predicate/operator/comparison/InsensitiveBinaryComparison.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/predicate/operator/comparison/InsensitiveBinaryComparison.java index 509ac902de22d..0a486b77ea839 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/predicate/operator/comparison/InsensitiveBinaryComparison.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/predicate/operator/comparison/InsensitiveBinaryComparison.java @@ -10,8 +10,8 @@ import org.elasticsearch.xpack.eql.expression.predicate.operator.comparison.InsensitiveBinaryComparisonProcessor.InsensitiveBinaryComparisonOperation; import org.elasticsearch.xpack.ql.expression.Expression; import org.elasticsearch.xpack.ql.expression.Expressions; -import org.elasticsearch.xpack.ql.expression.TypeResolutions.ParamOrdinal; import org.elasticsearch.xpack.ql.expression.TypeResolutions; +import org.elasticsearch.xpack.ql.expression.TypeResolutions.ParamOrdinal; import org.elasticsearch.xpack.ql.expression.gen.pipeline.Pipe; import org.elasticsearch.xpack.ql.expression.predicate.BinaryOperator; import org.elasticsearch.xpack.ql.tree.Source; @@ -25,8 +25,13 @@ public abstract class InsensitiveBinaryComparison extends BinaryOperator { +public class InsensitiveBinaryComparisonProcessor extends FunctionalEnumBinaryProcessor< + Object, + Object, + Boolean, + InsensitiveBinaryComparisonProcessor.InsensitiveBinaryComparisonOperation> { public enum InsensitiveBinaryComparisonOperation implements PredicateBiFunction { diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/predicate/operator/comparison/InsensitiveNotEquals.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/predicate/operator/comparison/InsensitiveNotEquals.java index 5f699cb46ddbf..148a335385487 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/predicate/operator/comparison/InsensitiveNotEquals.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/predicate/operator/comparison/InsensitiveNotEquals.java @@ -45,7 +45,3 @@ protected String regularOperatorSymbol() { return "not in"; } } - - - - diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/predicate/operator/comparison/InsensitiveWildcardNotEquals.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/predicate/operator/comparison/InsensitiveWildcardNotEquals.java index 3a8fa24b6fa63..a266105dd43d1 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/predicate/operator/comparison/InsensitiveWildcardNotEquals.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/predicate/operator/comparison/InsensitiveWildcardNotEquals.java @@ -16,9 +16,7 @@ public class InsensitiveWildcardNotEquals extends InsensitiveNotEquals { - public InsensitiveWildcardNotEquals(Source source, - Expression left, - Expression right, ZoneId zoneId) { + public InsensitiveWildcardNotEquals(Source source, Expression left, Expression right, ZoneId zoneId) { super(source, left, right, zoneId); } diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/predicate/operator/comparison/StringComparisons.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/predicate/operator/comparison/StringComparisons.java index d3aacb1ee385c..e5cd4545f8603 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/predicate/operator/comparison/StringComparisons.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/expression/predicate/operator/comparison/StringComparisons.java @@ -17,7 +17,7 @@ private StringComparisons() {} static Boolean insensitiveEquals(Object l, Object r) { if (l instanceof String && r instanceof String) { - return ((String)l).compareToIgnoreCase((String) r) == 0; + return ((String) l).compareToIgnoreCase((String) r) == 0; } if (l == null || r == null) { return null; diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/optimizer/Optimizer.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/optimizer/Optimizer.java index d2f936ba58712..5e80fd66fa6f3 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/optimizer/Optimizer.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/optimizer/Optimizer.java @@ -77,45 +77,41 @@ public LogicalPlan optimize(LogicalPlan verified) { @Override protected Iterable.Batch> batches() { - Batch substitutions = new Batch("Substitution", Limiter.ONCE, - new ReplaceWildcards(), - new ReplaceSurrogateFunction(), - new ReplaceRegexMatch(), - new ReplaceNullChecks(), - new AddMandatoryJoinKeyFilter() + Batch substitutions = new Batch( + "Substitution", + Limiter.ONCE, + new ReplaceWildcards(), + new ReplaceSurrogateFunction(), + new ReplaceRegexMatch(), + new ReplaceNullChecks(), + new AddMandatoryJoinKeyFilter() ); - Batch operators = new Batch("Operator Optimization", - new ConstantFolding(), - // boolean - new EqlBooleanSimplification(), - new LiteralsOnTheRight(), - new BinaryComparisonSimplification(), - new BooleanFunctionEqualsElimination(), - new CombineDisjunctionsToIn(), - new SimplifyComparisonsArithmetics(DataTypes::areCompatible), - // prune/elimination - new PruneFilters(), - new PruneLiteralsInOrderBy(), - new PruneCast(), - new CombineLimits(), - new PushDownAndCombineFilters() - ); + Batch operators = new Batch( + "Operator Optimization", + new ConstantFolding(), + // boolean + new EqlBooleanSimplification(), + new LiteralsOnTheRight(), + new BinaryComparisonSimplification(), + new BooleanFunctionEqualsElimination(), + new CombineDisjunctionsToIn(), + new SimplifyComparisonsArithmetics(DataTypes::areCompatible), + // prune/elimination + new PruneFilters(), + new PruneLiteralsInOrderBy(), + new PruneCast(), + new CombineLimits(), + new PushDownAndCombineFilters() + ); - Batch constraints = new Batch("Infer constraints", Limiter.ONCE, - new PropagateJoinKeyConstraints()); + Batch constraints = new Batch("Infer constraints", Limiter.ONCE, new PropagateJoinKeyConstraints()); - Batch ordering = new Batch("Implicit Order", - new SortByLimit(), - new PushDownOrderBy()); + Batch ordering = new Batch("Implicit Order", new SortByLimit(), new PushDownOrderBy()); - Batch local = new Batch("Skip Elasticsearch", - new SkipEmptyFilter(), - new SkipEmptyJoin(), - new SkipQueryOnLimitZero()); + Batch local = new Batch("Skip Elasticsearch", new SkipEmptyFilter(), new SkipEmptyJoin(), new SkipQueryOnLimitZero()); - Batch label = new Batch("Set as Optimized", Limiter.ONCE, - new SetAsOptimized()); + Batch label = new Batch("Set as Optimized", Limiter.ONCE, new SetAsOptimized()); return asList(substitutions, operators, constraints, operators, ordering, local, label); } @@ -384,14 +380,10 @@ protected LogicalPlan rule(Join join) { List constraints = new ArrayList<>(); // collect constraints for each filter - join.queries().forEach(k -> - k.forEachDown(Filter.class, f -> constraints.addAll(detectKeyConstraints(f.condition(), k)) - )); + join.queries().forEach(k -> k.forEachDown(Filter.class, f -> constraints.addAll(detectKeyConstraints(f.condition(), k)))); if (constraints.isEmpty() == false) { - List queries = join.queries().stream() - .map(k -> addConstraint(k, constraints)) - .collect(toList()); + List queries = join.queries().stream().map(k -> addConstraint(k, constraints)).collect(toList()); join = join.with(queries, join.until(), join.direction()); } @@ -427,18 +419,16 @@ private List detectKeyConstraints(Expression condition, KeyedFilter // adapt constraint to the given filter by replacing the keys accordingly in the expressions private KeyedFilter addConstraint(KeyedFilter k, List constraints) { - Expression constraint = Predicates.combineAnd(constraints.stream() - .map(c -> c.constraintFor(k)) - .filter(Objects::nonNull) - .collect(toList())); + Expression constraint = Predicates.combineAnd( + constraints.stream().map(c -> c.constraintFor(k)).filter(Objects::nonNull).collect(toList()) + ); return constraint != null - ? new KeyedFilter(k.source(), new Filter(k.source(), k.child(), constraint), k.keys(), k.timestamp(), k.tiebreaker()) - : k; + ? new KeyedFilter(k.source(), new Filter(k.source(), k.child(), constraint), k.keys(), k.timestamp(), k.tiebreaker()) + : k; } } - /** * Align the implicit order with the limit (head means ASC or tail means DESC). */ @@ -521,8 +511,12 @@ private static List changeOrderDirection(List orders, Order.OrderD boolean hasChanged = false; for (Order order : orders) { if (order.direction() != direction) { - order = new Order(order.source(), order.child(), direction, - direction == OrderDirection.ASC ? NullsPosition.FIRST : NullsPosition.LAST); + order = new Order( + order.source(), + order.child(), + direction, + direction == OrderDirection.ASC ? NullsPosition.FIRST : NullsPosition.LAST + ); hasChanged = true; } changed.add(order); diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/parser/AbstractBuilder.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/parser/AbstractBuilder.java index 09466502f4a6a..808ede5ee7d53 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/parser/AbstractBuilder.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/parser/AbstractBuilder.java @@ -41,8 +41,11 @@ public static String unquoteString(Source source) { // catch old method of ?" and ?' to define unescaped strings if (text.startsWith("?")) { - throw new ParsingException(source, - "Use triple double quotes [\"\"\"] to define unescaped string literals, not [?{}]", text.charAt(1)); + throw new ParsingException( + source, + "Use triple double quotes [\"\"\"] to define unescaped string literals, not [?{}]", + text.charAt(1) + ); } // unescaped strings can be interpreted directly @@ -106,8 +109,12 @@ private static int handleUnicodePoints(Source source, StringBuilder sb, String t unicodeSequence = text.substring(startIdx, endIdx); int length = unicodeSequence.length(); if (length < 2 || length > 8) { - throw new ParsingException(source, "Unicode sequence should use [2-8] hex digits, [{}] has [{}]", - text.substring(startIdx - 3, endIdx + 1), length); + throw new ParsingException( + source, + "Unicode sequence should use [2-8] hex digits, [{}] has [{}]", + text.substring(startIdx - 3, endIdx + 1), + length + ); } sb.append(hexToUnicode(source, unicodeSequence)); return endIdx; @@ -128,8 +135,7 @@ private static String hexToUnicode(Source source, String hex) { private static void checkForSingleQuotedString(Source source, String text, int i) { if (text.charAt(i) == '\'') { - throw new ParsingException(source, - "Use double quotes [\"] to define string literals, not single quotes [']"); + throw new ParsingException(source, "Use double quotes [\"] to define string literals, not single quotes [']"); } } } diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/parser/EqlBaseBaseListener.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/parser/EqlBaseBaseListener.java index 19e982069ac44..1184c7efeb0c2 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/parser/EqlBaseBaseListener.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/parser/EqlBaseBaseListener.java @@ -11,557 +11,739 @@ * of the available methods. */ class EqlBaseBaseListener implements EqlBaseListener { - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterSingleStatement(EqlBaseParser.SingleStatementContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitSingleStatement(EqlBaseParser.SingleStatementContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterSingleExpression(EqlBaseParser.SingleExpressionContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitSingleExpression(EqlBaseParser.SingleExpressionContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterStatement(EqlBaseParser.StatementContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitStatement(EqlBaseParser.StatementContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterQuery(EqlBaseParser.QueryContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitQuery(EqlBaseParser.QueryContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterSequenceParams(EqlBaseParser.SequenceParamsContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitSequenceParams(EqlBaseParser.SequenceParamsContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterSequence(EqlBaseParser.SequenceContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitSequence(EqlBaseParser.SequenceContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterJoin(EqlBaseParser.JoinContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitJoin(EqlBaseParser.JoinContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterPipe(EqlBaseParser.PipeContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitPipe(EqlBaseParser.PipeContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterJoinKeys(EqlBaseParser.JoinKeysContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitJoinKeys(EqlBaseParser.JoinKeysContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterJoinTerm(EqlBaseParser.JoinTermContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitJoinTerm(EqlBaseParser.JoinTermContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterSequenceTerm(EqlBaseParser.SequenceTermContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitSequenceTerm(EqlBaseParser.SequenceTermContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterSubquery(EqlBaseParser.SubqueryContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitSubquery(EqlBaseParser.SubqueryContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterEventQuery(EqlBaseParser.EventQueryContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitEventQuery(EqlBaseParser.EventQueryContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterEventFilter(EqlBaseParser.EventFilterContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitEventFilter(EqlBaseParser.EventFilterContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterExpression(EqlBaseParser.ExpressionContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitExpression(EqlBaseParser.ExpressionContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterLogicalNot(EqlBaseParser.LogicalNotContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitLogicalNot(EqlBaseParser.LogicalNotContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterBooleanDefault(EqlBaseParser.BooleanDefaultContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitBooleanDefault(EqlBaseParser.BooleanDefaultContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterProcessCheck(EqlBaseParser.ProcessCheckContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitProcessCheck(EqlBaseParser.ProcessCheckContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterLogicalBinary(EqlBaseParser.LogicalBinaryContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitLogicalBinary(EqlBaseParser.LogicalBinaryContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterValueExpressionDefault(EqlBaseParser.ValueExpressionDefaultContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitValueExpressionDefault(EqlBaseParser.ValueExpressionDefaultContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterComparison(EqlBaseParser.ComparisonContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitComparison(EqlBaseParser.ComparisonContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterOperatorExpressionDefault(EqlBaseParser.OperatorExpressionDefaultContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitOperatorExpressionDefault(EqlBaseParser.OperatorExpressionDefaultContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterArithmeticBinary(EqlBaseParser.ArithmeticBinaryContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitArithmeticBinary(EqlBaseParser.ArithmeticBinaryContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterArithmeticUnary(EqlBaseParser.ArithmeticUnaryContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitArithmeticUnary(EqlBaseParser.ArithmeticUnaryContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterPredicate(EqlBaseParser.PredicateContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitPredicate(EqlBaseParser.PredicateContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterConstantDefault(EqlBaseParser.ConstantDefaultContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitConstantDefault(EqlBaseParser.ConstantDefaultContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterFunction(EqlBaseParser.FunctionContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitFunction(EqlBaseParser.FunctionContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterDereference(EqlBaseParser.DereferenceContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitDereference(EqlBaseParser.DereferenceContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterParenthesizedExpression(EqlBaseParser.ParenthesizedExpressionContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitParenthesizedExpression(EqlBaseParser.ParenthesizedExpressionContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterFunctionExpression(EqlBaseParser.FunctionExpressionContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitFunctionExpression(EqlBaseParser.FunctionExpressionContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterFunctionName(EqlBaseParser.FunctionNameContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitFunctionName(EqlBaseParser.FunctionNameContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterNullLiteral(EqlBaseParser.NullLiteralContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitNullLiteral(EqlBaseParser.NullLiteralContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterNumericLiteral(EqlBaseParser.NumericLiteralContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitNumericLiteral(EqlBaseParser.NumericLiteralContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterBooleanLiteral(EqlBaseParser.BooleanLiteralContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitBooleanLiteral(EqlBaseParser.BooleanLiteralContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterStringLiteral(EqlBaseParser.StringLiteralContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitStringLiteral(EqlBaseParser.StringLiteralContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterComparisonOperator(EqlBaseParser.ComparisonOperatorContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitComparisonOperator(EqlBaseParser.ComparisonOperatorContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterBooleanValue(EqlBaseParser.BooleanValueContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitBooleanValue(EqlBaseParser.BooleanValueContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterQualifiedName(EqlBaseParser.QualifiedNameContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitQualifiedName(EqlBaseParser.QualifiedNameContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterIdentifier(EqlBaseParser.IdentifierContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitIdentifier(EqlBaseParser.IdentifierContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterTimeUnit(EqlBaseParser.TimeUnitContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitTimeUnit(EqlBaseParser.TimeUnitContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterDecimalLiteral(EqlBaseParser.DecimalLiteralContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitDecimalLiteral(EqlBaseParser.DecimalLiteralContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterIntegerLiteral(EqlBaseParser.IntegerLiteralContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitIntegerLiteral(EqlBaseParser.IntegerLiteralContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterString(EqlBaseParser.StringContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitString(EqlBaseParser.StringContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterEventValue(EqlBaseParser.EventValueContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitEventValue(EqlBaseParser.EventValueContext ctx) { } - - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterEveryRule(ParserRuleContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitEveryRule(ParserRuleContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void visitTerminal(TerminalNode node) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void visitErrorNode(ErrorNode node) { } + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterSingleStatement(EqlBaseParser.SingleStatementContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitSingleStatement(EqlBaseParser.SingleStatementContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterSingleExpression(EqlBaseParser.SingleExpressionContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitSingleExpression(EqlBaseParser.SingleExpressionContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterStatement(EqlBaseParser.StatementContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitStatement(EqlBaseParser.StatementContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterQuery(EqlBaseParser.QueryContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitQuery(EqlBaseParser.QueryContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterSequenceParams(EqlBaseParser.SequenceParamsContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitSequenceParams(EqlBaseParser.SequenceParamsContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterSequence(EqlBaseParser.SequenceContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitSequence(EqlBaseParser.SequenceContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterJoin(EqlBaseParser.JoinContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitJoin(EqlBaseParser.JoinContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterPipe(EqlBaseParser.PipeContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitPipe(EqlBaseParser.PipeContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterJoinKeys(EqlBaseParser.JoinKeysContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitJoinKeys(EqlBaseParser.JoinKeysContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterJoinTerm(EqlBaseParser.JoinTermContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitJoinTerm(EqlBaseParser.JoinTermContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterSequenceTerm(EqlBaseParser.SequenceTermContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitSequenceTerm(EqlBaseParser.SequenceTermContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterSubquery(EqlBaseParser.SubqueryContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitSubquery(EqlBaseParser.SubqueryContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterEventQuery(EqlBaseParser.EventQueryContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitEventQuery(EqlBaseParser.EventQueryContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterEventFilter(EqlBaseParser.EventFilterContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitEventFilter(EqlBaseParser.EventFilterContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterExpression(EqlBaseParser.ExpressionContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitExpression(EqlBaseParser.ExpressionContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterLogicalNot(EqlBaseParser.LogicalNotContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitLogicalNot(EqlBaseParser.LogicalNotContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterBooleanDefault(EqlBaseParser.BooleanDefaultContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitBooleanDefault(EqlBaseParser.BooleanDefaultContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterProcessCheck(EqlBaseParser.ProcessCheckContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitProcessCheck(EqlBaseParser.ProcessCheckContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterLogicalBinary(EqlBaseParser.LogicalBinaryContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitLogicalBinary(EqlBaseParser.LogicalBinaryContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterValueExpressionDefault(EqlBaseParser.ValueExpressionDefaultContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitValueExpressionDefault(EqlBaseParser.ValueExpressionDefaultContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterComparison(EqlBaseParser.ComparisonContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitComparison(EqlBaseParser.ComparisonContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterOperatorExpressionDefault(EqlBaseParser.OperatorExpressionDefaultContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitOperatorExpressionDefault(EqlBaseParser.OperatorExpressionDefaultContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterArithmeticBinary(EqlBaseParser.ArithmeticBinaryContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitArithmeticBinary(EqlBaseParser.ArithmeticBinaryContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterArithmeticUnary(EqlBaseParser.ArithmeticUnaryContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitArithmeticUnary(EqlBaseParser.ArithmeticUnaryContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterPredicate(EqlBaseParser.PredicateContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitPredicate(EqlBaseParser.PredicateContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterConstantDefault(EqlBaseParser.ConstantDefaultContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitConstantDefault(EqlBaseParser.ConstantDefaultContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterFunction(EqlBaseParser.FunctionContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitFunction(EqlBaseParser.FunctionContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterDereference(EqlBaseParser.DereferenceContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitDereference(EqlBaseParser.DereferenceContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterParenthesizedExpression(EqlBaseParser.ParenthesizedExpressionContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitParenthesizedExpression(EqlBaseParser.ParenthesizedExpressionContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterFunctionExpression(EqlBaseParser.FunctionExpressionContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitFunctionExpression(EqlBaseParser.FunctionExpressionContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterFunctionName(EqlBaseParser.FunctionNameContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitFunctionName(EqlBaseParser.FunctionNameContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterNullLiteral(EqlBaseParser.NullLiteralContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitNullLiteral(EqlBaseParser.NullLiteralContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterNumericLiteral(EqlBaseParser.NumericLiteralContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitNumericLiteral(EqlBaseParser.NumericLiteralContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterBooleanLiteral(EqlBaseParser.BooleanLiteralContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitBooleanLiteral(EqlBaseParser.BooleanLiteralContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterStringLiteral(EqlBaseParser.StringLiteralContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitStringLiteral(EqlBaseParser.StringLiteralContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterComparisonOperator(EqlBaseParser.ComparisonOperatorContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitComparisonOperator(EqlBaseParser.ComparisonOperatorContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterBooleanValue(EqlBaseParser.BooleanValueContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitBooleanValue(EqlBaseParser.BooleanValueContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterQualifiedName(EqlBaseParser.QualifiedNameContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitQualifiedName(EqlBaseParser.QualifiedNameContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterIdentifier(EqlBaseParser.IdentifierContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitIdentifier(EqlBaseParser.IdentifierContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterTimeUnit(EqlBaseParser.TimeUnitContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitTimeUnit(EqlBaseParser.TimeUnitContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterDecimalLiteral(EqlBaseParser.DecimalLiteralContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitDecimalLiteral(EqlBaseParser.DecimalLiteralContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterIntegerLiteral(EqlBaseParser.IntegerLiteralContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitIntegerLiteral(EqlBaseParser.IntegerLiteralContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterString(EqlBaseParser.StringContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitString(EqlBaseParser.StringContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterEventValue(EqlBaseParser.EventValueContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitEventValue(EqlBaseParser.EventValueContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterEveryRule(ParserRuleContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitEveryRule(ParserRuleContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void visitTerminal(TerminalNode node) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void visitErrorNode(ErrorNode node) {} } diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/parser/EqlBaseBaseVisitor.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/parser/EqlBaseBaseVisitor.java index b380f3dc807f6..fc401be6a1b5a 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/parser/EqlBaseBaseVisitor.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/parser/EqlBaseBaseVisitor.java @@ -1,5 +1,6 @@ // ANTLR GENERATED CODE: DO NOT EDIT package org.elasticsearch.xpack.eql.parser; + import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor; /** @@ -11,312 +12,487 @@ * operations with no return type. */ class EqlBaseBaseVisitor extends AbstractParseTreeVisitor implements EqlBaseVisitor { - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitSingleStatement(EqlBaseParser.SingleStatementContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitSingleExpression(EqlBaseParser.SingleExpressionContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitStatement(EqlBaseParser.StatementContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitQuery(EqlBaseParser.QueryContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitSequenceParams(EqlBaseParser.SequenceParamsContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitSequence(EqlBaseParser.SequenceContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitJoin(EqlBaseParser.JoinContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitPipe(EqlBaseParser.PipeContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitJoinKeys(EqlBaseParser.JoinKeysContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitJoinTerm(EqlBaseParser.JoinTermContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitSequenceTerm(EqlBaseParser.SequenceTermContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitSubquery(EqlBaseParser.SubqueryContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitEventQuery(EqlBaseParser.EventQueryContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitEventFilter(EqlBaseParser.EventFilterContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitExpression(EqlBaseParser.ExpressionContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitLogicalNot(EqlBaseParser.LogicalNotContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitBooleanDefault(EqlBaseParser.BooleanDefaultContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitProcessCheck(EqlBaseParser.ProcessCheckContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitLogicalBinary(EqlBaseParser.LogicalBinaryContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitValueExpressionDefault(EqlBaseParser.ValueExpressionDefaultContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitComparison(EqlBaseParser.ComparisonContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitOperatorExpressionDefault(EqlBaseParser.OperatorExpressionDefaultContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitArithmeticBinary(EqlBaseParser.ArithmeticBinaryContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitArithmeticUnary(EqlBaseParser.ArithmeticUnaryContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitPredicate(EqlBaseParser.PredicateContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitConstantDefault(EqlBaseParser.ConstantDefaultContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitFunction(EqlBaseParser.FunctionContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitDereference(EqlBaseParser.DereferenceContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitParenthesizedExpression(EqlBaseParser.ParenthesizedExpressionContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitFunctionExpression(EqlBaseParser.FunctionExpressionContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitFunctionName(EqlBaseParser.FunctionNameContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitNullLiteral(EqlBaseParser.NullLiteralContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitNumericLiteral(EqlBaseParser.NumericLiteralContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitBooleanLiteral(EqlBaseParser.BooleanLiteralContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitStringLiteral(EqlBaseParser.StringLiteralContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitComparisonOperator(EqlBaseParser.ComparisonOperatorContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitBooleanValue(EqlBaseParser.BooleanValueContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitQualifiedName(EqlBaseParser.QualifiedNameContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitIdentifier(EqlBaseParser.IdentifierContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitTimeUnit(EqlBaseParser.TimeUnitContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitDecimalLiteral(EqlBaseParser.DecimalLiteralContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitIntegerLiteral(EqlBaseParser.IntegerLiteralContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitString(EqlBaseParser.StringContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitEventValue(EqlBaseParser.EventValueContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitSingleStatement(EqlBaseParser.SingleStatementContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitSingleExpression(EqlBaseParser.SingleExpressionContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitStatement(EqlBaseParser.StatementContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitQuery(EqlBaseParser.QueryContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitSequenceParams(EqlBaseParser.SequenceParamsContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitSequence(EqlBaseParser.SequenceContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitJoin(EqlBaseParser.JoinContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitPipe(EqlBaseParser.PipeContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitJoinKeys(EqlBaseParser.JoinKeysContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitJoinTerm(EqlBaseParser.JoinTermContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitSequenceTerm(EqlBaseParser.SequenceTermContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitSubquery(EqlBaseParser.SubqueryContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitEventQuery(EqlBaseParser.EventQueryContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitEventFilter(EqlBaseParser.EventFilterContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitExpression(EqlBaseParser.ExpressionContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitLogicalNot(EqlBaseParser.LogicalNotContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitBooleanDefault(EqlBaseParser.BooleanDefaultContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitProcessCheck(EqlBaseParser.ProcessCheckContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitLogicalBinary(EqlBaseParser.LogicalBinaryContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitValueExpressionDefault(EqlBaseParser.ValueExpressionDefaultContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitComparison(EqlBaseParser.ComparisonContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitOperatorExpressionDefault(EqlBaseParser.OperatorExpressionDefaultContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitArithmeticBinary(EqlBaseParser.ArithmeticBinaryContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitArithmeticUnary(EqlBaseParser.ArithmeticUnaryContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitPredicate(EqlBaseParser.PredicateContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitConstantDefault(EqlBaseParser.ConstantDefaultContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitFunction(EqlBaseParser.FunctionContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitDereference(EqlBaseParser.DereferenceContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitParenthesizedExpression(EqlBaseParser.ParenthesizedExpressionContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitFunctionExpression(EqlBaseParser.FunctionExpressionContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitFunctionName(EqlBaseParser.FunctionNameContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitNullLiteral(EqlBaseParser.NullLiteralContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitNumericLiteral(EqlBaseParser.NumericLiteralContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitBooleanLiteral(EqlBaseParser.BooleanLiteralContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitStringLiteral(EqlBaseParser.StringLiteralContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitComparisonOperator(EqlBaseParser.ComparisonOperatorContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitBooleanValue(EqlBaseParser.BooleanValueContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitQualifiedName(EqlBaseParser.QualifiedNameContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitIdentifier(EqlBaseParser.IdentifierContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitTimeUnit(EqlBaseParser.TimeUnitContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitDecimalLiteral(EqlBaseParser.DecimalLiteralContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitIntegerLiteral(EqlBaseParser.IntegerLiteralContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitString(EqlBaseParser.StringContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitEventValue(EqlBaseParser.EventValueContext ctx) { + return visitChildren(ctx); + } } diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/parser/EqlBaseLexer.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/parser/EqlBaseLexer.java index 5d23efd4c5bf8..5d3acb6dbf500 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/parser/EqlBaseLexer.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/parser/EqlBaseLexer.java @@ -1,317 +1,452 @@ // ANTLR GENERATED CODE: DO NOT EDIT package org.elasticsearch.xpack.eql.parser; -import org.antlr.v4.runtime.Lexer; -import org.antlr.v4.runtime.CharStream; -import org.antlr.v4.runtime.Token; -import org.antlr.v4.runtime.TokenStream; + import org.antlr.v4.runtime.*; +import org.antlr.v4.runtime.CharStream; +import org.antlr.v4.runtime.Lexer; import org.antlr.v4.runtime.atn.*; import org.antlr.v4.runtime.dfa.DFA; import org.antlr.v4.runtime.misc.*; -@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"}) +@SuppressWarnings({ "all", "warnings", "unchecked", "unused", "cast" }) class EqlBaseLexer extends Lexer { - static { RuntimeMetaData.checkVersion("4.9.2", RuntimeMetaData.VERSION); } + static { + RuntimeMetaData.checkVersion("4.9.2", RuntimeMetaData.VERSION); + } - protected static final DFA[] _decisionToDFA; - protected static final PredictionContextCache _sharedContextCache = - new PredictionContextCache(); - public static final int - AND=1, ANY=2, BY=3, FALSE=4, IN=5, IN_INSENSITIVE=6, JOIN=7, LIKE=8, LIKE_INSENSITIVE=9, - MAXSPAN=10, NOT=11, NULL=12, OF=13, OR=14, REGEX=15, REGEX_INSENSITIVE=16, - SEQUENCE=17, TRUE=18, UNTIL=19, WHERE=20, WITH=21, SEQ=22, ASGN=23, EQ=24, - NEQ=25, LT=26, LTE=27, GT=28, GTE=29, PLUS=30, MINUS=31, ASTERISK=32, - SLASH=33, PERCENT=34, DOT=35, COMMA=36, LB=37, RB=38, LP=39, RP=40, PIPE=41, - OPTIONAL=42, STRING=43, INTEGER_VALUE=44, DECIMAL_VALUE=45, IDENTIFIER=46, - QUOTED_IDENTIFIER=47, TILDE_IDENTIFIER=48, LINE_COMMENT=49, BRACKETED_COMMENT=50, - WS=51; - public static String[] channelNames = { - "DEFAULT_TOKEN_CHANNEL", "HIDDEN" - }; + protected static final DFA[] _decisionToDFA; + protected static final PredictionContextCache _sharedContextCache = new PredictionContextCache(); + public static final int AND = 1, ANY = 2, BY = 3, FALSE = 4, IN = 5, IN_INSENSITIVE = 6, JOIN = 7, LIKE = 8, LIKE_INSENSITIVE = 9, + MAXSPAN = 10, NOT = 11, NULL = 12, OF = 13, OR = 14, REGEX = 15, REGEX_INSENSITIVE = 16, SEQUENCE = 17, TRUE = 18, UNTIL = 19, + WHERE = 20, WITH = 21, SEQ = 22, ASGN = 23, EQ = 24, NEQ = 25, LT = 26, LTE = 27, GT = 28, GTE = 29, PLUS = 30, MINUS = 31, + ASTERISK = 32, SLASH = 33, PERCENT = 34, DOT = 35, COMMA = 36, LB = 37, RB = 38, LP = 39, RP = 40, PIPE = 41, OPTIONAL = 42, + STRING = 43, INTEGER_VALUE = 44, DECIMAL_VALUE = 45, IDENTIFIER = 46, QUOTED_IDENTIFIER = 47, TILDE_IDENTIFIER = 48, LINE_COMMENT = + 49, BRACKETED_COMMENT = 50, WS = 51; + public static String[] channelNames = { "DEFAULT_TOKEN_CHANNEL", "HIDDEN" }; - public static String[] modeNames = { - "DEFAULT_MODE" - }; + public static String[] modeNames = { "DEFAULT_MODE" }; - private static String[] makeRuleNames() { - return new String[] { - "AND", "ANY", "BY", "FALSE", "IN", "IN_INSENSITIVE", "JOIN", "LIKE", - "LIKE_INSENSITIVE", "MAXSPAN", "NOT", "NULL", "OF", "OR", "REGEX", "REGEX_INSENSITIVE", - "SEQUENCE", "TRUE", "UNTIL", "WHERE", "WITH", "SEQ", "ASGN", "EQ", "NEQ", - "LT", "LTE", "GT", "GTE", "PLUS", "MINUS", "ASTERISK", "SLASH", "PERCENT", - "DOT", "COMMA", "LB", "RB", "LP", "RP", "PIPE", "OPTIONAL", "STRING_ESCAPE", - "HEX_DIGIT", "UNICODE_ESCAPE", "UNESCAPED_CHARS", "STRING", "INTEGER_VALUE", - "DECIMAL_VALUE", "IDENTIFIER", "QUOTED_IDENTIFIER", "TILDE_IDENTIFIER", - "EXPONENT", "DIGIT", "LETTER", "LINE_COMMENT", "BRACKETED_COMMENT", "WS" - }; - } - public static final String[] ruleNames = makeRuleNames(); + private static String[] makeRuleNames() { + return new String[] { + "AND", + "ANY", + "BY", + "FALSE", + "IN", + "IN_INSENSITIVE", + "JOIN", + "LIKE", + "LIKE_INSENSITIVE", + "MAXSPAN", + "NOT", + "NULL", + "OF", + "OR", + "REGEX", + "REGEX_INSENSITIVE", + "SEQUENCE", + "TRUE", + "UNTIL", + "WHERE", + "WITH", + "SEQ", + "ASGN", + "EQ", + "NEQ", + "LT", + "LTE", + "GT", + "GTE", + "PLUS", + "MINUS", + "ASTERISK", + "SLASH", + "PERCENT", + "DOT", + "COMMA", + "LB", + "RB", + "LP", + "RP", + "PIPE", + "OPTIONAL", + "STRING_ESCAPE", + "HEX_DIGIT", + "UNICODE_ESCAPE", + "UNESCAPED_CHARS", + "STRING", + "INTEGER_VALUE", + "DECIMAL_VALUE", + "IDENTIFIER", + "QUOTED_IDENTIFIER", + "TILDE_IDENTIFIER", + "EXPONENT", + "DIGIT", + "LETTER", + "LINE_COMMENT", + "BRACKETED_COMMENT", + "WS" }; + } + + public static final String[] ruleNames = makeRuleNames(); - private static String[] makeLiteralNames() { - return new String[] { - null, "'and'", "'any'", "'by'", "'false'", "'in'", "'in~'", "'join'", - "'like'", "'like~'", "'maxspan'", "'not'", "'null'", "'of'", "'or'", - "'regex'", "'regex~'", "'sequence'", "'true'", "'until'", "'where'", - "'with'", "':'", "'='", "'=='", "'!='", "'<'", "'<='", "'>'", "'>='", - "'+'", "'-'", "'*'", "'/'", "'%'", "'.'", "','", "'['", "']'", "'('", - "')'", "'|'", "'?'" - }; - } - private static final String[] _LITERAL_NAMES = makeLiteralNames(); - private static String[] makeSymbolicNames() { - return new String[] { - null, "AND", "ANY", "BY", "FALSE", "IN", "IN_INSENSITIVE", "JOIN", "LIKE", - "LIKE_INSENSITIVE", "MAXSPAN", "NOT", "NULL", "OF", "OR", "REGEX", "REGEX_INSENSITIVE", - "SEQUENCE", "TRUE", "UNTIL", "WHERE", "WITH", "SEQ", "ASGN", "EQ", "NEQ", - "LT", "LTE", "GT", "GTE", "PLUS", "MINUS", "ASTERISK", "SLASH", "PERCENT", - "DOT", "COMMA", "LB", "RB", "LP", "RP", "PIPE", "OPTIONAL", "STRING", - "INTEGER_VALUE", "DECIMAL_VALUE", "IDENTIFIER", "QUOTED_IDENTIFIER", - "TILDE_IDENTIFIER", "LINE_COMMENT", "BRACKETED_COMMENT", "WS" - }; - } - private static final String[] _SYMBOLIC_NAMES = makeSymbolicNames(); - public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); + private static String[] makeLiteralNames() { + return new String[] { + null, + "'and'", + "'any'", + "'by'", + "'false'", + "'in'", + "'in~'", + "'join'", + "'like'", + "'like~'", + "'maxspan'", + "'not'", + "'null'", + "'of'", + "'or'", + "'regex'", + "'regex~'", + "'sequence'", + "'true'", + "'until'", + "'where'", + "'with'", + "':'", + "'='", + "'=='", + "'!='", + "'<'", + "'<='", + "'>'", + "'>='", + "'+'", + "'-'", + "'*'", + "'/'", + "'%'", + "'.'", + "','", + "'['", + "']'", + "'('", + "')'", + "'|'", + "'?'" }; + } - /** - * @deprecated Use {@link #VOCABULARY} instead. - */ - @Deprecated - public static final String[] tokenNames; - static { - tokenNames = new String[_SYMBOLIC_NAMES.length]; - for (int i = 0; i < tokenNames.length; i++) { - tokenNames[i] = VOCABULARY.getLiteralName(i); - if (tokenNames[i] == null) { - tokenNames[i] = VOCABULARY.getSymbolicName(i); - } + private static final String[] _LITERAL_NAMES = makeLiteralNames(); - if (tokenNames[i] == null) { - tokenNames[i] = ""; - } + private static String[] makeSymbolicNames() { + return new String[] { + null, + "AND", + "ANY", + "BY", + "FALSE", + "IN", + "IN_INSENSITIVE", + "JOIN", + "LIKE", + "LIKE_INSENSITIVE", + "MAXSPAN", + "NOT", + "NULL", + "OF", + "OR", + "REGEX", + "REGEX_INSENSITIVE", + "SEQUENCE", + "TRUE", + "UNTIL", + "WHERE", + "WITH", + "SEQ", + "ASGN", + "EQ", + "NEQ", + "LT", + "LTE", + "GT", + "GTE", + "PLUS", + "MINUS", + "ASTERISK", + "SLASH", + "PERCENT", + "DOT", + "COMMA", + "LB", + "RB", + "LP", + "RP", + "PIPE", + "OPTIONAL", + "STRING", + "INTEGER_VALUE", + "DECIMAL_VALUE", + "IDENTIFIER", + "QUOTED_IDENTIFIER", + "TILDE_IDENTIFIER", + "LINE_COMMENT", + "BRACKETED_COMMENT", + "WS" }; } - } - @Override - @Deprecated - public String[] getTokenNames() { - return tokenNames; - } + private static final String[] _SYMBOLIC_NAMES = makeSymbolicNames(); + public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); - @Override + /** + * @deprecated Use {@link #VOCABULARY} instead. + */ + @Deprecated + public static final String[] tokenNames; + static { + tokenNames = new String[_SYMBOLIC_NAMES.length]; + for (int i = 0; i < tokenNames.length; i++) { + tokenNames[i] = VOCABULARY.getLiteralName(i); + if (tokenNames[i] == null) { + tokenNames[i] = VOCABULARY.getSymbolicName(i); + } - public Vocabulary getVocabulary() { - return VOCABULARY; - } + if (tokenNames[i] == null) { + tokenNames[i] = ""; + } + } + } + @Override + @Deprecated + public String[] getTokenNames() { + return tokenNames; + } - public EqlBaseLexer(CharStream input) { - super(input); - _interp = new LexerATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache); - } + @Override - @Override - public String getGrammarFileName() { return "EqlBase.g4"; } + public Vocabulary getVocabulary() { + return VOCABULARY; + } + + public EqlBaseLexer(CharStream input) { + super(input); + _interp = new LexerATNSimulator(this, _ATN, _decisionToDFA, _sharedContextCache); + } + + @Override + public String getGrammarFileName() { + return "EqlBase.g4"; + } - @Override - public String[] getRuleNames() { return ruleNames; } + @Override + public String[] getRuleNames() { + return ruleNames; + } - @Override - public String getSerializedATN() { return _serializedATN; } + @Override + public String getSerializedATN() { + return _serializedATN; + } - @Override - public String[] getChannelNames() { return channelNames; } + @Override + public String[] getChannelNames() { + return channelNames; + } - @Override - public String[] getModeNames() { return modeNames; } + @Override + public String[] getModeNames() { + return modeNames; + } - @Override - public ATN getATN() { return _ATN; } + @Override + public ATN getATN() { + return _ATN; + } - public static final String _serializedATN = - "\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\2\65\u01eb\b\1\4\2"+ - "\t\2\4\3\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b\t\b\4\t\t\t\4\n\t\n\4"+ - "\13\t\13\4\f\t\f\4\r\t\r\4\16\t\16\4\17\t\17\4\20\t\20\4\21\t\21\4\22"+ - "\t\22\4\23\t\23\4\24\t\24\4\25\t\25\4\26\t\26\4\27\t\27\4\30\t\30\4\31"+ - "\t\31\4\32\t\32\4\33\t\33\4\34\t\34\4\35\t\35\4\36\t\36\4\37\t\37\4 \t"+ - " \4!\t!\4\"\t\"\4#\t#\4$\t$\4%\t%\4&\t&\4\'\t\'\4(\t(\4)\t)\4*\t*\4+\t"+ - "+\4,\t,\4-\t-\4.\t.\4/\t/\4\60\t\60\4\61\t\61\4\62\t\62\4\63\t\63\4\64"+ - "\t\64\4\65\t\65\4\66\t\66\4\67\t\67\48\t8\49\t9\4:\t:\4;\t;\3\2\3\2\3"+ - "\2\3\2\3\3\3\3\3\3\3\3\3\4\3\4\3\4\3\5\3\5\3\5\3\5\3\5\3\5\3\6\3\6\3\6"+ - "\3\7\3\7\3\7\3\7\3\b\3\b\3\b\3\b\3\b\3\t\3\t\3\t\3\t\3\t\3\n\3\n\3\n\3"+ - "\n\3\n\3\n\3\13\3\13\3\13\3\13\3\13\3\13\3\13\3\13\3\f\3\f\3\f\3\f\3\r"+ - "\3\r\3\r\3\r\3\r\3\16\3\16\3\16\3\17\3\17\3\17\3\20\3\20\3\20\3\20\3\20"+ - "\3\20\3\21\3\21\3\21\3\21\3\21\3\21\3\21\3\22\3\22\3\22\3\22\3\22\3\22"+ - "\3\22\3\22\3\22\3\23\3\23\3\23\3\23\3\23\3\24\3\24\3\24\3\24\3\24\3\24"+ - "\3\25\3\25\3\25\3\25\3\25\3\25\3\26\3\26\3\26\3\26\3\26\3\27\3\27\3\30"+ - "\3\30\3\31\3\31\3\31\3\32\3\32\3\32\3\33\3\33\3\34\3\34\3\34\3\35\3\35"+ - "\3\36\3\36\3\36\3\37\3\37\3 \3 \3!\3!\3\"\3\"\3#\3#\3$\3$\3%\3%\3&\3&"+ - "\3\'\3\'\3(\3(\3)\3)\3*\3*\3+\3+\3,\3,\3,\3-\3-\3.\3.\3.\3.\3.\6.\u011b"+ - "\n.\r.\16.\u011c\3.\3.\3/\3/\3\60\3\60\3\60\3\60\7\60\u0127\n\60\f\60"+ - "\16\60\u012a\13\60\3\60\3\60\3\60\3\60\3\60\3\60\7\60\u0132\n\60\f\60"+ - "\16\60\u0135\13\60\3\60\3\60\3\60\3\60\3\60\5\60\u013c\n\60\3\60\5\60"+ - "\u013f\n\60\3\60\3\60\3\60\3\60\7\60\u0145\n\60\f\60\16\60\u0148\13\60"+ - "\3\60\3\60\3\60\3\60\3\60\3\60\3\60\7\60\u0151\n\60\f\60\16\60\u0154\13"+ - "\60\3\60\3\60\3\60\3\60\3\60\3\60\3\60\7\60\u015d\n\60\f\60\16\60\u0160"+ - "\13\60\3\60\5\60\u0163\n\60\3\61\6\61\u0166\n\61\r\61\16\61\u0167\3\62"+ - "\6\62\u016b\n\62\r\62\16\62\u016c\3\62\3\62\7\62\u0171\n\62\f\62\16\62"+ - "\u0174\13\62\3\62\3\62\6\62\u0178\n\62\r\62\16\62\u0179\3\62\6\62\u017d"+ - "\n\62\r\62\16\62\u017e\3\62\3\62\7\62\u0183\n\62\f\62\16\62\u0186\13\62"+ - "\5\62\u0188\n\62\3\62\3\62\3\62\3\62\6\62\u018e\n\62\r\62\16\62\u018f"+ - "\3\62\3\62\5\62\u0194\n\62\3\63\3\63\5\63\u0198\n\63\3\63\3\63\3\63\7"+ - "\63\u019d\n\63\f\63\16\63\u01a0\13\63\3\64\3\64\3\64\3\64\7\64\u01a6\n"+ - "\64\f\64\16\64\u01a9\13\64\3\64\3\64\3\65\3\65\3\65\3\65\7\65\u01b1\n"+ - "\65\f\65\16\65\u01b4\13\65\3\65\3\65\3\66\3\66\5\66\u01ba\n\66\3\66\6"+ - "\66\u01bd\n\66\r\66\16\66\u01be\3\67\3\67\38\38\39\39\39\39\79\u01c9\n"+ - "9\f9\169\u01cc\139\39\59\u01cf\n9\39\59\u01d2\n9\39\39\3:\3:\3:\3:\3:"+ - "\7:\u01db\n:\f:\16:\u01de\13:\3:\3:\3:\3:\3:\3;\6;\u01e6\n;\r;\16;\u01e7"+ - "\3;\3;\4\u0133\u01dc\2<\3\3\5\4\7\5\t\6\13\7\r\b\17\t\21\n\23\13\25\f"+ - "\27\r\31\16\33\17\35\20\37\21!\22#\23%\24\'\25)\26+\27-\30/\31\61\32\63"+ - "\33\65\34\67\359\36;\37= ?!A\"C#E$G%I&K\'M(O)Q*S+U,W\2Y\2[\2]\2_-a.c/"+ - "e\60g\61i\62k\2m\2o\2q\63s\64u\65\3\2\20\n\2$$))^^ddhhppttvv\5\2\62;C"+ - "Hch\6\2\f\f\17\17$$^^\4\2\f\f\17\17\6\2\f\f\17\17))^^\5\2\f\f\17\17$$"+ - "\5\2\f\f\17\17))\4\2BBaa\3\2bb\4\2GGgg\4\2--//\3\2\62;\4\2C\\c|\5\2\13"+ - "\f\17\17\"\"\2\u0210\2\3\3\2\2\2\2\5\3\2\2\2\2\7\3\2\2\2\2\t\3\2\2\2\2"+ - "\13\3\2\2\2\2\r\3\2\2\2\2\17\3\2\2\2\2\21\3\2\2\2\2\23\3\2\2\2\2\25\3"+ - "\2\2\2\2\27\3\2\2\2\2\31\3\2\2\2\2\33\3\2\2\2\2\35\3\2\2\2\2\37\3\2\2"+ - "\2\2!\3\2\2\2\2#\3\2\2\2\2%\3\2\2\2\2\'\3\2\2\2\2)\3\2\2\2\2+\3\2\2\2"+ - "\2-\3\2\2\2\2/\3\2\2\2\2\61\3\2\2\2\2\63\3\2\2\2\2\65\3\2\2\2\2\67\3\2"+ - "\2\2\29\3\2\2\2\2;\3\2\2\2\2=\3\2\2\2\2?\3\2\2\2\2A\3\2\2\2\2C\3\2\2\2"+ - "\2E\3\2\2\2\2G\3\2\2\2\2I\3\2\2\2\2K\3\2\2\2\2M\3\2\2\2\2O\3\2\2\2\2Q"+ - "\3\2\2\2\2S\3\2\2\2\2U\3\2\2\2\2_\3\2\2\2\2a\3\2\2\2\2c\3\2\2\2\2e\3\2"+ - "\2\2\2g\3\2\2\2\2i\3\2\2\2\2q\3\2\2\2\2s\3\2\2\2\2u\3\2\2\2\3w\3\2\2\2"+ - "\5{\3\2\2\2\7\177\3\2\2\2\t\u0082\3\2\2\2\13\u0088\3\2\2\2\r\u008b\3\2"+ - "\2\2\17\u008f\3\2\2\2\21\u0094\3\2\2\2\23\u0099\3\2\2\2\25\u009f\3\2\2"+ - "\2\27\u00a7\3\2\2\2\31\u00ab\3\2\2\2\33\u00b0\3\2\2\2\35\u00b3\3\2\2\2"+ - "\37\u00b6\3\2\2\2!\u00bc\3\2\2\2#\u00c3\3\2\2\2%\u00cc\3\2\2\2\'\u00d1"+ - "\3\2\2\2)\u00d7\3\2\2\2+\u00dd\3\2\2\2-\u00e2\3\2\2\2/\u00e4\3\2\2\2\61"+ - "\u00e6\3\2\2\2\63\u00e9\3\2\2\2\65\u00ec\3\2\2\2\67\u00ee\3\2\2\29\u00f1"+ - "\3\2\2\2;\u00f3\3\2\2\2=\u00f6\3\2\2\2?\u00f8\3\2\2\2A\u00fa\3\2\2\2C"+ - "\u00fc\3\2\2\2E\u00fe\3\2\2\2G\u0100\3\2\2\2I\u0102\3\2\2\2K\u0104\3\2"+ - "\2\2M\u0106\3\2\2\2O\u0108\3\2\2\2Q\u010a\3\2\2\2S\u010c\3\2\2\2U\u010e"+ - "\3\2\2\2W\u0110\3\2\2\2Y\u0113\3\2\2\2[\u0115\3\2\2\2]\u0120\3\2\2\2_"+ - "\u0162\3\2\2\2a\u0165\3\2\2\2c\u0193\3\2\2\2e\u0197\3\2\2\2g\u01a1\3\2"+ - "\2\2i\u01ac\3\2\2\2k\u01b7\3\2\2\2m\u01c0\3\2\2\2o\u01c2\3\2\2\2q\u01c4"+ - "\3\2\2\2s\u01d5\3\2\2\2u\u01e5\3\2\2\2wx\7c\2\2xy\7p\2\2yz\7f\2\2z\4\3"+ - "\2\2\2{|\7c\2\2|}\7p\2\2}~\7{\2\2~\6\3\2\2\2\177\u0080\7d\2\2\u0080\u0081"+ - "\7{\2\2\u0081\b\3\2\2\2\u0082\u0083\7h\2\2\u0083\u0084\7c\2\2\u0084\u0085"+ - "\7n\2\2\u0085\u0086\7u\2\2\u0086\u0087\7g\2\2\u0087\n\3\2\2\2\u0088\u0089"+ - "\7k\2\2\u0089\u008a\7p\2\2\u008a\f\3\2\2\2\u008b\u008c\7k\2\2\u008c\u008d"+ - "\7p\2\2\u008d\u008e\7\u0080\2\2\u008e\16\3\2\2\2\u008f\u0090\7l\2\2\u0090"+ - "\u0091\7q\2\2\u0091\u0092\7k\2\2\u0092\u0093\7p\2\2\u0093\20\3\2\2\2\u0094"+ - "\u0095\7n\2\2\u0095\u0096\7k\2\2\u0096\u0097\7m\2\2\u0097\u0098\7g\2\2"+ - "\u0098\22\3\2\2\2\u0099\u009a\7n\2\2\u009a\u009b\7k\2\2\u009b\u009c\7"+ - "m\2\2\u009c\u009d\7g\2\2\u009d\u009e\7\u0080\2\2\u009e\24\3\2\2\2\u009f"+ - "\u00a0\7o\2\2\u00a0\u00a1\7c\2\2\u00a1\u00a2\7z\2\2\u00a2\u00a3\7u\2\2"+ - "\u00a3\u00a4\7r\2\2\u00a4\u00a5\7c\2\2\u00a5\u00a6\7p\2\2\u00a6\26\3\2"+ - "\2\2\u00a7\u00a8\7p\2\2\u00a8\u00a9\7q\2\2\u00a9\u00aa\7v\2\2\u00aa\30"+ - "\3\2\2\2\u00ab\u00ac\7p\2\2\u00ac\u00ad\7w\2\2\u00ad\u00ae\7n\2\2\u00ae"+ - "\u00af\7n\2\2\u00af\32\3\2\2\2\u00b0\u00b1\7q\2\2\u00b1\u00b2\7h\2\2\u00b2"+ - "\34\3\2\2\2\u00b3\u00b4\7q\2\2\u00b4\u00b5\7t\2\2\u00b5\36\3\2\2\2\u00b6"+ - "\u00b7\7t\2\2\u00b7\u00b8\7g\2\2\u00b8\u00b9\7i\2\2\u00b9\u00ba\7g\2\2"+ - "\u00ba\u00bb\7z\2\2\u00bb \3\2\2\2\u00bc\u00bd\7t\2\2\u00bd\u00be\7g\2"+ - "\2\u00be\u00bf\7i\2\2\u00bf\u00c0\7g\2\2\u00c0\u00c1\7z\2\2\u00c1\u00c2"+ - "\7\u0080\2\2\u00c2\"\3\2\2\2\u00c3\u00c4\7u\2\2\u00c4\u00c5\7g\2\2\u00c5"+ - "\u00c6\7s\2\2\u00c6\u00c7\7w\2\2\u00c7\u00c8\7g\2\2\u00c8\u00c9\7p\2\2"+ - "\u00c9\u00ca\7e\2\2\u00ca\u00cb\7g\2\2\u00cb$\3\2\2\2\u00cc\u00cd\7v\2"+ - "\2\u00cd\u00ce\7t\2\2\u00ce\u00cf\7w\2\2\u00cf\u00d0\7g\2\2\u00d0&\3\2"+ - "\2\2\u00d1\u00d2\7w\2\2\u00d2\u00d3\7p\2\2\u00d3\u00d4\7v\2\2\u00d4\u00d5"+ - "\7k\2\2\u00d5\u00d6\7n\2\2\u00d6(\3\2\2\2\u00d7\u00d8\7y\2\2\u00d8\u00d9"+ - "\7j\2\2\u00d9\u00da\7g\2\2\u00da\u00db\7t\2\2\u00db\u00dc\7g\2\2\u00dc"+ - "*\3\2\2\2\u00dd\u00de\7y\2\2\u00de\u00df\7k\2\2\u00df\u00e0\7v\2\2\u00e0"+ - "\u00e1\7j\2\2\u00e1,\3\2\2\2\u00e2\u00e3\7<\2\2\u00e3.\3\2\2\2\u00e4\u00e5"+ - "\7?\2\2\u00e5\60\3\2\2\2\u00e6\u00e7\7?\2\2\u00e7\u00e8\7?\2\2\u00e8\62"+ - "\3\2\2\2\u00e9\u00ea\7#\2\2\u00ea\u00eb\7?\2\2\u00eb\64\3\2\2\2\u00ec"+ - "\u00ed\7>\2\2\u00ed\66\3\2\2\2\u00ee\u00ef\7>\2\2\u00ef\u00f0\7?\2\2\u00f0"+ - "8\3\2\2\2\u00f1\u00f2\7@\2\2\u00f2:\3\2\2\2\u00f3\u00f4\7@\2\2\u00f4\u00f5"+ - "\7?\2\2\u00f5<\3\2\2\2\u00f6\u00f7\7-\2\2\u00f7>\3\2\2\2\u00f8\u00f9\7"+ - "/\2\2\u00f9@\3\2\2\2\u00fa\u00fb\7,\2\2\u00fbB\3\2\2\2\u00fc\u00fd\7\61"+ - "\2\2\u00fdD\3\2\2\2\u00fe\u00ff\7\'\2\2\u00ffF\3\2\2\2\u0100\u0101\7\60"+ - "\2\2\u0101H\3\2\2\2\u0102\u0103\7.\2\2\u0103J\3\2\2\2\u0104\u0105\7]\2"+ - "\2\u0105L\3\2\2\2\u0106\u0107\7_\2\2\u0107N\3\2\2\2\u0108\u0109\7*\2\2"+ - "\u0109P\3\2\2\2\u010a\u010b\7+\2\2\u010bR\3\2\2\2\u010c\u010d\7~\2\2\u010d"+ - "T\3\2\2\2\u010e\u010f\7A\2\2\u010fV\3\2\2\2\u0110\u0111\7^\2\2\u0111\u0112"+ - "\t\2\2\2\u0112X\3\2\2\2\u0113\u0114\t\3\2\2\u0114Z\3\2\2\2\u0115\u0116"+ - "\7^\2\2\u0116\u0117\7w\2\2\u0117\u0118\3\2\2\2\u0118\u011a\7}\2\2\u0119"+ - "\u011b\5Y-\2\u011a\u0119\3\2\2\2\u011b\u011c\3\2\2\2\u011c\u011a\3\2\2"+ - "\2\u011c\u011d\3\2\2\2\u011d\u011e\3\2\2\2\u011e\u011f\7\177\2\2\u011f"+ - "\\\3\2\2\2\u0120\u0121\n\4\2\2\u0121^\3\2\2\2\u0122\u0128\7$\2\2\u0123"+ - "\u0127\5W,\2\u0124\u0127\5[.\2\u0125\u0127\5]/\2\u0126\u0123\3\2\2\2\u0126"+ - "\u0124\3\2\2\2\u0126\u0125\3\2\2\2\u0127\u012a\3\2\2\2\u0128\u0126\3\2"+ - "\2\2\u0128\u0129\3\2\2\2\u0129\u012b\3\2\2\2\u012a\u0128\3\2\2\2\u012b"+ - "\u0163\7$\2\2\u012c\u012d\7$\2\2\u012d\u012e\7$\2\2\u012e\u012f\7$\2\2"+ - "\u012f\u0133\3\2\2\2\u0130\u0132\n\5\2\2\u0131\u0130\3\2\2\2\u0132\u0135"+ - "\3\2\2\2\u0133\u0134\3\2\2\2\u0133\u0131\3\2\2\2\u0134\u0136\3\2\2\2\u0135"+ - "\u0133\3\2\2\2\u0136\u0137\7$\2\2\u0137\u0138\7$\2\2\u0138\u0139\7$\2"+ - "\2\u0139\u013b\3\2\2\2\u013a\u013c\7$\2\2\u013b\u013a\3\2\2\2\u013b\u013c"+ - "\3\2\2\2\u013c\u013e\3\2\2\2\u013d\u013f\7$\2\2\u013e\u013d\3\2\2\2\u013e"+ - "\u013f\3\2\2\2\u013f\u0163\3\2\2\2\u0140\u0146\7)\2\2\u0141\u0142\7^\2"+ - "\2\u0142\u0145\t\2\2\2\u0143\u0145\n\6\2\2\u0144\u0141\3\2\2\2\u0144\u0143"+ - "\3\2\2\2\u0145\u0148\3\2\2\2\u0146\u0144\3\2\2\2\u0146\u0147\3\2\2\2\u0147"+ - "\u0149\3\2\2\2\u0148\u0146\3\2\2\2\u0149\u0163\7)\2\2\u014a\u014b\7A\2"+ - "\2\u014b\u014c\7$\2\2\u014c\u0152\3\2\2\2\u014d\u014e\7^\2\2\u014e\u0151"+ - "\7$\2\2\u014f\u0151\n\7\2\2\u0150\u014d\3\2\2\2\u0150\u014f\3\2\2\2\u0151"+ - "\u0154\3\2\2\2\u0152\u0150\3\2\2\2\u0152\u0153\3\2\2\2\u0153\u0155\3\2"+ - "\2\2\u0154\u0152\3\2\2\2\u0155\u0163\7$\2\2\u0156\u0157\7A\2\2\u0157\u0158"+ - "\7)\2\2\u0158\u015e\3\2\2\2\u0159\u015a\7^\2\2\u015a\u015d\7)\2\2\u015b"+ - "\u015d\n\b\2\2\u015c\u0159\3\2\2\2\u015c\u015b\3\2\2\2\u015d\u0160\3\2"+ - "\2\2\u015e\u015c\3\2\2\2\u015e\u015f\3\2\2\2\u015f\u0161\3\2\2\2\u0160"+ - "\u015e\3\2\2\2\u0161\u0163\7)\2\2\u0162\u0122\3\2\2\2\u0162\u012c\3\2"+ - "\2\2\u0162\u0140\3\2\2\2\u0162\u014a\3\2\2\2\u0162\u0156\3\2\2\2\u0163"+ - "`\3\2\2\2\u0164\u0166\5m\67\2\u0165\u0164\3\2\2\2\u0166\u0167\3\2\2\2"+ - "\u0167\u0165\3\2\2\2\u0167\u0168\3\2\2\2\u0168b\3\2\2\2\u0169\u016b\5"+ - "m\67\2\u016a\u0169\3\2\2\2\u016b\u016c\3\2\2\2\u016c\u016a\3\2\2\2\u016c"+ - "\u016d\3\2\2\2\u016d\u016e\3\2\2\2\u016e\u0172\5G$\2\u016f\u0171\5m\67"+ - "\2\u0170\u016f\3\2\2\2\u0171\u0174\3\2\2\2\u0172\u0170\3\2\2\2\u0172\u0173"+ - "\3\2\2\2\u0173\u0194\3\2\2\2\u0174\u0172\3\2\2\2\u0175\u0177\5G$\2\u0176"+ - "\u0178\5m\67\2\u0177\u0176\3\2\2\2\u0178\u0179\3\2\2\2\u0179\u0177\3\2"+ - "\2\2\u0179\u017a\3\2\2\2\u017a\u0194\3\2\2\2\u017b\u017d\5m\67\2\u017c"+ - "\u017b\3\2\2\2\u017d\u017e\3\2\2\2\u017e\u017c\3\2\2\2\u017e\u017f\3\2"+ - "\2\2\u017f\u0187\3\2\2\2\u0180\u0184\5G$\2\u0181\u0183\5m\67\2\u0182\u0181"+ - "\3\2\2\2\u0183\u0186\3\2\2\2\u0184\u0182\3\2\2\2\u0184\u0185\3\2\2\2\u0185"+ - "\u0188\3\2\2\2\u0186\u0184\3\2\2\2\u0187\u0180\3\2\2\2\u0187\u0188\3\2"+ - "\2\2\u0188\u0189\3\2\2\2\u0189\u018a\5k\66\2\u018a\u0194\3\2\2\2\u018b"+ - "\u018d\5G$\2\u018c\u018e\5m\67\2\u018d\u018c\3\2\2\2\u018e\u018f\3\2\2"+ - "\2\u018f\u018d\3\2\2\2\u018f\u0190\3\2\2\2\u0190\u0191\3\2\2\2\u0191\u0192"+ - "\5k\66\2\u0192\u0194\3\2\2\2\u0193\u016a\3\2\2\2\u0193\u0175\3\2\2\2\u0193"+ - "\u017c\3\2\2\2\u0193\u018b\3\2\2\2\u0194d\3\2\2\2\u0195\u0198\5o8\2\u0196"+ - "\u0198\t\t\2\2\u0197\u0195\3\2\2\2\u0197\u0196\3\2\2\2\u0198\u019e\3\2"+ - "\2\2\u0199\u019d\5o8\2\u019a\u019d\5m\67\2\u019b\u019d\7a\2\2\u019c\u0199"+ - "\3\2\2\2\u019c\u019a\3\2\2\2\u019c\u019b\3\2\2\2\u019d\u01a0\3\2\2\2\u019e"+ - "\u019c\3\2\2\2\u019e\u019f\3\2\2\2\u019ff\3\2\2\2\u01a0\u019e\3\2\2\2"+ - "\u01a1\u01a7\7b\2\2\u01a2\u01a6\n\n\2\2\u01a3\u01a4\7b\2\2\u01a4\u01a6"+ - "\7b\2\2\u01a5\u01a2\3\2\2\2\u01a5\u01a3\3\2\2\2\u01a6\u01a9\3\2\2\2\u01a7"+ - "\u01a5\3\2\2\2\u01a7\u01a8\3\2\2\2\u01a8\u01aa\3\2\2\2\u01a9\u01a7\3\2"+ - "\2\2\u01aa\u01ab\7b\2\2\u01abh\3\2\2\2\u01ac\u01b2\5o8\2\u01ad\u01b1\5"+ - "o8\2\u01ae\u01b1\5m\67\2\u01af\u01b1\7a\2\2\u01b0\u01ad\3\2\2\2\u01b0"+ - "\u01ae\3\2\2\2\u01b0\u01af\3\2\2\2\u01b1\u01b4\3\2\2\2\u01b2\u01b0\3\2"+ - "\2\2\u01b2\u01b3\3\2\2\2\u01b3\u01b5\3\2\2\2\u01b4\u01b2\3\2\2\2\u01b5"+ - "\u01b6\7\u0080\2\2\u01b6j\3\2\2\2\u01b7\u01b9\t\13\2\2\u01b8\u01ba\t\f"+ - "\2\2\u01b9\u01b8\3\2\2\2\u01b9\u01ba\3\2\2\2\u01ba\u01bc\3\2\2\2\u01bb"+ - "\u01bd\5m\67\2\u01bc\u01bb\3\2\2\2\u01bd\u01be\3\2\2\2\u01be\u01bc\3\2"+ - "\2\2\u01be\u01bf\3\2\2\2\u01bfl\3\2\2\2\u01c0\u01c1\t\r\2\2\u01c1n\3\2"+ - "\2\2\u01c2\u01c3\t\16\2\2\u01c3p\3\2\2\2\u01c4\u01c5\7\61\2\2\u01c5\u01c6"+ - "\7\61\2\2\u01c6\u01ca\3\2\2\2\u01c7\u01c9\n\5\2\2\u01c8\u01c7\3\2\2\2"+ - "\u01c9\u01cc\3\2\2\2\u01ca\u01c8\3\2\2\2\u01ca\u01cb\3\2\2\2\u01cb\u01ce"+ - "\3\2\2\2\u01cc\u01ca\3\2\2\2\u01cd\u01cf\7\17\2\2\u01ce\u01cd\3\2\2\2"+ - "\u01ce\u01cf\3\2\2\2\u01cf\u01d1\3\2\2\2\u01d0\u01d2\7\f\2\2\u01d1\u01d0"+ - "\3\2\2\2\u01d1\u01d2\3\2\2\2\u01d2\u01d3\3\2\2\2\u01d3\u01d4\b9\2\2\u01d4"+ - "r\3\2\2\2\u01d5\u01d6\7\61\2\2\u01d6\u01d7\7,\2\2\u01d7\u01dc\3\2\2\2"+ - "\u01d8\u01db\5s:\2\u01d9\u01db\13\2\2\2\u01da\u01d8\3\2\2\2\u01da\u01d9"+ - "\3\2\2\2\u01db\u01de\3\2\2\2\u01dc\u01dd\3\2\2\2\u01dc\u01da\3\2\2\2\u01dd"+ - "\u01df\3\2\2\2\u01de\u01dc\3\2\2\2\u01df\u01e0\7,\2\2\u01e0\u01e1\7\61"+ - "\2\2\u01e1\u01e2\3\2\2\2\u01e2\u01e3\b:\2\2\u01e3t\3\2\2\2\u01e4\u01e6"+ - "\t\17\2\2\u01e5\u01e4\3\2\2\2\u01e6\u01e7\3\2\2\2\u01e7\u01e5\3\2\2\2"+ - "\u01e7\u01e8\3\2\2\2\u01e8\u01e9\3\2\2\2\u01e9\u01ea\b;\2\2\u01eav\3\2"+ - "\2\2(\2\u011c\u0126\u0128\u0133\u013b\u013e\u0144\u0146\u0150\u0152\u015c"+ - "\u015e\u0162\u0167\u016c\u0172\u0179\u017e\u0184\u0187\u018f\u0193\u0197"+ - "\u019c\u019e\u01a5\u01a7\u01b0\u01b2\u01b9\u01be\u01ca\u01ce\u01d1\u01da"+ - "\u01dc\u01e7\3\2\3\2"; - public static final ATN _ATN = - new ATNDeserializer().deserialize(_serializedATN.toCharArray()); - static { - _decisionToDFA = new DFA[_ATN.getNumberOfDecisions()]; - for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) { - _decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i); + public static final String _serializedATN = "\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\2\65\u01eb\b\1\4\2" + + "\t\2\4\3\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b\t\b\4\t\t\t\4\n\t\n\4" + + "\13\t\13\4\f\t\f\4\r\t\r\4\16\t\16\4\17\t\17\4\20\t\20\4\21\t\21\4\22" + + "\t\22\4\23\t\23\4\24\t\24\4\25\t\25\4\26\t\26\4\27\t\27\4\30\t\30\4\31" + + "\t\31\4\32\t\32\4\33\t\33\4\34\t\34\4\35\t\35\4\36\t\36\4\37\t\37\4 \t" + + " \4!\t!\4\"\t\"\4#\t#\4$\t$\4%\t%\4&\t&\4\'\t\'\4(\t(\4)\t)\4*\t*\4+\t" + + "+\4,\t,\4-\t-\4.\t.\4/\t/\4\60\t\60\4\61\t\61\4\62\t\62\4\63\t\63\4\64" + + "\t\64\4\65\t\65\4\66\t\66\4\67\t\67\48\t8\49\t9\4:\t:\4;\t;\3\2\3\2\3" + + "\2\3\2\3\3\3\3\3\3\3\3\3\4\3\4\3\4\3\5\3\5\3\5\3\5\3\5\3\5\3\6\3\6\3\6" + + "\3\7\3\7\3\7\3\7\3\b\3\b\3\b\3\b\3\b\3\t\3\t\3\t\3\t\3\t\3\n\3\n\3\n\3" + + "\n\3\n\3\n\3\13\3\13\3\13\3\13\3\13\3\13\3\13\3\13\3\f\3\f\3\f\3\f\3\r" + + "\3\r\3\r\3\r\3\r\3\16\3\16\3\16\3\17\3\17\3\17\3\20\3\20\3\20\3\20\3\20" + + "\3\20\3\21\3\21\3\21\3\21\3\21\3\21\3\21\3\22\3\22\3\22\3\22\3\22\3\22" + + "\3\22\3\22\3\22\3\23\3\23\3\23\3\23\3\23\3\24\3\24\3\24\3\24\3\24\3\24" + + "\3\25\3\25\3\25\3\25\3\25\3\25\3\26\3\26\3\26\3\26\3\26\3\27\3\27\3\30" + + "\3\30\3\31\3\31\3\31\3\32\3\32\3\32\3\33\3\33\3\34\3\34\3\34\3\35\3\35" + + "\3\36\3\36\3\36\3\37\3\37\3 \3 \3!\3!\3\"\3\"\3#\3#\3$\3$\3%\3%\3&\3&" + + "\3\'\3\'\3(\3(\3)\3)\3*\3*\3+\3+\3,\3,\3,\3-\3-\3.\3.\3.\3.\3.\6.\u011b" + + "\n.\r.\16.\u011c\3.\3.\3/\3/\3\60\3\60\3\60\3\60\7\60\u0127\n\60\f\60" + + "\16\60\u012a\13\60\3\60\3\60\3\60\3\60\3\60\3\60\7\60\u0132\n\60\f\60" + + "\16\60\u0135\13\60\3\60\3\60\3\60\3\60\3\60\5\60\u013c\n\60\3\60\5\60" + + "\u013f\n\60\3\60\3\60\3\60\3\60\7\60\u0145\n\60\f\60\16\60\u0148\13\60" + + "\3\60\3\60\3\60\3\60\3\60\3\60\3\60\7\60\u0151\n\60\f\60\16\60\u0154\13" + + "\60\3\60\3\60\3\60\3\60\3\60\3\60\3\60\7\60\u015d\n\60\f\60\16\60\u0160" + + "\13\60\3\60\5\60\u0163\n\60\3\61\6\61\u0166\n\61\r\61\16\61\u0167\3\62" + + "\6\62\u016b\n\62\r\62\16\62\u016c\3\62\3\62\7\62\u0171\n\62\f\62\16\62" + + "\u0174\13\62\3\62\3\62\6\62\u0178\n\62\r\62\16\62\u0179\3\62\6\62\u017d" + + "\n\62\r\62\16\62\u017e\3\62\3\62\7\62\u0183\n\62\f\62\16\62\u0186\13\62" + + "\5\62\u0188\n\62\3\62\3\62\3\62\3\62\6\62\u018e\n\62\r\62\16\62\u018f" + + "\3\62\3\62\5\62\u0194\n\62\3\63\3\63\5\63\u0198\n\63\3\63\3\63\3\63\7" + + "\63\u019d\n\63\f\63\16\63\u01a0\13\63\3\64\3\64\3\64\3\64\7\64\u01a6\n" + + "\64\f\64\16\64\u01a9\13\64\3\64\3\64\3\65\3\65\3\65\3\65\7\65\u01b1\n" + + "\65\f\65\16\65\u01b4\13\65\3\65\3\65\3\66\3\66\5\66\u01ba\n\66\3\66\6" + + "\66\u01bd\n\66\r\66\16\66\u01be\3\67\3\67\38\38\39\39\39\39\79\u01c9\n" + + "9\f9\169\u01cc\139\39\59\u01cf\n9\39\59\u01d2\n9\39\39\3:\3:\3:\3:\3:" + + "\7:\u01db\n:\f:\16:\u01de\13:\3:\3:\3:\3:\3:\3;\6;\u01e6\n;\r;\16;\u01e7" + + "\3;\3;\4\u0133\u01dc\2<\3\3\5\4\7\5\t\6\13\7\r\b\17\t\21\n\23\13\25\f" + + "\27\r\31\16\33\17\35\20\37\21!\22#\23%\24\'\25)\26+\27-\30/\31\61\32\63" + + "\33\65\34\67\359\36;\37= ?!A\"C#E$G%I&K\'M(O)Q*S+U,W\2Y\2[\2]\2_-a.c/" + + "e\60g\61i\62k\2m\2o\2q\63s\64u\65\3\2\20\n\2$$))^^ddhhppttvv\5\2\62;C" + + "Hch\6\2\f\f\17\17$$^^\4\2\f\f\17\17\6\2\f\f\17\17))^^\5\2\f\f\17\17$$" + + "\5\2\f\f\17\17))\4\2BBaa\3\2bb\4\2GGgg\4\2--//\3\2\62;\4\2C\\c|\5\2\13" + + "\f\17\17\"\"\2\u0210\2\3\3\2\2\2\2\5\3\2\2\2\2\7\3\2\2\2\2\t\3\2\2\2\2" + + "\13\3\2\2\2\2\r\3\2\2\2\2\17\3\2\2\2\2\21\3\2\2\2\2\23\3\2\2\2\2\25\3" + + "\2\2\2\2\27\3\2\2\2\2\31\3\2\2\2\2\33\3\2\2\2\2\35\3\2\2\2\2\37\3\2\2" + + "\2\2!\3\2\2\2\2#\3\2\2\2\2%\3\2\2\2\2\'\3\2\2\2\2)\3\2\2\2\2+\3\2\2\2" + + "\2-\3\2\2\2\2/\3\2\2\2\2\61\3\2\2\2\2\63\3\2\2\2\2\65\3\2\2\2\2\67\3\2" + + "\2\2\29\3\2\2\2\2;\3\2\2\2\2=\3\2\2\2\2?\3\2\2\2\2A\3\2\2\2\2C\3\2\2\2" + + "\2E\3\2\2\2\2G\3\2\2\2\2I\3\2\2\2\2K\3\2\2\2\2M\3\2\2\2\2O\3\2\2\2\2Q" + + "\3\2\2\2\2S\3\2\2\2\2U\3\2\2\2\2_\3\2\2\2\2a\3\2\2\2\2c\3\2\2\2\2e\3\2" + + "\2\2\2g\3\2\2\2\2i\3\2\2\2\2q\3\2\2\2\2s\3\2\2\2\2u\3\2\2\2\3w\3\2\2\2" + + "\5{\3\2\2\2\7\177\3\2\2\2\t\u0082\3\2\2\2\13\u0088\3\2\2\2\r\u008b\3\2" + + "\2\2\17\u008f\3\2\2\2\21\u0094\3\2\2\2\23\u0099\3\2\2\2\25\u009f\3\2\2" + + "\2\27\u00a7\3\2\2\2\31\u00ab\3\2\2\2\33\u00b0\3\2\2\2\35\u00b3\3\2\2\2" + + "\37\u00b6\3\2\2\2!\u00bc\3\2\2\2#\u00c3\3\2\2\2%\u00cc\3\2\2\2\'\u00d1" + + "\3\2\2\2)\u00d7\3\2\2\2+\u00dd\3\2\2\2-\u00e2\3\2\2\2/\u00e4\3\2\2\2\61" + + "\u00e6\3\2\2\2\63\u00e9\3\2\2\2\65\u00ec\3\2\2\2\67\u00ee\3\2\2\29\u00f1" + + "\3\2\2\2;\u00f3\3\2\2\2=\u00f6\3\2\2\2?\u00f8\3\2\2\2A\u00fa\3\2\2\2C" + + "\u00fc\3\2\2\2E\u00fe\3\2\2\2G\u0100\3\2\2\2I\u0102\3\2\2\2K\u0104\3\2" + + "\2\2M\u0106\3\2\2\2O\u0108\3\2\2\2Q\u010a\3\2\2\2S\u010c\3\2\2\2U\u010e" + + "\3\2\2\2W\u0110\3\2\2\2Y\u0113\3\2\2\2[\u0115\3\2\2\2]\u0120\3\2\2\2_" + + "\u0162\3\2\2\2a\u0165\3\2\2\2c\u0193\3\2\2\2e\u0197\3\2\2\2g\u01a1\3\2" + + "\2\2i\u01ac\3\2\2\2k\u01b7\3\2\2\2m\u01c0\3\2\2\2o\u01c2\3\2\2\2q\u01c4" + + "\3\2\2\2s\u01d5\3\2\2\2u\u01e5\3\2\2\2wx\7c\2\2xy\7p\2\2yz\7f\2\2z\4\3" + + "\2\2\2{|\7c\2\2|}\7p\2\2}~\7{\2\2~\6\3\2\2\2\177\u0080\7d\2\2\u0080\u0081" + + "\7{\2\2\u0081\b\3\2\2\2\u0082\u0083\7h\2\2\u0083\u0084\7c\2\2\u0084\u0085" + + "\7n\2\2\u0085\u0086\7u\2\2\u0086\u0087\7g\2\2\u0087\n\3\2\2\2\u0088\u0089" + + "\7k\2\2\u0089\u008a\7p\2\2\u008a\f\3\2\2\2\u008b\u008c\7k\2\2\u008c\u008d" + + "\7p\2\2\u008d\u008e\7\u0080\2\2\u008e\16\3\2\2\2\u008f\u0090\7l\2\2\u0090" + + "\u0091\7q\2\2\u0091\u0092\7k\2\2\u0092\u0093\7p\2\2\u0093\20\3\2\2\2\u0094" + + "\u0095\7n\2\2\u0095\u0096\7k\2\2\u0096\u0097\7m\2\2\u0097\u0098\7g\2\2" + + "\u0098\22\3\2\2\2\u0099\u009a\7n\2\2\u009a\u009b\7k\2\2\u009b\u009c\7" + + "m\2\2\u009c\u009d\7g\2\2\u009d\u009e\7\u0080\2\2\u009e\24\3\2\2\2\u009f" + + "\u00a0\7o\2\2\u00a0\u00a1\7c\2\2\u00a1\u00a2\7z\2\2\u00a2\u00a3\7u\2\2" + + "\u00a3\u00a4\7r\2\2\u00a4\u00a5\7c\2\2\u00a5\u00a6\7p\2\2\u00a6\26\3\2" + + "\2\2\u00a7\u00a8\7p\2\2\u00a8\u00a9\7q\2\2\u00a9\u00aa\7v\2\2\u00aa\30" + + "\3\2\2\2\u00ab\u00ac\7p\2\2\u00ac\u00ad\7w\2\2\u00ad\u00ae\7n\2\2\u00ae" + + "\u00af\7n\2\2\u00af\32\3\2\2\2\u00b0\u00b1\7q\2\2\u00b1\u00b2\7h\2\2\u00b2" + + "\34\3\2\2\2\u00b3\u00b4\7q\2\2\u00b4\u00b5\7t\2\2\u00b5\36\3\2\2\2\u00b6" + + "\u00b7\7t\2\2\u00b7\u00b8\7g\2\2\u00b8\u00b9\7i\2\2\u00b9\u00ba\7g\2\2" + + "\u00ba\u00bb\7z\2\2\u00bb \3\2\2\2\u00bc\u00bd\7t\2\2\u00bd\u00be\7g\2" + + "\2\u00be\u00bf\7i\2\2\u00bf\u00c0\7g\2\2\u00c0\u00c1\7z\2\2\u00c1\u00c2" + + "\7\u0080\2\2\u00c2\"\3\2\2\2\u00c3\u00c4\7u\2\2\u00c4\u00c5\7g\2\2\u00c5" + + "\u00c6\7s\2\2\u00c6\u00c7\7w\2\2\u00c7\u00c8\7g\2\2\u00c8\u00c9\7p\2\2" + + "\u00c9\u00ca\7e\2\2\u00ca\u00cb\7g\2\2\u00cb$\3\2\2\2\u00cc\u00cd\7v\2" + + "\2\u00cd\u00ce\7t\2\2\u00ce\u00cf\7w\2\2\u00cf\u00d0\7g\2\2\u00d0&\3\2" + + "\2\2\u00d1\u00d2\7w\2\2\u00d2\u00d3\7p\2\2\u00d3\u00d4\7v\2\2\u00d4\u00d5" + + "\7k\2\2\u00d5\u00d6\7n\2\2\u00d6(\3\2\2\2\u00d7\u00d8\7y\2\2\u00d8\u00d9" + + "\7j\2\2\u00d9\u00da\7g\2\2\u00da\u00db\7t\2\2\u00db\u00dc\7g\2\2\u00dc" + + "*\3\2\2\2\u00dd\u00de\7y\2\2\u00de\u00df\7k\2\2\u00df\u00e0\7v\2\2\u00e0" + + "\u00e1\7j\2\2\u00e1,\3\2\2\2\u00e2\u00e3\7<\2\2\u00e3.\3\2\2\2\u00e4\u00e5" + + "\7?\2\2\u00e5\60\3\2\2\2\u00e6\u00e7\7?\2\2\u00e7\u00e8\7?\2\2\u00e8\62" + + "\3\2\2\2\u00e9\u00ea\7#\2\2\u00ea\u00eb\7?\2\2\u00eb\64\3\2\2\2\u00ec" + + "\u00ed\7>\2\2\u00ed\66\3\2\2\2\u00ee\u00ef\7>\2\2\u00ef\u00f0\7?\2\2\u00f0" + + "8\3\2\2\2\u00f1\u00f2\7@\2\2\u00f2:\3\2\2\2\u00f3\u00f4\7@\2\2\u00f4\u00f5" + + "\7?\2\2\u00f5<\3\2\2\2\u00f6\u00f7\7-\2\2\u00f7>\3\2\2\2\u00f8\u00f9\7" + + "/\2\2\u00f9@\3\2\2\2\u00fa\u00fb\7,\2\2\u00fbB\3\2\2\2\u00fc\u00fd\7\61" + + "\2\2\u00fdD\3\2\2\2\u00fe\u00ff\7\'\2\2\u00ffF\3\2\2\2\u0100\u0101\7\60" + + "\2\2\u0101H\3\2\2\2\u0102\u0103\7.\2\2\u0103J\3\2\2\2\u0104\u0105\7]\2" + + "\2\u0105L\3\2\2\2\u0106\u0107\7_\2\2\u0107N\3\2\2\2\u0108\u0109\7*\2\2" + + "\u0109P\3\2\2\2\u010a\u010b\7+\2\2\u010bR\3\2\2\2\u010c\u010d\7~\2\2\u010d" + + "T\3\2\2\2\u010e\u010f\7A\2\2\u010fV\3\2\2\2\u0110\u0111\7^\2\2\u0111\u0112" + + "\t\2\2\2\u0112X\3\2\2\2\u0113\u0114\t\3\2\2\u0114Z\3\2\2\2\u0115\u0116" + + "\7^\2\2\u0116\u0117\7w\2\2\u0117\u0118\3\2\2\2\u0118\u011a\7}\2\2\u0119" + + "\u011b\5Y-\2\u011a\u0119\3\2\2\2\u011b\u011c\3\2\2\2\u011c\u011a\3\2\2" + + "\2\u011c\u011d\3\2\2\2\u011d\u011e\3\2\2\2\u011e\u011f\7\177\2\2\u011f" + + "\\\3\2\2\2\u0120\u0121\n\4\2\2\u0121^\3\2\2\2\u0122\u0128\7$\2\2\u0123" + + "\u0127\5W,\2\u0124\u0127\5[.\2\u0125\u0127\5]/\2\u0126\u0123\3\2\2\2\u0126" + + "\u0124\3\2\2\2\u0126\u0125\3\2\2\2\u0127\u012a\3\2\2\2\u0128\u0126\3\2" + + "\2\2\u0128\u0129\3\2\2\2\u0129\u012b\3\2\2\2\u012a\u0128\3\2\2\2\u012b" + + "\u0163\7$\2\2\u012c\u012d\7$\2\2\u012d\u012e\7$\2\2\u012e\u012f\7$\2\2" + + "\u012f\u0133\3\2\2\2\u0130\u0132\n\5\2\2\u0131\u0130\3\2\2\2\u0132\u0135" + + "\3\2\2\2\u0133\u0134\3\2\2\2\u0133\u0131\3\2\2\2\u0134\u0136\3\2\2\2\u0135" + + "\u0133\3\2\2\2\u0136\u0137\7$\2\2\u0137\u0138\7$\2\2\u0138\u0139\7$\2" + + "\2\u0139\u013b\3\2\2\2\u013a\u013c\7$\2\2\u013b\u013a\3\2\2\2\u013b\u013c" + + "\3\2\2\2\u013c\u013e\3\2\2\2\u013d\u013f\7$\2\2\u013e\u013d\3\2\2\2\u013e" + + "\u013f\3\2\2\2\u013f\u0163\3\2\2\2\u0140\u0146\7)\2\2\u0141\u0142\7^\2" + + "\2\u0142\u0145\t\2\2\2\u0143\u0145\n\6\2\2\u0144\u0141\3\2\2\2\u0144\u0143" + + "\3\2\2\2\u0145\u0148\3\2\2\2\u0146\u0144\3\2\2\2\u0146\u0147\3\2\2\2\u0147" + + "\u0149\3\2\2\2\u0148\u0146\3\2\2\2\u0149\u0163\7)\2\2\u014a\u014b\7A\2" + + "\2\u014b\u014c\7$\2\2\u014c\u0152\3\2\2\2\u014d\u014e\7^\2\2\u014e\u0151" + + "\7$\2\2\u014f\u0151\n\7\2\2\u0150\u014d\3\2\2\2\u0150\u014f\3\2\2\2\u0151" + + "\u0154\3\2\2\2\u0152\u0150\3\2\2\2\u0152\u0153\3\2\2\2\u0153\u0155\3\2" + + "\2\2\u0154\u0152\3\2\2\2\u0155\u0163\7$\2\2\u0156\u0157\7A\2\2\u0157\u0158" + + "\7)\2\2\u0158\u015e\3\2\2\2\u0159\u015a\7^\2\2\u015a\u015d\7)\2\2\u015b" + + "\u015d\n\b\2\2\u015c\u0159\3\2\2\2\u015c\u015b\3\2\2\2\u015d\u0160\3\2" + + "\2\2\u015e\u015c\3\2\2\2\u015e\u015f\3\2\2\2\u015f\u0161\3\2\2\2\u0160" + + "\u015e\3\2\2\2\u0161\u0163\7)\2\2\u0162\u0122\3\2\2\2\u0162\u012c\3\2" + + "\2\2\u0162\u0140\3\2\2\2\u0162\u014a\3\2\2\2\u0162\u0156\3\2\2\2\u0163" + + "`\3\2\2\2\u0164\u0166\5m\67\2\u0165\u0164\3\2\2\2\u0166\u0167\3\2\2\2" + + "\u0167\u0165\3\2\2\2\u0167\u0168\3\2\2\2\u0168b\3\2\2\2\u0169\u016b\5" + + "m\67\2\u016a\u0169\3\2\2\2\u016b\u016c\3\2\2\2\u016c\u016a\3\2\2\2\u016c" + + "\u016d\3\2\2\2\u016d\u016e\3\2\2\2\u016e\u0172\5G$\2\u016f\u0171\5m\67" + + "\2\u0170\u016f\3\2\2\2\u0171\u0174\3\2\2\2\u0172\u0170\3\2\2\2\u0172\u0173" + + "\3\2\2\2\u0173\u0194\3\2\2\2\u0174\u0172\3\2\2\2\u0175\u0177\5G$\2\u0176" + + "\u0178\5m\67\2\u0177\u0176\3\2\2\2\u0178\u0179\3\2\2\2\u0179\u0177\3\2" + + "\2\2\u0179\u017a\3\2\2\2\u017a\u0194\3\2\2\2\u017b\u017d\5m\67\2\u017c" + + "\u017b\3\2\2\2\u017d\u017e\3\2\2\2\u017e\u017c\3\2\2\2\u017e\u017f\3\2" + + "\2\2\u017f\u0187\3\2\2\2\u0180\u0184\5G$\2\u0181\u0183\5m\67\2\u0182\u0181" + + "\3\2\2\2\u0183\u0186\3\2\2\2\u0184\u0182\3\2\2\2\u0184\u0185\3\2\2\2\u0185" + + "\u0188\3\2\2\2\u0186\u0184\3\2\2\2\u0187\u0180\3\2\2\2\u0187\u0188\3\2" + + "\2\2\u0188\u0189\3\2\2\2\u0189\u018a\5k\66\2\u018a\u0194\3\2\2\2\u018b" + + "\u018d\5G$\2\u018c\u018e\5m\67\2\u018d\u018c\3\2\2\2\u018e\u018f\3\2\2" + + "\2\u018f\u018d\3\2\2\2\u018f\u0190\3\2\2\2\u0190\u0191\3\2\2\2\u0191\u0192" + + "\5k\66\2\u0192\u0194\3\2\2\2\u0193\u016a\3\2\2\2\u0193\u0175\3\2\2\2\u0193" + + "\u017c\3\2\2\2\u0193\u018b\3\2\2\2\u0194d\3\2\2\2\u0195\u0198\5o8\2\u0196" + + "\u0198\t\t\2\2\u0197\u0195\3\2\2\2\u0197\u0196\3\2\2\2\u0198\u019e\3\2" + + "\2\2\u0199\u019d\5o8\2\u019a\u019d\5m\67\2\u019b\u019d\7a\2\2\u019c\u0199" + + "\3\2\2\2\u019c\u019a\3\2\2\2\u019c\u019b\3\2\2\2\u019d\u01a0\3\2\2\2\u019e" + + "\u019c\3\2\2\2\u019e\u019f\3\2\2\2\u019ff\3\2\2\2\u01a0\u019e\3\2\2\2" + + "\u01a1\u01a7\7b\2\2\u01a2\u01a6\n\n\2\2\u01a3\u01a4\7b\2\2\u01a4\u01a6" + + "\7b\2\2\u01a5\u01a2\3\2\2\2\u01a5\u01a3\3\2\2\2\u01a6\u01a9\3\2\2\2\u01a7" + + "\u01a5\3\2\2\2\u01a7\u01a8\3\2\2\2\u01a8\u01aa\3\2\2\2\u01a9\u01a7\3\2" + + "\2\2\u01aa\u01ab\7b\2\2\u01abh\3\2\2\2\u01ac\u01b2\5o8\2\u01ad\u01b1\5" + + "o8\2\u01ae\u01b1\5m\67\2\u01af\u01b1\7a\2\2\u01b0\u01ad\3\2\2\2\u01b0" + + "\u01ae\3\2\2\2\u01b0\u01af\3\2\2\2\u01b1\u01b4\3\2\2\2\u01b2\u01b0\3\2" + + "\2\2\u01b2\u01b3\3\2\2\2\u01b3\u01b5\3\2\2\2\u01b4\u01b2\3\2\2\2\u01b5" + + "\u01b6\7\u0080\2\2\u01b6j\3\2\2\2\u01b7\u01b9\t\13\2\2\u01b8\u01ba\t\f" + + "\2\2\u01b9\u01b8\3\2\2\2\u01b9\u01ba\3\2\2\2\u01ba\u01bc\3\2\2\2\u01bb" + + "\u01bd\5m\67\2\u01bc\u01bb\3\2\2\2\u01bd\u01be\3\2\2\2\u01be\u01bc\3\2" + + "\2\2\u01be\u01bf\3\2\2\2\u01bfl\3\2\2\2\u01c0\u01c1\t\r\2\2\u01c1n\3\2" + + "\2\2\u01c2\u01c3\t\16\2\2\u01c3p\3\2\2\2\u01c4\u01c5\7\61\2\2\u01c5\u01c6" + + "\7\61\2\2\u01c6\u01ca\3\2\2\2\u01c7\u01c9\n\5\2\2\u01c8\u01c7\3\2\2\2" + + "\u01c9\u01cc\3\2\2\2\u01ca\u01c8\3\2\2\2\u01ca\u01cb\3\2\2\2\u01cb\u01ce" + + "\3\2\2\2\u01cc\u01ca\3\2\2\2\u01cd\u01cf\7\17\2\2\u01ce\u01cd\3\2\2\2" + + "\u01ce\u01cf\3\2\2\2\u01cf\u01d1\3\2\2\2\u01d0\u01d2\7\f\2\2\u01d1\u01d0" + + "\3\2\2\2\u01d1\u01d2\3\2\2\2\u01d2\u01d3\3\2\2\2\u01d3\u01d4\b9\2\2\u01d4" + + "r\3\2\2\2\u01d5\u01d6\7\61\2\2\u01d6\u01d7\7,\2\2\u01d7\u01dc\3\2\2\2" + + "\u01d8\u01db\5s:\2\u01d9\u01db\13\2\2\2\u01da\u01d8\3\2\2\2\u01da\u01d9" + + "\3\2\2\2\u01db\u01de\3\2\2\2\u01dc\u01dd\3\2\2\2\u01dc\u01da\3\2\2\2\u01dd" + + "\u01df\3\2\2\2\u01de\u01dc\3\2\2\2\u01df\u01e0\7,\2\2\u01e0\u01e1\7\61" + + "\2\2\u01e1\u01e2\3\2\2\2\u01e2\u01e3\b:\2\2\u01e3t\3\2\2\2\u01e4\u01e6" + + "\t\17\2\2\u01e5\u01e4\3\2\2\2\u01e6\u01e7\3\2\2\2\u01e7\u01e5\3\2\2\2" + + "\u01e7\u01e8\3\2\2\2\u01e8\u01e9\3\2\2\2\u01e9\u01ea\b;\2\2\u01eav\3\2" + + "\2\2(\2\u011c\u0126\u0128\u0133\u013b\u013e\u0144\u0146\u0150\u0152\u015c" + + "\u015e\u0162\u0167\u016c\u0172\u0179\u017e\u0184\u0187\u018f\u0193\u0197" + + "\u019c\u019e\u01a5\u01a7\u01b0\u01b2\u01b9\u01be\u01ca\u01ce\u01d1\u01da" + + "\u01dc\u01e7\3\2\3\2"; + public static final ATN _ATN = new ATNDeserializer().deserialize(_serializedATN.toCharArray()); + static { + _decisionToDFA = new DFA[_ATN.getNumberOfDecisions()]; + for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) { + _decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i); + } } - } } diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/parser/EqlBaseListener.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/parser/EqlBaseListener.java index bca6c3b045ce8..53343ed082755 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/parser/EqlBaseListener.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/parser/EqlBaseListener.java @@ -1,5 +1,6 @@ // ANTLR GENERATED CODE: DO NOT EDIT package org.elasticsearch.xpack.eql.parser; + import org.antlr.v4.runtime.tree.ParseTreeListener; /** @@ -7,482 +8,569 @@ * {@link EqlBaseParser}. */ interface EqlBaseListener extends ParseTreeListener { - /** - * Enter a parse tree produced by {@link EqlBaseParser#singleStatement}. - * @param ctx the parse tree - */ - void enterSingleStatement(EqlBaseParser.SingleStatementContext ctx); - /** - * Exit a parse tree produced by {@link EqlBaseParser#singleStatement}. - * @param ctx the parse tree - */ - void exitSingleStatement(EqlBaseParser.SingleStatementContext ctx); - /** - * Enter a parse tree produced by {@link EqlBaseParser#singleExpression}. - * @param ctx the parse tree - */ - void enterSingleExpression(EqlBaseParser.SingleExpressionContext ctx); - /** - * Exit a parse tree produced by {@link EqlBaseParser#singleExpression}. - * @param ctx the parse tree - */ - void exitSingleExpression(EqlBaseParser.SingleExpressionContext ctx); - /** - * Enter a parse tree produced by {@link EqlBaseParser#statement}. - * @param ctx the parse tree - */ - void enterStatement(EqlBaseParser.StatementContext ctx); - /** - * Exit a parse tree produced by {@link EqlBaseParser#statement}. - * @param ctx the parse tree - */ - void exitStatement(EqlBaseParser.StatementContext ctx); - /** - * Enter a parse tree produced by {@link EqlBaseParser#query}. - * @param ctx the parse tree - */ - void enterQuery(EqlBaseParser.QueryContext ctx); - /** - * Exit a parse tree produced by {@link EqlBaseParser#query}. - * @param ctx the parse tree - */ - void exitQuery(EqlBaseParser.QueryContext ctx); - /** - * Enter a parse tree produced by {@link EqlBaseParser#sequenceParams}. - * @param ctx the parse tree - */ - void enterSequenceParams(EqlBaseParser.SequenceParamsContext ctx); - /** - * Exit a parse tree produced by {@link EqlBaseParser#sequenceParams}. - * @param ctx the parse tree - */ - void exitSequenceParams(EqlBaseParser.SequenceParamsContext ctx); - /** - * Enter a parse tree produced by {@link EqlBaseParser#sequence}. - * @param ctx the parse tree - */ - void enterSequence(EqlBaseParser.SequenceContext ctx); - /** - * Exit a parse tree produced by {@link EqlBaseParser#sequence}. - * @param ctx the parse tree - */ - void exitSequence(EqlBaseParser.SequenceContext ctx); - /** - * Enter a parse tree produced by {@link EqlBaseParser#join}. - * @param ctx the parse tree - */ - void enterJoin(EqlBaseParser.JoinContext ctx); - /** - * Exit a parse tree produced by {@link EqlBaseParser#join}. - * @param ctx the parse tree - */ - void exitJoin(EqlBaseParser.JoinContext ctx); - /** - * Enter a parse tree produced by {@link EqlBaseParser#pipe}. - * @param ctx the parse tree - */ - void enterPipe(EqlBaseParser.PipeContext ctx); - /** - * Exit a parse tree produced by {@link EqlBaseParser#pipe}. - * @param ctx the parse tree - */ - void exitPipe(EqlBaseParser.PipeContext ctx); - /** - * Enter a parse tree produced by {@link EqlBaseParser#joinKeys}. - * @param ctx the parse tree - */ - void enterJoinKeys(EqlBaseParser.JoinKeysContext ctx); - /** - * Exit a parse tree produced by {@link EqlBaseParser#joinKeys}. - * @param ctx the parse tree - */ - void exitJoinKeys(EqlBaseParser.JoinKeysContext ctx); - /** - * Enter a parse tree produced by {@link EqlBaseParser#joinTerm}. - * @param ctx the parse tree - */ - void enterJoinTerm(EqlBaseParser.JoinTermContext ctx); - /** - * Exit a parse tree produced by {@link EqlBaseParser#joinTerm}. - * @param ctx the parse tree - */ - void exitJoinTerm(EqlBaseParser.JoinTermContext ctx); - /** - * Enter a parse tree produced by {@link EqlBaseParser#sequenceTerm}. - * @param ctx the parse tree - */ - void enterSequenceTerm(EqlBaseParser.SequenceTermContext ctx); - /** - * Exit a parse tree produced by {@link EqlBaseParser#sequenceTerm}. - * @param ctx the parse tree - */ - void exitSequenceTerm(EqlBaseParser.SequenceTermContext ctx); - /** - * Enter a parse tree produced by {@link EqlBaseParser#subquery}. - * @param ctx the parse tree - */ - void enterSubquery(EqlBaseParser.SubqueryContext ctx); - /** - * Exit a parse tree produced by {@link EqlBaseParser#subquery}. - * @param ctx the parse tree - */ - void exitSubquery(EqlBaseParser.SubqueryContext ctx); - /** - * Enter a parse tree produced by {@link EqlBaseParser#eventQuery}. - * @param ctx the parse tree - */ - void enterEventQuery(EqlBaseParser.EventQueryContext ctx); - /** - * Exit a parse tree produced by {@link EqlBaseParser#eventQuery}. - * @param ctx the parse tree - */ - void exitEventQuery(EqlBaseParser.EventQueryContext ctx); - /** - * Enter a parse tree produced by {@link EqlBaseParser#eventFilter}. - * @param ctx the parse tree - */ - void enterEventFilter(EqlBaseParser.EventFilterContext ctx); - /** - * Exit a parse tree produced by {@link EqlBaseParser#eventFilter}. - * @param ctx the parse tree - */ - void exitEventFilter(EqlBaseParser.EventFilterContext ctx); - /** - * Enter a parse tree produced by {@link EqlBaseParser#expression}. - * @param ctx the parse tree - */ - void enterExpression(EqlBaseParser.ExpressionContext ctx); - /** - * Exit a parse tree produced by {@link EqlBaseParser#expression}. - * @param ctx the parse tree - */ - void exitExpression(EqlBaseParser.ExpressionContext ctx); - /** - * Enter a parse tree produced by the {@code logicalNot} - * labeled alternative in {@link EqlBaseParser#booleanExpression}. - * @param ctx the parse tree - */ - void enterLogicalNot(EqlBaseParser.LogicalNotContext ctx); - /** - * Exit a parse tree produced by the {@code logicalNot} - * labeled alternative in {@link EqlBaseParser#booleanExpression}. - * @param ctx the parse tree - */ - void exitLogicalNot(EqlBaseParser.LogicalNotContext ctx); - /** - * Enter a parse tree produced by the {@code booleanDefault} - * labeled alternative in {@link EqlBaseParser#booleanExpression}. - * @param ctx the parse tree - */ - void enterBooleanDefault(EqlBaseParser.BooleanDefaultContext ctx); - /** - * Exit a parse tree produced by the {@code booleanDefault} - * labeled alternative in {@link EqlBaseParser#booleanExpression}. - * @param ctx the parse tree - */ - void exitBooleanDefault(EqlBaseParser.BooleanDefaultContext ctx); - /** - * Enter a parse tree produced by the {@code processCheck} - * labeled alternative in {@link EqlBaseParser#booleanExpression}. - * @param ctx the parse tree - */ - void enterProcessCheck(EqlBaseParser.ProcessCheckContext ctx); - /** - * Exit a parse tree produced by the {@code processCheck} - * labeled alternative in {@link EqlBaseParser#booleanExpression}. - * @param ctx the parse tree - */ - void exitProcessCheck(EqlBaseParser.ProcessCheckContext ctx); - /** - * Enter a parse tree produced by the {@code logicalBinary} - * labeled alternative in {@link EqlBaseParser#booleanExpression}. - * @param ctx the parse tree - */ - void enterLogicalBinary(EqlBaseParser.LogicalBinaryContext ctx); - /** - * Exit a parse tree produced by the {@code logicalBinary} - * labeled alternative in {@link EqlBaseParser#booleanExpression}. - * @param ctx the parse tree - */ - void exitLogicalBinary(EqlBaseParser.LogicalBinaryContext ctx); - /** - * Enter a parse tree produced by the {@code valueExpressionDefault} - * labeled alternative in {@link EqlBaseParser#valueExpression}. - * @param ctx the parse tree - */ - void enterValueExpressionDefault(EqlBaseParser.ValueExpressionDefaultContext ctx); - /** - * Exit a parse tree produced by the {@code valueExpressionDefault} - * labeled alternative in {@link EqlBaseParser#valueExpression}. - * @param ctx the parse tree - */ - void exitValueExpressionDefault(EqlBaseParser.ValueExpressionDefaultContext ctx); - /** - * Enter a parse tree produced by the {@code comparison} - * labeled alternative in {@link EqlBaseParser#valueExpression}. - * @param ctx the parse tree - */ - void enterComparison(EqlBaseParser.ComparisonContext ctx); - /** - * Exit a parse tree produced by the {@code comparison} - * labeled alternative in {@link EqlBaseParser#valueExpression}. - * @param ctx the parse tree - */ - void exitComparison(EqlBaseParser.ComparisonContext ctx); - /** - * Enter a parse tree produced by the {@code operatorExpressionDefault} - * labeled alternative in {@link EqlBaseParser#operatorExpression}. - * @param ctx the parse tree - */ - void enterOperatorExpressionDefault(EqlBaseParser.OperatorExpressionDefaultContext ctx); - /** - * Exit a parse tree produced by the {@code operatorExpressionDefault} - * labeled alternative in {@link EqlBaseParser#operatorExpression}. - * @param ctx the parse tree - */ - void exitOperatorExpressionDefault(EqlBaseParser.OperatorExpressionDefaultContext ctx); - /** - * Enter a parse tree produced by the {@code arithmeticBinary} - * labeled alternative in {@link EqlBaseParser#operatorExpression}. - * @param ctx the parse tree - */ - void enterArithmeticBinary(EqlBaseParser.ArithmeticBinaryContext ctx); - /** - * Exit a parse tree produced by the {@code arithmeticBinary} - * labeled alternative in {@link EqlBaseParser#operatorExpression}. - * @param ctx the parse tree - */ - void exitArithmeticBinary(EqlBaseParser.ArithmeticBinaryContext ctx); - /** - * Enter a parse tree produced by the {@code arithmeticUnary} - * labeled alternative in {@link EqlBaseParser#operatorExpression}. - * @param ctx the parse tree - */ - void enterArithmeticUnary(EqlBaseParser.ArithmeticUnaryContext ctx); - /** - * Exit a parse tree produced by the {@code arithmeticUnary} - * labeled alternative in {@link EqlBaseParser#operatorExpression}. - * @param ctx the parse tree - */ - void exitArithmeticUnary(EqlBaseParser.ArithmeticUnaryContext ctx); - /** - * Enter a parse tree produced by {@link EqlBaseParser#predicate}. - * @param ctx the parse tree - */ - void enterPredicate(EqlBaseParser.PredicateContext ctx); - /** - * Exit a parse tree produced by {@link EqlBaseParser#predicate}. - * @param ctx the parse tree - */ - void exitPredicate(EqlBaseParser.PredicateContext ctx); - /** - * Enter a parse tree produced by the {@code constantDefault} - * labeled alternative in {@link EqlBaseParser#primaryExpression}. - * @param ctx the parse tree - */ - void enterConstantDefault(EqlBaseParser.ConstantDefaultContext ctx); - /** - * Exit a parse tree produced by the {@code constantDefault} - * labeled alternative in {@link EqlBaseParser#primaryExpression}. - * @param ctx the parse tree - */ - void exitConstantDefault(EqlBaseParser.ConstantDefaultContext ctx); - /** - * Enter a parse tree produced by the {@code function} - * labeled alternative in {@link EqlBaseParser#primaryExpression}. - * @param ctx the parse tree - */ - void enterFunction(EqlBaseParser.FunctionContext ctx); - /** - * Exit a parse tree produced by the {@code function} - * labeled alternative in {@link EqlBaseParser#primaryExpression}. - * @param ctx the parse tree - */ - void exitFunction(EqlBaseParser.FunctionContext ctx); - /** - * Enter a parse tree produced by the {@code dereference} - * labeled alternative in {@link EqlBaseParser#primaryExpression}. - * @param ctx the parse tree - */ - void enterDereference(EqlBaseParser.DereferenceContext ctx); - /** - * Exit a parse tree produced by the {@code dereference} - * labeled alternative in {@link EqlBaseParser#primaryExpression}. - * @param ctx the parse tree - */ - void exitDereference(EqlBaseParser.DereferenceContext ctx); - /** - * Enter a parse tree produced by the {@code parenthesizedExpression} - * labeled alternative in {@link EqlBaseParser#primaryExpression}. - * @param ctx the parse tree - */ - void enterParenthesizedExpression(EqlBaseParser.ParenthesizedExpressionContext ctx); - /** - * Exit a parse tree produced by the {@code parenthesizedExpression} - * labeled alternative in {@link EqlBaseParser#primaryExpression}. - * @param ctx the parse tree - */ - void exitParenthesizedExpression(EqlBaseParser.ParenthesizedExpressionContext ctx); - /** - * Enter a parse tree produced by {@link EqlBaseParser#functionExpression}. - * @param ctx the parse tree - */ - void enterFunctionExpression(EqlBaseParser.FunctionExpressionContext ctx); - /** - * Exit a parse tree produced by {@link EqlBaseParser#functionExpression}. - * @param ctx the parse tree - */ - void exitFunctionExpression(EqlBaseParser.FunctionExpressionContext ctx); - /** - * Enter a parse tree produced by {@link EqlBaseParser#functionName}. - * @param ctx the parse tree - */ - void enterFunctionName(EqlBaseParser.FunctionNameContext ctx); - /** - * Exit a parse tree produced by {@link EqlBaseParser#functionName}. - * @param ctx the parse tree - */ - void exitFunctionName(EqlBaseParser.FunctionNameContext ctx); - /** - * Enter a parse tree produced by the {@code nullLiteral} - * labeled alternative in {@link EqlBaseParser#constant}. - * @param ctx the parse tree - */ - void enterNullLiteral(EqlBaseParser.NullLiteralContext ctx); - /** - * Exit a parse tree produced by the {@code nullLiteral} - * labeled alternative in {@link EqlBaseParser#constant}. - * @param ctx the parse tree - */ - void exitNullLiteral(EqlBaseParser.NullLiteralContext ctx); - /** - * Enter a parse tree produced by the {@code numericLiteral} - * labeled alternative in {@link EqlBaseParser#constant}. - * @param ctx the parse tree - */ - void enterNumericLiteral(EqlBaseParser.NumericLiteralContext ctx); - /** - * Exit a parse tree produced by the {@code numericLiteral} - * labeled alternative in {@link EqlBaseParser#constant}. - * @param ctx the parse tree - */ - void exitNumericLiteral(EqlBaseParser.NumericLiteralContext ctx); - /** - * Enter a parse tree produced by the {@code booleanLiteral} - * labeled alternative in {@link EqlBaseParser#constant}. - * @param ctx the parse tree - */ - void enterBooleanLiteral(EqlBaseParser.BooleanLiteralContext ctx); - /** - * Exit a parse tree produced by the {@code booleanLiteral} - * labeled alternative in {@link EqlBaseParser#constant}. - * @param ctx the parse tree - */ - void exitBooleanLiteral(EqlBaseParser.BooleanLiteralContext ctx); - /** - * Enter a parse tree produced by the {@code stringLiteral} - * labeled alternative in {@link EqlBaseParser#constant}. - * @param ctx the parse tree - */ - void enterStringLiteral(EqlBaseParser.StringLiteralContext ctx); - /** - * Exit a parse tree produced by the {@code stringLiteral} - * labeled alternative in {@link EqlBaseParser#constant}. - * @param ctx the parse tree - */ - void exitStringLiteral(EqlBaseParser.StringLiteralContext ctx); - /** - * Enter a parse tree produced by {@link EqlBaseParser#comparisonOperator}. - * @param ctx the parse tree - */ - void enterComparisonOperator(EqlBaseParser.ComparisonOperatorContext ctx); - /** - * Exit a parse tree produced by {@link EqlBaseParser#comparisonOperator}. - * @param ctx the parse tree - */ - void exitComparisonOperator(EqlBaseParser.ComparisonOperatorContext ctx); - /** - * Enter a parse tree produced by {@link EqlBaseParser#booleanValue}. - * @param ctx the parse tree - */ - void enterBooleanValue(EqlBaseParser.BooleanValueContext ctx); - /** - * Exit a parse tree produced by {@link EqlBaseParser#booleanValue}. - * @param ctx the parse tree - */ - void exitBooleanValue(EqlBaseParser.BooleanValueContext ctx); - /** - * Enter a parse tree produced by {@link EqlBaseParser#qualifiedName}. - * @param ctx the parse tree - */ - void enterQualifiedName(EqlBaseParser.QualifiedNameContext ctx); - /** - * Exit a parse tree produced by {@link EqlBaseParser#qualifiedName}. - * @param ctx the parse tree - */ - void exitQualifiedName(EqlBaseParser.QualifiedNameContext ctx); - /** - * Enter a parse tree produced by {@link EqlBaseParser#identifier}. - * @param ctx the parse tree - */ - void enterIdentifier(EqlBaseParser.IdentifierContext ctx); - /** - * Exit a parse tree produced by {@link EqlBaseParser#identifier}. - * @param ctx the parse tree - */ - void exitIdentifier(EqlBaseParser.IdentifierContext ctx); - /** - * Enter a parse tree produced by {@link EqlBaseParser#timeUnit}. - * @param ctx the parse tree - */ - void enterTimeUnit(EqlBaseParser.TimeUnitContext ctx); - /** - * Exit a parse tree produced by {@link EqlBaseParser#timeUnit}. - * @param ctx the parse tree - */ - void exitTimeUnit(EqlBaseParser.TimeUnitContext ctx); - /** - * Enter a parse tree produced by the {@code decimalLiteral} - * labeled alternative in {@link EqlBaseParser#number}. - * @param ctx the parse tree - */ - void enterDecimalLiteral(EqlBaseParser.DecimalLiteralContext ctx); - /** - * Exit a parse tree produced by the {@code decimalLiteral} - * labeled alternative in {@link EqlBaseParser#number}. - * @param ctx the parse tree - */ - void exitDecimalLiteral(EqlBaseParser.DecimalLiteralContext ctx); - /** - * Enter a parse tree produced by the {@code integerLiteral} - * labeled alternative in {@link EqlBaseParser#number}. - * @param ctx the parse tree - */ - void enterIntegerLiteral(EqlBaseParser.IntegerLiteralContext ctx); - /** - * Exit a parse tree produced by the {@code integerLiteral} - * labeled alternative in {@link EqlBaseParser#number}. - * @param ctx the parse tree - */ - void exitIntegerLiteral(EqlBaseParser.IntegerLiteralContext ctx); - /** - * Enter a parse tree produced by {@link EqlBaseParser#string}. - * @param ctx the parse tree - */ - void enterString(EqlBaseParser.StringContext ctx); - /** - * Exit a parse tree produced by {@link EqlBaseParser#string}. - * @param ctx the parse tree - */ - void exitString(EqlBaseParser.StringContext ctx); - /** - * Enter a parse tree produced by {@link EqlBaseParser#eventValue}. - * @param ctx the parse tree - */ - void enterEventValue(EqlBaseParser.EventValueContext ctx); - /** - * Exit a parse tree produced by {@link EqlBaseParser#eventValue}. - * @param ctx the parse tree - */ - void exitEventValue(EqlBaseParser.EventValueContext ctx); + /** + * Enter a parse tree produced by {@link EqlBaseParser#singleStatement}. + * @param ctx the parse tree + */ + void enterSingleStatement(EqlBaseParser.SingleStatementContext ctx); + + /** + * Exit a parse tree produced by {@link EqlBaseParser#singleStatement}. + * @param ctx the parse tree + */ + void exitSingleStatement(EqlBaseParser.SingleStatementContext ctx); + + /** + * Enter a parse tree produced by {@link EqlBaseParser#singleExpression}. + * @param ctx the parse tree + */ + void enterSingleExpression(EqlBaseParser.SingleExpressionContext ctx); + + /** + * Exit a parse tree produced by {@link EqlBaseParser#singleExpression}. + * @param ctx the parse tree + */ + void exitSingleExpression(EqlBaseParser.SingleExpressionContext ctx); + + /** + * Enter a parse tree produced by {@link EqlBaseParser#statement}. + * @param ctx the parse tree + */ + void enterStatement(EqlBaseParser.StatementContext ctx); + + /** + * Exit a parse tree produced by {@link EqlBaseParser#statement}. + * @param ctx the parse tree + */ + void exitStatement(EqlBaseParser.StatementContext ctx); + + /** + * Enter a parse tree produced by {@link EqlBaseParser#query}. + * @param ctx the parse tree + */ + void enterQuery(EqlBaseParser.QueryContext ctx); + + /** + * Exit a parse tree produced by {@link EqlBaseParser#query}. + * @param ctx the parse tree + */ + void exitQuery(EqlBaseParser.QueryContext ctx); + + /** + * Enter a parse tree produced by {@link EqlBaseParser#sequenceParams}. + * @param ctx the parse tree + */ + void enterSequenceParams(EqlBaseParser.SequenceParamsContext ctx); + + /** + * Exit a parse tree produced by {@link EqlBaseParser#sequenceParams}. + * @param ctx the parse tree + */ + void exitSequenceParams(EqlBaseParser.SequenceParamsContext ctx); + + /** + * Enter a parse tree produced by {@link EqlBaseParser#sequence}. + * @param ctx the parse tree + */ + void enterSequence(EqlBaseParser.SequenceContext ctx); + + /** + * Exit a parse tree produced by {@link EqlBaseParser#sequence}. + * @param ctx the parse tree + */ + void exitSequence(EqlBaseParser.SequenceContext ctx); + + /** + * Enter a parse tree produced by {@link EqlBaseParser#join}. + * @param ctx the parse tree + */ + void enterJoin(EqlBaseParser.JoinContext ctx); + + /** + * Exit a parse tree produced by {@link EqlBaseParser#join}. + * @param ctx the parse tree + */ + void exitJoin(EqlBaseParser.JoinContext ctx); + + /** + * Enter a parse tree produced by {@link EqlBaseParser#pipe}. + * @param ctx the parse tree + */ + void enterPipe(EqlBaseParser.PipeContext ctx); + + /** + * Exit a parse tree produced by {@link EqlBaseParser#pipe}. + * @param ctx the parse tree + */ + void exitPipe(EqlBaseParser.PipeContext ctx); + + /** + * Enter a parse tree produced by {@link EqlBaseParser#joinKeys}. + * @param ctx the parse tree + */ + void enterJoinKeys(EqlBaseParser.JoinKeysContext ctx); + + /** + * Exit a parse tree produced by {@link EqlBaseParser#joinKeys}. + * @param ctx the parse tree + */ + void exitJoinKeys(EqlBaseParser.JoinKeysContext ctx); + + /** + * Enter a parse tree produced by {@link EqlBaseParser#joinTerm}. + * @param ctx the parse tree + */ + void enterJoinTerm(EqlBaseParser.JoinTermContext ctx); + + /** + * Exit a parse tree produced by {@link EqlBaseParser#joinTerm}. + * @param ctx the parse tree + */ + void exitJoinTerm(EqlBaseParser.JoinTermContext ctx); + + /** + * Enter a parse tree produced by {@link EqlBaseParser#sequenceTerm}. + * @param ctx the parse tree + */ + void enterSequenceTerm(EqlBaseParser.SequenceTermContext ctx); + + /** + * Exit a parse tree produced by {@link EqlBaseParser#sequenceTerm}. + * @param ctx the parse tree + */ + void exitSequenceTerm(EqlBaseParser.SequenceTermContext ctx); + + /** + * Enter a parse tree produced by {@link EqlBaseParser#subquery}. + * @param ctx the parse tree + */ + void enterSubquery(EqlBaseParser.SubqueryContext ctx); + + /** + * Exit a parse tree produced by {@link EqlBaseParser#subquery}. + * @param ctx the parse tree + */ + void exitSubquery(EqlBaseParser.SubqueryContext ctx); + + /** + * Enter a parse tree produced by {@link EqlBaseParser#eventQuery}. + * @param ctx the parse tree + */ + void enterEventQuery(EqlBaseParser.EventQueryContext ctx); + + /** + * Exit a parse tree produced by {@link EqlBaseParser#eventQuery}. + * @param ctx the parse tree + */ + void exitEventQuery(EqlBaseParser.EventQueryContext ctx); + + /** + * Enter a parse tree produced by {@link EqlBaseParser#eventFilter}. + * @param ctx the parse tree + */ + void enterEventFilter(EqlBaseParser.EventFilterContext ctx); + + /** + * Exit a parse tree produced by {@link EqlBaseParser#eventFilter}. + * @param ctx the parse tree + */ + void exitEventFilter(EqlBaseParser.EventFilterContext ctx); + + /** + * Enter a parse tree produced by {@link EqlBaseParser#expression}. + * @param ctx the parse tree + */ + void enterExpression(EqlBaseParser.ExpressionContext ctx); + + /** + * Exit a parse tree produced by {@link EqlBaseParser#expression}. + * @param ctx the parse tree + */ + void exitExpression(EqlBaseParser.ExpressionContext ctx); + + /** + * Enter a parse tree produced by the {@code logicalNot} + * labeled alternative in {@link EqlBaseParser#booleanExpression}. + * @param ctx the parse tree + */ + void enterLogicalNot(EqlBaseParser.LogicalNotContext ctx); + + /** + * Exit a parse tree produced by the {@code logicalNot} + * labeled alternative in {@link EqlBaseParser#booleanExpression}. + * @param ctx the parse tree + */ + void exitLogicalNot(EqlBaseParser.LogicalNotContext ctx); + + /** + * Enter a parse tree produced by the {@code booleanDefault} + * labeled alternative in {@link EqlBaseParser#booleanExpression}. + * @param ctx the parse tree + */ + void enterBooleanDefault(EqlBaseParser.BooleanDefaultContext ctx); + + /** + * Exit a parse tree produced by the {@code booleanDefault} + * labeled alternative in {@link EqlBaseParser#booleanExpression}. + * @param ctx the parse tree + */ + void exitBooleanDefault(EqlBaseParser.BooleanDefaultContext ctx); + + /** + * Enter a parse tree produced by the {@code processCheck} + * labeled alternative in {@link EqlBaseParser#booleanExpression}. + * @param ctx the parse tree + */ + void enterProcessCheck(EqlBaseParser.ProcessCheckContext ctx); + + /** + * Exit a parse tree produced by the {@code processCheck} + * labeled alternative in {@link EqlBaseParser#booleanExpression}. + * @param ctx the parse tree + */ + void exitProcessCheck(EqlBaseParser.ProcessCheckContext ctx); + + /** + * Enter a parse tree produced by the {@code logicalBinary} + * labeled alternative in {@link EqlBaseParser#booleanExpression}. + * @param ctx the parse tree + */ + void enterLogicalBinary(EqlBaseParser.LogicalBinaryContext ctx); + + /** + * Exit a parse tree produced by the {@code logicalBinary} + * labeled alternative in {@link EqlBaseParser#booleanExpression}. + * @param ctx the parse tree + */ + void exitLogicalBinary(EqlBaseParser.LogicalBinaryContext ctx); + + /** + * Enter a parse tree produced by the {@code valueExpressionDefault} + * labeled alternative in {@link EqlBaseParser#valueExpression}. + * @param ctx the parse tree + */ + void enterValueExpressionDefault(EqlBaseParser.ValueExpressionDefaultContext ctx); + + /** + * Exit a parse tree produced by the {@code valueExpressionDefault} + * labeled alternative in {@link EqlBaseParser#valueExpression}. + * @param ctx the parse tree + */ + void exitValueExpressionDefault(EqlBaseParser.ValueExpressionDefaultContext ctx); + + /** + * Enter a parse tree produced by the {@code comparison} + * labeled alternative in {@link EqlBaseParser#valueExpression}. + * @param ctx the parse tree + */ + void enterComparison(EqlBaseParser.ComparisonContext ctx); + + /** + * Exit a parse tree produced by the {@code comparison} + * labeled alternative in {@link EqlBaseParser#valueExpression}. + * @param ctx the parse tree + */ + void exitComparison(EqlBaseParser.ComparisonContext ctx); + + /** + * Enter a parse tree produced by the {@code operatorExpressionDefault} + * labeled alternative in {@link EqlBaseParser#operatorExpression}. + * @param ctx the parse tree + */ + void enterOperatorExpressionDefault(EqlBaseParser.OperatorExpressionDefaultContext ctx); + + /** + * Exit a parse tree produced by the {@code operatorExpressionDefault} + * labeled alternative in {@link EqlBaseParser#operatorExpression}. + * @param ctx the parse tree + */ + void exitOperatorExpressionDefault(EqlBaseParser.OperatorExpressionDefaultContext ctx); + + /** + * Enter a parse tree produced by the {@code arithmeticBinary} + * labeled alternative in {@link EqlBaseParser#operatorExpression}. + * @param ctx the parse tree + */ + void enterArithmeticBinary(EqlBaseParser.ArithmeticBinaryContext ctx); + + /** + * Exit a parse tree produced by the {@code arithmeticBinary} + * labeled alternative in {@link EqlBaseParser#operatorExpression}. + * @param ctx the parse tree + */ + void exitArithmeticBinary(EqlBaseParser.ArithmeticBinaryContext ctx); + + /** + * Enter a parse tree produced by the {@code arithmeticUnary} + * labeled alternative in {@link EqlBaseParser#operatorExpression}. + * @param ctx the parse tree + */ + void enterArithmeticUnary(EqlBaseParser.ArithmeticUnaryContext ctx); + + /** + * Exit a parse tree produced by the {@code arithmeticUnary} + * labeled alternative in {@link EqlBaseParser#operatorExpression}. + * @param ctx the parse tree + */ + void exitArithmeticUnary(EqlBaseParser.ArithmeticUnaryContext ctx); + + /** + * Enter a parse tree produced by {@link EqlBaseParser#predicate}. + * @param ctx the parse tree + */ + void enterPredicate(EqlBaseParser.PredicateContext ctx); + + /** + * Exit a parse tree produced by {@link EqlBaseParser#predicate}. + * @param ctx the parse tree + */ + void exitPredicate(EqlBaseParser.PredicateContext ctx); + + /** + * Enter a parse tree produced by the {@code constantDefault} + * labeled alternative in {@link EqlBaseParser#primaryExpression}. + * @param ctx the parse tree + */ + void enterConstantDefault(EqlBaseParser.ConstantDefaultContext ctx); + + /** + * Exit a parse tree produced by the {@code constantDefault} + * labeled alternative in {@link EqlBaseParser#primaryExpression}. + * @param ctx the parse tree + */ + void exitConstantDefault(EqlBaseParser.ConstantDefaultContext ctx); + + /** + * Enter a parse tree produced by the {@code function} + * labeled alternative in {@link EqlBaseParser#primaryExpression}. + * @param ctx the parse tree + */ + void enterFunction(EqlBaseParser.FunctionContext ctx); + + /** + * Exit a parse tree produced by the {@code function} + * labeled alternative in {@link EqlBaseParser#primaryExpression}. + * @param ctx the parse tree + */ + void exitFunction(EqlBaseParser.FunctionContext ctx); + + /** + * Enter a parse tree produced by the {@code dereference} + * labeled alternative in {@link EqlBaseParser#primaryExpression}. + * @param ctx the parse tree + */ + void enterDereference(EqlBaseParser.DereferenceContext ctx); + + /** + * Exit a parse tree produced by the {@code dereference} + * labeled alternative in {@link EqlBaseParser#primaryExpression}. + * @param ctx the parse tree + */ + void exitDereference(EqlBaseParser.DereferenceContext ctx); + + /** + * Enter a parse tree produced by the {@code parenthesizedExpression} + * labeled alternative in {@link EqlBaseParser#primaryExpression}. + * @param ctx the parse tree + */ + void enterParenthesizedExpression(EqlBaseParser.ParenthesizedExpressionContext ctx); + + /** + * Exit a parse tree produced by the {@code parenthesizedExpression} + * labeled alternative in {@link EqlBaseParser#primaryExpression}. + * @param ctx the parse tree + */ + void exitParenthesizedExpression(EqlBaseParser.ParenthesizedExpressionContext ctx); + + /** + * Enter a parse tree produced by {@link EqlBaseParser#functionExpression}. + * @param ctx the parse tree + */ + void enterFunctionExpression(EqlBaseParser.FunctionExpressionContext ctx); + + /** + * Exit a parse tree produced by {@link EqlBaseParser#functionExpression}. + * @param ctx the parse tree + */ + void exitFunctionExpression(EqlBaseParser.FunctionExpressionContext ctx); + + /** + * Enter a parse tree produced by {@link EqlBaseParser#functionName}. + * @param ctx the parse tree + */ + void enterFunctionName(EqlBaseParser.FunctionNameContext ctx); + + /** + * Exit a parse tree produced by {@link EqlBaseParser#functionName}. + * @param ctx the parse tree + */ + void exitFunctionName(EqlBaseParser.FunctionNameContext ctx); + + /** + * Enter a parse tree produced by the {@code nullLiteral} + * labeled alternative in {@link EqlBaseParser#constant}. + * @param ctx the parse tree + */ + void enterNullLiteral(EqlBaseParser.NullLiteralContext ctx); + + /** + * Exit a parse tree produced by the {@code nullLiteral} + * labeled alternative in {@link EqlBaseParser#constant}. + * @param ctx the parse tree + */ + void exitNullLiteral(EqlBaseParser.NullLiteralContext ctx); + + /** + * Enter a parse tree produced by the {@code numericLiteral} + * labeled alternative in {@link EqlBaseParser#constant}. + * @param ctx the parse tree + */ + void enterNumericLiteral(EqlBaseParser.NumericLiteralContext ctx); + + /** + * Exit a parse tree produced by the {@code numericLiteral} + * labeled alternative in {@link EqlBaseParser#constant}. + * @param ctx the parse tree + */ + void exitNumericLiteral(EqlBaseParser.NumericLiteralContext ctx); + + /** + * Enter a parse tree produced by the {@code booleanLiteral} + * labeled alternative in {@link EqlBaseParser#constant}. + * @param ctx the parse tree + */ + void enterBooleanLiteral(EqlBaseParser.BooleanLiteralContext ctx); + + /** + * Exit a parse tree produced by the {@code booleanLiteral} + * labeled alternative in {@link EqlBaseParser#constant}. + * @param ctx the parse tree + */ + void exitBooleanLiteral(EqlBaseParser.BooleanLiteralContext ctx); + + /** + * Enter a parse tree produced by the {@code stringLiteral} + * labeled alternative in {@link EqlBaseParser#constant}. + * @param ctx the parse tree + */ + void enterStringLiteral(EqlBaseParser.StringLiteralContext ctx); + + /** + * Exit a parse tree produced by the {@code stringLiteral} + * labeled alternative in {@link EqlBaseParser#constant}. + * @param ctx the parse tree + */ + void exitStringLiteral(EqlBaseParser.StringLiteralContext ctx); + + /** + * Enter a parse tree produced by {@link EqlBaseParser#comparisonOperator}. + * @param ctx the parse tree + */ + void enterComparisonOperator(EqlBaseParser.ComparisonOperatorContext ctx); + + /** + * Exit a parse tree produced by {@link EqlBaseParser#comparisonOperator}. + * @param ctx the parse tree + */ + void exitComparisonOperator(EqlBaseParser.ComparisonOperatorContext ctx); + + /** + * Enter a parse tree produced by {@link EqlBaseParser#booleanValue}. + * @param ctx the parse tree + */ + void enterBooleanValue(EqlBaseParser.BooleanValueContext ctx); + + /** + * Exit a parse tree produced by {@link EqlBaseParser#booleanValue}. + * @param ctx the parse tree + */ + void exitBooleanValue(EqlBaseParser.BooleanValueContext ctx); + + /** + * Enter a parse tree produced by {@link EqlBaseParser#qualifiedName}. + * @param ctx the parse tree + */ + void enterQualifiedName(EqlBaseParser.QualifiedNameContext ctx); + + /** + * Exit a parse tree produced by {@link EqlBaseParser#qualifiedName}. + * @param ctx the parse tree + */ + void exitQualifiedName(EqlBaseParser.QualifiedNameContext ctx); + + /** + * Enter a parse tree produced by {@link EqlBaseParser#identifier}. + * @param ctx the parse tree + */ + void enterIdentifier(EqlBaseParser.IdentifierContext ctx); + + /** + * Exit a parse tree produced by {@link EqlBaseParser#identifier}. + * @param ctx the parse tree + */ + void exitIdentifier(EqlBaseParser.IdentifierContext ctx); + + /** + * Enter a parse tree produced by {@link EqlBaseParser#timeUnit}. + * @param ctx the parse tree + */ + void enterTimeUnit(EqlBaseParser.TimeUnitContext ctx); + + /** + * Exit a parse tree produced by {@link EqlBaseParser#timeUnit}. + * @param ctx the parse tree + */ + void exitTimeUnit(EqlBaseParser.TimeUnitContext ctx); + + /** + * Enter a parse tree produced by the {@code decimalLiteral} + * labeled alternative in {@link EqlBaseParser#number}. + * @param ctx the parse tree + */ + void enterDecimalLiteral(EqlBaseParser.DecimalLiteralContext ctx); + + /** + * Exit a parse tree produced by the {@code decimalLiteral} + * labeled alternative in {@link EqlBaseParser#number}. + * @param ctx the parse tree + */ + void exitDecimalLiteral(EqlBaseParser.DecimalLiteralContext ctx); + + /** + * Enter a parse tree produced by the {@code integerLiteral} + * labeled alternative in {@link EqlBaseParser#number}. + * @param ctx the parse tree + */ + void enterIntegerLiteral(EqlBaseParser.IntegerLiteralContext ctx); + + /** + * Exit a parse tree produced by the {@code integerLiteral} + * labeled alternative in {@link EqlBaseParser#number}. + * @param ctx the parse tree + */ + void exitIntegerLiteral(EqlBaseParser.IntegerLiteralContext ctx); + + /** + * Enter a parse tree produced by {@link EqlBaseParser#string}. + * @param ctx the parse tree + */ + void enterString(EqlBaseParser.StringContext ctx); + + /** + * Exit a parse tree produced by {@link EqlBaseParser#string}. + * @param ctx the parse tree + */ + void exitString(EqlBaseParser.StringContext ctx); + + /** + * Enter a parse tree produced by {@link EqlBaseParser#eventValue}. + * @param ctx the parse tree + */ + void enterEventValue(EqlBaseParser.EventValueContext ctx); + + /** + * Exit a parse tree produced by {@link EqlBaseParser#eventValue}. + * @param ctx the parse tree + */ + void exitEventValue(EqlBaseParser.EventValueContext ctx); } diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/parser/EqlBaseParser.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/parser/EqlBaseParser.java index 53837f766ab4d..efd1728a3d8c9 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/parser/EqlBaseParser.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/parser/EqlBaseParser.java @@ -1,2945 +1,3582 @@ // ANTLR GENERATED CODE: DO NOT EDIT package org.elasticsearch.xpack.eql.parser; + +import org.antlr.v4.runtime.*; import org.antlr.v4.runtime.atn.*; import org.antlr.v4.runtime.dfa.DFA; -import org.antlr.v4.runtime.*; import org.antlr.v4.runtime.misc.*; import org.antlr.v4.runtime.tree.*; + import java.util.List; -import java.util.Iterator; -import java.util.ArrayList; -@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"}) +@SuppressWarnings({ "all", "warnings", "unchecked", "unused", "cast" }) class EqlBaseParser extends Parser { - static { RuntimeMetaData.checkVersion("4.9.2", RuntimeMetaData.VERSION); } - - protected static final DFA[] _decisionToDFA; - protected static final PredictionContextCache _sharedContextCache = - new PredictionContextCache(); - public static final int - AND=1, ANY=2, BY=3, FALSE=4, IN=5, IN_INSENSITIVE=6, JOIN=7, LIKE=8, LIKE_INSENSITIVE=9, - MAXSPAN=10, NOT=11, NULL=12, OF=13, OR=14, REGEX=15, REGEX_INSENSITIVE=16, - SEQUENCE=17, TRUE=18, UNTIL=19, WHERE=20, WITH=21, SEQ=22, ASGN=23, EQ=24, - NEQ=25, LT=26, LTE=27, GT=28, GTE=29, PLUS=30, MINUS=31, ASTERISK=32, - SLASH=33, PERCENT=34, DOT=35, COMMA=36, LB=37, RB=38, LP=39, RP=40, PIPE=41, - OPTIONAL=42, STRING=43, INTEGER_VALUE=44, DECIMAL_VALUE=45, IDENTIFIER=46, - QUOTED_IDENTIFIER=47, TILDE_IDENTIFIER=48, LINE_COMMENT=49, BRACKETED_COMMENT=50, - WS=51; - public static final int - RULE_singleStatement = 0, RULE_singleExpression = 1, RULE_statement = 2, - RULE_query = 3, RULE_sequenceParams = 4, RULE_sequence = 5, RULE_join = 6, - RULE_pipe = 7, RULE_joinKeys = 8, RULE_joinTerm = 9, RULE_sequenceTerm = 10, - RULE_subquery = 11, RULE_eventQuery = 12, RULE_eventFilter = 13, RULE_expression = 14, - RULE_booleanExpression = 15, RULE_valueExpression = 16, RULE_operatorExpression = 17, - RULE_predicate = 18, RULE_primaryExpression = 19, RULE_functionExpression = 20, - RULE_functionName = 21, RULE_constant = 22, RULE_comparisonOperator = 23, - RULE_booleanValue = 24, RULE_qualifiedName = 25, RULE_identifier = 26, - RULE_timeUnit = 27, RULE_number = 28, RULE_string = 29, RULE_eventValue = 30; - private static String[] makeRuleNames() { - return new String[] { - "singleStatement", "singleExpression", "statement", "query", "sequenceParams", - "sequence", "join", "pipe", "joinKeys", "joinTerm", "sequenceTerm", "subquery", - "eventQuery", "eventFilter", "expression", "booleanExpression", "valueExpression", - "operatorExpression", "predicate", "primaryExpression", "functionExpression", - "functionName", "constant", "comparisonOperator", "booleanValue", "qualifiedName", - "identifier", "timeUnit", "number", "string", "eventValue" - }; - } - public static final String[] ruleNames = makeRuleNames(); - - private static String[] makeLiteralNames() { - return new String[] { - null, "'and'", "'any'", "'by'", "'false'", "'in'", "'in~'", "'join'", - "'like'", "'like~'", "'maxspan'", "'not'", "'null'", "'of'", "'or'", - "'regex'", "'regex~'", "'sequence'", "'true'", "'until'", "'where'", - "'with'", "':'", "'='", "'=='", "'!='", "'<'", "'<='", "'>'", "'>='", - "'+'", "'-'", "'*'", "'/'", "'%'", "'.'", "','", "'['", "']'", "'('", - "')'", "'|'", "'?'" - }; - } - private static final String[] _LITERAL_NAMES = makeLiteralNames(); - private static String[] makeSymbolicNames() { - return new String[] { - null, "AND", "ANY", "BY", "FALSE", "IN", "IN_INSENSITIVE", "JOIN", "LIKE", - "LIKE_INSENSITIVE", "MAXSPAN", "NOT", "NULL", "OF", "OR", "REGEX", "REGEX_INSENSITIVE", - "SEQUENCE", "TRUE", "UNTIL", "WHERE", "WITH", "SEQ", "ASGN", "EQ", "NEQ", - "LT", "LTE", "GT", "GTE", "PLUS", "MINUS", "ASTERISK", "SLASH", "PERCENT", - "DOT", "COMMA", "LB", "RB", "LP", "RP", "PIPE", "OPTIONAL", "STRING", - "INTEGER_VALUE", "DECIMAL_VALUE", "IDENTIFIER", "QUOTED_IDENTIFIER", - "TILDE_IDENTIFIER", "LINE_COMMENT", "BRACKETED_COMMENT", "WS" - }; - } - private static final String[] _SYMBOLIC_NAMES = makeSymbolicNames(); - public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); - - /** - * @deprecated Use {@link #VOCABULARY} instead. - */ - @Deprecated - public static final String[] tokenNames; - static { - tokenNames = new String[_SYMBOLIC_NAMES.length]; - for (int i = 0; i < tokenNames.length; i++) { - tokenNames[i] = VOCABULARY.getLiteralName(i); - if (tokenNames[i] == null) { - tokenNames[i] = VOCABULARY.getSymbolicName(i); - } - - if (tokenNames[i] == null) { - tokenNames[i] = ""; - } - } - } - - @Override - @Deprecated - public String[] getTokenNames() { - return tokenNames; - } - - @Override - - public Vocabulary getVocabulary() { - return VOCABULARY; - } - - @Override - public String getGrammarFileName() { return "EqlBase.g4"; } - - @Override - public String[] getRuleNames() { return ruleNames; } - - @Override - public String getSerializedATN() { return _serializedATN; } - - @Override - public ATN getATN() { return _ATN; } - - public EqlBaseParser(TokenStream input) { - super(input); - _interp = new ParserATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache); - } - - public static class SingleStatementContext extends ParserRuleContext { - public StatementContext statement() { - return getRuleContext(StatementContext.class,0); - } - public TerminalNode EOF() { return getToken(EqlBaseParser.EOF, 0); } - public SingleStatementContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_singleStatement; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).enterSingleStatement(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).exitSingleStatement(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof EqlBaseVisitor ) return ((EqlBaseVisitor)visitor).visitSingleStatement(this); - else return visitor.visitChildren(this); - } - } - - public final SingleStatementContext singleStatement() throws RecognitionException { - SingleStatementContext _localctx = new SingleStatementContext(_ctx, getState()); - enterRule(_localctx, 0, RULE_singleStatement); - try { - enterOuterAlt(_localctx, 1); - { - setState(62); - statement(); - setState(63); - match(EOF); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class SingleExpressionContext extends ParserRuleContext { - public ExpressionContext expression() { - return getRuleContext(ExpressionContext.class,0); - } - public TerminalNode EOF() { return getToken(EqlBaseParser.EOF, 0); } - public SingleExpressionContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_singleExpression; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).enterSingleExpression(this); + static { + RuntimeMetaData.checkVersion("4.9.2", RuntimeMetaData.VERSION); + } + + protected static final DFA[] _decisionToDFA; + protected static final PredictionContextCache _sharedContextCache = new PredictionContextCache(); + public static final int AND = 1, ANY = 2, BY = 3, FALSE = 4, IN = 5, IN_INSENSITIVE = 6, JOIN = 7, LIKE = 8, LIKE_INSENSITIVE = 9, + MAXSPAN = 10, NOT = 11, NULL = 12, OF = 13, OR = 14, REGEX = 15, REGEX_INSENSITIVE = 16, SEQUENCE = 17, TRUE = 18, UNTIL = 19, + WHERE = 20, WITH = 21, SEQ = 22, ASGN = 23, EQ = 24, NEQ = 25, LT = 26, LTE = 27, GT = 28, GTE = 29, PLUS = 30, MINUS = 31, + ASTERISK = 32, SLASH = 33, PERCENT = 34, DOT = 35, COMMA = 36, LB = 37, RB = 38, LP = 39, RP = 40, PIPE = 41, OPTIONAL = 42, + STRING = 43, INTEGER_VALUE = 44, DECIMAL_VALUE = 45, IDENTIFIER = 46, QUOTED_IDENTIFIER = 47, TILDE_IDENTIFIER = 48, LINE_COMMENT = + 49, BRACKETED_COMMENT = 50, WS = 51; + public static final int RULE_singleStatement = 0, RULE_singleExpression = 1, RULE_statement = 2, RULE_query = 3, RULE_sequenceParams = + 4, RULE_sequence = 5, RULE_join = 6, RULE_pipe = 7, RULE_joinKeys = 8, RULE_joinTerm = 9, RULE_sequenceTerm = 10, RULE_subquery = + 11, RULE_eventQuery = 12, RULE_eventFilter = 13, RULE_expression = 14, RULE_booleanExpression = 15, RULE_valueExpression = 16, + RULE_operatorExpression = 17, RULE_predicate = 18, RULE_primaryExpression = 19, RULE_functionExpression = 20, RULE_functionName = + 21, RULE_constant = 22, RULE_comparisonOperator = 23, RULE_booleanValue = 24, RULE_qualifiedName = 25, RULE_identifier = 26, + RULE_timeUnit = 27, RULE_number = 28, RULE_string = 29, RULE_eventValue = 30; + + private static String[] makeRuleNames() { + return new String[] { + "singleStatement", + "singleExpression", + "statement", + "query", + "sequenceParams", + "sequence", + "join", + "pipe", + "joinKeys", + "joinTerm", + "sequenceTerm", + "subquery", + "eventQuery", + "eventFilter", + "expression", + "booleanExpression", + "valueExpression", + "operatorExpression", + "predicate", + "primaryExpression", + "functionExpression", + "functionName", + "constant", + "comparisonOperator", + "booleanValue", + "qualifiedName", + "identifier", + "timeUnit", + "number", + "string", + "eventValue" }; + } + + public static final String[] ruleNames = makeRuleNames(); + + private static String[] makeLiteralNames() { + return new String[] { + null, + "'and'", + "'any'", + "'by'", + "'false'", + "'in'", + "'in~'", + "'join'", + "'like'", + "'like~'", + "'maxspan'", + "'not'", + "'null'", + "'of'", + "'or'", + "'regex'", + "'regex~'", + "'sequence'", + "'true'", + "'until'", + "'where'", + "'with'", + "':'", + "'='", + "'=='", + "'!='", + "'<'", + "'<='", + "'>'", + "'>='", + "'+'", + "'-'", + "'*'", + "'/'", + "'%'", + "'.'", + "','", + "'['", + "']'", + "'('", + "')'", + "'|'", + "'?'" }; + } + + private static final String[] _LITERAL_NAMES = makeLiteralNames(); + + private static String[] makeSymbolicNames() { + return new String[] { + null, + "AND", + "ANY", + "BY", + "FALSE", + "IN", + "IN_INSENSITIVE", + "JOIN", + "LIKE", + "LIKE_INSENSITIVE", + "MAXSPAN", + "NOT", + "NULL", + "OF", + "OR", + "REGEX", + "REGEX_INSENSITIVE", + "SEQUENCE", + "TRUE", + "UNTIL", + "WHERE", + "WITH", + "SEQ", + "ASGN", + "EQ", + "NEQ", + "LT", + "LTE", + "GT", + "GTE", + "PLUS", + "MINUS", + "ASTERISK", + "SLASH", + "PERCENT", + "DOT", + "COMMA", + "LB", + "RB", + "LP", + "RP", + "PIPE", + "OPTIONAL", + "STRING", + "INTEGER_VALUE", + "DECIMAL_VALUE", + "IDENTIFIER", + "QUOTED_IDENTIFIER", + "TILDE_IDENTIFIER", + "LINE_COMMENT", + "BRACKETED_COMMENT", + "WS" }; + } + + private static final String[] _SYMBOLIC_NAMES = makeSymbolicNames(); + public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); + + /** + * @deprecated Use {@link #VOCABULARY} instead. + */ + @Deprecated + public static final String[] tokenNames; + static { + tokenNames = new String[_SYMBOLIC_NAMES.length]; + for (int i = 0; i < tokenNames.length; i++) { + tokenNames[i] = VOCABULARY.getLiteralName(i); + if (tokenNames[i] == null) { + tokenNames[i] = VOCABULARY.getSymbolicName(i); + } + + if (tokenNames[i] == null) { + tokenNames[i] = ""; + } + } } + @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).exitSingleExpression(this); + @Deprecated + public String[] getTokenNames() { + return tokenNames; } + @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof EqlBaseVisitor ) return ((EqlBaseVisitor)visitor).visitSingleExpression(this); - else return visitor.visitChildren(this); - } - } - public final SingleExpressionContext singleExpression() throws RecognitionException { - SingleExpressionContext _localctx = new SingleExpressionContext(_ctx, getState()); - enterRule(_localctx, 2, RULE_singleExpression); - try { - enterOuterAlt(_localctx, 1); - { - setState(65); - expression(); - setState(66); - match(EOF); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); + public Vocabulary getVocabulary() { + return VOCABULARY; } - finally { - exitRule(); - } - return _localctx; - } - public static class StatementContext extends ParserRuleContext { - public QueryContext query() { - return getRuleContext(QueryContext.class,0); - } - public List pipe() { - return getRuleContexts(PipeContext.class); - } - public PipeContext pipe(int i) { - return getRuleContext(PipeContext.class,i); - } - public StatementContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_statement; } @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).enterStatement(this); + public String getGrammarFileName() { + return "EqlBase.g4"; } + @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).exitStatement(this); + public String[] getRuleNames() { + return ruleNames; } + @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof EqlBaseVisitor ) return ((EqlBaseVisitor)visitor).visitStatement(this); - else return visitor.visitChildren(this); - } - } - - public final StatementContext statement() throws RecognitionException { - StatementContext _localctx = new StatementContext(_ctx, getState()); - enterRule(_localctx, 4, RULE_statement); - int _la; - try { - enterOuterAlt(_localctx, 1); - { - setState(68); - query(); - setState(72); - _errHandler.sync(this); - _la = _input.LA(1); - while (_la==PIPE) { - { - { - setState(69); - pipe(); - } - } - setState(74); - _errHandler.sync(this); - _la = _input.LA(1); - } - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class QueryContext extends ParserRuleContext { - public SequenceContext sequence() { - return getRuleContext(SequenceContext.class,0); - } - public JoinContext join() { - return getRuleContext(JoinContext.class,0); - } - public EventQueryContext eventQuery() { - return getRuleContext(EventQueryContext.class,0); - } - public QueryContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_query; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).enterQuery(this); + public String getSerializedATN() { + return _serializedATN; } + @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).exitQuery(this); + public ATN getATN() { + return _ATN; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof EqlBaseVisitor ) return ((EqlBaseVisitor)visitor).visitQuery(this); - else return visitor.visitChildren(this); - } - } - - public final QueryContext query() throws RecognitionException { - QueryContext _localctx = new QueryContext(_ctx, getState()); - enterRule(_localctx, 6, RULE_query); - try { - setState(78); - _errHandler.sync(this); - switch (_input.LA(1)) { - case SEQUENCE: - enterOuterAlt(_localctx, 1); - { - setState(75); - sequence(); - } - break; - case JOIN: - enterOuterAlt(_localctx, 2); - { - setState(76); - join(); - } - break; - case ANY: - case STRING: - case IDENTIFIER: - enterOuterAlt(_localctx, 3); - { - setState(77); - eventQuery(); - } - break; - default: - throw new NoViableAltException(this); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class SequenceParamsContext extends ParserRuleContext { - public TerminalNode WITH() { return getToken(EqlBaseParser.WITH, 0); } - public TerminalNode MAXSPAN() { return getToken(EqlBaseParser.MAXSPAN, 0); } - public TerminalNode ASGN() { return getToken(EqlBaseParser.ASGN, 0); } - public TimeUnitContext timeUnit() { - return getRuleContext(TimeUnitContext.class,0); - } - public SequenceParamsContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_sequenceParams; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).enterSequenceParams(this); + + public EqlBaseParser(TokenStream input) { + super(input); + _interp = new ParserATNSimulator(this, _ATN, _decisionToDFA, _sharedContextCache); } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).exitSequenceParams(this); + + public static class SingleStatementContext extends ParserRuleContext { + public StatementContext statement() { + return getRuleContext(StatementContext.class, 0); + } + + public TerminalNode EOF() { + return getToken(EqlBaseParser.EOF, 0); + } + + public SingleStatementContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_singleStatement; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).enterSingleStatement(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).exitSingleStatement(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof EqlBaseVisitor) return ((EqlBaseVisitor) visitor).visitSingleStatement(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof EqlBaseVisitor ) return ((EqlBaseVisitor)visitor).visitSequenceParams(this); - else return visitor.visitChildren(this); - } - } - - public final SequenceParamsContext sequenceParams() throws RecognitionException { - SequenceParamsContext _localctx = new SequenceParamsContext(_ctx, getState()); - enterRule(_localctx, 8, RULE_sequenceParams); - try { - enterOuterAlt(_localctx, 1); - { - setState(80); - match(WITH); - { - setState(81); - match(MAXSPAN); - setState(82); - match(ASGN); - setState(83); - timeUnit(); - } - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class SequenceContext extends ParserRuleContext { - public JoinKeysContext by; - public JoinKeysContext disallowed; - public SequenceTermContext until; - public TerminalNode SEQUENCE() { return getToken(EqlBaseParser.SEQUENCE, 0); } - public SequenceParamsContext sequenceParams() { - return getRuleContext(SequenceParamsContext.class,0); - } - public List sequenceTerm() { - return getRuleContexts(SequenceTermContext.class); - } - public SequenceTermContext sequenceTerm(int i) { - return getRuleContext(SequenceTermContext.class,i); - } - public TerminalNode UNTIL() { return getToken(EqlBaseParser.UNTIL, 0); } - public JoinKeysContext joinKeys() { - return getRuleContext(JoinKeysContext.class,0); - } - public SequenceContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_sequence; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).enterSequence(this); + + public final SingleStatementContext singleStatement() throws RecognitionException { + SingleStatementContext _localctx = new SingleStatementContext(_ctx, getState()); + enterRule(_localctx, 0, RULE_singleStatement); + try { + enterOuterAlt(_localctx, 1); + { + setState(62); + statement(); + setState(63); + match(EOF); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).exitSequence(this); + + public static class SingleExpressionContext extends ParserRuleContext { + public ExpressionContext expression() { + return getRuleContext(ExpressionContext.class, 0); + } + + public TerminalNode EOF() { + return getToken(EqlBaseParser.EOF, 0); + } + + public SingleExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_singleExpression; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).enterSingleExpression(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).exitSingleExpression(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof EqlBaseVisitor) return ((EqlBaseVisitor) visitor).visitSingleExpression(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof EqlBaseVisitor ) return ((EqlBaseVisitor)visitor).visitSequence(this); - else return visitor.visitChildren(this); - } - } - - public final SequenceContext sequence() throws RecognitionException { - SequenceContext _localctx = new SequenceContext(_ctx, getState()); - enterRule(_localctx, 10, RULE_sequence); - int _la; - try { - enterOuterAlt(_localctx, 1); - { - setState(85); - match(SEQUENCE); - setState(94); - _errHandler.sync(this); - switch (_input.LA(1)) { - case BY: - { - setState(86); - ((SequenceContext)_localctx).by = joinKeys(); - setState(88); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==WITH) { - { - setState(87); - sequenceParams(); - } - } - - } - break; - case WITH: - { - setState(90); - sequenceParams(); - setState(92); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==BY) { - { - setState(91); - ((SequenceContext)_localctx).disallowed = joinKeys(); - } - } - - } - break; - case LB: - break; - default: - break; - } - setState(97); - _errHandler.sync(this); - _la = _input.LA(1); - do { - { - { - setState(96); - sequenceTerm(); - } - } - setState(99); - _errHandler.sync(this); - _la = _input.LA(1); - } while ( _la==LB ); - setState(103); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==UNTIL) { - { - setState(101); - match(UNTIL); - setState(102); - ((SequenceContext)_localctx).until = sequenceTerm(); - } - } - - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class JoinContext extends ParserRuleContext { - public JoinKeysContext by; - public JoinTermContext until; - public TerminalNode JOIN() { return getToken(EqlBaseParser.JOIN, 0); } - public List joinTerm() { - return getRuleContexts(JoinTermContext.class); - } - public JoinTermContext joinTerm(int i) { - return getRuleContext(JoinTermContext.class,i); - } - public TerminalNode UNTIL() { return getToken(EqlBaseParser.UNTIL, 0); } - public JoinKeysContext joinKeys() { - return getRuleContext(JoinKeysContext.class,0); - } - public JoinContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_join; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).enterJoin(this); + + public final SingleExpressionContext singleExpression() throws RecognitionException { + SingleExpressionContext _localctx = new SingleExpressionContext(_ctx, getState()); + enterRule(_localctx, 2, RULE_singleExpression); + try { + enterOuterAlt(_localctx, 1); + { + setState(65); + expression(); + setState(66); + match(EOF); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).exitJoin(this); + + public static class StatementContext extends ParserRuleContext { + public QueryContext query() { + return getRuleContext(QueryContext.class, 0); + } + + public List pipe() { + return getRuleContexts(PipeContext.class); + } + + public PipeContext pipe(int i) { + return getRuleContext(PipeContext.class, i); + } + + public StatementContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_statement; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).enterStatement(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).exitStatement(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof EqlBaseVisitor) return ((EqlBaseVisitor) visitor).visitStatement(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof EqlBaseVisitor ) return ((EqlBaseVisitor)visitor).visitJoin(this); - else return visitor.visitChildren(this); - } - } - - public final JoinContext join() throws RecognitionException { - JoinContext _localctx = new JoinContext(_ctx, getState()); - enterRule(_localctx, 12, RULE_join); - int _la; - try { - enterOuterAlt(_localctx, 1); - { - setState(105); - match(JOIN); - setState(107); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==BY) { - { - setState(106); - ((JoinContext)_localctx).by = joinKeys(); - } - } - - setState(109); - joinTerm(); - setState(111); - _errHandler.sync(this); - _la = _input.LA(1); - do { - { - { - setState(110); - joinTerm(); - } - } - setState(113); - _errHandler.sync(this); - _la = _input.LA(1); - } while ( _la==LB ); - setState(117); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==UNTIL) { - { - setState(115); - match(UNTIL); - setState(116); - ((JoinContext)_localctx).until = joinTerm(); - } - } - - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class PipeContext extends ParserRuleContext { - public Token kind; - public TerminalNode PIPE() { return getToken(EqlBaseParser.PIPE, 0); } - public TerminalNode IDENTIFIER() { return getToken(EqlBaseParser.IDENTIFIER, 0); } - public List booleanExpression() { - return getRuleContexts(BooleanExpressionContext.class); - } - public BooleanExpressionContext booleanExpression(int i) { - return getRuleContext(BooleanExpressionContext.class,i); - } - public List COMMA() { return getTokens(EqlBaseParser.COMMA); } - public TerminalNode COMMA(int i) { - return getToken(EqlBaseParser.COMMA, i); - } - public PipeContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_pipe; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).enterPipe(this); + + public final StatementContext statement() throws RecognitionException { + StatementContext _localctx = new StatementContext(_ctx, getState()); + enterRule(_localctx, 4, RULE_statement); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(68); + query(); + setState(72); + _errHandler.sync(this); + _la = _input.LA(1); + while (_la == PIPE) { + { + { + setState(69); + pipe(); + } + } + setState(74); + _errHandler.sync(this); + _la = _input.LA(1); + } + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).exitPipe(this); + + public static class QueryContext extends ParserRuleContext { + public SequenceContext sequence() { + return getRuleContext(SequenceContext.class, 0); + } + + public JoinContext join() { + return getRuleContext(JoinContext.class, 0); + } + + public EventQueryContext eventQuery() { + return getRuleContext(EventQueryContext.class, 0); + } + + public QueryContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_query; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).enterQuery(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).exitQuery(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof EqlBaseVisitor) return ((EqlBaseVisitor) visitor).visitQuery(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof EqlBaseVisitor ) return ((EqlBaseVisitor)visitor).visitPipe(this); - else return visitor.visitChildren(this); - } - } - - public final PipeContext pipe() throws RecognitionException { - PipeContext _localctx = new PipeContext(_ctx, getState()); - enterRule(_localctx, 14, RULE_pipe); - int _la; - try { - enterOuterAlt(_localctx, 1); - { - setState(119); - match(PIPE); - setState(120); - ((PipeContext)_localctx).kind = match(IDENTIFIER); - setState(129); - _errHandler.sync(this); - _la = _input.LA(1); - if ((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << FALSE) | (1L << NOT) | (1L << NULL) | (1L << TRUE) | (1L << PLUS) | (1L << MINUS) | (1L << LP) | (1L << OPTIONAL) | (1L << STRING) | (1L << INTEGER_VALUE) | (1L << DECIMAL_VALUE) | (1L << IDENTIFIER) | (1L << QUOTED_IDENTIFIER) | (1L << TILDE_IDENTIFIER))) != 0)) { - { - setState(121); - booleanExpression(0); - setState(126); - _errHandler.sync(this); - _la = _input.LA(1); - while (_la==COMMA) { - { - { - setState(122); - match(COMMA); - setState(123); - booleanExpression(0); - } - } - setState(128); - _errHandler.sync(this); - _la = _input.LA(1); - } - } - } - - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class JoinKeysContext extends ParserRuleContext { - public TerminalNode BY() { return getToken(EqlBaseParser.BY, 0); } - public List expression() { - return getRuleContexts(ExpressionContext.class); - } - public ExpressionContext expression(int i) { - return getRuleContext(ExpressionContext.class,i); - } - public List COMMA() { return getTokens(EqlBaseParser.COMMA); } - public TerminalNode COMMA(int i) { - return getToken(EqlBaseParser.COMMA, i); - } - public JoinKeysContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_joinKeys; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).enterJoinKeys(this); + + public final QueryContext query() throws RecognitionException { + QueryContext _localctx = new QueryContext(_ctx, getState()); + enterRule(_localctx, 6, RULE_query); + try { + setState(78); + _errHandler.sync(this); + switch (_input.LA(1)) { + case SEQUENCE: + enterOuterAlt(_localctx, 1); { + setState(75); + sequence(); + } + break; + case JOIN: + enterOuterAlt(_localctx, 2); { + setState(76); + join(); + } + break; + case ANY: + case STRING: + case IDENTIFIER: + enterOuterAlt(_localctx, 3); { + setState(77); + eventQuery(); + } + break; + default: + throw new NoViableAltException(this); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).exitJoinKeys(this); + + public static class SequenceParamsContext extends ParserRuleContext { + public TerminalNode WITH() { + return getToken(EqlBaseParser.WITH, 0); + } + + public TerminalNode MAXSPAN() { + return getToken(EqlBaseParser.MAXSPAN, 0); + } + + public TerminalNode ASGN() { + return getToken(EqlBaseParser.ASGN, 0); + } + + public TimeUnitContext timeUnit() { + return getRuleContext(TimeUnitContext.class, 0); + } + + public SequenceParamsContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_sequenceParams; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).enterSequenceParams(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).exitSequenceParams(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof EqlBaseVisitor) return ((EqlBaseVisitor) visitor).visitSequenceParams(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof EqlBaseVisitor ) return ((EqlBaseVisitor)visitor).visitJoinKeys(this); - else return visitor.visitChildren(this); - } - } - - public final JoinKeysContext joinKeys() throws RecognitionException { - JoinKeysContext _localctx = new JoinKeysContext(_ctx, getState()); - enterRule(_localctx, 16, RULE_joinKeys); - int _la; - try { - enterOuterAlt(_localctx, 1); - { - setState(131); - match(BY); - setState(132); - expression(); - setState(137); - _errHandler.sync(this); - _la = _input.LA(1); - while (_la==COMMA) { - { - { - setState(133); - match(COMMA); - setState(134); - expression(); - } - } - setState(139); - _errHandler.sync(this); - _la = _input.LA(1); - } - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class JoinTermContext extends ParserRuleContext { - public JoinKeysContext by; - public SubqueryContext subquery() { - return getRuleContext(SubqueryContext.class,0); - } - public JoinKeysContext joinKeys() { - return getRuleContext(JoinKeysContext.class,0); - } - public JoinTermContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_joinTerm; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).enterJoinTerm(this); + + public final SequenceParamsContext sequenceParams() throws RecognitionException { + SequenceParamsContext _localctx = new SequenceParamsContext(_ctx, getState()); + enterRule(_localctx, 8, RULE_sequenceParams); + try { + enterOuterAlt(_localctx, 1); + { + setState(80); + match(WITH); + { + setState(81); + match(MAXSPAN); + setState(82); + match(ASGN); + setState(83); + timeUnit(); + } + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).exitJoinTerm(this); + + public static class SequenceContext extends ParserRuleContext { + public JoinKeysContext by; + public JoinKeysContext disallowed; + public SequenceTermContext until; + + public TerminalNode SEQUENCE() { + return getToken(EqlBaseParser.SEQUENCE, 0); + } + + public SequenceParamsContext sequenceParams() { + return getRuleContext(SequenceParamsContext.class, 0); + } + + public List sequenceTerm() { + return getRuleContexts(SequenceTermContext.class); + } + + public SequenceTermContext sequenceTerm(int i) { + return getRuleContext(SequenceTermContext.class, i); + } + + public TerminalNode UNTIL() { + return getToken(EqlBaseParser.UNTIL, 0); + } + + public JoinKeysContext joinKeys() { + return getRuleContext(JoinKeysContext.class, 0); + } + + public SequenceContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_sequence; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).enterSequence(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).exitSequence(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof EqlBaseVisitor) return ((EqlBaseVisitor) visitor).visitSequence(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof EqlBaseVisitor ) return ((EqlBaseVisitor)visitor).visitJoinTerm(this); - else return visitor.visitChildren(this); - } - } - - public final JoinTermContext joinTerm() throws RecognitionException { - JoinTermContext _localctx = new JoinTermContext(_ctx, getState()); - enterRule(_localctx, 18, RULE_joinTerm); - int _la; - try { - enterOuterAlt(_localctx, 1); - { - setState(140); - subquery(); - setState(142); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==BY) { - { - setState(141); - ((JoinTermContext)_localctx).by = joinKeys(); - } - } - - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class SequenceTermContext extends ParserRuleContext { - public JoinKeysContext by; - public Token key; - public NumberContext value; - public SubqueryContext subquery() { - return getRuleContext(SubqueryContext.class,0); - } - public TerminalNode WITH() { return getToken(EqlBaseParser.WITH, 0); } - public TerminalNode ASGN() { return getToken(EqlBaseParser.ASGN, 0); } - public JoinKeysContext joinKeys() { - return getRuleContext(JoinKeysContext.class,0); - } - public TerminalNode IDENTIFIER() { return getToken(EqlBaseParser.IDENTIFIER, 0); } - public NumberContext number() { - return getRuleContext(NumberContext.class,0); - } - public SequenceTermContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_sequenceTerm; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).enterSequenceTerm(this); + + public final SequenceContext sequence() throws RecognitionException { + SequenceContext _localctx = new SequenceContext(_ctx, getState()); + enterRule(_localctx, 10, RULE_sequence); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(85); + match(SEQUENCE); + setState(94); + _errHandler.sync(this); + switch (_input.LA(1)) { + case BY: { + setState(86); + ((SequenceContext) _localctx).by = joinKeys(); + setState(88); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == WITH) { + { + setState(87); + sequenceParams(); + } + } + + } + break; + case WITH: { + setState(90); + sequenceParams(); + setState(92); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == BY) { + { + setState(91); + ((SequenceContext) _localctx).disallowed = joinKeys(); + } + } + + } + break; + case LB: + break; + default: + break; + } + setState(97); + _errHandler.sync(this); + _la = _input.LA(1); + do { + { + { + setState(96); + sequenceTerm(); + } + } + setState(99); + _errHandler.sync(this); + _la = _input.LA(1); + } while (_la == LB); + setState(103); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == UNTIL) { + { + setState(101); + match(UNTIL); + setState(102); + ((SequenceContext) _localctx).until = sequenceTerm(); + } + } + + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).exitSequenceTerm(this); + + public static class JoinContext extends ParserRuleContext { + public JoinKeysContext by; + public JoinTermContext until; + + public TerminalNode JOIN() { + return getToken(EqlBaseParser.JOIN, 0); + } + + public List joinTerm() { + return getRuleContexts(JoinTermContext.class); + } + + public JoinTermContext joinTerm(int i) { + return getRuleContext(JoinTermContext.class, i); + } + + public TerminalNode UNTIL() { + return getToken(EqlBaseParser.UNTIL, 0); + } + + public JoinKeysContext joinKeys() { + return getRuleContext(JoinKeysContext.class, 0); + } + + public JoinContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_join; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).enterJoin(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).exitJoin(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof EqlBaseVisitor) return ((EqlBaseVisitor) visitor).visitJoin(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof EqlBaseVisitor ) return ((EqlBaseVisitor)visitor).visitSequenceTerm(this); - else return visitor.visitChildren(this); - } - } - - public final SequenceTermContext sequenceTerm() throws RecognitionException { - SequenceTermContext _localctx = new SequenceTermContext(_ctx, getState()); - enterRule(_localctx, 20, RULE_sequenceTerm); - int _la; - try { - enterOuterAlt(_localctx, 1); - { - setState(144); - subquery(); - setState(146); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==BY) { - { - setState(145); - ((SequenceTermContext)_localctx).by = joinKeys(); - } - } - - setState(152); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==WITH) { - { - setState(148); - match(WITH); - setState(149); - ((SequenceTermContext)_localctx).key = match(IDENTIFIER); - setState(150); - match(ASGN); - setState(151); - ((SequenceTermContext)_localctx).value = number(); - } - } - - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class SubqueryContext extends ParserRuleContext { - public TerminalNode LB() { return getToken(EqlBaseParser.LB, 0); } - public EventFilterContext eventFilter() { - return getRuleContext(EventFilterContext.class,0); - } - public TerminalNode RB() { return getToken(EqlBaseParser.RB, 0); } - public SubqueryContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_subquery; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).enterSubquery(this); + + public final JoinContext join() throws RecognitionException { + JoinContext _localctx = new JoinContext(_ctx, getState()); + enterRule(_localctx, 12, RULE_join); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(105); + match(JOIN); + setState(107); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == BY) { + { + setState(106); + ((JoinContext) _localctx).by = joinKeys(); + } + } + + setState(109); + joinTerm(); + setState(111); + _errHandler.sync(this); + _la = _input.LA(1); + do { + { + { + setState(110); + joinTerm(); + } + } + setState(113); + _errHandler.sync(this); + _la = _input.LA(1); + } while (_la == LB); + setState(117); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == UNTIL) { + { + setState(115); + match(UNTIL); + setState(116); + ((JoinContext) _localctx).until = joinTerm(); + } + } + + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).exitSubquery(this); + + public static class PipeContext extends ParserRuleContext { + public Token kind; + + public TerminalNode PIPE() { + return getToken(EqlBaseParser.PIPE, 0); + } + + public TerminalNode IDENTIFIER() { + return getToken(EqlBaseParser.IDENTIFIER, 0); + } + + public List booleanExpression() { + return getRuleContexts(BooleanExpressionContext.class); + } + + public BooleanExpressionContext booleanExpression(int i) { + return getRuleContext(BooleanExpressionContext.class, i); + } + + public List COMMA() { + return getTokens(EqlBaseParser.COMMA); + } + + public TerminalNode COMMA(int i) { + return getToken(EqlBaseParser.COMMA, i); + } + + public PipeContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_pipe; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).enterPipe(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).exitPipe(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof EqlBaseVisitor) return ((EqlBaseVisitor) visitor).visitPipe(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof EqlBaseVisitor ) return ((EqlBaseVisitor)visitor).visitSubquery(this); - else return visitor.visitChildren(this); - } - } - - public final SubqueryContext subquery() throws RecognitionException { - SubqueryContext _localctx = new SubqueryContext(_ctx, getState()); - enterRule(_localctx, 22, RULE_subquery); - try { - enterOuterAlt(_localctx, 1); - { - setState(154); - match(LB); - setState(155); - eventFilter(); - setState(156); - match(RB); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class EventQueryContext extends ParserRuleContext { - public EventFilterContext eventFilter() { - return getRuleContext(EventFilterContext.class,0); - } - public EventQueryContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_eventQuery; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).enterEventQuery(this); + + public final PipeContext pipe() throws RecognitionException { + PipeContext _localctx = new PipeContext(_ctx, getState()); + enterRule(_localctx, 14, RULE_pipe); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(119); + match(PIPE); + setState(120); + ((PipeContext) _localctx).kind = match(IDENTIFIER); + setState(129); + _errHandler.sync(this); + _la = _input.LA(1); + if ((((_la) & ~0x3f) == 0 + && ((1L << _la) & ((1L << FALSE) | (1L << NOT) | (1L << NULL) | (1L << TRUE) | (1L << PLUS) | (1L << MINUS) | (1L << LP) + | (1L << OPTIONAL) | (1L << STRING) | (1L << INTEGER_VALUE) | (1L << DECIMAL_VALUE) | (1L << IDENTIFIER) | (1L + << QUOTED_IDENTIFIER) | (1L << TILDE_IDENTIFIER))) != 0)) { + { + setState(121); + booleanExpression(0); + setState(126); + _errHandler.sync(this); + _la = _input.LA(1); + while (_la == COMMA) { + { + { + setState(122); + match(COMMA); + setState(123); + booleanExpression(0); + } + } + setState(128); + _errHandler.sync(this); + _la = _input.LA(1); + } + } + } + + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).exitEventQuery(this); + + public static class JoinKeysContext extends ParserRuleContext { + public TerminalNode BY() { + return getToken(EqlBaseParser.BY, 0); + } + + public List expression() { + return getRuleContexts(ExpressionContext.class); + } + + public ExpressionContext expression(int i) { + return getRuleContext(ExpressionContext.class, i); + } + + public List COMMA() { + return getTokens(EqlBaseParser.COMMA); + } + + public TerminalNode COMMA(int i) { + return getToken(EqlBaseParser.COMMA, i); + } + + public JoinKeysContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_joinKeys; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).enterJoinKeys(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).exitJoinKeys(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof EqlBaseVisitor) return ((EqlBaseVisitor) visitor).visitJoinKeys(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof EqlBaseVisitor ) return ((EqlBaseVisitor)visitor).visitEventQuery(this); - else return visitor.visitChildren(this); - } - } - - public final EventQueryContext eventQuery() throws RecognitionException { - EventQueryContext _localctx = new EventQueryContext(_ctx, getState()); - enterRule(_localctx, 24, RULE_eventQuery); - try { - enterOuterAlt(_localctx, 1); - { - setState(158); - eventFilter(); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class EventFilterContext extends ParserRuleContext { - public EventValueContext event; - public TerminalNode WHERE() { return getToken(EqlBaseParser.WHERE, 0); } - public ExpressionContext expression() { - return getRuleContext(ExpressionContext.class,0); - } - public TerminalNode ANY() { return getToken(EqlBaseParser.ANY, 0); } - public EventValueContext eventValue() { - return getRuleContext(EventValueContext.class,0); - } - public EventFilterContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_eventFilter; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).enterEventFilter(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).exitEventFilter(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof EqlBaseVisitor ) return ((EqlBaseVisitor)visitor).visitEventFilter(this); - else return visitor.visitChildren(this); - } - } - - public final EventFilterContext eventFilter() throws RecognitionException { - EventFilterContext _localctx = new EventFilterContext(_ctx, getState()); - enterRule(_localctx, 26, RULE_eventFilter); - try { - enterOuterAlt(_localctx, 1); - { - setState(162); - _errHandler.sync(this); - switch (_input.LA(1)) { - case ANY: - { - setState(160); - match(ANY); - } - break; - case STRING: - case IDENTIFIER: - { - setState(161); - ((EventFilterContext)_localctx).event = eventValue(); - } - break; - default: - throw new NoViableAltException(this); - } - setState(164); - match(WHERE); - setState(165); - expression(); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class ExpressionContext extends ParserRuleContext { - public BooleanExpressionContext booleanExpression() { - return getRuleContext(BooleanExpressionContext.class,0); - } - public ExpressionContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_expression; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).enterExpression(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).exitExpression(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof EqlBaseVisitor ) return ((EqlBaseVisitor)visitor).visitExpression(this); - else return visitor.visitChildren(this); - } - } - - public final ExpressionContext expression() throws RecognitionException { - ExpressionContext _localctx = new ExpressionContext(_ctx, getState()); - enterRule(_localctx, 28, RULE_expression); - try { - enterOuterAlt(_localctx, 1); - { - setState(167); - booleanExpression(0); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class BooleanExpressionContext extends ParserRuleContext { - public BooleanExpressionContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_booleanExpression; } - - public BooleanExpressionContext() { } - public void copyFrom(BooleanExpressionContext ctx) { - super.copyFrom(ctx); - } - } - public static class LogicalNotContext extends BooleanExpressionContext { - public TerminalNode NOT() { return getToken(EqlBaseParser.NOT, 0); } - public BooleanExpressionContext booleanExpression() { - return getRuleContext(BooleanExpressionContext.class,0); - } - public LogicalNotContext(BooleanExpressionContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).enterLogicalNot(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).exitLogicalNot(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof EqlBaseVisitor ) return ((EqlBaseVisitor)visitor).visitLogicalNot(this); - else return visitor.visitChildren(this); - } - } - public static class BooleanDefaultContext extends BooleanExpressionContext { - public ValueExpressionContext valueExpression() { - return getRuleContext(ValueExpressionContext.class,0); - } - public BooleanDefaultContext(BooleanExpressionContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).enterBooleanDefault(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).exitBooleanDefault(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof EqlBaseVisitor ) return ((EqlBaseVisitor)visitor).visitBooleanDefault(this); - else return visitor.visitChildren(this); - } - } - public static class ProcessCheckContext extends BooleanExpressionContext { - public Token relationship; - public TerminalNode OF() { return getToken(EqlBaseParser.OF, 0); } - public SubqueryContext subquery() { - return getRuleContext(SubqueryContext.class,0); - } - public TerminalNode IDENTIFIER() { return getToken(EqlBaseParser.IDENTIFIER, 0); } - public ProcessCheckContext(BooleanExpressionContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).enterProcessCheck(this); + + public final JoinKeysContext joinKeys() throws RecognitionException { + JoinKeysContext _localctx = new JoinKeysContext(_ctx, getState()); + enterRule(_localctx, 16, RULE_joinKeys); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(131); + match(BY); + setState(132); + expression(); + setState(137); + _errHandler.sync(this); + _la = _input.LA(1); + while (_la == COMMA) { + { + { + setState(133); + match(COMMA); + setState(134); + expression(); + } + } + setState(139); + _errHandler.sync(this); + _la = _input.LA(1); + } + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).exitProcessCheck(this); + + public static class JoinTermContext extends ParserRuleContext { + public JoinKeysContext by; + + public SubqueryContext subquery() { + return getRuleContext(SubqueryContext.class, 0); + } + + public JoinKeysContext joinKeys() { + return getRuleContext(JoinKeysContext.class, 0); + } + + public JoinTermContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_joinTerm; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).enterJoinTerm(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).exitJoinTerm(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof EqlBaseVisitor) return ((EqlBaseVisitor) visitor).visitJoinTerm(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof EqlBaseVisitor ) return ((EqlBaseVisitor)visitor).visitProcessCheck(this); - else return visitor.visitChildren(this); - } - } - public static class LogicalBinaryContext extends BooleanExpressionContext { - public BooleanExpressionContext left; - public Token operator; - public BooleanExpressionContext right; - public List booleanExpression() { - return getRuleContexts(BooleanExpressionContext.class); - } - public BooleanExpressionContext booleanExpression(int i) { - return getRuleContext(BooleanExpressionContext.class,i); - } - public TerminalNode AND() { return getToken(EqlBaseParser.AND, 0); } - public TerminalNode OR() { return getToken(EqlBaseParser.OR, 0); } - public LogicalBinaryContext(BooleanExpressionContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).enterLogicalBinary(this); + + public final JoinTermContext joinTerm() throws RecognitionException { + JoinTermContext _localctx = new JoinTermContext(_ctx, getState()); + enterRule(_localctx, 18, RULE_joinTerm); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(140); + subquery(); + setState(142); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == BY) { + { + setState(141); + ((JoinTermContext) _localctx).by = joinKeys(); + } + } + + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).exitLogicalBinary(this); + + public static class SequenceTermContext extends ParserRuleContext { + public JoinKeysContext by; + public Token key; + public NumberContext value; + + public SubqueryContext subquery() { + return getRuleContext(SubqueryContext.class, 0); + } + + public TerminalNode WITH() { + return getToken(EqlBaseParser.WITH, 0); + } + + public TerminalNode ASGN() { + return getToken(EqlBaseParser.ASGN, 0); + } + + public JoinKeysContext joinKeys() { + return getRuleContext(JoinKeysContext.class, 0); + } + + public TerminalNode IDENTIFIER() { + return getToken(EqlBaseParser.IDENTIFIER, 0); + } + + public NumberContext number() { + return getRuleContext(NumberContext.class, 0); + } + + public SequenceTermContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_sequenceTerm; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).enterSequenceTerm(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).exitSequenceTerm(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof EqlBaseVisitor) return ((EqlBaseVisitor) visitor).visitSequenceTerm(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof EqlBaseVisitor ) return ((EqlBaseVisitor)visitor).visitLogicalBinary(this); - else return visitor.visitChildren(this); - } - } - - public final BooleanExpressionContext booleanExpression() throws RecognitionException { - return booleanExpression(0); - } - - private BooleanExpressionContext booleanExpression(int _p) throws RecognitionException { - ParserRuleContext _parentctx = _ctx; - int _parentState = getState(); - BooleanExpressionContext _localctx = new BooleanExpressionContext(_ctx, _parentState); - BooleanExpressionContext _prevctx = _localctx; - int _startState = 30; - enterRecursionRule(_localctx, 30, RULE_booleanExpression, _p); - try { - int _alt; - enterOuterAlt(_localctx, 1); - { - setState(176); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,17,_ctx) ) { - case 1: - { - _localctx = new LogicalNotContext(_localctx); - _ctx = _localctx; - _prevctx = _localctx; - - setState(170); - match(NOT); - setState(171); - booleanExpression(5); - } - break; - case 2: - { - _localctx = new ProcessCheckContext(_localctx); - _ctx = _localctx; - _prevctx = _localctx; - setState(172); - ((ProcessCheckContext)_localctx).relationship = match(IDENTIFIER); - setState(173); - match(OF); - setState(174); - subquery(); - } - break; - case 3: - { - _localctx = new BooleanDefaultContext(_localctx); - _ctx = _localctx; - _prevctx = _localctx; - setState(175); - valueExpression(); - } - break; - } - _ctx.stop = _input.LT(-1); - setState(186); - _errHandler.sync(this); - _alt = getInterpreter().adaptivePredict(_input,19,_ctx); - while ( _alt!=2 && _alt!=org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER ) { - if ( _alt==1 ) { - if ( _parseListeners!=null ) triggerExitRuleEvent(); - _prevctx = _localctx; - { - setState(184); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,18,_ctx) ) { - case 1: + + public final SequenceTermContext sequenceTerm() throws RecognitionException { + SequenceTermContext _localctx = new SequenceTermContext(_ctx, getState()); + enterRule(_localctx, 20, RULE_sequenceTerm); + int _la; + try { + enterOuterAlt(_localctx, 1); { - _localctx = new LogicalBinaryContext(new BooleanExpressionContext(_parentctx, _parentState)); - ((LogicalBinaryContext)_localctx).left = _prevctx; - pushNewRecursionContext(_localctx, _startState, RULE_booleanExpression); - setState(178); - if (!(precpred(_ctx, 2))) throw new FailedPredicateException(this, "precpred(_ctx, 2)"); - setState(179); - ((LogicalBinaryContext)_localctx).operator = match(AND); - setState(180); - ((LogicalBinaryContext)_localctx).right = booleanExpression(3); + setState(144); + subquery(); + setState(146); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == BY) { + { + setState(145); + ((SequenceTermContext) _localctx).by = joinKeys(); + } + } + + setState(152); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == WITH) { + { + setState(148); + match(WITH); + setState(149); + ((SequenceTermContext) _localctx).key = match(IDENTIFIER); + setState(150); + match(ASGN); + setState(151); + ((SequenceTermContext) _localctx).value = number(); + } + } + } - break; - case 2: + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; + } + + public static class SubqueryContext extends ParserRuleContext { + public TerminalNode LB() { + return getToken(EqlBaseParser.LB, 0); + } + + public EventFilterContext eventFilter() { + return getRuleContext(EventFilterContext.class, 0); + } + + public TerminalNode RB() { + return getToken(EqlBaseParser.RB, 0); + } + + public SubqueryContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_subquery; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).enterSubquery(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).exitSubquery(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof EqlBaseVisitor) return ((EqlBaseVisitor) visitor).visitSubquery(this); + else return visitor.visitChildren(this); + } + } + + public final SubqueryContext subquery() throws RecognitionException { + SubqueryContext _localctx = new SubqueryContext(_ctx, getState()); + enterRule(_localctx, 22, RULE_subquery); + try { + enterOuterAlt(_localctx, 1); { - _localctx = new LogicalBinaryContext(new BooleanExpressionContext(_parentctx, _parentState)); - ((LogicalBinaryContext)_localctx).left = _prevctx; - pushNewRecursionContext(_localctx, _startState, RULE_booleanExpression); - setState(181); - if (!(precpred(_ctx, 1))) throw new FailedPredicateException(this, "precpred(_ctx, 1)"); - setState(182); - ((LogicalBinaryContext)_localctx).operator = match(OR); - setState(183); - ((LogicalBinaryContext)_localctx).right = booleanExpression(2); + setState(154); + match(LB); + setState(155); + eventFilter(); + setState(156); + match(RB); } - break; - } - } - } - setState(188); - _errHandler.sync(this); - _alt = getInterpreter().adaptivePredict(_input,19,_ctx); - } - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - unrollRecursionContexts(_parentctx); - } - return _localctx; - } - - public static class ValueExpressionContext extends ParserRuleContext { - public ValueExpressionContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_valueExpression; } - - public ValueExpressionContext() { } - public void copyFrom(ValueExpressionContext ctx) { - super.copyFrom(ctx); - } - } - public static class ValueExpressionDefaultContext extends ValueExpressionContext { - public OperatorExpressionContext operatorExpression() { - return getRuleContext(OperatorExpressionContext.class,0); - } - public ValueExpressionDefaultContext(ValueExpressionContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).enterValueExpressionDefault(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).exitValueExpressionDefault(this); + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof EqlBaseVisitor ) return ((EqlBaseVisitor)visitor).visitValueExpressionDefault(this); - else return visitor.visitChildren(this); + + public static class EventQueryContext extends ParserRuleContext { + public EventFilterContext eventFilter() { + return getRuleContext(EventFilterContext.class, 0); + } + + public EventQueryContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_eventQuery; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).enterEventQuery(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).exitEventQuery(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof EqlBaseVisitor) return ((EqlBaseVisitor) visitor).visitEventQuery(this); + else return visitor.visitChildren(this); + } } - } - public static class ComparisonContext extends ValueExpressionContext { - public OperatorExpressionContext left; - public OperatorExpressionContext right; - public ComparisonOperatorContext comparisonOperator() { - return getRuleContext(ComparisonOperatorContext.class,0); + + public final EventQueryContext eventQuery() throws RecognitionException { + EventQueryContext _localctx = new EventQueryContext(_ctx, getState()); + enterRule(_localctx, 24, RULE_eventQuery); + try { + enterOuterAlt(_localctx, 1); + { + setState(158); + eventFilter(); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - public List operatorExpression() { - return getRuleContexts(OperatorExpressionContext.class); + + public static class EventFilterContext extends ParserRuleContext { + public EventValueContext event; + + public TerminalNode WHERE() { + return getToken(EqlBaseParser.WHERE, 0); + } + + public ExpressionContext expression() { + return getRuleContext(ExpressionContext.class, 0); + } + + public TerminalNode ANY() { + return getToken(EqlBaseParser.ANY, 0); + } + + public EventValueContext eventValue() { + return getRuleContext(EventValueContext.class, 0); + } + + public EventFilterContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_eventFilter; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).enterEventFilter(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).exitEventFilter(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof EqlBaseVisitor) return ((EqlBaseVisitor) visitor).visitEventFilter(this); + else return visitor.visitChildren(this); + } } - public OperatorExpressionContext operatorExpression(int i) { - return getRuleContext(OperatorExpressionContext.class,i); + + public final EventFilterContext eventFilter() throws RecognitionException { + EventFilterContext _localctx = new EventFilterContext(_ctx, getState()); + enterRule(_localctx, 26, RULE_eventFilter); + try { + enterOuterAlt(_localctx, 1); + { + setState(162); + _errHandler.sync(this); + switch (_input.LA(1)) { + case ANY: { + setState(160); + match(ANY); + } + break; + case STRING: + case IDENTIFIER: { + setState(161); + ((EventFilterContext) _localctx).event = eventValue(); + } + break; + default: + throw new NoViableAltException(this); + } + setState(164); + match(WHERE); + setState(165); + expression(); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - public ComparisonContext(ValueExpressionContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).enterComparison(this); + + public static class ExpressionContext extends ParserRuleContext { + public BooleanExpressionContext booleanExpression() { + return getRuleContext(BooleanExpressionContext.class, 0); + } + + public ExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_expression; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).enterExpression(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).exitExpression(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof EqlBaseVisitor) return ((EqlBaseVisitor) visitor).visitExpression(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).exitComparison(this); + + public final ExpressionContext expression() throws RecognitionException { + ExpressionContext _localctx = new ExpressionContext(_ctx, getState()); + enterRule(_localctx, 28, RULE_expression); + try { + enterOuterAlt(_localctx, 1); + { + setState(167); + booleanExpression(0); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof EqlBaseVisitor ) return ((EqlBaseVisitor)visitor).visitComparison(this); - else return visitor.visitChildren(this); - } - } - - public final ValueExpressionContext valueExpression() throws RecognitionException { - ValueExpressionContext _localctx = new ValueExpressionContext(_ctx, getState()); - enterRule(_localctx, 32, RULE_valueExpression); - try { - setState(194); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,20,_ctx) ) { - case 1: - _localctx = new ValueExpressionDefaultContext(_localctx); - enterOuterAlt(_localctx, 1); - { - setState(189); - operatorExpression(0); - } - break; - case 2: - _localctx = new ComparisonContext(_localctx); - enterOuterAlt(_localctx, 2); - { - setState(190); - ((ComparisonContext)_localctx).left = operatorExpression(0); - setState(191); - comparisonOperator(); - setState(192); - ((ComparisonContext)_localctx).right = operatorExpression(0); - } - break; - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class OperatorExpressionContext extends ParserRuleContext { - public OperatorExpressionContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_operatorExpression; } - - public OperatorExpressionContext() { } - public void copyFrom(OperatorExpressionContext ctx) { - super.copyFrom(ctx); - } - } - public static class OperatorExpressionDefaultContext extends OperatorExpressionContext { - public PrimaryExpressionContext primaryExpression() { - return getRuleContext(PrimaryExpressionContext.class,0); - } - public PredicateContext predicate() { - return getRuleContext(PredicateContext.class,0); - } - public OperatorExpressionDefaultContext(OperatorExpressionContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).enterOperatorExpressionDefault(this); + + public static class BooleanExpressionContext extends ParserRuleContext { + public BooleanExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_booleanExpression; + } + + public BooleanExpressionContext() {} + + public void copyFrom(BooleanExpressionContext ctx) { + super.copyFrom(ctx); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).exitOperatorExpressionDefault(this); + + public static class LogicalNotContext extends BooleanExpressionContext { + public TerminalNode NOT() { + return getToken(EqlBaseParser.NOT, 0); + } + + public BooleanExpressionContext booleanExpression() { + return getRuleContext(BooleanExpressionContext.class, 0); + } + + public LogicalNotContext(BooleanExpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).enterLogicalNot(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).exitLogicalNot(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof EqlBaseVisitor) return ((EqlBaseVisitor) visitor).visitLogicalNot(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof EqlBaseVisitor ) return ((EqlBaseVisitor)visitor).visitOperatorExpressionDefault(this); - else return visitor.visitChildren(this); - } - } - public static class ArithmeticBinaryContext extends OperatorExpressionContext { - public OperatorExpressionContext left; - public Token operator; - public OperatorExpressionContext right; - public List operatorExpression() { - return getRuleContexts(OperatorExpressionContext.class); - } - public OperatorExpressionContext operatorExpression(int i) { - return getRuleContext(OperatorExpressionContext.class,i); - } - public TerminalNode ASTERISK() { return getToken(EqlBaseParser.ASTERISK, 0); } - public TerminalNode SLASH() { return getToken(EqlBaseParser.SLASH, 0); } - public TerminalNode PERCENT() { return getToken(EqlBaseParser.PERCENT, 0); } - public TerminalNode PLUS() { return getToken(EqlBaseParser.PLUS, 0); } - public TerminalNode MINUS() { return getToken(EqlBaseParser.MINUS, 0); } - public ArithmeticBinaryContext(OperatorExpressionContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).enterArithmeticBinary(this); + + public static class BooleanDefaultContext extends BooleanExpressionContext { + public ValueExpressionContext valueExpression() { + return getRuleContext(ValueExpressionContext.class, 0); + } + + public BooleanDefaultContext(BooleanExpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).enterBooleanDefault(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).exitBooleanDefault(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof EqlBaseVisitor) return ((EqlBaseVisitor) visitor).visitBooleanDefault(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).exitArithmeticBinary(this); + + public static class ProcessCheckContext extends BooleanExpressionContext { + public Token relationship; + + public TerminalNode OF() { + return getToken(EqlBaseParser.OF, 0); + } + + public SubqueryContext subquery() { + return getRuleContext(SubqueryContext.class, 0); + } + + public TerminalNode IDENTIFIER() { + return getToken(EqlBaseParser.IDENTIFIER, 0); + } + + public ProcessCheckContext(BooleanExpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).enterProcessCheck(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).exitProcessCheck(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof EqlBaseVisitor) return ((EqlBaseVisitor) visitor).visitProcessCheck(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof EqlBaseVisitor ) return ((EqlBaseVisitor)visitor).visitArithmeticBinary(this); - else return visitor.visitChildren(this); - } - } - public static class ArithmeticUnaryContext extends OperatorExpressionContext { - public Token operator; - public OperatorExpressionContext operatorExpression() { - return getRuleContext(OperatorExpressionContext.class,0); - } - public TerminalNode MINUS() { return getToken(EqlBaseParser.MINUS, 0); } - public TerminalNode PLUS() { return getToken(EqlBaseParser.PLUS, 0); } - public ArithmeticUnaryContext(OperatorExpressionContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).enterArithmeticUnary(this); + + public static class LogicalBinaryContext extends BooleanExpressionContext { + public BooleanExpressionContext left; + public Token operator; + public BooleanExpressionContext right; + + public List booleanExpression() { + return getRuleContexts(BooleanExpressionContext.class); + } + + public BooleanExpressionContext booleanExpression(int i) { + return getRuleContext(BooleanExpressionContext.class, i); + } + + public TerminalNode AND() { + return getToken(EqlBaseParser.AND, 0); + } + + public TerminalNode OR() { + return getToken(EqlBaseParser.OR, 0); + } + + public LogicalBinaryContext(BooleanExpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).enterLogicalBinary(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).exitLogicalBinary(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof EqlBaseVisitor) return ((EqlBaseVisitor) visitor).visitLogicalBinary(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).exitArithmeticUnary(this); + + public final BooleanExpressionContext booleanExpression() throws RecognitionException { + return booleanExpression(0); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof EqlBaseVisitor ) return ((EqlBaseVisitor)visitor).visitArithmeticUnary(this); - else return visitor.visitChildren(this); - } - } - - public final OperatorExpressionContext operatorExpression() throws RecognitionException { - return operatorExpression(0); - } - - private OperatorExpressionContext operatorExpression(int _p) throws RecognitionException { - ParserRuleContext _parentctx = _ctx; - int _parentState = getState(); - OperatorExpressionContext _localctx = new OperatorExpressionContext(_ctx, _parentState); - OperatorExpressionContext _prevctx = _localctx; - int _startState = 34; - enterRecursionRule(_localctx, 34, RULE_operatorExpression, _p); - int _la; - try { - int _alt; - enterOuterAlt(_localctx, 1); - { - setState(203); - _errHandler.sync(this); - switch (_input.LA(1)) { - case FALSE: - case NULL: - case TRUE: - case LP: - case OPTIONAL: - case STRING: - case INTEGER_VALUE: - case DECIMAL_VALUE: - case IDENTIFIER: - case QUOTED_IDENTIFIER: - case TILDE_IDENTIFIER: - { - _localctx = new OperatorExpressionDefaultContext(_localctx); - _ctx = _localctx; - _prevctx = _localctx; - - setState(197); - primaryExpression(); - setState(199); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,21,_ctx) ) { - case 1: - { - setState(198); - predicate(); - } - break; - } - } - break; - case PLUS: - case MINUS: - { - _localctx = new ArithmeticUnaryContext(_localctx); - _ctx = _localctx; - _prevctx = _localctx; - setState(201); - ((ArithmeticUnaryContext)_localctx).operator = _input.LT(1); - _la = _input.LA(1); - if ( !(_la==PLUS || _la==MINUS) ) { - ((ArithmeticUnaryContext)_localctx).operator = (Token)_errHandler.recoverInline(this); - } - else { - if ( _input.LA(1)==Token.EOF ) matchedEOF = true; - _errHandler.reportMatch(this); - consume(); - } - setState(202); - operatorExpression(3); - } - break; - default: - throw new NoViableAltException(this); - } - _ctx.stop = _input.LT(-1); - setState(213); - _errHandler.sync(this); - _alt = getInterpreter().adaptivePredict(_input,24,_ctx); - while ( _alt!=2 && _alt!=org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER ) { - if ( _alt==1 ) { - if ( _parseListeners!=null ) triggerExitRuleEvent(); - _prevctx = _localctx; - { - setState(211); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,23,_ctx) ) { - case 1: - { - _localctx = new ArithmeticBinaryContext(new OperatorExpressionContext(_parentctx, _parentState)); - ((ArithmeticBinaryContext)_localctx).left = _prevctx; - pushNewRecursionContext(_localctx, _startState, RULE_operatorExpression); - setState(205); - if (!(precpred(_ctx, 2))) throw new FailedPredicateException(this, "precpred(_ctx, 2)"); - setState(206); - ((ArithmeticBinaryContext)_localctx).operator = _input.LT(1); - _la = _input.LA(1); - if ( !((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << ASTERISK) | (1L << SLASH) | (1L << PERCENT))) != 0)) ) { - ((ArithmeticBinaryContext)_localctx).operator = (Token)_errHandler.recoverInline(this); - } - else { - if ( _input.LA(1)==Token.EOF ) matchedEOF = true; - _errHandler.reportMatch(this); - consume(); - } - setState(207); - ((ArithmeticBinaryContext)_localctx).right = operatorExpression(3); - } - break; - case 2: + + private BooleanExpressionContext booleanExpression(int _p) throws RecognitionException { + ParserRuleContext _parentctx = _ctx; + int _parentState = getState(); + BooleanExpressionContext _localctx = new BooleanExpressionContext(_ctx, _parentState); + BooleanExpressionContext _prevctx = _localctx; + int _startState = 30; + enterRecursionRule(_localctx, 30, RULE_booleanExpression, _p); + try { + int _alt; + enterOuterAlt(_localctx, 1); { - _localctx = new ArithmeticBinaryContext(new OperatorExpressionContext(_parentctx, _parentState)); - ((ArithmeticBinaryContext)_localctx).left = _prevctx; - pushNewRecursionContext(_localctx, _startState, RULE_operatorExpression); - setState(208); - if (!(precpred(_ctx, 1))) throw new FailedPredicateException(this, "precpred(_ctx, 1)"); - setState(209); - ((ArithmeticBinaryContext)_localctx).operator = _input.LT(1); - _la = _input.LA(1); - if ( !(_la==PLUS || _la==MINUS) ) { - ((ArithmeticBinaryContext)_localctx).operator = (Token)_errHandler.recoverInline(this); + setState(176); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 17, _ctx)) { + case 1: { + _localctx = new LogicalNotContext(_localctx); + _ctx = _localctx; + _prevctx = _localctx; + + setState(170); + match(NOT); + setState(171); + booleanExpression(5); + } + break; + case 2: { + _localctx = new ProcessCheckContext(_localctx); + _ctx = _localctx; + _prevctx = _localctx; + setState(172); + ((ProcessCheckContext) _localctx).relationship = match(IDENTIFIER); + setState(173); + match(OF); + setState(174); + subquery(); + } + break; + case 3: { + _localctx = new BooleanDefaultContext(_localctx); + _ctx = _localctx; + _prevctx = _localctx; + setState(175); + valueExpression(); + } + break; + } + _ctx.stop = _input.LT(-1); + setState(186); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input, 19, _ctx); + while (_alt != 2 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER) { + if (_alt == 1) { + if (_parseListeners != null) triggerExitRuleEvent(); + _prevctx = _localctx; + { + setState(184); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 18, _ctx)) { + case 1: { + _localctx = new LogicalBinaryContext(new BooleanExpressionContext(_parentctx, _parentState)); + ((LogicalBinaryContext) _localctx).left = _prevctx; + pushNewRecursionContext(_localctx, _startState, RULE_booleanExpression); + setState(178); + if (!(precpred(_ctx, 2))) throw new FailedPredicateException(this, "precpred(_ctx, 2)"); + setState(179); + ((LogicalBinaryContext) _localctx).operator = match(AND); + setState(180); + ((LogicalBinaryContext) _localctx).right = booleanExpression(3); + } + break; + case 2: { + _localctx = new LogicalBinaryContext(new BooleanExpressionContext(_parentctx, _parentState)); + ((LogicalBinaryContext) _localctx).left = _prevctx; + pushNewRecursionContext(_localctx, _startState, RULE_booleanExpression); + setState(181); + if (!(precpred(_ctx, 1))) throw new FailedPredicateException(this, "precpred(_ctx, 1)"); + setState(182); + ((LogicalBinaryContext) _localctx).operator = match(OR); + setState(183); + ((LogicalBinaryContext) _localctx).right = booleanExpression(2); + } + break; + } + } + } + setState(188); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input, 19, _ctx); + } } - else { - if ( _input.LA(1)==Token.EOF ) matchedEOF = true; - _errHandler.reportMatch(this); - consume(); - } - setState(210); - ((ArithmeticBinaryContext)_localctx).right = operatorExpression(2); - } - break; - } - } - } - setState(215); - _errHandler.sync(this); - _alt = getInterpreter().adaptivePredict(_input,24,_ctx); - } - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - unrollRecursionContexts(_parentctx); - } - return _localctx; - } - - public static class PredicateContext extends ParserRuleContext { - public Token kind; - public TerminalNode LP() { return getToken(EqlBaseParser.LP, 0); } - public List expression() { - return getRuleContexts(ExpressionContext.class); - } - public ExpressionContext expression(int i) { - return getRuleContext(ExpressionContext.class,i); - } - public TerminalNode RP() { return getToken(EqlBaseParser.RP, 0); } - public TerminalNode IN() { return getToken(EqlBaseParser.IN, 0); } - public TerminalNode IN_INSENSITIVE() { return getToken(EqlBaseParser.IN_INSENSITIVE, 0); } - public TerminalNode NOT() { return getToken(EqlBaseParser.NOT, 0); } - public List COMMA() { return getTokens(EqlBaseParser.COMMA); } - public TerminalNode COMMA(int i) { - return getToken(EqlBaseParser.COMMA, i); - } - public List constant() { - return getRuleContexts(ConstantContext.class); - } - public ConstantContext constant(int i) { - return getRuleContext(ConstantContext.class,i); - } - public TerminalNode SEQ() { return getToken(EqlBaseParser.SEQ, 0); } - public TerminalNode LIKE() { return getToken(EqlBaseParser.LIKE, 0); } - public TerminalNode LIKE_INSENSITIVE() { return getToken(EqlBaseParser.LIKE_INSENSITIVE, 0); } - public TerminalNode REGEX() { return getToken(EqlBaseParser.REGEX, 0); } - public TerminalNode REGEX_INSENSITIVE() { return getToken(EqlBaseParser.REGEX_INSENSITIVE, 0); } - public PredicateContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_predicate; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).enterPredicate(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).exitPredicate(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof EqlBaseVisitor ) return ((EqlBaseVisitor)visitor).visitPredicate(this); - else return visitor.visitChildren(this); - } - } - - public final PredicateContext predicate() throws RecognitionException { - PredicateContext _localctx = new PredicateContext(_ctx, getState()); - enterRule(_localctx, 36, RULE_predicate); - int _la; - try { - setState(245); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,28,_ctx) ) { - case 1: - enterOuterAlt(_localctx, 1); - { - setState(217); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==NOT) { - { - setState(216); - match(NOT); - } - } - - setState(219); - ((PredicateContext)_localctx).kind = _input.LT(1); - _la = _input.LA(1); - if ( !(_la==IN || _la==IN_INSENSITIVE) ) { - ((PredicateContext)_localctx).kind = (Token)_errHandler.recoverInline(this); - } - else { - if ( _input.LA(1)==Token.EOF ) matchedEOF = true; - _errHandler.reportMatch(this); - consume(); - } - setState(220); - match(LP); - setState(221); - expression(); - setState(226); - _errHandler.sync(this); - _la = _input.LA(1); - while (_la==COMMA) { - { - { - setState(222); - match(COMMA); - setState(223); - expression(); - } - } - setState(228); - _errHandler.sync(this); - _la = _input.LA(1); - } - setState(229); - match(RP); - } - break; - case 2: - enterOuterAlt(_localctx, 2); - { - setState(231); - ((PredicateContext)_localctx).kind = _input.LT(1); - _la = _input.LA(1); - if ( !((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << LIKE) | (1L << LIKE_INSENSITIVE) | (1L << REGEX) | (1L << REGEX_INSENSITIVE) | (1L << SEQ))) != 0)) ) { - ((PredicateContext)_localctx).kind = (Token)_errHandler.recoverInline(this); - } - else { - if ( _input.LA(1)==Token.EOF ) matchedEOF = true; - _errHandler.reportMatch(this); - consume(); - } - setState(232); - constant(); - } - break; - case 3: - enterOuterAlt(_localctx, 3); - { - setState(233); - ((PredicateContext)_localctx).kind = _input.LT(1); - _la = _input.LA(1); - if ( !((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << LIKE) | (1L << LIKE_INSENSITIVE) | (1L << REGEX) | (1L << REGEX_INSENSITIVE) | (1L << SEQ))) != 0)) ) { - ((PredicateContext)_localctx).kind = (Token)_errHandler.recoverInline(this); - } - else { - if ( _input.LA(1)==Token.EOF ) matchedEOF = true; - _errHandler.reportMatch(this); - consume(); - } - setState(234); - match(LP); - setState(235); - constant(); - setState(240); - _errHandler.sync(this); - _la = _input.LA(1); - while (_la==COMMA) { - { - { - setState(236); - match(COMMA); - setState(237); - constant(); - } - } - setState(242); - _errHandler.sync(this); - _la = _input.LA(1); - } - setState(243); - match(RP); - } - break; - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class PrimaryExpressionContext extends ParserRuleContext { - public PrimaryExpressionContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_primaryExpression; } - - public PrimaryExpressionContext() { } - public void copyFrom(PrimaryExpressionContext ctx) { - super.copyFrom(ctx); - } - } - public static class DereferenceContext extends PrimaryExpressionContext { - public QualifiedNameContext qualifiedName() { - return getRuleContext(QualifiedNameContext.class,0); - } - public DereferenceContext(PrimaryExpressionContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).enterDereference(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).exitDereference(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof EqlBaseVisitor ) return ((EqlBaseVisitor)visitor).visitDereference(this); - else return visitor.visitChildren(this); + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + unrollRecursionContexts(_parentctx); + } + return _localctx; } - } - public static class ConstantDefaultContext extends PrimaryExpressionContext { - public ConstantContext constant() { - return getRuleContext(ConstantContext.class,0); + + public static class ValueExpressionContext extends ParserRuleContext { + public ValueExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_valueExpression; + } + + public ValueExpressionContext() {} + + public void copyFrom(ValueExpressionContext ctx) { + super.copyFrom(ctx); + } } - public ConstantDefaultContext(PrimaryExpressionContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).enterConstantDefault(this); + + public static class ValueExpressionDefaultContext extends ValueExpressionContext { + public OperatorExpressionContext operatorExpression() { + return getRuleContext(OperatorExpressionContext.class, 0); + } + + public ValueExpressionDefaultContext(ValueExpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).enterValueExpressionDefault(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).exitValueExpressionDefault(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof EqlBaseVisitor) return ((EqlBaseVisitor) visitor).visitValueExpressionDefault(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).exitConstantDefault(this); + + public static class ComparisonContext extends ValueExpressionContext { + public OperatorExpressionContext left; + public OperatorExpressionContext right; + + public ComparisonOperatorContext comparisonOperator() { + return getRuleContext(ComparisonOperatorContext.class, 0); + } + + public List operatorExpression() { + return getRuleContexts(OperatorExpressionContext.class); + } + + public OperatorExpressionContext operatorExpression(int i) { + return getRuleContext(OperatorExpressionContext.class, i); + } + + public ComparisonContext(ValueExpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).enterComparison(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).exitComparison(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof EqlBaseVisitor) return ((EqlBaseVisitor) visitor).visitComparison(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof EqlBaseVisitor ) return ((EqlBaseVisitor)visitor).visitConstantDefault(this); - else return visitor.visitChildren(this); - } - } - public static class ParenthesizedExpressionContext extends PrimaryExpressionContext { - public TerminalNode LP() { return getToken(EqlBaseParser.LP, 0); } - public ExpressionContext expression() { - return getRuleContext(ExpressionContext.class,0); - } - public TerminalNode RP() { return getToken(EqlBaseParser.RP, 0); } - public ParenthesizedExpressionContext(PrimaryExpressionContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).enterParenthesizedExpression(this); + + public final ValueExpressionContext valueExpression() throws RecognitionException { + ValueExpressionContext _localctx = new ValueExpressionContext(_ctx, getState()); + enterRule(_localctx, 32, RULE_valueExpression); + try { + setState(194); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 20, _ctx)) { + case 1: + _localctx = new ValueExpressionDefaultContext(_localctx); + enterOuterAlt(_localctx, 1); { + setState(189); + operatorExpression(0); + } + break; + case 2: + _localctx = new ComparisonContext(_localctx); + enterOuterAlt(_localctx, 2); { + setState(190); + ((ComparisonContext) _localctx).left = operatorExpression(0); + setState(191); + comparisonOperator(); + setState(192); + ((ComparisonContext) _localctx).right = operatorExpression(0); + } + break; + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).exitParenthesizedExpression(this); + + public static class OperatorExpressionContext extends ParserRuleContext { + public OperatorExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_operatorExpression; + } + + public OperatorExpressionContext() {} + + public void copyFrom(OperatorExpressionContext ctx) { + super.copyFrom(ctx); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof EqlBaseVisitor ) return ((EqlBaseVisitor)visitor).visitParenthesizedExpression(this); - else return visitor.visitChildren(this); + + public static class OperatorExpressionDefaultContext extends OperatorExpressionContext { + public PrimaryExpressionContext primaryExpression() { + return getRuleContext(PrimaryExpressionContext.class, 0); + } + + public PredicateContext predicate() { + return getRuleContext(PredicateContext.class, 0); + } + + public OperatorExpressionDefaultContext(OperatorExpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).enterOperatorExpressionDefault(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).exitOperatorExpressionDefault(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof EqlBaseVisitor) return ((EqlBaseVisitor) visitor).visitOperatorExpressionDefault(this); + else return visitor.visitChildren(this); + } } - } - public static class FunctionContext extends PrimaryExpressionContext { - public FunctionExpressionContext functionExpression() { - return getRuleContext(FunctionExpressionContext.class,0); + + public static class ArithmeticBinaryContext extends OperatorExpressionContext { + public OperatorExpressionContext left; + public Token operator; + public OperatorExpressionContext right; + + public List operatorExpression() { + return getRuleContexts(OperatorExpressionContext.class); + } + + public OperatorExpressionContext operatorExpression(int i) { + return getRuleContext(OperatorExpressionContext.class, i); + } + + public TerminalNode ASTERISK() { + return getToken(EqlBaseParser.ASTERISK, 0); + } + + public TerminalNode SLASH() { + return getToken(EqlBaseParser.SLASH, 0); + } + + public TerminalNode PERCENT() { + return getToken(EqlBaseParser.PERCENT, 0); + } + + public TerminalNode PLUS() { + return getToken(EqlBaseParser.PLUS, 0); + } + + public TerminalNode MINUS() { + return getToken(EqlBaseParser.MINUS, 0); + } + + public ArithmeticBinaryContext(OperatorExpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).enterArithmeticBinary(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).exitArithmeticBinary(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof EqlBaseVisitor) return ((EqlBaseVisitor) visitor).visitArithmeticBinary(this); + else return visitor.visitChildren(this); + } } - public FunctionContext(PrimaryExpressionContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).enterFunction(this); + + public static class ArithmeticUnaryContext extends OperatorExpressionContext { + public Token operator; + + public OperatorExpressionContext operatorExpression() { + return getRuleContext(OperatorExpressionContext.class, 0); + } + + public TerminalNode MINUS() { + return getToken(EqlBaseParser.MINUS, 0); + } + + public TerminalNode PLUS() { + return getToken(EqlBaseParser.PLUS, 0); + } + + public ArithmeticUnaryContext(OperatorExpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).enterArithmeticUnary(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).exitArithmeticUnary(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof EqlBaseVisitor) return ((EqlBaseVisitor) visitor).visitArithmeticUnary(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).exitFunction(this); + + public final OperatorExpressionContext operatorExpression() throws RecognitionException { + return operatorExpression(0); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof EqlBaseVisitor ) return ((EqlBaseVisitor)visitor).visitFunction(this); - else return visitor.visitChildren(this); - } - } - - public final PrimaryExpressionContext primaryExpression() throws RecognitionException { - PrimaryExpressionContext _localctx = new PrimaryExpressionContext(_ctx, getState()); - enterRule(_localctx, 38, RULE_primaryExpression); - try { - setState(254); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,29,_ctx) ) { - case 1: - _localctx = new ConstantDefaultContext(_localctx); - enterOuterAlt(_localctx, 1); - { - setState(247); - constant(); - } - break; - case 2: - _localctx = new FunctionContext(_localctx); - enterOuterAlt(_localctx, 2); - { - setState(248); - functionExpression(); - } - break; - case 3: - _localctx = new DereferenceContext(_localctx); - enterOuterAlt(_localctx, 3); - { - setState(249); - qualifiedName(); - } - break; - case 4: - _localctx = new ParenthesizedExpressionContext(_localctx); - enterOuterAlt(_localctx, 4); - { - setState(250); - match(LP); - setState(251); - expression(); - setState(252); - match(RP); - } - break; - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class FunctionExpressionContext extends ParserRuleContext { - public FunctionNameContext name; - public TerminalNode LP() { return getToken(EqlBaseParser.LP, 0); } - public TerminalNode RP() { return getToken(EqlBaseParser.RP, 0); } - public FunctionNameContext functionName() { - return getRuleContext(FunctionNameContext.class,0); - } - public List expression() { - return getRuleContexts(ExpressionContext.class); - } - public ExpressionContext expression(int i) { - return getRuleContext(ExpressionContext.class,i); - } - public List COMMA() { return getTokens(EqlBaseParser.COMMA); } - public TerminalNode COMMA(int i) { - return getToken(EqlBaseParser.COMMA, i); - } - public FunctionExpressionContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_functionExpression; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).enterFunctionExpression(this); + + private OperatorExpressionContext operatorExpression(int _p) throws RecognitionException { + ParserRuleContext _parentctx = _ctx; + int _parentState = getState(); + OperatorExpressionContext _localctx = new OperatorExpressionContext(_ctx, _parentState); + OperatorExpressionContext _prevctx = _localctx; + int _startState = 34; + enterRecursionRule(_localctx, 34, RULE_operatorExpression, _p); + int _la; + try { + int _alt; + enterOuterAlt(_localctx, 1); + { + setState(203); + _errHandler.sync(this); + switch (_input.LA(1)) { + case FALSE: + case NULL: + case TRUE: + case LP: + case OPTIONAL: + case STRING: + case INTEGER_VALUE: + case DECIMAL_VALUE: + case IDENTIFIER: + case QUOTED_IDENTIFIER: + case TILDE_IDENTIFIER: { + _localctx = new OperatorExpressionDefaultContext(_localctx); + _ctx = _localctx; + _prevctx = _localctx; + + setState(197); + primaryExpression(); + setState(199); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 21, _ctx)) { + case 1: { + setState(198); + predicate(); + } + break; + } + } + break; + case PLUS: + case MINUS: { + _localctx = new ArithmeticUnaryContext(_localctx); + _ctx = _localctx; + _prevctx = _localctx; + setState(201); + ((ArithmeticUnaryContext) _localctx).operator = _input.LT(1); + _la = _input.LA(1); + if (!(_la == PLUS || _la == MINUS)) { + ((ArithmeticUnaryContext) _localctx).operator = (Token) _errHandler.recoverInline(this); + } else { + if (_input.LA(1) == Token.EOF) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + setState(202); + operatorExpression(3); + } + break; + default: + throw new NoViableAltException(this); + } + _ctx.stop = _input.LT(-1); + setState(213); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input, 24, _ctx); + while (_alt != 2 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER) { + if (_alt == 1) { + if (_parseListeners != null) triggerExitRuleEvent(); + _prevctx = _localctx; + { + setState(211); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 23, _ctx)) { + case 1: { + _localctx = new ArithmeticBinaryContext(new OperatorExpressionContext(_parentctx, _parentState)); + ((ArithmeticBinaryContext) _localctx).left = _prevctx; + pushNewRecursionContext(_localctx, _startState, RULE_operatorExpression); + setState(205); + if (!(precpred(_ctx, 2))) throw new FailedPredicateException(this, "precpred(_ctx, 2)"); + setState(206); + ((ArithmeticBinaryContext) _localctx).operator = _input.LT(1); + _la = _input.LA(1); + if (!((((_la) & ~0x3f) == 0 + && ((1L << _la) & ((1L << ASTERISK) | (1L << SLASH) | (1L << PERCENT))) != 0))) { + ((ArithmeticBinaryContext) _localctx).operator = (Token) _errHandler.recoverInline(this); + } else { + if (_input.LA(1) == Token.EOF) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + setState(207); + ((ArithmeticBinaryContext) _localctx).right = operatorExpression(3); + } + break; + case 2: { + _localctx = new ArithmeticBinaryContext(new OperatorExpressionContext(_parentctx, _parentState)); + ((ArithmeticBinaryContext) _localctx).left = _prevctx; + pushNewRecursionContext(_localctx, _startState, RULE_operatorExpression); + setState(208); + if (!(precpred(_ctx, 1))) throw new FailedPredicateException(this, "precpred(_ctx, 1)"); + setState(209); + ((ArithmeticBinaryContext) _localctx).operator = _input.LT(1); + _la = _input.LA(1); + if (!(_la == PLUS || _la == MINUS)) { + ((ArithmeticBinaryContext) _localctx).operator = (Token) _errHandler.recoverInline(this); + } else { + if (_input.LA(1) == Token.EOF) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + setState(210); + ((ArithmeticBinaryContext) _localctx).right = operatorExpression(2); + } + break; + } + } + } + setState(215); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input, 24, _ctx); + } + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + unrollRecursionContexts(_parentctx); + } + return _localctx; } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).exitFunctionExpression(this); + + public static class PredicateContext extends ParserRuleContext { + public Token kind; + + public TerminalNode LP() { + return getToken(EqlBaseParser.LP, 0); + } + + public List expression() { + return getRuleContexts(ExpressionContext.class); + } + + public ExpressionContext expression(int i) { + return getRuleContext(ExpressionContext.class, i); + } + + public TerminalNode RP() { + return getToken(EqlBaseParser.RP, 0); + } + + public TerminalNode IN() { + return getToken(EqlBaseParser.IN, 0); + } + + public TerminalNode IN_INSENSITIVE() { + return getToken(EqlBaseParser.IN_INSENSITIVE, 0); + } + + public TerminalNode NOT() { + return getToken(EqlBaseParser.NOT, 0); + } + + public List COMMA() { + return getTokens(EqlBaseParser.COMMA); + } + + public TerminalNode COMMA(int i) { + return getToken(EqlBaseParser.COMMA, i); + } + + public List constant() { + return getRuleContexts(ConstantContext.class); + } + + public ConstantContext constant(int i) { + return getRuleContext(ConstantContext.class, i); + } + + public TerminalNode SEQ() { + return getToken(EqlBaseParser.SEQ, 0); + } + + public TerminalNode LIKE() { + return getToken(EqlBaseParser.LIKE, 0); + } + + public TerminalNode LIKE_INSENSITIVE() { + return getToken(EqlBaseParser.LIKE_INSENSITIVE, 0); + } + + public TerminalNode REGEX() { + return getToken(EqlBaseParser.REGEX, 0); + } + + public TerminalNode REGEX_INSENSITIVE() { + return getToken(EqlBaseParser.REGEX_INSENSITIVE, 0); + } + + public PredicateContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_predicate; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).enterPredicate(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).exitPredicate(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof EqlBaseVisitor) return ((EqlBaseVisitor) visitor).visitPredicate(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof EqlBaseVisitor ) return ((EqlBaseVisitor)visitor).visitFunctionExpression(this); - else return visitor.visitChildren(this); - } - } - - public final FunctionExpressionContext functionExpression() throws RecognitionException { - FunctionExpressionContext _localctx = new FunctionExpressionContext(_ctx, getState()); - enterRule(_localctx, 40, RULE_functionExpression); - int _la; - try { - enterOuterAlt(_localctx, 1); - { - setState(256); - ((FunctionExpressionContext)_localctx).name = functionName(); - setState(257); - match(LP); - setState(266); - _errHandler.sync(this); - _la = _input.LA(1); - if ((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << FALSE) | (1L << NOT) | (1L << NULL) | (1L << TRUE) | (1L << PLUS) | (1L << MINUS) | (1L << LP) | (1L << OPTIONAL) | (1L << STRING) | (1L << INTEGER_VALUE) | (1L << DECIMAL_VALUE) | (1L << IDENTIFIER) | (1L << QUOTED_IDENTIFIER) | (1L << TILDE_IDENTIFIER))) != 0)) { - { - setState(258); - expression(); - setState(263); - _errHandler.sync(this); - _la = _input.LA(1); - while (_la==COMMA) { - { - { - setState(259); - match(COMMA); - setState(260); - expression(); - } - } - setState(265); - _errHandler.sync(this); - _la = _input.LA(1); - } - } - } - - setState(268); - match(RP); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class FunctionNameContext extends ParserRuleContext { - public TerminalNode IDENTIFIER() { return getToken(EqlBaseParser.IDENTIFIER, 0); } - public TerminalNode TILDE_IDENTIFIER() { return getToken(EqlBaseParser.TILDE_IDENTIFIER, 0); } - public FunctionNameContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_functionName; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).enterFunctionName(this); + + public final PredicateContext predicate() throws RecognitionException { + PredicateContext _localctx = new PredicateContext(_ctx, getState()); + enterRule(_localctx, 36, RULE_predicate); + int _la; + try { + setState(245); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 28, _ctx)) { + case 1: + enterOuterAlt(_localctx, 1); { + setState(217); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == NOT) { + { + setState(216); + match(NOT); + } + } + + setState(219); + ((PredicateContext) _localctx).kind = _input.LT(1); + _la = _input.LA(1); + if (!(_la == IN || _la == IN_INSENSITIVE)) { + ((PredicateContext) _localctx).kind = (Token) _errHandler.recoverInline(this); + } else { + if (_input.LA(1) == Token.EOF) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + setState(220); + match(LP); + setState(221); + expression(); + setState(226); + _errHandler.sync(this); + _la = _input.LA(1); + while (_la == COMMA) { + { + { + setState(222); + match(COMMA); + setState(223); + expression(); + } + } + setState(228); + _errHandler.sync(this); + _la = _input.LA(1); + } + setState(229); + match(RP); + } + break; + case 2: + enterOuterAlt(_localctx, 2); { + setState(231); + ((PredicateContext) _localctx).kind = _input.LT(1); + _la = _input.LA(1); + if (!((((_la) & ~0x3f) == 0 + && ((1L << _la) & ((1L << LIKE) | (1L << LIKE_INSENSITIVE) | (1L << REGEX) | (1L << REGEX_INSENSITIVE) | (1L + << SEQ))) != 0))) { + ((PredicateContext) _localctx).kind = (Token) _errHandler.recoverInline(this); + } else { + if (_input.LA(1) == Token.EOF) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + setState(232); + constant(); + } + break; + case 3: + enterOuterAlt(_localctx, 3); { + setState(233); + ((PredicateContext) _localctx).kind = _input.LT(1); + _la = _input.LA(1); + if (!((((_la) & ~0x3f) == 0 + && ((1L << _la) & ((1L << LIKE) | (1L << LIKE_INSENSITIVE) | (1L << REGEX) | (1L << REGEX_INSENSITIVE) | (1L + << SEQ))) != 0))) { + ((PredicateContext) _localctx).kind = (Token) _errHandler.recoverInline(this); + } else { + if (_input.LA(1) == Token.EOF) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + setState(234); + match(LP); + setState(235); + constant(); + setState(240); + _errHandler.sync(this); + _la = _input.LA(1); + while (_la == COMMA) { + { + { + setState(236); + match(COMMA); + setState(237); + constant(); + } + } + setState(242); + _errHandler.sync(this); + _la = _input.LA(1); + } + setState(243); + match(RP); + } + break; + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).exitFunctionName(this); + + public static class PrimaryExpressionContext extends ParserRuleContext { + public PrimaryExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_primaryExpression; + } + + public PrimaryExpressionContext() {} + + public void copyFrom(PrimaryExpressionContext ctx) { + super.copyFrom(ctx); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof EqlBaseVisitor ) return ((EqlBaseVisitor)visitor).visitFunctionName(this); - else return visitor.visitChildren(this); - } - } - - public final FunctionNameContext functionName() throws RecognitionException { - FunctionNameContext _localctx = new FunctionNameContext(_ctx, getState()); - enterRule(_localctx, 42, RULE_functionName); - int _la; - try { - enterOuterAlt(_localctx, 1); - { - setState(270); - _la = _input.LA(1); - if ( !(_la==IDENTIFIER || _la==TILDE_IDENTIFIER) ) { - _errHandler.recoverInline(this); - } - else { - if ( _input.LA(1)==Token.EOF ) matchedEOF = true; - _errHandler.reportMatch(this); - consume(); - } - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class ConstantContext extends ParserRuleContext { - public ConstantContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_constant; } - - public ConstantContext() { } - public void copyFrom(ConstantContext ctx) { - super.copyFrom(ctx); - } - } - public static class NullLiteralContext extends ConstantContext { - public TerminalNode NULL() { return getToken(EqlBaseParser.NULL, 0); } - public NullLiteralContext(ConstantContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).enterNullLiteral(this); + + public static class DereferenceContext extends PrimaryExpressionContext { + public QualifiedNameContext qualifiedName() { + return getRuleContext(QualifiedNameContext.class, 0); + } + + public DereferenceContext(PrimaryExpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).enterDereference(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).exitDereference(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof EqlBaseVisitor) return ((EqlBaseVisitor) visitor).visitDereference(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).exitNullLiteral(this); + + public static class ConstantDefaultContext extends PrimaryExpressionContext { + public ConstantContext constant() { + return getRuleContext(ConstantContext.class, 0); + } + + public ConstantDefaultContext(PrimaryExpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).enterConstantDefault(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).exitConstantDefault(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof EqlBaseVisitor) return ((EqlBaseVisitor) visitor).visitConstantDefault(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof EqlBaseVisitor ) return ((EqlBaseVisitor)visitor).visitNullLiteral(this); - else return visitor.visitChildren(this); + + public static class ParenthesizedExpressionContext extends PrimaryExpressionContext { + public TerminalNode LP() { + return getToken(EqlBaseParser.LP, 0); + } + + public ExpressionContext expression() { + return getRuleContext(ExpressionContext.class, 0); + } + + public TerminalNode RP() { + return getToken(EqlBaseParser.RP, 0); + } + + public ParenthesizedExpressionContext(PrimaryExpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).enterParenthesizedExpression(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).exitParenthesizedExpression(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof EqlBaseVisitor) return ((EqlBaseVisitor) visitor).visitParenthesizedExpression(this); + else return visitor.visitChildren(this); + } } - } - public static class StringLiteralContext extends ConstantContext { - public StringContext string() { - return getRuleContext(StringContext.class,0); + + public static class FunctionContext extends PrimaryExpressionContext { + public FunctionExpressionContext functionExpression() { + return getRuleContext(FunctionExpressionContext.class, 0); + } + + public FunctionContext(PrimaryExpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).enterFunction(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).exitFunction(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof EqlBaseVisitor) return ((EqlBaseVisitor) visitor).visitFunction(this); + else return visitor.visitChildren(this); + } } - public StringLiteralContext(ConstantContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).enterStringLiteral(this); + + public final PrimaryExpressionContext primaryExpression() throws RecognitionException { + PrimaryExpressionContext _localctx = new PrimaryExpressionContext(_ctx, getState()); + enterRule(_localctx, 38, RULE_primaryExpression); + try { + setState(254); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 29, _ctx)) { + case 1: + _localctx = new ConstantDefaultContext(_localctx); + enterOuterAlt(_localctx, 1); { + setState(247); + constant(); + } + break; + case 2: + _localctx = new FunctionContext(_localctx); + enterOuterAlt(_localctx, 2); { + setState(248); + functionExpression(); + } + break; + case 3: + _localctx = new DereferenceContext(_localctx); + enterOuterAlt(_localctx, 3); { + setState(249); + qualifiedName(); + } + break; + case 4: + _localctx = new ParenthesizedExpressionContext(_localctx); + enterOuterAlt(_localctx, 4); { + setState(250); + match(LP); + setState(251); + expression(); + setState(252); + match(RP); + } + break; + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).exitStringLiteral(this); + + public static class FunctionExpressionContext extends ParserRuleContext { + public FunctionNameContext name; + + public TerminalNode LP() { + return getToken(EqlBaseParser.LP, 0); + } + + public TerminalNode RP() { + return getToken(EqlBaseParser.RP, 0); + } + + public FunctionNameContext functionName() { + return getRuleContext(FunctionNameContext.class, 0); + } + + public List expression() { + return getRuleContexts(ExpressionContext.class); + } + + public ExpressionContext expression(int i) { + return getRuleContext(ExpressionContext.class, i); + } + + public List COMMA() { + return getTokens(EqlBaseParser.COMMA); + } + + public TerminalNode COMMA(int i) { + return getToken(EqlBaseParser.COMMA, i); + } + + public FunctionExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_functionExpression; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).enterFunctionExpression(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).exitFunctionExpression(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof EqlBaseVisitor) return ((EqlBaseVisitor) visitor).visitFunctionExpression(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof EqlBaseVisitor ) return ((EqlBaseVisitor)visitor).visitStringLiteral(this); - else return visitor.visitChildren(this); + + public final FunctionExpressionContext functionExpression() throws RecognitionException { + FunctionExpressionContext _localctx = new FunctionExpressionContext(_ctx, getState()); + enterRule(_localctx, 40, RULE_functionExpression); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(256); + ((FunctionExpressionContext) _localctx).name = functionName(); + setState(257); + match(LP); + setState(266); + _errHandler.sync(this); + _la = _input.LA(1); + if ((((_la) & ~0x3f) == 0 + && ((1L << _la) & ((1L << FALSE) | (1L << NOT) | (1L << NULL) | (1L << TRUE) | (1L << PLUS) | (1L << MINUS) | (1L << LP) + | (1L << OPTIONAL) | (1L << STRING) | (1L << INTEGER_VALUE) | (1L << DECIMAL_VALUE) | (1L << IDENTIFIER) | (1L + << QUOTED_IDENTIFIER) | (1L << TILDE_IDENTIFIER))) != 0)) { + { + setState(258); + expression(); + setState(263); + _errHandler.sync(this); + _la = _input.LA(1); + while (_la == COMMA) { + { + { + setState(259); + match(COMMA); + setState(260); + expression(); + } + } + setState(265); + _errHandler.sync(this); + _la = _input.LA(1); + } + } + } + + setState(268); + match(RP); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - } - public static class NumericLiteralContext extends ConstantContext { - public NumberContext number() { - return getRuleContext(NumberContext.class,0); + + public static class FunctionNameContext extends ParserRuleContext { + public TerminalNode IDENTIFIER() { + return getToken(EqlBaseParser.IDENTIFIER, 0); + } + + public TerminalNode TILDE_IDENTIFIER() { + return getToken(EqlBaseParser.TILDE_IDENTIFIER, 0); + } + + public FunctionNameContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_functionName; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).enterFunctionName(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).exitFunctionName(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof EqlBaseVisitor) return ((EqlBaseVisitor) visitor).visitFunctionName(this); + else return visitor.visitChildren(this); + } } - public NumericLiteralContext(ConstantContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).enterNumericLiteral(this); + + public final FunctionNameContext functionName() throws RecognitionException { + FunctionNameContext _localctx = new FunctionNameContext(_ctx, getState()); + enterRule(_localctx, 42, RULE_functionName); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(270); + _la = _input.LA(1); + if (!(_la == IDENTIFIER || _la == TILDE_IDENTIFIER)) { + _errHandler.recoverInline(this); + } else { + if (_input.LA(1) == Token.EOF) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).exitNumericLiteral(this); + + public static class ConstantContext extends ParserRuleContext { + public ConstantContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_constant; + } + + public ConstantContext() {} + + public void copyFrom(ConstantContext ctx) { + super.copyFrom(ctx); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof EqlBaseVisitor ) return ((EqlBaseVisitor)visitor).visitNumericLiteral(this); - else return visitor.visitChildren(this); + + public static class NullLiteralContext extends ConstantContext { + public TerminalNode NULL() { + return getToken(EqlBaseParser.NULL, 0); + } + + public NullLiteralContext(ConstantContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).enterNullLiteral(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).exitNullLiteral(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof EqlBaseVisitor) return ((EqlBaseVisitor) visitor).visitNullLiteral(this); + else return visitor.visitChildren(this); + } } - } - public static class BooleanLiteralContext extends ConstantContext { - public BooleanValueContext booleanValue() { - return getRuleContext(BooleanValueContext.class,0); + + public static class StringLiteralContext extends ConstantContext { + public StringContext string() { + return getRuleContext(StringContext.class, 0); + } + + public StringLiteralContext(ConstantContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).enterStringLiteral(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).exitStringLiteral(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof EqlBaseVisitor) return ((EqlBaseVisitor) visitor).visitStringLiteral(this); + else return visitor.visitChildren(this); + } } - public BooleanLiteralContext(ConstantContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).enterBooleanLiteral(this); + + public static class NumericLiteralContext extends ConstantContext { + public NumberContext number() { + return getRuleContext(NumberContext.class, 0); + } + + public NumericLiteralContext(ConstantContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).enterNumericLiteral(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).exitNumericLiteral(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof EqlBaseVisitor) return ((EqlBaseVisitor) visitor).visitNumericLiteral(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).exitBooleanLiteral(this); + + public static class BooleanLiteralContext extends ConstantContext { + public BooleanValueContext booleanValue() { + return getRuleContext(BooleanValueContext.class, 0); + } + + public BooleanLiteralContext(ConstantContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).enterBooleanLiteral(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).exitBooleanLiteral(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof EqlBaseVisitor) return ((EqlBaseVisitor) visitor).visitBooleanLiteral(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof EqlBaseVisitor ) return ((EqlBaseVisitor)visitor).visitBooleanLiteral(this); - else return visitor.visitChildren(this); - } - } - - public final ConstantContext constant() throws RecognitionException { - ConstantContext _localctx = new ConstantContext(_ctx, getState()); - enterRule(_localctx, 44, RULE_constant); - try { - setState(276); - _errHandler.sync(this); - switch (_input.LA(1)) { - case NULL: - _localctx = new NullLiteralContext(_localctx); - enterOuterAlt(_localctx, 1); - { - setState(272); - match(NULL); - } - break; - case INTEGER_VALUE: - case DECIMAL_VALUE: - _localctx = new NumericLiteralContext(_localctx); - enterOuterAlt(_localctx, 2); - { - setState(273); - number(); - } - break; - case FALSE: - case TRUE: - _localctx = new BooleanLiteralContext(_localctx); - enterOuterAlt(_localctx, 3); - { - setState(274); - booleanValue(); - } - break; - case STRING: - _localctx = new StringLiteralContext(_localctx); - enterOuterAlt(_localctx, 4); - { - setState(275); - string(); - } - break; - default: - throw new NoViableAltException(this); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class ComparisonOperatorContext extends ParserRuleContext { - public TerminalNode EQ() { return getToken(EqlBaseParser.EQ, 0); } - public TerminalNode NEQ() { return getToken(EqlBaseParser.NEQ, 0); } - public TerminalNode LT() { return getToken(EqlBaseParser.LT, 0); } - public TerminalNode LTE() { return getToken(EqlBaseParser.LTE, 0); } - public TerminalNode GT() { return getToken(EqlBaseParser.GT, 0); } - public TerminalNode GTE() { return getToken(EqlBaseParser.GTE, 0); } - public ComparisonOperatorContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_comparisonOperator; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).enterComparisonOperator(this); + + public final ConstantContext constant() throws RecognitionException { + ConstantContext _localctx = new ConstantContext(_ctx, getState()); + enterRule(_localctx, 44, RULE_constant); + try { + setState(276); + _errHandler.sync(this); + switch (_input.LA(1)) { + case NULL: + _localctx = new NullLiteralContext(_localctx); + enterOuterAlt(_localctx, 1); { + setState(272); + match(NULL); + } + break; + case INTEGER_VALUE: + case DECIMAL_VALUE: + _localctx = new NumericLiteralContext(_localctx); + enterOuterAlt(_localctx, 2); { + setState(273); + number(); + } + break; + case FALSE: + case TRUE: + _localctx = new BooleanLiteralContext(_localctx); + enterOuterAlt(_localctx, 3); { + setState(274); + booleanValue(); + } + break; + case STRING: + _localctx = new StringLiteralContext(_localctx); + enterOuterAlt(_localctx, 4); { + setState(275); + string(); + } + break; + default: + throw new NoViableAltException(this); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).exitComparisonOperator(this); + + public static class ComparisonOperatorContext extends ParserRuleContext { + public TerminalNode EQ() { + return getToken(EqlBaseParser.EQ, 0); + } + + public TerminalNode NEQ() { + return getToken(EqlBaseParser.NEQ, 0); + } + + public TerminalNode LT() { + return getToken(EqlBaseParser.LT, 0); + } + + public TerminalNode LTE() { + return getToken(EqlBaseParser.LTE, 0); + } + + public TerminalNode GT() { + return getToken(EqlBaseParser.GT, 0); + } + + public TerminalNode GTE() { + return getToken(EqlBaseParser.GTE, 0); + } + + public ComparisonOperatorContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_comparisonOperator; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).enterComparisonOperator(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).exitComparisonOperator(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof EqlBaseVisitor) return ((EqlBaseVisitor) visitor).visitComparisonOperator(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof EqlBaseVisitor ) return ((EqlBaseVisitor)visitor).visitComparisonOperator(this); - else return visitor.visitChildren(this); - } - } - - public final ComparisonOperatorContext comparisonOperator() throws RecognitionException { - ComparisonOperatorContext _localctx = new ComparisonOperatorContext(_ctx, getState()); - enterRule(_localctx, 46, RULE_comparisonOperator); - int _la; - try { - enterOuterAlt(_localctx, 1); - { - setState(278); - _la = _input.LA(1); - if ( !((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << EQ) | (1L << NEQ) | (1L << LT) | (1L << LTE) | (1L << GT) | (1L << GTE))) != 0)) ) { - _errHandler.recoverInline(this); - } - else { - if ( _input.LA(1)==Token.EOF ) matchedEOF = true; - _errHandler.reportMatch(this); - consume(); - } - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class BooleanValueContext extends ParserRuleContext { - public TerminalNode TRUE() { return getToken(EqlBaseParser.TRUE, 0); } - public TerminalNode FALSE() { return getToken(EqlBaseParser.FALSE, 0); } - public BooleanValueContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_booleanValue; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).enterBooleanValue(this); + + public final ComparisonOperatorContext comparisonOperator() throws RecognitionException { + ComparisonOperatorContext _localctx = new ComparisonOperatorContext(_ctx, getState()); + enterRule(_localctx, 46, RULE_comparisonOperator); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(278); + _la = _input.LA(1); + if (!((((_la) & ~0x3f) == 0 + && ((1L << _la) & ((1L << EQ) | (1L << NEQ) | (1L << LT) | (1L << LTE) | (1L << GT) | (1L << GTE))) != 0))) { + _errHandler.recoverInline(this); + } else { + if (_input.LA(1) == Token.EOF) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).exitBooleanValue(this); + + public static class BooleanValueContext extends ParserRuleContext { + public TerminalNode TRUE() { + return getToken(EqlBaseParser.TRUE, 0); + } + + public TerminalNode FALSE() { + return getToken(EqlBaseParser.FALSE, 0); + } + + public BooleanValueContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_booleanValue; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).enterBooleanValue(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).exitBooleanValue(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof EqlBaseVisitor) return ((EqlBaseVisitor) visitor).visitBooleanValue(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof EqlBaseVisitor ) return ((EqlBaseVisitor)visitor).visitBooleanValue(this); - else return visitor.visitChildren(this); - } - } - - public final BooleanValueContext booleanValue() throws RecognitionException { - BooleanValueContext _localctx = new BooleanValueContext(_ctx, getState()); - enterRule(_localctx, 48, RULE_booleanValue); - int _la; - try { - enterOuterAlt(_localctx, 1); - { - setState(280); - _la = _input.LA(1); - if ( !(_la==FALSE || _la==TRUE) ) { - _errHandler.recoverInline(this); - } - else { - if ( _input.LA(1)==Token.EOF ) matchedEOF = true; - _errHandler.reportMatch(this); - consume(); - } - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class QualifiedNameContext extends ParserRuleContext { - public List identifier() { - return getRuleContexts(IdentifierContext.class); - } - public IdentifierContext identifier(int i) { - return getRuleContext(IdentifierContext.class,i); - } - public TerminalNode OPTIONAL() { return getToken(EqlBaseParser.OPTIONAL, 0); } - public List DOT() { return getTokens(EqlBaseParser.DOT); } - public TerminalNode DOT(int i) { - return getToken(EqlBaseParser.DOT, i); - } - public List LB() { return getTokens(EqlBaseParser.LB); } - public TerminalNode LB(int i) { - return getToken(EqlBaseParser.LB, i); - } - public List RB() { return getTokens(EqlBaseParser.RB); } - public TerminalNode RB(int i) { - return getToken(EqlBaseParser.RB, i); - } - public List INTEGER_VALUE() { return getTokens(EqlBaseParser.INTEGER_VALUE); } - public TerminalNode INTEGER_VALUE(int i) { - return getToken(EqlBaseParser.INTEGER_VALUE, i); - } - public QualifiedNameContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_qualifiedName; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).enterQualifiedName(this); + + public final BooleanValueContext booleanValue() throws RecognitionException { + BooleanValueContext _localctx = new BooleanValueContext(_ctx, getState()); + enterRule(_localctx, 48, RULE_booleanValue); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(280); + _la = _input.LA(1); + if (!(_la == FALSE || _la == TRUE)) { + _errHandler.recoverInline(this); + } else { + if (_input.LA(1) == Token.EOF) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).exitQualifiedName(this); + + public static class QualifiedNameContext extends ParserRuleContext { + public List identifier() { + return getRuleContexts(IdentifierContext.class); + } + + public IdentifierContext identifier(int i) { + return getRuleContext(IdentifierContext.class, i); + } + + public TerminalNode OPTIONAL() { + return getToken(EqlBaseParser.OPTIONAL, 0); + } + + public List DOT() { + return getTokens(EqlBaseParser.DOT); + } + + public TerminalNode DOT(int i) { + return getToken(EqlBaseParser.DOT, i); + } + + public List LB() { + return getTokens(EqlBaseParser.LB); + } + + public TerminalNode LB(int i) { + return getToken(EqlBaseParser.LB, i); + } + + public List RB() { + return getTokens(EqlBaseParser.RB); + } + + public TerminalNode RB(int i) { + return getToken(EqlBaseParser.RB, i); + } + + public List INTEGER_VALUE() { + return getTokens(EqlBaseParser.INTEGER_VALUE); + } + + public TerminalNode INTEGER_VALUE(int i) { + return getToken(EqlBaseParser.INTEGER_VALUE, i); + } + + public QualifiedNameContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_qualifiedName; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).enterQualifiedName(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).exitQualifiedName(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof EqlBaseVisitor) return ((EqlBaseVisitor) visitor).visitQualifiedName(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof EqlBaseVisitor ) return ((EqlBaseVisitor)visitor).visitQualifiedName(this); - else return visitor.visitChildren(this); - } - } - - public final QualifiedNameContext qualifiedName() throws RecognitionException { - QualifiedNameContext _localctx = new QualifiedNameContext(_ctx, getState()); - enterRule(_localctx, 50, RULE_qualifiedName); - int _la; - try { - int _alt; - enterOuterAlt(_localctx, 1); - { - setState(283); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==OPTIONAL) { - { - setState(282); - match(OPTIONAL); - } - } - - setState(285); - identifier(); - setState(297); - _errHandler.sync(this); - _alt = getInterpreter().adaptivePredict(_input,36,_ctx); - while ( _alt!=2 && _alt!=org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER ) { - if ( _alt==1 ) { - { - setState(295); - _errHandler.sync(this); - switch (_input.LA(1)) { - case DOT: + + public final QualifiedNameContext qualifiedName() throws RecognitionException { + QualifiedNameContext _localctx = new QualifiedNameContext(_ctx, getState()); + enterRule(_localctx, 50, RULE_qualifiedName); + int _la; + try { + int _alt; + enterOuterAlt(_localctx, 1); { - setState(286); - match(DOT); - setState(287); - identifier(); + setState(283); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == OPTIONAL) { + { + setState(282); + match(OPTIONAL); + } + } + + setState(285); + identifier(); + setState(297); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input, 36, _ctx); + while (_alt != 2 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER) { + if (_alt == 1) { + { + setState(295); + _errHandler.sync(this); + switch (_input.LA(1)) { + case DOT: { + setState(286); + match(DOT); + setState(287); + identifier(); + } + break; + case LB: { + setState(288); + match(LB); + setState(290); + _errHandler.sync(this); + _la = _input.LA(1); + do { + { + { + setState(289); + match(INTEGER_VALUE); + } + } + setState(292); + _errHandler.sync(this); + _la = _input.LA(1); + } while (_la == INTEGER_VALUE); + setState(294); + match(RB); + } + break; + default: + throw new NoViableAltException(this); + } + } + } + setState(299); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input, 36, _ctx); + } } - break; - case LB: + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; + } + + public static class IdentifierContext extends ParserRuleContext { + public TerminalNode IDENTIFIER() { + return getToken(EqlBaseParser.IDENTIFIER, 0); + } + + public TerminalNode QUOTED_IDENTIFIER() { + return getToken(EqlBaseParser.QUOTED_IDENTIFIER, 0); + } + + public IdentifierContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_identifier; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).enterIdentifier(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).exitIdentifier(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof EqlBaseVisitor) return ((EqlBaseVisitor) visitor).visitIdentifier(this); + else return visitor.visitChildren(this); + } + } + + public final IdentifierContext identifier() throws RecognitionException { + IdentifierContext _localctx = new IdentifierContext(_ctx, getState()); + enterRule(_localctx, 52, RULE_identifier); + int _la; + try { + enterOuterAlt(_localctx, 1); { - setState(288); - match(LB); - setState(290); - _errHandler.sync(this); - _la = _input.LA(1); - do { - { - { - setState(289); - match(INTEGER_VALUE); - } - } - setState(292); - _errHandler.sync(this); - _la = _input.LA(1); - } while ( _la==INTEGER_VALUE ); - setState(294); - match(RB); + setState(300); + _la = _input.LA(1); + if (!(_la == IDENTIFIER || _la == QUOTED_IDENTIFIER)) { + _errHandler.recoverInline(this); + } else { + if (_input.LA(1) == Token.EOF) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } } - break; - default: - throw new NoViableAltException(this); - } - } - } - setState(299); - _errHandler.sync(this); - _alt = getInterpreter().adaptivePredict(_input,36,_ctx); - } - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class IdentifierContext extends ParserRuleContext { - public TerminalNode IDENTIFIER() { return getToken(EqlBaseParser.IDENTIFIER, 0); } - public TerminalNode QUOTED_IDENTIFIER() { return getToken(EqlBaseParser.QUOTED_IDENTIFIER, 0); } - public IdentifierContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_identifier; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).enterIdentifier(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).exitIdentifier(this); + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof EqlBaseVisitor ) return ((EqlBaseVisitor)visitor).visitIdentifier(this); - else return visitor.visitChildren(this); - } - } - - public final IdentifierContext identifier() throws RecognitionException { - IdentifierContext _localctx = new IdentifierContext(_ctx, getState()); - enterRule(_localctx, 52, RULE_identifier); - int _la; - try { - enterOuterAlt(_localctx, 1); - { - setState(300); - _la = _input.LA(1); - if ( !(_la==IDENTIFIER || _la==QUOTED_IDENTIFIER) ) { - _errHandler.recoverInline(this); - } - else { - if ( _input.LA(1)==Token.EOF ) matchedEOF = true; - _errHandler.reportMatch(this); - consume(); - } - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class TimeUnitContext extends ParserRuleContext { - public Token unit; - public NumberContext number() { - return getRuleContext(NumberContext.class,0); - } - public TerminalNode IDENTIFIER() { return getToken(EqlBaseParser.IDENTIFIER, 0); } - public TimeUnitContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_timeUnit; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).enterTimeUnit(this); + + public static class TimeUnitContext extends ParserRuleContext { + public Token unit; + + public NumberContext number() { + return getRuleContext(NumberContext.class, 0); + } + + public TerminalNode IDENTIFIER() { + return getToken(EqlBaseParser.IDENTIFIER, 0); + } + + public TimeUnitContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_timeUnit; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).enterTimeUnit(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).exitTimeUnit(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof EqlBaseVisitor) return ((EqlBaseVisitor) visitor).visitTimeUnit(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).exitTimeUnit(this); + + public final TimeUnitContext timeUnit() throws RecognitionException { + TimeUnitContext _localctx = new TimeUnitContext(_ctx, getState()); + enterRule(_localctx, 54, RULE_timeUnit); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(302); + number(); + setState(304); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == IDENTIFIER) { + { + setState(303); + ((TimeUnitContext) _localctx).unit = match(IDENTIFIER); + } + } + + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof EqlBaseVisitor ) return ((EqlBaseVisitor)visitor).visitTimeUnit(this); - else return visitor.visitChildren(this); - } - } - - public final TimeUnitContext timeUnit() throws RecognitionException { - TimeUnitContext _localctx = new TimeUnitContext(_ctx, getState()); - enterRule(_localctx, 54, RULE_timeUnit); - int _la; - try { - enterOuterAlt(_localctx, 1); - { - setState(302); - number(); - setState(304); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==IDENTIFIER) { - { - setState(303); - ((TimeUnitContext)_localctx).unit = match(IDENTIFIER); - } - } - - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class NumberContext extends ParserRuleContext { - public NumberContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_number; } - - public NumberContext() { } - public void copyFrom(NumberContext ctx) { - super.copyFrom(ctx); - } - } - public static class DecimalLiteralContext extends NumberContext { - public TerminalNode DECIMAL_VALUE() { return getToken(EqlBaseParser.DECIMAL_VALUE, 0); } - public DecimalLiteralContext(NumberContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).enterDecimalLiteral(this); + + public static class NumberContext extends ParserRuleContext { + public NumberContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_number; + } + + public NumberContext() {} + + public void copyFrom(NumberContext ctx) { + super.copyFrom(ctx); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).exitDecimalLiteral(this); + + public static class DecimalLiteralContext extends NumberContext { + public TerminalNode DECIMAL_VALUE() { + return getToken(EqlBaseParser.DECIMAL_VALUE, 0); + } + + public DecimalLiteralContext(NumberContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).enterDecimalLiteral(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).exitDecimalLiteral(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof EqlBaseVisitor) return ((EqlBaseVisitor) visitor).visitDecimalLiteral(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof EqlBaseVisitor ) return ((EqlBaseVisitor)visitor).visitDecimalLiteral(this); - else return visitor.visitChildren(this); - } - } - public static class IntegerLiteralContext extends NumberContext { - public TerminalNode INTEGER_VALUE() { return getToken(EqlBaseParser.INTEGER_VALUE, 0); } - public IntegerLiteralContext(NumberContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).enterIntegerLiteral(this); + + public static class IntegerLiteralContext extends NumberContext { + public TerminalNode INTEGER_VALUE() { + return getToken(EqlBaseParser.INTEGER_VALUE, 0); + } + + public IntegerLiteralContext(NumberContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).enterIntegerLiteral(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).exitIntegerLiteral(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof EqlBaseVisitor) return ((EqlBaseVisitor) visitor).visitIntegerLiteral(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).exitIntegerLiteral(this); + + public final NumberContext number() throws RecognitionException { + NumberContext _localctx = new NumberContext(_ctx, getState()); + enterRule(_localctx, 56, RULE_number); + try { + setState(308); + _errHandler.sync(this); + switch (_input.LA(1)) { + case DECIMAL_VALUE: + _localctx = new DecimalLiteralContext(_localctx); + enterOuterAlt(_localctx, 1); { + setState(306); + match(DECIMAL_VALUE); + } + break; + case INTEGER_VALUE: + _localctx = new IntegerLiteralContext(_localctx); + enterOuterAlt(_localctx, 2); { + setState(307); + match(INTEGER_VALUE); + } + break; + default: + throw new NoViableAltException(this); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof EqlBaseVisitor ) return ((EqlBaseVisitor)visitor).visitIntegerLiteral(this); - else return visitor.visitChildren(this); - } - } - - public final NumberContext number() throws RecognitionException { - NumberContext _localctx = new NumberContext(_ctx, getState()); - enterRule(_localctx, 56, RULE_number); - try { - setState(308); - _errHandler.sync(this); - switch (_input.LA(1)) { - case DECIMAL_VALUE: - _localctx = new DecimalLiteralContext(_localctx); - enterOuterAlt(_localctx, 1); - { - setState(306); - match(DECIMAL_VALUE); - } - break; - case INTEGER_VALUE: - _localctx = new IntegerLiteralContext(_localctx); - enterOuterAlt(_localctx, 2); - { - setState(307); - match(INTEGER_VALUE); - } - break; - default: - throw new NoViableAltException(this); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class StringContext extends ParserRuleContext { - public TerminalNode STRING() { return getToken(EqlBaseParser.STRING, 0); } - public StringContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_string; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).enterString(this); + + public static class StringContext extends ParserRuleContext { + public TerminalNode STRING() { + return getToken(EqlBaseParser.STRING, 0); + } + + public StringContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_string; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).enterString(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).exitString(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof EqlBaseVisitor) return ((EqlBaseVisitor) visitor).visitString(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).exitString(this); + + public final StringContext string() throws RecognitionException { + StringContext _localctx = new StringContext(_ctx, getState()); + enterRule(_localctx, 58, RULE_string); + try { + enterOuterAlt(_localctx, 1); + { + setState(310); + match(STRING); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof EqlBaseVisitor ) return ((EqlBaseVisitor)visitor).visitString(this); - else return visitor.visitChildren(this); - } - } - - public final StringContext string() throws RecognitionException { - StringContext _localctx = new StringContext(_ctx, getState()); - enterRule(_localctx, 58, RULE_string); - try { - enterOuterAlt(_localctx, 1); - { - setState(310); - match(STRING); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class EventValueContext extends ParserRuleContext { - public TerminalNode STRING() { return getToken(EqlBaseParser.STRING, 0); } - public TerminalNode IDENTIFIER() { return getToken(EqlBaseParser.IDENTIFIER, 0); } - public EventValueContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_eventValue; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).enterEventValue(this); + + public static class EventValueContext extends ParserRuleContext { + public TerminalNode STRING() { + return getToken(EqlBaseParser.STRING, 0); + } + + public TerminalNode IDENTIFIER() { + return getToken(EqlBaseParser.IDENTIFIER, 0); + } + + public EventValueContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_eventValue; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).enterEventValue(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof EqlBaseListener) ((EqlBaseListener) listener).exitEventValue(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof EqlBaseVisitor) return ((EqlBaseVisitor) visitor).visitEventValue(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof EqlBaseListener ) ((EqlBaseListener)listener).exitEventValue(this); + + public final EventValueContext eventValue() throws RecognitionException { + EventValueContext _localctx = new EventValueContext(_ctx, getState()); + enterRule(_localctx, 60, RULE_eventValue); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(312); + _la = _input.LA(1); + if (!(_la == STRING || _la == IDENTIFIER)) { + _errHandler.recoverInline(this); + } else { + if (_input.LA(1) == Token.EOF) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; + } + + public boolean sempred(RuleContext _localctx, int ruleIndex, int predIndex) { + switch (ruleIndex) { + case 15: + return booleanExpression_sempred((BooleanExpressionContext) _localctx, predIndex); + case 17: + return operatorExpression_sempred((OperatorExpressionContext) _localctx, predIndex); + } + return true; + } + + private boolean booleanExpression_sempred(BooleanExpressionContext _localctx, int predIndex) { + switch (predIndex) { + case 0: + return precpred(_ctx, 2); + case 1: + return precpred(_ctx, 1); + } + return true; + } + + private boolean operatorExpression_sempred(OperatorExpressionContext _localctx, int predIndex) { + switch (predIndex) { + case 2: + return precpred(_ctx, 2); + case 3: + return precpred(_ctx, 1); + } + return true; + } + + public static final String _serializedATN = "\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\3\65\u013d\4\2\t\2" + + "\4\3\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b\t\b\4\t\t\t\4\n\t\n\4\13" + + "\t\13\4\f\t\f\4\r\t\r\4\16\t\16\4\17\t\17\4\20\t\20\4\21\t\21\4\22\t\22" + + "\4\23\t\23\4\24\t\24\4\25\t\25\4\26\t\26\4\27\t\27\4\30\t\30\4\31\t\31" + + "\4\32\t\32\4\33\t\33\4\34\t\34\4\35\t\35\4\36\t\36\4\37\t\37\4 \t \3\2" + + "\3\2\3\2\3\3\3\3\3\3\3\4\3\4\7\4I\n\4\f\4\16\4L\13\4\3\5\3\5\3\5\5\5Q" + + "\n\5\3\6\3\6\3\6\3\6\3\6\3\7\3\7\3\7\5\7[\n\7\3\7\3\7\5\7_\n\7\5\7a\n" + + "\7\3\7\6\7d\n\7\r\7\16\7e\3\7\3\7\5\7j\n\7\3\b\3\b\5\bn\n\b\3\b\3\b\6" + + "\br\n\b\r\b\16\bs\3\b\3\b\5\bx\n\b\3\t\3\t\3\t\3\t\3\t\7\t\177\n\t\f\t" + + "\16\t\u0082\13\t\5\t\u0084\n\t\3\n\3\n\3\n\3\n\7\n\u008a\n\n\f\n\16\n" + + "\u008d\13\n\3\13\3\13\5\13\u0091\n\13\3\f\3\f\5\f\u0095\n\f\3\f\3\f\3" + + "\f\3\f\5\f\u009b\n\f\3\r\3\r\3\r\3\r\3\16\3\16\3\17\3\17\5\17\u00a5\n" + + "\17\3\17\3\17\3\17\3\20\3\20\3\21\3\21\3\21\3\21\3\21\3\21\3\21\5\21\u00b3" + + "\n\21\3\21\3\21\3\21\3\21\3\21\3\21\7\21\u00bb\n\21\f\21\16\21\u00be\13" + + "\21\3\22\3\22\3\22\3\22\3\22\5\22\u00c5\n\22\3\23\3\23\3\23\5\23\u00ca" + + "\n\23\3\23\3\23\5\23\u00ce\n\23\3\23\3\23\3\23\3\23\3\23\3\23\7\23\u00d6" + + "\n\23\f\23\16\23\u00d9\13\23\3\24\5\24\u00dc\n\24\3\24\3\24\3\24\3\24" + + "\3\24\7\24\u00e3\n\24\f\24\16\24\u00e6\13\24\3\24\3\24\3\24\3\24\3\24" + + "\3\24\3\24\3\24\3\24\7\24\u00f1\n\24\f\24\16\24\u00f4\13\24\3\24\3\24" + + "\5\24\u00f8\n\24\3\25\3\25\3\25\3\25\3\25\3\25\3\25\5\25\u0101\n\25\3" + + "\26\3\26\3\26\3\26\3\26\7\26\u0108\n\26\f\26\16\26\u010b\13\26\5\26\u010d" + + "\n\26\3\26\3\26\3\27\3\27\3\30\3\30\3\30\3\30\5\30\u0117\n\30\3\31\3\31" + + "\3\32\3\32\3\33\5\33\u011e\n\33\3\33\3\33\3\33\3\33\3\33\6\33\u0125\n" + + "\33\r\33\16\33\u0126\3\33\7\33\u012a\n\33\f\33\16\33\u012d\13\33\3\34" + + "\3\34\3\35\3\35\5\35\u0133\n\35\3\36\3\36\5\36\u0137\n\36\3\37\3\37\3" + + " \3 \3 \2\4 $!\2\4\6\b\n\f\16\20\22\24\26\30\32\34\36 \"$&(*,.\60\62\64" + + "\668:<>\2\13\3\2 !\3\2\"$\3\2\7\b\5\2\n\13\21\22\30\30\4\2\60\60\62\62" + + "\3\2\32\37\4\2\6\6\24\24\3\2\60\61\4\2--\60\60\2\u014c\2@\3\2\2\2\4C\3" + + "\2\2\2\6F\3\2\2\2\bP\3\2\2\2\nR\3\2\2\2\fW\3\2\2\2\16k\3\2\2\2\20y\3\2" + + "\2\2\22\u0085\3\2\2\2\24\u008e\3\2\2\2\26\u0092\3\2\2\2\30\u009c\3\2\2" + + "\2\32\u00a0\3\2\2\2\34\u00a4\3\2\2\2\36\u00a9\3\2\2\2 \u00b2\3\2\2\2\"" + + "\u00c4\3\2\2\2$\u00cd\3\2\2\2&\u00f7\3\2\2\2(\u0100\3\2\2\2*\u0102\3\2" + + "\2\2,\u0110\3\2\2\2.\u0116\3\2\2\2\60\u0118\3\2\2\2\62\u011a\3\2\2\2\64" + + "\u011d\3\2\2\2\66\u012e\3\2\2\28\u0130\3\2\2\2:\u0136\3\2\2\2<\u0138\3" + + "\2\2\2>\u013a\3\2\2\2@A\5\6\4\2AB\7\2\2\3B\3\3\2\2\2CD\5\36\20\2DE\7\2" + + "\2\3E\5\3\2\2\2FJ\5\b\5\2GI\5\20\t\2HG\3\2\2\2IL\3\2\2\2JH\3\2\2\2JK\3" + + "\2\2\2K\7\3\2\2\2LJ\3\2\2\2MQ\5\f\7\2NQ\5\16\b\2OQ\5\32\16\2PM\3\2\2\2" + + "PN\3\2\2\2PO\3\2\2\2Q\t\3\2\2\2RS\7\27\2\2ST\7\f\2\2TU\7\31\2\2UV\58\35" + + "\2V\13\3\2\2\2W`\7\23\2\2XZ\5\22\n\2Y[\5\n\6\2ZY\3\2\2\2Z[\3\2\2\2[a\3" + + "\2\2\2\\^\5\n\6\2]_\5\22\n\2^]\3\2\2\2^_\3\2\2\2_a\3\2\2\2`X\3\2\2\2`" + + "\\\3\2\2\2`a\3\2\2\2ac\3\2\2\2bd\5\26\f\2cb\3\2\2\2de\3\2\2\2ec\3\2\2" + + "\2ef\3\2\2\2fi\3\2\2\2gh\7\25\2\2hj\5\26\f\2ig\3\2\2\2ij\3\2\2\2j\r\3" + + "\2\2\2km\7\t\2\2ln\5\22\n\2ml\3\2\2\2mn\3\2\2\2no\3\2\2\2oq\5\24\13\2" + + "pr\5\24\13\2qp\3\2\2\2rs\3\2\2\2sq\3\2\2\2st\3\2\2\2tw\3\2\2\2uv\7\25" + + "\2\2vx\5\24\13\2wu\3\2\2\2wx\3\2\2\2x\17\3\2\2\2yz\7+\2\2z\u0083\7\60" + + "\2\2{\u0080\5 \21\2|}\7&\2\2}\177\5 \21\2~|\3\2\2\2\177\u0082\3\2\2\2" + + "\u0080~\3\2\2\2\u0080\u0081\3\2\2\2\u0081\u0084\3\2\2\2\u0082\u0080\3" + + "\2\2\2\u0083{\3\2\2\2\u0083\u0084\3\2\2\2\u0084\21\3\2\2\2\u0085\u0086" + + "\7\5\2\2\u0086\u008b\5\36\20\2\u0087\u0088\7&\2\2\u0088\u008a\5\36\20" + + "\2\u0089\u0087\3\2\2\2\u008a\u008d\3\2\2\2\u008b\u0089\3\2\2\2\u008b\u008c" + + "\3\2\2\2\u008c\23\3\2\2\2\u008d\u008b\3\2\2\2\u008e\u0090\5\30\r\2\u008f" + + "\u0091\5\22\n\2\u0090\u008f\3\2\2\2\u0090\u0091\3\2\2\2\u0091\25\3\2\2" + + "\2\u0092\u0094\5\30\r\2\u0093\u0095\5\22\n\2\u0094\u0093\3\2\2\2\u0094" + + "\u0095\3\2\2\2\u0095\u009a\3\2\2\2\u0096\u0097\7\27\2\2\u0097\u0098\7" + + "\60\2\2\u0098\u0099\7\31\2\2\u0099\u009b\5:\36\2\u009a\u0096\3\2\2\2\u009a" + + "\u009b\3\2\2\2\u009b\27\3\2\2\2\u009c\u009d\7\'\2\2\u009d\u009e\5\34\17" + + "\2\u009e\u009f\7(\2\2\u009f\31\3\2\2\2\u00a0\u00a1\5\34\17\2\u00a1\33" + + "\3\2\2\2\u00a2\u00a5\7\4\2\2\u00a3\u00a5\5> \2\u00a4\u00a2\3\2\2\2\u00a4" + + "\u00a3\3\2\2\2\u00a5\u00a6\3\2\2\2\u00a6\u00a7\7\26\2\2\u00a7\u00a8\5" + + "\36\20\2\u00a8\35\3\2\2\2\u00a9\u00aa\5 \21\2\u00aa\37\3\2\2\2\u00ab\u00ac" + + "\b\21\1\2\u00ac\u00ad\7\r\2\2\u00ad\u00b3\5 \21\7\u00ae\u00af\7\60\2\2" + + "\u00af\u00b0\7\17\2\2\u00b0\u00b3\5\30\r\2\u00b1\u00b3\5\"\22\2\u00b2" + + "\u00ab\3\2\2\2\u00b2\u00ae\3\2\2\2\u00b2\u00b1\3\2\2\2\u00b3\u00bc\3\2" + + "\2\2\u00b4\u00b5\f\4\2\2\u00b5\u00b6\7\3\2\2\u00b6\u00bb\5 \21\5\u00b7" + + "\u00b8\f\3\2\2\u00b8\u00b9\7\20\2\2\u00b9\u00bb\5 \21\4\u00ba\u00b4\3" + + "\2\2\2\u00ba\u00b7\3\2\2\2\u00bb\u00be\3\2\2\2\u00bc\u00ba\3\2\2\2\u00bc" + + "\u00bd\3\2\2\2\u00bd!\3\2\2\2\u00be\u00bc\3\2\2\2\u00bf\u00c5\5$\23\2" + + "\u00c0\u00c1\5$\23\2\u00c1\u00c2\5\60\31\2\u00c2\u00c3\5$\23\2\u00c3\u00c5" + + "\3\2\2\2\u00c4\u00bf\3\2\2\2\u00c4\u00c0\3\2\2\2\u00c5#\3\2\2\2\u00c6" + + "\u00c7\b\23\1\2\u00c7\u00c9\5(\25\2\u00c8\u00ca\5&\24\2\u00c9\u00c8\3" + + "\2\2\2\u00c9\u00ca\3\2\2\2\u00ca\u00ce\3\2\2\2\u00cb\u00cc\t\2\2\2\u00cc" + + "\u00ce\5$\23\5\u00cd\u00c6\3\2\2\2\u00cd\u00cb\3\2\2\2\u00ce\u00d7\3\2" + + "\2\2\u00cf\u00d0\f\4\2\2\u00d0\u00d1\t\3\2\2\u00d1\u00d6\5$\23\5\u00d2" + + "\u00d3\f\3\2\2\u00d3\u00d4\t\2\2\2\u00d4\u00d6\5$\23\4\u00d5\u00cf\3\2" + + "\2\2\u00d5\u00d2\3\2\2\2\u00d6\u00d9\3\2\2\2\u00d7\u00d5\3\2\2\2\u00d7" + + "\u00d8\3\2\2\2\u00d8%\3\2\2\2\u00d9\u00d7\3\2\2\2\u00da\u00dc\7\r\2\2" + + "\u00db\u00da\3\2\2\2\u00db\u00dc\3\2\2\2\u00dc\u00dd\3\2\2\2\u00dd\u00de" + + "\t\4\2\2\u00de\u00df\7)\2\2\u00df\u00e4\5\36\20\2\u00e0\u00e1\7&\2\2\u00e1" + + "\u00e3\5\36\20\2\u00e2\u00e0\3\2\2\2\u00e3\u00e6\3\2\2\2\u00e4\u00e2\3" + + "\2\2\2\u00e4\u00e5\3\2\2\2\u00e5\u00e7\3\2\2\2\u00e6\u00e4\3\2\2\2\u00e7" + + "\u00e8\7*\2\2\u00e8\u00f8\3\2\2\2\u00e9\u00ea\t\5\2\2\u00ea\u00f8\5.\30" + + "\2\u00eb\u00ec\t\5\2\2\u00ec\u00ed\7)\2\2\u00ed\u00f2\5.\30\2\u00ee\u00ef" + + "\7&\2\2\u00ef\u00f1\5.\30\2\u00f0\u00ee\3\2\2\2\u00f1\u00f4\3\2\2\2\u00f2" + + "\u00f0\3\2\2\2\u00f2\u00f3\3\2\2\2\u00f3\u00f5\3\2\2\2\u00f4\u00f2\3\2" + + "\2\2\u00f5\u00f6\7*\2\2\u00f6\u00f8\3\2\2\2\u00f7\u00db\3\2\2\2\u00f7" + + "\u00e9\3\2\2\2\u00f7\u00eb\3\2\2\2\u00f8\'\3\2\2\2\u00f9\u0101\5.\30\2" + + "\u00fa\u0101\5*\26\2\u00fb\u0101\5\64\33\2\u00fc\u00fd\7)\2\2\u00fd\u00fe" + + "\5\36\20\2\u00fe\u00ff\7*\2\2\u00ff\u0101\3\2\2\2\u0100\u00f9\3\2\2\2" + + "\u0100\u00fa\3\2\2\2\u0100\u00fb\3\2\2\2\u0100\u00fc\3\2\2\2\u0101)\3" + + "\2\2\2\u0102\u0103\5,\27\2\u0103\u010c\7)\2\2\u0104\u0109\5\36\20\2\u0105" + + "\u0106\7&\2\2\u0106\u0108\5\36\20\2\u0107\u0105\3\2\2\2\u0108\u010b\3" + + "\2\2\2\u0109\u0107\3\2\2\2\u0109\u010a\3\2\2\2\u010a\u010d\3\2\2\2\u010b" + + "\u0109\3\2\2\2\u010c\u0104\3\2\2\2\u010c\u010d\3\2\2\2\u010d\u010e\3\2" + + "\2\2\u010e\u010f\7*\2\2\u010f+\3\2\2\2\u0110\u0111\t\6\2\2\u0111-\3\2" + + "\2\2\u0112\u0117\7\16\2\2\u0113\u0117\5:\36\2\u0114\u0117\5\62\32\2\u0115" + + "\u0117\5<\37\2\u0116\u0112\3\2\2\2\u0116\u0113\3\2\2\2\u0116\u0114\3\2" + + "\2\2\u0116\u0115\3\2\2\2\u0117/\3\2\2\2\u0118\u0119\t\7\2\2\u0119\61\3" + + "\2\2\2\u011a\u011b\t\b\2\2\u011b\63\3\2\2\2\u011c\u011e\7,\2\2\u011d\u011c" + + "\3\2\2\2\u011d\u011e\3\2\2\2\u011e\u011f\3\2\2\2\u011f\u012b\5\66\34\2" + + "\u0120\u0121\7%\2\2\u0121\u012a\5\66\34\2\u0122\u0124\7\'\2\2\u0123\u0125" + + "\7.\2\2\u0124\u0123\3\2\2\2\u0125\u0126\3\2\2\2\u0126\u0124\3\2\2\2\u0126" + + "\u0127\3\2\2\2\u0127\u0128\3\2\2\2\u0128\u012a\7(\2\2\u0129\u0120\3\2" + + "\2\2\u0129\u0122\3\2\2\2\u012a\u012d\3\2\2\2\u012b\u0129\3\2\2\2\u012b" + + "\u012c\3\2\2\2\u012c\65\3\2\2\2\u012d\u012b\3\2\2\2\u012e\u012f\t\t\2" + + "\2\u012f\67\3\2\2\2\u0130\u0132\5:\36\2\u0131\u0133\7\60\2\2\u0132\u0131" + + "\3\2\2\2\u0132\u0133\3\2\2\2\u01339\3\2\2\2\u0134\u0137\7/\2\2\u0135\u0137" + + "\7.\2\2\u0136\u0134\3\2\2\2\u0136\u0135\3\2\2\2\u0137;\3\2\2\2\u0138\u0139" + + "\7-\2\2\u0139=\3\2\2\2\u013a\u013b\t\n\2\2\u013b?\3\2\2\2)JPZ^`eimsw\u0080" + + "\u0083\u008b\u0090\u0094\u009a\u00a4\u00b2\u00ba\u00bc\u00c4\u00c9\u00cd" + + "\u00d5\u00d7\u00db\u00e4\u00f2\u00f7\u0100\u0109\u010c\u0116\u011d\u0126" + + "\u0129\u012b\u0132\u0136"; + public static final ATN _ATN = new ATNDeserializer().deserialize(_serializedATN.toCharArray()); + static { + _decisionToDFA = new DFA[_ATN.getNumberOfDecisions()]; + for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) { + _decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof EqlBaseVisitor ) return ((EqlBaseVisitor)visitor).visitEventValue(this); - else return visitor.visitChildren(this); - } - } - - public final EventValueContext eventValue() throws RecognitionException { - EventValueContext _localctx = new EventValueContext(_ctx, getState()); - enterRule(_localctx, 60, RULE_eventValue); - int _la; - try { - enterOuterAlt(_localctx, 1); - { - setState(312); - _la = _input.LA(1); - if ( !(_la==STRING || _la==IDENTIFIER) ) { - _errHandler.recoverInline(this); - } - else { - if ( _input.LA(1)==Token.EOF ) matchedEOF = true; - _errHandler.reportMatch(this); - consume(); - } - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public boolean sempred(RuleContext _localctx, int ruleIndex, int predIndex) { - switch (ruleIndex) { - case 15: - return booleanExpression_sempred((BooleanExpressionContext)_localctx, predIndex); - case 17: - return operatorExpression_sempred((OperatorExpressionContext)_localctx, predIndex); - } - return true; - } - private boolean booleanExpression_sempred(BooleanExpressionContext _localctx, int predIndex) { - switch (predIndex) { - case 0: - return precpred(_ctx, 2); - case 1: - return precpred(_ctx, 1); - } - return true; - } - private boolean operatorExpression_sempred(OperatorExpressionContext _localctx, int predIndex) { - switch (predIndex) { - case 2: - return precpred(_ctx, 2); - case 3: - return precpred(_ctx, 1); - } - return true; - } - - public static final String _serializedATN = - "\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\3\65\u013d\4\2\t\2"+ - "\4\3\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b\t\b\4\t\t\t\4\n\t\n\4\13"+ - "\t\13\4\f\t\f\4\r\t\r\4\16\t\16\4\17\t\17\4\20\t\20\4\21\t\21\4\22\t\22"+ - "\4\23\t\23\4\24\t\24\4\25\t\25\4\26\t\26\4\27\t\27\4\30\t\30\4\31\t\31"+ - "\4\32\t\32\4\33\t\33\4\34\t\34\4\35\t\35\4\36\t\36\4\37\t\37\4 \t \3\2"+ - "\3\2\3\2\3\3\3\3\3\3\3\4\3\4\7\4I\n\4\f\4\16\4L\13\4\3\5\3\5\3\5\5\5Q"+ - "\n\5\3\6\3\6\3\6\3\6\3\6\3\7\3\7\3\7\5\7[\n\7\3\7\3\7\5\7_\n\7\5\7a\n"+ - "\7\3\7\6\7d\n\7\r\7\16\7e\3\7\3\7\5\7j\n\7\3\b\3\b\5\bn\n\b\3\b\3\b\6"+ - "\br\n\b\r\b\16\bs\3\b\3\b\5\bx\n\b\3\t\3\t\3\t\3\t\3\t\7\t\177\n\t\f\t"+ - "\16\t\u0082\13\t\5\t\u0084\n\t\3\n\3\n\3\n\3\n\7\n\u008a\n\n\f\n\16\n"+ - "\u008d\13\n\3\13\3\13\5\13\u0091\n\13\3\f\3\f\5\f\u0095\n\f\3\f\3\f\3"+ - "\f\3\f\5\f\u009b\n\f\3\r\3\r\3\r\3\r\3\16\3\16\3\17\3\17\5\17\u00a5\n"+ - "\17\3\17\3\17\3\17\3\20\3\20\3\21\3\21\3\21\3\21\3\21\3\21\3\21\5\21\u00b3"+ - "\n\21\3\21\3\21\3\21\3\21\3\21\3\21\7\21\u00bb\n\21\f\21\16\21\u00be\13"+ - "\21\3\22\3\22\3\22\3\22\3\22\5\22\u00c5\n\22\3\23\3\23\3\23\5\23\u00ca"+ - "\n\23\3\23\3\23\5\23\u00ce\n\23\3\23\3\23\3\23\3\23\3\23\3\23\7\23\u00d6"+ - "\n\23\f\23\16\23\u00d9\13\23\3\24\5\24\u00dc\n\24\3\24\3\24\3\24\3\24"+ - "\3\24\7\24\u00e3\n\24\f\24\16\24\u00e6\13\24\3\24\3\24\3\24\3\24\3\24"+ - "\3\24\3\24\3\24\3\24\7\24\u00f1\n\24\f\24\16\24\u00f4\13\24\3\24\3\24"+ - "\5\24\u00f8\n\24\3\25\3\25\3\25\3\25\3\25\3\25\3\25\5\25\u0101\n\25\3"+ - "\26\3\26\3\26\3\26\3\26\7\26\u0108\n\26\f\26\16\26\u010b\13\26\5\26\u010d"+ - "\n\26\3\26\3\26\3\27\3\27\3\30\3\30\3\30\3\30\5\30\u0117\n\30\3\31\3\31"+ - "\3\32\3\32\3\33\5\33\u011e\n\33\3\33\3\33\3\33\3\33\3\33\6\33\u0125\n"+ - "\33\r\33\16\33\u0126\3\33\7\33\u012a\n\33\f\33\16\33\u012d\13\33\3\34"+ - "\3\34\3\35\3\35\5\35\u0133\n\35\3\36\3\36\5\36\u0137\n\36\3\37\3\37\3"+ - " \3 \3 \2\4 $!\2\4\6\b\n\f\16\20\22\24\26\30\32\34\36 \"$&(*,.\60\62\64"+ - "\668:<>\2\13\3\2 !\3\2\"$\3\2\7\b\5\2\n\13\21\22\30\30\4\2\60\60\62\62"+ - "\3\2\32\37\4\2\6\6\24\24\3\2\60\61\4\2--\60\60\2\u014c\2@\3\2\2\2\4C\3"+ - "\2\2\2\6F\3\2\2\2\bP\3\2\2\2\nR\3\2\2\2\fW\3\2\2\2\16k\3\2\2\2\20y\3\2"+ - "\2\2\22\u0085\3\2\2\2\24\u008e\3\2\2\2\26\u0092\3\2\2\2\30\u009c\3\2\2"+ - "\2\32\u00a0\3\2\2\2\34\u00a4\3\2\2\2\36\u00a9\3\2\2\2 \u00b2\3\2\2\2\""+ - "\u00c4\3\2\2\2$\u00cd\3\2\2\2&\u00f7\3\2\2\2(\u0100\3\2\2\2*\u0102\3\2"+ - "\2\2,\u0110\3\2\2\2.\u0116\3\2\2\2\60\u0118\3\2\2\2\62\u011a\3\2\2\2\64"+ - "\u011d\3\2\2\2\66\u012e\3\2\2\28\u0130\3\2\2\2:\u0136\3\2\2\2<\u0138\3"+ - "\2\2\2>\u013a\3\2\2\2@A\5\6\4\2AB\7\2\2\3B\3\3\2\2\2CD\5\36\20\2DE\7\2"+ - "\2\3E\5\3\2\2\2FJ\5\b\5\2GI\5\20\t\2HG\3\2\2\2IL\3\2\2\2JH\3\2\2\2JK\3"+ - "\2\2\2K\7\3\2\2\2LJ\3\2\2\2MQ\5\f\7\2NQ\5\16\b\2OQ\5\32\16\2PM\3\2\2\2"+ - "PN\3\2\2\2PO\3\2\2\2Q\t\3\2\2\2RS\7\27\2\2ST\7\f\2\2TU\7\31\2\2UV\58\35"+ - "\2V\13\3\2\2\2W`\7\23\2\2XZ\5\22\n\2Y[\5\n\6\2ZY\3\2\2\2Z[\3\2\2\2[a\3"+ - "\2\2\2\\^\5\n\6\2]_\5\22\n\2^]\3\2\2\2^_\3\2\2\2_a\3\2\2\2`X\3\2\2\2`"+ - "\\\3\2\2\2`a\3\2\2\2ac\3\2\2\2bd\5\26\f\2cb\3\2\2\2de\3\2\2\2ec\3\2\2"+ - "\2ef\3\2\2\2fi\3\2\2\2gh\7\25\2\2hj\5\26\f\2ig\3\2\2\2ij\3\2\2\2j\r\3"+ - "\2\2\2km\7\t\2\2ln\5\22\n\2ml\3\2\2\2mn\3\2\2\2no\3\2\2\2oq\5\24\13\2"+ - "pr\5\24\13\2qp\3\2\2\2rs\3\2\2\2sq\3\2\2\2st\3\2\2\2tw\3\2\2\2uv\7\25"+ - "\2\2vx\5\24\13\2wu\3\2\2\2wx\3\2\2\2x\17\3\2\2\2yz\7+\2\2z\u0083\7\60"+ - "\2\2{\u0080\5 \21\2|}\7&\2\2}\177\5 \21\2~|\3\2\2\2\177\u0082\3\2\2\2"+ - "\u0080~\3\2\2\2\u0080\u0081\3\2\2\2\u0081\u0084\3\2\2\2\u0082\u0080\3"+ - "\2\2\2\u0083{\3\2\2\2\u0083\u0084\3\2\2\2\u0084\21\3\2\2\2\u0085\u0086"+ - "\7\5\2\2\u0086\u008b\5\36\20\2\u0087\u0088\7&\2\2\u0088\u008a\5\36\20"+ - "\2\u0089\u0087\3\2\2\2\u008a\u008d\3\2\2\2\u008b\u0089\3\2\2\2\u008b\u008c"+ - "\3\2\2\2\u008c\23\3\2\2\2\u008d\u008b\3\2\2\2\u008e\u0090\5\30\r\2\u008f"+ - "\u0091\5\22\n\2\u0090\u008f\3\2\2\2\u0090\u0091\3\2\2\2\u0091\25\3\2\2"+ - "\2\u0092\u0094\5\30\r\2\u0093\u0095\5\22\n\2\u0094\u0093\3\2\2\2\u0094"+ - "\u0095\3\2\2\2\u0095\u009a\3\2\2\2\u0096\u0097\7\27\2\2\u0097\u0098\7"+ - "\60\2\2\u0098\u0099\7\31\2\2\u0099\u009b\5:\36\2\u009a\u0096\3\2\2\2\u009a"+ - "\u009b\3\2\2\2\u009b\27\3\2\2\2\u009c\u009d\7\'\2\2\u009d\u009e\5\34\17"+ - "\2\u009e\u009f\7(\2\2\u009f\31\3\2\2\2\u00a0\u00a1\5\34\17\2\u00a1\33"+ - "\3\2\2\2\u00a2\u00a5\7\4\2\2\u00a3\u00a5\5> \2\u00a4\u00a2\3\2\2\2\u00a4"+ - "\u00a3\3\2\2\2\u00a5\u00a6\3\2\2\2\u00a6\u00a7\7\26\2\2\u00a7\u00a8\5"+ - "\36\20\2\u00a8\35\3\2\2\2\u00a9\u00aa\5 \21\2\u00aa\37\3\2\2\2\u00ab\u00ac"+ - "\b\21\1\2\u00ac\u00ad\7\r\2\2\u00ad\u00b3\5 \21\7\u00ae\u00af\7\60\2\2"+ - "\u00af\u00b0\7\17\2\2\u00b0\u00b3\5\30\r\2\u00b1\u00b3\5\"\22\2\u00b2"+ - "\u00ab\3\2\2\2\u00b2\u00ae\3\2\2\2\u00b2\u00b1\3\2\2\2\u00b3\u00bc\3\2"+ - "\2\2\u00b4\u00b5\f\4\2\2\u00b5\u00b6\7\3\2\2\u00b6\u00bb\5 \21\5\u00b7"+ - "\u00b8\f\3\2\2\u00b8\u00b9\7\20\2\2\u00b9\u00bb\5 \21\4\u00ba\u00b4\3"+ - "\2\2\2\u00ba\u00b7\3\2\2\2\u00bb\u00be\3\2\2\2\u00bc\u00ba\3\2\2\2\u00bc"+ - "\u00bd\3\2\2\2\u00bd!\3\2\2\2\u00be\u00bc\3\2\2\2\u00bf\u00c5\5$\23\2"+ - "\u00c0\u00c1\5$\23\2\u00c1\u00c2\5\60\31\2\u00c2\u00c3\5$\23\2\u00c3\u00c5"+ - "\3\2\2\2\u00c4\u00bf\3\2\2\2\u00c4\u00c0\3\2\2\2\u00c5#\3\2\2\2\u00c6"+ - "\u00c7\b\23\1\2\u00c7\u00c9\5(\25\2\u00c8\u00ca\5&\24\2\u00c9\u00c8\3"+ - "\2\2\2\u00c9\u00ca\3\2\2\2\u00ca\u00ce\3\2\2\2\u00cb\u00cc\t\2\2\2\u00cc"+ - "\u00ce\5$\23\5\u00cd\u00c6\3\2\2\2\u00cd\u00cb\3\2\2\2\u00ce\u00d7\3\2"+ - "\2\2\u00cf\u00d0\f\4\2\2\u00d0\u00d1\t\3\2\2\u00d1\u00d6\5$\23\5\u00d2"+ - "\u00d3\f\3\2\2\u00d3\u00d4\t\2\2\2\u00d4\u00d6\5$\23\4\u00d5\u00cf\3\2"+ - "\2\2\u00d5\u00d2\3\2\2\2\u00d6\u00d9\3\2\2\2\u00d7\u00d5\3\2\2\2\u00d7"+ - "\u00d8\3\2\2\2\u00d8%\3\2\2\2\u00d9\u00d7\3\2\2\2\u00da\u00dc\7\r\2\2"+ - "\u00db\u00da\3\2\2\2\u00db\u00dc\3\2\2\2\u00dc\u00dd\3\2\2\2\u00dd\u00de"+ - "\t\4\2\2\u00de\u00df\7)\2\2\u00df\u00e4\5\36\20\2\u00e0\u00e1\7&\2\2\u00e1"+ - "\u00e3\5\36\20\2\u00e2\u00e0\3\2\2\2\u00e3\u00e6\3\2\2\2\u00e4\u00e2\3"+ - "\2\2\2\u00e4\u00e5\3\2\2\2\u00e5\u00e7\3\2\2\2\u00e6\u00e4\3\2\2\2\u00e7"+ - "\u00e8\7*\2\2\u00e8\u00f8\3\2\2\2\u00e9\u00ea\t\5\2\2\u00ea\u00f8\5.\30"+ - "\2\u00eb\u00ec\t\5\2\2\u00ec\u00ed\7)\2\2\u00ed\u00f2\5.\30\2\u00ee\u00ef"+ - "\7&\2\2\u00ef\u00f1\5.\30\2\u00f0\u00ee\3\2\2\2\u00f1\u00f4\3\2\2\2\u00f2"+ - "\u00f0\3\2\2\2\u00f2\u00f3\3\2\2\2\u00f3\u00f5\3\2\2\2\u00f4\u00f2\3\2"+ - "\2\2\u00f5\u00f6\7*\2\2\u00f6\u00f8\3\2\2\2\u00f7\u00db\3\2\2\2\u00f7"+ - "\u00e9\3\2\2\2\u00f7\u00eb\3\2\2\2\u00f8\'\3\2\2\2\u00f9\u0101\5.\30\2"+ - "\u00fa\u0101\5*\26\2\u00fb\u0101\5\64\33\2\u00fc\u00fd\7)\2\2\u00fd\u00fe"+ - "\5\36\20\2\u00fe\u00ff\7*\2\2\u00ff\u0101\3\2\2\2\u0100\u00f9\3\2\2\2"+ - "\u0100\u00fa\3\2\2\2\u0100\u00fb\3\2\2\2\u0100\u00fc\3\2\2\2\u0101)\3"+ - "\2\2\2\u0102\u0103\5,\27\2\u0103\u010c\7)\2\2\u0104\u0109\5\36\20\2\u0105"+ - "\u0106\7&\2\2\u0106\u0108\5\36\20\2\u0107\u0105\3\2\2\2\u0108\u010b\3"+ - "\2\2\2\u0109\u0107\3\2\2\2\u0109\u010a\3\2\2\2\u010a\u010d\3\2\2\2\u010b"+ - "\u0109\3\2\2\2\u010c\u0104\3\2\2\2\u010c\u010d\3\2\2\2\u010d\u010e\3\2"+ - "\2\2\u010e\u010f\7*\2\2\u010f+\3\2\2\2\u0110\u0111\t\6\2\2\u0111-\3\2"+ - "\2\2\u0112\u0117\7\16\2\2\u0113\u0117\5:\36\2\u0114\u0117\5\62\32\2\u0115"+ - "\u0117\5<\37\2\u0116\u0112\3\2\2\2\u0116\u0113\3\2\2\2\u0116\u0114\3\2"+ - "\2\2\u0116\u0115\3\2\2\2\u0117/\3\2\2\2\u0118\u0119\t\7\2\2\u0119\61\3"+ - "\2\2\2\u011a\u011b\t\b\2\2\u011b\63\3\2\2\2\u011c\u011e\7,\2\2\u011d\u011c"+ - "\3\2\2\2\u011d\u011e\3\2\2\2\u011e\u011f\3\2\2\2\u011f\u012b\5\66\34\2"+ - "\u0120\u0121\7%\2\2\u0121\u012a\5\66\34\2\u0122\u0124\7\'\2\2\u0123\u0125"+ - "\7.\2\2\u0124\u0123\3\2\2\2\u0125\u0126\3\2\2\2\u0126\u0124\3\2\2\2\u0126"+ - "\u0127\3\2\2\2\u0127\u0128\3\2\2\2\u0128\u012a\7(\2\2\u0129\u0120\3\2"+ - "\2\2\u0129\u0122\3\2\2\2\u012a\u012d\3\2\2\2\u012b\u0129\3\2\2\2\u012b"+ - "\u012c\3\2\2\2\u012c\65\3\2\2\2\u012d\u012b\3\2\2\2\u012e\u012f\t\t\2"+ - "\2\u012f\67\3\2\2\2\u0130\u0132\5:\36\2\u0131\u0133\7\60\2\2\u0132\u0131"+ - "\3\2\2\2\u0132\u0133\3\2\2\2\u01339\3\2\2\2\u0134\u0137\7/\2\2\u0135\u0137"+ - "\7.\2\2\u0136\u0134\3\2\2\2\u0136\u0135\3\2\2\2\u0137;\3\2\2\2\u0138\u0139"+ - "\7-\2\2\u0139=\3\2\2\2\u013a\u013b\t\n\2\2\u013b?\3\2\2\2)JPZ^`eimsw\u0080"+ - "\u0083\u008b\u0090\u0094\u009a\u00a4\u00b2\u00ba\u00bc\u00c4\u00c9\u00cd"+ - "\u00d5\u00d7\u00db\u00e4\u00f2\u00f7\u0100\u0109\u010c\u0116\u011d\u0126"+ - "\u0129\u012b\u0132\u0136"; - public static final ATN _ATN = - new ATNDeserializer().deserialize(_serializedATN.toCharArray()); - static { - _decisionToDFA = new DFA[_ATN.getNumberOfDecisions()]; - for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) { - _decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i); - } - } } diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/parser/EqlBaseVisitor.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/parser/EqlBaseVisitor.java index ceb7d7f1d05c0..5969ba619118e 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/parser/EqlBaseVisitor.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/parser/EqlBaseVisitor.java @@ -1,5 +1,6 @@ // ANTLR GENERATED CODE: DO NOT EDIT package org.elasticsearch.xpack.eql.parser; + import org.antlr.v4.runtime.tree.ParseTreeVisitor; /** @@ -10,287 +11,330 @@ * operations with no return type. */ interface EqlBaseVisitor extends ParseTreeVisitor { - /** - * Visit a parse tree produced by {@link EqlBaseParser#singleStatement}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitSingleStatement(EqlBaseParser.SingleStatementContext ctx); - /** - * Visit a parse tree produced by {@link EqlBaseParser#singleExpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitSingleExpression(EqlBaseParser.SingleExpressionContext ctx); - /** - * Visit a parse tree produced by {@link EqlBaseParser#statement}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitStatement(EqlBaseParser.StatementContext ctx); - /** - * Visit a parse tree produced by {@link EqlBaseParser#query}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitQuery(EqlBaseParser.QueryContext ctx); - /** - * Visit a parse tree produced by {@link EqlBaseParser#sequenceParams}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitSequenceParams(EqlBaseParser.SequenceParamsContext ctx); - /** - * Visit a parse tree produced by {@link EqlBaseParser#sequence}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitSequence(EqlBaseParser.SequenceContext ctx); - /** - * Visit a parse tree produced by {@link EqlBaseParser#join}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitJoin(EqlBaseParser.JoinContext ctx); - /** - * Visit a parse tree produced by {@link EqlBaseParser#pipe}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitPipe(EqlBaseParser.PipeContext ctx); - /** - * Visit a parse tree produced by {@link EqlBaseParser#joinKeys}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitJoinKeys(EqlBaseParser.JoinKeysContext ctx); - /** - * Visit a parse tree produced by {@link EqlBaseParser#joinTerm}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitJoinTerm(EqlBaseParser.JoinTermContext ctx); - /** - * Visit a parse tree produced by {@link EqlBaseParser#sequenceTerm}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitSequenceTerm(EqlBaseParser.SequenceTermContext ctx); - /** - * Visit a parse tree produced by {@link EqlBaseParser#subquery}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitSubquery(EqlBaseParser.SubqueryContext ctx); - /** - * Visit a parse tree produced by {@link EqlBaseParser#eventQuery}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitEventQuery(EqlBaseParser.EventQueryContext ctx); - /** - * Visit a parse tree produced by {@link EqlBaseParser#eventFilter}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitEventFilter(EqlBaseParser.EventFilterContext ctx); - /** - * Visit a parse tree produced by {@link EqlBaseParser#expression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitExpression(EqlBaseParser.ExpressionContext ctx); - /** - * Visit a parse tree produced by the {@code logicalNot} - * labeled alternative in {@link EqlBaseParser#booleanExpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitLogicalNot(EqlBaseParser.LogicalNotContext ctx); - /** - * Visit a parse tree produced by the {@code booleanDefault} - * labeled alternative in {@link EqlBaseParser#booleanExpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitBooleanDefault(EqlBaseParser.BooleanDefaultContext ctx); - /** - * Visit a parse tree produced by the {@code processCheck} - * labeled alternative in {@link EqlBaseParser#booleanExpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitProcessCheck(EqlBaseParser.ProcessCheckContext ctx); - /** - * Visit a parse tree produced by the {@code logicalBinary} - * labeled alternative in {@link EqlBaseParser#booleanExpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitLogicalBinary(EqlBaseParser.LogicalBinaryContext ctx); - /** - * Visit a parse tree produced by the {@code valueExpressionDefault} - * labeled alternative in {@link EqlBaseParser#valueExpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitValueExpressionDefault(EqlBaseParser.ValueExpressionDefaultContext ctx); - /** - * Visit a parse tree produced by the {@code comparison} - * labeled alternative in {@link EqlBaseParser#valueExpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitComparison(EqlBaseParser.ComparisonContext ctx); - /** - * Visit a parse tree produced by the {@code operatorExpressionDefault} - * labeled alternative in {@link EqlBaseParser#operatorExpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitOperatorExpressionDefault(EqlBaseParser.OperatorExpressionDefaultContext ctx); - /** - * Visit a parse tree produced by the {@code arithmeticBinary} - * labeled alternative in {@link EqlBaseParser#operatorExpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitArithmeticBinary(EqlBaseParser.ArithmeticBinaryContext ctx); - /** - * Visit a parse tree produced by the {@code arithmeticUnary} - * labeled alternative in {@link EqlBaseParser#operatorExpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitArithmeticUnary(EqlBaseParser.ArithmeticUnaryContext ctx); - /** - * Visit a parse tree produced by {@link EqlBaseParser#predicate}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitPredicate(EqlBaseParser.PredicateContext ctx); - /** - * Visit a parse tree produced by the {@code constantDefault} - * labeled alternative in {@link EqlBaseParser#primaryExpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitConstantDefault(EqlBaseParser.ConstantDefaultContext ctx); - /** - * Visit a parse tree produced by the {@code function} - * labeled alternative in {@link EqlBaseParser#primaryExpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitFunction(EqlBaseParser.FunctionContext ctx); - /** - * Visit a parse tree produced by the {@code dereference} - * labeled alternative in {@link EqlBaseParser#primaryExpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitDereference(EqlBaseParser.DereferenceContext ctx); - /** - * Visit a parse tree produced by the {@code parenthesizedExpression} - * labeled alternative in {@link EqlBaseParser#primaryExpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitParenthesizedExpression(EqlBaseParser.ParenthesizedExpressionContext ctx); - /** - * Visit a parse tree produced by {@link EqlBaseParser#functionExpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitFunctionExpression(EqlBaseParser.FunctionExpressionContext ctx); - /** - * Visit a parse tree produced by {@link EqlBaseParser#functionName}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitFunctionName(EqlBaseParser.FunctionNameContext ctx); - /** - * Visit a parse tree produced by the {@code nullLiteral} - * labeled alternative in {@link EqlBaseParser#constant}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitNullLiteral(EqlBaseParser.NullLiteralContext ctx); - /** - * Visit a parse tree produced by the {@code numericLiteral} - * labeled alternative in {@link EqlBaseParser#constant}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitNumericLiteral(EqlBaseParser.NumericLiteralContext ctx); - /** - * Visit a parse tree produced by the {@code booleanLiteral} - * labeled alternative in {@link EqlBaseParser#constant}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitBooleanLiteral(EqlBaseParser.BooleanLiteralContext ctx); - /** - * Visit a parse tree produced by the {@code stringLiteral} - * labeled alternative in {@link EqlBaseParser#constant}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitStringLiteral(EqlBaseParser.StringLiteralContext ctx); - /** - * Visit a parse tree produced by {@link EqlBaseParser#comparisonOperator}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitComparisonOperator(EqlBaseParser.ComparisonOperatorContext ctx); - /** - * Visit a parse tree produced by {@link EqlBaseParser#booleanValue}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitBooleanValue(EqlBaseParser.BooleanValueContext ctx); - /** - * Visit a parse tree produced by {@link EqlBaseParser#qualifiedName}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitQualifiedName(EqlBaseParser.QualifiedNameContext ctx); - /** - * Visit a parse tree produced by {@link EqlBaseParser#identifier}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitIdentifier(EqlBaseParser.IdentifierContext ctx); - /** - * Visit a parse tree produced by {@link EqlBaseParser#timeUnit}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitTimeUnit(EqlBaseParser.TimeUnitContext ctx); - /** - * Visit a parse tree produced by the {@code decimalLiteral} - * labeled alternative in {@link EqlBaseParser#number}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitDecimalLiteral(EqlBaseParser.DecimalLiteralContext ctx); - /** - * Visit a parse tree produced by the {@code integerLiteral} - * labeled alternative in {@link EqlBaseParser#number}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitIntegerLiteral(EqlBaseParser.IntegerLiteralContext ctx); - /** - * Visit a parse tree produced by {@link EqlBaseParser#string}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitString(EqlBaseParser.StringContext ctx); - /** - * Visit a parse tree produced by {@link EqlBaseParser#eventValue}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitEventValue(EqlBaseParser.EventValueContext ctx); + /** + * Visit a parse tree produced by {@link EqlBaseParser#singleStatement}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitSingleStatement(EqlBaseParser.SingleStatementContext ctx); + + /** + * Visit a parse tree produced by {@link EqlBaseParser#singleExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitSingleExpression(EqlBaseParser.SingleExpressionContext ctx); + + /** + * Visit a parse tree produced by {@link EqlBaseParser#statement}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitStatement(EqlBaseParser.StatementContext ctx); + + /** + * Visit a parse tree produced by {@link EqlBaseParser#query}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitQuery(EqlBaseParser.QueryContext ctx); + + /** + * Visit a parse tree produced by {@link EqlBaseParser#sequenceParams}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitSequenceParams(EqlBaseParser.SequenceParamsContext ctx); + + /** + * Visit a parse tree produced by {@link EqlBaseParser#sequence}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitSequence(EqlBaseParser.SequenceContext ctx); + + /** + * Visit a parse tree produced by {@link EqlBaseParser#join}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitJoin(EqlBaseParser.JoinContext ctx); + + /** + * Visit a parse tree produced by {@link EqlBaseParser#pipe}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitPipe(EqlBaseParser.PipeContext ctx); + + /** + * Visit a parse tree produced by {@link EqlBaseParser#joinKeys}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitJoinKeys(EqlBaseParser.JoinKeysContext ctx); + + /** + * Visit a parse tree produced by {@link EqlBaseParser#joinTerm}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitJoinTerm(EqlBaseParser.JoinTermContext ctx); + + /** + * Visit a parse tree produced by {@link EqlBaseParser#sequenceTerm}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitSequenceTerm(EqlBaseParser.SequenceTermContext ctx); + + /** + * Visit a parse tree produced by {@link EqlBaseParser#subquery}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitSubquery(EqlBaseParser.SubqueryContext ctx); + + /** + * Visit a parse tree produced by {@link EqlBaseParser#eventQuery}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitEventQuery(EqlBaseParser.EventQueryContext ctx); + + /** + * Visit a parse tree produced by {@link EqlBaseParser#eventFilter}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitEventFilter(EqlBaseParser.EventFilterContext ctx); + + /** + * Visit a parse tree produced by {@link EqlBaseParser#expression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitExpression(EqlBaseParser.ExpressionContext ctx); + + /** + * Visit a parse tree produced by the {@code logicalNot} + * labeled alternative in {@link EqlBaseParser#booleanExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitLogicalNot(EqlBaseParser.LogicalNotContext ctx); + + /** + * Visit a parse tree produced by the {@code booleanDefault} + * labeled alternative in {@link EqlBaseParser#booleanExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitBooleanDefault(EqlBaseParser.BooleanDefaultContext ctx); + + /** + * Visit a parse tree produced by the {@code processCheck} + * labeled alternative in {@link EqlBaseParser#booleanExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitProcessCheck(EqlBaseParser.ProcessCheckContext ctx); + + /** + * Visit a parse tree produced by the {@code logicalBinary} + * labeled alternative in {@link EqlBaseParser#booleanExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitLogicalBinary(EqlBaseParser.LogicalBinaryContext ctx); + + /** + * Visit a parse tree produced by the {@code valueExpressionDefault} + * labeled alternative in {@link EqlBaseParser#valueExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitValueExpressionDefault(EqlBaseParser.ValueExpressionDefaultContext ctx); + + /** + * Visit a parse tree produced by the {@code comparison} + * labeled alternative in {@link EqlBaseParser#valueExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitComparison(EqlBaseParser.ComparisonContext ctx); + + /** + * Visit a parse tree produced by the {@code operatorExpressionDefault} + * labeled alternative in {@link EqlBaseParser#operatorExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitOperatorExpressionDefault(EqlBaseParser.OperatorExpressionDefaultContext ctx); + + /** + * Visit a parse tree produced by the {@code arithmeticBinary} + * labeled alternative in {@link EqlBaseParser#operatorExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitArithmeticBinary(EqlBaseParser.ArithmeticBinaryContext ctx); + + /** + * Visit a parse tree produced by the {@code arithmeticUnary} + * labeled alternative in {@link EqlBaseParser#operatorExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitArithmeticUnary(EqlBaseParser.ArithmeticUnaryContext ctx); + + /** + * Visit a parse tree produced by {@link EqlBaseParser#predicate}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitPredicate(EqlBaseParser.PredicateContext ctx); + + /** + * Visit a parse tree produced by the {@code constantDefault} + * labeled alternative in {@link EqlBaseParser#primaryExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitConstantDefault(EqlBaseParser.ConstantDefaultContext ctx); + + /** + * Visit a parse tree produced by the {@code function} + * labeled alternative in {@link EqlBaseParser#primaryExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitFunction(EqlBaseParser.FunctionContext ctx); + + /** + * Visit a parse tree produced by the {@code dereference} + * labeled alternative in {@link EqlBaseParser#primaryExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitDereference(EqlBaseParser.DereferenceContext ctx); + + /** + * Visit a parse tree produced by the {@code parenthesizedExpression} + * labeled alternative in {@link EqlBaseParser#primaryExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitParenthesizedExpression(EqlBaseParser.ParenthesizedExpressionContext ctx); + + /** + * Visit a parse tree produced by {@link EqlBaseParser#functionExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitFunctionExpression(EqlBaseParser.FunctionExpressionContext ctx); + + /** + * Visit a parse tree produced by {@link EqlBaseParser#functionName}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitFunctionName(EqlBaseParser.FunctionNameContext ctx); + + /** + * Visit a parse tree produced by the {@code nullLiteral} + * labeled alternative in {@link EqlBaseParser#constant}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitNullLiteral(EqlBaseParser.NullLiteralContext ctx); + + /** + * Visit a parse tree produced by the {@code numericLiteral} + * labeled alternative in {@link EqlBaseParser#constant}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitNumericLiteral(EqlBaseParser.NumericLiteralContext ctx); + + /** + * Visit a parse tree produced by the {@code booleanLiteral} + * labeled alternative in {@link EqlBaseParser#constant}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitBooleanLiteral(EqlBaseParser.BooleanLiteralContext ctx); + + /** + * Visit a parse tree produced by the {@code stringLiteral} + * labeled alternative in {@link EqlBaseParser#constant}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitStringLiteral(EqlBaseParser.StringLiteralContext ctx); + + /** + * Visit a parse tree produced by {@link EqlBaseParser#comparisonOperator}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitComparisonOperator(EqlBaseParser.ComparisonOperatorContext ctx); + + /** + * Visit a parse tree produced by {@link EqlBaseParser#booleanValue}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitBooleanValue(EqlBaseParser.BooleanValueContext ctx); + + /** + * Visit a parse tree produced by {@link EqlBaseParser#qualifiedName}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitQualifiedName(EqlBaseParser.QualifiedNameContext ctx); + + /** + * Visit a parse tree produced by {@link EqlBaseParser#identifier}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitIdentifier(EqlBaseParser.IdentifierContext ctx); + + /** + * Visit a parse tree produced by {@link EqlBaseParser#timeUnit}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitTimeUnit(EqlBaseParser.TimeUnitContext ctx); + + /** + * Visit a parse tree produced by the {@code decimalLiteral} + * labeled alternative in {@link EqlBaseParser#number}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitDecimalLiteral(EqlBaseParser.DecimalLiteralContext ctx); + + /** + * Visit a parse tree produced by the {@code integerLiteral} + * labeled alternative in {@link EqlBaseParser#number}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitIntegerLiteral(EqlBaseParser.IntegerLiteralContext ctx); + + /** + * Visit a parse tree produced by {@link EqlBaseParser#string}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitString(EqlBaseParser.StringContext ctx); + + /** + * Visit a parse tree produced by {@link EqlBaseParser#eventValue}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitEventValue(EqlBaseParser.EventValueContext ctx); } diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/parser/EqlParser.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/parser/EqlParser.java index a893d1b3637ad..d544c0a729172 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/parser/EqlParser.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/parser/EqlParser.java @@ -65,9 +65,12 @@ public Expression createExpression(String expression, ParserParams params) { return invokeParser(expression, params, EqlBaseParser::singleExpression, AstBuilder::expression); } - private T invokeParser(String eql, ParserParams params, - Function parseFunction, - BiFunction visitor) { + private T invokeParser( + String eql, + ParserParams params, + Function parseFunction, + BiFunction visitor + ) { try { EqlBaseLexer lexer = new EqlBaseLexer(new ANTLRInputStream(eql)); @@ -91,9 +94,7 @@ private T invokeParser(String eql, ParserParams params, for (Token t : tokenStream.getTokens()) { String symbolicName = EqlBaseLexer.VOCABULARY.getSymbolicName(t.getType()); String literalName = EqlBaseLexer.VOCABULARY.getLiteralName(t.getType()); - log.info(format(Locale.ROOT, " %-15s '%s'", - symbolicName == null ? literalName : symbolicName, - t.getText())); + log.info(format(Locale.ROOT, " %-15s '%s'", symbolicName == null ? literalName : symbolicName, t.getText())); } } @@ -105,8 +106,10 @@ private T invokeParser(String eql, ParserParams params, return visitor.apply(new AstBuilder(params), tree); } catch (StackOverflowError e) { - throw new ParsingException("EQL statement is too large, " + - "causing stack overflow when generating the parsing tree: [{}]", eql); + throw new ParsingException( + "EQL statement is too large, " + "causing stack overflow when generating the parsing tree: [{}]", + eql + ); } } @@ -119,12 +122,24 @@ private static void debug(EqlBaseParser parser) { parser.addErrorListener(new DiagnosticErrorListener(false) { @Override - public void reportAttemptingFullContext(Parser recognizer, DFA dfa, - int startIndex, int stopIndex, BitSet conflictingAlts, ATNConfigSet configs) {} + public void reportAttemptingFullContext( + Parser recognizer, + DFA dfa, + int startIndex, + int stopIndex, + BitSet conflictingAlts, + ATNConfigSet configs + ) {} @Override - public void reportContextSensitivity(Parser recognizer, DFA dfa, - int startIndex, int stopIndex, int prediction, ATNConfigSet configs) {} + public void reportContextSensitivity( + Parser recognizer, + DFA dfa, + int startIndex, + int stopIndex, + int prediction, + ATNConfigSet configs + ) {} }); } @@ -138,11 +153,7 @@ private class PostProcessor extends EqlBaseBaseListener { @Override public void exitProcessCheck(EqlBaseParser.ProcessCheckContext context) { Token token = context.relationship; - throw new ParsingException( - "Process relationships are not supported", - null, - token.getLine(), - token.getCharPositionInLine()); + throw new ParsingException("Process relationships are not supported", null, token.getLine(), token.getCharPositionInLine()); } @Override @@ -153,15 +164,22 @@ public void exitQualifiedName(EqlBaseParser.QualifiedNameContext context) { "Array indexes are not supported", null, firstIndex.getLine(), - firstIndex.getCharPositionInLine()); + firstIndex.getCharPositionInLine() + ); } } } private static final BaseErrorListener ERROR_LISTENER = new BaseErrorListener() { @Override - public void syntaxError(Recognizer recognizer, Object offendingSymbol, int line, - int charPositionInLine, String message, RecognitionException e) { + public void syntaxError( + Recognizer recognizer, + Object offendingSymbol, + int line, + int charPositionInLine, + String message, + RecognitionException e + ) { throw new ParsingException(message, e, line, charPositionInLine); } }; diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/parser/ExpressionBuilder.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/parser/ExpressionBuilder.java index d0c6f23897080..421ee98791a79 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/parser/ExpressionBuilder.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/parser/ExpressionBuilder.java @@ -63,7 +63,6 @@ import static org.elasticsearch.xpack.ql.parser.ParserUtils.typedParsing; import static org.elasticsearch.xpack.ql.parser.ParserUtils.visitList; - public class ExpressionBuilder extends IdentifierBuilder { protected final ParserParams params; @@ -177,8 +176,15 @@ public Expression visitOperatorExpressionDefault(EqlBaseParser.OperatorExpressio return combineExpressions(predicate.constant(), c -> new InsensitiveWildcardEquals(source, expr, c, zoneId)); case EqlBaseParser.LIKE: case EqlBaseParser.LIKE_INSENSITIVE: - return combineExpressions(predicate.constant(), e -> new Like(source, expr, - toLikePattern(e.fold().toString()), predicate.kind.getType() == EqlBaseParser.LIKE_INSENSITIVE)); + return combineExpressions( + predicate.constant(), + e -> new Like( + source, + expr, + toLikePattern(e.fold().toString()), + predicate.kind.getType() == EqlBaseParser.LIKE_INSENSITIVE + ) + ); case EqlBaseParser.REGEX: case EqlBaseParser.REGEX_INSENSITIVE: return new Match( @@ -255,8 +261,7 @@ public Literal visitIntegerLiteral(EqlBaseParser.IntegerLiteralContext ctx) { // if it's too large, then quietly try to parse as a float instead try { return new Literal(source, Double.valueOf(StringUtils.parseDouble(text)), DataTypes.DOUBLE); - } catch (QlIllegalArgumentException ignored) { - } + } catch (QlIllegalArgumentException ignored) {} throw new ParsingException(source, siae.getMessage()); } diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/parser/LogicalPlanBuilder.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/parser/LogicalPlanBuilder.java index 4a7198ac7eea7..82d4ef96a3044 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/parser/LogicalPlanBuilder.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/parser/LogicalPlanBuilder.java @@ -65,8 +65,15 @@ public abstract class LogicalPlanBuilder extends ExpressionBuilder { static final String FILTER_PIPE = "filter", HEAD_PIPE = "head", TAIL_PIPE = "tail", RUNS = "runs"; - static final Set SUPPORTED_PIPES = Sets.newHashSet("count", FILTER_PIPE, HEAD_PIPE, "sort", TAIL_PIPE, "unique", - "unique_count"); + static final Set SUPPORTED_PIPES = Sets.newHashSet( + "count", + FILTER_PIPE, + HEAD_PIPE, + "sort", + TAIL_PIPE, + "unique", + "unique_count" + ); private final UnresolvedRelation RELATION = new UnresolvedRelation(synthetic(""), null, "", false, ""); private final EmptyAttribute UNSPECIFIED_FIELD = new EmptyAttribute(synthetic("")); @@ -80,8 +87,9 @@ private Attribute fieldTimestamp() { } private Attribute fieldTiebreaker() { - return params.fieldTiebreaker() != null ? - new UnresolvedAttribute(synthetic(""), params.fieldTiebreaker()) : UNSPECIFIED_FIELD; + return params.fieldTiebreaker() != null + ? new UnresolvedAttribute(synthetic(""), params.fieldTiebreaker()) + : UNSPECIFIED_FIELD; } private OrderDirection resultPosition() { @@ -192,8 +200,12 @@ public Join visitJoin(JoinContext ctx) { Source src = source(joinTermCtx.by != null ? joinTermCtx.by : joinTermCtx); int expected = numberOfKeys - parentJoinKeys.size(); int found = keySize - parentJoinKeys.size(); - throw new ParsingException(src, "Inconsistent number of join keys specified; expected [{}] but found [{}]", expected, - found); + throw new ParsingException( + src, + "Inconsistent number of join keys specified; expected [{}] but found [{}]", + expected, + found + ); } } queries.add(joinTerm); @@ -250,8 +262,12 @@ public Sequence visitSequence(SequenceContext ctx) { Source src = source(sequenceTermCtx.by != null ? sequenceTermCtx.by : sequenceTermCtx); int expected = numberOfKeys - parentJoinKeys.size(); int found = keySize - parentJoinKeys.size(); - throw new ParsingException(src, "Inconsistent number of join keys specified; expected [{}] but found [{}]", expected, - found); + throw new ParsingException( + src, + "Inconsistent number of join keys specified; expected [{}] but found [{}]", + expected, + found + ); } } // check runs @@ -327,8 +343,11 @@ public TimeValue visitSequenceParams(SequenceParamsContext ctx) { String timeString = text(ctx.timeUnit().IDENTIFIER()); if (timeString == null) { - throw new ParsingException(source(ctx.timeUnit()), "No time unit specified, did you mean [s] as in [{}s]?", text(ctx - .timeUnit())); + throw new ParsingException( + source(ctx.timeUnit()), + "No time unit specified, did you mean [s] as in [{}s]?", + text(ctx.timeUnit()) + ); } TimeUnit timeUnit = null; @@ -349,16 +368,22 @@ public TimeValue visitSequenceParams(SequenceParamsContext ctx) { timeUnit = TimeUnit.DAYS; break; default: - throw new ParsingException(source(ctx.timeUnit().IDENTIFIER()), - "Unrecognized time unit [{}] in [{}], please specify one of [ms, s, m, h, d]", - timeString, text(ctx.timeUnit())); + throw new ParsingException( + source(ctx.timeUnit().IDENTIFIER()), + "Unrecognized time unit [{}] in [{}], please specify one of [ms, s, m, h, d]", + timeString, + text(ctx.timeUnit()) + ); } return new TimeValue(value, timeUnit); } else { - throw new ParsingException(source(numberCtx), "Decimal time interval [{}] not supported; please use an positive integer", - text(numberCtx)); + throw new ParsingException( + source(numberCtx), + "Decimal time interval [{}] not supported; please use an positive integer", + text(numberCtx) + ); } } @@ -371,9 +396,7 @@ private LogicalPlan pipe(PipeContext ctx, LogicalPlan plan) { String msg = "Unrecognized pipe [{}]"; if (potentialMatches.isEmpty() == false) { String matchString = potentialMatches.toString(); - msg += ", did you mean " + (potentialMatches.size() == 1 - ? matchString - : "any of " + matchString) + "?"; + msg += ", did you mean " + (potentialMatches.size() == 1 ? matchString : "any of " + matchString) + "?"; } throw new ParsingException(source(ctx.IDENTIFIER()), msg, name); } @@ -405,8 +428,12 @@ private Expression pipeIntArgument(Source source, String pipeName, List queries, - KeyedFilter until, - Attribute timestamp, - Attribute tiebreaker, - OrderDirection direction) { + public Join( + Source source, + List queries, + KeyedFilter until, + Attribute timestamp, + Attribute tiebreaker, + OrderDirection direction + ) { super(source, CollectionUtils.combine(queries, until)); this.queries = queries; this.until = until; @@ -45,12 +47,14 @@ public Join(Source source, this.direction = direction; } - private Join(Source source, - List queries, - LogicalPlan until, - Attribute timestamp, - Attribute tiebreaker, - OrderDirection direction) { + private Join( + Source source, + List queries, + LogicalPlan until, + Attribute timestamp, + Attribute tiebreaker, + OrderDirection direction + ) { this(source, asKeyed(queries), asKeyed(until), timestamp, tiebreaker, direction); } @@ -141,10 +145,11 @@ public boolean equals(Object obj) { Join other = (Join) obj; - return Objects.equals(direction, other.direction) && Objects.equals(queries, other.queries) - && Objects.equals(until, other.until) - && Objects.equals(timestamp, other.timestamp) - && Objects.equals(tiebreaker, other.tiebreaker); + return Objects.equals(direction, other.direction) + && Objects.equals(queries, other.queries) + && Objects.equals(until, other.until) + && Objects.equals(timestamp, other.timestamp) + && Objects.equals(tiebreaker, other.tiebreaker); } @Override diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/logical/KeyedFilter.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/logical/KeyedFilter.java index 2ab922606e635..fd8b9895456a1 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/logical/KeyedFilter.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/logical/KeyedFilter.java @@ -93,8 +93,8 @@ public boolean equals(Object obj) { KeyedFilter other = (KeyedFilter) obj; return Objects.equals(keys, other.keys) - && Objects.equals(timestamp, other.timestamp) - && Objects.equals(tiebreaker, other.tiebreaker) - && Objects.equals(child(), other.child()); + && Objects.equals(timestamp, other.timestamp) + && Objects.equals(tiebreaker, other.tiebreaker) + && Objects.equals(child(), other.child()); } } diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/logical/Sequence.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/logical/Sequence.java index f6a086a674009..48aed6e87ee67 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/logical/Sequence.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/logical/Sequence.java @@ -23,24 +23,28 @@ public class Sequence extends Join { private final TimeValue maxSpan; - public Sequence(Source source, - List queries, - KeyedFilter until, - TimeValue maxSpan, - Attribute timestamp, - Attribute tiebreaker, - OrderDirection direction) { + public Sequence( + Source source, + List queries, + KeyedFilter until, + TimeValue maxSpan, + Attribute timestamp, + Attribute tiebreaker, + OrderDirection direction + ) { super(source, queries, until, timestamp, tiebreaker, direction); this.maxSpan = maxSpan; } - private Sequence(Source source, - List queries, - LogicalPlan until, - TimeValue maxSpan, - Attribute timestamp, - Attribute tiebreaker, - OrderDirection direction) { + private Sequence( + Source source, + List queries, + LogicalPlan until, + TimeValue maxSpan, + Attribute timestamp, + Attribute tiebreaker, + OrderDirection direction + ) { super(source, asKeyed(queries), asKeyed(until), timestamp, tiebreaker, direction); this.maxSpan = maxSpan; } @@ -53,8 +57,15 @@ protected NodeInfo info() { @Override public Join replaceChildren(List newChildren) { int lastIndex = newChildren.size() - 1; - return new Sequence(source(), newChildren.subList(0, lastIndex), newChildren.get(lastIndex), maxSpan, timestamp(), tiebreaker(), - direction()); + return new Sequence( + source(), + newChildren.subList(0, lastIndex), + newChildren.get(lastIndex), + maxSpan, + timestamp(), + tiebreaker(), + direction() + ); } public TimeValue maxSpan() { diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/physical/EsQueryExec.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/physical/EsQueryExec.java index cc62efa558a46..cedd4ec8d035c 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/physical/EsQueryExec.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/physical/EsQueryExec.java @@ -52,7 +52,6 @@ public List output() { return output; } - /* * {@param includeFetchFields} should be true for event queries and false for in progress sequence queries * Fetching fields during in progress sequence queries is unnecessary. @@ -60,8 +59,12 @@ public List output() { public SearchSourceBuilder source(EqlSession session, boolean includeFetchFields) { EqlConfiguration cfg = session.configuration(); // by default use the configuration size - return SourceGenerator.sourceBuilder(queryContainer, cfg.filter(), includeFetchFields ? cfg.fetchFields() : null, - cfg.runtimeMappings()); + return SourceGenerator.sourceBuilder( + queryContainer, + cfg.filter(), + includeFetchFields ? cfg.fetchFields() : null, + cfg.runtimeMappings() + ); } @Override @@ -99,8 +102,7 @@ public boolean equals(Object obj) { } EsQueryExec other = (EsQueryExec) obj; - return Objects.equals(queryContainer, other.queryContainer) - && Objects.equals(output, other.output); + return Objects.equals(queryContainer, other.queryContainer) && Objects.equals(output, other.output); } @Override diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/physical/FilterExec.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/physical/FilterExec.java index 54be2d4d3f13f..dd6cc84638f0b 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/physical/FilterExec.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/physical/FilterExec.java @@ -68,8 +68,6 @@ public boolean equals(Object obj) { } FilterExec other = (FilterExec) obj; - return onAggs == other.onAggs - && Objects.equals(condition, other.condition) - && Objects.equals(child(), other.child()); + return onAggs == other.onAggs && Objects.equals(condition, other.condition) && Objects.equals(child(), other.child()); } } diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/physical/LimitWithOffsetExec.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/physical/LimitWithOffsetExec.java index 8bccbd303d2d5..b57ee9213a152 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/physical/LimitWithOffsetExec.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/physical/LimitWithOffsetExec.java @@ -51,7 +51,6 @@ public boolean equals(Object obj) { } LimitWithOffsetExec other = (LimitWithOffsetExec) obj; - return Objects.equals(limit, other.limit) - && Objects.equals(child(), other.child()); + return Objects.equals(limit, other.limit) && Objects.equals(child(), other.child()); } } diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/physical/LocalRelation.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/physical/LocalRelation.java index fa73fad90ca04..fcbd90b04cb58 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/physical/LocalRelation.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/physical/LocalRelation.java @@ -87,7 +87,6 @@ public boolean equals(Object obj) { return Objects.equals(executable, other.executable); } - @Override public String nodeString() { return nodeName() + NodeUtils.limitedToString(output()); diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/physical/OrderExec.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/physical/OrderExec.java index d765513a6be31..efe7b8986a290 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/physical/OrderExec.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/physical/OrderExec.java @@ -52,7 +52,6 @@ public boolean equals(Object obj) { OrderExec other = (OrderExec) obj; - return Objects.equals(order, other.order) - && Objects.equals(child(), other.child()); + return Objects.equals(order, other.order) && Objects.equals(child(), other.child()); } } diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/physical/ProjectExec.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/physical/ProjectExec.java index c615615a61292..15fa220bd092d 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/physical/ProjectExec.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/physical/ProjectExec.java @@ -59,7 +59,6 @@ public boolean equals(Object obj) { ProjectExec other = (ProjectExec) obj; - return Objects.equals(projections, other.projections) - && Objects.equals(child(), other.child()); + return Objects.equals(projections, other.projections) && Objects.equals(child(), other.child()); } } diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/physical/SequenceExec.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/physical/SequenceExec.java index be0fe4fd2b54b..42e383d6a39c7 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/physical/SequenceExec.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/physical/SequenceExec.java @@ -36,26 +36,30 @@ public class SequenceExec extends PhysicalPlan { private final OrderDirection direction; private final TimeValue maxSpan; - public SequenceExec(Source source, - List> keys, - List matches, - List untilKeys, - PhysicalPlan until, - Attribute timestamp, - Attribute tiebreaker, - OrderDirection direction, - TimeValue maxSpan) { + public SequenceExec( + Source source, + List> keys, + List matches, + List untilKeys, + PhysicalPlan until, + Attribute timestamp, + Attribute tiebreaker, + OrderDirection direction, + TimeValue maxSpan + ) { this(source, combine(matches, until), combine(keys, singletonList(untilKeys)), timestamp, tiebreaker, null, direction, maxSpan); } - private SequenceExec(Source source, - List children, - List> keys, - Attribute ts, - Attribute tb, - Limit limit, - OrderDirection direction, - TimeValue maxSpan) { + private SequenceExec( + Source source, + List children, + List> keys, + Attribute ts, + Attribute tb, + Limit limit, + OrderDirection direction, + TimeValue maxSpan + ) { super(source, children); this.keys = keys; this.timestamp = ts; @@ -114,8 +118,7 @@ public SequenceExec with(Limit limit) { @Override public void execute(EqlSession session, ActionListener listener) { - new ExecutionManager(session) - .assemble(keys(), children(), timestamp(), tiebreaker(), direction, maxSpan, limit()) + new ExecutionManager(session).assemble(keys(), children(), timestamp(), tiebreaker(), direction, maxSpan, limit()) .execute(listener); } @@ -136,10 +139,10 @@ public boolean equals(Object obj) { SequenceExec other = (SequenceExec) obj; return Objects.equals(timestamp, other.timestamp) - && Objects.equals(tiebreaker, other.tiebreaker) - && Objects.equals(limit, other.limit) - && Objects.equals(direction, other.direction) - && Objects.equals(children(), other.children()) - && Objects.equals(keys, other.keys); + && Objects.equals(tiebreaker, other.tiebreaker) + && Objects.equals(limit, other.limit) + && Objects.equals(direction, other.direction) + && Objects.equals(children(), other.children()) + && Objects.equals(keys, other.keys); } } diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/physical/Unexecutable.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/physical/Unexecutable.java index 91f1e3f8155db..5dfbee5f8fb67 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/physical/Unexecutable.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/physical/Unexecutable.java @@ -12,7 +12,6 @@ import org.elasticsearch.xpack.eql.session.Executable; import org.elasticsearch.xpack.eql.session.Payload; - // this is mainly a marker interface to validate a plan before being executed public interface Unexecutable extends Executable { diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/planner/Mapper.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/planner/Mapper.java index 21304dc5538a2..de667f4d2e53a 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/planner/Mapper.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/planner/Mapper.java @@ -72,15 +72,17 @@ protected PhysicalPlan map(LogicalPlan p) { matches.add(map(keyed.child())); } - return new SequenceExec(p.source(), - keys, - matches, - Expressions.asAttributes(s.until().keys()), - map(s.until().child()), - s.timestamp(), - s.tiebreaker(), - s.direction(), - s.maxSpan()); + return new SequenceExec( + p.source(), + keys, + matches, + Expressions.asAttributes(s.until().keys()), + map(s.until().child()), + s.timestamp(), + s.tiebreaker(), + s.direction(), + s.maxSpan() + ); } if (p instanceof LocalRelation) { diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/planner/MultiValueAwareScriptQuery.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/planner/MultiValueAwareScriptQuery.java index d8b18b6ed14cd..650077b6ebf55 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/planner/MultiValueAwareScriptQuery.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/planner/MultiValueAwareScriptQuery.java @@ -21,5 +21,5 @@ class MultiValueAwareScriptQuery extends ScriptQuery { protected ScriptTemplate nullSafeScript(ScriptTemplate script) { return script; } - + } diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/planner/QueryFolder.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/planner/QueryFolder.java index 5bbd81608b124..b601188000de2 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/planner/QueryFolder.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/planner/QueryFolder.java @@ -40,20 +40,12 @@ PhysicalPlan fold(PhysicalPlan plan) { @Override protected Iterable.Batch> batches() { - Batch fold = new Batch("Fold queries", - new FoldProject(), - new FoldFilter(), - new FoldOrderBy(), - new FoldLimit() - ); - Batch finish = new Batch("Finish query", Limiter.ONCE, - new PlanOutputToQueryRef() - ); + Batch fold = new Batch("Fold queries", new FoldProject(), new FoldFilter(), new FoldOrderBy(), new FoldLimit()); + Batch finish = new Batch("Finish query", Limiter.ONCE, new PlanOutputToQueryRef()); return Arrays.asList(fold, finish); } - private static class FoldProject extends QueryFoldingRule { @Override diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/planner/QueryTranslator.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/planner/QueryTranslator.java index 606cc15d25a11..f2d1ff5998269 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/planner/QueryTranslator.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/planner/QueryTranslator.java @@ -78,9 +78,9 @@ public static Query toQuery(Expression e, TranslatorHandler handler) { if (translation instanceof ScriptQuery) { // check the operators and the expressions involved in these operations so that all can be used // in a doc-values multi-valued context - boolean multiValuedIncompatible = e.anyMatch(exp -> { - return false == (exp instanceof Literal || exp instanceof FieldAttribute || exp instanceof Function); - }); + boolean multiValuedIncompatible = e.anyMatch( + exp -> { return false == (exp instanceof Literal || exp instanceof FieldAttribute || exp instanceof Function); } + ); if (multiValuedIncompatible == false) { ScriptQuery query = (ScriptQuery) translation; return new MultiValueAwareScriptQuery(query.source(), Scripts.multiValueDocValuesRewrite(query.script())); @@ -105,10 +105,14 @@ public static Query doTranslate(InsensitiveBinaryComparison bc, TranslatorHandle } public static void checkInsensitiveComparison(InsensitiveBinaryComparison bc) { - Check.isTrue(bc.right().foldable(), + Check.isTrue( + bc.right().foldable(), "Line {}:{}: Comparisons against fields are not (currently) supported; offender [{}] in [{}]", - bc.right().sourceLocation().getLineNumber(), bc.right().sourceLocation().getColumnNumber(), - Expressions.name(bc.right()), bc.symbol()); + bc.right().sourceLocation().getLineNumber(), + bc.right().sourceLocation().getColumnNumber(), + Expressions.name(bc.right()), + bc.symbol() + ); } private static Query translate(InsensitiveBinaryComparison bc, TranslatorHandler handler) { diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plugin/EqlPlugin.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plugin/EqlPlugin.java index 08f48ec06bb46..6d553092fa740 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plugin/EqlPlugin.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plugin/EqlPlugin.java @@ -20,7 +20,6 @@ import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsFilter; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.env.Environment; import org.elasticsearch.env.NodeEnvironment; import org.elasticsearch.indices.breaker.BreakerSettings; @@ -35,6 +34,7 @@ import org.elasticsearch.script.ScriptService; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.watcher.ResourceWatcherService; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.XPackPlugin; import org.elasticsearch.xpack.core.action.XPackInfoFeatureAction; import org.elasticsearch.xpack.core.action.XPackUsageFeatureAction; @@ -45,6 +45,7 @@ import org.elasticsearch.xpack.ql.index.IndexResolver; import org.elasticsearch.xpack.ql.index.RemoteClusterResolver; import org.elasticsearch.xpack.ql.type.DefaultDataTypeRegistry; + import java.util.Collection; import java.util.Collections; import java.util.List; @@ -53,7 +54,7 @@ public class EqlPlugin extends Plugin implements ActionPlugin, CircuitBreakerPlugin { private static final String CIRCUIT_BREAKER_NAME = "eql_sequence"; - private static final long CIRCUIT_BREAKER_LIMIT = (long)((0.50) * JvmInfo.jvmInfo().getMem().getHeapMax().getBytes()); + private static final long CIRCUIT_BREAKER_LIMIT = (long) ((0.50) * JvmInfo.jvmInfo().getMem().getHeapMax().getBytes()); private static final double CIRCUIT_BREAKER_OVERHEAD = 1.0D; private final SetOnce circuitBreaker = new SetOnce<>(); @@ -64,21 +65,33 @@ public class EqlPlugin extends Plugin implements ActionPlugin, CircuitBreakerPlu Setting.Property.Deprecated ); - public EqlPlugin() { - } + public EqlPlugin() {} @Override - public Collection createComponents(Client client, ClusterService clusterService, ThreadPool threadPool, - ResourceWatcherService resourceWatcherService, ScriptService scriptService, NamedXContentRegistry xContentRegistry, - Environment environment, NodeEnvironment nodeEnvironment, NamedWriteableRegistry namedWriteableRegistry, - IndexNameExpressionResolver expressionResolver, Supplier repositoriesServiceSupplier) { + public Collection createComponents( + Client client, + ClusterService clusterService, + ThreadPool threadPool, + ResourceWatcherService resourceWatcherService, + ScriptService scriptService, + NamedXContentRegistry xContentRegistry, + Environment environment, + NodeEnvironment nodeEnvironment, + NamedWriteableRegistry namedWriteableRegistry, + IndexNameExpressionResolver expressionResolver, + Supplier repositoriesServiceSupplier + ) { return createComponents(client, environment.settings(), clusterService); } private Collection createComponents(Client client, Settings settings, ClusterService clusterService) { RemoteClusterResolver remoteClusterResolver = new RemoteClusterResolver(settings, clusterService.getClusterSettings()); - IndexResolver indexResolver = new IndexResolver(client, clusterService.getClusterName().value(), DefaultDataTypeRegistry.INSTANCE, - remoteClusterResolver::remoteClusters); + IndexResolver indexResolver = new IndexResolver( + client, + clusterService.getClusterName().value(), + DefaultDataTypeRegistry.INSTANCE, + remoteClusterResolver::remoteClusters + ); PlanExecutor planExecutor = new PlanExecutor(client, indexResolver, circuitBreaker.get()); return Collections.singletonList(planExecutor); } @@ -106,13 +119,15 @@ public List> getSettings() { } @Override - public List getRestHandlers(Settings settings, - RestController restController, - ClusterSettings clusterSettings, - IndexScopedSettings indexScopedSettings, - SettingsFilter settingsFilter, - IndexNameExpressionResolver indexNameExpressionResolver, - Supplier nodesInCluster) { + public List getRestHandlers( + Settings settings, + RestController restController, + ClusterSettings clusterSettings, + IndexScopedSettings indexScopedSettings, + SettingsFilter settingsFilter, + IndexNameExpressionResolver indexNameExpressionResolver, + Supplier nodesInCluster + ) { return List.of( new RestEqlSearchAction(), @@ -131,14 +146,15 @@ protected XPackLicenseState getLicenseState() { @Override public BreakerSettings getCircuitBreaker(Settings settings) { return BreakerSettings.updateFromSettings( - new BreakerSettings( - CIRCUIT_BREAKER_NAME, - CIRCUIT_BREAKER_LIMIT, - CIRCUIT_BREAKER_OVERHEAD, - CircuitBreaker.Type.MEMORY, - CircuitBreaker.Durability.TRANSIENT - ), - settings); + new BreakerSettings( + CIRCUIT_BREAKER_NAME, + CIRCUIT_BREAKER_LIMIT, + CIRCUIT_BREAKER_OVERHEAD, + CircuitBreaker.Type.MEMORY, + CircuitBreaker.Durability.TRANSIENT + ), + settings + ); } @Override diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plugin/RestEqlSearchAction.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plugin/RestEqlSearchAction.java index 73b812bb42e4a..05bb359fe5928 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plugin/RestEqlSearchAction.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plugin/RestEqlSearchAction.java @@ -12,15 +12,15 @@ import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.common.Strings; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.IndexNotFoundException; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestCancellableNodeClient; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.eql.action.EqlSearchAction; import org.elasticsearch.xpack.eql.action.EqlSearchRequest; import org.elasticsearch.xpack.eql.action.EqlSearchResponse; @@ -37,15 +37,11 @@ public class RestEqlSearchAction extends BaseRestHandler { @Override public List routes() { - return List.of( - new Route(GET, SEARCH_PATH), - new Route(POST, SEARCH_PATH) - ); + return List.of(new Route(GET, SEARCH_PATH), new Route(POST, SEARCH_PATH)); } @Override - protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) - throws IOException { + protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException { EqlSearchRequest eqlRequest; String indices; @@ -56,7 +52,8 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli eqlRequest.indicesOptions(IndicesOptions.fromRequest(request, eqlRequest.indicesOptions())); if (request.hasParam("wait_for_completion_timeout")) { eqlRequest.waitForCompletionTimeout( - request.paramAsTime("wait_for_completion_timeout", eqlRequest.waitForCompletionTimeout())); + request.paramAsTime("wait_for_completion_timeout", eqlRequest.waitForCompletionTimeout()) + ); } if (request.hasParam("keep_alive")) { eqlRequest.keepAlive(request.paramAsTime("keep_alive", eqlRequest.keepAlive())); diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plugin/TransportEqlAsyncGetResultsAction.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plugin/TransportEqlAsyncGetResultsAction.java index 9027586d96bc4..b0c1c35f59035 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plugin/TransportEqlAsyncGetResultsAction.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plugin/TransportEqlAsyncGetResultsAction.java @@ -23,15 +23,26 @@ public class TransportEqlAsyncGetResultsAction extends AbstractTransportQlAsyncGetResultsAction { @Inject - public TransportEqlAsyncGetResultsAction(TransportService transportService, - ActionFilters actionFilters, - ClusterService clusterService, - NamedWriteableRegistry registry, - Client client, - ThreadPool threadPool, - BigArrays bigArrays) { - super(EqlAsyncActionNames.EQL_ASYNC_GET_RESULT_ACTION_NAME, transportService, actionFilters, clusterService, registry, client, - threadPool, bigArrays, EqlSearchTask.class); + public TransportEqlAsyncGetResultsAction( + TransportService transportService, + ActionFilters actionFilters, + ClusterService clusterService, + NamedWriteableRegistry registry, + Client client, + ThreadPool threadPool, + BigArrays bigArrays + ) { + super( + EqlAsyncActionNames.EQL_ASYNC_GET_RESULT_ACTION_NAME, + transportService, + actionFilters, + clusterService, + registry, + client, + threadPool, + bigArrays, + EqlSearchTask.class + ); } @Override diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plugin/TransportEqlAsyncGetStatusAction.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plugin/TransportEqlAsyncGetStatusAction.java index 57b3a94a5f17e..72f9ff2594502 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plugin/TransportEqlAsyncGetStatusAction.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plugin/TransportEqlAsyncGetStatusAction.java @@ -19,18 +19,28 @@ import org.elasticsearch.xpack.eql.action.EqlSearchTask; import org.elasticsearch.xpack.ql.plugin.AbstractTransportQlAsyncGetStatusAction; - public class TransportEqlAsyncGetStatusAction extends AbstractTransportQlAsyncGetStatusAction { @Inject - public TransportEqlAsyncGetStatusAction(TransportService transportService, - ActionFilters actionFilters, - ClusterService clusterService, - NamedWriteableRegistry registry, - Client client, - ThreadPool threadPool, - BigArrays bigArrays) { - super(EqlAsyncGetStatusAction.NAME, transportService, actionFilters, clusterService, registry, client, threadPool, bigArrays, - EqlSearchTask.class); + public TransportEqlAsyncGetStatusAction( + TransportService transportService, + ActionFilters actionFilters, + ClusterService clusterService, + NamedWriteableRegistry registry, + Client client, + ThreadPool threadPool, + BigArrays bigArrays + ) { + super( + EqlAsyncGetStatusAction.NAME, + transportService, + actionFilters, + clusterService, + registry, + client, + threadPool, + bigArrays, + EqlSearchTask.class + ); } @Override diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plugin/TransportEqlSearchAction.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plugin/TransportEqlSearchAction.java index 846174e9c56ce..d0f789ef41469 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plugin/TransportEqlSearchAction.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plugin/TransportEqlSearchAction.java @@ -60,7 +60,8 @@ import static org.elasticsearch.xpack.ql.plugin.TransportActionUtils.executeRequestWithRetryAttempt; public class TransportEqlSearchAction extends HandledTransportAction - implements AsyncTaskManagementService.AsyncOperation { + implements + AsyncTaskManagementService.AsyncOperation { private static final Logger log = LogManager.getLogger(TransportEqlSearchAction.class); private final SecurityContext securityContext; @@ -71,27 +72,64 @@ public class TransportEqlSearchAction extends HandledTransportAction asyncTaskManagementService; @Inject - public TransportEqlSearchAction(Settings settings, ClusterService clusterService, TransportService transportService, - ThreadPool threadPool, ActionFilters actionFilters, PlanExecutor planExecutor, - NamedWriteableRegistry registry, Client client, BigArrays bigArrays) { + public TransportEqlSearchAction( + Settings settings, + ClusterService clusterService, + TransportService transportService, + ThreadPool threadPool, + ActionFilters actionFilters, + PlanExecutor planExecutor, + NamedWriteableRegistry registry, + Client client, + BigArrays bigArrays + ) { super(EqlSearchAction.NAME, transportService, actionFilters, EqlSearchRequest::new); - this.securityContext = XPackSettings.SECURITY_ENABLED.get(settings) ? - new SecurityContext(settings, threadPool.getThreadContext()) : null; + this.securityContext = XPackSettings.SECURITY_ENABLED.get(settings) + ? new SecurityContext(settings, threadPool.getThreadContext()) + : null; this.clusterService = clusterService; this.planExecutor = planExecutor; this.threadPool = threadPool; this.transportService = transportService; - this.asyncTaskManagementService = new AsyncTaskManagementService<>(XPackPlugin.ASYNC_RESULTS_INDEX, client, ASYNC_SEARCH_ORIGIN, - registry, taskManager, EqlSearchAction.INSTANCE.name(), this, EqlSearchTask.class, clusterService, threadPool, bigArrays); + this.asyncTaskManagementService = new AsyncTaskManagementService<>( + XPackPlugin.ASYNC_RESULTS_INDEX, + client, + ASYNC_SEARCH_ORIGIN, + registry, + taskManager, + EqlSearchAction.INSTANCE.name(), + this, + EqlSearchTask.class, + clusterService, + threadPool, + bigArrays + ); } @Override - public EqlSearchTask createTask(EqlSearchRequest request, long id, String type, String action, TaskId parentTaskId, - Map headers, Map originHeaders, AsyncExecutionId asyncExecutionId) { - return new EqlSearchTask(id, type, action, request.getDescription(), parentTaskId, headers, originHeaders, asyncExecutionId, - request.keepAlive()); + public EqlSearchTask createTask( + EqlSearchRequest request, + long id, + String type, + String action, + TaskId parentTaskId, + Map headers, + Map originHeaders, + AsyncExecutionId asyncExecutionId + ) { + return new EqlSearchTask( + id, + type, + action, + request.getDescription(), + parentTaskId, + headers, + originHeaders, + asyncExecutionId, + request.keepAlive() + ); } @Override @@ -101,8 +139,14 @@ public void execute(EqlSearchRequest request, EqlSearchTask task, ActionListener @Override public EqlSearchResponse initialResponse(EqlSearchTask task) { - return new EqlSearchResponse(EqlSearchResponse.Hits.EMPTY, - TimeValue.nsecToMSec(System.nanoTime() - task.getStartTimeNanos()), false, task.getExecutionId().getEncoded(), true, true); + return new EqlSearchResponse( + EqlSearchResponse.Hits.EMPTY, + TimeValue.nsecToMSec(System.nanoTime() - task.getStartTimeNanos()), + false, + task.getExecutionId().getEncoded(), + true, + true + ); } @Override @@ -113,16 +157,27 @@ public EqlSearchResponse readResponse(StreamInput inputStream) throws IOExceptio @Override protected void doExecute(Task task, EqlSearchRequest request, ActionListener listener) { if (requestIsAsync(request)) { - asyncTaskManagementService.asyncExecute(request, request.waitForCompletionTimeout(), request.keepAlive(), - request.keepOnCompletion(), listener); + asyncTaskManagementService.asyncExecute( + request, + request.waitForCompletionTimeout(), + request.keepAlive(), + request.keepOnCompletion(), + listener + ); } else { operation(planExecutor, (EqlSearchTask) task, request, username(securityContext), transportService, clusterService, listener); } } - public static void operation(PlanExecutor planExecutor, EqlSearchTask task, EqlSearchRequest request, String username, - TransportService transportService, ClusterService clusterService, - ActionListener listener) { + public static void operation( + PlanExecutor planExecutor, + EqlSearchTask task, + EqlSearchRequest request, + String username, + TransportService transportService, + ClusterService clusterService, + ActionListener listener + ) { String nodeId = clusterService.localNode().getId(); String clusterName = clusterName(clusterService); // TODO: these should be sent by the client @@ -132,8 +187,10 @@ public static void operation(PlanExecutor planExecutor, EqlSearchTask task, EqlS TimeValue timeout = TimeValue.timeValueSeconds(30); String clientId = null; - RemoteClusterRegistry remoteClusterRegistry = new RemoteClusterRegistry(transportService.getRemoteClusterService(), - request.indicesOptions()); + RemoteClusterRegistry remoteClusterRegistry = new RemoteClusterRegistry( + transportService.getRemoteClusterService(), + request.indicesOptions() + ); Set clusterAliases = remoteClusterRegistry.clusterAliases(request.indices(), false); if (canMinimizeRountrips(request, clusterAliases)) { String clusterAlias = clusterAliases.iterator().next(); @@ -141,29 +198,60 @@ public static void operation(PlanExecutor planExecutor, EqlSearchTask task, EqlS for (int i = 0; i < request.indices().length; i++) { remoteIndices[i] = request.indices()[i].substring(clusterAlias.length() + 1); // strip cluster plus `:` delimiter } - transportService.sendRequest(transportService.getRemoteClusterService().getConnection(clusterAlias), - EqlSearchAction.INSTANCE.name(), request.indices(remoteIndices), TransportRequestOptions.EMPTY, - new ActionListenerResponseHandler<>(wrap(r -> listener.onResponse(qualifyHits(r, clusterAlias)), - e -> listener.onFailure(qualifyException(e, remoteIndices, clusterAlias))), - EqlSearchAction.INSTANCE.getResponseReader())); + transportService.sendRequest( + transportService.getRemoteClusterService().getConnection(clusterAlias), + EqlSearchAction.INSTANCE.name(), + request.indices(remoteIndices), + TransportRequestOptions.EMPTY, + new ActionListenerResponseHandler<>( + wrap( + r -> listener.onResponse(qualifyHits(r, clusterAlias)), + e -> listener.onFailure(qualifyException(e, remoteIndices, clusterAlias)) + ), + EqlSearchAction.INSTANCE.getResponseReader() + ) + ); } else { - ParserParams params = new ParserParams(zoneId) - .fieldEventCategory(request.eventCategoryField()) + ParserParams params = new ParserParams(zoneId).fieldEventCategory(request.eventCategoryField()) .fieldTimestamp(request.timestampField()) .fieldTiebreaker(request.tiebreakerField()) .resultPosition("tail".equals(request.resultPosition()) ? Order.OrderDirection.DESC : Order.OrderDirection.ASC) .size(request.size()) .fetchSize(request.fetchSize()); - EqlConfiguration cfg = new EqlConfiguration(request.indices(), zoneId, username, clusterName, filter, - request.runtimeMappings(), fetchFields, timeout, request.indicesOptions(), request.fetchSize(), - clientId, new TaskId(nodeId, task.getId()), task, remoteClusterRegistry::versionIncompatibleClusters); - executeRequestWithRetryAttempt(clusterService, listener::onFailure, - onFailure -> planExecutor.eql(cfg, request.query(), params, - wrap(r -> listener.onResponse(createResponse(r, task.getExecutionId())), onFailure)), - node -> transportService.sendRequest(node, EqlSearchAction.NAME, request, - new ActionListenerResponseHandler<>(listener, EqlSearchResponse::new, ThreadPool.Names.SAME)), - log); + EqlConfiguration cfg = new EqlConfiguration( + request.indices(), + zoneId, + username, + clusterName, + filter, + request.runtimeMappings(), + fetchFields, + timeout, + request.indicesOptions(), + request.fetchSize(), + clientId, + new TaskId(nodeId, task.getId()), + task, + remoteClusterRegistry::versionIncompatibleClusters + ); + executeRequestWithRetryAttempt( + clusterService, + listener::onFailure, + onFailure -> planExecutor.eql( + cfg, + request.query(), + params, + wrap(r -> listener.onResponse(createResponse(r, task.getExecutionId())), onFailure) + ), + node -> transportService.sendRequest( + node, + EqlSearchAction.NAME, + request, + new ActionListenerResponseHandler<>(listener, EqlSearchResponse::new, ThreadPool.Names.SAME) + ), + log + ); } } @@ -244,7 +332,7 @@ private static Exception qualifyException(Exception e, String[] indices, String } private static String[] notFoundIndices(String exceptionIndexName, String[] indices) { - final String[] EXCEPTION_PREFIXES = new String[] {"Unknown index [", "["}; + final String[] EXCEPTION_PREFIXES = new String[] { "Unknown index [", "[" }; for (String prefix : EXCEPTION_PREFIXES) { if (exceptionIndexName.startsWith(prefix) && exceptionIndexName.endsWith("]")) { String indexList = exceptionIndexName.substring(prefix.length(), exceptionIndexName.length() - 1); diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plugin/TransportEqlStatsAction.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plugin/TransportEqlStatsAction.java index c281bf3f2dfc6..cbfedb2e096f4 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plugin/TransportEqlStatsAction.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plugin/TransportEqlStatsAction.java @@ -24,24 +24,43 @@ /** * Performs the stats operation. */ -public class TransportEqlStatsAction extends TransportNodesAction { +public class TransportEqlStatsAction extends TransportNodesAction< + EqlStatsRequest, + EqlStatsResponse, + EqlStatsRequest.NodeStatsRequest, + EqlStatsResponse.NodeStatsResponse> { // the plan executor holds the metrics private final PlanExecutor planExecutor; @Inject - public TransportEqlStatsAction(TransportService transportService, ClusterService clusterService, - ThreadPool threadPool, ActionFilters actionFilters, PlanExecutor planExecutor) { - super(EqlStatsAction.NAME, threadPool, clusterService, transportService, actionFilters, - EqlStatsRequest::new, EqlStatsRequest.NodeStatsRequest::new, ThreadPool.Names.MANAGEMENT, - EqlStatsResponse.NodeStatsResponse.class); + public TransportEqlStatsAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + PlanExecutor planExecutor + ) { + super( + EqlStatsAction.NAME, + threadPool, + clusterService, + transportService, + actionFilters, + EqlStatsRequest::new, + EqlStatsRequest.NodeStatsRequest::new, + ThreadPool.Names.MANAGEMENT, + EqlStatsResponse.NodeStatsResponse.class + ); this.planExecutor = planExecutor; } @Override - protected EqlStatsResponse newResponse(EqlStatsRequest request, List nodes, - List failures) { + protected EqlStatsResponse newResponse( + EqlStatsRequest request, + List nodes, + List failures + ) { return new EqlStatsResponse(clusterService.getClusterName(), nodes, failures); } diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/querydsl/container/ComputedRef.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/querydsl/container/ComputedRef.java index 6d370d0d37d27..ae01a4bc6919a 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/querydsl/container/ComputedRef.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/querydsl/container/ComputedRef.java @@ -38,4 +38,3 @@ public String toString() { return processor + "(" + processor + ")"; } } - diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/querydsl/container/QueryContainer.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/querydsl/container/QueryContainer.java index 13a3f26d87cdb..93ad7dd9264ca 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/querydsl/container/QueryContainer.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/querydsl/container/QueryContainer.java @@ -54,13 +54,15 @@ public QueryContainer() { this(null, emptyList(), AttributeMap.emptyAttributeMap(), emptyMap(), false, false, null); } - private QueryContainer(Query query, - List> fields, - AttributeMap attributes, - Map sort, - boolean trackHits, - boolean includeFrozen, - Limit limit) { + private QueryContainer( + Query query, + List> fields, + AttributeMap attributes, + Map sort, + boolean trackHits, + boolean includeFrozen, + Limit limit + ) { this.query = query; this.fields = fields; this.sort = sort; @@ -163,11 +165,11 @@ public boolean equals(Object obj) { QueryContainer other = (QueryContainer) obj; return Objects.equals(query, other.query) - && Objects.equals(attributes, other.attributes) - && Objects.equals(fields, other.fields) - && trackHits == other.trackHits - && includeFrozen == other.includeFrozen - && Objects.equals(limit, other.limit); + && Objects.equals(attributes, other.attributes) + && Objects.equals(fields, other.fields) + && trackHits == other.trackHits + && includeFrozen == other.includeFrozen + && Objects.equals(limit, other.limit); } @Override diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/session/EmptyExecutable.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/session/EmptyExecutable.java index 62634c9ea0b8d..2d9b43bb1b7b6 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/session/EmptyExecutable.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/session/EmptyExecutable.java @@ -49,8 +49,7 @@ public boolean equals(Object obj) { } EmptyExecutable other = (EmptyExecutable) obj; - return Objects.equals(resultType, other.resultType) - && Objects.equals(output, other.output); + return Objects.equals(resultType, other.resultType) && Objects.equals(output, other.output); } @Override diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/session/EqlConfiguration.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/session/EqlConfiguration.java index 82120a158e73e..30f89bf050372 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/session/EqlConfiguration.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/session/EqlConfiguration.java @@ -39,10 +39,22 @@ public class EqlConfiguration extends org.elasticsearch.xpack.ql.session.Configu @Nullable private Map runtimeMappings; - public EqlConfiguration(String[] indices, ZoneId zi, String username, String clusterName, QueryBuilder filter, - Map runtimeMappings, List fetchFields, TimeValue requestTimeout, - IndicesOptions indicesOptions, int fetchSize, String clientId, TaskId taskId, EqlSearchTask task, - Function> versionIncompatibleClusters) { + public EqlConfiguration( + String[] indices, + ZoneId zi, + String username, + String clusterName, + QueryBuilder filter, + Map runtimeMappings, + List fetchFields, + TimeValue requestTimeout, + IndicesOptions indicesOptions, + int fetchSize, + String clientId, + TaskId taskId, + EqlSearchTask task, + Function> versionIncompatibleClusters + ) { super(zi, username, clusterName, versionIncompatibleClusters); this.indices = indices; diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/session/EqlSession.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/session/EqlSession.java index 6078d4a488584..5d6ef65c0940d 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/session/EqlSession.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/session/EqlSession.java @@ -41,9 +41,18 @@ public class EqlSession { private final Planner planner; private final CircuitBreaker circuitBreaker; - public EqlSession(Client client, EqlConfiguration cfg, IndexResolver indexResolver, PreAnalyzer preAnalyzer, PostAnalyzer postAnalyzer, - FunctionRegistry functionRegistry, Verifier verifier, Optimizer optimizer, Planner planner, - CircuitBreaker circuitBreaker) { + public EqlSession( + Client client, + EqlConfiguration cfg, + IndexResolver indexResolver, + PreAnalyzer preAnalyzer, + PostAnalyzer postAnalyzer, + FunctionRegistry functionRegistry, + Verifier verifier, + Optimizer optimizer, + Planner planner, + CircuitBreaker circuitBreaker + ) { this.client = new ParentTaskAssigningClient(client, cfg.getTaskId()); this.configuration = cfg; @@ -103,11 +112,14 @@ public void analyzedPlan(LogicalPlan parsed, ActionListener listene private void preAnalyze(LogicalPlan parsed, ActionListener listener) { String indexWildcard = configuration.indexAsWildcard(); - if(configuration.isCancelled()){ + if (configuration.isCancelled()) { listener.onFailure(new TaskCancelledException("cancelled")); return; } - indexResolver.resolveAsMergedMapping(indexWildcard, configuration.indicesOptions(), configuration.runtimeMappings(), + indexResolver.resolveAsMergedMapping( + indexWildcard, + configuration.indicesOptions(), + configuration.runtimeMappings(), map(listener, r -> preAnalyzer.preAnalyze(parsed, r)) ); } diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/stats/Metrics.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/stats/Metrics.java index 94570bfa41509..4789f665b5bac 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/stats/Metrics.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/stats/Metrics.java @@ -22,7 +22,8 @@ public class Metrics { private enum OperationType { - FAILED, TOTAL; + FAILED, + TOTAL; @Override public String toString() { @@ -41,7 +42,7 @@ public Metrics() { for (QueryMetric metric : QueryMetric.values()) { Map metricsMap = new LinkedHashMap<>(OperationType.values().length); for (OperationType type : OperationType.values()) { - metricsMap.put(type, new CounterMetric()); + metricsMap.put(type, new CounterMetric()); } qMap.put(metric, Collections.unmodifiableMap(metricsMap)); @@ -50,7 +51,7 @@ public Metrics() { Map fMap = new LinkedHashMap<>(FeatureMetric.values().length); for (FeatureMetric featureMetric : FeatureMetric.values()) { - fMap.put(featureMetric, new CounterMetric()); + fMap.put(featureMetric, new CounterMetric()); } featuresMetrics = Collections.unmodifiableMap(fMap); } diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/util/RemoteClusterRegistry.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/util/RemoteClusterRegistry.java index 8d783746009d7..9bbab111e3f4e 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/util/RemoteClusterRegistry.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/util/RemoteClusterRegistry.java @@ -28,7 +28,7 @@ public RemoteClusterRegistry(RemoteClusterService remoteClusterService, IndicesO public Set versionIncompatibleClusters(String indexPattern) { Set incompatibleClusters = new TreeSet<>(); - for (String clusterAlias: clusterAliases(Strings.splitStringByCommaToArray(indexPattern), true)) { + for (String clusterAlias : clusterAliases(Strings.splitStringByCommaToArray(indexPattern), true)) { Version clusterVersion = remoteClusterService.getConnection(clusterAlias).getVersion(); if (clusterVersion.equals(Version.CURRENT) == false) { // TODO: should newer clusters be eventually allowed? incompatibleClusters.add(clusterAlias); diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/util/StringUtils.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/util/StringUtils.java index b1a5211bc8343..0f27a4989147a 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/util/StringUtils.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/util/StringUtils.java @@ -25,10 +25,7 @@ public static LikePattern toLikePattern(String s) { String escapeString = Character.toString(escape); // replace wildcards with % and escape special characters - String likeString = s.replace("%", escapeString + "%") - .replace("_", escapeString + "_") - .replace("*", "%") - .replace("?", "_"); + String likeString = s.replace("%", escapeString + "%").replace("_", escapeString + "_").replace("*", "%").replace("?", "_"); return new LikePattern(likeString, escape); } diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/AbstractBWCSerializationTestCase.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/AbstractBWCSerializationTestCase.java index cf863eaf3d778..f69918dac97f2 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/AbstractBWCSerializationTestCase.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/AbstractBWCSerializationTestCase.java @@ -8,8 +8,8 @@ import org.elasticsearch.Version; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.ToXContent; import java.io.IOException; import java.util.Collections; @@ -25,8 +25,9 @@ public abstract class AbstractBWCSerializationTestCase ALL_VERSIONS = Collections.unmodifiableList(getDeclaredVersions(Version.class)); private static List getAllBWCVersions(Version version) { - return ALL_VERSIONS.stream().filter(v -> v.onOrAfter(EQL_GA_VERSION) && v.before(version) && version.isCompatible(v)).collect( - Collectors.toList()); + return ALL_VERSIONS.stream() + .filter(v -> v.onOrAfter(EQL_GA_VERSION) && v.before(version) && version.isCompatible(v)) + .collect(Collectors.toList()); } private static final List DEFAULT_BWC_VERSIONS = getAllBWCVersions(Version.CURRENT); diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/AbstractBWCWireSerializingTestCase.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/AbstractBWCWireSerializingTestCase.java index 615ace9abfe0c..3a5a4b3357593 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/AbstractBWCWireSerializingTestCase.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/AbstractBWCWireSerializingTestCase.java @@ -24,8 +24,9 @@ public abstract class AbstractBWCWireSerializingTestCase ex private static final List ALL_VERSIONS = Collections.unmodifiableList(getDeclaredVersions(Version.class)); private static List getAllBWCVersions(Version version) { - return ALL_VERSIONS.stream().filter(v -> v.onOrAfter(EQL_GA_VERSION) && v.before(version) && version.isCompatible(v)).collect( - Collectors.toList()); + return ALL_VERSIONS.stream() + .filter(v -> v.onOrAfter(EQL_GA_VERSION) && v.before(version) && version.isCompatible(v)) + .collect(Collectors.toList()); } private static final List DEFAULT_BWC_VERSIONS = getAllBWCVersions(Version.CURRENT); diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/EqlInfoTransportActionTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/EqlInfoTransportActionTests.java index 07726f9b11b73..3fc411d0105ea 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/EqlInfoTransportActionTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/EqlInfoTransportActionTests.java @@ -16,11 +16,11 @@ import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.ObjectPath; import org.elasticsearch.tasks.Task; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.ObjectPath; import org.elasticsearch.xpack.core.action.XPackUsageFeatureResponse; import org.elasticsearch.xpack.core.eql.EqlFeatureSetUsage; import org.elasticsearch.xpack.core.watcher.common.stats.Counters; @@ -54,22 +54,19 @@ public void init() throws Exception { } public void testAvailable() { - EqlInfoTransportAction featureSet = new EqlInfoTransportAction( - mock(TransportService.class), mock(ActionFilters.class)); + EqlInfoTransportAction featureSet = new EqlInfoTransportAction(mock(TransportService.class), mock(ActionFilters.class)); assertThat(featureSet.available(), is(true)); } public void testEnabled() { - EqlInfoTransportAction featureSet = new EqlInfoTransportAction( - mock(TransportService.class), mock(ActionFilters.class)); + EqlInfoTransportAction featureSet = new EqlInfoTransportAction(mock(TransportService.class), mock(ActionFilters.class)); assertThat(featureSet.enabled(), is(true)); } @SuppressWarnings("unchecked") public void testUsageStats() throws Exception { doAnswer(mock -> { - ActionListener listener = - (ActionListener) mock.getArguments()[2]; + ActionListener listener = (ActionListener) mock.getArguments()[2]; List nodes = new ArrayList<>(); DiscoveryNode first = new DiscoveryNode("first", buildNewFakeTransportAddress(), Version.CURRENT); @@ -96,8 +93,14 @@ public void testUsageStats() throws Exception { when(mockNode.getId()).thenReturn("mocknode"); when(clusterService.localNode()).thenReturn(mockNode); - var usageAction = new EqlUsageTransportAction(mock(TransportService.class), clusterService, null, - mock(ActionFilters.class), null, client); + var usageAction = new EqlUsageTransportAction( + mock(TransportService.class), + clusterService, + null, + mock(ActionFilters.class), + null, + client + ); PlainActionFuture future = new PlainActionFuture<>(); usageAction.masterOperation(mock(Task.class), null, null, future); EqlFeatureSetUsage eqlUsage = (EqlFeatureSetUsage) future.get().getUsage(); diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/EqlTestUtils.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/EqlTestUtils.java index ba7656ab4b9b5..030fb4612a1ab 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/EqlTestUtils.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/EqlTestUtils.java @@ -38,15 +38,28 @@ public final class EqlTestUtils { public static final Version EQL_GA_VERSION = Version.V_7_10_0; - private EqlTestUtils() { - } + private EqlTestUtils() {} - public static final EqlConfiguration TEST_CFG = new EqlConfiguration(new String[] {"none"}, - org.elasticsearch.xpack.ql.util.DateUtils.UTC, "nobody", "cluster", null, emptyMap(), null, - TimeValue.timeValueSeconds(30), null, 123, "", new TaskId("test", 123), null, x -> Collections.emptySet()); + public static final EqlConfiguration TEST_CFG = new EqlConfiguration( + new String[] { "none" }, + org.elasticsearch.xpack.ql.util.DateUtils.UTC, + "nobody", + "cluster", + null, + emptyMap(), + null, + TimeValue.timeValueSeconds(30), + null, + 123, + "", + new TaskId("test", 123), + null, + x -> Collections.emptySet() + ); public static EqlConfiguration randomConfiguration() { - return new EqlConfiguration(new String[]{randomAlphaOfLength(16)}, + return new EqlConfiguration( + new String[] { randomAlphaOfLength(16) }, randomZone(), randomAlphaOfLength(16), randomAlphaOfLength(16), @@ -59,12 +72,22 @@ public static EqlConfiguration randomConfiguration() { randomAlphaOfLength(16), new TaskId(randomAlphaOfLength(10), randomNonNegativeLong()), randomTask(), - x -> Collections.emptySet()); + x -> Collections.emptySet() + ); } public static EqlSearchTask randomTask() { - return new EqlSearchTask(randomLong(), "transport", EqlSearchAction.NAME, "", null, emptyMap(), emptyMap(), - new AsyncExecutionId("", new TaskId(randomAlphaOfLength(10), 1)), TimeValue.timeValueDays(5)); + return new EqlSearchTask( + randomLong(), + "transport", + EqlSearchAction.NAME, + "", + null, + emptyMap(), + emptyMap(), + new AsyncExecutionId("", new TaskId(randomAlphaOfLength(10), 1)), + TimeValue.timeValueDays(5) + ); } public static InsensitiveEquals seq(Expression left, Expression right) { @@ -76,8 +99,17 @@ public static InsensitiveNotEquals sneq(Expression left, Expression right) { } public static IndicesOptions randomIndicesOptions() { - return IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean(), - randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean()); + return IndicesOptions.fromOptions( + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean() + ); } public static SearchSortValues randomSearchSortValues(Object[] values) { diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/StringUtilsTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/StringUtilsTests.java index f7c8bfe90a180..b821634ab2cac 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/StringUtilsTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/StringUtilsTests.java @@ -27,8 +27,10 @@ public void testLikePatternLikeChars() throws Exception { String escape = Character.toString(1); LikePattern pattern = toLikePattern(string); assertEquals(string, pattern.exactMatch()); - assertEquals("a" + escape + "%bc" + escape + "%" + escape + "%" + - "12" + escape + "_" + "3" + escape + "_" + escape + "_", pattern.pattern()); + assertEquals( + "a" + escape + "%bc" + escape + "%" + escape + "%" + "12" + escape + "_" + "3" + escape + "_" + escape + "_", + pattern.pattern() + ); assertEquals(string, pattern.asLuceneWildcard()); } diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/action/EqlRequestParserTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/action/EqlRequestParserTests.java index 9b202a9dde866..3530283b7d722 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/action/EqlRequestParserTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/action/EqlRequestParserTests.java @@ -8,13 +8,13 @@ package org.elasticsearch.xpack.eql.action; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.index.query.MatchQueryBuilder; +import org.elasticsearch.search.SearchModule; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.index.query.MatchQueryBuilder; -import org.elasticsearch.search.SearchModule; -import org.elasticsearch.test.ESTestCase; import java.io.IOException; import java.util.List; @@ -25,35 +25,55 @@ public class EqlRequestParserTests extends ESTestCase { - private static final NamedXContentRegistry REGISTRY = - new NamedXContentRegistry(new SearchModule(Settings.EMPTY, List.of()).getNamedXContents()); + private static final NamedXContentRegistry REGISTRY = new NamedXContentRegistry( + new SearchModule(Settings.EMPTY, List.of()).getNamedXContents() + ); + public void testUnknownFieldParsingErrors() throws IOException { assertParsingErrorMessage("{\"key\" : \"value\"}", "unknown field [key]", EqlSearchRequest::fromXContent); } public void testSearchRequestParser() throws IOException { - assertParsingErrorMessage("{\"filter\" : 123}", "filter doesn't support values of type: VALUE_NUMBER", - EqlSearchRequest::fromXContent); - assertParsingErrorMessage("{\"timestamp_field\" : 123}", "timestamp_field doesn't support values of type: VALUE_NUMBER", - EqlSearchRequest::fromXContent); - assertParsingErrorMessage("{\"event_category_field\" : 123}", "event_category_field doesn't support values of type: VALUE_NUMBER", - EqlSearchRequest::fromXContent); + assertParsingErrorMessage( + "{\"filter\" : 123}", + "filter doesn't support values of type: VALUE_NUMBER", + EqlSearchRequest::fromXContent + ); + assertParsingErrorMessage( + "{\"timestamp_field\" : 123}", + "timestamp_field doesn't support values of type: VALUE_NUMBER", + EqlSearchRequest::fromXContent + ); + assertParsingErrorMessage( + "{\"event_category_field\" : 123}", + "event_category_field doesn't support values of type: VALUE_NUMBER", + EqlSearchRequest::fromXContent + ); assertParsingErrorMessage("{\"size\" : \"foo\"}", "failed to parse field [size]", EqlSearchRequest::fromXContent); - assertParsingErrorMessage("{\"query\" : 123}", "query doesn't support values of type: VALUE_NUMBER", - EqlSearchRequest::fromXContent); - assertParsingErrorMessage("{\"query\" : \"whatever\", \"size\":\"abc\"}", "failed to parse field [size]", - EqlSearchRequest::fromXContent); + assertParsingErrorMessage( + "{\"query\" : 123}", + "query doesn't support values of type: VALUE_NUMBER", + EqlSearchRequest::fromXContent + ); + assertParsingErrorMessage( + "{\"query\" : \"whatever\", \"size\":\"abc\"}", + "failed to parse field [size]", + EqlSearchRequest::fromXContent + ); - EqlSearchRequest request = generateRequest("endgame-*", "{\"filter\" : {\"match\" : {\"foo\":\"bar\"}}, " - + "\"timestamp_field\" : \"tsf\", " - + "\"event_category_field\" : \"etf\"," - + "\"size\" : \"101\"," - + "\"query\" : \"file where user != 'SYSTEM' by file_path\"}" - , EqlSearchRequest::fromXContent); - assertArrayEquals(new String[]{"endgame-*"}, request.indices()); + EqlSearchRequest request = generateRequest( + "endgame-*", + "{\"filter\" : {\"match\" : {\"foo\":\"bar\"}}, " + + "\"timestamp_field\" : \"tsf\", " + + "\"event_category_field\" : \"etf\"," + + "\"size\" : \"101\"," + + "\"query\" : \"file where user != 'SYSTEM' by file_path\"}", + EqlSearchRequest::fromXContent + ); + assertArrayEquals(new String[] { "endgame-*" }, request.indices()); assertNotNull(request.query()); assertTrue(request.filter() instanceof MatchQueryBuilder); - MatchQueryBuilder filter = (MatchQueryBuilder)request.filter(); + MatchQueryBuilder filter = (MatchQueryBuilder) request.filter(); assertEquals("foo", filter.fieldName()); assertEquals("bar", filter.value()); assertEquals("tsf", request.timestampField()); @@ -64,7 +84,7 @@ public void testSearchRequestParser() throws IOException { } private EqlSearchRequest generateRequest(String index, String json, Function fromXContent) - throws IOException { + throws IOException { XContentParser parser = parser(json); return fromXContent.apply(parser).indices(index); } diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/action/EqlSearchRequestTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/action/EqlSearchRequestTests.java index dc4f306ab3b42..0afddb518c59d 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/action/EqlSearchRequestTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/action/EqlSearchRequestTests.java @@ -10,12 +10,12 @@ import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.search.SearchModule; import org.elasticsearch.search.fetch.subphase.FieldAndFormat; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.eql.AbstractBWCSerializationTestCase; import org.junit.Before; @@ -31,18 +31,13 @@ public class EqlSearchRequestTests extends AbstractBWCSerializationTestCase { // TODO: possibly add mutations - static String defaultTestFilter = "{\n" + - " \"match\" : {\n" + - " \"foo\": \"bar\"\n" + - " }" + - "}"; + static String defaultTestFilter = "{\n" + " \"match\" : {\n" + " \"foo\": \"bar\"\n" + " }" + "}"; static String defaultTestIndex = "endgame-*"; boolean ccsMinimizeRoundtrips; @Before - public void setup() { - } + public void setup() {} @Override protected NamedWriteableRegistry getNamedWriteableRegistry() { @@ -69,8 +64,7 @@ protected EqlSearchRequest createTestInstance() { } QueryBuilder filter = parseFilter(defaultTestFilter); ccsMinimizeRoundtrips = randomBoolean(); - return new EqlSearchRequest() - .indices(defaultTestIndex) + return new EqlSearchRequest().indices(defaultTestIndex) .filter(filter) .timestampField(randomAlphaOfLength(10)) .eventCategoryField(randomAlphaOfLength(10)) diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/action/EqlSearchResponseTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/action/EqlSearchResponseTests.java index 5308528829e80..9d41aeee3110e 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/action/EqlSearchResponseTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/action/EqlSearchResponseTests.java @@ -9,16 +9,16 @@ import org.apache.lucene.search.TotalHits; import org.elasticsearch.Version; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.document.DocumentField; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Tuple; +import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.RandomObjects; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.test.RandomObjects; import org.elasticsearch.xpack.eql.AbstractBWCWireSerializingTestCase; import org.elasticsearch.xpack.eql.action.EqlSearchResponse.Event; import org.elasticsearch.xpack.eql.action.EqlSearchResponse.Sequence; @@ -131,10 +131,18 @@ private static Tuple randomDocumentField(XContentT DocumentField listField = new DocumentField(randomAlphaOfLength(5), listValues); return Tuple.tuple(listField, listField); case 2: - List objectValues = randomList(1, 5, () -> - Map.of(randomAlphaOfLength(5), randomInt(), - randomAlphaOfLength(5), randomBoolean(), - randomAlphaOfLength(5), randomAlphaOfLength(10))); + List objectValues = randomList( + 1, + 5, + () -> Map.of( + randomAlphaOfLength(5), + randomInt(), + randomAlphaOfLength(5), + randomBoolean(), + randomAlphaOfLength(5), + randomAlphaOfLength(10) + ) + ); DocumentField objectField = new DocumentField(randomAlphaOfLength(5), objectValues); return Tuple.tuple(objectField, objectField); default: @@ -168,8 +176,14 @@ public static EqlSearchResponse createRandomEventsResponse(TotalHits totalHits, if (randomBoolean()) { return new EqlSearchResponse(hits, randomIntBetween(0, 1001), randomBoolean()); } else { - return new EqlSearchResponse(hits, randomIntBetween(0, 1001), randomBoolean(), - randomAlphaOfLength(10), randomBoolean(), randomBoolean()); + return new EqlSearchResponse( + hits, + randomIntBetween(0, 1001), + randomBoolean(), + randomAlphaOfLength(10), + randomBoolean(), + randomBoolean() + ); } } @@ -194,24 +208,30 @@ public static EqlSearchResponse createRandomSequencesResponse(TotalHits totalHit if (randomBoolean()) { return new EqlSearchResponse(hits, randomIntBetween(0, 1001), randomBoolean()); } else { - return new EqlSearchResponse(hits, randomIntBetween(0, 1001), randomBoolean(), - randomAlphaOfLength(10), randomBoolean(), randomBoolean()); + return new EqlSearchResponse( + hits, + randomIntBetween(0, 1001), + randomBoolean(), + randomAlphaOfLength(10), + randomBoolean(), + randomBoolean() + ); } } private static List> getKeysGenerators() { List> randoms = new ArrayList<>(); randoms.add(() -> generateRandomStringArray(6, 11, false)); - randoms.add(() -> randomArray(0, 6, Integer[]::new, ()-> randomInt())); - randoms.add(() -> randomArray(0, 6, Long[]::new, ()-> randomLong())); - randoms.add(() -> randomArray(0, 6, Boolean[]::new, ()-> randomBoolean())); + randoms.add(() -> randomArray(0, 6, Integer[]::new, () -> randomInt())); + randoms.add(() -> randomArray(0, 6, Long[]::new, () -> randomLong())); + randoms.add(() -> randomArray(0, 6, Boolean[]::new, () -> randomBoolean())); return randoms; } public static EqlSearchResponse createRandomInstance(TotalHits totalHits, XContentType xType) { int type = between(0, 1); - switch(type) { + switch (type) { case 0: return createRandomEventsResponse(totalHits, xType); case 1: @@ -229,13 +249,19 @@ protected EqlSearchResponse mutateInstanceForVersion(EqlSearchResponse instance, List mutatedSequences = null; if (sequences != null) { mutatedSequences = new ArrayList<>(sequences.size()); - for(Sequence s : sequences) { + for (Sequence s : sequences) { mutatedSequences.add(new Sequence(s.joinKeys(), mutateEvents(s.events(), version))); } } - return new EqlSearchResponse(new EqlSearchResponse.Hits(mutatedEvents, mutatedSequences, instance.hits().totalHits()), - instance.took(), instance.isTimeout(), instance.id(), instance.isRunning(), instance.isPartial()); + return new EqlSearchResponse( + new EqlSearchResponse.Hits(mutatedEvents, mutatedSequences, instance.hits().totalHits()), + instance.took(), + instance.isTimeout(), + instance.id(), + instance.isRunning(), + instance.isPartial() + ); } private List mutateEvents(List original, Version version) { @@ -243,7 +269,7 @@ private List mutateEvents(List original, Version version) { return original; } List mutatedEvents = new ArrayList<>(original.size()); - for(Event e : original) { + for (Event e : original) { mutatedEvents.add(new Event(e.index(), e.id(), e.source(), version.onOrAfter(Version.V_7_13_0) ? e.fetchFields() : null)); } return mutatedEvents; diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/analysis/CancellationTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/analysis/CancellationTests.java index 27d737750ffb7..b2da309eda579 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/analysis/CancellationTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/analysis/CancellationTests.java @@ -85,8 +85,14 @@ public void testCancellationBeforeFieldCaps() throws InterruptedException { IndexResolver indexResolver = indexResolver(client); PlanExecutor planExecutor = planExecutor(client, indexResolver); CountDownLatch countDownLatch = new CountDownLatch(1); - TransportEqlSearchAction.operation(planExecutor, task, new EqlSearchRequest().indices(Strings.EMPTY_ARRAY).query("foo where blah"), - "", transportService, mockClusterService, new ActionListener<>() { + TransportEqlSearchAction.operation( + planExecutor, + task, + new EqlSearchRequest().indices(Strings.EMPTY_ARRAY).query("foo where blah"), + "", + transportService, + mockClusterService, + new ActionListener<>() { @Override public void onResponse(EqlSearchResponse eqlSearchResponse) { fail("Shouldn't be here"); @@ -98,7 +104,8 @@ public void onFailure(Exception e) { assertThat(e, instanceOf(TaskCancelledException.class)); countDownLatch.countDown(); } - }); + } + ); countDownLatch.await(); verify(client, times(1)).settings(); verify(client, times(1)).threadPool(); @@ -106,12 +113,19 @@ public void onFailure(Exception e) { } private Map> fields(String[] indices) { - FieldCapabilities fooField = - new FieldCapabilities("foo", "integer", false, true, true, indices, null, null, emptyMap()); - FieldCapabilities categoryField = - new FieldCapabilities("event.category", "keyword", false, true, true, indices, null, null, emptyMap()); - FieldCapabilities timestampField = - new FieldCapabilities("@timestamp", "date", false, true, true, indices, null, null, emptyMap()); + FieldCapabilities fooField = new FieldCapabilities("foo", "integer", false, true, true, indices, null, null, emptyMap()); + FieldCapabilities categoryField = new FieldCapabilities( + "event.category", + "keyword", + false, + true, + true, + indices, + null, + null, + emptyMap() + ); + FieldCapabilities timestampField = new FieldCapabilities("@timestamp", "date", false, true, true, indices, null, null, emptyMap()); Map> fields = new HashMap<>(); fields.put(fooField.getName(), singletonMap(fooField.getName(), fooField)); fields.put(categoryField.getName(), singletonMap(categoryField.getName(), categoryField)); @@ -125,7 +139,7 @@ public void testCancellationBeforeSearch() throws InterruptedException { EqlSearchTask task = EqlTestUtils.randomTask(); ClusterService mockClusterService = mockClusterService(); - String[] indices = new String[]{"endgame"}; + String[] indices = new String[] { "endgame" }; FieldCapabilitiesResponse fieldCapabilitiesResponse = mock(FieldCapabilitiesResponse.class); when(fieldCapabilitiesResponse.getIndices()).thenReturn(indices); @@ -138,24 +152,30 @@ public void testCancellationBeforeSearch() throws InterruptedException { return null; }).when(client).fieldCaps(any(), any()); - IndexResolver indexResolver = indexResolver(client); PlanExecutor planExecutor = planExecutor(client, indexResolver); CountDownLatch countDownLatch = new CountDownLatch(1); - TransportEqlSearchAction.operation(planExecutor, task, new EqlSearchRequest().indices("endgame") - .query("process where foo==3"), "", transportService, mockClusterService, new ActionListener<>() { - @Override - public void onResponse(EqlSearchResponse eqlSearchResponse) { - fail("Shouldn't be here"); - countDownLatch.countDown(); - } + TransportEqlSearchAction.operation( + planExecutor, + task, + new EqlSearchRequest().indices("endgame").query("process where foo==3"), + "", + transportService, + mockClusterService, + new ActionListener<>() { + @Override + public void onResponse(EqlSearchResponse eqlSearchResponse) { + fail("Shouldn't be here"); + countDownLatch.countDown(); + } - @Override - public void onFailure(Exception e) { - assertThat(e, instanceOf(TaskCancelledException.class)); - countDownLatch.countDown(); + @Override + public void onFailure(Exception e) { + assertThat(e, instanceOf(TaskCancelledException.class)); + countDownLatch.countDown(); + } } - }); + ); countDownLatch.await(); verify(client).fieldCaps(any(), any()); verify(client, times(1)).settings(); @@ -170,7 +190,7 @@ public void testCancellationDuringSearch() throws InterruptedException { String nodeId = randomAlphaOfLength(10); ClusterService mockClusterService = mockClusterService(nodeId); - String[] indices = new String[]{"endgame"}; + String[] indices = new String[] { "endgame" }; // Emulation of field capabilities FieldCapabilitiesResponse fieldCapabilitiesResponse = mock(FieldCapabilitiesResponse.class); @@ -202,20 +222,27 @@ public void testCancellationDuringSearch() throws InterruptedException { IndexResolver indexResolver = indexResolver(client); PlanExecutor planExecutor = planExecutor(client, indexResolver); CountDownLatch countDownLatch = new CountDownLatch(1); - TransportEqlSearchAction.operation(planExecutor, task, new EqlSearchRequest().indices("endgame") - .query("process where foo==3"), "", transportService, mockClusterService, new ActionListener<>() { - @Override - public void onResponse(EqlSearchResponse eqlSearchResponse) { - fail("Shouldn't be here"); - countDownLatch.countDown(); - } + TransportEqlSearchAction.operation( + planExecutor, + task, + new EqlSearchRequest().indices("endgame").query("process where foo==3"), + "", + transportService, + mockClusterService, + new ActionListener<>() { + @Override + public void onResponse(EqlSearchResponse eqlSearchResponse) { + fail("Shouldn't be here"); + countDownLatch.countDown(); + } - @Override - public void onFailure(Exception e) { - assertThat(e, instanceOf(TaskCancelledException.class)); - countDownLatch.countDown(); + @Override + public void onFailure(Exception e) { + assertThat(e, instanceOf(TaskCancelledException.class)); + countDownLatch.countDown(); + } } - }); + ); countDownLatch.await(); // Final verification to ensure no more interaction verify(client).fieldCaps(any(), any()); diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/analysis/VerifierTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/analysis/VerifierTests.java index b6476025e64bd..220bb0d60cbf4 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/analysis/VerifierTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/analysis/VerifierTests.java @@ -112,34 +112,48 @@ public void testMisspelledColumnWithMultipleOptions() { } public void testProcessRelationshipsUnsupported() { - assertEquals("2:7: Process relationships are not supported", - errorParsing("process where opcode==1 and process_name == \"csrss.exe\"\n" + - " and descendant of [file where file_name == \"csrss.exe\" and opcode==0]")); - assertEquals("2:7: Process relationships are not supported", - errorParsing("process where process_name==\"svchost.exe\"\n" + - " and child of [file where file_name=\"svchost.exe\" and opcode==0]")); + assertEquals( + "2:7: Process relationships are not supported", + errorParsing( + "process where opcode==1 and process_name == \"csrss.exe\"\n" + + " and descendant of [file where file_name == \"csrss.exe\" and opcode==0]" + ) + ); + assertEquals( + "2:7: Process relationships are not supported", + errorParsing( + "process where process_name==\"svchost.exe\"\n" + " and child of [file where file_name=\"svchost.exe\" and opcode==0]" + ) + ); } // Some functions fail with "Unsupported" message at the parse stage public void testArrayFunctionsUnsupported() { - assertEquals("1:16: Unknown function [arrayContains], did you mean [stringcontains]?", - error("registry where arrayContains(bytes_written_string_list, \"En\")")); - assertEquals("1:16: Unknown function [arraySearch]", - error("registry where arraySearch(bytes_written_string_list, bytes_written_string, true)")); - assertEquals("1:16: Unknown function [arrayCount]", - error("registry where arrayCount(bytes_written_string_list, bytes_written_string, true) == 1")); + assertEquals( + "1:16: Unknown function [arrayContains], did you mean [stringcontains]?", + error("registry where arrayContains(bytes_written_string_list, \"En\")") + ); + assertEquals( + "1:16: Unknown function [arraySearch]", + error("registry where arraySearch(bytes_written_string_list, bytes_written_string, true)") + ); + assertEquals( + "1:16: Unknown function [arrayCount]", + error("registry where arrayCount(bytes_written_string_list, bytes_written_string, true) == 1") + ); } // Some functions fail with "Unknown" message at the parse stage public void testFunctionParsingUnknown() { - assertEquals("1:15: Unknown function [safe]", - error("network where safe(process_name)")); + assertEquals("1:15: Unknown function [safe]", error("network where safe(process_name)")); } // Test unsupported array indexes public void testArrayIndexesUnsupported() { - assertEquals("1:84: Array indexes are not supported", - errorParsing("registry where length(bytes_written_string_list) > 0 and bytes_written_string_list[0] == \"EN-us")); + assertEquals( + "1:84: Array indexes are not supported", + errorParsing("registry where length(bytes_written_string_list) > 0 and bytes_written_string_list[0] == \"EN-us") + ); } public void testOptionalFieldsUnsupported() { @@ -166,8 +180,7 @@ public void testQueryOk() { accept("process where (serial_event_id<=8 and not serial_event_id > 7) and (opcode==3 and opcode>2)"); // In statement - accept("process where not (exit_code > -1)\n" + - " and serial_event_id in (58, 64, 69, 74, 80, 85, 90, 93, 94)"); + accept("process where not (exit_code > -1)\n" + " and serial_event_id in (58, 64, 69, 74, 80, 85, 90, 93, 94)"); // Combination accept("file where serial_event_id == 82 and (true == (process_name in (\"svchost.EXE\", \"bad.exe\", \"bad2.exe\")))"); @@ -201,12 +214,16 @@ public void testAliasErrors() { final IndexResolution idxr = loadIndexResolution("mapping-alias.json"); // Check unsupported - assertEquals("1:11: Cannot use field [user_name_alias] with unsupported type [alias]", - error(idxr, "foo where user_name_alias == \"bob\"")); + assertEquals( + "1:11: Cannot use field [user_name_alias] with unsupported type [alias]", + error(idxr, "foo where user_name_alias == \"bob\"") + ); // Check alias name typo - assertEquals("1:11: Unknown column [user_name_alia], did you mean any of [user_name, user_domain]?", - error(idxr, "foo where user_name_alia == \"bob\"")); + assertEquals( + "1:11: Unknown column [user_name_alia], did you mean any of [user_name, user_domain]?", + error(idxr, "foo where user_name_alia == \"bob\"") + ); } // Test all elasticsearch numeric field types @@ -222,8 +239,10 @@ public void testNumeric() { accept(idxr, "foo where scaled_float_field == 0"); // Test query against unsupported field type int - assertEquals("1:11: Cannot use field [wrong_int_type_field] with unsupported type [int]", - error(idxr, "foo where wrong_int_type_field == 0")); + assertEquals( + "1:11: Cannot use field [wrong_int_type_field] with unsupported type [int]", + error(idxr, "foo where wrong_int_type_field == 0") + ); } public void testNoDoc() { @@ -274,51 +293,68 @@ public void testBinary() { public void testRange() { final IndexResolution idxr = loadIndexResolution("mapping-range.json"); - assertEquals("1:11: Cannot use field [integer_range_field] with unsupported type [integer_range]", - error(idxr, "foo where integer_range_field == \"\"")); - assertEquals("1:11: Cannot use field [float_range_field] with unsupported type [float_range]", - error(idxr, "foo where float_range_field == \"\"")); - assertEquals("1:11: Cannot use field [long_range_field] with unsupported type [long_range]", - error(idxr, "foo where long_range_field == \"\"")); - assertEquals("1:11: Cannot use field [double_range_field] with unsupported type [double_range]", - error(idxr, "foo where double_range_field == \"\"")); - assertEquals("1:11: Cannot use field [date_range_field] with unsupported type [date_range]", - error(idxr, "foo where date_range_field == \"\"")); - assertEquals("1:11: Cannot use field [ip_range_field] with unsupported type [ip_range]", - error(idxr, "foo where ip_range_field == \"\"")); + assertEquals( + "1:11: Cannot use field [integer_range_field] with unsupported type [integer_range]", + error(idxr, "foo where integer_range_field == \"\"") + ); + assertEquals( + "1:11: Cannot use field [float_range_field] with unsupported type [float_range]", + error(idxr, "foo where float_range_field == \"\"") + ); + assertEquals( + "1:11: Cannot use field [long_range_field] with unsupported type [long_range]", + error(idxr, "foo where long_range_field == \"\"") + ); + assertEquals( + "1:11: Cannot use field [double_range_field] with unsupported type [double_range]", + error(idxr, "foo where double_range_field == \"\"") + ); + assertEquals( + "1:11: Cannot use field [date_range_field] with unsupported type [date_range]", + error(idxr, "foo where date_range_field == \"\"") + ); + assertEquals( + "1:11: Cannot use field [ip_range_field] with unsupported type [ip_range]", + error(idxr, "foo where ip_range_field == \"\"") + ); } public void testMixedSet() { final IndexResolution idxr = loadIndexResolution("mapping-numeric.json"); - assertEquals("1:11: 2nd argument of [long_field in (1, \"string\")] must be [long], found value [\"string\"] type [keyword]", - error(idxr, "foo where long_field in (1, \"string\")")); + assertEquals( + "1:11: 2nd argument of [long_field in (1, \"string\")] must be [long], found value [\"string\"] type [keyword]", + error(idxr, "foo where long_field in (1, \"string\")") + ); } public void testObject() { final IndexResolution idxr = loadIndexResolution("mapping-object.json"); accept(idxr, "foo where endgame.pid == 0"); - assertEquals("1:11: Unknown column [endgame.pi], did you mean [endgame.pid]?", - error(idxr, "foo where endgame.pi == 0")); + assertEquals("1:11: Unknown column [endgame.pi], did you mean [endgame.pid]?", error(idxr, "foo where endgame.pi == 0")); } public void testNested() { final IndexResolution idxr = loadIndexResolution("mapping-nested.json"); - assertEquals("1:11: Cannot use field [processes] type [nested] due to nested fields not being supported yet", - error(idxr, "foo where processes == 0")); - assertEquals("1:11: Cannot use field [processes.pid] type [long] with unsupported nested type in hierarchy (field [processes])", - error(idxr, "foo where processes.pid == 0")); - assertEquals("1:11: Unknown column [processe.pid], did you mean any of [processes.pid, processes.path, processes.path.keyword]?", - error(idxr, "foo where processe.pid == 0")); + assertEquals( + "1:11: Cannot use field [processes] type [nested] due to nested fields not being supported yet", + error(idxr, "foo where processes == 0") + ); + assertEquals( + "1:11: Cannot use field [processes.pid] type [long] with unsupported nested type in hierarchy (field [processes])", + error(idxr, "foo where processes.pid == 0") + ); + assertEquals( + "1:11: Unknown column [processe.pid], did you mean any of [processes.pid, processes.path, processes.path.keyword]?", + error(idxr, "foo where processe.pid == 0") + ); accept(idxr, "foo where long_field == 123"); } public void testGeo() { final IndexResolution idxr = loadIndexResolution("mapping-geo.json"); - assertEquals("1:11: Cannot use field [location] with unsupported type [geo_point]", - error(idxr, "foo where location == 0")); - assertEquals("1:11: Cannot use field [site] with unsupported type [geo_shape]", - error(idxr, "foo where site == 0")); + assertEquals("1:11: Cannot use field [location] with unsupported type [geo_point]", error(idxr, "foo where location == 0")); + assertEquals("1:11: Cannot use field [site] with unsupported type [geo_shape]", error(idxr, "foo where site == 0")); } public void testIP() { @@ -335,71 +371,98 @@ public void testMultiField() { final IndexResolution idxr = loadIndexResolution("mapping-multi-field.json"); accept(idxr, "foo where multi_field.raw == \"bar\""); - assertEquals("1:11: [multi_field.english == \"bar\"] cannot operate on first argument field of data type [text]: " + - "No keyword/multi-field defined exact matches for [english]; define one or use MATCH/QUERY instead", - error(idxr, "foo where multi_field.english == \"bar\"")); + assertEquals( + "1:11: [multi_field.english == \"bar\"] cannot operate on first argument field of data type [text]: " + + "No keyword/multi-field defined exact matches for [english]; define one or use MATCH/QUERY instead", + error(idxr, "foo where multi_field.english == \"bar\"") + ); accept(idxr, "foo where multi_field_options.raw == \"bar\""); accept(idxr, "foo where multi_field_options.key == \"bar\""); accept(idxr, "foo where multi_field_ambiguous.one == \"bar\""); accept(idxr, "foo where multi_field_ambiguous.two == \"bar\""); - assertEquals("1:11: [multi_field_ambiguous.normalized == \"bar\"] cannot operate on first argument field of data type [keyword]: " + - "Normalized keyword field cannot be used for exact match operations", - error(idxr, "foo where multi_field_ambiguous.normalized == \"bar\"")); - assertEquals("1:11: Cannot use field [multi_field_nested.dep_name] type [text] with unsupported nested type in hierarchy " + - "(field [multi_field_nested])", - error(idxr, "foo where multi_field_nested.dep_name == \"bar\"")); - assertEquals("1:11: Cannot use field [multi_field_nested.dep_id.keyword] type [keyword] with unsupported nested type in " + - "hierarchy (field [multi_field_nested])", - error(idxr, "foo where multi_field_nested.dep_id.keyword == \"bar\"")); - assertEquals("1:11: Cannot use field [multi_field_nested.end_date] type [datetime] with unsupported nested type in " + - "hierarchy (field [multi_field_nested])", - error(idxr, "foo where multi_field_nested.end_date == \"\"")); - assertEquals("1:11: Cannot use field [multi_field_nested.start_date] type [datetime] with unsupported nested type in " + - "hierarchy (field [multi_field_nested])", - error(idxr, "foo where multi_field_nested.start_date == \"bar\"")); + assertEquals( + "1:11: [multi_field_ambiguous.normalized == \"bar\"] cannot operate on first argument field of data type [keyword]: " + + "Normalized keyword field cannot be used for exact match operations", + error(idxr, "foo where multi_field_ambiguous.normalized == \"bar\"") + ); + assertEquals( + "1:11: Cannot use field [multi_field_nested.dep_name] type [text] with unsupported nested type in hierarchy " + + "(field [multi_field_nested])", + error(idxr, "foo where multi_field_nested.dep_name == \"bar\"") + ); + assertEquals( + "1:11: Cannot use field [multi_field_nested.dep_id.keyword] type [keyword] with unsupported nested type in " + + "hierarchy (field [multi_field_nested])", + error(idxr, "foo where multi_field_nested.dep_id.keyword == \"bar\"") + ); + assertEquals( + "1:11: Cannot use field [multi_field_nested.end_date] type [datetime] with unsupported nested type in " + + "hierarchy (field [multi_field_nested])", + error(idxr, "foo where multi_field_nested.end_date == \"\"") + ); + assertEquals( + "1:11: Cannot use field [multi_field_nested.start_date] type [datetime] with unsupported nested type in " + + "hierarchy (field [multi_field_nested])", + error(idxr, "foo where multi_field_nested.start_date == \"bar\"") + ); } public void testStringFunctionWithText() { final IndexResolution idxr = loadIndexResolution("mapping-multi-field.json"); - assertEquals("1:15: [string(multi_field.english)] cannot operate on field " + - "of data type [text]: No keyword/multi-field defined exact matches for [english]; " + - "define one or use MATCH/QUERY instead", - error(idxr, "process where string(multi_field.english) == \"foo\"")); + assertEquals( + "1:15: [string(multi_field.english)] cannot operate on field " + + "of data type [text]: No keyword/multi-field defined exact matches for [english]; " + + "define one or use MATCH/QUERY instead", + error(idxr, "process where string(multi_field.english) == \"foo\"") + ); } public void testIncorrectUsageOfStringEquals() { final IndexResolution idxr = loadIndexResolution("mapping-default.json"); - assertEquals("1:11: first argument of [:] must be [string], found value [pid] type [long]; consider using [==] instead", - error(idxr, "foo where pid : 123")); + assertEquals( + "1:11: first argument of [:] must be [string], found value [pid] type [long]; consider using [==] instead", + error(idxr, "foo where pid : 123") + ); } public void testKeysWithDifferentTypes() throws Exception { - assertEquals("1:62: Sequence key [md5] type [keyword] is incompatible with key [pid] type [long]", - error(index, "sequence " + - "[process where true] by pid " + - "[process where true] by md5")); + assertEquals( + "1:62: Sequence key [md5] type [keyword] is incompatible with key [pid] type [long]", + error(index, "sequence " + "[process where true] by pid " + "[process where true] by md5") + ); } public void testKeysWithDifferentButCompatibleTypes() throws Exception { - accept(index, "sequence " + - "[process where true] by hostname " + - "[process where true] by user_domain"); + accept(index, "sequence " + "[process where true] by hostname " + "[process where true] by user_domain"); } public void testKeysWithSimilarYetDifferentTypes() throws Exception { - assertEquals("1:69: Sequence key [opcode] type [long] is incompatible with key [@timestamp] type [date]", - error(index, "sequence " + - "[process where true] by @timestamp " + - "[process where true] by opcode")); + assertEquals( + "1:69: Sequence key [opcode] type [long] is incompatible with key [@timestamp] type [date]", + error(index, "sequence " + "[process where true] by @timestamp " + "[process where true] by opcode") + ); } private LogicalPlan analyzeWithVerifierFunction(Function> versionIncompatibleClusters) { PreAnalyzer preAnalyzer = new PreAnalyzer(); - EqlConfiguration eqlConfiguration = new EqlConfiguration(new String[] {"none"}, - org.elasticsearch.xpack.ql.util.DateUtils.UTC, "nobody", "cluster", null, emptyMap(), null, - TimeValue.timeValueSeconds(30), null, 123, "", new TaskId("test", 123), null, versionIncompatibleClusters); + EqlConfiguration eqlConfiguration = new EqlConfiguration( + new String[] { "none" }, + org.elasticsearch.xpack.ql.util.DateUtils.UTC, + "nobody", + "cluster", + null, + emptyMap(), + null, + TimeValue.timeValueSeconds(30), + null, + 123, + "", + new TaskId("test", 123), + null, + versionIncompatibleClusters + ); Analyzer analyzer = new Analyzer(eqlConfiguration, new EqlFunctionRegistry(), new Verifier(new Metrics())); IndexResolution resolution = IndexResolution.valid(new EsIndex("irrelevant", loadEqlMapping("mapping-default.json"))); return analyzer.analyze(preAnalyzer.preAnalyze(new EqlParser().createStatement("any where true"), resolution)); @@ -408,16 +471,32 @@ private LogicalPlan analyzeWithVerifierFunction(Function emptySet())); - Set clusters = new TreeSet<>() {{ - add("one"); - }}; + Set clusters = new TreeSet<>() { + { + add("one"); + } + }; VerificationException e = expectThrows(VerificationException.class, () -> analyzeWithVerifierFunction(x -> clusters)); - assertTrue(e.getMessage().contains("the following remote cluster is incompatible, being on a version different than local " - + "cluster's [" + Version.CURRENT + "]: [one]")); + assertTrue( + e.getMessage() + .contains( + "the following remote cluster is incompatible, being on a version different than local " + + "cluster's [" + + Version.CURRENT + + "]: [one]" + ) + ); clusters.add("two"); e = expectThrows(VerificationException.class, () -> analyzeWithVerifierFunction(x -> clusters)); - assertTrue(e.getMessage().contains("the following remote clusters are incompatible, being on a version different than local " - + "cluster's [" + Version.CURRENT + "]: [one, two]")); + assertTrue( + e.getMessage() + .contains( + "the following remote clusters are incompatible, being on a version different than local " + + "cluster's [" + + Version.CURRENT + + "]: [one, two]" + ) + ); } } diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/execution/assembler/ImplicitTiebreakerTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/execution/assembler/ImplicitTiebreakerTests.java index 582cf4f6bc6b2..612a7f5c8efeb 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/execution/assembler/ImplicitTiebreakerTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/execution/assembler/ImplicitTiebreakerTests.java @@ -65,15 +65,21 @@ public void query(QueryRequest r, ActionListener l) { if (ordinal > 0) { int previous = ordinal - 1; // except the first request, the rest should have the previous response's search_after _shard_doc value - assertArrayEquals("Elements at stage " + ordinal + " do not match", - r.searchSource().searchAfter(), new Object[] { String.valueOf(previous), implicitTiebreakerValues.get(previous) }); + assertArrayEquals( + "Elements at stage " + ordinal + " do not match", + r.searchSource().searchAfter(), + new Object[] { String.valueOf(previous), implicitTiebreakerValues.get(previous) } + ); } long sortValue = implicitTiebreakerValues.get(ordinal); SearchHit searchHit = new SearchHit(ordinal, String.valueOf(ordinal), null, null); - searchHit.sortValues(new SearchSortValues( - new Long[] { (long) ordinal, sortValue }, - new DocValueFormat[] { DocValueFormat.RAW, DocValueFormat.RAW })); + searchHit.sortValues( + new SearchSortValues( + new Long[] { (long) ordinal, sortValue }, + new DocValueFormat[] { DocValueFormat.RAW, DocValueFormat.RAW } + ) + ); SearchHits searchHits = new SearchHits(new SearchHit[] { searchHit }, new TotalHits(1, Relation.EQUAL_TO), 0.0f); SearchResponseSections internal = new SearchResponseSections(searchHits, null, null, false, false, null, 0); SearchResponse s = new SearchResponse(internal, null, 0, 1, 0, 0, null, Clusters.EMPTY); @@ -102,16 +108,22 @@ public void testImplicitTiebreakerBeingSet() { for (int i = 0; i < stages; i++) { final int j = i; - criteria.add(new Criterion(i, - new BoxedQueryRequest(() -> SearchSourceBuilder.searchSource() - .size(10) - .query(matchAllQuery()) - .terminateAfter(j), "@timestamp", emptyList(), emptySet()), - keyExtractors, - tsExtractor, - tbExtractor, - implicitTbExtractor, - criteriaDescending)); + criteria.add( + new Criterion( + i, + new BoxedQueryRequest( + () -> SearchSourceBuilder.searchSource().size(10).query(matchAllQuery()).terminateAfter(j), + "@timestamp", + emptyList(), + emptySet() + ), + keyExtractors, + tsExtractor, + tbExtractor, + implicitTbExtractor, + criteriaDescending + ) + ); // for DESC (TAIL) sequences only the first criterion is descending the rest are ASC, so flip it after the first query if (criteriaDescending && i == 0) { criteriaDescending = false; @@ -120,8 +132,6 @@ public void testImplicitTiebreakerBeingSet() { SequenceMatcher matcher = new SequenceMatcher(stages, descending, TimeValue.MINUS_ONE, null, NOOP_CIRCUIT_BREAKER); TumblingWindow window = new TumblingWindow(client, criteria, null, matcher); - window.execute(wrap(p -> {}, ex -> { - throw ExceptionsHelper.convertToRuntime(ex); - })); + window.execute(wrap(p -> {}, ex -> { throw ExceptionsHelper.convertToRuntime(ex); })); } } diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/execution/assembler/SequenceSpecTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/execution/assembler/SequenceSpecTests.java index 0997c34a82a89..cf9c1bbc3eaef 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/execution/assembler/SequenceSpecTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/execution/assembler/SequenceSpecTests.java @@ -7,19 +7,7 @@ package org.elasticsearch.xpack.eql.execution.assembler; -import static java.util.Collections.emptyList; -import static java.util.Collections.emptyMap; -import static java.util.Collections.emptySet; -import static java.util.Collections.singletonList; -import static org.elasticsearch.action.ActionListener.wrap; -import static org.elasticsearch.common.logging.LoggerMessageFormat.format; -import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; +import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; import org.apache.lucene.search.TotalHits; import org.apache.lucene.search.TotalHits.Relation; @@ -29,9 +17,9 @@ import org.elasticsearch.action.search.SearchResponse.Clusters; import org.elasticsearch.action.search.SearchResponseSections; import org.elasticsearch.common.breaker.NoopCircuitBreaker; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.core.Tuple; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.builder.SearchSourceBuilder; @@ -48,7 +36,19 @@ import org.elasticsearch.xpack.eql.session.Results; import org.elasticsearch.xpack.ql.execution.search.extractor.HitExtractor; -import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import static java.util.Collections.emptyList; +import static java.util.Collections.emptyMap; +import static java.util.Collections.emptySet; +import static java.util.Collections.singletonList; +import static org.elasticsearch.action.ActionListener.wrap; +import static org.elasticsearch.common.logging.LoggerMessageFormat.format; +import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery; public class SequenceSpecTests extends ESTestCase { @@ -113,15 +113,25 @@ class TestCriterion extends Criterion { private boolean unused = true; TestCriterion(final int ordinal) { - super(ordinal, - new BoxedQueryRequest(() -> SearchSourceBuilder.searchSource() - // set a non-negative size - .size(10) - .query(matchAllQuery()) - // pass the ordinal through terminate after - .terminateAfter(ordinal), "timestamp", emptyList(), emptySet()), - keyExtractors, - tsExtractor, tbExtractor, implicitTbExtractor, false); + super( + ordinal, + new BoxedQueryRequest( + () -> SearchSourceBuilder.searchSource() + // set a non-negative size + .size(10) + .query(matchAllQuery()) + // pass the ordinal through terminate after + .terminateAfter(ordinal), + "timestamp", + emptyList(), + emptySet() + ), + keyExtractors, + tsExtractor, + tbExtractor, + implicitTbExtractor, + false + ); this.ordinal = ordinal; } @@ -197,8 +207,11 @@ public void query(QueryRequest r, ActionListener l) { Map> evs = ordinal != Integer.MAX_VALUE ? events.get(ordinal) : emptyMap(); EventsAsHits eah = new EventsAsHits(evs); - SearchHits searchHits = new SearchHits(eah.hits.toArray(new SearchHit[0]), new TotalHits(eah.hits.size(), Relation.EQUAL_TO), - 0.0f); + SearchHits searchHits = new SearchHits( + eah.hits.toArray(new SearchHit[0]), + new TotalHits(eah.hits.size(), Relation.EQUAL_TO), + 0.0f + ); SearchResponseSections internal = new SearchResponseSections(searchHits, null, null, false, false, null, 0); SearchResponse s = new SearchResponse(internal, null, 0, 1, 0, 0, null, Clusters.EMPTY); l.onResponse(s); @@ -251,9 +264,7 @@ public void test() throws Exception { TumblingWindow window = new TumblingWindow(testClient, criteria, null, matcher); // finally make the assertion at the end of the listener - window.execute(wrap(this::checkResults, ex -> { - throw ExceptionsHelper.convertToRuntime(ex); - })); + window.execute(wrap(this::checkResults, ex -> { throw ExceptionsHelper.convertToRuntime(ex); })); } private void checkResults(Payload payload) { diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/execution/assembler/SeriesUtils.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/execution/assembler/SeriesUtils.java index 8bac0d58ef6ae..1f168250cfc67 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/execution/assembler/SeriesUtils.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/execution/assembler/SeriesUtils.java @@ -29,7 +29,9 @@ class SeriesUtils { private SeriesUtils() {} private enum SpecItem { - NAME, EVENT_STREAM, RESULTS + NAME, + EVENT_STREAM, + RESULTS } static class SeriesSpec { @@ -40,7 +42,6 @@ static class SeriesSpec { List> matches = new ArrayList<>(); Map allEvents = new HashMap<>(); - Object[] toArray() { return new Object[] { name, lineNumber, this }; } @@ -54,8 +55,8 @@ static Iterable readSpec(String url) throws Exception { SeriesSpec spec = new SeriesSpec(); try ( - InputStreamReader in = new InputStreamReader(SeriesUtils.class.getResourceAsStream(url), StandardCharsets.UTF_8); - BufferedReader reader = new BufferedReader(in) + InputStreamReader in = new InputStreamReader(SeriesUtils.class.getResourceAsStream(url), StandardCharsets.UTF_8); + BufferedReader reader = new BufferedReader(in) ) { int lineNumber = 0; @@ -75,9 +76,15 @@ static Iterable readSpec(String url) throws Exception { Integer previousLine = testNames.put(line, lineNumber); if (previousLine != null) { - throw new IllegalArgumentException(format(null, + throw new IllegalArgumentException( + format( + null, "Duplicate test name '{}' at line [{}] (previously seen at line [{}])", - line, lineNumber, previousLine)); + line, + lineNumber, + previousLine + ) + ); } readerState = SpecItem.EVENT_STREAM; break; @@ -103,29 +110,48 @@ static Iterable readSpec(String url) throws Exception { event = event.substring(i + 1); // validate if (spec.allEvents.isEmpty() == false && spec.hasKeys == false) { - throw new IllegalArgumentException(format(null, + throw new IllegalArgumentException( + format( + null, "Cannot have a mixture of key [{}] and non-key [{}] events at line [{}]", - event, spec.allEvents.values().iterator().next(), lineNumber)); + event, + spec.allEvents.values().iterator().next(), + lineNumber + ) + ); } spec.hasKeys = true; } else { if (spec.hasKeys) { - throw new IllegalArgumentException(format(null, + throw new IllegalArgumentException( + format( + null, "Cannot have a mixture of key [{}] and non-key [{}] events at line [{}]", - event, spec.allEvents.values().iterator().next(), lineNumber)); + event, + spec.allEvents.values().iterator().next(), + lineNumber + ) + ); } } // find number int id = event.chars() - .filter(Character::isDigit) - .map(Character::getNumericValue) - .reduce(0, (l, r) -> l * 10 + r); + .filter(Character::isDigit) + .map(Character::getNumericValue) + .reduce(0, (l, r) -> l * 10 + r); String old = spec.allEvents.put(id, event); if (old != null) { - throw new IllegalArgumentException(format(null, + throw new IllegalArgumentException( + format( + null, "Detected colision for id [{}] between [{}] and [{}] at line [{}]", - id, old, event, lineNumber)); + id, + old, + event, + lineNumber + ) + ); } eventsMap.put(id, new Tuple<>(key, event)); } diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/execution/search/CriterionOrdinalExtractionTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/execution/search/CriterionOrdinalExtractionTests.java index f707b76e0f4bb..aaceabaf27738 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/execution/search/CriterionOrdinalExtractionTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/execution/search/CriterionOrdinalExtractionTests.java @@ -76,16 +76,30 @@ public void testTimeNotComparable() throws Exception { public void testImplicitTiebreakerMissing() throws Exception { SearchHit hit = searchHit(randomTimestamp(), null, new Object[0]); - Criterion criterion = new Criterion(0, null, emptyList(), tsExtractor, null, - implicitTbExtractor, randomBoolean()); + Criterion criterion = new Criterion( + 0, + null, + emptyList(), + tsExtractor, + null, + implicitTbExtractor, + randomBoolean() + ); EqlIllegalArgumentException exception = expectThrows(EqlIllegalArgumentException.class, () -> criterion.ordinal(hit)); assertTrue(exception.getMessage().startsWith("Expected at least one sorting value in the search hit, but got none")); } public void testImplicitTiebreakerNotANumber() throws Exception { SearchHit hit = searchHit(randomTimestamp(), null, new Object[] { "test string" }); - Criterion criterion = new Criterion(0, null, emptyList(), tsExtractor, null, - implicitTbExtractor, randomBoolean()); + Criterion criterion = new Criterion( + 0, + null, + emptyList(), + tsExtractor, + null, + implicitTbExtractor, + randomBoolean() + ); EqlIllegalArgumentException exception = expectThrows(EqlIllegalArgumentException.class, () -> criterion.ordinal(hit)); assertTrue(exception.getMessage().startsWith("Expected _shard_doc/implicit tiebreaker as long but got [test string]")); } @@ -109,12 +123,18 @@ public String getWriteableName() { } @Override - public void writeTo(StreamOutput out) throws IOException { - } + public void writeTo(StreamOutput out) throws IOException {} }; SearchHit hit = searchHit(randomTimestamp(), o); - Criterion criterion = new Criterion(0, null, emptyList(), tsExtractor, badExtractor, - implicitTbExtractor, false); + Criterion criterion = new Criterion( + 0, + null, + emptyList(), + tsExtractor, + badExtractor, + implicitTbExtractor, + false + ); EqlIllegalArgumentException exception = expectThrows(EqlIllegalArgumentException.class, () -> criterion.ordinal(hit)); assertTrue(exception.getMessage().startsWith("Expected tiebreaker")); } @@ -138,7 +158,14 @@ private SearchHit searchHit(Object timeValue, Object tiebreakerValue, Supplier(0, null, emptyList(), tsExtractor, withTiebreaker ? tbExtractor : null, - implicitTbExtractor, false).ordinal(hit); + return new Criterion( + 0, + null, + emptyList(), + tsExtractor, + withTiebreaker ? tbExtractor : null, + implicitTbExtractor, + false + ).ordinal(hit); } } diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/execution/search/LimitTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/execution/search/LimitTests.java index 1e6508651aa70..9427573cd4bcd 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/execution/search/LimitTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/execution/search/LimitTests.java @@ -13,7 +13,6 @@ import static java.util.Arrays.asList; - public class LimitTests extends ESTestCase { private final List list = asList(1, 2, 3, 4, 5, 6, 7); diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/execution/search/OrdinalTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/execution/search/OrdinalTests.java index b527af7beba48..c8299a367ae26 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/execution/search/OrdinalTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/execution/search/OrdinalTests.java @@ -9,7 +9,7 @@ import org.elasticsearch.test.ESTestCase; -@SuppressWarnings({"unchecked", "rawtypes"}) +@SuppressWarnings({ "unchecked", "rawtypes" }) public class OrdinalTests extends ESTestCase { public void testCompareToDifferentTs() { diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/execution/sequence/CircuitBreakerTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/execution/sequence/CircuitBreakerTests.java index fc34e27591bba..d71c626b85bd3 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/execution/sequence/CircuitBreakerTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/execution/sequence/CircuitBreakerTests.java @@ -35,6 +35,7 @@ import org.elasticsearch.xpack.eql.execution.search.Timestamp; import org.elasticsearch.xpack.eql.execution.search.extractor.ImplicitTiebreakerHitExtractor; import org.elasticsearch.xpack.ql.execution.search.extractor.HitExtractor; + import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -59,9 +60,9 @@ static class TestQueryClient implements QueryClient { public void query(QueryRequest r, ActionListener l) { int ordinal = r.searchSource().terminateAfter(); SearchHit searchHit = new SearchHit(ordinal, String.valueOf(ordinal), null, null); - searchHit.sortValues(new SearchSortValues( - new Long[] { (long) ordinal, 1L }, - new DocValueFormat[] { DocValueFormat.RAW, DocValueFormat.RAW })); + searchHit.sortValues( + new SearchSortValues(new Long[] { (long) ordinal, 1L }, new DocValueFormat[] { DocValueFormat.RAW, DocValueFormat.RAW }) + ); SearchHits searchHits = new SearchHits(new SearchHit[] { searchHit }, new TotalHits(1, Relation.EQUAL_TO), 0.0f); SearchResponseSections internal = new SearchResponseSections(searchHits, null, null, false, false, null, 0); SearchResponse s = new SearchResponse(internal, null, 0, 1, 0, 0, null, Clusters.EMPTY); @@ -88,42 +89,48 @@ public void testCircuitBreakerTumblingWindow() { for (int i = 0; i < stages; i++) { final int j = i; - criteria.add(new Criterion<>(i, - new BoxedQueryRequest(() -> SearchSourceBuilder.searchSource() - .size(10) - .query(matchAllQuery()) - .terminateAfter(j), "@timestamp", emptyList(), emptySet()), + criteria.add( + new Criterion<>( + i, + new BoxedQueryRequest( + () -> SearchSourceBuilder.searchSource().size(10).query(matchAllQuery()).terminateAfter(j), + "@timestamp", + emptyList(), + emptySet() + ), keyExtractors, tsExtractor, null, implicitTbExtractor, - false)); + false + ) + ); } SequenceMatcher matcher = new SequenceMatcher(stages, false, TimeValue.MINUS_ONE, null, CIRCUIT_BREAKER); TumblingWindow window = new TumblingWindow(client, criteria, null, matcher); - window.execute(wrap(p -> {}, ex -> { - throw ExceptionsHelper.convertToRuntime(ex); - })); + window.execute(wrap(p -> {}, ex -> { throw ExceptionsHelper.convertToRuntime(ex); })); CIRCUIT_BREAKER.startBreaking(); RuntimeException e = expectThrows( - RuntimeException.class, - () -> window.execute(wrap(p -> {}, ex -> { throw new RuntimeException(ex); })) + RuntimeException.class, + () -> window.execute(wrap(p -> {}, ex -> { throw new RuntimeException(ex); })) ); assertEquals(CircuitBreakingException.class, e.getCause().getClass()); CIRCUIT_BREAKER.stopBreaking(); - window.execute(wrap(p -> {}, ex -> { - throw ExceptionsHelper.convertToRuntime(ex); - })); + window.execute(wrap(p -> {}, ex -> { throw ExceptionsHelper.convertToRuntime(ex); })); } public void testCircuitBreakerSequnceMatcher() { List> hits = new ArrayList<>(); for (int i = 0; i < 10; i++) { - hits.add(new Tuple<>(new KeyAndOrdinal(new SequenceKey(i), new Ordinal(Timestamp.of(String.valueOf(i)), o -> 1, 0)), - new HitReference("index", i + ""))); + hits.add( + new Tuple<>( + new KeyAndOrdinal(new SequenceKey(i), new Ordinal(Timestamp.of(String.valueOf(i)), o -> 1, 0)), + new HitReference("index", i + "") + ) + ); } // Break on first iteration diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/EqlFunctionRegistryTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/EqlFunctionRegistryTests.java index 2e148772fe807..3cc33f28a39d0 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/EqlFunctionRegistryTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/EqlFunctionRegistryTests.java @@ -27,8 +27,11 @@ public class EqlFunctionRegistryTests extends ESTestCase { public void testBinaryCaseAwareFunction() { boolean caseAware = randomBoolean(); - UnresolvedFunction ur = uf(caseAware ? EqlFunctionResolution.CASE_INSENSITIVE : DEFAULT, - mock(Expression.class), mock(Expression.class)); + UnresolvedFunction ur = uf( + caseAware ? EqlFunctionResolution.CASE_INSENSITIVE : DEFAULT, + mock(Expression.class), + mock(Expression.class) + ); FunctionDefinition definition = def(DummyFunction.class, (Source l, Expression left, Expression right, boolean insensitive) -> { assertEquals(caseAware, insensitive); assertSame(left, ur.children().get(0)); @@ -43,29 +46,34 @@ public void testBinaryCaseAwareFunction() { assertFalse(((EqlFunctionDefinition) def).isCaseAware()); // No children aren't supported - ParsingException e = expectThrows(ParsingException.class, () -> - uf(DEFAULT).buildResolved(randomConfiguration(), def)); + ParsingException e = expectThrows(ParsingException.class, () -> uf(DEFAULT).buildResolved(randomConfiguration(), def)); assertThat(e.getMessage(), endsWith("error building [DUMMY_FUNCTION]: expects exactly two arguments")); // Multiple children aren't supported - e = expectThrows(ParsingException.class, () -> - uf(DEFAULT, mock(Expression.class)).buildResolved(randomConfiguration(), def)); + e = expectThrows(ParsingException.class, () -> uf(DEFAULT, mock(Expression.class)).buildResolved(randomConfiguration(), def)); assertThat(e.getMessage(), endsWith("expects exactly two arguments")); } public void testTernaryCaseAwareWithOptionalFunction() { boolean caseAware = randomBoolean(); boolean hasOptional = randomBoolean(); - UnresolvedFunction ur = uf(caseAware ? EqlFunctionResolution.CASE_INSENSITIVE : DEFAULT, - mock(Expression.class), mock(Expression.class), mock(Expression.class)); - FunctionDefinition definition = def(DummyFunction.class, + UnresolvedFunction ur = uf( + caseAware ? EqlFunctionResolution.CASE_INSENSITIVE : DEFAULT, + mock(Expression.class), + mock(Expression.class), + mock(Expression.class) + ); + FunctionDefinition definition = def( + DummyFunction.class, (Source l, Expression one, Expression two, Expression three, boolean insensitive) -> { assertEquals(caseAware, insensitive); assertSame(one, ur.children().get(0)); assertSame(two, ur.children().get(1)); assertSame(three, ur.children().get(2)); return new DummyFunction(l); - }, "DUMMY_FUNCTION"); + }, + "DUMMY_FUNCTION" + ); FunctionRegistry r = new EqlFunctionRegistry(definition); FunctionDefinition def = r.resolveFunction(ur.name()); assertEquals(ur.source(), ur.buildResolved(randomConfiguration(), def).source()); @@ -75,14 +83,11 @@ public void testTernaryCaseAwareWithOptionalFunction() { String message = "expects exactly three arguments"; // No children aren't supported - ParsingException e = expectThrows(ParsingException.class, () -> - uf(DEFAULT).buildResolved(randomConfiguration(), def)); + ParsingException e = expectThrows(ParsingException.class, () -> uf(DEFAULT).buildResolved(randomConfiguration(), def)); assertThat(e.getMessage(), endsWith(message)); // Multiple children aren't supported - e = expectThrows(ParsingException.class, () -> - uf(DEFAULT, mock(Expression.class)).buildResolved(randomConfiguration(), def)); + e = expectThrows(ParsingException.class, () -> uf(DEFAULT, mock(Expression.class)).buildResolved(randomConfiguration(), def)); assertThat(e.getMessage(), endsWith(message)); } } - diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/math/ToNumberFunctionPipeTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/math/ToNumberFunctionPipeTests.java index 0c4f10b5a6fa8..574c82c98c0d3 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/math/ToNumberFunctionPipeTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/math/ToNumberFunctionPipeTests.java @@ -35,10 +35,10 @@ private Expression randomToNumberFunctionExpression() { public static ToNumberFunctionPipe randomToNumberFunctionPipe() { return (ToNumberFunctionPipe) (new ToNumber( - randomSource(), - randomStringLiteral(), - randomFrom(true, false) ? randomIntLiteral() : null) - .makePipe()); + randomSource(), + randomStringLiteral(), + randomFrom(true, false) ? randomIntLiteral() : null + ).makePipe()); } @Override @@ -47,22 +47,13 @@ public void testTransform() { // skipping the children (string and base) which are tested separately ToNumberFunctionPipe b1 = randomInstance(); Expression newExpression = randomValueOtherThan(b1.expression(), () -> randomToNumberFunctionExpression()); - ToNumberFunctionPipe newB = new ToNumberFunctionPipe( - b1.source(), - newExpression, - b1.value(), - b1.base()); + ToNumberFunctionPipe newB = new ToNumberFunctionPipe(b1.source(), newExpression, b1.value(), b1.base()); assertEquals(newB, b1.transformPropertiesOnly(Expression.class, v -> Objects.equals(v, b1.expression()) ? newExpression : v)); ToNumberFunctionPipe b2 = randomInstance(); Source newLoc = randomValueOtherThan(b2.source(), () -> randomSource()); - newB = new ToNumberFunctionPipe( - newLoc, - b2.expression(), - b2.value(), - b2.base()); - assertEquals(newB, - b2.transformPropertiesOnly(Source.class, v -> Objects.equals(v, b2.source()) ? newLoc : v)); + newB = new ToNumberFunctionPipe(newLoc, b2.expression(), b2.value(), b2.base()); + assertEquals(newB, b2.transformPropertiesOnly(Source.class, v -> Objects.equals(v, b2.source()) ? newLoc : v)); } @Override @@ -94,27 +85,36 @@ public void testReplaceChildren() { @Override protected ToNumberFunctionPipe mutate(ToNumberFunctionPipe instance) { List> randoms = new ArrayList<>(); - randoms.add(f -> new ToNumberFunctionPipe(f.source(), + randoms.add( + f -> new ToNumberFunctionPipe( + f.source(), f.expression(), pipe(((Expression) randomValueOtherThan(f.value(), () -> randomStringLiteral()))), - f.base())); - randoms.add(f -> new ToNumberFunctionPipe(f.source(), + f.base() + ) + ); + randoms.add( + f -> new ToNumberFunctionPipe( + f.source(), f.expression(), f.value(), - f.base() == null ? null : randomValueOtherThan(f.base(), () -> pipe(randomIntLiteral())))); - randoms.add(f -> new ToNumberFunctionPipe(f.source(), + f.base() == null ? null : randomValueOtherThan(f.base(), () -> pipe(randomIntLiteral())) + ) + ); + randoms.add( + f -> new ToNumberFunctionPipe( + f.source(), f.expression(), pipe(((Expression) randomValueOtherThan(f.value(), () -> randomStringLiteral()))), - f.base() == null ? null : randomValueOtherThan(f.base(), () -> pipe(randomIntLiteral())))); + f.base() == null ? null : randomValueOtherThan(f.base(), () -> pipe(randomIntLiteral())) + ) + ); return randomFrom(randoms).apply(instance); } @Override protected ToNumberFunctionPipe copy(ToNumberFunctionPipe instance) { - return new ToNumberFunctionPipe(instance.source(), - instance.expression(), - instance.value(), - instance.base()); + return new ToNumberFunctionPipe(instance.source(), instance.expression(), instance.value(), instance.base()); } } diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/math/ToNumberFunctionProcessorTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/math/ToNumberFunctionProcessorTests.java index 31c159b813120..cb7c01e0047bf 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/math/ToNumberFunctionProcessorTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/math/ToNumberFunctionProcessorTests.java @@ -13,7 +13,6 @@ import static org.elasticsearch.xpack.ql.expression.function.scalar.FunctionTestUtils.l; import static org.elasticsearch.xpack.ql.tree.Source.EMPTY; - public class ToNumberFunctionProcessorTests extends ESTestCase { private static Object process(Object value, Object base) { @@ -21,8 +20,10 @@ private static Object process(Object value, Object base) { } private static String error(Object value, Object base) { - QlIllegalArgumentException saie = expectThrows(QlIllegalArgumentException.class, - () -> new ToNumber(EMPTY, l(value), l(base)).makePipe().asProcessor().process(null)); + QlIllegalArgumentException saie = expectThrows( + QlIllegalArgumentException.class, + () -> new ToNumber(EMPTY, l(value), l(base)).makePipe().asProcessor().process(null) + ); return saie.getMessage(); } @@ -90,8 +91,8 @@ public void toNumberWithPositiveExponent() { double expected = Math.pow((double) number, (double) exponent); - assertEquals(expected, process(number + "e" + exponent, null)); - assertEquals(expected, process(number + "e" + exponent, 10)); + assertEquals(expected, process(number + "e" + exponent, null)); + assertEquals(expected, process(number + "e" + exponent, 10)); } public void toNumberWithNegativeExponent() { @@ -100,69 +101,49 @@ public void toNumberWithNegativeExponent() { double expected = Math.pow(number, exponent); - assertEquals(expected, process(number + "e-" + exponent, null)); - assertEquals(expected, process(number + "e-" + exponent, 10)); + assertEquals(expected, process(number + "e-" + exponent, null)); + assertEquals(expected, process(number + "e-" + exponent, 10)); } public void toNumberWithLocales() { - assertEquals("Unable to convert [1,000] to number of base [10]", - error("1,000", 7)); - assertEquals("Unable to convert [1,000] to number of base [10]", - error("1,000,000", 7)); - assertEquals("Unable to convert [1,000] to number of base [10]", - error("1.000.000", 7)); - assertEquals("Unable to convert [1,000] to number of base [10]", - error("1,000.000.000", 7)); + assertEquals("Unable to convert [1,000] to number of base [10]", error("1,000", 7)); + assertEquals("Unable to convert [1,000] to number of base [10]", error("1,000,000", 7)); + assertEquals("Unable to convert [1,000] to number of base [10]", error("1.000.000", 7)); + assertEquals("Unable to convert [1,000] to number of base [10]", error("1,000.000.000", 7)); } public void toNumberWithUnsupportedDoubleBase() { // test that only base 10 fractions are supported double decimal = randomDouble(); - assertEquals("Unable to convert [1.0] to number of base [7]", - error(Double.toString(decimal), 7)); - assertEquals("Unable to convert [1.0] to number of base [8]", - error(Double.toString(decimal), 8)); - assertEquals("Unable to convert [1.0] to number of base [16]", - error(Double.toString(decimal), 16)); + assertEquals("Unable to convert [1.0] to number of base [7]", error(Double.toString(decimal), 7)); + assertEquals("Unable to convert [1.0] to number of base [8]", error(Double.toString(decimal), 8)); + assertEquals("Unable to convert [1.0] to number of base [16]", error(Double.toString(decimal), 16)); } public void testNegativeBase16() { - assertEquals("Unable to convert [-0x1] to number of base [16]", - error("-0x1", 16)); + assertEquals("Unable to convert [-0x1] to number of base [16]", error("-0x1", 16)); } public void testNumberInvalidDataType() { - assertEquals("A string/char is required; received [false]", - error(false, null)); - assertEquals("A string/char is required; received [1.0]", - error(1.0, null)); - assertEquals("A string/char is required; received [1]", - error(1, null)); + assertEquals("A string/char is required; received [false]", error(false, null)); + assertEquals("A string/char is required; received [1.0]", error(1.0, null)); + assertEquals("A string/char is required; received [1]", error(1, null)); } public void testInvalidBase() { int number = randomIntBetween(-100, 100); - assertEquals("An integer base is required; received [foo]", - error(Integer.toString(number), "foo")); - assertEquals("An integer base is required; received [1.0]", - error(Integer.toString(number), 1.0)); - assertEquals("An integer base is required; received [false]", - error(Integer.toString(number), false)); + assertEquals("An integer base is required; received [foo]", error(Integer.toString(number), "foo")); + assertEquals("An integer base is required; received [1.0]", error(Integer.toString(number), 1.0)); + assertEquals("An integer base is required; received [false]", error(Integer.toString(number), false)); } public void testInvalidSourceString() { - assertEquals("Unable to convert [] to number of base [10]", - error("", null)); - assertEquals("Unable to convert [] to number of base [16]", - error("", 16)); - assertEquals("Unable to convert [foo] to number of base [10]", - error("foo", null)); - assertEquals("Unable to convert [foo] to number of base [16]", - error("foo", 16)); - assertEquals("Unable to convert [1.2.3.4] to number of base [10]", - error("1.2.3.4", 10)); - assertEquals("Unable to convert [1.2.3.4] to number of base [16]", - error("1.2.3.4", 16)); + assertEquals("Unable to convert [] to number of base [10]", error("", null)); + assertEquals("Unable to convert [] to number of base [16]", error("", 16)); + assertEquals("Unable to convert [foo] to number of base [10]", error("foo", null)); + assertEquals("Unable to convert [foo] to number of base [16]", error("foo", 16)); + assertEquals("Unable to convert [1.2.3.4] to number of base [10]", error("1.2.3.4", 10)); + assertEquals("Unable to convert [1.2.3.4] to number of base [16]", error("1.2.3.4", 16)); } } diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/BetweenFunctionPipeTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/BetweenFunctionPipeTests.java index 9d80b294734dc..6d8bd19fd13d9 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/BetweenFunctionPipeTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/BetweenFunctionPipeTests.java @@ -36,13 +36,14 @@ private Expression randomBetweenFunctionExpression() { } public static BetweenFunctionPipe randomBetweenFunctionPipe() { - return (BetweenFunctionPipe) (new Between(randomSource(), + return (BetweenFunctionPipe) (new Between( + randomSource(), randomStringLiteral(), randomStringLiteral(), randomStringLiteral(), randomBooleanLiteral(), - randomBoolean()) - .makePipe()); + randomBoolean() + ).makePipe()); } @Override @@ -58,20 +59,14 @@ public void testTransform() { b1.left(), b1.right(), b1.greedy(), - b1.isCaseInsensitive()); + b1.isCaseInsensitive() + ); assertEquals(newB, b1.transformPropertiesOnly(Expression.class, v -> Objects.equals(v, b1.expression()) ? newExpression : v)); BetweenFunctionPipe b2 = randomInstance(); Source newLoc = randomValueOtherThan(b2.source(), () -> randomSource()); - newB = new BetweenFunctionPipe( - newLoc, - b2.expression(), - b2.input(), - b2.left(), - b2.right(), - b2.greedy(), - b2.isCaseInsensitive()); + newB = new BetweenFunctionPipe(newLoc, b2.expression(), b2.input(), b2.left(), b2.right(), b2.greedy(), b2.isCaseInsensitive()); assertEquals(newB, b2.transformPropertiesOnly(Source.class, v -> Objects.equals(v, b2.source()) ? newLoc : v)); } @@ -85,8 +80,15 @@ public void testReplaceChildren() { Pipe newGreedy = b.greedy() == null ? null : randomValueOtherThan(b.greedy(), () -> pipe(randomBooleanLiteral())); boolean newCaseSensitive = b.isCaseInsensitive() == false; - BetweenFunctionPipe newB = new BetweenFunctionPipe(b.source(), b.expression(), b.input(), b.left(), b.right(), b.greedy(), - newCaseSensitive); + BetweenFunctionPipe newB = new BetweenFunctionPipe( + b.source(), + b.expression(), + b.input(), + b.left(), + b.right(), + b.greedy(), + newCaseSensitive + ); BetweenFunctionPipe transformed = null; // generate all the combinations of possible children modifications and test all of them @@ -97,7 +99,8 @@ public void testReplaceChildren() { comb.get(0) ? newInput : b.input(), comb.get(1) ? newLeft : b.left(), comb.get(2) ? newRight : b.right(), - tempNewGreedy); + tempNewGreedy + ); assertEquals(transformed.input(), comb.get(0) ? newInput : b.input()); assertEquals(transformed.left(), comb.get(1) ? newLeft : b.left()); @@ -114,27 +117,35 @@ public void testReplaceChildren() { protected BetweenFunctionPipe mutate(BetweenFunctionPipe instance) { List> randoms = new ArrayList<>(); if (instance.greedy() == null) { - for(int i = 1; i < 5; i++) { - for(BitSet comb : new Combinations(4, i)) { - randoms.add(f -> new BetweenFunctionPipe(f.source(), - f.expression(), - comb.get(0) ? randomValueOtherThan(f.input(), () -> pipe(randomStringLiteral())) : f.input(), - comb.get(1) ? randomValueOtherThan(f.left(), () -> pipe(randomStringLiteral())) : f.left(), - comb.get(2) ? randomValueOtherThan(f.right(), () -> pipe(randomStringLiteral())) : f.right(), - null, - comb.get(4) ? f.isCaseInsensitive() == false : f.isCaseInsensitive())); + for (int i = 1; i < 5; i++) { + for (BitSet comb : new Combinations(4, i)) { + randoms.add( + f -> new BetweenFunctionPipe( + f.source(), + f.expression(), + comb.get(0) ? randomValueOtherThan(f.input(), () -> pipe(randomStringLiteral())) : f.input(), + comb.get(1) ? randomValueOtherThan(f.left(), () -> pipe(randomStringLiteral())) : f.left(), + comb.get(2) ? randomValueOtherThan(f.right(), () -> pipe(randomStringLiteral())) : f.right(), + null, + comb.get(4) ? f.isCaseInsensitive() == false : f.isCaseInsensitive() + ) + ); } } } else { - for(int i = 1; i < 6; i++) { - for(BitSet comb : new Combinations(5, i)) { - randoms.add(f -> new BetweenFunctionPipe(f.source(), - f.expression(), - comb.get(0) ? randomValueOtherThan(f.input(), () -> pipe(randomStringLiteral())) : f.input(), - comb.get(1) ? randomValueOtherThan(f.left(), () -> pipe(randomStringLiteral())) : f.left(), - comb.get(2) ? randomValueOtherThan(f.right(), () -> pipe(randomStringLiteral())) : f.right(), - comb.get(3) ? randomValueOtherThan(f.greedy(), () -> pipe(randomBooleanLiteral())) : f.greedy(), - comb.get(4) ? f.isCaseInsensitive() == false : f.isCaseInsensitive())); + for (int i = 1; i < 6; i++) { + for (BitSet comb : new Combinations(5, i)) { + randoms.add( + f -> new BetweenFunctionPipe( + f.source(), + f.expression(), + comb.get(0) ? randomValueOtherThan(f.input(), () -> pipe(randomStringLiteral())) : f.input(), + comb.get(1) ? randomValueOtherThan(f.left(), () -> pipe(randomStringLiteral())) : f.left(), + comb.get(2) ? randomValueOtherThan(f.right(), () -> pipe(randomStringLiteral())) : f.right(), + comb.get(3) ? randomValueOtherThan(f.greedy(), () -> pipe(randomBooleanLiteral())) : f.greedy(), + comb.get(4) ? f.isCaseInsensitive() == false : f.isCaseInsensitive() + ) + ); } } } @@ -144,12 +155,14 @@ protected BetweenFunctionPipe mutate(BetweenFunctionPipe instance) { @Override protected BetweenFunctionPipe copy(BetweenFunctionPipe instance) { - return new BetweenFunctionPipe(instance.source(), + return new BetweenFunctionPipe( + instance.source(), instance.expression(), instance.input(), instance.left(), instance.right(), instance.greedy(), - instance.isCaseInsensitive()); + instance.isCaseInsensitive() + ); } } diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/BetweenFunctionProcessorTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/BetweenFunctionProcessorTests.java index a962b0ac7bcf6..b14b528507409 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/BetweenFunctionProcessorTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/BetweenFunctionProcessorTests.java @@ -24,8 +24,10 @@ public void testNullOrEmptyParameters() throws Exception { // The source parameter can be null. Expect exception if any of other parameters is null. if ((source != null) && (left == null || right == null || greedy == null || caseInsensitive == null)) { - QlIllegalArgumentException e = expectThrows(QlIllegalArgumentException.class, - () -> BetweenFunctionProcessor.doProcess(source, left, right, greedy, caseInsensitive)); + QlIllegalArgumentException e = expectThrows( + QlIllegalArgumentException.class, + () -> BetweenFunctionProcessor.doProcess(source, left, right, greedy, caseInsensitive) + ); if (left == null || right == null) { assertThat(e.getMessage(), equalTo("A string/char is required; received [null]")); } else { diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/CIDRMatchFunctionPipeTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/CIDRMatchFunctionPipeTests.java index b87756edf8716..00a360e2e5105 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/CIDRMatchFunctionPipeTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/CIDRMatchFunctionPipeTests.java @@ -47,21 +47,13 @@ public void testTransform() { // skipping the children (input, addresses) which are tested separately CIDRMatchFunctionPipe b1 = randomInstance(); Expression newExpression = randomValueOtherThan(b1.expression(), () -> randomCIDRMatchFunctionExpression()); - CIDRMatchFunctionPipe newB = new CIDRMatchFunctionPipe( - b1.source(), - newExpression, - b1.input(), - b1.addresses()); + CIDRMatchFunctionPipe newB = new CIDRMatchFunctionPipe(b1.source(), newExpression, b1.input(), b1.addresses()); assertEquals(newB, b1.transformPropertiesOnly(Expression.class, v -> Objects.equals(v, b1.expression()) ? newExpression : v)); CIDRMatchFunctionPipe b2 = randomInstance(); Source newLoc = randomValueOtherThan(b2.source(), () -> randomSource()); - newB = new CIDRMatchFunctionPipe( - newLoc, - b2.expression(), - b2.input(), - b2.addresses()); + newB = new CIDRMatchFunctionPipe(newLoc, b2.expression(), b2.input(), b2.addresses()); assertEquals(newB, b2.transformPropertiesOnly(Source.class, v -> Objects.equals(v, b2.source()) ? newLoc : v)); } @@ -96,18 +88,23 @@ public void testReplaceChildren() { @Override protected CIDRMatchFunctionPipe mutate(CIDRMatchFunctionPipe instance) { List> randoms = new ArrayList<>(); - randoms.add(f -> new CIDRMatchFunctionPipe(f.source(), + randoms.add( + f -> new CIDRMatchFunctionPipe( + f.source(), f.expression(), randomValueOtherThan(f.input(), () -> pipe(randomStringLiteral())), - f.addresses())); - randoms.add(f -> new CIDRMatchFunctionPipe(f.source(), - f.expression(), - f.input(), - mutateOneAddress(f.addresses()))); - randoms.add(f -> new CIDRMatchFunctionPipe(f.source(), + f.addresses() + ) + ); + randoms.add(f -> new CIDRMatchFunctionPipe(f.source(), f.expression(), f.input(), mutateOneAddress(f.addresses()))); + randoms.add( + f -> new CIDRMatchFunctionPipe( + f.source(), f.expression(), randomValueOtherThan(f.input(), () -> pipe(randomStringLiteral())), - mutateOneAddress(f.addresses()))); + mutateOneAddress(f.addresses()) + ) + ); return randomFrom(randoms).apply(instance); } diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/CIDRMatchFunctionProcessorTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/CIDRMatchFunctionProcessorTests.java index 6b28327794e6c..66e544ed55ec3 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/CIDRMatchFunctionProcessorTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/CIDRMatchFunctionProcessorTests.java @@ -36,38 +36,47 @@ public void testCIDRMatchFunctionInvalidInput() { ArrayList addresses = new ArrayList<>(); // Invalid source address - EqlIllegalArgumentException e = expectThrows(EqlIllegalArgumentException.class, - () -> new CIDRMatch(EMPTY, l("10.6.48"), addresses).makePipe().asProcessor().process(null)); + EqlIllegalArgumentException e = expectThrows( + EqlIllegalArgumentException.class, + () -> new CIDRMatch(EMPTY, l("10.6.48"), addresses).makePipe().asProcessor().process(null) + ); assertEquals("'10.6.48' is not an IP string literal.", e.getMessage()); // Invalid match ip address addresses.add(l("10.6.48")); - e = expectThrows(EqlIllegalArgumentException.class, - () -> new CIDRMatch(EMPTY, l("10.6.48.157"), addresses).makePipe().asProcessor().process(null)); + e = expectThrows( + EqlIllegalArgumentException.class, + () -> new CIDRMatch(EMPTY, l("10.6.48.157"), addresses).makePipe().asProcessor().process(null) + ); assertEquals("'10.6.48' is not an IP string literal.", e.getMessage()); addresses.clear(); // Invalid CIDR addresses.add(l("10.6.12/12")); - e = expectThrows(EqlIllegalArgumentException.class, - () -> new CIDRMatch(EMPTY, l("10.6.48.157"), addresses).makePipe().asProcessor().process(null)); + e = expectThrows( + EqlIllegalArgumentException.class, + () -> new CIDRMatch(EMPTY, l("10.6.48.157"), addresses).makePipe().asProcessor().process(null) + ); assertEquals("'10.6.12' is not an IP string literal.", e.getMessage()); addresses.clear(); // Invalid source type - QlIllegalArgumentException eqe = expectThrows(QlIllegalArgumentException.class, - () -> new CIDRMatch(EMPTY, l(12345), addresses).makePipe().asProcessor().process(null)); + QlIllegalArgumentException eqe = expectThrows( + QlIllegalArgumentException.class, + () -> new CIDRMatch(EMPTY, l(12345), addresses).makePipe().asProcessor().process(null) + ); assertEquals("A string/char is required; received [12345]", eqe.getMessage()); - // Invalid cidr type addresses.add(l(5678)); - eqe = expectThrows(QlIllegalArgumentException.class, - () -> new CIDRMatch(EMPTY, l("10.6.48.157"), addresses).makePipe().asProcessor().process(null)); + eqe = expectThrows( + QlIllegalArgumentException.class, + () -> new CIDRMatch(EMPTY, l("10.6.48.157"), addresses).makePipe().asProcessor().process(null) + ); assertEquals("A string/char is required; received [5678]", eqe.getMessage()); } diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/ConcatFunctionPipeTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/ConcatFunctionPipeTests.java index 31bad7be7a873..17d24409173b8 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/ConcatFunctionPipeTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/ConcatFunctionPipeTests.java @@ -46,19 +46,13 @@ public void testTransform() { // skipping the children (the values) which are tested separately ConcatFunctionPipe b1 = randomInstance(); Expression newExpression = randomValueOtherThan(b1.expression(), () -> randomConcatFunctionExpression()); - ConcatFunctionPipe newB = new ConcatFunctionPipe( - b1.source(), - newExpression, - b1.values()); + ConcatFunctionPipe newB = new ConcatFunctionPipe(b1.source(), newExpression, b1.values()); assertEquals(newB, b1.transformPropertiesOnly(Expression.class, v -> Objects.equals(v, b1.expression()) ? newExpression : v)); ConcatFunctionPipe b2 = randomInstance(); Source newLoc = randomValueOtherThan(b2.source(), () -> randomSource()); - newB = new ConcatFunctionPipe( - newLoc, - b2.expression(), - b2.values()); + newB = new ConcatFunctionPipe(newLoc, b2.expression(), b2.values()); assertEquals(newB, b2.transformPropertiesOnly(Source.class, v -> Objects.equals(v, b2.source()) ? newLoc : v)); } @@ -78,9 +72,7 @@ public void testReplaceChildren() { @Override protected ConcatFunctionPipe mutate(ConcatFunctionPipe instance) { - return new ConcatFunctionPipe(instance.source(), - instance.expression(), - mutateOneValue(instance.values())); + return new ConcatFunctionPipe(instance.source(), instance.expression(), mutateOneValue(instance.values())); } @Override diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/ConcatFunctionProcessorTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/ConcatFunctionProcessorTests.java index 8bd0fd1e2eb17..9695eb55bb3a5 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/ConcatFunctionProcessorTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/ConcatFunctionProcessorTests.java @@ -16,10 +16,9 @@ import static org.elasticsearch.xpack.ql.expression.function.scalar.FunctionTestUtils.l; import static org.elasticsearch.xpack.ql.tree.Source.EMPTY; - public class ConcatFunctionProcessorTests extends ESTestCase { - private static Object process(Object ... arguments) { + private static Object process(Object... arguments) { List literals = new ArrayList<>(arguments.length); for (Object arg : arguments) { literals.add(l(arg)); diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/EndsWithFunctionPipeTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/EndsWithFunctionPipeTests.java index e9262c016ccc0..e96e901e2df43 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/EndsWithFunctionPipeTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/EndsWithFunctionPipeTests.java @@ -33,10 +33,7 @@ private Expression randomEndsWithFunctionExpression() { } public static EndsWithFunctionPipe randomEndsWithFunctionPipe() { - return (EndsWithFunctionPipe) (new EndsWith(randomSource(), - randomStringLiteral(), - randomStringLiteral(), - randomBoolean()) + return (EndsWithFunctionPipe) (new EndsWith(randomSource(), randomStringLiteral(), randomStringLiteral(), randomBoolean()) .makePipe()); } @@ -46,24 +43,13 @@ public void testTransform() { // skipping the children (input and pattern) which are tested separately EndsWithFunctionPipe b1 = randomInstance(); Expression newExpression = randomValueOtherThan(b1.expression(), this::randomEndsWithFunctionExpression); - EndsWithFunctionPipe newB = new EndsWithFunctionPipe( - b1.source(), - newExpression, - b1.input(), - b1.pattern(), - b1.isCaseInsensitive()); + EndsWithFunctionPipe newB = new EndsWithFunctionPipe(b1.source(), newExpression, b1.input(), b1.pattern(), b1.isCaseInsensitive()); assertEquals(newB, b1.transformPropertiesOnly(Expression.class, v -> Objects.equals(v, b1.expression()) ? newExpression : v)); EndsWithFunctionPipe b2 = randomInstance(); Source newLoc = randomValueOtherThan(b2.source(), () -> randomSource()); - newB = new EndsWithFunctionPipe( - newLoc, - b2.expression(), - b2.input(), - b2.pattern(), - b2.isCaseInsensitive()); - assertEquals(newB, - b2.transformPropertiesOnly(Source.class, v -> Objects.equals(v, b2.source()) ? newLoc : v)); + newB = new EndsWithFunctionPipe(newLoc, b2.expression(), b2.input(), b2.pattern(), b2.isCaseInsensitive()); + assertEquals(newB, b2.transformPropertiesOnly(Source.class, v -> Objects.equals(v, b2.source()) ? newLoc : v)); } @Override @@ -72,8 +58,7 @@ public void testReplaceChildren() { Pipe newInput = pipe(((Expression) randomValueOtherThan(b.input(), () -> randomStringLiteral()))); Pipe newPattern = pipe(((Expression) randomValueOtherThan(b.pattern(), () -> randomStringLiteral()))); boolean newCaseSensitive = randomValueOtherThan(b.isCaseInsensitive(), () -> randomBoolean()); - EndsWithFunctionPipe newB = - new EndsWithFunctionPipe(b.source(), b.expression(), b.input(), b.pattern(), newCaseSensitive); + EndsWithFunctionPipe newB = new EndsWithFunctionPipe(b.source(), b.expression(), b.input(), b.pattern(), newCaseSensitive); EndsWithFunctionPipe transformed = newB.replaceChildren(newInput, b.pattern()); assertEquals(transformed.input(), newInput); @@ -97,31 +82,45 @@ public void testReplaceChildren() { @Override protected EndsWithFunctionPipe mutate(EndsWithFunctionPipe instance) { List> randoms = new ArrayList<>(); - randoms.add(f -> new EndsWithFunctionPipe(f.source(), - f.expression(), - pipe(((Expression) randomValueOtherThan(f.input(), () -> randomStringLiteral()))), - f.pattern(), - randomValueOtherThan(f.isCaseInsensitive(), () -> randomBoolean()))); - randoms.add(f -> new EndsWithFunctionPipe(f.source(), - f.expression(), - f.input(), - pipe(((Expression) randomValueOtherThan(f.pattern(), () -> randomStringLiteral()))), - randomValueOtherThan(f.isCaseInsensitive(), () -> randomBoolean()))); - randoms.add(f -> new EndsWithFunctionPipe(f.source(), - f.expression(), - pipe(((Expression) randomValueOtherThan(f.input(), () -> randomStringLiteral()))), - pipe(((Expression) randomValueOtherThan(f.pattern(), () -> randomStringLiteral()))), - randomValueOtherThan(f.isCaseInsensitive(), () -> randomBoolean()))); + randoms.add( + f -> new EndsWithFunctionPipe( + f.source(), + f.expression(), + pipe(((Expression) randomValueOtherThan(f.input(), () -> randomStringLiteral()))), + f.pattern(), + randomValueOtherThan(f.isCaseInsensitive(), () -> randomBoolean()) + ) + ); + randoms.add( + f -> new EndsWithFunctionPipe( + f.source(), + f.expression(), + f.input(), + pipe(((Expression) randomValueOtherThan(f.pattern(), () -> randomStringLiteral()))), + randomValueOtherThan(f.isCaseInsensitive(), () -> randomBoolean()) + ) + ); + randoms.add( + f -> new EndsWithFunctionPipe( + f.source(), + f.expression(), + pipe(((Expression) randomValueOtherThan(f.input(), () -> randomStringLiteral()))), + pipe(((Expression) randomValueOtherThan(f.pattern(), () -> randomStringLiteral()))), + randomValueOtherThan(f.isCaseInsensitive(), () -> randomBoolean()) + ) + ); return randomFrom(randoms).apply(instance); } @Override protected EndsWithFunctionPipe copy(EndsWithFunctionPipe instance) { - return new EndsWithFunctionPipe(instance.source(), + return new EndsWithFunctionPipe( + instance.source(), instance.expression(), instance.input(), instance.pattern(), - instance.isCaseInsensitive()); + instance.isCaseInsensitive() + ); } } diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/EndsWithFunctionProcessorTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/EndsWithFunctionProcessorTests.java index 32301e89a5414..77a894ca59bb7 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/EndsWithFunctionProcessorTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/EndsWithFunctionProcessorTests.java @@ -73,22 +73,18 @@ private Boolean untypedEndsWith(Object left, Object right) { public void testEndsWithFunctionInputsValidation() { boolean caseSensitive = randomBoolean(); - QlIllegalArgumentException siae = expectThrows(QlIllegalArgumentException.class, - () -> untypedEndsWith(5, "foo")); + QlIllegalArgumentException siae = expectThrows(QlIllegalArgumentException.class, () -> untypedEndsWith(5, "foo")); assertEquals("A string/char is required; received [5]", siae.getMessage()); - siae = expectThrows(QlIllegalArgumentException.class, - () -> untypedEndsWith("bar", false)); + siae = expectThrows(QlIllegalArgumentException.class, () -> untypedEndsWith("bar", false)); assertEquals("A string/char is required; received [false]", siae.getMessage()); } public void testEndsWithFunctionWithRandomInvalidDataType() { boolean caseSensitive = randomBoolean(); Literal literal = randomValueOtherThanMany(v -> v.dataType() == KEYWORD, () -> LiteralTests.randomLiteral()); - QlIllegalArgumentException siae = expectThrows(QlIllegalArgumentException.class, - () -> untypedEndsWith(literal, "foo")); + QlIllegalArgumentException siae = expectThrows(QlIllegalArgumentException.class, () -> untypedEndsWith(literal, "foo")); assertThat(siae.getMessage(), startsWith("A string/char is required; received")); - siae = expectThrows(QlIllegalArgumentException.class, - () -> untypedEndsWith("foo", literal)); + siae = expectThrows(QlIllegalArgumentException.class, () -> untypedEndsWith("foo", literal)); assertThat(siae.getMessage(), startsWith("A string/char is required; received")); } } diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/IndexOfFunctionPipeTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/IndexOfFunctionPipeTests.java index 1ef4fd190a2a4..b8c0955c0f6aa 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/IndexOfFunctionPipeTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/IndexOfFunctionPipeTests.java @@ -36,12 +36,13 @@ private Expression randomIndexOfFunctionExpression() { } public static IndexOfFunctionPipe randomIndexOfFunctionPipe() { - return (IndexOfFunctionPipe) (new IndexOf(randomSource(), + return (IndexOfFunctionPipe) (new IndexOf( + randomSource(), randomStringLiteral(), randomStringLiteral(), randomFrom(true, false) ? randomIntLiteral() : null, - randomBoolean()) - .makePipe()); + randomBoolean() + ).makePipe()); } @Override @@ -56,19 +57,14 @@ public void testTransform() { b1.input(), b1.substring(), b1.start(), - b1.isCaseInsensitive()); + b1.isCaseInsensitive() + ); assertEquals(newB, b1.transformPropertiesOnly(Expression.class, v -> Objects.equals(v, b1.expression()) ? newExpression : v)); IndexOfFunctionPipe b2 = randomInstance(); Source newLoc = randomValueOtherThan(b2.source(), () -> randomSource()); - newB = new IndexOfFunctionPipe( - newLoc, - b2.expression(), - b2.input(), - b2.substring(), - b2.start(), - b2.isCaseInsensitive()); + newB = new IndexOfFunctionPipe(newLoc, b2.expression(), b2.input(), b2.substring(), b2.start(), b2.isCaseInsensitive()); assertEquals(newB, b2.transformPropertiesOnly(Source.class, v -> Objects.equals(v, b2.source()) ? newLoc : v)); } @@ -81,18 +77,25 @@ public void testReplaceChildren() { Pipe newStart = b.start() == null ? null : randomValueOtherThan(b.start(), () -> pipe(randomIntLiteral())); boolean newCaseSensitive = randomValueOtherThan(b.isCaseInsensitive(), () -> randomBoolean()); - IndexOfFunctionPipe newB = new IndexOfFunctionPipe(b.source(), b.expression(), b.input(), b.substring(), b.start(), - newCaseSensitive); + IndexOfFunctionPipe newB = new IndexOfFunctionPipe( + b.source(), + b.expression(), + b.input(), + b.substring(), + b.start(), + newCaseSensitive + ); IndexOfFunctionPipe transformed = null; // generate all the combinations of possible children modifications and test all of them - for(int i = 1; i < 4; i++) { - for(BitSet comb : new Combinations(3, i)) { + for (int i = 1; i < 4; i++) { + for (BitSet comb : new Combinations(3, i)) { Pipe tempNewStart = b.start() == null ? b.start() : (comb.get(2) ? newStart : b.start()); transformed = newB.replaceChildren( - comb.get(0) ? newInput : b.input(), - comb.get(1) ? newSubstring : b.substring(), - tempNewStart); + comb.get(0) ? newInput : b.input(), + comb.get(1) ? newSubstring : b.substring(), + tempNewStart + ); assertEquals(transformed.input(), comb.get(0) ? newInput : b.input()); assertEquals(transformed.substring(), comb.get(1) ? newSubstring : b.substring()); @@ -107,25 +110,33 @@ public void testReplaceChildren() { protected IndexOfFunctionPipe mutate(IndexOfFunctionPipe instance) { List> randoms = new ArrayList<>(); if (instance.start() == null) { - for(int i = 1; i < 3; i++) { - for(BitSet comb : new Combinations(2, i)) { - randoms.add(f -> new IndexOfFunctionPipe(f.source(), - f.expression(), - comb.get(0) ? randomValueOtherThan(f.input(), () -> pipe(randomStringLiteral())) : f.input(), - comb.get(1) ? randomValueOtherThan(f.substring(), () -> pipe(randomStringLiteral())) : f.substring(), - null, - randomValueOtherThan(f.isCaseInsensitive(), () -> randomBoolean()))); + for (int i = 1; i < 3; i++) { + for (BitSet comb : new Combinations(2, i)) { + randoms.add( + f -> new IndexOfFunctionPipe( + f.source(), + f.expression(), + comb.get(0) ? randomValueOtherThan(f.input(), () -> pipe(randomStringLiteral())) : f.input(), + comb.get(1) ? randomValueOtherThan(f.substring(), () -> pipe(randomStringLiteral())) : f.substring(), + null, + randomValueOtherThan(f.isCaseInsensitive(), () -> randomBoolean()) + ) + ); } } } else { - for(int i = 1; i < 4; i++) { - for(BitSet comb : new Combinations(3, i)) { - randoms.add(f -> new IndexOfFunctionPipe(f.source(), - f.expression(), - comb.get(0) ? randomValueOtherThan(f.input(), () -> pipe(randomStringLiteral())) : f.input(), - comb.get(1) ? randomValueOtherThan(f.substring(), () -> pipe(randomStringLiteral())) : f.substring(), - comb.get(2) ? randomValueOtherThan(f.start(), () -> pipe(randomIntLiteral())) : f.start(), - randomValueOtherThan(f.isCaseInsensitive(), () -> randomBoolean()))); + for (int i = 1; i < 4; i++) { + for (BitSet comb : new Combinations(3, i)) { + randoms.add( + f -> new IndexOfFunctionPipe( + f.source(), + f.expression(), + comb.get(0) ? randomValueOtherThan(f.input(), () -> pipe(randomStringLiteral())) : f.input(), + comb.get(1) ? randomValueOtherThan(f.substring(), () -> pipe(randomStringLiteral())) : f.substring(), + comb.get(2) ? randomValueOtherThan(f.start(), () -> pipe(randomIntLiteral())) : f.start(), + randomValueOtherThan(f.isCaseInsensitive(), () -> randomBoolean()) + ) + ); } } } @@ -135,11 +146,13 @@ protected IndexOfFunctionPipe mutate(IndexOfFunctionPipe instance) { @Override protected IndexOfFunctionPipe copy(IndexOfFunctionPipe instance) { - return new IndexOfFunctionPipe(instance.source(), + return new IndexOfFunctionPipe( + instance.source(), instance.expression(), instance.input(), instance.substring(), instance.start(), - instance.isCaseInsensitive()); + instance.isCaseInsensitive() + ); } } diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/IndexOfFunctionProcessorTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/IndexOfFunctionProcessorTests.java index e5ae011392158..54513d3aa5f06 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/IndexOfFunctionProcessorTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/IndexOfFunctionProcessorTests.java @@ -83,31 +83,25 @@ protected Object indexOfUntyped(Object left, Object right, Object optional) { } public void testIndexOfFunctionInputsValidation() { - QlIllegalArgumentException siae = expectThrows(QlIllegalArgumentException.class, - () -> indexOfUntyped(5, "foo", null)); + QlIllegalArgumentException siae = expectThrows(QlIllegalArgumentException.class, () -> indexOfUntyped(5, "foo", null)); assertEquals("A string/char is required; received [5]", siae.getMessage()); - siae = expectThrows(QlIllegalArgumentException.class, - () -> indexOfUntyped("bar", false, 2)); + siae = expectThrows(QlIllegalArgumentException.class, () -> indexOfUntyped("bar", false, 2)); assertEquals("A string/char is required; received [false]", siae.getMessage()); - siae = expectThrows(QlIllegalArgumentException.class, - () -> indexOfUntyped("bar", "a", "1")); + siae = expectThrows(QlIllegalArgumentException.class, () -> indexOfUntyped("bar", "a", "1")); assertEquals("A number is required; received [1]", siae.getMessage()); } public void testIndexOfFunctionWithRandomInvalidDataType() { Configuration config = randomConfiguration(); Literal stringLiteral = randomValueOtherThanMany(v -> v.dataType() == KEYWORD, () -> LiteralTests.randomLiteral()); - QlIllegalArgumentException siae = expectThrows(QlIllegalArgumentException.class, - () -> indexOfUntyped(stringLiteral, "foo", 1)); + QlIllegalArgumentException siae = expectThrows(QlIllegalArgumentException.class, () -> indexOfUntyped(stringLiteral, "foo", 1)); assertThat(siae.getMessage(), startsWith("A string/char is required; received")); - siae = expectThrows(QlIllegalArgumentException.class, - () -> indexOfUntyped("foo", stringLiteral, 2)); + siae = expectThrows(QlIllegalArgumentException.class, () -> indexOfUntyped("foo", stringLiteral, 2)); assertThat(siae.getMessage(), startsWith("A string/char is required; received")); Literal numericLiteral = randomValueOtherThanMany(v -> v.dataType().isNumeric(), () -> LiteralTests.randomLiteral()); - siae = expectThrows(QlIllegalArgumentException.class, - () -> indexOfUntyped("foo", "o", numericLiteral)); + siae = expectThrows(QlIllegalArgumentException.class, () -> indexOfUntyped("foo", "o", numericLiteral)); assertThat(siae.getMessage(), startsWith("A number is required; received")); } } diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/LengthFunctionPipeTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/LengthFunctionPipeTests.java index adb923fd52865..12fd77c36e89e 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/LengthFunctionPipeTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/LengthFunctionPipeTests.java @@ -39,19 +39,13 @@ public void testTransform() { // skipping the children (the input itself) which are tested separately LengthFunctionPipe b1 = randomInstance(); Expression newExpression = randomValueOtherThan(b1.expression(), () -> randomLengthFunctionExpression()); - LengthFunctionPipe newB = new LengthFunctionPipe( - b1.source(), - newExpression, - b1.input()); + LengthFunctionPipe newB = new LengthFunctionPipe(b1.source(), newExpression, b1.input()); assertEquals(newB, b1.transformPropertiesOnly(Expression.class, v -> Objects.equals(v, b1.expression()) ? newExpression : v)); LengthFunctionPipe b2 = randomInstance(); Source newLoc = randomValueOtherThan(b2.source(), () -> randomSource()); - newB = new LengthFunctionPipe( - newLoc, - b2.expression(), - b2.input()); + newB = new LengthFunctionPipe(newLoc, b2.expression(), b2.input()); assertEquals(newB, b2.transformPropertiesOnly(Source.class, v -> Objects.equals(v, b2.source()) ? newLoc : v)); } @@ -71,9 +65,11 @@ public void testReplaceChildren() { @Override protected LengthFunctionPipe mutate(LengthFunctionPipe instance) { - return new LengthFunctionPipe(instance.source(), - instance.expression(), - randomValueOtherThan(instance.input(), () -> pipe(randomStringLiteral()))); + return new LengthFunctionPipe( + instance.source(), + instance.expression(), + randomValueOtherThan(instance.input(), () -> pipe(randomStringLiteral())) + ); } @Override diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/LengthFunctionProcessorTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/LengthFunctionProcessorTests.java index 66211bf0825af..cab4c75650de4 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/LengthFunctionProcessorTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/LengthFunctionProcessorTests.java @@ -26,8 +26,10 @@ public void testLengthFunctionWithValidInput() { } public void testLengthFunctionInputsValidation() { - QlIllegalArgumentException siae = expectThrows(QlIllegalArgumentException.class, - () -> new Length(EMPTY, l(5)).makePipe().asProcessor().process(null)); + QlIllegalArgumentException siae = expectThrows( + QlIllegalArgumentException.class, + () -> new Length(EMPTY, l(5)).makePipe().asProcessor().process(null) + ); assertEquals("A string/char is required; received [5]", siae.getMessage()); siae = expectThrows(QlIllegalArgumentException.class, () -> new Length(EMPTY, l(true)).makePipe().asProcessor().process(null)); assertEquals("A string/char is required; received [true]", siae.getMessage()); @@ -35,8 +37,10 @@ public void testLengthFunctionInputsValidation() { public void testLengthFunctionWithRandomInvalidDataType() { Literal literal = randomValueOtherThanMany(v -> v.dataType() == KEYWORD, () -> LiteralTests.randomLiteral()); - QlIllegalArgumentException siae = expectThrows(QlIllegalArgumentException.class, - () -> new Length(EMPTY, literal).makePipe().asProcessor().process(null)); + QlIllegalArgumentException siae = expectThrows( + QlIllegalArgumentException.class, + () -> new Length(EMPTY, literal).makePipe().asProcessor().process(null) + ); assertThat(siae.getMessage(), startsWith("A string/char is required; received")); } } diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/StartsWithFunctionProcessorTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/StartsWithFunctionProcessorTests.java index 521a0cb19cdb8..3600e5efe6c25 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/StartsWithFunctionProcessorTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/StartsWithFunctionProcessorTests.java @@ -7,5 +7,5 @@ package org.elasticsearch.xpack.eql.expression.function.scalar.string; -public class StartsWithFunctionProcessorTests extends org.elasticsearch.xpack.ql.expression.function.scalar.string.StartsWithProcessorTests{ -} +public class StartsWithFunctionProcessorTests extends + org.elasticsearch.xpack.ql.expression.function.scalar.string.StartsWithProcessorTests {} diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/StringContainsFunctionPipeTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/StringContainsFunctionPipeTests.java index aa19b1419de28..a4e067c4a4ddf 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/StringContainsFunctionPipeTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/StringContainsFunctionPipeTests.java @@ -33,11 +33,12 @@ private Expression randomStringContainsFunctionExpression() { } public static StringContainsFunctionPipe randomStringContainsFunctionPipe() { - return (StringContainsFunctionPipe) (new StringContains(randomSource(), + return (StringContainsFunctionPipe) (new StringContains( + randomSource(), randomStringLiteral(), randomStringLiteral(), - randomBoolean()) - .makePipe()); + randomBoolean() + ).makePipe()); } @Override @@ -47,23 +48,18 @@ public void testTransform() { StringContainsFunctionPipe b1 = randomInstance(); Expression newExpression = randomValueOtherThan(b1.expression(), () -> randomStringContainsFunctionExpression()); StringContainsFunctionPipe newB = new StringContainsFunctionPipe( - b1.source(), - newExpression, - b1.string(), - b1.substring(), - b1.isCaseInsensitive()); + b1.source(), + newExpression, + b1.string(), + b1.substring(), + b1.isCaseInsensitive() + ); assertEquals(newB, b1.transformPropertiesOnly(Expression.class, v -> Objects.equals(v, b1.expression()) ? newExpression : v)); StringContainsFunctionPipe b2 = randomInstance(); Source newLoc = randomValueOtherThan(b2.source(), () -> randomSource()); - newB = new StringContainsFunctionPipe( - newLoc, - b2.expression(), - b2.string(), - b2.substring(), - b2.isCaseInsensitive()); - assertEquals(newB, - b2.transformPropertiesOnly(Source.class, v -> Objects.equals(v, b2.source()) ? newLoc : v)); + newB = new StringContainsFunctionPipe(newLoc, b2.expression(), b2.string(), b2.substring(), b2.isCaseInsensitive()); + assertEquals(newB, b2.transformPropertiesOnly(Source.class, v -> Objects.equals(v, b2.source()) ? newLoc : v)); } @Override @@ -72,8 +68,13 @@ public void testReplaceChildren() { Pipe newString = pipe(((Expression) randomValueOtherThan(b.string(), () -> randomStringLiteral()))); Pipe newSubstring = pipe(((Expression) randomValueOtherThan(b.substring(), () -> randomStringLiteral()))); boolean newCaseSensitive = randomValueOtherThan(b.isCaseInsensitive(), () -> randomBoolean()); - StringContainsFunctionPipe newB = - new StringContainsFunctionPipe(b.source(), b.expression(), b.string(), b.substring(), newCaseSensitive); + StringContainsFunctionPipe newB = new StringContainsFunctionPipe( + b.source(), + b.expression(), + b.string(), + b.substring(), + newCaseSensitive + ); StringContainsFunctionPipe transformed = newB.replaceChildren(newString, b.substring()); assertEquals(transformed.string(), newString); @@ -97,31 +98,45 @@ public void testReplaceChildren() { @Override protected StringContainsFunctionPipe mutate(StringContainsFunctionPipe instance) { List> randoms = new ArrayList<>(); - randoms.add(f -> new StringContainsFunctionPipe(f.source(), + randoms.add( + f -> new StringContainsFunctionPipe( + f.source(), f.expression(), pipe(((Expression) randomValueOtherThan(f.string(), () -> randomStringLiteral()))), f.substring(), - randomValueOtherThan(f.isCaseInsensitive(), () -> randomBoolean()))); - randoms.add(f -> new StringContainsFunctionPipe(f.source(), + randomValueOtherThan(f.isCaseInsensitive(), () -> randomBoolean()) + ) + ); + randoms.add( + f -> new StringContainsFunctionPipe( + f.source(), f.expression(), f.string(), pipe(((Expression) randomValueOtherThan(f.substring(), () -> randomStringLiteral()))), - randomValueOtherThan(f.isCaseInsensitive(), () -> randomBoolean()))); - randoms.add(f -> new StringContainsFunctionPipe(f.source(), + randomValueOtherThan(f.isCaseInsensitive(), () -> randomBoolean()) + ) + ); + randoms.add( + f -> new StringContainsFunctionPipe( + f.source(), f.expression(), pipe(((Expression) randomValueOtherThan(f.string(), () -> randomStringLiteral()))), pipe(((Expression) randomValueOtherThan(f.substring(), () -> randomStringLiteral()))), - randomValueOtherThan(f.isCaseInsensitive(), () -> randomBoolean()))); + randomValueOtherThan(f.isCaseInsensitive(), () -> randomBoolean()) + ) + ); return randomFrom(randoms).apply(instance); } @Override protected StringContainsFunctionPipe copy(StringContainsFunctionPipe instance) { - return new StringContainsFunctionPipe(instance.source(), - instance.expression(), - instance.string(), - instance.substring(), - instance.isCaseInsensitive()); + return new StringContainsFunctionPipe( + instance.source(), + instance.expression(), + instance.string(), + instance.substring(), + instance.isCaseInsensitive() + ); } } diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/StringContainsFunctionProcessorTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/StringContainsFunctionProcessorTests.java index 508c148b67672..ca6cf31ac5149 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/StringContainsFunctionProcessorTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/StringContainsFunctionProcessorTests.java @@ -39,8 +39,10 @@ public void testStringContains() throws Exception { // The string parameter can be null. Expect exception if any of other parameters is null. if (string != null && substring == null) { - EqlIllegalArgumentException e = expectThrows(EqlIllegalArgumentException.class, - () -> doProcess(string, substring, insensitive)); + EqlIllegalArgumentException e = expectThrows( + EqlIllegalArgumentException.class, + () -> doProcess(string, substring, insensitive) + ); assertThat(e.getMessage(), equalTo("A string/char is required; received [null]")); } else { assertThat(doProcess(string, substring, insensitive), equalTo(string == null ? null : true)); diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/StringUtilsTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/StringUtilsTests.java index ad7de1e5eee58..bd9fb3f0c5237 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/StringUtilsTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/StringUtilsTests.java @@ -100,46 +100,49 @@ public void testBetweenEmptyNullLeftRight() throws Exception { // Test from EQL doc https://eql.readthedocs.io/en/latest/query-guide/functions.html public void testBetweenBasicEQLExamples() { - assertThat(StringUtils.between("welcome to event query language", " ", " ", false, true), - equalTo("to")); - assertThat(StringUtils.between("welcome to event query language", " ", " ", true, true), - equalTo("to event query")); - assertThat(StringUtils.between("System Idle Process", "s", "e", true, true), - equalTo("ystem Idle Proc")); + assertThat(StringUtils.between("welcome to event query language", " ", " ", false, true), equalTo("to")); + assertThat(StringUtils.between("welcome to event query language", " ", " ", true, true), equalTo("to event query")); + assertThat(StringUtils.between("System Idle Process", "s", "e", true, true), equalTo("ystem Idle Proc")); - assertThat(StringUtils.between("C:\\workspace\\dev\\TestLogs\\something.json", "dev", ".json", false, true), - equalTo("\\TestLogs\\something")); + assertThat( + StringUtils.between("C:\\workspace\\dev\\TestLogs\\something.json", "dev", ".json", false, true), + equalTo("\\TestLogs\\something") + ); - assertThat(StringUtils.between("C:\\workspace\\dev\\TestLogs\\something.json", "dev", ".json", true, true), - equalTo("\\TestLogs\\something")); + assertThat( + StringUtils.between("C:\\workspace\\dev\\TestLogs\\something.json", "dev", ".json", true, true), + equalTo("\\TestLogs\\something") + ); - assertThat(StringUtils.between("System Idle Process", "s", "e", false, true), - equalTo("yst")); + assertThat(StringUtils.between("System Idle Process", "s", "e", false, true), equalTo("yst")); + assertThat( + StringUtils.between("C:\\workspace\\dev\\TestLogs\\something.json", "dev", ".json", false, false), + equalTo("\\TestLogs\\something") + ); - assertThat(StringUtils.between("C:\\workspace\\dev\\TestLogs\\something.json", "dev", ".json", false, false), - equalTo("\\TestLogs\\something")); + assertThat( + StringUtils.between("C:\\workspace\\dev\\TestLogs\\something.json", "Test", ".json", false, false), + equalTo("Logs\\something") + ); - assertThat(StringUtils.between("C:\\workspace\\dev\\TestLogs\\something.json", "Test", ".json", false, false), - equalTo("Logs\\something")); + assertThat(StringUtils.between("C:\\workspace\\dev\\TestLogs\\something.json", "test", ".json", false, false), equalTo("")); - assertThat(StringUtils.between("C:\\workspace\\dev\\TestLogs\\something.json", "test", ".json", false, false), - equalTo("")); + assertThat( + StringUtils.between("C:\\workspace\\dev\\TestLogs\\something.json", "dev", ".json", true, false), + equalTo("\\TestLogs\\something") + ); - assertThat(StringUtils.between("C:\\workspace\\dev\\TestLogs\\something.json", "dev", ".json", true, false), - equalTo("\\TestLogs\\something")); + assertThat( + StringUtils.between("C:\\workspace\\dev\\TestLogs\\something.json", "Test", ".json", true, false), + equalTo("Logs\\something") + ); - assertThat(StringUtils.between("C:\\workspace\\dev\\TestLogs\\something.json", "Test", ".json", true, false), - equalTo("Logs\\something")); + assertThat(StringUtils.between("C:\\workspace\\dev\\TestLogs\\something.json", "test", ".json", true, false), equalTo("")); - assertThat(StringUtils.between("C:\\workspace\\dev\\TestLogs\\something.json", "test", ".json", true, false), - equalTo("")); + assertThat(StringUtils.between("System Idle Process", "S", "e", false, false), equalTo("yst")); - assertThat(StringUtils.between("System Idle Process", "S", "e", false, false), - equalTo("yst")); - - assertThat(StringUtils.between("System Idle Process", "Y", "e", false, false), - equalTo("")); + assertThat(StringUtils.between("System Idle Process", "Y", "e", false, false), equalTo("")); } public void testStringContainsWithNullOrEmpty() { @@ -154,25 +157,28 @@ public void testStringContainsWithNullOrEmpty() { public void testStringContainsWithRandomCaseSensitive() throws Exception { String substring = randomAlphaOfLength(10); - String string = randomValueOtherThan(substring, () -> randomAlphaOfLength(10)) - + substring - + randomValueOtherThan(substring, () -> randomAlphaOfLength(10)); + String string = randomValueOtherThan(substring, () -> randomAlphaOfLength(10)) + substring + randomValueOtherThan( + substring, + () -> randomAlphaOfLength(10) + ); assertTrue(stringContains(string, substring, true)); } public void testStringContainsWithRandomCaseInsensitive() throws Exception { String substring = randomAlphaOfLength(10); String subsChanged = substring.toUpperCase(Locale.ROOT); - String string = randomValueOtherThan(subsChanged, () -> randomAlphaOfLength(10)) - + subsChanged - + randomValueOtherThan(subsChanged, () -> randomAlphaOfLength(10)); + String string = randomValueOtherThan(subsChanged, () -> randomAlphaOfLength(10)) + subsChanged + randomValueOtherThan( + subsChanged, + () -> randomAlphaOfLength(10) + ); assertTrue(stringContains(string, substring, true)); substring = randomAlphaOfLength(10); subsChanged = substring.toLowerCase(Locale.ROOT); - string = randomValueOtherThan(subsChanged, () -> randomAlphaOfLength(10)) - + subsChanged - + randomValueOtherThan(subsChanged, () -> randomAlphaOfLength(10)); + string = randomValueOtherThan(subsChanged, () -> randomAlphaOfLength(10)) + subsChanged + randomValueOtherThan( + subsChanged, + () -> randomAlphaOfLength(10) + ); assertTrue(stringContains(string, substring, true)); } } diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/SubstringFunctionPipeTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/SubstringFunctionPipeTests.java index dc7288bb03765..3eece45c7dacc 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/SubstringFunctionPipeTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/SubstringFunctionPipeTests.java @@ -36,11 +36,12 @@ private Expression randomSubstringFunctionExpression() { } public static SubstringFunctionPipe randomSubstringFunctionPipe() { - return (SubstringFunctionPipe) (new Substring(randomSource(), - randomStringLiteral(), - randomIntLiteral(), - randomFrom(true, false) ? randomIntLiteral() : null) - .makePipe()); + return (SubstringFunctionPipe) (new Substring( + randomSource(), + randomStringLiteral(), + randomIntLiteral(), + randomFrom(true, false) ? randomIntLiteral() : null + ).makePipe()); } @Override @@ -49,23 +50,13 @@ public void testTransform() { // skipping the children (input, start, end) which are tested separately SubstringFunctionPipe b1 = randomInstance(); Expression newExpression = randomValueOtherThan(b1.expression(), () -> randomSubstringFunctionExpression()); - SubstringFunctionPipe newB = new SubstringFunctionPipe( - b1.source(), - newExpression, - b1.input(), - b1.start(), - b1.end()); + SubstringFunctionPipe newB = new SubstringFunctionPipe(b1.source(), newExpression, b1.input(), b1.start(), b1.end()); assertEquals(newB, b1.transformPropertiesOnly(Expression.class, v -> Objects.equals(v, b1.expression()) ? newExpression : v)); SubstringFunctionPipe b2 = randomInstance(); Source newLoc = randomValueOtherThan(b2.source(), () -> randomSource()); - newB = new SubstringFunctionPipe( - newLoc, - b2.expression(), - b2.input(), - b2.start(), - b2.end()); + newB = new SubstringFunctionPipe(newLoc, b2.expression(), b2.input(), b2.start(), b2.end()); assertEquals(newB, b2.transformPropertiesOnly(Source.class, v -> Objects.equals(v, b2.source()) ? newLoc : v)); } @@ -81,13 +72,10 @@ public void testReplaceChildren() { SubstringFunctionPipe transformed = null; // generate all the combinations of possible children modifications and test all of them - for(int i = 1; i < 4; i++) { - for(BitSet comb : new Combinations(3, i)) { + for (int i = 1; i < 4; i++) { + for (BitSet comb : new Combinations(3, i)) { Pipe tempNewEnd = b.end() == null ? b.end() : (comb.get(2) ? newEnd : b.end()); - transformed = newB.replaceChildren( - comb.get(0) ? newInput : b.input(), - comb.get(1) ? newStart : b.start(), - tempNewEnd); + transformed = newB.replaceChildren(comb.get(0) ? newInput : b.input(), comb.get(1) ? newStart : b.start(), tempNewEnd); assertEquals(transformed.input(), comb.get(0) ? newInput : b.input()); assertEquals(transformed.start(), comb.get(1) ? newStart : b.start()); @@ -102,23 +90,31 @@ public void testReplaceChildren() { protected SubstringFunctionPipe mutate(SubstringFunctionPipe instance) { List> randoms = new ArrayList<>(); if (instance.end() == null) { - for(int i = 1; i < 3; i++) { - for(BitSet comb : new Combinations(2, i)) { - randoms.add(f -> new SubstringFunctionPipe(f.source(), + for (int i = 1; i < 3; i++) { + for (BitSet comb : new Combinations(2, i)) { + randoms.add( + f -> new SubstringFunctionPipe( + f.source(), f.expression(), comb.get(0) ? randomValueOtherThan(f.input(), () -> pipe(randomStringLiteral())) : f.input(), comb.get(1) ? randomValueOtherThan(f.start(), () -> pipe(randomIntLiteral())) : f.start(), - null)); + null + ) + ); } } } else { - for(int i = 1; i < 4; i++) { - for(BitSet comb : new Combinations(3, i)) { - randoms.add(f -> new SubstringFunctionPipe(f.source(), + for (int i = 1; i < 4; i++) { + for (BitSet comb : new Combinations(3, i)) { + randoms.add( + f -> new SubstringFunctionPipe( + f.source(), f.expression(), comb.get(0) ? randomValueOtherThan(f.input(), () -> pipe(randomStringLiteral())) : f.input(), comb.get(1) ? randomValueOtherThan(f.start(), () -> pipe(randomIntLiteral())) : f.start(), - comb.get(2) ? randomValueOtherThan(f.end(), () -> pipe(randomIntLiteral())) : f.end())); + comb.get(2) ? randomValueOtherThan(f.end(), () -> pipe(randomIntLiteral())) : f.end() + ) + ); } } } @@ -128,10 +124,6 @@ protected SubstringFunctionPipe mutate(SubstringFunctionPipe instance) { @Override protected SubstringFunctionPipe copy(SubstringFunctionPipe instance) { - return new SubstringFunctionPipe(instance.source(), - instance.expression(), - instance.input(), - instance.start(), - instance.end()); + return new SubstringFunctionPipe(instance.source(), instance.expression(), instance.input(), instance.start(), instance.end()); } } diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/SubstringFunctionProcessorTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/SubstringFunctionProcessorTests.java index 5593fc6457057..4ddc7cd76589e 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/SubstringFunctionProcessorTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/SubstringFunctionProcessorTests.java @@ -16,14 +16,20 @@ public class SubstringFunctionProcessorTests extends ESTestCase { public void testSubstringFunctionInputsValidation() { - QlIllegalArgumentException siae = expectThrows(QlIllegalArgumentException.class, - () -> new Substring(EMPTY, l(5), l("foo"), null).makePipe().asProcessor().process(null)); + QlIllegalArgumentException siae = expectThrows( + QlIllegalArgumentException.class, + () -> new Substring(EMPTY, l(5), l("foo"), null).makePipe().asProcessor().process(null) + ); assertEquals("A string/char is required; received [5]", siae.getMessage()); - siae = expectThrows(QlIllegalArgumentException.class, - () -> new Substring(EMPTY, l("bar"), l(false), null).makePipe().asProcessor().process(null)); + siae = expectThrows( + QlIllegalArgumentException.class, + () -> new Substring(EMPTY, l("bar"), l(false), null).makePipe().asProcessor().process(null) + ); assertEquals("A number is required; received [false]", siae.getMessage()); - siae = expectThrows(QlIllegalArgumentException.class, - () -> new Substring(EMPTY, l("foo"), l(1), l("abc")).makePipe().asProcessor().process(null)); + siae = expectThrows( + QlIllegalArgumentException.class, + () -> new Substring(EMPTY, l("foo"), l(1), l("abc")).makePipe().asProcessor().process(null) + ); assertEquals("A number is required; received [abc]", siae.getMessage()); } } diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/ToStringFunctionPipeTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/ToStringFunctionPipeTests.java index 41c2b8b39c147..09f90c726e456 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/ToStringFunctionPipeTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/function/scalar/string/ToStringFunctionPipeTests.java @@ -39,19 +39,13 @@ public void testTransform() { // skipping the children (the input itself) which are tested separately ToStringFunctionPipe b1 = randomInstance(); Expression newExpression = randomValueOtherThan(b1.expression(), () -> randomToStringFunctionExpression()); - ToStringFunctionPipe newB = new ToStringFunctionPipe( - b1.source(), - newExpression, - b1.input()); + ToStringFunctionPipe newB = new ToStringFunctionPipe(b1.source(), newExpression, b1.input()); assertEquals(newB, b1.transformPropertiesOnly(Expression.class, v -> Objects.equals(v, b1.expression()) ? newExpression : v)); ToStringFunctionPipe b2 = randomInstance(); Source newLoc = randomValueOtherThan(b2.source(), () -> randomSource()); - newB = new ToStringFunctionPipe( - newLoc, - b2.expression(), - b2.input()); + newB = new ToStringFunctionPipe(newLoc, b2.expression(), b2.input()); assertEquals(newB, b2.transformPropertiesOnly(Source.class, v -> Objects.equals(v, b2.source()) ? newLoc : v)); } @@ -71,9 +65,11 @@ public void testReplaceChildren() { @Override protected ToStringFunctionPipe mutate(ToStringFunctionPipe instance) { - return new ToStringFunctionPipe(instance.source(), - instance.expression(), - randomValueOtherThan(instance.input(), () -> pipe(randomStringLiteral()))); + return new ToStringFunctionPipe( + instance.source(), + instance.expression(), + randomValueOtherThan(instance.input(), () -> pipe(randomStringLiteral())) + ); } @Override diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/predicate/operator/comparison/InsensitiveBinaryComparisonPipeTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/predicate/operator/comparison/InsensitiveBinaryComparisonPipeTests.java index 89dd94a2dc3c3..fd5c1b40ab8b1 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/predicate/operator/comparison/InsensitiveBinaryComparisonPipeTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/predicate/operator/comparison/InsensitiveBinaryComparisonPipeTests.java @@ -36,11 +36,12 @@ private Expression randomInsensitiveBinaryComparisonExpression() { } public static InsensitiveBinaryComparisonPipe randomInsensitiveBinaryComparisonPipe() { - return (InsensitiveBinaryComparisonPipe) (new InsensitiveEquals(randomSource(), + return (InsensitiveBinaryComparisonPipe) (new InsensitiveEquals( + randomSource(), randomStringLiteral(), randomStringLiteral(), - TestUtils.UTC). - makePipe()); + TestUtils.UTC + ).makePipe()); } @Override @@ -54,9 +55,12 @@ public void testTransform() { newExpression, pipe.left(), pipe.right(), - pipe.asProcessor().function()); - assertEquals(newPipe, - pipe.transformPropertiesOnly(Expression.class, v -> Objects.equals(v, pipe.expression()) ? newExpression : v)); + pipe.asProcessor().function() + ); + assertEquals( + newPipe, + pipe.transformPropertiesOnly(Expression.class, v -> Objects.equals(v, pipe.expression()) ? newExpression : v) + ); InsensitiveBinaryComparisonPipe anotherPipe = randomInstance(); Source newLoc = randomValueOtherThan(anotherPipe.source(), SourceTests::randomSource); @@ -65,9 +69,9 @@ public void testTransform() { anotherPipe.expression(), anotherPipe.left(), anotherPipe.right(), - anotherPipe.asProcessor().function()); - assertEquals(newPipe, - anotherPipe.transformPropertiesOnly(Source.class, v -> Objects.equals(v, anotherPipe.source()) ? newLoc : v)); + anotherPipe.asProcessor().function() + ); + assertEquals(newPipe, anotherPipe.transformPropertiesOnly(Source.class, v -> Objects.equals(v, anotherPipe.source()) ? newLoc : v)); } @Override @@ -75,8 +79,13 @@ public void testReplaceChildren() { InsensitiveBinaryComparisonPipe pipe = randomInstance(); Pipe newLeft = pipe(((Expression) randomValueOtherThan(pipe.left(), FunctionTestUtils::randomStringLiteral))); Pipe newRight = pipe(((Expression) randomValueOtherThan(pipe.right(), FunctionTestUtils::randomStringLiteral))); - InsensitiveBinaryComparisonPipe newPipe = - new InsensitiveBinaryComparisonPipe(pipe.source(), pipe.expression(), pipe.left(), pipe.right(), pipe.asProcessor().function()); + InsensitiveBinaryComparisonPipe newPipe = new InsensitiveBinaryComparisonPipe( + pipe.source(), + pipe.expression(), + pipe.left(), + pipe.right(), + pipe.asProcessor().function() + ); InsensitiveBinaryComparisonPipe transformed = newPipe.replaceChildren(newLeft, pipe.right()); assertEquals(transformed.source(), pipe.source()); @@ -100,31 +109,45 @@ public void testReplaceChildren() { @Override protected InsensitiveBinaryComparisonPipe mutate(InsensitiveBinaryComparisonPipe instance) { List> randoms = new ArrayList<>(); - randoms.add(f -> new InsensitiveBinaryComparisonPipe(f.source(), - f.expression(), - pipe(((Expression) randomValueOtherThan(f.left(), FunctionTestUtils::randomStringLiteral))), - f.right(), - f.asProcessor().function())); - randoms.add(f -> new InsensitiveBinaryComparisonPipe(f.source(), - f.expression(), - f.left(), - pipe(((Expression) randomValueOtherThan(f.right(), FunctionTestUtils::randomStringLiteral))), - f.asProcessor().function())); - randoms.add(f -> new InsensitiveBinaryComparisonPipe(f.source(), - f.expression(), - pipe(((Expression) randomValueOtherThan(f.left(), FunctionTestUtils::randomStringLiteral))), - pipe(((Expression) randomValueOtherThan(f.right(), FunctionTestUtils::randomStringLiteral))), - f.asProcessor().function())); + randoms.add( + f -> new InsensitiveBinaryComparisonPipe( + f.source(), + f.expression(), + pipe(((Expression) randomValueOtherThan(f.left(), FunctionTestUtils::randomStringLiteral))), + f.right(), + f.asProcessor().function() + ) + ); + randoms.add( + f -> new InsensitiveBinaryComparisonPipe( + f.source(), + f.expression(), + f.left(), + pipe(((Expression) randomValueOtherThan(f.right(), FunctionTestUtils::randomStringLiteral))), + f.asProcessor().function() + ) + ); + randoms.add( + f -> new InsensitiveBinaryComparisonPipe( + f.source(), + f.expression(), + pipe(((Expression) randomValueOtherThan(f.left(), FunctionTestUtils::randomStringLiteral))), + pipe(((Expression) randomValueOtherThan(f.right(), FunctionTestUtils::randomStringLiteral))), + f.asProcessor().function() + ) + ); return randomFrom(randoms).apply(instance); } @Override protected InsensitiveBinaryComparisonPipe copy(InsensitiveBinaryComparisonPipe instance) { - return new InsensitiveBinaryComparisonPipe(instance.source(), + return new InsensitiveBinaryComparisonPipe( + instance.source(), instance.expression(), instance.left(), instance.right(), - instance.asProcessor().function()); + instance.asProcessor().function() + ); } } diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/predicate/operator/comparison/InsensitiveBinaryComparisonProcessorTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/predicate/operator/comparison/InsensitiveBinaryComparisonProcessorTests.java index b715536cd8c6d..13abf95e5f1e1 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/predicate/operator/comparison/InsensitiveBinaryComparisonProcessorTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/expression/predicate/operator/comparison/InsensitiveBinaryComparisonProcessorTests.java @@ -24,7 +24,8 @@ public static InsensitiveBinaryComparisonProcessor randomProcessor() { return new InsensitiveBinaryComparisonProcessor( new ConstantProcessor(randomLong()), new ConstantProcessor(randomLong()), - randomFrom(InsensitiveBinaryComparisonProcessor.InsensitiveBinaryComparisonOperation.values())); + randomFrom(InsensitiveBinaryComparisonProcessor.InsensitiveBinaryComparisonOperation.values()) + ); } @Override @@ -79,9 +80,9 @@ public void testNullStringNotEquals() { } public void testRegularNotEquals() { - expectThrows(EqlIllegalArgumentException.class, () -> p(sneq(l(12), l(12)))); - expectThrows(EqlIllegalArgumentException.class, () -> p(sneq(l(12), l("12")))); - expectThrows(EqlIllegalArgumentException.class, () -> p(sneq(l("12"), l(12)))); + expectThrows(EqlIllegalArgumentException.class, () -> p(sneq(l(12), l(12)))); + expectThrows(EqlIllegalArgumentException.class, () -> p(sneq(l(12), l("12")))); + expectThrows(EqlIllegalArgumentException.class, () -> p(sneq(l("12"), l(12)))); } private static Literal l(Object value) { diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/optimizer/EqlFoldSpecLoader.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/optimizer/EqlFoldSpecLoader.java index 62098e0390c81..57610dd7039d4 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/optimizer/EqlFoldSpecLoader.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/optimizer/EqlFoldSpecLoader.java @@ -15,7 +15,6 @@ import java.util.ArrayList; import java.util.List; - public class EqlFoldSpecLoader { public static List load(String path) throws Exception { try (InputStream is = EqlFoldSpecLoader.class.getResourceAsStream(path)) { diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/optimizer/OptimizerTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/optimizer/OptimizerTests.java index 85174fac7ea04..3d8a6e346bc0c 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/optimizer/OptimizerTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/optimizer/OptimizerTests.java @@ -91,8 +91,9 @@ private LogicalPlan accept(IndexResolution resolution, String eql) { PreAnalyzer preAnalyzer = new PreAnalyzer(); PostAnalyzer postAnalyzer = new PostAnalyzer(); Analyzer analyzer = new Analyzer(TEST_CFG, new EqlFunctionRegistry(), new Verifier(new Metrics())); - return optimizer.optimize(postAnalyzer.postAnalyze(analyzer.analyze(preAnalyzer.preAnalyze(parser.createStatement(eql), - resolution)), TEST_CFG)); + return optimizer.optimize( + postAnalyzer.postAnalyze(analyzer.analyze(preAnalyzer.preAnalyze(parser.createStatement(eql), resolution)), TEST_CFG) + ); } private LogicalPlan accept(String eql) { @@ -100,10 +101,7 @@ private LogicalPlan accept(String eql) { } public void testIsNull() { - List tests = asList( - "foo where command_line == null", - "foo where null == command_line" - ); + List tests = asList("foo where command_line == null", "foo where null == command_line"); for (String q : tests) { LogicalPlan plan = defaultPipes(accept(q)); @@ -119,10 +117,7 @@ public void testIsNull() { } public void testIsNotNull() { - List tests = asList( - "foo where command_line != null", - "foo where null != command_line" - ); + List tests = asList("foo where command_line != null", "foo where null != command_line"); for (String q : tests) { LogicalPlan plan = defaultPipes(accept(q)); @@ -170,17 +165,13 @@ public void testEqualsWildcardQuestionmarkOnRight() { Like like = (Like) condition.right(); assertEquals("command_line", ((FieldAttribute) like.field()).name()); - assertEquals( "^. bar .$", like.pattern().asJavaRegex()); + assertEquals("^. bar .$", like.pattern().asJavaRegex()); assertEquals("? bar ?", like.pattern().asLuceneWildcard()); - assertEquals( "* bar *", like.pattern().asIndexNameWildcard()); + assertEquals("* bar *", like.pattern().asIndexNameWildcard()); } public void testEqualsWildcardWithLiteralsOnLeft() { - List tests = asList( - "foo where \"abc\": \"*b*\"", - "foo where \"abc\": \"ab*\"", - "foo where \"abc\": \"*bc\"" - ); + List tests = asList("foo where \"abc\": \"*b*\"", "foo where \"abc\": \"ab*\"", "foo where \"abc\": \"*bc\""); for (String q : tests) { LogicalPlan plan = accept(q); diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/optimizer/TomlFoldTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/optimizer/TomlFoldTests.java index 87e509acf75ef..dc10a99b9638f 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/optimizer/TomlFoldTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/optimizer/TomlFoldTests.java @@ -62,15 +62,16 @@ public static List readTestSpecs() throws Exception { private static List asArray(Collection specs) { AtomicInteger counter = new AtomicInteger(); - return specs.stream().map(spec -> new Object[] { - counter.incrementAndGet(), spec - }).collect(toList()); + return specs.stream().map(spec -> new Object[] { counter.incrementAndGet(), spec }).collect(toList()); } public void test() { Expression expr = PARSER.createExpression(spec.expression()); - LogicalPlan logicalPlan = new Project(EMPTY, new LocalRelation(EMPTY, emptyList()), - singletonList(new Alias(Source.EMPTY, "test", expr))); + LogicalPlan logicalPlan = new Project( + EMPTY, + new LocalRelation(EMPTY, emptyList()), + singletonList(new Alias(Source.EMPTY, "test", expr)) + ); LogicalPlan analyzed = ANALYZER.analyze(logicalPlan); assertTrue(analyzed instanceof Project); @@ -84,7 +85,7 @@ public void test() { // upgrade to a long, because the parser typically downgrades Long -> Integer when possible if (folded instanceof Integer) { - folded = ((Integer) folded).longValue(); + folded = ((Integer) folded).longValue(); } assertEquals(spec.expected(), folded); diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/parser/ExpressionTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/parser/ExpressionTests.java index d9243f8bf1af1..4693030024627 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/parser/ExpressionTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/parser/ExpressionTests.java @@ -5,7 +5,6 @@ * 2.0. */ - package org.elasticsearch.xpack.eql.parser; import org.elasticsearch.test.ESTestCase; @@ -100,11 +99,9 @@ public void testLiterals() { public void testSingleQuotedStringForbidden() { ParsingException e = expectThrows(ParsingException.class, () -> expr("'hello world'")); - assertEquals("line 1:2: Use double quotes [\"] to define string literals, not single quotes [']", - e.getMessage()); + assertEquals("line 1:2: Use double quotes [\"] to define string literals, not single quotes [']", e.getMessage()); e = expectThrows(ParsingException.class, () -> parser.createStatement("process where name=='hello world'")); - assertEquals("line 1:22: Use double quotes [\"] to define string literals, not single quotes [']", - e.getMessage()); + assertEquals("line 1:22: Use double quotes [\"] to define string literals, not single quotes [']", e.getMessage()); } public void testDoubleQuotedString() { @@ -116,20 +113,16 @@ public void testDoubleQuotedString() { public void testSingleQuotedUnescapedStringDisallowed() { ParsingException e = expectThrows(ParsingException.class, () -> expr("?'hello world'")); - assertEquals("line 1:2: Use triple double quotes [\"\"\"] to define unescaped string literals, not [?']", - e.getMessage()); + assertEquals("line 1:2: Use triple double quotes [\"\"\"] to define unescaped string literals, not [?']", e.getMessage()); e = expectThrows(ParsingException.class, () -> parser.createStatement("process where name == ?'hello world'")); - assertEquals("line 1:24: Use triple double quotes [\"\"\"] to define unescaped string literals, not [?']", - e.getMessage()); + assertEquals("line 1:24: Use triple double quotes [\"\"\"] to define unescaped string literals, not [?']", e.getMessage()); } public void testDoubleQuotedUnescapedStringForbidden() { ParsingException e = expectThrows(ParsingException.class, () -> expr("?\"hello world\"")); - assertEquals("line 1:2: Use triple double quotes [\"\"\"] to define unescaped string literals, not [?\"]", - e.getMessage()); + assertEquals("line 1:2: Use triple double quotes [\"\"\"] to define unescaped string literals, not [?\"]", e.getMessage()); e = expectThrows(ParsingException.class, () -> parser.createStatement("process where name == ?\"hello world\"")); - assertEquals("line 1:24: Use triple double quotes [\"\"\"] to define unescaped string literals, not [?\"]", - e.getMessage()); + assertEquals("line 1:24: Use triple double quotes [\"\"\"] to define unescaped string literals, not [?\"]", e.getMessage()); } public void testTripleDoubleQuotedUnescapedString() { @@ -159,8 +152,8 @@ public void testTripleDoubleQuotedUnescapedString() { // """""\""hello\\""\""world!\\""""" == """\\""\""foo""\\""\"bar""\\""\"""" => // ""\""hello\\""\""world!\\"" == \\""\""foo""\\""\"bar""\\""\" - str = " \"\"\"\"\"\\\"\"hello\\\\\"\"\\\"\"world!\\\\\"\"\"\"\" == " + - " \"\"\"\\\\\"\"\\\"\"foo\"\"\\\\\"\"\\\"bar\"\"\\\\\"\"\\\"\"\"\" "; + str = " \"\"\"\"\"\\\"\"hello\\\\\"\"\\\"\"world!\\\\\"\"\"\"\" == " + + " \"\"\"\\\\\"\"\\\"\"foo\"\"\\\\\"\"\\\"bar\"\"\\\\\"\"\\\"\"\"\" "; expectedStrLeft = "\"\"\\\"\"hello\\\\\"\"\\\"\"world!\\\\\"\""; expectedStrRight = "\\\\\"\"\\\"\"foo\"\"\\\\\"\"\\\"bar\"\"\\\\\"\"\\\""; parsed = expr(str); @@ -172,23 +165,35 @@ public void testTripleDoubleQuotedUnescapedString() { assertEquals(expectedStrRight, ((Literal) eq.right()).value()); // """"""hello world!""" == """foobar""" - ParsingException e = expectThrows(ParsingException.class, "Expected syntax error", - () -> expr("\"\"\"\"\"\"hello world!\"\"\" == \"\"\"foobar\"\"\"")); + ParsingException e = expectThrows( + ParsingException.class, + "Expected syntax error", + () -> expr("\"\"\"\"\"\"hello world!\"\"\" == \"\"\"foobar\"\"\"") + ); assertThat(e.getMessage(), startsWith("line 1:7: mismatched input 'hello' expecting {,")); // """""\"hello world!"""""" == """foobar""" - e = expectThrows(ParsingException.class, "Expected syntax error", - () -> expr("\"\"\"\"\"\\\"hello world!\"\"\"\"\"\" == \"\"\"foobar\"\"\"")); + e = expectThrows( + ParsingException.class, + "Expected syntax error", + () -> expr("\"\"\"\"\"\\\"hello world!\"\"\"\"\"\" == \"\"\"foobar\"\"\"") + ); assertThat(e.getMessage(), startsWith("line 1:25: mismatched input '\" == \"' expecting {,")); // """""\"hello world!""\"""" == """"""foobar""" - e = expectThrows(ParsingException.class, "Expected syntax error", - () -> expr("\"\"\"\"\"\\\"hello world!\"\"\\\"\"\"\" == \"\"\"\"\"\"foobar\"\"\"")); + e = expectThrows( + ParsingException.class, + "Expected syntax error", + () -> expr("\"\"\"\"\"\\\"hello world!\"\"\\\"\"\"\" == \"\"\"\"\"\"foobar\"\"\"") + ); assertThat(e.getMessage(), startsWith("line 1:37: mismatched input 'foobar' expecting {,")); // """""\"hello world!""\"""" == """""\"foobar\"\"""""" - e = expectThrows(ParsingException.class, "Expected syntax error", - () -> expr("\"\"\"\"\"\\\"hello world!\"\"\\\"\"\"\" == \"\"\"\"\"\\\"foobar\\\"\\\"\"\"\"\"\"")); + e = expectThrows( + ParsingException.class, + "Expected syntax error", + () -> expr("\"\"\"\"\"\\\"hello world!\"\"\\\"\"\"\" == \"\"\"\"\"\\\"foobar\\\"\\\"\"\"\"\"\"") + ); assertEquals("line 1:52: token recognition error at: '\"'", e.getMessage()); } @@ -201,28 +206,24 @@ public void testUnicodeWithWrongHexDigits() { } public void testUnicodeWithWrongNumberOfHexDigits() { - ParsingException e = expectThrows(ParsingException.class, "Expected syntax error", - () -> expr("\"\\u{}\"")); + ParsingException e = expectThrows(ParsingException.class, "Expected syntax error", () -> expr("\"\\u{}\"")); assertEquals("line 1:1: token recognition error at: '\"\\u{}'", e.getMessage()); String[] strings = new String[] { "\\u{D}", "\\u{123456789}", "\\u{123456789A}" }; for (String str : strings) { e = expectThrows(ParsingException.class, "Expected syntax error", () -> expr("\"" + str + "\"")); - assertEquals("line 1:2: Unicode sequence should use [2-8] hex digits, [" + str + "] has [" + (str.length() - 4) + "]", - e.getMessage()); + assertEquals( + "line 1:2: Unicode sequence should use [2-8] hex digits, [" + str + "] has [" + (str.length() - 4) + "]", + e.getMessage() + ); } } public void testUnicodeWithWrongCurlyBraces() { - ParsingException e = expectThrows(ParsingException.class, "Expected syntax error", - () -> expr("\"\\u{}\"")); + ParsingException e = expectThrows(ParsingException.class, "Expected syntax error", () -> expr("\"\\u{}\"")); assertEquals("line 1:1: token recognition error at: '\"\\u{}'", e.getMessage()); - String[][] strings = new String[][] { - { "\\uad12", "\\ua" }, - { "\\u{DA12", "\\u{DA12\"" }, - { "\\u01f0}", "\\u0" } - }; + String[][] strings = new String[][] { { "\\uad12", "\\ua" }, { "\\u{DA12", "\\u{DA12\"" }, { "\\u01f0}", "\\u0" } }; for (String[] str : strings) { e = expectThrows(ParsingException.class, "Expected syntax error", () -> expr("\"" + str[0] + "\"")); assertEquals("line 1:1: token recognition error at: '\"" + str[1] + "'", e.getMessage()); @@ -230,26 +231,19 @@ public void testUnicodeWithWrongCurlyBraces() { } public void testUnicodeWithInvalidUnicodePoints() { - String[] strings = new String[] { - "\\u{10000000}", - "\\u{FFFFFFFa}", - "\\u{FFFF0000}", - }; + String[] strings = new String[] { "\\u{10000000}", "\\u{FFFFFFFa}", "\\u{FFFF0000}", }; for (String str : strings) { ParsingException e = expectThrows(ParsingException.class, "Expected syntax error", () -> expr("\"" + str + "\"")); - assertEquals("line 1:2: Invalid unicode character code [" + str.substring(3, str.length() - 1) +"]", e.getMessage()); + assertEquals("line 1:2: Invalid unicode character code [" + str.substring(3, str.length() - 1) + "]", e.getMessage()); } - strings = new String[] { - "\\u{d800}", - "\\u{dB12}", - "\\u{DcF7}", - "\\u{dFFF}", - }; + strings = new String[] { "\\u{d800}", "\\u{dB12}", "\\u{DcF7}", "\\u{dFFF}", }; for (String str : strings) { ParsingException e = expectThrows(ParsingException.class, "Expected syntax error", () -> expr("\"" + str + "\"")); - assertEquals("line 1:2: Invalid unicode character code, [" + str.substring(3, str.length() - 1) +"] is a surrogate code", - e.getMessage()); + assertEquals( + "line 1:2: Invalid unicode character code, [" + str.substring(3, str.length() - 1) + "] is a surrogate code", + e.getMessage() + ); } } @@ -277,14 +271,13 @@ public void testStringWithUnicodeEscapedChars() { { "\\u{00007c71}", "籱" }, { "\\u{1680B}", "𖠋" }, { "\\u{01f4a9}", "💩" }, - { "\\u{0010989}", "\uD802\uDD89"}, - { "\\u{d7FF}", "\uD7FF"}, - { "\\u{e000}", "\uE000"}, - { "\\u{00}", "\u0000"}, - { "\\u{0000}", "\u0000"}, - { "\\u{000000}", "\u0000"}, - { "\\u{00000000}", "\u0000"}, - }; + { "\\u{0010989}", "\uD802\uDD89" }, + { "\\u{d7FF}", "\uD7FF" }, + { "\\u{e000}", "\uE000" }, + { "\\u{00}", "\u0000" }, + { "\\u{0000}", "\u0000" }, + { "\\u{000000}", "\u0000" }, + { "\\u{00000000}", "\u0000" }, }; StringBuilder sbExpected = new StringBuilder(); StringBuilder sbInput = new StringBuilder(); @@ -348,38 +341,34 @@ public void testBackQuotedIdentifierWithEscapedBackQuote() { } public void testBackQuotedIdentifierWithUnescapedBackQuotes() { - ParsingException e = expectThrows(ParsingException.class, "Expected syntax error", - () -> expr("`wrong_identifier == true")); + ParsingException e = expectThrows(ParsingException.class, "Expected syntax error", () -> expr("`wrong_identifier == true")); assertEquals("line 1:1: token recognition error at: '`wrong_identifier == true'", e.getMessage()); - e = expectThrows(ParsingException.class, "Expected syntax error", - () -> expr("``wrong_identifier == true")); + e = expectThrows(ParsingException.class, "Expected syntax error", () -> expr("``wrong_identifier == true")); assertThat(e.getMessage(), startsWith("line 1:3: mismatched input 'wrong_identifier' expecting {, ")); - e = expectThrows(ParsingException.class, "Expected syntax error", - () -> expr("``wrong_identifier` == true")); + e = expectThrows(ParsingException.class, "Expected syntax error", () -> expr("``wrong_identifier` == true")); assertEquals("line 1:19: token recognition error at: '` == true'", e.getMessage()); - e = expectThrows(ParsingException.class, "Expected syntax error", - () -> expr("`wrong`identifier` == true")); + e = expectThrows(ParsingException.class, "Expected syntax error", () -> expr("`wrong`identifier` == true")); assertEquals("line 1:18: token recognition error at: '` == true'", e.getMessage()); - e = expectThrows(ParsingException.class, "Expected syntax error", - () -> expr("wrong_identifier` == true")); + e = expectThrows(ParsingException.class, "Expected syntax error", () -> expr("wrong_identifier` == true")); assertEquals("line 1:17: token recognition error at: '` == true'", e.getMessage()); - e = expectThrows(ParsingException.class, "Expected syntax error", - () -> expr("wrong_identifier`` == true")); + e = expectThrows(ParsingException.class, "Expected syntax error", () -> expr("wrong_identifier`` == true")); assertThat(e.getMessage(), startsWith("line 1:17: mismatched input '``' expecting {,")); - e = expectThrows(ParsingException.class, "Expected syntax error", - () -> expr("`wrong_identifier`` == true")); + e = expectThrows(ParsingException.class, "Expected syntax error", () -> expr("`wrong_identifier`` == true")); assertEquals("line 1:19: token recognition error at: '` == true'", e.getMessage()); } public void testIdentifierForEventTypeDisallowed() { - ParsingException e = expectThrows(ParsingException.class, "Expected syntax error", - () -> parser.createStatement("`identifier` where foo == true")); + ParsingException e = expectThrows( + ParsingException.class, + "Expected syntax error", + () -> parser.createStatement("`identifier` where foo == true") + ); assertThat(e.getMessage(), startsWith("line 1:1: mismatched input '`identifier`' expecting")); } @@ -408,8 +397,7 @@ public void testComparison() { assertEquals(new GreaterThan(null, field, value, UTC), expr(fieldText + ">" + valueText)); assertEquals(new LessThan(null, field, value, UTC), expr(fieldText + "<" + valueText)); - expectThrows(ParsingException.class, "Expected syntax error", - () -> expr(fieldText + "=" + valueText)); + expectThrows(ParsingException.class, "Expected syntax error", () -> expr(fieldText + "=" + valueText)); } public void testBoolean() { @@ -427,19 +415,10 @@ public void testBoolean() { } public void testInSet() { - assertEquals( - expr("name in (1)"), - new In(null, expr("name"), exprs("1")) - ); + assertEquals(expr("name in (1)"), new In(null, expr("name"), exprs("1"))); - assertEquals( - expr("name in (2, 1)"), - new In(null, expr("name"), exprs("2", "1")) - ); - assertEquals( - expr("name in (\"net.exe\")"), - new In(null, expr("name"), exprs("\"net.exe\"")) - ); + assertEquals(expr("name in (2, 1)"), new In(null, expr("name"), exprs("2", "1"))); + assertEquals(expr("name in (\"net.exe\")"), new In(null, expr("name"), exprs("\"net.exe\""))); assertEquals( expr("name in (\"net.exe\", \"whoami.exe\", \"hostname.exe\")"), @@ -448,29 +427,20 @@ public void testInSet() { } public void testInSetDuplicates() { - assertEquals( - expr("name in (1, 1)"), - new In(null, expr("name"), exprs("1", "1")) - ); + assertEquals(expr("name in (1, 1)"), new In(null, expr("name"), exprs("1", "1"))); - assertEquals( - expr("name in (\"net.exe\", \"net.exe\")"), - new In(null, expr("name"), exprs("\"net.exe\"", "\"net.exe\"")) - ); + assertEquals(expr("name in (\"net.exe\", \"net.exe\")"), new In(null, expr("name"), exprs("\"net.exe\"", "\"net.exe\""))); } public void testNotInSet() { assertEquals( expr("name not in (\"net.exe\", \"whoami.exe\", \"hostname.exe\")"), - new Not(null, new In(null, - expr("name"), - exprs("\"net.exe\"", "\"whoami.exe\"", "\"hostname.exe\""))) + new Not(null, new In(null, expr("name"), exprs("\"net.exe\"", "\"whoami.exe\"", "\"hostname.exe\""))) ); } public void testInEmptySet() { - expectThrows(ParsingException.class, "Expected syntax error", - () -> expr("name in ()")); + expectThrows(ParsingException.class, "Expected syntax error", () -> expr("name in ()")); } public void testComplexComparison() { @@ -481,12 +451,8 @@ public void testComplexComparison() { comparison = "(1 * -2) <= (-3 * 4)"; } - Mul left = new Mul(null, - new Literal(null, 1, DataTypes.INTEGER), - new Neg(null, new Literal(null, 2, DataTypes.INTEGER))); - Mul right = new Mul(null, - new Neg(null, new Literal(null, 3, DataTypes.INTEGER)), - new Literal(null, 4, DataTypes.INTEGER)); + Mul left = new Mul(null, new Literal(null, 1, DataTypes.INTEGER), new Neg(null, new Literal(null, 2, DataTypes.INTEGER))); + Mul right = new Mul(null, new Neg(null, new Literal(null, 3, DataTypes.INTEGER)), new Literal(null, 4, DataTypes.INTEGER)); assertEquals(new LessThanOrEqual(null, left, right, UTC), expr(comparison)); } @@ -496,7 +462,7 @@ public void testChainedComparisonsDisallowed() { String firstComparator = ""; String secondComparator = ""; StringBuilder sb = new StringBuilder("a "); - for (int i = 0 ; i < noComparisons; i++) { + for (int i = 0; i < noComparisons; i++) { String comparator = randomFrom("==", "!=", "<", "<=", ">", ">="); sb.append(comparator).append(" a "); @@ -507,16 +473,24 @@ public void testChainedComparisonsDisallowed() { } } ParsingException e = expectThrows(ParsingException.class, () -> expr(sb.toString())); - assertEquals("line 1:" + (6 + firstComparator.length()) + ": mismatched input '" + secondComparator + - "' expecting {, 'and', 'in', 'in~', 'like', 'like~', 'not', 'or', " - + "'regex', 'regex~', ':', '+', '-', '*', '/', '%', '.', '['}", - e.getMessage()); + assertEquals( + "line 1:" + + (6 + firstComparator.length()) + + ": mismatched input '" + + secondComparator + + "' expecting {, 'and', 'in', 'in~', 'like', 'like~', 'not', 'or', " + + "'regex', 'regex~', ':', '+', '-', '*', '/', '%', '.', '['}", + e.getMessage() + ); } public void testUnsupportedPipes() { String pipe = randomValueOtherThanMany(Arrays.asList(HEAD_PIPE, TAIL_PIPE)::contains, () -> randomFrom(SUPPORTED_PIPES)); - ParsingException pe = expectThrows(ParsingException.class, "Expected parsing exception", - () -> parser.createStatement("process where foo == true | " + pipe)); + ParsingException pe = expectThrows( + ParsingException.class, + "Expected parsing exception", + () -> parser.createStatement("process where foo == true | " + pipe) + ); assertThat(pe.getMessage(), endsWith("Pipe [" + pipe + "] is not supported")); } } diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/parser/GrammarTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/parser/GrammarTests.java index 09c4d9e4008f1..042c017f5cdc3 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/parser/GrammarTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/parser/GrammarTests.java @@ -46,11 +46,15 @@ public void testUnsupportedQueries() throws Exception { ParsingException pe = expectThrows( ParsingException.class, "Query not identified as unsupported: " + q, - () -> parser.createStatement(q)); + () -> parser.createStatement(q) + ); if (pe.getErrorMessage().contains("supported") == false) { - throw new ParsingException(new Source(pe.getLineNumber() + line.v2() - 1, pe.getColumnNumber(), q), - pe.getErrorMessage() + " inside statement <{}>", q); + throw new ParsingException( + new Source(pe.getLineNumber() + line.v2() - 1, pe.getColumnNumber(), q), + pe.getErrorMessage() + " inside statement <{}>", + q + ); } } } diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/parser/LogicalPlanTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/parser/LogicalPlanTests.java index d88da21a9efe2..579b58584e195 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/parser/LogicalPlanTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/parser/LogicalPlanTests.java @@ -79,13 +79,14 @@ public void testParameterizedEventQuery() { public void testJoinPlan() { LogicalPlan plan = parser.createStatement( - "join by pid " + - " [process where true] " + - " [network where true] " + - " [registry where true] " + - " [file where true] " + - " " + - "until [process where event_subtype_full == \"termination_event\"]"); + "join by pid " + + " [process where true] " + + " [network where true] " + + " [registry where true] " + + " [file where true] " + + " " + + "until [process where event_subtype_full == \"termination_event\"]" + ); plan = defaultPipes(plan); assertEquals(Join.class, plan.getClass()); @@ -110,9 +111,8 @@ public void testJoinPlan() { public void testSequencePlan() { LogicalPlan plan = parser.createStatement( - "sequence by pid with maxspan=2s " + - " [process where process_name == \"*\" ] " + - " [file where file_path == \"*\"]"); + "sequence by pid with maxspan=2s " + " [process where process_name == \"*\" ] " + " [file where file_path == \"*\"]" + ); plan = defaultPipes(plan); assertEquals(Sequence.class, plan.getClass()); @@ -137,9 +137,10 @@ public void testSequencePlan() { public void testQuotedEventType() { LogicalPlan plan = parser.createStatement( - "sequence by pid with maxspan=2s " + - " [\"12\\\"34!@#$\" where process_name == \"test.exe\" ] " + - " [\"\"\"!@#$%test\"\"\\)(*&^\"\"\" where file_path == \"test.exe\"]"); + "sequence by pid with maxspan=2s " + + " [\"12\\\"34!@#$\" where process_name == \"test.exe\" ] " + + " [\"\"\"!@#$%test\"\"\\)(*&^\"\"\" where file_path == \"test.exe\"]" + ); plan = defaultPipes(plan); assertEquals(Sequence.class, plan.getClass()); diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/planner/AbstractQueryTranslatorTestCase.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/planner/AbstractQueryTranslatorTestCase.java index b9a1d1415dbbf..bcca55a5692ba 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/planner/AbstractQueryTranslatorTestCase.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/planner/AbstractQueryTranslatorTestCase.java @@ -36,8 +36,11 @@ public abstract class AbstractQueryTranslatorTestCase extends ESTestCase { protected IndexResolution index = IndexResolution.valid(new EsIndex("test", loadMapping("mapping-default.json", true))); protected PhysicalPlan plan(IndexResolution resolution, String eql) { - return planner.plan(optimizer.optimize(postAnalyzer.postAnalyze(analyzer.analyze(preAnalyzer.preAnalyze(parser.createStatement(eql), - resolution)), configuration))); + return planner.plan( + optimizer.optimize( + postAnalyzer.postAnalyze(analyzer.analyze(preAnalyzer.preAnalyze(parser.createStatement(eql), resolution)), configuration) + ) + ); } protected PhysicalPlan plan(String eql) { diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/planner/QueryTranslatorFailTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/planner/QueryTranslatorFailTests.java index 6fdd8fd7b7737..017489003ce50 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/planner/QueryTranslatorFailTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/planner/QueryTranslatorFailTests.java @@ -30,152 +30,192 @@ private String errorParsing(String eql) { public void testBetweenMissingOrNullParams() { final String[] queries = { - "process where between() == \"yst\"", - "process where between(process_name) == \"yst\"", - "process where between(process_name, \"s\") == \"yst\"", - "process where between(null) == \"yst\"", - "process where between(process_name, null) == \"yst\"", - "process where between(process_name, \"s\", \"e\", false, false) == \"yst\"", - }; + "process where between() == \"yst\"", + "process where between(process_name) == \"yst\"", + "process where between(process_name, \"s\") == \"yst\"", + "process where between(null) == \"yst\"", + "process where between(process_name, null) == \"yst\"", + "process where between(process_name, \"s\", \"e\", false, false) == \"yst\"", }; for (String query : queries) { - ParsingException e = expectThrows(ParsingException.class, - () -> plan(query)); + ParsingException e = expectThrows(ParsingException.class, () -> plan(query)); assertEquals("line 1:16: error building [between]: expects three or four arguments", e.getMessage()); } } public void testBetweenWrongTypeParams() { - assertEquals("1:15: second argument of [between(process_name, 1, 2)] must be [string], found value [1] type [integer]", - error("process where between(process_name, 1, 2)")); + assertEquals( + "1:15: second argument of [between(process_name, 1, 2)] must be [string], found value [1] type [integer]", + error("process where between(process_name, 1, 2)") + ); - assertEquals("1:15: third argument of [between(process_name, \"s\", 2)] must be [string], found value [2] type [integer]", - error("process where between(process_name, \"s\", 2)")); + assertEquals( + "1:15: third argument of [between(process_name, \"s\", 2)] must be [string], found value [2] type [integer]", + error("process where between(process_name, \"s\", 2)") + ); - assertEquals("1:15: fourth argument of [between(process_name, \"s\", \"e\", 1)] must be [boolean], found value [1] type [integer]", - error("process where between(process_name, \"s\", \"e\", 1)")); + assertEquals( + "1:15: fourth argument of [between(process_name, \"s\", \"e\", 1)] must be [boolean], found value [1] type [integer]", + error("process where between(process_name, \"s\", \"e\", 1)") + ); - assertEquals("1:15: fourth argument of [between(process_name, \"s\", \"e\", \"true\")] must be [boolean], " + - "found value [\"true\"] type [keyword]", - error("process where between(process_name, \"s\", \"e\", \"true\")")); + assertEquals( + "1:15: fourth argument of [between(process_name, \"s\", \"e\", \"true\")] must be [boolean], " + + "found value [\"true\"] type [keyword]", + error("process where between(process_name, \"s\", \"e\", \"true\")") + ); } public void testCIDRMatchAgainstField() { - VerificationException e = expectThrows(VerificationException.class, - () -> plan("process where cidrMatch(source_address, hostname)")); + VerificationException e = expectThrows( + VerificationException.class, + () -> plan("process where cidrMatch(source_address, hostname)") + ); String msg = e.getMessage(); - assertEquals("Found 1 problem\n" + - "line 1:15: second argument of [cidrMatch(source_address, hostname)] must be a constant, received [hostname]", msg); + assertEquals( + "Found 1 problem\n" + + "line 1:15: second argument of [cidrMatch(source_address, hostname)] must be a constant, received [hostname]", + msg + ); } public void testCIDRMatchMissingValue() { - ParsingException e = expectThrows(ParsingException.class, - () -> plan("process where cidrMatch(source_address)")); + ParsingException e = expectThrows(ParsingException.class, () -> plan("process where cidrMatch(source_address)")); String msg = e.getMessage(); assertEquals("line 1:16: error building [cidrmatch]: expects at least two arguments", msg); } public void testCIDRMatchNonIPField() { - VerificationException e = expectThrows(VerificationException.class, - () -> plan("process where cidrMatch(hostname, \"10.0.0.0/8\")")); + VerificationException e = expectThrows( + VerificationException.class, + () -> plan("process where cidrMatch(hostname, \"10.0.0.0/8\")") + ); String msg = e.getMessage(); - assertEquals("Found 1 problem\n" + - "line 1:15: first argument of [cidrMatch(hostname, \"10.0.0.0/8\")] must be [ip], found value [hostname] type [text]", msg); + assertEquals( + "Found 1 problem\n" + + "line 1:15: first argument of [cidrMatch(hostname, \"10.0.0.0/8\")] must be [ip], found value [hostname] type [text]", + msg + ); } public void testCIDRMatchNonString() { - VerificationException e = expectThrows(VerificationException.class, - () -> plan("process where cidrMatch(source_address, 12345)")); + VerificationException e = expectThrows(VerificationException.class, () -> plan("process where cidrMatch(source_address, 12345)")); String msg = e.getMessage(); - assertEquals("Found 1 problem\n" + - "line 1:15: second argument of [cidrMatch(source_address, 12345)] must be [string], found value [12345] type [integer]", msg); + assertEquals( + "Found 1 problem\n" + + "line 1:15: second argument of [cidrMatch(source_address, 12345)] must be [string], found value [12345] type [integer]", + msg + ); } public void testConcatWithInexact() { - VerificationException e = expectThrows(VerificationException.class, - () -> plan("process where concat(plain_text)")); + VerificationException e = expectThrows(VerificationException.class, () -> plan("process where concat(plain_text)")); String msg = e.getMessage(); - assertEquals("Found 1 problem\nline 1:15: [concat(plain_text)] cannot operate on field of data type " - + "[text]: No keyword/multi-field defined exact matches for [plain_text]; define one or use MATCH/QUERY instead", msg); + assertEquals( + "Found 1 problem\nline 1:15: [concat(plain_text)] cannot operate on field of data type " + + "[text]: No keyword/multi-field defined exact matches for [plain_text]; define one or use MATCH/QUERY instead", + msg + ); } public void testEndsWithFunctionWithInexact() { - VerificationException e = expectThrows(VerificationException.class, - () -> plan("process where endsWith(plain_text, \"foo\") == true")); + VerificationException e = expectThrows( + VerificationException.class, + () -> plan("process where endsWith(plain_text, \"foo\") == true") + ); String msg = e.getMessage(); - assertEquals("Found 1 problem\nline 1:15: [endsWith(plain_text, \"foo\")] cannot operate on first argument field of data type " - + "[text]: No keyword/multi-field defined exact matches for [plain_text]; define one or use MATCH/QUERY instead", msg); + assertEquals( + "Found 1 problem\nline 1:15: [endsWith(plain_text, \"foo\")] cannot operate on first argument field of data type " + + "[text]: No keyword/multi-field defined exact matches for [plain_text]; define one or use MATCH/QUERY instead", + msg + ); } public void testIndexOfFunctionWithInexact() { - VerificationException e = expectThrows(VerificationException.class, - () -> plan("process where indexOf(plain_text, \"foo\") == 1")); + VerificationException e = expectThrows(VerificationException.class, () -> plan("process where indexOf(plain_text, \"foo\") == 1")); String msg = e.getMessage(); - assertEquals("Found 1 problem\nline 1:15: [indexOf(plain_text, \"foo\")] cannot operate on first argument field of data type " - + "[text]: No keyword/multi-field defined exact matches for [plain_text]; define one or use MATCH/QUERY instead", msg); + assertEquals( + "Found 1 problem\nline 1:15: [indexOf(plain_text, \"foo\")] cannot operate on first argument field of data type " + + "[text]: No keyword/multi-field defined exact matches for [plain_text]; define one or use MATCH/QUERY instead", + msg + ); - e = expectThrows(VerificationException.class, - () -> plan("process where indexOf(\"bla\", plain_text) == 1")); + e = expectThrows(VerificationException.class, () -> plan("process where indexOf(\"bla\", plain_text) == 1")); msg = e.getMessage(); - assertEquals("Found 1 problem\nline 1:15: [indexOf(\"bla\", plain_text)] cannot operate on second argument field of data type " - + "[text]: No keyword/multi-field defined exact matches for [plain_text]; define one or use MATCH/QUERY instead", msg); + assertEquals( + "Found 1 problem\nline 1:15: [indexOf(\"bla\", plain_text)] cannot operate on second argument field of data type " + + "[text]: No keyword/multi-field defined exact matches for [plain_text]; define one or use MATCH/QUERY instead", + msg + ); } public void testLengthFunctionWithInexact() { - VerificationException e = expectThrows(VerificationException.class, - () -> plan("process where length(plain_text) > 0")); + VerificationException e = expectThrows(VerificationException.class, () -> plan("process where length(plain_text) > 0")); String msg = e.getMessage(); - assertEquals("Found 1 problem\nline 1:15: [length(plain_text)] cannot operate on field of data type [text]: No keyword/multi-field " - + "defined exact matches for [plain_text]; define one or use MATCH/QUERY instead", msg); + assertEquals( + "Found 1 problem\nline 1:15: [length(plain_text)] cannot operate on field of data type [text]: No keyword/multi-field " + + "defined exact matches for [plain_text]; define one or use MATCH/QUERY instead", + msg + ); } public void testMatchIsNotValidFunction() { - VerificationException e = expectThrows(VerificationException.class, - () -> plan("process where match(plain_text, \"foo.*\")")); + VerificationException e = expectThrows(VerificationException.class, () -> plan("process where match(plain_text, \"foo.*\")")); String msg = e.getMessage(); - assertEquals("Found 1 problem\n" + - "line 1:15: Unknown function [match], did you mean [cidrmatch]?", msg); + assertEquals("Found 1 problem\n" + "line 1:15: Unknown function [match], did you mean [cidrmatch]?", msg); } public void testNumberFunctionAlreadyNumber() { - VerificationException e = expectThrows(VerificationException.class, - () -> plan("process where number(pid) == 1")); + VerificationException e = expectThrows(VerificationException.class, () -> plan("process where number(pid) == 1")); String msg = e.getMessage(); - assertEquals("Found 1 problem\nline 1:15: first argument of [number(pid)] must be [string], " - + "found value [pid] type [long]", msg); + assertEquals( + "Found 1 problem\nline 1:15: first argument of [number(pid)] must be [string], " + "found value [pid] type [long]", + msg + ); } public void testNumberFunctionFloatBase() { - VerificationException e = expectThrows(VerificationException.class, - () -> plan("process where number(process_name, 1.0) == 1")); + VerificationException e = expectThrows(VerificationException.class, () -> plan("process where number(process_name, 1.0) == 1")); String msg = e.getMessage(); - assertEquals("Found 1 problem\nline 1:15: second argument of [number(process_name, 1.0)] must be [integer], " - + "found value [1.0] type [double]", msg); + assertEquals( + "Found 1 problem\nline 1:15: second argument of [number(process_name, 1.0)] must be [integer], " + + "found value [1.0] type [double]", + msg + ); } public void testNumberFunctionNonString() { - VerificationException e = expectThrows(VerificationException.class, - () -> plan("process where number(plain_text) == 1")); + VerificationException e = expectThrows(VerificationException.class, () -> plan("process where number(plain_text) == 1")); String msg = e.getMessage(); - assertEquals("Found 1 problem\nline 1:15: [number(plain_text)] cannot operate on first argument field of data type " - + "[text]: No keyword/multi-field defined exact matches for [plain_text]; define one or use MATCH/QUERY instead", msg); + assertEquals( + "Found 1 problem\nline 1:15: [number(plain_text)] cannot operate on first argument field of data type " + + "[text]: No keyword/multi-field defined exact matches for [plain_text]; define one or use MATCH/QUERY instead", + msg + ); } public void testPropertyEquationFilterUnsupported() { - QlIllegalArgumentException e = expectThrows(QlIllegalArgumentException.class, - () -> plan("process where (serial_event_id<9 and serial_event_id >= 7) or (opcode == pid)")); + QlIllegalArgumentException e = expectThrows( + QlIllegalArgumentException.class, + () -> plan("process where (serial_event_id<9 and serial_event_id >= 7) or (opcode == pid)") + ); String msg = e.getMessage(); assertEquals("Line 1:74: Comparisons against fields are not (currently) supported; offender [pid] in [==]", msg); } public void testPropertyEquationInClauseFilterUnsupported() { - VerificationException e = expectThrows(VerificationException.class, - () -> plan("process where opcode in (1,3) and process_name in (parent_process_name, \"SYSTEM\")")); + VerificationException e = expectThrows( + VerificationException.class, + () -> plan("process where opcode in (1,3) and process_name in (parent_process_name, \"SYSTEM\")") + ); String msg = e.getMessage(); - assertEquals("Found 1 problem\nline 1:35: Comparisons against fields are not (currently) supported; " + - "offender [parent_process_name] in [process_name in (parent_process_name, \"SYSTEM\")]", msg); + assertEquals( + "Found 1 problem\nline 1:35: Comparisons against fields are not (currently) supported; " + + "offender [parent_process_name] in [process_name in (parent_process_name, \"SYSTEM\")]", + msg + ); } public void testSequenceWithBeforeBy() { @@ -189,31 +229,42 @@ public void testSequenceWithNoTimeUnit() { } public void testStartsWithFunctionWithInexact() { - VerificationException e = expectThrows(VerificationException.class, - () -> plan("process where startsWith(plain_text, \"foo\") == true")); + VerificationException e = expectThrows( + VerificationException.class, + () -> plan("process where startsWith(plain_text, \"foo\") == true") + ); String msg = e.getMessage(); - assertEquals("Found 1 problem\nline 1:15: [startsWith(plain_text, \"foo\")] cannot operate on first argument field of data type " - + "[text]: No keyword/multi-field defined exact matches for [plain_text]; define one or use MATCH/QUERY instead", msg); + assertEquals( + "Found 1 problem\nline 1:15: [startsWith(plain_text, \"foo\")] cannot operate on first argument field of data type " + + "[text]: No keyword/multi-field defined exact matches for [plain_text]; define one or use MATCH/QUERY instead", + msg + ); } public void testStringContainsWrongParams() { - assertEquals("1:16: error building [stringcontains]: expects exactly two arguments", - errorParsing("process where stringContains()")); + assertEquals( + "1:16: error building [stringcontains]: expects exactly two arguments", + errorParsing("process where stringContains()") + ); - assertEquals("1:16: error building [stringcontains]: expects exactly two arguments", - errorParsing("process where stringContains(process_name)")); + assertEquals( + "1:16: error building [stringcontains]: expects exactly two arguments", + errorParsing("process where stringContains(process_name)") + ); - assertEquals("1:15: second argument of [stringContains(process_name, 1)] must be [string], found value [1] type [integer]", - error("process where stringContains(process_name, 1)")); + assertEquals( + "1:15: second argument of [stringContains(process_name, 1)] must be [string], found value [1] type [integer]", + error("process where stringContains(process_name, 1)") + ); } public void testLikeWithNumericField() { - VerificationException e = expectThrows(VerificationException.class, - () -> plan("process where pid like \"*.exe\"") - ); + VerificationException e = expectThrows(VerificationException.class, () -> plan("process where pid like \"*.exe\"")); String msg = e.getMessage(); - assertEquals("Found 1 problem\n" + - "line 1:15: argument of [pid like \"*.exe\"] must be [string], found value [pid] type [long]", msg); + assertEquals( + "Found 1 problem\n" + "line 1:15: argument of [pid like \"*.exe\"] must be [string], found value [pid] type [long]", + msg + ); } public void testSequenceWithTooLittleQueries() throws Exception { diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/planner/QueryTranslatorSpecTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/planner/QueryTranslatorSpecTests.java index 4d91d79cc2ad6..f13876bf94957 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/planner/QueryTranslatorSpecTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/planner/QueryTranslatorSpecTests.java @@ -8,6 +8,7 @@ package org.elasticsearch.xpack.eql.planner; import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; + import org.elasticsearch.xpack.eql.plan.physical.EsQueryExec; import org.elasticsearch.xpack.eql.plan.physical.PhysicalPlan; import org.elasticsearch.xpack.ql.TestUtils; diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/stats/VerifierMetricsTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/stats/VerifierMetricsTests.java index 5a242e9616dbe..873250d72c666 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/stats/VerifierMetricsTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/stats/VerifierMetricsTests.java @@ -52,19 +52,19 @@ public void testEventQuery() { } public void testSequenceQuery() { - Counters c = eql("sequence\r\n" + - " [process where serial_event_id == 1]\r\n" + - " [process where serial_event_id == 2]"); + Counters c = eql("sequence\r\n" + " [process where serial_event_id == 1]\r\n" + " [process where serial_event_id == 2]"); assertCounters(c, Set.of(SEQUENCE, PIPE_HEAD, SEQUENCE_QUERIES_TWO)); } @AwaitsFix(bugUrl = "waiting for the join implementation") public void testJoinQuery() { - Counters c = eql("join\r\n" + - " [file where file_name=\"*.exe\"] by ppid\r\n" + - " [file where file_name=\"*.com\"] by pid\r\n" + - "until [process where opcode=1] by ppid\r\n" + - "| head 1"); + Counters c = eql( + "join\r\n" + + " [file where file_name=\"*.exe\"] by ppid\r\n" + + " [file where file_name=\"*.com\"] by pid\r\n" + + "until [process where opcode=1] by ppid\r\n" + + "| head 1" + ); assertCounters(c, Set.of(JOIN, PIPE_HEAD, JOIN_UNTIL, JOIN_QUERIES_TWO, JOIN_KEYS_ONE)); } @@ -79,78 +79,94 @@ public void testTailQuery() { } public void testSequenceMaxSpanQuery() { - Counters c = eql("sequence with maxspan=1d\r\n" + - " [process where serial_event_id < 4] by exit_code\r\n" + - " [process where opcode == 1] by opcode\r\n" + - " [process where opcode == 2] by opcode\r\n" + - " [file where parent_process_name == \"file_delete_event\"] by exit_code\r\n" + - "until [process where opcode==1] by ppid\r\n" + - "| head 4\r\n" + - "| tail 2"); + Counters c = eql( + "sequence with maxspan=1d\r\n" + + " [process where serial_event_id < 4] by exit_code\r\n" + + " [process where opcode == 1] by opcode\r\n" + + " [process where opcode == 2] by opcode\r\n" + + " [file where parent_process_name == \"file_delete_event\"] by exit_code\r\n" + + "until [process where opcode==1] by ppid\r\n" + + "| head 4\r\n" + + "| tail 2" + ); assertCounters(c, Set.of(SEQUENCE, PIPE_HEAD, PIPE_TAIL, SEQUENCE_MAXSPAN, SEQUENCE_UNTIL, SEQUENCE_QUERIES_FOUR, JOIN_KEYS_ONE)); } public void testSequenceWithTwoQueries() { - Counters c = eql("sequence with maxspan=1d\r\n" + - " [process where serial_event_id < 4] by exit_code\r\n" + - " [process where opcode == 1] by opcode\r\n" + - "until [process where opcode==1] by ppid\r\n" + - "| head 4\r\n" + - "| tail 2"); + Counters c = eql( + "sequence with maxspan=1d\r\n" + + " [process where serial_event_id < 4] by exit_code\r\n" + + " [process where opcode == 1] by opcode\r\n" + + "until [process where opcode==1] by ppid\r\n" + + "| head 4\r\n" + + "| tail 2" + ); assertCounters(c, Set.of(SEQUENCE, PIPE_HEAD, PIPE_TAIL, SEQUENCE_MAXSPAN, SEQUENCE_UNTIL, SEQUENCE_QUERIES_TWO, JOIN_KEYS_ONE)); } public void testSequenceWithThreeQueries() { - Counters c = eql("sequence with maxspan=1d\r\n" + - " [process where serial_event_id < 4] by exit_code\r\n" + - " [process where opcode == 1] by opcode\r\n" + - " [file where parent_process_name == \"file_delete_event\"] by exit_code\r\n" + - "| head 4"); + Counters c = eql( + "sequence with maxspan=1d\r\n" + + " [process where serial_event_id < 4] by exit_code\r\n" + + " [process where opcode == 1] by opcode\r\n" + + " [file where parent_process_name == \"file_delete_event\"] by exit_code\r\n" + + "| head 4" + ); assertCounters(c, Set.of(SEQUENCE, PIPE_HEAD, SEQUENCE_MAXSPAN, SEQUENCE_QUERIES_THREE, JOIN_KEYS_ONE)); } public void testSequenceWithFiveQueries() { - Counters c = eql("sequence with maxspan=1d\r\n" + - " [process where serial_event_id < 4] by exit_code\r\n" + - " [process where opcode == 1] by opcode\r\n" + - " [file where parent_process_name == \"file_delete_event\"] by exit_code\r\n" + - " [process where serial_event_id < 4] by exit_code\r\n" + - " [process where opcode == 1] by opcode\r\n" + - "| head 4"); + Counters c = eql( + "sequence with maxspan=1d\r\n" + + " [process where serial_event_id < 4] by exit_code\r\n" + + " [process where opcode == 1] by opcode\r\n" + + " [file where parent_process_name == \"file_delete_event\"] by exit_code\r\n" + + " [process where serial_event_id < 4] by exit_code\r\n" + + " [process where opcode == 1] by opcode\r\n" + + "| head 4" + ); assertCounters(c, Set.of(SEQUENCE, PIPE_HEAD, SEQUENCE_MAXSPAN, SEQUENCE_QUERIES_FIVE_OR_MORE, JOIN_KEYS_ONE)); } public void testSequenceWithSevenQueries() { - Counters c = eql("sequence by exit_code, opcode\r\n" + - " [process where serial_event_id < 4]\r\n" + - " [process where opcode == 1]\r\n" + - " [file where parent_process_name == \"file_delete_event\"]\r\n" + - " [process where serial_event_id < 4]\r\n" + - " [process where opcode == 1]\r\n" + - " [process where true]\r\n" + - " [process where true]\r\n" + - "| tail 1"); + Counters c = eql( + "sequence by exit_code, opcode\r\n" + + " [process where serial_event_id < 4]\r\n" + + " [process where opcode == 1]\r\n" + + " [file where parent_process_name == \"file_delete_event\"]\r\n" + + " [process where serial_event_id < 4]\r\n" + + " [process where opcode == 1]\r\n" + + " [process where true]\r\n" + + " [process where true]\r\n" + + "| tail 1" + ); assertCounters(c, Set.of(SEQUENCE, PIPE_TAIL, SEQUENCE_QUERIES_FIVE_OR_MORE, JOIN_KEYS_TWO)); } public void testSequenceWithThreeKeys() { - Counters c = eql("sequence by exit_code, opcode, serial_event_id\r\n" + - " [process where serial_event_id < 4]\r\n" + - " [process where opcode == 1]\r\n"); + Counters c = eql( + "sequence by exit_code, opcode, serial_event_id\r\n" + + " [process where serial_event_id < 4]\r\n" + + " [process where opcode == 1]\r\n" + ); assertCounters(c, Set.of(SEQUENCE, PIPE_HEAD, SEQUENCE_QUERIES_TWO, JOIN_KEYS_THREE)); } public void testSequenceWithFourKeys() { - Counters c = eql("sequence by exit_code, user, serial_event_id, pid\r\n" + - " [process where serial_event_id < 4]\r\n" + - " [process where opcode == 1]\r\n"); + Counters c = eql( + "sequence by exit_code, user, serial_event_id, pid\r\n" + + " [process where serial_event_id < 4]\r\n" + + " [process where opcode == 1]\r\n" + ); assertCounters(c, Set.of(SEQUENCE, PIPE_HEAD, SEQUENCE_QUERIES_TWO, JOIN_KEYS_FOUR)); } public void testSequenceWithFiveKeys() { - Counters c = eql("sequence by exit_code, user, serial_event_id, pid, ppid\r\n" + - " [process where serial_event_id < 4]\r\n" + - " [process where opcode == 1]\r\n"); + Counters c = eql( + "sequence by exit_code, user, serial_event_id, pid, ppid\r\n" + + " [process where serial_event_id < 4]\r\n" + + " [process where opcode == 1]\r\n" + ); assertCounters(c, Set.of(SEQUENCE, PIPE_HEAD, SEQUENCE_QUERIES_TWO, JOIN_KEYS_FIVE_OR_MORE)); } diff --git a/x-pack/plugin/fleet/qa/rest/src/yamlRestTest/java/org/elasticsearch/xpack/fleet/FleetRestIT.java b/x-pack/plugin/fleet/qa/rest/src/yamlRestTest/java/org/elasticsearch/xpack/fleet/FleetRestIT.java index 1261e7dad2e70..ce6c4c456d8eb 100644 --- a/x-pack/plugin/fleet/qa/rest/src/yamlRestTest/java/org/elasticsearch/xpack/fleet/FleetRestIT.java +++ b/x-pack/plugin/fleet/qa/rest/src/yamlRestTest/java/org/elasticsearch/xpack/fleet/FleetRestIT.java @@ -8,6 +8,7 @@ package org.elasticsearch.xpack.fleet; import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; + import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate; import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase; diff --git a/x-pack/plugin/graph/qa/with-security/src/yamlRestTest/java/org/elasticsearch/smoketest/GraphWithSecurityIT.java b/x-pack/plugin/graph/qa/with-security/src/yamlRestTest/java/org/elasticsearch/smoketest/GraphWithSecurityIT.java index cdd3e4cdbadd0..8c5a39072f6b2 100644 --- a/x-pack/plugin/graph/qa/with-security/src/yamlRestTest/java/org/elasticsearch/smoketest/GraphWithSecurityIT.java +++ b/x-pack/plugin/graph/qa/with-security/src/yamlRestTest/java/org/elasticsearch/smoketest/GraphWithSecurityIT.java @@ -17,7 +17,6 @@ import static org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken.basicAuthHeaderValue; - public class GraphWithSecurityIT extends ESClientYamlSuiteTestCase { private static final String TEST_ADMIN_USERNAME = "test_admin"; @@ -33,25 +32,19 @@ public static Iterable parameters() throws Exception { } protected String[] getCredentials() { - return new String[]{"graph_explorer", "x-pack-test-password"}; + return new String[] { "graph_explorer", "x-pack-test-password" }; } - @Override protected Settings restClientSettings() { String[] creds = getCredentials(); String token = basicAuthHeaderValue(creds[0], new SecureString(creds[1].toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } @Override protected Settings restAdminSettings() { String token = basicAuthHeaderValue(TEST_ADMIN_USERNAME, new SecureString(TEST_ADMIN_PASSWORD.toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } } - diff --git a/x-pack/plugin/graph/qa/with-security/src/yamlRestTest/java/org/elasticsearch/smoketest/GraphWithSecurityInsufficientRoleIT.java b/x-pack/plugin/graph/qa/with-security/src/yamlRestTest/java/org/elasticsearch/smoketest/GraphWithSecurityInsufficientRoleIT.java index 9126ab8ee2308..164b3240dfb58 100644 --- a/x-pack/plugin/graph/qa/with-security/src/yamlRestTest/java/org/elasticsearch/smoketest/GraphWithSecurityInsufficientRoleIT.java +++ b/x-pack/plugin/graph/qa/with-security/src/yamlRestTest/java/org/elasticsearch/smoketest/GraphWithSecurityInsufficientRoleIT.java @@ -24,7 +24,7 @@ public void test() throws IOException { try { super.test(); fail("should have failed because of missing role"); - } catch(AssertionError ae) { + } catch (AssertionError ae) { assertThat(ae.getMessage(), containsString("action [indices:data/read/xpack/graph/explore")); assertThat(ae.getMessage(), containsString("returned [403 Forbidden]")); assertThat(ae.getMessage(), containsString("is unauthorized for user [no_graph_explorer]")); @@ -33,7 +33,6 @@ public void test() throws IOException { @Override protected String[] getCredentials() { - return new String[]{"no_graph_explorer", "x-pack-test-password"}; + return new String[] { "no_graph_explorer", "x-pack-test-password" }; } } - diff --git a/x-pack/plugin/graph/src/internalClusterTest/java/org/elasticsearch/xpack/graph/test/GraphTests.java b/x-pack/plugin/graph/src/internalClusterTest/java/org/elasticsearch/xpack/graph/test/GraphTests.java index c337b84e99dfe..1ed6eb4a87585 100644 --- a/x-pack/plugin/graph/src/internalClusterTest/java/org/elasticsearch/xpack/graph/test/GraphTests.java +++ b/x-pack/plugin/graph/src/internalClusterTest/java/org/elasticsearch/xpack/graph/test/GraphTests.java @@ -45,7 +45,6 @@ import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; import static org.hamcrest.Matchers.greaterThan; - public class GraphTests extends ESSingleNodeTestCase { static class DocTemplate { @@ -63,29 +62,28 @@ static class DocTemplate { } } - static final DocTemplate[] socialNetTemplate = { - new DocTemplate(10, "60s", "beatles", "john", "paul", "george", "ringo"), - new DocTemplate(2, "60s", "collaboration", "ravi", "george"), - new DocTemplate(3, "80s", "travelling wilburys", "roy", "george", "jeff"), - new DocTemplate(5, "80s", "travelling wilburys", "roy", "jeff", "bob"), - new DocTemplate(1, "70s", "collaboration", "roy", "elvis"), - new DocTemplate(10, "90s", "nirvana", "dave", "kurt"), - new DocTemplate(2, "00s", "collaboration", "dave", "paul"), - new DocTemplate(2, "80s", "collaboration", "stevie", "paul"), - new DocTemplate(2, "70s", "collaboration", "john", "yoko"), - new DocTemplate(100, "70s", "fillerDoc", "other", "irrelevant", "duplicated", "spammy", "background") - }; + new DocTemplate(10, "60s", "beatles", "john", "paul", "george", "ringo"), + new DocTemplate(2, "60s", "collaboration", "ravi", "george"), + new DocTemplate(3, "80s", "travelling wilburys", "roy", "george", "jeff"), + new DocTemplate(5, "80s", "travelling wilburys", "roy", "jeff", "bob"), + new DocTemplate(1, "70s", "collaboration", "roy", "elvis"), + new DocTemplate(10, "90s", "nirvana", "dave", "kurt"), + new DocTemplate(2, "00s", "collaboration", "dave", "paul"), + new DocTemplate(2, "80s", "collaboration", "stevie", "paul"), + new DocTemplate(2, "70s", "collaboration", "john", "yoko"), + new DocTemplate(100, "70s", "fillerDoc", "other", "irrelevant", "duplicated", "spammy", "background") }; @Override public void setUp() throws Exception { super.setUp(); - assertAcked(client().admin().indices().prepareCreate("test") + assertAcked( + client().admin() + .indices() + .prepareCreate("test") .setSettings(Settings.builder().put(SETTING_NUMBER_OF_SHARDS, 2).put(SETTING_NUMBER_OF_REPLICAS, 0)) - .setMapping( - "decade", "type=keyword", - "people", "type=keyword", - "description", "type=text,fielddata=true")); + .setMapping("decade", "type=keyword", "people", "type=keyword", "description", "type=text,fielddata=true") + ); createIndex("idx_unmapped"); ensureGreen(); @@ -94,17 +92,23 @@ public void setUp() throws Exception { for (DocTemplate dt : socialNetTemplate) { for (int i = 0; i < dt.numDocs; i++) { // Supply a doc ID for deterministic routing of docs to shards - client().prepareIndex("test").setId("doc#" + numDocs) - .setSource("decade", dt.decade, "people", dt.people, "description", dt.description) - .get(); + client().prepareIndex("test") + .setId("doc#" + numDocs) + .setSource("decade", dt.decade, "people", dt.people, "description", dt.description) + .get(); numDocs++; } } client().admin().indices().prepareRefresh("test").get(); // Ensure single segment with no deletes. Hopefully solves test instability in // issue https://github.com/elastic/x-pack-elasticsearch/issues/918 - ForceMergeResponse actionGet = client().admin().indices().prepareForceMerge("test").setFlush(true).setMaxNumSegments(1) - .execute().actionGet(); + ForceMergeResponse actionGet = client().admin() + .indices() + .prepareForceMerge("test") + .setFlush(true) + .setMaxNumSegments(1) + .execute() + .actionGet(); client().admin().indices().prepareRefresh("test").get(); assertAllSuccessful(actionGet); for (IndexShardSegments seg : client().admin().indices().prepareSegments().get().getIndices().get("test")) { @@ -134,19 +138,18 @@ public void testSignificanceQueryCrawl() { checkVertexDepth(response, 1, "stevie", "yoko", "roy"); checkVertexIsMoreImportant(response, "John's only collaboration is more relevant than one of Paul's many", "yoko", "stevie"); checkVertexIsMoreImportant(response, "John's only collaboration is more relevant than George's with profligate Roy", "yoko", "roy"); - assertNull("Elvis is a 3rd tier connection so should not be returned here", response.getVertex(Vertex.createId("people","elvis"))); + assertNull("Elvis is a 3rd tier connection so should not be returned here", response.getVertex(Vertex.createId("people", "elvis"))); } - @Override - protected Settings nodeSettings() { + protected Settings nodeSettings() { // Disable security otherwise authentication failures happen creating indices. Builder newSettings = Settings.builder(); newSettings.put(super.nodeSettings()); newSettings.put(LicenseService.SELF_GENERATED_LICENSE_TYPE.getKey(), "trial"); -// newSettings.put(XPackSettings.SECURITY_ENABLED.getKey(), false); -// newSettings.put(XPackSettings.MONITORING_ENABLED.getKey(), false); -// newSettings.put(XPackSettings.WATCHER_ENABLED.getKey(), false); + // newSettings.put(XPackSettings.SECURITY_ENABLED.getKey(), false); + // newSettings.put(XPackSettings.MONITORING_ENABLED.getKey(), false); + // newSettings.put(XPackSettings.WATCHER_ENABLED.getKey(), false); return newSettings.build(); } @@ -155,28 +158,26 @@ public void testTargetedQueryCrawl() { GraphExploreRequestBuilder grb = new GraphExploreRequestBuilder(client(), GraphExploreAction.INSTANCE).setIndices("test"); Hop hop1 = grb.createNextHop(QueryBuilders.termQuery("description", "beatles")); hop1.addVertexRequest("people").size(10).minDocCount(1); // members of beatles - //70s friends of beatles + // 70s friends of beatles grb.createNextHop(QueryBuilders.termQuery("decade", "70s")).addVertexRequest("people").size(100).minDocCount(1); GraphExploreResponse response = grb.get(); checkVertexDepth(response, 0, "john", "paul", "george", "ringo"); checkVertexDepth(response, 1, "yoko"); - assertNull("Roy collaborated with George in the 80s not the 70s", response.getVertex(Vertex.createId("people","roy"))); - assertNull("Stevie collaborated with Paul in the 80s not the 70s", response.getVertex(Vertex.createId("people","stevie"))); + assertNull("Roy collaborated with George in the 80s not the 70s", response.getVertex(Vertex.createId("people", "roy"))); + assertNull("Stevie collaborated with Paul in the 80s not the 70s", response.getVertex(Vertex.createId("people", "stevie"))); } - - public void testLargeNumberTermsStartCrawl() { GraphExploreRequestBuilder grb = new GraphExploreRequestBuilder(client(), GraphExploreAction.INSTANCE).setIndices("test"); Hop hop1 = grb.createNextHop(null); VertexRequest peopleNames = hop1.addVertexRequest("people").minDocCount(1); peopleNames.addInclude("john", 1); - for (int i = 0; i < BooleanQuery.getMaxClauseCount()+1; i++) { - peopleNames.addInclude("unknown"+i, 1); + for (int i = 0; i < BooleanQuery.getMaxClauseCount() + 1; i++) { + peopleNames.addInclude("unknown" + i, 1); } grb.createNextHop(null).addVertexRequest("people").size(100).minDocCount(1); // friends of members of beatles @@ -184,21 +185,20 @@ public void testLargeNumberTermsStartCrawl() { GraphExploreResponse response = grb.get(); checkVertexDepth(response, 0, "john"); - checkVertexDepth(response, 1, "yoko"); + checkVertexDepth(response, 1, "yoko"); } public void testTargetedQueryCrawlDepth2() { GraphExploreRequestBuilder grb = new GraphExploreRequestBuilder(client(), GraphExploreAction.INSTANCE).setIndices("test"); Hop hop1 = grb.createNextHop(QueryBuilders.termQuery("description", "beatles")); hop1.addVertexRequest("people").size(10).minDocCount(1); // members of beatles - //00s friends of beatles + // 00s friends of beatles grb.createNextHop(QueryBuilders.termQuery("decade", "00s")).addVertexRequest("people").size(100).minDocCount(1); - //90s friends of friends of beatles + // 90s friends of friends of beatles grb.createNextHop(QueryBuilders.termQuery("decade", "90s")).addVertexRequest("people").size(100).minDocCount(1); GraphExploreResponse response = grb.get(); - checkVertexDepth(response, 0, "john", "paul", "george", "ringo"); checkVertexDepth(response, 1, "dave"); checkVertexDepth(response, 2, "kurt"); @@ -219,7 +219,7 @@ public void testPopularityQueryCrawl() { checkVertexDepth(response, 1, "stevie", "yoko", "roy"); checkVertexIsMoreImportant(response, "Yoko has more collaborations than Stevie", "yoko", "stevie"); checkVertexIsMoreImportant(response, "Roy has more collaborations than Stevie", "roy", "stevie"); - assertNull("Elvis is a 3rd tier connection so should not be returned here", response.getVertex(Vertex.createId("people","elvis"))); + assertNull("Elvis is a 3rd tier connection so should not be returned here", response.getVertex(Vertex.createId("people", "elvis"))); } @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/55396") @@ -228,11 +228,12 @@ public void testTimedoutQueryCrawl() { grb.setTimeout(TimeValue.timeValueMillis(400)); Hop hop1 = grb.createNextHop(QueryBuilders.termQuery("description", "beatles")); hop1.addVertexRequest("people").size(10).minDocCount(1); // members of beatles - //00s friends of beatles + // 00s friends of beatles grb.createNextHop(QueryBuilders.termQuery("decade", "00s")).addVertexRequest("people").size(100).minDocCount(1); // A query that should cause a timeout - ScriptQueryBuilder timeoutQuery = QueryBuilders.scriptQuery(new Script(ScriptType.INLINE, "mockscript", - "graph_timeout", Collections.emptyMap())); + ScriptQueryBuilder timeoutQuery = QueryBuilders.scriptQuery( + new Script(ScriptType.INLINE, "mockscript", "graph_timeout", Collections.emptyMap()) + ); grb.createNextHop(timeoutQuery).addVertexRequest("people").size(100).minDocCount(1); GraphExploreResponse response = grb.get(); @@ -268,7 +269,7 @@ public void testDiversifiedCrawl() { GraphExploreResponse response = grb.get(); checkVertexDepth(response, 0, "dave", "kurt"); - assertNull("Duplicate spam should be removed from the results", response.getVertex(Vertex.createId("people","spammy"))); + assertNull("Duplicate spam should be removed from the results", response.getVertex(Vertex.createId("people", "spammy"))); } public void testInvalidDiversifiedCrawl() { @@ -293,8 +294,10 @@ public void testInvalidDiversifiedCrawl() { } public void testMappedAndUnmappedQueryCrawl() { - GraphExploreRequestBuilder grb = new GraphExploreRequestBuilder(client(), GraphExploreAction.INSTANCE) - .setIndices("test", "idx_unmapped"); + GraphExploreRequestBuilder grb = new GraphExploreRequestBuilder(client(), GraphExploreAction.INSTANCE).setIndices( + "test", + "idx_unmapped" + ); Hop hop1 = grb.createNextHop(QueryBuilders.termQuery("description", "beatles")); hop1.addVertexRequest("people").size(10).minDocCount(1); // members of beatles grb.createNextHop(null).addVertexRequest("people").size(100).minDocCount(1); // friends of members of beatles @@ -305,7 +308,7 @@ public void testMappedAndUnmappedQueryCrawl() { checkVertexDepth(response, 1, "stevie", "yoko", "roy"); checkVertexIsMoreImportant(response, "John's only collaboration is more relevant than one of Paul's many", "yoko", "stevie"); checkVertexIsMoreImportant(response, "John's only collaboration is more relevant than George's with profligate Roy", "yoko", "roy"); - assertNull("Elvis is a 3rd tier connection so should not be returned here", response.getVertex(Vertex.createId("people","elvis"))); + assertNull("Elvis is a 3rd tier connection so should not be returned here", response.getVertex(Vertex.createId("people", "elvis"))); } public void testUnmappedQueryCrawl() { @@ -340,7 +343,6 @@ public void testRequestValidation() { } - private static void checkVertexDepth(GraphExploreResponse response, int expectedDepth, String... ids) { for (String id : ids) { Vertex vertex = response.getVertex(Vertex.createId("people", id)); @@ -354,7 +356,7 @@ private static void checkVertexIsMoreImportant(GraphExploreResponse response, St // make this test fail. Scores vary slightly due to deletes I suspect. Vertex strongVertex = response.getVertex(Vertex.createId("people", strongerId)); assertNotNull(strongVertex); - Vertex weakVertex = response.getVertex(Vertex.createId("people",weakerId)); + Vertex weakVertex = response.getVertex(Vertex.createId("people", weakerId)); assertNotNull(weakVertex); assertThat(why, strongVertex.getWeight(), greaterThan(weakVertex.getWeight())); } diff --git a/x-pack/plugin/graph/src/main/java/org/elasticsearch/xpack/graph/Graph.java b/x-pack/plugin/graph/src/main/java/org/elasticsearch/xpack/graph/Graph.java index e937468fd66a9..4e9502c073d2a 100644 --- a/x-pack/plugin/graph/src/main/java/org/elasticsearch/xpack/graph/Graph.java +++ b/x-pack/plugin/graph/src/main/java/org/elasticsearch/xpack/graph/Graph.java @@ -51,17 +51,19 @@ public Graph(Settings settings) { if (false == enabled) { return Arrays.asList(usageAction, infoAction); } - return Arrays.asList( - new ActionHandler<>(GraphExploreAction.INSTANCE, TransportGraphExploreAction.class), - usageAction, - infoAction); + return Arrays.asList(new ActionHandler<>(GraphExploreAction.INSTANCE, TransportGraphExploreAction.class), usageAction, infoAction); } @Override - public List getRestHandlers(Settings settings, RestController restController, ClusterSettings clusterSettings, - IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter, - IndexNameExpressionResolver indexNameExpressionResolver, - Supplier nodesInCluster) { + public List getRestHandlers( + Settings settings, + RestController restController, + ClusterSettings clusterSettings, + IndexScopedSettings indexScopedSettings, + SettingsFilter settingsFilter, + IndexNameExpressionResolver indexNameExpressionResolver, + Supplier nodesInCluster + ) { if (false == enabled) { return emptyList(); } diff --git a/x-pack/plugin/graph/src/main/java/org/elasticsearch/xpack/graph/GraphInfoTransportAction.java b/x-pack/plugin/graph/src/main/java/org/elasticsearch/xpack/graph/GraphInfoTransportAction.java index 0e03ecb6a55f2..7a26b59c3d895 100644 --- a/x-pack/plugin/graph/src/main/java/org/elasticsearch/xpack/graph/GraphInfoTransportAction.java +++ b/x-pack/plugin/graph/src/main/java/org/elasticsearch/xpack/graph/GraphInfoTransportAction.java @@ -22,8 +22,12 @@ public class GraphInfoTransportAction extends XPackInfoFeatureTransportAction { private final XPackLicenseState licenseState; @Inject - public GraphInfoTransportAction(TransportService transportService, ActionFilters actionFilters, - Settings settings, XPackLicenseState licenseState) { + public GraphInfoTransportAction( + TransportService transportService, + ActionFilters actionFilters, + Settings settings, + XPackLicenseState licenseState + ) { super(XPackInfoFeatureAction.GRAPH.name(), transportService, actionFilters); this.enabled = XPackSettings.GRAPH_ENABLED.get(settings); this.licenseState = licenseState; diff --git a/x-pack/plugin/graph/src/main/java/org/elasticsearch/xpack/graph/GraphUsageTransportAction.java b/x-pack/plugin/graph/src/main/java/org/elasticsearch/xpack/graph/GraphUsageTransportAction.java index 316e911288cc1..f1bd79b2f3b53 100644 --- a/x-pack/plugin/graph/src/main/java/org/elasticsearch/xpack/graph/GraphUsageTransportAction.java +++ b/x-pack/plugin/graph/src/main/java/org/elasticsearch/xpack/graph/GraphUsageTransportAction.java @@ -30,20 +30,38 @@ public class GraphUsageTransportAction extends XPackUsageFeatureTransportAction private final XPackLicenseState licenseState; @Inject - public GraphUsageTransportAction(TransportService transportService, ClusterService clusterService, ThreadPool threadPool, - ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver, - Settings settings, XPackLicenseState licenseState) { - super(XPackUsageFeatureAction.GRAPH.name(), transportService, clusterService, - threadPool, actionFilters, indexNameExpressionResolver); + public GraphUsageTransportAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver, + Settings settings, + XPackLicenseState licenseState + ) { + super( + XPackUsageFeatureAction.GRAPH.name(), + transportService, + clusterService, + threadPool, + actionFilters, + indexNameExpressionResolver + ); this.settings = settings; this.licenseState = licenseState; } @Override - protected void masterOperation(Task task, XPackUsageRequest request, ClusterState state, - ActionListener listener) { - GraphFeatureSetUsage usage = - new GraphFeatureSetUsage(Graph.GRAPH_FEATURE.checkWithoutTracking(licenseState), XPackSettings.GRAPH_ENABLED.get(settings)); + protected void masterOperation( + Task task, + XPackUsageRequest request, + ClusterState state, + ActionListener listener + ) { + GraphFeatureSetUsage usage = new GraphFeatureSetUsage( + Graph.GRAPH_FEATURE.checkWithoutTracking(licenseState), + XPackSettings.GRAPH_ENABLED.get(settings) + ); listener.onResponse(new XPackUsageFeatureResponse(usage)); } } diff --git a/x-pack/plugin/graph/src/main/java/org/elasticsearch/xpack/graph/action/TransportGraphExploreAction.java b/x-pack/plugin/graph/src/main/java/org/elasticsearch/xpack/graph/action/TransportGraphExploreAction.java index 7e4925b8506be..1a716fa9b500e 100644 --- a/x-pack/plugin/graph/src/main/java/org/elasticsearch/xpack/graph/action/TransportGraphExploreAction.java +++ b/x-pack/plugin/graph/src/main/java/org/elasticsearch/xpack/graph/action/TransportGraphExploreAction.java @@ -18,8 +18,8 @@ import org.elasticsearch.action.support.HandledTransportAction; import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.common.inject.Inject; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.CollectionUtils; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.license.LicenseUtils; @@ -37,10 +37,10 @@ import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.bucket.sampler.DiversifiedAggregationBuilder; import org.elasticsearch.search.aggregations.bucket.sampler.Sampler; +import org.elasticsearch.search.aggregations.bucket.terms.IncludeExclude; import org.elasticsearch.search.aggregations.bucket.terms.SignificantTerms; import org.elasticsearch.search.aggregations.bucket.terms.SignificantTerms.Bucket; import org.elasticsearch.search.aggregations.bucket.terms.SignificantTermsAggregationBuilder; -import org.elasticsearch.search.aggregations.bucket.terms.IncludeExclude; import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; import org.elasticsearch.search.builder.SearchSourceBuilder; @@ -84,8 +84,13 @@ protected boolean lessThan(Vertex a, Vertex b) { } @Inject - public TransportGraphExploreAction(ThreadPool threadPool, NodeClient client, TransportService transportService, - ActionFilters actionFilters, XPackLicenseState licenseState) { + public TransportGraphExploreAction( + ThreadPool threadPool, + NodeClient client, + TransportService transportService, + ActionFilters actionFilters, + XPackLicenseState licenseState + ) { super(GraphExploreAction.NAME, transportService, actionFilters, GraphExploreRequest::new); this.threadPool = threadPool; this.client = client; @@ -160,7 +165,6 @@ private void removeVertex(Vertex vertex) { hopFindings.get(currentHopNumber).get(vertex.getField()).remove(vertex); } - /** * Step out from some existing vertex terms looking for useful * connections @@ -182,8 +186,7 @@ synchronized void expand() { currentHopNumber++; Hop currentHop = request.getHop(currentHopNumber); - final SearchRequest searchRequest = new SearchRequest(request.indices()).indicesOptions( - request.indicesOptions()); + final SearchRequest searchRequest = new SearchRequest(request.indices()).indicesOptions(request.indicesOptions()); if (request.routing() != null) { searchRequest.routing(request.routing()); } @@ -201,11 +204,11 @@ synchronized void expand() { AggregationBuilder sampleAgg = null; if (request.sampleDiversityField() != null) { DiversifiedAggregationBuilder diversifiedSampleAgg = AggregationBuilders.diversifiedSampler("sample") - .shardSize(request.sampleSize()); + .shardSize(request.sampleSize()); diversifiedSampleAgg.field(request.sampleDiversityField()); diversifiedSampleAgg.maxDocsPerValue(request.maxDocsPerDiversityValue()); sampleAgg = diversifiedSampleAgg; - }else{ + } else { sampleAgg = AggregationBuilders.sampler("sample").shardSize(request.sampleSize()); } @@ -221,10 +224,9 @@ synchronized void expand() { rootBool.must(sourceTermsOrClause); - - //Now build the agg tree that will channel the content -> - // base agg is terms agg for terms from last wave (one per field), - // under each is a sig_terms agg to find next candidates (again, one per field)... + // Now build the agg tree that will channel the content -> + // base agg is terms agg for terms from last wave (one per field), + // under each is a sig_terms agg to find next candidates (again, one per field)... for (int fieldNum = 0; fieldNum < lastHop.getNumberVertexRequests(); fieldNum++) { VertexRequest lastVr = lastHop.getVertexRequest(fieldNum); Set lastWaveVerticesForField = lastHopFindings.get(lastVr.fieldName()); @@ -237,36 +239,40 @@ synchronized void expand() { terms[i++] = v.getTerm(); } TermsAggregationBuilder lastWaveTermsAgg = AggregationBuilders.terms("field" + fieldNum) - .includeExclude(new IncludeExclude(terms, null)) - .shardMinDocCount(1) - .field(lastVr.fieldName()).minDocCount(1) - // Map execution mode used because Sampler agg keeps us - // focused on smaller sets of high quality docs and therefore - // examine smaller volumes of terms - .executionHint("map") - .size(terms.length); + .includeExclude(new IncludeExclude(terms, null)) + .shardMinDocCount(1) + .field(lastVr.fieldName()) + .minDocCount(1) + // Map execution mode used because Sampler agg keeps us + // focused on smaller sets of high quality docs and therefore + // examine smaller volumes of terms + .executionHint("map") + .size(terms.length); sampleAgg.subAggregation(lastWaveTermsAgg); for (int f = 0; f < currentHop.getNumberVertexRequests(); f++) { VertexRequest vr = currentHop.getVertexRequest(f); - int size=vr.size(); + int size = vr.size(); if (vr.fieldName().equals(lastVr.fieldName())) { - //We have the potential for self-loops as we are looking at the same field so add 1 to the requested size + // We have the potential for self-loops as we are looking at the same field so add 1 to the requested size // because we need to eliminate fieldA:termA -> fieldA:termA links that are likely to be in the results. size++; } if (request.useSignificance()) { SignificantTermsAggregationBuilder nextWaveSigTerms = AggregationBuilders.significantTerms("field" + f) - .field(vr.fieldName()) - .minDocCount(vr.minDocCount()).shardMinDocCount(vr.shardMinDocCount()).executionHint("map").size(size); -// nextWaveSigTerms.significanceHeuristic(new PercentageScore.PercentageScoreBuilder()); - //Had some issues with no significant terms being returned when asking for small + .field(vr.fieldName()) + .minDocCount(vr.minDocCount()) + .shardMinDocCount(vr.shardMinDocCount()) + .executionHint("map") + .size(size); + // nextWaveSigTerms.significanceHeuristic(new PercentageScore.PercentageScoreBuilder()); + // Had some issues with no significant terms being returned when asking for small // number of final results (eg 1) and only one shard. Setting shard_size higher helped. if (size < 10) { nextWaveSigTerms.shardSize(10); } // Alternative choices of significance algo didn't seem to be improvements.... -// nextWaveSigTerms.significanceHeuristic(new GND.GNDBuilder(true)); -// nextWaveSigTerms.significanceHeuristic(new ChiSquare.ChiSquareBuilder(false, true)); + // nextWaveSigTerms.significanceHeuristic(new GND.GNDBuilder(true)); + // nextWaveSigTerms.significanceHeuristic(new ChiSquare.ChiSquareBuilder(false, true)); if (vr.hasIncludeClauses()) { String[] includes = vr.includeValuesAsStringArray(); @@ -286,13 +292,15 @@ synchronized void expand() { } lastWaveTermsAgg.subAggregation(nextWaveSigTerms); } else { - TermsAggregationBuilder nextWavePopularTerms = AggregationBuilders.terms("field" + f).field(vr.fieldName()) - .minDocCount(vr.minDocCount()).shardMinDocCount(vr.shardMinDocCount()) - // Map execution mode used because Sampler agg keeps us - // focused on smaller sets of high quality docs and therefore - // examine smaller volumes of terms - .executionHint("map") - .size(size); + TermsAggregationBuilder nextWavePopularTerms = AggregationBuilders.terms("field" + f) + .field(vr.fieldName()) + .minDocCount(vr.minDocCount()) + .shardMinDocCount(vr.shardMinDocCount()) + // Map execution mode used because Sampler agg keeps us + // focused on smaller sets of high quality docs and therefore + // examine smaller volumes of terms + .executionHint("map") + .size(size); if (vr.hasIncludeClauses()) { String[] includes = vr.includeValuesAsStringArray(); nextWavePopularTerms.includeExclude(new IncludeExclude(includes, null)); @@ -345,17 +353,22 @@ public void onResponse(SearchResponse searchResponse) { } - // Add new vertices and apportion share of total signal along // connections - private void addAndScoreNewVertices(Hop lastHop, Hop currentHop, Sampler sample, double totalSignalOutput, - ArrayList newConnections, ArrayList newVertices) { + private void addAndScoreNewVertices( + Hop lastHop, + Hop currentHop, + Sampler sample, + double totalSignalOutput, + ArrayList newConnections, + ArrayList newVertices + ) { // Gather all matching terms into the graph and propagate // signals for (int j = 0; j < lastHop.getNumberVertexRequests(); j++) { VertexRequest lastVr = lastHop.getVertexRequest(j); Terms lastWaveTerms = sample.getAggregations().get("field" + j); - if(lastWaveTerms == null){ + if (lastWaveTerms == null) { // There were no terms from the previous phase that needed pursuing continue; } @@ -371,8 +384,8 @@ private void addAndScoreNewVertices(Hop lastHop, Hop currentHop, Sampler sample, SignificantTerms significantTerms = lastWaveTerm.getAggregations().get("field" + k); if (significantTerms != null) { for (Bucket bucket : significantTerms.getBuckets()) { - if ((vr.fieldName().equals(fromVertex.getField())) && - (bucket.getKeyAsString().equals(fromVertex.getTerm()))) { + if ((vr.fieldName().equals(fromVertex.getField())) + && (bucket.getKeyAsString().equals(fromVertex.getTerm()))) { // Avoid self-joins continue; } @@ -383,8 +396,14 @@ private void addAndScoreNewVertices(Hop lastHop, Hop currentHop, Sampler sample, Vertex toVertex = getVertex(vr.fieldName(), bucket.getKeyAsString()); if (toVertex == null) { - toVertex = addVertex(vr.fieldName(), bucket.getKeyAsString(), signalStrength, - currentHopNumber, bucket.getSupersetDf(), bucket.getSubsetDf()); + toVertex = addVertex( + vr.fieldName(), + bucket.getKeyAsString(), + signalStrength, + currentHopNumber, + bucket.getSupersetDf(), + bucket.getSubsetDf() + ); newVertices.add(toVertex); } else { toVertex.setWeight(toVertex.getWeight() + signalStrength); @@ -407,8 +426,14 @@ private void addAndScoreNewVertices(Hop lastHop, Hop currentHop, Sampler sample, Vertex toVertex = getVertex(vr.fieldName(), bucket.getKeyAsString()); if (toVertex == null) { - toVertex = addVertex(vr.fieldName(), bucket.getKeyAsString(), signalStrength, - currentHopNumber, 0, 0); + toVertex = addVertex( + vr.fieldName(), + bucket.getKeyAsString(), + signalStrength, + currentHopNumber, + 0, + 0 + ); newVertices.add(toVertex); } else { toVertex.setWeight(toVertex.getWeight() + signalStrength); @@ -422,7 +447,6 @@ private void addAndScoreNewVertices(Hop lastHop, Hop currentHop, Sampler sample, } } - // Having let the signals from the last results rattle around the graph // we have adjusted weights for the various vertices we encountered. // Now we review these new additions and remove those with the @@ -460,7 +484,7 @@ private void trimNewAdditions(Hop currentHop, ArrayList newConnectio } } } - //TODO right now we only trim down to the best N vertices. We might also want to offer + // TODO right now we only trim down to the best N vertices. We might also want to offer // clients the option to limit to the best M connections. One scenario where this is required // is if the "from" and "to" nodes are a client-supplied set of includes e.g. a list of // music artists then the client may be wanting to draw only the most-interesting connections @@ -487,7 +511,7 @@ private double getExpandTotalSignalStrength(Hop lastHop, Hop currentHop, Sampler if (significantTerms != null) { for (Bucket bucket : significantTerms.getBuckets()) { if ((vr.fieldName().equals(lastVr.fieldName())) - && (bucket.getKeyAsString().equals(lastWaveTerm.getKeyAsString()))) { + && (bucket.getKeyAsString().equals(lastWaveTerm.getKeyAsString()))) { // don't count self joins (term A obviously co-occurs with term A) continue; } else { @@ -502,7 +526,7 @@ private double getExpandTotalSignalStrength(Hop lastHop, Hop currentHop, Sampler if (terms != null) { for (org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket bucket : terms.getBuckets()) { if ((vr.fieldName().equals(lastVr.fieldName())) - && (bucket.getKeyAsString().equals(lastWaveTerm.getKeyAsString()))) { + && (bucket.getKeyAsString().equals(lastWaveTerm.getKeyAsString()))) { // don't count self joins (term A obviously co-occurs with term A) continue; } else { @@ -521,7 +545,7 @@ private double getExpandTotalSignalStrength(Hop lastHop, Hop currentHop, Sampler private void addUserDefinedIncludesToQuery(Hop hop, BoolQueryBuilder sourceTermsOrClause) { for (int i = 0; i < hop.getNumberVertexRequests(); i++) { - VertexRequest vr=hop.getVertexRequest(i); + VertexRequest vr = hop.getVertexRequest(i); if (vr.hasIncludeClauses()) { addNormalizedBoosts(sourceTermsOrClause, vr); } @@ -539,8 +563,9 @@ private void addBigOrClause(Map> lastHopFindings, BoolQueryB for (Entry> entry : lastHopFindings.entrySet()) { for (Vertex vertex : entry.getValue()) { sourceTermsOrClause.should( - QueryBuilders.constantScoreQuery( - QueryBuilders.termQuery(vertex.getField(), vertex.getTerm())).boost((float) vertex.getWeight())); + QueryBuilders.constantScoreQuery(QueryBuilders.termQuery(vertex.getField(), vertex.getTerm())) + .boost((float) vertex.getWeight()) + ); } } @@ -564,8 +589,7 @@ private void addBigOrClause(Map> lastHopFindings, BoolQueryB public synchronized void start() { try { - final SearchRequest searchRequest = new SearchRequest(request.indices()).indicesOptions( - request.indicesOptions()); + final SearchRequest searchRequest = new SearchRequest(request.indices()).indicesOptions(request.indicesOptions()); if (request.routing() != null) { searchRequest.routing(request.routing()); } @@ -575,7 +599,7 @@ public synchronized void start() { AggregationBuilder rootSampleAgg = null; if (request.sampleDiversityField() != null) { DiversifiedAggregationBuilder diversifiedRootSampleAgg = AggregationBuilders.diversifiedSampler("sample") - .shardSize(request.sampleSize()); + .shardSize(request.sampleSize()); diversifiedRootSampleAgg.field(request.sampleDiversityField()); diversifiedRootSampleAgg.maxDocsPerValue(request.maxDocsPerDiversityValue()); rootSampleAgg = diversifiedRootSampleAgg; @@ -583,14 +607,11 @@ public synchronized void start() { rootSampleAgg = AggregationBuilders.sampler("sample").shardSize(request.sampleSize()); } - - Hop rootHop = request.getHop(0); // Add any user-supplied criteria to the root query as a should clause rootBool.must(rootHop.guidingQuery()); - // If any of the root terms have an "include" restriction then // we add a root-level MUST clause that // mandates that at least one of the potentially many terms of @@ -601,16 +622,18 @@ public synchronized void start() { rootBool.must(includesContainer); } - for (int i = 0; i < rootHop.getNumberVertexRequests(); i++) { VertexRequest vr = rootHop.getVertexRequest(i); if (request.useSignificance()) { SignificantTermsAggregationBuilder sigBuilder = AggregationBuilders.significantTerms("field" + i); - sigBuilder.field(vr.fieldName()).shardMinDocCount(vr.shardMinDocCount()).minDocCount(vr.minDocCount()) - // Map execution mode used because Sampler agg - // keeps us focused on smaller sets of high quality - // docs and therefore examine smaller volumes of terms - .executionHint("map").size(vr.size()); + sigBuilder.field(vr.fieldName()) + .shardMinDocCount(vr.shardMinDocCount()) + .minDocCount(vr.minDocCount()) + // Map execution mode used because Sampler agg + // keeps us focused on smaller sets of high quality + // docs and therefore examine smaller volumes of terms + .executionHint("map") + .size(vr.size()); // It is feasible that clients could provide a choice of // significance heuristic at some point e.g: // sigBuilder.significanceHeuristic(new @@ -618,7 +641,7 @@ public synchronized void start() { if (vr.hasIncludeClauses()) { String[] includes = vr.includeValuesAsStringArray(); - sigBuilder.includeExclude(new IncludeExclude(includes,null)); + sigBuilder.includeExclude(new IncludeExclude(includes, null)); sigBuilder.size(includes.length); } if (vr.hasExcludeClauses()) { @@ -631,11 +654,11 @@ public synchronized void start() { // thinking about certainty of significance scores - // perhaps less necessary when considering popularity // termsBuilder.field(vr.fieldName()).shardMinDocCount(shardMinDocCount) - // .minDocCount(minDocCount).executionHint("map").size(vr.size()); + // .minDocCount(minDocCount).executionHint("map").size(vr.size()); termsBuilder.field(vr.fieldName()).executionHint("map").size(vr.size()); if (vr.hasIncludeClauses()) { String[] includes = vr.includeValuesAsStringArray(); - termsBuilder.includeExclude(new IncludeExclude(includes,null)); + termsBuilder.includeExclude(new IncludeExclude(includes, null)); termsBuilder.size(includes.length); } if (vr.hasExcludeClauses()) { @@ -645,11 +668,8 @@ public synchronized void start() { } } - // Run the search - SearchSourceBuilder source = new SearchSourceBuilder() - .query(rootBool) - .aggregation(rootSampleAgg).size(0); + SearchSourceBuilder source = new SearchSourceBuilder().query(rootBool).aggregation(rootSampleAgg).size(0); if (request.timeout() != null) { source.timeout(request.timeout()); } @@ -665,7 +685,6 @@ public void onResponse(SearchResponse searchResponse) { // Determine the total scores for all interesting terms double totalSignalStrength = getInitialTotalSignalStrength(rootHop, sample); - // Now gather the best matching terms and compute signal weight according to their // share of the total signal strength for (int j = 0; j < rootHop.getNumberVertexRequests(); j++) { @@ -675,8 +694,14 @@ public void onResponse(SearchResponse searchResponse) { List buckets = significantTerms.getBuckets(); for (Bucket bucket : buckets) { double signalWeight = bucket.getSignificanceScore() / totalSignalStrength; - addVertex(vr.fieldName(), bucket.getKeyAsString(), signalWeight, - currentHopNumber, bucket.getSupersetDf(), bucket.getSubsetDf()); + addVertex( + vr.fieldName(), + bucket.getKeyAsString(), + signalWeight, + currentHopNumber, + bucket.getSupersetDf(), + bucket.getSubsetDf() + ); } } else { Terms terms = sample.getAggregations().get("field" + j); @@ -724,7 +749,6 @@ private double getInitialTotalSignalStrength(Hop rootHop, Sampler sample) { private void addNormalizedBoosts(BoolQueryBuilder includesContainer, VertexRequest vr) { TermBoost[] termBoosts = vr.includeValues(); - if ((includesContainer.should().size() + termBoosts.length) > BooleanQuery.getMaxClauseCount()) { // Too many terms - we need a cheaper form of query to execute this List termValues = new ArrayList<>(); @@ -772,6 +796,5 @@ protected GraphExploreResponse buildResponse() { return new GraphExploreResponse(took, timedOut.get(), shardFailures, vertices, connections, request.returnDetailedInfo()); } - } } diff --git a/x-pack/plugin/graph/src/main/java/org/elasticsearch/xpack/graph/rest/action/RestGraphAction.java b/x-pack/plugin/graph/src/main/java/org/elasticsearch/xpack/graph/rest/action/RestGraphAction.java index df1fd9ba746e4..f0ceb9a4b2f9b 100644 --- a/x-pack/plugin/graph/src/main/java/org/elasticsearch/xpack/graph/rest/action/RestGraphAction.java +++ b/x-pack/plugin/graph/src/main/java/org/elasticsearch/xpack/graph/rest/action/RestGraphAction.java @@ -10,12 +10,10 @@ import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.common.logging.DeprecationLogger; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; +import org.elasticsearch.common.logging.DeprecationLogger; import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.protocol.xpack.graph.GraphExploreRequest; import org.elasticsearch.protocol.xpack.graph.GraphExploreRequest.TermBoost; import org.elasticsearch.protocol.xpack.graph.Hop; @@ -23,6 +21,8 @@ import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestToXContentListener; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.HashMap; @@ -41,8 +41,7 @@ public class RestGraphAction extends BaseRestHandler { private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(RestGraphAction.class); - public static final String TYPES_DEPRECATION_MESSAGE = "[types removal]" + - " Specifying types in graph requests is deprecated."; + public static final String TYPES_DEPRECATION_MESSAGE = "[types removal]" + " Specifying types in graph requests is deprecated."; private static final String URI_BASE = "/_xpack"; public static final ParseField TIMEOUT_FIELD = new ParseField("timeout"); @@ -68,17 +67,19 @@ public class RestGraphAction extends BaseRestHandler { public List routes() { return List.of( Route.builder(GET, "/{index}/_graph/explore") - .replaces(GET, "/{index}" + URI_BASE + "/graph/_explore", RestApiVersion.V_7).build(), + .replaces(GET, "/{index}" + URI_BASE + "/graph/_explore", RestApiVersion.V_7) + .build(), Route.builder(POST, "/{index}/_graph/explore") - .replaces(POST, "/{index}" + URI_BASE + "/graph/_explore", RestApiVersion.V_7).build(), - Route.builder(GET, "/{index}/{type}/_graph/explore") - .deprecated(TYPES_DEPRECATION_MESSAGE, RestApiVersion.V_7).build(), + .replaces(POST, "/{index}" + URI_BASE + "/graph/_explore", RestApiVersion.V_7) + .build(), + Route.builder(GET, "/{index}/{type}/_graph/explore").deprecated(TYPES_DEPRECATION_MESSAGE, RestApiVersion.V_7).build(), Route.builder(GET, "/{index}/{type}" + URI_BASE + "/graph/_explore") - .deprecated(TYPES_DEPRECATION_MESSAGE, RestApiVersion.V_7).build(), - Route.builder(POST, "/{index}/{type}/_graph/explore") - .deprecated(TYPES_DEPRECATION_MESSAGE, RestApiVersion.V_7).build(), + .deprecated(TYPES_DEPRECATION_MESSAGE, RestApiVersion.V_7) + .build(), + Route.builder(POST, "/{index}/{type}/_graph/explore").deprecated(TYPES_DEPRECATION_MESSAGE, RestApiVersion.V_7).build(), Route.builder(POST, "/{index}/{type}" + URI_BASE + "/graph/_explore") - .deprecated(TYPES_DEPRECATION_MESSAGE, RestApiVersion.V_7).build() + .deprecated(TYPES_DEPRECATION_MESSAGE, RestApiVersion.V_7) + .build() ); } @@ -111,8 +112,10 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC XContentParser.Token token = parser.nextToken(); if (token != XContentParser.Token.START_OBJECT) { - throw new ElasticsearchParseException("failed to parse search source. source must be an object, but found [{}] instead", - token.name()); + throw new ElasticsearchParseException( + "failed to parse search source. source must be an object, but found [{}] instead", + token.name() + ); } parseHop(parser, currentHop, graphRequest); } @@ -142,7 +145,9 @@ private void parseHop(XContentParser parser, Hop currentHop, GraphExploreRequest } else if (CONTROLS_FIELD.match(fieldName, parser.getDeprecationHandler())) { if (currentHop.getParentHop() != null) { throw new ElasticsearchParseException( - "Controls are a global setting that can only be set in the root " + fieldName, token.name()); + "Controls are a global setting that can only be set in the root " + fieldName, + token.name() + ); } parseControls(parser, graphRequest); } else { @@ -155,8 +160,7 @@ private void parseHop(XContentParser parser, Hop currentHop, GraphExploreRequest } } - private void parseVertices(XContentParser parser, Hop currentHop) - throws IOException { + private void parseVertices(XContentParser parser, Hop currentHop) throws IOException { XContentParser.Token token; String fieldName = null; @@ -178,8 +182,13 @@ private void parseVertices(XContentParser parser, Hop currentHop) if (INCLUDE_FIELD.match(fieldName, parser.getDeprecationHandler())) { if (excludes != null) { throw new ElasticsearchParseException( - "Graph vertices definition cannot contain both "+INCLUDE_FIELD.getPreferredName()+" and " - +EXCLUDE_FIELD.getPreferredName()+" clauses", token.name()); + "Graph vertices definition cannot contain both " + + INCLUDE_FIELD.getPreferredName() + + " and " + + EXCLUDE_FIELD.getPreferredName() + + " clauses", + token.name() + ); } includes = new HashMap<>(); while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { @@ -195,29 +204,40 @@ private void parseVertices(XContentParser parser, Hop currentHop) includeTerm = parser.text(); } else { throw new ElasticsearchParseException( - "Graph vertices definition " + INCLUDE_FIELD.getPreferredName() + - " clause has invalid property:" + fieldName); + "Graph vertices definition " + + INCLUDE_FIELD.getPreferredName() + + " clause has invalid property:" + + fieldName + ); } } else if (token == XContentParser.Token.VALUE_NUMBER) { if (BOOST_FIELD.match(fieldName, parser.getDeprecationHandler())) { boost = parser.floatValue(); } else { throw new ElasticsearchParseException( - "Graph vertices definition " + INCLUDE_FIELD.getPreferredName() + - " clause has invalid property:" + fieldName); + "Graph vertices definition " + + INCLUDE_FIELD.getPreferredName() + + " clause has invalid property:" + + fieldName + ); } } else { throw new ElasticsearchParseException( - "Graph vertices definition " + INCLUDE_FIELD.getPreferredName() + - " clause has invalid property type:"+ token.name()); + "Graph vertices definition " + + INCLUDE_FIELD.getPreferredName() + + " clause has invalid property type:" + + token.name() + ); } } } if (includeTerm == null) { throw new ElasticsearchParseException( - "Graph vertices definition " + INCLUDE_FIELD.getPreferredName() + - " clause has missing object property for term"); + "Graph vertices definition " + + INCLUDE_FIELD.getPreferredName() + + " clause has missing object property for term" + ); } includes.put(includeTerm, new TermBoost(includeTerm, boost)); } else if (token == XContentParser.Token.VALUE_STRING) { @@ -225,24 +245,33 @@ private void parseVertices(XContentParser parser, Hop currentHop) includes.put(term, new TermBoost(term, 1f)); } else { throw new ElasticsearchParseException( - "Graph vertices definition " + INCLUDE_FIELD.getPreferredName() + - " clauses must be string terms or Objects with terms and boosts, not" - + token.name()); + "Graph vertices definition " + + INCLUDE_FIELD.getPreferredName() + + " clauses must be string terms or Objects with terms and boosts, not" + + token.name() + ); } } } else if (EXCLUDE_FIELD.match(fieldName, parser.getDeprecationHandler())) { if (includes != null) { throw new ElasticsearchParseException( - "Graph vertices definition cannot contain both "+ INCLUDE_FIELD.getPreferredName()+ - " and "+EXCLUDE_FIELD.getPreferredName()+" clauses", token.name()); + "Graph vertices definition cannot contain both " + + INCLUDE_FIELD.getPreferredName() + + " and " + + EXCLUDE_FIELD.getPreferredName() + + " clauses", + token.name() + ); } excludes = new HashSet<>(); while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { excludes.add(parser.text()); } } else { - throw new ElasticsearchParseException("Illegal property in graph vertices definition " + fieldName, - token.name()); + throw new ElasticsearchParseException( + "Illegal property in graph vertices definition " + fieldName, + token.name() + ); } } if (token == XContentParser.Token.VALUE_STRING) { @@ -288,7 +317,6 @@ private void parseVertices(XContentParser parser, Hop currentHop) } - private void parseControls(XContentParser parser, GraphExploreRequest graphRequest) throws IOException { XContentParser.Token token; @@ -309,7 +337,7 @@ private void parseControls(XContentParser parser, GraphExploreRequest graphReque graphRequest.useSignificance(parser.booleanValue()); } else if (RETURN_DETAILED_INFO.match(fieldName, parser.getDeprecationHandler())) { graphRequest.returnDetailedInfo(parser.booleanValue()); - } else{ + } else { throw new ElasticsearchParseException("Unknown boolean property: [" + fieldName + "]"); } } else if (token == XContentParser.Token.VALUE_STRING) { diff --git a/x-pack/plugin/graph/src/test/java/org/elasticsearch/xpack/graph/GraphInfoTransportActionTests.java b/x-pack/plugin/graph/src/test/java/org/elasticsearch/xpack/graph/GraphInfoTransportActionTests.java index 06276c2e2a028..82440a61cb78d 100644 --- a/x-pack/plugin/graph/src/test/java/org/elasticsearch/xpack/graph/GraphInfoTransportActionTests.java +++ b/x-pack/plugin/graph/src/test/java/org/elasticsearch/xpack/graph/GraphInfoTransportActionTests.java @@ -33,13 +33,24 @@ public void init() throws Exception { public void testAvailable() throws Exception { GraphInfoTransportAction featureSet = new GraphInfoTransportAction( - mock(TransportService.class), mock(ActionFilters.class), Settings.EMPTY, licenseState); + mock(TransportService.class), + mock(ActionFilters.class), + Settings.EMPTY, + licenseState + ); boolean available = randomBoolean(); when(licenseState.isAllowed(Graph.GRAPH_FEATURE)).thenReturn(available); assertThat(featureSet.available(), is(available)); - var usageAction = new GraphUsageTransportAction(mock(TransportService.class), null, null, - mock(ActionFilters.class), null, Settings.EMPTY, licenseState); + var usageAction = new GraphUsageTransportAction( + mock(TransportService.class), + null, + null, + mock(ActionFilters.class), + null, + Settings.EMPTY, + licenseState + ); PlainActionFuture future = new PlainActionFuture<>(); usageAction.masterOperation(null, null, null, future); XPackFeatureSet.Usage usage = future.get().getUsage(); @@ -62,11 +73,22 @@ public void testEnabled() throws Exception { settings.put("xpack.graph.enabled", enabled); } GraphInfoTransportAction featureSet = new GraphInfoTransportAction( - mock(TransportService.class), mock(ActionFilters.class), settings.build(), licenseState); + mock(TransportService.class), + mock(ActionFilters.class), + settings.build(), + licenseState + ); assertThat(featureSet.enabled(), is(enabled)); - GraphUsageTransportAction usageAction = new GraphUsageTransportAction(mock(TransportService.class), - null, null, mock(ActionFilters.class), null, settings.build(), licenseState); + GraphUsageTransportAction usageAction = new GraphUsageTransportAction( + mock(TransportService.class), + null, + null, + mock(ActionFilters.class), + null, + settings.build(), + licenseState + ); PlainActionFuture future = new PlainActionFuture<>(); usageAction.masterOperation(null, null, null, future); XPackFeatureSet.Usage usage = future.get().getUsage(); diff --git a/x-pack/plugin/graph/src/test/java/org/elasticsearch/xpack/graph/rest/action/RestGraphActionTests.java b/x-pack/plugin/graph/src/test/java/org/elasticsearch/xpack/graph/rest/action/RestGraphActionTests.java index 9acca76d71435..baab860b95005 100644 --- a/x-pack/plugin/graph/src/test/java/org/elasticsearch/xpack/graph/rest/action/RestGraphActionTests.java +++ b/x-pack/plugin/graph/src/test/java/org/elasticsearch/xpack/graph/rest/action/RestGraphActionTests.java @@ -8,7 +8,6 @@ package org.elasticsearch.xpack.graph.rest.action; import org.elasticsearch.common.bytes.BytesArray; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.core.Tuple; import org.elasticsearch.protocol.xpack.graph.GraphExploreRequest; @@ -16,6 +15,7 @@ import org.elasticsearch.rest.RestRequest; import org.elasticsearch.test.rest.FakeRestRequest; import org.elasticsearch.test.rest.RestActionTestCase; +import org.elasticsearch.xcontent.XContentType; import org.junit.Before; import org.mockito.Mockito; @@ -38,21 +38,21 @@ public void setUpAction() { } public void testTypeInPath() { - for (Tuple methodAndPath : - List.of( - Tuple.tuple(RestRequest.Method.GET, "/some_index/some_type/_graph/explore"), - Tuple.tuple(RestRequest.Method.POST, "/some_index/some_type/_graph/explore"), - Tuple.tuple(RestRequest.Method.GET, "/some_index/some_type/_xpack/graph/_explore"), - Tuple.tuple(RestRequest.Method.POST, "/some_index/some_type/_xpack/graph/_explore") - )) { - - RestRequest request = new FakeRestRequest.Builder(xContentRegistry()) - .withHeaders(Map.of("Accept", compatibleMediaType, "Content-Type", - Collections.singletonList(compatibleMediaType(XContentType.VND_JSON, RestApiVersion.V_7)))) - .withMethod(methodAndPath.v1()) - .withPath(methodAndPath.v2()) - .withContent(new BytesArray("{}"), null) - .build(); + for (Tuple methodAndPath : List.of( + Tuple.tuple(RestRequest.Method.GET, "/some_index/some_type/_graph/explore"), + Tuple.tuple(RestRequest.Method.POST, "/some_index/some_type/_graph/explore"), + Tuple.tuple(RestRequest.Method.GET, "/some_index/some_type/_xpack/graph/_explore"), + Tuple.tuple(RestRequest.Method.POST, "/some_index/some_type/_xpack/graph/_explore") + )) { + + RestRequest request = new FakeRestRequest.Builder(xContentRegistry()).withHeaders( + Map.of( + "Accept", + compatibleMediaType, + "Content-Type", + Collections.singletonList(compatibleMediaType(XContentType.VND_JSON, RestApiVersion.V_7)) + ) + ).withMethod(methodAndPath.v1()).withPath(methodAndPath.v2()).withContent(new BytesArray("{}"), null).build(); dispatchRequest(request); assertWarnings(RestGraphAction.TYPES_DEPRECATION_MESSAGE); diff --git a/x-pack/plugin/identity-provider/qa/idp-rest-tests/src/javaRestTest/java/org/elasticsearch/xpack/idp/IdentityProviderAuthenticationIT.java b/x-pack/plugin/identity-provider/qa/idp-rest-tests/src/javaRestTest/java/org/elasticsearch/xpack/idp/IdentityProviderAuthenticationIT.java index 19d6a5732faa9..521cff20114ae 100644 --- a/x-pack/plugin/identity-provider/qa/idp-rest-tests/src/javaRestTest/java/org/elasticsearch/xpack/idp/IdentityProviderAuthenticationIT.java +++ b/x-pack/plugin/identity-provider/qa/idp-rest-tests/src/javaRestTest/java/org/elasticsearch/xpack/idp/IdentityProviderAuthenticationIT.java @@ -12,11 +12,11 @@ import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.Response; import org.elasticsearch.client.RestClient; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ObjectPath; import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.security.action.saml.SamlPrepareAuthenticationResponse; @@ -46,26 +46,27 @@ public class IdentityProviderAuthenticationIT extends IdpRestTestCase { @Before public void setupSecurityData() throws IOException { setUserPassword("kibana_system", new SecureString("kibana_system".toCharArray())); - createApplicationPrivileges("elastic-cloud", Map.ofEntries( - Map.entry("deployment_admin", Set.of("sso:admin")), - Map.entry("deployment_viewer", Set.of("sso:viewer")) - )); + createApplicationPrivileges( + "elastic-cloud", + Map.ofEntries(Map.entry("deployment_admin", Set.of("sso:admin")), Map.entry("deployment_viewer", Set.of("sso:viewer"))) + ); } public void testRegistrationAndIdpInitiatedSso() throws Exception { final Map request = Map.ofEntries( Map.entry("name", "Test SP"), Map.entry("acs", SP_ACS), - Map.entry("privileges", Map.ofEntries( - Map.entry("resource", SP_ENTITY_ID), - Map.entry("roles", List.of("sso:(\\w+)")) - )), - Map.entry("attributes", Map.ofEntries( - Map.entry("principal", "https://idp.test.es.elasticsearch.org/attribute/principal"), - Map.entry("name", "https://idp.test.es.elasticsearch.org/attribute/name"), - Map.entry("email", "https://idp.test.es.elasticsearch.org/attribute/email"), - Map.entry("roles", "https://idp.test.es.elasticsearch.org/attribute/roles") - ))); + Map.entry("privileges", Map.ofEntries(Map.entry("resource", SP_ENTITY_ID), Map.entry("roles", List.of("sso:(\\w+)")))), + Map.entry( + "attributes", + Map.ofEntries( + Map.entry("principal", "https://idp.test.es.elasticsearch.org/attribute/principal"), + Map.entry("name", "https://idp.test.es.elasticsearch.org/attribute/name"), + Map.entry("email", "https://idp.test.es.elasticsearch.org/attribute/email"), + Map.entry("roles", "https://idp.test.es.elasticsearch.org/attribute/roles") + ) + ) + ); final SamlServiceProviderIndex.DocumentVersion docVersion = createServiceProvider(SP_ENTITY_ID, request); checkIndexDoc(docVersion); ensureGreen(SamlServiceProviderIndex.INDEX_NAME); @@ -77,16 +78,17 @@ public void testRegistrationAndSpInitiatedSso() throws Exception { final Map request = Map.ofEntries( Map.entry("name", "Test SP"), Map.entry("acs", SP_ACS), - Map.entry("privileges", Map.ofEntries( - Map.entry("resource", SP_ENTITY_ID), - Map.entry("roles", List.of("sso:(\\w+)")) - )), - Map.entry("attributes", Map.ofEntries( - Map.entry("principal", "https://idp.test.es.elasticsearch.org/attribute/principal"), - Map.entry("name", "https://idp.test.es.elasticsearch.org/attribute/name"), - Map.entry("email", "https://idp.test.es.elasticsearch.org/attribute/email"), - Map.entry("roles", "https://idp.test.es.elasticsearch.org/attribute/roles") - ))); + Map.entry("privileges", Map.ofEntries(Map.entry("resource", SP_ENTITY_ID), Map.entry("roles", List.of("sso:(\\w+)")))), + Map.entry( + "attributes", + Map.ofEntries( + Map.entry("principal", "https://idp.test.es.elasticsearch.org/attribute/principal"), + Map.entry("name", "https://idp.test.es.elasticsearch.org/attribute/name"), + Map.entry("email", "https://idp.test.es.elasticsearch.org/attribute/email"), + Map.entry("roles", "https://idp.test.es.elasticsearch.org/attribute/roles") + ) + ) + ); final SamlServiceProviderIndex.DocumentVersion docVersion = createServiceProvider(SP_ENTITY_ID, request); checkIndexDoc(docVersion); ensureGreen(SamlServiceProviderIndex.INDEX_NAME); @@ -126,15 +128,24 @@ private SamlPrepareAuthenticationResponse generateSamlAuthnRequest(String realmN private String generateSamlResponse(String entityId, String acs, @Nullable Map authnState) throws Exception { final Request request = new Request("POST", "/_idp/saml/init"); if (authnState != null && authnState.isEmpty() == false) { - request.setJsonEntity("{\"entity_id\":\"" + entityId + "\", \"acs\":\"" + acs + "\"," + - "\"authn_state\":" + Strings.toString(JsonXContent.contentBuilder().map(authnState)) + "}"); + request.setJsonEntity( + "{\"entity_id\":\"" + + entityId + + "\", \"acs\":\"" + + acs + + "\"," + + "\"authn_state\":" + + Strings.toString(JsonXContent.contentBuilder().map(authnState)) + + "}" + ); } else { request.setJsonEntity("{\"entity_id\":\"" + entityId + "\", \"acs\":\"" + acs + "\"}"); } - request.setOptions(RequestOptions.DEFAULT.toBuilder() - .addHeader("es-secondary-authorization", basicAuthHeaderValue("idp_user", - new SecureString("idp-password".toCharArray()))) - .build()); + request.setOptions( + RequestOptions.DEFAULT.toBuilder() + .addHeader("es-secondary-authorization", basicAuthHeaderValue("idp_user", new SecureString("idp-password".toCharArray()))) + .build() + ); final Response response = client().performRequest(request); final Map map = entityAsMap(response); assertThat(ObjectPath.eval("service_provider.entity_id", map), equalTo(entityId)); @@ -170,8 +181,10 @@ private void authenticateWithSamlResponse(String samlResponse, @Nullable String assertThat(ObjectPath.eval("username", authMap), instanceOf(String.class)); assertThat(ObjectPath.eval("username", authMap), equalTo("idp_user")); assertThat(ObjectPath.eval("metadata.saml_nameid_format", authMap), instanceOf(String.class)); - assertThat(ObjectPath.eval("metadata.saml_nameid_format", authMap), - equalTo("urn:oasis:names:tc:SAML:2.0:nameid-format:transient")); + assertThat( + ObjectPath.eval("metadata.saml_nameid_format", authMap), + equalTo("urn:oasis:names:tc:SAML:2.0:nameid-format:transient") + ); assertThat(ObjectPath.eval("metadata.saml_roles", authMap), instanceOf(List.class)); assertThat(ObjectPath.eval("metadata.saml_roles", authMap), hasSize(1)); assertThat(ObjectPath.eval("metadata.saml_roles", authMap), contains("viewer")); @@ -181,14 +194,19 @@ private void authenticateWithSamlResponse(String samlResponse, @Nullable String private RestClient restClientWithToken(String accessToken) throws IOException { return buildClient( Settings.builder().put(ThreadContext.PREFIX + ".Authorization", "Bearer " + accessToken).build(), - getClusterHosts().toArray(new HttpHost[getClusterHosts().size()])); + getClusterHosts().toArray(new HttpHost[getClusterHosts().size()]) + ); } private RestClient restClientAsKibanaSystem() throws IOException { return buildClient( - Settings.builder().put(ThreadContext.PREFIX + ".Authorization", basicAuthHeaderValue("kibana_system", - new SecureString("kibana_system".toCharArray()))).build(), - getClusterHosts().toArray(new HttpHost[getClusterHosts().size()])); + Settings.builder() + .put( + ThreadContext.PREFIX + ".Authorization", + basicAuthHeaderValue("kibana_system", new SecureString("kibana_system".toCharArray())) + ) + .build(), + getClusterHosts().toArray(new HttpHost[getClusterHosts().size()]) + ); } } - diff --git a/x-pack/plugin/identity-provider/qa/idp-rest-tests/src/javaRestTest/java/org/elasticsearch/xpack/idp/IdpRestTestCase.java b/x-pack/plugin/identity-provider/qa/idp-rest-tests/src/javaRestTest/java/org/elasticsearch/xpack/idp/IdpRestTestCase.java index 384e2aea8449c..568a68181f1e2 100644 --- a/x-pack/plugin/identity-provider/qa/idp-rest-tests/src/javaRestTest/java/org/elasticsearch/xpack/idp/IdpRestTestCase.java +++ b/x-pack/plugin/identity-provider/qa/idp-rest-tests/src/javaRestTest/java/org/elasticsearch/xpack/idp/IdpRestTestCase.java @@ -26,9 +26,9 @@ import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.test.rest.ESRestTestCase; import org.elasticsearch.xcontent.ObjectPath; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.rest.ESRestTestCase; import org.elasticsearch.xpack.idp.saml.sp.SamlServiceProviderIndex; import java.io.IOException; @@ -50,26 +50,18 @@ public abstract class IdpRestTestCase extends ESRestTestCase { @Override protected Settings restAdminSettings() { String token = basicAuthHeaderValue("admin_user", new SecureString("admin-password".toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } @Override protected Settings restClientSettings() { String token = basicAuthHeaderValue("idp_admin", new SecureString("idp-password".toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } private RestHighLevelClient getHighLevelAdminClient() { if (highLevelAdminClient == null) { - highLevelAdminClient = new RestHighLevelClient( - adminClient(), - ignore -> { - }, - List.of()) { + highLevelAdminClient = new RestHighLevelClient(adminClient(), ignore -> {}, List.of()) { }; } return highLevelAdminClient; @@ -89,8 +81,12 @@ protected void deleteUser(String username) throws IOException { client.security().deleteUser(request, RequestOptions.DEFAULT); } - protected void createRole(String name, Collection clusterPrivileges, Collection indicesPrivileges, - Collection applicationPrivileges) throws IOException { + protected void createRole( + String name, + Collection clusterPrivileges, + Collection indicesPrivileges, + Collection applicationPrivileges + ) throws IOException { final RestHighLevelClient client = getHighLevelAdminClient(); final Role role = Role.builder() .name(name) @@ -109,7 +105,8 @@ protected void deleteRole(String name) throws IOException { protected void createApplicationPrivileges(String applicationName, Map> privileges) throws IOException { final RestHighLevelClient client = getHighLevelAdminClient(); - final List applicationPrivileges = privileges.entrySet().stream() + final List applicationPrivileges = privileges.entrySet() + .stream() .map(e -> new ApplicationPrivilege(applicationName, e.getKey(), List.copyOf(e.getValue()), null)) .collect(Collectors.toUnmodifiableList()); final PutPrivilegesRequest request = new PutPrivilegesRequest(applicationPrivileges, RefreshPolicy.IMMEDIATE); diff --git a/x-pack/plugin/identity-provider/qa/idp-rest-tests/src/javaRestTest/java/org/elasticsearch/xpack/idp/ManageServiceProviderRestIT.java b/x-pack/plugin/identity-provider/qa/idp-rest-tests/src/javaRestTest/java/org/elasticsearch/xpack/idp/ManageServiceProviderRestIT.java index 4e35aa71b2542..943453e3e24d9 100644 --- a/x-pack/plugin/identity-provider/qa/idp-rest-tests/src/javaRestTest/java/org/elasticsearch/xpack/idp/ManageServiceProviderRestIT.java +++ b/x-pack/plugin/identity-provider/qa/idp-rest-tests/src/javaRestTest/java/org/elasticsearch/xpack/idp/ManageServiceProviderRestIT.java @@ -35,10 +35,10 @@ public class ManageServiceProviderRestIT extends IdpRestTestCase { @Before public void defineApplicationPrivileges() throws IOException { - super.createApplicationPrivileges("elastic-cloud", Map.ofEntries( - Map.entry("deployment_admin", Set.of("sso:superuser")), - Map.entry("deployment_viewer", Set.of("sso:viewer")) - )); + super.createApplicationPrivileges( + "elastic-cloud", + Map.ofEntries(Map.entry("deployment_admin", Set.of("sso:superuser")), Map.entry("deployment_viewer", Set.of("sso:viewer"))) + ); } public void testCreateAndDeleteServiceProvider() throws Exception { @@ -46,16 +46,17 @@ public void testCreateAndDeleteServiceProvider() throws Exception { final Map request = Map.ofEntries( Map.entry("name", "Test SP"), Map.entry("acs", "https://sp1.test.es.elasticsearch.org/saml/acs"), - Map.entry("privileges", Map.ofEntries( - Map.entry("resource", entityId), - Map.entry("roles", Set.of("role:(\\w+)")) - )), - Map.entry("attributes", Map.ofEntries( - Map.entry("principal", "https://idp.test.es.elasticsearch.org/attribute/principal"), - Map.entry("name", "https://idp.test.es.elasticsearch.org/attribute/name"), - Map.entry("email", "https://idp.test.es.elasticsearch.org/attribute/email"), - Map.entry("roles", "https://idp.test.es.elasticsearch.org/attribute/roles") - ))); + Map.entry("privileges", Map.ofEntries(Map.entry("resource", entityId), Map.entry("roles", Set.of("role:(\\w+)")))), + Map.entry( + "attributes", + Map.ofEntries( + Map.entry("principal", "https://idp.test.es.elasticsearch.org/attribute/principal"), + Map.entry("name", "https://idp.test.es.elasticsearch.org/attribute/name"), + Map.entry("email", "https://idp.test.es.elasticsearch.org/attribute/email"), + Map.entry("roles", "https://idp.test.es.elasticsearch.org/attribute/roles") + ) + ) + ); final DocumentVersion docVersion = createServiceProvider(entityId, request); checkIndexDoc(docVersion); ensureGreen(SamlServiceProviderIndex.INDEX_NAME); @@ -66,8 +67,9 @@ public void testCreateAndDeleteServiceProvider() throws Exception { } private void deleteServiceProvider(String entityId, DocumentVersion version) throws IOException { - final Response response = client().performRequest(new Request("DELETE", - "/_idp/saml/sp/" + encode(entityId) + "?refresh=" + RefreshPolicy.IMMEDIATE.getValue())); + final Response response = client().performRequest( + new Request("DELETE", "/_idp/saml/sp/" + encode(entityId) + "?refresh=" + RefreshPolicy.IMMEDIATE.getValue()) + ); final Map map = entityAsMap(response); assertThat(ObjectPath.eval("document._id", map), equalTo(version.id)); diff --git a/x-pack/plugin/identity-provider/qa/idp-rest-tests/src/javaRestTest/java/org/elasticsearch/xpack/idp/WildcardServiceProviderRestIT.java b/x-pack/plugin/identity-provider/qa/idp-rest-tests/src/javaRestTest/java/org/elasticsearch/xpack/idp/WildcardServiceProviderRestIT.java index d5f0120797464..a7d8c9c798a08 100644 --- a/x-pack/plugin/identity-provider/qa/idp-rest-tests/src/javaRestTest/java/org/elasticsearch/xpack/idp/WildcardServiceProviderRestIT.java +++ b/x-pack/plugin/identity-provider/qa/idp-rest-tests/src/javaRestTest/java/org/elasticsearch/xpack/idp/WildcardServiceProviderRestIT.java @@ -37,10 +37,10 @@ public class WildcardServiceProviderRestIT extends IdpRestTestCase { @Before public void defineApplicationPrivileges() throws IOException { - super.createApplicationPrivileges("elastic-cloud", Map.ofEntries( - Map.entry("deployment_admin", Set.of("sso:admin")), - Map.entry("deployment_viewer", Set.of("sso:viewer")) - )); + super.createApplicationPrivileges( + "elastic-cloud", + Map.ofEntries(Map.entry("deployment_admin", Set.of("sso:admin")), Map.entry("deployment_viewer", Set.of("sso:viewer"))) + ); } public void testGetWildcardServiceProviderMetadata() throws Exception { @@ -65,7 +65,9 @@ public void testInitSingleSignOnToWildcardServiceProvider() throws Exception { final User user = createUser(username, password, roleName); final ApplicationResourcePrivileges applicationPrivilege = new ApplicationResourcePrivileges( - "elastic-cloud", List.of("sso:admin"), List.of("sso:" + entityId) + "elastic-cloud", + List.of("sso:admin"), + List.of("sso:" + entityId) ); createRole(roleName, List.of(), List.of(), List.of(applicationPrivilege)); @@ -99,8 +101,14 @@ private void getMetadata(String entityId, String acs) throws IOException { private String initSso(String entityId, String acs, UsernamePasswordToken secondaryAuth) throws IOException { final Request request = new Request("POST", "/_idp/saml/init/"); request.setJsonEntity(toJson(Map.of("entity_id", entityId, "acs", acs))); - request.setOptions(request.getOptions().toBuilder().addHeader("es-secondary-authorization", - UsernamePasswordToken.basicAuthHeaderValue(secondaryAuth.principal(), secondaryAuth.credentials()))); + request.setOptions( + request.getOptions() + .toBuilder() + .addHeader( + "es-secondary-authorization", + UsernamePasswordToken.basicAuthHeaderValue(secondaryAuth.principal(), secondaryAuth.credentials()) + ) + ); Response response = client().performRequest(request); final Map map = entityAsMap(response); diff --git a/x-pack/plugin/identity-provider/src/internalClusterTest/java/org/elasticsearch/xpack/idp/action/SamlIdentityProviderTests.java b/x-pack/plugin/identity-provider/src/internalClusterTest/java/org/elasticsearch/xpack/idp/action/SamlIdentityProviderTests.java index 4362f01eb8042..401f658be67d7 100644 --- a/x-pack/plugin/identity-provider/src/internalClusterTest/java/org/elasticsearch/xpack/idp/action/SamlIdentityProviderTests.java +++ b/x-pack/plugin/identity-provider/src/internalClusterTest/java/org/elasticsearch/xpack/idp/action/SamlIdentityProviderTests.java @@ -16,14 +16,14 @@ import org.elasticsearch.client.ResponseException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.SecureString; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.test.ESIntegTestCase; import org.elasticsearch.test.junit.annotations.TestLogging; import org.elasticsearch.test.rest.yaml.ObjectPath; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xpack.core.security.action.CreateApiKeyRequestBuilder; import org.elasticsearch.xpack.core.security.action.CreateApiKeyResponse; import org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken; @@ -66,9 +66,11 @@ import static org.opensaml.saml.saml2.core.NameIDType.TRANSIENT; @ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.SUITE, numClientNodes = 0, numDataNodes = 0) -@TestLogging(value = "org.elasticsearch.xpack.idp.action.TransportPutSamlServiceProviderAction:TRACE," + - "org.elasticsearch.xpack.idp.saml.sp.SamlServiceProviderIndex:TRACE", - reason = "https://github.com/elastic/elasticsearch/issues/54423") +@TestLogging( + value = "org.elasticsearch.xpack.idp.action.TransportPutSamlServiceProviderAction:TRACE," + + "org.elasticsearch.xpack.idp.saml.sp.SamlServiceProviderIndex:TRACE", + reason = "https://github.com/elastic/elasticsearch/issues/54423" +) public class SamlIdentityProviderTests extends IdentityProviderIntegTestCase { private final SamlFactory samlFactory = new SamlFactory(); @@ -81,15 +83,18 @@ public void testIdpInitiatedSso() throws Exception { ensureGreen(SamlServiceProviderIndex.INDEX_NAME); // User login a.k.a exchange the user credentials for an API Key - final String apiKeyCredentials = getApiKeyFromCredentials(SAMPLE_IDPUSER_NAME, - new SecureString(SAMPLE_IDPUSER_PASSWORD.toCharArray())); + final String apiKeyCredentials = getApiKeyFromCredentials( + SAMPLE_IDPUSER_NAME, + new SecureString(SAMPLE_IDPUSER_PASSWORD.toCharArray()) + ); // Make a request to init an SSO flow with the API Key as secondary authentication Request request = new Request("POST", "/_idp/saml/init"); - request.setOptions(RequestOptions.DEFAULT.toBuilder() - .addHeader("Authorization", basicAuthHeaderValue(CONSOLE_USER_NAME, - new SecureString(CONSOLE_USER_PASSWORD.toCharArray()))) - .addHeader("es-secondary-authorization", "ApiKey " + apiKeyCredentials) - .build()); + request.setOptions( + RequestOptions.DEFAULT.toBuilder() + .addHeader("Authorization", basicAuthHeaderValue(CONSOLE_USER_NAME, new SecureString(CONSOLE_USER_PASSWORD.toCharArray()))) + .addHeader("es-secondary-authorization", "ApiKey " + apiKeyCredentials) + .build() + ); request.setJsonEntity("{ \"entity_id\": \"" + entityId + "\", \"acs\": \"" + acsUrl + "\" }"); Response initResponse = getRestClient().performRequest(request); ObjectPath objectPath = ObjectPath.createFromResponse(initResponse); @@ -113,15 +118,18 @@ public void testIdPInitiatedSsoFailsForUnknownSP() throws Exception { registerApplicationPrivileges(); ensureGreen(SamlServiceProviderIndex.INDEX_NAME); // User login a.k.a exchange the user credentials for an API Key - final String apiKeyCredentials = getApiKeyFromCredentials(SAMPLE_IDPUSER_NAME, - new SecureString(SAMPLE_IDPUSER_PASSWORD.toCharArray())); + final String apiKeyCredentials = getApiKeyFromCredentials( + SAMPLE_IDPUSER_NAME, + new SecureString(SAMPLE_IDPUSER_PASSWORD.toCharArray()) + ); // Make a request to init an SSO flow with the API Key as secondary authentication Request request = new Request("POST", "/_idp/saml/init"); - request.setOptions(RequestOptions.DEFAULT.toBuilder() - .addHeader("Authorization", basicAuthHeaderValue(CONSOLE_USER_NAME, - new SecureString(CONSOLE_USER_PASSWORD.toCharArray()))) - .addHeader("es-secondary-authorization", "ApiKey " + apiKeyCredentials) - .build()); + request.setOptions( + RequestOptions.DEFAULT.toBuilder() + .addHeader("Authorization", basicAuthHeaderValue(CONSOLE_USER_NAME, new SecureString(CONSOLE_USER_PASSWORD.toCharArray()))) + .addHeader("es-secondary-authorization", "ApiKey " + apiKeyCredentials) + .build() + ); request.setJsonEntity("{ \"entity_id\": \"" + entityId + randomAlphaOfLength(3) + "\", \"acs\": \"" + acsUrl + "\" }"); ResponseException e = expectThrows(ResponseException.class, () -> getRestClient().performRequest(request)); assertThat(e.getMessage(), containsString("is not known to this Identity Provider")); @@ -154,8 +162,13 @@ public void testSpInitiatedSso() throws Exception { final String nameIdFormat = TRANSIENT; final String relayString = randomBoolean() ? randomAlphaOfLength(8) : null; final boolean forceAuthn = true; - final AuthnRequest authnRequest = buildAuthnRequest(entityId, new URL(acsUrl), - new URL("https://idp.org/sso/redirect"), nameIdFormat, forceAuthn); + final AuthnRequest authnRequest = buildAuthnRequest( + entityId, + new URL(acsUrl), + new URL("https://idp.org/sso/redirect"), + nameIdFormat, + forceAuthn + ); final String query = getQueryString(authnRequest, relayString, false, null); validateRequest.setJsonEntity("{\"authn_request_query\":\"" + query + "\"}"); Response validateResponse = getRestClient().performRequest(validateRequest); @@ -173,22 +186,27 @@ public void testSpInitiatedSso() throws Exception { final String expectedInResponeTo = authnState.get("authn_request_id"); // User login a.k.a exchange the user credentials for an API Key - final String apiKeyCredentials = getApiKeyFromCredentials(SAMPLE_IDPUSER_NAME, - new SecureString(SAMPLE_IDPUSER_PASSWORD.toCharArray())); + final String apiKeyCredentials = getApiKeyFromCredentials( + SAMPLE_IDPUSER_NAME, + new SecureString(SAMPLE_IDPUSER_PASSWORD.toCharArray()) + ); // Make a request to init an SSO flow with the API Key as secondary authentication Request initRequest = new Request("POST", "/_idp/saml/init"); - initRequest.setOptions(RequestOptions.DEFAULT.toBuilder() - .addHeader("Authorization", basicAuthHeaderValue(CONSOLE_USER_NAME, - new SecureString(CONSOLE_USER_PASSWORD.toCharArray()))) - .addHeader("es-secondary-authorization", "ApiKey " + apiKeyCredentials) - .build()); + initRequest.setOptions( + RequestOptions.DEFAULT.toBuilder() + .addHeader("Authorization", basicAuthHeaderValue(CONSOLE_USER_NAME, new SecureString(CONSOLE_USER_PASSWORD.toCharArray()))) + .addHeader("es-secondary-authorization", "ApiKey " + apiKeyCredentials) + .build() + ); XContentBuilder authnStateBuilder = jsonBuilder(); authnStateBuilder.map(authnState); - initRequest.setJsonEntity("{" - + ("\"entity_id\":\"" + entityId + "\",") - + ("\"acs\":\"" + serviceProvider.get("acs") + "\",") - + ("\"authn_state\":" + Strings.toString(authnStateBuilder)) - + "}"); + initRequest.setJsonEntity( + "{" + + ("\"entity_id\":\"" + entityId + "\",") + + ("\"acs\":\"" + serviceProvider.get("acs") + "\",") + + ("\"authn_state\":" + Strings.toString(authnStateBuilder)) + + "}" + ); Response initResponse = getRestClient().performRequest(initRequest); ObjectPath initResponseObject = ObjectPath.createFromResponse(initResponse); assertThat(initResponseObject.evaluate("post_url").toString(), equalTo(acsUrl)); @@ -217,8 +235,13 @@ public void testSpInitiatedSsoFailsForUserWithNoAccess() throws Exception { final String nameIdFormat = TRANSIENT; final String relayString = randomBoolean() ? randomAlphaOfLength(8) : null; final boolean forceAuthn = true; - final AuthnRequest authnRequest = buildAuthnRequest(entityId, new URL(acsUrl), - new URL("https://idp.org/sso/redirect"), nameIdFormat, forceAuthn); + final AuthnRequest authnRequest = buildAuthnRequest( + entityId, + new URL(acsUrl), + new URL("https://idp.org/sso/redirect"), + nameIdFormat, + forceAuthn + ); final String query = getQueryString(authnRequest, relayString, false, null); validateRequest.setJsonEntity("{\"authn_request_query\":\"" + query + "\"}"); Response validateResponse = getRestClient().performRequest(validateRequest); @@ -236,19 +259,27 @@ public void testSpInitiatedSsoFailsForUserWithNoAccess() throws Exception { final String expectedInResponeTo = authnState.get("authn_request_id"); // User login a.k.a exchange the user credentials for an API Key - user can authenticate but shouldn't have access this SP - final String apiKeyCredentials = getApiKeyFromCredentials(SAMPLE_USER_NAME, - new SecureString(SAMPLE_USER_PASSWORD.toCharArray())); + final String apiKeyCredentials = getApiKeyFromCredentials(SAMPLE_USER_NAME, new SecureString(SAMPLE_USER_PASSWORD.toCharArray())); // Make a request to init an SSO flow with the API Key as secondary authentication Request initRequest = new Request("POST", "/_idp/saml/init"); - initRequest.setOptions(RequestOptions.DEFAULT.toBuilder() - .addHeader("Authorization", basicAuthHeaderValue(CONSOLE_USER_NAME, - new SecureString(CONSOLE_USER_PASSWORD.toCharArray()))) - .addHeader("es-secondary-authorization", "ApiKey " + apiKeyCredentials) - .build()); + initRequest.setOptions( + RequestOptions.DEFAULT.toBuilder() + .addHeader("Authorization", basicAuthHeaderValue(CONSOLE_USER_NAME, new SecureString(CONSOLE_USER_PASSWORD.toCharArray()))) + .addHeader("es-secondary-authorization", "ApiKey " + apiKeyCredentials) + .build() + ); XContentBuilder authnStateBuilder = jsonBuilder(); authnStateBuilder.map(authnState); - initRequest.setJsonEntity("{ \"entity_id\":\"" + entityId + "\", \"acs\":\"" + acsUrl + "\"," + - "\"authn_state\":" + Strings.toString(authnStateBuilder) + "}"); + initRequest.setJsonEntity( + "{ \"entity_id\":\"" + + entityId + + "\", \"acs\":\"" + + acsUrl + + "\"," + + "\"authn_state\":" + + Strings.toString(authnStateBuilder) + + "}" + ); Response initResponse = getRestClient().performRequest(initRequest); ObjectPath initResponseObject = ObjectPath.createFromResponse(initResponse); assertThat(initResponseObject.evaluate("post_url").toString(), equalTo(acsUrl)); @@ -258,8 +289,10 @@ public void testSpInitiatedSsoFailsForUserWithNoAccess() throws Exception { Map sp = initResponseObject.evaluate("service_provider"); assertThat(sp, hasKey("entity_id")); assertThat(sp.get("entity_id"), equalTo(entityId)); - assertThat(initResponseObject.evaluate("error"), - equalTo("User [" + SAMPLE_USER_NAME + "] is not permitted to access service [" + entityId + "]")); + assertThat( + initResponseObject.evaluate("error"), + equalTo("User [" + SAMPLE_USER_NAME + "] is not permitted to access service [" + entityId + "]") + ); } public void testSpInitiatedSsoFailsForUnknownSp() throws Exception { @@ -274,8 +307,13 @@ public void testSpInitiatedSsoFailsForUnknownSp() throws Exception { final String nameIdFormat = TRANSIENT; final String relayString = null; final boolean forceAuthn = randomBoolean(); - final AuthnRequest authnRequest = buildAuthnRequest(entityId + randomAlphaOfLength(4), new URL(acsUrl), - new URL("https://idp.org/sso/redirect"), nameIdFormat, forceAuthn); + final AuthnRequest authnRequest = buildAuthnRequest( + entityId + randomAlphaOfLength(4), + new URL(acsUrl), + new URL("https://idp.org/sso/redirect"), + nameIdFormat, + forceAuthn + ); final String query = getQueryString(authnRequest, relayString, false, null); validateRequest.setJsonEntity("{\"authn_request_query\":\"" + query + "\"}"); ResponseException e = expectThrows(ResponseException.class, () -> getRestClient().performRequest(validateRequest)); @@ -296,8 +334,13 @@ public void testSpInitiatedSsoFailsForMalformedRequest() throws Exception { final String nameIdFormat = TRANSIENT; final String relayString = null; final boolean forceAuthn = randomBoolean(); - final AuthnRequest authnRequest = buildAuthnRequest(entityId + randomAlphaOfLength(4), new URL(acsUrl), - new URL("https://idp.org/sso/redirect"), nameIdFormat, forceAuthn); + final AuthnRequest authnRequest = buildAuthnRequest( + entityId + randomAlphaOfLength(4), + new URL(acsUrl), + new URL("https://idp.org/sso/redirect"), + nameIdFormat, + forceAuthn + ); final String query = getQueryString(authnRequest, relayString, false, null); // Skip http parameter name @@ -323,16 +366,20 @@ private void registerServiceProvider(String entityId, String acsUrl) throws Exce spFields.put(SamlServiceProviderDocument.Fields.ENTITY_ID.getPreferredName(), entityId); spFields.put(SamlServiceProviderDocument.Fields.NAME_ID.getPreferredName(), TRANSIENT); spFields.put(SamlServiceProviderDocument.Fields.NAME.getPreferredName(), "Dummy SP"); - spFields.put("attributes", Map.of( - "principal", "https://saml.elasticsearch.org/attributes/principal", - "roles", "https://saml.elasticsearch.org/attributes/roles" - )); - spFields.put("privileges", Map.of( - "resource", entityId, - "roles", Set.of("sso:(\\w+)") - )); - Request request = - new Request("PUT", "/_idp/saml/sp/" + urlEncode(entityId) + "?refresh=" + WriteRequest.RefreshPolicy.IMMEDIATE.getValue()); + spFields.put( + "attributes", + Map.of( + "principal", + "https://saml.elasticsearch.org/attributes/principal", + "roles", + "https://saml.elasticsearch.org/attributes/roles" + ) + ); + spFields.put("privileges", Map.of("resource", entityId, "roles", Set.of("sso:(\\w+)"))); + Request request = new Request( + "PUT", + "/_idp/saml/sp/" + urlEncode(entityId) + "?refresh=" + WriteRequest.RefreshPolicy.IMMEDIATE.getValue() + ); request.setOptions(REQUEST_OPTIONS_AS_CONSOLE_USER); final XContentBuilder builder = XContentFactory.jsonBuilder(); builder.map(spFields); @@ -378,15 +425,14 @@ private void registerApplicationPrivileges(Map> privileges) } private String getApiKeyFromCredentials(String username, SecureString password) { - Client client = client().filterWithHeader(Collections.singletonMap("Authorization", - UsernamePasswordToken.basicAuthHeaderValue(username, password))); - final CreateApiKeyResponse response = new CreateApiKeyRequestBuilder(client) - .setName("test key") + Client client = client().filterWithHeader( + Collections.singletonMap("Authorization", UsernamePasswordToken.basicAuthHeaderValue(username, password)) + ); + final CreateApiKeyResponse response = new CreateApiKeyRequestBuilder(client).setName("test key") .setExpiration(TimeValue.timeValueHours(TimeUnit.DAYS.toHours(7L))) .get(); assertNotNull(response); - return Base64.getEncoder().encodeToString( - (response.getId() + ":" + response.getKey().toString()).getBytes(StandardCharsets.UTF_8)); + return Base64.getEncoder().encodeToString((response.getId() + ":" + response.getKey().toString()).getBytes(StandardCharsets.UTF_8)); } private AuthnRequest buildAuthnRequest(String entityId, URL acs, URL destination, String nameIdFormat, boolean forceAuthn) { @@ -434,8 +480,10 @@ private static String urlEncode(String param) throws UnsupportedEncodingExceptio private String deflateAndBase64Encode(SAMLObject message) throws Exception { Deflater deflater = new Deflater(Deflater.DEFLATED, true); - try (ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); - DeflaterOutputStream deflaterStream = new DeflaterOutputStream(bytesOut, deflater)) { + try ( + ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); + DeflaterOutputStream deflaterStream = new DeflaterOutputStream(bytesOut, deflater) + ) { String messageStr = samlFactory.toString(XMLObjectSupport.marshall(message), false); deflaterStream.write(messageStr.getBytes(StandardCharsets.UTF_8)); deflaterStream.finish(); @@ -452,9 +500,20 @@ private byte[] sign(byte[] content, String algo, X509Credential credential) thro } private void assertContainsAttributeWithValue(String message, String attribute, String value) { - assertThat(message, containsString("" + value + "")); + assertThat( + message, + containsString( + "" + + value + + "" + ) + ); } } diff --git a/x-pack/plugin/identity-provider/src/internalClusterTest/java/org/elasticsearch/xpack/idp/saml/sp/SamlServiceProviderIndexTests.java b/x-pack/plugin/identity-provider/src/internalClusterTest/java/org/elasticsearch/xpack/idp/saml/sp/SamlServiceProviderIndexTests.java index df1267aaf80f9..08931c557ef83 100644 --- a/x-pack/plugin/identity-provider/src/internalClusterTest/java/org/elasticsearch/xpack/idp/saml/sp/SamlServiceProviderIndexTests.java +++ b/x-pack/plugin/identity-provider/src/internalClusterTest/java/org/elasticsearch/xpack/idp/saml/sp/SamlServiceProviderIndexTests.java @@ -191,10 +191,14 @@ private boolean installTemplate() { private Set getAllDocs() { final PlainActionFuture> future = new PlainActionFuture<>(); - serviceProviderIndex.findAll(assertListenerIsOnlyCalledOnce(ActionListener.wrap( - set -> future.onResponse(set.stream().map(doc -> doc.document.get()).collect(Collectors.toUnmodifiableSet())), - future::onFailure - ))); + serviceProviderIndex.findAll( + assertListenerIsOnlyCalledOnce( + ActionListener.wrap( + set -> future.onResponse(set.stream().map(doc -> doc.document.get()).collect(Collectors.toUnmodifiableSet())), + future::onFailure + ) + ) + ); return future.actionGet(); } @@ -207,16 +211,26 @@ private SamlServiceProviderDocument readDocument(String docId) { private void writeDocument(SamlServiceProviderDocument doc) { final PlainActionFuture future = new PlainActionFuture<>(); - serviceProviderIndex.writeDocument(doc, DocWriteRequest.OpType.INDEX, WriteRequest.RefreshPolicy.WAIT_UNTIL, - assertListenerIsOnlyCalledOnce(future)); + serviceProviderIndex.writeDocument( + doc, + DocWriteRequest.OpType.INDEX, + WriteRequest.RefreshPolicy.WAIT_UNTIL, + assertListenerIsOnlyCalledOnce(future) + ); doc.setDocId(future.actionGet().getId()); } private DeleteResponse deleteDocument(SamlServiceProviderDocument doc) { final PlainActionFuture future = new PlainActionFuture<>(); - serviceProviderIndex.readDocument(doc.docId, assertListenerIsOnlyCalledOnce(ActionListener.wrap( - info -> serviceProviderIndex.deleteDocument(info.version, WriteRequest.RefreshPolicy.IMMEDIATE, future), - future::onFailure))); + serviceProviderIndex.readDocument( + doc.docId, + assertListenerIsOnlyCalledOnce( + ActionListener.wrap( + info -> serviceProviderIndex.deleteDocument(info.version, WriteRequest.RefreshPolicy.IMMEDIATE, future), + future::onFailure + ) + ) + ); return future.actionGet(); } @@ -228,10 +242,15 @@ private SamlServiceProviderDocument findByEntityId(String entityId) { private Set findAllByEntityId(String entityId) { final PlainActionFuture> future = new PlainActionFuture<>(); - serviceProviderIndex.findByEntityId(entityId, assertListenerIsOnlyCalledOnce(ActionListener.wrap( - set -> future.onResponse(set.stream().map(doc -> doc.document.get()).collect(Collectors.toUnmodifiableSet())), - future::onFailure - ))); + serviceProviderIndex.findByEntityId( + entityId, + assertListenerIsOnlyCalledOnce( + ActionListener.wrap( + set -> future.onResponse(set.stream().map(doc -> doc.document.get()).collect(Collectors.toUnmodifiableSet())), + future::onFailure + ) + ) + ); return future.actionGet(); } diff --git a/x-pack/plugin/identity-provider/src/internalClusterTest/java/org/elasticsearch/xpack/idp/saml/test/IdentityProviderIntegTestCase.java b/x-pack/plugin/identity-provider/src/internalClusterTest/java/org/elasticsearch/xpack/idp/saml/test/IdentityProviderIntegTestCase.java index cf462ffda7380..c339f0baac416 100644 --- a/x-pack/plugin/identity-provider/src/internalClusterTest/java/org/elasticsearch/xpack/idp/saml/test/IdentityProviderIntegTestCase.java +++ b/x-pack/plugin/identity-provider/src/internalClusterTest/java/org/elasticsearch/xpack/idp/saml/test/IdentityProviderIntegTestCase.java @@ -9,6 +9,7 @@ import io.netty.util.ThreadDeathWatcher; import io.netty.util.concurrent.GlobalEventExecutor; + import org.elasticsearch.ElasticsearchException; import org.elasticsearch.analysis.common.CommonAnalysisPlugin; import org.elasticsearch.client.Client; @@ -72,25 +73,27 @@ public abstract class IdentityProviderIntegTestCase extends ESIntegTestCase { // Local Security Cluster user public static final String SAMPLE_USER_NAME = "es_user"; public static final String SAMPLE_USER_PASSWORD = "es_user_password"; - public static final String SAMPLE_USER_PASSWORD_HASHED = - new String(Hasher.resolve("bcrypt9").hash(new SecureString(SAMPLE_USER_PASSWORD.toCharArray()))); + public static final String SAMPLE_USER_PASSWORD_HASHED = new String( + Hasher.resolve("bcrypt9").hash(new SecureString(SAMPLE_USER_PASSWORD.toCharArray())) + ); public static final String SAMPLE_USER_ROLE = "es_user_role"; // User that is authenticated to the Security Cluster in order to perform SSO to cloud resources public static final String SAMPLE_IDPUSER_NAME = "idp_user"; public static final String SAMPLE_IDPUSER_PASSWORD = "idp_user_password"; - public static final String SAMPLE_IDPUSER_PASSWORD_HASHED = - new String(Hasher.resolve("bcrypt9").hash(new SecureString(SAMPLE_IDPUSER_PASSWORD.toCharArray()))); + public static final String SAMPLE_IDPUSER_PASSWORD_HASHED = new String( + Hasher.resolve("bcrypt9").hash(new SecureString(SAMPLE_IDPUSER_PASSWORD.toCharArray())) + ); public static final String SAMPLE_IDPUSER_ROLE = "idp_user_role"; // Cloud console user that calls all IDP related APIs public static final String CONSOLE_USER_NAME = "console_user"; public static final String CONSOLE_USER_PASSWORD = "console_user_password"; - public static final String CONSOLE_USER_PASSWORD_HASHED = - new String(Hasher.resolve("bcrypt9").hash(new SecureString(CONSOLE_USER_PASSWORD.toCharArray()))); + public static final String CONSOLE_USER_PASSWORD_HASHED = new String( + Hasher.resolve("bcrypt9").hash(new SecureString(CONSOLE_USER_PASSWORD.toCharArray())) + ); public static final String CONSOLE_USER_ROLE = "console_user_role"; public static final String SP_ENTITY_ID = "ec:abcdef:123456"; public static final RequestOptions REQUEST_OPTIONS_AS_CONSOLE_USER = RequestOptions.DEFAULT.toBuilder() - .addHeader("Authorization", basicAuthHeaderValue(CONSOLE_USER_NAME, - new SecureString(CONSOLE_USER_PASSWORD.toCharArray()))) + .addHeader("Authorization", basicAuthHeaderValue(CONSOLE_USER_NAME, new SecureString(CONSOLE_USER_PASSWORD.toCharArray()))) .build(); private static Path PARENT_DIR; @@ -182,8 +185,10 @@ protected boolean addMockHttpTransport() { @Override protected Function getClientWrapper() { - Map headers = Collections.singletonMap("Authorization", - basicAuthHeaderValue(SAMPLE_USER_NAME, new SecureString(SAMPLE_USER_PASSWORD.toCharArray()))); + Map headers = Collections.singletonMap( + "Authorization", + basicAuthHeaderValue(SAMPLE_USER_NAME, new SecureString(SAMPLE_USER_PASSWORD.toCharArray())) + ); // we need to wrap node clients because we do not specify a user for nodes and all requests will use the system // user. This is ok for internal n2n stuff but the test framework does other things like wiping indices, repositories, etc // that the system user cannot do. so we wrap the node client with a user that can do these things since the client() calls @@ -198,39 +203,65 @@ protected Path nodeConfigPath(int nodeOrdinal) { private String configRoles() { // test role allows for everything - return SAMPLE_USER_ROLE + ":\n" + - " cluster: [ ALL ]\n" + - " indices:\n" + - " - names: '*'\n" + - " allow_restricted_indices: true\n" + - " privileges: [ ALL ]\n" + - "\n" + + return SAMPLE_USER_ROLE + + ":\n" + + " cluster: [ ALL ]\n" + + " indices:\n" + + " - names: '*'\n" + + " allow_restricted_indices: true\n" + + " privileges: [ ALL ]\n" + + "\n" + + // IDP end user doesn't need any privileges on the security cluster - SAMPLE_IDPUSER_ROLE + ":\n" + + SAMPLE_IDPUSER_ROLE + + ":\n" + + // Could switch to grant apikey for user and call this as console_user - " cluster: ['cluster:admin/xpack/security/api_key/create']\n" + - " indices: []\n" + - " applications:\n " + - " - application: elastic-cloud\n" + - " resources: [ '" + SP_ENTITY_ID + "' ]\n" + - " privileges: [ 'sso:superuser' ]\n" + - "\n" + + " cluster: ['cluster:admin/xpack/security/api_key/create']\n" + + " indices: []\n" + + " applications:\n " + + " - application: elastic-cloud\n" + + " resources: [ '" + + SP_ENTITY_ID + + "' ]\n" + + " privileges: [ 'sso:superuser' ]\n" + + "\n" + + // Console user should be able to call all IDP related endpoints and register application privileges - CONSOLE_USER_ROLE + ":\n" + - " cluster: ['cluster:admin/idp/*', 'cluster:admin/xpack/security/privilege/*' ]\n" + - " indices: []\n"; + CONSOLE_USER_ROLE + + ":\n" + + " cluster: ['cluster:admin/idp/*', 'cluster:admin/xpack/security/privilege/*' ]\n" + + " indices: []\n"; } private String configUsers() { - return SAMPLE_USER_NAME + ":" + SAMPLE_USER_PASSWORD_HASHED + "\n" + - SAMPLE_IDPUSER_NAME + ":" + SAMPLE_IDPUSER_PASSWORD_HASHED + "\n" + - CONSOLE_USER_NAME + ":" + CONSOLE_USER_PASSWORD_HASHED + "\n"; + return SAMPLE_USER_NAME + + ":" + + SAMPLE_USER_PASSWORD_HASHED + + "\n" + + SAMPLE_IDPUSER_NAME + + ":" + + SAMPLE_IDPUSER_PASSWORD_HASHED + + "\n" + + CONSOLE_USER_NAME + + ":" + + CONSOLE_USER_PASSWORD_HASHED + + "\n"; } private String configUsersRoles() { - return SAMPLE_USER_ROLE + ":" + SAMPLE_USER_NAME + "\n" + - SAMPLE_IDPUSER_ROLE + ":" + SAMPLE_IDPUSER_NAME + "\n" + - CONSOLE_USER_ROLE + ":" + CONSOLE_USER_NAME + "\n"; + return SAMPLE_USER_ROLE + + ":" + + SAMPLE_USER_NAME + + "\n" + + SAMPLE_IDPUSER_ROLE + + ":" + + SAMPLE_IDPUSER_NAME + + "\n" + + CONSOLE_USER_ROLE + + ":" + + CONSOLE_USER_NAME + + "\n"; } Path nodePath(Path confDir, final int nodeOrdinal) { diff --git a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/IdentityProviderPlugin.java b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/IdentityProviderPlugin.java index 93cc055a3bdc8..475530bd25511 100644 --- a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/IdentityProviderPlugin.java +++ b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/IdentityProviderPlugin.java @@ -21,7 +21,6 @@ import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsFilter; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.env.Environment; import org.elasticsearch.env.NodeEnvironment; import org.elasticsearch.license.XPackLicenseState; @@ -33,6 +32,7 @@ import org.elasticsearch.script.ScriptService; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.watcher.ResourceWatcherService; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.XPackPlugin; import org.elasticsearch.xpack.core.security.SecurityContext; import org.elasticsearch.xpack.core.ssl.X509KeyPairSettings; @@ -83,12 +83,19 @@ public class IdentityProviderPlugin extends Plugin implements ActionPlugin { private Settings settings; @Override - public Collection createComponents(Client client, ClusterService clusterService, ThreadPool threadPool, - ResourceWatcherService resourceWatcherService, ScriptService scriptService, - NamedXContentRegistry xContentRegistry, Environment environment, - NodeEnvironment nodeEnvironment, NamedWriteableRegistry namedWriteableRegistry, - IndexNameExpressionResolver indexNameExpressionResolver, - Supplier repositoriesServiceSupplier) { + public Collection createComponents( + Client client, + ClusterService clusterService, + ThreadPool threadPool, + ResourceWatcherService resourceWatcherService, + ScriptService scriptService, + NamedXContentRegistry xContentRegistry, + Environment environment, + NodeEnvironment nodeEnvironment, + NamedWriteableRegistry namedWriteableRegistry, + IndexNameExpressionResolver indexNameExpressionResolver, + Supplier repositoriesServiceSupplier + ) { settings = environment.settings(); enabled = ENABLED_SETTING.get(settings); if (enabled == false) { @@ -104,10 +111,17 @@ public Collection createComponents(Client client, ClusterService cluster final UserPrivilegeResolver userPrivilegeResolver = new UserPrivilegeResolver(client, securityContext, actionsResolver); final SamlServiceProviderFactory serviceProviderFactory = new SamlServiceProviderFactory(serviceProviderDefaults); - final SamlServiceProviderResolver registeredServiceProviderResolver - = new SamlServiceProviderResolver(settings, index, serviceProviderFactory); - final WildcardServiceProviderResolver wildcardServiceProviderResolver - = WildcardServiceProviderResolver.create(environment, resourceWatcherService, scriptService, serviceProviderFactory); + final SamlServiceProviderResolver registeredServiceProviderResolver = new SamlServiceProviderResolver( + settings, + index, + serviceProviderFactory + ); + final WildcardServiceProviderResolver wildcardServiceProviderResolver = WildcardServiceProviderResolver.create( + environment, + resourceWatcherService, + scriptService, + serviceProviderFactory + ); final SamlIdentityProvider idp = SamlIdentityProvider.builder(registeredServiceProviderResolver, wildcardServiceProviderResolver) .fromSettings(environment) .serviceProviderDefaults(serviceProviderDefaults) @@ -115,12 +129,7 @@ public Collection createComponents(Client client, ClusterService cluster final SamlFactory factory = new SamlFactory(); - return List.of( - index, - idp, - factory, - userPrivilegeResolver - ); + return List.of(index, idp, factory, userPrivilegeResolver); } @Override @@ -138,10 +147,15 @@ public Collection createComponents(Client client, ClusterService cluster } @Override - public List getRestHandlers(Settings settings, RestController restController, ClusterSettings clusterSettings, - IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter, - IndexNameExpressionResolver indexNameExpressionResolver, - Supplier nodesInCluster) { + public List getRestHandlers( + Settings settings, + RestController restController, + ClusterSettings clusterSettings, + IndexScopedSettings indexScopedSettings, + SettingsFilter settingsFilter, + IndexNameExpressionResolver indexNameExpressionResolver, + Supplier nodesInCluster + ) { if (enabled == false) { return List.of(); } diff --git a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/DeleteSamlServiceProviderResponse.java b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/DeleteSamlServiceProviderResponse.java index 2d2fc186ecd2a..98bcbf027e743 100644 --- a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/DeleteSamlServiceProviderResponse.java +++ b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/DeleteSamlServiceProviderResponse.java @@ -9,9 +9,9 @@ import org.elasticsearch.action.ActionResponse; import org.elasticsearch.action.delete.DeleteResponse; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -39,10 +39,12 @@ public DeleteSamlServiceProviderResponse(String docId, long seqNo, long primaryT } public DeleteSamlServiceProviderResponse(DeleteResponse deleteResponse, String entityId) { - this(deleteResponse == null ? null : deleteResponse.getId(), + this( + deleteResponse == null ? null : deleteResponse.getId(), deleteResponse == null ? UNASSIGNED_SEQ_NO : deleteResponse.getSeqNo(), deleteResponse == null ? UNASSIGNED_PRIMARY_TERM : deleteResponse.getPrimaryTerm(), - entityId); + entityId + ); } public DeleteSamlServiceProviderResponse(StreamInput in) throws IOException { diff --git a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/PutSamlServiceProviderRequest.java b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/PutSamlServiceProviderRequest.java index 18352bd5a5759..930bfc512e736 100644 --- a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/PutSamlServiceProviderRequest.java +++ b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/PutSamlServiceProviderRequest.java @@ -31,24 +31,34 @@ public class PutSamlServiceProviderRequest extends ActionRequest { private final SamlServiceProviderDocument document; private final WriteRequest.RefreshPolicy refreshPolicy; - public static PutSamlServiceProviderRequest fromXContent(String entityId, WriteRequest.RefreshPolicy refreshPolicy, - XContentParser parser) throws IOException { + public static PutSamlServiceProviderRequest fromXContent( + String entityId, + WriteRequest.RefreshPolicy refreshPolicy, + XContentParser parser + ) throws IOException { final SamlServiceProviderDocument document = SamlServiceProviderDocument.fromXContent(null, parser); if (document.entityId == null) { document.setEntityId(entityId); } else if (entityId != null) { if (entityId.equals(document.entityId) == false) { throw new ElasticsearchParseException( - "Entity id [{}] inside request body and entity id [{}] from parameter do not match", document.entityId, entityId); + "Entity id [{}] inside request body and entity id [{}] from parameter do not match", + document.entityId, + entityId + ); } } if (document.created != null) { throw new ElasticsearchParseException( - "Field [{}] may not be specified in a request", SamlServiceProviderDocument.Fields.CREATED_DATE); + "Field [{}] may not be specified in a request", + SamlServiceProviderDocument.Fields.CREATED_DATE + ); } if (document.lastModified != null) { throw new ElasticsearchParseException( - "Field [{}] may not be specified in a request", SamlServiceProviderDocument.Fields.LAST_MODIFIED); + "Field [{}] may not be specified in a request", + SamlServiceProviderDocument.Fields.LAST_MODIFIED + ); } document.setCreatedMillis(System.currentTimeMillis()); document.setLastModifiedMillis(System.currentTimeMillis()); @@ -93,7 +103,9 @@ public ActionRequestValidationException validate() { final URL url = new URL(document.acs); if (url.getProtocol().equals("https") == false) { validationException = addValidationError( - "[" + SamlServiceProviderDocument.Fields.ACS + "] must use the [https] protocol", validationException); + "[" + SamlServiceProviderDocument.Fields.ACS + "] must use the [https] protocol", + validationException + ); } } catch (MalformedURLException e) { String error = "[" + SamlServiceProviderDocument.Fields.ACS + "] must be a valid URL"; @@ -107,13 +119,15 @@ public ActionRequestValidationException validate() { if (document.certificates.identityProviderSigning.isEmpty() == false) { validationException = addValidationError( "[" + SamlServiceProviderDocument.Fields.Certificates.IDP_SIGNING + "] certificates may not be specified", - validationException); + validationException + ); } if (document.certificates.identityProviderMetadataSigning.isEmpty() == false) { validationException = addValidationError( "[" + SamlServiceProviderDocument.Fields.Certificates.IDP_METADATA + "] certificates may not be specified", - validationException); + validationException + ); } return validationException; diff --git a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/SamlInitiateSingleSignOnRequest.java b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/SamlInitiateSingleSignOnRequest.java index 090b72aed17ec..facbe4739dcce 100644 --- a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/SamlInitiateSingleSignOnRequest.java +++ b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/SamlInitiateSingleSignOnRequest.java @@ -30,8 +30,7 @@ public SamlInitiateSingleSignOnRequest(StreamInput in) throws IOException { samlAuthenticationState = in.readOptionalWriteable(SamlAuthenticationState::new); } - public SamlInitiateSingleSignOnRequest() { - } + public SamlInitiateSingleSignOnRequest() {} @Override public ActionRequestValidationException validate() { diff --git a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/SamlInitiateSingleSignOnResponse.java b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/SamlInitiateSingleSignOnResponse.java index 729dccc6ad877..d920b29de7bcd 100644 --- a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/SamlInitiateSingleSignOnResponse.java +++ b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/SamlInitiateSingleSignOnResponse.java @@ -7,9 +7,9 @@ package org.elasticsearch.xpack.idp.action; import org.elasticsearch.action.ActionResponse; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; import java.io.IOException; @@ -30,8 +30,13 @@ public SamlInitiateSingleSignOnResponse(StreamInput in) throws IOException { this.error = in.readOptionalString(); } - public SamlInitiateSingleSignOnResponse(String entityId, String postUrl, String samlResponse, String samlStatus, - @Nullable String error) { + public SamlInitiateSingleSignOnResponse( + String entityId, + String postUrl, + String samlResponse, + String samlStatus, + @Nullable String error + ) { this.entityId = entityId; this.postUrl = postUrl; this.samlResponse = samlResponse; diff --git a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/SamlMetadataRequest.java b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/SamlMetadataRequest.java index 1aab315065ae8..cceafca37a60d 100644 --- a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/SamlMetadataRequest.java +++ b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/SamlMetadataRequest.java @@ -8,9 +8,9 @@ import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionRequestValidationException; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; import java.io.IOException; import java.util.Objects; diff --git a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/SamlValidateAuthnRequestRequest.java b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/SamlValidateAuthnRequestRequest.java index a25774e703277..97eb840c63058 100644 --- a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/SamlValidateAuthnRequestRequest.java +++ b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/SamlValidateAuthnRequestRequest.java @@ -25,8 +25,7 @@ public SamlValidateAuthnRequestRequest(StreamInput in) throws IOException { queryString = in.readString(); } - public SamlValidateAuthnRequestRequest() { - } + public SamlValidateAuthnRequestRequest() {} @Override public ActionRequestValidationException validate() { diff --git a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/SamlValidateAuthnRequestResponse.java b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/SamlValidateAuthnRequestResponse.java index f765ef3d2135a..fef45284f92eb 100644 --- a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/SamlValidateAuthnRequestResponse.java +++ b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/SamlValidateAuthnRequestResponse.java @@ -62,9 +62,18 @@ public void writeTo(StreamOutput out) throws IOException { @Override public String toString() { - return getClass().getSimpleName() + "{ spEntityId='" + getSpEntityId() + "',\n" + - " acs='" + getAssertionConsumerService() + "',\n" + - " forceAuthn='" + isForceAuthn() + "',\n" + - " authnState='" + getAuthnState() + "' }"; + return getClass().getSimpleName() + + "{ spEntityId='" + + getSpEntityId() + + "',\n" + + " acs='" + + getAssertionConsumerService() + + "',\n" + + " forceAuthn='" + + isForceAuthn() + + "',\n" + + " authnState='" + + getAuthnState() + + "' }"; } } diff --git a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/TransportDeleteSamlServiceProviderAction.java b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/TransportDeleteSamlServiceProviderAction.java index e2152d42ce063..b5a935f7af06c 100644 --- a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/TransportDeleteSamlServiceProviderAction.java +++ b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/TransportDeleteSamlServiceProviderAction.java @@ -23,22 +23,29 @@ /** * Transport action to remove a service provider from the IdP */ -public class TransportDeleteSamlServiceProviderAction - extends HandledTransportAction { +public class TransportDeleteSamlServiceProviderAction extends HandledTransportAction< + DeleteSamlServiceProviderRequest, + DeleteSamlServiceProviderResponse> { private final Logger logger = LogManager.getLogger(); private final SamlServiceProviderIndex index; @Inject - public TransportDeleteSamlServiceProviderAction(TransportService transportService, ActionFilters actionFilters, - SamlServiceProviderIndex index) { + public TransportDeleteSamlServiceProviderAction( + TransportService transportService, + ActionFilters actionFilters, + SamlServiceProviderIndex index + ) { super(DeleteSamlServiceProviderAction.NAME, transportService, actionFilters, DeleteSamlServiceProviderRequest::new); this.index = index; } @Override - protected void doExecute(Task task, final DeleteSamlServiceProviderRequest request, - final ActionListener listener) { + protected void doExecute( + Task task, + final DeleteSamlServiceProviderRequest request, + final ActionListener listener + ) { final String entityId = request.getEntityId(); index.findByEntityId(entityId, ActionListener.wrap(matchingDocuments -> { if (matchingDocuments.isEmpty()) { @@ -49,13 +56,21 @@ protected void doExecute(Task task, final DeleteSamlServiceProviderRequest reque assert existingDoc.docId != null : "Loaded document with no doc id"; assert existingDoc.entityId.equals(entityId) : "Loaded document with non-matching entity-id"; logger.info("Deleting Service Provider [{}]", existingDoc); - index.deleteDocument(docInfo.version, request.getRefreshPolicy(), ActionListener.wrap( - deleteResponse -> listener.onResponse(new DeleteSamlServiceProviderResponse(deleteResponse, entityId)), - listener::onFailure - )); + index.deleteDocument( + docInfo.version, + request.getRefreshPolicy(), + ActionListener.wrap( + deleteResponse -> listener.onResponse(new DeleteSamlServiceProviderResponse(deleteResponse, entityId)), + listener::onFailure + ) + ); } else { - logger.warn("Found multiple existing service providers in [{}] with entity id [{}] - [{}]", - index, entityId, matchingDocuments.stream().map(d -> d.getDocument().docId).collect(Collectors.joining(","))); + logger.warn( + "Found multiple existing service providers in [{}] with entity id [{}] - [{}]", + index, + entityId, + matchingDocuments.stream().map(d -> d.getDocument().docId).collect(Collectors.joining(",")) + ); listener.onFailure(new IllegalStateException("Multiple service providers exist with entity id [" + entityId + "]")); } }, listener::onFailure)); diff --git a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/TransportPutSamlServiceProviderAction.java b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/TransportPutSamlServiceProviderAction.java index 170d597020396..c4b00a90fd08a 100644 --- a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/TransportPutSamlServiceProviderAction.java +++ b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/TransportPutSamlServiceProviderAction.java @@ -30,8 +30,9 @@ import java.util.Base64; import java.util.stream.Collectors; -public class TransportPutSamlServiceProviderAction - extends HandledTransportAction { +public class TransportPutSamlServiceProviderAction extends HandledTransportAction< + PutSamlServiceProviderRequest, + PutSamlServiceProviderResponse> { private final Logger logger = LogManager.getLogger(); private final SamlServiceProviderIndex index; @@ -39,13 +40,22 @@ public class TransportPutSamlServiceProviderAction private final Clock clock; @Inject - public TransportPutSamlServiceProviderAction(TransportService transportService, ActionFilters actionFilters, - SamlServiceProviderIndex index, SamlIdentityProvider identityProvider) { + public TransportPutSamlServiceProviderAction( + TransportService transportService, + ActionFilters actionFilters, + SamlServiceProviderIndex index, + SamlIdentityProvider identityProvider + ) { this(transportService, actionFilters, index, identityProvider, Clock.systemUTC()); } - TransportPutSamlServiceProviderAction(TransportService transportService, ActionFilters actionFilters, - SamlServiceProviderIndex index, SamlIdentityProvider identityProvider, Clock clock) { + TransportPutSamlServiceProviderAction( + TransportService transportService, + ActionFilters actionFilters, + SamlServiceProviderIndex index, + SamlIdentityProvider identityProvider, + Clock clock + ) { super(PutSamlServiceProviderAction.NAME, transportService, actionFilters, PutSamlServiceProviderRequest::new); this.index = index; this.identityProvider = identityProvider; @@ -53,8 +63,11 @@ public TransportPutSamlServiceProviderAction(TransportService transportService, } @Override - protected void doExecute(Task task, final PutSamlServiceProviderRequest request, - final ActionListener listener) { + protected void doExecute( + Task task, + final PutSamlServiceProviderRequest request, + final ActionListener listener + ) { final SamlServiceProviderDocument document = request.getDocument(); if (document.docId != null) { listener.onFailure(new IllegalArgumentException("request document must not have an id [" + document.docId + "]")); @@ -82,16 +95,25 @@ protected void doExecute(Task task, final PutSamlServiceProviderRequest request, logger.trace("Found existing ServiceProvider for EntityID=[{}], writing to doc [{}]", document.entityId, document.docId); writeDocument(document, DocWriteRequest.OpType.INDEX, request.getRefreshPolicy(), listener); } else { - logger.warn("Found multiple existing service providers in [{}] with entity id [{}] - [{}]", - index, document.entityId, matchingDocuments.stream().map(d -> d.getDocument().docId).collect(Collectors.joining(","))); - listener.onFailure(new IllegalStateException( - "Multiple service providers already exist with entity id [" + document.entityId + "]")); + logger.warn( + "Found multiple existing service providers in [{}] with entity id [{}] - [{}]", + index, + document.entityId, + matchingDocuments.stream().map(d -> d.getDocument().docId).collect(Collectors.joining(",")) + ); + listener.onFailure( + new IllegalStateException("Multiple service providers already exist with entity id [" + document.entityId + "]") + ); } }, listener::onFailure)); } - private void writeDocument(SamlServiceProviderDocument document, DocWriteRequest.OpType opType, - WriteRequest.RefreshPolicy refreshPolicy, ActionListener listener) { + private void writeDocument( + SamlServiceProviderDocument document, + DocWriteRequest.OpType opType, + WriteRequest.RefreshPolicy refreshPolicy, + ActionListener listener + ) { final Instant now = clock.instant(); if (document.created == null || opType == DocWriteRequest.OpType.CREATE) { @@ -104,16 +126,24 @@ private void writeDocument(SamlServiceProviderDocument document, DocWriteRequest return; } logger.debug("[{}] service provider [{}] in document [{}] of [{}]", opType, document.entityId, document.docId, index); - index.writeDocument(document, opType, refreshPolicy, ActionListener.wrap( - response -> listener.onResponse(new PutSamlServiceProviderResponse( - response.getId(), - response.getResult() == DocWriteResponse.Result.CREATED, - response.getSeqNo(), - response.getPrimaryTerm(), - document.entityId, - document.enabled)), - listener::onFailure - )); + index.writeDocument( + document, + opType, + refreshPolicy, + ActionListener.wrap( + response -> listener.onResponse( + new PutSamlServiceProviderResponse( + response.getId(), + response.getResult() == DocWriteResponse.Result.CREATED, + response.getSeqNo(), + response.getPrimaryTerm(), + document.entityId, + document.enabled + ) + ), + listener::onFailure + ) + ); } private String deriveDocumentId(SamlServiceProviderDocument document) { diff --git a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/TransportSamlInitiateSingleSignOnAction.java b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/TransportSamlInitiateSingleSignOnAction.java index d179b89526446..32dd8c8c2ee6d 100644 --- a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/TransportSamlInitiateSingleSignOnAction.java +++ b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/TransportSamlInitiateSingleSignOnAction.java @@ -33,8 +33,9 @@ import java.time.Clock; -public class TransportSamlInitiateSingleSignOnAction - extends HandledTransportAction { +public class TransportSamlInitiateSingleSignOnAction extends HandledTransportAction< + SamlInitiateSingleSignOnRequest, + SamlInitiateSingleSignOnResponse> { private final Logger logger = LogManager.getLogger(TransportSamlInitiateSingleSignOnAction.class); @@ -44,9 +45,14 @@ public class TransportSamlInitiateSingleSignOnAction private final UserPrivilegeResolver privilegeResolver; @Inject - public TransportSamlInitiateSingleSignOnAction(TransportService transportService, ActionFilters actionFilters, - SecurityContext securityContext, SamlIdentityProvider idp, SamlFactory factory, - UserPrivilegeResolver privilegeResolver) { + public TransportSamlInitiateSingleSignOnAction( + TransportService transportService, + ActionFilters actionFilters, + SecurityContext securityContext, + SamlIdentityProvider idp, + SamlFactory factory, + UserPrivilegeResolver privilegeResolver + ) { super(SamlInitiateSingleSignOnAction.NAME, transportService, actionFilters, SamlInitiateSingleSignOnRequest::new); this.securityContext = securityContext; this.identityProvider = idp; @@ -55,102 +61,151 @@ public TransportSamlInitiateSingleSignOnAction(TransportService transportService } @Override - protected void doExecute(Task task, SamlInitiateSingleSignOnRequest request, - ActionListener listener) { + protected void doExecute( + Task task, + SamlInitiateSingleSignOnRequest request, + ActionListener listener + ) { final SamlAuthenticationState authenticationState = request.getSamlAuthenticationState(); identityProvider.resolveServiceProvider( request.getSpEntityId(), request.getAssertionConsumerService(), false, - ActionListener.wrap( - sp -> { - if (null == sp) { - final String message = "Service Provider with Entity ID [" + request.getSpEntityId() + "] and ACS [" - + request.getAssertionConsumerService() + "] is not known to this Identity Provider"; - possiblyReplyWithSamlFailure(authenticationState, request.getSpEntityId(), request.getAssertionConsumerService(), - StatusCode.RESPONDER, new IllegalArgumentException(message), listener); - return; - } - final SecondaryAuthentication secondaryAuthentication = SecondaryAuthentication.readFromContext(securityContext); - if (secondaryAuthentication == null) { - possiblyReplyWithSamlFailure(authenticationState, request.getSpEntityId(), request.getAssertionConsumerService(), + ActionListener.wrap(sp -> { + if (null == sp) { + final String message = "Service Provider with Entity ID [" + + request.getSpEntityId() + + "] and ACS [" + + request.getAssertionConsumerService() + + "] is not known to this Identity Provider"; + possiblyReplyWithSamlFailure( + authenticationState, + request.getSpEntityId(), + request.getAssertionConsumerService(), + StatusCode.RESPONDER, + new IllegalArgumentException(message), + listener + ); + return; + } + final SecondaryAuthentication secondaryAuthentication = SecondaryAuthentication.readFromContext(securityContext); + if (secondaryAuthentication == null) { + possiblyReplyWithSamlFailure( + authenticationState, + request.getSpEntityId(), + request.getAssertionConsumerService(), + StatusCode.REQUESTER, + new ElasticsearchSecurityException("Request is missing secondary authentication", RestStatus.FORBIDDEN), + listener + ); + return; + } + buildUserFromAuthentication(secondaryAuthentication, sp, ActionListener.wrap(user -> { + if (user == null) { + possiblyReplyWithSamlFailure( + authenticationState, + request.getSpEntityId(), + request.getAssertionConsumerService(), StatusCode.REQUESTER, - new ElasticsearchSecurityException("Request is missing secondary authentication", RestStatus.FORBIDDEN), - listener); + new ElasticsearchSecurityException( + "User [{}] is not permitted to access service [{}]", + RestStatus.FORBIDDEN, + secondaryAuthentication.getUser().principal(), + sp.getEntityId() + ), + listener + ); return; } - buildUserFromAuthentication(secondaryAuthentication, sp, ActionListener.wrap( - user -> { - if (user == null) { - possiblyReplyWithSamlFailure(authenticationState, request.getSpEntityId(), - request.getAssertionConsumerService(), StatusCode.REQUESTER, - new ElasticsearchSecurityException("User [{}] is not permitted to access service [{}]", - RestStatus.FORBIDDEN, secondaryAuthentication.getUser().principal(), sp.getEntityId()), - listener); - return; - } - final SuccessfulAuthenticationResponseMessageBuilder builder = - new SuccessfulAuthenticationResponseMessageBuilder(samlFactory, Clock.systemUTC(), identityProvider); - try { - final Response response = builder.build(user, authenticationState); - listener.onResponse(new SamlInitiateSingleSignOnResponse( - user.getServiceProvider().getEntityId(), - user.getServiceProvider().getAssertionConsumerService().toString(), - samlFactory.getXmlContent(response), - StatusCode.SUCCESS, - null)); - } catch (ElasticsearchException e) { - listener.onFailure(e); - } - }, - e -> possiblyReplyWithSamlFailure(authenticationState, request.getSpEntityId(), - request.getAssertionConsumerService(), StatusCode.RESPONDER, e, listener) - )); + final SuccessfulAuthenticationResponseMessageBuilder builder = new SuccessfulAuthenticationResponseMessageBuilder( + samlFactory, + Clock.systemUTC(), + identityProvider + ); + try { + final Response response = builder.build(user, authenticationState); + listener.onResponse( + new SamlInitiateSingleSignOnResponse( + user.getServiceProvider().getEntityId(), + user.getServiceProvider().getAssertionConsumerService().toString(), + samlFactory.getXmlContent(response), + StatusCode.SUCCESS, + null + ) + ); + } catch (ElasticsearchException e) { + listener.onFailure(e); + } }, - e -> possiblyReplyWithSamlFailure(authenticationState, request.getSpEntityId(), request.getAssertionConsumerService(), - StatusCode.RESPONDER, e, listener) - )); + e -> possiblyReplyWithSamlFailure( + authenticationState, + request.getSpEntityId(), + request.getAssertionConsumerService(), + StatusCode.RESPONDER, + e, + listener + ) + )); + }, + e -> possiblyReplyWithSamlFailure( + authenticationState, + request.getSpEntityId(), + request.getAssertionConsumerService(), + StatusCode.RESPONDER, + e, + listener + ) + ) + ); } - private void buildUserFromAuthentication(SecondaryAuthentication secondaryAuthentication, SamlServiceProvider serviceProvider, - ActionListener listener) { + private void buildUserFromAuthentication( + SecondaryAuthentication secondaryAuthentication, + SamlServiceProvider serviceProvider, + ActionListener listener + ) { User user = secondaryAuthentication.getUser(); secondaryAuthentication.execute(ignore -> { - ActionListener wrapped = ActionListener.wrap( - userPrivileges -> { - if (userPrivileges.hasAccess == false) { - listener.onResponse(null); - } else { - logger.debug("Resolved [{}] for [{}]", userPrivileges, user); - listener.onResponse(new UserServiceAuthentication(user.principal(), user.fullName(), user.email(), - userPrivileges.roles, serviceProvider)); - } - }, - listener::onFailure - ); - privilegeResolver.resolve(serviceProvider.getPrivileges(), wrapped); - return null; - } - ); + ActionListener wrapped = ActionListener.wrap(userPrivileges -> { + if (userPrivileges.hasAccess == false) { + listener.onResponse(null); + } else { + logger.debug("Resolved [{}] for [{}]", userPrivileges, user); + listener.onResponse( + new UserServiceAuthentication( + user.principal(), + user.fullName(), + user.email(), + userPrivileges.roles, + serviceProvider + ) + ); + } + }, listener::onFailure); + privilegeResolver.resolve(serviceProvider.getPrivileges(), wrapped); + return null; + }); } - private void possiblyReplyWithSamlFailure(SamlAuthenticationState authenticationState, String spEntityId, - String acsUrl, String statusCode, Exception e, - ActionListener listener) { + private void possiblyReplyWithSamlFailure( + SamlAuthenticationState authenticationState, + String spEntityId, + String acsUrl, + String statusCode, + Exception e, + ActionListener listener + ) { logger.debug("Failed to generate a successful SAML response: ", e); if (authenticationState != null) { - final FailedAuthenticationResponseMessageBuilder builder = - new FailedAuthenticationResponseMessageBuilder(samlFactory, Clock.systemUTC(), identityProvider) - .setInResponseTo(authenticationState.getAuthnRequestId()) - .setAcsUrl(acsUrl) - .setPrimaryStatusCode(statusCode); + final FailedAuthenticationResponseMessageBuilder builder = new FailedAuthenticationResponseMessageBuilder( + samlFactory, + Clock.systemUTC(), + identityProvider + ).setInResponseTo(authenticationState.getAuthnRequestId()).setAcsUrl(acsUrl).setPrimaryStatusCode(statusCode); final Response response = builder.build(); - listener.onResponse(new SamlInitiateSingleSignOnResponse( - spEntityId, - acsUrl, - samlFactory.getXmlContent(response), - statusCode, - e.getMessage())); + listener.onResponse( + new SamlInitiateSingleSignOnResponse(spEntityId, acsUrl, samlFactory.getXmlContent(response), statusCode, e.getMessage()) + ); } else { listener.onFailure(e); } diff --git a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/TransportSamlMetadataAction.java b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/TransportSamlMetadataAction.java index a9b9b11b1cd0f..358bce6d03685 100644 --- a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/TransportSamlMetadataAction.java +++ b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/action/TransportSamlMetadataAction.java @@ -22,8 +22,12 @@ public class TransportSamlMetadataAction extends HandledTransportAction { +public class TransportSamlValidateAuthnRequestAction extends HandledTransportAction< + SamlValidateAuthnRequestRequest, + SamlValidateAuthnRequestResponse> { private final SamlIdentityProvider identityProvider; private final SamlFactory samlFactory; @Inject - public TransportSamlValidateAuthnRequestAction(TransportService transportService, ActionFilters actionFilters, - SamlIdentityProvider idp, SamlFactory factory) { + public TransportSamlValidateAuthnRequestAction( + TransportService transportService, + ActionFilters actionFilters, + SamlIdentityProvider idp, + SamlFactory factory + ) { super(SamlValidateAuthnRequestAction.NAME, transportService, actionFilters, SamlValidateAuthnRequestRequest::new); this.identityProvider = idp; this.samlFactory = factory; } @Override - protected void doExecute(Task task, SamlValidateAuthnRequestRequest request, - ActionListener listener) { + protected void doExecute( + Task task, + SamlValidateAuthnRequestRequest request, + ActionListener listener + ) { final SamlAuthnRequestValidator validator = new SamlAuthnRequestValidator(samlFactory, identityProvider); try { validator.processQueryString(request.getQueryString(), listener); diff --git a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/privileges/ApplicationActionsResolver.java b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/privileges/ApplicationActionsResolver.java index 8d45429755f03..8c04cc319112b 100644 --- a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/privileges/ApplicationActionsResolver.java +++ b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/privileges/ApplicationActionsResolver.java @@ -54,10 +54,16 @@ public class ApplicationActionsResolver extends AbstractLifecycleComponent { private static final int CACHE_SIZE_DEFAULT = 100; private static final TimeValue CACHE_TTL_DEFAULT = TimeValue.timeValueMinutes(90); - public static final Setting CACHE_SIZE - = Setting.intSetting("xpack.idp.privileges.cache.size", CACHE_SIZE_DEFAULT, Setting.Property.NodeScope); - public static final Setting CACHE_TTL - = Setting.timeSetting("xpack.idp.privileges.cache.ttl", CACHE_TTL_DEFAULT, Setting.Property.NodeScope); + public static final Setting CACHE_SIZE = Setting.intSetting( + "xpack.idp.privileges.cache.size", + CACHE_SIZE_DEFAULT, + Setting.Property.NodeScope + ); + public static final Setting CACHE_TTL = Setting.timeSetting( + "xpack.idp.privileges.cache.ttl", + CACHE_TTL_DEFAULT, + Setting.Property.NodeScope + ); private final Logger logger = LogManager.getLogger(); @@ -91,11 +97,23 @@ protected void doStart() { } private void loadPrivilegesForDefaultApplication() { - loadActions(defaults.applicationName, ActionListener.wrap( - actions -> logger.info("Found actions [{}] defined within application privileges for [{}]", actions, defaults.applicationName), - ex -> logger.warn(new ParameterizedMessage( - "Failed to load application privileges actions for application [{}]", defaults.applicationName), ex) - )); + loadActions( + defaults.applicationName, + ActionListener.wrap( + actions -> logger.info( + "Found actions [{}] defined within application privileges for [{}]", + actions, + defaults.applicationName + ), + ex -> logger.warn( + new ParameterizedMessage( + "Failed to load application privileges actions for application [{}]", + defaults.applicationName + ), + ex + ) + ) + ); } @Override @@ -120,17 +138,14 @@ public void getActions(String application, ActionListener> listener) private void loadActions(String applicationName, ActionListener> listener) { final GetPrivilegesRequest request = new GetPrivilegesRequest(); request.application(applicationName); - this.client.execute(GetPrivilegesAction.INSTANCE, request, ActionListener.wrap( - response -> { - final Set fixedActions = Stream.of(response.privileges()) - .map(p -> p.getActions()) - .flatMap(Collection::stream) - .filter(s -> s.indexOf('*') == -1) - .collect(Collectors.toUnmodifiableSet()); - cache.put(applicationName, fixedActions); - listener.onResponse(fixedActions); - }, - listener::onFailure - )); + this.client.execute(GetPrivilegesAction.INSTANCE, request, ActionListener.wrap(response -> { + final Set fixedActions = Stream.of(response.privileges()) + .map(p -> p.getActions()) + .flatMap(Collection::stream) + .filter(s -> s.indexOf('*') == -1) + .collect(Collectors.toUnmodifiableSet()); + cache.put(applicationName, fixedActions); + listener.onResponse(fixedActions); + }, listener::onFailure)); } } diff --git a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/privileges/UserPrivilegeResolver.java b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/privileges/UserPrivilegeResolver.java index 2872ebdeea6a5..cadb81d274ce1 100644 --- a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/privileges/UserPrivilegeResolver.java +++ b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/privileges/UserPrivilegeResolver.java @@ -45,8 +45,7 @@ public UserPrivileges(String principal, boolean hasAccess, Set roles) { @Override public String toString() { - StringBuilder str = new StringBuilder() - .append(getClass().getSimpleName()) + StringBuilder str = new StringBuilder().append(getClass().getSimpleName()) .append("{") .append(principal) .append(", ") @@ -90,16 +89,17 @@ public void resolve(ServiceProviderPrivileges service, ActionListener { - logger.debug("Checking access for user [{}] to application [{}] resource [{}]", - username, service.getApplicationName(), service.getResource()); - UserPrivileges privileges = buildResult(response, service); - logger.debug("Resolved service privileges [{}]", privileges); - listener.onResponse(privileges); - }, - listener::onFailure - )); + client.execute(HasPrivilegesAction.INSTANCE, request, ActionListener.wrap(response -> { + logger.debug( + "Checking access for user [{}] to application [{}] resource [{}]", + username, + service.getApplicationName(), + service.getResource() + ); + UserPrivileges privileges = buildResult(response, service); + logger.debug("Resolved service privileges [{}]", privileges); + listener.onResponse(privileges); + }, listener::onFailure)); }, listener::onFailure)); } @@ -124,8 +124,10 @@ private UserPrivileges buildResult(HasPrivilegesResponse response, ServiceProvid return new UserPrivileges(response.getUsername(), hasAccess, roles); } - private void buildResourcePrivilege(ServiceProviderPrivileges service, - ActionListener listener) { + private void buildResourcePrivilege( + ServiceProviderPrivileges service, + ActionListener listener + ) { actionsResolver.getActions(service.getApplicationName(), ActionListener.wrap(actions -> { if (actions == null || actions.isEmpty()) { logger.warn("No application-privilege actions defined for application [{}]", service.getApplicationName()); diff --git a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/authn/SamlAuthnRequestValidator.java b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/authn/SamlAuthnRequestValidator.java index 4c2e6c0752515..e1e07e067fbe8 100644 --- a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/authn/SamlAuthnRequestValidator.java +++ b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/authn/SamlAuthnRequestValidator.java @@ -11,8 +11,8 @@ import org.apache.logging.log4j.message.ParameterizedMessage; import org.elasticsearch.ElasticsearchSecurityException; import org.elasticsearch.action.ActionListener; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.internal.io.Streams; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.RestUtils; @@ -30,7 +30,6 @@ import org.w3c.dom.Element; import org.xml.sax.SAXException; -import javax.xml.parsers.DocumentBuilder; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -50,6 +49,8 @@ import java.util.zip.Inflater; import java.util.zip.InflaterInputStream; +import javax.xml.parsers.DocumentBuilder; + import static org.opensaml.saml.common.xml.SAMLConstants.SAML2_REDIRECT_BINDING_URI; import static org.opensaml.saml.saml2.core.NameIDType.UNSPECIFIED; @@ -61,10 +62,11 @@ public class SamlAuthnRequestValidator { private final SamlFactory samlFactory; private final SamlIdentityProvider idp; private final Logger logger = LogManager.getLogger(SamlAuthnRequestValidator.class); - private static final String[] XSD_FILES = new String[]{"/org/elasticsearch/xpack/idp/saml/support/saml-schema-protocol-2.0.xsd", + private static final String[] XSD_FILES = new String[] { + "/org/elasticsearch/xpack/idp/saml/support/saml-schema-protocol-2.0.xsd", "/org/elasticsearch/xpack/idp/saml/support/saml-schema-assertion-2.0.xsd", "/org/elasticsearch/xpack/idp/saml/support/xenc-schema.xsd", - "/org/elasticsearch/xpack/idp/saml/support/xmldsig-core-schema.xsd"}; + "/org/elasticsearch/xpack/idp/saml/support/xmldsig-core-schema.xsd" }; private static final ThreadLocal THREAD_LOCAL_DOCUMENT_BUILDER = ThreadLocal.withInitial(() -> { try { @@ -100,19 +102,16 @@ public void processQueryString(String queryString, ActionListener { - try { - validateAuthnRequest(authnRequest, sp, parsedQueryString, listener); - } catch (ElasticsearchSecurityException e) { - logger.debug("Could not validate AuthnRequest", e); - listener.onFailure(e); - } catch (Exception e) { - logAndRespond("Could not validate AuthnRequest", e, listener); - } - }, - listener::onFailure - )); + getSpFromAuthnRequest(authnRequest.getIssuer(), authnRequest.getAssertionConsumerServiceURL(), ActionListener.wrap(sp -> { + try { + validateAuthnRequest(authnRequest, sp, parsedQueryString, listener); + } catch (ElasticsearchSecurityException e) { + logger.debug("Could not validate AuthnRequest", e); + listener.onFailure(e); + } catch (Exception e) { + logAndRespond("Could not validate AuthnRequest", e, listener); + } + }, listener::onFailure)); } catch (ElasticsearchSecurityException e) { logger.debug("Could not process AuthnRequest", e); listener.onFailure(e); @@ -131,48 +130,80 @@ private ParsedQueryString parseQueryString(String queryString) throws Elasticsea logger.trace(new ParameterizedMessage("Parsed the following parameters from the query string: {}", parameters)); final String samlRequest = parameters.get("SAMLRequest"); if (null == samlRequest) { - throw new ElasticsearchSecurityException("Query string [{}] does not contain a SAMLRequest parameter", - RestStatus.BAD_REQUEST, queryString); + throw new ElasticsearchSecurityException( + "Query string [{}] does not contain a SAMLRequest parameter", + RestStatus.BAD_REQUEST, + queryString + ); } return new ParsedQueryString( queryString, samlRequest, parameters.get("RelayState"), parameters.get("SigAlg"), - parameters.get("Signature")); + parameters.get("Signature") + ); } - private void validateAuthnRequest(AuthnRequest authnRequest, SamlServiceProvider sp, ParsedQueryString parsedQueryString, - ActionListener listener) { + private void validateAuthnRequest( + AuthnRequest authnRequest, + SamlServiceProvider sp, + ParsedQueryString parsedQueryString, + ActionListener listener + ) { // If the Service Provider should not sign requests, do not try to handle signatures even if they are added to the request if (sp.shouldSignAuthnRequests()) { if (Strings.hasText(parsedQueryString.signature)) { if (Strings.hasText(parsedQueryString.sigAlg) == false) { - logAndRespond(new ParameterizedMessage("Query string [{}] contains a Signature but SigAlg parameter is missing", - parsedQueryString.queryString), listener); + logAndRespond( + new ParameterizedMessage( + "Query string [{}] contains a Signature but SigAlg parameter is missing", + parsedQueryString.queryString + ), + listener + ); return; } final Set spSigningCredentials = sp.getSpSigningCredentials(); if (spSigningCredentials == null || spSigningCredentials.isEmpty()) { - logAndRespond(new ParameterizedMessage("Unable to validate signature of authentication request, " + - "Service Provider [{}] hasn't registered signing credentials", sp.getEntityId()), listener); + logAndRespond( + new ParameterizedMessage( + "Unable to validate signature of authentication request, " + + "Service Provider [{}] hasn't registered signing credentials", + sp.getEntityId() + ), + listener + ); return; } if (validateSignature(parsedQueryString, spSigningCredentials) == false) { logAndRespond( - new ParameterizedMessage("Unable to validate signature of authentication request [{}] using credentials [{}]", - parsedQueryString.queryString, samlFactory.describeCredentials(spSigningCredentials)), listener); + new ParameterizedMessage( + "Unable to validate signature of authentication request [{}] using credentials [{}]", + parsedQueryString.queryString, + samlFactory.describeCredentials(spSigningCredentials) + ), + listener + ); return; } } else if (Strings.hasText(parsedQueryString.sigAlg)) { - logAndRespond(new ParameterizedMessage("Query string [{}] contains a SigAlg parameter but Signature is missing", - parsedQueryString.queryString), listener); + logAndRespond( + new ParameterizedMessage( + "Query string [{}] contains a SigAlg parameter but Signature is missing", + parsedQueryString.queryString + ), + listener + ); return; } else { logAndRespond( new ParameterizedMessage( - "The Service Provider [{}] must sign authentication requests but no signature was found", sp.getEntityId()), - listener); + "The Service Provider [{}] must sign authentication requests but no signature was found", + sp.getEntityId() + ), + listener + ); return; } } @@ -181,10 +212,19 @@ private void validateAuthnRequest(AuthnRequest authnRequest, SamlServiceProvider final String acs = checkAcs(authnRequest, sp, authnState); validateNameIdPolicy(authnRequest, sp, authnState); authnState.put(SamlAuthenticationState.Fields.AUTHN_REQUEST_ID.getPreferredName(), authnRequest.getID()); - final SamlValidateAuthnRequestResponse response = new SamlValidateAuthnRequestResponse(sp.getEntityId(), acs, - authnRequest.isForceAuthn(), authnState); - logger.trace(new ParameterizedMessage("Validated AuthnResponse from queryString [{}] and extracted [{}]", - parsedQueryString.queryString, response)); + final SamlValidateAuthnRequestResponse response = new SamlValidateAuthnRequestResponse( + sp.getEntityId(), + acs, + authnRequest.isForceAuthn(), + authnState + ); + logger.trace( + new ParameterizedMessage( + "Validated AuthnResponse from queryString [{}] and extracted [{}]", + parsedQueryString.queryString, + response + ) + ); listener.onResponse(response); } @@ -194,10 +234,15 @@ private void validateNameIdPolicy(AuthnRequest request, SamlServiceProvider sp, final String requestedFormat = nameIDPolicy.getFormat(); final String allowedFormat = sp.getAllowedNameIdFormat(); if (Strings.hasText(requestedFormat)) { - if (allowedFormat != null && requestedFormat.equals(UNSPECIFIED) == false + if (allowedFormat != null + && requestedFormat.equals(UNSPECIFIED) == false && requestedFormat.equals(allowedFormat) == false) { - throw new ElasticsearchSecurityException("The requested NameID format [{}] doesn't match the allowed NameID format" + - " for this Service Provider which is [{}]", requestedFormat, sp.getAllowedNameIdFormat()); + throw new ElasticsearchSecurityException( + "The requested NameID format [{}] doesn't match the allowed NameID format" + + " for this Service Provider which is [{}]", + requestedFormat, + sp.getAllowedNameIdFormat() + ); } else { authnState.put(SamlAuthenticationState.Fields.NAMEID_FORMAT.getPreferredName(), requestedFormat); } @@ -216,11 +261,20 @@ private boolean validateSignature(ParsedQueryString queryString, Collection { - if (null == serviceProvider) { - throw new ElasticsearchSecurityException( - "Service Provider with Entity ID [{}] and ACS [{}] is not known to this Identity Provider", RestStatus.BAD_REQUEST, - issuerString, acs); - } - listener.onResponse(serviceProvider); - }, - listener::onFailure - )); + idp.resolveServiceProvider(issuerString, acs, false, ActionListener.wrap(serviceProvider -> { + if (null == serviceProvider) { + throw new ElasticsearchSecurityException( + "Service Provider with Entity ID [{}] and ACS [{}] is not known to this Identity Provider", + RestStatus.BAD_REQUEST, + issuerString, + acs + ); + } + listener.onResponse(serviceProvider); + }, listener::onFailure)); } private void checkDestination(AuthnRequest request) { @@ -249,22 +303,30 @@ private void checkDestination(AuthnRequest request) { if (url.equals(request.getDestination()) == false) { throw new ElasticsearchSecurityException( "SAML authentication request [{}] is for destination [{}] but the SSO endpoint of this Identity Provider is [{}]", - RestStatus.BAD_REQUEST, request.getID(), request.getDestination(), url); + RestStatus.BAD_REQUEST, + request.getID(), + request.getDestination(), + url + ); } } private String checkAcs(AuthnRequest request, SamlServiceProvider sp, Map authnState) { final String acs = request.getAssertionConsumerServiceURL(); if (Strings.hasText(acs) == false) { - final String message = request.getAssertionConsumerServiceIndex() == null ? - "SAML authentication does not contain an AssertionConsumerService URL" : - "SAML authentication does not contain an AssertionConsumerService URL. It contains an Assertion Consumer Service Index " + - "but this IDP doesn't support multiple AssertionConsumerService URLs."; + final String message = request.getAssertionConsumerServiceIndex() == null + ? "SAML authentication does not contain an AssertionConsumerService URL" + : "SAML authentication does not contain an AssertionConsumerService URL. It contains an Assertion Consumer Service Index " + + "but this IDP doesn't support multiple AssertionConsumerService URLs."; throw new ElasticsearchSecurityException(message, RestStatus.BAD_REQUEST); } if (acs.equals(sp.getAssertionConsumerService().toString()) == false) { - throw new ElasticsearchSecurityException("The registered ACS URL for this Service Provider is [{}] but the authentication " + - "request contained [{}]", RestStatus.BAD_REQUEST, sp.getAssertionConsumerService(), acs); + throw new ElasticsearchSecurityException( + "The registered ACS URL for this Service Provider is [{}] but the authentication " + "request contained [{}]", + RestStatus.BAD_REQUEST, + sp.getAssertionConsumerService(), + acs + ); } return acs; } @@ -295,9 +357,11 @@ private byte[] decodeBase64(String content) { private byte[] inflate(byte[] bytes) { Inflater inflater = new Inflater(true); - try (ByteArrayInputStream in = new ByteArrayInputStream(bytes); - InflaterInputStream inflate = new InflaterInputStream(in, inflater); - ByteArrayOutputStream out = new ByteArrayOutputStream(bytes.length * 3 / 2)) { + try ( + ByteArrayInputStream in = new ByteArrayInputStream(bytes); + InflaterInputStream inflate = new InflaterInputStream(in, inflater); + ByteArrayOutputStream out = new ByteArrayOutputStream(bytes.length * 3 / 2) + ) { Streams.copy(inflate, out); return out.toByteArray(); } catch (IOException e) { @@ -343,9 +407,9 @@ private ParsedQueryString(String queryString, String samlRequest, String relaySt public String reconstructQueryParameters() throws ElasticsearchSecurityException { try { - return relayState == null ? - "SAMLRequest=" + urlEncode(samlRequest) + "&SigAlg=" + urlEncode(sigAlg) : - "SAMLRequest=" + urlEncode(samlRequest) + "&RelayState=" + urlEncode(relayState) + "&SigAlg=" + urlEncode(sigAlg); + return relayState == null + ? "SAMLRequest=" + urlEncode(samlRequest) + "&SigAlg=" + urlEncode(sigAlg) + : "SAMLRequest=" + urlEncode(samlRequest) + "&RelayState=" + urlEncode(relayState) + "&SigAlg=" + urlEncode(sigAlg); } catch (UnsupportedEncodingException e) { throw new ElasticsearchSecurityException("Cannot reconstruct query for signature verification", e); } diff --git a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/authn/SuccessfulAuthenticationResponseMessageBuilder.java b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/authn/SuccessfulAuthenticationResponseMessageBuilder.java index 4ff2bc6d7cd34..26814702a1d02 100644 --- a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/authn/SuccessfulAuthenticationResponseMessageBuilder.java +++ b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/authn/SuccessfulAuthenticationResponseMessageBuilder.java @@ -123,8 +123,10 @@ private Subject buildSubject(Instant now, UserServiceAuthentication user, SamlAu final Subject subject = samlFactory.object(Subject.class, Subject.DEFAULT_ELEMENT_NAME); subject.setNameID(nameID); - final SubjectConfirmationData data = samlFactory.object(SubjectConfirmationData.class, - SubjectConfirmationData.DEFAULT_ELEMENT_NAME); + final SubjectConfirmationData data = samlFactory.object( + SubjectConfirmationData.class, + SubjectConfirmationData.DEFAULT_ELEMENT_NAME + ); if (authnState != null && authnState.getAuthnRequestId() != null) { data.setInResponseTo(authnState.getAuthnRequestId()); } @@ -250,8 +252,9 @@ private NameID buildNameId(UserServiceAuthentication user, @Nullable SamlAuthent if (authnState != null && authnState.getRequestedNameidFormat() != null) { nameIdFormat = authnState.getRequestedNameidFormat(); } else { - nameIdFormat = serviceProvider.getAllowedNameIdFormat() != null ? serviceProvider.getAllowedNameIdFormat() : - idp.getServiceProviderDefaults().nameIdFormat; + nameIdFormat = serviceProvider.getAllowedNameIdFormat() != null + ? serviceProvider.getAllowedNameIdFormat() + : idp.getServiceProviderDefaults().nameIdFormat; } nameID.setFormat(nameIdFormat); nameID.setValue(getNameIdValueForFormat(nameIdFormat, user)); diff --git a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/authn/UserServiceAuthentication.java b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/authn/UserServiceAuthentication.java index 2cffcf7070110..b8e6194e670d7 100644 --- a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/authn/UserServiceAuthentication.java +++ b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/authn/UserServiceAuthentication.java @@ -27,9 +27,15 @@ public class UserServiceAuthentication { private final Set authenticationMethods; private final Set networkControls; - public UserServiceAuthentication(String principal, String name, String email, Set roles, - SamlServiceProvider serviceProvider, - Set authenticationMethods, Set networkControls) { + public UserServiceAuthentication( + String principal, + String name, + String email, + Set roles, + SamlServiceProvider serviceProvider, + Set authenticationMethods, + Set networkControls + ) { this.principal = principal; this.name = name; this.email = email; @@ -73,14 +79,25 @@ public Set getNetworkControls() { @Override public String toString() { - return getClass().getSimpleName() + "{" + - "principal='" + principal + '\'' + - ", name='" + name + '\'' + - ", email='" + email + '\'' + - ", roles=" + roles + - ", serviceProvider=" + serviceProvider + - ", authenticationMethods=" + authenticationMethods + - ", networkControls=" + networkControls + - '}'; + return getClass().getSimpleName() + + "{" + + "principal='" + + principal + + '\'' + + ", name='" + + name + + '\'' + + ", email='" + + email + + '\'' + + ", roles=" + + roles + + ", serviceProvider=" + + serviceProvider + + ", authenticationMethods=" + + authenticationMethods + + ", networkControls=" + + networkControls + + '}'; } } diff --git a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/idp/SamlIdPMetadataBuilder.java b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/idp/SamlIdPMetadataBuilder.java index c5af7be626b02..c580087daf183 100644 --- a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/idp/SamlIdPMetadataBuilder.java +++ b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/idp/SamlIdPMetadataBuilder.java @@ -65,7 +65,6 @@ public class SamlIdPMetadataBuilder { private SamlIdentityProvider.OrganizationInfo organization; private final List contacts; - public SamlIdPMetadataBuilder(String entityId) { this.entityId = entityId; this.locale = Locale.getDefault(); @@ -93,7 +92,7 @@ public SamlIdPMetadataBuilder wantAuthnRequestsSigned(boolean wants) { } public SamlIdPMetadataBuilder withSingleSignOnServiceUrl(String binding, URL url) { - if ( null != url) { + if (null != url) { this.singleSignOnServiceUrls.put(binding, url); } return this; @@ -114,7 +113,7 @@ public SamlIdPMetadataBuilder withSigningCertificates(List sign } public SamlIdPMetadataBuilder withSigningCertificate(X509Certificate signingCertificate) { - if ( null != signingCertificate ) { + if (null != signingCertificate) { return withSigningCertificates(Collections.singletonList(signingCertificate)); } return this; diff --git a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/idp/SamlIdentityProvider.java b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/idp/SamlIdentityProvider.java index 809ff1b2e1e00..14d2b1456b183 100644 --- a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/idp/SamlIdentityProvider.java +++ b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/idp/SamlIdentityProvider.java @@ -7,13 +7,12 @@ package org.elasticsearch.xpack.idp.saml.idp; - import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.action.ActionListener; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.collect.MapBuilder; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xpack.idp.saml.sp.SamlServiceProvider; import org.elasticsearch.xpack.idp.saml.sp.SamlServiceProviderResolver; import org.elasticsearch.xpack.idp.saml.sp.ServiceProviderDefaults; @@ -49,10 +48,19 @@ public class SamlIdentityProvider { private OrganizationInfo organization; // Package access - use Builder instead - SamlIdentityProvider(String entityId, Map ssoEndpoints, Map sloEndpoints, Set allowedNameIdFormats, - X509Credential signingCredential, X509Credential metadataSigningCredential, - ContactInfo technicalContact, OrganizationInfo organization, ServiceProviderDefaults serviceProviderDefaults, - SamlServiceProviderResolver serviceProviderResolver, WildcardServiceProviderResolver wildcardServiceResolver) { + SamlIdentityProvider( + String entityId, + Map ssoEndpoints, + Map sloEndpoints, + Set allowedNameIdFormats, + X509Credential signingCredential, + X509Credential metadataSigningCredential, + ContactInfo technicalContact, + OrganizationInfo organization, + ServiceProviderDefaults serviceProviderDefaults, + SamlServiceProviderResolver serviceProviderResolver, + WildcardServiceProviderResolver wildcardServiceResolver + ) { this.entityId = entityId; this.ssoEndpoints = ssoEndpoints; this.sloEndpoints = sloEndpoints; @@ -66,8 +74,10 @@ public class SamlIdentityProvider { this.wildcardServiceResolver = wildcardServiceResolver; } - public static SamlIdentityProviderBuilder builder(SamlServiceProviderResolver serviceResolver, - WildcardServiceProviderResolver wildcardResolver) { + public static SamlIdentityProviderBuilder builder( + SamlServiceProviderResolver serviceResolver, + WildcardServiceProviderResolver wildcardResolver + ) { return new SamlIdentityProviderBuilder(serviceResolver, wildcardResolver); } @@ -115,25 +125,26 @@ public ServiceProviderDefaults getServiceProviderDefaults() { * @param allowDisabled whether to return service providers that are not {@link SamlServiceProvider#isEnabled() enabled}. * For security reasons, callers should typically avoid working with disabled service providers. * @param listener Responds with the requested Service Provider object, or {@code null} if no such SP exists. - * {@link ActionListener#onFailure} is only used for fatal errors (e.g. being unable to access + * {@link ActionListener#onFailure} is only used for fatal errors (e.g. being unable to access */ - public void resolveServiceProvider(String spEntityId, @Nullable String acs, boolean allowDisabled, - ActionListener listener) { - serviceProviderResolver.resolve(spEntityId, ActionListener.wrap( - sp -> { - if (sp == null) { - logger.debug("No explicitly registered service provider exists for entityId [{}]", spEntityId); - resolveWildcardService(spEntityId, acs, listener); - } else if (allowDisabled == false && sp.isEnabled() == false) { - logger.info("Service provider [{}][{}] is not enabled", spEntityId, sp.getName()); - listener.onResponse(null); - } else { - logger.debug("Service provider for [{}] is [{}]", spEntityId, sp); - listener.onResponse(sp); - } - }, - listener::onFailure - )); + public void resolveServiceProvider( + String spEntityId, + @Nullable String acs, + boolean allowDisabled, + ActionListener listener + ) { + serviceProviderResolver.resolve(spEntityId, ActionListener.wrap(sp -> { + if (sp == null) { + logger.debug("No explicitly registered service provider exists for entityId [{}]", spEntityId); + resolveWildcardService(spEntityId, acs, listener); + } else if (allowDisabled == false && sp.isEnabled() == false) { + logger.info("Service provider [{}][{}] is not enabled", spEntityId, sp.getName()); + listener.onResponse(null); + } else { + logger.debug("Service provider for [{}] is [{}]", spEntityId, sp); + listener.onResponse(sp); + } + }, listener::onFailure)); } private void resolveWildcardService(String entityId, String acs, ActionListener listener) { @@ -156,14 +167,14 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; final SamlIdentityProvider that = (SamlIdentityProvider) o; - return Objects.equals(entityId, that.entityId) && - Objects.equals(ssoEndpoints, that.ssoEndpoints) && - Objects.equals(sloEndpoints, that.sloEndpoints) && - Objects.equals(allowedNameIdFormats, that.allowedNameIdFormats) && - Objects.equals(signingCredential, that.signingCredential) && - Objects.equals(metadataSigningCredential, that.metadataSigningCredential) && - Objects.equals(technicalContact, that.technicalContact) && - Objects.equals(organization, that.organization); + return Objects.equals(entityId, that.entityId) + && Objects.equals(ssoEndpoints, that.ssoEndpoints) + && Objects.equals(sloEndpoints, that.sloEndpoints) + && Objects.equals(allowedNameIdFormats, that.allowedNameIdFormats) + && Objects.equals(signingCredential, that.signingCredential) + && Objects.equals(metadataSigningCredential, that.metadataSigningCredential) + && Objects.equals(technicalContact, that.technicalContact) + && Objects.equals(organization, that.organization); } @Override @@ -179,7 +190,8 @@ public static class ContactInfo { .put(ContactPersonTypeEnumeration.SUPPORT.toString(), ContactPersonTypeEnumeration.SUPPORT) .put(ContactPersonTypeEnumeration.TECHNICAL.toString(), ContactPersonTypeEnumeration.TECHNICAL) .put(ContactPersonTypeEnumeration.OTHER.toString(), ContactPersonTypeEnumeration.OTHER) - .map()); + .map() + ); public final ContactPersonTypeEnumeration type; public final String givenName; @@ -196,8 +208,9 @@ public ContactInfo(ContactPersonTypeEnumeration type, String givenName, String s public static ContactPersonTypeEnumeration getType(String name) { final ContactPersonTypeEnumeration type = TYPES.get(name.toLowerCase(Locale.ROOT)); if (type == null) { - throw new IllegalArgumentException("Invalid contact type " + name + " allowed values are " - + Strings.collectionToCommaDelimitedString(TYPES.keySet())); + throw new IllegalArgumentException( + "Invalid contact type " + name + " allowed values are " + Strings.collectionToCommaDelimitedString(TYPES.keySet()) + ); } return type; } @@ -219,9 +232,9 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; OrganizationInfo that = (OrganizationInfo) o; - return Objects.equals(organizationName, that.organizationName) && - Objects.equals(displayName, that.displayName) && - Objects.equals(url, that.url); + return Objects.equals(organizationName, that.organizationName) + && Objects.equals(displayName, that.displayName) + && Objects.equals(url, that.url); } @Override diff --git a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/idp/SamlIdentityProviderBuilder.java b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/idp/SamlIdentityProviderBuilder.java index 977c2ef14d10f..c03bdaaf8893a 100644 --- a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/idp/SamlIdentityProviderBuilder.java +++ b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/idp/SamlIdentityProviderBuilder.java @@ -22,7 +22,6 @@ import org.opensaml.security.x509.X509Credential; import org.opensaml.security.x509.impl.X509KeyManagerX509CredentialAdapter; -import javax.net.ssl.X509KeyManager; import java.net.MalformedURLException; import java.net.URL; import java.security.PrivateKey; @@ -38,6 +37,8 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import javax.net.ssl.X509KeyManager; + import static org.opensaml.saml.common.xml.SAMLConstants.SAML2_POST_BINDING_URI; import static org.opensaml.saml.common.xml.SAMLConstants.SAML2_REDIRECT_BINDING_URI; import static org.opensaml.saml.saml2.core.NameIDType.TRANSIENT; @@ -50,33 +51,68 @@ public class SamlIdentityProviderBuilder { private static final List ALLOWED_NAMEID_FORMATS = List.of(TRANSIENT); public static final Setting IDP_ENTITY_ID = Setting.simpleString("xpack.idp.entity_id", Setting.Property.NodeScope); - public static final Setting IDP_SSO_REDIRECT_ENDPOINT = new Setting<>("xpack.idp.sso_endpoint.redirect", "https:", - value -> parseUrl("xpack.idp.sso_endpoint.redirect", value), Setting.Property.NodeScope); - public static final Setting IDP_SSO_POST_ENDPOINT = new Setting<>("xpack.idp.sso_endpoint.post", "https:", - value -> parseUrl("xpack.idp.sso_endpoint.post", value), Setting.Property.NodeScope); - public static final Setting IDP_SLO_REDIRECT_ENDPOINT = new Setting<>("xpack.idp.slo_endpoint.redirect", "https:", - value -> parseUrl("xpack.idp.slo_endpoint.redirect", value), Setting.Property.NodeScope); - public static final Setting IDP_SLO_POST_ENDPOINT = new Setting<>("xpack.idp.slo_endpoint.post", "https:", - value -> parseUrl("xpack.idp.slo_endpoint.post", value), Setting.Property.NodeScope); - public static final Setting> IDP_ALLOWED_NAMEID_FORMATS = Setting.listSetting("xpack.idp.allowed_nameid_formats", - List.of(TRANSIENT), Function.identity(), SamlIdentityProviderBuilder::validateNameIDs, Setting.Property.NodeScope); - - public static final Setting IDP_SIGNING_KEY_ALIAS = Setting.simpleString("xpack.idp.signing.keystore.alias", - Setting.Property.NodeScope); - public static final Setting IDP_METADATA_SIGNING_KEY_ALIAS = Setting.simpleString("xpack.idp.metadata.signing.keystore.alias", - Setting.Property.NodeScope); - - public static final Setting IDP_ORGANIZATION_NAME = Setting.simpleString("xpack.idp.organization.name", - Setting.Property.NodeScope); - public static final Setting IDP_ORGANIZATION_DISPLAY_NAME = Setting.simpleString("xpack.idp.organization.display_name", - IDP_ORGANIZATION_NAME, Setting.Property.NodeScope); - public static final Setting IDP_ORGANIZATION_URL = new Setting<>("xpack.idp.organization.url", "http:", - value -> parseUrl("xpack.idp.organization.url", value), Setting.Property.NodeScope); - - public static final Setting IDP_CONTACT_GIVEN_NAME = Setting.simpleString("xpack.idp.contact.given_name", - Setting.Property.NodeScope); - public static final Setting IDP_CONTACT_SURNAME = Setting.simpleString("xpack.idp.contact.surname", - Setting.Property.NodeScope); + public static final Setting IDP_SSO_REDIRECT_ENDPOINT = new Setting<>( + "xpack.idp.sso_endpoint.redirect", + "https:", + value -> parseUrl("xpack.idp.sso_endpoint.redirect", value), + Setting.Property.NodeScope + ); + public static final Setting IDP_SSO_POST_ENDPOINT = new Setting<>( + "xpack.idp.sso_endpoint.post", + "https:", + value -> parseUrl("xpack.idp.sso_endpoint.post", value), + Setting.Property.NodeScope + ); + public static final Setting IDP_SLO_REDIRECT_ENDPOINT = new Setting<>( + "xpack.idp.slo_endpoint.redirect", + "https:", + value -> parseUrl("xpack.idp.slo_endpoint.redirect", value), + Setting.Property.NodeScope + ); + public static final Setting IDP_SLO_POST_ENDPOINT = new Setting<>( + "xpack.idp.slo_endpoint.post", + "https:", + value -> parseUrl("xpack.idp.slo_endpoint.post", value), + Setting.Property.NodeScope + ); + public static final Setting> IDP_ALLOWED_NAMEID_FORMATS = Setting.listSetting( + "xpack.idp.allowed_nameid_formats", + List.of(TRANSIENT), + Function.identity(), + SamlIdentityProviderBuilder::validateNameIDs, + Setting.Property.NodeScope + ); + + public static final Setting IDP_SIGNING_KEY_ALIAS = Setting.simpleString( + "xpack.idp.signing.keystore.alias", + Setting.Property.NodeScope + ); + public static final Setting IDP_METADATA_SIGNING_KEY_ALIAS = Setting.simpleString( + "xpack.idp.metadata.signing.keystore.alias", + Setting.Property.NodeScope + ); + + public static final Setting IDP_ORGANIZATION_NAME = Setting.simpleString( + "xpack.idp.organization.name", + Setting.Property.NodeScope + ); + public static final Setting IDP_ORGANIZATION_DISPLAY_NAME = Setting.simpleString( + "xpack.idp.organization.display_name", + IDP_ORGANIZATION_NAME, + Setting.Property.NodeScope + ); + public static final Setting IDP_ORGANIZATION_URL = new Setting<>( + "xpack.idp.organization.url", + "http:", + value -> parseUrl("xpack.idp.organization.url", value), + Setting.Property.NodeScope + ); + + public static final Setting IDP_CONTACT_GIVEN_NAME = Setting.simpleString( + "xpack.idp.contact.given_name", + Setting.Property.NodeScope + ); + public static final Setting IDP_CONTACT_SURNAME = Setting.simpleString("xpack.idp.contact.surname", Setting.Property.NodeScope); public static final Setting IDP_CONTACT_EMAIL = Setting.simpleString("xpack.idp.contact.email", Setting.Property.NodeScope); private final SamlServiceProviderResolver serviceProviderResolver; @@ -141,11 +177,14 @@ public SamlIdentityProvider build() throws ValidationException { Map.copyOf(ssoEndpoints), sloEndpoints == null ? Map.of() : Map.copyOf(sloEndpoints), Set.copyOf(allowedNameIdFormats), - signingCredential, metadataSigningCredential, - technicalContact, organization, + signingCredential, + metadataSigningCredential, + technicalContact, + organization, serviceProviderDefaults, serviceProviderResolver, - wildcardServiceResolver); + wildcardServiceResolver + ); } public SamlIdentityProviderBuilder fromSettings(Environment env) { @@ -186,7 +225,8 @@ public static List> getSettings() { IDP_ORGANIZATION_URL, IDP_CONTACT_GIVEN_NAME, IDP_CONTACT_SURNAME, - IDP_CONTACT_EMAIL); + IDP_CONTACT_EMAIL + ); } public SamlIdentityProviderBuilder serviceProviderDefaults(ServiceProviderDefaults serviceProviderDefaults) { @@ -253,11 +293,14 @@ private static URL parseUrl(String key, String value) { } private static void validateNameIDs(List values) { - final Set invalidFormats = - values.stream().distinct().filter(e -> ALLOWED_NAMEID_FORMATS.contains(e) == false).collect(Collectors.toSet()); + final Set invalidFormats = values.stream() + .distinct() + .filter(e -> ALLOWED_NAMEID_FORMATS.contains(e) == false) + .collect(Collectors.toSet()); if (invalidFormats.size() > 0) { throw new IllegalArgumentException( - invalidFormats + " are not valid NameID formats. Allowed values are " + ALLOWED_NAMEID_FORMATS); + invalidFormats + " are not valid NameID formats. Allowed values are " + ALLOWED_NAMEID_FORMATS + ); } } @@ -310,11 +353,13 @@ static List buildCredentials(Environment env, Settings settings, } if (selectedAliases.isEmpty()) { throw new IllegalArgumentException( - "The configured keystore for [" + prefix + "keystore] does not contain any RSA or EC key pairs."); + "The configured keystore for [" + prefix + "keystore] does not contain any RSA or EC key pairs." + ); } if (selectedAliases.size() > 1 && allowMultiple == false) { throw new IllegalArgumentException( - "The configured keystore for [" + prefix + "keystore] contains multiple private key entries, when one was expected."); + "The configured keystore for [" + prefix + "keystore] contains multiple private key entries, when one was expected." + ); } } else { selectedAliases.add(configAlias); @@ -323,8 +368,14 @@ static List buildCredentials(Environment env, Settings settings, try { validateSigningKey(keyManager.getPrivateKey(alias)); } catch (ElasticsearchSecurityException e) { - throw new IllegalArgumentException("The configured credential [" + prefix + "keystore] with alias [" + alias - + "] is not a valid signing key - " + e.getMessage()); + throw new IllegalArgumentException( + "The configured credential [" + + prefix + + "keystore] with alias [" + + alias + + "] is not a valid signing key - " + + e.getMessage() + ); } credentials.add(new X509KeyManagerX509CredentialAdapter(keyManager, alias)); } @@ -337,15 +388,17 @@ private static void validateSigningKey(PrivateKey privateKey) { } final String keyType = privateKey.getAlgorithm(); if (keyType.equals("RSA") == false && keyType.equals("EC") == false) { - throw new ElasticsearchSecurityException("The private key uses unsupported key algorithm type [" + keyType - + "], only RSA and EC are supported"); + throw new ElasticsearchSecurityException( + "The private key uses unsupported key algorithm type [" + keyType + "], only RSA and EC are supported" + ); } } private static SamlIdentityProvider.OrganizationInfo buildOrganization(Settings settings) { final String name = settings.hasValue(IDP_ORGANIZATION_NAME.getKey()) ? IDP_ORGANIZATION_NAME.get(settings) : null; - final String displayName = settings.hasValue(IDP_ORGANIZATION_DISPLAY_NAME.getKey()) ? - IDP_ORGANIZATION_DISPLAY_NAME.get(settings) : null; + final String displayName = settings.hasValue(IDP_ORGANIZATION_DISPLAY_NAME.getKey()) + ? IDP_ORGANIZATION_DISPLAY_NAME.get(settings) + : null; final String url = settings.hasValue(IDP_ORGANIZATION_URL.getKey()) ? IDP_ORGANIZATION_URL.get(settings).toString() : null; if (Stream.of(name, displayName, url).allMatch(Objects::isNull) == false) { return new SamlIdentityProvider.OrganizationInfo(name, displayName, url); @@ -355,8 +408,12 @@ private static SamlIdentityProvider.OrganizationInfo buildOrganization(Settings private static SamlIdentityProvider.ContactInfo buildContactInfo(Settings settings) { if (settings.hasValue(IDP_CONTACT_EMAIL.getKey())) { - return new SamlIdentityProvider.ContactInfo(ContactPersonTypeEnumeration.TECHNICAL, - IDP_CONTACT_GIVEN_NAME.get(settings), IDP_CONTACT_SURNAME.get(settings), IDP_CONTACT_EMAIL.get(settings)); + return new SamlIdentityProvider.ContactInfo( + ContactPersonTypeEnumeration.TECHNICAL, + IDP_CONTACT_GIVEN_NAME.get(settings), + IDP_CONTACT_SURNAME.get(settings), + IDP_CONTACT_EMAIL.get(settings) + ); } return null; } diff --git a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/idp/SamlMetadataGenerator.java b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/idp/SamlMetadataGenerator.java index 1cd476e73d761..fa30825651a61 100644 --- a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/idp/SamlMetadataGenerator.java +++ b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/idp/SamlMetadataGenerator.java @@ -43,38 +43,35 @@ public SamlMetadataGenerator(SamlFactory samlFactory, SamlIdentityProvider idp) } public void generateMetadata(String spEntityId, String acs, ActionListener listener) { - idp.resolveServiceProvider(spEntityId, acs, true, ActionListener.wrap( - sp -> { - try { - if (null == sp) { - listener.onFailure(new IllegalArgumentException("Service provider with Entity ID [" + spEntityId - + "] is not registered with this Identity Provider")); - return; - } - EntityDescriptor metadata = buildEntityDescriptor(sp); - final X509Credential signingCredential = idp.getMetadataSigningCredential(); - Element metadataElement = possiblySignDescriptor(metadata, signingCredential); - listener.onResponse(new SamlMetadataResponse(samlFactory.toString(metadataElement, false))); - } catch (Exception e) { - logger.debug("Error generating IDP metadata to share with [" + spEntityId + "]", e); - listener.onFailure(e); + idp.resolveServiceProvider(spEntityId, acs, true, ActionListener.wrap(sp -> { + try { + if (null == sp) { + listener.onFailure( + new IllegalArgumentException( + "Service provider with Entity ID [" + spEntityId + "] is not registered with this Identity Provider" + ) + ); + return; } - }, - listener::onFailure - )); + EntityDescriptor metadata = buildEntityDescriptor(sp); + final X509Credential signingCredential = idp.getMetadataSigningCredential(); + Element metadataElement = possiblySignDescriptor(metadata, signingCredential); + listener.onResponse(new SamlMetadataResponse(samlFactory.toString(metadataElement, false))); + } catch (Exception e) { + logger.debug("Error generating IDP metadata to share with [" + spEntityId + "]", e); + listener.onFailure(e); + } + }, listener::onFailure)); } EntityDescriptor buildEntityDescriptor(SamlServiceProvider sp) throws Exception { - final SamlIdPMetadataBuilder builder = new SamlIdPMetadataBuilder(idp.getEntityId()) - .wantAuthnRequestsSigned(sp.shouldSignAuthnRequests()) - .withSingleSignOnServiceUrl(SAML2_REDIRECT_BINDING_URI, - idp.getSingleSignOnEndpoint(SAML2_REDIRECT_BINDING_URI)) - .withSingleSignOnServiceUrl(SAML2_POST_BINDING_URI, - idp.getSingleSignOnEndpoint(SAML2_POST_BINDING_URI)) - .withSingleLogoutServiceUrl(SAML2_REDIRECT_BINDING_URI, - idp.getSingleLogoutEndpoint(SAML2_REDIRECT_BINDING_URI)) - .withSingleLogoutServiceUrl(SAML2_POST_BINDING_URI, - idp.getSingleLogoutEndpoint(SAML2_POST_BINDING_URI)) + final SamlIdPMetadataBuilder builder = new SamlIdPMetadataBuilder(idp.getEntityId()).wantAuthnRequestsSigned( + sp.shouldSignAuthnRequests() + ) + .withSingleSignOnServiceUrl(SAML2_REDIRECT_BINDING_URI, idp.getSingleSignOnEndpoint(SAML2_REDIRECT_BINDING_URI)) + .withSingleSignOnServiceUrl(SAML2_POST_BINDING_URI, idp.getSingleSignOnEndpoint(SAML2_POST_BINDING_URI)) + .withSingleLogoutServiceUrl(SAML2_REDIRECT_BINDING_URI, idp.getSingleLogoutEndpoint(SAML2_REDIRECT_BINDING_URI)) + .withSingleLogoutServiceUrl(SAML2_POST_BINDING_URI, idp.getSingleLogoutEndpoint(SAML2_POST_BINDING_URI)) .withNameIdFormat(PERSISTENT) .withNameIdFormat(TRANSIENT) .organization(idp.getOrganization()) diff --git a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/rest/action/IdpBaseRestHandler.java b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/rest/action/IdpBaseRestHandler.java index 78577d0a6ecb4..d4d5aca1ef098 100644 --- a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/rest/action/IdpBaseRestHandler.java +++ b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/rest/action/IdpBaseRestHandler.java @@ -31,8 +31,9 @@ protected final RestChannelConsumer prepareRequest(RestRequest request, NodeClie if (isIdpFeatureAllowed()) { return consumer; } else { - return channel -> channel.sendResponse(new BytesRestResponse(channel, - LicenseUtils.newComplianceException("Identity Provider"))); + return channel -> channel.sendResponse( + new BytesRestResponse(channel, LicenseUtils.newComplianceException("Identity Provider")) + ); } } @@ -47,5 +48,3 @@ protected boolean isIdpFeatureAllowed() { */ protected abstract RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClient client) throws IOException; } - - diff --git a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/rest/action/RestDeleteSamlServiceProviderAction.java b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/rest/action/RestDeleteSamlServiceProviderAction.java index 995f13859a6b6..7fccfae8167e9 100644 --- a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/rest/action/RestDeleteSamlServiceProviderAction.java +++ b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/rest/action/RestDeleteSamlServiceProviderAction.java @@ -9,13 +9,13 @@ import org.elasticsearch.action.support.WriteRequest; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.idp.action.DeleteSamlServiceProviderAction; import org.elasticsearch.xpack.idp.action.DeleteSamlServiceProviderRequest; import org.elasticsearch.xpack.idp.action.DeleteSamlServiceProviderResponse; @@ -48,15 +48,15 @@ public List routes() { protected RestChannelConsumer innerPrepareRequest(RestRequest restRequest, NodeClient client) throws IOException { final String entityId = restRequest.param("sp_entity_id"); final WriteRequest.RefreshPolicy refresh = restRequest.hasParam("refresh") - ? WriteRequest.RefreshPolicy.parse(restRequest.param("refresh")) : WriteRequest.RefreshPolicy.NONE; + ? WriteRequest.RefreshPolicy.parse(restRequest.param("refresh")) + : WriteRequest.RefreshPolicy.NONE; final DeleteSamlServiceProviderRequest request = new DeleteSamlServiceProviderRequest(entityId, refresh); - return channel -> client.execute(DeleteSamlServiceProviderAction.INSTANCE, request, - new RestBuilderListener<>(channel) { - @Override - public RestResponse buildResponse(DeleteSamlServiceProviderResponse response, XContentBuilder builder) throws Exception { - response.toXContent(builder, restRequest); - return new BytesRestResponse(response.found() ? RestStatus.OK : RestStatus.NOT_FOUND, builder); - } - }); + return channel -> client.execute(DeleteSamlServiceProviderAction.INSTANCE, request, new RestBuilderListener<>(channel) { + @Override + public RestResponse buildResponse(DeleteSamlServiceProviderResponse response, XContentBuilder builder) throws Exception { + response.toXContent(builder, restRequest); + return new BytesRestResponse(response.found() ? RestStatus.OK : RestStatus.NOT_FOUND, builder); + } + }); } } diff --git a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/rest/action/RestPutSamlServiceProviderAction.java b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/rest/action/RestPutSamlServiceProviderAction.java index 68dfc0725a15a..f1d47f6e75787 100644 --- a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/rest/action/RestPutSamlServiceProviderAction.java +++ b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/rest/action/RestPutSamlServiceProviderAction.java @@ -9,14 +9,14 @@ import org.elasticsearch.action.support.WriteRequest; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.idp.action.PutSamlServiceProviderAction; import org.elasticsearch.xpack.idp.action.PutSamlServiceProviderRequest; import org.elasticsearch.xpack.idp.action.PutSamlServiceProviderResponse; @@ -40,27 +40,24 @@ public String getName() { @Override public List routes() { - return List.of( - new Route(PUT, "/_idp/saml/sp/{sp_entity_id}"), - new Route(POST, "/_idp/saml/sp/{sp_entity_id}") - ); + return List.of(new Route(PUT, "/_idp/saml/sp/{sp_entity_id}"), new Route(POST, "/_idp/saml/sp/{sp_entity_id}")); } @Override protected RestChannelConsumer innerPrepareRequest(RestRequest restRequest, NodeClient client) throws IOException { final String entityId = restRequest.param("sp_entity_id"); final WriteRequest.RefreshPolicy refreshPolicy = restRequest.hasParam("refresh") - ? WriteRequest.RefreshPolicy.parse(restRequest.param("refresh")) : PutSamlServiceProviderRequest.DEFAULT_REFRESH_POLICY; + ? WriteRequest.RefreshPolicy.parse(restRequest.param("refresh")) + : PutSamlServiceProviderRequest.DEFAULT_REFRESH_POLICY; try (XContentParser parser = restRequest.contentParser()) { final PutSamlServiceProviderRequest request = PutSamlServiceProviderRequest.fromXContent(entityId, refreshPolicy, parser); - return channel -> client.execute(PutSamlServiceProviderAction.INSTANCE, request, - new RestBuilderListener<>(channel) { - @Override - public RestResponse buildResponse(PutSamlServiceProviderResponse response, XContentBuilder builder) throws Exception { - response.toXContent(builder, restRequest); - return new BytesRestResponse(RestStatus.OK, builder); - } - }); + return channel -> client.execute(PutSamlServiceProviderAction.INSTANCE, request, new RestBuilderListener<>(channel) { + @Override + public RestResponse buildResponse(PutSamlServiceProviderResponse response, XContentBuilder builder) throws Exception { + response.toXContent(builder, restRequest); + return new BytesRestResponse(RestStatus.OK, builder); + } + }); } } } diff --git a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/rest/action/RestSamlInitiateSingleSignOnAction.java b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/rest/action/RestSamlInitiateSingleSignOnAction.java index e4a49dda805ba..fc646bf750169 100644 --- a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/rest/action/RestSamlInitiateSingleSignOnAction.java +++ b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/rest/action/RestSamlInitiateSingleSignOnAction.java @@ -7,17 +7,16 @@ package org.elasticsearch.xpack.idp.saml.rest.action; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.xcontent.ObjectParser; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; - import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.idp.action.SamlInitiateSingleSignOnAction; import org.elasticsearch.xpack.idp.action.SamlInitiateSingleSignOnRequest; import org.elasticsearch.xpack.idp.action.SamlInitiateSingleSignOnResponse; @@ -30,14 +29,19 @@ import static org.elasticsearch.rest.RestRequest.Method.POST; public class RestSamlInitiateSingleSignOnAction extends IdpBaseRestHandler { - static final ObjectParser PARSER = new ObjectParser<>("idp_init_sso", - SamlInitiateSingleSignOnRequest::new); + static final ObjectParser PARSER = new ObjectParser<>( + "idp_init_sso", + SamlInitiateSingleSignOnRequest::new + ); static { PARSER.declareString(SamlInitiateSingleSignOnRequest::setSpEntityId, new ParseField("entity_id")); PARSER.declareString(SamlInitiateSingleSignOnRequest::setAssertionConsumerService, new ParseField("acs")); - PARSER.declareObject(SamlInitiateSingleSignOnRequest::setSamlAuthenticationState, (p, c) -> SamlAuthenticationState.fromXContent(p), - new ParseField("authn_state")); + PARSER.declareObject( + SamlInitiateSingleSignOnRequest::setSamlAuthenticationState, + (p, c) -> SamlAuthenticationState.fromXContent(p), + new ParseField("authn_state") + ); } public RestSamlInitiateSingleSignOnAction(XPackLicenseState licenseState) { @@ -46,9 +50,7 @@ public RestSamlInitiateSingleSignOnAction(XPackLicenseState licenseState) { @Override public List routes() { - return Collections.singletonList( - new Route(POST, "/_idp/saml/init") - ); + return Collections.singletonList(new Route(POST, "/_idp/saml/init")); } @Override @@ -60,7 +62,9 @@ public String getName() { protected RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClient client) throws IOException { try (XContentParser parser = request.contentParser()) { final SamlInitiateSingleSignOnRequest initRequest = PARSER.parse(parser, null); - return channel -> client.execute(SamlInitiateSingleSignOnAction.INSTANCE, initRequest, + return channel -> client.execute( + SamlInitiateSingleSignOnAction.INSTANCE, + initRequest, new RestBuilderListener(channel) { @Override public RestResponse buildResponse(SamlInitiateSingleSignOnResponse response, XContentBuilder builder) throws Exception { @@ -75,7 +79,8 @@ public RestResponse buildResponse(SamlInitiateSingleSignOnResponse response, XCo builder.endObject(); return new BytesRestResponse(RestStatus.OK, builder); } - }); + } + ); } } } diff --git a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/rest/action/RestSamlMetadataAction.java b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/rest/action/RestSamlMetadataAction.java index 96fc8054db0a4..61999cca75d61 100644 --- a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/rest/action/RestSamlMetadataAction.java +++ b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/rest/action/RestSamlMetadataAction.java @@ -7,13 +7,13 @@ package org.elasticsearch.xpack.idp.saml.rest.action; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.idp.action.SamlMetadataAction; import org.elasticsearch.xpack.idp.action.SamlMetadataRequest; import org.elasticsearch.xpack.idp.action.SamlMetadataResponse; @@ -44,7 +44,9 @@ protected RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClien final String spEntityId = request.param("sp_entity_id"); final String acs = request.param("acs"); final SamlMetadataRequest metadataRequest = new SamlMetadataRequest(spEntityId, acs); - return channel -> client.execute(SamlMetadataAction.INSTANCE, metadataRequest, + return channel -> client.execute( + SamlMetadataAction.INSTANCE, + metadataRequest, new RestBuilderListener(channel) { @Override public RestResponse buildResponse(SamlMetadataResponse response, XContentBuilder builder) throws Exception { @@ -53,7 +55,8 @@ public RestResponse buildResponse(SamlMetadataResponse response, XContentBuilder builder.endObject(); return new BytesRestResponse(RestStatus.OK, builder); } - }); + } + ); } } diff --git a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/rest/action/RestSamlValidateAuthenticationRequestAction.java b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/rest/action/RestSamlValidateAuthenticationRequestAction.java index a26a73b8968a3..88307073968cd 100644 --- a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/rest/action/RestSamlValidateAuthenticationRequestAction.java +++ b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/rest/action/RestSamlValidateAuthenticationRequestAction.java @@ -7,16 +7,16 @@ package org.elasticsearch.xpack.idp.saml.rest.action; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.xcontent.ObjectParser; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.idp.action.SamlValidateAuthnRequestAction; import org.elasticsearch.xpack.idp.action.SamlValidateAuthnRequestRequest; import org.elasticsearch.xpack.idp.action.SamlValidateAuthnRequestResponse; @@ -28,8 +28,10 @@ public class RestSamlValidateAuthenticationRequestAction extends IdpBaseRestHandler { - static final ObjectParser PARSER = - new ObjectParser<>("idp_validate_authn_request", SamlValidateAuthnRequestRequest::new); + static final ObjectParser PARSER = new ObjectParser<>( + "idp_validate_authn_request", + SamlValidateAuthnRequestRequest::new + ); static { PARSER.declareString(SamlValidateAuthnRequestRequest::setQueryString, new ParseField("authn_request_query")); @@ -53,7 +55,9 @@ public List routes() { protected RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClient client) throws IOException { try (XContentParser parser = request.contentParser()) { final SamlValidateAuthnRequestRequest validateRequest = PARSER.parse(parser, null); - return channel -> client.execute(SamlValidateAuthnRequestAction.INSTANCE, validateRequest, + return channel -> client.execute( + SamlValidateAuthnRequestAction.INSTANCE, + validateRequest, new RestBuilderListener(channel) { @Override public RestResponse buildResponse(SamlValidateAuthnRequestResponse response, XContentBuilder builder) throws Exception { @@ -67,7 +71,8 @@ public RestResponse buildResponse(SamlValidateAuthnRequestResponse response, XCo builder.endObject(); return new BytesRestResponse(RestStatus.OK, builder); } - }); + } + ); } } } diff --git a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/sp/CloudServiceProvider.java b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/sp/CloudServiceProvider.java index fccdc8ab562bd..4c6e006fe2b76 100644 --- a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/sp/CloudServiceProvider.java +++ b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/sp/CloudServiceProvider.java @@ -15,7 +15,6 @@ import java.time.Duration; import java.util.Set; - public class CloudServiceProvider implements SamlServiceProvider { private final String entityId; @@ -30,9 +29,19 @@ public class CloudServiceProvider implements SamlServiceProvider { private final boolean signAuthnRequests; private final boolean signLogoutRequests; - public CloudServiceProvider(String entityId, String name, boolean enabled, URL assertionConsumerService, String allowedNameIdFormat, - Duration authnExpiry, ServiceProviderPrivileges privileges, AttributeNames attributeNames, - Set spSigningCredentials, boolean signAuthnRequests, boolean signLogoutRequests) { + public CloudServiceProvider( + String entityId, + String name, + boolean enabled, + URL assertionConsumerService, + String allowedNameIdFormat, + Duration authnExpiry, + ServiceProviderPrivileges privileges, + AttributeNames attributeNames, + Set spSigningCredentials, + boolean signAuthnRequests, + boolean signLogoutRequests + ) { if (Strings.isNullOrEmpty(entityId)) { throw new IllegalArgumentException("Service Provider Entity ID cannot be null or empty"); } @@ -108,10 +117,17 @@ public ServiceProviderPrivileges getPrivileges() { public String toString() { return getClass().getSimpleName() + "{" - + "entityId=[" + entityId + ']' - + " name=[" + name + ']' - + " enabled=" + enabled - + " acs=[" + assertionConsumerService + "]" + + "entityId=[" + + entityId + + ']' + + " name=[" + + name + + ']' + + " enabled=" + + enabled + + " acs=[" + + assertionConsumerService + + "]" + "}"; } } diff --git a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/sp/SamlServiceProviderDocument.java b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/sp/SamlServiceProviderDocument.java index 622d860a1acdd..278fc186685c8 100644 --- a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/sp/SamlServiceProviderDocument.java +++ b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/sp/SamlServiceProviderDocument.java @@ -14,12 +14,12 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.util.set.Sets; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ObjectParser; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.core.Nullable; import org.elasticsearch.xpack.core.ssl.CertParsingUtils; import java.io.ByteArrayInputStream; @@ -70,8 +70,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; final Privileges that = (Privileges) o; - return Objects.equals(resource, that.resource) && - Objects.equals(rolePatterns, that.rolePatterns); + return Objects.equals(resource, that.resource) && Objects.equals(rolePatterns, that.rolePatterns); } @Override @@ -110,10 +109,10 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; final AttributeNames that = (AttributeNames) o; - return Objects.equals(principal, that.principal) && - Objects.equals(email, that.email) && - Objects.equals(name, that.name) && - Objects.equals(roles, that.roles); + return Objects.equals(principal, that.principal) + && Objects.equals(email, that.email) + && Objects.equals(name, that.name) + && Objects.equals(roles, that.roles); } @Override @@ -136,8 +135,9 @@ public void setIdentityProviderSigning(Collection identityProviderSignin } public void setIdentityProviderMetadataSigning(Collection identityProviderMetadataSigning) { - this.identityProviderMetadataSigning - = identityProviderMetadataSigning == null ? List.of() : List.copyOf(identityProviderMetadataSigning); + this.identityProviderMetadataSigning = identityProviderMetadataSigning == null + ? List.of() + : List.copyOf(identityProviderMetadataSigning); } public void setServiceProviderX509SigningCertificates(Collection certificates) { @@ -165,16 +165,13 @@ public List getIdentityProviderX509MetadataSigningCertificates( } private List encodeCertificates(Collection certificates) { - return certificates == null ? List.of() : certificates.stream() - .map(cert -> { - try { - return cert.getEncoded(); - } catch (CertificateEncodingException e) { - throw new ElasticsearchException("Cannot read certificate", e); - } - }) - .map(Base64.getEncoder()::encodeToString) - .collect(Collectors.toUnmodifiableList()); + return certificates == null ? List.of() : certificates.stream().map(cert -> { + try { + return cert.getEncoded(); + } catch (CertificateEncodingException e) { + throw new ElasticsearchException("Cannot read certificate", e); + } + }).map(Base64.getEncoder()::encodeToString).collect(Collectors.toUnmodifiableList()); } private List decodeCertificates(List encodedCertificates) { @@ -210,9 +207,9 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; final Certificates that = (Certificates) o; - return Objects.equals(serviceProviderSigning, that.serviceProviderSigning) && - Objects.equals(identityProviderSigning, that.identityProviderSigning) && - Objects.equals(identityProviderMetadataSigning, that.identityProviderMetadataSigning); + return Objects.equals(serviceProviderSigning, that.serviceProviderSigning) + && Objects.equals(identityProviderSigning, that.identityProviderSigning) + && Objects.equals(identityProviderMetadataSigning, that.identityProviderMetadataSigning); } @Override @@ -246,8 +243,7 @@ public int hashCode() { public final AttributeNames attributeNames = new AttributeNames(); public final Certificates certificates = new Certificates(); - public SamlServiceProviderDocument() { - } + public SamlServiceProviderDocument() {} public SamlServiceProviderDocument(StreamInput in) throws IOException { docId = in.readOptionalString(); @@ -363,34 +359,48 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; final SamlServiceProviderDocument that = (SamlServiceProviderDocument) o; - return Objects.equals(docId, that.docId) && - Objects.equals(name, that.name) && - Objects.equals(entityId, that.entityId) && - Objects.equals(acs, that.acs) && - Objects.equals(enabled, that.enabled) && - Objects.equals(created, that.created) && - Objects.equals(lastModified, that.lastModified) && - Objects.equals(nameIdFormat, that.nameIdFormat) && - Objects.equals(authenticationExpiryMillis, that.authenticationExpiryMillis) && - Objects.equals(certificates, that.certificates) && - Objects.equals(privileges, that.privileges) && - Objects.equals(attributeNames, that.attributeNames); + return Objects.equals(docId, that.docId) + && Objects.equals(name, that.name) + && Objects.equals(entityId, that.entityId) + && Objects.equals(acs, that.acs) + && Objects.equals(enabled, that.enabled) + && Objects.equals(created, that.created) + && Objects.equals(lastModified, that.lastModified) + && Objects.equals(nameIdFormat, that.nameIdFormat) + && Objects.equals(authenticationExpiryMillis, that.authenticationExpiryMillis) + && Objects.equals(certificates, that.certificates) + && Objects.equals(privileges, that.privileges) + && Objects.equals(attributeNames, that.attributeNames); } @Override public int hashCode() { - return Objects.hash(docId, name, entityId, acs, enabled, created, lastModified, nameIdFormat, - authenticationExpiryMillis, certificates, privileges, attributeNames); + return Objects.hash( + docId, + name, + entityId, + acs, + enabled, + created, + lastModified, + nameIdFormat, + authenticationExpiryMillis, + certificates, + privileges, + attributeNames + ); } - private static final ObjectParser DOC_PARSER - = new ObjectParser<>("service_provider_doc", true, SamlServiceProviderDocument::new); + private static final ObjectParser DOC_PARSER = new ObjectParser<>( + "service_provider_doc", + true, + SamlServiceProviderDocument::new + ); private static final ObjectParser PRIVILEGES_PARSER = new ObjectParser<>("service_provider_priv", true, null); private static final ObjectParser ATTRIBUTES_PARSER = new ObjectParser<>("service_provider_attr", true, null); private static final ObjectParser CERTIFICATES_PARSER = new ObjectParser<>("service_provider_cert", true, null); - private static final BiConsumer NULL_CONSUMER = (doc, obj) -> { - }; + private static final BiConsumer NULL_CONSUMER = (doc, obj) -> {}; static { DOC_PARSER.declareString(SamlServiceProviderDocument::setName, Fields.NAME); @@ -401,9 +411,12 @@ public int hashCode() { DOC_PARSER.declareLong(SamlServiceProviderDocument::setLastModifiedMillis, Fields.LAST_MODIFIED); DOC_PARSER.declareStringOrNull(SamlServiceProviderDocument::setNameIdFormat, Fields.NAME_ID); DOC_PARSER.declareStringArray(SamlServiceProviderDocument::setSignMessages, Fields.SIGN_MSGS); - DOC_PARSER.declareField(SamlServiceProviderDocument::setAuthenticationExpiryMillis, + DOC_PARSER.declareField( + SamlServiceProviderDocument::setAuthenticationExpiryMillis, parser -> parser.currentToken() == XContentParser.Token.VALUE_NULL ? null : parser.longValue(), - Fields.AUTHN_EXPIRY, ObjectParser.ValueType.LONG_OR_NULL); + Fields.AUTHN_EXPIRY, + ObjectParser.ValueType.LONG_OR_NULL + ); DOC_PARSER.declareObject(NULL_CONSUMER, (parser, doc) -> PRIVILEGES_PARSER.parse(parser, doc.privileges, null), Fields.PRIVILEGES); PRIVILEGES_PARSER.declareString(Privileges::setResource, Fields.Privileges.RESOURCE); @@ -447,17 +460,32 @@ public ValidationException validate() { final Set invalidSignOptions = Sets.difference(signMessages, ALLOWED_SIGN_MESSAGES); if (invalidSignOptions.isEmpty() == false) { - validation.addValidationError("the values [" + invalidSignOptions + "] are not permitted for [" + Fields.SIGN_MSGS - + "] - permitted values are [" + ALLOWED_SIGN_MESSAGES + "]"); + validation.addValidationError( + "the values [" + + invalidSignOptions + + "] are not permitted for [" + + Fields.SIGN_MSGS + + "] - permitted values are [" + + ALLOWED_SIGN_MESSAGES + + "]" + ); } if (Strings.isNullOrEmpty(privileges.resource)) { - validation.addValidationError("field [" + Fields.PRIVILEGES + "." + Fields.Privileges.RESOURCE - + "] is required, but was [" + privileges.resource + "]"); + validation.addValidationError( + "field [" + Fields.PRIVILEGES + "." + Fields.Privileges.RESOURCE + "] is required, but was [" + privileges.resource + "]" + ); } if (Strings.isNullOrEmpty(attributeNames.principal)) { - validation.addValidationError("field [" + Fields.ATTRIBUTES + "." + Fields.Attributes.PRINCIPAL - + "] is required, but was [" + attributeNames.principal + "]"); + validation.addValidationError( + "field [" + + Fields.ATTRIBUTES + + "." + + Fields.Attributes.PRINCIPAL + + "] is required, but was [" + + attributeNames.principal + + "]" + ); } if (validation.validationErrors().isEmpty()) { return null; diff --git a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/sp/SamlServiceProviderFactory.java b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/sp/SamlServiceProviderFactory.java index 1a46131e8ed02..ec8fc728c61f3 100644 --- a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/sp/SamlServiceProviderFactory.java +++ b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/sp/SamlServiceProviderFactory.java @@ -35,7 +35,10 @@ public SamlServiceProviderFactory(ServiceProviderDefaults defaults) { SamlServiceProvider buildServiceProvider(SamlServiceProviderDocument document) { final ServiceProviderPrivileges privileges = buildPrivileges(document.privileges); final SamlServiceProvider.AttributeNames attributes = new SamlServiceProvider.AttributeNames( - document.attributeNames.principal, document.attributeNames.name, document.attributeNames.email, document.attributeNames.roles + document.attributeNames.principal, + document.attributeNames.name, + document.attributeNames.email, + document.attributeNames.roles ); final Set credentials = document.certificates.getServiceProviderX509SigningCertificates() .stream() @@ -53,8 +56,19 @@ SamlServiceProvider buildServiceProvider(SamlServiceProviderDocument document) { final boolean signAuthnRequests = document.signMessages.contains(SamlServiceProviderDocument.SIGN_AUTHN); final boolean signLogoutRequests = document.signMessages.contains(SamlServiceProviderDocument.SIGN_LOGOUT); - return new CloudServiceProvider(document.entityId, document.name, document.enabled, acs, nameIdFormat, authnExpiry, - privileges, attributes, credentials, signAuthnRequests, signLogoutRequests); + return new CloudServiceProvider( + document.entityId, + document.name, + document.enabled, + acs, + nameIdFormat, + authnExpiry, + privileges, + attributes, + credentials, + signAuthnRequests, + signLogoutRequests + ); } private ServiceProviderPrivileges buildPrivileges(SamlServiceProviderDocument.Privileges configuredPrivileges) { @@ -81,7 +95,12 @@ private URL parseUrl(SamlServiceProviderDocument document) { acs = new URL(document.acs); } catch (MalformedURLException e) { final ServiceProviderException exception = new ServiceProviderException( - "Service provider [{}] (doc {}) has an invalid ACS [{}]", e, document.entityId, document.docId, document.acs); + "Service provider [{}] (doc {}) has an invalid ACS [{}]", + e, + document.entityId, + document.docId, + document.acs + ); exception.setEntityId(document.entityId); throw exception; } diff --git a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/sp/SamlServiceProviderIndex.java b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/sp/SamlServiceProviderIndex.java index b4a0eea512c6b..804ba0f04afae 100644 --- a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/sp/SamlServiceProviderIndex.java +++ b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/sp/SamlServiceProviderIndex.java @@ -36,17 +36,17 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.util.CachedSupplier; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.gateway.GatewayService; import org.elasticsearch.index.IndexNotFoundException; import org.elasticsearch.index.get.GetResult; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.ClientHelper; import org.elasticsearch.xpack.core.template.TemplateUtils; @@ -110,8 +110,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; final DocumentVersion that = (DocumentVersion) o; - return Objects.equals(this.id, that.id) && primaryTerm == that.primaryTerm && - seqNo == that.seqNo; + return Objects.equals(this.id, that.id) && primaryTerm == that.primaryTerm && seqNo == that.seqNo; } @Override @@ -161,16 +160,14 @@ private void installTemplateIfRequired(ClusterState state) { if (state.nodes().isLocalNodeElectedMaster() == false) { return; } - installIndexTemplate(ActionListener.wrap( - installed -> { - templateInstalled = true; - if (installed) { - logger.debug("Template [{}] has been updated", TEMPLATE_NAME); - } else { - logger.debug("Template [{}] appears to be up to date", TEMPLATE_NAME); - } - }, e -> logger.warn(new ParameterizedMessage("Failed to install template [{}]", TEMPLATE_NAME), e) - )); + installIndexTemplate(ActionListener.wrap(installed -> { + templateInstalled = true; + if (installed) { + logger.debug("Template [{}] has been updated", TEMPLATE_NAME); + } else { + logger.debug("Template [{}] appears to be up to date", TEMPLATE_NAME); + } + }, e -> logger.warn(new ParameterizedMessage("Failed to install template [{}]", TEMPLATE_NAME), e))); } private void checkForAliasStateChange(ClusterState state) { @@ -194,8 +191,11 @@ private void logChangedAliasState(IndexAbstraction aliasInfo) { } else if (aliasInfo.getType() != IndexAbstraction.Type.ALIAS) { logger.warn("service provider index [{}] does not exist as an alias, but it should be", ALIAS_NAME); } else if (aliasInfo.getIndices().size() != 1) { - logger.warn("service provider alias [{}] refers to multiple indices [{}] - this is unexpected and is likely to cause problems", - ALIAS_NAME, Strings.collectionToCommaDelimitedString(aliasInfo.getIndices())); + logger.warn( + "service provider alias [{}] refers to multiple indices [{}] - this is unexpected and is likely to cause problems", + ALIAS_NAME, + Strings.collectionToCommaDelimitedString(aliasInfo.getIndices()) + ); } else { logger.info("service provider alias [{}] refers to [{}]", ALIAS_NAME, aliasInfo.getIndices().get(0)); } @@ -220,8 +220,7 @@ private boolean isTemplateUpToDate(ClusterState state) { } public void deleteDocument(DocumentVersion version, WriteRequest.RefreshPolicy refreshPolicy, ActionListener listener) { - final DeleteRequest request = new DeleteRequest(aliasExists ? ALIAS_NAME : INDEX_NAME) - .id(version.id) + final DeleteRequest request = new DeleteRequest(aliasExists ? ALIAS_NAME : INDEX_NAME).id(version.id) .setIfSeqNo(version.seqNo) .setIfPrimaryTerm(version.primaryTerm) .setRefreshPolicy(refreshPolicy); @@ -231,8 +230,12 @@ public void deleteDocument(DocumentVersion version, WriteRequest.RefreshPolicy r }, listener::onFailure)); } - public void writeDocument(SamlServiceProviderDocument document, DocWriteRequest.OpType opType, - WriteRequest.RefreshPolicy refreshPolicy, ActionListener listener) { + public void writeDocument( + SamlServiceProviderDocument document, + DocWriteRequest.OpType opType, + WriteRequest.RefreshPolicy refreshPolicy, + ActionListener listener + ) { final ValidationException exception = document.validate(); if (exception != null) { listener.onFailure(exception); @@ -242,27 +245,38 @@ public void writeDocument(SamlServiceProviderDocument document, DocWriteRequest. if (templateInstalled) { _writeDocument(document, opType, refreshPolicy, listener); } else { - installIndexTemplate(ActionListener.wrap(installed -> - _writeDocument(document, opType, refreshPolicy, listener), listener::onFailure)); + installIndexTemplate( + ActionListener.wrap(installed -> _writeDocument(document, opType, refreshPolicy, listener), listener::onFailure) + ); } } - private void _writeDocument(SamlServiceProviderDocument document, DocWriteRequest.OpType opType, - WriteRequest.RefreshPolicy refreshPolicy, ActionListener listener) { - try (ByteArrayOutputStream out = new ByteArrayOutputStream(); - XContentBuilder xContentBuilder = new XContentBuilder(XContentType.JSON.xContent(), out)) { + private void _writeDocument( + SamlServiceProviderDocument document, + DocWriteRequest.OpType opType, + WriteRequest.RefreshPolicy refreshPolicy, + ActionListener listener + ) { + try ( + ByteArrayOutputStream out = new ByteArrayOutputStream(); + XContentBuilder xContentBuilder = new XContentBuilder(XContentType.JSON.xContent(), out) + ) { document.toXContent(xContentBuilder, ToXContent.EMPTY_PARAMS); // Due to the lack of "alias templates" (at the current time), we cannot write to the alias if it doesn't exist yet // - that would cause the alias to be created as a concrete index, which is not what we want. // So, until we know that the alias exists we have to write to the expected index name instead. - final IndexRequest request = new IndexRequest(aliasExists ? ALIAS_NAME : INDEX_NAME) - .opType(opType) + final IndexRequest request = new IndexRequest(aliasExists ? ALIAS_NAME : INDEX_NAME).opType(opType) .source(xContentBuilder) .id(document.docId) .setRefreshPolicy(refreshPolicy); client.index(request, ActionListener.wrap(response -> { - logger.debug("Wrote service provider [{}][{}] as document [{}] ({})", - document.name, document.entityId, response.getId(), response.getResult()); + logger.debug( + "Wrote service provider [{}][{}] as document [{}] ({})", + document.name, + document.entityId, + response.getId(), + response.getResult() + ); listener.onResponse(response); }, listener::onFailure)); } catch (IOException e) { @@ -294,17 +308,14 @@ public void findAll(ActionListener> listener) { } public void refresh(ActionListener listener) { - client.admin().indices().refresh(new RefreshRequest(ALIAS_NAME), ActionListener.wrap( - response -> listener.onResponse(null), listener::onFailure)); + client.admin() + .indices() + .refresh(new RefreshRequest(ALIAS_NAME), ActionListener.wrap(response -> listener.onResponse(null), listener::onFailure)); } private void findDocuments(QueryBuilder query, ActionListener> listener) { logger.trace("Searching [{}] for [{}]", ALIAS_NAME, query); - final SearchRequest request = client.prepareSearch(ALIAS_NAME) - .setQuery(query) - .setSize(1000) - .setFetchSource(true) - .request(); + final SearchRequest request = client.prepareSearch(ALIAS_NAME).setQuery(query).setSize(1000).setFetchSource(true).request(); client.search(request, ActionListener.wrap(response -> { if (logger.isTraceEnabled()) { logger.trace("Search hits: [{}] [{}]", response.getHits().getTotalHits(), Arrays.toString(response.getHits().getHits())); @@ -323,9 +334,11 @@ private void findDocuments(QueryBuilder query, ActionListener listener) { - index.findByEntityId(entityId, ActionListener.wrap( - documentSuppliers -> { - if (documentSuppliers.isEmpty()) { - listener.onResponse(null); - return; - } - if (documentSuppliers.size() > 1) { - listener.onFailure(new IllegalStateException( - "Found multiple service providers with entity ID [" + entityId + index.findByEntityId(entityId, ActionListener.wrap(documentSuppliers -> { + if (documentSuppliers.isEmpty()) { + listener.onResponse(null); + return; + } + if (documentSuppliers.size() > 1) { + listener.onFailure( + new IllegalStateException( + "Found multiple service providers with entity ID [" + + entityId + "] - document ids [" + documentSuppliers.stream().map(s -> s.version.id).collect(Collectors.joining(",")) - + "] in index [" + index + "]")); - return; - } - final DocumentSupplier doc = Iterables.get(documentSuppliers, 0); - final CachedServiceProvider cached = cache.get(entityId); - if (cached != null && cached.documentVersion.equals(doc.version)) { - listener.onResponse(cached.serviceProvider); - } else { - populateCacheAndReturn(entityId, doc, listener); - } - }, - listener::onFailure - )); + + "] in index [" + + index + + "]" + ) + ); + return; + } + final DocumentSupplier doc = Iterables.get(documentSuppliers, 0); + final CachedServiceProvider cached = cache.get(entityId); + if (cached != null && cached.documentVersion.equals(doc.version)) { + listener.onResponse(cached.serviceProvider); + } else { + populateCacheAndReturn(entityId, doc, listener); + } + }, listener::onFailure)); } private void populateCacheAndReturn(String entityId, DocumentSupplier doc, ActionListener listener) { diff --git a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/sp/ServiceProviderCacheSettings.java b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/sp/ServiceProviderCacheSettings.java index a06dc9823dbe6..0b6b000554a5b 100644 --- a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/sp/ServiceProviderCacheSettings.java +++ b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/sp/ServiceProviderCacheSettings.java @@ -22,10 +22,16 @@ public final class ServiceProviderCacheSettings { private static final int CACHE_SIZE_DEFAULT = 1000; private static final TimeValue CACHE_TTL_DEFAULT = TimeValue.timeValueMinutes(60); - public static final Setting CACHE_SIZE - = Setting.intSetting("xpack.idp.sp.cache.size", CACHE_SIZE_DEFAULT, Setting.Property.NodeScope); - public static final Setting CACHE_TTL - = Setting.timeSetting("xpack.idp.sp.cache.ttl", CACHE_TTL_DEFAULT, Setting.Property.NodeScope); + public static final Setting CACHE_SIZE = Setting.intSetting( + "xpack.idp.sp.cache.size", + CACHE_SIZE_DEFAULT, + Setting.Property.NodeScope + ); + public static final Setting CACHE_TTL = Setting.timeSetting( + "xpack.idp.sp.cache.ttl", + CACHE_TTL_DEFAULT, + Setting.Property.NodeScope + ); static Cache buildCache(Settings settings) { return CacheBuilder.builder() diff --git a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/sp/ServiceProviderDefaults.java b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/sp/ServiceProviderDefaults.java index 3fc9407729d66..c99dceda1e81c 100644 --- a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/sp/ServiceProviderDefaults.java +++ b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/sp/ServiceProviderDefaults.java @@ -20,20 +20,26 @@ */ public final class ServiceProviderDefaults { - public static final Setting APPLICATION_NAME_SETTING - = Setting.simpleString("xpack.idp.privileges.application", Setting.Property.NodeScope); - public static final Setting NAMEID_FORMAT_SETTING - = Setting.simpleString("xpack.idp.defaults.nameid_format", NameID.TRANSIENT, Setting.Property.NodeScope); - public static final Setting AUTHN_EXPIRY_SETTING - = Setting.timeSetting("xpack.idp.defaults.authn_expiry", TimeValue.timeValueMinutes(5), Setting.Property.NodeScope); + public static final Setting APPLICATION_NAME_SETTING = Setting.simpleString( + "xpack.idp.privileges.application", + Setting.Property.NodeScope + ); + public static final Setting NAMEID_FORMAT_SETTING = Setting.simpleString( + "xpack.idp.defaults.nameid_format", + NameID.TRANSIENT, + Setting.Property.NodeScope + ); + public static final Setting AUTHN_EXPIRY_SETTING = Setting.timeSetting( + "xpack.idp.defaults.authn_expiry", + TimeValue.timeValueMinutes(5), + Setting.Property.NodeScope + ); public final String applicationName; public final String nameIdFormat; public final Duration authenticationExpiry; - public ServiceProviderDefaults(String applicationName, - String nameIdFormat, - Duration authenticationExpiry) { + public ServiceProviderDefaults(String applicationName, String nameIdFormat, Duration authenticationExpiry) { this.applicationName = applicationName; this.nameIdFormat = nameIdFormat; this.authenticationExpiry = authenticationExpiry; diff --git a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/sp/WildcardServiceProvider.java b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/sp/WildcardServiceProvider.java index fc42c8ff4ed74..f358966526535 100644 --- a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/sp/WildcardServiceProvider.java +++ b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/sp/WildcardServiceProvider.java @@ -7,19 +7,19 @@ package org.elasticsearch.xpack.idp.saml.sp; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.script.ScriptService; +import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.script.ScriptService; import org.elasticsearch.xpack.core.security.support.MustacheTemplateEvaluator; import java.io.IOException; @@ -48,7 +48,8 @@ class WildcardServiceProvider { final Collection tokens = (Collection) args[2]; final Map definition = (Map) args[3]; return new WildcardServiceProvider(entityId, acs, tokens, definition); - }); + } + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), Fields.ENTITY_ID); @@ -70,10 +71,12 @@ private WildcardServiceProvider(Pattern matchEntityId, Pattern matchAcs, Set tokens, Map serviceTemplate) { - this(Pattern.compile(Objects.requireNonNull(matchEntityId, "EntityID to match cannot be null")), + this( + Pattern.compile(Objects.requireNonNull(matchEntityId, "EntityID to match cannot be null")), Pattern.compile(Objects.requireNonNull(matchAcs, "ACS to match cannot be null")), Set.copyOf(Objects.requireNonNull(tokens, "Tokens collection may not be null")), - toMustacheScript(Objects.requireNonNull(serviceTemplate, "Service definition may not be null"))); + toMustacheScript(Objects.requireNonNull(serviceTemplate, "Service definition may not be null")) + ); } public static WildcardServiceProvider parse(XContentParser parser) throws IOException { @@ -89,10 +92,10 @@ public boolean equals(Object o) { return false; } final WildcardServiceProvider that = (WildcardServiceProvider) o; - return matchEntityId.pattern().equals(that.matchEntityId.pattern()) && - matchAcs.pattern().equals(that.matchAcs.pattern()) && - tokens.equals(that.tokens) && - serviceTemplate.equals(that.serviceTemplate); + return matchEntityId.pattern().equals(that.matchEntityId.pattern()) + && matchAcs.pattern().equals(that.matchAcs.pattern()) + && tokens.equals(that.tokens) + && serviceTemplate.equals(that.serviceTemplate); } @Override @@ -151,8 +154,19 @@ Map extractTokens(String entityId, String acs) { if (entityIdToken != null) { if (acsToken != null) { if (entityIdToken.equals(acsToken) == false) { - throw new IllegalArgumentException("Extracted token [" + token + "] values from EntityID ([" + entityIdToken - + "] from [" + entityId + "]) and ACS ([" + acsToken + "] from [" + acs + "]) do not match"); + throw new IllegalArgumentException( + "Extracted token [" + + token + + "] values from EntityID ([" + + entityIdToken + + "] from [" + + entityId + + "]) and ACS ([" + + acsToken + + "] from [" + + acs + + "]) do not match" + ); } } parameters.put(token, entityIdToken); diff --git a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/sp/WildcardServiceProviderResolver.java b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/sp/WildcardServiceProviderResolver.java index e80ffab1adb9e..5e3a929913093 100644 --- a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/sp/WildcardServiceProviderResolver.java +++ b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/sp/WildcardServiceProviderResolver.java @@ -10,25 +10,25 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.ElasticsearchException; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.cache.Cache; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.iterable.Iterables; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentLocation; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParserUtils; -import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.core.Tuple; import org.elasticsearch.env.Environment; import org.elasticsearch.script.ScriptService; import org.elasticsearch.watcher.FileChangesListener; import org.elasticsearch.watcher.FileWatcher; import org.elasticsearch.watcher.ResourceWatcherService; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentLocation; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.XPackPlugin; import java.io.IOException; @@ -44,8 +44,11 @@ public class WildcardServiceProviderResolver { - public static final Setting FILE_PATH_SETTING = Setting.simpleString("xpack.idp.sp.wildcard.path", - "wildcard_services.json", Setting.Property.NodeScope); + public static final Setting FILE_PATH_SETTING = Setting.simpleString( + "xpack.idp.sp.wildcard.path", + "wildcard_services.json", + Setting.Property.NodeScope + ); private class State { final Map services; @@ -75,10 +78,12 @@ private State(Map services) { * This is implemented as a factory method to facilitate testing - the core resolver just works on InputStreams, this method * handles all the Path/ResourceWatcher logic */ - public static WildcardServiceProviderResolver create(Environment environment, - ResourceWatcherService resourceWatcherService, - ScriptService scriptService, - SamlServiceProviderFactory spFactory) { + public static WildcardServiceProviderResolver create( + Environment environment, + ResourceWatcherService resourceWatcherService, + ScriptService scriptService, + SamlServiceProviderFactory spFactory + ) { final Settings settings = environment.settings(); final Path path = XPackPlugin.resolveConfigFile(environment, FILE_PATH_SETTING.get(environment.settings())); @@ -90,13 +95,20 @@ public static WildcardServiceProviderResolver create(Environment environment, try { resolver.reload(path); } catch (IOException e) { - throw new ElasticsearchException("File [{}] (from setting [{}]) cannot be loaded", - e, path.toAbsolutePath(), FILE_PATH_SETTING.getKey()); + throw new ElasticsearchException( + "File [{}] (from setting [{}]) cannot be loaded", + e, + path.toAbsolutePath(), + FILE_PATH_SETTING.getKey() + ); } } else if (FILE_PATH_SETTING.exists(environment.settings())) { // A file was explicitly configured, but doesn't exist. That's a mistake... - throw new ElasticsearchException("File [{}] (from setting [{}]) does not exist", - path.toAbsolutePath(), FILE_PATH_SETTING.getKey()); + throw new ElasticsearchException( + "File [{}] (from setting [{}]) does not exist", + path.toAbsolutePath(), + FILE_PATH_SETTING.getKey() + ); } final FileWatcher fileWatcher = new FileWatcher(path); @@ -123,8 +135,12 @@ public void onFileChanged(Path file) { try { resourceWatcherService.add(fileWatcher); } catch (IOException e) { - throw new ElasticsearchException("Failed to watch file [{}] (from setting [{}])", - e, path.toAbsolutePath(), FILE_PATH_SETTING.getKey()); + throw new ElasticsearchException( + "Failed to watch file [{}] (from setting [{}])", + e, + path.toAbsolutePath(), + FILE_PATH_SETTING.getKey() + ); } return resolver; } @@ -163,8 +179,14 @@ public SamlServiceProvider resolve(String entityId, String acs) { final String names = Strings.collectionToCommaDelimitedString(matches.keySet()); logger.warn("Found multiple matching wildcard services for [{}] [{}] - [{}]", entityId, acs, names); throw new IllegalStateException( - "Found multiple wildcard service providers for entity ID [" + entityId + "] and ACS [" + acs - + "] - wildcard service names [" + names + "]"); + "Found multiple wildcard service providers for entity ID [" + + entityId + + "] and ACS [" + + acs + + "] - wildcard service names [" + + names + + "]" + ); } } @@ -180,8 +202,10 @@ void reload(XContentParser parser) throws IOException { if (newServices.equals(oldState.services) == false) { // Services have changed if (this.stateRef.compareAndSet(oldState, new State(newServices))) { - logger.info("Reloaded cached wildcard service providers, new providers [{}]", - Strings.collectionToCommaDelimitedString(newServices.keySet())); + logger.info( + "Reloaded cached wildcard service providers, new providers [{}]", + Strings.collectionToCommaDelimitedString(newServices.keySet()) + ); } else { // some other thread reloaded it } @@ -189,8 +213,7 @@ void reload(XContentParser parser) throws IOException { } private void reload(Path file) throws IOException { - try (InputStream in = Files.newInputStream(file); - XContentParser parser = buildServicesParser(in)) { + try (InputStream in = Files.newInputStream(file); XContentParser parser = buildServicesParser(in)) { reload(parser); } } diff --git a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/support/SamlAuthenticationState.java b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/support/SamlAuthenticationState.java index 2022345390b12..b6e4a23c6e21b 100644 --- a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/support/SamlAuthenticationState.java +++ b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/support/SamlAuthenticationState.java @@ -6,13 +6,13 @@ */ package org.elasticsearch.xpack.idp.saml.support; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -57,8 +57,11 @@ public void setAuthnRequestId(String authnRequestId) { this.authnRequestId = authnRequestId; } - public static final ObjectParser PARSER - = new ObjectParser<>("saml_authn_state", true, SamlAuthenticationState::new); + public static final ObjectParser PARSER = new ObjectParser<>( + "saml_authn_state", + true, + SamlAuthenticationState::new + ); static { PARSER.declareStringOrNull(SamlAuthenticationState::setRequestedNameidFormat, Fields.NAMEID_FORMAT); @@ -99,8 +102,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; SamlAuthenticationState that = (SamlAuthenticationState) o; - return Objects.equals(requestedNameidFormat, that.requestedNameidFormat) && - Objects.equals(authnRequestId, that.authnRequestId); + return Objects.equals(requestedNameidFormat, that.requestedNameidFormat) && Objects.equals(authnRequestId, that.authnRequestId); } @Override diff --git a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/support/SamlFactory.java b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/support/SamlFactory.java index 954d86157c310..ac4583f645a4c 100644 --- a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/support/SamlFactory.java +++ b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/support/SamlFactory.java @@ -10,8 +10,8 @@ import org.apache.logging.log4j.Logger; import org.elasticsearch.ElasticsearchSecurityException; import org.elasticsearch.common.Strings; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.common.hash.MessageDigests; +import org.elasticsearch.core.SuppressForbidden; import org.opensaml.core.xml.XMLObject; import org.opensaml.core.xml.XMLObjectBuilderFactory; import org.opensaml.core.xml.config.XMLObjectProviderRegistrySupport; @@ -27,6 +27,17 @@ import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; +import java.io.StringWriter; +import java.io.Writer; +import java.net.URISyntaxException; +import java.security.SecureRandom; +import java.security.cert.CertificateEncodingException; +import java.util.Arrays; +import java.util.Base64; +import java.util.Collection; +import java.util.Objects; +import java.util.stream.Collectors; + import javax.xml.XMLConstants; import javax.xml.namespace.QName; import javax.xml.parsers.DocumentBuilder; @@ -39,16 +50,6 @@ import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; -import java.io.StringWriter; -import java.io.Writer; -import java.net.URISyntaxException; -import java.security.SecureRandom; -import java.security.cert.CertificateEncodingException; -import java.util.Arrays; -import java.util.Base64; -import java.util.Collection; -import java.util.Objects; -import java.util.stream.Collectors; import static org.opensaml.core.xml.config.XMLObjectProviderRegistrySupport.getUnmarshallerFactory; @@ -81,8 +82,9 @@ private T cast(Class type, QName elementName, XMLObject if (type.isInstance(obj)) { return type.cast(obj); } else { - throw new IllegalArgumentException("Object for element " + elementName.getLocalPart() + " is of type " + obj.getClass() - + " not " + type); + throw new IllegalArgumentException( + "Object for element " + elementName.getLocalPart() + " is of type " + obj.getClass() + " not " + type + ); } } @@ -102,8 +104,9 @@ public T buildObject(Class type, QName elementName) { if (type.isInstance(obj)) { return type.cast(obj); } else { - throw new IllegalArgumentException("Object for element " + elementName.getLocalPart() + " is of type " + obj.getClass() - + " not " + type); + throw new IllegalArgumentException( + "Object for element " + elementName.getLocalPart() + " is of type " + obj.getClass() + " not " + type + ); } } @@ -122,14 +125,17 @@ public T buildXmlObject(Element element, Class type) { UnmarshallerFactory unmarshallerFactory = getUnmarshallerFactory(); Unmarshaller unmarshaller = unmarshallerFactory.getUnmarshaller(element); if (unmarshaller == null) { - throw new ElasticsearchSecurityException("XML element [{}] cannot be unmarshalled to SAML type [{}] (no unmarshaller)", - element.getTagName(), type); + throw new ElasticsearchSecurityException( + "XML element [{}] cannot be unmarshalled to SAML type [{}] (no unmarshaller)", + element.getTagName(), + type + ); } final XMLObject object = unmarshaller.unmarshall(element); if (type.isInstance(object)) { return type.cast(object); } - Object[] args = new Object[]{element.getTagName(), type.getName(), object.getClass().getName()}; + Object[] args = new Object[] { element.getTagName(), type.getName(), object.getClass().getName() }; throw new ElasticsearchSecurityException("SAML object [{}] is incorrect type. Expected [{}] but was [{}]", args); } catch (UnmarshallingException e) { throw new ElasticsearchSecurityException("Failed to unmarshall SAML content [{}]", e, element.getTagName()); @@ -144,7 +150,7 @@ void print(Element element, Writer writer, boolean pretty) throws TransformerExc serializer.transform(new DOMSource(element), new StreamResult(writer)); } - public String getXmlContent(SAMLObject object){ + public String getXmlContent(SAMLObject object) { return getXmlContent(object, false); } @@ -197,25 +203,23 @@ protected static String text(Element dom, int prefixLength, int suffixLength) { } public String describeCredentials(Collection credentials) { - return credentials.stream() - .map(c -> { - if (c == null) { - return ""; - } - byte[] encoded; - if (c instanceof X509Credential) { - X509Credential x = (X509Credential) c; - try { - encoded = x.getEntityCertificate().getEncoded(); - } catch (CertificateEncodingException e) { - encoded = c.getPublicKey().getEncoded(); - } - } else { + return credentials.stream().map(c -> { + if (c == null) { + return ""; + } + byte[] encoded; + if (c instanceof X509Credential) { + X509Credential x = (X509Credential) c; + try { + encoded = x.getEntityCertificate().getEncoded(); + } catch (CertificateEncodingException e) { encoded = c.getPublicKey().getEncoded(); } - return Base64.getEncoder().encodeToString(encoded).substring(0, 64) + "..."; - }) - .collect(Collectors.joining(",")); + } else { + encoded = c.getPublicKey().getEncoded(); + } + return Base64.getEncoder().encodeToString(encoded).substring(0, 64) + "..."; + }).collect(Collectors.joining(",")); } public Element toDomElement(XMLObject object) { @@ -226,7 +230,6 @@ public Element toDomElement(XMLObject object) { } } - @SuppressForbidden(reason = "This is the only allowed way to construct a Transformer") public Transformer getHardenedXMLTransformer() throws TransformerConfigurationException { final TransformerFactory tfactory = TransformerFactory.newInstance(); @@ -272,8 +275,7 @@ public static DocumentBuilder getHardenedBuilder(String[] schemaFiles) throws Pa dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); dbf.setAttribute("http://apache.org/xml/features/validation/schema", true); dbf.setAttribute("http://apache.org/xml/features/validation/schema-full-checking", true); - dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", - XMLConstants.W3C_XML_SCHEMA_NS_URI); + dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", XMLConstants.W3C_XML_SCHEMA_NS_URI); // We ship our own xsd files for schema validation since we do not trust anyone else. dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", resolveSchemaFilePaths(schemaFiles)); DocumentBuilder documentBuilder = dbf.newDocumentBuilder(); @@ -298,18 +300,16 @@ public String getJavaAlorithmNameFromUri(String sigAlg) { } } - private static String[] resolveSchemaFilePaths(String[] relativePaths) { - return Arrays.stream(relativePaths). - map(file -> { - try { - return SamlFactory.class.getResource(file).toURI().toString(); - } catch (URISyntaxException e) { - LOGGER.warn("Error resolving schema file path", e); - return null; - } - }).filter(Objects::nonNull).toArray(String[]::new); + return Arrays.stream(relativePaths).map(file -> { + try { + return SamlFactory.class.getResource(file).toURI().toString(); + } catch (URISyntaxException e) { + LOGGER.warn("Error resolving schema file path", e); + return null; + } + }).filter(Objects::nonNull).toArray(String[]::new); } private static class DocumentBuilderErrorHandler implements org.xml.sax.ErrorHandler { @@ -357,4 +357,3 @@ public void fatalError(TransformerException e) throws TransformerException { } } - diff --git a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/support/SamlInit.java b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/support/SamlInit.java index 82a2054a3bfac..07a765f859d5a 100644 --- a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/support/SamlInit.java +++ b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/support/SamlInit.java @@ -25,7 +25,7 @@ public final class SamlInit { private static final AtomicBoolean INITIALISED = new AtomicBoolean(false); private static final Logger LOGGER = LogManager.getLogger(); - private SamlInit() { } + private SamlInit() {} /** * This is needed in order to initialize the underlying OpenSAML library. @@ -44,7 +44,7 @@ public static void initialize() { try (RestorableContextClassLoader ignore = new RestorableContextClassLoader(InitializationService.class)) { InitializationService.initialize(); // Force load this now, because it has a static field that needs to run inside the doPrivileged block - var ignore2 = new X509CertificateBuilder().buildObject(); + var ignore2 = new X509CertificateBuilder().buildObject(); } LOGGER.debug("Initialized OpenSAML"); return null; diff --git a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/support/XmlValidator.java b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/support/XmlValidator.java index f497b882b6052..027b38d8d6ce4 100644 --- a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/support/XmlValidator.java +++ b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/support/XmlValidator.java @@ -13,11 +13,6 @@ import org.w3c.dom.ls.LSInput; import org.w3c.dom.ls.LSResourceResolver; -import javax.xml.XMLConstants; -import javax.xml.transform.stream.StreamSource; -import javax.xml.validation.Schema; -import javax.xml.validation.SchemaFactory; -import javax.xml.validation.Validator; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; @@ -25,6 +20,12 @@ import java.util.ArrayList; import java.util.List; +import javax.xml.XMLConstants; +import javax.xml.transform.stream.StreamSource; +import javax.xml.validation.Schema; +import javax.xml.validation.SchemaFactory; +import javax.xml.validation.Validator; + /** * Validates an XML stream against a specified schema. */ @@ -39,14 +40,13 @@ public XmlValidator(String xsdName) { } public void validate(String xml) throws Exception { - try(InputStream stream = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8))) { + try (InputStream stream = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8))) { validate(stream); } } public void validate(InputStream xml) throws Exception { - try (InputStream xsdStream = loadSchema(xsdName); - ResourceResolver resolver = new ResourceResolver()) { + try (InputStream xsdStream = loadSchema(xsdName); ResourceResolver resolver = new ResourceResolver()) { schemaFactory.setResourceResolver(resolver); Schema schema = schemaFactory.newSchema(new StreamSource(xsdStream)); Validator validator = schema.newValidator(); diff --git a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/action/DeleteSamlServiceProviderRequestTests.java b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/action/DeleteSamlServiceProviderRequestTests.java index 8ed4237587105..e352f75451934 100644 --- a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/action/DeleteSamlServiceProviderRequestTests.java +++ b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/action/DeleteSamlServiceProviderRequestTests.java @@ -22,13 +22,22 @@ public class DeleteSamlServiceProviderRequestTests extends IdpSamlTestCase { public void testSerialization() throws IOException { - final DeleteSamlServiceProviderRequest request = new DeleteSamlServiceProviderRequest(randomAlphaOfLengthBetween(1, 100), - randomFrom(WriteRequest.RefreshPolicy.values())); + final DeleteSamlServiceProviderRequest request = new DeleteSamlServiceProviderRequest( + randomAlphaOfLengthBetween(1, 100), + randomFrom(WriteRequest.RefreshPolicy.values()) + ); final Version version = VersionUtils.randomVersionBetween(random(), Version.V_7_7_0, Version.CURRENT); - final DeleteSamlServiceProviderRequest read = copyWriteable(request, new NamedWriteableRegistry(List.of()), - DeleteSamlServiceProviderRequest::new, version); - MatcherAssert.assertThat("Serialized request with version [" + version + "] does not match original object", - read, equalTo(request)); + final DeleteSamlServiceProviderRequest read = copyWriteable( + request, + new NamedWriteableRegistry(List.of()), + DeleteSamlServiceProviderRequest::new, + version + ); + MatcherAssert.assertThat( + "Serialized request with version [" + version + "] does not match original object", + read, + equalTo(request) + ); } } diff --git a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/action/PutSamlServiceProviderRequestTests.java b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/action/PutSamlServiceProviderRequestTests.java index 2b8c797658f2a..ac3503fce59b2 100644 --- a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/action/PutSamlServiceProviderRequestTests.java +++ b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/action/PutSamlServiceProviderRequestTests.java @@ -13,12 +13,12 @@ import org.elasticsearch.action.support.WriteRequest.RefreshPolicy; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.TestMatchers; import org.elasticsearch.test.VersionUtils; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.idp.saml.sp.SamlServiceProviderDocument; import org.elasticsearch.xpack.idp.saml.sp.SamlServiceProviderTestUtils; import org.hamcrest.MatcherAssert; @@ -85,10 +85,17 @@ public void testSerialization() throws IOException { final SamlServiceProviderDocument doc = SamlServiceProviderTestUtils.randomDocument(); final PutSamlServiceProviderRequest request = new PutSamlServiceProviderRequest(doc, RefreshPolicy.NONE); final Version version = VersionUtils.randomVersionBetween(random(), Version.V_7_7_0, Version.CURRENT); - final PutSamlServiceProviderRequest read = copyWriteable(request, new NamedWriteableRegistry(List.of()), - PutSamlServiceProviderRequest::new, version); - MatcherAssert.assertThat("Serialized request with version [" + version + "] does not match original object", - read, equalTo(request)); + final PutSamlServiceProviderRequest read = copyWriteable( + request, + new NamedWriteableRegistry(List.of()), + PutSamlServiceProviderRequest::new, + version + ); + MatcherAssert.assertThat( + "Serialized request with version [" + version + "] does not match original object", + read, + equalTo(request) + ); } public void testParseRequestBodySuccessfully() throws Exception { @@ -96,16 +103,23 @@ public void testParseRequestBodySuccessfully() throws Exception { fields.put("name", randomAlphaOfLengthBetween(3, 30)); fields.put("acs", "https://www." + randomAlphaOfLengthBetween(3, 30) + ".fake/saml/acs"); fields.put("enabled", randomBoolean()); - fields.put("attributes", Map.of( - "principal", "urn:oid:0.1." + randomLongBetween(1, 1000), - "email", "urn:oid:0.2." + randomLongBetween(1001, 2000), - "name", "urn:oid:0.3." + randomLongBetween(2001, 3000), - "roles", "urn:oid:0.4." + randomLongBetween(3001, 4000) - )); - fields.put("privileges", Map.of( - "resource", "ece:deployment:" + randomLongBetween(1_000_000, 999_999_999), - "roles", List.of("role:(.*)") - )); + fields.put( + "attributes", + Map.of( + "principal", + "urn:oid:0.1." + randomLongBetween(1, 1000), + "email", + "urn:oid:0.2." + randomLongBetween(1001, 2000), + "name", + "urn:oid:0.3." + randomLongBetween(2001, 3000), + "roles", + "urn:oid:0.4." + randomLongBetween(3001, 4000) + ) + ); + fields.put( + "privileges", + Map.of("resource", "ece:deployment:" + randomLongBetween(1_000_000, 999_999_999), "roles", List.of("role:(.*)")) + ); fields.put("certificates", Map.of()); final String entityId = "https://www." + randomAlphaOfLengthBetween(5, 12) + ".app/"; final PutSamlServiceProviderRequest request = parseRequest(entityId, fields); diff --git a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/action/TransportPutSamlServiceProviderActionTests.java b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/action/TransportPutSamlServiceProviderActionTests.java index c42beb21a6503..ae031a050d3d5 100644 --- a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/action/TransportPutSamlServiceProviderActionTests.java +++ b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/action/TransportPutSamlServiceProviderActionTests.java @@ -60,8 +60,7 @@ public void setupMocks() { now = Instant.ofEpochMilli(System.currentTimeMillis() + randomLongBetween(-500_000, 500_000)); final Clock clock = Clock.fixed(now, randomZone()); - action = new TransportPutSamlServiceProviderAction( - mock(TransportService.class), mock(ActionFilters.class), index, idp, clock); + action = new TransportPutSamlServiceProviderAction(mock(TransportService.class), mock(ActionFilters.class), index, idp, clock); } public void testRegisterNewServiceProvider() throws Exception { @@ -140,7 +139,12 @@ public AtomicReference mockWriteResponse(SamlServiceProviderDo final DocWriteResponse docWriteResponse = new IndexResponse( new ShardId(randomAlphaOfLengthBetween(4, 12), randomAlphaOfLength(24), randomIntBetween(1, 10)), - doc.docId, randomLong(), randomLong(), randomLong(), created); + doc.docId, + randomLong(), + randomLong(), + randomLong(), + created + ); writeResponse.set(docWriteResponse); @SuppressWarnings("unchecked") @@ -148,17 +152,25 @@ public AtomicReference mockWriteResponse(SamlServiceProviderDo listener.onResponse(docWriteResponse); return null; - }).when(index).writeDocument(any(SamlServiceProviderDocument.class), any(DocWriteRequest.OpType.class), - any(WriteRequest.RefreshPolicy.class), any()); + }).when(index) + .writeDocument( + any(SamlServiceProviderDocument.class), + any(DocWriteRequest.OpType.class), + any(WriteRequest.RefreshPolicy.class), + any() + ); return writeResponse; } public void mockExistingDocuments(String expectedEntityId, Set documents) { final Set documentSuppliers = documents.stream() - .map(doc -> new SamlServiceProviderIndex.DocumentSupplier( - new DocumentVersion(randomAlphaOfLength(24), randomLong(), randomLong()), - () -> doc)) + .map( + doc -> new SamlServiceProviderIndex.DocumentSupplier( + new DocumentVersion(randomAlphaOfLength(24), randomLong(), randomLong()), + () -> doc + ) + ) .collect(Collectors.toUnmodifiableSet()); doAnswer(inv -> { final Object[] args = inv.getArguments(); diff --git a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/action/TransportSamlInitiateSingleSignOnActionTests.java b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/action/TransportSamlInitiateSingleSignOnActionTests.java index 7a001e1e9e01b..1cfd4be509fb2 100644 --- a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/action/TransportSamlInitiateSingleSignOnActionTests.java +++ b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/action/TransportSamlInitiateSingleSignOnActionTests.java @@ -132,28 +132,45 @@ private TransportSamlInitiateSingleSignOnAction setupTransportAction(boolean wit final ThreadContext threadContext = new ThreadContext(settings); final ThreadPool threadPool = mock(ThreadPool.class); final SecurityContext securityContext = new SecurityContext(settings, threadContext); - final TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet()); + final TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); final ActionFilters actionFilters = mock(ActionFilters.class); final Environment env = TestEnvironment.newEnvironment(settings); when(threadPool.getThreadContext()).thenReturn(threadContext); - new Authentication(new User("saml_service_account", "saml_service_role"), + new Authentication( + new User("saml_service_account", "saml_service_role"), new Authentication.RealmRef("default_native", "native", "node_name"), - new Authentication.RealmRef("default_native", "native", "node_name")) - .writeToContext(threadContext); + new Authentication.RealmRef("default_native", "native", "node_name") + ).writeToContext(threadContext); if (withSecondaryAuth) { - new SecondaryAuthentication(securityContext, + new SecondaryAuthentication( + securityContext, new Authentication( - new User("saml_enduser", new String[]{"saml_enduser_role"}, "Saml Enduser", "samlenduser@elastic.co", - new HashMap<>(), true), + new User( + "saml_enduser", + new String[] { "saml_enduser_role" }, + "Saml Enduser", + "samlenduser@elastic.co", + new HashMap<>(), + true + ), new Authentication.RealmRef("_es_api_key", "_es_api_key", "node_name"), - new Authentication.RealmRef("_es_api_key", "_es_api_key", "node_name"))) - .writeToContext(threadContext); + new Authentication.RealmRef("_es_api_key", "_es_api_key", "node_name") + ) + ).writeToContext(threadContext); } final SamlServiceProviderResolver serviceResolver = Mockito.mock(SamlServiceProviderResolver.class); final WildcardServiceProviderResolver wildcardResolver = Mockito.mock(WildcardServiceProviderResolver.class); - final CloudServiceProvider serviceProvider = new CloudServiceProvider("https://sp.some.org", + final CloudServiceProvider serviceProvider = new CloudServiceProvider( + "https://sp.some.org", "test sp", true, new URL("https://sp.some.org/api/security/v1/saml"), @@ -164,15 +181,17 @@ private TransportSamlInitiateSingleSignOnAction setupTransportAction(boolean wit "https://saml.elasticsearch.org/attributes/principal", "https://saml.elasticsearch.org/attributes/name", "https://saml.elasticsearch.org/attributes/email", - "https://saml.elasticsearch.org/attributes/roles"), - null, false, false); + "https://saml.elasticsearch.org/attributes/roles" + ), + null, + false, + false + ); mockRegisteredServiceProvider(serviceResolver, "https://sp.some.org", serviceProvider); mockRegisteredServiceProvider(serviceResolver, "https://sp2.other.org", null); - final ServiceProviderDefaults defaults = new ServiceProviderDefaults( - "elastic-cloud", TRANSIENT, Duration.ofMinutes(15)); + final ServiceProviderDefaults defaults = new ServiceProviderDefaults("elastic-cloud", TRANSIENT, Duration.ofMinutes(15)); final X509Credential signingCredential = readCredentials("RSA", randomFrom(1024, 2048, 4096)); - final SamlIdentityProvider idp = SamlIdentityProvider - .builder(serviceResolver, wildcardResolver) + final SamlIdentityProvider idp = SamlIdentityProvider.builder(serviceResolver, wildcardResolver) .fromSettings(env) .signingCredential(signingCredential) .serviceProviderDefaults(defaults) @@ -183,19 +202,38 @@ private TransportSamlInitiateSingleSignOnAction setupTransportAction(boolean wit assertThat(inv.getArguments(), arrayWithSize(2)); ActionListener listener = inv.getArgument(1); final UserPrivilegeResolver.UserPrivileges privileges = new UserPrivilegeResolver.UserPrivileges( - "saml_enduser", true, Set.of(generateRandomStringArray(5, 8, false, false)) + "saml_enduser", + true, + Set.of(generateRandomStringArray(5, 8, false, false)) ); listener.onResponse(privileges); return null; }).when(privilegeResolver).resolve(nullable(ServiceProviderPrivileges.class), any()); - return new TransportSamlInitiateSingleSignOnAction(transportService, actionFilters, securityContext, - idp, factory, privilegeResolver); + return new TransportSamlInitiateSingleSignOnAction( + transportService, + actionFilters, + securityContext, + idp, + factory, + privilegeResolver + ); } private void assertContainsAttributeWithValue(String message, String attribute, String value) { - assertThat(message, containsString("" + value + "")); + assertThat( + message, + containsString( + "" + + value + + "" + ) + ); } } diff --git a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/privileges/UserPrivilegeResolverTests.java b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/privileges/UserPrivilegeResolverTests.java index d694c1134ca38..54c9d6bf062f0 100644 --- a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/privileges/UserPrivilegeResolverTests.java +++ b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/privileges/UserPrivilegeResolverTests.java @@ -11,10 +11,10 @@ import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.support.PlainActionFuture; import org.elasticsearch.client.Client; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.hash.MessageDigests; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.Tuple; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.security.SecurityContext; import org.elasticsearch.xpack.core.security.action.user.HasPrivilegesAction; @@ -70,8 +70,12 @@ public void testResolveZeroAccess() throws Exception { setupUser(username); setupHasPrivileges(username, app); final PlainActionFuture future = new PlainActionFuture<>(); - final Function> roleMapping = - Map.of("role:cluster:view", Set.of("viewer"), "role:cluster:admin", Set.of("admin"))::get; + final Function> roleMapping = Map.of( + "role:cluster:view", + Set.of("viewer"), + "role:cluster:admin", + Set.of("admin") + )::get; resolver.resolve(service(app, "cluster:" + randomLong(), roleMapping), future); final UserPrivilegeResolver.UserPrivileges privileges = future.get(); assertThat(privileges.principal, equalTo(username)); @@ -127,7 +131,9 @@ public void testResolveSsoWithMultipleRoles() throws Exception { final String monitorAction = "role:cluster:monitor"; setupUser(username); - setupHasPrivileges(username, app, + setupHasPrivileges( + username, + app, access(resource, viewerAction, false), access(resource, adminAction, false), access(resource, operatorAction, true), @@ -161,8 +167,11 @@ private ServiceProviderPrivileges service(String appName, String resource, Funct @SafeVarargs @SuppressWarnings("unchecked") - private HasPrivilegesResponse setupHasPrivileges(String username, String appName, - Tuple>... resourceActionAccess) { + private HasPrivilegesResponse setupHasPrivileges( + String username, + String appName, + Tuple>... resourceActionAccess + ) { final boolean isCompleteMatch = randomBoolean(); final Map> resourcePrivilegeMap = new HashMap<>(resourceActionAccess.length); for (Tuple> t : resourceActionAccess) { @@ -171,7 +180,8 @@ private HasPrivilegesResponse setupHasPrivileges(String username, String appName final Boolean access = t.v2().v2(); resourcePrivilegeMap.computeIfAbsent(resource, ignore -> new HashMap<>()).put(action, access); } - final Collection privileges = resourcePrivilegeMap.entrySet().stream() + final Collection privileges = resourcePrivilegeMap.entrySet() + .stream() .map(e -> ResourcePrivileges.builder(e.getKey()).addPrivileges(e.getValue()).build()) .collect(Collectors.toList()); final Map> appPrivs = Map.of(appName, privileges); diff --git a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/authn/FailedAuthenticationResponseBuilderTests.java b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/authn/FailedAuthenticationResponseBuilderTests.java index 0b8b2a748a053..2d3ba0fc8f539 100644 --- a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/authn/FailedAuthenticationResponseBuilderTests.java +++ b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/authn/FailedAuthenticationResponseBuilderTests.java @@ -40,11 +40,9 @@ public void setupSaml() throws Exception { public void testSimpleErrorResponseIsValid() throws Exception { final Clock clock = Clock.systemUTC(); final FailedAuthenticationResponseMessageBuilder builder = new FailedAuthenticationResponseMessageBuilder(samlFactory, clock, idp); - final Response response = builder - .setAcsUrl("https://" + randomAlphaOfLengthBetween(4, 8) + "." + randomAlphaOfLengthBetween(4, 8) + "/saml/acs") - .setPrimaryStatusCode(StatusCode.REQUESTER) - .setInResponseTo(randomAlphaOfLength(12)) - .build(); + final Response response = builder.setAcsUrl( + "https://" + randomAlphaOfLengthBetween(4, 8) + "." + randomAlphaOfLengthBetween(4, 8) + "/saml/acs" + ).setPrimaryStatusCode(StatusCode.REQUESTER).setInResponseTo(randomAlphaOfLength(12)).build(); final String xml = super.toString(response); validator.validate(xml); } @@ -52,8 +50,9 @@ public void testSimpleErrorResponseIsValid() throws Exception { public void testErrorResponseWithCodeIsValid() throws Exception { final Clock clock = Clock.systemUTC(); final FailedAuthenticationResponseMessageBuilder builder = new FailedAuthenticationResponseMessageBuilder(samlFactory, clock, idp); - final Response response = builder - .setAcsUrl("https://" + randomAlphaOfLengthBetween(4, 8) + "." + randomAlphaOfLengthBetween(4, 8) + "/saml/acs") + final Response response = builder.setAcsUrl( + "https://" + randomAlphaOfLengthBetween(4, 8) + "." + randomAlphaOfLengthBetween(4, 8) + "/saml/acs" + ) .setPrimaryStatusCode(StatusCode.REQUESTER) .setInResponseTo(randomAlphaOfLength(12)) .setSecondaryStatusCode(StatusCode.INVALID_NAMEID_POLICY) diff --git a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/authn/SamlAuthnRequestValidatorTests.java b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/authn/SamlAuthnRequestValidatorTests.java index deaea64b9e425..9e71e7e9393d1 100644 --- a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/authn/SamlAuthnRequestValidatorTests.java +++ b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/authn/SamlAuthnRequestValidatorTests.java @@ -76,8 +76,12 @@ public void setupValidator() throws Exception { public void testValidAuthnRequest() throws Exception { final String relayState = randomAlphaOfLength(6); - final AuthnRequest authnRequest = buildAuthnRequest("https://sp1.kibana.org", new URL("https://sp1.kibana.org/saml/acs"), - idp.getSingleSignOnEndpoint(SAML2_REDIRECT_BINDING_URI), TRANSIENT); + final AuthnRequest authnRequest = buildAuthnRequest( + "https://sp1.kibana.org", + new URL("https://sp1.kibana.org/saml/acs"), + idp.getSingleSignOnEndpoint(SAML2_REDIRECT_BINDING_URI), + TRANSIENT + ); PlainActionFuture future = new PlainActionFuture<>(); validator.processQueryString(getQueryString(authnRequest, relayState), future); SamlValidateAuthnRequestResponse response = future.actionGet(); @@ -90,11 +94,14 @@ public void testValidAuthnRequest() throws Exception { public void testValidSignedAuthnRequest() throws Exception { final String relayState = randomAlphaOfLength(6); - final AuthnRequest authnRequest = buildAuthnRequest("https://sp2.kibana.org", new URL("https://sp2.kibana.org/saml/acs"), - idp.getSingleSignOnEndpoint(SAML2_REDIRECT_BINDING_URI), PERSISTENT); + final AuthnRequest authnRequest = buildAuthnRequest( + "https://sp2.kibana.org", + new URL("https://sp2.kibana.org/saml/acs"), + idp.getSingleSignOnEndpoint(SAML2_REDIRECT_BINDING_URI), + PERSISTENT + ); PlainActionFuture future = new PlainActionFuture<>(); - validator.processQueryString(getQueryString(authnRequest, relayState, true, - readCredentials("RSA", 4096)), future); + validator.processQueryString(getQueryString(authnRequest, relayState, true, readCredentials("RSA", 4096)), future); SamlValidateAuthnRequestResponse response = future.actionGet(); assertThat(response.isForceAuthn(), equalTo(false)); assertThat(response.getSpEntityId(), equalTo("https://sp2.kibana.org")); @@ -104,11 +111,14 @@ public void testValidSignedAuthnRequest() throws Exception { } public void testValidSignedAuthnRequestWithoutRelayState() throws Exception { - final AuthnRequest authnRequest = buildAuthnRequest("https://sp2.kibana.org", new URL("https://sp2.kibana.org/saml/acs"), - idp.getSingleSignOnEndpoint(SAML2_REDIRECT_BINDING_URI), PERSISTENT); + final AuthnRequest authnRequest = buildAuthnRequest( + "https://sp2.kibana.org", + new URL("https://sp2.kibana.org/saml/acs"), + idp.getSingleSignOnEndpoint(SAML2_REDIRECT_BINDING_URI), + PERSISTENT + ); PlainActionFuture future = new PlainActionFuture<>(); - validator.processQueryString(getQueryString(authnRequest, null, true, - readCredentials("RSA", 4096)), future); + validator.processQueryString(getQueryString(authnRequest, null, true, readCredentials("RSA", 4096)), future); SamlValidateAuthnRequestResponse response = future.actionGet(); assertThat(response.isForceAuthn(), equalTo(false)); assertThat(response.getSpEntityId(), equalTo("https://sp2.kibana.org")); @@ -119,8 +129,12 @@ public void testValidSignedAuthnRequestWithoutRelayState() throws Exception { public void testValidSignedAuthnRequestWhenServiceProviderShouldNotSign() throws Exception { final String relayState = randomAlphaOfLength(6); - final AuthnRequest authnRequest = buildAuthnRequest("https://sp1.kibana.org", new URL("https://sp1.kibana.org/saml/acs"), - idp.getSingleSignOnEndpoint(SAML2_REDIRECT_BINDING_URI), TRANSIENT); + final AuthnRequest authnRequest = buildAuthnRequest( + "https://sp1.kibana.org", + new URL("https://sp1.kibana.org/saml/acs"), + idp.getSingleSignOnEndpoint(SAML2_REDIRECT_BINDING_URI), + TRANSIENT + ); PlainActionFuture future = new PlainActionFuture<>(); validator.processQueryString(getQueryString(authnRequest, relayState, true, readCredentials("RSA", 4096)), future); SamlValidateAuthnRequestResponse response = future.actionGet(); @@ -133,65 +147,84 @@ public void testValidSignedAuthnRequestWhenServiceProviderShouldNotSign() throws public void testValidUnSignedAuthnRequestWhenServiceProviderShouldSign() throws Exception { final String relayState = randomAlphaOfLength(6); - final AuthnRequest authnRequest = buildAuthnRequest("https://sp2.kibana.org", new URL("https://sp2.kibana.org/saml/acs"), - idp.getSingleSignOnEndpoint(SAML2_REDIRECT_BINDING_URI), TRANSIENT); + final AuthnRequest authnRequest = buildAuthnRequest( + "https://sp2.kibana.org", + new URL("https://sp2.kibana.org/saml/acs"), + idp.getSingleSignOnEndpoint(SAML2_REDIRECT_BINDING_URI), + TRANSIENT + ); PlainActionFuture future = new PlainActionFuture<>(); validator.processQueryString(getQueryString(authnRequest, relayState), future); - ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, - future::actionGet); + ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, future::actionGet); assertThat(e.getMessage(), containsString("must sign authentication requests but no signature was found")); } public void testSignedAuthnRequestWithWrongKey() throws Exception { final String relayState = randomAlphaOfLength(6); - final AuthnRequest authnRequest = buildAuthnRequest("https://sp2.kibana.org", new URL("https://sp2.kibana.org/saml/acs"), - idp.getSingleSignOnEndpoint(SAML2_REDIRECT_BINDING_URI), TRANSIENT); + final AuthnRequest authnRequest = buildAuthnRequest( + "https://sp2.kibana.org", + new URL("https://sp2.kibana.org/saml/acs"), + idp.getSingleSignOnEndpoint(SAML2_REDIRECT_BINDING_URI), + TRANSIENT + ); PlainActionFuture future = new PlainActionFuture<>(); validator.processQueryString(getQueryString(authnRequest, relayState, true, readCredentials("RSA2", 4096)), future); - ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, - future::actionGet); + ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, future::actionGet); assertThat(e.getMessage(), containsString("Unable to validate signature of authentication request")); } public void testSignedAuthnRequestWithWrongSizeKey() throws Exception { final String relayState = randomAlphaOfLength(6); - final AuthnRequest authnRequest = buildAuthnRequest("https://sp2.kibana.org", new URL("https://sp2.kibana.org/saml/acs"), - idp.getSingleSignOnEndpoint(SAML2_REDIRECT_BINDING_URI), TRANSIENT); + final AuthnRequest authnRequest = buildAuthnRequest( + "https://sp2.kibana.org", + new URL("https://sp2.kibana.org/saml/acs"), + idp.getSingleSignOnEndpoint(SAML2_REDIRECT_BINDING_URI), + TRANSIENT + ); PlainActionFuture future = new PlainActionFuture<>(); validator.processQueryString(getQueryString(authnRequest, relayState, true, readCredentials("RSA", 2048)), future); - ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, - future::actionGet); + ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, future::actionGet); assertThat(e.getMessage(), containsString("Unable to validate signature of authentication request")); } public void testWrongDestination() throws Exception { final String relayState = randomAlphaOfLength(6); - final AuthnRequest authnRequest = buildAuthnRequest("https://sp1.kibana.org", new URL("https://sp1.kibana.org/saml/acs"), - new URL("https://wrong.destination.org"), TRANSIENT); + final AuthnRequest authnRequest = buildAuthnRequest( + "https://sp1.kibana.org", + new URL("https://sp1.kibana.org/saml/acs"), + new URL("https://wrong.destination.org"), + TRANSIENT + ); PlainActionFuture future = new PlainActionFuture<>(); validator.processQueryString(getQueryString(authnRequest, relayState), future); - ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, - future::actionGet); + ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, future::actionGet); assertThat(e.getMessage(), containsString("but the SSO endpoint of this Identity Provider is")); assertThat(e.getMessage(), containsString("wrong.destination.org")); } public void testUnregisteredAcsForSp() throws Exception { final String relayState = randomAlphaOfLength(6); - final AuthnRequest authnRequest = buildAuthnRequest("https://sp1.kibana.org", new URL("https://malicious.kibana.org/saml/acs"), - idp.getSingleSignOnEndpoint(SAML2_REDIRECT_BINDING_URI), TRANSIENT); + final AuthnRequest authnRequest = buildAuthnRequest( + "https://sp1.kibana.org", + new URL("https://malicious.kibana.org/saml/acs"), + idp.getSingleSignOnEndpoint(SAML2_REDIRECT_BINDING_URI), + TRANSIENT + ); PlainActionFuture future = new PlainActionFuture<>(); validator.processQueryString(getQueryString(authnRequest, relayState), future); - ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, - future::actionGet); + ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, future::actionGet); assertThat(e.getMessage(), containsString("The registered ACS URL for this Service Provider is")); assertThat(e.getMessage(), containsString("https://malicious.kibana.org/saml/acs")); } - public void testUnregisteredSp()throws Exception { + public void testUnregisteredSp() throws Exception { final String relayState = randomAlphaOfLength(6); - final AuthnRequest authnRequest = buildAuthnRequest("https://unknown.kibana.org", new URL("https://unknown.kibana.org/saml/acs"), - idp.getSingleSignOnEndpoint(SAML2_REDIRECT_BINDING_URI), TRANSIENT); + final AuthnRequest authnRequest = buildAuthnRequest( + "https://unknown.kibana.org", + new URL("https://unknown.kibana.org/saml/acs"), + idp.getSingleSignOnEndpoint(SAML2_REDIRECT_BINDING_URI), + TRANSIENT + ); mockRegisteredServiceProvider(idp, "https://unknown.kibana.org", null); PlainActionFuture future = new PlainActionFuture<>(); validator.processQueryString(getQueryString(authnRequest, relayState), future); @@ -200,10 +233,14 @@ public void testUnregisteredSp()throws Exception { assertThat(e.getMessage(), containsString("https://unknown.kibana.org")); } - public void testAuthnRequestWithoutAcsUrl() throws Exception{ + public void testAuthnRequestWithoutAcsUrl() throws Exception { final String relayState = randomAlphaOfLength(6); - final AuthnRequest authnRequest = buildAuthnRequest("https://sp1.kibana.org", new URL("https://sp1.kibana.org/saml/acs"), - idp.getSingleSignOnEndpoint(SAML2_REDIRECT_BINDING_URI), TRANSIENT); + final AuthnRequest authnRequest = buildAuthnRequest( + "https://sp1.kibana.org", + new URL("https://sp1.kibana.org/saml/acs"), + idp.getSingleSignOnEndpoint(SAML2_REDIRECT_BINDING_URI), + TRANSIENT + ); // remove ACS authnRequest.setAssertionConsumerServiceURL(null); final boolean containsIndex = randomBoolean(); @@ -212,8 +249,7 @@ public void testAuthnRequestWithoutAcsUrl() throws Exception{ } PlainActionFuture future = new PlainActionFuture<>(); validator.processQueryString(getQueryString(authnRequest, relayState), future); - ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, - future::actionGet); + ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, future::actionGet); assertThat(e.getMessage(), containsString("SAML authentication does not contain an AssertionConsumerService URL")); if (containsIndex) { assertThat(e.getMessage(), containsString("It contains an Assertion Consumer Service Index ")); @@ -222,25 +258,31 @@ public void testAuthnRequestWithoutAcsUrl() throws Exception{ public void testAuthnRequestWithoutIssuer() throws Exception { final String relayState = randomAlphaOfLength(6); - final AuthnRequest authnRequest = buildAuthnRequest("https://sp1.kibana.org", new URL("https://sp1.kibana.org/saml/acs"), - idp.getSingleSignOnEndpoint(SAML2_REDIRECT_BINDING_URI), TRANSIENT); + final AuthnRequest authnRequest = buildAuthnRequest( + "https://sp1.kibana.org", + new URL("https://sp1.kibana.org/saml/acs"), + idp.getSingleSignOnEndpoint(SAML2_REDIRECT_BINDING_URI), + TRANSIENT + ); // remove issuer authnRequest.setIssuer(null); PlainActionFuture future = new PlainActionFuture<>(); validator.processQueryString(getQueryString(authnRequest, relayState), future); - ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, - future::actionGet); + ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, future::actionGet); assertThat(e.getMessage(), containsString("SAML authentication request has no issuer")); } public void testInvalidNameIDPolicy() throws Exception { final String relayState = randomAlphaOfLength(6); - final AuthnRequest authnRequest = buildAuthnRequest("https://sp1.kibana.org", new URL("https://sp1.kibana.org/saml/acs"), - idp.getSingleSignOnEndpoint(SAML2_REDIRECT_BINDING_URI), PERSISTENT); + final AuthnRequest authnRequest = buildAuthnRequest( + "https://sp1.kibana.org", + new URL("https://sp1.kibana.org/saml/acs"), + idp.getSingleSignOnEndpoint(SAML2_REDIRECT_BINDING_URI), + PERSISTENT + ); PlainActionFuture future = new PlainActionFuture<>(); validator.processQueryString(getQueryString(authnRequest, relayState), future); - ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, - future::actionGet); + ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, future::actionGet); assertThat(e.getMessage(), containsString("doesn't match the allowed NameID format")); } @@ -290,11 +332,12 @@ private String urlEncode(String param) throws UnsupportedEncodingException { return URLEncoder.encode(param, StandardCharsets.UTF_8.name()); } - private String deflateAndBase64Encode(SAMLObject message) - throws Exception { + private String deflateAndBase64Encode(SAMLObject message) throws Exception { Deflater deflater = new Deflater(Deflater.DEFLATED, true); - try (ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); - DeflaterOutputStream deflaterStream = new DeflaterOutputStream(bytesOut, deflater)) { + try ( + ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); + DeflaterOutputStream deflaterStream = new DeflaterOutputStream(bytesOut, deflater) + ) { String messageStr = samlFactory.toString(XMLObjectSupport.marshall(message), false); deflaterStream.write(messageStr.getBytes(StandardCharsets.UTF_8)); deflaterStream.finish(); diff --git a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/authn/SuccessfulAuthenticationResponseMessageBuilderTests.java b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/authn/SuccessfulAuthenticationResponseMessageBuilderTests.java index c6ae5995cf672..bd433e828436b 100644 --- a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/authn/SuccessfulAuthenticationResponseMessageBuilderTests.java +++ b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/authn/SuccessfulAuthenticationResponseMessageBuilderTests.java @@ -42,8 +42,7 @@ public void setupSaml() throws Exception { idp = mock(SamlIdentityProvider.class); when(idp.getEntityId()).thenReturn("https://cloud.elastic.co/saml/idp"); when(idp.getSigningCredential()).thenReturn(readCredentials("RSA", 2048)); - when(idp.getServiceProviderDefaults()) - .thenReturn(new ServiceProviderDefaults("elastic-cloud", TRANSIENT, Duration.ofMinutes(5))); + when(idp.getServiceProviderDefaults()).thenReturn(new ServiceProviderDefaults("elastic-cloud", TRANSIENT, Duration.ofMinutes(5))); } public void testSignedResponseIsValidAgainstXmlSchema() throws Exception { @@ -53,7 +52,7 @@ public void testSignedResponseIsValidAgainstXmlSchema() throws Exception { validator.validate(xml); } - private Response buildResponse() throws Exception{ + private Response buildResponse() throws Exception { final Clock clock = Clock.systemUTC(); final SamlServiceProvider sp = mock(SamlServiceProvider.class); @@ -71,10 +70,12 @@ private Response buildResponse() throws Exception{ when(user.getName()).thenReturn(randomAlphaOfLength(6) + " " + randomAlphaOfLength(8)); when(user.getServiceProvider()).thenReturn(sp); - final SuccessfulAuthenticationResponseMessageBuilder builder = - new SuccessfulAuthenticationResponseMessageBuilder(samlFactory, clock, idp); + final SuccessfulAuthenticationResponseMessageBuilder builder = new SuccessfulAuthenticationResponseMessageBuilder( + samlFactory, + clock, + idp + ); return builder.build(user, null); } - } diff --git a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/idp/SamlIdentityProviderBuilderTests.java b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/idp/SamlIdentityProviderBuilderTests.java index 4db340d26673f..4282111874032 100644 --- a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/idp/SamlIdentityProviderBuilderTests.java +++ b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/idp/SamlIdentityProviderBuilderTests.java @@ -101,8 +101,10 @@ public void testAllSettings() throws Exception { assertThat(idp.getSingleLogoutEndpoint(SAML2_POST_BINDING_URI).toString(), equalTo("https://idp.org/slo/post")); assertThat(idp.getAllowedNameIdFormats(), hasSize(1)); assertThat(idp.getAllowedNameIdFormats(), Matchers.contains(TRANSIENT)); - assertThat(idp.getOrganization(), equalTo(new SamlIdentityProvider.OrganizationInfo("organization_name", - "organization_display_name", "https://idp.org"))); + assertThat( + idp.getOrganization(), + equalTo(new SamlIdentityProvider.OrganizationInfo("organization_name", "organization_display_name", "https://idp.org")) + ); assertThat(idp.getServiceProviderDefaults().applicationName, equalTo("my_super_idp")); assertThat(idp.getServiceProviderDefaults().nameIdFormat, equalTo(PERSISTENT)); assertThat(idp.getServiceProviderDefaults().authenticationExpiry, equalTo(Duration.ofMinutes(2))); @@ -134,11 +136,13 @@ public void testMissingCredentials() { randomFrom(TRANSIENT, PERSISTENT), java.time.Duration.ofMinutes(randomIntBetween(2, 90)) ); - IllegalArgumentException e = LuceneTestCase.expectThrows(IllegalArgumentException.class, + IllegalArgumentException e = LuceneTestCase.expectThrows( + IllegalArgumentException.class, () -> SamlIdentityProvider.builder(serviceResolver, wildcardResolver) .fromSettings(env) .serviceProviderDefaults(defaults) - .build()); + .build() + ); assertThat(e, instanceOf(ValidationException.class)); assertThat(e.getMessage(), containsString("Signing credential must be specified")); } @@ -217,8 +221,13 @@ public void testConfigurationWithForbiddenAllowedNameIdFormats() throws Exceptio final SamlServiceProviderResolver serviceResolver = Mockito.mock(SamlServiceProviderResolver.class); final WildcardServiceProviderResolver wildcardResolver = Mockito.mock(WildcardServiceProviderResolver.class); final ServiceProviderDefaults defaults = ServiceProviderDefaults.forSettings(settings); - IllegalArgumentException e = LuceneTestCase.expectThrows(IllegalArgumentException.class, () -> - SamlIdentityProvider.builder(serviceResolver, wildcardResolver).fromSettings(env).serviceProviderDefaults(defaults).build()); + IllegalArgumentException e = LuceneTestCase.expectThrows( + IllegalArgumentException.class, + () -> SamlIdentityProvider.builder(serviceResolver, wildcardResolver) + .fromSettings(env) + .serviceProviderDefaults(defaults) + .build() + ); assertThat(e.getMessage(), containsString("are not valid NameID formats. Allowed values are")); assertThat(e.getMessage(), containsString(PERSISTENT)); } @@ -232,8 +241,10 @@ public void testInvalidSsoEndpoint() { final Environment env = TestEnvironment.newEnvironment(settings); final SamlServiceProviderResolver serviceResolver = Mockito.mock(SamlServiceProviderResolver.class); final WildcardServiceProviderResolver wildcardResolver = Mockito.mock(WildcardServiceProviderResolver.class); - IllegalArgumentException e = LuceneTestCase.expectThrows(IllegalArgumentException.class, - () -> SamlIdentityProvider.builder(serviceResolver, wildcardResolver).fromSettings(env).build()); + IllegalArgumentException e = LuceneTestCase.expectThrows( + IllegalArgumentException.class, + () -> SamlIdentityProvider.builder(serviceResolver, wildcardResolver).fromSettings(env).build() + ); assertThat(e.getMessage(), containsString(IDP_SSO_REDIRECT_ENDPOINT.getKey())); assertThat(e.getMessage(), containsString("Not a valid URL")); } @@ -249,8 +260,10 @@ public void testMissingSsoRedirectEndpoint() { final Environment env = TestEnvironment.newEnvironment(settings); final SamlServiceProviderResolver serviceResolver = Mockito.mock(SamlServiceProviderResolver.class); final WildcardServiceProviderResolver wildcardResolver = Mockito.mock(WildcardServiceProviderResolver.class); - IllegalArgumentException e = LuceneTestCase.expectThrows(IllegalArgumentException.class, - () -> SamlIdentityProvider.builder(serviceResolver, wildcardResolver).fromSettings(env).build()); + IllegalArgumentException e = LuceneTestCase.expectThrows( + IllegalArgumentException.class, + () -> SamlIdentityProvider.builder(serviceResolver, wildcardResolver).fromSettings(env).build() + ); assertThat(e.getMessage(), containsString(IDP_SSO_REDIRECT_ENDPOINT.getKey())); assertThat(e.getMessage(), containsString("is required")); } @@ -267,8 +280,10 @@ public void testMalformedOrganizationUrl() { final Environment env = TestEnvironment.newEnvironment(settings); final SamlServiceProviderResolver serviceResolver = Mockito.mock(SamlServiceProviderResolver.class); final WildcardServiceProviderResolver wildcardResolver = Mockito.mock(WildcardServiceProviderResolver.class); - IllegalArgumentException e = LuceneTestCase.expectThrows(IllegalArgumentException.class, - () -> SamlIdentityProvider.builder(serviceResolver, wildcardResolver).fromSettings(env).build()); + IllegalArgumentException e = LuceneTestCase.expectThrows( + IllegalArgumentException.class, + () -> SamlIdentityProvider.builder(serviceResolver, wildcardResolver).fromSettings(env).build() + ); assertThat(e.getMessage(), containsString(IDP_ORGANIZATION_URL.getKey())); assertThat(e.getMessage(), containsString("Not a valid URL")); } @@ -367,11 +382,17 @@ public void testCreateSigningCredentialFromKeyStoreWithSingleEntryButWrongAlias( builder.setSecureSettings(secureSettings); final Settings settings = builder.build(); final Environment env = TestEnvironment.newEnvironment(settings); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> SamlIdentityProviderBuilder.buildSigningCredential(env, settings, "xpack.idp.signing.")); - assertThat(e, throwableWithMessage( - "The configured credential [xpack.idp.signing.keystore] with alias [some-other] is not a valid signing key" - + " - There is no private key available for this credential")); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> SamlIdentityProviderBuilder.buildSigningCredential(env, settings, "xpack.idp.signing.") + ); + assertThat( + e, + throwableWithMessage( + "The configured credential [xpack.idp.signing.keystore] with alias [some-other] is not a valid signing key" + + " - There is no private key available for this credential" + ) + ); } public void testCreateSigningCredentialFromKeyStoreWithMultipleEntriesAndConfiguredAlias() throws Exception { @@ -419,11 +440,17 @@ public void testCreateSigningCredentialFromKeyStoreWithMultipleEntriesButWrongAl builder.setSecureSettings(secureSettings); final Settings settings = builder.build(); final Environment env = TestEnvironment.newEnvironment(settings); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> SamlIdentityProviderBuilder.buildSigningCredential(env, settings, "xpack.idp.signing.")); - assertThat(e, throwableWithMessage( - "The configured credential [xpack.idp.signing.keystore] with alias [some-other] is not a valid signing key" - + " - There is no private key available for this credential")); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> SamlIdentityProviderBuilder.buildSigningCredential(env, settings, "xpack.idp.signing.") + ); + assertThat( + e, + throwableWithMessage( + "The configured credential [xpack.idp.signing.keystore] with alias [some-other] is not a valid signing key" + + " - There is no private key available for this credential" + ) + ); } public void testCreateMetadataSigningCredentialFromΚeystoreWithSingleEntry() throws Exception { @@ -494,11 +521,17 @@ public void testCreateMetadataSigningCredentialFromKeyStoreWithSingleEntryButWro builder.setSecureSettings(secureSettings); final Settings settings = builder.build(); final Environment env = TestEnvironment.newEnvironment(settings); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> SamlIdentityProviderBuilder.buildSigningCredential(env, settings, "xpack.idp.metadata_signing.")); - assertThat(e, throwableWithMessage( - "The configured credential [xpack.idp.metadata_signing.keystore] with alias [some-other] is not a valid signing key" - + " - There is no private key available for this credential")); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> SamlIdentityProviderBuilder.buildSigningCredential(env, settings, "xpack.idp.metadata_signing.") + ); + assertThat( + e, + throwableWithMessage( + "The configured credential [xpack.idp.metadata_signing.keystore] with alias [some-other] is not a valid signing key" + + " - There is no private key available for this credential" + ) + ); } public void testCreateMetadataSigningCredentialFromKeyStoreWithMultipleEntriesAndConfiguredAlias() throws Exception { @@ -546,11 +579,17 @@ public void testCreateMetadataSigningCredentialFromKeyStoreWithMultipleEntriesBu builder.setSecureSettings(secureSettings); final Settings settings = builder.build(); final Environment env = TestEnvironment.newEnvironment(settings); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> SamlIdentityProviderBuilder.buildSigningCredential(env, settings, "xpack.idp.metadata_signing.")); - assertThat(e, throwableWithMessage( - "The configured credential [xpack.idp.metadata_signing.keystore] with alias [some-other] is not a valid signing key" - + " - There is no private key available for this credential")); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> SamlIdentityProviderBuilder.buildSigningCredential(env, settings, "xpack.idp.metadata_signing.") + ); + assertThat( + e, + throwableWithMessage( + "The configured credential [xpack.idp.metadata_signing.keystore] with alias [some-other] is not a valid signing key" + + " - There is no private key available for this credential" + ) + ); } } diff --git a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/idp/SamlIdpMetadataBuilderTests.java b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/idp/SamlIdpMetadataBuilderTests.java index 3241f7688a377..bd39a8aa697ea 100644 --- a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/idp/SamlIdpMetadataBuilderTests.java +++ b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/idp/SamlIdpMetadataBuilderTests.java @@ -39,9 +39,10 @@ public void setup() throws Exception { public void testSimpleMetadataGeneration() throws Exception { final String entityId = "https://idp.org"; - final EntityDescriptor entityDescriptor = new SamlIdPMetadataBuilder(entityId) - .withSingleSignOnServiceUrl(SAML2_REDIRECT_BINDING_URI, new URL(entityId + "/sso/redirect")) - .build(); + final EntityDescriptor entityDescriptor = new SamlIdPMetadataBuilder(entityId).withSingleSignOnServiceUrl( + SAML2_REDIRECT_BINDING_URI, + new URL(entityId + "/sso/redirect") + ).build(); final Element element = new EntityDescriptorMarshaller().marshall(entityDescriptor); final String xml = samlFactory.toString(element, false); assertThat( @@ -61,8 +62,7 @@ public void testSimpleMetadataGeneration() throws Exception { public void testMetadataGenerationWithAllParameters() throws Exception { final String entityId = "https://idp.org"; - final EntityDescriptor entityDescriptor = new SamlIdPMetadataBuilder(entityId) - .withLocale(Locale.forLanguageTag("en")) + final EntityDescriptor entityDescriptor = new SamlIdPMetadataBuilder(entityId).withLocale(Locale.forLanguageTag("en")) .withSingleSignOnServiceUrl(SAML2_REDIRECT_BINDING_URI, new URL(entityId + "/sso/redirect")) .withSingleSignOnServiceUrl(SAML2_POST_BINDING_URI, new URL(entityId + "/sso/post")) .withSingleLogoutServiceUrl(SAML2_REDIRECT_BINDING_URI, new URL(entityId + "/slo/redirect")) @@ -96,7 +96,7 @@ public void testMetadataGenerationWithAllParameters() throws Exception { "8TDTdZlV3d26STLy5h7Uy6vyCka8Xu8HFQ4hH2qf2L6EhBbzVTB6tuyPQOQwrlLE65nhUNkfBbjZ", "lre45UMc9GuxzHkbvd3HEQaroMHZxnu+/n/JDlgsrCYUEXnZnOXvgUPupPynoRdDN1F6r95TLyU9", "pYjDf/6zNPE854VF6y1TqQ==" - ); + ); // RSA_4096 final String signingCertificateTwo = joinCertificateLines( "MIIFCTCCAvGgAwIBAgIUei1EtkLvWStQusThWwgO14R+gFowDQYJKoZIhvcNAQELBQAwFDESMBAG", @@ -122,7 +122,7 @@ public void testMetadataGenerationWithAllParameters() throws Exception { "L9JH3IKNtUgodr6Z+CcyZswWKutHyyZE5vteNQFKeTidCQAw9kRW6gtGUVRU0+PrMvD/8WhSd6Wk", "FS4XjN+BXrmruCSGugdL9fgpg21qKcZwkR9rYQXqRPK+nTiVCRrOzUyTFnPmusz8fg7eg6ONaf2x", "MUeWfI+F8kK4NH5GkGggGqQDtes3Y+bWQ28lV7ny44TkMBARz6zH" - ); + ); final String expectedXml = "" + "" diff --git a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/idp/SamlMetadataGeneratorTests.java b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/idp/SamlMetadataGeneratorTests.java index 80787e22f86ee..4395ada877337 100644 --- a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/idp/SamlMetadataGeneratorTests.java +++ b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/idp/SamlMetadataGeneratorTests.java @@ -15,10 +15,9 @@ import org.hamcrest.Matchers; import org.opensaml.saml.saml2.metadata.EntityDescriptor; import org.opensaml.saml.security.impl.SAMLSignatureProfileValidator; - -import org.opensaml.xmlsec.signature.support.SignatureException; import org.opensaml.security.x509.X509Credential; import org.opensaml.xmlsec.signature.Signature; +import org.opensaml.xmlsec.signature.support.SignatureException; import org.opensaml.xmlsec.signature.support.SignatureValidator; import org.w3c.dom.Element; @@ -124,9 +123,11 @@ public void testGenerateAndSignMetadata() throws Exception { SAMLSignatureProfileValidator profileValidator = new SAMLSignatureProfileValidator(); profileValidator.validate(signature); SignatureValidator.validate(signature, signingCredential); - //no exception thrown - SignatureException e = expectThrows(SignatureException.class, - () -> SignatureValidator.validate(signature, readCredentials("RSA", 2048))); + // no exception thrown + SignatureException e = expectThrows( + SignatureException.class, + () -> SignatureValidator.validate(signature, readCredentials("RSA", 2048)) + ); if (inFipsJvm()) { assertThat(e.getMessage(), containsString("Signature cryptographic validation not successful")); } else { diff --git a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/rest/action/IdpBaseRestHandlerTests.java b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/rest/action/IdpBaseRestHandlerTests.java index ba1dd1cd6faf8..5a90c5adb35a5 100644 --- a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/rest/action/IdpBaseRestHandlerTests.java +++ b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/rest/action/IdpBaseRestHandlerTests.java @@ -26,17 +26,16 @@ public void testIdpAvailableOnTrialOrEnterprise() { } public void testIdpNotAvailableOnOtherLicenses() { - License.OperationMode mode = - randomValueOtherThanMany(m -> m == License.OperationMode.ENTERPRISE || m == License.OperationMode.TRIAL, - () -> randomFrom(License.OperationMode.values())); + License.OperationMode mode = randomValueOtherThanMany( + m -> m == License.OperationMode.ENTERPRISE || m == License.OperationMode.TRIAL, + () -> randomFrom(License.OperationMode.values()) + ); final IdpBaseRestHandler handler = buildHandler(mode); assertThat(handler.isIdpFeatureAllowed(), equalTo(false)); } private IdpBaseRestHandler buildHandler(License.OperationMode licenseMode) { - final Settings settings = Settings.builder() - .put("xpack.idp.enabled", true) - .build(); + final Settings settings = Settings.builder().put("xpack.idp.enabled", true).build(); final TestUtils.UpdatableLicenseState licenseState = new TestUtils.UpdatableLicenseState(settings); licenseState.update(licenseMode, true, null); return new IdpBaseRestHandler(licenseState) { diff --git a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/sp/SamlServiceProviderDocumentTests.java b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/sp/SamlServiceProviderDocumentTests.java index f06785ffcf3fc..c04619b466d3e 100644 --- a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/sp/SamlServiceProviderDocumentTests.java +++ b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/sp/SamlServiceProviderDocumentTests.java @@ -11,12 +11,12 @@ import org.elasticsearch.common.ValidationException; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.test.VersionUtils; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.VersionUtils; import org.elasticsearch.xpack.idp.saml.test.IdpSamlTestCase; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; @@ -46,15 +46,18 @@ public void testValidationFailuresForMissingFields() throws Exception { final ValidationException validationException = doc.validate(); assertThat(validationException, notNullValue()); assertThat(validationException.validationErrors(), not(emptyIterable())); - assertThat(validationException.validationErrors(), Matchers.containsInAnyOrder( - "field [name] is required, but was [null]", - "field [entity_id] is required, but was [null]", - "field [acs] is required, but was [null]", - "field [created] is required, but was [null]", - "field [last_modified] is required, but was [null]", - "field [privileges.resource] is required, but was [null]", - "field [attributes.principal] is required, but was [null]" - )); + assertThat( + validationException.validationErrors(), + Matchers.containsInAnyOrder( + "field [name] is required, but was [null]", + "field [entity_id] is required, but was [null]", + "field [acs] is required, but was [null]", + "field [created] is required, but was [null]", + "field [last_modified] is required, but was [null]", + "field [privileges.resource] is required, but was [null]", + "field [attributes.principal] is required, but was [null]" + ) + ); } public void testValidationSucceedsWithMinimalFields() throws Exception { @@ -140,8 +143,14 @@ private SamlServiceProviderDocument assertXContentRoundTrip(SamlServiceProviderD final XContentType xContentType = randomFrom(XContentType.values()); final boolean humanReadable = randomBoolean(); final BytesReference bytes1 = XContentHelper.toXContent(obj1, xContentType, humanReadable); - try (XContentParser parser = XContentHelper.createParser( - NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, bytes1, xContentType)) { + try ( + XContentParser parser = XContentHelper.createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + bytes1, + xContentType + ) + ) { final SamlServiceProviderDocument obj2 = SamlServiceProviderDocument.fromXContent(obj1.docId, parser); assertThat(obj2, equalTo(obj1)); @@ -154,8 +163,12 @@ private SamlServiceProviderDocument assertXContentRoundTrip(SamlServiceProviderD private SamlServiceProviderDocument assertSerializationRoundTrip(SamlServiceProviderDocument doc) throws IOException { final Version version = VersionUtils.randomVersionBetween(random(), Version.V_7_7_0, Version.CURRENT); - final SamlServiceProviderDocument read = copyWriteable(doc, new NamedWriteableRegistry(List.of()), - SamlServiceProviderDocument::new, version); + final SamlServiceProviderDocument read = copyWriteable( + doc, + new NamedWriteableRegistry(List.of()), + SamlServiceProviderDocument::new, + version + ); MatcherAssert.assertThat("Serialized document with version [" + version + "] does not match original object", read, equalTo(doc)); return read; } diff --git a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/sp/SamlServiceProviderResolverTests.java b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/sp/SamlServiceProviderResolverTests.java index 474e5768b4d52..6b76deb08aad6 100644 --- a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/sp/SamlServiceProviderResolverTests.java +++ b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/sp/SamlServiceProviderResolverTests.java @@ -58,8 +58,7 @@ public void testResolveWithoutCache() throws Exception { final String resource = "ece:" + randomAlphaOfLengthBetween(6, 12); final Set rolePrivileges = Set.of("role:(.*)"); - final DocumentVersion docVersion = new DocumentVersion( - randomAlphaOfLength(12), randomNonNegativeLong(), randomNonNegativeLong()); + final DocumentVersion docVersion = new DocumentVersion(randomAlphaOfLength(12), randomNonNegativeLong(), randomNonNegativeLong()); final SamlServiceProviderDocument document = new SamlServiceProviderDocument(); document.setEntityId(entityId); document.setAuthenticationExpiry(null); @@ -162,8 +161,8 @@ private void mockDocument(String entityId, DocumentVersion docVersion, SamlServi assertThat(args[0], equalTo(entityId)); - ActionListener> listener - = (ActionListener>) args[args.length - 1]; + ActionListener> listener = (ActionListener< + Set>) args[args.length - 1]; listener.onResponse(Set.of(new SamlServiceProviderIndex.DocumentSupplier(docVersion, () -> document))); return null; }).when(index).findByEntityId(anyString(), any(ActionListener.class)); diff --git a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/sp/SamlServiceProviderTestUtils.java b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/sp/SamlServiceProviderTestUtils.java index 13c7d7b981928..a4d0dbd34945f 100644 --- a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/sp/SamlServiceProviderTestUtils.java +++ b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/sp/SamlServiceProviderTestUtils.java @@ -23,7 +23,7 @@ public class SamlServiceProviderTestUtils { - private SamlServiceProviderTestUtils(){} //utility class + private SamlServiceProviderTestUtils() {} // utility class public static SamlServiceProviderDocument randomDocument() { return randomDocument(randomIntBetween(1, 999_999)); @@ -74,7 +74,13 @@ private static String randomUri() { } private static String randomUri(String scheme) { - return scheme + "://" + randomAlphaOfLengthBetween(2, 6) + "." - + randomAlphaOfLengthBetween(4, 8) + "." + randomAlphaOfLengthBetween(2, 4) + "/"; + return scheme + + "://" + + randomAlphaOfLengthBetween(2, 6) + + "." + + randomAlphaOfLengthBetween(4, 8) + + "." + + randomAlphaOfLengthBetween(2, 4) + + "/"; } } diff --git a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/sp/WildcardServiceProviderResolverTests.java b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/sp/WildcardServiceProviderResolverTests.java index baa8441f1f45f..b2b2008098d41 100644 --- a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/sp/WildcardServiceProviderResolverTests.java +++ b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/sp/WildcardServiceProviderResolverTests.java @@ -8,10 +8,10 @@ package org.elasticsearch.xpack.idp.saml.sp; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.script.ScriptModule; import org.elasticsearch.script.ScriptService; import org.elasticsearch.script.mustache.MustacheScriptEngine; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.idp.saml.test.IdpSamlTestCase; import org.junit.Before; import org.opensaml.saml.saml2.core.NameID; @@ -92,8 +92,11 @@ public class WildcardServiceProviderResolverTests extends IdpSamlTestCase { @Before public void setUpResolver() { final Settings settings = Settings.EMPTY; - final ScriptService scriptService = new ScriptService(settings, - Collections.singletonMap(MustacheScriptEngine.NAME, new MustacheScriptEngine()), ScriptModule.CORE_CONTEXTS); + final ScriptService scriptService = new ScriptService( + settings, + Collections.singletonMap(MustacheScriptEngine.NAME, new MustacheScriptEngine()), + ScriptModule.CORE_CONTEXTS + ); final ServiceProviderDefaults samlDefaults = new ServiceProviderDefaults("elastic-cloud", NameID.TRANSIENT, Duration.ofMinutes(15)); resolver = new WildcardServiceProviderResolver(settings, scriptService, new SamlServiceProviderFactory(samlDefaults)); } @@ -105,24 +108,37 @@ public void testParsingOfServices() throws IOException { final WildcardServiceProvider service1a = resolver.services().get("service1a"); assertThat( service1a.extractTokens("https://abcdef.example.com/", "https://abcdef.service.example.com/saml2/acs"), - equalTo(Map.ofEntries( - Map.entry("service", "abcdef"), - Map.entry("entity_id", "https://abcdef.example.com/"), - Map.entry("acs", "https://abcdef.service.example.com/saml2/acs")))); - expectThrows(IllegalArgumentException.class, () -> - service1a.extractTokens("https://abcdef.example.com/", "https://different.service.example.com/saml2/acs")); + equalTo( + Map.ofEntries( + Map.entry("service", "abcdef"), + Map.entry("entity_id", "https://abcdef.example.com/"), + Map.entry("acs", "https://abcdef.service.example.com/saml2/acs") + ) + ) + ); + expectThrows( + IllegalArgumentException.class, + () -> service1a.extractTokens("https://abcdef.example.com/", "https://different.service.example.com/saml2/acs") + ); assertThat(service1a.extractTokens("urn:foo:bar", "https://something.example.org/foo/bar"), nullValue()); assertThat(service1a.extractTokens("https://xyzzy.example.com/", "https://services.example.com/xyzzy/saml2/acs"), nullValue()); final WildcardServiceProvider service1b = resolver.services().get("service1b"); - assertThat(service1b.extractTokens("https://xyzzy.example.com/", "https://services.example.com/xyzzy/saml2/acs"), - equalTo(Map.ofEntries( - Map.entry("service", "xyzzy"), - Map.entry("entity_id", "https://xyzzy.example.com/"), - Map.entry("acs", "https://services.example.com/xyzzy/saml2/acs")))); + assertThat( + service1b.extractTokens("https://xyzzy.example.com/", "https://services.example.com/xyzzy/saml2/acs"), + equalTo( + Map.ofEntries( + Map.entry("service", "xyzzy"), + Map.entry("entity_id", "https://xyzzy.example.com/"), + Map.entry("acs", "https://services.example.com/xyzzy/saml2/acs") + ) + ) + ); assertThat(service1b.extractTokens("https://abcdef.example.com/", "https://abcdef.service.example.com/saml2/acs"), nullValue()); - expectThrows(IllegalArgumentException.class, () -> - service1b.extractTokens("https://abcdef.example.com/", "https://services.example.com/xyzzy/saml2/acs")); + expectThrows( + IllegalArgumentException.class, + () -> service1b.extractTokens("https://abcdef.example.com/", "https://services.example.com/xyzzy/saml2/acs") + ); assertThat(service1b.extractTokens("urn:foo:bar", "https://something.example.org/foo/bar"), nullValue()); } diff --git a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/support/SamlAuthenticationStateTests.java b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/support/SamlAuthenticationStateTests.java index 4aa0a1bd0fc85..626f63e0368ca 100644 --- a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/support/SamlAuthenticationStateTests.java +++ b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/support/SamlAuthenticationStateTests.java @@ -9,12 +9,12 @@ import org.elasticsearch.Version; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.test.VersionUtils; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.VersionUtils; import org.elasticsearch.xpack.idp.saml.test.IdpSamlTestCase; import org.hamcrest.MatcherAssert; import org.opensaml.saml.saml2.core.NameID; @@ -68,8 +68,14 @@ private SamlAuthenticationState assertXContentRoundTrip(SamlAuthenticationState final XContentType xContentType = randomFrom(XContentType.values()); final boolean humanReadable = randomBoolean(); final BytesReference bytes1 = XContentHelper.toXContent(obj1, xContentType, humanReadable); - try (XContentParser parser = XContentHelper.createParser( - NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, bytes1, xContentType)) { + try ( + XContentParser parser = XContentHelper.createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + bytes1, + xContentType + ) + ) { final SamlAuthenticationState obj2 = SamlAuthenticationState.fromXContent(parser); assertThat(obj2, equalTo(obj1)); @@ -82,8 +88,12 @@ private SamlAuthenticationState assertXContentRoundTrip(SamlAuthenticationState private SamlAuthenticationState assertSerializationRoundTrip(SamlAuthenticationState state) throws IOException { final Version version = VersionUtils.randomVersionBetween(random(), Version.V_7_7_0, Version.CURRENT); - final SamlAuthenticationState read = copyWriteable(state, new NamedWriteableRegistry(List.of()), - SamlAuthenticationState::new, version); + final SamlAuthenticationState read = copyWriteable( + state, + new NamedWriteableRegistry(List.of()), + SamlAuthenticationState::new, + version + ); MatcherAssert.assertThat("Serialized state with version [" + version + "] does not match original object", read, equalTo(state)); return read; } diff --git a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/support/SamlObjectSignerTests.java b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/support/SamlObjectSignerTests.java index 8d29907d034ec..536de6b0f6b2b 100644 --- a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/support/SamlObjectSignerTests.java +++ b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/support/SamlObjectSignerTests.java @@ -40,6 +40,7 @@ public class SamlObjectSignerTests extends IdpSamlTestCase { private SamlFactory samlFactory; + @Before public void setupState() { SamlInit.initialize(); @@ -62,9 +63,7 @@ public void testSignLogoutRequest() throws Exception { // verify with correct credential SignatureValidator.validate(signedRequest.getSignature(), credential); // fail with incorrect credential - expectThrows(SignatureException.class, - () -> SignatureValidator.validate(signedRequest.getSignature(), alternateCredential) - ); + expectThrows(SignatureException.class, () -> SignatureValidator.validate(signedRequest.getSignature(), alternateCredential)); } } @@ -84,9 +83,7 @@ public void testSignAuthResponse() throws Exception { // verify with correct credential SignatureValidator.validate(signedResponse.getSignature(), credential); // fail with incorrect credential - expectThrows(SignatureException.class, - () -> SignatureValidator.validate(signedResponse.getSignature(), alternateCredential) - ); + expectThrows(SignatureException.class, () -> SignatureValidator.validate(signedResponse.getSignature(), alternateCredential)); } } diff --git a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/test/IdpSamlTestCase.java b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/test/IdpSamlTestCase.java index 762561970b3ee..0dbcdb18d05d9 100644 --- a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/test/IdpSamlTestCase.java +++ b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/test/IdpSamlTestCase.java @@ -50,6 +50,7 @@ import java.util.List; import java.util.Locale; import java.util.stream.Collectors; + import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; @@ -99,19 +100,19 @@ protected static void mockRegisteredServiceProvider(SamlIdentityProvider idp, St listener.onResponse(sp); return null; - }).when(idp).resolveServiceProvider(Mockito.eq(entityId), nullable(String.class), Mockito.anyBoolean(), - Mockito.any(ActionListener.class)); + }) + .when(idp) + .resolveServiceProvider(Mockito.eq(entityId), nullable(String.class), Mockito.anyBoolean(), Mockito.any(ActionListener.class)); } @SuppressWarnings("unchecked") - protected static void mockRegisteredServiceProvider(SamlServiceProviderResolver resolverMock, String entityId, - SamlServiceProvider sp) { + protected static void mockRegisteredServiceProvider(SamlServiceProviderResolver resolverMock, String entityId, SamlServiceProvider sp) { Mockito.doAnswer(inv -> { final Object[] args = inv.getArguments(); assertThat(args, Matchers.arrayWithSize(2)); assertThat(args[0], Matchers.equalTo(entityId)); - assertThat(args[args.length-1], Matchers.instanceOf(ActionListener.class)); - ActionListener listener = (ActionListener) args[args.length-1]; + assertThat(args[args.length - 1], Matchers.instanceOf(ActionListener.class)); + ActionListener listener = (ActionListener) args[args.length - 1]; listener.onResponse(sp); return null; @@ -176,7 +177,7 @@ protected String toString(Element element) { } protected void assertValidXml(String xml) throws Exception { - new XmlValidator( "saml-schema-metadata-2.0.xsd").validate(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8))); + new XmlValidator("saml-schema-metadata-2.0.xsd").validate(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8))); } protected String joinCertificateLines(String... lines) { diff --git a/x-pack/plugin/ilm/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ilm/CCRIndexLifecycleIT.java b/x-pack/plugin/ilm/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ilm/CCRIndexLifecycleIT.java index e7476514f90d6..05c945cefa1c1 100644 --- a/x-pack/plugin/ilm/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ilm/CCRIndexLifecycleIT.java +++ b/x-pack/plugin/ilm/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ilm/CCRIndexLifecycleIT.java @@ -17,13 +17,13 @@ import org.elasticsearch.client.RestClient; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.rest.RestStatus; import org.elasticsearch.xcontent.ObjectPath; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.rest.RestStatus; import org.elasticsearch.xpack.ccr.ESCCRRestTestCase; import org.elasticsearch.xpack.core.ilm.LifecycleAction; import org.elasticsearch.xpack.core.ilm.LifecyclePolicy; @@ -55,7 +55,7 @@ public void testBasicCCRAndILMIntegration() throws Exception { String policyName = "basic-test"; if ("leader".equals(targetCluster)) { - putILMPolicy(policyName, "50GB", null, TimeValue.timeValueHours(7*24)); + putILMPolicy(policyName, "50GB", null, TimeValue.timeValueHours(7 * 24)); Settings indexSettings = Settings.builder() .put("index.number_of_shards", 1) .put("index.number_of_replicas", 0) @@ -66,7 +66,7 @@ public void testBasicCCRAndILMIntegration() throws Exception { ensureGreen(indexName); } else if ("follow".equals(targetCluster)) { // Policy with the same name must exist in follower cluster too: - putILMPolicy(policyName, "50GB", null, TimeValue.timeValueHours(7*24)); + putILMPolicy(policyName, "50GB", null, TimeValue.timeValueHours(7 * 24)); followIndex(indexName, indexName); ensureGreen(indexName); @@ -85,10 +85,7 @@ public void testBasicCCRAndILMIntegration() throws Exception { assertILMPolicy(client(), indexName, policyName, "hot"); }); - updateIndexSettings(leaderClient, indexName, Settings.builder() - .put("index.lifecycle.indexing_complete", true) - .build() - ); + updateIndexSettings(leaderClient, indexName, Settings.builder().put("index.lifecycle.indexing_complete", true).build()); assertBusy(() -> { // Ensure that 'index.lifecycle.indexing_complete' is replicated: @@ -114,10 +111,7 @@ public void testBasicCCRAndILMIntegration() throws Exception { public void testCCRUnfollowDuringSnapshot() throws Exception { String indexName = "unfollow-test-index"; if ("leader".equals(targetCluster)) { - Settings indexSettings = Settings.builder() - .put("index.number_of_shards", 2) - .put("index.number_of_replicas", 0) - .build(); + Settings indexSettings = Settings.builder().put("index.number_of_shards", 2).put("index.number_of_replicas", 0).build(); createIndex(indexName, indexSettings); ensureGreen(indexName); } else if ("follow".equals(targetCluster)) { @@ -127,25 +121,26 @@ public void testCCRUnfollowDuringSnapshot() throws Exception { // Create the repository before taking the snapshot. Request request = new Request("PUT", "/_snapshot/repo"); - request.setJsonEntity(Strings - .toString(JsonXContent.contentBuilder() - .startObject() - .field("type", "fs") - .startObject("settings") - .field("compress", randomBoolean()) - .field("location", System.getProperty("tests.path.repo")) - .field("max_snapshot_bytes_per_sec", "256b") - .endObject() - .endObject())); + request.setJsonEntity( + Strings.toString( + JsonXContent.contentBuilder() + .startObject() + .field("type", "fs") + .startObject("settings") + .field("compress", randomBoolean()) + .field("location", System.getProperty("tests.path.repo")) + .field("max_snapshot_bytes_per_sec", "256b") + .endObject() + .endObject() + ) + ); assertOK(client().performRequest(request)); try (RestClient leaderClient = buildLeaderClient()) { index(leaderClient, indexName, "1"); assertDocumentExists(leaderClient, indexName, "1"); - updateIndexSettings(leaderClient, indexName, Settings.builder() - .put("index.lifecycle.indexing_complete", true) - .build()); + updateIndexSettings(leaderClient, indexName, Settings.builder().put("index.lifecycle.indexing_complete", true).build()); // start snapshot String snapName = "snapshot-" + randomAlphaOfLength(10).toLowerCase(Locale.ROOT); @@ -196,8 +191,9 @@ public void testCcrAndIlmWithRollover() throws Exception { .put("index.lifecycle.name", policyName) .put("index.lifecycle.rollover_alias", alias) .build(); - templateRequest.setJsonEntity("{\"index_patterns\": [\"mymetrics-*\"], \"template\":{\"settings\": " + - Strings.toString(indexSettings) + "}}"); + templateRequest.setJsonEntity( + "{\"index_patterns\": [\"mymetrics-*\"], \"template\":{\"settings\": " + Strings.toString(indexSettings) + "}}" + ); assertOK(client().performRequest(templateRequest)); } else if ("follow".equals(targetCluster)) { // Policy with the same name must exist in follower cluster too: @@ -205,16 +201,22 @@ public void testCcrAndIlmWithRollover() throws Exception { // Set up an auto-follow pattern Request createAutoFollowRequest = new Request("PUT", "/_ccr/auto_follow/my_auto_follow_pattern"); - createAutoFollowRequest.setJsonEntity("{\"leader_index_patterns\": [\"mymetrics-*\"], " + - "\"remote_cluster\": \"leader_cluster\", \"read_poll_timeout\": \"1000ms\"}"); + createAutoFollowRequest.setJsonEntity( + "{\"leader_index_patterns\": [\"mymetrics-*\"], " + + "\"remote_cluster\": \"leader_cluster\", \"read_poll_timeout\": \"1000ms\"}" + ); assertOK(client().performRequest(createAutoFollowRequest)); try (RestClient leaderClient = buildLeaderClient()) { // Create an index on the leader using the template set up above Request createIndexRequest = new Request("PUT", "/" + indexName); - createIndexRequest.setJsonEntity("{" + - "\"mappings\": {\"properties\": {\"field\": {\"type\": \"keyword\"}}}, " + - "\"aliases\": {\"" + alias + "\": {\"is_write_index\": true}} }"); + createIndexRequest.setJsonEntity( + "{" + + "\"mappings\": {\"properties\": {\"field\": {\"type\": \"keyword\"}}}, " + + "\"aliases\": {\"" + + alias + + "\": {\"is_write_index\": true}} }" + ); assertOK(leaderClient.performRequest(createIndexRequest)); // Check that the new index is created Request checkIndexRequest = new Request("GET", "/_cluster/health/" + indexName); @@ -289,10 +291,10 @@ public void testAliasReplicatedOnShrink() throws Exception { if ("leader".equals(targetCluster)) { Settings indexSettings = Settings.builder() - .put("index.number_of_shards", 3) - .put("index.number_of_replicas", 0) - .put("index.lifecycle.name", policyName) // this policy won't exist on the leader, that's fine - .build(); + .put("index.number_of_shards", 3) + .put("index.number_of_replicas", 0) + .put("index.lifecycle.name", policyName) // this policy won't exist on the leader, that's fine + .build(); final StringBuilder aliases = new StringBuilder(); boolean first = true; for (int i = 0; i < numberOfAliases; i++) { @@ -322,16 +324,13 @@ public void testAliasReplicatedOnShrink() throws Exception { // Set the indexing_complete flag on the leader so the index will actually unfollow try (RestClient leaderClient = buildLeaderClient()) { - updateIndexSettings(leaderClient, indexName, Settings.builder() - .put("index.lifecycle.indexing_complete", true) - .build() - ); + updateIndexSettings(leaderClient, indexName, Settings.builder().put("index.lifecycle.indexing_complete", true).build()); } // Wait for the setting to get replicated assertBusy(() -> assertThat(getIndexSetting(client(), indexName, "index.lifecycle.indexing_complete"), equalTo("true"))); - assertBusy(() -> assertThat(getShrinkIndexName(client(), indexName) , notNullValue()), 30, TimeUnit.SECONDS); + assertBusy(() -> assertThat(getShrinkIndexName(client(), indexName), notNullValue()), 30, TimeUnit.SECONDS); String shrunkenIndexName = getShrinkIndexName(client(), indexName); // Wait for the index to continue with its lifecycle and be shrunk @@ -375,10 +374,7 @@ public void testUnfollowInjectedBeforeShrink() throws Exception { // Set the indexing_complete flag on the leader so the index will actually unfollow try (RestClient leaderClient = buildLeaderClient()) { - updateIndexSettings(leaderClient, indexName, Settings.builder() - .put("index.lifecycle.indexing_complete", true) - .build() - ); + updateIndexSettings(leaderClient, indexName, Settings.builder().put("index.lifecycle.indexing_complete", true).build()); } // Wait for the setting to get replicated @@ -388,7 +384,7 @@ public void testUnfollowInjectedBeforeShrink() throws Exception { // moves through the unfollow and shrink actions so fast that the // index often disappears between assertBusy checks - assertBusy(() -> assertThat(getShrinkIndexName(client(), indexName) , notNullValue()), 1, TimeUnit.MINUTES); + assertBusy(() -> assertThat(getShrinkIndexName(client(), indexName), notNullValue()), 1, TimeUnit.MINUTES); String shrunkenIndexName = getShrinkIndexName(client(), indexName); // Wait for the index to continue with its lifecycle and be shrunk @@ -407,10 +403,7 @@ public void testCannotShrinkLeaderIndex() throws Exception { // otherwise it'll proceed through shrink before we can set up the // follower putShrinkOnlyPolicy(client(), policyName); - Settings indexSettings = Settings.builder() - .put("index.number_of_shards", 2) - .put("index.number_of_replicas", 0) - .build(); + Settings indexSettings = Settings.builder().put("index.number_of_shards", 2).put("index.number_of_replicas", 0).build(); createIndex(indexName, indexSettings, "", ""); ensureGreen(indexName); } else if ("follow".equals(targetCluster)) { @@ -423,8 +416,10 @@ public void testCannotShrinkLeaderIndex() throws Exception { // Now we can set up the leader to use the policy Request changePolicyRequest = new Request("PUT", "/" + indexName + "/_settings"); - final StringEntity changePolicyEntity = new StringEntity("{ \"index.lifecycle.name\": \"" + policyName + "\" }", - ContentType.APPLICATION_JSON); + final StringEntity changePolicyEntity = new StringEntity( + "{ \"index.lifecycle.name\": \"" + policyName + "\" }", + ContentType.APPLICATION_JSON + ); changePolicyRequest.setEntity(changePolicyEntity); assertOK(leaderClient.performRequest(changePolicyRequest)); @@ -452,12 +447,9 @@ public void testCannotShrinkLeaderIndex() throws Exception { assertILMPolicy(client(), indexName, policyName, "hot", "unfollow", "wait-for-indexing-complete"); // Manually set this to kick the process - updateIndexSettings(leaderClient, indexName, Settings.builder() - .put("index.lifecycle.indexing_complete", true) - .build() - ); + updateIndexSettings(leaderClient, indexName, Settings.builder().put("index.lifecycle.indexing_complete", true).build()); - assertBusy(() -> assertThat(getShrinkIndexName(leaderClient, indexName) , notNullValue()), 30, TimeUnit.SECONDS); + assertBusy(() -> assertThat(getShrinkIndexName(leaderClient, indexName), notNullValue()), 30, TimeUnit.SECONDS); String shrunkenIndexName = getShrinkIndexName(leaderClient, indexName); assertBusy(() -> { // The shrunken index should now be created on the leader... @@ -492,9 +484,9 @@ public void testILMUnfollowFailsToRemoveRetentionLeases() throws Exception { String leaderRemoteClusterSeed = System.getProperty("tests.leader_remote_cluster_seed"); configureRemoteClusters("other_remote", leaderRemoteClusterSeed); assertBusy(() -> { - Map localConnection = (Map) toMap(client() - .performRequest(new Request("GET", "/_remote/info"))) - .get("other_remote"); + Map localConnection = (Map) toMap(client().performRequest(new Request("GET", "/_remote/info"))).get( + "other_remote" + ); assertThat(localConnection, notNullValue()); assertThat(localConnection.get("connected"), is(true)); }); @@ -506,29 +498,26 @@ public void testILMUnfollowFailsToRemoveRetentionLeases() throws Exception { client().performRequest(new Request("POST", "/_ilm/stop")); // Set indexing complete and wait for it to be replicated - updateIndexSettings(leaderClient, leaderIndex, Settings.builder() - .put("index.lifecycle.indexing_complete", true) - .build() + updateIndexSettings(leaderClient, leaderIndex, Settings.builder().put("index.lifecycle.indexing_complete", true).build()); + assertBusy( + () -> { assertThat(getIndexSetting(client(), followerIndex, "index.lifecycle.indexing_complete"), is("true")); } ); - assertBusy(() -> { - assertThat(getIndexSetting(client(), followerIndex, "index.lifecycle.indexing_complete"), is("true")); - }); // Remove remote cluster alias: configureRemoteClusters("other_remote", null); assertBusy(() -> { - Map localConnection = (Map) toMap(client() - .performRequest(new Request("GET", "/_remote/info"))) - .get("other_remote"); + Map localConnection = (Map) toMap(client().performRequest(new Request("GET", "/_remote/info"))).get( + "other_remote" + ); assertThat(localConnection, nullValue()); }); // Then add it back with an incorrect seed node: // (unfollow api needs a remote cluster alias) configureRemoteClusters("other_remote", "localhost:9999"); assertBusy(() -> { - Map localConnection = (Map) toMap(client() - .performRequest(new Request("GET", "/_remote/info"))) - .get("other_remote"); + Map localConnection = (Map) toMap(client().performRequest(new Request("GET", "/_remote/info"))).get( + "other_remote" + ); assertThat(localConnection, notNullValue()); assertThat(localConnection.get("connected"), is(false)); @@ -544,14 +533,10 @@ public void testILMUnfollowFailsToRemoveRetentionLeases() throws Exception { // Start ILM back up and let it unfollow client().performRequest(new Request("POST", "/_ilm/start")); // Wait for the policy to be complete - assertBusy(() -> { - assertILMPolicy(client(), followerIndex, policyName, "hot", "complete", "complete"); - }); + assertBusy(() -> { assertILMPolicy(client(), followerIndex, policyName, "hot", "complete", "complete"); }); // Ensure the "follower" index has successfully unfollowed - assertBusy(() -> { - assertThat(getIndexSetting(client(), followerIndex, "index.xpack.ccr.following_index"), nullValue()); - }); + assertBusy(() -> { assertThat(getIndexSetting(client(), followerIndex, "index.xpack.ccr.following_index"), nullValue()); }); } } } @@ -559,8 +544,13 @@ public void testILMUnfollowFailsToRemoveRetentionLeases() throws Exception { private void configureRemoteClusters(String name, String leaderRemoteClusterSeed) throws IOException { logger.info("Configuring leader remote cluster [{}]", leaderRemoteClusterSeed); Request request = new Request("PUT", "/_cluster/settings"); - request.setJsonEntity("{\"persistent\": {\"cluster.remote." + name + ".seeds\": " + - (leaderRemoteClusterSeed != null ? String.format(Locale.ROOT, "\"%s\"", leaderRemoteClusterSeed) : null) + "}}"); + request.setJsonEntity( + "{\"persistent\": {\"cluster.remote." + + name + + ".seeds\": " + + (leaderRemoteClusterSeed != null ? String.format(Locale.ROOT, "\"%s\"", leaderRemoteClusterSeed) : null) + + "}}" + ); assertThat(client().performRequest(request).getStatusLine().getStatusCode(), equalTo(200)); } @@ -713,8 +703,14 @@ private static void assertILMPolicy(RestClient client, String index, String poli assertILMPolicy(client, index, policy, expectedPhase, null, null); } - private static void assertILMPolicy(RestClient client, String index, String policy, String expectedPhase, - String expectedAction, String expectedStep) throws IOException { + private static void assertILMPolicy( + RestClient client, + String index, + String policy, + String expectedPhase, + String expectedAction, + String expectedStep + ) throws IOException { final Request request = new Request("GET", "/" + index + "/_ilm/explain"); Map response = toMap(client.performRequest(request)); LOGGER.info("response={}", response); @@ -776,8 +772,7 @@ private void createNewSingletonPolicy(String policyName, String phaseName, Lifec LifecyclePolicy lifecyclePolicy = new LifecyclePolicy(policyName, singletonMap(phase.getName(), phase)); XContentBuilder builder = jsonBuilder(); lifecyclePolicy.toXContent(builder, null); - final StringEntity entity = new StringEntity( - "{ \"policy\":" + Strings.toString(builder) + "}", ContentType.APPLICATION_JSON); + final StringEntity entity = new StringEntity("{ \"policy\":" + Strings.toString(builder) + "}", ContentType.APPLICATION_JSON); Request request = new Request("PUT", "_ilm/policy/" + policyName); request.setEntity(entity); client().performRequest(request); @@ -786,8 +781,10 @@ private void createNewSingletonPolicy(String policyName, String phaseName, Lifec public static void updatePolicy(String indexName, String policy) throws IOException { Request changePolicyRequest = new Request("PUT", "/" + indexName + "/_settings"); - final StringEntity changePolicyEntity = new StringEntity("{ \"index.lifecycle.name\": \"" + policy + "\" }", - ContentType.APPLICATION_JSON); + final StringEntity changePolicyEntity = new StringEntity( + "{ \"index.lifecycle.name\": \"" + policy + "\" }", + ContentType.APPLICATION_JSON + ); changePolicyRequest.setEntity(changePolicyEntity); assertOK(client().performRequest(changePolicyRequest)); } @@ -810,8 +807,10 @@ private static String getShrinkIndexName(RestClient client, String originalIndex String[] shrunkenIndexName = new String[1]; waitUntil(() -> { try { - Request explainRequest = new Request("GET", SHRUNKEN_INDEX_PREFIX + "*" + originalIndex + "," + originalIndex - + "/_ilm/explain"); + Request explainRequest = new Request( + "GET", + SHRUNKEN_INDEX_PREFIX + "*" + originalIndex + "," + originalIndex + "/_ilm/explain" + ); explainRequest.addParameter("only_errors", Boolean.toString(false)); explainRequest.addParameter("only_managed", Boolean.toString(false)); Response response = client.performRequest(explainRequest); @@ -822,7 +821,7 @@ private static String getShrinkIndexName(RestClient client, String originalIndex Map> indexResponse = ((Map>) responseMap.get("indices")); Map explainIndexResponse = indexResponse.get(originalIndex); - if(explainIndexResponse == null) { + if (explainIndexResponse == null) { // maybe we swapped the alias from the original index to the shrunken one already for (Map.Entry> indexToExplainMap : indexResponse.entrySet()) { // we don't know the exact name of the shrunken index, but we know it starts with the configured prefix @@ -844,8 +843,8 @@ private static String getShrinkIndexName(RestClient client, String originalIndex return false; } }, 30, TimeUnit.SECONDS); - assert shrunkenIndexName[0] != null : "lifecycle execution state must contain the target shrink index name for index [" - + originalIndex + "]"; + assert shrunkenIndexName[0] != null + : "lifecycle execution state must contain the target shrink index name for index [" + originalIndex + "]"; return shrunkenIndexName[0]; } } diff --git a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/MigrateToDataTiersIT.java b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/MigrateToDataTiersIT.java index 5aeba982a135a..71e3530a8457b 100644 --- a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/MigrateToDataTiersIT.java +++ b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/MigrateToDataTiersIT.java @@ -106,7 +106,9 @@ public void testMigrateToDataTiersAction() throws Exception { coldActions.put(SetPriorityAction.NAME, new SetPriorityAction(0)); coldActions.put(AllocateAction.NAME, new AllocateAction(0, null, null, null, singletonMap("data", "cold"))); - createPolicy(client(), policy, + createPolicy( + client(), + policy, new Phase("hot", TimeValue.ZERO, hotActions), new Phase("warm", TimeValue.ZERO, warmActions), new Phase("cold", TimeValue.timeValueDays(100), coldActions), @@ -114,20 +116,26 @@ public void testMigrateToDataTiersAction() throws Exception { new Phase("delete", TimeValue.ZERO, singletonMap(DeleteAction.NAME, new DeleteAction())) ); - createIndexWithSettings(client(), index, alias, Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put(LifecycleSettings.LIFECYCLE_NAME, policy) - .putNull(DataTier.TIER_PREFERENCE) - .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias) + createIndexWithSettings( + client(), + index, + alias, + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put(LifecycleSettings.LIFECYCLE_NAME, policy) + .putNull(DataTier.TIER_PREFERENCE) + .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias) ); // wait for the index to advance to the warm phase - assertBusy(() -> - assertThat(getStepKeyForIndex(client(), index).getPhase(), equalTo("warm")), 30, TimeUnit.SECONDS); + assertBusy(() -> assertThat(getStepKeyForIndex(client(), index).getPhase(), equalTo("warm")), 30, TimeUnit.SECONDS); // let's wait for this index to have received the `require.data` configuration from the warm phase/allocate action - assertBusy(() -> - assertThat(getStepKeyForIndex(client(), index).getName(), equalTo(AllocationRoutedStep.NAME)), 30, TimeUnit.SECONDS); + assertBusy( + () -> assertThat(getStepKeyForIndex(client(), index).getName(), equalTo(AllocationRoutedStep.NAME)), + 30, + TimeUnit.SECONDS + ); // let's also have a policy that doesn't need migrating String rolloverOnlyPolicyName = "rollover-policy"; @@ -138,11 +146,16 @@ public void testMigrateToDataTiersAction() throws Exception { // assign the rollover-only policy to a few other indices - these indices will end up getting caught by the catch-all // tier preference migration String rolloverIndex = rolloverIndexPrefix + "-00000" + i; - createIndexWithSettings(client(), rolloverIndex, alias + i, Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .putNull(DataTier.TIER_PREFERENCE) // since we always enforce a tier preference, this will be ignored (i.e. data_content) - .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias + i) + createIndexWithSettings( + client(), + rolloverIndex, + alias + i, + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .putNull(DataTier.TIER_PREFERENCE) // since we always enforce a tier preference, this will be ignored (i.e. + // data_content) + .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias + i) ); // the tier preference will have defaulted to data_content, set it back to null @@ -167,19 +180,20 @@ public void testMigrateToDataTiersAction() throws Exception { updateIndexSettings(indexWithDataWarmRouting, Settings.builder().putNull(DataTier.TIER_PREFERENCE)); Request migrateRequest = new Request("POST", "_ilm/migrate_to_data_tiers"); - migrateRequest.setJsonEntity( - "{\"legacy_template_to_delete\": \"" + templateName + "\", \"node_attribute\": \"data\"}" - ); + migrateRequest.setJsonEntity("{\"legacy_template_to_delete\": \"" + templateName + "\", \"node_attribute\": \"data\"}"); Response migrateDeploymentResponse = client().performRequest(migrateRequest); assertOK(migrateDeploymentResponse); Map migrateResponseAsMap = responseAsMap(migrateDeploymentResponse); - assertThat((List) migrateResponseAsMap.get(MigrateToDataTiersResponse.MIGRATED_ILM_POLICIES.getPreferredName()), - contains(policy)); - assertThat((List) migrateResponseAsMap.get(MigrateToDataTiersResponse.MIGRATED_INDICES.getPreferredName()), - containsInAnyOrder(index, indexWithDataWarmRouting, rolloverIndexPrefix + "-000001", rolloverIndexPrefix + "-000002")); - assertThat(migrateResponseAsMap.get(MigrateToDataTiersResponse.REMOVED_LEGACY_TEMPLATE.getPreferredName()), - is(templateName)); + assertThat( + (List) migrateResponseAsMap.get(MigrateToDataTiersResponse.MIGRATED_ILM_POLICIES.getPreferredName()), + contains(policy) + ); + assertThat( + (List) migrateResponseAsMap.get(MigrateToDataTiersResponse.MIGRATED_INDICES.getPreferredName()), + containsInAnyOrder(index, indexWithDataWarmRouting, rolloverIndexPrefix + "-000001", rolloverIndexPrefix + "-000002") + ); + assertThat(migrateResponseAsMap.get(MigrateToDataTiersResponse.REMOVED_LEGACY_TEMPLATE.getPreferredName()), is(templateName)); // let's verify the legacy template doesn't exist anymore Request getTemplateRequest = new Request("HEAD", "_template/" + templateName); @@ -211,8 +225,11 @@ public void testMigrateToDataTiersAction() throws Exception { String cachedPhaseDefinition = getCachedPhaseDefAsMap(clusterMetadataResponse, index); // let's also verify the cached phase definition was updated - as the managed index was in the warm phase, which after migration // does not contain the allocate action anymore, the cached warm phase should not contain the allocate action either - assertThat("the cached phase definition should reflect the migrated warm phase which must NOT contain an allocate action anymore", - cachedPhaseDefinition, not(containsString(AllocateAction.NAME))); + assertThat( + "the cached phase definition should reflect the migrated warm phase which must NOT contain an allocate action anymore", + cachedPhaseDefinition, + not(containsString(AllocateAction.NAME)) + ); assertThat(cachedPhaseDefinition, containsString(ShrinkAction.NAME)); assertThat(cachedPhaseDefinition, containsString(SetPriorityAction.NAME)); assertThat(cachedPhaseDefinition, containsString(ForceMergeAction.NAME)); @@ -242,7 +259,9 @@ public void testMigrationDryRun() throws Exception { coldActions.put(SetPriorityAction.NAME, new SetPriorityAction(0)); coldActions.put(AllocateAction.NAME, new AllocateAction(0, null, null, null, singletonMap("data", "cold"))); - createPolicy(client(), policy, + createPolicy( + client(), + policy, new Phase("hot", TimeValue.ZERO, hotActions), new Phase("warm", TimeValue.ZERO, warmActions), new Phase("cold", TimeValue.timeValueDays(100), coldActions), @@ -250,20 +269,26 @@ public void testMigrationDryRun() throws Exception { new Phase("delete", TimeValue.ZERO, singletonMap(DeleteAction.NAME, new DeleteAction())) ); - createIndexWithSettings(client(), index, alias, Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put(LifecycleSettings.LIFECYCLE_NAME, policy) - .putNull(DataTier.TIER_PREFERENCE) // since we always enforce a tier preference, this will be ignored (i.e. data_content) - .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias) + createIndexWithSettings( + client(), + index, + alias, + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put(LifecycleSettings.LIFECYCLE_NAME, policy) + .putNull(DataTier.TIER_PREFERENCE) // since we always enforce a tier preference, this will be ignored (i.e. data_content) + .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias) ); // wait for the index to advance to the warm phase - assertBusy(() -> - assertThat(getStepKeyForIndex(client(), index).getPhase(), equalTo("warm")), 30, TimeUnit.SECONDS); + assertBusy(() -> assertThat(getStepKeyForIndex(client(), index).getPhase(), equalTo("warm")), 30, TimeUnit.SECONDS); // let's wait for this index to have received the `require.data` configuration from the warm phase/allocate action - assertBusy(() -> - assertThat(getStepKeyForIndex(client(), index).getName(), equalTo(AllocationRoutedStep.NAME)), 30, TimeUnit.SECONDS); + assertBusy( + () -> assertThat(getStepKeyForIndex(client(), index).getName(), equalTo(AllocationRoutedStep.NAME)), + 30, + TimeUnit.SECONDS + ); // let's stop ILM so we can simulate the migration client().performRequest(new Request("POST", "_ilm/stop")); @@ -284,20 +309,21 @@ public void testMigrationDryRun() throws Exception { Request migrateRequest = new Request("POST", "_ilm/migrate_to_data_tiers"); migrateRequest.addParameter("dry_run", "true"); - migrateRequest.setJsonEntity( - "{\"legacy_template_to_delete\": \"" + templateName + "\", \"node_attribute\": \"data\"}" - ); + migrateRequest.setJsonEntity("{\"legacy_template_to_delete\": \"" + templateName + "\", \"node_attribute\": \"data\"}"); Response migrateDeploymentResponse = client().performRequest(migrateRequest); assertOK(migrateDeploymentResponse); // response should contain the correct "to migrate" entities Map migrateResponseAsMap = responseAsMap(migrateDeploymentResponse); - assertThat((List) migrateResponseAsMap.get(MigrateToDataTiersResponse.MIGRATED_ILM_POLICIES.getPreferredName()), - containsInAnyOrder(policy)); - assertThat((List) migrateResponseAsMap.get(MigrateToDataTiersResponse.MIGRATED_INDICES.getPreferredName()), - containsInAnyOrder(index, indexWithDataWarmRouting)); - assertThat(migrateResponseAsMap.get(MigrateToDataTiersResponse.REMOVED_LEGACY_TEMPLATE.getPreferredName()), - is(templateName)); + assertThat( + (List) migrateResponseAsMap.get(MigrateToDataTiersResponse.MIGRATED_ILM_POLICIES.getPreferredName()), + containsInAnyOrder(policy) + ); + assertThat( + (List) migrateResponseAsMap.get(MigrateToDataTiersResponse.MIGRATED_INDICES.getPreferredName()), + containsInAnyOrder(index, indexWithDataWarmRouting) + ); + assertThat(migrateResponseAsMap.get(MigrateToDataTiersResponse.REMOVED_LEGACY_TEMPLATE.getPreferredName()), is(templateName)); // however the entities should NOT have been changed // the index template should still exist @@ -334,17 +360,22 @@ private String getCachedPhaseDefAsMap(Response clusterMetadataResponse, String i private void createLegacyTemplate(String templateName) throws IOException { String indexPrefix = randomAlphaOfLengthBetween(5, 15).toLowerCase(Locale.ROOT); - final StringEntity template = new StringEntity("{\n" + - " \"index_patterns\": \"" + indexPrefix + "*\",\n" + - " \"settings\": {\n" + - " \"index\": {\n" + - " \"lifecycle\": {\n" + - " \"name\": \"does_not_exist\",\n" + - " \"rollover_alias\": \"test_alias\"\n" + - " }\n" + - " }\n" + - " }\n" + - "}", ContentType.APPLICATION_JSON); + final StringEntity template = new StringEntity( + "{\n" + + " \"index_patterns\": \"" + + indexPrefix + + "*\",\n" + + " \"settings\": {\n" + + " \"index\": {\n" + + " \"lifecycle\": {\n" + + " \"name\": \"does_not_exist\",\n" + + " \"rollover_alias\": \"test_alias\"\n" + + " }\n" + + " }\n" + + " }\n" + + "}", + ContentType.APPLICATION_JSON + ); Request templateRequest = new Request("PUT", "_template/" + templateName); templateRequest.setEntity(template); templateRequest.setOptions(expectWarnings(RestPutIndexTemplateAction.DEPRECATION_WARNING)); diff --git a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/TimeSeriesRestDriver.java b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/TimeSeriesRestDriver.java index 1f146d3f41933..5158519415d0e 100644 --- a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/TimeSeriesRestDriver.java +++ b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/TimeSeriesRestDriver.java @@ -15,13 +15,13 @@ import org.elasticsearch.client.Response; import org.elasticsearch.client.RestClient; import org.elasticsearch.cluster.metadata.Template; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.ilm.AllocateAction; @@ -45,12 +45,12 @@ import java.util.concurrent.TimeUnit; import static java.util.Collections.singletonMap; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.test.ESTestCase.randomAlphaOfLengthBetween; import static org.elasticsearch.test.ESTestCase.randomBoolean; import static org.elasticsearch.test.ESTestCase.waitUntil; import static org.elasticsearch.test.rest.ESRestTestCase.assertOK; import static org.elasticsearch.test.rest.ESRestTestCase.ensureHealth; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.xpack.core.ilm.ShrinkIndexNameSupplier.SHRUNKEN_INDEX_PREFIX; import static org.hamcrest.Matchers.anyOf; import static org.hamcrest.Matchers.equalTo; @@ -63,8 +63,7 @@ public final class TimeSeriesRestDriver { private static final Logger logger = LogManager.getLogger(TimeSeriesRestDriver.class); - private TimeSeriesRestDriver() { - } + private TimeSeriesRestDriver() {} public static Step.StepKey getStepKeyForIndex(RestClient client, String indexName) throws IOException { Map indexResponse = explainIndex(client, indexName); @@ -86,8 +85,8 @@ public static Map explainIndex(RestClient client, String indexNa return explain(client, indexName, false, false).get(indexName); } - public static Map> explain(RestClient client, String indexPattern, boolean onlyErrors, - boolean onlyManaged) throws IOException { + public static Map> explain(RestClient client, String indexPattern, boolean onlyErrors, boolean onlyManaged) + throws IOException { Request explainRequest = new Request("GET", indexPattern + "/_ilm/explain"); explainRequest.addParameter("only_errors", Boolean.toString(onlyErrors)); explainRequest.addParameter("only_managed", Boolean.toString(onlyManaged)); @@ -97,8 +96,8 @@ public static Map> explain(RestClient client, String responseMap = XContentHelper.convertToMap(XContentType.JSON.xContent(), is, true); } - @SuppressWarnings("unchecked") Map> indexResponse = - ((Map>) responseMap.get("indices")); + @SuppressWarnings("unchecked") + Map> indexResponse = ((Map>) responseMap.get("indices")); return indexResponse; } @@ -129,14 +128,18 @@ public static void createNewSingletonPolicy(RestClient client, String policyName createNewSingletonPolicy(client, policyName, phaseName, action, TimeValue.ZERO); } - public static void createNewSingletonPolicy(RestClient client, String policyName, String phaseName, LifecycleAction action, - TimeValue after) throws IOException { + public static void createNewSingletonPolicy( + RestClient client, + String policyName, + String phaseName, + LifecycleAction action, + TimeValue after + ) throws IOException { Phase phase = new Phase(phaseName, after, singletonMap(action.getWriteableName(), action)); LifecyclePolicy lifecyclePolicy = new LifecyclePolicy(policyName, singletonMap(phase.getName(), phase)); XContentBuilder builder = jsonBuilder(); lifecyclePolicy.toXContent(builder, null); - final StringEntity entity = new StringEntity( - "{ \"policy\":" + Strings.toString(builder) + "}", ContentType.APPLICATION_JSON); + final StringEntity entity = new StringEntity("{ \"policy\":" + Strings.toString(builder) + "}", ContentType.APPLICATION_JSON); Request request = new Request("PUT", "_ilm/policy/" + policyName); request.setEntity(entity); client.performRequest(request); @@ -147,12 +150,14 @@ public static void createComposableTemplate(RestClient client, String templateNa XContentBuilder builder = jsonBuilder(); template.toXContent(builder, ToXContent.EMPTY_PARAMS); StringEntity templateJSON = new StringEntity( - String.format(Locale.ROOT, "{\n" + - " \"index_patterns\": \"%s\",\n" + - " \"data_stream\": {},\n" + - " \"template\": %s\n" + - "}", indexPattern, Strings.toString(builder)), - ContentType.APPLICATION_JSON); + String.format( + Locale.ROOT, + "{\n" + " \"index_patterns\": \"%s\",\n" + " \"data_stream\": {},\n" + " \"template\": %s\n" + "}", + indexPattern, + Strings.toString(builder) + ), + ContentType.APPLICATION_JSON + ); Request createIndexTemplateRequest = new Request("PUT", "_index_template/" + templateName); createIndexTemplateRequest.setEntity(templateJSON); client.performRequest(createIndexTemplateRequest); @@ -160,12 +165,7 @@ public static void createComposableTemplate(RestClient client, String templateNa public static void rolloverMaxOneDocCondition(RestClient client, String indexAbstractionName) throws IOException { Request rolloverRequest = new Request("POST", "/" + indexAbstractionName + "/_rollover"); - rolloverRequest.setJsonEntity("{\n" + - " \"conditions\": {\n" + - " \"max_docs\": \"1\"\n" + - " }\n" + - "}" - ); + rolloverRequest.setJsonEntity("{\n" + " \"conditions\": {\n" + " \"max_docs\": \"1\"\n" + " }\n" + "}"); client.performRequest(rolloverRequest); } @@ -176,15 +176,29 @@ public static void createFullPolicy(RestClient client, String policyName, TimeVa Map warmActions = new HashMap<>(); warmActions.put(SetPriorityAction.NAME, new SetPriorityAction(50)); warmActions.put(ForceMergeAction.NAME, new ForceMergeAction(1, null)); - warmActions.put(AllocateAction.NAME, new AllocateAction(1, null, singletonMap("_name", "javaRestTest-0,javaRestTest-1," + - "javaRestTest-2," + - "javaRestTest-3"), null, null)); + warmActions.put( + AllocateAction.NAME, + new AllocateAction( + 1, + null, + singletonMap("_name", "javaRestTest-0,javaRestTest-1," + "javaRestTest-2," + "javaRestTest-3"), + null, + null + ) + ); warmActions.put(ShrinkAction.NAME, new ShrinkAction(1, null)); Map coldActions = new HashMap<>(); coldActions.put(SetPriorityAction.NAME, new SetPriorityAction(0)); - coldActions.put(AllocateAction.NAME, new AllocateAction(0, null, singletonMap("_name", "javaRestTest-0,javaRestTest-1," + - "javaRestTest-2," + - "javaRestTest-3"), null, null)); + coldActions.put( + AllocateAction.NAME, + new AllocateAction( + 0, + null, + singletonMap("_name", "javaRestTest-0,javaRestTest-1," + "javaRestTest-2," + "javaRestTest-3"), + null, + null + ) + ); Map phases = new HashMap<>(); phases.put("hot", new Phase("hot", hotTime, hotActions)); phases.put("warm", new Phase("warm", TimeValue.ZERO, warmActions)); @@ -194,16 +208,21 @@ public static void createFullPolicy(RestClient client, String policyName, TimeVa // PUT policy XContentBuilder builder = jsonBuilder(); lifecyclePolicy.toXContent(builder, null); - final StringEntity entity = new StringEntity( - "{ \"policy\":" + Strings.toString(builder) + "}", ContentType.APPLICATION_JSON); + final StringEntity entity = new StringEntity("{ \"policy\":" + Strings.toString(builder) + "}", ContentType.APPLICATION_JSON); Request request = new Request("PUT", "_ilm/policy/" + policyName); request.setEntity(entity); client.performRequest(request); } - public static void createPolicy(RestClient client, String policyName, @Nullable Phase hotPhase, - @Nullable Phase warmPhase, @Nullable Phase coldPhase, - @Nullable Phase frozenPhase, @Nullable Phase deletePhase) throws IOException { + public static void createPolicy( + RestClient client, + String policyName, + @Nullable Phase hotPhase, + @Nullable Phase warmPhase, + @Nullable Phase coldPhase, + @Nullable Phase frozenPhase, + @Nullable Phase deletePhase + ) throws IOException { if (hotPhase == null && warmPhase == null && coldPhase == null && deletePhase == null) { throw new IllegalArgumentException("specify at least one phase"); } @@ -226,8 +245,7 @@ public static void createPolicy(RestClient client, String policyName, @Nullable LifecyclePolicy lifecyclePolicy = new LifecyclePolicy(policyName, phases); XContentBuilder builder = jsonBuilder(); lifecyclePolicy.toXContent(builder, null); - final StringEntity entity = new StringEntity( - "{ \"policy\":" + Strings.toString(builder) + "}", ContentType.APPLICATION_JSON); + final StringEntity entity = new StringEntity("{ \"policy\":" + Strings.toString(builder) + "}", ContentType.APPLICATION_JSON); Request request = new Request("PUT", "_ilm/policy/" + policyName); request.setEntity(entity); client.performRequest(request); @@ -235,17 +253,20 @@ public static void createPolicy(RestClient client, String policyName, @Nullable public static void createSnapshotRepo(RestClient client, String repoName, boolean compress) throws IOException { Request request = new Request("PUT", "/_snapshot/" + repoName); - request.setJsonEntity(Strings - .toString(JsonXContent.contentBuilder() - .startObject() - .field("type", "fs") - .startObject("settings") - .field("compress", compress) - //random location to avoid clash with other snapshots - .field("location", System.getProperty("tests.path.repo") + "/" + randomAlphaOfLengthBetween(4, 10)) - .field("max_snapshot_bytes_per_sec", "100m") - .endObject() - .endObject())); + request.setJsonEntity( + Strings.toString( + JsonXContent.contentBuilder() + .startObject() + .field("type", "fs") + .startObject("settings") + .field("compress", compress) + // random location to avoid clash with other snapshots + .field("location", System.getProperty("tests.path.repo") + "/" + randomAlphaOfLengthBetween(4, 10)) + .field("max_snapshot_bytes_per_sec", "100m") + .endObject() + .endObject() + ) + ); client.performRequest(request); } @@ -269,16 +290,28 @@ public static void createIndexWithSettings(RestClient client, String index, Stri createIndexWithSettings(client, index, alias, settings, randomBoolean()); } - public static void createIndexWithSettings(RestClient client, String index, String alias, Settings.Builder settings, - boolean useWriteIndex) throws IOException { + public static void createIndexWithSettings( + RestClient client, + String index, + String alias, + Settings.Builder settings, + boolean useWriteIndex + ) throws IOException { Request request = new Request("PUT", "/" + index); String writeIndexSnippet = ""; if (useWriteIndex) { writeIndexSnippet = "\"is_write_index\": true"; } - request.setJsonEntity("{\n \"settings\": " + Strings.toString(settings.build()) - + ", \"aliases\" : { \"" + alias + "\": { " + writeIndexSnippet + " } } }"); + request.setJsonEntity( + "{\n \"settings\": " + + Strings.toString(settings.build()) + + ", \"aliases\" : { \"" + + alias + + "\": { " + + writeIndexSnippet + + " } } }" + ); client.performRequest(request); // wait for the shards to initialize ensureGreen(index); @@ -303,8 +336,11 @@ private static void ensureGreen(String index) throws IOException { public static Integer getNumberOfSegments(RestClient client, String index) throws IOException { Response response = client.performRequest(new Request("GET", index + "/_segments")); XContentType entityContentType = XContentType.fromMediaType(response.getEntity().getContentType().getValue()); - Map responseEntity = XContentHelper.convertToMap(entityContentType.xContent(), - response.getEntity().getContent(), false); + Map responseEntity = XContentHelper.convertToMap( + entityContentType.xContent(), + response.getEntity().getContent(), + false + ); responseEntity = (Map) responseEntity.get("indices"); responseEntity = (Map) responseEntity.get(index); responseEntity = (Map) responseEntity.get("shards"); @@ -314,8 +350,10 @@ public static Integer getNumberOfSegments(RestClient client, String index) throw public static void updatePolicy(RestClient client, String indexName, String policy) throws IOException { Request changePolicyRequest = new Request("PUT", "/" + indexName + "/_settings"); - final StringEntity changePolicyEntity = new StringEntity("{ \"index.lifecycle.name\": \"" + policy + "\" }", - ContentType.APPLICATION_JSON); + final StringEntity changePolicyEntity = new StringEntity( + "{ \"index.lifecycle.name\": \"" + policy + "\" }", + ContentType.APPLICATION_JSON + ); changePolicyRequest.setEntity(changePolicyEntity); assertOK(client.performRequest(changePolicyRequest)); } @@ -340,8 +378,10 @@ public static String waitAndGetShrinkIndexName(RestClient client, String origina waitUntil(() -> { try { // we're including here the case where the original index was already deleted and we have to look for the shrunken index - Request explainRequest = new Request("GET", SHRUNKEN_INDEX_PREFIX + "*" + originalIndex + "," + originalIndex - + "/_ilm/explain"); + Request explainRequest = new Request( + "GET", + SHRUNKEN_INDEX_PREFIX + "*" + originalIndex + "," + originalIndex + "/_ilm/explain" + ); explainRequest.addParameter("only_errors", Boolean.toString(false)); explainRequest.addParameter("only_managed", Boolean.toString(false)); Response response = client.performRequest(explainRequest); diff --git a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/ChangePolicyforIndexIT.java b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/ChangePolicyforIndexIT.java index 2783c21cd8ed6..a90a90b348490 100644 --- a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/ChangePolicyforIndexIT.java +++ b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/ChangePolicyforIndexIT.java @@ -14,9 +14,9 @@ import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.core.TimeValue; import org.elasticsearch.test.rest.ESRestTestCase; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ilm.AllocateAction; import org.elasticsearch.xpack.core.ilm.LifecyclePolicy; import org.elasticsearch.xpack.core.ilm.LifecycleSettings; @@ -63,15 +63,37 @@ public void testChangePolicyForIndex() throws Exception { // create policy_1 and policy_2 Map phases1 = new HashMap<>(); phases1.put("hot", new Phase("hot", TimeValue.ZERO, singletonMap(RolloverAction.NAME, new RolloverAction(null, null, null, 1L)))); - phases1.put("warm", new Phase("warm", TimeValue.ZERO, - singletonMap(AllocateAction.NAME, new AllocateAction(1, null, singletonMap("_name", "foobarbaz"), null, null)))); + phases1.put( + "warm", + new Phase( + "warm", + TimeValue.ZERO, + singletonMap(AllocateAction.NAME, new AllocateAction(1, null, singletonMap("_name", "foobarbaz"), null, null)) + ) + ); LifecyclePolicy lifecyclePolicy1 = new LifecyclePolicy("policy_1", phases1); Map phases2 = new HashMap<>(); - phases2.put("hot", new Phase("hot", TimeValue.ZERO, singletonMap(RolloverAction.NAME, new RolloverAction(null, null, null, 1000L)))); - phases2.put("warm", new Phase("warm", TimeValue.ZERO, - singletonMap(AllocateAction.NAME, - new AllocateAction(1, null, singletonMap("_name", "javaRestTest-0,javaRestTest-1,javaRestTest-2,javaRestTest-3"), - null, null)))); + phases2.put( + "hot", + new Phase("hot", TimeValue.ZERO, singletonMap(RolloverAction.NAME, new RolloverAction(null, null, null, 1000L))) + ); + phases2.put( + "warm", + new Phase( + "warm", + TimeValue.ZERO, + singletonMap( + AllocateAction.NAME, + new AllocateAction( + 1, + null, + singletonMap("_name", "javaRestTest-0,javaRestTest-1,javaRestTest-2,javaRestTest-3"), + null, + null + ) + ) + ) + ); LifecyclePolicy lifecyclePolicy2 = new LifecyclePolicy("policy_2", phases2); // PUT policy_1 and policy_2 XContentBuilder builder1 = jsonBuilder(); @@ -88,12 +110,17 @@ public void testChangePolicyForIndex() throws Exception { assertOK(client().performRequest(request2)); // create the test-index index and set the policy to policy_1 - Settings settings = Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 4) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).put("index.routing.allocation.include._name", "javaRestTest-0") - .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, "alias").put(LifecycleSettings.LIFECYCLE_NAME, "policy_1").build(); + Settings settings = Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 4) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put("index.routing.allocation.include._name", "javaRestTest-0") + .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, "alias") + .put(LifecycleSettings.LIFECYCLE_NAME, "policy_1") + .build(); Request createIndexRequest = new Request("PUT", "/" + indexName); createIndexRequest.setJsonEntity( - "{\n \"settings\": " + Strings.toString(settings) + ", \"aliases\" : { \"alias\": { \"is_write_index\": true } } }"); + "{\n \"settings\": " + Strings.toString(settings) + ", \"aliases\" : { \"alias\": { \"is_write_index\": true } } }" + ); client().performRequest(createIndexRequest); // wait for the shards to initialize ensureGreen(indexName); @@ -103,8 +130,10 @@ public void testChangePolicyForIndex() throws Exception { // Change the policy to policy_2 Request changePolicyRequest = new Request("PUT", "/" + indexName + "/_settings"); - final StringEntity changePolicyEntity = new StringEntity("{ \"index.lifecycle.name\": \"policy_2\" }", - ContentType.APPLICATION_JSON); + final StringEntity changePolicyEntity = new StringEntity( + "{ \"index.lifecycle.name\": \"policy_2\" }", + ContentType.APPLICATION_JSON + ); changePolicyRequest.setEntity(changePolicyEntity); assertOK(client().performRequest(changePolicyRequest)); @@ -134,22 +163,33 @@ public void testILMHonoursTheCachedPhaseAfterPolicyUpdate() throws Exception { String alias = "thealias"; createNewSingletonPolicy(client(), policyName, "hot", new RolloverAction(null, null, null, 1L)); - createIndexWithSettings(client(), indexName, alias, Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias) - .put(LifecycleSettings.LIFECYCLE_NAME, policyName)); + createIndexWithSettings( + client(), + indexName, + alias, + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias) + .put(LifecycleSettings.LIFECYCLE_NAME, policyName) + ); // Check the index is on the check-rollover-ready step - assertBusy(() -> assertStep(indexName, new StepKey("hot", RolloverAction.NAME, WaitForRolloverReadyStep.NAME)), 30, - TimeUnit.SECONDS); + assertBusy( + () -> assertStep(indexName, new StepKey("hot", RolloverAction.NAME, WaitForRolloverReadyStep.NAME)), + 30, + TimeUnit.SECONDS + ); // update the policy to not contain rollover createNewSingletonPolicy(client(), policyName, "hot", new SetPriorityAction(200)); // Check the index is on the check-rollover-ready step - assertBusy(() -> assertStep(indexName, new StepKey("hot", RolloverAction.NAME, WaitForRolloverReadyStep.NAME)), 30, - TimeUnit.SECONDS); + assertBusy( + () -> assertStep(indexName, new StepKey("hot", RolloverAction.NAME, WaitForRolloverReadyStep.NAME)), + 30, + TimeUnit.SECONDS + ); indexDocument(client(), indexName, true); @@ -164,8 +204,9 @@ private void assertStep(String indexName, StepKey expectedStep) throws IOExcepti assertOK(explainResponse); Map explainResponseMap = entityAsMap(explainResponse); @SuppressWarnings("unchecked") - Map indexExplainResponse = (Map) ((Map) explainResponseMap.get("indices")) - .get(indexName); + Map indexExplainResponse = (Map) ((Map) explainResponseMap.get("indices")).get( + indexName + ); assertEquals(expectedStep.getPhase(), indexExplainResponse.get("phase")); assertEquals(expectedStep.getAction(), indexExplainResponse.get("action")); assertEquals(expectedStep.getName(), indexExplainResponse.get("step")); diff --git a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/ExplainLifecycleIT.java b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/ExplainLifecycleIT.java index b2f58e88b80e2..1394adf379b8a 100644 --- a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/ExplainLifecycleIT.java +++ b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/ExplainLifecycleIT.java @@ -15,9 +15,9 @@ import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.core.TimeValue; import org.elasticsearch.test.rest.ESRestTestCase; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ilm.DeleteAction; import org.elasticsearch.xpack.core.ilm.LifecycleAction; import org.elasticsearch.xpack.core.ilm.LifecyclePolicy; @@ -79,33 +79,42 @@ public void testExplainFilters() throws Exception { // PUT policy XContentBuilder builder = jsonBuilder(); lifecyclePolicy.toXContent(builder, null); - final StringEntity entity = new StringEntity( - "{ \"policy\":" + Strings.toString(builder) + "}", ContentType.APPLICATION_JSON); + final StringEntity entity = new StringEntity("{ \"policy\":" + Strings.toString(builder) + "}", ContentType.APPLICATION_JSON); Request request = new Request("PUT", "_ilm/policy/shrink-only-policy"); request.setEntity(entity); assertOK(client().performRequest(request)); } - createIndexWithSettings(client(), goodIndex, alias, Settings.builder() - .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put(LifecycleSettings.LIFECYCLE_NAME, policy) + createIndexWithSettings( + client(), + goodIndex, + alias, + Settings.builder() + .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put(LifecycleSettings.LIFECYCLE_NAME, policy) ); - createIndexWithSettings(client(), errorIndex, Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put(LifecycleSettings.LIFECYCLE_NAME, "shrink-only-policy") + createIndexWithSettings( + client(), + errorIndex, + Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).put(LifecycleSettings.LIFECYCLE_NAME, "shrink-only-policy") ); - createIndexWithSettings(client(), nonexistantPolicyIndex, Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put(LifecycleSettings.LIFECYCLE_NAME, randomValueOtherThan(policy, () -> randomAlphaOfLengthBetween(3, 10)))); - createIndexWithSettings(client(), unmanagedIndex, Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)); + createIndexWithSettings( + client(), + nonexistantPolicyIndex, + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put(LifecycleSettings.LIFECYCLE_NAME, randomValueOtherThan(policy, () -> randomAlphaOfLengthBetween(3, 10))) + ); + createIndexWithSettings(client(), unmanagedIndex, Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)); assertBusy(() -> { Map> explainResponse = explain(client(), index + "*", false, false); assertNotNull(explainResponse); - assertThat(explainResponse, - allOf(hasKey(goodIndex), hasKey(errorIndex), hasKey(nonexistantPolicyIndex), hasKey(unmanagedIndex))); + assertThat( + explainResponse, + allOf(hasKey(goodIndex), hasKey(errorIndex), hasKey(nonexistantPolicyIndex), hasKey(unmanagedIndex)) + ); Map> onlyManagedResponse = explain(client(), index + "*", false, true); assertNotNull(onlyManagedResponse); @@ -123,9 +132,10 @@ public void testExplainIndexContainsAutomaticRetriesInformation() throws Excepti createFullPolicy(client(), policy, TimeValue.ZERO); // create index without alias so the rollover action fails and is retried - createIndexWithSettings(client(), index, Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put(LifecycleSettings.LIFECYCLE_NAME, policy) + createIndexWithSettings( + client(), + index, + Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).put(LifecycleSettings.LIFECYCLE_NAME, policy) ); assertBusy(() -> { @@ -142,22 +152,40 @@ public void testExplainIndicesWildcard() throws Exception { String secondIndex = this.index + "-second"; String unmanagedIndex = this.index + "-unmanaged"; String indexWithMissingPolicy = this.index + "-missing_policy"; - createIndexWithSettings(client(), firstIndex, alias + firstIndex, Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put(LifecycleSettings.LIFECYCLE_NAME, policy)); - createIndexWithSettings(client(), secondIndex, alias + secondIndex, Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put(LifecycleSettings.LIFECYCLE_NAME, policy)); - createIndexWithSettings(client(), unmanagedIndex, alias + unmanagedIndex, Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)); + createIndexWithSettings( + client(), + firstIndex, + alias + firstIndex, + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put(LifecycleSettings.LIFECYCLE_NAME, policy) + ); + createIndexWithSettings( + client(), + secondIndex, + alias + secondIndex, + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put(LifecycleSettings.LIFECYCLE_NAME, policy) + ); + createIndexWithSettings( + client(), + unmanagedIndex, + alias + unmanagedIndex, + Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + ); String missingPolicyName = "missing_policy_"; - createIndexWithSettings(client(), indexWithMissingPolicy, alias + indexWithMissingPolicy, Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put(LifecycleSettings.LIFECYCLE_NAME, missingPolicyName)); + createIndexWithSettings( + client(), + indexWithMissingPolicy, + alias + indexWithMissingPolicy, + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put(LifecycleSettings.LIFECYCLE_NAME, missingPolicyName) + ); assertBusy(() -> { Map> explain = explain(client(), this.index + "*", false, false); diff --git a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/LifecycleLicenseIT.java b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/LifecycleLicenseIT.java index 6468a4a249963..afaae559c1644 100644 --- a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/LifecycleLicenseIT.java +++ b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/LifecycleLicenseIT.java @@ -16,12 +16,12 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.license.License; import org.elasticsearch.license.TestUtils; import org.elasticsearch.test.rest.ESRestTestCase; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.ilm.ErrorStep; import org.elasticsearch.xpack.core.ilm.LifecycleSettings; import org.elasticsearch.xpack.core.ilm.PhaseCompleteStep; @@ -69,12 +69,21 @@ public void testCreatePolicyUsingActionAndNonCompliantLicense() throws Exception assertOK(client().performRequest(new Request("DELETE", "/_license"))); checkCurrentLicenseIs("basic"); - ResponseException exception = expectThrows(ResponseException.class, - () -> createNewSingletonPolicy(client(), policy, "cold", - new SearchableSnapshotAction(snapshotRepo, true))); - assertThat(EntityUtils.toString(exception.getResponse().getEntity()), - containsStringIgnoringCase("policy [" + policy + "] defines the [" + SearchableSnapshotAction.NAME + "] action but the " + - "current license is non-compliant for [searchable-snapshots]")); + ResponseException exception = expectThrows( + ResponseException.class, + () -> createNewSingletonPolicy(client(), policy, "cold", new SearchableSnapshotAction(snapshotRepo, true)) + ); + assertThat( + EntityUtils.toString(exception.getResponse().getEntity()), + containsStringIgnoringCase( + "policy [" + + policy + + "] defines the [" + + SearchableSnapshotAction.NAME + + "] action but the " + + "current license is non-compliant for [searchable-snapshots]" + ) + ); } @SuppressWarnings("unchecked") @@ -83,8 +92,12 @@ public void testSearchableSnapshotActionErrorsOnInvalidLicense() throws Exceptio createSnapshotRepo(client(), snapshotRepo, randomBoolean()); createNewSingletonPolicy(client(), policy, "cold", new SearchableSnapshotAction(snapshotRepo, true)); - createComposableTemplate(client(), "template-name", dataStream, - new Template(Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, policy).build(), null, null)); + createComposableTemplate( + client(), + "template-name", + dataStream, + new Template(Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, policy).build(), null, null) + ); assertOK(client().performRequest(new Request("DELETE", "/_license"))); checkCurrentLicenseIs("basic"); @@ -106,8 +119,10 @@ public void testSearchableSnapshotActionErrorsOnInvalidLicense() throws Exceptio // until the failed step is executed successfully). // So, *if* we catch ILM in the ERROR step, we check the failed message if (ErrorStep.NAME.equals(explainIndex.get("step"))) { - assertThat(((Map) explainIndex.get("step_info")).get("reason"), - containsStringIgnoringCase("current license is non-compliant for [searchable-snapshots]")); + assertThat( + ((Map) explainIndex.get("step_info")).get("reason"), + containsStringIgnoringCase("current license is non-compliant for [searchable-snapshots]") + ); } }, 30, TimeUnit.SECONDS); @@ -124,8 +139,11 @@ public void testSearchableSnapshotActionErrorsOnInvalidLicense() throws Exceptio } }, 30, TimeUnit.SECONDS)); - assertBusy(() -> assertThat(explainIndex(client(), restoredIndexName).get("step"), is(PhaseCompleteStep.NAME)), 30, - TimeUnit.SECONDS); + assertBusy( + () -> assertThat(explainIndex(client(), restoredIndexName).get("step"), is(PhaseCompleteStep.NAME)), + 30, + TimeUnit.SECONDS + ); } private void putTrialLicense() throws Exception { @@ -142,12 +160,11 @@ private void putTrialLicense() throws Exception { } private void checkCurrentLicenseIs(String type) throws Exception { - assertBusy(() -> { + assertBusy(() -> { Response getLicense = client().performRequest(new Request("GET", "/_license")); String responseBody = EntityUtils.toString(getLicense.getEntity()); logger.info("get license response body is [{}]", responseBody); - assertThat(responseBody, - containsStringIgnoringCase("\"type\" : \"" + type + "\"")); + assertThat(responseBody, containsStringIgnoringCase("\"type\" : \"" + type + "\"")); }); } } diff --git a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/TimeSeriesDataStreamsIT.java b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/TimeSeriesDataStreamsIT.java index 8a903a289d2c7..f87c5262daf27 100644 --- a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/TimeSeriesDataStreamsIT.java +++ b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/TimeSeriesDataStreamsIT.java @@ -14,8 +14,8 @@ import org.elasticsearch.cluster.metadata.Template; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.test.rest.ESRestTestCase; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.ilm.CheckNotDataStreamWriteIndexStep; import org.elasticsearch.xpack.core.ilm.DeleteAction; import org.elasticsearch.xpack.core.ilm.ForceMergeAction; @@ -60,11 +60,15 @@ public void refreshAbstractions() { policyName = "policy-" + randomAlphaOfLength(5); dataStream = "logs-" + randomAlphaOfLength(10).toLowerCase(Locale.ROOT); template = "template-" + randomAlphaOfLength(10).toLowerCase(Locale.ROOT); - logger.info("--> running [{}] with data stream [{}], template [{}] and policy [{}]", getTestName(), dataStream, template, - policyName); + logger.info( + "--> running [{}] with data stream [{}], template [{}] and policy [{}]", + getTestName(), + dataStream, + template, + policyName + ); } - public void testRolloverAction() throws Exception { createNewSingletonPolicy(client(), policyName, "hot", new RolloverAction(null, null, null, 1L)); @@ -73,10 +77,19 @@ public void testRolloverAction() throws Exception { indexDocument(client(), dataStream, true); assertBusy(() -> assertTrue(indexExists(DataStream.getDefaultBackingIndexName(dataStream, 2)))); - assertBusy(() -> assertTrue(Boolean.parseBoolean((String) getIndexSettingsAsMap( - DataStream.getDefaultBackingIndexName(dataStream, 2)).get("index.hidden")))); - assertBusy(() -> assertThat(getStepKeyForIndex(client(), DataStream.getDefaultBackingIndexName(dataStream, 1)), - equalTo(PhaseCompleteStep.finalStep("hot").getKey()))); + assertBusy( + () -> assertTrue( + Boolean.parseBoolean( + (String) getIndexSettingsAsMap(DataStream.getDefaultBackingIndexName(dataStream, 2)).get("index.hidden") + ) + ) + ); + assertBusy( + () -> assertThat( + getStepKeyForIndex(client(), DataStream.getDefaultBackingIndexName(dataStream, 1)), + equalTo(PhaseCompleteStep.finalStep("hot").getKey()) + ) + ); } public void testRolloverIsSkippedOnManualDataStreamRollover() throws Exception { @@ -87,28 +100,40 @@ public void testRolloverIsSkippedOnManualDataStreamRollover() throws Exception { indexDocument(client(), dataStream, true); String firstGenerationIndex = DataStream.getDefaultBackingIndexName(dataStream, 1); - assertBusy(() -> assertThat(getStepKeyForIndex(client(), firstGenerationIndex).getName(), - equalTo(WaitForRolloverReadyStep.NAME)), 30, TimeUnit.SECONDS); + assertBusy( + () -> assertThat(getStepKeyForIndex(client(), firstGenerationIndex).getName(), equalTo(WaitForRolloverReadyStep.NAME)), + 30, + TimeUnit.SECONDS + ); rolloverMaxOneDocCondition(client(), dataStream); assertBusy(() -> assertThat(indexExists(DataStream.getDefaultBackingIndexName(dataStream, 2)), is(true)), 30, TimeUnit.SECONDS); // even though the first index doesn't have 2 documents to fulfill the rollover condition, it should complete the rollover action // because it's not the write index anymore - assertBusy(() -> assertThat(getStepKeyForIndex(client(), firstGenerationIndex), - equalTo(PhaseCompleteStep.finalStep("hot").getKey())), 30, TimeUnit.SECONDS); + assertBusy( + () -> assertThat(getStepKeyForIndex(client(), firstGenerationIndex), equalTo(PhaseCompleteStep.finalStep("hot").getKey())), + 30, + TimeUnit.SECONDS + ); } @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/70595") public void testShrinkActionInPolicyWithoutHotPhase() throws Exception { createNewSingletonPolicy(client(), policyName, "warm", new ShrinkAction(1, null)); - createComposableTemplate(client(), template, dataStream + "*", getTemplate(policyName)); + createComposableTemplate(client(), template, dataStream + "*", getTemplate(policyName)); indexDocument(client(), dataStream, true); String backingIndexName = DataStream.getDefaultBackingIndexName(dataStream, 1); - assertBusy(() -> assertThat( - "original index must wait in the " + CheckNotDataStreamWriteIndexStep.NAME + " until it is not the write index anymore", - explainIndex(client(), backingIndexName).get("step"), is(CheckNotDataStreamWriteIndexStep.NAME)), 30, TimeUnit.SECONDS); + assertBusy( + () -> assertThat( + "original index must wait in the " + CheckNotDataStreamWriteIndexStep.NAME + " until it is not the write index anymore", + explainIndex(client(), backingIndexName).get("step"), + is(CheckNotDataStreamWriteIndexStep.NAME) + ), + 30, + TimeUnit.SECONDS + ); // Manual rollover the original index such that it's not the write index in the data stream anymore rolloverMaxOneDocCondition(client(), dataStream); @@ -127,63 +152,89 @@ public void testSearchableSnapshotAction() throws Exception { createSnapshotRepo(client(), snapshotRepo, randomBoolean()); createNewSingletonPolicy(client(), policyName, "cold", new SearchableSnapshotAction(snapshotRepo)); - createComposableTemplate(client(), template, dataStream + "*", getTemplate(policyName)); + createComposableTemplate(client(), template, dataStream + "*", getTemplate(policyName)); indexDocument(client(), dataStream, true); String backingIndexName = DataStream.getDefaultBackingIndexName(dataStream, 1); String restoredIndexName = SearchableSnapshotAction.FULL_RESTORED_INDEX_PREFIX + backingIndexName; - assertBusy(() -> assertThat( - "original index must wait in the " + CheckNotDataStreamWriteIndexStep.NAME + " until it is not the write index anymore", - explainIndex(client(), backingIndexName).get("step"), is(CheckNotDataStreamWriteIndexStep.NAME)), - 30, TimeUnit.SECONDS); + assertBusy( + () -> assertThat( + "original index must wait in the " + CheckNotDataStreamWriteIndexStep.NAME + " until it is not the write index anymore", + explainIndex(client(), backingIndexName).get("step"), + is(CheckNotDataStreamWriteIndexStep.NAME) + ), + 30, + TimeUnit.SECONDS + ); // Manual rollover the original index such that it's not the write index in the data stream anymore rolloverMaxOneDocCondition(client(), dataStream); assertBusy(() -> assertThat(indexExists(restoredIndexName), is(true))); assertBusy(() -> assertFalse(indexExists(backingIndexName)), 60, TimeUnit.SECONDS); - assertBusy(() -> assertThat(explainIndex(client(), restoredIndexName).get("step"), is(PhaseCompleteStep.NAME)), 30, - TimeUnit.SECONDS); + assertBusy( + () -> assertThat(explainIndex(client(), restoredIndexName).get("step"), is(PhaseCompleteStep.NAME)), + 30, + TimeUnit.SECONDS + ); } public void testReadOnlyAction() throws Exception { createNewSingletonPolicy(client(), policyName, "warm", new ReadOnlyAction()); - createComposableTemplate(client(), template, dataStream + "*", getTemplate(policyName)); + createComposableTemplate(client(), template, dataStream + "*", getTemplate(policyName)); indexDocument(client(), dataStream, true); String backingIndexName = DataStream.getDefaultBackingIndexName(dataStream, 1); - assertBusy(() -> assertThat( - "index must wait in the " + CheckNotDataStreamWriteIndexStep.NAME + " until it is not the write index anymore", - explainIndex(client(), backingIndexName).get("step"), is(CheckNotDataStreamWriteIndexStep.NAME)), - 30, TimeUnit.SECONDS); + assertBusy( + () -> assertThat( + "index must wait in the " + CheckNotDataStreamWriteIndexStep.NAME + " until it is not the write index anymore", + explainIndex(client(), backingIndexName).get("step"), + is(CheckNotDataStreamWriteIndexStep.NAME) + ), + 30, + TimeUnit.SECONDS + ); // Manual rollover the original index such that it's not the write index in the data stream anymore rolloverMaxOneDocCondition(client(), dataStream); - assertBusy(() -> assertThat(explainIndex(client(), backingIndexName).get("step"), is(PhaseCompleteStep.NAME)), 30, - TimeUnit.SECONDS); - assertThat(getOnlyIndexSettings(client(), backingIndexName).get(IndexMetadata.INDEX_BLOCKS_WRITE_SETTING.getKey()), - equalTo("true")); + assertBusy( + () -> assertThat(explainIndex(client(), backingIndexName).get("step"), is(PhaseCompleteStep.NAME)), + 30, + TimeUnit.SECONDS + ); + assertThat( + getOnlyIndexSettings(client(), backingIndexName).get(IndexMetadata.INDEX_BLOCKS_WRITE_SETTING.getKey()), + equalTo("true") + ); } public void testFreezeAction() throws Exception { createNewSingletonPolicy(client(), policyName, "cold", new FreezeAction()); - createComposableTemplate(client(), template, dataStream + "*", getTemplate(policyName)); + createComposableTemplate(client(), template, dataStream + "*", getTemplate(policyName)); indexDocument(client(), dataStream, true); String backingIndexName = DataStream.getDefaultBackingIndexName(dataStream, 1); - assertBusy(() -> assertThat( - "index must wait in the " + CheckNotDataStreamWriteIndexStep.NAME + " until it is not the write index anymore", - explainIndex(client(), backingIndexName).get("step"), is(CheckNotDataStreamWriteIndexStep.NAME)), - 30, TimeUnit.SECONDS); + assertBusy( + () -> assertThat( + "index must wait in the " + CheckNotDataStreamWriteIndexStep.NAME + " until it is not the write index anymore", + explainIndex(client(), backingIndexName).get("step"), + is(CheckNotDataStreamWriteIndexStep.NAME) + ), + 30, + TimeUnit.SECONDS + ); // Manual rollover the original index such that it's not the write index in the data stream anymore rolloverMaxOneDocCondition(client(), dataStream); - assertBusy(() -> assertThat(explainIndex(client(), backingIndexName).get("step"), is(PhaseCompleteStep.NAME)), 30, - TimeUnit.SECONDS); + assertBusy( + () -> assertThat(explainIndex(client(), backingIndexName).get("step"), is(PhaseCompleteStep.NAME)), + 30, + TimeUnit.SECONDS + ); Map settings = getOnlyIndexSettings(client(), backingIndexName); assertNull(settings.get("index.frozen")); @@ -191,25 +242,33 @@ public void testFreezeAction() throws Exception { public void testForceMergeAction() throws Exception { createNewSingletonPolicy(client(), policyName, "warm", new ForceMergeAction(1, null)); - createComposableTemplate(client(), template, dataStream + "*", getTemplate(policyName)); + createComposableTemplate(client(), template, dataStream + "*", getTemplate(policyName)); indexDocument(client(), dataStream, true); String backingIndexName = DataStream.getDefaultBackingIndexName(dataStream, 1); - assertBusy(() -> assertThat( - "index must wait in the " + CheckNotDataStreamWriteIndexStep.NAME + " until it is not the write index anymore", - explainIndex(client(), backingIndexName).get("step"), is(CheckNotDataStreamWriteIndexStep.NAME)), - 30, TimeUnit.SECONDS); + assertBusy( + () -> assertThat( + "index must wait in the " + CheckNotDataStreamWriteIndexStep.NAME + " until it is not the write index anymore", + explainIndex(client(), backingIndexName).get("step"), + is(CheckNotDataStreamWriteIndexStep.NAME) + ), + 30, + TimeUnit.SECONDS + ); // Manual rollover the original index such that it's not the write index in the data stream anymore rolloverMaxOneDocCondition(client(), dataStream); - assertBusy(() -> assertThat(explainIndex(client(), backingIndexName).get("step"), is(PhaseCompleteStep.NAME)), 30, - TimeUnit.SECONDS); + assertBusy( + () -> assertThat(explainIndex(client(), backingIndexName).get("step"), is(PhaseCompleteStep.NAME)), + 30, + TimeUnit.SECONDS + ); } @SuppressWarnings("unchecked") public void testGetDataStreamReturnsILMPolicy() throws Exception { - createComposableTemplate(client(), template, dataStream + "*", getTemplate(policyName)); + createComposableTemplate(client(), template, dataStream + "*", getTemplate(policyName)); indexDocument(client(), dataStream, true); Request explainRequest = new Request("GET", "/_data_stream/" + dataStream); @@ -242,9 +301,6 @@ private static Template getTemplate(String policyName) throws IOException { } private static Settings getLifecycleSettings(String policyName) { - return Settings.builder() - .put(LifecycleSettings.LIFECYCLE_NAME, policyName) - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 2) - .build(); + return Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, policyName).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 2).build(); } } diff --git a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/TimeSeriesLifecycleActionsIT.java b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/TimeSeriesLifecycleActionsIT.java index 39da9598259a8..5303490530c48 100644 --- a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/TimeSeriesLifecycleActionsIT.java +++ b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/TimeSeriesLifecycleActionsIT.java @@ -18,10 +18,7 @@ import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.core.CheckedRunnable; import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; @@ -29,6 +26,9 @@ import org.elasticsearch.rest.action.admin.indices.RestPutIndexTemplateAction; import org.elasticsearch.snapshots.SnapshotState; import org.elasticsearch.test.rest.ESRestTestCase; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.ilm.AllocateAction; import org.elasticsearch.xpack.core.ilm.DeleteAction; import org.elasticsearch.xpack.core.ilm.ForceMergeAction; @@ -97,10 +97,16 @@ public void testFullPolicy() throws Exception { String originalIndex = index + "-000001"; String shrunkenOriginalIndex = SHRUNKEN_INDEX_PREFIX + originalIndex; String secondIndex = index + "-000002"; - createIndexWithSettings(client(), originalIndex, alias, Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 2) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put("index.routing.allocation.include._name", "javaRestTest-0") - .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias)); + createIndexWithSettings( + client(), + originalIndex, + alias, + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 2) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put("index.routing.allocation.include._name", "javaRestTest-0") + .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias) + ); // create policy createFullPolicy(client(), policy, TimeValue.ZERO); @@ -125,14 +131,22 @@ public void testFullPolicy() throws Exception { public void testRetryFailedDeleteAction() throws Exception { createNewSingletonPolicy(client(), policy, "delete", new DeleteAction()); - createIndexWithSettings(client(), index, alias, Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put(IndexMetadata.SETTING_READ_ONLY, true) - .put("index.lifecycle.name", policy)); - - assertBusy(() -> assertThat((Integer) explainIndex(client(), index).get(FAILED_STEP_RETRY_COUNT_FIELD), greaterThanOrEqualTo(1)), - 30, TimeUnit.SECONDS); + createIndexWithSettings( + client(), + index, + alias, + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put(IndexMetadata.SETTING_READ_ONLY, true) + .put("index.lifecycle.name", policy) + ); + + assertBusy( + () -> assertThat((Integer) explainIndex(client(), index).get(FAILED_STEP_RETRY_COUNT_FIELD), greaterThanOrEqualTo(1)), + 30, + TimeUnit.SECONDS + ); assertTrue(indexExists(index)); Request request = new Request("PUT", index + "/_settings"); @@ -145,27 +159,41 @@ public void testRetryFailedDeleteAction() throws Exception { public void testFreezeNoop() throws Exception { createNewSingletonPolicy(client(), policy, "cold", new FreezeAction()); - createIndexWithSettings(client(), index, alias, Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put("index.lifecycle.name", policy)); - - assertBusy(() -> assertThat(getStepKeyForIndex(client(), index), equalTo(PhaseCompleteStep.finalStep("cold").getKey())), - 30, TimeUnit.SECONDS); + createIndexWithSettings( + client(), + index, + alias, + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put("index.lifecycle.name", policy) + ); + + assertBusy( + () -> assertThat(getStepKeyForIndex(client(), index), equalTo(PhaseCompleteStep.finalStep("cold").getKey())), + 30, + TimeUnit.SECONDS + ); assertFalse(getOnlyIndexSettings(client(), index).containsKey("index.frozen")); } - public void testAllocateOnlyAllocation() throws Exception { - createIndexWithSettings(client(), index, alias, Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 2) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)); + createIndexWithSettings( + client(), + index, + alias, + Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 2).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + ); String allocateNodeName = "javaRestTest-0,javaRestTest-1,javaRestTest-2,javaRestTest-3"; AllocateAction allocateAction = new AllocateAction(null, null, singletonMap("_name", allocateNodeName), null, null); String endPhase = randomFrom("warm", "cold"); createNewSingletonPolicy(client(), policy, endPhase, allocateAction); updatePolicy(client(), index, policy); - assertBusy(() -> assertThat(getStepKeyForIndex(client(), index), equalTo(PhaseCompleteStep.finalStep(endPhase).getKey())), - 30, TimeUnit.SECONDS); + assertBusy( + () -> assertThat(getStepKeyForIndex(client(), index), equalTo(PhaseCompleteStep.finalStep(endPhase).getKey())), + 30, + TimeUnit.SECONDS + ); ensureGreen(index); } @@ -173,8 +201,14 @@ public void testAllocateActionOnlyReplicas() throws Exception { int numShards = randomFrom(1, 5); int numReplicas = randomFrom(0, 1); int finalNumReplicas = (numReplicas + 1) % 2; - createIndexWithSettings(client(), index, alias, Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, numShards) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, numReplicas)); + createIndexWithSettings( + client(), + index, + alias, + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, numShards) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, numReplicas) + ); AllocateAction allocateAction = new AllocateAction(finalNumReplicas, null, null, null, null); String endPhase = randomFrom("warm", "cold"); createNewSingletonPolicy(client(), policy, endPhase, allocateAction); @@ -187,8 +221,12 @@ public void testAllocateActionOnlyReplicas() throws Exception { } public void testWaitForSnapshot() throws Exception { - createIndexWithSettings(client(), index, alias, Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)); + createIndexWithSettings( + client(), + index, + alias, + Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + ); String slmPolicy = randomAlphaOfLengthBetween(4, 10); String snapshotRepo = randomAlphaOfLengthBetween(4, 10); createSnapshotRepo(client(), snapshotRepo, randomBoolean()); @@ -207,7 +245,7 @@ public void testWaitForSnapshot() throws Exception { createSlmPolicy(slmPolicy, snapshotRepo); // put the slm policy back assertBusy(() -> { Map indexILMState = explainIndex(client(), index); - //wait for step to notice that the slm policy is created and to get out of error + // wait for step to notice that the slm policy is created and to get out of error assertThat(indexILMState.get("failed_step"), nullValue()); assertThat(indexILMState.get("action"), is("wait_for_snapshot")); assertThat(indexILMState.get("step"), is("wait-for-snapshot")); @@ -228,8 +266,12 @@ public void testWaitForSnapshot() throws Exception { * time gets set to a time earlier than the policy's action's time. */ public void testWaitForSnapshotFast() throws Exception { - createIndexWithSettings(client(), index, alias, Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)); + createIndexWithSettings( + client(), + index, + alias, + Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + ); String slmPolicy = randomAlphaOfLengthBetween(4, 10); String snapshotRepo = randomAlphaOfLengthBetween(4, 10); createSnapshotRepo(client(), snapshotRepo, randomBoolean()); @@ -250,8 +292,12 @@ public void testWaitForSnapshotFast() throws Exception { } public void testWaitForSnapshotSlmExecutedBefore() throws Exception { - createIndexWithSettings(client(), index, alias, Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)); + createIndexWithSettings( + client(), + index, + alias, + Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + ); String slmPolicy = randomAlphaOfLengthBetween(4, 10); String snapshotRepo = randomAlphaOfLengthBetween(4, 10); createSnapshotRepo(client(), snapshotRepo, randomBoolean()); @@ -263,7 +309,7 @@ public void testWaitForSnapshotSlmExecutedBefore() throws Exception { Request request = new Request("PUT", "/_slm/policy/" + slmPolicy + "/_execute"); assertOK(client().performRequest(request)); - //wait for slm to finish execution + // wait for slm to finish execution assertBusy(() -> { Response response = client().performRequest(new Request("GET", "/_slm/policy/" + slmPolicy)); try (InputStream is = response.getEntity().getContent()) { @@ -285,7 +331,7 @@ public void testWaitForSnapshotSlmExecutedBefore() throws Exception { request = new Request("PUT", "/_slm/policy/" + slmPolicy + "/_execute"); assertOK(client().performRequest(request)); - //wait for slm to finish execution + // wait for slm to finish execution assertBusy(() -> { Response response = client().performRequest(new Request("GET", "/_slm/policy/" + slmPolicy)); try (InputStream is = response.getEntity().getContent()) { @@ -325,16 +371,24 @@ private void waitForPhaseTime(String phaseName) throws Exception { } public void testDelete() throws Exception { - createIndexWithSettings(client(), index, alias, Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)); + createIndexWithSettings( + client(), + index, + alias, + Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + ); createNewSingletonPolicy(client(), policy, "delete", new DeleteAction()); updatePolicy(client(), index, policy); assertBusy(() -> assertFalse(indexExists(index))); } public void testDeleteOnlyShouldNotMakeIndexReadonly() throws Exception { - createIndexWithSettings(client(), index, alias, Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)); + createIndexWithSettings( + client(), + index, + alias, + Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + ); createNewSingletonPolicy(client(), policy, "delete", new DeleteAction(), TimeValue.timeValueHours(1)); updatePolicy(client(), index, policy); assertBusy(() -> { @@ -348,22 +402,29 @@ public void testDeleteOnlyShouldNotMakeIndexReadonly() throws Exception { public void testDeleteDuringSnapshot() throws Exception { // Create the repository before taking the snapshot. Request request = new Request("PUT", "/_snapshot/repo"); - request.setJsonEntity(Strings - .toString(JsonXContent.contentBuilder() - .startObject() - .field("type", "fs") - .startObject("settings") - .field("compress", randomBoolean()) - .field("location", System.getProperty("tests.path.repo")) - .field("max_snapshot_bytes_per_sec", "256b") - .endObject() - .endObject())); + request.setJsonEntity( + Strings.toString( + JsonXContent.contentBuilder() + .startObject() + .field("type", "fs") + .startObject("settings") + .field("compress", randomBoolean()) + .field("location", System.getProperty("tests.path.repo")) + .field("max_snapshot_bytes_per_sec", "256b") + .endObject() + .endObject() + ) + ); assertOK(client().performRequest(request)); // create delete policy createNewSingletonPolicy(client(), policy, "delete", new DeleteAction(), TimeValue.timeValueMillis(0)); // create index without policy - createIndexWithSettings(client(), index, alias, Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)); + createIndexWithSettings( + client(), + index, + alias, + Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + ); // index document so snapshot actually does something indexDocument(client(), index); // start snapshot @@ -382,8 +443,12 @@ public void testDeleteDuringSnapshot() throws Exception { } public void forceMergeActionWithCodec(String codec) throws Exception { - createIndexWithSettings(client(), index, alias, Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)); + createIndexWithSettings( + client(), + index, + alias, + Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + ); for (int i = 0; i < randomIntBetween(2, 10); i++) { Request request = new Request("PUT", index + "/_doc/" + i); request.addParameter("refresh", "true"); @@ -413,8 +478,15 @@ public void testForceMergeActionWithCompressionCodec() throws Exception { } public void testSetPriority() throws Exception { - createIndexWithSettings(client(), index, alias, Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetadata.INDEX_PRIORITY_SETTING.getKey(), 100)); + createIndexWithSettings( + client(), + index, + alias, + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put(IndexMetadata.INDEX_PRIORITY_SETTING.getKey(), 100) + ); int priority = randomIntBetween(0, 99); createNewSingletonPolicy(client(), policy, "warm", new SetPriorityAction(priority)); updatePolicy(client(), index, policy); @@ -426,8 +498,15 @@ public void testSetPriority() throws Exception { } public void testSetNullPriority() throws Exception { - createIndexWithSettings(client(), index, alias, Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetadata.INDEX_PRIORITY_SETTING.getKey(), 100)); + createIndexWithSettings( + client(), + index, + alias, + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put(IndexMetadata.INDEX_PRIORITY_SETTING.getKey(), 100) + ); createNewSingletonPolicy(client(), policy, "warm", new SetPriorityAction((Integer) null)); updatePolicy(client(), index, policy); assertBusy(() -> { @@ -440,17 +519,22 @@ public void testSetNullPriority() throws Exception { @SuppressWarnings("unchecked") public void testNonexistentPolicy() throws Exception { String indexPrefix = randomAlphaOfLengthBetween(5, 15).toLowerCase(Locale.ROOT); - final StringEntity template = new StringEntity("{\n" + - " \"index_patterns\": \"" + indexPrefix + "*\",\n" + - " \"settings\": {\n" + - " \"index\": {\n" + - " \"lifecycle\": {\n" + - " \"name\": \"does_not_exist\",\n" + - " \"rollover_alias\": \"test_alias\"\n" + - " }\n" + - " }\n" + - " }\n" + - "}", ContentType.APPLICATION_JSON); + final StringEntity template = new StringEntity( + "{\n" + + " \"index_patterns\": \"" + + indexPrefix + + "*\",\n" + + " \"settings\": {\n" + + " \"index\": {\n" + + " \"lifecycle\": {\n" + + " \"name\": \"does_not_exist\",\n" + + " \"rollover_alias\": \"test_alias\"\n" + + " }\n" + + " }\n" + + " }\n" + + "}", + ContentType.APPLICATION_JSON + ); Request templateRequest = new Request("PUT", "_template/test"); templateRequest.setEntity(template); templateRequest.setOptions(expectWarnings(RestPutIndexTemplateAction.DEPRECATION_WARNING)); @@ -460,13 +544,10 @@ public void testNonexistentPolicy() throws Exception { createNewSingletonPolicy(client(), policy, "hot", new RolloverAction(null, null, null, 1L)); index = indexPrefix + "-000001"; - final StringEntity putIndex = new StringEntity("{\n" + - " \"aliases\": {\n" + - " \"test_alias\": {\n" + - " \"is_write_index\": true\n" + - " }\n" + - " }\n" + - "}", ContentType.APPLICATION_JSON); + final StringEntity putIndex = new StringEntity( + "{\n" + " \"aliases\": {\n" + " \"test_alias\": {\n" + " \"is_write_index\": true\n" + " }\n" + " }\n" + "}", + ContentType.APPLICATION_JSON + ); Request putIndexRequest = new Request("PUT", index); putIndexRequest.setEntity(putIndex); client().performRequest(putIndexRequest); @@ -523,27 +604,45 @@ public void testDeletePolicyInUse() throws IOException { policy = otherPolicy; createNewSingletonPolicy(client(), policy, "delete", new DeleteAction(), TimeValue.timeValueHours(13)); - createIndexWithSettings(client(), managedIndex1, Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 10)) - .put(LifecycleSettings.LIFECYCLE_NAME_SETTING.getKey(), originalPolicy)); - createIndexWithSettings(client(), managedIndex2, Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 10)) - .put(LifecycleSettings.LIFECYCLE_NAME_SETTING.getKey(), originalPolicy)); - createIndexWithSettings(client(), unmanagedIndex, Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 10))); - createIndexWithSettings(client(), managedByOtherPolicyIndex, Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 10)) - .put(LifecycleSettings.LIFECYCLE_NAME_SETTING.getKey(), otherPolicy)); + createIndexWithSettings( + client(), + managedIndex1, + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 10)) + .put(LifecycleSettings.LIFECYCLE_NAME_SETTING.getKey(), originalPolicy) + ); + createIndexWithSettings( + client(), + managedIndex2, + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 10)) + .put(LifecycleSettings.LIFECYCLE_NAME_SETTING.getKey(), originalPolicy) + ); + createIndexWithSettings( + client(), + unmanagedIndex, + Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 10)) + ); + createIndexWithSettings( + client(), + managedByOtherPolicyIndex, + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 10)) + .put(LifecycleSettings.LIFECYCLE_NAME_SETTING.getKey(), otherPolicy) + ); Request deleteRequest = new Request("DELETE", "_ilm/policy/" + originalPolicy); ResponseException ex = expectThrows(ResponseException.class, () -> client().performRequest(deleteRequest)); - assertThat(ex.getMessage(), + assertThat( + ex.getMessage(), Matchers.allOf( containsString("Cannot delete policy [" + originalPolicy + "]. It is in use by one or more indices: ["), containsString(managedIndex1), containsString(managedIndex2), not(containsString(unmanagedIndex)), - not(containsString(managedByOtherPolicyIndex)))); + not(containsString(managedByOtherPolicyIndex)) + ) + ); } public void testRemoveAndReaddPolicy() throws Exception { @@ -555,10 +654,12 @@ public void testRemoveAndReaddPolicy() throws Exception { client(), originalIndex, alias, - Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) .put(LifecycleSettings.LIFECYCLE_NAME, policy) - .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias)); + .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias) + ); // Index a document index(client(), originalIndex, "_id", "foo", "bar"); @@ -573,12 +674,18 @@ public void testRemoveAndReaddPolicy() throws Exception { // Add the policy again Request addPolicyRequest = new Request("PUT", "/" + originalIndex + "/_settings"); - addPolicyRequest.setJsonEntity("{\n" + - " \"settings\": {\n" + - " \"index.lifecycle.name\": \"" + policy + "\",\n" + - " \"index.lifecycle.rollover_alias\": \"" + alias + "\"\n" + - " }\n" + - "}"); + addPolicyRequest.setJsonEntity( + "{\n" + + " \"settings\": {\n" + + " \"index.lifecycle.name\": \"" + + policy + + "\",\n" + + " \"index.lifecycle.rollover_alias\": \"" + + alias + + "\"\n" + + " }\n" + + "}" + ); client().performRequest(addPolicyRequest); assertBusy(() -> assertTrue((boolean) explainIndex(client(), originalIndex).getOrDefault("managed", false))); @@ -587,9 +694,15 @@ public void testRemoveAndReaddPolicy() throws Exception { } public void testCanStopILMWithPolicyUsingNonexistentPolicy() throws Exception { - createIndexWithSettings(client(), index, alias, Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put(LifecycleSettings.LIFECYCLE_NAME_SETTING.getKey(), randomAlphaOfLengthBetween(5, 15))); + createIndexWithSettings( + client(), + index, + alias, + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put(LifecycleSettings.LIFECYCLE_NAME_SETTING.getKey(), randomAlphaOfLengthBetween(5, 15)) + ); Request stopILMRequest = new Request("POST", "_ilm/stop"); assertOK(client().performRequest(stopILMRequest)); @@ -611,24 +724,34 @@ public void testCanStopILMWithPolicyUsingNonexistentPolicy() throws Exception { public void testWaitForActiveShardsStep() throws Exception { String originalIndex = index + "-000001"; String secondIndex = index + "-000002"; - createIndexWithSettings(client(), originalIndex, alias, Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + createIndexWithSettings( + client(), + originalIndex, + alias, + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias), - true); + true + ); // create policy createNewSingletonPolicy(client(), policy, "hot", new RolloverAction(null, null, null, 1L)); // update policy on index updatePolicy(client(), originalIndex, policy); Request createIndexTemplate = new Request("PUT", "_template/rolling_indexes"); - createIndexTemplate.setJsonEntity("{" + - "\"index_patterns\": [\"" + index + "-*\"], \n" + - " \"settings\": {\n" + - " \"number_of_shards\": 1,\n" + - " \"number_of_replicas\": 142,\n" + - " \"index.write.wait_for_active_shards\": \"all\"\n" + - " }\n" + - "}"); + createIndexTemplate.setJsonEntity( + "{" + + "\"index_patterns\": [\"" + + index + + "-*\"], \n" + + " \"settings\": {\n" + + " \"number_of_shards\": 1,\n" + + " \"number_of_replicas\": 142,\n" + + " \"index.write.wait_for_active_shards\": \"all\"\n" + + " }\n" + + "}" + ); createIndexTemplate.setOptions(expectWarnings(RestPutIndexTemplateAction.DEPRECATION_WARNING)); client().performRequest(createIndexTemplate); @@ -647,15 +770,23 @@ public void testWaitForActiveShardsStep() throws Exception { public void testHistoryIsWrittenWithSuccess() throws Exception { createNewSingletonPolicy(client(), policy, "hot", new RolloverAction(null, null, null, 1L)); Request createIndexTemplate = new Request("PUT", "_template/rolling_indexes"); - createIndexTemplate.setJsonEntity("{" + - "\"index_patterns\": [\"" + index + "-*\"], \n" + - " \"settings\": {\n" + - " \"number_of_shards\": 1,\n" + - " \"number_of_replicas\": 0,\n" + - " \"index.lifecycle.name\": \"" + policy + "\",\n" + - " \"index.lifecycle.rollover_alias\": \"" + alias + "\"\n" + - " }\n" + - "}"); + createIndexTemplate.setJsonEntity( + "{" + + "\"index_patterns\": [\"" + + index + + "-*\"], \n" + + " \"settings\": {\n" + + " \"number_of_shards\": 1,\n" + + " \"number_of_replicas\": 0,\n" + + " \"index.lifecycle.name\": \"" + + policy + + "\",\n" + + " \"index.lifecycle.rollover_alias\": \"" + + alias + + "\"\n" + + " }\n" + + "}" + ); createIndexTemplate.setOptions(expectWarnings(RestPutIndexTemplateAction.DEPRECATION_WARNING)); client().performRequest(createIndexTemplate); @@ -687,8 +818,9 @@ public void testHistoryIsWrittenWithFailure() throws Exception { client().performRequest(refreshIndex); // Check that we've had error and auto retried - assertBusy(() -> assertThat((Integer) explainIndex(client(), index + "-1").get("failed_step_retry_count"), - greaterThanOrEqualTo(1))); + assertBusy( + () -> assertThat((Integer) explainIndex(client(), index + "-1").get("failed_step_retry_count"), greaterThanOrEqualTo(1)) + ); assertBusy(() -> assertHistoryIsPresent(policy, index + "-1", false, "ERROR"), 30, TimeUnit.SECONDS); } @@ -722,22 +854,25 @@ public void testRetryableInitializationStep() throws Exception { client(), index, alias, - Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) .put(LifecycleSettings.LIFECYCLE_NAME, policy) - .put(LifecycleSettings.LIFECYCLE_PARSE_ORIGINATION_DATE, false)); + .put(LifecycleSettings.LIFECYCLE_PARSE_ORIGINATION_DATE, false) + ); - updateIndexSettings(index, Settings.builder() - .put(LifecycleSettings.LIFECYCLE_PARSE_ORIGINATION_DATE, true)); + updateIndexSettings(index, Settings.builder().put(LifecycleSettings.LIFECYCLE_PARSE_ORIGINATION_DATE, true)); assertOK(client().performRequest(startReq)); - assertBusy(() -> assertThat((Integer) explainIndex(client(), index).get(FAILED_STEP_RETRY_COUNT_FIELD), greaterThanOrEqualTo(1)), - 30, TimeUnit.SECONDS); + assertBusy( + () -> assertThat((Integer) explainIndex(client(), index).get(FAILED_STEP_RETRY_COUNT_FIELD), greaterThanOrEqualTo(1)), + 30, + TimeUnit.SECONDS + ); // Turn origination date parsing back off - updateIndexSettings(index, Settings.builder() - .put(LifecycleSettings.LIFECYCLE_PARSE_ORIGINATION_DATE, false)); + updateIndexSettings(index, Settings.builder().put(LifecycleSettings.LIFECYCLE_PARSE_ORIGINATION_DATE, false)); assertBusy(() -> { Map explainResp = explainIndex(client(), index); @@ -751,22 +886,33 @@ public void testRefreshablePhaseJson() throws Exception { createNewSingletonPolicy(client(), policy, "hot", new RolloverAction(null, null, null, 100L)); Request createIndexTemplate = new Request("PUT", "_template/rolling_indexes"); - createIndexTemplate.setJsonEntity("{" + - "\"index_patterns\": [\"" + index + "-*\"], \n" + - " \"settings\": {\n" + - " \"number_of_shards\": 1,\n" + - " \"number_of_replicas\": 0,\n" + - " \"index.lifecycle.name\": \"" + policy + "\",\n" + - " \"index.lifecycle.rollover_alias\": \"" + alias + "\"\n" + - " }\n" + - "}"); + createIndexTemplate.setJsonEntity( + "{" + + "\"index_patterns\": [\"" + + index + + "-*\"], \n" + + " \"settings\": {\n" + + " \"number_of_shards\": 1,\n" + + " \"number_of_replicas\": 0,\n" + + " \"index.lifecycle.name\": \"" + + policy + + "\",\n" + + " \"index.lifecycle.rollover_alias\": \"" + + alias + + "\"\n" + + " }\n" + + "}" + ); createIndexTemplate.setOptions(expectWarnings(RestPutIndexTemplateAction.DEPRECATION_WARNING)); client().performRequest(createIndexTemplate); - createIndexWithSettings(client(), index + "-1", alias, - Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0), - true); + createIndexWithSettings( + client(), + index + "-1", + alias, + Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0), + true + ); // Index a document index(client(), index + "-1", "1", "foo", "bar"); @@ -787,12 +933,16 @@ public void testHaltAtEndOfPhase() throws Exception { createNewSingletonPolicy(client(), policy, "hot", new SetPriorityAction(100)); - createIndexWithSettings(client(), index, alias, + createIndexWithSettings( + client(), + index, + alias, Settings.builder() .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) .put(LifecycleSettings.LIFECYCLE_NAME, policy), - randomBoolean()); + randomBoolean() + ); // Wait for the index to finish the "hot" phase assertBusy(() -> assertThat(getStepKeyForIndex(client(), index), equalTo(PhaseCompleteStep.finalStep("hot").getKey()))); @@ -808,8 +958,7 @@ public void testHaltAtEndOfPhase() throws Exception { // PUT policy XContentBuilder builder = jsonBuilder(); lifecyclePolicy.toXContent(builder, null); - final StringEntity entity = new StringEntity( - "{ \"policy\":" + Strings.toString(builder) + "}", ContentType.APPLICATION_JSON); + final StringEntity entity = new StringEntity("{ \"policy\":" + Strings.toString(builder) + "}", ContentType.APPLICATION_JSON); Request request = new Request("PUT", "_ilm/policy/" + policy); request.setEntity(entity); assertOK(client().performRequest(request)); @@ -825,28 +974,32 @@ public void testDeleteActionDoesntDeleteSearchableSnapshot() throws Exception { createSnapshotRepo(client(), snapshotRepo, randomBoolean()); // create policy with cold and delete phases - Map coldActions = - Map.of(SearchableSnapshotAction.NAME, new SearchableSnapshotAction(snapshotRepo)); + Map coldActions = Map.of(SearchableSnapshotAction.NAME, new SearchableSnapshotAction(snapshotRepo)); Map phases = new HashMap<>(); phases.put("cold", new Phase("cold", TimeValue.ZERO, coldActions)); - phases.put("delete", new Phase("delete", TimeValue.timeValueMillis(10000), singletonMap(DeleteAction.NAME, - new DeleteAction(false)))); + phases.put( + "delete", + new Phase("delete", TimeValue.timeValueMillis(10000), singletonMap(DeleteAction.NAME, new DeleteAction(false))) + ); LifecyclePolicy lifecyclePolicy = new LifecyclePolicy(policy, phases); // PUT policy XContentBuilder builder = jsonBuilder(); lifecyclePolicy.toXContent(builder, null); - final StringEntity entity = new StringEntity( - "{ \"policy\":" + Strings.toString(builder) + "}", ContentType.APPLICATION_JSON); + final StringEntity entity = new StringEntity("{ \"policy\":" + Strings.toString(builder) + "}", ContentType.APPLICATION_JSON); Request createPolicyRequest = new Request("PUT", "_ilm/policy/" + policy); createPolicyRequest.setEntity(entity); assertOK(client().performRequest(createPolicyRequest)); - createIndexWithSettings(client(), index, alias, + createIndexWithSettings( + client(), + index, + alias, Settings.builder() .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) .put(LifecycleSettings.LIFECYCLE_NAME, policy), - randomBoolean()); + randomBoolean() + ); String[] snapshotName = new String[1]; String restoredIndexName = SearchableSnapshotAction.FULL_RESTORED_INDEX_PREFIX + this.index; @@ -887,8 +1040,8 @@ public void testDeleteActionDoesntDeleteSearchableSnapshot() throws Exception { for (Object snapshot : snapshots) { Map snapshotInfoMap = (Map) snapshot; if (snapshotInfoMap.get("snapshot").equals(snapshotName[0]) && - // wait for the snapshot to be completed (successfully or not) otherwise the teardown might fail - SnapshotState.valueOf((String) snapshotInfoMap.get("state")).completed()) { + // wait for the snapshot to be completed (successfully or not) otherwise the teardown might fail + SnapshotState.valueOf((String) snapshotInfoMap.get("state")).completed()) { return true; } } @@ -903,21 +1056,35 @@ public void testDeleteActionDoesntDeleteSearchableSnapshot() throws Exception { public void testSearchableSnapshotRequiresSnapshotRepoToExist() throws IOException { String repo = randomAlphaOfLengthBetween(4, 10); final String phaseName = "cold"; - ResponseException ex = expectThrows(ResponseException.class, () -> - createNewSingletonPolicy(client(), policy, phaseName, new SearchableSnapshotAction(repo))); + ResponseException ex = expectThrows( + ResponseException.class, + () -> createNewSingletonPolicy(client(), policy, phaseName, new SearchableSnapshotAction(repo)) + ); assertThat(ex.getMessage(), containsString("no such repository")); - assertThat(ex.getMessage(), containsString("the snapshot repository referenced by the [searchable_snapshot] action " + - "in the [cold] phase must exist before it can be referenced by an ILM policy")); + assertThat( + ex.getMessage(), + containsString( + "the snapshot repository referenced by the [searchable_snapshot] action " + + "in the [cold] phase must exist before it can be referenced by an ILM policy" + ) + ); } public void testWaitForSnapshotRequiresSLMPolicyToExist() throws IOException { String slmPolicy = randomAlphaOfLengthBetween(4, 10); final String phaseName = "delete"; - ResponseException ex = expectThrows(ResponseException.class, () -> - createNewSingletonPolicy(client(), policy, phaseName, new WaitForSnapshotAction(slmPolicy))); + ResponseException ex = expectThrows( + ResponseException.class, + () -> createNewSingletonPolicy(client(), policy, phaseName, new WaitForSnapshotAction(slmPolicy)) + ); assertThat(ex.getMessage(), containsString("no such snapshot lifecycle policy")); - assertThat(ex.getMessage(), containsString("the snapshot lifecycle policy referenced by the [wait_for_snapshot] action " + - "in the [delete] phase must exist before it can be referenced by an ILM policy")); + assertThat( + ex.getMessage(), + containsString( + "the snapshot lifecycle policy referenced by the [wait_for_snapshot] action " + + "in the [delete] phase must exist before it can be referenced by an ILM policy" + ) + ); } // This method should be called inside an assertBusy, it has no retry logic of its own @@ -927,41 +1094,64 @@ private void assertHistoryIsPresent(String policyName, String indexName, boolean // This method should be called inside an assertBusy, it has no retry logic of its own @SuppressWarnings("unchecked") - private void assertHistoryIsPresent(String policyName, String indexName, boolean success, - @Nullable String phase, @Nullable String action, String stepName) throws IOException { - logger.info("--> checking for history item [{}], [{}], success: [{}], phase: [{}], action: [{}], step: [{}]", - policyName, indexName, success, phase, action, stepName); + private void assertHistoryIsPresent( + String policyName, + String indexName, + boolean success, + @Nullable String phase, + @Nullable String action, + String stepName + ) throws IOException { + logger.info( + "--> checking for history item [{}], [{}], success: [{}], phase: [{}], action: [{}], step: [{}]", + policyName, + indexName, + success, + phase, + action, + stepName + ); final Request historySearchRequest = new Request("GET", "ilm-history*/_search?expand_wildcards=all"); - historySearchRequest.setJsonEntity("{\n" + - " \"query\": {\n" + - " \"bool\": {\n" + - " \"must\": [\n" + - " {\n" + - " \"term\": {\n" + - " \"policy\": \"" + policyName + "\"\n" + - " }\n" + - " },\n" + - " {\n" + - " \"term\": {\n" + - " \"success\": " + success + "\n" + - " }\n" + - " },\n" + - " {\n" + - " \"term\": {\n" + - " \"index\": \"" + indexName + "\"\n" + - " }\n" + - " },\n" + - " {\n" + - " \"term\": {\n" + - " \"state.step\": \"" + stepName + "\"\n" + - " }\n" + - " }\n" + - (phase == null ? "" : ",{\"term\": {\"state.phase\": \"" + phase + "\"}}") + - (action == null ? "" : ",{\"term\": {\"state.action\": \"" + action + "\"}}") + - " ]\n" + - " }\n" + - " }\n" + - "}"); + historySearchRequest.setJsonEntity( + "{\n" + + " \"query\": {\n" + + " \"bool\": {\n" + + " \"must\": [\n" + + " {\n" + + " \"term\": {\n" + + " \"policy\": \"" + + policyName + + "\"\n" + + " }\n" + + " },\n" + + " {\n" + + " \"term\": {\n" + + " \"success\": " + + success + + "\n" + + " }\n" + + " },\n" + + " {\n" + + " \"term\": {\n" + + " \"index\": \"" + + indexName + + "\"\n" + + " }\n" + + " },\n" + + " {\n" + + " \"term\": {\n" + + " \"state.step\": \"" + + stepName + + "\"\n" + + " }\n" + + " }\n" + + (phase == null ? "" : ",{\"term\": {\"state.phase\": \"" + phase + "\"}}") + + (action == null ? "" : ",{\"term\": {\"state.action\": \"" + action + "\"}}") + + " ]\n" + + " }\n" + + " }\n" + + "}" + ); Response historyResponse; try { historyResponse = client().performRequest(historySearchRequest); @@ -975,24 +1165,30 @@ private void assertHistoryIsPresent(String policyName, String indexName, boolean // For a failure, print out whatever history we *do* have for the index if (hits == 0) { final Request allResults = new Request("GET", "ilm-history*/_search"); - allResults.setJsonEntity("{\n" + - " \"query\": {\n" + - " \"bool\": {\n" + - " \"must\": [\n" + - " {\n" + - " \"term\": {\n" + - " \"policy\": \"" + policyName + "\"\n" + - " }\n" + - " },\n" + - " {\n" + - " \"term\": {\n" + - " \"index\": \"" + indexName + "\"\n" + - " }\n" + - " }\n" + - " ]\n" + - " }\n" + - " }\n" + - "}"); + allResults.setJsonEntity( + "{\n" + + " \"query\": {\n" + + " \"bool\": {\n" + + " \"must\": [\n" + + " {\n" + + " \"term\": {\n" + + " \"policy\": \"" + + policyName + + "\"\n" + + " }\n" + + " },\n" + + " {\n" + + " \"term\": {\n" + + " \"index\": \"" + + indexName + + "\"\n" + + " }\n" + + " }\n" + + " ]\n" + + " }\n" + + " }\n" + + "}" + ); final Response allResultsResp = client().performRequest(historySearchRequest); Map allResultsMap; try (InputStream is = allResultsResp.getEntity().getContent()) { @@ -1017,16 +1213,19 @@ private void assertHistoryIsPresent(String policyName, String indexName, boolean private void createSlmPolicy(String smlPolicy, String repo) throws IOException { Request request; request = new Request("PUT", "/_slm/policy/" + smlPolicy); - request.setJsonEntity(Strings - .toString(JsonXContent.contentBuilder() - .startObject() - .field("schedule", "59 59 23 31 12 ? 2099") - .field("repository", repo) - .field("name", "snap" + randomAlphaOfLengthBetween(5, 10).toLowerCase(Locale.ROOT)) - .startObject("config") - .field("include_global_state", false) - .endObject() - .endObject())); + request.setJsonEntity( + Strings.toString( + JsonXContent.contentBuilder() + .startObject() + .field("schedule", "59 59 23 31 12 ? 2099") + .field("repository", repo) + .field("name", "snap" + randomAlphaOfLengthBetween(5, 10).toLowerCase(Locale.ROOT)) + .startObject("config") + .field("include_global_state", false) + .endObject() + .endObject() + ) + ); assertOK(client().performRequest(request)); } @@ -1035,7 +1234,7 @@ private void deleteSlmPolicy(String smlPolicy) throws IOException { assertOK(client().performRequest(new Request("DELETE", "/_slm/policy/" + smlPolicy))); } - //adds debug information for waitForSnapshot tests + // adds debug information for waitForSnapshot tests private void assertBusy(CheckedRunnable runnable, String slmPolicy) throws Exception { assertBusy(() -> { try { diff --git a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/TimeseriesMoveToStepIT.java b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/TimeseriesMoveToStepIT.java index 0ad36fbd65f9e..8e1fca3644ea2 100644 --- a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/TimeseriesMoveToStepIT.java +++ b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/TimeseriesMoveToStepIT.java @@ -57,27 +57,35 @@ public void testMoveToAllocateStep() throws Exception { String originalIndex = index + "-000001"; // create policy createFullPolicy(client(), policy, TimeValue.timeValueHours(10)); - createIndexWithSettings(client(), originalIndex, alias, Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 4) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put("index.routing.allocation.include._name", "javaRestTest-0") - .put(LifecycleSettings.LIFECYCLE_NAME, policy) - .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, "alias")); + createIndexWithSettings( + client(), + originalIndex, + alias, + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 4) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put("index.routing.allocation.include._name", "javaRestTest-0") + .put(LifecycleSettings.LIFECYCLE_NAME, policy) + .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, "alias") + ); // move to a step Request moveToStepRequest = new Request("POST", "_ilm/move/" + originalIndex); assertBusy(() -> assertTrue(getStepKeyForIndex(client(), originalIndex).equals(new StepKey("new", "complete", "complete")))); - moveToStepRequest.setJsonEntity("{\n" + - " \"current_step\": {\n" + - " \"phase\": \"new\",\n" + - " \"action\": \"complete\",\n" + - " \"name\": \"complete\"\n" + - " },\n" + - " \"next_step\": {\n" + - " \"phase\": \"cold\",\n" + - " \"action\": \"allocate\",\n" + - " \"name\": \"allocate\"\n" + - " }\n" + - "}"); + moveToStepRequest.setJsonEntity( + "{\n" + + " \"current_step\": {\n" + + " \"phase\": \"new\",\n" + + " \"action\": \"complete\",\n" + + " \"name\": \"complete\"\n" + + " },\n" + + " \"next_step\": {\n" + + " \"phase\": \"cold\",\n" + + " \"action\": \"allocate\",\n" + + " \"name\": \"allocate\"\n" + + " }\n" + + "}" + ); client().performRequest(moveToStepRequest); assertBusy(() -> assertFalse(indexExists(originalIndex))); } @@ -88,29 +96,37 @@ public void testMoveToRolloverStep() throws Exception { String secondIndex = index + "-000002"; createFullPolicy(client(), policy, TimeValue.timeValueHours(10)); - createIndexWithSettings(client(), originalIndex, alias, Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 4) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put("index.routing.allocation.include._name", "javaRestTest-0") - .put(LifecycleSettings.LIFECYCLE_NAME, policy) - .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias)); + createIndexWithSettings( + client(), + originalIndex, + alias, + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 4) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put("index.routing.allocation.include._name", "javaRestTest-0") + .put(LifecycleSettings.LIFECYCLE_NAME, policy) + .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias) + ); // move to a step Request moveToStepRequest = new Request("POST", "_ilm/move/" + originalIndex); // index document to trigger rollover index(client(), originalIndex, "_id", "foo", "bar"); logger.info(getStepKeyForIndex(client(), originalIndex)); - moveToStepRequest.setJsonEntity("{\n" + - " \"current_step\": {\n" + - " \"phase\": \"new\",\n" + - " \"action\": \"complete\",\n" + - " \"name\": \"complete\"\n" + - " },\n" + - " \"next_step\": {\n" + - " \"phase\": \"hot\",\n" + - " \"action\": \"rollover\",\n" + - " \"name\": \"attempt-rollover\"\n" + - " }\n" + - "}"); + moveToStepRequest.setJsonEntity( + "{\n" + + " \"current_step\": {\n" + + " \"phase\": \"new\",\n" + + " \"action\": \"complete\",\n" + + " \"name\": \"complete\"\n" + + " },\n" + + " \"next_step\": {\n" + + " \"phase\": \"hot\",\n" + + " \"action\": \"rollover\",\n" + + " \"name\": \"attempt-rollover\"\n" + + " }\n" + + "}" + ); client().performRequest(moveToStepRequest); /* @@ -130,27 +146,35 @@ public void testMoveToRolloverStep() throws Exception { public void testMoveToInjectedStep() throws Exception { createNewSingletonPolicy(client(), policy, "warm", new ShrinkAction(1, null), TimeValue.timeValueHours(12)); - createIndexWithSettings(client(), index, alias, Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 3) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put(LifecycleSettings.LIFECYCLE_NAME, policy) - .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias)); + createIndexWithSettings( + client(), + index, + alias, + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 3) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put(LifecycleSettings.LIFECYCLE_NAME, policy) + .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias) + ); assertBusy(() -> assertThat(getStepKeyForIndex(client(), index), equalTo(new StepKey("new", "complete", "complete")))); // Move to a step from the injected unfollow action Request moveToStepRequest = new Request("POST", "_ilm/move/" + index); - moveToStepRequest.setJsonEntity("{\n" + - " \"current_step\": { \n" + - " \"phase\": \"new\",\n" + - " \"action\": \"complete\",\n" + - " \"name\": \"complete\"\n" + - " },\n" + - " \"next_step\": { \n" + - " \"phase\": \"warm\",\n" + - " \"action\": \"unfollow\",\n" + - " \"name\": \"wait-for-indexing-complete\"\n" + - " }\n" + - "}"); + moveToStepRequest.setJsonEntity( + "{\n" + + " \"current_step\": { \n" + + " \"phase\": \"new\",\n" + + " \"action\": \"complete\",\n" + + " \"name\": \"complete\"\n" + + " },\n" + + " \"next_step\": { \n" + + " \"phase\": \"warm\",\n" + + " \"action\": \"unfollow\",\n" + + " \"name\": \"wait-for-indexing-complete\"\n" + + " }\n" + + "}" + ); // If we get an OK on this request we have successfully moved to the injected step assertOK(client().performRequest(moveToStepRequest)); @@ -164,75 +188,99 @@ public void testMoveToInjectedStep() throws Exception { } public void testMoveToStepRereadsPolicy() throws Exception { - createNewSingletonPolicy(client(), policy, "hot", new RolloverAction(null, null, TimeValue.timeValueHours(1), null), TimeValue.ZERO); - - createIndexWithSettings(client(), "test-1", alias, Settings.builder() + createNewSingletonPolicy( + client(), + policy, + "hot", + new RolloverAction(null, null, TimeValue.timeValueHours(1), null), + TimeValue.ZERO + ); + + createIndexWithSettings( + client(), + "test-1", + alias, + Settings.builder() .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) .put(LifecycleSettings.LIFECYCLE_NAME, policy) .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias), - true); + true + ); - assertBusy(() -> assertThat(getStepKeyForIndex(client(), "test-1"), - equalTo(new StepKey("hot", "rollover", "check-rollover-ready"))), 30, TimeUnit.SECONDS); + assertBusy( + () -> assertThat(getStepKeyForIndex(client(), "test-1"), equalTo(new StepKey("hot", "rollover", "check-rollover-ready"))), + 30, + TimeUnit.SECONDS + ); createNewSingletonPolicy(client(), policy, "hot", new RolloverAction(null, null, null, 1L), TimeValue.ZERO); // Move to the same step, which should re-read the policy Request moveToStepRequest = new Request("POST", "_ilm/move/test-1"); - moveToStepRequest.setJsonEntity("{\n" + - " \"current_step\": { \n" + - " \"phase\": \"hot\",\n" + - " \"action\": \"rollover\",\n" + - " \"name\": \"check-rollover-ready\"\n" + - " },\n" + - " \"next_step\": { \n" + - " \"phase\": \"hot\",\n" + - " \"action\": \"rollover\",\n" + - " \"name\": \"check-rollover-ready\"\n" + - " }\n" + - "}"); + moveToStepRequest.setJsonEntity( + "{\n" + + " \"current_step\": { \n" + + " \"phase\": \"hot\",\n" + + " \"action\": \"rollover\",\n" + + " \"name\": \"check-rollover-ready\"\n" + + " },\n" + + " \"next_step\": { \n" + + " \"phase\": \"hot\",\n" + + " \"action\": \"rollover\",\n" + + " \"name\": \"check-rollover-ready\"\n" + + " }\n" + + "}" + ); // busy asserting here as ILM moves the index from the `check-rollover-ready` step into the `error` step and back into the // `check-rollover-ready` when retrying. the `_ilm/move` api might fail when the as the `current_step` of the index might be - // the `error` step at execution time. + // the `error` step at execution time. assertBusy(() -> client().performRequest(moveToStepRequest), 30, TimeUnit.SECONDS); indexDocument(client(), "test-1", true); // Make sure we actually rolled over - assertBusy(() -> { - indexExists("test-000002"); - }); + assertBusy(() -> { indexExists("test-000002"); }); } public void testMoveToStepWithInvalidNextStep() throws Exception { createNewSingletonPolicy(client(), policy, "delete", new DeleteAction(), TimeValue.timeValueDays(100)); - createIndexWithSettings(client(), index, alias, Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put(LifecycleSettings.LIFECYCLE_NAME, policy)); + createIndexWithSettings( + client(), + index, + alias, + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put(LifecycleSettings.LIFECYCLE_NAME, policy) + ); // move to a step Request moveToStepRequest = new Request("POST", "_ilm/move/" + index); - moveToStepRequest.setJsonEntity("{\n" + - " \"current_step\": {\n" + - " \"phase\": \"new\",\n" + - " \"action\": \"complete\",\n" + - " \"name\": \"complete\"\n" + - " },\n" + - " \"next_step\": {\n" + - " \"phase\": \"hot\",\n" + - " \"action\": \"rollover\",\n" + - " \"name\": \"attempt-rollover\"\n" + - " }\n" + - "}"); + moveToStepRequest.setJsonEntity( + "{\n" + + " \"current_step\": {\n" + + " \"phase\": \"new\",\n" + + " \"action\": \"complete\",\n" + + " \"name\": \"complete\"\n" + + " },\n" + + " \"next_step\": {\n" + + " \"phase\": \"hot\",\n" + + " \"action\": \"rollover\",\n" + + " \"name\": \"attempt-rollover\"\n" + + " }\n" + + "}" + ); assertBusy(() -> { - ResponseException exception = - expectThrows(ResponseException.class, () -> client().performRequest(moveToStepRequest)); + ResponseException exception = expectThrows(ResponseException.class, () -> client().performRequest(moveToStepRequest)); String responseEntityAsString = EntityUtils.toString(exception.getResponse().getEntity()); - String expectedErrorMessage = "step [{\\\"phase\\\":\\\"hot\\\",\\\"action\\\":\\\"rollover\\\",\\\"name\\\":" + - "\\\"attempt-rollover\\\"}] for index [" + index + "] with policy [" + policy + "] does not exist"; + String expectedErrorMessage = "step [{\\\"phase\\\":\\\"hot\\\",\\\"action\\\":\\\"rollover\\\",\\\"name\\\":" + + "\\\"attempt-rollover\\\"}] for index [" + + index + + "] with policy [" + + policy + + "] does not exist"; assertThat(responseEntityAsString, containsStringIgnoringCase(expectedErrorMessage)); }); @@ -240,119 +288,149 @@ public void testMoveToStepWithInvalidNextStep() throws Exception { public void testMoveToStepWithoutStepName() throws Exception { createNewSingletonPolicy(client(), policy, "warm", new ForceMergeAction(1, null), TimeValue.timeValueHours(1)); - createIndexWithSettings(client(), index, alias, Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put(LifecycleSettings.LIFECYCLE_NAME, policy)); + createIndexWithSettings( + client(), + index, + alias, + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put(LifecycleSettings.LIFECYCLE_NAME, policy) + ); // move to a step Request moveToStepRequest = new Request("POST", "_ilm/move/" + index); - moveToStepRequest.setJsonEntity("{\n" + - " \"current_step\": {\n" + - " \"phase\": \"new\",\n" + - " \"action\": \"complete\",\n" + - " \"name\": \"complete\"\n" + - " },\n" + - " \"next_step\": {\n" + - " \"phase\": \"warm\",\n" + - " \"action\": \"forcemerge\"\n" + - " }\n" + - "}"); + moveToStepRequest.setJsonEntity( + "{\n" + + " \"current_step\": {\n" + + " \"phase\": \"new\",\n" + + " \"action\": \"complete\",\n" + + " \"name\": \"complete\"\n" + + " },\n" + + " \"next_step\": {\n" + + " \"phase\": \"warm\",\n" + + " \"action\": \"forcemerge\"\n" + + " }\n" + + "}" + ); assertOK(client().performRequest(moveToStepRequest)); // Make sure we actually move on to and execute the forcemerge action - assertBusy(() -> { - assertThat(getStepKeyForIndex(client(), index), equalTo(PhaseCompleteStep.finalStep("warm").getKey())); - }, 30, TimeUnit.SECONDS); + assertBusy( + () -> { assertThat(getStepKeyForIndex(client(), index), equalTo(PhaseCompleteStep.finalStep("warm").getKey())); }, + 30, + TimeUnit.SECONDS + ); } public void testMoveToStepWithoutAction() throws Exception { createNewSingletonPolicy(client(), policy, "warm", new ForceMergeAction(1, null), TimeValue.timeValueHours(1)); - createIndexWithSettings(client(), index, alias, Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put(LifecycleSettings.LIFECYCLE_NAME, policy)); + createIndexWithSettings( + client(), + index, + alias, + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put(LifecycleSettings.LIFECYCLE_NAME, policy) + ); // move to a step Request moveToStepRequest = new Request("POST", "_ilm/move/" + index); - moveToStepRequest.setJsonEntity("{\n" + - " \"current_step\": {\n" + - " \"phase\": \"new\",\n" + - " \"action\": \"complete\",\n" + - " \"name\": \"complete\"\n" + - " },\n" + - " \"next_step\": {\n" + - " \"phase\": \"warm\"\n" + - " }\n" + - "}"); + moveToStepRequest.setJsonEntity( + "{\n" + + " \"current_step\": {\n" + + " \"phase\": \"new\",\n" + + " \"action\": \"complete\",\n" + + " \"name\": \"complete\"\n" + + " },\n" + + " \"next_step\": {\n" + + " \"phase\": \"warm\"\n" + + " }\n" + + "}" + ); assertOK(client().performRequest(moveToStepRequest)); // Make sure we actually move on to and execute the forcemerge action - assertBusy(() -> { - assertThat(getStepKeyForIndex(client(), index), equalTo(PhaseCompleteStep.finalStep("warm").getKey())); - }, 30, TimeUnit.SECONDS); + assertBusy( + () -> { assertThat(getStepKeyForIndex(client(), index), equalTo(PhaseCompleteStep.finalStep("warm").getKey())); }, + 30, + TimeUnit.SECONDS + ); } public void testInvalidToMoveToStepWithoutActionButWithName() throws Exception { createNewSingletonPolicy(client(), policy, "warm", new ForceMergeAction(1, null), TimeValue.timeValueHours(1)); - createIndexWithSettings(client(), index, alias, Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put(LifecycleSettings.LIFECYCLE_NAME, policy)); + createIndexWithSettings( + client(), + index, + alias, + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put(LifecycleSettings.LIFECYCLE_NAME, policy) + ); // move to a step with an invalid request Request moveToStepRequest = new Request("POST", "_ilm/move/" + index); - moveToStepRequest.setJsonEntity("{\n" + - " \"current_step\": {\n" + - " \"phase\": \"new\",\n" + - " \"action\": \"complete\",\n" + - " \"name\": \"complete\"\n" + - " },\n" + - " \"next_step\": {\n" + - " \"phase\": \"warm\",\n" + - " \"name\": \"forcemerge\"\n" + - " }\n" + - "}"); + moveToStepRequest.setJsonEntity( + "{\n" + + " \"current_step\": {\n" + + " \"phase\": \"new\",\n" + + " \"action\": \"complete\",\n" + + " \"name\": \"complete\"\n" + + " },\n" + + " \"next_step\": {\n" + + " \"phase\": \"warm\",\n" + + " \"name\": \"forcemerge\"\n" + + " }\n" + + "}" + ); assertBusy(() -> { - ResponseException exception = - expectThrows(ResponseException.class, () -> client().performRequest(moveToStepRequest)); + ResponseException exception = expectThrows(ResponseException.class, () -> client().performRequest(moveToStepRequest)); String responseEntityAsString = EntityUtils.toString(exception.getResponse().getEntity()); - String expectedErrorMessage = "phase; phase and action; or phase, action, and step must be provided, " + - "but a step name was specified without a corresponding action"; + String expectedErrorMessage = "phase; phase and action; or phase, action, and step must be provided, " + + "but a step name was specified without a corresponding action"; assertThat(responseEntityAsString, containsStringIgnoringCase(expectedErrorMessage)); }); } public void testResolveToNonexistentStep() throws Exception { createNewSingletonPolicy(client(), policy, "warm", new ForceMergeAction(1, null), TimeValue.timeValueHours(1)); - createIndexWithSettings(client(), index, alias, Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put(LifecycleSettings.LIFECYCLE_NAME, policy)); + createIndexWithSettings( + client(), + index, + alias, + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put(LifecycleSettings.LIFECYCLE_NAME, policy) + ); // move to a step with an invalid request Request moveToStepRequest = new Request("POST", "_ilm/move/" + index); - moveToStepRequest.setJsonEntity("{\n" + - " \"current_step\": {\n" + - " \"phase\": \"new\",\n" + - " \"action\": \"complete\",\n" + - " \"name\": \"complete\"\n" + - " },\n" + - " \"next_step\": {\n" + - " \"phase\": \"warm\",\n" + - " \"action\": \"shrink\"\n" + - " }\n" + - "}"); + moveToStepRequest.setJsonEntity( + "{\n" + + " \"current_step\": {\n" + + " \"phase\": \"new\",\n" + + " \"action\": \"complete\",\n" + + " \"name\": \"complete\"\n" + + " },\n" + + " \"next_step\": {\n" + + " \"phase\": \"warm\",\n" + + " \"action\": \"shrink\"\n" + + " }\n" + + "}" + ); assertBusy(() -> { - ResponseException exception = - expectThrows(ResponseException.class, () -> client().performRequest(moveToStepRequest)); + ResponseException exception = expectThrows(ResponseException.class, () -> client().performRequest(moveToStepRequest)); String responseEntityAsString = EntityUtils.toString(exception.getResponse().getEntity()); - String expectedErrorMessage = "unable to determine concrete step key from target next step key: " + - "{\\\"phase\\\":\\\"warm\\\",\\\"action\\\":\\\"shrink\\\"}"; + String expectedErrorMessage = "unable to determine concrete step key from target next step key: " + + "{\\\"phase\\\":\\\"warm\\\",\\\"action\\\":\\\"shrink\\\"}"; assertThat(responseEntityAsString, containsStringIgnoringCase(expectedErrorMessage)); }); } diff --git a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/actions/ReadonlyActionIT.java b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/actions/ReadonlyActionIT.java index 91a104bd8719f..180c40ae6d467 100644 --- a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/actions/ReadonlyActionIT.java +++ b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/actions/ReadonlyActionIT.java @@ -50,9 +50,12 @@ public void refreshAbstractions() { } public void testReadOnly() throws Exception { - createIndexWithSettings(client(), index, alias, Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)); + createIndexWithSettings( + client(), + index, + alias, + Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + ); String phaseName = randomFrom("warm", "cold"); createNewSingletonPolicy(client(), policy, phaseName, new ReadOnlyAction()); updatePolicy(client(), index, policy); @@ -69,21 +72,28 @@ public void testReadOnlyInTheHotPhase() throws Exception { // add a policy Map hotActions = Map.of( - RolloverAction.NAME, new RolloverAction(null, null, null, 1L), - ReadOnlyAction.NAME, new ReadOnlyAction()); - Map phases = Map.of( - "hot", new Phase("hot", TimeValue.ZERO, hotActions)); + RolloverAction.NAME, + new RolloverAction(null, null, null, 1L), + ReadOnlyAction.NAME, + new ReadOnlyAction() + ); + Map phases = Map.of("hot", new Phase("hot", TimeValue.ZERO, hotActions)); LifecyclePolicy lifecyclePolicy = new LifecyclePolicy(policy, phases); Request createPolicyRequest = new Request("PUT", "_ilm/policy/" + policy); createPolicyRequest.setJsonEntity("{ \"policy\":" + Strings.toString(lifecyclePolicy) + "}"); client().performRequest(createPolicyRequest); // then create the index and index a document to trigger rollover - createIndexWithSettings(client(), originalIndex, alias, Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias) - .put(LifecycleSettings.LIFECYCLE_NAME, policy)); + createIndexWithSettings( + client(), + originalIndex, + alias, + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias) + .put(LifecycleSettings.LIFECYCLE_NAME, policy) + ); index(client(), originalIndex, "_id", "foo", "bar"); assertBusy(() -> { diff --git a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/actions/RolloverActionIT.java b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/actions/RolloverActionIT.java index 73191b0091eec..a4f0a3d4aff39 100644 --- a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/actions/RolloverActionIT.java +++ b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/actions/RolloverActionIT.java @@ -56,9 +56,15 @@ public void refreshIndex() { public void testRolloverAction() throws Exception { String originalIndex = index + "-000001"; String secondIndex = index + "-000002"; - createIndexWithSettings(client(), originalIndex, alias, Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias)); + createIndexWithSettings( + client(), + originalIndex, + alias, + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias) + ); // create policy createNewSingletonPolicy(client(), policy, "hot", new RolloverAction(null, null, null, 1L)); @@ -78,29 +84,39 @@ public void testRolloverAction() throws Exception { public void testRolloverActionWithIndexingComplete() throws Exception { String originalIndex = index + "-000001"; String secondIndex = index + "-000002"; - createIndexWithSettings(client(), originalIndex, alias, Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias)); + createIndexWithSettings( + client(), + originalIndex, + alias, + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias) + ); Request updateSettingsRequest = new Request("PUT", "/" + originalIndex + "/_settings"); - updateSettingsRequest.setJsonEntity("{\n" + - " \"settings\": {\n" + - " \"" + LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE + "\": true\n" + - " }\n" + - "}"); + updateSettingsRequest.setJsonEntity( + "{\n" + " \"settings\": {\n" + " \"" + LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE + "\": true\n" + " }\n" + "}" + ); client().performRequest(updateSettingsRequest); Request updateAliasRequest = new Request("POST", "/_aliases"); - updateAliasRequest.setJsonEntity("{\n" + - " \"actions\": [\n" + - " {\n" + - " \"add\": {\n" + - " \"index\": \"" + originalIndex + "\",\n" + - " \"alias\": \"" + alias + "\",\n" + - " \"is_write_index\": false\n" + - " }\n" + - " }\n" + - " ]\n" + - "}"); + updateAliasRequest.setJsonEntity( + "{\n" + + " \"actions\": [\n" + + " {\n" + + " \"add\": {\n" + + " \"index\": \"" + + originalIndex + + "\",\n" + + " \"alias\": \"" + + alias + + "\",\n" + + " \"is_write_index\": false\n" + + " }\n" + + " }\n" + + " ]\n" + + "}" + ); client().performRequest(updateAliasRequest); // create policy @@ -121,10 +137,15 @@ public void testRolloverActionWithIndexingComplete() throws Exception { public void testRolloverActionWithMaxPrimaryShardSize() throws Exception { String originalIndex = index + "-000001"; String secondIndex = index + "-000002"; - createIndexWithSettings(client(), originalIndex, alias, Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 3) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias)); + createIndexWithSettings( + client(), + originalIndex, + alias, + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 3) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias) + ); index(client(), originalIndex, "_id", "foo", "bar"); @@ -151,7 +172,8 @@ public void testILMRolloverRetriesOnReadOnlyBlock() throws Exception { client(), firstIndex, alias, - Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) .put(LifecycleSettings.LIFECYCLE_NAME, policy) .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias) @@ -160,16 +182,15 @@ public void testILMRolloverRetriesOnReadOnlyBlock() throws Exception { ); // wait for ILM to start retrying the step - assertBusy(() -> assertThat((Integer) explainIndex(client(), firstIndex).get(FAILED_STEP_RETRY_COUNT_FIELD), - greaterThanOrEqualTo(1))); + assertBusy( + () -> assertThat((Integer) explainIndex(client(), firstIndex).get(FAILED_STEP_RETRY_COUNT_FIELD), greaterThanOrEqualTo(1)) + ); // remove the read only block Request allowWritesOnIndexSettingUpdate = new Request("PUT", firstIndex + "/_settings"); - allowWritesOnIndexSettingUpdate.setJsonEntity("{" + - " \"index\": {\n" + - " \"blocks.read_only\" : \"false\" \n" + - " }\n" + - "}"); + allowWritesOnIndexSettingUpdate.setJsonEntity( + "{" + " \"index\": {\n" + " \"blocks.read_only\" : \"false\" \n" + " }\n" + "}" + ); client().performRequest(allowWritesOnIndexSettingUpdate); // index is not readonly so the ILM should complete successfully @@ -184,15 +205,23 @@ public void testILMRolloverOnManuallyRolledIndex() throws Exception { // Set up a policy with rollover createNewSingletonPolicy(client(), policy, "hot", new RolloverAction(null, null, null, 2L)); Request createIndexTemplate = new Request("PUT", "_template/rolling_indexes"); - createIndexTemplate.setJsonEntity("{" + - "\"index_patterns\": [\"" + index + "-*\"], \n" + - " \"settings\": {\n" + - " \"number_of_shards\": 1,\n" + - " \"number_of_replicas\": 0,\n" + - " \"index.lifecycle.name\": \"" + policy + "\", \n" + - " \"index.lifecycle.rollover_alias\": \"" + alias + "\"\n" + - " }\n" + - "}"); + createIndexTemplate.setJsonEntity( + "{" + + "\"index_patterns\": [\"" + + index + + "-*\"], \n" + + " \"settings\": {\n" + + " \"number_of_shards\": 1,\n" + + " \"number_of_replicas\": 0,\n" + + " \"index.lifecycle.name\": \"" + + policy + + "\", \n" + + " \"index.lifecycle.rollover_alias\": \"" + + alias + + "\"\n" + + " }\n" + + "}" + ); createIndexTemplate.setOptions(expectWarnings(RestPutIndexTemplateAction.DEPRECATION_WARNING)); client().performRequest(createIndexTemplate); @@ -200,8 +229,7 @@ public void testILMRolloverOnManuallyRolledIndex() throws Exception { client(), originalIndex, alias, - Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0), + Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0), true ); @@ -247,7 +275,8 @@ public void testRolloverStepRetriesUntilRolledOverIndexIsDeleted() throws Except client(), rolledIndex, alias, - Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias), false @@ -257,30 +286,35 @@ public void testRolloverStepRetriesUntilRolledOverIndexIsDeleted() throws Except client(), index, alias, - Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) .put(LifecycleSettings.LIFECYCLE_NAME, policy) .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias), true ); - assertBusy(() -> assertThat((Integer) explainIndex(client(), index).get(FAILED_STEP_RETRY_COUNT_FIELD), greaterThanOrEqualTo(1)), + assertBusy( + () -> assertThat((Integer) explainIndex(client(), index).get(FAILED_STEP_RETRY_COUNT_FIELD), greaterThanOrEqualTo(1)), 30, - TimeUnit.SECONDS); + TimeUnit.SECONDS + ); Request moveToStepRequest = new Request("POST", "_ilm/move/" + index); - moveToStepRequest.setJsonEntity("{\n" + - " \"current_step\": {\n" + - " \"phase\": \"hot\",\n" + - " \"action\": \"rollover\",\n" + - " \"name\": \"check-rollover-ready\"\n" + - " },\n" + - " \"next_step\": {\n" + - " \"phase\": \"hot\",\n" + - " \"action\": \"rollover\",\n" + - " \"name\": \"attempt-rollover\"\n" + - " }\n" + - "}"); + moveToStepRequest.setJsonEntity( + "{\n" + + " \"current_step\": {\n" + + " \"phase\": \"hot\",\n" + + " \"action\": \"rollover\",\n" + + " \"name\": \"check-rollover-ready\"\n" + + " },\n" + + " \"next_step\": {\n" + + " \"phase\": \"hot\",\n" + + " \"action\": \"rollover\",\n" + + " \"name\": \"attempt-rollover\"\n" + + " }\n" + + "}" + ); // Using {@link #waitUntil} here as ILM moves back and forth between the {@link WaitForRolloverReadyStep} step and // {@link org.elasticsearch.xpack.core.ilm.ErrorStep} in order to retry the failing step. As {@link #assertBusy} @@ -323,7 +357,8 @@ public void testUpdateRolloverLifecycleDateStepRetriesWhenRolloverInfoIsMissing( client(), index, alias, - Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) .put(LifecycleSettings.LIFECYCLE_NAME, policy) .put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias), @@ -335,22 +370,27 @@ public void testUpdateRolloverLifecycleDateStepRetriesWhenRolloverInfoIsMissing( // moving ILM to the "update-rollover-lifecycle-date" without having gone through the actual rollover step // the "update-rollover-lifecycle-date" step will fail as the index has no rollover information Request moveToStepRequest = new Request("POST", "_ilm/move/" + index); - moveToStepRequest.setJsonEntity("{\n" + - " \"current_step\": {\n" + - " \"phase\": \"hot\",\n" + - " \"action\": \"rollover\",\n" + - " \"name\": \"check-rollover-ready\"\n" + - " },\n" + - " \"next_step\": {\n" + - " \"phase\": \"hot\",\n" + - " \"action\": \"rollover\",\n" + - " \"name\": \"update-rollover-lifecycle-date\"\n" + - " }\n" + - "}"); + moveToStepRequest.setJsonEntity( + "{\n" + + " \"current_step\": {\n" + + " \"phase\": \"hot\",\n" + + " \"action\": \"rollover\",\n" + + " \"name\": \"check-rollover-ready\"\n" + + " },\n" + + " \"next_step\": {\n" + + " \"phase\": \"hot\",\n" + + " \"action\": \"rollover\",\n" + + " \"name\": \"update-rollover-lifecycle-date\"\n" + + " }\n" + + "}" + ); client().performRequest(moveToStepRequest); - assertBusy(() -> assertThat((Integer) explainIndex(client(), index).get(FAILED_STEP_RETRY_COUNT_FIELD), greaterThanOrEqualTo(1)), - 30, TimeUnit.SECONDS); + assertBusy( + () -> assertThat((Integer) explainIndex(client(), index).get(FAILED_STEP_RETRY_COUNT_FIELD), greaterThanOrEqualTo(1)), + 30, + TimeUnit.SECONDS + ); index(client(), index, "1", "foo", "bar"); Request refreshIndex = new Request("POST", "/" + index + "/_refresh"); diff --git a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/actions/RollupActionIT.java b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/actions/RollupActionIT.java index 0ce68b3f5f46b..a2a83bcd6b64a 100644 --- a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/actions/RollupActionIT.java +++ b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/actions/RollupActionIT.java @@ -50,12 +50,17 @@ public void refreshIndex() { } public void testRollupIndex() throws Exception { - createIndexWithSettings(client(), index, alias, Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)); + createIndexWithSettings( + client(), + index, + alias, + Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + ); index(client(), index, "_id", "timestamp", "2020-01-01T05:10:00Z", "volume", 11.0); RollupActionConfig rollupConfig = new RollupActionConfig( new RollupActionGroupConfig(new RollupActionDateHistogramGroupConfig.FixedInterval("timestamp", DateHistogramInterval.DAY)), - Collections.singletonList(new MetricConfig("volume", Collections.singletonList("max")))); + Collections.singletonList(new MetricConfig("volume", Collections.singletonList("max"))) + ); createNewSingletonPolicy(client(), policy, "cold", new RollupILMAction(rollupConfig, null)); updatePolicy(client(), index, policy); @@ -68,12 +73,17 @@ public void testRollupIndex() throws Exception { } public void testRollupIndexAndSetNewRollupPolicy() throws Exception { - createIndexWithSettings(client(), index, alias, Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)); + createIndexWithSettings( + client(), + index, + alias, + Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + ); index(client(), index, "_id", "timestamp", "2020-01-01T05:10:00Z", "volume", 11.0); RollupActionConfig rollupConfig = new RollupActionConfig( new RollupActionGroupConfig(new RollupActionDateHistogramGroupConfig.FixedInterval("timestamp", DateHistogramInterval.DAY)), - Collections.singletonList(new MetricConfig("volume", Collections.singletonList("max")))); + Collections.singletonList(new MetricConfig("volume", Collections.singletonList("max"))) + ); createNewSingletonPolicy(client(), policy, "cold", new RollupILMAction(rollupConfig, policy)); updatePolicy(client(), index, policy); diff --git a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/actions/SearchableSnapshotActionIT.java b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/actions/SearchableSnapshotActionIT.java index 52bc351e579ac..9e66d1a64cf83 100644 --- a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/actions/SearchableSnapshotActionIT.java +++ b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/actions/SearchableSnapshotActionIT.java @@ -18,11 +18,11 @@ import org.elasticsearch.cluster.routing.allocation.DataTier; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.test.rest.ESRestTestCase; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.rest.ESRestTestCase; import org.elasticsearch.xpack.core.ilm.DeleteAction; import org.elasticsearch.xpack.core.ilm.ForceMergeAction; import org.elasticsearch.xpack.core.ilm.FreezeAction; @@ -73,8 +73,13 @@ public void refreshIndex() { dataStream = "logs-" + randomAlphaOfLength(10).toLowerCase(Locale.ROOT); policy = "policy-" + randomAlphaOfLength(5); snapshotRepo = randomAlphaOfLengthBetween(10, 20); - logger.info("--> running [{}] with data stream [{}], snapshot repo [{}] and policy [{}]", getTestName(), dataStream, - snapshotRepo, policy); + logger.info( + "--> running [{}] with data stream [{}], snapshot repo [{}] and policy [{}]", + getTestName(), + dataStream, + snapshotRepo, + policy + ); } @Override @@ -86,8 +91,12 @@ public void testSearchableSnapshotAction() throws Exception { createSnapshotRepo(client(), snapshotRepo, randomBoolean()); createNewSingletonPolicy(client(), policy, "cold", new SearchableSnapshotAction(snapshotRepo, true)); - createComposableTemplate(client(), randomAlphaOfLengthBetween(5, 10).toLowerCase(), dataStream, - new Template(Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, policy).build(), null, null)); + createComposableTemplate( + client(), + randomAlphaOfLengthBetween(5, 10).toLowerCase(), + dataStream, + new Template(Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, policy).build(), null, null) + ); indexDocument(client(), dataStream, true); @@ -104,8 +113,11 @@ public void testSearchableSnapshotAction() throws Exception { } }, 30, TimeUnit.SECONDS)); - assertBusy(() -> assertThat(explainIndex(client(), restoredIndexName).get("step"), is(PhaseCompleteStep.NAME)), 30, - TimeUnit.SECONDS); + assertBusy( + () -> assertThat(explainIndex(client(), restoredIndexName).get("step"), is(PhaseCompleteStep.NAME)), + 30, + TimeUnit.SECONDS + ); } public void testSearchableSnapshotForceMergesIndexToOneSegment() throws Exception { @@ -156,8 +168,11 @@ public void testSearchableSnapshotForceMergesIndexToOneSegment() throws Exceptio } }, 60, TimeUnit.SECONDS)); - assertBusy(() -> assertThat(explainIndex(client(), restoredIndexName).get("step"), is(PhaseCompleteStep.NAME)), 30, - TimeUnit.SECONDS); + assertBusy( + () -> assertThat(explainIndex(client(), restoredIndexName).get("step"), is(PhaseCompleteStep.NAME)), + 30, + TimeUnit.SECONDS + ); } @SuppressWarnings("unchecked") @@ -165,24 +180,28 @@ public void testDeleteActionDeletesSearchableSnapshot() throws Exception { createSnapshotRepo(client(), snapshotRepo, randomBoolean()); // create policy with cold and delete phases - Map coldActions = - Map.of(SearchableSnapshotAction.NAME, new SearchableSnapshotAction(snapshotRepo)); + Map coldActions = Map.of(SearchableSnapshotAction.NAME, new SearchableSnapshotAction(snapshotRepo)); Map phases = new HashMap<>(); phases.put("cold", new Phase("cold", TimeValue.ZERO, coldActions)); - phases.put("delete", new Phase("delete", TimeValue.timeValueMillis(10000), singletonMap(DeleteAction.NAME, - new DeleteAction(true)))); + phases.put( + "delete", + new Phase("delete", TimeValue.timeValueMillis(10000), singletonMap(DeleteAction.NAME, new DeleteAction(true))) + ); LifecyclePolicy lifecyclePolicy = new LifecyclePolicy(policy, phases); // PUT policy XContentBuilder builder = jsonBuilder(); lifecyclePolicy.toXContent(builder, null); - final StringEntity entity = new StringEntity( - "{ \"policy\":" + Strings.toString(builder) + "}", ContentType.APPLICATION_JSON); + final StringEntity entity = new StringEntity("{ \"policy\":" + Strings.toString(builder) + "}", ContentType.APPLICATION_JSON); Request createPolicyRequest = new Request("PUT", "_ilm/policy/" + policy); createPolicyRequest.setEntity(entity); assertOK(client().performRequest(createPolicyRequest)); - createComposableTemplate(client(), randomAlphaOfLengthBetween(5, 10).toLowerCase(), dataStream, - new Template(Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, policy).build(), null, null)); + createComposableTemplate( + client(), + randomAlphaOfLengthBetween(5, 10).toLowerCase(), + dataStream, + new Template(Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, policy).build(), null, null) + ); indexDocument(client(), dataStream, true); @@ -215,33 +234,67 @@ public void testDeleteActionDeletesSearchableSnapshot() throws Exception { } public void testCreateInvalidPolicy() { - ResponseException exception = expectThrows(ResponseException.class, () -> createPolicy(client(), policy, - new Phase("hot", TimeValue.ZERO, Map.of(RolloverAction.NAME, new RolloverAction(null, null, null, 1L), SearchableSnapshotAction.NAME, - new SearchableSnapshotAction(randomAlphaOfLengthBetween(4, 10)))), - new Phase("warm", TimeValue.ZERO, Map.of(ForceMergeAction.NAME, new ForceMergeAction(1, null))), - new Phase("cold", TimeValue.ZERO, Map.of(FreezeAction.NAME, new FreezeAction())), - null, null + ResponseException exception = expectThrows( + ResponseException.class, + () -> createPolicy( + client(), + policy, + new Phase( + "hot", + TimeValue.ZERO, + Map.of( + RolloverAction.NAME, + new RolloverAction(null, null, null, 1L), + SearchableSnapshotAction.NAME, + new SearchableSnapshotAction(randomAlphaOfLengthBetween(4, 10)) + ) + ), + new Phase("warm", TimeValue.ZERO, Map.of(ForceMergeAction.NAME, new ForceMergeAction(1, null))), + new Phase("cold", TimeValue.ZERO, Map.of(FreezeAction.NAME, new FreezeAction())), + null, + null ) ); - assertThat(exception.getMessage(), containsString("phases [warm,cold] define one or more of [forcemerge, freeze, shrink, rollup]" + - " actions which are not allowed after a managed index is mounted as a searchable snapshot")); + assertThat( + exception.getMessage(), + containsString( + "phases [warm,cold] define one or more of [forcemerge, freeze, shrink, rollup]" + + " actions which are not allowed after a managed index is mounted as a searchable snapshot" + ) + ); } public void testUpdatePolicyToAddPhasesYieldsInvalidActionsToBeSkipped() throws Exception { createSnapshotRepo(client(), snapshotRepo, randomBoolean()); - createPolicy(client(), policy, - new Phase("hot", TimeValue.ZERO, Map.of(RolloverAction.NAME, new RolloverAction(null, null, null, 1L), SearchableSnapshotAction.NAME, - new SearchableSnapshotAction(snapshotRepo))), + createPolicy( + client(), + policy, + new Phase( + "hot", + TimeValue.ZERO, + Map.of( + RolloverAction.NAME, + new RolloverAction(null, null, null, 1L), + SearchableSnapshotAction.NAME, + new SearchableSnapshotAction(snapshotRepo) + ) + ), new Phase("warm", TimeValue.timeValueDays(30), Map.of(SetPriorityAction.NAME, new SetPriorityAction(999))), - null, null, null + null, + null, + null ); - createComposableTemplate(client(), randomAlphaOfLengthBetween(5, 10).toLowerCase(), dataStream, - new Template(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 5) - .put(LifecycleSettings.LIFECYCLE_NAME, policy) - .build(), null, null) + createComposableTemplate( + client(), + randomAlphaOfLengthBetween(5, 10).toLowerCase(), + dataStream, + new Template( + Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 5).put(LifecycleSettings.LIFECYCLE_NAME, policy).build(), + null, + null + ) ); // rolling over the data stream so we can apply the searchable snapshot policy to a backing index that's not the write index @@ -249,7 +302,10 @@ public void testUpdatePolicyToAddPhasesYieldsInvalidActionsToBeSkipped() throws indexDocument(client(), dataStream, true); } - String restoredIndexName = SearchableSnapshotAction.FULL_RESTORED_INDEX_PREFIX + DataStream.getDefaultBackingIndexName(dataStream, 1L); + String restoredIndexName = SearchableSnapshotAction.FULL_RESTORED_INDEX_PREFIX + DataStream.getDefaultBackingIndexName( + dataStream, + 1L + ); assertTrue(waitUntil(() -> { try { return indexExists(restoredIndexName); @@ -264,13 +320,18 @@ public void testUpdatePolicyToAddPhasesYieldsInvalidActionsToBeSkipped() throws assertThat(stepKeyForIndex.getName(), is(PhaseCompleteStep.NAME)); }, 30, TimeUnit.SECONDS); - createPolicy(client(), policy, + createPolicy( + client(), + policy, new Phase("hot", TimeValue.ZERO, Map.of(SetPriorityAction.NAME, new SetPriorityAction(10))), - new Phase("warm", TimeValue.ZERO, + new Phase( + "warm", + TimeValue.ZERO, Map.of(ShrinkAction.NAME, new ShrinkAction(1, null), ForceMergeAction.NAME, new ForceMergeAction(1, null)) ), new Phase("cold", TimeValue.ZERO, Map.of(SearchableSnapshotAction.NAME, new SearchableSnapshotAction(snapshotRepo))), - null, null + null, + null ); // even though the index is now mounted as a searchable snapshot, the actions that can't operate on it should @@ -285,26 +346,44 @@ public void testUpdatePolicyToAddPhasesYieldsInvalidActionsToBeSkipped() throws public void testRestoredIndexManagedByLocalPolicySkipsIllegalActions() throws Exception { // let's create a data stream, rollover it and convert the first generation backing index into a searchable snapshot createSnapshotRepo(client(), snapshotRepo, randomBoolean()); - createPolicy(client(), policy, - new Phase("hot", TimeValue.ZERO, Map.of(RolloverAction.NAME, new RolloverAction(null, null, null, 1L), - SearchableSnapshotAction.NAME, new SearchableSnapshotAction(snapshotRepo))), + createPolicy( + client(), + policy, + new Phase( + "hot", + TimeValue.ZERO, + Map.of( + RolloverAction.NAME, + new RolloverAction(null, null, null, 1L), + SearchableSnapshotAction.NAME, + new SearchableSnapshotAction(snapshotRepo) + ) + ), new Phase("warm", TimeValue.timeValueDays(30), Map.of(SetPriorityAction.NAME, new SetPriorityAction(999))), - null, null, null + null, + null, + null ); - createComposableTemplate(client(), randomAlphaOfLengthBetween(5, 10).toLowerCase(), dataStream, - new Template(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 5) - .put(LifecycleSettings.LIFECYCLE_NAME, policy) - .build(), null, null) + createComposableTemplate( + client(), + randomAlphaOfLengthBetween(5, 10).toLowerCase(), + dataStream, + new Template( + Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 5).put(LifecycleSettings.LIFECYCLE_NAME, policy).build(), + null, + null + ) ); // rolling over the data stream so we can apply the searchable snapshot policy to a backing index that's not the write index // indexing only one document as we want only one rollover to be triggered indexDocument(client(), dataStream, true); - String searchableSnapMountedIndexName = SearchableSnapshotAction.FULL_RESTORED_INDEX_PREFIX + - DataStream.getDefaultBackingIndexName(dataStream, 1L); + String searchableSnapMountedIndexName = SearchableSnapshotAction.FULL_RESTORED_INDEX_PREFIX + DataStream.getDefaultBackingIndexName( + dataStream, + 1L + ); assertTrue(waitUntil(() -> { try { return indexExists(searchableSnapMountedIndexName); @@ -331,13 +410,18 @@ SearchableSnapshotAction.NAME, new SearchableSnapshotAction(snapshotRepo))), // snapshot) assertOK(client().performRequest(new Request("DELETE", "/_data_stream/" + dataStream))); - createPolicy(client(), policy, + createPolicy( + client(), + policy, new Phase("hot", TimeValue.ZERO, Map.of()), - new Phase("warm", TimeValue.ZERO, + new Phase( + "warm", + TimeValue.ZERO, Map.of(ShrinkAction.NAME, new ShrinkAction(1, null), ForceMergeAction.NAME, new ForceMergeAction(1, null)) ), new Phase("cold", TimeValue.ZERO, Map.of(FreezeAction.NAME, new FreezeAction())), - null, null + null, + null ); // restore the datastream @@ -364,15 +448,26 @@ public void testIdenticalSearchableSnapshotActionIsNoop() throws Exception { Map hotActions = new HashMap<>(); hotActions.put(RolloverAction.NAME, new RolloverAction(null, null, null, 1L)); hotActions.put(SearchableSnapshotAction.NAME, new SearchableSnapshotAction(snapshotRepo, randomBoolean())); - createPolicy(client(), policy, null, null, + createPolicy( + client(), + policy, + null, + null, new Phase("hot", TimeValue.ZERO, hotActions), - new Phase("cold", TimeValue.ZERO, - singletonMap(SearchableSnapshotAction.NAME, new SearchableSnapshotAction(snapshotRepo, randomBoolean()))), + new Phase( + "cold", + TimeValue.ZERO, + singletonMap(SearchableSnapshotAction.NAME, new SearchableSnapshotAction(snapshotRepo, randomBoolean())) + ), null ); - createIndex(index, Settings.builder().put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, "alias").build(), - null, "\"alias\": {\"is_write_index\": true}"); + createIndex( + index, + Settings.builder().put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, "alias").build(), + null, + "\"alias\": {\"is_write_index\": true}" + ); ensureGreen(index); indexDocument(client(), index, true); @@ -399,8 +494,11 @@ public void testIdenticalSearchableSnapshotActionIsNoop() throws Exception { try (InputStream is = response.getEntity().getContent()) { responseMap = XContentHelper.convertToMap(XContentType.JSON.xContent(), is, true); } - assertThat("expected to have only one snapshot, but got: " + responseMap, - ((List>) responseMap.get("snapshots")).size(), equalTo(1)); + assertThat( + "expected to have only one snapshot, but got: " + responseMap, + ((List>) responseMap.get("snapshots")).size(), + equalTo(1) + ); Request hitCount = new Request("GET", "/" + searchableSnapMountedIndexName + "/_count"); Map count = entityAsMap(client().performRequest(hitCount)); @@ -411,11 +509,21 @@ public void testIdenticalSearchableSnapshotActionIsNoop() throws Exception { public void testConvertingSearchableSnapshotFromFullToPartial() throws Exception { String index = "myindex-" + randomAlphaOfLength(4).toLowerCase(Locale.ROOT); createSnapshotRepo(client(), snapshotRepo, randomBoolean()); - createPolicy(client(), policy, null, null, - new Phase("cold", TimeValue.ZERO, - singletonMap(SearchableSnapshotAction.NAME, new SearchableSnapshotAction(snapshotRepo, randomBoolean()))), - new Phase("frozen", TimeValue.ZERO, - singletonMap(SearchableSnapshotAction.NAME, new SearchableSnapshotAction(snapshotRepo, randomBoolean()))), + createPolicy( + client(), + policy, + null, + null, + new Phase( + "cold", + TimeValue.ZERO, + singletonMap(SearchableSnapshotAction.NAME, new SearchableSnapshotAction(snapshotRepo, randomBoolean())) + ), + new Phase( + "frozen", + TimeValue.ZERO, + singletonMap(SearchableSnapshotAction.NAME, new SearchableSnapshotAction(snapshotRepo, randomBoolean())) + ), null ); @@ -427,8 +535,8 @@ public void testConvertingSearchableSnapshotFromFullToPartial() throws Exception // `index_not_found_exception` updateIndexSettings(index, Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, policy)); - final String searchableSnapMountedIndexName = SearchableSnapshotAction.PARTIAL_RESTORED_INDEX_PREFIX + - SearchableSnapshotAction.FULL_RESTORED_INDEX_PREFIX + index; + final String searchableSnapMountedIndexName = SearchableSnapshotAction.PARTIAL_RESTORED_INDEX_PREFIX + + SearchableSnapshotAction.FULL_RESTORED_INDEX_PREFIX + index; assertBusy(() -> { logger.info("--> waiting for [{}] to exist...", searchableSnapMountedIndexName); @@ -447,16 +555,21 @@ public void testConvertingSearchableSnapshotFromFullToPartial() throws Exception try (InputStream is = response.getEntity().getContent()) { responseMap = XContentHelper.convertToMap(XContentType.JSON.xContent(), is, true); } - assertThat("expected to have only one snapshot, but got: " + responseMap, - ((List>) responseMap.get("snapshots")).size(), equalTo(1)); + assertThat( + "expected to have only one snapshot, but got: " + responseMap, + ((List>) responseMap.get("snapshots")).size(), + equalTo(1) + ); Request hitCount = new Request("GET", "/" + searchableSnapMountedIndexName + "/_count"); Map count = entityAsMap(client().performRequest(hitCount)); assertThat("expected a single document but got: " + count, (int) count.get("count"), equalTo(1)); - assertBusy(() -> assertTrue( - "Expecting the mounted index to be deleted and to be converted to an alias", - aliasExists(searchableSnapMountedIndexName, SearchableSnapshotAction.FULL_RESTORED_INDEX_PREFIX + index)) + assertBusy( + () -> assertTrue( + "Expecting the mounted index to be deleted and to be converted to an alias", + aliasExists(searchableSnapMountedIndexName, SearchableSnapshotAction.FULL_RESTORED_INDEX_PREFIX + index) + ) ); } @@ -464,34 +577,63 @@ public void testSecondSearchableSnapshotUsingDifferentRepoThrows() throws Except String secondRepo = randomAlphaOfLengthBetween(10, 20); createSnapshotRepo(client(), snapshotRepo, randomBoolean()); createSnapshotRepo(client(), secondRepo, randomBoolean()); - ResponseException e = expectThrows(ResponseException.class, () -> - createPolicy(client(), policy, null, null, - new Phase("cold", TimeValue.ZERO, - singletonMap(SearchableSnapshotAction.NAME, new SearchableSnapshotAction(snapshotRepo, randomBoolean()))), - new Phase("frozen", TimeValue.ZERO, - singletonMap(SearchableSnapshotAction.NAME, new SearchableSnapshotAction(secondRepo, randomBoolean()))), + ResponseException e = expectThrows( + ResponseException.class, + () -> createPolicy( + client(), + policy, + null, + null, + new Phase( + "cold", + TimeValue.ZERO, + singletonMap(SearchableSnapshotAction.NAME, new SearchableSnapshotAction(snapshotRepo, randomBoolean())) + ), + new Phase( + "frozen", + TimeValue.ZERO, + singletonMap(SearchableSnapshotAction.NAME, new SearchableSnapshotAction(secondRepo, randomBoolean())) + ), null - )); + ) + ); - assertThat(e.getMessage(), - containsString("policy specifies [searchable_snapshot] action multiple times with differing repositories")); + assertThat( + e.getMessage(), + containsString("policy specifies [searchable_snapshot] action multiple times with differing repositories") + ); } public void testSearchableSnapshotsInHotPhasePinnedToHotNodes() throws Exception { createSnapshotRepo(client(), snapshotRepo, randomBoolean()); - createPolicy(client(), policy, - new Phase("hot", TimeValue.ZERO, Map.of(RolloverAction.NAME, new RolloverAction(null, null, null, 1L), - SearchableSnapshotAction.NAME, new SearchableSnapshotAction( - snapshotRepo, randomBoolean())) + createPolicy( + client(), + policy, + new Phase( + "hot", + TimeValue.ZERO, + Map.of( + RolloverAction.NAME, + new RolloverAction(null, null, null, 1L), + SearchableSnapshotAction.NAME, + new SearchableSnapshotAction(snapshotRepo, randomBoolean()) + ) ), - null, null, null, null + null, + null, + null, + null ); - createComposableTemplate(client(), randomAlphaOfLengthBetween(5, 10).toLowerCase(), dataStream, - new Template(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(LifecycleSettings.LIFECYCLE_NAME, policy) - .build(), null, null) + createComposableTemplate( + client(), + randomAlphaOfLengthBetween(5, 10).toLowerCase(), + dataStream, + new Template( + Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(LifecycleSettings.LIFECYCLE_NAME, policy).build(), + null, + null + ) ); indexDocument(client(), dataStream, true); @@ -507,12 +649,14 @@ snapshotRepo, randomBoolean())) logger.info("--> waiting for [{}] to exist...", restoredIndex); assertTrue(indexExists(restoredIndex)); }, 30, TimeUnit.SECONDS); - assertBusy(() -> assertThat(getStepKeyForIndex(client(), restoredIndex), is(PhaseCompleteStep.finalStep("hot").getKey())), - 30, TimeUnit.SECONDS); + assertBusy( + () -> assertThat(getStepKeyForIndex(client(), restoredIndex), is(PhaseCompleteStep.finalStep("hot").getKey())), + 30, + TimeUnit.SECONDS + ); Map hotIndexSettings = getIndexSettingsAsMap(restoredIndex); // searchable snapshots mounted in the hot phase should be pinned to hot nodes - assertThat(hotIndexSettings.get(DataTier.TIER_PREFERENCE), - is("data_hot")); + assertThat(hotIndexSettings.get(DataTier.TIER_PREFERENCE), is("data_hot")); } } diff --git a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/actions/ShrinkActionIT.java b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/actions/ShrinkActionIT.java index 577bf09fe8d5c..724603fa33ad0 100644 --- a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/actions/ShrinkActionIT.java +++ b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/actions/ShrinkActionIT.java @@ -17,10 +17,10 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.rest.action.admin.indices.RestPutIndexTemplateAction; import org.elasticsearch.test.rest.ESRestTestCase; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.ilm.CheckTargetShardsCountStep; import org.elasticsearch.xpack.core.ilm.LifecycleAction; import org.elasticsearch.xpack.core.ilm.LifecyclePolicy; @@ -74,16 +74,21 @@ public void testShrinkAction() throws Exception { int numShards = 4; int divisor = randomFrom(2, 4); int expectedFinalShards = numShards / divisor; - createIndexWithSettings(client(), index, alias, Settings.builder().put(SETTING_NUMBER_OF_SHARDS, numShards) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)); + createIndexWithSettings( + client(), + index, + alias, + Settings.builder().put(SETTING_NUMBER_OF_SHARDS, numShards).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + ); createNewSingletonPolicy(client(), policy, "warm", new ShrinkAction(expectedFinalShards, null)); updatePolicy(client(), index, policy); String shrunkenIndexName = waitAndGetShrinkIndexName(client(), index); assertBusy(() -> assertTrue(indexExists(shrunkenIndexName)), 30, TimeUnit.SECONDS); assertBusy(() -> assertTrue(aliasExists(shrunkenIndexName, index))); - assertBusy(() -> assertThat(getStepKeyForIndex(client(), shrunkenIndexName), - equalTo(PhaseCompleteStep.finalStep("warm").getKey()))); + assertBusy( + () -> assertThat(getStepKeyForIndex(client(), shrunkenIndexName), equalTo(PhaseCompleteStep.finalStep("warm").getKey())) + ); assertBusy(() -> { Map settings = getOnlyIndexSettings(client(), shrunkenIndexName); assertThat(settings.get(SETTING_NUMBER_OF_SHARDS), equalTo(String.valueOf(expectedFinalShards))); @@ -95,8 +100,12 @@ public void testShrinkAction() throws Exception { public void testShrinkSameShards() throws Exception { int numberOfShards = randomFrom(1, 2); - createIndexWithSettings(client(), index, alias, Settings.builder().put(SETTING_NUMBER_OF_SHARDS, numberOfShards) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)); + createIndexWithSettings( + client(), + index, + alias, + Settings.builder().put(SETTING_NUMBER_OF_SHARDS, numberOfShards).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + ); createNewSingletonPolicy(client(), policy, "warm", new ShrinkAction(numberOfShards, null)); updatePolicy(client(), index, policy); assertBusy(() -> { @@ -114,25 +123,33 @@ public void testShrinkSameShards() throws Exception { public void testShrinkDuringSnapshot() throws Exception { // Create the repository before taking the snapshot. Request request = new Request("PUT", "/_snapshot/repo"); - request.setJsonEntity(Strings - .toString(JsonXContent.contentBuilder() - .startObject() - .field("type", "fs") - .startObject("settings") - .field("compress", randomBoolean()) - .field("location", System.getProperty("tests.path.repo")) - .field("max_snapshot_bytes_per_sec", "256b") - .endObject() - .endObject())); + request.setJsonEntity( + Strings.toString( + JsonXContent.contentBuilder() + .startObject() + .field("type", "fs") + .startObject("settings") + .field("compress", randomBoolean()) + .field("location", System.getProperty("tests.path.repo")) + .field("max_snapshot_bytes_per_sec", "256b") + .endObject() + .endObject() + ) + ); assertOK(client().performRequest(request)); // create delete policy createNewSingletonPolicy(client(), policy, "warm", new ShrinkAction(1, null), TimeValue.timeValueMillis(0)); // create index without policy - createIndexWithSettings(client(), index, alias, Settings.builder() - .put(SETTING_NUMBER_OF_SHARDS, 2) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - // required so the shrink doesn't wait on SetSingleNodeAllocateStep - .put(IndexMetadata.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + "_name", "javaRestTest-0")); + createIndexWithSettings( + client(), + index, + alias, + Settings.builder() + .put(SETTING_NUMBER_OF_SHARDS, 2) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + // required so the shrink doesn't wait on SetSingleNodeAllocateStep + .put(IndexMetadata.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + "_name", "javaRestTest-0") + ); // index document so snapshot actually does something indexDocument(client(), index); // start snapshot @@ -166,10 +183,12 @@ public void testShrinkActionInTheHotPhase() throws Exception { // add a policy Map hotActions = Map.of( - RolloverAction.NAME, new RolloverAction(null, null, null, 1L), - ShrinkAction.NAME, new ShrinkAction(expectedFinalShards, null)); - Map phases = Map.of( - "hot", new Phase("hot", TimeValue.ZERO, hotActions)); + RolloverAction.NAME, + new RolloverAction(null, null, null, 1L), + ShrinkAction.NAME, + new ShrinkAction(expectedFinalShards, null) + ); + Map phases = Map.of("hot", new Phase("hot", TimeValue.ZERO, hotActions)); LifecyclePolicy lifecyclePolicy = new LifecyclePolicy(policy, phases); Request createPolicyRequest = new Request("PUT", "_ilm/policy/" + policy); createPolicyRequest.setJsonEntity("{ \"policy\":" + Strings.toString(lifecyclePolicy) + "}"); @@ -177,15 +196,25 @@ RolloverAction.NAME, new RolloverAction(null, null, null, 1L), // and a template Request createTemplateRequest = new Request("PUT", "_template/" + index); - createTemplateRequest.setJsonEntity("{" + - "\"index_patterns\": [\"" + index + "-*\"], \n" + - " \"settings\": {\n" + - " \"number_of_shards\": " + numShards + ",\n" + - " \"number_of_replicas\": 0,\n" + - " \"index.lifecycle.name\": \"" + policy + "\", \n" + - " \"index.lifecycle.rollover_alias\": \"" + alias + "\"\n" + - " }\n" + - "}"); + createTemplateRequest.setJsonEntity( + "{" + + "\"index_patterns\": [\"" + + index + + "-*\"], \n" + + " \"settings\": {\n" + + " \"number_of_shards\": " + + numShards + + ",\n" + + " \"number_of_replicas\": 0,\n" + + " \"index.lifecycle.name\": \"" + + policy + + "\", \n" + + " \"index.lifecycle.rollover_alias\": \"" + + alias + + "\"\n" + + " }\n" + + "}" + ); createTemplateRequest.setOptions(expectWarnings(RestPutIndexTemplateAction.DEPRECATION_WARNING)); client().performRequest(createTemplateRequest); @@ -205,20 +234,23 @@ RolloverAction.NAME, new RolloverAction(null, null, null, 1L), public void testSetSingleNodeAllocationRetriesUntilItSucceeds() throws Exception { int numShards = 2; int expectedFinalShards = 1; - createIndexWithSettings(client(), index, alias, Settings.builder() - .put(SETTING_NUMBER_OF_SHARDS, numShards) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .putNull(DataTier.TIER_PREFERENCE)); + createIndexWithSettings( + client(), + index, + alias, + Settings.builder() + .put(SETTING_NUMBER_OF_SHARDS, numShards) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .putNull(DataTier.TIER_PREFERENCE) + ); ensureGreen(index); // unallocate all index shards Request setAllocationToMissingAttribute = new Request("PUT", "/" + index + "/_settings"); - setAllocationToMissingAttribute.setJsonEntity("{\n" + - " \"settings\": {\n" + - " \"index.routing.allocation.include.rack\": \"bogus_rack\"" + - " }\n" + - "}"); + setAllocationToMissingAttribute.setJsonEntity( + "{\n" + " \"settings\": {\n" + " \"index.routing.allocation.include.rack\": \"bogus_rack\"" + " }\n" + "}" + ); client().performRequest(setAllocationToMissingAttribute); ensureHealth(index, (request) -> { @@ -231,13 +263,15 @@ public void testSetSingleNodeAllocationRetriesUntilItSucceeds() throws Exception // all shards to be active and we want that to happen as part of the shrink action) MigrateAction migrateAction = new MigrateAction(false); ShrinkAction shrinkAction = new ShrinkAction(expectedFinalShards, null); - Phase phase = new Phase("warm", TimeValue.ZERO, Map.of(migrateAction.getWriteableName(), migrateAction, - shrinkAction.getWriteableName(), shrinkAction)); + Phase phase = new Phase( + "warm", + TimeValue.ZERO, + Map.of(migrateAction.getWriteableName(), migrateAction, shrinkAction.getWriteableName(), shrinkAction) + ); LifecyclePolicy lifecyclePolicy = new LifecyclePolicy(policy, singletonMap(phase.getName(), phase)); XContentBuilder builder = jsonBuilder(); lifecyclePolicy.toXContent(builder, null); - final StringEntity entity = new StringEntity( - "{ \"policy\":" + Strings.toString(builder) + "}", ContentType.APPLICATION_JSON); + final StringEntity entity = new StringEntity("{ \"policy\":" + Strings.toString(builder) + "}", ContentType.APPLICATION_JSON); Request putPolicyRequest = new Request("PUT", "_ilm/policy/" + policy); putPolicyRequest.setEntity(entity); client().performRequest(putPolicyRequest); @@ -258,11 +292,9 @@ public void testSetSingleNodeAllocationRetriesUntilItSucceeds() throws Exception }, 30, TimeUnit.SECONDS)); Request resetAllocationForIndex = new Request("PUT", "/" + index + "/_settings"); - resetAllocationForIndex.setJsonEntity("{\n" + - " \"settings\": {\n" + - " \"index.routing.allocation.include.rack\": null" + - " }\n" + - "}"); + resetAllocationForIndex.setJsonEntity( + "{\n" + " \"settings\": {\n" + " \"index.routing.allocation.include.rack\": null" + " }\n" + "}" + ); client().performRequest(resetAllocationForIndex); String shrunkenIndex = waitAndGetShrinkIndexName(client(), index); @@ -276,12 +308,22 @@ public void testAutomaticRetryFailedShrinkAction() throws Exception { int numShards = 4; int divisor = randomFrom(2, 4); int expectedFinalShards = numShards / divisor; - createIndexWithSettings(client(), index, alias, Settings.builder().put(SETTING_NUMBER_OF_SHARDS, numShards) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)); + createIndexWithSettings( + client(), + index, + alias, + Settings.builder().put(SETTING_NUMBER_OF_SHARDS, numShards).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + ); createNewSingletonPolicy(client(), policy, "warm", new ShrinkAction(numShards + randomIntBetween(1, numShards), null)); updatePolicy(client(), index, policy); - assertBusy(() -> assertThat(getStepKeyForIndex(client(), index), - equalTo(new Step.StepKey("warm", ShrinkAction.NAME, CheckTargetShardsCountStep.NAME))), 60, TimeUnit.SECONDS); + assertBusy( + () -> assertThat( + getStepKeyForIndex(client(), index), + equalTo(new Step.StepKey("warm", ShrinkAction.NAME, CheckTargetShardsCountStep.NAME)) + ), + 60, + TimeUnit.SECONDS + ); // update policy to be correct createNewSingletonPolicy(client(), policy, "warm", new ShrinkAction(expectedFinalShards, null)); @@ -309,17 +351,24 @@ public void testTotalShardsPerNodeTooLow() throws Exception { int numShards = 4; int divisor = randomFrom(2, 4); int expectedFinalShards = numShards / divisor; - createIndexWithSettings(client(), index, alias, Settings.builder().put(SETTING_NUMBER_OF_SHARDS, numShards) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put(ShardsLimitAllocationDecider.INDEX_TOTAL_SHARDS_PER_NODE_SETTING.getKey(), numShards - 2)); + createIndexWithSettings( + client(), + index, + alias, + Settings.builder() + .put(SETTING_NUMBER_OF_SHARDS, numShards) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put(ShardsLimitAllocationDecider.INDEX_TOTAL_SHARDS_PER_NODE_SETTING.getKey(), numShards - 2) + ); createNewSingletonPolicy(client(), policy, "warm", new ShrinkAction(expectedFinalShards, null)); updatePolicy(client(), index, policy); String shrunkenIndexName = waitAndGetShrinkIndexName(client(), index); assertBusy(() -> assertTrue(indexExists(shrunkenIndexName)), 60, TimeUnit.SECONDS); assertBusy(() -> assertTrue(aliasExists(shrunkenIndexName, index))); - assertBusy(() -> assertThat(getStepKeyForIndex(client(), shrunkenIndexName), - equalTo(PhaseCompleteStep.finalStep("warm").getKey()))); + assertBusy( + () -> assertThat(getStepKeyForIndex(client(), shrunkenIndexName), equalTo(PhaseCompleteStep.finalStep("warm").getKey())) + ); assertBusy(() -> { Map settings = getOnlyIndexSettings(client(), shrunkenIndexName); assertThat(settings.get(SETTING_NUMBER_OF_SHARDS), equalTo(String.valueOf(expectedFinalShards))); diff --git a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/slm/SnapshotLifecycleRestIT.java b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/slm/SnapshotLifecycleRestIT.java index 718c9fb60d3d6..6d4f96193144b 100644 --- a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/slm/SnapshotLifecycleRestIT.java +++ b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/slm/SnapshotLifecycleRestIT.java @@ -21,17 +21,17 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.compress.CompressedXContent; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.test.junit.annotations.TestIssueLogging; +import org.elasticsearch.test.rest.ESRestTestCase; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.junit.annotations.TestIssueLogging; -import org.elasticsearch.test.rest.ESRestTestCase; import org.elasticsearch.xpack.core.ilm.LifecycleSettings; import org.elasticsearch.xpack.core.ilm.Step; import org.elasticsearch.xpack.core.ilm.WaitForRolloverReadyStep; @@ -81,8 +81,14 @@ protected boolean preserveILMPoliciesUponCompletion() { } public void testMissingRepo() throws Exception { - SnapshotLifecyclePolicy policy = new SnapshotLifecyclePolicy("missing-repo-policy", "snap", - "0 0/15 * * * ?", "missing-repo", Collections.emptyMap(), SnapshotRetentionConfiguration.EMPTY); + SnapshotLifecyclePolicy policy = new SnapshotLifecyclePolicy( + "missing-repo-policy", + "snap", + "0 0/15 * * * ?", + "missing-repo", + Collections.emptyMap(), + SnapshotRetentionConfiguration.EMPTY + ); Request putLifecycle = new Request("PUT", "/_slm/policy/missing-repo-policy"); XContentBuilder lifecycleBuilder = JsonXContent.contentBuilder(); @@ -220,8 +226,10 @@ public void testPolicyFailure() throws Exception { } @SuppressWarnings("unchecked") - @TestIssueLogging(value = "org.elasticsearch.xpack.slm:TRACE,org.elasticsearch.xpack.core.slm:TRACE,org.elasticsearch.snapshots:DEBUG", - issueUrl = "https://github.com/elastic/elasticsearch/issues/48531") + @TestIssueLogging( + value = "org.elasticsearch.xpack.slm:TRACE,org.elasticsearch.xpack.core.slm:TRACE,org.elasticsearch.snapshots:DEBUG", + issueUrl = "https://github.com/elastic/elasticsearch/issues/48531" + ) public void testPolicyManualExecution() throws Exception { final String indexName = "test"; final String policyName = "manual-policy"; @@ -238,10 +246,14 @@ public void testPolicyManualExecution() throws Exception { createSnapshotPolicy(policyName, "snap", NEVER_EXECUTE_CRON_SCHEDULE, repoId, indexName, true); - ResponseException badResp = expectThrows(ResponseException.class, - () -> client().performRequest(new Request("POST", "/_slm/policy/" + policyName + "-bad/_execute"))); - assertThat(EntityUtils.toString(badResp.getResponse().getEntity()), - containsString("no such snapshot lifecycle policy [" + policyName + "-bad]")); + ResponseException badResp = expectThrows( + ResponseException.class, + () -> client().performRequest(new Request("POST", "/_slm/policy/" + policyName + "-bad/_execute")) + ); + assertThat( + EntityUtils.toString(badResp.getResponse().getEntity()), + containsString("no such snapshot lifecycle policy [" + policyName + "-bad]") + ); final String snapshotName = executePolicy(policyName); @@ -274,7 +286,6 @@ public void testPolicyManualExecution() throws Exception { }); } - @SuppressWarnings("unchecked") public void testStartStopStatus() throws Exception { final String indexName = "test"; @@ -293,13 +304,22 @@ public void testStartStopStatus() throws Exception { assertBusy(() -> { logger.info("--> waiting for SLM to stop"); - assertThat(EntityUtils.toString(client().performRequest(new Request("GET", "/_slm/status")).getEntity()), - containsString("STOPPED")); + assertThat( + EntityUtils.toString(client().performRequest(new Request("GET", "/_slm/status")).getEntity()), + containsString("STOPPED") + ); }); try { - createSnapshotPolicy(policyName, "snap", "0 0/15 * * * ?", repoId, indexName, true, - new SnapshotRetentionConfiguration(TimeValue.ZERO, null, null)); + createSnapshotPolicy( + policyName, + "snap", + "0 0/15 * * * ?", + repoId, + indexName, + true, + new SnapshotRetentionConfiguration(TimeValue.ZERO, null, null) + ); long start = System.currentTimeMillis(); final String snapshotName = executePolicy(policyName); @@ -360,15 +380,19 @@ public void testStartStopStatus() throws Exception { assertBusy(() -> { logger.info("--> waiting for SLM to start"); - assertThat(EntityUtils.toString(client().performRequest(new Request("GET", "/_slm/status")).getEntity()), - containsString("RUNNING")); + assertThat( + EntityUtils.toString(client().performRequest(new Request("GET", "/_slm/status")).getEntity()), + containsString("RUNNING") + ); }); } } @SuppressWarnings("unchecked") - @TestIssueLogging(value = "org.elasticsearch.xpack.slm:TRACE,org.elasticsearch.xpack.core.slm:TRACE,org.elasticsearch.snapshots:TRACE", - issueUrl = "https://github.com/elastic/elasticsearch/issues/48017") + @TestIssueLogging( + value = "org.elasticsearch.xpack.slm:TRACE,org.elasticsearch.xpack.core.slm:TRACE,org.elasticsearch.snapshots:TRACE", + issueUrl = "https://github.com/elastic/elasticsearch/issues/48017" + ) public void testBasicTimeBasedRetention() throws Exception { final String indexName = "test"; final String policyName = "basic-time-policy"; @@ -384,8 +408,15 @@ public void testBasicTimeBasedRetention() throws Exception { initializeRepo(repoId); // Create a policy with a retention period of 1 millisecond - createSnapshotPolicy(policyName, "snap", NEVER_EXECUTE_CRON_SCHEDULE, repoId, indexName, true, - new SnapshotRetentionConfiguration(TimeValue.timeValueMillis(1), null, null)); + createSnapshotPolicy( + policyName, + "snap", + NEVER_EXECUTE_CRON_SCHEDULE, + repoId, + indexName, + true, + new SnapshotRetentionConfiguration(TimeValue.timeValueMillis(1), null, null) + ); // Manually create a snapshot final String snapshotName = executePolicy(policyName); @@ -467,13 +498,13 @@ public void testDataStreams() throws Exception { String repoId = "ds-repo"; String policyName = "ds-policy"; - String mapping = "{\n" + - " \"properties\": {\n" + - " \"@timestamp\": {\n" + - " \"type\": \"date\"\n" + - " }\n" + - " }\n" + - " }"; + String mapping = "{\n" + + " \"properties\": {\n" + + " \"@timestamp\": {\n" + + " \"type\": \"date\"\n" + + " }\n" + + " }\n" + + " }"; Template template = new Template(null, new CompressedXContent(mapping), null); createComposableTemplate(client(), "ds-template", dataStreamName, template); @@ -525,8 +556,15 @@ public void testSLMXpackUsage() throws Exception { // Create a snapshot repo initializeRepo("repo"); // Create a policy with a retention period of 1 millisecond - createSnapshotPolicy("policy", "snap", NEVER_EXECUTE_CRON_SCHEDULE, "repo", "*", true, - new SnapshotRetentionConfiguration(TimeValue.timeValueMillis(1), null, null)); + createSnapshotPolicy( + "policy", + "snap", + NEVER_EXECUTE_CRON_SCHEDULE, + "repo", + "*", + true, + new SnapshotRetentionConfiguration(TimeValue.timeValueMillis(1), null, null) + ); final String snapshotName = executePolicy("policy"); // Check that the executed snapshot is created @@ -564,8 +602,13 @@ public void testSLMXpackUsage() throws Exception { public Map getLocation(String path) { try { Response executeRepsonse = client().performRequest(new Request("GET", path)); - try (XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, EntityUtils.toByteArray(executeRepsonse.getEntity()))) { + try ( + XContentParser parser = JsonXContent.jsonXContent.createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + EntityUtils.toByteArray(executeRepsonse.getEntity()) + ) + ) { return parser.map(); } } catch (Exception e) { @@ -580,8 +623,13 @@ public Map getLocation(String path) { private String executePolicy(String policyId) { try { Response executeRepsonse = client().performRequest(new Request("POST", "/_slm/policy/" + policyId + "/_execute")); - try (XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, EntityUtils.toByteArray(executeRepsonse.getEntity()))) { + try ( + XContentParser parser = JsonXContent.jsonXContent.createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + EntityUtils.toByteArray(executeRepsonse.getEntity()) + ) + ) { return parser.mapStrings().get("snapshot_name"); } } catch (Exception e) { @@ -620,34 +668,44 @@ private Map getSLMStats() { @SuppressWarnings("unchecked") private void assertHistoryIsPresent(String policyName, boolean success, String repository, String operation) throws IOException { final Request historySearchRequest = new Request("GET", ".slm-history*/_search"); - historySearchRequest.setJsonEntity("{\n" + - " \"query\": {\n" + - " \"bool\": {\n" + - " \"must\": [\n" + - " {\n" + - " \"term\": {\n" + - " \"policy\": \"" + policyName + "\"\n" + - " }\n" + - " },\n" + - " {\n" + - " \"term\": {\n" + - " \"success\": " + success + "\n" + - " }\n" + - " },\n" + - " {\n" + - " \"term\": {\n" + - " \"repository\": \"" + repository + "\"\n" + - " }\n" + - " },\n" + - " {\n" + - " \"term\": {\n" + - " \"operation\": \"" + operation + "\"\n" + - " }\n" + - " }\n" + - " ]\n" + - " }\n" + - " }\n" + - "}"); + historySearchRequest.setJsonEntity( + "{\n" + + " \"query\": {\n" + + " \"bool\": {\n" + + " \"must\": [\n" + + " {\n" + + " \"term\": {\n" + + " \"policy\": \"" + + policyName + + "\"\n" + + " }\n" + + " },\n" + + " {\n" + + " \"term\": {\n" + + " \"success\": " + + success + + "\n" + + " }\n" + + " },\n" + + " {\n" + + " \"term\": {\n" + + " \"repository\": \"" + + repository + + "\"\n" + + " }\n" + + " },\n" + + " {\n" + + " \"term\": {\n" + + " \"operation\": \"" + + operation + + "\"\n" + + " }\n" + + " }\n" + + " ]\n" + + " }\n" + + " }\n" + + "}" + ); Response historyResponse; try { historyResponse = client().performRequest(historySearchRequest); @@ -655,8 +713,10 @@ private void assertHistoryIsPresent(String policyName, boolean success, String r try (InputStream is = historyResponse.getEntity().getContent()) { historyResponseMap = XContentHelper.convertToMap(XContentType.JSON.xContent(), is, true); } - assertThat((int)((Map) ((Map) historyResponseMap.get("hits")).get("total")).get("value"), - greaterThanOrEqualTo(1)); + assertThat( + (int) ((Map) ((Map) historyResponseMap.get("hits")).get("total")).get("value"), + greaterThanOrEqualTo(1) + ); } catch (ResponseException e) { // Throw AssertionError instead of an exception if the search fails so that assertBusy works as expected logger.error(e); @@ -674,28 +734,55 @@ private void assertHistoryIndexWaitingForRollover() throws IOException { assertEquals(WaitForRolloverReadyStep.NAME, stepKey.getName()); } - private void createSnapshotPolicy(String policyName, String snapshotNamePattern, String schedule, String repoId, - String indexPattern, boolean ignoreUnavailable) throws IOException { - createSnapshotPolicy(policyName, snapshotNamePattern, schedule, repoId, indexPattern, - ignoreUnavailable, SnapshotRetentionConfiguration.EMPTY); + private void createSnapshotPolicy( + String policyName, + String snapshotNamePattern, + String schedule, + String repoId, + String indexPattern, + boolean ignoreUnavailable + ) throws IOException { + createSnapshotPolicy( + policyName, + snapshotNamePattern, + schedule, + repoId, + indexPattern, + ignoreUnavailable, + SnapshotRetentionConfiguration.EMPTY + ); } - private void createSnapshotPolicy(String policyName, String snapshotNamePattern, String schedule, String repoId, - String indexPattern, boolean ignoreUnavailable, - SnapshotRetentionConfiguration retention) throws IOException { + private void createSnapshotPolicy( + String policyName, + String snapshotNamePattern, + String schedule, + String repoId, + String indexPattern, + boolean ignoreUnavailable, + SnapshotRetentionConfiguration retention + ) throws IOException { Map snapConfig = new HashMap<>(); snapConfig.put("indices", Collections.singletonList(indexPattern)); snapConfig.put("ignore_unavailable", ignoreUnavailable); if (randomBoolean()) { Map metadata = new HashMap<>(); - int fieldCount = randomIntBetween(2,5); + int fieldCount = randomIntBetween(2, 5); for (int i = 0; i < fieldCount; i++) { - metadata.put(randomValueOtherThanMany(key -> "policy".equals(key) || metadata.containsKey(key), - () -> randomAlphaOfLength(5)), randomAlphaOfLength(4)); + metadata.put( + randomValueOtherThanMany(key -> "policy".equals(key) || metadata.containsKey(key), () -> randomAlphaOfLength(5)), + randomAlphaOfLength(4) + ); } } - SnapshotLifecyclePolicy policy = new SnapshotLifecyclePolicy(policyName, snapshotNamePattern, schedule, - repoId, snapConfig, retention); + SnapshotLifecyclePolicy policy = new SnapshotLifecyclePolicy( + policyName, + snapshotNamePattern, + schedule, + repoId, + snapConfig, + retention + ); Request putLifecycle = new Request("PUT", "/_slm/policy/" + policyName); XContentBuilder lifecycleBuilder = JsonXContent.contentBuilder(); @@ -723,16 +810,19 @@ private void initializeRepo(String repoName) throws IOException { private void initializeRepo(String repoName, String maxBytesPerSecond) throws IOException { Request request = new Request("PUT", "/_snapshot/" + repoName); - request.setJsonEntity(Strings - .toString(JsonXContent.contentBuilder() - .startObject() - .field("type", "fs") - .startObject("settings") - .field("compress", randomBoolean()) - .field("location", System.getProperty("tests.path.repo")) - .field("max_snapshot_bytes_per_sec", maxBytesPerSecond) - .endObject() - .endObject())); + request.setJsonEntity( + Strings.toString( + JsonXContent.contentBuilder() + .startObject() + .field("type", "fs") + .startObject("settings") + .field("compress", randomBoolean()) + .field("location", System.getProperty("tests.path.repo")) + .field("max_snapshot_bytes_per_sec", maxBytesPerSecond) + .endObject() + .endObject() + ) + ); assertOK(client().performRequest(request)); } @@ -749,11 +839,13 @@ private static void index(RestClient client, String index, String id, Object... @SuppressWarnings("unchecked") private static Map policyStatsAsMap(Map stats) { - return ((List>) stats.get(SnapshotLifecycleStats.POLICY_STATS.getPreferredName())) - .stream() - .collect(Collectors.toMap( - m -> (String) m.get(SnapshotLifecycleStats.SnapshotPolicyStats.POLICY_ID.getPreferredName()), - Function.identity())); + return ((List>) stats.get(SnapshotLifecycleStats.POLICY_STATS.getPreferredName())).stream() + .collect( + Collectors.toMap( + m -> (String) m.get(SnapshotLifecycleStats.SnapshotPolicyStats.POLICY_ID.getPreferredName()), + Function.identity() + ) + ); } private void assertAcked(Response response) throws IOException { @@ -766,7 +858,7 @@ private void assertAcked(Response response) throws IOException { } private void logSLMPolicies() throws IOException { - Request request = new Request("GET" , "/_slm/policy?human"); + Request request = new Request("GET", "/_slm/policy?human"); Response response = client().performRequest(request); assertOK(response); logger.info("SLM policies: {}", EntityUtils.toString(response.getEntity())); diff --git a/x-pack/plugin/ilm/qa/rest/src/yamlRestTest/java/org/elasticsearch/xpack/ilm/IndexLifecycleRestIT.java b/x-pack/plugin/ilm/qa/rest/src/yamlRestTest/java/org/elasticsearch/xpack/ilm/IndexLifecycleRestIT.java index b2fe4825b7baa..6cfa0d72e6307 100644 --- a/x-pack/plugin/ilm/qa/rest/src/yamlRestTest/java/org/elasticsearch/xpack/ilm/IndexLifecycleRestIT.java +++ b/x-pack/plugin/ilm/qa/rest/src/yamlRestTest/java/org/elasticsearch/xpack/ilm/IndexLifecycleRestIT.java @@ -10,6 +10,7 @@ import com.carrotsearch.randomizedtesting.annotations.Name; import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; import com.carrotsearch.randomizedtesting.annotations.TimeoutSuite; + import org.apache.lucene.util.TimeUnits; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; @@ -39,10 +40,6 @@ public static Iterable parameters() throws Exception { @Override protected Settings restClientSettings() { String token = basicAuthHeaderValue(USER, new SecureString(PASS.toCharArray())); - return Settings.builder() - .put(super.restClientSettings()) - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(super.restClientSettings()).put(ThreadContext.PREFIX + ".Authorization", token).build(); } } - diff --git a/x-pack/plugin/ilm/qa/with-security/src/javaRestTest/java/org/elasticsearch/xpack/security/PermissionsIT.java b/x-pack/plugin/ilm/qa/with-security/src/javaRestTest/java/org/elasticsearch/xpack/security/PermissionsIT.java index c3f5c5c910d33..d976b045bb748 100644 --- a/x-pack/plugin/ilm/qa/with-security/src/javaRestTest/java/org/elasticsearch/xpack/security/PermissionsIT.java +++ b/x-pack/plugin/ilm/qa/with-security/src/javaRestTest/java/org/elasticsearch/xpack/security/PermissionsIT.java @@ -34,18 +34,18 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.common.xcontent.support.XContentMapValues; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.repositories.fs.FsRepository; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.admin.indices.RestPutIndexTemplateAction; import org.elasticsearch.snapshots.SnapshotState; import org.elasticsearch.test.rest.ESRestTestCase; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.ilm.DeleteAction; import org.elasticsearch.xpack.core.ilm.LifecycleAction; import org.elasticsearch.xpack.core.ilm.LifecyclePolicy; @@ -78,17 +78,13 @@ public class PermissionsIT extends ESRestTestCase { @Override protected Settings restClientSettings() { String token = basicAuthHeaderValue("test_ilm", new SecureString("x-pack-test-password".toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } @Override protected Settings restAdminSettings() { String token = basicAuthHeaderValue("test_admin", new SecureString("x-pack-test-password".toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } @Before @@ -107,7 +103,7 @@ public void init() throws Exception { .put("number_of_shards", 1) .put("number_of_replicas", 0) .build(); - createNewSingletonPolicy(client(), deletePolicy,"delete", new DeleteAction()); + createNewSingletonPolicy(client(), deletePolicy, "delete", new DeleteAction()); } /** @@ -153,11 +149,16 @@ public void testCanManageIndexWithNoPermissions() throws Exception { assertThat(indexExplain.get("failed_step"), equalTo("wait-for-shard-history-leases")); Map stepInfo = (Map) indexExplain.get("step_info"); assertThat(stepInfo.get("type"), equalTo("security_exception")); - assertThat(stepInfo.get("reason"), equalTo("action [indices:monitor/stats] is unauthorized" + - " for user [test_ilm]" + - " with roles [ilm]" + - " on indices [not-ilm]," + - " this action is granted by the index privileges [monitor,manage,all]")); + assertThat( + stepInfo.get("reason"), + equalTo( + "action [indices:monitor/stats] is unauthorized" + + " for user [test_ilm]" + + " with roles [ilm]" + + " on indices [not-ilm]," + + " this action is granted by the index privileges [monitor,manage,all]" + ) + ); } } }, 30, TimeUnit.SECONDS); @@ -172,8 +173,10 @@ public void testSLMWithPermissions() throws Exception { roleRequest.setJsonEntity("{ \"cluster\": [\"read_slm\"] }"); assertOK(adminClient().performRequest(roleRequest)); roleRequest = new Request("PUT", "/_security/role/slm-manage"); - roleRequest.setJsonEntity("{ \"cluster\": [\"manage_slm\", \"cluster:admin/repository/*\", \"cluster:admin/snapshot/*\"]," + - "\"indices\": [{ \"names\": [\".slm-history*\"],\"privileges\": [\"all\"] }] }"); + roleRequest.setJsonEntity( + "{ \"cluster\": [\"manage_slm\", \"cluster:admin/repository/*\", \"cluster:admin/snapshot/*\"]," + + "\"indices\": [{ \"names\": [\".slm-history*\"],\"privileges\": [\"all\"] }] }" + ); assertOK(adminClient().performRequest(roleRequest)); createUser("slm_admin", "slm-admin-password", "slm-manage"); @@ -184,17 +187,13 @@ public void testSLMWithPermissions() throws Exception { // Build two high level clients, each using a different user final RestClientBuilder adminBuilder = RestClient.builder(adminClient().getNodes().toArray(new Node[0])); final String adminToken = basicAuthHeaderValue("slm_admin", new SecureString("slm-admin-password".toCharArray())); - configureClient(adminBuilder, Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", adminToken) - .build()); + configureClient(adminBuilder, Settings.builder().put(ThreadContext.PREFIX + ".Authorization", adminToken).build()); adminBuilder.setStrictDeprecationMode(true); final RestHighLevelClient adminHLRC = new RestHighLevelClient(adminBuilder); final RestClientBuilder userBuilder = RestClient.builder(adminClient().getNodes().toArray(new Node[0])); final String userToken = basicAuthHeaderValue("slm_user", new SecureString("slm-user-password".toCharArray())); - configureClient(userBuilder, Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", userToken) - .build()); + configureClient(userBuilder, Settings.builder().put(ThreadContext.PREFIX + ".Authorization", userToken).build()); userBuilder.setStrictDeprecationMode(true); final RestHighLevelClient readHlrc = new RestHighLevelClient(userBuilder); @@ -204,19 +203,26 @@ public void testSLMWithPermissions() throws Exception { repoRequest.settings(settingsBuilder); repoRequest.name(repo); repoRequest.type(FsRepository.TYPE); - org.elasticsearch.action.support.master.AcknowledgedResponse response = - hlAdminClient.snapshot().createRepository(repoRequest, RequestOptions.DEFAULT); + org.elasticsearch.action.support.master.AcknowledgedResponse response = hlAdminClient.snapshot() + .createRepository(repoRequest, RequestOptions.DEFAULT); assertTrue(response.isAcknowledged()); Map config = new HashMap<>(); config.put("indices", Collections.singletonList("index")); SnapshotLifecyclePolicy policy = new SnapshotLifecyclePolicy( - "policy_id", "name", "1 2 3 * * ?", repo, config, - new SnapshotRetentionConfiguration(TimeValue.ZERO, null, null)); + "policy_id", + "name", + "1 2 3 * * ?", + repo, + config, + new SnapshotRetentionConfiguration(TimeValue.ZERO, null, null) + ); PutSnapshotLifecyclePolicyRequest request = new PutSnapshotLifecyclePolicyRequest(policy); - expectThrows(ElasticsearchStatusException.class, - () -> readHlrc.indexLifecycle().putSnapshotLifecyclePolicy(request, RequestOptions.DEFAULT)); + expectThrows( + ElasticsearchStatusException.class, + () -> readHlrc.indexLifecycle().putSnapshotLifecyclePolicy(request, RequestOptions.DEFAULT) + ); adminHLRC.indexLifecycle().putSnapshotLifecyclePolicy(request, RequestOptions.DEFAULT); @@ -225,18 +231,20 @@ public void testSLMWithPermissions() throws Exception { adminHLRC.indexLifecycle().getSnapshotLifecyclePolicy(getRequest, RequestOptions.DEFAULT); ExecuteSnapshotLifecyclePolicyRequest executeRequest = new ExecuteSnapshotLifecyclePolicyRequest("policy_id"); - expectThrows(ElasticsearchStatusException.class, () -> - readHlrc.indexLifecycle().executeSnapshotLifecyclePolicy(executeRequest, RequestOptions.DEFAULT)); + expectThrows( + ElasticsearchStatusException.class, + () -> readHlrc.indexLifecycle().executeSnapshotLifecyclePolicy(executeRequest, RequestOptions.DEFAULT) + ); - ExecuteSnapshotLifecyclePolicyResponse executeResp = - adminHLRC.indexLifecycle().executeSnapshotLifecyclePolicy(executeRequest, RequestOptions.DEFAULT); + ExecuteSnapshotLifecyclePolicyResponse executeResp = adminHLRC.indexLifecycle() + .executeSnapshotLifecyclePolicy(executeRequest, RequestOptions.DEFAULT); final String snapName = executeResp.getSnapshotName(); assertBusy(() -> { try { logger.info("--> checking for snapshot to be created"); GetSnapshotsRequest getSnaps = new GetSnapshotsRequest(repo); - getSnaps.snapshots(new String[]{snapName}); + getSnaps.snapshots(new String[] { snapName }); GetSnapshotsResponse getResp = adminHLRC.snapshot().get(getSnaps, RequestOptions.DEFAULT); assertThat(getResp.getSnapshots().get(0).state(), equalTo(SnapshotState.SUCCESS)); } catch (ElasticsearchException e) { @@ -245,18 +253,20 @@ public void testSLMWithPermissions() throws Exception { }); ExecuteSnapshotLifecycleRetentionRequest executeRetention = new ExecuteSnapshotLifecycleRetentionRequest(); - expectThrows(ElasticsearchStatusException.class, () -> - readHlrc.indexLifecycle().executeSnapshotLifecycleRetention(executeRetention, RequestOptions.DEFAULT)); + expectThrows( + ElasticsearchStatusException.class, + () -> readHlrc.indexLifecycle().executeSnapshotLifecycleRetention(executeRetention, RequestOptions.DEFAULT) + ); - AcknowledgedResponse retentionResp = - adminHLRC.indexLifecycle().executeSnapshotLifecycleRetention(executeRetention, RequestOptions.DEFAULT); + AcknowledgedResponse retentionResp = adminHLRC.indexLifecycle() + .executeSnapshotLifecycleRetention(executeRetention, RequestOptions.DEFAULT); assertTrue(retentionResp.isAcknowledged()); assertBusy(() -> { try { logger.info("--> checking for snapshot to be deleted"); GetSnapshotsRequest getSnaps = new GetSnapshotsRequest(repo); - getSnaps.snapshots(new String[]{snapName}); + getSnaps.snapshots(new String[] { snapName }); GetSnapshotsResponse getResp = adminHLRC.snapshot().get(getSnaps, RequestOptions.DEFAULT); assertThat(getResp.getSnapshots().size(), equalTo(0)); } catch (ElasticsearchException e) { @@ -266,8 +276,10 @@ public void testSLMWithPermissions() throws Exception { }); DeleteSnapshotLifecyclePolicyRequest deleteRequest = new DeleteSnapshotLifecyclePolicyRequest("policy_id"); - expectThrows(ElasticsearchStatusException.class, () -> - readHlrc.indexLifecycle().deleteSnapshotLifecyclePolicy(deleteRequest, RequestOptions.DEFAULT)); + expectThrows( + ElasticsearchStatusException.class, + () -> readHlrc.indexLifecycle().deleteSnapshotLifecyclePolicy(deleteRequest, RequestOptions.DEFAULT) + ); adminHLRC.indexLifecycle().deleteSnapshotLifecyclePolicy(deleteRequest, RequestOptions.DEFAULT); @@ -333,8 +345,7 @@ private void createNewSingletonPolicy(RestClient client, String policy, String p LifecyclePolicy lifecyclePolicy = new LifecyclePolicy(policy, singletonMap(phase.getName(), phase)); XContentBuilder builder = jsonBuilder(); lifecyclePolicy.toXContent(builder, null); - final StringEntity entity = new StringEntity( - "{ \"policy\":" + Strings.toString(builder) + "}", ContentType.APPLICATION_JSON); + final StringEntity entity = new StringEntity("{ \"policy\":" + Strings.toString(builder) + "}", ContentType.APPLICATION_JSON); Request request = new Request("PUT", "_ilm/policy/" + policy); request.setEntity(entity); assertOK(client.performRequest(request)); @@ -342,51 +353,55 @@ private void createNewSingletonPolicy(RestClient client, String policy, String p private void createIndexAsAdmin(String name, Settings settings, String mapping) throws IOException { Request request = new Request("PUT", "/" + name); - request.setJsonEntity("{\n \"settings\": " + Strings.toString(settings) - + ", \"mappings\" : {" + mapping + "} }"); + request.setJsonEntity("{\n \"settings\": " + Strings.toString(settings) + ", \"mappings\" : {" + mapping + "} }"); assertOK(adminClient().performRequest(request)); } private void createIndexAsAdmin(String name, String alias, boolean isWriteIndex) throws IOException { Request request = new Request("PUT", "/" + name); - request.setJsonEntity("{ \"aliases\": { \""+alias+"\": {" + ((isWriteIndex) ? "\"is_write_index\" : true" : "") - + "} } }"); + request.setJsonEntity("{ \"aliases\": { \"" + alias + "\": {" + ((isWriteIndex) ? "\"is_write_index\" : true" : "") + "} } }"); assertOK(adminClient().performRequest(request)); } private void createIndexTemplate(String name, String pattern, String alias, String policy) throws IOException { Request request = new Request("PUT", "/_template/" + name); - request.setJsonEntity("{\n" + - " \"index_patterns\": [\""+pattern+"\"],\n" + - " \"settings\": {\n" + - " \"number_of_shards\": 1,\n" + - " \"number_of_replicas\": 0,\n" + - " \"index.lifecycle.name\": \""+policy+"\",\n" + - " \"index.lifecycle.rollover_alias\": \""+alias+"\"\n" + - " }\n" + - " }"); + request.setJsonEntity( + "{\n" + + " \"index_patterns\": [\"" + + pattern + + "\"],\n" + + " \"settings\": {\n" + + " \"number_of_shards\": 1,\n" + + " \"number_of_replicas\": 0,\n" + + " \"index.lifecycle.name\": \"" + + policy + + "\",\n" + + " \"index.lifecycle.rollover_alias\": \"" + + alias + + "\"\n" + + " }\n" + + " }" + ); request.setOptions(expectWarnings(RestPutIndexTemplateAction.DEPRECATION_WARNING)); assertOK(adminClient().performRequest(request)); } private void createUser(String name, String password, String role) throws IOException { Request request = new Request("PUT", "/_security/user/" + name); - request.setJsonEntity("{ \"password\": \""+password+"\", \"roles\": [ \""+ role+"\"] }"); + request.setJsonEntity("{ \"password\": \"" + password + "\", \"roles\": [ \"" + role + "\"] }"); assertOK(adminClient().performRequest(request)); } private void createRole(String name, String alias) throws IOException { Request request = new Request("PUT", "/_security/role/" + name); - request.setJsonEntity("{ \"indices\": [ { \"names\" : [ \""+ alias+"\"], \"privileges\": [ \"write\", \"manage\" ] } ] }"); + request.setJsonEntity("{ \"indices\": [ { \"names\" : [ \"" + alias + "\"], \"privileges\": [ \"write\", \"manage\" ] } ] }"); assertOK(adminClient().performRequest(request)); } private void indexDocs(String user, String passwd, String index, int noOfDocs) throws IOException { RestClientBuilder builder = RestClient.builder(adminClient().getNodes().toArray(new Node[0])); String token = basicAuthHeaderValue(user, new SecureString(passwd.toCharArray())); - configureClient(builder, Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build()); + configureClient(builder, Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build()); builder.setStrictDeprecationMode(true); try (RestClient userClient = builder.build();) { diff --git a/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/ClusterStateWaitThresholdBreachTests.java b/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/ClusterStateWaitThresholdBreachTests.java index 0b297a283c727..c73a1eed24a78 100644 --- a/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/ClusterStateWaitThresholdBreachTests.java +++ b/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/ClusterStateWaitThresholdBreachTests.java @@ -90,16 +90,22 @@ public void testWaitInShrunkShardsAllocatedExceedsThreshold() throws Exception { internalCluster().startDataOnlyNode(); int numShards = 2; - Phase warmPhase = new Phase("warm", TimeValue.ZERO, Map.of(MigrateAction.NAME, new MigrateAction(false), ShrinkAction.NAME, - new ShrinkAction(1, null))); + Phase warmPhase = new Phase( + "warm", + TimeValue.ZERO, + Map.of(MigrateAction.NAME, new MigrateAction(false), ShrinkAction.NAME, new ShrinkAction(1, null)) + ); LifecyclePolicy lifecyclePolicy = new LifecyclePolicy(policy, Map.of("warm", warmPhase)); PutLifecycleAction.Request putLifecycleRequest = new PutLifecycleAction.Request(lifecyclePolicy); assertAcked(client().execute(PutLifecycleAction.INSTANCE, putLifecycleRequest).get()); // we're configuring a very high number of replicas. this will make ths shrunk index unable to allocate successfully, so ILM will // wait in the `shrunk-shards-allocated` step (we don't wait for the original index to be GREEN before) - Settings settings = Settings.builder().put(indexSettings()).put(SETTING_NUMBER_OF_SHARDS, numShards) - .put(SETTING_NUMBER_OF_REPLICAS, 42).put(LifecycleSettings.LIFECYCLE_NAME, policy) + Settings settings = Settings.builder() + .put(indexSettings()) + .put(SETTING_NUMBER_OF_SHARDS, numShards) + .put(SETTING_NUMBER_OF_REPLICAS, 42) + .put(LifecycleSettings.LIFECYCLE_NAME, policy) // configuring the threshold to the minimum value .put(LifecycleSettings.LIFECYCLE_STEP_WAIT_TIME_THRESHOLD, "1h") .build(); @@ -109,8 +115,7 @@ public void testWaitInShrunkShardsAllocatedExceedsThreshold() throws Exception { String[] firstAttemptShrinkIndexName = new String[1]; assertBusy(() -> { ExplainLifecycleRequest explainRequest = new ExplainLifecycleRequest().indices(managedIndex); - ExplainLifecycleResponse explainResponse = client().execute(ExplainLifecycleAction.INSTANCE, - explainRequest).get(); + ExplainLifecycleResponse explainResponse = client().execute(ExplainLifecycleAction.INSTANCE, explainRequest).get(); IndexLifecycleExplainResponse indexLifecycleExplainResponse = explainResponse.getIndexResponses().get(managedIndex); firstAttemptShrinkIndexName[0] = indexLifecycleExplainResponse.getShrinkIndexName(); @@ -120,8 +125,7 @@ public void testWaitInShrunkShardsAllocatedExceedsThreshold() throws Exception { // let's check ILM for the managed index is waiting in the `shrunk-shards-allocated` step assertBusy(() -> { ExplainLifecycleRequest explainRequest = new ExplainLifecycleRequest().indices(managedIndex); - ExplainLifecycleResponse explainResponse = client().execute(ExplainLifecycleAction.INSTANCE, - explainRequest).get(); + ExplainLifecycleResponse explainResponse = client().execute(ExplainLifecycleAction.INSTANCE, explainRequest).get(); IndexLifecycleExplainResponse indexLifecycleExplainResponse = explainResponse.getIndexResponses().get(managedIndex); assertThat(indexLifecycleExplainResponse.getStep(), is(ShrunkShardsAllocatedStep.NAME)); @@ -140,26 +144,30 @@ public void testWaitInShrunkShardsAllocatedExceedsThreshold() throws Exception { // an old timestamp so the `1h` wait threshold we configured using LIFECYCLE_STEP_WAIT_TIME_THRESHOLD is breached and a new // shrink cycle is started LongSupplier nowWayBackInThePastSupplier = () -> 1234L; - clusterService.submitStateUpdateTask("testing-move-to-step-to-manipulate-step-time", - new ClusterStateUpdateTask() { - @Override - public ClusterState execute(ClusterState currentState) throws Exception { - return new MoveToNextStepUpdateTask(managedIndexMetadata.getIndex(), policy, currentStepKey, currentStepKey, - nowWayBackInThePastSupplier, indexLifecycleService.getPolicyRegistry(), state -> { - }).execute(currentState); - } - - @Override - public void onFailure(String source, Exception e) { - throw new AssertionError(e); - } - }); + clusterService.submitStateUpdateTask("testing-move-to-step-to-manipulate-step-time", new ClusterStateUpdateTask() { + @Override + public ClusterState execute(ClusterState currentState) throws Exception { + return new MoveToNextStepUpdateTask( + managedIndexMetadata.getIndex(), + policy, + currentStepKey, + currentStepKey, + nowWayBackInThePastSupplier, + indexLifecycleService.getPolicyRegistry(), + state -> {} + ).execute(currentState); + } + + @Override + public void onFailure(String source, Exception e) { + throw new AssertionError(e); + } + }); String[] secondCycleShrinkIndexName = new String[1]; assertBusy(() -> { ExplainLifecycleRequest explainRequest = new ExplainLifecycleRequest().indices(managedIndex); - ExplainLifecycleResponse explainResponse = client().execute(ExplainLifecycleAction.INSTANCE, - explainRequest).get(); + ExplainLifecycleResponse explainResponse = client().execute(ExplainLifecycleAction.INSTANCE, explainRequest).get(); IndexLifecycleExplainResponse indexLifecycleExplainResponse = explainResponse.getIndexResponses().get(managedIndex); secondCycleShrinkIndexName[0] = indexLifecycleExplainResponse.getShrinkIndexName(); @@ -177,14 +185,11 @@ public void onFailure(String source, Exception e) { // waiting for the huge numbers of replicas for the shrunk index to allocate. this will never happen, so let's unblock this // situation and allow for shrink to complete by reducing the number of shards for the shrunk index to 0 Settings.Builder zeroReplicasSetting = Settings.builder().put(INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 0); - assertAcked( - client().admin().indices().prepareUpdateSettings(secondCycleShrinkIndexName[0]).setSettings(zeroReplicasSetting) - ); + assertAcked(client().admin().indices().prepareUpdateSettings(secondCycleShrinkIndexName[0]).setSettings(zeroReplicasSetting)); assertBusy(() -> { ExplainLifecycleRequest explainRequest = new ExplainLifecycleRequest().indices(secondCycleShrinkIndexName[0]); - ExplainLifecycleResponse explainResponse = client().execute(ExplainLifecycleAction.INSTANCE, - explainRequest).get(); + ExplainLifecycleResponse explainResponse = client().execute(ExplainLifecycleAction.INSTANCE, explainRequest).get(); IndexLifecycleExplainResponse indexLifecycleExplainResponse = explainResponse.getIndexResponses() .get(secondCycleShrinkIndexName[0]); assertThat(indexLifecycleExplainResponse.getPhase(), equalTo("warm")); diff --git a/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/DataTiersMigrationsTests.java b/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/DataTiersMigrationsTests.java index 6b39068111972..8d3bb24c7da0b 100644 --- a/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/DataTiersMigrationsTests.java +++ b/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/DataTiersMigrationsTests.java @@ -114,15 +114,18 @@ public void testIndexDataTierMigration() throws Exception { PutLifecycleAction.Request putLifecycleRequest = new PutLifecycleAction.Request(lifecyclePolicy); assertAcked(client().execute(PutLifecycleAction.INSTANCE, putLifecycleRequest).get()); - Settings settings = Settings.builder().put(indexSettings()).put(SETTING_NUMBER_OF_SHARDS, 1) - .put(SETTING_NUMBER_OF_REPLICAS, 1).put(LifecycleSettings.LIFECYCLE_NAME, policy).build(); + Settings settings = Settings.builder() + .put(indexSettings()) + .put(SETTING_NUMBER_OF_SHARDS, 1) + .put(SETTING_NUMBER_OF_REPLICAS, 1) + .put(LifecycleSettings.LIFECYCLE_NAME, policy) + .build(); CreateIndexResponse res = client().admin().indices().prepareCreate(managedIndex).setSettings(settings).get(); assertTrue(res.isAcknowledged()); assertBusy(() -> { ExplainLifecycleRequest explainRequest = new ExplainLifecycleRequest().indices(managedIndex); - ExplainLifecycleResponse explainResponse = client().execute(ExplainLifecycleAction.INSTANCE, - explainRequest).get(); + ExplainLifecycleResponse explainResponse = client().execute(ExplainLifecycleAction.INSTANCE, explainRequest).get(); IndexLifecycleExplainResponse indexLifecycleExplainResponse = explainResponse.getIndexResponses().get(managedIndex); assertThat(indexLifecycleExplainResponse.getPhase(), is("warm")); @@ -133,8 +136,7 @@ public void testIndexDataTierMigration() throws Exception { internalCluster().startNode(warmNode(Settings.EMPTY)); assertBusy(() -> { ExplainLifecycleRequest explainRequest = new ExplainLifecycleRequest().indices(managedIndex); - ExplainLifecycleResponse explainResponse = client().execute(ExplainLifecycleAction.INSTANCE, - explainRequest).get(); + ExplainLifecycleResponse explainResponse = client().execute(ExplainLifecycleAction.INSTANCE, explainRequest).get(); IndexLifecycleExplainResponse indexLifecycleExplainResponse = explainResponse.getIndexResponses().get(managedIndex); assertThat(indexLifecycleExplainResponse.getPhase(), is("cold")); @@ -147,8 +149,7 @@ public void testIndexDataTierMigration() throws Exception { // wait for lifecycle to complete in the cold phase after the index has been migrated to the cold node assertBusy(() -> { ExplainLifecycleRequest explainRequest = new ExplainLifecycleRequest().indices(managedIndex); - ExplainLifecycleResponse explainResponse = client().execute(ExplainLifecycleAction.INSTANCE, - explainRequest).get(); + ExplainLifecycleResponse explainResponse = client().execute(ExplainLifecycleAction.INSTANCE, explainRequest).get(); IndexLifecycleExplainResponse indexLifecycleExplainResponse = explainResponse.getIndexResponses().get(managedIndex); assertThat(indexLifecycleExplainResponse.getPhase(), is("cold")); @@ -174,15 +175,18 @@ public void testUserOptsOutOfTierMigration() throws Exception { PutLifecycleAction.Request putLifecycleRequest = new PutLifecycleAction.Request(lifecyclePolicy); assertAcked(client().execute(PutLifecycleAction.INSTANCE, putLifecycleRequest).get()); - Settings settings = Settings.builder().put(indexSettings()).put(SETTING_NUMBER_OF_SHARDS, 1) - .put(SETTING_NUMBER_OF_REPLICAS, 1).put(LifecycleSettings.LIFECYCLE_NAME, policy).build(); + Settings settings = Settings.builder() + .put(indexSettings()) + .put(SETTING_NUMBER_OF_SHARDS, 1) + .put(SETTING_NUMBER_OF_REPLICAS, 1) + .put(LifecycleSettings.LIFECYCLE_NAME, policy) + .build(); CreateIndexResponse res = client().admin().indices().prepareCreate(managedIndex).setSettings(settings).get(); assertTrue(res.isAcknowledged()); assertBusy(() -> { ExplainLifecycleRequest explainRequest = new ExplainLifecycleRequest().indices(managedIndex); - ExplainLifecycleResponse explainResponse = client().execute(ExplainLifecycleAction.INSTANCE, - explainRequest).get(); + ExplainLifecycleResponse explainResponse = client().execute(ExplainLifecycleAction.INSTANCE, explainRequest).get(); IndexLifecycleExplainResponse indexLifecycleExplainResponse = explainResponse.getIndexResponses().get(managedIndex); assertThat(indexLifecycleExplainResponse.getPhase(), is("warm")); @@ -200,12 +204,11 @@ public void testUserOptsOutOfTierMigration() throws Exception { // the index is successfully allocated but the migrate action from the cold phase re-configured the tier migration setting to the // cold tier so ILM is stuck in `check-migration` in the cold phase this time // we have 2 options to resume the ILM execution: - // 1. start another cold node so both the primary and replica can relocate to the cold nodes - // 2. remove the tier routing setting from the index again (we're doing this below) + // 1. start another cold node so both the primary and replica can relocate to the cold nodes + // 2. remove the tier routing setting from the index again (we're doing this below) assertBusy(() -> { ExplainLifecycleRequest explainRequest = new ExplainLifecycleRequest().indices(managedIndex); - ExplainLifecycleResponse explainResponse = client().execute(ExplainLifecycleAction.INSTANCE, - explainRequest).get(); + ExplainLifecycleResponse explainResponse = client().execute(ExplainLifecycleAction.INSTANCE, explainRequest).get(); IndexLifecycleExplainResponse indexLifecycleExplainResponse = explainResponse.getIndexResponses().get(managedIndex); assertThat(indexLifecycleExplainResponse.getPhase(), is("cold")); @@ -218,8 +221,7 @@ public void testUserOptsOutOfTierMigration() throws Exception { // wait for lifecycle to complete in the cold phase assertBusy(() -> { ExplainLifecycleRequest explainRequest = new ExplainLifecycleRequest().indices(managedIndex); - ExplainLifecycleResponse explainResponse = client().execute(ExplainLifecycleAction.INSTANCE, - explainRequest).get(); + ExplainLifecycleResponse explainResponse = client().execute(ExplainLifecycleAction.INSTANCE, explainRequest).get(); IndexLifecycleExplainResponse indexLifecycleExplainResponse = explainResponse.getIndexResponses().get(managedIndex); assertThat(indexLifecycleExplainResponse.getPhase(), is("cold")); @@ -228,8 +230,9 @@ public void testUserOptsOutOfTierMigration() throws Exception { } private void assertReplicaIsUnassigned() { - ClusterAllocationExplainRequest explainReplicaShard = - new ClusterAllocationExplainRequest().setIndex(managedIndex).setPrimary(false).setShard(0); + ClusterAllocationExplainRequest explainReplicaShard = new ClusterAllocationExplainRequest().setIndex(managedIndex) + .setPrimary(false) + .setShard(0); ClusterAllocationExplainResponse response = client().admin().cluster().allocationExplain(explainReplicaShard).actionGet(); assertThat(response.getExplanation().getShardState(), is(ShardRoutingState.UNASSIGNED)); } diff --git a/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/ILMMultiNodeIT.java b/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/ILMMultiNodeIT.java index 66e6ba3d37f66..8bc121871ba6b 100644 --- a/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/ILMMultiNodeIT.java +++ b/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/ILMMultiNodeIT.java @@ -53,7 +53,8 @@ protected Collection> nodePlugins() { @Override protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { - return Settings.builder().put(super.nodeSettings(nodeOrdinal, otherSettings)) + return Settings.builder() + .put(super.nodeSettings(nodeOrdinal, otherSettings)) .put(LifecycleSettings.LIFECYCLE_POLL_INTERVAL, "1s") // This just generates less churn and makes it easier to read the log file if needed .put(LifecycleSettings.LIFECYCLE_HISTORY_INDEX_ENABLED, false) @@ -63,7 +64,7 @@ protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { @After public void cleanup() { try { - client().execute(DeleteDataStreamAction.INSTANCE, new DeleteDataStreamAction.Request(new String[]{index})).get(); + client().execute(DeleteDataStreamAction.INSTANCE, new DeleteDataStreamAction.Request(new String[] { index })).get(); } catch (Exception e) { // Okay to ignore this logger.info("failed to clean up data stream", e); @@ -85,11 +86,15 @@ public void testShrinkOnTiers() throws Exception { LifecyclePolicy lifecyclePolicy = new LifecyclePolicy("shrink-policy", phases); client().execute(PutLifecycleAction.INSTANCE, new PutLifecycleAction.Request(lifecyclePolicy)).get(); - Template t = new Template(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 2) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put(LifecycleSettings.LIFECYCLE_NAME, "shrink-policy") - .build(), null, null); + Template t = new Template( + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 2) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put(LifecycleSettings.LIFECYCLE_NAME, "shrink-policy") + .build(), + null, + null + ); ComposableIndexTemplate template = new ComposableIndexTemplate( Collections.singletonList(index), @@ -108,15 +113,14 @@ public void testShrinkOnTiers() throws Exception { client().prepareIndex(index).setCreate(true).setId("1").setSource("@timestamp", "2020-09-09").get(); assertBusy(() -> { - ExplainLifecycleResponse explain = - client().execute(ExplainLifecycleAction.INSTANCE, new ExplainLifecycleRequest().indices("*")).get(); + ExplainLifecycleResponse explain = client().execute(ExplainLifecycleAction.INSTANCE, new ExplainLifecycleRequest().indices("*")) + .get(); logger.info("--> explain: {}", Strings.toString(explain)); String backingIndexName = DataStream.getDefaultBackingIndexName(index, 1); IndexLifecycleExplainResponse indexResp = null; for (Map.Entry indexNameAndResp : explain.getIndexResponses().entrySet()) { - if (indexNameAndResp.getKey().startsWith(SHRUNKEN_INDEX_PREFIX) && - indexNameAndResp.getKey().contains(backingIndexName)) { + if (indexNameAndResp.getKey().startsWith(SHRUNKEN_INDEX_PREFIX) && indexNameAndResp.getKey().contains(backingIndexName)) { indexResp = indexNameAndResp.getValue(); assertNotNull(indexResp); assertThat(indexResp.getPhase(), equalTo("warm")); diff --git a/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/IndexLifecycleInitialisationTests.java b/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/IndexLifecycleInitialisationTests.java index 68962b55e3e23..d780da5dfbbf7 100644 --- a/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/IndexLifecycleInitialisationTests.java +++ b/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/IndexLifecycleInitialisationTests.java @@ -9,7 +9,6 @@ import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.routing.RoutingNode; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.NamedWriteable; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.StreamInput; @@ -18,12 +17,13 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.time.DateFormatter; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.index.Index; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.test.ESIntegTestCase; import org.elasticsearch.test.ESIntegTestCase.Scope; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xpack.core.LocalStateCompositeXPackPlugin; import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.xpack.core.ilm.ClusterStateWaitStep; @@ -119,8 +119,12 @@ protected Collection> nodePlugins() { @Before public void init() { - settings = Settings.builder().put(indexSettings()).put(SETTING_NUMBER_OF_SHARDS, 1) - .put(SETTING_NUMBER_OF_REPLICAS, 0).put(LifecycleSettings.LIFECYCLE_NAME, "test").build(); + settings = Settings.builder() + .put(indexSettings()) + .put(SETTING_NUMBER_OF_SHARDS, 1) + .put(SETTING_NUMBER_OF_REPLICAS, 0) + .put(LifecycleSettings.LIFECYCLE_NAME, "test") + .build(); List steps = new ArrayList<>(); Step.StepKey key = new Step.StepKey("mock", ObservableAction.NAME, ObservableClusterStateWaitStep.NAME); Step.StepKey compKey = new Step.StepKey("mock", "complete", "complete"); @@ -140,11 +144,15 @@ public void testSingleNodeCluster() throws Exception { final String node1 = getLocalNodeId(server_1); // test get-lifecycle behavior when IndexLifecycleMetadata is null - GetLifecycleAction.Response getUninitializedLifecycleResponse = client().execute(GetLifecycleAction.INSTANCE, - new GetLifecycleAction.Request()).get(); + GetLifecycleAction.Response getUninitializedLifecycleResponse = client().execute( + GetLifecycleAction.INSTANCE, + new GetLifecycleAction.Request() + ).get(); assertThat(getUninitializedLifecycleResponse.getPolicies().size(), equalTo(0)); - ExecutionException exception = expectThrows(ExecutionException.class,() -> client() - .execute(GetLifecycleAction.INSTANCE, new GetLifecycleAction.Request("non-existent-policy")).get()); + ExecutionException exception = expectThrows( + ExecutionException.class, + () -> client().execute(GetLifecycleAction.INSTANCE, new GetLifecycleAction.Request("non-existent-policy")).get() + ); assertThat(exception.getMessage(), containsString("Lifecycle policy not found: [non-existent-policy]")); logger.info("Creating lifecycle [test_lifecycle]"); @@ -154,32 +162,35 @@ public void testSingleNodeCluster() throws Exception { long upperBoundModifiedDate = Instant.now().toEpochMilli(); // assert version and modified_date - GetLifecycleAction.Response getLifecycleResponse = client().execute(GetLifecycleAction.INSTANCE, - new GetLifecycleAction.Request()).get(); + GetLifecycleAction.Response getLifecycleResponse = client().execute(GetLifecycleAction.INSTANCE, new GetLifecycleAction.Request()) + .get(); assertThat(getLifecycleResponse.getPolicies().size(), equalTo(1)); GetLifecycleAction.LifecyclePolicyResponseItem responseItem = getLifecycleResponse.getPolicies().get(0); assertThat(responseItem.getLifecyclePolicy(), equalTo(lifecyclePolicy)); assertThat(responseItem.getVersion(), equalTo(1L)); long actualModifiedDate = Instant.from(ISO_ZONED_DATE_TIME.parse(responseItem.getModifiedDate())).toEpochMilli(); - assertThat(actualModifiedDate, - is(both(greaterThanOrEqualTo(lowerBoundModifiedDate)).and(lessThanOrEqualTo(upperBoundModifiedDate)))); + assertThat( + actualModifiedDate, + is(both(greaterThanOrEqualTo(lowerBoundModifiedDate)).and(lessThanOrEqualTo(upperBoundModifiedDate))) + ); logger.info("Creating index [test]"); - CreateIndexResponse createIndexResponse = client().admin().indices().create(createIndexRequest("test").settings(settings)) - .actionGet(); + CreateIndexResponse createIndexResponse = client().admin() + .indices() + .create(createIndexRequest("test").settings(settings)) + .actionGet(); assertAcked(createIndexResponse); ClusterState clusterState = client().admin().cluster().prepareState().get().getState(); RoutingNode routingNodeEntry1 = clusterState.getRoutingNodes().node(node1); assertThat(routingNodeEntry1.numberOfShardsWithState(STARTED), equalTo(1)); - assertBusy(() -> { - assertTrue(indexExists("test")); - }); + assertBusy(() -> { assertTrue(indexExists("test")); }); IndexLifecycleService indexLifecycleService = internalCluster().getInstance(IndexLifecycleService.class, server_1); assertThat(indexLifecycleService.getScheduler().jobCount(), equalTo(1)); assertNotNull(indexLifecycleService.getScheduledJob()); assertBusy(() -> { - LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(client().admin().cluster() - .prepareState().execute().actionGet().getState().getMetadata().index("test")); + LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata( + client().admin().cluster().prepareState().execute().actionGet().getState().getMetadata().index("test") + ); assertThat(lifecycleState.getStep(), equalTo("complete")); }); } @@ -192,8 +203,8 @@ public void testExplainExecution() throws Exception { PutLifecycleAction.Request putLifecycleRequest = new PutLifecycleAction.Request(lifecyclePolicy); assertAcked(client().execute(PutLifecycleAction.INSTANCE, putLifecycleRequest).get()); - GetLifecycleAction.Response getLifecycleResponse = client().execute(GetLifecycleAction.INSTANCE, - new GetLifecycleAction.Request()).get(); + GetLifecycleAction.Response getLifecycleResponse = client().execute(GetLifecycleAction.INSTANCE, new GetLifecycleAction.Request()) + .get(); assertThat(getLifecycleResponse.getPolicies().size(), equalTo(1)); GetLifecycleAction.LifecyclePolicyResponseItem responseItem = getLifecycleResponse.getPolicies().get(0); assertThat(responseItem.getLifecyclePolicy(), equalTo(lifecyclePolicy)); @@ -201,7 +212,9 @@ public void testExplainExecution() throws Exception { long actualModifiedDate = Instant.from(ISO_ZONED_DATE_TIME.parse(responseItem.getModifiedDate())).toEpochMilli(); logger.info("Creating index [test]"); - CreateIndexResponse createIndexResponse = client().admin().indices().create(createIndexRequest("test").settings(settings)) + CreateIndexResponse createIndexResponse = client().admin() + .indices() + .create(createIndexRequest("test").settings(settings)) .actionGet(); assertAcked(createIndexResponse); @@ -218,32 +231,43 @@ public void testExplainExecution() throws Exception { } // set the origination date setting to an older value - client().admin().indices().prepareUpdateSettings("test") - .setSettings(Collections.singletonMap(LifecycleSettings.LIFECYCLE_ORIGINATION_DATE, 1000L)).get(); + client().admin() + .indices() + .prepareUpdateSettings("test") + .setSettings(Collections.singletonMap(LifecycleSettings.LIFECYCLE_ORIGINATION_DATE, 1000L)) + .get(); { assertBusy(() -> { IndexLifecycleExplainResponse indexResponse = executeExplainRequestAndGetTestIndexResponse("test"); - assertThat("The configured origination date dictates the lifecycle date", - indexResponse.getLifecycleDate(), equalTo(1000L)); + assertThat("The configured origination date dictates the lifecycle date", indexResponse.getLifecycleDate(), equalTo(1000L)); }); } // set the origination date setting to null - client().admin().indices().prepareUpdateSettings("test") - .setSettings(Collections.singletonMap(LifecycleSettings.LIFECYCLE_ORIGINATION_DATE, null)).get(); + client().admin() + .indices() + .prepareUpdateSettings("test") + .setSettings(Collections.singletonMap(LifecycleSettings.LIFECYCLE_ORIGINATION_DATE, null)) + .get(); { assertBusy(() -> { IndexLifecycleExplainResponse indexResponse = executeExplainRequestAndGetTestIndexResponse("test"); - assertThat("Without the origination date, the index create date should dictate the lifecycle date", - indexResponse.getLifecycleDate(), equalTo(originalLifecycleDate.get())); + assertThat( + "Without the origination date, the index create date should dictate the lifecycle date", + indexResponse.getLifecycleDate(), + equalTo(originalLifecycleDate.get()) + ); }); } // complete the step - client().admin().indices().prepareUpdateSettings("test") - .setSettings(Collections.singletonMap("index.lifecycle.test.complete", true)).get(); + client().admin() + .indices() + .prepareUpdateSettings("test") + .setSettings(Collections.singletonMap("index.lifecycle.test.complete", true)) + .get(); { Phase phase = new Phase("mock", TimeValue.ZERO, Collections.singletonMap("TEST_ACTION", OBSERVABLE_ACTION)); @@ -267,8 +291,8 @@ public void testExplainParseOriginationDate() throws Exception { PutLifecycleAction.Request putLifecycleRequest = new PutLifecycleAction.Request(lifecyclePolicy); assertAcked(client().execute(PutLifecycleAction.INSTANCE, putLifecycleRequest).get()); - GetLifecycleAction.Response getLifecycleResponse = client().execute(GetLifecycleAction.INSTANCE, - new GetLifecycleAction.Request()).get(); + GetLifecycleAction.Response getLifecycleResponse = client().execute(GetLifecycleAction.INSTANCE, new GetLifecycleAction.Request()) + .get(); assertThat(getLifecycleResponse.getPolicies().size(), equalTo(1)); GetLifecycleAction.LifecyclePolicyResponseItem responseItem = getLifecycleResponse.getPolicies().get(0); assertThat(responseItem.getLifecyclePolicy(), equalTo(lifecyclePolicy)); @@ -276,10 +300,14 @@ public void testExplainParseOriginationDate() throws Exception { String indexName = "test-2019.09.14"; logger.info("Creating index [{}]", indexName); - CreateIndexResponse createIndexResponse = - client().admin().indices().create(createIndexRequest(indexName) - .settings(Settings.builder().put(settings).put(LifecycleSettings.LIFECYCLE_PARSE_ORIGINATION_DATE, true)) - ).actionGet(); + CreateIndexResponse createIndexResponse = client().admin() + .indices() + .create( + createIndexRequest(indexName).settings( + Settings.builder().put(settings).put(LifecycleSettings.LIFECYCLE_PARSE_ORIGINATION_DATE, true) + ) + ) + .actionGet(); assertAcked(createIndexResponse); DateFormatter dateFormatter = DateFormatter.forPattern("yyyy.MM.dd"); @@ -290,8 +318,11 @@ public void testExplainParseOriginationDate() throws Exception { }); // disabling the lifecycle parsing would maintain the parsed value as that was set as the origination date - client().admin().indices().prepareUpdateSettings(indexName) - .setSettings(Collections.singletonMap(LifecycleSettings.LIFECYCLE_PARSE_ORIGINATION_DATE, false)).get(); + client().admin() + .indices() + .prepareUpdateSettings(indexName) + .setSettings(Collections.singletonMap(LifecycleSettings.LIFECYCLE_PARSE_ORIGINATION_DATE, false)) + .get(); assertBusy(() -> { IndexLifecycleExplainResponse indexResponse = executeExplainRequestAndGetTestIndexResponse(indexName); @@ -299,8 +330,11 @@ public void testExplainParseOriginationDate() throws Exception { }); // setting the lifecycle origination date setting to null should make the lifecyle date fallback on the index creation date - client().admin().indices().prepareUpdateSettings(indexName) - .setSettings(Collections.singletonMap(LifecycleSettings.LIFECYCLE_ORIGINATION_DATE, null)).get(); + client().admin() + .indices() + .prepareUpdateSettings(indexName) + .setSettings(Collections.singletonMap(LifecycleSettings.LIFECYCLE_ORIGINATION_DATE, null)) + .get(); assertBusy(() -> { IndexLifecycleExplainResponse indexResponse = executeExplainRequestAndGetTestIndexResponse(indexName); @@ -309,12 +343,18 @@ public void testExplainParseOriginationDate() throws Exception { // setting the lifecycle origination date to an explicit value overrides the date parsing long originationDate = 42L; - client().admin().indices().prepareUpdateSettings(indexName) + client().admin() + .indices() + .prepareUpdateSettings(indexName) .setSettings( Map.of( - LifecycleSettings.LIFECYCLE_PARSE_ORIGINATION_DATE, true, - LifecycleSettings.LIFECYCLE_ORIGINATION_DATE, originationDate) - ).get(); + LifecycleSettings.LIFECYCLE_PARSE_ORIGINATION_DATE, + true, + LifecycleSettings.LIFECYCLE_ORIGINATION_DATE, + originationDate + ) + ) + .get(); assertBusy(() -> { IndexLifecycleExplainResponse indexResponse = executeExplainRequestAndGetTestIndexResponse(indexName); @@ -356,8 +396,10 @@ public void testMasterDedicatedDataDedicated() throws Exception { PutLifecycleAction.Request putLifecycleRequest = new PutLifecycleAction.Request(lifecyclePolicy); assertAcked(client().execute(PutLifecycleAction.INSTANCE, putLifecycleRequest).get()); logger.info("Creating index [test]"); - CreateIndexResponse createIndexResponse = client().admin().indices().create(createIndexRequest("test").settings(settings)) - .actionGet(); + CreateIndexResponse createIndexResponse = client().admin() + .indices() + .create(createIndexRequest("test").settings(settings)) + .actionGet(); assertAcked(createIndexResponse); ClusterState clusterState = client().admin().cluster().prepareState().get().getState(); @@ -366,8 +408,9 @@ public void testMasterDedicatedDataDedicated() throws Exception { assertBusy(() -> assertTrue(indexExists("test"))); assertBusy(() -> { - LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(client().admin().cluster() - .prepareState().execute().actionGet().getState().getMetadata().index("test")); + LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata( + client().admin().cluster().prepareState().execute().actionGet().getState().getMetadata().index("test") + ); assertThat(lifecycleState.getStep(), equalTo("complete")); }); } @@ -379,9 +422,12 @@ public void testCreatePolicyWhenStopped() throws Exception { final String node1 = getLocalNodeId(server_1); assertAcked(client().execute(StopILMAction.INSTANCE, new StopILMRequest()).get()); - assertBusy(() -> assertThat( - client().execute(GetStatusAction.INSTANCE, new GetStatusAction.Request()).get().getMode(), - equalTo(OperationMode.STOPPED))); + assertBusy( + () -> assertThat( + client().execute(GetStatusAction.INSTANCE, new GetStatusAction.Request()).get().getMode(), + equalTo(OperationMode.STOPPED) + ) + ); logger.info("Creating lifecycle [test_lifecycle]"); PutLifecycleAction.Request putLifecycleRequest = new PutLifecycleAction.Request(lifecyclePolicy); @@ -390,15 +436,17 @@ public void testCreatePolicyWhenStopped() throws Exception { long upperBoundModifiedDate = Instant.now().toEpochMilli(); // assert version and modified_date - GetLifecycleAction.Response getLifecycleResponse = client().execute(GetLifecycleAction.INSTANCE, - new GetLifecycleAction.Request()).get(); + GetLifecycleAction.Response getLifecycleResponse = client().execute(GetLifecycleAction.INSTANCE, new GetLifecycleAction.Request()) + .get(); assertThat(getLifecycleResponse.getPolicies().size(), equalTo(1)); GetLifecycleAction.LifecyclePolicyResponseItem responseItem = getLifecycleResponse.getPolicies().get(0); assertThat(responseItem.getLifecyclePolicy(), equalTo(lifecyclePolicy)); assertThat(responseItem.getVersion(), equalTo(1L)); long actualModifiedDate = Instant.from(ISO_ZONED_DATE_TIME.parse(responseItem.getModifiedDate())).toEpochMilli(); - assertThat(actualModifiedDate, - is(both(greaterThanOrEqualTo(lowerBoundModifiedDate)).and(lessThanOrEqualTo(upperBoundModifiedDate)))); + assertThat( + actualModifiedDate, + is(both(greaterThanOrEqualTo(lowerBoundModifiedDate)).and(lessThanOrEqualTo(upperBoundModifiedDate))) + ); // assert ILM is still stopped GetStatusAction.Response statusResponse = client().execute(GetStatusAction.INSTANCE, new GetStatusAction.Request()).get(); assertThat(statusResponse.getMode(), equalTo(OperationMode.STOPPED)); @@ -407,7 +455,8 @@ public void testCreatePolicyWhenStopped() throws Exception { public void testPollIntervalUpdate() throws Exception { TimeValue pollInterval = TimeValue.timeValueSeconds(randomLongBetween(1, 5)); final String server_1 = internalCluster().startMasterOnlyNode( - Settings.builder().put(LifecycleSettings.LIFECYCLE_POLL_INTERVAL, pollInterval.getStringRep()).build()); + Settings.builder().put(LifecycleSettings.LIFECYCLE_POLL_INTERVAL, pollInterval.getStringRep()).build() + ); IndexLifecycleService indexLifecycleService = internalCluster().getInstance(IndexLifecycleService.class, server_1); assertBusy(() -> { assertNotNull(indexLifecycleService.getScheduler()); @@ -420,8 +469,9 @@ public void testPollIntervalUpdate() throws Exception { // update the poll interval TimeValue newPollInterval = TimeValue.timeValueHours(randomLongBetween(6, 1000)); - Settings newIntervalSettings = Settings.builder().put(LifecycleSettings.LIFECYCLE_POLL_INTERVAL, - newPollInterval.getStringRep()).build(); + Settings newIntervalSettings = Settings.builder() + .put(LifecycleSettings.LIFECYCLE_POLL_INTERVAL, newPollInterval.getStringRep()) + .build(); assertAcked(client().admin().cluster().prepareUpdateSettings().setPersistentSettings(newIntervalSettings)); { TimeValueSchedule schedule = (TimeValueSchedule) indexLifecycleService.getScheduledJob().getSchedule(); @@ -437,32 +487,37 @@ private String getLocalNodeId(String name) { } public static class TestILMPlugin extends Plugin { - public TestILMPlugin() { - } + public TestILMPlugin() {} public List> getSettings() { - final Setting COMPLETE_SETTING = Setting.boolSetting("index.lifecycle.test.complete", false, - Setting.Property.Dynamic, Setting.Property.IndexScope); + final Setting COMPLETE_SETTING = Setting.boolSetting( + "index.lifecycle.test.complete", + false, + Setting.Property.Dynamic, + Setting.Property.IndexScope + ); return Collections.singletonList(COMPLETE_SETTING); } @Override public List getNamedXContent() { - return Arrays.asList( - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ObservableAction.NAME), (p) -> { - MockAction.parse(p); - return OBSERVABLE_ACTION; - }) - ); + return Arrays.asList(new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ObservableAction.NAME), (p) -> { + MockAction.parse(p); + return OBSERVABLE_ACTION; + })); } @Override public List getNamedWriteables() { - return Arrays.asList(new NamedWriteableRegistry.Entry(LifecycleType.class, LockableLifecycleType.TYPE, - (in) -> LockableLifecycleType.INSTANCE), + return Arrays.asList( + new NamedWriteableRegistry.Entry(LifecycleType.class, LockableLifecycleType.TYPE, (in) -> LockableLifecycleType.INSTANCE), new NamedWriteableRegistry.Entry(LifecycleAction.class, ObservableAction.NAME, ObservableAction::readObservableAction), - new NamedWriteableRegistry.Entry(ObservableClusterStateWaitStep.class, ObservableClusterStateWaitStep.NAME, - ObservableClusterStateWaitStep::new)); + new NamedWriteableRegistry.Entry( + ObservableClusterStateWaitStep.class, + ObservableClusterStateWaitStep.NAME, + ObservableClusterStateWaitStep::new + ) + ); } } @@ -510,8 +565,7 @@ public String getWriteableName() { @Override public Result isConditionMet(Index index, ClusterState clusterState) { - boolean complete = clusterState.metadata().index("test").getSettings() - .getAsBoolean("index.lifecycle.test.complete", false); + boolean complete = clusterState.metadata().index("test").getSettings().getAsBoolean("index.lifecycle.test.complete", false); return new Result(complete, null); } } diff --git a/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/UpdateSettingsStepTests.java b/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/UpdateSettingsStepTests.java index 70c0d322cc1fd..4bd1be084596d 100644 --- a/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/UpdateSettingsStepTests.java +++ b/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/UpdateSettingsStepTests.java @@ -18,7 +18,6 @@ import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Setting.Property; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.env.Environment; import org.elasticsearch.env.NodeEnvironment; import org.elasticsearch.index.IndexModule; @@ -28,6 +27,7 @@ import org.elasticsearch.test.ESSingleNodeTestCase; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.watcher.ResourceWatcherService; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.ilm.Step.StepKey; import org.elasticsearch.xpack.core.ilm.UpdateSettingsStep; import org.junit.After; @@ -59,19 +59,28 @@ public void onIndexModule(IndexModule module) { } @Override - public Collection createComponents(Client client, ClusterService clusterService, ThreadPool threadPool, - ResourceWatcherService resourceWatcherService, ScriptService scriptService, - NamedXContentRegistry xContentRegistry, Environment environment, - NodeEnvironment nodeEnvironment, NamedWriteableRegistry namedWriteableRegistry, - IndexNameExpressionResolver expressionResolver, - Supplier repositoriesServiceSupplier) { + public Collection createComponents( + Client client, + ClusterService clusterService, + ThreadPool threadPool, + ResourceWatcherService resourceWatcherService, + ScriptService scriptService, + NamedXContentRegistry xContentRegistry, + Environment environment, + NodeEnvironment nodeEnvironment, + NamedWriteableRegistry namedWriteableRegistry, + IndexNameExpressionResolver expressionResolver, + Supplier repositoriesServiceSupplier + ) { return List.of(service); } } + public static class SettingsListenerModule extends AbstractModule { private final SettingsTestingService service; + SettingsListenerModule(SettingsTestingService service) { this.service = service; } @@ -82,6 +91,7 @@ protected void configure() { } } + static class SettingsTestingService { public static final String INVALID_VALUE = "INVALID"; @@ -103,6 +113,7 @@ void resetValues() { } } + @After public void resetSettingValue() { service.resetValues(); @@ -114,8 +125,7 @@ protected Collection> getPlugins() { } public void testUpdateSettingsStepRetriesOnError() throws InterruptedException { - assertAcked(client().admin().indices().prepareCreate("test").setSettings(Settings.builder() - .build()).get()); + assertAcked(client().admin().indices().prepareCreate("test").setSettings(Settings.builder().build()).get()); ClusterService clusterService = getInstanceFromNode(ClusterService.class); ClusterState state = clusterService.state(); @@ -128,8 +138,11 @@ public void testUpdateSettingsStepRetriesOnError() throws InterruptedException { // fail the first setting update by using an invalid valid Settings invalidValueSetting = Settings.builder().put("index.test.setting", INVALID_VALUE).build(); UpdateSettingsStep step = new UpdateSettingsStep( - new StepKey("hot", "action", "updateSetting"), new StepKey("hot", "action", "validate"), client(), - invalidValueSetting); + new StepKey("hot", "action", "updateSetting"), + new StepKey("hot", "action", "validate"), + client(), + invalidValueSetting + ); step.performAction(indexMetadata, state, observer, new ActionListener<>() { @Override @@ -145,8 +158,11 @@ public void onFailure(Exception e) { // use a valid setting value so the second update call is successful Settings validIndexSetting = Settings.builder().put("index.test.setting", "valid").build(); UpdateSettingsStep step = new UpdateSettingsStep( - new StepKey("hot", "action", "updateSetting"), new StepKey("hot", "action", "validate"), client(), - validIndexSetting); + new StepKey("hot", "action", "updateSetting"), + new StepKey("hot", "action", "validate"), + client(), + validIndexSetting + ); step.performAction(indexMetadata, state, observer, new ActionListener<>() { @Override diff --git a/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/slm/SLMSnapshotBlockingIntegTests.java b/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/slm/SLMSnapshotBlockingIntegTests.java index 0e07246601976..b1eb667d3d5ec 100644 --- a/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/slm/SLMSnapshotBlockingIntegTests.java +++ b/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/slm/SLMSnapshotBlockingIntegTests.java @@ -76,7 +76,8 @@ public class SLMSnapshotBlockingIntegTests extends AbstractSnapshotIntegTestCase @Override protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { - return Settings.builder().put(super.nodeSettings(nodeOrdinal, otherSettings)) + return Settings.builder() + .put(super.nodeSettings(nodeOrdinal, otherSettings)) .put(LifecycleSettings.LIFECYCLE_HISTORY_INDEX_ENABLED, false) .build(); } @@ -92,14 +93,18 @@ public void ensureClusterNodes() { @After public void cleanUp() throws Exception { awaitNoMoreRunningOperations(); - DeleteDataStreamAction.Request req = new DeleteDataStreamAction.Request(new String[]{SLM_HISTORY_DATA_STREAM}); + DeleteDataStreamAction.Request req = new DeleteDataStreamAction.Request(new String[] { SLM_HISTORY_DATA_STREAM }); assertAcked(client().execute(DeleteDataStreamAction.INSTANCE, req).get()); } @Override protected Collection> nodePlugins() { - return Arrays.asList(MockRepository.Plugin.class, LocalStateCompositeXPackPlugin.class, IndexLifecycle.class, - DataStreamsPlugin.class); + return Arrays.asList( + MockRepository.Plugin.class, + LocalStateCompositeXPackPlugin.class, + IndexLifecycle.class, + DataStreamsPlugin.class + ); } public void testSnapshotInProgress() throws Exception { @@ -122,8 +127,10 @@ public void testSnapshotInProgress() throws Exception { // Check that the executed snapshot shows up in the SLM output assertBusy(() -> { - GetSnapshotLifecycleAction.Response getResp = - client().execute(GetSnapshotLifecycleAction.INSTANCE, new GetSnapshotLifecycleAction.Request(policyName)).get(); + GetSnapshotLifecycleAction.Response getResp = client().execute( + GetSnapshotLifecycleAction.INSTANCE, + new GetSnapshotLifecycleAction.Request(policyName) + ).get(); logger.info("--> checking for in progress snapshot..."); assertThat(getResp.getPolicies().size(), greaterThan(0)); @@ -132,8 +139,10 @@ public void testSnapshotInProgress() throws Exception { SnapshotLifecyclePolicyItem.SnapshotInProgress inProgress = item.getSnapshotInProgress(); assertThat(inProgress.getSnapshotId().getName(), equalTo(snapshotName)); assertThat(inProgress.getStartTime(), greaterThan(0L)); - assertThat(inProgress.getState(), - anyOf(equalTo(SnapshotsInProgress.State.STARTED), equalTo(SnapshotsInProgress.State.SUCCESS))); + assertThat( + inProgress.getState(), + anyOf(equalTo(SnapshotsInProgress.State.STARTED), equalTo(SnapshotsInProgress.State.SUCCESS)) + ); assertNull(inProgress.getFailure()); }); @@ -158,8 +167,16 @@ public void testRetentionWhileSnapshotInProgress() throws Exception { createRepository(REPO, "mock"); logger.info("--> creating policy {}", policyId); - createSnapshotPolicy(policyId, "snap", NEVER_EXECUTE_CRON_SCHEDULE, REPO, indexName, true, - false, new SnapshotRetentionConfiguration(TimeValue.timeValueSeconds(0), null, null)); + createSnapshotPolicy( + policyId, + "snap", + NEVER_EXECUTE_CRON_SCHEDULE, + REPO, + indexName, + true, + false, + new SnapshotRetentionConfiguration(TimeValue.timeValueSeconds(0), null, null) + ); // Create a snapshot and wait for it to be complete (need something that can be deleted) final String completedSnapshotName = executePolicy(policyId); @@ -198,8 +215,10 @@ public void testRetentionWhileSnapshotInProgress() throws Exception { waitForBlockOnAnyDataNode(REPO); assertBusy(() -> { logger.info("--> at least one data node has hit the block"); - GetSnapshotLifecycleAction.Response getResp = - client().execute(GetSnapshotLifecycleAction.INSTANCE, new GetSnapshotLifecycleAction.Request(policyId)).get(); + GetSnapshotLifecycleAction.Response getResp = client().execute( + GetSnapshotLifecycleAction.INSTANCE, + new GetSnapshotLifecycleAction.Request(policyId) + ).get(); logger.info("--> checking for in progress snapshot..."); assertThat(getResp.getPolicies().size(), greaterThan(0)); @@ -208,15 +227,20 @@ public void testRetentionWhileSnapshotInProgress() throws Exception { SnapshotLifecyclePolicyItem.SnapshotInProgress inProgress = item.getSnapshotInProgress(); assertThat(inProgress.getSnapshotId().getName(), equalTo(secondSnapName)); assertThat(inProgress.getStartTime(), greaterThan(0L)); - assertThat(inProgress.getState(), anyOf(equalTo(SnapshotsInProgress.State.INIT), - equalTo(SnapshotsInProgress.State.STARTED))); + assertThat( + inProgress.getState(), + anyOf(equalTo(SnapshotsInProgress.State.INIT), equalTo(SnapshotsInProgress.State.STARTED)) + ); assertNull(inProgress.getFailure()); }, 60, TimeUnit.SECONDS); // Run retention logger.info("--> triggering retention"); - assertTrue(client().execute(ExecuteSnapshotRetentionAction.INSTANCE, - new ExecuteSnapshotRetentionAction.Request()).get().isAcknowledged()); + assertTrue( + client().execute(ExecuteSnapshotRetentionAction.INSTANCE, new ExecuteSnapshotRetentionAction.Request()) + .get() + .isAcknowledged() + ); logger.info("--> unblocking snapshots"); unblockNode(REPO, internalCluster().getMasterName()); @@ -250,9 +274,13 @@ public void testRetentionWhileSnapshotInProgress() throws Exception { // Assert that the history document has been written for taking the snapshot and deleting it assertBusy(() -> { SearchResponse resp = client().prepareSearch(".slm-history*") - .setQuery(QueryBuilders.matchQuery("snapshot_name", completedSnapshotName)).get(); - logger.info("--> checking history written for {}, got: {}", - completedSnapshotName, Strings.arrayToCommaDelimitedString(resp.getHits().getHits())); + .setQuery(QueryBuilders.matchQuery("snapshot_name", completedSnapshotName)) + .get(); + logger.info( + "--> checking history written for {}, got: {}", + completedSnapshotName, + Strings.arrayToCommaDelimitedString(resp.getHits().getHits()) + ); assertThat(resp.getHits().getTotalHits().value, equalTo(2L)); }); } finally { @@ -274,16 +302,26 @@ public void testRetentionWithMultipleRepositories() throws Exception { final String secondRepo = "other-repo"; createRepository(secondRepo, "fs"); final String policyId = "some-policy-id"; - createSnapshotPolicy(policyId, "snap", NEVER_EXECUTE_CRON_SCHEDULE, secondRepo, - "*", true, - true, new SnapshotRetentionConfiguration(null, 1, 2)); + createSnapshotPolicy( + policyId, + "snap", + NEVER_EXECUTE_CRON_SCHEDULE, + secondRepo, + "*", + true, + true, + new SnapshotRetentionConfiguration(null, 1, 2) + ); logger.info("--> start snapshot"); client().execute(ExecuteSnapshotLifecycleAction.INSTANCE, new ExecuteSnapshotLifecycleAction.Request(policyId)).get(); // make sure the SLM history data stream is green and won't not be green for long because of delayed allocation when data nodes // are stopped ensureGreen(SLM_HISTORY_DATA_STREAM); - client().admin().indices().prepareUpdateSettings(SLM_HISTORY_DATA_STREAM).setSettings( - Settings.builder().put(UnassignedInfo.INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING.getKey(), 0)).get(); + client().admin() + .indices() + .prepareUpdateSettings(SLM_HISTORY_DATA_STREAM) + .setSettings(Settings.builder().put(UnassignedInfo.INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING.getKey(), 0)) + .get(); testUnsuccessfulSnapshotRetention(randomBoolean()); } @@ -295,8 +333,16 @@ private void testUnsuccessfulSnapshotRetention(boolean partialSuccess) throws Ex createAndPopulateIndex(indexName); createRepositoryNoVerify(REPO, "mock"); - createSnapshotPolicy(policyId, "snap", NEVER_EXECUTE_CRON_SCHEDULE, REPO, indexName, true, - partialSuccess, new SnapshotRetentionConfiguration(null, 1, 2)); + createSnapshotPolicy( + policyId, + "snap", + NEVER_EXECUTE_CRON_SCHEDULE, + REPO, + indexName, + true, + partialSuccess, + new SnapshotRetentionConfiguration(null, 1, 2) + ); // Create a failed snapshot AtomicReference failedSnapshotName = new AtomicReference<>(); @@ -304,14 +350,19 @@ private void testUnsuccessfulSnapshotRetention(boolean partialSuccess) throws Ex if (partialSuccess) { logger.info("--> stopping random data node, which should cause shards to go missing"); internalCluster().stopRandomDataNode(); - assertBusy(() -> assertEquals(ClusterHealthStatus.RED, client().admin().cluster().prepareHealth().get().getStatus()), - 30, TimeUnit.SECONDS); + assertBusy( + () -> assertEquals(ClusterHealthStatus.RED, client().admin().cluster().prepareHealth().get().getStatus()), + 30, + TimeUnit.SECONDS + ); blockMasterFromFinalizingSnapshotOnIndexFile(REPO); logger.info("--> start snapshot"); - ActionFuture snapshotFuture = client() - .execute(ExecuteSnapshotLifecycleAction.INSTANCE, new ExecuteSnapshotLifecycleAction.Request(policyId)); + ActionFuture snapshotFuture = client().execute( + ExecuteSnapshotLifecycleAction.INSTANCE, + new ExecuteSnapshotLifecycleAction.Request(policyId) + ); waitForBlock(internalCluster().getMasterName(), REPO); @@ -323,16 +374,18 @@ private void testUnsuccessfulSnapshotRetention(boolean partialSuccess) throws Ex assertNotNull(failedSnapshotName.get()); } else { final String snapshotName = "failed-snapshot-1"; - addBwCFailedSnapshot(REPO, snapshotName, - Collections.singletonMap(SnapshotsService.POLICY_ID_METADATA_FIELD, policyId)); + addBwCFailedSnapshot(REPO, snapshotName, Collections.singletonMap(SnapshotsService.POLICY_ID_METADATA_FIELD, policyId)); failedSnapshotName.set(snapshotName); } logger.info("--> verify that snapshot [{}] is {}", failedSnapshotName.get(), expectedUnsuccessfulState); assertBusy(() -> { try { - GetSnapshotsResponse snapshotsStatusResponse = client().admin().cluster() - .prepareGetSnapshots(REPO).setSnapshots(failedSnapshotName.get()).get(); + GetSnapshotsResponse snapshotsStatusResponse = client().admin() + .cluster() + .prepareGetSnapshots(REPO) + .setSnapshots(failedSnapshotName.get()) + .get(); SnapshotInfo snapshotInfo = snapshotsStatusResponse.getSnapshots().get(0); assertEquals(expectedUnsuccessfulState, snapshotInfo.state()); } catch (SnapshotMissingException ex) { @@ -361,8 +414,10 @@ private void testUnsuccessfulSnapshotRetention(boolean partialSuccess) throws Ex logger.info("--> taking new snapshot"); - ActionFuture snapshotResponse = client() - .execute(ExecuteSnapshotLifecycleAction.INSTANCE, new ExecuteSnapshotLifecycleAction.Request(policyId)); + ActionFuture snapshotResponse = client().execute( + ExecuteSnapshotLifecycleAction.INSTANCE, + new ExecuteSnapshotLifecycleAction.Request(policyId) + ); logger.info("--> waiting for snapshot to complete"); successfulSnapshotName.set(snapshotResponse.get().getSnapshotName()); assertNotNull(successfulSnapshotName.get()); @@ -370,8 +425,12 @@ private void testUnsuccessfulSnapshotRetention(boolean partialSuccess) throws Ex assertBusy(() -> { final SnapshotInfo snapshotInfo; try { - GetSnapshotsResponse snapshotsStatusResponse = client().admin().cluster() - .prepareGetSnapshots(REPO).setSnapshots(successfulSnapshotName.get()).execute().actionGet(); + GetSnapshotsResponse snapshotsStatusResponse = client().admin() + .cluster() + .prepareGetSnapshots(REPO) + .setSnapshots(successfulSnapshotName.get()) + .execute() + .actionGet(); snapshotInfo = snapshotsStatusResponse.getSnapshots().get(0); } catch (SnapshotMissingException sme) { throw new AssertionError(sme); @@ -383,8 +442,11 @@ private void testUnsuccessfulSnapshotRetention(boolean partialSuccess) throws Ex // Check that the failed snapshot from before still exists, now that retention has run { logger.info("--> verify that snapshot [{}] still exists", failedSnapshotName.get()); - GetSnapshotsResponse snapshotsStatusResponse = client().admin().cluster() - .prepareGetSnapshots(REPO).setSnapshots(failedSnapshotName.get()).get(); + GetSnapshotsResponse snapshotsStatusResponse = client().admin() + .cluster() + .prepareGetSnapshots(REPO) + .setSnapshots(failedSnapshotName.get()) + .get(); SnapshotInfo snapshotInfo = snapshotsStatusResponse.getSnapshots().get(0); assertEquals(expectedUnsuccessfulState, snapshotInfo.state()); } @@ -396,16 +458,26 @@ private void testUnsuccessfulSnapshotRetention(boolean partialSuccess) throws Ex logger.info("--> waiting for {} snapshot [{}] to be deleted", expectedUnsuccessfulState, failedSnapshotName.get()); assertBusy(() -> { try { - GetSnapshotsResponse snapshotsStatusResponse = client().admin().cluster() - .prepareGetSnapshots(REPO).setSnapshots(failedSnapshotName.get()).get(); + GetSnapshotsResponse snapshotsStatusResponse = client().admin() + .cluster() + .prepareGetSnapshots(REPO) + .setSnapshots(failedSnapshotName.get()) + .get(); assertThat(snapshotsStatusResponse.getSnapshots(), empty()); } catch (SnapshotMissingException e) { // This is what we want to happen } - logger.info("--> {} snapshot [{}] has been deleted, checking successful snapshot [{}] still exists", - expectedUnsuccessfulState, failedSnapshotName.get(), successfulSnapshotName.get()); - GetSnapshotsResponse snapshotsStatusResponse = client().admin().cluster() - .prepareGetSnapshots(REPO).setSnapshots(successfulSnapshotName.get()).get(); + logger.info( + "--> {} snapshot [{}] has been deleted, checking successful snapshot [{}] still exists", + expectedUnsuccessfulState, + failedSnapshotName.get(), + successfulSnapshotName.get() + ); + GetSnapshotsResponse snapshotsStatusResponse = client().admin() + .cluster() + .prepareGetSnapshots(REPO) + .setSnapshots(successfulSnapshotName.get()) + .get(); SnapshotInfo snapshotInfo = snapshotsStatusResponse.getSnapshots().get(0); assertEquals(SnapshotState.SUCCESS, snapshotInfo.state()); }, 30L, TimeUnit.SECONDS); @@ -419,16 +491,26 @@ public void testSLMRetentionAfterRestore() throws Exception { createRepository(REPO, "mock"); logger.info("--> creating policy {}", policyName); - createSnapshotPolicy(policyName, "snap", NEVER_EXECUTE_CRON_SCHEDULE, REPO, indexName, true, false, - new SnapshotRetentionConfiguration(TimeValue.ZERO, null, null)); + createSnapshotPolicy( + policyName, + "snap", + NEVER_EXECUTE_CRON_SCHEDULE, + REPO, + indexName, + true, + false, + new SnapshotRetentionConfiguration(TimeValue.ZERO, null, null) + ); logger.info("--> executing snapshot lifecycle"); final String snapshotName = executePolicy(policyName); // Check that the executed snapshot shows up in the SLM output assertBusy(() -> { - GetSnapshotLifecycleAction.Response getResp = - client().execute(GetSnapshotLifecycleAction.INSTANCE, new GetSnapshotLifecycleAction.Request(policyName)).get(); + GetSnapshotLifecycleAction.Response getResp = client().execute( + GetSnapshotLifecycleAction.INSTANCE, + new GetSnapshotLifecycleAction.Request(policyName) + ).get(); logger.info("--> checking for in progress snapshot..."); assertThat(getResp.getPolicies().size(), greaterThan(0)); @@ -452,8 +534,11 @@ public void testSLMRetentionAfterRestore() throws Exception { logger.info("--> waiting for {} snapshot to be deleted", snapshotName); assertBusy(() -> { try { - GetSnapshotsResponse snapshotsStatusResponse = client().admin().cluster() - .prepareGetSnapshots(REPO).setSnapshots(snapshotName).get(); + GetSnapshotsResponse snapshotsStatusResponse = client().admin() + .cluster() + .prepareGetSnapshots(REPO) + .setSnapshots(snapshotName) + .get(); assertThat(snapshotsStatusResponse.getSnapshots(), empty()); } catch (SnapshotMissingException e) { // This is what we want to happen @@ -473,29 +558,58 @@ private void createAndPopulateIndex(String indexName) throws InterruptedExceptio indexRandomDocs(indexName, randomIntBetween(50, 100)); } - private void createSnapshotPolicy(String policyName, String snapshotNamePattern, String schedule, String repoId, - String indexPattern, boolean ignoreUnavailable) { - createSnapshotPolicy(policyName, snapshotNamePattern, schedule, repoId, indexPattern, - ignoreUnavailable, false, SnapshotRetentionConfiguration.EMPTY); + private void createSnapshotPolicy( + String policyName, + String snapshotNamePattern, + String schedule, + String repoId, + String indexPattern, + boolean ignoreUnavailable + ) { + createSnapshotPolicy( + policyName, + snapshotNamePattern, + schedule, + repoId, + indexPattern, + ignoreUnavailable, + false, + SnapshotRetentionConfiguration.EMPTY + ); } - private void createSnapshotPolicy(String policyName, String snapshotNamePattern, String schedule, String repoId, - String indexPattern, boolean ignoreUnavailable, - boolean partialSnapsAllowed, SnapshotRetentionConfiguration retention) { + private void createSnapshotPolicy( + String policyName, + String snapshotNamePattern, + String schedule, + String repoId, + String indexPattern, + boolean ignoreUnavailable, + boolean partialSnapsAllowed, + SnapshotRetentionConfiguration retention + ) { Map snapConfig = new HashMap<>(); snapConfig.put("indices", Collections.singletonList(indexPattern)); snapConfig.put("ignore_unavailable", ignoreUnavailable); snapConfig.put("partial", partialSnapsAllowed); if (randomBoolean()) { Map metadata = new HashMap<>(); - int fieldCount = randomIntBetween(2,5); + int fieldCount = randomIntBetween(2, 5); for (int i = 0; i < fieldCount; i++) { - metadata.put(randomValueOtherThanMany(key -> "policy".equals(key) || metadata.containsKey(key), - () -> randomAlphaOfLength(5)), randomAlphaOfLength(4)); + metadata.put( + randomValueOtherThanMany(key -> "policy".equals(key) || metadata.containsKey(key), () -> randomAlphaOfLength(5)), + randomAlphaOfLength(4) + ); } } - SnapshotLifecyclePolicy policy = new SnapshotLifecyclePolicy(policyName, snapshotNamePattern, schedule, - repoId, snapConfig, retention); + SnapshotLifecyclePolicy policy = new SnapshotLifecyclePolicy( + policyName, + snapshotNamePattern, + schedule, + repoId, + snapConfig, + retention + ); PutSnapshotLifecycleAction.Request putLifecycle = new PutSnapshotLifecycleAction.Request(policyName, policy); try { diff --git a/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/slm/SnapshotLifecycleInitialisationTests.java b/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/slm/SnapshotLifecycleInitialisationTests.java index 4ac92e693f655..9dc38cccedfb2 100644 --- a/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/slm/SnapshotLifecycleInitialisationTests.java +++ b/x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/slm/SnapshotLifecycleInitialisationTests.java @@ -59,14 +59,26 @@ protected Collection> getPlugins() { } public void testSLMIsInRunningModeWhenILMIsDisabled() throws Exception { - client().execute(PutRepositoryAction.INSTANCE, - new PutRepositoryRequest().name("repo").type("fs") + client().execute( + PutRepositoryAction.INSTANCE, + new PutRepositoryRequest().name("repo") + .type("fs") .settings(Settings.builder().put("repositories.fs.location", repositoryLocation).build()) ).get(10, TimeUnit.SECONDS); - client().execute(PutSnapshotLifecycleAction.INSTANCE, - new Request("snapshot-policy", new SnapshotLifecyclePolicy("test-policy", "snap", - "0 0/15 * * * ?", "repo", Collections.emptyMap(), SnapshotRetentionConfiguration.EMPTY)) + client().execute( + PutSnapshotLifecycleAction.INSTANCE, + new Request( + "snapshot-policy", + new SnapshotLifecyclePolicy( + "test-policy", + "snap", + "0 0/15 * * * ?", + "repo", + Collections.emptyMap(), + SnapshotRetentionConfiguration.EMPTY + ) + ) ).get(10, TimeUnit.SECONDS); ClusterState state = getInstanceFromNode(ClusterService.class).state(); diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/cluster/metadata/MetadataMigrateToDataTiersRoutingService.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/cluster/metadata/MetadataMigrateToDataTiersRoutingService.java index d7bab2749f69c..075959efb2267 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/cluster/metadata/MetadataMigrateToDataTiersRoutingService.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/cluster/metadata/MetadataMigrateToDataTiersRoutingService.java @@ -63,8 +63,7 @@ public final class MetadataMigrateToDataTiersRoutingService { public static final String DEFAULT_NODE_ATTRIBUTE_NAME = "data"; private static final Logger logger = LogManager.getLogger(MetadataMigrateToDataTiersRoutingService.class); - private MetadataMigrateToDataTiersRoutingService() { - } + private MetadataMigrateToDataTiersRoutingService() {} /** * Migrates the elasticsearch abstractions to use data tiers for allocation routing. @@ -116,15 +115,19 @@ private MetadataMigrateToDataTiersRoutingService() { * This returns a new {@link ClusterState} representing the migrated state that is ready to use data tiers for index and * ILM routing allocations. It also returns a summary of the affected abstractions encapsulated in {@link MigratedEntities} */ - public static Tuple migrateToDataTiersRouting(ClusterState currentState, - @Nullable String nodeAttrName, - @Nullable String indexTemplateToDelete, - NamedXContentRegistry xContentRegistry, Client client, - XPackLicenseState licenseState) { + public static Tuple migrateToDataTiersRouting( + ClusterState currentState, + @Nullable String nodeAttrName, + @Nullable String indexTemplateToDelete, + NamedXContentRegistry xContentRegistry, + Client client, + XPackLicenseState licenseState + ) { IndexLifecycleMetadata currentMetadata = currentState.metadata().custom(IndexLifecycleMetadata.TYPE); if (currentMetadata != null && currentMetadata.getOperationMode() != STOPPED) { - throw new IllegalStateException("stop ILM before migrating to data tiers, current state is [" + - currentMetadata.getOperationMode() + "]"); + throw new IllegalStateException( + "stop ILM before migrating to data tiers, current state is [" + currentMetadata.getOperationMode() + "]" + ); } Metadata.Builder mb = Metadata.builder(currentState.metadata()); @@ -161,8 +164,10 @@ public static Tuple migrateToDataTiersRouting(Cl ClusterState intermediateState = ClusterState.builder(currentState).metadata(mb).build(); mb = Metadata.builder(intermediateState.metadata()); List migratedIndices = migrateIndices(mb, intermediateState, attribute); - return Tuple.tuple(ClusterState.builder(currentState).metadata(mb).build(), - new MigratedEntities(removedIndexTemplateName, migratedIndices, migratedPolicies)); + return Tuple.tuple( + ClusterState.builder(currentState).metadata(mb).build(), + new MigratedEntities(removedIndexTemplateName, migratedIndices, migratedPolicies) + ); } /** @@ -172,8 +177,14 @@ public static Tuple migrateToDataTiersRouting(Cl * This also iterates through all the indices that are executing a given *migrated* policy and refreshes the cached phase definition * for each of these managed indices. */ - static List migrateIlmPolicies(Metadata.Builder mb, ClusterState currentState, String nodeAttrName, - NamedXContentRegistry xContentRegistry, Client client, XPackLicenseState licenseState) { + static List migrateIlmPolicies( + Metadata.Builder mb, + ClusterState currentState, + String nodeAttrName, + NamedXContentRegistry xContentRegistry, + Client client, + XPackLicenseState licenseState + ) { IndexLifecycleMetadata currentLifecycleMetadata = currentState.metadata().custom(IndexLifecycleMetadata.TYPE); if (currentLifecycleMetadata == null) { return Collections.emptyList(); @@ -187,11 +198,15 @@ static List migrateIlmPolicies(Metadata.Builder mb, ClusterState current if (newLifecyclePolicy != null) { // we updated at least one phase long nextVersion = policyMetadataEntry.getValue().getVersion() + 1L; - LifecyclePolicyMetadata newPolicyMetadata = new LifecyclePolicyMetadata(newLifecyclePolicy, - policyMetadataEntry.getValue().getHeaders(), nextVersion, Instant.now().toEpochMilli()); + LifecyclePolicyMetadata newPolicyMetadata = new LifecyclePolicyMetadata( + newLifecyclePolicy, + policyMetadataEntry.getValue().getHeaders(), + nextVersion, + Instant.now().toEpochMilli() + ); LifecyclePolicyMetadata oldPolicyMetadata = newPolicies.put(policyMetadataEntry.getKey(), newPolicyMetadata); - assert oldPolicyMetadata != null : - "we must only update policies, not create new ones, but " + policyMetadataEntry.getKey() + " didn't exist"; + assert oldPolicyMetadata != null + : "we must only update policies, not create new ones, but " + policyMetadataEntry.getKey() + " didn't exist"; refreshCachedPhases(mb, currentState, oldPolicyMetadata, newPolicyMetadata, xContentRegistry, client, licenseState); migratedPolicies.add(policyMetadataEntry.getKey()); @@ -208,26 +223,44 @@ static List migrateIlmPolicies(Metadata.Builder mb, ClusterState current /** * Refreshed the cached ILM phase definition for the indices managed by the migrated policy. */ - static void refreshCachedPhases(Metadata.Builder mb, ClusterState currentState, LifecyclePolicyMetadata oldPolicyMetadata, - LifecyclePolicyMetadata newPolicyMetadata, NamedXContentRegistry xContentRegistry, - Client client, XPackLicenseState licenseState) { + static void refreshCachedPhases( + Metadata.Builder mb, + ClusterState currentState, + LifecyclePolicyMetadata oldPolicyMetadata, + LifecyclePolicyMetadata newPolicyMetadata, + NamedXContentRegistry xContentRegistry, + Client client, + XPackLicenseState licenseState + ) { // this performs a walk through the managed indices and safely updates the cached phase (ie. for the phases we did not // remove the allocate action) updateIndicesForPolicy(mb, currentState, xContentRegistry, client, oldPolicyMetadata.getPolicy(), newPolicyMetadata, licenseState); LifecyclePolicy newLifecyclePolicy = newPolicyMetadata.getPolicy(); - List migratedPhasesWithoutAllocateAction = - getMigratedPhasesWithoutAllocateAction(oldPolicyMetadata.getPolicy(), newLifecyclePolicy); + List migratedPhasesWithoutAllocateAction = getMigratedPhasesWithoutAllocateAction( + oldPolicyMetadata.getPolicy(), + newLifecyclePolicy + ); if (migratedPhasesWithoutAllocateAction.size() > 0) { - logger.debug("the updated policy [{}] does not contain the allocate action in phases [{}] anymore", - newLifecyclePolicy.getName(), migratedPhasesWithoutAllocateAction); + logger.debug( + "the updated policy [{}] does not contain the allocate action in phases [{}] anymore", + newLifecyclePolicy.getName(), + migratedPhasesWithoutAllocateAction + ); // if we removed the allocate action in any phase we won't be able to perform a safe update of the ilm cached phase (as // defined by {@link PhaseCacheManagement#isIndexPhaseDefinitionUpdatable} because the number of steps in the new phase is // not the same as in the cached phase) so let's forcefully (and still safely :) ) refresh the cached phase for the managed // indices in these phases. - refreshCachedPhaseForPhasesWithoutAllocateAction(mb, currentState, oldPolicyMetadata.getPolicy(), newPolicyMetadata, - migratedPhasesWithoutAllocateAction, client, licenseState); + refreshCachedPhaseForPhasesWithoutAllocateAction( + mb, + currentState, + oldPolicyMetadata.getPolicy(), + newPolicyMetadata, + migratedPhasesWithoutAllocateAction, + client, + licenseState + ); } } @@ -239,16 +272,22 @@ static void refreshCachedPhases(Metadata.Builder mb, ClusterState currentState, * inject at the end of every phase) * 2) if the index is anywhere else in the phase, we simply update the cached phase definition to reflect the migrated phase */ - private static void refreshCachedPhaseForPhasesWithoutAllocateAction(Metadata.Builder mb, ClusterState currentState, - LifecyclePolicy oldPolicy, - LifecyclePolicyMetadata newPolicyMetadata, - List phasesWithoutAllocateAction, Client client, - XPackLicenseState licenseState) { + private static void refreshCachedPhaseForPhasesWithoutAllocateAction( + Metadata.Builder mb, + ClusterState currentState, + LifecyclePolicy oldPolicy, + LifecyclePolicyMetadata newPolicyMetadata, + List phasesWithoutAllocateAction, + Client client, + XPackLicenseState licenseState + ) { String policyName = oldPolicy.getName(); - final List managedIndices = - currentState.metadata().indices().values().stream() - .filter(meta -> policyName.equals(LifecycleSettings.LIFECYCLE_NAME_SETTING.get(meta.getSettings()))) - .collect(Collectors.toList()); + final List managedIndices = currentState.metadata() + .indices() + .values() + .stream() + .filter(meta -> policyName.equals(LifecycleSettings.LIFECYCLE_NAME_SETTING.get(meta.getSettings()))) + .collect(Collectors.toList()); for (IndexMetadata indexMetadata : managedIndices) { LifecycleExecutionState currentExState = LifecycleExecutionState.fromIndexMetadata(indexMetadata); @@ -261,8 +300,15 @@ private static void refreshCachedPhaseForPhasesWithoutAllocateAction(Metadata.Bu // this index is in the middle of executing the allocate action - which doesn't exist in the updated policy // anymore so let's try to move the index to the next action - LifecycleExecutionState newLifecycleState = moveStateToNextActionAndUpdateCachedPhase(indexMetadata, - currentExState, System::currentTimeMillis, oldPolicy, newPolicyMetadata, client, licenseState); + LifecycleExecutionState newLifecycleState = moveStateToNextActionAndUpdateCachedPhase( + indexMetadata, + currentExState, + System::currentTimeMillis, + oldPolicy, + newPolicyMetadata, + client, + licenseState + ); if (currentExState.equals(newLifecycleState) == false) { mb.put(IndexMetadata.builder(indexMetadata).putCustom(ILM_CUSTOM_METADATA_KEY, newLifecycleState.asMap())); } @@ -273,16 +319,23 @@ private static void refreshCachedPhaseForPhasesWithoutAllocateAction(Metadata.Bu // executing the allocate action, we made sure of that) LifecycleExecutionState.Builder updatedState = LifecycleExecutionState.builder(currentExState); - PhaseExecutionInfo phaseExecutionInfo = new PhaseExecutionInfo(newPolicyMetadata.getPolicy().getName(), - newPolicyMetadata.getPolicy().getPhases().get(currentStepKey.getPhase()), newPolicyMetadata.getVersion(), - newPolicyMetadata.getModifiedDate()); + PhaseExecutionInfo phaseExecutionInfo = new PhaseExecutionInfo( + newPolicyMetadata.getPolicy().getName(), + newPolicyMetadata.getPolicy().getPhases().get(currentStepKey.getPhase()), + newPolicyMetadata.getVersion(), + newPolicyMetadata.getModifiedDate() + ); String newPhaseDefinition = Strings.toString(phaseExecutionInfo, false, false); updatedState.setPhaseDefinition(newPhaseDefinition); - logger.debug("updating the cached phase definition for index [{}], current step [{}] in policy " + - "[{}] to [{}]", indexMetadata.getIndex().getName(), currentStepKey, policyName, newPhaseDefinition); - mb.put(IndexMetadata.builder(indexMetadata) - .putCustom(ILM_CUSTOM_METADATA_KEY, updatedState.build().asMap())); + logger.debug( + "updating the cached phase definition for index [{}], current step [{}] in policy " + "[{}] to [{}]", + indexMetadata.getIndex().getName(), + currentStepKey, + policyName, + newPhaseDefinition + ); + mb.put(IndexMetadata.builder(indexMetadata).putCustom(ILM_CUSTOM_METADATA_KEY, updatedState.build().asMap())); } } } @@ -332,17 +385,24 @@ private static LifecyclePolicy migrateSingleILMPolicy(String nodeAttrName, Lifec // rules to allow for the migrate action to be injected if (allocateAction.getNumberOfReplicas() != null) { // keep the number of replicas configuration - AllocateAction updatedAllocateAction = - new AllocateAction(allocateAction.getNumberOfReplicas(), allocateAction.getTotalShardsPerNode(), - null, null, null); + AllocateAction updatedAllocateAction = new AllocateAction( + allocateAction.getNumberOfReplicas(), + allocateAction.getTotalShardsPerNode(), + null, + null, + null + ); actionMap.put(allocateAction.getWriteableName(), updatedAllocateAction); - logger.debug("ILM policy [{}], phase [{}]: updated the allocate action to [{}]", lifecyclePolicy.getName(), - phase.getName(), allocateAction); + logger.debug( + "ILM policy [{}], phase [{}]: updated the allocate action to [{}]", + lifecyclePolicy.getName(), + phase.getName(), + allocateAction + ); } else { // remove the action altogether actionMap.remove(allocateAction.getWriteableName()); - logger.debug("ILM policy [{}], phase [{}]: removed the allocate action", lifecyclePolicy.getName(), - phase.getName()); + logger.debug("ILM policy [{}], phase [{}]: removed the allocate action", lifecyclePolicy.getName(), phase.getName()); } // we removed the allocate action allocation rules (or the action completely) so let's check if there is an @@ -351,14 +411,18 @@ private static LifecyclePolicy migrateSingleILMPolicy(String nodeAttrName, Lifec MigrateAction migrateAction = (MigrateAction) actionMap.get(MigrateAction.NAME); if (migrateAction.isEnabled() == false) { actionMap.remove(MigrateAction.NAME); - logger.debug("ILM policy [{}], phase [{}]: removed the deactivated migrate action", lifecyclePolicy.getName(), - phase.getName()); + logger.debug( + "ILM policy [{}], phase [{}]: removed the deactivated migrate action", + lifecyclePolicy.getName(), + phase.getName() + ); } } Phase updatedPhase = new Phase(phase.getName(), phase.getMinimumAge(), actionMap); - Map updatedPhases = - new HashMap<>(newLifecyclePolicy == null ? lifecyclePolicy.getPhases() : newLifecyclePolicy.getPhases()); + Map updatedPhases = new HashMap<>( + newLifecyclePolicy == null ? lifecyclePolicy.getPhases() : newLifecyclePolicy.getPhases() + ); updatedPhases.put(phaseEntry.getKey(), updatedPhase); newLifecyclePolicy = new LifecyclePolicy(lifecyclePolicy.getName(), updatedPhases); } @@ -370,9 +434,10 @@ private static LifecyclePolicy migrateSingleILMPolicy(String nodeAttrName, Lifec * Returns true of the provided {@link AllocateAction} defines any index allocation rules. */ static boolean allocateActionDefinesRoutingRules(String nodeAttrName, @Nullable AllocateAction allocateAction) { - return allocateAction != null && (allocateAction.getRequire().get(nodeAttrName) != null || - allocateAction.getInclude().get(nodeAttrName) != null || - allocateAction.getExclude().get(nodeAttrName) != null); + return allocateAction != null + && (allocateAction.getRequire().get(nodeAttrName) != null + || allocateAction.getInclude().get(nodeAttrName) != null + || allocateAction.getExclude().get(nodeAttrName) != null); } /** @@ -417,9 +482,9 @@ static List migrateIndices(Metadata.Builder mb, ClusterState currentStat finalSettings.remove(nodeAttrIndexIncludeRoutingSetting); } - mb.put(IndexMetadata.builder(indexMetadata) - .settings(finalSettings) - .settingsVersion(indexMetadata.getSettingsVersion() + 1)); + mb.put( + IndexMetadata.builder(indexMetadata).settings(finalSettings).settingsVersion(indexMetadata.getSettingsVersion() + 1) + ); migratedIndices.add(indexMetadata.getIndex().getName()); } } @@ -433,8 +498,10 @@ static List migrateIndices(Metadata.Builder mb, ClusterState currentStat * * If the migration is **not** executed the current index settings is returned, otherwise the updated settings are returned */ - private static Settings maybeMigrateRoutingSettingToTierPreference(String attributeBasedRoutingSettingName, - IndexMetadata indexMetadata) { + private static Settings maybeMigrateRoutingSettingToTierPreference( + String attributeBasedRoutingSettingName, + IndexMetadata indexMetadata + ) { Settings currentIndexSettings = indexMetadata.getSettings(); if (currentIndexSettings.keySet().contains(attributeBasedRoutingSettingName) == false) { return currentIndexSettings; @@ -455,13 +522,17 @@ private static Settings maybeMigrateRoutingSettingToTierPreference(String attrib newSettingsBuilder.put(TIER_PREFERENCE, convertedTierPreference); newSettingsBuilder.remove(attributeBasedRoutingSettingName); logger.debug("index [{}]: removed setting [{}]", indexName, attributeBasedRoutingSettingName); - logger.debug("index [{}]: configured setting [{}] to [{}]", indexName, - TIER_PREFERENCE, convertedTierPreference); + logger.debug("index [{}]: configured setting [{}] to [{}]", indexName, TIER_PREFERENCE, convertedTierPreference); } else { // log warning and do *not* remove setting, return the settings unchanged - logger.warn("index [{}]: could not convert attribute based setting [{}] value of [{}] to a tier preference " + - "configuration. the only known values are: {}", indexName, - attributeBasedRoutingSettingName, attributeValue, "hot,warm,cold, and frozen"); + logger.warn( + "index [{}]: could not convert attribute based setting [{}] value of [{}] to a tier preference " + + "configuration. the only known values are: {}", + indexName, + attributeBasedRoutingSettingName, + attributeValue, + "hot,warm,cold, and frozen" + ); return currentIndexSettings; } } @@ -537,9 +608,9 @@ public boolean equals(Object o) { return false; } MigratedEntities that = (MigratedEntities) o; - return Objects.equals(removedIndexTemplateName, that.removedIndexTemplateName) && - Objects.equals(migratedIndices, that.migratedIndices) && - Objects.equals(migratedPolicies, that.migratedPolicies); + return Objects.equals(removedIndexTemplateName, that.removedIndexTemplateName) + && Objects.equals(migratedIndices, that.migratedIndices) + && Objects.equals(migratedPolicies, that.migratedPolicies); } @Override diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/ExecuteStepsUpdateTask.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/ExecuteStepsUpdateTask.java index 25db1bb72ad00..8f0ed0e6d0872 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/ExecuteStepsUpdateTask.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/ExecuteStepsUpdateTask.java @@ -14,8 +14,8 @@ import org.elasticsearch.cluster.ClusterStateUpdateTask; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.common.Strings; -import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.index.Index; +import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xpack.core.ilm.ClusterStateActionStep; import org.elasticsearch.xpack.core.ilm.ClusterStateWaitStep; import org.elasticsearch.xpack.core.ilm.ErrorStep; @@ -37,8 +37,14 @@ public class ExecuteStepsUpdateTask extends IndexLifecycleClusterStateUpdateTask private Step.StepKey nextStepKey = null; private Exception failure = null; - public ExecuteStepsUpdateTask(String policy, Index index, Step startStep, PolicyStepsRegistry policyStepsRegistry, - IndexLifecycleRunner lifecycleRunner, LongSupplier nowSupplier) { + public ExecuteStepsUpdateTask( + String policy, + Index index, + Step startStep, + PolicyStepsRegistry policyStepsRegistry, + IndexLifecycleRunner lifecycleRunner, + LongSupplier nowSupplier + ) { super(index, startStep.getKey()); this.policy = policy; this.startStep = startStep; @@ -88,8 +94,12 @@ public ClusterState doExecute(final ClusterState currentState) throws IOExceptio if (currentStep instanceof ClusterStateActionStep) { // cluster state action step so do the action and // move the cluster state to the next step - logger.trace("[{}] performing cluster state action ({}) [{}]", - index.getName(), currentStep.getClass().getSimpleName(), currentStep.getKey()); + logger.trace( + "[{}] performing cluster state action ({}) [{}]", + index.getName(), + currentStep.getClass().getSimpleName(), + currentStep.getKey() + ); try { state = ((ClusterStateActionStep) currentStep).performAction(index, state); } catch (Exception exception) { @@ -102,8 +112,14 @@ public ClusterState doExecute(final ClusterState currentState) throws IOExceptio return state; } else { logger.trace("[{}] moving cluster state to next step [{}]", index.getName(), nextStepKey); - state = IndexLifecycleTransition.moveClusterStateToStep(index, state, nextStepKey, nowSupplier, - policyStepsRegistry, false); + state = IndexLifecycleTransition.moveClusterStateToStep( + index, + state, + nextStepKey, + nowSupplier, + policyStepsRegistry, + false + ); } } else { // cluster state wait step so evaluate the @@ -112,8 +128,12 @@ public ClusterState doExecute(final ClusterState currentState) throws IOExceptio // cluster state so it can be applied and we will // wait for the next trigger to evaluate the // condition again - logger.trace("[{}] waiting for cluster state step condition ({}) [{}]", - index.getName(), currentStep.getClass().getSimpleName(), currentStep.getKey()); + logger.trace( + "[{}] waiting for cluster state step condition ({}) [{}]", + index.getName(), + currentStep.getClass().getSimpleName(), + currentStep.getKey() + ); ClusterStateWaitStep.Result result; try { result = ((ClusterStateWaitStep) currentStep).isConditionMet(index, state); @@ -125,20 +145,35 @@ public ClusterState doExecute(final ClusterState currentState) throws IOExceptio // re-evaluate what the next step is after we evaluate the condition nextStepKey = currentStep.getNextStepKey(); if (result.isComplete()) { - logger.trace("[{}] cluster state step condition met successfully ({}) [{}], moving to next step {}", - index.getName(), currentStep.getClass().getSimpleName(), currentStep.getKey(), nextStepKey); + logger.trace( + "[{}] cluster state step condition met successfully ({}) [{}], moving to next step {}", + index.getName(), + currentStep.getClass().getSimpleName(), + currentStep.getKey(), + nextStepKey + ); if (nextStepKey == null) { return state; } else { - state = IndexLifecycleTransition.moveClusterStateToStep(index, state, - nextStepKey, nowSupplier, policyStepsRegistry,false); + state = IndexLifecycleTransition.moveClusterStateToStep( + index, + state, + nextStepKey, + nowSupplier, + policyStepsRegistry, + false + ); } } else { final ToXContentObject stepInfo = result.getInfomationContext(); if (logger.isTraceEnabled()) { - logger.trace("[{}] condition not met ({}) [{}], returning existing state (info: {})", - index.getName(), currentStep.getClass().getSimpleName(), currentStep.getKey(), - stepInfo == null ? "null" : Strings.toString(stepInfo)); + logger.trace( + "[{}] condition not met ({}) [{}], returning existing state (info: {})", + index.getName(), + currentStep.getClass().getSimpleName(), + currentStep.getKey(), + stepInfo == null ? "null" : Strings.toString(stepInfo) + ); } // We may have executed a step and set "nextStepKey" to // a value, but in this case, since the condition was @@ -183,8 +218,12 @@ public void onClusterStateProcessed(String source, ClusterState oldState, Cluste } if (nextStepKey != null && nextStepKey != TerminalPolicyStep.KEY) { - logger.trace("[{}] step sequence starting with {} has completed, running next step {} if it is an async action", - index.getName(), startStep.getKey(), nextStepKey); + logger.trace( + "[{}] step sequence starting with {} has completed, running next step {} if it is an async action", + index.getName(), + startStep.getKey(), + nextStepKey + ); // After the cluster state has been processed and we have moved // to a new step, we need to conditionally execute the step iff // it is an `AsyncAction` so that it is executed exactly once. @@ -200,8 +239,12 @@ public void handleFailure(String source, Exception e) { private ClusterState moveToErrorStep(final ClusterState state, Step.StepKey currentStepKey, Exception cause) throws IOException { this.failure = cause; - logger.warn("policy [{}] for index [{}] failed on cluster state step [{}]. Moving to ERROR step", policy, index.getName(), - currentStepKey); + logger.warn( + "policy [{}] for index [{}] failed on cluster state step [{}]. Moving to ERROR step", + policy, + index.getName(), + currentStepKey + ); return IndexLifecycleTransition.moveClusterStateToErrorStep(index, state, cause, nowSupplier, policyStepsRegistry::getStep); } @@ -210,8 +253,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; ExecuteStepsUpdateTask that = (ExecuteStepsUpdateTask) o; - return policy.equals(that.policy) && index.equals(that.index) - && Objects.equals(startStep, that.startStep); + return policy.equals(that.policy) && index.equals(that.index) && Objects.equals(startStep, that.startStep); } @Override diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycle.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycle.java index 088ce1b8d69d5..4e1372d8ab898 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycle.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycle.java @@ -23,8 +23,6 @@ import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsFilter; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.env.Environment; import org.elasticsearch.env.NodeEnvironment; @@ -39,10 +37,10 @@ import org.elasticsearch.script.ScriptService; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.watcher.ResourceWatcherService; -import org.elasticsearch.xpack.core.XPackPlugin; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xpack.cluster.action.MigrateToDataTiersAction; -import org.elasticsearch.xpack.ilm.action.TransportMigrateToDataTiersAction; -import org.elasticsearch.xpack.ilm.action.RestMigrateToDataTiersAction; +import org.elasticsearch.xpack.core.XPackPlugin; import org.elasticsearch.xpack.core.action.XPackInfoFeatureAction; import org.elasticsearch.xpack.core.action.XPackUsageFeatureAction; import org.elasticsearch.xpack.core.ilm.AllocateAction; @@ -89,6 +87,7 @@ import org.elasticsearch.xpack.ilm.action.RestExplainLifecycleAction; import org.elasticsearch.xpack.ilm.action.RestGetLifecycleAction; import org.elasticsearch.xpack.ilm.action.RestGetStatusAction; +import org.elasticsearch.xpack.ilm.action.RestMigrateToDataTiersAction; import org.elasticsearch.xpack.ilm.action.RestMoveToStepAction; import org.elasticsearch.xpack.ilm.action.RestPutLifecycleAction; import org.elasticsearch.xpack.ilm.action.RestRemoveIndexLifecyclePolicyAction; @@ -99,6 +98,7 @@ import org.elasticsearch.xpack.ilm.action.TransportExplainLifecycleAction; import org.elasticsearch.xpack.ilm.action.TransportGetLifecycleAction; import org.elasticsearch.xpack.ilm.action.TransportGetStatusAction; +import org.elasticsearch.xpack.ilm.action.TransportMigrateToDataTiersAction; import org.elasticsearch.xpack.ilm.action.TransportMoveToStepAction; import org.elasticsearch.xpack.ilm.action.TransportPutLifecycleAction; import org.elasticsearch.xpack.ilm.action.TransportRemoveIndexLifecyclePolicyAction; @@ -176,7 +176,8 @@ public List> getSettings() { LifecycleSettings.SLM_HISTORY_INDEX_ENABLED_SETTING, LifecycleSettings.SLM_RETENTION_SCHEDULE_SETTING, LifecycleSettings.SLM_RETENTION_DURATION_SETTING, - LifecycleSettings.SLM_MINIMUM_INTERVAL_SETTING); + LifecycleSettings.SLM_MINIMUM_INTERVAL_SETTING + ); } protected XPackLicenseState getLicenseState() { @@ -184,18 +185,31 @@ protected XPackLicenseState getLicenseState() { } @Override - public Collection createComponents(Client client, ClusterService clusterService, ThreadPool threadPool, - ResourceWatcherService resourceWatcherService, ScriptService scriptService, - NamedXContentRegistry xContentRegistry, Environment environment, - NodeEnvironment nodeEnvironment, NamedWriteableRegistry namedWriteableRegistry, - IndexNameExpressionResolver expressionResolver, - Supplier repositoriesServiceSupplier) { + public Collection createComponents( + Client client, + ClusterService clusterService, + ThreadPool threadPool, + ResourceWatcherService resourceWatcherService, + ScriptService scriptService, + NamedXContentRegistry xContentRegistry, + Environment environment, + NodeEnvironment nodeEnvironment, + NamedWriteableRegistry namedWriteableRegistry, + IndexNameExpressionResolver expressionResolver, + Supplier repositoriesServiceSupplier + ) { final List components = new ArrayList<>(); - ILMHistoryTemplateRegistry ilmTemplateRegistry = - new ILMHistoryTemplateRegistry(settings, clusterService, threadPool, client, xContentRegistry); + ILMHistoryTemplateRegistry ilmTemplateRegistry = new ILMHistoryTemplateRegistry( + settings, + clusterService, + threadPool, + client, + xContentRegistry + ); ilmTemplateRegistry.initialize(); - ilmHistoryStore.set(new ILMHistoryStore(settings, new OriginSettingClient(client, INDEX_LIFECYCLE_ORIGIN), - clusterService, threadPool)); + ilmHistoryStore.set( + new ILMHistoryStore(settings, new OriginSettingClient(client, INDEX_LIFECYCLE_ORIGIN), clusterService, threadPool) + ); /* * Here we use threadPool::absoluteTimeInMillis rather than System::currentTimeInMillis because snapshot start time is set using * ThreadPool.absoluteTimeInMillis(). ThreadPool.absoluteTimeInMillis() returns a cached time that can be several hundred @@ -203,21 +217,48 @@ public Collection createComponents(Client client, ClusterService cluster * time that is before the policy's (or action's) start time if System::currentTimeInMillis is used here. */ LongSupplier nowSupplier = threadPool::absoluteTimeInMillis; - indexLifecycleInitialisationService.set(new IndexLifecycleService(settings, client, clusterService, threadPool, - getClock(), nowSupplier, xContentRegistry, ilmHistoryStore.get(), getLicenseState())); + indexLifecycleInitialisationService.set( + new IndexLifecycleService( + settings, + client, + clusterService, + threadPool, + getClock(), + nowSupplier, + xContentRegistry, + ilmHistoryStore.get(), + getLicenseState() + ) + ); components.add(indexLifecycleInitialisationService.get()); - SnapshotLifecycleTemplateRegistry templateRegistry = new SnapshotLifecycleTemplateRegistry(settings, clusterService, threadPool, - client, xContentRegistry); + SnapshotLifecycleTemplateRegistry templateRegistry = new SnapshotLifecycleTemplateRegistry( + settings, + clusterService, + threadPool, + client, + xContentRegistry + ); templateRegistry.initialize(); - snapshotHistoryStore.set(new SnapshotHistoryStore(settings, new OriginSettingClient(client, INDEX_LIFECYCLE_ORIGIN), - clusterService)); - snapshotLifecycleService.set(new SnapshotLifecycleService(settings, - () -> new SnapshotLifecycleTask(client, clusterService, snapshotHistoryStore.get()), clusterService, getClock())); + snapshotHistoryStore.set( + new SnapshotHistoryStore(settings, new OriginSettingClient(client, INDEX_LIFECYCLE_ORIGIN), clusterService) + ); + snapshotLifecycleService.set( + new SnapshotLifecycleService( + settings, + () -> new SnapshotLifecycleTask(client, clusterService, snapshotHistoryStore.get()), + clusterService, + getClock() + ) + ); snapshotLifecycleService.get().init(); - snapshotRetentionService.set(new SnapshotRetentionService(settings, - () -> new SnapshotRetentionTask(client, clusterService, System::nanoTime, snapshotHistoryStore.get()), - getClock())); + snapshotRetentionService.set( + new SnapshotRetentionService( + settings, + () -> new SnapshotRetentionTask(client, clusterService, System::nanoTime, snapshotHistoryStore.get()), + getClock() + ) + ); snapshotRetentionService.get().init(clusterService); components.addAll(Arrays.asList(snapshotLifecycleService.get(), snapshotHistoryStore.get(), snapshotRetentionService.get())); @@ -231,71 +272,97 @@ public List getNamedWriteables() { @Override public List getNamedXContent() { - List entries = new ArrayList<>(Arrays.asList( - // Custom Metadata - new NamedXContentRegistry.Entry(Metadata.Custom.class, new ParseField(IndexLifecycleMetadata.TYPE), - parser -> IndexLifecycleMetadata.PARSER.parse(parser, null)), - new NamedXContentRegistry.Entry(Metadata.Custom.class, new ParseField(SnapshotLifecycleMetadata.TYPE), - parser -> SnapshotLifecycleMetadata.PARSER.parse(parser, null)), - // Lifecycle Types - new NamedXContentRegistry.Entry(LifecycleType.class, new ParseField(TimeseriesLifecycleType.TYPE), - (p, c) -> TimeseriesLifecycleType.INSTANCE), - // Lifecycle Actions - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(AllocateAction.NAME), AllocateAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ForceMergeAction.NAME), ForceMergeAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ReadOnlyAction.NAME), ReadOnlyAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(RolloverAction.NAME), RolloverAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ShrinkAction.NAME), ShrinkAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(FreezeAction.NAME), FreezeAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(SetPriorityAction.NAME), SetPriorityAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(UnfollowAction.NAME), UnfollowAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(WaitForSnapshotAction.NAME), - WaitForSnapshotAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(SearchableSnapshotAction.NAME), - SearchableSnapshotAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(MigrateAction.NAME), - MigrateAction::parse))); + List entries = new ArrayList<>( + Arrays.asList( + // Custom Metadata + new NamedXContentRegistry.Entry( + Metadata.Custom.class, + new ParseField(IndexLifecycleMetadata.TYPE), + parser -> IndexLifecycleMetadata.PARSER.parse(parser, null) + ), + new NamedXContentRegistry.Entry( + Metadata.Custom.class, + new ParseField(SnapshotLifecycleMetadata.TYPE), + parser -> SnapshotLifecycleMetadata.PARSER.parse(parser, null) + ), + // Lifecycle Types + new NamedXContentRegistry.Entry( + LifecycleType.class, + new ParseField(TimeseriesLifecycleType.TYPE), + (p, c) -> TimeseriesLifecycleType.INSTANCE + ), + // Lifecycle Actions + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(AllocateAction.NAME), AllocateAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ForceMergeAction.NAME), ForceMergeAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ReadOnlyAction.NAME), ReadOnlyAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(RolloverAction.NAME), RolloverAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ShrinkAction.NAME), ShrinkAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(FreezeAction.NAME), FreezeAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(SetPriorityAction.NAME), SetPriorityAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(UnfollowAction.NAME), UnfollowAction::parse), + new NamedXContentRegistry.Entry( + LifecycleAction.class, + new ParseField(WaitForSnapshotAction.NAME), + WaitForSnapshotAction::parse + ), + new NamedXContentRegistry.Entry( + LifecycleAction.class, + new ParseField(SearchableSnapshotAction.NAME), + SearchableSnapshotAction::parse + ), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(MigrateAction.NAME), MigrateAction::parse) + ) + ); if (RollupV2.isEnabled()) { - entries.add(new NamedXContentRegistry.Entry(LifecycleAction.class, - new ParseField(RollupILMAction.NAME), RollupILMAction::parse)); + entries.add( + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(RollupILMAction.NAME), RollupILMAction::parse) + ); } return entries; } @Override - public List getRestHandlers(Settings settings, RestController restController, ClusterSettings clusterSettings, - IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter, IndexNameExpressionResolver indexNameExpressionResolver, - Supplier nodesInCluster) { + public List getRestHandlers( + Settings settings, + RestController restController, + ClusterSettings clusterSettings, + IndexScopedSettings indexScopedSettings, + SettingsFilter settingsFilter, + IndexNameExpressionResolver indexNameExpressionResolver, + Supplier nodesInCluster + ) { List handlers = new ArrayList<>(); - handlers.addAll(Arrays.asList( - // add ILM rest handlers - new RestPutLifecycleAction(), - new RestGetLifecycleAction(), - new RestDeleteLifecycleAction(), - new RestExplainLifecycleAction(), - new RestRemoveIndexLifecyclePolicyAction(), - new RestMoveToStepAction(), - new RestRetryAction(), - new RestStopAction(), - new RestStartILMAction(), - new RestGetStatusAction(), - new RestMigrateToDataTiersAction(), + handlers.addAll( + Arrays.asList( + // add ILM rest handlers + new RestPutLifecycleAction(), + new RestGetLifecycleAction(), + new RestDeleteLifecycleAction(), + new RestExplainLifecycleAction(), + new RestRemoveIndexLifecyclePolicyAction(), + new RestMoveToStepAction(), + new RestRetryAction(), + new RestStopAction(), + new RestStartILMAction(), + new RestGetStatusAction(), + new RestMigrateToDataTiersAction(), - // add SLM rest headers - new RestPutSnapshotLifecycleAction(), - new RestDeleteSnapshotLifecycleAction(), - new RestGetSnapshotLifecycleAction(), - new RestExecuteSnapshotLifecycleAction(), - new RestGetSnapshotLifecycleStatsAction(), - new RestExecuteSnapshotRetentionAction(), - new RestStopSLMAction(), - new RestStartSLMAction(), - new RestGetSLMStatusAction() - )); + // add SLM rest headers + new RestPutSnapshotLifecycleAction(), + new RestDeleteSnapshotLifecycleAction(), + new RestGetSnapshotLifecycleAction(), + new RestExecuteSnapshotLifecycleAction(), + new RestGetSnapshotLifecycleStatsAction(), + new RestExecuteSnapshotRetentionAction(), + new RestStopSLMAction(), + new RestStartSLMAction(), + new RestGetSLMStatusAction() + ) + ); return handlers; } @@ -312,30 +379,32 @@ public List getRestHandlers(Settings settings, RestController restC actions.add(slmUsageAction); actions.add(slmInfoAction); actions.add(migrateToDataTiersAction); - actions.addAll(Arrays.asList( - // add ILM actions - new ActionHandler<>(PutLifecycleAction.INSTANCE, TransportPutLifecycleAction.class), - new ActionHandler<>(GetLifecycleAction.INSTANCE, TransportGetLifecycleAction.class), - new ActionHandler<>(DeleteLifecycleAction.INSTANCE, TransportDeleteLifecycleAction.class), - new ActionHandler<>(ExplainLifecycleAction.INSTANCE, TransportExplainLifecycleAction.class), - new ActionHandler<>(RemoveIndexLifecyclePolicyAction.INSTANCE, TransportRemoveIndexLifecyclePolicyAction.class), - new ActionHandler<>(MoveToStepAction.INSTANCE, TransportMoveToStepAction.class), - new ActionHandler<>(RetryAction.INSTANCE, TransportRetryAction.class), - new ActionHandler<>(StartILMAction.INSTANCE, TransportStartILMAction.class), - new ActionHandler<>(StopILMAction.INSTANCE, TransportStopILMAction.class), - new ActionHandler<>(GetStatusAction.INSTANCE, TransportGetStatusAction.class), + actions.addAll( + Arrays.asList( + // add ILM actions + new ActionHandler<>(PutLifecycleAction.INSTANCE, TransportPutLifecycleAction.class), + new ActionHandler<>(GetLifecycleAction.INSTANCE, TransportGetLifecycleAction.class), + new ActionHandler<>(DeleteLifecycleAction.INSTANCE, TransportDeleteLifecycleAction.class), + new ActionHandler<>(ExplainLifecycleAction.INSTANCE, TransportExplainLifecycleAction.class), + new ActionHandler<>(RemoveIndexLifecyclePolicyAction.INSTANCE, TransportRemoveIndexLifecyclePolicyAction.class), + new ActionHandler<>(MoveToStepAction.INSTANCE, TransportMoveToStepAction.class), + new ActionHandler<>(RetryAction.INSTANCE, TransportRetryAction.class), + new ActionHandler<>(StartILMAction.INSTANCE, TransportStartILMAction.class), + new ActionHandler<>(StopILMAction.INSTANCE, TransportStopILMAction.class), + new ActionHandler<>(GetStatusAction.INSTANCE, TransportGetStatusAction.class), - // add SLM actions - new ActionHandler<>(PutSnapshotLifecycleAction.INSTANCE, TransportPutSnapshotLifecycleAction.class), - new ActionHandler<>(DeleteSnapshotLifecycleAction.INSTANCE, TransportDeleteSnapshotLifecycleAction.class), - new ActionHandler<>(GetSnapshotLifecycleAction.INSTANCE, TransportGetSnapshotLifecycleAction.class), - new ActionHandler<>(ExecuteSnapshotLifecycleAction.INSTANCE, TransportExecuteSnapshotLifecycleAction.class), - new ActionHandler<>(GetSnapshotLifecycleStatsAction.INSTANCE, TransportGetSnapshotLifecycleStatsAction.class), - new ActionHandler<>(ExecuteSnapshotRetentionAction.INSTANCE, TransportExecuteSnapshotRetentionAction.class), - new ActionHandler<>(StartSLMAction.INSTANCE, TransportStartSLMAction.class), - new ActionHandler<>(StopSLMAction.INSTANCE, TransportStopSLMAction.class), - new ActionHandler<>(GetSLMStatusAction.INSTANCE, TransportGetSLMStatusAction.class) - )); + // add SLM actions + new ActionHandler<>(PutSnapshotLifecycleAction.INSTANCE, TransportPutSnapshotLifecycleAction.class), + new ActionHandler<>(DeleteSnapshotLifecycleAction.INSTANCE, TransportDeleteSnapshotLifecycleAction.class), + new ActionHandler<>(GetSnapshotLifecycleAction.INSTANCE, TransportGetSnapshotLifecycleAction.class), + new ActionHandler<>(ExecuteSnapshotLifecycleAction.INSTANCE, TransportExecuteSnapshotLifecycleAction.class), + new ActionHandler<>(GetSnapshotLifecycleStatsAction.INSTANCE, TransportGetSnapshotLifecycleStatsAction.class), + new ActionHandler<>(ExecuteSnapshotRetentionAction.INSTANCE, TransportExecuteSnapshotRetentionAction.class), + new ActionHandler<>(StartSLMAction.INSTANCE, TransportStartSLMAction.class), + new ActionHandler<>(StopSLMAction.INSTANCE, TransportStopSLMAction.class), + new ActionHandler<>(GetSLMStatusAction.INSTANCE, TransportGetSLMStatusAction.class) + ) + ); return actions; } @@ -348,8 +417,12 @@ public void onIndexModule(IndexModule indexModule) { @Override public void close() { try { - IOUtils.close(indexLifecycleInitialisationService.get(), ilmHistoryStore.get(), - snapshotLifecycleService.get(), snapshotRetentionService.get()); + IOUtils.close( + indexLifecycleInitialisationService.get(), + ilmHistoryStore.get(), + snapshotLifecycleService.get(), + snapshotRetentionService.get() + ); } catch (IOException e) { throw new ElasticsearchException("unable to close index lifecycle services", e); } diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycleClusterStateUpdateTask.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycleClusterStateUpdateTask.java index 98e66b4706344..8cb2cde8b6ec9 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycleClusterStateUpdateTask.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycleClusterStateUpdateTask.java @@ -82,8 +82,7 @@ public final void addListener(ActionListener listener) { * implementation was a noop and returned the input cluster state, then this method will not be invoked. It is therefore guaranteed * that {@code oldState} is always different from {@code newState}. */ - protected void onClusterStateProcessed(String source, ClusterState oldState, ClusterState newState) { - } + protected void onClusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {} @Override public abstract boolean equals(Object other); diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycleRunner.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycleRunner.java index e3fff06817d5b..d821b4bfb1c11 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycleRunner.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycleRunner.java @@ -19,12 +19,12 @@ import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.Priority; -import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; import org.elasticsearch.core.Tuple; import org.elasticsearch.index.Index; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xpack.core.ilm.AsyncActionStep; import org.elasticsearch.xpack.core.ilm.AsyncWaitStep; import org.elasticsearch.xpack.core.ilm.ClusterStateActionStep; @@ -57,7 +57,7 @@ class IndexLifecycleRunner { private static final ClusterStateTaskExecutor ILM_TASK_EXECUTOR = (currentState, tasks) -> { ClusterStateTaskExecutor.ClusterTasksResult.Builder builder = - ClusterStateTaskExecutor.ClusterTasksResult.builder(); + ClusterStateTaskExecutor.ClusterTasksResult.builder(); ClusterState state = currentState; for (IndexLifecycleClusterStateUpdateTask task : tasks) { try { @@ -72,8 +72,13 @@ class IndexLifecycleRunner { return builder.build(state); }; - IndexLifecycleRunner(PolicyStepsRegistry stepRegistry, ILMHistoryStore ilmHistoryStore, ClusterService clusterService, - ThreadPool threadPool, LongSupplier nowSupplier) { + IndexLifecycleRunner( + PolicyStepsRegistry stepRegistry, + ILMHistoryStore ilmHistoryStore, + ClusterService clusterService, + ThreadPool threadPool, + LongSupplier nowSupplier + ) { this.stepRegistry = stepRegistry; this.ilmHistoryStore = ilmHistoryStore; this.clusterService = clusterService; @@ -89,10 +94,12 @@ static Step getCurrentStep(PolicyStepsRegistry stepRegistry, String policy, Inde return getCurrentStep(stepRegistry, policy, indexMetadata, lifecycleState); } - static Step getCurrentStep(PolicyStepsRegistry stepRegistry, - String policy, - IndexMetadata indexMetadata, - LifecycleExecutionState lifecycleState) { + static Step getCurrentStep( + PolicyStepsRegistry stepRegistry, + String policy, + IndexMetadata indexMetadata, + LifecycleExecutionState lifecycleState + ) { StepKey currentStepKey = LifecycleExecutionState.getCurrentStepKey(lifecycleState); logger.trace("[{}] retrieved current step key: {}", indexMetadata.getIndex().getName(), currentStepKey); if (currentStepKey == null) { @@ -138,12 +145,18 @@ boolean isReadyToTransitionToThisPhase(final String policy, final IndexMetadata age = new TimeValue(-ageMillis); } if (logger.isTraceEnabled()) { - logger.trace("[{}] checking for index age to be at least [{}] before performing actions in " + - "the \"{}\" phase. Now: {}, lifecycle date: {}, age: [{}{}/{}s]", - indexMetadata.getIndex().getName(), after, phase, + logger.trace( + "[{}] checking for index age to be at least [{}] before performing actions in " + + "the \"{}\" phase. Now: {}, lifecycle date: {}, age: [{}{}/{}s]", + indexMetadata.getIndex().getName(), + after, + phase, new TimeValue(now).seconds(), new TimeValue(lifecycleDate).seconds(), - ageMillis < 0 ? "-" : "", age, age.seconds()); + ageMillis < 0 ? "-" : "", + age, + age.seconds() + ); } return now >= lifecycleDate + after.getMillis(); } @@ -175,8 +188,7 @@ void runPeriodicStep(String policy, Metadata metadata, IndexMetadata indexMetada // for now return; } - logger.error("current step [{}] for index [{}] with policy [{}] is not recognized", - currentStepKey, index, policy); + logger.error("current step [{}] for index [{}] with policy [{}] is not recognized", currentStepKey, index, policy); return; } } @@ -189,13 +201,20 @@ void runPeriodicStep(String policy, Metadata metadata, IndexMetadata indexMetada return; } - logger.trace("[{}] maybe running periodic step ({}) with current step {}", - index, currentStep.getClass().getSimpleName(), currentStep.getKey()); + logger.trace( + "[{}] maybe running periodic step ({}) with current step {}", + index, + currentStep.getClass().getSimpleName(), + currentStep.getKey() + ); // Only phase changing and async wait steps should be run through periodic polling if (currentStep instanceof PhaseCompleteStep) { if (currentStep.getNextStepKey() == null) { - logger.debug("[{}] stopping in the current phase ({}) as there are no more steps in the policy", - index, currentStep.getKey().getPhase()); + logger.debug( + "[{}] stopping in the current phase ({}) as there are no more steps in the policy", + index, + currentStep.getKey().getPhase() + ); return; } // Only proceed to the next step if enough time has elapsed to go into the next phase @@ -234,35 +253,64 @@ public void onFailure(Exception e) { void onErrorMaybeRetryFailedStep(String policy, IndexMetadata indexMetadata) { String index = indexMetadata.getIndex().getName(); LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(indexMetadata); - Step failedStep = stepRegistry.getStep(indexMetadata, new StepKey(lifecycleState.getPhase(), lifecycleState.getAction(), - lifecycleState.getFailedStep())); + Step failedStep = stepRegistry.getStep( + indexMetadata, + new StepKey(lifecycleState.getPhase(), lifecycleState.getAction(), lifecycleState.getFailedStep()) + ); if (failedStep == null) { - logger.warn("failed step [{}] for index [{}] is not part of policy [{}] anymore, or it is invalid. skipping execution", - lifecycleState.getFailedStep(), index, policy); + logger.warn( + "failed step [{}] for index [{}] is not part of policy [{}] anymore, or it is invalid. skipping execution", + lifecycleState.getFailedStep(), + index, + policy + ); return; } if (lifecycleState.isAutoRetryableError() != null && lifecycleState.isAutoRetryableError()) { int currentRetryAttempt = lifecycleState.getFailedStepRetryCount() == null ? 1 : 1 + lifecycleState.getFailedStepRetryCount(); - logger.info("policy [{}] for index [{}] on an error step due to a transient error, moving back to the failed " + - "step [{}] for execution. retry attempt [{}]", policy, index, lifecycleState.getFailedStep(), currentRetryAttempt); + logger.info( + "policy [{}] for index [{}] on an error step due to a transient error, moving back to the failed " + + "step [{}] for execution. retry attempt [{}]", + policy, + index, + lifecycleState.getFailedStep(), + currentRetryAttempt + ); // we can afford to drop these requests if they timeout as on the next {@link // IndexLifecycleRunner#runPeriodicStep} run the policy will still be in the ERROR step, as we haven't been able // to move it back into the failed step, so we'll try again clusterService.submitStateUpdateTask( - String.format(Locale.ROOT, "ilm-retry-failed-step {policy [%s], index [%s], failedStep [%s]}", policy, index, - failedStep.getKey()), new ClusterStateUpdateTask(TimeValue.MAX_VALUE) { + String.format( + Locale.ROOT, + "ilm-retry-failed-step {policy [%s], index [%s], failedStep [%s]}", + policy, + index, + failedStep.getKey() + ), + new ClusterStateUpdateTask(TimeValue.MAX_VALUE) { @Override public ClusterState execute(ClusterState currentState) { - return IndexLifecycleTransition.moveClusterStateToPreviouslyFailedStep(currentState, index, - nowSupplier, stepRegistry, true); + return IndexLifecycleTransition.moveClusterStateToPreviouslyFailedStep( + currentState, + index, + nowSupplier, + stepRegistry, + true + ); } @Override public void onFailure(String source, Exception e) { - logger.error(new ParameterizedMessage("retry execution of step [{}] for index [{}] failed", - failedStep.getKey().getName(), index), e); + logger.error( + new ParameterizedMessage( + "retry execution of step [{}] for index [{}] failed", + failedStep.getKey().getName(), + index + ), + e + ); } @Override @@ -272,13 +320,19 @@ public void clusterStateProcessed(String source, ClusterState oldState, ClusterS Step indexMetaCurrentStep = getCurrentStep(stepRegistry, policy, newIndexMeta); StepKey stepKey = indexMetaCurrentStep.getKey(); if (stepKey != null && stepKey != TerminalPolicyStep.KEY && newIndexMeta != null) { - logger.trace("policy [{}] for index [{}] was moved back on the failed step for as part of an automatic " + - "retry. Attempting to execute the failed step [{}] if it's an async action", policy, index, stepKey); + logger.trace( + "policy [{}] for index [{}] was moved back on the failed step for as part of an automatic " + + "retry. Attempting to execute the failed step [{}] if it's an async action", + policy, + index, + stepKey + ); maybeRunAsyncAction(newState, newIndexMeta, policy, stepKey); } } } - }); + } + ); } else { logger.debug("policy [{}] for index [{}] on an error step after a terminal error, skipping execution", policy, index); } @@ -305,21 +359,35 @@ void maybeRunAsyncAction(ClusterState currentState, IndexMetadata indexMetadata, // for now return; } - logger.warn("current step [{}] for index [{}] with policy [{}] is not recognized", - currentStepKey, index, policy); + logger.warn("current step [{}] for index [{}] with policy [{}] is not recognized", currentStepKey, index, policy); return; } - logger.trace("[{}] maybe running async action step ({}) with current step {}", - index, currentStep.getClass().getSimpleName(), currentStep.getKey()); + logger.trace( + "[{}] maybe running async action step ({}) with current step {}", + index, + currentStep.getClass().getSimpleName(), + currentStep.getKey() + ); if (currentStep.getKey().equals(expectedStepKey) == false) { - throw new IllegalStateException("expected index [" + indexMetadata.getIndex().getName() + "] with policy [" + policy + - "] to have current step consistent with provided step key (" + expectedStepKey + ") but it was " + currentStep.getKey()); + throw new IllegalStateException( + "expected index [" + + indexMetadata.getIndex().getName() + + "] with policy [" + + policy + + "] to have current step consistent with provided step key (" + + expectedStepKey + + ") but it was " + + currentStep.getKey() + ); } if (currentStep instanceof AsyncActionStep) { logger.debug("[{}] running policy with async action step [{}]", index, currentStep.getKey()); - ((AsyncActionStep) currentStep).performAction(indexMetadata, currentState, - new ClusterStateObserver(clusterService, null, logger, threadPool.getThreadContext()), new ActionListener<>() { + ((AsyncActionStep) currentStep).performAction( + indexMetadata, + currentState, + new ClusterStateObserver(clusterService, null, logger, threadPool.getThreadContext()), + new ActionListener<>() { @Override public void onResponse(Void unused) { @@ -338,7 +406,8 @@ public void onResponse(Void unused) { public void onFailure(Exception e) { moveToErrorStep(indexMetadata.getIndex(), policy, currentStep.getKey(), e); } - }); + } + ); } else { logger.trace("[{}] ignoring non async action step execution from step transition [{}]", index, currentStep.getKey()); } @@ -374,8 +443,7 @@ void runPolicyAfterStateChange(String policy, IndexMetadata indexMetadata) { // for now return; } - logger.error("current step [{}] for index [{}] with policy [{}] is not recognized", - currentStepKey, index, policy); + logger.error("current step [{}] for index [{}] with policy [{}] is not recognized", currentStepKey, index, policy); return; } } @@ -388,12 +456,19 @@ void runPolicyAfterStateChange(String policy, IndexMetadata indexMetadata) { return; } - logger.trace("[{}] maybe running step ({}) after state change: {}", - index, currentStep.getClass().getSimpleName(), currentStep.getKey()); + logger.trace( + "[{}] maybe running step ({}) after state change: {}", + index, + currentStep.getClass().getSimpleName(), + currentStep.getKey() + ); if (currentStep instanceof PhaseCompleteStep) { if (currentStep.getNextStepKey() == null) { - logger.debug("[{}] stopping in the current phase ({}) as there are no more steps in the policy", - index, currentStep.getKey().getPhase()); + logger.debug( + "[{}] stopping in the current phase ({}) as there are no more steps in the policy", + index, + currentStep.getKey().getPhase() + ); return; } // Only proceed to the next step if enough time has elapsed to go into the next phase @@ -402,8 +477,10 @@ void runPolicyAfterStateChange(String policy, IndexMetadata indexMetadata) { } } else if (currentStep instanceof ClusterStateActionStep || currentStep instanceof ClusterStateWaitStep) { logger.debug("[{}] running policy with current-step [{}]", indexMetadata.getIndex().getName(), currentStep.getKey()); - submitUnlessAlreadyQueued(String.format(Locale.ROOT, "ilm-execute-cluster-state-steps [%s]", currentStep), - new ExecuteStepsUpdateTask(policy, indexMetadata.getIndex(), currentStep, stepRegistry, this, nowSupplier)); + submitUnlessAlreadyQueued( + String.format(Locale.ROOT, "ilm-execute-cluster-state-steps [%s]", currentStep), + new ExecuteStepsUpdateTask(policy, indexMetadata.getIndex(), currentStep, stepRegistry, this, nowSupplier) + ); } else { logger.trace("[{}] ignoring step execution from cluster state change event [{}]", index, currentStep.getKey()); } @@ -416,31 +493,50 @@ void runPolicyAfterStateChange(String policy, IndexMetadata indexMetadata) { private void moveToStep(Index index, String policy, Step.StepKey currentStepKey, Step.StepKey newStepKey) { logger.debug("[{}] moving to step [{}] {} -> {}", index.getName(), policy, currentStepKey, newStepKey); submitUnlessAlreadyQueued( - String.format(Locale.ROOT, "ilm-move-to-step {policy [%s], index [%s], currentStep [%s], nextStep [%s]}", policy, - index.getName(), currentStepKey, newStepKey), - new MoveToNextStepUpdateTask(index, policy, currentStepKey, newStepKey, nowSupplier, stepRegistry, clusterState -> - { + String.format( + Locale.ROOT, + "ilm-move-to-step {policy [%s], index [%s], currentStep [%s], nextStep [%s]}", + policy, + index.getName(), + currentStepKey, + newStepKey + ), + new MoveToNextStepUpdateTask(index, policy, currentStepKey, newStepKey, nowSupplier, stepRegistry, clusterState -> { IndexMetadata indexMetadata = clusterState.metadata().index(index); registerSuccessfulOperation(indexMetadata); if (newStepKey != null && newStepKey != TerminalPolicyStep.KEY && indexMetadata != null) { maybeRunAsyncAction(clusterState, indexMetadata, policy, newStepKey); } - })); + }) + ); } /** * Move the index to the ERROR step. */ private void moveToErrorStep(Index index, String policy, Step.StepKey currentStepKey, Exception e) { - logger.error(new ParameterizedMessage("policy [{}] for index [{}] failed on step [{}]. Moving to ERROR step", - policy, index.getName(), currentStepKey), e); + logger.error( + new ParameterizedMessage( + "policy [{}] for index [{}] failed on step [{}]. Moving to ERROR step", + policy, + index.getName(), + currentStepKey + ), + e + ); clusterService.submitStateUpdateTask( - String.format(Locale.ROOT, "ilm-move-to-error-step {policy [%s], index [%s], currentStep [%s]}", policy, index.getName(), - currentStepKey), + String.format( + Locale.ROOT, + "ilm-move-to-error-step {policy [%s], index [%s], currentStep [%s]}", + policy, + index.getName(), + currentStepKey + ), new MoveToErrorStepUpdateTask(index, policy, currentStepKey, e, nowSupplier, stepRegistry::getStep, clusterState -> { IndexMetadata indexMetadata = clusterState.metadata().index(index); registerFailedOperation(indexMetadata, e); - })); + }) + ); } /** @@ -449,17 +545,27 @@ private void moveToErrorStep(Index index, String policy, Step.StepKey currentSte */ private void setStepInfo(Index index, String policy, @Nullable Step.StepKey currentStepKey, ToXContentObject stepInfo) { submitUnlessAlreadyQueued( - String.format(Locale.ROOT, "ilm-set-step-info {policy [%s], index [%s], currentStep [%s]}", policy, index.getName(), - currentStepKey), - new SetStepInfoUpdateTask(index, policy, currentStepKey, stepInfo)); + String.format( + Locale.ROOT, + "ilm-set-step-info {policy [%s], index [%s], currentStep [%s]}", + policy, + index.getName(), + currentStepKey + ), + new SetStepInfoUpdateTask(index, policy, currentStepKey, stepInfo) + ); } /** * Mark the index with step info explaining that the policy doesn't exist. */ private void markPolicyDoesNotExist(String policyName, Index index, LifecycleExecutionState executionState) { - markPolicyRetrievalError(policyName, index, executionState, - new IllegalArgumentException("policy [" + policyName + "] does not exist")); + markPolicyRetrievalError( + policyName, + index, + executionState, + new IllegalArgumentException("policy [" + policyName + "] does not exist") + ); } /** @@ -470,10 +576,19 @@ private void markPolicyDoesNotExist(String policyName, Index index, LifecycleExe */ private void markPolicyRetrievalError(String policyName, Index index, LifecycleExecutionState executionState, Exception e) { logger.debug( - new ParameterizedMessage("unable to retrieve policy [{}] for index [{}], recording this in step_info for this index", - policyName, index.getName()), e); - setStepInfo(index, policyName, LifecycleExecutionState.getCurrentStepKey(executionState), - new SetStepInfoUpdateTask.ExceptionWrapper(e)); + new ParameterizedMessage( + "unable to retrieve policy [{}] for index [{}], recording this in step_info for this index", + policyName, + index.getName() + ), + e + ); + setStepInfo( + index, + policyName, + LifecycleExecutionState.getCurrentStepKey(executionState), + new SetStepInfoUpdateTask.ExceptionWrapper(e) + ); } /** @@ -487,11 +602,14 @@ void registerSuccessfulOperation(IndexMetadata indexMetadata) { } Long origination = calculateOriginationMillis(indexMetadata); ilmHistoryStore.putAsync( - ILMHistoryItem.success(indexMetadata.getIndex().getName(), + ILMHistoryItem.success( + indexMetadata.getIndex().getName(), LifecycleSettings.LIFECYCLE_NAME_SETTING.get(indexMetadata.getSettings()), nowSupplier.getAsLong(), origination == null ? null : (nowSupplier.getAsLong() - origination), - LifecycleExecutionState.fromIndexMetadata(indexMetadata))); + LifecycleExecutionState.fromIndexMetadata(indexMetadata) + ) + ); } /** @@ -504,14 +622,17 @@ void registerDeleteOperation(IndexMetadata metadataBeforeDeletion) { } Long origination = calculateOriginationMillis(metadataBeforeDeletion); ilmHistoryStore.putAsync( - ILMHistoryItem.success(metadataBeforeDeletion.getIndex().getName(), + ILMHistoryItem.success( + metadataBeforeDeletion.getIndex().getName(), LifecycleSettings.LIFECYCLE_NAME_SETTING.get(metadataBeforeDeletion.getSettings()), nowSupplier.getAsLong(), origination == null ? null : (nowSupplier.getAsLong() - origination), LifecycleExecutionState.builder(LifecycleExecutionState.fromIndexMetadata(metadataBeforeDeletion)) // Register that the delete phase is now "complete" .setStep(PhaseCompleteStep.NAME) - .build())); + .build() + ) + ); } /** @@ -525,12 +646,15 @@ void registerFailedOperation(IndexMetadata indexMetadata, Exception failure) { } Long origination = calculateOriginationMillis(indexMetadata); ilmHistoryStore.putAsync( - ILMHistoryItem.failure(indexMetadata.getIndex().getName(), + ILMHistoryItem.failure( + indexMetadata.getIndex().getName(), LifecycleSettings.LIFECYCLE_NAME_SETTING.get(indexMetadata.getSettings()), nowSupplier.getAsLong(), origination == null ? null : (nowSupplier.getAsLong() - origination), LifecycleExecutionState.fromIndexMetadata(indexMetadata), - failure)); + failure + ) + ); } private final Set executingTasks = Collections.synchronizedSet(new HashSet<>()); diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycleService.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycleService.java index cbd5bb852b6cc..9d5c19fcb0af7 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycleService.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycleService.java @@ -22,7 +22,6 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.component.Lifecycle.State; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; import org.elasticsearch.gateway.GatewayService; @@ -32,6 +31,7 @@ import org.elasticsearch.plugins.ShutdownAwarePlugin; import org.elasticsearch.shutdown.PluginShutdownService; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.XPackField; import org.elasticsearch.xpack.core.ilm.CheckShrinkReadyStep; import org.elasticsearch.xpack.core.ilm.IndexLifecycleMetadata; @@ -64,7 +64,13 @@ * A service which runs the {@link LifecyclePolicy}s associated with indexes. */ public class IndexLifecycleService - implements ClusterStateListener, ClusterStateApplier, SchedulerEngine.Listener, Closeable, IndexEventListener, ShutdownAwarePlugin { + implements + ClusterStateListener, + ClusterStateApplier, + SchedulerEngine.Listener, + Closeable, + IndexEventListener, + ShutdownAwarePlugin { private static final Logger logger = LogManager.getLogger(IndexLifecycleService.class); private static final Set IGNORE_STEPS_MAINTENANCE_REQUESTED = Set.of(ShrinkStep.NAME, RollupStep.NAME); private volatile boolean isMaster = false; @@ -79,9 +85,17 @@ public class IndexLifecycleService private final LongSupplier nowSupplier; private SchedulerEngine.Job scheduledJob; - public IndexLifecycleService(Settings settings, Client client, ClusterService clusterService, ThreadPool threadPool, Clock clock, - LongSupplier nowSupplier, NamedXContentRegistry xContentRegistry, - ILMHistoryStore ilmHistoryStore, XPackLicenseState licenseState) { + public IndexLifecycleService( + Settings settings, + Client client, + ClusterService clusterService, + ThreadPool threadPool, + Clock clock, + LongSupplier nowSupplier, + NamedXContentRegistry xContentRegistry, + ILMHistoryStore ilmHistoryStore, + XPackLicenseState licenseState + ) { super(); this.settings = settings; this.clusterService = clusterService; @@ -93,8 +107,8 @@ public IndexLifecycleService(Settings settings, Client client, ClusterService cl this.pollInterval = LifecycleSettings.LIFECYCLE_POLL_INTERVAL_SETTING.get(settings); clusterService.addStateApplier(this); clusterService.addListener(this); - clusterService.getClusterSettings().addSettingsUpdateConsumer(LifecycleSettings.LIFECYCLE_POLL_INTERVAL_SETTING, - this::updatePollInterval); + clusterService.getClusterSettings() + .addSettingsUpdateConsumer(LifecycleSettings.LIFECYCLE_POLL_INTERVAL_SETTING, this::updatePollInterval); } public void maybeRunAsyncAction(ClusterState clusterState, IndexMetadata indexMetadata, StepKey nextStepKey) { @@ -114,8 +128,8 @@ public StepKey resolveStepKey(ClusterState state, Index index, String phase, @Nu return this.policyRegistry.getFirstStepForPhaseAndAction(state, index, phase, action); } } else { - assert action != null : - "action should never be null because we don't allow constructing a partial step key with only a phase and name"; + assert action != null + : "action should never be null because we don't allow constructing a partial step key with only a phase and name"; return new StepKey(phase, action, name); } } @@ -134,17 +148,14 @@ public ClusterState moveClusterStateToStep(ClusterState currentState, Index inde // when moving to an arbitrary step key (to avoid race conditions between the // check-and-set). moveClusterStateToStep also does its own validation, but doesn't take // the user-input for the current step (which is why we validate here for a passed in step) - IndexLifecycleTransition.validateTransition(currentState.getMetadata().index(index), - currentStepKey, newStepKey, policyRegistry); - return IndexLifecycleTransition.moveClusterStateToStep(index, currentState, newStepKey, - nowSupplier, policyRegistry, true); + IndexLifecycleTransition.validateTransition(currentState.getMetadata().index(index), currentStepKey, newStepKey, policyRegistry); + return IndexLifecycleTransition.moveClusterStateToStep(index, currentState, newStepKey, nowSupplier, policyRegistry, true); } public ClusterState moveClusterStateToPreviouslyFailedStep(ClusterState currentState, String[] indices) { ClusterState newState = currentState; for (String index : indices) { - newState = IndexLifecycleTransition.moveClusterStateToPreviouslyFailedStep(newState, index, - nowSupplier, policyRegistry, false); + newState = IndexLifecycleTransition.moveClusterStateToPreviouslyFailedStep(newState, index, nowSupplier, policyRegistry, false); } return newState; } @@ -173,28 +184,50 @@ void onMaster(ClusterState clusterState) { try { if (OperationMode.STOPPING == currentMode) { if (stepKey != null && IGNORE_STEPS_MAINTENANCE_REQUESTED.contains(stepKey.getName())) { - logger.info("waiting to stop ILM because index [{}] with policy [{}] is currently in step [{}]", - idxMeta.getIndex().getName(), policyName, stepKey.getName()); + logger.info( + "waiting to stop ILM because index [{}] with policy [{}] is currently in step [{}]", + idxMeta.getIndex().getName(), + policyName, + stepKey.getName() + ); lifecycleRunner.maybeRunAsyncAction(clusterState, idxMeta, policyName, stepKey); // ILM is trying to stop, but this index is in a Shrink step (or other dangerous step) so we can't stop safeToStop = false; } else { - logger.info("skipping policy execution of step [{}] for index [{}] with policy [{}]" + - " because ILM is stopping", - stepKey == null ? "n/a" : stepKey.getName(), idxMeta.getIndex().getName(), policyName); + logger.info( + "skipping policy execution of step [{}] for index [{}] with policy [{}]" + " because ILM is stopping", + stepKey == null ? "n/a" : stepKey.getName(), + idxMeta.getIndex().getName(), + policyName + ); } } else { lifecycleRunner.maybeRunAsyncAction(clusterState, idxMeta, policyName, stepKey); } } catch (Exception e) { if (logger.isTraceEnabled()) { - logger.warn(new ParameterizedMessage("async action execution failed during master election trigger" + - " for index [{}] with policy [{}] in step [{}], lifecycle state: [{}]", - idxMeta.getIndex().getName(), policyName, stepKey, lifecycleState.asMap()), e); + logger.warn( + new ParameterizedMessage( + "async action execution failed during master election trigger" + + " for index [{}] with policy [{}] in step [{}], lifecycle state: [{}]", + idxMeta.getIndex().getName(), + policyName, + stepKey, + lifecycleState.asMap() + ), + e + ); } else { - logger.warn(new ParameterizedMessage("async action execution failed during master election trigger" + - " for index [{}] with policy [{}] in step [{}]", - idxMeta.getIndex().getName(), policyName, stepKey), e); + logger.warn( + new ParameterizedMessage( + "async action execution failed during master election trigger" + + " for index [{}] with policy [{}] in step [{}]", + idxMeta.getIndex().getName(), + policyName, + stepKey + ), + e + ); } // Don't rethrow the exception, we don't want a failure for one index to be @@ -281,7 +314,8 @@ public void applyClusterState(ClusterChangedEvent event) { // keep idle until elected final IndexLifecycleMetadata ilmMetadata = event.state().metadata().custom(IndexLifecycleMetadata.TYPE); // only update the policy registry if we just became the master node or if the ilm metadata changed - if (ilmMetadata != null && (event.previousState().nodes().isLocalNodeElectedMaster() == false + if (ilmMetadata != null + && (event.previousState().nodes().isLocalNodeElectedMaster() == false || ilmMetadata != event.previousState().metadata().custom(IndexLifecycleMetadata.TYPE))) { policyRegistry.update(ilmMetadata); } @@ -343,8 +377,12 @@ void triggerPolicies(ClusterState clusterState, boolean fromClusterStateChange) try { if (OperationMode.STOPPING == currentMode) { if (stepKey != null && IGNORE_STEPS_MAINTENANCE_REQUESTED.contains(stepKey.getName())) { - logger.info("waiting to stop ILM because index [{}] with policy [{}] is currently in step [{}]", - idxMeta.getIndex().getName(), policyName, stepKey.getName()); + logger.info( + "waiting to stop ILM because index [{}] with policy [{}] is currently in step [{}]", + idxMeta.getIndex().getName(), + policyName, + stepKey.getName() + ); if (fromClusterStateChange) { lifecycleRunner.runPolicyAfterStateChange(policyName, idxMeta); } else { @@ -353,8 +391,12 @@ void triggerPolicies(ClusterState clusterState, boolean fromClusterStateChange) // ILM is trying to stop, but this index is in a Shrink step (or other dangerous step) so we can't stop safeToStop = false; } else { - logger.info("skipping policy execution of step [{}] for index [{}] with policy [{}] because ILM is stopping", - stepKey == null ? "n/a" : stepKey.getName(), idxMeta.getIndex().getName(), policyName); + logger.info( + "skipping policy execution of step [{}] for index [{}] with policy [{}] because ILM is stopping", + stepKey == null ? "n/a" : stepKey.getName(), + idxMeta.getIndex().getName(), + policyName + ); } } else { if (fromClusterStateChange) { @@ -365,13 +407,27 @@ void triggerPolicies(ClusterState clusterState, boolean fromClusterStateChange) } } catch (Exception e) { if (logger.isTraceEnabled()) { - logger.warn(new ParameterizedMessage("async action execution failed during policy trigger" + - " for index [{}] with policy [{}] in step [{}], lifecycle state: [{}]", - idxMeta.getIndex().getName(), policyName, stepKey, lifecycleState.asMap()), e); + logger.warn( + new ParameterizedMessage( + "async action execution failed during policy trigger" + + " for index [{}] with policy [{}] in step [{}], lifecycle state: [{}]", + idxMeta.getIndex().getName(), + policyName, + stepKey, + lifecycleState.asMap() + ), + e + ); } else { - logger.warn(new ParameterizedMessage("async action execution failed during policy trigger" + - " for index [{}] with policy [{}] in step [{}]", - idxMeta.getIndex().getName(), policyName, stepKey), e); + logger.warn( + new ParameterizedMessage( + "async action execution failed during policy trigger" + " for index [{}] with policy [{}] in step [{}]", + idxMeta.getIndex().getName(), + policyName, + stepKey + ), + e + ); } // Don't rethrow the exception, we don't want a failure for one index to be @@ -389,8 +445,8 @@ void triggerPolicies(ClusterState clusterState, boolean fromClusterStateChange) public synchronized void close() { // this assertion is here to ensure that the check we use in maybeScheduleJob is accurate for detecting a shutdown in // progress, which is that the cluster service is stopped and closed at some point prior to closing plugins - assert isClusterServiceStoppedOrClosed() : "close is called by closing the plugin, which is expected to happen after " + - "the cluster service is stopped"; + assert isClusterServiceStoppedOrClosed() + : "close is called by closing the plugin, which is expected to happen after " + "the cluster service is stopped"; SchedulerEngine engine = scheduler.get(); if (engine != null) { engine.stop(); @@ -422,37 +478,50 @@ PolicyStepsRegistry getPolicyRegistry() { } static Set indicesOnShuttingDownNodesInDangerousStep(ClusterState state, String nodeId) { - final Set shutdownNodes = PluginShutdownService.shutdownTypeNodes(state, - SingleNodeShutdownMetadata.Type.REMOVE, SingleNodeShutdownMetadata.Type.REPLACE); + final Set shutdownNodes = PluginShutdownService.shutdownTypeNodes( + state, + SingleNodeShutdownMetadata.Type.REMOVE, + SingleNodeShutdownMetadata.Type.REPLACE + ); if (shutdownNodes.isEmpty()) { return Collections.emptySet(); } - Set indicesPreventingShutdown = state.metadata().indices().stream() + Set indicesPreventingShutdown = state.metadata() + .indices() + .stream() // Filter out to only consider managed indices - .filter(indexToMetadata -> Strings.hasText(LifecycleSettings.LIFECYCLE_NAME_SETTING.get( - indexToMetadata.getValue().getSettings()))) + .filter( + indexToMetadata -> Strings.hasText(LifecycleSettings.LIFECYCLE_NAME_SETTING.get(indexToMetadata.getValue().getSettings())) + ) // Only look at indices in the shrink action - .filter(indexToMetadata -> - ShrinkAction.NAME.equals(LifecycleExecutionState.fromIndexMetadata(indexToMetadata.getValue()).getAction())) + .filter( + indexToMetadata -> ShrinkAction.NAME.equals( + LifecycleExecutionState.fromIndexMetadata(indexToMetadata.getValue()).getAction() + ) + ) // Only look at indices on a step that may potentially be dangerous if we removed the node .filter(indexToMetadata -> { String step = LifecycleExecutionState.fromIndexMetadata(indexToMetadata.getValue()).getStep(); - return SetSingleNodeAllocateStep.NAME.equals(step) || - CheckShrinkReadyStep.NAME.equals(step) || - ShrinkStep.NAME.equals(step) || - ShrunkShardsAllocatedStep.NAME.equals(step); + return SetSingleNodeAllocateStep.NAME.equals(step) + || CheckShrinkReadyStep.NAME.equals(step) + || ShrinkStep.NAME.equals(step) + || ShrunkShardsAllocatedStep.NAME.equals(step); }) // Only look at indices where the node picked for the shrink is the node marked as shutting down .filter(indexToMetadata -> { - String nodePicked = indexToMetadata.getValue().getSettings() + String nodePicked = indexToMetadata.getValue() + .getSettings() .get(IndexMetadata.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + "_id"); return nodeId.equals(nodePicked); }) .map(Map.Entry::getKey) .collect(Collectors.toSet()); - logger.trace("with nodes marked as shutdown for removal {}, indices {} are preventing shutdown", - shutdownNodes, indicesPreventingShutdown); + logger.trace( + "with nodes marked as shutdown for removal {}, indices {} are preventing shutdown", + shutdownNodes, + indicesPreventingShutdown + ); return indicesPreventingShutdown; } diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycleTransition.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycleTransition.java index 1765f57fcc172..6fab733076749 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycleTransition.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycleTransition.java @@ -18,12 +18,12 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.index.Index; +import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.index.Index; -import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.xpack.core.ilm.ErrorStep; import org.elasticsearch.xpack.core.ilm.IndexLifecycleMetadata; import org.elasticsearch.xpack.core.ilm.InitializePolicyContextStep; @@ -60,15 +60,20 @@ */ public final class IndexLifecycleTransition { private static final Logger logger = LogManager.getLogger(IndexLifecycleTransition.class); - private static final ToXContent.Params STACKTRACE_PARAMS = - new ToXContent.MapParams(Collections.singletonMap(REST_EXCEPTION_SKIP_STACK_TRACE, "false")); + private static final ToXContent.Params STACKTRACE_PARAMS = new ToXContent.MapParams( + Collections.singletonMap(REST_EXCEPTION_SKIP_STACK_TRACE, "false") + ); /** * Validates that the given transition from {@code currentStepKey} to {@code newStepKey} can be accomplished * @throws IllegalArgumentException when the transition is not valid */ - public static void validateTransition(IndexMetadata idxMeta, Step.StepKey currentStepKey, - Step.StepKey newStepKey, PolicyStepsRegistry stepRegistry) { + public static void validateTransition( + IndexMetadata idxMeta, + Step.StepKey currentStepKey, + Step.StepKey newStepKey, + PolicyStepsRegistry stepRegistry + ) { String indexName = idxMeta.getIndex().getName(); Settings indexSettings = idxMeta.getSettings(); String indexPolicySetting = LifecycleSettings.LIFECYCLE_NAME_SETTING.get(indexSettings); @@ -81,19 +86,29 @@ public static void validateTransition(IndexMetadata idxMeta, Step.StepKey curren LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(idxMeta); Step.StepKey realKey = LifecycleExecutionState.getCurrentStepKey(lifecycleState); if (currentStepKey != null && currentStepKey.equals(realKey) == false) { - throw new IllegalArgumentException("index [" + indexName + "] is not on current step [" + currentStepKey + - "], currently: [" + realKey + "]"); + throw new IllegalArgumentException( + "index [" + indexName + "] is not on current step [" + currentStepKey + "], currently: [" + realKey + "]" + ); } - final Set cachedStepKeys = - stepRegistry.parseStepKeysFromPhase(lifecycleState.getPhaseDefinition(), lifecycleState.getPhase()); + final Set cachedStepKeys = stepRegistry.parseStepKeysFromPhase( + lifecycleState.getPhaseDefinition(), + lifecycleState.getPhase() + ); boolean isNewStepCached = cachedStepKeys != null && cachedStepKeys.contains(newStepKey); // Always allow moving to the terminal step or to a step that's present in the cached phase, even if it doesn't exist in the policy - if (isNewStepCached == false && - (stepRegistry.stepExists(indexPolicySetting, newStepKey) == false && newStepKey.equals(TerminalPolicyStep.KEY) == false)) { - throw new IllegalArgumentException("step [" + newStepKey + "] for index [" + idxMeta.getIndex().getName() + - "] with policy [" + indexPolicySetting + "] does not exist"); + if (isNewStepCached == false + && (stepRegistry.stepExists(indexPolicySetting, newStepKey) == false && newStepKey.equals(TerminalPolicyStep.KEY) == false)) { + throw new IllegalArgumentException( + "step [" + + newStepKey + + "] for index [" + + idxMeta.getIndex().getName() + + "] with policy [" + + indexPolicySetting + + "] does not exist" + ); } } @@ -109,8 +124,14 @@ public static void validateTransition(IndexMetadata idxMeta, Step.StepKey curren * @param forcePhaseDefinitionRefresh Whether to force the phase JSON to be reread or not * @return The updated cluster state where the index moved to newStepKey */ - static ClusterState moveClusterStateToStep(Index index, ClusterState state, Step.StepKey newStepKey, LongSupplier nowSupplier, - PolicyStepsRegistry stepRegistry, boolean forcePhaseDefinitionRefresh) { + static ClusterState moveClusterStateToStep( + Index index, + ClusterState state, + Step.StepKey newStepKey, + LongSupplier nowSupplier, + PolicyStepsRegistry stepRegistry, + boolean forcePhaseDefinitionRefresh + ) { IndexMetadata idxMeta = state.getMetadata().index(index); Step.StepKey currentStepKey = LifecycleExecutionState.getCurrentStepKey(LifecycleExecutionState.fromIndexMetadata(idxMeta)); validateTransition(idxMeta, currentStepKey, newStepKey, stepRegistry); @@ -123,8 +144,13 @@ static ClusterState moveClusterStateToStep(Index index, ClusterState state, Step LifecyclePolicyMetadata policyMetadata = ilmMeta.getPolicyMetadatas() .get(LifecycleSettings.LIFECYCLE_NAME_SETTING.get(idxMeta.getSettings())); LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(idxMeta); - LifecycleExecutionState newLifecycleState = updateExecutionStateToStep(policyMetadata, - lifecycleState, newStepKey, nowSupplier, forcePhaseDefinitionRefresh); + LifecycleExecutionState newLifecycleState = updateExecutionStateToStep( + policyMetadata, + lifecycleState, + newStepKey, + nowSupplier, + forcePhaseDefinitionRefresh + ); ClusterState.Builder newClusterStateBuilder = newClusterStateWithLifecycleState(index, state, newLifecycleState); return newClusterStateBuilder.build(); @@ -134,8 +160,13 @@ static ClusterState moveClusterStateToStep(Index index, ClusterState state, Step * Moves the given index into the ERROR step. The ERROR step will have the same phase and * action, but use the {@link ErrorStep#NAME} as the name in the lifecycle execution state. */ - static ClusterState moveClusterStateToErrorStep(Index index, ClusterState clusterState, Exception cause, LongSupplier nowSupplier, - BiFunction stepLookupFunction) throws IOException { + static ClusterState moveClusterStateToErrorStep( + Index index, + ClusterState clusterState, + Exception cause, + LongSupplier nowSupplier, + BiFunction stepLookupFunction + ) throws IOException { IndexMetadata idxMeta = clusterState.getMetadata().index(index); IndexLifecycleMetadata ilmMeta = clusterState.metadata().custom(IndexLifecycleMetadata.TYPE); LifecyclePolicyMetadata policyMetadata = ilmMeta.getPolicyMetadatas() @@ -152,11 +183,18 @@ static ClusterState moveClusterStateToErrorStep(Index index, ClusterState cluste if (cause instanceof InitializePolicyException) { currentStep = InitializePolicyContextStep.KEY; } else { - currentStep = Objects.requireNonNull(LifecycleExecutionState.getCurrentStepKey(currentState), - "unable to move to an error step where there is no current step, state: " + currentState); + currentStep = Objects.requireNonNull( + LifecycleExecutionState.getCurrentStepKey(currentState), + "unable to move to an error step where there is no current step, state: " + currentState + ); } - LifecycleExecutionState nextStepState = updateExecutionStateToStep(policyMetadata, currentState, - new Step.StepKey(currentStep.getPhase(), currentStep.getAction(), ErrorStep.NAME), nowSupplier, false); + LifecycleExecutionState nextStepState = updateExecutionStateToStep( + policyMetadata, + currentState, + new Step.StepKey(currentStep.getPhase(), currentStep.getAction(), ErrorStep.NAME), + nowSupplier, + false + ); LifecycleExecutionState.Builder failedState = LifecycleExecutionState.builder(nextStepState); failedState.setFailedStep(currentStep.getName()); @@ -170,8 +208,12 @@ static ClusterState moveClusterStateToErrorStep(Index index, ClusterState cluste // maintain the retry count of the failed step as it will be cleared after a successful execution failedState.setFailedStepRetryCount(currentState.getFailedStepRetryCount()); } else { - logger.warn("failed step [{}] for index [{}] is not part of policy [{}] anymore, or it is invalid", - currentStep.getName(), index, policyMetadata.getName()); + logger.warn( + "failed step [{}] for index [{}] is not part of policy [{}] anymore, or it is invalid", + currentStep.getName(), + index, + policyMetadata.getName() + ); } ClusterState.Builder newClusterStateBuilder = newClusterStateWithLifecycleState(index, clusterState, failedState.build()); @@ -182,8 +224,13 @@ static ClusterState moveClusterStateToErrorStep(Index index, ClusterState cluste * Move the given index's execution state back to a step that had previously failed. If this is * an automatic retry ({@code isAutomaticRetry}), the retry count is incremented. */ - static ClusterState moveClusterStateToPreviouslyFailedStep(ClusterState currentState, String index, LongSupplier nowSupplier, - PolicyStepsRegistry stepRegistry, boolean isAutomaticRetry) { + static ClusterState moveClusterStateToPreviouslyFailedStep( + ClusterState currentState, + String index, + LongSupplier nowSupplier, + PolicyStepsRegistry stepRegistry, + boolean isAutomaticRetry + ) { ClusterState newState; IndexMetadata indexMetadata = currentState.metadata().index(index); if (indexMetadata == null) { @@ -199,8 +246,13 @@ static ClusterState moveClusterStateToPreviouslyFailedStep(ClusterState currentS LifecyclePolicyMetadata policyMetadata = ilmMeta.getPolicyMetadatas() .get(LifecycleSettings.LIFECYCLE_NAME_SETTING.get(indexMetadata.getSettings())); - LifecycleExecutionState nextStepState = IndexLifecycleTransition.updateExecutionStateToStep(policyMetadata, - lifecycleState, nextStepKey, nowSupplier, true); + LifecycleExecutionState nextStepState = IndexLifecycleTransition.updateExecutionStateToStep( + policyMetadata, + lifecycleState, + nextStepKey, + nowSupplier, + true + ); LifecycleExecutionState.Builder retryStepState = LifecycleExecutionState.builder(nextStepState); retryStepState.setIsAutoRetryableError(lifecycleState.isAutoRetryableError()); Integer currentRetryCount = lifecycleState.getFailedStepRetryCount(); @@ -210,11 +262,15 @@ static ClusterState moveClusterStateToPreviouslyFailedStep(ClusterState currentS // manual retries don't update the retry count retryStepState.setFailedStepRetryCount(lifecycleState.getFailedStepRetryCount()); } - newState = IndexLifecycleTransition.newClusterStateWithLifecycleState(indexMetadata.getIndex(), - currentState, retryStepState.build()).build(); + newState = IndexLifecycleTransition.newClusterStateWithLifecycleState( + indexMetadata.getIndex(), + currentState, + retryStepState.build() + ).build(); } else { - throw new IllegalArgumentException("cannot retry an action for an index [" - + index + "] that has not encountered an error when running a Lifecycle Policy"); + throw new IllegalArgumentException( + "cannot retry an action for an index [" + index + "] that has not encountered an error when running a Lifecycle Policy" + ); } return newState; } @@ -223,11 +279,13 @@ static ClusterState moveClusterStateToPreviouslyFailedStep(ClusterState currentS * Given the existing execution state for an index, this updates pieces of the state with new * timings and optionally the phase JSON (when transitioning to a different phase). */ - private static LifecycleExecutionState updateExecutionStateToStep(LifecyclePolicyMetadata policyMetadata, - LifecycleExecutionState existingState, - Step.StepKey newStep, - LongSupplier nowSupplier, - boolean forcePhaseDefinitionRefresh) { + private static LifecycleExecutionState updateExecutionStateToStep( + LifecyclePolicyMetadata policyMetadata, + LifecycleExecutionState existingState, + Step.StepKey newStep, + LongSupplier nowSupplier, + boolean forcePhaseDefinitionRefresh + ) { Step.StepKey currentStep = LifecycleExecutionState.getCurrentStepKey(existingState); long nowAsMillis = nowSupplier.getAsLong(); LifecycleExecutionState.Builder updatedState = LifecycleExecutionState.builder(existingState); @@ -242,9 +300,7 @@ private static LifecycleExecutionState updateExecutionStateToStep(LifecyclePolic updatedState.setIsAutoRetryableError(null); updatedState.setFailedStepRetryCount(null); - if (currentStep == null || - currentStep.getPhase().equals(newStep.getPhase()) == false || - forcePhaseDefinitionRefresh) { + if (currentStep == null || currentStep.getPhase().equals(newStep.getPhase()) == false || forcePhaseDefinitionRefresh) { final String newPhaseDefinition; final Phase nextPhase; if ("new".equals(newStep.getPhase()) || TerminalPolicyStep.KEY.equals(newStep)) { @@ -252,8 +308,12 @@ private static LifecycleExecutionState updateExecutionStateToStep(LifecyclePolic } else { nextPhase = policyMetadata.getPolicy().getPhases().get(newStep.getPhase()); } - PhaseExecutionInfo phaseExecutionInfo = new PhaseExecutionInfo(policyMetadata.getName(), nextPhase, - policyMetadata.getVersion(), policyMetadata.getModifiedDate()); + PhaseExecutionInfo phaseExecutionInfo = new PhaseExecutionInfo( + policyMetadata.getName(), + nextPhase, + policyMetadata.getVersion(), + policyMetadata.getModifiedDate() + ); newPhaseDefinition = Strings.toString(phaseExecutionInfo, false, false); updatedState.setPhaseDefinition(newPhaseDefinition); updatedState.setPhaseTime(nowAsMillis); @@ -280,27 +340,38 @@ private static LifecycleExecutionState updateExecutionStateToStep(LifecyclePolic * * Returns the same {@link LifecycleExecutionState} if the transition is not possible or the new execution state otherwise. */ - public static LifecycleExecutionState moveStateToNextActionAndUpdateCachedPhase(IndexMetadata indexMetadata, - LifecycleExecutionState existingState, - LongSupplier nowSupplier, LifecyclePolicy oldPolicy, - LifecyclePolicyMetadata newPolicyMetadata, - Client client, XPackLicenseState licenseState) { + public static LifecycleExecutionState moveStateToNextActionAndUpdateCachedPhase( + IndexMetadata indexMetadata, + LifecycleExecutionState existingState, + LongSupplier nowSupplier, + LifecyclePolicy oldPolicy, + LifecyclePolicyMetadata newPolicyMetadata, + Client client, + XPackLicenseState licenseState + ) { String policyName = LifecycleSettings.LIFECYCLE_NAME_SETTING.get(indexMetadata.getSettings()); Step.StepKey currentStepKey = LifecycleExecutionState.getCurrentStepKey(existingState); if (currentStepKey == null) { - logger.warn("unable to identify what the current step is for index [{}] as part of policy [{}]. the " + - "cached phase definition will not be updated for this index", indexMetadata.getIndex().getName(), policyName); + logger.warn( + "unable to identify what the current step is for index [{}] as part of policy [{}]. the " + + "cached phase definition will not be updated for this index", + indexMetadata.getIndex().getName(), + policyName + ); return existingState; } List policySteps = oldPolicy.toSteps(client, licenseState); - Optional currentStep = policySteps.stream() - .filter(step -> step.getKey().equals(currentStepKey)) - .findFirst(); + Optional currentStep = policySteps.stream().filter(step -> step.getKey().equals(currentStepKey)).findFirst(); if (currentStep.isPresent() == false) { - logger.warn("unable to find current step [{}] for index [{}] as part of policy [{}]. the cached phase definition will not be " + - "updated for this index", currentStepKey, indexMetadata.getIndex().getName(), policyName); + logger.warn( + "unable to find current step [{}] for index [{}] as part of policy [{}]. the cached phase definition will not be " + + "updated for this index", + currentStepKey, + indexMetadata.getIndex().getName(), + policyName + ); return existingState; } @@ -314,8 +385,13 @@ public static LifecycleExecutionState moveStateToNextActionAndUpdateCachedPhase( assert nextStepInActionAfterCurrent.isPresent() : "there should always be a complete step at the end of every phase"; Step.StepKey nextStep = nextStepInActionAfterCurrent.get().getKey(); - logger.debug("moving index [{}] in policy [{}] out of step [{}] to new step [{}]", - indexMetadata.getIndex().getName(), policyName, currentStepKey, nextStep); + logger.debug( + "moving index [{}] in policy [{}] out of step [{}] to new step [{}]", + indexMetadata.getIndex().getName(), + policyName, + currentStepKey, + nextStep + ); long nowAsMillis = nowSupplier.getAsLong(); LifecycleExecutionState.Builder updatedState = LifecycleExecutionState.builder(existingState); @@ -329,9 +405,12 @@ public static LifecycleExecutionState moveStateToNextActionAndUpdateCachedPhase( updatedState.setIsAutoRetryableError(null); updatedState.setFailedStepRetryCount(null); - PhaseExecutionInfo phaseExecutionInfo = new PhaseExecutionInfo(newPolicyMetadata.getPolicy().getName(), - newPolicyMetadata.getPolicy().getPhases().get(currentStepKey.getPhase()), newPolicyMetadata.getVersion(), - newPolicyMetadata.getModifiedDate()); + PhaseExecutionInfo phaseExecutionInfo = new PhaseExecutionInfo( + newPolicyMetadata.getPolicy().getName(), + newPolicyMetadata.getPolicy().getPhases().get(currentStepKey.getPhase()), + newPolicyMetadata.getVersion(), + newPolicyMetadata.getModifiedDate() + ); updatedState.setPhaseDefinition(Strings.toString(phaseExecutionInfo, false, false)); return updatedState.build(); } @@ -339,13 +418,20 @@ public static LifecycleExecutionState moveStateToNextActionAndUpdateCachedPhase( /** * Given a cluster state and lifecycle state, return a new state using the new lifecycle state for the given index. */ - public static ClusterState.Builder newClusterStateWithLifecycleState(Index index, ClusterState clusterState, - LifecycleExecutionState lifecycleState) { + public static ClusterState.Builder newClusterStateWithLifecycleState( + Index index, + ClusterState clusterState, + LifecycleExecutionState lifecycleState + ) { ClusterState.Builder newClusterStateBuilder = ClusterState.builder(clusterState); - newClusterStateBuilder.metadata(Metadata.builder(clusterState.getMetadata()) - .put(IndexMetadata.builder(clusterState.getMetadata().index(index)) - .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.asMap())) - .build(false)); + newClusterStateBuilder.metadata( + Metadata.builder(clusterState.getMetadata()) + .put( + IndexMetadata.builder(clusterState.getMetadata().index(index)) + .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.asMap()) + ) + .build(false) + ); return newClusterStateBuilder; } diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycleUsageTransportAction.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycleUsageTransportAction.java index 687b2cd75fc0a..2a88765c86c2a 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycleUsageTransportAction.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycleUsageTransportAction.java @@ -12,8 +12,8 @@ import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.inject.Inject; +import org.elasticsearch.core.Tuple; import org.elasticsearch.protocol.xpack.XPackUsageRequest; import org.elasticsearch.tasks.Task; import org.elasticsearch.threadpool.ThreadPool; @@ -44,15 +44,30 @@ public class IndexLifecycleUsageTransportAction extends XPackUsageFeatureTransportAction { @Inject - public IndexLifecycleUsageTransportAction(TransportService transportService, ClusterService clusterService, ThreadPool threadPool, - ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver) { - super(XPackUsageFeatureAction.INDEX_LIFECYCLE.name(), transportService, clusterService, threadPool, actionFilters, - indexNameExpressionResolver); + public IndexLifecycleUsageTransportAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver + ) { + super( + XPackUsageFeatureAction.INDEX_LIFECYCLE.name(), + transportService, + clusterService, + threadPool, + actionFilters, + indexNameExpressionResolver + ); } @Override - protected void masterOperation(Task task, XPackUsageRequest request, ClusterState state, - ActionListener listener) { + protected void masterOperation( + Task task, + XPackUsageRequest request, + ClusterState state, + ActionListener listener + ) { Metadata metadata = state.metadata(); IndexLifecycleMetadata lifecycleMetadata = metadata.custom(IndexLifecycleMetadata.TYPE); final IndexLifecycleFeatureSetUsage usage; @@ -77,8 +92,10 @@ protected void masterOperation(Task task, XPackUsageRequest request, ClusterStat } String[] actionNames = actionStream.toArray(String[]::new); phase.getActions().forEach((k, v) -> collectActionConfigurations(k, v, configurations)); - return new Tuple<>(phase.getName(), new IndexLifecycleFeatureSetUsage.PhaseStats(phase.getMinimumAge(), actionNames, - configurations.build())); + return new Tuple<>( + phase.getName(), + new IndexLifecycleFeatureSetUsage.PhaseStats(phase.getMinimumAge(), actionNames, configurations.build()) + ); }).collect(Collectors.toMap(Tuple::v1, Tuple::v2)); return new IndexLifecycleFeatureSetUsage.PolicyStats(phaseStats, policyUsage.getOrDefault(policy.getName(), 0)); }).collect(Collectors.toList()); diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/LifecyclePolicySecurityClient.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/LifecyclePolicySecurityClient.java index fcce184ecf7bd..01cc7cb2801d5 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/LifecyclePolicySecurityClient.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/LifecyclePolicySecurityClient.java @@ -7,10 +7,10 @@ package org.elasticsearch.xpack.ilm; -import org.elasticsearch.action.ActionType; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionResponse; +import org.elasticsearch.action.ActionType; import org.elasticsearch.client.Client; import org.elasticsearch.client.support.AbstractClient; import org.elasticsearch.xpack.core.ClientHelper; @@ -47,8 +47,11 @@ public void close() { } @Override - protected void doExecute(ActionType action, Request request, - ActionListener listener) { + protected void doExecute( + ActionType action, + Request request, + ActionListener listener + ) { ClientHelper.executeWithHeadersAsync(headers, origin, client, action, request, listener); } diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/MoveToErrorStepUpdateTask.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/MoveToErrorStepUpdateTask.java index f83290bcd0cbd..a5af2c24ac272 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/MoveToErrorStepUpdateTask.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/MoveToErrorStepUpdateTask.java @@ -39,9 +39,15 @@ public class MoveToErrorStepUpdateTask extends ClusterStateUpdateTask { private final LongSupplier nowSupplier; private final Exception cause; - public MoveToErrorStepUpdateTask(Index index, String policy, Step.StepKey currentStepKey, Exception cause, LongSupplier nowSupplier, - BiFunction stepLookupFunction, - Consumer stateChangeConsumer) { + public MoveToErrorStepUpdateTask( + Index index, + String policy, + Step.StepKey currentStepKey, + Exception cause, + LongSupplier nowSupplier, + BiFunction stepLookupFunction, + Consumer stateChangeConsumer + ) { this.index = index; this.policy = policy; this.currentStepKey = currentStepKey; @@ -81,8 +87,11 @@ public void clusterStateProcessed(String source, ClusterState oldState, ClusterS @Override public void onFailure(String source, Exception e) { final MessageSupplier messageSupplier = () -> new ParameterizedMessage( - "policy [{}] for index [{}] failed trying to move from step [{}] to the ERROR step.", policy, index.getName(), - currentStepKey); + "policy [{}] for index [{}] failed trying to move from step [{}] to the ERROR step.", + policy, + index.getName(), + currentStepKey + ); if (ExceptionsHelper.unwrap(e, NotMasterException.class, FailedToCommitClusterStateException.class) != null) { logger.debug(messageSupplier, e); } else { diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/MoveToNextStepUpdateTask.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/MoveToNextStepUpdateTask.java index f3953c4f4008f..b0e70eea9c44f 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/MoveToNextStepUpdateTask.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/MoveToNextStepUpdateTask.java @@ -30,9 +30,15 @@ public class MoveToNextStepUpdateTask extends IndexLifecycleClusterStateUpdateTa private final PolicyStepsRegistry stepRegistry; private final Consumer stateChangeConsumer; - public MoveToNextStepUpdateTask(Index index, String policy, Step.StepKey currentStepKey, Step.StepKey nextStepKey, - LongSupplier nowSupplier, PolicyStepsRegistry stepRegistry, - Consumer stateChangeConsumer) { + public MoveToNextStepUpdateTask( + Index index, + String policy, + Step.StepKey currentStepKey, + Step.StepKey nextStepKey, + LongSupplier nowSupplier, + PolicyStepsRegistry stepRegistry, + Consumer stateChangeConsumer + ) { super(index, currentStepKey); this.policy = policy; this.nextStepKey = nextStepKey; @@ -88,7 +94,10 @@ public void handleFailure(String source, Exception e) { logger.warn( new ParameterizedMessage( "policy [{}] for index [{}] failed trying to move from step [{}] to step [{}].", - policy, index, currentStepKey, nextStepKey + policy, + index, + currentStepKey, + nextStepKey ), e ); diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/OperationModeUpdateTask.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/OperationModeUpdateTask.java index 8e11eb47efab0..e21b13b1588e3 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/OperationModeUpdateTask.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/OperationModeUpdateTask.java @@ -11,8 +11,8 @@ import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.ClusterStateUpdateTask; import org.elasticsearch.cluster.metadata.Metadata; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Priority; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xpack.core.ilm.IndexLifecycleMetadata; import org.elasticsearch.xpack.core.ilm.OperationMode; import org.elasticsearch.xpack.core.slm.SnapshotLifecycleMetadata; @@ -86,9 +86,10 @@ private ClusterState updateILMState(final ClusterState currentState) { logger.info("updating ILM operation mode to {}", newMode); } return ClusterState.builder(currentState) - .metadata(Metadata.builder(currentState.metadata()) - .putCustom(IndexLifecycleMetadata.TYPE, - new IndexLifecycleMetadata(currentMetadata.getPolicyMetadatas(), newMode))) + .metadata( + Metadata.builder(currentState.metadata()) + .putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata(currentMetadata.getPolicyMetadatas(), newMode)) + ) .build(); } @@ -114,10 +115,13 @@ private ClusterState updateSLMState(final ClusterState currentState) { logger.info("updating SLM operation mode to {}", newMode); } return ClusterState.builder(currentState) - .metadata(Metadata.builder(currentState.metadata()) - .putCustom(SnapshotLifecycleMetadata.TYPE, - new SnapshotLifecycleMetadata(currentMetadata.getSnapshotConfigurations(), - newMode, currentMetadata.getStats()))) + .metadata( + Metadata.builder(currentState.metadata()) + .putCustom( + SnapshotLifecycleMetadata.TYPE, + new SnapshotLifecycleMetadata(currentMetadata.getSnapshotConfigurations(), newMode, currentMetadata.getStats()) + ) + ) .build(); } diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/PolicyStepsRegistry.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/PolicyStepsRegistry.java index 73f8c695b3609..af63c5b54bdf4 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/PolicyStepsRegistry.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/PolicyStepsRegistry.java @@ -16,15 +16,15 @@ import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.core.TimeValue; +import org.elasticsearch.index.Index; +import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParseException; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.core.TimeValue; -import org.elasticsearch.index.Index; -import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.xpack.core.ClientHelper; import org.elasticsearch.xpack.core.ilm.ErrorStep; import org.elasticsearch.xpack.core.ilm.IndexLifecycleMetadata; @@ -68,9 +68,14 @@ public PolicyStepsRegistry(NamedXContentRegistry xContentRegistry, Client client this(new TreeMap<>(), new HashMap<>(), new HashMap<>(), xContentRegistry, client, licenseState); } - PolicyStepsRegistry(SortedMap lifecyclePolicyMap, - Map firstStepMap, Map> stepMap, - NamedXContentRegistry xContentRegistry, Client client, XPackLicenseState licenseState) { + PolicyStepsRegistry( + SortedMap lifecyclePolicyMap, + Map firstStepMap, + Map> stepMap, + NamedXContentRegistry xContentRegistry, + Client client, + XPackLicenseState licenseState + ) { this.lifecyclePolicyMap = lifecyclePolicyMap; this.firstStepMap = firstStepMap; this.stepMap = stepMap; @@ -94,25 +99,28 @@ Map> getStepMap() { public void update(IndexLifecycleMetadata meta) { assert meta != null : "IndexLifecycleMetadata cannot be null when updating the policy steps registry"; - DiffableUtils.MapDiff> mapDiff = - DiffableUtils.diff(lifecyclePolicyMap, meta.getPolicyMetadatas(), DiffableUtils.getStringKeySerializer(), - // Use a non-diffable value serializer. Otherwise actions in the same - // action and phase that are changed show up as diffs instead of upserts. - // We want to treat any change in the policy as an upsert so the map is - // correctly rebuilt - new DiffableUtils.NonDiffableValueSerializer<>() { - @Override - public void write(LifecyclePolicyMetadata value, StreamOutput out) { - // This is never called - throw new UnsupportedOperationException("should never be called"); - } + DiffableUtils.MapDiff> mapDiff = DiffableUtils.diff( + lifecyclePolicyMap, + meta.getPolicyMetadatas(), + DiffableUtils.getStringKeySerializer(), + // Use a non-diffable value serializer. Otherwise actions in the same + // action and phase that are changed show up as diffs instead of upserts. + // We want to treat any change in the policy as an upsert so the map is + // correctly rebuilt + new DiffableUtils.NonDiffableValueSerializer<>() { + @Override + public void write(LifecyclePolicyMetadata value, StreamOutput out) { + // This is never called + throw new UnsupportedOperationException("should never be called"); + } - @Override - public LifecyclePolicyMetadata read(StreamInput in, String key) { - // This is never called - throw new UnsupportedOperationException("should never be called"); - } - }); + @Override + public LifecyclePolicyMetadata read(StreamInput in, String key) { + // This is never called + throw new UnsupportedOperationException("should never be called"); + } + } + ); for (String deletedPolicyName : mapDiff.getDeletes()) { lifecyclePolicyMap.remove(deletedPolicyName); @@ -122,8 +130,11 @@ public LifecyclePolicyMetadata read(StreamInput in, String key) { if (mapDiff.getUpserts().isEmpty() == false) { for (LifecyclePolicyMetadata policyMetadata : mapDiff.getUpserts().values()) { - LifecyclePolicySecurityClient policyClient = new LifecyclePolicySecurityClient(client, ClientHelper.INDEX_LIFECYCLE_ORIGIN, - policyMetadata.getHeaders()); + LifecyclePolicySecurityClient policyClient = new LifecyclePolicySecurityClient( + client, + ClientHelper.INDEX_LIFECYCLE_ORIGIN, + policyMetadata.getHeaders() + ); lifecyclePolicyMap.put(policyMetadata.getName(), policyMetadata); List policyAsSteps = policyMetadata.getPolicy().toSteps(policyClient, licenseState); if (policyAsSteps.isEmpty() == false) { @@ -133,8 +144,11 @@ public LifecyclePolicyMetadata read(StreamInput in, String key) { assert ErrorStep.NAME.equals(step.getKey().getName()) == false : "unexpected error step in policy"; stepMapForPolicy.put(step.getKey(), step); } - logger.trace("updating cached steps for [{}] policy, new steps: {}", - policyMetadata.getName(), stepMapForPolicy.keySet()); + logger.trace( + "updating cached steps for [{}] policy, new steps: {}", + policyMetadata.getName(), + stepMapForPolicy.keySet() + ); stepMap.put(policyMetadata.getName(), stepMapForPolicy); } } @@ -156,8 +170,11 @@ private List getAllStepsForIndex(ClusterState state, Index index) { if (policyMetadata == null) { throw new IllegalArgumentException("the policy [" + policyName + "] for index" + index + " does not exist"); } - final LifecyclePolicySecurityClient policyClient = new LifecyclePolicySecurityClient(client, ClientHelper.INDEX_LIFECYCLE_ORIGIN, - policyMetadata.getHeaders()); + final LifecyclePolicySecurityClient policyClient = new LifecyclePolicySecurityClient( + client, + ClientHelper.INDEX_LIFECYCLE_ORIGIN, + policyMetadata.getHeaders() + ); return policyMetadata.getPolicy().toSteps(policyClient, licenseState); } @@ -205,14 +222,18 @@ private List parseStepsFromPhase(String policy, String currentPhase, Strin } LifecyclePolicy currentPolicy = policyMetadata.getPolicy(); final LifecyclePolicy policyToExecute; - if (InitializePolicyContextStep.INITIALIZATION_PHASE.equals(phaseDef) - || TerminalPolicyStep.COMPLETED_PHASE.equals(phaseDef)) { + if (InitializePolicyContextStep.INITIALIZATION_PHASE.equals(phaseDef) || TerminalPolicyStep.COMPLETED_PHASE.equals(phaseDef)) { // It is ok to re-use potentially modified policy here since we are in an initialization or completed phase policyToExecute = currentPolicy; } else { // if the current phase definition describes an internal step/phase, do not parse - try (XContentParser parser = JsonXContent.jsonXContent.createParser(xContentRegistry, - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, phaseDef)) { + try ( + XContentParser parser = JsonXContent.jsonXContent.createParser( + xContentRegistry, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + phaseDef + ) + ) { phaseExecutionInfo = PhaseExecutionInfo.parse(parser, currentPhase); } Map phaseMap = new HashMap<>(currentPolicy.getPhases()); @@ -221,20 +242,26 @@ private List parseStepsFromPhase(String policy, String currentPhase, Strin } policyToExecute = new LifecyclePolicy(currentPolicy.getType(), currentPolicy.getName(), phaseMap, currentPolicy.getMetadata()); } - LifecyclePolicySecurityClient policyClient = new LifecyclePolicySecurityClient(client, - ClientHelper.INDEX_LIFECYCLE_ORIGIN, lifecyclePolicyMap.get(policy).getHeaders()); + LifecyclePolicySecurityClient policyClient = new LifecyclePolicySecurityClient( + client, + ClientHelper.INDEX_LIFECYCLE_ORIGIN, + lifecyclePolicyMap.get(policy).getHeaders() + ); final List steps = policyToExecute.toSteps(policyClient, licenseState); // Build a list of steps that correspond with the phase the index is currently in final List phaseSteps; if (steps == null) { phaseSteps = new ArrayList<>(); } else { - phaseSteps = steps.stream() - .filter(e -> e.getKey().getPhase().equals(currentPhase)) - .collect(Collectors.toList()); + phaseSteps = steps.stream().filter(e -> e.getKey().getPhase().equals(currentPhase)).collect(Collectors.toList()); } - logger.trace("parsed steps for policy [{}] in phase [{}], definition: [{}], steps: [{}]", - policy, currentPhase, phaseDef, phaseSteps); + logger.trace( + "parsed steps for policy [{}] in phase [{}], definition: [{}], steps: [{}]", + policy, + currentPhase, + phaseDef, + phaseSteps + ); return phaseSteps; } @@ -262,13 +289,20 @@ public Step getStep(final IndexMetadata indexMetadata, final Step.StepKey stepKe } catch (IOException e) { throw new ElasticsearchException("failed to load cached steps for " + stepKey, e); } catch (XContentParseException parseErr) { - throw new XContentParseException(parseErr.getLocation(), - "failed to load steps for " + stepKey + " from [" + phaseJson + "]", parseErr); + throw new XContentParseException( + parseErr.getLocation(), + "failed to load steps for " + stepKey + " from [" + phaseJson + "]", + parseErr + ); } - assert phaseSteps.stream().allMatch(step -> step.getKey().getPhase().equals(phase)) : - "expected phase steps loaded from phase definition for [" + index.getName() + "] to be in phase [" + phase + - "] but they were not, steps: " + phaseSteps; + assert phaseSteps.stream().allMatch(step -> step.getKey().getPhase().equals(phase)) + : "expected phase steps loaded from phase definition for [" + + index.getName() + + "] to be in phase [" + + phase + + "] but they were not, steps: " + + phaseSteps; // Return the step that matches the given stepKey or else null if we couldn't find it return phaseSteps.stream().filter(step -> step.getKey().equals(stepKey)).findFirst().orElse(null); diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/SetStepInfoUpdateTask.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/SetStepInfoUpdateTask.java index 620f7da7f9ee5..69c4d5334b4d5 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/SetStepInfoUpdateTask.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/SetStepInfoUpdateTask.java @@ -14,9 +14,9 @@ import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.index.Index; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.index.Index; import org.elasticsearch.xpack.core.ilm.LifecycleExecutionState; import org.elasticsearch.xpack.core.ilm.LifecycleSettings; import org.elasticsearch.xpack.core.ilm.Step; @@ -55,7 +55,7 @@ protected ClusterState doExecute(ClusterState currentState) throws IOException { Settings indexSettings = idxMeta.getSettings(); LifecycleExecutionState indexILMData = LifecycleExecutionState.fromIndexMetadata(idxMeta); if (policy.equals(LifecycleSettings.LIFECYCLE_NAME_SETTING.get(indexSettings)) - && Objects.equals(currentStepKey, LifecycleExecutionState.getCurrentStepKey(indexILMData))) { + && Objects.equals(currentStepKey, LifecycleExecutionState.getCurrentStepKey(indexILMData))) { return IndexLifecycleTransition.addStepInfoToClusterState(index, currentState, stepInfo); } else { // either the policy has changed or the step is now @@ -70,7 +70,9 @@ public void handleFailure(String source, Exception e) { logger.warn( new ParameterizedMessage( "policy [{}] for index [{}] failed trying to set step info for step [{}].", - policy, index, currentStepKey + policy, + index, + currentStepKey ), e ); @@ -81,8 +83,10 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; SetStepInfoUpdateTask that = (SetStepInfoUpdateTask) o; - return index.equals(that.index) && policy.equals(that.policy) - && currentStepKey.equals(that.currentStepKey) && Objects.equals(stepInfo, that.stepInfo); + return index.equals(that.index) + && policy.equals(that.policy) + && currentStepKey.equals(that.currentStepKey) + && Objects.equals(stepInfo, that.stepInfo); } @Override diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/RestGetLifecycleAction.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/RestGetLifecycleAction.java index 9efd08f4dfa30..2af9de4af413c 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/RestGetLifecycleAction.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/RestGetLifecycleAction.java @@ -22,9 +22,7 @@ public class RestGetLifecycleAction extends BaseRestHandler { @Override public List routes() { - return List.of( - new Route(GET, "/_ilm/policy"), - new Route(GET, "/_ilm/policy/{name}")); + return List.of(new Route(GET, "/_ilm/policy"), new Route(GET, "/_ilm/policy/{name}")); } @Override diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/RestMigrateToDataTiersAction.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/RestMigrateToDataTiersAction.java index 0bc765f71ead3..12b761fb6b3bd 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/RestMigrateToDataTiersAction.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/RestMigrateToDataTiersAction.java @@ -33,8 +33,9 @@ public List routes() { @Override protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException { - MigrateToDataTiersRequest migrateRequest = request.hasContent() ? - MigrateToDataTiersRequest.parse(request.contentParser()) : new MigrateToDataTiersRequest(); + MigrateToDataTiersRequest migrateRequest = request.hasContent() + ? MigrateToDataTiersRequest.parse(request.contentParser()) + : new MigrateToDataTiersRequest(); migrateRequest.setDryRun(request.paramAsBoolean("dry_run", false)); return channel -> client.execute(MigrateToDataTiersAction.INSTANCE, migrateRequest, new RestToXContentListener<>(channel)); } diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/RestMoveToStepAction.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/RestMoveToStepAction.java index d4e34bd2de922..43f1a6b7f7624 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/RestMoveToStepAction.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/RestMoveToStepAction.java @@ -9,10 +9,10 @@ package org.elasticsearch.xpack.ilm.action; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestToXContentListener; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ilm.action.MoveToStepAction; import java.io.IOException; diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/RestPutLifecycleAction.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/RestPutLifecycleAction.java index 978ba45ed03d4..0c183070aa8f2 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/RestPutLifecycleAction.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/RestPutLifecycleAction.java @@ -8,10 +8,10 @@ package org.elasticsearch.xpack.ilm.action; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestToXContentListener; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ilm.action.PutLifecycleAction; import java.io.IOException; diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/RestRemoveIndexLifecyclePolicyAction.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/RestRemoveIndexLifecyclePolicyAction.java index 05a4c592182b0..272e5bd57fe3a 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/RestRemoveIndexLifecyclePolicyAction.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/RestRemoveIndexLifecyclePolicyAction.java @@ -38,7 +38,10 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient changePolicyRequest.masterNodeTimeout(restRequest.paramAsTime("master_timeout", changePolicyRequest.masterNodeTimeout())); changePolicyRequest.indicesOptions(IndicesOptions.fromRequest(restRequest, changePolicyRequest.indicesOptions())); - return channel -> - client.execute(RemoveIndexLifecyclePolicyAction.INSTANCE, changePolicyRequest, new RestToXContentListener<>(channel)); + return channel -> client.execute( + RemoveIndexLifecyclePolicyAction.INSTANCE, + changePolicyRequest, + new RestToXContentListener<>(channel) + ); } } diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportDeleteLifecycleAction.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportDeleteLifecycleAction.java index a43a35154602b..17ca84b81c2dc 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportDeleteLifecycleAction.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportDeleteLifecycleAction.java @@ -38,41 +38,64 @@ public class TransportDeleteLifecycleAction extends TransportMasterNodeAction { @Inject - public TransportDeleteLifecycleAction(TransportService transportService, ClusterService clusterService, ThreadPool threadPool, - ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver) { - super(DeleteLifecycleAction.NAME, transportService, clusterService, threadPool, actionFilters, - Request::new, indexNameExpressionResolver, AcknowledgedResponse::readFrom, ThreadPool.Names.SAME); + public TransportDeleteLifecycleAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver + ) { + super( + DeleteLifecycleAction.NAME, + transportService, + clusterService, + threadPool, + actionFilters, + Request::new, + indexNameExpressionResolver, + AcknowledgedResponse::readFrom, + ThreadPool.Names.SAME + ); } @Override protected void masterOperation(Task task, Request request, ClusterState state, ActionListener listener) { - clusterService.submitStateUpdateTask("delete-lifecycle-" + request.getPolicyName(), - new AckedClusterStateUpdateTask(request, listener) { - @Override - public ClusterState execute(ClusterState currentState) { - String policyToDelete = request.getPolicyName(); - List indicesUsingPolicy = currentState.metadata().indices().values().stream() - .filter(idxMeta -> LIFECYCLE_NAME_SETTING.get(idxMeta.getSettings()).equals(policyToDelete)) - .map(idxMeta -> idxMeta.getIndex().getName()) - .collect(Collectors.toList()); - if (indicesUsingPolicy.isEmpty() == false) { - throw new IllegalArgumentException("Cannot delete policy [" + request.getPolicyName() - + "]. It is in use by one or more indices: " + indicesUsingPolicy); - } - ClusterState.Builder newState = ClusterState.builder(currentState); - IndexLifecycleMetadata currentMetadata = currentState.metadata().custom(IndexLifecycleMetadata.TYPE); - if (currentMetadata == null - || currentMetadata.getPolicyMetadatas().containsKey(request.getPolicyName()) == false) { - throw new ResourceNotFoundException("Lifecycle policy not found: {}", request.getPolicyName()); - } - SortedMap newPolicies = new TreeMap<>(currentMetadata.getPolicyMetadatas()); - newPolicies.remove(request.getPolicyName()); - IndexLifecycleMetadata newMetadata = new IndexLifecycleMetadata(newPolicies, currentMetadata.getOperationMode()); - newState.metadata(Metadata.builder(currentState.getMetadata()) - .putCustom(IndexLifecycleMetadata.TYPE, newMetadata).build()); - return newState.build(); + clusterService.submitStateUpdateTask( + "delete-lifecycle-" + request.getPolicyName(), + new AckedClusterStateUpdateTask(request, listener) { + @Override + public ClusterState execute(ClusterState currentState) { + String policyToDelete = request.getPolicyName(); + List indicesUsingPolicy = currentState.metadata() + .indices() + .values() + .stream() + .filter(idxMeta -> LIFECYCLE_NAME_SETTING.get(idxMeta.getSettings()).equals(policyToDelete)) + .map(idxMeta -> idxMeta.getIndex().getName()) + .collect(Collectors.toList()); + if (indicesUsingPolicy.isEmpty() == false) { + throw new IllegalArgumentException( + "Cannot delete policy [" + + request.getPolicyName() + + "]. It is in use by one or more indices: " + + indicesUsingPolicy + ); } - }); + ClusterState.Builder newState = ClusterState.builder(currentState); + IndexLifecycleMetadata currentMetadata = currentState.metadata().custom(IndexLifecycleMetadata.TYPE); + if (currentMetadata == null || currentMetadata.getPolicyMetadatas().containsKey(request.getPolicyName()) == false) { + throw new ResourceNotFoundException("Lifecycle policy not found: {}", request.getPolicyName()); + } + SortedMap newPolicies = new TreeMap<>(currentMetadata.getPolicyMetadatas()); + newPolicies.remove(request.getPolicyName()); + IndexLifecycleMetadata newMetadata = new IndexLifecycleMetadata(newPolicies, currentMetadata.getOperationMode()); + newState.metadata( + Metadata.builder(currentState.getMetadata()).putCustom(IndexLifecycleMetadata.TYPE, newMetadata).build() + ); + return newState.build(); + } + } + ); } @Override diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportExplainLifecycleAction.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportExplainLifecycleAction.java index b8001d97dd861..cec1886c26e42 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportExplainLifecycleAction.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportExplainLifecycleAction.java @@ -19,14 +19,14 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.DeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.core.Nullable; import org.elasticsearch.tasks.Task; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.DeprecationHandler; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.ilm.ErrorStep; import org.elasticsearch.xpack.core.ilm.ExplainLifecycleRequest; import org.elasticsearch.xpack.core.ilm.ExplainLifecycleResponse; @@ -43,32 +43,55 @@ import static org.elasticsearch.xpack.core.ilm.LifecycleSettings.LIFECYCLE_ORIGINATION_DATE; -public class TransportExplainLifecycleAction - extends TransportClusterInfoAction { +public class TransportExplainLifecycleAction extends TransportClusterInfoAction { private final NamedXContentRegistry xContentRegistry; private final IndexLifecycleService indexLifecycleService; @Inject - public TransportExplainLifecycleAction(TransportService transportService, ClusterService clusterService, ThreadPool threadPool, - ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver, - NamedXContentRegistry xContentRegistry, IndexLifecycleService indexLifecycleService) { - super(ExplainLifecycleAction.NAME, transportService, clusterService, threadPool, actionFilters, ExplainLifecycleRequest::new, - indexNameExpressionResolver, ExplainLifecycleResponse::new); + public TransportExplainLifecycleAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver, + NamedXContentRegistry xContentRegistry, + IndexLifecycleService indexLifecycleService + ) { + super( + ExplainLifecycleAction.NAME, + transportService, + clusterService, + threadPool, + actionFilters, + ExplainLifecycleRequest::new, + indexNameExpressionResolver, + ExplainLifecycleResponse::new + ); this.xContentRegistry = xContentRegistry; this.indexLifecycleService = indexLifecycleService; } @Override - protected void doMasterOperation(Task task, ExplainLifecycleRequest request, String[] concreteIndices, ClusterState state, - ActionListener listener) { + protected void doMasterOperation( + Task task, + ExplainLifecycleRequest request, + String[] concreteIndices, + ClusterState state, + ActionListener listener + ) { Map indexResponses = new HashMap<>(); for (String index : concreteIndices) { IndexMetadata idxMetadata = state.metadata().index(index); final IndexLifecycleExplainResponse indexResponse; try { - indexResponse = getIndexLifecycleExplainResponse(idxMetadata, request.onlyErrors(), request.onlyManaged(), - indexLifecycleService, xContentRegistry); + indexResponse = getIndexLifecycleExplainResponse( + idxMetadata, + request.onlyErrors(), + request.onlyManaged(), + indexLifecycleService, + xContentRegistry + ); } catch (IOException e) { listener.onFailure(new ElasticsearchParseException("failed to parse phase definition for index [" + index + "]", e)); return; @@ -82,9 +105,13 @@ protected void doMasterOperation(Task task, ExplainLifecycleRequest request, Str } @Nullable - static IndexLifecycleExplainResponse getIndexLifecycleExplainResponse(IndexMetadata indexMetadata, boolean onlyErrors, - boolean onlyManaged, IndexLifecycleService indexLifecycleService, - NamedXContentRegistry xContentRegistry) throws IOException { + static IndexLifecycleExplainResponse getIndexLifecycleExplainResponse( + IndexMetadata indexMetadata, + boolean onlyErrors, + boolean onlyManaged, + IndexLifecycleService indexLifecycleService, + NamedXContentRegistry xContentRegistry + ) throws IOException { Settings idxSettings = indexMetadata.getSettings(); LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(indexMetadata); String policyName = LifecycleSettings.LIFECYCLE_NAME_SETTING.get(idxSettings); @@ -100,8 +127,13 @@ static IndexLifecycleExplainResponse getIndexLifecycleExplainResponse(IndexMetad String phaseDef = lifecycleState.getPhaseDefinition(); PhaseExecutionInfo phaseExecutionInfo = null; if (Strings.isNullOrEmpty(phaseDef) == false) { - try (XContentParser parser = JsonXContent.jsonXContent.createParser(xContentRegistry, - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, phaseDef)) { + try ( + XContentParser parser = JsonXContent.jsonXContent.createParser( + xContentRegistry, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + phaseDef + ) + ) { phaseExecutionInfo = PhaseExecutionInfo.parse(parser, currentPhase); } } @@ -112,7 +144,9 @@ static IndexLifecycleExplainResponse getIndexLifecycleExplainResponse(IndexMetad if (onlyErrors == false || (ErrorStep.NAME.equals(lifecycleState.getStep()) || indexLifecycleService.policyExists(policyName) == false)) { Long originationDate = idxSettings.getAsLong(LIFECYCLE_ORIGINATION_DATE, -1L); - indexResponse = IndexLifecycleExplainResponse.newManagedIndexResponse(indexName, policyName, + indexResponse = IndexLifecycleExplainResponse.newManagedIndexResponse( + indexName, + policyName, originationDate != -1L ? originationDate : lifecycleState.getLifecycleDate(), lifecycleState.getPhase(), lifecycleState.getAction(), @@ -127,7 +161,8 @@ static IndexLifecycleExplainResponse getIndexLifecycleExplainResponse(IndexMetad lifecycleState.getSnapshotName(), lifecycleState.getShrinkIndexName(), stepInfoBytes, - phaseExecutionInfo); + phaseExecutionInfo + ); } else { indexResponse = null; } diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportGetLifecycleAction.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportGetLifecycleAction.java index 641b007ee61d8..13335a4a57b36 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportGetLifecycleAction.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportGetLifecycleAction.java @@ -39,11 +39,25 @@ public class TransportGetLifecycleAction extends TransportMasterNodeAction requestedPolicies; @@ -63,9 +78,14 @@ protected void masterOperation(Task task, Request request, ClusterState state, A if (request.getPolicyNames().length == 0) { requestedPolicies = new ArrayList<>(metadata.getPolicyMetadatas().size()); for (LifecyclePolicyMetadata policyMetadata : metadata.getPolicyMetadatas().values()) { - requestedPolicies.add(new LifecyclePolicyResponseItem(policyMetadata.getPolicy(), - policyMetadata.getVersion(), policyMetadata.getModifiedDateString(), - LifecyclePolicyUtils.calculateUsage(indexNameExpressionResolver, state, policyMetadata.getName()))); + requestedPolicies.add( + new LifecyclePolicyResponseItem( + policyMetadata.getPolicy(), + policyMetadata.getVersion(), + policyMetadata.getModifiedDateString(), + LifecyclePolicyUtils.calculateUsage(indexNameExpressionResolver, state, policyMetadata.getName()) + ) + ); } } else { requestedPolicies = new ArrayList<>(request.getPolicyNames().length); @@ -75,9 +95,14 @@ protected void masterOperation(Task task, Request request, ClusterState state, A listener.onFailure(new ResourceNotFoundException("Lifecycle policy not found: {}", name)); return; } - requestedPolicies.add(new LifecyclePolicyResponseItem(policyMetadata.getPolicy(), - policyMetadata.getVersion(), policyMetadata.getModifiedDateString(), - LifecyclePolicyUtils.calculateUsage(indexNameExpressionResolver, state, policyMetadata.getName()))); + requestedPolicies.add( + new LifecyclePolicyResponseItem( + policyMetadata.getPolicy(), + policyMetadata.getVersion(), + policyMetadata.getModifiedDateString(), + LifecyclePolicyUtils.calculateUsage(indexNameExpressionResolver, state, policyMetadata.getName()) + ) + ); } } listener.onResponse(new Response(requestedPolicies)); diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportGetStatusAction.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportGetStatusAction.java index ee73650c3682f..62c59c80b8157 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportGetStatusAction.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportGetStatusAction.java @@ -28,10 +28,24 @@ public class TransportGetStatusAction extends TransportMasterNodeAction { @Inject - public TransportGetStatusAction(TransportService transportService, ClusterService clusterService, ThreadPool threadPool, - ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver) { - super(GetStatusAction.NAME, transportService, clusterService, threadPool, actionFilters, - Request::new, indexNameExpressionResolver, Response::new, ThreadPool.Names.SAME); + public TransportGetStatusAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver + ) { + super( + GetStatusAction.NAME, + transportService, + clusterService, + threadPool, + actionFilters, + Request::new, + indexNameExpressionResolver, + Response::new, + ThreadPool.Names.SAME + ); } @Override diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportMigrateToDataTiersAction.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportMigrateToDataTiersAction.java index 7e29dd77f8c40..c038b36d96b93 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportMigrateToDataTiersAction.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportMigrateToDataTiersAction.java @@ -20,12 +20,12 @@ import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.Priority; import org.elasticsearch.common.inject.Inject; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.core.Tuple; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.tasks.Task; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.cluster.action.MigrateToDataTiersAction; import org.elasticsearch.xpack.cluster.action.MigrateToDataTiersRequest; import org.elasticsearch.xpack.cluster.action.MigrateToDataTiersResponse; @@ -42,31 +42,58 @@ public class TransportMigrateToDataTiersAction extends TransportMasterNodeAction private final XPackLicenseState licenseState; @Inject - public TransportMigrateToDataTiersAction(TransportService transportService, ClusterService clusterService, - ThreadPool threadPool, ActionFilters actionFilters, - IndexNameExpressionResolver indexNameExpressionResolver, - NamedXContentRegistry xContentRegistry, Client client, XPackLicenseState licenseState) { - super(MigrateToDataTiersAction.NAME, transportService, clusterService, threadPool, actionFilters, MigrateToDataTiersRequest::new, - indexNameExpressionResolver, MigrateToDataTiersResponse::new, ThreadPool.Names.SAME); + public TransportMigrateToDataTiersAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver, + NamedXContentRegistry xContentRegistry, + Client client, + XPackLicenseState licenseState + ) { + super( + MigrateToDataTiersAction.NAME, + transportService, + clusterService, + threadPool, + actionFilters, + MigrateToDataTiersRequest::new, + indexNameExpressionResolver, + MigrateToDataTiersResponse::new, + ThreadPool.Names.SAME + ); this.xContentRegistry = xContentRegistry; this.client = client; this.licenseState = licenseState; } @Override - protected void masterOperation(Task task, MigrateToDataTiersRequest request, ClusterState state, - ActionListener listener) throws Exception { + protected void masterOperation( + Task task, + MigrateToDataTiersRequest request, + ClusterState state, + ActionListener listener + ) throws Exception { IndexLifecycleMetadata currentMetadata = state.metadata().custom(IndexLifecycleMetadata.TYPE); if (currentMetadata != null && currentMetadata.getOperationMode() != STOPPED) { - listener.onFailure(new IllegalStateException("stop ILM before migrating to data tiers, current state is [" + - currentMetadata.getOperationMode() + "]")); + listener.onFailure( + new IllegalStateException( + "stop ILM before migrating to data tiers, current state is [" + currentMetadata.getOperationMode() + "]" + ) + ); return; } if (request.isDryRun()) { - MigratedEntities entities = - migrateToDataTiersRouting(state, request.getNodeAttributeName(), request.getLegacyTemplateToDelete(), - xContentRegistry, client, licenseState).v2(); + MigratedEntities entities = migrateToDataTiersRouting( + state, + request.getNodeAttributeName(), + request.getLegacyTemplateToDelete(), + xContentRegistry, + client, + licenseState + ).v2(); listener.onResponse( new MigrateToDataTiersResponse(entities.removedIndexTemplateName, entities.migratedPolicies, entities.migratedIndices, true) ); @@ -77,9 +104,14 @@ protected void masterOperation(Task task, MigrateToDataTiersRequest request, Clu clusterService.submitStateUpdateTask("migrate-to-data-tiers []", new ClusterStateUpdateTask(Priority.HIGH) { @Override public ClusterState execute(ClusterState currentState) throws Exception { - Tuple migratedEntitiesTuple = - migrateToDataTiersRouting(state, request.getNodeAttributeName(), request.getLegacyTemplateToDelete(), - xContentRegistry, client, licenseState); + Tuple migratedEntitiesTuple = migrateToDataTiersRouting( + state, + request.getNodeAttributeName(), + request.getLegacyTemplateToDelete(), + xContentRegistry, + client, + licenseState + ); migratedEntities.set(migratedEntitiesTuple.v2()); return migratedEntitiesTuple.v1(); @@ -94,8 +126,13 @@ public void onFailure(String source, Exception e) { public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) { super.clusterStateProcessed(source, oldState, newState); MigratedEntities entities = migratedEntities.get(); - listener.onResponse(new MigrateToDataTiersResponse(entities.removedIndexTemplateName, entities.migratedPolicies, - entities.migratedIndices, false) + listener.onResponse( + new MigrateToDataTiersResponse( + entities.removedIndexTemplateName, + entities.migratedPolicies, + entities.migratedIndices, + false + ) ); } }); diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportMoveToStepAction.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportMoveToStepAction.java index 97b6f2ca0b1bd..3f67e4a4e50f3 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportMoveToStepAction.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportMoveToStepAction.java @@ -35,12 +35,27 @@ public class TransportMoveToStepAction extends TransportMasterNodeAction concreteTargetKey = new SetOnce<>(); @@ -84,20 +109,34 @@ protected void masterOperation(Task task, Request request, ClusterState state, A public ClusterState execute(ClusterState currentState) { // Resolve the key that could have optional parts into one // that is totally concrete given the existing policy and index - Step.StepKey concreteTargetStepKey = indexLifecycleService.resolveStepKey(state, indexMetadata.getIndex(), - abstractTargetKey.getPhase(), abstractTargetKey.getAction(), abstractTargetKey.getName()); + Step.StepKey concreteTargetStepKey = indexLifecycleService.resolveStepKey( + state, + indexMetadata.getIndex(), + abstractTargetKey.getPhase(), + abstractTargetKey.getAction(), + abstractTargetKey.getName() + ); // Make one more check, because it could have changed in the meantime. If that is the case, the request is ignored. if (concreteTargetStepKey == null) { // This means we weren't able to find the key they specified - logger.error("unable to move index " + indexMetadata.getIndex() + " as we are unable to resolve a concrete " + - "step key from target next step key: " + abstractTargetKey); + logger.error( + "unable to move index " + + indexMetadata.getIndex() + + " as we are unable to resolve a concrete " + + "step key from target next step key: " + + abstractTargetKey + ); return currentState; } concreteTargetKey.set(concreteTargetStepKey); - return indexLifecycleService.moveClusterStateToStep(currentState, indexMetadata.getIndex(), request.getCurrentStepKey(), - concreteTargetKey.get()); + return indexLifecycleService.moveClusterStateToStep( + currentState, + indexMetadata.getIndex(), + request.getCurrentStepKey(), + concreteTargetKey.get() + ); } @Override @@ -105,13 +144,19 @@ public void clusterStateProcessed(String source, ClusterState oldState, ClusterS IndexMetadata newIndexMetadata = newState.metadata().index(indexMetadata.getIndex()); if (newIndexMetadata == null) { // The index has somehow been deleted - there shouldn't be any opportunity for this to happen, but just in case. - logger.debug("index [" + indexMetadata.getIndex() + "] has been deleted after moving to step [" + - concreteTargetKey.get() + "], skipping async action check"); + logger.debug( + "index [" + + indexMetadata.getIndex() + + "] has been deleted after moving to step [" + + concreteTargetKey.get() + + "], skipping async action check" + ); return; } indexLifecycleService.maybeRunAsyncAction(newState, newIndexMetadata, concreteTargetKey.get()); } - }); + } + ); } @Override diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportPutLifecycleAction.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportPutLifecycleAction.java index ab48fa35803c7..e3599c2341c27 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportPutLifecycleAction.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportPutLifecycleAction.java @@ -24,11 +24,11 @@ import org.elasticsearch.cluster.metadata.RepositoriesMetadata; import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.inject.Inject; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.tasks.Task; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.ClientHelper; import org.elasticsearch.xpack.core.ilm.IndexLifecycleMetadata; import org.elasticsearch.xpack.core.ilm.LifecyclePolicy; @@ -62,11 +62,27 @@ public class TransportPutLifecycleAction extends TransportMasterNodeAction newPolicies = new TreeMap<>(currentMetadata.getPolicyMetadatas()); - LifecyclePolicyMetadata lifecyclePolicyMetadata = new LifecyclePolicyMetadata(request.getPolicy(), filteredHeaders, - nextVersion, Instant.now().toEpochMilli()); + LifecyclePolicyMetadata lifecyclePolicyMetadata = new LifecyclePolicyMetadata( + request.getPolicy(), + filteredHeaders, + nextVersion, + Instant.now().toEpochMilli() + ); LifecyclePolicyMetadata oldPolicy = newPolicies.put(lifecyclePolicyMetadata.getName(), lifecyclePolicyMetadata); if (oldPolicy == null) { logger.info("adding index lifecycle policy [{}]", request.getPolicy().getName()); @@ -106,24 +127,37 @@ public ClusterState execute(ClusterState currentState) throws Exception { logger.info("updating index lifecycle policy [{}]", request.getPolicy().getName()); } IndexLifecycleMetadata newMetadata = new IndexLifecycleMetadata(newPolicies, currentMetadata.getOperationMode()); - stateBuilder.metadata(Metadata.builder(currentState.getMetadata()) - .putCustom(IndexLifecycleMetadata.TYPE, newMetadata).build()); + stateBuilder.metadata( + Metadata.builder(currentState.getMetadata()).putCustom(IndexLifecycleMetadata.TYPE, newMetadata).build() + ); ClusterState nonRefreshedState = stateBuilder.build(); if (oldPolicy == null) { return nonRefreshedState; } else { try { - return updateIndicesForPolicy(nonRefreshedState, xContentRegistry, client, - oldPolicy.getPolicy(), lifecyclePolicyMetadata, licenseState); + return updateIndicesForPolicy( + nonRefreshedState, + xContentRegistry, + client, + oldPolicy.getPolicy(), + lifecyclePolicyMetadata, + licenseState + ); } catch (Exception e) { - logger.warn(new ParameterizedMessage("unable to refresh indices phase JSON for updated policy [{}]", - oldPolicy.getName()), e); + logger.warn( + new ParameterizedMessage( + "unable to refresh indices phase JSON for updated policy [{}]", + oldPolicy.getName() + ), + e + ); // Revert to the non-refreshed state return nonRefreshedState; } } } - }); + } + ); } /** @@ -134,39 +168,65 @@ public ClusterState execute(ClusterState currentState) throws Exception { * @param state The cluster state */ private void validatePrerequisites(LifecyclePolicy policy, ClusterState state) { - List phasesWithSearchableSnapshotActions = policy.getPhases().values().stream() + List phasesWithSearchableSnapshotActions = policy.getPhases() + .values() + .stream() .filter(phase -> phase.getActions().containsKey(SearchableSnapshotAction.NAME)) .collect(Collectors.toList()); // check license level for searchable snapshots - if (phasesWithSearchableSnapshotActions.isEmpty() == false && - SEARCHABLE_SNAPSHOT_FEATURE.checkWithoutTracking(licenseState) == false) { - throw new IllegalArgumentException("policy [" + policy.getName() + "] defines the [" + - SearchableSnapshotAction.NAME + "] action but the current license is non-compliant for [searchable-snapshots]"); + if (phasesWithSearchableSnapshotActions.isEmpty() == false + && SEARCHABLE_SNAPSHOT_FEATURE.checkWithoutTracking(licenseState) == false) { + throw new IllegalArgumentException( + "policy [" + + policy.getName() + + "] defines the [" + + SearchableSnapshotAction.NAME + + "] action but the current license is non-compliant for [searchable-snapshots]" + ); } // make sure any referenced snapshot repositories exist for (Phase phase : phasesWithSearchableSnapshotActions) { SearchableSnapshotAction action = (SearchableSnapshotAction) phase.getActions().get(SearchableSnapshotAction.NAME); String repository = action.getSnapshotRepository(); - if (state.metadata().custom(RepositoriesMetadata.TYPE, RepositoriesMetadata.EMPTY) - .repository(repository) == null) { - throw new IllegalArgumentException("no such repository [" + repository + "], the snapshot repository " + - "referenced by the [" + SearchableSnapshotAction.NAME + "] action in the [" + phase.getName() + "] phase " + - "must exist before it can be referenced by an ILM policy"); + if (state.metadata().custom(RepositoriesMetadata.TYPE, RepositoriesMetadata.EMPTY).repository(repository) == null) { + throw new IllegalArgumentException( + "no such repository [" + + repository + + "], the snapshot repository " + + "referenced by the [" + + SearchableSnapshotAction.NAME + + "] action in the [" + + phase.getName() + + "] phase " + + "must exist before it can be referenced by an ILM policy" + ); } } - List phasesWithWaitForSnapshotActions = policy.getPhases().values().stream() + List phasesWithWaitForSnapshotActions = policy.getPhases() + .values() + .stream() .filter(phase -> phase.getActions().containsKey(WaitForSnapshotAction.NAME)) .collect(Collectors.toList()); // make sure any referenced snapshot lifecycle policies exist for (Phase phase : phasesWithWaitForSnapshotActions) { WaitForSnapshotAction action = (WaitForSnapshotAction) phase.getActions().get(WaitForSnapshotAction.NAME); String slmPolicy = action.getPolicy(); - if (state.metadata().custom(SnapshotLifecycleMetadata.TYPE, SnapshotLifecycleMetadata.EMPTY) - .getSnapshotConfigurations().get(slmPolicy) == null) { - throw new IllegalArgumentException("no such snapshot lifecycle policy [" + slmPolicy + "], the snapshot lifecycle policy " + - "referenced by the [" + WaitForSnapshotAction.NAME + "] action in the [" + phase.getName() + "] phase " + - "must exist before it can be referenced by an ILM policy"); + if (state.metadata() + .custom(SnapshotLifecycleMetadata.TYPE, SnapshotLifecycleMetadata.EMPTY) + .getSnapshotConfigurations() + .get(slmPolicy) == null) { + throw new IllegalArgumentException( + "no such snapshot lifecycle policy [" + + slmPolicy + + "], the snapshot lifecycle policy " + + "referenced by the [" + + WaitForSnapshotAction.NAME + + "] action in the [" + + phase.getName() + + "] phase " + + "must exist before it can be referenced by an ILM policy" + ); } } } diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportRemoveIndexLifecyclePolicyAction.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportRemoveIndexLifecyclePolicyAction.java index 0a7adf575dd4c..a248eb55beb5c 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportRemoveIndexLifecyclePolicyAction.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportRemoveIndexLifecyclePolicyAction.java @@ -32,11 +32,24 @@ public class TransportRemoveIndexLifecyclePolicyAction extends TransportMasterNodeAction { @Inject - public TransportRemoveIndexLifecyclePolicyAction(TransportService transportService, ClusterService clusterService, - ThreadPool threadPool, ActionFilters actionFilters, - IndexNameExpressionResolver indexNameExpressionResolver) { - super(RemoveIndexLifecyclePolicyAction.NAME, transportService, clusterService, threadPool, actionFilters, - Request::new, indexNameExpressionResolver, Response::new, ThreadPool.Names.SAME); + public TransportRemoveIndexLifecyclePolicyAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver + ) { + super( + RemoveIndexLifecyclePolicyAction.NAME, + transportService, + clusterService, + threadPool, + actionFilters, + Request::new, + indexNameExpressionResolver, + Response::new, + ThreadPool.Names.SAME + ); } @Override @@ -47,26 +60,25 @@ protected ClusterBlockException checkBlock(Request request, ClusterState state) @Override protected void masterOperation(Task task, Request request, ClusterState state, ActionListener listener) throws Exception { final Index[] indices = indexNameExpressionResolver.concreteIndices(state, request.indicesOptions(), true, request.indices()); - clusterService.submitStateUpdateTask("remove-lifecycle-for-index", - new ClusterStateUpdateTask(request.masterNodeTimeout()) { + clusterService.submitStateUpdateTask("remove-lifecycle-for-index", new ClusterStateUpdateTask(request.masterNodeTimeout()) { - private final List failedIndexes = new ArrayList<>(); + private final List failedIndexes = new ArrayList<>(); - @Override - public ClusterState execute(ClusterState currentState) throws Exception { - return IndexLifecycleTransition.removePolicyForIndexes(indices, currentState, failedIndexes); - } + @Override + public ClusterState execute(ClusterState currentState) throws Exception { + return IndexLifecycleTransition.removePolicyForIndexes(indices, currentState, failedIndexes); + } - @Override - public void onFailure(String source, Exception e) { - listener.onFailure(e); - } + @Override + public void onFailure(String source, Exception e) { + listener.onFailure(e); + } - @Override - public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) { - listener.onResponse(new Response(failedIndexes)); - } - }); + @Override + public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) { + listener.onResponse(new Response(failedIndexes)); + } + }); } } diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportRetryAction.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportRetryAction.java index 81647157ee9c3..3548653121124 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportRetryAction.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportRetryAction.java @@ -37,39 +37,57 @@ public class TransportRetryAction extends TransportMasterNodeAction listener) { - clusterService.submitStateUpdateTask("ilm-re-run", - new AckedClusterStateUpdateTask(request, listener) { - @Override - public ClusterState execute(ClusterState currentState) { - return indexLifecycleService.moveClusterStateToPreviouslyFailedStep(currentState, request.indices()); - } + clusterService.submitStateUpdateTask("ilm-re-run", new AckedClusterStateUpdateTask(request, listener) { + @Override + public ClusterState execute(ClusterState currentState) { + return indexLifecycleService.moveClusterStateToPreviouslyFailedStep(currentState, request.indices()); + } - @Override - public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) { - for (String index : request.indices()) { - IndexMetadata idxMeta = newState.metadata().index(index); - LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(idxMeta); - StepKey retryStep = new StepKey(lifecycleState.getPhase(), lifecycleState.getAction(), lifecycleState.getStep()); - if (idxMeta == null) { - // The index has somehow been deleted - there shouldn't be any opportunity for this to happen, but just in case. - logger.debug("index [" + index + "] has been deleted after moving to step [" + - lifecycleState.getStep() + "], skipping async action check"); - return; - } - indexLifecycleService.maybeRunAsyncAction(newState, idxMeta, retryStep); + @Override + public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) { + for (String index : request.indices()) { + IndexMetadata idxMeta = newState.metadata().index(index); + LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(idxMeta); + StepKey retryStep = new StepKey(lifecycleState.getPhase(), lifecycleState.getAction(), lifecycleState.getStep()); + if (idxMeta == null) { + // The index has somehow been deleted - there shouldn't be any opportunity for this to happen, but just in case. + logger.debug( + "index [" + + index + + "] has been deleted after moving to step [" + + lifecycleState.getStep() + + "], skipping async action check" + ); + return; } + indexLifecycleService.maybeRunAsyncAction(newState, idxMeta, retryStep); } - }); + } + }); } @Override diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportStartILMAction.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportStartILMAction.java index 9664c57f15f75..6e947ac486517 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportStartILMAction.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportStartILMAction.java @@ -29,10 +29,23 @@ public class TransportStartILMAction extends AcknowledgedTransportMasterNodeAction { @Inject - public TransportStartILMAction(TransportService transportService, ClusterService clusterService, ThreadPool threadPool, - ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver) { - super(StartILMAction.NAME, transportService, clusterService, threadPool, actionFilters, StartILMRequest::new, - indexNameExpressionResolver, ThreadPool.Names.SAME); + public TransportStartILMAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver + ) { + super( + StartILMAction.NAME, + transportService, + clusterService, + threadPool, + actionFilters, + StartILMRequest::new, + indexNameExpressionResolver, + ThreadPool.Names.SAME + ); } @Override diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportStopILMAction.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportStopILMAction.java index 15ec7017d2cfb..41c70e47e276b 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportStopILMAction.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportStopILMAction.java @@ -30,21 +30,36 @@ public class TransportStopILMAction extends AcknowledgedTransportMasterNodeAction { @Inject - public TransportStopILMAction(TransportService transportService, ClusterService clusterService, ThreadPool threadPool, - ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver) { - super(StopILMAction.NAME, transportService, clusterService, threadPool, actionFilters, StopILMRequest::new, - indexNameExpressionResolver, ThreadPool.Names.SAME); + public TransportStopILMAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver + ) { + super( + StopILMAction.NAME, + transportService, + clusterService, + threadPool, + actionFilters, + StopILMRequest::new, + indexNameExpressionResolver, + ThreadPool.Names.SAME + ); } @Override protected void masterOperation(Task task, StopILMRequest request, ClusterState state, ActionListener listener) { - clusterService.submitStateUpdateTask("ilm_operation_mode_update", + clusterService.submitStateUpdateTask( + "ilm_operation_mode_update", new AckedClusterStateUpdateTask(Priority.IMMEDIATE, request, listener) { @Override public ClusterState execute(ClusterState currentState) { return (OperationModeUpdateTask.ilmMode(OperationMode.STOPPING)).execute(currentState); } - }); + } + ); } @Override diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/history/ILMHistoryItem.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/history/ILMHistoryItem.java index 2495e6d2ef3d7..d54e0ed066c2c 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/history/ILMHistoryItem.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/history/ILMHistoryItem.java @@ -8,10 +8,10 @@ package org.elasticsearch.xpack.ilm.history; import org.elasticsearch.ElasticsearchException; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.json.JsonXContent; @@ -49,8 +49,15 @@ public class ILMHistoryItem implements ToXContentObject { @Nullable private final String errorDetails; - private ILMHistoryItem(String index, String policyId, long timestamp, @Nullable Long indexAge, boolean success, - @Nullable LifecycleExecutionState executionState, @Nullable String errorDetails) { + private ILMHistoryItem( + String index, + String policyId, + long timestamp, + @Nullable Long indexAge, + boolean success, + @Nullable LifecycleExecutionState executionState, + @Nullable String errorDetails + ) { this.index = index; this.policyId = policyId; this.timestamp = timestamp; @@ -60,13 +67,24 @@ private ILMHistoryItem(String index, String policyId, long timestamp, @Nullable this.errorDetails = errorDetails; } - public static ILMHistoryItem success(String index, String policyId, long timestamp, @Nullable Long indexAge, - @Nullable LifecycleExecutionState executionState) { + public static ILMHistoryItem success( + String index, + String policyId, + long timestamp, + @Nullable Long indexAge, + @Nullable LifecycleExecutionState executionState + ) { return new ILMHistoryItem(index, policyId, timestamp, indexAge, true, executionState, null); } - public static ILMHistoryItem failure(String index, String policyId, long timestamp, @Nullable Long indexAge, - @Nullable LifecycleExecutionState executionState, Exception error) { + public static ILMHistoryItem failure( + String index, + String policyId, + long timestamp, + @Nullable Long indexAge, + @Nullable LifecycleExecutionState executionState, + Exception error + ) { Objects.requireNonNull(error, "ILM failures require an attached exception"); return new ILMHistoryItem(index, policyId, timestamp, indexAge, false, executionState, exceptionToString(error)); } @@ -103,8 +121,10 @@ private static String exceptionToString(Exception exception) { // In the unlikely case that we cannot generate an exception string, // try the best way can to encapsulate the error(s) with at least // the message - exceptionString = "unable to generate the ILM error details due to: " + e.getMessage() + - "; the ILM error was: " + exception.getMessage(); + exceptionString = "unable to generate the ILM error details due to: " + + e.getMessage() + + "; the ILM error was: " + + exception.getMessage(); } return exceptionString; } diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/history/ILMHistoryStore.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/history/ILMHistoryStore.java index 7118187749c4f..708ec7b60a1a2 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/history/ILMHistoryStore.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/history/ILMHistoryStore.java @@ -25,10 +25,10 @@ import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.threadpool.ThreadPool; import java.io.Closeable; import java.io.IOException; @@ -61,56 +61,68 @@ public ILMHistoryStore(Settings nodeSettings, Client client, ClusterService clus this.ilmHistoryEnabled = LIFECYCLE_HISTORY_INDEX_ENABLED_SETTING.get(nodeSettings); this.threadPool = threadPool; - this.processor = BulkProcessor.builder( - new OriginSettingClient(client, INDEX_LIFECYCLE_ORIGIN)::bulk, - new BulkProcessor.Listener() { - @Override - public void beforeBulk(long executionId, BulkRequest request) { - if (clusterService.state().getMetadata().templatesV2().containsKey(ILM_TEMPLATE_NAME) == false) { - ElasticsearchException e = new ElasticsearchException("no ILM history template"); - logger.warn(new ParameterizedMessage("unable to index the following ILM history items:\n{}", - request.requests().stream() + this.processor = BulkProcessor.builder(new OriginSettingClient(client, INDEX_LIFECYCLE_ORIGIN)::bulk, new BulkProcessor.Listener() { + @Override + public void beforeBulk(long executionId, BulkRequest request) { + if (clusterService.state().getMetadata().templatesV2().containsKey(ILM_TEMPLATE_NAME) == false) { + ElasticsearchException e = new ElasticsearchException("no ILM history template"); + logger.warn( + new ParameterizedMessage( + "unable to index the following ILM history items:\n{}", + request.requests() + .stream() .filter(dwr -> (dwr instanceof IndexRequest)) .map(dwr -> ((IndexRequest) dwr)) .map(IndexRequest::sourceAsMap) .map(Object::toString) - .collect(Collectors.joining("\n"))), e); - throw new ElasticsearchException(e); - } - if (logger.isTraceEnabled()) { - logger.info("about to index: {}", - request.requests().stream() - .map(dwr -> ((IndexRequest) dwr).sourceAsMap()) - .map(Objects::toString) - .collect(Collectors.joining(","))); - } + .collect(Collectors.joining("\n")) + ), + e + ); + throw new ElasticsearchException(e); } - - @Override - public void afterBulk(long executionId, BulkRequest request, BulkResponse response) { - long items = request.numberOfActions(); - if (logger.isTraceEnabled()) { - logger.trace("indexed [{}] items into ILM history index [{}]", items, - Arrays.stream(response.getItems()) - .map(BulkItemResponse::getIndex) - .distinct() - .collect(Collectors.joining(","))); - } - if (response.hasFailures()) { - Map failures = Arrays.stream(response.getItems()) - .filter(BulkItemResponse::isFailed) - .collect(Collectors.toMap(BulkItemResponse::getId, BulkItemResponse::getFailureMessage, - (msg1, msg2) -> Objects.equals(msg1, msg2) ? msg1 : msg1 + "," + msg2)); - logger.error("failures: [{}]", failures); - } + if (logger.isTraceEnabled()) { + logger.info( + "about to index: {}", + request.requests() + .stream() + .map(dwr -> ((IndexRequest) dwr).sourceAsMap()) + .map(Objects::toString) + .collect(Collectors.joining(",")) + ); } + } - @Override - public void afterBulk(long executionId, BulkRequest request, Throwable failure) { - long items = request.numberOfActions(); - logger.error(new ParameterizedMessage("failed to index {} items into ILM history index", items), failure); + @Override + public void afterBulk(long executionId, BulkRequest request, BulkResponse response) { + long items = request.numberOfActions(); + if (logger.isTraceEnabled()) { + logger.trace( + "indexed [{}] items into ILM history index [{}]", + items, + Arrays.stream(response.getItems()).map(BulkItemResponse::getIndex).distinct().collect(Collectors.joining(",")) + ); } - }, "ilm-history-store") + if (response.hasFailures()) { + Map failures = Arrays.stream(response.getItems()) + .filter(BulkItemResponse::isFailed) + .collect( + Collectors.toMap( + BulkItemResponse::getId, + BulkItemResponse::getFailureMessage, + (msg1, msg2) -> Objects.equals(msg1, msg2) ? msg1 : msg1 + "," + msg2 + ) + ); + logger.error("failures: [{}]", failures); + } + } + + @Override + public void afterBulk(long executionId, BulkRequest request, Throwable failure) { + long items = request.numberOfActions(); + logger.error(new ParameterizedMessage("failed to index {} items into ILM history index", items), failure); + } + }, "ilm-history-store") .setBulkActions(100) .setBulkSize(new ByteSizeValue(5, ByteSizeUnit.MB)) .setFlushInterval(TimeValue.timeValueSeconds(5)) @@ -124,8 +136,11 @@ public void afterBulk(long executionId, BulkRequest request, Throwable failure) */ public void putAsync(ILMHistoryItem item) { if (ilmHistoryEnabled == false) { - logger.trace("not recording ILM history item because [{}] is [false]: [{}]", - LIFECYCLE_HISTORY_INDEX_ENABLED_SETTING.getKey(), item); + logger.trace( + "not recording ILM history item because [{}] is [false]: [{}]", + LIFECYCLE_HISTORY_INDEX_ENABLED_SETTING.getKey(), + item + ); return; } logger.trace("queueing ILM history item for indexing [{}]: [{}]", ILM_HISTORY_DATA_STREAM, item); @@ -133,19 +148,27 @@ public void putAsync(ILMHistoryItem item) { item.toXContent(builder, ToXContent.EMPTY_PARAMS); IndexRequest request = new IndexRequest(ILM_HISTORY_DATA_STREAM).source(builder).opType(DocWriteRequest.OpType.CREATE); // TODO: remove the threadpool wrapping when the .add call is non-blocking - // (it can currently execute the bulk request occasionally) - // see: https://github.com/elastic/elasticsearch/issues/50440 + // (it can currently execute the bulk request occasionally) + // see: https://github.com/elastic/elasticsearch/issues/50440 threadPool.executor(ThreadPool.Names.GENERIC).execute(() -> { try { processor.add(request); } catch (Exception e) { - logger.error(new ParameterizedMessage("failed add ILM history item to queue for index [{}]: [{}]", - ILM_HISTORY_DATA_STREAM, item), e); + logger.error( + new ParameterizedMessage( + "failed add ILM history item to queue for index [{}]: [{}]", + ILM_HISTORY_DATA_STREAM, + item + ), + e + ); } }); } catch (IOException exception) { - logger.error(new ParameterizedMessage("failed to queue ILM history item in index [{}]: [{}]", - ILM_HISTORY_DATA_STREAM, item), exception); + logger.error( + new ParameterizedMessage("failed to queue ILM history item in index [{}]: [{}]", ILM_HISTORY_DATA_STREAM, item), + exception + ); } } diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/history/ILMHistoryTemplateRegistry.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/history/ILMHistoryTemplateRegistry.java index f31bd9eaa6fc7..acbf7d3edf1e4 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/history/ILMHistoryTemplateRegistry.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/history/ILMHistoryTemplateRegistry.java @@ -10,8 +10,8 @@ import org.elasticsearch.client.Client; import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.ClientHelper; import org.elasticsearch.xpack.core.ilm.LifecycleSettings; import org.elasticsearch.xpack.core.template.IndexTemplateConfig; @@ -58,9 +58,13 @@ protected boolean requiresMasterNode() { private final boolean ilmHistoryEnabled; - public ILMHistoryTemplateRegistry(Settings nodeSettings, ClusterService clusterService, - ThreadPool threadPool, Client client, - NamedXContentRegistry xContentRegistry) { + public ILMHistoryTemplateRegistry( + Settings nodeSettings, + ClusterService clusterService, + ThreadPool threadPool, + Client client, + NamedXContentRegistry xContentRegistry + ) { super(nodeSettings, clusterService, threadPool, client, xContentRegistry); this.ilmHistoryEnabled = LifecycleSettings.LIFECYCLE_HISTORY_INDEX_ENABLED_SETTING.get(nodeSettings); } diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/package-info.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/package-info.java index dd2e8394a436f..6f2d87d7497b6 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/package-info.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/package-info.java @@ -94,4 +94,3 @@ * */ package org.elasticsearch.xpack.ilm; - diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/SLMUsageTransportAction.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/SLMUsageTransportAction.java index 26d53a99ecda4..a54dfdd8f1714 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/SLMUsageTransportAction.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/SLMUsageTransportAction.java @@ -26,15 +26,30 @@ public class SLMUsageTransportAction extends XPackUsageFeatureTransportAction { @Inject - public SLMUsageTransportAction(TransportService transportService, ClusterService clusterService, ThreadPool threadPool, - ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver) { - super(XPackUsageFeatureAction.SNAPSHOT_LIFECYCLE.name(), transportService, clusterService, threadPool, actionFilters, - indexNameExpressionResolver); + public SLMUsageTransportAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver + ) { + super( + XPackUsageFeatureAction.SNAPSHOT_LIFECYCLE.name(), + transportService, + clusterService, + threadPool, + actionFilters, + indexNameExpressionResolver + ); } @Override - protected void masterOperation(Task task, XPackUsageRequest request, ClusterState state, - ActionListener listener) { + protected void masterOperation( + Task task, + XPackUsageRequest request, + ClusterState state, + ActionListener listener + ) { final SnapshotLifecycleMetadata slmMeta = state.metadata().custom(SnapshotLifecycleMetadata.TYPE); final SLMFeatureSetUsage usage = new SLMFeatureSetUsage(slmMeta == null ? null : slmMeta.getStats()); listener.onResponse(new XPackUsageFeatureResponse(usage)); diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/SnapshotLifecycleService.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/SnapshotLifecycleService.java index 79e3e2b9255f0..7068993999958 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/SnapshotLifecycleService.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/SnapshotLifecycleService.java @@ -15,8 +15,8 @@ import org.elasticsearch.cluster.metadata.RepositoriesMetadata; import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.ConcurrentCollections; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.xpack.core.ilm.LifecycleSettings; import org.elasticsearch.xpack.core.ilm.OperationMode; import org.elasticsearch.xpack.core.scheduler.CronSchedule; @@ -53,10 +53,12 @@ public class SnapshotLifecycleService implements Closeable, ClusterStateListener private final AtomicBoolean running = new AtomicBoolean(true); private volatile boolean isMaster = false; - public SnapshotLifecycleService(Settings settings, - Supplier taskSupplier, - ClusterService clusterService, - Clock clock) { + public SnapshotLifecycleService( + Settings settings, + Supplier taskSupplier, + ClusterService clusterService, + Clock clock + ) { this.scheduler = new SchedulerEngine(settings, clock); this.clusterService = clusterService; this.snapshotTask = taskSupplier.get(); @@ -146,14 +148,14 @@ public void cleanupDeletedPolicies(final ClusterState state) { SnapshotLifecycleMetadata snapMeta = state.metadata().custom(SnapshotLifecycleMetadata.TYPE); if (snapMeta != null) { // Retrieve all of the expected policy job ids from the policies in the metadata - final Set policyJobIds = snapMeta.getSnapshotConfigurations().values().stream() + final Set policyJobIds = snapMeta.getSnapshotConfigurations() + .values() + .stream() .map(SnapshotLifecycleService::getJobId) .collect(Collectors.toSet()); // Cancel all jobs that are *NOT* in the scheduled tasks map - scheduledTasks.keySet().stream() - .filter(jobId -> policyJobIds.contains(jobId) == false) - .forEach(this::cancelScheduledSnapshot); + scheduledTasks.keySet().stream().filter(jobId -> policyJobIds.contains(jobId) == false).forEach(this::cancelScheduledSnapshot); } } @@ -171,7 +173,8 @@ public void maybeScheduleSnapshot(final SnapshotLifecyclePolicyMetadata snapshot final Pattern existingJobPattern = Pattern.compile(snapshotLifecyclePolicy.getPolicy().getId() + JOB_PATTERN_SUFFIX); // Find and cancel any existing jobs for this policy - final boolean existingJobsFoundAndCancelled = scheduledTasks.keySet().stream() + final boolean existingJobsFoundAndCancelled = scheduledTasks.keySet() + .stream() // Find all jobs matching the `jobid-\d+` pattern .filter(jId -> existingJobPattern.matcher(jId).matches()) // Filter out a job that has not been changed (matches the id exactly meaning the version is the same) @@ -190,8 +193,10 @@ public void maybeScheduleSnapshot(final SnapshotLifecyclePolicyMetadata snapshot // is identical to an existing job (meaning the version has not changed) then this does // not reschedule it. scheduledTasks.computeIfAbsent(jobId, id -> { - final SchedulerEngine.Job job = new SchedulerEngine.Job(jobId, - new CronSchedule(snapshotLifecyclePolicy.getPolicy().getSchedule())); + final SchedulerEngine.Job job = new SchedulerEngine.Job( + jobId, + new CronSchedule(snapshotLifecyclePolicy.getPolicy().getSchedule()) + ); if (existingJobsFoundAndCancelled) { logger.info("rescheduling updated snapshot lifecycle job [{}]", jobId); } else { @@ -246,8 +251,14 @@ public static void validateMinimumInterval(final SnapshotLifecyclePolicy lifecyc TimeValue minimum = LifecycleSettings.SLM_MINIMUM_INTERVAL_SETTING.get(state.metadata().settings()); TimeValue next = lifecycle.calculateNextInterval(); if (next.duration() > 0 && minimum.duration() > 0 && next.millis() < minimum.millis()) { - throw new IllegalArgumentException("invalid schedule [" + lifecycle.getSchedule() + "]: " + - "schedule would be too frequent, executing more than every [" + minimum.getStringRep() + "]"); + throw new IllegalArgumentException( + "invalid schedule [" + + lifecycle.getSchedule() + + "]: " + + "schedule would be too frequent, executing more than every [" + + minimum.getStringRep() + + "]" + ); } } diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/SnapshotLifecycleTask.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/SnapshotLifecycleTask.java index 3b5e4a9462d19..809d74309be47 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/SnapshotLifecycleTask.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/SnapshotLifecycleTask.java @@ -22,11 +22,11 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.snapshots.SnapshotException; +import org.elasticsearch.snapshots.SnapshotInfo; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.snapshots.SnapshotException; -import org.elasticsearch.snapshots.SnapshotInfo; import org.elasticsearch.xpack.core.ClientHelper; import org.elasticsearch.xpack.core.scheduler.SchedulerEngine; import org.elasticsearch.xpack.core.slm.SnapshotInvocationRecord; @@ -67,9 +67,13 @@ public void triggered(SchedulerEngine.Event event) { final Optional snapshotName = maybeTakeSnapshot(event.getJobName(), client, clusterService, historyStore); // Would be cleaner if we could use Optional#ifPresentOrElse - snapshotName.ifPresent(name -> - logger.info("snapshot lifecycle policy job [{}] issued new snapshot creation for [{}] successfully", - event.getJobName(), name)); + snapshotName.ifPresent( + name -> logger.info( + "snapshot lifecycle policy job [{}] issued new snapshot creation for [{}] successfully", + event.getJobName(), + name + ) + ); if (snapshotName.isPresent() == false) { logger.warn("snapshot lifecycle policy for job [{}] no longer exists, snapshot not created", event.getJobName()); @@ -82,36 +86,55 @@ public void triggered(SchedulerEngine.Event event) { * state in the policy's metadata * @return An optional snapshot name if the request was issued successfully */ - public static Optional maybeTakeSnapshot(final String jobId, final Client client, final ClusterService clusterService, - final SnapshotHistoryStore historyStore) { + public static Optional maybeTakeSnapshot( + final String jobId, + final Client client, + final ClusterService clusterService, + final SnapshotHistoryStore historyStore + ) { Optional maybeMetadata = getSnapPolicyMetadata(jobId, clusterService.state()); String snapshotName = maybeMetadata.map(policyMetadata -> { // don't time out on this request to not produce failed SLM runs in case of a temporarily slow master node CreateSnapshotRequest request = policyMetadata.getPolicy().toRequest().masterNodeTimeout(TimeValue.MAX_VALUE); - final LifecyclePolicySecurityClient clientWithHeaders = new LifecyclePolicySecurityClient(client, - ClientHelper.INDEX_LIFECYCLE_ORIGIN, policyMetadata.getHeaders()); - logger.info("snapshot lifecycle policy [{}] issuing create snapshot [{}]", - policyMetadata.getPolicy().getId(), request.snapshot()); + final LifecyclePolicySecurityClient clientWithHeaders = new LifecyclePolicySecurityClient( + client, + ClientHelper.INDEX_LIFECYCLE_ORIGIN, + policyMetadata.getHeaders() + ); + logger.info( + "snapshot lifecycle policy [{}] issuing create snapshot [{}]", + policyMetadata.getPolicy().getId(), + request.snapshot() + ); clientWithHeaders.admin().cluster().createSnapshot(request, new ActionListener<>() { @Override public void onResponse(CreateSnapshotResponse createSnapshotResponse) { - logger.debug("snapshot response for [{}]: {}", - policyMetadata.getPolicy().getId(), Strings.toString(createSnapshotResponse)); + logger.debug( + "snapshot response for [{}]: {}", + policyMetadata.getPolicy().getId(), + Strings.toString(createSnapshotResponse) + ); final SnapshotInfo snapInfo = createSnapshotResponse.getSnapshotInfo(); // Check that there are no failed shards, since the request may not entirely // fail, but may still have failures (such as in the case of an aborted snapshot) if (snapInfo.failedShards() == 0) { long snapshotStartTime = snapInfo.startTime(); final long timestamp = Instant.now().toEpochMilli(); - clusterService.submitStateUpdateTask("slm-record-success-" + policyMetadata.getPolicy().getId(), - WriteJobStatus.success(policyMetadata.getPolicy().getId(), request.snapshot(), snapshotStartTime, timestamp)); - historyStore.putAsync(SnapshotHistoryItem.creationSuccessRecord(timestamp, policyMetadata.getPolicy(), - request.snapshot())); + clusterService.submitStateUpdateTask( + "slm-record-success-" + policyMetadata.getPolicy().getId(), + WriteJobStatus.success(policyMetadata.getPolicy().getId(), request.snapshot(), snapshotStartTime, timestamp) + ); + historyStore.putAsync( + SnapshotHistoryItem.creationSuccessRecord(timestamp, policyMetadata.getPolicy(), request.snapshot()) + ); } else { int failures = snapInfo.failedShards(); int total = snapInfo.totalShards(); - final SnapshotException e = new SnapshotException(request.repository(), request.snapshot(), - "failed to create snapshot successfully, " + failures + " out of " + total + " total shards failed"); + final SnapshotException e = new SnapshotException( + request.repository(), + request.snapshot(), + "failed to create snapshot successfully, " + failures + " out of " + total + " total shards failed" + ); // Add each failed shard's exception as suppressed, the exception contains // information about which shard failed snapInfo.shardFailures().forEach(failure -> e.addSuppressed(failure.getCause())); @@ -122,21 +145,30 @@ public void onResponse(CreateSnapshotResponse createSnapshotResponse) { @Override public void onFailure(Exception e) { - logger.error("failed to create snapshot for snapshot lifecycle policy [{}]: {}", - policyMetadata.getPolicy().getId(), e); + logger.error("failed to create snapshot for snapshot lifecycle policy [{}]: {}", policyMetadata.getPolicy().getId(), e); final long timestamp = Instant.now().toEpochMilli(); - clusterService.submitStateUpdateTask("slm-record-failure-" + policyMetadata.getPolicy().getId(), - WriteJobStatus.failure(policyMetadata.getPolicy().getId(), request.snapshot(), timestamp, e)); + clusterService.submitStateUpdateTask( + "slm-record-failure-" + policyMetadata.getPolicy().getId(), + WriteJobStatus.failure(policyMetadata.getPolicy().getId(), request.snapshot(), timestamp, e) + ); final SnapshotHistoryItem failureRecord; try { - failureRecord = SnapshotHistoryItem.creationFailureRecord(timestamp, policyMetadata.getPolicy(), - request.snapshot(), e); + failureRecord = SnapshotHistoryItem.creationFailureRecord( + timestamp, + policyMetadata.getPolicy(), + request.snapshot(), + e + ); historyStore.putAsync(failureRecord); } catch (IOException ex) { // This shouldn't happen unless there's an issue with serializing the original exception, which shouldn't happen - logger.error(new ParameterizedMessage( - "failed to record snapshot creation failure for snapshot lifecycle policy [{}]", - policyMetadata.getPolicy().getId()), e); + logger.error( + new ParameterizedMessage( + "failed to record snapshot creation failure for snapshot lifecycle policy [{}]", + policyMetadata.getPolicy().getId() + ), + e + ); } } }); @@ -150,19 +182,23 @@ public void onFailure(Exception e) { * For the given job id, return an optional policy metadata object, if one exists */ static Optional getSnapPolicyMetadata(final String jobId, final ClusterState state) { - return Optional.ofNullable((SnapshotLifecycleMetadata) state.metadata().custom(SnapshotLifecycleMetadata.TYPE)) - .map(SnapshotLifecycleMetadata::getSnapshotConfigurations) - .flatMap(configMap -> configMap.values().stream() - .filter(policyMeta -> jobId.equals(SnapshotLifecycleService.getJobId(policyMeta))) - .findFirst()); + return Optional.ofNullable((SnapshotLifecycleMetadata) state.metadata().custom(SnapshotLifecycleMetadata.TYPE)) + .map(SnapshotLifecycleMetadata::getSnapshotConfigurations) + .flatMap( + configMap -> configMap.values() + .stream() + .filter(policyMeta -> jobId.equals(SnapshotLifecycleService.getJobId(policyMeta))) + .findFirst() + ); } /** * A cluster state update task to write the result of a snapshot job to the cluster metadata for the associated policy. */ private static class WriteJobStatus extends ClusterStateUpdateTask { - private static final ToXContent.Params STACKTRACE_PARAMS = - new ToXContent.MapParams(Collections.singletonMap(REST_EXCEPTION_SKIP_STACK_TRACE, "false")); + private static final ToXContent.Params STACKTRACE_PARAMS = new ToXContent.MapParams( + Collections.singletonMap(REST_EXCEPTION_SKIP_STACK_TRACE, "false") + ); private final String policyName; private final String snapshotName; @@ -170,8 +206,13 @@ private static class WriteJobStatus extends ClusterStateUpdateTask { private final long snapshotFinishTime; private final Optional exception; - private WriteJobStatus(String policyName, String snapshotName, long snapshotStartTime, long snapshotFinishTime, - Optional exception) { + private WriteJobStatus( + String policyName, + String snapshotName, + long snapshotStartTime, + long snapshotFinishTime, + Optional exception + ) { this.policyName = policyName; this.snapshotName = snapshotName; this.exception = exception; @@ -205,16 +246,24 @@ public ClusterState execute(ClusterState currentState) throws Exception { assert snapMeta != null : "this should never be called while the snapshot lifecycle cluster metadata is null"; if (snapMeta == null) { - logger.error("failed to record snapshot [{}] for snapshot [{}] in policy [{}]: snapshot lifecycle metadata is null", - exception.isPresent() ? "failure" : "success", snapshotName, policyName); + logger.error( + "failed to record snapshot [{}] for snapshot [{}] in policy [{}]: snapshot lifecycle metadata is null", + exception.isPresent() ? "failure" : "success", + snapshotName, + policyName + ); return currentState; } Map snapLifecycles = new HashMap<>(snapMeta.getSnapshotConfigurations()); SnapshotLifecyclePolicyMetadata policyMetadata = snapLifecycles.get(policyName); if (policyMetadata == null) { - logger.warn("failed to record snapshot [{}] for snapshot [{}] in policy [{}]: policy not found", - exception.isPresent() ? "failure" : "success", snapshotName, policyName); + logger.warn( + "failed to record snapshot [{}] for snapshot [{}] in policy [{}]: policy not found", + exception.isPresent() ? "failure" : "success", + snapshotName, + policyName + ); return currentState; } @@ -223,27 +272,29 @@ public ClusterState execute(ClusterState currentState) throws Exception { if (exception.isPresent()) { stats.snapshotFailed(policyName); - newPolicyMetadata.setLastFailure(new SnapshotInvocationRecord(snapshotName, null, snapshotFinishTime, - exceptionToString())); + newPolicyMetadata.setLastFailure(new SnapshotInvocationRecord(snapshotName, null, snapshotFinishTime, exceptionToString())); } else { stats.snapshotTaken(policyName); newPolicyMetadata.setLastSuccess(new SnapshotInvocationRecord(snapshotName, snapshotStartTime, snapshotFinishTime, null)); } snapLifecycles.put(policyName, newPolicyMetadata.build()); - SnapshotLifecycleMetadata lifecycleMetadata = new SnapshotLifecycleMetadata(snapLifecycles, - snapMeta.getOperationMode(), stats); + SnapshotLifecycleMetadata lifecycleMetadata = new SnapshotLifecycleMetadata(snapLifecycles, snapMeta.getOperationMode(), stats); Metadata currentMeta = currentState.metadata(); return ClusterState.builder(currentState) - .metadata(Metadata.builder(currentMeta) - .putCustom(SnapshotLifecycleMetadata.TYPE, lifecycleMetadata)) + .metadata(Metadata.builder(currentMeta).putCustom(SnapshotLifecycleMetadata.TYPE, lifecycleMetadata)) .build(); } @Override public void onFailure(String source, Exception e) { - logger.error("failed to record snapshot policy execution status for snapshot [{}] in policy [{}], (source: [{}]): {}", - snapshotName, policyName, source, e); + logger.error( + "failed to record snapshot policy execution status for snapshot [{}] in policy [{}], (source: [{}]): {}", + snapshotName, + policyName, + source, + e + ); } } } diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/SnapshotRetentionService.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/SnapshotRetentionService.java index 4029e6de91ad6..f73d8b2ba4278 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/SnapshotRetentionService.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/SnapshotRetentionService.java @@ -44,9 +44,7 @@ public class SnapshotRetentionService implements LocalNodeMasterListener, Closea private volatile String slmRetentionSchedule; private volatile boolean isMaster = false; - public SnapshotRetentionService(Settings settings, - Supplier taskSupplier, - Clock clock) { + public SnapshotRetentionService(Settings settings, Supplier taskSupplier, Clock clock) { this.clock = clock; this.scheduler = new SchedulerEngine(settings, clock); this.retentionTask = taskSupplier.get(); @@ -59,8 +57,8 @@ public SnapshotRetentionService(Settings settings, */ public void init(ClusterService clusterService) { clusterService.addLocalNodeMasterListener(this); - clusterService.getClusterSettings().addSettingsUpdateConsumer(LifecycleSettings.SLM_RETENTION_SCHEDULE_SETTING, - this::setUpdateSchedule); + clusterService.getClusterSettings() + .addSettingsUpdateConsumer(LifecycleSettings.SLM_RETENTION_SCHEDULE_SETTING, this::setUpdateSchedule); } void setUpdateSchedule(String retentionSchedule) { @@ -89,8 +87,7 @@ public void offMaster() { private void rescheduleRetentionJob() { final String schedule = this.slmRetentionSchedule; if (this.running.get() && this.isMaster && Strings.hasText(schedule)) { - final SchedulerEngine.Job retentionJob = new SchedulerEngine.Job(SLM_RETENTION_JOB_ID, - new CronSchedule(schedule)); + final SchedulerEngine.Job retentionJob = new SchedulerEngine.Job(SLM_RETENTION_JOB_ID, new CronSchedule(schedule)); logger.debug("scheduling SLM retention job for [{}]", schedule); this.scheduler.add(retentionJob); } else { diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/SnapshotRetentionTask.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/SnapshotRetentionTask.java index 6a685e42a64ab..c62e799dc2a88 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/SnapshotRetentionTask.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/SnapshotRetentionTask.java @@ -18,8 +18,8 @@ import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.Strings; -import org.elasticsearch.core.Tuple; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.core.Tuple; import org.elasticsearch.snapshots.SnapshotId; import org.elasticsearch.snapshots.SnapshotInfo; import org.elasticsearch.snapshots.SnapshotState; @@ -61,8 +61,11 @@ public class SnapshotRetentionTask implements SchedulerEngine.Listener { private static final Logger logger = LogManager.getLogger(SnapshotRetentionTask.class); - private static final Set RETAINABLE_STATES = - EnumSet.of(SnapshotState.SUCCESS, SnapshotState.FAILED, SnapshotState.PARTIAL); + private static final Set RETAINABLE_STATES = EnumSet.of( + SnapshotState.SUCCESS, + SnapshotState.FAILED, + SnapshotState.PARTIAL + ); private final Client client; private final ClusterService clusterService; @@ -74,8 +77,12 @@ public class SnapshotRetentionTask implements SchedulerEngine.Listener { */ private final Set runningDeletions = Collections.synchronizedSet(new HashSet<>()); - public SnapshotRetentionTask(Client client, ClusterService clusterService, LongSupplier nowNanoSupplier, - SnapshotHistoryStore historyStore) { + public SnapshotRetentionTask( + Client client, + ClusterService clusterService, + LongSupplier nowNanoSupplier, + SnapshotHistoryStore historyStore + ) { this.client = new OriginSettingClient(client, ClientHelper.INDEX_LIFECYCLE_ORIGIN); this.clusterService = clusterService; this.nowNanoSupplier = nowNanoSupplier; @@ -83,27 +90,31 @@ public SnapshotRetentionTask(Client client, ClusterService clusterService, LongS } private static String formatSnapshots(Map> snapshotMap) { - return snapshotMap.entrySet().stream() - .map(e -> e.getKey() + ": [" + e.getValue().stream() - .map(si -> si.snapshotId().getName()) - .collect(Collectors.joining(",")) - + "]") + return snapshotMap.entrySet() + .stream() + .map( + e -> e.getKey() + ": [" + e.getValue().stream().map(si -> si.snapshotId().getName()).collect(Collectors.joining(",")) + "]" + ) .collect(Collectors.joining(",")); } @Override public void triggered(SchedulerEngine.Event event) { - assert event.getJobName().equals(SnapshotRetentionService.SLM_RETENTION_JOB_ID) || - event.getJobName().equals(SnapshotRetentionService.SLM_RETENTION_MANUAL_JOB_ID): - "expected id to be " + SnapshotRetentionService.SLM_RETENTION_JOB_ID + " or " + - SnapshotRetentionService.SLM_RETENTION_MANUAL_JOB_ID + " but it was " + event.getJobName(); + assert event.getJobName().equals(SnapshotRetentionService.SLM_RETENTION_JOB_ID) + || event.getJobName().equals(SnapshotRetentionService.SLM_RETENTION_MANUAL_JOB_ID) + : "expected id to be " + + SnapshotRetentionService.SLM_RETENTION_JOB_ID + + " or " + + SnapshotRetentionService.SLM_RETENTION_MANUAL_JOB_ID + + " but it was " + + event.getJobName(); final ClusterState state = clusterService.state(); // Skip running retention if SLM is disabled, however, even if it's // disabled we allow manual running. - if (SnapshotLifecycleService.slmStoppedOrStopping(state) && - event.getJobName().equals(SnapshotRetentionService.SLM_RETENTION_MANUAL_JOB_ID) == false) { + if (SnapshotLifecycleService.slmStoppedOrStopping(state) + && event.getJobName().equals(SnapshotRetentionService.SLM_RETENTION_MANUAL_JOB_ID) == false) { logger.debug("skipping SLM retention as SLM is currently stopped or stopping"); return; } @@ -130,9 +141,10 @@ public void triggered(SchedulerEngine.Event event) { // For those policies (there may be more than one for the same repo), // return the repos that we need to get the snapshots for - final Set repositioriesToFetch = policiesWithRetention.values().stream() - .map(SnapshotLifecyclePolicy::getRepository) - .collect(Collectors.toSet()); + final Set repositioriesToFetch = policiesWithRetention.values() + .stream() + .map(SnapshotLifecyclePolicy::getRepository) + .collect(Collectors.toSet()); logger.trace("fetching snapshots from repositories: {}", repositioriesToFetch); if (repositioriesToFetch.isEmpty()) { @@ -149,14 +161,20 @@ public void onResponse(Map> allSnapshots) { logger.trace("retrieved snapshots: [{}]", formatSnapshots(allSnapshots)); } // Find all the snapshots that are past their retention date - final Map>> snapshotsToBeDeleted = allSnapshots.entrySet().stream() - .collect(Collectors.toMap(Map.Entry::getKey, - e -> e.getValue().stream() - .filter(snapshot -> snapshotEligibleForDeletion(snapshot, allSnapshots, policiesWithRetention)) - // SnapshotInfo instances can be quite large in case they contain e.g. a large collection of - // exceptions so we extract the only two things (id + policy id) here so they can be GCed - .map(snapshotInfo -> Tuple.tuple(snapshotInfo.snapshotId(), getPolicyId(snapshotInfo))) - .collect(Collectors.toList()))); + final Map>> snapshotsToBeDeleted = allSnapshots.entrySet() + .stream() + .collect( + Collectors.toMap( + Map.Entry::getKey, + e -> e.getValue() + .stream() + .filter(snapshot -> snapshotEligibleForDeletion(snapshot, allSnapshots, policiesWithRetention)) + // SnapshotInfo instances can be quite large in case they contain e.g. a large collection of + // exceptions so we extract the only two things (id + policy id) here so they can be GCed + .map(snapshotInfo -> Tuple.tuple(snapshotInfo.snapshotId(), getPolicyId(snapshotInfo))) + .collect(Collectors.toList()) + ) + ); if (logger.isTraceEnabled()) { logger.trace("snapshots eligible for deletion: [{}]", snapshotsToBeDeleted); @@ -184,16 +202,21 @@ static Map getAllPoliciesWithRetentionEnabled(f if (snapMeta == null) { return Collections.emptyMap(); } - return snapMeta.getSnapshotConfigurations().entrySet().stream() + return snapMeta.getSnapshotConfigurations() + .entrySet() + .stream() .filter(e -> e.getValue().getPolicy().getRetentionPolicy() != null) .filter(e -> e.getValue().getPolicy().getRetentionPolicy().equals(SnapshotRetentionConfiguration.EMPTY) == false) .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().getPolicy())); } - static boolean snapshotEligibleForDeletion(SnapshotInfo snapshot, Map> allSnapshots, - Map policies) { - assert snapshot.userMetadata() != null : "snapshots without user metadata should have gotten filtered by the caller but saw [" - + snapshot + "]"; + static boolean snapshotEligibleForDeletion( + SnapshotInfo snapshot, + Map> allSnapshots, + Map policies + ) { + assert snapshot.userMetadata() != null + : "snapshots without user metadata should have gotten filtered by the caller but saw [" + snapshot + "]"; final Object policyId = snapshot.userMetadata().get(POLICY_ID_METADATA_FIELD); assert policyId instanceof String : "snapshots without a policy id should have gotten filtered by the caller but saw [" + snapshot + "]"; @@ -212,57 +235,68 @@ static boolean snapshotEligibleForDeletion(SnapshotInfo snapshot, Map Optional.ofNullable(info.userMetadata()) - .map(meta -> meta.get(POLICY_ID_METADATA_FIELD)) - .map(pId -> pId.equals(policyId)) - .orElse(false)) - .collect(Collectors.toList())) - .test(snapshot); - logger.debug("[{}] testing snapshot [{}] deletion eligibility: {}", - repository, snapshot.snapshotId(), eligible ? "ELIGIBLE" : "INELIGIBLE"); + allSnapshots.get(repository) + .stream() + .filter( + info -> Optional.ofNullable(info.userMetadata()) + .map(meta -> meta.get(POLICY_ID_METADATA_FIELD)) + .map(pId -> pId.equals(policyId)) + .orElse(false) + ) + .collect(Collectors.toList()) + ).test(snapshot); + logger.debug( + "[{}] testing snapshot [{}] deletion eligibility: {}", + repository, + snapshot.snapshotId(), + eligible ? "ELIGIBLE" : "INELIGIBLE" + ); return eligible; } - void getAllRetainableSnapshots(Collection repositories, - Set policies, - ActionListener>> listener) { + void getAllRetainableSnapshots( + Collection repositories, + Set policies, + ActionListener>> listener + ) { if (repositories.isEmpty()) { // Skip retrieving anything if there are no repositories to fetch listener.onResponse(Collections.emptyMap()); return; } - client.admin().cluster() + client.admin() + .cluster() .prepareGetSnapshots(repositories.toArray(Strings.EMPTY_ARRAY)) // don't time out on this request to not produce failed SLM runs in case of a temporarily slow master node .setMasterNodeTimeout(TimeValue.MAX_VALUE) .setIgnoreUnavailable(true) .setPolicies(policies.toArray(Strings.EMPTY_ARRAY)) .execute(ActionListener.wrap(resp -> { - if (logger.isTraceEnabled()) { - logger.trace("retrieved snapshots: {}", - repositories.stream() - .flatMap(repo -> - resp.getSnapshots() - .stream() - .filter(info -> repo.equals(info.repository())) - .map(si -> si.snapshotId().getName()) - ).collect(Collectors.toList())); - } - Map> snapshots = new HashMap<>(); - for (SnapshotInfo info : resp.getSnapshots()) { - if (RETAINABLE_STATES.contains(info.state()) && info.userMetadata() != null) { - snapshots.computeIfAbsent(info.repository(), repo -> new ArrayList<>()).add(info); - } + if (logger.isTraceEnabled()) { + logger.trace( + "retrieved snapshots: {}", + repositories.stream() + .flatMap( + repo -> resp.getSnapshots() + .stream() + .filter(info -> repo.equals(info.repository())) + .map(si -> si.snapshotId().getName()) + ) + .collect(Collectors.toList()) + ); + } + Map> snapshots = new HashMap<>(); + for (SnapshotInfo info : resp.getSnapshots()) { + if (RETAINABLE_STATES.contains(info.state()) && info.userMetadata() != null) { + snapshots.computeIfAbsent(info.repository(), repo -> new ArrayList<>()).add(info); } - listener.onResponse(snapshots); - }, - e -> { - logger.debug(new ParameterizedMessage("unable to retrieve snapshots for [{}] repositories", repositories), e); - listener.onFailure(e); - }) - ); + } + listener.onResponse(snapshots); + }, e -> { + logger.debug(new ParameterizedMessage("unable to retrieve snapshots for [{}] repositories", repositories), e); + listener.onFailure(e); + })); } static String getPolicyId(SnapshotInfo snapshotInfo) { @@ -270,13 +304,16 @@ static String getPolicyId(SnapshotInfo snapshotInfo) { .filter(meta -> meta.get(POLICY_ID_METADATA_FIELD) != null) .filter(meta -> meta.get(POLICY_ID_METADATA_FIELD) instanceof String) .map(meta -> (String) meta.get(POLICY_ID_METADATA_FIELD)) - .orElseThrow(() -> new IllegalStateException("expected snapshot " + snapshotInfo + - " to have a policy in its metadata, but it did not")); + .orElseThrow( + () -> new IllegalStateException("expected snapshot " + snapshotInfo + " to have a policy in its metadata, but it did not") + ); } - void deleteSnapshots(Map>> snapshotsToDelete, - SnapshotLifecycleStats slmStats, - ActionListener listener) { + void deleteSnapshots( + Map>> snapshotsToDelete, + SnapshotLifecycleStats slmStats, + ActionListener listener + ) { int count = snapshotsToDelete.values().stream().mapToInt(List::size).sum(); if (count == 0) { listener.onResponse(null); @@ -288,13 +325,14 @@ void deleteSnapshots(Map>> snapshotsToDel long startTime = nowNanoSupplier.getAsLong(); final AtomicInteger deleted = new AtomicInteger(0); final AtomicInteger failed = new AtomicInteger(0); - final GroupedActionListener allDeletesListener = - new GroupedActionListener<>(ActionListener.runAfter(listener.map(v -> null), - () -> { - TimeValue totalElapsedTime = TimeValue.timeValueNanos(nowNanoSupplier.getAsLong() - startTime); - logger.debug("total elapsed time for deletion of [{}] snapshots: {}", deleted, totalElapsedTime); - slmStats.deletionTime(totalElapsedTime); - }), snapshotsToDelete.size()); + final GroupedActionListener allDeletesListener = new GroupedActionListener<>( + ActionListener.runAfter(listener.map(v -> null), () -> { + TimeValue totalElapsedTime = TimeValue.timeValueNanos(nowNanoSupplier.getAsLong() - startTime); + logger.debug("total elapsed time for deletion of [{}] snapshots: {}", deleted, totalElapsedTime); + slmStats.deletionTime(totalElapsedTime); + }), + snapshotsToDelete.size() + ); for (Map.Entry>> entry : snapshotsToDelete.entrySet()) { String repo = entry.getKey(); List> snapshots = entry.getValue(); @@ -304,11 +342,16 @@ void deleteSnapshots(Map>> snapshotsToDel } } - private void deleteSnapshots(SnapshotLifecycleStats slmStats, AtomicInteger deleted, AtomicInteger failed, String repo, - List> snapshots, ActionListener listener) { + private void deleteSnapshots( + SnapshotLifecycleStats slmStats, + AtomicInteger deleted, + AtomicInteger failed, + String repo, + List> snapshots, + ActionListener listener + ) { - final ActionListener allDeletesListener = - new GroupedActionListener<>(listener.map(v -> null), snapshots.size()); + final ActionListener allDeletesListener = new GroupedActionListener<>(listener.map(v -> null), snapshots.size()); for (Tuple info : snapshots) { final SnapshotId snapshotId = info.v1(); if (runningDeletions.add(snapshotId) == false) { @@ -321,34 +364,43 @@ private void deleteSnapshots(SnapshotLifecycleStats slmStats, AtomicInteger dele final String policyId = info.v2(); final long deleteStartTime = nowNanoSupplier.getAsLong(); // TODO: Use snapshot multi-delete instead of this loop if all nodes in the cluster support it - // i.e are newer or equal to SnapshotsService#MULTI_DELETE_VERSION - deleteSnapshot(policyId, repo, snapshotId, slmStats, ActionListener.runAfter( - ActionListener.wrap(acknowledgedResponse -> { - deleted.incrementAndGet(); - assert acknowledgedResponse.isAcknowledged(); - historyStore.putAsync(SnapshotHistoryItem.deletionSuccessRecord(Instant.now().toEpochMilli(), - snapshotId.getName(), policyId, repo)); - allDeletesListener.onResponse(null); - }, e -> { - failed.incrementAndGet(); - try { - final SnapshotHistoryItem result = SnapshotHistoryItem.deletionFailureRecord(Instant.now().toEpochMilli(), - snapshotId.getName(), policyId, repo, e); - historyStore.putAsync(result); - } catch (IOException ex) { - // This shouldn't happen unless there's an issue with serializing the original exception - logger.error(new ParameterizedMessage( - "failed to record snapshot deletion failure for snapshot lifecycle policy [{}]", - policyId), ex); - } finally { - allDeletesListener.onFailure(e); - } - }), () -> { - runningDeletions.remove(snapshotId); - long finishTime = nowNanoSupplier.getAsLong(); - TimeValue deletionTime = TimeValue.timeValueNanos(finishTime - deleteStartTime); - logger.debug("elapsed time for deletion of [{}] snapshot: {}", snapshotId, deletionTime); - })); + // i.e are newer or equal to SnapshotsService#MULTI_DELETE_VERSION + deleteSnapshot(policyId, repo, snapshotId, slmStats, ActionListener.runAfter(ActionListener.wrap(acknowledgedResponse -> { + deleted.incrementAndGet(); + assert acknowledgedResponse.isAcknowledged(); + historyStore.putAsync( + SnapshotHistoryItem.deletionSuccessRecord(Instant.now().toEpochMilli(), snapshotId.getName(), policyId, repo) + ); + allDeletesListener.onResponse(null); + }, e -> { + failed.incrementAndGet(); + try { + final SnapshotHistoryItem result = SnapshotHistoryItem.deletionFailureRecord( + Instant.now().toEpochMilli(), + snapshotId.getName(), + policyId, + repo, + e + ); + historyStore.putAsync(result); + } catch (IOException ex) { + // This shouldn't happen unless there's an issue with serializing the original exception + logger.error( + new ParameterizedMessage( + "failed to record snapshot deletion failure for snapshot lifecycle policy [{}]", + policyId + ), + ex + ); + } finally { + allDeletesListener.onFailure(e); + } + }), () -> { + runningDeletions.remove(snapshotId); + long finishTime = nowNanoSupplier.getAsLong(); + TimeValue deletionTime = TimeValue.timeValueNanos(finishTime - deleteStartTime); + logger.debug("elapsed time for deletion of [{}] snapshot: {}", snapshotId, deletionTime); + })); success = true; } catch (Exception e) { listener.onFailure(e); @@ -368,24 +420,30 @@ private void deleteSnapshots(SnapshotLifecycleStats slmStats, AtomicInteger dele * @param listener {@link ActionListener#onResponse(Object)} is called if a {@link SnapshotHistoryItem} can be created representing a * successful or failed deletion call. {@link ActionListener#onFailure(Exception)} is called only if interrupted. */ - void deleteSnapshot(String slmPolicy, String repo, SnapshotId snapshot, SnapshotLifecycleStats slmStats, - ActionListener listener) { + void deleteSnapshot( + String slmPolicy, + String repo, + SnapshotId snapshot, + SnapshotLifecycleStats slmStats, + ActionListener listener + ) { logger.info("[{}] snapshot retention deleting snapshot [{}]", repo, snapshot); // don't time out on this request to not produce failed SLM runs in case of a temporarily slow master node - client.admin().cluster().prepareDeleteSnapshot(repo, snapshot.getName()).setMasterNodeTimeout(TimeValue.MAX_VALUE).execute( - ActionListener.wrap(acknowledgedResponse -> { - slmStats.snapshotDeleted(slmPolicy); - listener.onResponse(acknowledgedResponse); - }, - e -> { - try { - logger.warn(new ParameterizedMessage("[{}] failed to delete snapshot [{}] for retention", - repo, snapshot), e); - slmStats.snapshotDeleteFailure(slmPolicy); - } finally { - listener.onFailure(e); - } - })); + client.admin() + .cluster() + .prepareDeleteSnapshot(repo, snapshot.getName()) + .setMasterNodeTimeout(TimeValue.MAX_VALUE) + .execute(ActionListener.wrap(acknowledgedResponse -> { + slmStats.snapshotDeleted(slmPolicy); + listener.onResponse(acknowledgedResponse); + }, e -> { + try { + logger.warn(new ParameterizedMessage("[{}] failed to delete snapshot [{}] for retention", repo, snapshot), e); + slmStats.snapshotDeleteFailure(slmPolicy); + } finally { + listener.onFailure(e); + } + })); } void updateStateWithStats(SnapshotLifecycleStats newStats) { diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/UpdateSnapshotLifecycleStatsTask.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/UpdateSnapshotLifecycleStatsTask.java index 4b621e279f554..45b839c3bdf7c 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/UpdateSnapshotLifecycleStatsTask.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/UpdateSnapshotLifecycleStatsTask.java @@ -40,19 +40,26 @@ public ClusterState execute(ClusterState currentState) { } SnapshotLifecycleStats newMetrics = currentSlmMeta.getStats().merge(runStats); - SnapshotLifecycleMetadata newSlmMeta = new SnapshotLifecycleMetadata(currentSlmMeta.getSnapshotConfigurations(), - currentSlmMeta.getOperationMode(), newMetrics); + SnapshotLifecycleMetadata newSlmMeta = new SnapshotLifecycleMetadata( + currentSlmMeta.getSnapshotConfigurations(), + currentSlmMeta.getOperationMode(), + newMetrics + ); return ClusterState.builder(currentState) - .metadata(Metadata.builder(currentMeta) - .putCustom(SnapshotLifecycleMetadata.TYPE, newSlmMeta)) + .metadata(Metadata.builder(currentMeta).putCustom(SnapshotLifecycleMetadata.TYPE, newSlmMeta)) .build(); } @Override public void onFailure(String source, Exception e) { - logger.error(new ParameterizedMessage("failed to update cluster state with snapshot lifecycle stats, " + - "source: [{}], missing stats: [{}]", source, runStats), - e); + logger.error( + new ParameterizedMessage( + "failed to update cluster state with snapshot lifecycle stats, " + "source: [{}], missing stats: [{}]", + source, + runStats + ), + e + ); } } diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/RestExecuteSnapshotLifecycleAction.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/RestExecuteSnapshotLifecycleAction.java index 13938bdec9f7e..74a3fcc7d80d9 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/RestExecuteSnapshotLifecycleAction.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/RestExecuteSnapshotLifecycleAction.java @@ -22,9 +22,7 @@ public class RestExecuteSnapshotLifecycleAction extends BaseRestHandler { @Override public List routes() { - return List.of( - new Route(POST, "/_slm/policy/{name}/_execute"), - new Route(PUT, "/_slm/policy/{name}/_execute")); + return List.of(new Route(POST, "/_slm/policy/{name}/_execute"), new Route(PUT, "/_slm/policy/{name}/_execute")); } @Override diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/RestGetSnapshotLifecycleAction.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/RestGetSnapshotLifecycleAction.java index 47db4afe49a23..1b1840d867499 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/RestGetSnapshotLifecycleAction.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/RestGetSnapshotLifecycleAction.java @@ -22,9 +22,7 @@ public class RestGetSnapshotLifecycleAction extends BaseRestHandler { @Override public List routes() { - return List.of( - new Route(GET, "/_slm/policy"), - new Route(GET, "/_slm/policy/{name}")); + return List.of(new Route(GET, "/_slm/policy"), new Route(GET, "/_slm/policy/{name}")); } @Override diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/RestPutSnapshotLifecycleAction.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/RestPutSnapshotLifecycleAction.java index 92f66a793e90d..ae23b1f58fe3b 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/RestPutSnapshotLifecycleAction.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/RestPutSnapshotLifecycleAction.java @@ -8,10 +8,10 @@ package org.elasticsearch.xpack.slm.action; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestToXContentListener; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.slm.action.PutSnapshotLifecycleAction; import java.io.IOException; diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/TransportDeleteSnapshotLifecycleAction.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/TransportDeleteSnapshotLifecycleAction.java index 6cd790b7cb89a..e419c28d969c0 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/TransportDeleteSnapshotLifecycleAction.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/TransportDeleteSnapshotLifecycleAction.java @@ -29,22 +29,40 @@ import java.util.Map; import java.util.stream.Collectors; -public class TransportDeleteSnapshotLifecycleAction extends - TransportMasterNodeAction { +public class TransportDeleteSnapshotLifecycleAction extends TransportMasterNodeAction< + DeleteSnapshotLifecycleAction.Request, + DeleteSnapshotLifecycleAction.Response> { @Inject - public TransportDeleteSnapshotLifecycleAction(TransportService transportService, ClusterService clusterService, ThreadPool threadPool, - ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver) { - super(DeleteSnapshotLifecycleAction.NAME, transportService, clusterService, threadPool, actionFilters, - DeleteSnapshotLifecycleAction.Request::new, indexNameExpressionResolver, DeleteSnapshotLifecycleAction.Response::new, - ThreadPool.Names.SAME); + public TransportDeleteSnapshotLifecycleAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver + ) { + super( + DeleteSnapshotLifecycleAction.NAME, + transportService, + clusterService, + threadPool, + actionFilters, + DeleteSnapshotLifecycleAction.Request::new, + indexNameExpressionResolver, + DeleteSnapshotLifecycleAction.Response::new, + ThreadPool.Names.SAME + ); } @Override - protected void masterOperation(Task task, DeleteSnapshotLifecycleAction.Request request, - ClusterState state, - ActionListener listener) throws Exception { - clusterService.submitStateUpdateTask("delete-snapshot-lifecycle-" + request.getLifecycleId(), + protected void masterOperation( + Task task, + DeleteSnapshotLifecycleAction.Request request, + ClusterState state, + ActionListener listener + ) throws Exception { + clusterService.submitStateUpdateTask( + "delete-snapshot-lifecycle-" + request.getLifecycleId(), new AckedClusterStateUpdateTask(request, listener) { @Override protected DeleteSnapshotLifecycleAction.Response newResponse(boolean acknowledged) { @@ -58,25 +76,38 @@ public ClusterState execute(ClusterState currentState) { throw new ResourceNotFoundException("snapshot lifecycle policy not found: {}", request.getLifecycleId()); } // Check that the policy exists in the first place - snapMeta.getSnapshotConfigurations().entrySet().stream() + snapMeta.getSnapshotConfigurations() + .entrySet() + .stream() .filter(e -> e.getValue().getPolicy().getId().equals(request.getLifecycleId())) .findAny() - .orElseThrow(() -> new ResourceNotFoundException("snapshot lifecycle policy not found: {}", - request.getLifecycleId())); + .orElseThrow( + () -> new ResourceNotFoundException("snapshot lifecycle policy not found: {}", request.getLifecycleId()) + ); - Map newConfigs = snapMeta.getSnapshotConfigurations().entrySet().stream() + Map newConfigs = snapMeta.getSnapshotConfigurations() + .entrySet() + .stream() .filter(e -> e.getKey().equals(request.getLifecycleId()) == false) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); Metadata metadata = currentState.metadata(); return ClusterState.builder(currentState) - .metadata(Metadata.builder(metadata) - .putCustom(SnapshotLifecycleMetadata.TYPE, - new SnapshotLifecycleMetadata(newConfigs, - snapMeta.getOperationMode(), snapMeta.getStats().removePolicy(request.getLifecycleId())))) + .metadata( + Metadata.builder(metadata) + .putCustom( + SnapshotLifecycleMetadata.TYPE, + new SnapshotLifecycleMetadata( + newConfigs, + snapMeta.getOperationMode(), + snapMeta.getStats().removePolicy(request.getLifecycleId()) + ) + ) + ) .build(); } - }); + } + ); } @Override diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/TransportExecuteSnapshotLifecycleAction.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/TransportExecuteSnapshotLifecycleAction.java index 3acb4ddf60650..bb85c836ba31a 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/TransportExecuteSnapshotLifecycleAction.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/TransportExecuteSnapshotLifecycleAction.java @@ -30,27 +30,45 @@ import java.util.Optional; -public class TransportExecuteSnapshotLifecycleAction - extends TransportMasterNodeAction { +public class TransportExecuteSnapshotLifecycleAction extends TransportMasterNodeAction< + ExecuteSnapshotLifecycleAction.Request, + ExecuteSnapshotLifecycleAction.Response> { private final Client client; private final SnapshotHistoryStore historyStore; @Inject - public TransportExecuteSnapshotLifecycleAction(TransportService transportService, ClusterService clusterService, ThreadPool threadPool, - ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver, - Client client, SnapshotHistoryStore historyStore) { - super(ExecuteSnapshotLifecycleAction.NAME, transportService, clusterService, threadPool, actionFilters, - ExecuteSnapshotLifecycleAction.Request::new, indexNameExpressionResolver, ExecuteSnapshotLifecycleAction.Response::new, - ThreadPool.Names.GENERIC); + public TransportExecuteSnapshotLifecycleAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver, + Client client, + SnapshotHistoryStore historyStore + ) { + super( + ExecuteSnapshotLifecycleAction.NAME, + transportService, + clusterService, + threadPool, + actionFilters, + ExecuteSnapshotLifecycleAction.Request::new, + indexNameExpressionResolver, + ExecuteSnapshotLifecycleAction.Response::new, + ThreadPool.Names.GENERIC + ); this.client = client; this.historyStore = historyStore; } @Override - protected void masterOperation(final Task task, final ExecuteSnapshotLifecycleAction.Request request, - final ClusterState state, - final ActionListener listener) { + protected void masterOperation( + final Task task, + final ExecuteSnapshotLifecycleAction.Request request, + final ClusterState state, + final ActionListener listener + ) { try { final String policyId = request.getLifecycleId(); SnapshotLifecycleMetadata snapMeta = state.metadata().custom(SnapshotLifecycleMetadata.TYPE, SnapshotLifecycleMetadata.EMPTY); @@ -60,8 +78,12 @@ protected void masterOperation(final Task task, final ExecuteSnapshotLifecycleAc return; } - final Optional snapshotName = SnapshotLifecycleTask.maybeTakeSnapshot(SnapshotLifecycleService.getJobId(policyMetadata), - client, clusterService, historyStore); + final Optional snapshotName = SnapshotLifecycleTask.maybeTakeSnapshot( + SnapshotLifecycleService.getJobId(policyMetadata), + client, + clusterService, + historyStore + ); if (snapshotName.isPresent()) { listener.onResponse(new ExecuteSnapshotLifecycleAction.Response(snapshotName.get())); } else { diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/TransportExecuteSnapshotRetentionAction.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/TransportExecuteSnapshotRetentionAction.java index 635d17919a7e6..7adb58bcf8a8c 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/TransportExecuteSnapshotRetentionAction.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/TransportExecuteSnapshotRetentionAction.java @@ -26,26 +26,41 @@ import org.elasticsearch.xpack.core.slm.action.ExecuteSnapshotRetentionAction; import org.elasticsearch.xpack.slm.SnapshotRetentionService; -public class TransportExecuteSnapshotRetentionAction - extends AcknowledgedTransportMasterNodeAction { +public class TransportExecuteSnapshotRetentionAction extends AcknowledgedTransportMasterNodeAction { private static final Logger logger = LogManager.getLogger(TransportExecuteSnapshotRetentionAction.class); private final SnapshotRetentionService retentionService; @Inject - public TransportExecuteSnapshotRetentionAction(TransportService transportService, ClusterService clusterService, ThreadPool threadPool, - ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver, - SnapshotRetentionService retentionService) { - super(ExecuteSnapshotRetentionAction.NAME, transportService, clusterService, threadPool, actionFilters, - ExecuteSnapshotRetentionAction.Request::new, indexNameExpressionResolver, ThreadPool.Names.GENERIC); + public TransportExecuteSnapshotRetentionAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver, + SnapshotRetentionService retentionService + ) { + super( + ExecuteSnapshotRetentionAction.NAME, + transportService, + clusterService, + threadPool, + actionFilters, + ExecuteSnapshotRetentionAction.Request::new, + indexNameExpressionResolver, + ThreadPool.Names.GENERIC + ); this.retentionService = retentionService; } @Override - protected void masterOperation(final Task task, final ExecuteSnapshotRetentionAction.Request request, - final ClusterState state, - final ActionListener listener) { + protected void masterOperation( + final Task task, + final ExecuteSnapshotRetentionAction.Request request, + final ClusterState state, + final ActionListener listener + ) { try { logger.info("manually triggering SLM snapshot retention"); this.retentionService.triggerRetention(); diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/TransportGetSLMStatusAction.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/TransportGetSLMStatusAction.java index 3a9b3941a7cd7..fbb182824a1d7 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/TransportGetSLMStatusAction.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/TransportGetSLMStatusAction.java @@ -26,15 +26,33 @@ public class TransportGetSLMStatusAction extends TransportMasterNodeAction { @Inject - public TransportGetSLMStatusAction(TransportService transportService, ClusterService clusterService, ThreadPool threadPool, - ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver) { - super(GetSLMStatusAction.NAME, transportService, clusterService, threadPool, actionFilters, - GetSLMStatusAction.Request::new, indexNameExpressionResolver, GetSLMStatusAction.Response::new, ThreadPool.Names.SAME); + public TransportGetSLMStatusAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver + ) { + super( + GetSLMStatusAction.NAME, + transportService, + clusterService, + threadPool, + actionFilters, + GetSLMStatusAction.Request::new, + indexNameExpressionResolver, + GetSLMStatusAction.Response::new, + ThreadPool.Names.SAME + ); } @Override - protected void masterOperation(Task task, GetSLMStatusAction.Request request, - ClusterState state, ActionListener listener) { + protected void masterOperation( + Task task, + GetSLMStatusAction.Request request, + ClusterState state, + ActionListener listener + ) { SnapshotLifecycleMetadata metadata = state.metadata().custom(SnapshotLifecycleMetadata.TYPE); final GetSLMStatusAction.Response response; if (metadata == null) { @@ -51,4 +69,3 @@ protected ClusterBlockException checkBlock(GetSLMStatusAction.Request request, C return state.blocks().globalBlockedException(ClusterBlockLevel.METADATA_WRITE); } } - diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/TransportGetSnapshotLifecycleAction.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/TransportGetSnapshotLifecycleAction.java index 45ddca0c84817..08e1d8766f0e2 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/TransportGetSnapshotLifecycleAction.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/TransportGetSnapshotLifecycleAction.java @@ -24,8 +24,8 @@ import org.elasticsearch.transport.TransportService; import org.elasticsearch.xpack.core.slm.SnapshotLifecycleMetadata; import org.elasticsearch.xpack.core.slm.SnapshotLifecyclePolicyItem; -import org.elasticsearch.xpack.core.slm.action.GetSnapshotLifecycleAction; import org.elasticsearch.xpack.core.slm.SnapshotLifecycleStats; +import org.elasticsearch.xpack.core.slm.action.GetSnapshotLifecycleAction; import java.util.Arrays; import java.util.Collections; @@ -36,29 +36,49 @@ import java.util.Set; import java.util.stream.Collectors; -public class TransportGetSnapshotLifecycleAction extends - TransportMasterNodeAction { +public class TransportGetSnapshotLifecycleAction extends TransportMasterNodeAction< + GetSnapshotLifecycleAction.Request, + GetSnapshotLifecycleAction.Response> { @Inject - public TransportGetSnapshotLifecycleAction(TransportService transportService, ClusterService clusterService, ThreadPool threadPool, - ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver) { - super(GetSnapshotLifecycleAction.NAME, transportService, clusterService, threadPool, actionFilters, - GetSnapshotLifecycleAction.Request::new, indexNameExpressionResolver, GetSnapshotLifecycleAction.Response::new, - ThreadPool.Names.SAME); + public TransportGetSnapshotLifecycleAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver + ) { + super( + GetSnapshotLifecycleAction.NAME, + transportService, + clusterService, + threadPool, + actionFilters, + GetSnapshotLifecycleAction.Request::new, + indexNameExpressionResolver, + GetSnapshotLifecycleAction.Response::new, + ThreadPool.Names.SAME + ); } @Override - protected void masterOperation(final Task task, final GetSnapshotLifecycleAction.Request request, - final ClusterState state, - final ActionListener listener) { + protected void masterOperation( + final Task task, + final GetSnapshotLifecycleAction.Request request, + final ClusterState state, + final ActionListener listener + ) { SnapshotLifecycleMetadata snapMeta = state.metadata().custom(SnapshotLifecycleMetadata.TYPE); if (snapMeta == null) { if (request.getLifecycleIds().length == 0) { listener.onResponse(new GetSnapshotLifecycleAction.Response(Collections.emptyList())); } else { - listener.onFailure(new ResourceNotFoundException( - "snapshot lifecycle policy or policies {} not found, no policies are configured", - Arrays.toString(request.getLifecycleIds()))); + listener.onFailure( + new ResourceNotFoundException( + "snapshot lifecycle policy or policies {} not found, no policies are configured", + Arrays.toString(request.getLifecycleIds()) + ) + ); } } else { final Map inProgress; @@ -70,9 +90,9 @@ protected void masterOperation(final Task task, final GetSnapshotLifecycleAction for (List entriesForRepo : sip.entriesByRepo()) { for (SnapshotsInProgress.Entry entry : entriesForRepo) { Map meta = entry.userMetadata(); - if (meta == null || - meta.get(SnapshotsService.POLICY_ID_METADATA_FIELD) == null || - (meta.get(SnapshotsService.POLICY_ID_METADATA_FIELD) instanceof String == false)) { + if (meta == null + || meta.get(SnapshotsService.POLICY_ID_METADATA_FIELD) == null + || (meta.get(SnapshotsService.POLICY_ID_METADATA_FIELD) instanceof String == false)) { continue; } @@ -84,24 +104,31 @@ protected void masterOperation(final Task task, final GetSnapshotLifecycleAction final Set ids = new HashSet<>(Arrays.asList(request.getLifecycleIds())); final SnapshotLifecycleStats slmStats = snapMeta.getStats(); - List lifecycles = snapMeta.getSnapshotConfigurations().values().stream() - .filter(meta -> { - if (ids.isEmpty()) { - return true; - } else { - return ids.contains(meta.getPolicy().getId()); - } - }) - .map(policyMeta -> - new SnapshotLifecyclePolicyItem(policyMeta, inProgress.get(policyMeta.getPolicy().getId()), - slmStats.getMetrics().get(policyMeta.getPolicy().getId()))) + List lifecycles = snapMeta.getSnapshotConfigurations().values().stream().filter(meta -> { + if (ids.isEmpty()) { + return true; + } else { + return ids.contains(meta.getPolicy().getId()); + } + }) + .map( + policyMeta -> new SnapshotLifecyclePolicyItem( + policyMeta, + inProgress.get(policyMeta.getPolicy().getId()), + slmStats.getMetrics().get(policyMeta.getPolicy().getId()) + ) + ) .collect(Collectors.toList()); if (lifecycles.size() == 0) { if (request.getLifecycleIds().length == 0) { listener.onResponse(new GetSnapshotLifecycleAction.Response(Collections.emptyList())); } else { - listener.onFailure(new ResourceNotFoundException("snapshot lifecycle policy or policies {} not found", - Arrays.toString(request.getLifecycleIds()))); + listener.onFailure( + new ResourceNotFoundException( + "snapshot lifecycle policy or policies {} not found", + Arrays.toString(request.getLifecycleIds()) + ) + ); } } else { listener.onResponse(new GetSnapshotLifecycleAction.Response(lifecycles)); diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/TransportGetSnapshotLifecycleStatsAction.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/TransportGetSnapshotLifecycleStatsAction.java index 3dcb35bb9becb..574bb9d6ea6c0 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/TransportGetSnapshotLifecycleStatsAction.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/TransportGetSnapshotLifecycleStatsAction.java @@ -20,24 +20,41 @@ import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; import org.elasticsearch.xpack.core.slm.SnapshotLifecycleMetadata; -import org.elasticsearch.xpack.core.slm.action.GetSnapshotLifecycleStatsAction; import org.elasticsearch.xpack.core.slm.SnapshotLifecycleStats; +import org.elasticsearch.xpack.core.slm.action.GetSnapshotLifecycleStatsAction; -public class TransportGetSnapshotLifecycleStatsAction extends - TransportMasterNodeAction { +public class TransportGetSnapshotLifecycleStatsAction extends TransportMasterNodeAction< + GetSnapshotLifecycleStatsAction.Request, + GetSnapshotLifecycleStatsAction.Response> { @Inject - public TransportGetSnapshotLifecycleStatsAction(TransportService transportService, ClusterService clusterService, - ThreadPool threadPool, ActionFilters actionFilters, - IndexNameExpressionResolver indexNameExpressionResolver) { - super(GetSnapshotLifecycleStatsAction.NAME, transportService, clusterService, threadPool, actionFilters, - GetSnapshotLifecycleStatsAction.Request::new, indexNameExpressionResolver, GetSnapshotLifecycleStatsAction.Response::new, - ThreadPool.Names.SAME); + public TransportGetSnapshotLifecycleStatsAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver + ) { + super( + GetSnapshotLifecycleStatsAction.NAME, + transportService, + clusterService, + threadPool, + actionFilters, + GetSnapshotLifecycleStatsAction.Request::new, + indexNameExpressionResolver, + GetSnapshotLifecycleStatsAction.Response::new, + ThreadPool.Names.SAME + ); } @Override - protected void masterOperation(Task task, GetSnapshotLifecycleStatsAction.Request request, - ClusterState state, ActionListener listener) { + protected void masterOperation( + Task task, + GetSnapshotLifecycleStatsAction.Request request, + ClusterState state, + ActionListener listener + ) { SnapshotLifecycleMetadata slmMeta = state.metadata().custom(SnapshotLifecycleMetadata.TYPE); if (slmMeta == null) { listener.onResponse(new GetSnapshotLifecycleStatsAction.Response(new SnapshotLifecycleStats())); diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/TransportPutSnapshotLifecycleAction.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/TransportPutSnapshotLifecycleAction.java index 4245351a41e98..0b340a9d894b4 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/TransportPutSnapshotLifecycleAction.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/TransportPutSnapshotLifecycleAction.java @@ -37,23 +37,40 @@ import java.util.HashMap; import java.util.Map; -public class TransportPutSnapshotLifecycleAction extends - TransportMasterNodeAction { +public class TransportPutSnapshotLifecycleAction extends TransportMasterNodeAction< + PutSnapshotLifecycleAction.Request, + PutSnapshotLifecycleAction.Response> { private static final Logger logger = LogManager.getLogger(TransportPutSnapshotLifecycleAction.class); @Inject - public TransportPutSnapshotLifecycleAction(TransportService transportService, ClusterService clusterService, ThreadPool threadPool, - ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver) { - super(PutSnapshotLifecycleAction.NAME, transportService, clusterService, threadPool, actionFilters, - PutSnapshotLifecycleAction.Request::new, indexNameExpressionResolver, PutSnapshotLifecycleAction.Response::new, - ThreadPool.Names.SAME); + public TransportPutSnapshotLifecycleAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver + ) { + super( + PutSnapshotLifecycleAction.NAME, + transportService, + clusterService, + threadPool, + actionFilters, + PutSnapshotLifecycleAction.Request::new, + indexNameExpressionResolver, + PutSnapshotLifecycleAction.Response::new, + ThreadPool.Names.SAME + ); } @Override - protected void masterOperation(final Task task, final PutSnapshotLifecycleAction.Request request, - final ClusterState state, - final ActionListener listener) { + protected void masterOperation( + final Task task, + final PutSnapshotLifecycleAction.Request request, + final ClusterState state, + final ActionListener listener + ) { SnapshotLifecycleService.validateRepositoryExists(request.getLifecycle().getRepository(), state); SnapshotLifecycleService.validateMinimumInterval(request.getLifecycle(), state); @@ -64,7 +81,8 @@ protected void masterOperation(final Task task, final PutSnapshotLifecycleAction // same context, and therefore does not have access to the appropriate security headers. final Map filteredHeaders = ClientHelper.filterSecurityHeaders(threadPool.getThreadContext().getHeaders()); LifecyclePolicy.validatePolicyName(request.getLifecycleId()); - clusterService.submitStateUpdateTask("put-snapshot-lifecycle-" + request.getLifecycleId(), + clusterService.submitStateUpdateTask( + "put-snapshot-lifecycle-" + request.getLifecycleId(), new AckedClusterStateUpdateTask(request, listener) { @Override public ClusterState execute(ClusterState currentState) { @@ -78,8 +96,11 @@ public ClusterState execute(ClusterState currentState) { .setHeaders(filteredHeaders) .setModifiedDate(Instant.now().toEpochMilli()) .build(); - lifecycleMetadata = new SnapshotLifecycleMetadata(Collections.singletonMap(id, meta), - OperationMode.RUNNING, new SnapshotLifecycleStats()); + lifecycleMetadata = new SnapshotLifecycleMetadata( + Collections.singletonMap(id, meta), + OperationMode.RUNNING, + new SnapshotLifecycleStats() + ); logger.info("adding new snapshot lifecycle [{}]", id); } else { Map snapLifecycles = new HashMap<>(snapMeta.getSnapshotConfigurations()); @@ -91,8 +112,7 @@ public ClusterState execute(ClusterState currentState) { .setModifiedDate(Instant.now().toEpochMilli()) .build(); snapLifecycles.put(id, newLifecycle); - lifecycleMetadata = new SnapshotLifecycleMetadata(snapLifecycles, - snapMeta.getOperationMode(), snapMeta.getStats()); + lifecycleMetadata = new SnapshotLifecycleMetadata(snapLifecycles, snapMeta.getOperationMode(), snapMeta.getStats()); if (oldLifecycle == null) { logger.info("adding new snapshot lifecycle [{}]", id); } else { @@ -102,8 +122,7 @@ public ClusterState execute(ClusterState currentState) { Metadata currentMeta = currentState.metadata(); return ClusterState.builder(currentState) - .metadata(Metadata.builder(currentMeta) - .putCustom(SnapshotLifecycleMetadata.TYPE, lifecycleMetadata)) + .metadata(Metadata.builder(currentMeta).putCustom(SnapshotLifecycleMetadata.TYPE, lifecycleMetadata)) .build(); } @@ -111,7 +130,8 @@ public ClusterState execute(ClusterState currentState) { protected PutSnapshotLifecycleAction.Response newResponse(boolean acknowledged) { return new PutSnapshotLifecycleAction.Response(acknowledged); } - }); + } + ); } @Override diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/TransportStartSLMAction.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/TransportStartSLMAction.java index 27ea97e9ca2a1..4a1d9c7ffcabc 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/TransportStartSLMAction.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/TransportStartSLMAction.java @@ -28,15 +28,32 @@ public class TransportStartSLMAction extends AcknowledgedTransportMasterNodeAction { @Inject - public TransportStartSLMAction(TransportService transportService, ClusterService clusterService, ThreadPool threadPool, - ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver) { - super(StartSLMAction.NAME, transportService, clusterService, threadPool, actionFilters, StartSLMAction.Request::new, - indexNameExpressionResolver, ThreadPool.Names.SAME); + public TransportStartSLMAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver + ) { + super( + StartSLMAction.NAME, + transportService, + clusterService, + threadPool, + actionFilters, + StartSLMAction.Request::new, + indexNameExpressionResolver, + ThreadPool.Names.SAME + ); } @Override - protected void masterOperation(Task task, StartSLMAction.Request request, ClusterState state, - ActionListener listener) { + protected void masterOperation( + Task task, + StartSLMAction.Request request, + ClusterState state, + ActionListener listener + ) { clusterService.submitStateUpdateTask("slm_operation_mode_update", new AckedClusterStateUpdateTask(request, listener) { @Override public ClusterState execute(ClusterState currentState) { diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/TransportStopSLMAction.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/TransportStopSLMAction.java index ec7c348c2598d..c2ca65bc784fc 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/TransportStopSLMAction.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/TransportStopSLMAction.java @@ -28,15 +28,32 @@ public class TransportStopSLMAction extends AcknowledgedTransportMasterNodeAction { @Inject - public TransportStopSLMAction(TransportService transportService, ClusterService clusterService, ThreadPool threadPool, - ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver) { - super(StopSLMAction.NAME, transportService, clusterService, threadPool, actionFilters, StopSLMAction.Request::new, - indexNameExpressionResolver, ThreadPool.Names.SAME); + public TransportStopSLMAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver + ) { + super( + StopSLMAction.NAME, + transportService, + clusterService, + threadPool, + actionFilters, + StopSLMAction.Request::new, + indexNameExpressionResolver, + ThreadPool.Names.SAME + ); } @Override - protected void masterOperation(Task task, StopSLMAction.Request request, ClusterState state, - ActionListener listener) { + protected void masterOperation( + Task task, + StopSLMAction.Request request, + ClusterState state, + ActionListener listener + ) { clusterService.submitStateUpdateTask("slm_operation_mode_update", new AckedClusterStateUpdateTask(request, listener) { @Override public ClusterState execute(ClusterState currentState) { diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/cluster/metadata/MetadataMigrateToDataTiersRoutingServiceTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/cluster/metadata/MetadataMigrateToDataTiersRoutingServiceTests.java index 4515b087fd646..a2093c75561a5 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/cluster/metadata/MetadataMigrateToDataTiersRoutingServiceTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/cluster/metadata/MetadataMigrateToDataTiersRoutingServiceTests.java @@ -73,10 +73,12 @@ public class MetadataMigrateToDataTiersRoutingServiceTests extends ESTestCase { private static final NamedXContentRegistry REGISTRY; static { - REGISTRY = new NamedXContentRegistry(List.of( - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ShrinkAction.NAME), ShrinkAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(AllocateAction.NAME), AllocateAction::parse) - )); + REGISTRY = new NamedXContentRegistry( + List.of( + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ShrinkAction.NAME), ShrinkAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(AllocateAction.NAME), AllocateAction::parse) + ) + ); } private String lifecycleName; @@ -96,13 +98,26 @@ public void testMigrateIlmPolicyForIndexWithoutILMMetadata() { AllocateAction warmAllocateAction = new AllocateAction(null, null, Map.of("data", "warm"), null, Map.of("rack", "rack1")); AllocateAction coldAllocateAction = new AllocateAction(0, null, null, null, Map.of("data", "cold")); SetPriorityAction warmSetPriority = new SetPriorityAction(100); - LifecyclePolicyMetadata policyMetadata = getWarmColdPolicyMeta(warmSetPriority, shrinkAction, warmAllocateAction, - coldAllocateAction); - - ClusterState state = ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder() - .putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata( - Collections.singletonMap(policyMetadata.getName(), policyMetadata), OperationMode.STOPPED)) - .put(IndexMetadata.builder(indexName).settings(getBaseIndexSettings())).build()) + LifecyclePolicyMetadata policyMetadata = getWarmColdPolicyMeta( + warmSetPriority, + shrinkAction, + warmAllocateAction, + coldAllocateAction + ); + + ClusterState state = ClusterState.builder(ClusterName.DEFAULT) + .metadata( + Metadata.builder() + .putCustom( + IndexLifecycleMetadata.TYPE, + new IndexLifecycleMetadata( + Collections.singletonMap(policyMetadata.getName(), policyMetadata), + OperationMode.STOPPED + ) + ) + .put(IndexMetadata.builder(indexName).settings(getBaseIndexSettings())) + .build() + ) .build(); Metadata.Builder newMetadata = Metadata.builder(state.metadata()); @@ -114,8 +129,11 @@ public void testMigrateIlmPolicyForIndexWithoutILMMetadata() { IndexLifecycleMetadata updatedLifecycleMetadata = newState.metadata().custom(IndexLifecycleMetadata.TYPE); LifecyclePolicy lifecyclePolicy = updatedLifecycleMetadata.getPolicies().get(lifecycleName); Map warmActions = lifecyclePolicy.getPhases().get("warm").getActions(); - assertThat("allocate action in the warm phase didn't specify any number of replicas so it must be removed", - warmActions.size(), is(2)); + assertThat( + "allocate action in the warm phase didn't specify any number of replicas so it must be removed", + warmActions.size(), + is(2) + ); assertThat(warmActions.get(shrinkAction.getWriteableName()), is(shrinkAction)); assertThat(warmActions.get(warmSetPriority.getWriteableName()), is(warmSetPriority)); @@ -131,19 +149,44 @@ public void testMigrateIlmPolicyFOrPhaseWithDeactivatedMigrateAction() { AllocateAction warmAllocateAction = new AllocateAction(null, null, Map.of("data", "warm"), null, Map.of("rack", "rack1")); MigrateAction deactivatedMigrateAction = new MigrateAction(false); - LifecyclePolicy policy = new LifecyclePolicy(lifecycleName, - Map.of("warm", - new Phase("warm", TimeValue.ZERO, Map.of(shrinkAction.getWriteableName(), shrinkAction, - warmAllocateAction.getWriteableName(), warmAllocateAction, deactivatedMigrateAction.getWriteableName(), - deactivatedMigrateAction)) - )); - LifecyclePolicyMetadata policyMetadata = new LifecyclePolicyMetadata(policy, Collections.emptyMap(), - randomNonNegativeLong(), randomNonNegativeLong()); - - ClusterState state = ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder() - .putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata( - Collections.singletonMap(policyMetadata.getName(), policyMetadata), OperationMode.STOPPED)) - .put(IndexMetadata.builder(indexName).settings(getBaseIndexSettings())).build()) + LifecyclePolicy policy = new LifecyclePolicy( + lifecycleName, + Map.of( + "warm", + new Phase( + "warm", + TimeValue.ZERO, + Map.of( + shrinkAction.getWriteableName(), + shrinkAction, + warmAllocateAction.getWriteableName(), + warmAllocateAction, + deactivatedMigrateAction.getWriteableName(), + deactivatedMigrateAction + ) + ) + ) + ); + LifecyclePolicyMetadata policyMetadata = new LifecyclePolicyMetadata( + policy, + Collections.emptyMap(), + randomNonNegativeLong(), + randomNonNegativeLong() + ); + + ClusterState state = ClusterState.builder(ClusterName.DEFAULT) + .metadata( + Metadata.builder() + .putCustom( + IndexLifecycleMetadata.TYPE, + new IndexLifecycleMetadata( + Collections.singletonMap(policyMetadata.getName(), policyMetadata), + OperationMode.STOPPED + ) + ) + .put(IndexMetadata.builder(indexName).settings(getBaseIndexSettings())) + .build() + ) .build(); Metadata.Builder newMetadata = Metadata.builder(state.metadata()); @@ -155,8 +198,12 @@ public void testMigrateIlmPolicyFOrPhaseWithDeactivatedMigrateAction() { IndexLifecycleMetadata updatedLifecycleMetadata = newState.metadata().custom(IndexLifecycleMetadata.TYPE); LifecyclePolicy lifecyclePolicy = updatedLifecycleMetadata.getPolicies().get(lifecycleName); Map warmActions = lifecyclePolicy.getPhases().get("warm").getActions(); - assertThat("allocate action in the warm phase didn't specify any number of replicas so it must be removed, together with the " + - "deactivated migrate action", warmActions.size(), is(1)); + assertThat( + "allocate action in the warm phase didn't specify any number of replicas so it must be removed, together with the " + + "deactivated migrate action", + warmActions.size(), + is(1) + ); assertThat(warmActions.get(shrinkAction.getWriteableName()), is(shrinkAction)); } @@ -166,8 +213,12 @@ public void testMigrateIlmPolicyRefreshesCachedPhase() { AllocateAction warmAllocateAction = new AllocateAction(null, null, Map.of("data", "warm"), null, Map.of("rack", "rack1")); AllocateAction coldAllocateAction = new AllocateAction(0, null, null, null, Map.of("data", "cold")); SetPriorityAction warmSetPriority = new SetPriorityAction(100); - LifecyclePolicyMetadata policyMetadata = getWarmColdPolicyMeta(warmSetPriority, shrinkAction, warmAllocateAction, - coldAllocateAction); + LifecyclePolicyMetadata policyMetadata = getWarmColdPolicyMeta( + warmSetPriority, + shrinkAction, + warmAllocateAction, + coldAllocateAction + ); { // index is in the cold phase and the migrated allocate action is not removed @@ -178,13 +229,23 @@ public void testMigrateIlmPolicyRefreshesCachedPhase() { .setPhaseDefinition(getColdPhaseDefinition()) .build(); - IndexMetadata.Builder indexMetadata = IndexMetadata.builder(indexName).settings(getBaseIndexSettings()) + IndexMetadata.Builder indexMetadata = IndexMetadata.builder(indexName) + .settings(getBaseIndexSettings()) .putCustom(ILM_CUSTOM_METADATA_KEY, preMigrationExecutionState.asMap()); - ClusterState state = ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder() - .putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata( - Collections.singletonMap(policyMetadata.getName(), policyMetadata), OperationMode.STOPPED)) - .put(indexMetadata).build()) + ClusterState state = ClusterState.builder(ClusterName.DEFAULT) + .metadata( + Metadata.builder() + .putCustom( + IndexLifecycleMetadata.TYPE, + new IndexLifecycleMetadata( + Collections.singletonMap(policyMetadata.getName(), policyMetadata), + OperationMode.STOPPED + ) + ) + .put(indexMetadata) + .build() + ) .build(); Metadata.Builder newMetadata = Metadata.builder(state.metadata()); @@ -216,13 +277,23 @@ public void testMigrateIlmPolicyRefreshesCachedPhase() { .setPhaseDefinition(getWarmPhaseDef()) .build(); - IndexMetadata.Builder indexMetadata = IndexMetadata.builder(indexName).settings(getBaseIndexSettings()) + IndexMetadata.Builder indexMetadata = IndexMetadata.builder(indexName) + .settings(getBaseIndexSettings()) .putCustom(ILM_CUSTOM_METADATA_KEY, preMigrationExecutionState.asMap()); - ClusterState state = ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder() - .putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata( - Collections.singletonMap(policyMetadata.getName(), policyMetadata), OperationMode.STOPPED)) - .put(indexMetadata).build()) + ClusterState state = ClusterState.builder(ClusterName.DEFAULT) + .metadata( + Metadata.builder() + .putCustom( + IndexLifecycleMetadata.TYPE, + new IndexLifecycleMetadata( + Collections.singletonMap(policyMetadata.getName(), policyMetadata), + OperationMode.STOPPED + ) + ) + .put(indexMetadata) + .build() + ) .build(); Metadata.Builder newMetadata = Metadata.builder(state.metadata()); @@ -260,13 +331,23 @@ public void testMigrateIlmPolicyRefreshesCachedPhase() { .setPhaseDefinition(getWarmPhaseDef()) .build(); - IndexMetadata.Builder indexMetadata = IndexMetadata.builder(indexName).settings(getBaseIndexSettings()) + IndexMetadata.Builder indexMetadata = IndexMetadata.builder(indexName) + .settings(getBaseIndexSettings()) .putCustom(ILM_CUSTOM_METADATA_KEY, preMigrationExecutionState.asMap()); - ClusterState state = ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder() - .putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata( - Collections.singletonMap(policyMetadata.getName(), policyMetadata), OperationMode.STOPPED)) - .put(indexMetadata).build()) + ClusterState state = ClusterState.builder(ClusterName.DEFAULT) + .metadata( + Metadata.builder() + .putCustom( + IndexLifecycleMetadata.TYPE, + new IndexLifecycleMetadata( + Collections.singletonMap(policyMetadata.getName(), policyMetadata), + OperationMode.STOPPED + ) + ) + .put(indexMetadata) + .build() + ) .build(); Metadata.Builder newMetadata = Metadata.builder(state.metadata()); @@ -300,13 +381,23 @@ public void testMigrateIlmPolicyRefreshesCachedPhase() { .setPhaseDefinition(getWarmPhaseDef()) .build(); - IndexMetadata.Builder indexMetadata = IndexMetadata.builder(indexName).settings(getBaseIndexSettings()) + IndexMetadata.Builder indexMetadata = IndexMetadata.builder(indexName) + .settings(getBaseIndexSettings()) .putCustom(ILM_CUSTOM_METADATA_KEY, preMigrationExecutionState.asMap()); - ClusterState state = ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder() - .putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata( - Collections.singletonMap(policyMetadata.getName(), policyMetadata), OperationMode.STOPPED)) - .put(indexMetadata).build()) + ClusterState state = ClusterState.builder(ClusterName.DEFAULT) + .metadata( + Metadata.builder() + .putCustom( + IndexLifecycleMetadata.TYPE, + new IndexLifecycleMetadata( + Collections.singletonMap(policyMetadata.getName(), policyMetadata), + OperationMode.STOPPED + ) + ) + .put(indexMetadata) + .build() + ) .build(); Metadata.Builder newMetadata = Metadata.builder(state.metadata()); @@ -344,11 +435,17 @@ private Settings.Builder getBaseIndexSettings() { public void testAllocateActionDefinesRoutingRules() { assertThat(allocateActionDefinesRoutingRules("data", new AllocateAction(null, null, Map.of("data", "cold"), null, null)), is(true)); assertThat(allocateActionDefinesRoutingRules("data", new AllocateAction(null, null, null, Map.of("data", "cold"), null)), is(true)); - assertThat(allocateActionDefinesRoutingRules("data", new AllocateAction(null, null, Map.of("another_attribute", "rack1"), null, - Map.of("data", "cold"))), is(true)); - assertThat(allocateActionDefinesRoutingRules("data", new AllocateAction(null, null, null, null, Map.of("another_attribute", - "cold"))), - is(false)); + assertThat( + allocateActionDefinesRoutingRules( + "data", + new AllocateAction(null, null, Map.of("another_attribute", "rack1"), null, Map.of("data", "cold")) + ), + is(true) + ); + assertThat( + allocateActionDefinesRoutingRules("data", new AllocateAction(null, null, null, null, Map.of("another_attribute", "cold"))), + is(false) + ); assertThat(allocateActionDefinesRoutingRules("data", null), is(false)); } @@ -364,11 +461,11 @@ public void testConvertAttributeValueToTierPreference() { public void testMigrateIndices() { { // index with `warm` data attribute is migrated to the equivalent _tier_preference routing - IndexMetadata.Builder indexWithWarmDataAttribute = - IndexMetadata.builder("indexWithWarmDataAttribute").settings(getBaseIndexSettings() - .put(DATA_ROUTING_REQUIRE_SETTING, "warm")); - ClusterState state = - ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder().put(indexWithWarmDataAttribute)).build(); + IndexMetadata.Builder indexWithWarmDataAttribute = IndexMetadata.builder("indexWithWarmDataAttribute") + .settings(getBaseIndexSettings().put(DATA_ROUTING_REQUIRE_SETTING, "warm")); + ClusterState state = ClusterState.builder(ClusterName.DEFAULT) + .metadata(Metadata.builder().put(indexWithWarmDataAttribute)) + .build(); Metadata.Builder mb = Metadata.builder(state.metadata()); @@ -384,11 +481,11 @@ public void testMigrateIndices() { { // test the migration of the `include.data` configuration to the equivalent _tier_preference routing - IndexMetadata.Builder indexWithWarmDataAttribute = - IndexMetadata.builder("indexWithWarmDataAttribute").settings(getBaseIndexSettings() - .put(DATA_ROUTING_INCLUDE_SETTING, "warm")); - ClusterState state = - ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder().put(indexWithWarmDataAttribute)).build(); + IndexMetadata.Builder indexWithWarmDataAttribute = IndexMetadata.builder("indexWithWarmDataAttribute") + .settings(getBaseIndexSettings().put(DATA_ROUTING_INCLUDE_SETTING, "warm")); + ClusterState state = ClusterState.builder(ClusterName.DEFAULT) + .metadata(Metadata.builder().put(indexWithWarmDataAttribute)) + .build(); Metadata.Builder mb = Metadata.builder(state.metadata()); @@ -405,13 +502,15 @@ public void testMigrateIndices() { { // since the index has a _tier_preference configuration the migrated index should still contain it and have the `data` // attributes routing removed - IndexMetadata.Builder indexWithTierPreferenceAndDataAttribute = - IndexMetadata.builder("indexWithTierPreferenceAndDataAttribute").settings(getBaseIndexSettings() - .put(DATA_ROUTING_REQUIRE_SETTING, "cold") - .put(DATA_ROUTING_INCLUDE_SETTING, "hot") - .put(TIER_PREFERENCE, "data_warm,data_hot")); - ClusterState state = - ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder().put(indexWithTierPreferenceAndDataAttribute)).build(); + IndexMetadata.Builder indexWithTierPreferenceAndDataAttribute = IndexMetadata.builder("indexWithTierPreferenceAndDataAttribute") + .settings( + getBaseIndexSettings().put(DATA_ROUTING_REQUIRE_SETTING, "cold") + .put(DATA_ROUTING_INCLUDE_SETTING, "hot") + .put(TIER_PREFERENCE, "data_warm,data_hot") + ); + ClusterState state = ClusterState.builder(ClusterName.DEFAULT) + .metadata(Metadata.builder().put(indexWithTierPreferenceAndDataAttribute)) + .build(); Metadata.Builder mb = Metadata.builder(state.metadata()); @@ -429,12 +528,11 @@ public void testMigrateIndices() { { // like above, test a combination of node attribute and _tier_preference routings configured for the original index, but this // time using the `include.data` setting - IndexMetadata.Builder indexWithTierPreferenceAndDataAttribute = - IndexMetadata.builder("indexWithTierPreferenceAndDataAttribute").settings(getBaseIndexSettings() - .put(DATA_ROUTING_INCLUDE_SETTING, "cold") - .put(TIER_PREFERENCE, "data_warm,data_hot")); - ClusterState state = - ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder().put(indexWithTierPreferenceAndDataAttribute)).build(); + IndexMetadata.Builder indexWithTierPreferenceAndDataAttribute = IndexMetadata.builder("indexWithTierPreferenceAndDataAttribute") + .settings(getBaseIndexSettings().put(DATA_ROUTING_INCLUDE_SETTING, "cold").put(TIER_PREFERENCE, "data_warm,data_hot")); + ClusterState state = ClusterState.builder(ClusterName.DEFAULT) + .metadata(Metadata.builder().put(indexWithTierPreferenceAndDataAttribute)) + .build(); Metadata.Builder mb = Metadata.builder(state.metadata()); @@ -450,11 +548,11 @@ public void testMigrateIndices() { { // index with an unknown `data` attribute routing value should be migrated - IndexMetadata.Builder indexWithUnknownDataAttribute = - IndexMetadata.builder("indexWithUnknownDataAttribute").settings(getBaseIndexSettings() - .put(DATA_ROUTING_REQUIRE_SETTING, "something_else")); - ClusterState state = - ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder().put(indexWithUnknownDataAttribute)).build(); + IndexMetadata.Builder indexWithUnknownDataAttribute = IndexMetadata.builder("indexWithUnknownDataAttribute") + .settings(getBaseIndexSettings().put(DATA_ROUTING_REQUIRE_SETTING, "something_else")); + ClusterState state = ClusterState.builder(ClusterName.DEFAULT) + .metadata(Metadata.builder().put(indexWithUnknownDataAttribute)) + .build(); Metadata.Builder mb = Metadata.builder(state.metadata()); List migratedIndices = migrateIndices(mb, state, "data"); @@ -469,12 +567,11 @@ public void testMigrateIndices() { { // index with data and another attribute should only see the data attribute removed and the corresponding tier_preference // configured - IndexMetadata.Builder indexDataAndBoxAttribute = - IndexMetadata.builder("indexWithDataAndBoxAttribute").settings(getBaseIndexSettings() - .put(DATA_ROUTING_REQUIRE_SETTING, "warm") - .put(BOX_ROUTING_REQUIRE_SETTING, "box1")); - ClusterState state = - ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder().put(indexDataAndBoxAttribute)).build(); + IndexMetadata.Builder indexDataAndBoxAttribute = IndexMetadata.builder("indexWithDataAndBoxAttribute") + .settings(getBaseIndexSettings().put(DATA_ROUTING_REQUIRE_SETTING, "warm").put(BOX_ROUTING_REQUIRE_SETTING, "box1")); + ClusterState state = ClusterState.builder(ClusterName.DEFAULT) + .metadata(Metadata.builder().put(indexDataAndBoxAttribute)) + .build(); Metadata.Builder mb = Metadata.builder(state.metadata()); List migratedIndices = migrateIndices(mb, state, "data"); @@ -490,11 +587,9 @@ public void testMigrateIndices() { { // index that doesn't have any data attribute routing but has another attribute should be migrated - IndexMetadata.Builder indexBoxAttribute = - IndexMetadata.builder("indexWithBoxAttribute").settings(getBaseIndexSettings() - .put(BOX_ROUTING_REQUIRE_SETTING, "warm")); - ClusterState state = - ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder().put(indexBoxAttribute)).build(); + IndexMetadata.Builder indexBoxAttribute = IndexMetadata.builder("indexWithBoxAttribute") + .settings(getBaseIndexSettings().put(BOX_ROUTING_REQUIRE_SETTING, "warm")); + ClusterState state = ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder().put(indexBoxAttribute)).build(); Metadata.Builder mb = Metadata.builder(state.metadata()); List migratedIndices = migrateIndices(mb, state, "data"); @@ -508,10 +603,11 @@ public void testMigrateIndices() { } { - IndexMetadata.Builder indexNoRoutingAttribute = - IndexMetadata.builder("indexNoRoutingAttribute").settings(getBaseIndexSettings()); - ClusterState state = - ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder().put(indexNoRoutingAttribute)).build(); + IndexMetadata.Builder indexNoRoutingAttribute = IndexMetadata.builder("indexNoRoutingAttribute") + .settings(getBaseIndexSettings()); + ClusterState state = ClusterState.builder(ClusterName.DEFAULT) + .metadata(Metadata.builder().put(indexNoRoutingAttribute)) + .build(); Metadata.Builder mb = Metadata.builder(state.metadata()); List migratedIndices = migrateIndices(mb, state, "data"); @@ -526,15 +622,15 @@ public void testMigrateIndices() { } public void testRequireAttributeIndexSettingTakesPriorityOverInclude() { - IndexMetadata.Builder indexWithAllRoutingSettings = - IndexMetadata.builder("indexWithAllRoutingSettings") - .settings(getBaseIndexSettings() - .put(DATA_ROUTING_REQUIRE_SETTING, "warm") + IndexMetadata.Builder indexWithAllRoutingSettings = IndexMetadata.builder("indexWithAllRoutingSettings") + .settings( + getBaseIndexSettings().put(DATA_ROUTING_REQUIRE_SETTING, "warm") .put(DATA_ROUTING_INCLUDE_SETTING, "cold") .put(DATA_ROUTING_EXCLUDE_SETTING, "hot") - ); - ClusterState state = - ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder().put(indexWithAllRoutingSettings)).build(); + ); + ClusterState state = ClusterState.builder(ClusterName.DEFAULT) + .metadata(Metadata.builder().put(indexWithAllRoutingSettings)) + .build(); Metadata.Builder mb = Metadata.builder(state.metadata()); @@ -551,48 +647,97 @@ public void testRequireAttributeIndexSettingTakesPriorityOverInclude() { } public void testMigrateToDataTiersRouting() { - AllocateAction allocateActionWithDataAttribute = new AllocateAction(null, null, Map.of("data", "warm"), null, Map.of("rack", - "rack1")); + AllocateAction allocateActionWithDataAttribute = new AllocateAction( + null, + null, + Map.of("data", "warm"), + null, + Map.of("rack", "rack1") + ); AllocateAction allocateActionWithOtherAttribute = new AllocateAction(0, null, null, null, Map.of("other", "cold")); - LifecyclePolicy policyToMigrate = new LifecyclePolicy(lifecycleName, - Map.of("warm", - new Phase("warm", TimeValue.ZERO, Map.of(allocateActionWithDataAttribute.getWriteableName(), - allocateActionWithDataAttribute)))); - LifecyclePolicyMetadata policyWithDataAttribute = new LifecyclePolicyMetadata(policyToMigrate, Collections.emptyMap(), - randomNonNegativeLong(), randomNonNegativeLong()); - - LifecyclePolicy shouldntBeMigratedPolicy = new LifecyclePolicy("dont-migrate", - Map.of("warm", - new Phase("warm", TimeValue.ZERO, Map.of(allocateActionWithOtherAttribute.getWriteableName(), - allocateActionWithOtherAttribute)))); - LifecyclePolicyMetadata policyWithOtherAttribute = new LifecyclePolicyMetadata(shouldntBeMigratedPolicy, Collections.emptyMap(), - randomNonNegativeLong(), randomNonNegativeLong()); - - - IndexMetadata.Builder indexWithUnknownDataAttribute = - IndexMetadata.builder("indexWithUnknownDataAttribute").settings(getBaseIndexSettings().put(DATA_ROUTING_REQUIRE_SETTING, - "something_else")); - IndexMetadata.Builder indexWithWarmDataAttribute = - IndexMetadata.builder("indexWithWarmDataAttribute").settings(getBaseIndexSettings().put(DATA_ROUTING_REQUIRE_SETTING, "warm")); - - ClusterState state = ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder() - .putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata( - Map.of(policyToMigrate.getName(), policyWithDataAttribute, - shouldntBeMigratedPolicy.getName(), policyWithOtherAttribute), - OperationMode.STOPPED)) - .put(IndexTemplateMetadata.builder("catch-all").patterns(List.of("*")) - .settings(Settings.builder().put(DATA_ROUTING_REQUIRE_SETTING, "hot")) - .build()) - .put(IndexTemplateMetadata.builder("other-template").patterns(List.of("other-*")) - .settings(Settings.builder().put(DATA_ROUTING_REQUIRE_SETTING, "hot")) - .build()) - .put(indexWithUnknownDataAttribute).put(indexWithWarmDataAttribute)) + LifecyclePolicy policyToMigrate = new LifecyclePolicy( + lifecycleName, + Map.of( + "warm", + new Phase( + "warm", + TimeValue.ZERO, + Map.of(allocateActionWithDataAttribute.getWriteableName(), allocateActionWithDataAttribute) + ) + ) + ); + LifecyclePolicyMetadata policyWithDataAttribute = new LifecyclePolicyMetadata( + policyToMigrate, + Collections.emptyMap(), + randomNonNegativeLong(), + randomNonNegativeLong() + ); + + LifecyclePolicy shouldntBeMigratedPolicy = new LifecyclePolicy( + "dont-migrate", + Map.of( + "warm", + new Phase( + "warm", + TimeValue.ZERO, + Map.of(allocateActionWithOtherAttribute.getWriteableName(), allocateActionWithOtherAttribute) + ) + ) + ); + LifecyclePolicyMetadata policyWithOtherAttribute = new LifecyclePolicyMetadata( + shouldntBeMigratedPolicy, + Collections.emptyMap(), + randomNonNegativeLong(), + randomNonNegativeLong() + ); + + IndexMetadata.Builder indexWithUnknownDataAttribute = IndexMetadata.builder("indexWithUnknownDataAttribute") + .settings(getBaseIndexSettings().put(DATA_ROUTING_REQUIRE_SETTING, "something_else")); + IndexMetadata.Builder indexWithWarmDataAttribute = IndexMetadata.builder("indexWithWarmDataAttribute") + .settings(getBaseIndexSettings().put(DATA_ROUTING_REQUIRE_SETTING, "warm")); + + ClusterState state = ClusterState.builder(ClusterName.DEFAULT) + .metadata( + Metadata.builder() + .putCustom( + IndexLifecycleMetadata.TYPE, + new IndexLifecycleMetadata( + Map.of( + policyToMigrate.getName(), + policyWithDataAttribute, + shouldntBeMigratedPolicy.getName(), + policyWithOtherAttribute + ), + OperationMode.STOPPED + ) + ) + .put( + IndexTemplateMetadata.builder("catch-all") + .patterns(List.of("*")) + .settings(Settings.builder().put(DATA_ROUTING_REQUIRE_SETTING, "hot")) + .build() + ) + .put( + IndexTemplateMetadata.builder("other-template") + .patterns(List.of("other-*")) + .settings(Settings.builder().put(DATA_ROUTING_REQUIRE_SETTING, "hot")) + .build() + ) + .put(indexWithUnknownDataAttribute) + .put(indexWithWarmDataAttribute) + ) .build(); { - Tuple migratedEntitiesTuple = - migrateToDataTiersRouting(state, "data", "catch-all", REGISTRY, client, null); + Tuple migratedEntitiesTuple = migrateToDataTiersRouting( + state, + "data", + "catch-all", + REGISTRY, + client, + null + ); MigratedEntities migratedEntities = migratedEntitiesTuple.v2(); assertThat(migratedEntities.removedIndexTemplateName, is("catch-all")); @@ -609,8 +754,14 @@ public void testMigrateToDataTiersRouting() { { // let's test a null template name to make sure nothing is removed - Tuple migratedEntitiesTuple = - migrateToDataTiersRouting(state, "data", null, REGISTRY, client, null); + Tuple migratedEntitiesTuple = migrateToDataTiersRouting( + state, + "data", + null, + REGISTRY, + client, + null + ); MigratedEntities migratedEntities = migratedEntitiesTuple.v2(); assertThat(migratedEntities.removedIndexTemplateName, nullValue()); @@ -627,8 +778,14 @@ public void testMigrateToDataTiersRouting() { { // let's test a null node attribute parameter defaults to "data" - Tuple migratedEntitiesTuple = - migrateToDataTiersRouting(state, null, null, REGISTRY, client, null); + Tuple migratedEntitiesTuple = migrateToDataTiersRouting( + state, + null, + null, + REGISTRY, + client, + null + ); MigratedEntities migratedEntities = migratedEntitiesTuple.v2(); assertThat(migratedEntities.migratedPolicies.size(), is(1)); @@ -646,32 +803,45 @@ public void testMigrateToDataTiersRouting() { public void testMigrateToDataTiersRoutingRequiresILMStopped() { { - ClusterState ilmRunningState = ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder() - .putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata( - Map.of(), OperationMode.RUNNING))) + ClusterState ilmRunningState = ClusterState.builder(ClusterName.DEFAULT) + .metadata( + Metadata.builder().putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata(Map.of(), OperationMode.RUNNING)) + ) .build(); - IllegalStateException illegalStateException = expectThrows(IllegalStateException.class, - () -> migrateToDataTiersRouting(ilmRunningState, "data", "catch-all", REGISTRY, client, null)); + IllegalStateException illegalStateException = expectThrows( + IllegalStateException.class, + () -> migrateToDataTiersRouting(ilmRunningState, "data", "catch-all", REGISTRY, client, null) + ); assertThat(illegalStateException.getMessage(), is("stop ILM before migrating to data tiers, current state is [RUNNING]")); } { - ClusterState ilmStoppingState = ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder() - .putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata( - Map.of(), OperationMode.STOPPING))) + ClusterState ilmStoppingState = ClusterState.builder(ClusterName.DEFAULT) + .metadata( + Metadata.builder().putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata(Map.of(), OperationMode.STOPPING)) + ) .build(); - IllegalStateException illegalStateException = expectThrows(IllegalStateException.class, - () -> migrateToDataTiersRouting(ilmStoppingState, "data", "catch-all", REGISTRY, client, null)); + IllegalStateException illegalStateException = expectThrows( + IllegalStateException.class, + () -> migrateToDataTiersRouting(ilmStoppingState, "data", "catch-all", REGISTRY, client, null) + ); assertThat(illegalStateException.getMessage(), is("stop ILM before migrating to data tiers, current state is [STOPPING]")); } { - ClusterState ilmStoppedState = ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder() - .putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata( - Map.of(), OperationMode.STOPPED))) + ClusterState ilmStoppedState = ClusterState.builder(ClusterName.DEFAULT) + .metadata( + Metadata.builder().putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata(Map.of(), OperationMode.STOPPED)) + ) .build(); - Tuple migratedState = migrateToDataTiersRouting(ilmStoppedState, "data", "catch-all", - REGISTRY, client, null); + Tuple migratedState = migrateToDataTiersRouting( + ilmStoppedState, + "data", + "catch-all", + REGISTRY, + client, + null + ); assertThat(migratedState.v2().migratedIndices, empty()); assertThat(migratedState.v2().migratedPolicies, empty()); assertThat(migratedState.v2().removedIndexTemplateName, nullValue()); @@ -679,17 +849,22 @@ public void testMigrateToDataTiersRoutingRequiresILMStopped() { } public void testMigrationDoesNotRemoveComposableTemplates() { - ComposableIndexTemplate composableIndexTemplate = new ComposableIndexTemplate.Builder() - .indexPatterns(Collections.singletonList("*")) - .template(new Template(Settings.builder().put(DATA_ROUTING_REQUIRE_SETTING, "hot").build(), null, null)) - .build(); + ComposableIndexTemplate composableIndexTemplate = new ComposableIndexTemplate.Builder().indexPatterns( + Collections.singletonList("*") + ).template(new Template(Settings.builder().put(DATA_ROUTING_REQUIRE_SETTING, "hot").build(), null, null)).build(); String composableTemplateName = "catch-all-composable-template"; - ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder() - .put(composableTemplateName, composableIndexTemplate).build()) + ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) + .metadata(Metadata.builder().put(composableTemplateName, composableIndexTemplate).build()) .build(); - Tuple migratedEntitiesTuple = - migrateToDataTiersRouting(clusterState, "data", composableTemplateName, REGISTRY, client, null); + Tuple migratedEntitiesTuple = migrateToDataTiersRouting( + clusterState, + "data", + composableTemplateName, + REGISTRY, + client, + null + ); assertThat(migratedEntitiesTuple.v2().removedIndexTemplateName, nullValue()); assertThat(migratedEntitiesTuple.v1().metadata().templatesV2().get(composableTemplateName), is(composableIndexTemplate)); } @@ -716,70 +891,91 @@ public void testMigrationSetsEnforceTierPreferenceToTrue() { assertFalse(migratedEntitiesTuple.v1().metadata().transientSettings().keySet().contains(DataTier.ENFORCE_DEFAULT_TIER_PREFERENCE)); } - private LifecyclePolicyMetadata getWarmColdPolicyMeta(SetPriorityAction setPriorityAction, ShrinkAction shrinkAction, - AllocateAction warmAllocateAction, AllocateAction coldAllocateAction) { - LifecyclePolicy policy = new LifecyclePolicy(lifecycleName, - Map.of("warm", - new Phase("warm", TimeValue.ZERO, Map.of(shrinkAction.getWriteableName(), shrinkAction, - warmAllocateAction.getWriteableName(), warmAllocateAction, setPriorityAction.getWriteableName(), setPriorityAction)), + private LifecyclePolicyMetadata getWarmColdPolicyMeta( + SetPriorityAction setPriorityAction, + ShrinkAction shrinkAction, + AllocateAction warmAllocateAction, + AllocateAction coldAllocateAction + ) { + LifecyclePolicy policy = new LifecyclePolicy( + lifecycleName, + Map.of( + "warm", + new Phase( + "warm", + TimeValue.ZERO, + Map.of( + shrinkAction.getWriteableName(), + shrinkAction, + warmAllocateAction.getWriteableName(), + warmAllocateAction, + setPriorityAction.getWriteableName(), + setPriorityAction + ) + ), "cold", new Phase("cold", TimeValue.ZERO, Map.of(coldAllocateAction.getWriteableName(), coldAllocateAction)) - )); - return new LifecyclePolicyMetadata(policy, Collections.emptyMap(), - randomNonNegativeLong(), randomNonNegativeLong()); + ) + ); + return new LifecyclePolicyMetadata(policy, Collections.emptyMap(), randomNonNegativeLong(), randomNonNegativeLong()); } private String getWarmPhaseDef() { - return "{\n" + - " \"policy\" : \"" + lifecycleName + "\",\n" + - " \"phase_definition\" : {\n" + - " \"min_age\" : \"0m\",\n" + - " \"actions\" : {\n" + - " \"allocate\" : {\n" + - " \"number_of_replicas\" : \"0\",\n" + - " \"require\" : {\n" + - " \"data\": \"cold\"\n" + - " }\n" + - " },\n" + - " \"set_priority\": {\n" + - " \"priority\": 100 \n" + - " },\n" + - " \"shrink\": {\n" + - " \"number_of_shards\": 2 \n" + - " }\n" + - " }\n" + - " },\n" + - " \"version\" : 1,\n" + - " \"modified_date_in_millis\" : 1578521007076\n" + - " }"; + return "{\n" + + " \"policy\" : \"" + + lifecycleName + + "\",\n" + + " \"phase_definition\" : {\n" + + " \"min_age\" : \"0m\",\n" + + " \"actions\" : {\n" + + " \"allocate\" : {\n" + + " \"number_of_replicas\" : \"0\",\n" + + " \"require\" : {\n" + + " \"data\": \"cold\"\n" + + " }\n" + + " },\n" + + " \"set_priority\": {\n" + + " \"priority\": 100 \n" + + " },\n" + + " \"shrink\": {\n" + + " \"number_of_shards\": 2 \n" + + " }\n" + + " }\n" + + " },\n" + + " \"version\" : 1,\n" + + " \"modified_date_in_millis\" : 1578521007076\n" + + " }"; } private String getColdPhaseDefinition() { - return "{\n" + - " \"policy\" : \"" + lifecycleName + "\",\n" + - " \"phase_definition\" : {\n" + - " \"min_age\" : \"0m\",\n" + - " \"actions\" : {\n" + - " \"allocate\" : {\n" + - " \"number_of_replicas\" : \"0\",\n" + - " \"require\" : {\n" + - " \"data\": \"cold\"\n" + - " }\n" + - " }\n" + - " }\n" + - " },\n" + - " \"version\" : 1,\n" + - " \"modified_date_in_millis\" : 1578521007076\n" + - " }"; + return "{\n" + + " \"policy\" : \"" + + lifecycleName + + "\",\n" + + " \"phase_definition\" : {\n" + + " \"min_age\" : \"0m\",\n" + + " \"actions\" : {\n" + + " \"allocate\" : {\n" + + " \"number_of_replicas\" : \"0\",\n" + + " \"require\" : {\n" + + " \"data\": \"cold\"\n" + + " }\n" + + " }\n" + + " }\n" + + " },\n" + + " \"version\" : 1,\n" + + " \"modified_date_in_millis\" : 1578521007076\n" + + " }"; } @SuppressWarnings("unchecked") private Map getPhaseDefinitionAsMap(LifecycleExecutionState newLifecycleState) { XContentType entityContentType = XContentType.fromMediaType("application/json"); - return (Map) XContentHelper.convertToMap(entityContentType.xContent(), - new ByteArrayInputStream(newLifecycleState.getPhaseDefinition().getBytes(StandardCharsets.UTF_8)), - false) - .get("phase_definition"); + return (Map) XContentHelper.convertToMap( + entityContentType.xContent(), + new ByteArrayInputStream(newLifecycleState.getPhaseDefinition().getBytes(StandardCharsets.UTF_8)), + false + ).get("phase_definition"); } } diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/ExecuteStepsUpdateTaskTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/ExecuteStepsUpdateTaskTests.java index 2af56b1c31d96..69534b38f8ed6 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/ExecuteStepsUpdateTaskTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/ExecuteStepsUpdateTaskTests.java @@ -18,10 +18,10 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.TransportAddress; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.index.Index; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.NodeRoles; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.ilm.ErrorStep; import org.elasticsearch.xpack.core.ilm.IndexLifecycleMetadata; import org.elasticsearch.xpack.core.ilm.LifecycleExecutionState; @@ -87,25 +87,46 @@ public void prepareState() throws IOException { mixedPolicyName = randomAlphaOfLengthBetween(5, 10); allClusterPolicyName = randomAlphaOfLengthBetween(1, 4); invalidPolicyName = randomAlphaOfLength(11); - Phase mixedPhase = new Phase("first_phase", TimeValue.ZERO, Collections.singletonMap(MockAction.NAME, - new MockAction(Arrays.asList(firstStep, secondStep, thirdStep)))); - Phase allClusterPhase = new Phase("first_phase", TimeValue.ZERO, Collections.singletonMap(MockAction.NAME, - new MockAction(Arrays.asList(firstStep, allClusterSecondStep)))); - Phase invalidPhase = new Phase("invalid_phase", TimeValue.ZERO, Collections.singletonMap(MockAction.NAME, - new MockAction(Arrays.asList(new MockClusterStateActionStep(firstStepKey, invalidStepKey))))); - LifecyclePolicy mixedPolicy = newTestLifecyclePolicy(mixedPolicyName, - Collections.singletonMap(mixedPhase.getName(), mixedPhase)); - LifecyclePolicy allClusterPolicy = newTestLifecyclePolicy(allClusterPolicyName, - Collections.singletonMap(allClusterPhase.getName(), allClusterPhase)); - LifecyclePolicy invalidPolicy = newTestLifecyclePolicy(invalidPolicyName, - Collections.singletonMap(invalidPhase.getName(), invalidPhase)); + Phase mixedPhase = new Phase( + "first_phase", + TimeValue.ZERO, + Collections.singletonMap(MockAction.NAME, new MockAction(Arrays.asList(firstStep, secondStep, thirdStep))) + ); + Phase allClusterPhase = new Phase( + "first_phase", + TimeValue.ZERO, + Collections.singletonMap(MockAction.NAME, new MockAction(Arrays.asList(firstStep, allClusterSecondStep))) + ); + Phase invalidPhase = new Phase( + "invalid_phase", + TimeValue.ZERO, + Collections.singletonMap( + MockAction.NAME, + new MockAction(Arrays.asList(new MockClusterStateActionStep(firstStepKey, invalidStepKey))) + ) + ); + LifecyclePolicy mixedPolicy = newTestLifecyclePolicy(mixedPolicyName, Collections.singletonMap(mixedPhase.getName(), mixedPhase)); + LifecyclePolicy allClusterPolicy = newTestLifecyclePolicy( + allClusterPolicyName, + Collections.singletonMap(allClusterPhase.getName(), allClusterPhase) + ); + LifecyclePolicy invalidPolicy = newTestLifecyclePolicy( + invalidPolicyName, + Collections.singletonMap(invalidPhase.getName(), invalidPhase) + ); Map policyMap = new HashMap<>(); - policyMap.put(mixedPolicyName, new LifecyclePolicyMetadata(mixedPolicy, Collections.emptyMap(), - randomNonNegativeLong(), randomNonNegativeLong())); - policyMap.put(allClusterPolicyName, new LifecyclePolicyMetadata(allClusterPolicy, Collections.emptyMap(), - randomNonNegativeLong(), randomNonNegativeLong())); - policyMap.put(invalidPolicyName, new LifecyclePolicyMetadata(invalidPolicy, Collections.emptyMap(), - randomNonNegativeLong(), randomNonNegativeLong())); + policyMap.put( + mixedPolicyName, + new LifecyclePolicyMetadata(mixedPolicy, Collections.emptyMap(), randomNonNegativeLong(), randomNonNegativeLong()) + ); + policyMap.put( + allClusterPolicyName, + new LifecyclePolicyMetadata(allClusterPolicy, Collections.emptyMap(), randomNonNegativeLong(), randomNonNegativeLong()) + ); + policyMap.put( + invalidPolicyName, + new LifecyclePolicyMetadata(invalidPolicy, Collections.emptyMap(), randomNonNegativeLong(), randomNonNegativeLong()) + ); policyStepsRegistry = new PolicyStepsRegistry(NamedXContentRegistry.EMPTY, client, null); indexName = randomAlphaOfLength(5); @@ -120,10 +141,11 @@ private IndexMetadata setupIndexPolicy(String policyName) { lifecycleState.setAction("init"); lifecycleState.setStep("init"); IndexMetadata indexMetadata = IndexMetadata.builder(indexName) - .settings(settings(Version.CURRENT) - .put(LifecycleSettings.LIFECYCLE_NAME, policyName)) + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); index = indexMetadata.getIndex(); Metadata metadata = Metadata.builder() .persistentSettings(settings(Version.CURRENT).build()) @@ -134,7 +156,8 @@ private IndexMetadata setupIndexPolicy(String policyName) { DiscoveryNode masterNode = DiscoveryNode.createLocal( NodeRoles.masterNode(settings(Version.CURRENT).build()), new TransportAddress(TransportAddress.META_ADDRESS, 9300), - nodeId); + nodeId + ); clusterState = ClusterState.builder(ClusterName.DEFAULT) .metadata(metadata) .nodes(DiscoveryNodes.builder().localNodeId(nodeId).masterNodeId(nodeId).add(masterNode).build()) @@ -188,21 +211,33 @@ public void testExecuteUntilFirstNonClusterStateStep() throws Exception { public void testExecuteInvalidStartStep() throws Exception { // Unset the index's phase/action/step to simulate starting from scratch LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder( - LifecycleExecutionState.fromIndexMetadata(clusterState.getMetadata().index(index))); + LifecycleExecutionState.fromIndexMetadata(clusterState.getMetadata().index(index)) + ); lifecycleState.setPhase(null); lifecycleState.setAction(null); lifecycleState.setStep(null); clusterState = ClusterState.builder(clusterState) - .metadata(Metadata.builder(clusterState.getMetadata()) - .put(IndexMetadata.builder(clusterState.getMetadata().index(index)) - .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()))).build(); + .metadata( + Metadata.builder(clusterState.getMetadata()) + .put( + IndexMetadata.builder(clusterState.getMetadata().index(index)) + .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()) + ) + ) + .build(); policyStepsRegistry.update(clusterState.metadata().custom(IndexLifecycleMetadata.TYPE)); Step invalidStep = new MockClusterStateActionStep(firstStepKey, secondStepKey); long now = randomNonNegativeLong(); - ExecuteStepsUpdateTask task = new ExecuteStepsUpdateTask(invalidPolicyName, index, - invalidStep, policyStepsRegistry, null, () -> now); + ExecuteStepsUpdateTask task = new ExecuteStepsUpdateTask( + invalidPolicyName, + index, + invalidStep, + policyStepsRegistry, + null, + () -> now + ); ClusterState newState = task.execute(clusterState); assertSame(newState, clusterState); } @@ -268,8 +303,7 @@ public void testClusterActionStepThrowsException() throws Exception { assertNull(task.getNextStepKey()); assertThat(lifecycleState.getPhaseTime(), nullValue()); assertThat(lifecycleState.getActionTime(), nullValue()); - assertThat(lifecycleState.getStepInfo(), - containsString("{\"type\":\"runtime_exception\",\"reason\":\"error\",\"stack_trace\":\"")); + assertThat(lifecycleState.getStepInfo(), containsString("{\"type\":\"runtime_exception\",\"reason\":\"error\",\"stack_trace\":\"")); } public void testClusterWaitStepThrowsException() throws Exception { @@ -287,20 +321,25 @@ public void testClusterWaitStepThrowsException() throws Exception { assertThat(secondStep.getExecuteCount(), equalTo(1L)); assertThat(lifecycleState.getPhaseTime(), nullValue()); assertThat(lifecycleState.getActionTime(), nullValue()); - assertThat(lifecycleState.getStepInfo(), - containsString("{\"type\":\"runtime_exception\",\"reason\":\"error\",\"stack_trace\":\"")); + assertThat(lifecycleState.getStepInfo(), containsString("{\"type\":\"runtime_exception\",\"reason\":\"error\",\"stack_trace\":\"")); } private void setStateToKey(StepKey stepKey) throws IOException { LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder( - LifecycleExecutionState.fromIndexMetadata(clusterState.getMetadata().index(index))); + LifecycleExecutionState.fromIndexMetadata(clusterState.getMetadata().index(index)) + ); lifecycleState.setPhase(stepKey.getPhase()); lifecycleState.setAction(stepKey.getAction()); lifecycleState.setStep(stepKey.getName()); clusterState = ClusterState.builder(clusterState) - .metadata(Metadata.builder(clusterState.getMetadata()) - .put(IndexMetadata.builder(clusterState.getMetadata().index(index)) - .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()))).build(); + .metadata( + Metadata.builder(clusterState.getMetadata()) + .put( + IndexMetadata.builder(clusterState.getMetadata().index(index)) + .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()) + ) + ) + .build(); policyStepsRegistry.update(clusterState.metadata().custom(IndexLifecycleMetadata.TYPE)); } } diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleInfoTransportActionTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleInfoTransportActionTests.java index 6d3104be4f2a5..7430566087ec1 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleInfoTransportActionTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleInfoTransportActionTests.java @@ -52,13 +52,17 @@ public void init() throws Exception { public void testAvailable() { IndexLifecycleInfoTransportAction featureSet = new IndexLifecycleInfoTransportAction( - mock(TransportService.class), mock(ActionFilters.class)); + mock(TransportService.class), + mock(ActionFilters.class) + ); assertThat(featureSet.available(), equalTo(true)); } public void testName() { IndexLifecycleInfoTransportAction featureSet = new IndexLifecycleInfoTransportAction( - mock(TransportService.class), mock(ActionFilters.class)); + mock(TransportService.class), + mock(ActionFilters.class) + ); assertThat(featureSet.name(), equalTo("ilm")); } @@ -89,8 +93,7 @@ public void testUsageStats() throws Exception { ClusterState clusterState = buildClusterState(policies, indexPolicies); Mockito.when(clusterService.state()).thenReturn(clusterState); - var usageAction = new IndexLifecycleUsageTransportAction(mock(TransportService.class), null, null, - mock(ActionFilters.class), null); + var usageAction = new IndexLifecycleUsageTransportAction(mock(TransportService.class), null, null, mock(ActionFilters.class), null); PlainActionFuture future = new PlainActionFuture<>(); usageAction.masterOperation(null, null, clusterState, future); IndexLifecycleFeatureSetUsage ilmUsage = (IndexLifecycleFeatureSetUsage) future.get().getUsage(); @@ -107,16 +110,18 @@ public void testUsageStats() throws Exception { private ClusterState buildClusterState(List lifecyclePolicies, Map indexPolicies) { Map lifecyclePolicyMetadatasMap = lifecyclePolicies.stream() - .map(p -> new LifecyclePolicyMetadata(p, Collections.emptyMap(), 1, 0L)) - .collect(Collectors.toMap(LifecyclePolicyMetadata::getName, Function.identity())); + .map(p -> new LifecyclePolicyMetadata(p, Collections.emptyMap(), 1, 0L)) + .collect(Collectors.toMap(LifecyclePolicyMetadata::getName, Function.identity())); IndexLifecycleMetadata indexLifecycleMetadata = new IndexLifecycleMetadata(lifecyclePolicyMetadatasMap, OperationMode.RUNNING); Metadata.Builder metadata = Metadata.builder().putCustom(IndexLifecycleMetadata.TYPE, indexLifecycleMetadata); indexPolicies.forEach((indexName, policyName) -> { - Settings indexSettings = Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, policyName) - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).build(); + Settings indexSettings = Settings.builder() + .put(LifecycleSettings.LIFECYCLE_NAME, policyName) + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .build(); IndexMetadata.Builder indexMetadata = IndexMetadata.builder(indexName).settings(indexSettings); metadata.put(indexMetadata); }); diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleMetadataTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleMetadataTests.java index 44fd254e1ec21..9996bf0c3678b 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleMetadataTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleMetadataTests.java @@ -11,14 +11,14 @@ import org.elasticsearch.cluster.Diff; import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.cluster.metadata.Metadata.Custom; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.Writeable.Reader; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractDiffableSerializationTestCase; import org.elasticsearch.test.VersionUtils; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ilm.AllocateAction; import org.elasticsearch.xpack.core.ilm.DeleteAction; import org.elasticsearch.xpack.core.ilm.ForceMergeAction; @@ -63,8 +63,10 @@ protected IndexLifecycleMetadata createTestInstance() { Map policies = new HashMap<>(numPolicies); for (int i = 0; i < numPolicies; i++) { LifecyclePolicy policy = randomTimeseriesLifecyclePolicy(randomAlphaOfLength(4) + i); - policies.put(policy.getName(), new LifecyclePolicyMetadata(policy, Collections.emptyMap(), - randomNonNegativeLong(), randomNonNegativeLong())); + policies.put( + policy.getName(), + new LifecyclePolicyMetadata(policy, Collections.emptyMap(), randomNonNegativeLong(), randomNonNegativeLong()) + ); } return new IndexLifecycleMetadata(policies, randomFrom(OperationMode.values())); } @@ -83,8 +85,11 @@ protected Reader instanceReader() { protected NamedWriteableRegistry getNamedWriteableRegistry() { return new NamedWriteableRegistry( Arrays.asList( - new NamedWriteableRegistry.Entry(LifecycleType.class, TimeseriesLifecycleType.TYPE, - (in) -> TimeseriesLifecycleType.INSTANCE), + new NamedWriteableRegistry.Entry( + LifecycleType.class, + TimeseriesLifecycleType.TYPE, + (in) -> TimeseriesLifecycleType.INSTANCE + ), new NamedWriteableRegistry.Entry(LifecycleAction.class, AllocateAction.NAME, AllocateAction::new), new NamedWriteableRegistry.Entry(LifecycleAction.class, WaitForSnapshotAction.NAME, WaitForSnapshotAction::new), new NamedWriteableRegistry.Entry(LifecycleAction.class, DeleteAction.NAME, DeleteAction::new), @@ -98,31 +103,43 @@ protected NamedWriteableRegistry getNamedWriteableRegistry() { new NamedWriteableRegistry.Entry(LifecycleAction.class, MigrateAction.NAME, MigrateAction::new), new NamedWriteableRegistry.Entry(LifecycleAction.class, SearchableSnapshotAction.NAME, SearchableSnapshotAction::new), new NamedWriteableRegistry.Entry(LifecycleAction.class, RollupILMAction.NAME, RollupILMAction::new) - )); + ) + ); } @Override protected NamedXContentRegistry xContentRegistry() { List entries = new ArrayList<>(ClusterModule.getNamedXWriteables()); - entries.addAll(Arrays.asList( - new NamedXContentRegistry.Entry(LifecycleType.class, new ParseField(TimeseriesLifecycleType.TYPE), - (p) -> TimeseriesLifecycleType.INSTANCE), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(AllocateAction.NAME), AllocateAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, - new ParseField(WaitForSnapshotAction.NAME), WaitForSnapshotAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ForceMergeAction.NAME), ForceMergeAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ReadOnlyAction.NAME), ReadOnlyAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(RolloverAction.NAME), RolloverAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ShrinkAction.NAME), ShrinkAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(FreezeAction.NAME), FreezeAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(SetPriorityAction.NAME), SetPriorityAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(UnfollowAction.NAME), UnfollowAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(MigrateAction.NAME), MigrateAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(SearchableSnapshotAction.NAME), - SearchableSnapshotAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(RollupILMAction.NAME), RollupILMAction::parse) - )); + entries.addAll( + Arrays.asList( + new NamedXContentRegistry.Entry( + LifecycleType.class, + new ParseField(TimeseriesLifecycleType.TYPE), + (p) -> TimeseriesLifecycleType.INSTANCE + ), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(AllocateAction.NAME), AllocateAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse), + new NamedXContentRegistry.Entry( + LifecycleAction.class, + new ParseField(WaitForSnapshotAction.NAME), + WaitForSnapshotAction::parse + ), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ForceMergeAction.NAME), ForceMergeAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ReadOnlyAction.NAME), ReadOnlyAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(RolloverAction.NAME), RolloverAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ShrinkAction.NAME), ShrinkAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(FreezeAction.NAME), FreezeAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(SetPriorityAction.NAME), SetPriorityAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(UnfollowAction.NAME), UnfollowAction::parse), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(MigrateAction.NAME), MigrateAction::parse), + new NamedXContentRegistry.Entry( + LifecycleAction.class, + new ParseField(SearchableSnapshotAction.NAME), + SearchableSnapshotAction::parse + ), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(RollupILMAction.NAME), RollupILMAction::parse) + ) + ); return new NamedXContentRegistry(entries); } @@ -134,8 +151,15 @@ protected Metadata.Custom mutateInstance(Metadata.Custom instance) { OperationMode mode = metadata.getOperationMode(); if (randomBoolean()) { String policyName = randomAlphaOfLength(10); - policies.put(policyName, new LifecyclePolicyMetadata(randomTimeseriesLifecyclePolicy(policyName), Collections.emptyMap(), - randomNonNegativeLong(), randomNonNegativeLong())); + policies.put( + policyName, + new LifecyclePolicyMetadata( + randomTimeseriesLifecyclePolicy(policyName), + Collections.emptyMap(), + randomNonNegativeLong(), + randomNonNegativeLong() + ) + ); } else { mode = randomValueOtherThan(metadata.getOperationMode(), () -> randomFrom(OperationMode.values())); } @@ -176,8 +200,15 @@ public static IndexLifecycleMetadata createTestInstance(int numPolicies, Operati phases.put(phaseName, new Phase(phaseName, after, actions)); } String policyName = randomAlphaOfLength(10); - policies.put(policyName, new LifecyclePolicyMetadata(newTestLifecyclePolicy(policyName, phases), Collections.emptyMap(), - randomNonNegativeLong(), randomNonNegativeLong())); + policies.put( + policyName, + new LifecyclePolicyMetadata( + newTestLifecyclePolicy(policyName, phases), + Collections.emptyMap(), + randomNonNegativeLong(), + randomNonNegativeLong() + ) + ); } return new IndexLifecycleMetadata(policies, mode); } diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleRunnerTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleRunnerTests.java index 3cec88385c0dd..635d66aa5fdfa 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleRunnerTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleRunnerTests.java @@ -23,17 +23,17 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.ToXContentObject; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.index.Index; import org.elasticsearch.test.ClusterServiceUtils; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.client.NoOpClient; import org.elasticsearch.threadpool.TestThreadPool; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.ToXContentObject; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.ilm.AsyncActionStep; import org.elasticsearch.xpack.core.ilm.AsyncWaitStep; import org.elasticsearch.xpack.core.ilm.ClusterStateActionStep; @@ -130,8 +130,11 @@ public void testRunPolicyTerminalPolicyStep() { PolicyStepsRegistry stepRegistry = createOneStepPolicyStepRegistry(policyName, step); ClusterService clusterService = mock(ClusterService.class); IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, historyStore, clusterService, threadPool, () -> 0L); - IndexMetadata indexMetadata = IndexMetadata.builder("my_index").settings(settings(Version.CURRENT)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + IndexMetadata indexMetadata = IndexMetadata.builder("my_index") + .settings(settings(Version.CURRENT)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); runner.runPolicyAfterStateChange(policyName, indexMetadata); @@ -144,8 +147,11 @@ public void testRunPolicyPhaseCompletePolicyStep() { PolicyStepsRegistry stepRegistry = createOneStepPolicyStepRegistry(policyName, step); ClusterService clusterService = mock(ClusterService.class); IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, historyStore, clusterService, threadPool, () -> 0L); - IndexMetadata indexMetadata = IndexMetadata.builder("my_index").settings(settings(Version.CURRENT)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + IndexMetadata indexMetadata = IndexMetadata.builder("my_index") + .settings(settings(Version.CURRENT)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); runner.runPolicyAfterStateChange(policyName, indexMetadata); runner.runPeriodicStep(policyName, Metadata.builder().put(indexMetadata, true).build(), indexMetadata); @@ -160,8 +166,11 @@ public void testRunPolicyPhaseCompleteWithMoreStepsPolicyStep() { PolicyStepsRegistry stepRegistry = createOneStepPolicyStepRegistry(policyName, step); ClusterService clusterService = mock(ClusterService.class); IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, historyStore, clusterService, threadPool, () -> 0L); - IndexMetadata indexMetadata = IndexMetadata.builder("my_index").settings(settings(Version.CURRENT)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + IndexMetadata indexMetadata = IndexMetadata.builder("my_index") + .settings(settings(Version.CURRENT)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); runner.runPolicyAfterStateChange(policyName, indexMetadata); runner.runPeriodicStep(policyName, Metadata.builder().put(indexMetadata, true).build(), indexMetadata); @@ -193,7 +202,8 @@ public void testRunPolicyErrorStep() { IndexMetadata indexMetadata = IndexMetadata.builder("test") .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) .putCustom(ILM_CUSTOM_METADATA_KEY, newState.build().asMap()) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) .build(); runner.runPolicyAfterStateChange(policyName, indexMetadata); @@ -212,10 +222,10 @@ public void testRunPolicyErrorStepOnRetryableFailedStep() { PhaseExecutionInfo phaseExecutionInfo = new PhaseExecutionInfo(policyName, phase, 1, randomNonNegativeLong()); String phaseJson = Strings.toString(phaseExecutionInfo); NoOpClient client = new NoOpClient(threadPool); - List waitForRolloverStepList = - action.toSteps(client, phaseName, null).stream() - .filter(s -> s.getKey().getName().equals(WaitForRolloverReadyStep.NAME)) - .collect(toList()); + List waitForRolloverStepList = action.toSteps(client, phaseName, null) + .stream() + .filter(s -> s.getKey().getName().equals(WaitForRolloverReadyStep.NAME)) + .collect(toList()); assertThat(waitForRolloverStepList.size(), is(1)); Step waitForRolloverStep = waitForRolloverStepList.get(0); StepKey stepKey = waitForRolloverStep.getKey(); @@ -234,7 +244,8 @@ public void testRunPolicyErrorStepOnRetryableFailedStep() { IndexMetadata indexMetadata = IndexMetadata.builder("test") .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) .putCustom(ILM_CUSTOM_METADATA_KEY, newState.build().asMap()) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) .build(); runner.runPeriodicStep(policyName, Metadata.builder().put(indexMetadata, true).build(), indexMetadata); @@ -249,23 +260,20 @@ public void testRunStateChangePolicyWithNoNextStep() throws Exception { PolicyStepsRegistry stepRegistry = createOneStepPolicyStepRegistry(policyName, step); ThreadPool threadPool = new TestThreadPool("name"); IndexMetadata indexMetadata = IndexMetadata.builder("test") - .settings(Settings.builder() - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) - .put(LifecycleSettings.LIFECYCLE_NAME, policyName)) + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) + .put(LifecycleSettings.LIFECYCLE_NAME, policyName) + ) .build(); ClusterService clusterService = ClusterServiceUtils.createClusterService(threadPool); DiscoveryNode node = clusterService.localNode(); IndexLifecycleMetadata ilm = new IndexLifecycleMetadata(Collections.emptyMap(), OperationMode.RUNNING); ClusterState state = ClusterState.builder(new ClusterName("cluster")) - .metadata(Metadata.builder() - .put(indexMetadata, true) - .putCustom(IndexLifecycleMetadata.TYPE, ilm)) - .nodes(DiscoveryNodes.builder() - .add(node) - .masterNodeId(node.getId()) - .localNodeId(node.getId())) + .metadata(Metadata.builder().put(indexMetadata, true).putCustom(IndexLifecycleMetadata.TYPE, ilm)) + .nodes(DiscoveryNodes.builder().add(node).masterNodeId(node.getId()).localNodeId(node.getId())) .build(); ClusterServiceUtils.setState(clusterService, state); IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, historyStore, clusterService, threadPool, () -> 0L); @@ -308,29 +316,25 @@ public void testRunStateChangePolicyWithNextStep() throws Exception { .setStep("cluster_state_action_step") .build(); IndexMetadata indexMetadata = IndexMetadata.builder("test") - .settings(Settings.builder() - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) - .put(LifecycleSettings.LIFECYCLE_NAME, policyName)) + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) + .put(LifecycleSettings.LIFECYCLE_NAME, policyName) + ) .putCustom(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY, les.asMap()) .build(); ClusterService clusterService = ClusterServiceUtils.createClusterService(threadPool); DiscoveryNode node = clusterService.localNode(); IndexLifecycleMetadata ilm = new IndexLifecycleMetadata(Collections.emptyMap(), OperationMode.RUNNING); ClusterState state = ClusterState.builder(new ClusterName("cluster")) - .metadata(Metadata.builder() - .put(indexMetadata, true) - .putCustom(IndexLifecycleMetadata.TYPE, ilm)) - .nodes(DiscoveryNodes.builder() - .add(node) - .masterNodeId(node.getId()) - .localNodeId(node.getId())) + .metadata(Metadata.builder().put(indexMetadata, true).putCustom(IndexLifecycleMetadata.TYPE, ilm)) + .nodes(DiscoveryNodes.builder().add(node).masterNodeId(node.getId()).localNodeId(node.getId())) .build(); ClusterServiceUtils.setState(clusterService, state); long stepTime = randomLong(); - IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, historyStore, - clusterService, threadPool, () -> stepTime); + IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, historyStore, clusterService, threadPool, () -> stepTime); ClusterState before = clusterService.state(); CountDownLatch latch = new CountDownLatch(1); @@ -341,8 +345,9 @@ public void testRunStateChangePolicyWithNextStep() throws Exception { // The cluster state can take a few extra milliseconds to update after the steps are executed assertBusy(() -> assertNotEquals(before, clusterService.state())); - LifecycleExecutionState newExecutionState = LifecycleExecutionState - .fromIndexMetadata(clusterService.state().metadata().index(indexMetadata.getIndex())); + LifecycleExecutionState newExecutionState = LifecycleExecutionState.fromIndexMetadata( + clusterService.state().metadata().index(indexMetadata.getIndex()) + ); assertThat(newExecutionState.getPhase(), equalTo("phase")); assertThat(newExecutionState.getAction(), equalTo("action")); assertThat(newExecutionState.getStep(), equalTo("next_cluster_state_action_step")); @@ -352,13 +357,21 @@ public void testRunStateChangePolicyWithNextStep() throws Exception { clusterService.close(); threadPool.shutdownNow(); - ILMHistoryItem historyItem = historyStore.getItems().stream() + ILMHistoryItem historyItem = historyStore.getItems() + .stream() .findFirst() .orElseThrow(() -> new AssertionError("failed to register ILM history")); - assertThat(historyItem.toString(), - containsString("{\"index\":\"test\",\"policy\":\"foo\",\"@timestamp\":" + stepTime + - ",\"success\":true,\"state\":{\"phase\":\"phase\",\"action\":\"action\"," + - "\"step\":\"next_cluster_state_action_step\",\"step_time\":\"" + stepTime + "\"}}")); + assertThat( + historyItem.toString(), + containsString( + "{\"index\":\"test\",\"policy\":\"foo\",\"@timestamp\":" + + stepTime + + ",\"success\":true,\"state\":{\"phase\":\"phase\",\"action\":\"action\"," + + "\"step\":\"next_cluster_state_action_step\",\"step_time\":\"" + + stepTime + + "\"}}" + ) + ); } public void testRunPeriodicPolicyWithFailureToReadPolicy() throws Exception { @@ -392,29 +405,25 @@ public void doTestRunPolicyWithFailureToReadPolicy(boolean asyncAction, boolean .setStep("cluster_state_action_step") .build(); IndexMetadata indexMetadata = IndexMetadata.builder("test") - .settings(Settings.builder() - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) - .put(LifecycleSettings.LIFECYCLE_NAME, policyName)) + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) + .put(LifecycleSettings.LIFECYCLE_NAME, policyName) + ) .putCustom(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY, les.asMap()) .build(); ClusterService clusterService = ClusterServiceUtils.createClusterService(threadPool); DiscoveryNode node = clusterService.localNode(); IndexLifecycleMetadata ilm = new IndexLifecycleMetadata(Collections.emptyMap(), OperationMode.RUNNING); ClusterState state = ClusterState.builder(new ClusterName("cluster")) - .metadata(Metadata.builder() - .put(indexMetadata, true) - .putCustom(IndexLifecycleMetadata.TYPE, ilm)) - .nodes(DiscoveryNodes.builder() - .add(node) - .masterNodeId(node.getId()) - .localNodeId(node.getId())) + .metadata(Metadata.builder().put(indexMetadata, true).putCustom(IndexLifecycleMetadata.TYPE, ilm)) + .nodes(DiscoveryNodes.builder().add(node).masterNodeId(node.getId()).localNodeId(node.getId())) .build(); ClusterServiceUtils.setState(clusterService, state); long stepTime = randomLong(); - IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, historyStore, - clusterService, threadPool, () -> stepTime); + IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, historyStore, clusterService, threadPool, () -> stepTime); ClusterState before = clusterService.state(); if (asyncAction) { @@ -427,15 +436,18 @@ public void doTestRunPolicyWithFailureToReadPolicy(boolean asyncAction, boolean // The cluster state can take a few extra milliseconds to update after the steps are executed assertBusy(() -> assertNotEquals(before, clusterService.state())); - LifecycleExecutionState newExecutionState = LifecycleExecutionState - .fromIndexMetadata(clusterService.state().metadata().index(indexMetadata.getIndex())); + LifecycleExecutionState newExecutionState = LifecycleExecutionState.fromIndexMetadata( + clusterService.state().metadata().index(indexMetadata.getIndex()) + ); assertThat(newExecutionState.getPhase(), equalTo("phase")); assertThat(newExecutionState.getAction(), equalTo("action")); assertThat(newExecutionState.getStep(), equalTo("cluster_state_action_step")); assertThat(step.getExecuteCount(), equalTo(0L)); assertThat(nextStep.getExecuteCount(), equalTo(0L)); - assertThat(newExecutionState.getStepInfo(), - containsString("{\"type\":\"illegal_argument_exception\",\"reason\":\"fake failure retrieving step\"}")); + assertThat( + newExecutionState.getStepInfo(), + containsString("{\"type\":\"illegal_argument_exception\",\"reason\":\"fake failure retrieving step\"}") + ); clusterService.close(); threadPool.shutdownNow(); } @@ -447,23 +459,20 @@ public void testRunAsyncActionDoesNotRun() { PolicyStepsRegistry stepRegistry = createOneStepPolicyStepRegistry(policyName, step); ThreadPool threadPool = new TestThreadPool("name"); IndexMetadata indexMetadata = IndexMetadata.builder("test") - .settings(Settings.builder() - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) - .put(LifecycleSettings.LIFECYCLE_NAME, policyName)) + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) + .put(LifecycleSettings.LIFECYCLE_NAME, policyName) + ) .build(); ClusterService clusterService = ClusterServiceUtils.createClusterService(threadPool); DiscoveryNode node = clusterService.localNode(); IndexLifecycleMetadata ilm = new IndexLifecycleMetadata(Collections.emptyMap(), OperationMode.RUNNING); ClusterState state = ClusterState.builder(new ClusterName("cluster")) - .metadata(Metadata.builder() - .put(indexMetadata, true) - .putCustom(IndexLifecycleMetadata.TYPE, ilm)) - .nodes(DiscoveryNodes.builder() - .add(node) - .masterNodeId(node.getId()) - .localNodeId(node.getId())) + .metadata(Metadata.builder().put(indexMetadata, true).putCustom(IndexLifecycleMetadata.TYPE, ilm)) + .nodes(DiscoveryNodes.builder().add(node).masterNodeId(node.getId()).localNodeId(node.getId())) .build(); ClusterServiceUtils.setState(clusterService, state); IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, historyStore, clusterService, threadPool, () -> 0L); @@ -504,24 +513,21 @@ public void testRunStateChangePolicyWithAsyncActionNextStep() throws Exception { .setStep("cluster_state_action_step") .build(); IndexMetadata indexMetadata = IndexMetadata.builder("test") - .settings(Settings.builder() - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) - .put(LifecycleSettings.LIFECYCLE_NAME, policyName)) + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) + .put(LifecycleSettings.LIFECYCLE_NAME, policyName) + ) .putCustom(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY, les.asMap()) .build(); ClusterService clusterService = ClusterServiceUtils.createClusterService(threadPool); DiscoveryNode node = clusterService.localNode(); IndexLifecycleMetadata ilm = new IndexLifecycleMetadata(Collections.emptyMap(), OperationMode.RUNNING); ClusterState state = ClusterState.builder(new ClusterName("cluster")) - .metadata(Metadata.builder() - .put(indexMetadata, true) - .putCustom(IndexLifecycleMetadata.TYPE, ilm)) - .nodes(DiscoveryNodes.builder() - .add(node) - .masterNodeId(node.getId()) - .localNodeId(node.getId())) + .metadata(Metadata.builder().put(indexMetadata, true).putCustom(IndexLifecycleMetadata.TYPE, ilm)) + .nodes(DiscoveryNodes.builder().add(node).masterNodeId(node.getId()).localNodeId(node.getId())) .build(); logger.info("--> state: {}", state); ClusterServiceUtils.setState(clusterService, state); @@ -546,12 +552,17 @@ public void testRunStateChangePolicyWithAsyncActionNextStep() throws Exception { clusterService.close(); threadPool.shutdownNow(); - ILMHistoryItem historyItem = historyStore.getItems().stream() + ILMHistoryItem historyItem = historyStore.getItems() + .stream() .findFirst() .orElseThrow(() -> new AssertionError("failed to register ILM history")); - assertThat(historyItem.toString(), - containsString("{\"index\":\"test\",\"policy\":\"foo\",\"@timestamp\":0,\"success\":true," + - "\"state\":{\"phase\":\"phase\",\"action\":\"action\",\"step\":\"async_action_step\",\"step_time\":\"0\"}}")); + assertThat( + historyItem.toString(), + containsString( + "{\"index\":\"test\",\"policy\":\"foo\",\"@timestamp\":0,\"success\":true," + + "\"state\":{\"phase\":\"phase\",\"action\":\"action\",\"step\":\"async_action_step\",\"step_time\":\"0\"}}" + ) + ); } public void testRunPeriodicStep() throws Exception { @@ -578,24 +589,21 @@ public void testRunPeriodicStep() throws Exception { .setStep("cluster_state_action_step") .build(); IndexMetadata indexMetadata = IndexMetadata.builder("test") - .settings(Settings.builder() - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) - .put(LifecycleSettings.LIFECYCLE_NAME, policyName)) + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) + .put(LifecycleSettings.LIFECYCLE_NAME, policyName) + ) .putCustom(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY, les.asMap()) .build(); ClusterService clusterService = ClusterServiceUtils.createClusterService(threadPool); DiscoveryNode node = clusterService.localNode(); IndexLifecycleMetadata ilm = new IndexLifecycleMetadata(Collections.emptyMap(), OperationMode.RUNNING); ClusterState state = ClusterState.builder(new ClusterName("cluster")) - .metadata(Metadata.builder() - .put(indexMetadata, true) - .putCustom(IndexLifecycleMetadata.TYPE, ilm)) - .nodes(DiscoveryNodes.builder() - .add(node) - .masterNodeId(node.getId()) - .localNodeId(node.getId())) + .metadata(Metadata.builder().put(indexMetadata, true).putCustom(IndexLifecycleMetadata.TYPE, ilm)) + .nodes(DiscoveryNodes.builder().add(node).masterNodeId(node.getId()).localNodeId(node.getId())) .build(); logger.info("--> state: {}", state); ClusterServiceUtils.setState(clusterService, state); @@ -623,21 +631,26 @@ public void testRunPolicyClusterStateActionStep() { PolicyStepsRegistry stepRegistry = createOneStepPolicyStepRegistry(policyName, step); ClusterService clusterService = mock(ClusterService.class); IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, historyStore, clusterService, threadPool, () -> 0L); - IndexMetadata indexMetadata = IndexMetadata.builder("my_index").settings(settings(Version.CURRENT)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + IndexMetadata indexMetadata = IndexMetadata.builder("my_index") + .settings(settings(Version.CURRENT)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); runner.runPolicyAfterStateChange(policyName, indexMetadata); - final ExecuteStepsUpdateTaskMatcher taskMatcher = - new ExecuteStepsUpdateTaskMatcher(indexMetadata.getIndex(), policyName, step); - Mockito.verify(clusterService, Mockito.times(1)).submitStateUpdateTask( - Mockito.eq("ilm-execute-cluster-state-steps [{\"phase\":\"phase\",\"action\":\"action\"," + - "\"name\":\"cluster_state_action_step\"} => null]"), + final ExecuteStepsUpdateTaskMatcher taskMatcher = new ExecuteStepsUpdateTaskMatcher(indexMetadata.getIndex(), policyName, step); + Mockito.verify(clusterService, Mockito.times(1)) + .submitStateUpdateTask( + Mockito.eq( + "ilm-execute-cluster-state-steps [{\"phase\":\"phase\",\"action\":\"action\"," + + "\"name\":\"cluster_state_action_step\"} => null]" + ), Mockito.argThat(taskMatcher), eq(IndexLifecycleRunner.ILM_TASK_CONFIG), any(), Mockito.argThat(taskMatcher) - ); + ); Mockito.verifyNoMoreInteractions(clusterService); } @@ -648,21 +661,26 @@ public void testRunPolicyClusterStateWaitStep() { PolicyStepsRegistry stepRegistry = createOneStepPolicyStepRegistry(policyName, step); ClusterService clusterService = mock(ClusterService.class); IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, historyStore, clusterService, threadPool, () -> 0L); - IndexMetadata indexMetadata = IndexMetadata.builder("my_index").settings(settings(Version.CURRENT)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + IndexMetadata indexMetadata = IndexMetadata.builder("my_index") + .settings(settings(Version.CURRENT)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); runner.runPolicyAfterStateChange(policyName, indexMetadata); - final ExecuteStepsUpdateTaskMatcher taskMatcher = - new ExecuteStepsUpdateTaskMatcher(indexMetadata.getIndex(), policyName, step); - Mockito.verify(clusterService, Mockito.times(1)).submitStateUpdateTask( - Mockito.eq("ilm-execute-cluster-state-steps [{\"phase\":\"phase\",\"action\":\"action\"," + - "\"name\":\"cluster_state_action_step\"} => null]"), + final ExecuteStepsUpdateTaskMatcher taskMatcher = new ExecuteStepsUpdateTaskMatcher(indexMetadata.getIndex(), policyName, step); + Mockito.verify(clusterService, Mockito.times(1)) + .submitStateUpdateTask( + Mockito.eq( + "ilm-execute-cluster-state-steps [{\"phase\":\"phase\",\"action\":\"action\"," + + "\"name\":\"cluster_state_action_step\"} => null]" + ), Mockito.argThat(taskMatcher), eq(IndexLifecycleRunner.ILM_TASK_CONFIG), any(), Mockito.argThat(taskMatcher) - ); + ); Mockito.verifyNoMoreInteractions(clusterService); } @@ -675,8 +693,11 @@ public void testRunPolicyAsyncActionStepClusterStateChangeIgnored() { PolicyStepsRegistry stepRegistry = createOneStepPolicyStepRegistry(policyName, step); ClusterService clusterService = mock(ClusterService.class); IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, historyStore, clusterService, threadPool, () -> 0L); - IndexMetadata indexMetadata = IndexMetadata.builder("my_index").settings(settings(Version.CURRENT)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + IndexMetadata indexMetadata = IndexMetadata.builder("my_index") + .settings(settings(Version.CURRENT)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); runner.runPolicyAfterStateChange(policyName, indexMetadata); @@ -693,8 +714,11 @@ public void testRunPolicyAsyncWaitStepClusterStateChangeIgnored() { PolicyStepsRegistry stepRegistry = createOneStepPolicyStepRegistry(policyName, step); ClusterService clusterService = mock(ClusterService.class); IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, historyStore, clusterService, threadPool, () -> 0L); - IndexMetadata indexMetadata = IndexMetadata.builder("my_index").settings(settings(Version.CURRENT)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + IndexMetadata indexMetadata = IndexMetadata.builder("my_index") + .settings(settings(Version.CURRENT)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); runner.runPolicyAfterStateChange(policyName, indexMetadata); @@ -705,27 +729,40 @@ public void testRunPolicyAsyncWaitStepClusterStateChangeIgnored() { public void testRunPolicyThatDoesntExist() { String policyName = "cluster_state_action_policy"; ClusterService clusterService = mock(ClusterService.class); - IndexLifecycleRunner runner = new IndexLifecycleRunner(new PolicyStepsRegistry(NamedXContentRegistry.EMPTY, null, null), - historyStore, clusterService, threadPool, () -> 0L); - IndexMetadata indexMetadata = IndexMetadata.builder("my_index").settings(settings(Version.CURRENT)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + IndexLifecycleRunner runner = new IndexLifecycleRunner( + new PolicyStepsRegistry(NamedXContentRegistry.EMPTY, null, null), + historyStore, + clusterService, + threadPool, + () -> 0L + ); + IndexMetadata indexMetadata = IndexMetadata.builder("my_index") + .settings(settings(Version.CURRENT)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); // verify that no exception is thrown runner.runPolicyAfterStateChange(policyName, indexMetadata); - final SetStepInfoUpdateTaskMatcher taskMatcher = new SetStepInfoUpdateTaskMatcher(indexMetadata.getIndex(), policyName, null, + final SetStepInfoUpdateTaskMatcher taskMatcher = new SetStepInfoUpdateTaskMatcher( + indexMetadata.getIndex(), + policyName, + null, (builder, params) -> { builder.startObject(); builder.field("reason", "policy [does_not_exist] does not exist"); builder.field("type", "illegal_argument_exception"); builder.endObject(); return builder; - }); - Mockito.verify(clusterService, Mockito.times(1)).submitStateUpdateTask( - Mockito.eq("ilm-set-step-info {policy [cluster_state_action_policy], index [my_index], currentStep [null]}"), - Mockito.argThat(taskMatcher), - eq(IndexLifecycleRunner.ILM_TASK_CONFIG), - any(), - Mockito.argThat(taskMatcher) + } ); + Mockito.verify(clusterService, Mockito.times(1)) + .submitStateUpdateTask( + Mockito.eq("ilm-set-step-info {policy [cluster_state_action_policy], index [my_index], currentStep [null]}"), + Mockito.argThat(taskMatcher), + eq(IndexLifecycleRunner.ILM_TASK_CONFIG), + any(), + Mockito.argThat(taskMatcher) + ); Mockito.verifyNoMoreInteractions(clusterService); } @@ -786,62 +823,79 @@ public void testIsReadyToTransition() { String policyName = "async_action_policy"; StepKey stepKey = new StepKey("phase", MockAction.NAME, MockAction.NAME); MockAsyncActionStep step = new MockAsyncActionStep(stepKey, null); - SortedMap lifecyclePolicyMap = new TreeMap<>(Collections.singletonMap(policyName, - new LifecyclePolicyMetadata(createPolicy(policyName, null, step.getKey()), new HashMap<>(), - randomNonNegativeLong(), randomNonNegativeLong()))); + SortedMap lifecyclePolicyMap = new TreeMap<>( + Collections.singletonMap( + policyName, + new LifecyclePolicyMetadata( + createPolicy(policyName, null, step.getKey()), + new HashMap<>(), + randomNonNegativeLong(), + randomNonNegativeLong() + ) + ) + ); Map firstStepMap = Collections.singletonMap(policyName, step); Map policySteps = Collections.singletonMap(step.getKey(), step); Map> stepMap = Collections.singletonMap(policyName, policySteps); - PolicyStepsRegistry policyStepsRegistry = new PolicyStepsRegistry(lifecyclePolicyMap, firstStepMap, - stepMap, NamedXContentRegistry.EMPTY, null, null); + PolicyStepsRegistry policyStepsRegistry = new PolicyStepsRegistry( + lifecyclePolicyMap, + firstStepMap, + stepMap, + NamedXContentRegistry.EMPTY, + null, + null + ); ClusterService clusterService = mock(ClusterService.class); final AtomicLong now = new AtomicLong(5); - IndexLifecycleRunner runner = new IndexLifecycleRunner(policyStepsRegistry, historyStore, - clusterService, threadPool, now::get); - IndexMetadata indexMetadata = IndexMetadata.builder("my_index").settings(settings(Version.CURRENT)) + IndexLifecycleRunner runner = new IndexLifecycleRunner(policyStepsRegistry, historyStore, clusterService, threadPool, now::get); + IndexMetadata indexMetadata = IndexMetadata.builder("my_index") + .settings(settings(Version.CURRENT)) .numberOfShards(randomIntBetween(1, 5)) .numberOfReplicas(randomIntBetween(0, 5)) .build(); // With no time, always transition - assertTrue("index should be able to transition with no creation date", - runner.isReadyToTransitionToThisPhase(policyName, indexMetadata, "phase")); + assertTrue( + "index should be able to transition with no creation date", + runner.isReadyToTransitionToThisPhase(policyName, indexMetadata, "phase") + ); LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); lifecycleState.setIndexCreationDate(10L); indexMetadata = IndexMetadata.builder(indexMetadata) - .settings(Settings.builder() - .put(indexMetadata.getSettings()) - .build()) + .settings(Settings.builder().put(indexMetadata.getSettings()).build()) .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()) .build(); // Index is not old enough to transition - assertFalse("index is not able to transition if it isn't old enough", - runner.isReadyToTransitionToThisPhase(policyName, indexMetadata, "phase")); + assertFalse( + "index is not able to transition if it isn't old enough", + runner.isReadyToTransitionToThisPhase(policyName, indexMetadata, "phase") + ); // Set to the fuuuuuttuuuuuuurre now.set(Long.MAX_VALUE); - assertTrue("index should be able to transition past phase's age", - runner.isReadyToTransitionToThisPhase(policyName, indexMetadata, "phase")); + assertTrue( + "index should be able to transition past phase's age", + runner.isReadyToTransitionToThisPhase(policyName, indexMetadata, "phase") + ); // Come back to the "present" now.set(5L); indexMetadata = IndexMetadata.builder(indexMetadata) - .settings(Settings.builder() - .put(indexMetadata.getSettings()) - .put(LifecycleSettings.LIFECYCLE_ORIGINATION_DATE, 3L) - .build()) + .settings(Settings.builder().put(indexMetadata.getSettings()).put(LifecycleSettings.LIFECYCLE_ORIGINATION_DATE, 3L).build()) .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()) .build(); - assertTrue("index should be able to transition due to the origination date indicating it's old enough", - runner.isReadyToTransitionToThisPhase(policyName, indexMetadata, "phase")); + assertTrue( + "index should be able to transition due to the origination date indicating it's old enough", + runner.isReadyToTransitionToThisPhase(policyName, indexMetadata, "phase") + ); } private static LifecyclePolicy createPolicy(String policyName, StepKey safeStep, StepKey unsafeStep) { Map phases = new HashMap<>(); if (safeStep != null) { assert MockAction.NAME.equals(safeStep.getAction()) : "The safe action needs to be MockAction.NAME"; - assert unsafeStep == null - || safeStep.getPhase().equals(unsafeStep.getPhase()) == false : "safe and unsafe actions must be in different phases"; + assert unsafeStep == null || safeStep.getPhase().equals(unsafeStep.getPhase()) == false + : "safe and unsafe actions must be in different phases"; Map actions = new HashMap<>(); List steps = Collections.singletonList(new MockStep(safeStep, null)); MockAction safeAction = new MockAction(steps, true); @@ -861,17 +915,21 @@ private static LifecyclePolicy createPolicy(String policyName, StepKey safeStep, return newTestLifecyclePolicy(policyName, phases); } - public static void assertClusterStateOnNextStep(ClusterState oldClusterState, Index index, StepKey currentStep, StepKey nextStep, - ClusterState newClusterState, long now) { + public static void assertClusterStateOnNextStep( + ClusterState oldClusterState, + Index index, + StepKey currentStep, + StepKey nextStep, + ClusterState newClusterState, + long now + ) { assertNotSame(oldClusterState, newClusterState); Metadata newMetadata = newClusterState.metadata(); assertNotSame(oldClusterState.metadata(), newMetadata); IndexMetadata newIndexMetadata = newMetadata.getIndexSafe(index); assertNotSame(oldClusterState.metadata().index(index), newIndexMetadata); - LifecycleExecutionState newLifecycleState = LifecycleExecutionState - .fromIndexMetadata(newClusterState.metadata().index(index)); - LifecycleExecutionState oldLifecycleState = LifecycleExecutionState - .fromIndexMetadata(oldClusterState.metadata().index(index)); + LifecycleExecutionState newLifecycleState = LifecycleExecutionState.fromIndexMetadata(newClusterState.metadata().index(index)); + LifecycleExecutionState oldLifecycleState = LifecycleExecutionState.fromIndexMetadata(oldClusterState.metadata().index(index)); assertNotSame(oldLifecycleState, newLifecycleState); assertEquals(nextStep.getPhase(), newLifecycleState.getPhase()); assertEquals(nextStep.getAction(), newLifecycleState.getAction()); @@ -925,8 +983,12 @@ public void setLatch(CountDownLatch latch) { } @Override - public void performAction(IndexMetadata indexMetadata, ClusterState currentState, - ClusterStateObserver observer, ActionListener listener) { + public void performAction( + IndexMetadata indexMetadata, + ClusterState currentState, + ClusterStateObserver observer, + ActionListener listener + ) { executeCount++; if (latch != null) { latch.countDown(); @@ -1085,10 +1147,10 @@ public boolean matches(SetStepInfoUpdateTask other) { if (other == null) { return false; } - return Objects.equals(index, other.getIndex()) && - Objects.equals(policy, other.getPolicy())&& - Objects.equals(currentStepKey, other.getCurrentStepKey()) && - Objects.equals(xContentToString(stepInfo), xContentToString(other.getStepInfo())); + return Objects.equals(index, other.getIndex()) + && Objects.equals(policy, other.getPolicy()) + && Objects.equals(currentStepKey, other.getCurrentStepKey()) + && Objects.equals(xContentToString(stepInfo), xContentToString(other.getStepInfo())); } private String xContentToString(ToXContentObject xContent) { @@ -1120,9 +1182,9 @@ public boolean matches(ExecuteStepsUpdateTask other) { if (other == null) { return false; } - return Objects.equals(index, other.getIndex()) && - Objects.equals(policy, other.getPolicy()) && - Objects.equals(startStep, other.getStartStep()); + return Objects.equals(index, other.getIndex()) + && Objects.equals(policy, other.getPolicy()) + && Objects.equals(startStep, other.getStartStep()); } } @@ -1144,8 +1206,13 @@ public static class MockPolicyStepsRegistry extends PolicyStepsRegistry { private BiFunction fn = null; private static Logger logger = LogManager.getLogger(MockPolicyStepsRegistry.class); - MockPolicyStepsRegistry(SortedMap lifecyclePolicyMap, Map firstStepMap, - Map> stepMap, NamedXContentRegistry xContentRegistry, Client client) { + MockPolicyStepsRegistry( + SortedMap lifecyclePolicyMap, + Map firstStepMap, + Map> stepMap, + NamedXContentRegistry xContentRegistry, + Client client + ) { super(lifecyclePolicyMap, firstStepMap, stepMap, xContentRegistry, client, null); } diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleServiceTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleServiceTests.java index 725983dfe8b22..a6e4f6561d886 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleServiceTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleServiceTests.java @@ -99,7 +99,8 @@ public void prepareServices() { masterNode = DiscoveryNode.createLocal( NodeRoles.masterNode(settings(Version.CURRENT).build()), new TransportAddress(TransportAddress.META_ADDRESS, 9300), - nodeId); + nodeId + ); now = randomNonNegativeLong(); Clock clock = Clock.fixed(Instant.ofEpochMilli(now), ZoneId.of(randomFrom(ZoneId.getAvailableZoneIds()))); @@ -110,8 +111,9 @@ public void prepareServices() { return null; }).when(executorService).execute(any()); Settings settings = Settings.builder().put(LifecycleSettings.LIFECYCLE_POLL_INTERVAL, "1s").build(); - when(clusterService.getClusterSettings()).thenReturn(new ClusterSettings(settings, - Collections.singleton(LifecycleSettings.LIFECYCLE_POLL_INTERVAL_SETTING))); + when(clusterService.getClusterSettings()).thenReturn( + new ClusterSettings(settings, Collections.singleton(LifecycleSettings.LIFECYCLE_POLL_INTERVAL_SETTING)) + ); when(clusterService.lifecycleState()).thenReturn(State.STARTED); Client client = mock(Client.class); @@ -122,8 +124,17 @@ public void prepareServices() { when(client.settings()).thenReturn(Settings.EMPTY); threadPool = new TestThreadPool("test"); - indexLifecycleService = new IndexLifecycleService(Settings.EMPTY, client, clusterService, threadPool, - clock, () -> now, null, null, null); + indexLifecycleService = new IndexLifecycleService( + Settings.EMPTY, + client, + clusterService, + threadPool, + clock, + () -> now, + null, + null, + null + ); Mockito.verify(clusterService).addListener(indexLifecycleService); Mockito.verify(clusterService).addStateApplier(indexLifecycleService); } @@ -135,22 +146,27 @@ public void cleanup() { threadPool.shutdownNow(); } - public void testStoppedModeSkip() { String policyName = randomAlphaOfLengthBetween(1, 20); - IndexLifecycleRunnerTests.MockClusterStateActionStep mockStep = - new IndexLifecycleRunnerTests.MockClusterStateActionStep(randomStepKey(), randomStepKey()); + IndexLifecycleRunnerTests.MockClusterStateActionStep mockStep = new IndexLifecycleRunnerTests.MockClusterStateActionStep( + randomStepKey(), + randomStepKey() + ); MockAction mockAction = new MockAction(Collections.singletonList(mockStep)); Phase phase = new Phase("phase", TimeValue.ZERO, Collections.singletonMap("action", mockAction)); LifecyclePolicy policy = newTestLifecyclePolicy(policyName, Collections.singletonMap(phase.getName(), phase)); SortedMap policyMap = new TreeMap<>(); - policyMap.put(policyName, new LifecyclePolicyMetadata(policy, Collections.emptyMap(), - randomNonNegativeLong(), randomNonNegativeLong())); + policyMap.put( + policyName, + new LifecyclePolicyMetadata(policy, Collections.emptyMap(), randomNonNegativeLong(), randomNonNegativeLong()) + ); Index index = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20)); IndexMetadata indexMetadata = IndexMetadata.builder(index.getName()) .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME_SETTING.getKey(), policyName)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); - ImmutableOpenMap.Builder indices = ImmutableOpenMap. builder() + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); + ImmutableOpenMap.Builder indices = ImmutableOpenMap.builder() .fPut(index.getName(), indexMetadata); Metadata metadata = Metadata.builder() .putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata(policyMap, OperationMode.STOPPED)) @@ -170,14 +186,18 @@ public void testStoppedModeSkip() { public void testRequestedStopOnShrink() { Step.StepKey mockShrinkStep = new Step.StepKey(randomAlphaOfLength(4), ShrinkAction.NAME, ShrinkStep.NAME); String policyName = randomAlphaOfLengthBetween(1, 20); - IndexLifecycleRunnerTests.MockClusterStateActionStep mockStep = - new IndexLifecycleRunnerTests.MockClusterStateActionStep(mockShrinkStep, randomStepKey()); + IndexLifecycleRunnerTests.MockClusterStateActionStep mockStep = new IndexLifecycleRunnerTests.MockClusterStateActionStep( + mockShrinkStep, + randomStepKey() + ); MockAction mockAction = new MockAction(Collections.singletonList(mockStep)); Phase phase = new Phase("phase", TimeValue.ZERO, Collections.singletonMap("action", mockAction)); LifecyclePolicy policy = newTestLifecyclePolicy(policyName, Collections.singletonMap(phase.getName(), phase)); SortedMap policyMap = new TreeMap<>(); - policyMap.put(policyName, new LifecyclePolicyMetadata(policy, Collections.emptyMap(), - randomNonNegativeLong(), randomNonNegativeLong())); + policyMap.put( + policyName, + new LifecyclePolicyMetadata(policy, Collections.emptyMap(), randomNonNegativeLong(), randomNonNegativeLong()) + ); Index index = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20)); LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); lifecycleState.setPhase(mockShrinkStep.getPhase()); @@ -186,8 +206,10 @@ public void testRequestedStopOnShrink() { IndexMetadata indexMetadata = IndexMetadata.builder(index.getName()) .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME_SETTING.getKey(), policyName)) .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); - ImmutableOpenMap.Builder indices = ImmutableOpenMap. builder() + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); + ImmutableOpenMap.Builder indices = ImmutableOpenMap.builder() .fPut(index.getName(), indexMetadata); Metadata metadata = Metadata.builder() .putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata(policyMap, OperationMode.STOPPING)) @@ -213,7 +235,8 @@ public void testRequestedStopOnShrink() { public void testRequestedStopInShrinkActionButNotShrinkStep() { // test all the shrink action steps that ILM can be stopped during (basically all of them minus the actual shrink) ShrinkAction action = new ShrinkAction(1, null); - action.toSteps(mock(Client.class), "warm", randomStepKey()).stream() + action.toSteps(mock(Client.class), "warm", randomStepKey()) + .stream() .map(sk -> sk.getKey().getName()) .filter(name -> name.equals(ShrinkStep.NAME) == false) .forEach(this::verifyCanStopWithStep); @@ -223,14 +246,18 @@ public void testRequestedStopInShrinkActionButNotShrinkStep() { private void verifyCanStopWithStep(String stoppableStep) { Step.StepKey mockShrinkStep = new Step.StepKey(randomAlphaOfLength(4), ShrinkAction.NAME, stoppableStep); String policyName = randomAlphaOfLengthBetween(1, 20); - IndexLifecycleRunnerTests.MockClusterStateActionStep mockStep = - new IndexLifecycleRunnerTests.MockClusterStateActionStep(mockShrinkStep, randomStepKey()); + IndexLifecycleRunnerTests.MockClusterStateActionStep mockStep = new IndexLifecycleRunnerTests.MockClusterStateActionStep( + mockShrinkStep, + randomStepKey() + ); MockAction mockAction = new MockAction(Collections.singletonList(mockStep)); Phase phase = new Phase("phase", TimeValue.ZERO, Collections.singletonMap("action", mockAction)); LifecyclePolicy policy = newTestLifecyclePolicy(policyName, Collections.singletonMap(phase.getName(), phase)); SortedMap policyMap = new TreeMap<>(); - policyMap.put(policyName, new LifecyclePolicyMetadata(policy, Collections.emptyMap(), - randomNonNegativeLong(), randomNonNegativeLong())); + policyMap.put( + policyName, + new LifecyclePolicyMetadata(policy, Collections.emptyMap(), randomNonNegativeLong(), randomNonNegativeLong()) + ); Index index = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20)); LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); lifecycleState.setPhase(mockShrinkStep.getPhase()); @@ -239,8 +266,10 @@ private void verifyCanStopWithStep(String stoppableStep) { IndexMetadata indexMetadata = IndexMetadata.builder(index.getName()) .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME_SETTING.getKey(), policyName)) .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); - ImmutableOpenMap.Builder indices = ImmutableOpenMap. builder() + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); + ImmutableOpenMap.Builder indices = ImmutableOpenMap.builder() .fPut(index.getName(), indexMetadata); Metadata metadata = Metadata.builder() .putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata(policyMap, OperationMode.STOPPING)) @@ -257,8 +286,8 @@ private void verifyCanStopWithStep(String stoppableStep) { doAnswer(invocationOnMock -> { changedOperationMode.set(true); return null; - }).when(clusterService).submitStateUpdateTask(eq("ilm_operation_mode_update {OperationMode STOPPED}"), - any(OperationModeUpdateTask.class)); + }).when(clusterService) + .submitStateUpdateTask(eq("ilm_operation_mode_update {OperationMode STOPPED}"), any(OperationModeUpdateTask.class)); indexLifecycleService.applyClusterState(event); indexLifecycleService.triggerPolicies(currentState, true); assertTrue(changedOperationMode.get()); @@ -267,14 +296,18 @@ private void verifyCanStopWithStep(String stoppableStep) { public void testRequestedStopOnSafeAction() { String policyName = randomAlphaOfLengthBetween(1, 20); Step.StepKey currentStepKey = randomStepKey(); - IndexLifecycleRunnerTests.MockClusterStateActionStep mockStep = - new IndexLifecycleRunnerTests.MockClusterStateActionStep(currentStepKey, randomStepKey()); + IndexLifecycleRunnerTests.MockClusterStateActionStep mockStep = new IndexLifecycleRunnerTests.MockClusterStateActionStep( + currentStepKey, + randomStepKey() + ); MockAction mockAction = new MockAction(Collections.singletonList(mockStep)); Phase phase = new Phase("phase", TimeValue.ZERO, Collections.singletonMap("action", mockAction)); LifecyclePolicy policy = newTestLifecyclePolicy(policyName, Collections.singletonMap(phase.getName(), phase)); SortedMap policyMap = new TreeMap<>(); - policyMap.put(policyName, new LifecyclePolicyMetadata(policy, Collections.emptyMap(), - randomNonNegativeLong(), randomNonNegativeLong())); + policyMap.put( + policyName, + new LifecyclePolicyMetadata(policy, Collections.emptyMap(), randomNonNegativeLong(), randomNonNegativeLong()) + ); Index index = new Index(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20)); LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); lifecycleState.setPhase(currentStepKey.getPhase()); @@ -283,8 +316,10 @@ public void testRequestedStopOnSafeAction() { IndexMetadata indexMetadata = IndexMetadata.builder(index.getName()) .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME_SETTING.getKey(), policyName)) .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); - ImmutableOpenMap.Builder indices = ImmutableOpenMap. builder() + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); + ImmutableOpenMap.Builder indices = ImmutableOpenMap.builder() .fPut(index.getName(), indexMetadata); Metadata metadata = Metadata.builder() .putCustom(IndexLifecycleMetadata.TYPE, new IndexLifecycleMetadata(policyMap, OperationMode.STOPPING)) @@ -310,8 +345,8 @@ public void testRequestedStopOnSafeAction() { assertThat(task.getILMOperationMode(), equalTo(OperationMode.STOPPED)); moveToMaintenance.set(true); return null; - }).when(clusterService).submitStateUpdateTask(eq("ilm_operation_mode_update {OperationMode STOPPED}"), - any(OperationModeUpdateTask.class)); + }).when(clusterService) + .submitStateUpdateTask(eq("ilm_operation_mode_update {OperationMode STOPPED}"), any(OperationModeUpdateTask.class)); indexLifecycleService.applyClusterState(event); indexLifecycleService.triggerPolicies(currentState, randomBoolean()); @@ -338,7 +373,7 @@ public void testOperationModeUpdateTaskPriority() { private void verifyOperationModeUpdateTaskPriority(OperationMode mode, Priority expectedPriority) { verify(clusterService).submitStateUpdateTask( - Mockito.eq("ilm_operation_mode_update {OperationMode " + mode.name() +"}"), + Mockito.eq("ilm_operation_mode_update {OperationMode " + mode.name() + "}"), argThat(new ArgumentMatcher() { Priority actualPriority = null; @@ -392,35 +427,47 @@ public void doTestExceptionStillProcessesOtherIndices(boolean useOnMaster) { boolean failStep1 = randomBoolean(); if (useOnMaster) { ((IndexLifecycleRunnerTests.MockAsyncActionStep) i1mockStep).setLatch(stepLatch); - ((IndexLifecycleRunnerTests.MockAsyncActionStep) i1mockStep) - .setException(failStep1 ? new IllegalArgumentException("forcing a failure for index 1") : null); + ((IndexLifecycleRunnerTests.MockAsyncActionStep) i1mockStep).setException( + failStep1 ? new IllegalArgumentException("forcing a failure for index 1") : null + ); ((IndexLifecycleRunnerTests.MockAsyncActionStep) i2mockStep).setLatch(stepLatch); - ((IndexLifecycleRunnerTests.MockAsyncActionStep) i2mockStep) - .setException(failStep1 ? null : new IllegalArgumentException("forcing a failure for index 2")); + ((IndexLifecycleRunnerTests.MockAsyncActionStep) i2mockStep).setException( + failStep1 ? null : new IllegalArgumentException("forcing a failure for index 2") + ); } else { ((IndexLifecycleRunnerTests.MockClusterStateActionStep) i1mockStep).setLatch(stepLatch); - ((IndexLifecycleRunnerTests.MockClusterStateActionStep) i1mockStep) - .setException(failStep1 ? new IllegalArgumentException("forcing a failure for index 1") : null); + ((IndexLifecycleRunnerTests.MockClusterStateActionStep) i1mockStep).setException( + failStep1 ? new IllegalArgumentException("forcing a failure for index 1") : null + ); ((IndexLifecycleRunnerTests.MockClusterStateActionStep) i1mockStep).setLatch(stepLatch); - ((IndexLifecycleRunnerTests.MockClusterStateActionStep) i1mockStep) - .setException(failStep1 ? null : new IllegalArgumentException("forcing a failure for index 2")); + ((IndexLifecycleRunnerTests.MockClusterStateActionStep) i1mockStep).setException( + failStep1 ? null : new IllegalArgumentException("forcing a failure for index 2") + ); } SortedMap policyMap = new TreeMap<>(); - policyMap.put(policy1, new LifecyclePolicyMetadata(i1policy, Collections.emptyMap(), - randomNonNegativeLong(), randomNonNegativeLong())); - policyMap.put(policy2, new LifecyclePolicyMetadata(i2policy, Collections.emptyMap(), - randomNonNegativeLong(), randomNonNegativeLong())); + policyMap.put( + policy1, + new LifecyclePolicyMetadata(i1policy, Collections.emptyMap(), randomNonNegativeLong(), randomNonNegativeLong()) + ); + policyMap.put( + policy2, + new LifecyclePolicyMetadata(i2policy, Collections.emptyMap(), randomNonNegativeLong(), randomNonNegativeLong()) + ); IndexMetadata i1indexMetadata = IndexMetadata.builder(index1.getName()) .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME_SETTING.getKey(), policy1)) .putCustom(ILM_CUSTOM_METADATA_KEY, i1lifecycleState.build().asMap()) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); IndexMetadata i2indexMetadata = IndexMetadata.builder(index2.getName()) .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME_SETTING.getKey(), policy1)) .putCustom(ILM_CUSTOM_METADATA_KEY, i2lifecycleState.build().asMap()) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); - ImmutableOpenMap.Builder indices = ImmutableOpenMap. builder() + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); + ImmutableOpenMap.Builder indices = ImmutableOpenMap.builder() .fPut(index1.getName(), i1indexMetadata) .fPut(index2.getName(), i2indexMetadata); @@ -450,8 +497,17 @@ public void doTestExceptionStillProcessesOtherIndices(boolean useOnMaster) { } public void testClusterChangedWaitsForTheStateToBeRecovered() { - IndexLifecycleService ilmService = new IndexLifecycleService(Settings.EMPTY, mock(Client.class), clusterService, threadPool, - systemUTC(), () -> now, null, null, null) { + IndexLifecycleService ilmService = new IndexLifecycleService( + Settings.EMPTY, + mock(Client.class), + clusterService, + threadPool, + systemUTC(), + () -> now, + null, + null, + null + ) { @Override void onMaster(ClusterState clusterState) { @@ -480,9 +536,11 @@ public void testTriggeredDifferentJob() { public void testParsingOriginationDateBeforeIndexCreation() { Settings indexSettings = Settings.builder().put(LifecycleSettings.LIFECYCLE_PARSE_ORIGINATION_DATE, true).build(); Index index = new Index("invalid_index_name", UUID.randomUUID().toString()); - expectThrows(IllegalArgumentException.class, - "The parse origination date setting was configured for index " + index.getName() + - " but the index name did not match the expected format", + expectThrows( + IllegalArgumentException.class, + "The parse origination date setting was configured for index " + + index.getName() + + " but the index name did not match the expected format", () -> indexLifecycleService.beforeIndexAddedToCluster(index, indexSettings) ); @@ -496,31 +554,51 @@ public void testParsingOriginationDateBeforeIndexCreation() { public void testIndicesOnShuttingDownNodesInDangerousStep() { ClusterState state = ClusterState.builder(ClusterName.DEFAULT).build(); - assertThat(IndexLifecycleService.indicesOnShuttingDownNodesInDangerousStep(state, "regular_node"), - equalTo(Collections.emptySet())); - assertThat(IndexLifecycleService.indicesOnShuttingDownNodesInDangerousStep(state, "shutdown_node"), - equalTo(Collections.emptySet())); + assertThat(IndexLifecycleService.indicesOnShuttingDownNodesInDangerousStep(state, "regular_node"), equalTo(Collections.emptySet())); + assertThat( + IndexLifecycleService.indicesOnShuttingDownNodesInDangerousStep(state, "shutdown_node"), + equalTo(Collections.emptySet()) + ); IndexMetadata nonDangerousIndex = IndexMetadata.builder("no_danger") .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME_SETTING.getKey(), "mypolicy")) - .putCustom(ILM_CUSTOM_METADATA_KEY, LifecycleExecutionState.builder() - .setPhase("warm") - .setAction("shrink") - .setStep(GenerateUniqueIndexNameStep.NAME) - .build().asMap()) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + .putCustom( + ILM_CUSTOM_METADATA_KEY, + LifecycleExecutionState.builder() + .setPhase("warm") + .setAction("shrink") + .setStep(GenerateUniqueIndexNameStep.NAME) + .build() + .asMap() + ) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); IndexMetadata dangerousIndex = IndexMetadata.builder("danger") - .settings(settings(Version.CURRENT) - .put(LifecycleSettings.LIFECYCLE_NAME_SETTING.getKey(), "mypolicy") - .put(IndexMetadata.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + "_id", "shutdown_node")) - .putCustom(ILM_CUSTOM_METADATA_KEY, LifecycleExecutionState.builder() - .setPhase("warm") - .setAction("shrink") - .setStep(randomFrom(SetSingleNodeAllocateStep.NAME, CheckShrinkReadyStep.NAME, - ShrinkStep.NAME, ShrunkShardsAllocatedStep.NAME)) - .build().asMap()) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); - ImmutableOpenMap.Builder indices = ImmutableOpenMap. builder() + .settings( + settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME_SETTING.getKey(), "mypolicy") + .put(IndexMetadata.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + "_id", "shutdown_node") + ) + .putCustom( + ILM_CUSTOM_METADATA_KEY, + LifecycleExecutionState.builder() + .setPhase("warm") + .setAction("shrink") + .setStep( + randomFrom( + SetSingleNodeAllocateStep.NAME, + CheckShrinkReadyStep.NAME, + ShrinkStep.NAME, + ShrunkShardsAllocatedStep.NAME + ) + ) + .build() + .asMap() + ) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); + ImmutableOpenMap.Builder indices = ImmutableOpenMap.builder() .fPut("no_danger", nonDangerousIndex) .fPut("danger", dangerousIndex); @@ -532,43 +610,64 @@ public void testIndicesOnShuttingDownNodesInDangerousStep() { state = ClusterState.builder(ClusterName.DEFAULT) .metadata(metadata) - .nodes(DiscoveryNodes.builder().localNodeId(nodeId).masterNodeId(nodeId) - .add(masterNode) - .add(DiscoveryNode.createLocal( - NodeRoles.masterNode(settings(Version.CURRENT).build()), - new TransportAddress(TransportAddress.META_ADDRESS, 9301), - "regular_node")) - .add(DiscoveryNode.createLocal( - NodeRoles.masterNode(settings(Version.CURRENT).build()), - new TransportAddress(TransportAddress.META_ADDRESS, 9302), - "shutdown_node")) - .build()) + .nodes( + DiscoveryNodes.builder() + .localNodeId(nodeId) + .masterNodeId(nodeId) + .add(masterNode) + .add( + DiscoveryNode.createLocal( + NodeRoles.masterNode(settings(Version.CURRENT).build()), + new TransportAddress(TransportAddress.META_ADDRESS, 9301), + "regular_node" + ) + ) + .add( + DiscoveryNode.createLocal( + NodeRoles.masterNode(settings(Version.CURRENT).build()), + new TransportAddress(TransportAddress.META_ADDRESS, 9302), + "shutdown_node" + ) + ) + .build() + ) .build(); // No danger yet, because no node is shutting down - assertThat(IndexLifecycleService.indicesOnShuttingDownNodesInDangerousStep(state, "regular_node"), - equalTo(Collections.emptySet())); - assertThat(IndexLifecycleService.indicesOnShuttingDownNodesInDangerousStep(state, "shutdown_node"), - equalTo(Collections.emptySet())); + assertThat(IndexLifecycleService.indicesOnShuttingDownNodesInDangerousStep(state, "regular_node"), equalTo(Collections.emptySet())); + assertThat( + IndexLifecycleService.indicesOnShuttingDownNodesInDangerousStep(state, "shutdown_node"), + equalTo(Collections.emptySet()) + ); state = ClusterState.builder(state) - .metadata(Metadata.builder(state.metadata()) - .putCustom(NodesShutdownMetadata.TYPE, new NodesShutdownMetadata(Collections.singletonMap("shutdown_node", - SingleNodeShutdownMetadata.builder() - .setNodeId("shutdown_node") - .setReason("shut down for test") - .setStartedAtMillis(randomNonNegativeLong()) - .setType(SingleNodeShutdownMetadata.Type.RESTART) - .build()))) - .build()) + .metadata( + Metadata.builder(state.metadata()) + .putCustom( + NodesShutdownMetadata.TYPE, + new NodesShutdownMetadata( + Collections.singletonMap( + "shutdown_node", + SingleNodeShutdownMetadata.builder() + .setNodeId("shutdown_node") + .setReason("shut down for test") + .setStartedAtMillis(randomNonNegativeLong()) + .setType(SingleNodeShutdownMetadata.Type.RESTART) + .build() + ) + ) + ) + .build() + ) .build(); - assertThat(IndexLifecycleService.indicesOnShuttingDownNodesInDangerousStep(state, "regular_node"), - equalTo(Collections.emptySet())); + assertThat(IndexLifecycleService.indicesOnShuttingDownNodesInDangerousStep(state, "regular_node"), equalTo(Collections.emptySet())); // No danger, because this is a "RESTART" type shutdown - assertThat("restart type shutdowns are not considered dangerous", + assertThat( + "restart type shutdowns are not considered dangerous", IndexLifecycleService.indicesOnShuttingDownNodesInDangerousStep(state, "shutdown_node"), - equalTo(Collections.emptySet())); + equalTo(Collections.emptySet()) + ); final SingleNodeShutdownMetadata.Type type = randomFrom( SingleNodeShutdownMetadata.Type.REMOVE, @@ -576,20 +675,31 @@ public void testIndicesOnShuttingDownNodesInDangerousStep() { ); final String targetNodeName = type == SingleNodeShutdownMetadata.Type.REPLACE ? randomAlphaOfLengthBetween(10, 20) : null; state = ClusterState.builder(state) - .metadata(Metadata.builder(state.metadata()) - .putCustom(NodesShutdownMetadata.TYPE, new NodesShutdownMetadata(Collections.singletonMap("shutdown_node", - SingleNodeShutdownMetadata.builder() - .setNodeId("shutdown_node") - .setReason("shut down for test") - .setStartedAtMillis(randomNonNegativeLong()) - .setType(type) - .setTargetNodeName(targetNodeName) - .build()))) - .build()) + .metadata( + Metadata.builder(state.metadata()) + .putCustom( + NodesShutdownMetadata.TYPE, + new NodesShutdownMetadata( + Collections.singletonMap( + "shutdown_node", + SingleNodeShutdownMetadata.builder() + .setNodeId("shutdown_node") + .setReason("shut down for test") + .setStartedAtMillis(randomNonNegativeLong()) + .setType(type) + .setTargetNodeName(targetNodeName) + .build() + ) + ) + ) + .build() + ) .build(); // The dangerous index should be calculated as being in danger now - assertThat(IndexLifecycleService.indicesOnShuttingDownNodesInDangerousStep(state, "shutdown_node"), - equalTo(Collections.singleton("danger"))); + assertThat( + IndexLifecycleService.indicesOnShuttingDownNodesInDangerousStep(state, "shutdown_node"), + equalTo(Collections.singleton("danger")) + ); } } diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleTransitionTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleTransitionTests.java index 9e0f1bf3547c4..e2f3c7f9b1768 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleTransitionTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleTransitionTests.java @@ -16,14 +16,14 @@ import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.ToXContentObject; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.Index; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.client.NoOpClient; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.ToXContentObject; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.ilm.AbstractStepTestCase; import org.elasticsearch.xpack.core.ilm.ErrorStep; import org.elasticsearch.xpack.core.ilm.IndexLifecycleMetadata; @@ -69,25 +69,39 @@ public class IndexLifecycleTransitionTests extends ESTestCase { public void testMoveClusterStateToNextStep() { String indexName = "my_index"; - LifecyclePolicy policy = randomValueOtherThanMany(p -> p.getPhases().size() == 0, - () -> LifecyclePolicyTests.randomTestLifecyclePolicy("policy")); - Phase nextPhase = policy.getPhases().values().stream() - .findFirst().orElseThrow(() -> new AssertionError("expected next phase to be present")); + LifecyclePolicy policy = randomValueOtherThanMany( + p -> p.getPhases().size() == 0, + () -> LifecyclePolicyTests.randomTestLifecyclePolicy("policy") + ); + Phase nextPhase = policy.getPhases() + .values() + .stream() + .findFirst() + .orElseThrow(() -> new AssertionError("expected next phase to be present")); List policyMetadatas = Collections.singletonList( - new LifecyclePolicyMetadata(policy, Collections.emptyMap(), randomNonNegativeLong(), randomNonNegativeLong())); + new LifecyclePolicyMetadata(policy, Collections.emptyMap(), randomNonNegativeLong(), randomNonNegativeLong()) + ); Step.StepKey currentStep = new Step.StepKey("current_phase", "current_action", "current_step"); Step.StepKey nextStep = new Step.StepKey(nextPhase.getName(), "next_action", "next_step"); long now = randomNonNegativeLong(); // test going from null lifecycle settings to next step - ClusterState clusterState = buildClusterState(indexName, - Settings.builder() - .put(LifecycleSettings.LIFECYCLE_NAME, policy.getName()), LifecycleExecutionState.builder().build(), policyMetadatas); + ClusterState clusterState = buildClusterState( + indexName, + Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, policy.getName()), + LifecycleExecutionState.builder().build(), + policyMetadatas + ); Index index = clusterState.metadata().index(indexName).getIndex(); - PolicyStepsRegistry stepsRegistry = createOneStepPolicyStepRegistry(policy.getName(), - new MockStep(nextStep, nextStep)); - ClusterState newClusterState = IndexLifecycleTransition.moveClusterStateToStep(index, clusterState, nextStep, - () -> now, stepsRegistry, false); + PolicyStepsRegistry stepsRegistry = createOneStepPolicyStepRegistry(policy.getName(), new MockStep(nextStep, nextStep)); + ClusterState newClusterState = IndexLifecycleTransition.moveClusterStateToStep( + index, + clusterState, + nextStep, + () -> now, + stepsRegistry, + false + ); assertClusterStateOnNextStep(clusterState, index, currentStep, nextStep, newClusterState, now); LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); @@ -95,42 +109,50 @@ public void testMoveClusterStateToNextStep() { lifecycleState.setAction(currentStep.getAction()); lifecycleState.setStep(currentStep.getName()); // test going from set currentStep settings to nextStep - Settings.Builder indexSettingsBuilder = Settings.builder() - .put(LifecycleSettings.LIFECYCLE_NAME, policy.getName()); + Settings.Builder indexSettingsBuilder = Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, policy.getName()); if (randomBoolean()) { lifecycleState.setStepInfo(randomAlphaOfLength(20)); } clusterState = buildClusterState(indexName, indexSettingsBuilder, lifecycleState.build(), policyMetadatas); index = clusterState.metadata().index(indexName).getIndex(); - newClusterState = IndexLifecycleTransition.moveClusterStateToStep(index, clusterState, - nextStep, () -> now, stepsRegistry, false); + newClusterState = IndexLifecycleTransition.moveClusterStateToStep(index, clusterState, nextStep, () -> now, stepsRegistry, false); assertClusterStateOnNextStep(clusterState, index, currentStep, nextStep, newClusterState, now); } public void testMoveClusterStateToNextStepSamePhase() { String indexName = "my_index"; - LifecyclePolicy policy = randomValueOtherThanMany(p -> p.getPhases().size() == 0, - () -> LifecyclePolicyTests.randomTestLifecyclePolicy("policy")); + LifecyclePolicy policy = randomValueOtherThanMany( + p -> p.getPhases().size() == 0, + () -> LifecyclePolicyTests.randomTestLifecyclePolicy("policy") + ); List policyMetadatas = Collections.singletonList( - new LifecyclePolicyMetadata(policy, Collections.emptyMap(), randomNonNegativeLong(), randomNonNegativeLong())); + new LifecyclePolicyMetadata(policy, Collections.emptyMap(), randomNonNegativeLong(), randomNonNegativeLong()) + ); Step.StepKey currentStep = new Step.StepKey("current_phase", "current_action", "current_step"); Step.StepKey nextStep = new Step.StepKey("current_phase", "next_action", "next_step"); long now = randomNonNegativeLong(); - ClusterState clusterState = buildClusterState(indexName, - Settings.builder() - .put(LifecycleSettings.LIFECYCLE_NAME, policy.getName()), + ClusterState clusterState = buildClusterState( + indexName, + Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, policy.getName()), LifecycleExecutionState.builder() .setPhase(currentStep.getPhase()) .setAction(currentStep.getAction()) .setStep(currentStep.getName()) - .build(), policyMetadatas); + .build(), + policyMetadatas + ); Index index = clusterState.metadata().index(indexName).getIndex(); - PolicyStepsRegistry stepsRegistry = createOneStepPolicyStepRegistry(policy.getName(), - new MockStep(nextStep, nextStep)); - ClusterState newClusterState = IndexLifecycleTransition.moveClusterStateToStep(index, clusterState, nextStep, - () -> now, stepsRegistry, false); + PolicyStepsRegistry stepsRegistry = createOneStepPolicyStepRegistry(policy.getName(), new MockStep(nextStep, nextStep)); + ClusterState newClusterState = IndexLifecycleTransition.moveClusterStateToStep( + index, + clusterState, + nextStep, + () -> now, + stepsRegistry, + false + ); assertClusterStateOnNextStep(clusterState, index, currentStep, nextStep, newClusterState, now); LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); @@ -141,39 +163,47 @@ public void testMoveClusterStateToNextStepSamePhase() { lifecycleState.setStepInfo(randomAlphaOfLength(20)); } - Settings.Builder indexSettingsBuilder = Settings.builder() - .put(LifecycleSettings.LIFECYCLE_NAME, policy.getName()); + Settings.Builder indexSettingsBuilder = Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, policy.getName()); clusterState = buildClusterState(indexName, indexSettingsBuilder, lifecycleState.build(), policyMetadatas); index = clusterState.metadata().index(indexName).getIndex(); - newClusterState = IndexLifecycleTransition.moveClusterStateToStep(index, clusterState, nextStep, - () -> now, stepsRegistry, false); + newClusterState = IndexLifecycleTransition.moveClusterStateToStep(index, clusterState, nextStep, () -> now, stepsRegistry, false); assertClusterStateOnNextStep(clusterState, index, currentStep, nextStep, newClusterState, now); } public void testMoveClusterStateToNextStepSameAction() { String indexName = "my_index"; - LifecyclePolicy policy = randomValueOtherThanMany(p -> p.getPhases().size() == 0, - () -> LifecyclePolicyTests.randomTestLifecyclePolicy("policy")); + LifecyclePolicy policy = randomValueOtherThanMany( + p -> p.getPhases().size() == 0, + () -> LifecyclePolicyTests.randomTestLifecyclePolicy("policy") + ); List policyMetadatas = Collections.singletonList( - new LifecyclePolicyMetadata(policy, Collections.emptyMap(), randomNonNegativeLong(), randomNonNegativeLong())); + new LifecyclePolicyMetadata(policy, Collections.emptyMap(), randomNonNegativeLong(), randomNonNegativeLong()) + ); Step.StepKey currentStep = new Step.StepKey("current_phase", "current_action", "current_step"); Step.StepKey nextStep = new Step.StepKey("current_phase", "current_action", "next_step"); long now = randomNonNegativeLong(); - ClusterState clusterState = buildClusterState(indexName, - Settings.builder() - .put(LifecycleSettings.LIFECYCLE_NAME, policy.getName()), + ClusterState clusterState = buildClusterState( + indexName, + Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, policy.getName()), LifecycleExecutionState.builder() .setPhase(currentStep.getPhase()) .setAction(currentStep.getAction()) .setStep(currentStep.getName()) - .build(), policyMetadatas); + .build(), + policyMetadatas + ); Index index = clusterState.metadata().index(indexName).getIndex(); - PolicyStepsRegistry stepsRegistry = createOneStepPolicyStepRegistry(policy.getName(), - new MockStep(nextStep, nextStep)); - ClusterState newClusterState = IndexLifecycleTransition.moveClusterStateToStep(index, clusterState, nextStep, - () -> now, stepsRegistry, false); + PolicyStepsRegistry stepsRegistry = createOneStepPolicyStepRegistry(policy.getName(), new MockStep(nextStep, nextStep)); + ClusterState newClusterState = IndexLifecycleTransition.moveClusterStateToStep( + index, + clusterState, + nextStep, + () -> now, + stepsRegistry, + false + ); assertClusterStateOnNextStep(clusterState, index, currentStep, nextStep, newClusterState, now); LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); @@ -184,25 +214,29 @@ public void testMoveClusterStateToNextStepSameAction() { lifecycleState.setStepInfo(randomAlphaOfLength(20)); } - Settings.Builder indexSettingsBuilder = Settings.builder() - .put(LifecycleSettings.LIFECYCLE_NAME, policy.getName()); + Settings.Builder indexSettingsBuilder = Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, policy.getName()); clusterState = buildClusterState(indexName, indexSettingsBuilder, lifecycleState.build(), policyMetadatas); index = clusterState.metadata().index(indexName).getIndex(); - newClusterState = IndexLifecycleTransition.moveClusterStateToStep(index, clusterState, nextStep, - () -> now, stepsRegistry, false); + newClusterState = IndexLifecycleTransition.moveClusterStateToStep(index, clusterState, nextStep, () -> now, stepsRegistry, false); assertClusterStateOnNextStep(clusterState, index, currentStep, nextStep, newClusterState, now); } public void testSuccessfulValidatedMoveClusterStateToNextStep() { String indexName = "my_index"; String policyName = "my_policy"; - LifecyclePolicy policy = randomValueOtherThanMany(p -> p.getPhases().size() == 0, - () -> LifecyclePolicyTests.randomTestLifecyclePolicy(policyName)); - Phase nextPhase = policy.getPhases().values().stream() - .findFirst().orElseThrow(() -> new AssertionError("expected next phase to be present")); + LifecyclePolicy policy = randomValueOtherThanMany( + p -> p.getPhases().size() == 0, + () -> LifecyclePolicyTests.randomTestLifecyclePolicy(policyName) + ); + Phase nextPhase = policy.getPhases() + .values() + .stream() + .findFirst() + .orElseThrow(() -> new AssertionError("expected next phase to be present")); List policyMetadatas = Collections.singletonList( - new LifecyclePolicyMetadata(policy, Collections.emptyMap(), randomNonNegativeLong(), randomNonNegativeLong())); + new LifecyclePolicyMetadata(policy, Collections.emptyMap(), randomNonNegativeLong(), randomNonNegativeLong()) + ); Step.StepKey currentStepKey = new Step.StepKey("current_phase", "current_action", "current_step"); Step.StepKey nextStepKey = new Step.StepKey(nextPhase.getName(), "next_action", "next_step"); long now = randomNonNegativeLong(); @@ -217,8 +251,14 @@ public void testSuccessfulValidatedMoveClusterStateToNextStep() { Settings.Builder indexSettingsBuilder = Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, policyName); ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, lifecycleState.build(), policyMetadatas); Index index = clusterState.metadata().index(indexName).getIndex(); - ClusterState newClusterState = IndexLifecycleTransition.moveClusterStateToStep(index, clusterState, - nextStepKey, () -> now, stepRegistry, true); + ClusterState newClusterState = IndexLifecycleTransition.moveClusterStateToStep( + index, + clusterState, + nextStepKey, + () -> now, + stepRegistry, + true + ); assertClusterStateOnNextStep(clusterState, index, currentStepKey, nextStepKey, newClusterState, now); } @@ -239,8 +279,10 @@ public void testValidatedMoveClusterStateToNextStepWithoutPolicy() { ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, lifecycleState.build(), Collections.emptyList()); Index index = clusterState.metadata().index(indexName).getIndex(); - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, - () -> IndexLifecycleTransition.moveClusterStateToStep(index, clusterState, nextStepKey, () -> now, stepRegistry, true)); + IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> IndexLifecycleTransition.moveClusterStateToStep(index, clusterState, nextStepKey, () -> now, stepRegistry, true) + ); assertThat(exception.getMessage(), equalTo("index [my_index] is not associated with an Index Lifecycle Policy")); } @@ -261,11 +303,17 @@ public void testValidatedMoveClusterStateToNextStepInvalidNextStep() { ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, lifecycleState.build(), Collections.emptyList()); Index index = clusterState.metadata().index(indexName).getIndex(); - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, - () -> IndexLifecycleTransition.moveClusterStateToStep(index, clusterState, nextStepKey, () -> now, stepRegistry, true)); - assertThat(exception.getMessage(), - equalTo("step [{\"phase\":\"next_phase\",\"action\":\"next_action\",\"name\":\"next_step\"}] " + - "for index [my_index] with policy [my_policy] does not exist")); + IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> IndexLifecycleTransition.moveClusterStateToStep(index, clusterState, nextStepKey, () -> now, stepRegistry, true) + ); + assertThat( + exception.getMessage(), + equalTo( + "step [{\"phase\":\"next_phase\",\"action\":\"next_action\",\"name\":\"next_step\"}] " + + "for index [my_index] with policy [my_policy] does not exist" + ) + ); } public void testMoveClusterStateToErrorStep() throws IOException { @@ -282,16 +330,38 @@ public void testMoveClusterStateToErrorStep() throws IOException { ClusterState clusterState = buildClusterState(indexName, Settings.builder(), lifecycleState.build(), Collections.emptyList()); Index index = clusterState.metadata().index(indexName).getIndex(); - ClusterState newClusterState = IndexLifecycleTransition.moveClusterStateToErrorStep(index, clusterState, cause, - () -> now, (idxMeta, stepKey) -> new MockStep(stepKey, nextStepKey)); - assertClusterStateOnErrorStep(clusterState, index, currentStep, newClusterState, now, - "{\"type\":\"exception\",\"reason\":\"THIS IS AN EXPECTED CAUSE\""); + ClusterState newClusterState = IndexLifecycleTransition.moveClusterStateToErrorStep( + index, + clusterState, + cause, + () -> now, + (idxMeta, stepKey) -> new MockStep(stepKey, nextStepKey) + ); + assertClusterStateOnErrorStep( + clusterState, + index, + currentStep, + newClusterState, + now, + "{\"type\":\"exception\",\"reason\":\"THIS IS AN EXPECTED CAUSE\"" + ); cause = new IllegalArgumentException("non elasticsearch-exception"); - newClusterState = IndexLifecycleTransition.moveClusterStateToErrorStep(index, clusterState, cause, () -> now, - (idxMeta, stepKey) -> new MockStep(stepKey, nextStepKey)); - assertClusterStateOnErrorStep(clusterState, index, currentStep, newClusterState, now, - "{\"type\":\"illegal_argument_exception\",\"reason\":\"non elasticsearch-exception\",\"stack_trace\":\""); + newClusterState = IndexLifecycleTransition.moveClusterStateToErrorStep( + index, + clusterState, + cause, + () -> now, + (idxMeta, stepKey) -> new MockStep(stepKey, nextStepKey) + ); + assertClusterStateOnErrorStep( + clusterState, + index, + currentStep, + newClusterState, + now, + "{\"type\":\"illegal_argument_exception\",\"reason\":\"non elasticsearch-exception\",\"stack_trace\":\"" + ); } public void testAddStepInfoToClusterState() throws IOException { @@ -311,7 +381,6 @@ public void testAddStepInfoToClusterState() throws IOException { assertSame(newClusterState, runAgainClusterState); } - public void testRemovePolicyForIndex() { String indexName = randomAlphaOfLength(10); String oldPolicyName = "old_policy"; @@ -323,8 +392,9 @@ public void testRemovePolicyForIndex() { lifecycleState.setAction(currentStep.getAction()); lifecycleState.setStep(currentStep.getName()); List policyMetadatas = new ArrayList<>(); - policyMetadatas.add(new LifecyclePolicyMetadata(oldPolicy, Collections.emptyMap(), - randomNonNegativeLong(), randomNonNegativeLong())); + policyMetadatas.add( + new LifecyclePolicyMetadata(oldPolicy, Collections.emptyMap(), randomNonNegativeLong(), randomNonNegativeLong()) + ); ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, lifecycleState.build(), policyMetadatas); Index index = clusterState.metadata().index(indexName).getIndex(); Index[] indices = new Index[] { index }; @@ -339,8 +409,12 @@ public void testRemovePolicyForIndex() { public void testRemovePolicyForIndexNoCurrentPolicy() { String indexName = randomAlphaOfLength(10); Settings.Builder indexSettingsBuilder = Settings.builder(); - ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, LifecycleExecutionState.builder().build(), - Collections.emptyList()); + ClusterState clusterState = buildClusterState( + indexName, + indexSettingsBuilder, + LifecycleExecutionState.builder().build(), + Collections.emptyList() + ); Index index = clusterState.metadata().index(indexName).getIndex(); Index[] indices = new Index[] { index }; List failedIndexes = new ArrayList<>(); @@ -362,8 +436,9 @@ public void testRemovePolicyForIndexIndexDoesntExist() { lifecycleState.setAction(currentStep.getAction()); lifecycleState.setStep(currentStep.getName()); List policyMetadatas = new ArrayList<>(); - policyMetadatas.add(new LifecyclePolicyMetadata(oldPolicy, Collections.emptyMap(), - randomNonNegativeLong(), randomNonNegativeLong())); + policyMetadatas.add( + new LifecyclePolicyMetadata(oldPolicy, Collections.emptyMap(), randomNonNegativeLong(), randomNonNegativeLong()) + ); ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, lifecycleState.build(), policyMetadatas); Index index = new Index("doesnt_exist", "im_not_here"); Index[] indices = new Index[] { index }; @@ -387,8 +462,9 @@ public void testRemovePolicyForIndexIndexInUnsafe() { lifecycleState.setAction(currentStep.getAction()); lifecycleState.setStep(currentStep.getName()); List policyMetadatas = new ArrayList<>(); - policyMetadatas.add(new LifecyclePolicyMetadata(oldPolicy, Collections.emptyMap(), - randomNonNegativeLong(), randomNonNegativeLong())); + policyMetadatas.add( + new LifecyclePolicyMetadata(oldPolicy, Collections.emptyMap(), randomNonNegativeLong(), randomNonNegativeLong()) + ); ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, lifecycleState.build(), policyMetadatas); Index index = clusterState.metadata().index(indexName).getIndex(); Index[] indices = new Index[] { index }; @@ -413,8 +489,9 @@ public void testRemovePolicyWithIndexingComplete() { lifecycleState.setAction(currentStep.getAction()); lifecycleState.setStep(currentStep.getName()); List policyMetadatas = new ArrayList<>(); - policyMetadatas.add(new LifecyclePolicyMetadata(oldPolicy, Collections.emptyMap(), - randomNonNegativeLong(), randomNonNegativeLong())); + policyMetadatas.add( + new LifecyclePolicyMetadata(oldPolicy, Collections.emptyMap(), randomNonNegativeLong(), randomNonNegativeLong()) + ); ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, lifecycleState.build(), policyMetadatas); Index index = clusterState.metadata().index(indexName).getIndex(); Index[] indices = new Index[] { index }; @@ -427,7 +504,8 @@ public void testRemovePolicyWithIndexingComplete() { } public void testValidateTransitionThrowsExceptionForMissingIndexPolicy() { - IndexMetadata indexMetadata = IndexMetadata.builder("index").settings(settings(Version.CURRENT)) + IndexMetadata indexMetadata = IndexMetadata.builder("index") + .settings(settings(Version.CURRENT)) .numberOfShards(randomIntBetween(1, 5)) .numberOfReplicas(randomIntBetween(0, 5)) .build(); @@ -437,8 +515,10 @@ public void testValidateTransitionThrowsExceptionForMissingIndexPolicy() { Step currentStep = new MockStep(currentStepKey, nextStepKey); PolicyStepsRegistry policyRegistry = createOneStepPolicyStepRegistry("policy", currentStep); - expectThrows(IllegalArgumentException.class, - () -> IndexLifecycleTransition.validateTransition(indexMetadata, currentStepKey, nextStepKey, policyRegistry)); + expectThrows( + IllegalArgumentException.class, + () -> IndexLifecycleTransition.validateTransition(indexMetadata, currentStepKey, nextStepKey, policyRegistry) + ); } public void testValidateTransitionThrowsExceptionIfTheCurrentStepIsIncorrect() { @@ -454,8 +534,10 @@ public void testValidateTransitionThrowsExceptionIfTheCurrentStepIsIncorrect() { Step currentStep = new MockStep(currentStepKey, nextStepKey); PolicyStepsRegistry policyRegistry = createOneStepPolicyStepRegistry(policy, currentStep); - expectThrows(IllegalArgumentException.class, - () -> IndexLifecycleTransition.validateTransition(indexMetadata, currentStepKey, nextStepKey, policyRegistry)); + expectThrows( + IllegalArgumentException.class, + () -> IndexLifecycleTransition.validateTransition(indexMetadata, currentStepKey, nextStepKey, policyRegistry) + ); } public void testValidateTransitionThrowsExceptionIfNextStepDoesNotExist() { @@ -471,8 +553,10 @@ public void testValidateTransitionThrowsExceptionIfNextStepDoesNotExist() { Step currentStep = new MockStep(currentStepKey, nextStepKey); PolicyStepsRegistry policyRegistry = createOneStepPolicyStepRegistry(policy, currentStep); - expectThrows(IllegalArgumentException.class, - () -> IndexLifecycleTransition.validateTransition(indexMetadata, currentStepKey, nextStepKey, policyRegistry)); + expectThrows( + IllegalArgumentException.class, + () -> IndexLifecycleTransition.validateTransition(indexMetadata, currentStepKey, nextStepKey, policyRegistry) + ); } public void testValidateValidTransition() { @@ -501,22 +585,24 @@ public void testValidateTransitionToCachedStepMissingFromPolicy() { .setPhase("hot") .setAction("rollover") .setStep("check-rollover-ready") - .setPhaseDefinition("{\n" + - " \"policy\" : \"my-policy\",\n" + - " \"phase_definition\" : {\n" + - " \"min_age\" : \"20m\",\n" + - " \"actions\" : {\n" + - " \"rollover\" : {\n" + - " \"max_age\" : \"5s\"\n" + - " },\n" + - " \"set_priority\" : {\n" + - " \"priority\" : 150\n" + - " }\n" + - " }\n" + - " },\n" + - " \"version\" : 1,\n" + - " \"modified_date_in_millis\" : 1578521007076\n" + - " }"); + .setPhaseDefinition( + "{\n" + + " \"policy\" : \"my-policy\",\n" + + " \"phase_definition\" : {\n" + + " \"min_age\" : \"20m\",\n" + + " \"actions\" : {\n" + + " \"rollover\" : {\n" + + " \"max_age\" : \"5s\"\n" + + " },\n" + + " \"set_priority\" : {\n" + + " \"priority\" : 150\n" + + " }\n" + + " }\n" + + " },\n" + + " \"version\" : 1,\n" + + " \"modified_date_in_millis\" : 1578521007076\n" + + " }" + ); IndexMetadata meta = buildIndexMetadata("my-policy", executionState); @@ -528,17 +614,19 @@ public void testValidateTransitionToCachedStepMissingFromPolicy() { LifecyclePolicyMetadata policyMetadata = new LifecyclePolicyMetadata(policyWithoutRollover, Collections.emptyMap(), 2L, 2L); ClusterState existingState = ClusterState.builder(ClusterState.EMPTY_STATE) - .metadata(Metadata.builder(Metadata.EMPTY_METADATA) - .put(meta, false) - .build()) + .metadata(Metadata.builder(Metadata.EMPTY_METADATA).put(meta, false).build()) .build(); try (Client client = new NoOpClient(getTestName())) { Step.StepKey currentStepKey = new Step.StepKey("hot", RolloverAction.NAME, WaitForRolloverReadyStep.NAME); Step.StepKey nextStepKey = new Step.StepKey("hot", RolloverAction.NAME, RolloverStep.NAME); Step currentStep = new WaitForRolloverReadyStep(currentStepKey, nextStepKey, client, null, null, null, 1L); try { - IndexLifecycleTransition.validateTransition(meta, currentStepKey, nextStepKey, createOneStepPolicyStepRegistry("my-policy", - currentStep)); + IndexLifecycleTransition.validateTransition( + meta, + currentStepKey, + nextStepKey, + createOneStepPolicyStepRegistry("my-policy", currentStep) + ); } catch (Exception e) { logger.error(e.getMessage(), e); fail("validateTransition should not throw exception on valid transitions"); @@ -554,12 +642,15 @@ public void testMoveClusterStateToFailedStep() { Step.StepKey errorStepKey = new Step.StepKey(failedStepKey.getPhase(), failedStepKey.getAction(), ErrorStep.NAME); Step step = new MockStep(failedStepKey, null); LifecyclePolicy policy = createPolicy(policyName, failedStepKey, null); - LifecyclePolicyMetadata policyMetadata = new LifecyclePolicyMetadata(policy, Collections.emptyMap(), - randomNonNegativeLong(), randomNonNegativeLong()); + LifecyclePolicyMetadata policyMetadata = new LifecyclePolicyMetadata( + policy, + Collections.emptyMap(), + randomNonNegativeLong(), + randomNonNegativeLong() + ); PolicyStepsRegistry policyRegistry = createOneStepPolicyStepRegistry(policyName, step); - Settings.Builder indexSettingsBuilder = Settings.builder() - .put(LifecycleSettings.LIFECYCLE_NAME, policyName); + Settings.Builder indexSettingsBuilder = Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, policyName); LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); lifecycleState.setPhase(errorStepKey.getPhase()); lifecycleState.setPhaseTime(now); @@ -568,13 +659,21 @@ public void testMoveClusterStateToFailedStep() { lifecycleState.setStep(errorStepKey.getName()); lifecycleState.setStepTime(now); lifecycleState.setFailedStep(failedStepKey.getName()); - ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, lifecycleState.build(), - Collections.singletonList(policyMetadata)); + ClusterState clusterState = buildClusterState( + indexName, + indexSettingsBuilder, + lifecycleState.build(), + Collections.singletonList(policyMetadata) + ); Index index = clusterState.metadata().index(indexName).getIndex(); - ClusterState nextClusterState = IndexLifecycleTransition.moveClusterStateToPreviouslyFailedStep(clusterState, - indexName, () -> now, policyRegistry, false); - IndexLifecycleRunnerTests.assertClusterStateOnNextStep(clusterState, index, errorStepKey, failedStepKey, - nextClusterState, now); + ClusterState nextClusterState = IndexLifecycleTransition.moveClusterStateToPreviouslyFailedStep( + clusterState, + indexName, + () -> now, + policyRegistry, + false + ); + IndexLifecycleRunnerTests.assertClusterStateOnNextStep(clusterState, index, errorStepKey, failedStepKey, nextClusterState, now); LifecycleExecutionState executionState = LifecycleExecutionState.fromIndexMetadata(nextClusterState.metadata().index(indexName)); assertThat("manual move to failed step should not count as a retry", executionState.getFailedStepRetryCount(), is(nullValue())); } @@ -586,16 +685,18 @@ public void testMoveClusterStateToFailedStepWithUnknownStep() { Step.StepKey failedStepKey = new Step.StepKey("current_phase", MockAction.NAME, "current_step"); Step.StepKey errorStepKey = new Step.StepKey(failedStepKey.getPhase(), failedStepKey.getAction(), ErrorStep.NAME); - Step.StepKey registeredStepKey = new Step.StepKey(randomFrom(failedStepKey.getPhase(), "other"), - MockAction.NAME, "different_step"); + Step.StepKey registeredStepKey = new Step.StepKey(randomFrom(failedStepKey.getPhase(), "other"), MockAction.NAME, "different_step"); Step step = new MockStep(registeredStepKey, null); LifecyclePolicy policy = createPolicy(policyName, failedStepKey, null); - LifecyclePolicyMetadata policyMetadata = new LifecyclePolicyMetadata(policy, Collections.emptyMap(), - randomNonNegativeLong(), randomNonNegativeLong()); + LifecyclePolicyMetadata policyMetadata = new LifecyclePolicyMetadata( + policy, + Collections.emptyMap(), + randomNonNegativeLong(), + randomNonNegativeLong() + ); PolicyStepsRegistry policyRegistry = createOneStepPolicyStepRegistry(policyName, step); - Settings.Builder indexSettingsBuilder = Settings.builder() - .put(LifecycleSettings.LIFECYCLE_NAME, policyName); + Settings.Builder indexSettingsBuilder = Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, policyName); LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); lifecycleState.setPhase(errorStepKey.getPhase()); lifecycleState.setPhaseTime(now); @@ -604,23 +705,35 @@ public void testMoveClusterStateToFailedStepWithUnknownStep() { lifecycleState.setStep(errorStepKey.getName()); lifecycleState.setStepTime(now); lifecycleState.setFailedStep(failedStepKey.getName()); - ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, lifecycleState.build(), - Collections.singletonList(policyMetadata)); - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, - () -> IndexLifecycleTransition.moveClusterStateToPreviouslyFailedStep(clusterState, - indexName, () -> now, policyRegistry, false)); - assertThat(exception.getMessage(), equalTo("step [" + failedStepKey - + "] for index [my_index] with policy [my_policy] does not exist")); + ClusterState clusterState = buildClusterState( + indexName, + indexSettingsBuilder, + lifecycleState.build(), + Collections.singletonList(policyMetadata) + ); + IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> IndexLifecycleTransition.moveClusterStateToPreviouslyFailedStep(clusterState, indexName, () -> now, policyRegistry, false) + ); + assertThat( + exception.getMessage(), + equalTo("step [" + failedStepKey + "] for index [my_index] with policy [my_policy] does not exist") + ); } public void testMoveClusterStateToFailedStepIndexNotFound() { String existingIndexName = "my_index"; String invalidIndexName = "does_not_exist"; - ClusterState clusterState = buildClusterState(existingIndexName, Settings.builder(), LifecycleExecutionState.builder().build(), - Collections.emptyList()); - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, - () -> IndexLifecycleTransition.moveClusterStateToPreviouslyFailedStep(clusterState, - invalidIndexName, () -> 0L, null, false)); + ClusterState clusterState = buildClusterState( + existingIndexName, + Settings.builder(), + LifecycleExecutionState.builder().build(), + Collections.emptyList() + ); + IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> IndexLifecycleTransition.moveClusterStateToPreviouslyFailedStep(clusterState, invalidIndexName, () -> 0L, null, false) + ); assertThat(exception.getMessage(), equalTo("index [" + invalidIndexName + "] does not exist")); } @@ -632,17 +745,17 @@ public void testMoveClusterStateToFailedStepInvalidPolicySetting() { Step.StepKey errorStepKey = new Step.StepKey(failedStepKey.getPhase(), failedStepKey.getAction(), ErrorStep.NAME); Step step = new MockStep(failedStepKey, null); PolicyStepsRegistry policyRegistry = createOneStepPolicyStepRegistry(policyName, step); - Settings.Builder indexSettingsBuilder = Settings.builder() - .put(LifecycleSettings.LIFECYCLE_NAME, (String) null); + Settings.Builder indexSettingsBuilder = Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, (String) null); LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); lifecycleState.setPhase(errorStepKey.getPhase()); lifecycleState.setAction(errorStepKey.getAction()); lifecycleState.setStep(errorStepKey.getName()); lifecycleState.setFailedStep(failedStepKey.getName()); ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, lifecycleState.build(), Collections.emptyList()); - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, - () -> IndexLifecycleTransition.moveClusterStateToPreviouslyFailedStep(clusterState, - indexName, () -> now, policyRegistry, false)); + IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> IndexLifecycleTransition.moveClusterStateToPreviouslyFailedStep(clusterState, indexName, () -> now, policyRegistry, false) + ); assertThat(exception.getMessage(), equalTo("index [" + indexName + "] is not associated with an Index Lifecycle Policy")); } @@ -653,18 +766,22 @@ public void testMoveClusterStateToFailedNotOnError() { Step.StepKey failedStepKey = new Step.StepKey("current_phase", "current_action", "current_step"); Step step = new MockStep(failedStepKey, null); PolicyStepsRegistry policyRegistry = createOneStepPolicyStepRegistry(policyName, step); - Settings.Builder indexSettingsBuilder = Settings.builder() - .put(LifecycleSettings.LIFECYCLE_NAME, (String) null); + Settings.Builder indexSettingsBuilder = Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, (String) null); LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); lifecycleState.setPhase(failedStepKey.getPhase()); lifecycleState.setAction(failedStepKey.getAction()); lifecycleState.setStep(failedStepKey.getName()); ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, lifecycleState.build(), Collections.emptyList()); - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, - () -> IndexLifecycleTransition.moveClusterStateToPreviouslyFailedStep(clusterState, - indexName, () -> now, policyRegistry, false)); - assertThat(exception.getMessage(), equalTo("cannot retry an action for an index [" + indexName - + "] that has not encountered an error when running a Lifecycle Policy")); + IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> IndexLifecycleTransition.moveClusterStateToPreviouslyFailedStep(clusterState, indexName, () -> now, policyRegistry, false) + ); + assertThat( + exception.getMessage(), + equalTo( + "cannot retry an action for an index [" + indexName + "] that has not encountered an error when running a Lifecycle Policy" + ) + ); } public void testMoveClusterStateToPreviouslyFailedStepAsAutomaticRetry() { @@ -675,12 +792,15 @@ public void testMoveClusterStateToPreviouslyFailedStepAsAutomaticRetry() { Step.StepKey errorStepKey = new Step.StepKey(failedStepKey.getPhase(), failedStepKey.getAction(), ErrorStep.NAME); Step retryableStep = new IndexLifecycleRunnerTests.RetryableMockStep(failedStepKey, null); LifecyclePolicy policy = createPolicy(policyName, failedStepKey, null); - LifecyclePolicyMetadata policyMetadata = new LifecyclePolicyMetadata(policy, Collections.emptyMap(), - randomNonNegativeLong(), randomNonNegativeLong()); + LifecyclePolicyMetadata policyMetadata = new LifecyclePolicyMetadata( + policy, + Collections.emptyMap(), + randomNonNegativeLong(), + randomNonNegativeLong() + ); PolicyStepsRegistry policyRegistry = createOneStepPolicyStepRegistry(policyName, retryableStep); - Settings.Builder indexSettingsBuilder = Settings.builder() - .put(LifecycleSettings.LIFECYCLE_NAME, policyName); + Settings.Builder indexSettingsBuilder = Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, policyName); LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); lifecycleState.setPhase(errorStepKey.getPhase()); lifecycleState.setPhaseTime(now); @@ -689,13 +809,21 @@ public void testMoveClusterStateToPreviouslyFailedStepAsAutomaticRetry() { lifecycleState.setStep(errorStepKey.getName()); lifecycleState.setStepTime(now); lifecycleState.setFailedStep(failedStepKey.getName()); - ClusterState clusterState = buildClusterState(indexName, indexSettingsBuilder, lifecycleState.build(), - Collections.singletonList(policyMetadata)); + ClusterState clusterState = buildClusterState( + indexName, + indexSettingsBuilder, + lifecycleState.build(), + Collections.singletonList(policyMetadata) + ); Index index = clusterState.metadata().index(indexName).getIndex(); - ClusterState nextClusterState = IndexLifecycleTransition.moveClusterStateToPreviouslyFailedStep(clusterState, - indexName, () -> now, policyRegistry, true); - IndexLifecycleRunnerTests.assertClusterStateOnNextStep(clusterState, index, errorStepKey, failedStepKey, - nextClusterState, now); + ClusterState nextClusterState = IndexLifecycleTransition.moveClusterStateToPreviouslyFailedStep( + clusterState, + indexName, + () -> now, + policyRegistry, + true + ); + IndexLifecycleRunnerTests.assertClusterStateOnNextStep(clusterState, index, errorStepKey, failedStepKey, nextClusterState, now); LifecycleExecutionState executionState = LifecycleExecutionState.fromIndexMetadata(nextClusterState.metadata().index(indexName)); assertThat(executionState.getFailedStepRetryCount(), is(1)); } @@ -705,22 +833,24 @@ public void testRefreshPhaseJson() { .setPhase("hot") .setAction("rollover") .setStep("check-rollover-ready") - .setPhaseDefinition("{\n" + - " \"policy\" : \"my-policy\",\n" + - " \"phase_definition\" : {\n" + - " \"min_age\" : \"20m\",\n" + - " \"actions\" : {\n" + - " \"rollover\" : {\n" + - " \"max_age\" : \"5s\"\n" + - " },\n" + - " \"set_priority\" : {\n" + - " \"priority\" : 150\n" + - " }\n" + - " }\n" + - " },\n" + - " \"version\" : 1,\n" + - " \"modified_date_in_millis\" : 1578521007076\n" + - " }"); + .setPhaseDefinition( + "{\n" + + " \"policy\" : \"my-policy\",\n" + + " \"phase_definition\" : {\n" + + " \"min_age\" : \"20m\",\n" + + " \"actions\" : {\n" + + " \"rollover\" : {\n" + + " \"max_age\" : \"5s\"\n" + + " },\n" + + " \"set_priority\" : {\n" + + " \"priority\" : 150\n" + + " }\n" + + " }\n" + + " },\n" + + " \"version\" : 1,\n" + + " \"modified_date_in_millis\" : 1578521007076\n" + + " }" + ); IndexMetadata meta = buildIndexMetadata("my-policy", exState); String index = meta.getIndex().getName(); @@ -734,9 +864,7 @@ public void testRefreshPhaseJson() { LifecyclePolicyMetadata policyMetadata = new LifecyclePolicyMetadata(newPolicy, Collections.emptyMap(), 2L, 2L); ClusterState existingState = ClusterState.builder(ClusterState.EMPTY_STATE) - .metadata(Metadata.builder(Metadata.EMPTY_METADATA) - .put(meta, false) - .build()) + .metadata(Metadata.builder(Metadata.EMPTY_METADATA).put(meta, false).build()) .build(); ClusterState changedState = refreshPhaseDefinition(existingState, index, policyMetadata); @@ -751,56 +879,62 @@ public void testRefreshPhaseJson() { assertThat(beforeState, equalTo(afterState)); // Check that the phase definition has been refreshed - assertThat(afterExState.getPhaseDefinition(), - equalTo("{\"policy\":\"my-policy\",\"phase_definition\":{\"min_age\":\"0ms\",\"actions\":{\"rollover\":{\"max_docs\":1}," + - "\"set_priority\":{\"priority\":100}}},\"version\":2,\"modified_date_in_millis\":2}")); + assertThat( + afterExState.getPhaseDefinition(), + equalTo( + "{\"policy\":\"my-policy\",\"phase_definition\":{\"min_age\":\"0ms\",\"actions\":{\"rollover\":{\"max_docs\":1}," + + "\"set_priority\":{\"priority\":100}}},\"version\":2,\"modified_date_in_millis\":2}" + ) + ); } public void testEligibleForRefresh() { IndexMetadata meta = IndexMetadata.builder("index") - .settings(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 10)) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, randomIntBetween(0, 5)) - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(IndexMetadata.SETTING_INDEX_UUID, randomAlphaOfLength(5))) + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 10)) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, randomIntBetween(0, 5)) + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetadata.SETTING_INDEX_UUID, randomAlphaOfLength(5)) + ) .build(); assertFalse(eligibleToCheckForRefresh(meta)); LifecycleExecutionState state = LifecycleExecutionState.builder().build(); meta = IndexMetadata.builder("index") - .settings(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 10)) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, randomIntBetween(0, 5)) - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(IndexMetadata.SETTING_INDEX_UUID, randomAlphaOfLength(5))) + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 10)) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, randomIntBetween(0, 5)) + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetadata.SETTING_INDEX_UUID, randomAlphaOfLength(5)) + ) .putCustom(ILM_CUSTOM_METADATA_KEY, state.asMap()) .build(); assertFalse(eligibleToCheckForRefresh(meta)); - state = LifecycleExecutionState.builder() - .setPhase("phase") - .setAction("action") - .setStep("step") - .build(); + state = LifecycleExecutionState.builder().setPhase("phase").setAction("action").setStep("step").build(); meta = IndexMetadata.builder("index") - .settings(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 10)) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, randomIntBetween(0, 5)) - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(IndexMetadata.SETTING_INDEX_UUID, randomAlphaOfLength(5))) + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 10)) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, randomIntBetween(0, 5)) + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetadata.SETTING_INDEX_UUID, randomAlphaOfLength(5)) + ) .putCustom(ILM_CUSTOM_METADATA_KEY, state.asMap()) .build(); assertFalse(eligibleToCheckForRefresh(meta)); - state = LifecycleExecutionState.builder() - .setPhaseDefinition("{}") - .build(); + state = LifecycleExecutionState.builder().setPhaseDefinition("{}").build(); meta = IndexMetadata.builder("index") - .settings(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 10)) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, randomIntBetween(0, 5)) - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(IndexMetadata.SETTING_INDEX_UUID, randomAlphaOfLength(5))) + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 10)) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, randomIntBetween(0, 5)) + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetadata.SETTING_INDEX_UUID, randomAlphaOfLength(5)) + ) .putCustom(ILM_CUSTOM_METADATA_KEY, state.asMap()) .build(); assertFalse(eligibleToCheckForRefresh(meta)); @@ -812,27 +946,26 @@ public void testEligibleForRefresh() { .setPhaseDefinition("{}") .build(); meta = IndexMetadata.builder("index") - .settings(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 10)) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, randomIntBetween(0, 5)) - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(IndexMetadata.SETTING_INDEX_UUID, randomAlphaOfLength(5))) + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 10)) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, randomIntBetween(0, 5)) + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetadata.SETTING_INDEX_UUID, randomAlphaOfLength(5)) + ) .putCustom(ILM_CUSTOM_METADATA_KEY, state.asMap()) .build(); assertFalse(eligibleToCheckForRefresh(meta)); - state = LifecycleExecutionState.builder() - .setPhase("phase") - .setAction("action") - .setStep("step") - .setPhaseDefinition("{}") - .build(); + state = LifecycleExecutionState.builder().setPhase("phase").setAction("action").setStep("step").setPhaseDefinition("{}").build(); meta = IndexMetadata.builder("index") - .settings(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 10)) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, randomIntBetween(0, 5)) - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(IndexMetadata.SETTING_INDEX_UUID, randomAlphaOfLength(5))) + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 10)) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, randomIntBetween(0, 5)) + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetadata.SETTING_INDEX_UUID, randomAlphaOfLength(5)) + ) .putCustom(ILM_CUSTOM_METADATA_KEY, state.asMap()) .build(); assertTrue(eligibleToCheckForRefresh(meta)); @@ -843,22 +976,24 @@ public void testMoveStateToNextActionAndUpdateCachedPhase() { .setPhase("hot") .setAction("rollover") .setStep("check-rollover-ready") - .setPhaseDefinition("{\n" + - " \"policy\" : \"my-policy\",\n" + - " \"phase_definition\" : {\n" + - " \"min_age\" : \"20m\",\n" + - " \"actions\" : {\n" + - " \"rollover\" : {\n" + - " \"max_age\" : \"5s\"\n" + - " },\n" + - " \"set_priority\" : {\n" + - " \"priority\" : 150\n" + - " }\n" + - " }\n" + - " },\n" + - " \"version\" : 1,\n" + - " \"modified_date_in_millis\" : 1578521007076\n" + - " }"); + .setPhaseDefinition( + "{\n" + + " \"policy\" : \"my-policy\",\n" + + " \"phase_definition\" : {\n" + + " \"min_age\" : \"20m\",\n" + + " \"actions\" : {\n" + + " \"rollover\" : {\n" + + " \"max_age\" : \"5s\"\n" + + " },\n" + + " \"set_priority\" : {\n" + + " \"priority\" : 150\n" + + " }\n" + + " }\n" + + " },\n" + + " \"version\" : 1,\n" + + " \"modified_date_in_millis\" : 1578521007076\n" + + " }" + ); IndexMetadata meta = buildIndexMetadata("my-policy", currentExecutionState); @@ -877,19 +1012,32 @@ public void testMoveStateToNextActionAndUpdateCachedPhase() { actionsWithoutRollover.put("set_priority", new SetPriorityAction(100)); Phase hotPhaseNoRollover = new Phase("hot", TimeValue.ZERO, actionsWithoutRollover); Map phasesNoRollover = Collections.singletonMap("hot", hotPhaseNoRollover); - LifecyclePolicyMetadata updatedPolicyMetadata = new LifecyclePolicyMetadata(new LifecyclePolicy("my-policy", - phasesNoRollover), Collections.emptyMap(), 2L, 2L); + LifecyclePolicyMetadata updatedPolicyMetadata = new LifecyclePolicyMetadata( + new LifecyclePolicy("my-policy", phasesNoRollover), + Collections.emptyMap(), + 2L, + 2L + ); try (Client client = new NoOpClient(getTestName())) { - LifecycleExecutionState newState = moveStateToNextActionAndUpdateCachedPhase(meta, - LifecycleExecutionState.fromIndexMetadata(meta), System::currentTimeMillis, currentPolicy, updatedPolicyMetadata, - client, null); + LifecycleExecutionState newState = moveStateToNextActionAndUpdateCachedPhase( + meta, + LifecycleExecutionState.fromIndexMetadata(meta), + System::currentTimeMillis, + currentPolicy, + updatedPolicyMetadata, + client, + null + ); Step.StepKey hotPhaseCompleteStepKey = PhaseCompleteStep.finalStep("hot").getKey(); assertThat(newState.getAction(), is(hotPhaseCompleteStepKey.getAction())); assertThat(newState.getStep(), is(hotPhaseCompleteStepKey.getName())); - assertThat("the cached phase should not contain rollover anymore", newState.getPhaseDefinition(), - not(containsString(RolloverAction.NAME))); + assertThat( + "the cached phase should not contain rollover anymore", + newState.getPhaseDefinition(), + not(containsString(RolloverAction.NAME)) + ); } } @@ -901,13 +1049,23 @@ public void testMoveStateToNextActionAndUpdateCachedPhase() { actionsWitoutSetPriority.put("rollover", new RolloverAction(null, null, null, 1L)); Phase hotPhaseNoSetPriority = new Phase("hot", TimeValue.ZERO, actionsWitoutSetPriority); Map phasesWithoutSetPriority = Collections.singletonMap("hot", hotPhaseNoSetPriority); - LifecyclePolicyMetadata updatedPolicyMetadata = new LifecyclePolicyMetadata(new LifecyclePolicy("my-policy", - phasesWithoutSetPriority), Collections.emptyMap(), 2L, 2L); + LifecyclePolicyMetadata updatedPolicyMetadata = new LifecyclePolicyMetadata( + new LifecyclePolicy("my-policy", phasesWithoutSetPriority), + Collections.emptyMap(), + 2L, + 2L + ); try (Client client = new NoOpClient(getTestName())) { - LifecycleExecutionState newState = moveStateToNextActionAndUpdateCachedPhase(meta, - LifecycleExecutionState.fromIndexMetadata(meta), System::currentTimeMillis, currentPolicy, updatedPolicyMetadata, - client, null); + LifecycleExecutionState newState = moveStateToNextActionAndUpdateCachedPhase( + meta, + LifecycleExecutionState.fromIndexMetadata(meta), + System::currentTimeMillis, + currentPolicy, + updatedPolicyMetadata, + client, + null + ); Step.StepKey hotPhaseCompleteStepKey = PhaseCompleteStep.finalStep("hot").getKey(); // the state was still moved into the next action, even if the updated policy still contained the action the index was @@ -915,8 +1073,11 @@ public void testMoveStateToNextActionAndUpdateCachedPhase() { assertThat(newState.getAction(), is(hotPhaseCompleteStepKey.getAction())); assertThat(newState.getStep(), is(hotPhaseCompleteStepKey.getName())); assertThat(newState.getPhaseDefinition(), containsString(RolloverAction.NAME)); - assertThat("the cached phase should not contain set_priority anymore", newState.getPhaseDefinition(), - not(containsString(SetPriorityAction.NAME))); + assertThat( + "the cached phase should not contain set_priority anymore", + newState.getPhaseDefinition(), + not(containsString(SetPriorityAction.NAME)) + ); } } } @@ -925,8 +1086,8 @@ private static LifecyclePolicy createPolicy(String policyName, Step.StepKey safe Map phases = new HashMap<>(); if (safeStep != null) { assert MockAction.NAME.equals(safeStep.getAction()) : "The safe action needs to be MockAction.NAME"; - assert unsafeStep == null - || safeStep.getPhase().equals(unsafeStep.getPhase()) == false : "safe and unsafe actions must be in different phases"; + assert unsafeStep == null || safeStep.getPhase().equals(unsafeStep.getPhase()) == false + : "safe and unsafe actions must be in different phases"; Map actions = new HashMap<>(); List steps = Collections.singletonList(new MockStep(safeStep, null)); MockAction safeAction = new MockAction(steps, true); @@ -946,11 +1107,16 @@ private static LifecyclePolicy createPolicy(String policyName, Step.StepKey safe return newTestLifecyclePolicy(policyName, phases); } - private ClusterState buildClusterState(String indexName, Settings.Builder indexSettingsBuilder, - LifecycleExecutionState lifecycleState, - List lifecyclePolicyMetadatas) { + private ClusterState buildClusterState( + String indexName, + Settings.Builder indexSettingsBuilder, + LifecycleExecutionState lifecycleState, + List lifecyclePolicyMetadatas + ) { Settings indexSettings = indexSettingsBuilder.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).build(); + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .build(); IndexMetadata indexMetadata = IndexMetadata.builder(indexName) .settings(indexSettings) .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.asMap()) @@ -960,7 +1126,9 @@ private ClusterState buildClusterState(String indexName, Settings.Builder indexS .collect(Collectors.toMap(LifecyclePolicyMetadata::getName, Function.identity())); IndexLifecycleMetadata indexLifecycleMetadata = new IndexLifecycleMetadata(lifecyclePolicyMetadatasMap, OperationMode.RUNNING); - Metadata metadata = Metadata.builder().put(indexMetadata, true).putCustom(IndexLifecycleMetadata.TYPE, indexLifecycleMetadata) + Metadata metadata = Metadata.builder() + .put(indexMetadata, true) + .putCustom(IndexLifecycleMetadata.TYPE, indexLifecycleMetadata) .build(); return ClusterState.builder(new ClusterName("my_cluster")).metadata(metadata).build(); } @@ -977,30 +1145,40 @@ public static void assertIndexNotManagedByILM(ClusterState clusterState, Index i assertFalse(LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE_SETTING.exists(indexSettings)); } - public static void assertClusterStateOnNextStep(ClusterState oldClusterState, Index index, Step.StepKey currentStep, - Step.StepKey nextStep, ClusterState newClusterState, long now) { + public static void assertClusterStateOnNextStep( + ClusterState oldClusterState, + Index index, + Step.StepKey currentStep, + Step.StepKey nextStep, + ClusterState newClusterState, + long now + ) { assertNotSame(oldClusterState, newClusterState); Metadata newMetadata = newClusterState.metadata(); assertNotSame(oldClusterState.metadata(), newMetadata); IndexMetadata newIndexMetadata = newMetadata.getIndexSafe(index); assertNotSame(oldClusterState.metadata().index(index), newIndexMetadata); - LifecycleExecutionState newLifecycleState = LifecycleExecutionState - .fromIndexMetadata(newClusterState.metadata().index(index)); - LifecycleExecutionState oldLifecycleState = LifecycleExecutionState - .fromIndexMetadata(oldClusterState.metadata().index(index)); + LifecycleExecutionState newLifecycleState = LifecycleExecutionState.fromIndexMetadata(newClusterState.metadata().index(index)); + LifecycleExecutionState oldLifecycleState = LifecycleExecutionState.fromIndexMetadata(oldClusterState.metadata().index(index)); assertNotSame(oldLifecycleState, newLifecycleState); assertEquals(nextStep.getPhase(), newLifecycleState.getPhase()); assertEquals(nextStep.getAction(), newLifecycleState.getAction()); assertEquals(nextStep.getName(), newLifecycleState.getStep()); if (currentStep.getPhase().equals(nextStep.getPhase())) { - assertEquals("expected phase times to be the same but they were different", - oldLifecycleState.getPhaseTime(), newLifecycleState.getPhaseTime()); + assertEquals( + "expected phase times to be the same but they were different", + oldLifecycleState.getPhaseTime(), + newLifecycleState.getPhaseTime() + ); } else { assertEquals(now, newLifecycleState.getPhaseTime().longValue()); } if (currentStep.getAction().equals(nextStep.getAction())) { - assertEquals("expected action times to be the same but they were different", - oldLifecycleState.getActionTime(), newLifecycleState.getActionTime()); + assertEquals( + "expected action times to be the same but they were different", + oldLifecycleState.getActionTime(), + newLifecycleState.getActionTime() + ); } else { assertEquals(now, newLifecycleState.getActionTime().longValue()); } @@ -1018,17 +1196,21 @@ private IndexMetadata buildIndexMetadata(String policy, LifecycleExecutionState. .build(); } - private void assertClusterStateOnErrorStep(ClusterState oldClusterState, Index index, Step.StepKey currentStep, - ClusterState newClusterState, long now, String expectedCauseValue) { + private void assertClusterStateOnErrorStep( + ClusterState oldClusterState, + Index index, + Step.StepKey currentStep, + ClusterState newClusterState, + long now, + String expectedCauseValue + ) { assertNotSame(oldClusterState, newClusterState); Metadata newMetadata = newClusterState.metadata(); assertNotSame(oldClusterState.metadata(), newMetadata); IndexMetadata newIndexMetadata = newMetadata.getIndexSafe(index); assertNotSame(oldClusterState.metadata().index(index), newIndexMetadata); - LifecycleExecutionState newLifecycleState = LifecycleExecutionState - .fromIndexMetadata(newClusterState.metadata().index(index)); - LifecycleExecutionState oldLifecycleState = LifecycleExecutionState - .fromIndexMetadata(oldClusterState.metadata().index(index)); + LifecycleExecutionState newLifecycleState = LifecycleExecutionState.fromIndexMetadata(newClusterState.metadata().index(index)); + LifecycleExecutionState oldLifecycleState = LifecycleExecutionState.fromIndexMetadata(oldClusterState.metadata().index(index)); assertNotSame(oldLifecycleState, newLifecycleState); assertEquals(currentStep.getPhase(), newLifecycleState.getPhase()); assertEquals(currentStep.getAction(), newLifecycleState.getAction()); @@ -1040,8 +1222,13 @@ private void assertClusterStateOnErrorStep(ClusterState oldClusterState, Index i assertEquals(now, newLifecycleState.getStepTime().longValue()); } - private void assertClusterStateStepInfo(ClusterState oldClusterState, Index index, Step.StepKey currentStep, - ClusterState newClusterState, ToXContentObject stepInfo) throws IOException { + private void assertClusterStateStepInfo( + ClusterState oldClusterState, + Index index, + Step.StepKey currentStep, + ClusterState newClusterState, + ToXContentObject stepInfo + ) throws IOException { XContentBuilder stepInfoXContentBuilder = JsonXContent.contentBuilder(); stepInfo.toXContent(stepInfoXContentBuilder, ToXContent.EMPTY_PARAMS); String expectedstepInfoValue = BytesReference.bytes(stepInfoXContentBuilder).utf8ToString(); @@ -1050,10 +1237,8 @@ private void assertClusterStateStepInfo(ClusterState oldClusterState, Index inde assertNotSame(oldClusterState.metadata(), newMetadata); IndexMetadata newIndexMetadata = newMetadata.getIndexSafe(index); assertNotSame(oldClusterState.metadata().index(index), newIndexMetadata); - LifecycleExecutionState newLifecycleState = LifecycleExecutionState - .fromIndexMetadata(newClusterState.metadata().index(index)); - LifecycleExecutionState oldLifecycleState = LifecycleExecutionState - .fromIndexMetadata(oldClusterState.metadata().index(index)); + LifecycleExecutionState newLifecycleState = LifecycleExecutionState.fromIndexMetadata(newClusterState.metadata().index(index)); + LifecycleExecutionState oldLifecycleState = LifecycleExecutionState.fromIndexMetadata(oldClusterState.metadata().index(index)); assertNotSame(oldLifecycleState, newLifecycleState); assertEquals(currentStep.getPhase(), newLifecycleState.getPhase()); assertEquals(currentStep.getAction(), newLifecycleState.getAction()); diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/LifecyclePolicyClientTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/LifecyclePolicyClientTests.java index 2d6d69a87c584..792daf0ff32f5 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/LifecyclePolicyClientTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/LifecyclePolicyClientTests.java @@ -55,8 +55,13 @@ public void testExecuteWithHeadersAsyncNoHeaders() throws InterruptedException { SearchRequest request = new SearchRequest("foo"); - try (LifecyclePolicySecurityClient policyClient = new LifecyclePolicySecurityClient(client, ClientHelper.INDEX_LIFECYCLE_ORIGIN, - Collections.emptyMap())) { + try ( + LifecyclePolicySecurityClient policyClient = new LifecyclePolicySecurityClient( + client, + ClientHelper.INDEX_LIFECYCLE_ORIGIN, + Collections.emptyMap() + ) + ) { policyClient.execute(SearchAction.INSTANCE, request, listener); } @@ -89,8 +94,13 @@ public void testExecuteWithHeadersAsyncWrongHeaders() throws InterruptedExceptio headers.put("foo", "foo"); headers.put("bar", "bar"); - try (LifecyclePolicySecurityClient policyClient = new LifecyclePolicySecurityClient(client, ClientHelper.INDEX_LIFECYCLE_ORIGIN, - headers)) { + try ( + LifecyclePolicySecurityClient policyClient = new LifecyclePolicySecurityClient( + client, + ClientHelper.INDEX_LIFECYCLE_ORIGIN, + headers + ) + ) { policyClient.execute(SearchAction.INSTANCE, request, listener); } @@ -125,8 +135,13 @@ public void testExecuteWithHeadersAsyncWithHeaders() throws Exception { headers.put("es-security-runas-user", "foo"); headers.put("_xpack_security_authentication", "bar"); - try (LifecyclePolicySecurityClient policyClient = new LifecyclePolicySecurityClient(client, ClientHelper.INDEX_LIFECYCLE_ORIGIN, - headers)) { + try ( + LifecyclePolicySecurityClient policyClient = new LifecyclePolicySecurityClient( + client, + ClientHelper.INDEX_LIFECYCLE_ORIGIN, + headers + ) + ) { policyClient.execute(SearchAction.INSTANCE, request, listener); } diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/LockableLifecycleType.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/LockableLifecycleType.java index c7dd5d934378c..b515dcf692b16 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/LockableLifecycleType.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/LockableLifecycleType.java @@ -51,8 +51,7 @@ public String getNextActionName(String currentActionName, Phase phase) { } @Override - public void validate(Collection phases) { - } + public void validate(Collection phases) {} @Override public String getWriteableName() { @@ -60,6 +59,5 @@ public String getWriteableName() { } @Override - public void writeTo(StreamOutput out) { - } + public void writeTo(StreamOutput out) {} } diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/MoveToErrorStepUpdateTaskTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/MoveToErrorStepUpdateTaskTests.java index 858c57cefbe70..a0c670a2123e3 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/MoveToErrorStepUpdateTaskTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/MoveToErrorStepUpdateTaskTests.java @@ -14,11 +14,11 @@ import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.index.Index; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.index.Index; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.ilm.ErrorStep; import org.elasticsearch.xpack.core.ilm.IndexLifecycleMetadata; import org.elasticsearch.xpack.core.ilm.LifecycleExecutionState; @@ -51,14 +51,18 @@ public void setupClusterState() { policy = randomAlphaOfLength(10); LifecyclePolicy lifecyclePolicy = LifecyclePolicyTests.randomTestLifecyclePolicy(policy); IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(5)) - .settings(settings(Version.CURRENT) - .put(LifecycleSettings.LIFECYCLE_NAME, policy)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policy)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); index = indexMetadata.getIndex(); IndexLifecycleMetadata ilmMeta = new IndexLifecycleMetadata( - Collections.singletonMap(policy, new LifecyclePolicyMetadata(lifecyclePolicy, Collections.emptyMap(), - randomNonNegativeLong(), randomNonNegativeLong())), - OperationMode.RUNNING); + Collections.singletonMap( + policy, + new LifecyclePolicyMetadata(lifecyclePolicy, Collections.emptyMap(), randomNonNegativeLong(), randomNonNegativeLong()) + ), + OperationMode.RUNNING + ); Metadata metadata = Metadata.builder() .persistentSettings(settings(Version.CURRENT).build()) .put(IndexMetadata.builder(indexMetadata)) @@ -75,8 +79,15 @@ public void testExecuteSuccessfullyMoved() throws IOException { setStateToKey(currentStepKey); - MoveToErrorStepUpdateTask task = new MoveToErrorStepUpdateTask(index, policy, currentStepKey, cause, () -> now, - (idxMeta, stepKey) -> new MockStep(stepKey, nextStepKey), state -> {}); + MoveToErrorStepUpdateTask task = new MoveToErrorStepUpdateTask( + index, + policy, + currentStepKey, + cause, + () -> now, + (idxMeta, stepKey) -> new MockStep(stepKey, nextStepKey), + state -> {} + ); ClusterState newState = task.execute(clusterState); LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(newState.getMetadata().index(index)); StepKey actualKey = LifecycleExecutionState.getCurrentStepKey(lifecycleState); @@ -91,8 +102,10 @@ public void testExecuteSuccessfullyMoved() throws IOException { ElasticsearchException.generateThrowableXContent(causeXContentBuilder, ToXContent.EMPTY_PARAMS, cause); causeXContentBuilder.endObject(); String expectedCauseValue = BytesReference.bytes(causeXContentBuilder).utf8ToString(); - assertThat(lifecycleState.getStepInfo(), - containsString("{\"type\":\"exception\",\"reason\":\"THIS IS AN EXPECTED CAUSE\",\"stack_trace\":\"")); + assertThat( + lifecycleState.getStepInfo(), + containsString("{\"type\":\"exception\",\"reason\":\"THIS IS AN EXPECTED CAUSE\",\"stack_trace\":\"") + ); } public void testExecuteNoopDifferentStep() throws IOException { @@ -101,8 +114,15 @@ public void testExecuteNoopDifferentStep() throws IOException { long now = randomNonNegativeLong(); Exception cause = new ElasticsearchException("THIS IS AN EXPECTED CAUSE"); setStateToKey(notCurrentStepKey); - MoveToErrorStepUpdateTask task = new MoveToErrorStepUpdateTask(index, policy, currentStepKey, cause, () -> now, - (idxMeta, stepKey) -> new MockStep(stepKey, new StepKey("next-phase", "action", "step")), state -> {}); + MoveToErrorStepUpdateTask task = new MoveToErrorStepUpdateTask( + index, + policy, + currentStepKey, + cause, + () -> now, + (idxMeta, stepKey) -> new MockStep(stepKey, new StepKey("next-phase", "action", "step")), + state -> {} + ); ClusterState newState = task.execute(clusterState); assertThat(newState, sameInstance(clusterState)); } @@ -113,28 +133,44 @@ public void testExecuteNoopDifferentPolicy() throws IOException { Exception cause = new ElasticsearchException("THIS IS AN EXPECTED CAUSE"); setStateToKey(currentStepKey); setStatePolicy("not-" + policy); - MoveToErrorStepUpdateTask task = new MoveToErrorStepUpdateTask(index, policy, currentStepKey, cause, () -> now, - (idxMeta, stepKey) -> new MockStep(stepKey, new StepKey("next-phase", "action", "step")), state -> {}); + MoveToErrorStepUpdateTask task = new MoveToErrorStepUpdateTask( + index, + policy, + currentStepKey, + cause, + () -> now, + (idxMeta, stepKey) -> new MockStep(stepKey, new StepKey("next-phase", "action", "step")), + state -> {} + ); ClusterState newState = task.execute(clusterState); assertThat(newState, sameInstance(clusterState)); } private void setStatePolicy(String policy) { clusterState = ClusterState.builder(clusterState) - .metadata(Metadata.builder(clusterState.metadata()) - .updateSettings(Settings.builder() - .put(LifecycleSettings.LIFECYCLE_NAME, policy).build(), index.getName())).build(); + .metadata( + Metadata.builder(clusterState.metadata()) + .updateSettings(Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, policy).build(), index.getName()) + ) + .build(); } + private void setStateToKey(StepKey stepKey) { LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder( - LifecycleExecutionState.fromIndexMetadata(clusterState.metadata().index(index))); + LifecycleExecutionState.fromIndexMetadata(clusterState.metadata().index(index)) + ); lifecycleState.setPhase(stepKey.getPhase()); lifecycleState.setAction(stepKey.getAction()); lifecycleState.setStep(stepKey.getName()); clusterState = ClusterState.builder(clusterState) - .metadata(Metadata.builder(clusterState.getMetadata()) - .put(IndexMetadata.builder(clusterState.getMetadata().index(index)) - .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()))).build(); + .metadata( + Metadata.builder(clusterState.getMetadata()) + .put( + IndexMetadata.builder(clusterState.getMetadata().index(index)) + .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()) + ) + ) + .build(); } } diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/MoveToNextStepUpdateTaskTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/MoveToNextStepUpdateTaskTests.java index dac83e49a0ef2..3783f0569dce2 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/MoveToNextStepUpdateTaskTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/MoveToNextStepUpdateTaskTests.java @@ -13,9 +13,9 @@ import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.index.Index; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.ilm.IndexLifecycleMetadata; import org.elasticsearch.xpack.core.ilm.LifecycleExecutionState; import org.elasticsearch.xpack.core.ilm.LifecyclePolicy; @@ -45,15 +45,19 @@ public class MoveToNextStepUpdateTaskTests extends ESTestCase { public void setupClusterState() { policy = randomAlphaOfLength(10); IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(5)) - .settings(settings(Version.CURRENT) - .put(LifecycleSettings.LIFECYCLE_NAME, policy)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policy)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); index = indexMetadata.getIndex(); lifecyclePolicy = LifecyclePolicyTests.randomTestLifecyclePolicy(policy); IndexLifecycleMetadata ilmMeta = new IndexLifecycleMetadata( - Collections.singletonMap(policy, new LifecyclePolicyMetadata(lifecyclePolicy, Collections.emptyMap(), - randomNonNegativeLong(), randomNonNegativeLong())), - OperationMode.RUNNING); + Collections.singletonMap( + policy, + new LifecyclePolicyMetadata(lifecyclePolicy, Collections.emptyMap(), randomNonNegativeLong(), randomNonNegativeLong()) + ), + OperationMode.RUNNING + ); Metadata metadata = Metadata.builder() .persistentSettings(settings(Version.CURRENT).build()) .put(IndexMetadata.builder(indexMetadata)) @@ -71,8 +75,15 @@ public void testExecuteSuccessfullyMoved() throws Exception { setStateToKey(currentStepKey, now); AtomicBoolean changed = new AtomicBoolean(false); - MoveToNextStepUpdateTask task = new MoveToNextStepUpdateTask(index, policy, currentStepKey, nextStepKey, - () -> now, new AlwaysExistingStepRegistry(), state -> changed.set(true)); + MoveToNextStepUpdateTask task = new MoveToNextStepUpdateTask( + index, + policy, + currentStepKey, + nextStepKey, + () -> now, + new AlwaysExistingStepRegistry(), + state -> changed.set(true) + ); ClusterState newState = task.execute(clusterState); LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(newState.getMetadata().index(index)); StepKey actualKey = LifecycleExecutionState.getCurrentStepKey(lifecycleState); @@ -89,8 +100,15 @@ public void testExecuteDifferentCurrentStep() throws Exception { StepKey notCurrentStepKey = new StepKey("not-current", "not-current", "not-current"); long now = randomNonNegativeLong(); setStateToKey(notCurrentStepKey, now); - MoveToNextStepUpdateTask task = new MoveToNextStepUpdateTask(index, policy, currentStepKey, null, - () -> now, new AlwaysExistingStepRegistry(), null); + MoveToNextStepUpdateTask task = new MoveToNextStepUpdateTask( + index, + policy, + currentStepKey, + null, + () -> now, + new AlwaysExistingStepRegistry(), + null + ); ClusterState newState = task.execute(clusterState); assertSame(newState, clusterState); } @@ -100,8 +118,15 @@ public void testExecuteDifferentPolicy() throws Exception { long now = randomNonNegativeLong(); setStateToKey(currentStepKey, now); setStatePolicy("not-" + policy); - MoveToNextStepUpdateTask task = new MoveToNextStepUpdateTask(index, policy, currentStepKey, null, () -> now, - new AlwaysExistingStepRegistry(), null); + MoveToNextStepUpdateTask task = new MoveToNextStepUpdateTask( + index, + policy, + currentStepKey, + null, + () -> now, + new AlwaysExistingStepRegistry(), + null + ); ClusterState newState = task.execute(clusterState); assertSame(newState, clusterState); } @@ -115,8 +140,15 @@ public void testExecuteSuccessfulMoveWithInvalidNextStep() throws Exception { setStateToKey(currentStepKey, now); SetOnce changed = new SetOnce<>(); - MoveToNextStepUpdateTask task = new MoveToNextStepUpdateTask(index, policy, currentStepKey, - invalidNextStep, () -> now, new AlwaysExistingStepRegistry(), s -> changed.set(true)); + MoveToNextStepUpdateTask task = new MoveToNextStepUpdateTask( + index, + policy, + currentStepKey, + invalidNextStep, + () -> now, + new AlwaysExistingStepRegistry(), + s -> changed.set(true) + ); ClusterState newState = task.execute(clusterState); LifecycleExecutionState lifecycleState = LifecycleExecutionState.fromIndexMetadata(newState.getMetadata().index(index)); StepKey actualKey = LifecycleExecutionState.getCurrentStepKey(lifecycleState); @@ -135,8 +167,15 @@ public void testOnFailure() { setStateToKey(currentStepKey, now); - MoveToNextStepUpdateTask task = new MoveToNextStepUpdateTask(index, policy, currentStepKey, nextStepKey, () -> now, - new AlwaysExistingStepRegistry(), state -> {}); + MoveToNextStepUpdateTask task = new MoveToNextStepUpdateTask( + index, + policy, + currentStepKey, + nextStepKey, + () -> now, + new AlwaysExistingStepRegistry(), + state -> {} + ); Exception expectedException = new RuntimeException(); task.onFailure(randomAlphaOfLength(10), expectedException); } @@ -158,14 +197,18 @@ public boolean stepExists(String policy, StepKey stepKey) { private void setStatePolicy(String policy) { clusterState = ClusterState.builder(clusterState) - .metadata(Metadata.builder(clusterState.metadata()) - .updateSettings(Settings.builder() - .put(LifecycleSettings.LIFECYCLE_NAME, policy).build(), index.getName())).build(); + .metadata( + Metadata.builder(clusterState.metadata()) + .updateSettings(Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, policy).build(), index.getName()) + ) + .build(); } + private void setStateToKey(StepKey stepKey, long now) { LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder( - LifecycleExecutionState.fromIndexMetadata(clusterState.metadata().index(index))); + LifecycleExecutionState.fromIndexMetadata(clusterState.metadata().index(index)) + ); lifecycleState.setPhase(stepKey.getPhase()); lifecycleState.setPhaseTime(now); lifecycleState.setAction(stepKey.getAction()); @@ -174,8 +217,13 @@ private void setStateToKey(StepKey stepKey, long now) { lifecycleState.setStepTime(now); lifecycleState.setPhaseDefinition("{\"actions\":{\"TEST_ACTION\":{}}}"); clusterState = ClusterState.builder(clusterState) - .metadata(Metadata.builder(clusterState.getMetadata()) - .put(IndexMetadata.builder(clusterState.getMetadata().index(index)) - .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()))).build(); + .metadata( + Metadata.builder(clusterState.getMetadata()) + .put( + IndexMetadata.builder(clusterState.getMetadata().index(index)) + .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()) + ) + ) + .build(); } } diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/OperationModeUpdateTaskTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/OperationModeUpdateTaskTests.java index f6a949ff63e10..ad9696d83123c 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/OperationModeUpdateTaskTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/OperationModeUpdateTaskTests.java @@ -37,13 +37,11 @@ public void testExecute() { public void testExecuteWithEmptyMetadata() { OperationMode requestedMode = OperationMode.STOPPING; - OperationMode newMode = executeUpdate(false, IndexLifecycleMetadata.EMPTY.getOperationMode(), - requestedMode, false); + OperationMode newMode = executeUpdate(false, IndexLifecycleMetadata.EMPTY.getOperationMode(), requestedMode, false); assertThat(newMode, equalTo(requestedMode)); requestedMode = randomFrom(OperationMode.RUNNING, OperationMode.STOPPED); - newMode = executeUpdate(false, IndexLifecycleMetadata.EMPTY.getOperationMode(), - requestedMode, false); + newMode = executeUpdate(false, IndexLifecycleMetadata.EMPTY.getOperationMode(), requestedMode, false); assertThat(newMode, equalTo(OperationMode.RUNNING)); } @@ -57,19 +55,26 @@ private void assertNoMove(OperationMode currentMode, OperationMode requestedMode assertThat(newMode, equalTo(currentMode)); } - private OperationMode executeUpdate(boolean metadataInstalled, OperationMode currentMode, OperationMode requestMode, - boolean assertSameClusterState) { + private OperationMode executeUpdate( + boolean metadataInstalled, + OperationMode currentMode, + OperationMode requestMode, + boolean assertSameClusterState + ) { IndexLifecycleMetadata indexLifecycleMetadata = new IndexLifecycleMetadata(Collections.emptyMap(), currentMode); - SnapshotLifecycleMetadata snapshotLifecycleMetadata = - new SnapshotLifecycleMetadata(Collections.emptyMap(), currentMode, new SnapshotLifecycleStats()); + SnapshotLifecycleMetadata snapshotLifecycleMetadata = new SnapshotLifecycleMetadata( + Collections.emptyMap(), + currentMode, + new SnapshotLifecycleStats() + ); ImmutableOpenMap.Builder customsMapBuilder = ImmutableOpenMap.builder(); - Metadata.Builder metadata = Metadata.builder() - .persistentSettings(settings(Version.CURRENT).build()); + Metadata.Builder metadata = Metadata.builder().persistentSettings(settings(Version.CURRENT).build()); if (metadataInstalled) { - metadata.customs(customsMapBuilder - .fPut(IndexLifecycleMetadata.TYPE, indexLifecycleMetadata) - .fPut(SnapshotLifecycleMetadata.TYPE, snapshotLifecycleMetadata) - .build()); + metadata.customs( + customsMapBuilder.fPut(IndexLifecycleMetadata.TYPE, indexLifecycleMetadata) + .fPut(SnapshotLifecycleMetadata.TYPE, snapshotLifecycleMetadata) + .build() + ); } ClusterState state = ClusterState.builder(ClusterName.DEFAULT).metadata(metadata).build(); OperationModeUpdateTask task = OperationModeUpdateTask.ilmMode(requestMode); diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/PhaseStatsTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/PhaseStatsTests.java index 213577293417d..cecc218d0552a 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/PhaseStatsTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/PhaseStatsTests.java @@ -34,16 +34,18 @@ protected PhaseStats mutateInstance(PhaseStats instance) throws IOException { TimeValue minimumAge = instance.getAfter(); String[] actionNames = instance.getActionNames(); switch (between(0, 1)) { - case 0: - minimumAge = randomValueOtherThan(minimumAge, - () -> TimeValue.parseTimeValue(randomTimeValue(0, 1000000000, "s", "m", "h", "d"), "test_after")); - break; - case 1: - actionNames = Arrays.copyOf(actionNames, actionNames.length + 1); - actionNames[actionNames.length - 1] = randomAlphaOfLengthBetween(10, 20); - break; - default: - throw new AssertionError("Illegal randomisation branch"); + case 0: + minimumAge = randomValueOtherThan( + minimumAge, + () -> TimeValue.parseTimeValue(randomTimeValue(0, 1000000000, "s", "m", "h", "d"), "test_after") + ); + break; + case 1: + actionNames = Arrays.copyOf(actionNames, actionNames.length + 1); + actionNames[actionNames.length - 1] = randomAlphaOfLengthBetween(10, 20); + break; + default: + throw new AssertionError("Illegal randomisation branch"); } return new PhaseStats(minimumAge, actionNames, instance.getConfigurations()); } diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/PolicyStatsTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/PolicyStatsTests.java index 2c51e8fdc77b7..0454342f380bd 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/PolicyStatsTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/PolicyStatsTests.java @@ -38,15 +38,15 @@ protected PolicyStats mutateInstance(PolicyStats instance) throws IOException { Map phaseStats = instance.getPhaseStats(); int numberIndicesManaged = instance.getIndicesManaged(); switch (between(0, 1)) { - case 0: - phaseStats = new HashMap<>(phaseStats); - phaseStats.put(randomAlphaOfLength(11), PhaseStatsTests.randomPhaseStats()); - break; - case 1: - numberIndicesManaged += randomIntBetween(1, 10); - break; - default: - throw new AssertionError("Illegal randomisation branch"); + case 0: + phaseStats = new HashMap<>(phaseStats); + phaseStats.put(randomAlphaOfLength(11), PhaseStatsTests.randomPhaseStats()); + break; + case 1: + numberIndicesManaged += randomIntBetween(1, 10); + break; + default: + throw new AssertionError("Illegal randomisation branch"); } return new PolicyStats(phaseStats, numberIndicesManaged); } diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/PolicyStepsRegistryTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/PolicyStepsRegistryTests.java index 9dcd515d4d49d..f28b9ba75522e 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/PolicyStepsRegistryTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/PolicyStepsRegistryTests.java @@ -18,13 +18,13 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.TransportAddress; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.index.Index; +import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.NodeRoles; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.index.Index; -import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.test.NodeRoles; import org.elasticsearch.xpack.core.ilm.ErrorStep; import org.elasticsearch.xpack.core.ilm.IndexLifecycleMetadata; import org.elasticsearch.xpack.core.ilm.InitializePolicyContextStep; @@ -62,8 +62,11 @@ public class PolicyStepsRegistryTests extends ESTestCase { private static final NamedXContentRegistry REGISTRY = new NamedXContentRegistry(new IndexLifecycle(Settings.EMPTY).getNamedXContent()); private IndexMetadata emptyMetadata(Index index) { - return IndexMetadata.builder(index.getName()).settings(settings(Version.CURRENT)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + return IndexMetadata.builder(index.getName()) + .settings(settings(Version.CURRENT)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); } public void testGetFirstStep() { @@ -98,12 +101,14 @@ public void testGetStep() { LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); lifecycleState.setPhaseDefinition(phaseJson); IndexMetadata indexMetadata = IndexMetadata.builder("test") - .settings(Settings.builder() - .put("index.number_of_shards", 1) - .put("index.number_of_replicas", 0) - .put("index.version.created", Version.CURRENT) - .put(LifecycleSettings.LIFECYCLE_NAME, "policy") - .build()) + .settings( + Settings.builder() + .put("index.number_of_shards", 1) + .put("index.number_of_replicas", 0) + .put("index.version.created", Version.CURRENT) + .put(LifecycleSettings.LIFECYCLE_NAME, "policy") + .build() + ) .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()) .build(); SortedMap metas = new TreeMap<>(); @@ -125,11 +130,16 @@ public void testGetStepErrorStep() { public void testGetStepUnknownPolicy() { PolicyStepsRegistry registry = new PolicyStepsRegistry(null, null, null, NamedXContentRegistry.EMPTY, null, null); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> registry.getStep(emptyMetadata(new Index("test", "uuid")), MOCK_STEP_KEY)); - assertThat(e.getMessage(), - containsString("failed to retrieve step {\"phase\":\"mock\",\"action\":\"mock\",\"name\":\"mock\"}" + - " as index [test] has no policy")); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> registry.getStep(emptyMetadata(new Index("test", "uuid")), MOCK_STEP_KEY) + ); + assertThat( + e.getMessage(), + containsString( + "failed to retrieve step {\"phase\":\"mock\",\"action\":\"mock\",\"name\":\"mock\"}" + " as index [test] has no policy" + ) + ); } public void testGetStepForIndexWithNoPhaseGetsInitializationStep() { @@ -138,12 +148,14 @@ public void testGetStepForIndexWithNoPhaseGetsInitializationStep() { LifecyclePolicy policy = LifecyclePolicyTests.randomTimeseriesLifecyclePolicy("policy"); LifecyclePolicyMetadata policyMetadata = new LifecyclePolicyMetadata(policy, Collections.emptyMap(), 1, randomNonNegativeLong()); IndexMetadata indexMetadata = IndexMetadata.builder("test") - .settings(Settings.builder() - .put("index.number_of_shards", 1) - .put("index.number_of_replicas", 0) - .put("index.version.created", Version.CURRENT) - .put(LifecycleSettings.LIFECYCLE_NAME, "policy") - .build()) + .settings( + Settings.builder() + .put("index.number_of_shards", 1) + .put("index.number_of_replicas", 0) + .put("index.version.created", Version.CURRENT) + .put(LifecycleSettings.LIFECYCLE_NAME, "policy") + .build() + ) .build(); SortedMap metas = new TreeMap<>(); metas.put("policy", policyMetadata); @@ -166,19 +178,23 @@ public void testGetStepUnknownStepKey() { LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); lifecycleState.setPhaseDefinition(phaseJson); IndexMetadata indexMetadata = IndexMetadata.builder("test") - .settings(Settings.builder() - .put("index.number_of_shards", 1) - .put("index.number_of_replicas", 0) - .put("index.version.created", Version.CURRENT) - .put(LifecycleSettings.LIFECYCLE_NAME, "policy") - .build()) + .settings( + Settings.builder() + .put("index.number_of_shards", 1) + .put("index.number_of_replicas", 0) + .put("index.version.created", Version.CURRENT) + .put(LifecycleSettings.LIFECYCLE_NAME, "policy") + .build() + ) .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()) .build(); SortedMap metas = new TreeMap<>(); metas.put("policy", policyMetadata); PolicyStepsRegistry registry = new PolicyStepsRegistry(metas, null, null, REGISTRY, client, null); - Step actualStep = registry.getStep(indexMetadata, - new Step.StepKey(step.getKey().getPhase(), step.getKey().getAction(), step.getKey().getName() + "-bad")); + Step actualStep = registry.getStep( + indexMetadata, + new Step.StepKey(step.getKey().getPhase(), step.getKey().getAction(), step.getKey().getName() + "-bad") + ); assertNull(actualStep); } @@ -195,22 +211,28 @@ public void testUpdateFromNothingToSomethingToNothing() throws Exception { headers.put(randomAlphaOfLength(10), randomAlphaOfLength(10)); headers.put(randomAlphaOfLength(10), randomAlphaOfLength(10)); } - Map policyMap = Collections.singletonMap(newPolicy.getName(), - new LifecyclePolicyMetadata(newPolicy, headers, randomNonNegativeLong(), randomNonNegativeLong())); + Map policyMap = Collections.singletonMap( + newPolicy.getName(), + new LifecyclePolicyMetadata(newPolicy, headers, randomNonNegativeLong(), randomNonNegativeLong()) + ); IndexLifecycleMetadata lifecycleMetadata = new IndexLifecycleMetadata(policyMap, OperationMode.RUNNING); LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); lifecycleState.setPhase("new"); Metadata metadata = Metadata.builder() .persistentSettings(settings(Version.CURRENT).build()) .putCustom(IndexLifecycleMetadata.TYPE, lifecycleMetadata) - .put(IndexMetadata.builder("test") - .settings(Settings.builder() - .put("index.uuid", "uuid") - .put("index.number_of_shards", 1) - .put("index.number_of_replicas", 0) - .put("index.version.created", Version.CURRENT.id) - .put(LifecycleSettings.LIFECYCLE_NAME, policyName)) - .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap())) + .put( + IndexMetadata.builder("test") + .settings( + Settings.builder() + .put("index.uuid", "uuid") + .put("index.number_of_shards", 1) + .put("index.number_of_replicas", 0) + .put("index.version.created", Version.CURRENT.id) + .put(LifecycleSettings.LIFECYCLE_NAME, policyName) + ) + .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()) + ) .build(); try (XContentBuilder builder = JsonXContent.contentBuilder()) { builder.startObject(); @@ -222,7 +244,8 @@ public void testUpdateFromNothingToSomethingToNothing() throws Exception { DiscoveryNode masterNode = DiscoveryNode.createLocal( NodeRoles.masterNode(settings(Version.CURRENT).build()), new TransportAddress(TransportAddress.META_ADDRESS, 9300), - nodeId); + nodeId + ); ClusterState currentState = ClusterState.builder(ClusterName.DEFAULT) .metadata(metadata) .nodes(DiscoveryNodes.builder().localNodeId(nodeId).masterNodeId(nodeId).add(masterNode).build()) @@ -246,10 +269,14 @@ public void testUpdateFromNothingToSomethingToNothing() throws Exception { LifecycleExecutionState.Builder newIndexState = LifecycleExecutionState.builder(); newIndexState.setPhase(step.getKey().getPhase()); currentState = ClusterState.builder(currentState) - .metadata(Metadata.builder(currentState.metadata()) - .put(IndexMetadata.builder(currentState.metadata().index("test")) - .settings(Settings.builder().put(currentState.metadata().index("test").getSettings())) - .putCustom(ILM_CUSTOM_METADATA_KEY, newIndexState.build().asMap()))) + .metadata( + Metadata.builder(currentState.metadata()) + .put( + IndexMetadata.builder(currentState.metadata().index("test")) + .settings(Settings.builder().put(currentState.metadata().index("test").getSettings())) + .putCustom(ILM_CUSTOM_METADATA_KEY, newIndexState.build().asMap()) + ) + ) .nodes(DiscoveryNodes.builder().localNodeId(nodeId).masterNodeId(nodeId).add(masterNode).build()) .build(); registry.update(currentState.metadata().custom(IndexLifecycleMetadata.TYPE)); @@ -268,9 +295,8 @@ public void testUpdateFromNothingToSomethingToNothing() throws Exception { // remove policy lifecycleMetadata = new IndexLifecycleMetadata(Collections.emptyMap(), OperationMode.RUNNING); currentState = ClusterState.builder(currentState) - .metadata( - Metadata.builder(metadata) - .putCustom(IndexLifecycleMetadata.TYPE, lifecycleMetadata)).build(); + .metadata(Metadata.builder(metadata).putCustom(IndexLifecycleMetadata.TYPE, lifecycleMetadata)) + .build(); registry.update(currentState.metadata().custom(IndexLifecycleMetadata.TYPE)); assertTrue(registry.getLifecyclePolicyMap().isEmpty()); assertTrue(registry.getFirstStepMap().isEmpty()); @@ -287,8 +313,10 @@ public void testUpdateChangedPolicy() { headers.put(randomAlphaOfLength(10), randomAlphaOfLength(10)); headers.put(randomAlphaOfLength(10), randomAlphaOfLength(10)); } - Map policyMap = Collections.singletonMap(newPolicy.getName(), - new LifecyclePolicyMetadata(newPolicy, headers, randomNonNegativeLong(), randomNonNegativeLong())); + Map policyMap = Collections.singletonMap( + newPolicy.getName(), + new LifecyclePolicyMetadata(newPolicy, headers, randomNonNegativeLong(), randomNonNegativeLong()) + ); IndexLifecycleMetadata lifecycleMetadata = new IndexLifecycleMetadata(policyMap, OperationMode.RUNNING); Metadata metadata = Metadata.builder() .persistentSettings(settings(Version.CURRENT).build()) @@ -298,7 +326,8 @@ public void testUpdateChangedPolicy() { DiscoveryNode masterNode = DiscoveryNode.createLocal( NodeRoles.masterNode(settings(Version.CURRENT).build()), new TransportAddress(TransportAddress.META_ADDRESS, 9300), - nodeId); + nodeId + ); ClusterState currentState = ClusterState.builder(ClusterName.DEFAULT) .metadata(metadata) .nodes(DiscoveryNodes.builder().localNodeId(nodeId).masterNodeId(nodeId).add(masterNode).build()) @@ -309,11 +338,16 @@ public void testUpdateChangedPolicy() { // swap out policy newPolicy = LifecyclePolicyTests.randomTestLifecyclePolicy(policyName); - lifecycleMetadata = new IndexLifecycleMetadata(Collections.singletonMap(policyName, - new LifecyclePolicyMetadata(newPolicy, Collections.emptyMap(), - randomNonNegativeLong(), randomNonNegativeLong())), OperationMode.RUNNING); + lifecycleMetadata = new IndexLifecycleMetadata( + Collections.singletonMap( + policyName, + new LifecyclePolicyMetadata(newPolicy, Collections.emptyMap(), randomNonNegativeLong(), randomNonNegativeLong()) + ), + OperationMode.RUNNING + ); currentState = ClusterState.builder(currentState) - .metadata(Metadata.builder(metadata).putCustom(IndexLifecycleMetadata.TYPE, lifecycleMetadata)).build(); + .metadata(Metadata.builder(metadata).putCustom(IndexLifecycleMetadata.TYPE, lifecycleMetadata)) + .build(); registry.update(currentState.metadata().custom(IndexLifecycleMetadata.TYPE)); // TODO(talevy): assert changes... right now we do not support updates to policies. will require internal cleanup } @@ -345,8 +379,10 @@ public void testUpdatePolicyButNoPhaseChangeIndexStepsDontChange() throws Except headers.put(randomAlphaOfLength(10), randomAlphaOfLength(10)); headers.put(randomAlphaOfLength(10), randomAlphaOfLength(10)); } - Map policyMap = Collections.singletonMap(newPolicy.getName(), - new LifecyclePolicyMetadata(newPolicy, headers, randomNonNegativeLong(), randomNonNegativeLong())); + Map policyMap = Collections.singletonMap( + newPolicy.getName(), + new LifecyclePolicyMetadata(newPolicy, headers, randomNonNegativeLong(), randomNonNegativeLong()) + ); IndexLifecycleMetadata lifecycleMetadata = new IndexLifecycleMetadata(policyMap, OperationMode.RUNNING); LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder(); lifecycleState.setPhase("warm"); @@ -354,14 +390,18 @@ public void testUpdatePolicyButNoPhaseChangeIndexStepsDontChange() throws Except Metadata metadata = Metadata.builder() .persistentSettings(settings(Version.CURRENT).build()) .putCustom(IndexLifecycleMetadata.TYPE, lifecycleMetadata) - .put(IndexMetadata.builder("test") - .settings(Settings.builder() - .put("index.uuid", "uuid") - .put("index.number_of_shards", 1) - .put("index.number_of_replicas", 0) - .put("index.version.created", Version.CURRENT.id) - .put(LifecycleSettings.LIFECYCLE_NAME, policyName)) - .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap())) + .put( + IndexMetadata.builder("test") + .settings( + Settings.builder() + .put("index.uuid", "uuid") + .put("index.number_of_shards", 1) + .put("index.number_of_replicas", 0) + .put("index.version.created", Version.CURRENT.id) + .put(LifecycleSettings.LIFECYCLE_NAME, policyName) + ) + .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()) + ) .build(); try (XContentBuilder builder = JsonXContent.contentBuilder()) { builder.startObject(); @@ -373,7 +413,8 @@ public void testUpdatePolicyButNoPhaseChangeIndexStepsDontChange() throws Except DiscoveryNode masterNode = DiscoveryNode.createLocal( NodeRoles.masterNode(settings(Version.CURRENT).build()), new TransportAddress(TransportAddress.META_ADDRESS, 9300), - nodeId); + nodeId + ); ClusterState currentState = ClusterState.builder(ClusterName.DEFAULT) .metadata(metadata) .nodes(DiscoveryNodes.builder().localNodeId(nodeId).masterNodeId(nodeId).add(masterNode).build()) @@ -386,20 +427,23 @@ public void testUpdatePolicyButNoPhaseChangeIndexStepsDontChange() throws Except registry.update(currentState.metadata().custom(IndexLifecycleMetadata.TYPE)); Map registeredStepsForPolicy = registry.getStepMap().get(newPolicy.getName()); - Step shrinkStep = registeredStepsForPolicy.entrySet().stream() + Step shrinkStep = registeredStepsForPolicy.entrySet() + .stream() .filter(e -> e.getKey().getPhase().equals("warm") && e.getKey().getName().equals("shrink")) - .findFirst().get().getValue(); + .findFirst() + .get() + .getValue(); Step gotStep = registry.getStep(metadata.index(index), shrinkStep.getKey()); assertThat(((ShrinkStep) shrinkStep).getNumberOfShards(), equalTo(1)); assertThat(((ShrinkStep) gotStep).getNumberOfShards(), equalTo(1)); // Update the policy with the new policy, but keep the phase the same - policyMap = Collections.singletonMap(updatedPolicy.getName(), new LifecyclePolicyMetadata(updatedPolicy, headers, - randomNonNegativeLong(), randomNonNegativeLong())); + policyMap = Collections.singletonMap( + updatedPolicy.getName(), + new LifecyclePolicyMetadata(updatedPolicy, headers, randomNonNegativeLong(), randomNonNegativeLong()) + ); lifecycleMetadata = new IndexLifecycleMetadata(policyMap, OperationMode.RUNNING); - metadata = Metadata.builder(metadata) - .putCustom(IndexLifecycleMetadata.TYPE, lifecycleMetadata) - .build(); + metadata = Metadata.builder(metadata).putCustom(IndexLifecycleMetadata.TYPE, lifecycleMetadata).build(); try (XContentBuilder builder = JsonXContent.contentBuilder()) { builder.startObject(); metadata.toXContent(builder, ToXContent.EMPTY_PARAMS); @@ -412,9 +456,12 @@ public void testUpdatePolicyButNoPhaseChangeIndexStepsDontChange() throws Except registry.update(currentState.metadata().custom(IndexLifecycleMetadata.TYPE)); registeredStepsForPolicy = registry.getStepMap().get(newPolicy.getName()); - shrinkStep = registeredStepsForPolicy.entrySet().stream() + shrinkStep = registeredStepsForPolicy.entrySet() + .stream() .filter(e -> e.getKey().getPhase().equals("warm") && e.getKey().getName().equals("shrink")) - .findFirst().get().getValue(); + .findFirst() + .get() + .getValue(); gotStep = registry.getStep(metadata.index(index), shrinkStep.getKey()); assertThat(((ShrinkStep) shrinkStep).getNumberOfShards(), equalTo(2)); assertThat(((ShrinkStep) gotStep).getNumberOfShards(), equalTo(1)); diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/SetStepInfoUpdateTaskTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/SetStepInfoUpdateTaskTests.java index 1e1dd8963bc5f..2c1908a641484 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/SetStepInfoUpdateTaskTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/SetStepInfoUpdateTaskTests.java @@ -18,14 +18,14 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.logging.Loggers; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.ToXContentObject; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.index.Index; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.MockLogAppender; import org.elasticsearch.test.junit.annotations.TestLogging; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.ToXContentObject; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.ilm.LifecycleExecutionState; import org.elasticsearch.xpack.core.ilm.LifecycleSettings; import org.elasticsearch.xpack.core.ilm.Step.StepKey; @@ -46,9 +46,10 @@ public class SetStepInfoUpdateTaskTests extends ESTestCase { public void setupClusterState() { policy = randomAlphaOfLength(10); IndexMetadata indexMetadata = IndexMetadata.builder(randomAlphaOfLength(5)) - .settings(settings(Version.CURRENT) - .put(LifecycleSettings.LIFECYCLE_NAME, policy)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policy)) + .numberOfShards(randomIntBetween(1, 5)) + .numberOfReplicas(randomIntBetween(0, 5)) + .build(); index = indexMetadata.getIndex(); Metadata metadata = Metadata.builder() .persistentSettings(settings(Version.CURRENT).build()) @@ -108,7 +109,7 @@ public void testExecuteNoopDifferentPolicy() throws Exception { assertThat(newState, sameInstance(clusterState)); } - @TestLogging(reason = "logging test", value="logger.org.elasticsearch.xpack.ilm.SetStepInfoUpdateTask:WARN") + @TestLogging(reason = "logging test", value = "logger.org.elasticsearch.xpack.ilm.SetStepInfoUpdateTask:WARN") public void testOnFailure() throws IllegalAccessException { StepKey currentStepKey = new StepKey("current-phase", "current-action", "current-name"); ToXContentObject stepInfo = getRandomStepInfo(); @@ -120,12 +121,13 @@ public void testOnFailure() throws IllegalAccessException { final MockLogAppender mockAppender = new MockLogAppender(); mockAppender.start(); mockAppender.addExpectation( - new MockLogAppender.SeenEventExpectation( - "warning", - SetStepInfoUpdateTask.class.getCanonicalName(), - Level.WARN, - "*policy [" + policy + "] for index [" + index + "] failed trying to set step info for step [" - + currentStepKey + "].")); + new MockLogAppender.SeenEventExpectation( + "warning", + SetStepInfoUpdateTask.class.getCanonicalName(), + Level.WARN, + "*policy [" + policy + "] for index [" + index + "] failed trying to set step info for step [" + currentStepKey + "]." + ) + ); final Logger taskLogger = LogManager.getLogger(SetStepInfoUpdateTask.class); Loggers.addAppender(taskLogger, mockAppender); @@ -140,21 +142,30 @@ public void testOnFailure() throws IllegalAccessException { private void setStatePolicy(String policy) { clusterState = ClusterState.builder(clusterState) - .metadata(Metadata.builder(clusterState.metadata()) - .updateSettings(Settings.builder() - .put(LifecycleSettings.LIFECYCLE_NAME, policy).build(), index.getName())).build(); + .metadata( + Metadata.builder(clusterState.metadata()) + .updateSettings(Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, policy).build(), index.getName()) + ) + .build(); } + private void setStateToKey(StepKey stepKey) { LifecycleExecutionState.Builder lifecycleState = LifecycleExecutionState.builder( - LifecycleExecutionState.fromIndexMetadata(clusterState.metadata().index(index))); + LifecycleExecutionState.fromIndexMetadata(clusterState.metadata().index(index)) + ); lifecycleState.setPhase(stepKey.getPhase()); lifecycleState.setAction(stepKey.getAction()); lifecycleState.setStep(stepKey.getName()); clusterState = ClusterState.builder(clusterState) - .metadata(Metadata.builder(clusterState.getMetadata()) - .put(IndexMetadata.builder(clusterState.getMetadata().index(index)) - .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()))).build(); + .metadata( + Metadata.builder(clusterState.getMetadata()) + .put( + IndexMetadata.builder(clusterState.getMetadata().index(index)) + .putCustom(ILM_CUSTOM_METADATA_KEY, lifecycleState.build().asMap()) + ) + ) + .build(); } } diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/TimeValueScheduleTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/TimeValueScheduleTests.java index 7332328bebeda..522b820c94ce4 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/TimeValueScheduleTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/TimeValueScheduleTests.java @@ -37,9 +37,11 @@ public void setUpStartAndInterval() { public void testHashcodeAndEquals() { for (int i = 0; i < 20; i++) { - EqualsHashCodeTestUtils.checkEqualsAndHashCode(createRandomInstance(), - instance -> new TimeValueSchedule(instance.getInterval()), - instance -> new TimeValueSchedule(randomValueOtherThan(instance.getInterval(), () -> createRandomTimeValue()))); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + createRandomInstance(), + instance -> new TimeValueSchedule(instance.getInterval()), + instance -> new TimeValueSchedule(randomValueOtherThan(instance.getInterval(), () -> createRandomTimeValue())) + ); } } @@ -66,8 +68,10 @@ public void testNextScheduledTimeAtStartTime() { public void testNextScheduledTimeAfterFirstTrigger() { long numberIntervalsPassed = randomLongBetween(0, 10000); long triggerTime = start + (numberIntervalsPassed + 1) * interval.millis(); - long now = start - + randomLongBetween(numberIntervalsPassed * interval.millis(), (numberIntervalsPassed + 1) * interval.millis() - 1); + long now = start + randomLongBetween( + numberIntervalsPassed * interval.millis(), + (numberIntervalsPassed + 1) * interval.millis() - 1 + ); TimeValueSchedule schedule = new TimeValueSchedule(interval); assertEquals(triggerTime, schedule.nextScheduledTimeAfter(start, now)); } diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/action/TransportExplainLifecycleActionTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/action/TransportExplainLifecycleActionTests.java index f89d4d1400bf4..a252ea344be4a 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/action/TransportExplainLifecycleActionTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/action/TransportExplainLifecycleActionTests.java @@ -9,9 +9,9 @@ import org.elasticsearch.Version; import org.elasticsearch.cluster.metadata.IndexMetadata; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.ilm.ErrorStep; import org.elasticsearch.xpack.core.ilm.IndexLifecycleExplainResponse; import org.elasticsearch.xpack.core.ilm.LifecycleAction; @@ -35,19 +35,19 @@ public class TransportExplainLifecycleActionTests extends ESTestCase { - public static final String PHASE_DEFINITION = "{\n" + - " \"policy\" : \"my-policy\",\n" + - " \"phase_definition\" : {\n" + - " \"min_age\" : \"20m\",\n" + - " \"actions\" : {\n" + - " \"rollover\" : {\n" + - " \"max_age\" : \"5s\"\n" + - " }\n" + - " }\n" + - " },\n" + - " \"version\" : 1,\n" + - " \"modified_date_in_millis\" : 1578521007076\n" + - " }"; + public static final String PHASE_DEFINITION = "{\n" + + " \"policy\" : \"my-policy\",\n" + + " \"phase_definition\" : {\n" + + " \"min_age\" : \"20m\",\n" + + " \"actions\" : {\n" + + " \"rollover\" : {\n" + + " \"max_age\" : \"5s\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"version\" : 1,\n" + + " \"modified_date_in_millis\" : 1578521007076\n" + + " }"; private static final NamedXContentRegistry REGISTRY; @@ -76,8 +76,13 @@ public void testGetIndexLifecycleExplainResponse() throws IOException { .putCustom(ILM_CUSTOM_METADATA_KEY, errorStepState.build().asMap()) .build(); - IndexLifecycleExplainResponse onlyErrorsResponse = getIndexLifecycleExplainResponse(meta, true, true, indexLifecycleService, - REGISTRY); + IndexLifecycleExplainResponse onlyErrorsResponse = getIndexLifecycleExplainResponse( + meta, + true, + true, + indexLifecycleService, + REGISTRY + ); assertThat(onlyErrorsResponse, notNullValue()); assertThat(onlyErrorsResponse.getIndex(), is(indexInErrorStep)); assertThat(onlyErrorsResponse.getStep(), is(ErrorStep.NAME)); @@ -102,12 +107,22 @@ public void testGetIndexLifecycleExplainResponse() throws IOException { .putCustom(ILM_CUSTOM_METADATA_KEY, checkRolloverReadyStepState.build().asMap()) .build(); - IndexLifecycleExplainResponse onlyErrorsResponse = getIndexLifecycleExplainResponse(meta, true, true, indexLifecycleService, - REGISTRY); + IndexLifecycleExplainResponse onlyErrorsResponse = getIndexLifecycleExplainResponse( + meta, + true, + true, + indexLifecycleService, + REGISTRY + ); assertThat(onlyErrorsResponse, nullValue()); - IndexLifecycleExplainResponse allManagedResponse = getIndexLifecycleExplainResponse(meta, false, true, indexLifecycleService, - REGISTRY); + IndexLifecycleExplainResponse allManagedResponse = getIndexLifecycleExplainResponse( + meta, + false, + true, + indexLifecycleService, + REGISTRY + ); assertThat(allManagedResponse, notNullValue()); assertThat(allManagedResponse.getIndex(), is(indexInCheckRolloverStep)); assertThat(allManagedResponse.getStep(), is(WaitForRolloverReadyStep.NAME)); @@ -125,8 +140,13 @@ public void testGetIndexLifecycleExplainResponse() throws IOException { .numberOfReplicas(randomIntBetween(0, 5)) .build(); - IndexLifecycleExplainResponse onlyErrorsResponse = getIndexLifecycleExplainResponse(meta, true, true, indexLifecycleService, - REGISTRY); + IndexLifecycleExplainResponse onlyErrorsResponse = getIndexLifecycleExplainResponse( + meta, + true, + true, + indexLifecycleService, + REGISTRY + ); assertThat(onlyErrorsResponse, notNullValue()); assertThat(onlyErrorsResponse.getPolicyName(), is("random-policy")); } @@ -142,8 +162,13 @@ public void testGetIndexLifecycleExplainResponse() throws IOException { .numberOfReplicas(randomIntBetween(0, 5)) .build(); - IndexLifecycleExplainResponse onlyManaged = getIndexLifecycleExplainResponse(meta, false, true, indexLifecycleService, - REGISTRY); + IndexLifecycleExplainResponse onlyManaged = getIndexLifecycleExplainResponse( + meta, + false, + true, + indexLifecycleService, + REGISTRY + ); assertThat(onlyManaged, nullValue()); } } diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/action/TransportStopILMActionTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/action/TransportStopILMActionTests.java index 1592a6e272425..bd269b4832aa1 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/action/TransportStopILMActionTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/action/TransportStopILMActionTests.java @@ -47,10 +47,21 @@ public void onFailure(Exception e) { public void testStopILMClusterStatePriorityIsImmediate() { ClusterService clusterService = mock(ClusterService.class); - TransportStopILMAction transportStopILMAction = new TransportStopILMAction(mock(TransportService.class), - clusterService, mock(ThreadPool.class), mock(ActionFilters.class), mock(IndexNameExpressionResolver.class)); - Task task = new Task(randomLong(), "transport", StopILMAction.NAME, "description", - new TaskId(randomLong() + ":" + randomLong()), emptyMap()); + TransportStopILMAction transportStopILMAction = new TransportStopILMAction( + mock(TransportService.class), + clusterService, + mock(ThreadPool.class), + mock(ActionFilters.class), + mock(IndexNameExpressionResolver.class) + ); + Task task = new Task( + randomLong(), + "transport", + StopILMAction.NAME, + "description", + new TaskId(randomLong() + ":" + randomLong()), + emptyMap() + ); StopILMRequest request = new StopILMRequest(); transportStopILMAction.masterOperation(task, request, ClusterState.EMPTY_STATE, EMPTY_LISTENER); diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/history/ILMHistoryItemTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/history/ILMHistoryItemTests.java index 46a2b767480b1..d888154aa064b 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/history/ILMHistoryItemTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/history/ILMHistoryItemTests.java @@ -8,9 +8,9 @@ package org.elasticsearch.xpack.ilm.history; import org.elasticsearch.common.Strings; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.ilm.LifecycleExecutionState; import java.io.IOException; @@ -22,7 +22,11 @@ public class ILMHistoryItemTests extends ESTestCase { public void testToXContent() throws IOException { - ILMHistoryItem success = ILMHistoryItem.success("index", "policy", 1234L, 100L, + ILMHistoryItem success = ILMHistoryItem.success( + "index", + "policy", + 1234L, + 100L, LifecycleExecutionState.builder() .setPhase("phase") .setAction("action") @@ -32,9 +36,14 @@ public void testToXContent() throws IOException { .setStepTime(30L) .setPhaseDefinition("{}") .setStepInfo("{\"step_info\": \"foo\"") - .build()); + .build() + ); - ILMHistoryItem failure = ILMHistoryItem.failure("index", "policy", 1234L, 100L, + ILMHistoryItem failure = ILMHistoryItem.failure( + "index", + "policy", + 1234L, + 100L, LifecycleExecutionState.builder() .setPhase("phase") .setAction("action") @@ -48,46 +57,56 @@ public void testToXContent() throws IOException { .setPhaseDefinition("{\"phase_json\": \"eggplant\"}") .setStepInfo("{\"step_info\": \"foo\"") .build(), - new IllegalArgumentException("failure")); + new IllegalArgumentException("failure") + ); try (XContentBuilder builder = jsonBuilder()) { success.toXContent(builder, ToXContent.EMPTY_PARAMS); String json = Strings.toString(builder); - assertThat(json, equalTo("{\"index\":\"index\"," + - "\"policy\":\"policy\"," + - "\"@timestamp\":1234," + - "\"index_age\":100," + - "\"success\":true," + - "\"state\":{\"phase\":\"phase\"," + - "\"phase_definition\":\"{}\"," + - "\"action_time\":\"20\"," + - "\"phase_time\":\"10\"," + - "\"step_info\":\"{\\\"step_info\\\": \\\"foo\\\"\",\"action\":\"action\",\"step\":\"step\",\"step_time\":\"30\"}}" - )); + assertThat( + json, + equalTo( + "{\"index\":\"index\"," + + "\"policy\":\"policy\"," + + "\"@timestamp\":1234," + + "\"index_age\":100," + + "\"success\":true," + + "\"state\":{\"phase\":\"phase\"," + + "\"phase_definition\":\"{}\"," + + "\"action_time\":\"20\"," + + "\"phase_time\":\"10\"," + + "\"step_info\":\"{\\\"step_info\\\": \\\"foo\\\"\",\"action\":\"action\",\"step\":\"step\",\"step_time\":\"30\"}}" + ) + ); } try (XContentBuilder builder = jsonBuilder()) { failure.toXContent(builder, ToXContent.EMPTY_PARAMS); String json = Strings.toString(builder); - assertThat(json, startsWith("{\"index\":\"index\"," + - "\"policy\":\"policy\"," + - "\"@timestamp\":1234," + - "\"index_age\":100," + - "\"success\":false," + - "\"state\":{\"phase\":\"phase\"," + - "\"failed_step\":\"step\"," + - "\"phase_definition\":\"{\\\"phase_json\\\": \\\"eggplant\\\"}\"," + - "\"action_time\":\"20\"," + - "\"is_auto_retryable_error\":\"true\"," + - "\"failed_step_retry_count\":\"7\"," + - "\"phase_time\":\"10\"," + - "\"step_info\":\"{\\\"step_info\\\": \\\"foo\\\"\"," + - "\"action\":\"action\"," + - "\"step\":\"ERROR\"," + - "\"step_time\":\"30\"}," + - "\"error_details\":\"{\\\"type\\\":\\\"illegal_argument_exception\\\"," + - "\\\"reason\\\":\\\"failure\\\"," + - "\\\"stack_trace\\\":\\\"java.lang.IllegalArgumentException: failure")); + assertThat( + json, + startsWith( + "{\"index\":\"index\"," + + "\"policy\":\"policy\"," + + "\"@timestamp\":1234," + + "\"index_age\":100," + + "\"success\":false," + + "\"state\":{\"phase\":\"phase\"," + + "\"failed_step\":\"step\"," + + "\"phase_definition\":\"{\\\"phase_json\\\": \\\"eggplant\\\"}\"," + + "\"action_time\":\"20\"," + + "\"is_auto_retryable_error\":\"true\"," + + "\"failed_step_retry_count\":\"7\"," + + "\"phase_time\":\"10\"," + + "\"step_info\":\"{\\\"step_info\\\": \\\"foo\\\"\"," + + "\"action\":\"action\"," + + "\"step\":\"ERROR\"," + + "\"step_time\":\"30\"}," + + "\"error_details\":\"{\\\"type\\\":\\\"illegal_argument_exception\\\"," + + "\\\"reason\\\":\\\"failure\\\"," + + "\\\"stack_trace\\\":\\\"java.lang.IllegalArgumentException: failure" + ) + ); } } } diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/history/ILMHistoryStoreTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/history/ILMHistoryStoreTests.java index 7ae5871dd11c2..a53890f48a46f 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/history/ILMHistoryStoreTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/history/ILMHistoryStoreTests.java @@ -27,15 +27,15 @@ import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.TriFunction; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.DeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.test.ClusterServiceUtils; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.client.NoOpClient; import org.elasticsearch.threadpool.TestThreadPool; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.DeprecationHandler; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.ilm.LifecycleExecutionState; import org.elasticsearch.xpack.core.template.IndexTemplateConfig; import org.hamcrest.Matchers; @@ -67,21 +67,33 @@ public void setup() { threadPool = new TestThreadPool(this.getClass().getName()); client = new VerifyingClient(threadPool); clusterService = ClusterServiceUtils.createClusterService(threadPool); - ILMHistoryTemplateRegistry registry = new ILMHistoryTemplateRegistry(clusterService.getSettings(), clusterService, threadPool, - client, NamedXContentRegistry.EMPTY); - Map templates = - registry.getComposableTemplateConfigs().stream().collect(Collectors.toMap(IndexTemplateConfig::getTemplateName, - this::parseIndexTemplate)); + ILMHistoryTemplateRegistry registry = new ILMHistoryTemplateRegistry( + clusterService.getSettings(), + clusterService, + threadPool, + client, + NamedXContentRegistry.EMPTY + ); + Map templates = registry.getComposableTemplateConfigs() + .stream() + .collect(Collectors.toMap(IndexTemplateConfig::getTemplateName, this::parseIndexTemplate)); ClusterState state = clusterService.state(); - ClusterServiceUtils.setState(clusterService, - ClusterState.builder(state).metadata(Metadata.builder(state.metadata()).indexTemplates(templates)).build()); + ClusterServiceUtils.setState( + clusterService, + ClusterState.builder(state).metadata(Metadata.builder(state.metadata()).indexTemplates(templates)).build() + ); historyStore = new ILMHistoryStore(Settings.EMPTY, client, clusterService, threadPool); } private ComposableIndexTemplate parseIndexTemplate(IndexTemplateConfig c) { try { - return ComposableIndexTemplate.parse(JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, c.loadBytes())); + return ComposableIndexTemplate.parse( + JsonXContent.jsonXContent.createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + c.loadBytes() + ) + ); } catch (IOException e) { throw new IllegalStateException(e); } @@ -118,10 +130,13 @@ public void testPut() throws Exception { String policyId = randomAlphaOfLength(5); final long timestamp = randomNonNegativeLong(); { - ILMHistoryItem record = ILMHistoryItem.success("index", policyId, timestamp, 10L, - LifecycleExecutionState.builder() - .setPhase("phase") - .build()); + ILMHistoryItem record = ILMHistoryItem.success( + "index", + policyId, + timestamp, + 10L, + LifecycleExecutionState.builder().setPhase("phase").build() + ); AtomicInteger calledTimes = new AtomicInteger(0); client.setVerifier((action, request, listener) -> { @@ -134,11 +149,18 @@ public void testPut() throws Exception { // The content of this BulkResponse doesn't matter, so just make it have the same number of responses int responses = bulkRequest.numberOfActions(); - return new BulkResponse(IntStream.range(0, responses) - .mapToObj(i -> BulkItemResponse.success(i, DocWriteRequest.OpType.INDEX, - new IndexResponse(new ShardId("index", "uuid", 0), randomAlphaOfLength(10), 1, 1, 1, true))) - .toArray(BulkItemResponse[]::new), - 1000L); + return new BulkResponse( + IntStream.range(0, responses) + .mapToObj( + i -> BulkItemResponse.success( + i, + DocWriteRequest.OpType.INDEX, + new IndexResponse(new ShardId("index", "uuid", 0), randomAlphaOfLength(10), 1, 1, 1, true) + ) + ) + .toArray(BulkItemResponse[]::new), + 1000L + ); }); historyStore.putAsync(record); @@ -148,10 +170,14 @@ public void testPut() throws Exception { { final String cause = randomAlphaOfLength(9); Exception failureException = new RuntimeException(cause); - ILMHistoryItem record = ILMHistoryItem.failure("index", policyId, timestamp, 10L, - LifecycleExecutionState.builder() - .setPhase("phase") - .build(), failureException); + ILMHistoryItem record = ILMHistoryItem.failure( + "index", + policyId, + timestamp, + 10L, + LifecycleExecutionState.builder().setPhase("phase").build(), + failureException + ); AtomicInteger calledTimes = new AtomicInteger(0); client.setVerifier((action, request, listener) -> { @@ -174,11 +200,18 @@ public void testPut() throws Exception { // The content of this BulkResponse doesn't matter, so just make it have the same number of responses with failures int responses = bulkRequest.numberOfActions(); - return new BulkResponse(IntStream.range(0, responses) - .mapToObj(i -> BulkItemResponse.failure(i, DocWriteRequest.OpType.INDEX, - new BulkItemResponse.Failure("index", i + "", failureException))) - .toArray(BulkItemResponse[]::new), - 1000L); + return new BulkResponse( + IntStream.range(0, responses) + .mapToObj( + i -> BulkItemResponse.failure( + i, + DocWriteRequest.OpType.INDEX, + new BulkItemResponse.Failure("index", i + "", failureException) + ) + ) + .toArray(BulkItemResponse[]::new), + 1000L + ); }); historyStore.putAsync(record); @@ -202,9 +235,11 @@ public static class VerifyingClient extends NoOpClient { @Override @SuppressWarnings("unchecked") - protected void doExecute(ActionType action, - Request request, - ActionListener listener) { + protected void doExecute( + ActionType action, + Request request, + ActionListener listener + ) { try { listener.onResponse((Response) verifier.apply(action, request, listener)); } catch (Exception e) { diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/slm/SnapshotLifecyclePolicyTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/slm/SnapshotLifecyclePolicyTests.java index 1cf5b0f0622bb..7257b08e14d16 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/slm/SnapshotLifecyclePolicyTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/slm/SnapshotLifecyclePolicyTests.java @@ -11,8 +11,8 @@ import org.elasticsearch.common.ValidationException; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.slm.SnapshotLifecyclePolicy; import org.elasticsearch.xpack.core.slm.SnapshotLifecyclePolicyMetadataTests; import org.elasticsearch.xpack.core.slm.SnapshotRetentionConfiguration; @@ -33,8 +33,14 @@ public class SnapshotLifecyclePolicyTests extends AbstractSerializingTestCase", - "* * * * * L", " ", Collections.emptyMap(), SnapshotRetentionConfiguration.EMPTY); + SnapshotLifecyclePolicy policy = new SnapshotLifecyclePolicy( + "a,b", + "", + "* * * * * L", + " ", + Collections.emptyMap(), + SnapshotRetentionConfiguration.EMPTY + ); ValidationException e = policy.validate(); - assertThat(e.validationErrors(), + assertThat( + e.validationErrors(), containsInAnyOrder( "invalid policy id [a,b]: must not contain the following characters [ , \", *, \\, <, |, ,, >, /, ?]", - "invalid snapshot name []: must not contain contain" + - " the following characters [ , \", *, \\, <, |, ,, >, /, ?]", + "invalid snapshot name []: must not contain contain" + + " the following characters [ , \", *, \\, <, |, ,, >, /, ?]", "invalid repository name [ ]: cannot be empty", - "invalid schedule: invalid cron expression [* * * * * L]")); + "invalid schedule: invalid cron expression [* * * * * L]" + ) + ); } { - SnapshotLifecyclePolicy policy = new SnapshotLifecyclePolicy("_my_policy", "mySnap", - " ", "repo", Collections.emptyMap(), SnapshotRetentionConfiguration.EMPTY); + SnapshotLifecyclePolicy policy = new SnapshotLifecyclePolicy( + "_my_policy", + "mySnap", + " ", + "repo", + Collections.emptyMap(), + SnapshotRetentionConfiguration.EMPTY + ); ValidationException e = policy.validate(); - assertThat(e.validationErrors(), - containsInAnyOrder("invalid policy id [_my_policy]: must not start with '_'", + assertThat( + e.validationErrors(), + containsInAnyOrder( + "invalid policy id [_my_policy]: must not start with '_'", "invalid snapshot name [mySnap]: must be lowercase", - "invalid schedule [ ]: must not be empty")); + "invalid schedule [ ]: must not be empty" + ) + ); } { - SnapshotLifecyclePolicy policy = new SnapshotLifecyclePolicy("my_policy", "my_snap", - "0 0/30 * * * ?", "repo", Collections.emptyMap(), SnapshotRetentionConfiguration.EMPTY); + SnapshotLifecyclePolicy policy = new SnapshotLifecyclePolicy( + "my_policy", + "my_snap", + "0 0/30 * * * ?", + "repo", + Collections.emptyMap(), + SnapshotRetentionConfiguration.EMPTY + ); ValidationException e = policy.validate(); assertThat(e, nullValue()); } @@ -109,11 +165,19 @@ public void testMetadataValidation() { final String metadataString = randomAlphaOfLength(10); configuration.put("metadata", metadataString); - SnapshotLifecyclePolicy policy = new SnapshotLifecyclePolicy("mypolicy", "", - "1 * * * * ?", "myrepo", configuration, SnapshotRetentionConfiguration.EMPTY); + SnapshotLifecyclePolicy policy = new SnapshotLifecyclePolicy( + "mypolicy", + "", + "1 * * * * ?", + "myrepo", + configuration, + SnapshotRetentionConfiguration.EMPTY + ); ValidationException e = policy.validate(); - assertThat(e.validationErrors(), contains("invalid configuration.metadata [" + metadataString + - "]: must be an object if present")); + assertThat( + e.validationErrors(), + contains("invalid configuration.metadata [" + metadataString + "]: must be an object if present") + ); } { @@ -122,11 +186,19 @@ public void testMetadataValidation() { Map configuration = new HashMap<>(); configuration.put("metadata", metadata); - SnapshotLifecyclePolicy policy = new SnapshotLifecyclePolicy("mypolicy", "", - "1 * * * * ?", "myrepo", configuration, SnapshotRetentionConfiguration.EMPTY); + SnapshotLifecyclePolicy policy = new SnapshotLifecyclePolicy( + "mypolicy", + "", + "1 * * * * ?", + "myrepo", + configuration, + SnapshotRetentionConfiguration.EMPTY + ); ValidationException e = policy.validate(); - assertThat(e.validationErrors(), contains("invalid configuration.metadata: field name [policy] is reserved and " + - "will be added automatically")); + assertThat( + e.validationErrors(), + contains("invalid configuration.metadata: field name [policy] is reserved and " + "will be added automatically") + ); } { @@ -136,17 +208,27 @@ public void testMetadataValidation() { final int valueBytes = 4; // chosen arbitrarily int totalBytes = fieldCount * (keyBytes + valueBytes + 6 /* bytes of overhead per key/value pair */) + 1; for (int i = 0; i < fieldCount; i++) { - metadata.put(randomValueOtherThanMany(key -> "policy".equals(key) || metadata.containsKey(key), - () -> randomAlphaOfLength(keyBytes)), randomAlphaOfLength(valueBytes)); + metadata.put( + randomValueOtherThanMany(key -> "policy".equals(key) || metadata.containsKey(key), () -> randomAlphaOfLength(keyBytes)), + randomAlphaOfLength(valueBytes) + ); } Map configuration = new HashMap<>(); configuration.put("metadata", metadata); - SnapshotLifecyclePolicy policy = new SnapshotLifecyclePolicy("mypolicy", "", - "1 * * * * ?", "myrepo", configuration, SnapshotRetentionConfiguration.EMPTY); + SnapshotLifecyclePolicy policy = new SnapshotLifecyclePolicy( + "mypolicy", + "", + "1 * * * * ?", + "myrepo", + configuration, + SnapshotRetentionConfiguration.EMPTY + ); ValidationException e = policy.validate(); - assertThat(e.validationErrors(), contains("invalid configuration.metadata: must be smaller than [1004] bytes, but is [" + - totalBytes + "] bytes")); + assertThat( + e.validationErrors(), + contains("invalid configuration.metadata: must be smaller than [1004] bytes, but is [" + totalBytes + "] bytes") + ); } } @@ -165,51 +247,63 @@ protected SnapshotLifecyclePolicy createTestInstance() { protected SnapshotLifecyclePolicy mutateInstance(SnapshotLifecyclePolicy instance) { switch (between(0, 5)) { case 0: - return new SnapshotLifecyclePolicy(instance.getId() + randomAlphaOfLength(2), + return new SnapshotLifecyclePolicy( + instance.getId() + randomAlphaOfLength(2), instance.getName(), instance.getSchedule(), instance.getRepository(), instance.getConfig(), - instance.getRetentionPolicy()); + instance.getRetentionPolicy() + ); case 1: - return new SnapshotLifecyclePolicy(instance.getId(), + return new SnapshotLifecyclePolicy( + instance.getId(), instance.getName() + randomAlphaOfLength(2), instance.getSchedule(), instance.getRepository(), instance.getConfig(), - instance.getRetentionPolicy()); + instance.getRetentionPolicy() + ); case 2: - return new SnapshotLifecyclePolicy(instance.getId(), + return new SnapshotLifecyclePolicy( + instance.getId(), instance.getName(), randomValueOtherThan(instance.getSchedule(), SnapshotLifecyclePolicyMetadataTests::randomSchedule), instance.getRepository(), instance.getConfig(), - instance.getRetentionPolicy()); + instance.getRetentionPolicy() + ); case 3: - return new SnapshotLifecyclePolicy(instance.getId(), + return new SnapshotLifecyclePolicy( + instance.getId(), instance.getName(), instance.getSchedule(), instance.getRepository() + randomAlphaOfLength(2), instance.getConfig(), - instance.getRetentionPolicy()); + instance.getRetentionPolicy() + ); case 4: Map newConfig = new HashMap<>(); for (int i = 0; i < randomIntBetween(2, 5); i++) { newConfig.put(randomAlphaOfLength(3), randomAlphaOfLength(3)); } - return new SnapshotLifecyclePolicy(instance.getId(), + return new SnapshotLifecyclePolicy( + instance.getId(), instance.getName() + randomAlphaOfLength(2), instance.getSchedule(), instance.getRepository(), newConfig, - instance.getRetentionPolicy()); + instance.getRetentionPolicy() + ); case 5: - return new SnapshotLifecyclePolicy(instance.getId(), + return new SnapshotLifecyclePolicy( + instance.getId(), instance.getName(), instance.getSchedule(), instance.getRepository(), instance.getConfig(), - randomValueOtherThan(instance.getRetentionPolicy(), SnapshotLifecyclePolicyMetadataTests::randomRetention)); + randomValueOtherThan(instance.getRetentionPolicy(), SnapshotLifecyclePolicyMetadataTests::randomRetention) + ); default: throw new AssertionError("failure, got illegal switch case"); } diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/slm/SnapshotLifecycleServiceTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/slm/SnapshotLifecycleServiceTests.java index 4acafb488f8a4..1f6517af07ef9 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/slm/SnapshotLifecycleServiceTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/slm/SnapshotLifecycleServiceTests.java @@ -69,16 +69,17 @@ public void testGetJobId() { public void testRepositoryExistenceForExistingRepo() { ClusterState state = ClusterState.builder(new ClusterName("cluster")).build(); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> SnapshotLifecycleService.validateRepositoryExists("repo", state)); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> SnapshotLifecycleService.validateRepositoryExists("repo", state) + ); assertThat(e.getMessage(), containsString("no such repository [repo]")); RepositoryMetadata repo = new RepositoryMetadata("repo", "fs", Settings.EMPTY); RepositoriesMetadata repoMeta = new RepositoriesMetadata(Collections.singletonList(repo)); ClusterState stateWithRepo = ClusterState.builder(state) - .metadata(Metadata.builder() - .putCustom(RepositoriesMetadata.TYPE, repoMeta)) + .metadata(Metadata.builder().putCustom(RepositoriesMetadata.TYPE, repoMeta)) .build(); SnapshotLifecycleService.validateRepositoryExists("repo", stateWithRepo); @@ -87,8 +88,10 @@ public void testRepositoryExistenceForExistingRepo() { public void testRepositoryExistenceForMissingRepo() { ClusterState state = ClusterState.builder(new ClusterName("cluster")).build(); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> SnapshotLifecycleService.validateRepositoryExists("repo", state)); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> SnapshotLifecycleService.validateRepositoryExists("repo", state) + ); assertThat(e.getMessage(), containsString("no such repository [repo]")); } @@ -101,13 +104,23 @@ public void testNothingScheduledWhenNotRunning() throws InterruptedException { .setVersion(1) .setModifiedDate(1) .build(); - ClusterState initialState = createState(new SnapshotLifecycleMetadata( - Collections.singletonMap(initialPolicy.getPolicy().getId(), initialPolicy), - OperationMode.RUNNING, new SnapshotLifecycleStats())); + ClusterState initialState = createState( + new SnapshotLifecycleMetadata( + Collections.singletonMap(initialPolicy.getPolicy().getId(), initialPolicy), + OperationMode.RUNNING, + new SnapshotLifecycleStats() + ) + ); ThreadPool threadPool = new TestThreadPool("test"); - try (ClusterService clusterService = ClusterServiceUtils.createClusterService(initialState, threadPool); - SnapshotLifecycleService sls = new SnapshotLifecycleService(Settings.EMPTY, - () -> new FakeSnapshotTask(e -> logger.info("triggered")), clusterService, clock)) { + try ( + ClusterService clusterService = ClusterServiceUtils.createClusterService(initialState, threadPool); + SnapshotLifecycleService sls = new SnapshotLifecycleService( + Settings.EMPTY, + () -> new FakeSnapshotTask(e -> logger.info("triggered")), + clusterService, + clock + ) + ) { sls.init(); SnapshotLifecyclePolicyMetadata newPolicy = SnapshotLifecyclePolicyMetadata.builder() @@ -118,10 +131,10 @@ public void testNothingScheduledWhenNotRunning() throws InterruptedException { .build(); Map policies = new HashMap<>(); policies.put(newPolicy.getPolicy().getId(), newPolicy); - ClusterState emptyState = - createState(new SnapshotLifecycleMetadata(Collections.emptyMap(), OperationMode.RUNNING, new SnapshotLifecycleStats())); - ClusterState state = - createState(new SnapshotLifecycleMetadata(policies, OperationMode.RUNNING, new SnapshotLifecycleStats())); + ClusterState emptyState = createState( + new SnapshotLifecycleMetadata(Collections.emptyMap(), OperationMode.RUNNING, new SnapshotLifecycleStats()) + ); + ClusterState state = createState(new SnapshotLifecycleMetadata(policies, OperationMode.RUNNING, new SnapshotLifecycleStats())); sls.clusterChanged(new ClusterChangedEvent("1", state, emptyState)); @@ -168,12 +181,21 @@ public void testPolicyCRUD() throws Exception { final AtomicInteger triggerCount = new AtomicInteger(0); final AtomicReference> trigger = new AtomicReference<>(e -> triggerCount.incrementAndGet()); ThreadPool threadPool = new TestThreadPool("test"); - try (ClusterService clusterService = ClusterServiceUtils.createClusterService(threadPool); - SnapshotLifecycleService sls = new SnapshotLifecycleService(Settings.EMPTY, - () -> new FakeSnapshotTask(e -> trigger.get().accept(e)), clusterService, clock)) { + try ( + ClusterService clusterService = ClusterServiceUtils.createClusterService(threadPool); + SnapshotLifecycleService sls = new SnapshotLifecycleService( + Settings.EMPTY, + () -> new FakeSnapshotTask(e -> trigger.get().accept(e)), + clusterService, + clock + ) + ) { sls.init(); - SnapshotLifecycleMetadata snapMeta = - new SnapshotLifecycleMetadata(Collections.emptyMap(), OperationMode.RUNNING, new SnapshotLifecycleStats()); + SnapshotLifecycleMetadata snapMeta = new SnapshotLifecycleMetadata( + Collections.emptyMap(), + OperationMode.RUNNING, + new SnapshotLifecycleStats() + ); ClusterState state = createState(snapMeta, false); sls.clusterChanged(new ClusterChangedEvent("1", state, ClusterState.EMPTY_STATE)); @@ -189,9 +211,7 @@ public void testPolicyCRUD() throws Exception { ClusterState previousState = state; state = createState(snapMeta, false); ClusterChangedEvent event = new ClusterChangedEvent("2", state, previousState); - trigger.set(e -> { - fail("trigger should not be invoked"); - }); + trigger.set(e -> { fail("trigger should not be invoked"); }); sls.clusterChanged(event); // Since the service does not think it is master, it should not be triggered or scheduled @@ -242,7 +262,9 @@ public void testPolicyCRUD() throws Exception { previousState = state; // Create a state simulating the policy being deleted state = createState( - new SnapshotLifecycleMetadata(Collections.emptyMap(), OperationMode.RUNNING, new SnapshotLifecycleStats()), true); + new SnapshotLifecycleMetadata(Collections.emptyMap(), OperationMode.RUNNING, new SnapshotLifecycleStats()), + true + ); event = new ClusterChangedEvent("5", state, previousState); sls.clusterChanged(event); clock.fastForwardSeconds(2); @@ -291,12 +313,21 @@ public void testPolicyNamesEndingInNumbers() throws Exception { final AtomicInteger triggerCount = new AtomicInteger(0); final AtomicReference> trigger = new AtomicReference<>(e -> triggerCount.incrementAndGet()); ThreadPool threadPool = new TestThreadPool("test"); - try (ClusterService clusterService = ClusterServiceUtils.createClusterService(threadPool); - SnapshotLifecycleService sls = new SnapshotLifecycleService(Settings.EMPTY, - () -> new FakeSnapshotTask(e -> trigger.get().accept(e)), clusterService, clock)) { + try ( + ClusterService clusterService = ClusterServiceUtils.createClusterService(threadPool); + SnapshotLifecycleService sls = new SnapshotLifecycleService( + Settings.EMPTY, + () -> new FakeSnapshotTask(e -> trigger.get().accept(e)), + clusterService, + clock + ) + ) { sls.init(); - SnapshotLifecycleMetadata snapMeta = - new SnapshotLifecycleMetadata(Collections.emptyMap(), OperationMode.RUNNING, new SnapshotLifecycleStats()); + SnapshotLifecycleMetadata snapMeta = new SnapshotLifecycleMetadata( + Collections.emptyMap(), + OperationMode.RUNNING, + new SnapshotLifecycleStats() + ); ClusterState state = createState(snapMeta, true); ClusterChangedEvent event = new ClusterChangedEvent("1", state, ClusterState.EMPTY_STATE); sls.clusterChanged(event); @@ -348,26 +379,30 @@ public void testValidateMinimumInterval() { ClusterState defaultState = ClusterState.builder(new ClusterName("cluster")).build(); ClusterState validationOneMinuteState = ClusterState.builder(new ClusterName("cluster")) - .metadata(Metadata.builder().persistentSettings( - Settings.builder() - .put(defaultState.metadata().persistentSettings()) - .put(LifecycleSettings.SLM_MINIMUM_INTERVAL, TimeValue.timeValueMinutes(1)) - .build())) + .metadata( + Metadata.builder() + .persistentSettings( + Settings.builder() + .put(defaultState.metadata().persistentSettings()) + .put(LifecycleSettings.SLM_MINIMUM_INTERVAL, TimeValue.timeValueMinutes(1)) + .build() + ) + ) .build(); ClusterState validationDisabledState = ClusterState.builder(new ClusterName("cluster")) - .metadata(Metadata.builder().persistentSettings( - Settings.builder() - .put(defaultState.metadata().persistentSettings()) - .put(LifecycleSettings.SLM_MINIMUM_INTERVAL, TimeValue.ZERO) - .build())) + .metadata( + Metadata.builder() + .persistentSettings( + Settings.builder() + .put(defaultState.metadata().persistentSettings()) + .put(LifecycleSettings.SLM_MINIMUM_INTERVAL, TimeValue.ZERO) + .build() + ) + ) .build(); - for (String schedule : List.of( - "0 0/15 * * * ?", - "0 0 1 * * ?", - "0 0 0 1 1 ? 2099" /* once */, - "* * * 31 FEB ? *" /* never */)) { + for (String schedule : List.of("0 0/15 * * * ?", "0 0 1 * * ?", "0 0 0 1 1 ? 2099" /* once */, "* * * 31 FEB ? *" /* never */)) { SnapshotLifecycleService.validateMinimumInterval(createPolicy("foo-1", schedule), defaultState); SnapshotLifecycleService.validateMinimumInterval(createPolicy("foo-1", schedule), validationOneMinuteState); SnapshotLifecycleService.validateMinimumInterval(createPolicy("foo-1", schedule), validationDisabledState); @@ -375,16 +410,24 @@ public void testValidateMinimumInterval() { IllegalArgumentException e; - e = expectThrows(IllegalArgumentException.class, - () -> SnapshotLifecycleService.validateMinimumInterval(createPolicy("foo-1", "0 0/1 * * * ?"), defaultState)); - assertThat(e.getMessage(), equalTo("invalid schedule [0 0/1 * * * ?]: " + - "schedule would be too frequent, executing more than every [15m]")); + e = expectThrows( + IllegalArgumentException.class, + () -> SnapshotLifecycleService.validateMinimumInterval(createPolicy("foo-1", "0 0/1 * * * ?"), defaultState) + ); + assertThat( + e.getMessage(), + equalTo("invalid schedule [0 0/1 * * * ?]: " + "schedule would be too frequent, executing more than every [15m]") + ); SnapshotLifecycleService.validateMinimumInterval(createPolicy("foo-1", "0 0/1 * * * ?"), validationOneMinuteState); - e = expectThrows(IllegalArgumentException.class, - () -> SnapshotLifecycleService.validateMinimumInterval(createPolicy("foo-1", "0/30 0/1 * * * ?"), validationOneMinuteState)); - assertThat(e.getMessage(), equalTo("invalid schedule [0/30 0/1 * * * ?]: " + - "schedule would be too frequent, executing more than every [1m]")); + e = expectThrows( + IllegalArgumentException.class, + () -> SnapshotLifecycleService.validateMinimumInterval(createPolicy("foo-1", "0/30 0/1 * * * ?"), validationOneMinuteState) + ); + assertThat( + e.getMessage(), + equalTo("invalid schedule [0/30 0/1 * * * ?]: " + "schedule would be too frequent, executing more than every [1m]") + ); SnapshotLifecycleService.validateMinimumInterval(createPolicy("foo-1", "0/30 0/1 * * * ?"), validationDisabledState); } @@ -408,18 +451,13 @@ public ClusterState createState(SnapshotLifecycleMetadata snapMeta) { } public ClusterState createState(SnapshotLifecycleMetadata snapMeta, boolean localNodeMaster) { - Metadata metadata = Metadata.builder() - .putCustom(SnapshotLifecycleMetadata.TYPE, snapMeta) - .build(); + Metadata metadata = Metadata.builder().putCustom(SnapshotLifecycleMetadata.TYPE, snapMeta).build(); final DiscoveryNodes.Builder discoveryNodesBuilder = DiscoveryNodes.builder() .add(DiscoveryNode.createLocal(Settings.EMPTY, new TransportAddress(TransportAddress.META_ADDRESS, 9300), "local")) .add(new DiscoveryNode("remote", new TransportAddress(TransportAddress.META_ADDRESS, 9301), Version.CURRENT)) .localNodeId("local") .masterNodeId(localNodeMaster ? "local" : "remote"); - return ClusterState.builder(new ClusterName("cluster")) - .nodes(discoveryNodesBuilder) - .metadata(metadata) - .build(); + return ClusterState.builder(new ClusterName("cluster")).nodes(discoveryNodesBuilder).metadata(metadata).build(); } public static SnapshotLifecyclePolicy createPolicy(String id) { @@ -433,13 +471,17 @@ public static SnapshotLifecyclePolicy createPolicy(String id, String schedule) { indices.add("foo-*"); indices.add(randomAlphaOfLength(4)); config.put("indices", indices); - return new SnapshotLifecyclePolicy(id, randomAlphaOfLength(4), schedule, randomAlphaOfLength(4), config, - SnapshotRetentionConfiguration.EMPTY); + return new SnapshotLifecyclePolicy( + id, + randomAlphaOfLength(4), + schedule, + randomAlphaOfLength(4), + config, + SnapshotRetentionConfiguration.EMPTY + ); } public static String randomSchedule() { - return randomIntBetween(0, 59) + " " + - randomIntBetween(0, 59) + " " + - randomIntBetween(0, 12) + " * * ?"; + return randomIntBetween(0, 59) + " " + randomIntBetween(0, 59) + " " + randomIntBetween(0, 12) + " * * ?"; } } diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/slm/SnapshotLifecycleTaskTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/slm/SnapshotLifecycleTaskTests.java index 415acf0101af6..be4e04fa9130e 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/slm/SnapshotLifecycleTaskTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/slm/SnapshotLifecycleTaskTests.java @@ -23,7 +23,6 @@ import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.TriFunction; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.snapshots.Snapshot; import org.elasticsearch.snapshots.SnapshotId; @@ -34,6 +33,7 @@ import org.elasticsearch.test.client.NoOpClient; import org.elasticsearch.threadpool.TestThreadPool; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.ilm.OperationMode; import org.elasticsearch.xpack.core.scheduler.SchedulerEngine; import org.elasticsearch.xpack.core.slm.SnapshotLifecycleMetadata; @@ -64,17 +64,20 @@ public class SnapshotLifecycleTaskTests extends ESTestCase { public void testGetSnapMetadata() { final String id = randomAlphaOfLength(4); final SnapshotLifecyclePolicyMetadata slpm = makePolicyMeta(id); - final SnapshotLifecycleMetadata meta = - new SnapshotLifecycleMetadata(Collections.singletonMap(id, slpm), OperationMode.RUNNING, new SnapshotLifecycleStats()); + final SnapshotLifecycleMetadata meta = new SnapshotLifecycleMetadata( + Collections.singletonMap(id, slpm), + OperationMode.RUNNING, + new SnapshotLifecycleStats() + ); final ClusterState state = ClusterState.builder(new ClusterName("test")) - .metadata(Metadata.builder() - .putCustom(SnapshotLifecycleMetadata.TYPE, meta) - .build()) + .metadata(Metadata.builder().putCustom(SnapshotLifecycleMetadata.TYPE, meta).build()) .build(); - final Optional o = - SnapshotLifecycleTask.getSnapPolicyMetadata(SnapshotLifecycleService.getJobId(slpm), state); + final Optional o = SnapshotLifecycleTask.getSnapPolicyMetadata( + SnapshotLifecycleService.getJobId(slpm), + state + ); assertTrue("the policy metadata should be retrieved from the cluster state", o.isPresent()); assertThat(o.get(), equalTo(slpm)); @@ -85,23 +88,29 @@ public void testGetSnapMetadata() { public void testSkipCreatingSnapshotWhenJobDoesNotMatch() { final String id = randomAlphaOfLength(4); final SnapshotLifecyclePolicyMetadata slpm = makePolicyMeta(id); - final SnapshotLifecycleMetadata meta = - new SnapshotLifecycleMetadata(Collections.singletonMap(id, slpm), OperationMode.RUNNING, new SnapshotLifecycleStats()); + final SnapshotLifecycleMetadata meta = new SnapshotLifecycleMetadata( + Collections.singletonMap(id, slpm), + OperationMode.RUNNING, + new SnapshotLifecycleStats() + ); final ClusterState state = ClusterState.builder(new ClusterName("test")) - .metadata(Metadata.builder() - .putCustom(SnapshotLifecycleMetadata.TYPE, meta) - .build()) + .metadata(Metadata.builder().putCustom(SnapshotLifecycleMetadata.TYPE, meta).build()) .build(); final ThreadPool threadPool = new TestThreadPool("test"); - try (ClusterService clusterService = ClusterServiceUtils.createClusterService(state, threadPool); - VerifyingClient client = new VerifyingClient(threadPool, (a, r, l) -> { - fail("should not have tried to take a snapshot"); - return null; - })) { - SnapshotHistoryStore historyStore = new VerifyingHistoryStore(null, ZoneOffset.UTC, - item -> fail("should not have tried to store an item")); + try ( + ClusterService clusterService = ClusterServiceUtils.createClusterService(state, threadPool); + VerifyingClient client = new VerifyingClient(threadPool, (a, r, l) -> { + fail("should not have tried to take a snapshot"); + return null; + }) + ) { + SnapshotHistoryStore historyStore = new VerifyingHistoryStore( + null, + ZoneOffset.UTC, + item -> fail("should not have tried to store an item") + ); SnapshotLifecycleTask task = new SnapshotLifecycleTask(client, clusterService, historyStore); @@ -116,86 +125,92 @@ public void testSkipCreatingSnapshotWhenJobDoesNotMatch() { public void testCreateSnapshotOnTrigger() { final String id = randomAlphaOfLength(4); final SnapshotLifecyclePolicyMetadata slpm = makePolicyMeta(id); - final SnapshotLifecycleMetadata meta = - new SnapshotLifecycleMetadata(Collections.singletonMap(id, slpm), OperationMode.RUNNING, new SnapshotLifecycleStats()); + final SnapshotLifecycleMetadata meta = new SnapshotLifecycleMetadata( + Collections.singletonMap(id, slpm), + OperationMode.RUNNING, + new SnapshotLifecycleStats() + ); final ClusterState state = ClusterState.builder(new ClusterName("test")) - .metadata(Metadata.builder() - .putCustom(SnapshotLifecycleMetadata.TYPE, meta) - .build()) + .metadata(Metadata.builder().putCustom(SnapshotLifecycleMetadata.TYPE, meta).build()) .build(); final ThreadPool threadPool = new TestThreadPool("test"); - final String createSnapResponse = "{" + - " \"snapshot\" : {" + - " \"snapshot\" : \"snapshot_1\"," + - " \"uuid\" : \"bcP3ClgCSYO_TP7_FCBbBw\"," + - " \"version_id\" : " + Version.CURRENT.id + "," + - " \"version\" : \"" + Version.CURRENT + "\"," + - " \"indices\" : [ ]," + - " \"include_global_state\" : true," + - " \"state\" : \"SUCCESS\"," + - " \"start_time\" : \"2019-03-19T22:19:53.542Z\"," + - " \"start_time_in_millis\" : 1553033993542," + - " \"end_time\" : \"2019-03-19T22:19:53.567Z\"," + - " \"end_time_in_millis\" : 1553033993567," + - " \"duration_in_millis\" : 25," + - " \"failures\" : [ ]," + - " \"shards\" : {" + - " \"total\" : 0," + - " \"failed\" : 0," + - " \"successful\" : 0" + - " }" + - " }" + - "}"; + final String createSnapResponse = "{" + + " \"snapshot\" : {" + + " \"snapshot\" : \"snapshot_1\"," + + " \"uuid\" : \"bcP3ClgCSYO_TP7_FCBbBw\"," + + " \"version_id\" : " + + Version.CURRENT.id + + "," + + " \"version\" : \"" + + Version.CURRENT + + "\"," + + " \"indices\" : [ ]," + + " \"include_global_state\" : true," + + " \"state\" : \"SUCCESS\"," + + " \"start_time\" : \"2019-03-19T22:19:53.542Z\"," + + " \"start_time_in_millis\" : 1553033993542," + + " \"end_time\" : \"2019-03-19T22:19:53.567Z\"," + + " \"end_time_in_millis\" : 1553033993567," + + " \"duration_in_millis\" : 25," + + " \"failures\" : [ ]," + + " \"shards\" : {" + + " \"total\" : 0," + + " \"failed\" : 0," + + " \"successful\" : 0" + + " }" + + " }" + + "}"; final AtomicBoolean clientCalled = new AtomicBoolean(false); final SetOnce snapshotName = new SetOnce<>(); - try (ClusterService clusterService = ClusterServiceUtils.createClusterService(state, threadPool); - // This verifying client will verify that we correctly invoked - // client.admin().createSnapshot(...) with the appropriate - // request. It also returns a mock real response - VerifyingClient client = new VerifyingClient(threadPool, - (action, request, listener) -> { - assertFalse(clientCalled.getAndSet(true)); - assertThat(action, instanceOf(CreateSnapshotAction.class)); - assertThat(request, instanceOf(CreateSnapshotRequest.class)); - - CreateSnapshotRequest req = (CreateSnapshotRequest) request; - - SnapshotLifecyclePolicy policy = slpm.getPolicy(); - assertThat(req.snapshot(), startsWith(policy.getName() + "-")); - assertThat(req.repository(), equalTo(policy.getRepository())); - snapshotName.set(req.snapshot()); - if (req.indices().length > 0) { - assertThat(Arrays.asList(req.indices()), equalTo(policy.getConfig().get("indices"))); - } - boolean globalState = policy.getConfig().get("include_global_state") == null || - Boolean.parseBoolean((String) policy.getConfig().get("include_global_state")); - assertThat(req.includeGlobalState(), equalTo(globalState)); - - try { - return CreateSnapshotResponse.fromXContent(createParser(JsonXContent.jsonXContent, createSnapResponse)); - } catch (IOException e) { - fail("failed to parse snapshot response"); - return null; - } - })) { + try ( + ClusterService clusterService = ClusterServiceUtils.createClusterService(state, threadPool); + // This verifying client will verify that we correctly invoked + // client.admin().createSnapshot(...) with the appropriate + // request. It also returns a mock real response + VerifyingClient client = new VerifyingClient(threadPool, (action, request, listener) -> { + assertFalse(clientCalled.getAndSet(true)); + assertThat(action, instanceOf(CreateSnapshotAction.class)); + assertThat(request, instanceOf(CreateSnapshotRequest.class)); + + CreateSnapshotRequest req = (CreateSnapshotRequest) request; + + SnapshotLifecyclePolicy policy = slpm.getPolicy(); + assertThat(req.snapshot(), startsWith(policy.getName() + "-")); + assertThat(req.repository(), equalTo(policy.getRepository())); + snapshotName.set(req.snapshot()); + if (req.indices().length > 0) { + assertThat(Arrays.asList(req.indices()), equalTo(policy.getConfig().get("indices"))); + } + boolean globalState = policy.getConfig().get("include_global_state") == null + || Boolean.parseBoolean((String) policy.getConfig().get("include_global_state")); + assertThat(req.includeGlobalState(), equalTo(globalState)); + + try { + return CreateSnapshotResponse.fromXContent(createParser(JsonXContent.jsonXContent, createSnapResponse)); + } catch (IOException e) { + fail("failed to parse snapshot response"); + return null; + } + }) + ) { final AtomicBoolean historyStoreCalled = new AtomicBoolean(false); - SnapshotHistoryStore historyStore = new VerifyingHistoryStore(null, ZoneOffset.UTC, - item -> { - assertFalse(historyStoreCalled.getAndSet(true)); - final SnapshotLifecyclePolicy policy = slpm.getPolicy(); - assertEquals(policy.getId(), item.getPolicyId()); - assertEquals(policy.getRepository(), item.getRepository()); - assertEquals(policy.getConfig(), item.getSnapshotConfiguration()); - assertEquals(snapshotName.get(), item.getSnapshotName()); - }); + SnapshotHistoryStore historyStore = new VerifyingHistoryStore(null, ZoneOffset.UTC, item -> { + assertFalse(historyStoreCalled.getAndSet(true)); + final SnapshotLifecyclePolicy policy = slpm.getPolicy(); + assertEquals(policy.getId(), item.getPolicyId()); + assertEquals(policy.getRepository(), item.getRepository()); + assertEquals(policy.getConfig(), item.getSnapshotConfiguration()); + assertEquals(snapshotName.get(), item.getSnapshotName()); + }); SnapshotLifecycleTask task = new SnapshotLifecycleTask(client, clusterService, historyStore); // Trigger the event with a matching job name for the policy - task.triggered(new SchedulerEngine.Event(SnapshotLifecycleService.getJobId(slpm), - System.currentTimeMillis(), System.currentTimeMillis())); + task.triggered( + new SchedulerEngine.Event(SnapshotLifecycleService.getJobId(slpm), System.currentTimeMillis(), System.currentTimeMillis()) + ); assertTrue("snapshot should be triggered once", clientCalled.get()); assertTrue("history store should be called once", historyStoreCalled.get()); @@ -207,70 +222,80 @@ public void testCreateSnapshotOnTrigger() { public void testPartialFailureSnapshot() throws Exception { final String id = randomAlphaOfLength(4); final SnapshotLifecyclePolicyMetadata slpm = makePolicyMeta(id); - final SnapshotLifecycleMetadata meta = - new SnapshotLifecycleMetadata(Collections.singletonMap(id, slpm), OperationMode.RUNNING, new SnapshotLifecycleStats()); + final SnapshotLifecycleMetadata meta = new SnapshotLifecycleMetadata( + Collections.singletonMap(id, slpm), + OperationMode.RUNNING, + new SnapshotLifecycleStats() + ); final ClusterState state = ClusterState.builder(new ClusterName("test")) - .metadata(Metadata.builder() - .putCustom(SnapshotLifecycleMetadata.TYPE, meta) - .build()) + .metadata(Metadata.builder().putCustom(SnapshotLifecycleMetadata.TYPE, meta).build()) .build(); final ThreadPool threadPool = new TestThreadPool("test"); final AtomicBoolean clientCalled = new AtomicBoolean(false); final SetOnce snapshotName = new SetOnce<>(); - try (ClusterService clusterService = ClusterServiceUtils.createClusterService(state, threadPool); - VerifyingClient client = new VerifyingClient(threadPool, - (action, request, listener) -> { - assertFalse(clientCalled.getAndSet(true)); - assertThat(action, instanceOf(CreateSnapshotAction.class)); - assertThat(request, instanceOf(CreateSnapshotRequest.class)); - - CreateSnapshotRequest req = (CreateSnapshotRequest) request; - - SnapshotLifecyclePolicy policy = slpm.getPolicy(); - assertThat(req.snapshot(), startsWith(policy.getName() + "-")); - assertThat(req.repository(), equalTo(policy.getRepository())); - snapshotName.set(req.snapshot()); - if (req.indices().length > 0) { - assertThat(Arrays.asList(req.indices()), equalTo(policy.getConfig().get("indices"))); - } - boolean globalState = policy.getConfig().get("include_global_state") == null || - Boolean.parseBoolean((String) policy.getConfig().get("include_global_state")); - assertThat(req.includeGlobalState(), equalTo(globalState)); - - long startTime = randomNonNegativeLong(); - long endTime = randomLongBetween(startTime, Long.MAX_VALUE); - return new CreateSnapshotResponse( - new SnapshotInfo( - new Snapshot(req.repository(), new SnapshotId(req.snapshot(), "uuid")), - Arrays.asList(req.indices()), - Collections.emptyList(), - Collections.emptyList(), "snapshot started", endTime, 3, Collections.singletonList( - new SnapshotShardFailure("nodeId", new ShardId("index", "uuid", 0), "forced failure")), - req.includeGlobalState(), req.userMetadata(), startTime, Collections.emptyMap() - )); - })) { + try ( + ClusterService clusterService = ClusterServiceUtils.createClusterService(state, threadPool); + VerifyingClient client = new VerifyingClient(threadPool, (action, request, listener) -> { + assertFalse(clientCalled.getAndSet(true)); + assertThat(action, instanceOf(CreateSnapshotAction.class)); + assertThat(request, instanceOf(CreateSnapshotRequest.class)); + + CreateSnapshotRequest req = (CreateSnapshotRequest) request; + + SnapshotLifecyclePolicy policy = slpm.getPolicy(); + assertThat(req.snapshot(), startsWith(policy.getName() + "-")); + assertThat(req.repository(), equalTo(policy.getRepository())); + snapshotName.set(req.snapshot()); + if (req.indices().length > 0) { + assertThat(Arrays.asList(req.indices()), equalTo(policy.getConfig().get("indices"))); + } + boolean globalState = policy.getConfig().get("include_global_state") == null + || Boolean.parseBoolean((String) policy.getConfig().get("include_global_state")); + assertThat(req.includeGlobalState(), equalTo(globalState)); + + long startTime = randomNonNegativeLong(); + long endTime = randomLongBetween(startTime, Long.MAX_VALUE); + return new CreateSnapshotResponse( + new SnapshotInfo( + new Snapshot(req.repository(), new SnapshotId(req.snapshot(), "uuid")), + Arrays.asList(req.indices()), + Collections.emptyList(), + Collections.emptyList(), + "snapshot started", + endTime, + 3, + Collections.singletonList(new SnapshotShardFailure("nodeId", new ShardId("index", "uuid", 0), "forced failure")), + req.includeGlobalState(), + req.userMetadata(), + startTime, + Collections.emptyMap() + ) + ); + }) + ) { final AtomicBoolean historyStoreCalled = new AtomicBoolean(false); - SnapshotHistoryStore historyStore = new VerifyingHistoryStore(null, ZoneOffset.UTC, - item -> { - assertFalse(historyStoreCalled.getAndSet(true)); - final SnapshotLifecyclePolicy policy = slpm.getPolicy(); - assertEquals(policy.getId(), item.getPolicyId()); - assertEquals(policy.getRepository(), item.getRepository()); - assertEquals(policy.getConfig(), item.getSnapshotConfiguration()); - assertEquals(snapshotName.get(), item.getSnapshotName()); - assertFalse("item should be a failure", item.isSuccess()); - assertThat(item.getErrorDetails(), - containsString("failed to create snapshot successfully, 1 out of 3 total shards failed")); - assertThat(item.getErrorDetails(), - containsString("forced failure")); - }); + SnapshotHistoryStore historyStore = new VerifyingHistoryStore(null, ZoneOffset.UTC, item -> { + assertFalse(historyStoreCalled.getAndSet(true)); + final SnapshotLifecyclePolicy policy = slpm.getPolicy(); + assertEquals(policy.getId(), item.getPolicyId()); + assertEquals(policy.getRepository(), item.getRepository()); + assertEquals(policy.getConfig(), item.getSnapshotConfiguration()); + assertEquals(snapshotName.get(), item.getSnapshotName()); + assertFalse("item should be a failure", item.isSuccess()); + assertThat( + item.getErrorDetails(), + containsString("failed to create snapshot successfully, 1 out of 3 total shards failed") + ); + assertThat(item.getErrorDetails(), containsString("forced failure")); + }); SnapshotLifecycleTask task = new SnapshotLifecycleTask(client, clusterService, historyStore); // Trigger the event with a matching job name for the policy - task.triggered(new SchedulerEngine.Event(SnapshotLifecycleService.getJobId(slpm), - System.currentTimeMillis(), System.currentTimeMillis())); + task.triggered( + new SchedulerEngine.Event(SnapshotLifecycleService.getJobId(slpm), System.currentTimeMillis(), System.currentTimeMillis()) + ); assertTrue("snapshot should be triggered once", clientCalled.get()); assertTrue("history store should be called once", historyStoreCalled.get()); @@ -286,17 +311,18 @@ public static class VerifyingClient extends NoOpClient { private final TriFunction, ActionRequest, ActionListener, ActionResponse> verifier; - VerifyingClient(ThreadPool threadPool, - TriFunction, ActionRequest, ActionListener, ActionResponse> verifier) { + VerifyingClient(ThreadPool threadPool, TriFunction, ActionRequest, ActionListener, ActionResponse> verifier) { super(threadPool); this.verifier = verifier; } @Override @SuppressWarnings("unchecked") - protected void doExecute(ActionType action, - Request request, - ActionListener listener) { + protected void doExecute( + ActionType action, + Request request, + ActionListener listener + ) { listener.onResponse((Response) verifier.apply(action, request, listener)); } } @@ -305,12 +331,7 @@ private SnapshotLifecyclePolicyMetadata makePolicyMeta(final String id) { SnapshotLifecyclePolicy policy = SnapshotLifecycleServiceTests.createPolicy(id); Map headers = new HashMap<>(); headers.put("X-Opaque-ID", randomAlphaOfLength(4)); - return SnapshotLifecyclePolicyMetadata.builder() - .setPolicy(policy) - .setHeaders(headers) - .setVersion(1) - .setModifiedDate(1) - .build(); + return SnapshotLifecyclePolicyMetadata.builder().setPolicy(policy).setHeaders(headers).setVersion(1).setModifiedDate(1).build(); } public static class VerifyingHistoryStore extends SnapshotHistoryStore { diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/slm/SnapshotRetentionServiceTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/slm/SnapshotRetentionServiceTests.java index c94246b9e7fc6..151930827b08a 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/slm/SnapshotRetentionServiceTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/slm/SnapshotRetentionServiceTests.java @@ -45,13 +45,20 @@ public class SnapshotRetentionServiceTests extends ESTestCase { } public void testJobsAreScheduled() throws InterruptedException { - final DiscoveryNode discoveryNode = new DiscoveryNode("node", ESTestCase.buildNewFakeTransportAddress(), - Collections.emptyMap(), DiscoveryNodeRole.roles(), Version.CURRENT); + final DiscoveryNode discoveryNode = new DiscoveryNode( + "node", + ESTestCase.buildNewFakeTransportAddress(), + Collections.emptyMap(), + DiscoveryNodeRole.roles(), + Version.CURRENT + ); ClockMock clock = new ClockMock(); ThreadPool threadPool = new TestThreadPool("test"); - try (ClusterService clusterService = ClusterServiceUtils.createClusterService(threadPool, discoveryNode, clusterSettings); - SnapshotRetentionService service = new SnapshotRetentionService(Settings.EMPTY, FakeRetentionTask::new, clock)) { + try ( + ClusterService clusterService = ClusterServiceUtils.createClusterService(threadPool, discoveryNode, clusterSettings); + SnapshotRetentionService service = new SnapshotRetentionService(Settings.EMPTY, FakeRetentionTask::new, clock) + ) { service.init(clusterService); assertThat(service.getScheduler().jobCount(), equalTo(0)); @@ -78,18 +85,24 @@ public void testJobsAreScheduled() throws InterruptedException { } public void testManualTriggering() throws InterruptedException { - final DiscoveryNode discoveryNode = new DiscoveryNode("node", ESTestCase.buildNewFakeTransportAddress(), - Collections.emptyMap(), DiscoveryNodeRole.roles(), Version.CURRENT); + final DiscoveryNode discoveryNode = new DiscoveryNode( + "node", + ESTestCase.buildNewFakeTransportAddress(), + Collections.emptyMap(), + DiscoveryNodeRole.roles(), + Version.CURRENT + ); ClockMock clock = new ClockMock(); AtomicInteger invoked = new AtomicInteger(0); ThreadPool threadPool = new TestThreadPool("test"); - try (ClusterService clusterService = ClusterServiceUtils.createClusterService(threadPool, discoveryNode, clusterSettings); - SnapshotRetentionService service = new SnapshotRetentionService(Settings.EMPTY, - () -> new FakeRetentionTask(event -> { - assertThat(event.getJobName(), equalTo(SnapshotRetentionService.SLM_RETENTION_MANUAL_JOB_ID)); - invoked.incrementAndGet(); - }), clock)) { + try ( + ClusterService clusterService = ClusterServiceUtils.createClusterService(threadPool, discoveryNode, clusterSettings); + SnapshotRetentionService service = new SnapshotRetentionService(Settings.EMPTY, () -> new FakeRetentionTask(event -> { + assertThat(event.getJobName(), equalTo(SnapshotRetentionService.SLM_RETENTION_MANUAL_JOB_ID)); + invoked.incrementAndGet(); + }), clock) + ) { service.init(clusterService); service.onMaster(); service.triggerRetention(); diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/slm/SnapshotRetentionTaskTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/slm/SnapshotRetentionTaskTests.java index 4f10b8f8555df..5de5706a5c27e 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/slm/SnapshotRetentionTaskTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/slm/SnapshotRetentionTaskTests.java @@ -69,12 +69,30 @@ public class SnapshotRetentionTaskTests extends ESTestCase { public void testGetAllPoliciesWithRetentionEnabled() { - SnapshotLifecyclePolicy policyWithout = new SnapshotLifecyclePolicy("policyWithout", "snap", "1 * * * * ?", - "repo", null, SnapshotRetentionConfiguration.EMPTY); - SnapshotLifecyclePolicy policyWithout2 = new SnapshotLifecyclePolicy("policyWithout2", "snap", "1 * * * * ?", - "repo", null, new SnapshotRetentionConfiguration(null, null, null)); - SnapshotLifecyclePolicy policyWith = new SnapshotLifecyclePolicy("policyWith", "snap", "1 * * * * ?", - "repo", null, new SnapshotRetentionConfiguration(TimeValue.timeValueDays(30), null, null)); + SnapshotLifecyclePolicy policyWithout = new SnapshotLifecyclePolicy( + "policyWithout", + "snap", + "1 * * * * ?", + "repo", + null, + SnapshotRetentionConfiguration.EMPTY + ); + SnapshotLifecyclePolicy policyWithout2 = new SnapshotLifecyclePolicy( + "policyWithout2", + "snap", + "1 * * * * ?", + "repo", + null, + new SnapshotRetentionConfiguration(null, null, null) + ); + SnapshotLifecyclePolicy policyWith = new SnapshotLifecyclePolicy( + "policyWith", + "snap", + "1 * * * * ?", + "repo", + null, + new SnapshotRetentionConfiguration(TimeValue.timeValueDays(30), null, null) + ); // Test with no SLM metadata ClusterState state = ClusterState.builder(new ClusterName("cluster")).build(); @@ -82,8 +100,10 @@ public void testGetAllPoliciesWithRetentionEnabled() { // Test with empty SLM metadata Metadata metadata = Metadata.builder() - .putCustom(SnapshotLifecycleMetadata.TYPE, - new SnapshotLifecycleMetadata(Collections.emptyMap(), OperationMode.RUNNING, new SnapshotLifecycleStats())) + .putCustom( + SnapshotLifecycleMetadata.TYPE, + new SnapshotLifecycleMetadata(Collections.emptyMap(), OperationMode.RUNNING, new SnapshotLifecycleStats()) + ) .build(); state = ClusterState.builder(new ClusterName("cluster")).metadata(metadata).build(); assertThat(SnapshotRetentionTask.getAllPoliciesWithRetentionEnabled(state), equalTo(Collections.emptyMap())); @@ -101,11 +121,19 @@ public void testGetAllPoliciesWithRetentionEnabled() { public void testSnapshotEligibleForDeletion() { final String repoName = "repo"; - SnapshotLifecyclePolicy policy = new SnapshotLifecyclePolicy("policy", "snap", "1 * * * * ?", - repoName, null, new SnapshotRetentionConfiguration(TimeValue.timeValueDays(30), null, null)); + SnapshotLifecyclePolicy policy = new SnapshotLifecyclePolicy( + "policy", + "snap", + "1 * * * * ?", + repoName, + null, + new SnapshotRetentionConfiguration(TimeValue.timeValueDays(30), null, null) + ); Map policyMap = Collections.singletonMap("policy", policy); - Function>> mkInfos = i -> - Collections.singletonMap(repoName, Collections.singletonList(i)); + Function>> mkInfos = i -> Collections.singletonMap( + repoName, + Collections.singletonList(i) + ); // Test with an ancient snapshot that should be expunged SnapshotInfo info = new SnapshotInfo( @@ -120,7 +148,8 @@ public void testSnapshotEligibleForDeletion() { true, Collections.singletonMap("policy", "policy"), 0L, - Collections.emptyMap()); + Collections.emptyMap() + ); assertThat(SnapshotRetentionTask.snapshotEligibleForDeletion(info, mkInfos.apply(info), policyMap), equalTo(true)); // Test with a snapshot that's start date is old enough to be expunged (but the finish date is not) @@ -137,7 +166,8 @@ public void testSnapshotEligibleForDeletion() { true, Collections.singletonMap("policy", "policy"), time, - Collections.emptyMap()); + Collections.emptyMap() + ); assertThat(SnapshotRetentionTask.snapshotEligibleForDeletion(info, mkInfos.apply(info), policyMap), equalTo(true)); // Test with a fresh snapshot that should not be expunged @@ -153,7 +183,8 @@ public void testSnapshotEligibleForDeletion() { true, Collections.singletonMap("policy", "policy"), System.currentTimeMillis(), - Collections.emptyMap()); + Collections.emptyMap() + ); assertThat(SnapshotRetentionTask.snapshotEligibleForDeletion(info, mkInfos.apply(info), policyMap), equalTo(false)); } @@ -167,21 +198,39 @@ public void testRetentionTaskFailure() throws Exception { private void retentionTaskTest(final boolean deletionSuccess) throws Exception { ThreadPool threadPool = new TestThreadPool("slm-test"); - try (ClusterService clusterService = ClusterServiceUtils.createClusterService(threadPool); - Client noOpClient = new NoOpClient("slm-test")) { + try ( + ClusterService clusterService = ClusterServiceUtils.createClusterService(threadPool); + Client noOpClient = new NoOpClient("slm-test") + ) { final String policyId = "policy"; final String repoId = "repo"; - SnapshotLifecyclePolicy policy = new SnapshotLifecyclePolicy(policyId, "snap", "1 * * * * ?", - repoId, null, new SnapshotRetentionConfiguration(TimeValue.timeValueDays(30), null, null)); + SnapshotLifecyclePolicy policy = new SnapshotLifecyclePolicy( + policyId, + "snap", + "1 * * * * ?", + repoId, + null, + new SnapshotRetentionConfiguration(TimeValue.timeValueDays(30), null, null) + ); ClusterState state = createState(policy); ClusterServiceUtils.setState(clusterService, state); final SnapshotInfo eligibleSnapshot = new SnapshotInfo( - new Snapshot(repoId, new SnapshotId("name", "uuid")), Collections.singletonList("index"), - Collections.emptyList(), Collections.emptyList(), null, 1L, 1, Collections.emptyList(), true, - Collections.singletonMap("policy", policyId), 0L, Collections.emptyMap()); + new Snapshot(repoId, new SnapshotId("name", "uuid")), + Collections.singletonList("index"), + Collections.emptyList(), + Collections.emptyList(), + null, + 1L, + 1, + Collections.emptyList(), + true, + Collections.singletonMap("policy", policyId), + 0L, + Collections.emptyMap() + ); final SnapshotInfo ineligibleSnapshot = new SnapshotInfo( new Snapshot(repoId, new SnapshotId("name2", "uuid2")), Collections.singletonList("index"), @@ -194,26 +243,28 @@ private void retentionTaskTest(final boolean deletionSuccess) throws Exception { true, Collections.singletonMap("policy", policyId), System.currentTimeMillis(), - Collections.emptyMap()); + Collections.emptyMap() + ); Set deleted = ConcurrentHashMap.newKeySet(); Set deletedSnapshotsInHistory = ConcurrentHashMap.newKeySet(); CountDownLatch deletionLatch = new CountDownLatch(1); CountDownLatch historyLatch = new CountDownLatch(1); - MockSnapshotRetentionTask retentionTask = new MockSnapshotRetentionTask(noOpClient, clusterService, - new SnapshotLifecycleTaskTests.VerifyingHistoryStore(noOpClient, ZoneOffset.UTC, - (historyItem) -> { - assertEquals(deletionSuccess, historyItem.isSuccess()); - if (historyItem.isSuccess() == false) { - assertThat(historyItem.getErrorDetails(), containsString("deletion_failed")); - } - assertEquals(policyId, historyItem.getPolicyId()); - assertEquals(repoId, historyItem.getRepository()); - assertEquals(DELETE_OPERATION, historyItem.getOperation()); - deletedSnapshotsInHistory.add(historyItem.getSnapshotName()); - historyLatch.countDown(); - }), + MockSnapshotRetentionTask retentionTask = new MockSnapshotRetentionTask( + noOpClient, + clusterService, + new SnapshotLifecycleTaskTests.VerifyingHistoryStore(noOpClient, ZoneOffset.UTC, (historyItem) -> { + assertEquals(deletionSuccess, historyItem.isSuccess()); + if (historyItem.isSuccess() == false) { + assertThat(historyItem.getErrorDetails(), containsString("deletion_failed")); + } + assertEquals(policyId, historyItem.getPolicyId()); + assertEquals(repoId, historyItem.getRepository()); + assertEquals(DELETE_OPERATION, historyItem.getOperation()); + deletedSnapshotsInHistory.add(historyItem.getSnapshotName()); + historyLatch.countDown(); + }), () -> { List snaps = new ArrayList<>(2); snaps.add(eligibleSnapshot); @@ -231,7 +282,8 @@ private void retentionTaskTest(final boolean deletionSuccess) throws Exception { } deletionLatch.countDown(); }, - System::nanoTime); + System::nanoTime + ); long time = System.currentTimeMillis(); retentionTask.triggered(new SchedulerEngine.Event(SnapshotRetentionService.SLM_RETENTION_JOB_ID, time, time)); @@ -255,32 +307,50 @@ public void testErrStillRunsFailureHandlerWhenRetrieving() throws Exception { ThreadPool threadPool = new TestThreadPool("slm-test"); final String policyId = "policy"; final String repoId = "repo"; - try (ClusterService clusterService = ClusterServiceUtils.createClusterService(threadPool); - Client noOpClient = new NoOpClient("slm-test") { - - @Override - @SuppressWarnings("unchecked") - protected - void doExecute(ActionType action, Request request, ActionListener listener) { - if (request instanceof GetSnapshotsRequest) { - logger.info("--> called"); - listener.onResponse((Response) new GetSnapshotsResponse( - Collections.emptyList(), Collections.emptyMap(), null, 0, 0)); - } else { - super.doExecute(action, request, listener); - } - } - }) { - SnapshotLifecyclePolicy policy = new SnapshotLifecyclePolicy(policyId, "snap", "1 * * * * ?", - repoId, null, new SnapshotRetentionConfiguration(TimeValue.timeValueDays(30), null, null)); + try ( + ClusterService clusterService = ClusterServiceUtils.createClusterService(threadPool); + Client noOpClient = new NoOpClient("slm-test") { + + @Override + @SuppressWarnings("unchecked") + protected void doExecute( + ActionType action, + Request request, + ActionListener listener + ) { + if (request instanceof GetSnapshotsRequest) { + logger.info("--> called"); + listener.onResponse( + (Response) new GetSnapshotsResponse(Collections.emptyList(), Collections.emptyMap(), null, 0, 0) + ); + } else { + super.doExecute(action, request, listener); + } + } + } + ) { + SnapshotLifecyclePolicy policy = new SnapshotLifecyclePolicy( + policyId, + "snap", + "1 * * * * ?", + repoId, + null, + new SnapshotRetentionConfiguration(TimeValue.timeValueDays(30), null, null) + ); ClusterState state = createState(policy); ClusterServiceUtils.setState(clusterService, state); - SnapshotRetentionTask task = new SnapshotRetentionTask(noOpClient, clusterService, + SnapshotRetentionTask task = new SnapshotRetentionTask( + noOpClient, + clusterService, System::nanoTime, - new SnapshotLifecycleTaskTests.VerifyingHistoryStore(noOpClient, ZoneOffset.UTC, - (historyItem) -> fail("should never write history"))); + new SnapshotLifecycleTaskTests.VerifyingHistoryStore( + noOpClient, + ZoneOffset.UTC, + (historyItem) -> fail("should never write history") + ) + ); AtomicReference errHandlerCalled = new AtomicReference<>(null); task.getAllRetainableSnapshots(Collections.singleton(repoId), Collections.singleton(policyId), new ActionListener<>() { @@ -306,37 +376,58 @@ public void onFailure(Exception e) { public void testErrStillRunsFailureHandlerWhenDeleting() throws Exception { ThreadPool threadPool = new TestThreadPool("slm-test"); - try (ClusterService clusterService = ClusterServiceUtils.createClusterService(threadPool); - Client noOpClient = new NoOpClient("slm-test") { - - @Override - @SuppressWarnings("unchecked") - protected - void doExecute(ActionType action, Request request, ActionListener listener) { - if (request instanceof DeleteSnapshotRequest) { - logger.info("--> called"); - listener.onResponse((Response) AcknowledgedResponse.TRUE); - } else { - super.doExecute(action, request, listener); - } - } - }) { + try ( + ClusterService clusterService = ClusterServiceUtils.createClusterService(threadPool); + Client noOpClient = new NoOpClient("slm-test") { + + @Override + @SuppressWarnings("unchecked") + protected void doExecute( + ActionType action, + Request request, + ActionListener listener + ) { + if (request instanceof DeleteSnapshotRequest) { + logger.info("--> called"); + listener.onResponse((Response) AcknowledgedResponse.TRUE); + } else { + super.doExecute(action, request, listener); + } + } + } + ) { final String policyId = "policy"; final String repoId = "repo"; - SnapshotLifecyclePolicy policy = new SnapshotLifecyclePolicy(policyId, "snap", "1 * * * * ?", - repoId, null, new SnapshotRetentionConfiguration(TimeValue.timeValueDays(30), null, null)); + SnapshotLifecyclePolicy policy = new SnapshotLifecyclePolicy( + policyId, + "snap", + "1 * * * * ?", + repoId, + null, + new SnapshotRetentionConfiguration(TimeValue.timeValueDays(30), null, null) + ); ClusterState state = createState(policy); ClusterServiceUtils.setState(clusterService, state); - SnapshotRetentionTask task = new SnapshotRetentionTask(noOpClient, clusterService, + SnapshotRetentionTask task = new SnapshotRetentionTask( + noOpClient, + clusterService, System::nanoTime, - new SnapshotLifecycleTaskTests.VerifyingHistoryStore(noOpClient, ZoneOffset.UTC, - (historyItem) -> fail("should never write history"))); + new SnapshotLifecycleTaskTests.VerifyingHistoryStore( + noOpClient, + ZoneOffset.UTC, + (historyItem) -> fail("should never write history") + ) + ); AtomicBoolean onFailureCalled = new AtomicBoolean(false); - task.deleteSnapshot("policy", "foo", new SnapshotId("name", "uuid"), - new SnapshotLifecycleStats(0, 0, 0, 0, new HashMap<>()), new ActionListener<>() { + task.deleteSnapshot( + "policy", + "foo", + new SnapshotId("name", "uuid"), + new SnapshotLifecycleStats(0, 0, 0, 0, new HashMap<>()), + new ActionListener<>() { @Override public void onResponse(AcknowledgedResponse acknowledgedResponse) { logger.info("--> forcing failure"); @@ -347,7 +438,8 @@ public void onResponse(AcknowledgedResponse acknowledgedResponse) { public void onFailure(Exception e) { onFailureCalled.set(true); } - }); + } + ); assertThat(onFailureCalled.get(), equalTo(true)); } finally { @@ -366,25 +458,39 @@ public void testSkipWhileStopped() throws Exception { private void doTestSkipDuringMode(OperationMode mode) throws Exception { ThreadPool threadPool = new TestThreadPool("slm-test"); - try (ClusterService clusterService = ClusterServiceUtils.createClusterService(threadPool); - Client noOpClient = new NoOpClient("slm-test")) { + try ( + ClusterService clusterService = ClusterServiceUtils.createClusterService(threadPool); + Client noOpClient = new NoOpClient("slm-test") + ) { final String policyId = "policy"; final String repoId = "repo"; - SnapshotLifecyclePolicy policy = new SnapshotLifecyclePolicy(policyId, "snap", "1 * * * * ?", - repoId, null, new SnapshotRetentionConfiguration(TimeValue.timeValueDays(30), null, null)); + SnapshotLifecyclePolicy policy = new SnapshotLifecyclePolicy( + policyId, + "snap", + "1 * * * * ?", + repoId, + null, + new SnapshotRetentionConfiguration(TimeValue.timeValueDays(30), null, null) + ); ClusterState state = createState(mode, policy); ClusterServiceUtils.setState(clusterService, state); - SnapshotRetentionTask task = new MockSnapshotRetentionTask(noOpClient, clusterService, - new SnapshotLifecycleTaskTests.VerifyingHistoryStore(noOpClient, ZoneOffset.UTC, - (historyItem) -> fail("should never write history")), + SnapshotRetentionTask task = new MockSnapshotRetentionTask( + noOpClient, + clusterService, + new SnapshotLifecycleTaskTests.VerifyingHistoryStore( + noOpClient, + ZoneOffset.UTC, + (historyItem) -> fail("should never write history") + ), () -> { fail("should not retrieve snapshots"); return null; }, (a, b, c, d, e) -> fail("should not delete snapshots"), - System::nanoTime); + System::nanoTime + ); long time = System.currentTimeMillis(); task.triggered(new SchedulerEngine.Event(SnapshotRetentionService.SLM_RETENTION_JOB_ID, time, time)); @@ -404,27 +510,36 @@ public void testRunManuallyWhileStopped() throws Exception { private void doTestRunManuallyDuringMode(OperationMode mode) throws Exception { ThreadPool threadPool = new TestThreadPool("slm-test"); - try (ClusterService clusterService = ClusterServiceUtils.createClusterService(threadPool); - Client noOpClient = new NoOpClient("slm-test")) { + try ( + ClusterService clusterService = ClusterServiceUtils.createClusterService(threadPool); + Client noOpClient = new NoOpClient("slm-test") + ) { final String policyId = "policy"; final String repoId = "repo"; - SnapshotLifecyclePolicy policy = new SnapshotLifecyclePolicy(policyId, "snap", "1 * * * * ?", - repoId, null, new SnapshotRetentionConfiguration(TimeValue.timeValueDays(30), null, null)); + SnapshotLifecyclePolicy policy = new SnapshotLifecyclePolicy( + policyId, + "snap", + "1 * * * * ?", + repoId, + null, + new SnapshotRetentionConfiguration(TimeValue.timeValueDays(30), null, null) + ); ClusterState state = createState(mode, policy); ClusterServiceUtils.setState(clusterService, state); AtomicBoolean retentionWasRun = new AtomicBoolean(false); - MockSnapshotRetentionTask task = new MockSnapshotRetentionTask(noOpClient, clusterService, - new SnapshotLifecycleTaskTests.VerifyingHistoryStore(noOpClient, ZoneOffset.UTC, (historyItem) -> { - }), + MockSnapshotRetentionTask task = new MockSnapshotRetentionTask( + noOpClient, + clusterService, + new SnapshotLifecycleTaskTests.VerifyingHistoryStore(noOpClient, ZoneOffset.UTC, (historyItem) -> {}), () -> { retentionWasRun.set(true); return Collections.emptyMap(); }, - (deletionPolicyId, repo, snapId, slmStats, listener) -> { - }, - System::nanoTime); + (deletionPolicyId, repo, snapId, slmStats, listener) -> {}, + System::nanoTime + ); long time = System.currentTimeMillis(); task.triggered(new SchedulerEngine.Event(SnapshotRetentionService.SLM_RETENTION_MANUAL_JOB_ID, time, time)); @@ -442,54 +557,68 @@ public ClusterState createState(SnapshotLifecyclePolicy... policies) { public ClusterState createState(OperationMode mode, SnapshotLifecyclePolicy... policies) { Map policyMetadataMap = Arrays.stream(policies) - .map(policy -> SnapshotLifecyclePolicyMetadata.builder() - .setPolicy(policy) - .setHeaders(Collections.emptyMap()) - .setModifiedDate(randomNonNegativeLong()) - .setVersion(randomNonNegativeLong()) - .build()) + .map( + policy -> SnapshotLifecyclePolicyMetadata.builder() + .setPolicy(policy) + .setHeaders(Collections.emptyMap()) + .setModifiedDate(randomNonNegativeLong()) + .setVersion(randomNonNegativeLong()) + .build() + ) .collect(Collectors.toMap(pm -> pm.getPolicy().getId(), pm -> pm)); Metadata metadata = Metadata.builder() - .putCustom(SnapshotLifecycleMetadata.TYPE, - new SnapshotLifecycleMetadata(policyMetadataMap, mode, new SnapshotLifecycleStats())) - .build(); - return ClusterState.builder(new ClusterName("cluster")) - .metadata(metadata) + .putCustom(SnapshotLifecycleMetadata.TYPE, new SnapshotLifecycleMetadata(policyMetadataMap, mode, new SnapshotLifecycleStats())) .build(); + return ClusterState.builder(new ClusterName("cluster")).metadata(metadata).build(); } private static class MockSnapshotRetentionTask extends SnapshotRetentionTask { private final Supplier>> snapshotRetriever; private final DeleteSnapshotMock deleteRunner; - MockSnapshotRetentionTask(Client client, - ClusterService clusterService, - SnapshotHistoryStore historyStore, - Supplier>> snapshotRetriever, - DeleteSnapshotMock deleteRunner, - LongSupplier nanoSupplier) { + MockSnapshotRetentionTask( + Client client, + ClusterService clusterService, + SnapshotHistoryStore historyStore, + Supplier>> snapshotRetriever, + DeleteSnapshotMock deleteRunner, + LongSupplier nanoSupplier + ) { super(client, clusterService, nanoSupplier, historyStore); this.snapshotRetriever = snapshotRetriever; this.deleteRunner = deleteRunner; } @Override - void getAllRetainableSnapshots(Collection repositories, Set policies, - ActionListener>> listener) { + void getAllRetainableSnapshots( + Collection repositories, + Set policies, + ActionListener>> listener + ) { listener.onResponse(this.snapshotRetriever.get()); } @Override - void deleteSnapshot(String policyId, String repo, SnapshotId snapshot, SnapshotLifecycleStats slmStats, - ActionListener listener) { + void deleteSnapshot( + String policyId, + String repo, + SnapshotId snapshot, + SnapshotLifecycleStats slmStats, + ActionListener listener + ) { deleteRunner.apply(policyId, repo, snapshot, slmStats, listener); } } @FunctionalInterface interface DeleteSnapshotMock { - void apply(String policyId, String repo, SnapshotId snapshot, SnapshotLifecycleStats slmStats, - ActionListener listener); + void apply( + String policyId, + String repo, + SnapshotId snapshot, + SnapshotLifecycleStats slmStats, + ActionListener listener + ); } } diff --git a/x-pack/plugin/mapper-constant-keyword/src/internalClusterTest/java/org/elasticsearch/xpack/constantkeyword/mapper/ConstantKeywordFieldMapperTests.java b/x-pack/plugin/mapper-constant-keyword/src/internalClusterTest/java/org/elasticsearch/xpack/constantkeyword/mapper/ConstantKeywordFieldMapperTests.java index 55002f83201e2..0890b033fa4c5 100644 --- a/x-pack/plugin/mapper-constant-keyword/src/internalClusterTest/java/org/elasticsearch/xpack/constantkeyword/mapper/ConstantKeywordFieldMapperTests.java +++ b/x-pack/plugin/mapper-constant-keyword/src/internalClusterTest/java/org/elasticsearch/xpack/constantkeyword/mapper/ConstantKeywordFieldMapperTests.java @@ -12,7 +12,6 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.compress.CompressedXContent; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.LuceneDocument; import org.elasticsearch.index.mapper.MappedFieldType; @@ -22,6 +21,7 @@ import org.elasticsearch.index.mapper.MapperTestCase; import org.elasticsearch.index.mapper.ParsedDocument; import org.elasticsearch.plugins.Plugin; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.constantkeyword.ConstantKeywordMapperPlugin; import org.elasticsearch.xpack.constantkeyword.mapper.ConstantKeywordFieldMapper.ConstantKeywordFieldType; @@ -36,7 +36,7 @@ public class ConstantKeywordFieldMapperTests extends MapperTestCase { @Override protected void writeField(XContentBuilder builder) { - //do nothing + // do nothing } @Override @@ -76,10 +76,7 @@ public void testDefaults() throws Exception { doc = mapper.parse(source(b -> b.field("field", "foo"))); assertNull(doc.rootDoc().getField("field")); - MapperParsingException e = expectThrows( - MapperParsingException.class, - () -> mapper.parse(source(b -> b.field("field", "bar"))) - ); + MapperParsingException e = expectThrows(MapperParsingException.class, () -> mapper.parse(source(b -> b.field("field", "bar")))); assertEquals( "[constant_keyword] field [field] only accepts values that are equal to the value defined in the mappings [foo], " + "but got [bar]", @@ -107,7 +104,8 @@ public void testDynamicValue() throws Exception { public void testDynamicValueFieldLimit() throws Exception { MapperService mapperService = createMapperService( Settings.builder().put(INDEX_MAPPING_TOTAL_FIELDS_LIMIT_SETTING.getKey(), 1).build(), - fieldMapping(b -> b.field("type", "constant_keyword"))); + fieldMapping(b -> b.field("type", "constant_keyword")) + ); ParsedDocument doc = mapperService.documentMapper().parse(source(b -> b.field("field", "foo"))); assertNull(doc.rootDoc().getField("field")); @@ -129,16 +127,20 @@ public void testBadValues() { b.field("type", "constant_keyword"); b.nullField("value"); }))); - assertEquals(e.getMessage(), - "Failed to parse mapping: [value] on mapper [field] of type [constant_keyword] must not have a [null] value"); + assertEquals( + e.getMessage(), + "Failed to parse mapping: [value] on mapper [field] of type [constant_keyword] must not have a [null] value" + ); } { MapperParsingException e = expectThrows(MapperParsingException.class, () -> createMapperService(fieldMapping(b -> { b.field("type", "constant_keyword"); b.startObject("value").field("foo", "bar").endObject(); }))); - assertEquals(e.getMessage(), - "Failed to parse mapping: Property [value] on field [field] must be a number or a string, but got [{foo=bar}]"); + assertEquals( + e.getMessage(), + "Failed to parse mapping: Property [value] on field [field] must be a number or a string, but got [{foo=bar}]" + ); } } @@ -147,8 +149,7 @@ public void testNumericValue() throws IOException { b.field("type", "constant_keyword"); b.field("value", 74); })); - ConstantKeywordFieldType ft - = (ConstantKeywordFieldType) mapperService.fieldType("field"); + ConstantKeywordFieldType ft = (ConstantKeywordFieldType) mapperService.fieldType("field"); assertEquals("74", ft.value()); } @@ -162,9 +163,10 @@ public void testUpdate() throws IOException { b.field("type", "constant_keyword"); b.field("value", "bar"); }))); - assertEquals(e.getMessage(), - "Mapper for [field] conflicts with existing mapper:\n" + - "\tCannot update parameter [value] from [foo] to [bar]"); + assertEquals( + e.getMessage(), + "Mapper for [field] conflicts with existing mapper:\n" + "\tCannot update parameter [value] from [foo] to [bar]" + ); } @Override @@ -178,15 +180,13 @@ protected void registerParameters(ParameterChecker checker) throws IOException { ConstantKeywordFieldType ft = (ConstantKeywordFieldType) m.fieldType(); assertEquals("foo", ft.value()); }); - checker.registerConflictCheck("value", - fieldMapping(b -> { - b.field("type", "constant_keyword"); - b.field("value", "foo"); - }), - fieldMapping(b -> { - b.field("type", "constant_keyword"); - b.field("value", "bar"); - })); + checker.registerConflictCheck("value", fieldMapping(b -> { + b.field("type", "constant_keyword"); + b.field("value", "foo"); + }), fieldMapping(b -> { + b.field("type", "constant_keyword"); + b.field("value", "bar"); + })); } @Override diff --git a/x-pack/plugin/mapper-constant-keyword/src/main/java/org/elasticsearch/xpack/constantkeyword/mapper/ConstantKeywordFieldMapper.java b/x-pack/plugin/mapper-constant-keyword/src/main/java/org/elasticsearch/xpack/constantkeyword/mapper/ConstantKeywordFieldMapper.java index 7dac799216556..d3b6d0e49cf9f 100644 --- a/x-pack/plugin/mapper-constant-keyword/src/main/java/org/elasticsearch/xpack/constantkeyword/mapper/ConstantKeywordFieldMapper.java +++ b/x-pack/plugin/mapper-constant-keyword/src/main/java/org/elasticsearch/xpack/constantkeyword/mapper/ConstantKeywordFieldMapper.java @@ -5,7 +5,6 @@ * 2.0. */ - package org.elasticsearch.xpack.constantkeyword.mapper; import org.apache.lucene.index.TermsEnum; @@ -24,7 +23,6 @@ import org.elasticsearch.common.regex.Regex; import org.elasticsearch.common.time.DateMathParser; import org.elasticsearch.common.unit.Fuzziness; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.index.fielddata.IndexFieldData; import org.elasticsearch.index.fielddata.plain.ConstantIndexFieldData; import org.elasticsearch.index.mapper.ConstantFieldType; @@ -39,6 +37,7 @@ import org.elasticsearch.index.query.SearchExecutionContext; import org.elasticsearch.search.aggregations.support.CoreValuesSourceType; import org.elasticsearch.search.lookup.SearchLookup; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.termsenum.action.SimpleTermCountEnum; import org.elasticsearch.xpack.core.termsenum.action.TermCount; @@ -70,15 +69,13 @@ public FieldMapper.Builder getMergeBuilder() { public static class Builder extends FieldMapper.Builder { // This is defined as updateable because it can be updated once, from [null] to any value, - // by a dynamic mapping update. Once it has been set, however, the value cannot be changed. - private final Parameter value = new Parameter<>("value", true, () -> null, - (n, c, o) -> { - if (o instanceof Number == false && o instanceof CharSequence == false) { - throw new MapperParsingException("Property [value] on field [" + n + - "] must be a number or a string, but got [" + o + "]"); - } - return o.toString(); - }, m -> toType(m).fieldType().value); + // by a dynamic mapping update. Once it has been set, however, the value cannot be changed. + private final Parameter value = new Parameter<>("value", true, () -> null, (n, c, o) -> { + if (o instanceof Number == false && o instanceof CharSequence == false) { + throw new MapperParsingException("Property [value] on field [" + n + "] must be a number or a string, but got [" + o + "]"); + } + return o.toString(); + }, m -> toType(m).fieldType().value); private final Parameter> meta = Parameter.metaParam(); public Builder(String name) { @@ -95,7 +92,9 @@ protected List> getParameters() { @Override public ConstantKeywordFieldMapper build(MapperBuilderContext context) { return new ConstantKeywordFieldMapper( - name, new ConstantKeywordFieldType(context.buildFullName(name), value.getValue(), meta.getValue())); + name, + new ConstantKeywordFieldType(context.buildFullName(name), value.getValue(), meta.getValue()) + ); } } @@ -140,19 +139,15 @@ public ValueFetcher valueFetcher(SearchExecutionContext context, String format) throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats."); } - return value == null - ? (lookup, ignoredValues) -> List.of() - : (lookup, ignoredValues) -> List.of(value); + return value == null ? (lookup, ignoredValues) -> List.of() : (lookup, ignoredValues) -> List.of(value); } - - @Override public TermsEnum getTerms(boolean caseInsensitive, String string, SearchExecutionContext queryShardContext, String searchAfter) throws IOException { - boolean matches = caseInsensitive ? - value.toLowerCase(Locale.ROOT).startsWith(string.toLowerCase(Locale.ROOT)) : - value.startsWith(string); + boolean matches = caseInsensitive + ? value.toLowerCase(Locale.ROOT).startsWith(string.toLowerCase(Locale.ROOT)) + : value.startsWith(string); if (matches == false) { return null; } @@ -181,10 +176,15 @@ public Query existsQuery(SearchExecutionContext context) { @Override public Query rangeQuery( - Object lowerTerm, Object upperTerm, - boolean includeLower, boolean includeUpper, - ShapeRelation relation, ZoneId timeZone, DateMathParser parser, - SearchExecutionContext context) { + Object lowerTerm, + Object upperTerm, + boolean includeLower, + boolean includeUpper, + ShapeRelation relation, + ZoneId timeZone, + DateMathParser parser, + SearchExecutionContext context + ) { if (this.value == null) { return new MatchNoDocsQuery(); } @@ -200,8 +200,14 @@ public Query rangeQuery( } @Override - public Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int maxExpansions, - boolean transpositions, SearchExecutionContext context) { + public Query fuzzyQuery( + Object value, + Fuzziness fuzziness, + int prefixLength, + int maxExpansions, + boolean transpositions, + SearchExecutionContext context + ) { if (this.value == null) { return new MatchNoDocsQuery(); } @@ -211,7 +217,7 @@ public Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int final int[] termText = new int[termAsString.codePointCount(0, termAsString.length())]; for (int cp, i = 0, j = 0; i < termAsString.length(); i += Character.charCount(cp)) { - termText[j++] = cp = termAsString.codePointAt(i); + termText[j++] = cp = termAsString.codePointAt(i); } final int termLength = termText.length; @@ -230,8 +236,14 @@ public Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int } @Override - public Query regexpQuery(String value, int syntaxFlags, int matchFlags, int maxDeterminizedStates, - MultiTermQuery.RewriteMethod method, SearchExecutionContext context) { + public Query regexpQuery( + String value, + int syntaxFlags, + int matchFlags, + int maxDeterminizedStates, + MultiTermQuery.RewriteMethod method, + SearchExecutionContext context + ) { if (this.value == null) { return new MatchNoDocsQuery(); } @@ -270,9 +282,15 @@ protected void parseCreateField(DocumentParserContext context) throws IOExceptio Mapper update = new ConstantKeywordFieldMapper(simpleName(), newFieldType); context.addDynamicMapper(update); } else if (Objects.equals(fieldType().value, value) == false) { - throw new IllegalArgumentException("[constant_keyword] field [" + name() + - "] only accepts values that are equal to the value defined in the mappings [" + fieldType().value() + - "], but got [" + value + "]"); + throw new IllegalArgumentException( + "[constant_keyword] field [" + + name() + + "] only accepts values that are equal to the value defined in the mappings [" + + fieldType().value() + + "], but got [" + + value + + "]" + ); } } diff --git a/x-pack/plugin/ml/qa/basic-multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlBasicMultiNodeIT.java b/x-pack/plugin/ml/qa/basic-multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlBasicMultiNodeIT.java index 9e870a900d5e5..f4998e63b297b 100644 --- a/x-pack/plugin/ml/qa/basic-multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlBasicMultiNodeIT.java +++ b/x-pack/plugin/ml/qa/basic-multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlBasicMultiNodeIT.java @@ -13,8 +13,8 @@ import org.elasticsearch.client.Response; import org.elasticsearch.client.ResponseException; import org.elasticsearch.common.Strings; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.test.rest.ESRestTestCase; +import org.elasticsearch.xcontent.XContentBuilder; import org.yaml.snakeyaml.util.UriEncoder; import java.io.IOException; @@ -34,8 +34,13 @@ public class MlBasicMultiNodeIT extends ESRestTestCase { private static final String BASE_PATH = "/_ml/"; private static final RequestOptions POST_DATA_OPTIONS = RequestOptions.DEFAULT.toBuilder() - .setWarningsHandler(warnings -> Collections.singletonList("Posting data directly to anomaly detection jobs is deprecated, " + - "in a future major version it will be compulsory to use a datafeed").equals(warnings) == false).build(); + .setWarningsHandler( + warnings -> Collections.singletonList( + "Posting data directly to anomaly detection jobs is deprecated, " + + "in a future major version it will be compulsory to use a datafeed" + ).equals(warnings) == false + ) + .build(); public void testMachineLearningInstalled() throws Exception { Response response = client().performRequest(new Request("GET", "/_xpack")); @@ -52,8 +57,8 @@ public void testInvalidJob() { ResponseException e = expectThrows(ResponseException.class, () -> createFarequoteJob(jobId)); assertTrue(e.getMessage(), e.getMessage().contains("can contain lowercase alphanumeric (a-z and 0-9), hyphens or underscores")); // If validation of the invalid job is not done until after transportation to the master node then the - // root cause gets reported as a remote_transport_exception. The code in PubJobAction is supposed to - // validate before transportation to avoid this. This test must be done in a multi-node cluster to have + // root cause gets reported as a remote_transport_exception. The code in PubJobAction is supposed to + // validate before transportation to avoid this. This test must be done in a multi-node cluster to have // a chance of catching a problem, hence it is here rather than in the single node integration tests. assertFalse(e.getMessage(), e.getMessage().contains("remote_transport_exception")); } @@ -62,15 +67,17 @@ public void testMiniFarequote() throws Exception { String jobId = "mini-farequote-job"; createFarequoteJob(jobId); - Response openResponse = client().performRequest( - new Request("POST", BASE_PATH + "anomaly_detectors/" + jobId + "/_open")); + Response openResponse = client().performRequest(new Request("POST", BASE_PATH + "anomaly_detectors/" + jobId + "/_open")); assertThat(entityAsMap(openResponse), hasEntry("opened", true)); Request addData = new Request("POST", BASE_PATH + "anomaly_detectors/" + jobId + "/_data"); - addData.setEntity(new NStringEntity( - "{\"airline\":\"AAL\",\"responsetime\":\"132.2046\",\"sourcetype\":\"farequote\",\"time\":\"1403481600\"}\n" + - "{\"airline\":\"JZA\",\"responsetime\":\"990.4628\",\"sourcetype\":\"farequote\",\"time\":\"1403481700\"}", - randomFrom(ContentType.APPLICATION_JSON, ContentType.create("application/x-ndjson")))); + addData.setEntity( + new NStringEntity( + "{\"airline\":\"AAL\",\"responsetime\":\"132.2046\",\"sourcetype\":\"farequote\",\"time\":\"1403481600\"}\n" + + "{\"airline\":\"JZA\",\"responsetime\":\"990.4628\",\"sourcetype\":\"farequote\",\"time\":\"1403481700\"}", + randomFrom(ContentType.APPLICATION_JSON, ContentType.create("application/x-ndjson")) + ) + ); addData.setOptions(POST_DATA_OPTIONS); Response addDataResponse = client().performRequest(addData); assertEquals(202, addDataResponse.getStatusLine().getStatusCode()); @@ -86,8 +93,7 @@ public void testMiniFarequote() throws Exception { assertEquals(1403481600000L, responseBody.get("earliest_record_timestamp")); assertEquals(1403481700000L, responseBody.get("latest_record_timestamp")); - Response flushResponse = client().performRequest( - new Request("POST", BASE_PATH + "anomaly_detectors/" + jobId + "/_flush")); + Response flushResponse = client().performRequest(new Request("POST", BASE_PATH + "anomaly_detectors/" + jobId + "/_flush")); assertFlushResponse(flushResponse, true, 1403481600000L); Request closeRequest = new Request("POST", BASE_PATH + "anomaly_detectors/" + jobId + "/_close"); @@ -95,10 +101,8 @@ public void testMiniFarequote() throws Exception { Response closeResponse = client().performRequest(closeRequest); assertEquals(Collections.singletonMap("closed", true), entityAsMap(closeResponse)); - Response statsResponse = client().performRequest( - new Request("GET", BASE_PATH + "anomaly_detectors/" + jobId + "/_stats")); - Map dataCountsDoc = (Map) - ((Map)((List) entityAsMap(statsResponse).get("jobs")).get(0)).get("data_counts"); + Response statsResponse = client().performRequest(new Request("GET", BASE_PATH + "anomaly_detectors/" + jobId + "/_stats")); + Map dataCountsDoc = (Map) ((Map) ((List) entityAsMap(statsResponse).get("jobs")).get(0)).get("data_counts"); assertEquals(2, dataCountsDoc.get("processed_record_count")); assertEquals(4, dataCountsDoc.get("processed_field_count")); assertEquals(177, dataCountsDoc.get("input_bytes")); @@ -120,8 +124,7 @@ public void testMiniFarequoteWithDatafeeder() throws Exception { String datafeedId = "bar"; createDatafeed(datafeedId, jobId); - Response openResponse = client().performRequest( - new Request("POST", BASE_PATH + "anomaly_detectors/" + jobId + "/_open")); + Response openResponse = client().performRequest(new Request("POST", BASE_PATH + "anomaly_detectors/" + jobId + "/_open")); assertThat(entityAsMap(openResponse), hasEntry("opened", true)); Request startRequest = new Request("POST", BASE_PATH + "datafeeds/" + datafeedId + "/_start"); @@ -131,10 +134,10 @@ public void testMiniFarequoteWithDatafeeder() throws Exception { assertBusy(() -> { try { - Response statsResponse = client().performRequest( - new Request("GET", BASE_PATH + "anomaly_detectors/" + jobId + "/_stats")); - Map dataCountsDoc = (Map) - ((Map)((List) entityAsMap(statsResponse).get("jobs")).get(0)).get("data_counts"); + Response statsResponse = client().performRequest(new Request("GET", BASE_PATH + "anomaly_detectors/" + jobId + "/_stats")); + Map dataCountsDoc = (Map) ((Map) ((List) entityAsMap(statsResponse).get("jobs")).get(0)).get( + "data_counts" + ); assertEquals(2, dataCountsDoc.get("input_record_count")); assertEquals(2, dataCountsDoc.get("processed_record_count")); } catch (IOException e) { @@ -142,14 +145,12 @@ public void testMiniFarequoteWithDatafeeder() throws Exception { } }); - Response stopResponse = client().performRequest( - new Request("POST", BASE_PATH + "datafeeds/" + datafeedId + "/_stop")); + Response stopResponse = client().performRequest(new Request("POST", BASE_PATH + "datafeeds/" + datafeedId + "/_stop")); assertEquals(Collections.singletonMap("stopped", true), entityAsMap(stopResponse)); Request closeRequest = new Request("POST", BASE_PATH + "anomaly_detectors/" + jobId + "/_close"); closeRequest.addParameter("timeout", "20s"); - assertEquals(Collections.singletonMap("closed", true), - entityAsMap(client().performRequest(closeRequest))); + assertEquals(Collections.singletonMap("closed", true), entityAsMap(client().performRequest(closeRequest))); client().performRequest(new Request("DELETE", BASE_PATH + "datafeeds/" + datafeedId)); client().performRequest(new Request("DELETE", BASE_PATH + "anomaly_detectors/" + jobId)); @@ -159,18 +160,20 @@ public void testMiniFarequoteReopen() throws Exception { String jobId = "mini-farequote-reopen"; createFarequoteJob(jobId); - Response openResponse = client().performRequest( - new Request("POST", BASE_PATH + "anomaly_detectors/" + jobId + "/_open")); + Response openResponse = client().performRequest(new Request("POST", BASE_PATH + "anomaly_detectors/" + jobId + "/_open")); assertThat(entityAsMap(openResponse), hasEntry("opened", true)); Request addDataRequest = new Request("POST", BASE_PATH + "anomaly_detectors/" + jobId + "/_data"); - addDataRequest.setEntity(new NStringEntity( - "{\"airline\":\"AAL\",\"responsetime\":\"132.2046\",\"sourcetype\":\"farequote\",\"time\":\"1403481600\"}\n" + - "{\"airline\":\"JZA\",\"responsetime\":\"990.4628\",\"sourcetype\":\"farequote\",\"time\":\"1403481700\"}\n" + - "{\"airline\":\"JBU\",\"responsetime\":\"877.5927\",\"sourcetype\":\"farequote\",\"time\":\"1403481800\"}\n" + - "{\"airline\":\"KLM\",\"responsetime\":\"1355.4812\",\"sourcetype\":\"farequote\",\"time\":\"1403481900\"}\n" + - "{\"airline\":\"NKS\",\"responsetime\":\"9991.3981\",\"sourcetype\":\"farequote\",\"time\":\"1403482000\"}", - randomFrom(ContentType.APPLICATION_JSON, ContentType.create("application/x-ndjson")))); + addDataRequest.setEntity( + new NStringEntity( + "{\"airline\":\"AAL\",\"responsetime\":\"132.2046\",\"sourcetype\":\"farequote\",\"time\":\"1403481600\"}\n" + + "{\"airline\":\"JZA\",\"responsetime\":\"990.4628\",\"sourcetype\":\"farequote\",\"time\":\"1403481700\"}\n" + + "{\"airline\":\"JBU\",\"responsetime\":\"877.5927\",\"sourcetype\":\"farequote\",\"time\":\"1403481800\"}\n" + + "{\"airline\":\"KLM\",\"responsetime\":\"1355.4812\",\"sourcetype\":\"farequote\",\"time\":\"1403481900\"}\n" + + "{\"airline\":\"NKS\",\"responsetime\":\"9991.3981\",\"sourcetype\":\"farequote\",\"time\":\"1403482000\"}", + randomFrom(ContentType.APPLICATION_JSON, ContentType.create("application/x-ndjson")) + ) + ); // Post data is deprecated, so expect a deprecation warning addDataRequest.setOptions(POST_DATA_OPTIONS); Response addDataResponse = client().performRequest(addDataRequest); @@ -187,14 +190,12 @@ public void testMiniFarequoteReopen() throws Exception { assertEquals(1403481600000L, responseBody.get("earliest_record_timestamp")); assertEquals(1403482000000L, responseBody.get("latest_record_timestamp")); - Response flushResponse = client().performRequest( - new Request("POST", BASE_PATH + "anomaly_detectors/" + jobId + "/_flush")); + Response flushResponse = client().performRequest(new Request("POST", BASE_PATH + "anomaly_detectors/" + jobId + "/_flush")); assertFlushResponse(flushResponse, true, 1403481600000L); Request closeRequest = new Request("POST", BASE_PATH + "anomaly_detectors/" + jobId + "/_close"); closeRequest.addParameter("timeout", "20s"); - assertEquals(Collections.singletonMap("closed", true), - entityAsMap(client().performRequest(closeRequest))); + assertEquals(Collections.singletonMap("closed", true), entityAsMap(client().performRequest(closeRequest))); Request statsRequest = new Request("GET", BASE_PATH + "anomaly_detectors/" + jobId + "/_stats"); client().performRequest(statsRequest); @@ -206,13 +207,16 @@ public void testMiniFarequoteReopen() throws Exception { // feed some more data points Request addDataRequest2 = new Request("POST", BASE_PATH + "anomaly_detectors/" + jobId + "/_data"); - addDataRequest2.setEntity(new NStringEntity( - "{\"airline\":\"AAL\",\"responsetime\":\"136.2361\",\"sourcetype\":\"farequote\",\"time\":\"1407081600\"}\n" + - "{\"airline\":\"VRD\",\"responsetime\":\"282.9847\",\"sourcetype\":\"farequote\",\"time\":\"1407081700\"}\n" + - "{\"airline\":\"JAL\",\"responsetime\":\"493.0338\",\"sourcetype\":\"farequote\",\"time\":\"1407081800\"}\n" + - "{\"airline\":\"UAL\",\"responsetime\":\"8.4275\",\"sourcetype\":\"farequote\",\"time\":\"1407081900\"}\n" + - "{\"airline\":\"FFT\",\"responsetime\":\"221.8693\",\"sourcetype\":\"farequote\",\"time\":\"1407082000\"}", - randomFrom(ContentType.APPLICATION_JSON, ContentType.create("application/x-ndjson")))); + addDataRequest2.setEntity( + new NStringEntity( + "{\"airline\":\"AAL\",\"responsetime\":\"136.2361\",\"sourcetype\":\"farequote\",\"time\":\"1407081600\"}\n" + + "{\"airline\":\"VRD\",\"responsetime\":\"282.9847\",\"sourcetype\":\"farequote\",\"time\":\"1407081700\"}\n" + + "{\"airline\":\"JAL\",\"responsetime\":\"493.0338\",\"sourcetype\":\"farequote\",\"time\":\"1407081800\"}\n" + + "{\"airline\":\"UAL\",\"responsetime\":\"8.4275\",\"sourcetype\":\"farequote\",\"time\":\"1407081900\"}\n" + + "{\"airline\":\"FFT\",\"responsetime\":\"221.8693\",\"sourcetype\":\"farequote\",\"time\":\"1407082000\"}", + randomFrom(ContentType.APPLICATION_JSON, ContentType.create("application/x-ndjson")) + ) + ); // Post data is deprecated, so expect a deprecation warning addDataRequest2.setOptions(POST_DATA_OPTIONS); Response addDataResponse2 = client().performRequest(addDataRequest2); @@ -231,14 +235,12 @@ public void testMiniFarequoteReopen() throws Exception { assertNull(responseBody2.get("earliest_record_timestamp")); assertEquals(1407082000000L, responseBody2.get("latest_record_timestamp")); - assertEquals(Collections.singletonMap("closed", true), - entityAsMap(client().performRequest(closeRequest))); + assertEquals(Collections.singletonMap("closed", true), entityAsMap(client().performRequest(closeRequest))); // counts should be summed up Response statsResponse = client().performRequest(statsRequest); - Map dataCountsDoc = (Map) - ((Map)((List) entityAsMap(statsResponse).get("jobs")).get(0)).get("data_counts"); + Map dataCountsDoc = (Map) ((Map) ((List) entityAsMap(statsResponse).get("jobs")).get(0)).get("data_counts"); assertEquals(10, dataCountsDoc.get("processed_record_count")); assertEquals(20, dataCountsDoc.get("processed_field_count")); assertEquals(888, dataCountsDoc.get("input_bytes")); @@ -258,8 +260,9 @@ public void testExportAndPutJob() throws Exception { String jobId = "test-export-import-job"; createFarequoteJob(jobId); Response jobResponse = client().performRequest( - new Request("GET", BASE_PATH + "anomaly_detectors/" + jobId + "?exclude_generated=true")); - Map originalJobBody = (Map)((List) entityAsMap(jobResponse).get("jobs")).get(0); + new Request("GET", BASE_PATH + "anomaly_detectors/" + jobId + "?exclude_generated=true") + ); + Map originalJobBody = (Map) ((List) entityAsMap(jobResponse).get("jobs")).get(0); originalJobBody.remove("job_id"); XContentBuilder xContentBuilder = jsonBuilder().map(originalJobBody); @@ -268,8 +271,9 @@ public void testExportAndPutJob() throws Exception { client().performRequest(request); Response importedJobResponse = client().performRequest( - new Request("GET", BASE_PATH + "anomaly_detectors/" + jobId + "-import" + "?exclude_generated=true")); - Map importedJobBody = (Map)((List) entityAsMap(importedJobResponse).get("jobs")).get(0); + new Request("GET", BASE_PATH + "anomaly_detectors/" + jobId + "-import" + "?exclude_generated=true") + ); + Map importedJobBody = (Map) ((List) entityAsMap(importedJobResponse).get("jobs")).get(0); importedJobBody.remove("job_id"); assertThat(originalJobBody, equalTo(importedJobBody)); } @@ -283,11 +287,12 @@ public void testExportAndPutDatafeed() throws Exception { createDatafeed(datafeedId, jobId); Response dfResponse = client().performRequest( - new Request("GET", BASE_PATH + "datafeeds/" + datafeedId + "?exclude_generated=true")); - Map originalDfBody = (Map)((List) entityAsMap(dfResponse).get("datafeeds")).get(0); + new Request("GET", BASE_PATH + "datafeeds/" + datafeedId + "?exclude_generated=true") + ); + Map originalDfBody = (Map) ((List) entityAsMap(dfResponse).get("datafeeds")).get(0); originalDfBody.remove("datafeed_id"); - //Delete this so we can PUT another datafeed for the same job + // Delete this so we can PUT another datafeed for the same job client().performRequest(new Request("DELETE", BASE_PATH + "datafeeds/" + datafeedId)); Map toPut = new HashMap<>(originalDfBody); @@ -298,8 +303,9 @@ public void testExportAndPutDatafeed() throws Exception { client().performRequest(request); Response importedDfResponse = client().performRequest( - new Request("GET", BASE_PATH + "datafeeds/" + datafeedId + "-import" + "?exclude_generated=true")); - Map importedDfBody = (Map)((List) entityAsMap(importedDfResponse).get("datafeeds")).get(0); + new Request("GET", BASE_PATH + "datafeeds/" + datafeedId + "-import" + "?exclude_generated=true") + ); + Map importedDfBody = (Map) ((List) entityAsMap(importedDfResponse).get("datafeeds")).get(0); importedDfBody.remove("datafeed_id"); assertThat(originalDfBody, equalTo(importedDfBody)); } @@ -340,8 +346,9 @@ public void testExportAndPutDataFrameAnalytics_OutlierDetection() throws Excepti client().performRequest(request); Response jobResponse = client().performRequest( - new Request("GET", BASE_PATH + "data_frame/analytics/" + analyticsId + "?exclude_generated=true")); - Map originalJobBody = (Map)((List) entityAsMap(jobResponse).get("data_frame_analytics")).get(0); + new Request("GET", BASE_PATH + "data_frame/analytics/" + analyticsId + "?exclude_generated=true") + ); + Map originalJobBody = (Map) ((List) entityAsMap(jobResponse).get("data_frame_analytics")).get(0); originalJobBody.remove("id"); XContentBuilder newBuilder = jsonBuilder().map(originalJobBody); @@ -350,9 +357,9 @@ public void testExportAndPutDataFrameAnalytics_OutlierDetection() throws Excepti client().performRequest(request); Response importedJobResponse = client().performRequest( - new Request("GET", BASE_PATH + "data_frame/analytics/" + analyticsId + "-import" + "?exclude_generated=true")); - Map importedJobBody = (Map)((List) entityAsMap(importedJobResponse) - .get("data_frame_analytics")) + new Request("GET", BASE_PATH + "data_frame/analytics/" + analyticsId + "-import" + "?exclude_generated=true") + ); + Map importedJobBody = (Map) ((List) entityAsMap(importedJobResponse).get("data_frame_analytics")) .get(0); importedJobBody.remove("id"); assertThat(originalJobBody, equalTo(importedJobBody)); @@ -395,8 +402,9 @@ public void testExportAndPutDataFrameAnalytics_Regression() throws Exception { client().performRequest(request); Response jobResponse = client().performRequest( - new Request("GET", BASE_PATH + "data_frame/analytics/" + analyticsId + "?exclude_generated=true")); - Map originalJobBody = (Map)((List) entityAsMap(jobResponse).get("data_frame_analytics")).get(0); + new Request("GET", BASE_PATH + "data_frame/analytics/" + analyticsId + "?exclude_generated=true") + ); + Map originalJobBody = (Map) ((List) entityAsMap(jobResponse).get("data_frame_analytics")).get(0); originalJobBody.remove("id"); XContentBuilder newBuilder = jsonBuilder().map(originalJobBody); @@ -405,9 +413,9 @@ public void testExportAndPutDataFrameAnalytics_Regression() throws Exception { client().performRequest(request); Response importedJobResponse = client().performRequest( - new Request("GET", BASE_PATH + "data_frame/analytics/" + analyticsId + "-import" + "?exclude_generated=true")); - Map importedJobBody = (Map)((List) entityAsMap(importedJobResponse) - .get("data_frame_analytics")) + new Request("GET", BASE_PATH + "data_frame/analytics/" + analyticsId + "-import" + "?exclude_generated=true") + ); + Map importedJobBody = (Map) ((List) entityAsMap(importedJobResponse).get("data_frame_analytics")) .get(0); importedJobBody.remove("id"); assertThat(originalJobBody, equalTo(importedJobBody)); @@ -450,8 +458,9 @@ public void testExportAndPutDataFrameAnalytics_Classification() throws Exception client().performRequest(request); Response jobResponse = client().performRequest( - new Request("GET", BASE_PATH + "data_frame/analytics/" + analyticsId + "?exclude_generated=true")); - Map originalJobBody = (Map)((List) entityAsMap(jobResponse).get("data_frame_analytics")).get(0); + new Request("GET", BASE_PATH + "data_frame/analytics/" + analyticsId + "?exclude_generated=true") + ); + Map originalJobBody = (Map) ((List) entityAsMap(jobResponse).get("data_frame_analytics")).get(0); originalJobBody.remove("id"); XContentBuilder newBuilder = jsonBuilder().map(originalJobBody); @@ -460,9 +469,9 @@ public void testExportAndPutDataFrameAnalytics_Classification() throws Exception client().performRequest(request); Response importedJobResponse = client().performRequest( - new Request("GET", BASE_PATH + "data_frame/analytics/" + analyticsId + "-import" + "?exclude_generated=true")); - Map importedJobBody = (Map)((List) entityAsMap(importedJobResponse) - .get("data_frame_analytics")) + new Request("GET", BASE_PATH + "data_frame/analytics/" + analyticsId + "-import" + "?exclude_generated=true") + ); + Map importedJobBody = (Map) ((List) entityAsMap(importedJobResponse).get("data_frame_analytics")) .get(0); importedJobBody.remove("id"); assertThat(originalJobBody, equalTo(importedJobBody)); @@ -519,7 +528,7 @@ private Response createFarequoteJob(String jobId) throws Exception { } private static void assertFlushResponse(Response response, boolean expectedFlushed, long expectedLastFinalizedBucketEnd) - throws IOException { + throws IOException { Map asMap = entityAsMap(response); assertThat(asMap.size(), equalTo(2)); assertThat(asMap.get("flushed"), is(true)); @@ -532,15 +541,21 @@ private void createAndIndexFarequote() throws Exception { String dateFormat = datesHaveNanoSecondResolution ? "strict_date_optional_time_nanos" : "strict_date_optional_time"; String randomNanos = datesHaveNanoSecondResolution ? "," + randomIntBetween(100000000, 999999999) : ""; Request createAirlineDataRequest = new Request("PUT", "/airline-data"); - createAirlineDataRequest.setJsonEntity("{" - + " \"mappings\": {" - + " \"properties\": {" - + " \"time\": { \"type\":\"" + dateMappingType + "\", \"format\":\"" + dateFormat + "\"}," - + " \"airline\": { \"type\":\"keyword\"}," - + " \"responsetime\": { \"type\":\"float\"}" - + " }" - + " }" - + "}"); + createAirlineDataRequest.setJsonEntity( + "{" + + " \"mappings\": {" + + " \"properties\": {" + + " \"time\": { \"type\":\"" + + dateMappingType + + "\", \"format\":\"" + + dateFormat + + "\"}," + + " \"airline\": { \"type\":\"keyword\"}," + + " \"responsetime\": { \"type\":\"float\"}" + + " }" + + " }" + + "}" + ); client().performRequest(createAirlineDataRequest); Request airlineData1 = new Request("PUT", "/airline-data/_doc/1"); airlineData1.setJsonEntity("{\"time\":\"2016-06-01T00:00:00" + randomNanos + "Z\",\"airline\":\"AAA\",\"responsetime\":135.22}"); diff --git a/x-pack/plugin/ml/qa/disabled/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlPluginDisabledIT.java b/x-pack/plugin/ml/qa/disabled/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlPluginDisabledIT.java index 93c11bb1ccb56..8170621cfbdbb 100644 --- a/x-pack/plugin/ml/qa/disabled/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlPluginDisabledIT.java +++ b/x-pack/plugin/ml/qa/disabled/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlPluginDisabledIT.java @@ -9,8 +9,8 @@ import org.elasticsearch.client.Request; import org.elasticsearch.client.ResponseException; import org.elasticsearch.common.Strings; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.test.rest.ESRestTestCase; +import org.elasticsearch.xcontent.XContentBuilder; import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.Matchers.containsString; diff --git a/x-pack/plugin/ml/qa/ml-with-security/src/yamlRestTest/java/org/elasticsearch/smoketest/MlWithSecurityIT.java b/x-pack/plugin/ml/qa/ml-with-security/src/yamlRestTest/java/org/elasticsearch/smoketest/MlWithSecurityIT.java index 1676c3c385be9..1dc2eedfedff0 100644 --- a/x-pack/plugin/ml/qa/ml-with-security/src/yamlRestTest/java/org/elasticsearch/smoketest/MlWithSecurityIT.java +++ b/x-pack/plugin/ml/qa/ml-with-security/src/yamlRestTest/java/org/elasticsearch/smoketest/MlWithSecurityIT.java @@ -7,6 +7,7 @@ package org.elasticsearch.smoketest; import com.carrotsearch.randomizedtesting.annotations.Name; + import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; @@ -19,7 +20,6 @@ import static org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken.basicAuthHeaderValue; - public class MlWithSecurityIT extends AbstractXPackRestTest { private static final String TEST_ADMIN_USERNAME = "x_pack_rest_user"; @@ -29,29 +29,27 @@ public MlWithSecurityIT(@Name("yaml") ClientYamlTestCandidate testCandidate) { } protected String[] getCredentials() { - return new String[]{"ml_admin", "x-pack-test-password"}; + return new String[] { "ml_admin", "x-pack-test-password" }; } @Override protected Settings restClientSettings() { String[] creds = getCredentials(); String token = basicAuthHeaderValue(creds[0], new SecureString(creds[1].toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } @Override protected Settings restAdminSettings() { String token = basicAuthHeaderValue(TEST_ADMIN_USERNAME, SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } protected Map getApiCallHeaders() { - return Collections.singletonMap("Authorization", basicAuthHeaderValue(TEST_ADMIN_USERNAME, - SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)); + return Collections.singletonMap( + "Authorization", + basicAuthHeaderValue(TEST_ADMIN_USERNAME, SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING) + ); } @Override diff --git a/x-pack/plugin/ml/qa/ml-with-security/src/yamlRestTest/java/org/elasticsearch/smoketest/MlWithSecurityInsufficientRoleIT.java b/x-pack/plugin/ml/qa/ml-with-security/src/yamlRestTest/java/org/elasticsearch/smoketest/MlWithSecurityInsufficientRoleIT.java index 060f6a65eb870..4720e26e9b32d 100644 --- a/x-pack/plugin/ml/qa/ml-with-security/src/yamlRestTest/java/org/elasticsearch/smoketest/MlWithSecurityInsufficientRoleIT.java +++ b/x-pack/plugin/ml/qa/ml-with-security/src/yamlRestTest/java/org/elasticsearch/smoketest/MlWithSecurityInsufficientRoleIT.java @@ -49,7 +49,7 @@ public void test() throws IOException { List> bodies = doSection.getApiCallSection().getBodies(); boolean containsInferenceAgg = false; for (Map body : bodies) { - Map aggs = (Map)body.get("aggs"); + Map aggs = (Map) body.get("aggs"); containsInferenceAgg = containsInferenceAgg || containsKey("inference", aggs); } @@ -61,25 +61,29 @@ public void test() throws IOException { } } catch (AssertionError ae) { - // Some tests assert on searches of wildcarded ML indices rather than on ML endpoints. For these we expect no hits. + // Some tests assert on searches of wildcarded ML indices rather than on ML endpoints. For these we expect no hits. if (ae.getMessage().contains("hits.total didn't match expected value")) { assertThat(ae.getMessage(), containsString("but was Integer [0]")); } else { - assertThat(ae.getMessage(), - either(containsString("action [cluster:monitor/xpack/ml")) - .or(containsString("action [cluster:admin/xpack/ml")) - .or(containsString("security_exception"))); + assertThat( + ae.getMessage(), + either(containsString("action [cluster:monitor/xpack/ml")).or(containsString("action [cluster:admin/xpack/ml")) + .or(containsString("security_exception")) + ); assertThat(ae.getMessage(), containsString("returned [403 Forbidden]")); - assertThat(ae.getMessage(), - either(containsString("is unauthorized for user [no_ml]")) - .or(containsString("user [no_ml] does not have the privilege to get trained models"))); + assertThat( + ae.getMessage(), + either(containsString("is unauthorized for user [no_ml]")).or( + containsString("user [no_ml] does not have the privilege to get trained models") + ) + ); } } } @Override protected String[] getCredentials() { - return new String[]{"no_ml", "x-pack-test-password"}; + return new String[] { "no_ml", "x-pack-test-password" }; } @SuppressWarnings("unchecked") @@ -90,8 +94,8 @@ static boolean containsKey(String key, Map mapOfMaps) { Set> entries = mapOfMaps.entrySet(); for (Map.Entry entry : entries) { - if (entry.getValue() instanceof Map) { - boolean isInNestedMap = containsKey(key, (Map)entry.getValue()); + if (entry.getValue() instanceof Map) { + boolean isInNestedMap = containsKey(key, (Map) entry.getValue()); if (isInNestedMap) { return true; } @@ -101,4 +105,3 @@ static boolean containsKey(String key, Map mapOfMaps) { return false; } } - diff --git a/x-pack/plugin/ml/qa/ml-with-security/src/yamlRestTest/java/org/elasticsearch/smoketest/MlWithSecurityUserRoleIT.java b/x-pack/plugin/ml/qa/ml-with-security/src/yamlRestTest/java/org/elasticsearch/smoketest/MlWithSecurityUserRoleIT.java index de81131d452bd..970bc8218bfae 100644 --- a/x-pack/plugin/ml/qa/ml-with-security/src/yamlRestTest/java/org/elasticsearch/smoketest/MlWithSecurityUserRoleIT.java +++ b/x-pack/plugin/ml/qa/ml-with-security/src/yamlRestTest/java/org/elasticsearch/smoketest/MlWithSecurityUserRoleIT.java @@ -7,6 +7,7 @@ package org.elasticsearch.smoketest; import com.carrotsearch.randomizedtesting.annotations.Name; + import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate; import org.elasticsearch.test.rest.yaml.section.DoSection; import org.elasticsearch.test.rest.yaml.section.ExecutableSection; @@ -54,10 +55,11 @@ public void test() throws IOException { } } } catch (AssertionError ae) { - assertThat(ae.getMessage(), - either(containsString("action [cluster:monitor/xpack/ml")) - .or(containsString("action [cluster:admin/xpack/ml")) - .or(containsString("action [cluster:admin/ingest"))); + assertThat( + ae.getMessage(), + either(containsString("action [cluster:monitor/xpack/ml")).or(containsString("action [cluster:admin/xpack/ml")) + .or(containsString("action [cluster:admin/ingest")) + ); assertThat(ae.getMessage(), containsString("returned [403 Forbidden]")); assertThat(ae.getMessage(), containsString("is unauthorized for user [ml_user]")); } @@ -74,6 +76,6 @@ private static boolean isAllowed(String apiName) { @Override protected String[] getCredentials() { - return new String[]{"ml_user", "x-pack-test-password"}; + return new String[] { "ml_user", "x-pack-test-password" }; } } diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/AutodetectMemoryLimitIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/AutodetectMemoryLimitIT.java index 69d69ab522b13..ada95c7e0034c 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/AutodetectMemoryLimitIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/AutodetectMemoryLimitIT.java @@ -24,10 +24,10 @@ import java.util.Map; import java.util.stream.Collectors; +import static org.hamcrest.Matchers.anyOf; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.lessThan; -import static org.hamcrest.Matchers.anyOf; /** * A set of tests that ensure we comply to the model memory limit @@ -65,7 +65,7 @@ public void testTooManyPartitions() throws Exception { while (timestamp < now) { for (int i = 0; i < 11000; i++) { // It's important that the values used here are either always represented in less than 16 UTF-8 bytes or - // always represented in more than 22 UTF-8 bytes. Otherwise platform differences in when the small string + // always represented in more than 22 UTF-8 bytes. Otherwise platform differences in when the small string // optimisation is used will make the results of this test very different for the different platforms. data.add(createJsonRecord(createRecord(timestamp, String.valueOf(i), ""))); } @@ -80,8 +80,10 @@ public void testTooManyPartitions() throws Exception { ModelSizeStats modelSizeStats = jobStats.getModelSizeStats(); assertThat(modelSizeStats.getModelBytes(), lessThan(32000000L)); assertThat(modelSizeStats.getModelBytes(), greaterThan(24000000L)); - assertThat(modelSizeStats.getMemoryStatus(), anyOf(equalTo(ModelSizeStats.MemoryStatus.SOFT_LIMIT), - equalTo(ModelSizeStats.MemoryStatus.HARD_LIMIT))); + assertThat( + modelSizeStats.getMemoryStatus(), + anyOf(equalTo(ModelSizeStats.MemoryStatus.SOFT_LIMIT), equalTo(ModelSizeStats.MemoryStatus.HARD_LIMIT)) + ); } public void testTooManyByFields() throws Exception { @@ -110,7 +112,7 @@ public void testTooManyByFields() throws Exception { while (timestamp < now) { for (int i = 0; i < 10000; i++) { // It's important that the values used here are either always represented in less than 16 UTF-8 bytes or - // always represented in more than 22 UTF-8 bytes. Otherwise platform differences in when the small string + // always represented in more than 22 UTF-8 bytes. Otherwise platform differences in when the small string // optimisation is used will make the results of this test very different for the different platforms. data.add(createJsonRecord(createRecord(timestamp, String.valueOf(i), ""))); } @@ -156,10 +158,13 @@ public void testTooManyByAndOverFields() throws Exception { List data = new ArrayList<>(); for (int user = 0; user < 10000; user++) { // It's important that the values used here are either always represented in less than 16 UTF-8 bytes or - // always represented in more than 22 UTF-8 bytes. Otherwise platform differences in when the small string + // always represented in more than 22 UTF-8 bytes. Otherwise platform differences in when the small string // optimisation is used will make the results of this test very different for the different platforms. - data.add(createJsonRecord(createRecord( - timestamp, String.valueOf(department) + "_" + String.valueOf(user), String.valueOf(department)))); + data.add( + createJsonRecord( + createRecord(timestamp, String.valueOf(department) + "_" + String.valueOf(user), String.valueOf(department)) + ) + ); } postData(job.getId(), data.stream().collect(Collectors.joining())); } @@ -203,7 +208,7 @@ public void testManyDistinctOverFields() throws Exception { List data = new ArrayList<>(); for (int i = 0; i < 10000; i++) { // It's important that the values used here are either always represented in less than 16 UTF-8 bytes or - // always represented in more than 22 UTF-8 bytes. Otherwise platform differences in when the small string + // always represented in more than 22 UTF-8 bytes. Otherwise platform differences in when the small string // optimisation is used will make the results of this test very different for the different platforms. Map record = new HashMap<>(); record.put("time", timestamp); diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/AutoscalingIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/AutoscalingIT.java index 5618f94cdee15..95526d00bdddf 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/AutoscalingIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/AutoscalingIT.java @@ -63,9 +63,8 @@ public void putSettings() { client().admin() .cluster() .prepareUpdateSettings() - .setTransientSettings(Settings.builder() - .put(MachineLearning.MAX_LAZY_ML_NODES.getKey(), 100) - .put("logger.org.elasticsearch.xpack.ml", "TRACE") + .setTransientSettings( + Settings.builder().put(MachineLearning.MAX_LAZY_ML_NODES.getKey(), 100).put("logger.org.elasticsearch.xpack.ml", "TRACE") ) .get(); } @@ -75,9 +74,8 @@ public void removeSettings() { client().admin() .cluster() .prepareUpdateSettings() - .setTransientSettings(Settings.builder() - .putNull(MachineLearning.MAX_LAZY_ML_NODES.getKey()) - .putNull("logger.org.elasticsearch.xpack.ml") + .setTransientSettings( + Settings.builder().putNull(MachineLearning.MAX_LAZY_ML_NODES.getKey()).putNull("logger.org.elasticsearch.xpack.ml") ) .get(); cleanUp(); @@ -87,49 +85,47 @@ public void removeSettings() { // and that xpack.ml.use_auto_machine_memory_percent is false public void testMLAutoscalingCapacity() throws Exception { SortedMap deciders = new TreeMap<>(); - deciders.put(MlAutoscalingDeciderService.NAME, - Settings.builder().put(MlAutoscalingDeciderService.DOWN_SCALE_DELAY.getKey(), TimeValue.ZERO).build()); + deciders.put( + MlAutoscalingDeciderService.NAME, + Settings.builder().put(MlAutoscalingDeciderService.DOWN_SCALE_DELAY.getKey(), TimeValue.ZERO).build() + ); final PutAutoscalingPolicyAction.Request request = new PutAutoscalingPolicyAction.Request( "ml_test", - new TreeSet<>(Arrays.asList("master","data","ingest","ml")), + new TreeSet<>(Arrays.asList("master", "data", "ingest", "ml")), deciders ); assertAcked(client().execute(PutAutoscalingPolicyAction.INSTANCE, request).actionGet()); - assertBusy(() -> assertMlCapacity( - client().execute( - GetAutoscalingCapacityAction.INSTANCE, - new GetAutoscalingCapacityAction.Request() - ).actionGet(), - "Requesting scale down as tier and/or node size could be smaller", - 0L, - 0L) + assertBusy( + () -> assertMlCapacity( + client().execute(GetAutoscalingCapacityAction.INSTANCE, new GetAutoscalingCapacityAction.Request()).actionGet(), + "Requesting scale down as tier and/or node size could be smaller", + 0L, + 0L + ) ); putJob("job1", 100); putJob("job2", 200); openJob("job1"); openJob("job2"); - long expectedTierBytes = (long)Math.ceil( + long expectedTierBytes = (long) Math.ceil( ByteSizeValue.ofMb(100 + BASELINE_OVERHEAD_MB + 200 + BASELINE_OVERHEAD_MB).getBytes() * 100 / 30.0 ); - long expectedNodeBytes = (long)Math.ceil(ByteSizeValue.ofMb(200 + BASELINE_OVERHEAD_MB).getBytes() * 100 / 30.0); + long expectedNodeBytes = (long) Math.ceil(ByteSizeValue.ofMb(200 + BASELINE_OVERHEAD_MB).getBytes() * 100 / 30.0); assertMlCapacity( - client().execute( - GetAutoscalingCapacityAction.INSTANCE, - new GetAutoscalingCapacityAction.Request() - ).actionGet(), + client().execute(GetAutoscalingCapacityAction.INSTANCE, new GetAutoscalingCapacityAction.Request()).actionGet(), "Requesting scale down as tier and/or node size could be smaller", expectedTierBytes, - expectedNodeBytes); + expectedNodeBytes + ); putJob("bigjob1", 60_000); putJob("bigjob2", 50_000); openJob("bigjob1"); openJob("bigjob2"); - List mlNodes = admin() - .cluster() + List mlNodes = admin().cluster() .prepareNodesInfo() .all() .get() @@ -139,49 +135,41 @@ public void testMLAutoscalingCapacity() throws Exception { .filter(MachineLearning::isMlNode) .collect(Collectors.toList()); NativeMemoryCapacity currentScale = MlAutoscalingDeciderService.currentScale(mlNodes, 30, false); - expectedTierBytes = (long)Math.ceil( - (ByteSizeValue.ofMb(50_000 + BASIC_REQUIREMENT_MB + 60_000 + BASELINE_OVERHEAD_MB).getBytes() - + currentScale.getTier() - ) * 100 / 30.0 + expectedTierBytes = (long) Math.ceil( + (ByteSizeValue.ofMb(50_000 + BASIC_REQUIREMENT_MB + 60_000 + BASELINE_OVERHEAD_MB).getBytes() + currentScale.getTier()) * 100 + / 30.0 ); expectedNodeBytes = (long) (ByteSizeValue.ofMb(60_000 + BASELINE_OVERHEAD_MB).getBytes() * 100 / 30.0); - assertMlCapacity( - client().execute( - GetAutoscalingCapacityAction.INSTANCE, - new GetAutoscalingCapacityAction.Request() - ).actionGet(), + client().execute(GetAutoscalingCapacityAction.INSTANCE, new GetAutoscalingCapacityAction.Request()).actionGet(), "requesting scale up as number of jobs in queues exceeded configured limit", expectedTierBytes, - expectedNodeBytes); + expectedNodeBytes + ); - expectedTierBytes = (long)Math.ceil( + expectedTierBytes = (long) Math.ceil( ByteSizeValue.ofMb(100 + BASELINE_OVERHEAD_MB + 200 + BASELINE_OVERHEAD_MB).getBytes() * 100 / 30.0 ); - expectedNodeBytes = (long)Math.ceil(ByteSizeValue.ofMb(200 + BASELINE_OVERHEAD_MB).getBytes() * 100 / 30.0); + expectedNodeBytes = (long) Math.ceil(ByteSizeValue.ofMb(200 + BASELINE_OVERHEAD_MB).getBytes() * 100 / 30.0); closeJob("bigjob1"); closeJob("bigjob2"); assertMlCapacity( - client().execute( - GetAutoscalingCapacityAction.INSTANCE, - new GetAutoscalingCapacityAction.Request() - ).actionGet(), + client().execute(GetAutoscalingCapacityAction.INSTANCE, new GetAutoscalingCapacityAction.Request()).actionGet(), "Requesting scale down as tier and/or node size could be smaller", expectedTierBytes, - expectedNodeBytes); + expectedNodeBytes + ); closeJob("job1"); closeJob("job2"); assertMlCapacity( - client().execute( - GetAutoscalingCapacityAction.INSTANCE, - new GetAutoscalingCapacityAction.Request() - ).actionGet(), + client().execute(GetAutoscalingCapacityAction.INSTANCE, new GetAutoscalingCapacityAction.Request()).actionGet(), "Requesting scale down as tier and/or node size could be smaller", 0L, - 0L); + 0L + ); } public void testMLAutoscalingForLargeModelAllocation() { @@ -214,8 +202,7 @@ public void testMLAutoscalingForLargeModelAllocation() { long modelSize = ByteSizeValue.ofMb(50_000).getBytes(); putAndStartModelDeployment(modelId, modelSize, AllocationStatus.State.STARTING); - List mlNodes = admin() - .cluster() + List mlNodes = admin().cluster() .prepareNodesInfo() .all() .get() @@ -225,7 +212,7 @@ public void testMLAutoscalingForLargeModelAllocation() { .filter(MachineLearning::isMlNode) .collect(Collectors.toList()); NativeMemoryCapacity currentScale = MlAutoscalingDeciderService.currentScale(mlNodes, 30, false); - expectedTierBytes = (long)Math.ceil( + expectedTierBytes = (long) Math.ceil( (ByteSizeValue.ofMb(50_000 + BASIC_REQUIREMENT_MB).getBytes() + currentScale.getTier()) * 100 / 30.0 ); expectedNodeBytes = (long) (ByteSizeValue.ofMb(50_000 + BASELINE_OVERHEAD_MB).getBytes() * 100 / 30.0); @@ -252,21 +239,13 @@ private void assertMlCapacity(GetAutoscalingCapacityAction.Response capacity, St } private void putJob(String jobId, long limitMb) { - Job.Builder job = - new Job.Builder(jobId) - .setAllowLazyOpen(true) - .setAnalysisLimits(new AnalysisLimits(limitMb, null)) - .setAnalysisConfig( - new AnalysisConfig.Builder((List) null) - .setBucketSpan(TimeValue.timeValueHours(1)) - .setDetectors( - Collections.singletonList( - new Detector.Builder("count", null) - .setPartitionFieldName("user") - .build()))) - .setDataDescription( - new DataDescription.Builder() - .setTimeFormat("epoch")); + Job.Builder job = new Job.Builder(jobId).setAllowLazyOpen(true) + .setAnalysisLimits(new AnalysisLimits(limitMb, null)) + .setAnalysisConfig( + new AnalysisConfig.Builder((List) null).setBucketSpan(TimeValue.timeValueHours(1)) + .setDetectors(Collections.singletonList(new Detector.Builder("count", null).setPartitionFieldName("user").build())) + ) + .setDataDescription(new DataDescription.Builder().setTimeFormat("epoch")); putJob(job); } diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/BasicRenormalizationIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/BasicRenormalizationIT.java index 686d3a23d1c05..50419addbb243 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/BasicRenormalizationIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/BasicRenormalizationIT.java @@ -71,18 +71,17 @@ private void createAndRunJob(String jobId, Long renormalizationWindow) throws Ex Job.Builder job = buildAndRegisterJob(jobId, bucketSpan, renormalizationWindow); openJob(job.getId()); - postData(job.getId(), generateData(startTime, bucketSpan, 50, - bucketIndex -> { - if (bucketIndex == 35) { - // First anomaly is 10 events - return 10; - } else if (bucketIndex == 45) { - // Second anomaly is 100, should get the highest score and should bring the first score down - return 100; - } else { - return 1; - } - }).stream().collect(Collectors.joining())); + postData(job.getId(), generateData(startTime, bucketSpan, 50, bucketIndex -> { + if (bucketIndex == 35) { + // First anomaly is 10 events + return 10; + } else if (bucketIndex == 45) { + // Second anomaly is 100, should get the highest score and should bring the first score down + return 100; + } else { + return 1; + } + }).stream().collect(Collectors.joining())); closeJob(job.getId()); } diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/BulkFailureRetryIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/BulkFailureRetryIT.java index 9dc6dab9c0b12..75fe4c6ea28de 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/BulkFailureRetryIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/BulkFailureRetryIT.java @@ -48,9 +48,7 @@ public class BulkFailureRetryIT extends MlNativeAutodetectIntegTestCase { @Before public void putPastDataIntoIndex() { - client().admin().indices().prepareCreate(index) - .setMapping("time", "type=date", "value", "type=long") - .get(); + client().admin().indices().prepareCreate(index).setMapping("time", "type=date", "value", "type=long").get(); long twoDaysAgo = now - DAY * 2; long threeDaysAgo = now - DAY * 3; writeData(logger, index, 250, threeDaysAgo, twoDaysAgo); @@ -61,13 +59,16 @@ public void cleanUpTest() { client().admin() .cluster() .prepareUpdateSettings() - .setPersistentSettings(Settings.builder() - .putNull("xpack.ml.persist_results_max_retries") - .putNull("logger.org.elasticsearch.xpack.ml.datafeed.DatafeedJob") - .putNull("logger.org.elasticsearch.xpack.ml.job.persistence.JobResultsPersister") - .putNull("logger.org.elasticsearch.xpack.ml.job.process.autodetect.output") - .putNull("logger.org.elasticsearch.xpack.ml.utils.persistence.ResultsPersisterService") - .build()).get(); + .setPersistentSettings( + Settings.builder() + .putNull("xpack.ml.persist_results_max_retries") + .putNull("logger.org.elasticsearch.xpack.ml.datafeed.DatafeedJob") + .putNull("logger.org.elasticsearch.xpack.ml.job.persistence.JobResultsPersister") + .putNull("logger.org.elasticsearch.xpack.ml.job.process.autodetect.output") + .putNull("logger.org.elasticsearch.xpack.ml.utils.persistence.ResultsPersisterService") + .build() + ) + .get(); cleanUp(); } @@ -78,9 +79,10 @@ private void ensureAnomaliesWrite() throws InterruptedException { blockingCall( listener -> client().admin().indices().prepareUpdateSettings(resultsIndex).setSettings(settings).execute(listener), acknowledgedResponseHolder, - exceptionHolder); + exceptionHolder + ); if (exceptionHolder.get() != null) { - fail("FAILED TO MARK ["+ resultsIndex + "] as read-write again" + exceptionHolder.get()); + fail("FAILED TO MARK [" + resultsIndex + "] as read-write again" + exceptionHolder.get()); } } @@ -91,9 +93,10 @@ private void setAnomaliesReadOnlyBlock() throws InterruptedException { blockingCall( listener -> client().admin().indices().prepareUpdateSettings(resultsIndex).setSettings(settings).execute(listener), acknowledgedResponseHolder, - exceptionHolder); + exceptionHolder + ); if (exceptionHolder.get() != null) { - fail("FAILED TO MARK ["+ resultsIndex + "] as read-ONLY: " + exceptionHolder.get()); + fail("FAILED TO MARK [" + resultsIndex + "] as read-ONLY: " + exceptionHolder.get()); } } @@ -101,8 +104,11 @@ public void testBulkFailureRetries() throws Exception { Job.Builder job = createJob(jobId, TimeValue.timeValueMinutes(5), "count", null); job.setResultsIndexName(jobId); - DatafeedConfig.Builder datafeedConfigBuilder = - createDatafeedBuilder(job.getId() + "-datafeed", job.getId(), Collections.singletonList(index)); + DatafeedConfig.Builder datafeedConfigBuilder = createDatafeedBuilder( + job.getId() + "-datafeed", + job.getId(), + Collections.singletonList(index) + ); DatafeedConfig datafeedConfig = datafeedConfigBuilder.build(); putJob(job); openJob(job.getId()); @@ -118,13 +124,16 @@ public void testBulkFailureRetries() throws Exception { client().admin() .cluster() .prepareUpdateSettings() - .setPersistentSettings(Settings.builder() - .put("logger.org.elasticsearch.xpack.ml.datafeed.DatafeedJob", "TRACE") - .put("logger.org.elasticsearch.xpack.ml.job.persistence.JobResultsPersister", "TRACE") - .put("logger.org.elasticsearch.xpack.ml.utils.persistence.ResultsPersisterService", "TRACE") - .put("logger.org.elasticsearch.xpack.ml.job.process.autodetect.output", "TRACE") - .put("xpack.ml.persist_results_max_retries", "15") - .build()).get(); + .setPersistentSettings( + Settings.builder() + .put("logger.org.elasticsearch.xpack.ml.datafeed.DatafeedJob", "TRACE") + .put("logger.org.elasticsearch.xpack.ml.job.persistence.JobResultsPersister", "TRACE") + .put("logger.org.elasticsearch.xpack.ml.utils.persistence.ResultsPersisterService", "TRACE") + .put("logger.org.elasticsearch.xpack.ml.job.process.autodetect.output", "TRACE") + .put("xpack.ml.persist_results_max_retries", "15") + .build() + ) + .get(); setAnomaliesReadOnlyBlock(); @@ -151,8 +160,7 @@ private Job.Builder createJob(String id, TimeValue bucketSpan, String function, dataDescription.setTimeFormat(DataDescription.EPOCH_MS); Detector.Builder d = new Detector.Builder(function, field); - AnalysisConfig.Builder analysisConfig = new AnalysisConfig.Builder(Collections.singletonList(d.build())) - .setBucketSpan(bucketSpan) + AnalysisConfig.Builder analysisConfig = new AnalysisConfig.Builder(Collections.singletonList(d.build())).setBucketSpan(bucketSpan) .setSummaryCountFieldName(summaryCountField); return new Job.Builder().setId(id).setAnalysisConfig(analysisConfig).setDataDescription(dataDescription); @@ -168,9 +176,7 @@ private void writeData(Logger logger, String index, long numDocs, long start, lo indexRequest.source("time", timestamp, "value", i); bulkRequestBuilder.add(indexRequest); } - BulkResponse bulkResponse = bulkRequestBuilder - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .get(); + BulkResponse bulkResponse = bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).get(); if (bulkResponse.hasFailures()) { int failures = 0; for (BulkItemResponse itemResponse : bulkResponse) { @@ -193,20 +199,16 @@ private Bucket getLatestFinalizedBucket(String jobId) { return getBuckets(getBucketsRequest).get(0); } - private void blockingCall(Consumer> function, - AtomicReference response, - AtomicReference error) throws InterruptedException { + private void blockingCall(Consumer> function, AtomicReference response, AtomicReference error) + throws InterruptedException { CountDownLatch latch = new CountDownLatch(1); - ActionListener listener = ActionListener.wrap( - r -> { - response.set(r); - latch.countDown(); - }, - e -> { - error.set(e); - latch.countDown(); - } - ); + ActionListener listener = ActionListener.wrap(r -> { + response.set(r); + latch.countDown(); + }, e -> { + error.set(e); + latch.countDown(); + }); function.accept(listener); latch.await(); diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/CategorizationIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/CategorizationIT.java index 94f9374c96ee7..0d9ab24b351a0 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/CategorizationIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/CategorizationIT.java @@ -13,13 +13,13 @@ import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.support.WriteRequest; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.index.query.QueryBuilders; +import org.elasticsearch.search.SearchHit; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.index.query.QueryBuilders; -import org.elasticsearch.search.SearchHit; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfig; import org.elasticsearch.xpack.core.ml.job.config.AnalysisConfig; import org.elasticsearch.xpack.core.ml.job.config.DataDescription; @@ -64,43 +64,42 @@ public class CategorizationIT extends MlNativeAutodetectIntegTestCase { @Before public void setUpData() { - client().admin().indices().prepareCreate(DATA_INDEX) - .setMapping("time", "type=date,format=epoch_millis", - "msg", "type=text") - .get(); + client().admin().indices().prepareCreate(DATA_INDEX).setMapping("time", "type=date,format=epoch_millis", "msg", "type=text").get(); nowMillis = System.currentTimeMillis(); BulkRequestBuilder bulkRequestBuilder = client().prepareBulk(); IndexRequest indexRequest = new IndexRequest(DATA_INDEX); - indexRequest.source("time", nowMillis - TimeValue.timeValueHours(2).millis(), - "msg", "Node 1 started", - "part", "nodes"); + indexRequest.source("time", nowMillis - TimeValue.timeValueHours(2).millis(), "msg", "Node 1 started", "part", "nodes"); bulkRequestBuilder.add(indexRequest); indexRequest = new IndexRequest(DATA_INDEX); - indexRequest.source("time", nowMillis - TimeValue.timeValueHours(2).millis() + 1, - "msg", "Failed to shutdown [error org.aaaa.bbbb.Cccc line 54 caused by foo exception]", - "part", "shutdowns"); + indexRequest.source( + "time", + nowMillis - TimeValue.timeValueHours(2).millis() + 1, + "msg", + "Failed to shutdown [error org.aaaa.bbbb.Cccc line 54 caused by foo exception]", + "part", + "shutdowns" + ); bulkRequestBuilder.add(indexRequest); indexRequest = new IndexRequest(DATA_INDEX); - indexRequest.source("time", nowMillis - TimeValue.timeValueHours(1).millis(), - "msg", "Node 2 started", - "part", "nodes"); + indexRequest.source("time", nowMillis - TimeValue.timeValueHours(1).millis(), "msg", "Node 2 started", "part", "nodes"); bulkRequestBuilder.add(indexRequest); indexRequest = new IndexRequest(DATA_INDEX); - indexRequest.source("time", nowMillis - TimeValue.timeValueHours(1).millis() + 1, - "msg", "Failed to shutdown [error but this time completely different]", - "part", "shutdowns"); + indexRequest.source( + "time", + nowMillis - TimeValue.timeValueHours(1).millis() + 1, + "msg", + "Failed to shutdown [error but this time completely different]", + "part", + "shutdowns" + ); bulkRequestBuilder.add(indexRequest); indexRequest = new IndexRequest(DATA_INDEX); - indexRequest.source("time", nowMillis, - "msg", "Node 3 started", - "part", "nodes"); + indexRequest.source("time", nowMillis, "msg", "Node 3 started", "part", "nodes"); bulkRequestBuilder.add(indexRequest); - BulkResponse bulkResponse = bulkRequestBuilder - .setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL) - .get(); + BulkResponse bulkResponse = bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL).get(); assertThat(bulkResponse.hasFailures(), is(false)); } @@ -131,16 +130,21 @@ public void testBasicCategorization() throws Exception { assertThat(category1.getExamples(), equalTo(Arrays.asList("Node 1 started", "Node 2 started"))); CategoryDefinition category2 = categories.get(1); - assertThat(category2.getRegex(), - equalTo(".*?Failed.+?to.+?shutdown.+?error.+?org\\.aaaa\\.bbbb\\.Cccc.+?line.+?caused.+?by.+?foo.+?exception.*")); - assertThat(category2.getExamples(), - equalTo(Collections.singletonList("Failed to shutdown [error org.aaaa.bbbb.Cccc line 54 caused by foo exception]"))); + assertThat( + category2.getRegex(), + equalTo(".*?Failed.+?to.+?shutdown.+?error.+?org\\.aaaa\\.bbbb\\.Cccc.+?line.+?caused.+?by.+?foo.+?exception.*") + ); + assertThat( + category2.getExamples(), + equalTo(Collections.singletonList("Failed to shutdown [error org.aaaa.bbbb.Cccc line 54 caused by foo exception]")) + ); CategoryDefinition category3 = categories.get(2); - assertThat(category3.getRegex(), - equalTo(".*?Failed.+?to.+?shutdown.+?error.+?but.+?this.+?time.+?completely.+?different.*")); - assertThat(category3.getExamples(), - equalTo(Collections.singletonList("Failed to shutdown [error but this time completely different]"))); + assertThat(category3.getRegex(), equalTo(".*?Failed.+?to.+?shutdown.+?error.+?but.+?this.+?time.+?completely.+?different.*")); + assertThat( + category3.getExamples(), + equalTo(Collections.singletonList("Failed to shutdown [error but this time completely different]")) + ); List stats = getCategorizerStats(job.getId()); assertThat(stats, hasSize(1)); @@ -190,18 +194,23 @@ public void testPerPartitionCategorization() throws Exception { assertThat(category1.getPartitionFieldValue(), equalTo("nodes")); CategoryDefinition category2 = categories.get(1); - assertThat(category2.getRegex(), - equalTo(".*?Failed.+?to.+?shutdown.+?error.+?org\\.aaaa\\.bbbb\\.Cccc.+?line.+?caused.+?by.+?foo.+?exception.*")); - assertThat(category2.getExamples(), - equalTo(Collections.singletonList("Failed to shutdown [error org.aaaa.bbbb.Cccc line 54 caused by foo exception]"))); + assertThat( + category2.getRegex(), + equalTo(".*?Failed.+?to.+?shutdown.+?error.+?org\\.aaaa\\.bbbb\\.Cccc.+?line.+?caused.+?by.+?foo.+?exception.*") + ); + assertThat( + category2.getExamples(), + equalTo(Collections.singletonList("Failed to shutdown [error org.aaaa.bbbb.Cccc line 54 caused by foo exception]")) + ); assertThat(category2.getPartitionFieldName(), equalTo("part")); assertThat(category2.getPartitionFieldValue(), equalTo("shutdowns")); CategoryDefinition category3 = categories.get(2); - assertThat(category3.getRegex(), - equalTo(".*?Failed.+?to.+?shutdown.+?error.+?but.+?this.+?time.+?completely.+?different.*")); - assertThat(category3.getExamples(), - equalTo(Collections.singletonList("Failed to shutdown [error but this time completely different]"))); + assertThat(category3.getRegex(), equalTo(".*?Failed.+?to.+?shutdown.+?error.+?but.+?this.+?time.+?completely.+?different.*")); + assertThat( + category3.getExamples(), + equalTo(Collections.singletonList("Failed to shutdown [error but this time completely different]")) + ); assertThat(category3.getPartitionFieldName(), equalTo("part")); assertThat(category3.getPartitionFieldValue(), equalTo("shutdowns")); @@ -263,14 +272,19 @@ public void testCategorizationWithFilters() throws Exception { CategoryDefinition category1 = categories.get(0); assertThat(category1.getRegex(), equalTo(".*?Node.+?started.*")); - assertThat(category1.getExamples(), - equalTo(Arrays.asList("Node 1 started", "Node 2 started"))); + assertThat(category1.getExamples(), equalTo(Arrays.asList("Node 1 started", "Node 2 started"))); CategoryDefinition category2 = categories.get(1); assertThat(category2.getRegex(), equalTo(".*?Failed.+?to.+?shutdown.*")); - assertThat(category2.getExamples(), equalTo(Arrays.asList( - "Failed to shutdown [error but this time completely different]", - "Failed to shutdown [error org.aaaa.bbbb.Cccc line 54 caused by foo exception]"))); + assertThat( + category2.getExamples(), + equalTo( + Arrays.asList( + "Failed to shutdown [error but this time completely different]", + "Failed to shutdown [error org.aaaa.bbbb.Cccc line 54 caused by foo exception]" + ) + ) + ); } public void testCategorizationStatePersistedOnSwitchToRealtime() throws Exception { @@ -287,7 +301,7 @@ public void testCategorizationStatePersistedOnSwitchToRealtime() throws Exceptio startDatafeed(datafeedId, 0, null); // When the datafeed switches to realtime the C++ process will be told to persist - // state, and this should include the categorizer state. We assert that this exists + // state, and this should include the categorizer state. We assert that this exists // before closing the job to prove that it was persisted in the background at the // end of lookback rather than when the job was closed. assertBusy(() -> { @@ -308,20 +322,24 @@ public void testCategorizationStatePersistedOnSwitchToRealtime() throws Exceptio CategoryDefinition category1 = categories.get(0); assertThat(category1.getRegex(), equalTo(".*?Node.+?started.*")); - assertThat(category1.getExamples(), - equalTo(Arrays.asList("Node 1 started", "Node 2 started"))); + assertThat(category1.getExamples(), equalTo(Arrays.asList("Node 1 started", "Node 2 started"))); CategoryDefinition category2 = categories.get(1); - assertThat(category2.getRegex(), equalTo(".*?Failed.+?to.+?shutdown.+?error.+?" + - "org\\.aaaa\\.bbbb\\.Cccc.+?line.+?caused.+?by.+?foo.+?exception.*")); - assertThat(category2.getExamples(), equalTo(Collections.singletonList( - "Failed to shutdown [error org.aaaa.bbbb.Cccc line 54 caused by foo exception]"))); + assertThat( + category2.getRegex(), + equalTo(".*?Failed.+?to.+?shutdown.+?error.+?" + "org\\.aaaa\\.bbbb\\.Cccc.+?line.+?caused.+?by.+?foo.+?exception.*") + ); + assertThat( + category2.getExamples(), + equalTo(Collections.singletonList("Failed to shutdown [error org.aaaa.bbbb.Cccc line 54 caused by foo exception]")) + ); CategoryDefinition category3 = categories.get(2); - assertThat(category3.getRegex(), equalTo(".*?Failed.+?to.+?shutdown.+?error.+?but.+?" + - "this.+?time.+?completely.+?different.*")); - assertThat(category3.getExamples(), equalTo(Collections.singletonList( - "Failed to shutdown [error but this time completely different]"))); + assertThat(category3.getRegex(), equalTo(".*?Failed.+?to.+?shutdown.+?error.+?but.+?" + "this.+?time.+?completely.+?different.*")); + assertThat( + category3.getExamples(), + equalTo(Collections.singletonList("Failed to shutdown [error but this time completely different]")) + ); } public void testCategorizationPerformance() { @@ -332,22 +350,20 @@ public void testCategorizationPerformance() { // 4. Run the test several more times // 5. Check the timings that get logged // 6. Revert the changes to this assumption and MachineLearning.CATEGORIZATION_TOKENIZATION_IN_JAVA - assumeTrue("This is time consuming to run on every build - it should be run manually when comparing Java/C++ tokenization", - false); + assumeTrue("This is time consuming to run on every build - it should be run manually when comparing Java/C++ tokenization", false); int testBatchSize = 1000; int testNumBatches = 1000; String[] possibleMessages = new String[] { - " Source LOTS on 33080:817 has shut down.", - " P2PS failed to connect to the hrm server. " - + "Reason: Failed to connect to hrm server - No ACK from SIPC", - " Did not receive an image data for IDN_SELECTFEED:7630.T on 493. " - + "Recalling item. ", - " " - + "RRCP STATUS MSG: RRCP_REBOOT: node 33191 has rebooted", - " Source PRISM_VOBr on 33069:757 has shut down.", - " Service PRISM_VOB has shut down." - }; + " Source LOTS on 33080:817 has shut down.", + " P2PS failed to connect to the hrm server. " + + "Reason: Failed to connect to hrm server - No ACK from SIPC", + " Did not receive an image data for IDN_SELECTFEED:7630.T on 493. " + + "Recalling item. ", + " " + + "RRCP STATUS MSG: RRCP_REBOOT: node 33191 has rebooted", + " Source PRISM_VOBr on 33069:757 has shut down.", + " Service PRISM_VOB has shut down." }; String jobId = "categorization-performance"; Job.Builder job = newJobBuilder(jobId, Collections.emptyList(), false); @@ -359,16 +375,23 @@ public void testCategorizationPerformance() { for (int batchNum = 0; batchNum < testNumBatches; ++batchNum) { StringBuilder json = new StringBuilder(testBatchSize * 100); for (int docNum = 0; docNum < testBatchSize; ++docNum) { - json.append(String.format(Locale.ROOT, "{\"time\":1000000,\"msg\":\"%s\"}\n", - possibleMessages[docNum % possibleMessages.length])); + json.append( + String.format(Locale.ROOT, "{\"time\":1000000,\"msg\":\"%s\"}\n", possibleMessages[docNum % possibleMessages.length]) + ); } postData(jobId, json.toString()); } flushJob(jobId, false); long duration = System.currentTimeMillis() - startTime; - LogManager.getLogger(CategorizationIT.class).info("Performance test with tokenization in " + - (MachineLearning.CATEGORIZATION_TOKENIZATION_IN_JAVA ? "Java" : "C++") + " took " + duration + "ms"); + LogManager.getLogger(CategorizationIT.class) + .info( + "Performance test with tokenization in " + + (MachineLearning.CATEGORIZATION_TOKENIZATION_IN_JAVA ? "Java" : "C++") + + " took " + + duration + + "ms" + ); } public void testStopOnWarn() throws IOException { @@ -387,8 +410,14 @@ public void testStopOnWarn() throws IOException { for (int docNum = 0; docNum < 200; ++docNum) { // Two thirds of our messages are "Node 1 started", the rest "Failed to shutdown" int partitionNum = (docNum % 3) / 2; - json.append(String.format(Locale.ROOT, "{\"time\":1000000,\"part\":\"%s\",\"msg\":\"%s\"}\n", - partitions[partitionNum], messages[partitionNum])); + json.append( + String.format( + Locale.ROOT, + "{\"time\":1000000,\"part\":\"%s\",\"msg\":\"%s\"}\n", + partitions[partitionNum], + messages[partitionNum] + ) + ); } postData(jobId, json.toString()); @@ -461,62 +490,82 @@ public void testStopOnWarn() throws IOException { public void testNumMatchesAndCategoryPreference() throws Exception { String index = "hadoop_logs"; - client().admin().indices().prepareCreate(index) - .setMapping("time", "type=date,format=epoch_millis", - "msg", "type=text") - .get(); + client().admin().indices().prepareCreate(index).setMapping("time", "type=date,format=epoch_millis", "msg", "type=text").get(); nowMillis = System.currentTimeMillis(); BulkRequestBuilder bulkRequestBuilder = client().prepareBulk(); IndexRequest indexRequest = new IndexRequest(index); - indexRequest.source("time", nowMillis - TimeValue.timeValueHours(8).millis(), - "msg", "2015-10-18 18:01:51,963 INFO [main] org.mortbay.log: jetty-6.1.26"); + indexRequest.source( + "time", + nowMillis - TimeValue.timeValueHours(8).millis(), + "msg", + "2015-10-18 18:01:51,963 INFO [main] org.mortbay.log: jetty-6.1.26" + ); bulkRequestBuilder.add(indexRequest); indexRequest = new IndexRequest(index); - indexRequest.source("time", nowMillis - TimeValue.timeValueHours(7).millis(), + indexRequest.source( + "time", + nowMillis - TimeValue.timeValueHours(7).millis(), "msg", - "2015-10-18 18:01:52,728 INFO [main] org.mortbay.log: Started HttpServer2$SelectChannelConnectorWithSafeStartup@0.0.0.0:62267"); + "2015-10-18 18:01:52,728 INFO [main] org.mortbay.log: Started HttpServer2$SelectChannelConnectorWithSafeStartup@0.0.0.0:62267" + ); bulkRequestBuilder.add(indexRequest); indexRequest = new IndexRequest(index); - indexRequest.source("time", nowMillis - TimeValue.timeValueHours(6).millis(), - "msg", "2015-10-18 18:01:53,400 INFO [main] org.apache.hadoop.yarn.webapp.WebApps: Registered webapp guice modules"); + indexRequest.source( + "time", + nowMillis - TimeValue.timeValueHours(6).millis(), + "msg", + "2015-10-18 18:01:53,400 INFO [main] org.apache.hadoop.yarn.webapp.WebApps: Registered webapp guice modules" + ); bulkRequestBuilder.add(indexRequest); indexRequest = new IndexRequest(index); - indexRequest.source("time", nowMillis - TimeValue.timeValueHours(5).millis(), + indexRequest.source( + "time", + nowMillis - TimeValue.timeValueHours(5).millis(), "msg", - "2015-10-18 18:01:53,447 INFO [main] org.apache.hadoop.mapreduce.v2.app.rm.RMContainerRequestor: nodeBlacklistingEnabled:true"); + "2015-10-18 18:01:53,447 INFO [main] org.apache.hadoop.mapreduce.v2.app.rm.RMContainerRequestor: nodeBlacklistingEnabled:true" + ); bulkRequestBuilder.add(indexRequest); indexRequest = new IndexRequest(index); - indexRequest.source("time", nowMillis - TimeValue.timeValueHours(4).millis(), + indexRequest.source( + "time", + nowMillis - TimeValue.timeValueHours(4).millis(), "msg", - "2015-10-18 18:01:52,728 INFO [main] org.apache.hadoop.yarn.webapp.WebApps: Web app /mapreduce started at 62267"); + "2015-10-18 18:01:52,728 INFO [main] org.apache.hadoop.yarn.webapp.WebApps: Web app /mapreduce started at 62267" + ); bulkRequestBuilder.add(indexRequest); indexRequest = new IndexRequest(index); - indexRequest.source("time", nowMillis - TimeValue.timeValueHours(2).millis(), + indexRequest.source( + "time", + nowMillis - TimeValue.timeValueHours(2).millis(), "msg", - "2015-10-18 18:01:53,557 INFO [main] org.apache.hadoop.yarn.client.RMProxy: " + - "Connecting to ResourceManager at msra-sa-41/10.190.173.170:8030"); + "2015-10-18 18:01:53,557 INFO [main] org.apache.hadoop.yarn.client.RMProxy: " + + "Connecting to ResourceManager at msra-sa-41/10.190.173.170:8030" + ); bulkRequestBuilder.add(indexRequest); indexRequest = new IndexRequest(index); - indexRequest.source("time", nowMillis - TimeValue.timeValueHours(1).millis(), + indexRequest.source( + "time", + nowMillis - TimeValue.timeValueHours(1).millis(), "msg", - "2015-10-18 18:01:53,713 INFO [main] org.apache.hadoop.mapreduce.v2.app.rm.RMContainerAllocator: " + - "maxContainerCapability: "); + "2015-10-18 18:01:53,713 INFO [main] org.apache.hadoop.mapreduce.v2.app.rm.RMContainerAllocator: " + + "maxContainerCapability: " + ); bulkRequestBuilder.add(indexRequest); indexRequest = new IndexRequest(index); - indexRequest.source("time", nowMillis, + indexRequest.source( + "time", + nowMillis, "msg", - "2015-10-18 18:01:53,713 INFO [main] org.apache.hadoop.yarn.client.api.impl.ContainerManagementProtocolProxy: " + - "yarn.client.max-cached-nodemanagers-proxies : 0"); + "2015-10-18 18:01:53,713 INFO [main] org.apache.hadoop.yarn.client.api.impl.ContainerManagementProtocolProxy: " + + "yarn.client.max-cached-nodemanagers-proxies : 0" + ); bulkRequestBuilder.add(indexRequest); - - BulkResponse bulkResponse = bulkRequestBuilder - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .get(); + BulkResponse bulkResponse = bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).get(); assertThat(bulkResponse.hasFailures(), is(false)); Job.Builder job = newJobBuilder("categorization-with-preferred-categories", Collections.emptyList(), false); @@ -537,7 +586,7 @@ public void testNumMatchesAndCategoryPreference() throws Exception { CategoryDefinition category1 = categories.get(0); assertThat(category1.getNumMatches(), equalTo(2L)); - long[] expectedPreferenceTo = new long[]{2L, 3L, 4L, 5L, 6L, 7L}; + long[] expectedPreferenceTo = new long[] { 2L, 3L, 4L, 5L, 6L, 7L }; assertThat(category1.getPreferredToCategories(), equalTo(expectedPreferenceTo)); client().admin().indices().prepareDelete(index).get(); } @@ -565,16 +614,24 @@ private static Job.Builder newJobBuilder(String id, List categorizationF private List getCategorizerStats(String jobId) throws IOException { SearchResponse searchResponse = client().prepareSearch(AnomalyDetectorsIndex.jobResultsAliasedName(jobId)) - .setQuery(QueryBuilders.boolQuery() - .filter(QueryBuilders.termQuery(Result.RESULT_TYPE.getPreferredName(), CategorizerStats.RESULT_TYPE_VALUE)) - .filter(QueryBuilders.termQuery(Job.ID.getPreferredName(), jobId))) + .setQuery( + QueryBuilders.boolQuery() + .filter(QueryBuilders.termQuery(Result.RESULT_TYPE.getPreferredName(), CategorizerStats.RESULT_TYPE_VALUE)) + .filter(QueryBuilders.termQuery(Job.ID.getPreferredName(), jobId)) + ) .setSize(1000) .get(); List stats = new ArrayList<>(); for (SearchHit hit : searchResponse.getHits().getHits()) { - try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser( - NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, hit.getSourceRef().streamInput())) { + try ( + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + hit.getSourceRef().streamInput() + ) + ) { stats.add(CategorizerStats.LENIENT_PARSER.apply(parser, null).build()); } } diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ClassificationEvaluationIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ClassificationEvaluationIT.java index b25da00306683..09a39393c2a22 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ClassificationEvaluationIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ClassificationEvaluationIT.java @@ -68,17 +68,14 @@ public void setup() { @After public void cleanup() { cleanUp(); - client().admin().cluster() - .prepareUpdateSettings() - .setPersistentSettings(Settings.builder().putNull("search.max_buckets")) - .get(); + client().admin().cluster().prepareUpdateSettings().setPersistentSettings(Settings.builder().putNull("search.max_buckets")).get(); } public void testEvaluate_DefaultMetrics() { - EvaluateDataFrameAction.Response evaluateDataFrameResponse = - evaluateDataFrame( - ANIMALS_DATA_INDEX, - new Classification(ANIMAL_NAME_KEYWORD_FIELD, ANIMAL_NAME_PREDICTION_KEYWORD_FIELD, null, null)); + EvaluateDataFrameAction.Response evaluateDataFrameResponse = evaluateDataFrame( + ANIMALS_DATA_INDEX, + new Classification(ANIMAL_NAME_KEYWORD_FIELD, ANIMAL_NAME_PREDICTION_KEYWORD_FIELD, null, null) + ); assertThat(evaluateDataFrameResponse.getEvaluationName(), equalTo(Classification.NAME.getPreferredName())); assertThat( @@ -93,14 +90,15 @@ public void testEvaluate_DefaultMetrics() { } public void testEvaluate_AllMetrics() { - EvaluateDataFrameAction.Response evaluateDataFrameResponse = - evaluateDataFrame( - ANIMALS_DATA_INDEX, - new Classification( - ANIMAL_NAME_KEYWORD_FIELD, - ANIMAL_NAME_PREDICTION_KEYWORD_FIELD, - null, - List.of(new Accuracy(), new MulticlassConfusionMatrix(), new Precision(), new Recall()))); + EvaluateDataFrameAction.Response evaluateDataFrameResponse = evaluateDataFrame( + ANIMALS_DATA_INDEX, + new Classification( + ANIMAL_NAME_KEYWORD_FIELD, + ANIMAL_NAME_PREDICTION_KEYWORD_FIELD, + null, + List.of(new Accuracy(), new MulticlassConfusionMatrix(), new Precision(), new Recall()) + ) + ); assertThat(evaluateDataFrameResponse.getEvaluationName(), equalTo(Classification.NAME.getPreferredName())); assertThat( @@ -109,45 +107,50 @@ public void testEvaluate_AllMetrics() { Accuracy.NAME.getPreferredName(), MulticlassConfusionMatrix.NAME.getPreferredName(), Precision.NAME.getPreferredName(), - Recall.NAME.getPreferredName())); + Recall.NAME.getPreferredName() + ) + ); } public void testEvaluate_AllMetrics_KeywordField_CaseSensitivity() { String indexName = "some-index"; String actualField = "fieldA"; String predictedField = "fieldB"; - client().admin().indices().prepareCreate(indexName) - .setMapping( - actualField, "type=keyword", - predictedField, "type=keyword") - .get(); + client().admin().indices().prepareCreate(indexName).setMapping(actualField, "type=keyword", predictedField, "type=keyword").get(); client().prepareIndex(indexName) .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .setSource( - actualField, "crocodile", - predictedField, "cRoCoDiLe") + .setSource(actualField, "crocodile", predictedField, "cRoCoDiLe") .get(); - EvaluateDataFrameAction.Response evaluateDataFrameResponse = - evaluateDataFrame( - indexName, - new Classification( - actualField, - predictedField, - null, - List.of(new Accuracy(), new MulticlassConfusionMatrix(), new Precision(), new Recall()))); + EvaluateDataFrameAction.Response evaluateDataFrameResponse = evaluateDataFrame( + indexName, + new Classification( + actualField, + predictedField, + null, + List.of(new Accuracy(), new MulticlassConfusionMatrix(), new Precision(), new Recall()) + ) + ); Accuracy.Result accuracyResult = (Accuracy.Result) evaluateDataFrameResponse.getMetrics().get(0); assertThat(accuracyResult.getClasses(), contains(new PerClassSingleValue("crocodile", 0.0))); assertThat(accuracyResult.getOverallAccuracy(), equalTo(0.0)); - MulticlassConfusionMatrix.Result confusionMatrixResult = - (MulticlassConfusionMatrix.Result) evaluateDataFrameResponse.getMetrics().get(1); + MulticlassConfusionMatrix.Result confusionMatrixResult = (MulticlassConfusionMatrix.Result) evaluateDataFrameResponse.getMetrics() + .get(1); assertThat( confusionMatrixResult.getConfusionMatrix(), - equalTo(List.of( - new MulticlassConfusionMatrix.ActualClass( - "crocodile", 1, List.of(new MulticlassConfusionMatrix.PredictedClass("crocodile", 0L)), 1)))); + equalTo( + List.of( + new MulticlassConfusionMatrix.ActualClass( + "crocodile", + 1, + List.of(new MulticlassConfusionMatrix.PredictedClass("crocodile", 0L)), + 1 + ) + ) + ) + ); Precision.Result precisionResult = (Precision.Result) evaluateDataFrameResponse.getMetrics().get(2); assertThat(precisionResult.getClasses(), empty()); @@ -159,10 +162,10 @@ public void testEvaluate_AllMetrics_KeywordField_CaseSensitivity() { } private AucRoc.Result evaluateAucRoc(boolean includeCurve) { - EvaluateDataFrameAction.Response evaluateDataFrameResponse = - evaluateDataFrame( - ANIMALS_DATA_INDEX, - new Classification(ANIMAL_NAME_KEYWORD_FIELD, null, ML_TOP_CLASSES_FIELD, List.of(new AucRoc(includeCurve, "cat")))); + EvaluateDataFrameAction.Response evaluateDataFrameResponse = evaluateDataFrame( + ANIMALS_DATA_INDEX, + new Classification(ANIMAL_NAME_KEYWORD_FIELD, null, ML_TOP_CLASSES_FIELD, List.of(new AucRoc(includeCurve, "cat"))) + ); assertThat(evaluateDataFrameResponse.getEvaluationName(), equalTo(Classification.NAME.getPreferredName())); assertThat(evaluateDataFrameResponse.getMetrics(), hasSize(1)); @@ -185,8 +188,10 @@ public void testEvaluate_AucRoc_IncludeCurve() { } private Accuracy.Result evaluateAccuracy(String actualField, String predictedField) { - EvaluateDataFrameAction.Response evaluateDataFrameResponse = - evaluateDataFrame(ANIMALS_DATA_INDEX, new Classification(actualField, predictedField, null, List.of(new Accuracy()))); + EvaluateDataFrameAction.Response evaluateDataFrameResponse = evaluateDataFrame( + ANIMALS_DATA_INDEX, + new Classification(actualField, predictedField, null, List.of(new Accuracy())) + ); assertThat(evaluateDataFrameResponse.getEvaluationName(), equalTo(Classification.NAME.getPreferredName())); assertThat(evaluateDataFrameResponse.getMetrics(), hasSize(1)); @@ -197,13 +202,13 @@ private Accuracy.Result evaluateAccuracy(String actualField, String predictedFie } public void testEvaluate_Accuracy_KeywordField() { - List expectedPerClassResults = - List.of( - new PerClassSingleValue("ant", 47.0 / 75), - new PerClassSingleValue("cat", 47.0 / 75), - new PerClassSingleValue("dog", 47.0 / 75), - new PerClassSingleValue("fox", 47.0 / 75), - new PerClassSingleValue("mouse", 47.0 / 75)); + List expectedPerClassResults = List.of( + new PerClassSingleValue("ant", 47.0 / 75), + new PerClassSingleValue("cat", 47.0 / 75), + new PerClassSingleValue("dog", 47.0 / 75), + new PerClassSingleValue("fox", 47.0 / 75), + new PerClassSingleValue("mouse", 47.0 / 75) + ); double expectedOverallAccuracy = 5.0 / 75; Accuracy.Result accuracyResult = evaluateAccuracy(ANIMAL_NAME_KEYWORD_FIELD, ANIMAL_NAME_PREDICTION_KEYWORD_FIELD); @@ -217,13 +222,13 @@ public void testEvaluate_Accuracy_KeywordField() { } public void testEvaluate_Accuracy_IntegerField() { - List expectedPerClassResults = - List.of( - new PerClassSingleValue("1", 57.0 / 75), - new PerClassSingleValue("2", 54.0 / 75), - new PerClassSingleValue("3", 51.0 / 75), - new PerClassSingleValue("4", 48.0 / 75), - new PerClassSingleValue("5", 45.0 / 75)); + List expectedPerClassResults = List.of( + new PerClassSingleValue("1", 57.0 / 75), + new PerClassSingleValue("2", 54.0 / 75), + new PerClassSingleValue("3", 51.0 / 75), + new PerClassSingleValue("4", 48.0 / 75), + new PerClassSingleValue("5", 45.0 / 75) + ); double expectedOverallAccuracy = 15.0 / 75; Accuracy.Result accuracyResult = evaluateAccuracy(NO_LEGS_INTEGER_FIELD, NO_LEGS_PREDICTION_INTEGER_FIELD); @@ -247,10 +252,10 @@ public void testEvaluate_Accuracy_IntegerField() { } public void testEvaluate_Accuracy_BooleanField() { - List expectedPerClassResults = - List.of( - new PerClassSingleValue("false", 18.0 / 30), - new PerClassSingleValue("true", 27.0 / 45)); + List expectedPerClassResults = List.of( + new PerClassSingleValue("false", 18.0 / 30), + new PerClassSingleValue("true", 27.0 / 45) + ); double expectedOverallAccuracy = 45.0 / 75; Accuracy.Result accuracyResult = evaluateAccuracy(IS_PREDATOR_BOOLEAN_FIELD, IS_PREDATOR_PREDICTION_BOOLEAN_FIELD); @@ -276,13 +281,13 @@ public void testEvaluate_Accuracy_BooleanField() { public void testEvaluate_Accuracy_FieldTypeMismatch() { { // When actual and predicted fields have different types, the sets of classes are disjoint - List expectedPerClassResults = - List.of( - new PerClassSingleValue("1", 0.8), - new PerClassSingleValue("2", 0.8), - new PerClassSingleValue("3", 0.8), - new PerClassSingleValue("4", 0.8), - new PerClassSingleValue("5", 0.8)); + List expectedPerClassResults = List.of( + new PerClassSingleValue("1", 0.8), + new PerClassSingleValue("2", 0.8), + new PerClassSingleValue("3", 0.8), + new PerClassSingleValue("4", 0.8), + new PerClassSingleValue("5", 0.8) + ); double expectedOverallAccuracy = 0.0; Accuracy.Result accuracyResult = evaluateAccuracy(NO_LEGS_INTEGER_FIELD, IS_PREDATOR_BOOLEAN_FIELD); @@ -291,10 +296,10 @@ public void testEvaluate_Accuracy_FieldTypeMismatch() { } { // When actual and predicted fields have different types, the sets of classes are disjoint - List expectedPerClassResults = - List.of( - new PerClassSingleValue("false", 0.6), - new PerClassSingleValue("true", 0.4)); + List expectedPerClassResults = List.of( + new PerClassSingleValue("false", 0.6), + new PerClassSingleValue("true", 0.4) + ); double expectedOverallAccuracy = 0.0; Accuracy.Result accuracyResult = evaluateAccuracy(IS_PREDATOR_BOOLEAN_FIELD, NO_LEGS_INTEGER_FIELD); @@ -304,8 +309,10 @@ public void testEvaluate_Accuracy_FieldTypeMismatch() { } private Precision.Result evaluatePrecision(String actualField, String predictedField) { - EvaluateDataFrameAction.Response evaluateDataFrameResponse = - evaluateDataFrame(ANIMALS_DATA_INDEX, new Classification(actualField, predictedField, null, List.of(new Precision()))); + EvaluateDataFrameAction.Response evaluateDataFrameResponse = evaluateDataFrame( + ANIMALS_DATA_INDEX, + new Classification(actualField, predictedField, null, List.of(new Precision())) + ); assertThat(evaluateDataFrameResponse.getEvaluationName(), equalTo(Classification.NAME.getPreferredName())); assertThat(evaluateDataFrameResponse.getMetrics(), hasSize(1)); @@ -316,13 +323,13 @@ private Precision.Result evaluatePrecision(String actualField, String predictedF } public void testEvaluate_Precision_KeywordField() { - List expectedPerClassResults = - List.of( - new PerClassSingleValue("ant", 1.0 / 15), - new PerClassSingleValue("cat", 1.0 / 15), - new PerClassSingleValue("dog", 1.0 / 15), - new PerClassSingleValue("fox", 1.0 / 15), - new PerClassSingleValue("mouse", 1.0 / 15)); + List expectedPerClassResults = List.of( + new PerClassSingleValue("ant", 1.0 / 15), + new PerClassSingleValue("cat", 1.0 / 15), + new PerClassSingleValue("dog", 1.0 / 15), + new PerClassSingleValue("fox", 1.0 / 15), + new PerClassSingleValue("mouse", 1.0 / 15) + ); double expectedAvgPrecision = 5.0 / 75; Precision.Result precisionResult = evaluatePrecision(ANIMAL_NAME_KEYWORD_FIELD, ANIMAL_NAME_PREDICTION_KEYWORD_FIELD); @@ -333,13 +340,13 @@ public void testEvaluate_Precision_KeywordField() { } public void testEvaluate_Precision_IntegerField() { - List expectedPerClassResults = - List.of( - new PerClassSingleValue("1", 0.2), - new PerClassSingleValue("2", 0.2), - new PerClassSingleValue("3", 0.2), - new PerClassSingleValue("4", 0.2), - new PerClassSingleValue("5", 0.2)); + List expectedPerClassResults = List.of( + new PerClassSingleValue("1", 0.2), + new PerClassSingleValue("2", 0.2), + new PerClassSingleValue("3", 0.2), + new PerClassSingleValue("4", 0.2), + new PerClassSingleValue("5", 0.2) + ); double expectedAvgPrecision = 0.2; Precision.Result precisionResult = evaluatePrecision(NO_LEGS_INTEGER_FIELD, NO_LEGS_PREDICTION_INTEGER_FIELD); @@ -357,10 +364,10 @@ public void testEvaluate_Precision_IntegerField() { } public void testEvaluate_Precision_BooleanField() { - List expectedPerClassResults = - List.of( - new PerClassSingleValue("false", 0.5), - new PerClassSingleValue("true", 9.0 / 13)); + List expectedPerClassResults = List.of( + new PerClassSingleValue("false", 0.5), + new PerClassSingleValue("true", 9.0 / 13) + ); double expectedAvgPrecision = 31.0 / 52; Precision.Result precisionResult = evaluatePrecision(IS_PREDATOR_BOOLEAN_FIELD, IS_PREDATOR_PREDICTION_BOOLEAN_FIELD); @@ -394,18 +401,21 @@ public void testEvaluate_Precision_FieldTypeMismatch() { public void testEvaluate_Precision_CardinalityTooHigh() { indexDistinctAnimals(ANIMALS_DATA_INDEX, 1001); - ElasticsearchStatusException e = - expectThrows( - ElasticsearchStatusException.class, - () -> evaluateDataFrame( - ANIMALS_DATA_INDEX, - new Classification(ANIMAL_NAME_KEYWORD_FIELD, ANIMAL_NAME_PREDICTION_KEYWORD_FIELD, null, List.of(new Precision())))); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> evaluateDataFrame( + ANIMALS_DATA_INDEX, + new Classification(ANIMAL_NAME_KEYWORD_FIELD, ANIMAL_NAME_PREDICTION_KEYWORD_FIELD, null, List.of(new Precision())) + ) + ); assertThat(e.getMessage(), containsString("Cardinality of field [animal_name_keyword] is too high")); } private Recall.Result evaluateRecall(String actualField, String predictedField) { - EvaluateDataFrameAction.Response evaluateDataFrameResponse = - evaluateDataFrame(ANIMALS_DATA_INDEX, new Classification(actualField, predictedField, null, List.of(new Recall()))); + EvaluateDataFrameAction.Response evaluateDataFrameResponse = evaluateDataFrame( + ANIMALS_DATA_INDEX, + new Classification(actualField, predictedField, null, List.of(new Recall())) + ); assertThat(evaluateDataFrameResponse.getEvaluationName(), equalTo(Classification.NAME.getPreferredName())); assertThat(evaluateDataFrameResponse.getMetrics(), hasSize(1)); @@ -416,13 +426,13 @@ private Recall.Result evaluateRecall(String actualField, String predictedField) } public void testEvaluate_Recall_KeywordField() { - List expectedPerClassResults = - List.of( - new PerClassSingleValue("ant", 1.0 / 15), - new PerClassSingleValue("cat", 1.0 / 15), - new PerClassSingleValue ("dog", 1.0 / 15), - new PerClassSingleValue("fox", 1.0 / 15), - new PerClassSingleValue("mouse", 1.0 / 15)); + List expectedPerClassResults = List.of( + new PerClassSingleValue("ant", 1.0 / 15), + new PerClassSingleValue("cat", 1.0 / 15), + new PerClassSingleValue("dog", 1.0 / 15), + new PerClassSingleValue("fox", 1.0 / 15), + new PerClassSingleValue("mouse", 1.0 / 15) + ); double expectedAvgRecall = 5.0 / 75; Recall.Result recallResult = evaluateRecall(ANIMAL_NAME_KEYWORD_FIELD, ANIMAL_NAME_PREDICTION_KEYWORD_FIELD); @@ -433,13 +443,13 @@ public void testEvaluate_Recall_KeywordField() { } public void testEvaluate_Recall_IntegerField() { - List expectedPerClassResults = - List.of( - new PerClassSingleValue("1", 1.0 / 15), - new PerClassSingleValue("2", 2.0 / 15), - new PerClassSingleValue("3", 3.0 / 15), - new PerClassSingleValue("4", 4.0 / 15), - new PerClassSingleValue("5", 5.0 / 15)); + List expectedPerClassResults = List.of( + new PerClassSingleValue("1", 1.0 / 15), + new PerClassSingleValue("2", 2.0 / 15), + new PerClassSingleValue("3", 3.0 / 15), + new PerClassSingleValue("4", 4.0 / 15), + new PerClassSingleValue("5", 5.0 / 15) + ); double expectedAvgRecall = 3.0 / 15; Recall.Result recallResult = evaluateRecall(NO_LEGS_INTEGER_FIELD, NO_LEGS_PREDICTION_INTEGER_FIELD); @@ -457,10 +467,10 @@ public void testEvaluate_Recall_IntegerField() { } public void testEvaluate_Recall_BooleanField() { - List expectedPerClassResults = - List.of( - new PerClassSingleValue("true", 0.6), - new PerClassSingleValue("false", 0.6)); + List expectedPerClassResults = List.of( + new PerClassSingleValue("true", 0.6), + new PerClassSingleValue("false", 0.6) + ); double expectedAvgRecall = 0.6; Recall.Result recallResult = evaluateRecall(IS_PREDATOR_BOOLEAN_FIELD, IS_PREDATOR_PREDICTION_BOOLEAN_FIELD); @@ -480,13 +490,13 @@ public void testEvaluate_Recall_BooleanField() { public void testEvaluate_Recall_FieldTypeMismatch() { { // When actual and predicted fields have different types, the sets of classes are disjoint, hence 0.0 results here - List expectedPerClassResults = - List.of( - new PerClassSingleValue("1", 0.0), - new PerClassSingleValue("2", 0.0), - new PerClassSingleValue("3", 0.0), - new PerClassSingleValue("4", 0.0), - new PerClassSingleValue("5", 0.0)); + List expectedPerClassResults = List.of( + new PerClassSingleValue("1", 0.0), + new PerClassSingleValue("2", 0.0), + new PerClassSingleValue("3", 0.0), + new PerClassSingleValue("4", 0.0), + new PerClassSingleValue("5", 0.0) + ); double expectedAvgRecall = 0.0; Recall.Result recallResult = evaluateRecall(NO_LEGS_INTEGER_FIELD, IS_PREDATOR_BOOLEAN_FIELD); @@ -495,10 +505,10 @@ public void testEvaluate_Recall_FieldTypeMismatch() { } { // When actual and predicted fields have different types, the sets of classes are disjoint, hence 0.0 results here - List expectedPerClassResults = - List.of( - new PerClassSingleValue("true", 0.0), - new PerClassSingleValue("false", 0.0)); + List expectedPerClassResults = List.of( + new PerClassSingleValue("true", 0.0), + new PerClassSingleValue("false", 0.0) + ); double expectedAvgRecall = 0.0; Recall.Result recallResult = evaluateRecall(IS_PREDATOR_BOOLEAN_FIELD, NO_LEGS_INTEGER_FIELD); @@ -509,79 +519,100 @@ public void testEvaluate_Recall_FieldTypeMismatch() { public void testEvaluate_Recall_CardinalityTooHigh() { indexDistinctAnimals(ANIMALS_DATA_INDEX, 1001); - ElasticsearchStatusException e = - expectThrows( - ElasticsearchStatusException.class, - () -> evaluateDataFrame( - ANIMALS_DATA_INDEX, - new Classification(ANIMAL_NAME_KEYWORD_FIELD, ANIMAL_NAME_PREDICTION_KEYWORD_FIELD, null, List.of(new Recall())))); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> evaluateDataFrame( + ANIMALS_DATA_INDEX, + new Classification(ANIMAL_NAME_KEYWORD_FIELD, ANIMAL_NAME_PREDICTION_KEYWORD_FIELD, null, List.of(new Recall())) + ) + ); assertThat(e.getMessage(), containsString("Cardinality of field [animal_name_keyword] is too high")); } private void evaluateMulticlassConfusionMatrix() { - EvaluateDataFrameAction.Response evaluateDataFrameResponse = - evaluateDataFrame( - ANIMALS_DATA_INDEX, - new Classification( - ANIMAL_NAME_KEYWORD_FIELD, - ANIMAL_NAME_PREDICTION_KEYWORD_FIELD, - null, - List.of(new MulticlassConfusionMatrix()))); + EvaluateDataFrameAction.Response evaluateDataFrameResponse = evaluateDataFrame( + ANIMALS_DATA_INDEX, + new Classification( + ANIMAL_NAME_KEYWORD_FIELD, + ANIMAL_NAME_PREDICTION_KEYWORD_FIELD, + null, + List.of(new MulticlassConfusionMatrix()) + ) + ); assertThat(evaluateDataFrameResponse.getEvaluationName(), equalTo(Classification.NAME.getPreferredName())); assertThat(evaluateDataFrameResponse.getMetrics(), hasSize(1)); - MulticlassConfusionMatrix.Result confusionMatrixResult = - (MulticlassConfusionMatrix.Result) evaluateDataFrameResponse.getMetrics().get(0); + MulticlassConfusionMatrix.Result confusionMatrixResult = (MulticlassConfusionMatrix.Result) evaluateDataFrameResponse.getMetrics() + .get(0); assertThat(confusionMatrixResult.getMetricName(), equalTo(MulticlassConfusionMatrix.NAME.getPreferredName())); assertThat( confusionMatrixResult.getConfusionMatrix(), - equalTo(List.of( - new MulticlassConfusionMatrix.ActualClass("ant", - 15, - List.of( - new MulticlassConfusionMatrix.PredictedClass("ant", 1L), - new MulticlassConfusionMatrix.PredictedClass("cat", 4L), - new MulticlassConfusionMatrix.PredictedClass("dog", 3L), - new MulticlassConfusionMatrix.PredictedClass("fox", 2L), - new MulticlassConfusionMatrix.PredictedClass("mouse", 5L)), - 0), - new MulticlassConfusionMatrix.ActualClass("cat", - 15, - List.of( - new MulticlassConfusionMatrix.PredictedClass("ant", 3L), - new MulticlassConfusionMatrix.PredictedClass("cat", 1L), - new MulticlassConfusionMatrix.PredictedClass("dog", 5L), - new MulticlassConfusionMatrix.PredictedClass("fox", 4L), - new MulticlassConfusionMatrix.PredictedClass("mouse", 2L)), - 0), - new MulticlassConfusionMatrix.ActualClass("dog", - 15, - List.of( - new MulticlassConfusionMatrix.PredictedClass("ant", 4L), - new MulticlassConfusionMatrix.PredictedClass("cat", 2L), - new MulticlassConfusionMatrix.PredictedClass("dog", 1L), - new MulticlassConfusionMatrix.PredictedClass("fox", 5L), - new MulticlassConfusionMatrix.PredictedClass("mouse", 3L)), - 0), - new MulticlassConfusionMatrix.ActualClass("fox", - 15, - List.of( - new MulticlassConfusionMatrix.PredictedClass("ant", 5L), - new MulticlassConfusionMatrix.PredictedClass("cat", 3L), - new MulticlassConfusionMatrix.PredictedClass("dog", 2L), - new MulticlassConfusionMatrix.PredictedClass("fox", 1L), - new MulticlassConfusionMatrix.PredictedClass("mouse", 4L)), - 0), - new MulticlassConfusionMatrix.ActualClass("mouse", - 15, - List.of( - new MulticlassConfusionMatrix.PredictedClass("ant", 2L), - new MulticlassConfusionMatrix.PredictedClass("cat", 5L), - new MulticlassConfusionMatrix.PredictedClass("dog", 4L), - new MulticlassConfusionMatrix.PredictedClass("fox", 3L), - new MulticlassConfusionMatrix.PredictedClass("mouse", 1L)), - 0)))); + equalTo( + List.of( + new MulticlassConfusionMatrix.ActualClass( + "ant", + 15, + List.of( + new MulticlassConfusionMatrix.PredictedClass("ant", 1L), + new MulticlassConfusionMatrix.PredictedClass("cat", 4L), + new MulticlassConfusionMatrix.PredictedClass("dog", 3L), + new MulticlassConfusionMatrix.PredictedClass("fox", 2L), + new MulticlassConfusionMatrix.PredictedClass("mouse", 5L) + ), + 0 + ), + new MulticlassConfusionMatrix.ActualClass( + "cat", + 15, + List.of( + new MulticlassConfusionMatrix.PredictedClass("ant", 3L), + new MulticlassConfusionMatrix.PredictedClass("cat", 1L), + new MulticlassConfusionMatrix.PredictedClass("dog", 5L), + new MulticlassConfusionMatrix.PredictedClass("fox", 4L), + new MulticlassConfusionMatrix.PredictedClass("mouse", 2L) + ), + 0 + ), + new MulticlassConfusionMatrix.ActualClass( + "dog", + 15, + List.of( + new MulticlassConfusionMatrix.PredictedClass("ant", 4L), + new MulticlassConfusionMatrix.PredictedClass("cat", 2L), + new MulticlassConfusionMatrix.PredictedClass("dog", 1L), + new MulticlassConfusionMatrix.PredictedClass("fox", 5L), + new MulticlassConfusionMatrix.PredictedClass("mouse", 3L) + ), + 0 + ), + new MulticlassConfusionMatrix.ActualClass( + "fox", + 15, + List.of( + new MulticlassConfusionMatrix.PredictedClass("ant", 5L), + new MulticlassConfusionMatrix.PredictedClass("cat", 3L), + new MulticlassConfusionMatrix.PredictedClass("dog", 2L), + new MulticlassConfusionMatrix.PredictedClass("fox", 1L), + new MulticlassConfusionMatrix.PredictedClass("mouse", 4L) + ), + 0 + ), + new MulticlassConfusionMatrix.ActualClass( + "mouse", + 15, + List.of( + new MulticlassConfusionMatrix.PredictedClass("ant", 2L), + new MulticlassConfusionMatrix.PredictedClass("cat", 5L), + new MulticlassConfusionMatrix.PredictedClass("dog", 4L), + new MulticlassConfusionMatrix.PredictedClass("fox", 3L), + new MulticlassConfusionMatrix.PredictedClass("mouse", 1L) + ), + 0 + ) + ) + ) + ); assertThat(confusionMatrixResult.getOtherActualClassCount(), equalTo(0L)); } @@ -603,92 +634,128 @@ public void testEvaluate_ConfusionMatrixMetricWithDefaultSize() { } public void testEvaluate_ConfusionMatrixMetricWithUserProvidedSize() { - EvaluateDataFrameAction.Response evaluateDataFrameResponse = - evaluateDataFrame( - ANIMALS_DATA_INDEX, - new Classification( - ANIMAL_NAME_KEYWORD_FIELD, - ANIMAL_NAME_PREDICTION_KEYWORD_FIELD, - null, - List.of(new MulticlassConfusionMatrix(3, null)))); + EvaluateDataFrameAction.Response evaluateDataFrameResponse = evaluateDataFrame( + ANIMALS_DATA_INDEX, + new Classification( + ANIMAL_NAME_KEYWORD_FIELD, + ANIMAL_NAME_PREDICTION_KEYWORD_FIELD, + null, + List.of(new MulticlassConfusionMatrix(3, null)) + ) + ); assertThat(evaluateDataFrameResponse.getEvaluationName(), equalTo(Classification.NAME.getPreferredName())); assertThat(evaluateDataFrameResponse.getMetrics(), hasSize(1)); - MulticlassConfusionMatrix.Result confusionMatrixResult = - (MulticlassConfusionMatrix.Result) evaluateDataFrameResponse.getMetrics().get(0); + MulticlassConfusionMatrix.Result confusionMatrixResult = (MulticlassConfusionMatrix.Result) evaluateDataFrameResponse.getMetrics() + .get(0); assertThat(confusionMatrixResult.getMetricName(), equalTo(MulticlassConfusionMatrix.NAME.getPreferredName())); assertThat( confusionMatrixResult.getConfusionMatrix(), - equalTo(List.of( - new MulticlassConfusionMatrix.ActualClass("ant", - 15, - List.of( - new MulticlassConfusionMatrix.PredictedClass("ant", 1L), - new MulticlassConfusionMatrix.PredictedClass("cat", 4L), - new MulticlassConfusionMatrix.PredictedClass("dog", 3L)), - 7), - new MulticlassConfusionMatrix.ActualClass("cat", - 15, - List.of( - new MulticlassConfusionMatrix.PredictedClass("ant", 3L), - new MulticlassConfusionMatrix.PredictedClass("cat", 1L), - new MulticlassConfusionMatrix.PredictedClass("dog", 5L)), - 6), - new MulticlassConfusionMatrix.ActualClass("dog", - 15, - List.of( - new MulticlassConfusionMatrix.PredictedClass("ant", 4L), - new MulticlassConfusionMatrix.PredictedClass("cat", 2L), - new MulticlassConfusionMatrix.PredictedClass("dog", 1L)), - 8)))); + equalTo( + List.of( + new MulticlassConfusionMatrix.ActualClass( + "ant", + 15, + List.of( + new MulticlassConfusionMatrix.PredictedClass("ant", 1L), + new MulticlassConfusionMatrix.PredictedClass("cat", 4L), + new MulticlassConfusionMatrix.PredictedClass("dog", 3L) + ), + 7 + ), + new MulticlassConfusionMatrix.ActualClass( + "cat", + 15, + List.of( + new MulticlassConfusionMatrix.PredictedClass("ant", 3L), + new MulticlassConfusionMatrix.PredictedClass("cat", 1L), + new MulticlassConfusionMatrix.PredictedClass("dog", 5L) + ), + 6 + ), + new MulticlassConfusionMatrix.ActualClass( + "dog", + 15, + List.of( + new MulticlassConfusionMatrix.PredictedClass("ant", 4L), + new MulticlassConfusionMatrix.PredictedClass("cat", 2L), + new MulticlassConfusionMatrix.PredictedClass("dog", 1L) + ), + 8 + ) + ) + ) + ); assertThat(confusionMatrixResult.getOtherActualClassCount(), equalTo(2L)); } static void createAnimalsIndex(String indexName) { - client().admin().indices().prepareCreate(indexName) + client().admin() + .indices() + .prepareCreate(indexName) .setMapping( - ANIMAL_NAME_KEYWORD_FIELD, "type=keyword", - ANIMAL_NAME_PREDICTION_KEYWORD_FIELD, "type=keyword", - NO_LEGS_KEYWORD_FIELD, "type=keyword", - NO_LEGS_INTEGER_FIELD, "type=integer", - NO_LEGS_PREDICTION_INTEGER_FIELD, "type=integer", - IS_PREDATOR_KEYWORD_FIELD, "type=keyword", - IS_PREDATOR_BOOLEAN_FIELD, "type=boolean", - IS_PREDATOR_PREDICTION_BOOLEAN_FIELD, "type=boolean", - IS_PREDATOR_PREDICTION_PROBABILITY_FIELD, "type=double", - ML_TOP_CLASSES_FIELD, "type=nested") + ANIMAL_NAME_KEYWORD_FIELD, + "type=keyword", + ANIMAL_NAME_PREDICTION_KEYWORD_FIELD, + "type=keyword", + NO_LEGS_KEYWORD_FIELD, + "type=keyword", + NO_LEGS_INTEGER_FIELD, + "type=integer", + NO_LEGS_PREDICTION_INTEGER_FIELD, + "type=integer", + IS_PREDATOR_KEYWORD_FIELD, + "type=keyword", + IS_PREDATOR_BOOLEAN_FIELD, + "type=boolean", + IS_PREDATOR_PREDICTION_BOOLEAN_FIELD, + "type=boolean", + IS_PREDATOR_PREDICTION_PROBABILITY_FIELD, + "type=double", + ML_TOP_CLASSES_FIELD, + "type=nested" + ) .get(); } static void indexAnimalsData(String indexName) { List animalNames = List.of("dog", "cat", "mouse", "ant", "fox"); - BulkRequestBuilder bulkRequestBuilder = client().prepareBulk() - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); + BulkRequestBuilder bulkRequestBuilder = client().prepareBulk().setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); for (int i = 0; i < animalNames.size(); i++) { for (int j = 0; j < animalNames.size(); j++) { for (int k = 0; k < j + 1; k++) { - List topClasses = - IntStream - .range(0, 5) - .mapToObj(ix -> new HashMap() {{ - put("class_name", animalNames.get(ix)); - put("class_probability", 0.4 - 0.1 * ix); - }}) - .collect(toList()); + List topClasses = IntStream.range(0, 5).mapToObj(ix -> new HashMap() { + { + put("class_name", animalNames.get(ix)); + put("class_probability", 0.4 - 0.1 * ix); + } + }).collect(toList()); bulkRequestBuilder.add( - new IndexRequest(indexName) - .source( - ANIMAL_NAME_KEYWORD_FIELD, animalNames.get(i), - ANIMAL_NAME_PREDICTION_KEYWORD_FIELD, animalNames.get((i + j) % animalNames.size()), - ANIMAL_NAME_PREDICTION_PROB_FIELD, animalNames.get((i + j) % animalNames.size()), - NO_LEGS_KEYWORD_FIELD, String.valueOf(i + 1), - NO_LEGS_INTEGER_FIELD, i + 1, - NO_LEGS_PREDICTION_INTEGER_FIELD, j + 1, - IS_PREDATOR_KEYWORD_FIELD, String.valueOf(i % 2 == 0), - IS_PREDATOR_BOOLEAN_FIELD, i % 2 == 0, - IS_PREDATOR_PREDICTION_BOOLEAN_FIELD, (i + j) % 2 == 0, - IS_PREDATOR_PREDICTION_PROBABILITY_FIELD, i % 2 == 0 ? 1.0 - 0.1 * i : 0.1 * i, - ML_TOP_CLASSES_FIELD, topClasses)); + new IndexRequest(indexName).source( + ANIMAL_NAME_KEYWORD_FIELD, + animalNames.get(i), + ANIMAL_NAME_PREDICTION_KEYWORD_FIELD, + animalNames.get((i + j) % animalNames.size()), + ANIMAL_NAME_PREDICTION_PROB_FIELD, + animalNames.get((i + j) % animalNames.size()), + NO_LEGS_KEYWORD_FIELD, + String.valueOf(i + 1), + NO_LEGS_INTEGER_FIELD, + i + 1, + NO_LEGS_PREDICTION_INTEGER_FIELD, + j + 1, + IS_PREDATOR_KEYWORD_FIELD, + String.valueOf(i % 2 == 0), + IS_PREDATOR_BOOLEAN_FIELD, + i % 2 == 0, + IS_PREDATOR_PREDICTION_BOOLEAN_FIELD, + (i + j) % 2 == 0, + IS_PREDATOR_PREDICTION_PROBABILITY_FIELD, + i % 2 == 0 ? 1.0 - 0.1 * i : 0.1 * i, + ML_TOP_CLASSES_FIELD, + topClasses + ) + ); } } } @@ -699,12 +766,16 @@ static void indexAnimalsData(String indexName) { } private static void indexDistinctAnimals(String indexName, int distinctAnimalCount) { - BulkRequestBuilder bulkRequestBuilder = client().prepareBulk() - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); + BulkRequestBuilder bulkRequestBuilder = client().prepareBulk().setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); for (int i = 0; i < distinctAnimalCount; i++) { bulkRequestBuilder.add( - new IndexRequest(indexName) - .source(ANIMAL_NAME_KEYWORD_FIELD, "animal_" + i, ANIMAL_NAME_PREDICTION_KEYWORD_FIELD, randomAlphaOfLength(5))); + new IndexRequest(indexName).source( + ANIMAL_NAME_KEYWORD_FIELD, + "animal_" + i, + ANIMAL_NAME_PREDICTION_KEYWORD_FIELD, + randomAlphaOfLength(5) + ) + ); } BulkResponse bulkResponse = bulkRequestBuilder.get(); if (bulkResponse.hasFailures()) { diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ClassificationEvaluationWithSecurityIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ClassificationEvaluationWithSecurityIT.java index 28b8acde02881..a255b72aefd8e 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ClassificationEvaluationWithSecurityIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ClassificationEvaluationWithSecurityIT.java @@ -23,8 +23,10 @@ import java.util.stream.Collectors; public class ClassificationEvaluationWithSecurityIT extends ESRestTestCase { - private static final String BASIC_AUTH_VALUE_SUPER_USER = - UsernamePasswordToken.basicAuthHeaderValue("x_pack_rest_user", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING); + private static final String BASIC_AUTH_VALUE_SUPER_USER = UsernamePasswordToken.basicAuthHeaderValue( + "x_pack_rest_user", + SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING + ); @Override protected Settings restClientSettings() { @@ -33,11 +35,9 @@ protected Settings restClientSettings() { private static void setupDataAccessRole(String index) throws IOException { Request request = new Request("PUT", "/_security/role/test_data_access"); - request.setJsonEntity("{" - + " \"indices\" : [" - + " { \"names\": [\"" + index + "\"], \"privileges\": [\"read\"] }" - + " ]" - + "}"); + request.setJsonEntity( + "{" + " \"indices\" : [" + " { \"names\": [\"" + index + "\"], \"privileges\": [\"read\"] }" + " ]" + "}" + ); client().performRequest(request); } @@ -45,22 +45,23 @@ private void setupUser(String user, List roles) throws IOException { String password = new String(SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING.getChars()); Request request = new Request("PUT", "/_security/user/" + user); - request.setJsonEntity("{" - + " \"password\" : \"" + password + "\"," - + " \"roles\" : [ " + roles.stream().map(unquoted -> "\"" + unquoted + "\"").collect(Collectors.joining(", ")) + " ]" - + "}"); + request.setJsonEntity( + "{" + + " \"password\" : \"" + + password + + "\"," + + " \"roles\" : [ " + + roles.stream().map(unquoted -> "\"" + unquoted + "\"").collect(Collectors.joining(", ")) + + " ]" + + "}" + ); client().performRequest(request); } public void testEvaluate_withSecurity() throws Exception { String index = "test_data"; Request createDoc = new Request("POST", index + "/_doc"); - createDoc.setJsonEntity( - "{\n" + - " \"is_outlier\": 0.0,\n" + - " \"ml.outlier_score\": 1.0\n" + - "}" - ); + createDoc.setJsonEntity("{\n" + " \"is_outlier\": 0.0,\n" + " \"ml.outlier_score\": 1.0\n" + "}"); client().performRequest(createDoc); Request refreshRequest = new Request("POST", index + "/_refresh"); client().performRequest(refreshRequest); @@ -82,15 +83,17 @@ public void testEvaluate_withSecurity() throws Exception { private static Request buildRegressionEval(String index, String primaryHeader, String secondaryHeader) { Request evaluateRequest = new Request("POST", "_ml/data_frame/_evaluate"); evaluateRequest.setJsonEntity( - "{\n" + - " \"index\": \"" + index + "\",\n" + - " \"evaluation\": {\n" + - " \"regression\": {\n" + - " \"actual_field\": \"is_outlier\",\n" + - " \"predicted_field\": \"ml.outlier_score\"\n" + - " }\n" + - " }\n" + - "}\n" + "{\n" + + " \"index\": \"" + + index + + "\",\n" + + " \"evaluation\": {\n" + + " \"regression\": {\n" + + " \"actual_field\": \"is_outlier\",\n" + + " \"predicted_field\": \"ml.outlier_score\"\n" + + " }\n" + + " }\n" + + "}\n" ); RequestOptions.Builder options = evaluateRequest.getOptions().toBuilder(); options.addHeader("Authorization", primaryHeader); diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ClassificationIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ClassificationIT.java index 0119b703638a4..9dac95da3e45f 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ClassificationIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ClassificationIT.java @@ -22,13 +22,13 @@ import org.elasticsearch.common.collect.MapBuilder; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeValue; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchModule; import org.elasticsearch.search.fetch.subphase.FetchSourceContext; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.ml.action.EvaluateDataFrameAction; import org.elasticsearch.xpack.core.ml.action.GetDataFrameAnalyticsStatsAction; import org.elasticsearch.xpack.core.ml.action.GetTrainedModelsAction; @@ -105,22 +105,26 @@ public class ClassificationIT extends MlNativeDataFrameAnalyticsIntegTestCase { @Before public void setupLogging() { - client().admin().cluster() + client().admin() + .cluster() .prepareUpdateSettings() - .setTransientSettings(Settings.builder() - .put("logger.org.elasticsearch.xpack.ml.process", "DEBUG") - .put("logger.org.elasticsearch.xpack.ml.dataframe", "DEBUG") + .setTransientSettings( + Settings.builder() + .put("logger.org.elasticsearch.xpack.ml.process", "DEBUG") + .put("logger.org.elasticsearch.xpack.ml.dataframe", "DEBUG") ) .get(); } @After public void cleanup() { - client().admin().cluster() + client().admin() + .cluster() .prepareUpdateSettings() - .setTransientSettings(Settings.builder() - .putNull("logger.org.elasticsearch.xpack.ml.process") - .putNull("logger.org.elasticsearch.xpack.ml.dataframe") + .setTransientSettings( + Settings.builder() + .putNull("logger.org.elasticsearch.xpack.ml.process") + .putNull("logger.org.elasticsearch.xpack.ml.dataframe") ) .get(); cleanUp(); @@ -140,7 +144,11 @@ public void testSingleNumericFeatureAndMixedTrainingAndNonTrainingRows() throws String predictedClassField = KEYWORD_FIELD + "_prediction"; indexData(sourceIndex, 300, 50, KEYWORD_FIELD); - DataFrameAnalyticsConfig config = buildAnalytics(jobId, sourceIndex, destIndex, null, + DataFrameAnalyticsConfig config = buildAnalytics( + jobId, + sourceIndex, + destIndex, + null, new Classification( KEYWORD_FIELD, BoostedTreeParams.builder().setNumTopFeatureImportanceValues(1).build(), @@ -150,7 +158,9 @@ public void testSingleNumericFeatureAndMixedTrainingAndNonTrainingRows() throws null, null, null, - null)); + null + ) + ); putAnalytics(config); assertIsStopped(jobId); @@ -168,7 +178,7 @@ public void testSingleNumericFeatureAndMixedTrainingAndNonTrainingRows() throws assertThat(getFieldValue(resultsObject, "is_training"), is(destDoc.containsKey(KEYWORD_FIELD))); assertTopClasses(resultsObject, 2, KEYWORD_FIELD, KEYWORD_FIELD_VALUES); @SuppressWarnings("unchecked") - List> importanceArray = (List>)resultsObject.get("feature_importance"); + List> importanceArray = (List>) resultsObject.get("feature_importance"); assertThat(importanceArray, hasSize(greaterThan(0))); } @@ -177,7 +187,8 @@ public void testSingleNumericFeatureAndMixedTrainingAndNonTrainingRows() throws assertModelStatePersisted(stateDocId()); assertExactlyOneInferenceModelPersisted(jobId); assertMlResultsFieldMappings(destIndex, predictedClassField, "keyword"); - assertThatAuditMessagesMatch(jobId, + assertThatAuditMessagesMatch( + jobId, "Created analytics with type [classification]", "Estimated memory usage [", "Starting analytics on node", @@ -188,7 +199,8 @@ public void testSingleNumericFeatureAndMixedTrainingAndNonTrainingRows() throws "Started loading data", "Started analyzing", "Started writing results", - "Finished analysis"); + "Finished analysis" + ); assertEvaluation(KEYWORD_FIELD, KEYWORD_FIELD_VALUES, "ml." + predictedClassField); } @@ -197,7 +209,11 @@ public void testWithDatastreams() throws Exception { String predictedClassField = KEYWORD_FIELD + "_prediction"; indexData(sourceIndex, 300, 50, KEYWORD_FIELD); - DataFrameAnalyticsConfig config = buildAnalytics(jobId, sourceIndex, destIndex, null, + DataFrameAnalyticsConfig config = buildAnalytics( + jobId, + sourceIndex, + destIndex, + null, new Classification( KEYWORD_FIELD, BoostedTreeParams.builder().setNumTopFeatureImportanceValues(1).build(), @@ -207,7 +223,9 @@ public void testWithDatastreams() throws Exception { null, null, null, - null)); + null + ) + ); putAnalytics(config); assertIsStopped(jobId); @@ -225,7 +243,7 @@ public void testWithDatastreams() throws Exception { assertThat(getFieldValue(resultsObject, "is_training"), is(destDoc.containsKey(KEYWORD_FIELD))); assertTopClasses(resultsObject, 2, KEYWORD_FIELD, KEYWORD_FIELD_VALUES); @SuppressWarnings("unchecked") - List> importanceArray = (List>)resultsObject.get("feature_importance"); + List> importanceArray = (List>) resultsObject.get("feature_importance"); assertThat(importanceArray, hasSize(greaterThan(0))); } @@ -234,7 +252,8 @@ public void testWithDatastreams() throws Exception { assertModelStatePersisted(stateDocId()); assertExactlyOneInferenceModelPersisted(jobId); assertMlResultsFieldMappings(destIndex, predictedClassField, "keyword"); - assertThatAuditMessagesMatch(jobId, + assertThatAuditMessagesMatch( + jobId, "Created analytics with type [classification]", "Estimated memory usage [", "Starting analytics on node", @@ -245,7 +264,8 @@ public void testWithDatastreams() throws Exception { "Started loading data", "Started analyzing", "Started writing results", - "Finished analysis"); + "Finished analysis" + ); assertEvaluation(KEYWORD_FIELD, KEYWORD_FIELD_VALUES, "ml." + predictedClassField); } @@ -284,7 +304,8 @@ public void testWithOnlyTrainingRowsAndTrainingPercentIsHundred() throws Excepti assertModelStatePersisted(stateDocId()); assertExactlyOneInferenceModelPersisted(jobId); assertMlResultsFieldMappings(destIndex, predictedClassField, "keyword"); - assertThatAuditMessagesMatch(jobId, + assertThatAuditMessagesMatch( + jobId, "Created analytics with type [classification]", "Estimated memory usage [", "Starting analytics on node", @@ -295,7 +316,8 @@ public void testWithOnlyTrainingRowsAndTrainingPercentIsHundred() throws Excepti "Started loading data", "Started analyzing", "Started writing results", - "Finished analysis"); + "Finished analysis" + ); assertEvaluation(KEYWORD_FIELD, KEYWORD_FIELD_VALUES, "ml." + predictedClassField); } @@ -304,8 +326,11 @@ public void testWithCustomFeatureProcessors() throws Exception { String predictedClassField = KEYWORD_FIELD + "_prediction"; indexData(sourceIndex, 100, 0, KEYWORD_FIELD); - DataFrameAnalyticsConfig config = - buildAnalytics(jobId, sourceIndex, destIndex, null, + DataFrameAnalyticsConfig config = buildAnalytics( + jobId, + sourceIndex, + destIndex, + null, new Classification( KEYWORD_FIELD, BoostedTreeParams.builder().setNumTopFeatureImportanceValues(0).build(), @@ -315,20 +340,42 @@ public void testWithCustomFeatureProcessors() throws Exception { 10.0, 42L, Arrays.asList( - new OneHotEncoding(ALIAS_TO_KEYWORD_FIELD, MapBuilder.newMapBuilder() - .put(KEYWORD_FIELD_VALUES.get(0), "cat_column_custom") - .put(KEYWORD_FIELD_VALUES.get(1), "dog_column_custom").map(), true), - new OneHotEncoding(ALIAS_TO_NESTED_FIELD, MapBuilder.newMapBuilder() - .put(KEYWORD_FIELD_VALUES.get(0), "cat_column_custom_1") - .put(KEYWORD_FIELD_VALUES.get(1), "dog_column_custom_1").map(), true), - new OneHotEncoding(NESTED_FIELD, MapBuilder.newMapBuilder() - .put(KEYWORD_FIELD_VALUES.get(0), "cat_column_custom_2") - .put(KEYWORD_FIELD_VALUES.get(1), "dog_column_custom_2").map(), true), - new OneHotEncoding(TEXT_FIELD, MapBuilder.newMapBuilder() - .put(KEYWORD_FIELD_VALUES.get(0), "cat_column_custom_3") - .put(KEYWORD_FIELD_VALUES.get(1), "dog_column_custom_3").map(), true) + new OneHotEncoding( + ALIAS_TO_KEYWORD_FIELD, + MapBuilder.newMapBuilder() + .put(KEYWORD_FIELD_VALUES.get(0), "cat_column_custom") + .put(KEYWORD_FIELD_VALUES.get(1), "dog_column_custom") + .map(), + true + ), + new OneHotEncoding( + ALIAS_TO_NESTED_FIELD, + MapBuilder.newMapBuilder() + .put(KEYWORD_FIELD_VALUES.get(0), "cat_column_custom_1") + .put(KEYWORD_FIELD_VALUES.get(1), "dog_column_custom_1") + .map(), + true + ), + new OneHotEncoding( + NESTED_FIELD, + MapBuilder.newMapBuilder() + .put(KEYWORD_FIELD_VALUES.get(0), "cat_column_custom_2") + .put(KEYWORD_FIELD_VALUES.get(1), "dog_column_custom_2") + .map(), + true + ), + new OneHotEncoding( + TEXT_FIELD, + MapBuilder.newMapBuilder() + .put(KEYWORD_FIELD_VALUES.get(0), "cat_column_custom_3") + .put(KEYWORD_FIELD_VALUES.get(1), "dog_column_custom_3") + .map(), + true + ) ), - null)); + null + ) + ); putAnalytics(config); assertIsStopped(jobId); @@ -351,7 +398,8 @@ public void testWithCustomFeatureProcessors() throws Exception { assertModelStatePersisted(stateDocId()); assertExactlyOneInferenceModelPersisted(jobId); assertMlResultsFieldMappings(destIndex, predictedClassField, "keyword"); - assertThatAuditMessagesMatch(jobId, + assertThatAuditMessagesMatch( + jobId, "Created analytics with type [classification]", "Estimated memory usage [", "Starting analytics on node", @@ -362,11 +410,14 @@ public void testWithCustomFeatureProcessors() throws Exception { "Started loading data", "Started analyzing", "Started writing results", - "Finished analysis"); + "Finished analysis" + ); assertEvaluation(KEYWORD_FIELD, KEYWORD_FIELD_VALUES, "ml." + predictedClassField); - GetTrainedModelsAction.Response response = client().execute(GetTrainedModelsAction.INSTANCE, - new GetTrainedModelsAction.Request(jobId + "*", Collections.emptyList(), Collections.singleton("definition"))).actionGet(); + GetTrainedModelsAction.Response response = client().execute( + GetTrainedModelsAction.INSTANCE, + new GetTrainedModelsAction.Request(jobId + "*", Collections.emptyList(), Collections.singleton("definition")) + ).actionGet(); assertThat(response.getResources().results().size(), equalTo(1)); TrainedModelConfig modelConfig = response.getResources().results().get(0); modelConfig.ensureParsedDefinition(xContentRegistry()); @@ -381,24 +432,25 @@ public void testWithCustomFeatureProcessors() throws Exception { } } - public void testWithOnlyTrainingRowsAndTrainingPercentIsFifty(String jobId, - String dependentVariable, - List dependentVariableValues, - String expectedMappingTypeForPredictedField) throws Exception { + public void testWithOnlyTrainingRowsAndTrainingPercentIsFifty( + String jobId, + String dependentVariable, + List dependentVariableValues, + String expectedMappingTypeForPredictedField + ) throws Exception { initialize(jobId); String predictedClassField = dependentVariable + "_prediction"; indexData(sourceIndex, 300, 0, dependentVariable); int numTopClasses = randomBoolean() ? 2 : -1; // Occasionally it's worth testing the special value -1. int expectedNumTopClasses = 2; - DataFrameAnalyticsConfig config = - buildAnalytics( - jobId, - sourceIndex, - destIndex, - null, - new Classification(dependentVariable, BoostedTreeParams.builder().build(), null, null, - numTopClasses, 50.0, null, null, null)); + DataFrameAnalyticsConfig config = buildAnalytics( + jobId, + sourceIndex, + destIndex, + null, + new Classification(dependentVariable, BoostedTreeParams.builder().build(), null, null, numTopClasses, 50.0, null, null, null) + ); putAnalytics(config); assertIsStopped(jobId); @@ -442,7 +494,8 @@ public void testWithOnlyTrainingRowsAndTrainingPercentIsFifty(String jobId, assertModelStatePersisted(stateDocId()); assertExactlyOneInferenceModelPersisted(jobId); assertMlResultsFieldMappings(destIndex, predictedClassField, expectedMappingTypeForPredictedField); - assertThatAuditMessagesMatch(jobId, + assertThatAuditMessagesMatch( + jobId, "Created analytics with type [classification]", "Estimated memory usage [", "Starting analytics on node", @@ -453,25 +506,39 @@ public void testWithOnlyTrainingRowsAndTrainingPercentIsFifty(String jobId, "Started loading data", "Started analyzing", "Started writing results", - "Finished analysis"); + "Finished analysis" + ); assertEvaluation(dependentVariable, dependentVariableValues, "ml." + predictedClassField); } public void testWithOnlyTrainingRowsAndTrainingPercentIsFifty_DependentVariableIsKeyword() throws Exception { testWithOnlyTrainingRowsAndTrainingPercentIsFifty( - "classification_training_percent_is_50_keyword", KEYWORD_FIELD, KEYWORD_FIELD_VALUES, "keyword"); + "classification_training_percent_is_50_keyword", + KEYWORD_FIELD, + KEYWORD_FIELD_VALUES, + "keyword" + ); } public void testWithOnlyTrainingRowsAndTrainingPercentIsFifty_DependentVariableIsInteger() throws Exception { testWithOnlyTrainingRowsAndTrainingPercentIsFifty( - "classification_training_percent_is_50_integer", DISCRETE_NUMERICAL_FIELD, DISCRETE_NUMERICAL_FIELD_VALUES, "integer"); + "classification_training_percent_is_50_integer", + DISCRETE_NUMERICAL_FIELD, + DISCRETE_NUMERICAL_FIELD_VALUES, + "integer" + ); } public void testWithOnlyTrainingRowsAndTrainingPercentIsFifty_DependentVariableIsDouble() { ElasticsearchStatusException e = expectThrows( ElasticsearchStatusException.class, () -> testWithOnlyTrainingRowsAndTrainingPercentIsFifty( - "classification_training_percent_is_50_double", NUMERICAL_FIELD, NUMERICAL_FIELD_VALUES, null)); + "classification_training_percent_is_50_double", + NUMERICAL_FIELD, + NUMERICAL_FIELD_VALUES, + null + ) + ); assertThat(e.getMessage(), startsWith("invalid types [double] for required field [numerical-field];")); } @@ -479,18 +546,31 @@ public void testWithOnlyTrainingRowsAndTrainingPercentIsFifty_DependentVariableI ElasticsearchStatusException e = expectThrows( ElasticsearchStatusException.class, () -> testWithOnlyTrainingRowsAndTrainingPercentIsFifty( - "classification_training_percent_is_50_text", TEXT_FIELD, KEYWORD_FIELD_VALUES, null)); + "classification_training_percent_is_50_text", + TEXT_FIELD, + KEYWORD_FIELD_VALUES, + null + ) + ); assertThat(e.getMessage(), startsWith("field [text-field] of type [text] is non-aggregatable")); } public void testWithOnlyTrainingRowsAndTrainingPercentIsFifty_DependentVariableIsTextAndKeyword() throws Exception { testWithOnlyTrainingRowsAndTrainingPercentIsFifty( - "classification_training_percent_is_50_text_and_keyword", TEXT_FIELD + ".keyword", KEYWORD_FIELD_VALUES, "keyword"); + "classification_training_percent_is_50_text_and_keyword", + TEXT_FIELD + ".keyword", + KEYWORD_FIELD_VALUES, + "keyword" + ); } public void testWithOnlyTrainingRowsAndTrainingPercentIsFifty_DependentVariableIsBoolean() throws Exception { testWithOnlyTrainingRowsAndTrainingPercentIsFifty( - "classification_training_percent_is_50_boolean", BOOLEAN_FIELD, BOOLEAN_FIELD_VALUES, "boolean"); + "classification_training_percent_is_50_boolean", + BOOLEAN_FIELD, + BOOLEAN_FIELD_VALUES, + "boolean" + ); } public void testStopAndRestart() throws Exception { @@ -570,10 +650,10 @@ public void testDependentVariableCardinalityTooHighButWithQueryMakesItWithinRang initialize("cardinality_too_high_with_query"); indexData(sourceIndex, 6, 5, KEYWORD_FIELD); // Index one more document with a class different than the two already used. - client().execute(IndexAction.INSTANCE, new IndexRequest(sourceIndex) - .source(KEYWORD_FIELD, "fox") - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)) - .actionGet(); + client().execute( + IndexAction.INSTANCE, + new IndexRequest(sourceIndex).source(KEYWORD_FIELD, "fox").setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + ).actionGet(); QueryBuilder query = QueryBuilders.boolQuery().filter(QueryBuilders.termsQuery(KEYWORD_FIELD, KEYWORD_FIELD_VALUES)); DataFrameAnalyticsConfig config = buildAnalytics(jobId, sourceIndex, destIndex, null, new Classification(KEYWORD_FIELD), query); @@ -660,8 +740,13 @@ public void testTwoJobsWithSameRandomizeSeedUseSameTrainingSet() throws Exceptio .setMaxTrees(1) .build(); - DataFrameAnalyticsConfig firstJob = buildAnalytics(firstJobId, sourceIndex, firstJobDestIndex, null, - new Classification(dependentVariable, boostedTreeParams, null, null, 1, 50.0, null, null, null)); + DataFrameAnalyticsConfig firstJob = buildAnalytics( + firstJobId, + sourceIndex, + firstJobDestIndex, + null, + new Classification(dependentVariable, boostedTreeParams, null, null, 1, 50.0, null, null, null) + ); putAnalytics(firstJob); startAnalytics(firstJobId); waitUntilAnalyticsIsStopped(firstJobId); @@ -670,8 +755,13 @@ public void testTwoJobsWithSameRandomizeSeedUseSameTrainingSet() throws Exceptio String secondJobDestIndex = secondJobId + "_dest"; long randomizeSeed = ((Classification) firstJob.getAnalysis()).getRandomizeSeed(); - DataFrameAnalyticsConfig secondJob = buildAnalytics(secondJobId, sourceIndex, secondJobDestIndex, null, - new Classification(dependentVariable, boostedTreeParams, null, null, 1, 50.0, randomizeSeed, null, null)); + DataFrameAnalyticsConfig secondJob = buildAnalytics( + secondJobId, + sourceIndex, + secondJobDestIndex, + null, + new Classification(dependentVariable, boostedTreeParams, null, null, 1, 50.0, randomizeSeed, null, null) + ); putAnalytics(secondJob); startAnalytics(secondJobId); @@ -744,7 +834,8 @@ public void testSetUpgradeMode_NewTaskDoesNotStart() throws Exception { assertThat(e.status(), is(equalTo(RestStatus.TOO_MANY_REQUESTS))); assertThat( e.getMessage(), - is(equalTo("Cannot perform cluster:admin/xpack/ml/data_frame/analytics/start action while upgrade mode is enabled"))); + is(equalTo("Cannot perform cluster:admin/xpack/ml/data_frame/analytics/start action while upgrade mode is enabled")) + ); assertThat(analyticsTaskList(), is(empty())); assertThat(analyticsAssignedTaskList(), is(empty())); @@ -771,7 +862,9 @@ public void testDeleteExpiredData_RemovesUnusedState() throws Exception { // Delete the config straight from the config index DeleteResponse deleteResponse = client().prepareDelete(".ml-config", DataFrameAnalyticsConfig.documentId(jobId)) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).execute().actionGet(); + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + .execute() + .actionGet(); assertThat(deleteResponse.status(), equalTo(RestStatus.OK)); // Now calling the _delete_expired_data API should remove unused state @@ -804,9 +897,8 @@ public void testTooLowConfiguredMemoryStillStarts() throws Exception { indexData(sourceIndex, 10_000, 0, NESTED_FIELD); DataFrameAnalyticsConfig config = new DataFrameAnalyticsConfig.Builder( - buildAnalytics(jobId, sourceIndex, destIndex, null, new Classification(NESTED_FIELD))) - .setModelMemoryLimit(ByteSizeValue.ofKb(1)) - .build(); + buildAnalytics(jobId, sourceIndex, destIndex, null, new Classification(NESTED_FIELD)) + ).setModelMemoryLimit(ByteSizeValue.ofKb(1)).build(); putAnalytics(config); // Shouldn't throw startAnalytics(jobId); @@ -830,27 +922,31 @@ public void testWithSearchRuntimeMappings() throws Exception { numericRuntimeFieldMapping.put("script", "emit(doc['" + NUMERICAL_FIELD + "'].value)"); Map dependentVariableRuntimeFieldMapping = new HashMap<>(); dependentVariableRuntimeFieldMapping.put("type", "keyword"); - dependentVariableRuntimeFieldMapping.put("script", - "if (doc['" + KEYWORD_FIELD + "'].size() > 0) { emit(doc['" + KEYWORD_FIELD + "'].value); }"); + dependentVariableRuntimeFieldMapping.put( + "script", + "if (doc['" + KEYWORD_FIELD + "'].size() > 0) { emit(doc['" + KEYWORD_FIELD + "'].value); }" + ); Map runtimeFields = new HashMap<>(); runtimeFields.put(numericRuntimeField, numericRuntimeFieldMapping); runtimeFields.put(dependentVariableRuntimeField, dependentVariableRuntimeFieldMapping); - DataFrameAnalyticsConfig config = new DataFrameAnalyticsConfig.Builder() - .setId(jobId) + DataFrameAnalyticsConfig config = new DataFrameAnalyticsConfig.Builder().setId(jobId) .setSource(new DataFrameAnalyticsSource(new String[] { sourceIndex }, null, null, runtimeFields)) .setDest(new DataFrameAnalyticsDest(destIndex, null)) .setAnalyzedFields(new FetchSourceContext(true, new String[] { numericRuntimeField, dependentVariableRuntimeField }, null)) - .setAnalysis(new Classification( - dependentVariableRuntimeField, - BoostedTreeParams.builder().setNumTopFeatureImportanceValues(1).build(), - predictedClassField, - null, - null, - null, - null, - null, - null)) + .setAnalysis( + new Classification( + dependentVariableRuntimeField, + BoostedTreeParams.builder().setNumTopFeatureImportanceValues(1).build(), + predictedClassField, + null, + null, + null, + null, + null, + null + ) + ) .build(); putAnalytics(config); @@ -870,7 +966,7 @@ public void testWithSearchRuntimeMappings() throws Exception { assertThat(getFieldValue(resultsObject, "is_training"), is(destDoc.containsKey(KEYWORD_FIELD))); assertTopClasses(resultsObject, 2, dependentVariableRuntimeField, KEYWORD_FIELD_VALUES); @SuppressWarnings("unchecked") - List> importanceArray = (List>)resultsObject.get("feature_importance"); + List> importanceArray = (List>) resultsObject.get("feature_importance"); assertThat(importanceArray, hasSize(1)); assertThat(importanceArray.get(0), hasEntry("feature_name", numericRuntimeField)); } @@ -880,7 +976,8 @@ public void testWithSearchRuntimeMappings() throws Exception { assertModelStatePersisted(stateDocId()); assertExactlyOneInferenceModelPersisted(jobId); assertMlResultsFieldMappings(destIndex, predictedClassField, "keyword"); - assertThatAuditMessagesMatch(jobId, + assertThatAuditMessagesMatch( + jobId, "Created analytics with type [classification]", "Estimated memory usage [", "Starting analytics on node", @@ -891,7 +988,8 @@ public void testWithSearchRuntimeMappings() throws Exception { "Started loading data", "Started analyzing", "Started writing results", - "Finished analysis"); + "Finished analysis" + ); assertEvaluation(KEYWORD_FIELD, KEYWORD_FIELD_VALUES, "ml." + predictedClassField); } @@ -903,55 +1001,76 @@ public void testPreview() throws Exception { List> preview = previewDataFrame(jobId).getFeatureValues(); for (Map feature : preview) { - assertThat(feature.keySet(), containsInAnyOrder( - BOOLEAN_FIELD, - KEYWORD_FIELD, - NUMERICAL_FIELD, - DISCRETE_NUMERICAL_FIELD, - TEXT_FIELD+".keyword", - NESTED_FIELD, - ALIAS_TO_KEYWORD_FIELD, - ALIAS_TO_NESTED_FIELD - )); + assertThat( + feature.keySet(), + containsInAnyOrder( + BOOLEAN_FIELD, + KEYWORD_FIELD, + NUMERICAL_FIELD, + DISCRETE_NUMERICAL_FIELD, + TEXT_FIELD + ".keyword", + NESTED_FIELD, + ALIAS_TO_KEYWORD_FIELD, + ALIAS_TO_NESTED_FIELD + ) + ); } } public void testPreviewWithProcessors() throws Exception { initialize("processed_preview_analytics"); indexData(sourceIndex, 300, 50, KEYWORD_FIELD); - DataFrameAnalyticsConfig config = - buildAnalytics(jobId, sourceIndex, destIndex, null, - new Classification( - KEYWORD_FIELD, - BoostedTreeParams.builder().setNumTopFeatureImportanceValues(0).build(), - null, - null, - 2, - 10.0, - 42L, - Arrays.asList( - new OneHotEncoding(NESTED_FIELD, MapBuilder.newMapBuilder() + DataFrameAnalyticsConfig config = buildAnalytics( + jobId, + sourceIndex, + destIndex, + null, + new Classification( + KEYWORD_FIELD, + BoostedTreeParams.builder().setNumTopFeatureImportanceValues(0).build(), + null, + null, + 2, + 10.0, + 42L, + Arrays.asList( + new OneHotEncoding( + NESTED_FIELD, + MapBuilder.newMapBuilder() .put(KEYWORD_FIELD_VALUES.get(0), "cat_column_custom_2") - .put(KEYWORD_FIELD_VALUES.get(1), "dog_column_custom_2").map(), true), - new OneHotEncoding(TEXT_FIELD, MapBuilder.newMapBuilder() - .put(KEYWORD_FIELD_VALUES.get(0), "cat_column_custom_3") - .put(KEYWORD_FIELD_VALUES.get(1), "dog_column_custom_3").map(), true) + .put(KEYWORD_FIELD_VALUES.get(1), "dog_column_custom_2") + .map(), + true ), - null)); + new OneHotEncoding( + TEXT_FIELD, + MapBuilder.newMapBuilder() + .put(KEYWORD_FIELD_VALUES.get(0), "cat_column_custom_3") + .put(KEYWORD_FIELD_VALUES.get(1), "dog_column_custom_3") + .map(), + true + ) + ), + null + ) + ); putAnalytics(config); List> preview = previewDataFrame(jobId).getFeatureValues(); for (Map feature : preview) { - assertThat(feature.keySet(), hasItems( - BOOLEAN_FIELD, - KEYWORD_FIELD, - NUMERICAL_FIELD, - DISCRETE_NUMERICAL_FIELD, - "cat_column_custom_2", - "dog_column_custom_2", - "cat_column_custom_3", - "dog_column_custom_3" - )); + assertThat( + feature.keySet(), + hasItems( + BOOLEAN_FIELD, + KEYWORD_FIELD, + NUMERICAL_FIELD, + DISCRETE_NUMERICAL_FIELD, + "cat_column_custom_2", + "dog_column_custom_2", + "cat_column_custom_3", + "dog_column_custom_3" + ) + ); assertThat(feature.keySet(), not(hasItems(NESTED_FIELD, TEXT_FIELD))); } } @@ -977,44 +1096,64 @@ private void initialize(String jobId, boolean isDatastream) { } static void createIndex(String index, boolean isDatastream) { - String mapping = "{\n" + - " \"properties\": {\n" + - " \"@timestamp\": {\n" + - " \"type\": \"date\"\n" + - " }," + - " \""+ BOOLEAN_FIELD + "\": {\n" + - " \"type\": \"boolean\"\n" + - " }," + - " \""+ NUMERICAL_FIELD + "\": {\n" + - " \"type\": \"double\"\n" + - " }," + - " \""+ DISCRETE_NUMERICAL_FIELD + "\": {\n" + - " \"type\": \"integer\"\n" + - " }," + - " \""+ TEXT_FIELD + "\": {\n" + - " \"type\": \"text\",\n" + - " \"fields\": {" + - " \"keyword\": {" + - " \"type\": \"keyword\"\n" + - " }" + - " }" + - " }," + - " \""+ KEYWORD_FIELD + "\": {\n" + - " \"type\": \"keyword\"\n" + - " }," + - " \""+ NESTED_FIELD + "\": {\n" + - " \"type\": \"keyword\"\n" + - " }," + - " \""+ ALIAS_TO_KEYWORD_FIELD + "\": {\n" + - " \"type\": \"alias\",\n" + - " \"path\": \"" + KEYWORD_FIELD + "\"\n" + - " }," + - " \""+ ALIAS_TO_NESTED_FIELD + "\": {\n" + - " \"type\": \"alias\",\n" + - " \"path\": \"" + NESTED_FIELD + "\"\n" + - " }" + - " }\n" + - " }"; + String mapping = "{\n" + + " \"properties\": {\n" + + " \"@timestamp\": {\n" + + " \"type\": \"date\"\n" + + " }," + + " \"" + + BOOLEAN_FIELD + + "\": {\n" + + " \"type\": \"boolean\"\n" + + " }," + + " \"" + + NUMERICAL_FIELD + + "\": {\n" + + " \"type\": \"double\"\n" + + " }," + + " \"" + + DISCRETE_NUMERICAL_FIELD + + "\": {\n" + + " \"type\": \"integer\"\n" + + " }," + + " \"" + + TEXT_FIELD + + "\": {\n" + + " \"type\": \"text\",\n" + + " \"fields\": {" + + " \"keyword\": {" + + " \"type\": \"keyword\"\n" + + " }" + + " }" + + " }," + + " \"" + + KEYWORD_FIELD + + "\": {\n" + + " \"type\": \"keyword\"\n" + + " }," + + " \"" + + NESTED_FIELD + + "\": {\n" + + " \"type\": \"keyword\"\n" + + " }," + + " \"" + + ALIAS_TO_KEYWORD_FIELD + + "\": {\n" + + " \"type\": \"alias\",\n" + + " \"path\": \"" + + KEYWORD_FIELD + + "\"\n" + + " }," + + " \"" + + ALIAS_TO_NESTED_FIELD + + "\": {\n" + + " \"type\": \"alias\",\n" + + " \"path\": \"" + + NESTED_FIELD + + "\"\n" + + " }" + + " }\n" + + " }"; if (isDatastream) { try { createDataStreamAndTemplate(index, mapping); @@ -1022,24 +1161,29 @@ static void createIndex(String index, boolean isDatastream) { throw new ElasticsearchException(ex); } } else { - client().admin().indices().prepareCreate(index) - .setMapping(mapping) - .get(); + client().admin().indices().prepareCreate(index).setMapping(mapping).get(); } } static void indexData(String sourceIndex, int numTrainingRows, int numNonTrainingRows, String dependentVariable) { - BulkRequestBuilder bulkRequestBuilder = client().prepareBulk() - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); + BulkRequestBuilder bulkRequestBuilder = client().prepareBulk().setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); for (int i = 0; i < numTrainingRows; i++) { List source = List.of( - "@timestamp", "2020-12-12", - BOOLEAN_FIELD, BOOLEAN_FIELD_VALUES.get(i % BOOLEAN_FIELD_VALUES.size()), - NUMERICAL_FIELD, NUMERICAL_FIELD_VALUES.get(i % NUMERICAL_FIELD_VALUES.size()), - DISCRETE_NUMERICAL_FIELD, DISCRETE_NUMERICAL_FIELD_VALUES.get(i % DISCRETE_NUMERICAL_FIELD_VALUES.size()), - TEXT_FIELD, KEYWORD_FIELD_VALUES.get(i % KEYWORD_FIELD_VALUES.size()), - KEYWORD_FIELD, KEYWORD_FIELD_VALUES.get(i % KEYWORD_FIELD_VALUES.size()), - NESTED_FIELD, KEYWORD_FIELD_VALUES.get(i % KEYWORD_FIELD_VALUES.size())); + "@timestamp", + "2020-12-12", + BOOLEAN_FIELD, + BOOLEAN_FIELD_VALUES.get(i % BOOLEAN_FIELD_VALUES.size()), + NUMERICAL_FIELD, + NUMERICAL_FIELD_VALUES.get(i % NUMERICAL_FIELD_VALUES.size()), + DISCRETE_NUMERICAL_FIELD, + DISCRETE_NUMERICAL_FIELD_VALUES.get(i % DISCRETE_NUMERICAL_FIELD_VALUES.size()), + TEXT_FIELD, + KEYWORD_FIELD_VALUES.get(i % KEYWORD_FIELD_VALUES.size()), + KEYWORD_FIELD, + KEYWORD_FIELD_VALUES.get(i % KEYWORD_FIELD_VALUES.size()), + NESTED_FIELD, + KEYWORD_FIELD_VALUES.get(i % KEYWORD_FIELD_VALUES.size()) + ); IndexRequest indexRequest = new IndexRequest(sourceIndex).source(source.toArray()).opType(DocWriteRequest.OpType.CREATE); bulkRequestBuilder.add(indexRequest); } @@ -1053,7 +1197,8 @@ static void indexData(String sourceIndex, int numTrainingRows, int numNonTrainin } if (DISCRETE_NUMERICAL_FIELD.equals(dependentVariable) == false) { source.addAll( - List.of(DISCRETE_NUMERICAL_FIELD, DISCRETE_NUMERICAL_FIELD_VALUES.get(i % DISCRETE_NUMERICAL_FIELD_VALUES.size()))); + List.of(DISCRETE_NUMERICAL_FIELD, DISCRETE_NUMERICAL_FIELD_VALUES.get(i % DISCRETE_NUMERICAL_FIELD_VALUES.size())) + ); } if (TEXT_FIELD.equals(dependentVariable) == false) { source.addAll(List.of(TEXT_FIELD, KEYWORD_FIELD_VALUES.get(i % KEYWORD_FIELD_VALUES.size()))); @@ -1086,10 +1231,12 @@ private static Map getDestDoc(DataFrameAnalyticsConfig config, S return destDoc; } - private static void assertTopClasses(Map resultsObject, - int numTopClasses, - String dependentVariable, - List dependentVariableValues) { + private static void assertTopClasses( + Map resultsObject, + int numTopClasses, + String dependentVariable, + List dependentVariableValues + ) { List> topClasses = getFieldValue(resultsObject, "top_classes"); assertThat(topClasses, hasSize(numTopClasses)); List classNames = new ArrayList<>(topClasses.size()); @@ -1116,19 +1263,21 @@ private static void assertTopClasses(Map resultsObject, private void assertEvaluation(String dependentVariable, List dependentVariableValues, String predictedClassField) { List dependentVariableValuesAsStrings = dependentVariableValues.stream().map(String::valueOf).collect(toList()); - EvaluateDataFrameAction.Response evaluateDataFrameResponse = - evaluateDataFrame( - destIndex, - new org.elasticsearch.xpack.core.ml.dataframe.evaluation.classification.Classification( - dependentVariable, - predictedClassField, - null, - Arrays.asList( - new Accuracy(), - new AucRoc(true, dependentVariableValues.get(0).toString()), - new MulticlassConfusionMatrix(), - new Precision(), - new Recall()))); + EvaluateDataFrameAction.Response evaluateDataFrameResponse = evaluateDataFrame( + destIndex, + new org.elasticsearch.xpack.core.ml.dataframe.evaluation.classification.Classification( + dependentVariable, + predictedClassField, + null, + Arrays.asList( + new Accuracy(), + new AucRoc(true, dependentVariableValues.get(0).toString()), + new MulticlassConfusionMatrix(), + new Precision(), + new Recall() + ) + ) + ); assertThat(evaluateDataFrameResponse.getEvaluationName(), equalTo(Classification.NAME.getPreferredName())); assertThat(evaluateDataFrameResponse.getMetrics(), hasSize(5)); @@ -1149,20 +1298,24 @@ private void assertEvaluation(String dependentVariable, List dependentVar } { // MulticlassConfusionMatrix - MulticlassConfusionMatrix.Result confusionMatrixResult = - (MulticlassConfusionMatrix.Result) evaluateDataFrameResponse.getMetrics().get(2); + MulticlassConfusionMatrix.Result confusionMatrixResult = (MulticlassConfusionMatrix.Result) evaluateDataFrameResponse + .getMetrics() + .get(2); assertThat(confusionMatrixResult.getMetricName(), equalTo(MulticlassConfusionMatrix.NAME.getPreferredName())); List actualClasses = confusionMatrixResult.getConfusionMatrix(); assertThat( actualClasses.stream().map(MulticlassConfusionMatrix.ActualClass::getActualClass).collect(toList()), - equalTo(dependentVariableValuesAsStrings)); + equalTo(dependentVariableValuesAsStrings) + ); for (MulticlassConfusionMatrix.ActualClass actualClass : actualClasses) { assertThat(actualClass.getOtherPredictedClassDocCount(), equalTo(0L)); assertThat( - actualClass.getPredictedClasses().stream() + actualClass.getPredictedClasses() + .stream() .map(MulticlassConfusionMatrix.PredictedClass::getPredictedClass) .collect(toList()), - equalTo(dependentVariableValuesAsStrings)); + equalTo(dependentVariableValuesAsStrings) + ); } assertThat(confusionMatrixResult.getOtherActualClassCount(), equalTo(0L)); } diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DataFrameAnalysisCustomFeatureIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DataFrameAnalysisCustomFeatureIT.java index 6da425e60653a..0ef2d7f20a64b 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DataFrameAnalysisCustomFeatureIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DataFrameAnalysisCustomFeatureIT.java @@ -17,11 +17,11 @@ import org.elasticsearch.action.support.WriteRequest; import org.elasticsearch.common.collect.MapBuilder; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchModule; import org.elasticsearch.search.fetch.subphase.FetchSourceContext; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsConfig; import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsDest; import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsSource; @@ -74,22 +74,28 @@ public class DataFrameAnalysisCustomFeatureIT extends MlNativeDataFrameAnalytics @Before public void setupLogging() { - client().admin().cluster() + client().admin() + .cluster() .prepareUpdateSettings() - .setPersistentSettings(Settings.builder() - .put("logger.org.elasticsearch.xpack.ml.dataframe", "DEBUG") - .put("logger.org.elasticsearch.xpack.core.ml.inference", "DEBUG")) + .setPersistentSettings( + Settings.builder() + .put("logger.org.elasticsearch.xpack.ml.dataframe", "DEBUG") + .put("logger.org.elasticsearch.xpack.core.ml.inference", "DEBUG") + ) .get(); } @After public void cleanup() { cleanUp(); - client().admin().cluster() + client().admin() + .cluster() .prepareUpdateSettings() - .setPersistentSettings(Settings.builder() - .putNull("logger.org.elasticsearch.xpack.ml.dataframe") - .putNull("logger.org.elasticsearch.xpack.core.ml.inference")) + .setPersistentSettings( + Settings.builder() + .putNull("logger.org.elasticsearch.xpack.ml.dataframe") + .putNull("logger.org.elasticsearch.xpack.core.ml.inference") + ) .get(); } @@ -107,31 +113,48 @@ public void testNGramCustomFeature() throws Exception { String predictedClassField = NUMERICAL_FIELD + "_prediction"; indexData(sourceIndex, 300, 50, NUMERICAL_FIELD); - DataFrameAnalyticsConfig config = new DataFrameAnalyticsConfig.Builder() - .setId(jobId) - .setSource(new DataFrameAnalyticsSource(new String[] { sourceIndex }, - QueryProvider.fromParsedQuery(QueryBuilders.matchAllQuery()), null, null)) + DataFrameAnalyticsConfig config = new DataFrameAnalyticsConfig.Builder().setId(jobId) + .setSource( + new DataFrameAnalyticsSource( + new String[] { sourceIndex }, + QueryProvider.fromParsedQuery(QueryBuilders.matchAllQuery()), + null, + null + ) + ) .setDest(new DataFrameAnalyticsDest(destIndex, null)) - .setAnalysis(new Regression(NUMERICAL_FIELD, - BoostedTreeParams.builder().setNumTopFeatureImportanceValues(6).build(), - null, - null, - 42L, - null, - null, - Arrays.asList( - new NGram(TEXT_FIELD, "f", new int[]{1}, 0, 2, true), - new Multi(new PreProcessor[]{ - new NGram(TEXT_FIELD, "ngram", new int[]{2}, 0, 3, true), - new FrequencyEncoding("ngram.20", - "frequency", - MapBuilder.newMapBuilder().put("ca", 5.0).put("do", 1.0).map(), true), - new OneHotEncoding("ngram.21", MapBuilder.newMapBuilder().put("at", "is_cat").map(), true) - }, - true) + .setAnalysis( + new Regression( + NUMERICAL_FIELD, + BoostedTreeParams.builder().setNumTopFeatureImportanceValues(6).build(), + null, + null, + 42L, + null, + null, + Arrays.asList( + new NGram(TEXT_FIELD, "f", new int[] { 1 }, 0, 2, true), + new Multi( + new PreProcessor[] { + new NGram(TEXT_FIELD, "ngram", new int[] { 2 }, 0, 3, true), + new FrequencyEncoding( + "ngram.20", + "frequency", + MapBuilder.newMapBuilder().put("ca", 5.0).put("do", 1.0).map(), + true + ), + new OneHotEncoding( + "ngram.21", + MapBuilder.newMapBuilder().put("at", "is_cat").map(), + true + ) }, + true + ) ), - null)) - .setAnalyzedFields(new FetchSourceContext(true, new String[]{TEXT_FIELD, NUMERICAL_FIELD}, new String[]{})) + null + ) + ) + .setAnalyzedFields(new FetchSourceContext(true, new String[] { TEXT_FIELD, NUMERICAL_FIELD }, new String[] {})) .build(); putAnalytics(config); @@ -147,9 +170,11 @@ public void testNGramCustomFeature() throws Exception { Map destDoc = getDestDoc(config, hit); Map resultsObject = getFieldValue(destDoc, "ml"); @SuppressWarnings("unchecked") - List> importanceArray = (List>)resultsObject.get("feature_importance"); - assertThat(importanceArray.stream().map(m -> m.get("feature_name").toString()).collect(Collectors.toSet()), - everyItem(anyOf(startsWith("f."), startsWith("ngram"), equalTo("is_cat"), equalTo("frequency")))); + List> importanceArray = (List>) resultsObject.get("feature_importance"); + assertThat( + importanceArray.stream().map(m -> m.get("feature_name").toString()).collect(Collectors.toSet()), + everyItem(anyOf(startsWith("f."), startsWith("ngram"), equalTo("is_cat"), equalTo("frequency"))) + ); } assertProgressComplete(jobId); @@ -174,39 +199,59 @@ private void initialize(String jobId, boolean isDatastream) { } private static void createIndex(String index, boolean isDatastream) { - String mapping = "{\n" + - " \"properties\": {\n" + - " \"@timestamp\": {\n" + - " \"type\": \"date\"\n" + - " }," + - " \""+ BOOLEAN_FIELD + "\": {\n" + - " \"type\": \"boolean\"\n" + - " }," + - " \""+ NUMERICAL_FIELD + "\": {\n" + - " \"type\": \"double\"\n" + - " }," + - " \""+ DISCRETE_NUMERICAL_FIELD + "\": {\n" + - " \"type\": \"unsigned_long\"\n" + - " }," + - " \""+ TEXT_FIELD + "\": {\n" + - " \"type\": \"text\"\n" + - " }," + - " \""+ KEYWORD_FIELD + "\": {\n" + - " \"type\": \"keyword\"\n" + - " }," + - " \""+ NESTED_FIELD + "\": {\n" + - " \"type\": \"keyword\"\n" + - " }," + - " \""+ ALIAS_TO_KEYWORD_FIELD + "\": {\n" + - " \"type\": \"alias\",\n" + - " \"path\": \"" + KEYWORD_FIELD + "\"\n" + - " }," + - " \""+ ALIAS_TO_NESTED_FIELD + "\": {\n" + - " \"type\": \"alias\",\n" + - " \"path\": \"" + NESTED_FIELD + "\"\n" + - " }" + - " }\n" + - " }"; + String mapping = "{\n" + + " \"properties\": {\n" + + " \"@timestamp\": {\n" + + " \"type\": \"date\"\n" + + " }," + + " \"" + + BOOLEAN_FIELD + + "\": {\n" + + " \"type\": \"boolean\"\n" + + " }," + + " \"" + + NUMERICAL_FIELD + + "\": {\n" + + " \"type\": \"double\"\n" + + " }," + + " \"" + + DISCRETE_NUMERICAL_FIELD + + "\": {\n" + + " \"type\": \"unsigned_long\"\n" + + " }," + + " \"" + + TEXT_FIELD + + "\": {\n" + + " \"type\": \"text\"\n" + + " }," + + " \"" + + KEYWORD_FIELD + + "\": {\n" + + " \"type\": \"keyword\"\n" + + " }," + + " \"" + + NESTED_FIELD + + "\": {\n" + + " \"type\": \"keyword\"\n" + + " }," + + " \"" + + ALIAS_TO_KEYWORD_FIELD + + "\": {\n" + + " \"type\": \"alias\",\n" + + " \"path\": \"" + + KEYWORD_FIELD + + "\"\n" + + " }," + + " \"" + + ALIAS_TO_NESTED_FIELD + + "\": {\n" + + " \"type\": \"alias\",\n" + + " \"path\": \"" + + NESTED_FIELD + + "\"\n" + + " }" + + " }\n" + + " }"; if (isDatastream) { try { createDataStreamAndTemplate(index, mapping); @@ -214,24 +259,29 @@ private static void createIndex(String index, boolean isDatastream) { throw new ElasticsearchException(ex); } } else { - client().admin().indices().prepareCreate(index) - .setMapping(mapping) - .get(); + client().admin().indices().prepareCreate(index).setMapping(mapping).get(); } } private static void indexData(String sourceIndex, int numTrainingRows, int numNonTrainingRows, String dependentVariable) { - BulkRequestBuilder bulkRequestBuilder = client().prepareBulk() - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); + BulkRequestBuilder bulkRequestBuilder = client().prepareBulk().setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); for (int i = 0; i < numTrainingRows; i++) { List source = List.of( - "@timestamp", "2020-12-12", - BOOLEAN_FIELD, BOOLEAN_FIELD_VALUES.get(i % BOOLEAN_FIELD_VALUES.size()), - NUMERICAL_FIELD, NUMERICAL_FIELD_VALUES.get(i % NUMERICAL_FIELD_VALUES.size()), - DISCRETE_NUMERICAL_FIELD, DISCRETE_NUMERICAL_FIELD_VALUES.get(i % DISCRETE_NUMERICAL_FIELD_VALUES.size()), - TEXT_FIELD, KEYWORD_FIELD_VALUES.get(i % KEYWORD_FIELD_VALUES.size()), - KEYWORD_FIELD, KEYWORD_FIELD_VALUES.get(i % KEYWORD_FIELD_VALUES.size()), - NESTED_FIELD, KEYWORD_FIELD_VALUES.get(i % KEYWORD_FIELD_VALUES.size())); + "@timestamp", + "2020-12-12", + BOOLEAN_FIELD, + BOOLEAN_FIELD_VALUES.get(i % BOOLEAN_FIELD_VALUES.size()), + NUMERICAL_FIELD, + NUMERICAL_FIELD_VALUES.get(i % NUMERICAL_FIELD_VALUES.size()), + DISCRETE_NUMERICAL_FIELD, + DISCRETE_NUMERICAL_FIELD_VALUES.get(i % DISCRETE_NUMERICAL_FIELD_VALUES.size()), + TEXT_FIELD, + KEYWORD_FIELD_VALUES.get(i % KEYWORD_FIELD_VALUES.size()), + KEYWORD_FIELD, + KEYWORD_FIELD_VALUES.get(i % KEYWORD_FIELD_VALUES.size()), + NESTED_FIELD, + KEYWORD_FIELD_VALUES.get(i % KEYWORD_FIELD_VALUES.size()) + ); IndexRequest indexRequest = new IndexRequest(sourceIndex).source(source.toArray()).opType(DocWriteRequest.OpType.CREATE); bulkRequestBuilder.add(indexRequest); } @@ -245,7 +295,8 @@ private static void indexData(String sourceIndex, int numTrainingRows, int numNo } if (DISCRETE_NUMERICAL_FIELD.equals(dependentVariable) == false) { source.addAll( - List.of(DISCRETE_NUMERICAL_FIELD, DISCRETE_NUMERICAL_FIELD_VALUES.get(i % DISCRETE_NUMERICAL_FIELD_VALUES.size()))); + List.of(DISCRETE_NUMERICAL_FIELD, DISCRETE_NUMERICAL_FIELD_VALUES.get(i % DISCRETE_NUMERICAL_FIELD_VALUES.size())) + ); } if (TEXT_FIELD.equals(dependentVariable) == false) { source.addAll(List.of(TEXT_FIELD, KEYWORD_FIELD_VALUES.get(i % KEYWORD_FIELD_VALUES.size()))); diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DatafeedJobsIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DatafeedJobsIT.java index de760201ad60e..1ea3d629d9975 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DatafeedJobsIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DatafeedJobsIT.java @@ -13,11 +13,11 @@ import org.elasticsearch.action.admin.cluster.node.hotthreads.NodesHotThreadsResponse; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.support.master.AcknowledgedResponse; -import org.elasticsearch.core.CheckedRunnable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.ConcurrentCollections; +import org.elasticsearch.core.CheckedRunnable; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.aggregations.AggregationBuilders; @@ -75,25 +75,20 @@ public void cleanup() { client().admin() .cluster() .prepareUpdateSettings() - .setPersistentSettings(Settings.builder() - .putNull("logger.org.elasticsearch.xpack.ml.datafeed") - .build()).get(); + .setPersistentSettings(Settings.builder().putNull("logger.org.elasticsearch.xpack.ml.datafeed").build()) + .get(); cleanUp(); } public void testLookbackOnly() throws Exception { - client().admin().indices().prepareCreate("data-1") - .setMapping("time", "type=date") - .get(); + client().admin().indices().prepareCreate("data-1").setMapping("time", "type=date").get(); long numDocs = randomIntBetween(32, 2048); long now = System.currentTimeMillis(); long oneWeekAgo = now - 604800000; long twoWeeksAgo = oneWeekAgo - 604800000; indexDocs(logger, "data-1", numDocs, twoWeeksAgo, oneWeekAgo); - client().admin().indices().prepareCreate("data-2") - .setMapping("time", "type=date") - .get(); + client().admin().indices().prepareCreate("data-2").setMapping("time", "type=date").get(); client().admin().cluster().prepareHealth("data-1", "data-2").setWaitForYellowStatus().get(); long numDocs2 = randomIntBetween(32, 2048); indexDocs(logger, "data-2", numDocs2, oneWeekAgo, now); @@ -124,16 +119,16 @@ public void testLookbackOnly() throws Exception { } public void testLookbackOnlyDataStream() throws Exception { - String mapping = "{\n" + - " \"properties\": {\n" + - " \"time\": {\n" + - " \"type\": \"date\"\n" + - " }," + - " \"@timestamp\": {\n" + - " \"type\": \"date\"\n" + - " }" + - " }\n" + - " }"; + String mapping = "{\n" + + " \"properties\": {\n" + + " \"time\": {\n" + + " \"type\": \"date\"\n" + + " }," + + " \"@timestamp\": {\n" + + " \"type\": \"date\"\n" + + " }" + + " }\n" + + " }"; createDataStreamAndTemplate("datafeed_data_stream", mapping); long numDocs = randomIntBetween(32, 2048); long now = System.currentTimeMillis(); @@ -149,9 +144,11 @@ public void testLookbackOnlyDataStream() throws Exception { openJob(job.getId()); assertBusy(() -> assertEquals(getJobStats(job.getId()).get(0).getState(), JobState.OPENED)); - DatafeedConfig datafeedConfig = createDatafeed(job.getId() + "-datafeed", + DatafeedConfig datafeedConfig = createDatafeed( + job.getId() + "-datafeed", job.getId(), - Collections.singletonList("datafeed_data_stream")); + Collections.singletonList("datafeed_data_stream") + ); putDatafeed(datafeedConfig); startDatafeed(datafeedConfig.getId(), 0L, now); @@ -169,9 +166,7 @@ public void testLookbackOnlyDataStream() throws Exception { } public void testLookbackOnlyRuntimeMapping() throws Exception { - client().admin().indices().prepareCreate("data-1") - .setMapping("time", "type=date") - .get(); + client().admin().indices().prepareCreate("data-1").setMapping("time", "type=date").get(); long numDocs = randomIntBetween(32, 2048); long now = System.currentTimeMillis(); long oneWeekAgo = now - 604800000; @@ -210,7 +205,6 @@ public void testLookbackOnlyRuntimeMapping() throws Exception { DatafeedConfig datafeedConfig = dfBuilder.build(); - putDatafeed(datafeedConfig); startDatafeed(datafeedConfig.getId(), 0L, now); @@ -230,9 +224,7 @@ public void testLookbackOnlyRuntimeMapping() throws Exception { @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/63973") public void testDatafeedTimingStats_DatafeedRecreated() throws Exception { - client().admin().indices().prepareCreate("data") - .setMapping("time", "type=date") - .get(); + client().admin().indices().prepareCreate("data").setMapping("time", "type=date").get(); long numDocs = randomIntBetween(32, 2048); Instant now = Instant.now(); indexDocs(logger, "data", numDocs, now.minus(Duration.ofDays(14)).toEpochMilli(), now.toEpochMilli()); @@ -266,9 +258,7 @@ public void testDatafeedTimingStats_DatafeedRecreated() throws Exception { } public void testDatafeedTimingStats_QueryDelayUpdated_TimingStatsNotReset() throws Exception { - client().admin().indices().prepareCreate("data") - .setMapping("time", "type=date") - .get(); + client().admin().indices().prepareCreate("data").setMapping("time", "type=date").get(); long numDocs = randomIntBetween(32, 2048); Instant now = Instant.now(); indexDocs(logger, "data", numDocs, now.minus(Duration.ofDays(14)).toEpochMilli(), now.toEpochMilli()); @@ -302,13 +292,10 @@ public void testStopAndRestartCompositeDatafeed() throws Exception { client().admin() .cluster() .prepareUpdateSettings() - .setPersistentSettings(Settings.builder() - .put("logger.org.elasticsearch.xpack.ml.datafeed", "TRACE") - .build()).get(); - String indexName = "stop-restart-data"; - client().admin().indices().prepareCreate("stop-restart-data") - .setMapping("time", "type=date") + .setPersistentSettings(Settings.builder().put("logger.org.elasticsearch.xpack.ml.datafeed", "TRACE").build()) .get(); + String indexName = "stop-restart-data"; + client().admin().indices().prepareCreate("stop-restart-data").setMapping("time", "type=date").get(); long numDocs = randomIntBetween(32, 2048); final long intervalMillis = TimeValue.timeValueHours(1).millis(); long now = System.currentTimeMillis(); @@ -344,7 +331,7 @@ public void testStopAndRestartCompositeDatafeed() throws Exception { openJob(scrollJobId); assertBusy(() -> assertEquals(getJobStats(scrollJobId).get(0).getState(), JobState.OPENED)); - DatafeedConfig datafeedConfig = createDatafeedBuilder(scrollJobId+ "-datafeed", scrollJobId, Collections.singletonList(indexName)) + DatafeedConfig datafeedConfig = createDatafeedBuilder(scrollJobId + "-datafeed", scrollJobId, Collections.singletonList(indexName)) .setChunkingConfig(ChunkingConfig.newManual(new TimeValue(1, TimeUnit.SECONDS))) .build(); putDatafeed(datafeedConfig); @@ -373,9 +360,7 @@ public void testStopAndRestartCompositeDatafeed() throws Exception { String compositeJobId = "stop-restart-composite"; Job.Builder compositeJob = createScheduledJob(compositeJobId); - compositeJob.setAnalysisConfig( - new AnalysisConfig.Builder(compositeJob.getAnalysisConfig()).setSummaryCountFieldName("doc_count") - ); + compositeJob.setAnalysisConfig(new AnalysisConfig.Builder(compositeJob.getAnalysisConfig()).setSummaryCountFieldName("doc_count")); putJob(compositeJob); openJob(compositeJobId); assertBusy(() -> assertEquals(getJobStats(compositeJobId).get(0).getState(), JobState.OPENED)); @@ -385,29 +370,23 @@ public void testStopAndRestartCompositeDatafeed() throws Exception { AggregationBuilders.composite( "buckets", Collections.singletonList( - new DateHistogramValuesSourceBuilder("timebucket") - .fixedInterval(new DateHistogramInterval("1h")) - .field("time") + new DateHistogramValuesSourceBuilder("timebucket").fixedInterval(new DateHistogramInterval("1h")).field("time") ) - // Set size to 1 so that start stop actually doesn't page through all the results too quickly + // Set size to 1 so that start stop actually doesn't page through all the results too quickly ).subAggregation(AggregationBuilders.max("time").field("time")).size(1) ); DatafeedConfig compositeDatafeedConfig = createDatafeedBuilder( compositeJobId + "-datafeed", compositeJobId, - Collections.singletonList(indexName)) - .setParsedAggregations(aggs) - .setFrequency(TimeValue.timeValueHours(1)) - .build(); + Collections.singletonList(indexName) + ).setParsedAggregations(aggs).setFrequency(TimeValue.timeValueHours(1)).build(); putDatafeed(compositeDatafeedConfig); startDatafeed(compositeDatafeedConfig.getId(), 0L, null); // Wait until we have processed data assertBusy(() -> assertThat(getDataCounts(compositeJobId).getProcessedRecordCount(), greaterThan(0L))); stopDatafeed(compositeDatafeedConfig.getId()); - assertBusy(() -> - assertThat(getJobStats(compositeJobId).get(0).getState(), is(oneOf(JobState.CLOSED, JobState.OPENED))) - ); + assertBusy(() -> assertThat(getJobStats(compositeJobId).get(0).getState(), is(oneOf(JobState.CLOSED, JobState.OPENED)))); // If we are not OPENED, then we are closed and shouldn't restart as the datafeed finished running through the data if (getJobStats(compositeJobId).get(0).getState().equals(JobState.OPENED)) { aggs = new AggregatorFactories.Builder(); @@ -415,16 +394,11 @@ public void testStopAndRestartCompositeDatafeed() throws Exception { AggregationBuilders.composite( "buckets", Collections.singletonList( - new DateHistogramValuesSourceBuilder("timebucket") - .fixedInterval(new DateHistogramInterval("1h")) - .field("time") + new DateHistogramValuesSourceBuilder("timebucket").fixedInterval(new DateHistogramInterval("1h")).field("time") ) ).subAggregation(AggregationBuilders.max("time").field("time")).size(100) ); - updateDatafeed(new DatafeedUpdate.Builder() - .setId(compositeDatafeedConfig.getId()) - .setParsedAggregations(aggs) - .build()); + updateDatafeed(new DatafeedUpdate.Builder().setId(compositeDatafeedConfig.getId()).setParsedAggregations(aggs).build()); startDatafeed( compositeDatafeedConfig.getId(), randomLongBetween(0, getDataCounts(compositeJobId).getLatestRecordTimeStamp().getTime()), @@ -444,26 +418,34 @@ public void testStopAndRestartCompositeDatafeed() throws Exception { Bucket scrollBucket = scrollBuckets.get(i); Bucket compositeBucket = compositeBuckets.get(i); try { - assertThat("scroll buckets " + scrollBuckets + " composite buckets " + compositeBuckets, + assertThat( + "scroll buckets " + scrollBuckets + " composite buckets " + compositeBuckets, compositeBucket.getTimestamp(), equalTo(scrollBucket.getTimestamp()) ); assertThat( - "composite bucket [" + compositeBucket.getTimestamp() + "] [" + compositeBucket.getEventCount() + "] does not equal" - + " scroll bucket [" + scrollBucket.getTimestamp() + "] [" + scrollBucket.getEventCount() + "]", + "composite bucket [" + + compositeBucket.getTimestamp() + + "] [" + + compositeBucket.getEventCount() + + "] does not equal" + + " scroll bucket [" + + scrollBucket.getTimestamp() + + "] [" + + scrollBucket.getEventCount() + + "]", compositeBucket.getEventCount(), equalTo(scrollBucket.getEventCount()) ); } catch (AssertionError ae) { String originalMessage = ae.getMessage(); try { - SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.rangeQuery("time") - .gte(scrollBucket.getTimestamp().getTime()) - .lte(scrollBucket.getTimestamp().getTime() + TimeValue.timeValueHours(1).getMillis())) - .size(10_000); - SearchHits hits = client().search(new SearchRequest() - .indices(indexName) - .source(builder)).actionGet().getHits(); + SearchSourceBuilder builder = new SearchSourceBuilder().query( + QueryBuilders.rangeQuery("time") + .gte(scrollBucket.getTimestamp().getTime()) + .lte(scrollBucket.getTimestamp().getTime() + TimeValue.timeValueHours(1).getMillis()) + ).size(10_000); + SearchHits hits = client().search(new SearchRequest().indices(indexName).source(builder)).actionGet().getHits(); fail("Hits: " + Strings.arrayToDelimitedString(hits.getHits(), "\n") + " \n failure: " + originalMessage); } catch (ElasticsearchException ee) { fail("could not search indices for better info. Original failure: " + originalMessage); @@ -494,7 +476,7 @@ public void testRealtime() throws Exception { NodesHotThreadsResponse nodesHotThreadsResponse = client().admin().cluster().prepareNodesHotThreads().get(); int i = 0; for (NodeHotThreads nodeHotThreads : nodesHotThreadsResponse.getNodes()) { - logger.info(i++ + ":\n" +nodeHotThreads.getHotThreads()); + logger.info(i++ + ":\n" + nodeHotThreads.getHotThreads()); } throw e; } @@ -517,7 +499,7 @@ public void testCloseJobStopsRealtimeDatafeed() throws Exception { NodesHotThreadsResponse nodesHotThreadsResponse = client().admin().cluster().prepareNodesHotThreads().get(); int i = 0; for (NodeHotThreads nodeHotThreads : nodesHotThreadsResponse.getNodes()) { - logger.info(i++ + ":\n" +nodeHotThreads.getHotThreads()); + logger.info(i++ + ":\n" + nodeHotThreads.getHotThreads()); } throw e; } @@ -533,9 +515,7 @@ public void testCloseJobStopsLookbackOnlyDatafeed() throws Exception { String datafeedId = jobId + "-datafeed"; boolean useForce = randomBoolean(); - client().admin().indices().prepareCreate("data") - .setMapping("time", "type=date") - .get(); + client().admin().indices().prepareCreate("data").setMapping("time", "type=date").get(); long numDocs = randomIntBetween(1024, 2048); long now = System.currentTimeMillis(); long oneWeekAgo = now - 604800000; @@ -566,7 +546,7 @@ public void testCloseJobStopsLookbackOnlyDatafeed() throws Exception { NodesHotThreadsResponse nodesHotThreadsResponse = client().admin().cluster().prepareNodesHotThreads().get(); int i = 0; for (NodeHotThreads nodeHotThreads : nodesHotThreadsResponse.getNodes()) { - logger.info(i++ + ":\n" +nodeHotThreads.getHotThreads()); + logger.info(i++ + ":\n" + nodeHotThreads.getHotThreads()); } throw e; } @@ -626,9 +606,10 @@ public void testRealtime_multipleStopCalls() throws Exception { GetDatafeedsStatsAction.Request request = new GetDatafeedsStatsAction.Request(datafeedId); GetDatafeedsStatsAction.Response response = client().execute(GetDatafeedsStatsAction.INSTANCE, request).actionGet(); if (response.getResponse().results().get(0).getDatafeedState() != DatafeedState.STOPPED) { - exceptions.put(Thread.currentThread().getId(), - new AssertionError("Expected STOPPED datafeed state got " - + response.getResponse().results().get(0).getDatafeedState())); + exceptions.put( + Thread.currentThread().getId(), + new AssertionError("Expected STOPPED datafeed state got " + response.getResponse().results().get(0).getDatafeedState()) + ); } }; @@ -636,14 +617,14 @@ public void testRealtime_multipleStopCalls() throws Exception { // the other to complete. This is difficult to schedule but // hopefully it will happen in CI int numThreads = 5; - Thread [] threads = new Thread[numThreads]; - for (int i=0; i exception = new AtomicReference<>(); // The UI now force deletes datafeeds, which means they can be deleted while running. - // The first step is to isolate the datafeed. But if it was already being stopped then - // the datafeed may not be running by the time the isolate action is executed. This + // The first step is to isolate the datafeed. But if it was already being stopped then + // the datafeed may not be running by the time the isolate action is executed. This // test will sometimes (depending on thread scheduling) achieve this situation and ensure // the code is robust to it. Thread deleteDatafeedThread = new Thread(() -> { @@ -671,8 +652,10 @@ public void testRealtime_givenSimultaneousStopAndForceDelete() throws Throwable AcknowledgedResponse response = client().execute(DeleteDatafeedAction.INSTANCE, request).actionGet(); if (response.isAcknowledged()) { GetDatafeedsStatsAction.Request statsRequest = new GetDatafeedsStatsAction.Request(datafeedId); - expectThrows(ResourceNotFoundException.class, - () -> client().execute(GetDatafeedsStatsAction.INSTANCE, statsRequest).actionGet()); + expectThrows( + ResourceNotFoundException.class, + () -> client().execute(GetDatafeedsStatsAction.INSTANCE, statsRequest).actionGet() + ); } else { exception.set(new AssertionError("Job is not deleted")); } @@ -716,9 +699,7 @@ public void testRealtime_GivenProcessIsKilled() throws Exception { * put the job into the "failed" state. */ public void testStopLookbackFollowedByProcessKill() throws Exception { - client().admin().indices().prepareCreate("data") - .setMapping("time", "type=date") - .get(); + client().admin().indices().prepareCreate("data").setMapping("time", "type=date").get(); long numDocs = randomIntBetween(1024, 2048); long now = System.currentTimeMillis(); long oneWeekAgo = now - 604800000; @@ -746,7 +727,7 @@ public void testStopLookbackFollowedByProcessKill() throws Exception { stopDatafeed(datafeedConfig.getId()); // At this point, stopping the datafeed will have submitted a request for the job to close. - // Depending on thread scheduling, the following kill request might overtake it. The Thread.sleep() + // Depending on thread scheduling, the following kill request might overtake it. The Thread.sleep() // call here makes it more likely; to make it inevitable for testing also add a Thread.sleep(10) // immediately before the checkProcessIsAlive() call in AutodetectCommunicator.close(). Thread.sleep(randomIntBetween(1, 9)); @@ -754,7 +735,7 @@ public void testStopLookbackFollowedByProcessKill() throws Exception { KillProcessAction.Request killRequest = new KillProcessAction.Request(job.getId()); client().execute(KillProcessAction.INSTANCE, killRequest).actionGet(); - // This should close very quickly, as we killed the process. If the job goes into the "failed" + // This should close very quickly, as we killed the process. If the job goes into the "failed" // state that's wrong and this test will fail. waitUntilJobIsClosed(job.getId(), TimeValue.timeValueSeconds(2)); } @@ -764,9 +745,7 @@ private void startRealtime(String jobId) throws Exception { } private void startRealtime(String jobId, Integer maxEmptySearches) throws Exception { - client().admin().indices().prepareCreate("data") - .setMapping("time", "type=date") - .get(); + client().admin().indices().prepareCreate("data").setMapping("time", "type=date").get(); long now = System.currentTimeMillis(); long numDocs1; if (maxEmptySearches == null) { @@ -782,8 +761,11 @@ private void startRealtime(String jobId, Integer maxEmptySearches) throws Except openJob(job.getId()); assertBusy(() -> assertEquals(getJobStats(job.getId()).get(0).getState(), JobState.OPENED)); - DatafeedConfig.Builder datafeedConfigBuilder = - createDatafeedBuilder(job.getId() + "-datafeed", job.getId(), Collections.singletonList("data")); + DatafeedConfig.Builder datafeedConfigBuilder = createDatafeedBuilder( + job.getId() + "-datafeed", + job.getId(), + Collections.singletonList("data") + ); if (maxEmptySearches != null) { datafeedConfigBuilder.setMaxEmptySearches(maxEmptySearches); } diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DatafeedJobsRestIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DatafeedJobsRestIT.java index 022d738f71c7d..2b5f3cd6be6fb 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DatafeedJobsRestIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DatafeedJobsRestIT.java @@ -41,12 +41,18 @@ public class DatafeedJobsRestIT extends ESRestTestCase { - private static final String BASIC_AUTH_VALUE_SUPER_USER = - UsernamePasswordToken.basicAuthHeaderValue("x_pack_rest_user", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING); - private static final String BASIC_AUTH_VALUE_ML_ADMIN = - UsernamePasswordToken.basicAuthHeaderValue("ml_admin", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING); - private static final String BASIC_AUTH_VALUE_ML_ADMIN_WITH_SOME_DATA_ACCESS = - UsernamePasswordToken.basicAuthHeaderValue("ml_admin_plus_data", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING); + private static final String BASIC_AUTH_VALUE_SUPER_USER = UsernamePasswordToken.basicAuthHeaderValue( + "x_pack_rest_user", + SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING + ); + private static final String BASIC_AUTH_VALUE_ML_ADMIN = UsernamePasswordToken.basicAuthHeaderValue( + "ml_admin", + SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING + ); + private static final String BASIC_AUTH_VALUE_ML_ADMIN_WITH_SOME_DATA_ACCESS = UsernamePasswordToken.basicAuthHeaderValue( + "ml_admin_plus_data", + SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING + ); @Override protected Settings restClientSettings() { @@ -60,21 +66,17 @@ protected boolean preserveTemplatesUponCompletion() { private static void setupDataAccessRole(String index) throws IOException { Request request = new Request("PUT", "/_security/role/test_data_access"); - request.setJsonEntity("{" - + " \"indices\" : [" - + " { \"names\": [\"" + index + "\"], \"privileges\": [\"read\"] }" - + " ]" - + "}"); + request.setJsonEntity( + "{" + " \"indices\" : [" + " { \"names\": [\"" + index + "\"], \"privileges\": [\"read\"] }" + " ]" + "}" + ); client().performRequest(request); } private void setupFullAccessRole(String index) throws IOException { Request request = new Request("PUT", "/_security/role/test_data_access"); - request.setJsonEntity("{" - + " \"indices\" : [" - + " { \"names\": [\"" + index + "\"], \"privileges\": [\"all\"] }" - + " ]" - + "}"); + request.setJsonEntity( + "{" + " \"indices\" : [" + " { \"names\": [\"" + index + "\"], \"privileges\": [\"all\"] }" + " ]" + "}" + ); client().performRequest(request); } @@ -82,10 +84,16 @@ private void setupUser(String user, List roles) throws IOException { String password = new String(SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING.getChars()); Request request = new Request("PUT", "/_security/user/" + user); - request.setJsonEntity("{" - + " \"password\" : \"" + password + "\"," - + " \"roles\" : [ " + roles.stream().map(unquoted -> "\"" + unquoted + "\"").collect(Collectors.joining(", ")) + " ]" - + "}"); + request.setJsonEntity( + "{" + + " \"password\" : \"" + + password + + "\"," + + " \"roles\" : [ " + + roles.stream().map(unquoted -> "\"" + unquoted + "\"").collect(Collectors.joining(", ")) + + " ]" + + "}" + ); client().performRequest(request); } @@ -105,7 +113,8 @@ private void addAirlineData() throws IOException { StringBuilder bulk = new StringBuilder(); Request createEmptyAirlineDataRequest = new Request("PUT", "/airline-data-empty"); - createEmptyAirlineDataRequest.setJsonEntity("{" + createEmptyAirlineDataRequest.setJsonEntity( + "{" + " \"mappings\": {" + " \"properties\": {" + " \"time stamp\": { \"type\":\"date\"}," // space in 'time stamp' is intentional @@ -113,12 +122,14 @@ private void addAirlineData() throws IOException { + " \"responsetime\": { \"type\":\"float\"}" + " }" + " }" - + "}"); + + "}" + ); client().performRequest(createEmptyAirlineDataRequest); // Create index with source = enabled, doc_values = enabled, stored = false + multi-field Request createAirlineDataRequest = new Request("PUT", "/airline-data"); - createAirlineDataRequest.setJsonEntity("{" + createAirlineDataRequest.setJsonEntity( + "{" + " \"mappings\": {" + " \"runtime\": {" + " \"airline_lowercase_rt\": { " @@ -138,7 +149,8 @@ private void addAirlineData() throws IOException { + " \"responsetime\": { \"type\":\"float\"}" + " }" + " }" - + "}"); + + "}" + ); client().performRequest(createAirlineDataRequest); bulk.append("{\"index\": {\"_index\": \"airline-data\", \"_id\": 1}}\n"); @@ -148,7 +160,8 @@ private void addAirlineData() throws IOException { // Create index with source = enabled, doc_values = disabled (except time), stored = false Request createAirlineDataDisabledDocValues = new Request("PUT", "/airline-data-disabled-doc-values"); - createAirlineDataDisabledDocValues.setJsonEntity("{" + createAirlineDataDisabledDocValues.setJsonEntity( + "{" + " \"mappings\": {" + " \"properties\": {" + " \"time stamp\": { \"type\":\"date\"}," @@ -156,7 +169,8 @@ private void addAirlineData() throws IOException { + " \"responsetime\": { \"type\":\"float\", \"doc_values\":false}" + " }" + " }" - + "}"); + + "}" + ); client().performRequest(createAirlineDataDisabledDocValues); bulk.append("{\"index\": {\"_index\": \"airline-data-disabled-doc-values\", \"_id\": 1}}\n"); @@ -166,7 +180,8 @@ private void addAirlineData() throws IOException { // Create index with source = disabled, doc_values = enabled (except time), stored = true Request createAirlineDataDisabledSource = new Request("PUT", "/airline-data-disabled-source"); - createAirlineDataDisabledSource.setJsonEntity("{" + createAirlineDataDisabledSource.setJsonEntity( + "{" + " \"mappings\": {" + " \"_source\":{\"enabled\":false}," + " \"properties\": {" @@ -175,7 +190,8 @@ private void addAirlineData() throws IOException { + " \"responsetime\": { \"type\":\"float\", \"store\":true}" + " }" + " }" - + "}"); + + "}" + ); bulk.append("{\"index\": {\"_index\": \"airline-data-disabled-source\", \"_id\": 1}}\n"); bulk.append("{\"time stamp\":\"2016-06-01T00:00:00Z\",\"airline\":\"AAA\",\"responsetime\":135.22}\n"); @@ -184,13 +200,9 @@ private void addAirlineData() throws IOException { // Create index with nested documents Request createAirlineDataNested = new Request("PUT", "/nested-data"); - createAirlineDataNested.setJsonEntity("{" - + " \"mappings\": {" - + " \"properties\": {" - + " \"time\": { \"type\":\"date\"}" - + " }" - + " }" - + "}"); + createAirlineDataNested.setJsonEntity( + "{" + " \"mappings\": {" + " \"properties\": {" + " \"time\": { \"type\":\"date\"}" + " }" + " }" + "}" + ); client().performRequest(createAirlineDataNested); bulk.append("{\"index\": {\"_index\": \"nested-data\", \"_id\": 1}}\n"); @@ -200,7 +212,8 @@ private void addAirlineData() throws IOException { // Create index with multiple docs per time interval for aggregation testing Request createAirlineDataAggs = new Request("PUT", "/airline-data-aggs"); - createAirlineDataAggs.setJsonEntity("{" + createAirlineDataAggs.setJsonEntity( + "{" + " \"mappings\": {" + " \"properties\": {" + " \"time stamp\": { \"type\":\"date\"}," // space in 'time stamp' is intentional @@ -208,7 +221,8 @@ private void addAirlineData() throws IOException { + " \"responsetime\": { \"type\":\"float\"}" + " }" + " }" - + "}"); + + "}" + ); client().performRequest(createAirlineDataAggs); bulk.append("{\"index\": {\"_index\": \"airline-data-aggs\", \"_id\": 1}}\n"); @@ -234,7 +248,8 @@ private void addAirlineData() throws IOException { private void addNetworkData(String index) throws IOException { // Create index with source = enabled, doc_values = enabled, stored = false + multi-field Request createIndexRequest = new Request("PUT", index); - createIndexRequest.setJsonEntity("{" + createIndexRequest.setJsonEntity( + "{" + " \"mappings\": {" + " \"properties\": {" + " \"timestamp\": { \"type\":\"date\"}," @@ -248,7 +263,8 @@ private void addNetworkData(String index) throws IOException { + " \"network_bytes_out\": { \"type\":\"long\"}" + " }" + " }" - + "}"); + + "}" + ); client().performRequest(createIndexRequest); StringBuilder bulk = new StringBuilder(); @@ -270,18 +286,19 @@ private void addNetworkData(String index) throws IOException { } public void testLookbackOnlyWithMixedTypes() throws Exception { - new LookbackOnlyTestHelper("test-lookback-only-with-mixed-types", "airline-data") - .setShouldSucceedProcessing(true).execute(); + new LookbackOnlyTestHelper("test-lookback-only-with-mixed-types", "airline-data").setShouldSucceedProcessing(true).execute(); } public void testLookbackOnlyWithKeywordMultiField() throws Exception { - new LookbackOnlyTestHelper("test-lookback-only-with-keyword-multi-field", "airline-data") - .setAirlineVariant("airline.keyword").setShouldSucceedProcessing(true).execute(); + new LookbackOnlyTestHelper("test-lookback-only-with-keyword-multi-field", "airline-data").setAirlineVariant("airline.keyword") + .setShouldSucceedProcessing(true) + .execute(); } public void testLookbackOnlyWithTextMultiField() throws Exception { - new LookbackOnlyTestHelper("test-lookback-only-with-keyword-multi-field", "airline-data") - .setAirlineVariant("airline.text").setShouldSucceedProcessing(true).execute(); + new LookbackOnlyTestHelper("test-lookback-only-with-keyword-multi-field", "airline-data").setAirlineVariant("airline.text") + .setShouldSucceedProcessing(true) + .execute(); } public void testLookbackOnlyWithDocValuesDisabled() throws Exception { @@ -293,16 +310,13 @@ public void testLookbackOnlyWithSourceDisabled() throws Exception { } public void testLookbackOnlyWithScriptFields() throws Exception { - new LookbackOnlyTestHelper("test-lookback-only-with-script-fields", "airline-data") - .setScriptedFields( - "{\"scripted_airline\":{\"script\":{\"lang\":\"painless\",\"source\":\"doc['airline.keyword'].value\"}}}") - .setAirlineVariant("scripted_airline") - .execute(); + new LookbackOnlyTestHelper("test-lookback-only-with-script-fields", "airline-data").setScriptedFields( + "{\"scripted_airline\":{\"script\":{\"lang\":\"painless\",\"source\":\"doc['airline.keyword'].value\"}}}" + ).setAirlineVariant("scripted_airline").execute(); } public void testLookbackOnlyWithRuntimeFields() throws Exception { - new LookbackOnlyTestHelper("test-lookback-only-with-runtime-fields", "airline-data") - .setAirlineVariant("airline_lowercase_rt") + new LookbackOnlyTestHelper("test-lookback-only-with-runtime-fields", "airline-data").setAirlineVariant("airline_lowercase_rt") .setShouldSucceedProcessing(true) .execute(); } @@ -310,7 +324,8 @@ public void testLookbackOnlyWithRuntimeFields() throws Exception { public void testLookbackonlyWithNestedFields() throws Exception { String jobId = "test-lookback-only-with-nested-fields"; Request createJobRequest = new Request("PUT", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId); - createJobRequest.setJsonEntity("{\n" + createJobRequest.setJsonEntity( + "{\n" + " \"description\": \"Nested job\",\n" + " \"analysis_config\": {\n" + " \"bucket_span\": \"1h\",\n" @@ -323,7 +338,8 @@ public void testLookbackonlyWithNestedFields() throws Exception { + " ]\n" + " }," + " \"data_description\": {\"time_field\": \"time\"}\n" - + "}"); + + "}" + ); client().performRequest(createJobRequest); String datafeedId = jobId + "-datafeed"; @@ -333,7 +349,8 @@ public void testLookbackonlyWithNestedFields() throws Exception { startDatafeedAndWaitUntilStopped(datafeedId); waitUntilJobIsClosed(jobId); Response jobStatsResponse = client().performRequest( - new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats")); + new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats") + ); String jobStatsResponseAsString = EntityUtils.toString(jobStatsResponse.getEntity()); assertThat(jobStatsResponseAsString, containsString("\"input_record_count\":2")); assertThat(jobStatsResponseAsString, containsString("\"processed_record_count\":2")); @@ -343,19 +360,21 @@ public void testLookbackonlyWithNestedFields() throws Exception { public void testLookbackWithGeo() throws Exception { String jobId = "test-lookback-only-with-geo"; Request createJobRequest = new Request("PUT", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId); - createJobRequest.setJsonEntity("{\n" - + " \"description\": \"lat_long with geo_point\",\n" - + " \"analysis_config\": {\n" - + " \"bucket_span\": \"15m\",\n" - + " \"detectors\": [\n" - + " {\n" - + " \"function\": \"lat_long\",\n" - + " \"field_name\": \"location\"\n" - + " }\n" - + " ]\n" - + " }," - + " \"data_description\": {\"time_field\": \"time\"}\n" - + "}"); + createJobRequest.setJsonEntity( + "{\n" + + " \"description\": \"lat_long with geo_point\",\n" + + " \"analysis_config\": {\n" + + " \"bucket_span\": \"15m\",\n" + + " \"detectors\": [\n" + + " {\n" + + " \"function\": \"lat_long\",\n" + + " \"field_name\": \"location\"\n" + + " }\n" + + " ]\n" + + " }," + + " \"data_description\": {\"time_field\": \"time\"}\n" + + "}" + ); client().performRequest(createJobRequest); String datafeedId = jobId + "-datafeed"; new DatafeedBuilder(datafeedId, jobId, "geo-data").build(); @@ -363,14 +382,16 @@ public void testLookbackWithGeo() throws Exception { StringBuilder bulk = new StringBuilder(); Request createGeoData = new Request("PUT", "/geo-data"); - createGeoData.setJsonEntity("{" - + " \"mappings\": {" - + " \"properties\": {" - + " \"time\": { \"type\":\"date\"}," - + " \"location\": { \"type\":\"geo_point\"}" - + " }" - + " }" - + "}"); + createGeoData.setJsonEntity( + "{" + + " \"mappings\": {" + + " \"properties\": {" + + " \"time\": { \"type\":\"date\"}," + + " \"location\": { \"type\":\"geo_point\"}" + + " }" + + " }" + + "}" + ); client().performRequest(createGeoData); bulk.append("{\"index\": {\"_index\": \"geo-data\", \"_id\": 1}}\n"); @@ -398,7 +419,8 @@ public void testLookbackWithGeo() throws Exception { startDatafeedAndWaitUntilStopped(datafeedId); waitUntilJobIsClosed(jobId); Response jobStatsResponse = client().performRequest( - new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats")); + new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats") + ); String jobStatsResponseAsString = EntityUtils.toString(jobStatsResponse.getEntity()); assertThat(jobStatsResponseAsString, containsString("\"input_record_count\":9")); assertThat(jobStatsResponseAsString, containsString("\"processed_record_count\":9")); @@ -408,38 +430,39 @@ public void testLookbackWithGeo() throws Exception { public void testLookbackWithIndicesOptions() throws Exception { String jobId = "test-lookback-only-with-indices-options"; Request createJobRequest = new Request("PUT", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId); - createJobRequest.setJsonEntity("{\n" - + " \"description\": \"custom indices options\",\n" - + " \"analysis_config\": {\n" - + " \"bucket_span\": \"15m\",\n" - + " \"detectors\": [\n" - + " {\n" - + " \"function\": \"count\"\n" - + " }\n" - + " ]\n" - + " }," - + " \"data_description\": {\"time_field\": \"time\"}\n" - + "}"); + createJobRequest.setJsonEntity( + "{\n" + + " \"description\": \"custom indices options\",\n" + + " \"analysis_config\": {\n" + + " \"bucket_span\": \"15m\",\n" + + " \"detectors\": [\n" + + " {\n" + + " \"function\": \"count\"\n" + + " }\n" + + " ]\n" + + " }," + + " \"data_description\": {\"time_field\": \"time\"}\n" + + "}" + ); client().performRequest(createJobRequest); String datafeedId = jobId + "-datafeed"; - new DatafeedBuilder(datafeedId, jobId, "*hidden-*") - .setIndicesOptions("{" + - "\"expand_wildcards\": [\"all\"]," + - "\"allow_no_indices\": true"+ - "}") - .build(); + new DatafeedBuilder(datafeedId, jobId, "*hidden-*").setIndicesOptions( + "{" + "\"expand_wildcards\": [\"all\"]," + "\"allow_no_indices\": true" + "}" + ).build(); StringBuilder bulk = new StringBuilder(); Request createGeoData = new Request("PUT", "/.hidden-index"); - createGeoData.setJsonEntity("{" - + " \"mappings\": {" - + " \"properties\": {" - + " \"time\": { \"type\":\"date\"}," - + " \"value\": { \"type\":\"long\"}" - + " }" - + " }, \"settings\": {\"index.hidden\": true} " - + "}"); + createGeoData.setJsonEntity( + "{" + + " \"mappings\": {" + + " \"properties\": {" + + " \"time\": { \"type\":\"date\"}," + + " \"value\": { \"type\":\"long\"}" + + " }" + + " }, \"settings\": {\"index.hidden\": true} " + + "}" + ); client().performRequest(createGeoData); bulk.append("{\"index\": {\"_index\": \".hidden-index\", \"_id\": 1}}\n"); @@ -467,7 +490,8 @@ public void testLookbackWithIndicesOptions() throws Exception { startDatafeedAndWaitUntilStopped(datafeedId); waitUntilJobIsClosed(jobId); Response jobStatsResponse = client().performRequest( - new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats")); + new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats") + ); String jobStatsResponseAsString = EntityUtils.toString(jobStatsResponse.getEntity()); assertThat(jobStatsResponseAsString, containsString("\"input_record_count\":9")); assertThat(jobStatsResponseAsString, containsString("\"processed_record_count\":9")); @@ -475,14 +499,16 @@ public void testLookbackWithIndicesOptions() throws Exception { } public void testLookbackOnlyGivenEmptyIndex() throws Exception { - new LookbackOnlyTestHelper("test-lookback-only-given-empty-index", "airline-data-empty") - .setShouldSucceedInput(false).setShouldSucceedProcessing(false).execute(); + new LookbackOnlyTestHelper("test-lookback-only-given-empty-index", "airline-data-empty").setShouldSucceedInput(false) + .setShouldSucceedProcessing(false) + .execute(); } public void testInsufficientSearchPrivilegesOnPut() throws Exception { String jobId = "privs-put-job"; Request createJobRequest = new Request("PUT", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId); - createJobRequest.setJsonEntity("{\n" + createJobRequest.setJsonEntity( + "{\n" + " \"description\": \"Aggs job\",\n" + " \"analysis_config\": {\n" + " \"bucket_span\": \"1h\",\n " @@ -496,49 +522,50 @@ public void testInsufficientSearchPrivilegesOnPut() throws Exception { + " ]\n" + " },\n" + " \"data_description\" : {\"time_field\": \"time stamp\"}\n" - + "}"); + + "}" + ); client().performRequest(createJobRequest); String datafeedId = "datafeed-" + jobId; // This should be disallowed, because even though the ml_admin user has permission to // create a datafeed they DON'T have permission to search the index the datafeed is // configured to read - ResponseException e = expectThrows(ResponseException.class, () -> - new DatafeedBuilder(datafeedId, jobId, "airline-data-aggs") - .setAuthHeader(BASIC_AUTH_VALUE_ML_ADMIN) - .build()); + ResponseException e = expectThrows( + ResponseException.class, + () -> new DatafeedBuilder(datafeedId, jobId, "airline-data-aggs").setAuthHeader(BASIC_AUTH_VALUE_ML_ADMIN).build() + ); assertThat(e.getMessage(), containsString("Cannot create datafeed")); - assertThat(e.getMessage(), - containsString("user ml_admin lacks permissions on the indices")); + assertThat(e.getMessage(), containsString("user ml_admin lacks permissions on the indices")); } public void testInsufficientSearchPrivilegesOnPutWithJob() { String jobId = "privs-failed-put-job"; Request createJobRequest = new Request("PUT", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId); - createJobRequest.setJsonEntity("{\n" - + " \"description\": \"Aggs job\",\n" - + " \"datafeed_config\": {\"indexes\": [\"airline-data-aggs\"]},\n" - + " \"analysis_config\": {\n" - + " \"bucket_span\": \"1h\",\n " - + " \"summary_count_field_name\": \"doc_count\",\n" - + " \"detectors\": [\n" - + " {\n" - + " \"function\": \"mean\",\n" - + " \"field_name\": \"responsetime\",\n" - + " \"by_field_name\":\"airline\"\n" - + " }\n" - + " ]\n" - + " },\n" - + " \"data_description\" : {\"time_field\": \"time stamp\"}\n" - + "}"); + createJobRequest.setJsonEntity( + "{\n" + + " \"description\": \"Aggs job\",\n" + + " \"datafeed_config\": {\"indexes\": [\"airline-data-aggs\"]},\n" + + " \"analysis_config\": {\n" + + " \"bucket_span\": \"1h\",\n " + + " \"summary_count_field_name\": \"doc_count\",\n" + + " \"detectors\": [\n" + + " {\n" + + " \"function\": \"mean\",\n" + + " \"field_name\": \"responsetime\",\n" + + " \"by_field_name\":\"airline\"\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"data_description\" : {\"time_field\": \"time stamp\"}\n" + + "}" + ); RequestOptions.Builder options = createJobRequest.getOptions().toBuilder(); options.addHeader("Authorization", BASIC_AUTH_VALUE_ML_ADMIN); createJobRequest.setOptions(options); ResponseException e = expectThrows(ResponseException.class, () -> client().performRequest(createJobRequest)); assertThat(e.getMessage(), containsString("Cannot create datafeed")); - assertThat(e.getMessage(), - containsString("user ml_admin lacks permissions on the indices")); + assertThat(e.getMessage(), containsString("user ml_admin lacks permissions on the indices")); ResponseException missing = expectThrows( ResponseException.class, @@ -554,14 +581,17 @@ public void testCreationOnPutWithRollup() throws Exception { final Response response = createJobAndDataFeed(jobId, datafeedId); assertEquals(200, response.getStatusLine().getStatusCode()); - assertThat(EntityUtils.toString(response.getEntity()), containsString("\"datafeed_id\":\"" + datafeedId - + "\",\"job_id\":\"" + jobId + "\"")); + assertThat( + EntityUtils.toString(response.getEntity()), + containsString("\"datafeed_id\":\"" + datafeedId + "\",\"job_id\":\"" + jobId + "\"") + ); } public void testInsufficientSearchPrivilegesOnPreview() throws Exception { String jobId = "privs-preview-job"; Request createJobRequest = new Request("PUT", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId); - createJobRequest.setJsonEntity("{\n" + createJobRequest.setJsonEntity( + "{\n" + " \"description\": \"Aggs job\",\n" + " \"analysis_config\": {\n" + " \"bucket_span\": \"1h\",\n" @@ -575,7 +605,8 @@ public void testInsufficientSearchPrivilegesOnPreview() throws Exception { + " ]\n" + " },\n" + " \"data_description\" : {\"time_field\": \"time stamp\"}\n" - + "}"); + + "}" + ); client().performRequest(createJobRequest); String datafeedId = "datafeed-" + jobId; @@ -590,8 +621,7 @@ public void testInsufficientSearchPrivilegesOnPreview() throws Exception { getFeed.setOptions(options); ResponseException e = expectThrows(ResponseException.class, () -> client().performRequest(getFeed)); - assertThat(e.getMessage(), - containsString("[indices:data/read/field_caps] is unauthorized for user [ml_admin]")); + assertThat(e.getMessage(), containsString("[indices:data/read/field_caps] is unauthorized for user [ml_admin]")); } public void testSecondaryAuthSearchPrivilegesLookBack() throws Exception { @@ -600,17 +630,17 @@ public void testSecondaryAuthSearchPrivilegesLookBack() throws Exception { createJob(jobId, "airline.keyword"); String datafeedId = "datafeed-" + jobId; // Primary auth header does not have access, but secondary auth does - new DatafeedBuilder(datafeedId, jobId, "airline-data") - .setAuthHeader(BASIC_AUTH_VALUE_ML_ADMIN) - .setSecondaryAuthHeader(BASIC_AUTH_VALUE_ML_ADMIN_WITH_SOME_DATA_ACCESS) - .build(); + new DatafeedBuilder(datafeedId, jobId, "airline-data").setAuthHeader(BASIC_AUTH_VALUE_ML_ADMIN) + .setSecondaryAuthHeader(BASIC_AUTH_VALUE_ML_ADMIN_WITH_SOME_DATA_ACCESS) + .build(); openJob(client(), jobId); startDatafeedAndWaitUntilStopped(datafeedId); waitUntilJobIsClosed(jobId); - Response jobStatsResponse = client().performRequest(new Request("GET", - MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats")); + Response jobStatsResponse = client().performRequest( + new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats") + ); String jobStatsResponseAsString = EntityUtils.toString(jobStatsResponse.getEntity()); assertThat(jobStatsResponseAsString, containsString("\"input_record_count\":2")); assertThat(jobStatsResponseAsString, containsString("\"processed_record_count\":2")); @@ -637,7 +667,8 @@ public void testSecondaryAuthSearchPrivilegesOnPreview() throws Exception { public void testLookbackOnlyGivenAggregationsWithHistogram() throws Exception { String jobId = "aggs-histogram-job"; Request createJobRequest = new Request("PUT", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId); - createJobRequest.setJsonEntity("{\n" + createJobRequest.setJsonEntity( + "{\n" + " \"description\": \"Aggs job\",\n" + " \"analysis_config\": {\n" + " \"bucket_span\": \"1h\",\n" @@ -651,22 +682,24 @@ public void testLookbackOnlyGivenAggregationsWithHistogram() throws Exception { + " ]\n" + " },\n" + " \"data_description\": {\"time_field\": \"time stamp\"}\n" - + "}"); + + "}" + ); client().performRequest(createJobRequest); String datafeedId = "datafeed-" + jobId; String aggregations = "{\"buckets\":{\"histogram\":{\"field\":\"time stamp\",\"interval\":3600000}," - + "\"aggregations\":{" - + "\"time stamp\":{\"max\":{\"field\":\"time stamp\"}}," - + "\"airline\":{\"terms\":{\"field\":\"airline\",\"size\":10}," - + " \"aggregations\":{\"responsetime\":{\"avg\":{\"field\":\"responsetime\"}}}}}}}"; + + "\"aggregations\":{" + + "\"time stamp\":{\"max\":{\"field\":\"time stamp\"}}," + + "\"airline\":{\"terms\":{\"field\":\"airline\",\"size\":10}," + + " \"aggregations\":{\"responsetime\":{\"avg\":{\"field\":\"responsetime\"}}}}}}}"; new DatafeedBuilder(datafeedId, jobId, "airline-data-aggs").setAggregations(aggregations).build(); openJob(client(), jobId); startDatafeedAndWaitUntilStopped(datafeedId); waitUntilJobIsClosed(jobId); - Response jobStatsResponse = client().performRequest(new Request("GET", - MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats")); + Response jobStatsResponse = client().performRequest( + new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats") + ); String jobStatsResponseAsString = EntityUtils.toString(jobStatsResponse.getEntity()); assertThat(jobStatsResponseAsString, containsString("\"input_record_count\":4")); assertThat(jobStatsResponseAsString, containsString("\"processed_record_count\":4")); @@ -676,7 +709,8 @@ public void testLookbackOnlyGivenAggregationsWithHistogram() throws Exception { public void testLookbackOnlyGivenAggregationsWithDateHistogram() throws Exception { String jobId = "aggs-date-histogram-job"; Request createJobRequest = new Request("PUT", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId); - createJobRequest.setJsonEntity("{\n" + createJobRequest.setJsonEntity( + "{\n" + " \"description\": \"Aggs job\",\n" + " \"analysis_config\": {\n" + " \"bucket_span\": \"3600s\",\n" @@ -690,22 +724,24 @@ public void testLookbackOnlyGivenAggregationsWithDateHistogram() throws Exceptio + " ]\n" + " },\n" + " \"data_description\": {\"time_field\": \"time stamp\"}\n" - + "}"); + + "}" + ); client().performRequest(createJobRequest); String datafeedId = "datafeed-" + jobId; String aggregations = "{\"time stamp\":{\"date_histogram\":{\"field\":\"time stamp\",\"calendar_interval\":\"1h\"}," - + "\"aggregations\":{" - + "\"time stamp\":{\"max\":{\"field\":\"time stamp\"}}," - + "\"airline\":{\"terms\":{\"field\":\"airline\",\"size\":10}," - + " \"aggregations\":{\"responsetime\":{\"avg\":{\"field\":\"responsetime\"}}}}}}}"; + + "\"aggregations\":{" + + "\"time stamp\":{\"max\":{\"field\":\"time stamp\"}}," + + "\"airline\":{\"terms\":{\"field\":\"airline\",\"size\":10}," + + " \"aggregations\":{\"responsetime\":{\"avg\":{\"field\":\"responsetime\"}}}}}}}"; new DatafeedBuilder(datafeedId, jobId, "airline-data-aggs").setAggregations(aggregations).build(); openJob(client(), jobId); startDatafeedAndWaitUntilStopped(datafeedId); waitUntilJobIsClosed(jobId); - Response jobStatsResponse = client().performRequest(new Request("GET", - MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats")); + Response jobStatsResponse = client().performRequest( + new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats") + ); String jobStatsResponseAsString = EntityUtils.toString(jobStatsResponse.getEntity()); assertThat(jobStatsResponseAsString, containsString("\"input_record_count\":4")); assertThat(jobStatsResponseAsString, containsString("\"processed_record_count\":4")); @@ -715,7 +751,8 @@ public void testLookbackOnlyGivenAggregationsWithDateHistogram() throws Exceptio public void testLookbackUsingDerivativeAggWithLargerHistogramBucketThanDataRate() throws Exception { String jobId = "derivative-agg-network-job"; Request createJobRequest = new Request("PUT", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId); - createJobRequest.setJsonEntity("{\n" + createJobRequest.setJsonEntity( + "{\n" + " \"analysis_config\": {\n" + " \"bucket_span\": \"300s\",\n" + " \"summary_count_field_name\": \"doc_count\",\n" @@ -728,27 +765,25 @@ public void testLookbackUsingDerivativeAggWithLargerHistogramBucketThanDataRate( + " ]\n" + " },\n" + " \"data_description\": {\"time_field\": \"timestamp\"}\n" - + "}"); + + "}" + ); client().performRequest(createJobRequest); String datafeedId = "datafeed-" + jobId; - String aggregations = - "{\"hostname\": {\"terms\" : {\"field\": \"host.keyword\", \"size\":10}," - + "\"aggs\": {\"buckets\": {\"date_histogram\":{\"field\":\"timestamp\",\"fixed_interval\":\"60s\"}," - + "\"aggs\": {\"timestamp\":{\"max\":{\"field\":\"timestamp\"}}," - + "\"bytes-delta\":{\"derivative\":{\"buckets_path\":\"avg_bytes_out\"}}," - + "\"avg_bytes_out\":{\"avg\":{\"field\":\"network_bytes_out\"}} }}}}}"; - new DatafeedBuilder(datafeedId, jobId, "network-data") - .setAggregations(aggregations) - .setChunkingTimespan("300s") - .build(); + String aggregations = "{\"hostname\": {\"terms\" : {\"field\": \"host.keyword\", \"size\":10}," + + "\"aggs\": {\"buckets\": {\"date_histogram\":{\"field\":\"timestamp\",\"fixed_interval\":\"60s\"}," + + "\"aggs\": {\"timestamp\":{\"max\":{\"field\":\"timestamp\"}}," + + "\"bytes-delta\":{\"derivative\":{\"buckets_path\":\"avg_bytes_out\"}}," + + "\"avg_bytes_out\":{\"avg\":{\"field\":\"network_bytes_out\"}} }}}}}"; + new DatafeedBuilder(datafeedId, jobId, "network-data").setAggregations(aggregations).setChunkingTimespan("300s").build(); openJob(client(), jobId); startDatafeedAndWaitUntilStopped(datafeedId); waitUntilJobIsClosed(jobId); - Response jobStatsResponse = client().performRequest(new Request("GET", - MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats")); + Response jobStatsResponse = client().performRequest( + new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats") + ); String jobStatsResponseAsString = EntityUtils.toString(jobStatsResponse.getEntity()); assertThat(jobStatsResponseAsString, containsString("\"input_record_count\":40")); assertThat(jobStatsResponseAsString, containsString("\"processed_record_count\":40")); @@ -760,8 +795,9 @@ public void testLookbackUsingDerivativeAggWithLargerHistogramBucketThanDataRate( public void testLookbackUsingDerivativeAggWithSmallerHistogramBucketThanDataRate() throws Exception { String jobId = "derivative-agg-network-job"; - Request createJobRequest = new Request("PUT", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId); - createJobRequest.setJsonEntity("{\n" + Request createJobRequest = new Request("PUT", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId); + createJobRequest.setJsonEntity( + "{\n" + " \"analysis_config\": {\n" + " \"bucket_span\": \"300s\",\n" + " \"summary_count_field_name\": \"doc_count\",\n" @@ -774,27 +810,25 @@ public void testLookbackUsingDerivativeAggWithSmallerHistogramBucketThanDataRate + " ]\n" + " },\n" + " \"data_description\": {\"time_field\": \"timestamp\"}\n" - + "}"); + + "}" + ); client().performRequest(createJobRequest); String datafeedId = "datafeed-" + jobId; - String aggregations = - "{\"hostname\": {\"terms\" : {\"field\": \"host.keyword\", \"size\":10}," - + "\"aggs\": {\"buckets\": {\"date_histogram\":{\"field\":\"timestamp\",\"fixed_interval\":\"5s\"}," - + "\"aggs\": {\"timestamp\":{\"max\":{\"field\":\"timestamp\"}}," - + "\"bytes-delta\":{\"derivative\":{\"buckets_path\":\"avg_bytes_out\"}}," - + "\"avg_bytes_out\":{\"avg\":{\"field\":\"network_bytes_out\"}} }}}}}"; - new DatafeedBuilder(datafeedId, jobId, "network-data") - .setAggregations(aggregations) - .setChunkingTimespan("300s") - .build(); + String aggregations = "{\"hostname\": {\"terms\" : {\"field\": \"host.keyword\", \"size\":10}," + + "\"aggs\": {\"buckets\": {\"date_histogram\":{\"field\":\"timestamp\",\"fixed_interval\":\"5s\"}," + + "\"aggs\": {\"timestamp\":{\"max\":{\"field\":\"timestamp\"}}," + + "\"bytes-delta\":{\"derivative\":{\"buckets_path\":\"avg_bytes_out\"}}," + + "\"avg_bytes_out\":{\"avg\":{\"field\":\"network_bytes_out\"}} }}}}}"; + new DatafeedBuilder(datafeedId, jobId, "network-data").setAggregations(aggregations).setChunkingTimespan("300s").build(); openJob(client(), jobId); startDatafeedAndWaitUntilStopped(datafeedId); waitUntilJobIsClosed(jobId); - Response jobStatsResponse = client().performRequest(new Request("GET", - MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats")); + Response jobStatsResponse = client().performRequest( + new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats") + ); String jobStatsResponseAsString = EntityUtils.toString(jobStatsResponse.getEntity()); assertThat(jobStatsResponseAsString, containsString("\"input_record_count\":240")); assertThat(jobStatsResponseAsString, containsString("\"processed_record_count\":240")); @@ -803,7 +837,8 @@ public void testLookbackUsingDerivativeAggWithSmallerHistogramBucketThanDataRate public void testLookbackWithoutPermissions() throws Exception { String jobId = "permission-test-network-job"; Request createJobRequest = new Request("PUT", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId); - createJobRequest.setJsonEntity("{\n" + createJobRequest.setJsonEntity( + "{\n" + " \"analysis_config\": {\n" + " \"bucket_span\": \"300s\",\n" + " \"summary_count_field_name\": \"doc_count\",\n" @@ -816,23 +851,22 @@ public void testLookbackWithoutPermissions() throws Exception { + " ]\n" + " },\n" + " \"data_description\": {\"time_field\": \"timestamp\"}\n" - + "}"); + + "}" + ); client().performRequest(createJobRequest); String datafeedId = "datafeed-" + jobId; - String aggregations = - "{\"hostname\": {\"terms\" : {\"field\": \"host.keyword\", \"size\":10}," - + "\"aggs\": {\"buckets\": {\"date_histogram\":{\"field\":\"timestamp\",\"fixed_interval\":\"5s\"}," - + "\"aggs\": {\"timestamp\":{\"max\":{\"field\":\"timestamp\"}}," - + "\"bytes-delta\":{\"derivative\":{\"buckets_path\":\"avg_bytes_out\"}}," - + "\"avg_bytes_out\":{\"avg\":{\"field\":\"network_bytes_out\"}} }}}}}"; + String aggregations = "{\"hostname\": {\"terms\" : {\"field\": \"host.keyword\", \"size\":10}," + + "\"aggs\": {\"buckets\": {\"date_histogram\":{\"field\":\"timestamp\",\"fixed_interval\":\"5s\"}," + + "\"aggs\": {\"timestamp\":{\"max\":{\"field\":\"timestamp\"}}," + + "\"bytes-delta\":{\"derivative\":{\"buckets_path\":\"avg_bytes_out\"}}," + + "\"avg_bytes_out\":{\"avg\":{\"field\":\"network_bytes_out\"}} }}}}}"; // At the time we create the datafeed the user can access the network-data index that we have access to - new DatafeedBuilder(datafeedId, jobId, "network-data") - .setAggregations(aggregations) - .setChunkingTimespan("300s") - .setAuthHeader(BASIC_AUTH_VALUE_ML_ADMIN_WITH_SOME_DATA_ACCESS) - .build(); + new DatafeedBuilder(datafeedId, jobId, "network-data").setAggregations(aggregations) + .setChunkingTimespan("300s") + .setAuthHeader(BASIC_AUTH_VALUE_ML_ADMIN_WITH_SOME_DATA_ACCESS) + .build(); // Change the role so that the user can no longer access network-data setupDataAccessRole("some-other-data"); @@ -841,8 +875,9 @@ public void testLookbackWithoutPermissions() throws Exception { startDatafeedAndWaitUntilStopped(datafeedId, BASIC_AUTH_VALUE_ML_ADMIN_WITH_SOME_DATA_ACCESS); waitUntilJobIsClosed(jobId); - Response jobStatsResponse = client().performRequest(new Request("GET", - MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats")); + Response jobStatsResponse = client().performRequest( + new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats") + ); String jobStatsResponseAsString = EntityUtils.toString(jobStatsResponse.getEntity()); // We expect that no data made it through to the job assertThat(jobStatsResponseAsString, containsString("\"input_record_count\":0")); @@ -851,19 +886,26 @@ public void testLookbackWithoutPermissions() throws Exception { // There should be a notification saying that there was a problem extracting data refreshAllIndices(); Response notificationsResponse = client().performRequest( - new Request("GET", NotificationsIndex.NOTIFICATIONS_INDEX + "/_search?size=1000&q=job_id:" + jobId)); + new Request("GET", NotificationsIndex.NOTIFICATIONS_INDEX + "/_search?size=1000&q=job_id:" + jobId) + ); String notificationsResponseAsString = EntityUtils.toString(notificationsResponse.getEntity()); - assertThat(notificationsResponseAsString, containsString("\"message\":\"Datafeed is encountering errors extracting data: " + - "action [indices:data/read/search] is unauthorized" + - " for user [ml_admin_plus_data]" + - " with roles [machine_learning_admin,test_data_access]" + - " on indices [network-data]")); + assertThat( + notificationsResponseAsString, + containsString( + "\"message\":\"Datafeed is encountering errors extracting data: " + + "action [indices:data/read/search] is unauthorized" + + " for user [ml_admin_plus_data]" + + " with roles [machine_learning_admin,test_data_access]" + + " on indices [network-data]" + ) + ); } public void testLookbackWithPipelineBucketAgg() throws Exception { String jobId = "pipeline-bucket-agg-job"; Request createJobRequest = new Request("PUT", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId); - createJobRequest.setJsonEntity("{\n" + createJobRequest.setJsonEntity( + "{\n" + " \"analysis_config\": {\n" + " \"bucket_span\": \"1h\",\n" + " \"summary_count_field_name\": \"doc_count\",\n" @@ -875,24 +917,26 @@ public void testLookbackWithPipelineBucketAgg() throws Exception { + " ]\n" + " },\n" + " \"data_description\": {\"time_field\": \"time stamp\"}\n" - + "}"); + + "}" + ); client().performRequest(createJobRequest); String datafeedId = "datafeed-" + jobId; String aggregations = "{\"buckets\":{\"date_histogram\":{\"field\":\"time stamp\",\"fixed_interval\":\"15m\"}," - + "\"aggregations\":{" - + "\"time stamp\":{\"max\":{\"field\":\"time stamp\"}}," - + "\"airlines\":{\"terms\":{\"field\":\"airline.keyword\",\"size\":10}}," - + "\"percentile95_airlines_count\":{\"percentiles_bucket\":" + - "{\"buckets_path\":\"airlines._count\", \"percents\": [95]}}}}}"; + + "\"aggregations\":{" + + "\"time stamp\":{\"max\":{\"field\":\"time stamp\"}}," + + "\"airlines\":{\"terms\":{\"field\":\"airline.keyword\",\"size\":10}}," + + "\"percentile95_airlines_count\":{\"percentiles_bucket\":" + + "{\"buckets_path\":\"airlines._count\", \"percents\": [95]}}}}}"; new DatafeedBuilder(datafeedId, jobId, "airline-data").setAggregations(aggregations).build(); openJob(client(), jobId); startDatafeedAndWaitUntilStopped(datafeedId); waitUntilJobIsClosed(jobId); - Response jobStatsResponse = client().performRequest(new Request("GET", - MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats")); + Response jobStatsResponse = client().performRequest( + new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats") + ); String jobStatsResponseAsString = EntityUtils.toString(jobStatsResponse.getEntity()); assertThat(jobStatsResponseAsString, containsString("\"input_record_count\":2")); assertThat(jobStatsResponseAsString, containsString("\"input_field_count\":4")); @@ -905,51 +949,55 @@ public void testLookbackWithPipelineBucketAgg() throws Exception { public void testLookbackOnlyGivenAggregationsWithHistogramAndRollupIndex() throws Exception { String jobId = "aggs-histogram-rollup-job"; Request createJobRequest = new Request("PUT", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId); - createJobRequest.setJsonEntity("{\n" - + " \"description\": \"Aggs job\",\n" - + " \"analysis_config\": {\n" - + " \"bucket_span\": \"1h\",\n" - + " \"summary_count_field_name\": \"doc_count\",\n" - + " \"detectors\": [\n" - + " {\n" - + " \"function\": \"mean\",\n" - + " \"field_name\": \"responsetime\",\n" - + " \"by_field_name\": \"airline\"\n" - + " }\n" - + " ]\n" - + " },\n" - + " \"data_description\": {\"time_field\": \"time stamp\"}\n" - + "}"); + createJobRequest.setJsonEntity( + "{\n" + + " \"description\": \"Aggs job\",\n" + + " \"analysis_config\": {\n" + + " \"bucket_span\": \"1h\",\n" + + " \"summary_count_field_name\": \"doc_count\",\n" + + " \"detectors\": [\n" + + " {\n" + + " \"function\": \"mean\",\n" + + " \"field_name\": \"responsetime\",\n" + + " \"by_field_name\": \"airline\"\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"data_description\": {\"time_field\": \"time stamp\"}\n" + + "}" + ); client().performRequest(createJobRequest); String rollupJobId = "rollup-" + jobId; Request createRollupRequest = new Request("PUT", "/_rollup/job/" + rollupJobId); - createRollupRequest.setJsonEntity("{\n" - + "\"index_pattern\": \"airline-data-aggs\",\n" - + " \"rollup_index\": \"airline-data-aggs-rollup\",\n" - + " \"cron\": \"*/30 * * * * ?\",\n" - + " \"page_size\" :1000,\n" - + " \"groups\" : {\n" - + " \"date_histogram\": {\n" - + " \"field\": \"time stamp\",\n" - + " \"fixed_interval\": \"2m\",\n" - + " \"delay\": \"7d\"\n" - + " },\n" - + " \"terms\": {\n" - + " \"fields\": [\"airline\"]\n" - + " }" - + " },\n" - + " \"metrics\": [\n" - + " {\n" - + " \"field\": \"responsetime\",\n" - + " \"metrics\": [\"avg\",\"min\",\"max\",\"sum\"]\n" - + " },\n" - + " {\n" - + " \"field\": \"time stamp\",\n" - + " \"metrics\": [\"min\",\"max\"]\n" - + " }\n" - + " ]\n" - + "}"); + createRollupRequest.setJsonEntity( + "{\n" + + "\"index_pattern\": \"airline-data-aggs\",\n" + + " \"rollup_index\": \"airline-data-aggs-rollup\",\n" + + " \"cron\": \"*/30 * * * * ?\",\n" + + " \"page_size\" :1000,\n" + + " \"groups\" : {\n" + + " \"date_histogram\": {\n" + + " \"field\": \"time stamp\",\n" + + " \"fixed_interval\": \"2m\",\n" + + " \"delay\": \"7d\"\n" + + " },\n" + + " \"terms\": {\n" + + " \"fields\": [\"airline\"]\n" + + " }" + + " },\n" + + " \"metrics\": [\n" + + " {\n" + + " \"field\": \"responsetime\",\n" + + " \"metrics\": [\"avg\",\"min\",\"max\",\"sum\"]\n" + + " },\n" + + " {\n" + + " \"field\": \"time stamp\",\n" + + " \"metrics\": [\"min\",\"max\"]\n" + + " }\n" + + " ]\n" + + "}" + ); client().performRequest(createRollupRequest); client().performRequest(new Request("POST", "/_rollup/job/" + rollupJobId + "/_start")); @@ -979,8 +1027,9 @@ public void testLookbackOnlyGivenAggregationsWithHistogramAndRollupIndex() throw startDatafeedAndWaitUntilStopped(datafeedId); waitUntilJobIsClosed(jobId); - Response jobStatsResponse = client().performRequest(new Request("GET", - MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats")); + Response jobStatsResponse = client().performRequest( + new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats") + ); String jobStatsResponseAsString = EntityUtils.toString(jobStatsResponse.getEntity()); assertThat(jobStatsResponseAsString, containsString("\"input_record_count\":2")); assertThat(jobStatsResponseAsString, containsString("\"processed_record_count\":2")); @@ -1002,32 +1051,40 @@ public void testLookbackWithoutPermissionsAndRollup() throws Exception { // There should be a notification saying that there was a problem extracting data refreshAllIndices(); Response notificationsResponse = client().performRequest( - new Request("GET", NotificationsIndex.NOTIFICATIONS_INDEX + "/_search?size=1000&q=job_id:" + jobId)); + new Request("GET", NotificationsIndex.NOTIFICATIONS_INDEX + "/_search?size=1000&q=job_id:" + jobId) + ); String notificationsResponseAsString = EntityUtils.toString(notificationsResponse.getEntity()); - assertThat(notificationsResponseAsString, containsString("\"message\":\"Datafeed is encountering errors extracting data: " + - "action [indices:data/read/xpack/rollup/search] is unauthorized" + - " for user [ml_admin_plus_data]" + - " with roles [machine_learning_admin,test_data_access]" + - " on indices [airline-data-aggs-rollup]")); + assertThat( + notificationsResponseAsString, + containsString( + "\"message\":\"Datafeed is encountering errors extracting data: " + + "action [indices:data/read/xpack/rollup/search] is unauthorized" + + " for user [ml_admin_plus_data]" + + " with roles [machine_learning_admin,test_data_access]" + + " on indices [airline-data-aggs-rollup]" + ) + ); } public void testLookbackWithSingleBucketAgg() throws Exception { String jobId = "aggs-date-histogram-with-single-bucket-agg-job"; Request createJobRequest = new Request("PUT", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId); - createJobRequest.setJsonEntity("{\n" - + " \"description\": \"Aggs job\",\n" - + " \"analysis_config\": {\n" - + " \"bucket_span\": \"3600s\",\n" - + " \"summary_count_field_name\": \"doc_count\",\n" - + " \"detectors\": [\n" - + " {\n" - + " \"function\": \"mean\",\n" - + " \"field_name\": \"responsetime\"" - + " }\n" - + " ]\n" - + " },\n" - + " \"data_description\": {\"time_field\": \"time stamp\"}\n" - + "}"); + createJobRequest.setJsonEntity( + "{\n" + + " \"description\": \"Aggs job\",\n" + + " \"analysis_config\": {\n" + + " \"bucket_span\": \"3600s\",\n" + + " \"summary_count_field_name\": \"doc_count\",\n" + + " \"detectors\": [\n" + + " {\n" + + " \"function\": \"mean\",\n" + + " \"field_name\": \"responsetime\"" + + " }\n" + + " ]\n" + + " },\n" + + " \"data_description\": {\"time_field\": \"time stamp\"}\n" + + "}" + ); client().performRequest(createJobRequest); String datafeedId = "datafeed-" + jobId; @@ -1041,8 +1098,9 @@ public void testLookbackWithSingleBucketAgg() throws Exception { startDatafeedAndWaitUntilStopped(datafeedId); waitUntilJobIsClosed(jobId); - Response jobStatsResponse = client().performRequest(new Request("GET", - MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats")); + Response jobStatsResponse = client().performRequest( + new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats") + ); String jobStatsResponseAsString = EntityUtils.toString(jobStatsResponse.getEntity()); assertThat(jobStatsResponseAsString, containsString("\"input_record_count\":2")); assertThat(jobStatsResponseAsString, containsString("\"processed_record_count\":2")); @@ -1064,22 +1122,21 @@ public void testRealtime() throws Exception { // We should now be running in real time but may or may not have finished look back assertBusy(() -> { try { - Response datafeedStatsResponse = client().performRequest(new Request("GET", - MachineLearning.BASE_PATH + "datafeeds/" + datafeedId + "/_stats")); + Response datafeedStatsResponse = client().performRequest( + new Request("GET", MachineLearning.BASE_PATH + "datafeeds/" + datafeedId + "/_stats") + ); String body = EntityUtils.toString(datafeedStatsResponse.getEntity()); assertThat(body, containsString("\"real_time_configured\":true")); - assertThat(body, anyOf( - containsString("\"real_time_running\":true"), - containsString("\"real_time_running\":false") - )); + assertThat(body, anyOf(containsString("\"real_time_running\":true"), containsString("\"real_time_running\":false"))); } catch (Exception e1) { throw new RuntimeException(e1); } }); assertBusy(() -> { try { - Response getJobResponse = client().performRequest(new Request("GET", - MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats")); + Response getJobResponse = client().performRequest( + new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats") + ); String responseAsString = EntityUtils.toString(getJobResponse.getEntity()); assertThat(responseAsString, containsString("\"processed_record_count\":2")); assertThat(responseAsString, containsString("\"state\":\"opened\"")); @@ -1092,8 +1149,9 @@ public void testRealtime() throws Exception { // test a model snapshot is present assertBusy(() -> { try { - Response getJobResponse = client().performRequest(new Request("GET", - MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/model_snapshots")); + Response getJobResponse = client().performRequest( + new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/model_snapshots") + ); String responseAsString = EntityUtils.toString(getJobResponse.getEntity()); assertThat(responseAsString, containsString("\"count\":1")); } catch (Exception e1) { @@ -1101,18 +1159,23 @@ public void testRealtime() throws Exception { } }); - ResponseException e = expectThrows(ResponseException.class, - () -> client().performRequest(new Request("DELETE", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId))); + ResponseException e = expectThrows( + ResponseException.class, + () -> client().performRequest(new Request("DELETE", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId)) + ); response = e.getResponse(); assertThat(response.getStatusLine().getStatusCode(), equalTo(409)); - assertThat(EntityUtils.toString(response.getEntity()), - containsString("Cannot delete job [" + jobId + "] because the job is opened")); + assertThat( + EntityUtils.toString(response.getEntity()), + containsString("Cannot delete job [" + jobId + "] because the job is opened") + ); // Look back should now be completed and we are still considered a real time datafeed (no endtime set) assertBusy(() -> { try { - Response datafeedStatsResponse = client().performRequest(new Request("GET", - MachineLearning.BASE_PATH + "datafeeds/" + datafeedId + "/_stats")); + Response datafeedStatsResponse = client().performRequest( + new Request("GET", MachineLearning.BASE_PATH + "datafeeds/" + datafeedId + "/_stats") + ); String body = EntityUtils.toString(datafeedStatsResponse.getEntity()); assertThat(body, containsString("\"real_time_configured\":true")); assertThat(body, containsString("\"real_time_running\":true")); @@ -1149,20 +1212,23 @@ public void testForceDeleteWhileDatafeedIsRunning() throws Exception { assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); assertThat(EntityUtils.toString(response.getEntity()), containsString("\"started\":true")); - ResponseException e = expectThrows(ResponseException.class, - () -> client().performRequest(new Request("DELETE", MachineLearning.BASE_PATH + "datafeeds/" + datafeedId))); + ResponseException e = expectThrows( + ResponseException.class, + () -> client().performRequest(new Request("DELETE", MachineLearning.BASE_PATH + "datafeeds/" + datafeedId)) + ); response = e.getResponse(); assertThat(response.getStatusLine().getStatusCode(), equalTo(409)); - assertThat(EntityUtils.toString(response.getEntity()), - containsString("Cannot delete datafeed [" + datafeedId + "] while its status is started")); + assertThat( + EntityUtils.toString(response.getEntity()), + containsString("Cannot delete datafeed [" + datafeedId + "] while its status is started") + ); Request forceDeleteRequest = new Request("DELETE", MachineLearning.BASE_PATH + "datafeeds/" + datafeedId); forceDeleteRequest.addParameter("force", "true"); response = client().performRequest(forceDeleteRequest); assertThat(EntityUtils.toString(response.getEntity()), equalTo("{\"acknowledged\":true}")); - expectThrows(ResponseException.class, - () -> client().performRequest(new Request("GET", "/_ml/datafeeds/" + datafeedId))); + expectThrows(ResponseException.class, () -> client().performRequest(new Request("GET", "/_ml/datafeeds/" + datafeedId))); } private class LookbackOnlyTestHelper { @@ -1191,7 +1257,6 @@ public LookbackOnlyTestHelper setAirlineVariant(String airlineVariant) { return this; } - public LookbackOnlyTestHelper setShouldSucceedInput(boolean value) { shouldSucceedInput = value; return this; @@ -1212,8 +1277,9 @@ public void execute() throws Exception { startDatafeedAndWaitUntilStopped(datafeedId); waitUntilJobIsClosed(jobId); - Response jobStatsResponse = client().performRequest(new Request("GET", - MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats")); + Response jobStatsResponse = client().performRequest( + new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats") + ); String jobStatsResponseAsString = EntityUtils.toString(jobStatsResponse.getEntity()); if (shouldSucceedInput) { assertThat(jobStatsResponseAsString, containsString("\"input_record_count\":2")); @@ -1228,6 +1294,7 @@ public void execute() throws Exception { assertThat(jobStatsResponseAsString, containsString("\"missing_field_count\":0")); } } + private void startDatafeedAndWaitUntilStopped(String datafeedId) throws Exception { startDatafeedAndWaitUntilStopped(datafeedId, BASIC_AUTH_VALUE_SUPER_USER); } @@ -1243,10 +1310,10 @@ private void startDatafeedAndWaitUntilStopped(String datafeedId, String authHead assertThat(EntityUtils.toString(startDatafeedResponse.getEntity()), containsString("\"started\":true")); assertBusy(() -> { try { - Response datafeedStatsResponse = client().performRequest(new Request("GET", - MachineLearning.BASE_PATH + "datafeeds/" + datafeedId + "/_stats")); - assertThat(EntityUtils.toString(datafeedStatsResponse.getEntity()), - containsString("\"state\":\"stopped\"")); + Response datafeedStatsResponse = client().performRequest( + new Request("GET", MachineLearning.BASE_PATH + "datafeeds/" + datafeedId + "/_stats") + ); + assertThat(EntityUtils.toString(datafeedStatsResponse.getEntity()), containsString("\"state\":\"stopped\"")); } catch (Exception e) { throw new RuntimeException(e); } @@ -1256,8 +1323,9 @@ private void startDatafeedAndWaitUntilStopped(String datafeedId, String authHead private void waitUntilJobIsClosed(String jobId) throws Exception { assertBusy(() -> { try { - Response jobStatsResponse = client().performRequest(new Request("GET", - MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats")); + Response jobStatsResponse = client().performRequest( + new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats") + ); assertThat(EntityUtils.toString(jobStatsResponse.getEntity()), containsString("\"state\":\"closed\"")); } catch (Exception e) { throw new RuntimeException(e); @@ -1267,7 +1335,8 @@ private void waitUntilJobIsClosed(String jobId) throws Exception { private Response createJob(String id, String airlineVariant) throws Exception { Request request = new Request("PUT", MachineLearning.BASE_PATH + "anomaly_detectors/" + id); - request.setJsonEntity("{\n" + request.setJsonEntity( + "{\n" + " \"description\": \"Analysis of response time by airline\",\n" + " \"analysis_config\": {\n" + " \"bucket_span\": \"1h\",\n" @@ -1275,7 +1344,9 @@ private Response createJob(String id, String airlineVariant) throws Exception { + " {\n" + " \"function\": \"mean\",\n" + " \"field_name\": \"responsetime\",\n" - + " \"by_field_name\": \"" + airlineVariant + "\"\n" + + " \"by_field_name\": \"" + + airlineVariant + + "\"\n" + " }\n" + " ]\n" + " },\n" @@ -1284,7 +1355,8 @@ private Response createJob(String id, String airlineVariant) throws Exception { + " \"time_field\": \"time stamp\",\n" + " \"time_format\": \"yyyy-MM-dd'T'HH:mm:ssX\"\n" + " }\n" - + "}"); + + "}" + ); return client().performRequest(request); } @@ -1355,16 +1427,23 @@ DatafeedBuilder setIndicesOptions(String indicesOptions) { Response build() throws IOException { Request request = new Request("PUT", MachineLearning.BASE_PATH + "datafeeds/" + datafeedId); - request.setJsonEntity("{" - + "\"job_id\": \"" + jobId + "\",\"indexes\":[\"" + index + "\"]" + request.setJsonEntity( + "{" + + "\"job_id\": \"" + + jobId + + "\",\"indexes\":[\"" + + index + + "\"]" + (source ? ",\"_source\":true" : "") + (scriptedFields == null ? "" : ",\"script_fields\":" + scriptedFields) + (aggregations == null ? "" : ",\"aggs\":" + aggregations) + (frequency == null ? "" : ",\"frequency\":\"" + frequency + "\"") + (indicesOptions == null ? "" : ",\"indices_options\":" + indicesOptions) - + (chunkingTimespan == null ? "" : - ",\"chunking_config\":{\"mode\":\"MANUAL\",\"time_span\":\"" + chunkingTimespan + "\"}") - + "}"); + + (chunkingTimespan == null + ? "" + : ",\"chunking_config\":{\"mode\":\"MANUAL\",\"time_span\":\"" + chunkingTimespan + "\"}") + + "}" + ); RequestOptions.Builder options = request.getOptions().toBuilder(); options.addHeader("Authorization", authHeader); if (this.secondaryAuthHeader != null) { @@ -1386,51 +1465,55 @@ private void bulkIndex(String bulk) throws IOException { private Response createJobAndDataFeed(String jobId, String datafeedId) throws IOException { Request createJobRequest = new Request("PUT", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId); - createJobRequest.setJsonEntity("{\n" - + " \"description\": \"Aggs job\",\n" - + " \"analysis_config\": {\n" - + " \"bucket_span\": \"1h\",\n" - + " \"summary_count_field_name\": \"doc_count\",\n" - + " \"detectors\": [\n" - + " {\n" - + " \"function\": \"mean\",\n" - + " \"field_name\": \"responsetime\",\n" - + " \"by_field_name\": \"airline\"\n" - + " }\n" - + " ]\n" - + " },\n" - + " \"data_description\": {\"time_field\": \"time stamp\"}\n" - + "}"); + createJobRequest.setJsonEntity( + "{\n" + + " \"description\": \"Aggs job\",\n" + + " \"analysis_config\": {\n" + + " \"bucket_span\": \"1h\",\n" + + " \"summary_count_field_name\": \"doc_count\",\n" + + " \"detectors\": [\n" + + " {\n" + + " \"function\": \"mean\",\n" + + " \"field_name\": \"responsetime\",\n" + + " \"by_field_name\": \"airline\"\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"data_description\": {\"time_field\": \"time stamp\"}\n" + + "}" + ); client().performRequest(createJobRequest); String rollupJobId = "rollup-" + jobId; Request createRollupRequest = new Request("PUT", "/_rollup/job/" + rollupJobId); - createRollupRequest.setJsonEntity("{\n" - + "\"index_pattern\": \"airline-data-aggs\",\n" - + " \"rollup_index\": \"airline-data-aggs-rollup\",\n" - + " \"cron\": \"*/30 * * * * ?\",\n" - + " \"page_size\" :1000,\n" - + " \"groups\" : {\n" - + " \"date_histogram\": {\n" - + " \"field\": \"time stamp\",\n" - + " \"fixed_interval\": \"2m\",\n" - + " \"delay\": \"7d\"\n" - + " },\n" - + " \"terms\": {\n" - + " \"fields\": [\"airline\"]\n" - + " }" - + " },\n" - + " \"metrics\": [\n" - + " {\n" - + " \"field\": \"responsetime\",\n" - + " \"metrics\": [\"avg\",\"min\",\"max\",\"sum\"]\n" - + " },\n" - + " {\n" - + " \"field\": \"time stamp\",\n" - + " \"metrics\": [\"min\",\"max\"]\n" - + " }\n" - + " ]\n" - + "}"); + createRollupRequest.setJsonEntity( + "{\n" + + "\"index_pattern\": \"airline-data-aggs\",\n" + + " \"rollup_index\": \"airline-data-aggs-rollup\",\n" + + " \"cron\": \"*/30 * * * * ?\",\n" + + " \"page_size\" :1000,\n" + + " \"groups\" : {\n" + + " \"date_histogram\": {\n" + + " \"field\": \"time stamp\",\n" + + " \"fixed_interval\": \"2m\",\n" + + " \"delay\": \"7d\"\n" + + " },\n" + + " \"terms\": {\n" + + " \"fields\": [\"airline\"]\n" + + " }" + + " },\n" + + " \"metrics\": [\n" + + " {\n" + + " \"field\": \"responsetime\",\n" + + " \"metrics\": [\"avg\",\"min\",\"max\",\"sum\"]\n" + + " },\n" + + " {\n" + + " \"field\": \"time stamp\",\n" + + " \"metrics\": [\"min\",\"max\"]\n" + + " }\n" + + " ]\n" + + "}" + ); client().performRequest(createRollupRequest); String aggregations = "{\"buckets\":{\"date_histogram\":{\"field\":\"time stamp\",\"fixed_interval\":\"3600000ms\"}," @@ -1438,8 +1521,7 @@ private Response createJobAndDataFeed(String jobId, String datafeedId) throws IO + "\"time stamp\":{\"max\":{\"field\":\"time stamp\"}}," + "\"responsetime\":{\"avg\":{\"field\":\"responsetime\"}}}}}"; - return new DatafeedBuilder(datafeedId, jobId, "airline-data-aggs-rollup") - .setAggregations(aggregations) + return new DatafeedBuilder(datafeedId, jobId, "airline-data-aggs-rollup").setAggregations(aggregations) .setAuthHeader(BASIC_AUTH_VALUE_ML_ADMIN_WITH_SOME_DATA_ACCESS) .build(); } diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DatafeedWithAggsIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DatafeedWithAggsIT.java index 1eb0255426a51..8d8fd7adf7dbc 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DatafeedWithAggsIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DatafeedWithAggsIT.java @@ -40,32 +40,32 @@ public class DatafeedWithAggsIT extends MlNativeAutodetectIntegTestCase { @After - public void cleanup(){ + public void cleanup() { cleanUp(); } public void testRealtime() throws Exception { AggregatorFactories.Builder aggs = new AggregatorFactories.Builder(); - aggs.addAggregator(AggregationBuilders.dateHistogram("time").field("time") - .fixedInterval(new DateHistogramInterval("1000ms")) - .subAggregation(AggregationBuilders.max("time").field("time"))); - testDfWithAggs( - aggs, - new Detector.Builder("count", null), - "datafeed-with-aggs-rt-job", - "datafeed-with-aggs-rt-data" + aggs.addAggregator( + AggregationBuilders.dateHistogram("time") + .field("time") + .fixedInterval(new DateHistogramInterval("1000ms")) + .subAggregation(AggregationBuilders.max("time").field("time")) ); + testDfWithAggs(aggs, new Detector.Builder("count", null), "datafeed-with-aggs-rt-job", "datafeed-with-aggs-rt-data"); } public void testRealtimeComposite() throws Exception { AggregatorFactories.Builder aggs = new AggregatorFactories.Builder(); - aggs.addAggregator(AggregationBuilders.composite("buckets", - Arrays.asList( - new DateHistogramValuesSourceBuilder("time").field("time").fixedInterval(new DateHistogramInterval("1000ms")), - new TermsValuesSourceBuilder("field").field("field") - )) - .size(1000) - .subAggregation(AggregationBuilders.max("time").field("time"))); + aggs.addAggregator( + AggregationBuilders.composite( + "buckets", + Arrays.asList( + new DateHistogramValuesSourceBuilder("time").field("time").fixedInterval(new DateHistogramInterval("1000ms")), + new TermsValuesSourceBuilder("field").field("field") + ) + ).size(1000).subAggregation(AggregationBuilders.max("time").field("time")) + ); testDfWithAggs( aggs, new Detector.Builder("count", null).setByFieldName("field"), @@ -107,9 +107,7 @@ private void testDfWithAggs(AggregatorFactories.Builder aggs, Detector.Builder d openJob(jobId); // Now let's index the data - client().admin().indices().prepareCreate(dfId) - .setMapping("time", "type=date", "field", "type=keyword") - .get(); + client().admin().indices().prepareCreate(dfId).setMapping("time", "type=date", "field", "type=keyword").get(); // Index a doc per second from a minute ago to a minute later long now = System.currentTimeMillis(); @@ -123,9 +121,7 @@ private void testDfWithAggs(AggregatorFactories.Builder aggs, Detector.Builder d bulkRequestBuilder.add(indexRequest); curTime += TimeValue.timeValueSeconds(1).millis(); } - BulkResponse bulkResponse = bulkRequestBuilder - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .get(); + BulkResponse bulkResponse = bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).get(); if (bulkResponse.hasFailures()) { fail("Failed to index docs: " + bulkResponse.buildFailureMessage()); } diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DelayedDataDetectorIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DelayedDataDetectorIT.java index c8f749039be0d..2d527ac974723 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DelayedDataDetectorIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DelayedDataDetectorIT.java @@ -18,8 +18,8 @@ import org.elasticsearch.search.aggregations.AggregatorFactories; import org.elasticsearch.search.aggregations.metrics.AvgAggregationBuilder; import org.elasticsearch.search.aggregations.metrics.MaxAggregationBuilder; -import org.elasticsearch.xpack.core.ml.action.GetBucketsAction; import org.elasticsearch.xpack.core.action.util.PageParams; +import org.elasticsearch.xpack.core.ml.action.GetBucketsAction; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfig; import org.elasticsearch.xpack.core.ml.datafeed.DelayedDataCheckConfig; import org.elasticsearch.xpack.core.ml.job.config.AnalysisConfig; @@ -50,9 +50,7 @@ public class DelayedDataDetectorIT extends MlNativeAutodetectIntegTestCase { @Before public void putDataintoIndex() { - client().admin().indices().prepareCreate(index) - .setMapping("time", "type=date", "value", "type=long") - .get(); + client().admin().indices().prepareCreate(index).setMapping("time", "type=date", "value", "type=long").get(); numDocs = randomIntBetween(32, 128); long oneDayAgo = now - 86400000; writeData(logger, index, numDocs, oneDayAgo, now); @@ -67,14 +65,16 @@ public void testMissingDataDetection() throws Exception { final String jobId = "delayed-data-detection-job"; Job.Builder job = createJob(jobId, TimeValue.timeValueMinutes(5), "count", null); - DatafeedConfig.Builder datafeedConfigBuilder = - createDatafeedBuilder(job.getId() + "-datafeed", job.getId(), Collections.singletonList(index)); + DatafeedConfig.Builder datafeedConfigBuilder = createDatafeedBuilder( + job.getId() + "-datafeed", + job.getId(), + Collections.singletonList(index) + ); datafeedConfigBuilder.setDelayedDataCheckConfig(DelayedDataCheckConfig.enabledDelayedDataCheckConfig(TimeValue.timeValueHours(12))); DatafeedConfig datafeedConfig = datafeedConfigBuilder.build(); putJob(job); openJob(job.getId()); - putDatafeed(datafeedConfig); startDatafeed(datafeedConfig.getId(), 0L, now); waitUntilJobIsClosed(jobId); @@ -84,15 +84,15 @@ public void testMissingDataDetection() throws Exception { DelayedDataDetector delayedDataDetector = newDetector(job.build(new Date()), datafeedConfig); - List response = delayedDataDetector.detectMissingData(lastBucket.getEpoch()*1000); + List response = delayedDataDetector.detectMissingData(lastBucket.getEpoch() * 1000); assertThat(response.stream().mapToLong(BucketWithMissingData::getMissingDocumentCount).sum(), equalTo(0L)); long missingDocs = randomIntBetween(32, 128); // Simply adding data within the current delayed data detection, the choice of 43100000 is arbitrary and within the window // for the DatafeedDelayedDataDetector - writeData(logger, index, missingDocs, now - 43100000, lastBucket.getEpoch()*1000); + writeData(logger, index, missingDocs, now - 43100000, lastBucket.getEpoch() * 1000); - response = delayedDataDetector.detectMissingData(lastBucket.getEpoch()*1000); + response = delayedDataDetector.detectMissingData(lastBucket.getEpoch() * 1000); assertThat(response.stream().mapToLong(BucketWithMissingData::getMissingDocumentCount).sum(), equalTo(missingDocs)); // Assert that the are returned in order List timeStamps = response.stream().map(BucketWithMissingData::getTimeStamp).collect(Collectors.toList()); @@ -103,15 +103,17 @@ public void testMissingDataDetectionInSpecificBucket() throws Exception { final String jobId = "delayed-data-detection-job-missing-test-specific-bucket"; Job.Builder job = createJob(jobId, TimeValue.timeValueMinutes(5), "count", null); - DatafeedConfig.Builder datafeedConfigBuilder = - createDatafeedBuilder(job.getId() + "-datafeed", job.getId(), Collections.singletonList(index)); + DatafeedConfig.Builder datafeedConfigBuilder = createDatafeedBuilder( + job.getId() + "-datafeed", + job.getId(), + Collections.singletonList(index) + ); datafeedConfigBuilder.setDelayedDataCheckConfig(DelayedDataCheckConfig.enabledDelayedDataCheckConfig(TimeValue.timeValueHours(12))); DatafeedConfig datafeedConfig = datafeedConfigBuilder.build(); putJob(job); openJob(job.getId()); - putDatafeed(datafeedConfig); startDatafeed(datafeedConfig.getId(), 0L, now); @@ -125,8 +127,8 @@ public void testMissingDataDetectionInSpecificBucket() throws Exception { long missingDocs = randomIntBetween(1, 10); // Write our missing data in the bucket right before the last finalized bucket - writeData(logger, index, missingDocs, (lastBucket.getEpoch() - lastBucket.getBucketSpan())*1000, lastBucket.getEpoch()*1000); - List response = delayedDataDetector.detectMissingData(lastBucket.getEpoch()*1000); + writeData(logger, index, missingDocs, (lastBucket.getEpoch() - lastBucket.getBucketSpan()) * 1000, lastBucket.getEpoch() * 1000); + List response = delayedDataDetector.detectMissingData(lastBucket.getEpoch() * 1000); boolean hasBucketWithMissing = false; for (BucketWithMissingData bucketWithMissingData : response) { @@ -149,16 +151,21 @@ public void testMissingDataDetectionWithAggregationsAndQuery() throws Exception MaxAggregationBuilder maxTime = AggregationBuilders.max("time").field("time"); AvgAggregationBuilder avgAggregationBuilder = AggregationBuilders.avg("value").field("value"); - DatafeedConfig.Builder datafeedConfigBuilder = createDatafeedBuilder(job.getId() + "-datafeed", + DatafeedConfig.Builder datafeedConfigBuilder = createDatafeedBuilder( + job.getId() + "-datafeed", job.getId(), - Collections.singletonList(index)); - datafeedConfigBuilder.setParsedAggregations(new AggregatorFactories.Builder().addAggregator( + Collections.singletonList(index) + ); + datafeedConfigBuilder.setParsedAggregations( + new AggregatorFactories.Builder().addAggregator( AggregationBuilders.histogram("time") .subAggregation(maxTime) .subAggregation(avgAggregationBuilder) .field("time") - .interval(TimeValue.timeValueMinutes(5).millis()))); - datafeedConfigBuilder.setParsedQuery(QueryBuilders.rangeQuery("value").gte(numDocs/2)); + .interval(TimeValue.timeValueMinutes(5).millis()) + ) + ); + datafeedConfigBuilder.setParsedQuery(QueryBuilders.rangeQuery("value").gte(numDocs / 2)); datafeedConfigBuilder.setFrequency(TimeValue.timeValueMinutes(5)); datafeedConfigBuilder.setDelayedDataCheckConfig(DelayedDataCheckConfig.enabledDelayedDataCheckConfig(TimeValue.timeValueHours(12))); @@ -166,7 +173,6 @@ public void testMissingDataDetectionWithAggregationsAndQuery() throws Exception putJob(job); openJob(job.getId()); - putDatafeed(datafeedConfig); startDatafeed(datafeedConfig.getId(), 0L, now); waitUntilJobIsClosed(jobId); @@ -176,16 +182,16 @@ public void testMissingDataDetectionWithAggregationsAndQuery() throws Exception DelayedDataDetector delayedDataDetector = newDetector(job.build(new Date()), datafeedConfig); - List response = delayedDataDetector.detectMissingData(lastBucket.getEpoch()*1000); + List response = delayedDataDetector.detectMissingData(lastBucket.getEpoch() * 1000); assertThat(response.stream().mapToLong(BucketWithMissingData::getMissingDocumentCount).sum(), equalTo(0L)); long missingDocs = numDocs; // Simply adding data within the current delayed data detection, the choice of 43100000 is arbitrary and within the window // for the DatafeedDelayedDataDetector - writeData(logger, index, missingDocs, now - 43100000, lastBucket.getEpoch()*1000); + writeData(logger, index, missingDocs, now - 43100000, lastBucket.getEpoch() * 1000); - response = delayedDataDetector.detectMissingData(lastBucket.getEpoch()*1000); - assertThat(response.stream().mapToLong(BucketWithMissingData::getMissingDocumentCount).sum(), equalTo((missingDocs+1)/2)); + response = delayedDataDetector.detectMissingData(lastBucket.getEpoch() * 1000); + assertThat(response.stream().mapToLong(BucketWithMissingData::getMissingDocumentCount).sum(), equalTo((missingDocs + 1) / 2)); // Assert that the are returned in order List timeStamps = response.stream().map(BucketWithMissingData::getTimeStamp).collect(Collectors.toList()); assertEquals(timeStamps.stream().sorted().collect(Collectors.toList()), timeStamps); @@ -222,9 +228,7 @@ private void writeData(Logger logger, String index, long numDocs, long start, lo indexRequest.source("time", timestamp, "value", i); bulkRequestBuilder.add(indexRequest); } - BulkResponse bulkResponse = bulkRequestBuilder - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .get(); + BulkResponse bulkResponse = bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).get(); if (bulkResponse.hasFailures()) { int failures = 0; for (BulkItemResponse itemResponse : bulkResponse) { diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DeleteExpiredDataIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DeleteExpiredDataIT.java index a284843a307cd..9b58cbe3a70d0 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DeleteExpiredDataIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DeleteExpiredDataIT.java @@ -18,14 +18,14 @@ import org.elasticsearch.action.update.UpdateAction; import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.common.Strings; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.search.SearchHit; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.ml.action.DeleteExpiredDataAction; import org.elasticsearch.xpack.core.ml.action.UpdateModelSnapshotAction; import org.elasticsearch.xpack.core.ml.annotations.Annotation; @@ -70,10 +70,8 @@ public class DeleteExpiredDataIT extends MlNativeAutodetectIntegTestCase { private static final String USER_NAME = "some-user"; @Before - public void setUpData() { - client().admin().indices().prepareCreate(DATA_INDEX) - .setMapping("time", "type=date,format=epoch_millis") - .get(); + public void setUpData() { + client().admin().indices().prepareCreate(DATA_INDEX).setMapping("time", "type=date,format=epoch_millis").get(); // We are going to create 3 days of data ending 1 hr ago long latestBucketTime = System.currentTimeMillis() - TimeValue.timeValueHours(1).millis(); @@ -92,9 +90,7 @@ public void setUpData() { } } - BulkResponse bulkResponse = bulkRequestBuilder - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .get(); + BulkResponse bulkResponse = bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).get(); assertThat(bulkResponse.hasFailures(), is(false)); } @@ -109,7 +105,7 @@ public void testDeleteExpiredData_GivenNothingToDelete() throws Exception { client().execute(DeleteExpiredDataAction.INSTANCE, new DeleteExpiredDataAction.Request()).get(); } - @AwaitsFix( bugUrl = "https://github.com/elastic/elasticsearch/issues/62699") + @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/62699") public void testDeleteExpiredDataNoThrottle() throws Exception { testExpiredDeletion(null, 10010); } @@ -138,21 +134,25 @@ public void testDeleteExpiredDataActionDeletesEmptyStateIndices() throws Excepti refresh(); GetIndexResponse getIndexResponse = client().admin().indices().prepareGetIndex().setIndices(".ml-state*").get(); - assertThat(Strings.toString(getIndexResponse), + assertThat( + Strings.toString(getIndexResponse), getIndexResponse.getIndices(), - is(arrayContaining(".ml-state", ".ml-state-000001", ".ml-state-000003", ".ml-state-000005", ".ml-state-000007"))); + is(arrayContaining(".ml-state", ".ml-state-000001", ".ml-state-000003", ".ml-state-000005", ".ml-state-000007")) + ); client().execute(DeleteExpiredDataAction.INSTANCE, new DeleteExpiredDataAction.Request()).get(); refresh(); getIndexResponse = client().admin().indices().prepareGetIndex().setIndices(".ml-state*").get(); - assertThat(Strings.toString(getIndexResponse), + assertThat( + Strings.toString(getIndexResponse), getIndexResponse.getIndices(), // Only non-empty or current indices should survive deletion process - is(arrayContaining(".ml-state-000001", ".ml-state-000005", ".ml-state-000007"))); + is(arrayContaining(".ml-state-000001", ".ml-state-000005", ".ml-state-000007")) + ); } - @AwaitsFix( bugUrl = "https://github.com/elastic/elasticsearch/issues/62699") + @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/62699") public void testDeleteExpiredDataWithStandardThrottle() throws Exception { testExpiredDeletion(-1.0f, 100); } @@ -163,26 +163,38 @@ private void testExpiredDeletion(Float customThrottle, int numUnusedState) throw BulkRequestBuilder bulkRequestBuilder = client().prepareBulk().setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); for (int i = 0; i < numUnusedState; i++) { String docId = "non_existing_job_" + randomFrom("model_state_1234567#" + i, "quantiles", "categorizer_state#" + i); - IndexRequest indexRequest = - new IndexRequest(mlStateIndexName) - .id(docId) - .source(Collections.emptyMap()); + IndexRequest indexRequest = new IndexRequest(mlStateIndexName).id(docId).source(Collections.emptyMap()); bulkRequestBuilder.add(indexRequest); } ActionFuture indexUnusedStateDocsResponse = bulkRequestBuilder.execute(); List jobs = new ArrayList<>(); // These jobs don't thin out model state; ModelSnapshotRetentionIT tests that - jobs.add(newJobBuilder("no-retention") - .setResultsRetentionDays(null).setModelSnapshotRetentionDays(1000L).setDailyModelSnapshotRetentionAfterDays(1000L)); - jobs.add(newJobBuilder("results-retention") - .setResultsRetentionDays(1L).setModelSnapshotRetentionDays(1000L).setDailyModelSnapshotRetentionAfterDays(1000L)); - jobs.add(newJobBuilder("snapshots-retention") - .setResultsRetentionDays(null).setModelSnapshotRetentionDays(2L).setDailyModelSnapshotRetentionAfterDays(2L)); - jobs.add(newJobBuilder("snapshots-retention-with-retain") - .setResultsRetentionDays(null).setModelSnapshotRetentionDays(2L).setDailyModelSnapshotRetentionAfterDays(2L)); - jobs.add(newJobBuilder("results-and-snapshots-retention") - .setResultsRetentionDays(1L).setModelSnapshotRetentionDays(2L).setDailyModelSnapshotRetentionAfterDays(2L)); + jobs.add( + newJobBuilder("no-retention").setResultsRetentionDays(null) + .setModelSnapshotRetentionDays(1000L) + .setDailyModelSnapshotRetentionAfterDays(1000L) + ); + jobs.add( + newJobBuilder("results-retention").setResultsRetentionDays(1L) + .setModelSnapshotRetentionDays(1000L) + .setDailyModelSnapshotRetentionAfterDays(1000L) + ); + jobs.add( + newJobBuilder("snapshots-retention").setResultsRetentionDays(null) + .setModelSnapshotRetentionDays(2L) + .setDailyModelSnapshotRetentionAfterDays(2L) + ); + jobs.add( + newJobBuilder("snapshots-retention-with-retain").setResultsRetentionDays(null) + .setModelSnapshotRetentionDays(2L) + .setDailyModelSnapshotRetentionAfterDays(2L) + ); + jobs.add( + newJobBuilder("results-and-snapshots-retention").setResultsRetentionDays(1L) + .setModelSnapshotRetentionDays(2L) + .setDailyModelSnapshotRetentionAfterDays(2L) + ); List shortExpiryForecastIds = new ArrayList<>(); @@ -260,9 +272,13 @@ private void testExpiredDeletion(Float customThrottle, int numUnusedState) throw long totalModelSizeStatsBeforeDelete = client().prepareSearch("*") .setIndicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN_CLOSED_HIDDEN) .setQuery(QueryBuilders.termQuery("result_type", "model_size_stats")) - .get().getHits().getTotalHits().value; - long totalNotificationsCountBeforeDelete = - client().prepareSearch(NotificationsIndex.NOTIFICATIONS_INDEX).get().getHits().getTotalHits().value; + .get() + .getHits() + .getTotalHits().value; + long totalNotificationsCountBeforeDelete = client().prepareSearch(NotificationsIndex.NOTIFICATIONS_INDEX) + .get() + .getHits() + .getTotalHits().value; assertThat(totalModelSizeStatsBeforeDelete, greaterThan(0L)); assertThat(totalNotificationsCountBeforeDelete, greaterThan(0L)); @@ -309,9 +325,13 @@ private void testExpiredDeletion(Float customThrottle, int numUnusedState) throw long totalModelSizeStatsAfterDelete = client().prepareSearch("*") .setIndicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN_CLOSED_HIDDEN) .setQuery(QueryBuilders.termQuery("result_type", "model_size_stats")) - .get().getHits().getTotalHits().value; - long totalNotificationsCountAfterDelete = - client().prepareSearch(NotificationsIndex.NOTIFICATIONS_INDEX).get().getHits().getTotalHits().value; + .get() + .getHits() + .getTotalHits().value; + long totalNotificationsCountAfterDelete = client().prepareSearch(NotificationsIndex.NOTIFICATIONS_INDEX) + .get() + .getHits() + .getTotalHits().value; assertThat(totalModelSizeStatsAfterDelete, equalTo(totalModelSizeStatsBeforeDelete)); assertThat(totalNotificationsCountAfterDelete, greaterThanOrEqualTo(totalNotificationsCountBeforeDelete)); @@ -347,8 +367,11 @@ private void testExpiredDeletion(Float customThrottle, int numUnusedState) throw } } } - assertThat("Documents for non_existing_job are still around; examples: " + nonExistingJobExampleIds, - nonExistingJobDocsCount, equalTo(0)); + assertThat( + "Documents for non_existing_job are still around; examples: " + nonExistingJobExampleIds, + nonExistingJobDocsCount, + equalTo(0) + ); } public void testDeleteExpiresDataDeletesAnnotations() throws Exception { @@ -358,22 +381,17 @@ public void testDeleteExpiresDataDeletesAnnotations() throws Exception { // No annotations so far assertThatNumberOfAnnotationsIsEqualTo(0); - Job.Builder job = - new Job.Builder(jobId) - .setResultsRetentionDays(1L) - .setAnalysisConfig( - new AnalysisConfig.Builder(Collections.singletonList(new Detector.Builder().setFunction("count").build())) - .setBucketSpan(TimeValue.timeValueHours(1))) - .setDataDescription( - new DataDescription.Builder() - .setTimeField(TIME_FIELD)); + Job.Builder job = new Job.Builder(jobId).setResultsRetentionDays(1L) + .setAnalysisConfig( + new AnalysisConfig.Builder(Collections.singletonList(new Detector.Builder().setFunction("count").build())).setBucketSpan( + TimeValue.timeValueHours(1) + ) + ) + .setDataDescription(new DataDescription.Builder().setTimeField(TIME_FIELD)); putJob(job); - DatafeedConfig datafeed = - new DatafeedConfig.Builder(datafeedId, jobId) - .setIndices(Collections.singletonList(DATA_INDEX)) - .build(); + DatafeedConfig datafeed = new DatafeedConfig.Builder(datafeedId, jobId).setIndices(Collections.singletonList(DATA_INDEX)).build(); putDatafeed(datafeed); @@ -405,14 +423,11 @@ public void testDeleteExpiresDataDeletesAnnotations() throws Exception { } private static IndexRequest randomAnnotationIndexRequest(String jobId, Instant timestamp, String createUsername) throws IOException { - Annotation annotation = - new Annotation.Builder(randomAnnotation(jobId)) - .setTimestamp(Date.from(timestamp)) - .setCreateUsername(createUsername) - .build(); + Annotation annotation = new Annotation.Builder(randomAnnotation(jobId)).setTimestamp(Date.from(timestamp)) + .setCreateUsername(createUsername) + .build(); try (XContentBuilder xContentBuilder = annotation.toXContent(XContentFactory.jsonBuilder(), ToXContent.EMPTY_PARAMS)) { - return new IndexRequest(AnnotationIndex.WRITE_ALIAS_NAME) - .source(xContentBuilder) + return new IndexRequest(AnnotationIndex.WRITE_ALIAS_NAME).source(xContentBuilder) .setRequireAlias(true) .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); } diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DeleteJobIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DeleteJobIT.java index e5845172a079a..a05b0b646d359 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DeleteJobIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DeleteJobIT.java @@ -35,10 +35,8 @@ public class DeleteJobIT extends MlNativeAutodetectIntegTestCase { private static final String TIME_FIELD = "time"; @Before - public void setUpData() { - client().admin().indices().prepareCreate(DATA_INDEX) - .setMapping(TIME_FIELD, "type=date,format=epoch_millis") - .get(); + public void setUpData() { + client().admin().indices().prepareCreate(DATA_INDEX).setMapping(TIME_FIELD, "type=date,format=epoch_millis").get(); } @After @@ -89,13 +87,12 @@ public void testDeletingMultipleJobsInOneRequestIsImpossible() { private void runJob(String jobId, String datafeedId) throws Exception { Detector.Builder detector = new Detector.Builder().setFunction("count"); - AnalysisConfig.Builder analysisConfig = new AnalysisConfig.Builder(Collections.singletonList(detector.build())) - .setBucketSpan(TimeValue.timeValueHours(1)); + AnalysisConfig.Builder analysisConfig = new AnalysisConfig.Builder(Collections.singletonList(detector.build())).setBucketSpan( + TimeValue.timeValueHours(1) + ); DataDescription.Builder dataDescription = new DataDescription.Builder(); dataDescription.setTimeField(TIME_FIELD); - Job.Builder job = new Job.Builder(jobId) - .setAnalysisConfig(analysisConfig) - .setDataDescription(dataDescription); + Job.Builder job = new Job.Builder(jobId).setAnalysisConfig(analysisConfig).setDataDescription(dataDescription); putJob(job); @@ -115,8 +112,7 @@ private void runJob(String jobId, String datafeedId) throws Exception { private static IndexRequest randomAnnotationIndexRequest(String jobId, String createUsername) throws IOException { Annotation annotation = new Annotation.Builder(randomAnnotation(jobId)).setCreateUsername(createUsername).build(); try (XContentBuilder xContentBuilder = annotation.toXContent(XContentFactory.jsonBuilder(), ToXContent.EMPTY_PARAMS)) { - return new IndexRequest(AnnotationIndex.WRITE_ALIAS_NAME) - .source(xContentBuilder) + return new IndexRequest(AnnotationIndex.WRITE_ALIAS_NAME).source(xContentBuilder) .setRequireAlias(true) .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); } diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DetectionRulesIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DetectionRulesIT.java index 4329f1ea81b5e..a8825ea46774c 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DetectionRulesIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DetectionRulesIT.java @@ -53,9 +53,8 @@ public void cleanUpTest() { } public void testCondition() throws Exception { - DetectionRule rule = new DetectionRule.Builder(Arrays.asList( - new RuleCondition(RuleCondition.AppliesTo.ACTUAL, Operator.LT, 100.0) - )).build(); + DetectionRule rule = new DetectionRule.Builder(Arrays.asList(new RuleCondition(RuleCondition.AppliesTo.ACTUAL, Operator.LT, 100.0))) + .build(); Detector.Builder detector = new Detector.Builder("mean", "value"); detector.setByFieldName("by_field"); @@ -102,9 +101,9 @@ public void testCondition() throws Exception { { // Update rules so that the anomalies suppression is inverted - DetectionRule newRule = new DetectionRule.Builder(Arrays.asList( - new RuleCondition(RuleCondition.AppliesTo.ACTUAL, Operator.GT, 700.0) - )).build(); + DetectionRule newRule = new DetectionRule.Builder( + Arrays.asList(new RuleCondition(RuleCondition.AppliesTo.ACTUAL, Operator.GT, 700.0)) + ).build(); JobUpdate.Builder update = new JobUpdate.Builder(job.getId()); update.setDetectorUpdates(Arrays.asList(new JobUpdate.DetectorUpdate(0, null, Arrays.asList(newRule)))); updateJob(job.getId(), update.build()); @@ -118,7 +117,7 @@ public void testCondition() throws Exception { GetRecordsAction.Request recordsAfterFirstHalf = new GetRecordsAction.Request(job.getId()); recordsAfterFirstHalf.setStart(String.valueOf(firstRecordTimestamp + 1)); records = getRecords(recordsAfterFirstHalf); - assertThat("records were " + records, (int)(records.stream().filter(r -> r.getProbability() < 0.01).count()), equalTo(1)); + assertThat("records were " + records, (int) (records.stream().filter(r -> r.getProbability() < 0.01).count()), equalTo(1)); assertThat(records.get(0).getByFieldValue(), equalTo("low")); } @@ -187,14 +186,15 @@ public void testScope() throws Exception { // Wait until the notification that the filter was updated is indexed assertBusy(() -> { - SearchResponse searchResponse = - client().prepareSearch(NotificationsIndex.NOTIFICATIONS_INDEX) - .setSize(1) - .addSort("timestamp", SortOrder.DESC) - .setQuery(QueryBuilders.boolQuery() - .filter(QueryBuilders.termQuery("job_id", job.getId())) - .filter(QueryBuilders.termQuery("level", "info")) - ).get(); + SearchResponse searchResponse = client().prepareSearch(NotificationsIndex.NOTIFICATIONS_INDEX) + .setSize(1) + .addSort("timestamp", SortOrder.DESC) + .setQuery( + QueryBuilders.boolQuery() + .filter(QueryBuilders.termQuery("job_id", job.getId())) + .filter(QueryBuilders.termQuery("level", "info")) + ) + .get(); SearchHit[] hits = searchResponse.getHits().getHits(); assertThat(hits.length, equalTo(1)); assertThat((String) hits[0].getSourceAsMap().get("message"), containsString("Filter [safe_ips] has been modified")); @@ -238,9 +238,9 @@ public void testScopeAndCondition() throws IOException { assertThat(putMlFilter(safeIps).getFilter(), equalTo(safeIps)); // Ignore if ip in safe list AND actual < 10. - DetectionRule rule = new DetectionRule.Builder(RuleScope.builder().include("ip", "safe_ips")) - .setConditions(Arrays.asList(new RuleCondition(RuleCondition.AppliesTo.ACTUAL, Operator.LT, 10.0))) - .build(); + DetectionRule rule = new DetectionRule.Builder(RuleScope.builder().include("ip", "safe_ips")).setConditions( + Arrays.asList(new RuleCondition(RuleCondition.AppliesTo.ACTUAL, Operator.LT, 10.0)) + ).build(); Detector.Builder detector = new Detector.Builder("count", null); detector.setRules(Arrays.asList(rule)); diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ExplainDataFrameAnalyticsIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ExplainDataFrameAnalyticsIT.java index 3d20104430c8b..b321f2f899638 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ExplainDataFrameAnalyticsIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ExplainDataFrameAnalyticsIT.java @@ -43,10 +43,9 @@ public class ExplainDataFrameAnalyticsIT extends MlNativeDataFrameAnalyticsIntegTestCase { public void testExplain_GivenMissingSourceIndex() { - DataFrameAnalyticsConfig config = new DataFrameAnalyticsConfig.Builder() - .setSource(new DataFrameAnalyticsSource(new String[] {"missing_index"}, null, null, Collections.emptyMap())) - .setAnalysis(new OutlierDetection.Builder().build()) - .buildForExplain(); + DataFrameAnalyticsConfig config = new DataFrameAnalyticsConfig.Builder().setSource( + new DataFrameAnalyticsSource(new String[] { "missing_index" }, null, null, Collections.emptyMap()) + ).setAnalysis(new OutlierDetection.Builder().build()).buildForExplain(); ResourceNotFoundException e = expectThrows(ResourceNotFoundException.class, () -> explainDataFrame(config)); assertThat(e.getMessage(), equalTo("cannot retrieve data because index [missing_index] does not exist")); @@ -59,12 +58,19 @@ public void testSourceQueryIsApplied() throws IOException { String sourceIndex = "test-source-query-is-applied"; - client().admin().indices().prepareCreate(sourceIndex) + client().admin() + .indices() + .prepareCreate(sourceIndex) .setMapping( - "numeric_1", "type=double", - "numeric_2", "type=unsigned_long", - "categorical", "type=keyword", - "filtered_field", "type=keyword") + "numeric_1", + "type=double", + "numeric_2", + "type=unsigned_long", + "categorical", + "type=keyword", + "filtered_field", + "type=keyword" + ) .get(); BulkRequestBuilder bulkRequestBuilder = client().prepareBulk(); @@ -73,10 +79,15 @@ public void testSourceQueryIsApplied() throws IOException { for (int i = 0; i < 30; i++) { IndexRequest indexRequest = new IndexRequest(sourceIndex); indexRequest.source( - "numeric_1", 1.0, - "numeric_2", 2, - "categorical", i % 2 == 0 ? "class_1" : "class_2", - "filtered_field", i < 2 ? "bingo" : "rest"); // We tag bingo on the first two docs to ensure we have 2 classes + "numeric_1", + 1.0, + "numeric_2", + 2, + "categorical", + i % 2 == 0 ? "class_1" : "class_2", + "filtered_field", + i < 2 ? "bingo" : "rest" + ); // We tag bingo on the first two docs to ensure we have 2 classes bulkRequestBuilder.add(indexRequest); } BulkResponse bulkResponse = bulkRequestBuilder.get(); @@ -86,12 +97,15 @@ public void testSourceQueryIsApplied() throws IOException { String id = "test_source_query_is_applied"; - DataFrameAnalyticsConfig config = new DataFrameAnalyticsConfig.Builder() - .setId(id) - .setSource(new DataFrameAnalyticsSource(new String[] { sourceIndex }, - QueryProvider.fromParsedQuery(QueryBuilders.termQuery("filtered_field", "bingo")), - null, - Collections.emptyMap())) + DataFrameAnalyticsConfig config = new DataFrameAnalyticsConfig.Builder().setId(id) + .setSource( + new DataFrameAnalyticsSource( + new String[] { sourceIndex }, + QueryProvider.fromParsedQuery(QueryBuilders.termQuery("filtered_field", "bingo")), + null, + Collections.emptyMap() + ) + ) .setAnalysis(new Classification("categorical")) .buildForExplain(); @@ -104,48 +118,61 @@ public void testTrainingPercentageIsApplied() throws IOException { String sourceIndex = "test-training-percentage-applied"; RegressionIT.indexData(sourceIndex, 100, 0); - DataFrameAnalyticsConfig config = new DataFrameAnalyticsConfig.Builder() - .setId("dfa-training-100-" + sourceIndex) - .setSource(new DataFrameAnalyticsSource(new String[] { sourceIndex }, - QueryProvider.fromParsedQuery(QueryBuilders.matchAllQuery()), - null, - Collections.emptyMap())) - .setAnalysis(new Regression(RegressionIT.DEPENDENT_VARIABLE_FIELD, - BoostedTreeParams.builder().build(), - null, - 100.0, - null, - null, - null, - null, - null)) + DataFrameAnalyticsConfig config = new DataFrameAnalyticsConfig.Builder().setId("dfa-training-100-" + sourceIndex) + .setSource( + new DataFrameAnalyticsSource( + new String[] { sourceIndex }, + QueryProvider.fromParsedQuery(QueryBuilders.matchAllQuery()), + null, + Collections.emptyMap() + ) + ) + .setAnalysis( + new Regression( + RegressionIT.DEPENDENT_VARIABLE_FIELD, + BoostedTreeParams.builder().build(), + null, + 100.0, + null, + null, + null, + null, + null + ) + ) .buildForExplain(); ExplainDataFrameAnalyticsAction.Response explainResponse = explainDataFrame(config); ByteSizeValue allDataUsedForTraining = explainResponse.getMemoryEstimation().getExpectedMemoryWithoutDisk(); - config = new DataFrameAnalyticsConfig.Builder() - .setId("dfa-training-50-" + sourceIndex) - .setSource(new DataFrameAnalyticsSource(new String[] { sourceIndex }, - QueryProvider.fromParsedQuery(QueryBuilders.matchAllQuery()), - null, - Collections.emptyMap())) - .setAnalysis(new Regression(RegressionIT.DEPENDENT_VARIABLE_FIELD, - BoostedTreeParams.builder().build(), - null, - 50.0, - null, - null, - null, - null, - null)) + config = new DataFrameAnalyticsConfig.Builder().setId("dfa-training-50-" + sourceIndex) + .setSource( + new DataFrameAnalyticsSource( + new String[] { sourceIndex }, + QueryProvider.fromParsedQuery(QueryBuilders.matchAllQuery()), + null, + Collections.emptyMap() + ) + ) + .setAnalysis( + new Regression( + RegressionIT.DEPENDENT_VARIABLE_FIELD, + BoostedTreeParams.builder().build(), + null, + 50.0, + null, + null, + null, + null, + null + ) + ) .buildForExplain(); explainResponse = explainDataFrame(config); - assertThat(explainResponse.getMemoryEstimation().getExpectedMemoryWithoutDisk(), - lessThanOrEqualTo(allDataUsedForTraining)); + assertThat(explainResponse.getMemoryEstimation().getExpectedMemoryWithoutDisk(), lessThanOrEqualTo(allDataUsedForTraining)); } public void testSimultaneousExplainSameConfig() throws IOException { @@ -155,21 +182,28 @@ public void testSimultaneousExplainSameConfig() throws IOException { String sourceIndex = "test-simultaneous-explain"; RegressionIT.indexData(sourceIndex, 100, 0); - DataFrameAnalyticsConfig config = new DataFrameAnalyticsConfig.Builder() - .setId("dfa-simultaneous-explain-" + sourceIndex) - .setSource(new DataFrameAnalyticsSource(new String[]{sourceIndex}, - QueryProvider.fromParsedQuery(QueryBuilders.matchAllQuery()), - null, - Collections.emptyMap())) - .setAnalysis(new Regression(RegressionIT.DEPENDENT_VARIABLE_FIELD, - BoostedTreeParams.builder().build(), - null, - 100.0, - null, - null, - null, - null, - null)) + DataFrameAnalyticsConfig config = new DataFrameAnalyticsConfig.Builder().setId("dfa-simultaneous-explain-" + sourceIndex) + .setSource( + new DataFrameAnalyticsSource( + new String[] { sourceIndex }, + QueryProvider.fromParsedQuery(QueryBuilders.matchAllQuery()), + null, + Collections.emptyMap() + ) + ) + .setAnalysis( + new Regression( + RegressionIT.DEPENDENT_VARIABLE_FIELD, + BoostedTreeParams.builder().build(), + null, + 100.0, + null, + null, + null, + null, + null + ) + ) .buildForExplain(); List> futures = new ArrayList<>(); @@ -196,26 +230,23 @@ public void testSimultaneousExplainSameConfig() throws IOException { public void testRuntimeFields() { String sourceIndex = "test-explain-runtime-fields"; - String mapping = "{\n" + - " \"properties\": {\n" + - " \"mapped_field\": {\n" + - " \"type\": \"double\"\n" + - " }\n" + - " },\n" + - " \"runtime\": {\n" + - " \"mapped_runtime_field\": {\n" + - " \"type\": \"double\"\n," + - " \"script\": \"emit(doc['mapped_field'].value + 10.0)\"\n" + - " }\n" + - " }\n" + - " }"; - client().admin().indices().prepareCreate(sourceIndex) - .setMapping(mapping) - .get(); - BulkRequestBuilder bulkRequestBuilder = client().prepareBulk() - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); + String mapping = "{\n" + + " \"properties\": {\n" + + " \"mapped_field\": {\n" + + " \"type\": \"double\"\n" + + " }\n" + + " },\n" + + " \"runtime\": {\n" + + " \"mapped_runtime_field\": {\n" + + " \"type\": \"double\"\n," + + " \"script\": \"emit(doc['mapped_field'].value + 10.0)\"\n" + + " }\n" + + " }\n" + + " }"; + client().admin().indices().prepareCreate(sourceIndex).setMapping(mapping).get(); + BulkRequestBuilder bulkRequestBuilder = client().prepareBulk().setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); for (int i = 0; i < 10; i++) { - Object[] source = new Object[] {"mapped_field", i}; + Object[] source = new Object[] { "mapped_field", i }; IndexRequest indexRequest = new IndexRequest(sourceIndex).source(source).opType(DocWriteRequest.OpType.CREATE); bulkRequestBuilder.add(indexRequest); } @@ -229,8 +260,7 @@ public void testRuntimeFields() { configRuntimeField.put("script", "emit(doc['mapped_field'].value + 20.0)"); Map configRuntimeFields = Collections.singletonMap("config_runtime_field", configRuntimeField); - DataFrameAnalyticsConfig config = new DataFrameAnalyticsConfig.Builder() - .setId(sourceIndex + "-job") + DataFrameAnalyticsConfig config = new DataFrameAnalyticsConfig.Builder().setId(sourceIndex + "-job") .setSource(new DataFrameAnalyticsSource(new String[] { sourceIndex }, null, null, configRuntimeFields)) .setDest(new DataFrameAnalyticsDest(sourceIndex + "-results", null)) .setAnalysis(new OutlierDetection.Builder().build()) @@ -240,8 +270,10 @@ public void testRuntimeFields() { List fieldSelection = explainResponse.getFieldSelection(); assertThat(fieldSelection.size(), equalTo(3)); - assertThat(fieldSelection.stream().map(FieldSelection::getName).collect(Collectors.toList()), - contains("config_runtime_field", "mapped_field", "mapped_runtime_field")); + assertThat( + fieldSelection.stream().map(FieldSelection::getName).collect(Collectors.toList()), + contains("config_runtime_field", "mapped_field", "mapped_runtime_field") + ); assertThat(fieldSelection.stream().map(FieldSelection::isIncluded).allMatch(isIncluded -> isIncluded), is(true)); } diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ExplainDataFrameAnalyticsRestIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ExplainDataFrameAnalyticsRestIT.java index 289054c610596..eabcc647c3faf 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ExplainDataFrameAnalyticsRestIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ExplainDataFrameAnalyticsRestIT.java @@ -56,10 +56,16 @@ private void setupUser(String user, List roles) throws IOException { String password = new String(SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING.getChars()); Request request = new Request("PUT", "/_security/user/" + user); - request.setJsonEntity("{" - + " \"password\" : \"" + password + "\"," - + " \"roles\" : [ " + roles.stream().map(unquoted -> "\"" + unquoted + "\"").collect(Collectors.joining(", ")) + " ]" - + "}"); + request.setJsonEntity( + "{" + + " \"password\" : \"" + + password + + "\"," + + " \"roles\" : [ " + + roles.stream().map(unquoted -> "\"" + unquoted + "\"").collect(Collectors.joining(", ")) + + " ]" + + "}" + ); client().performRequest(request); } @@ -76,7 +82,8 @@ private void addAirlineData() throws IOException { // Create index with source = enabled, doc_values = enabled, stored = false + multi-field Request createAirlineDataRequest = new Request("PUT", "/airline-data"); - createAirlineDataRequest.setJsonEntity("{" + createAirlineDataRequest.setJsonEntity( + "{" + " \"mappings\": {" + " \"properties\": {" + " \"time stamp\": { \"type\":\"date\"}," // space in 'time stamp' is intentional @@ -84,9 +91,10 @@ private void addAirlineData() throws IOException { + " \"type\":\"keyword\"" + " }," + " \"responsetime\": { \"type\":\"float\"}" - + " }" + + " }" + " }" - + "}"); + + "}" + ); client().performRequest(createAirlineDataRequest); bulk.append("{\"index\": {\"_index\": \"airline-data\", \"_id\": 1}}\n"); @@ -98,17 +106,16 @@ private void addAirlineData() throws IOException { } public void testExplain_GivenSecondaryHeadersAndConfig() throws IOException { - String config = "{\n" + - " \"source\": {\n" + - " \"index\": \"airline-data\"\n" + - " },\n" + - " \"analysis\": {\n" + - " \"regression\": {\n" + - " \"dependent_variable\": \"responsetime\"\n" + - " }\n" + - " }\n" + - "}"; - + String config = "{\n" + + " \"source\": {\n" + + " \"index\": \"airline-data\"\n" + + " },\n" + + " \"analysis\": {\n" + + " \"regression\": {\n" + + " \"dependent_variable\": \"responsetime\"\n" + + " }\n" + + " }\n" + + "}"; { // Request with secondary headers without perms Request explain = explainRequestViaConfig(config); @@ -131,20 +138,20 @@ public void testExplain_GivenSecondaryHeadersAndConfig() throws IOException { } public void testExplain_GivenSecondaryHeadersAndPreviouslyStoredConfig() throws IOException { - String config = "{\n" + - " \"source\": {\n" + - " \"index\": \"airline-data\"\n" + - " },\n" + - " \"dest\": {\n" + - " \"index\": \"response_prediction\"\n" + - " },\n" + - " \"analysis\":\n" + - " {\n" + - " \"regression\": {\n" + - " \"dependent_variable\": \"responsetime\"\n" + - " }\n" + - " }\n" + - "}"; + String config = "{\n" + + " \"source\": {\n" + + " \"index\": \"airline-data\"\n" + + " },\n" + + " \"dest\": {\n" + + " \"index\": \"response_prediction\"\n" + + " },\n" + + " \"analysis\":\n" + + " {\n" + + " \"regression\": {\n" + + " \"dependent_variable\": \"responsetime\"\n" + + " }\n" + + " }\n" + + "}"; String configId = "explain_test"; diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ForecastIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ForecastIT.java index a297d44da2e88..e9dc6ed52ef06 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ForecastIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ForecastIT.java @@ -34,7 +34,6 @@ import java.util.Map; import java.util.stream.Collectors; import java.util.stream.IntStream; -import java.util.stream.Stream; import static org.elasticsearch.xpack.core.ml.job.messages.Messages.JOB_FORECAST_NATIVE_PROCESS_KILLED; import static org.hamcrest.Matchers.closeTo; @@ -100,9 +99,10 @@ public void testSingleSeries() throws Exception { { ForecastRequestStats forecastDefaultDurationDefaultExpiry = idToForecastStats.get(forecastIdDefaultDurationDefaultExpiry); - assertThat(forecastDefaultDurationDefaultExpiry.getExpiryTime().toEpochMilli(), - equalTo(forecastDefaultDurationDefaultExpiry.getCreateTime().toEpochMilli() - + TimeValue.timeValueHours(14 * 24).getMillis())); + assertThat( + forecastDefaultDurationDefaultExpiry.getExpiryTime().toEpochMilli(), + equalTo(forecastDefaultDurationDefaultExpiry.getCreateTime().toEpochMilli() + TimeValue.timeValueHours(14 * 24).getMillis()) + ); List forecasts = getForecasts(job.getId(), forecastDefaultDurationDefaultExpiry); assertThat(forecastDefaultDurationDefaultExpiry.getRecordCount(), equalTo(24L)); assertThat(forecasts.size(), equalTo(24)); @@ -132,9 +132,10 @@ public void testSingleSeries() throws Exception { { ForecastRequestStats forecastDuration3HoursExpiresIn24Hours = idToForecastStats.get(forecastIdDuration3HoursExpiresIn24Hours); - assertThat(forecastDuration3HoursExpiresIn24Hours.getExpiryTime().toEpochMilli(), - equalTo(forecastDuration3HoursExpiresIn24Hours.getCreateTime().toEpochMilli() - + TimeValue.timeValueHours(24).getMillis())); + assertThat( + forecastDuration3HoursExpiresIn24Hours.getExpiryTime().toEpochMilli(), + equalTo(forecastDuration3HoursExpiresIn24Hours.getCreateTime().toEpochMilli() + TimeValue.timeValueHours(24).getMillis()) + ); List forecasts = getForecasts(job.getId(), forecastDuration3HoursExpiresIn24Hours); assertThat(forecastDuration3HoursExpiresIn24Hours.getRecordCount(), equalTo(3L)); assertThat(forecasts.size(), equalTo(3)); @@ -162,10 +163,11 @@ public void testDurationCannotBeLessThanBucketSpan() { putJob(job); openJob(job.getId()); - ElasticsearchException e = expectThrows(ElasticsearchException.class,() -> forecast(job.getId(), - TimeValue.timeValueMinutes(10), null)); - assertThat(e.getMessage(), - equalTo("[duration] must be greater or equal to the bucket span: [10m/1h]")); + ElasticsearchException e = expectThrows( + ElasticsearchException.class, + () -> forecast(job.getId(), TimeValue.timeValueMinutes(10), null) + ); + assertThat(e.getMessage(), equalTo("[duration] must be greater or equal to the bucket span: [10m/1h]")); } public void testNoData() { @@ -182,10 +184,14 @@ public void testNoData() { putJob(job); openJob(job.getId()); - ElasticsearchException e = expectThrows(ElasticsearchException.class, - () -> forecast(job.getId(), TimeValue.timeValueMinutes(120), null)); - assertThat(e.getMessage(), - equalTo("Cannot run forecast: Forecast cannot be executed as job requires data to have been processed and modeled")); + ElasticsearchException e = expectThrows( + ElasticsearchException.class, + () -> forecast(job.getId(), TimeValue.timeValueMinutes(120), null) + ); + assertThat( + e.getMessage(), + equalTo("Cannot run forecast: Forecast cannot be executed as job requires data to have been processed and modeled") + ); } public void testMemoryStatus() { @@ -208,8 +214,10 @@ public void testMemoryStatus() { putJob(job); openJob(job.getId()); createDataWithLotsOfClientIps(bucketSpan, job); - ElasticsearchException e = expectThrows(ElasticsearchException.class, - () -> forecast(job.getId(), TimeValue.timeValueMinutes(120), null)); + ElasticsearchException e = expectThrows( + ElasticsearchException.class, + () -> forecast(job.getId(), TimeValue.timeValueMinutes(120), null) + ); assertThat(e.getMessage(), equalTo("Cannot run forecast: Forecast cannot be executed as model memory status is not OK")); } @@ -241,7 +249,10 @@ public void testOverflowToDisk() throws Exception { } catch (ElasticsearchStatusException e) { if (e.getMessage().contains("disk space")) { throw new ElasticsearchStatusException( - "Test likely fails due to insufficient disk space on test machine, please free up space.", e.status(), e); + "Test likely fails due to insufficient disk space on test machine, please free up space.", + e.status(), + e + ); } throw e; } @@ -265,7 +276,10 @@ public void testOverflowToDisk() throws Exception { } catch (ElasticsearchStatusException e) { if (e.getMessage().contains("disk space")) { throw new ElasticsearchStatusException( - "Test likely fails due to insufficient disk space on test machine, please free up space.", e.status(), e); + "Test likely fails due to insufficient disk space on test machine, please free up space.", + e.status(), + e + ); } throw e; } @@ -326,10 +340,13 @@ public void testDeleteWildCard() throws Exception { assertNotNull(getForecastStats(job.getId(), forecastId2Duration1HourNoExpiry2)); { - DeleteForecastAction.Request request = new DeleteForecastAction.Request(job.getId(), - forecastIdDefaultDurationDefaultExpiry.substring(0, forecastIdDefaultDurationDefaultExpiry.length() - 2) + "*" + DeleteForecastAction.Request request = new DeleteForecastAction.Request( + job.getId(), + forecastIdDefaultDurationDefaultExpiry.substring(0, forecastIdDefaultDurationDefaultExpiry.length() - 2) + + "*" + "," - + forecastIdDuration1HourNoExpiry); + + forecastIdDuration1HourNoExpiry + ); AcknowledgedResponse response = client().execute(DeleteForecastAction.INSTANCE, request).actionGet(); assertTrue(response.isAcknowledged()); @@ -447,8 +464,10 @@ public void testDelete() throws Exception { } { - DeleteForecastAction.Request request = new DeleteForecastAction.Request(job.getId(), - forecastIdDefaultDurationDefaultExpiry + "," + forecastIdDuration1HourNoExpiry); + DeleteForecastAction.Request request = new DeleteForecastAction.Request( + job.getId(), + forecastIdDefaultDurationDefaultExpiry + "," + forecastIdDuration1HourNoExpiry + ); AcknowledgedResponse response = client().execute(DeleteForecastAction.INSTANCE, request).actionGet(); assertTrue(response.isAcknowledged()); @@ -460,10 +479,11 @@ public void testDelete() throws Exception { { DeleteForecastAction.Request request = new DeleteForecastAction.Request(job.getId(), "forecast-does-not-exist"); - ElasticsearchException e = expectThrows(ElasticsearchException.class, - () -> client().execute(DeleteForecastAction.INSTANCE, request).actionGet()); - assertThat(e.getMessage(), - equalTo("No forecast(s) [forecast-does-not-exist] exists for job [forecast-it-test-delete]")); + ElasticsearchException e = expectThrows( + ElasticsearchException.class, + () -> client().execute(DeleteForecastAction.INSTANCE, request).actionGet() + ); + assertThat(e.getMessage(), equalTo("No forecast(s) [forecast-does-not-exist] exists for job [forecast-it-test-delete]")); } { @@ -495,10 +515,14 @@ public void testDelete() throws Exception { DeleteForecastAction.Request request = new DeleteForecastAction.Request(otherJob.getId(), Metadata.ALL); request.setAllowNoForecasts(false); - ElasticsearchException e = expectThrows(ElasticsearchException.class, - () -> client().execute(DeleteForecastAction.INSTANCE, request).actionGet()); - assertThat(e.getMessage(), - equalTo("No forecast(s) [_all] exists for job [forecasts-delete-with-all-and-not-allow-no-forecasts]")); + ElasticsearchException e = expectThrows( + ElasticsearchException.class, + () -> client().execute(DeleteForecastAction.INSTANCE, request).actionGet() + ); + assertThat( + e.getMessage(), + equalTo("No forecast(s) [_all] exists for job [forecasts-delete-with-all-and-not-allow-no-forecasts]") + ); } } @@ -535,10 +559,12 @@ public void testForceStopSetsForecastToFailed() throws Exception { closeJob(jobId, true); // On force close job, it should always be at least failed or finished - waitForecastStatus(jobId, + waitForecastStatus( + jobId, forecastId, ForecastRequestStats.ForecastRequestStatus.FAILED, - ForecastRequestStats.ForecastRequestStatus.FINISHED); + ForecastRequestStats.ForecastRequestStatus.FINISHED + ); ForecastRequestStats forecastStats = getForecastStats(job.getId(), forecastId); assertNotNull(forecastStats); if (forecastStats.getStatus().equals(ForecastRequestStats.ForecastRequestStatus.FAILED)) { @@ -575,10 +601,7 @@ public void testForecastWithHigherMemoryUse() throws Exception { // Now we can start doing forecast requests - String forecastId = forecast(job.getId(), - TimeValue.timeValueHours(1), - TimeValue.ZERO, - ByteSizeValue.ofMb(50).getBytes()); + String forecastId = forecast(job.getId(), TimeValue.timeValueHours(1), TimeValue.ZERO, ByteSizeValue.ofMb(50).getBytes()); waitForecastToFinish(job.getId(), forecastId); closeJob(job.getId()); @@ -601,8 +624,14 @@ private void createDataWithLotsOfClientIps(TimeValue bucketSpan, Job.Builder job double value = 10.0 + h; for (int i = 1; i < 101; i++) { for (int j = 1; j < 81; j++) { - String json = String.format(Locale.ROOT, "{\"time\": %d, \"value\": %f, \"clientIP\": \"192.168.%d.%d\"}\n", - timestamp, value, i, j); + String json = String.format( + Locale.ROOT, + "{\"time\": %d, \"value\": %f, \"clientIP\": \"192.168.%d.%d\"}\n", + timestamp, + value, + i, + j + ); data.add(json); } } diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/InferenceIngestIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/InferenceIngestIT.java index dcc3bf0c2f266..d92e0048b4ca5 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/InferenceIngestIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/InferenceIngestIT.java @@ -15,18 +15,18 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.test.ExternalTestCluster; import org.elasticsearch.test.SecuritySettingsSourceField; import org.elasticsearch.test.rest.ESRestTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.ml.inference.MlInferenceNamedXContentProvider; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.inference.InferenceDefinitionTests; import org.elasticsearch.xpack.core.ml.integration.MlRestTestStateCleaner; @@ -54,18 +54,17 @@ */ public class InferenceIngestIT extends ESRestTestCase { - private static final String BASIC_AUTH_VALUE_SUPER_USER = - UsernamePasswordToken.basicAuthHeaderValue("x_pack_rest_user", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING); + private static final String BASIC_AUTH_VALUE_SUPER_USER = UsernamePasswordToken.basicAuthHeaderValue( + "x_pack_rest_user", + SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING + ); @Before public void setup() throws Exception { Request loggingSettings = new Request("PUT", "_cluster/settings"); - loggingSettings.setJsonEntity("" + - "{" + - "\"persistent\" : {\n" + - " \"logger.org.elasticsearch.xpack.ml.inference\" : \"TRACE\"\n" + - " }" + - "}"); + loggingSettings.setJsonEntity( + "" + "{" + "\"persistent\" : {\n" + " \"logger.org.elasticsearch.xpack.ml.inference\" : \"TRACE\"\n" + " }" + "}" + ); client().performRequest(loggingSettings); client().performRequest(new Request("GET", "/_cluster/health?wait_for_status=green&timeout=30s")); } @@ -79,12 +78,9 @@ protected Settings restClientSettings() { public void cleanUpData() throws Exception { new MlRestTestStateCleaner(logger, adminClient()).resetFeatures(); Request loggingSettings = new Request("PUT", "_cluster/settings"); - loggingSettings.setJsonEntity("" + - "{" + - "\"persistent\" : {\n" + - " \"logger.org.elasticsearch.xpack.ml.inference\" : null\n" + - " }" + - "}"); + loggingSettings.setJsonEntity( + "" + "{" + "\"persistent\" : {\n" + " \"logger.org.elasticsearch.xpack.ml.inference\" : null\n" + " }" + "}" + ); client().performRequest(loggingSettings); } @@ -96,8 +92,9 @@ public void testPathologicalPipelineCreationAndDeletion() throws Exception { putModel(regressionModelId, REGRESSION_CONFIG); for (int i = 0; i < 10; i++) { - client().performRequest(putPipeline("simple_classification_pipeline", - pipelineDefinition(classificationModelId, "classification"))); + client().performRequest( + putPipeline("simple_classification_pipeline", pipelineDefinition(classificationModelId, "classification")) + ); client().performRequest(indexRequest("index_for_inference_test", "simple_classification_pipeline", generateSourceDoc())); client().performRequest(new Request("DELETE", "_ingest/pipeline/simple_classification_pipeline")); @@ -107,16 +104,20 @@ public void testPathologicalPipelineCreationAndDeletion() throws Exception { } client().performRequest(new Request("POST", "index_for_inference_test/_refresh")); - Response searchResponse = client().performRequest(searchRequest("index_for_inference_test", - QueryBuilders.boolQuery() - .filter( - QueryBuilders.existsQuery("ml.inference.regression.predicted_value")))); + Response searchResponse = client().performRequest( + searchRequest( + "index_for_inference_test", + QueryBuilders.boolQuery().filter(QueryBuilders.existsQuery("ml.inference.regression.predicted_value")) + ) + ); assertThat(EntityUtils.toString(searchResponse.getEntity()), containsString("\"value\":10")); - searchResponse = client().performRequest(searchRequest("index_for_inference_test", - QueryBuilders.boolQuery() - .filter( - QueryBuilders.existsQuery("ml.inference.classification.predicted_value")))); + searchResponse = client().performRequest( + searchRequest( + "index_for_inference_test", + QueryBuilders.boolQuery().filter(QueryBuilders.existsQuery("ml.inference.classification.predicted_value")) + ) + ); assertThat(EntityUtils.toString(searchResponse.getEntity()), containsString("\"value\":10")); assertBusy(() -> { @@ -124,7 +125,7 @@ public void testPathologicalPipelineCreationAndDeletion() throws Exception { assertStatsWithCacheMisses(classificationModelId, 10L); assertStatsWithCacheMisses(regressionModelId, 10L); } catch (ResponseException ex) { - //this could just mean shard failures. + // this could just mean shard failures. fail(ex.getMessage()); } }, 30, TimeUnit.SECONDS); @@ -137,8 +138,7 @@ public void testPipelineIngest() throws Exception { String regressionModelId = "test_regression"; putModel(regressionModelId, REGRESSION_CONFIG); - client().performRequest(putPipeline("simple_classification_pipeline", - pipelineDefinition(classificationModelId, "classification"))); + client().performRequest(putPipeline("simple_classification_pipeline", pipelineDefinition(classificationModelId, "classification"))); client().performRequest(putPipeline("simple_regression_pipeline", pipelineDefinition(regressionModelId, "regression"))); for (int i = 0; i < 10; i++) { @@ -155,16 +155,20 @@ public void testPipelineIngest() throws Exception { client().performRequest(new Request("POST", "index_for_inference_test/_refresh")); - Response searchResponse = client().performRequest(searchRequest("index_for_inference_test", - QueryBuilders.boolQuery() - .filter( - QueryBuilders.existsQuery("ml.inference.regression.predicted_value")))); + Response searchResponse = client().performRequest( + searchRequest( + "index_for_inference_test", + QueryBuilders.boolQuery().filter(QueryBuilders.existsQuery("ml.inference.regression.predicted_value")) + ) + ); assertThat(EntityUtils.toString(searchResponse.getEntity()), containsString("\"value\":15")); - searchResponse = client().performRequest(searchRequest("index_for_inference_test", - QueryBuilders.boolQuery() - .filter( - QueryBuilders.existsQuery("ml.inference.classification.predicted_value")))); + searchResponse = client().performRequest( + searchRequest( + "index_for_inference_test", + QueryBuilders.boolQuery().filter(QueryBuilders.existsQuery("ml.inference.classification.predicted_value")) + ) + ); assertThat(EntityUtils.toString(searchResponse.getEntity()), containsString("\"value\":10")); @@ -173,7 +177,7 @@ public void testPipelineIngest() throws Exception { assertStatsWithCacheMisses(classificationModelId, 10L); assertStatsWithCacheMisses(regressionModelId, 15L); } catch (ResponseException ex) { - //this could just mean shard failures. + // this could just mean shard failures. fail(ex.getMessage()); } }, 30, TimeUnit.SECONDS); @@ -195,15 +199,15 @@ public void testPipelineIngestWithModelAliases() throws Exception { putModelAlias(modelAlias, regressionModelId2); // Need to assert busy as loading the model and then switching the model alias can take time assertBusy(() -> { - String source = "{\n" + - " \"docs\": [\n" + - " {\"_source\": {\n" + - " \"col1\": \"female\",\n" + - " \"col2\": \"M\",\n" + - " \"col3\": \"none\",\n" + - " \"col4\": 10\n" + - " }}]\n" + - "}"; + String source = "{\n" + + " \"docs\": [\n" + + " {\"_source\": {\n" + + " \"col1\": \"female\",\n" + + " \"col2\": \"M\",\n" + + " \"col3\": \"none\",\n" + + " \"col4\": 10\n" + + " }}]\n" + + "}"; Request request = new Request("POST", "_ingest/pipeline/simple_regression_pipeline/_simulate"); request.setJsonEntity(source); Response response = client().performRequest(request); @@ -219,47 +223,55 @@ public void testPipelineIngestWithModelAliases() throws Exception { client().performRequest(new Request("POST", "index_for_inference_test/_refresh")); - Response searchResponse = client().performRequest(searchRequest("index_for_inference_test", - QueryBuilders.boolQuery() - .filter( - QueryBuilders.existsQuery("ml.inference.regression.predicted_value")))); + Response searchResponse = client().performRequest( + searchRequest( + "index_for_inference_test", + QueryBuilders.boolQuery().filter(QueryBuilders.existsQuery("ml.inference.regression.predicted_value")) + ) + ); // Verify we have 20 documents that contain a predicted value for regression assertThat(EntityUtils.toString(searchResponse.getEntity()), containsString("\"value\":20")); - // Since this is a multi-node cluster, the model could be loaded and cached on one ingest node but not the other // Consequently, we should only verify that some of the documents refer to the first regression model // and some refer to the second. - searchResponse = client().performRequest(searchRequest("index_for_inference_test", - QueryBuilders.boolQuery() - .filter( - QueryBuilders.termQuery("ml.inference.regression.model_id.keyword", regressionModelId)))); + searchResponse = client().performRequest( + searchRequest( + "index_for_inference_test", + QueryBuilders.boolQuery().filter(QueryBuilders.termQuery("ml.inference.regression.model_id.keyword", regressionModelId)) + ) + ); assertThat(EntityUtils.toString(searchResponse.getEntity()), not(containsString("\"value\":0"))); - searchResponse = client().performRequest(searchRequest("index_for_inference_test", - QueryBuilders.boolQuery() - .filter( - QueryBuilders.termQuery("ml.inference.regression.model_id.keyword", regressionModelId2)))); + searchResponse = client().performRequest( + searchRequest( + "index_for_inference_test", + QueryBuilders.boolQuery().filter(QueryBuilders.termQuery("ml.inference.regression.model_id.keyword", regressionModelId2)) + ) + ); assertThat(EntityUtils.toString(searchResponse.getEntity()), not(containsString("\"value\":0"))); assertBusy(() -> { - try (XContentParser parser = createParser(JsonXContent.jsonXContent, client().performRequest(new Request("GET", - "_ml/trained_models/" + modelAlias + "/_stats")).getEntity().getContent())) { + try ( + XContentParser parser = createParser( + JsonXContent.jsonXContent, + client().performRequest(new Request("GET", "_ml/trained_models/" + modelAlias + "/_stats")).getEntity().getContent() + ) + ) { GetTrainedModelsStatsResponse response = GetTrainedModelsStatsResponse.fromXContent(parser); assertThat(response.toString(), response.getTrainedModelStats(), hasSize(1)); TrainedModelStats trainedModelStats = response.getTrainedModelStats().get(0); assertThat(trainedModelStats.getModelId(), equalTo(regressionModelId2)); assertThat(trainedModelStats.getInferenceStats(), is(notNullValue())); } catch (ResponseException ex) { - //this could just mean shard failures. + // this could just mean shard failures. fail(ex.getMessage()); } }); } public void assertStatsWithCacheMisses(String modelId, long inferenceCount) throws IOException { - Response statsResponse = client().performRequest(new Request("GET", - "_ml/trained_models/" + modelId + "/_stats")); + Response statsResponse = client().performRequest(new Request("GET", "_ml/trained_models/" + modelId + "/_stats")); try (XContentParser parser = createParser(JsonXContent.jsonXContent, statsResponse.getEntity().getContent())) { GetTrainedModelsStatsResponse response = GetTrainedModelsStatsResponse.fromXContent(parser); assertThat(response.getTrainedModelStats(), hasSize(1)); @@ -277,49 +289,53 @@ public void testSimulate() throws IOException { String regressionModelId = "test_regression_simulate"; putModel(regressionModelId, REGRESSION_CONFIG); - String source = "{\n" + - " \"pipeline\": {\n" + - " \"processors\": [\n" + - " {\n" + - " \"inference\": {\n" + - " \"target_field\": \"ml.classification\",\n" + - " \"inference_config\": {\"classification\": " + - " {\"num_top_classes\":0, " + - " \"top_classes_results_field\": \"result_class_prob\"," + - " \"num_top_feature_importance_values\": 2" + - " }},\n" + - " \"model_id\": \"" + classificationModelId + "\",\n" + - " \"field_map\": {\n" + - " \"col1\": \"col1\",\n" + - " \"col2\": \"col2\",\n" + - " \"col3\": \"col3\",\n" + - " \"col4\": \"col4\"\n" + - " }\n" + - " }\n" + - " },\n" + - " {\n" + - " \"inference\": {\n" + - " \"target_field\": \"ml.regression\",\n" + - " \"model_id\": \"" + regressionModelId + "\",\n" + - " \"inference_config\": {\"regression\":{}},\n" + - " \"field_map\": {\n" + - " \"col1\": \"col1\",\n" + - " \"col2\": \"col2\",\n" + - " \"col3\": \"col3\",\n" + - " \"col4\": \"col4\"\n" + - " }\n" + - " }\n" + - " }\n" + - " ]\n" + - " },\n" + - " \"docs\": [\n" + - " {\"_source\": {\n" + - " \"col1\": \"female\",\n" + - " \"col2\": \"M\",\n" + - " \"col3\": \"none\",\n" + - " \"col4\": 10\n" + - " }}]\n" + - "}"; + String source = "{\n" + + " \"pipeline\": {\n" + + " \"processors\": [\n" + + " {\n" + + " \"inference\": {\n" + + " \"target_field\": \"ml.classification\",\n" + + " \"inference_config\": {\"classification\": " + + " {\"num_top_classes\":0, " + + " \"top_classes_results_field\": \"result_class_prob\"," + + " \"num_top_feature_importance_values\": 2" + + " }},\n" + + " \"model_id\": \"" + + classificationModelId + + "\",\n" + + " \"field_map\": {\n" + + " \"col1\": \"col1\",\n" + + " \"col2\": \"col2\",\n" + + " \"col3\": \"col3\",\n" + + " \"col4\": \"col4\"\n" + + " }\n" + + " }\n" + + " },\n" + + " {\n" + + " \"inference\": {\n" + + " \"target_field\": \"ml.regression\",\n" + + " \"model_id\": \"" + + regressionModelId + + "\",\n" + + " \"inference_config\": {\"regression\":{}},\n" + + " \"field_map\": {\n" + + " \"col1\": \"col1\",\n" + + " \"col2\": \"col2\",\n" + + " \"col3\": \"col3\",\n" + + " \"col4\": \"col4\"\n" + + " }\n" + + " }\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"docs\": [\n" + + " {\"_source\": {\n" + + " \"col1\": \"female\",\n" + + " \"col2\": \"M\",\n" + + " \"col3\": \"none\",\n" + + " \"col4\": 10\n" + + " }}]\n" + + "}"; Response response = client().performRequest(simulateRequest(source)); String responseString = EntityUtils.toString(response.getEntity()); @@ -332,31 +348,31 @@ public void testSimulate() throws IOException { assertThat(responseString, containsString("\"importance\":0.944")); assertThat(responseString, containsString("\"importance\":0.19999")); - String sourceWithMissingModel = "{\n" + - " \"pipeline\": {\n" + - " \"processors\": [\n" + - " {\n" + - " \"inference\": {\n" + - " \"model_id\": \"test_classification_missing\",\n" + - " \"inference_config\": {\"classification\":{}},\n" + - " \"field_map\": {\n" + - " \"col1\": \"col1\",\n" + - " \"col2\": \"col2\",\n" + - " \"col3\": \"col3\",\n" + - " \"col4\": \"col4\"\n" + - " }\n" + - " }\n" + - " }\n" + - " ]\n" + - " },\n" + - " \"docs\": [\n" + - " {\"_source\": {\n" + - " \"col1\": \"female\",\n" + - " \"col2\": \"M\",\n" + - " \"col3\": \"none\",\n" + - " \"col4\": 10\n" + - " }}]\n" + - "}"; + String sourceWithMissingModel = "{\n" + + " \"pipeline\": {\n" + + " \"processors\": [\n" + + " {\n" + + " \"inference\": {\n" + + " \"model_id\": \"test_classification_missing\",\n" + + " \"inference_config\": {\"classification\":{}},\n" + + " \"field_map\": {\n" + + " \"col1\": \"col1\",\n" + + " \"col2\": \"col2\",\n" + + " \"col3\": \"col3\",\n" + + " \"col4\": \"col4\"\n" + + " }\n" + + " }\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"docs\": [\n" + + " {\"_source\": {\n" + + " \"col1\": \"female\",\n" + + " \"col2\": \"M\",\n" + + " \"col3\": \"none\",\n" + + " \"col4\": 10\n" + + " }}]\n" + + "}"; response = client().performRequest(simulateRequest(sourceWithMissingModel)); responseString = EntityUtils.toString(response.getEntity()); @@ -367,31 +383,33 @@ public void testSimulate() throws IOException { public void testSimulateWithDefaultMappedField() throws IOException { String classificationModelId = "test_classification_default_mapped_field"; putModel(classificationModelId, CLASSIFICATION_CONFIG); - String source = "{\n" + - " \"pipeline\": {\n" + - " \"processors\": [\n" + - " {\n" + - " \"inference\": {\n" + - " \"target_field\": \"ml.classification\",\n" + - " \"inference_config\": {\"classification\": " + - " {\"num_top_classes\":2, " + - " \"top_classes_results_field\": \"result_class_prob\"," + - " \"num_top_feature_importance_values\": 2" + - " }},\n" + - " \"model_id\": \"" + classificationModelId + "\",\n" + - " \"field_map\": {}\n" + - " }\n" + - " }\n"+ - " ]\n" + - " },\n" + - " \"docs\": [\n" + - " {\"_source\": {\n" + - " \"col_1_alias\": \"female\",\n" + - " \"col2\": \"M\",\n" + - " \"col3\": \"none\",\n" + - " \"col4\": 10\n" + - " }}]\n" + - "}"; + String source = "{\n" + + " \"pipeline\": {\n" + + " \"processors\": [\n" + + " {\n" + + " \"inference\": {\n" + + " \"target_field\": \"ml.classification\",\n" + + " \"inference_config\": {\"classification\": " + + " {\"num_top_classes\":2, " + + " \"top_classes_results_field\": \"result_class_prob\"," + + " \"num_top_feature_importance_values\": 2" + + " }},\n" + + " \"model_id\": \"" + + classificationModelId + + "\",\n" + + " \"field_map\": {}\n" + + " }\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"docs\": [\n" + + " {\"_source\": {\n" + + " \"col_1_alias\": \"female\",\n" + + " \"col2\": \"M\",\n" + + " \"col3\": \"none\",\n" + + " \"col4\": 10\n" + + " }}]\n" + + "}"; Response response = client().performRequest(simulateRequest(source)); String responseString = EntityUtils.toString(response.getEntity()); @@ -403,68 +421,68 @@ public void testSimulateWithDefaultMappedField() throws IOException { } public void testSimulateLangIdent() throws IOException { - String source = "{\n" + - " \"pipeline\": {\n" + - " \"processors\": [\n" + - " {\n" + - " \"inference\": {\n" + - " \"inference_config\": {\"classification\":{}},\n" + - " \"model_id\": \"lang_ident_model_1\",\n" + - " \"field_map\": {}\n" + - " }\n" + - " }\n" + - " ]\n" + - " },\n" + - " \"docs\": [\n" + - " {\"_source\": {\n" + - " \"text\": \"this is some plain text.\"\n" + - " }}]\n" + - "}"; + String source = "{\n" + + " \"pipeline\": {\n" + + " \"processors\": [\n" + + " {\n" + + " \"inference\": {\n" + + " \"inference_config\": {\"classification\":{}},\n" + + " \"model_id\": \"lang_ident_model_1\",\n" + + " \"field_map\": {}\n" + + " }\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"docs\": [\n" + + " {\"_source\": {\n" + + " \"text\": \"this is some plain text.\"\n" + + " }}]\n" + + "}"; Response response = client().performRequest(simulateRequest(source)); assertThat(EntityUtils.toString(response.getEntity()), containsString("\"predicted_value\":\"en\"")); } public void testSimulateLangIdentForeach() throws IOException { - String source = "{" + - " \"pipeline\": {\n" + - " \"description\": \"detect text lang\",\n" + - " \"processors\": [\n" + - " {\n" + - " \"foreach\": {\n" + - " \"field\": \"greetings\",\n" + - " \"processor\": {\n" + - " \"inference\": {\n" + - " \"model_id\": \"lang_ident_model_1\",\n" + - " \"inference_config\": {\n" + - " \"classification\": {\n" + - " \"num_top_classes\": 5\n" + - " }\n" + - " },\n" + - " \"field_map\": {\n" + - " \"_ingest._value.text\": \"text\"\n" + - " }\n" + - " }\n" + - " }\n" + - " }\n" + - " }\n" + - " ]\n" + - " },\n" + - " \"docs\": [\n" + - " {\n" + - " \"_source\": {\n" + - " \"greetings\": [\n" + - " {\n" + - " \"text\": \" a backup credit card by visiting your billing preferences page or visit the adwords help\"\n" + - " },\n" + - " {\n" + - " \"text\": \" 개별적으로 리포트 액세스 권한을 부여할 수 있습니다 액세스 권한 부여사용자에게 프로필 리포트에 \"\n" + - " }\n" + - " ]\n" + - " }\n" + - " }\n" + - " ]\n" + - "}"; + String source = "{" + + " \"pipeline\": {\n" + + " \"description\": \"detect text lang\",\n" + + " \"processors\": [\n" + + " {\n" + + " \"foreach\": {\n" + + " \"field\": \"greetings\",\n" + + " \"processor\": {\n" + + " \"inference\": {\n" + + " \"model_id\": \"lang_ident_model_1\",\n" + + " \"inference_config\": {\n" + + " \"classification\": {\n" + + " \"num_top_classes\": 5\n" + + " }\n" + + " },\n" + + " \"field_map\": {\n" + + " \"_ingest._value.text\": \"text\"\n" + + " }\n" + + " }\n" + + " }\n" + + " }\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"docs\": [\n" + + " {\n" + + " \"_source\": {\n" + + " \"greetings\": [\n" + + " {\n" + + " \"text\": \" a backup credit card by visiting your billing preferences page or visit the adwords help\"\n" + + " },\n" + + " {\n" + + " \"text\": \" 개별적으로 리포트 액세스 권한을 부여할 수 있습니다 액세스 권한 부여사용자에게 프로필 리포트에 \"\n" + + " }\n" + + " ]\n" + + " }\n" + + " }\n" + + " ]\n" + + "}"; Response response = client().performRequest(simulateRequest(source)); String stringResponse = EntityUtils.toString(response.getEntity()); assertThat(stringResponse, containsString("\"predicted_value\":\"en\"")); @@ -478,10 +496,12 @@ private static Request simulateRequest(String jsonEntity) { } private static Request indexRequest(String index, String pipeline, Map doc) throws IOException { - try(XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().map(doc)) { - return indexRequest(index, + try (XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().map(doc)) { + return indexRequest( + index, pipeline, - XContentHelper.convertToJson(BytesReference.bytes(xContentBuilder), false, XContentType.JSON)); + XContentHelper.convertToJson(BytesReference.bytes(xContentBuilder), false, XContentType.JSON) + ); } } @@ -507,176 +527,186 @@ private static Request searchRequest(String index, QueryBuilder queryBuilder) th } private Map generateSourceDoc() { - return new HashMap<>(){{ - put("col1", randomFrom("female", "male")); - put("col2", randomFrom("S", "M", "L", "XL")); - put("col3", randomFrom("true", "false", "none", "other")); - put("col4", randomIntBetween(0, 10)); - }}; + return new HashMap<>() { + { + put("col1", randomFrom("female", "male")); + put("col2", randomFrom("S", "M", "L", "XL")); + put("col3", randomFrom("true", "false", "none", "other")); + put("col4", randomIntBetween(0, 10)); + } + }; } - private static final String REGRESSION_DEFINITION = "{" + - " \"preprocessors\": [\n" + - " {\n" + - " \"one_hot_encoding\": {\n" + - " \"field\": \"col1\",\n" + - " \"hot_map\": {\n" + - " \"male\": \"col1_male\",\n" + - " \"female\": \"col1_female\"\n" + - " }\n" + - " }\n" + - " },\n" + - " {\n" + - " \"target_mean_encoding\": {\n" + - " \"field\": \"col2\",\n" + - " \"feature_name\": \"col2_encoded\",\n" + - " \"target_map\": {\n" + - " \"S\": 5.0,\n" + - " \"M\": 10.0,\n" + - " \"L\": 20\n" + - " },\n" + - " \"default_value\": 5.0\n" + - " }\n" + - " },\n" + - " {\n" + - " \"frequency_encoding\": {\n" + - " \"field\": \"col3\",\n" + - " \"feature_name\": \"col3_encoded\",\n" + - " \"frequency_map\": {\n" + - " \"none\": 0.75,\n" + - " \"true\": 0.10,\n" + - " \"false\": 0.15\n" + - " }\n" + - " }\n" + - " }\n" + - " ],\n" + - " \"trained_model\": {\n" + - " \"ensemble\": {\n" + - " \"feature_names\": [\n" + - " \"col1_male\",\n" + - " \"col1_female\",\n" + - " \"col2_encoded\",\n" + - " \"col3_encoded\",\n" + - " \"col4\"\n" + - " ],\n" + - " \"aggregate_output\": {\n" + - " \"weighted_sum\": {\n" + - " \"weights\": [\n" + - " 0.5,\n" + - " 0.5\n" + - " ]\n" + - " }\n" + - " },\n" + - " \"target_type\": \"regression\",\n" + - " \"trained_models\": [\n" + - " {\n" + - " \"tree\": {\n" + - " \"feature_names\": [\n" + - " \"col1_male\",\n" + - " \"col1_female\",\n" + - " \"col4\"\n" + - " ],\n" + - " \"tree_structure\": [\n" + - " {\n" + - " \"node_index\": 0,\n" + - " \"split_feature\": 0,\n" + - " \"split_gain\": 12.0,\n" + - " \"threshold\": 10.0,\n" + - " \"decision_type\": \"lte\",\n" + - " \"number_samples\": 300,\n" + - " \"default_left\": true,\n" + - " \"left_child\": 1,\n" + - " \"right_child\": 2\n" + - " },\n" + - " {\n" + - " \"node_index\": 1,\n" + - " \"number_samples\": 100,\n" + - " \"leaf_value\": 1\n" + - " },\n" + - " {\n" + - " \"node_index\": 2,\n" + - " \"number_samples\": 200,\n" + - " \"leaf_value\": 2\n" + - " }\n" + - " ],\n" + - " \"target_type\": \"regression\"\n" + - " }\n" + - " },\n" + - " {\n" + - " \"tree\": {\n" + - " \"feature_names\": [\n" + - " \"col2_encoded\",\n" + - " \"col3_encoded\",\n" + - " \"col4\"\n" + - " ],\n" + - " \"tree_structure\": [\n" + - " {\n" + - " \"node_index\": 0,\n" + - " \"split_feature\": 0,\n" + - " \"split_gain\": 12.0,\n" + - " \"threshold\": 10.0,\n" + - " \"decision_type\": \"lte\",\n" + - " \"default_left\": true,\n" + - " \"number_samples\": 150,\n" + - " \"left_child\": 1,\n" + - " \"right_child\": 2\n" + - " },\n" + - " {\n" + - " \"node_index\": 1,\n" + - " \"number_samples\": 50,\n" + - " \"leaf_value\": 1\n" + - " },\n" + - " {\n" + - " \"node_index\": 2,\n" + - " \"number_samples\": 100,\n" + - " \"leaf_value\": 2\n" + - " }\n" + - " ],\n" + - " \"target_type\": \"regression\"\n" + - " }\n" + - " }\n" + - " ]\n" + - " }\n" + - " }\n" + - "}"; - - private static final String REGRESSION_CONFIG = "{" + - " \"input\":{\"field_names\":[\"col1\",\"col2\",\"col3\",\"col4\"]}," + - " \"description\": \"test model for regression\",\n" + - " \"inference_config\": {\"regression\": {}},\n" + - " \"definition\": " + REGRESSION_DEFINITION + - "}"; + private static final String REGRESSION_DEFINITION = "{" + + " \"preprocessors\": [\n" + + " {\n" + + " \"one_hot_encoding\": {\n" + + " \"field\": \"col1\",\n" + + " \"hot_map\": {\n" + + " \"male\": \"col1_male\",\n" + + " \"female\": \"col1_female\"\n" + + " }\n" + + " }\n" + + " },\n" + + " {\n" + + " \"target_mean_encoding\": {\n" + + " \"field\": \"col2\",\n" + + " \"feature_name\": \"col2_encoded\",\n" + + " \"target_map\": {\n" + + " \"S\": 5.0,\n" + + " \"M\": 10.0,\n" + + " \"L\": 20\n" + + " },\n" + + " \"default_value\": 5.0\n" + + " }\n" + + " },\n" + + " {\n" + + " \"frequency_encoding\": {\n" + + " \"field\": \"col3\",\n" + + " \"feature_name\": \"col3_encoded\",\n" + + " \"frequency_map\": {\n" + + " \"none\": 0.75,\n" + + " \"true\": 0.10,\n" + + " \"false\": 0.15\n" + + " }\n" + + " }\n" + + " }\n" + + " ],\n" + + " \"trained_model\": {\n" + + " \"ensemble\": {\n" + + " \"feature_names\": [\n" + + " \"col1_male\",\n" + + " \"col1_female\",\n" + + " \"col2_encoded\",\n" + + " \"col3_encoded\",\n" + + " \"col4\"\n" + + " ],\n" + + " \"aggregate_output\": {\n" + + " \"weighted_sum\": {\n" + + " \"weights\": [\n" + + " 0.5,\n" + + " 0.5\n" + + " ]\n" + + " }\n" + + " },\n" + + " \"target_type\": \"regression\",\n" + + " \"trained_models\": [\n" + + " {\n" + + " \"tree\": {\n" + + " \"feature_names\": [\n" + + " \"col1_male\",\n" + + " \"col1_female\",\n" + + " \"col4\"\n" + + " ],\n" + + " \"tree_structure\": [\n" + + " {\n" + + " \"node_index\": 0,\n" + + " \"split_feature\": 0,\n" + + " \"split_gain\": 12.0,\n" + + " \"threshold\": 10.0,\n" + + " \"decision_type\": \"lte\",\n" + + " \"number_samples\": 300,\n" + + " \"default_left\": true,\n" + + " \"left_child\": 1,\n" + + " \"right_child\": 2\n" + + " },\n" + + " {\n" + + " \"node_index\": 1,\n" + + " \"number_samples\": 100,\n" + + " \"leaf_value\": 1\n" + + " },\n" + + " {\n" + + " \"node_index\": 2,\n" + + " \"number_samples\": 200,\n" + + " \"leaf_value\": 2\n" + + " }\n" + + " ],\n" + + " \"target_type\": \"regression\"\n" + + " }\n" + + " },\n" + + " {\n" + + " \"tree\": {\n" + + " \"feature_names\": [\n" + + " \"col2_encoded\",\n" + + " \"col3_encoded\",\n" + + " \"col4\"\n" + + " ],\n" + + " \"tree_structure\": [\n" + + " {\n" + + " \"node_index\": 0,\n" + + " \"split_feature\": 0,\n" + + " \"split_gain\": 12.0,\n" + + " \"threshold\": 10.0,\n" + + " \"decision_type\": \"lte\",\n" + + " \"default_left\": true,\n" + + " \"number_samples\": 150,\n" + + " \"left_child\": 1,\n" + + " \"right_child\": 2\n" + + " },\n" + + " {\n" + + " \"node_index\": 1,\n" + + " \"number_samples\": 50,\n" + + " \"leaf_value\": 1\n" + + " },\n" + + " {\n" + + " \"node_index\": 2,\n" + + " \"number_samples\": 100,\n" + + " \"leaf_value\": 2\n" + + " }\n" + + " ],\n" + + " \"target_type\": \"regression\"\n" + + " }\n" + + " }\n" + + " ]\n" + + " }\n" + + " }\n" + + "}"; + + private static final String REGRESSION_CONFIG = "{" + + " \"input\":{\"field_names\":[\"col1\",\"col2\",\"col3\",\"col4\"]}," + + " \"description\": \"test model for regression\",\n" + + " \"inference_config\": {\"regression\": {}},\n" + + " \"definition\": " + + REGRESSION_DEFINITION + + "}"; @Override protected NamedXContentRegistry xContentRegistry() { return new NamedXContentRegistry(new MlInferenceNamedXContentProvider().getNamedXContentParsers()); } - private static final String CLASSIFICATION_CONFIG = "" + - "{\n" + - " \"input\":{\"field_names\":[\"col1\",\"col2\",\"col3\",\"col4\"]}," + - " \"description\": \"test model for classification\",\n" + - " \"default_field_map\": {\"col_1_alias\": \"col1\"},\n" + - " \"inference_config\": {\"classification\": {}},\n" + - " \"definition\": " + InferenceDefinitionTests.getClassificationDefinition(false) + - "}"; + private static final String CLASSIFICATION_CONFIG = "" + + "{\n" + + " \"input\":{\"field_names\":[\"col1\",\"col2\",\"col3\",\"col4\"]}," + + " \"description\": \"test model for classification\",\n" + + " \"default_field_map\": {\"col_1_alias\": \"col1\"},\n" + + " \"inference_config\": {\"classification\": {}},\n" + + " \"definition\": " + + InferenceDefinitionTests.getClassificationDefinition(false) + + "}"; private static String pipelineDefinition(String modelId, String inferenceConfig) { - return "{" + - " \"processors\": [\n" + - " {\n" + - " \"inference\": {\n" + - " \"model_id\": \"" + modelId + "\",\n" + - " \"tag\": \""+ inferenceConfig + "\",\n" + - " \"inference_config\": {\"" + inferenceConfig + "\": {}},\n" + - " \"field_map\": {\n" + - " \"col1\": \"col1\",\n" + - " \"col2\": \"col2\",\n" + - " \"col3\": \"col3\",\n" + - " \"col4\": \"col4\"\n" + - " }\n" + - " }\n" + - " }]}\n"; + return "{" + + " \"processors\": [\n" + + " {\n" + + " \"inference\": {\n" + + " \"model_id\": \"" + + modelId + + "\",\n" + + " \"tag\": \"" + + inferenceConfig + + "\",\n" + + " \"inference_config\": {\"" + + inferenceConfig + + "\": {}},\n" + + " \"field_map\": {\n" + + " \"col1\": \"col1\",\n" + + " \"col2\": \"col2\",\n" + + " \"col3\": \"col3\",\n" + + " \"col4\": \"col4\"\n" + + " }\n" + + " }\n" + + " }]}\n"; } private void putModel(String modelId, String modelConfiguration) throws IOException { diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/InterimResultsDeletedAfterReopeningJobIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/InterimResultsDeletedAfterReopeningJobIT.java index e3f4c818534e7..4cc4811c5bfe0 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/InterimResultsDeletedAfterReopeningJobIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/InterimResultsDeletedAfterReopeningJobIT.java @@ -42,8 +42,7 @@ public void test() throws Exception { Detector.Builder detector = new Detector.Builder("mean", "value"); detector.setByFieldName("by_field"); - AnalysisConfig.Builder analysisConfig = new AnalysisConfig.Builder( - Arrays.asList(detector.build())); + AnalysisConfig.Builder analysisConfig = new AnalysisConfig.Builder(Arrays.asList(detector.build())); analysisConfig.setBucketSpan(TimeValue.timeValueHours(1)); DataDescription.Builder dataDescription = new DataDescription.Builder(); Job.Builder job = new Job.Builder("interim-results-deleted-after-reopening-job-test"); @@ -109,8 +108,7 @@ private static Map createRecord(long timestamp, String byFieldVa private void assertNoInterimResults(String jobId) { String indexName = AnomalyDetectorsIndex.jobResultsAliasedName(jobId); - SearchResponse search = client().prepareSearch(indexName).setSize(1000) - .setQuery(QueryBuilders.termQuery("is_interim", true)).get(); + SearchResponse search = client().prepareSearch(indexName).setSize(1000).setQuery(QueryBuilders.termQuery("is_interim", true)).get(); assertThat(search.getHits().getTotalHits().value, equalTo(0L)); } } diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/InterimResultsIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/InterimResultsIT.java index 8cacbcfcfdc0e..fd394354fbb1f 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/InterimResultsIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/InterimResultsIT.java @@ -8,9 +8,9 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.xpack.core.action.util.PageParams; import org.elasticsearch.xpack.core.ml.action.FlushJobAction; import org.elasticsearch.xpack.core.ml.action.GetBucketsAction; -import org.elasticsearch.xpack.core.action.util.PageParams; import org.elasticsearch.xpack.core.ml.job.config.AnalysisConfig; import org.elasticsearch.xpack.core.ml.job.config.DataDescription; import org.elasticsearch.xpack.core.ml.job.config.Detector; @@ -42,7 +42,8 @@ public void cleanUpTest() { public void testInterimResultsUpdates() throws Exception { String jobId = "test-interim-results-updates"; AnalysisConfig.Builder analysisConfig = new AnalysisConfig.Builder( - Collections.singletonList(new Detector.Builder("max", "value").build())); + Collections.singletonList(new Detector.Builder("max", "value").build()) + ); analysisConfig.setBucketSpan(TimeValue.timeValueSeconds(BUCKET_SPAN_SECONDS)); DataDescription.Builder dataDescription = new DataDescription.Builder(); dataDescription.setTimeFormat("epoch"); @@ -68,8 +69,8 @@ public void testInterimResultsUpdates() throws Exception { // push some data up to a 1/4 bucket boundary, flush (with interim), check interim results String data = "{\"time\":1400040000,\"value\":14}\n" - + "{\"time\":1400040500,\"value\":12}\n" - + "{\"time\":1400040510,\"value\":16}\n"; + + "{\"time\":1400040500,\"value\":12}\n" + + "{\"time\":1400040510,\"value\":16}\n"; assertThat(postData(job.getId(), data).getProcessedRecordCount(), equalTo(3L)); flushJob(job.getId(), true); @@ -109,7 +110,8 @@ public void testInterimResultsUpdates() throws Exception { public void testNoInterimResultsAfterAdvancingBucket() throws Exception { String jobId = "test-no-inerim-results-after-advancing-bucket"; AnalysisConfig.Builder analysisConfig = new AnalysisConfig.Builder( - Collections.singletonList(new Detector.Builder("count", null).build())); + Collections.singletonList(new Detector.Builder("count", null).build()) + ); analysisConfig.setBucketSpan(TimeValue.timeValueSeconds(BUCKET_SPAN_SECONDS)); DataDescription.Builder dataDescription = new DataDescription.Builder(); dataDescription.setTimeFormat("epoch"); diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/JobAndDatafeedResilienceIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/JobAndDatafeedResilienceIT.java index 7a2441e168909..fd3360dae83f7 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/JobAndDatafeedResilienceIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/JobAndDatafeedResilienceIT.java @@ -54,26 +54,24 @@ public void testCloseOpenJobWithMissingConfig() throws Exception { assertThat(ex.getMessage(), equalTo("No known job with id 'job-with-missing-config'")); forceCloseJob(jobId); - assertBusy(() -> - assertThat(client().admin() - .cluster() - .prepareListTasks() - .setActions(MlTasks.JOB_TASK_NAME + "[c]") - .get() - .getTasks() - .size(), equalTo(0)) + assertBusy( + () -> assertThat( + client().admin().cluster().prepareListTasks().setActions(MlTasks.JOB_TASK_NAME + "[c]").get().getTasks().size(), + equalTo(0) + ) ); } public void testStopStartedDatafeedWithMissingConfig() throws Exception { - client().admin().indices().prepareCreate(index) - .setMapping("time", "type=date", "value", "type=long") - .get(); + client().admin().indices().prepareCreate(index).setMapping("time", "type=date", "value", "type=long").get(); final String jobId = "job-with-missing-datafeed-with-config"; Job.Builder job = createJob(jobId, TimeValue.timeValueMinutes(5), "count", null); - DatafeedConfig.Builder datafeedConfigBuilder = - createDatafeedBuilder(job.getId() + "-datafeed", job.getId(), Collections.singletonList(index)); + DatafeedConfig.Builder datafeedConfigBuilder = createDatafeedBuilder( + job.getId() + "-datafeed", + job.getId(), + Collections.singletonList(index) + ); DatafeedConfig datafeedConfig = datafeedConfigBuilder.build(); putJob(job); @@ -93,14 +91,11 @@ public void testStopStartedDatafeedWithMissingConfig() throws Exception { assertThat(ex.getMessage(), equalTo("No datafeed with id [job-with-missing-datafeed-with-config-datafeed] exists")); forceStopDatafeed(datafeedConfig.getId()); - assertBusy(() -> - assertThat(client().admin() - .cluster() - .prepareListTasks() - .setActions(MlTasks.DATAFEED_TASK_NAME + "[c]") - .get() - .getTasks() - .size(), equalTo(0)) + assertBusy( + () -> assertThat( + client().admin().cluster().prepareListTasks().setActions(MlTasks.DATAFEED_TASK_NAME + "[c]").get().getTasks().size(), + equalTo(0) + ) ); closeJob(jobId); waitUntilJobIsClosed(jobId); @@ -121,31 +116,25 @@ public void testGetJobStats() throws Exception { client().prepareDelete(MlConfigIndex.indexName(), Job.documentId(jobId1)).get(); client().admin().indices().prepareRefresh(MlConfigIndex.indexName()).get(); - List jobStats = client().execute(GetJobsStatsAction.INSTANCE, - new GetJobsStatsAction.Request("*")) - .get() - .getResponse() - .results(); + List jobStats = client().execute( + GetJobsStatsAction.INSTANCE, + new GetJobsStatsAction.Request("*") + ).get().getResponse().results(); assertThat(jobStats.size(), equalTo(2)); assertThat(jobStats.get(0).getJobId(), equalTo(jobId2)); assertThat(jobStats.get(1).getJobId(), equalTo(jobId1)); forceCloseJob(jobId1); closeJob(jobId2); - assertBusy(() -> - assertThat(client().admin() - .cluster() - .prepareListTasks() - .setActions(MlTasks.JOB_TASK_NAME + "[c]") - .get() - .getTasks() - .size(), equalTo(0)) + assertBusy( + () -> assertThat( + client().admin().cluster().prepareListTasks().setActions(MlTasks.JOB_TASK_NAME + "[c]").get().getTasks().size(), + equalTo(0) + ) ); } public void testGetDatafeedStats() throws Exception { - client().admin().indices().prepareCreate(index) - .setMapping("time", "type=date", "value", "type=long") - .get(); + client().admin().indices().prepareCreate(index).setMapping("time", "type=date", "value", "type=long").get(); final String jobId1 = "job-with-datafeed-missing-config-stats"; final String jobId2 = "job-with-datafeed-config-stats"; @@ -157,15 +146,21 @@ public void testGetDatafeedStats() throws Exception { putJob(job2); openJob(job2.getId()); - DatafeedConfig.Builder datafeedConfigBuilder1 = - createDatafeedBuilder(job1.getId() + "-datafeed", job1.getId(), Collections.singletonList(index)); + DatafeedConfig.Builder datafeedConfigBuilder1 = createDatafeedBuilder( + job1.getId() + "-datafeed", + job1.getId(), + Collections.singletonList(index) + ); DatafeedConfig datafeedConfig1 = datafeedConfigBuilder1.build(); putDatafeed(datafeedConfig1); startDatafeed(datafeedConfig1.getId(), 0L, null); - DatafeedConfig.Builder datafeedConfigBuilder2 = - createDatafeedBuilder(job2.getId() + "-datafeed", job2.getId(), Collections.singletonList(index)); + DatafeedConfig.Builder datafeedConfigBuilder2 = createDatafeedBuilder( + job2.getId() + "-datafeed", + job2.getId(), + Collections.singletonList(index) + ); DatafeedConfig datafeedConfig2 = datafeedConfigBuilder2.build(); putDatafeed(datafeedConfig2); @@ -174,25 +169,21 @@ public void testGetDatafeedStats() throws Exception { client().prepareDelete(MlConfigIndex.indexName(), DatafeedConfig.documentId(datafeedConfig1.getId())).get(); client().admin().indices().prepareRefresh(MlConfigIndex.indexName()).get(); - List dfStats = client().execute(GetDatafeedsStatsAction.INSTANCE, - new GetDatafeedsStatsAction.Request("*")) - .get() - .getResponse() - .results(); + List dfStats = client().execute( + GetDatafeedsStatsAction.INSTANCE, + new GetDatafeedsStatsAction.Request("*") + ).get().getResponse().results(); assertThat(dfStats.size(), equalTo(2)); assertThat(dfStats.get(0).getDatafeedId(), equalTo(datafeedConfig2.getId())); assertThat(dfStats.get(1).getDatafeedId(), equalTo(datafeedConfig1.getId())); forceStopDatafeed(datafeedConfig1.getId()); stopDatafeed(datafeedConfig2.getId()); - assertBusy(() -> - assertThat(client().admin() - .cluster() - .prepareListTasks() - .setActions(MlTasks.DATAFEED_TASK_NAME + "[c]") - .get() - .getTasks() - .size(), equalTo(0)) + assertBusy( + () -> assertThat( + client().admin().cluster().prepareListTasks().setActions(MlTasks.DATAFEED_TASK_NAME + "[c]").get().getTasks().size(), + equalTo(0) + ) ); closeJob(jobId1); closeJob(jobId2); diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlDailyMaintenanceServiceIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlDailyMaintenanceServiceIT.java index 607468375e0da..9fd81500d10e0 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlDailyMaintenanceServiceIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlDailyMaintenanceServiceIT.java @@ -50,14 +50,14 @@ public void setUpMocks() { } public void testTriggerDeleteJobsInStateDeletingWithoutDeletionTask() throws InterruptedException { - MlDailyMaintenanceService maintenanceService = - new MlDailyMaintenanceService( - settings(Version.CURRENT).build(), - ClusterName.DEFAULT, - threadPool, - client(), - mock(ClusterService.class), - mock(MlAssignmentNotifier.class)); + MlDailyMaintenanceService maintenanceService = new MlDailyMaintenanceService( + settings(Version.CURRENT).build(), + ClusterName.DEFAULT, + threadPool, + client(), + mock(ClusterService.class), + mock(MlAssignmentNotifier.class) + ); putJob("maintenance-test-1"); putJob("maintenance-test-2"); @@ -67,10 +67,12 @@ public void testTriggerDeleteJobsInStateDeletingWithoutDeletionTask() throws Int blockingCall(maintenanceService::triggerDeleteJobsInStateDeletingWithoutDeletionTask); assertThat(getJobIds(), containsInAnyOrder("maintenance-test-1", "maintenance-test-2", "maintenance-test-3")); - this.blockingCall(listener -> jobConfigProvider.updateJobBlockReason( - "maintenance-test-2", new Blocked(Blocked.Reason.DELETE, null), listener)); - this.blockingCall(listener -> jobConfigProvider.updateJobBlockReason( - "maintenance-test-3", new Blocked(Blocked.Reason.DELETE, null), listener)); + this.blockingCall( + listener -> jobConfigProvider.updateJobBlockReason("maintenance-test-2", new Blocked(Blocked.Reason.DELETE, null), listener) + ); + this.blockingCall( + listener -> jobConfigProvider.updateJobBlockReason("maintenance-test-3", new Blocked(Blocked.Reason.DELETE, null), listener) + ); assertThat(getJobIds(), containsInAnyOrder("maintenance-test-1", "maintenance-test-2", "maintenance-test-3")); assertThat(getJob("maintenance-test-1").get(0).isDeleting(), is(false)); assertThat(getJob("maintenance-test-2").get(0).isDeleting(), is(true)); @@ -83,15 +85,10 @@ public void testTriggerDeleteJobsInStateDeletingWithoutDeletionTask() throws Int private void blockingCall(Consumer> function) throws InterruptedException { AtomicReference exceptionHolder = new AtomicReference<>(); CountDownLatch latch = new CountDownLatch(1); - ActionListener listener = ActionListener.wrap( - r -> { - latch.countDown(); - }, - e -> { - exceptionHolder.set(e); - latch.countDown(); - } - ); + ActionListener listener = ActionListener.wrap(r -> { latch.countDown(); }, e -> { + exceptionHolder.set(e); + latch.countDown(); + }); function.accept(listener); latch.await(); if (exceptionHolder.get() != null) { @@ -100,19 +97,10 @@ private void blockingCall(Consumer> function) throws Inter } private void putJob(String jobId) { - Job.Builder job = - new Job.Builder(jobId) - .setAnalysisConfig( - new AnalysisConfig.Builder((List) null) - .setBucketSpan(TimeValue.timeValueHours(1)) - .setDetectors( - Collections.singletonList( - new Detector.Builder("count", null) - .setPartitionFieldName("user") - .build()))) - .setDataDescription( - new DataDescription.Builder() - .setTimeFormat("epoch")); + Job.Builder job = new Job.Builder(jobId).setAnalysisConfig( + new AnalysisConfig.Builder((List) null).setBucketSpan(TimeValue.timeValueHours(1)) + .setDetectors(Collections.singletonList(new Detector.Builder("count", null).setPartitionFieldName("user").build())) + ).setDataDescription(new DataDescription.Builder().setTimeFormat("epoch")); putJob(job); } diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlInitializationServiceIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlInitializationServiceIT.java index a3dc2475ba75a..7b839d5914fb9 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlInitializationServiceIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlInitializationServiceIT.java @@ -52,8 +52,7 @@ public void testThatMlIndicesBecomeHiddenWhenTheNodeBecomesMaster() throws Excep ".ml-state-000001", ".ml-stats-000001", ".ml-notifications-000002", - ".ml-annotations-6" - }; + ".ml-annotations-6" }; String[] otherIndexNames = { "some-index-1", "some-other-index-2" }; String[] allIndexNames = Stream.concat(Arrays.stream(mlHiddenIndexNames), Arrays.stream(otherIndexNames)).toArray(String[]::new); @@ -66,10 +65,11 @@ public void testThatMlIndicesBecomeHiddenWhenTheNodeBecomesMaster() throws Excep } createIndex(otherIndexNames); - GetSettingsResponse settingsResponse = - client().admin().indices().prepareGetSettings(allIndexNames) - .setIndicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN_CLOSED_HIDDEN) - .get(); + GetSettingsResponse settingsResponse = client().admin() + .indices() + .prepareGetSettings(allIndexNames) + .setIndicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN_CLOSED_HIDDEN) + .get(); assertThat(settingsResponse, is(notNullValue())); for (String indexName : mlHiddenIndexNames) { Settings settings = settingsResponse.getIndexToSettings().get(indexName); @@ -80,37 +80,42 @@ public void testThatMlIndicesBecomeHiddenWhenTheNodeBecomesMaster() throws Excep assertThat(settings, is(notNullValue())); assertThat( "Index " + indexName + " expected not to be hidden but was", - settings.getAsBoolean(SETTING_INDEX_HIDDEN, false), is(equalTo(false))); + settings.getAsBoolean(SETTING_INDEX_HIDDEN, false), + is(equalTo(false)) + ); } mlInitializationService.onMaster(); assertBusy(() -> assertTrue(mlInitializationService.areMlInternalIndicesHidden())); - settingsResponse = - client().admin().indices().prepareGetSettings(allIndexNames) - .setIndicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN_CLOSED_HIDDEN) - .get(); + settingsResponse = client().admin() + .indices() + .prepareGetSettings(allIndexNames) + .setIndicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN_CLOSED_HIDDEN) + .get(); assertThat(settingsResponse, is(notNullValue())); for (String indexName : mlHiddenIndexNames) { Settings settings = settingsResponse.getIndexToSettings().get(indexName); assertThat(settings, is(notNullValue())); assertThat( "Index " + indexName + " expected to be hidden but wasn't, settings = " + settings, - settings.getAsBoolean(SETTING_INDEX_HIDDEN, false), is(equalTo(true))); + settings.getAsBoolean(SETTING_INDEX_HIDDEN, false), + is(equalTo(true)) + ); } for (String indexName : otherIndexNames) { Settings settings = settingsResponse.getIndexToSettings().get(indexName); assertThat(settings, is(notNullValue())); assertThat( "Index " + indexName + " expected not to be hidden but was, settings = " + settings, - settings.getAsBoolean(SETTING_INDEX_HIDDEN, false), is(equalTo(false))); + settings.getAsBoolean(SETTING_INDEX_HIDDEN, false), + is(equalTo(false)) + ); } } @Override public Settings indexSettings() { - return Settings.builder().put(super.indexSettings()) - .put(IndexMetadata.SETTING_DATA_PATH, (String) null) - .build(); + return Settings.builder().put(super.indexSettings()).put(IndexMetadata.SETTING_DATA_PATH, (String) null).build(); } } diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlJobIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlJobIT.java index 1ec6245b61602..4ee80fb30e381 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlJobIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlJobIT.java @@ -43,12 +43,18 @@ public class MlJobIT extends ESRestTestCase { - private static final String BASIC_AUTH_VALUE = UsernamePasswordToken.basicAuthHeaderValue("x_pack_rest_user", - SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING); + private static final String BASIC_AUTH_VALUE = UsernamePasswordToken.basicAuthHeaderValue( + "x_pack_rest_user", + SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING + ); private static final RequestOptions POST_DATA = RequestOptions.DEFAULT.toBuilder() - .setWarningsHandler(warnings -> Collections.singletonList( - "Posting data directly to anomaly detection jobs is deprecated, " - + "in a future major version it will be compulsory to use a datafeed").equals(warnings) == false).build(); + .setWarningsHandler( + warnings -> Collections.singletonList( + "Posting data directly to anomaly detection jobs is deprecated, " + + "in a future major version it will be compulsory to use a datafeed" + ).equals(warnings) == false + ) + .build(); @Override protected Settings restClientSettings() { @@ -67,8 +73,10 @@ public void testPutJob_GivenFarequoteConfig() throws Exception { } public void testGetJob_GivenNoSuchJob() { - ResponseException e = expectThrows(ResponseException.class, () -> - client().performRequest(new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/non-existing-job/_stats"))); + ResponseException e = expectThrows( + ResponseException.class, + () -> client().performRequest(new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/non-existing-job/_stats")) + ); assertThat(e.getResponse().getStatusLine().getStatusCode(), equalTo(404)); assertThat(e.getMessage(), containsString("No known job with id 'non-existing-job'")); @@ -77,8 +85,9 @@ public void testGetJob_GivenNoSuchJob() { public void testGetJob_GivenJobExists() throws Exception { createFarequoteJob("get-job_given-job-exists-job"); - Response response = client().performRequest(new Request("GET", - MachineLearning.BASE_PATH + "anomaly_detectors/get-job_given-job-exists-job/_stats")); + Response response = client().performRequest( + new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/get-job_given-job-exists-job/_stats") + ); String responseAsString = EntityUtils.toString(response.getEntity()); assertThat(responseAsString, containsString("\"count\":1")); assertThat(responseAsString, containsString("\"job_id\":\"get-job_given-job-exists-job\"")); @@ -90,13 +99,15 @@ public void testGetJobs_GivenSingleJob() throws Exception { // Explicit _all String explictAll = EntityUtils.toString( - client().performRequest(new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/_all")).getEntity()); + client().performRequest(new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/_all")).getEntity() + ); assertThat(explictAll, containsString("\"count\":1")); assertThat(explictAll, containsString("\"job_id\":\"" + jobId + "\"")); // Implicit _all String implicitAll = EntityUtils.toString( - client().performRequest(new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors")).getEntity()); + client().performRequest(new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors")).getEntity() + ); assertThat(implicitAll, containsString("\"count\":1")); assertThat(implicitAll, containsString("\"job_id\":\"" + jobId + "\"")); } @@ -108,7 +119,8 @@ public void testGetJobs_GivenMultipleJobs() throws Exception { // Explicit _all String explicitAll = EntityUtils.toString( - client().performRequest(new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/_all")).getEntity()); + client().performRequest(new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/_all")).getEntity() + ); assertThat(explicitAll, containsString("\"count\":3")); assertThat(explicitAll, containsString("\"job_id\":\"given-multiple-jobs-job-1\"")); assertThat(explicitAll, containsString("\"job_id\":\"given-multiple-jobs-job-2\"")); @@ -116,7 +128,8 @@ public void testGetJobs_GivenMultipleJobs() throws Exception { // Implicit _all String implicitAll = EntityUtils.toString( - client().performRequest(new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors")).getEntity()); + client().performRequest(new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors")).getEntity() + ); assertThat(implicitAll, containsString("\"count\":3")); assertThat(implicitAll, containsString("\"job_id\":\"given-multiple-jobs-job-1\"")); assertThat(implicitAll, containsString("\"job_id\":\"given-multiple-jobs-job-2\"")); @@ -138,29 +151,34 @@ public void testUsage() throws IOException { } private Response createFarequoteJob(String jobId) throws IOException { - return putJob(jobId, "{\n" - + " \"description\":\"Analysis of response time by airline\",\n" - + " \"analysis_config\" : {\n" - + " \"bucket_span\": \"3600s\",\n" - + " \"detectors\" :[{\"function\":\"metric\",\"field_name\":\"responsetime\",\"by_field_name\":\"airline\"}]\n" - + " },\n" + " \"data_description\" : {\n" - + " \"time_field\":\"time\",\n" - + " \"time_format\":\"yyyy-MM-dd HH:mm:ssX\"\n" - + " }\n" - + "}"); + return putJob( + jobId, + "{\n" + + " \"description\":\"Analysis of response time by airline\",\n" + + " \"analysis_config\" : {\n" + + " \"bucket_span\": \"3600s\",\n" + + " \"detectors\" :[{\"function\":\"metric\",\"field_name\":\"responsetime\",\"by_field_name\":\"airline\"}]\n" + + " },\n" + + " \"data_description\" : {\n" + + " \"time_field\":\"time\",\n" + + " \"time_format\":\"yyyy-MM-dd HH:mm:ssX\"\n" + + " }\n" + + "}" + ); } public void testCantCreateJobWithSameID() throws Exception { - String jobTemplate = "{\n" + - " \"analysis_config\" : {\n" + - " \"detectors\" :[{\"function\":\"metric\",\"field_name\":\"responsetime\"}]\n" + - " },\n" + - " \"data_description\": {},\n" + - " \"results_index_name\" : \"%s\"}"; + String jobTemplate = "{\n" + + " \"analysis_config\" : {\n" + + " \"detectors\" :[{\"function\":\"metric\",\"field_name\":\"responsetime\"}]\n" + + " },\n" + + " \"data_description\": {},\n" + + " \"results_index_name\" : \"%s\"}"; String jobId = "cant-create-job-with-same-id-job"; putJob(jobId, String.format(Locale.ROOT, jobTemplate, "index-1")); - ResponseException e = expectThrows(ResponseException.class, + ResponseException e = expectThrows( + ResponseException.class, () -> putJob(jobId, String.format(Locale.ROOT, jobTemplate, "index-2")) ); @@ -169,12 +187,12 @@ public void testCantCreateJobWithSameID() throws Exception { } public void testCreateJobsWithIndexNameOption() throws Exception { - String jobTemplate = "{\n" + - " \"analysis_config\" : {\n" + - " \"detectors\" :[{\"function\":\"metric\",\"field_name\":\"responsetime\"}]\n" + - " },\n" + - " \"data_description\": {},\n" + - " \"results_index_name\" : \"%s\"}"; + String jobTemplate = "{\n" + + " \"analysis_config\" : {\n" + + " \"detectors\" :[{\"function\":\"metric\",\"field_name\":\"responsetime\"}]\n" + + " },\n" + + " \"data_description\": {},\n" + + " \"results_index_name\" : \"%s\"}"; String jobId1 = "create-jobs-with-index-name-option-job-1"; String indexName = "non-default-index"; @@ -189,77 +207,130 @@ public void testCreateJobsWithIndexNameOption() throws Exception { assertBusy(() -> { try { String aliasesResponse = getAliases(); - assertThat(aliasesResponse, containsString("\"" + AnomalyDetectorsIndex.jobResultsAliasedName("custom-" + indexName) - + "\":{\"aliases\":{")); - assertThat(aliasesResponse, containsString("\"" + AnomalyDetectorsIndex.jobResultsAliasedName(jobId1) - + "\":{\"filter\":{\"term\":{\"job_id\":{\"value\":\"" + jobId1 + "\",\"boost\":1.0}}},\"is_hidden\":true}")); - assertThat(aliasesResponse, containsString("\"" + AnomalyDetectorsIndex.resultsWriteAlias(jobId1) - + "\":{\"is_hidden\":true}")); - assertThat(aliasesResponse, containsString("\"" + AnomalyDetectorsIndex.jobResultsAliasedName(jobId2) - + "\":{\"filter\":{\"term\":{\"job_id\":{\"value\":\"" + jobId2 + "\",\"boost\":1.0}}},\"is_hidden\":true}")); - assertThat(aliasesResponse, containsString("\"" + AnomalyDetectorsIndex.resultsWriteAlias(jobId2) - + "\":{\"is_hidden\":true}")); + assertThat( + aliasesResponse, + containsString("\"" + AnomalyDetectorsIndex.jobResultsAliasedName("custom-" + indexName) + "\":{\"aliases\":{") + ); + assertThat( + aliasesResponse, + containsString( + "\"" + + AnomalyDetectorsIndex.jobResultsAliasedName(jobId1) + + "\":{\"filter\":{\"term\":{\"job_id\":{\"value\":\"" + + jobId1 + + "\",\"boost\":1.0}}},\"is_hidden\":true}" + ) + ); + assertThat( + aliasesResponse, + containsString("\"" + AnomalyDetectorsIndex.resultsWriteAlias(jobId1) + "\":{\"is_hidden\":true}") + ); + assertThat( + aliasesResponse, + containsString( + "\"" + + AnomalyDetectorsIndex.jobResultsAliasedName(jobId2) + + "\":{\"filter\":{\"term\":{\"job_id\":{\"value\":\"" + + jobId2 + + "\",\"boost\":1.0}}},\"is_hidden\":true}" + ) + ); + assertThat( + aliasesResponse, + containsString("\"" + AnomalyDetectorsIndex.resultsWriteAlias(jobId2) + "\":{\"is_hidden\":true}") + ); } catch (ResponseException e) { throw new AssertionError(e); } }); // Use _cat/indices/.ml-anomalies-* instead of _cat/indices/_all to workaround https://github.com/elastic/elasticsearch/issues/45652 - String responseAsString = EntityUtils.toString(client().performRequest( - new Request("GET", "/_cat/indices/" + AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + "*")).getEntity()); - assertThat(responseAsString, - containsString(AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + "custom-" + indexName)); + String responseAsString = EntityUtils.toString( + client().performRequest(new Request("GET", "/_cat/indices/" + AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + "*")) + .getEntity() + ); + assertThat(responseAsString, containsString(AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + "custom-" + indexName)); assertThat(responseAsString, not(containsString(AnomalyDetectorsIndex.jobResultsAliasedName(jobId1)))); assertThat(responseAsString, not(containsString(AnomalyDetectorsIndex.jobResultsAliasedName(jobId2)))); - { //create jobId1 docs + { // create jobId1 docs String id = String.format(Locale.ROOT, "%s_bucket_%s_%s", jobId1, "1234", 300); Request createResultRequest = new Request("PUT", AnomalyDetectorsIndex.jobResultsAliasedName(jobId1) + "/_doc/" + id); - createResultRequest.setJsonEntity(String.format(Locale.ROOT, - "{\"job_id\":\"%s\", \"timestamp\": \"%s\", \"result_type\":\"bucket\", \"bucket_span\": \"%s\"}", - jobId1, "1234", 1)); + createResultRequest.setJsonEntity( + String.format( + Locale.ROOT, + "{\"job_id\":\"%s\", \"timestamp\": \"%s\", \"result_type\":\"bucket\", \"bucket_span\": \"%s\"}", + jobId1, + "1234", + 1 + ) + ); client().performRequest(createResultRequest); id = String.format(Locale.ROOT, "%s_bucket_%s_%s", jobId1, "1236", 300); createResultRequest = new Request("PUT", AnomalyDetectorsIndex.jobResultsAliasedName(jobId1) + "/_doc/" + id); - createResultRequest.setJsonEntity(String.format(Locale.ROOT, - "{\"job_id\":\"%s\", \"timestamp\": \"%s\", \"result_type\":\"bucket\", \"bucket_span\": \"%s\"}", - jobId1, "1236", 1)); + createResultRequest.setJsonEntity( + String.format( + Locale.ROOT, + "{\"job_id\":\"%s\", \"timestamp\": \"%s\", \"result_type\":\"bucket\", \"bucket_span\": \"%s\"}", + jobId1, + "1236", + 1 + ) + ); client().performRequest(createResultRequest); refreshAllIndices(); - responseAsString = EntityUtils.toString(client().performRequest( - new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId1 + "/results/buckets")).getEntity()); + responseAsString = EntityUtils.toString( + client().performRequest(new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId1 + "/results/buckets")) + .getEntity() + ); assertThat(responseAsString, containsString("\"count\":2")); - responseAsString = EntityUtils.toString(client().performRequest( - new Request("GET", AnomalyDetectorsIndex.jobResultsAliasedName(jobId1) + "/_search")).getEntity()); + responseAsString = EntityUtils.toString( + client().performRequest(new Request("GET", AnomalyDetectorsIndex.jobResultsAliasedName(jobId1) + "/_search")).getEntity() + ); assertThat(responseAsString, containsString("\"value\":2")); } - { //create jobId2 docs + { // create jobId2 docs String id = String.format(Locale.ROOT, "%s_bucket_%s_%s", jobId2, "1234", 300); Request createResultRequest = new Request("PUT", AnomalyDetectorsIndex.jobResultsAliasedName(jobId2) + "/_doc/" + id); - createResultRequest.setJsonEntity(String.format(Locale.ROOT, - "{\"job_id\":\"%s\", \"timestamp\": \"%s\", \"result_type\":\"bucket\", \"bucket_span\": \"%s\"}", - jobId2, "1234", 1)); + createResultRequest.setJsonEntity( + String.format( + Locale.ROOT, + "{\"job_id\":\"%s\", \"timestamp\": \"%s\", \"result_type\":\"bucket\", \"bucket_span\": \"%s\"}", + jobId2, + "1234", + 1 + ) + ); client().performRequest(createResultRequest); id = String.format(Locale.ROOT, "%s_bucket_%s_%s", jobId2, "1236", 300); createResultRequest = new Request("PUT", AnomalyDetectorsIndex.jobResultsAliasedName(jobId2) + "/_doc/" + id); - createResultRequest.setJsonEntity(String.format(Locale.ROOT, - "{\"job_id\":\"%s\", \"timestamp\": \"%s\", \"result_type\":\"bucket\", \"bucket_span\": \"%s\"}", - jobId2, "1236", 1)); + createResultRequest.setJsonEntity( + String.format( + Locale.ROOT, + "{\"job_id\":\"%s\", \"timestamp\": \"%s\", \"result_type\":\"bucket\", \"bucket_span\": \"%s\"}", + jobId2, + "1236", + 1 + ) + ); client().performRequest(createResultRequest); refreshAllIndices(); - responseAsString = EntityUtils.toString(client().performRequest( - new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId2 + "/results/buckets")).getEntity()); + responseAsString = EntityUtils.toString( + client().performRequest(new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId2 + "/results/buckets")) + .getEntity() + ); assertThat(responseAsString, containsString("\"count\":2")); - responseAsString = EntityUtils.toString(client().performRequest( - new Request("GET", AnomalyDetectorsIndex.jobResultsAliasedName(jobId2) + "/_search")).getEntity()); + responseAsString = EntityUtils.toString( + client().performRequest(new Request("GET", AnomalyDetectorsIndex.jobResultsAliasedName(jobId2) + "/_search")).getEntity() + ); assertThat(responseAsString, containsString("\"value\":2")); } @@ -268,16 +339,21 @@ public void testCreateJobsWithIndexNameOption() throws Exception { // check that indices still exist, but no longer have job1 entries and aliases are gone responseAsString = getAliases(); assertThat(responseAsString, not(containsString(AnomalyDetectorsIndex.jobResultsAliasedName(jobId1)))); - assertThat(responseAsString, containsString(AnomalyDetectorsIndex.jobResultsAliasedName(jobId2))); //job2 still exists + assertThat(responseAsString, containsString(AnomalyDetectorsIndex.jobResultsAliasedName(jobId2))); // job2 still exists - responseAsString = EntityUtils.toString(client().performRequest( - new Request("GET", "/_cat/indices/" + AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + "*")).getEntity()); + responseAsString = EntityUtils.toString( + client().performRequest(new Request("GET", "/_cat/indices/" + AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + "*")) + .getEntity() + ); assertThat(responseAsString, containsString(AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + "custom-" + indexName)); refreshAllIndices(); - responseAsString = EntityUtils.toString(client().performRequest( - new Request("GET", AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + "custom-" + indexName + "/_count")).getEntity()); + responseAsString = EntityUtils.toString( + client().performRequest( + new Request("GET", AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + "custom-" + indexName + "/_count") + ).getEntity() + ); assertThat(responseAsString, containsString("\"count\":2")); // Delete the second job and verify aliases are gone, and original concrete/custom index is gone @@ -286,18 +362,20 @@ public void testCreateJobsWithIndexNameOption() throws Exception { assertThat(responseAsString, not(containsString(AnomalyDetectorsIndex.jobResultsAliasedName(jobId2)))); refreshAllIndices(); - responseAsString = EntityUtils.toString(client().performRequest( - new Request("GET", "/_cat/indices/" + AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + "*")).getEntity()); + responseAsString = EntityUtils.toString( + client().performRequest(new Request("GET", "/_cat/indices/" + AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + "*")) + .getEntity() + ); assertThat(responseAsString, not(containsString(AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + "custom-" + indexName))); } public void testCreateJobInSharedIndexUpdatesMapping() throws Exception { - String jobTemplate = "{\n" + - " \"analysis_config\" : {\n" + - " \"detectors\" :[{\"function\":\"metric\",\"field_name\":\"metric\", \"by_field_name\":\"%s\"}]\n" + - " },\n" + - " \"data_description\": {}\n" + - "}"; + String jobTemplate = "{\n" + + " \"analysis_config\" : {\n" + + " \"detectors\" :[{\"function\":\"metric\",\"field_name\":\"metric\", \"by_field_name\":\"%s\"}]\n" + + " },\n" + + " \"data_description\": {}\n" + + "}"; String jobId1 = "create-job-in-shared-index-updates-mapping-job-1"; String byFieldName1 = "responsetime"; @@ -307,8 +385,10 @@ public void testCreateJobInSharedIndexUpdatesMapping() throws Exception { putJob(jobId1, String.format(Locale.ROOT, jobTemplate, byFieldName1)); // Check the index mapping contains the first by_field_name - Request getResultsMappingRequest = new Request("GET", - AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + AnomalyDetectorsIndexFields.RESULTS_INDEX_DEFAULT + "/_mapping"); + Request getResultsMappingRequest = new Request( + "GET", + AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + AnomalyDetectorsIndexFields.RESULTS_INDEX_DEFAULT + "/_mapping" + ); getResultsMappingRequest.addParameter("pretty", null); String resultsMappingAfterJob1 = EntityUtils.toString(client().performRequest(getResultsMappingRequest).getEntity()); assertThat(resultsMappingAfterJob1, containsString(byFieldName1)); @@ -322,12 +402,12 @@ public void testCreateJobInSharedIndexUpdatesMapping() throws Exception { } public void testCreateJobInCustomSharedIndexUpdatesMapping() throws Exception { - String jobTemplate = "{\n" + - " \"analysis_config\" : {\n" + - " \"detectors\" :[{\"function\":\"metric\",\"field_name\":\"metric\", \"by_field_name\":\"%s\"}]\n" + - " },\n" + - " \"data_description\": {},\n" + - " \"results_index_name\" : \"shared-index\"}"; + String jobTemplate = "{\n" + + " \"analysis_config\" : {\n" + + " \"detectors\" :[{\"function\":\"metric\",\"field_name\":\"metric\", \"by_field_name\":\"%s\"}]\n" + + " },\n" + + " \"data_description\": {},\n" + + " \"results_index_name\" : \"shared-index\"}"; String jobId1 = "create-job-in-custom-shared-index-updates-mapping-job-1"; String byFieldName1 = "responsetime"; @@ -337,8 +417,10 @@ public void testCreateJobInCustomSharedIndexUpdatesMapping() throws Exception { putJob(jobId1, String.format(Locale.ROOT, jobTemplate, byFieldName1)); // Check the index mapping contains the first by_field_name - Request getResultsMappingRequest = new Request("GET", - AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + "custom-shared-index/_mapping"); + Request getResultsMappingRequest = new Request( + "GET", + AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + "custom-shared-index/_mapping" + ); getResultsMappingRequest.addParameter("pretty", null); String resultsMappingAfterJob1 = EntityUtils.toString(client().performRequest(getResultsMappingRequest).getEntity()); assertThat(resultsMappingAfterJob1, containsString(byFieldName1)); @@ -353,12 +435,12 @@ public void testCreateJobInCustomSharedIndexUpdatesMapping() throws Exception { } public void testCreateJob_WithClashingFieldMappingsFails() throws Exception { - String jobTemplate = "{\n" + - " \"analysis_config\" : {\n" + - " \"detectors\" :[{\"function\":\"metric\",\"field_name\":\"metric\", \"by_field_name\":\"%s\"}]\n" + - " },\n" + - " \"data_description\": {}\n" + - "}"; + String jobTemplate = "{\n" + + " \"analysis_config\" : {\n" + + " \"detectors\" :[{\"function\":\"metric\",\"field_name\":\"metric\", \"by_field_name\":\"%s\"}]\n" + + " },\n" + + " \"data_description\": {}\n" + + "}"; String jobId1 = "job-with-response-field"; String byFieldName1; @@ -375,11 +457,17 @@ public void testCreateJob_WithClashingFieldMappingsFails() throws Exception { putJob(jobId1, String.format(Locale.ROOT, jobTemplate, byFieldName1)); - ResponseException e = expectThrows(ResponseException.class, - () -> putJob(jobId2, String.format(Locale.ROOT, jobTemplate, byFieldName2))); - assertThat(e.getMessage(), - containsString("This job would cause a mapping clash with existing field [response] - " + - "avoid the clash by assigning a dedicated results index")); + ResponseException e = expectThrows( + ResponseException.class, + () -> putJob(jobId2, String.format(Locale.ROOT, jobTemplate, byFieldName2)) + ); + assertThat( + e.getMessage(), + containsString( + "This job would cause a mapping clash with existing field [response] - " + + "avoid the clash by assigning a dedicated results index" + ) + ); } public void testOpenJobFailsWhenPersistentTaskAssignmentDisabled() throws Exception { @@ -387,32 +475,29 @@ public void testOpenJobFailsWhenPersistentTaskAssignmentDisabled() throws Except createFarequoteJob(jobId); Request disablePersistentTaskAssignmentRequest = new Request("PUT", "_cluster/settings"); - disablePersistentTaskAssignmentRequest.setJsonEntity("{\n" + - " \"persistent\": {\n" + - " \"cluster.persistent_tasks.allocation.enable\": \"none\"\n" + - " }\n" + - "}"); + disablePersistentTaskAssignmentRequest.setJsonEntity( + "{\n" + " \"persistent\": {\n" + " \"cluster.persistent_tasks.allocation.enable\": \"none\"\n" + " }\n" + "}" + ); Response disablePersistentTaskAssignmentResponse = client().performRequest(disablePersistentTaskAssignmentRequest); assertThat(entityAsMap(disablePersistentTaskAssignmentResponse), hasEntry("acknowledged", true)); try { - ResponseException exception = expectThrows( - ResponseException.class, - () -> openJob(jobId) - ); + ResponseException exception = expectThrows(ResponseException.class, () -> openJob(jobId)); assertThat(exception.getResponse().getStatusLine().getStatusCode(), equalTo(429)); - assertThat(EntityUtils.toString(exception.getResponse().getEntity()), - containsString("Cannot open jobs because persistent task assignment is disabled by the " + - "[cluster.persistent_tasks.allocation.enable] setting")); + assertThat( + EntityUtils.toString(exception.getResponse().getEntity()), + containsString( + "Cannot open jobs because persistent task assignment is disabled by the " + + "[cluster.persistent_tasks.allocation.enable] setting" + ) + ); } finally { // Try to revert the cluster setting change even if the test fails, // because otherwise this setting will cause many other tests to fail Request enablePersistentTaskAssignmentRequest = new Request("PUT", "_cluster/settings"); - enablePersistentTaskAssignmentRequest.setJsonEntity("{\n" + - " \"persistent\": {\n" + - " \"cluster.persistent_tasks.allocation.enable\": \"all\"\n" + - " }\n" + - "}"); + enablePersistentTaskAssignmentRequest.setJsonEntity( + "{\n" + " \"persistent\": {\n" + " \"cluster.persistent_tasks.allocation.enable\": \"all\"\n" + " }\n" + "}" + ); Response enablePersistentTaskAssignmentResponse = client().performRequest(disablePersistentTaskAssignmentRequest); assertThat(entityAsMap(enablePersistentTaskAssignmentResponse), hasEntry("acknowledged", true)); } @@ -424,22 +509,28 @@ public void testDeleteJob() throws Exception { createFarequoteJob(jobId); // Use _cat/indices/.ml-anomalies-* instead of _cat/indices/_all to workaround https://github.com/elastic/elasticsearch/issues/45652 - String indicesBeforeDelete = EntityUtils.toString(client().performRequest( - new Request("GET", "/_cat/indices/" + AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + "*")).getEntity()); + String indicesBeforeDelete = EntityUtils.toString( + client().performRequest(new Request("GET", "/_cat/indices/" + AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + "*")) + .getEntity() + ); assertThat(indicesBeforeDelete, containsString(indexName)); client().performRequest(new Request("DELETE", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId)); // check that the index still exists (it's shared by default) - String indicesAfterDelete = EntityUtils.toString(client().performRequest( - new Request("GET", "/_cat/indices/" + AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + "*")).getEntity()); + String indicesAfterDelete = EntityUtils.toString( + client().performRequest(new Request("GET", "/_cat/indices/" + AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + "*")) + .getEntity() + ); assertThat(indicesAfterDelete, containsString(indexName)); waitUntilIndexIsEmpty(indexName); // check that the job itself is gone - expectThrows(ResponseException.class, () -> - client().performRequest(new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats"))); + expectThrows( + ResponseException.class, + () -> client().performRequest(new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats")) + ); } public void testOutOfOrderData() throws Exception { @@ -460,8 +551,9 @@ public void testOutOfOrderData() throws Exception { postDataRequest.setJsonEntity("{ \"airline\":\"LOT\", \"responsetime\":100, \"time\":\"2019-07-01 00:10:00Z\" }"); client().performRequest(postDataRequest); - Response flushResponse = - client().performRequest(new Request("POST", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_flush")); + Response flushResponse = client().performRequest( + new Request("POST", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_flush") + ); assertThat(entityAsMap(flushResponse), hasEntry("flushed", true)); closeJob(jobId); @@ -469,7 +561,7 @@ public void testOutOfOrderData() throws Exception { String stats = EntityUtils.toString( client().performRequest(new Request("GET", "_ml/anomaly_detectors/" + jobId + "/_stats")).getEntity() ); - //assert 2019-07-01 00:30:00Z + // assert 2019-07-01 00:30:00Z assertThat(stats, containsString("\"latest_record_timestamp\":1561941000000")); assertThat(stats, containsString("\"out_of_order_timestamp_count\":0")); assertThat(stats, containsString("\"processed_record_count\":3")); @@ -484,7 +576,8 @@ public void testDeleteJob_TimingStatsDocumentIsDeleted() throws Exception { assertThat( EntityUtils.toString(client().performRequest(new Request("GET", indexName + "/_count")).getEntity()), - containsString("\"count\":0")); // documents related to the job do not exist yet + containsString("\"count\":0") + ); // documents related to the job do not exist yet openJob(jobId); @@ -496,15 +589,16 @@ public void testDeleteJob_TimingStatsDocumentIsDeleted() throws Exception { postDataRequest.setJsonEntity("{ \"airline\":\"LOT\", \"response_time\":100, \"time\":\"2019-07-01 02:00:00Z\" }"); client().performRequest(postDataRequest); - Response flushResponse = - client().performRequest(new Request("POST", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_flush")); + Response flushResponse = client().performRequest( + new Request("POST", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_flush") + ); assertThat(entityAsMap(flushResponse), hasEntry("flushed", true)); closeJob(jobId); - String timingStatsDoc = - EntityUtils.toString( - client().performRequest(new Request("GET", indexName + "/_doc/" + TimingStats.documentId(jobId))).getEntity()); + String timingStatsDoc = EntityUtils.toString( + client().performRequest(new Request("GET", indexName + "/_doc/" + TimingStats.documentId(jobId))).getEntity() + ); assertThat(timingStatsDoc, containsString("\"bucket_count\":2")); // TimingStats doc exists, 2 buckets have been processed client().performRequest(new Request("DELETE", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId)); @@ -514,13 +608,15 @@ public void testDeleteJob_TimingStatsDocumentIsDeleted() throws Exception { // check that the TimingStats documents got deleted ResponseException exception = expectThrows( ResponseException.class, - () -> client().performRequest(new Request("GET", indexName + "/_doc/" + TimingStats.documentId(jobId)))); + () -> client().performRequest(new Request("GET", indexName + "/_doc/" + TimingStats.documentId(jobId))) + ); assertThat(exception.getResponse().getStatusLine().getStatusCode(), equalTo(404)); // check that the job itself is gone exception = expectThrows( ResponseException.class, - () -> client().performRequest(new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats"))); + () -> client().performRequest(new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats")) + ); assertThat(exception.getResponse().getStatusLine().getStatusCode(), equalTo(404)); } @@ -530,12 +626,15 @@ public void testDeleteJobAsync() throws Exception { createFarequoteJob(jobId); // Use _cat/indices/.ml-anomalies-* instead of _cat/indices/_all to workaround https://github.com/elastic/elasticsearch/issues/45652 - String indicesBeforeDelete = EntityUtils.toString(client().performRequest( - new Request("GET", "/_cat/indices/" + AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + "*")).getEntity()); + String indicesBeforeDelete = EntityUtils.toString( + client().performRequest(new Request("GET", "/_cat/indices/" + AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + "*")) + .getEntity() + ); assertThat(indicesBeforeDelete, containsString(indexName)); - Response response = client().performRequest(new Request("DELETE", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId - + "?wait_for_completion=false")); + Response response = client().performRequest( + new Request("DELETE", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "?wait_for_completion=false") + ); // Wait for task to complete String taskId = extractTaskId(response); @@ -543,15 +642,19 @@ public void testDeleteJobAsync() throws Exception { assertThat(EntityUtils.toString(taskResponse.getEntity()), containsString("\"acknowledged\":true")); // check that the index still exists (it's shared by default) - String indicesAfterDelete = EntityUtils.toString(client().performRequest( - new Request("GET", "/_cat/indices/" + AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + "*")).getEntity()); + String indicesAfterDelete = EntityUtils.toString( + client().performRequest(new Request("GET", "/_cat/indices/" + AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + "*")) + .getEntity() + ); assertThat(indicesAfterDelete, containsString(indexName)); waitUntilIndexIsEmpty(indexName); // check that the job itself is gone - expectThrows(ResponseException.class, () -> - client().performRequest(new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats"))); + expectThrows( + ResponseException.class, + () -> client().performRequest(new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats")) + ); } private void waitUntilIndexIsEmpty(String indexName) throws Exception { @@ -580,8 +683,10 @@ public void testDeleteJobAfterMissingIndex() throws Exception { createFarequoteJob(jobId); // Use _cat/indices/.ml-anomalies-* instead of _cat/indices/_all to workaround https://github.com/elastic/elasticsearch/issues/45652 - String indicesBeforeDelete = EntityUtils.toString(client().performRequest( - new Request("GET", "/_cat/indices/" + AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + "*")).getEntity()); + String indicesBeforeDelete = EntityUtils.toString( + client().performRequest(new Request("GET", "/_cat/indices/" + AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + "*")) + .getEntity() + ); assertThat(indicesBeforeDelete, containsString(indexName)); // Manually delete the index so that we can test that deletion proceeds @@ -591,13 +696,17 @@ public void testDeleteJobAfterMissingIndex() throws Exception { client().performRequest(new Request("DELETE", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId)); // check index was deleted - String indicesAfterDelete = EntityUtils.toString(client().performRequest( - new Request("GET", "/_cat/indices/" + AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + "*")).getEntity()); + String indicesAfterDelete = EntityUtils.toString( + client().performRequest(new Request("GET", "/_cat/indices/" + AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + "*")) + .getEntity() + ); assertThat(indicesAfterDelete, not(containsString(aliasName))); assertThat(indicesAfterDelete, not(containsString(indexName))); - expectThrows(ResponseException.class, () -> - client().performRequest(new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats"))); + expectThrows( + ResponseException.class, + () -> client().performRequest(new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats")) + ); } public void testDeleteJobAfterMissingAliases() throws Exception { @@ -640,42 +749,66 @@ public void testMultiIndexDelete() throws Exception { // Make the job's results span an extra two indices, i.e. three in total. // To do this the job's results alias needs to encompass all three indices. Request extraIndex1 = new Request("PUT", indexName + "-001"); - extraIndex1.setJsonEntity("{\n" + - " \"aliases\" : {\n" + - " \"" + AnomalyDetectorsIndex.jobResultsAliasedName(jobId)+ "\" : {\n" + - " \"is_hidden\" : true,\n" + - " \"filter\" : {\n" + - " \"term\" : {\"" + Job.ID + "\" : \"" + jobId + "\" }\n" + - " }\n" + - " }\n" + - " }\n" + - "}"); + extraIndex1.setJsonEntity( + "{\n" + + " \"aliases\" : {\n" + + " \"" + + AnomalyDetectorsIndex.jobResultsAliasedName(jobId) + + "\" : {\n" + + " \"is_hidden\" : true,\n" + + " \"filter\" : {\n" + + " \"term\" : {\"" + + Job.ID + + "\" : \"" + + jobId + + "\" }\n" + + " }\n" + + " }\n" + + " }\n" + + "}" + ); client().performRequest(extraIndex1); Request extraIndex2 = new Request("PUT", indexName + "-002"); - extraIndex2.setJsonEntity("{\n" + - " \"aliases\" : {\n" + - " \"" + AnomalyDetectorsIndex.jobResultsAliasedName(jobId)+ "\" : {\n" + - " \"is_hidden\" : true,\n" + - " \"filter\" : {\n" + - " \"term\" : {\"" + Job.ID + "\" : \"" + jobId + "\" }\n" + - " }\n" + - " }\n" + - " }\n" + - "}"); + extraIndex2.setJsonEntity( + "{\n" + + " \"aliases\" : {\n" + + " \"" + + AnomalyDetectorsIndex.jobResultsAliasedName(jobId) + + "\" : {\n" + + " \"is_hidden\" : true,\n" + + " \"filter\" : {\n" + + " \"term\" : {\"" + + Job.ID + + "\" : \"" + + jobId + + "\" }\n" + + " }\n" + + " }\n" + + " }\n" + + "}" + ); client().performRequest(extraIndex2); // Use _cat/indices/.ml-anomalies-* instead of _cat/indices/_all to workaround https://github.com/elastic/elasticsearch/issues/45652 - String indicesBeforeDelete = EntityUtils.toString(client().performRequest( - new Request("GET", "/_cat/indices/" + AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + "*")).getEntity()); + String indicesBeforeDelete = EntityUtils.toString( + client().performRequest(new Request("GET", "/_cat/indices/" + AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + "*")) + .getEntity() + ); assertThat(indicesBeforeDelete, containsString(indexName)); assertThat(indicesBeforeDelete, containsString(indexName + "-001")); assertThat(indicesBeforeDelete, containsString(indexName + "-002")); // Add some documents to each index to make sure the DBQ clears them out Request createDoc0 = new Request("PUT", indexName + "/_doc/" + 123); - createDoc0.setJsonEntity(String.format(Locale.ROOT, - "{\"job_id\":\"%s\", \"timestamp\": \"%s\", \"bucket_span\":%d, \"result_type\":\"record\"}", - jobId, 123, 1)); + createDoc0.setJsonEntity( + String.format( + Locale.ROOT, + "{\"job_id\":\"%s\", \"timestamp\": \"%s\", \"bucket_span\":%d, \"result_type\":\"record\"}", + jobId, + 123, + 1 + ) + ); client().performRequest(createDoc0); Request createDoc1 = new Request("PUT", indexName + "-001/_doc/" + 123); createDoc1.setEntity(createDoc0.getEntity()); @@ -692,12 +825,18 @@ public void testMultiIndexDelete() throws Exception { refreshAllIndices(); // check for the documents - assertThat(EntityUtils.toString(client().performRequest(new Request("GET", indexName+ "/_count")).getEntity()), - containsString("\"count\":2")); - assertThat(EntityUtils.toString(client().performRequest(new Request("GET", indexName+ "-001/_count")).getEntity()), - containsString("\"count\":1")); - assertThat(EntityUtils.toString(client().performRequest(new Request("GET", indexName+ "-002/_count")).getEntity()), - containsString("\"count\":1")); + assertThat( + EntityUtils.toString(client().performRequest(new Request("GET", indexName + "/_count")).getEntity()), + containsString("\"count\":2") + ); + assertThat( + EntityUtils.toString(client().performRequest(new Request("GET", indexName + "-001/_count")).getEntity()), + containsString("\"count\":1") + ); + assertThat( + EntityUtils.toString(client().performRequest(new Request("GET", indexName + "-002/_count")).getEntity()), + containsString("\"count\":1") + ); // Delete client().performRequest(new Request("DELETE", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId)); @@ -705,18 +844,24 @@ public void testMultiIndexDelete() throws Exception { refreshAllIndices(); // check that the default shared index still exists but is empty - String indicesAfterDelete = EntityUtils.toString(client().performRequest( - new Request("GET", "/_cat/indices/" + AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + "*")).getEntity()); + String indicesAfterDelete = EntityUtils.toString( + client().performRequest(new Request("GET", "/_cat/indices/" + AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + "*")) + .getEntity() + ); assertThat(indicesAfterDelete, containsString(indexName)); // other results indices should be deleted as this test job ID is the only job in those indices assertThat(indicesAfterDelete, not(containsString(indexName + "-001"))); assertThat(indicesAfterDelete, not(containsString(indexName + "-002"))); - assertThat(EntityUtils.toString(client().performRequest(new Request("GET", indexName+ "/_count")).getEntity()), - containsString("\"count\":0")); - expectThrows(ResponseException.class, () -> - client().performRequest(new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats"))); + assertThat( + EntityUtils.toString(client().performRequest(new Request("GET", indexName + "/_count")).getEntity()), + containsString("\"count\":0") + ); + expectThrows( + ResponseException.class, + () -> client().performRequest(new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats")) + ); } public void testDelete_multipleRequest() throws Exception { @@ -745,7 +890,7 @@ public void testDelete_multipleRequest() throws Exception { ioe.set(e); } - // Immediately after the first deletion finishes, recreate the job. This should pick up + // Immediately after the first deletion finishes, recreate the job. This should pick up // race conditions where another delete request deletes part of the newly created job. if (recreationGuard.getAndIncrement() == 0) { try { @@ -792,32 +937,37 @@ public void testDelete_multipleRequest() throws Exception { } assertNotNull(recreationResponse.get()); - assertEquals(EntityUtils.toString(recreationResponse.get().getEntity()), - 200, recreationResponse.get().getStatusLine().getStatusCode()); + assertEquals( + EntityUtils.toString(recreationResponse.get().getEntity()), + 200, + recreationResponse.get().getStatusLine().getStatusCode() + ); if (recreationException.get() != null) { assertNull(recreationException.get().getMessage(), recreationException.get()); } - String expectedReadAliasString = "\"" + AnomalyDetectorsIndex.jobResultsAliasedName(jobId) - + "\":{\"filter\":{\"term\":{\"job_id\":{\"value\":\"" + jobId + "\",\"boost\":1.0}}},\"is_hidden\":true}"; + String expectedReadAliasString = "\"" + + AnomalyDetectorsIndex.jobResultsAliasedName(jobId) + + "\":{\"filter\":{\"term\":{\"job_id\":{\"value\":\"" + + jobId + + "\",\"boost\":1.0}}},\"is_hidden\":true}"; String expectedWriteAliasString = "\"" + AnomalyDetectorsIndex.resultsWriteAlias(jobId) + "\":{\"is_hidden\":true}"; try { // The idea of the code above is that the deletion is sufficiently time-consuming that - // all threads enter the deletion call before the first one exits it. Usually this happens, + // all threads enter the deletion call before the first one exits it. Usually this happens, // but in the case that it does not the job that is recreated may get deleted. // It is not a error if the job does not exist but the following assertions // will fail in that case. client().performRequest(new Request("GET", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId)); - // Check that the job aliases exist. These are the last thing to be deleted when a job is deleted, so + // Check that the job aliases exist. These are the last thing to be deleted when a job is deleted, so // if there's been a race between deletion and recreation these are what will be missing. String aliases = getAliases(); assertThat(aliases, containsString(expectedReadAliasString)); assertThat(aliases, containsString(expectedWriteAliasString)); - } catch (ResponseException missingJobException) { // The job does not exist assertThat(missingJobException.getResponse().getStatusLine().getStatusCode(), equalTo(404)); @@ -849,18 +999,16 @@ private String getAliases() throws IOException { } private void openJob(String jobId) throws IOException { - Response openResponse = client().performRequest(new Request( - "POST", - MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_open" - )); + Response openResponse = client().performRequest( + new Request("POST", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_open") + ); assertThat(entityAsMap(openResponse), hasEntry("opened", true)); } private void closeJob(String jobId) throws IOException { - Response openResponse = client().performRequest(new Request( - "POST", - MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_close" - )); + Response openResponse = client().performRequest( + new Request("POST", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_close") + ); assertThat(entityAsMap(openResponse), hasEntry("closed", true)); } diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlNativeAutodetectIntegTestCase.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlNativeAutodetectIntegTestCase.java index 88f836d10f730..b53e33d4f9793 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlNativeAutodetectIntegTestCase.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlNativeAutodetectIntegTestCase.java @@ -13,17 +13,17 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.index.query.QueryBuilders; +import org.elasticsearch.search.SearchHit; +import org.elasticsearch.search.SearchHits; +import org.elasticsearch.search.sort.SortBuilders; +import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.index.query.QueryBuilders; -import org.elasticsearch.search.SearchHit; -import org.elasticsearch.search.SearchHits; -import org.elasticsearch.search.sort.SortBuilders; -import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.xpack.core.action.util.PageParams; import org.elasticsearch.xpack.core.ml.action.CloseJobAction; import org.elasticsearch.xpack.core.ml.action.DeleteDatafeedAction; @@ -160,8 +160,11 @@ protected void waitUntilJobIsClosed(String jobId) throws Exception { } protected void waitUntilJobIsClosed(String jobId, TimeValue waitTime) throws Exception { - assertBusy(() -> assertThat(getJobStats(jobId).get(0).getState(), equalTo(JobState.CLOSED)), - waitTime.getMillis(), TimeUnit.MILLISECONDS); + assertBusy( + () -> assertThat(getJobStats(jobId).get(0).getState(), equalTo(JobState.CLOSED)), + waitTime.getMillis(), + TimeUnit.MILLISECONDS + ); } protected List getJob(String jobId) { @@ -208,8 +211,7 @@ protected RevertModelSnapshotAction.Response revertModelSnapshot(String jobId, S } protected List getCategories(String jobId) { - GetCategoriesAction.Request getCategoriesRequest = - new GetCategoriesAction.Request(jobId); + GetCategoriesAction.Request getCategoriesRequest = new GetCategoriesAction.Request(jobId); getCategoriesRequest.setPageParams(new PageParams()); GetCategoriesAction.Response categoriesResponse = client().execute(GetCategoriesAction.INSTANCE, getCategoriesRequest).actionGet(); return categoriesResponse.getResult().results(); @@ -245,16 +247,17 @@ protected void waitForecastToFinish(String jobId, String forecastId) throws Exce waitForecastStatus(inFipsJvm() ? 300 : 60, jobId, forecastId, ForecastRequestStats.ForecastRequestStatus.FINISHED); } - protected void waitForecastStatus(String jobId, - String forecastId, - ForecastRequestStats.ForecastRequestStatus... status) throws Exception { + protected void waitForecastStatus(String jobId, String forecastId, ForecastRequestStats.ForecastRequestStatus... status) + throws Exception { waitForecastStatus(30, jobId, forecastId, status); } - protected void waitForecastStatus(int maxWaitTimeSeconds, - String jobId, - String forecastId, - ForecastRequestStats.ForecastRequestStatus... status) throws Exception { + protected void waitForecastStatus( + int maxWaitTimeSeconds, + String jobId, + String forecastId, + ForecastRequestStats.ForecastRequestStatus... status + ) throws Exception { assertBusy(() -> { ForecastRequestStats forecastRequestStats = getForecastStats(jobId, forecastId); assertThat(forecastRequestStats, is(notNullValue())); @@ -264,13 +267,16 @@ protected void waitForecastStatus(int maxWaitTimeSeconds, protected void assertThatNumberOfAnnotationsIsEqualTo(int expectedNumberOfAnnotations) throws IOException { // Refresh the annotations index so that recently indexed annotation docs are visible. - client().admin().indices().prepareRefresh(AnnotationIndex.LATEST_INDEX_NAME) + client().admin() + .indices() + .prepareRefresh(AnnotationIndex.LATEST_INDEX_NAME) .setIndicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN_CLOSED_HIDDEN) .execute() .actionGet(); - SearchRequest searchRequest = - new SearchRequest(AnnotationIndex.READ_ALIAS_NAME).indicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN_CLOSED_HIDDEN); + SearchRequest searchRequest = new SearchRequest(AnnotationIndex.READ_ALIAS_NAME).indicesOptions( + IndicesOptions.LENIENT_EXPAND_OPEN_CLOSED_HIDDEN + ); SearchResponse searchResponse = client().search(searchRequest).actionGet(); List annotations = new ArrayList<>(); for (SearchHit hit : searchResponse.getHits().getHits()) { @@ -292,9 +298,14 @@ protected ForecastRequestStats getForecastStats(String jobId, String forecastId) assertThat(searchResponse.getHits().getHits().length, equalTo(1)); - try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser( - NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, - searchResponse.getHits().getHits()[0].getSourceRef().streamInput())) { + try ( + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + searchResponse.getHits().getHits()[0].getSourceRef().streamInput() + ) + ) { return ForecastRequestStats.STRICT_PARSER.apply(parser, null); } catch (IOException e) { throw new IllegalStateException(e); @@ -305,15 +316,22 @@ protected List getForecastStats() { List forecastStats = new ArrayList<>(); SearchResponse searchResponse = client().prepareSearch(AnomalyDetectorsIndex.jobResultsIndexPrefix() + "*") - .setSize(1000) - .setQuery(QueryBuilders.boolQuery() - .filter(QueryBuilders.termQuery(Result.RESULT_TYPE.getPreferredName(), ForecastRequestStats.RESULT_TYPE_VALUE))) - .execute().actionGet(); + .setSize(1000) + .setQuery( + QueryBuilders.boolQuery() + .filter(QueryBuilders.termQuery(Result.RESULT_TYPE.getPreferredName(), ForecastRequestStats.RESULT_TYPE_VALUE)) + ) + .execute() + .actionGet(); SearchHits hits = searchResponse.getHits(); for (SearchHit hit : hits) { try { - XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser( - NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, hit.getSourceRef().streamInput()); + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + hit.getSourceRef().streamInput() + ); forecastStats.add(ForecastRequestStats.STRICT_PARSER.apply(parser, null)); } catch (IOException e) { throw new IllegalStateException(e); @@ -324,30 +342,39 @@ protected List getForecastStats() { protected long countForecastDocs(String jobId, String forecastId) { SearchResponse searchResponse = client().prepareSearch(AnomalyDetectorsIndex.jobResultsIndexPrefix() + "*") - .setQuery(QueryBuilders.boolQuery() - .filter(QueryBuilders.termQuery(Result.RESULT_TYPE.getPreferredName(), Forecast.RESULT_TYPE_VALUE)) - .filter(QueryBuilders.termQuery(Job.ID.getPreferredName(), jobId)) - .filter(QueryBuilders.termQuery(Forecast.FORECAST_ID.getPreferredName(), forecastId))) - .execute().actionGet(); + .setQuery( + QueryBuilders.boolQuery() + .filter(QueryBuilders.termQuery(Result.RESULT_TYPE.getPreferredName(), Forecast.RESULT_TYPE_VALUE)) + .filter(QueryBuilders.termQuery(Job.ID.getPreferredName(), jobId)) + .filter(QueryBuilders.termQuery(Forecast.FORECAST_ID.getPreferredName(), forecastId)) + ) + .execute() + .actionGet(); return searchResponse.getHits().getTotalHits().value; } protected List getForecasts(String jobId, ForecastRequestStats forecastRequestStats) { List forecasts = new ArrayList<>(); SearchResponse searchResponse = client().prepareSearch(AnomalyDetectorsIndex.jobResultsIndexPrefix() + "*") - .setSize((int) forecastRequestStats.getRecordCount()) - .setQuery(QueryBuilders.boolQuery() - .filter(QueryBuilders.termQuery(Result.RESULT_TYPE.getPreferredName(), Forecast.RESULT_TYPE_VALUE)) - .filter(QueryBuilders.termQuery(Job.ID.getPreferredName(), jobId)) - .filter(QueryBuilders.termQuery(Forecast.FORECAST_ID.getPreferredName(), forecastRequestStats.getForecastId()))) - .addSort(SortBuilders.fieldSort(Result.TIMESTAMP.getPreferredName()).order(SortOrder.ASC)) - .execute().actionGet(); + .setSize((int) forecastRequestStats.getRecordCount()) + .setQuery( + QueryBuilders.boolQuery() + .filter(QueryBuilders.termQuery(Result.RESULT_TYPE.getPreferredName(), Forecast.RESULT_TYPE_VALUE)) + .filter(QueryBuilders.termQuery(Job.ID.getPreferredName(), jobId)) + .filter(QueryBuilders.termQuery(Forecast.FORECAST_ID.getPreferredName(), forecastRequestStats.getForecastId())) + ) + .addSort(SortBuilders.fieldSort(Result.TIMESTAMP.getPreferredName()).order(SortOrder.ASC)) + .execute() + .actionGet(); SearchHits hits = searchResponse.getHits(); for (SearchHit hit : hits) { try { - XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser( - NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, - hit.getSourceRef().streamInput()); + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + hit.getSourceRef().streamInput() + ); forecasts.add(Forecast.STRICT_PARSER.apply(parser, null)); } catch (IOException e) { throw new IllegalStateException(e); @@ -371,8 +398,12 @@ protected PersistJobAction.Response persistJob(String jobId) { return client().execute(PersistJobAction.INSTANCE, request).actionGet(); } - protected List generateData(long timestamp, TimeValue bucketSpan, int bucketCount, - Function timeToCountFunction) throws IOException { + protected List generateData( + long timestamp, + TimeValue bucketSpan, + int bucketCount, + Function timeToCountFunction + ) throws IOException { List data = new ArrayList<>(); long now = timestamp; for (int bucketIndex = 0; bucketIndex < bucketCount; bucketIndex++) { diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlNativeDataFrameAnalyticsIntegTestCase.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlNativeDataFrameAnalyticsIntegTestCase.java index f3007a1b9956f..c57e41537f722 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlNativeDataFrameAnalyticsIntegTestCase.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlNativeDataFrameAnalyticsIntegTestCase.java @@ -10,8 +10,8 @@ import org.elasticsearch.action.admin.indices.get.GetIndexRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.cluster.ClusterState; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; @@ -129,34 +129,45 @@ protected ExplainDataFrameAnalyticsAction.Response explainDataFrame(DataFrameAna } protected EvaluateDataFrameAction.Response evaluateDataFrame(String index, Evaluation evaluation) { - EvaluateDataFrameAction.Request request = - new EvaluateDataFrameAction.Request() - .setIndices(List.of(index)) - .setEvaluation(evaluation); + EvaluateDataFrameAction.Request request = new EvaluateDataFrameAction.Request().setIndices(List.of(index)) + .setEvaluation(evaluation); return client().execute(EvaluateDataFrameAction.INSTANCE, request).actionGet(); } protected PreviewDataFrameAnalyticsAction.Response previewDataFrame(String id) { List analytics = getAnalytics(id); assertThat(analytics, hasSize(1)); - return client().execute( - PreviewDataFrameAnalyticsAction.INSTANCE, - new PreviewDataFrameAnalyticsAction.Request(analytics.get(0)) - ).actionGet(); + return client().execute(PreviewDataFrameAnalyticsAction.INSTANCE, new PreviewDataFrameAnalyticsAction.Request(analytics.get(0))) + .actionGet(); } - static DataFrameAnalyticsConfig buildAnalytics(String id, String sourceIndex, String destIndex, - @Nullable String resultsField, DataFrameAnalysis analysis) throws Exception { + static DataFrameAnalyticsConfig buildAnalytics( + String id, + String sourceIndex, + String destIndex, + @Nullable String resultsField, + DataFrameAnalysis analysis + ) throws Exception { return buildAnalytics(id, sourceIndex, destIndex, resultsField, analysis, QueryBuilders.matchAllQuery()); } - protected static DataFrameAnalyticsConfig buildAnalytics(String id, String sourceIndex, String destIndex, - @Nullable String resultsField, DataFrameAnalysis analysis, - QueryBuilder queryBuilder) throws Exception { - return new DataFrameAnalyticsConfig.Builder() - .setId(id) - .setSource(new DataFrameAnalyticsSource( - new String[] { sourceIndex }, QueryProvider.fromParsedQuery(queryBuilder), null, Collections.emptyMap())) + protected static DataFrameAnalyticsConfig buildAnalytics( + String id, + String sourceIndex, + String destIndex, + @Nullable String resultsField, + DataFrameAnalysis analysis, + QueryBuilder queryBuilder + ) throws Exception { + return new DataFrameAnalyticsConfig.Builder().setId(id) + .setSource( + new DataFrameAnalyticsSource( + new String[] { sourceIndex }, + QueryProvider.fromParsedQuery(queryBuilder), + null, + Collections.emptyMap() + ) + ) .setDest(new DataFrameAnalyticsDest(destIndex, resultsField)) .setAnalysis(analysis) .build(); @@ -176,14 +187,20 @@ protected void assertIsFailed(String id) { protected void assertProgressIsZero(String id) { List progress = getProgress(id); - assertThat("progress is not all zero: " + progress, - progress.stream().allMatch(phaseProgress -> phaseProgress.getProgressPercent() == 0), is(true)); + assertThat( + "progress is not all zero: " + progress, + progress.stream().allMatch(phaseProgress -> phaseProgress.getProgressPercent() == 0), + is(true) + ); } protected void assertProgressComplete(String id) { List progress = getProgress(id); - assertThat("progress is complete: " + progress, - progress.stream().allMatch(phaseProgress -> phaseProgress.getProgressPercent() == 100), is(true)); + assertThat( + "progress is complete: " + progress, + progress.stream().allMatch(phaseProgress -> phaseProgress.getProgressPercent() == 100), + is(true) + ); } abstract boolean supportsInference(); @@ -207,9 +224,7 @@ private List getProgress(String id) { protected SearchResponse searchStoredProgress(String jobId) { String docId = StoredProgress.documentId(jobId); - return client().prepareSearch(AnomalyDetectorsIndex.jobStateIndexPattern()) - .setQuery(QueryBuilders.idsQuery().addIds(docId)) - .get(); + return client().prepareSearch(AnomalyDetectorsIndex.jobStateIndexPattern()).setQuery(QueryBuilders.idsQuery().addIds(docId)).get(); } protected void assertExactlyOneInferenceModelPersisted(String jobId) { @@ -226,8 +241,11 @@ private void assertInferenceModelPersisted(String jobId, Matcher> analyticsTaskList() { @@ -250,6 +268,7 @@ protected void waitUntilSomeProgressHasBeenMadeForPhase(String jobId, String pha assertThat(phaseProgress.get().getProgressPercent(), greaterThan(1)); }, 60, TimeUnit.SECONDS); } + /** * Asserts whether the audit messages fetched from index match provided prefixes. * More specifically, in order to pass: @@ -299,32 +318,39 @@ protected static void assertModelStatePersisted(String stateDocId) { } protected static void assertMlResultsFieldMappings(String index, String predictedClassField, String expectedType) { - Map mappings = - client() - .execute(GetIndexAction.INSTANCE, new GetIndexRequest().indices(index)) - .actionGet() - .mappings() - .get(index) - .sourceAsMap(); + Map mappings = client().execute(GetIndexAction.INSTANCE, new GetIndexRequest().indices(index)) + .actionGet() + .mappings() + .get(index) + .sourceAsMap(); assertThat( mappings.toString(), getFieldValue( mappings, - "properties", "ml", "properties", String.join(".properties.", predictedClassField.split("\\.")), "type"), - equalTo(expectedType)); + "properties", + "ml", + "properties", + String.join(".properties.", predictedClassField.split("\\.")), + "type" + ), + equalTo(expectedType) + ); if (getFieldValue(mappings, "properties", "ml", "properties", "top_classes") != null) { assertThat( mappings.toString(), getFieldValue(mappings, "properties", "ml", "properties", "top_classes", "type"), - equalTo("nested")); + equalTo("nested") + ); assertThat( mappings.toString(), getFieldValue(mappings, "properties", "ml", "properties", "top_classes", "properties", "class_name", "type"), - equalTo(expectedType)); + equalTo(expectedType) + ); assertThat( mappings.toString(), getFieldValue(mappings, "properties", "ml", "properties", "top_classes", "properties", "class_probability", "type"), - equalTo("double")); + equalTo("double") + ); } } diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlNativeIntegTestCase.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlNativeIntegTestCase.java index 0d12b24db03d9..fabd338e1c17b 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlNativeIntegTestCase.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlNativeIntegTestCase.java @@ -25,19 +25,18 @@ import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.cluster.metadata.Template; import org.elasticsearch.common.compress.CompressedXContent; -import org.elasticsearch.core.PathUtils; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.network.NetworkModule; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.core.PathUtils; import org.elasticsearch.env.Environment; import org.elasticsearch.index.query.QueryBuilders; -import org.elasticsearch.reindex.ReindexPlugin; import org.elasticsearch.ingest.common.IngestCommonPlugin; import org.elasticsearch.license.LicenseService; import org.elasticsearch.persistent.PersistentTaskParams; import org.elasticsearch.persistent.PersistentTaskState; import org.elasticsearch.plugins.Plugin; +import org.elasticsearch.reindex.ReindexPlugin; import org.elasticsearch.script.IngestScript; import org.elasticsearch.script.MockDeterministicScript; import org.elasticsearch.script.MockScriptEngine; @@ -50,6 +49,7 @@ import org.elasticsearch.test.ESIntegTestCase; import org.elasticsearch.test.SecuritySettingsSourceField; import org.elasticsearch.transport.netty4.Netty4Plugin; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.autoscaling.Autoscaling; import org.elasticsearch.xpack.autoscaling.AutoscalingMetadata; import org.elasticsearch.xpack.autoscaling.capacity.AutoscalingDeciderResult; @@ -145,19 +145,23 @@ protected Collection> nodePlugins() { IndexLifecycle.class, // The feature reset API touches transform custom cluster state so we need this plugin to understand it Transform.class, - DataStreamsPlugin.class); + DataStreamsPlugin.class + ); } @Override protected Function getClientWrapper() { - final Map headers = - Map.of("Authorization", basicAuthHeaderValue("x_pack_rest_user", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)); + final Map headers = Map.of( + "Authorization", + basicAuthHeaderValue("x_pack_rest_user", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING) + ); // we need to wrap node clients because we do not specify a user for nodes and all requests will use the system // user. This is ok for internal n2n stuff but the test framework does other things like wiping indices, repositories, etc // that the system user cannot do. so we wrap the node client with a user that can do these things since the client() calls // return a node client return client -> client.filterWithHeader(headers); } + @Override protected Settings externalClusterClientSettings() { final Path home = createTempDir(); @@ -207,23 +211,25 @@ protected void cleanUp() { @Override protected Set excludeTemplates() { - return new HashSet<>(Arrays.asList( - NotificationsIndex.NOTIFICATIONS_INDEX, - MlMetaIndex.indexName(), - AnomalyDetectorsIndexFields.STATE_INDEX_PREFIX, - AnomalyDetectorsIndex.jobResultsIndexPrefix(), - InferenceIndexConstants.LATEST_INDEX_NAME, - SnapshotLifecycleTemplateRegistry.SLM_TEMPLATE_NAME - )); + return new HashSet<>( + Arrays.asList( + NotificationsIndex.NOTIFICATIONS_INDEX, + MlMetaIndex.indexName(), + AnomalyDetectorsIndexFields.STATE_INDEX_PREFIX, + AnomalyDetectorsIndex.jobResultsIndexPrefix(), + InferenceIndexConstants.LATEST_INDEX_NAME, + SnapshotLifecycleTemplateRegistry.SLM_TEMPLATE_NAME + ) + ); } - protected void cleanUpResources(){ + protected void cleanUpResources() { client().execute(ResetFeatureStateAction.INSTANCE, new ResetFeatureStateRequest()).actionGet(); } protected void setUpgradeModeTo(boolean enabled) { - AcknowledgedResponse response = - client().execute(SetUpgradeModeAction.INSTANCE, new SetUpgradeModeAction.Request(enabled)).actionGet(); + AcknowledgedResponse response = client().execute(SetUpgradeModeAction.INSTANCE, new SetUpgradeModeAction.Request(enabled)) + .actionGet(); assertThat(response.isAcknowledged(), is(true)); assertThat(upgradeMode(), is(enabled)); } @@ -235,8 +241,10 @@ protected boolean upgradeMode() { } protected DeleteExpiredDataAction.Response deleteExpiredData() throws Exception { - DeleteExpiredDataAction.Response response = client().execute(DeleteExpiredDataAction.INSTANCE, - new DeleteExpiredDataAction.Request()).get(); + DeleteExpiredDataAction.Response response = client().execute( + DeleteExpiredDataAction.INSTANCE, + new DeleteExpiredDataAction.Request() + ).get(); // We need to refresh to ensure the deletion is visible refresh("*"); @@ -263,12 +271,9 @@ protected static List fetchAllAuditMessages(String jobId) { RefreshResponse refreshResponse = client().execute(RefreshAction.INSTANCE, refreshRequest).actionGet(); assertThat(refreshResponse.getStatus().getStatus(), anyOf(equalTo(200), equalTo(201))); - SearchRequest searchRequest = new SearchRequestBuilder(client(), SearchAction.INSTANCE) - .setIndices(NotificationsIndex.NOTIFICATIONS_INDEX) - .addSort("timestamp", SortOrder.ASC) - .setQuery(QueryBuilders.termQuery("job_id", jobId)) - .setSize(100) - .request(); + SearchRequest searchRequest = new SearchRequestBuilder(client(), SearchAction.INSTANCE).setIndices( + NotificationsIndex.NOTIFICATIONS_INDEX + ).addSort("timestamp", SortOrder.ASC).setQuery(QueryBuilders.termQuery("job_id", jobId)).setSize(100).request(); SearchResponse searchResponse = client().execute(SearchAction.INSTANCE, searchRequest).actionGet(); return Arrays.stream(searchResponse.getHits().getHits()) @@ -299,30 +304,51 @@ protected void ensureClusterStateConsistency() throws IOException { entries.add(new NamedWriteableRegistry.Entry(NamedDiff.class, ModelAliasMetadata.NAME, ModelAliasMetadata::readDiffFrom)); entries.add(new NamedWriteableRegistry.Entry(Metadata.Custom.class, "ml", MlMetadata::new)); entries.add(new NamedWriteableRegistry.Entry(Metadata.Custom.class, IndexLifecycleMetadata.TYPE, IndexLifecycleMetadata::new)); - entries.add(new NamedWriteableRegistry.Entry(LifecycleType.class, TimeseriesLifecycleType.TYPE, - (in) -> TimeseriesLifecycleType.INSTANCE)); + entries.add( + new NamedWriteableRegistry.Entry( + LifecycleType.class, + TimeseriesLifecycleType.TYPE, + (in) -> TimeseriesLifecycleType.INSTANCE + ) + ); entries.add(new NamedWriteableRegistry.Entry(LifecycleAction.class, DeleteAction.NAME, DeleteAction::new)); entries.add(new NamedWriteableRegistry.Entry(LifecycleAction.class, RolloverAction.NAME, RolloverAction::new)); - entries.add(new NamedWriteableRegistry.Entry(PersistentTaskParams.class, MlTasks.DATAFEED_TASK_NAME, - StartDatafeedAction.DatafeedParams::new)); - entries.add(new NamedWriteableRegistry.Entry(PersistentTaskParams.class, MlTasks.DATA_FRAME_ANALYTICS_TASK_NAME, - StartDataFrameAnalyticsAction.TaskParams::new)); - entries.add(new NamedWriteableRegistry.Entry(PersistentTaskParams.class, MlTasks.JOB_TASK_NAME, - OpenJobAction.JobParams::new)); + entries.add( + new NamedWriteableRegistry.Entry( + PersistentTaskParams.class, + MlTasks.DATAFEED_TASK_NAME, + StartDatafeedAction.DatafeedParams::new + ) + ); + entries.add( + new NamedWriteableRegistry.Entry( + PersistentTaskParams.class, + MlTasks.DATA_FRAME_ANALYTICS_TASK_NAME, + StartDataFrameAnalyticsAction.TaskParams::new + ) + ); + entries.add(new NamedWriteableRegistry.Entry(PersistentTaskParams.class, MlTasks.JOB_TASK_NAME, OpenJobAction.JobParams::new)); entries.add(new NamedWriteableRegistry.Entry(PersistentTaskState.class, JobTaskState.NAME, JobTaskState::new)); entries.add(new NamedWriteableRegistry.Entry(PersistentTaskState.class, DatafeedState.NAME, DatafeedState::fromStream)); - entries.add(new NamedWriteableRegistry.Entry(PersistentTaskState.class, DataFrameAnalyticsTaskState.NAME, - DataFrameAnalyticsTaskState::new)); + entries.add( + new NamedWriteableRegistry.Entry( + PersistentTaskState.class, + DataFrameAnalyticsTaskState.NAME, + DataFrameAnalyticsTaskState::new + ) + ); entries.add(new NamedWriteableRegistry.Entry(ClusterState.Custom.class, TokenMetadata.TYPE, TokenMetadata::new)); entries.add(new NamedWriteableRegistry.Entry(Metadata.Custom.class, AutoscalingMetadata.NAME, AutoscalingMetadata::new)); - entries.add(new NamedWriteableRegistry.Entry(NamedDiff.class, - AutoscalingMetadata.NAME, - AutoscalingMetadata.AutoscalingMetadataDiff::new)); - entries.add(new NamedWriteableRegistry.Entry( - AutoscalingDeciderResult.Reason.class, - MlScalingReason.NAME, - MlScalingReason::new - )); + entries.add( + new NamedWriteableRegistry.Entry( + NamedDiff.class, + AutoscalingMetadata.NAME, + AutoscalingMetadata.AutoscalingMetadataDiff::new + ) + ); + entries.add( + new NamedWriteableRegistry.Entry(AutoscalingDeciderResult.Reason.class, MlScalingReason.NAME, MlScalingReason::new) + ); final NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(entries); ClusterState masterClusterState = client().admin().cluster().prepareState().all().get().getState(); byte[] masterClusterStateBytes = ClusterState.Builder.toBytes(masterClusterState); @@ -340,19 +366,24 @@ protected void ensureClusterStateConsistency() throws IOException { final int localClusterStateSize = ClusterState.Builder.toBytes(localClusterState).length; // Check that the non-master node has the same version of the cluster state as the master and // that the master node matches the master (otherwise there is no requirement for the cluster state to match) - if (masterClusterState.version() == localClusterState.version() && - masterId.equals(localClusterState.nodes().getMasterNodeId())) { + if (masterClusterState.version() == localClusterState.version() + && masterId.equals(localClusterState.nodes().getMasterNodeId())) { try { assertEquals("clusterstate UUID does not match", masterClusterState.stateUUID(), localClusterState.stateUUID()); // We cannot compare serialization bytes since serialization order of maps is not guaranteed // but we can compare serialization sizes - they should be the same assertEquals("clusterstate size does not match", masterClusterStateSize, localClusterStateSize); // Compare JSON serialization - assertNull("clusterstate JSON serialization does not match", - differenceBetweenMapsIgnoringArrayOrder(masterStateMap, localStateMap)); + assertNull( + "clusterstate JSON serialization does not match", + differenceBetweenMapsIgnoringArrayOrder(masterStateMap, localStateMap) + ); } catch (AssertionError error) { - logger.error("Cluster state from master:\n{}\nLocal cluster state:\n{}", - masterClusterState.toString(), localClusterState.toString()); + logger.error( + "Cluster state from master:\n{}\nLocal cluster state:\n{}", + masterClusterState.toString(), + localClusterState.toString() + ); throw error; } } @@ -361,24 +392,28 @@ protected void ensureClusterStateConsistency() throws IOException { } protected static void createDataStreamAndTemplate(String dataStreamName, String mapping) throws IOException { - client().execute(PutComposableIndexTemplateAction.INSTANCE, - new PutComposableIndexTemplateAction.Request(dataStreamName + "_template") - .indexTemplate(new ComposableIndexTemplate(Collections.singletonList(dataStreamName), + client().execute( + PutComposableIndexTemplateAction.INSTANCE, + new PutComposableIndexTemplateAction.Request(dataStreamName + "_template").indexTemplate( + new ComposableIndexTemplate( + Collections.singletonList(dataStreamName), new Template(null, new CompressedXContent(mapping), null), null, null, null, null, new ComposableIndexTemplate.DataStreamTemplate(), - null))) - .actionGet(); + null + ) + ) + ).actionGet(); client().execute(CreateDataStreamAction.INSTANCE, new CreateDataStreamAction.Request(dataStreamName)).actionGet(); } protected static void deleteAllDataStreams() { AcknowledgedResponse response = client().execute( DeleteDataStreamAction.INSTANCE, - new DeleteDataStreamAction.Request(new String[]{"*"}) + new DeleteDataStreamAction.Request(new String[] { "*" }) ).actionGet(); assertAcked(response); } @@ -412,8 +447,7 @@ public T compile(String name, String script, ScriptContext context, Map new IngestScript(vars) { @Override - public void execute(Map ctx) { - } + public void execute(Map ctx) {} }; return context.factoryClazz.cast(factory); } diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ModelPlotsIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ModelPlotsIT.java index 4cc9a039540a1..f93dbdf1c1f2e 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ModelPlotsIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ModelPlotsIT.java @@ -40,9 +40,11 @@ public class ModelPlotsIT extends MlNativeAutodetectIntegTestCase { @Before public void setUpData() { - client().admin().indices().prepareCreate(DATA_INDEX) - .setMapping("time", "type=date,format=epoch_millis", "user", "type=keyword") - .get(); + client().admin() + .indices() + .prepareCreate(DATA_INDEX) + .setMapping("time", "type=date,format=epoch_millis", "user", "type=keyword") + .get(); List users = Arrays.asList("user_1", "user_2", "user_3"); @@ -59,9 +61,7 @@ public void setUpData() { } } - BulkResponse bulkResponse = bulkRequestBuilder - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .get(); + BulkResponse bulkResponse = bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).get(); assertThat(bulkResponse.hasFailures(), is(false)); } @@ -164,9 +164,9 @@ private static DatafeedConfig newDatafeed(String datafeedId, String jobId) { private Set modelPlotTerms(String jobId, String fieldName) { SearchResponse searchResponse = client().prepareSearch(".ml-anomalies-" + jobId) - .setQuery(QueryBuilders.termQuery("result_type", "model_plot")) - .addAggregation(AggregationBuilders.terms("model_plot_terms").field(fieldName)) - .get(); + .setQuery(QueryBuilders.termQuery("result_type", "model_plot")) + .addAggregation(AggregationBuilders.terms("model_plot_terms").field(fieldName)) + .get(); Terms aggregation = searchResponse.getAggregations().get("model_plot_terms"); return aggregation.getBuckets().stream().map(agg -> agg.getKeyAsString()).collect(Collectors.toSet()); diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ModelSnapshotRetentionIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ModelSnapshotRetentionIT.java index b028f117827e6..1b12eae165c29 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ModelSnapshotRetentionIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ModelSnapshotRetentionIT.java @@ -22,14 +22,14 @@ import org.elasticsearch.action.support.master.MasterNodeRequest; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.indices.TestIndexNameExpressionResolver; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.builder.SearchSourceBuilder; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.ml.action.PutJobAction; import org.elasticsearch.xpack.core.ml.action.UpdateJobAction; import org.elasticsearch.xpack.core.ml.job.config.AnalysisConfig; @@ -69,8 +69,13 @@ public class ModelSnapshotRetentionIT extends MlNativeAutodetectIntegTestCase { @Before public void addMlState() { PlainActionFuture future = new PlainActionFuture<>(); - createStateIndexAndAliasIfNecessary(client(), ClusterState.EMPTY_STATE, TestIndexNameExpressionResolver.newInstance(), - MasterNodeRequest.DEFAULT_MASTER_NODE_TIMEOUT, future); + createStateIndexAndAliasIfNecessary( + client(), + ClusterState.EMPTY_STATE, + TestIndexNameExpressionResolver.newInstance(), + MasterNodeRequest.DEFAULT_MASTER_NODE_TIMEOUT, + future + ); future.actionGet(); } @@ -146,9 +151,9 @@ public void testModelSnapshotRetentionWithDailyThinning() throws Exception { // - Nothing older than modelSnapshotRetentionDays // - Everything newer than dailyModelSnapshotRetentionAfterDays // - The first snapshot of each day in between - if (timeMs >= now - MS_IN_DAY * modelSnapshotRetentionDays && - (timeMs >= now - MS_IN_DAY * dailyModelSnapshotRetentionAfterDays || - (now - timeMs) % MS_IN_DAY < MS_IN_DAY / numSnapshotsPerDay)) { + if (timeMs >= now - MS_IN_DAY * modelSnapshotRetentionDays + && (timeMs >= now - MS_IN_DAY * dailyModelSnapshotRetentionAfterDays + || (now - timeMs) % MS_IN_DAY < MS_IN_DAY / numSnapshotsPerDay)) { expectedModelSnapshotDocIds.add(ModelSnapshot.documentId(jobId, snapshotId)); for (int j = 1; j <= numDocsPerSnapshot; ++j) { expectedModelStateDocIds.add(ModelState.documentId(jobId, snapshotId, j)); @@ -221,14 +226,14 @@ private void createModelSnapshot(String jobId, String snapshotId, Date timestamp } } - private void persistModelSnapshotDoc(String jobId, String snapshotId, Date timestamp, int numDocs, - boolean immediateRefresh) throws IOException { + private void persistModelSnapshotDoc(String jobId, String snapshotId, Date timestamp, int numDocs, boolean immediateRefresh) + throws IOException { ModelSnapshot.Builder modelSnapshotBuilder = new ModelSnapshot.Builder(); modelSnapshotBuilder.setJobId(jobId).setSnapshotId(snapshotId).setTimestamp(timestamp).setSnapshotDocCount(numDocs); - IndexRequest indexRequest = new IndexRequest(AnomalyDetectorsIndex.resultsWriteAlias(jobId)) - .id(ModelSnapshot.documentId(jobId, snapshotId)) - .setRequireAlias(true); + IndexRequest indexRequest = new IndexRequest(AnomalyDetectorsIndex.resultsWriteAlias(jobId)).id( + ModelSnapshot.documentId(jobId, snapshotId) + ).setRequireAlias(true); if (immediateRefresh) { indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); } @@ -245,8 +250,9 @@ private void persistModelStateDocs(String jobId, String snapshotId, int numDocs) BulkRequest bulkRequest = new BulkRequest(); for (int i = 1; i <= numDocs; ++i) { - IndexRequest indexRequest = new IndexRequest(AnomalyDetectorsIndex.jobStateIndexWriteAlias()) - .id(ModelState.documentId(jobId, snapshotId, i)) + IndexRequest indexRequest = new IndexRequest(AnomalyDetectorsIndex.jobStateIndexWriteAlias()).id( + ModelState.documentId(jobId, snapshotId, i) + ) // The exact contents of the model state doesn't matter - we are not going to try and restore it .source(Collections.singletonMap("compressed", Collections.singletonList("foo"))) .setRequireAlias(true); diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/OutlierDetectionEvaluationIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/OutlierDetectionEvaluationIT.java index 9c3638ce51624..3356604a16f84 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/OutlierDetectionEvaluationIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/OutlierDetectionEvaluationIT.java @@ -45,9 +45,10 @@ public void cleanup() { } public void testEvaluate_DefaultMetrics() { - EvaluateDataFrameAction.Response evaluateDataFrameResponse = - evaluateDataFrame( - ANIMALS_DATA_INDEX, new OutlierDetection(IS_PREDATOR_BOOLEAN_FIELD, IS_PREDATOR_PREDICTION_PROBABILITY_FIELD, null)); + EvaluateDataFrameAction.Response evaluateDataFrameResponse = evaluateDataFrame( + ANIMALS_DATA_INDEX, + new OutlierDetection(IS_PREDATOR_BOOLEAN_FIELD, IS_PREDATOR_PREDICTION_PROBABILITY_FIELD, null) + ); assertThat(evaluateDataFrameResponse.getEvaluationName(), equalTo(OutlierDetection.NAME.getPreferredName())); assertThat( @@ -56,21 +57,20 @@ public void testEvaluate_DefaultMetrics() { AucRoc.NAME.getPreferredName(), Precision.NAME.getPreferredName(), Recall.NAME.getPreferredName(), - ConfusionMatrix.NAME.getPreferredName())); + ConfusionMatrix.NAME.getPreferredName() + ) + ); } public void testEvaluate_AllMetrics() { - EvaluateDataFrameAction.Response evaluateDataFrameResponse = - evaluateDataFrame( - ANIMALS_DATA_INDEX, - new OutlierDetection( - IS_PREDATOR_BOOLEAN_FIELD, - IS_PREDATOR_PREDICTION_PROBABILITY_FIELD, - List.of( - new AucRoc(false), - new Precision(List.of(0.5)), - new Recall(List.of(0.5)), - new ConfusionMatrix(List.of(0.5))))); + EvaluateDataFrameAction.Response evaluateDataFrameResponse = evaluateDataFrame( + ANIMALS_DATA_INDEX, + new OutlierDetection( + IS_PREDATOR_BOOLEAN_FIELD, + IS_PREDATOR_PREDICTION_PROBABILITY_FIELD, + List.of(new AucRoc(false), new Precision(List.of(0.5)), new Recall(List.of(0.5)), new ConfusionMatrix(List.of(0.5))) + ) + ); assertThat(evaluateDataFrameResponse.getEvaluationName(), equalTo(OutlierDetection.NAME.getPreferredName())); assertThat( @@ -79,14 +79,16 @@ public void testEvaluate_AllMetrics() { AucRoc.NAME.getPreferredName(), Precision.NAME.getPreferredName(), Recall.NAME.getPreferredName(), - ConfusionMatrix.NAME.getPreferredName())); + ConfusionMatrix.NAME.getPreferredName() + ) + ); } private AucRoc.Result evaluateAucRoc(String actualField, String predictedField, boolean includeCurve) { - EvaluateDataFrameAction.Response evaluateDataFrameResponse = - evaluateDataFrame( - ANIMALS_DATA_INDEX, - new OutlierDetection(actualField, predictedField, List.of(new AucRoc(includeCurve)))); + EvaluateDataFrameAction.Response evaluateDataFrameResponse = evaluateDataFrame( + ANIMALS_DATA_INDEX, + new OutlierDetection(actualField, predictedField, List.of(new AucRoc(includeCurve))) + ); assertThat(evaluateDataFrameResponse.getEvaluationName(), equalTo(OutlierDetection.NAME.getPreferredName())); assertThat(evaluateDataFrameResponse.getMetrics(), hasSize(1)); diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/OutlierDetectionWithMissingFieldsIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/OutlierDetectionWithMissingFieldsIT.java index 9f8db62b5a212..2265ad8e934ce 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/OutlierDetectionWithMissingFieldsIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/OutlierDetectionWithMissingFieldsIT.java @@ -36,9 +36,7 @@ public void cleanup() { public void testMissingFields() throws Exception { String sourceIndex = "test-outlier-detection-with-missing-fields"; - client().admin().indices().prepareCreate(sourceIndex) - .setMapping("numeric", "type=double", "categorical", "type=keyword") - .get(); + client().admin().indices().prepareCreate(sourceIndex).setMapping("numeric", "type=double", "categorical", "type=keyword").get(); BulkRequestBuilder bulkRequestBuilder = client().prepareBulk(); bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); @@ -60,7 +58,7 @@ public void testMissingFields() throws Exception { // Add a doc with numeric being array which is also treated as missing { IndexRequest arrayIndexRequest = new IndexRequest(sourceIndex); - arrayIndexRequest.source("numeric", new double[]{1.0, 2.0}, "categorical", "foo"); + arrayIndexRequest.source("numeric", new double[] { 1.0, 2.0 }, "categorical", "foo"); bulkRequestBuilder.add(arrayIndexRequest); } @@ -70,8 +68,13 @@ public void testMissingFields() throws Exception { } String id = "test_outlier_detection_with_missing_fields"; - DataFrameAnalyticsConfig config = buildAnalytics(id, sourceIndex, sourceIndex + "-results", null, - new OutlierDetection.Builder().build()); + DataFrameAnalyticsConfig config = buildAnalytics( + id, + sourceIndex, + sourceIndex + "-results", + null, + new OutlierDetection.Builder().build() + ); putAnalytics(config); assertIsStopped(id); diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/OverallBucketsIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/OverallBucketsIT.java index 5d8c4b7903e90..0558b7f6f2fb7 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/OverallBucketsIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/OverallBucketsIT.java @@ -7,9 +7,9 @@ package org.elasticsearch.xpack.ml.integration; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.xpack.core.action.util.PageParams; import org.elasticsearch.xpack.core.ml.action.GetBucketsAction; import org.elasticsearch.xpack.core.ml.action.GetOverallBucketsAction; -import org.elasticsearch.xpack.core.action.util.PageParams; import org.elasticsearch.xpack.core.ml.job.config.AnalysisConfig; import org.elasticsearch.xpack.core.ml.job.config.DataDescription; import org.elasticsearch.xpack.core.ml.job.config.Detector; @@ -41,7 +41,8 @@ public void cleanUpTest() { public void test() throws Exception { AnalysisConfig.Builder analysisConfig = new AnalysisConfig.Builder( - Collections.singletonList(new Detector.Builder("count", null).build())); + Collections.singletonList(new Detector.Builder("count", null).build()) + ); analysisConfig.setBucketSpan(TimeValue.timeValueSeconds(BUCKET_SPAN_SECONDS)); DataDescription.Builder dataDescription = new DataDescription.Builder(); dataDescription.setTimeFormat("epoch"); @@ -76,7 +77,9 @@ public void test() throws Exception { // Check we get equal number of overall buckets on a default request GetOverallBucketsAction.Request overallBucketsRequest = new GetOverallBucketsAction.Request(job.getId()); GetOverallBucketsAction.Response overallBucketsResponse = client().execute( - GetOverallBucketsAction.INSTANCE, overallBucketsRequest).actionGet(); + GetOverallBucketsAction.INSTANCE, + overallBucketsRequest + ).actionGet(); assertThat(overallBucketsResponse.getOverallBuckets().count(), equalTo(3000L)); } @@ -85,7 +88,9 @@ public void test() throws Exception { GetOverallBucketsAction.Request aggregatedOverallBucketsRequest = new GetOverallBucketsAction.Request(job.getId()); aggregatedOverallBucketsRequest.setBucketSpan(TimeValue.timeValueSeconds(2 * BUCKET_SPAN_SECONDS)); GetOverallBucketsAction.Response aggregatedOverallBucketsResponse = client().execute( - GetOverallBucketsAction.INSTANCE, aggregatedOverallBucketsRequest).actionGet(); + GetOverallBucketsAction.INSTANCE, + aggregatedOverallBucketsRequest + ).actionGet(); assertThat(aggregatedOverallBucketsResponse.getOverallBuckets().count(), equalTo(1500L)); } @@ -94,7 +99,9 @@ public void test() throws Exception { GetOverallBucketsAction.Request filteredOverallBucketsRequest = new GetOverallBucketsAction.Request(job.getId()); filteredOverallBucketsRequest.setOverallScore(0.1); GetOverallBucketsAction.Response filteredOverallBucketsResponse = client().execute( - GetOverallBucketsAction.INSTANCE, filteredOverallBucketsRequest).actionGet(); + GetOverallBucketsAction.INSTANCE, + filteredOverallBucketsRequest + ).actionGet(); assertThat(filteredOverallBucketsResponse.getOverallBuckets().count(), equalTo(2L)); } } diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/PyTorchModelIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/PyTorchModelIT.java index 5cabe5393b225..a859214d0d438 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/PyTorchModelIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/PyTorchModelIT.java @@ -42,7 +42,6 @@ import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.greaterThanOrEqualTo; import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.is; @@ -73,32 +72,34 @@ */ public class PyTorchModelIT extends ESRestTestCase { - private static final String BASIC_AUTH_VALUE_SUPER_USER = - UsernamePasswordToken.basicAuthHeaderValue("x_pack_rest_user", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING); + private static final String BASIC_AUTH_VALUE_SUPER_USER = UsernamePasswordToken.basicAuthHeaderValue( + "x_pack_rest_user", + SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING + ); static final String BASE_64_ENCODED_MODEL = - "UEsDBAAACAgAAAAAAAAAAAAAAAAAAAAAAAAUAA4Ac2ltcGxlbW9kZWwvZGF0YS5wa2xGQgoAWlpaWlpaWlpaWoACY19fdG9yY2hfXwp" + - "TdXBlclNpbXBsZQpxACmBfShYCAAAAHRyYWluaW5ncQGIdWJxAi5QSwcIXOpBBDQAAAA0AAAAUEsDBBQACAgIAAAAAAAAAAAAAAAAAA" + - "AAAAAdAEEAc2ltcGxlbW9kZWwvY29kZS9fX3RvcmNoX18ucHlGQj0AWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaW" + - "lpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWnWOMWvDMBCF9/yKI5MMrnHTQsHgjt2aJdlCEIp9SgWSTpykFvfXV1htaYds0nfv473Jqhjh" + - "kAPywbhgUbzSnC02wwZAyqBYOUzIUUoY4XRe6SVr/Q8lVsYbf4UBLkS2kBk1aOIPxbOIaPVQtEQ8vUnZ/WlrSxTA+JCTNHMc4Ig+Ele" + - "s+Jod+iR3N/jDDf74wxu4e/5+DmtE9mUyhdgFNq7bZ3ekehbruC6aTxS/c1rom6Z698WrEfIYxcn4JGTftLA7tzCnJeD41IJVC+U07k" + - "umUHw3E47Vqh+xnULeFisYLx064mV8UTZibWFMmX0p23wBUEsHCE0EGH3yAAAAlwEAAFBLAwQUAAgICAAAAAAAAAAAAAAAAAAAAAAAJ" + - "wA5AHNpbXBsZW1vZGVsL2NvZGUvX190b3JjaF9fLnB5LmRlYnVnX3BrbEZCNQBaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpa" + - "WlpaWlpaWlpaWlpaWlpaWlpaWlpaWrWST0+DMBiHW6bOod/BGS94kKpo2Mwyox5x3pbgiXSAFtdR/nQu3IwHiZ9oX88CaeGu9tL0efq" + - "+v8P7fmiGA1wgTgoIcECZQqe6vmYD6G4hAJOcB1E8NazTm+ELyzY4C3Q0z8MsRwF+j4JlQUPEEo5wjH0WB9hCNFqgpOCExZY5QnnEw7" + - "ME+0v8GuaIs8wnKI7RigVrKkBzm0lh2OdjkeHllG28f066vK6SfEypF60S+vuYt4gjj2fYr/uPrSvRv356TepfJ9iWJRN0OaELQSZN3" + - "FRPNbcP1PTSntMr0x0HzLZQjPYIEo3UaFeiISRKH0Mil+BE/dyT1m7tCBLwVO1MX4DK3bbuTlXuy8r71j5Aoho66udAoseOnrdVzx28" + - "UFW6ROuO/lT6QKKyo79VU54emj9QSwcInsUTEDMBAAAFAwAAUEsDBAAACAgAAAAAAAAAAAAAAAAAAAAAAAAZAAYAc2ltcGxlbW9kZWw" + - "vY29uc3RhbnRzLnBrbEZCAgBaWoACKS5QSwcIbS8JVwQAAAAEAAAAUEsDBAAACAgAAAAAAAAAAAAAAAAAAAAAAAATADsAc2ltcGxlbW" + - "9kZWwvdmVyc2lvbkZCNwBaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaMwpQSwcI0" + - "Z5nVQIAAAACAAAAUEsBAgAAAAAICAAAAAAAAFzqQQQ0AAAANAAAABQAAAAAAAAAAAAAAAAAAAAAAHNpbXBsZW1vZGVsL2RhdGEucGts" + - "UEsBAgAAFAAICAgAAAAAAE0EGH3yAAAAlwEAAB0AAAAAAAAAAAAAAAAAhAAAAHNpbXBsZW1vZGVsL2NvZGUvX190b3JjaF9fLnB5UEs" + - "BAgAAFAAICAgAAAAAAJ7FExAzAQAABQMAACcAAAAAAAAAAAAAAAAAAgIAAHNpbXBsZW1vZGVsL2NvZGUvX190b3JjaF9fLnB5LmRlYn" + - "VnX3BrbFBLAQIAAAAACAgAAAAAAABtLwlXBAAAAAQAAAAZAAAAAAAAAAAAAAAAAMMDAABzaW1wbGVtb2RlbC9jb25zdGFudHMucGtsU" + - "EsBAgAAAAAICAAAAAAAANGeZ1UCAAAAAgAAABMAAAAAAAAAAAAAAAAAFAQAAHNpbXBsZW1vZGVsL3ZlcnNpb25QSwYGLAAAAAAAAAAe" + - "Ay0AAAAAAAAAAAAFAAAAAAAAAAUAAAAAAAAAagEAAAAAAACSBAAAAAAAAFBLBgcAAAAA/AUAAAAAAAABAAAAUEsFBgAAAAAFAAUAagE" + - "AAJIEAAAAAA=="; + "UEsDBAAACAgAAAAAAAAAAAAAAAAAAAAAAAAUAA4Ac2ltcGxlbW9kZWwvZGF0YS5wa2xGQgoAWlpaWlpaWlpaWoACY19fdG9yY2hfXwp" + + "TdXBlclNpbXBsZQpxACmBfShYCAAAAHRyYWluaW5ncQGIdWJxAi5QSwcIXOpBBDQAAAA0AAAAUEsDBBQACAgIAAAAAAAAAAAAAAAAAA" + + "AAAAAdAEEAc2ltcGxlbW9kZWwvY29kZS9fX3RvcmNoX18ucHlGQj0AWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaW" + + "lpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWnWOMWvDMBCF9/yKI5MMrnHTQsHgjt2aJdlCEIp9SgWSTpykFvfXV1htaYds0nfv473Jqhjh" + + "kAPywbhgUbzSnC02wwZAyqBYOUzIUUoY4XRe6SVr/Q8lVsYbf4UBLkS2kBk1aOIPxbOIaPVQtEQ8vUnZ/WlrSxTA+JCTNHMc4Ig+Ele" + + "s+Jod+iR3N/jDDf74wxu4e/5+DmtE9mUyhdgFNq7bZ3ekehbruC6aTxS/c1rom6Z698WrEfIYxcn4JGTftLA7tzCnJeD41IJVC+U07k" + + "umUHw3E47Vqh+xnULeFisYLx064mV8UTZibWFMmX0p23wBUEsHCE0EGH3yAAAAlwEAAFBLAwQUAAgICAAAAAAAAAAAAAAAAAAAAAAAJ" + + "wA5AHNpbXBsZW1vZGVsL2NvZGUvX190b3JjaF9fLnB5LmRlYnVnX3BrbEZCNQBaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpa" + + "WlpaWlpaWlpaWlpaWlpaWlpaWlpaWrWST0+DMBiHW6bOod/BGS94kKpo2Mwyox5x3pbgiXSAFtdR/nQu3IwHiZ9oX88CaeGu9tL0efq" + + "+v8P7fmiGA1wgTgoIcECZQqe6vmYD6G4hAJOcB1E8NazTm+ELyzY4C3Q0z8MsRwF+j4JlQUPEEo5wjH0WB9hCNFqgpOCExZY5QnnEw7" + + "ME+0v8GuaIs8wnKI7RigVrKkBzm0lh2OdjkeHllG28f066vK6SfEypF60S+vuYt4gjj2fYr/uPrSvRv356TepfJ9iWJRN0OaELQSZN3" + + "FRPNbcP1PTSntMr0x0HzLZQjPYIEo3UaFeiISRKH0Mil+BE/dyT1m7tCBLwVO1MX4DK3bbuTlXuy8r71j5Aoho66udAoseOnrdVzx28" + + "UFW6ROuO/lT6QKKyo79VU54emj9QSwcInsUTEDMBAAAFAwAAUEsDBAAACAgAAAAAAAAAAAAAAAAAAAAAAAAZAAYAc2ltcGxlbW9kZWw" + + "vY29uc3RhbnRzLnBrbEZCAgBaWoACKS5QSwcIbS8JVwQAAAAEAAAAUEsDBAAACAgAAAAAAAAAAAAAAAAAAAAAAAATADsAc2ltcGxlbW" + + "9kZWwvdmVyc2lvbkZCNwBaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaMwpQSwcI0" + + "Z5nVQIAAAACAAAAUEsBAgAAAAAICAAAAAAAAFzqQQQ0AAAANAAAABQAAAAAAAAAAAAAAAAAAAAAAHNpbXBsZW1vZGVsL2RhdGEucGts" + + "UEsBAgAAFAAICAgAAAAAAE0EGH3yAAAAlwEAAB0AAAAAAAAAAAAAAAAAhAAAAHNpbXBsZW1vZGVsL2NvZGUvX190b3JjaF9fLnB5UEs" + + "BAgAAFAAICAgAAAAAAJ7FExAzAQAABQMAACcAAAAAAAAAAAAAAAAAAgIAAHNpbXBsZW1vZGVsL2NvZGUvX190b3JjaF9fLnB5LmRlYn" + + "VnX3BrbFBLAQIAAAAACAgAAAAAAABtLwlXBAAAAAQAAAAZAAAAAAAAAAAAAAAAAMMDAABzaW1wbGVtb2RlbC9jb25zdGFudHMucGtsU" + + "EsBAgAAAAAICAAAAAAAANGeZ1UCAAAAAgAAABMAAAAAAAAAAAAAAAAAFAQAAHNpbXBsZW1vZGVsL3ZlcnNpb25QSwYGLAAAAAAAAAAe" + + "Ay0AAAAAAAAAAAAFAAAAAAAAAAUAAAAAAAAAagEAAAAAAACSBAAAAAAAAFBLBgcAAAAA/AUAAAAAAAABAAAAUEsFBgAAAAAFAAUAagE" + + "AAJIEAAAAAA=="; static final long RAW_MODEL_SIZE; // size of the model before base64 encoding static { RAW_MODEL_SIZE = Base64.getDecoder().decode(BASE_64_ENCODED_MODEL).length; @@ -114,14 +115,16 @@ protected Settings restClientSettings() { @Before public void setLogging() throws IOException { Request loggingSettings = new Request("PUT", "_cluster/settings"); - loggingSettings.setJsonEntity("" + - "{" + - "\"persistent\" : {\n" + - " \"logger.org.elasticsearch.xpack.ml.inference.allocation\" : \"TRACE\",\n" + - " \"logger.org.elasticsearch.xpack.ml.inference.deployment\" : \"TRACE\",\n" + - " \"logger.org.elasticsearch.xpack.ml.process.logging\" : \"TRACE\"\n" + - " }" + - "}"); + loggingSettings.setJsonEntity( + "" + + "{" + + "\"persistent\" : {\n" + + " \"logger.org.elasticsearch.xpack.ml.inference.allocation\" : \"TRACE\",\n" + + " \"logger.org.elasticsearch.xpack.ml.inference.deployment\" : \"TRACE\",\n" + + " \"logger.org.elasticsearch.xpack.ml.process.logging\" : \"TRACE\"\n" + + " }" + + "}" + ); client().performRequest(loggingSettings); } @@ -130,14 +133,16 @@ public void cleanup() throws Exception { terminate(executorService); Request loggingSettings = new Request("PUT", "_cluster/settings"); - loggingSettings.setJsonEntity("" + - "{" + - "\"persistent\" : {\n" + - " \"logger.org.elasticsearch.xpack.ml.inference.allocation\" :null,\n" + - " \"logger.org.elasticsearch.xpack.ml.inference.deployment\" : null,\n" + - " \"logger.org.elasticsearch.xpack.ml.process.logging\" : null\n" + - " }" + - "}"); + loggingSettings.setJsonEntity( + "" + + "{" + + "\"persistent\" : {\n" + + " \"logger.org.elasticsearch.xpack.ml.inference.allocation\" :null,\n" + + " \"logger.org.elasticsearch.xpack.ml.inference.deployment\" : null,\n" + + " \"logger.org.elasticsearch.xpack.ml.process.logging\" : null\n" + + " }" + + "}" + ); client().performRequest(loggingSettings); new MlRestTestStateCleaner(logger, adminClient()).resetFeatures(); @@ -204,10 +209,7 @@ public void testDeleteFailureDueToDeployment() throws IOException { putModelDefinition(modelId); putVocabulary(List.of("these", "are", "my", "words"), modelId); startDeployment(modelId); - Exception ex = expectThrows( - Exception.class, - () -> client().performRequest(new Request("DELETE", "_ml/trained_models/" + modelId)) - ); + Exception ex = expectThrows(Exception.class, () -> client().performRequest(new Request("DELETE", "_ml/trained_models/" + modelId))); assertThat(ex.getMessage(), containsString("Cannot delete model [test_deployed_model_delete] as it is currently deployed")); stopDeployment(modelId); } @@ -230,20 +232,21 @@ public void testDeploymentStats() throws IOException { CheckedBiConsumer assertAtLeast = (modelId, state) -> { startDeployment(modelId, state.toString()); Response response = getDeploymentStats(modelId); - List> stats = (List>)entityAsMap(response).get("deployment_stats"); + List> stats = (List>) entityAsMap(response).get("deployment_stats"); assertThat(stats, hasSize(1)); - String statusState = (String)XContentMapValues.extractValue("allocation_status.state", stats.get(0)); + String statusState = (String) XContentMapValues.extractValue("allocation_status.state", stats.get(0)); assertThat(stats.toString(), statusState, is(not(nullValue()))); assertThat(AllocationStatus.State.fromString(statusState), greaterThanOrEqualTo(state)); - Integer byteSize = (Integer)XContentMapValues.extractValue("model_size_bytes", stats.get(0)); + Integer byteSize = (Integer) XContentMapValues.extractValue("model_size_bytes", stats.get(0)); assertThat(byteSize, is(not(nullValue()))); - assertThat(byteSize, equalTo((int)RAW_MODEL_SIZE)); + assertThat(byteSize, equalTo((int) RAW_MODEL_SIZE)); - Response humanResponse = client() - .performRequest(new Request("GET", "/_ml/trained_models/" + modelId + "/deployment/_stats?human")); - stats = (List>)entityAsMap(humanResponse).get("deployment_stats"); + Response humanResponse = client().performRequest( + new Request("GET", "/_ml/trained_models/" + modelId + "/deployment/_stats?human") + ); + stats = (List>) entityAsMap(humanResponse).get("deployment_stats"); assertThat(stats, hasSize(1)); - String stringBytes = (String)XContentMapValues.extractValue("model_size", stats.get(0)); + String stringBytes = (String) XContentMapValues.extractValue("model_size", stats.get(0)); assertThat(stringBytes, is(not(nullValue()))); assertThat(stringBytes, equalTo("1.5kb")); stopDeployment(model); @@ -265,11 +268,11 @@ public void testLiveDeploymentStats() throws IOException { infer("once", modelA); infer("twice", modelA); Response response = getDeploymentStats(modelA); - List> stats = (List>)entityAsMap(response).get("deployment_stats"); + List> stats = (List>) entityAsMap(response).get("deployment_stats"); assertThat(stats, hasSize(1)); assertThat(stats.get(0).get("model_id"), equalTo(modelA)); - assertThat(stats.get(0).get("model_size_bytes"), equalTo((int)RAW_MODEL_SIZE)); - List> nodes = (List>)stats.get(0).get("nodes"); + assertThat(stats.get(0).get("model_size_bytes"), equalTo((int) RAW_MODEL_SIZE)); + List> nodes = (List>) stats.get(0).get("nodes"); // 2 of the 3 nodes in the cluster are ML nodes assertThat(nodes, hasSize(2)); int inferenceCount = sumInferenceCountOnNodes(nodes); @@ -306,11 +309,11 @@ public void testGetDeploymentStats_WithWildcard() throws IOException { assertThat(stats, hasSize(2)); assertThat(stats.get(0).get("model_id"), equalTo(modelBar)); assertThat(stats.get(1).get("model_id"), equalTo(modelFoo)); - List> barNodes = (List>)stats.get(0).get("nodes"); + List> barNodes = (List>) stats.get(0).get("nodes"); // 2 of the 3 nodes in the cluster are ML nodes assertThat(barNodes, hasSize(2)); assertThat(sumInferenceCountOnNodes(barNodes), equalTo(1)); - List> fooNodes = (List>)stats.get(0).get("nodes"); + List> fooNodes = (List>) stats.get(0).get("nodes"); assertThat(fooNodes, hasSize(2)); assertThat(sumInferenceCountOnNodes(fooNodes), equalTo(1)); } @@ -330,13 +333,17 @@ public void testGetDeploymentStats_WithWildcard() throws IOException { } { ResponseException e = expectThrows(ResponseException.class, () -> getDeploymentStats("c*", false)); - assertThat(EntityUtils.toString(e.getResponse().getEntity()), - containsString("No known trained model with deployment with id [c*]")); + assertThat( + EntityUtils.toString(e.getResponse().getEntity()), + containsString("No known trained model with deployment with id [c*]") + ); } { ResponseException e = expectThrows(ResponseException.class, () -> getDeploymentStats("foo,c*", false)); - assertThat(EntityUtils.toString(e.getResponse().getEntity()), - containsString("No known trained model with deployment with id [c*]")); + assertThat( + EntityUtils.toString(e.getResponse().getEntity()), + containsString("No known trained model with deployment with id [c*]") + ); } } @@ -363,11 +370,11 @@ public void testGetDeploymentStats_WithStartedStoppedDeployments() throws IOExce assertThat(stats, hasSize(2)); // check all nodes are started - for (int i : new int[]{0, 1}) { + for (int i : new int[] { 0, 1 }) { List> nodes = (List>) stats.get(i).get("nodes"); // 2 ml nodes assertThat(nodes, hasSize(2)); - for (int j : new int[]{0, 1}) { + for (int j : new int[] { 0, 1 }) { Object state = MapHelper.dig("routing_state.routing_state", nodes.get(j)); assertEquals("started", state); } @@ -385,7 +392,7 @@ public void testGetDeploymentStats_WithStartedStoppedDeployments() throws IOExce List> nodes = (List>) stats.get(0).get("nodes"); // 2 ml nodes assertThat(nodes, hasSize(2)); - for (int j : new int[]{0, 1}) { + for (int j : new int[] { 0, 1 }) { Object state = MapHelper.dig("routing_state.routing_state", nodes.get(j)); assertEquals("started", state); } @@ -408,11 +415,17 @@ private int sumInferenceCountOnNodes(List> nodes) { private void putModelDefinition(String modelId) throws IOException { Request request = new Request("PUT", "_ml/trained_models/" + modelId + "/definition/0"); - request.setJsonEntity("{ " + - "\"total_definition_length\":" + RAW_MODEL_SIZE + "," + - "\"definition\": \"" + BASE_64_ENCODED_MODEL + "\"," + - "\"total_parts\": 1" + - "}"); + request.setJsonEntity( + "{ " + + "\"total_definition_length\":" + + RAW_MODEL_SIZE + + "," + + "\"definition\": \"" + + BASE_64_ENCODED_MODEL + + "\"," + + "\"total_parts\": 1" + + "}" + ); client().performRequest(request); } @@ -422,29 +435,26 @@ private void putVocabulary(List vocabulary, String modelId) throws IOExc vocabularyWithPad.addAll(vocabulary); String quotedWords = vocabularyWithPad.stream().map(s -> "\"" + s + "\"").collect(Collectors.joining(",")); - Request request = new Request( - "PUT", - "_ml/trained_models/" + modelId + "/vocabulary" - ); - request.setJsonEntity("{ " + - "\"vocabulary\": [" + quotedWords + "]\n" + - "}"); + Request request = new Request("PUT", "_ml/trained_models/" + modelId + "/vocabulary"); + request.setJsonEntity("{ " + "\"vocabulary\": [" + quotedWords + "]\n" + "}"); client().performRequest(request); } private void createTrainedModel(String modelId) throws IOException { Request request = new Request("PUT", "/_ml/trained_models/" + modelId); - request.setJsonEntity("{ " + - " \"description\": \"simple model for testing\",\n" + - " \"model_type\": \"pytorch\",\n" + - " \"inference_config\": {\n" + - " \"pass_through\": {\n" + - " \"tokenization\": {" + - " \"bert\": {\"with_special_tokens\": false}\n" + - " }\n" + - " }\n" + - " }\n" + - "}"); + request.setJsonEntity( + "{ " + + " \"description\": \"simple model for testing\",\n" + + " \"model_type\": \"pytorch\",\n" + + " \"inference_config\": {\n" + + " \"pass_through\": {\n" + + " \"tokenization\": {" + + " \"bert\": {\"with_special_tokens\": false}\n" + + " }\n" + + " }\n" + + " }\n" + + "}" + ); client().performRequest(request); } @@ -453,8 +463,14 @@ private Response startDeployment(String modelId) throws IOException { } private Response startDeployment(String modelId, String waitForState) throws IOException { - Request request = new Request("POST", "/_ml/trained_models/" + modelId + - "/deployment/_start?timeout=40s&wait_for=" + waitForState + "&inference_threads=1&model_threads=1"); + Request request = new Request( + "POST", + "/_ml/trained_models/" + + modelId + + "/deployment/_start?timeout=40s&wait_for=" + + waitForState + + "&inference_threads=1&model_threads=1" + ); return client().performRequest(request); } @@ -474,26 +490,28 @@ private Response getDeploymentStats(String modelId, boolean allowNoMatch) throws private Response infer(String input, String modelId, TimeValue timeout) throws IOException { Request request = new Request("POST", "/_ml/trained_models/" + modelId + "/deployment/_infer?timeout=" + timeout.toString()); - request.setJsonEntity("{ " + - "\"docs\": [{\"input\":\"" + input + "\"}]\n" + - "}"); + request.setJsonEntity("{ " + "\"docs\": [{\"input\":\"" + input + "\"}]\n" + "}"); return client().performRequest(request); } private Response infer(String input, String modelId) throws IOException { Request request = new Request("POST", "/_ml/trained_models/" + modelId + "/deployment/_infer"); - request.setJsonEntity("{ " + - "\"docs\": [{\"input\":\"" + input + "\"}]\n" + - "}"); + request.setJsonEntity("{ " + "\"docs\": [{\"input\":\"" + input + "\"}]\n" + "}"); return client().performRequest(request); } private Response infer(String input, String modelId, String resultsField) throws IOException { Request request = new Request("POST", "/_ml/trained_models/" + modelId + "/deployment/_infer"); - request.setJsonEntity("{ " + - "\"docs\": [{\"input\":\"" + input + "\"}],\n" + - "\"inference_config\": {\"pass_through\":{\"results_field\": \"" + resultsField + "\"}}\n" + - "}"); + request.setJsonEntity( + "{ " + + "\"docs\": [{\"input\":\"" + + input + + "\"}],\n" + + "\"inference_config\": {\"pass_through\":{\"results_field\": \"" + + resultsField + + "\"}}\n" + + "}" + ); return client().performRequest(request); } diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/RegressionEvaluationIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/RegressionEvaluationIT.java index f96d8cf507003..fae33ec52415f 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/RegressionEvaluationIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/RegressionEvaluationIT.java @@ -48,28 +48,27 @@ public void cleanup() { } public void testEvaluate_DefaultMetrics() { - EvaluateDataFrameAction.Response evaluateDataFrameResponse = - evaluateDataFrame(HOUSES_DATA_INDEX, new Regression(PRICE_FIELD, PRICE_PREDICTION_FIELD, null)); + EvaluateDataFrameAction.Response evaluateDataFrameResponse = evaluateDataFrame( + HOUSES_DATA_INDEX, + new Regression(PRICE_FIELD, PRICE_PREDICTION_FIELD, null) + ); assertThat(evaluateDataFrameResponse.getEvaluationName(), equalTo(Regression.NAME.getPreferredName())); assertThat( evaluateDataFrameResponse.getMetrics().stream().map(EvaluationMetricResult::getMetricName).collect(toList()), - containsInAnyOrder( - MeanSquaredError.NAME.getPreferredName(), - RSquared.NAME.getPreferredName(), - Huber.NAME.getPreferredName() - ) + containsInAnyOrder(MeanSquaredError.NAME.getPreferredName(), RSquared.NAME.getPreferredName(), Huber.NAME.getPreferredName()) ); } public void testEvaluate_AllMetrics() { - EvaluateDataFrameAction.Response evaluateDataFrameResponse = - evaluateDataFrame( - HOUSES_DATA_INDEX, - new Regression( - PRICE_FIELD, - PRICE_PREDICTION_FIELD, - List.of(new MeanSquaredError(), new MeanSquaredLogarithmicError((Double) null), new RSquared()))); + EvaluateDataFrameAction.Response evaluateDataFrameResponse = evaluateDataFrame( + HOUSES_DATA_INDEX, + new Regression( + PRICE_FIELD, + PRICE_PREDICTION_FIELD, + List.of(new MeanSquaredError(), new MeanSquaredLogarithmicError((Double) null), new RSquared()) + ) + ); assertThat(evaluateDataFrameResponse.getEvaluationName(), equalTo(Regression.NAME.getPreferredName())); assertThat( @@ -77,12 +76,16 @@ public void testEvaluate_AllMetrics() { contains( MeanSquaredError.NAME.getPreferredName(), MeanSquaredLogarithmicError.NAME.getPreferredName(), - RSquared.NAME.getPreferredName())); + RSquared.NAME.getPreferredName() + ) + ); } public void testEvaluate_MeanSquaredError() { - EvaluateDataFrameAction.Response evaluateDataFrameResponse = - evaluateDataFrame(HOUSES_DATA_INDEX, new Regression(PRICE_FIELD, PRICE_PREDICTION_FIELD, List.of(new MeanSquaredError()))); + EvaluateDataFrameAction.Response evaluateDataFrameResponse = evaluateDataFrame( + HOUSES_DATA_INDEX, + new Regression(PRICE_FIELD, PRICE_PREDICTION_FIELD, List.of(new MeanSquaredError())) + ); assertThat(evaluateDataFrameResponse.getEvaluationName(), equalTo(Regression.NAME.getPreferredName())); assertThat(evaluateDataFrameResponse.getMetrics(), hasSize(1)); @@ -93,10 +96,10 @@ public void testEvaluate_MeanSquaredError() { } public void testEvaluate_MeanSquaredLogarithmicError() { - EvaluateDataFrameAction.Response evaluateDataFrameResponse = - evaluateDataFrame( - HOUSES_DATA_INDEX, - new Regression(PRICE_FIELD, PRICE_PREDICTION_FIELD, List.of(new MeanSquaredLogarithmicError((Double) null)))); + EvaluateDataFrameAction.Response evaluateDataFrameResponse = evaluateDataFrame( + HOUSES_DATA_INDEX, + new Regression(PRICE_FIELD, PRICE_PREDICTION_FIELD, List.of(new MeanSquaredLogarithmicError((Double) null))) + ); assertThat(evaluateDataFrameResponse.getEvaluationName(), equalTo(Regression.NAME.getPreferredName())); assertThat(evaluateDataFrameResponse.getMetrics(), hasSize(1)); @@ -107,10 +110,10 @@ public void testEvaluate_MeanSquaredLogarithmicError() { } public void testEvaluate_Huber() { - EvaluateDataFrameAction.Response evaluateDataFrameResponse = - evaluateDataFrame( - HOUSES_DATA_INDEX, - new Regression(PRICE_FIELD, PRICE_PREDICTION_FIELD, List.of(new Huber((Double) null)))); + EvaluateDataFrameAction.Response evaluateDataFrameResponse = evaluateDataFrame( + HOUSES_DATA_INDEX, + new Regression(PRICE_FIELD, PRICE_PREDICTION_FIELD, List.of(new Huber((Double) null))) + ); assertThat(evaluateDataFrameResponse.getEvaluationName(), equalTo(Regression.NAME.getPreferredName())); assertThat(evaluateDataFrameResponse.getMetrics(), hasSize(1)); @@ -121,8 +124,10 @@ public void testEvaluate_Huber() { } public void testEvaluate_RSquared() { - EvaluateDataFrameAction.Response evaluateDataFrameResponse = - evaluateDataFrame(HOUSES_DATA_INDEX, new Regression(PRICE_FIELD, PRICE_PREDICTION_FIELD, List.of(new RSquared()))); + EvaluateDataFrameAction.Response evaluateDataFrameResponse = evaluateDataFrame( + HOUSES_DATA_INDEX, + new Regression(PRICE_FIELD, PRICE_PREDICTION_FIELD, List.of(new RSquared())) + ); assertThat(evaluateDataFrameResponse.getEvaluationName(), equalTo(Regression.NAME.getPreferredName())); assertThat(evaluateDataFrameResponse.getMetrics(), hasSize(1)); @@ -133,22 +138,17 @@ public void testEvaluate_RSquared() { } private static void createHousesIndex(String indexName) { - client().admin().indices().prepareCreate(indexName) - .setMapping( - PRICE_FIELD, "type=double", - PRICE_PREDICTION_FIELD, "type=double") + client().admin() + .indices() + .prepareCreate(indexName) + .setMapping(PRICE_FIELD, "type=double", PRICE_PREDICTION_FIELD, "type=double") .get(); } private static void indexHousesData(String indexName) { - BulkRequestBuilder bulkRequestBuilder = client().prepareBulk() - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); + BulkRequestBuilder bulkRequestBuilder = client().prepareBulk().setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); for (int i = 0; i < 100; i++) { - bulkRequestBuilder.add( - new IndexRequest(indexName) - .source( - PRICE_FIELD, 1000, - PRICE_PREDICTION_FIELD, 0)); + bulkRequestBuilder.add(new IndexRequest(indexName).source(PRICE_FIELD, 1000, PRICE_PREDICTION_FIELD, 0)); } BulkResponse bulkResponse = bulkRequestBuilder.get(); if (bulkResponse.hasFailures()) { diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/RegressionIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/RegressionIT.java index 97d3e2e2491af..478c15d9237b0 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/RegressionIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/RegressionIT.java @@ -16,11 +16,11 @@ import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.support.WriteRequest; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchModule; import org.elasticsearch.search.fetch.subphase.FetchSourceContext; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.ml.action.GetDataFrameAnalyticsStatsAction; import org.elasticsearch.xpack.core.ml.action.GetTrainedModelsAction; import org.elasticsearch.xpack.core.ml.action.NodeAcknowledgedResponse; @@ -96,7 +96,11 @@ public void testSingleNumericFeatureAndMixedTrainingAndNonTrainingRows() throws String predictedClassField = DEPENDENT_VARIABLE_FIELD + "_prediction"; indexData(sourceIndex, 300, 50); - DataFrameAnalyticsConfig config = buildAnalytics(jobId, sourceIndex, destIndex, null, + DataFrameAnalyticsConfig config = buildAnalytics( + jobId, + sourceIndex, + destIndex, + null, new Regression( DEPENDENT_VARIABLE_FIELD, BoostedTreeParams.builder().setNumTopFeatureImportanceValues(1).build(), @@ -106,7 +110,8 @@ public void testSingleNumericFeatureAndMixedTrainingAndNonTrainingRows() throws null, null, null, - null) + null + ) ); putAnalytics(config); @@ -137,7 +142,7 @@ public void testSingleNumericFeatureAndMixedTrainingAndNonTrainingRows() throws assertThat(resultsObject.containsKey("is_training"), is(true)); assertThat(resultsObject.get("is_training"), is(destDoc.containsKey(DEPENDENT_VARIABLE_FIELD))); @SuppressWarnings("unchecked") - List> importanceArray = (List>)resultsObject.get("feature_importance"); + List> importanceArray = (List>) resultsObject.get("feature_importance"); if (importanceArray.isEmpty()) { badDocuments.add(destDoc); @@ -150,25 +155,39 @@ public void testSingleNumericFeatureAndMixedTrainingAndNonTrainingRows() throws assertThat(importanceArray, hasSize(greaterThan(0))); assertThat( - importanceArray.stream().filter(m -> NUMERICAL_FEATURE_FIELD.equals(m.get("feature_name")) - || DISCRETE_NUMERICAL_FEATURE_FIELD.equals(m.get("feature_name"))).findAny(), - isPresent()); + importanceArray.stream() + .filter( + m -> NUMERICAL_FEATURE_FIELD.equals(m.get("feature_name")) + || DISCRETE_NUMERICAL_FEATURE_FIELD.equals(m.get("feature_name")) + ) + .findAny(), + isPresent() + ); } // If feature importance was empty for some of the docs this assertion helps us // understand whether the offending docs were training or test docs. - assertThat("There were [" + trainingDocsWithEmptyFeatureImportance + "] training docs and [" - + testDocsWithEmptyFeatureImportance + "] test docs with empty feature importance" - + " from " + sourceData.getHits().getTotalHits().value + " hits.\n" - + badDocuments, - trainingDocsWithEmptyFeatureImportance + testDocsWithEmptyFeatureImportance, equalTo(0)); + assertThat( + "There were [" + + trainingDocsWithEmptyFeatureImportance + + "] training docs and [" + + testDocsWithEmptyFeatureImportance + + "] test docs with empty feature importance" + + " from " + + sourceData.getHits().getTotalHits().value + + " hits.\n" + + badDocuments, + trainingDocsWithEmptyFeatureImportance + testDocsWithEmptyFeatureImportance, + equalTo(0) + ); assertProgressComplete(jobId); assertThat(searchStoredProgress(jobId).getHits().getTotalHits().value, equalTo(1L)); assertModelStatePersisted(stateDocId()); assertExactlyOneInferenceModelPersisted(jobId); assertMlResultsFieldMappings(destIndex, predictedClassField, "double"); - assertThatAuditMessagesMatch(jobId, + assertThatAuditMessagesMatch( + jobId, "Created analytics with type [regression]", "Estimated memory usage [", "Starting analytics on node", @@ -179,7 +198,8 @@ public void testSingleNumericFeatureAndMixedTrainingAndNonTrainingRows() throws "Started loading data", "Started analyzing", "Started writing results", - "Finished analysis"); + "Finished analysis" + ); } public void testWithOnlyTrainingRowsAndTrainingPercentIsHundred() throws Exception { @@ -217,7 +237,8 @@ public void testWithOnlyTrainingRowsAndTrainingPercentIsHundred() throws Excepti assertModelStatePersisted(stateDocId()); assertExactlyOneInferenceModelPersisted(jobId); assertMlResultsFieldMappings(destIndex, predictedClassField, "double"); - assertThatAuditMessagesMatch(jobId, + assertThatAuditMessagesMatch( + jobId, "Created analytics with type [regression]", "Estimated memory usage [", "Starting analytics on node", @@ -228,7 +249,8 @@ public void testWithOnlyTrainingRowsAndTrainingPercentIsHundred() throws Excepti "Started loading data", "Started analyzing", "Started writing results", - "Finished analysis"); + "Finished analysis" + ); } public void testWithOnlyTrainingRowsAndTrainingPercentIsFifty() throws Exception { @@ -236,14 +258,13 @@ public void testWithOnlyTrainingRowsAndTrainingPercentIsFifty() throws Exception String predictedClassField = DEPENDENT_VARIABLE_FIELD + "_prediction"; indexData(sourceIndex, 350, 0); - DataFrameAnalyticsConfig config = - buildAnalytics( - jobId, - sourceIndex, - destIndex, - null, - new Regression(DEPENDENT_VARIABLE_FIELD, BoostedTreeParams.builder().build(), - null, 50.0, null, null, null, null, null)); + DataFrameAnalyticsConfig config = buildAnalytics( + jobId, + sourceIndex, + destIndex, + null, + new Regression(DEPENDENT_VARIABLE_FIELD, BoostedTreeParams.builder().build(), null, 50.0, null, null, null, null, null) + ); putAnalytics(config); assertIsStopped(jobId); @@ -283,7 +304,8 @@ public void testWithOnlyTrainingRowsAndTrainingPercentIsFifty() throws Exception assertModelStatePersisted(stateDocId()); assertExactlyOneInferenceModelPersisted(jobId); assertMlResultsFieldMappings(destIndex, predictedClassField, "double"); - assertThatAuditMessagesMatch(jobId, + assertThatAuditMessagesMatch( + jobId, "Created analytics with type [regression]", "Estimated memory usage [", "Starting analytics on node", @@ -294,7 +316,8 @@ public void testWithOnlyTrainingRowsAndTrainingPercentIsFifty() throws Exception "Started loading data", "Started analyzing", "Started writing results", - "Finished analysis"); + "Finished analysis" + ); } public void testStopAndRestart() throws Exception { @@ -361,9 +384,13 @@ public void testTwoJobsWithSameRandomizeSeedUseSameTrainingSet() throws Exceptio .setMaxTrees(1) .build(); - DataFrameAnalyticsConfig firstJob = buildAnalytics(firstJobId, sourceIndex, firstJobDestIndex, null, - new Regression(DEPENDENT_VARIABLE_FIELD, boostedTreeParams, null, 50.0, - null, null, null, null, null)); + DataFrameAnalyticsConfig firstJob = buildAnalytics( + firstJobId, + sourceIndex, + firstJobDestIndex, + null, + new Regression(DEPENDENT_VARIABLE_FIELD, boostedTreeParams, null, 50.0, null, null, null, null, null) + ); putAnalytics(firstJob); startAnalytics(firstJobId); waitUntilAnalyticsIsStopped(firstJobId); @@ -372,9 +399,13 @@ public void testTwoJobsWithSameRandomizeSeedUseSameTrainingSet() throws Exceptio String secondJobDestIndex = secondJobId + "_dest"; long randomizeSeed = ((Regression) firstJob.getAnalysis()).getRandomizeSeed(); - DataFrameAnalyticsConfig secondJob = buildAnalytics(secondJobId, sourceIndex, secondJobDestIndex, null, - new Regression(DEPENDENT_VARIABLE_FIELD, boostedTreeParams, null, 50.0, - randomizeSeed, null, null, null, null)); + DataFrameAnalyticsConfig secondJob = buildAnalytics( + secondJobId, + sourceIndex, + secondJobDestIndex, + null, + new Regression(DEPENDENT_VARIABLE_FIELD, boostedTreeParams, null, 50.0, randomizeSeed, null, null, null, null) + ); putAnalytics(secondJob); startAnalytics(secondJobId); @@ -410,7 +441,9 @@ public void testDeleteExpiredData_RemovesUnusedState() throws Exception { // Delete the config straight from the config index DeleteResponse deleteResponse = client().prepareDelete(".ml-config", DataFrameAnalyticsConfig.documentId(jobId)) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).execute().actionGet(); + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + .execute() + .actionGet(); assertThat(deleteResponse.status(), equalTo(RestStatus.OK)); // Now calling the _delete_expired_data API should remove unused state @@ -425,14 +458,13 @@ public void testDependentVariableIsLong() throws Exception { String predictedClassField = DISCRETE_NUMERICAL_FEATURE_FIELD + "_prediction"; indexData(sourceIndex, 100, 0); - DataFrameAnalyticsConfig config = - buildAnalytics( - jobId, - sourceIndex, - destIndex, - null, - new Regression(DISCRETE_NUMERICAL_FEATURE_FIELD, BoostedTreeParams.builder().build(), - null, null, null, null, null, null, null)); + DataFrameAnalyticsConfig config = buildAnalytics( + jobId, + sourceIndex, + destIndex, + null, + new Regression(DISCRETE_NUMERICAL_FEATURE_FIELD, BoostedTreeParams.builder().build(), null, null, null, null, null, null, null) + ); putAnalytics(config); assertIsStopped(jobId); @@ -450,7 +482,11 @@ public void testWithDatastream() throws Exception { String predictedClassField = DEPENDENT_VARIABLE_FIELD + "_prediction"; indexData(sourceIndex, 300, 50, true); - DataFrameAnalyticsConfig config = buildAnalytics(jobId, sourceIndex, destIndex, null, + DataFrameAnalyticsConfig config = buildAnalytics( + jobId, + sourceIndex, + destIndex, + null, new Regression( DEPENDENT_VARIABLE_FIELD, BoostedTreeParams.builder().setNumTopFeatureImportanceValues(1).build(), @@ -460,7 +496,8 @@ public void testWithDatastream() throws Exception { null, null, null, - null) + null + ) ); putAnalytics(config); @@ -479,12 +516,17 @@ public void testWithDatastream() throws Exception { assertThat(resultsObject.containsKey("is_training"), is(true)); assertThat(resultsObject.get("is_training"), is(destDoc.containsKey(DEPENDENT_VARIABLE_FIELD))); @SuppressWarnings("unchecked") - List> importanceArray = (List>)resultsObject.get("feature_importance"); + List> importanceArray = (List>) resultsObject.get("feature_importance"); assertThat(importanceArray, hasSize(greaterThan(0))); assertThat( - importanceArray.stream().filter(m -> NUMERICAL_FEATURE_FIELD.equals(m.get("feature_name")) - || DISCRETE_NUMERICAL_FEATURE_FIELD.equals(m.get("feature_name"))).findAny(), - isPresent()); + importanceArray.stream() + .filter( + m -> NUMERICAL_FEATURE_FIELD.equals(m.get("feature_name")) + || DISCRETE_NUMERICAL_FEATURE_FIELD.equals(m.get("feature_name")) + ) + .findAny(), + isPresent() + ); } assertProgressComplete(jobId); @@ -492,7 +534,8 @@ public void testWithDatastream() throws Exception { assertModelStatePersisted(stateDocId()); assertExactlyOneInferenceModelPersisted(jobId); assertMlResultsFieldMappings(destIndex, predictedClassField, "double"); - assertThatAuditMessagesMatch(jobId, + assertThatAuditMessagesMatch( + jobId, "Created analytics with type [regression]", "Estimated memory usage [", "Starting analytics on node", @@ -503,7 +546,8 @@ public void testWithDatastream() throws Exception { "Started loading data", "Started analyzing", "Started writing results", - "Finished analysis"); + "Finished analysis" + ); } public void testAliasFields() throws Exception { @@ -518,27 +562,24 @@ public void testAliasFields() throws Exception { initialize("regression_alias_fields"); String predictionField = "field_2_prediction"; - String mapping = "{\n" + - " \"properties\": {\n" + - " \"field_1\": {\n" + - " \"type\": \"integer\"\n" + - " }," + - " \"field_2\": {\n" + - " \"type\": \"integer\"\n" + - " }," + - " \"field_1_alias\": {\n" + - " \"type\": \"alias\",\n" + - " \"path\": \"field_1\"\n" + - " }" + - " }\n" + - " }"; - client().admin().indices().prepareCreate(sourceIndex) - .setMapping(mapping) - .get(); + String mapping = "{\n" + + " \"properties\": {\n" + + " \"field_1\": {\n" + + " \"type\": \"integer\"\n" + + " }," + + " \"field_2\": {\n" + + " \"type\": \"integer\"\n" + + " }," + + " \"field_1_alias\": {\n" + + " \"type\": \"alias\",\n" + + " \"path\": \"field_1\"\n" + + " }" + + " }\n" + + " }"; + client().admin().indices().prepareCreate(sourceIndex).setMapping(mapping).get(); int totalDocCount = 300; - BulkRequestBuilder bulkRequestBuilder = client().prepareBulk() - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); + BulkRequestBuilder bulkRequestBuilder = client().prepareBulk().setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); for (int i = 0; i < totalDocCount; i++) { List source = List.of("field_1", i, "field_2", 2 * i); IndexRequest indexRequest = new IndexRequest(sourceIndex).source(source.toArray()).opType(DocWriteRequest.OpType.CREATE); @@ -558,13 +599,13 @@ public void testAliasFields() throws Exception { null, null, null, - null); - DataFrameAnalyticsConfig config = new DataFrameAnalyticsConfig.Builder() - .setId(jobId) + null + ); + DataFrameAnalyticsConfig config = new DataFrameAnalyticsConfig.Builder().setId(jobId) .setSource(new DataFrameAnalyticsSource(new String[] { sourceIndex }, null, null, Collections.emptyMap())) .setDest(new DataFrameAnalyticsDest(destIndex, null)) .setAnalysis(regression) - .setAnalyzedFields(new FetchSourceContext(true, null, new String[] {"field_1"})) + .setAnalyzedFields(new FetchSourceContext(true, null, new String[] { "field_1" })) .build(); putAnalytics(config); @@ -600,7 +641,8 @@ public void testAliasFields() throws Exception { assertModelStatePersisted(stateDocId()); assertExactlyOneInferenceModelPersisted(jobId); assertMlResultsFieldMappings(destIndex, predictionField, "double"); - assertThatAuditMessagesMatch(jobId, + assertThatAuditMessagesMatch( + jobId, "Created analytics with type [regression]", "Estimated memory usage [", "Starting analytics on node", @@ -611,7 +653,8 @@ public void testAliasFields() throws Exception { "Started loading data", "Started analyzing", "Started writing results", - "Finished analysis"); + "Finished analysis" + ); } public void testWithCustomFeatureProcessors() throws Exception { @@ -619,7 +662,11 @@ public void testWithCustomFeatureProcessors() throws Exception { String predictedClassField = DEPENDENT_VARIABLE_FIELD + "_prediction"; indexData(sourceIndex, 300, 50); - DataFrameAnalyticsConfig config = buildAnalytics(jobId, sourceIndex, destIndex, null, + DataFrameAnalyticsConfig config = buildAnalytics( + jobId, + sourceIndex, + destIndex, + null, new Regression( DEPENDENT_VARIABLE_FIELD, BoostedTreeParams.builder().setNumTopFeatureImportanceValues(1).build(), @@ -629,10 +676,14 @@ public void testWithCustomFeatureProcessors() throws Exception { null, null, Arrays.asList( - new OneHotEncoding(DISCRETE_NUMERICAL_FEATURE_FIELD, - Collections.singletonMap(DISCRETE_NUMERICAL_FEATURE_VALUES.get(0).toString(), "tenner"), true) + new OneHotEncoding( + DISCRETE_NUMERICAL_FEATURE_FIELD, + Collections.singletonMap(DISCRETE_NUMERICAL_FEATURE_VALUES.get(0).toString(), "tenner"), + true + ) ), - null) + null + ) ); putAnalytics(config); @@ -658,7 +709,8 @@ public void testWithCustomFeatureProcessors() throws Exception { assertModelStatePersisted(stateDocId()); assertExactlyOneInferenceModelPersisted(jobId); assertMlResultsFieldMappings(destIndex, predictedClassField, "double"); - assertThatAuditMessagesMatch(jobId, + assertThatAuditMessagesMatch( + jobId, "Created analytics with type [regression]", "Estimated memory usage [", "Starting analytics on node", @@ -669,9 +721,12 @@ public void testWithCustomFeatureProcessors() throws Exception { "Started loading data", "Started analyzing", "Started writing results", - "Finished analysis"); - GetTrainedModelsAction.Response response = client().execute(GetTrainedModelsAction.INSTANCE, - new GetTrainedModelsAction.Request(jobId + "*", Collections.emptyList(), Collections.singleton("definition"))).actionGet(); + "Finished analysis" + ); + GetTrainedModelsAction.Response response = client().execute( + GetTrainedModelsAction.INSTANCE, + new GetTrainedModelsAction.Request(jobId + "*", Collections.emptyList(), Collections.singleton("definition")) + ).actionGet(); assertThat(response.getResources().results().size(), equalTo(1)); TrainedModelConfig modelConfig = response.getResources().results().get(0); modelConfig.ensureParsedDefinition(xContentRegistry()); @@ -696,27 +751,31 @@ public void testWithSearchRuntimeMappings() throws Exception { numericRuntimeFieldMapping.put("script", "emit(doc['" + NUMERICAL_FEATURE_FIELD + "'].value)"); Map dependentVariableRuntimeFieldMapping = new HashMap<>(); dependentVariableRuntimeFieldMapping.put("type", "double"); - dependentVariableRuntimeFieldMapping.put("script", - "if (doc['" + DEPENDENT_VARIABLE_FIELD + "'].size() > 0) { emit(doc['" + DEPENDENT_VARIABLE_FIELD + "'].value); }"); + dependentVariableRuntimeFieldMapping.put( + "script", + "if (doc['" + DEPENDENT_VARIABLE_FIELD + "'].size() > 0) { emit(doc['" + DEPENDENT_VARIABLE_FIELD + "'].value); }" + ); Map runtimeFields = new HashMap<>(); runtimeFields.put(numericRuntimeField, numericRuntimeFieldMapping); runtimeFields.put(dependentVariableRuntimeField, dependentVariableRuntimeFieldMapping); - DataFrameAnalyticsConfig config = new DataFrameAnalyticsConfig.Builder() - .setId(jobId) + DataFrameAnalyticsConfig config = new DataFrameAnalyticsConfig.Builder().setId(jobId) .setSource(new DataFrameAnalyticsSource(new String[] { sourceIndex }, null, null, runtimeFields)) .setDest(new DataFrameAnalyticsDest(destIndex, null)) .setAnalyzedFields(new FetchSourceContext(true, new String[] { numericRuntimeField, dependentVariableRuntimeField }, null)) - .setAnalysis(new Regression( - dependentVariableRuntimeField, - BoostedTreeParams.builder().setNumTopFeatureImportanceValues(1).build(), - null, - null, - null, - null, - null, - null, - null)) + .setAnalysis( + new Regression( + dependentVariableRuntimeField, + BoostedTreeParams.builder().setNumTopFeatureImportanceValues(1).build(), + null, + null, + null, + null, + null, + null, + null + ) + ) .build(); putAnalytics(config); @@ -735,7 +794,7 @@ public void testWithSearchRuntimeMappings() throws Exception { assertThat(resultsObject.containsKey("is_training"), is(true)); assertThat(resultsObject.get("is_training"), is(destDoc.containsKey(DEPENDENT_VARIABLE_FIELD))); @SuppressWarnings("unchecked") - List> importanceArray = (List>)resultsObject.get("feature_importance"); + List> importanceArray = (List>) resultsObject.get("feature_importance"); assertThat(importanceArray, hasSize(1)); assertThat(importanceArray.get(0), hasEntry("feature_name", numericRuntimeField)); } @@ -745,7 +804,8 @@ public void testWithSearchRuntimeMappings() throws Exception { assertModelStatePersisted(stateDocId()); assertExactlyOneInferenceModelPersisted(jobId); assertMlResultsFieldMappings(destIndex, predictedClassField, "double"); - assertThatAuditMessagesMatch(jobId, + assertThatAuditMessagesMatch( + jobId, "Created analytics with type [regression]", "Estimated memory usage [", "Starting analytics on node", @@ -756,7 +816,8 @@ public void testWithSearchRuntimeMappings() throws Exception { "Started loading data", "Started analyzing", "Started writing results", - "Finished analysis"); + "Finished analysis" + ); } public void testPreview() throws Exception { @@ -775,7 +836,11 @@ public void testPreviewWithProcessors() throws Exception { initialize("processed_preview_analytics"); indexData(sourceIndex, 300, 50); - DataFrameAnalyticsConfig config = buildAnalytics(jobId, sourceIndex, destIndex, null, + DataFrameAnalyticsConfig config = buildAnalytics( + jobId, + sourceIndex, + destIndex, + null, new Regression( DEPENDENT_VARIABLE_FIELD, BoostedTreeParams.builder().setNumTopFeatureImportanceValues(1).build(), @@ -785,10 +850,14 @@ public void testPreviewWithProcessors() throws Exception { null, null, Arrays.asList( - new OneHotEncoding(DISCRETE_NUMERICAL_FEATURE_FIELD, - Collections.singletonMap(DISCRETE_NUMERICAL_FEATURE_VALUES.get(0).toString(), "tenner"), true) + new OneHotEncoding( + DISCRETE_NUMERICAL_FEATURE_FIELD, + Collections.singletonMap(DISCRETE_NUMERICAL_FEATURE_VALUES.get(0).toString(), "tenner"), + true + ) ), - null) + null + ) ); putAnalytics(config); List> preview = previewDataFrame(jobId).getFeatureValues(); @@ -809,22 +878,28 @@ static void indexData(String sourceIndex, int numTrainingRows, int numNonTrainin } static void indexData(String sourceIndex, int numTrainingRows, int numNonTrainingRows, boolean dataStream) { - String mapping = "{\n" + - " \"properties\": {\n" + - " \"@timestamp\": {\n" + - " \"type\": \"date\"\n" + - " }," + - " \""+ NUMERICAL_FEATURE_FIELD + "\": {\n" + - " \"type\": \"double\"\n" + - " }," + - " \"" + DISCRETE_NUMERICAL_FEATURE_FIELD + "\": {\n" + - " \"type\": \"unsigned_long\"\n" + - " }," + - " \"" + DEPENDENT_VARIABLE_FIELD + "\": {\n" + - " \"type\": \"double\"\n" + - " }" + - " }\n" + - " }"; + String mapping = "{\n" + + " \"properties\": {\n" + + " \"@timestamp\": {\n" + + " \"type\": \"date\"\n" + + " }," + + " \"" + + NUMERICAL_FEATURE_FIELD + + "\": {\n" + + " \"type\": \"double\"\n" + + " }," + + " \"" + + DISCRETE_NUMERICAL_FEATURE_FIELD + + "\": {\n" + + " \"type\": \"unsigned_long\"\n" + + " }," + + " \"" + + DEPENDENT_VARIABLE_FIELD + + "\": {\n" + + " \"type\": \"double\"\n" + + " }" + + " }\n" + + " }"; if (dataStream) { try { createDataStreamAndTemplate(sourceIndex, mapping); @@ -832,27 +907,33 @@ static void indexData(String sourceIndex, int numTrainingRows, int numNonTrainin throw new ElasticsearchException(ex); } } else { - client().admin().indices().prepareCreate(sourceIndex) - .setMapping(mapping) - .get(); + client().admin().indices().prepareCreate(sourceIndex).setMapping(mapping).get(); } - BulkRequestBuilder bulkRequestBuilder = client().prepareBulk() - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); + BulkRequestBuilder bulkRequestBuilder = client().prepareBulk().setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); for (int i = 0; i < numTrainingRows; i++) { List source = List.of( - NUMERICAL_FEATURE_FIELD, NUMERICAL_FEATURE_VALUES.get(i % NUMERICAL_FEATURE_VALUES.size()), - DISCRETE_NUMERICAL_FEATURE_FIELD, DISCRETE_NUMERICAL_FEATURE_VALUES.get(i % DISCRETE_NUMERICAL_FEATURE_VALUES.size()), - DEPENDENT_VARIABLE_FIELD, DEPENDENT_VARIABLE_VALUES.get(i % DEPENDENT_VARIABLE_VALUES.size()), - "@timestamp", Instant.now().toEpochMilli()); + NUMERICAL_FEATURE_FIELD, + NUMERICAL_FEATURE_VALUES.get(i % NUMERICAL_FEATURE_VALUES.size()), + DISCRETE_NUMERICAL_FEATURE_FIELD, + DISCRETE_NUMERICAL_FEATURE_VALUES.get(i % DISCRETE_NUMERICAL_FEATURE_VALUES.size()), + DEPENDENT_VARIABLE_FIELD, + DEPENDENT_VARIABLE_VALUES.get(i % DEPENDENT_VARIABLE_VALUES.size()), + "@timestamp", + Instant.now().toEpochMilli() + ); IndexRequest indexRequest = new IndexRequest(sourceIndex).source(source.toArray()).opType(DocWriteRequest.OpType.CREATE); bulkRequestBuilder.add(indexRequest); } for (int i = numTrainingRows; i < numTrainingRows + numNonTrainingRows; i++) { List source = List.of( - NUMERICAL_FEATURE_FIELD, NUMERICAL_FEATURE_VALUES.get(i % NUMERICAL_FEATURE_VALUES.size()), - DISCRETE_NUMERICAL_FEATURE_FIELD, DISCRETE_NUMERICAL_FEATURE_VALUES.get(i % DISCRETE_NUMERICAL_FEATURE_VALUES.size()), - "@timestamp", Instant.now().toEpochMilli()); + NUMERICAL_FEATURE_FIELD, + NUMERICAL_FEATURE_VALUES.get(i % NUMERICAL_FEATURE_VALUES.size()), + DISCRETE_NUMERICAL_FEATURE_FIELD, + DISCRETE_NUMERICAL_FEATURE_VALUES.get(i % DISCRETE_NUMERICAL_FEATURE_VALUES.size()), + "@timestamp", + Instant.now().toEpochMilli() + ); IndexRequest indexRequest = new IndexRequest(sourceIndex).source(source.toArray()).opType(DocWriteRequest.OpType.CREATE); bulkRequestBuilder.add(indexRequest); } diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ReopenJobWithGapIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ReopenJobWithGapIT.java index bd47a4c056012..4a79f0e376833 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ReopenJobWithGapIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ReopenJobWithGapIT.java @@ -40,7 +40,8 @@ public void cleanUpTest() { public void test() throws Exception { AnalysisConfig.Builder analysisConfig = new AnalysisConfig.Builder( - Collections.singletonList(new Detector.Builder("count", null).build())); + Collections.singletonList(new Detector.Builder("count", null).build()) + ); analysisConfig.setBucketSpan(TimeValue.timeValueSeconds(BUCKET_SPAN_SECONDS)); DataDescription.Builder dataDescription = new DataDescription.Builder(); dataDescription.setTimeFormat("epoch"); diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ResetJobIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ResetJobIT.java index 6e9b07b9d3e3a..6112a76174562 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ResetJobIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ResetJobIT.java @@ -41,8 +41,11 @@ public void testReset() throws IOException { Job.Builder job = createJob("test-reset", bucketSpan); openJob(job.getId()); - postData(job.getId(), generateData(startTime, bucketSpan, bucketCount + 1, bucketIndex -> randomIntBetween(100, 200)) - .stream().collect(Collectors.joining())); + postData( + job.getId(), + generateData(startTime, bucketSpan, bucketCount + 1, bucketIndex -> randomIntBetween(100, 200)).stream() + .collect(Collectors.joining()) + ); closeJob(job.getId()); List buckets = getBuckets(job.getId()); diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/RevertModelSnapshotIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/RevertModelSnapshotIT.java index 0ec7596557bd3..174b1b06634f2 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/RevertModelSnapshotIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/RevertModelSnapshotIT.java @@ -9,16 +9,16 @@ import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.support.WriteRequest; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.core.TimeValue; +import org.elasticsearch.index.query.QueryBuilders; +import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.search.SearchHits; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.index.query.QueryBuilders; -import org.elasticsearch.rest.RestStatus; -import org.elasticsearch.search.SearchHits; import org.elasticsearch.xpack.core.ml.action.GetJobsStatsAction; import org.elasticsearch.xpack.core.ml.action.RevertModelSnapshotAction; import org.elasticsearch.xpack.core.ml.annotations.Annotation; @@ -84,8 +84,13 @@ public void testRevertToEmptySnapshot() throws Exception { TimeValue bucketSpan = TimeValue.timeValueHours(1); long startTime = 1491004800000L; - String data = generateData(startTime, bucketSpan, 20, Arrays.asList("foo"), - (bucketIndex, series) -> bucketIndex == 19 ? 100.0 : 10.0).stream().collect(Collectors.joining()); + String data = generateData( + startTime, + bucketSpan, + 20, + Arrays.asList("foo"), + (bucketIndex, series) -> bucketIndex == 19 ? 100.0 : 10.0 + ).stream().collect(Collectors.joining()); Job.Builder job = buildAndRegisterJob(jobId, bucketSpan); openJob(job.getId()); @@ -125,8 +130,11 @@ private void testRunJobInTwoPartsAndRevertSnapshotAndRunToCompletion(String jobI Job.Builder job = buildAndRegisterJob(jobId, bucketSpan); openJob(job.getId()); - postData(job.getId(), generateData(startTime, bucketSpan, 10, Arrays.asList("foo"), - (bucketIndex, series) -> bucketIndex == 5 ? 100.0 : 10.0).stream().collect(Collectors.joining())); + postData( + job.getId(), + generateData(startTime, bucketSpan, 10, Arrays.asList("foo"), (bucketIndex, series) -> bucketIndex == 5 ? 100.0 : 10.0).stream() + .collect(Collectors.joining()) + ); flushJob(job.getId(), true); closeJob(job.getId()); @@ -141,8 +149,16 @@ private void testRunJobInTwoPartsAndRevertSnapshotAndRunToCompletion(String jobI waitUntil(() -> false, 1, TimeUnit.SECONDS); openJob(job.getId()); - postData(job.getId(), generateData(startTime + 10 * bucketSpan.getMillis(), bucketSpan, 10, Arrays.asList("foo", "bar"), - (bucketIndex, series) -> 10.0).stream().collect(Collectors.joining())); + postData( + job.getId(), + generateData( + startTime + 10 * bucketSpan.getMillis(), + bucketSpan, + 10, + Arrays.asList("foo", "bar"), + (bucketIndex, series) -> 10.0 + ).stream().collect(Collectors.joining()) + ); closeJob(job.getId()); ModelSizeStats modelSizeStats2 = getJobStats(job.getId()).get(0).getModelSizeStats(); @@ -182,7 +198,8 @@ private void testRunJobInTwoPartsAndRevertSnapshotAndRunToCompletion(String jobI assertThat( revertModelSnapshot(job.getId(), revertSnapshot.getSnapshotId(), deleteInterveningResults).status(), - equalTo(RestStatus.OK)); + equalTo(RestStatus.OK) + ); GetJobsStatsAction.Response.JobStats statsAfterRevert = getJobStats(job.getId()).get(0); @@ -205,8 +222,16 @@ private void testRunJobInTwoPartsAndRevertSnapshotAndRunToCompletion(String jobI // Re-run 2nd half of data openJob(job.getId()); - postData(job.getId(), generateData(startTime + 10 * bucketSpan.getMillis(), bucketSpan, 10, Arrays.asList("foo", "bar"), - (bucketIndex, series) -> 10.0).stream().collect(Collectors.joining())); + postData( + job.getId(), + generateData( + startTime + 10 * bucketSpan.getMillis(), + bucketSpan, + 10, + Arrays.asList("foo", "bar"), + (bucketIndex, series) -> 10.0 + ).stream().collect(Collectors.joining()) + ); closeJob(job.getId()); List finalPostRevertBuckets = getBuckets(job.getId()); @@ -229,8 +254,13 @@ private Job.Builder buildAndRegisterJob(String jobId, TimeValue bucketSpan) thro return job; } - private static List generateData(long timestamp, TimeValue bucketSpan, int bucketCount, List series, - BiFunction timeAndSeriesToValueFunction) throws IOException { + private static List generateData( + long timestamp, + TimeValue bucketSpan, + int bucketCount, + List series, + BiFunction timeAndSeriesToValueFunction + ) throws IOException { List data = new ArrayList<>(); long now = timestamp; for (int i = 0; i < bucketCount; i++) { @@ -254,14 +284,17 @@ record = new HashMap<>(); private Quantiles getQuantiles(String jobId) { SearchResponse response = client().prepareSearch(".ml-state*") - .setQuery(QueryBuilders.idsQuery().addIds(Quantiles.documentId(jobId))) - .setSize(1) - .get(); + .setQuery(QueryBuilders.idsQuery().addIds(Quantiles.documentId(jobId))) + .setSize(1) + .get(); SearchHits hits = response.getHits(); assertThat(hits.getTotalHits().value, equalTo(1L)); try { - XContentParser parser = JsonXContent.jsonXContent - .createParser(null, LoggingDeprecationHandler.INSTANCE, hits.getAt(0).getSourceAsString()); + XContentParser parser = JsonXContent.jsonXContent.createParser( + null, + LoggingDeprecationHandler.INSTANCE, + hits.getAt(0).getSourceAsString() + ); return Quantiles.LENIENT_PARSER.apply(parser, null); } catch (IOException e) { throw new IllegalStateException(e); @@ -269,14 +302,12 @@ private Quantiles getQuantiles(String jobId) { } private static IndexRequest randomAnnotationIndexRequest(String jobId, Instant timestamp, Event event) throws IOException { - Annotation annotation = new Annotation.Builder(randomAnnotation(jobId)) - .setTimestamp(Date.from(timestamp)) + Annotation annotation = new Annotation.Builder(randomAnnotation(jobId)).setTimestamp(Date.from(timestamp)) .setCreateUsername(XPackUser.NAME) .setEvent(event) .build(); try (XContentBuilder xContentBuilder = annotation.toXContent(XContentFactory.jsonBuilder(), ToXContent.EMPTY_PARAMS)) { - return new IndexRequest(AnnotationIndex.WRITE_ALIAS_NAME) - .source(xContentBuilder) + return new IndexRequest(AnnotationIndex.WRITE_ALIAS_NAME).source(xContentBuilder) .setRequireAlias(true) .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); } diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/RunDataFrameAnalyticsIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/RunDataFrameAnalyticsIT.java index 89865e7deb0b6..cb931bedce339 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/RunDataFrameAnalyticsIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/RunDataFrameAnalyticsIT.java @@ -15,10 +15,10 @@ import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.support.WriteRequest; import org.elasticsearch.common.unit.ByteSizeValue; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.ml.action.GetDataFrameAnalyticsStatsAction; import org.elasticsearch.xpack.core.ml.action.NodeAcknowledgedResponse; import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsConfig; @@ -56,7 +56,9 @@ public void cleanup() { public void testOutlierDetectionWithFewDocuments() throws Exception { String sourceIndex = "test-outlier-detection-with-few-docs"; - client().admin().indices().prepareCreate(sourceIndex) + client().admin() + .indices() + .prepareCreate(sourceIndex) .setMapping("numeric_1", "type=double", "numeric_2", "type=unsigned_long", "categorical_1", "type=keyword") .get(); @@ -78,8 +80,13 @@ public void testOutlierDetectionWithFewDocuments() throws Exception { } String id = "test_outlier_detection_with_few_docs"; - DataFrameAnalyticsConfig config = buildAnalytics(id, sourceIndex, sourceIndex + "-results", null, - new OutlierDetection.Builder().build()); + DataFrameAnalyticsConfig config = buildAnalytics( + id, + sourceIndex, + sourceIndex + "-results", + null, + new OutlierDetection.Builder().build() + ); putAnalytics(config); assertIsStopped(id); @@ -127,7 +134,8 @@ public void testOutlierDetectionWithFewDocuments() throws Exception { assertProgressComplete(id); assertThat(searchStoredProgress(id).getHits().getTotalHits().value, equalTo(1L)); - assertThatAuditMessagesMatch(id, + assertThatAuditMessagesMatch( + id, "Created analytics with type [outlier_detection]", "Estimated memory usage [", "Starting analytics on node", @@ -138,13 +146,16 @@ public void testOutlierDetectionWithFewDocuments() throws Exception { "Started loading data", "Started analyzing", "Started writing results", - "Finished analysis"); + "Finished analysis" + ); } public void testPreview() throws Exception { String sourceIndex = "test-outlier-detection-preview"; - client().admin().indices().prepareCreate(sourceIndex) + client().admin() + .indices() + .prepareCreate(sourceIndex) .setMapping("numeric_1", "type=double", "numeric_2", "type=unsigned_long", "categorical_1", "type=keyword") .get(); @@ -166,8 +177,13 @@ public void testPreview() throws Exception { } String id = "test_outlier_detection_preview"; - DataFrameAnalyticsConfig config = buildAnalytics(id, sourceIndex, sourceIndex + "-results", null, - new OutlierDetection.Builder().build()); + DataFrameAnalyticsConfig config = buildAnalytics( + id, + sourceIndex, + sourceIndex + "-results", + null, + new OutlierDetection.Builder().build() + ); putAnalytics(config); List> preview = previewDataFrame(id).getFeatureValues(); for (Map feature : preview) { @@ -179,7 +195,9 @@ public void testPreview() throws Exception { public void testOutlierDetectionWithEnoughDocumentsToScroll() throws Exception { String sourceIndex = "test-outlier-detection-with-enough-docs-to-scroll"; - client().admin().indices().prepareCreate(sourceIndex) + client().admin() + .indices() + .prepareCreate(sourceIndex) .setMapping("numeric_1", "type=double", "numeric_2", "type=float", "categorical_1", "type=keyword") .get(); @@ -198,8 +216,13 @@ public void testOutlierDetectionWithEnoughDocumentsToScroll() throws Exception { } String id = "test_outlier_detection_with_enough_docs_to_scroll"; - DataFrameAnalyticsConfig config = buildAnalytics(id, sourceIndex, sourceIndex + "-results", "custom_ml", - new OutlierDetection.Builder().build()); + DataFrameAnalyticsConfig config = buildAnalytics( + id, + sourceIndex, + sourceIndex + "-results", + "custom_ml", + new OutlierDetection.Builder().build() + ); putAnalytics(config); assertIsStopped(id); @@ -215,12 +238,14 @@ public void testOutlierDetectionWithEnoughDocumentsToScroll() throws Exception { // Check they all have an outlier_score searchResponse = client().prepareSearch(config.getDest().getIndex()) .setTrackTotalHits(true) - .setQuery(QueryBuilders.existsQuery("custom_ml.outlier_score")).get(); + .setQuery(QueryBuilders.existsQuery("custom_ml.outlier_score")) + .get(); assertThat(searchResponse.getHits().getTotalHits().value, equalTo((long) docCount)); assertProgressComplete(id); assertThat(searchStoredProgress(id).getHits().getTotalHits().value, equalTo(1L)); - assertThatAuditMessagesMatch(id, + assertThatAuditMessagesMatch( + id, "Created analytics with type [outlier_detection]", "Estimated memory usage [", "Starting analytics on node", @@ -231,7 +256,8 @@ public void testOutlierDetectionWithEnoughDocumentsToScroll() throws Exception { "Started loading data", "Started analyzing", "Started writing results", - "Finished analysis"); + "Finished analysis" + ); } public void testOutlierDetectionWithMoreFieldsThanDocValueFieldLimit() throws Exception { @@ -246,7 +272,8 @@ public void testOutlierDetectionWithMoreFieldsThanDocValueFieldLimit() throws Ex GetSettingsResponse docValueLimitSetting = client().admin().indices().getSettings(getSettingsRequest).actionGet(); int docValueLimit = IndexSettings.MAX_DOCVALUE_FIELDS_SEARCH_SETTING.get( - docValueLimitSetting.getIndexToSettings().valuesIt().next()); + docValueLimitSetting.getIndexToSettings().valuesIt().next() + ); BulkRequestBuilder bulkRequestBuilder = client().prepareBulk(); bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); @@ -272,8 +299,13 @@ public void testOutlierDetectionWithMoreFieldsThanDocValueFieldLimit() throws Ex } String id = "test_outlier_detection_with_more_fields_than_docvalue_limit"; - DataFrameAnalyticsConfig config = buildAnalytics(id, sourceIndex, sourceIndex + "-results", null, - new OutlierDetection.Builder().build()); + DataFrameAnalyticsConfig config = buildAnalytics( + id, + sourceIndex, + sourceIndex + "-results", + null, + new OutlierDetection.Builder().build() + ); putAnalytics(config); assertIsStopped(id); @@ -304,7 +336,8 @@ public void testOutlierDetectionWithMoreFieldsThanDocValueFieldLimit() throws Ex assertProgressComplete(id); assertThat(searchStoredProgress(id).getHits().getTotalHits().value, equalTo(1L)); - assertThatAuditMessagesMatch(id, + assertThatAuditMessagesMatch( + id, "Created analytics with type [outlier_detection]", "Estimated memory usage [", "Starting analytics on node", @@ -315,13 +348,16 @@ public void testOutlierDetectionWithMoreFieldsThanDocValueFieldLimit() throws Ex "Started loading data", "Started analyzing", "Started writing results", - "Finished analysis"); + "Finished analysis" + ); } public void testStopOutlierDetectionWithEnoughDocumentsToScroll() throws Exception { String sourceIndex = "test-stop-outlier-detection-with-enough-docs-to-scroll"; - client().admin().indices().prepareCreate(sourceIndex) + client().admin() + .indices() + .prepareCreate(sourceIndex) .setMapping("numeric_1", "type=double", "numeric_2", "type=float", "categorical_1", "type=keyword") .get(); @@ -340,8 +376,13 @@ public void testStopOutlierDetectionWithEnoughDocumentsToScroll() throws Excepti } String id = "test_stop_outlier_detection_with_enough_docs_to_scroll"; - DataFrameAnalyticsConfig config = buildAnalytics(id, sourceIndex, sourceIndex + "-results", "custom_ml", - new OutlierDetection.Builder().build()); + DataFrameAnalyticsConfig config = buildAnalytics( + id, + sourceIndex, + sourceIndex + "-results", + "custom_ml", + new OutlierDetection.Builder().build() + ); putAnalytics(config); assertIsStopped(id); @@ -359,20 +400,23 @@ public void testStopOutlierDetectionWithEnoughDocumentsToScroll() throws Excepti if (searchResponse.getHits().getTotalHits().value == docCount) { searchResponse = client().prepareSearch(config.getDest().getIndex()) .setTrackTotalHits(true) - .setQuery(QueryBuilders.existsQuery("custom_ml.outlier_score")).get(); + .setQuery(QueryBuilders.existsQuery("custom_ml.outlier_score")) + .get(); logger.debug("We stopped during analysis: [{}] < [{}]", searchResponse.getHits().getTotalHits().value, docCount); assertThat(searchResponse.getHits().getTotalHits().value, lessThan((long) docCount)); } else { logger.debug("We stopped during reindexing: [{}] < [{}]", searchResponse.getHits().getTotalHits().value, docCount); } - assertThatAuditMessagesMatch(id, + assertThatAuditMessagesMatch( + id, "Created analytics with type [outlier_detection]", "Estimated memory usage [", "Starting analytics on node", "Started analytics", "Creating destination index [test-stop-outlier-detection-with-enough-docs-to-scroll-results]", - "Stopped analytics"); + "Stopped analytics" + ); } public void testOutlierDetectionWithMultipleSourceIndices() throws Exception { @@ -381,11 +425,15 @@ public void testOutlierDetectionWithMultipleSourceIndices() throws Exception { String destIndex = "test-outlier-detection-with-multiple-source-indices-results"; String[] sourceIndex = new String[] { sourceIndex1, sourceIndex2 }; - client().admin().indices().prepareCreate(sourceIndex1) + client().admin() + .indices() + .prepareCreate(sourceIndex1) .setMapping("numeric_1", "type=double", "numeric_2", "type=float", "categorical_1", "type=keyword") .get(); - client().admin().indices().prepareCreate(sourceIndex2) + client().admin() + .indices() + .prepareCreate(sourceIndex2) .setMapping("numeric_1", "type=double", "numeric_2", "type=float", "categorical_1", "type=keyword") .get(); @@ -405,8 +453,7 @@ public void testOutlierDetectionWithMultipleSourceIndices() throws Exception { } String id = "test_outlier_detection_with_multiple_source_indices"; - DataFrameAnalyticsConfig config = new DataFrameAnalyticsConfig.Builder() - .setId(id) + DataFrameAnalyticsConfig config = new DataFrameAnalyticsConfig.Builder().setId(id) .setSource(new DataFrameAnalyticsSource(sourceIndex, null, null, null)) .setDest(new DataFrameAnalyticsDest(destIndex, null)) .setAnalysis(new OutlierDetection.Builder().build()) @@ -426,12 +473,14 @@ public void testOutlierDetectionWithMultipleSourceIndices() throws Exception { // Check they all have an outlier_score searchResponse = client().prepareSearch(config.getDest().getIndex()) .setTrackTotalHits(true) - .setQuery(QueryBuilders.existsQuery("ml.outlier_score")).get(); + .setQuery(QueryBuilders.existsQuery("ml.outlier_score")) + .get(); assertThat(searchResponse.getHits().getTotalHits().value, equalTo((long) bulkRequestBuilder.numberOfActions())); assertProgressComplete(id); assertThat(searchStoredProgress(id).getHits().getTotalHits().value, equalTo(1L)); - assertThatAuditMessagesMatch(id, + assertThatAuditMessagesMatch( + id, "Created analytics with type [outlier_detection]", "Estimated memory usage [", "Starting analytics on node", @@ -442,18 +491,23 @@ public void testOutlierDetectionWithMultipleSourceIndices() throws Exception { "Started loading data", "Started analyzing", "Started writing results", - "Finished analysis"); + "Finished analysis" + ); } public void testOutlierDetectionWithPreExistingDestIndex() throws Exception { String sourceIndex = "test-outlier-detection-with-pre-existing-dest-index"; String destIndex = "test-outlier-detection-with-pre-existing-dest-index-results"; - client().admin().indices().prepareCreate(sourceIndex) + client().admin() + .indices() + .prepareCreate(sourceIndex) .setMapping("numeric_1", "type=double", "numeric_2", "type=float", "categorical_1", "type=keyword") .get(); - client().admin().indices().prepareCreate(destIndex) + client().admin() + .indices() + .prepareCreate(destIndex) .setMapping("numeric_1", "type=double", "numeric_2", "type=float", "categorical_1", "type=keyword") .get(); @@ -487,12 +541,14 @@ public void testOutlierDetectionWithPreExistingDestIndex() throws Exception { // Check they all have an outlier_score searchResponse = client().prepareSearch(config.getDest().getIndex()) .setTrackTotalHits(true) - .setQuery(QueryBuilders.existsQuery("ml.outlier_score")).get(); + .setQuery(QueryBuilders.existsQuery("ml.outlier_score")) + .get(); assertThat(searchResponse.getHits().getTotalHits().value, equalTo((long) bulkRequestBuilder.numberOfActions())); assertProgressComplete(id); assertThat(searchStoredProgress(id).getHits().getTotalHits().value, equalTo(1L)); - assertThatAuditMessagesMatch(id, + assertThatAuditMessagesMatch( + id, "Created analytics with type [outlier_detection]", "Estimated memory usage [", "Starting analytics on node", @@ -503,21 +559,22 @@ public void testOutlierDetectionWithPreExistingDestIndex() throws Exception { "Started loading data", "Started analyzing", "Started writing results", - "Finished analysis"); + "Finished analysis" + ); } public void testModelMemoryLimitLowerThanEstimatedMemoryUsage() throws Exception { String sourceIndex = "test-model-memory-limit"; - client().admin().indices().prepareCreate(sourceIndex) + client().admin() + .indices() + .prepareCreate(sourceIndex) .setMapping("col_1", "type=double", "col_2", "type=float", "col_3", "type=keyword") .get(); BulkRequestBuilder bulkRequestBuilder = client().prepareBulk().setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); for (int i = 0; i < 10000; i++) { // This number of rows should make memory usage estimate greater than 1MB - IndexRequest indexRequest = new IndexRequest(sourceIndex) - .id("doc_" + i) - .source("col_1", 1.0, "col_2", 1.0, "col_3", "str"); + IndexRequest indexRequest = new IndexRequest(sourceIndex).id("doc_" + i).source("col_1", 1.0, "col_2", 1.0, "col_3", "str"); bulkRequestBuilder.add(indexRequest); } BulkResponse bulkResponse = bulkRequestBuilder.get(); @@ -527,8 +584,7 @@ public void testModelMemoryLimitLowerThanEstimatedMemoryUsage() throws Exception String id = "test_model_memory_limit_lower_than_estimated_memory_usage"; ByteSizeValue modelMemoryLimit = ByteSizeValue.ofMb(1); - DataFrameAnalyticsConfig config = new DataFrameAnalyticsConfig.Builder() - .setId(id) + DataFrameAnalyticsConfig config = new DataFrameAnalyticsConfig.Builder().setId(id) .setSource(new DataFrameAnalyticsSource(new String[] { sourceIndex }, null, null, null)) .setDest(new DataFrameAnalyticsDest(sourceIndex + "-results", null)) .setAnalysis(new OutlierDetection.Builder().build()) @@ -537,7 +593,7 @@ public void testModelMemoryLimitLowerThanEstimatedMemoryUsage() throws Exception putAnalytics(config); assertIsStopped(id); - //should not throw + // should not throw startAnalytics(id); waitUntilAnalyticsIsFailed(id); forceStopAnalytics(id); @@ -547,14 +603,14 @@ public void testModelMemoryLimitLowerThanEstimatedMemoryUsage() throws Exception public void testLazyAssignmentWithModelMemoryLimitTooHighForAssignment() throws Exception { String sourceIndex = "test-lazy-assign-model-memory-limit-too-high"; - client().admin().indices().prepareCreate(sourceIndex) + client().admin() + .indices() + .prepareCreate(sourceIndex) .setMapping("col_1", "type=double", "col_2", "type=float", "col_3", "type=keyword") .get(); BulkRequestBuilder bulkRequestBuilder = client().prepareBulk().setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - IndexRequest indexRequest = new IndexRequest(sourceIndex) - .id("doc_1") - .source("col_1", 1.0, "col_2", 1.0, "col_3", "str"); + IndexRequest indexRequest = new IndexRequest(sourceIndex).id("doc_1").source("col_1", 1.0, "col_2", 1.0, "col_3", "str"); bulkRequestBuilder.add(indexRequest); BulkResponse bulkResponse = bulkRequestBuilder.get(); if (bulkResponse.hasFailures()) { @@ -564,8 +620,7 @@ public void testLazyAssignmentWithModelMemoryLimitTooHighForAssignment() throws String id = "test_lazy_assign_model_memory_limit_too_high"; // Assuming a 1TB job will never fit on the test machine - increase this when machines get really big! ByteSizeValue modelMemoryLimit = ByteSizeValue.ofTb(1); - DataFrameAnalyticsConfig config = new DataFrameAnalyticsConfig.Builder() - .setId(id) + DataFrameAnalyticsConfig config = new DataFrameAnalyticsConfig.Builder().setId(id) .setSource(new DataFrameAnalyticsSource(new String[] { sourceIndex }, null, null, null)) .setDest(new DataFrameAnalyticsDest(sourceIndex + "-results", null)) .setAnalysis(new OutlierDetection.Builder().build()) @@ -590,18 +645,22 @@ public void testLazyAssignmentWithModelMemoryLimitTooHighForAssignment() throws stopAnalytics(id); waitUntilAnalyticsIsStopped(id); - assertThatAuditMessagesMatch(id, + assertThatAuditMessagesMatch( + id, "Created analytics with type [outlier_detection]", "Estimated memory usage [", "Job requires at least [1tb] free memory on a machine learning capable node to run", "Started analytics", - "Stopped analytics"); + "Stopped analytics" + ); } public void testOutlierDetectionStopAndRestart() throws Exception { String sourceIndex = "test-outlier-detection-stop-and-restart"; - client().admin().indices().prepareCreate(sourceIndex) + client().admin() + .indices() + .prepareCreate(sourceIndex) .setMapping("numeric_1", "type=double", "numeric_2", "type=float", "categorical_1", "type=keyword") .get(); @@ -620,8 +679,13 @@ public void testOutlierDetectionStopAndRestart() throws Exception { } String id = "test_outlier_detection_stop_and_restart"; - DataFrameAnalyticsConfig config = buildAnalytics(id, sourceIndex, sourceIndex + "-results", "custom_ml", - new OutlierDetection.Builder().build()); + DataFrameAnalyticsConfig config = buildAnalytics( + id, + sourceIndex, + sourceIndex + "-results", + "custom_ml", + new OutlierDetection.Builder().build() + ); putAnalytics(config); assertIsStopped(id); @@ -654,7 +718,8 @@ public void testOutlierDetectionStopAndRestart() throws Exception { // Check they all have an outlier_score searchResponse = client().prepareSearch(config.getDest().getIndex()) .setTrackTotalHits(true) - .setQuery(QueryBuilders.existsQuery("custom_ml.outlier_score")).get(); + .setQuery(QueryBuilders.existsQuery("custom_ml.outlier_score")) + .get(); assertThat(searchResponse.getHits().getTotalHits().value, equalTo((long) docCount)); assertProgressComplete(id); @@ -664,7 +729,9 @@ public void testOutlierDetectionStopAndRestart() throws Exception { public void testOutlierDetectionWithCustomParams() throws Exception { String sourceIndex = "test-outlier-detection-with-custom-params"; - client().admin().indices().prepareCreate(sourceIndex) + client().admin() + .indices() + .prepareCreate(sourceIndex) .setMapping("numeric_1", "type=double", "numeric_2", "type=float", "categorical_1", "type=keyword") .get(); @@ -686,15 +753,19 @@ public void testOutlierDetectionWithCustomParams() throws Exception { } String id = "test_outlier_detection_with_custom_params"; - DataFrameAnalyticsConfig config = buildAnalytics(id, sourceIndex, sourceIndex + "-results", null, - new OutlierDetection.Builder() - .setNNeighbors(3) + DataFrameAnalyticsConfig config = buildAnalytics( + id, + sourceIndex, + sourceIndex + "-results", + null, + new OutlierDetection.Builder().setNNeighbors(3) .setMethod(OutlierDetection.Method.DISTANCE_KNN) .setFeatureInfluenceThreshold(0.01) .setComputeFeatureInfluence(false) .setOutlierFraction(0.04) .setStandardizationEnabled(true) - .build()); + .build() + ); putAnalytics(config); assertIsStopped(id); @@ -739,7 +810,8 @@ public void testOutlierDetectionWithCustomParams() throws Exception { assertProgressComplete(id); assertThat(searchStoredProgress(id).getHits().getTotalHits().value, equalTo(1L)); - assertThatAuditMessagesMatch(id, + assertThatAuditMessagesMatch( + id, "Created analytics with type [outlier_detection]", "Estimated memory usage [", "Starting analytics on node", @@ -750,18 +822,17 @@ public void testOutlierDetectionWithCustomParams() throws Exception { "Started loading data", "Started analyzing", "Started writing results", - "Finished analysis"); + "Finished analysis" + ); } public void testOutlierDetection_GivenIndexWithRuntimeFields() throws Exception { String sourceIndex = "test-outlier-detection-with-index-with-runtime-fields"; - String mappings = "{\"dynamic\":false, \"runtime\": { \"runtime_numeric\": " + - "{ \"type\": \"double\", \"script\": { \"source\": \"emit(params._source.numeric)\", \"lang\": \"painless\" } } }}"; + String mappings = "{\"dynamic\":false, \"runtime\": { \"runtime_numeric\": " + + "{ \"type\": \"double\", \"script\": { \"source\": \"emit(params._source.numeric)\", \"lang\": \"painless\" } } }}"; - client().admin().indices().prepareCreate(sourceIndex) - .setMapping(mappings) - .get(); + client().admin().indices().prepareCreate(sourceIndex).setMapping(mappings).get(); BulkRequestBuilder bulkRequestBuilder = client().prepareBulk(); bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); @@ -781,8 +852,13 @@ public void testOutlierDetection_GivenIndexWithRuntimeFields() throws Exception } String id = "test_outlier_detection_with_index_with_runtime_mappings"; - DataFrameAnalyticsConfig config = buildAnalytics(id, sourceIndex, sourceIndex + "-results", null, - new OutlierDetection.Builder().build()); + DataFrameAnalyticsConfig config = buildAnalytics( + id, + sourceIndex, + sourceIndex + "-results", + null, + new OutlierDetection.Builder().build() + ); putAnalytics(config); assertIsStopped(id); @@ -835,7 +911,8 @@ public void testOutlierDetection_GivenIndexWithRuntimeFields() throws Exception assertProgressComplete(id); assertThat(searchStoredProgress(id).getHits().getTotalHits().value, equalTo(1L)); - assertThatAuditMessagesMatch(id, + assertThatAuditMessagesMatch( + id, "Created analytics with type [outlier_detection]", "Estimated memory usage [", "Starting analytics on node", @@ -846,7 +923,8 @@ public void testOutlierDetection_GivenIndexWithRuntimeFields() throws Exception "Started loading data", "Started analyzing", "Started writing results", - "Finished analysis"); + "Finished analysis" + ); } public void testOutlierDetection_GivenSearchRuntimeMappings() throws Exception { @@ -854,9 +932,7 @@ public void testOutlierDetection_GivenSearchRuntimeMappings() throws Exception { String mappings = "{\"enabled\": false}"; - client().admin().indices().prepareCreate(sourceIndex) - .setMapping(mappings) - .get(); + client().admin().indices().prepareCreate(sourceIndex).setMapping(mappings).get(); BulkRequestBuilder bulkRequestBuilder = client().prepareBulk(); bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); @@ -883,8 +959,7 @@ public void testOutlierDetection_GivenSearchRuntimeMappings() throws Exception { numericFieldRuntimeMapping.put("script", "emit(params._source.numeric)"); runtimeMappings.put("runtime_numeric", numericFieldRuntimeMapping); - DataFrameAnalyticsConfig config = new DataFrameAnalyticsConfig.Builder() - .setId(id) + DataFrameAnalyticsConfig config = new DataFrameAnalyticsConfig.Builder().setId(id) .setSource(new DataFrameAnalyticsSource(new String[] { sourceIndex }, null, null, runtimeMappings)) .setDest(new DataFrameAnalyticsDest(sourceIndex + "-results", null)) .setAnalysis(new OutlierDetection.Builder().build()) @@ -941,7 +1016,8 @@ public void testOutlierDetection_GivenSearchRuntimeMappings() throws Exception { assertProgressComplete(id); assertThat(searchStoredProgress(id).getHits().getTotalHits().value, equalTo(1L)); - assertThatAuditMessagesMatch(id, + assertThatAuditMessagesMatch( + id, "Created analytics with type [outlier_detection]", "Estimated memory usage [", "Starting analytics on node", @@ -952,7 +1028,8 @@ public void testOutlierDetection_GivenSearchRuntimeMappings() throws Exception { "Started loading data", "Started analyzing", "Started writing results", - "Finished analysis"); + "Finished analysis" + ); } @Override diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ScheduledEventsIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ScheduledEventsIT.java index 2df0300c54ee8..0a69500e51d5e 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ScheduledEventsIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ScheduledEventsIT.java @@ -56,23 +56,32 @@ public void testScheduledEvents() throws IOException { List events = new ArrayList<>(); long firstEventStartTime = 1514937600000L; long firstEventEndTime = firstEventStartTime + 2 * 60 * 60 * 1000; - events.add(new ScheduledEvent.Builder().description("1st event (2hr)") + events.add( + new ScheduledEvent.Builder().description("1st event (2hr)") .startTime(Instant.ofEpochMilli(firstEventStartTime)) .endTime(Instant.ofEpochMilli(firstEventEndTime)) - .calendarId(calendarId).build()); + .calendarId(calendarId) + .build() + ); // add 10 min event smaller than the bucket long secondEventStartTime = 1515067200000L; long secondEventEndTime = secondEventStartTime + 10 * 60 * 1000; - events.add(new ScheduledEvent.Builder().description("2nd event with period smaller than bucketspan") + events.add( + new ScheduledEvent.Builder().description("2nd event with period smaller than bucketspan") .startTime(Instant.ofEpochMilli(secondEventStartTime)) .endTime(Instant.ofEpochMilli(secondEventEndTime)) - .calendarId(calendarId).build()); + .calendarId(calendarId) + .build() + ); long thirdEventStartTime = 1515088800000L; long thirdEventEndTime = thirdEventStartTime + 3 * 60 * 60 * 1000; - events.add(new ScheduledEvent.Builder().description("3rd event 3hr") + events.add( + new ScheduledEvent.Builder().description("3rd event 3hr") .startTime(Instant.ofEpochMilli(thirdEventStartTime)) .endTime(Instant.ofEpochMilli(thirdEventEndTime)) - .calendarId(calendarId).build()); + .calendarId(calendarId) + .build() + ); postScheduledEvents(calendarId, events); @@ -168,17 +177,22 @@ public void testScheduledEventWithInterimResults() throws IOException { int bucketCount = 10; long firstEventStartTime = startTime + bucketSpan.millis() * bucketCount; long firstEventEndTime = firstEventStartTime + bucketSpan.millis() * 2; - events.add(new ScheduledEvent.Builder().description("1st event 2hr") + events.add( + new ScheduledEvent.Builder().description("1st event 2hr") .startTime(Instant.ofEpochMilli(firstEventStartTime)) .endTime((Instant.ofEpochMilli(firstEventEndTime))) - .calendarId(calendarId).build()); + .calendarId(calendarId) + .build() + ); postScheduledEvents(calendarId, events); - openJob(job.getId()); // write data up to and including the event - postData(job.getId(), generateData(startTime, bucketSpan, bucketCount + 1, bucketIndex -> randomIntBetween(100, 200)) - .stream().collect(Collectors.joining())); + postData( + job.getId(), + generateData(startTime, bucketSpan, bucketCount + 1, bucketIndex -> randomIntBetween(100, 200)).stream() + .collect(Collectors.joining()) + ); // flush the job and get the interim result during the event flushJob(job.getId(), true); @@ -207,8 +221,11 @@ public void testAddEventsToOpenJob() throws Exception { openJob(job.getId()); // write some buckets of data - postData(job.getId(), generateData(startTime, bucketSpan, bucketCount, bucketIndex -> randomIntBetween(100, 200)) - .stream().collect(Collectors.joining())); + postData( + job.getId(), + generateData(startTime, bucketSpan, bucketCount, bucketIndex -> randomIntBetween(100, 200)).stream() + .collect(Collectors.joining()) + ); // Now create a calendar and events for the job while it is open String calendarId = "test-calendar-online-update"; @@ -216,33 +233,39 @@ public void testAddEventsToOpenJob() throws Exception { List events = new ArrayList<>(); long eventStartTime = startTime + (bucketCount + 1) * bucketSpan.millis(); - long eventEndTime = eventStartTime + (long)(1.5 * bucketSpan.millis()); - events.add(new ScheduledEvent.Builder().description("Some Event") + long eventEndTime = eventStartTime + (long) (1.5 * bucketSpan.millis()); + events.add( + new ScheduledEvent.Builder().description("Some Event") .startTime((Instant.ofEpochMilli(eventStartTime))) .endTime((Instant.ofEpochMilli(eventEndTime))) - .calendarId(calendarId).build()); + .calendarId(calendarId) + .build() + ); postScheduledEvents(calendarId, events); // Wait until the notification that the process was updated is indexed assertBusy(() -> { - SearchResponse searchResponse = - client().prepareSearch(NotificationsIndex.NOTIFICATIONS_INDEX) - .setSize(1) - .addSort("timestamp", SortOrder.DESC) - .setQuery(QueryBuilders.boolQuery() - .filter(QueryBuilders.termQuery("job_id", job.getId())) - .filter(QueryBuilders.termQuery("level", "info")) - ).get(); + SearchResponse searchResponse = client().prepareSearch(NotificationsIndex.NOTIFICATIONS_INDEX) + .setSize(1) + .addSort("timestamp", SortOrder.DESC) + .setQuery( + QueryBuilders.boolQuery() + .filter(QueryBuilders.termQuery("job_id", job.getId())) + .filter(QueryBuilders.termQuery("level", "info")) + ) + .get(); SearchHit[] hits = searchResponse.getHits().getHits(); assertThat(hits.length, equalTo(1)); assertThat(hits[0].getSourceAsMap().get("message"), equalTo("Updated calendars in running process")); }); // write some more buckets of data that cover the scheduled event period - postData(job.getId(), generateData(startTime + bucketCount * bucketSpan.millis(), bucketSpan, 5, - bucketIndex -> randomIntBetween(100, 200)) - .stream().collect(Collectors.joining())); + postData( + job.getId(), + generateData(startTime + bucketCount * bucketSpan.millis(), bucketSpan, 5, bucketIndex -> randomIntBetween(100, 200)).stream() + .collect(Collectors.joining()) + ); // and close closeJob(job.getId()); @@ -250,7 +273,7 @@ public void testAddEventsToOpenJob() throws Exception { List buckets = getBuckets(getBucketsRequest); // the first buckets have no events - for (int i=0; i<=bucketCount; i++) { + for (int i = 0; i <= bucketCount; i++) { assertEquals(0, buckets.get(i).getScheduledEvents().size()); } // 7th and 8th buckets have the event @@ -276,8 +299,11 @@ public void testAddOpenedJobToGroupWithCalendar() throws Exception { openJob(job.getId()); // write some buckets of data - postData(job.getId(), generateData(startTime, bucketSpan, bucketCount, bucketIndex -> randomIntBetween(100, 200)) - .stream().collect(Collectors.joining())); + postData( + job.getId(), + generateData(startTime, bucketSpan, bucketCount, bucketIndex -> randomIntBetween(100, 200)).stream() + .collect(Collectors.joining()) + ); String calendarId = "test-calendar-open-job-update"; @@ -287,38 +313,46 @@ public void testAddOpenedJobToGroupWithCalendar() throws Exception { // Put events in the calendar List events = new ArrayList<>(); long eventStartTime = startTime + (bucketCount + 1) * bucketSpan.millis(); - long eventEndTime = eventStartTime + (long)(1.5 * bucketSpan.millis()); - events.add(new ScheduledEvent.Builder().description("Some Event") + long eventEndTime = eventStartTime + (long) (1.5 * bucketSpan.millis()); + events.add( + new ScheduledEvent.Builder().description("Some Event") .startTime((Instant.ofEpochMilli(eventStartTime))) .endTime((Instant.ofEpochMilli(eventEndTime))) - .calendarId(calendarId).build()); + .calendarId(calendarId) + .build() + ); postScheduledEvents(calendarId, events); // Update the job to be a member of the group - UpdateJobAction.Request jobUpdateRequest = new UpdateJobAction.Request(job.getId(), - new JobUpdate.Builder(job.getId()).setGroups(Collections.singletonList(groupName)).build()); + UpdateJobAction.Request jobUpdateRequest = new UpdateJobAction.Request( + job.getId(), + new JobUpdate.Builder(job.getId()).setGroups(Collections.singletonList(groupName)).build() + ); client().execute(UpdateJobAction.INSTANCE, jobUpdateRequest).actionGet(); // Wait until the notification that the job was updated is indexed assertBusy(() -> { - SearchResponse searchResponse = - client().prepareSearch(NotificationsIndex.NOTIFICATIONS_INDEX) - .setSize(1) - .addSort("timestamp", SortOrder.DESC) - .setQuery(QueryBuilders.boolQuery() - .filter(QueryBuilders.termQuery("job_id", job.getId())) - .filter(QueryBuilders.termQuery("level", "info")) - ).get(); + SearchResponse searchResponse = client().prepareSearch(NotificationsIndex.NOTIFICATIONS_INDEX) + .setSize(1) + .addSort("timestamp", SortOrder.DESC) + .setQuery( + QueryBuilders.boolQuery() + .filter(QueryBuilders.termQuery("job_id", job.getId())) + .filter(QueryBuilders.termQuery("level", "info")) + ) + .get(); SearchHit[] hits = searchResponse.getHits().getHits(); assertThat(hits.length, equalTo(1)); assertThat(hits[0].getSourceAsMap().get("message"), equalTo("Job updated: [groups]")); }); // write some more buckets of data that cover the scheduled event period - postData(job.getId(), generateData(startTime + bucketCount * bucketSpan.millis(), bucketSpan, 5, - bucketIndex -> randomIntBetween(100, 200)) - .stream().collect(Collectors.joining())); + postData( + job.getId(), + generateData(startTime + bucketCount * bucketSpan.millis(), bucketSpan, 5, bucketIndex -> randomIntBetween(100, 200)).stream() + .collect(Collectors.joining()) + ); // and close closeJob(job.getId()); @@ -326,7 +360,7 @@ public void testAddOpenedJobToGroupWithCalendar() throws Exception { List buckets = getBuckets(getBucketsRequest); // the first 6 buckets have no events - for (int i=0; i<=bucketCount; i++) { + for (int i = 0; i <= bucketCount; i++) { assertEquals(0, buckets.get(i).getScheduledEvents().size()); } // 7th and 8th buckets have the event but the last one does not @@ -354,10 +388,13 @@ public void testNewJobWithGlobalCalendar() throws Exception { List events = new ArrayList<>(); long eventStartTime = startTime; long eventEndTime = eventStartTime + (long) (1.5 * bucketSpan.millis()); - events.add(new ScheduledEvent.Builder().description("Some Event") - .startTime((Instant.ofEpochMilli(eventStartTime))) - .endTime((Instant.ofEpochMilli(eventEndTime))) - .calendarId(calendarId).build()); + events.add( + new ScheduledEvent.Builder().description("Some Event") + .startTime((Instant.ofEpochMilli(eventStartTime))) + .endTime((Instant.ofEpochMilli(eventEndTime))) + .calendarId(calendarId) + .build() + ); postScheduledEvents(calendarId, events); @@ -367,8 +404,11 @@ public void testNewJobWithGlobalCalendar() throws Exception { openJob(job.getId()); // write some buckets of data - postData(job.getId(), generateData(startTime, bucketSpan, bucketCount + 1, bucketIndex -> randomIntBetween(100, 200)) - .stream().collect(Collectors.joining())); + postData( + job.getId(), + generateData(startTime, bucketSpan, bucketCount + 1, bucketIndex -> randomIntBetween(100, 200)).stream() + .collect(Collectors.joining()) + ); // and close closeJob(job.getId()); @@ -399,8 +439,11 @@ private Job.Builder createJob(String jobId, TimeValue bucketSpan) { private void runJob(Job.Builder job, long startTime, TimeValue bucketSpan, int bucketCount) throws IOException { openJob(job.getId()); - postData(job.getId(), generateData(startTime, bucketSpan, bucketCount, bucketIndex -> randomIntBetween(100, 200)) - .stream().collect(Collectors.joining())); + postData( + job.getId(), + generateData(startTime, bucketSpan, bucketCount, bucketIndex -> randomIntBetween(100, 200)).stream() + .collect(Collectors.joining()) + ); closeJob(job.getId()); } } diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/SetUpgradeModeIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/SetUpgradeModeIT.java index 2db4ad0db26f3..093268e54b167 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/SetUpgradeModeIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/SetUpgradeModeIT.java @@ -54,12 +54,15 @@ public void testEnableUpgradeMode() throws Exception { assertThat(upgradeMode(), is(false)); // Assert appropriate task state and assignment numbers - assertThat(client().admin() - .cluster() - .prepareListTasks() - .setActions(MlTasks.JOB_TASK_NAME + "[c]", MlTasks.DATAFEED_TASK_NAME + "[c]") - .get() - .getTasks(), hasSize(2)); + assertThat( + client().admin() + .cluster() + .prepareListTasks() + .setActions(MlTasks.JOB_TASK_NAME + "[c]", MlTasks.DATAFEED_TASK_NAME + "[c]") + .get() + .getTasks(), + hasSize(2) + ); ClusterState masterClusterState = client().admin().cluster().prepareState().all().get().getState(); @@ -77,12 +80,15 @@ public void testEnableUpgradeMode() throws Exception { assertThat(persistentTasks.findTasks(MlTasks.DATAFEED_TASK_NAME, task -> true), hasSize(1)); assertThat(persistentTasks.findTasks(MlTasks.JOB_TASK_NAME, task -> true), hasSize(1)); - assertThat(client().admin() - .cluster() - .prepareListTasks() - .setActions(MlTasks.JOB_TASK_NAME + "[c]", MlTasks.DATAFEED_TASK_NAME + "[c]") - .get() - .getTasks(), is(empty())); + assertThat( + client().admin() + .cluster() + .prepareListTasks() + .setActions(MlTasks.JOB_TASK_NAME + "[c]", MlTasks.DATAFEED_TASK_NAME + "[c]") + .get() + .getTasks(), + is(empty()) + ); GetJobsStatsAction.Response.JobStats jobStats = getJobStats(jobId).get(0); assertThat(jobStats.getState(), is(equalTo(JobState.OPENED))); @@ -103,12 +109,17 @@ public void testEnableUpgradeMode() throws Exception { assertThat(persistentTasks.findTasks(MlTasks.DATAFEED_TASK_NAME, task -> true), hasSize(1)); assertThat(persistentTasks.findTasks(MlTasks.JOB_TASK_NAME, task -> true), hasSize(1)); - assertBusy(() -> assertThat(client().admin() - .cluster() - .prepareListTasks() - .setActions(MlTasks.JOB_TASK_NAME + "[c]", MlTasks.DATAFEED_TASK_NAME + "[c]") - .get() - .getTasks(), hasSize(2))); + assertBusy( + () -> assertThat( + client().admin() + .cluster() + .prepareListTasks() + .setActions(MlTasks.JOB_TASK_NAME + "[c]", MlTasks.DATAFEED_TASK_NAME + "[c]") + .get() + .getTasks(), + hasSize(2) + ) + ); jobStats = getJobStats(jobId).get(0); assertThat(jobStats.getState(), is(equalTo(JobState.OPENED))); @@ -162,9 +173,7 @@ public void testAnomalyDetectionActionsInUpgradeMode() { } private void startRealtime(String jobId) throws Exception { - client().admin().indices().prepareCreate("data") - .setMapping("time", "type=date") - .get(); + client().admin().indices().prepareCreate("data").setMapping("time", "type=date").get(); long numDocs1 = randomIntBetween(32, 2048); long now = System.currentTimeMillis(); long lastWeek = now - 604800000; diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/TestFeatureResetIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/TestFeatureResetIT.java index 1c616da018902..91811e133b88e 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/TestFeatureResetIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/TestFeatureResetIT.java @@ -15,8 +15,8 @@ import org.elasticsearch.action.ingest.PutPipelineRequest; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.common.bytes.BytesArray; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.tasks.TaskInfo; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.ml.MlMetadata; import org.elasticsearch.xpack.core.ml.action.PutDataFrameAnalyticsAction; import org.elasticsearch.xpack.core.ml.action.PutTrainedModelAction; @@ -125,19 +125,16 @@ public void testMLFeatureReset() throws Exception { startDataFrameJob("feature_reset_data_frame_analytics_job"); putTrainedModelIngestPipeline("feature_reset_inference_pipeline"); createdPipelines.add("feature_reset_inference_pipeline"); - for(int i = 0; i < 100; i ++) { + for (int i = 0; i < 100; i++) { indexDocForInference("feature_reset_inference_pipeline"); } client().execute(DeletePipelineAction.INSTANCE, new DeletePipelineRequest("feature_reset_inference_pipeline")).actionGet(); createdPipelines.remove("feature_reset_inference_pipeline"); - assertBusy(() -> - assertThat(countNumberInferenceProcessors(client().admin().cluster().prepareState().get().getState()), equalTo(0)) + assertBusy( + () -> assertThat(countNumberInferenceProcessors(client().admin().cluster().prepareState().get().getState()), equalTo(0)) ); - client().execute( - ResetFeatureStateAction.INSTANCE, - new ResetFeatureStateRequest() - ).actionGet(); + client().execute(ResetFeatureStateAction.INSTANCE, new ResetFeatureStateRequest()).actionGet(); assertBusy(() -> { List indices = Arrays.asList(client().admin().indices().prepareGetIndex().addIndices(".ml*").get().indices()); assertThat(indices.toString(), indices, is(empty())); @@ -151,10 +148,10 @@ public void testMLFeatureReset() throws Exception { public void testMLFeatureResetFailureDueToPipelines() throws Exception { putTrainedModelIngestPipeline("feature_reset_failure_inference_pipeline"); createdPipelines.add("feature_reset_failure_inference_pipeline"); - Exception ex = expectThrows(Exception.class, () -> client().execute( - ResetFeatureStateAction.INSTANCE, - new ResetFeatureStateRequest() - ).actionGet()); + Exception ex = expectThrows( + Exception.class, + () -> client().execute(ResetFeatureStateAction.INSTANCE, new ResetFeatureStateRequest()).actionGet() + ); assertThat( ex.getMessage(), containsString( @@ -168,10 +165,7 @@ public void testMLFeatureResetFailureDueToPipelines() throws Exception { public void testMLFeatureResetWithModelDeployment() throws Exception { createModelDeployment(); - client().execute( - ResetFeatureStateAction.INSTANCE, - new ResetFeatureStateRequest() - ).actionGet(); + client().execute(ResetFeatureStateAction.INSTANCE, new ResetFeatureStateRequest()).actionGet(); assertBusy(() -> { List indices = Arrays.asList(client().admin().indices().prepareGetIndex().addIndices(".ml*").get().indices()); assertThat(indices.toString(), indices, is(empty())); @@ -196,11 +190,7 @@ void createModelDeployment() { TrainedModelConfig.builder() .setModelType(TrainedModelType.PYTORCH) .setInferenceConfig( - new PassThroughConfig( - null, - new BertTokenization(null, false, null, Tokenization.Truncate.NONE), - null - ) + new PassThroughConfig(null, new BertTokenization(null, false, null, Tokenization.Truncate.NONE), null) ) .setModelId(TRAINED_MODEL_ID) .build(), @@ -215,10 +205,8 @@ void createModelDeployment() { PutTrainedModelVocabularyAction.INSTANCE, new PutTrainedModelVocabularyAction.Request(TRAINED_MODEL_ID, List.of("these", "are", "my", "words")) ).actionGet(); - client().execute( - StartTrainedModelDeploymentAction.INSTANCE, - new StartTrainedModelDeploymentAction.Request(TRAINED_MODEL_ID) - ).actionGet(); + client().execute(StartTrainedModelDeploymentAction.INSTANCE, new StartTrainedModelDeploymentAction.Request(TRAINED_MODEL_ID)) + .actionGet(); } private boolean isResetMode() { @@ -232,7 +220,11 @@ private void startDataFrameJob(String jobId) throws Exception { ClassificationIT.createIndex(sourceIndex, false); ClassificationIT.indexData(sourceIndex, 300, 50, KEYWORD_FIELD); - DataFrameAnalyticsConfig config = buildAnalytics(jobId, sourceIndex, destIndex, null, + DataFrameAnalyticsConfig config = buildAnalytics( + jobId, + sourceIndex, + destIndex, + null, new Classification( KEYWORD_FIELD, BoostedTreeParams.builder().setNumTopFeatureImportanceValues(1).build(), @@ -242,7 +234,9 @@ private void startDataFrameJob(String jobId) throws Exception { null, null, null, - null)); + null + ) + ); PutDataFrameAnalyticsAction.Request request = new PutDataFrameAnalyticsAction.Request(config); client().execute(PutDataFrameAnalyticsAction.INSTANCE, request).actionGet(); @@ -258,9 +252,7 @@ private void putAndStartJob(String jobId) throws Exception { } private void startRealtime(String jobId) throws Exception { - client().admin().indices().prepareCreate("data") - .setMapping("time", "type=date") - .get(); + client().admin().indices().prepareCreate("data").setMapping("time", "type=date").get(); long numDocs1 = randomIntBetween(32, 2048); long now = System.currentTimeMillis(); long lastWeek = now - 604800000; @@ -292,17 +284,17 @@ private void putTrainedModelIngestPipeline(String pipelineId) throws Exception { new PutPipelineRequest( pipelineId, new BytesArray( - "{\n" + - " \"processors\": [\n" + - " {\n" + - " \"inference\": {\n" + - " \"inference_config\": {\"classification\":{}},\n" + - " \"model_id\": \"lang_ident_model_1\",\n" + - " \"field_map\": {}\n" + - " }\n" + - " }\n" + - " ]\n" + - " }" + "{\n" + + " \"processors\": [\n" + + " {\n" + + " \"inference\": {\n" + + " \"inference_config\": {\"classification\":{}},\n" + + " \"model_id\": \"lang_ident_model_1\",\n" + + " \"field_map\": {}\n" + + " }\n" + + " }\n" + + " ]\n" + + " }" ), XContentType.JSON ) @@ -310,10 +302,7 @@ private void putTrainedModelIngestPipeline(String pipelineId) throws Exception { } private void indexDocForInference(String pipelineId) { - client().prepareIndex("foo") - .setPipeline(pipelineId) - .setSource("{\"text\": \"this is some plain text.\"}", XContentType.JSON) - .get(); + client().prepareIndex("foo").setPipeline(pipelineId).setSource("{\"text\": \"this is some plain text.\"}", XContentType.JSON).get(); } } diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/TrainedModelIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/TrainedModelIT.java index fe0b2878a6513..5636e2fe654d2 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/TrainedModelIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/TrainedModelIT.java @@ -24,14 +24,14 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.test.SecuritySettingsSourceField; +import org.elasticsearch.test.rest.ESRestTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.SecuritySettingsSourceField; -import org.elasticsearch.test.rest.ESRestTestCase; import org.elasticsearch.xpack.core.ml.inference.persistence.InferenceIndexConstants; import org.elasticsearch.xpack.core.ml.integration.MlRestTestStateCleaner; import org.elasticsearch.xpack.core.ml.job.messages.Messages; @@ -60,8 +60,10 @@ */ public class TrainedModelIT extends ESRestTestCase { - private static final String BASIC_AUTH_VALUE = UsernamePasswordToken.basicAuthHeaderValue("x_pack_rest_user", - SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING); + private static final String BASIC_AUTH_VALUE = UsernamePasswordToken.basicAuthHeaderValue( + "x_pack_rest_user", + SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING + ); @Override protected Settings restClientSettings() { @@ -83,8 +85,7 @@ public void testGetTrainedModels() throws IOException { String modelId2 = "a_test_regression_model-2"; putRegressionModel(modelId); putRegressionModel(modelId2); - Response getModel = client().performRequest(new Request("GET", - MachineLearning.BASE_PATH + "trained_models/" + modelId)); + Response getModel = client().performRequest(new Request("GET", MachineLearning.BASE_PATH + "trained_models/" + modelId)); assertThat(getModel.getStatusLine().getStatusCode(), equalTo(200)); String response = EntityUtils.toString(getModel.getEntity()); @@ -92,8 +93,7 @@ public void testGetTrainedModels() throws IOException { assertThat(response, containsString("\"model_id\":\"a_test_regression_model\"")); assertThat(response, containsString("\"count\":1")); - getModel = client().performRequest(new Request("GET", - MachineLearning.BASE_PATH + "trained_models/a_test_regression*")); + getModel = client().performRequest(new Request("GET", MachineLearning.BASE_PATH + "trained_models/a_test_regression*")); assertThat(getModel.getStatusLine().getStatusCode(), equalTo(200)); response = EntityUtils.toString(getModel.getEntity()); @@ -102,8 +102,9 @@ public void testGetTrainedModels() throws IOException { assertThat(response, not(containsString("\"definition\""))); assertThat(response, containsString("\"count\":2")); - getModel = client().performRequest(new Request("GET", - MachineLearning.BASE_PATH + "trained_models/a_test_regression_model?human=true&include=definition")); + getModel = client().performRequest( + new Request("GET", MachineLearning.BASE_PATH + "trained_models/a_test_regression_model?human=true&include=definition") + ); assertThat(getModel.getStatusLine().getStatusCode(), equalTo(200)); response = EntityUtils.toString(getModel.getEntity()); @@ -115,8 +116,12 @@ public void testGetTrainedModels() throws IOException { assertThat(response, not(containsString("\"compressed_definition\""))); assertThat(response, containsString("\"count\":1")); - getModel = client().performRequest(new Request("GET", - MachineLearning.BASE_PATH + "trained_models/a_test_regression_model?decompress_definition=false&include=definition")); + getModel = client().performRequest( + new Request( + "GET", + MachineLearning.BASE_PATH + "trained_models/a_test_regression_model?decompress_definition=false&include=definition" + ) + ); assertThat(getModel.getStatusLine().getStatusCode(), equalTo(200)); response = EntityUtils.toString(getModel.getEntity()); @@ -126,14 +131,20 @@ public void testGetTrainedModels() throws IOException { assertThat(response, not(containsString("\"definition\""))); assertThat(response, containsString("\"count\":1")); - ResponseException responseException = expectThrows(ResponseException.class, () -> - client().performRequest(new Request("GET", - MachineLearning.BASE_PATH + "trained_models/a_test_regression*?human=true&include=definition"))); - assertThat(EntityUtils.toString(responseException.getResponse().getEntity()), - containsString(Messages.INFERENCE_TOO_MANY_DEFINITIONS_REQUESTED)); - - getModel = client().performRequest(new Request("GET", - MachineLearning.BASE_PATH + "trained_models/a_test_regression_model,a_test_regression_model-2")); + ResponseException responseException = expectThrows( + ResponseException.class, + () -> client().performRequest( + new Request("GET", MachineLearning.BASE_PATH + "trained_models/a_test_regression*?human=true&include=definition") + ) + ); + assertThat( + EntityUtils.toString(responseException.getResponse().getEntity()), + containsString(Messages.INFERENCE_TOO_MANY_DEFINITIONS_REQUESTED) + ); + + getModel = client().performRequest( + new Request("GET", MachineLearning.BASE_PATH + "trained_models/a_test_regression_model,a_test_regression_model-2") + ); assertThat(getModel.getStatusLine().getStatusCode(), equalTo(200)); response = EntityUtils.toString(getModel.getEntity()); @@ -141,15 +152,20 @@ public void testGetTrainedModels() throws IOException { assertThat(response, containsString("\"model_id\":\"a_test_regression_model-2\"")); assertThat(response, containsString("\"count\":2")); - getModel = client().performRequest(new Request("GET", - MachineLearning.BASE_PATH + "trained_models/classification*?allow_no_match=true")); + getModel = client().performRequest( + new Request("GET", MachineLearning.BASE_PATH + "trained_models/classification*?allow_no_match=true") + ); assertThat(getModel.getStatusLine().getStatusCode(), equalTo(200)); response = EntityUtils.toString(getModel.getEntity()); assertThat(response, containsString("\"count\":0")); - ResponseException ex = expectThrows(ResponseException.class, () -> client().performRequest(new Request("GET", - MachineLearning.BASE_PATH + "trained_models/classification*?allow_no_match=false"))); + ResponseException ex = expectThrows( + ResponseException.class, + () -> client().performRequest( + new Request("GET", MachineLearning.BASE_PATH + "trained_models/classification*?allow_no_match=false") + ) + ); assertThat(ex.getResponse().getStatusLine().getStatusCode(), equalTo(404)); getModel = client().performRequest(new Request("GET", MachineLearning.BASE_PATH + "trained_models?from=0&size=1")); @@ -173,31 +189,35 @@ public void testDeleteTrainedModels() throws IOException { String modelId = "test_delete_regression_model"; putRegressionModel(modelId); - Response delModel = client().performRequest(new Request("DELETE", - MachineLearning.BASE_PATH + "trained_models/" + modelId)); + Response delModel = client().performRequest(new Request("DELETE", MachineLearning.BASE_PATH + "trained_models/" + modelId)); String response = EntityUtils.toString(delModel.getEntity()); assertThat(response, containsString("\"acknowledged\":true")); - ResponseException responseException = expectThrows(ResponseException.class, - () -> client().performRequest(new Request("DELETE", MachineLearning.BASE_PATH + "trained_models/" + modelId))); + ResponseException responseException = expectThrows( + ResponseException.class, + () -> client().performRequest(new Request("DELETE", MachineLearning.BASE_PATH + "trained_models/" + modelId)) + ); assertThat(responseException.getResponse().getStatusLine().getStatusCode(), equalTo(404)); - responseException = expectThrows(ResponseException.class, + responseException = expectThrows( + ResponseException.class, () -> client().performRequest( - new Request("GET", - InferenceIndexConstants.LATEST_INDEX_NAME + "/_doc/" + TrainedModelDefinitionDoc.docId(modelId, 0)))); + new Request("GET", InferenceIndexConstants.LATEST_INDEX_NAME + "/_doc/" + TrainedModelDefinitionDoc.docId(modelId, 0)) + ) + ); assertThat(responseException.getResponse().getStatusLine().getStatusCode(), equalTo(404)); - responseException = expectThrows(ResponseException.class, - () -> client().performRequest( - new Request("GET", - InferenceIndexConstants.LATEST_INDEX_NAME + "/_doc/" + modelId))); + responseException = expectThrows( + ResponseException.class, + () -> client().performRequest(new Request("GET", InferenceIndexConstants.LATEST_INDEX_NAME + "/_doc/" + modelId)) + ); assertThat(responseException.getResponse().getStatusLine().getStatusCode(), equalTo(404)); } public void testGetPrePackagedModels() throws IOException { - Response getModel = client().performRequest(new Request("GET", - MachineLearning.BASE_PATH + "trained_models/lang_ident_model_1?human=true&include=definition")); + Response getModel = client().performRequest( + new Request("GET", MachineLearning.BASE_PATH + "trained_models/lang_ident_model_1?human=true&include=definition") + ); assertThat(getModel.getStatusLine().getStatusCode(), equalTo(200)); String response = EntityUtils.toString(getModel.getEntity()); @@ -209,22 +229,26 @@ public void testGetPrePackagedModels() throws IOException { public void testExportImportModel() throws IOException { String modelId = "regression_model_to_export"; putRegressionModel(modelId); - Response getModel = client().performRequest(new Request("GET", - MachineLearning.BASE_PATH + "trained_models/" + modelId)); + Response getModel = client().performRequest(new Request("GET", MachineLearning.BASE_PATH + "trained_models/" + modelId)); assertThat(getModel.getStatusLine().getStatusCode(), equalTo(200)); String response = EntityUtils.toString(getModel.getEntity()); assertThat(response, containsString("\"model_id\":\"regression_model_to_export\"")); assertThat(response, containsString("\"count\":1")); - getModel = client().performRequest(new Request("GET", - MachineLearning.BASE_PATH + - "trained_models/" + modelId + - "?include=definition&decompress_definition=false&exclude_generated=true")); + getModel = client().performRequest( + new Request( + "GET", + MachineLearning.BASE_PATH + + "trained_models/" + + modelId + + "?include=definition&decompress_definition=false&exclude_generated=true" + ) + ); assertThat(getModel.getStatusLine().getStatusCode(), equalTo(200)); Map exportedModel = entityAsMap(getModel); - Map modelDefinition = ((List>)exportedModel.get("trained_model_configs")).get(0); + Map modelDefinition = ((List>) exportedModel.get("trained_model_configs")).get(0); modelDefinition.remove("model_id"); String importedModelId = "regression_model_to_import"; @@ -244,16 +268,16 @@ public void testExportImportModel() throws IOException { } private void putRegressionModel(String modelId) throws IOException { - try(XContentBuilder builder = XContentFactory.jsonBuilder()) { - TrainedModelDefinition.Builder definition = new TrainedModelDefinition.Builder() - .setPreProcessors(Collections.emptyList()) + try (XContentBuilder builder = XContentFactory.jsonBuilder()) { + TrainedModelDefinition.Builder definition = new TrainedModelDefinition.Builder().setPreProcessors(Collections.emptyList()) .setTrainedModel(buildRegression()); TrainedModelConfig.builder() .setDefinition(definition) .setInferenceConfig(new RegressionConfig()) .setModelId(modelId) .setInput(new TrainedModelInput(Arrays.asList("col1", "col2", "col3"))) - .build().toXContent(builder, ToXContent.EMPTY_PARAMS); + .build() + .toXContent(builder, ToXContent.EMPTY_PARAMS); Request model = new Request("PUT", "_ml/trained_models/" + modelId); model.setJsonEntity(XContentHelper.convertToJson(BytesReference.bytes(builder), false, XContentType.JSON)); assertThat(client().performRequest(model).getStatusLine().getStatusCode(), equalTo(200)); @@ -264,39 +288,29 @@ private static TrainedModel buildRegression() { List featureNames = Arrays.asList("field.foo", "field.bar", "animal_cat", "animal_dog"); Tree tree1 = Tree.builder() .setFeatureNames(featureNames) - .setNodes(TreeNode.builder(0) - .setLeftChild(1) - .setRightChild(2) - .setSplitFeature(0) - .setThreshold(0.5), + .setNodes( + TreeNode.builder(0).setLeftChild(1).setRightChild(2).setSplitFeature(0).setThreshold(0.5), TreeNode.builder(1).setLeafValue(Collections.singletonList(0.3)), - TreeNode.builder(2) - .setThreshold(0.0) - .setSplitFeature(3) - .setLeftChild(3) - .setRightChild(4), + TreeNode.builder(2).setThreshold(0.0).setSplitFeature(3).setLeftChild(3).setRightChild(4), TreeNode.builder(3).setLeafValue(Collections.singletonList(0.1)), - TreeNode.builder(4).setLeafValue(Collections.singletonList(0.2))) + TreeNode.builder(4).setLeafValue(Collections.singletonList(0.2)) + ) .build(); Tree tree2 = Tree.builder() .setFeatureNames(featureNames) - .setNodes(TreeNode.builder(0) - .setLeftChild(1) - .setRightChild(2) - .setSplitFeature(2) - .setThreshold(1.0), + .setNodes( + TreeNode.builder(0).setLeftChild(1).setRightChild(2).setSplitFeature(2).setThreshold(1.0), TreeNode.builder(1).setLeafValue(Collections.singletonList(1.5)), - TreeNode.builder(2).setLeafValue(Collections.singletonList(0.9))) + TreeNode.builder(2).setLeafValue(Collections.singletonList(0.9)) + ) .build(); Tree tree3 = Tree.builder() .setFeatureNames(featureNames) - .setNodes(TreeNode.builder(0) - .setLeftChild(1) - .setRightChild(2) - .setSplitFeature(1) - .setThreshold(0.2), + .setNodes( + TreeNode.builder(0).setLeftChild(1).setRightChild(2).setSplitFeature(1).setThreshold(0.2), TreeNode.builder(1).setLeafValue(Collections.singletonList(1.5)), - TreeNode.builder(2).setLeafValue(Collections.singletonList(0.9))) + TreeNode.builder(2).setLeafValue(Collections.singletonList(0.9)) + ) .build(); return Ensemble.builder() .setTargetType(TargetType.REGRESSION) diff --git a/x-pack/plugin/ml/qa/no-bootstrap-tests/src/test/java/org/elasticsearch/xpack/ml/utils/NamedPipeHelperNoBootstrapTests.java b/x-pack/plugin/ml/qa/no-bootstrap-tests/src/test/java/org/elasticsearch/xpack/ml/utils/NamedPipeHelperNoBootstrapTests.java index 79dc3c8831a52..0c22c07a69d02 100644 --- a/x-pack/plugin/ml/qa/no-bootstrap-tests/src/test/java/org/elasticsearch/xpack/ml/utils/NamedPipeHelperNoBootstrapTests.java +++ b/x-pack/plugin/ml/qa/no-bootstrap-tests/src/test/java/org/elasticsearch/xpack/ml/utils/NamedPipeHelperNoBootstrapTests.java @@ -6,20 +6,20 @@ */ package org.elasticsearch.xpack.ml.utils; +import com.sun.jna.IntegerType; +import com.sun.jna.Native; +import com.sun.jna.Pointer; +import com.sun.jna.WString; +import com.sun.jna.ptr.IntByReference; + import org.apache.lucene.util.Constants; import org.apache.lucene.util.LuceneTestCase; -import org.elasticsearch.core.PathUtils; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.PathUtils; import org.elasticsearch.env.Environment; import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.monitor.jvm.JvmInfo; -import com.sun.jna.IntegerType; -import com.sun.jna.Native; -import com.sun.jna.Pointer; -import com.sun.jna.WString; -import com.sun.jna.ptr.IntByReference; - import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; @@ -32,7 +32,6 @@ import java.nio.file.StandardOpenOption; import java.time.Duration; - /** * Covers positive test cases for create named pipes, which are not possible in Java with * the Elasticsearch security manager configuration or seccomp. This is why the class extends @@ -80,8 +79,16 @@ public DWord(long val) { } // https://msdn.microsoft.com/en-us/library/windows/desktop/aa365150(v=vs.85).aspx - private static native Pointer CreateNamedPipeW(WString name, DWord openMode, DWord pipeMode, DWord maxInstances, DWord outBufferSize, - DWord inBufferSize, DWord defaultTimeOut, Pointer securityAttributes); + private static native Pointer CreateNamedPipeW( + WString name, + DWord openMode, + DWord pipeMode, + DWord maxInstances, + DWord outBufferSize, + DWord inBufferSize, + DWord defaultTimeOut, + Pointer securityAttributes + ); // https://msdn.microsoft.com/en-us/library/windows/desktop/aa365146(v=vs.85).aspx private static native boolean ConnectNamedPipe(Pointer handle, Pointer overlapped); @@ -90,12 +97,22 @@ private static native Pointer CreateNamedPipeW(WString name, DWord openMode, DWo private static native boolean CloseHandle(Pointer handle); // https://msdn.microsoft.com/en-us/library/windows/desktop/aa365467(v=vs.85).aspx - private static native boolean ReadFile(Pointer handle, Pointer buffer, DWord numberOfBytesToRead, IntByReference numberOfBytesRead, - Pointer overlapped); + private static native boolean ReadFile( + Pointer handle, + Pointer buffer, + DWord numberOfBytesToRead, + IntByReference numberOfBytesRead, + Pointer overlapped + ); // https://msdn.microsoft.com/en-us/library/windows/desktop/aa365747(v=vs.85).aspx - private static native boolean WriteFile(Pointer handle, Pointer buffer, DWord numberOfBytesToWrite, IntByReference numberOfBytesWritten, - Pointer overlapped); + private static native boolean WriteFile( + Pointer handle, + Pointer buffer, + DWord numberOfBytesToWrite, + IntByReference numberOfBytesWritten, + Pointer overlapped + ); private static Pointer createPipe(String pipeName, boolean forWrite) throws IOException, InterruptedException { if (Constants.WINDOWS) { @@ -113,9 +130,16 @@ private static void createPipeUnix(String pipeName) throws IOException, Interrup } private static Pointer createPipeWindows(String pipeName, boolean forWrite) throws IOException { - Pointer handle = CreateNamedPipeW(new WString(pipeName), new DWord(forWrite ? PIPE_ACCESS_OUTBOUND : PIPE_ACCESS_INBOUND), - new DWord(PIPE_TYPE_BYTE | PIPE_WAIT | PIPE_REJECT_REMOTE_CLIENTS), new DWord(1), - new DWord(BUFFER_SIZE), new DWord(BUFFER_SIZE), new DWord(NMPWAIT_USE_DEFAULT_WAIT), Pointer.NULL); + Pointer handle = CreateNamedPipeW( + new WString(pipeName), + new DWord(forWrite ? PIPE_ACCESS_OUTBOUND : PIPE_ACCESS_INBOUND), + new DWord(PIPE_TYPE_BYTE | PIPE_WAIT | PIPE_REJECT_REMOTE_CLIENTS), + new DWord(1), + new DWord(BUFFER_SIZE), + new DWord(BUFFER_SIZE), + new DWord(NMPWAIT_USE_DEFAULT_WAIT), + Pointer.NULL + ); if (INVALID_HANDLE_VALUE.equals(handle)) { throw new IOException("CreateNamedPipeW failed for pipe " + pipeName + " with error " + Native.getLastError()); } @@ -218,8 +242,7 @@ public void run() { try { handle = createPipe(pipeName, false); line = readLineFromPipe(pipeName, handle); - } - catch (IOException | InterruptedException e) { + } catch (IOException | InterruptedException e) { exception = e; } try { @@ -278,14 +301,15 @@ public Exception getException() { public void testOpenForInput() throws IOException, InterruptedException { Environment env = TestEnvironment.newEnvironment( - Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build()); + Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build() + ); String pipeName = NAMED_PIPE_HELPER.getDefaultPipeDirectoryPrefix(env) + "inputPipe" + JvmInfo.jvmInfo().pid(); PipeWriterServer server = new PipeWriterServer(pipeName, HELLO_WORLD); server.start(); try { // Timeout is 10 seconds for the very rare case of Amazon EBS volumes created from snapshots - // being slow the first time a particular disk block is accessed. The same problem as + // being slow the first time a particular disk block is accessed. The same problem as // https://github.com/elastic/x-pack-elasticsearch/issues/922, which was fixed by // https://github.com/elastic/x-pack-elasticsearch/pull/987, has been observed in CI tests. InputStream is = NAMED_PIPE_HELPER.openNamedPipeInputStream(pipeName, Duration.ofSeconds(10)); @@ -309,14 +333,15 @@ public void testOpenForInput() throws IOException, InterruptedException { public void testOpenForOutput() throws IOException, InterruptedException { Environment env = TestEnvironment.newEnvironment( - Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build()); + Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build() + ); String pipeName = NAMED_PIPE_HELPER.getDefaultPipeDirectoryPrefix(env) + "outputPipe" + JvmInfo.jvmInfo().pid(); PipeReaderServer server = new PipeReaderServer(pipeName); server.start(); try { // Timeout is 10 seconds for the very rare case of Amazon EBS volumes created from snapshots - // being slow the first time a particular disk block is accessed. The same problem as + // being slow the first time a particular disk block is accessed. The same problem as // https://github.com/elastic/x-pack-elasticsearch/issues/922, which was fixed by // https://github.com/elastic/x-pack-elasticsearch/pull/987, has been observed in CI tests. OutputStream os = NAMED_PIPE_HELPER.openNamedPipeOutputStream(pipeName, Duration.ofSeconds(10)); diff --git a/x-pack/plugin/ml/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/InferencePipelineAggIT.java b/x-pack/plugin/ml/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/InferencePipelineAggIT.java index 2ef77df31bd52..6df3ecec39976 100644 --- a/x-pack/plugin/ml/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/InferencePipelineAggIT.java +++ b/x-pack/plugin/ml/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/InferencePipelineAggIT.java @@ -18,7 +18,6 @@ import static org.hamcrest.Matchers.contains; - public class InferencePipelineAggIT extends InferenceTestCase { private static final String MODEL_ID = "a-complex-regression-model"; @@ -28,69 +27,74 @@ public class InferencePipelineAggIT extends InferenceTestCase { public void setupModelAndData() throws IOException { putRegressionModel( MODEL_ID, - "{\n" + - " \"description\": \"super complex model for tests\",\n" + - " \"input\": {\"field_names\": [\"avg_cost\", \"item\"]},\n" + - " \"inference_config\": {\n" + - " \"regression\": {\n" + - " \"results_field\": \"regression-value\",\n" + - " \"num_top_feature_importance_values\": 2\n" + - " }\n" + - " },\n" + - " \"definition\": {\n" + - " \"preprocessors\" : [{\n" + - " \"one_hot_encoding\": {\n" + - " \"field\": \"product_type\",\n" + - " \"hot_map\": {\n" + - " \"TV\": \"type_tv\",\n" + - " \"VCR\": \"type_vcr\",\n" + - " \"Laptop\": \"type_laptop\"\n" + - " }\n" + - " }\n" + - " }],\n" + - " \"trained_model\": {\n" + - " \"ensemble\": {\n" + - " \"feature_names\": [],\n" + - " \"target_type\": \"regression\",\n" + - " \"trained_models\": [\n" + - " {\n" + - " \"tree\": {\n" + - " \"feature_names\": [\n" + - " \"avg_cost\", \"type_tv\", \"type_vcr\", \"type_laptop\"\n" + - " ],\n" + - " \"tree_structure\": [\n" + - " {\n" + - " \"node_index\": 0,\n" + - " \"split_feature\": 0,\n" + - " \"split_gain\": 12,\n" + - " \"threshold\": 38,\n" + - " \"decision_type\": \"lte\",\n" + - " \"default_left\": true,\n" + - " \"left_child\": 1,\n" + - " \"right_child\": 2\n" + - " },\n" + - " {\n" + - " \"node_index\": 1,\n" + - " \"leaf_value\": 5.0\n" + - " },\n" + - " {\n" + - " \"node_index\": 2,\n" + - " \"leaf_value\": 2.0\n" + - " }\n" + - " ],\n" + - " \"target_type\": \"regression\"\n" + - " }\n" + - " }\n" + - " ]\n" + - " }\n" + - " }\n" + - " }\n" + - " }"); - createIndex(INDEX_NAME, Settings.EMPTY, "\"properties\":{\n" + - " \"product\":{\"type\": \"keyword\"},\n" + - " \"cost\":{\"type\": \"integer\"},\n" + - " \"time\": {\"type\": \"date\"}" + - "}"); + "{\n" + + " \"description\": \"super complex model for tests\",\n" + + " \"input\": {\"field_names\": [\"avg_cost\", \"item\"]},\n" + + " \"inference_config\": {\n" + + " \"regression\": {\n" + + " \"results_field\": \"regression-value\",\n" + + " \"num_top_feature_importance_values\": 2\n" + + " }\n" + + " },\n" + + " \"definition\": {\n" + + " \"preprocessors\" : [{\n" + + " \"one_hot_encoding\": {\n" + + " \"field\": \"product_type\",\n" + + " \"hot_map\": {\n" + + " \"TV\": \"type_tv\",\n" + + " \"VCR\": \"type_vcr\",\n" + + " \"Laptop\": \"type_laptop\"\n" + + " }\n" + + " }\n" + + " }],\n" + + " \"trained_model\": {\n" + + " \"ensemble\": {\n" + + " \"feature_names\": [],\n" + + " \"target_type\": \"regression\",\n" + + " \"trained_models\": [\n" + + " {\n" + + " \"tree\": {\n" + + " \"feature_names\": [\n" + + " \"avg_cost\", \"type_tv\", \"type_vcr\", \"type_laptop\"\n" + + " ],\n" + + " \"tree_structure\": [\n" + + " {\n" + + " \"node_index\": 0,\n" + + " \"split_feature\": 0,\n" + + " \"split_gain\": 12,\n" + + " \"threshold\": 38,\n" + + " \"decision_type\": \"lte\",\n" + + " \"default_left\": true,\n" + + " \"left_child\": 1,\n" + + " \"right_child\": 2\n" + + " },\n" + + " {\n" + + " \"node_index\": 1,\n" + + " \"leaf_value\": 5.0\n" + + " },\n" + + " {\n" + + " \"node_index\": 2,\n" + + " \"leaf_value\": 2.0\n" + + " }\n" + + " ],\n" + + " \"target_type\": \"regression\"\n" + + " }\n" + + " }\n" + + " ]\n" + + " }\n" + + " }\n" + + " }\n" + + " }" + ); + createIndex( + INDEX_NAME, + Settings.EMPTY, + "\"properties\":{\n" + + " \"product\":{\"type\": \"keyword\"},\n" + + " \"cost\":{\"type\": \"integer\"},\n" + + " \"time\": {\"type\": \"date\"}" + + "}" + ); indexData("{ \"product\": \"TV\", \"cost\": 300, \"time\": 1587501233000 }"); indexData("{ \"product\": \"TV\", \"cost\": 400, \"time\": 1587501233000}"); indexData("{ \"product\": \"VCR\", \"cost\": 150, \"time\": 1587501233000 }"); @@ -114,43 +118,40 @@ private Response search(String searchBody) throws IOException { @SuppressWarnings("unchecked") public void testPipelineRegressionSimple() throws Exception { Response searchResponse = search( - "{\n" + - " \"size\": 0,\n" + - " \"aggs\": {\n" + - " \"good\": {\n" + - " \"terms\": {\n" + - " \"field\": \"product\",\n" + - " \"size\": 10\n" + - " },\n" + - " \"aggs\": {\n" + - " \"avg_cost_agg\": {\n" + - " \"avg\": {\n" + - " \"field\": \"cost\"\n" + - " }\n" + - " },\n" + - " \"regression_agg\": {\n" + - " \"inference\": {\n" + - " \"model_id\": \"a-complex-regression-model\",\n" + - " \"inference_config\": {\n" + - " \"regression\": {\n" + - " \"results_field\": \"value\"\n" + - " }\n" + - " },\n" + - " \"buckets_path\": {\n" + - " \"avg_cost\": \"avg_cost_agg\"\n" + - " }\n" + - " }\n" + - " }\n" + - " }\n" + - " }\n" + - " }\n" + - " }" + "{\n" + + " \"size\": 0,\n" + + " \"aggs\": {\n" + + " \"good\": {\n" + + " \"terms\": {\n" + + " \"field\": \"product\",\n" + + " \"size\": 10\n" + + " },\n" + + " \"aggs\": {\n" + + " \"avg_cost_agg\": {\n" + + " \"avg\": {\n" + + " \"field\": \"cost\"\n" + + " }\n" + + " },\n" + + " \"regression_agg\": {\n" + + " \"inference\": {\n" + + " \"model_id\": \"a-complex-regression-model\",\n" + + " \"inference_config\": {\n" + + " \"regression\": {\n" + + " \"results_field\": \"value\"\n" + + " }\n" + + " },\n" + + " \"buckets_path\": {\n" + + " \"avg_cost\": \"avg_cost_agg\"\n" + + " }\n" + + " }\n" + + " }\n" + + " }\n" + + " }\n" + + " }\n" + + " }" ); assertThat( - (List)XContentMapValues.extractValue( - "aggregations.good.buckets.regression_agg.value", - responseAsMap(searchResponse) - ), + (List) XContentMapValues.extractValue("aggregations.good.buckets.regression_agg.value", responseAsMap(searchResponse)), contains(2.0, 2.0, 2.0) ); } @@ -158,47 +159,47 @@ public void testPipelineRegressionSimple() throws Exception { @SuppressWarnings("unchecked") public void testPipelineAggReferencingSingleBucket() throws Exception { Response searchResponse = search( - "{\n" + - " \"size\": 0,\n" + - " \"query\": {\n" + - " \"match_all\": {}\n" + - " },\n" + - " \"aggs\": {\n" + - " \"date_histo\": {\n" + - " \"date_histogram\": {\n" + - " \"field\": \"time\",\n" + - " \"fixed_interval\": \"1d\"\n" + - " },\n" + - " \"aggs\": {\n" + - " \"good\": {\n" + - " \"terms\": {\n" + - " \"field\": \"product\",\n" + - " \"size\": 10\n" + - " },\n" + - " \"aggs\": {\n" + - " \"avg_cost_agg\": {\n" + - " \"avg\": {\n" + - " \"field\": \"cost\"\n" + - " }\n" + - " }\n" + - " }\n" + - " },\n" + - " \"regression_agg\": {\n" + - " \"inference\": {\n" + - " \"model_id\": \"a-complex-regression-model\",\n" + - " \"buckets_path\": {\n" + - " \"avg_cost\": \"good['TV']>avg_cost_agg\",\n" + - " \"product_type\": \"good['TV']\"\n" + - " }\n" + - " }\n" + - " }\n" + - " }\n" + - " }\n" + - " }\n" + - " }" + "{\n" + + " \"size\": 0,\n" + + " \"query\": {\n" + + " \"match_all\": {}\n" + + " },\n" + + " \"aggs\": {\n" + + " \"date_histo\": {\n" + + " \"date_histogram\": {\n" + + " \"field\": \"time\",\n" + + " \"fixed_interval\": \"1d\"\n" + + " },\n" + + " \"aggs\": {\n" + + " \"good\": {\n" + + " \"terms\": {\n" + + " \"field\": \"product\",\n" + + " \"size\": 10\n" + + " },\n" + + " \"aggs\": {\n" + + " \"avg_cost_agg\": {\n" + + " \"avg\": {\n" + + " \"field\": \"cost\"\n" + + " }\n" + + " }\n" + + " }\n" + + " },\n" + + " \"regression_agg\": {\n" + + " \"inference\": {\n" + + " \"model_id\": \"a-complex-regression-model\",\n" + + " \"buckets_path\": {\n" + + " \"avg_cost\": \"good['TV']>avg_cost_agg\",\n" + + " \"product_type\": \"good['TV']\"\n" + + " }\n" + + " }\n" + + " }\n" + + " }\n" + + " }\n" + + " }\n" + + " }" ); assertThat( - (List)XContentMapValues.extractValue( + (List) XContentMapValues.extractValue( "aggregations.date_histo.buckets.regression_agg.value", responseAsMap(searchResponse) ), @@ -209,36 +210,36 @@ public void testPipelineAggReferencingSingleBucket() throws Exception { @SuppressWarnings("unchecked") public void testAllFieldsMissingWarning() throws IOException { Response searchResponse = search( - "{\n" + - " \"size\": 0,\n" + - " \"query\": { \"match_all\" : { } },\n" + - " \"aggs\": {\n" + - " \"good\": {\n" + - " \"terms\": {\n" + - " \"field\": \"product\",\n" + - " \"size\": 10\n" + - " },\n" + - " \"aggs\": {\n" + - " \"avg_cost_agg\": {\n" + - " \"avg\": {\n" + - " \"field\": \"cost\"\n" + - " }\n" + - " },\n" + - " \"regression_agg\" : {\n" + - " \"inference\": {\n" + - " \"model_id\": \"a-complex-regression-model\",\n" + - " \"buckets_path\": {\n" + - " \"cost\" : \"avg_cost_agg\"\n" + - " }\n" + - " }\n" + - " }\n" + - " }\n" + - " }\n" + - " }\n" + - " }" + "{\n" + + " \"size\": 0,\n" + + " \"query\": { \"match_all\" : { } },\n" + + " \"aggs\": {\n" + + " \"good\": {\n" + + " \"terms\": {\n" + + " \"field\": \"product\",\n" + + " \"size\": 10\n" + + " },\n" + + " \"aggs\": {\n" + + " \"avg_cost_agg\": {\n" + + " \"avg\": {\n" + + " \"field\": \"cost\"\n" + + " }\n" + + " },\n" + + " \"regression_agg\" : {\n" + + " \"inference\": {\n" + + " \"model_id\": \"a-complex-regression-model\",\n" + + " \"buckets_path\": {\n" + + " \"cost\" : \"avg_cost_agg\"\n" + + " }\n" + + " }\n" + + " }\n" + + " }\n" + + " }\n" + + " }\n" + + " }" ); assertThat( - (List)XContentMapValues.extractValue( + (List) XContentMapValues.extractValue( "aggregations.good.buckets.regression_agg.warning", responseAsMap(searchResponse) ), diff --git a/x-pack/plugin/ml/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/InferenceProcessorIT.java b/x-pack/plugin/ml/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/InferenceProcessorIT.java index 1b4a643b25cdd..ab118e7122355 100644 --- a/x-pack/plugin/ml/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/InferenceProcessorIT.java +++ b/x-pack/plugin/ml/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/InferenceProcessorIT.java @@ -22,7 +22,6 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasSize; - public class InferenceProcessorIT extends InferenceTestCase { private static final String MODEL_ID = "a-perfect-regression-model"; @@ -30,9 +29,7 @@ public class InferenceProcessorIT extends InferenceTestCase { @Before public void enableLogging() throws IOException { Request setTrace = new Request("PUT", "_cluster/settings"); - setTrace.setJsonEntity( - "{\"persistent\": {\"logger.org.elasticsearch.xpack.ml.inference\": \"TRACE\"}}" - ); + setTrace.setJsonEntity("{\"persistent\": {\"logger.org.elasticsearch.xpack.ml.inference\": \"TRACE\"}}"); assertThat(client().performRequest(setTrace).getStatusLine().getStatusCode(), equalTo(200)); } @@ -49,12 +46,13 @@ public void testCreateAndDeletePipelineWithInferenceProcessor() throws Exception putPipeline(MODEL_ID, pipelineId); Map statsAsMap = getStats(MODEL_ID); - List pipelineCount = - (List)XContentMapValues.extractValue("trained_model_stats.pipeline_count", statsAsMap); + List pipelineCount = (List) XContentMapValues.extractValue("trained_model_stats.pipeline_count", statsAsMap); assertThat(pipelineCount.get(0), equalTo(1)); - List> counts = - (List>)XContentMapValues.extractValue("trained_model_stats.ingest.total", statsAsMap); + List> counts = (List>) XContentMapValues.extractValue( + "trained_model_stats.ingest.total", + statsAsMap + ); assertThat(counts.get(0).get("count"), equalTo(0)); assertThat(counts.get(0).get("time_in_millis"), equalTo(0)); assertThat(counts.get(0).get("current"), equalTo(0)); @@ -80,12 +78,16 @@ public void testCreateAndDeletePipelineWithInferenceProcessor() throws Exception } } - List updatedPipelineCount = - (List) XContentMapValues.extractValue("trained_model_stats.pipeline_count", updatedStatsMap); + List updatedPipelineCount = (List) XContentMapValues.extractValue( + "trained_model_stats.pipeline_count", + updatedStatsMap + ); assertThat(updatedPipelineCount.get(0), equalTo(0)); - List> inferenceStats = - (List>) XContentMapValues.extractValue("trained_model_stats.inference_stats", updatedStatsMap); + List> inferenceStats = (List>) XContentMapValues.extractValue( + "trained_model_stats.inference_stats", + updatedStatsMap + ); assertNotNull(inferenceStats); assertThat(inferenceStats, hasSize(1)); assertThat(inferenceStats.get(0).get("inference_count"), equalTo(1)); @@ -104,12 +106,13 @@ public void testCreateAndDeletePipelineWithInferenceProcessorByName() throws Exc putPipeline("regression_second", "second_pipeline"); Map statsAsMap = getStats(MODEL_ID); - List pipelineCount = - (List)XContentMapValues.extractValue("trained_model_stats.pipeline_count", statsAsMap); + List pipelineCount = (List) XContentMapValues.extractValue("trained_model_stats.pipeline_count", statsAsMap); assertThat(pipelineCount.get(0), equalTo(2)); - List> counts = - (List>)XContentMapValues.extractValue("trained_model_stats.ingest.total", statsAsMap); + List> counts = (List>) XContentMapValues.extractValue( + "trained_model_stats.ingest.total", + statsAsMap + ); assertThat(counts.get(0).get("count"), equalTo(0)); assertThat(counts.get(0).get("time_in_millis"), equalTo(0)); assertThat(counts.get(0).get("current"), equalTo(0)); @@ -138,12 +141,16 @@ public void testCreateAndDeletePipelineWithInferenceProcessorByName() throws Exc } } - List updatedPipelineCount = - (List) XContentMapValues.extractValue("trained_model_stats.pipeline_count", updatedStatsMap); + List updatedPipelineCount = (List) XContentMapValues.extractValue( + "trained_model_stats.pipeline_count", + updatedStatsMap + ); assertThat(updatedPipelineCount.get(0), equalTo(0)); - List> inferenceStats = - (List>) XContentMapValues.extractValue("trained_model_stats.inference_stats", updatedStatsMap); + List> inferenceStats = (List>) XContentMapValues.extractValue( + "trained_model_stats.inference_stats", + updatedStatsMap + ); assertNotNull(inferenceStats); assertThat(inferenceStats, hasSize(1)); assertThat(inferenceStats.toString(), inferenceStats.get(0).get("inference_count"), equalTo(2)); @@ -155,12 +162,18 @@ public void testDeleteModelWhileAliasReferencedByPipeline() throws Exception { putModelAlias("regression_first", MODEL_ID); createdPipelines.add("first_pipeline"); putPipeline("regression_first", "first_pipeline"); - Exception ex = expectThrows(Exception.class, - () -> client().performRequest(new Request("DELETE", "_ml/trained_models/" + MODEL_ID))); - assertThat(ex.getMessage(), - containsString("Cannot delete model [" - + MODEL_ID - + "] as it has a model_alias [regression_first] that is still referenced by ingest processors")); + Exception ex = expectThrows( + Exception.class, + () -> client().performRequest(new Request("DELETE", "_ml/trained_models/" + MODEL_ID)) + ); + assertThat( + ex.getMessage(), + containsString( + "Cannot delete model [" + + MODEL_ID + + "] as it has a model_alias [regression_first] that is still referenced by ingest processors" + ) + ); infer("first_pipeline"); deletePipeline("first_pipeline"); waitForStats(); @@ -171,13 +184,10 @@ public void testDeleteModelAliasWhileAliasReferencedByPipeline() throws Exceptio putModelAlias("regression_to_delete", MODEL_ID); createdPipelines.add("first_pipeline"); putPipeline("regression_to_delete", "first_pipeline"); - Exception ex = expectThrows(Exception.class, - () -> client().performRequest( - new Request( - "DELETE", - "_ml/trained_models/" + MODEL_ID + "/model_aliases/regression_to_delete" - ) - )); + Exception ex = expectThrows( + Exception.class, + () -> client().performRequest(new Request("DELETE", "_ml/trained_models/" + MODEL_ID + "/model_aliases/regression_to_delete")) + ); assertThat( ex.getMessage(), containsString("Cannot delete model_alias [regression_to_delete] as it is still referenced by ingest processors") @@ -191,12 +201,14 @@ public void testDeleteModelWhileReferencedByPipeline() throws Exception { putRegressionModel(MODEL_ID); createdPipelines.add("first_pipeline"); putPipeline(MODEL_ID, "first_pipeline"); - Exception ex = expectThrows(Exception.class, - () -> client().performRequest(new Request("DELETE", "_ml/trained_models/" + MODEL_ID))); - assertThat(ex.getMessage(), - containsString("Cannot delete model [" - + MODEL_ID - + "] as it is still referenced by ingest processors")); + Exception ex = expectThrows( + Exception.class, + () -> client().performRequest(new Request("DELETE", "_ml/trained_models/" + MODEL_ID)) + ); + assertThat( + ex.getMessage(), + containsString("Cannot delete model [" + MODEL_ID + "] as it is still referenced by ingest processors") + ); infer("first_pipeline"); deletePipeline("first_pipeline"); waitForStats(); @@ -209,17 +221,19 @@ public void testCreateProcessorWithDeprecatedFields() throws Exception { createdPipelines.add("regression-model-deprecated-pipeline"); Request putPipeline = new Request("PUT", "_ingest/pipeline/regression-model-deprecated-pipeline"); putPipeline.setJsonEntity( - "{\n" + - " \"processors\": [\n" + - " {\n" + - " \"inference\" : {\n" + - " \"model_id\" : \"" + MODEL_ID + "\",\n" + - " \"inference_config\": {\"regression\": {}},\n" + - " \"field_mappings\": {}\n" + - " }\n" + - " }\n" + - " ]\n" + - "}" + "{\n" + + " \"processors\": [\n" + + " {\n" + + " \"inference\" : {\n" + + " \"model_id\" : \"" + + MODEL_ID + + "\",\n" + + " \"inference_config\": {\"regression\": {}},\n" + + " \"field_mappings\": {}\n" + + " }\n" + + " }\n" + + " ]\n" + + "}" ); RequestOptions ro = expectWarnings("Deprecated field [field_mappings] used, expected [field_map] instead"); @@ -246,12 +260,16 @@ public void testCreateProcessorWithDeprecatedFields() throws Exception { } } - List updatedPipelineCount = - (List) XContentMapValues.extractValue("trained_model_stats.pipeline_count", updatedStatsMap); + List updatedPipelineCount = (List) XContentMapValues.extractValue( + "trained_model_stats.pipeline_count", + updatedStatsMap + ); assertThat(updatedPipelineCount.get(0), equalTo(0)); - List> inferenceStats = - (List>) XContentMapValues.extractValue("trained_model_stats.inference_stats", updatedStatsMap); + List> inferenceStats = (List>) XContentMapValues.extractValue( + "trained_model_stats.inference_stats", + updatedStatsMap + ); assertNotNull(inferenceStats); assertThat(inferenceStats, hasSize(1)); assertThat(inferenceStats.get(0).get("inference_count"), equalTo(1)); @@ -269,18 +287,20 @@ private void infer(String pipelineId) throws IOException { private void putPipeline(String modelId, String pipelineName) throws IOException { Request putPipeline = new Request("PUT", "_ingest/pipeline/" + pipelineName); putPipeline.setJsonEntity( - " {\n" + - " \"processors\": [\n" + - " {\n" + - " \"inference\" : {\n" + - " \"model_id\" : \"" + modelId + "\",\n" + - " \"inference_config\": {\"regression\": {}},\n" + - " \"target_field\": \"regression_field\",\n" + - " \"field_map\": {}\n" + - " }\n" + - " }\n" + - " ]\n" + - " }" + " {\n" + + " \"processors\": [\n" + + " {\n" + + " \"inference\" : {\n" + + " \"model_id\" : \"" + + modelId + + "\",\n" + + " \"inference_config\": {\"regression\": {}},\n" + + " \"target_field\": \"regression_field\",\n" + + " \"field_map\": {}\n" + + " }\n" + + " }\n" + + " ]\n" + + " }" ); assertThat(client().performRequest(putPipeline).getStatusLine().getStatusCode(), equalTo(200)); diff --git a/x-pack/plugin/ml/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/InferenceTestCase.java b/x-pack/plugin/ml/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/InferenceTestCase.java index c421b16638758..46c5f7dedd908 100644 --- a/x-pack/plugin/ml/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/InferenceTestCase.java +++ b/x-pack/plugin/ml/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/InferenceTestCase.java @@ -63,8 +63,10 @@ void waitForStats() throws Exception { } } - List> inferenceStats = - (List>) XContentMapValues.extractValue("trained_model_stats.inference_stats", updatedStatsMap); + List> inferenceStats = (List>) XContentMapValues.extractValue( + "trained_model_stats.inference_stats", + updatedStatsMap + ); assertNotNull(inferenceStats); }); } @@ -76,26 +78,27 @@ Map getStats(String modelId) throws IOException { } void putRegressionModel(String modelId) throws IOException { - putRegressionModel(modelId, - " {\n" + - " \"description\": \"empty model for tests\",\n" + - " \"tags\": [\"regression\", \"tag1\"],\n" + - " \"input\": {\"field_names\": [\"field1\", \"field2\"]},\n" + - " \"inference_config\": { \"regression\": {\"results_field\": \"my_regression\"}},\n" + - " \"definition\": {\n" + - " \"preprocessors\": [],\n" + - " \"trained_model\": {\n" + - " \"tree\": {\n" + - " \"feature_names\": [\"field1\", \"field2\"],\n" + - " \"tree_structure\": [\n" + - " {\"node_index\": 0, \"leaf_value\": 42}\n" + - " ],\n" + - " \"target_type\": \"regression\"\n" + - " }\n" + - " }\n" + - " }\n" + - " }" - ); + putRegressionModel( + modelId, + " {\n" + + " \"description\": \"empty model for tests\",\n" + + " \"tags\": [\"regression\", \"tag1\"],\n" + + " \"input\": {\"field_names\": [\"field1\", \"field2\"]},\n" + + " \"inference_config\": { \"regression\": {\"results_field\": \"my_regression\"}},\n" + + " \"definition\": {\n" + + " \"preprocessors\": [],\n" + + " \"trained_model\": {\n" + + " \"tree\": {\n" + + " \"feature_names\": [\"field1\", \"field2\"],\n" + + " \"tree_structure\": [\n" + + " {\"node_index\": 0, \"leaf_value\": 42}\n" + + " ],\n" + + " \"target_type\": \"regression\"\n" + + " }\n" + + " }\n" + + " }\n" + + " }" + ); } void putRegressionModel(String modelId, String body) throws IOException { diff --git a/x-pack/plugin/ml/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/transforms/PainlessDomainSplitIT.java b/x-pack/plugin/ml/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/transforms/PainlessDomainSplitIT.java index 28ec21d6d0063..6fc65c99cd7ab 100644 --- a/x-pack/plugin/ml/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/transforms/PainlessDomainSplitIT.java +++ b/x-pack/plugin/ml/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/transforms/PainlessDomainSplitIT.java @@ -13,7 +13,6 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.time.DateFormatter; -import org.elasticsearch.common.time.DateFormatters; import org.elasticsearch.test.rest.ESRestTestCase; import java.time.ZoneOffset; @@ -88,13 +87,18 @@ static class TestConfiguration { tests.add(new TestConfiguration("example", "local", "example.local")); tests.add(new TestConfiguration("b.example", "local", "b.example.local")); tests.add(new TestConfiguration("a.b.example", "local", "a.b.example.local")); - tests.add(new TestConfiguration("r192494180984795-1-1041782-channel-live.ums", "ustream.tv", "r192494180984795-1-1041782-cha" + - "nnel-live.ums.ustream.tv")); + tests.add( + new TestConfiguration( + "r192494180984795-1-1041782-channel-live.ums", + "ustream.tv", + "r192494180984795-1-1041782-cha" + "nnel-live.ums.ustream.tv" + ) + ); tests.add(new TestConfiguration("192.168.62.9", "prelert.com", "192.168.62.9.prelert.com")); // These are not a valid DNS names tests.add(new TestConfiguration("kerberos.http.192.168", "62.222", "kerberos.http.192.168.62.222")); - //tests.add(new TestConfiguration("192.168", "62.9\143\127", "192.168.62.9\143\127")); + // tests.add(new TestConfiguration("192.168", "62.9\143\127", "192.168.62.9\143\127")); // no part of the DNS name can be longer than 63 octets /* @@ -107,15 +111,14 @@ static class TestConfiguration { */ // [Zach] This breaks the script's JSON encoding, skipping for now - //String bad = "0u1aof\209\1945\188hI4\236\197\205J\244\188\247\223\190F\2135\229gVE7\230i\215\231\205Qzay\225UJ\192 + // String bad = "0u1aof\209\1945\188hI4\236\197\205J\244\188\247\223\190F\2135\229gVE7\230i\215\231\205Qzay\225UJ\192 // pw\216\231\204\194\216\193QV4g\196\207Whpvx.fVxl\194BjA\245kbYk\211XG\235\198\218B\252\219\225S\197\217I\2538n\229 // \244\213\252\215Ly\226NW\242\248\244Q\220\245\221c\207\189\205Hxq5\224\240.\189Jt4\243\245t\244\198\199p\210\1987 // r\2050L\239sR0M\190w\238\223\234L\226\2242D\233\210\206\195h\199\206tA\214J\192C\224\191b\188\201\251\198M\244h // \206.\198\242l\2114\191JBU\198h\207\215w\243\228R\1924\242\208\191CV\208p\197gDW\198P\217\195X\191Fp\196\197J\193 // \245\2070\196zH\197\243\253g\239.adz.beacon.base.net"; - //hrd = "base.net"; - //tests.add(new TestConfiguration(bad.substring(0, bad.length() - (hrd.length() + 1)), hrd, bad)); - + // hrd = "base.net"; + // tests.add(new TestConfiguration(bad.substring(0, bad.length() - (hrd.length() + 1)), hrd, bad)); tests.add(new TestConfiguration("_example", "local", "_example.local")); tests.add(new TestConfiguration("www._maps", "google.co.uk", "www._maps.google.co.uk")); @@ -132,7 +135,7 @@ static class TestConfiguration { tests.add(new TestConfiguration(null, "example.com", "WwW.example.COM")); // TLD with only 1 rule. - tests.add(new TestConfiguration(null, "domain.biz", "domain.biz" )); + tests.add(new TestConfiguration(null, "domain.biz", "domain.biz")); tests.add(new TestConfiguration(null, "domain.biz", "b.domain.biz")); tests.add(new TestConfiguration(null, "domain.biz", "a.b.domain.biz")); @@ -156,8 +159,8 @@ static class TestConfiguration { tests.add(new TestConfiguration(null, "test.kyoto.jp", "test.kyoto.jp")); tests.add(new TestConfiguration(null, "b.ide.kyoto.jp", "b.ide.kyoto.jp")); tests.add(new TestConfiguration(null, "b.ide.kyoto.jp", "a.b.ide.kyoto.jp")); - //tests.add(new TestConfiguration(null, "b.c.kobe.jp", "b.c.kobe.jp")); - //tests.add(new TestConfiguration(null, "b.c.kobe.jp", "a.b.c.kobe.jp")); + // tests.add(new TestConfiguration(null, "b.c.kobe.jp", "b.c.kobe.jp")); + // tests.add(new TestConfiguration(null, "b.c.kobe.jp", "a.b.c.kobe.jp")); tests.add(new TestConfiguration(null, "city.kobe.jp", "city.kobe.jp")); tests.add(new TestConfiguration(null, "city.kobe.jp", "www.city.kobe.jp")); tests.add(new TestConfiguration(null, "test.us", "test.us")); @@ -166,27 +169,27 @@ static class TestConfiguration { tests.add(new TestConfiguration(null, "test.ak.us", "www.test.ak.us")); tests.add(new TestConfiguration(null, "test.k12.ak.us", "test.k12.ak.us")); tests.add(new TestConfiguration(null, "test.k12.ak.us", "www.test.k12.ak.us")); - //tests.add(new TestConfiguration(null, "食狮.com.cn", "食狮.com.cn")); - //tests.add(new TestConfiguration(null, "食狮.公司.cn", "食狮.公司.cn")); - //tests.add(new TestConfiguration(null, "食狮.公司.cn", "www.食狮.公司.cn")); - //tests.add(new TestConfiguration(null, "shishi.公司.cn", "shishi.公司.cn")); - //tests.add(new TestConfiguration(null, "食狮.中国", "食狮.中国")); - //tests.add(new TestConfiguration(null, "食狮.中国", "www.食狮.中国")); - //tests.add(new TestConfiguration(null, "shishi.中国", "shishi.中国")); + // tests.add(new TestConfiguration(null, "食狮.com.cn", "食狮.com.cn")); + // tests.add(new TestConfiguration(null, "食狮.公司.cn", "食狮.公司.cn")); + // tests.add(new TestConfiguration(null, "食狮.公司.cn", "www.食狮.公司.cn")); + // tests.add(new TestConfiguration(null, "shishi.公司.cn", "shishi.公司.cn")); + // tests.add(new TestConfiguration(null, "食狮.中国", "食狮.中国")); + // tests.add(new TestConfiguration(null, "食狮.中国", "www.食狮.中国")); + // tests.add(new TestConfiguration(null, "shishi.中国", "shishi.中国")); tests.add(new TestConfiguration(null, "xn--85x722f.com.cn", "xn--85x722f.com.cn")); - tests.add(new TestConfiguration(null, "xn--85x722f.xn--55qx5d.cn", "xn--85x722f.xn--55qx5d.cn")); + tests.add(new TestConfiguration(null, "xn--85x722f.xn--55qx5d.cn", "xn--85x722f.xn--55qx5d.cn")); tests.add(new TestConfiguration(null, "xn--85x722f.xn--55qx5d.cn", "www.xn--85x722f.xn--55qx5d.cn")); tests.add(new TestConfiguration(null, "shishi.xn--55qx5d.cn", "shishi.xn--55qx5d.cn")); tests.add(new TestConfiguration(null, "xn--85x722f.xn--fiqs8s", "xn--85x722f.xn--fiqs8s")); tests.add(new TestConfiguration(null, "xn--85x722f.xn--fiqs8s", "www.xn--85x722f.xn--fiqs8s")); - tests.add(new TestConfiguration(null, "shishi.xn--fiqs8s","shishi.xn--fiqs8s")); + tests.add(new TestConfiguration(null, "shishi.xn--fiqs8s", "shishi.xn--fiqs8s")); } public void testIsolated() throws Exception { Settings.Builder settings = Settings.builder() - .put(IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), 1) - .put(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 0); + .put(IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), 1) + .put(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 0); createIndex("painless", settings.build()); Request createDoc = new Request("PUT", "/painless/_doc/1"); @@ -204,21 +207,24 @@ public void testIsolated() throws Exception { Request searchRequest = new Request("GET", "/painless/_search"); searchRequest.setJsonEntity( - "{\n" + - " \"query\" : {\n" + - " \"match_all\": {}\n" + - " },\n" + - " \"script_fields\" : {\n" + - " \"domain_split\" : {\n" + - " \"script\" : {\n" + - " \"lang\": \"painless\",\n" + - " \"source\": \"" + - " return domainSplit(params['host']); \",\n" + - " \"params\": " + mapAsJson + "\n" + - " }\n" + - " }\n" + - " }\n" + - "}"); + "{\n" + + " \"query\" : {\n" + + " \"match_all\": {}\n" + + " },\n" + + " \"script_fields\" : {\n" + + " \"domain_split\" : {\n" + + " \"script\" : {\n" + + " \"lang\": \"painless\",\n" + + " \"source\": \"" + + " return domainSplit(params['host']); \",\n" + + " \"params\": " + + mapAsJson + + "\n" + + " }\n" + + " }\n" + + " }\n" + + "}" + ); String responseBody = EntityUtils.toString(client().performRequest(searchRequest).getEntity()); Matcher m = pattern.matcher(responseBody); @@ -234,13 +240,32 @@ public void testIsolated() throws Exception { // domainSplit() tests had subdomain, testHighestRegisteredDomainCases() do not if (testConfig.subDomainExpected != null) { - assertThat("Expected subdomain [" + testConfig.subDomainExpected + "] but found [" + actualSubDomain - + "]. Actual " + actualTotal + " vs Expected " + expectedTotal, actualSubDomain, - equalTo(testConfig.subDomainExpected)); + assertThat( + "Expected subdomain [" + + testConfig.subDomainExpected + + "] but found [" + + actualSubDomain + + "]. Actual " + + actualTotal + + " vs Expected " + + expectedTotal, + actualSubDomain, + equalTo(testConfig.subDomainExpected) + ); } - assertThat("Expected domain [" + testConfig.domainExpected + "] but found [" + actualDomain + "]. Actual " - + actualTotal + " vs Expected " + expectedTotal, actualDomain, equalTo(testConfig.domainExpected)); + assertThat( + "Expected domain [" + + testConfig.domainExpected + + "] but found [" + + actualDomain + + "]. Actual " + + actualTotal + + " vs Expected " + + expectedTotal, + actualDomain, + equalTo(testConfig.domainExpected) + ); } } @@ -248,35 +273,39 @@ public void testHRDSplit() throws Exception { // Create job Request createJobRequest = new Request("PUT", BASE_PATH + "anomaly_detectors/hrd-split-job"); createJobRequest.setJsonEntity( - "{\n" + - " \"description\":\"Domain splitting\",\n" + - " \"analysis_config\" : {\n" + - " \"bucket_span\":\"3600s\",\n" + - " \"detectors\" :[{\"function\":\"count\", \"by_field_name\" : \"domain_split\"}]\n" + - " },\n" + - " \"data_description\" : {\n" + - " \"time_field\":\"time\"\n" + - " }\n" + - "}"); + "{\n" + + " \"description\":\"Domain splitting\",\n" + + " \"analysis_config\" : {\n" + + " \"bucket_span\":\"3600s\",\n" + + " \"detectors\" :[{\"function\":\"count\", \"by_field_name\" : \"domain_split\"}]\n" + + " },\n" + + " \"data_description\" : {\n" + + " \"time_field\":\"time\"\n" + + " }\n" + + "}" + ); client().performRequest(createJobRequest); client().performRequest(new Request("POST", BASE_PATH + "anomaly_detectors/hrd-split-job/_open")); // Create index to hold data Settings.Builder settings = Settings.builder() - .put(IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), 1) - .put(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 0); + .put(IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), 1) + .put(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 0); - createIndex("painless", settings.build(), "\"properties\": { \"domain\": { \"type\": \"keyword\" }," + - "\"time\": { \"type\": \"date\" } }"); + createIndex( + "painless", + settings.build(), + "\"properties\": { \"domain\": { \"type\": \"keyword\" }," + "\"time\": { \"type\": \"date\" } }" + ); // Index some data ZonedDateTime baseTime = ZonedDateTime.now(ZoneOffset.UTC).minusYears(1); - TestConfiguration test = tests.get(randomInt(tests.size()-1)); + TestConfiguration test = tests.get(randomInt(tests.size() - 1)); // domainSplit() tests had subdomain, testHighestRegisteredDomainCases() did not, so we need a special case for sub String expectedSub = test.subDomainExpected == null ? ".*" : test.subDomainExpected.replace(".", "\\."); String expectedHRD = test.domainExpected.replace(".", "\\."); - Pattern pattern = Pattern.compile("domain_split\":\\[\"(" + expectedSub + "),(" + expectedHRD +")\"[,\\]]"); + Pattern pattern = Pattern.compile("domain_split\":\\[\"(" + expectedSub + "),(" + expectedHRD + ")\"[,\\]]"); for (int i = 1; i <= 100; i++) { ZonedDateTime time = baseTime.plusHours(i); @@ -301,15 +330,16 @@ public void testHRDSplit() throws Exception { // Create and start datafeed Request createFeedRequest = new Request("PUT", BASE_PATH + "datafeeds/hrd-split-datafeed"); createFeedRequest.setJsonEntity( - "{\n" + - " \"job_id\":\"hrd-split-job\",\n" + - " \"indexes\":[\"painless\"],\n" + - " \"script_fields\": {\n" + - " \"domain_split\": {\n" + - " \"script\": \"return domainSplit(doc['domain'].value, params);\"\n" + - " }\n" + - " }\n" + - "}"); + "{\n" + + " \"job_id\":\"hrd-split-job\",\n" + + " \"indexes\":[\"painless\"],\n" + + " \"script_fields\": {\n" + + " \"domain_split\": {\n" + + " \"script\": \"return domainSplit(doc['domain'].value, params);\"\n" + + " }\n" + + " }\n" + + "}" + ); client().performRequest(createFeedRequest); Request startDatafeedRequest = new Request("POST", BASE_PATH + "datafeeds/hrd-split-datafeed/_start"); @@ -322,12 +352,9 @@ public void testHRDSplit() throws Exception { client().performRequest(new Request("POST", "/.ml-anomalies-*/_refresh")); - Response records = client().performRequest(new Request("GET", - BASE_PATH + "anomaly_detectors/hrd-split-job/results/records")); + Response records = client().performRequest(new Request("GET", BASE_PATH + "anomaly_detectors/hrd-split-job/results/records")); String responseBody = EntityUtils.toString(records.getEntity()); - assertThat("response body [" + responseBody + "] did not contain [\"count\":2]", - responseBody, - containsString("\"count\":2")); + assertThat("response body [" + responseBody + "] did not contain [\"count\":2]", responseBody, containsString("\"count\":2")); Matcher m = pattern.matcher(responseBody); String actualSubDomain = ""; @@ -342,20 +369,40 @@ public void testHRDSplit() throws Exception { // domainSplit() tests had subdomain, testHighestRegisteredDomainCases() do not if (test.subDomainExpected != null) { - assertThat("Expected subdomain [" + test.subDomainExpected + "] but found [" + actualSubDomain - + "]. Actual " + actualTotal + " vs Expected " + expectedTotal, actualSubDomain, - equalTo(test.subDomainExpected)); + assertThat( + "Expected subdomain [" + + test.subDomainExpected + + "] but found [" + + actualSubDomain + + "]. Actual " + + actualTotal + + " vs Expected " + + expectedTotal, + actualSubDomain, + equalTo(test.subDomainExpected) + ); } - assertThat("Expected domain [" + test.domainExpected + "] but found [" + actualDomain + "]. Actual " - + actualTotal + " vs Expected " + expectedTotal, actualDomain, equalTo(test.domainExpected)); + assertThat( + "Expected domain [" + + test.domainExpected + + "] but found [" + + actualDomain + + "]. Actual " + + actualTotal + + " vs Expected " + + expectedTotal, + actualDomain, + equalTo(test.domainExpected) + ); } private void waitUntilJobIsClosed(String jobId) throws Exception { assertBusy(() -> { try { - Response jobStatsResponse = client().performRequest(new Request("GET", - BASE_PATH + "anomaly_detectors/" + jobId + "/_stats")); + Response jobStatsResponse = client().performRequest( + new Request("GET", BASE_PATH + "anomaly_detectors/" + jobId + "/_stats") + ); assertThat(EntityUtils.toString(jobStatsResponse.getEntity()), containsString("\"state\":\"closed\"")); } catch (Exception e) { throw new RuntimeException(e); @@ -366,10 +413,8 @@ private void waitUntilJobIsClosed(String jobId) throws Exception { private void waitUntilDatafeedIsStopped(String dfId) throws Exception { assertBusy(() -> { try { - Response datafeedStatsResponse = client().performRequest(new Request("GET", - BASE_PATH + "datafeeds/" + dfId + "/_stats")); - assertThat(EntityUtils.toString(datafeedStatsResponse.getEntity()), - containsString("\"state\":\"stopped\"")); + Response datafeedStatsResponse = client().performRequest(new Request("GET", BASE_PATH + "datafeeds/" + dfId + "/_stats")); + assertThat(EntityUtils.toString(datafeedStatsResponse.getEntity()), containsString("\"state\":\"stopped\"")); } catch (Exception e) { throw new RuntimeException(e); } diff --git a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/license/MachineLearningLicensingIT.java b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/license/MachineLearningLicensingIT.java index 79662e4d6d3b5..7d58c2c0baffc 100644 --- a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/license/MachineLearningLicensingIT.java +++ b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/license/MachineLearningLicensingIT.java @@ -22,13 +22,13 @@ import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.core.TimeValue; import org.elasticsearch.license.License.OperationMode; import org.elasticsearch.persistent.PersistentTasksCustomMetadata; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; import org.elasticsearch.search.aggregations.metrics.AvgAggregationBuilder; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.XPackField; import org.elasticsearch.xpack.core.ml.MachineLearningField; import org.elasticsearch.xpack.core.ml.MlConfigIndex; @@ -169,8 +169,11 @@ public void testMachineLearningPutDatafeedActionRestricted() { // test that license restricted apis do not work ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, () -> { PlainActionFuture listener = PlainActionFuture.newFuture(); - client().execute(PutDatafeedAction.INSTANCE, - new PutDatafeedAction.Request(createDatafeed(datafeedId, jobId, Collections.singletonList(jobId))), listener); + client().execute( + PutDatafeedAction.INSTANCE, + new PutDatafeedAction.Request(createDatafeed(datafeedId, jobId, Collections.singletonList(jobId))), + listener + ); listener.actionGet(); }); assertThat(e.status(), is(RestStatus.FORBIDDEN)); @@ -183,8 +186,11 @@ public void testMachineLearningPutDatafeedActionRestricted() { assertMLAllowed(true); // test that license restricted apis do now work PlainActionFuture listener = PlainActionFuture.newFuture(); - client().execute(PutDatafeedAction.INSTANCE, - new PutDatafeedAction.Request(createDatafeed(datafeedId, jobId, Collections.singletonList(jobId))), listener); + client().execute( + PutDatafeedAction.INSTANCE, + new PutDatafeedAction.Request(createDatafeed(datafeedId, jobId, Collections.singletonList(jobId))), + listener + ); PutDatafeedAction.Response response = listener.actionGet(); assertNotNull(response); } @@ -203,9 +209,11 @@ public void testAutoCloseJobWithDatafeed() throws Exception { assertNotNull(putJobResponse); // put datafeed PlainActionFuture putDatafeedListener = PlainActionFuture.newFuture(); - client().execute(PutDatafeedAction.INSTANCE, - new PutDatafeedAction.Request(createDatafeed(datafeedId, jobId, - Collections.singletonList(datafeedIndex))), putDatafeedListener); + client().execute( + PutDatafeedAction.INSTANCE, + new PutDatafeedAction.Request(createDatafeed(datafeedId, jobId, Collections.singletonList(datafeedIndex))), + putDatafeedListener + ); PutDatafeedAction.Response putDatafeedResponse = putDatafeedListener.actionGet(); assertNotNull(putDatafeedResponse); // open job @@ -218,7 +226,6 @@ public void testAutoCloseJobWithDatafeed() throws Exception { client().execute(StartDatafeedAction.INSTANCE, new StartDatafeedAction.Request(datafeedId, 0L), listener); listener.actionGet(); - if (randomBoolean()) { enableLicensing(randomInvalidLicenseType()); } else { @@ -299,9 +306,11 @@ public void testMachineLearningStartDatafeedActionRestricted() throws Exception PutJobAction.Response putJobResponse = putJobListener.actionGet(); assertNotNull(putJobResponse); PlainActionFuture putDatafeedListener = PlainActionFuture.newFuture(); - client().execute(PutDatafeedAction.INSTANCE, - new PutDatafeedAction.Request(createDatafeed(datafeedId, jobId, - Collections.singletonList(datafeedIndex))), putDatafeedListener); + client().execute( + PutDatafeedAction.INSTANCE, + new PutDatafeedAction.Request(createDatafeed(datafeedId, jobId, Collections.singletonList(datafeedIndex))), + putDatafeedListener + ); PutDatafeedAction.Response putDatafeedResponse = putDatafeedListener.actionGet(); assertNotNull(putDatafeedResponse); PlainActionFuture openJobListener = PlainActionFuture.newFuture(); @@ -362,9 +371,11 @@ public void testMachineLearningStopDatafeedActionNotRestricted() throws Exceptio PutJobAction.Response putJobResponse = putJobListener.actionGet(); assertNotNull(putJobResponse); PlainActionFuture putDatafeedListener = PlainActionFuture.newFuture(); - client().execute(PutDatafeedAction.INSTANCE, - new PutDatafeedAction.Request(createDatafeed(datafeedId, jobId, - Collections.singletonList(datafeedIndex))), putDatafeedListener); + client().execute( + PutDatafeedAction.INSTANCE, + new PutDatafeedAction.Request(createDatafeed(datafeedId, jobId, Collections.singletonList(datafeedIndex))), + putDatafeedListener + ); PutDatafeedAction.Response putDatafeedResponse = putDatafeedListener.actionGet(); assertNotNull(putDatafeedResponse); PlainActionFuture openJobListener = PlainActionFuture.newFuture(); @@ -372,8 +383,7 @@ public void testMachineLearningStopDatafeedActionNotRestricted() throws Exceptio NodeAcknowledgedResponse openJobResponse = openJobListener.actionGet(); assertNotNull(openJobResponse); PlainActionFuture startDatafeedListener = PlainActionFuture.newFuture(); - client().execute(StartDatafeedAction.INSTANCE, - new StartDatafeedAction.Request(datafeedId, 0L), startDatafeedListener); + client().execute(StartDatafeedAction.INSTANCE, new StartDatafeedAction.Request(datafeedId, 0L), startDatafeedListener); NodeAcknowledgedResponse startDatafeedResponse = startDatafeedListener.actionGet(); assertNotNull(startDatafeedResponse); @@ -389,8 +399,10 @@ public void testMachineLearningStopDatafeedActionNotRestricted() throws Exceptio if (invalidLicense) { // the stop datafeed due to invalid license happens async, so check if the datafeed turns into stopped state: assertBusy(() -> { - GetDatafeedsStatsAction.Response response = - client().execute(GetDatafeedsStatsAction.INSTANCE, new GetDatafeedsStatsAction.Request(datafeedId)).actionGet(); + GetDatafeedsStatsAction.Response response = client().execute( + GetDatafeedsStatsAction.INSTANCE, + new GetDatafeedsStatsAction.Request(datafeedId) + ).actionGet(); assertEquals(DatafeedState.STOPPED, response.getResponse().results().get(0).getDatafeedState()); }); } else { @@ -400,8 +412,8 @@ public void testMachineLearningStopDatafeedActionNotRestricted() throws Exceptio if (invalidLicense) { // the close due to invalid license happens async, so check if the job turns into closed state: assertBusy(() -> { - GetJobsStatsAction.Response response = - client().execute(GetJobsStatsAction.INSTANCE, new GetJobsStatsAction.Request(jobId)).actionGet(); + GetJobsStatsAction.Response response = client().execute(GetJobsStatsAction.INSTANCE, new GetJobsStatsAction.Request(jobId)) + .actionGet(); assertEquals(JobState.CLOSED, response.getResponse().results().get(0).getState()); }); } @@ -433,8 +445,8 @@ public void testMachineLearningCloseJobActionNotRestricted() throws Exception { if (invalidLicense) { // the close due to invalid license happens async, so check if the job turns into closed state: assertBusy(() -> { - GetJobsStatsAction.Response response = - client().execute(GetJobsStatsAction.INSTANCE, new GetJobsStatsAction.Request(jobId)).actionGet(); + GetJobsStatsAction.Response response = client().execute(GetJobsStatsAction.INSTANCE, new GetJobsStatsAction.Request(jobId)) + .actionGet(); assertEquals(JobState.CLOSED, response.getResponse().results().get(0).getState()); }); } else { @@ -471,9 +483,11 @@ public void testMachineLearningDeleteDatafeedActionNotRestricted() { PutJobAction.Response putJobResponse = putJobListener.actionGet(); assertNotNull(putJobResponse); PlainActionFuture putDatafeedListener = PlainActionFuture.newFuture(); - client().execute(PutDatafeedAction.INSTANCE, - new PutDatafeedAction.Request(createDatafeed(datafeedId, jobId, - Collections.singletonList(jobId))), putDatafeedListener); + client().execute( + PutDatafeedAction.INSTANCE, + new PutDatafeedAction.Request(createDatafeed(datafeedId, jobId, Collections.singletonList(jobId))), + putDatafeedListener + ); PutDatafeedAction.Response putDatafeedResponse = putDatafeedListener.actionGet(); assertNotNull(putDatafeedResponse); @@ -491,23 +505,27 @@ public void testMachineLearningCreateInferenceProcessorRestricted() { assertMLAllowed(true); putInferenceModel(modelId); - String pipeline = "{" + - " \"processors\": [\n" + - " {\n" + - " \"inference\": {\n" + - " \"target_field\": \"regression_value\",\n" + - " \"model_id\": \"modelprocessorlicensetest\",\n" + - " \"inference_config\": {\"regression\": {}},\n" + - " \"field_map\": {}\n" + - " }\n" + - " }]}\n"; + String pipeline = "{" + + " \"processors\": [\n" + + " {\n" + + " \"inference\": {\n" + + " \"target_field\": \"regression_value\",\n" + + " \"model_id\": \"modelprocessorlicensetest\",\n" + + " \"inference_config\": {\"regression\": {}},\n" + + " \"field_map\": {}\n" + + " }\n" + + " }]}\n"; // Creating a pipeline should work PlainActionFuture putPipelineListener = PlainActionFuture.newFuture(); - client().execute(PutPipelineAction.INSTANCE, - new PutPipelineRequest("test_infer_license_pipeline", + client().execute( + PutPipelineAction.INSTANCE, + new PutPipelineRequest( + "test_infer_license_pipeline", new BytesArray(pipeline.getBytes(StandardCharsets.UTF_8)), - XContentType.JSON), - putPipelineListener); + XContentType.JSON + ), + putPipelineListener + ); AcknowledgedResponse putPipelineResponse = putPipelineListener.actionGet(); assertTrue(putPipelineResponse.isAcknowledged()); @@ -517,22 +535,24 @@ public void testMachineLearningCreateInferenceProcessorRestricted() { .execute() .actionGet(); - String simulateSource = "{\n" + - " \"pipeline\": \n" + - pipeline + - " ,\n" + - " \"docs\": [\n" + - " {\"_source\": {\n" + - " \"col1\": \"female\",\n" + - " \"col2\": \"M\",\n" + - " \"col3\": \"none\",\n" + - " \"col4\": 10\n" + - " }}]\n" + - "}"; + String simulateSource = "{\n" + + " \"pipeline\": \n" + + pipeline + + " ,\n" + + " \"docs\": [\n" + + " {\"_source\": {\n" + + " \"col1\": \"female\",\n" + + " \"col2\": \"M\",\n" + + " \"col3\": \"none\",\n" + + " \"col4\": 10\n" + + " }}]\n" + + "}"; PlainActionFuture simulatePipelineListener = PlainActionFuture.newFuture(); - client().execute(SimulatePipelineAction.INSTANCE, + client().execute( + SimulatePipelineAction.INSTANCE, new SimulatePipelineRequest(new BytesArray(simulateSource.getBytes(StandardCharsets.UTF_8)), XContentType.JSON), - simulatePipelineListener); + simulatePipelineListener + ); assertThat(simulatePipelineListener.actionGet().getResults(), is(not(empty()))); @@ -554,11 +574,15 @@ public void testMachineLearningCreateInferenceProcessorRestricted() { // Creating a new pipeline with an inference processor should work putPipelineListener = PlainActionFuture.newFuture(); - client().execute(PutPipelineAction.INSTANCE, - new PutPipelineRequest("test_infer_license_pipeline_again", + client().execute( + PutPipelineAction.INSTANCE, + new PutPipelineRequest( + "test_infer_license_pipeline_again", new BytesArray(pipeline.getBytes(StandardCharsets.UTF_8)), - XContentType.JSON), - putPipelineListener); + XContentType.JSON + ), + putPipelineListener + ); putPipelineResponse = putPipelineListener.actionGet(); assertTrue(putPipelineResponse.isAcknowledged()); @@ -575,11 +599,10 @@ public void testMachineLearningCreateInferenceProcessorRestricted() { assertThat(e.getMetadata(LicenseUtils.EXPIRED_FEATURE_METADATA), hasItem(XPackField.MACHINE_LEARNING)); // Simulating the pipeline should fail - SimulateDocumentBaseResult simulateResponse = (SimulateDocumentBaseResult)client().execute(SimulatePipelineAction.INSTANCE, - new SimulatePipelineRequest(new BytesArray(simulateSource.getBytes(StandardCharsets.UTF_8)), XContentType.JSON)) - .actionGet() - .getResults() - .get(0); + SimulateDocumentBaseResult simulateResponse = (SimulateDocumentBaseResult) client().execute( + SimulatePipelineAction.INSTANCE, + new SimulatePipelineRequest(new BytesArray(simulateSource.getBytes(StandardCharsets.UTF_8)), XContentType.JSON) + ).actionGet().getResults().get(0); assertThat(simulateResponse.getFailure(), is(not(nullValue()))); assertThat((simulateResponse.getFailure()).getCause(), is(instanceOf(ElasticsearchSecurityException.class))); @@ -589,22 +612,28 @@ public void testMachineLearningCreateInferenceProcessorRestricted() { assertMLAllowed(true); // test that license restricted apis do now work PlainActionFuture putPipelineListenerNewLicense = PlainActionFuture.newFuture(); - client().execute(PutPipelineAction.INSTANCE, - new PutPipelineRequest("test_infer_license_pipeline", + client().execute( + PutPipelineAction.INSTANCE, + new PutPipelineRequest( + "test_infer_license_pipeline", new BytesArray(pipeline.getBytes(StandardCharsets.UTF_8)), - XContentType.JSON), - putPipelineListenerNewLicense); + XContentType.JSON + ), + putPipelineListenerNewLicense + ); AcknowledgedResponse putPipelineResponseNewLicense = putPipelineListenerNewLicense.actionGet(); assertTrue(putPipelineResponseNewLicense.isAcknowledged()); PlainActionFuture simulatePipelineListenerNewLicense = PlainActionFuture.newFuture(); - client().execute(SimulatePipelineAction.INSTANCE, + client().execute( + SimulatePipelineAction.INSTANCE, new SimulatePipelineRequest(new BytesArray(simulateSource.getBytes(StandardCharsets.UTF_8)), XContentType.JSON), - simulatePipelineListenerNewLicense); + simulatePipelineListenerNewLicense + ); assertThat(simulatePipelineListenerNewLicense.actionGet().getResults(), is(not(empty()))); - //both ingest pipelines should work + // both ingest pipelines should work client().prepareIndex("infer_license_test") .setPipeline("test_infer_license_pipeline") @@ -623,14 +652,17 @@ public void testMachineLearningInferModelRestricted() { assertMLAllowed(true); putInferenceModel(modelId); - PlainActionFuture inferModelSuccess = PlainActionFuture.newFuture(); - client().execute(InternalInferModelAction.INSTANCE, new InternalInferModelAction.Request( - modelId, - Collections.singletonList(Collections.emptyMap()), - RegressionConfigUpdate.EMPTY_PARAMS, - false - ), inferModelSuccess); + client().execute( + InternalInferModelAction.INSTANCE, + new InternalInferModelAction.Request( + modelId, + Collections.singletonList(Collections.emptyMap()), + RegressionConfigUpdate.EMPTY_PARAMS, + false + ), + inferModelSuccess + ); InternalInferModelAction.Response response = inferModelSuccess.actionGet(); assertThat(response.getInferenceResults(), is(not(empty()))); assertThat(response.isLicensed(), is(true)); @@ -642,12 +674,15 @@ public void testMachineLearningInferModelRestricted() { // inferring against a model should now fail ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, () -> { - client().execute(InternalInferModelAction.INSTANCE, new InternalInferModelAction.Request( - modelId, - Collections.singletonList(Collections.emptyMap()), - RegressionConfigUpdate.EMPTY_PARAMS, - false - )).actionGet(); + client().execute( + InternalInferModelAction.INSTANCE, + new InternalInferModelAction.Request( + modelId, + Collections.singletonList(Collections.emptyMap()), + RegressionConfigUpdate.EMPTY_PARAMS, + false + ) + ).actionGet(); }); assertThat(e.status(), is(RestStatus.FORBIDDEN)); assertThat(e.getMessage(), containsString("non-compliant")); @@ -655,12 +690,16 @@ public void testMachineLearningInferModelRestricted() { // Inferring with previously Licensed == true should pass, but indicate license issues inferModelSuccess = PlainActionFuture.newFuture(); - client().execute(InternalInferModelAction.INSTANCE, new InternalInferModelAction.Request( - modelId, - Collections.singletonList(Collections.emptyMap()), - RegressionConfigUpdate.EMPTY_PARAMS, - true - ), inferModelSuccess); + client().execute( + InternalInferModelAction.INSTANCE, + new InternalInferModelAction.Request( + modelId, + Collections.singletonList(Collections.emptyMap()), + RegressionConfigUpdate.EMPTY_PARAMS, + true + ), + inferModelSuccess + ); response = inferModelSuccess.actionGet(); assertThat(response.getInferenceResults(), is(not(empty()))); assertThat(response.isLicensed(), is(false)); @@ -671,12 +710,16 @@ public void testMachineLearningInferModelRestricted() { assertMLAllowed(true); PlainActionFuture listener = PlainActionFuture.newFuture(); - client().execute(InternalInferModelAction.INSTANCE, new InternalInferModelAction.Request( - modelId, - Collections.singletonList(Collections.emptyMap()), - RegressionConfigUpdate.EMPTY_PARAMS, - false - ), listener); + client().execute( + InternalInferModelAction.INSTANCE, + new InternalInferModelAction.Request( + modelId, + Collections.singletonList(Collections.emptyMap()), + RegressionConfigUpdate.EMPTY_PARAMS, + false + ), + listener + ); assertThat(listener.actionGet().getInferenceResults(), is(not(empty()))); } @@ -706,8 +749,13 @@ public void testInferenceAggRestricted() { Map bucketPaths = new HashMap<>(); bucketPaths.put("feature1", "avg_feature1"); - InferencePipelineAggregationBuilder inferenceAgg = - new InferencePipelineAggregationBuilder("infer_agg", new SetOnce<>(modelLoading), licenseState, settings, bucketPaths); + InferencePipelineAggregationBuilder inferenceAgg = new InferencePipelineAggregationBuilder( + "infer_agg", + new SetOnce<>(modelLoading), + licenseState, + settings, + bucketPaths + ); inferenceAgg.setModelId(modelId); termsAgg.subAggregation(inferenceAgg); @@ -724,8 +772,10 @@ public void testInferenceAggRestricted() { // inferring against a model should now fail SearchRequest invalidSearch = new SearchRequest(index); invalidSearch.source().aggregation(termsAgg); - ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, - () -> client().search(invalidSearch).actionGet()); + ElasticsearchSecurityException e = expectThrows( + ElasticsearchSecurityException.class, + () -> client().search(invalidSearch).actionGet() + ); assertThat(e.status(), is(RestStatus.FORBIDDEN)); assertThat(e.getMessage(), containsString("current license is non-compliant for [ml]")); @@ -735,14 +785,14 @@ public void testInferenceAggRestricted() { private void putInferenceModel(String modelId) { TrainedModelConfig config = TrainedModelConfig.builder() .setParsedDefinition( - new TrainedModelDefinition.Builder() - .setTrainedModel( - Tree.builder() - .setTargetType(TargetType.REGRESSION) - .setFeatureNames(Collections.singletonList("feature1")) - .setNodes(TreeNode.builder(0).setLeafValue(1.0)) - .build()) - .setPreProcessors(Collections.emptyList())) + new TrainedModelDefinition.Builder().setTrainedModel( + Tree.builder() + .setTargetType(TargetType.REGRESSION) + .setFeatureNames(Collections.singletonList("feature1")) + .setNodes(TreeNode.builder(0).setLeafValue(1.0)) + .build() + ).setPreProcessors(Collections.emptyList()) + ) .setModelId(modelId) .setDescription("test model for classification") .setInput(new TrainedModelInput(Collections.singletonList("feature1"))) diff --git a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/AnnotationIndexIT.java b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/AnnotationIndexIT.java index 6d1ddc1a10d08..2574fac8b8e9d 100644 --- a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/AnnotationIndexIT.java +++ b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/AnnotationIndexIT.java @@ -42,7 +42,7 @@ public class AnnotationIndexIT extends MlSingleNodeTestCase { @Override - protected Settings nodeSettings() { + protected Settings nodeSettings() { Settings.Builder newSettings = Settings.builder(); newSettings.put(super.nodeSettings()); newSettings.put(XPackSettings.SECURITY_ENABLED.getKey(), false); @@ -76,15 +76,15 @@ public void testAliasesMovedFromOldToNew() throws Exception { // Create an old annotations index with both read and write aliases pointing at it. String oldIndex = randomFrom(AnnotationIndex.OLD_INDEX_NAMES); - CreateIndexRequest createIndexRequest = - new CreateIndexRequest(oldIndex) - .mapping(AnnotationIndex.annotationsMapping()) - .settings(Settings.builder() + CreateIndexRequest createIndexRequest = new CreateIndexRequest(oldIndex).mapping(AnnotationIndex.annotationsMapping()) + .settings( + Settings.builder() .put(IndexMetadata.SETTING_AUTO_EXPAND_REPLICAS, "0-1") .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, "1") - .put(IndexMetadata.SETTING_INDEX_HIDDEN, true)) - .alias(new Alias(AnnotationIndex.READ_ALIAS_NAME).isHidden(true)) - .alias(new Alias(AnnotationIndex.WRITE_ALIAS_NAME).isHidden(true)); + .put(IndexMetadata.SETTING_INDEX_HIDDEN, true) + ) + .alias(new Alias(AnnotationIndex.READ_ALIAS_NAME).isHidden(true)) + .alias(new Alias(AnnotationIndex.WRITE_ALIAS_NAME).isHidden(true)); client().execute(CreateIndexAction.INSTANCE, createIndexRequest).actionGet(); // Because the old annotations index name began with .ml, it will trigger the new annotations index to be created. @@ -92,7 +92,8 @@ public void testAliasesMovedFromOldToNew() throws Exception { // switched to only point at the new index. assertBusy(() -> { assertTrue(annotationsIndexExists()); - ImmutableOpenMap> aliases = client().admin().indices() + ImmutableOpenMap> aliases = client().admin() + .indices() .prepareGetAliases(AnnotationIndex.READ_ALIAS_NAME, AnnotationIndex.WRITE_ALIAS_NAME) .setIndicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN_CLOSED_HIDDEN) .get() @@ -175,7 +176,8 @@ private boolean annotationsIndexExists() { private int numberOfAnnotationsAliases() { int count = 0; - ImmutableOpenMap> aliases = client().admin().indices() + ImmutableOpenMap> aliases = client().admin() + .indices() .prepareGetAliases(AnnotationIndex.READ_ALIAS_NAME, AnnotationIndex.WRITE_ALIAS_NAME) .setIndicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN_CLOSED_HIDDEN) .get() diff --git a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/AnomalyJobCRUDIT.java b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/AnomalyJobCRUDIT.java index c870668b83ce2..49620406adb3f 100644 --- a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/AnomalyJobCRUDIT.java +++ b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/AnomalyJobCRUDIT.java @@ -6,17 +6,6 @@ */ package org.elasticsearch.xpack.ml.integration; -import static org.elasticsearch.xpack.core.ClientHelper.ML_ORIGIN; -import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.Matchers.hasSize; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.Date; -import java.util.HashSet; -import java.util.List; - import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.support.WriteRequest; @@ -27,8 +16,8 @@ import org.elasticsearch.cluster.service.MasterService; import org.elasticsearch.common.settings.ClusterSettings; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.ml.action.GetModelSnapshotsAction; import org.elasticsearch.xpack.core.ml.action.OpenJobAction; import org.elasticsearch.xpack.core.ml.action.PutJobAction; @@ -51,19 +40,37 @@ import org.elasticsearch.xpack.ml.utils.persistence.ResultsPersisterService; import org.junit.Before; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Date; +import java.util.HashSet; +import java.util.List; + +import static org.elasticsearch.xpack.core.ClientHelper.ML_ORIGIN; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.hasSize; + public class AnomalyJobCRUDIT extends MlSingleNodeTestCase { private JobResultsPersister jobResultsPersister; + @Before public void createComponents() throws Exception { ThreadPool tp = mockThreadPool(); - ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, - new HashSet<>(Arrays.asList(InferenceProcessor.MAX_INFERENCE_PROCESSORS, - MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING, - OperationRouting.USE_ADAPTIVE_REPLICA_SELECTION_SETTING, - ResultsPersisterService.PERSIST_RESULTS_MAX_RETRIES, - ClusterService.USER_DEFINED_METADATA, - ClusterApplierService.CLUSTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING))); + ClusterSettings clusterSettings = new ClusterSettings( + Settings.EMPTY, + new HashSet<>( + Arrays.asList( + InferenceProcessor.MAX_INFERENCE_PROCESSORS, + MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING, + OperationRouting.USE_ADAPTIVE_REPLICA_SELECTION_SETTING, + ResultsPersisterService.PERSIST_RESULTS_MAX_RETRIES, + ClusterService.USER_DEFINED_METADATA, + ClusterApplierService.CLUSTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING + ) + ) + ); ClusterService clusterService = new ClusterService(Settings.EMPTY, clusterSettings, tp); OriginSettingClient originSettingClient = new OriginSettingClient(client(), ML_ORIGIN); @@ -82,55 +89,62 @@ public void testUpdateModelMemoryLimitOnceEstablished() { String jobId = "memory-limit-established"; createJob(jobId); jobResultsPersister.persistModelSizeStats( - new ModelSizeStats.Builder(jobId) - .setTimestamp(new Date()) - .setLogTime(new Date()) - .setModelBytes(10000000).build(), () -> false); + new ModelSizeStats.Builder(jobId).setTimestamp(new Date()).setLogTime(new Date()).setModelBytes(10000000).build(), + () -> false + ); jobResultsPersister.commitResultWrites(jobId); - ElasticsearchStatusException iae = expectThrows(ElasticsearchStatusException.class, () -> client().execute(UpdateJobAction.INSTANCE, - new UpdateJobAction.Request(jobId, - new JobUpdate.Builder(jobId) - .setAnalysisLimits(new AnalysisLimits(5L, 0L)) - .build())).actionGet()); + ElasticsearchStatusException iae = expectThrows( + ElasticsearchStatusException.class, + () -> client().execute( + UpdateJobAction.INSTANCE, + new UpdateJobAction.Request(jobId, new JobUpdate.Builder(jobId).setAnalysisLimits(new AnalysisLimits(5L, 0L)).build()) + ).actionGet() + ); assertThat(iae.getMessage(), containsString("model_memory_limit cannot be decreased below current usage")); // Shouldn't throw - client().execute(UpdateJobAction.INSTANCE, - new UpdateJobAction.Request(jobId, - new JobUpdate.Builder(jobId) - .setAnalysisLimits(new AnalysisLimits(30L, 0L)) - .build())).actionGet(); + client().execute( + UpdateJobAction.INSTANCE, + new UpdateJobAction.Request(jobId, new JobUpdate.Builder(jobId).setAnalysisLimits(new AnalysisLimits(30L, 0L)).build()) + ).actionGet(); } public void testCreateWithExistingCategorizerDocs() { String jobId = "job-id-with-existing-docs"; - testCreateWithExistingDocs(client().prepareIndex(".ml-state-000001") - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .setId(jobId + "_categorizer_state#1") - .setSource("{}", XContentType.JSON) - .request(), - jobId); + testCreateWithExistingDocs( + client().prepareIndex(".ml-state-000001") + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + .setId(jobId + "_categorizer_state#1") + .setSource("{}", XContentType.JSON) + .request(), + jobId + ); } public void testCreateWithExistingQuantilesDocs() { String jobId = "job-id-with-existing-docs"; - testCreateWithExistingDocs(client().prepareIndex(".ml-state-000001") - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .setId(jobId + "_quantiles") - .setSource("{}", XContentType.JSON) - .request(), jobId); + testCreateWithExistingDocs( + client().prepareIndex(".ml-state-000001") + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + .setId(jobId + "_quantiles") + .setSource("{}", XContentType.JSON) + .request(), + jobId + ); } public void testCreateWithExistingResultsDocs() { String jobId = "job-id-with-existing-docs"; - testCreateWithExistingDocs(client().prepareIndex(".ml-anomalies-shared") - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .setId(jobId + "_1464739200000_1") - .setSource("{\"job_id\": \"" + jobId + "\"}", XContentType.JSON) - .request(), - jobId); + testCreateWithExistingDocs( + client().prepareIndex(".ml-anomalies-shared") + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + .setId(jobId + "_1464739200000_1") + .setSource("{\"job_id\": \"" + jobId + "\"}", XContentType.JSON) + .request(), + jobId + ); } public void testPutJobWithClosedResultsIndex() { @@ -138,8 +152,10 @@ public void testPutJobWithClosedResultsIndex() { client().admin().indices().prepareCreate(".ml-anomalies-shared").get(); client().admin().indices().prepareClose(".ml-anomalies-shared").get(); ElasticsearchStatusException ex = expectThrows(ElasticsearchStatusException.class, () -> createJob(jobId)); - assertThat(ex.getMessage(), - containsString("Cannot create job [job-with-closed-results-index] as it requires closed index [.ml-anomalies-*]")); + assertThat( + ex.getMessage(), + containsString("Cannot create job [job-with-closed-results-index] as it requires closed index [.ml-anomalies-*]") + ); client().admin().indices().prepareDelete(".ml-anomalies-shared").get(); } @@ -148,8 +164,10 @@ public void testPutJobWithClosedStateIndex() { client().admin().indices().prepareCreate(".ml-state-000001").get(); client().admin().indices().prepareClose(".ml-state-000001").setWaitForActiveShards(0).get(); ElasticsearchStatusException ex = expectThrows(ElasticsearchStatusException.class, () -> createJob(jobId)); - assertThat(ex.getMessage(), - containsString("Cannot create job [job-with-closed-results-index] as it requires closed index [.ml-state*]")); + assertThat( + ex.getMessage(), + containsString("Cannot create job [job-with-closed-results-index] as it requires closed index [.ml-state*]") + ); client().admin().indices().prepareDelete(".ml-state-000001").get(); } @@ -157,29 +175,30 @@ public void testOpenJobWithOldSnapshot() { String jobId = "open-job-with-old-model-snapshot"; Date timestamp = new Date(); createJob(jobId); - ModelSnapshot snapshot = new ModelSnapshot - .Builder(jobId) - .setMinVersion("6.0.0") + ModelSnapshot snapshot = new ModelSnapshot.Builder(jobId).setMinVersion("6.0.0") .setSnapshotId("snap_1") .setQuantiles(new Quantiles(jobId, timestamp, "quantiles-1")) .setSnapshotDocCount(1) .setModelSizeStats(new ModelSizeStats.Builder(jobId).setTimestamp(timestamp).setLogTime(timestamp)) .build(); indexModelSnapshot(snapshot); - GetModelSnapshotsAction.Response getResponse = - client().execute(GetModelSnapshotsAction.INSTANCE, new GetModelSnapshotsAction.Request(jobId, "snap_1")).actionGet(); + GetModelSnapshotsAction.Response getResponse = client().execute( + GetModelSnapshotsAction.INSTANCE, + new GetModelSnapshotsAction.Request(jobId, "snap_1") + ).actionGet(); assertThat(getResponse.getResources().results(), hasSize(1)); client().execute(RevertModelSnapshotAction.INSTANCE, new RevertModelSnapshotAction.Request(jobId, "snap_1")).actionGet(); // should fail? - Exception ex = expectThrows(Exception.class, - () -> client() - .execute(OpenJobAction.INSTANCE, new OpenJobAction.Request(jobId)) - .actionGet()); - assertThat(ex.getMessage(), + Exception ex = expectThrows( + Exception.class, + () -> client().execute(OpenJobAction.INSTANCE, new OpenJobAction.Request(jobId)).actionGet() + ); + assertThat( + ex.getMessage(), containsString( - "[open-job-with-old-model-snapshot] job snapshot [snap_1] has min version before [7.0.0], " + - "please revert to a newer model snapshot or reset the job" + "[open-job-with-old-model-snapshot] job snapshot [snap_1] has min version before [7.0.0], " + + "please revert to a newer model snapshot or reset the job" ) ); } diff --git a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/AutodetectResultProcessorIT.java b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/AutodetectResultProcessorIT.java index be7568aa18499..3447b8677a825 100644 --- a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/AutodetectResultProcessorIT.java +++ b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/AutodetectResultProcessorIT.java @@ -25,14 +25,14 @@ import org.elasticsearch.common.settings.ClusterSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.reindex.ReindexPlugin; import org.elasticsearch.indices.TestIndexNameExpressionResolver; import org.elasticsearch.ingest.common.IngestCommonPlugin; import org.elasticsearch.plugins.Plugin; +import org.elasticsearch.reindex.ReindexPlugin; import org.elasticsearch.search.SearchHit; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ClientHelper; import org.elasticsearch.xpack.core.action.util.QueryPage; import org.elasticsearch.xpack.core.ml.action.DeleteJobAction; @@ -134,13 +134,14 @@ protected Collection> getPlugins() { ReindexPlugin.class, MockPainlessScriptEngine.TestPlugin.class, // ILM is required for .ml-state template index settings - IndexLifecycle.class); + IndexLifecycle.class + ); } @Before public void createComponents() throws Exception { Settings.Builder builder = Settings.builder() - .put(UnassignedInfo.INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING.getKey(), TimeValue.timeValueSeconds(1)); + .put(UnassignedInfo.INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING.getKey(), TimeValue.timeValueSeconds(1)); AnomalyDetectionAuditor auditor = new AnomalyDetectionAuditor(client(), getInstanceFromNode(ClusterService.class)); jobResultsProvider = new JobResultsProvider(client(), builder.build(), TestIndexNameExpressionResolver.newInstance()); renormalizer = mock(Renormalizer.class); @@ -148,26 +149,33 @@ public void createComponents() throws Exception { capturedUpdateModelSnapshotOnJobRequests = new ArrayList<>(); ThreadPool tp = mockThreadPool(); Settings settings = Settings.builder().put("node.name", "InferenceProcessorFactoryTests_node").build(); - ClusterSettings clusterSettings = new ClusterSettings(settings, - new HashSet<>(Arrays.asList(InferenceProcessor.MAX_INFERENCE_PROCESSORS, - MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING, - OperationRouting.USE_ADAPTIVE_REPLICA_SELECTION_SETTING, - ClusterService.USER_DEFINED_METADATA, - ResultsPersisterService.PERSIST_RESULTS_MAX_RETRIES, - ClusterApplierService.CLUSTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING))); + ClusterSettings clusterSettings = new ClusterSettings( + settings, + new HashSet<>( + Arrays.asList( + InferenceProcessor.MAX_INFERENCE_PROCESSORS, + MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING, + OperationRouting.USE_ADAPTIVE_REPLICA_SELECTION_SETTING, + ClusterService.USER_DEFINED_METADATA, + ResultsPersisterService.PERSIST_RESULTS_MAX_RETRIES, + ClusterApplierService.CLUSTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING + ) + ) + ); ClusterService clusterService = new ClusterService(settings, clusterSettings, tp); OriginSettingClient originSettingClient = new OriginSettingClient(client(), ClientHelper.ML_ORIGIN); resultsPersisterService = new ResultsPersisterService(tp, originSettingClient, clusterService, settings); resultProcessor = new AutodetectResultProcessor( - client(), - auditor, - JOB_ID, - renormalizer, - new JobResultsPersister(originSettingClient, resultsPersisterService), - new AnnotationPersister(resultsPersisterService), - process, - new ModelSizeStats.Builder(JOB_ID).build(), - new TimingStats(JOB_ID)) { + client(), + auditor, + JOB_ID, + renormalizer, + new JobResultsPersister(originSettingClient, resultsPersisterService), + new AnnotationPersister(resultsPersisterService), + process, + new ModelSizeStats.Builder(JOB_ID).build(), + new TimingStats(JOB_ID) + ) { @Override protected void updateModelSnapshotOnJob(ModelSnapshot modelSnapshot) { capturedUpdateModelSnapshotOnJobRequests.add(modelSnapshot); @@ -180,8 +188,13 @@ protected void updateModelSnapshotOnJob(ModelSnapshot modelSnapshot) { // A a result they must create the index as part of the test setup. Do not // copy this setup to tests that run jobs in the way they are run in production. PlainActionFuture future = new PlainActionFuture<>(); - createStateIndexAndAliasIfNecessary(client(), ClusterState.EMPTY_STATE, TestIndexNameExpressionResolver.newInstance(), - MasterNodeRequest.DEFAULT_MASTER_NODE_TIMEOUT, future); + createStateIndexAndAliasIfNecessary( + client(), + ClusterState.EMPTY_STATE, + TestIndexNameExpressionResolver.newInstance(), + MasterNodeRequest.DEFAULT_MASTER_NODE_TIMEOUT, + future + ); future.get(); } @@ -193,7 +206,8 @@ public void deleteJob() throws Exception { // Verify that deleting job also deletes associated model snapshots annotations assertThat( getAnnotations().stream().map(Annotation::getAnnotation).collect(toList()), - everyItem(not(startsWith("Job model snapshot")))); + everyItem(not(startsWith("Job model snapshot"))) + ); } public void testProcessResults() throws Exception { @@ -237,9 +251,10 @@ public void testProcessResults() throws Exception { QueryPage persistedInfluencers = getInfluencers(); assertResultsAreSame(influencers, persistedInfluencers); - QueryPage persistedDefinition = - getCategoryDefinition(randomBoolean() ? categoryDefinition.getCategoryId() : null, - randomBoolean() ? categoryDefinition.getPartitionFieldValue() : null); + QueryPage persistedDefinition = getCategoryDefinition( + randomBoolean() ? categoryDefinition.getCategoryId() : null, + randomBoolean() ? categoryDefinition.getPartitionFieldValue() : null + ); assertEquals(1, persistedDefinition.count()); assertEquals(categoryDefinition, persistedDefinition.results().get(0)); @@ -264,15 +279,17 @@ public void testProcessResults() throws Exception { assertEquals(quantiles, persistedQuantiles.get()); // Verify that there are two annotations: - // 1. one related to creating model snapshot - // 2. one for {@link Annotation} result + // 1. one related to creating model snapshot + // 2. one for {@link Annotation} result List annotations = getAnnotations(); assertThat("Annotations were: " + annotations.toString(), annotations, hasSize(2)); assertThat( annotations.stream().map(Annotation::getAnnotation).collect(toList()), containsInAnyOrder( new ParameterizedMessage("Job model snapshot with id [{}] stored", modelSnapshot.getSnapshotId()).getFormattedMessage(), - annotation.getAnnotation())); + annotation.getAnnotation() + ) + ); } public void testProcessResults_ModelSnapshot() throws Exception { @@ -292,8 +309,12 @@ public void testProcessResults_ModelSnapshot() throws Exception { assertThat(annotations, hasSize(1)); assertThat( annotations.get(0).getAnnotation(), - is(equalTo( - new ParameterizedMessage("Job model snapshot with id [{}] stored", modelSnapshot.getSnapshotId()).getFormattedMessage()))); + is( + equalTo( + new ParameterizedMessage("Job model snapshot with id [{}] stored", modelSnapshot.getSnapshotId()).getFormattedMessage() + ) + ) + ); // Verify that deleting model snapshot also deletes associated annotation deleteModelSnapshot(JOB_ID, modelSnapshot.getSnapshotId()); @@ -301,17 +322,16 @@ public void testProcessResults_ModelSnapshot() throws Exception { } public void testProcessResults_TimingStats() throws Exception { - ResultsBuilder resultsBuilder = new ResultsBuilder() - .addBucket(createBucket(true, 100)) - .addBucket(createBucket(true, 1000)) - .addBucket(createBucket(true, 100)) - .addBucket(createBucket(true, 1000)) - .addBucket(createBucket(true, 100)) - .addBucket(createBucket(true, 1000)) - .addBucket(createBucket(true, 100)) - .addBucket(createBucket(true, 1000)) - .addBucket(createBucket(true, 100)) - .addBucket(createBucket(true, 1000)); + ResultsBuilder resultsBuilder = new ResultsBuilder().addBucket(createBucket(true, 100)) + .addBucket(createBucket(true, 1000)) + .addBucket(createBucket(true, 100)) + .addBucket(createBucket(true, 1000)) + .addBucket(createBucket(true, 100)) + .addBucket(createBucket(true, 1000)) + .addBucket(createBucket(true, 100)) + .addBucket(createBucket(true, 1000)) + .addBucket(createBucket(true, 100)) + .addBucket(createBucket(true, 1000)); when(process.readAutodetectResults()).thenReturn(resultsBuilder.build().iterator()); resultProcessor.process(); @@ -364,12 +384,11 @@ public void testDeleteInterimResults() throws Exception { Bucket nonInterimBucket = createBucket(false); Bucket interimBucket = createBucket(true); - ResultsBuilder resultsBuilder = new ResultsBuilder() - .addRecords(createRecords(true)) - .addInfluencers(createInfluencers(true)) - .addBucket(interimBucket) // this will persist the interim results - .addFlushAcknowledgement(createFlushAcknowledgement()) - .addBucket(nonInterimBucket); // and this will delete the interim results + ResultsBuilder resultsBuilder = new ResultsBuilder().addRecords(createRecords(true)) + .addInfluencers(createInfluencers(true)) + .addBucket(interimBucket) // this will persist the interim results + .addFlushAcknowledgement(createFlushAcknowledgement()) + .addBucket(nonInterimBucket); // and this will delete the interim results when(process.readAutodetectResults()).thenReturn(resultsBuilder.build().iterator()); resultProcessor.process(); @@ -393,16 +412,15 @@ public void testMultipleFlushesBetweenPersisting() throws Exception { Bucket finalBucket = createBucket(true); List finalAnomalyRecords = createRecords(true); - ResultsBuilder resultsBuilder = new ResultsBuilder() - .addRecords(createRecords(true)) - .addInfluencers(createInfluencers(true)) - .addBucket(createBucket(true)) // this will persist the interim results - .addFlushAcknowledgement(createFlushAcknowledgement()) - .addRecords(createRecords(true)) - .addBucket(createBucket(true)) // and this will delete the interim results and persist the new interim bucket & records - .addFlushAcknowledgement(createFlushAcknowledgement()) - .addRecords(finalAnomalyRecords) - .addBucket(finalBucket); // this deletes the previous interim and persists final bucket & records + ResultsBuilder resultsBuilder = new ResultsBuilder().addRecords(createRecords(true)) + .addInfluencers(createInfluencers(true)) + .addBucket(createBucket(true)) // this will persist the interim results + .addFlushAcknowledgement(createFlushAcknowledgement()) + .addRecords(createRecords(true)) + .addBucket(createBucket(true)) // and this will delete the interim results and persist the new interim bucket & records + .addFlushAcknowledgement(createFlushAcknowledgement()) + .addRecords(finalAnomalyRecords) + .addBucket(finalBucket); // this deletes the previous interim and persists final bucket & records when(process.readAutodetectResults()).thenReturn(resultsBuilder.build().iterator()); resultProcessor.process(); @@ -424,10 +442,9 @@ public void testEndOfStreamTriggersPersisting() throws Exception { List firstSetOfRecords = createRecords(false); List secondSetOfRecords = createRecords(false); - ResultsBuilder resultsBuilder = new ResultsBuilder() - .addRecords(firstSetOfRecords) - .addBucket(bucket) // bucket triggers persistence - .addRecords(secondSetOfRecords); + ResultsBuilder resultsBuilder = new ResultsBuilder().addRecords(firstSetOfRecords) + .addBucket(bucket) // bucket triggers persistence + .addRecords(secondSetOfRecords); when(process.readAutodetectResults()).thenReturn(resultsBuilder.build().iterator()); resultProcessor.process(); @@ -478,7 +495,7 @@ private static List createRecords(boolean isInterim) { int count = randomIntBetween(0, 100); Date now = randomDate(); - for (int i=0; i createInfluencers(boolean isInterim) { int count = randomIntBetween(0, 100); Date now = new Date(); - for (int i=0; i getCategoryDefinition(Long categoryId, Str AtomicReference errorHolder = new AtomicReference<>(); AtomicReference> resultHolder = new AtomicReference<>(); CountDownLatch latch = new CountDownLatch(1); - jobResultsProvider.categoryDefinitions(JOB_ID, categoryId, partitionFieldValue, false, (categoryId == null) ? 0 : null, - (categoryId == null) ? 100 : null, r -> { - resultHolder.set(r); - latch.countDown(); - }, e -> { - errorHolder.set(e); - latch.countDown(); - }, client()); + jobResultsProvider.categoryDefinitions( + JOB_ID, + categoryId, + partitionFieldValue, + false, + (categoryId == null) ? 0 : null, + (categoryId == null) ? 100 : null, + r -> { + resultHolder.set(r); + latch.countDown(); + }, + e -> { + errorHolder.set(e); + latch.countDown(); + }, + client() + ); latch.await(); if (errorHolder.get() != null) { throw errorHolder.get(); @@ -731,7 +755,9 @@ private QueryPage getModelSnapshots() throws Exception { private List getAnnotations() throws Exception { // Refresh the annotations index so that recently indexed annotation docs are visible. - client().admin().indices().prepareRefresh(AnnotationIndex.LATEST_INDEX_NAME) + client().admin() + .indices() + .prepareRefresh(AnnotationIndex.LATEST_INDEX_NAME) .setIndicesOptions(IndicesOptions.STRICT_EXPAND_OPEN_HIDDEN_FORBID_CLOSED) .execute() .actionGet(); diff --git a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/BasicDistributedJobsIT.java b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/BasicDistributedJobsIT.java index b65b71b6b7a0a..4f0fb42f71b2b 100644 --- a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/BasicDistributedJobsIT.java +++ b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/BasicDistributedJobsIT.java @@ -6,34 +6,16 @@ */ package org.elasticsearch.xpack.ml.integration; -import static org.elasticsearch.persistent.PersistentTasksClusterService.needsReassignment; -import static org.elasticsearch.test.NodeRoles.addRoles; -import static org.elasticsearch.test.NodeRoles.onlyRole; -import static org.elasticsearch.test.NodeRoles.removeRoles; -import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.hasEntry; -import static org.hamcrest.Matchers.notNullValue; - -import java.io.IOException; -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import java.util.Set; -import java.util.concurrent.CopyOnWriteArrayList; -import java.util.concurrent.TimeUnit; - import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.node.DiscoveryNodeRole; -import org.elasticsearch.core.CheckedRunnable; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.core.CheckedRunnable; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.persistent.PersistentTasksCustomMetadata; import org.elasticsearch.persistent.PersistentTasksCustomMetadata.PersistentTask; import org.elasticsearch.search.aggregations.AggregationBuilders; @@ -41,6 +23,7 @@ import org.elasticsearch.search.aggregations.bucket.histogram.HistogramAggregationBuilder; import org.elasticsearch.search.aggregations.metrics.MaxAggregationBuilder; import org.elasticsearch.test.InternalTestCluster; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.ml.MlTasks; import org.elasticsearch.xpack.core.ml.action.CloseJobAction; import org.elasticsearch.xpack.core.ml.action.GetDatafeedsStatsAction; @@ -64,6 +47,23 @@ import org.junit.After; import org.junit.Before; +import java.io.IOException; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Set; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.TimeUnit; + +import static org.elasticsearch.persistent.PersistentTasksClusterService.needsReassignment; +import static org.elasticsearch.test.NodeRoles.addRoles; +import static org.elasticsearch.test.NodeRoles.onlyRole; +import static org.elasticsearch.test.NodeRoles.removeRoles; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasEntry; +import static org.hamcrest.Matchers.notNullValue; + public class BasicDistributedJobsIT extends BaseMlIntegTestCase { @Before @@ -72,12 +72,15 @@ public void setLogging() { client().admin() .cluster() .prepareUpdateSettings() - .setPersistentSettings(Settings.builder() - .put("logger.org.elasticsearch.xpack.ml.action.TransportCloseJobAction", "TRACE") - .put("logger.org.elasticsearch.xpack.ml.action.TransportOpenJobAction", "TRACE") - .put("logger.org.elasticsearch.xpack.ml.job.task.OpenJobPersistentTasksExecutor", "TRACE") - .put("logger.org.elasticsearch.xpack.ml.job.process.autodetect.AutodetectProcessManager", "TRACE") - .build()).get(); + .setPersistentSettings( + Settings.builder() + .put("logger.org.elasticsearch.xpack.ml.action.TransportCloseJobAction", "TRACE") + .put("logger.org.elasticsearch.xpack.ml.action.TransportOpenJobAction", "TRACE") + .put("logger.org.elasticsearch.xpack.ml.job.task.OpenJobPersistentTasksExecutor", "TRACE") + .put("logger.org.elasticsearch.xpack.ml.job.process.autodetect.AutodetectProcessManager", "TRACE") + .build() + ) + .get(); } @After @@ -85,12 +88,15 @@ public void unsetLogging() { client().admin() .cluster() .prepareUpdateSettings() - .setPersistentSettings(Settings.builder() - .putNull("logger.org.elasticsearch.xpack.ml.action.TransportCloseJobAction") - .putNull("logger.org.elasticsearch.xpack.ml.action.TransportOpenJobAction") - .putNull("logger.org.elasticsearch.xpack.ml.job.task.OpenJobPersistentTasksExecutor") - .putNull("logger.org.elasticsearch.xpack.ml.job.process.autodetect.AutodetectProcessManager") - .build()).get(); + .setPersistentSettings( + Settings.builder() + .putNull("logger.org.elasticsearch.xpack.ml.action.TransportCloseJobAction") + .putNull("logger.org.elasticsearch.xpack.ml.action.TransportOpenJobAction") + .putNull("logger.org.elasticsearch.xpack.ml.job.task.OpenJobPersistentTasksExecutor") + .putNull("logger.org.elasticsearch.xpack.ml.job.process.autodetect.AutodetectProcessManager") + .build() + ) + .get(); } public void testFailOverBasics() throws Exception { @@ -135,8 +141,10 @@ public void testFailOverBasics_withDataFeeder() throws Exception { DatafeedConfig.Builder configBuilder = createDatafeedBuilder("data_feed_id", job.getId(), Collections.singletonList("*")); MaxAggregationBuilder maxAggregation = AggregationBuilders.max("time").field("time"); - HistogramAggregationBuilder histogramAggregation = AggregationBuilders.histogram("time").interval(60000) - .subAggregation(maxAggregation).field("time"); + HistogramAggregationBuilder histogramAggregation = AggregationBuilders.histogram("time") + .interval(60000) + .subAggregation(maxAggregation) + .field("time"); configBuilder.setParsedAggregations(AggregatorFactories.builder().addAggregator(histogramAggregation)); configBuilder.setFrequency(TimeValue.timeValueMinutes(2)); @@ -155,8 +163,10 @@ public void testFailOverBasics_withDataFeeder() throws Exception { client().execute(StartDatafeedAction.INSTANCE, startDataFeedRequest); assertBusy(() -> { - GetDatafeedsStatsAction.Response statsResponse = - client().execute(GetDatafeedsStatsAction.INSTANCE, new GetDatafeedsStatsAction.Request(config.getId())).actionGet(); + GetDatafeedsStatsAction.Response statsResponse = client().execute( + GetDatafeedsStatsAction.INSTANCE, + new GetDatafeedsStatsAction.Request(config.getId()) + ).actionGet(); assertEquals(1, statsResponse.getResponse().results().size()); assertEquals(DatafeedState.STARTED, statsResponse.getResponse().results().get(0).getDatafeedState()); }); @@ -166,8 +176,10 @@ public void testFailOverBasics_withDataFeeder() throws Exception { ensureStableCluster(3); awaitJobOpenedAndAssigned(job.getId(), null); assertBusy(() -> { - GetDatafeedsStatsAction.Response statsResponse = - client().execute(GetDatafeedsStatsAction.INSTANCE, new GetDatafeedsStatsAction.Request(config.getId())).actionGet(); + GetDatafeedsStatsAction.Response statsResponse = client().execute( + GetDatafeedsStatsAction.INSTANCE, + new GetDatafeedsStatsAction.Request(config.getId()) + ).actionGet(); assertEquals(1, statsResponse.getResponse().results().size()); assertEquals(DatafeedState.STARTED, statsResponse.getResponse().results().get(0).getDatafeedState()); }); @@ -177,8 +189,10 @@ public void testFailOverBasics_withDataFeeder() throws Exception { ensureStableCluster(2); awaitJobOpenedAndAssigned(job.getId(), null); assertBusy(() -> { - GetDatafeedsStatsAction.Response statsResponse = - client().execute(GetDatafeedsStatsAction.INSTANCE, new GetDatafeedsStatsAction.Request(config.getId())).actionGet(); + GetDatafeedsStatsAction.Response statsResponse = client().execute( + GetDatafeedsStatsAction.INSTANCE, + new GetDatafeedsStatsAction.Request(config.getId()) + ).actionGet(); assertEquals(1, statsResponse.getResponse().results().size()); assertEquals(DatafeedState.STARTED, statsResponse.getResponse().results().get(0).getDatafeedState()); }); @@ -189,9 +203,7 @@ public void testJobAutoClose() throws Exception { internalCluster().startNode(removeRoles(Set.of(DiscoveryNodeRole.ML_ROLE))); internalCluster().startNode(addRoles(Set.of(DiscoveryNodeRole.ML_ROLE))); - client().admin().indices().prepareCreate("data") - .setMapping("time", "type=date") - .get(); + client().admin().indices().prepareCreate("data").setMapping("time", "type=date").get(); IndexRequest indexRequest = new IndexRequest("data"); indexRequest.source("time", 1407081600L); @@ -288,10 +300,11 @@ public void testMaxConcurrentJobAllocations() throws Exception { ensureStableCluster(numMlNodes + 1); int maxConcurrentJobAllocations = randomIntBetween(1, 4); - client().admin().cluster().prepareUpdateSettings() - .setPersistentSettings(Settings.builder() - .put(MachineLearning.CONCURRENT_JOB_ALLOCATIONS.getKey(), maxConcurrentJobAllocations)) - .get(); + client().admin() + .cluster() + .prepareUpdateSettings() + .setPersistentSettings(Settings.builder().put(MachineLearning.CONCURRENT_JOB_ALLOCATIONS.getKey(), maxConcurrentJobAllocations)) + .get(); // Sample each cs update and keep track each time a node holds more than `maxConcurrentJobAllocations` opening jobs. List violations = new CopyOnWriteArrayList<>(); @@ -304,13 +317,19 @@ public void testMaxConcurrentJobAllocations() throws Exception { for (DiscoveryNode node : event.state().nodes()) { Collection> foundTasks = tasks.findTasks(MlTasks.JOB_TASK_NAME, task -> { JobTaskState jobTaskState = (JobTaskState) task.getState(); - return node.getId().equals(task.getExecutorNode()) && - (jobTaskState == null || jobTaskState.isStatusStale(task)); + return node.getId().equals(task.getExecutorNode()) && (jobTaskState == null || jobTaskState.isStatusStale(task)); }); int count = foundTasks.size(); if (count > maxConcurrentJobAllocations) { - violations.add("Observed node [" + node.getName() + "] with [" + count + "] opening jobs on cluster state version [" + - event.state().version() + "]"); + violations.add( + "Observed node [" + + node.getName() + + "] with [" + + count + + "] opening jobs on cluster state version [" + + event.state().version() + + "]" + ); } } }); @@ -367,37 +386,51 @@ public void testMlStateAndResultsIndicesNotAvailable() throws Exception { internalCluster().ensureAtMostNumDataNodes(0); // start non ml node that will hold the state and results indices logger.info("Start non ml node:"); - String nonMLNode = internalCluster().startNode(Settings.builder() - .put("node.attr.ml-indices", "state-and-results") - .put(removeRoles(Set.of(DiscoveryNodeRole.ML_ROLE)))); + String nonMLNode = internalCluster().startNode( + Settings.builder().put("node.attr.ml-indices", "state-and-results").put(removeRoles(Set.of(DiscoveryNodeRole.ML_ROLE))) + ); ensureStableCluster(1); // start an ml node for the config index logger.info("Starting ml node"); - String mlNode = internalCluster().startNode(Settings.builder() + String mlNode = internalCluster().startNode( + Settings.builder() .put("node.attr.ml-indices", "config") - .put(addRoles(Set.of(DiscoveryNodeRole.DATA_ROLE, DiscoveryNodeRole.ML_ROLE)))); + .put(addRoles(Set.of(DiscoveryNodeRole.DATA_ROLE, DiscoveryNodeRole.ML_ROLE))) + ); ensureStableCluster(2); // Create the indices (using installed templates) and set the routing to specific nodes // State and results go on the state-and-results node, config goes on the config node - client().admin().indices().prepareCreate(".ml-anomalies-shared") - .setSettings(Settings.builder() - .put("index.routing.allocation.include.ml-indices", "state-and-results") - .put("index.routing.allocation.exclude.ml-indices", "config") - .build()) - .get(); - client().admin().indices().prepareCreate(".ml-state") - .setSettings(Settings.builder() - .put("index.routing.allocation.include.ml-indices", "state-and-results") - .put("index.routing.allocation.exclude.ml-indices", "config") - .build()) - .get(); - client().admin().indices().prepareCreate(".ml-config") - .setSettings(Settings.builder() - .put("index.routing.allocation.exclude.ml-indices", "state-and-results") - .put("index.routing.allocation.include.ml-indices", "config") - .build()) - .get(); + client().admin() + .indices() + .prepareCreate(".ml-anomalies-shared") + .setSettings( + Settings.builder() + .put("index.routing.allocation.include.ml-indices", "state-and-results") + .put("index.routing.allocation.exclude.ml-indices", "config") + .build() + ) + .get(); + client().admin() + .indices() + .prepareCreate(".ml-state") + .setSettings( + Settings.builder() + .put("index.routing.allocation.include.ml-indices", "state-and-results") + .put("index.routing.allocation.exclude.ml-indices", "config") + .build() + ) + .get(); + client().admin() + .indices() + .prepareCreate(".ml-config") + .setSettings( + Settings.builder() + .put("index.routing.allocation.exclude.ml-indices", "state-and-results") + .put("index.routing.allocation.include.ml-indices", "config") + .build() + ) + .get(); String jobId = "ml-indices-not-available-job"; Job.Builder job = createFareQuoteJob(jobId); @@ -408,10 +441,13 @@ public void testMlStateAndResultsIndicesNotAvailable() throws Exception { client().execute(OpenJobAction.INSTANCE, openJobRequest).actionGet(); PostDataAction.Request postDataRequest = new PostDataAction.Request(jobId); - postDataRequest.setContent(new BytesArray( - "{\"airline\":\"AAL\",\"responsetime\":\"132.2046\",\"sourcetype\":\"farequote\",\"time\":\"1403481600\"}\n" + - "{\"airline\":\"JZA\",\"responsetime\":\"990.4628\",\"sourcetype\":\"farequote\",\"time\":\"1403481700\"}" - ), XContentType.JSON); + postDataRequest.setContent( + new BytesArray( + "{\"airline\":\"AAL\",\"responsetime\":\"132.2046\",\"sourcetype\":\"farequote\",\"time\":\"1403481600\"}\n" + + "{\"airline\":\"JZA\",\"responsetime\":\"990.4628\",\"sourcetype\":\"farequote\",\"time\":\"1403481700\"}" + ), + XContentType.JSON + ); PostDataAction.Response response = client().execute(PostDataAction.INSTANCE, postDataRequest).actionGet(); assertEquals(2, response.getDataCounts().getProcessedRecordCount()); @@ -427,22 +463,26 @@ public void testMlStateAndResultsIndicesNotAvailable() throws Exception { internalCluster().stopRandomNode(InternalTestCluster.nameFilter(nonMLNode)); ensureStableCluster(1); - Exception e = expectThrows(ElasticsearchStatusException.class, - () -> client().execute(OpenJobAction.INSTANCE, openJobRequest).actionGet()); + Exception e = expectThrows( + ElasticsearchStatusException.class, + () -> client().execute(OpenJobAction.INSTANCE, openJobRequest).actionGet() + ); assertEquals("Could not open job because no ML nodes with sufficient capacity were found", e.getMessage()); IllegalStateException detail = (IllegalStateException) e.getCause(); assertNotNull(detail); String detailedMessage = detail.getMessage(); - assertTrue(detailedMessage, - detailedMessage.startsWith("Could not open job because no suitable nodes were found, allocation explanation")); + assertTrue( + detailedMessage, + detailedMessage.startsWith("Could not open job because no suitable nodes were found, allocation explanation") + ); assertThat(detailedMessage, containsString("because not all primary shards are active for the following indices")); assertThat(detailedMessage, containsString(".ml-state")); assertThat(detailedMessage, containsString(".ml-anomalies-shared")); logger.info("Start data node"); - String nonMlNode = internalCluster().startNode(Settings.builder() - .put(nonMLNodeDataPathSettings) - .put(removeRoles(Set.of(DiscoveryNodeRole.ML_ROLE)))); + String nonMlNode = internalCluster().startNode( + Settings.builder().put(nonMLNodeDataPathSettings).put(removeRoles(Set.of(DiscoveryNodeRole.ML_ROLE))) + ); ensureStableCluster(2, mlNode); ensureStableCluster(2, nonMlNode); ensureYellow(); // at least the primary shards of the indices a job uses should be started @@ -481,8 +521,8 @@ public void testCloseUnassignedLazyJobAndDatafeed() { // Datafeed state should be starting while it waits for job assignment GetDatafeedsStatsAction.Request datafeedStatsRequest = new GetDatafeedsStatsAction.Request(datafeedId); - GetDatafeedsStatsAction.Response datafeedStatsResponse = - client().execute(GetDatafeedsStatsAction.INSTANCE, datafeedStatsRequest).actionGet(); + GetDatafeedsStatsAction.Response datafeedStatsResponse = client().execute(GetDatafeedsStatsAction.INSTANCE, datafeedStatsRequest) + .actionGet(); assertEquals(DatafeedState.STARTING, datafeedStatsResponse.getResponse().results().get(0).getDatafeedState()); // A starting datafeed can be stopped normally or by force diff --git a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/BucketCorrelationAggregationIT.java b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/BucketCorrelationAggregationIT.java index 25b2b2c966a09..fc35c8491094e 100644 --- a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/BucketCorrelationAggregationIT.java +++ b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/BucketCorrelationAggregationIT.java @@ -23,8 +23,8 @@ import org.elasticsearch.search.aggregations.pipeline.MovingFunctions; import org.elasticsearch.xpack.ml.MlSingleNodeTestCase; import org.elasticsearch.xpack.ml.aggs.correlation.BucketCorrelationAggregationBuilder; -import org.elasticsearch.xpack.ml.aggs.correlation.CountCorrelationIndicator; import org.elasticsearch.xpack.ml.aggs.correlation.CountCorrelationFunction; +import org.elasticsearch.xpack.ml.aggs.correlation.CountCorrelationIndicator; import java.util.ArrayList; import java.util.List; @@ -41,9 +41,7 @@ public void testCountCorrelation() { int[] isCat = new int[10000]; int[] isDog = new int[10000]; - client().admin().indices().prepareCreate("data") - .setMapping("metric", "type=double", "term", "type=keyword") - .get(); + client().admin().indices().prepareCreate("data").setMapping("metric", "type=double", "term", "type=keyword").get(); BulkRequestBuilder bulkRequestBuilder = client().prepareBulk("data"); for (int i = 0; i < 5000; i++) { IndexRequest indexRequest = new IndexRequest("data"); @@ -72,14 +70,9 @@ public void testCountCorrelation() { double dogCorrelation = pearsonCorrelation(xs, isDog); AtomicLong counter = new AtomicLong(); - double[] steps = Stream.generate(() -> counter.getAndAdd(2L)).limit(50).mapToDouble(l -> (double)l).toArray(); + double[] steps = Stream.generate(() -> counter.getAndAdd(2L)).limit(50).mapToDouble(l -> (double) l).toArray(); SearchResponse percentilesSearch = client().prepareSearch("data") - .addAggregation( - AggregationBuilders - .percentiles("percentiles") - .field("metric") - .percentiles(steps) - ) + .addAggregation(AggregationBuilders.percentiles("percentiles").field("metric").percentiles(steps)) .setSize(0) .setTrackTotalHits(true) .get(); @@ -92,16 +85,10 @@ public void testCountCorrelation() { "metric" ); - SearchResponse countCorrelations = client() - .prepareSearch("data") + SearchResponse countCorrelations = client().prepareSearch("data") .setSize(0) .setTrackTotalHits(false) - .addAggregation(AggregationBuilders - .terms("buckets") - .field("term") - .subAggregation(aggs.v1()) - .subAggregation(aggs.v2()) - ) + .addAggregation(AggregationBuilders.terms("buckets").field("term").subAggregation(aggs.v1()).subAggregation(aggs.v2())) .get(); Terms terms = countCorrelations.getAggregations().get("buckets"); @@ -139,7 +126,7 @@ private static Tuple base64Chunks = chunkBinaryDefinition(compressedDefinition, compressedDefinition.length() / 3); - ChunkedTrainedModelPersister persister = new ChunkedTrainedModelPersister(trainedModelProvider, + ChunkedTrainedModelPersister persister = new ChunkedTrainedModelPersister( + trainedModelProvider, analyticsConfig, new DataFrameAnalyticsAuditor(client(), getInstanceFromNode(ClusterService.class)), (ex) -> { throw new ElasticsearchException(ex); }, new ExtractedFields(extractedFieldList, Collections.emptyList(), Collections.emptyMap()) ); - //Accuracy for size is not tested here + // Accuracy for size is not tested here ModelSizeInfo modelSizeInfo = ModelSizeInfoTests.createRandom(); persister.createAndIndexInferenceModelConfig(modelSizeInfo, configBuilder.getModelType()); for (int i = 0; i < base64Chunks.size(); i++) { persister.createAndIndexInferenceModelDoc( - new TrainedModelDefinitionChunk(base64Chunks.get(i), i, i == (base64Chunks.size() - 1))); + new TrainedModelDefinitionChunk(base64Chunks.get(i), i, i == (base64Chunks.size() - 1)) + ); } - ModelMetadata modelMetadata = new ModelMetadata(Stream.generate(TotalFeatureImportanceTests::randomInstance) - .limit(randomIntBetween(1, 10)) - .collect(Collectors.toList()), + ModelMetadata modelMetadata = new ModelMetadata( + Stream.generate(TotalFeatureImportanceTests::randomInstance).limit(randomIntBetween(1, 10)).collect(Collectors.toList()), FeatureImportanceBaselineTests.randomInstance(), - Stream.generate(HyperparametersTests::randomInstance) - .limit(randomIntBetween(1, 10)) - .collect(Collectors.toList())); + Stream.generate(HyperparametersTests::randomInstance).limit(randomIntBetween(1, 10)).collect(Collectors.toList()) + ); persister.createAndIndexInferenceModelMetadata(modelMetadata); PlainActionFuture>>> getIdsFuture = new PlainActionFuture<>(); @@ -122,7 +121,7 @@ public void testStoreModelViaChunkedPersister() throws IOException { TrainedModelConfig storedConfig = getTrainedModelFuture.actionGet(); assertThat(storedConfig.getCompressedDefinition(), equalTo(compressedDefinition)); - assertThat(storedConfig.getEstimatedOperations(), equalTo((long)modelSizeInfo.numOperations())); + assertThat(storedConfig.getEstimatedOperations(), equalTo((long) modelSizeInfo.numOperations())); assertThat(storedConfig.getEstimatedHeapMemory(), equalTo(modelSizeInfo.ramBytesUsed())); assertThat(storedConfig.getMetadata(), hasKey("total_feature_importance")); assertThat(storedConfig.getMetadata(), hasKey("feature_importance_baseline")); @@ -157,8 +156,8 @@ public static List chunkBinaryDefinition(BytesReference bytes, int chunk List subStrings = new ArrayList<>((bytes.length() + chunkSize - 1) / chunkSize); for (int i = 0; i < bytes.length(); i += chunkSize) { subStrings.add( - Base64.getEncoder().encodeToString( - Arrays.copyOfRange(bytes.array(), i, Math.min(i + chunkSize, bytes.length())))); + Base64.getEncoder().encodeToString(Arrays.copyOfRange(bytes.array(), i, Math.min(i + chunkSize, bytes.length()))) + ); } return subStrings; } diff --git a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/ChunkedTrainedModelRestorerIT.java b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/ChunkedTrainedModelRestorerIT.java index 80f2481a80959..a47efbcff65f3 100644 --- a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/ChunkedTrainedModelRestorerIT.java +++ b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/ChunkedTrainedModelRestorerIT.java @@ -42,7 +42,7 @@ public void testRestoreWithMultipleSearches() throws IOException, InterruptedExc int numDocs = 22; List modelDefs = new ArrayList<>(numDocs); - for (int i=0; i expectedDocs = createModelDefinitionDocs(modelDefs, modelId); putModelDefinitions(expectedDocs, InferenceIndexConstants.LATEST_INDEX_NAME, 0); - - ChunkedTrainedModelRestorer restorer = new ChunkedTrainedModelRestorer(modelId, client(), - client().threadPool().executor(MachineLearning.UTILITY_THREAD_POOL_NAME), xContentRegistry()); + ChunkedTrainedModelRestorer restorer = new ChunkedTrainedModelRestorer( + modelId, + client(), + client().threadPool().executor(MachineLearning.UTILITY_THREAD_POOL_NAME), + xContentRegistry() + ); restorer.setSearchSize(5); List actualDocs = new ArrayList<>(); AtomicReference exceptionHolder = new AtomicReference<>(); CountDownLatch latch = new CountDownLatch(1); - restorer.restoreModelDefinition( - actualDocs::add, - success -> latch.countDown(), - failure -> { - exceptionHolder.set(failure); - latch.countDown(); - }); + restorer.restoreModelDefinition(actualDocs::add, success -> latch.countDown(), failure -> { + exceptionHolder.set(failure); + latch.countDown(); + }); latch.await(); @@ -78,7 +78,7 @@ public void testCancel() throws IOException, InterruptedException { int numDocs = 6; List modelDefs = new ArrayList<>(numDocs); - for (int i=0; i expectedDocs = createModelDefinitionDocs(modelDefs, modelId); putModelDefinitions(expectedDocs, InferenceIndexConstants.LATEST_INDEX_NAME, 0); - ChunkedTrainedModelRestorer restorer = new ChunkedTrainedModelRestorer(modelId, client(), - client().threadPool().executor(MachineLearning.UTILITY_THREAD_POOL_NAME), xContentRegistry()); + ChunkedTrainedModelRestorer restorer = new ChunkedTrainedModelRestorer( + modelId, + client(), + client().threadPool().executor(MachineLearning.UTILITY_THREAD_POOL_NAME), + xContentRegistry() + ); restorer.setSearchSize(5); List actualDocs = new ArrayList<>(); @@ -95,19 +99,16 @@ public void testCancel() throws IOException, InterruptedException { AtomicBoolean successValue = new AtomicBoolean(Boolean.TRUE); CountDownLatch latch = new CountDownLatch(1); - restorer.restoreModelDefinition( - doc -> { - actualDocs.add(doc); - return false; - }, - success -> { - successValue.set(success); - latch.countDown(); - }, - failure -> { - exceptionHolder.set(failure); - latch.countDown(); - }); + restorer.restoreModelDefinition(doc -> { + actualDocs.add(doc); + return false; + }, success -> { + successValue.set(success); + latch.countDown(); + }, failure -> { + exceptionHolder.set(failure); + latch.countDown(); + }); latch.await(); @@ -121,29 +122,41 @@ public void testRestoreWithDocumentsInMultipleIndices() throws IOException, Inte String index1 = "foo-1"; String index2 = "foo-2"; - for (String index : new String[]{index1, index2}) { - client().admin().indices().prepareCreate(index) - .setMapping(TrainedModelDefinitionDoc.DEFINITION.getPreferredName(), "type=binary", - InferenceIndexConstants.DOC_TYPE.getPreferredName(), "type=keyword", - TrainedModelConfig.MODEL_ID.getPreferredName(), "type=keyword").get(); + for (String index : new String[] { index1, index2 }) { + client().admin() + .indices() + .prepareCreate(index) + .setMapping( + TrainedModelDefinitionDoc.DEFINITION.getPreferredName(), + "type=binary", + InferenceIndexConstants.DOC_TYPE.getPreferredName(), + "type=keyword", + TrainedModelConfig.MODEL_ID.getPreferredName(), + "type=keyword" + ) + .get(); } String modelId = "test-multiple-indices"; int numDocs = 24; List modelDefs = new ArrayList<>(numDocs); - for (int i=0; i expectedDocs = createModelDefinitionDocs(modelDefs, modelId); - int splitPoint = (numDocs / 2) -1; + int splitPoint = (numDocs / 2) - 1; putModelDefinitions(expectedDocs.subList(0, splitPoint), index1, 0); putModelDefinitions(expectedDocs.subList(splitPoint, numDocs), index2, splitPoint); - ChunkedTrainedModelRestorer restorer = new ChunkedTrainedModelRestorer(modelId, client(), - client().threadPool().executor(MachineLearning.UTILITY_THREAD_POOL_NAME), xContentRegistry()); + ChunkedTrainedModelRestorer restorer = new ChunkedTrainedModelRestorer( + modelId, + client(), + client().threadPool().executor(MachineLearning.UTILITY_THREAD_POOL_NAME), + xContentRegistry() + ); restorer.setSearchSize(10); restorer.setSearchIndex("foo-*"); @@ -151,13 +164,10 @@ public void testRestoreWithDocumentsInMultipleIndices() throws IOException, Inte CountDownLatch latch = new CountDownLatch(1); List actualDocs = new ArrayList<>(); - restorer.restoreModelDefinition( - actualDocs::add, - success -> latch.countDown(), - failure -> { - exceptionHolder.set(failure); - latch.countDown(); - }); + restorer.restoreModelDefinition(actualDocs::add, success -> latch.countDown(), failure -> { + exceptionHolder.set(failure); + latch.countDown(); + }); latch.await(); @@ -175,15 +185,16 @@ private List createModelDefinitionDocs(List docs = new ArrayList<>(); for (int i = 0; i < compressedDefinitions.size(); i++) { - docs.add(new TrainedModelDefinitionDoc.Builder() - .setDocNum(i) - .setBinaryData(compressedDefinitions.get(i)) - .setCompressionVersion(TrainedModelConfig.CURRENT_DEFINITION_COMPRESSION_VERSION) - .setTotalDefinitionLength(totalLength) - .setDefinitionLength(compressedDefinitions.get(i).length()) - .setEos(i == compressedDefinitions.size() - 1) - .setModelId(modelId) - .build()); + docs.add( + new TrainedModelDefinitionDoc.Builder().setDocNum(i) + .setBinaryData(compressedDefinitions.get(i)) + .setCompressionVersion(TrainedModelConfig.CURRENT_DEFINITION_COMPRESSION_VERSION) + .setTotalDefinitionLength(totalLength) + .setDefinitionLength(compressedDefinitions.get(i).length()) + .setEos(i == compressedDefinitions.size() - 1) + .setModelId(modelId) + .build() + ); } return docs; @@ -201,9 +212,7 @@ private void putModelDefinitions(List docs, String in } } - BulkResponse bulkResponse = bulkRequestBuilder - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .get(); + BulkResponse bulkResponse = bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).get(); if (bulkResponse.hasFailures()) { int failures = 0; for (BulkItemResponse itemResponse : bulkResponse) { diff --git a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/DataFrameAnalyticsCRUDIT.java b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/DataFrameAnalyticsCRUDIT.java index abd11ad6880d5..f5033c424619e 100644 --- a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/DataFrameAnalyticsCRUDIT.java +++ b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/DataFrameAnalyticsCRUDIT.java @@ -9,9 +9,9 @@ import org.elasticsearch.ResourceNotFoundException; import org.elasticsearch.client.OriginSettingClient; import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.query.QueryBuilders; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.ClientHelper; import org.elasticsearch.xpack.core.ml.action.DeleteDataFrameAnalyticsAction; import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsConfig; @@ -37,8 +37,11 @@ public class DataFrameAnalyticsCRUDIT extends MlSingleNodeTestCase { @Before public void createComponents() throws Exception { - configProvider = new DataFrameAnalyticsConfigProvider(client(), xContentRegistry(), - new DataFrameAnalyticsAuditor(client(), getInstanceFromNode(ClusterService.class))); + configProvider = new DataFrameAnalyticsConfigProvider( + client(), + xContentRegistry(), + new DataFrameAnalyticsAuditor(client(), getInstanceFromNode(ClusterService.class)) + ); waitForMlTemplates(); } @@ -62,8 +65,11 @@ public void testDeleteConfigWithStateAndStats() throws InterruptedException { AtomicReference configHolder = new AtomicReference<>(); AtomicReference exceptionHolder = new AtomicReference<>(); - blockingCall(actionListener -> configProvider.put(config, emptyMap(), TimeValue.timeValueSeconds(5), actionListener), - configHolder, exceptionHolder); + blockingCall( + actionListener -> configProvider.put(config, emptyMap(), TimeValue.timeValueSeconds(5), actionListener), + configHolder, + exceptionHolder + ); assertThat(configHolder.get(), is(notNullValue())); assertThat(configHolder.get(), is(equalTo(config))); @@ -89,25 +95,31 @@ public void testDeleteConfigWithStateAndStats() throws InterruptedException { client().execute(DeleteDataFrameAnalyticsAction.INSTANCE, new DeleteDataFrameAnalyticsAction.Request(configId)).actionGet(); - assertThat(originSettingClient.prepareSearch(".ml-state-*") - .setQuery(QueryBuilders.idsQuery() - .addIds("delete-config-with-state-and-stats_regression_state#1", - "data_frame_analytics-delete-config-with-state-and-stats-progress")) - .setTrackTotalHits(true) - .get() - .getHits() - .getTotalHits() - .value, equalTo(0L)); - - assertThat(originSettingClient.prepareSearch(".ml-stats-*") - .setQuery(QueryBuilders.idsQuery() - .addIds("delete-config-with-state-and-stats_1", - "delete-config-with-state-and-stats_2")) - .setTrackTotalHits(true) - .get() - .getHits() - .getTotalHits() - .value, equalTo(0L)); + assertThat( + originSettingClient.prepareSearch(".ml-state-*") + .setQuery( + QueryBuilders.idsQuery() + .addIds( + "delete-config-with-state-and-stats_regression_state#1", + "data_frame_analytics-delete-config-with-state-and-stats-progress" + ) + ) + .setTrackTotalHits(true) + .get() + .getHits() + .getTotalHits().value, + equalTo(0L) + ); + + assertThat( + originSettingClient.prepareSearch(".ml-stats-*") + .setQuery(QueryBuilders.idsQuery().addIds("delete-config-with-state-and-stats_1", "delete-config-with-state-and-stats_2")) + .setTrackTotalHits(true) + .get() + .getHits() + .getTotalHits().value, + equalTo(0L) + ); } } diff --git a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/DataFrameAnalyticsConfigProviderIT.java b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/DataFrameAnalyticsConfigProviderIT.java index ee25c86058e32..0248ec0a8df8a 100644 --- a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/DataFrameAnalyticsConfigProviderIT.java +++ b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/DataFrameAnalyticsConfigProviderIT.java @@ -49,8 +49,11 @@ public class DataFrameAnalyticsConfigProviderIT extends MlSingleNodeTestCase { @Before public void createComponents() throws Exception { - configProvider = new DataFrameAnalyticsConfigProvider(client(), xContentRegistry(), - new DataFrameAnalyticsAuditor(client(), getInstanceFromNode(ClusterService.class))); + configProvider = new DataFrameAnalyticsConfigProvider( + client(), + xContentRegistry(), + new DataFrameAnalyticsAuditor(client(), getInstanceFromNode(ClusterService.class)) + ); waitForMlTemplates(); } @@ -73,8 +76,7 @@ public void testPutAndGet() throws InterruptedException { AtomicReference configHolder = new AtomicReference<>(); AtomicReference exceptionHolder = new AtomicReference<>(); - blockingCall( - actionListener -> configProvider.put(config, emptyMap(), TIMEOUT, actionListener), configHolder, exceptionHolder); + blockingCall(actionListener -> configProvider.put(config, emptyMap(), TIMEOUT, actionListener), configHolder, exceptionHolder); assertThat(configHolder.get(), is(notNullValue())); assertThat(configHolder.get(), is(equalTo(config))); @@ -100,16 +102,14 @@ public void testPutAndGet_WithSecurityHeaders() throws InterruptedException { AtomicReference configHolder = new AtomicReference<>(); AtomicReference exceptionHolder = new AtomicReference<>(); - blockingCall(actionListener -> configProvider.put(config, securityHeaders, TIMEOUT, actionListener), - configHolder, exceptionHolder); + blockingCall( + actionListener -> configProvider.put(config, securityHeaders, TIMEOUT, actionListener), + configHolder, + exceptionHolder + ); assertThat(configHolder.get(), is(notNullValue())); - assertThat( - configHolder.get(), - is(equalTo( - new DataFrameAnalyticsConfig.Builder(config) - .setHeaders(securityHeaders) - .build()))); + assertThat(configHolder.get(), is(equalTo(new DataFrameAnalyticsConfig.Builder(config).setHeaders(securityHeaders).build()))); assertThat(exceptionHolder.get(), is(nullValue())); } { // Get the config back and verify the response @@ -119,12 +119,7 @@ public void testPutAndGet_WithSecurityHeaders() throws InterruptedException { blockingCall(actionListener -> configProvider.get(configId, actionListener), configHolder, exceptionHolder); assertThat(configHolder.get(), is(notNullValue())); - assertThat( - configHolder.get(), - is(equalTo( - new DataFrameAnalyticsConfig.Builder(config) - .setHeaders(securityHeaders) - .build()))); + assertThat(configHolder.get(), is(equalTo(new DataFrameAnalyticsConfig.Builder(config).setHeaders(securityHeaders).build()))); assertThat(exceptionHolder.get(), is(nullValue())); } } @@ -137,7 +132,10 @@ public void testPut_ConfigAlreadyExists() throws InterruptedException { DataFrameAnalyticsConfig initialConfig = DataFrameAnalyticsConfigTests.createRandom(configId); blockingCall( - actionListener -> configProvider.put(initialConfig, emptyMap(), TIMEOUT, actionListener), configHolder, exceptionHolder); + actionListener -> configProvider.put(initialConfig, emptyMap(), TIMEOUT, actionListener), + configHolder, + exceptionHolder + ); assertThat(configHolder.get(), is(notNullValue())); assertThat(configHolder.get(), is(equalTo(initialConfig))); @@ -151,7 +149,8 @@ public void testPut_ConfigAlreadyExists() throws InterruptedException { blockingCall( actionListener -> configProvider.put(configWithSameId, emptyMap(), TIMEOUT, actionListener), configHolder, - exceptionHolder); + exceptionHolder + ); assertThat(configHolder.get(), is(nullValue())); assertThat(exceptionHolder.get(), is(notNullValue())); @@ -167,7 +166,10 @@ public void testUpdate() throws Exception { AtomicReference exceptionHolder = new AtomicReference<>(); blockingCall( - actionListener -> configProvider.put(initialConfig, emptyMap(), TIMEOUT, actionListener), configHolder, exceptionHolder); + actionListener -> configProvider.put(initialConfig, emptyMap(), TIMEOUT, actionListener), + configHolder, + exceptionHolder + ); assertNoException(exceptionHolder); assertThat(configHolder.get(), is(notNullValue())); @@ -177,47 +179,48 @@ public void testUpdate() throws Exception { AtomicReference updatedConfigHolder = new AtomicReference<>(); AtomicReference exceptionHolder = new AtomicReference<>(); - DataFrameAnalyticsConfigUpdate configUpdate = - new DataFrameAnalyticsConfigUpdate.Builder(configId) - .setDescription("description-1") - .build(); + DataFrameAnalyticsConfigUpdate configUpdate = new DataFrameAnalyticsConfigUpdate.Builder(configId).setDescription( + "description-1" + ).build(); blockingCall( actionListener -> configProvider.update(configUpdate, emptyMap(), ClusterState.EMPTY_STATE, actionListener), updatedConfigHolder, - exceptionHolder); + exceptionHolder + ); assertNoException(exceptionHolder); assertThat(updatedConfigHolder.get(), is(notNullValue())); assertThat( updatedConfigHolder.get(), - is(equalTo( - new DataFrameAnalyticsConfig.Builder(initialConfig) - .setDescription("description-1") - .build()))); + is(equalTo(new DataFrameAnalyticsConfig.Builder(initialConfig).setDescription("description-1").build())) + ); } { // Update that changes model memory limit AtomicReference updatedConfigHolder = new AtomicReference<>(); AtomicReference exceptionHolder = new AtomicReference<>(); - DataFrameAnalyticsConfigUpdate configUpdate = - new DataFrameAnalyticsConfigUpdate.Builder(configId) - .setModelMemoryLimit(ByteSizeValue.ofBytes(1024)) - .build(); + DataFrameAnalyticsConfigUpdate configUpdate = new DataFrameAnalyticsConfigUpdate.Builder(configId).setModelMemoryLimit( + ByteSizeValue.ofBytes(1024) + ).build(); blockingCall( actionListener -> configProvider.update(configUpdate, emptyMap(), ClusterState.EMPTY_STATE, actionListener), updatedConfigHolder, - exceptionHolder); + exceptionHolder + ); assertNoException(exceptionHolder); assertThat(updatedConfigHolder.get(), is(notNullValue())); assertThat( updatedConfigHolder.get(), - is(equalTo( - new DataFrameAnalyticsConfig.Builder(initialConfig) - .setDescription("description-1") - .setModelMemoryLimit(ByteSizeValue.ofBytes(1024)) - .build()))); + is( + equalTo( + new DataFrameAnalyticsConfig.Builder(initialConfig).setDescription("description-1") + .setModelMemoryLimit(ByteSizeValue.ofBytes(1024)) + .build() + ) + ) + ); } { // Noop update AtomicReference updatedConfigHolder = new AtomicReference<>(); @@ -228,42 +231,48 @@ public void testUpdate() throws Exception { blockingCall( actionListener -> configProvider.update(configUpdate, emptyMap(), ClusterState.EMPTY_STATE, actionListener), updatedConfigHolder, - exceptionHolder); + exceptionHolder + ); assertNoException(exceptionHolder); assertThat(updatedConfigHolder.get(), is(notNullValue())); assertThat( updatedConfigHolder.get(), - is(equalTo( - new DataFrameAnalyticsConfig.Builder(initialConfig) - .setDescription("description-1") - .setModelMemoryLimit(ByteSizeValue.ofBytes(1024)) - .build()))); + is( + equalTo( + new DataFrameAnalyticsConfig.Builder(initialConfig).setDescription("description-1") + .setModelMemoryLimit(ByteSizeValue.ofBytes(1024)) + .build() + ) + ) + ); } { // Update that changes both description and model memory limit AtomicReference updatedConfigHolder = new AtomicReference<>(); AtomicReference exceptionHolder = new AtomicReference<>(); - DataFrameAnalyticsConfigUpdate configUpdate = - new DataFrameAnalyticsConfigUpdate.Builder(configId) - .setDescription("description-2") - .setModelMemoryLimit(ByteSizeValue.ofBytes(2048)) - .build(); + DataFrameAnalyticsConfigUpdate configUpdate = new DataFrameAnalyticsConfigUpdate.Builder(configId).setDescription( + "description-2" + ).setModelMemoryLimit(ByteSizeValue.ofBytes(2048)).build(); blockingCall( actionListener -> configProvider.update(configUpdate, emptyMap(), ClusterState.EMPTY_STATE, actionListener), updatedConfigHolder, - exceptionHolder); + exceptionHolder + ); assertNoException(exceptionHolder); assertThat(updatedConfigHolder.get(), is(notNullValue())); assertThat( updatedConfigHolder.get(), - is(equalTo( - new DataFrameAnalyticsConfig.Builder(initialConfig) - .setDescription("description-2") - .setModelMemoryLimit(ByteSizeValue.ofBytes(2048)) - .build()))); + is( + equalTo( + new DataFrameAnalyticsConfig.Builder(initialConfig).setDescription("description-2") + .setModelMemoryLimit(ByteSizeValue.ofBytes(2048)) + .build() + ) + ) + ); } { // Update that applies security headers Map securityHeaders = Collections.singletonMap("_xpack_security_authentication", "dummy"); @@ -276,18 +285,22 @@ public void testUpdate() throws Exception { blockingCall( actionListener -> configProvider.update(configUpdate, securityHeaders, ClusterState.EMPTY_STATE, actionListener), updatedConfigHolder, - exceptionHolder); + exceptionHolder + ); assertNoException(exceptionHolder); assertThat(updatedConfigHolder.get(), is(notNullValue())); assertThat( updatedConfigHolder.get(), - is(equalTo( - new DataFrameAnalyticsConfig.Builder(initialConfig) - .setDescription("description-2") - .setModelMemoryLimit(ByteSizeValue.ofBytes(2048)) - .setHeaders(securityHeaders) - .build()))); + is( + equalTo( + new DataFrameAnalyticsConfig.Builder(initialConfig).setDescription("description-2") + .setModelMemoryLimit(ByteSizeValue.ofBytes(2048)) + .setHeaders(securityHeaders) + .build() + ) + ) + ); } } @@ -300,7 +313,8 @@ public void testUpdate_ConfigDoesNotExist() throws InterruptedException { blockingCall( actionListener -> configProvider.update(configUpdate, emptyMap(), ClusterState.EMPTY_STATE, actionListener), updatedConfigHolder, - exceptionHolder); + exceptionHolder + ); assertThat(updatedConfigHolder.get(), is(nullValue())); assertThat(exceptionHolder.get(), is(notNullValue())); @@ -316,7 +330,10 @@ public void testUpdate_UpdateCannotBeAppliedWhenTaskIsRunning() throws Interrupt AtomicReference exceptionHolder = new AtomicReference<>(); blockingCall( - actionListener -> configProvider.put(initialConfig, emptyMap(), TIMEOUT, actionListener), configHolder, exceptionHolder); + actionListener -> configProvider.put(initialConfig, emptyMap(), TIMEOUT, actionListener), + configHolder, + exceptionHolder + ); assertThat(configHolder.get(), is(notNullValue())); assertThat(configHolder.get(), is(equalTo(initialConfig))); @@ -326,16 +343,16 @@ public void testUpdate_UpdateCannotBeAppliedWhenTaskIsRunning() throws Interrupt AtomicReference updatedConfigHolder = new AtomicReference<>(); AtomicReference exceptionHolder = new AtomicReference<>(); - DataFrameAnalyticsConfigUpdate configUpdate = - new DataFrameAnalyticsConfigUpdate.Builder(configId) - .setModelMemoryLimit(ByteSizeValue.ofMb(2048)) - .build(); + DataFrameAnalyticsConfigUpdate configUpdate = new DataFrameAnalyticsConfigUpdate.Builder(configId).setModelMemoryLimit( + ByteSizeValue.ofMb(2048) + ).build(); ClusterState clusterState = clusterStateWithRunningAnalyticsTask(configId, DataFrameAnalyticsState.ANALYZING); blockingCall( actionListener -> configProvider.update(configUpdate, emptyMap(), clusterState, actionListener), updatedConfigHolder, - exceptionHolder); + exceptionHolder + ); assertThat(updatedConfigHolder.get(), is(nullValue())); assertThat(exceptionHolder.get(), is(notNullValue())); @@ -352,10 +369,12 @@ private static ClusterState clusterStateWithRunningAnalyticsTask(String analytic MlTasks.dataFrameAnalyticsTaskId(analyticsId), MlTasks.DATA_FRAME_ANALYTICS_TASK_NAME, new StartDataFrameAnalyticsAction.TaskParams(analyticsId, Version.CURRENT, false), - new PersistentTasksCustomMetadata.Assignment("node", "test assignment")); + new PersistentTasksCustomMetadata.Assignment("node", "test assignment") + ); builder.updateTaskState( MlTasks.dataFrameAnalyticsTaskId(analyticsId), - new DataFrameAnalyticsTaskState(analyticsState, builder.getLastAllocationId(), null)); + new DataFrameAnalyticsTaskState(analyticsState, builder.getLastAllocationId(), null) + ); PersistentTasksCustomMetadata tasks = builder.build(); return ClusterState.builder(new ClusterName("cluster")) diff --git a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/DatafeedConfigProviderIT.java b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/DatafeedConfigProviderIT.java index 72612cef46ba5..6e5c77e073657 100644 --- a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/DatafeedConfigProviderIT.java +++ b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/DatafeedConfigProviderIT.java @@ -68,15 +68,21 @@ public void testCrud() throws InterruptedException { // Create datafeed config DatafeedConfig.Builder config = createDatafeedConfig(datafeedId, "j1"); - blockingCall(actionListener -> datafeedConfigProvider.putDatafeedConfig(config.build(), createSecurityHeader(), actionListener), - indexResponseHolder, exceptionHolder); + blockingCall( + actionListener -> datafeedConfigProvider.putDatafeedConfig(config.build(), createSecurityHeader(), actionListener), + indexResponseHolder, + exceptionHolder + ); assertNull(exceptionHolder.get()); assertEquals(RestStatus.CREATED, indexResponseHolder.get().status()); // Read datafeed config AtomicReference configBuilderHolder = new AtomicReference<>(); - blockingCall(actionListener -> datafeedConfigProvider.getDatafeedConfig(datafeedId, actionListener), - configBuilderHolder, exceptionHolder); + blockingCall( + actionListener -> datafeedConfigProvider.getDatafeedConfig(datafeedId, actionListener), + configBuilderHolder, + exceptionHolder + ); assertNull(exceptionHolder.get()); // Headers are set by the putDatafeedConfig method so they @@ -94,26 +100,39 @@ public void testCrud() throws InterruptedException { updateHeaders.put(securityHeader, "CHANGED"); AtomicReference configHolder = new AtomicReference<>(); - blockingCall(actionListener -> - datafeedConfigProvider.updateDatefeedConfig(datafeedId, update.build(), updateHeaders, - (updatedConfig, listener) -> listener.onResponse(Boolean.TRUE), actionListener), - configHolder, exceptionHolder); + blockingCall( + actionListener -> datafeedConfigProvider.updateDatefeedConfig( + datafeedId, + update.build(), + updateHeaders, + (updatedConfig, listener) -> listener.onResponse(Boolean.TRUE), + actionListener + ), + configHolder, + exceptionHolder + ); assertNull(exceptionHolder.get()); assertThat(configHolder.get().getIndices(), equalTo(updateIndices)); assertThat(configHolder.get().getHeaders().get(securityHeader), equalTo("CHANGED")); // Read the updated config configBuilderHolder.set(null); - blockingCall(actionListener -> datafeedConfigProvider.getDatafeedConfig(datafeedId, actionListener), - configBuilderHolder, exceptionHolder); + blockingCall( + actionListener -> datafeedConfigProvider.getDatafeedConfig(datafeedId, actionListener), + configBuilderHolder, + exceptionHolder + ); assertNull(exceptionHolder.get()); assertThat(configBuilderHolder.get().build().getIndices(), equalTo(updateIndices)); assertThat(configBuilderHolder.get().build().getHeaders().get(securityHeader), equalTo("CHANGED")); // Delete AtomicReference deleteResponseHolder = new AtomicReference<>(); - blockingCall(actionListener -> datafeedConfigProvider.deleteDatafeedConfig(datafeedId, actionListener), - deleteResponseHolder, exceptionHolder); + blockingCall( + actionListener -> datafeedConfigProvider.deleteDatafeedConfig(datafeedId, actionListener), + deleteResponseHolder, + exceptionHolder + ); assertNull(exceptionHolder.get()); assertEquals(DocWriteResponse.Result.DELETED, deleteResponseHolder.get().getResult()); } @@ -121,8 +140,11 @@ public void testCrud() throws InterruptedException { public void testGetDatafeedConfig_missing() throws InterruptedException { AtomicReference exceptionHolder = new AtomicReference<>(); AtomicReference configBuilderHolder = new AtomicReference<>(); - blockingCall(actionListener -> datafeedConfigProvider.getDatafeedConfig("missing", actionListener), - configBuilderHolder, exceptionHolder); + blockingCall( + actionListener -> datafeedConfigProvider.getDatafeedConfig("missing", actionListener), + configBuilderHolder, + exceptionHolder + ); assertNull(configBuilderHolder.get()); assertEquals(ResourceNotFoundException.class, exceptionHolder.get().getClass()); } @@ -135,15 +157,21 @@ public void testMultipleCreateAndDeletes() throws InterruptedException { // Create datafeed config DatafeedConfig.Builder config = createDatafeedConfig(datafeedId, "j1"); - blockingCall(actionListener -> datafeedConfigProvider.putDatafeedConfig(config.build(), Collections.emptyMap(), actionListener), - indexResponseHolder, exceptionHolder); + blockingCall( + actionListener -> datafeedConfigProvider.putDatafeedConfig(config.build(), Collections.emptyMap(), actionListener), + indexResponseHolder, + exceptionHolder + ); assertNull(exceptionHolder.get()); assertEquals(RestStatus.CREATED, indexResponseHolder.get().status()); // cannot create another with the same id indexResponseHolder.set(null); - blockingCall(actionListener -> datafeedConfigProvider.putDatafeedConfig(config.build(), Collections.emptyMap(), actionListener), - indexResponseHolder, exceptionHolder); + blockingCall( + actionListener -> datafeedConfigProvider.putDatafeedConfig(config.build(), Collections.emptyMap(), actionListener), + indexResponseHolder, + exceptionHolder + ); assertNull(indexResponseHolder.get()); assertThat(exceptionHolder.get(), instanceOf(ResourceAlreadyExistsException.class)); assertEquals("A datafeed with id [df2] already exists", exceptionHolder.get().getMessage()); @@ -151,15 +179,21 @@ public void testMultipleCreateAndDeletes() throws InterruptedException { // delete exceptionHolder.set(null); AtomicReference deleteResponseHolder = new AtomicReference<>(); - blockingCall(actionListener -> datafeedConfigProvider.deleteDatafeedConfig(datafeedId, actionListener), - deleteResponseHolder, exceptionHolder); + blockingCall( + actionListener -> datafeedConfigProvider.deleteDatafeedConfig(datafeedId, actionListener), + deleteResponseHolder, + exceptionHolder + ); assertNull(exceptionHolder.get()); assertEquals(DocWriteResponse.Result.DELETED, deleteResponseHolder.get().getResult()); // error deleting twice deleteResponseHolder.set(null); - blockingCall(actionListener -> datafeedConfigProvider.deleteDatafeedConfig(datafeedId, actionListener), - deleteResponseHolder, exceptionHolder); + blockingCall( + actionListener -> datafeedConfigProvider.deleteDatafeedConfig(datafeedId, actionListener), + deleteResponseHolder, + exceptionHolder + ); assertNull(deleteResponseHolder.get()); assertEquals(ResourceNotFoundException.class, exceptionHolder.get().getClass()); } @@ -175,10 +209,17 @@ public void testUpdateWhenApplyingTheUpdateThrows() throws Exception { AtomicReference exceptionHolder = new AtomicReference<>(); AtomicReference configHolder = new AtomicReference<>(); - blockingCall(actionListener -> - datafeedConfigProvider.updateDatefeedConfig(datafeedId, update.build(), Collections.emptyMap(), - (updatedConfig, listener) -> listener.onResponse(Boolean.TRUE), actionListener), - configHolder, exceptionHolder); + blockingCall( + actionListener -> datafeedConfigProvider.updateDatefeedConfig( + datafeedId, + update.build(), + Collections.emptyMap(), + (updatedConfig, listener) -> listener.onResponse(Boolean.TRUE), + actionListener + ), + configHolder, + exceptionHolder + ); assertNull(configHolder.get()); assertNotNull(exceptionHolder.get()); assertThat(exceptionHolder.get(), IsInstanceOf.instanceOf(IllegalArgumentException.class)); @@ -201,10 +242,17 @@ public void testUpdateWithValidatorFunctionThatErrors() throws Exception { AtomicReference configHolder = new AtomicReference<>(); AtomicReference exceptionHolder = new AtomicReference<>(); - blockingCall(actionListener -> - datafeedConfigProvider.updateDatefeedConfig(datafeedId, update.build(), Collections.emptyMap(), - validateErrorFunction, actionListener), - configHolder, exceptionHolder); + blockingCall( + actionListener -> datafeedConfigProvider.updateDatefeedConfig( + datafeedId, + update.build(), + Collections.emptyMap(), + validateErrorFunction, + actionListener + ), + configHolder, + exceptionHolder + ); assertNull(configHolder.get()); assertThat(exceptionHolder.get(), IsInstanceOf.instanceOf(IllegalArgumentException.class)); @@ -216,8 +264,11 @@ public void testAllowNoMatch() throws InterruptedException { AtomicReference> datafeedIdsHolder = new AtomicReference<>(); AtomicReference exceptionHolder = new AtomicReference<>(); - blockingCall(actionListener -> datafeedConfigProvider.expandDatafeedIds("_all", false, null, false, actionListener), - datafeedIdsHolder, exceptionHolder); + blockingCall( + actionListener -> datafeedConfigProvider.expandDatafeedIds("_all", false, null, false, actionListener), + datafeedIdsHolder, + exceptionHolder + ); assertNull(datafeedIdsHolder.get()); assertNotNull(exceptionHolder.get()); @@ -225,14 +276,20 @@ public void testAllowNoMatch() throws InterruptedException { assertThat(exceptionHolder.get().getMessage(), containsString("No datafeed with id [*] exists")); exceptionHolder.set(null); - blockingCall(actionListener -> datafeedConfigProvider.expandDatafeedIds("_all", true, null, false,actionListener), - datafeedIdsHolder, exceptionHolder); + blockingCall( + actionListener -> datafeedConfigProvider.expandDatafeedIds("_all", true, null, false, actionListener), + datafeedIdsHolder, + exceptionHolder + ); assertNotNull(datafeedIdsHolder.get()); assertNull(exceptionHolder.get()); AtomicReference> datafeedsHolder = new AtomicReference<>(); - blockingCall(actionListener -> datafeedConfigProvider.expandDatafeedConfigs("*", false, actionListener), - datafeedsHolder, exceptionHolder); + blockingCall( + actionListener -> datafeedConfigProvider.expandDatafeedConfigs("*", false, actionListener), + datafeedsHolder, + exceptionHolder + ); assertNull(datafeedsHolder.get()); assertNotNull(exceptionHolder.get()); @@ -240,8 +297,11 @@ public void testAllowNoMatch() throws InterruptedException { assertThat(exceptionHolder.get().getMessage(), containsString("No datafeed with id [*] exists")); exceptionHolder.set(null); - blockingCall(actionListener -> datafeedConfigProvider.expandDatafeedConfigs("*", true, actionListener), - datafeedsHolder, exceptionHolder); + blockingCall( + actionListener -> datafeedConfigProvider.expandDatafeedConfigs("*", true, actionListener), + datafeedsHolder, + exceptionHolder + ); assertNotNull(datafeedsHolder.get()); assertNull(exceptionHolder.get()); } @@ -256,50 +316,55 @@ public void testExpandDatafeeds() throws Exception { client().admin().indices().prepareRefresh(MlConfigIndex.indexName()).get(); // Test datafeed IDs only - SortedSet expandedIds = - blockingCall(actionListener -> datafeedConfigProvider.expandDatafeedIds("foo*", true, null, false, actionListener)); + SortedSet expandedIds = blockingCall( + actionListener -> datafeedConfigProvider.expandDatafeedIds("foo*", true, null, false, actionListener) + ); assertEquals(new TreeSet<>(Arrays.asList("foo-1", "foo-2")), expandedIds); - expandedIds = blockingCall(actionListener -> datafeedConfigProvider.expandDatafeedIds("*-1", true,null, false, actionListener)); + expandedIds = blockingCall(actionListener -> datafeedConfigProvider.expandDatafeedIds("*-1", true, null, false, actionListener)); assertEquals(new TreeSet<>(Arrays.asList("bar-1", "foo-1")), expandedIds); - expandedIds = blockingCall(actionListener -> datafeedConfigProvider.expandDatafeedIds("bar*", true, null, false, actionListener)); + expandedIds = blockingCall(actionListener -> datafeedConfigProvider.expandDatafeedIds("bar*", true, null, false, actionListener)); assertEquals(new TreeSet<>(Arrays.asList("bar-1", "bar-2")), expandedIds); - expandedIds = blockingCall(actionListener -> datafeedConfigProvider.expandDatafeedIds("b*r-1", true, null, false, actionListener)); + expandedIds = blockingCall(actionListener -> datafeedConfigProvider.expandDatafeedIds("b*r-1", true, null, false, actionListener)); assertEquals(new TreeSet<>(Collections.singletonList("bar-1")), expandedIds); - expandedIds = blockingCall(actionListener -> datafeedConfigProvider.expandDatafeedIds("bar-1,foo*", - true, - null, - false, - actionListener)); + expandedIds = blockingCall( + actionListener -> datafeedConfigProvider.expandDatafeedIds("bar-1,foo*", true, null, false, actionListener) + ); assertEquals(new TreeSet<>(Arrays.asList("bar-1", "foo-1", "foo-2")), expandedIds); // Test full datafeed config - List expandedDatafeedBuilders = - blockingCall(actionListener -> datafeedConfigProvider.expandDatafeedConfigs("foo*", true, actionListener)); - List expandedDatafeeds = - expandedDatafeedBuilders.stream().map(DatafeedConfig.Builder::build).collect(Collectors.toList()); + List expandedDatafeedBuilders = blockingCall( + actionListener -> datafeedConfigProvider.expandDatafeedConfigs("foo*", true, actionListener) + ); + List expandedDatafeeds = expandedDatafeedBuilders.stream() + .map(DatafeedConfig.Builder::build) + .collect(Collectors.toList()); assertThat(expandedDatafeeds, containsInAnyOrder(foo1, foo2)); - expandedDatafeedBuilders = - blockingCall(actionListener -> datafeedConfigProvider.expandDatafeedConfigs("*-1", true, actionListener)); + expandedDatafeedBuilders = blockingCall( + actionListener -> datafeedConfigProvider.expandDatafeedConfigs("*-1", true, actionListener) + ); expandedDatafeeds = expandedDatafeedBuilders.stream().map(DatafeedConfig.Builder::build).collect(Collectors.toList()); assertThat(expandedDatafeeds, containsInAnyOrder(foo1, bar1)); - expandedDatafeedBuilders = - blockingCall(actionListener -> datafeedConfigProvider.expandDatafeedConfigs("bar*", true, actionListener)); + expandedDatafeedBuilders = blockingCall( + actionListener -> datafeedConfigProvider.expandDatafeedConfigs("bar*", true, actionListener) + ); expandedDatafeeds = expandedDatafeedBuilders.stream().map(DatafeedConfig.Builder::build).collect(Collectors.toList()); assertThat(expandedDatafeeds, containsInAnyOrder(bar1, bar2)); - expandedDatafeedBuilders = - blockingCall(actionListener -> datafeedConfigProvider.expandDatafeedConfigs("b*r-1", true, actionListener)); + expandedDatafeedBuilders = blockingCall( + actionListener -> datafeedConfigProvider.expandDatafeedConfigs("b*r-1", true, actionListener) + ); expandedDatafeeds = expandedDatafeedBuilders.stream().map(DatafeedConfig.Builder::build).collect(Collectors.toList()); assertThat(expandedDatafeeds, containsInAnyOrder(bar1)); - expandedDatafeedBuilders = - blockingCall(actionListener -> datafeedConfigProvider.expandDatafeedConfigs("bar-1,foo*", true, actionListener)); + expandedDatafeedBuilders = blockingCall( + actionListener -> datafeedConfigProvider.expandDatafeedConfigs("bar-1,foo*", true, actionListener) + ); expandedDatafeeds = expandedDatafeedBuilders.stream().map(DatafeedConfig.Builder::build).collect(Collectors.toList()); assertThat(expandedDatafeeds, containsInAnyOrder(bar1, foo1, foo2)); } @@ -309,21 +374,27 @@ public void testExpandDatafeedsWithTaskData() throws Exception { client().admin().indices().prepareRefresh(MlConfigIndex.indexName()).get(); PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); - tasksBuilder.addTask(MlTasks.datafeedTaskId("foo-1"), - MlTasks.DATAFEED_TASK_NAME, new StartDatafeedAction.DatafeedParams("foo-1", 0L), - new PersistentTasksCustomMetadata.Assignment("node-1", "test assignment")); + tasksBuilder.addTask( + MlTasks.datafeedTaskId("foo-1"), + MlTasks.DATAFEED_TASK_NAME, + new StartDatafeedAction.DatafeedParams("foo-1", 0L), + new PersistentTasksCustomMetadata.Assignment("node-1", "test assignment") + ); PersistentTasksCustomMetadata tasks = tasksBuilder.build(); AtomicReference exceptionHolder = new AtomicReference<>(); AtomicReference> datafeedIdsHolder = new AtomicReference<>(); // Test datafeed IDs only - SortedSet expandedIds = - blockingCall(actionListener -> datafeedConfigProvider.expandDatafeedIds("foo*", false, tasks, true, actionListener)); + SortedSet expandedIds = blockingCall( + actionListener -> datafeedConfigProvider.expandDatafeedIds("foo*", false, tasks, true, actionListener) + ); assertEquals(new TreeSet<>(Arrays.asList("foo-1", "foo-2")), expandedIds); - blockingCall(actionListener -> datafeedConfigProvider.expandDatafeedIds("foo-1*,foo-2*", false, tasks, false, actionListener), + blockingCall( + actionListener -> datafeedConfigProvider.expandDatafeedIds("foo-1*,foo-2*", false, tasks, false, actionListener), datafeedIdsHolder, - exceptionHolder); + exceptionHolder + ); assertThat(exceptionHolder.get(), is(not(nullValue()))); assertEquals(ResourceNotFoundException.class, exceptionHolder.get().getClass()); assertThat(exceptionHolder.get().getMessage(), containsString("No datafeed with id [foo-1*] exists")); @@ -340,21 +411,24 @@ public void testFindDatafeedIdsForJobIds() throws Exception { AtomicReference exceptionHolder = new AtomicReference<>(); blockingCall( - actionListener -> datafeedConfigProvider.findDatafeedIdsForJobIds( - Collections.singletonList("new-job"), - actionListener - ), + actionListener -> datafeedConfigProvider.findDatafeedIdsForJobIds(Collections.singletonList("new-job"), actionListener), datafeedIdsHolder, exceptionHolder ); assertThat(datafeedIdsHolder.get(), empty()); - blockingCall(actionListener -> datafeedConfigProvider.findDatafeedIdsForJobIds(Collections.singletonList("j2"), actionListener), - datafeedIdsHolder, exceptionHolder); + blockingCall( + actionListener -> datafeedConfigProvider.findDatafeedIdsForJobIds(Collections.singletonList("j2"), actionListener), + datafeedIdsHolder, + exceptionHolder + ); assertThat(datafeedIdsHolder.get(), contains("foo-2")); - blockingCall(actionListener -> datafeedConfigProvider.findDatafeedIdsForJobIds(Arrays.asList("j3", "j1"), actionListener), - datafeedIdsHolder, exceptionHolder); + blockingCall( + actionListener -> datafeedConfigProvider.findDatafeedIdsForJobIds(Arrays.asList("j3", "j1"), actionListener), + datafeedIdsHolder, + exceptionHolder + ); assertThat(datafeedIdsHolder.get(), contains("bar-1", "foo-1")); } @@ -369,22 +443,25 @@ public void testFindDatafeedsForJobIds() throws Exception { AtomicReference exceptionHolder = new AtomicReference<>(); blockingCall( - actionListener -> datafeedConfigProvider.findDatafeedsByJobIds( - Collections.singletonList("new-job"), - actionListener - ), + actionListener -> datafeedConfigProvider.findDatafeedsByJobIds(Collections.singletonList("new-job"), actionListener), datafeedMapHolder, exceptionHolder ); assertThat(datafeedMapHolder.get(), anEmptyMap()); - blockingCall(actionListener -> datafeedConfigProvider.findDatafeedsByJobIds(Collections.singletonList("j2"), actionListener), - datafeedMapHolder, exceptionHolder); + blockingCall( + actionListener -> datafeedConfigProvider.findDatafeedsByJobIds(Collections.singletonList("j2"), actionListener), + datafeedMapHolder, + exceptionHolder + ); assertThat(datafeedMapHolder.get(), hasKey("j2")); assertThat(datafeedMapHolder.get().get("j2").getId(), equalTo("foo-2")); - blockingCall(actionListener -> datafeedConfigProvider.findDatafeedsByJobIds(Arrays.asList("j3", "j1"), actionListener), - datafeedMapHolder, exceptionHolder); + blockingCall( + actionListener -> datafeedConfigProvider.findDatafeedsByJobIds(Arrays.asList("j3", "j1"), actionListener), + datafeedMapHolder, + exceptionHolder + ); assertThat(datafeedMapHolder.get(), allOf(hasKey("j3"), hasKey("j1"))); assertThat(datafeedMapHolder.get().get("j3").getId(), equalTo("bar-1")); assertThat(datafeedMapHolder.get().get("j1").getId(), equalTo("foo-1")); @@ -401,8 +478,11 @@ public void testHeadersAreOverwritten() throws Exception { AtomicReference exceptionHolder = new AtomicReference<>(); AtomicReference configBuilderHolder = new AtomicReference<>(); - blockingCall(actionListener -> datafeedConfigProvider.getDatafeedConfig(dfId, actionListener), - configBuilderHolder, exceptionHolder); + blockingCall( + actionListener -> datafeedConfigProvider.getDatafeedConfig(dfId, actionListener), + configBuilderHolder, + exceptionHolder + ); assertNull(exceptionHolder.get()); assertThat(configBuilderHolder.get().build().getHeaders().entrySet(), hasSize(1)); assertEquals(configBuilderHolder.get().build().getHeaders(), createSecurityHeader()); diff --git a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/EstablishedMemUsageIT.java b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/EstablishedMemUsageIT.java index 2ecf27d083ccc..224e2d3a2b615 100644 --- a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/EstablishedMemUsageIT.java +++ b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/EstablishedMemUsageIT.java @@ -47,13 +47,19 @@ public class EstablishedMemUsageIT extends BaseMlIntegTestCase { public void createComponents() { Settings settings = nodeSettings(0, Settings.EMPTY); ThreadPool tp = mockThreadPool(); - ClusterSettings clusterSettings = new ClusterSettings(settings, - new HashSet<>(Arrays.asList(InferenceProcessor.MAX_INFERENCE_PROCESSORS, - MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING, - ResultsPersisterService.PERSIST_RESULTS_MAX_RETRIES, - OperationRouting.USE_ADAPTIVE_REPLICA_SELECTION_SETTING, - ClusterService.USER_DEFINED_METADATA, - ClusterApplierService.CLUSTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING))); + ClusterSettings clusterSettings = new ClusterSettings( + settings, + new HashSet<>( + Arrays.asList( + InferenceProcessor.MAX_INFERENCE_PROCESSORS, + MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING, + ResultsPersisterService.PERSIST_RESULTS_MAX_RETRIES, + OperationRouting.USE_ADAPTIVE_REPLICA_SELECTION_SETTING, + ClusterService.USER_DEFINED_METADATA, + ClusterApplierService.CLUSTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING + ) + ) + ); ClusterService clusterService = new ClusterService(settings, clusterSettings, tp); OriginSettingClient originSettingClient = new OriginSettingClient(client(), ClientHelper.ML_ORIGIN); @@ -257,10 +263,10 @@ private void createBuckets(String jobId, int count) { } private ModelSizeStats createModelSizeStats(String jobId, int bucketNum, long modelBytes) { - ModelSizeStats modelSizeStats = new ModelSizeStats.Builder(jobId) - .setTimestamp(new Date(bucketSpan * bucketNum)) - .setLogTime(new Date(bucketSpan * bucketNum + randomIntBetween(1, 1000))) - .setModelBytes(modelBytes).build(); + ModelSizeStats modelSizeStats = new ModelSizeStats.Builder(jobId).setTimestamp(new Date(bucketSpan * bucketNum)) + .setLogTime(new Date(bucketSpan * bucketNum + randomIntBetween(1, 1000))) + .setModelBytes(modelBytes) + .build(); jobResultsPersister.persistModelSizeStats(modelSizeStats, () -> true); return modelSizeStats; } @@ -269,8 +275,7 @@ private Long queryEstablishedMemoryUsage(String jobId) throws Exception { return queryEstablishedMemoryUsage(jobId, null, null); } - private Long queryEstablishedMemoryUsage(String jobId, Integer bucketNum, ModelSizeStats latestModelSizeStats) - throws Exception { + private Long queryEstablishedMemoryUsage(String jobId, Integer bucketNum, ModelSizeStats latestModelSizeStats) throws Exception { AtomicReference establishedModelMemoryUsage = new AtomicReference<>(); AtomicReference exception = new AtomicReference<>(); @@ -278,12 +283,12 @@ private Long queryEstablishedMemoryUsage(String jobId, Integer bucketNum, ModelS Date latestBucketTimestamp = (bucketNum != null) ? new Date(bucketSpan * bucketNum) : null; jobResultsProvider.getEstablishedMemoryUsage(jobId, latestBucketTimestamp, latestModelSizeStats, memUse -> { - establishedModelMemoryUsage.set(memUse); - latch.countDown(); - }, e -> { - exception.set(e); - latch.countDown(); - }); + establishedModelMemoryUsage.set(memUse); + latch.countDown(); + }, e -> { + exception.set(e); + latch.countDown(); + }); latch.await(); diff --git a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/IndexLayoutIT.java b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/IndexLayoutIT.java index 25df6ea0e0991..0c4b9c5f7c36b 100644 --- a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/IndexLayoutIT.java +++ b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/IndexLayoutIT.java @@ -30,79 +30,91 @@ public class IndexLayoutIT extends BaseMlIntegTestCase { - public void testCrudOnTwoJobsInSharedIndex() throws Exception { String jobId = "index-layout-job"; String jobId2 = "index-layout-job2"; - client().execute(PutJobAction.INSTANCE, new PutJobAction.Request(createJob(jobId, ByteSizeValue.ofMb(2)))).get(); - client().execute(PutJobAction.INSTANCE, new PutJobAction.Request(createJob(jobId2, ByteSizeValue.ofMb(2)))).get(); + client().execute(PutJobAction.INSTANCE, new PutJobAction.Request(createJob(jobId, ByteSizeValue.ofMb(2)))).get(); + client().execute(PutJobAction.INSTANCE, new PutJobAction.Request(createJob(jobId2, ByteSizeValue.ofMb(2)))).get(); client().execute(OpenJobAction.INSTANCE, new OpenJobAction.Request(jobId)).get(); client().execute(OpenJobAction.INSTANCE, new OpenJobAction.Request(jobId2)).get(); assertBusy(() -> { - GetJobsStatsAction.Response statsResponse = - client().execute(GetJobsStatsAction.INSTANCE, new GetJobsStatsAction.Request(jobId)).actionGet(); + GetJobsStatsAction.Response statsResponse = client().execute(GetJobsStatsAction.INSTANCE, new GetJobsStatsAction.Request(jobId)) + .actionGet(); assertEquals(statsResponse.getResponse().results().get(0).getState(), JobState.OPENED); }); assertBusy(() -> { - GetJobsStatsAction.Response statsResponse = - client().execute(GetJobsStatsAction.INSTANCE, new GetJobsStatsAction.Request(jobId2)).actionGet(); + GetJobsStatsAction.Response statsResponse = client().execute( + GetJobsStatsAction.INSTANCE, + new GetJobsStatsAction.Request(jobId2) + ).actionGet(); assertEquals(statsResponse.getResponse().results().get(0).getState(), JobState.OPENED); }); OriginSettingClient client = new OriginSettingClient(client(), ML_ORIGIN); - assertThat(client - .admin() - .indices() - .prepareGetIndex() - .addIndices(AnomalyDetectorsIndex.jobStateIndexPattern()).get().indices(), arrayContaining(".ml-state-000001")); - assertThat(client - .admin() - .indices() - .prepareGetAliases(AnomalyDetectorsIndex.jobStateIndexPattern()) - .get() - .getAliases() - .get(".ml-state-000001") - .get(0) - .alias(), equalTo(".ml-state-write")); - assertThat(client - .admin() - .indices() - .prepareGetIndex() - .addIndices(AnomalyDetectorsIndex.jobResultsAliasedName(jobId)).get().indices().length, equalTo(1)); - assertThat(client - .admin() - .indices() - .prepareGetIndex() - .addIndices(AnomalyDetectorsIndex.jobResultsAliasedName(jobId2)).get().indices().length, equalTo(1)); + assertThat( + client.admin().indices().prepareGetIndex().addIndices(AnomalyDetectorsIndex.jobStateIndexPattern()).get().indices(), + arrayContaining(".ml-state-000001") + ); + assertThat( + client.admin() + .indices() + .prepareGetAliases(AnomalyDetectorsIndex.jobStateIndexPattern()) + .get() + .getAliases() + .get(".ml-state-000001") + .get(0) + .alias(), + equalTo(".ml-state-write") + ); + assertThat( + client.admin() + .indices() + .prepareGetIndex() + .addIndices(AnomalyDetectorsIndex.jobResultsAliasedName(jobId)) + .get() + .indices().length, + equalTo(1) + ); + assertThat( + client.admin() + .indices() + .prepareGetIndex() + .addIndices(AnomalyDetectorsIndex.jobResultsAliasedName(jobId2)) + .get() + .indices().length, + equalTo(1) + ); } public void testForceCloseDoesNotCreateState() throws Exception { - client().admin().indices().prepareCreate("data") - .setMapping("time", "type=date") - .get(); + client().admin().indices().prepareCreate("data").setMapping("time", "type=date").get(); String jobId = "index-layout-force-close-job"; - client().execute(PutJobAction.INSTANCE, new PutJobAction.Request(createJob(jobId, ByteSizeValue.ofMb(2)) - .setDataDescription(new DataDescription.Builder()))).get(); + client().execute( + PutJobAction.INSTANCE, + new PutJobAction.Request(createJob(jobId, ByteSizeValue.ofMb(2)).setDataDescription(new DataDescription.Builder())) + ).get(); client().execute(OpenJobAction.INSTANCE, new OpenJobAction.Request(jobId)).get(); assertBusy(() -> { - GetJobsStatsAction.Response statsResponse = - client().execute(GetJobsStatsAction.INSTANCE, new GetJobsStatsAction.Request(jobId)).actionGet(); + GetJobsStatsAction.Response statsResponse = client().execute(GetJobsStatsAction.INSTANCE, new GetJobsStatsAction.Request(jobId)) + .actionGet(); assertEquals(statsResponse.getResponse().results().get(0).getState(), JobState.OPENED); }); long now = System.currentTimeMillis(); long weekAgo = now - 604800000; indexDocs(logger, "data", 100, weekAgo, now); - client().execute(PutDatafeedAction.INSTANCE, - new PutDatafeedAction.Request(createDatafeed(jobId + "-datafeed", jobId, Collections.singletonList("data")))).get(); + client().execute( + PutDatafeedAction.INSTANCE, + new PutDatafeedAction.Request(createDatafeed(jobId + "-datafeed", jobId, Collections.singletonList("data"))) + ).get(); client().execute(StartDatafeedAction.INSTANCE, new StartDatafeedAction.Request(jobId + "-datafeed", 0)).get(); assertBusy(() -> { - GetJobsStatsAction.Response statsResponse = - client().execute(GetJobsStatsAction.INSTANCE, new GetJobsStatsAction.Request(jobId)).actionGet(); + GetJobsStatsAction.Response statsResponse = client().execute(GetJobsStatsAction.INSTANCE, new GetJobsStatsAction.Request(jobId)) + .actionGet(); assertThat(statsResponse.getResponse().results().get(0).getDataCounts().getInputRecordCount(), greaterThan(0L)); }); @@ -112,26 +124,21 @@ public void testForceCloseDoesNotCreateState() throws Exception { closeRequest.setForce(true); client().execute(CloseJobAction.INSTANCE, closeRequest).get(); assertBusy(() -> { - GetJobsStatsAction.Response statsResponse = - client().execute(GetJobsStatsAction.INSTANCE, new GetJobsStatsAction.Request(jobId)).actionGet(); + GetJobsStatsAction.Response statsResponse = client().execute(GetJobsStatsAction.INSTANCE, new GetJobsStatsAction.Request(jobId)) + .actionGet(); assertThat(statsResponse.getResponse().results().get(0).getState(), equalTo(JobState.CLOSED)); }); OriginSettingClient client = new OriginSettingClient(client(), ML_ORIGIN); - assertThat(client - .admin() - .indices() - .prepareGetIndex() - .addIndices(AnomalyDetectorsIndex.jobStateIndexPattern()).get().indices(), arrayContaining(".ml-state-000001")); - - assertThat(client - .prepareSearch(AnomalyDetectorsIndex.jobStateIndexPattern()) - .setTrackTotalHits(true) - .get() - .getHits() - .getTotalHits() - .value, equalTo(0L)); + assertThat( + client.admin().indices().prepareGetIndex().addIndices(AnomalyDetectorsIndex.jobStateIndexPattern()).get().indices(), + arrayContaining(".ml-state-000001") + ); + + assertThat( + client.prepareSearch(AnomalyDetectorsIndex.jobStateIndexPattern()).setTrackTotalHits(true).get().getHits().getTotalHits().value, + equalTo(0L) + ); } - } diff --git a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/JobConfigProviderIT.java b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/JobConfigProviderIT.java index f5542477a5f1e..f842ba13d7564 100644 --- a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/JobConfigProviderIT.java +++ b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/JobConfigProviderIT.java @@ -85,8 +85,11 @@ public void testCheckJobExists() throws InterruptedException { AtomicReference exceptionHolder = new AtomicReference<>(); boolean throwIfMissing = randomBoolean(); - blockingCall(actionListener -> - jobConfigProvider.jobExists("missing", throwIfMissing, actionListener), jobExistsHolder, exceptionHolder); + blockingCall( + actionListener -> jobConfigProvider.jobExists("missing", throwIfMissing, actionListener), + jobExistsHolder, + exceptionHolder + ); if (throwIfMissing) { assertNull(jobExistsHolder.get()); @@ -104,8 +107,11 @@ public void testCheckJobExists() throws InterruptedException { blockingCall(actionListener -> jobConfigProvider.putJob(job, actionListener), indexResponseHolder, exceptionHolder); exceptionHolder.set(null); - blockingCall(actionListener -> - jobConfigProvider.jobExists("existing-job", throwIfMissing, actionListener), jobExistsHolder, exceptionHolder); + blockingCall( + actionListener -> jobConfigProvider.jobExists("existing-job", throwIfMissing, actionListener), + jobExistsHolder, + exceptionHolder + ); assertNull(exceptionHolder.get()); assertNotNull(jobExistsHolder.get()); assertTrue(jobExistsHolder.get()); @@ -155,8 +161,11 @@ public void testCrud() throws InterruptedException { JobUpdate jobUpdate = new JobUpdate.Builder(jobId).setDescription("This job has been updated").build(); AtomicReference updateJobResponseHolder = new AtomicReference<>(); - blockingCall(actionListener -> jobConfigProvider.updateJob - (jobId, jobUpdate, ByteSizeValue.ofBytes(32), actionListener), updateJobResponseHolder, exceptionHolder); + blockingCall( + actionListener -> jobConfigProvider.updateJob(jobId, jobUpdate, ByteSizeValue.ofBytes(32), actionListener), + updateJobResponseHolder, + exceptionHolder + ); assertNull(exceptionHolder.get()); assertEquals("This job has been updated", updateJobResponseHolder.get().getDescription()); @@ -167,8 +176,7 @@ public void testCrud() throws InterruptedException { // Delete Job AtomicReference deleteJobResponseHolder = new AtomicReference<>(); - blockingCall(actionListener -> jobConfigProvider.deleteJob(jobId, true, actionListener), - deleteJobResponseHolder, exceptionHolder); + blockingCall(actionListener -> jobConfigProvider.deleteJob(jobId, true, actionListener), deleteJobResponseHolder, exceptionHolder); assertNull(exceptionHolder.get()); assertThat(deleteJobResponseHolder.get().getResult(), equalTo(DocWriteResponse.Result.DELETED)); @@ -181,16 +189,14 @@ public void testCrud() throws InterruptedException { // Delete deleted job deleteJobResponseHolder.set(null); exceptionHolder.set(null); - blockingCall(actionListener -> jobConfigProvider.deleteJob(jobId, true, actionListener), - deleteJobResponseHolder, exceptionHolder); + blockingCall(actionListener -> jobConfigProvider.deleteJob(jobId, true, actionListener), deleteJobResponseHolder, exceptionHolder); assertNull(deleteJobResponseHolder.get()); assertEquals(ResourceNotFoundException.class, exceptionHolder.get().getClass()); // and again with errorIfMissing set false deleteJobResponseHolder.set(null); exceptionHolder.set(null); - blockingCall(actionListener -> jobConfigProvider.deleteJob(jobId, false, actionListener), - deleteJobResponseHolder, exceptionHolder); + blockingCall(actionListener -> jobConfigProvider.deleteJob(jobId, false, actionListener), deleteJobResponseHolder, exceptionHolder); assertEquals(DocWriteResponse.Result.NOT_FOUND, deleteJobResponseHolder.get().getResult()); } @@ -208,13 +214,14 @@ public void testUpdateWithAValidationError() throws Exception { DetectionRule rule = new DetectionRule.Builder(RuleScope.builder().exclude("not a used field", "filerfoo")).build(); JobUpdate.DetectorUpdate detectorUpdate = new JobUpdate.DetectorUpdate(0, null, Collections.singletonList(rule)); - JobUpdate invalidUpdate = new JobUpdate.Builder(jobId) - .setDetectorUpdates(Collections.singletonList(detectorUpdate)) - .build(); + JobUpdate invalidUpdate = new JobUpdate.Builder(jobId).setDetectorUpdates(Collections.singletonList(detectorUpdate)).build(); AtomicReference updateJobResponseHolder = new AtomicReference<>(); - blockingCall(actionListener -> jobConfigProvider.updateJob(jobId, invalidUpdate, ByteSizeValue.ofBytes(32), - actionListener), updateJobResponseHolder, exceptionHolder); + blockingCall( + actionListener -> jobConfigProvider.updateJob(jobId, invalidUpdate, ByteSizeValue.ofBytes(32), actionListener), + updateJobResponseHolder, + exceptionHolder + ); assertNull(updateJobResponseHolder.get()); assertNotNull(exceptionHolder.get()); assertThat(exceptionHolder.get(), instanceOf(ElasticsearchStatusException.class)); @@ -230,15 +237,22 @@ public void testUpdateWithValidator() throws Exception { JobUpdate jobUpdate = new JobUpdate.Builder(jobId).setDescription("This job has been updated").build(); - JobConfigProvider.UpdateValidator validator = (job, update, listener) -> { - listener.onResponse(null); - }; + JobConfigProvider.UpdateValidator validator = (job, update, listener) -> { listener.onResponse(null); }; AtomicReference exceptionHolder = new AtomicReference<>(); AtomicReference updateJobResponseHolder = new AtomicReference<>(); // update with the no-op validator - blockingCall(actionListener -> jobConfigProvider.updateJobWithValidation( - jobId, jobUpdate, ByteSizeValue.ofBytes(32), validator, actionListener), updateJobResponseHolder, exceptionHolder); + blockingCall( + actionListener -> jobConfigProvider.updateJobWithValidation( + jobId, + jobUpdate, + ByteSizeValue.ofBytes(32), + validator, + actionListener + ), + updateJobResponseHolder, + exceptionHolder + ); assertNull(exceptionHolder.get()); assertNotNull(updateJobResponseHolder.get()); @@ -250,9 +264,17 @@ public void testUpdateWithValidator() throws Exception { updateJobResponseHolder.set(null); // Update with a validator that errors - blockingCall(actionListener -> jobConfigProvider.updateJobWithValidation(jobId, jobUpdate, ByteSizeValue.ofBytes(32), - validatorWithAnError, actionListener), - updateJobResponseHolder, exceptionHolder); + blockingCall( + actionListener -> jobConfigProvider.updateJobWithValidation( + jobId, + jobUpdate, + ByteSizeValue.ofBytes(32), + validatorWithAnError, + actionListener + ), + updateJobResponseHolder, + exceptionHolder + ); assertNull(updateJobResponseHolder.get()); assertNotNull(exceptionHolder.get()); @@ -264,8 +286,11 @@ public void testAllowNoMatch() throws InterruptedException { AtomicReference> jobIdsHolder = new AtomicReference<>(); AtomicReference exceptionHolder = new AtomicReference<>(); - blockingCall(actionListener -> jobConfigProvider.expandJobsIds("_all", false, true, null, false, actionListener), - jobIdsHolder, exceptionHolder); + blockingCall( + actionListener -> jobConfigProvider.expandJobsIds("_all", false, true, null, false, actionListener), + jobIdsHolder, + exceptionHolder + ); assertNull(jobIdsHolder.get()); assertNotNull(exceptionHolder.get()); @@ -273,14 +298,16 @@ public void testAllowNoMatch() throws InterruptedException { assertThat(exceptionHolder.get().getMessage(), containsString("No known job with id")); exceptionHolder.set(null); - blockingCall(actionListener -> jobConfigProvider.expandJobsIds("_all", true, false, null, false, actionListener), - jobIdsHolder, exceptionHolder); + blockingCall( + actionListener -> jobConfigProvider.expandJobsIds("_all", true, false, null, false, actionListener), + jobIdsHolder, + exceptionHolder + ); assertNotNull(jobIdsHolder.get()); assertNull(exceptionHolder.get()); AtomicReference> jobsHolder = new AtomicReference<>(); - blockingCall(actionListener -> jobConfigProvider.expandJobs("*", false, true, actionListener), - jobsHolder, exceptionHolder); + blockingCall(actionListener -> jobConfigProvider.expandJobs("*", false, true, actionListener), jobsHolder, exceptionHolder); assertNull(jobsHolder.get()); assertNotNull(exceptionHolder.get()); @@ -288,8 +315,7 @@ public void testAllowNoMatch() throws InterruptedException { assertThat(exceptionHolder.get().getMessage(), containsString("No known job with id")); exceptionHolder.set(null); - blockingCall(actionListener -> jobConfigProvider.expandJobs("*", true, true, actionListener), - jobsHolder, exceptionHolder); + blockingCall(actionListener -> jobConfigProvider.expandJobs("*", true, true, actionListener), jobsHolder, exceptionHolder); assertNotNull(jobsHolder.get()); assertNull(exceptionHolder.get()); } @@ -303,62 +329,61 @@ public void testExpandJobs_GroupsAndJobIds() throws Exception { client().admin().indices().prepareRefresh(MlConfigIndex.indexName()).get(); // Job Ids - SortedSet expandedIds = blockingCall(actionListener -> - jobConfigProvider.expandJobsIds("_all", true, false, null, false, actionListener)); + SortedSet expandedIds = blockingCall( + actionListener -> jobConfigProvider.expandJobsIds("_all", true, false, null, false, actionListener) + ); assertEquals(new TreeSet<>(Arrays.asList("tom", "dick", "harry", "harry-jnr")), expandedIds); expandedIds = blockingCall(actionListener -> jobConfigProvider.expandJobsIds("*", true, true, null, false, actionListener)); assertEquals(new TreeSet<>(Arrays.asList("tom", "dick", "harry", "harry-jnr")), expandedIds); - expandedIds = blockingCall(actionListener -> jobConfigProvider.expandJobsIds("tom,harry", - true, - false, - null, - false, - actionListener)); + expandedIds = blockingCall( + actionListener -> jobConfigProvider.expandJobsIds("tom,harry", true, false, null, false, actionListener) + ); assertEquals(new TreeSet<>(Arrays.asList("tom", "harry")), expandedIds); - expandedIds = blockingCall(actionListener -> jobConfigProvider.expandJobsIds("harry-group,tom", - true, - false, - null, - false, - actionListener)); + expandedIds = blockingCall( + actionListener -> jobConfigProvider.expandJobsIds("harry-group,tom", true, false, null, false, actionListener) + ); assertEquals(new TreeSet<>(Arrays.asList("harry", "harry-jnr", "tom")), expandedIds); AtomicReference exceptionHolder = new AtomicReference<>(); AtomicReference> jobIdsHolder = new AtomicReference<>(); - blockingCall(actionListener -> jobConfigProvider.expandJobsIds("tom,missing1,missing2", true, false, null, false, actionListener), - jobIdsHolder, exceptionHolder); + blockingCall( + actionListener -> jobConfigProvider.expandJobsIds("tom,missing1,missing2", true, false, null, false, actionListener), + jobIdsHolder, + exceptionHolder + ); assertNull(jobIdsHolder.get()); assertNotNull(exceptionHolder.get()); assertEquals(ResourceNotFoundException.class, exceptionHolder.get().getClass()); assertThat(exceptionHolder.get().getMessage(), equalTo("No known job with id 'missing1,missing2'")); // Job builders - List expandedJobsBuilders = blockingCall(actionListener -> - jobConfigProvider.expandJobs("harry-group,tom", false, true, actionListener)); + List expandedJobsBuilders = blockingCall( + actionListener -> jobConfigProvider.expandJobs("harry-group,tom", false, true, actionListener) + ); List expandedJobs = expandedJobsBuilders.stream().map(Job.Builder::build).collect(Collectors.toList()); assertThat(expandedJobs, containsInAnyOrder(harry, harryJnr, tom)); - expandedJobsBuilders = blockingCall(actionListener -> - jobConfigProvider.expandJobs("_all", false, true, actionListener)); + expandedJobsBuilders = blockingCall(actionListener -> jobConfigProvider.expandJobs("_all", false, true, actionListener)); expandedJobs = expandedJobsBuilders.stream().map(Job.Builder::build).collect(Collectors.toList()); assertThat(expandedJobs, containsInAnyOrder(tom, dick, harry, harryJnr)); - expandedJobsBuilders = blockingCall(actionListener -> - jobConfigProvider.expandJobs("tom,harry", false, false, actionListener)); + expandedJobsBuilders = blockingCall(actionListener -> jobConfigProvider.expandJobs("tom,harry", false, false, actionListener)); expandedJobs = expandedJobsBuilders.stream().map(Job.Builder::build).collect(Collectors.toList()); assertThat(expandedJobs, containsInAnyOrder(tom, harry)); - expandedJobsBuilders = blockingCall(actionListener -> - jobConfigProvider.expandJobs("", false, false, actionListener)); + expandedJobsBuilders = blockingCall(actionListener -> jobConfigProvider.expandJobs("", false, false, actionListener)); expandedJobs = expandedJobsBuilders.stream().map(Job.Builder::build).collect(Collectors.toList()); assertThat(expandedJobs, containsInAnyOrder(tom, dick, harry, harryJnr)); AtomicReference> jobsHolder = new AtomicReference<>(); - blockingCall(actionListener -> jobConfigProvider.expandJobs("tom,missing1,missing2", false, true, actionListener), - jobsHolder, exceptionHolder); + blockingCall( + actionListener -> jobConfigProvider.expandJobs("tom,missing1,missing2", false, true, actionListener), + jobsHolder, + exceptionHolder + ); assertNull(jobsHolder.get()); assertNotNull(exceptionHolder.get()); assertEquals(ResourceNotFoundException.class, exceptionHolder.get().getClass()); @@ -375,15 +400,12 @@ public void testExpandJobs_WildCardExpansion() throws Exception { client().admin().indices().prepareRefresh(MlConfigIndex.indexName()).get(); // Test job IDs only - SortedSet expandedIds = blockingCall(actionListener -> jobConfigProvider.expandJobsIds("foo*", - true, - true, - null, - false, - actionListener)); + SortedSet expandedIds = blockingCall( + actionListener -> jobConfigProvider.expandJobsIds("foo*", true, true, null, false, actionListener) + ); assertEquals(new TreeSet<>(Arrays.asList("foo-1", "foo-2")), expandedIds); - expandedIds = blockingCall(actionListener -> jobConfigProvider.expandJobsIds("*-1", true, true, null, false,actionListener)); + expandedIds = blockingCall(actionListener -> jobConfigProvider.expandJobsIds("*-1", true, true, null, false, actionListener)); assertEquals(new TreeSet<>(Arrays.asList("bar-1", "foo-1")), expandedIds); expandedIds = blockingCall(actionListener -> jobConfigProvider.expandJobsIds("bar*", true, true, null, false, actionListener)); @@ -393,8 +415,9 @@ public void testExpandJobs_WildCardExpansion() throws Exception { assertEquals(new TreeSet<>(Collections.singletonList("bar-1")), expandedIds); // Test full job config - List expandedJobsBuilders = - blockingCall(actionListener -> jobConfigProvider.expandJobs("foo*", true, true, actionListener)); + List expandedJobsBuilders = blockingCall( + actionListener -> jobConfigProvider.expandJobs("foo*", true, true, actionListener) + ); List expandedJobs = expandedJobsBuilders.stream().map(Job.Builder::build).collect(Collectors.toList()); assertThat(expandedJobs, containsInAnyOrder(foo1, foo2)); @@ -417,18 +440,20 @@ public void testExpandJobIds_excludeDeleting() throws Exception { putJob(createJob("foo-deleting", null)); putJob(createJob("bar", null)); - PutJobAction.Response marked = blockingCall(actionListener -> jobConfigProvider.updateJobBlockReason( - "foo-deleting", new Blocked(Blocked.Reason.DELETE, null), actionListener)); + PutJobAction.Response marked = blockingCall( + actionListener -> jobConfigProvider.updateJobBlockReason( + "foo-deleting", + new Blocked(Blocked.Reason.DELETE, null), + actionListener + ) + ); assertThat(marked.getResponse().getBlocked().getReason(), equalTo(Blocked.Reason.DELETE)); client().admin().indices().prepareRefresh(MlConfigIndex.indexName()).get(); - SortedSet expandedIds = blockingCall(actionListener -> jobConfigProvider.expandJobsIds("foo*", - true, - true, - null, - false, - actionListener)); + SortedSet expandedIds = blockingCall( + actionListener -> jobConfigProvider.expandJobsIds("foo*", true, true, null, false, actionListener) + ); assertEquals(new TreeSet<>(Arrays.asList("foo-1", "foo-2")), expandedIds); expandedIds = blockingCall(actionListener -> jobConfigProvider.expandJobsIds("foo*", true, false, null, false, actionListener)); @@ -440,8 +465,9 @@ public void testExpandJobIds_excludeDeleting() throws Exception { expandedIds = blockingCall(actionListener -> jobConfigProvider.expandJobsIds("*", true, false, null, false, actionListener)); assertEquals(new TreeSet<>(Arrays.asList("foo-1", "foo-2", "foo-deleting", "bar")), expandedIds); - List expandedJobsBuilders = - blockingCall(actionListener -> jobConfigProvider.expandJobs("foo*", true, true, actionListener)); + List expandedJobsBuilders = blockingCall( + actionListener -> jobConfigProvider.expandJobs("foo*", true, true, actionListener) + ); assertThat(expandedJobsBuilders, hasSize(2)); expandedJobsBuilders = blockingCall(actionListener -> jobConfigProvider.expandJobs("foo*", true, false, actionListener)); @@ -455,22 +481,28 @@ public void testExpandJobIdsWithTaskData() throws Exception { client().admin().indices().prepareRefresh(MlConfigIndex.indexName()).get(); PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); - tasksBuilder.addTask(MlTasks.jobTaskId("foo-2"), - MlTasks.JOB_TASK_NAME, new OpenJobAction.JobParams("foo-2"), - new PersistentTasksCustomMetadata.Assignment("node-1", "test assignment")); + tasksBuilder.addTask( + MlTasks.jobTaskId("foo-2"), + MlTasks.JOB_TASK_NAME, + new OpenJobAction.JobParams("foo-2"), + new PersistentTasksCustomMetadata.Assignment("node-1", "test assignment") + ); PersistentTasksCustomMetadata tasks = tasksBuilder.build(); AtomicReference exceptionHolder = new AtomicReference<>(); AtomicReference> jobIdsHolder = new AtomicReference<>(); // Test job IDs only - SortedSet expandedIds = - blockingCall(actionListener -> jobConfigProvider.expandJobsIds("foo*", false, false, tasks, true, actionListener)); + SortedSet expandedIds = blockingCall( + actionListener -> jobConfigProvider.expandJobsIds("foo*", false, false, tasks, true, actionListener) + ); assertEquals(new TreeSet<>(Arrays.asList("foo-1", "foo-2")), expandedIds); - blockingCall(actionListener -> jobConfigProvider.expandJobsIds("foo-1*,foo-2*", false, false, tasks, false, actionListener), + blockingCall( + actionListener -> jobConfigProvider.expandJobsIds("foo-1*,foo-2*", false, false, tasks, false, actionListener), jobIdsHolder, - exceptionHolder); + exceptionHolder + ); assertThat(exceptionHolder.get(), is(not(nullValue()))); assertEquals(ResourceNotFoundException.class, exceptionHolder.get().getClass()); assertThat(exceptionHolder.get().getMessage(), containsString("No known job with id 'foo-2*'")); @@ -486,20 +518,20 @@ public void testExpandGroups() throws Exception { client().admin().indices().prepareRefresh(MlConfigIndex.indexName()).get(); - SortedSet expandedIds = blockingCall(actionListener -> - jobConfigProvider.expandGroupIds(Collections.singletonList("fruit"), actionListener)); + SortedSet expandedIds = blockingCall( + actionListener -> jobConfigProvider.expandGroupIds(Collections.singletonList("fruit"), actionListener) + ); assertThat(expandedIds, contains("apples", "pears", "tomato")); - expandedIds = blockingCall(actionListener -> - jobConfigProvider.expandGroupIds(Collections.singletonList("veg"), actionListener)); + expandedIds = blockingCall(actionListener -> jobConfigProvider.expandGroupIds(Collections.singletonList("veg"), actionListener)); assertThat(expandedIds, contains("broccoli", "potato", "tomato")); - expandedIds = blockingCall(actionListener -> - jobConfigProvider.expandGroupIds(Arrays.asList("fruit", "veg"), actionListener)); + expandedIds = blockingCall(actionListener -> jobConfigProvider.expandGroupIds(Arrays.asList("fruit", "veg"), actionListener)); assertThat(expandedIds, contains("apples", "broccoli", "pears", "potato", "tomato")); - expandedIds = blockingCall(actionListener -> - jobConfigProvider.expandGroupIds(Collections.singletonList("unknown-group"), actionListener)); + expandedIds = blockingCall( + actionListener -> jobConfigProvider.expandGroupIds(Collections.singletonList("unknown-group"), actionListener) + ); assertThat(expandedIds, empty()); } @@ -511,8 +543,9 @@ public void testFindJobsWithCustomRules_GivenNoJobs() throws Exception { public void testFindJobsWithCustomRules() throws Exception { putJob(createJob("job-without-rules", Collections.emptyList())); - DetectionRule rule = new DetectionRule.Builder(Collections.singletonList( - new RuleCondition(RuleCondition.AppliesTo.ACTUAL, Operator.GT, 0.0))).build(); + DetectionRule rule = new DetectionRule.Builder( + Collections.singletonList(new RuleCondition(RuleCondition.AppliesTo.ACTUAL, Operator.GT, 0.0)) + ).build(); Job.Builder jobWithRules1 = createJob("job-with-rules-1", Collections.emptyList()); jobWithRules1 = addCustomRule(jobWithRules1, rule); @@ -551,8 +584,10 @@ public void testValidateDatafeedJob() throws Exception { // This config is not valid because it uses aggs but the job's // summary count field is not set MaxAggregationBuilder maxTime = AggregationBuilders.max("time").field("time"); - HistogramAggregationBuilder histogram = - AggregationBuilders.histogram("time").interval(1800.0).field("time").subAggregation(maxTime); + HistogramAggregationBuilder histogram = AggregationBuilders.histogram("time") + .interval(1800.0) + .field("time") + .subAggregation(maxTime); builder.setParsedAggregations(new AggregatorFactories.Builder().addAggregator(histogram)); DatafeedConfig badConfig = builder.build(); @@ -566,8 +601,11 @@ public void testUpdateJobBlockReason() throws Exception { AtomicReference responseHolder = new AtomicReference<>(); AtomicReference exceptionHolder = new AtomicReference<>(); - blockingCall(listener -> jobConfigProvider.updateJobBlockReason( - "missing-job", new Blocked(Blocked.Reason.RESET, null), listener), responseHolder, exceptionHolder); + blockingCall( + listener -> jobConfigProvider.updateJobBlockReason("missing-job", new Blocked(Blocked.Reason.RESET, null), listener), + responseHolder, + exceptionHolder + ); assertNull(responseHolder.get()); assertEquals(ResourceNotFoundException.class, exceptionHolder.get().getClass()); @@ -576,14 +614,20 @@ public void testUpdateJobBlockReason() throws Exception { client().admin().indices().prepareRefresh(MlConfigIndex.indexName()).get(); exceptionHolder.set(null); - blockingCall(listener -> jobConfigProvider.updateJobBlockReason( - jobId, new Blocked(Blocked.Reason.RESET, null), listener), responseHolder, exceptionHolder); + blockingCall( + listener -> jobConfigProvider.updateJobBlockReason(jobId, new Blocked(Blocked.Reason.RESET, null), listener), + responseHolder, + exceptionHolder + ); assertNull(exceptionHolder.get()); assertThat(responseHolder.get().getResponse().getBlocked().getReason(), equalTo(Blocked.Reason.RESET)); // repeat the update for good measure - blockingCall(listener -> jobConfigProvider.updateJobBlockReason( - jobId, new Blocked(Blocked.Reason.RESET, null), listener), responseHolder, exceptionHolder); + blockingCall( + listener -> jobConfigProvider.updateJobBlockReason(jobId, new Blocked(Blocked.Reason.RESET, null), listener), + responseHolder, + exceptionHolder + ); assertNull(exceptionHolder.get()); assertThat(responseHolder.get().getResponse().getBlocked().getReason(), equalTo(Blocked.Reason.RESET)); } diff --git a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/JobModelSnapshotCRUDIT.java b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/JobModelSnapshotCRUDIT.java index 1d3d28eb50ba3..cdc060afee75b 100644 --- a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/JobModelSnapshotCRUDIT.java +++ b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/JobModelSnapshotCRUDIT.java @@ -59,13 +59,19 @@ public class JobModelSnapshotCRUDIT extends MlSingleNodeTestCase { @Before public void createComponents() throws Exception { ThreadPool tp = mockThreadPool(); - ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, - new HashSet<>(Arrays.asList(InferenceProcessor.MAX_INFERENCE_PROCESSORS, - MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING, - OperationRouting.USE_ADAPTIVE_REPLICA_SELECTION_SETTING, - ResultsPersisterService.PERSIST_RESULTS_MAX_RETRIES, - ClusterService.USER_DEFINED_METADATA, - ClusterApplierService.CLUSTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING))); + ClusterSettings clusterSettings = new ClusterSettings( + Settings.EMPTY, + new HashSet<>( + Arrays.asList( + InferenceProcessor.MAX_INFERENCE_PROCESSORS, + MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING, + OperationRouting.USE_ADAPTIVE_REPLICA_SELECTION_SETTING, + ResultsPersisterService.PERSIST_RESULTS_MAX_RETRIES, + ClusterService.USER_DEFINED_METADATA, + ClusterApplierService.CLUSTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING + ) + ) + ); ClusterService clusterService = new ClusterService(Settings.EMPTY, clusterSettings, tp); OriginSettingClient originSettingClient = new OriginSettingClient(client(), ClientHelper.ML_ORIGIN); @@ -91,7 +97,8 @@ public void testUpgradeAlreadyUpgradedSnapshot() { () -> client().execute( UpgradeJobModelSnapshotAction.INSTANCE, new UpgradeJobModelSnapshotAction.Request(jobId, "snap_1", TimeValue.timeValueMinutes(10), true) - ).actionGet()); + ).actionGet() + ); assertThat(ex.status(), equalTo(RestStatus.CONFLICT)); assertThat( ex.getMessage(), @@ -105,15 +112,12 @@ public void testUpdateModelSnapshot() { String jobId = "update-job-model-snapshot"; createJob(jobId); Date timestamp = new Date(); - ModelSnapshot snapshot = new ModelSnapshot - .Builder(jobId) - .setMinVersion(Version.CURRENT) + ModelSnapshot snapshot = new ModelSnapshot.Builder(jobId).setMinVersion(Version.CURRENT) .setTimestamp(timestamp) .setSnapshotId("snap_1") .build(); indexModelSnapshot(snapshot); - UpdateModelSnapshotAction.Request request = new UpdateModelSnapshotAction.Request(jobId, "snap_1"); request.setDescription("new_description"); request.setRetain(true); @@ -121,8 +125,10 @@ public void testUpdateModelSnapshot() { assertThat(response.getModel().isRetain(), is(true)); assertThat(response.getModel().getDescription(), equalTo("new_description")); - GetModelSnapshotsAction.Response getResponse = - client().execute(GetModelSnapshotsAction.INSTANCE, new GetModelSnapshotsAction.Request(jobId, "snap_1")).actionGet(); + GetModelSnapshotsAction.Response getResponse = client().execute( + GetModelSnapshotsAction.INSTANCE, + new GetModelSnapshotsAction.Request(jobId, "snap_1") + ).actionGet(); assertThat(getResponse.getResources().results().get(0).isRetain(), is(true)); assertThat(getResponse.getResources().results().get(0).getDescription(), equalTo("new_description")); assertThat(getResponse.getResources().results().get(0).getTimestamp(), equalTo(timestamp)); @@ -131,20 +137,17 @@ public void testUpdateModelSnapshot() { public void testDeleteUnusedModelSnapshot() { String jobId = "delete-job-model-snapshot-unused"; createJob(jobId); - ModelSnapshot snapshot = new ModelSnapshot - .Builder(jobId) - .setMinVersion(Version.CURRENT) - .setSnapshotId("snap_1") - .build(); + ModelSnapshot snapshot = new ModelSnapshot.Builder(jobId).setMinVersion(Version.CURRENT).setSnapshotId("snap_1").build(); indexModelSnapshot(snapshot); - GetModelSnapshotsAction.Response getResponse = - client().execute(GetModelSnapshotsAction.INSTANCE, new GetModelSnapshotsAction.Request(jobId, "snap_1")).actionGet(); + GetModelSnapshotsAction.Response getResponse = client().execute( + GetModelSnapshotsAction.INSTANCE, + new GetModelSnapshotsAction.Request(jobId, "snap_1") + ).actionGet(); assertThat(getResponse.getResources().results(), hasSize(1)); client().execute(DeleteModelSnapshotAction.INSTANCE, new DeleteModelSnapshotAction.Request(jobId, "snap_1")).actionGet(); - getResponse = - client().execute(GetModelSnapshotsAction.INSTANCE, new GetModelSnapshotsAction.Request(jobId, "snap_1")).actionGet(); + getResponse = client().execute(GetModelSnapshotsAction.INSTANCE, new GetModelSnapshotsAction.Request(jobId, "snap_1")).actionGet(); assertThat(getResponse.getResources().results(), hasSize(0)); } @@ -152,30 +155,29 @@ public void testDeleteUsedModelSnapshot() { String jobId = "delete-job-model-snapshot-used"; Date timestamp = new Date(); createJob(jobId); - ModelSnapshot snapshot = new ModelSnapshot - .Builder(jobId) - .setMinVersion(Version.CURRENT) + ModelSnapshot snapshot = new ModelSnapshot.Builder(jobId).setMinVersion(Version.CURRENT) .setSnapshotId("snap_1") .setQuantiles(new Quantiles(jobId, timestamp, "quantiles-1")) .setSnapshotDocCount(1) .setModelSizeStats(new ModelSizeStats.Builder(jobId).setTimestamp(timestamp).setLogTime(timestamp)) .build(); indexModelSnapshot(snapshot); - GetModelSnapshotsAction.Response getResponse = - client().execute(GetModelSnapshotsAction.INSTANCE, new GetModelSnapshotsAction.Request(jobId, "snap_1")).actionGet(); + GetModelSnapshotsAction.Response getResponse = client().execute( + GetModelSnapshotsAction.INSTANCE, + new GetModelSnapshotsAction.Request(jobId, "snap_1") + ).actionGet(); assertThat(getResponse.getResources().results(), hasSize(1)); client().execute(RevertModelSnapshotAction.INSTANCE, new RevertModelSnapshotAction.Request(jobId, "snap_1")).actionGet(); // should fail? - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, - () -> client() - .execute(DeleteModelSnapshotAction.INSTANCE, new DeleteModelSnapshotAction.Request(jobId, "snap_1")) - .actionGet()); - assertThat(ex.getMessage(), - containsString( - "Model snapshot 'snap_1' is the active snapshot for job 'delete-job-model-snapshot-used', so cannot be deleted" - ) + IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> client().execute(DeleteModelSnapshotAction.INSTANCE, new DeleteModelSnapshotAction.Request(jobId, "snap_1")).actionGet() + ); + assertThat( + ex.getMessage(), + containsString("Model snapshot 'snap_1' is the active snapshot for job 'delete-job-model-snapshot-used', so cannot be deleted") ); } diff --git a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/JobResultsProviderIT.java b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/JobResultsProviderIT.java index fdf6bf647b60c..505333634522e 100644 --- a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/JobResultsProviderIT.java +++ b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/JobResultsProviderIT.java @@ -34,12 +34,12 @@ import org.elasticsearch.common.settings.ClusterSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.indices.TestIndexNameExpressionResolver; +import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.indices.TestIndexNameExpressionResolver; -import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.xpack.core.ClientHelper; import org.elasticsearch.xpack.core.action.util.PageParams; import org.elasticsearch.xpack.core.action.util.QueryPage; @@ -103,7 +103,6 @@ import static org.hamcrest.collection.IsEmptyCollection.empty; import static org.hamcrest.core.Is.is; - public class JobResultsProviderIT extends MlSingleNodeTestCase { private JobResultsProvider jobProvider; @@ -113,16 +112,22 @@ public class JobResultsProviderIT extends MlSingleNodeTestCase { @Before public void createComponents() throws Exception { Settings.Builder builder = Settings.builder() - .put(UnassignedInfo.INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING.getKey(), TimeValue.timeValueSeconds(1)); + .put(UnassignedInfo.INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING.getKey(), TimeValue.timeValueSeconds(1)); jobProvider = new JobResultsProvider(client(), builder.build(), TestIndexNameExpressionResolver.newInstance()); ThreadPool tp = mockThreadPool(); - ClusterSettings clusterSettings = new ClusterSettings(builder.build(), - new HashSet<>(Arrays.asList(InferenceProcessor.MAX_INFERENCE_PROCESSORS, - MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING, - OperationRouting.USE_ADAPTIVE_REPLICA_SELECTION_SETTING, - ResultsPersisterService.PERSIST_RESULTS_MAX_RETRIES, - ClusterService.USER_DEFINED_METADATA, - ClusterApplierService.CLUSTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING))); + ClusterSettings clusterSettings = new ClusterSettings( + builder.build(), + new HashSet<>( + Arrays.asList( + InferenceProcessor.MAX_INFERENCE_PROCESSORS, + MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING, + OperationRouting.USE_ADAPTIVE_REPLICA_SELECTION_SETTING, + ResultsPersisterService.PERSIST_RESULTS_MAX_RETRIES, + ClusterService.USER_DEFINED_METADATA, + ClusterApplierService.CLUSTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING + ) + ) + ); ClusterService clusterService = new ClusterService(builder.build(), clusterSettings, tp); OriginSettingClient originSettingClient = new OriginSettingClient(client(), ClientHelper.ML_ORIGIN); @@ -149,10 +154,13 @@ public void testPutJob_CreatesResultsIndex() { assertThat(mappingProperties.keySet(), hasItem("by_field_1")); // Check aliases have been created - assertThat(getAliases(sharedResultsIndex), containsInAnyOrder( - AnomalyDetectorsIndex.jobResultsAliasedName(job1.getId()), - AnomalyDetectorsIndex.resultsWriteAlias(job1.getId()) - )); + assertThat( + getAliases(sharedResultsIndex), + containsInAnyOrder( + AnomalyDetectorsIndex.jobResultsAliasedName(job1.getId()), + AnomalyDetectorsIndex.resultsWriteAlias(job1.getId()) + ) + ); // Now let's create a second job to test things work when the index exists already assertThat(mappingProperties.keySet(), not(hasItem("by_field_2"))); @@ -171,12 +179,15 @@ public void testPutJob_CreatesResultsIndex() { assertThat(mappingProperties.keySet(), hasItems("by_field_1", "by_field_2")); // Check aliases have been created - assertThat(getAliases(sharedResultsIndex), containsInAnyOrder( - AnomalyDetectorsIndex.jobResultsAliasedName(job1.getId()), - AnomalyDetectorsIndex.resultsWriteAlias(job1.getId()), - AnomalyDetectorsIndex.jobResultsAliasedName(job2.getId()), - AnomalyDetectorsIndex.resultsWriteAlias(job2.getId()) - )); + assertThat( + getAliases(sharedResultsIndex), + containsInAnyOrder( + AnomalyDetectorsIndex.jobResultsAliasedName(job1.getId()), + AnomalyDetectorsIndex.resultsWriteAlias(job1.getId()), + AnomalyDetectorsIndex.jobResultsAliasedName(job2.getId()), + AnomalyDetectorsIndex.resultsWriteAlias(job2.getId()) + ) + ); } public void testPutJob_WithCustomResultsIndex() { @@ -196,13 +207,16 @@ public void testPutJob_WithCustomResultsIndex() { assertThat(mappingProperties.keySet(), hasItem("by_field")); // Check aliases have been created - assertThat(getAliases(customIndex), containsInAnyOrder( - AnomalyDetectorsIndex.jobResultsAliasedName(job.getId()), - AnomalyDetectorsIndex.resultsWriteAlias(job.getId()) - )); + assertThat( + getAliases(customIndex), + containsInAnyOrder( + AnomalyDetectorsIndex.jobResultsAliasedName(job.getId()), + AnomalyDetectorsIndex.resultsWriteAlias(job.getId()) + ) + ); } - @AwaitsFix(bugUrl ="https://github.com/elastic/elasticsearch/issues/40134") + @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/40134") public void testMultipleSimultaneousJobCreations() { int numJobs = randomIntBetween(4, 7); @@ -255,7 +269,7 @@ public void testGetCalandarByJobId() throws Exception { calendars.add(new Calendar("empty calendar", Collections.emptyList(), null)); calendars.add(new Calendar("foo calendar", Collections.singletonList("foo"), null)); calendars.add(new Calendar("foo bar calendar", Arrays.asList("foo", "bar"), null)); - calendars.add(new Calendar("cat calendar", Collections.singletonList("cat"), null)); + calendars.add(new Calendar("cat calendar", Collections.singletonList("cat"), null)); calendars.add(new Calendar("cat foo calendar", Arrays.asList("cat", "foo"), null)); indexCalendars(calendars); @@ -264,9 +278,9 @@ public void testGetCalandarByJobId() throws Exception { queryResult = getCalendars(CalendarQueryBuilder.builder().jobId("foo")); assertThat(queryResult, hasSize(3)); - Long matchedCount = queryResult.stream().filter( - c -> c.getId().equals("foo calendar") || c.getId().equals("foo bar calendar") || c.getId().equals("cat foo calendar")) - .count(); + Long matchedCount = queryResult.stream() + .filter(c -> c.getId().equals("foo calendar") || c.getId().equals("foo bar calendar") || c.getId().equals("cat foo calendar")) + .count(); assertEquals(Long.valueOf(3), matchedCount); queryResult = getCalendars(CalendarQueryBuilder.builder().jobId("bar")); @@ -279,20 +293,18 @@ public void testGetCalandarById() throws Exception { calendars.add(new Calendar("empty calendar", Collections.emptyList(), null)); calendars.add(new Calendar("foo calendar", Collections.singletonList("foo"), null)); calendars.add(new Calendar("foo bar calendar", Arrays.asList("foo", "bar"), null)); - calendars.add(new Calendar("cat calendar", Collections.singletonList("cat"), null)); + calendars.add(new Calendar("cat calendar", Collections.singletonList("cat"), null)); calendars.add(new Calendar("cat foo calendar", Arrays.asList("cat", "foo"), null)); indexCalendars(calendars); - List queryResult = getCalendars(CalendarQueryBuilder.builder() - .calendarIdTokens(new String[]{"foo*"}) - .sort(true)); + List queryResult = getCalendars(CalendarQueryBuilder.builder().calendarIdTokens(new String[] { "foo*" }).sort(true)); assertThat(queryResult, hasSize(2)); assertThat(queryResult.get(0).getId(), equalTo("foo bar calendar")); assertThat(queryResult.get(1).getId(), equalTo("foo calendar")); - queryResult = getCalendars(CalendarQueryBuilder.builder() - .calendarIdTokens(new String[]{"foo calendar", "cat calendar"}) - .sort(true)); + queryResult = getCalendars( + CalendarQueryBuilder.builder().calendarIdTokens(new String[] { "foo calendar", "cat calendar" }).sort(true) + ); assertThat(queryResult, hasSize(2)); assertThat(queryResult.get(0).getId(), equalTo("cat calendar")); assertThat(queryResult.get(1).getId(), equalTo("foo calendar")); @@ -303,21 +315,22 @@ public void testGetCalendarByIdAndPaging() throws Exception { calendars.add(new Calendar("empty calendar", Collections.emptyList(), null)); calendars.add(new Calendar("foo calendar", Collections.singletonList("foo"), null)); calendars.add(new Calendar("foo bar calendar", Arrays.asList("foo", "bar"), null)); - calendars.add(new Calendar("cat calendar", Collections.singletonList("cat"), null)); + calendars.add(new Calendar("cat calendar", Collections.singletonList("cat"), null)); calendars.add(new Calendar("cat foo calendar", Arrays.asList("cat", "foo"), null)); indexCalendars(calendars); - List queryResult = getCalendars(CalendarQueryBuilder.builder() - .calendarIdTokens(new String[]{"foo*"}) - .pageParams(new PageParams(0, 1)) - .sort(true)); + List queryResult = getCalendars( + CalendarQueryBuilder.builder().calendarIdTokens(new String[] { "foo*" }).pageParams(new PageParams(0, 1)).sort(true) + ); assertThat(queryResult, hasSize(1)); assertThat(queryResult.get(0).getId(), equalTo("foo bar calendar")); - queryResult = getCalendars(CalendarQueryBuilder.builder() - .calendarIdTokens(new String[]{"foo calendar", "cat calendar"}) - .sort(true) - .pageParams(new PageParams(1, 1))); + queryResult = getCalendars( + CalendarQueryBuilder.builder() + .calendarIdTokens(new String[] { "foo calendar", "cat calendar" }) + .sort(true) + .pageParams(new PageParams(1, 1)) + ); assertThat(queryResult, hasSize(1)); assertThat(queryResult.get(0).getId(), equalTo("foo calendar")); } @@ -353,18 +366,16 @@ public void testRemoveJobFromCalendar() throws Exception { calendars.add(new Calendar("empty calendar", Collections.emptyList(), null)); calendars.add(new Calendar("foo calendar", Collections.singletonList("foo"), null)); calendars.add(new Calendar("foo bar calendar", Arrays.asList("foo", "bar"), null)); - calendars.add(new Calendar("cat calendar", Collections.singletonList("cat"), null)); + calendars.add(new Calendar("cat calendar", Collections.singletonList("cat"), null)); calendars.add(new Calendar("cat foo calendar", Arrays.asList("cat", "foo"), null)); indexCalendars(calendars); CountDownLatch latch = new CountDownLatch(1); final AtomicReference exceptionHolder = new AtomicReference<>(); - jobProvider.removeJobFromCalendars("bar", ActionListener.wrap( - r -> latch.countDown(), - e -> { - exceptionHolder.set(e); - latch.countDown(); - })); + jobProvider.removeJobFromCalendars("bar", ActionListener.wrap(r -> latch.countDown(), e -> { + exceptionHolder.set(e); + latch.countDown(); + })); latch.await(); if (exceptionHolder.get() != null) { @@ -373,7 +384,7 @@ public void testRemoveJobFromCalendar() throws Exception { List updatedCalendars = getCalendars(CalendarQueryBuilder.builder()); assertEquals(5, updatedCalendars.size()); - for (Calendar cal: updatedCalendars) { + for (Calendar cal : updatedCalendars) { assertThat("bar", is(not(in(cal.getJobIds())))); } @@ -381,12 +392,10 @@ public void testRemoveJobFromCalendar() throws Exception { assertThat(catFoo.getJobIds(), contains("cat", "foo")); CountDownLatch latch2 = new CountDownLatch(1); - jobProvider.removeJobFromCalendars("cat", ActionListener.wrap( - r -> latch2.countDown(), - e -> { - exceptionHolder.set(e); - latch2.countDown(); - })); + jobProvider.removeJobFromCalendars("cat", ActionListener.wrap(r -> latch2.countDown(), e -> { + exceptionHolder.set(e); + latch2.countDown(); + })); latch2.await(); if (exceptionHolder.get() != null) { @@ -395,7 +404,7 @@ public void testRemoveJobFromCalendar() throws Exception { updatedCalendars = getCalendars(CalendarQueryBuilder.builder()); assertEquals(5, updatedCalendars.size()); - for (Calendar cal: updatedCalendars) { + for (Calendar cal : updatedCalendars) { assertThat("bar", is(not(in(cal.getJobIds())))); assertThat("cat", is(not(in(cal.getJobIds())))); } @@ -440,15 +449,13 @@ private List getCalendars(CalendarQueryBuilder query) throws Exception AtomicReference exceptionHolder = new AtomicReference<>(); AtomicReference> result = new AtomicReference<>(); - jobProvider.calendars(query, ActionListener.wrap( - r -> { - result.set(r); - latch.countDown(); - }, - e -> { - exceptionHolder.set(e); - latch.countDown(); - })); + jobProvider.calendars(query, ActionListener.wrap(r -> { + result.set(r); + latch.countDown(); + }, e -> { + exceptionHolder.set(e); + latch.countDown(); + })); latch.await(); if (exceptionHolder.get() != null) { @@ -458,16 +465,13 @@ private List getCalendars(CalendarQueryBuilder query) throws Exception return result.get().results(); } - private void updateCalendar(String calendarId, Set idsToAdd, Set idsToRemove, MlMetadata mlMetadata) - throws Exception { + private void updateCalendar(String calendarId, Set idsToAdd, Set idsToRemove, MlMetadata mlMetadata) throws Exception { CountDownLatch latch = new CountDownLatch(1); AtomicReference exceptionHolder = new AtomicReference<>(); - jobProvider.updateCalendar(calendarId, idsToAdd, idsToRemove, - r -> latch.countDown(), - e -> { - exceptionHolder.set(e); - latch.countDown(); - }); + jobProvider.updateCalendar(calendarId, idsToAdd, idsToRemove, r -> latch.countDown(), e -> { + exceptionHolder.set(e); + latch.countDown(); + }); latch.await(); if (exceptionHolder.get() != null) { @@ -482,23 +486,20 @@ private Calendar getCalendar(String calendarId) throws Exception { CountDownLatch latch = new CountDownLatch(1); AtomicReference exceptionHolder = new AtomicReference<>(); AtomicReference calendarHolder = new AtomicReference<>(); - jobProvider.calendar(calendarId, ActionListener.wrap( - c -> { - calendarHolder.set(c); - latch.countDown(); - }, - e -> { - exceptionHolder.set(e); - latch.countDown(); - }) - ); + jobProvider.calendar(calendarId, ActionListener.wrap(c -> { + calendarHolder.set(c); + latch.countDown(); + }, e -> { + exceptionHolder.set(e); + latch.countDown(); + })); latch.await(); if (exceptionHolder.get() != null) { throw exceptionHolder.get(); } - return calendarHolder.get(); + return calendarHolder.get(); } public void testScheduledEventsForJobs() throws Exception { @@ -580,22 +581,22 @@ public void testScheduledEvents() throws Exception { assertEquals(events.get(3), returnedEvents.get(2)); assertEquals(events.get(2), returnedEvents.get(3)); - returnedEvents = getScheduledEvents(ScheduledEventsQueryBuilder.builder().calendarIds(new String[]{"maintenance_a"})); + returnedEvents = getScheduledEvents(ScheduledEventsQueryBuilder.builder().calendarIds(new String[] { "maintenance_a" })); assertEquals(3, returnedEvents.size()); assertEquals(events.get(0), returnedEvents.get(0)); assertEquals(events.get(1), returnedEvents.get(1)); assertEquals(events.get(2), returnedEvents.get(2)); - returnedEvents = getScheduledEvents(ScheduledEventsQueryBuilder.builder() - .calendarIds(new String[]{"maintenance_a", "maintenance_a_and_b"})); + returnedEvents = getScheduledEvents( + ScheduledEventsQueryBuilder.builder().calendarIds(new String[] { "maintenance_a", "maintenance_a_and_b" }) + ); assertEquals(4, returnedEvents.size()); assertEquals(events.get(0), returnedEvents.get(0)); assertEquals(events.get(1), returnedEvents.get(1)); assertEquals(events.get(3), returnedEvents.get(2)); assertEquals(events.get(2), returnedEvents.get(3)); - returnedEvents = getScheduledEvents(ScheduledEventsQueryBuilder.builder() - .calendarIds(new String[]{"maintenance_a*"})); + returnedEvents = getScheduledEvents(ScheduledEventsQueryBuilder.builder().calendarIds(new String[] { "maintenance_a*" })); assertEquals(4, returnedEvents.size()); assertEquals(events.get(0), returnedEvents.get(0)); assertEquals(events.get(1), returnedEvents.get(1)); @@ -636,8 +637,7 @@ public void testScheduledEventsForJob_withGroup() throws Exception { } private ScheduledEvent buildScheduledEvent(String description, ZonedDateTime start, ZonedDateTime end, String calendarId) { - return new ScheduledEvent.Builder() - .description(description) + return new ScheduledEvent.Builder().description(description) .startTime(start.toInstant()) .endTime(end.toInstant()) .calendarId(calendarId) @@ -647,36 +647,48 @@ private ScheduledEvent buildScheduledEvent(String description, ZonedDateTime sta public void testGetSnapshots() { String jobId = "test_get_snapshots"; Job.Builder job = createJob(jobId); - indexModelSnapshot(new ModelSnapshot.Builder(jobId).setSnapshotId("snap_2") - .setTimestamp(Date.from(Instant.ofEpochMilli(10))) - .setMinVersion(Version.V_7_4_0) - .setQuantiles(new Quantiles(jobId, Date.from(Instant.ofEpochMilli(10)), randomAlphaOfLength(20))) - .build()); - indexModelSnapshot(new ModelSnapshot.Builder(jobId).setSnapshotId("snap_1") - .setTimestamp(Date.from(Instant.ofEpochMilli(11))) - .setMinVersion(Version.V_7_2_0) - .setQuantiles(new Quantiles(jobId, Date.from(Instant.ofEpochMilli(11)), randomAlphaOfLength(20))) - .build()); - indexModelSnapshot(new ModelSnapshot.Builder(jobId).setSnapshotId("other_snap") - .setTimestamp(Date.from(Instant.ofEpochMilli(12))) - .setMinVersion(Version.V_7_3_0) - .setQuantiles(new Quantiles(jobId, Date.from(Instant.ofEpochMilli(12)), randomAlphaOfLength(20))) - .build()); + indexModelSnapshot( + new ModelSnapshot.Builder(jobId).setSnapshotId("snap_2") + .setTimestamp(Date.from(Instant.ofEpochMilli(10))) + .setMinVersion(Version.V_7_4_0) + .setQuantiles(new Quantiles(jobId, Date.from(Instant.ofEpochMilli(10)), randomAlphaOfLength(20))) + .build() + ); + indexModelSnapshot( + new ModelSnapshot.Builder(jobId).setSnapshotId("snap_1") + .setTimestamp(Date.from(Instant.ofEpochMilli(11))) + .setMinVersion(Version.V_7_2_0) + .setQuantiles(new Quantiles(jobId, Date.from(Instant.ofEpochMilli(11)), randomAlphaOfLength(20))) + .build() + ); + indexModelSnapshot( + new ModelSnapshot.Builder(jobId).setSnapshotId("other_snap") + .setTimestamp(Date.from(Instant.ofEpochMilli(12))) + .setMinVersion(Version.V_7_3_0) + .setQuantiles(new Quantiles(jobId, Date.from(Instant.ofEpochMilli(12)), randomAlphaOfLength(20))) + .build() + ); createJob("other_job"); - indexModelSnapshot(new ModelSnapshot.Builder("other_job").setSnapshotId("other_snap") - .setTimestamp(Date.from(Instant.ofEpochMilli(10))) - .setMinVersion(Version.CURRENT) - .setQuantiles(new Quantiles("other_job", Date.from(Instant.ofEpochMilli(10)), randomAlphaOfLength(20))) - .build()); + indexModelSnapshot( + new ModelSnapshot.Builder("other_job").setSnapshotId("other_snap") + .setTimestamp(Date.from(Instant.ofEpochMilli(10))) + .setMinVersion(Version.CURRENT) + .setQuantiles(new Quantiles("other_job", Date.from(Instant.ofEpochMilli(10)), randomAlphaOfLength(20))) + .build() + ); // Add a snapshot WITHOUT a min version. client().prepareIndex(AnomalyDetectorsIndex.jobResultsAliasedName("other_job")) .setId(ModelSnapshot.documentId("other_job", "11")) - .setSource("{\"job_id\":\"other_job\"," + - "\"snapshot_id\":\"11\", \"snapshot_doc_count\":1,\"retain\":false}", XContentType.JSON) + .setSource( + "{\"job_id\":\"other_job\"," + "\"snapshot_id\":\"11\", \"snapshot_doc_count\":1,\"retain\":false}", + XContentType.JSON + ) .get(); - client().admin().indices().prepareRefresh(AnomalyDetectorsIndex.jobStateIndexPattern(), - AnomalyDetectorsIndex.jobResultsIndexPrefix() + "*").get(); + client().admin() + .indices() + .prepareRefresh(AnomalyDetectorsIndex.jobStateIndexPattern(), AnomalyDetectorsIndex.jobResultsIndexPrefix() + "*") + .get(); PlainActionFuture> future = new PlainActionFuture<>(); jobProvider.modelSnapshots(jobId, 0, 4, "9", "15", "", false, "snap_2,snap_1", future::onResponse, future::onFailure); @@ -709,16 +721,7 @@ public void testGetSnapshots() { assertThat(snapshots.get(2).getSnapshotId(), equalTo("other_snap")); future = new PlainActionFuture<>(); - jobProvider.modelSnapshots("*", - 0, - 5, - null, - null, - "min_version", - false, - null, - future::onResponse, - future::onFailure); + jobProvider.modelSnapshots("*", 0, 5, null, null, "min_version", false, null, future::onResponse, future::onFailure); snapshots = future.actionGet().results(); assertThat(snapshots.get(0).getSnapshotId(), equalTo("11")); assertThat(snapshots.get(1).getSnapshotId(), equalTo("snap_1")); @@ -776,9 +779,14 @@ public void testGetAutodetectParams() throws Exception { Quantiles quantiles = new Quantiles(jobId, new Date(), "quantile-state"); indexQuantiles(quantiles); - client().admin().indices().prepareRefresh(MlMetaIndex.indexName(), AnomalyDetectorsIndex.jobStateIndexPattern(), - AnomalyDetectorsIndex.jobResultsAliasedName(jobId)).get(); - + client().admin() + .indices() + .prepareRefresh( + MlMetaIndex.indexName(), + AnomalyDetectorsIndex.jobStateIndexPattern(), + AnomalyDetectorsIndex.jobResultsAliasedName(jobId) + ) + .get(); AutodetectParams params = getAutodetectParams(job.build(new Date())); @@ -833,12 +841,11 @@ private AutodetectParams getAutodetectParams(Job job) throws Exception { } private List getScheduledEventsForJob(String jobId, List jobGroups, ScheduledEventsQueryBuilder query) - throws Exception { + throws Exception { AtomicReference errorHolder = new AtomicReference<>(); AtomicReference> searchResultHolder = new AtomicReference<>(); CountDownLatch latch = new CountDownLatch(1); - jobProvider.scheduledEventsForJob(jobId, jobGroups, query, ActionListener.wrap( - params -> { + jobProvider.scheduledEventsForJob(jobId, jobGroups, query, ActionListener.wrap(params -> { searchResultHolder.set(params); latch.countDown(); }, e -> { @@ -858,14 +865,13 @@ private List getScheduledEvents(ScheduledEventsQueryBuilder quer AtomicReference errorHolder = new AtomicReference<>(); AtomicReference> searchResultHolder = new AtomicReference<>(); CountDownLatch latch = new CountDownLatch(1); - jobProvider.scheduledEvents(query, ActionListener.wrap( - params -> { - searchResultHolder.set(params); - latch.countDown(); - }, e -> { - errorHolder.set(e); - latch.countDown(); - })); + jobProvider.scheduledEvents(query, ActionListener.wrap(params -> { + searchResultHolder.set(params); + latch.countDown(); + }, e -> { + errorHolder.set(e); + latch.countDown(); + })); latch.await(); if (errorHolder.get() != null) { @@ -919,8 +925,9 @@ private void indexScheduledEvents(List events) throws IOExceptio for (ScheduledEvent event : events) { IndexRequest indexRequest = new IndexRequest(MlMetaIndex.indexName()); try (XContentBuilder builder = XContentFactory.jsonBuilder()) { - ToXContent.MapParams params = new ToXContent.MapParams(Collections.singletonMap( - ToXContentParams.FOR_INTERNAL_STORAGE, "true")); + ToXContent.MapParams params = new ToXContent.MapParams( + Collections.singletonMap(ToXContentParams.FOR_INTERNAL_STORAGE, "true") + ); indexRequest.source(event.toXContent(builder, params)); bulkRequest.add(indexRequest); } @@ -943,8 +950,9 @@ private void indexFilters(List filters) throws IOException { for (MlFilter filter : filters) { IndexRequest indexRequest = new IndexRequest(MlMetaIndex.indexName()).id(filter.documentId()); try (XContentBuilder builder = XContentFactory.jsonBuilder()) { - ToXContent.MapParams params = new ToXContent.MapParams(Collections.singletonMap( - ToXContentParams.FOR_INTERNAL_STORAGE, "true")); + ToXContent.MapParams params = new ToXContent.MapParams( + Collections.singletonMap(ToXContentParams.FOR_INTERNAL_STORAGE, "true") + ); indexRequest.source(filter.toXContent(builder, params)); bulkRequest.add(indexRequest); } @@ -953,24 +961,35 @@ private void indexFilters(List filters) throws IOException { } private void indexModelSizeStats(ModelSizeStats modelSizeStats) { - JobResultsPersister persister = - new JobResultsPersister(new OriginSettingClient(client(), ClientHelper.ML_ORIGIN), resultsPersisterService); + JobResultsPersister persister = new JobResultsPersister( + new OriginSettingClient(client(), ClientHelper.ML_ORIGIN), + resultsPersisterService + ); persister.persistModelSizeStats(modelSizeStats, () -> true); } private void indexModelSnapshot(ModelSnapshot snapshot) { - JobResultsPersister persister = - new JobResultsPersister(new OriginSettingClient(client(), ClientHelper.ML_ORIGIN), resultsPersisterService); + JobResultsPersister persister = new JobResultsPersister( + new OriginSettingClient(client(), ClientHelper.ML_ORIGIN), + resultsPersisterService + ); persister.persistModelSnapshot(snapshot, WriteRequest.RefreshPolicy.IMMEDIATE, () -> true); } private void indexQuantiles(Quantiles quantiles) { PlainActionFuture future = new PlainActionFuture<>(); - createStateIndexAndAliasIfNecessary(client(), ClusterState.EMPTY_STATE, TestIndexNameExpressionResolver.newInstance(), - MasterNodeRequest.DEFAULT_MASTER_NODE_TIMEOUT, future); + createStateIndexAndAliasIfNecessary( + client(), + ClusterState.EMPTY_STATE, + TestIndexNameExpressionResolver.newInstance(), + MasterNodeRequest.DEFAULT_MASTER_NODE_TIMEOUT, + future + ); future.actionGet(); - JobResultsPersister persister = - new JobResultsPersister(new OriginSettingClient(client(), ClientHelper.ML_ORIGIN), resultsPersisterService); + JobResultsPersister persister = new JobResultsPersister( + new OriginSettingClient(client(), ClientHelper.ML_ORIGIN), + resultsPersisterService + ); persister.persistQuantiles(quantiles, () -> true); } @@ -978,11 +997,12 @@ private void indexCalendars(List calendars) throws IOException { BulkRequestBuilder bulkRequest = client().prepareBulk(); bulkRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - for (Calendar calendar: calendars) { + for (Calendar calendar : calendars) { IndexRequest indexRequest = new IndexRequest(MlMetaIndex.indexName()).id(calendar.documentId()); try (XContentBuilder builder = XContentFactory.jsonBuilder()) { ToXContent.MapParams params = new ToXContent.MapParams( - Collections.singletonMap(ToXContentParams.FOR_INTERNAL_STORAGE, "true")); + Collections.singletonMap(ToXContentParams.FOR_INTERNAL_STORAGE, "true") + ); indexRequest.source(calendar.toXContent(builder, params)); bulkRequest.add(indexRequest); } diff --git a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/JobStorageDeletionTaskIT.java b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/JobStorageDeletionTaskIT.java index 9021a6bf20a64..63a9646fc1217 100644 --- a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/JobStorageDeletionTaskIT.java +++ b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/JobStorageDeletionTaskIT.java @@ -62,13 +62,19 @@ public class JobStorageDeletionTaskIT extends BaseMlIntegTestCase { public void createComponents() { Settings settings = nodeSettings(0, Settings.EMPTY); ThreadPool tp = mockThreadPool(); - ClusterSettings clusterSettings = new ClusterSettings(settings, - new HashSet<>(Arrays.asList(InferenceProcessor.MAX_INFERENCE_PROCESSORS, - MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING, - ResultsPersisterService.PERSIST_RESULTS_MAX_RETRIES, - OperationRouting.USE_ADAPTIVE_REPLICA_SELECTION_SETTING, - ClusterService.USER_DEFINED_METADATA, - ClusterApplierService.CLUSTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING))); + ClusterSettings clusterSettings = new ClusterSettings( + settings, + new HashSet<>( + Arrays.asList( + InferenceProcessor.MAX_INFERENCE_PROCESSORS, + MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING, + ResultsPersisterService.PERSIST_RESULTS_MAX_RETRIES, + OperationRouting.USE_ADAPTIVE_REPLICA_SELECTION_SETTING, + ClusterService.USER_DEFINED_METADATA, + ClusterApplierService.CLUSTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING + ) + ) + ); ClusterService clusterService = new ClusterService(settings, clusterSettings, tp); OriginSettingClient originSettingClient = new OriginSettingClient(client(), ClientHelper.ML_ORIGIN); ResultsPersisterService resultsPersisterService = new ResultsPersisterService(tp, originSettingClient, clusterService, settings); @@ -122,24 +128,26 @@ public void testDeleteDedicatedJobWithDataInShared() throws Exception { createBuckets(jobIdShared, 1, 10); // Manually switching over alias info - IndicesAliasesRequest aliasesRequest = new IndicesAliasesRequest() - .addAliasAction(IndicesAliasesRequest.AliasActions - .add() + IndicesAliasesRequest aliasesRequest = new IndicesAliasesRequest().addAliasAction( + IndicesAliasesRequest.AliasActions.add() .alias(AnomalyDetectorsIndex.jobResultsAliasedName(jobIdDedicated)) .isHidden(true) .index(AnomalyDetectorsIndex.jobResultsIndexPrefix() + "shared") .writeIndex(false) - .filter(QueryBuilders.boolQuery().filter(QueryBuilders.termQuery(Job.ID.getPreferredName(), jobIdDedicated)))) - .addAliasAction(IndicesAliasesRequest.AliasActions - .add() - .alias(AnomalyDetectorsIndex.resultsWriteAlias(jobIdDedicated)) - .index(AnomalyDetectorsIndex.jobResultsIndexPrefix() + "shared") - .isHidden(true) - .writeIndex(true)) - .addAliasAction(IndicesAliasesRequest.AliasActions - .remove() - .alias(AnomalyDetectorsIndex.resultsWriteAlias(jobIdDedicated)) - .index(dedicatedIndex)); + .filter(QueryBuilders.boolQuery().filter(QueryBuilders.termQuery(Job.ID.getPreferredName(), jobIdDedicated))) + ) + .addAliasAction( + IndicesAliasesRequest.AliasActions.add() + .alias(AnomalyDetectorsIndex.resultsWriteAlias(jobIdDedicated)) + .index(AnomalyDetectorsIndex.jobResultsIndexPrefix() + "shared") + .isHidden(true) + .writeIndex(true) + ) + .addAliasAction( + IndicesAliasesRequest.AliasActions.remove() + .alias(AnomalyDetectorsIndex.resultsWriteAlias(jobIdDedicated)) + .index(dedicatedIndex) + ); client().admin().indices().aliases(aliasesRequest).actionGet(); @@ -147,11 +155,17 @@ public void testDeleteDedicatedJobWithDataInShared() throws Exception { client().admin().indices().prepareRefresh(AnomalyDetectorsIndex.jobResultsIndexPrefix() + "*").get(); AtomicReference> bucketHandler = new AtomicReference<>(); AtomicReference failureHandler = new AtomicReference<>(); - blockingCall(listener -> jobResultsProvider.buckets(jobIdDedicated, - new BucketsQueryBuilder().from(0).size(22), - listener::onResponse, - listener::onFailure, - client()), bucketHandler, failureHandler); + blockingCall( + listener -> jobResultsProvider.buckets( + jobIdDedicated, + new BucketsQueryBuilder().from(0).size(22), + listener::onResponse, + listener::onFailure, + client() + ), + bucketHandler, + failureHandler + ); assertThat(failureHandler.get(), is(nullValue())); assertThat(bucketHandler.get().count(), equalTo(22L)); @@ -163,35 +177,48 @@ public void testDeleteDedicatedJobWithDataInShared() throws Exception { // Make sure our shared index job is OK bucketHandler = new AtomicReference<>(); failureHandler = new AtomicReference<>(); - blockingCall(listener -> jobResultsProvider.buckets(jobIdShared, - new BucketsQueryBuilder().from(0).size(21), - listener::onResponse, - listener::onFailure, - client()), bucketHandler, failureHandler); + blockingCall( + listener -> jobResultsProvider.buckets( + jobIdShared, + new BucketsQueryBuilder().from(0).size(21), + listener::onResponse, + listener::onFailure, + client() + ), + bucketHandler, + failureHandler + ); assertThat(failureHandler.get(), is(nullValue())); assertThat(bucketHandler.get().count(), equalTo(11L)); // Make sure dedicated index is gone - assertThat(client().admin() - .indices() - .prepareGetIndex() - .setIndices(dedicatedIndex) - .setIndicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN_CLOSED_HIDDEN) - .get() - .indices().length, equalTo(0)); + assertThat( + client().admin() + .indices() + .prepareGetIndex() + .setIndices(dedicatedIndex) + .setIndicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN_CLOSED_HIDDEN) + .get() + .indices().length, + equalTo(0) + ); // Make sure all results referencing the dedicated job are gone - assertThat(client().prepareSearch() - .setIndices(AnomalyDetectorsIndex.jobResultsIndexPrefix() + "*") - .setIndicesOptions(IndicesOptions.lenientExpandOpenHidden()) - .setTrackTotalHits(true) - .setSize(0) - .setSource(SearchSourceBuilder.searchSource() - .query(QueryBuilders.boolQuery().filter(QueryBuilders.termQuery(Job.ID.getPreferredName(), jobIdDedicated)))) - .get() - .getHits() - .getTotalHits() - .value, equalTo(0L)); + assertThat( + client().prepareSearch() + .setIndices(AnomalyDetectorsIndex.jobResultsIndexPrefix() + "*") + .setIndicesOptions(IndicesOptions.lenientExpandOpenHidden()) + .setTrackTotalHits(true) + .setSize(0) + .setSource( + SearchSourceBuilder.searchSource() + .query(QueryBuilders.boolQuery().filter(QueryBuilders.termQuery(Job.ID.getPreferredName(), jobIdDedicated))) + ) + .get() + .getHits() + .getTotalHits().value, + equalTo(0L) + ); } private void createBuckets(String jobId, int from, int count) { diff --git a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/MlAutoUpdateServiceIT.java b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/MlAutoUpdateServiceIT.java index 5830a35e0d36d..f3e0ea2cbd179 100644 --- a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/MlAutoUpdateServiceIT.java +++ b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/MlAutoUpdateServiceIT.java @@ -16,8 +16,8 @@ import org.elasticsearch.cluster.node.DiscoveryNodeRole; import org.elasticsearch.cluster.node.DiscoveryNodes; import org.elasticsearch.common.transport.TransportAddress; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.indices.TestIndexNameExpressionResolver; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.ml.MlConfigIndex; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfig; import org.elasticsearch.xpack.ml.MlAutoUpdateService; @@ -45,27 +45,27 @@ public void createComponents() throws Exception { waitForMlTemplates(); } - private static final String AGG_WITH_OLD_DATE_HISTOGRAM_INTERVAL = "{\n" + - " \"datafeed_id\": \"farequote-datafeed-with-old-agg\",\n" + - " \"job_id\": \"farequote\",\n" + - " \"frequency\": \"1h\",\n" + - " \"config_type\": \"datafeed\",\n" + - " \"indices\": [\"farequote1\", \"farequote2\"],\n" + - " \"aggregations\": {\n" + - " \"buckets\": {\n" + - " \"date_histogram\": {\n" + - " \"field\": \"time\",\n" + - " \"interval\": \"360s\",\n" + - " \"time_zone\": \"UTC\"\n" + - " },\n" + - " \"aggregations\": {\n" + - " \"time\": {\n" + - " \"max\": {\"field\": \"time\"}\n" + - " }\n" + - " }\n" + - " }\n" + - " }\n" + - "}"; + private static final String AGG_WITH_OLD_DATE_HISTOGRAM_INTERVAL = "{\n" + + " \"datafeed_id\": \"farequote-datafeed-with-old-agg\",\n" + + " \"job_id\": \"farequote\",\n" + + " \"frequency\": \"1h\",\n" + + " \"config_type\": \"datafeed\",\n" + + " \"indices\": [\"farequote1\", \"farequote2\"],\n" + + " \"aggregations\": {\n" + + " \"buckets\": {\n" + + " \"date_histogram\": {\n" + + " \"field\": \"time\",\n" + + " \"interval\": \"360s\",\n" + + " \"time_zone\": \"UTC\"\n" + + " },\n" + + " \"aggregations\": {\n" + + " \"time\": {\n" + + " \"max\": {\"field\": \"time\"}\n" + + " }\n" + + " }\n" + + " }\n" + + " }\n" + + "}"; public void testAutomaticModelUpdate() throws Exception { ensureGreen("_all"); @@ -77,30 +77,39 @@ public void testAutomaticModelUpdate() throws Exception { AtomicReference getConfigHolder = new AtomicReference<>(); AtomicReference exceptionHolder = new AtomicReference<>(); - blockingCall(listener -> datafeedConfigProvider.getDatafeedConfig("farequote-datafeed-with-old-agg", listener), + blockingCall( + listener -> datafeedConfigProvider.getDatafeedConfig("farequote-datafeed-with-old-agg", listener), getConfigHolder, - exceptionHolder); + exceptionHolder + ); assertThat(exceptionHolder.get(), is(nullValue())); client().admin().indices().prepareRefresh(MlConfigIndex.indexName()).get(); DatafeedConfigAutoUpdater autoUpdater = new DatafeedConfigAutoUpdater(datafeedConfigProvider, indexNameExpressionResolver); - MlAutoUpdateService mlAutoUpdateService = new MlAutoUpdateService(client().threadPool(), - Collections.singletonList(autoUpdater)); + MlAutoUpdateService mlAutoUpdateService = new MlAutoUpdateService(client().threadPool(), Collections.singletonList(autoUpdater)); - ClusterChangedEvent event = new ClusterChangedEvent("test", + ClusterChangedEvent event = new ClusterChangedEvent( + "test", ClusterState.builder(new ClusterName("test")) - .nodes(DiscoveryNodes.builder().add( - new DiscoveryNode("node_name", - "node_id", - new TransportAddress(InetAddress.getLoopbackAddress(), 9300), - Collections.emptyMap(), - Set.of(DiscoveryNodeRole.MASTER_ROLE), - Version.V_8_0_0)) - .localNodeId("node_id") - .masterNodeId("node_id") - .build()) + .nodes( + DiscoveryNodes.builder() + .add( + new DiscoveryNode( + "node_name", + "node_id", + new TransportAddress(InetAddress.getLoopbackAddress(), 9300), + Collections.emptyMap(), + Set.of(DiscoveryNodeRole.MASTER_ROLE), + Version.V_8_0_0 + ) + ) + .localNodeId("node_id") + .masterNodeId("node_id") + .build() + ) .build(), - ClusterState.builder(new ClusterName("test")).build()); + ClusterState.builder(new ClusterName("test")).build() + ); mlAutoUpdateService.clusterChanged(event); assertBusy(() -> { diff --git a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/MlConfigMigratorIT.java b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/MlConfigMigratorIT.java index b4efa6afd3307..023e50619e3e4 100644 --- a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/MlConfigMigratorIT.java +++ b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/MlConfigMigratorIT.java @@ -31,14 +31,14 @@ import org.elasticsearch.common.settings.ClusterSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.Index; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.indices.TestIndexNameExpressionResolver; import org.elasticsearch.test.ESIntegTestCase; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.ml.MlConfigIndex; import org.elasticsearch.xpack.core.ml.MlMetadata; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfig; @@ -83,8 +83,10 @@ public class MlConfigMigratorIT extends MlSingleNodeTestCase { @Before public void setUpTests() { clusterService = mock(ClusterService.class); - ClusterSettings clusterSettings = new ClusterSettings(nodeSettings(), new HashSet<>(Collections.singletonList( - MlConfigMigrationEligibilityCheck.ENABLE_CONFIG_MIGRATION))); + ClusterSettings clusterSettings = new ClusterSettings( + nodeSettings(), + new HashSet<>(Collections.singletonList(MlConfigMigrationEligibilityCheck.ENABLE_CONFIG_MIGRATION)) + ); Metadata metadata = mock(Metadata.class); SortedMap indicesMap = new TreeMap<>(); when(metadata.getIndicesLookup()).thenReturn(indicesMap); @@ -96,7 +98,7 @@ public void setUpTests() { public void testWriteConfigToIndex() throws InterruptedException { - final String indexJobId = "job-already-migrated"; + final String indexJobId = "job-already-migrated"; // Add a job to the index JobConfigProvider jobConfigProvider = new JobConfigProvider(client(), xContentRegistry()); Job indexJob = buildJobBuilder(indexJobId).build(); @@ -116,17 +118,18 @@ public void testWriteConfigToIndex() throws InterruptedException { // try to write foo and 'job-already-migrated' which does not have the custom setting field assertNull(indexJob.getCustomSettings()); - blockingCall(actionListener -> mlConfigMigrator.writeConfigToIndex(Collections.emptyList(), - Arrays.asList(indexJob, foo), actionListener), - failedIdsHolder, exceptionHolder); + blockingCall( + actionListener -> mlConfigMigrator.writeConfigToIndex(Collections.emptyList(), Arrays.asList(indexJob, foo), actionListener), + failedIdsHolder, + exceptionHolder + ); assertNull(exceptionHolder.get()); assertThat(failedIdsHolder.get(), empty()); // Check job foo has been indexed and job-already-migrated has been overwritten AtomicReference> jobsHolder = new AtomicReference<>(); - blockingCall(actionListener -> jobConfigProvider.expandJobs("*", true, false, actionListener), - jobsHolder, exceptionHolder); + blockingCall(actionListener -> jobConfigProvider.expandJobs("*", true, false, actionListener), jobsHolder, exceptionHolder); assertNull(exceptionHolder.get()); assertThat(jobsHolder.get(), hasSize(2)); @@ -153,17 +156,17 @@ public void testMigrateConfigs() throws InterruptedException, IOException { RoutingTable.Builder routingTable = RoutingTable.builder(); addMlConfigIndex(metadata, routingTable); ClusterState clusterState = ClusterState.builder(new ClusterName("_name")) - .metadata(metadata.putCustom(MlMetadata.TYPE, mlMetadata.build())) - .routingTable(routingTable.build()) - .build(); + .metadata(metadata.putCustom(MlMetadata.TYPE, mlMetadata.build())) + .routingTable(routingTable.build()) + .build(); when(clusterService.state()).thenReturn(clusterState); List customs = new ArrayList<>(); doAnswer(invocation -> { - ClusterStateUpdateTask listener = (ClusterStateUpdateTask) invocation.getArguments()[1]; - ClusterState result = listener.execute(clusterState); - customs.addAll(result.metadata().customs().values()); - listener.clusterStateProcessed("source", mock(ClusterState.class), mock(ClusterState.class)); - return null; + ClusterStateUpdateTask listener = (ClusterStateUpdateTask) invocation.getArguments()[1]; + ClusterState result = listener.execute(clusterState); + customs.addAll(result.metadata().customs().values()); + listener.clusterStateProcessed("source", mock(ClusterState.class), mock(ClusterState.class)); + return null; }).when(clusterService).submitStateUpdateTask(eq("remove-migrated-ml-configs"), any()); AtomicReference exceptionHolder = new AtomicReference<>(); @@ -172,8 +175,7 @@ public void testMigrateConfigs() throws InterruptedException, IOException { // do the migration MlConfigMigrator mlConfigMigrator = new MlConfigMigrator(nodeSettings(), client(), clusterService, expressionResolver); // the first time this is called mlmetadata will be snap-shotted - blockingCall(actionListener -> mlConfigMigrator.migrateConfigs(clusterState, actionListener), - responseHolder, exceptionHolder); + blockingCall(actionListener -> mlConfigMigrator.migrateConfigs(clusterState, actionListener), responseHolder, exceptionHolder); // Verify that we have custom values in the new cluster state and that none of them is null assertThat(customs.size(), greaterThan(0)); @@ -185,8 +187,7 @@ public void testMigrateConfigs() throws InterruptedException, IOException { // check the jobs have been migrated AtomicReference> jobsHolder = new AtomicReference<>(); JobConfigProvider jobConfigProvider = new JobConfigProvider(client(), xContentRegistry()); - blockingCall(actionListener -> jobConfigProvider.expandJobs("*", true, true, actionListener), - jobsHolder, exceptionHolder); + blockingCall(actionListener -> jobConfigProvider.expandJobs("*", true, true, actionListener), jobsHolder, exceptionHolder); assertNull(exceptionHolder.get()); assertThat(jobsHolder.get(), hasSize(2)); @@ -198,8 +199,11 @@ public void testMigrateConfigs() throws InterruptedException, IOException { // check datafeeds are migrated DatafeedConfigProvider datafeedConfigProvider = new DatafeedConfigProvider(client(), xContentRegistry()); AtomicReference> datafeedsHolder = new AtomicReference<>(); - blockingCall(actionListener -> datafeedConfigProvider.expandDatafeedConfigs("*", true, actionListener), - datafeedsHolder, exceptionHolder); + blockingCall( + actionListener -> datafeedConfigProvider.expandDatafeedConfigs("*", true, actionListener), + datafeedsHolder, + exceptionHolder + ); assertNull(exceptionHolder.get()); assertThat(datafeedsHolder.get(), hasSize(1)); @@ -222,15 +226,20 @@ public void testExistingSnapshotDoesNotBlockMigration() throws InterruptedExcept // index a doc with the same Id as the config snapshot PlainActionFuture future = PlainActionFuture.newFuture(); - AnomalyDetectorsIndex.createStateIndexAndAliasIfNecessary(client(), clusterService.state(), expressionResolver, - MasterNodeRequest.DEFAULT_MASTER_NODE_TIMEOUT, future); + AnomalyDetectorsIndex.createStateIndexAndAliasIfNecessary( + client(), + clusterService.state(), + expressionResolver, + MasterNodeRequest.DEFAULT_MASTER_NODE_TIMEOUT, + future + ); future.actionGet(); IndexRequest indexRequest = new IndexRequest(AnomalyDetectorsIndex.jobStateIndexWriteAlias()).id("ml-config") - .source(Collections.singletonMap("a_field", "a_value")) - .opType(DocWriteRequest.OpType.CREATE) - .setRequireAlias(true) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); + .source(Collections.singletonMap("a_field", "a_value")) + .opType(DocWriteRequest.OpType.CREATE) + .setRequireAlias(true) + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); client().index(indexRequest).actionGet(); @@ -247,8 +256,7 @@ public void testExistingSnapshotDoesNotBlockMigration() throws InterruptedExcept MlConfigMigrator mlConfigMigrator = new MlConfigMigrator(nodeSettings(), client(), clusterService, expressionResolver); // writing the snapshot should fail because the doc already exists // in which case the migration should continue - blockingCall(actionListener -> mlConfigMigrator.migrateConfigs(clusterState, actionListener), - responseHolder, exceptionHolder); + blockingCall(actionListener -> mlConfigMigrator.migrateConfigs(clusterState, actionListener), responseHolder, exceptionHolder); assertNull(exceptionHolder.get()); assertTrue(responseHolder.get()); @@ -256,8 +264,7 @@ public void testExistingSnapshotDoesNotBlockMigration() throws InterruptedExcept // check the jobs have been migrated AtomicReference> jobsHolder = new AtomicReference<>(); JobConfigProvider jobConfigProvider = new JobConfigProvider(client(), xContentRegistry()); - blockingCall(actionListener -> jobConfigProvider.expandJobs("*", true, true, actionListener), - jobsHolder, exceptionHolder); + blockingCall(actionListener -> jobConfigProvider.expandJobs("*", true, true, actionListener), jobsHolder, exceptionHolder); assertNull(exceptionHolder.get()); assertThat(jobsHolder.get(), hasSize(1)); @@ -284,9 +291,9 @@ public void testMigrateConfigs_GivenLargeNumberOfJobsAndDatafeeds() throws Inter RoutingTable.Builder routingTable = RoutingTable.builder(); addMlConfigIndex(metadata, routingTable); ClusterState clusterState = ClusterState.builder(new ClusterName("_name")) - .metadata(metadata.putCustom(MlMetadata.TYPE, mlMetadata.build())) - .routingTable(routingTable.build()) - .build(); + .metadata(metadata.putCustom(MlMetadata.TYPE, mlMetadata.build())) + .routingTable(routingTable.build()) + .build(); when(clusterService.state()).thenReturn(clusterState); doAnswer(invocation -> { @@ -300,8 +307,7 @@ public void testMigrateConfigs_GivenLargeNumberOfJobsAndDatafeeds() throws Inter // do the migration MlConfigMigrator mlConfigMigrator = new MlConfigMigrator(nodeSettings(), client(), clusterService, expressionResolver); - blockingCall(actionListener -> mlConfigMigrator.migrateConfigs(clusterState, actionListener), - responseHolder, exceptionHolder); + blockingCall(actionListener -> mlConfigMigrator.migrateConfigs(clusterState, actionListener), responseHolder, exceptionHolder); assertNull(exceptionHolder.get()); assertTrue(responseHolder.get()); @@ -309,8 +315,7 @@ public void testMigrateConfigs_GivenLargeNumberOfJobsAndDatafeeds() throws Inter // check the jobs have been migrated AtomicReference> jobsHolder = new AtomicReference<>(); JobConfigProvider jobConfigProvider = new JobConfigProvider(client(), xContentRegistry()); - blockingCall(actionListener -> jobConfigProvider.expandJobs("*", true, true, actionListener), - jobsHolder, exceptionHolder); + blockingCall(actionListener -> jobConfigProvider.expandJobs("*", true, true, actionListener), jobsHolder, exceptionHolder); assertNull(exceptionHolder.get()); assertThat(jobsHolder.get(), hasSize(jobCount)); @@ -318,8 +323,11 @@ public void testMigrateConfigs_GivenLargeNumberOfJobsAndDatafeeds() throws Inter // check datafeeds are migrated DatafeedConfigProvider datafeedConfigProvider = new DatafeedConfigProvider(client(), xContentRegistry()); AtomicReference> datafeedsHolder = new AtomicReference<>(); - blockingCall(actionListener -> datafeedConfigProvider.expandDatafeedConfigs("*", true, actionListener), - datafeedsHolder, exceptionHolder); + blockingCall( + actionListener -> datafeedConfigProvider.expandDatafeedConfigs("*", true, actionListener), + datafeedsHolder, + exceptionHolder + ); assertNull(exceptionHolder.get()); assertThat(datafeedsHolder.get(), hasSize(datafeedCount)); @@ -329,8 +337,7 @@ public void testMigrateConfigs_GivenNoJobsOrDatafeeds() throws InterruptedExcept // Add empty ML metadata MlMetadata.Builder mlMetadata = new MlMetadata.Builder(); ClusterState clusterState = ClusterState.builder(new ClusterName("_name")) - .metadata(Metadata.builder() - .putCustom(MlMetadata.TYPE, mlMetadata.build())) + .metadata(Metadata.builder().putCustom(MlMetadata.TYPE, mlMetadata.build())) .build(); AtomicReference exceptionHolder = new AtomicReference<>(); @@ -338,19 +345,21 @@ public void testMigrateConfigs_GivenNoJobsOrDatafeeds() throws InterruptedExcept // do the migration MlConfigMigrator mlConfigMigrator = new MlConfigMigrator(nodeSettings(), client(), clusterService, expressionResolver); - blockingCall(actionListener -> mlConfigMigrator.migrateConfigs(clusterState, actionListener), - responseHolder, exceptionHolder); + blockingCall(actionListener -> mlConfigMigrator.migrateConfigs(clusterState, actionListener), responseHolder, exceptionHolder); assertNull(exceptionHolder.get()); assertFalse(responseHolder.get()); } public void testMigrateConfigsWithoutTasks_GivenMigrationIsDisabled() throws InterruptedException { - Settings settings = Settings.builder().put(nodeSettings()) - .put(MlConfigMigrationEligibilityCheck.ENABLE_CONFIG_MIGRATION.getKey(), false) - .build(); - ClusterSettings clusterSettings = new ClusterSettings(settings, new HashSet<>(Collections.singletonList( - MlConfigMigrationEligibilityCheck.ENABLE_CONFIG_MIGRATION))); + Settings settings = Settings.builder() + .put(nodeSettings()) + .put(MlConfigMigrationEligibilityCheck.ENABLE_CONFIG_MIGRATION.getKey(), false) + .build(); + ClusterSettings clusterSettings = new ClusterSettings( + settings, + new HashSet<>(Collections.singletonList(MlConfigMigrationEligibilityCheck.ENABLE_CONFIG_MIGRATION)) + ); when(clusterService.getClusterSettings()).thenReturn(clusterSettings); // and jobs and datafeeds clusterstate @@ -362,17 +371,15 @@ public void testMigrateConfigsWithoutTasks_GivenMigrationIsDisabled() throws Int mlMetadata.putDatafeed(builder.build(), Collections.emptyMap(), xContentRegistry()); ClusterState clusterState = ClusterState.builder(new ClusterName("_name")) - .metadata(Metadata.builder() - .putCustom(MlMetadata.TYPE, mlMetadata.build())) - .build(); + .metadata(Metadata.builder().putCustom(MlMetadata.TYPE, mlMetadata.build())) + .build(); AtomicReference exceptionHolder = new AtomicReference<>(); AtomicReference responseHolder = new AtomicReference<>(); // do the migration MlConfigMigrator mlConfigMigrator = new MlConfigMigrator(settings, client(), clusterService, expressionResolver); - blockingCall(actionListener -> mlConfigMigrator.migrateConfigs(clusterState, actionListener), - responseHolder, exceptionHolder); + blockingCall(actionListener -> mlConfigMigrator.migrateConfigs(clusterState, actionListener), responseHolder, exceptionHolder); assertNull(exceptionHolder.get()); assertFalse(responseHolder.get()); @@ -380,16 +387,18 @@ public void testMigrateConfigsWithoutTasks_GivenMigrationIsDisabled() throws Int // check the jobs have not been migrated AtomicReference> jobsHolder = new AtomicReference<>(); JobConfigProvider jobConfigProvider = new JobConfigProvider(client(), xContentRegistry()); - blockingCall(actionListener -> jobConfigProvider.expandJobs("*", true, true, actionListener), - jobsHolder, exceptionHolder); + blockingCall(actionListener -> jobConfigProvider.expandJobs("*", true, true, actionListener), jobsHolder, exceptionHolder); assertNull(exceptionHolder.get()); assertThat(jobsHolder.get().isEmpty(), is(true)); // check datafeeds have not been migrated DatafeedConfigProvider datafeedConfigProvider = new DatafeedConfigProvider(client(), xContentRegistry()); AtomicReference> datafeedsHolder = new AtomicReference<>(); - blockingCall(actionListener -> datafeedConfigProvider.expandDatafeedConfigs("*", true, actionListener), - datafeedsHolder, exceptionHolder); + blockingCall( + actionListener -> datafeedConfigProvider.expandDatafeedConfigs("*", true, actionListener), + datafeedsHolder, + exceptionHolder + ); assertNull(exceptionHolder.get()); assertThat(datafeedsHolder.get().isEmpty(), is(true)); @@ -397,17 +406,18 @@ public void testMigrateConfigsWithoutTasks_GivenMigrationIsDisabled() throws Int public void assertSnapshot(MlMetadata expectedMlMetadata) throws IOException { client().admin().indices().prepareRefresh(AnomalyDetectorsIndex.jobStateIndexPattern()).get(); - SearchResponse searchResponse = client() - .prepareSearch(AnomalyDetectorsIndex.jobStateIndexPattern()) + SearchResponse searchResponse = client().prepareSearch(AnomalyDetectorsIndex.jobStateIndexPattern()) .setSize(1) .setQuery(QueryBuilders.idsQuery().addIds("ml-config")) .get(); assertThat(searchResponse.getHits().getHits().length, greaterThan(0)); - try (InputStream stream = searchResponse.getHits().getAt(0).getSourceRef().streamInput(); - XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(xContentRegistry(), LoggingDeprecationHandler.INSTANCE, stream)) { + try ( + InputStream stream = searchResponse.getHits().getAt(0).getSourceRef().streamInput(); + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(xContentRegistry(), LoggingDeprecationHandler.INSTANCE, stream) + ) { MlMetadata recoveredMeta = MlMetadata.LENIENT_PARSER.apply(parser, null).build(); assertEquals(expectedMlMetadata, recoveredMeta); } @@ -415,7 +425,8 @@ public void assertSnapshot(MlMetadata expectedMlMetadata) throws IOException { private void addMlConfigIndex(Metadata.Builder metadata, RoutingTable.Builder routingTable) { IndexMetadata.Builder indexMetadata = IndexMetadata.builder(MlConfigIndex.indexName()); - indexMetadata.settings(Settings.builder() + indexMetadata.settings( + Settings.builder() .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) @@ -423,12 +434,17 @@ private void addMlConfigIndex(Metadata.Builder metadata, RoutingTable.Builder ro metadata.put(indexMetadata); Index index = new Index(MlConfigIndex.indexName(), "_uuid"); ShardId shardId = new ShardId(index, 0); - ShardRouting shardRouting = ShardRouting.newUnassigned(shardId, true, RecoverySource.EmptyStoreRecoverySource.INSTANCE, - new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "")); + ShardRouting shardRouting = ShardRouting.newUnassigned( + shardId, + true, + RecoverySource.EmptyStoreRecoverySource.INSTANCE, + new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "") + ); shardRouting = shardRouting.initialize("node_id", null, 0L); shardRouting = shardRouting.moveToStarted(); - routingTable.add(IndexRoutingTable.builder(index) - .addIndexShard(new IndexShardRoutingTable.Builder(shardId).addShard(shardRouting).build())); + routingTable.add( + IndexRoutingTable.builder(index).addIndexShard(new IndexShardRoutingTable.Builder(shardId).addShard(shardRouting).build()) + ); } public void testConfigIndexIsCreated() throws Exception { @@ -437,8 +453,8 @@ public void testConfigIndexIsCreated() throws Exception { mlMetadata.putJob(buildJobBuilder("job-foo").build(), false); ClusterState clusterState = ClusterState.builder(new ClusterName("_name")) - .metadata(Metadata.builder().putCustom(MlMetadata.TYPE, mlMetadata.build())) - .build(); + .metadata(Metadata.builder().putCustom(MlMetadata.TYPE, mlMetadata.build())) + .build(); AtomicReference exceptionHolder = new AtomicReference<>(); AtomicReference responseHolder = new AtomicReference<>(); @@ -446,8 +462,7 @@ public void testConfigIndexIsCreated() throws Exception { // if the cluster state has a job config and the index does not // exist it should be created - blockingCall(actionListener -> mlConfigMigrator.migrateConfigs(clusterState, actionListener), - responseHolder, exceptionHolder); + blockingCall(actionListener -> mlConfigMigrator.migrateConfigs(clusterState, actionListener), responseHolder, exceptionHolder); assertBusy(() -> assertTrue(configIndexExists())); } @@ -456,5 +471,3 @@ private boolean configIndexExists() { return ESIntegTestCase.indexExists(MlConfigIndex.indexName(), client()); } } - - diff --git a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/MlDistributedFailureIT.java b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/MlDistributedFailureIT.java index 987addad31e65..0f234cc7a3599 100644 --- a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/MlDistributedFailureIT.java +++ b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/MlDistributedFailureIT.java @@ -21,14 +21,7 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeValue; -import org.elasticsearch.xcontent.DeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.core.CheckedRunnable; import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.query.QueryBuilders; @@ -38,6 +31,13 @@ import org.elasticsearch.persistent.PersistentTasksCustomMetadata; import org.elasticsearch.persistent.PersistentTasksCustomMetadata.PersistentTask; import org.elasticsearch.persistent.UpdatePersistentTaskStatusAction; +import org.elasticsearch.xcontent.DeprecationHandler; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.action.util.QueryPage; import org.elasticsearch.xpack.core.ml.MlTasks; import org.elasticsearch.xpack.core.ml.action.CloseJobAction; @@ -92,7 +92,8 @@ public class MlDistributedFailureIT extends BaseMlIntegTestCase { @Override protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { - return Settings.builder().put(super.nodeSettings(nodeOrdinal, otherSettings)) + return Settings.builder() + .put(super.nodeSettings(nodeOrdinal, otherSettings)) .put(MachineLearning.CONCURRENT_JOB_ALLOCATIONS.getKey(), 4) .build(); } @@ -101,7 +102,7 @@ public void testFailOver() throws Exception { internalCluster().ensureAtLeastNumDataNodes(3); ensureStableCluster(); run("fail-over-job", () -> { - GetJobsStatsAction.Request request = new GetJobsStatsAction.Request("fail-over-job"); + GetJobsStatsAction.Request request = new GetJobsStatsAction.Request("fail-over-job"); GetJobsStatsAction.Response response = client().execute(GetJobsStatsAction.INSTANCE, request).actionGet(); DiscoveryNode discoveryNode = response.getResponse().results().get(0).getNode(); internalCluster().stopRandomNode(settings -> discoveryNode.getName().equals(settings.get("node.name"))); @@ -114,9 +115,8 @@ public void setLogging() { client().admin() .cluster() .prepareUpdateSettings() - .setPersistentSettings(Settings.builder() - .put("logger.org.elasticsearch.xpack.ml.utils.persistence", "TRACE") - .build()).get(); + .setPersistentSettings(Settings.builder().put("logger.org.elasticsearch.xpack.ml.utils.persistence", "TRACE").build()) + .get(); } @After @@ -124,9 +124,8 @@ public void unsetLogging() { client().admin() .cluster() .prepareUpdateSettings() - .setPersistentSettings(Settings.builder() - .putNull("logger.org.elasticsearch.xpack.ml.utils.persistence") - .build()).get(); + .setPersistentSettings(Settings.builder().putNull("logger.org.elasticsearch.xpack.ml.utils.persistence").build()) + .get(); } public void testLoseDedicatedMasterNode() throws Exception { @@ -141,15 +140,11 @@ public void testLoseDedicatedMasterNode() throws Exception { Settings masterDataPathSettings = internalCluster().dataPathSettings(internalCluster().getMasterName()); internalCluster().stopCurrentMasterNode(); assertBusy(() -> { - ClusterState state = client(mlAndDataNode).admin().cluster().prepareState() - .setLocal(true).get().getState(); + ClusterState state = client(mlAndDataNode).admin().cluster().prepareState().setLocal(true).get().getState(); assertNull(state.nodes().getMasterNodeId()); }); logger.info("Restarting dedicated master node"); - internalCluster().startNode(Settings.builder() - .put(masterDataPathSettings) - .put(masterOnlyNode()) - .build()); + internalCluster().startNode(Settings.builder().put(masterDataPathSettings).put(masterOnlyNode()).build()); ensureStableCluster(); }); } @@ -174,9 +169,7 @@ public void testCloseUnassignedJobAndDatafeed() throws Exception { ensureStableCluster(); // index some datafeed data - client().admin().indices().prepareCreate("data") - .setMapping("time", "type=date") - .get(); + client().admin().indices().prepareCreate("data").setMapping("time", "type=date").get(); long numDocs1 = randomIntBetween(32, 2048); long now = System.currentTimeMillis(); long weekAgo = now - 604800000; @@ -199,8 +192,8 @@ public void testCloseUnassignedJobAndDatafeed() throws Exception { assertEquals(JobState.OPENED, jobStatsResponse.getResponse().results().get(0).getState()); GetDatafeedsStatsAction.Request datafeedStatsRequest = new GetDatafeedsStatsAction.Request(datafeedId); - GetDatafeedsStatsAction.Response datafeedStatsResponse = - client().execute(GetDatafeedsStatsAction.INSTANCE, datafeedStatsRequest).actionGet(); + GetDatafeedsStatsAction.Response datafeedStatsResponse = client().execute(GetDatafeedsStatsAction.INSTANCE, datafeedStatsRequest) + .actionGet(); assertEquals(DatafeedState.STARTED, datafeedStatsResponse.getResponse().results().get(0).getDatafeedState()); // An unassigned datafeed can be stopped either normally or by force @@ -251,9 +244,7 @@ public void testCloseUnassignedFailedJobAndStopUnassignedStoppingDatafeed() thro ensureStableCluster(); // index some datafeed data - client().admin().indices().prepareCreate("data") - .setMapping("time", "type=date") - .get(); + client().admin().indices().prepareCreate("data").setMapping("time", "type=date").get(); long numDocs1 = randomIntBetween(32, 2048); long now = System.currentTimeMillis(); long weekAgo = now - 604800000; @@ -279,7 +270,9 @@ public void testCloseUnassignedFailedJobAndStopUnassignedStoppingDatafeed() thro // (remember it's not a real native process in these internal cluster tests). PostDataAction.Request postDataRequest = new PostDataAction.Request(jobId); postDataRequest.setContent( - new BytesArray("{ \"time\" : \"" + BlackHoleAutodetectProcess.MAGIC_FAILURE_VALUE_AS_DATE + "\" }"), XContentType.JSON); + new BytesArray("{ \"time\" : \"" + BlackHoleAutodetectProcess.MAGIC_FAILURE_VALUE_AS_DATE + "\" }"), + XContentType.JSON + ); PostDataAction.Response postDataResponse = client().execute(PostDataAction.INSTANCE, postDataRequest).actionGet(); assertEquals(1L, postDataResponse.getDataCounts().getInputRecordCount()); @@ -291,8 +284,8 @@ public void testCloseUnassignedFailedJobAndStopUnassignedStoppingDatafeed() thro }); // It's impossible to reliably get the datafeed into a stopping state at the point when the ML node is removed from the cluster - // using externally accessible actions. The only way this situation could occur in reality is through extremely unfortunate - // timing. Therefore, to simulate this unfortunate timing we cheat and access internal classes to set the datafeed state to + // using externally accessible actions. The only way this situation could occur in reality is through extremely unfortunate + // timing. Therefore, to simulate this unfortunate timing we cheat and access internal classes to set the datafeed state to // stopping. PersistentTasksCustomMetadata tasks = clusterService().state().getMetadata().custom(PersistentTasksCustomMetadata.TYPE); PersistentTasksCustomMetadata.PersistentTask task = MlTasks.getDatafeedTask(datafeedId, tasks); @@ -305,25 +298,34 @@ public void testCloseUnassignedFailedJobAndStopUnassignedStoppingDatafeed() thro CloseJobAction.Request closeJobRequest = new CloseJobAction.Request(jobId); closeJobRequest.setForce(true); client().execute(CloseJobAction.INSTANCE, closeJobRequest).actionGet(); - assumeFalse("The datafeed task is null most likely because the datafeed detected the job had failed. " + - "This is expected to happen extremely rarely but the test cannot continue in these circumstances.", task == null); + assumeFalse( + "The datafeed task is null most likely because the datafeed detected the job had failed. " + + "This is expected to happen extremely rarely but the test cannot continue in these circumstances.", + task == null + ); } - UpdatePersistentTaskStatusAction.Request updatePersistentTaskStatusRequest = - new UpdatePersistentTaskStatusAction.Request(task.getId(), task.getAllocationId(), DatafeedState.STOPPING); - PersistentTaskResponse updatePersistentTaskStatusResponse = - client().execute(UpdatePersistentTaskStatusAction.INSTANCE, updatePersistentTaskStatusRequest).actionGet(); + UpdatePersistentTaskStatusAction.Request updatePersistentTaskStatusRequest = new UpdatePersistentTaskStatusAction.Request( + task.getId(), + task.getAllocationId(), + DatafeedState.STOPPING + ); + PersistentTaskResponse updatePersistentTaskStatusResponse = client().execute( + UpdatePersistentTaskStatusAction.INSTANCE, + updatePersistentTaskStatusRequest + ).actionGet(); assertNotNull(updatePersistentTaskStatusResponse.getTask()); // Confirm the datafeed state is now stopping - this may take a while to update in cluster state assertBusy(() -> { GetDatafeedsStatsAction.Request datafeedStatsRequest = new GetDatafeedsStatsAction.Request(datafeedId); - GetDatafeedsStatsAction.Response datafeedStatsResponse = - client().execute(GetDatafeedsStatsAction.INSTANCE, datafeedStatsRequest).actionGet(); + GetDatafeedsStatsAction.Response datafeedStatsResponse = client().execute( + GetDatafeedsStatsAction.INSTANCE, + datafeedStatsRequest + ).actionGet(); assertEquals(DatafeedState.STOPPING, datafeedStatsResponse.getResponse().results().get(0).getDatafeedState()); }); - // Stop the node running the failed job/stopping datafeed ensureGreen(); // replicas must be assigned, otherwise we could lose a whole index internalCluster().stopRandomNode(settings -> jobNode.getName().equals(settings.get("node.name"))); @@ -338,8 +340,8 @@ public void testCloseUnassignedFailedJobAndStopUnassignedStoppingDatafeed() thro // Confirm the datafeed state is now stopped - shouldn't need a busy check here as // the stop endpoint shouldn't return until its effects are externally visible GetDatafeedsStatsAction.Request datafeedStatsRequest2 = new GetDatafeedsStatsAction.Request(datafeedId); - GetDatafeedsStatsAction.Response datafeedStatsResponse2 = - client().execute(GetDatafeedsStatsAction.INSTANCE, datafeedStatsRequest2).actionGet(); + GetDatafeedsStatsAction.Response datafeedStatsResponse2 = client().execute(GetDatafeedsStatsAction.INSTANCE, datafeedStatsRequest2) + .actionGet(); assertEquals(DatafeedState.STOPPED, datafeedStatsResponse2.getResponse().results().get(0).getDatafeedState()); // We should be allowed to force stop the unassigned failed job @@ -358,9 +360,7 @@ public void testStopAndForceStopDatafeed() throws Exception { ensureStableCluster(); // index some datafeed data - client().admin().indices().prepareCreate("data") - .setMapping("time", "type=date") - .get(); + client().admin().indices().prepareCreate("data").setMapping("time", "type=date").get(); long numDocs1 = randomIntBetween(32, 2048); long now = System.currentTimeMillis(); long weekAgo = now - 604800000; @@ -373,14 +373,16 @@ public void testStopAndForceStopDatafeed() throws Exception { waitForJobToHaveProcessedExactly(jobId, numDocs1); GetDatafeedsStatsAction.Request datafeedStatsRequest = new GetDatafeedsStatsAction.Request(datafeedId); - GetDatafeedsStatsAction.Response datafeedStatsResponse = - client().execute(GetDatafeedsStatsAction.INSTANCE, datafeedStatsRequest).actionGet(); + GetDatafeedsStatsAction.Response datafeedStatsResponse = client().execute(GetDatafeedsStatsAction.INSTANCE, datafeedStatsRequest) + .actionGet(); assertEquals(DatafeedState.STARTED, datafeedStatsResponse.getResponse().results().get(0).getDatafeedState()); // Stop the datafeed normally StopDatafeedAction.Request stopDatafeedRequest = new StopDatafeedAction.Request(datafeedId); - ActionFuture normalStopActionFuture - = client().execute(StopDatafeedAction.INSTANCE, stopDatafeedRequest); + ActionFuture normalStopActionFuture = client().execute( + StopDatafeedAction.INSTANCE, + stopDatafeedRequest + ); // Force stop the datafeed without waiting for the normal stop to return first stopDatafeedRequest = new StopDatafeedAction.Request(datafeedId); @@ -402,14 +404,14 @@ public void testJobRelocationIsMemoryAware() throws Exception { internalCluster().ensureAtLeastNumDataNodes(1); ensureStableCluster(); - // Open 4 small jobs. Since there is only 1 node in the cluster they'll have to go on that node. + // Open 4 small jobs. Since there is only 1 node in the cluster they'll have to go on that node. setupJobWithoutDatafeed("small1", ByteSizeValue.ofMb(2)); setupJobWithoutDatafeed("small2", ByteSizeValue.ofMb(2)); setupJobWithoutDatafeed("small3", ByteSizeValue.ofMb(2)); setupJobWithoutDatafeed("small4", ByteSizeValue.ofMb(2)); - // Expand the cluster to 3 nodes. The 4 small jobs will stay on the + // Expand the cluster to 3 nodes. The 4 small jobs will stay on the // same node because we don't rebalance jobs that are happily running. internalCluster().ensureAtLeastNumDataNodes(3); @@ -419,7 +421,7 @@ public void testJobRelocationIsMemoryAware() throws Exception { ensureGreen(); - // Open a big job. This should go on a different node to the 4 small ones. + // Open a big job. This should go on a different node to the 4 small ones. setupJobWithoutDatafeed("big1", ByteSizeValue.ofMb(500)); @@ -428,8 +430,10 @@ public void testJobRelocationIsMemoryAware() throws Exception { internalCluster().stopCurrentMasterNode(); ensureStableCluster(); - PersistentTasksClusterService persistentTasksClusterService = - internalCluster().getInstance(PersistentTasksClusterService.class, internalCluster().getMasterName()); + PersistentTasksClusterService persistentTasksClusterService = internalCluster().getInstance( + PersistentTasksClusterService.class, + internalCluster().getMasterName() + ); // Speed up rechecks to a rate that is quicker than what settings would allow. // The tests would work eventually without doing this, but the assertBusy() below // would need to wait 30 seconds, which would make the suite run very slowly. @@ -439,21 +443,29 @@ public void testJobRelocationIsMemoryAware() throws Exception { persistentTasksClusterService.setRecheckInterval(TimeValue.timeValueMillis(200)); // If memory requirements are used to reallocate the 4 small jobs (as we expect) then they should - // all reallocate to the same node, that being the one that doesn't have the big job on. If job counts + // all reallocate to the same node, that being the one that doesn't have the big job on. If job counts // are used to reallocate the small jobs then this implies the fallback allocation mechanism has been // used in a situation we don't want it to be used in, and at least one of the small jobs will be on - // the same node as the big job. (This all relies on xpack.ml.node_concurrent_job_allocations being set + // the same node as the big job. (This all relies on xpack.ml.node_concurrent_job_allocations being set // to at least 4, which we do in the nodeSettings() method.) assertBusy(() -> { - GetJobsStatsAction.Response statsResponse = - client().execute(GetJobsStatsAction.INSTANCE, new GetJobsStatsAction.Request(Metadata.ALL)).actionGet(); + GetJobsStatsAction.Response statsResponse = client().execute( + GetJobsStatsAction.INSTANCE, + new GetJobsStatsAction.Request(Metadata.ALL) + ).actionGet(); QueryPage jobStats = statsResponse.getResponse(); assertNotNull(jobStats); - List smallJobNodes = jobStats.results().stream().filter(s -> s.getJobId().startsWith("small") && s.getNode() != null) - .map(s -> s.getNode().getName()).collect(Collectors.toList()); - List bigJobNodes = jobStats.results().stream().filter(s -> s.getJobId().startsWith("big") && s.getNode() != null) - .map(s -> s.getNode().getName()).collect(Collectors.toList()); + List smallJobNodes = jobStats.results() + .stream() + .filter(s -> s.getJobId().startsWith("small") && s.getNode() != null) + .map(s -> s.getNode().getName()) + .collect(Collectors.toList()); + List bigJobNodes = jobStats.results() + .stream() + .filter(s -> s.getJobId().startsWith("big") && s.getNode() != null) + .map(s -> s.getNode().getName()) + .collect(Collectors.toList()); logger.info("small job nodes: " + smallJobNodes + ", big job nodes: " + bigJobNodes); assertEquals(5, jobStats.count()); assertEquals(4, smallJobNodes.size()); @@ -475,9 +487,7 @@ public void testClusterWithTwoMlNodes_RunsDatafeed_GivenOriginalNodeGoesDown() t ensureStableCluster(); // index some datafeed data - client().admin().indices().prepareCreate("data") - .setMapping("time", "type=date") - .get(); + client().admin().indices().prepareCreate("data").setMapping("time", "type=date").get(); long numDocs = 80000; long now = System.currentTimeMillis(); long weekAgo = now - 604800000; @@ -498,13 +508,19 @@ public void testClusterWithTwoMlNodes_RunsDatafeed_GivenOriginalNodeGoesDown() t client().execute(OpenJobAction.INSTANCE, new OpenJobAction.Request(job.getId())); assertBusy(() -> { - GetJobsStatsAction.Response statsResponse = - client().execute(GetJobsStatsAction.INSTANCE, new GetJobsStatsAction.Request(job.getId())).actionGet(); + GetJobsStatsAction.Response statsResponse = client().execute( + GetJobsStatsAction.INSTANCE, + new GetJobsStatsAction.Request(job.getId()) + ).actionGet(); assertEquals(JobState.OPENED, statsResponse.getResponse().results().get(0).getState()); }, 30, TimeUnit.SECONDS); DiscoveryNode nodeRunningJob = client().execute(GetJobsStatsAction.INSTANCE, new GetJobsStatsAction.Request(job.getId())) - .actionGet().getResponse().results().get(0).getNode(); + .actionGet() + .getResponse() + .results() + .get(0) + .getNode(); setMlIndicesDelayedNodeLeftTimeoutToZero(); @@ -536,8 +552,10 @@ private void setupJobWithoutDatafeed(String jobId, ByteSizeValue modelMemoryLimi client().execute(OpenJobAction.INSTANCE, new OpenJobAction.Request(job.getId())).actionGet(); assertBusy(() -> { - GetJobsStatsAction.Response statsResponse = - client().execute(GetJobsStatsAction.INSTANCE, new GetJobsStatsAction.Request(job.getId())).actionGet(); + GetJobsStatsAction.Response statsResponse = client().execute( + GetJobsStatsAction.INSTANCE, + new GetJobsStatsAction.Request(job.getId()) + ).actionGet(); assertEquals(JobState.OPENED, statsResponse.getResponse().results().get(0).getState()); }); } @@ -553,8 +571,10 @@ private void setupJobAndDatafeed(String jobId, String datafeedId, TimeValue data client().execute(OpenJobAction.INSTANCE, new OpenJobAction.Request(job.getId())); assertBusy(() -> { - GetJobsStatsAction.Response statsResponse = - client().execute(GetJobsStatsAction.INSTANCE, new GetJobsStatsAction.Request(job.getId())).actionGet(); + GetJobsStatsAction.Response statsResponse = client().execute( + GetJobsStatsAction.INSTANCE, + new GetJobsStatsAction.Request(job.getId()) + ).actionGet(); assertEquals(JobState.OPENED, statsResponse.getResponse().results().get(0).getState()); }, 30, TimeUnit.SECONDS); @@ -565,9 +585,7 @@ private void setupJobAndDatafeed(String jobId, String datafeedId, TimeValue data } private void run(String jobId, CheckedRunnable disrupt) throws Exception { - client().admin().indices().prepareCreate("data") - .setMapping("time", "type=date") - .get(); + client().admin().indices().prepareCreate("data").setMapping("time", "type=date").get(); long numDocs1 = randomIntBetween(32, 2048); long now = System.currentTimeMillis(); long weekAgo = now - 604800000; @@ -586,8 +604,10 @@ private void run(String jobId, CheckedRunnable disrupt) throws Except disrupt.run(); - PersistentTasksClusterService persistentTasksClusterService = - internalCluster().getInstance(PersistentTasksClusterService.class, internalCluster().getMasterName()); + PersistentTasksClusterService persistentTasksClusterService = internalCluster().getInstance( + PersistentTasksClusterService.class, + internalCluster().getMasterName() + ); // Speed up rechecks to a rate that is quicker than what settings would allow. // The tests would work eventually without doing this, but the assertBusy() below // would need to wait 30 seconds, which would make the suite run very slowly. @@ -610,14 +630,16 @@ private void run(String jobId, CheckedRunnable disrupt) throws Except } GetJobsStatsAction.Request jobStatsRequest = new GetJobsStatsAction.Request(jobId); - JobStats jobStats = client().execute(GetJobsStatsAction.INSTANCE, jobStatsRequest).actionGet() - .getResponse().results().get(0); + JobStats jobStats = client().execute(GetJobsStatsAction.INSTANCE, jobStatsRequest).actionGet().getResponse().results().get(0); assertEquals(JobState.OPENED, jobStats.getState()); assertNotNull(jobStats.getNode()); GetDatafeedsStatsAction.Request datafeedStatsRequest = new GetDatafeedsStatsAction.Request("data_feed_id"); - DatafeedStats datafeedStats = client().execute(GetDatafeedsStatsAction.INSTANCE, datafeedStatsRequest).actionGet() - .getResponse().results().get(0); + DatafeedStats datafeedStats = client().execute(GetDatafeedsStatsAction.INSTANCE, datafeedStatsRequest) + .actionGet() + .getResponse() + .results() + .get(0); assertEquals(DatafeedState.STARTED, datafeedStats.getDatafeedState()); assertNotNull(datafeedStats.getNode()); }, 20, TimeUnit.SECONDS); @@ -634,16 +656,22 @@ private void run(String jobId, CheckedRunnable disrupt) throws Except // are what we expect them to be: private static DataCounts getDataCountsFromIndex(String jobId) { SearchResponse searchResponse = client().prepareSearch() - .setIndicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN_CLOSED_HIDDEN) - .setQuery(QueryBuilders.idsQuery().addIds(DataCounts.documentId(jobId))) - .get(); + .setIndicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN_CLOSED_HIDDEN) + .setQuery(QueryBuilders.idsQuery().addIds(DataCounts.documentId(jobId))) + .get(); if (searchResponse.getHits().getTotalHits().value != 1) { return new DataCounts(jobId); } BytesReference source = searchResponse.getHits().getHits()[0].getSourceRef(); - try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, source, XContentType.JSON)) { + try ( + XContentParser parser = XContentHelper.createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + source, + XContentType.JSON + ) + ) { return DataCounts.PARSER.apply(parser, null); } catch (IOException e) { throw new RuntimeException(e); @@ -681,8 +709,7 @@ private void indexModelSnapshotFromCurrentJobStats(String jobId) throws IOExcept JobStats jobStats = getJobStats(jobId); DataCounts dataCounts = jobStats.getDataCounts(); - ModelSnapshot modelSnapshot = new ModelSnapshot.Builder(jobId) - .setLatestResultTimeStamp(dataCounts.getLatestRecordTimeStamp()) + ModelSnapshot modelSnapshot = new ModelSnapshot.Builder(jobId).setLatestResultTimeStamp(dataCounts.getLatestRecordTimeStamp()) .setLatestRecordTimeStamp(dataCounts.getLatestRecordTimeStamp()) .setMinVersion(Version.CURRENT) .setSnapshotId(jobId + "_mock_snapshot") diff --git a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/MlFiltersIT.java b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/MlFiltersIT.java index ef193671eba11..c95c44fcb0a85 100644 --- a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/MlFiltersIT.java +++ b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/MlFiltersIT.java @@ -26,7 +26,8 @@ public void testGetFilters_ShouldReturnUpTo100ByDefault() { int filtersCount = randomIntBetween(11, 100); for (int i = 0; i < filtersCount; i++) { PutFilterAction.Request putFilterRequest = new PutFilterAction.Request( - MlFilter.builder("filter-" + i).setItems("item-" + i).build()); + MlFilter.builder("filter-" + i).setItems("item-" + i).build() + ); client().execute(PutFilterAction.INSTANCE, putFilterRequest).actionGet(); } diff --git a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/MlNodeShutdownIT.java b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/MlNodeShutdownIT.java index b16064f639b1c..9f46ed0579a06 100644 --- a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/MlNodeShutdownIT.java +++ b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/MlNodeShutdownIT.java @@ -42,7 +42,7 @@ public void testJobsVacateShuttingDownNode() throws Exception { // Index some source data for the datafeeds. createSourceData(); - // Open 6 jobs. Since there are 3 nodes in the cluster we should get 2 jobs per node. + // Open 6 jobs. Since there are 3 nodes in the cluster we should get 2 jobs per node. setupJobAndDatafeed("shutdown-job-1", ByteSizeValue.ofMb(2)); setupJobAndDatafeed("shutdown-job-2", ByteSizeValue.ofMb(2)); setupJobAndDatafeed("shutdown-job-3", ByteSizeValue.ofMb(2)); @@ -50,27 +50,42 @@ public void testJobsVacateShuttingDownNode() throws Exception { setupJobAndDatafeed("shutdown-job-5", ByteSizeValue.ofMb(2)); setupJobAndDatafeed("shutdown-job-6", ByteSizeValue.ofMb(2)); - // Choose a node to shut down. Choose a non-master node most of the time, as ML nodes in Cloud + // Choose a node to shut down. Choose a non-master node most of the time, as ML nodes in Cloud // will never be master, and Cloud is where the node shutdown API will primarily be used. - String nodeNameToShutdown = rarely() ? internalCluster().getMasterName() : Arrays.stream(internalCluster().getNodeNames()) - .filter(nodeName -> internalCluster().getMasterName().equals(nodeName) == false).findFirst().get(); + String nodeNameToShutdown = rarely() + ? internalCluster().getMasterName() + : Arrays.stream(internalCluster().getNodeNames()) + .filter(nodeName -> internalCluster().getMasterName().equals(nodeName) == false) + .findFirst() + .get(); SetOnce nodeIdToShutdown = new SetOnce<>(); // Wait for the desired initial state of 2 jobs running on each node. assertBusy(() -> { - GetJobsStatsAction.Response statsResponse = - client().execute(GetJobsStatsAction.INSTANCE, new GetJobsStatsAction.Request(Metadata.ALL)).actionGet(); + GetJobsStatsAction.Response statsResponse = client().execute( + GetJobsStatsAction.INSTANCE, + new GetJobsStatsAction.Request(Metadata.ALL) + ).actionGet(); QueryPage jobStats = statsResponse.getResponse(); assertThat(jobStats, notNullValue()); - long numJobsOnNodeToShutdown = jobStats.results().stream() - .filter(stats -> stats.getNode() != null && nodeNameToShutdown.equals(stats.getNode().getName())).count(); - long numJobsOnOtherNodes = jobStats.results().stream() - .filter(stats -> stats.getNode() != null && nodeNameToShutdown.equals(stats.getNode().getName()) == false).count(); + long numJobsOnNodeToShutdown = jobStats.results() + .stream() + .filter(stats -> stats.getNode() != null && nodeNameToShutdown.equals(stats.getNode().getName())) + .count(); + long numJobsOnOtherNodes = jobStats.results() + .stream() + .filter(stats -> stats.getNode() != null && nodeNameToShutdown.equals(stats.getNode().getName()) == false) + .count(); assertThat(numJobsOnNodeToShutdown, is(2L)); assertThat(numJobsOnOtherNodes, is(4L)); - nodeIdToShutdown.set(jobStats.results().stream() - .filter(stats -> stats.getNode() != null && nodeNameToShutdown.equals(stats.getNode().getName())) - .map(stats -> stats.getNode().getId()).findFirst().get()); + nodeIdToShutdown.set( + jobStats.results() + .stream() + .filter(stats -> stats.getNode() != null && nodeNameToShutdown.equals(stats.getNode().getName())) + .map(stats -> stats.getNode().getId()) + .findFirst() + .get() + ); }); // Call the shutdown API for the chosen node. @@ -78,24 +93,25 @@ public void testJobsVacateShuttingDownNode() throws Exception { final String targetNodeName = type == SingleNodeShutdownMetadata.Type.REPLACE ? randomAlphaOfLengthBetween(10, 20) : null; client().execute( PutShutdownNodeAction.INSTANCE, - new PutShutdownNodeAction.Request( - nodeIdToShutdown.get(), - type, - "just testing", - null, - targetNodeName) + new PutShutdownNodeAction.Request(nodeIdToShutdown.get(), type, "just testing", null, targetNodeName) ).actionGet(); // Wait for the desired end state of all 6 jobs running on nodes that are not shutting down. assertBusy(() -> { - GetJobsStatsAction.Response statsResponse = - client().execute(GetJobsStatsAction.INSTANCE, new GetJobsStatsAction.Request(Metadata.ALL)).actionGet(); + GetJobsStatsAction.Response statsResponse = client().execute( + GetJobsStatsAction.INSTANCE, + new GetJobsStatsAction.Request(Metadata.ALL) + ).actionGet(); QueryPage jobStats = statsResponse.getResponse(); assertThat(jobStats, notNullValue()); - long numJobsOnNodeToShutdown = jobStats.results().stream() - .filter(stats -> stats.getNode() != null && nodeNameToShutdown.equals(stats.getNode().getName())).count(); - long numJobsOnOtherNodes = jobStats.results().stream() - .filter(stats -> stats.getNode() != null && nodeNameToShutdown.equals(stats.getNode().getName()) == false).count(); + long numJobsOnNodeToShutdown = jobStats.results() + .stream() + .filter(stats -> stats.getNode() != null && nodeNameToShutdown.equals(stats.getNode().getName())) + .count(); + long numJobsOnOtherNodes = jobStats.results() + .stream() + .filter(stats -> stats.getNode() != null && nodeNameToShutdown.equals(stats.getNode().getName()) == false) + .count(); assertThat(numJobsOnNodeToShutdown, is(0L)); assertThat(numJobsOnOtherNodes, is(6L)); }, 30, TimeUnit.SECONDS); @@ -109,7 +125,7 @@ public void testCloseJobVacatingShuttingDownNode() throws Exception { // Index some source data for the datafeeds. createSourceData(); - // Open 6 jobs. Since there are 3 nodes in the cluster we should get 2 jobs per node. + // Open 6 jobs. Since there are 3 nodes in the cluster we should get 2 jobs per node. setupJobAndDatafeed("shutdown-close-job-1", ByteSizeValue.ofMb(2)); setupJobAndDatafeed("shutdown-close-job-2", ByteSizeValue.ofMb(2)); setupJobAndDatafeed("shutdown-close-job-3", ByteSizeValue.ofMb(2)); @@ -120,29 +136,49 @@ public void testCloseJobVacatingShuttingDownNode() throws Exception { // Choose a node to shut down, and one job on that node to close after the shutdown request has been sent. // Choose a non-master node most of the time, as ML nodes in Cloud will never be master, and Cloud is where // the node shutdown API will primarily be used. - String nodeNameToShutdown = rarely() ? internalCluster().getMasterName() : Arrays.stream(internalCluster().getNodeNames()) - .filter(nodeName -> internalCluster().getMasterName().equals(nodeName) == false).findFirst().get(); + String nodeNameToShutdown = rarely() + ? internalCluster().getMasterName() + : Arrays.stream(internalCluster().getNodeNames()) + .filter(nodeName -> internalCluster().getMasterName().equals(nodeName) == false) + .findFirst() + .get(); SetOnce nodeIdToShutdown = new SetOnce<>(); SetOnce jobIdToClose = new SetOnce<>(); // Wait for the desired initial state of 2 jobs running on each node. assertBusy(() -> { - GetJobsStatsAction.Response statsResponse = - client().execute(GetJobsStatsAction.INSTANCE, new GetJobsStatsAction.Request(Metadata.ALL)).actionGet(); + GetJobsStatsAction.Response statsResponse = client().execute( + GetJobsStatsAction.INSTANCE, + new GetJobsStatsAction.Request(Metadata.ALL) + ).actionGet(); QueryPage jobStats = statsResponse.getResponse(); assertThat(jobStats, notNullValue()); - long numJobsOnNodeToShutdown = jobStats.results().stream() - .filter(stats -> stats.getNode() != null && nodeNameToShutdown.equals(stats.getNode().getName())).count(); - long numJobsOnOtherNodes = jobStats.results().stream() - .filter(stats -> stats.getNode() != null && nodeNameToShutdown.equals(stats.getNode().getName()) == false).count(); + long numJobsOnNodeToShutdown = jobStats.results() + .stream() + .filter(stats -> stats.getNode() != null && nodeNameToShutdown.equals(stats.getNode().getName())) + .count(); + long numJobsOnOtherNodes = jobStats.results() + .stream() + .filter(stats -> stats.getNode() != null && nodeNameToShutdown.equals(stats.getNode().getName()) == false) + .count(); assertThat(numJobsOnNodeToShutdown, is(2L)); assertThat(numJobsOnOtherNodes, is(4L)); - nodeIdToShutdown.set(jobStats.results().stream() - .filter(stats -> stats.getNode() != null && nodeNameToShutdown.equals(stats.getNode().getName())) - .map(stats -> stats.getNode().getId()).findFirst().get()); - jobIdToClose.set(jobStats.results().stream() - .filter(stats -> stats.getNode() != null && nodeNameToShutdown.equals(stats.getNode().getName())) - .map(GetJobsStatsAction.Response.JobStats::getJobId).findAny().get()); + nodeIdToShutdown.set( + jobStats.results() + .stream() + .filter(stats -> stats.getNode() != null && nodeNameToShutdown.equals(stats.getNode().getName())) + .map(stats -> stats.getNode().getId()) + .findFirst() + .get() + ); + jobIdToClose.set( + jobStats.results() + .stream() + .filter(stats -> stats.getNode() != null && nodeNameToShutdown.equals(stats.getNode().getName())) + .map(GetJobsStatsAction.Response.JobStats::getJobId) + .findAny() + .get() + ); }); // Call the shutdown API for the chosen node. @@ -150,13 +186,8 @@ public void testCloseJobVacatingShuttingDownNode() throws Exception { final String targetNodeName = type == SingleNodeShutdownMetadata.Type.REPLACE ? randomAlphaOfLengthBetween(10, 20) : null; client().execute( PutShutdownNodeAction.INSTANCE, - new PutShutdownNodeAction.Request( - nodeIdToShutdown.get(), type, - "just testing", - null, - targetNodeName) - ) - .actionGet(); + new PutShutdownNodeAction.Request(nodeIdToShutdown.get(), type, "just testing", null, targetNodeName) + ).actionGet(); if (randomBoolean()) { // This isn't waiting for something to happen - just adding timing variation @@ -166,30 +197,36 @@ public void testCloseJobVacatingShuttingDownNode() throws Exception { // There are several different scenarios for this request: // 1. It might arrive at the original node that is shutting down before the job has transitioned into the - // vacating state. Then it's just a normal close that node shut down should not interfere with. + // vacating state. Then it's just a normal close that node shut down should not interfere with. // 2. It might arrive at the original node that is shutting down while the job is vacating, but early enough - // that the vacate can be promoted to a close (since the early part of the work they do is the same). + // that the vacate can be promoted to a close (since the early part of the work they do is the same). // 3. It might arrive at the original node that is shutting down while the job is vacating, but too late - // to promote the vacate to a close (since the request to unassign the persistent task has already been - // sent to the master node). In this case fallback code in the job task should delete the persistent - // task to effectively force-close the job on its new node. + // to promote the vacate to a close (since the request to unassign the persistent task has already been + // sent to the master node). In this case fallback code in the job task should delete the persistent + // task to effectively force-close the job on its new node. // 4. It might arrive after the job has been unassigned from its original node after vacating but before it's - // been assigned to a new node. In this case the close job action will delete the persistent task. - // 5. It might arrive after the job has been assigned to its new node. In this case it's just a normal close - // on a node that isn't even shutting down. + // been assigned to a new node. In this case the close job action will delete the persistent task. + // 5. It might arrive after the job has been assigned to its new node. In this case it's just a normal close + // on a node that isn't even shutting down. client().execute(CloseJobAction.INSTANCE, new CloseJobAction.Request(jobIdToClose.get())).actionGet(); // Wait for the desired end state of the 5 jobs that were not closed running on nodes that are not shutting // down, and the closed job not running anywhere. assertBusy(() -> { - GetJobsStatsAction.Response statsResponse = - client().execute(GetJobsStatsAction.INSTANCE, new GetJobsStatsAction.Request(Metadata.ALL)).actionGet(); + GetJobsStatsAction.Response statsResponse = client().execute( + GetJobsStatsAction.INSTANCE, + new GetJobsStatsAction.Request(Metadata.ALL) + ).actionGet(); QueryPage jobStats = statsResponse.getResponse(); assertThat(jobStats, notNullValue()); - long numJobsOnNodeToShutdown = jobStats.results().stream() - .filter(stats -> stats.getNode() != null && nodeNameToShutdown.equals(stats.getNode().getName())).count(); - long numJobsOnOtherNodes = jobStats.results().stream() - .filter(stats -> stats.getNode() != null && nodeNameToShutdown.equals(stats.getNode().getName()) == false).count(); + long numJobsOnNodeToShutdown = jobStats.results() + .stream() + .filter(stats -> stats.getNode() != null && nodeNameToShutdown.equals(stats.getNode().getName())) + .count(); + long numJobsOnOtherNodes = jobStats.results() + .stream() + .filter(stats -> stats.getNode() != null && nodeNameToShutdown.equals(stats.getNode().getName()) == false) + .count(); assertThat(numJobsOnNodeToShutdown, is(0L)); assertThat(numJobsOnOtherNodes, is(5L)); // 5 rather than 6 because we closed one }, 30, TimeUnit.SECONDS); @@ -207,8 +244,10 @@ private void setupJobAndDatafeed(String jobId, ByteSizeValue modelMemoryLimit) t client().execute(OpenJobAction.INSTANCE, new OpenJobAction.Request(job.getId())); assertBusy(() -> { - GetJobsStatsAction.Response statsResponse = - client().execute(GetJobsStatsAction.INSTANCE, new GetJobsStatsAction.Request(job.getId())).actionGet(); + GetJobsStatsAction.Response statsResponse = client().execute( + GetJobsStatsAction.INSTANCE, + new GetJobsStatsAction.Request(job.getId()) + ).actionGet(); assertEquals(JobState.OPENED, statsResponse.getResponse().results().get(0).getState()); }, 30, TimeUnit.SECONDS); @@ -221,9 +260,7 @@ private void ensureStableCluster() { } private void createSourceData() { - client().admin().indices().prepareCreate("data") - .setMapping("time", "type=date") - .get(); + client().admin().indices().prepareCreate("data").setMapping("time", "type=date").get(); long numDocs = randomIntBetween(50, 100); long now = System.currentTimeMillis(); long weekAgo = now - 604800000; diff --git a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/ModelInferenceActionIT.java b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/ModelInferenceActionIT.java index f85b87961e9af..df1ee1c4575c3 100644 --- a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/ModelInferenceActionIT.java +++ b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/ModelInferenceActionIT.java @@ -9,12 +9,14 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.Version; import org.elasticsearch.license.License; +import org.elasticsearch.xpack.core.ml.action.InternalInferModelAction; import org.elasticsearch.xpack.core.ml.inference.TrainedModelConfig; import org.elasticsearch.xpack.core.ml.inference.TrainedModelDefinition; import org.elasticsearch.xpack.core.ml.inference.TrainedModelDefinitionTests; import org.elasticsearch.xpack.core.ml.inference.TrainedModelInput; import org.elasticsearch.xpack.core.ml.inference.TrainedModelType; import org.elasticsearch.xpack.core.ml.inference.preprocessing.OneHotEncoding; +import org.elasticsearch.xpack.core.ml.inference.results.ClassificationInferenceResults; import org.elasticsearch.xpack.core.ml.inference.results.InferenceResults; import org.elasticsearch.xpack.core.ml.inference.results.SingleValueInferenceResults; import org.elasticsearch.xpack.core.ml.inference.results.WarningInferenceResults; @@ -28,8 +30,6 @@ import org.elasticsearch.xpack.core.ml.inference.trainedmodel.tree.TreeNode; import org.elasticsearch.xpack.core.ml.job.messages.Messages; import org.elasticsearch.xpack.ml.MlSingleNodeTestCase; -import org.elasticsearch.xpack.core.ml.inference.results.ClassificationInferenceResults; -import org.elasticsearch.xpack.core.ml.action.InternalInferModelAction; import org.elasticsearch.xpack.ml.inference.persistence.TrainedModelProvider; import org.junit.Before; @@ -69,22 +69,28 @@ public void testInferModels() throws Exception { Map oneHotEncoding = new HashMap<>(); oneHotEncoding.put("cat", "animal_cat"); oneHotEncoding.put("dog", "animal_dog"); - TrainedModelConfig config1 = buildTrainedModelConfigBuilder(modelId2) - .setInput(new TrainedModelInput(Arrays.asList("field.foo", "field.bar", "other.categorical"))) - .setParsedDefinition(new TrainedModelDefinition.Builder() - .setPreProcessors(Arrays.asList(new OneHotEncoding("other.categorical", oneHotEncoding, false))) - .setTrainedModel(buildClassification(true))) + TrainedModelConfig config1 = buildTrainedModelConfigBuilder(modelId2).setInput( + new TrainedModelInput(Arrays.asList("field.foo", "field.bar", "other.categorical")) + ) + .setParsedDefinition( + new TrainedModelDefinition.Builder().setPreProcessors( + Arrays.asList(new OneHotEncoding("other.categorical", oneHotEncoding, false)) + ).setTrainedModel(buildClassification(true)) + ) .setVersion(Version.CURRENT) .setLicenseLevel(License.OperationMode.PLATINUM.description()) .setCreateTime(Instant.now()) .setEstimatedOperations(0) .setEstimatedHeapMemory(0) .build(); - TrainedModelConfig config2 = buildTrainedModelConfigBuilder(modelId1) - .setInput(new TrainedModelInput(Arrays.asList("field.foo", "field.bar", "other.categorical"))) - .setParsedDefinition(new TrainedModelDefinition.Builder() - .setPreProcessors(Arrays.asList(new OneHotEncoding("other.categorical", oneHotEncoding, false))) - .setTrainedModel(buildRegression())) + TrainedModelConfig config2 = buildTrainedModelConfigBuilder(modelId1).setInput( + new TrainedModelInput(Arrays.asList("field.foo", "field.bar", "other.categorical")) + ) + .setParsedDefinition( + new TrainedModelDefinition.Builder().setPreProcessors( + Arrays.asList(new OneHotEncoding("other.categorical", oneHotEncoding, false)) + ).setTrainedModel(buildRegression()) + ) .setVersion(Version.CURRENT) .setEstimatedOperations(0) .setEstimatedHeapMemory(0) @@ -100,95 +106,129 @@ public void testInferModels() throws Exception { assertThat(putConfigHolder.get(), is(true)); assertThat(exceptionHolder.get(), is(nullValue())); - List> toInfer = new ArrayList<>(); - toInfer.add(new HashMap<>() {{ - put("field", new HashMap<>(){{ - put("foo", 1.0); - put("bar", 0.5); - }}); - put("other", new HashMap<>(){{ - put("categorical", "dog"); - }}); - }}); - toInfer.add(new HashMap<>() {{ - put("field", new HashMap<>(){{ - put("foo", 0.9); - put("bar", 1.5); - }}); - put("other", new HashMap<>(){{ - put("categorical", "cat"); - }}); - }}); + toInfer.add(new HashMap<>() { + { + put("field", new HashMap<>() { + { + put("foo", 1.0); + put("bar", 0.5); + } + }); + put("other", new HashMap<>() { + { + put("categorical", "dog"); + } + }); + } + }); + toInfer.add(new HashMap<>() { + { + put("field", new HashMap<>() { + { + put("foo", 0.9); + put("bar", 1.5); + } + }); + put("other", new HashMap<>() { + { + put("categorical", "cat"); + } + }); + } + }); List> toInfer2 = new ArrayList<>(); - toInfer2.add(new HashMap<>() {{ - put("field", new HashMap<>(){{ - put("foo", 0.0); - put("bar", 0.01); - }}); - put("other", new HashMap<>(){{ - put("categorical", "dog"); - }}); - }}); - toInfer2.add(new HashMap<>() {{ - put("field", new HashMap<>(){{ - put("foo", 1.0); - put("bar", 0.0); - }}); - put("other", new HashMap<>(){{ - put("categorical", "cat"); - }}); - }}); + toInfer2.add(new HashMap<>() { + { + put("field", new HashMap<>() { + { + put("foo", 0.0); + put("bar", 0.01); + } + }); + put("other", new HashMap<>() { + { + put("categorical", "dog"); + } + }); + } + }); + toInfer2.add(new HashMap<>() { + { + put("field", new HashMap<>() { + { + put("foo", 1.0); + put("bar", 0.0); + } + }); + put("other", new HashMap<>() { + { + put("categorical", "cat"); + } + }); + } + }); // Test regression - InternalInferModelAction.Request request = new InternalInferModelAction.Request(modelId1, + InternalInferModelAction.Request request = new InternalInferModelAction.Request( + modelId1, toInfer, RegressionConfigUpdate.EMPTY_PARAMS, - true); + true + ); InternalInferModelAction.Response response = client().execute(InternalInferModelAction.INSTANCE, request).actionGet(); - assertThat(response.getInferenceResults().stream().map(i -> ((SingleValueInferenceResults)i).value()).collect(Collectors.toList()), - contains(1.3, 1.25)); + assertThat( + response.getInferenceResults().stream().map(i -> ((SingleValueInferenceResults) i).value()).collect(Collectors.toList()), + contains(1.3, 1.25) + ); request = new InternalInferModelAction.Request(modelId1, toInfer2, RegressionConfigUpdate.EMPTY_PARAMS, true); response = client().execute(InternalInferModelAction.INSTANCE, request).actionGet(); - assertThat(response.getInferenceResults().stream().map(i -> ((SingleValueInferenceResults)i).value()).collect(Collectors.toList()), - contains(1.65, 1.55)); - + assertThat( + response.getInferenceResults().stream().map(i -> ((SingleValueInferenceResults) i).value()).collect(Collectors.toList()), + contains(1.65, 1.55) + ); // Test classification request = new InternalInferModelAction.Request(modelId2, toInfer, ClassificationConfigUpdate.EMPTY_PARAMS, true); response = client().execute(InternalInferModelAction.INSTANCE, request).actionGet(); - assertThat(response.getInferenceResults() + assertThat( + response.getInferenceResults() .stream() - .map(i -> ((SingleValueInferenceResults)i).valueAsString()) + .map(i -> ((SingleValueInferenceResults) i).valueAsString()) .collect(Collectors.toList()), - contains("no", "yes")); + contains("no", "yes") + ); // Get top classes request = new InternalInferModelAction.Request(modelId2, toInfer, new ClassificationConfigUpdate(2, null, null, null, null), true); response = client().execute(InternalInferModelAction.INSTANCE, request).actionGet(); - ClassificationInferenceResults classificationInferenceResults = - (ClassificationInferenceResults)response.getInferenceResults().get(0); + ClassificationInferenceResults classificationInferenceResults = (ClassificationInferenceResults) response.getInferenceResults() + .get(0); assertThat(classificationInferenceResults.getTopClasses().get(0).getClassification(), equalTo("no")); assertThat(classificationInferenceResults.getTopClasses().get(1).getClassification(), equalTo("yes")); - assertThat(classificationInferenceResults.getTopClasses().get(0).getProbability(), - greaterThan(classificationInferenceResults.getTopClasses().get(1).getProbability())); + assertThat( + classificationInferenceResults.getTopClasses().get(0).getProbability(), + greaterThan(classificationInferenceResults.getTopClasses().get(1).getProbability()) + ); - classificationInferenceResults = (ClassificationInferenceResults)response.getInferenceResults().get(1); + classificationInferenceResults = (ClassificationInferenceResults) response.getInferenceResults().get(1); assertThat(classificationInferenceResults.getTopClasses().get(0).getClassification(), equalTo("yes")); assertThat(classificationInferenceResults.getTopClasses().get(1).getClassification(), equalTo("no")); // they should always be in order of Most probable to least - assertThat(classificationInferenceResults.getTopClasses().get(0).getProbability(), - greaterThan(classificationInferenceResults.getTopClasses().get(1).getProbability())); + assertThat( + classificationInferenceResults.getTopClasses().get(0).getProbability(), + greaterThan(classificationInferenceResults.getTopClasses().get(1).getProbability()) + ); // Test that top classes restrict the number returned request = new InternalInferModelAction.Request(modelId2, toInfer2, new ClassificationConfigUpdate(1, null, null, null, null), true); response = client().execute(InternalInferModelAction.INSTANCE, request).actionGet(); - classificationInferenceResults = (ClassificationInferenceResults)response.getInferenceResults().get(0); + classificationInferenceResults = (ClassificationInferenceResults) response.getInferenceResults().get(0); assertThat(classificationInferenceResults.getTopClasses(), hasSize(1)); assertThat(classificationInferenceResults.getTopClasses().get(0).getClassification(), equalTo("yes")); } @@ -198,11 +238,14 @@ public void testInferModelMultiClassModel() throws Exception { Map oneHotEncoding = new HashMap<>(); oneHotEncoding.put("cat", "animal_cat"); oneHotEncoding.put("dog", "animal_dog"); - TrainedModelConfig config = buildTrainedModelConfigBuilder(modelId) - .setInput(new TrainedModelInput(Arrays.asList("field.foo", "field.bar", "other.categorical"))) - .setParsedDefinition(new TrainedModelDefinition.Builder() - .setPreProcessors(Arrays.asList(new OneHotEncoding("other.categorical", oneHotEncoding, false))) - .setTrainedModel(buildMultiClassClassification())) + TrainedModelConfig config = buildTrainedModelConfigBuilder(modelId).setInput( + new TrainedModelInput(Arrays.asList("field.foo", "field.bar", "other.categorical")) + ) + .setParsedDefinition( + new TrainedModelDefinition.Builder().setPreProcessors( + Arrays.asList(new OneHotEncoding("other.categorical", oneHotEncoding, false)) + ).setTrainedModel(buildMultiClassClassification()) + ) .setVersion(Version.CURRENT) .setLicenseLevel(License.OperationMode.PLATINUM.description()) .setCreateTime(Instant.now()) @@ -216,93 +259,121 @@ public void testInferModelMultiClassModel() throws Exception { assertThat(putConfigHolder.get(), is(true)); assertThat(exceptionHolder.get(), is(nullValue())); - List> toInfer = new ArrayList<>(); - toInfer.add(new HashMap<>() {{ - put("field", new HashMap<>(){{ - put("foo", 1.0); - put("bar", 0.5); - }}); - put("other", new HashMap<>(){{ - put("categorical", "dog"); - }}); - }}); - toInfer.add(new HashMap<>() {{ - put("field", new HashMap<>(){{ - put("foo", 0.9); - put("bar", 1.5); - }}); - put("other", new HashMap<>(){{ - put("categorical", "cat"); - }}); - }}); + toInfer.add(new HashMap<>() { + { + put("field", new HashMap<>() { + { + put("foo", 1.0); + put("bar", 0.5); + } + }); + put("other", new HashMap<>() { + { + put("categorical", "dog"); + } + }); + } + }); + toInfer.add(new HashMap<>() { + { + put("field", new HashMap<>() { + { + put("foo", 0.9); + put("bar", 1.5); + } + }); + put("other", new HashMap<>() { + { + put("categorical", "cat"); + } + }); + } + }); List> toInfer2 = new ArrayList<>(); - toInfer2.add(new HashMap<>() {{ - put("field", new HashMap<>(){{ - put("foo", 0.0); - put("bar", 0.01); - }}); - put("other", new HashMap<>(){{ - put("categorical", "dog"); - }}); - }}); - toInfer2.add(new HashMap<>() {{ - put("field", new HashMap<>(){{ - put("foo", 1.0); - put("bar", 0.0); - }}); - put("other", new HashMap<>(){{ - put("categorical", "cat"); - }}); - }}); + toInfer2.add(new HashMap<>() { + { + put("field", new HashMap<>() { + { + put("foo", 0.0); + put("bar", 0.01); + } + }); + put("other", new HashMap<>() { + { + put("categorical", "dog"); + } + }); + } + }); + toInfer2.add(new HashMap<>() { + { + put("field", new HashMap<>() { + { + put("foo", 1.0); + put("bar", 0.0); + } + }); + put("other", new HashMap<>() { + { + put("categorical", "cat"); + } + }); + } + }); // Test regression - InternalInferModelAction.Request request = new InternalInferModelAction.Request(modelId, + InternalInferModelAction.Request request = new InternalInferModelAction.Request( + modelId, toInfer, ClassificationConfigUpdate.EMPTY_PARAMS, - true); + true + ); InternalInferModelAction.Response response = client().execute(InternalInferModelAction.INSTANCE, request).actionGet(); - assertThat(response.getInferenceResults() + assertThat( + response.getInferenceResults() .stream() - .map(i -> ((SingleValueInferenceResults)i).valueAsString()) + .map(i -> ((SingleValueInferenceResults) i).valueAsString()) .collect(Collectors.toList()), - contains("option_0", "option_2")); + contains("option_0", "option_2") + ); request = new InternalInferModelAction.Request(modelId, toInfer2, ClassificationConfigUpdate.EMPTY_PARAMS, true); response = client().execute(InternalInferModelAction.INSTANCE, request).actionGet(); - assertThat(response.getInferenceResults() + assertThat( + response.getInferenceResults() .stream() - .map(i -> ((SingleValueInferenceResults)i).valueAsString()) + .map(i -> ((SingleValueInferenceResults) i).valueAsString()) .collect(Collectors.toList()), - contains("option_2", "option_0")); - + contains("option_2", "option_0") + ); // Get top classes request = new InternalInferModelAction.Request(modelId, toInfer, new ClassificationConfigUpdate(3, null, null, null, null), true); response = client().execute(InternalInferModelAction.INSTANCE, request).actionGet(); - ClassificationInferenceResults classificationInferenceResults = - (ClassificationInferenceResults)response.getInferenceResults().get(0); + ClassificationInferenceResults classificationInferenceResults = (ClassificationInferenceResults) response.getInferenceResults() + .get(0); assertThat(classificationInferenceResults.getTopClasses().get(0).getClassification(), equalTo("option_0")); assertThat(classificationInferenceResults.getTopClasses().get(1).getClassification(), equalTo("option_2")); assertThat(classificationInferenceResults.getTopClasses().get(2).getClassification(), equalTo("option_1")); - classificationInferenceResults = (ClassificationInferenceResults)response.getInferenceResults().get(1); + classificationInferenceResults = (ClassificationInferenceResults) response.getInferenceResults().get(1); assertThat(classificationInferenceResults.getTopClasses().get(0).getClassification(), equalTo("option_2")); assertThat(classificationInferenceResults.getTopClasses().get(1).getClassification(), equalTo("option_0")); assertThat(classificationInferenceResults.getTopClasses().get(2).getClassification(), equalTo("option_1")); } - public void testInferMissingModel() { String model = "test-infer-missing-model"; InternalInferModelAction.Request request = new InternalInferModelAction.Request( model, Collections.emptyList(), RegressionConfigUpdate.EMPTY_PARAMS, - true); + true + ); try { client().execute(InternalInferModelAction.INSTANCE, request).actionGet(); } catch (ElasticsearchException ex) { @@ -315,11 +386,14 @@ public void testInferMissingFields() throws Exception { Map oneHotEncoding = new HashMap<>(); oneHotEncoding.put("cat", "animal_cat"); oneHotEncoding.put("dog", "animal_dog"); - TrainedModelConfig config = buildTrainedModelConfigBuilder(modelId) - .setInput(new TrainedModelInput(Arrays.asList("field1", "field2"))) - .setParsedDefinition(new TrainedModelDefinition.Builder() - .setPreProcessors(Arrays.asList(new OneHotEncoding("categorical", oneHotEncoding, false))) - .setTrainedModel(buildRegression())) + TrainedModelConfig config = buildTrainedModelConfigBuilder(modelId).setInput( + new TrainedModelInput(Arrays.asList("field1", "field2")) + ) + .setParsedDefinition( + new TrainedModelDefinition.Builder().setPreProcessors( + Arrays.asList(new OneHotEncoding("categorical", oneHotEncoding, false)) + ).setTrainedModel(buildRegression()) + ) .setVersion(Version.CURRENT) .setEstimatedOperations(0) .setEstimatedHeapMemory(0) @@ -332,24 +406,27 @@ public void testInferMissingFields() throws Exception { assertThat(putConfigHolder.get(), is(true)); assertThat(exceptionHolder.get(), is(nullValue())); - List> toInferMissingField = new ArrayList<>(); - toInferMissingField.add(new HashMap<>() {{ - put("foo", 1.0); - put("bar", 0.5); - }}); + toInferMissingField.add(new HashMap<>() { + { + put("foo", 1.0); + put("bar", 0.5); + } + }); InternalInferModelAction.Request request = new InternalInferModelAction.Request( modelId, toInferMissingField, RegressionConfigUpdate.EMPTY_PARAMS, - true); + true + ); try { - InferenceResults result = - client().execute(InternalInferModelAction.INSTANCE, request).actionGet().getInferenceResults().get(0); + InferenceResults result = client().execute(InternalInferModelAction.INSTANCE, request).actionGet().getInferenceResults().get(0); assertThat(result, is(instanceOf(WarningInferenceResults.class))); - assertThat(((WarningInferenceResults)result).getWarning(), - equalTo(Messages.getMessage(Messages.INFERENCE_WARNING_ALL_FIELDS_MISSING, modelId))); + assertThat( + ((WarningInferenceResults) result).getWarning(), + equalTo(Messages.getMessage(Messages.INFERENCE_WARNING_ALL_FIELDS_MISSING, modelId)) + ); } catch (ElasticsearchException ex) { fail("Should not have thrown. Ex: " + ex.getMessage()); } @@ -369,36 +446,21 @@ public static TrainedModel buildMultiClassClassification() { Tree tree1 = Tree.builder() .setFeatureNames(featureNames) - .setRoot(TreeNode.builder(0) - .setLeftChild(1) - .setRightChild(2) - .setSplitFeature(0) - .setThreshold(0.5)) + .setRoot(TreeNode.builder(0).setLeftChild(1).setRightChild(2).setSplitFeature(0).setThreshold(0.5)) .addNode(TreeNode.builder(1).setLeafValue(Arrays.asList(1.0, 0.0, 2.0))) - .addNode(TreeNode.builder(2) - .setThreshold(0.8) - .setSplitFeature(1) - .setLeftChild(3) - .setRightChild(4)) + .addNode(TreeNode.builder(2).setThreshold(0.8).setSplitFeature(1).setLeftChild(3).setRightChild(4)) .addNode(TreeNode.builder(3).setLeafValue(Arrays.asList(0.0, 1.0, 0.0))) - .addNode(TreeNode.builder(4).setLeafValue(Arrays.asList(0.0, 0.0, 1.0))).build(); + .addNode(TreeNode.builder(4).setLeafValue(Arrays.asList(0.0, 0.0, 1.0))) + .build(); Tree tree2 = Tree.builder() .setFeatureNames(featureNames) - .setRoot(TreeNode.builder(0) - .setLeftChild(1) - .setRightChild(2) - .setSplitFeature(3) - .setThreshold(1.0)) + .setRoot(TreeNode.builder(0).setLeftChild(1).setRightChild(2).setSplitFeature(3).setThreshold(1.0)) .addNode(TreeNode.builder(1).setLeafValue(Arrays.asList(2.0, 0.0, 0.0))) .addNode(TreeNode.builder(2).setLeafValue(Arrays.asList(0.0, 2.0, 0.0))) .build(); Tree tree3 = Tree.builder() .setFeatureNames(featureNames) - .setRoot(TreeNode.builder(0) - .setLeftChild(1) - .setRightChild(2) - .setSplitFeature(0) - .setThreshold(1.0)) + .setRoot(TreeNode.builder(0).setLeftChild(1).setRightChild(2).setSplitFeature(0).setThreshold(1.0)) .addNode(TreeNode.builder(1).setLeafValue(Arrays.asList(0.0, 0.0, 1.0))) .addNode(TreeNode.builder(2).setLeafValue(Arrays.asList(0.0, 1.0, 0.0))) .build(); @@ -407,7 +469,7 @@ public static TrainedModel buildMultiClassClassification() { .setTargetType(TargetType.CLASSIFICATION) .setFeatureNames(featureNames) .setTrainedModels(Arrays.asList(tree1, tree2, tree3)) - .setOutputAggregator(new WeightedMode(new double[]{0.7, 0.5, 1.0}, 3)) + .setOutputAggregator(new WeightedMode(new double[] { 0.7, 0.5, 1.0 }, 3)) .build(); } diff --git a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/NetworkDisruptionIT.java b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/NetworkDisruptionIT.java index c2cd1ad41d09a..e2dc111007aac 100644 --- a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/NetworkDisruptionIT.java +++ b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/NetworkDisruptionIT.java @@ -61,8 +61,10 @@ public void testJobRelocation() throws Exception { restOfClusterSide.remove(origJobNode); String notIsolatedNode = restOfClusterSide.iterator().next(); - NetworkDisruption networkDisruption = - new NetworkDisruption(new NetworkDisruption.TwoPartitions(isolatedSide, restOfClusterSide), NetworkDisruption.DISCONNECT); + NetworkDisruption networkDisruption = new NetworkDisruption( + new NetworkDisruption.TwoPartitions(isolatedSide, restOfClusterSide), + NetworkDisruption.DISCONNECT + ); internalCluster().setDisruptionScheme(networkDisruption); networkDisruption.startDisrupting(); ensureStableCluster(4, notIsolatedNode); @@ -80,9 +82,11 @@ public void testJobRelocation() throws Exception { // The job running on the original node should have been killed, and hence should not have persisted quantiles SearchResponse searchResponse = client().prepareSearch(AnomalyDetectorsIndex.jobStateIndexPattern()) - .setQuery(QueryBuilders.idsQuery().addIds(Quantiles.documentId(job.getId()))) - .setTrackTotalHits(true) - .setIndicesOptions(IndicesOptions.lenientExpandOpen()).execute().actionGet(); + .setQuery(QueryBuilders.idsQuery().addIds(Quantiles.documentId(job.getId()))) + .setTrackTotalHits(true) + .setIndicesOptions(IndicesOptions.lenientExpandOpen()) + .execute() + .actionGet(); assertEquals(0L, searchResponse.getHits().getTotalHits().value); CloseJobAction.Request closeJobRequest = new CloseJobAction.Request(job.getId()); @@ -91,9 +95,11 @@ public void testJobRelocation() throws Exception { // The relocated job was closed rather than killed, and hence should have persisted quantiles searchResponse = client().prepareSearch(AnomalyDetectorsIndex.jobStateIndexPattern()) - .setQuery(QueryBuilders.idsQuery().addIds(Quantiles.documentId(job.getId()))) - .setTrackTotalHits(true) - .setIndicesOptions(IndicesOptions.lenientExpandOpen()).execute().actionGet(); + .setQuery(QueryBuilders.idsQuery().addIds(Quantiles.documentId(job.getId()))) + .setTrackTotalHits(true) + .setIndicesOptions(IndicesOptions.lenientExpandOpen()) + .execute() + .actionGet(); assertEquals(1L, searchResponse.getHits().getTotalHits().value); } } diff --git a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/PyTorchStateStreamerIT.java b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/PyTorchStateStreamerIT.java index df82dc1e7614d..db5e22b686c04 100644 --- a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/PyTorchStateStreamerIT.java +++ b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/PyTorchStateStreamerIT.java @@ -41,7 +41,7 @@ public void testRestoreState() throws IOException, InterruptedException { String modelId = "test-state-streamer-restore"; List chunks = new ArrayList<>(numChunks); - for (int i=0; i onSuccess = new AtomicReference<>(); AtomicReference onFailure = new AtomicReference<>(); - blockingCall(listener -> - stateStreamer.writeStateToStream(modelId, InferenceIndexConstants.LATEST_INDEX_NAME, outputStream, listener), - onSuccess, onFailure); + blockingCall( + listener -> stateStreamer.writeStateToStream(modelId, InferenceIndexConstants.LATEST_INDEX_NAME, outputStream, listener), + onSuccess, + onFailure + ); byte[] writtenData = outputStream.toByteArray(); @@ -65,7 +70,7 @@ public void testRestoreState() throws IOException, InterruptedException { assertEquals(modelSize, writtenSize); byte[] writtenChunk = new byte[chunkSize]; - for (int i=0; i createModelDefinitionDocs(List b for (int i = 0; i < binaryChunks.size(); i++) { String encodedData = new String(Base64.getEncoder().encode(binaryChunks.get(i)), StandardCharsets.UTF_8); - docs.add(new TrainedModelDefinitionDoc.Builder() - .setDocNum(i) - .setCompressedString(encodedData) - .setCompressionVersion(TrainedModelConfig.CURRENT_DEFINITION_COMPRESSION_VERSION) - .setTotalDefinitionLength(totalLength) - .setDefinitionLength(encodedData.length()) - .setEos(i == binaryChunks.size() - 1) - .setModelId(modelId) - .build()); + docs.add( + new TrainedModelDefinitionDoc.Builder().setDocNum(i) + .setCompressedString(encodedData) + .setCompressionVersion(TrainedModelConfig.CURRENT_DEFINITION_COMPRESSION_VERSION) + .setTotalDefinitionLength(totalLength) + .setDefinitionLength(encodedData.length()) + .setEos(i == binaryChunks.size() - 1) + .setModelId(modelId) + .build() + ); } return docs; } - private void putModelDefinition(List docs) throws IOException { BulkRequestBuilder bulkRequestBuilder = client().prepareBulk(); for (int i = 0; i < docs.size(); i++) { @@ -106,9 +111,7 @@ private void putModelDefinition(List docs) throws IOE } } - BulkResponse bulkResponse = bulkRequestBuilder - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .get(); + BulkResponse bulkResponse = bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).get(); if (bulkResponse.hasFailures()) { int failures = 0; for (BulkItemResponse itemResponse : bulkResponse) { diff --git a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/TestFeatureLicenseTrackingIT.java b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/TestFeatureLicenseTrackingIT.java index 9ff037b991cb5..1f334599a80da 100644 --- a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/TestFeatureLicenseTrackingIT.java +++ b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/TestFeatureLicenseTrackingIT.java @@ -13,10 +13,10 @@ import org.elasticsearch.action.ingest.PutPipelineAction; import org.elasticsearch.action.ingest.PutPipelineRequest; import org.elasticsearch.common.bytes.BytesArray; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.license.GetFeatureUsageRequest; import org.elasticsearch.license.GetFeatureUsageResponse; import org.elasticsearch.license.TransportGetFeatureUsageAction; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.ml.action.CloseJobAction; import org.elasticsearch.xpack.core.ml.action.GetJobsStatsAction; import org.elasticsearch.xpack.core.ml.action.OpenJobAction; @@ -55,6 +55,7 @@ public class TestFeatureLicenseTrackingIT extends MlSingleNodeTestCase { private final Set createdPipelines = new HashSet<>(); + @After public void cleanup() { for (String pipeline : createdPipelines) { @@ -68,8 +69,7 @@ public void cleanup() { public void testFeatureTrackingAnomalyJob() throws Exception { putAndStartJob("job-feature-usage"); - GetFeatureUsageResponse.FeatureUsageInfo mlFeatureUsage = getFeatureUsageInfo() - .stream() + GetFeatureUsageResponse.FeatureUsageInfo mlFeatureUsage = getFeatureUsageInfo().stream() .filter(f -> f.getFamily().equals(ML_FEATURE_FAMILY)) .filter(f -> f.getName().equals(MachineLearning.ML_ANOMALY_JOBS_FEATURE.getName())) .findAny() @@ -79,8 +79,7 @@ public void testFeatureTrackingAnomalyJob() throws Exception { // While the job is opened, the lastUsage moves forward to "now". Verify it does that ZonedDateTime lastUsage = mlFeatureUsage.getLastUsedTime(); assertBusy(() -> { - ZonedDateTime recentUsage = getFeatureUsageInfo() - .stream() + ZonedDateTime recentUsage = getFeatureUsageInfo().stream() .filter(f -> f.getFamily().equals(ML_FEATURE_FAMILY)) .filter(f -> f.getName().equals(MachineLearning.ML_ANOMALY_JOBS_FEATURE.getName())) .map(GetFeatureUsageResponse.FeatureUsageInfo::getLastUsedTime) @@ -92,8 +91,7 @@ public void testFeatureTrackingAnomalyJob() throws Exception { client().execute(CloseJobAction.INSTANCE, new CloseJobAction.Request("job-feature-usage")).actionGet(); - mlFeatureUsage = getFeatureUsageInfo() - .stream() + mlFeatureUsage = getFeatureUsageInfo().stream() .filter(f -> f.getFamily().equals(ML_FEATURE_FAMILY)) .filter(f -> f.getName().equals(MachineLearning.ML_ANOMALY_JOBS_FEATURE.getName())) .findAny() @@ -105,8 +103,7 @@ public void testFeatureTrackingAnomalyJob() throws Exception { ZonedDateTime lastUsageAfterClose = mlFeatureUsage.getLastUsedTime(); assertBusy(() -> { - ZonedDateTime recentUsage =getFeatureUsageInfo() - .stream() + ZonedDateTime recentUsage = getFeatureUsageInfo().stream() .filter(f -> f.getFamily().equals(ML_FEATURE_FAMILY)) .filter(f -> f.getName().equals(MachineLearning.ML_ANOMALY_JOBS_FEATURE.getName())) .map(GetFeatureUsageResponse.FeatureUsageInfo::getLastUsedTime) @@ -122,12 +119,15 @@ public void testFeatureTrackingInferenceModelPipeline() throws Exception { Map oneHotEncoding = new HashMap<>(); oneHotEncoding.put("cat", "animal_cat"); oneHotEncoding.put("dog", "animal_dog"); - TrainedModelConfig config = buildTrainedModelConfigBuilder(modelId) - .setInput(new TrainedModelInput(Arrays.asList("field.foo", "field.bar", "other.categorical"))) + TrainedModelConfig config = buildTrainedModelConfigBuilder(modelId).setInput( + new TrainedModelInput(Arrays.asList("field.foo", "field.bar", "other.categorical")) + ) .setInferenceConfig(new ClassificationConfig(3)) - .setParsedDefinition(new TrainedModelDefinition.Builder() - .setPreProcessors(Arrays.asList(new OneHotEncoding("other.categorical", oneHotEncoding, false))) - .setTrainedModel(buildClassification(true))) + .setParsedDefinition( + new TrainedModelDefinition.Builder().setPreProcessors( + Arrays.asList(new OneHotEncoding("other.categorical", oneHotEncoding, false)) + ).setTrainedModel(buildClassification(true)) + ) .build(); client().execute(PutTrainedModelAction.INSTANCE, new PutTrainedModelAction.Request(config, false)).actionGet(); @@ -137,8 +137,7 @@ public void testFeatureTrackingInferenceModelPipeline() throws Exception { // wait for the feature to start being used assertBusy(() -> { - GetFeatureUsageResponse.FeatureUsageInfo mlFeatureUsage = getFeatureUsageInfo() - .stream() + GetFeatureUsageResponse.FeatureUsageInfo mlFeatureUsage = getFeatureUsageInfo().stream() .filter(f -> f.getFamily().equals(ML_FEATURE_FAMILY)) .filter(f -> f.getName().equals(MachineLearning.ML_MODEL_INFERENCE_FEATURE.getName())) .findAny() @@ -147,8 +146,7 @@ public void testFeatureTrackingInferenceModelPipeline() throws Exception { assertThat(mlFeatureUsage.getContext(), containsString(modelId)); }); - GetFeatureUsageResponse.FeatureUsageInfo mlFeatureUsage = getFeatureUsageInfo() - .stream() + GetFeatureUsageResponse.FeatureUsageInfo mlFeatureUsage = getFeatureUsageInfo().stream() .filter(f -> f.getFamily().equals(ML_FEATURE_FAMILY)) .filter(f -> f.getName().equals(MachineLearning.ML_MODEL_INFERENCE_FEATURE.getName())) .findAny() @@ -157,8 +155,7 @@ public void testFeatureTrackingInferenceModelPipeline() throws Exception { // While the model is referenced, the lastUsage moves forward to "now". Verify it does that ZonedDateTime lastUsage = mlFeatureUsage.getLastUsedTime(); assertBusy(() -> { - ZonedDateTime recentUsage = getFeatureUsageInfo() - .stream() + ZonedDateTime recentUsage = getFeatureUsageInfo().stream() .filter(f -> f.getFamily().equals(ML_FEATURE_FAMILY)) .filter(f -> f.getName().equals(MachineLearning.ML_MODEL_INFERENCE_FEATURE.getName())) .map(GetFeatureUsageResponse.FeatureUsageInfo::getLastUsedTime) @@ -173,16 +170,14 @@ public void testFeatureTrackingInferenceModelPipeline() throws Exception { // Make sure that feature usage keeps the last usage once the model is removed assertBusy(() -> { - ZonedDateTime recentUsage = getFeatureUsageInfo() - .stream() + ZonedDateTime recentUsage = getFeatureUsageInfo().stream() .filter(f -> f.getFamily().equals(ML_FEATURE_FAMILY)) .filter(f -> f.getName().equals(MachineLearning.ML_MODEL_INFERENCE_FEATURE.getName())) .map(GetFeatureUsageResponse.FeatureUsageInfo::getLastUsedTime) .findAny() .orElse(null); assertThat(recentUsage, is(not(nullValue()))); - ZonedDateTime secondRecentUsage = getFeatureUsageInfo() - .stream() + ZonedDateTime secondRecentUsage = getFeatureUsageInfo().stream() .filter(f -> f.getFamily().equals(ML_FEATURE_FAMILY)) .filter(f -> f.getName().equals(MachineLearning.ML_MODEL_INFERENCE_FEATURE.getName())) .map(GetFeatureUsageResponse.FeatureUsageInfo::getLastUsedTime) @@ -194,10 +189,7 @@ public void testFeatureTrackingInferenceModelPipeline() throws Exception { } private List getFeatureUsageInfo() { - return client() - .execute(TransportGetFeatureUsageAction.TYPE, new GetFeatureUsageRequest()) - .actionGet() - .getFeatures(); + return client().execute(TransportGetFeatureUsageAction.TYPE, new GetFeatureUsageRequest()).actionGet().getFeatures(); } private void putAndStartJob(String jobId) throws Exception { @@ -219,17 +211,19 @@ private void putTrainedModelIngestPipeline(String pipelineId, String modelId) th new PutPipelineRequest( pipelineId, new BytesArray( - "{\n" + - " \"processors\": [\n" + - " {\n" + - " \"inference\": {\n" + - " \"inference_config\": {\"classification\":{}},\n" + - " \"model_id\": \"" + modelId + "\",\n" + - " \"field_map\": {}\n" + - " }\n" + - " }\n" + - " ]\n" + - " }" + "{\n" + + " \"processors\": [\n" + + " {\n" + + " \"inference\": {\n" + + " \"inference_config\": {\"classification\":{}},\n" + + " \"model_id\": \"" + + modelId + + "\",\n" + + " \"field_map\": {}\n" + + " }\n" + + " }\n" + + " ]\n" + + " }" ), XContentType.JSON ) diff --git a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/TooManyJobsIT.java b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/TooManyJobsIT.java index e0a1d771ab302..d8a6da6b4d1e8 100644 --- a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/TooManyJobsIT.java +++ b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/TooManyJobsIT.java @@ -14,6 +14,7 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.persistent.PersistentTasksCustomMetadata; import org.elasticsearch.transport.TransportService; import org.elasticsearch.xpack.core.ml.MlTasks; import org.elasticsearch.xpack.core.ml.action.CloseJobAction; @@ -23,7 +24,6 @@ import org.elasticsearch.xpack.core.ml.job.config.Job; import org.elasticsearch.xpack.core.ml.job.config.JobState; import org.elasticsearch.xpack.core.ml.job.config.JobTaskState; -import org.elasticsearch.persistent.PersistentTasksCustomMetadata; import org.elasticsearch.xpack.ml.MachineLearning; import org.elasticsearch.xpack.ml.support.BaseMlIntegTestCase; import org.elasticsearch.xpack.ml.utils.NativeMemoryCalculator; @@ -39,8 +39,10 @@ public void testCloseFailedJob() throws Exception { client().execute(PutJobAction.INSTANCE, putJobRequest).get(); client().execute(OpenJobAction.INSTANCE, new OpenJobAction.Request(job.getId())).get(); assertBusy(() -> { - GetJobsStatsAction.Response statsResponse = - client().execute(GetJobsStatsAction.INSTANCE, new GetJobsStatsAction.Request("close-failed-job-1")).actionGet(); + GetJobsStatsAction.Response statsResponse = client().execute( + GetJobsStatsAction.INSTANCE, + new GetJobsStatsAction.Request("close-failed-job-1") + ).actionGet(); assertEquals(statsResponse.getResponse().results().get(0).getState(), JobState.OPENED); }); @@ -48,12 +50,16 @@ public void testCloseFailedJob() throws Exception { job = createJob("close-failed-job-2", ByteSizeValue.ofMb(2)); putJobRequest = new PutJobAction.Request(job); client().execute(PutJobAction.INSTANCE, putJobRequest).get(); - expectThrows(ElasticsearchStatusException.class, - () -> client().execute(OpenJobAction.INSTANCE, new OpenJobAction.Request("close-failed-job-2")).actionGet()); + expectThrows( + ElasticsearchStatusException.class, + () -> client().execute(OpenJobAction.INSTANCE, new OpenJobAction.Request("close-failed-job-2")).actionGet() + ); // Ensure that the second job didn't even attempt to be opened and we still have 1 job open: - GetJobsStatsAction.Response statsResponse = - client().execute(GetJobsStatsAction.INSTANCE, new GetJobsStatsAction.Request("close-failed-job-2")).actionGet(); + GetJobsStatsAction.Response statsResponse = client().execute( + GetJobsStatsAction.INSTANCE, + new GetJobsStatsAction.Request("close-failed-job-2") + ).actionGet(); assertEquals(statsResponse.getResponse().results().get(0).getState(), JobState.CLOSED); ClusterState state = client().admin().cluster().prepareState().get().getState(); PersistentTasksCustomMetadata tasks = state.getMetadata().custom(PersistentTasksCustomMetadata.TYPE); @@ -70,31 +76,31 @@ public void testLazyNodeValidation() throws Exception { internalCluster().ensureAtMostNumDataNodes(0); logger.info("[{}] is [{}]", MachineLearning.MAX_OPEN_JOBS_PER_NODE.getKey(), maxNumberOfJobsPerNode); for (int i = 0; i < numNodes; i++) { - internalCluster().startNode(Settings.builder() - .put(MachineLearning.MAX_OPEN_JOBS_PER_NODE.getKey(), maxNumberOfJobsPerNode)); + internalCluster().startNode(Settings.builder().put(MachineLearning.MAX_OPEN_JOBS_PER_NODE.getKey(), maxNumberOfJobsPerNode)); } logger.info("Started [{}] nodes", numNodes); ensureStableCluster(numNodes); ensureTemplatesArePresent(); logger.info("[{}] is [{}]", MachineLearning.MAX_LAZY_ML_NODES.getKey(), maxNumberOfLazyNodes); // Set our lazy node number - assertTrue(client().admin() - .cluster() - .prepareUpdateSettings() - .setPersistentSettings( - Settings.builder() - .put(MachineLearning.MAX_LAZY_ML_NODES.getKey(), maxNumberOfLazyNodes)) - .get() - .isAcknowledged()); + assertTrue( + client().admin() + .cluster() + .prepareUpdateSettings() + .setPersistentSettings(Settings.builder().put(MachineLearning.MAX_LAZY_ML_NODES.getKey(), maxNumberOfLazyNodes)) + .get() + .isAcknowledged() + ); // create and open first job, which succeeds: Job.Builder job = createJob("lazy-node-validation-job-1", ByteSizeValue.ofMb(2)); PutJobAction.Request putJobRequest = new PutJobAction.Request(job); client().execute(PutJobAction.INSTANCE, putJobRequest).get(); client().execute(OpenJobAction.INSTANCE, new OpenJobAction.Request(job.getId())).get(); assertBusy(() -> { - GetJobsStatsAction.Response statsResponse = - client().execute(GetJobsStatsAction.INSTANCE, - new GetJobsStatsAction.Request("lazy-node-validation-job-1")).actionGet(); + GetJobsStatsAction.Response statsResponse = client().execute( + GetJobsStatsAction.INSTANCE, + new GetJobsStatsAction.Request("lazy-node-validation-job-1") + ).actionGet(); assertEquals(statsResponse.getResponse().results().get(0).getState(), JobState.OPENED); }); @@ -105,23 +111,24 @@ public void testLazyNodeValidation() throws Exception { client().execute(OpenJobAction.INSTANCE, new OpenJobAction.Request(job.getId())).get(); // Should return while job is opening assertBusy(() -> { - GetJobsStatsAction.Response statsResponse = - client().execute(GetJobsStatsAction.INSTANCE, - new GetJobsStatsAction.Request("lazy-node-validation-job-2")).actionGet(); + GetJobsStatsAction.Response statsResponse = client().execute( + GetJobsStatsAction.INSTANCE, + new GetJobsStatsAction.Request("lazy-node-validation-job-2") + ).actionGet(); // Should get to opening state w/o a node assertEquals(JobState.OPENING, statsResponse.getResponse().results().get(0).getState()); }); // Add another Node so we can get allocated - internalCluster().startNode(Settings.builder() - .put(MachineLearning.MAX_OPEN_JOBS_PER_NODE.getKey(), maxNumberOfJobsPerNode)); - ensureStableCluster(numNodes+1); + internalCluster().startNode(Settings.builder().put(MachineLearning.MAX_OPEN_JOBS_PER_NODE.getKey(), maxNumberOfJobsPerNode)); + ensureStableCluster(numNodes + 1); // We should automatically get allocated and opened to new node assertBusy(() -> { - GetJobsStatsAction.Response statsResponse = - client().execute(GetJobsStatsAction.INSTANCE, - new GetJobsStatsAction.Request("lazy-node-validation-job-2")).actionGet(); + GetJobsStatsAction.Response statsResponse = client().execute( + GetJobsStatsAction.INSTANCE, + new GetJobsStatsAction.Request("lazy-node-validation-job-2") + ).actionGet(); assertEquals(JobState.OPENED, statsResponse.getResponse().results().get(0).getState()); }); } @@ -145,7 +152,8 @@ private void verifyMaxNumberOfJobsLimit(int numNodes, int maxNumberOfJobsPerNode for (int i = 1; i <= (clusterWideMaxNumberOfJobs + 1); i++) { if (i == 2 && testDynamicChange) { ClusterUpdateSettingsRequest clusterUpdateSettingsRequest = new ClusterUpdateSettingsRequest().persistentSettings( - Settings.builder().put(MachineLearning.MAX_OPEN_JOBS_PER_NODE.getKey(), maxNumberOfJobsPerNode).build()); + Settings.builder().put(MachineLearning.MAX_OPEN_JOBS_PER_NODE.getKey(), maxNumberOfJobsPerNode).build() + ); client().execute(ClusterUpdateSettingsAction.INSTANCE, clusterUpdateSettingsRequest).actionGet(); } Job.Builder job = createJob("max-number-of-jobs-limit-job-" + Integer.toString(i), jobModelMemoryLimit); @@ -156,8 +164,10 @@ private void verifyMaxNumberOfJobsLimit(int numNodes, int maxNumberOfJobsPerNode try { client().execute(OpenJobAction.INSTANCE, openJobRequest).actionGet(); assertBusy(() -> { - GetJobsStatsAction.Response statsResponse = - client().execute(GetJobsStatsAction.INSTANCE, new GetJobsStatsAction.Request(job.getId())).actionGet(); + GetJobsStatsAction.Response statsResponse = client().execute( + GetJobsStatsAction.INSTANCE, + new GetJobsStatsAction.Request(job.getId()) + ).actionGet(); assertEquals(statsResponse.getResponse().results().get(0).getState(), JobState.OPENED); }); logger.info("Opened {}th job", i); @@ -166,18 +176,36 @@ private void verifyMaxNumberOfJobsLimit(int numNodes, int maxNumberOfJobsPerNode IllegalStateException detail = (IllegalStateException) e.getCause(); assertNotNull(detail); String detailedMessage = detail.getMessage(); - assertTrue(detailedMessage, - detailedMessage.startsWith("Could not open job because no suitable nodes were found, allocation explanation")); + assertTrue( + detailedMessage, + detailedMessage.startsWith("Could not open job because no suitable nodes were found, allocation explanation") + ); if (expectMemoryLimitBeforeCountLimit) { int expectedJobsAlreadyOpenOnNode = (i - 1) / numNodes; - assertTrue(detailedMessage, - detailedMessage.endsWith("node has insufficient available memory. Available memory for ML [" + - maxMlMemoryPerNode + "], memory required by existing jobs [" + - (expectedJobsAlreadyOpenOnNode * memoryFootprintPerJob) + "], estimated memory required for this job [" + - memoryFootprintPerJob + "].]")); + assertTrue( + detailedMessage, + detailedMessage.endsWith( + "node has insufficient available memory. Available memory for ML [" + + maxMlMemoryPerNode + + "], memory required by existing jobs [" + + (expectedJobsAlreadyOpenOnNode * memoryFootprintPerJob) + + "], estimated memory required for this job [" + + memoryFootprintPerJob + + "].]" + ) + ); } else { - assertTrue(detailedMessage, detailedMessage.endsWith("node is full. Number of opened jobs and allocated native " + - "inference processes [" + maxNumberOfJobsPerNode + "], xpack.ml.max_open_jobs [" + maxNumberOfJobsPerNode + "].]")); + assertTrue( + detailedMessage, + detailedMessage.endsWith( + "node is full. Number of opened jobs and allocated native " + + "inference processes [" + + maxNumberOfJobsPerNode + + "], xpack.ml.max_open_jobs [" + + maxNumberOfJobsPerNode + + "].]" + ) + ); } logger.info("good news everybody --> reached maximum number of allowed opened jobs, after trying to open the {}th job", i); @@ -189,8 +217,13 @@ private void verifyMaxNumberOfJobsLimit(int numNodes, int maxNumberOfJobsPerNode client().execute(OpenJobAction.INSTANCE, openJobRequest).actionGet(); assertBusy(() -> { for (Client client : clients()) { - PersistentTasksCustomMetadata tasks = client.admin().cluster().prepareState().get().getState() - .getMetadata().custom(PersistentTasksCustomMetadata.TYPE); + PersistentTasksCustomMetadata tasks = client.admin() + .cluster() + .prepareState() + .get() + .getState() + .getMetadata() + .custom(PersistentTasksCustomMetadata.TYPE); assertEquals(MlTasks.getJobState(job.getId(), tasks), JobState.OPENED); } }); @@ -205,8 +238,7 @@ private void startMlCluster(int numNodes, int maxNumberOfWorkersPerNode) throws internalCluster().ensureAtMostNumDataNodes(0); logger.info("[{}] is [{}]", MachineLearning.MAX_OPEN_JOBS_PER_NODE.getKey(), maxNumberOfWorkersPerNode); for (int i = 0; i < numNodes; i++) { - internalCluster().startNode(Settings.builder() - .put(MachineLearning.MAX_OPEN_JOBS_PER_NODE.getKey(), maxNumberOfWorkersPerNode)); + internalCluster().startNode(Settings.builder().put(MachineLearning.MAX_OPEN_JOBS_PER_NODE.getKey(), maxNumberOfWorkersPerNode)); } logger.info("Started [{}] nodes", numNodes); ensureStableCluster(numNodes); diff --git a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/TrainedModelCRUDIT.java b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/TrainedModelCRUDIT.java index a28870fc4307a..9585380ce4d38 100644 --- a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/TrainedModelCRUDIT.java +++ b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/TrainedModelCRUDIT.java @@ -13,9 +13,9 @@ import org.elasticsearch.xpack.core.ml.inference.TrainedModelConfig; import org.elasticsearch.xpack.core.ml.inference.TrainedModelType; import org.elasticsearch.xpack.core.ml.inference.persistence.InferenceIndexConstants; -import org.elasticsearch.xpack.core.ml.inference.trainedmodel.PassThroughConfig; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.BertTokenization; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.IndexLocation; +import org.elasticsearch.xpack.core.ml.inference.trainedmodel.PassThroughConfig; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.Tokenization; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.VocabularyConfig; import org.elasticsearch.xpack.ml.MlSingleNodeTestCase; @@ -29,28 +29,28 @@ public class TrainedModelCRUDIT extends MlSingleNodeTestCase { static final String BASE_64_ENCODED_MODEL = - "UEsDBAAACAgAAAAAAAAAAAAAAAAAAAAAAAAUAA4Ac2ltcGxlbW9kZWwvZGF0YS5wa2xGQgoAWlpaWlpaWlpaWoACY19fdG9yY2hfXwp" + - "TdXBlclNpbXBsZQpxACmBfShYCAAAAHRyYWluaW5ncQGIdWJxAi5QSwcIXOpBBDQAAAA0AAAAUEsDBBQACAgIAAAAAAAAAAAAAAAAAA" + - "AAAAAdAEEAc2ltcGxlbW9kZWwvY29kZS9fX3RvcmNoX18ucHlGQj0AWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaW" + - "lpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWnWOMWvDMBCF9/yKI5MMrnHTQsHgjt2aJdlCEIp9SgWSTpykFvfXV1htaYds0nfv473Jqhjh" + - "kAPywbhgUbzSnC02wwZAyqBYOUzIUUoY4XRe6SVr/Q8lVsYbf4UBLkS2kBk1aOIPxbOIaPVQtEQ8vUnZ/WlrSxTA+JCTNHMc4Ig+Ele" + - "s+Jod+iR3N/jDDf74wxu4e/5+DmtE9mUyhdgFNq7bZ3ekehbruC6aTxS/c1rom6Z698WrEfIYxcn4JGTftLA7tzCnJeD41IJVC+U07k" + - "umUHw3E47Vqh+xnULeFisYLx064mV8UTZibWFMmX0p23wBUEsHCE0EGH3yAAAAlwEAAFBLAwQUAAgICAAAAAAAAAAAAAAAAAAAAAAAJ" + - "wA5AHNpbXBsZW1vZGVsL2NvZGUvX190b3JjaF9fLnB5LmRlYnVnX3BrbEZCNQBaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpa" + - "WlpaWlpaWlpaWlpaWlpaWlpaWlpaWrWST0+DMBiHW6bOod/BGS94kKpo2Mwyox5x3pbgiXSAFtdR/nQu3IwHiZ9oX88CaeGu9tL0efq" + - "+v8P7fmiGA1wgTgoIcECZQqe6vmYD6G4hAJOcB1E8NazTm+ELyzY4C3Q0z8MsRwF+j4JlQUPEEo5wjH0WB9hCNFqgpOCExZY5QnnEw7" + - "ME+0v8GuaIs8wnKI7RigVrKkBzm0lh2OdjkeHllG28f066vK6SfEypF60S+vuYt4gjj2fYr/uPrSvRv356TepfJ9iWJRN0OaELQSZN3" + - "FRPNbcP1PTSntMr0x0HzLZQjPYIEo3UaFeiISRKH0Mil+BE/dyT1m7tCBLwVO1MX4DK3bbuTlXuy8r71j5Aoho66udAoseOnrdVzx28" + - "UFW6ROuO/lT6QKKyo79VU54emj9QSwcInsUTEDMBAAAFAwAAUEsDBAAACAgAAAAAAAAAAAAAAAAAAAAAAAAZAAYAc2ltcGxlbW9kZWw" + - "vY29uc3RhbnRzLnBrbEZCAgBaWoACKS5QSwcIbS8JVwQAAAAEAAAAUEsDBAAACAgAAAAAAAAAAAAAAAAAAAAAAAATADsAc2ltcGxlbW" + - "9kZWwvdmVyc2lvbkZCNwBaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaMwpQSwcI0" + - "Z5nVQIAAAACAAAAUEsBAgAAAAAICAAAAAAAAFzqQQQ0AAAANAAAABQAAAAAAAAAAAAAAAAAAAAAAHNpbXBsZW1vZGVsL2RhdGEucGts" + - "UEsBAgAAFAAICAgAAAAAAE0EGH3yAAAAlwEAAB0AAAAAAAAAAAAAAAAAhAAAAHNpbXBsZW1vZGVsL2NvZGUvX190b3JjaF9fLnB5UEs" + - "BAgAAFAAICAgAAAAAAJ7FExAzAQAABQMAACcAAAAAAAAAAAAAAAAAAgIAAHNpbXBsZW1vZGVsL2NvZGUvX190b3JjaF9fLnB5LmRlYn" + - "VnX3BrbFBLAQIAAAAACAgAAAAAAABtLwlXBAAAAAQAAAAZAAAAAAAAAAAAAAAAAMMDAABzaW1wbGVtb2RlbC9jb25zdGFudHMucGtsU" + - "EsBAgAAAAAICAAAAAAAANGeZ1UCAAAAAgAAABMAAAAAAAAAAAAAAAAAFAQAAHNpbXBsZW1vZGVsL3ZlcnNpb25QSwYGLAAAAAAAAAAe" + - "Ay0AAAAAAAAAAAAFAAAAAAAAAAUAAAAAAAAAagEAAAAAAACSBAAAAAAAAFBLBgcAAAAA/AUAAAAAAAABAAAAUEsFBgAAAAAFAAUAagE" + - "AAJIEAAAAAA=="; + "UEsDBAAACAgAAAAAAAAAAAAAAAAAAAAAAAAUAA4Ac2ltcGxlbW9kZWwvZGF0YS5wa2xGQgoAWlpaWlpaWlpaWoACY19fdG9yY2hfXwp" + + "TdXBlclNpbXBsZQpxACmBfShYCAAAAHRyYWluaW5ncQGIdWJxAi5QSwcIXOpBBDQAAAA0AAAAUEsDBBQACAgIAAAAAAAAAAAAAAAAAA" + + "AAAAAdAEEAc2ltcGxlbW9kZWwvY29kZS9fX3RvcmNoX18ucHlGQj0AWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaW" + + "lpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWnWOMWvDMBCF9/yKI5MMrnHTQsHgjt2aJdlCEIp9SgWSTpykFvfXV1htaYds0nfv473Jqhjh" + + "kAPywbhgUbzSnC02wwZAyqBYOUzIUUoY4XRe6SVr/Q8lVsYbf4UBLkS2kBk1aOIPxbOIaPVQtEQ8vUnZ/WlrSxTA+JCTNHMc4Ig+Ele" + + "s+Jod+iR3N/jDDf74wxu4e/5+DmtE9mUyhdgFNq7bZ3ekehbruC6aTxS/c1rom6Z698WrEfIYxcn4JGTftLA7tzCnJeD41IJVC+U07k" + + "umUHw3E47Vqh+xnULeFisYLx064mV8UTZibWFMmX0p23wBUEsHCE0EGH3yAAAAlwEAAFBLAwQUAAgICAAAAAAAAAAAAAAAAAAAAAAAJ" + + "wA5AHNpbXBsZW1vZGVsL2NvZGUvX190b3JjaF9fLnB5LmRlYnVnX3BrbEZCNQBaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpa" + + "WlpaWlpaWlpaWlpaWlpaWlpaWlpaWrWST0+DMBiHW6bOod/BGS94kKpo2Mwyox5x3pbgiXSAFtdR/nQu3IwHiZ9oX88CaeGu9tL0efq" + + "+v8P7fmiGA1wgTgoIcECZQqe6vmYD6G4hAJOcB1E8NazTm+ELyzY4C3Q0z8MsRwF+j4JlQUPEEo5wjH0WB9hCNFqgpOCExZY5QnnEw7" + + "ME+0v8GuaIs8wnKI7RigVrKkBzm0lh2OdjkeHllG28f066vK6SfEypF60S+vuYt4gjj2fYr/uPrSvRv356TepfJ9iWJRN0OaELQSZN3" + + "FRPNbcP1PTSntMr0x0HzLZQjPYIEo3UaFeiISRKH0Mil+BE/dyT1m7tCBLwVO1MX4DK3bbuTlXuy8r71j5Aoho66udAoseOnrdVzx28" + + "UFW6ROuO/lT6QKKyo79VU54emj9QSwcInsUTEDMBAAAFAwAAUEsDBAAACAgAAAAAAAAAAAAAAAAAAAAAAAAZAAYAc2ltcGxlbW9kZWw" + + "vY29uc3RhbnRzLnBrbEZCAgBaWoACKS5QSwcIbS8JVwQAAAAEAAAAUEsDBAAACAgAAAAAAAAAAAAAAAAAAAAAAAATADsAc2ltcGxlbW" + + "9kZWwvdmVyc2lvbkZCNwBaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaWlpaMwpQSwcI0" + + "Z5nVQIAAAACAAAAUEsBAgAAAAAICAAAAAAAAFzqQQQ0AAAANAAAABQAAAAAAAAAAAAAAAAAAAAAAHNpbXBsZW1vZGVsL2RhdGEucGts" + + "UEsBAgAAFAAICAgAAAAAAE0EGH3yAAAAlwEAAB0AAAAAAAAAAAAAAAAAhAAAAHNpbXBsZW1vZGVsL2NvZGUvX190b3JjaF9fLnB5UEs" + + "BAgAAFAAICAgAAAAAAJ7FExAzAQAABQMAACcAAAAAAAAAAAAAAAAAAgIAAHNpbXBsZW1vZGVsL2NvZGUvX190b3JjaF9fLnB5LmRlYn" + + "VnX3BrbFBLAQIAAAAACAgAAAAAAABtLwlXBAAAAAQAAAAZAAAAAAAAAAAAAAAAAMMDAABzaW1wbGVtb2RlbC9jb25zdGFudHMucGtsU" + + "EsBAgAAAAAICAAAAAAAANGeZ1UCAAAAAgAAABMAAAAAAAAAAAAAAAAAFAQAAHNpbXBsZW1vZGVsL3ZlcnNpb25QSwYGLAAAAAAAAAAe" + + "Ay0AAAAAAAAAAAAFAAAAAAAAAAUAAAAAAAAAagEAAAAAAACSBAAAAAAAAFBLBgcAAAAA/AUAAAAAAAABAAAAUEsFBgAAAAAFAAUAagE" + + "AAJIEAAAAAA=="; static final long RAW_MODEL_SIZE; // size of the model before base64 encoding static { RAW_MODEL_SIZE = Base64.getDecoder().decode(BASE_64_ENCODED_MODEL).length; @@ -70,9 +70,7 @@ public void testPutTrainedModelAndDefinition() { .setModelType(TrainedModelType.PYTORCH) .setInferenceConfig( new PassThroughConfig( - new VocabularyConfig( - InferenceIndexConstants.nativeDefinitionStore() - ), + new VocabularyConfig(InferenceIndexConstants.nativeDefinitionStore()), new BertTokenization(null, false, null, Tokenization.Truncate.NONE), null ) @@ -91,19 +89,11 @@ public void testPutTrainedModelAndDefinition() { ).actionGet(); assertThat( - client().admin() - .indices() - .prepareGetIndex() - .addIndices(InferenceIndexConstants.nativeDefinitionStore()) - .get() - .indices().length, + client().admin().indices().prepareGetIndex().addIndices(InferenceIndexConstants.nativeDefinitionStore()).get().indices().length, equalTo(1) ); - client().execute( - DeleteTrainedModelAction.INSTANCE, - new DeleteTrainedModelAction.Request(modelId) - ).actionGet(); + client().execute(DeleteTrainedModelAction.INSTANCE, new DeleteTrainedModelAction.Request(modelId)).actionGet(); assertThat( client().prepareSearch(InferenceIndexConstants.nativeDefinitionStore()) @@ -111,8 +101,7 @@ public void testPutTrainedModelAndDefinition() { .setSize(0) .get() .getHits() - .getTotalHits() - .value, + .getTotalHits().value, equalTo(0L) ); } diff --git a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/TrainedModelProviderIT.java b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/TrainedModelProviderIT.java index de5273851aeff..d112942587b8e 100644 --- a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/TrainedModelProviderIT.java +++ b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/TrainedModelProviderIT.java @@ -15,10 +15,10 @@ import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.support.WriteRequest; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.license.License; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.license.License; import org.elasticsearch.xpack.core.ml.action.GetTrainedModelsAction; import org.elasticsearch.xpack.core.ml.inference.TrainedModelConfig; import org.elasticsearch.xpack.core.ml.inference.TrainedModelDefinitionTests; @@ -81,8 +81,7 @@ public void testPutTrainedModelConfigThatAlreadyExists() throws Exception { blockingCall(listener -> trainedModelProvider.storeTrainedModel(config, listener), putConfigHolder, exceptionHolder); assertThat(exceptionHolder.get(), is(not(nullValue()))); - assertThat(exceptionHolder.get().getMessage(), - equalTo(Messages.getMessage(Messages.INFERENCE_TRAINED_MODEL_EXISTS, modelId))); + assertThat(exceptionHolder.get().getMessage(), equalTo(Messages.getMessage(Messages.INFERENCE_TRAINED_MODEL_EXISTS, modelId))); } public void testGetTrainedModelConfig() throws Exception { @@ -96,25 +95,32 @@ public void testGetTrainedModelConfig() throws Exception { assertThat(exceptionHolder.get(), is(nullValue())); AtomicReference putMetadataHolder = new AtomicReference<>(); - TrainedModelMetadata modelMetadata = new TrainedModelMetadata(modelId, + TrainedModelMetadata modelMetadata = new TrainedModelMetadata( + modelId, Collections.emptyList(), new FeatureImportanceBaseline(1.0, Collections.emptyList()), - Collections.emptyList()); - blockingCall(listener -> trainedModelProvider.storeTrainedModelMetadata(modelMetadata, listener), + Collections.emptyList() + ); + blockingCall( + listener -> trainedModelProvider.storeTrainedModelMetadata(modelMetadata, listener), putMetadataHolder, - exceptionHolder); + exceptionHolder + ); assertThat(exceptionHolder.get(), is(nullValue())); AtomicReference refreshResponseAtomicReference = new AtomicReference<>(); - blockingCall(listener -> trainedModelProvider.refreshInferenceIndex(listener), + blockingCall( + listener -> trainedModelProvider.refreshInferenceIndex(listener), refreshResponseAtomicReference, - new AtomicReference<>()); + new AtomicReference<>() + ); AtomicReference getConfigHolder = new AtomicReference<>(); blockingCall( listener -> trainedModelProvider.getTrainedModel(modelId, GetTrainedModelsAction.Includes.forModelDefinition(), listener), getConfigHolder, - exceptionHolder); + exceptionHolder + ); getConfigHolder.get().ensureParsedDefinition(xContentRegistry()); assertThat(getConfigHolder.get(), is(not(nullValue()))); assertThat(getConfigHolder.get(), equalTo(config)); @@ -127,7 +133,8 @@ public void testGetTrainedModelConfig() throws Exception { blockingCall( listener -> trainedModelProvider.getTrainedModel(modelId, GetTrainedModelsAction.Includes.all(), listener), getConfigHolder, - exceptionHolder); + exceptionHolder + ); assertThat(exceptionHolder.get(), is(nullValue())); getConfigHolder.get().ensureParsedDefinition(xContentRegistry()); assertThat(getConfigHolder.get(), is(not(nullValue()))); @@ -163,10 +170,11 @@ public void testGetTrainedModelConfigWithoutDefinition() throws Exception { assertThat(exceptionHolder.get(), is(nullValue())); AtomicReference getConfigHolder = new AtomicReference<>(); - blockingCall(listener -> - trainedModelProvider.getTrainedModel(modelId, GetTrainedModelsAction.Includes.empty(), listener), + blockingCall( + listener -> trainedModelProvider.getTrainedModel(modelId, GetTrainedModelsAction.Includes.empty(), listener), getConfigHolder, - exceptionHolder); + exceptionHolder + ); getConfigHolder.get().ensureParsedDefinition(xContentRegistry()); assertThat(getConfigHolder.get(), is(not(nullValue()))); assertThat(getConfigHolder.get(), equalTo(copyWithoutDefinition)); @@ -180,10 +188,10 @@ public void testGetMissingTrainingModelConfig() throws Exception { blockingCall( listener -> trainedModelProvider.getTrainedModel(modelId, GetTrainedModelsAction.Includes.forModelDefinition(), listener), getConfigHolder, - exceptionHolder); + exceptionHolder + ); assertThat(exceptionHolder.get(), is(not(nullValue()))); - assertThat(exceptionHolder.get().getMessage(), - equalTo(Messages.getMessage(Messages.INFERENCE_NOT_FOUND, modelId))); + assertThat(exceptionHolder.get().getMessage(), equalTo(Messages.getMessage(Messages.INFERENCE_NOT_FOUND, modelId))); } public void testGetMissingTrainingModelConfigDefinition() throws Exception { @@ -196,19 +204,19 @@ public void testGetMissingTrainingModelConfigDefinition() throws Exception { assertThat(putConfigHolder.get(), is(true)); assertThat(exceptionHolder.get(), is(nullValue())); - client().delete(new DeleteRequest(InferenceIndexConstants.LATEST_INDEX_NAME) - .id(TrainedModelDefinitionDoc.docId(config.getModelId(), 0)) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)) - .actionGet(); + client().delete( + new DeleteRequest(InferenceIndexConstants.LATEST_INDEX_NAME).id(TrainedModelDefinitionDoc.docId(config.getModelId(), 0)) + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + ).actionGet(); AtomicReference getConfigHolder = new AtomicReference<>(); blockingCall( listener -> trainedModelProvider.getTrainedModel(modelId, GetTrainedModelsAction.Includes.forModelDefinition(), listener), getConfigHolder, - exceptionHolder); + exceptionHolder + ); assertThat(exceptionHolder.get(), is(not(nullValue()))); - assertThat(exceptionHolder.get().getMessage(), - equalTo(Messages.getMessage(Messages.MODEL_DEFINITION_NOT_FOUND, modelId))); + assertThat(exceptionHolder.get().getMessage(), equalTo(Messages.getMessage(Messages.MODEL_DEFINITION_NOT_FOUND, modelId))); } public void testGetTruncatedModelDeprecatedDefinition() throws Exception { @@ -221,8 +229,7 @@ public void testGetTruncatedModelDeprecatedDefinition() throws Exception { assertThat(putConfigHolder.get(), is(true)); assertThat(exceptionHolder.get(), is(nullValue())); - TrainedModelDefinitionDoc truncatedDoc = new TrainedModelDefinitionDoc.Builder() - .setDocNum(0) + TrainedModelDefinitionDoc truncatedDoc = new TrainedModelDefinitionDoc.Builder().setDocNum(0) .setBinaryData(config.getCompressedDefinition().slice(0, config.getCompressedDefinition().length() - 10)) .setCompressionVersion(TrainedModelConfig.CURRENT_DEFINITION_COMPRESSION_VERSION) .setDefinitionLength(config.getCompressedDefinition().length()) @@ -230,16 +237,22 @@ public void testGetTruncatedModelDeprecatedDefinition() throws Exception { .setModelId(modelId) .build(); - try(XContentBuilder xContentBuilder = truncatedDoc.toXContent(XContentFactory.jsonBuilder(), - new ToXContent.MapParams(Collections.singletonMap(FOR_INTERNAL_STORAGE, "true")))) { + try ( + XContentBuilder xContentBuilder = truncatedDoc.toXContent( + XContentFactory.jsonBuilder(), + new ToXContent.MapParams(Collections.singletonMap(FOR_INTERNAL_STORAGE, "true")) + ) + ) { AtomicReference putDocHolder = new AtomicReference<>(); - blockingCall(listener -> client().prepareIndex(InferenceIndexConstants.LATEST_INDEX_NAME) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .setSource(xContentBuilder) - .setId(TrainedModelDefinitionDoc.docId(modelId, 0)) - .execute(listener), + blockingCall( + listener -> client().prepareIndex(InferenceIndexConstants.LATEST_INDEX_NAME) + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + .setSource(xContentBuilder) + .setId(TrainedModelDefinitionDoc.docId(modelId, 0)) + .execute(listener), putDocHolder, - exceptionHolder); + exceptionHolder + ); assertThat(exceptionHolder.get(), is(nullValue())); } @@ -247,7 +260,8 @@ public void testGetTruncatedModelDeprecatedDefinition() throws Exception { blockingCall( listener -> trainedModelProvider.getTrainedModel(modelId, GetTrainedModelsAction.Includes.forModelDefinition(), listener), getConfigHolder, - exceptionHolder); + exceptionHolder + ); assertThat(getConfigHolder.get(), is(nullValue())); assertThat(exceptionHolder.get(), is(not(nullValue()))); assertThat(exceptionHolder.get().getMessage(), equalTo(Messages.getMessage(Messages.MODEL_DEFINITION_TRUNCATED, modelId))); @@ -274,7 +288,7 @@ public void testGetTruncatedModelDefinition() throws Exception { docBuilders.get(docBuilders.size() - 1).setEos(false); } else { // else write fewer than the expected number of docs - docBuilders.remove(docBuilders.size() -1); + docBuilders.remove(docBuilders.size() - 1); } BulkRequestBuilder bulkRequestBuilder = client().prepareBulk(); for (int i = 0; i < docBuilders.size(); ++i) { @@ -295,12 +309,12 @@ public void testGetTruncatedModelDefinition() throws Exception { assertThat(exceptionHolder.get(), is(nullValue())); assertFalse(putDocsHolder.get().hasFailures()); - AtomicReference getConfigHolder = new AtomicReference<>(); blockingCall( listener -> trainedModelProvider.getTrainedModel(modelId, GetTrainedModelsAction.Includes.forModelDefinition(), listener), getConfigHolder, - exceptionHolder); + exceptionHolder + ); assertThat(getConfigHolder.get(), is(nullValue())); assertThat(exceptionHolder.get(), is(not(nullValue()))); assertThat(exceptionHolder.get().getMessage(), equalTo(Messages.getMessage(Messages.MODEL_DEFINITION_TRUNCATED, modelId))); @@ -341,23 +355,25 @@ public void testGetTrainedModelForInference() throws InterruptedException, IOExc blockingCall( listener -> trainedModelProvider.getTrainedModelForInference(modelId, false, listener), definitionHolder, - exceptionHolder); + exceptionHolder + ); assertThat(exceptionHolder.get(), is(nullValue())); assertThat(definitionHolder.get(), is(not(nullValue()))); } private List createModelDefinitionDocs(BytesReference compressedDefinition, String modelId) { - List chunks = TrainedModelProvider.chunkDefinitionWithSize(compressedDefinition, compressedDefinition.length()/3); + List chunks = TrainedModelProvider.chunkDefinitionWithSize(compressedDefinition, compressedDefinition.length() / 3); return IntStream.range(0, chunks.size()) - .mapToObj(i -> new TrainedModelDefinitionDoc.Builder() - .setDocNum(i) - .setBinaryData(chunks.get(i)) - .setCompressionVersion(TrainedModelConfig.CURRENT_DEFINITION_COMPRESSION_VERSION) - .setDefinitionLength(chunks.get(i).length()) - .setTotalDefinitionLength(compressedDefinition.length()) - .setEos(i == chunks.size() - 1) - .setModelId(modelId)) + .mapToObj( + i -> new TrainedModelDefinitionDoc.Builder().setDocNum(i) + .setBinaryData(chunks.get(i)) + .setCompressionVersion(TrainedModelConfig.CURRENT_DEFINITION_COMPRESSION_VERSION) + .setDefinitionLength(chunks.get(i).length()) + .setTotalDefinitionLength(compressedDefinition.length()) + .setEos(i == chunks.size() - 1) + .setModelId(modelId) + ) .collect(Collectors.toList()); } diff --git a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/UnusedStatsRemoverIT.java b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/UnusedStatsRemoverIT.java index 48b013613d7cb..1bb6877f8c01e 100644 --- a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/UnusedStatsRemoverIT.java +++ b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/UnusedStatsRemoverIT.java @@ -12,13 +12,13 @@ import org.elasticsearch.action.support.master.MasterNodeRequest; import org.elasticsearch.client.OriginSettingClient; import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.indices.TestIndexNameExpressionResolver; +import org.elasticsearch.tasks.TaskId; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.indices.TestIndexNameExpressionResolver; -import org.elasticsearch.tasks.TaskId; import org.elasticsearch.xpack.core.ClientHelper; import org.elasticsearch.xpack.core.ml.MlStatsIndex; import org.elasticsearch.xpack.core.ml.action.PutDataFrameAnalyticsAction; @@ -53,9 +53,13 @@ public class UnusedStatsRemoverIT extends BaseMlIntegTestCase { public void createComponents() { client = new OriginSettingClient(client(), ClientHelper.ML_ORIGIN); PlainActionFuture future = new PlainActionFuture<>(); - MlStatsIndex.createStatsIndexAndAliasIfNecessary(client(), clusterService().state(), + MlStatsIndex.createStatsIndexAndAliasIfNecessary( + client(), + clusterService().state(), TestIndexNameExpressionResolver.newInstance(client().threadPool().getThreadContext()), - MasterNodeRequest.DEFAULT_MASTER_NODE_TIMEOUT, future); + MasterNodeRequest.DEFAULT_MASTER_NODE_TIMEOUT, + future + ); future.actionGet(); } @@ -63,59 +67,52 @@ public void testRemoveUnusedStats() throws Exception { client().prepareIndex("foo").setId("some-empty-doc").setSource("{}", XContentType.JSON).get(); - PutDataFrameAnalyticsAction.Request request = new PutDataFrameAnalyticsAction.Request(new DataFrameAnalyticsConfig.Builder() - .setId("analytics-with-stats") - .setModelMemoryLimit(ByteSizeValue.ofGb(1)) - .setSource(new DataFrameAnalyticsSource(new String[]{"foo"}, null, null, null)) - .setDest(new DataFrameAnalyticsDest("bar", null)) - .setAnalysis(new Regression("prediction")) - .build()); + PutDataFrameAnalyticsAction.Request request = new PutDataFrameAnalyticsAction.Request( + new DataFrameAnalyticsConfig.Builder().setId("analytics-with-stats") + .setModelMemoryLimit(ByteSizeValue.ofGb(1)) + .setSource(new DataFrameAnalyticsSource(new String[] { "foo" }, null, null, null)) + .setDest(new DataFrameAnalyticsDest("bar", null)) + .setAnalysis(new Regression("prediction")) + .build() + ); client.execute(PutDataFrameAnalyticsAction.INSTANCE, request).actionGet(); - client.execute(PutTrainedModelAction.INSTANCE, - new PutTrainedModelAction.Request(TrainedModelConfig.builder() - .setModelId("model-with-stats") - .setInferenceConfig(RegressionConfig.EMPTY_PARAMS) - .setInput(new TrainedModelInput(Arrays.asList("foo", "bar"))) - .setParsedDefinition(new TrainedModelDefinition.Builder() - .setPreProcessors(Collections.emptyList()) - .setTrainedModel(Tree.builder() - .setFeatureNames(Arrays.asList("foo", "bar")) - .setRoot(TreeNode.builder(0).setLeafValue(42)) - .build()) - ) - .validate(true) - .build(), - false)).actionGet(); - - indexStatDocument(new DataCounts("analytics-with-stats", 1, 1, 1), - DataCounts.documentId("analytics-with-stats")); - indexStatDocument(new DataCounts("missing-analytics-with-stats", 1, 1, 1), - DataCounts.documentId("missing-analytics-with-stats")); - indexStatDocument(new InferenceStats(1, - 1, - 1, - 1, - TrainedModelProvider.MODELS_STORED_AS_RESOURCE.iterator().next(), - "test", - Instant.now()), - InferenceStats.docId(TrainedModelProvider.MODELS_STORED_AS_RESOURCE.iterator().next(), "test")); - indexStatDocument(new InferenceStats(1, - 1, - 1, - 1, - "missing-model", - "test", - Instant.now()), - InferenceStats.docId("missing-model", "test")); - indexStatDocument(new InferenceStats(1, - 1, - 1, - 1, - "model-with-stats", - "test", - Instant.now()), - InferenceStats.docId("model-with-stats", "test")); + client.execute( + PutTrainedModelAction.INSTANCE, + new PutTrainedModelAction.Request( + TrainedModelConfig.builder() + .setModelId("model-with-stats") + .setInferenceConfig(RegressionConfig.EMPTY_PARAMS) + .setInput(new TrainedModelInput(Arrays.asList("foo", "bar"))) + .setParsedDefinition( + new TrainedModelDefinition.Builder().setPreProcessors(Collections.emptyList()) + .setTrainedModel( + Tree.builder() + .setFeatureNames(Arrays.asList("foo", "bar")) + .setRoot(TreeNode.builder(0).setLeafValue(42)) + .build() + ) + ) + .validate(true) + .build(), + false + ) + ).actionGet(); + + indexStatDocument(new DataCounts("analytics-with-stats", 1, 1, 1), DataCounts.documentId("analytics-with-stats")); + indexStatDocument(new DataCounts("missing-analytics-with-stats", 1, 1, 1), DataCounts.documentId("missing-analytics-with-stats")); + indexStatDocument( + new InferenceStats(1, 1, 1, 1, TrainedModelProvider.MODELS_STORED_AS_RESOURCE.iterator().next(), "test", Instant.now()), + InferenceStats.docId(TrainedModelProvider.MODELS_STORED_AS_RESOURCE.iterator().next(), "test") + ); + indexStatDocument( + new InferenceStats(1, 1, 1, 1, "missing-model", "test", Instant.now()), + InferenceStats.docId("missing-model", "test") + ); + indexStatDocument( + new InferenceStats(1, 1, 1, 1, "model-with-stats", "test", Instant.now()), + InferenceStats.docId("model-with-stats", "test") + ); client().admin().indices().prepareRefresh(MlStatsIndex.indexPattern()).get(); PlainActionFuture deletionListener = new PlainActionFuture<>(); @@ -128,21 +125,24 @@ public void testRemoveUnusedStats() throws Exception { final String initialStateIndex = MlStatsIndex.TEMPLATE_NAME + "-000001"; // Make sure that stats that should exist still exist - assertTrue(client().prepareGet(initialStateIndex, - InferenceStats.docId("model-with-stats", "test")).get().isExists()); - assertTrue(client().prepareGet(initialStateIndex, - InferenceStats.docId(TrainedModelProvider.MODELS_STORED_AS_RESOURCE.iterator().next(), "test")).get().isExists()); + assertTrue(client().prepareGet(initialStateIndex, InferenceStats.docId("model-with-stats", "test")).get().isExists()); + assertTrue( + client().prepareGet( + initialStateIndex, + InferenceStats.docId(TrainedModelProvider.MODELS_STORED_AS_RESOURCE.iterator().next(), "test") + ).get().isExists() + ); assertTrue(client().prepareGet(initialStateIndex, DataCounts.documentId("analytics-with-stats")).get().isExists()); // make sure that unused stats were deleted assertFalse(client().prepareGet(initialStateIndex, DataCounts.documentId("missing-analytics-with-stats")).get().isExists()); - assertFalse(client().prepareGet(initialStateIndex, - InferenceStats.docId("missing-model", "test")).get().isExists()); + assertFalse(client().prepareGet(initialStateIndex, InferenceStats.docId("missing-model", "test")).get().isExists()); } private void indexStatDocument(ToXContentObject object, String docId) throws Exception { - ToXContent.Params params = new ToXContent.MapParams(Collections.singletonMap(ToXContentParams.FOR_INTERNAL_STORAGE, - Boolean.toString(true))); + ToXContent.Params params = new ToXContent.MapParams( + Collections.singletonMap(ToXContentParams.FOR_INTERNAL_STORAGE, Boolean.toString(true)) + ); IndexRequest doc = new IndexRequest(MlStatsIndex.writeAlias()); doc.id(docId); try (XContentBuilder builder = XContentFactory.jsonBuilder()) { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/InvalidLicenseEnforcer.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/InvalidLicenseEnforcer.java index c280f96c3bfe3..ecef840e79434 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/InvalidLicenseEnforcer.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/InvalidLicenseEnforcer.java @@ -28,8 +28,12 @@ public class InvalidLicenseEnforcer implements LicenseStateListener { private volatile boolean licenseStateListenerRegistered; - InvalidLicenseEnforcer(XPackLicenseState licenseState, ThreadPool threadPool, - DatafeedRunner datafeedRunner, AutodetectProcessManager autodetectProcessManager) { + InvalidLicenseEnforcer( + XPackLicenseState licenseState, + ThreadPool threadPool, + DatafeedRunner datafeedRunner, + AutodetectProcessManager autodetectProcessManager + ) { this.threadPool = threadPool; this.licenseState = licenseState; this.datafeedRunner = datafeedRunner; diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MachineLearning.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MachineLearning.java index d311e5f3aea13..29e58bbb48be2 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MachineLearning.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MachineLearning.java @@ -29,9 +29,8 @@ import org.elasticsearch.cluster.node.DiscoveryNodeRole; import org.elasticsearch.cluster.node.DiscoveryNodes; import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.common.collect.MapBuilder; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.breaker.CircuitBreaker; +import org.elasticsearch.common.collect.MapBuilder; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.settings.ClusterSettings; import org.elasticsearch.common.settings.IndexScopedSettings; @@ -41,9 +40,8 @@ import org.elasticsearch.common.settings.SettingsFilter; import org.elasticsearch.common.settings.SettingsModule; import org.elasticsearch.common.unit.ByteSizeValue; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.EsExecutors; -import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.env.Environment; import org.elasticsearch.env.NodeEnvironment; import org.elasticsearch.index.analysis.CharFilterFactory; @@ -77,6 +75,8 @@ import org.elasticsearch.threadpool.ScalingExecutorBuilder; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.watcher.ResourceWatcherService; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xpack.autoscaling.capacity.AutoscalingDeciderService; import org.elasticsearch.xpack.core.XPackPlugin; import org.elasticsearch.xpack.core.XPackSettings; @@ -103,12 +103,6 @@ import org.elasticsearch.xpack.core.ml.action.DeleteTrainedModelAction; import org.elasticsearch.xpack.core.ml.action.DeleteTrainedModelAliasAction; import org.elasticsearch.xpack.core.ml.action.DeleteTrainedModelAllocationAction; -import org.elasticsearch.xpack.core.ml.action.GetDatafeedRunningStateAction; -import org.elasticsearch.xpack.core.ml.action.GetDeploymentStatsAction; -import org.elasticsearch.xpack.core.ml.action.InferTrainedModelDeploymentAction; -import org.elasticsearch.xpack.core.ml.action.PutTrainedModelDefinitionPartAction; -import org.elasticsearch.xpack.core.ml.action.PutTrainedModelVocabularyAction; -import org.elasticsearch.xpack.core.ml.action.StartTrainedModelDeploymentAction; import org.elasticsearch.xpack.core.ml.action.EstimateModelMemoryAction; import org.elasticsearch.xpack.core.ml.action.EvaluateDataFrameAction; import org.elasticsearch.xpack.core.ml.action.ExplainDataFrameAnalyticsAction; @@ -121,8 +115,10 @@ import org.elasticsearch.xpack.core.ml.action.GetCategoriesAction; import org.elasticsearch.xpack.core.ml.action.GetDataFrameAnalyticsAction; import org.elasticsearch.xpack.core.ml.action.GetDataFrameAnalyticsStatsAction; +import org.elasticsearch.xpack.core.ml.action.GetDatafeedRunningStateAction; import org.elasticsearch.xpack.core.ml.action.GetDatafeedsAction; import org.elasticsearch.xpack.core.ml.action.GetDatafeedsStatsAction; +import org.elasticsearch.xpack.core.ml.action.GetDeploymentStatsAction; import org.elasticsearch.xpack.core.ml.action.GetFiltersAction; import org.elasticsearch.xpack.core.ml.action.GetInfluencersAction; import org.elasticsearch.xpack.core.ml.action.GetJobsAction; @@ -132,6 +128,7 @@ import org.elasticsearch.xpack.core.ml.action.GetRecordsAction; import org.elasticsearch.xpack.core.ml.action.GetTrainedModelsAction; import org.elasticsearch.xpack.core.ml.action.GetTrainedModelsStatsAction; +import org.elasticsearch.xpack.core.ml.action.InferTrainedModelDeploymentAction; import org.elasticsearch.xpack.core.ml.action.InternalInferModelAction; import org.elasticsearch.xpack.core.ml.action.IsolateDatafeedAction; import org.elasticsearch.xpack.core.ml.action.KillProcessAction; @@ -149,12 +146,15 @@ import org.elasticsearch.xpack.core.ml.action.PutJobAction; import org.elasticsearch.xpack.core.ml.action.PutTrainedModelAction; import org.elasticsearch.xpack.core.ml.action.PutTrainedModelAliasAction; +import org.elasticsearch.xpack.core.ml.action.PutTrainedModelDefinitionPartAction; +import org.elasticsearch.xpack.core.ml.action.PutTrainedModelVocabularyAction; import org.elasticsearch.xpack.core.ml.action.ResetJobAction; import org.elasticsearch.xpack.core.ml.action.RevertModelSnapshotAction; import org.elasticsearch.xpack.core.ml.action.SetResetModeAction; import org.elasticsearch.xpack.core.ml.action.SetUpgradeModeAction; import org.elasticsearch.xpack.core.ml.action.StartDataFrameAnalyticsAction; import org.elasticsearch.xpack.core.ml.action.StartDatafeedAction; +import org.elasticsearch.xpack.core.ml.action.StartTrainedModelDeploymentAction; import org.elasticsearch.xpack.core.ml.action.StopDataFrameAnalyticsAction; import org.elasticsearch.xpack.core.ml.action.StopDatafeedAction; import org.elasticsearch.xpack.core.ml.action.StopTrainedModelDeploymentAction; @@ -195,12 +195,6 @@ import org.elasticsearch.xpack.ml.action.TransportDeleteTrainedModelAction; import org.elasticsearch.xpack.ml.action.TransportDeleteTrainedModelAliasAction; import org.elasticsearch.xpack.ml.action.TransportDeleteTrainedModelAllocationAction; -import org.elasticsearch.xpack.ml.action.TransportGetDatafeedRunningStateAction; -import org.elasticsearch.xpack.ml.action.TransportGetDeploymentStatsAction; -import org.elasticsearch.xpack.ml.action.TransportInferTrainedModelDeploymentAction; -import org.elasticsearch.xpack.ml.action.TransportPutTrainedModelDefinitionPartAction; -import org.elasticsearch.xpack.ml.action.TransportPutTrainedModelVocabularyAction; -import org.elasticsearch.xpack.ml.action.TransportStartTrainedModelDeploymentAction; import org.elasticsearch.xpack.ml.action.TransportEstimateModelMemoryAction; import org.elasticsearch.xpack.ml.action.TransportEvaluateDataFrameAction; import org.elasticsearch.xpack.ml.action.TransportExplainDataFrameAnalyticsAction; @@ -213,8 +207,10 @@ import org.elasticsearch.xpack.ml.action.TransportGetCategoriesAction; import org.elasticsearch.xpack.ml.action.TransportGetDataFrameAnalyticsAction; import org.elasticsearch.xpack.ml.action.TransportGetDataFrameAnalyticsStatsAction; +import org.elasticsearch.xpack.ml.action.TransportGetDatafeedRunningStateAction; import org.elasticsearch.xpack.ml.action.TransportGetDatafeedsAction; import org.elasticsearch.xpack.ml.action.TransportGetDatafeedsStatsAction; +import org.elasticsearch.xpack.ml.action.TransportGetDeploymentStatsAction; import org.elasticsearch.xpack.ml.action.TransportGetFiltersAction; import org.elasticsearch.xpack.ml.action.TransportGetInfluencersAction; import org.elasticsearch.xpack.ml.action.TransportGetJobsAction; @@ -224,6 +220,7 @@ import org.elasticsearch.xpack.ml.action.TransportGetRecordsAction; import org.elasticsearch.xpack.ml.action.TransportGetTrainedModelsAction; import org.elasticsearch.xpack.ml.action.TransportGetTrainedModelsStatsAction; +import org.elasticsearch.xpack.ml.action.TransportInferTrainedModelDeploymentAction; import org.elasticsearch.xpack.ml.action.TransportInternalInferModelAction; import org.elasticsearch.xpack.ml.action.TransportIsolateDatafeedAction; import org.elasticsearch.xpack.ml.action.TransportKillProcessAction; @@ -241,12 +238,15 @@ import org.elasticsearch.xpack.ml.action.TransportPutJobAction; import org.elasticsearch.xpack.ml.action.TransportPutTrainedModelAction; import org.elasticsearch.xpack.ml.action.TransportPutTrainedModelAliasAction; +import org.elasticsearch.xpack.ml.action.TransportPutTrainedModelDefinitionPartAction; +import org.elasticsearch.xpack.ml.action.TransportPutTrainedModelVocabularyAction; import org.elasticsearch.xpack.ml.action.TransportResetJobAction; import org.elasticsearch.xpack.ml.action.TransportRevertModelSnapshotAction; import org.elasticsearch.xpack.ml.action.TransportSetResetModeAction; import org.elasticsearch.xpack.ml.action.TransportSetUpgradeModeAction; import org.elasticsearch.xpack.ml.action.TransportStartDataFrameAnalyticsAction; import org.elasticsearch.xpack.ml.action.TransportStartDatafeedAction; +import org.elasticsearch.xpack.ml.action.TransportStartTrainedModelDeploymentAction; import org.elasticsearch.xpack.ml.action.TransportStopDataFrameAnalyticsAction; import org.elasticsearch.xpack.ml.action.TransportStopDatafeedAction; import org.elasticsearch.xpack.ml.action.TransportStopTrainedModelDeploymentAction; @@ -266,8 +266,8 @@ import org.elasticsearch.xpack.ml.aggs.correlation.BucketCorrelationAggregationBuilder; import org.elasticsearch.xpack.ml.aggs.correlation.CorrelationNamedContentProvider; import org.elasticsearch.xpack.ml.aggs.heuristic.PValueScore; -import org.elasticsearch.xpack.ml.aggs.kstest.BucketCountKSTestAggregationBuilder; import org.elasticsearch.xpack.ml.aggs.inference.InferencePipelineAggregationBuilder; +import org.elasticsearch.xpack.ml.aggs.kstest.BucketCountKSTestAggregationBuilder; import org.elasticsearch.xpack.ml.annotations.AnnotationPersister; import org.elasticsearch.xpack.ml.autoscaling.MlAutoscalingDeciderService; import org.elasticsearch.xpack.ml.autoscaling.MlAutoscalingNamedWritableProvider; @@ -375,14 +375,14 @@ import org.elasticsearch.xpack.ml.rest.inference.RestDeleteTrainedModelAction; import org.elasticsearch.xpack.ml.rest.inference.RestDeleteTrainedModelAliasAction; import org.elasticsearch.xpack.ml.rest.inference.RestGetTrainedModelDeploymentStatsAction; -import org.elasticsearch.xpack.ml.rest.inference.RestInferTrainedModelDeploymentAction; -import org.elasticsearch.xpack.ml.rest.inference.RestPutTrainedModelDefinitionPartAction; -import org.elasticsearch.xpack.ml.rest.inference.RestPutTrainedModelVocabularyAction; -import org.elasticsearch.xpack.ml.rest.inference.RestStartTrainedModelDeploymentAction; import org.elasticsearch.xpack.ml.rest.inference.RestGetTrainedModelsAction; import org.elasticsearch.xpack.ml.rest.inference.RestGetTrainedModelsStatsAction; +import org.elasticsearch.xpack.ml.rest.inference.RestInferTrainedModelDeploymentAction; import org.elasticsearch.xpack.ml.rest.inference.RestPutTrainedModelAction; import org.elasticsearch.xpack.ml.rest.inference.RestPutTrainedModelAliasAction; +import org.elasticsearch.xpack.ml.rest.inference.RestPutTrainedModelDefinitionPartAction; +import org.elasticsearch.xpack.ml.rest.inference.RestPutTrainedModelVocabularyAction; +import org.elasticsearch.xpack.ml.rest.inference.RestStartTrainedModelDeploymentAction; import org.elasticsearch.xpack.ml.rest.inference.RestStopTrainedModelDeploymentAction; import org.elasticsearch.xpack.ml.rest.job.RestCloseJobAction; import org.elasticsearch.xpack.ml.rest.job.RestDeleteForecastAction; @@ -430,13 +430,15 @@ import static org.elasticsearch.xpack.core.ml.job.persistence.AnomalyDetectorsIndexFields.STATE_INDEX_PREFIX; import static org.elasticsearch.xpack.ml.inference.ingest.InferenceProcessor.Factory.countNumberInferenceProcessors; -public class MachineLearning extends Plugin implements SystemIndexPlugin, - AnalysisPlugin, - CircuitBreakerPlugin, - IngestPlugin, - PersistentTaskPlugin, - SearchPlugin, - ShutdownAwarePlugin { +public class MachineLearning extends Plugin + implements + SystemIndexPlugin, + AnalysisPlugin, + CircuitBreakerPlugin, + IngestPlugin, + PersistentTaskPlugin, + SearchPlugin, + ShutdownAwarePlugin { public static final String NAME = "ml"; public static final String BASE_PATH = "/_ml/"; // Endpoints that were deprecated in 7.x can still be called in 8.x using the REST compatibility layer @@ -447,9 +449,9 @@ public class MachineLearning extends Plugin implements SystemIndexPlugin, public static final String TRAINED_MODEL_CIRCUIT_BREAKER_NAME = "model_inference"; - private static final long DEFAULT_MODEL_CIRCUIT_BREAKER_LIMIT = (long)((0.50) * JvmInfo.jvmInfo().getMem().getHeapMax().getBytes()); + private static final long DEFAULT_MODEL_CIRCUIT_BREAKER_LIMIT = (long) ((0.50) * JvmInfo.jvmInfo().getMem().getHeapMax().getBytes()); private static final double DEFAULT_MODEL_CIRCUIT_BREAKER_OVERHEAD = 1.0D; - // This is for performance testing. It's not exposed to the end user. + // This is for performance testing. It's not exposed to the end user. // Recompile if you want to compare performance with C++ tokenization. public static final boolean CATEGORIZATION_TOKENIZATION_IN_JAVA = true; @@ -480,9 +482,11 @@ public Map getProcessors(Processor.Parameters paramet return Collections.emptyMap(); } - InferenceProcessor.Factory inferenceFactory = new InferenceProcessor.Factory(parameters.client, + InferenceProcessor.Factory inferenceFactory = new InferenceProcessor.Factory( + parameters.client, parameters.ingestService.getClusterService(), - this.settings); + this.settings + ); parameters.ingestService.addIngestClusterStateListener(inferenceFactory); return Collections.singletonMap(InferenceProcessor.TYPE, inferenceFactory); } @@ -491,8 +495,13 @@ public Map getProcessors(Processor.Parameters paramet private static final String PRE_V8_MAX_OPEN_JOBS_NODE_ATTR = "ml.max_open_jobs"; public static final String MACHINE_MEMORY_NODE_ATTR = "ml.machine_memory"; public static final String MAX_JVM_SIZE_NODE_ATTR = "ml.max_jvm_size"; - public static final Setting CONCURRENT_JOB_ALLOCATIONS = - Setting.intSetting("xpack.ml.node_concurrent_job_allocations", 2, 0, Property.OperatorDynamic, Property.NodeScope); + public static final Setting CONCURRENT_JOB_ALLOCATIONS = Setting.intSetting( + "xpack.ml.node_concurrent_job_allocations", + 2, + 0, + Property.OperatorDynamic, + Property.NodeScope + ); /** * The amount of memory needed to load the ML native code shared libraries. The assumption is that the first * ML job to run on a given node will do this, and then subsequent ML jobs on the same node will reuse the @@ -505,8 +514,14 @@ public Map getProcessors(Processor.Parameters paramet // Settings higher than available memory are only recommended for OEM type situations where a wrapper tightly // controls the types of jobs that can be created, and each job alone is considerably smaller than what each node // can handle. - public static final Setting MAX_MACHINE_MEMORY_PERCENT = - Setting.intSetting("xpack.ml.max_machine_memory_percent", 30, 5, 200, Property.OperatorDynamic, Property.NodeScope); + public static final Setting MAX_MACHINE_MEMORY_PERCENT = Setting.intSetting( + "xpack.ml.max_machine_memory_percent", + 30, + 5, + 200, + Property.OperatorDynamic, + Property.NodeScope + ); /** * This boolean value indicates if `max_machine_memory_percent` should be ignored and a automatic calculation is used instead. * @@ -523,53 +538,67 @@ public Map getProcessors(Processor.Parameters paramet "xpack.ml.use_auto_machine_memory_percent", false, Property.OperatorDynamic, - Property.NodeScope); - public static final Setting MAX_LAZY_ML_NODES = - Setting.intSetting("xpack.ml.max_lazy_ml_nodes", 0, 0, Property.OperatorDynamic, Property.NodeScope); + Property.NodeScope + ); + public static final Setting MAX_LAZY_ML_NODES = Setting.intSetting( + "xpack.ml.max_lazy_ml_nodes", + 0, + 0, + Property.OperatorDynamic, + Property.NodeScope + ); // Before 8.0.0 this needs to match the max allowed value for xpack.ml.max_open_jobs, // as the current node could be running in a cluster where some nodes are still using - // that setting. From 8.0.0 onwards we have the flexibility to increase it... + // that setting. From 8.0.0 onwards we have the flexibility to increase it... private static final int MAX_MAX_OPEN_JOBS_PER_NODE = 512; public static final int DEFAULT_MAX_OPEN_JOBS_PER_NODE = MAX_MAX_OPEN_JOBS_PER_NODE; // This setting is cluster-wide and can be set dynamically. However, prior to version 7.1 it was // a non-dynamic per-node setting. n a mixed version cluster containing 6.7 or 7.0 nodes those // older nodes will not react to the dynamic changes. Therefore, in such mixed version clusters // allocation will be based on the value first read at node startup rather than the current value. - public static final Setting MAX_OPEN_JOBS_PER_NODE = - Setting.intSetting( - "xpack.ml.max_open_jobs", - DEFAULT_MAX_OPEN_JOBS_PER_NODE, - 1, - MAX_MAX_OPEN_JOBS_PER_NODE, - Property.Dynamic, - Property.NodeScope - ); + public static final Setting MAX_OPEN_JOBS_PER_NODE = Setting.intSetting( + "xpack.ml.max_open_jobs", + DEFAULT_MAX_OPEN_JOBS_PER_NODE, + 1, + MAX_MAX_OPEN_JOBS_PER_NODE, + Property.Dynamic, + Property.NodeScope + ); - public static final Setting PROCESS_CONNECT_TIMEOUT = - Setting.timeSetting("xpack.ml.process_connect_timeout", TimeValue.timeValueSeconds(10), - TimeValue.timeValueSeconds(5), Property.OperatorDynamic, Setting.Property.NodeScope); + public static final Setting PROCESS_CONNECT_TIMEOUT = Setting.timeSetting( + "xpack.ml.process_connect_timeout", + TimeValue.timeValueSeconds(10), + TimeValue.timeValueSeconds(5), + Property.OperatorDynamic, + Setting.Property.NodeScope + ); // Undocumented setting for integration test purposes - public static final Setting MIN_DISK_SPACE_OFF_HEAP = - Setting.byteSizeSetting("xpack.ml.min_disk_space_off_heap", ByteSizeValue.ofGb(5), Setting.Property.NodeScope); + public static final Setting MIN_DISK_SPACE_OFF_HEAP = Setting.byteSizeSetting( + "xpack.ml.min_disk_space_off_heap", + ByteSizeValue.ofGb(5), + Setting.Property.NodeScope + ); // Requests per second throttling for the nightly maintenance task - public static final Setting NIGHTLY_MAINTENANCE_REQUESTS_PER_SECOND = - new Setting<>( - "xpack.ml.nightly_maintenance_requests_per_second", - (s) -> Float.toString(-1.0f), - (s) -> { - float value = Float.parseFloat(s); - if (value <= 0.0f && value != -1.0f) { - throw new IllegalArgumentException("Failed to parse value [" + - s + "] for setting [xpack.ml.nightly_maintenance_requests_per_second] must be > 0.0 or exactly equal to -1.0"); - } - return value; - }, - Property.OperatorDynamic, - Property.NodeScope - ); + public static final Setting NIGHTLY_MAINTENANCE_REQUESTS_PER_SECOND = new Setting<>( + "xpack.ml.nightly_maintenance_requests_per_second", + (s) -> Float.toString(-1.0f), + (s) -> { + float value = Float.parseFloat(s); + if (value <= 0.0f && value != -1.0f) { + throw new IllegalArgumentException( + "Failed to parse value [" + + s + + "] for setting [xpack.ml.nightly_maintenance_requests_per_second] must be > 0.0 or exactly equal to -1.0" + ); + } + return value; + }, + Property.OperatorDynamic, + Property.NodeScope + ); /** * This is the maximum possible node size for a machine learning node. It is useful when determining if a job could ever be opened @@ -581,7 +610,8 @@ public Map getProcessors(Processor.Parameters paramet "xpack.ml.max_ml_node_size", ByteSizeValue.ZERO, Property.OperatorDynamic, - Property.NodeScope); + Property.NodeScope + ); /** * This is the global setting for how often datafeeds should check for delayed data. @@ -589,12 +619,12 @@ public Map getProcessors(Processor.Parameters paramet * This is usually only modified by tests that require all datafeeds to check for delayed data more quickly */ public static final Setting DELAYED_DATA_CHECK_FREQ = Setting.timeSetting( - "xpack.ml.delayed_data_check_freq", - TimeValue.timeValueMinutes(15), - TimeValue.timeValueSeconds(1), - Property.Dynamic, - Setting.Property.NodeScope - ); + "xpack.ml.delayed_data_check_freq", + TimeValue.timeValueMinutes(15), + TimeValue.timeValueSeconds(1), + Property.Dynamic, + Setting.Property.NodeScope + ); private static final Logger logger = LogManager.getLogger(MachineLearning.class); @@ -620,7 +650,9 @@ public MachineLearning(Settings settings) { this.enabled = XPackSettings.MACHINE_LEARNING_ENABLED.get(settings); } - protected XPackLicenseState getLicenseState() { return XPackPlugin.getSharedLicenseState(); } + protected XPackLicenseState getLicenseState() { + return XPackPlugin.getSharedLicenseState(); + } public static boolean isMlNode(DiscoveryNode node) { logger.info("DMR node roles are " + node.getRoles()); @@ -629,25 +661,25 @@ public static boolean isMlNode(DiscoveryNode node) { public List> getSettings() { return List.of( - MachineLearningField.AUTODETECT_PROCESS, - PROCESS_CONNECT_TIMEOUT, - CONCURRENT_JOB_ALLOCATIONS, - MachineLearningField.MAX_MODEL_MEMORY_LIMIT, - MAX_LAZY_ML_NODES, - MAX_MACHINE_MEMORY_PERCENT, - AutodetectBuilder.MAX_ANOMALY_RECORDS_SETTING_DYNAMIC, - MAX_OPEN_JOBS_PER_NODE, - MIN_DISK_SPACE_OFF_HEAP, - MlConfigMigrationEligibilityCheck.ENABLE_CONFIG_MIGRATION, - InferenceProcessor.MAX_INFERENCE_PROCESSORS, - ModelLoadingService.INFERENCE_MODEL_CACHE_SIZE, - ModelLoadingService.INFERENCE_MODEL_CACHE_TTL, - ResultsPersisterService.PERSIST_RESULTS_MAX_RETRIES, - NIGHTLY_MAINTENANCE_REQUESTS_PER_SECOND, - USE_AUTO_MACHINE_MEMORY_PERCENT, - MAX_ML_NODE_SIZE, - DELAYED_DATA_CHECK_FREQ - ); + MachineLearningField.AUTODETECT_PROCESS, + PROCESS_CONNECT_TIMEOUT, + CONCURRENT_JOB_ALLOCATIONS, + MachineLearningField.MAX_MODEL_MEMORY_LIMIT, + MAX_LAZY_ML_NODES, + MAX_MACHINE_MEMORY_PERCENT, + AutodetectBuilder.MAX_ANOMALY_RECORDS_SETTING_DYNAMIC, + MAX_OPEN_JOBS_PER_NODE, + MIN_DISK_SPACE_OFF_HEAP, + MlConfigMigrationEligibilityCheck.ENABLE_CONFIG_MIGRATION, + InferenceProcessor.MAX_INFERENCE_PROCESSORS, + ModelLoadingService.INFERENCE_MODEL_CACHE_SIZE, + ModelLoadingService.INFERENCE_MODEL_CACHE_TTL, + ResultsPersisterService.PERSIST_RESULTS_MAX_RETRIES, + NIGHTLY_MAINTENANCE_REQUESTS_PER_SECOND, + USE_AUTO_MACHINE_MEMORY_PERCENT, + MAX_ML_NODE_SIZE, + DELAYED_DATA_CHECK_FREQ + ); } public Settings additionalSettings() { @@ -662,17 +694,16 @@ public Settings additionalSettings() { Settings.Builder additionalSettings = Settings.builder(); if (DiscoveryNode.hasRole(settings, DiscoveryNodeRole.ML_ROLE)) { - addMlNodeAttribute(additionalSettings, machineMemoryAttrName, - Long.toString(OsProbe.getInstance().osStats().getMem().getAdjustedTotal().getBytes())); + addMlNodeAttribute( + additionalSettings, + machineMemoryAttrName, + Long.toString(OsProbe.getInstance().osStats().getMem().getAdjustedTotal().getBytes()) + ); addMlNodeAttribute(additionalSettings, jvmSizeAttrName, Long.toString(Runtime.getRuntime().maxMemory())); // This is not used in v8 and higher, but users are still prevented from setting it directly to avoid confusion disallowMlNodeAttributes(maxOpenJobsPerNodeNodeAttrName); } else { - disallowMlNodeAttributes( - maxOpenJobsPerNodeNodeAttrName, - machineMemoryAttrName, - jvmSizeAttrName - ); + disallowMlNodeAttributes(maxOpenJobsPerNodeNodeAttrName, machineMemoryAttrName, jvmSizeAttrName); } return additionalSettings.build(); } @@ -695,9 +726,14 @@ private void disallowMlNodeAttributes(String... mlNodeAttributes) { } private void reportClashingNodeAttribute(String attrName) { - throw new IllegalArgumentException("Directly setting [" + attrName + "] is not permitted - " + - "it is reserved for machine learning. If your intention was to customize machine learning, set the [" + - attrName.replace("node.attr.", "xpack.") + "] setting instead."); + throw new IllegalArgumentException( + "Directly setting [" + + attrName + + "] is not permitted - " + + "it is reserved for machine learning. If your intention was to customize machine learning, set the [" + + attrName.replace("node.attr.", "xpack.") + + "] setting instead." + ); } // overridable by tests @@ -705,14 +741,20 @@ protected Clock getClock() { return Clock.systemUTC(); } - @Override - public Collection createComponents(Client client, ClusterService clusterService, ThreadPool threadPool, - ResourceWatcherService resourceWatcherService, ScriptService scriptService, - NamedXContentRegistry xContentRegistry, Environment environment, - NodeEnvironment nodeEnvironment, NamedWriteableRegistry namedWriteableRegistry, - IndexNameExpressionResolver indexNameExpressionResolver, - Supplier repositoriesServiceSupplier) { + public Collection createComponents( + Client client, + ClusterService clusterService, + ThreadPool threadPool, + ResourceWatcherService resourceWatcherService, + ScriptService scriptService, + NamedXContentRegistry xContentRegistry, + Environment environment, + NodeEnvironment nodeEnvironment, + NamedWriteableRegistry namedWriteableRegistry, + IndexNameExpressionResolver indexNameExpressionResolver, + Supplier repositoriesServiceSupplier + ) { if (enabled == false) { // special holder for @link(MachineLearningFeatureSetUsage) which needs access to job manager, empty if ML is disabled return singletonList(new JobManagerHolder()); @@ -737,14 +779,17 @@ public Collection createComponents(Client client, ClusterService cluster AnnotationPersister anomalyDetectionAnnotationPersister = new AnnotationPersister(resultsPersisterService); JobResultsProvider jobResultsProvider = new JobResultsProvider(client, settings, indexNameExpressionResolver); JobResultsPersister jobResultsPersister = new JobResultsPersister(originSettingClient, resultsPersisterService); - JobDataCountsPersister jobDataCountsPersister = new JobDataCountsPersister(client, + JobDataCountsPersister jobDataCountsPersister = new JobDataCountsPersister( + client, resultsPersisterService, - anomalyDetectionAuditor); + anomalyDetectionAuditor + ); JobConfigProvider jobConfigProvider = new JobConfigProvider(client, xContentRegistry); DatafeedConfigProvider datafeedConfigProvider = new DatafeedConfigProvider(client, xContentRegistry); this.datafeedConfigProvider.set(datafeedConfigProvider); UpdateJobProcessNotifier notifier = new UpdateJobProcessNotifier(client, clusterService, threadPool); - JobManager jobManager = new JobManager(environment, + JobManager jobManager = new JobManager( + environment, settings, jobResultsProvider, jobResultsPersister, @@ -778,15 +823,19 @@ public Collection createComponents(Client client, ClusterService cluster final PyTorchProcessFactory pyTorchProcessFactory; if (MachineLearningField.AUTODETECT_PROCESS.get(settings)) { try { - NativeController nativeController = - NativeController.makeNativeController(clusterService.getNodeName(), environment, xContentRegistry); + NativeController nativeController = NativeController.makeNativeController( + clusterService.getNodeName(), + environment, + xContentRegistry + ); autodetectProcessFactory = new NativeAutodetectProcessFactory( environment, settings, nativeController, clusterService, resultsPersisterService, - anomalyDetectionAuditor); + anomalyDetectionAuditor + ); normalizerProcessFactory = new NativeNormalizerProcessFactory(environment, nativeController, clusterService); analyticsProcessFactory = new NativeAnalyticsProcessFactory( environment, @@ -794,62 +843,102 @@ public Collection createComponents(Client client, ClusterService cluster clusterService, xContentRegistry, resultsPersisterService, - dataFrameAnalyticsAuditor); - memoryEstimationProcessFactory = - new NativeMemoryUsageEstimationProcessFactory(environment, nativeController, clusterService); + dataFrameAnalyticsAuditor + ); + memoryEstimationProcessFactory = new NativeMemoryUsageEstimationProcessFactory( + environment, + nativeController, + clusterService + ); pyTorchProcessFactory = new NativePyTorchProcessFactory(environment, nativeController, clusterService); mlController = nativeController; } catch (IOException e) { // The low level cause of failure from the named pipe helper's perspective is almost never the real root cause, so - // only log this at the lowest level of detail. It's almost always "file not found" on a named pipe we expect to be + // only log this at the lowest level of detail. It's almost always "file not found" on a named pipe we expect to be // able to connect to, but the thing we really need to know is what stopped the native process creating the named pipe. logger.trace("Failed to connect to ML native controller", e); - throw new ElasticsearchException("Failure running machine learning native code. This could be due to running " - + "on an unsupported OS or distribution, missing OS libraries, or a problem with the temp directory. To " - + "bypass this problem by running Elasticsearch without machine learning functionality set [" - + XPackSettings.MACHINE_LEARNING_ENABLED.getKey() + ": false]."); + throw new ElasticsearchException( + "Failure running machine learning native code. This could be due to running " + + "on an unsupported OS or distribution, missing OS libraries, or a problem with the temp directory. To " + + "bypass this problem by running Elasticsearch without machine learning functionality set [" + + XPackSettings.MACHINE_LEARNING_ENABLED.getKey() + + ": false]." + ); } } else { mlController = new DummyController(); - autodetectProcessFactory = (pipelineId, job, autodetectParams, executorService, onProcessCrash) -> - new BlackHoleAutodetectProcess(pipelineId, onProcessCrash); + autodetectProcessFactory = ( + pipelineId, + job, + autodetectParams, + executorService, + onProcessCrash) -> new BlackHoleAutodetectProcess(pipelineId, onProcessCrash); // factor of 1.0 makes renormalization a no-op normalizerProcessFactory = (jobId, quantilesState, bucketSpan, executorService) -> new MultiplyingNormalizerProcess(1.0); analyticsProcessFactory = (jobId, analyticsProcessConfig, hasState, executorService, onProcessCrash) -> null; memoryEstimationProcessFactory = (jobId, analyticsProcessConfig, hasState, executorService, onProcessCrash) -> null; pyTorchProcessFactory = (task, executorService, onProcessCrash) -> null; } - NormalizerFactory normalizerFactory = new NormalizerFactory(normalizerProcessFactory, - threadPool.executor(MachineLearning.UTILITY_THREAD_POOL_NAME)); - AutodetectProcessManager autodetectProcessManager = new AutodetectProcessManager(settings, client, threadPool, - xContentRegistry, anomalyDetectionAuditor, clusterService, jobManager, jobResultsProvider, jobResultsPersister, - jobDataCountsPersister, anomalyDetectionAnnotationPersister, autodetectProcessFactory, - normalizerFactory, nativeStorageProvider, indexNameExpressionResolver); + NormalizerFactory normalizerFactory = new NormalizerFactory( + normalizerProcessFactory, + threadPool.executor(MachineLearning.UTILITY_THREAD_POOL_NAME) + ); + AutodetectProcessManager autodetectProcessManager = new AutodetectProcessManager( + settings, + client, + threadPool, + xContentRegistry, + anomalyDetectionAuditor, + clusterService, + jobManager, + jobResultsProvider, + jobResultsPersister, + jobDataCountsPersister, + anomalyDetectionAnnotationPersister, + autodetectProcessFactory, + normalizerFactory, + nativeStorageProvider, + indexNameExpressionResolver + ); this.autodetectProcessManager.set(autodetectProcessManager); DatafeedJobBuilder datafeedJobBuilder = new DatafeedJobBuilder( - client, - xContentRegistry, - anomalyDetectionAuditor, - anomalyDetectionAnnotationPersister, - System::currentTimeMillis, - jobResultsPersister, - settings, - clusterService + client, + xContentRegistry, + anomalyDetectionAuditor, + anomalyDetectionAnnotationPersister, + System::currentTimeMillis, + jobResultsPersister, + settings, + clusterService + ); + DatafeedContextProvider datafeedContextProvider = new DatafeedContextProvider( + jobConfigProvider, + datafeedConfigProvider, + jobResultsProvider + ); + DatafeedRunner datafeedRunner = new DatafeedRunner( + threadPool, + client, + clusterService, + datafeedJobBuilder, + System::currentTimeMillis, + anomalyDetectionAuditor, + autodetectProcessManager, + datafeedContextProvider ); - DatafeedContextProvider datafeedContextProvider = new DatafeedContextProvider(jobConfigProvider, datafeedConfigProvider, - jobResultsProvider); - DatafeedRunner datafeedRunner = new DatafeedRunner(threadPool, client, clusterService, datafeedJobBuilder, - System::currentTimeMillis, anomalyDetectionAuditor, autodetectProcessManager, datafeedContextProvider); this.datafeedRunner.set(datafeedRunner); // Inference components - final TrainedModelStatsService trainedModelStatsService = new TrainedModelStatsService(resultsPersisterService, + final TrainedModelStatsService trainedModelStatsService = new TrainedModelStatsService( + resultsPersisterService, originSettingClient, indexNameExpressionResolver, clusterService, - threadPool); + threadPool + ); final TrainedModelProvider trainedModelProvider = new TrainedModelProvider(client, xContentRegistry); - final ModelLoadingService modelLoadingService = new ModelLoadingService(trainedModelProvider, + final ModelLoadingService modelLoadingService = new ModelLoadingService( + trainedModelProvider, inferenceAuditor, threadPool, clusterService, @@ -871,35 +960,71 @@ public Collection createComponents(Client client, ClusterService cluster dataFrameAnalyticsAuditor, trainedModelProvider, resultsPersisterService, - EsExecutors.allocatedProcessors(settings)); - MemoryUsageEstimationProcessManager memoryEstimationProcessManager = - new MemoryUsageEstimationProcessManager( - threadPool.generic(), threadPool.executor(UTILITY_THREAD_POOL_NAME), memoryEstimationProcessFactory); - DataFrameAnalyticsConfigProvider dataFrameAnalyticsConfigProvider = new DataFrameAnalyticsConfigProvider(client, xContentRegistry, - dataFrameAnalyticsAuditor); + EsExecutors.allocatedProcessors(settings) + ); + MemoryUsageEstimationProcessManager memoryEstimationProcessManager = new MemoryUsageEstimationProcessManager( + threadPool.generic(), + threadPool.executor(UTILITY_THREAD_POOL_NAME), + memoryEstimationProcessFactory + ); + DataFrameAnalyticsConfigProvider dataFrameAnalyticsConfigProvider = new DataFrameAnalyticsConfigProvider( + client, + xContentRegistry, + dataFrameAnalyticsAuditor + ); assert client instanceof NodeClient; - DataFrameAnalyticsManager dataFrameAnalyticsManager = new DataFrameAnalyticsManager(settings, (NodeClient) client, threadPool, - clusterService, dataFrameAnalyticsConfigProvider, analyticsProcessManager, dataFrameAnalyticsAuditor, - indexNameExpressionResolver, resultsPersisterService, modelLoadingService); + DataFrameAnalyticsManager dataFrameAnalyticsManager = new DataFrameAnalyticsManager( + settings, + (NodeClient) client, + threadPool, + clusterService, + dataFrameAnalyticsConfigProvider, + analyticsProcessManager, + dataFrameAnalyticsAuditor, + indexNameExpressionResolver, + resultsPersisterService, + modelLoadingService + ); this.dataFrameAnalyticsManager.set(dataFrameAnalyticsManager); // Components shared by anomaly detection and data frame analytics - MlMemoryTracker memoryTracker = new MlMemoryTracker(settings, clusterService, threadPool, jobManager, jobResultsProvider, - dataFrameAnalyticsConfigProvider); + MlMemoryTracker memoryTracker = new MlMemoryTracker( + settings, + clusterService, + threadPool, + jobManager, + jobResultsProvider, + dataFrameAnalyticsConfigProvider + ); this.memoryTracker.set(memoryTracker); - MlLifeCycleService mlLifeCycleService = - new MlLifeCycleService( - clusterService, datafeedRunner, mlController, autodetectProcessManager, dataFrameAnalyticsManager, memoryTracker); + MlLifeCycleService mlLifeCycleService = new MlLifeCycleService( + clusterService, + datafeedRunner, + mlController, + autodetectProcessManager, + dataFrameAnalyticsManager, + memoryTracker + ); this.mlLifeCycleService.set(mlLifeCycleService); - MlAssignmentNotifier mlAssignmentNotifier = new MlAssignmentNotifier(anomalyDetectionAuditor, dataFrameAnalyticsAuditor, threadPool, - clusterService); + MlAssignmentNotifier mlAssignmentNotifier = new MlAssignmentNotifier( + anomalyDetectionAuditor, + dataFrameAnalyticsAuditor, + threadPool, + clusterService + ); - MlAutoUpdateService mlAutoUpdateService = new MlAutoUpdateService(threadPool, - List.of(new DatafeedConfigAutoUpdater(datafeedConfigProvider, indexNameExpressionResolver))); + MlAutoUpdateService mlAutoUpdateService = new MlAutoUpdateService( + threadPool, + List.of(new DatafeedConfigAutoUpdater(datafeedConfigProvider, indexNameExpressionResolver)) + ); clusterService.addListener(mlAutoUpdateService); // this object registers as a license state listener, and is never removed, so there's no need to retain another reference to it - final InvalidLicenseEnforcer enforcer = - new InvalidLicenseEnforcer(getLicenseState(), threadPool, datafeedRunner, autodetectProcessManager); + final InvalidLicenseEnforcer enforcer = new InvalidLicenseEnforcer( + getLicenseState(), + threadPool, + datafeedRunner, + autodetectProcessManager + ); enforcer.listenForLicenseStateChanges(); // Perform node startup operations @@ -911,89 +1036,100 @@ public Collection createComponents(Client client, ClusterService cluster clusterService, threadPool ); - trainedModelAllocationClusterServiceSetOnce.set(new TrainedModelAllocationClusterService( - settings, - clusterService, - new NodeLoadDetector(memoryTracker) - )); + trainedModelAllocationClusterServiceSetOnce.set( + new TrainedModelAllocationClusterService(settings, clusterService, new NodeLoadDetector(memoryTracker)) + ); mlAutoscalingDeciderService.set(new MlAutoscalingDeciderService(memoryTracker, settings, clusterService)); return Arrays.asList( - mlLifeCycleService, - new MlControllerHolder(mlController), - jobResultsProvider, - jobResultsPersister, - jobConfigProvider, - datafeedConfigProvider, - jobManager, - jobManagerHolder, - autodetectProcessManager, - new MlInitializationService(settings, threadPool, clusterService, client, mlAssignmentNotifier), - jobDataCountsPersister, - datafeedRunner, - datafeedManager, - anomalyDetectionAuditor, - dataFrameAnalyticsAuditor, - inferenceAuditor, - mlAssignmentNotifier, - mlAutoUpdateService, - memoryTracker, - analyticsProcessManager, - memoryEstimationProcessManager, - dataFrameAnalyticsConfigProvider, - nativeStorageProvider, - modelLoadingService, - trainedModelProvider, - trainedModelAllocationService, - trainedModelAllocationClusterServiceSetOnce.get(), - deploymentManager.get() + mlLifeCycleService, + new MlControllerHolder(mlController), + jobResultsProvider, + jobResultsPersister, + jobConfigProvider, + datafeedConfigProvider, + jobManager, + jobManagerHolder, + autodetectProcessManager, + new MlInitializationService(settings, threadPool, clusterService, client, mlAssignmentNotifier), + jobDataCountsPersister, + datafeedRunner, + datafeedManager, + anomalyDetectionAuditor, + dataFrameAnalyticsAuditor, + inferenceAuditor, + mlAssignmentNotifier, + mlAutoUpdateService, + memoryTracker, + analyticsProcessManager, + memoryEstimationProcessManager, + dataFrameAnalyticsConfigProvider, + nativeStorageProvider, + modelLoadingService, + trainedModelProvider, + trainedModelAllocationService, + trainedModelAllocationClusterServiceSetOnce.get(), + deploymentManager.get() ); } @Override - public List> getPersistentTasksExecutor(ClusterService clusterService, - ThreadPool threadPool, - Client client, - SettingsModule settingsModule, - IndexNameExpressionResolver expressionResolver) { + public List> getPersistentTasksExecutor( + ClusterService clusterService, + ThreadPool threadPool, + Client client, + SettingsModule settingsModule, + IndexNameExpressionResolver expressionResolver + ) { if (enabled == false) { return emptyList(); } return Arrays.asList( - new OpenJobPersistentTasksExecutor(settings, - clusterService, - autodetectProcessManager.get(), - datafeedConfigProvider.get(), - memoryTracker.get(), - client, - expressionResolver, - getLicenseState()), - new TransportStartDatafeedAction.StartDatafeedPersistentTasksExecutor(datafeedRunner.get(), expressionResolver), - new TransportStartDataFrameAnalyticsAction.TaskExecutor(settings, - client, - clusterService, - dataFrameAnalyticsManager.get(), - dataFrameAnalyticsAuditor.get(), - memoryTracker.get(), - expressionResolver, - getLicenseState()), - new SnapshotUpgradeTaskExecutor(settings, - clusterService, - autodetectProcessManager.get(), - memoryTracker.get(), - expressionResolver, - client, - getLicenseState()) + new OpenJobPersistentTasksExecutor( + settings, + clusterService, + autodetectProcessManager.get(), + datafeedConfigProvider.get(), + memoryTracker.get(), + client, + expressionResolver, + getLicenseState() + ), + new TransportStartDatafeedAction.StartDatafeedPersistentTasksExecutor(datafeedRunner.get(), expressionResolver), + new TransportStartDataFrameAnalyticsAction.TaskExecutor( + settings, + client, + clusterService, + dataFrameAnalyticsManager.get(), + dataFrameAnalyticsAuditor.get(), + memoryTracker.get(), + expressionResolver, + getLicenseState() + ), + new SnapshotUpgradeTaskExecutor( + settings, + clusterService, + autodetectProcessManager.get(), + memoryTracker.get(), + expressionResolver, + client, + getLicenseState() + ) ); } @Override - public List getRestHandlers(Settings settings, RestController restController, ClusterSettings clusterSettings, - IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter, - IndexNameExpressionResolver indexNameExpressionResolver, - Supplier nodesInCluster) { + public List getRestHandlers( + Settings settings, + RestController restController, + ClusterSettings clusterSettings, + IndexScopedSettings indexScopedSettings, + SettingsFilter settingsFilter, + IndexNameExpressionResolver indexNameExpressionResolver, + Supplier nodesInCluster + ) { if (false == enabled) { return emptyList(); } @@ -1078,99 +1214,95 @@ public List getRestHandlers(Settings settings, RestController restC @Override public List> getActions() { - var usageAction = - new ActionHandler<>(XPackUsageFeatureAction.MACHINE_LEARNING, MachineLearningUsageTransportAction.class); - var infoAction = - new ActionHandler<>(XPackInfoFeatureAction.MACHINE_LEARNING, MachineLearningInfoTransportAction.class); + var usageAction = new ActionHandler<>(XPackUsageFeatureAction.MACHINE_LEARNING, MachineLearningUsageTransportAction.class); + var infoAction = new ActionHandler<>(XPackInfoFeatureAction.MACHINE_LEARNING, MachineLearningInfoTransportAction.class); if (false == enabled) { return List.of(usageAction, infoAction); } return List.of( - new ActionHandler<>(GetJobsAction.INSTANCE, TransportGetJobsAction.class), - new ActionHandler<>(GetJobsStatsAction.INSTANCE, TransportGetJobsStatsAction.class), - new ActionHandler<>(MlInfoAction.INSTANCE, TransportMlInfoAction.class), - new ActionHandler<>(PutJobAction.INSTANCE, TransportPutJobAction.class), - new ActionHandler<>(UpdateJobAction.INSTANCE, TransportUpdateJobAction.class), - new ActionHandler<>(DeleteJobAction.INSTANCE, TransportDeleteJobAction.class), - new ActionHandler<>(OpenJobAction.INSTANCE, TransportOpenJobAction.class), - new ActionHandler<>(GetFiltersAction.INSTANCE, TransportGetFiltersAction.class), - new ActionHandler<>(PutFilterAction.INSTANCE, TransportPutFilterAction.class), - new ActionHandler<>(UpdateFilterAction.INSTANCE, TransportUpdateFilterAction.class), - new ActionHandler<>(DeleteFilterAction.INSTANCE, TransportDeleteFilterAction.class), - new ActionHandler<>(KillProcessAction.INSTANCE, TransportKillProcessAction.class), - new ActionHandler<>(GetBucketsAction.INSTANCE, TransportGetBucketsAction.class), - new ActionHandler<>(GetInfluencersAction.INSTANCE, TransportGetInfluencersAction.class), - new ActionHandler<>(GetOverallBucketsAction.INSTANCE, TransportGetOverallBucketsAction.class), - new ActionHandler<>(GetRecordsAction.INSTANCE, TransportGetRecordsAction.class), - new ActionHandler<>(PostDataAction.INSTANCE, TransportPostDataAction.class), - new ActionHandler<>(CloseJobAction.INSTANCE, TransportCloseJobAction.class), - new ActionHandler<>(FinalizeJobExecutionAction.INSTANCE, TransportFinalizeJobExecutionAction.class), - new ActionHandler<>(FlushJobAction.INSTANCE, TransportFlushJobAction.class), - new ActionHandler<>(ResetJobAction.INSTANCE, TransportResetJobAction.class), - new ActionHandler<>(ValidateDetectorAction.INSTANCE, TransportValidateDetectorAction.class), - new ActionHandler<>(ValidateJobConfigAction.INSTANCE, TransportValidateJobConfigAction.class), - new ActionHandler<>(EstimateModelMemoryAction.INSTANCE, TransportEstimateModelMemoryAction.class), - new ActionHandler<>(GetCategoriesAction.INSTANCE, TransportGetCategoriesAction.class), - new ActionHandler<>(GetModelSnapshotsAction.INSTANCE, TransportGetModelSnapshotsAction.class), - new ActionHandler<>(RevertModelSnapshotAction.INSTANCE, TransportRevertModelSnapshotAction.class), - new ActionHandler<>(UpdateModelSnapshotAction.INSTANCE, TransportUpdateModelSnapshotAction.class), - new ActionHandler<>(GetDatafeedsAction.INSTANCE, TransportGetDatafeedsAction.class), - new ActionHandler<>(GetDatafeedsStatsAction.INSTANCE, TransportGetDatafeedsStatsAction.class), - new ActionHandler<>(PutDatafeedAction.INSTANCE, TransportPutDatafeedAction.class), - new ActionHandler<>(UpdateDatafeedAction.INSTANCE, TransportUpdateDatafeedAction.class), - new ActionHandler<>(DeleteDatafeedAction.INSTANCE, TransportDeleteDatafeedAction.class), - new ActionHandler<>(PreviewDatafeedAction.INSTANCE, TransportPreviewDatafeedAction.class), - new ActionHandler<>(StartDatafeedAction.INSTANCE, TransportStartDatafeedAction.class), - new ActionHandler<>(StopDatafeedAction.INSTANCE, TransportStopDatafeedAction.class), - new ActionHandler<>(IsolateDatafeedAction.INSTANCE, TransportIsolateDatafeedAction.class), - new ActionHandler<>(DeleteModelSnapshotAction.INSTANCE, TransportDeleteModelSnapshotAction.class), - new ActionHandler<>(UpdateProcessAction.INSTANCE, TransportUpdateProcessAction.class), - new ActionHandler<>(DeleteExpiredDataAction.INSTANCE, TransportDeleteExpiredDataAction.class), - new ActionHandler<>(ForecastJobAction.INSTANCE, TransportForecastJobAction.class), - new ActionHandler<>(DeleteForecastAction.INSTANCE, TransportDeleteForecastAction.class), - new ActionHandler<>(GetCalendarsAction.INSTANCE, TransportGetCalendarsAction.class), - new ActionHandler<>(PutCalendarAction.INSTANCE, TransportPutCalendarAction.class), - new ActionHandler<>(DeleteCalendarAction.INSTANCE, TransportDeleteCalendarAction.class), - new ActionHandler<>(DeleteCalendarEventAction.INSTANCE, TransportDeleteCalendarEventAction.class), - new ActionHandler<>(UpdateCalendarJobAction.INSTANCE, TransportUpdateCalendarJobAction.class), - new ActionHandler<>(GetCalendarEventsAction.INSTANCE, TransportGetCalendarEventsAction.class), - new ActionHandler<>(PostCalendarEventsAction.INSTANCE, TransportPostCalendarEventsAction.class), - new ActionHandler<>(PersistJobAction.INSTANCE, TransportPersistJobAction.class), - new ActionHandler<>(SetUpgradeModeAction.INSTANCE, TransportSetUpgradeModeAction.class), - new ActionHandler<>(GetDataFrameAnalyticsAction.INSTANCE, TransportGetDataFrameAnalyticsAction.class), - new ActionHandler<>(GetDataFrameAnalyticsStatsAction.INSTANCE, TransportGetDataFrameAnalyticsStatsAction.class), - new ActionHandler<>(PutDataFrameAnalyticsAction.INSTANCE, TransportPutDataFrameAnalyticsAction.class), - new ActionHandler<>(UpdateDataFrameAnalyticsAction.INSTANCE, TransportUpdateDataFrameAnalyticsAction.class), - new ActionHandler<>(DeleteDataFrameAnalyticsAction.INSTANCE, TransportDeleteDataFrameAnalyticsAction.class), - new ActionHandler<>(StartDataFrameAnalyticsAction.INSTANCE, TransportStartDataFrameAnalyticsAction.class), - new ActionHandler<>(StopDataFrameAnalyticsAction.INSTANCE, TransportStopDataFrameAnalyticsAction.class), - new ActionHandler<>(EvaluateDataFrameAction.INSTANCE, TransportEvaluateDataFrameAction.class), - new ActionHandler<>(ExplainDataFrameAnalyticsAction.INSTANCE, TransportExplainDataFrameAnalyticsAction.class), - new ActionHandler<>(InternalInferModelAction.INSTANCE, TransportInternalInferModelAction.class), - new ActionHandler<>(GetTrainedModelsAction.INSTANCE, TransportGetTrainedModelsAction.class), - new ActionHandler<>(DeleteTrainedModelAction.INSTANCE, TransportDeleteTrainedModelAction.class), - new ActionHandler<>(GetTrainedModelsStatsAction.INSTANCE, TransportGetTrainedModelsStatsAction.class), - new ActionHandler<>(PutTrainedModelAction.INSTANCE, TransportPutTrainedModelAction.class), - new ActionHandler<>(UpgradeJobModelSnapshotAction.INSTANCE, TransportUpgradeJobModelSnapshotAction.class), - new ActionHandler<>(PutTrainedModelAliasAction.INSTANCE, TransportPutTrainedModelAliasAction.class), - new ActionHandler<>(DeleteTrainedModelAliasAction.INSTANCE, TransportDeleteTrainedModelAliasAction.class), - new ActionHandler<>(PreviewDataFrameAnalyticsAction.INSTANCE, TransportPreviewDataFrameAnalyticsAction.class), - new ActionHandler<>(SetResetModeAction.INSTANCE, TransportSetResetModeAction.class), - new ActionHandler<>(StartTrainedModelDeploymentAction.INSTANCE, TransportStartTrainedModelDeploymentAction.class), - new ActionHandler<>(StopTrainedModelDeploymentAction.INSTANCE, TransportStopTrainedModelDeploymentAction.class), - new ActionHandler<>(InferTrainedModelDeploymentAction.INSTANCE, TransportInferTrainedModelDeploymentAction.class), - new ActionHandler<>(GetDeploymentStatsAction.INSTANCE, TransportGetDeploymentStatsAction.class), - new ActionHandler<>(GetDatafeedRunningStateAction.INSTANCE, TransportGetDatafeedRunningStateAction.class), - new ActionHandler<>(CreateTrainedModelAllocationAction.INSTANCE, TransportCreateTrainedModelAllocationAction.class), - new ActionHandler<>(DeleteTrainedModelAllocationAction.INSTANCE, TransportDeleteTrainedModelAllocationAction.class), - new ActionHandler<>(PutTrainedModelDefinitionPartAction.INSTANCE, TransportPutTrainedModelDefinitionPartAction.class), - new ActionHandler<>(PutTrainedModelVocabularyAction.INSTANCE, TransportPutTrainedModelVocabularyAction.class), - new ActionHandler<>( - UpdateTrainedModelAllocationStateAction.INSTANCE, - TransportUpdateTrainedModelAllocationStateAction.class - ), - usageAction, - infoAction); + new ActionHandler<>(GetJobsAction.INSTANCE, TransportGetJobsAction.class), + new ActionHandler<>(GetJobsStatsAction.INSTANCE, TransportGetJobsStatsAction.class), + new ActionHandler<>(MlInfoAction.INSTANCE, TransportMlInfoAction.class), + new ActionHandler<>(PutJobAction.INSTANCE, TransportPutJobAction.class), + new ActionHandler<>(UpdateJobAction.INSTANCE, TransportUpdateJobAction.class), + new ActionHandler<>(DeleteJobAction.INSTANCE, TransportDeleteJobAction.class), + new ActionHandler<>(OpenJobAction.INSTANCE, TransportOpenJobAction.class), + new ActionHandler<>(GetFiltersAction.INSTANCE, TransportGetFiltersAction.class), + new ActionHandler<>(PutFilterAction.INSTANCE, TransportPutFilterAction.class), + new ActionHandler<>(UpdateFilterAction.INSTANCE, TransportUpdateFilterAction.class), + new ActionHandler<>(DeleteFilterAction.INSTANCE, TransportDeleteFilterAction.class), + new ActionHandler<>(KillProcessAction.INSTANCE, TransportKillProcessAction.class), + new ActionHandler<>(GetBucketsAction.INSTANCE, TransportGetBucketsAction.class), + new ActionHandler<>(GetInfluencersAction.INSTANCE, TransportGetInfluencersAction.class), + new ActionHandler<>(GetOverallBucketsAction.INSTANCE, TransportGetOverallBucketsAction.class), + new ActionHandler<>(GetRecordsAction.INSTANCE, TransportGetRecordsAction.class), + new ActionHandler<>(PostDataAction.INSTANCE, TransportPostDataAction.class), + new ActionHandler<>(CloseJobAction.INSTANCE, TransportCloseJobAction.class), + new ActionHandler<>(FinalizeJobExecutionAction.INSTANCE, TransportFinalizeJobExecutionAction.class), + new ActionHandler<>(FlushJobAction.INSTANCE, TransportFlushJobAction.class), + new ActionHandler<>(ResetJobAction.INSTANCE, TransportResetJobAction.class), + new ActionHandler<>(ValidateDetectorAction.INSTANCE, TransportValidateDetectorAction.class), + new ActionHandler<>(ValidateJobConfigAction.INSTANCE, TransportValidateJobConfigAction.class), + new ActionHandler<>(EstimateModelMemoryAction.INSTANCE, TransportEstimateModelMemoryAction.class), + new ActionHandler<>(GetCategoriesAction.INSTANCE, TransportGetCategoriesAction.class), + new ActionHandler<>(GetModelSnapshotsAction.INSTANCE, TransportGetModelSnapshotsAction.class), + new ActionHandler<>(RevertModelSnapshotAction.INSTANCE, TransportRevertModelSnapshotAction.class), + new ActionHandler<>(UpdateModelSnapshotAction.INSTANCE, TransportUpdateModelSnapshotAction.class), + new ActionHandler<>(GetDatafeedsAction.INSTANCE, TransportGetDatafeedsAction.class), + new ActionHandler<>(GetDatafeedsStatsAction.INSTANCE, TransportGetDatafeedsStatsAction.class), + new ActionHandler<>(PutDatafeedAction.INSTANCE, TransportPutDatafeedAction.class), + new ActionHandler<>(UpdateDatafeedAction.INSTANCE, TransportUpdateDatafeedAction.class), + new ActionHandler<>(DeleteDatafeedAction.INSTANCE, TransportDeleteDatafeedAction.class), + new ActionHandler<>(PreviewDatafeedAction.INSTANCE, TransportPreviewDatafeedAction.class), + new ActionHandler<>(StartDatafeedAction.INSTANCE, TransportStartDatafeedAction.class), + new ActionHandler<>(StopDatafeedAction.INSTANCE, TransportStopDatafeedAction.class), + new ActionHandler<>(IsolateDatafeedAction.INSTANCE, TransportIsolateDatafeedAction.class), + new ActionHandler<>(DeleteModelSnapshotAction.INSTANCE, TransportDeleteModelSnapshotAction.class), + new ActionHandler<>(UpdateProcessAction.INSTANCE, TransportUpdateProcessAction.class), + new ActionHandler<>(DeleteExpiredDataAction.INSTANCE, TransportDeleteExpiredDataAction.class), + new ActionHandler<>(ForecastJobAction.INSTANCE, TransportForecastJobAction.class), + new ActionHandler<>(DeleteForecastAction.INSTANCE, TransportDeleteForecastAction.class), + new ActionHandler<>(GetCalendarsAction.INSTANCE, TransportGetCalendarsAction.class), + new ActionHandler<>(PutCalendarAction.INSTANCE, TransportPutCalendarAction.class), + new ActionHandler<>(DeleteCalendarAction.INSTANCE, TransportDeleteCalendarAction.class), + new ActionHandler<>(DeleteCalendarEventAction.INSTANCE, TransportDeleteCalendarEventAction.class), + new ActionHandler<>(UpdateCalendarJobAction.INSTANCE, TransportUpdateCalendarJobAction.class), + new ActionHandler<>(GetCalendarEventsAction.INSTANCE, TransportGetCalendarEventsAction.class), + new ActionHandler<>(PostCalendarEventsAction.INSTANCE, TransportPostCalendarEventsAction.class), + new ActionHandler<>(PersistJobAction.INSTANCE, TransportPersistJobAction.class), + new ActionHandler<>(SetUpgradeModeAction.INSTANCE, TransportSetUpgradeModeAction.class), + new ActionHandler<>(GetDataFrameAnalyticsAction.INSTANCE, TransportGetDataFrameAnalyticsAction.class), + new ActionHandler<>(GetDataFrameAnalyticsStatsAction.INSTANCE, TransportGetDataFrameAnalyticsStatsAction.class), + new ActionHandler<>(PutDataFrameAnalyticsAction.INSTANCE, TransportPutDataFrameAnalyticsAction.class), + new ActionHandler<>(UpdateDataFrameAnalyticsAction.INSTANCE, TransportUpdateDataFrameAnalyticsAction.class), + new ActionHandler<>(DeleteDataFrameAnalyticsAction.INSTANCE, TransportDeleteDataFrameAnalyticsAction.class), + new ActionHandler<>(StartDataFrameAnalyticsAction.INSTANCE, TransportStartDataFrameAnalyticsAction.class), + new ActionHandler<>(StopDataFrameAnalyticsAction.INSTANCE, TransportStopDataFrameAnalyticsAction.class), + new ActionHandler<>(EvaluateDataFrameAction.INSTANCE, TransportEvaluateDataFrameAction.class), + new ActionHandler<>(ExplainDataFrameAnalyticsAction.INSTANCE, TransportExplainDataFrameAnalyticsAction.class), + new ActionHandler<>(InternalInferModelAction.INSTANCE, TransportInternalInferModelAction.class), + new ActionHandler<>(GetTrainedModelsAction.INSTANCE, TransportGetTrainedModelsAction.class), + new ActionHandler<>(DeleteTrainedModelAction.INSTANCE, TransportDeleteTrainedModelAction.class), + new ActionHandler<>(GetTrainedModelsStatsAction.INSTANCE, TransportGetTrainedModelsStatsAction.class), + new ActionHandler<>(PutTrainedModelAction.INSTANCE, TransportPutTrainedModelAction.class), + new ActionHandler<>(UpgradeJobModelSnapshotAction.INSTANCE, TransportUpgradeJobModelSnapshotAction.class), + new ActionHandler<>(PutTrainedModelAliasAction.INSTANCE, TransportPutTrainedModelAliasAction.class), + new ActionHandler<>(DeleteTrainedModelAliasAction.INSTANCE, TransportDeleteTrainedModelAliasAction.class), + new ActionHandler<>(PreviewDataFrameAnalyticsAction.INSTANCE, TransportPreviewDataFrameAnalyticsAction.class), + new ActionHandler<>(SetResetModeAction.INSTANCE, TransportSetResetModeAction.class), + new ActionHandler<>(StartTrainedModelDeploymentAction.INSTANCE, TransportStartTrainedModelDeploymentAction.class), + new ActionHandler<>(StopTrainedModelDeploymentAction.INSTANCE, TransportStopTrainedModelDeploymentAction.class), + new ActionHandler<>(InferTrainedModelDeploymentAction.INSTANCE, TransportInferTrainedModelDeploymentAction.class), + new ActionHandler<>(GetDeploymentStatsAction.INSTANCE, TransportGetDeploymentStatsAction.class), + new ActionHandler<>(GetDatafeedRunningStateAction.INSTANCE, TransportGetDatafeedRunningStateAction.class), + new ActionHandler<>(CreateTrainedModelAllocationAction.INSTANCE, TransportCreateTrainedModelAllocationAction.class), + new ActionHandler<>(DeleteTrainedModelAllocationAction.INSTANCE, TransportDeleteTrainedModelAllocationAction.class), + new ActionHandler<>(PutTrainedModelDefinitionPartAction.INSTANCE, TransportPutTrainedModelDefinitionPartAction.class), + new ActionHandler<>(PutTrainedModelVocabularyAction.INSTANCE, TransportPutTrainedModelVocabularyAction.class), + new ActionHandler<>(UpdateTrainedModelAllocationStateAction.INSTANCE, TransportUpdateTrainedModelAllocationStateAction.class), + usageAction, + infoAction + ); } @Override @@ -1189,24 +1321,39 @@ public List> getExecutorBuilders(Settings settings) { } // These thread pools scale such that they can accommodate the maximum number of jobs per node - // that is permitted to be configured. It is up to other code to enforce the configured maximum + // that is permitted to be configured. It is up to other code to enforce the configured maximum // number of jobs per node. // 4 threads per job process: for input, c++ logger output, result processing and state processing. // Only use this thread pool for the main long-running process associated with an anomaly detection - // job or a data frame analytics job. (Using it for some other purpose could mean that an unrelated + // job or a data frame analytics job. (Using it for some other purpose could mean that an unrelated // job fails to start or that whatever needed the thread for another purpose has to queue for a very // long time.) - ScalingExecutorBuilder jobComms = new ScalingExecutorBuilder(JOB_COMMS_THREAD_POOL_NAME, - 4, MAX_MAX_OPEN_JOBS_PER_NODE * 4, TimeValue.timeValueMinutes(1), "xpack.ml.job_comms_thread_pool"); + ScalingExecutorBuilder jobComms = new ScalingExecutorBuilder( + JOB_COMMS_THREAD_POOL_NAME, + 4, + MAX_MAX_OPEN_JOBS_PER_NODE * 4, + TimeValue.timeValueMinutes(1), + "xpack.ml.job_comms_thread_pool" + ); // This pool is used by renormalization, data frame analytics memory estimation, plus some other parts // of ML that need to kick off non-trivial activities that mustn't block other threads. - ScalingExecutorBuilder utility = new ScalingExecutorBuilder(UTILITY_THREAD_POOL_NAME, - 1, MAX_MAX_OPEN_JOBS_PER_NODE * 4, TimeValue.timeValueMinutes(10), "xpack.ml.utility_thread_pool"); + ScalingExecutorBuilder utility = new ScalingExecutorBuilder( + UTILITY_THREAD_POOL_NAME, + 1, + MAX_MAX_OPEN_JOBS_PER_NODE * 4, + TimeValue.timeValueMinutes(10), + "xpack.ml.utility_thread_pool" + ); - ScalingExecutorBuilder datafeed = new ScalingExecutorBuilder(DATAFEED_THREAD_POOL_NAME, - 1, MAX_MAX_OPEN_JOBS_PER_NODE, TimeValue.timeValueMinutes(1), "xpack.ml.datafeed_thread_pool"); + ScalingExecutorBuilder datafeed = new ScalingExecutorBuilder( + DATAFEED_THREAD_POOL_NAME, + 1, + MAX_MAX_OPEN_JOBS_PER_NODE, + TimeValue.timeValueMinutes(1), + "xpack.ml.datafeed_thread_pool" + ); return List.of(jobComms, utility, datafeed); } @@ -1221,8 +1368,7 @@ public Map> getCharFilters() { @Override public Map> getTokenizers() { - return Map.of(MlClassicTokenizer.NAME, MlClassicTokenizerFactory::new, - MlStandardTokenizer.NAME, MlStandardTokenizerFactory::new); + return Map.of(MlClassicTokenizer.NAME, MlClassicTokenizerFactory::new, MlStandardTokenizer.NAME, MlStandardTokenizerFactory::new); } @Override @@ -1236,9 +1382,7 @@ public List getPipelineAggregations() { @Override public List> getSignificanceHeuristics() { - return List.of( - new SignificanceHeuristicSpec<>(PValueScore.NAME, PValueScore::new, PValueScore.PARSER) - ); + return List.of(new SignificanceHeuristicSpec<>(PValueScore.NAME, PValueScore::new, PValueScore.PARSER)); } @Override @@ -1249,7 +1393,7 @@ public List getAggregations() { CategorizeTextAggregationBuilder::new, CategorizeTextAggregationBuilder.PARSER ).addResultReader(InternalCategorizationAggregation::new) - .setAggregatorRegistrar(s -> s.registerUsage(CategorizeTextAggregationBuilder.NAME)) + .setAggregatorRegistrar(s -> s.registerUsage(CategorizeTextAggregationBuilder.NAME)) ); } @@ -1316,23 +1460,40 @@ public List getNamedWriteables() { ); // Persistent tasks params - namedWriteables.add(new NamedWriteableRegistry.Entry(PersistentTaskParams.class, MlTasks.DATAFEED_TASK_NAME, - StartDatafeedAction.DatafeedParams::new)); - namedWriteables.add(new NamedWriteableRegistry.Entry(PersistentTaskParams.class, MlTasks.JOB_TASK_NAME, - OpenJobAction.JobParams::new)); - namedWriteables.add(new NamedWriteableRegistry.Entry(PersistentTaskParams.class, MlTasks.DATA_FRAME_ANALYTICS_TASK_NAME, - StartDataFrameAnalyticsAction.TaskParams::new)); - namedWriteables.add(new NamedWriteableRegistry.Entry(PersistentTaskParams.class, MlTasks.JOB_SNAPSHOT_UPGRADE_TASK_NAME, - SnapshotUpgradeTaskParams::new)); + namedWriteables.add( + new NamedWriteableRegistry.Entry( + PersistentTaskParams.class, + MlTasks.DATAFEED_TASK_NAME, + StartDatafeedAction.DatafeedParams::new + ) + ); + namedWriteables.add( + new NamedWriteableRegistry.Entry(PersistentTaskParams.class, MlTasks.JOB_TASK_NAME, OpenJobAction.JobParams::new) + ); + namedWriteables.add( + new NamedWriteableRegistry.Entry( + PersistentTaskParams.class, + MlTasks.DATA_FRAME_ANALYTICS_TASK_NAME, + StartDataFrameAnalyticsAction.TaskParams::new + ) + ); + namedWriteables.add( + new NamedWriteableRegistry.Entry( + PersistentTaskParams.class, + MlTasks.JOB_SNAPSHOT_UPGRADE_TASK_NAME, + SnapshotUpgradeTaskParams::new + ) + ); // Persistent task states namedWriteables.add(new NamedWriteableRegistry.Entry(PersistentTaskState.class, JobTaskState.NAME, JobTaskState::new)); namedWriteables.add(new NamedWriteableRegistry.Entry(PersistentTaskState.class, DatafeedState.NAME, DatafeedState::fromStream)); - namedWriteables.add(new NamedWriteableRegistry.Entry(PersistentTaskState.class, DataFrameAnalyticsTaskState.NAME, - DataFrameAnalyticsTaskState::new)); - namedWriteables.add(new NamedWriteableRegistry.Entry(PersistentTaskState.class, - SnapshotUpgradeTaskState.NAME, - SnapshotUpgradeTaskState::new)); + namedWriteables.add( + new NamedWriteableRegistry.Entry(PersistentTaskState.class, DataFrameAnalyticsTaskState.NAME, DataFrameAnalyticsTaskState::new) + ); + namedWriteables.add( + new NamedWriteableRegistry.Entry(PersistentTaskState.class, SnapshotUpgradeTaskState.NAME, SnapshotUpgradeTaskState::new) + ); namedWriteables.addAll(new MlDataFrameAnalysisNamedXContentProvider().getNamedWriteables()); namedWriteables.addAll(new AnalysisStatsNamedWriteablesProvider().getNamedWriteables()); @@ -1391,10 +1552,11 @@ public void prepareForIndicesMigration(ClusterService clusterService, Client cli // Enable ML upgrade mode before upgrading the ML system indices to ensure nothing writes to them during the upgrade Client originClient = new OriginSettingClient(client, ML_ORIGIN); - originClient.execute(SetUpgradeModeAction.INSTANCE, new SetUpgradeModeAction.Request(true), ActionListener.wrap( - r -> listener.onResponse(Collections.singletonMap("already_in_upgrade_mode", false)), - listener::onFailure - )); + originClient.execute( + SetUpgradeModeAction.INSTANCE, + new SetUpgradeModeAction.Request(true), + ActionListener.wrap(r -> listener.onResponse(Collections.singletonMap("already_in_upgrade_mode", false)), listener::onFailure) + ); } @Override @@ -1412,24 +1574,24 @@ public void indicesMigrationComplete( } Client originClient = new OriginSettingClient(client, ML_ORIGIN); - originClient.execute(SetUpgradeModeAction.INSTANCE, new SetUpgradeModeAction.Request(false), ActionListener.wrap( - r -> listener.onResponse(r.isAcknowledged()), - listener::onFailure - )); + originClient.execute( + SetUpgradeModeAction.INSTANCE, + new SetUpgradeModeAction.Request(false), + ActionListener.wrap(r -> listener.onResponse(r.isAcknowledged()), listener::onFailure) + ); } /** * These are the ML hidden indices. They are "associated" in the sense that if the ML system indices * are backed up or deleted then these hidden indices should also be backed up or deleted. */ - private static final Collection ASSOCIATED_INDEX_DESCRIPTORS = - List.of( - new AssociatedIndexDescriptor(RESULTS_INDEX_PREFIX + "*", "Results indices"), - new AssociatedIndexDescriptor(STATE_INDEX_PREFIX + "*", "State indices"), - new AssociatedIndexDescriptor(MlStatsIndex.indexPattern(), "ML stats index"), - new AssociatedIndexDescriptor(".ml-notifications*", "ML notifications indices"), - new AssociatedIndexDescriptor(".ml-annotations*", "ML annotations indices") - ); + private static final Collection ASSOCIATED_INDEX_DESCRIPTORS = List.of( + new AssociatedIndexDescriptor(RESULTS_INDEX_PREFIX + "*", "Results indices"), + new AssociatedIndexDescriptor(STATE_INDEX_PREFIX + "*", "State indices"), + new AssociatedIndexDescriptor(MlStatsIndex.indexPattern(), "ML stats index"), + new AssociatedIndexDescriptor(".ml-notifications*", "ML notifications indices"), + new AssociatedIndexDescriptor(".ml-annotations*", "ML annotations indices") + ); @Override public Collection getAssociatedIndexDescriptors() { @@ -1437,10 +1599,7 @@ public Collection getAssociatedIndexDescriptors() { } public static String[] getMlHiddenIndexPatterns() { - return ASSOCIATED_INDEX_DESCRIPTORS - .stream() - .map(AssociatedIndexDescriptor::getIndexPattern) - .toArray(String[]::new); + return ASSOCIATED_INDEX_DESCRIPTORS.stream().map(AssociatedIndexDescriptor::getIndexPattern).toArray(String[]::new); } @Override @@ -1465,12 +1624,13 @@ public void cleanUpFeature( final Map results = new ConcurrentHashMap<>(); ActionListener unsetResetModeListener = ActionListener.wrap( - success -> client.execute(SetResetModeAction.INSTANCE, SetResetModeActionRequest.disabled(true), ActionListener.wrap( - resetSuccess -> { + success -> client.execute( + SetResetModeAction.INSTANCE, + SetResetModeActionRequest.disabled(true), + ActionListener.wrap(resetSuccess -> { finalListener.onResponse(success); logger.info("Finished machine learning feature reset"); - }, - resetFailure -> { + }, resetFailure -> { logger.error("failed to disable reset mode after state otherwise successful machine learning reset", resetFailure); finalListener.onFailure( ExceptionsHelper.serverError( @@ -1482,9 +1642,10 @@ public void cleanUpFeature( ), failure -> { logger.error("failed to reset machine learning", failure); - client.execute(SetResetModeAction.INSTANCE, SetResetModeActionRequest.disabled(false), ActionListener.wrap( - resetSuccess -> finalListener.onFailure(failure), - resetFailure -> { + client.execute( + SetResetModeAction.INSTANCE, + SetResetModeActionRequest.disabled(false), + ActionListener.wrap(resetSuccess -> finalListener.onFailure(failure), resetFailure -> { logger.error("failed to disable reset mode after state clean up failure", resetFailure); finalListener.onFailure(failure); }) @@ -1492,34 +1653,38 @@ public void cleanUpFeature( } ); - ActionListener afterWaitingForTasks = ActionListener.wrap( - listTasksResponse -> { - listTasksResponse.rethrowFailures("Waiting for indexing requests for .ml-* indices"); - if (results.values().stream().allMatch(b -> b)) { - if (memoryTracker.get() != null) { - memoryTracker.get().awaitAndClear(ActionListener.wrap( - cacheCleared -> SystemIndexPlugin.super.cleanUpFeature(clusterService, client, unsetResetModeListener), - clearFailed -> { - logger.error("failed to clear memory tracker cache via machine learning reset feature API", clearFailed); - SystemIndexPlugin.super.cleanUpFeature(clusterService, client, unsetResetModeListener); - } - )); - return; - } - // Call into the original listener to clean up the indices and then clear ml memory cache - SystemIndexPlugin.super.cleanUpFeature(clusterService, client, unsetResetModeListener); - } else { - final List failedComponents = results.entrySet().stream() - .filter(result -> result.getValue() == false) - .map(Map.Entry::getKey) - .collect(Collectors.toList()); - unsetResetModeListener.onFailure( - new RuntimeException("Some machine learning components failed to reset: " + failedComponents) - ); + ActionListener afterWaitingForTasks = ActionListener.wrap(listTasksResponse -> { + listTasksResponse.rethrowFailures("Waiting for indexing requests for .ml-* indices"); + if (results.values().stream().allMatch(b -> b)) { + if (memoryTracker.get() != null) { + memoryTracker.get() + .awaitAndClear( + ActionListener.wrap( + cacheCleared -> SystemIndexPlugin.super.cleanUpFeature(clusterService, client, unsetResetModeListener), + clearFailed -> { + logger.error( + "failed to clear memory tracker cache via machine learning reset feature API", + clearFailed + ); + SystemIndexPlugin.super.cleanUpFeature(clusterService, client, unsetResetModeListener); + } + ) + ); + return; } - }, - unsetResetModeListener::onFailure - ); + // Call into the original listener to clean up the indices and then clear ml memory cache + SystemIndexPlugin.super.cleanUpFeature(clusterService, client, unsetResetModeListener); + } else { + final List failedComponents = results.entrySet() + .stream() + .filter(result -> result.getValue() == false) + .map(Map.Entry::getKey) + .collect(Collectors.toList()); + unsetResetModeListener.onFailure( + new RuntimeException("Some machine learning components failed to reset: " + failedComponents) + ); + } + }, unsetResetModeListener::onFailure); ActionListener afterDataframesStopped = ActionListener.wrap(dataFrameStopResponse -> { // Handle the response @@ -1531,22 +1696,20 @@ public void cleanUpFeature( // This waits for all xpack actions including: allocations, anomaly detections, analytics .setActions("xpack/ml/*") .setWaitForCompletion(true) - .execute(ActionListener.wrap( - listMlTasks -> { - listMlTasks.rethrowFailures("Waiting for machine learning tasks"); - client.admin() - .cluster() - .prepareListTasks() - .setActions("indices:data/write/bulk") - .setDetailed(true) - .setWaitForCompletion(true) - .setDescriptions("*.ml-*") - .execute(afterWaitingForTasks); - }, - unsetResetModeListener::onFailure - )); + .execute(ActionListener.wrap(listMlTasks -> { + listMlTasks.rethrowFailures("Waiting for machine learning tasks"); + client.admin() + .cluster() + .prepareListTasks() + .setActions("indices:data/write/bulk") + .setDetailed(true) + .setWaitForCompletion(true) + .setDescriptions("*.ml-*") + .execute(afterWaitingForTasks); + }, unsetResetModeListener::onFailure)); } else { - final List failedComponents = results.entrySet().stream() + final List failedComponents = results.entrySet() + .stream() .filter(result -> result.getValue() == false) .map(Map.Entry::getKey) .collect(Collectors.toList()); @@ -1561,18 +1724,18 @@ public void cleanUpFeature( results.put("anomaly_detectors", closeJobResponse.isClosed()); // Stop data frame analytics - StopDataFrameAnalyticsAction.Request stopDataFramesReq = new StopDataFrameAnalyticsAction.Request("_all") - .setAllowNoMatch(true); - client.execute(StopDataFrameAnalyticsAction.INSTANCE, stopDataFramesReq, ActionListener.wrap( - afterDataframesStopped::onResponse, - failure -> { + StopDataFrameAnalyticsAction.Request stopDataFramesReq = new StopDataFrameAnalyticsAction.Request("_all").setAllowNoMatch(true); + client.execute( + StopDataFrameAnalyticsAction.INSTANCE, + stopDataFramesReq, + ActionListener.wrap(afterDataframesStopped::onResponse, failure -> { logger.warn( "failed stopping data frame analytics jobs for machine learning feature reset. Attempting with force=true", failure ); client.execute(StopDataFrameAnalyticsAction.INSTANCE, stopDataFramesReq.setForce(true), afterDataframesStopped); - } - )); + }) + ); }, unsetResetModeListener::onFailure); // Close anomaly detection jobs @@ -1580,68 +1743,65 @@ public void cleanUpFeature( // Handle the response results.put("datafeeds", datafeedResponse.isStopped()); - CloseJobAction.Request closeJobsRequest = new CloseJobAction.Request() - .setAllowNoMatch(true) - .setJobId("_all"); + CloseJobAction.Request closeJobsRequest = new CloseJobAction.Request().setAllowNoMatch(true).setJobId("_all"); // First attempt to kill all anomaly jobs - client.execute(KillProcessAction.INSTANCE, new KillProcessAction.Request("*"), ActionListener.wrap( - // If successful, close and wait for jobs - success -> client.execute(CloseJobAction.INSTANCE, closeJobsRequest, ActionListener.wrap( - afterAnomalyDetectionClosed::onResponse, - failure -> { - logger.warn("failed closing anomaly jobs for machine learning feature reset. Attempting with force=true", failure); - client.execute(CloseJobAction.INSTANCE, closeJobsRequest.setForce(true), afterAnomalyDetectionClosed); - } - )), - unsetResetModeListener::onFailure - )); + client.execute( + KillProcessAction.INSTANCE, + new KillProcessAction.Request("*"), + ActionListener.wrap( + // If successful, close and wait for jobs + success -> client.execute( + CloseJobAction.INSTANCE, + closeJobsRequest, + ActionListener.wrap(afterAnomalyDetectionClosed::onResponse, failure -> { + logger.warn( + "failed closing anomaly jobs for machine learning feature reset. Attempting with force=true", + failure + ); + client.execute(CloseJobAction.INSTANCE, closeJobsRequest.setForce(true), afterAnomalyDetectionClosed); + }) + ), + unsetResetModeListener::onFailure + ) + ); }, unsetResetModeListener::onFailure); // Stop data feeds - ActionListener stopDeploymentsListener = ActionListener.wrap( - acknowledgedResponse -> { - StopDatafeedAction.Request stopDatafeedsReq = new StopDatafeedAction.Request("_all") - .setAllowNoMatch(true); - client.execute(StopDatafeedAction.INSTANCE, stopDatafeedsReq, ActionListener.wrap( - afterDataFeedsStopped::onResponse, - failure -> { - logger.warn("failed stopping datafeeds for machine learning feature reset. Attempting with force=true", failure); - client.execute(StopDatafeedAction.INSTANCE, stopDatafeedsReq.setForce(true), afterDataFeedsStopped); - } - )); - }, - unsetResetModeListener::onFailure - ); + ActionListener stopDeploymentsListener = ActionListener.wrap(acknowledgedResponse -> { + StopDatafeedAction.Request stopDatafeedsReq = new StopDatafeedAction.Request("_all").setAllowNoMatch(true); + client.execute( + StopDatafeedAction.INSTANCE, + stopDatafeedsReq, + ActionListener.wrap(afterDataFeedsStopped::onResponse, failure -> { + logger.warn("failed stopping datafeeds for machine learning feature reset. Attempting with force=true", failure); + client.execute(StopDatafeedAction.INSTANCE, stopDatafeedsReq.setForce(true), afterDataFeedsStopped); + }) + ); + }, unsetResetModeListener::onFailure); // Stop all model deployments - ActionListener pipelineValidation = ActionListener.wrap( - acknowledgedResponse -> { - if (trainedModelAllocationClusterServiceSetOnce.get() == null) { - stopDeploymentsListener.onResponse(AcknowledgedResponse.TRUE); - return; - } - trainedModelAllocationClusterServiceSetOnce.get().removeAllModelAllocations(stopDeploymentsListener); - }, - unsetResetModeListener::onFailure - ); + ActionListener pipelineValidation = ActionListener.wrap(acknowledgedResponse -> { + if (trainedModelAllocationClusterServiceSetOnce.get() == null) { + stopDeploymentsListener.onResponse(AcknowledgedResponse.TRUE); + return; + } + trainedModelAllocationClusterServiceSetOnce.get().removeAllModelAllocations(stopDeploymentsListener); + }, unsetResetModeListener::onFailure); // validate no pipelines are using machine learning models - ActionListener afterResetModeSet = ActionListener.wrap( - acknowledgedResponse -> { - int numberInferenceProcessors = countNumberInferenceProcessors(clusterService.state()); - if (numberInferenceProcessors > 0) { - unsetResetModeListener.onFailure( - new RuntimeException( - "Unable to reset machine learning feature as there are ingest pipelines " + - "still referencing trained machine learning models" - ) - ); - return; - } - pipelineValidation.onResponse(AcknowledgedResponse.of(true)); - }, - finalListener::onFailure - ); + ActionListener afterResetModeSet = ActionListener.wrap(acknowledgedResponse -> { + int numberInferenceProcessors = countNumberInferenceProcessors(clusterService.state()); + if (numberInferenceProcessors > 0) { + unsetResetModeListener.onFailure( + new RuntimeException( + "Unable to reset machine learning feature as there are ingest pipelines " + + "still referencing trained machine learning models" + ) + ); + return; + } + pipelineValidation.onResponse(AcknowledgedResponse.of(true)); + }, finalListener::onFailure); // Indicate that a reset is now in progress client.execute(SetResetModeAction.INSTANCE, SetResetModeActionRequest.enabled(), afterResetModeSet); @@ -1657,7 +1817,8 @@ public BreakerSettings getCircuitBreaker(Settings settings) { CircuitBreaker.Type.MEMORY, CircuitBreaker.Durability.TRANSIENT ), - settings); + settings + ); } @Override diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MachineLearningInfoTransportAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MachineLearningInfoTransportAction.java index 2fff11b492579..a9e026b9b4529 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MachineLearningInfoTransportAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MachineLearningInfoTransportAction.java @@ -23,8 +23,12 @@ public class MachineLearningInfoTransportAction extends XPackInfoFeatureTranspor private final XPackLicenseState licenseState; @Inject - public MachineLearningInfoTransportAction(TransportService transportService, ActionFilters actionFilters, - Settings settings, XPackLicenseState licenseState) { + public MachineLearningInfoTransportAction( + TransportService transportService, + ActionFilters actionFilters, + Settings settings, + XPackLicenseState licenseState + ) { super(XPackInfoFeatureAction.MACHINE_LEARNING.name(), transportService, actionFilters); this.enabled = XPackSettings.MACHINE_LEARNING_ENABLED.get(settings); this.licenseState = licenseState; diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MachineLearningPainlessExtension.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MachineLearningPainlessExtension.java index 7958e20e0d568..cc41999e2b04c 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MachineLearningPainlessExtension.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MachineLearningPainlessExtension.java @@ -17,8 +17,10 @@ import java.util.Map; public class MachineLearningPainlessExtension implements PainlessExtension { - private static final Whitelist WHITELIST = - WhitelistLoader.loadFromResourceFiles(MachineLearningPainlessExtension.class, "whitelist.txt"); + private static final Whitelist WHITELIST = WhitelistLoader.loadFromResourceFiles( + MachineLearningPainlessExtension.class, + "whitelist.txt" + ); @Override public Map, List> getContextWhitelists() { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MachineLearningUsageTransportAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MachineLearningUsageTransportAction.java index 76ac5841340a6..7c136a660948d 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MachineLearningUsageTransportAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MachineLearningUsageTransportAction.java @@ -73,12 +73,25 @@ public class MachineLearningUsageTransportAction extends XPackUsageFeatureTransp private final boolean enabled; @Inject - public MachineLearningUsageTransportAction(TransportService transportService, ClusterService clusterService, ThreadPool threadPool, - ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver, - Environment environment, Client client, - XPackLicenseState licenseState, JobManagerHolder jobManagerHolder) { - super(XPackUsageFeatureAction.MACHINE_LEARNING.name(), transportService, clusterService, - threadPool, actionFilters, indexNameExpressionResolver); + public MachineLearningUsageTransportAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver, + Environment environment, + Client client, + XPackLicenseState licenseState, + JobManagerHolder jobManagerHolder + ) { + super( + XPackUsageFeatureAction.MACHINE_LEARNING.name(), + transportService, + clusterService, + threadPool, + actionFilters, + indexNameExpressionResolver + ); this.client = client; this.licenseState = licenseState; this.jobManagerHolder = jobManagerHolder; @@ -86,12 +99,22 @@ public MachineLearningUsageTransportAction(TransportService transportService, Cl } @Override - protected void masterOperation(Task task, XPackUsageRequest request, ClusterState state, - ActionListener listener) { + protected void masterOperation( + Task task, + XPackUsageRequest request, + ClusterState state, + ActionListener listener + ) { if (enabled == false) { MachineLearningFeatureSetUsage usage = new MachineLearningFeatureSetUsage( - MachineLearningField.ML_API_FEATURE.checkWithoutTracking(licenseState), enabled, - Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap(), 0); + MachineLearningField.ML_API_FEATURE.checkWithoutTracking(licenseState), + enabled, + Collections.emptyMap(), + Collections.emptyMap(), + Collections.emptyMap(), + Collections.emptyMap(), + 0 + ); listener.onResponse(new XPackUsageFeatureResponse(usage)); return; } @@ -103,73 +126,68 @@ protected void masterOperation(Task task, XPackUsageRequest request, ClusterStat int nodeCount = mlNodeCount(state); // Step 6. extract trained model config count and then return results - ActionListener trainedModelsListener = ActionListener.wrap( - response -> { - addTrainedModelStats(response, inferenceUsage); - MachineLearningFeatureSetUsage usage = new MachineLearningFeatureSetUsage( - MachineLearningField.ML_API_FEATURE.checkWithoutTracking(licenseState), - enabled, jobsUsage, datafeedsUsage, analyticsUsage, inferenceUsage, nodeCount); - listener.onResponse(new XPackUsageFeatureResponse(usage)); - }, - listener::onFailure - ); + ActionListener trainedModelsListener = ActionListener.wrap(response -> { + addTrainedModelStats(response, inferenceUsage); + MachineLearningFeatureSetUsage usage = new MachineLearningFeatureSetUsage( + MachineLearningField.ML_API_FEATURE.checkWithoutTracking(licenseState), + enabled, + jobsUsage, + datafeedsUsage, + analyticsUsage, + inferenceUsage, + nodeCount + ); + listener.onResponse(new XPackUsageFeatureResponse(usage)); + }, listener::onFailure); // Step 5. Extract usage from ingest statistics and gather trained model config count - ActionListener nodesStatsListener = ActionListener.wrap( - response -> { - addInferenceIngestUsage(response, inferenceUsage); - GetTrainedModelsAction.Request getModelsRequest = new GetTrainedModelsAction.Request("*", Collections.emptyList(), - Collections.emptySet()); - getModelsRequest.setPageParams(new PageParams(0, 10_000)); - client.execute(GetTrainedModelsAction.INSTANCE, getModelsRequest, trainedModelsListener); - }, - listener::onFailure - ); + ActionListener nodesStatsListener = ActionListener.wrap(response -> { + addInferenceIngestUsage(response, inferenceUsage); + GetTrainedModelsAction.Request getModelsRequest = new GetTrainedModelsAction.Request( + "*", + Collections.emptyList(), + Collections.emptySet() + ); + getModelsRequest.setPageParams(new PageParams(0, 10_000)); + client.execute(GetTrainedModelsAction.INSTANCE, getModelsRequest, trainedModelsListener); + }, listener::onFailure); // Step 4. Extract usage from data frame analytics configs and then request ingest node stats - ActionListener dataframeAnalyticsListener = ActionListener.wrap( - response -> { - addDataFrameAnalyticsUsage(response, analyticsUsage); - String[] ingestNodes = ingestNodes(state); - NodesStatsRequest nodesStatsRequest = new NodesStatsRequest(ingestNodes).clear() - .addMetric(NodesStatsRequest.Metric.INGEST.metricName()); - client.execute(NodesStatsAction.INSTANCE, nodesStatsRequest, nodesStatsListener); - }, - listener::onFailure - ); + ActionListener dataframeAnalyticsListener = ActionListener.wrap(response -> { + addDataFrameAnalyticsUsage(response, analyticsUsage); + String[] ingestNodes = ingestNodes(state); + NodesStatsRequest nodesStatsRequest = new NodesStatsRequest(ingestNodes).clear() + .addMetric(NodesStatsRequest.Metric.INGEST.metricName()); + client.execute(NodesStatsAction.INSTANCE, nodesStatsRequest, nodesStatsListener); + }, listener::onFailure); // Step 3. Extract usage from data frame analytics stats and then request data frame analytics configs - ActionListener dataframeAnalyticsStatsListener = ActionListener.wrap( - response -> { - addDataFrameAnalyticsStatsUsage(response, analyticsUsage); - GetDataFrameAnalyticsAction.Request getDfaRequest = new GetDataFrameAnalyticsAction.Request(Metadata.ALL); - getDfaRequest.setPageParams(new PageParams(0, 10_000)); - client.execute(GetDataFrameAnalyticsAction.INSTANCE, getDfaRequest, dataframeAnalyticsListener); - }, - listener::onFailure - ); + ActionListener dataframeAnalyticsStatsListener = ActionListener.wrap(response -> { + addDataFrameAnalyticsStatsUsage(response, analyticsUsage); + GetDataFrameAnalyticsAction.Request getDfaRequest = new GetDataFrameAnalyticsAction.Request(Metadata.ALL); + getDfaRequest.setPageParams(new PageParams(0, 10_000)); + client.execute(GetDataFrameAnalyticsAction.INSTANCE, getDfaRequest, dataframeAnalyticsListener); + }, listener::onFailure); // Step 2. Extract usage from datafeeds stats and then request stats for data frame analytics - ActionListener datafeedStatsListener = - ActionListener.wrap(response -> { - addDatafeedsUsage(response, datafeedsUsage); - GetDataFrameAnalyticsStatsAction.Request dataframeAnalyticsStatsRequest = - new GetDataFrameAnalyticsStatsAction.Request(Metadata.ALL); - dataframeAnalyticsStatsRequest.setPageParams(new PageParams(0, 10_000)); - client.execute(GetDataFrameAnalyticsStatsAction.INSTANCE, dataframeAnalyticsStatsRequest, dataframeAnalyticsStatsListener); - }, - listener::onFailure); + ActionListener datafeedStatsListener = ActionListener.wrap(response -> { + addDatafeedsUsage(response, datafeedsUsage); + GetDataFrameAnalyticsStatsAction.Request dataframeAnalyticsStatsRequest = new GetDataFrameAnalyticsStatsAction.Request( + Metadata.ALL + ); + dataframeAnalyticsStatsRequest.setPageParams(new PageParams(0, 10_000)); + client.execute(GetDataFrameAnalyticsStatsAction.INSTANCE, dataframeAnalyticsStatsRequest, dataframeAnalyticsStatsListener); + }, listener::onFailure); // Step 1. Extract usage from jobs stats and then request stats for all datafeeds GetJobsStatsAction.Request jobStatsRequest = new GetJobsStatsAction.Request(Metadata.ALL); - ActionListener jobStatsListener = ActionListener.wrap( - response -> { - jobManagerHolder.getJobManager().expandJobs(Metadata.ALL, true, ActionListener.wrap(jobs -> { - addJobsUsage(response, jobs.results(), jobsUsage); - GetDatafeedsStatsAction.Request datafeedStatsRequest = new GetDatafeedsStatsAction.Request(Metadata.ALL); - client.execute(GetDatafeedsStatsAction.INSTANCE, datafeedStatsRequest, datafeedStatsListener); - }, listener::onFailure)); - }, listener::onFailure); + ActionListener jobStatsListener = ActionListener.wrap(response -> { + jobManagerHolder.getJobManager().expandJobs(Metadata.ALL, true, ActionListener.wrap(jobs -> { + addJobsUsage(response, jobs.results(), jobsUsage); + GetDatafeedsStatsAction.Request datafeedStatsRequest = new GetDatafeedsStatsAction.Request(Metadata.ALL); + client.execute(GetDatafeedsStatsAction.INSTANCE, datafeedStatsRequest, datafeedStatsListener); + }, listener::onFailure)); + }, listener::onFailure); // Step 0. Kick off the chain of callbacks by requesting jobs stats client.execute(GetJobsStatsAction.INSTANCE, jobStatsRequest, jobStatsListener); @@ -188,8 +206,10 @@ private void addJobsUsage(GetJobsStatsAction.Response response, List jobs, List jobsStats = response.getResponse().results(); Map jobMap = jobs.stream().collect(Collectors.toMap(Job::getId, item -> item)); - Map allJobsCreatedBy = jobs.stream().map(this::jobCreatedBy) - .collect(Collectors.groupingBy(item -> item, Collectors.counting()));; + Map allJobsCreatedBy = jobs.stream() + .map(this::jobCreatedBy) + .collect(Collectors.groupingBy(item -> item, Collectors.counting())); + ; for (GetJobsStatsAction.Response.JobStats jobStats : jobsStats) { Job job = jobMap.get(jobStats.getJobId()); if (job == null) { @@ -199,8 +219,7 @@ private void addJobsUsage(GetJobsStatsAction.Response response, List jobs, } int detectorsCount = job.getAnalysisConfig().getDetectors().size(); ModelSizeStats modelSizeStats = jobStats.getModelSizeStats(); - double modelSize = modelSizeStats == null ? 0.0 - : jobStats.getModelSizeStats().getModelBytes(); + double modelSize = modelSizeStats == null ? 0.0 : jobStats.getModelSizeStats().getModelBytes(); allJobsForecastStats.merge(jobStats.getForecastStats()); allJobsDetectorsStats.add(detectorsCount); @@ -208,24 +227,28 @@ private void addJobsUsage(GetJobsStatsAction.Response response, List jobs, JobState jobState = jobStats.getState(); jobCountByState.computeIfAbsent(jobState, js -> Counter.newCounter()).addAndGet(1); - detectorStatsByState.computeIfAbsent(jobState, - js -> new StatsAccumulator()).add(detectorsCount); - modelSizeStatsByState.computeIfAbsent(jobState, - js -> new StatsAccumulator()).add(modelSize); + detectorStatsByState.computeIfAbsent(jobState, js -> new StatsAccumulator()).add(detectorsCount); + modelSizeStatsByState.computeIfAbsent(jobState, js -> new StatsAccumulator()).add(modelSize); forecastStatsByState.merge(jobState, jobStats.getForecastStats(), (f1, f2) -> f1.merge(f2)); createdByByState.computeIfAbsent(jobState, js -> new HashMap<>()) .compute(jobCreatedBy(job), (k, v) -> (v == null) ? 1L : (v + 1)); } - jobsUsage.put(MachineLearningFeatureSetUsage.ALL, createJobUsageEntry(jobs.size(), allJobsDetectorsStats, - allJobsModelSizeStats, allJobsForecastStats, allJobsCreatedBy)); + jobsUsage.put( + MachineLearningFeatureSetUsage.ALL, + createJobUsageEntry(jobs.size(), allJobsDetectorsStats, allJobsModelSizeStats, allJobsForecastStats, allJobsCreatedBy) + ); for (JobState jobState : jobCountByState.keySet()) { - jobsUsage.put(jobState.name().toLowerCase(Locale.ROOT), createJobUsageEntry( - jobCountByState.get(jobState).get(), - detectorStatsByState.get(jobState), - modelSizeStatsByState.get(jobState), - forecastStatsByState.get(jobState), - createdByByState.get(jobState))); + jobsUsage.put( + jobState.name().toLowerCase(Locale.ROOT), + createJobUsageEntry( + jobCountByState.get(jobState).get(), + detectorStatsByState.get(jobState), + modelSizeStatsByState.get(jobState), + forecastStatsByState.get(jobState), + createdByByState.get(jobState) + ) + ); } } @@ -239,9 +262,13 @@ private String jobCreatedBy(Job job) { return customSettings.get(MachineLearningFeatureSetUsage.CREATED_BY).toString().replaceAll("\\W", "_"); } - private Map createJobUsageEntry(long count, StatsAccumulator detectorStats, - StatsAccumulator modelSizeStats, - ForecastStats forecastStats, Map createdBy) { + private Map createJobUsageEntry( + long count, + StatsAccumulator detectorStats, + StatsAccumulator modelSizeStats, + ForecastStats forecastStats, + Map createdBy + ) { Map usage = new HashMap<>(); usage.put(MachineLearningFeatureSetUsage.COUNT, count); usage.put(MachineLearningFeatureSetUsage.DETECTORS, detectorStats.asMap()); @@ -256,14 +283,15 @@ private void addDatafeedsUsage(GetDatafeedsStatsAction.Response response, Map datafeedsStats = response.getResponse().results(); for (GetDatafeedsStatsAction.Response.DatafeedStats datafeedStats : datafeedsStats) { - datafeedCountByState.computeIfAbsent(datafeedStats.getDatafeedState(), - ds -> Counter.newCounter()).addAndGet(1); + datafeedCountByState.computeIfAbsent(datafeedStats.getDatafeedState(), ds -> Counter.newCounter()).addAndGet(1); } datafeedsUsage.put(MachineLearningFeatureSetUsage.ALL, createCountUsageEntry(response.getResponse().count())); for (DatafeedState datafeedState : datafeedCountByState.keySet()) { - datafeedsUsage.put(datafeedState.name().toLowerCase(Locale.ROOT), - createCountUsageEntry(datafeedCountByState.get(datafeedState).get())); + datafeedsUsage.put( + datafeedState.name().toLowerCase(Locale.ROOT), + createCountUsageEntry(datafeedCountByState.get(datafeedState).get()) + ); } } @@ -273,32 +301,38 @@ private Map createCountUsageEntry(long count) { return usage; } - private void addDataFrameAnalyticsStatsUsage(GetDataFrameAnalyticsStatsAction.Response response, - Map dataframeAnalyticsUsage) { + private void addDataFrameAnalyticsStatsUsage( + GetDataFrameAnalyticsStatsAction.Response response, + Map dataframeAnalyticsUsage + ) { Map dataFrameAnalyticsStateCounterMap = new HashMap<>(); StatsAccumulator memoryUsagePeakBytesStats = new StatsAccumulator(); - for(GetDataFrameAnalyticsStatsAction.Response.Stats stats : response.getResponse().results()) { + for (GetDataFrameAnalyticsStatsAction.Response.Stats stats : response.getResponse().results()) { dataFrameAnalyticsStateCounterMap.computeIfAbsent(stats.getState(), ds -> Counter.newCounter()).addAndGet(1); MemoryUsage memoryUsage = stats.getMemoryUsage(); if (memoryUsage != null && memoryUsage.getPeakUsageBytes() > 0) { memoryUsagePeakBytesStats.add(memoryUsage.getPeakUsageBytes()); } } - dataframeAnalyticsUsage.put("memory_usage", - Collections.singletonMap(MemoryUsage.PEAK_USAGE_BYTES.getPreferredName(), memoryUsagePeakBytesStats.asMap())); + dataframeAnalyticsUsage.put( + "memory_usage", + Collections.singletonMap(MemoryUsage.PEAK_USAGE_BYTES.getPreferredName(), memoryUsagePeakBytesStats.asMap()) + ); dataframeAnalyticsUsage.put(MachineLearningFeatureSetUsage.ALL, createCountUsageEntry(response.getResponse().count())); for (DataFrameAnalyticsState state : dataFrameAnalyticsStateCounterMap.keySet()) { - dataframeAnalyticsUsage.put(state.name().toLowerCase(Locale.ROOT), - createCountUsageEntry(dataFrameAnalyticsStateCounterMap.get(state).get())); + dataframeAnalyticsUsage.put( + state.name().toLowerCase(Locale.ROOT), + createCountUsageEntry(dataFrameAnalyticsStateCounterMap.get(state).get()) + ); } } private void addDataFrameAnalyticsUsage(GetDataFrameAnalyticsAction.Response response, Map dataframeAnalyticsUsage) { Map perAnalysisTypeCounterMap = new HashMap<>(); - for(DataFrameAnalyticsConfig config : response.getResources().results()) { + for (DataFrameAnalyticsConfig config : response.getResources().results()) { int count = perAnalysisTypeCounterMap.computeIfAbsent(config.getAnalysis().getWriteableName(), k -> 0); perAnalysisTypeCounterMap.put(config.getAnalysis().getWriteableName(), ++count); } @@ -360,7 +394,7 @@ private void addTrainedModelStats(GetTrainedModelsAction.Response response, Map< inferenceUsage.put("trained_models", trainedModelsUsage); } - //TODO separate out ours and users models possibly regression vs classification + // TODO separate out ours and users models possibly regression vs classification private void addInferenceIngestUsage(NodesStatsResponse response, Map inferenceUsage) { Set pipelines = new HashSet<>(); Map docCountStats = new HashMap<>(3); @@ -374,25 +408,23 @@ private void addInferenceIngestUsage(NodesStatsResponse response, Map - map.forEach((pipelineId, processors) -> { - boolean containsInference = false; - for(IngestStats.ProcessorStat stats : processors) { - if (stats.getName().equals(InferenceProcessor.TYPE)) { - containsInference = true; - long ingestCount = stats.getStats().getIngestCount(); - long ingestTime = stats.getStats().getIngestTimeInMillis(); - long failureCount = stats.getStats().getIngestFailedCount(); - updateStats(docCountStats, ingestCount); - updateStats(timeStats, ingestTime); - updateStats(failureStats, failureCount); - } + .forEach(map -> map.forEach((pipelineId, processors) -> { + boolean containsInference = false; + for (IngestStats.ProcessorStat stats : processors) { + if (stats.getName().equals(InferenceProcessor.TYPE)) { + containsInference = true; + long ingestCount = stats.getStats().getIngestCount(); + long ingestTime = stats.getStats().getIngestTimeInMillis(); + long failureCount = stats.getStats().getIngestFailedCount(); + updateStats(docCountStats, ingestCount); + updateStats(timeStats, ingestTime); + updateStats(failureStats, failureCount); } - if (containsInference) { - pipelines.add(pipelineId); - } - }) - ); + } + if (containsInference) { + pipelines.add(pipelineId); + } + })); Map ingestUsage = new HashMap<>(6); ingestUsage.put("pipelines", createCountUsageEntry(pipelines.size())); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlAssignmentNotifier.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlAssignmentNotifier.java index 28e1aa7794aeb..ca561975508ca 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlAssignmentNotifier.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlAssignmentNotifier.java @@ -27,7 +27,6 @@ import java.util.Objects; - public class MlAssignmentNotifier implements ClusterStateListener { private static final Logger logger = LogManager.getLogger(MlAssignmentNotifier.class); @@ -35,8 +34,12 @@ public class MlAssignmentNotifier implements ClusterStateListener { private final DataFrameAnalyticsAuditor dataFrameAnalyticsAuditor; private final ThreadPool threadPool; - MlAssignmentNotifier(AnomalyDetectionAuditor anomalyDetectionAuditor, DataFrameAnalyticsAuditor dataFrameAnalyticsAuditor, - ThreadPool threadPool, ClusterService clusterService) { + MlAssignmentNotifier( + AnomalyDetectionAuditor anomalyDetectionAuditor, + DataFrameAnalyticsAuditor dataFrameAnalyticsAuditor, + ThreadPool threadPool, + ClusterService clusterService + ) { this.anomalyDetectionAuditor = anomalyDetectionAuditor; this.dataFrameAnalyticsAuditor = dataFrameAnalyticsAuditor; this.threadPool = threadPool; @@ -82,9 +85,13 @@ public void auditUnassignedMlTasks(DiscoveryNodes nodes, PersistentTasksCustomMe auditMlTasks(nodes, nodes, tasks, tasks, true); } - private void auditMlTasks(DiscoveryNodes previousNodes, DiscoveryNodes currentNodes, - PersistentTasksCustomMetadata previousTasks, PersistentTasksCustomMetadata currentTasks, - boolean alwaysAuditUnassigned) { + private void auditMlTasks( + DiscoveryNodes previousNodes, + DiscoveryNodes currentNodes, + PersistentTasksCustomMetadata previousTasks, + PersistentTasksCustomMetadata currentTasks, + boolean alwaysAuditUnassigned + ) { for (PersistentTask currentTask : currentTasks.tasks()) { Assignment currentAssignment = currentTask.getAssignment(); @@ -92,8 +99,7 @@ private void auditMlTasks(DiscoveryNodes previousNodes, DiscoveryNodes currentNo Assignment previousAssignment = previousTask != null ? previousTask.getAssignment() : null; boolean isTaskAssigned = (currentAssignment.getExecutorNode() != null); - if (Objects.equals(currentAssignment, previousAssignment) && - (isTaskAssigned || alwaysAuditUnassigned == false)) { + if (Objects.equals(currentAssignment, previousAssignment) && (isTaskAssigned || alwaysAuditUnassigned == false)) { continue; } boolean wasTaskAssigned = (previousAssignment != null) && (previousAssignment.getExecutorNode() != null); @@ -104,8 +110,10 @@ private void auditMlTasks(DiscoveryNodes previousNodes, DiscoveryNodes currentNo String nodeName = nodeName(currentNodes, currentAssignment.getExecutorNode()); anomalyDetectionAuditor.info(jobId, "Opening job on node [" + nodeName + "]"); } else if (alwaysAuditUnassigned) { - anomalyDetectionAuditor.warning(jobId, - "No node found to open job. Reasons [" + currentAssignment.getExplanation() + "]"); + anomalyDetectionAuditor.warning( + jobId, + "No node found to open job. Reasons [" + currentAssignment.getExplanation() + "]" + ); } else if (wasTaskAssigned) { String nodeName = nodeName(previousNodes, previousAssignment.getExecutorNode()); anomalyDetectionAuditor.info(jobId, "Job unassigned from node [" + nodeName + "]"); @@ -116,19 +124,32 @@ private void auditMlTasks(DiscoveryNodes previousNodes, DiscoveryNodes currentNo if (jobId != null) { if (isTaskAssigned) { String nodeName = nodeName(currentNodes, currentAssignment.getExecutorNode()); - anomalyDetectionAuditor.info(jobId, - "Starting datafeed [" + datafeedParams.getDatafeedId() + "] on node [" + nodeName + "]"); + anomalyDetectionAuditor.info( + jobId, + "Starting datafeed [" + datafeedParams.getDatafeedId() + "] on node [" + nodeName + "]" + ); } else if (alwaysAuditUnassigned) { - anomalyDetectionAuditor.warning(jobId, - "No node found to start datafeed [" + datafeedParams.getDatafeedId() + "]. Reasons [" + - currentAssignment.getExplanation() + "]"); + anomalyDetectionAuditor.warning( + jobId, + "No node found to start datafeed [" + + datafeedParams.getDatafeedId() + + "]. Reasons [" + + currentAssignment.getExplanation() + + "]" + ); } else if (wasTaskAssigned) { String nodeName = nodeName(previousNodes, previousAssignment.getExecutorNode()); - anomalyDetectionAuditor.info(jobId, - "Datafeed [" + datafeedParams.getDatafeedId() + "] unassigned from node [" + nodeName + "]"); + anomalyDetectionAuditor.info( + jobId, + "Datafeed [" + datafeedParams.getDatafeedId() + "] unassigned from node [" + nodeName + "]" + ); } else { - logger.warn("[{}] No node found to start datafeed [{}]. Reasons [{}]", - jobId, datafeedParams.getDatafeedId(), currentAssignment.getExplanation()); + logger.warn( + "[{}] No node found to start datafeed [{}]. Reasons [{}]", + jobId, + datafeedParams.getDatafeedId(), + currentAssignment.getExplanation() + ); } } } else if (MlTasks.DATA_FRAME_ANALYTICS_TASK_NAME.equals(currentTask.getTaskName())) { @@ -137,8 +158,10 @@ private void auditMlTasks(DiscoveryNodes previousNodes, DiscoveryNodes currentNo String nodeName = nodeName(currentNodes, currentAssignment.getExecutorNode()); dataFrameAnalyticsAuditor.info(id, "Starting analytics on node [" + nodeName + "]"); } else if (alwaysAuditUnassigned) { - dataFrameAnalyticsAuditor.warning(id, - "No node found to start analytics. Reasons [" + currentAssignment.getExplanation() + "]"); + dataFrameAnalyticsAuditor.warning( + id, + "No node found to start analytics. Reasons [" + currentAssignment.getExplanation() + "]" + ); } else if (wasTaskAssigned) { String nodeName = nodeName(previousNodes, previousAssignment.getExecutorNode()); anomalyDetectionAuditor.info(id, "Analytics unassigned from node [" + nodeName + "]"); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlAutoUpdateService.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlAutoUpdateService.java index 18d39518d0f62..e324048fbb3bd 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlAutoUpdateService.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlAutoUpdateService.java @@ -27,8 +27,11 @@ public class MlAutoUpdateService implements ClusterStateListener { public interface UpdateAction { boolean isMinNodeVersionSupported(Version minNodeVersion); + boolean isAbleToRun(ClusterState latestState); + String getName(); + void runUpdate(); } @@ -60,9 +63,7 @@ public void clusterChanged(ClusterChangedEvent event) { .filter(action -> action.isAbleToRun(event.state())) .filter(action -> currentlyUpdating.add(action.getName())) .collect(Collectors.toList()); - threadPool.executor(MachineLearning.UTILITY_THREAD_POOL_NAME).execute( - () -> toRun.forEach(this::runUpdate) - ); + threadPool.executor(MachineLearning.UTILITY_THREAD_POOL_NAME).execute(() -> toRun.forEach(this::runUpdate)); } private void runUpdate(UpdateAction action) { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlConfigMigrationEligibilityCheck.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlConfigMigrationEligibilityCheck.java index 9f3ceb39bfc0b..5abaaf02503e7 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlConfigMigrationEligibilityCheck.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlConfigMigrationEligibilityCheck.java @@ -24,7 +24,11 @@ public class MlConfigMigrationEligibilityCheck { public static final Setting ENABLE_CONFIG_MIGRATION = Setting.boolSetting( - "xpack.ml.enable_config_migration", true, Setting.Property.OperatorDynamic, Setting.Property.NodeScope); + "xpack.ml.enable_config_migration", + true, + Setting.Property.OperatorDynamic, + Setting.Property.NodeScope + ); private volatile boolean isConfigMigrationEnabled; @@ -37,7 +41,6 @@ private void setConfigMigrationEnabled(boolean configMigrationEnabled) { this.isConfigMigrationEnabled = configMigrationEnabled; } - /** * Can migration start? Returns: * False if config migration is disabled via the setting {@link #ENABLE_CONFIG_MIGRATION} @@ -92,8 +95,8 @@ public boolean jobIsEligibleForMigration(String jobId, ClusterState clusterState } PersistentTasksCustomMetadata persistentTasks = clusterState.metadata().custom(PersistentTasksCustomMetadata.TYPE); - return MlTasks.openJobIds(persistentTasks).contains(jobId) == false || - MlTasks.unassignedJobIds(persistentTasks, clusterState.nodes()).contains(jobId); + return MlTasks.openJobIds(persistentTasks).contains(jobId) == false + || MlTasks.unassignedJobIds(persistentTasks, clusterState.nodes()).contains(jobId); } /** @@ -120,6 +123,6 @@ public boolean datafeedIsEligibleForMigration(String datafeedId, ClusterState cl PersistentTasksCustomMetadata persistentTasks = clusterState.metadata().custom(PersistentTasksCustomMetadata.TYPE); return MlTasks.startedDatafeedIds(persistentTasks).contains(datafeedId) == false - || MlTasks.unassignedDatafeedIds(persistentTasks, clusterState.nodes()).contains(datafeedId); + || MlTasks.unassignedDatafeedIds(persistentTasks, clusterState.nodes()).contains(datafeedId); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlConfigMigrator.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlConfigMigrator.java index 0b65b1593f6c7..2b86fb58ea39d 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlConfigMigrator.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlConfigMigrator.java @@ -30,12 +30,12 @@ import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.EsExecutors; +import org.elasticsearch.index.engine.VersionConflictEngineException; +import org.elasticsearch.persistent.PersistentTasksCustomMetadata; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.index.engine.VersionConflictEngineException; -import org.elasticsearch.persistent.PersistentTasksCustomMetadata; import org.elasticsearch.xpack.core.ml.MlConfigIndex; import org.elasticsearch.xpack.core.ml.MlMetadata; import org.elasticsearch.xpack.core.ml.MlTasks; @@ -113,8 +113,12 @@ public class MlConfigMigrator { private final AtomicBoolean migrationInProgress; private final AtomicBoolean tookConfigSnapshot; - public MlConfigMigrator(Settings settings, Client client, ClusterService clusterService, - IndexNameExpressionResolver expressionResolver) { + public MlConfigMigrator( + Settings settings, + Client client, + ClusterService clusterService, + IndexNameExpressionResolver expressionResolver + ) { this.client = Objects.requireNonNull(client); this.clusterService = Objects.requireNonNull(clusterService); this.expressionResolver = Objects.requireNonNull(expressionResolver); @@ -146,16 +150,13 @@ public void migrateConfigs(ClusterState clusterState, ActionListener li return; } - ActionListener unMarkMigrationInProgress = ActionListener.wrap( - response -> { - migrationInProgress.set(false); - listener.onResponse(response); - }, - e -> { - migrationInProgress.set(false); - listener.onFailure(e); - } - ); + ActionListener unMarkMigrationInProgress = ActionListener.wrap(response -> { + migrationInProgress.set(false); + listener.onResponse(response); + }, e -> { + migrationInProgress.set(false); + listener.onFailure(e); + }); List batches = splitInBatches(clusterState); if (batches.isEmpty()) { @@ -164,12 +165,12 @@ public void migrateConfigs(ClusterState clusterState, ActionListener li } if (clusterState.metadata().hasIndex(MlConfigIndex.indexName()) == false) { - createConfigIndex(ActionListener.wrap( - response -> { - unMarkMigrationInProgress.onResponse(Boolean.FALSE); - }, + createConfigIndex( + ActionListener.wrap( + response -> { unMarkMigrationInProgress.onResponse(Boolean.FALSE); }, unMarkMigrationInProgress::onFailure - )); + ) + ); return; } @@ -178,75 +179,78 @@ public void migrateConfigs(ClusterState clusterState, ActionListener li return; } - snapshotMlMeta(MlMetadata.getMlMetadata(clusterState), ActionListener.wrap( - response -> { - // We have successfully snapshotted the ML configs so we don't need to try again - tookConfigSnapshot.set(true); - migrateBatches(batches, unMarkMigrationInProgress); - }, - unMarkMigrationInProgress::onFailure - )); + snapshotMlMeta(MlMetadata.getMlMetadata(clusterState), ActionListener.wrap(response -> { + // We have successfully snapshotted the ML configs so we don't need to try again + tookConfigSnapshot.set(true); + migrateBatches(batches, unMarkMigrationInProgress); + }, unMarkMigrationInProgress::onFailure)); } private void migrateBatches(List batches, ActionListener listener) { VoidChainTaskExecutor voidChainTaskExecutor = new VoidChainTaskExecutor(EsExecutors.DIRECT_EXECUTOR_SERVICE, true); for (JobsAndDatafeeds batch : batches) { - voidChainTaskExecutor.add(chainedListener -> writeConfigToIndex(batch.datafeedConfigs, batch.jobs, ActionListener.wrap( - failedDocumentIds -> { + voidChainTaskExecutor.add( + chainedListener -> writeConfigToIndex(batch.datafeedConfigs, batch.jobs, ActionListener.wrap(failedDocumentIds -> { List successfulJobWrites = filterFailedJobConfigWrites(failedDocumentIds, batch.jobs); - List successfulDatafeedWrites = - filterFailedDatafeedConfigWrites(failedDocumentIds, batch.datafeedConfigs); + List successfulDatafeedWrites = filterFailedDatafeedConfigWrites( + failedDocumentIds, + batch.datafeedConfigs + ); removeFromClusterState(successfulJobWrites, successfulDatafeedWrites, chainedListener); - }, - chainedListener::onFailure - ))); + }, chainedListener::onFailure)) + ); } voidChainTaskExecutor.execute(ActionListener.wrap(aVoids -> listener.onResponse(true), listener::onFailure)); } // Exposed for testing - public void writeConfigToIndex(Collection datafeedsToMigrate, - Collection jobsToMigrate, - ActionListener> listener) { + public void writeConfigToIndex( + Collection datafeedsToMigrate, + Collection jobsToMigrate, + ActionListener> listener + ) { BulkRequestBuilder bulkRequestBuilder = client.prepareBulk(); addJobIndexRequests(jobsToMigrate, bulkRequestBuilder); addDatafeedIndexRequests(datafeedsToMigrate, bulkRequestBuilder); bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - executeAsyncWithOrigin(client.threadPool().getThreadContext(), ML_ORIGIN, bulkRequestBuilder.request(), - ActionListener.wrap( - bulkResponse -> { - Set failedDocumentIds = documentsNotWritten(bulkResponse); - listener.onResponse(failedDocumentIds); - }, - listener::onFailure), - client::bulk + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + ML_ORIGIN, + bulkRequestBuilder.request(), + ActionListener.wrap(bulkResponse -> { + Set failedDocumentIds = documentsNotWritten(bulkResponse); + listener.onResponse(failedDocumentIds); + }, listener::onFailure), + client::bulk ); } - private void removeFromClusterState(List jobsToRemove, List datafeedsToRemove, - ActionListener listener) { + private void removeFromClusterState(List jobsToRemove, List datafeedsToRemove, ActionListener listener) { if (jobsToRemove.isEmpty() && datafeedsToRemove.isEmpty()) { listener.onResponse(null); return; } Map jobsMap = jobsToRemove.stream().collect(Collectors.toMap(Job::getId, Function.identity())); - Map datafeedMap = - datafeedsToRemove.stream().collect(Collectors.toMap(DatafeedConfig::getId, Function.identity())); + Map datafeedMap = datafeedsToRemove.stream() + .collect(Collectors.toMap(DatafeedConfig::getId, Function.identity())); AtomicReference removedConfigs = new AtomicReference<>(); clusterService.submitStateUpdateTask("remove-migrated-ml-configs", new ClusterStateUpdateTask() { @Override public ClusterState execute(ClusterState currentState) { - RemovalResult removed = removeJobsAndDatafeeds(jobsToRemove, datafeedsToRemove, - MlMetadata.getMlMetadata(currentState)); + RemovalResult removed = removeJobsAndDatafeeds(jobsToRemove, datafeedsToRemove, MlMetadata.getMlMetadata(currentState)); removedConfigs.set(removed); - PersistentTasksCustomMetadata updatedTasks = rewritePersistentTaskParams(jobsMap, datafeedMap, - currentState.metadata().custom(PersistentTasksCustomMetadata.TYPE), currentState.nodes()); + PersistentTasksCustomMetadata updatedTasks = rewritePersistentTaskParams( + jobsMap, + datafeedMap, + currentState.metadata().custom(PersistentTasksCustomMetadata.TYPE), + currentState.nodes() + ); ClusterState.Builder newState = ClusterState.builder(currentState); Metadata.Builder metadataBuilder = Metadata.builder(currentState.getMetadata()) @@ -294,13 +298,18 @@ public void clusterStateProcessed(String source, ClusterState oldState, ClusterS * @param nodes The nodes in the cluster * @return The updated tasks */ - public static PersistentTasksCustomMetadata rewritePersistentTaskParams(Map jobs, Map datafeeds, - PersistentTasksCustomMetadata currentTasks, - DiscoveryNodes nodes) { + public static PersistentTasksCustomMetadata rewritePersistentTaskParams( + Map jobs, + Map datafeeds, + PersistentTasksCustomMetadata currentTasks, + DiscoveryNodes nodes + ) { Collection> unallocatedJobTasks = MlTasks.unassignedJobTasks(currentTasks, nodes); - Collection> unallocatedDatafeedsTasks = - MlTasks.unassignedDatafeedTasks(currentTasks, nodes); + Collection> unallocatedDatafeedsTasks = MlTasks.unassignedDatafeedTasks( + currentTasks, + nodes + ); if (unallocatedJobTasks.isEmpty() && unallocatedDatafeedsTasks.isEmpty()) { return currentTasks; @@ -337,8 +346,10 @@ public static PersistentTasksCustomMetadata rewritePersistentTaskParams(Map jobsToRemove, List listen logger.debug("taking a snapshot of ml_metadata"); String documentId = "ml-config"; - IndexRequest indexRequest = new IndexRequest(AnomalyDetectorsIndex.jobStateIndexWriteAlias()) - .id(documentId) - .setRequireAlias(true) - .opType(DocWriteRequest.OpType.CREATE); + IndexRequest indexRequest = new IndexRequest(AnomalyDetectorsIndex.jobStateIndexWriteAlias()).id(documentId) + .setRequireAlias(true) + .opType(DocWriteRequest.OpType.CREATE); ToXContent.MapParams params = new ToXContent.MapParams(Collections.singletonMap(ToXContentParams.FOR_INTERNAL_STORAGE, "true")); try (XContentBuilder builder = XContentFactory.jsonBuilder()) { @@ -464,15 +473,18 @@ public void snapshotMlMeta(MlMetadata mlMetadata, ActionListener listen return; } - AnomalyDetectorsIndex.createStateIndexAndAliasIfNecessary(client, clusterService.state(), expressionResolver, + AnomalyDetectorsIndex.createStateIndexAndAliasIfNecessary( + client, + clusterService.state(), + expressionResolver, MasterNodeRequest.DEFAULT_MASTER_NODE_TIMEOUT, - ActionListener.wrap( - r -> { - executeAsyncWithOrigin(client.threadPool().getThreadContext(), ML_ORIGIN, indexRequest, + ActionListener.wrap(r -> { + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + ML_ORIGIN, + indexRequest, ActionListener.wrap( - indexResponse -> { - listener.onResponse(indexResponse.getResult() == DocWriteResponse.Result.CREATED); - }, + indexResponse -> { listener.onResponse(indexResponse.getResult() == DocWriteResponse.Result.CREATED); }, e -> { if (ExceptionsHelper.unwrapCause(e) instanceof VersionConflictEngineException) { // the snapshot already exists @@ -480,19 +492,18 @@ public void snapshotMlMeta(MlMetadata mlMetadata, ActionListener listen } else { listener.onFailure(e); } - }), + } + ), client::index ); - }, - listener::onFailure - )); + }, listener::onFailure) + ); } private void createConfigIndex(ActionListener listener) { logger.info("creating the .ml-config index"); CreateIndexRequest createIndexRequest = new CreateIndexRequest(MlConfigIndex.indexName()); - try - { + try { createIndexRequest.settings(MlConfigIndex.settings()); createIndexRequest.mapping(MlConfigIndex.mapping()); createIndexRequest.origin(ML_ORIGIN); @@ -502,11 +513,13 @@ private void createConfigIndex(ActionListener listener) { return; } - executeAsyncWithOrigin(client.threadPool().getThreadContext(), ML_ORIGIN, createIndexRequest, - ActionListener.wrap( - r -> listener.onResponse(r.isAcknowledged()), - listener::onFailure - ), client.admin().indices()::create); + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + ML_ORIGIN, + createIndexRequest, + ActionListener.wrap(r -> listener.onResponse(r.isAcknowledged()), listener::onFailure), + client.admin().indices()::create + ); } public static Job updateJobForMigration(Job job) { @@ -534,9 +547,7 @@ public static Job updateJobForMigration(Job job) { * @return Jobs not marked as deleting */ public static List nonDeletingJobs(List jobs) { - return jobs.stream() - .filter(job -> job.isDeleting() == false) - .collect(Collectors.toList()); + return jobs.stream().filter(job -> job.isDeleting() == false).collect(Collectors.toList()); } /** @@ -554,9 +565,7 @@ public static List closedOrUnallocatedJobs(ClusterState clusterState) { openJobIds.removeAll(MlTasks.unassignedJobIds(persistentTasks, clusterState.nodes())); MlMetadata mlMetadata = MlMetadata.getMlMetadata(clusterState); - return mlMetadata.getJobs().values().stream() - .filter(job -> openJobIds.contains(job.getId()) == false) - .collect(Collectors.toList()); + return mlMetadata.getJobs().values().stream().filter(job -> openJobIds.contains(job.getId()) == false).collect(Collectors.toList()); } /** @@ -574,12 +583,14 @@ public static List stoppedOrUnallocatedDatafeeds(ClusterState cl startedDatafeedIds.removeAll(MlTasks.unassignedDatafeedIds(persistentTasks, clusterState.nodes())); MlMetadata mlMetadata = MlMetadata.getMlMetadata(clusterState); - return mlMetadata.getDatafeeds().values().stream() - .filter(datafeedConfig-> startedDatafeedIds.contains(datafeedConfig.getId()) == false) - .collect(Collectors.toList()); + return mlMetadata.getDatafeeds() + .values() + .stream() + .filter(datafeedConfig -> startedDatafeedIds.contains(datafeedConfig.getId()) == false) + .collect(Collectors.toList()); } - public static class JobsAndDatafeeds { + public static class JobsAndDatafeeds { List jobs; List datafeedConfigs; @@ -668,8 +679,12 @@ static Set documentsNotWritten(BulkResponse response) { if (itemResponse.isFailed()) { BulkItemResponse.Failure failure = itemResponse.getFailure(); failedDocumentIds.add(itemResponse.getFailure().getId()); - logger.info("failed to index ml configuration [" + itemResponse.getFailure().getId() + "], " + - itemResponse.getFailure().getMessage()); + logger.info( + "failed to index ml configuration [" + + itemResponse.getFailure().getId() + + "], " + + itemResponse.getFailure().getMessage() + ); } else { logger.info("ml configuration [" + itemResponse.getId() + "] indexed"); } @@ -678,14 +693,12 @@ static Set documentsNotWritten(BulkResponse response) { } static List filterFailedJobConfigWrites(Set failedDocumentIds, List jobs) { - return jobs.stream() - .filter(job -> failedDocumentIds.contains(Job.documentId(job.getId())) == false) - .collect(Collectors.toList()); + return jobs.stream().filter(job -> failedDocumentIds.contains(Job.documentId(job.getId())) == false).collect(Collectors.toList()); } static List filterFailedDatafeedConfigWrites(Set failedDocumentIds, Collection datafeeds) { return datafeeds.stream() - .filter(datafeed -> failedDocumentIds.contains(DatafeedConfig.documentId(datafeed.getId())) == false) - .collect(Collectors.toList()); + .filter(datafeed -> failedDocumentIds.contains(DatafeedConfig.documentId(datafeed.getId())) == false) + .collect(Collectors.toList()); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlDailyMaintenanceService.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlDailyMaintenanceService.java index 5c94a8a5a0d78..e14de7f6b9c01 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlDailyMaintenanceService.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlDailyMaintenanceService.java @@ -18,12 +18,12 @@ import org.elasticsearch.cluster.ClusterName; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.core.Tuple; -import org.elasticsearch.core.Releasable; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException; import org.elasticsearch.common.util.set.Sets; +import org.elasticsearch.core.Releasable; +import org.elasticsearch.core.TimeValue; +import org.elasticsearch.core.Tuple; import org.elasticsearch.persistent.PersistentTasksCustomMetadata; import org.elasticsearch.threadpool.Scheduler; import org.elasticsearch.threadpool.ThreadPool; @@ -70,8 +70,14 @@ public class MlDailyMaintenanceService implements Releasable { private volatile Scheduler.Cancellable cancellable; private volatile float deleteExpiredDataRequestsPerSecond; - MlDailyMaintenanceService(Settings settings, ThreadPool threadPool, Client client, ClusterService clusterService, - MlAssignmentNotifier mlAssignmentNotifier, Supplier scheduleProvider) { + MlDailyMaintenanceService( + Settings settings, + ThreadPool threadPool, + Client client, + ClusterService clusterService, + MlAssignmentNotifier mlAssignmentNotifier, + Supplier scheduleProvider + ) { this.threadPool = Objects.requireNonNull(threadPool); this.client = Objects.requireNonNull(client); this.clusterService = Objects.requireNonNull(clusterService); @@ -80,8 +86,14 @@ public class MlDailyMaintenanceService implements Releasable { this.deleteExpiredDataRequestsPerSecond = MachineLearning.NIGHTLY_MAINTENANCE_REQUESTS_PER_SECOND.get(settings); } - public MlDailyMaintenanceService(Settings settings, ClusterName clusterName, ThreadPool threadPool, - Client client, ClusterService clusterService, MlAssignmentNotifier mlAssignmentNotifier) { + public MlDailyMaintenanceService( + Settings settings, + ClusterName clusterName, + ThreadPool threadPool, + Client client, + ClusterService clusterService, + MlAssignmentNotifier mlAssignmentNotifier + ) { this(settings, threadPool, client, clusterService, mlAssignmentNotifier, () -> delayToNextTime(clusterName)); } @@ -104,11 +116,7 @@ private static TimeValue delayToNextTime(ClusterName clusterName) { int minutesOffset = random.ints(0, MAX_TIME_OFFSET_MINUTES).findFirst().getAsInt(); ZonedDateTime now = ZonedDateTime.now(Clock.systemDefaultZone()); - ZonedDateTime next = now.plusDays(1) - .toLocalDate() - .atStartOfDay(now.getZone()) - .plusMinutes(30) - .plusMinutes(minutesOffset); + ZonedDateTime next = now.plusDays(1).toLocalDate().atStartOfDay(now.getZone()).plusMinutes(30).plusMinutes(minutesOffset); return TimeValue.timeValueMillis(next.toInstant().toEpochMilli() - now.toInstant().toEpochMilli()); } @@ -200,7 +208,8 @@ private void triggerDeleteExpiredDataTask(ActionListener f ML_ORIGIN, DeleteExpiredDataAction.INSTANCE, new DeleteExpiredDataAction.Request(deleteExpiredDataRequestsPerSecond, TimeValue.timeValueHours(8)), - deleteExpiredDataActionListener); + deleteExpiredDataActionListener + ); } // Visible for testing @@ -209,12 +218,11 @@ public void triggerDeleteJobsInStateDeletingWithoutDeletionTask(ActionListener>> deleteJobsActionListener = ActionListener.wrap( deleteJobsResponses -> { - List jobIds = - deleteJobsResponses.stream() - .filter(t -> t.v2().isAcknowledged() == false) - .map(Tuple::v1) - .map(DeleteJobAction.Request::getJobId) - .collect(toList()); + List jobIds = deleteJobsResponses.stream() + .filter(t -> t.v2().isAcknowledged() == false) + .map(Tuple::v1) + .map(DeleteJobAction.Request::getJobId) + .collect(toList()); if (jobIds.isEmpty()) { LOGGER.info("Successfully completed [ML] maintenance task: triggerDeleteJobsInStateDeletingWithoutDeletionTask"); } else { @@ -225,60 +233,59 @@ public void triggerDeleteJobsInStateDeletingWithoutDeletionTask(ActionListener listTasksActionListener = ActionListener.wrap( - listTasksResponse -> { - Set jobsInStateDeleting = jobsInStateDeletingHolder.get(); - Set jobsWithDeletionTask = - listTasksResponse.getTasks().stream() - .filter(t -> t.getDescription() != null) - .filter(t -> t.getDescription().startsWith(DeleteJobAction.DELETION_TASK_DESCRIPTION_PREFIX)) - .map(t -> t.getDescription().substring(DeleteJobAction.DELETION_TASK_DESCRIPTION_PREFIX.length())) - .collect(toSet()); - Set jobsInStateDeletingWithoutDeletionTask = Sets.difference(jobsInStateDeleting, jobsWithDeletionTask); - if (jobsInStateDeletingWithoutDeletionTask.isEmpty()) { - finalListener.onResponse(AcknowledgedResponse.TRUE); - return; - } - TypedChainTaskExecutor> chainTaskExecutor = - new TypedChainTaskExecutor<>(threadPool.executor(ThreadPool.Names.SAME), unused -> true, unused -> true); - for (String jobId : jobsInStateDeletingWithoutDeletionTask) { - DeleteJobAction.Request request = new DeleteJobAction.Request(jobId); - chainTaskExecutor.add( - listener -> - executeAsyncWithOrigin( - client, - ML_ORIGIN, - DeleteJobAction.INSTANCE, - request, - ActionListener.wrap(response -> listener.onResponse(Tuple.tuple(request, response)), listener::onFailure)) - ); - } - chainTaskExecutor.execute(deleteJobsActionListener); - }, - finalListener::onFailure - ); - - ActionListener getJobsActionListener = ActionListener.wrap( - getJobsResponse -> { - Set jobsInStateDeleting = - getJobsResponse.getResponse().results().stream() - .filter(Job::isDeleting) - .map(Job::getId) - .collect(toSet()); - if (jobsInStateDeleting.isEmpty()) { - finalListener.onResponse(AcknowledgedResponse.TRUE); - return; - } - jobsInStateDeletingHolder.set(jobsInStateDeleting); - executeAsyncWithOrigin( - client, - ML_ORIGIN, - ListTasksAction.INSTANCE, - new ListTasksRequest().setActions(DeleteJobAction.NAME), - listTasksActionListener); - }, - finalListener::onFailure - ); + ActionListener listTasksActionListener = ActionListener.wrap(listTasksResponse -> { + Set jobsInStateDeleting = jobsInStateDeletingHolder.get(); + Set jobsWithDeletionTask = listTasksResponse.getTasks() + .stream() + .filter(t -> t.getDescription() != null) + .filter(t -> t.getDescription().startsWith(DeleteJobAction.DELETION_TASK_DESCRIPTION_PREFIX)) + .map(t -> t.getDescription().substring(DeleteJobAction.DELETION_TASK_DESCRIPTION_PREFIX.length())) + .collect(toSet()); + Set jobsInStateDeletingWithoutDeletionTask = Sets.difference(jobsInStateDeleting, jobsWithDeletionTask); + if (jobsInStateDeletingWithoutDeletionTask.isEmpty()) { + finalListener.onResponse(AcknowledgedResponse.TRUE); + return; + } + TypedChainTaskExecutor> chainTaskExecutor = new TypedChainTaskExecutor<>( + threadPool.executor(ThreadPool.Names.SAME), + unused -> true, + unused -> true + ); + for (String jobId : jobsInStateDeletingWithoutDeletionTask) { + DeleteJobAction.Request request = new DeleteJobAction.Request(jobId); + chainTaskExecutor.add( + listener -> executeAsyncWithOrigin( + client, + ML_ORIGIN, + DeleteJobAction.INSTANCE, + request, + ActionListener.wrap(response -> listener.onResponse(Tuple.tuple(request, response)), listener::onFailure) + ) + ); + } + chainTaskExecutor.execute(deleteJobsActionListener); + }, finalListener::onFailure); + + ActionListener getJobsActionListener = ActionListener.wrap(getJobsResponse -> { + Set jobsInStateDeleting = getJobsResponse.getResponse() + .results() + .stream() + .filter(Job::isDeleting) + .map(Job::getId) + .collect(toSet()); + if (jobsInStateDeleting.isEmpty()) { + finalListener.onResponse(AcknowledgedResponse.TRUE); + return; + } + jobsInStateDeletingHolder.set(jobsInStateDeleting); + executeAsyncWithOrigin( + client, + ML_ORIGIN, + ListTasksAction.INSTANCE, + new ListTasksRequest().setActions(DeleteJobAction.NAME), + listTasksActionListener + ); + }, finalListener::onFailure); executeAsyncWithOrigin(client, ML_ORIGIN, GetJobsAction.INSTANCE, new GetJobsAction.Request("*"), getJobsActionListener); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlIndexTemplateRegistry.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlIndexTemplateRegistry.java index 16f9970db900d..005f332923a1e 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlIndexTemplateRegistry.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlIndexTemplateRegistry.java @@ -10,8 +10,8 @@ import org.elasticsearch.client.Client; import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.ClientHelper; import org.elasticsearch.xpack.core.ml.MlStatsIndex; import org.elasticsearch.xpack.core.ml.job.persistence.AnomalyDetectorsIndex; @@ -45,8 +45,10 @@ public class MlIndexTemplateRegistry extends IndexTemplateRegistry { private static final IndexTemplateConfig STATS_TEMPLATE = statsTemplate(); private static final String ML_SIZE_BASED_ILM_POLICY_NAME = "ml-size-based-ilm-policy"; - private static final LifecyclePolicyConfig ML_SIZE_BASED_ILM_POLICY = - new LifecyclePolicyConfig(ML_SIZE_BASED_ILM_POLICY_NAME, ROOT_RESOURCE_PATH + "size_based_ilm_policy.json"); + private static final LifecyclePolicyConfig ML_SIZE_BASED_ILM_POLICY = new LifecyclePolicyConfig( + ML_SIZE_BASED_ILM_POLICY_NAME, + ROOT_RESOURCE_PATH + "size_based_ilm_policy.json" + ); private static IndexTemplateConfig stateTemplate() { Map variables = new HashMap<>(); @@ -54,10 +56,13 @@ private static IndexTemplateConfig stateTemplate() { variables.put(INDEX_LIFECYCLE_NAME, ML_SIZE_BASED_ILM_POLICY_NAME); variables.put(INDEX_LIFECYCLE_ROLLOVER_ALIAS, AnomalyDetectorsIndex.jobStateIndexWriteAlias()); - return new IndexTemplateConfig(AnomalyDetectorsIndexFields.STATE_INDEX_PREFIX, + return new IndexTemplateConfig( + AnomalyDetectorsIndexFields.STATE_INDEX_PREFIX, ANOMALY_DETECTION_PATH + "state_index_template.json", - Version.CURRENT.id, VERSION_PATTERN, - variables); + Version.CURRENT.id, + VERSION_PATTERN, + variables + ); } private static IndexTemplateConfig anomalyDetectionResultsTemplate() { @@ -65,10 +70,13 @@ private static IndexTemplateConfig anomalyDetectionResultsTemplate() { variables.put(VERSION_ID_PATTERN, String.valueOf(Version.CURRENT.id)); variables.put("xpack.ml.anomalydetection.results.mappings", AnomalyDetectorsIndex.resultsMapping()); - return new IndexTemplateConfig(AnomalyDetectorsIndex.jobResultsIndexPrefix(), + return new IndexTemplateConfig( + AnomalyDetectorsIndex.jobResultsIndexPrefix(), ANOMALY_DETECTION_PATH + "results_index_template.json", - Version.CURRENT.id, VERSION_PATTERN, - variables); + Version.CURRENT.id, + VERSION_PATTERN, + variables + ); } private static IndexTemplateConfig notificationsTemplate() { @@ -76,10 +84,13 @@ private static IndexTemplateConfig notificationsTemplate() { variables.put(VERSION_ID_PATTERN, String.valueOf(Version.CURRENT.id)); variables.put("xpack.ml.notifications.mappings", NotificationsIndex.mapping()); - return new IndexTemplateConfig(NotificationsIndex.NOTIFICATIONS_INDEX, + return new IndexTemplateConfig( + NotificationsIndex.NOTIFICATIONS_INDEX, ROOT_RESOURCE_PATH + "notifications_index_template.json", - Version.CURRENT.id, VERSION_PATTERN, - variables); + Version.CURRENT.id, + VERSION_PATTERN, + variables + ); } private static IndexTemplateConfig statsTemplate() { @@ -89,22 +100,31 @@ private static IndexTemplateConfig statsTemplate() { variables.put(INDEX_LIFECYCLE_NAME, ML_SIZE_BASED_ILM_POLICY_NAME); variables.put(INDEX_LIFECYCLE_ROLLOVER_ALIAS, MlStatsIndex.writeAlias()); - return new IndexTemplateConfig(MlStatsIndex.TEMPLATE_NAME, + return new IndexTemplateConfig( + MlStatsIndex.TEMPLATE_NAME, ROOT_RESOURCE_PATH + "stats_index_template.json", - Version.CURRENT.id, VERSION_PATTERN, - variables); + Version.CURRENT.id, + VERSION_PATTERN, + variables + ); } private final List templatesToUse; - public MlIndexTemplateRegistry(Settings nodeSettings, ClusterService clusterService, ThreadPool threadPool, Client client, - NamedXContentRegistry xContentRegistry) { + public MlIndexTemplateRegistry( + Settings nodeSettings, + ClusterService clusterService, + ThreadPool threadPool, + Client client, + NamedXContentRegistry xContentRegistry + ) { super(nodeSettings, clusterService, threadPool, client, xContentRegistry); templatesToUse = Arrays.asList( ANOMALY_DETECTION_RESULTS_TEMPLATE, ANOMALY_DETECTION_STATE_TEMPLATE, NOTIFICATIONS_TEMPLATE, - STATS_TEMPLATE); + STATS_TEMPLATE + ); } @Override diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlInitializationService.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlInitializationService.java index d4f6d69a51738..3ddbdf04ffc1a 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlInitializationService.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlInitializationService.java @@ -59,9 +59,15 @@ public class MlInitializationService implements ClusterStateListener { private boolean isMaster = false; - MlInitializationService(Settings settings, ThreadPool threadPool, ClusterService clusterService, Client client, - MlAssignmentNotifier mlAssignmentNotifier) { - this(client, + MlInitializationService( + Settings settings, + ThreadPool threadPool, + ClusterService clusterService, + Client client, + MlAssignmentNotifier mlAssignmentNotifier + ) { + this( + client, threadPool, new MlDailyMaintenanceService( settings, @@ -71,12 +77,17 @@ public class MlInitializationService implements ClusterStateListener { clusterService, mlAssignmentNotifier ), - clusterService); + clusterService + ); } // For testing - public MlInitializationService(Client client, ThreadPool threadPool, MlDailyMaintenanceService dailyMaintenanceService, - ClusterService clusterService) { + public MlInitializationService( + Client client, + ThreadPool threadPool, + MlDailyMaintenanceService dailyMaintenanceService, + ClusterService clusterService + ) { this.client = Objects.requireNonNull(client); this.threadPool = threadPool; this.mlDailyMaintenanceService = dailyMaintenanceService; @@ -84,10 +95,11 @@ public MlInitializationService(Client client, ThreadPool threadPool, MlDailyMain clusterService.addLifecycleListener(new LifecycleListener() { @Override public void afterStart() { - clusterService.getClusterSettings().addSettingsUpdateConsumer( - MachineLearning.NIGHTLY_MAINTENANCE_REQUESTS_PER_SECOND, - mlDailyMaintenanceService::setDeleteExpiredDataRequestsPerSecond - ); + clusterService.getClusterSettings() + .addSettingsUpdateConsumer( + MachineLearning.NIGHTLY_MAINTENANCE_REQUESTS_PER_SECOND, + mlDailyMaintenanceService::setDeleteExpiredDataRequestsPerSecond + ); } @Override @@ -126,13 +138,15 @@ public void clusterChanged(ClusterChangedEvent event) { // The atomic flag prevents multiple simultaneous attempts to create the // index if there is a flurry of cluster state updates in quick succession if (this.isMaster && isIndexCreationInProgress.compareAndSet(false, true)) { - AnnotationIndex.createAnnotationsIndexIfNecessary(client, event.state(), MasterNodeRequest.DEFAULT_MASTER_NODE_TIMEOUT, - ActionListener.wrap( - r -> isIndexCreationInProgress.set(false), - e -> { - isIndexCreationInProgress.set(false); - logger.error("Error creating ML annotations index or aliases", e); - })); + AnnotationIndex.createAnnotationsIndexIfNecessary( + client, + event.state(), + MasterNodeRequest.DEFAULT_MASTER_NODE_TIMEOUT, + ActionListener.wrap(r -> isIndexCreationInProgress.set(false), e -> { + isIndexCreationInProgress.set(false); + logger.error("Error creating ML annotations index or aliases", e); + }) + ); } } @@ -150,97 +164,80 @@ private void makeMlInternalIndicesHidden() { String[] mlHiddenIndexPatterns = MachineLearning.getMlHiddenIndexPatterns(); // Step 5: Handle errors encountered on the way. - ActionListener finalListener = ActionListener.wrap( - updateAliasesResponse -> { - if (updateAliasesResponse.isAcknowledged() == false) { - logger.error("One or more of the ML internal aliases could not be made hidden."); - return; - } - mlInternalIndicesHidden.set(true); - }, - e -> logger.error("An error occurred while making ML internal indices and aliases hidden", e) - ); + ActionListener finalListener = ActionListener.wrap(updateAliasesResponse -> { + if (updateAliasesResponse.isAcknowledged() == false) { + logger.error("One or more of the ML internal aliases could not be made hidden."); + return; + } + mlInternalIndicesHidden.set(true); + }, e -> logger.error("An error occurred while making ML internal indices and aliases hidden", e)); // Step 4: Extract ML internal aliases that are not hidden and make them hidden. - ActionListener getAliasesResponseListener = ActionListener.wrap( - getAliasesResponse -> { - IndicesAliasesRequest indicesAliasesRequest = new IndicesAliasesRequest(); - for (ObjectObjectCursor> entry : getAliasesResponse.getAliases()) { - String index = entry.key; - String[] nonHiddenAliases = entry.value.stream() - .filter(metadata -> metadata.isHidden() == null || metadata.isHidden() == false) - .map(AliasMetadata::alias) - .toArray(String[]::new); - if (nonHiddenAliases.length == 0) { - continue; - } - indicesAliasesRequest.addAliasAction( - IndicesAliasesRequest.AliasActions.add() - .index(index) - .aliases(entry.value.stream().map(AliasMetadata::alias).toArray(String[]::new)) - .isHidden(true)); - } - if (indicesAliasesRequest.getAliasActions().isEmpty()) { - logger.debug("There are no ML internal aliases that need to be made hidden, [{}]", getAliasesResponse.getAliases()); - finalListener.onResponse(AcknowledgedResponse.TRUE); - return; + ActionListener getAliasesResponseListener = ActionListener.wrap(getAliasesResponse -> { + IndicesAliasesRequest indicesAliasesRequest = new IndicesAliasesRequest(); + for (ObjectObjectCursor> entry : getAliasesResponse.getAliases()) { + String index = entry.key; + String[] nonHiddenAliases = entry.value.stream() + .filter(metadata -> metadata.isHidden() == null || metadata.isHidden() == false) + .map(AliasMetadata::alias) + .toArray(String[]::new); + if (nonHiddenAliases.length == 0) { + continue; } - String indicesWithNonHiddenAliasesString = - indicesAliasesRequest.getAliasActions().stream() - .map(aliasAction -> aliasAction.indices()[0] + ": " + String.join(",", aliasAction.aliases())) - .collect(Collectors.joining("; ")); - logger.debug("The following ML internal aliases will now be made hidden: [{}]", indicesWithNonHiddenAliasesString); - executeAsyncWithOrigin(client, ML_ORIGIN, IndicesAliasesAction.INSTANCE, indicesAliasesRequest, finalListener); - }, - finalListener::onFailure - ); + indicesAliasesRequest.addAliasAction( + IndicesAliasesRequest.AliasActions.add() + .index(index) + .aliases(entry.value.stream().map(AliasMetadata::alias).toArray(String[]::new)) + .isHidden(true) + ); + } + if (indicesAliasesRequest.getAliasActions().isEmpty()) { + logger.debug("There are no ML internal aliases that need to be made hidden, [{}]", getAliasesResponse.getAliases()); + finalListener.onResponse(AcknowledgedResponse.TRUE); + return; + } + String indicesWithNonHiddenAliasesString = indicesAliasesRequest.getAliasActions() + .stream() + .map(aliasAction -> aliasAction.indices()[0] + ": " + String.join(",", aliasAction.aliases())) + .collect(Collectors.joining("; ")); + logger.debug("The following ML internal aliases will now be made hidden: [{}]", indicesWithNonHiddenAliasesString); + executeAsyncWithOrigin(client, ML_ORIGIN, IndicesAliasesAction.INSTANCE, indicesAliasesRequest, finalListener); + }, finalListener::onFailure); // Step 3: Once indices are hidden, fetch ML internal aliases to find out whether the aliases are hidden or not. - ActionListener updateSettingsListener = ActionListener.wrap( - updateSettingsResponse -> { - if (updateSettingsResponse.isAcknowledged() == false) { - logger.error("One or more of the ML internal indices could not be made hidden."); - return; - } - GetAliasesRequest getAliasesRequest = new GetAliasesRequest() - .indices(mlHiddenIndexPatterns) - .indicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN_CLOSED_HIDDEN); - executeAsyncWithOrigin(client, ML_ORIGIN, GetAliasesAction.INSTANCE, getAliasesRequest, getAliasesResponseListener); - }, - finalListener::onFailure - ); + ActionListener updateSettingsListener = ActionListener.wrap(updateSettingsResponse -> { + if (updateSettingsResponse.isAcknowledged() == false) { + logger.error("One or more of the ML internal indices could not be made hidden."); + return; + } + GetAliasesRequest getAliasesRequest = new GetAliasesRequest().indices(mlHiddenIndexPatterns) + .indicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN_CLOSED_HIDDEN); + executeAsyncWithOrigin(client, ML_ORIGIN, GetAliasesAction.INSTANCE, getAliasesRequest, getAliasesResponseListener); + }, finalListener::onFailure); // Step 2: Extract ML internal indices that are not hidden and make them hidden. - ActionListener getSettingsListener = ActionListener.wrap( - getSettingsResponse -> { - String[] nonHiddenIndices = - getSettingsResponse.getIndexToSettings().stream() - .filter(e -> e.getValue().getAsBoolean(SETTING_INDEX_HIDDEN, false) == false) - .map(Map.Entry::getKey) - .toArray(String[]::new); - if (nonHiddenIndices.length == 0) { - logger.debug("There are no ML internal indices that need to be made hidden, [{}]", getSettingsResponse); - updateSettingsListener.onResponse(AcknowledgedResponse.TRUE); - return; - } - String nonHiddenIndicesString = Arrays.stream(nonHiddenIndices).collect(Collectors.joining(", ")); - logger.debug("The following ML internal indices will now be made hidden: [{}]", nonHiddenIndicesString); - UpdateSettingsRequest updateSettingsRequest = - new UpdateSettingsRequest() - .indices(nonHiddenIndices) - .indicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN_CLOSED_HIDDEN) - .settings(Collections.singletonMap(SETTING_INDEX_HIDDEN, true)); - executeAsyncWithOrigin(client, ML_ORIGIN, UpdateSettingsAction.INSTANCE, updateSettingsRequest, updateSettingsListener); - }, - finalListener::onFailure - ); + ActionListener getSettingsListener = ActionListener.wrap(getSettingsResponse -> { + String[] nonHiddenIndices = getSettingsResponse.getIndexToSettings() + .stream() + .filter(e -> e.getValue().getAsBoolean(SETTING_INDEX_HIDDEN, false) == false) + .map(Map.Entry::getKey) + .toArray(String[]::new); + if (nonHiddenIndices.length == 0) { + logger.debug("There are no ML internal indices that need to be made hidden, [{}]", getSettingsResponse); + updateSettingsListener.onResponse(AcknowledgedResponse.TRUE); + return; + } + String nonHiddenIndicesString = Arrays.stream(nonHiddenIndices).collect(Collectors.joining(", ")); + logger.debug("The following ML internal indices will now be made hidden: [{}]", nonHiddenIndicesString); + UpdateSettingsRequest updateSettingsRequest = new UpdateSettingsRequest().indices(nonHiddenIndices) + .indicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN_CLOSED_HIDDEN) + .settings(Collections.singletonMap(SETTING_INDEX_HIDDEN, true)); + executeAsyncWithOrigin(client, ML_ORIGIN, UpdateSettingsAction.INSTANCE, updateSettingsRequest, updateSettingsListener); + }, finalListener::onFailure); // Step 1: Fetch ML internal indices settings to find out whether they are already hidden or not. - GetSettingsRequest getSettingsRequest = - new GetSettingsRequest() - .indices(mlHiddenIndexPatterns) - .indicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN_CLOSED_HIDDEN); + GetSettingsRequest getSettingsRequest = new GetSettingsRequest().indices(mlHiddenIndexPatterns) + .indicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN_CLOSED_HIDDEN); client.admin().indices().getSettings(getSettingsRequest, getSettingsListener); } } - diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlLifeCycleService.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlLifeCycleService.java index 80a5d0890ecaa..48c4e1b9ed0ca 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlLifeCycleService.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlLifeCycleService.java @@ -47,9 +47,14 @@ public class MlLifeCycleService { private final MlMemoryTracker memoryTracker; private volatile Instant shutdownStartTime; - MlLifeCycleService(ClusterService clusterService, DatafeedRunner datafeedRunner, MlController mlController, - AutodetectProcessManager autodetectProcessManager, DataFrameAnalyticsManager analyticsManager, - MlMemoryTracker memoryTracker) { + MlLifeCycleService( + ClusterService clusterService, + DatafeedRunner datafeedRunner, + MlController mlController, + AutodetectProcessManager autodetectProcessManager, + DataFrameAnalyticsManager analyticsManager, + MlMemoryTracker memoryTracker + ) { this.clusterService = Objects.requireNonNull(clusterService); this.datafeedRunner = Objects.requireNonNull(datafeedRunner); this.mlController = Objects.requireNonNull(mlController); @@ -100,8 +105,8 @@ static boolean isNodeSafeToShutdown(String nodeId, ClusterState state, Instant s // TODO: currently only considering anomaly detection jobs - could extend in the future // Ignore failed jobs - the persistent task still exists to remember the failure (because no // persistent task means closed), but these don't need to be relocated to another node. - return MlTasks.nonFailedJobTasksOnNode(tasks, nodeId).isEmpty() && - MlTasks.nonFailedSnapshotUpgradeTasksOnNode(tasks, nodeId).isEmpty(); + return MlTasks.nonFailedJobTasksOnNode(tasks, nodeId).isEmpty() + && MlTasks.nonFailedSnapshotUpgradeTasksOnNode(tasks, nodeId).isEmpty(); } /** @@ -123,7 +128,8 @@ void signalGracefulShutdown(ClusterState state, Collection shutdownNodeI logger.info("Starting node shutdown sequence for ML"); } datafeedRunner.vacateAllDatafeedsOnThisNode( - "previously assigned node [" + state.nodes().getLocalNode().getName() + "] is shutting down"); + "previously assigned node [" + state.nodes().getLocalNode().getName() + "] is shutting down" + ); autodetectProcessManager.vacateOpenJobsOnThisNode(); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlUpgradeModeActionFilter.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlUpgradeModeActionFilter.java index a6dae69b6ece2..31f83bd16ed1a 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlUpgradeModeActionFilter.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlUpgradeModeActionFilter.java @@ -76,8 +76,8 @@ */ class MlUpgradeModeActionFilter extends ActionFilter.Simple { - private static final Set ACTIONS_DISALLOWED_IN_UPGRADE_MODE = - Collections.unmodifiableSet(Sets.newHashSet( + private static final Set ACTIONS_DISALLOWED_IN_UPGRADE_MODE = Collections.unmodifiableSet( + Sets.newHashSet( PutJobAction.NAME, UpdateJobAction.NAME, DeleteJobAction.NAME, @@ -129,14 +129,15 @@ class MlUpgradeModeActionFilter extends ActionFilter.Simple { PutTrainedModelDefinitionPartAction.NAME, PutTrainedModelVocabularyAction.NAME, // NOTE: StopTrainedModelDeploymentAction doesn't mutate internal indices, and technically neither does this action. - // But, preventing new deployments from being created while upgrading is for safety. + // But, preventing new deployments from being created while upgrading is for safety. StartTrainedModelDeploymentAction.NAME, DeleteTrainedModelAction.NAME, DeleteTrainedModelAliasAction.NAME - )); + ) + ); - private static final Set RESET_MODE_EXEMPTIONS = - Collections.unmodifiableSet(Sets.newHashSet( + private static final Set RESET_MODE_EXEMPTIONS = Collections.unmodifiableSet( + Sets.newHashSet( DeleteJobAction.NAME, CloseJobAction.NAME, @@ -150,7 +151,8 @@ class MlUpgradeModeActionFilter extends ActionFilter.Simple { // No other trained model APIs need to be exempted as `StopTrainedModelDeploymentAction` isn't filtered during upgrade mode DeleteTrainedModelAction.NAME - )); + ) + ); // At the time the action filter is installed no cluster state is available, so // initialise to false/false and let the first change event set the real values @@ -172,7 +174,10 @@ protected boolean apply(String action, ActionRequest request, ActionListener } if (localUpgradeResetFlags.isUpgradeMode && ACTIONS_DISALLOWED_IN_UPGRADE_MODE.contains(action)) { throw new ElasticsearchStatusException( - "Cannot perform {} action while upgrade mode is enabled", RestStatus.TOO_MANY_REQUESTS, action); + "Cannot perform {} action while upgrade mode is enabled", + RestStatus.TOO_MANY_REQUESTS, + action + ); } return true; } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportCloseJobAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportCloseJobAction.java index 83b948cca6354..50b98dcbc26e8 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportCloseJobAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportCloseJobAction.java @@ -59,8 +59,11 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; -public class TransportCloseJobAction extends TransportTasksAction { +public class TransportCloseJobAction extends TransportTasksAction< + JobTask, + CloseJobAction.Request, + CloseJobAction.Response, + CloseJobAction.Response> { private static final Logger logger = LogManager.getLogger(TransportCloseJobAction.class); @@ -73,13 +76,28 @@ public class TransportCloseJobAction extends TransportTasksAction(listener, CloseJobAction.Response::new)); + transportService.sendRequest( + nodes.getMasterNode(), + actionName, + request, + new ActionListenerResponseHandler<>(listener, CloseJobAction.Response::new) + ); } } else { /* @@ -125,15 +147,19 @@ protected void doExecute(Task task, CloseJobAction.Request request, ActionListen final TimeValue timeout = request.getCloseTimeout(); PersistentTasksCustomMetadata tasksMetadata = state.getMetadata().custom(PersistentTasksCustomMetadata.TYPE); - jobConfigProvider.expandJobsIds(request.getJobId(), + jobConfigProvider.expandJobsIds( + request.getJobId(), request.allowNoMatch(), true, tasksMetadata, isForce, ActionListener.wrap( - expandedJobIds -> validate(expandedJobIds, isForce, tasksMetadata, ActionListener.wrap( - response -> stopDatafeedsIfNecessary(response, isForce, timeout, tasksMetadata, ActionListener.wrap( - bool -> { + expandedJobIds -> validate( + expandedJobIds, + isForce, + tasksMetadata, + ActionListener.wrap( + response -> stopDatafeedsIfNecessary(response, isForce, timeout, tasksMetadata, ActionListener.wrap(bool -> { request.setOpenJobIds(response.openJobIds.toArray(new String[0])); if (response.openJobIds.isEmpty() && response.closingJobIds.isEmpty()) { listener.onResponse(new CloseJobAction.Response(true)); @@ -152,7 +178,8 @@ protected void doExecute(Task task, CloseJobAction.Request request, ActionListen if (jobTask == null) { // This should not happen, because openJobIds was // derived from the same tasks metadata as jobTask - String msg = "Requested job [" + resolvedJobId + String msg = "Requested job [" + + resolvedJobId + "] be stopped, but job's task could not be found."; assert jobTask != null : msg; logger.error(msg); @@ -160,12 +187,13 @@ protected void doExecute(Task task, CloseJobAction.Request request, ActionListen executorNodes.add(jobTask.getExecutorNode()); } else { // This is the easy case - the job is not currently assigned to a node, so can - // be gracefully stopped simply by removing its persistent task. (Usually a + // be gracefully stopped simply by removing its persistent task. (Usually a // graceful stop cannot be achieved by simply removing the persistent task, but // if the job has no running code then graceful/forceful are basically the same.) // The listener here can be a no-op, as waitForJobClosed() already waits for // these persistent tasks to disappear. - persistentTasksService.sendRemoveRequest(jobTask.getId(), + persistentTasksService.sendRemoveRequest( + jobTask.getId(), ActionListener.wrap( r -> logger.trace( () -> new ParameterizedMessage( @@ -180,19 +208,21 @@ protected void doExecute(Task task, CloseJobAction.Request request, ActionListen ), e ) - )); + ) + ); } } request.setNodes(executorNodes.toArray(new String[0])); normalCloseJob(state, task, request, response.openJobIds, response.closingJobIds, listener); } - }, + }, listener::onFailure)), listener::onFailure - )), - listener::onFailure)), + ) + ), listener::onFailure - )); + ) + ); } } @@ -201,6 +231,7 @@ static class OpenAndClosingIds { openJobIds = new ArrayList<>(); closingJobIds = new ArrayList<>(); } + List openJobIds; List closingJobIds; } @@ -215,8 +246,12 @@ static class OpenAndClosingIds { * @param tasksMetadata Persistent tasks * @param listener Resolved job Ids listener */ - void validate(Collection expandedJobIds, boolean forceClose, PersistentTasksCustomMetadata tasksMetadata, - ActionListener listener) { + void validate( + Collection expandedJobIds, + boolean forceClose, + PersistentTasksCustomMetadata tasksMetadata, + ActionListener listener + ) { OpenAndClosingIds ids = new OpenAndClosingIds(); List failedJobs = new ArrayList<>(); @@ -228,12 +263,14 @@ void validate(Collection expandedJobIds, boolean forceClose, PersistentT if (forceClose == false && failedJobs.size() > 0) { if (expandedJobIds.size() == 1) { listener.onFailure( - ExceptionsHelper.conflictStatusException("cannot close job [{}] because it failed, use force close", - expandedJobIds.iterator().next())); + ExceptionsHelper.conflictStatusException( + "cannot close job [{}] because it failed, use force close", + expandedJobIds.iterator().next() + ) + ); return; } - listener.onFailure( - ExceptionsHelper.conflictStatusException("one or more jobs have state failed, use force close")); + listener.onFailure(ExceptionsHelper.conflictStatusException("one or more jobs have state failed, use force close")); return; } @@ -242,43 +279,47 @@ void validate(Collection expandedJobIds, boolean forceClose, PersistentT listener.onResponse(ids); } - void stopDatafeedsIfNecessary(OpenAndClosingIds jobIds, boolean isForce, TimeValue timeout, PersistentTasksCustomMetadata tasksMetadata, - ActionListener listener) { - datafeedConfigProvider.findDatafeedIdsForJobIds(jobIds.openJobIds, ActionListener.wrap( - datafeedIds -> { - List runningDatafeedIds = datafeedIds - .stream() - .filter(datafeedId -> MlTasks.getDatafeedState(datafeedId, tasksMetadata) != DatafeedState.STOPPED) - .collect(Collectors.toList()); - if (runningDatafeedIds.isEmpty()) { - listener.onResponse(false); - } else { - if (isForce) { - // A datafeed with an end time will gracefully close its job when it stops even if it was force stopped. - // If we didn't do anything about this then it would turn requests to force close jobs into normal close - // requests for those datafeeds, which is undesirable - the caller specifically asked for the job to be - // closed forcefully, skipping the final state persistence to save time. Therefore, before stopping the - // datafeeds in this case we isolate them. An isolated datafeed will NOT close its associated job under - // any circumstances. The downside of doing this is that if the stop datafeeds call fails then this API - // will not have closed any jobs, but will have isolated one or more datafeeds, so the failure will have - // an unexpected side effect. Hopefully the project to combine jobs and datafeeds will be able to improve - // on this. - isolateDatafeeds(jobIds.openJobIds, runningDatafeedIds, ActionListener.wrap( - r -> stopDatafeeds(runningDatafeedIds, true, timeout, listener), - // As things stand this will never be called - see the comment in isolateDatafeeds() for why - listener::onFailure - )); - } else { - stopDatafeeds(runningDatafeedIds, false, timeout, listener); - } - } - }, - listener::onFailure - )); + void stopDatafeedsIfNecessary( + OpenAndClosingIds jobIds, + boolean isForce, + TimeValue timeout, + PersistentTasksCustomMetadata tasksMetadata, + ActionListener listener + ) { + datafeedConfigProvider.findDatafeedIdsForJobIds(jobIds.openJobIds, ActionListener.wrap(datafeedIds -> { + List runningDatafeedIds = datafeedIds.stream() + .filter(datafeedId -> MlTasks.getDatafeedState(datafeedId, tasksMetadata) != DatafeedState.STOPPED) + .collect(Collectors.toList()); + if (runningDatafeedIds.isEmpty()) { + listener.onResponse(false); + } else { + if (isForce) { + // A datafeed with an end time will gracefully close its job when it stops even if it was force stopped. + // If we didn't do anything about this then it would turn requests to force close jobs into normal close + // requests for those datafeeds, which is undesirable - the caller specifically asked for the job to be + // closed forcefully, skipping the final state persistence to save time. Therefore, before stopping the + // datafeeds in this case we isolate them. An isolated datafeed will NOT close its associated job under + // any circumstances. The downside of doing this is that if the stop datafeeds call fails then this API + // will not have closed any jobs, but will have isolated one or more datafeeds, so the failure will have + // an unexpected side effect. Hopefully the project to combine jobs and datafeeds will be able to improve + // on this. + isolateDatafeeds( + jobIds.openJobIds, + runningDatafeedIds, + ActionListener.wrap( + r -> stopDatafeeds(runningDatafeedIds, true, timeout, listener), + // As things stand this will never be called - see the comment in isolateDatafeeds() for why + listener::onFailure + ) + ); + } else { + stopDatafeeds(runningDatafeedIds, false, timeout, listener); + } + } + }, listener::onFailure)); } - private void stopDatafeeds(List runningDatafeedIds, boolean isForce, TimeValue timeout, - ActionListener listener) { + private void stopDatafeeds(List runningDatafeedIds, boolean isForce, TimeValue timeout, ActionListener listener) { StopDatafeedAction.Request request = new StopDatafeedAction.Request(String.join(",", runningDatafeedIds)); request.setForce(isForce); request.setStopTimeout(timeout); @@ -289,9 +330,14 @@ private void stopDatafeeds(List runningDatafeedIds, boolean isForce, Tim request, ActionListener.wrap( r -> listener.onResponse(r.isStopped()), - e -> listener.onFailure(ExceptionsHelper.conflictStatusException( - "failed to close jobs as one or more had started datafeeds that could not be stopped: " + - "started datafeeds [{}], error stopping them [{}]", e, request.getDatafeedId(), e.getMessage()) + e -> listener.onFailure( + ExceptionsHelper.conflictStatusException( + "failed to close jobs as one or more had started datafeeds that could not be stopped: " + + "started datafeeds [{}], error stopping them [{}]", + e, + request.getDatafeedId(), + e.getMessage() + ) ) ) ); @@ -300,46 +346,44 @@ private void stopDatafeeds(List runningDatafeedIds, boolean isForce, Tim void isolateDatafeeds(List openJobs, List runningDatafeedIds, ActionListener listener) { GroupedActionListener groupedListener = new GroupedActionListener( - ActionListener.wrap( - c -> listener.onResponse(null), - e -> { - // This is deliberately NOT an error. The reasoning is as follows: - // - Isolate datafeed just sets a flag on the datafeed, so cannot fail IF it reaches the running datafeed code - // - If the request fails because it cannot get to a node running the datafeed then that will be because either: - // 1. The datafeed isn't assigned to a node - // 2. There's no master node - // - But because close job runs on the master node, it cannot be option 2 - this code is running there - // - In the case where a datafeed isn't assigned to a node, stop and force stop, with and without isolation, are - // all the same - // - So we might as well move onto the next step, which is to force stop these same datafeeds (we know this because - // this is a specialist internal method of closing a job, not a generic isolation method) - // - Force stopping the datafeeds operates purely on the master node (i.e. the current node where this code is - // running), and is a simple cluster state update, so will not fail in the same way - // Unfortunately there is still a race condition here, which is that the datafeed could get assigned to a node - // after the isolation request fails but before we follow up with the force close. In this case force stopping - // the datafeed will gracefully close the associated job if the datafeed has an end time, which is not what we - // want. But this will be a rare edge case. Hopefully the loopholes can be closed during the job/datafeed - // unification project. In the meantime we'll log at a level that is usually enabled, to make diagnosing the - // race condition easier. - logger.info("could not isolate all datafeeds while force closing jobs " + openJobs, e); - listener.onResponse(null); - } - ), - runningDatafeedIds.size()); + ActionListener.wrap(c -> listener.onResponse(null), e -> { + // This is deliberately NOT an error. The reasoning is as follows: + // - Isolate datafeed just sets a flag on the datafeed, so cannot fail IF it reaches the running datafeed code + // - If the request fails because it cannot get to a node running the datafeed then that will be because either: + // 1. The datafeed isn't assigned to a node + // 2. There's no master node + // - But because close job runs on the master node, it cannot be option 2 - this code is running there + // - In the case where a datafeed isn't assigned to a node, stop and force stop, with and without isolation, are + // all the same + // - So we might as well move onto the next step, which is to force stop these same datafeeds (we know this because + // this is a specialist internal method of closing a job, not a generic isolation method) + // - Force stopping the datafeeds operates purely on the master node (i.e. the current node where this code is + // running), and is a simple cluster state update, so will not fail in the same way + // Unfortunately there is still a race condition here, which is that the datafeed could get assigned to a node + // after the isolation request fails but before we follow up with the force close. In this case force stopping + // the datafeed will gracefully close the associated job if the datafeed has an end time, which is not what we + // want. But this will be a rare edge case. Hopefully the loopholes can be closed during the job/datafeed + // unification project. In the meantime we'll log at a level that is usually enabled, to make diagnosing the + // race condition easier. + logger.info("could not isolate all datafeeds while force closing jobs " + openJobs, e); + listener.onResponse(null); + }), + runningDatafeedIds.size() + ); for (String runningDatafeedId : runningDatafeedIds) { IsolateDatafeedAction.Request request = new IsolateDatafeedAction.Request(runningDatafeedId); - ClientHelper.executeAsyncWithOrigin( - client, - ClientHelper.ML_ORIGIN, - IsolateDatafeedAction.INSTANCE, - request, - groupedListener); + ClientHelper.executeAsyncWithOrigin(client, ClientHelper.ML_ORIGIN, IsolateDatafeedAction.INSTANCE, request, groupedListener); } } - static void addJobAccordingToState(String jobId, PersistentTasksCustomMetadata tasksMetadata, - List openJobs, List closingJobs, List failedJobs) { + static void addJobAccordingToState( + String jobId, + PersistentTasksCustomMetadata tasksMetadata, + List openJobs, + List closingJobs, + List failedJobs + ) { JobState jobState = MlTasks.getJobState(jobId, tasksMetadata); switch (jobState) { @@ -358,10 +402,12 @@ static void addJobAccordingToState(String jobId, PersistentTasksCustomMetadata t } } - static TransportCloseJobAction.WaitForCloseRequest buildWaitForCloseRequest(List openJobIds, - List closingJobIds, - PersistentTasksCustomMetadata tasks, - AnomalyDetectionAuditor auditor) { + static TransportCloseJobAction.WaitForCloseRequest buildWaitForCloseRequest( + List openJobIds, + List closingJobIds, + PersistentTasksCustomMetadata tasks, + AnomalyDetectionAuditor auditor + ) { TransportCloseJobAction.WaitForCloseRequest waitForCloseRequest = new TransportCloseJobAction.WaitForCloseRequest(); for (String jobId : openJobIds) { @@ -382,7 +428,6 @@ static TransportCloseJobAction.WaitForCloseRequest buildWaitForCloseRequest(List return waitForCloseRequest; } - @Override protected void taskOperation(CloseJobAction.Request request, JobTask jobTask, ActionListener listener) { JobTaskState taskState = new JobTaskState(JobState.CLOSING, jobTask.getAllocationId(), "close job (api)"); @@ -431,19 +476,20 @@ protected void doRun() { } @Override - protected CloseJobAction.Response newResponse(CloseJobAction.Request request, List tasks, - List taskOperationFailures, - List failedNodeExceptions) { + protected CloseJobAction.Response newResponse( + CloseJobAction.Request request, + List tasks, + List taskOperationFailures, + List failedNodeExceptions + ) { // number of resolved jobs should be equal to the number of tasks, // otherwise something went wrong if (request.getOpenJobIds().length != tasks.size()) { if (taskOperationFailures.isEmpty() == false) { - throw org.elasticsearch.ExceptionsHelper - .convertToElastic(taskOperationFailures.get(0).getCause()); + throw org.elasticsearch.ExceptionsHelper.convertToElastic(taskOperationFailures.get(0).getCause()); } else if (failedNodeExceptions.isEmpty() == false) { - throw org.elasticsearch.ExceptionsHelper - .convertToElastic(failedNodeExceptions.get(0)); + throw org.elasticsearch.ExceptionsHelper.convertToElastic(failedNodeExceptions.get(0)); } else { // This can happen when the actual task in the node no longer exists, // which means the job(s) have already been closed. @@ -454,8 +500,12 @@ protected CloseJobAction.Response newResponse(CloseJobAction.Request request, Li return new CloseJobAction.Response(tasks.stream().allMatch(CloseJobAction.Response::isClosed)); } - private void forceCloseJob(ClusterState currentState, CloseJobAction.Request request, List jobIdsToForceClose, - ActionListener listener) { + private void forceCloseJob( + ClusterState currentState, + CloseJobAction.Request request, + List jobIdsToForceClose, + ActionListener listener + ) { PersistentTasksCustomMetadata tasks = currentState.getMetadata().custom(PersistentTasksCustomMetadata.TYPE); final int numberOfJobs = jobIdsToForceClose.size(); @@ -466,53 +516,63 @@ private void forceCloseJob(ClusterState currentState, CloseJobAction.Request req PersistentTasksCustomMetadata.PersistentTask jobTask = MlTasks.getJobTask(jobId, tasks); if (jobTask != null) { auditor.info(jobId, Messages.JOB_AUDIT_FORCE_CLOSING); - persistentTasksService.sendRemoveRequest(jobTask.getId(), - new ActionListener>() { - @Override - public void onResponse(PersistentTasksCustomMetadata.PersistentTask task) { - if (counter.incrementAndGet() == numberOfJobs) { - sendResponseOrFailure(request.getJobId(), listener, failures); - } + persistentTasksService.sendRemoveRequest( + jobTask.getId(), + new ActionListener>() { + @Override + public void onResponse(PersistentTasksCustomMetadata.PersistentTask task) { + if (counter.incrementAndGet() == numberOfJobs) { + sendResponseOrFailure(request.getJobId(), listener, failures); } + } - @Override - public void onFailure(Exception e) { - final int slot = counter.incrementAndGet(); - if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException == false) { - failures.set(slot - 1, e); - } - if (slot == numberOfJobs) { - sendResponseOrFailure(request.getJobId(), listener, failures); - } + @Override + public void onFailure(Exception e) { + final int slot = counter.incrementAndGet(); + if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException == false) { + failures.set(slot - 1, e); + } + if (slot == numberOfJobs) { + sendResponseOrFailure(request.getJobId(), listener, failures); } + } - private void sendResponseOrFailure(String jobId, - ActionListener listener, - AtomicArray failures) { - List caughtExceptions = failures.asList(); - if (caughtExceptions.size() == 0) { - listener.onResponse(new CloseJobAction.Response(true)); - return; - } + private void sendResponseOrFailure( + String jobId, + ActionListener listener, + AtomicArray failures + ) { + List caughtExceptions = failures.asList(); + if (caughtExceptions.size() == 0) { + listener.onResponse(new CloseJobAction.Response(true)); + return; + } - String msg = "Failed to force close job [" + jobId + "] with [" - + caughtExceptions.size() - + "] failures, rethrowing last, all Exceptions: [" - + caughtExceptions.stream().map(Exception::getMessage) - .collect(Collectors.joining(", ")) - + "]"; + String msg = "Failed to force close job [" + + jobId + + "] with [" + + caughtExceptions.size() + + "] failures, rethrowing last, all Exceptions: [" + + caughtExceptions.stream().map(Exception::getMessage).collect(Collectors.joining(", ")) + + "]"; - ElasticsearchException e = new ElasticsearchException(msg, caughtExceptions.get(0)); - listener.onFailure(e); - } - }); + ElasticsearchException e = new ElasticsearchException(msg, caughtExceptions.get(0)); + listener.onFailure(e); + } + } + ); } } } - private void normalCloseJob(ClusterState currentState, Task task, CloseJobAction.Request request, - List openJobIds, List closingJobIds, - ActionListener listener) { + private void normalCloseJob( + ClusterState currentState, + Task task, + CloseJobAction.Request request, + List openJobIds, + List closingJobIds, + ActionListener listener + ) { PersistentTasksCustomMetadata tasks = currentState.getMetadata().custom(PersistentTasksCustomMetadata.TYPE); WaitForCloseRequest waitForCloseRequest = buildWaitForCloseRequest(openJobIds, closingJobIds, tasks, auditor); @@ -525,24 +585,22 @@ private void normalCloseJob(ClusterState currentState, Task task, CloseJobAction final Set movedJobs = Sets.newConcurrentHashSet(); - ActionListener intermediateListener = ActionListener.wrap( - response -> { - for (String jobId : movedJobs) { - PersistentTasksCustomMetadata.PersistentTask jobTask = MlTasks.getJobTask(jobId, tasks); - persistentTasksService.sendRemoveRequest(jobTask.getId(), ActionListener.wrap( - r -> logger.trace("[{}] removed persistent task for relocated job", jobId), - e -> { - if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) { - logger.debug("[{}] relocated job task already removed", jobId); - } else { - logger.error("[" + jobId + "] failed to remove task to stop relocated job", e); - } - }) - ); - } - listener.onResponse(response); - }, listener::onFailure - ); + ActionListener intermediateListener = ActionListener.wrap(response -> { + for (String jobId : movedJobs) { + PersistentTasksCustomMetadata.PersistentTask jobTask = MlTasks.getJobTask(jobId, tasks); + persistentTasksService.sendRemoveRequest( + jobTask.getId(), + ActionListener.wrap(r -> logger.trace("[{}] removed persistent task for relocated job", jobId), e -> { + if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) { + logger.debug("[{}] relocated job task already removed", jobId); + } else { + logger.error("[" + jobId + "] failed to remove task to stop relocated job", e); + } + }) + ); + } + listener.onResponse(response); + }, listener::onFailure); boolean noOpenJobsToClose = openJobIds.isEmpty(); if (noOpenJobsToClose) { @@ -551,11 +609,10 @@ private void normalCloseJob(ClusterState currentState, Task task, CloseJobAction return; } - ActionListener finalListener = - ActionListener.wrap( - r -> waitForJobClosed(request, waitForCloseRequest, - r, intermediateListener, movedJobs), - listener::onFailure); + ActionListener finalListener = ActionListener.wrap( + r -> waitForJobClosed(request, waitForCloseRequest, r, intermediateListener, movedJobs), + listener::onFailure + ); super.doExecute(task, request, finalListener); } @@ -577,13 +634,19 @@ public boolean hasJobsToWaitFor() { * what to do with them at the beginning of the chain. We cannot simply wait for these, as the request to stop them will have * been sent to the wrong node and ignored there, so we'll just spin until the timeout expires. */ - void waitForJobClosed(CloseJobAction.Request request, WaitForCloseRequest waitForCloseRequest, CloseJobAction.Response response, - ActionListener listener, Set movedJobs) { + void waitForJobClosed( + CloseJobAction.Request request, + WaitForCloseRequest waitForCloseRequest, + CloseJobAction.Response response, + ActionListener listener, + Set movedJobs + ) { persistentTasksService.waitForPersistentTasksCondition(persistentTasksCustomMetadata -> { for (PersistentTasksCustomMetadata.PersistentTask originalPersistentTask : waitForCloseRequest.persistentTasks) { String originalPersistentTaskId = originalPersistentTask.getId(); - PersistentTasksCustomMetadata.PersistentTask currentPersistentTask = - persistentTasksCustomMetadata.getTask(originalPersistentTaskId); + PersistentTasksCustomMetadata.PersistentTask currentPersistentTask = persistentTasksCustomMetadata.getTask( + originalPersistentTaskId + ); if (currentPersistentTask != null) { if (Objects.equals(originalPersistentTask.getExecutorNode(), currentPersistentTask.getExecutorNode()) && originalPersistentTask.getAllocationId() == currentPersistentTask.getAllocationId()) { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteCalendarAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteCalendarAction.java index 2a9d1d506ca71..51515b17e997d 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteCalendarAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteCalendarAction.java @@ -36,9 +36,13 @@ public class TransportDeleteCalendarAction extends HandledTransportAction calendarListener = ActionListener.wrap( - calendar -> { - // Delete calendar and events - DeleteByQueryRequest dbqRequest = buildDeleteByQuery(calendarId); - executeAsyncWithOrigin(client, ML_ORIGIN, DeleteByQueryAction.INSTANCE, dbqRequest, ActionListener.wrap( - response -> { - if (response.getDeleted() == 0) { - listener.onFailure(new ResourceNotFoundException("No calendar with id [" + calendarId + "]")); - return; - } + ActionListener calendarListener = ActionListener.wrap(calendar -> { + // Delete calendar and events + DeleteByQueryRequest dbqRequest = buildDeleteByQuery(calendarId); + executeAsyncWithOrigin(client, ML_ORIGIN, DeleteByQueryAction.INSTANCE, dbqRequest, ActionListener.wrap(response -> { + if (response.getDeleted() == 0) { + listener.onFailure(new ResourceNotFoundException("No calendar with id [" + calendarId + "]")); + return; + } - jobManager.updateProcessOnCalendarChanged(calendar.getJobIds(), ActionListener.wrap( - r -> listener.onResponse(AcknowledgedResponse.TRUE), - listener::onFailure - )); - }, - listener::onFailure)); - }, - listener::onFailure - ); + jobManager.updateProcessOnCalendarChanged( + calendar.getJobIds(), + ActionListener.wrap(r -> listener.onResponse(AcknowledgedResponse.TRUE), listener::onFailure) + ); + }, listener::onFailure)); + }, listener::onFailure); jobResultsProvider.calendar(calendarId, calendarListener); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteCalendarEventAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteCalendarEventAction.java index 08eac12ec5028..e2e72bba0df14 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteCalendarEventAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteCalendarEventAction.java @@ -41,8 +41,13 @@ public class TransportDeleteCalendarEventAction extends HandledTransportAction listener) { + protected void doExecute(Task task, DeleteCalendarEventAction.Request request, ActionListener listener) { final String eventId = request.getEventId(); - ActionListener calendarListener = ActionListener.wrap( - calendar -> { - GetRequest getRequest = new GetRequest(MlMetaIndex.indexName(), eventId); - executeAsyncWithOrigin(client, ML_ORIGIN, GetAction.INSTANCE, getRequest, ActionListener.wrap( - getResponse -> { - if (getResponse.isExists() == false) { - listener.onFailure(new ResourceNotFoundException("No event with id [" + eventId + "]")); - return; - } - - Map source = getResponse.getSourceAsMap(); - String calendarId = (String) source.get(Calendar.ID.getPreferredName()); - if (calendarId == null) { - listener.onFailure(ExceptionsHelper.badRequestException("Event [" + eventId + "] does not have a valid " - + Calendar.ID.getPreferredName())); - return; - } - - if (calendarId.equals(request.getCalendarId()) == false) { - listener.onFailure(ExceptionsHelper.badRequestException( - "Event [" + eventId + "] has " + Calendar.ID.getPreferredName() - + " [" + calendarId + "] which does not match the request " - + Calendar.ID.getPreferredName() + " [" + request.getCalendarId() + "]")); - return; - } - - deleteEvent(eventId, calendar, listener); - }, listener::onFailure) + ActionListener calendarListener = ActionListener.wrap(calendar -> { + GetRequest getRequest = new GetRequest(MlMetaIndex.indexName(), eventId); + executeAsyncWithOrigin(client, ML_ORIGIN, GetAction.INSTANCE, getRequest, ActionListener.wrap(getResponse -> { + if (getResponse.isExists() == false) { + listener.onFailure(new ResourceNotFoundException("No event with id [" + eventId + "]")); + return; + } + + Map source = getResponse.getSourceAsMap(); + String calendarId = (String) source.get(Calendar.ID.getPreferredName()); + if (calendarId == null) { + listener.onFailure( + ExceptionsHelper.badRequestException( + "Event [" + eventId + "] does not have a valid " + Calendar.ID.getPreferredName() + ) ); - }, listener::onFailure); + return; + } + + if (calendarId.equals(request.getCalendarId()) == false) { + listener.onFailure( + ExceptionsHelper.badRequestException( + "Event [" + + eventId + + "] has " + + Calendar.ID.getPreferredName() + + " [" + + calendarId + + "] which does not match the request " + + Calendar.ID.getPreferredName() + + " [" + + request.getCalendarId() + + "]" + ) + ); + return; + } + + deleteEvent(eventId, calendar, listener); + }, listener::onFailure)); + }, listener::onFailure); // Get the calendar first so we check the calendar exists before checking the event exists jobResultsProvider.calendar(request.getCalendarId(), calendarListener); @@ -93,25 +108,24 @@ private void deleteEvent(String eventId, Calendar calendar, ActionListener() { - @Override - public void onResponse(DeleteResponse response) { - - if (response.status() == RestStatus.NOT_FOUND) { - listener.onFailure(new ResourceNotFoundException("No event with id [" + eventId + "]")); - } else { - jobManager.updateProcessOnCalendarChanged(calendar.getJobIds(), ActionListener.wrap( - r -> listener.onResponse(AcknowledgedResponse.TRUE), - listener::onFailure - )); - } - } - - @Override - public void onFailure(Exception e) { - listener.onFailure(ExceptionsHelper.serverError("Could not delete event [" + eventId + "]", e)); - } - }); + executeAsyncWithOrigin(client, ML_ORIGIN, DeleteAction.INSTANCE, deleteRequest, new ActionListener() { + @Override + public void onResponse(DeleteResponse response) { + + if (response.status() == RestStatus.NOT_FOUND) { + listener.onFailure(new ResourceNotFoundException("No event with id [" + eventId + "]")); + } else { + jobManager.updateProcessOnCalendarChanged( + calendar.getJobIds(), + ActionListener.wrap(r -> listener.onResponse(AcknowledgedResponse.TRUE), listener::onFailure) + ); + } + } + + @Override + public void onFailure(Exception e) { + listener.onFailure(ExceptionsHelper.serverError("Could not delete event [" + eventId + "]", e)); + } + }); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteDataFrameAnalyticsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteDataFrameAnalyticsAction.java index 8a26722ec30d2..c79cca811d24c 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteDataFrameAnalyticsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteDataFrameAnalyticsAction.java @@ -46,8 +46,7 @@ * state in order to determine whether there is a persistent task for the analytics * to delete. */ -public class TransportDeleteDataFrameAnalyticsAction - extends AcknowledgedTransportMasterNodeAction { +public class TransportDeleteDataFrameAnalyticsAction extends AcknowledgedTransportMasterNodeAction { private static final Logger logger = LogManager.getLogger(TransportDeleteDataFrameAnalyticsAction.class); @@ -57,13 +56,27 @@ public class TransportDeleteDataFrameAnalyticsAction private final DataFrameAnalyticsAuditor auditor; @Inject - public TransportDeleteDataFrameAnalyticsAction(TransportService transportService, ClusterService clusterService, - ThreadPool threadPool, ActionFilters actionFilters, - IndexNameExpressionResolver indexNameExpressionResolver, Client client, - MlMemoryTracker memoryTracker, DataFrameAnalyticsConfigProvider configProvider, - DataFrameAnalyticsAuditor auditor) { - super(DeleteDataFrameAnalyticsAction.NAME, transportService, clusterService, threadPool, actionFilters, - DeleteDataFrameAnalyticsAction.Request::new, indexNameExpressionResolver, ThreadPool.Names.SAME); + public TransportDeleteDataFrameAnalyticsAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver, + Client client, + MlMemoryTracker memoryTracker, + DataFrameAnalyticsConfigProvider configProvider, + DataFrameAnalyticsAuditor auditor + ) { + super( + DeleteDataFrameAnalyticsAction.NAME, + transportService, + clusterService, + threadPool, + actionFilters, + DeleteDataFrameAnalyticsAction.Request::new, + indexNameExpressionResolver, + ThreadPool.Names.SAME + ); this.client = client; this.memoryTracker = memoryTracker; this.configProvider = configProvider; @@ -71,8 +84,12 @@ public TransportDeleteDataFrameAnalyticsAction(TransportService transportService } @Override - protected void masterOperation(Task task, DeleteDataFrameAnalyticsAction.Request request, ClusterState state, - ActionListener listener) { + protected void masterOperation( + Task task, + DeleteDataFrameAnalyticsAction.Request request, + ClusterState state, + ActionListener listener + ) { TaskId taskId = new TaskId(clusterService.localNode().getId(), task.getId()); ParentTaskAssigningClient parentTaskClient = new ParentTaskAssigningClient(client, taskId); @@ -83,8 +100,11 @@ protected void masterOperation(Task task, DeleteDataFrameAnalyticsAction.Request } } - private void forceDelete(ParentTaskAssigningClient parentTaskClient, DeleteDataFrameAnalyticsAction.Request request, - ActionListener listener) { + private void forceDelete( + ParentTaskAssigningClient parentTaskClient, + DeleteDataFrameAnalyticsAction.Request request, + ActionListener listener + ) { logger.debug("[{}] Force deleting data frame analytics job", request.getId()); ActionListener stopListener = ActionListener.wrap( @@ -95,8 +115,11 @@ private void forceDelete(ParentTaskAssigningClient parentTaskClient, DeleteDataF stopJob(parentTaskClient, request, stopListener); } - private void stopJob(ParentTaskAssigningClient parentTaskClient, DeleteDataFrameAnalyticsAction.Request request, - ActionListener listener) { + private void stopJob( + ParentTaskAssigningClient parentTaskClient, + DeleteDataFrameAnalyticsAction.Request request, + ActionListener listener + ) { // We first try to stop the job normally. Normal stop returns after the job was stopped. // If that fails then we proceed to force stopping which returns as soon as the persistent task is removed. // If we just did force stopping, then there is a chance we proceed to delete the config while it's @@ -109,41 +132,46 @@ private void stopJob(ParentTaskAssigningClient parentTaskClient, DeleteDataFrame listener::onResponse, normalStopFailure -> { stopRequest.setForce(true); - executeAsyncWithOrigin(parentTaskClient, ML_ORIGIN, StopDataFrameAnalyticsAction.INSTANCE, stopRequest, ActionListener.wrap( - listener::onResponse, - forceStopFailure -> { + executeAsyncWithOrigin( + parentTaskClient, + ML_ORIGIN, + StopDataFrameAnalyticsAction.INSTANCE, + stopRequest, + ActionListener.wrap(listener::onResponse, forceStopFailure -> { logger.error(new ParameterizedMessage("[{}] Failed to stop normally", request.getId()), normalStopFailure); logger.error(new ParameterizedMessage("[{}] Failed to stop forcefully", request.getId()), forceStopFailure); listener.onFailure(forceStopFailure); - } - )); + }) + ); } ); executeAsyncWithOrigin(parentTaskClient, ML_ORIGIN, StopDataFrameAnalyticsAction.INSTANCE, stopRequest, normalStopListener); } - private void normalDelete(ParentTaskAssigningClient parentTaskClient, ClusterState state, - DeleteDataFrameAnalyticsAction.Request request, ActionListener listener) { + private void normalDelete( + ParentTaskAssigningClient parentTaskClient, + ClusterState state, + DeleteDataFrameAnalyticsAction.Request request, + ActionListener listener + ) { String id = request.getId(); PersistentTasksCustomMetadata tasks = state.getMetadata().custom(PersistentTasksCustomMetadata.TYPE); DataFrameAnalyticsState taskState = MlTasks.getDataFrameAnalyticsState(id, tasks); if (taskState != DataFrameAnalyticsState.STOPPED) { - listener.onFailure(ExceptionsHelper.conflictStatusException("Cannot delete data frame analytics [{}] while its status is [{}]", - id, taskState)); + listener.onFailure( + ExceptionsHelper.conflictStatusException("Cannot delete data frame analytics [{}] while its status is [{}]", id, taskState) + ); return; } // We clean up the memory tracker on delete because there is no stop; the task stops by itself memoryTracker.removeDataFrameAnalyticsJob(id); - configProvider.get(id, ActionListener.wrap( - config -> { - DataFrameAnalyticsDeleter deleter = new DataFrameAnalyticsDeleter(parentTaskClient, auditor); - deleter.deleteAllDocuments(config, request.timeout(), listener); - }, - listener::onFailure - )); + configProvider.get(id, ActionListener.wrap(config -> { + DataFrameAnalyticsDeleter deleter = new DataFrameAnalyticsDeleter(parentTaskClient, auditor); + deleter.deleteAllDocuments(config, request.timeout(), listener); + }, listener::onFailure)); } @Override diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteDatafeedAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteDatafeedAction.java index 541859f6d6fa5..13eb01bee1d35 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteDatafeedAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteDatafeedAction.java @@ -42,13 +42,27 @@ public class TransportDeleteDatafeedAction extends AcknowledgedTransportMasterNo private final MlConfigMigrationEligibilityCheck migrationEligibilityCheck; @Inject - public TransportDeleteDatafeedAction(Settings settings, TransportService transportService, ClusterService clusterService, - ThreadPool threadPool, ActionFilters actionFilters, - IndexNameExpressionResolver indexNameExpressionResolver, - Client client, PersistentTasksService persistentTasksService, - DatafeedManager datafeedManager) { - super(DeleteDatafeedAction.NAME, transportService, clusterService, threadPool, actionFilters, - DeleteDatafeedAction.Request::new, indexNameExpressionResolver, ThreadPool.Names.SAME); + public TransportDeleteDatafeedAction( + Settings settings, + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver, + Client client, + PersistentTasksService persistentTasksService, + DatafeedManager datafeedManager + ) { + super( + DeleteDatafeedAction.NAME, + transportService, + clusterService, + threadPool, + actionFilters, + DeleteDatafeedAction.Request::new, + indexNameExpressionResolver, + ThreadPool.Names.SAME + ); this.client = client; this.persistentTasksService = persistentTasksService; this.migrationEligibilityCheck = new MlConfigMigrationEligibilityCheck(settings, clusterService); @@ -56,8 +70,12 @@ public TransportDeleteDatafeedAction(Settings settings, TransportService transpo } @Override - protected void masterOperation(Task task, DeleteDatafeedAction.Request request, ClusterState state, - ActionListener listener) { + protected void masterOperation( + Task task, + DeleteDatafeedAction.Request request, + ClusterState state, + ActionListener listener + ) { if (migrationEligibilityCheck.datafeedIsEligibleForMigration(request.getDatafeedId(), state)) { listener.onFailure(ExceptionsHelper.configHasNotBeenMigrated("delete datafeed", request.getDatafeedId())); @@ -71,17 +89,20 @@ protected void masterOperation(Task task, DeleteDatafeedAction.Request request, } } - private void forceDeleteDatafeed(DeleteDatafeedAction.Request request, ClusterState state, - ActionListener listener) { + private void forceDeleteDatafeed( + DeleteDatafeedAction.Request request, + ClusterState state, + ActionListener listener + ) { ActionListener finalListener = ActionListener.wrap( - // use clusterService.state() here so that the updated state without the task is available - response -> datafeedManager.deleteDatafeed(request, clusterService.state(), listener), - listener::onFailure + // use clusterService.state() here so that the updated state without the task is available + response -> datafeedManager.deleteDatafeed(request, clusterService.state(), listener), + listener::onFailure ); ActionListener isolateDatafeedHandler = ActionListener.wrap( - response -> removeDatafeedTask(request, state, finalListener), - listener::onFailure + response -> removeDatafeedTask(request, state, finalListener), + listener::onFailure ); IsolateDatafeedAction.Request isolateDatafeedRequest = new IsolateDatafeedAction.Request(request.getDatafeedId()); @@ -94,23 +115,25 @@ private void removeDatafeedTask(DeleteDatafeedAction.Request request, ClusterSta if (datafeedTask == null) { listener.onResponse(true); } else { - persistentTasksService.sendRemoveRequest(datafeedTask.getId(), - new ActionListener>() { - @Override - public void onResponse(PersistentTasksCustomMetadata.PersistentTask persistentTask) { - listener.onResponse(Boolean.TRUE); - } + persistentTasksService.sendRemoveRequest( + datafeedTask.getId(), + new ActionListener>() { + @Override + public void onResponse(PersistentTasksCustomMetadata.PersistentTask persistentTask) { + listener.onResponse(Boolean.TRUE); + } - @Override - public void onFailure(Exception e) { - if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) { - // the task has been removed in between - listener.onResponse(true); - } else { - listener.onFailure(e); - } + @Override + public void onFailure(Exception e) { + if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) { + // the task has been removed in between + listener.onResponse(true); + } else { + listener.onFailure(e); } - }); + } + } + ); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteExpiredDataAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteExpiredDataAction.java index d8754b447a1eb..7a5b6ff47e4c6 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteExpiredDataAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteExpiredDataAction.java @@ -52,7 +52,8 @@ import java.util.function.BooleanSupplier; import java.util.stream.Collectors; -public class TransportDeleteExpiredDataAction extends HandledTransportAction { private static final Logger logger = LogManager.getLogger(TransportDeleteExpiredDataAction.class); @@ -67,18 +68,42 @@ public class TransportDeleteExpiredDataAction extends HandledTransportAction listener) { + protected void doExecute( + Task task, + DeleteExpiredDataAction.Request request, + ActionListener listener + ) { logger.info("Deleting expired data"); - Instant timeoutTime = Instant.now(clock).plus( - request.getTimeout() == null ? Duration.ofMillis(MlDataRemover.DEFAULT_MAX_DURATION.getMillis()) : - Duration.ofMillis(request.getTimeout().millis()) - ); + Instant timeoutTime = Instant.now(clock) + .plus( + request.getTimeout() == null + ? Duration.ofMillis(MlDataRemover.DEFAULT_MAX_DURATION.getMillis()) + : Duration.ofMillis(request.getTimeout().millis()) + ); TaskId taskId = new TaskId(clusterService.localNode().getId(), task.getId()); @@ -106,81 +136,83 @@ protected void doExecute(Task task, DeleteExpiredDataAction.Request request, if (Strings.isNullOrEmpty(request.getJobId()) || Strings.isAllOrWildcard(request.getJobId())) { List dataRemovers = createDataRemovers(client, taskId, auditor); - threadPool.executor(MachineLearning.UTILITY_THREAD_POOL_NAME).execute( - () -> deleteExpiredData(request, dataRemovers, listener, isTimedOutSupplier) - ); + threadPool.executor(MachineLearning.UTILITY_THREAD_POOL_NAME) + .execute(() -> deleteExpiredData(request, dataRemovers, listener, isTimedOutSupplier)); } else { - jobConfigProvider.expandJobs(request.getJobId(), false, true, ActionListener.wrap( - jobBuilders -> { - threadPool.executor(MachineLearning.UTILITY_THREAD_POOL_NAME).execute(() -> { - List jobs = jobBuilders.stream().map(Job.Builder::build).collect(Collectors.toList()); - String [] jobIds = jobs.stream().map(Job::getId).toArray(String[]::new); - request.setExpandedJobIds(jobIds); - List dataRemovers = createDataRemovers(jobs, taskId, auditor); - deleteExpiredData(request, dataRemovers, listener, isTimedOutSupplier); - } - ); - }, - listener::onFailure - )); + jobConfigProvider.expandJobs(request.getJobId(), false, true, ActionListener.wrap(jobBuilders -> { + threadPool.executor(MachineLearning.UTILITY_THREAD_POOL_NAME).execute(() -> { + List jobs = jobBuilders.stream().map(Job.Builder::build).collect(Collectors.toList()); + String[] jobIds = jobs.stream().map(Job::getId).toArray(String[]::new); + request.setExpandedJobIds(jobIds); + List dataRemovers = createDataRemovers(jobs, taskId, auditor); + deleteExpiredData(request, dataRemovers, listener, isTimedOutSupplier); + }); + }, listener::onFailure)); } } - private void deleteExpiredData(DeleteExpiredDataAction.Request request, - List dataRemovers, - ActionListener listener, - BooleanSupplier isTimedOutSupplier) { + private void deleteExpiredData( + DeleteExpiredDataAction.Request request, + List dataRemovers, + ActionListener listener, + BooleanSupplier isTimedOutSupplier + ) { Iterator dataRemoversIterator = new VolatileCursorIterator<>(dataRemovers); // If there is no throttle provided, default to none float requestsPerSec = request.getRequestsPerSecond() == null ? Float.POSITIVE_INFINITY : request.getRequestsPerSecond(); int numberOfDatanodes = Math.max(clusterService.state().getNodes().getDataNodes().size(), 1); if (requestsPerSec == -1.0f) { // With DEFAULT_SCROLL_SIZE = 1000 and a single data node this implies we spread deletion of - // 1 million documents over 5000 seconds ~= 83 minutes. + // 1 million documents over 5000 seconds ~= 83 minutes. // If we have > 5 data nodes, we don't set our throttling. - requestsPerSec = numberOfDatanodes < 5 ? - (float) (AbstractBulkByScrollRequest.DEFAULT_SCROLL_SIZE / 5) * numberOfDatanodes : - Float.POSITIVE_INFINITY; + requestsPerSec = numberOfDatanodes < 5 + ? (float) (AbstractBulkByScrollRequest.DEFAULT_SCROLL_SIZE / 5) * numberOfDatanodes + : Float.POSITIVE_INFINITY; } deleteExpiredData(request, dataRemoversIterator, requestsPerSec, listener, isTimedOutSupplier, true); } - void deleteExpiredData(DeleteExpiredDataAction.Request request, - Iterator mlDataRemoversIterator, - float requestsPerSecond, - ActionListener listener, - BooleanSupplier isTimedOutSupplier, - boolean haveAllPreviousDeletionsCompleted) { + void deleteExpiredData( + DeleteExpiredDataAction.Request request, + Iterator mlDataRemoversIterator, + float requestsPerSecond, + ActionListener listener, + BooleanSupplier isTimedOutSupplier, + boolean haveAllPreviousDeletionsCompleted + ) { if (haveAllPreviousDeletionsCompleted && mlDataRemoversIterator.hasNext()) { MlDataRemover remover = mlDataRemoversIterator.next(); ActionListener nextListener = ActionListener.wrap( - booleanResponse -> - deleteExpiredData( - request, - mlDataRemoversIterator, - requestsPerSecond, - listener, - isTimedOutSupplier, - booleanResponse - ), - listener::onFailure); + booleanResponse -> deleteExpiredData( + request, + mlDataRemoversIterator, + requestsPerSecond, + listener, + isTimedOutSupplier, + booleanResponse + ), + listener::onFailure + ); // Removing expired ML data and artifacts requires multiple operations. // These are queued up and executed sequentially in the action listener, // the chained calls must all run the ML utility thread pool NOT the thread // the previous action returned in which in the case of a transport_client_boss // thread is a disaster. - remover.remove(requestsPerSecond, new ThreadedActionListener<>(logger, threadPool, executor, nextListener, false), - isTimedOutSupplier); + remover.remove( + requestsPerSecond, + new ThreadedActionListener<>(logger, threadPool, executor, nextListener, false), + isTimedOutSupplier + ); } else { if (haveAllPreviousDeletionsCompleted) { logger.info("Completed deletion of expired ML data"); } else { if (isTimedOutSupplier.getAsBoolean()) { - TimeValue timeoutPeriod = request.getTimeout() == null ? MlDataRemover.DEFAULT_MAX_DURATION : - request.getTimeout(); - String msg = "Deleting expired ML data was cancelled after the timeout period of [" + - timeoutPeriod + "] was exceeded. The setting [xpack.ml.nightly_maintenance_requests_per_second] " + - "controls the deletion rate, consider increasing the value to assist in pruning old data"; + TimeValue timeoutPeriod = request.getTimeout() == null ? MlDataRemover.DEFAULT_MAX_DURATION : request.getTimeout(); + String msg = "Deleting expired ML data was cancelled after the timeout period of [" + + timeoutPeriod + + "] was exceeded. The setting [xpack.ml.nightly_maintenance_requests_per_second] " + + "controls the deletion rate, consider increasing the value to assist in pruning old data"; logger.warn(msg); if (Strings.isNullOrEmpty(request.getJobId()) @@ -200,20 +232,34 @@ void deleteExpiredData(DeleteExpiredDataAction.Request request, } } - private List createDataRemovers(OriginSettingClient client, - TaskId parentTaskId, - AnomalyDetectionAuditor auditor) { + private List createDataRemovers(OriginSettingClient client, TaskId parentTaskId, AnomalyDetectionAuditor auditor) { return Arrays.asList( - new ExpiredResultsRemover(client, - new WrappedBatchedJobsIterator(new SearchAfterJobsIterator(client)), parentTaskId, auditor, threadPool), + new ExpiredResultsRemover( + client, + new WrappedBatchedJobsIterator(new SearchAfterJobsIterator(client)), + parentTaskId, + auditor, + threadPool + ), new ExpiredForecastsRemover(client, threadPool, parentTaskId), - new ExpiredModelSnapshotsRemover(client, - new WrappedBatchedJobsIterator(new SearchAfterJobsIterator(client)), threadPool, parentTaskId, jobResultsProvider, auditor), + new ExpiredModelSnapshotsRemover( + client, + new WrappedBatchedJobsIterator(new SearchAfterJobsIterator(client)), + threadPool, + parentTaskId, + jobResultsProvider, + auditor + ), new UnusedStateRemover(client, parentTaskId), new EmptyStateIndexRemover(client, parentTaskId), new UnusedStatsRemover(client, parentTaskId), - new ExpiredAnnotationsRemover(client, - new WrappedBatchedJobsIterator(new SearchAfterJobsIterator(client)), parentTaskId, auditor, threadPool) + new ExpiredAnnotationsRemover( + client, + new WrappedBatchedJobsIterator(new SearchAfterJobsIterator(client)), + parentTaskId, + auditor, + threadPool + ) ); } @@ -221,11 +267,14 @@ private List createDataRemovers(List jobs, TaskId parentTask return Arrays.asList( new ExpiredResultsRemover(client, new VolatileCursorIterator<>(jobs), parentTaskId, auditor, threadPool), new ExpiredForecastsRemover(client, threadPool, parentTaskId), - new ExpiredModelSnapshotsRemover(client, + new ExpiredModelSnapshotsRemover( + client, new VolatileCursorIterator<>(jobs), - threadPool, parentTaskId, + threadPool, + parentTaskId, jobResultsProvider, - auditor), + auditor + ), new UnusedStateRemover(client, parentTaskId), new EmptyStateIndexRemover(client, parentTaskId), new UnusedStatsRemover(client, parentTaskId), diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteFilterAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteFilterAction.java index 95b2bc467ab68..ff48eec738e3e 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteFilterAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteFilterAction.java @@ -42,9 +42,12 @@ public class TransportDeleteFilterAction extends HandledTransportAction listener) { final String filterId = request.getFilterId(); - jobConfigProvider.findJobsWithCustomRules(ActionListener.wrap( - jobs-> { - List currentlyUsedBy = findJobsUsingFilter(jobs, filterId); - if (currentlyUsedBy.isEmpty() == false) { - listener.onFailure(ExceptionsHelper.conflictStatusException( - Messages.getMessage(Messages.FILTER_CANNOT_DELETE, filterId, currentlyUsedBy))); - } else { - deleteFilter(filterId, listener); - } - }, - listener::onFailure - ) - ); + jobConfigProvider.findJobsWithCustomRules(ActionListener.wrap(jobs -> { + List currentlyUsedBy = findJobsUsingFilter(jobs, filterId); + if (currentlyUsedBy.isEmpty() == false) { + listener.onFailure( + ExceptionsHelper.conflictStatusException(Messages.getMessage(Messages.FILTER_CANNOT_DELETE, filterId, currentlyUsedBy)) + ); + } else { + deleteFilter(filterId, listener); + } + }, listener::onFailure)); } private static List findJobsUsingFilter(List jobs, String filterId) { @@ -87,22 +87,22 @@ private void deleteFilter(String filterId, ActionListener BulkRequestBuilder bulkRequestBuilder = client.prepareBulk(); bulkRequestBuilder.add(deleteRequest); bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - executeAsyncWithOrigin(client, ML_ORIGIN, BulkAction.INSTANCE, bulkRequestBuilder.request(), - new ActionListener() { - @Override - public void onResponse(BulkResponse bulkResponse) { - if (bulkResponse.getItems()[0].status() == RestStatus.NOT_FOUND) { - listener.onFailure(new ResourceNotFoundException("Could not delete filter with ID [" + filterId - + "] because it does not exist")); - } else { - listener.onResponse(AcknowledgedResponse.TRUE); - } + executeAsyncWithOrigin(client, ML_ORIGIN, BulkAction.INSTANCE, bulkRequestBuilder.request(), new ActionListener() { + @Override + public void onResponse(BulkResponse bulkResponse) { + if (bulkResponse.getItems()[0].status() == RestStatus.NOT_FOUND) { + listener.onFailure( + new ResourceNotFoundException("Could not delete filter with ID [" + filterId + "] because it does not exist") + ); + } else { + listener.onResponse(AcknowledgedResponse.TRUE); } + } - @Override - public void onFailure(Exception e) { - listener.onFailure(ExceptionsHelper.serverError("Could not delete filter with ID [" + filterId + "]", e)); - } - }); + @Override + public void onFailure(Exception e) { + listener.onFailure(ExceptionsHelper.serverError("Could not delete filter with ID [" + filterId + "]", e)); + } + }); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteForecastAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteForecastAction.java index 0514fd45ef333..5062e67ac3926 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteForecastAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteForecastAction.java @@ -62,7 +62,6 @@ import static org.elasticsearch.xpack.core.ClientHelper.ML_ORIGIN; import static org.elasticsearch.xpack.core.ClientHelper.executeAsyncWithOrigin; - public class TransportDeleteForecastAction extends HandledTransportAction { private static final Logger logger = LogManager.getLogger(TransportDeleteForecastAction.class); @@ -71,16 +70,17 @@ public class TransportDeleteForecastAction extends HandledTransportAction DELETABLE_STATUSES = - Stream.of(ForecastRequestStatus.FINISHED, ForecastRequestStatus.FAILED) - .map(ForecastRequestStatus::toString) - .collect(toSet()); + private static final Set DELETABLE_STATUSES = Stream.of(ForecastRequestStatus.FINISHED, ForecastRequestStatus.FAILED) + .map(ForecastRequestStatus::toString) + .collect(toSet()); @Inject - public TransportDeleteForecastAction(TransportService transportService, - ActionFilters actionFilters, - Client client, - ClusterService clusterService) { + public TransportDeleteForecastAction( + TransportService transportService, + ActionFilters actionFilters, + Client client, + ClusterService clusterService + ) { super(DeleteForecastAction.NAME, transportService, actionFilters, DeleteForecastAction.Request::new); this.client = client; this.clusterService = clusterService; @@ -98,23 +98,16 @@ protected void doExecute(Task task, DeleteForecastAction.Request request, Action e -> handleFailure(e, request, listener) ); - BoolQueryBuilder query = - QueryBuilders.boolQuery() - .filter(QueryBuilders.termQuery(Result.RESULT_TYPE.getPreferredName(), ForecastRequestStats.RESULT_TYPE_VALUE)); - QueryBuilderHelper - .buildTokenFilterQuery(Forecast.FORECAST_ID.getPreferredName(), forecastIds) - .ifPresent(query::filter); - SearchSourceBuilder source = - new SearchSourceBuilder() - .size(MAX_FORECAST_TO_SEARCH) - // We only need forecast id and status, there is no need fetching the whole source - .fetchSource(false) - .docValueField(ForecastRequestStats.FORECAST_ID.getPreferredName()) - .docValueField(ForecastRequestStats.STATUS.getPreferredName()) - .query(query); - SearchRequest searchRequest = - new SearchRequest(AnomalyDetectorsIndex.jobResultsAliasedName(jobId)) - .source(source); + BoolQueryBuilder query = QueryBuilders.boolQuery() + .filter(QueryBuilders.termQuery(Result.RESULT_TYPE.getPreferredName(), ForecastRequestStats.RESULT_TYPE_VALUE)); + QueryBuilderHelper.buildTokenFilterQuery(Forecast.FORECAST_ID.getPreferredName(), forecastIds).ifPresent(query::filter); + SearchSourceBuilder source = new SearchSourceBuilder().size(MAX_FORECAST_TO_SEARCH) + // We only need forecast id and status, there is no need fetching the whole source + .fetchSource(false) + .docValueField(ForecastRequestStats.FORECAST_ID.getPreferredName()) + .docValueField(ForecastRequestStats.STATUS.getPreferredName()) + .query(query); + SearchRequest searchRequest = new SearchRequest(AnomalyDetectorsIndex.jobResultsAliasedName(jobId)).source(source); executeAsyncWithOrigin(client, ML_ORIGIN, SearchAction.INSTANCE, searchRequest, forecastStatsHandler); } @@ -133,14 +126,17 @@ static List extractForecastIds(SearchHit[] forecastsToDelete, JobState j } if (badStatusForecastIds.size() > 0 && JobState.OPENED.equals(jobState)) { throw ExceptionsHelper.conflictStatusException( - Messages.getMessage(Messages.REST_CANNOT_DELETE_FORECAST_IN_CURRENT_STATE, badStatusForecastIds, jobId)); + Messages.getMessage(Messages.REST_CANNOT_DELETE_FORECAST_IN_CURRENT_STATE, badStatusForecastIds, jobId) + ); } return forecastIds; } - private void deleteForecasts(SearchResponse searchResponse, - DeleteForecastAction.Request request, - ActionListener listener) { + private void deleteForecasts( + SearchResponse searchResponse, + DeleteForecastAction.Request request, + ActionListener listener + ) { final String jobId = request.getJobId(); SearchHits forecastsToDelete = searchResponse.getHits(); @@ -149,7 +145,8 @@ private void deleteForecasts(SearchResponse searchResponse, listener.onResponse(AcknowledgedResponse.TRUE); } else { listener.onFailure( - new ResourceNotFoundException(Messages.getMessage(Messages.REST_NO_SUCH_FORECAST, request.getForecastId(), jobId))); + new ResourceNotFoundException(Messages.getMessage(Messages.REST_NO_SUCH_FORECAST, request.getForecastId(), jobId)) + ); } return; } @@ -165,31 +162,36 @@ private void deleteForecasts(SearchResponse searchResponse, } DeleteByQueryRequest deleteByQueryRequest = buildDeleteByQuery(jobId, forecastIds); - executeAsyncWithOrigin(client, ML_ORIGIN, DeleteByQueryAction.INSTANCE, deleteByQueryRequest, ActionListener.wrap( - response -> { - if (response.isTimedOut()) { - listener.onFailure( - new TimeoutException("Delete request timed out. Successfully deleted " + - response.getDeleted() + " forecast documents from job [" + jobId + "]")); - return; - } - if ((response.getBulkFailures().isEmpty() && response.getSearchFailures().isEmpty()) == false) { - Tuple statusAndReason = getStatusAndReason(response); - listener.onFailure( - new ElasticsearchStatusException(statusAndReason.v2().getMessage(), statusAndReason.v1(), statusAndReason.v2())); - return; - } - logger.info("Deleted forecast(s) [{}] from job [{}]", forecastIds, jobId); - listener.onResponse(AcknowledgedResponse.TRUE); - }, - e -> handleFailure(e, request, listener))); + executeAsyncWithOrigin(client, ML_ORIGIN, DeleteByQueryAction.INSTANCE, deleteByQueryRequest, ActionListener.wrap(response -> { + if (response.isTimedOut()) { + listener.onFailure( + new TimeoutException( + "Delete request timed out. Successfully deleted " + + response.getDeleted() + + " forecast documents from job [" + + jobId + + "]" + ) + ); + return; + } + if ((response.getBulkFailures().isEmpty() && response.getSearchFailures().isEmpty()) == false) { + Tuple statusAndReason = getStatusAndReason(response); + listener.onFailure( + new ElasticsearchStatusException(statusAndReason.v2().getMessage(), statusAndReason.v1(), statusAndReason.v2()) + ); + return; + } + logger.info("Deleted forecast(s) [{}] from job [{}]", forecastIds, jobId); + listener.onResponse(AcknowledgedResponse.TRUE); + }, e -> handleFailure(e, request, listener))); } private static Tuple getStatusAndReason(final BulkByScrollResponse response) { RestStatus status = RestStatus.OK; Throwable reason = new Exception("Unknown error"); - //Getting the max RestStatus is sort of arbitrary, would the user care about 5xx over 4xx? - //Unsure of a better way to return an appropriate and possibly actionable cause to the user. + // Getting the max RestStatus is sort of arbitrary, would the user care about 5xx over 4xx? + // Unsure of a better way to return an appropriate and possibly actionable cause to the user. for (BulkItemResponse.Failure failure : response.getBulkFailures()) { if (failure.getStatus().getStatus() > status.getStatus()) { status = failure.getStatus(); @@ -209,31 +211,35 @@ private static Tuple getStatusAndReason(final BulkByScrol private DeleteByQueryRequest buildDeleteByQuery(String jobId, List forecastsToDelete) { BoolQueryBuilder innerBoolQuery = QueryBuilders.boolQuery() - .must(QueryBuilders.termsQuery(Result.RESULT_TYPE.getPreferredName(), - ForecastRequestStats.RESULT_TYPE_VALUE, Forecast.RESULT_TYPE_VALUE)) - .must(QueryBuilders.termsQuery(Forecast.FORECAST_ID.getPreferredName(), - forecastsToDelete)); + .must( + QueryBuilders.termsQuery( + Result.RESULT_TYPE.getPreferredName(), + ForecastRequestStats.RESULT_TYPE_VALUE, + Forecast.RESULT_TYPE_VALUE + ) + ) + .must(QueryBuilders.termsQuery(Forecast.FORECAST_ID.getPreferredName(), forecastsToDelete)); QueryBuilder query = QueryBuilders.boolQuery().filter(innerBoolQuery); // We want *all* of the docs to be deleted. Hence, we rely on the default value of max_docs. - return new DeleteByQueryRequest() - .setAbortOnVersionConflict(false) // since these documents are not updated, a conflict just means it was deleted previously + return new DeleteByQueryRequest().setAbortOnVersionConflict(false) // since these documents are not updated, a conflict just means + // it was deleted previously .setSlices(AbstractBulkByScrollRequest.AUTO_SLICES) .indices(AnomalyDetectorsIndex.jobResultsAliasedName(jobId)) .setQuery(query) .setRefresh(true); } - private static void handleFailure(Exception e, - DeleteForecastAction.Request request, - ActionListener listener) { + private static void handleFailure(Exception e, DeleteForecastAction.Request request, ActionListener listener) { if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) { if (request.isAllowNoForecasts() && Strings.isAllOrWildcard(request.getForecastId())) { listener.onResponse(AcknowledgedResponse.TRUE); } else { - listener.onFailure(new ResourceNotFoundException( - Messages.getMessage(Messages.REST_NO_SUCH_FORECAST, request.getForecastId(), request.getJobId()) - )); + listener.onFailure( + new ResourceNotFoundException( + Messages.getMessage(Messages.REST_NO_SUCH_FORECAST, request.getForecastId(), request.getJobId()) + ) + ); } } else { listener.onFailure(new ElasticsearchException("An error occurred while searching forecasts to delete", e)); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteJobAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteJobAction.java index 056ec315e5dbe..509ee420ae428 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteJobAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteJobAction.java @@ -24,9 +24,9 @@ import org.elasticsearch.cluster.block.ClusterBlockLevel; import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.Nullable; import org.elasticsearch.persistent.PersistentTasksCustomMetadata; import org.elasticsearch.persistent.PersistentTasksService; import org.elasticsearch.tasks.Task; @@ -84,14 +84,31 @@ public class TransportDeleteJobAction extends AcknowledgedTransportMasterNodeAct private final Map>> listenersByJobId; @Inject - public TransportDeleteJobAction(Settings settings, TransportService transportService, ClusterService clusterService, - ThreadPool threadPool, ActionFilters actionFilters, - IndexNameExpressionResolver indexNameExpressionResolver, PersistentTasksService persistentTasksService, - Client client, AnomalyDetectionAuditor auditor, - JobConfigProvider jobConfigProvider, DatafeedConfigProvider datafeedConfigProvider, - MlMemoryTracker memoryTracker, JobManager jobManager) { - super(DeleteJobAction.NAME, transportService, clusterService, threadPool, actionFilters, - DeleteJobAction.Request::new, indexNameExpressionResolver, ThreadPool.Names.SAME); + public TransportDeleteJobAction( + Settings settings, + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver, + PersistentTasksService persistentTasksService, + Client client, + AnomalyDetectionAuditor auditor, + JobConfigProvider jobConfigProvider, + DatafeedConfigProvider datafeedConfigProvider, + MlMemoryTracker memoryTracker, + JobManager jobManager + ) { + super( + DeleteJobAction.NAME, + transportService, + clusterService, + threadPool, + actionFilters, + DeleteJobAction.Request::new, + indexNameExpressionResolver, + ThreadPool.Names.SAME + ); this.client = client; this.persistentTasksService = persistentTasksService; this.auditor = auditor; @@ -109,8 +126,12 @@ protected ClusterBlockException checkBlock(DeleteJobAction.Request request, Clus } @Override - protected void masterOperation(Task task, DeleteJobAction.Request request, ClusterState state, - ActionListener listener) { + protected void masterOperation( + Task task, + DeleteJobAction.Request request, + ClusterState state, + ActionListener listener + ) { if (migrationEligibilityCheck.jobIsEligibleForMigration(request.getJobId(), state)) { listener.onFailure(ExceptionsHelper.configHasNotBeenMigrated("delete job", request.getJobId())); @@ -129,11 +150,13 @@ protected void masterOperation(Task task, DeleteJobAction.Request request, Clust // Check if there is a deletion task for this job already and if yes wait for it to complete synchronized (listenersByJobId) { if (listenersByJobId.containsKey(request.getJobId())) { - logger.debug(() -> new ParameterizedMessage( - "[{}] Deletion task [{}] will wait for existing deletion task to complete", - request.getJobId(), - task.getId() - )); + logger.debug( + () -> new ParameterizedMessage( + "[{}] Deletion task [{}] will wait for existing deletion task to complete", + request.getJobId(), + task.getId() + ) + ); listenersByJobId.get(request.getJobId()).add(listener); return; } else { @@ -145,50 +168,50 @@ protected void masterOperation(Task task, DeleteJobAction.Request request, Clust // The listener that will be executed at the end of the chain will notify all listeners ActionListener finalListener = ActionListener.wrap( - ack -> notifyListeners(request.getJobId(), ack, null), - e -> { - notifyListeners(request.getJobId(), null, e); - if ((ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) == false) { - auditor.error(request.getJobId(), Messages.getMessage(Messages.JOB_AUDIT_DELETING_FAILED, e.getMessage())); - } + ack -> notifyListeners(request.getJobId(), ack, null), + e -> { + notifyListeners(request.getJobId(), null, e); + if ((ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) == false) { + auditor.error(request.getJobId(), Messages.getMessage(Messages.JOB_AUDIT_DELETING_FAILED, e.getMessage())); } + } ); - ActionListener markAsDeletingListener = ActionListener.wrap( - response -> { - if (request.isForce()) { - forceDeleteJob(parentTaskClient, request, state, finalListener); - } else { - normalDeleteJob(parentTaskClient, request, state, finalListener); - } - }, - finalListener::onFailure); - - ActionListener datafeedDeleteListener = ActionListener.wrap( - response -> { - auditor.info(request.getJobId(), Messages.getMessage(Messages.JOB_AUDIT_DELETING, taskId)); - cancelResetTaskIfExists(request.getJobId(), ActionListener.wrap( - r -> jobConfigProvider.updateJobBlockReason(request.getJobId(), new Blocked(Blocked.Reason.DELETE, taskId), - markAsDeletingListener), + ActionListener markAsDeletingListener = ActionListener.wrap(response -> { + if (request.isForce()) { + forceDeleteJob(parentTaskClient, request, state, finalListener); + } else { + normalDeleteJob(parentTaskClient, request, state, finalListener); + } + }, finalListener::onFailure); + + ActionListener datafeedDeleteListener = ActionListener.wrap(response -> { + auditor.info(request.getJobId(), Messages.getMessage(Messages.JOB_AUDIT_DELETING, taskId)); + cancelResetTaskIfExists( + request.getJobId(), + ActionListener.wrap( + r -> jobConfigProvider.updateJobBlockReason( + request.getJobId(), + new Blocked(Blocked.Reason.DELETE, taskId), + markAsDeletingListener + ), finalListener::onFailure - )); - }, - finalListener::onFailure - ); + ) + ); + }, finalListener::onFailure); ActionListener jobExistsListener = ActionListener.wrap( response -> deleteDatafeedIfNecessary(request, datafeedDeleteListener), e -> { if (request.isForce() && MlTasks.getJobTask(request.getJobId(), state.getMetadata().custom(PersistentTasksCustomMetadata.TYPE)) != null) { - logger.info( - "[{}] config is missing but task exists. Attempting to delete tasks and stop process", - request.getJobId()); + logger.info("[{}] config is missing but task exists. Attempting to delete tasks and stop process", request.getJobId()); forceDeleteJob(parentTaskClient, request, state, finalListener); } else { finalListener.onFailure(e); } - }); + } + ); // First check that the job exists, because we don't want to audit // the beginning of its deletion if it didn't exist in the first place @@ -212,10 +235,12 @@ private void notifyListeners(String jobId, @Nullable AcknowledgedResponse ack, @ } } - private void normalDeleteJob(ParentTaskAssigningClient parentTaskClient, - DeleteJobAction.Request request, - ClusterState state, - ActionListener listener) { + private void normalDeleteJob( + ParentTaskAssigningClient parentTaskClient, + DeleteJobAction.Request request, + ClusterState state, + ActionListener listener + ) { String jobId = request.getJobId(); // We clean up the memory tracker on delete rather than close as close is not a master node action @@ -249,30 +274,32 @@ private void forceDeleteJob( // 2. Cancel the persistent task. This closes the process gracefully so // the process should be killed first. ActionListener killJobListener = ActionListener.wrap( - response -> removePersistentTask(jobId, state, removeTaskListener), - e -> { - if (ExceptionsHelper.unwrapCause(e) instanceof ElasticsearchStatusException) { - // Killing the process marks the task as completed so it - // may have disappeared when we get here - removePersistentTask(jobId, state, removeTaskListener); - } else { - listener.onFailure(e); - } + response -> removePersistentTask(jobId, state, removeTaskListener), + e -> { + if (ExceptionsHelper.unwrapCause(e) instanceof ElasticsearchStatusException) { + // Killing the process marks the task as completed so it + // may have disappeared when we get here + removePersistentTask(jobId, state, removeTaskListener); + } else { + listener.onFailure(e); } + } ); // 1. Kill the job's process killProcess(parentTaskClient, jobId, killJobListener); } - private void killProcess(ParentTaskAssigningClient parentTaskClient, String jobId, - ActionListener listener) { + private void killProcess( + ParentTaskAssigningClient parentTaskClient, + String jobId, + ActionListener listener + ) { KillProcessAction.Request killRequest = new KillProcessAction.Request(jobId); executeAsyncWithOrigin(parentTaskClient, ML_ORIGIN, KillProcessAction.INSTANCE, killRequest, listener); } - private void removePersistentTask(String jobId, ClusterState currentState, - ActionListener listener) { + private void removePersistentTask(String jobId, ClusterState currentState, ActionListener listener) { PersistentTasksCustomMetadata tasks = currentState.getMetadata().custom(PersistentTasksCustomMetadata.TYPE); PersistentTasksCustomMetadata.PersistentTask jobTask = MlTasks.getJobTask(jobId, tasks); @@ -288,15 +315,20 @@ private void checkJobIsNotOpen(String jobId, ClusterState state) { PersistentTasksCustomMetadata.PersistentTask jobTask = MlTasks.getJobTask(jobId, tasks); if (jobTask != null) { JobTaskState jobTaskState = (JobTaskState) jobTask.getState(); - throw ExceptionsHelper.conflictStatusException("Cannot delete job [" + jobId + "] because the job is " - + ((jobTaskState == null) ? JobState.OPENING : jobTaskState.getState())); + throw ExceptionsHelper.conflictStatusException( + "Cannot delete job [" + + jobId + + "] because the job is " + + ((jobTaskState == null) ? JobState.OPENING : jobTaskState.getState()) + ); } } private void deleteDatafeedIfNecessary(DeleteJobAction.Request deleteJobRequest, ActionListener listener) { - datafeedConfigProvider.findDatafeedIdsForJobIds(Collections.singletonList(deleteJobRequest.getJobId()), ActionListener.wrap( - datafeedIds -> { + datafeedConfigProvider.findDatafeedIdsForJobIds( + Collections.singletonList(deleteJobRequest.getJobId()), + ActionListener.wrap(datafeedIds -> { // Since it's only possible to delete a single job at a time there should not be more than one datafeed assert datafeedIds.size() <= 1 : "Expected at most 1 datafeed for a single job, got " + datafeedIds; if (datafeedIds.isEmpty()) { @@ -311,53 +343,53 @@ private void deleteDatafeedIfNecessary(DeleteJobAction.Request deleteJobRequest, ClientHelper.ML_ORIGIN, DeleteDatafeedAction.INSTANCE, deleteDatafeedRequest, - ActionListener.wrap( - listener::onResponse, - e -> { - // It's possible that a simultaneous call to delete the datafeed has deleted it in between - // us finding the datafeed ID and trying to delete it in this method - this is OK - if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) { - listener.onResponse(AcknowledgedResponse.TRUE); - } else { - listener.onFailure(ExceptionsHelper.conflictStatusException( - "failed to delete job [{}] as its datafeed [{}] could not be deleted", e, - deleteJobRequest.getJobId(), deleteDatafeedRequest.getDatafeedId()) - ); - } + ActionListener.wrap(listener::onResponse, e -> { + // It's possible that a simultaneous call to delete the datafeed has deleted it in between + // us finding the datafeed ID and trying to delete it in this method - this is OK + if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) { + listener.onResponse(AcknowledgedResponse.TRUE); + } else { + listener.onFailure( + ExceptionsHelper.conflictStatusException( + "failed to delete job [{}] as its datafeed [{}] could not be deleted", + e, + deleteJobRequest.getJobId(), + deleteDatafeedRequest.getDatafeedId() + ) + ); } - ) + }) ); - }, - listener::onFailure - )); + }, listener::onFailure) + ); } private void cancelResetTaskIfExists(String jobId, ActionListener listener) { - ActionListener jobListener = ActionListener.wrap( - jobBuilder -> { - Job job = jobBuilder.build(); - if (job.getBlocked().getReason() == Blocked.Reason.RESET) { - logger.info("[{}] Cancelling reset task [{}] because delete was requested", jobId, job.getBlocked().getTaskId()); - CancelTasksRequest cancelTasksRequest = new CancelTasksRequest(); - cancelTasksRequest.setReason("deleting job"); - cancelTasksRequest.setActions(ResetJobAction.NAME); - cancelTasksRequest.setTaskId(job.getBlocked().getTaskId()); - executeAsyncWithOrigin(client, ML_ORIGIN, CancelTasksAction.INSTANCE, cancelTasksRequest, ActionListener.wrap( - cancelTasksResponse -> listener.onResponse(true), - e -> { - if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) { - listener.onResponse(true); - } else { - listener.onFailure(e); - } + ActionListener jobListener = ActionListener.wrap(jobBuilder -> { + Job job = jobBuilder.build(); + if (job.getBlocked().getReason() == Blocked.Reason.RESET) { + logger.info("[{}] Cancelling reset task [{}] because delete was requested", jobId, job.getBlocked().getTaskId()); + CancelTasksRequest cancelTasksRequest = new CancelTasksRequest(); + cancelTasksRequest.setReason("deleting job"); + cancelTasksRequest.setActions(ResetJobAction.NAME); + cancelTasksRequest.setTaskId(job.getBlocked().getTaskId()); + executeAsyncWithOrigin( + client, + ML_ORIGIN, + CancelTasksAction.INSTANCE, + cancelTasksRequest, + ActionListener.wrap(cancelTasksResponse -> listener.onResponse(true), e -> { + if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) { + listener.onResponse(true); + } else { + listener.onFailure(e); } - )); - } else { - listener.onResponse(false); - } - }, - listener::onFailure - ); + }) + ); + } else { + listener.onResponse(false); + } + }, listener::onFailure); jobConfigProvider.getJob(jobId, jobListener); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteModelSnapshotAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteModelSnapshotAction.java index bb759b520feeb..c739e501599c6 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteModelSnapshotAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteModelSnapshotAction.java @@ -29,8 +29,7 @@ import java.util.Collections; import java.util.List; -public class TransportDeleteModelSnapshotAction extends HandledTransportAction { +public class TransportDeleteModelSnapshotAction extends HandledTransportAction { private static final Logger logger = LogManager.getLogger(TransportDeleteModelSnapshotAction.class); @@ -40,9 +39,14 @@ public class TransportDeleteModelSnapshotAction extends HandledTransportAction listener) { + protected void doExecute(Task task, DeleteModelSnapshotAction.Request request, ActionListener listener) { // Verify the snapshot exists - jobResultsProvider.modelSnapshots( - request.getJobId(), 0, 1, null, null, null, true, request.getSnapshotId(), - page -> { - List deleteCandidates = page.results(); - if (deleteCandidates.size() > 1) { - logger.warn("More than one model found for [job_id: " + request.getJobId() - + ", snapshot_id: " + request.getSnapshotId() + "] tuple."); - } + jobResultsProvider.modelSnapshots(request.getJobId(), 0, 1, null, null, null, true, request.getSnapshotId(), page -> { + List deleteCandidates = page.results(); + if (deleteCandidates.size() > 1) { + logger.warn( + "More than one model found for [job_id: " + + request.getJobId() + + ", snapshot_id: " + + request.getSnapshotId() + + "] tuple." + ); + } - if (deleteCandidates.isEmpty()) { - listener.onFailure(new ResourceNotFoundException(Messages.getMessage(Messages.REST_NO_SUCH_MODEL_SNAPSHOT, - request.getSnapshotId(), request.getJobId()))); - return; - } - ModelSnapshot deleteCandidate = deleteCandidates.get(0); + if (deleteCandidates.isEmpty()) { + listener.onFailure( + new ResourceNotFoundException( + Messages.getMessage(Messages.REST_NO_SUCH_MODEL_SNAPSHOT, request.getSnapshotId(), request.getJobId()) + ) + ); + return; + } + ModelSnapshot deleteCandidate = deleteCandidates.get(0); - // Verify the snapshot is not being used - jobManager.getJob(request.getJobId(), ActionListener.wrap( - job -> { - String currentModelInUse = job.getModelSnapshotId(); - if (currentModelInUse != null && currentModelInUse.equals(request.getSnapshotId())) { - listener.onFailure( - new IllegalArgumentException(Messages.getMessage(Messages.REST_CANNOT_DELETE_HIGHEST_PRIORITY, - request.getSnapshotId(), request.getJobId()))); - return; - } + // Verify the snapshot is not being used + jobManager.getJob(request.getJobId(), ActionListener.wrap(job -> { + String currentModelInUse = job.getModelSnapshotId(); + if (currentModelInUse != null && currentModelInUse.equals(request.getSnapshotId())) { + listener.onFailure( + new IllegalArgumentException( + Messages.getMessage(Messages.REST_CANNOT_DELETE_HIGHEST_PRIORITY, request.getSnapshotId(), request.getJobId()) + ) + ); + return; + } - // Delete the snapshot and any associated state files - JobDataDeleter deleter = new JobDataDeleter(client, request.getJobId()); - deleter.deleteModelSnapshots(Collections.singletonList(deleteCandidate), - listener.delegateFailure((l, bulkResponse) -> { - String msg = Messages.getMessage(Messages.JOB_AUDIT_SNAPSHOT_DELETED, - deleteCandidate.getSnapshotId(), deleteCandidate.getDescription()); + // Delete the snapshot and any associated state files + JobDataDeleter deleter = new JobDataDeleter(client, request.getJobId()); + deleter.deleteModelSnapshots(Collections.singletonList(deleteCandidate), listener.delegateFailure((l, bulkResponse) -> { + String msg = Messages.getMessage( + Messages.JOB_AUDIT_SNAPSHOT_DELETED, + deleteCandidate.getSnapshotId(), + deleteCandidate.getDescription() + ); - auditor.info(request.getJobId(), msg); - logger.debug(() -> new ParameterizedMessage("[{}] {}", request.getJobId(), msg)); - // We don't care about the bulk response, just that it succeeded - l.onResponse(AcknowledgedResponse.TRUE); - })); - }, - listener::onFailure - )); - }, listener::onFailure); + auditor.info(request.getJobId(), msg); + logger.debug(() -> new ParameterizedMessage("[{}] {}", request.getJobId(), msg)); + // We don't care about the bulk response, just that it succeeded + l.onResponse(AcknowledgedResponse.TRUE); + })); + }, listener::onFailure)); + }, listener::onFailure); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteTrainedModelAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteTrainedModelAction.java index 0e0bb79ef8d9a..247428e406b74 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteTrainedModelAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteTrainedModelAction.java @@ -49,8 +49,7 @@ * The action is a master node action to ensure it reads an up-to-date cluster * state in order to determine if there is a processor referencing the trained model */ -public class TransportDeleteTrainedModelAction - extends AcknowledgedTransportMasterNodeAction { +public class TransportDeleteTrainedModelAction extends AcknowledgedTransportMasterNodeAction { private static final Logger logger = LogManager.getLogger(TransportDeleteTrainedModelAction.class); @@ -59,31 +58,50 @@ public class TransportDeleteTrainedModelAction private final IngestService ingestService; @Inject - public TransportDeleteTrainedModelAction(TransportService transportService, ClusterService clusterService, - ThreadPool threadPool, ActionFilters actionFilters, - IndexNameExpressionResolver indexNameExpressionResolver, - TrainedModelProvider configProvider, InferenceAuditor auditor, - IngestService ingestService) { - super(DeleteTrainedModelAction.NAME, transportService, clusterService, threadPool, actionFilters, - DeleteTrainedModelAction.Request::new, indexNameExpressionResolver, ThreadPool.Names.SAME); + public TransportDeleteTrainedModelAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver, + TrainedModelProvider configProvider, + InferenceAuditor auditor, + IngestService ingestService + ) { + super( + DeleteTrainedModelAction.NAME, + transportService, + clusterService, + threadPool, + actionFilters, + DeleteTrainedModelAction.Request::new, + indexNameExpressionResolver, + ThreadPool.Names.SAME + ); this.trainedModelProvider = configProvider; this.ingestService = ingestService; this.auditor = Objects.requireNonNull(auditor); } @Override - protected void masterOperation(Task task, - DeleteTrainedModelAction.Request request, - ClusterState state, - ActionListener listener) { + protected void masterOperation( + Task task, + DeleteTrainedModelAction.Request request, + ClusterState state, + ActionListener listener + ) { String id = request.getId(); IngestMetadata currentIngestMetadata = state.metadata().custom(IngestMetadata.TYPE); Set referencedModels = getReferencedModelKeys(currentIngestMetadata, ingestService); if (referencedModels.contains(id)) { - listener.onFailure(new ElasticsearchStatusException("Cannot delete model [{}] as it is still referenced by ingest processors", - RestStatus.CONFLICT, - id)); + listener.onFailure( + new ElasticsearchStatusException( + "Cannot delete model [{}] as it is still referenced by ingest processors", + RestStatus.CONFLICT, + id + ) + ); return; } @@ -96,30 +114,29 @@ protected void masterOperation(Task task, } for (String modelAlias : modelAliases) { if (referencedModels.contains(modelAlias)) { - listener.onFailure(new ElasticsearchStatusException( - "Cannot delete model [{}] as it has a model_alias [{}] that is still referenced by ingest processors", - RestStatus.CONFLICT, - id, - modelAlias)); + listener.onFailure( + new ElasticsearchStatusException( + "Cannot delete model [{}] as it has a model_alias [{}] that is still referenced by ingest processors", + RestStatus.CONFLICT, + id, + modelAlias + ) + ); return; } } if (TrainedModelAllocationMetadata.fromState(state).isAllocated(request.getId())) { - listener.onFailure(new ElasticsearchStatusException( - "Cannot delete model [{}] as it is currently deployed", - RestStatus.CONFLICT, - id)); + listener.onFailure( + new ElasticsearchStatusException("Cannot delete model [{}] as it is currently deployed", RestStatus.CONFLICT, id) + ); return; } ActionListener nameDeletionListener = ActionListener.wrap( - ack -> trainedModelProvider.deleteTrainedModel(request.getId(), ActionListener.wrap( - r -> { - auditor.info(request.getId(), "trained model deleted"); - listener.onResponse(AcknowledgedResponse.TRUE); - }, - listener::onFailure - )), + ack -> trainedModelProvider.deleteTrainedModel(request.getId(), ActionListener.wrap(r -> { + auditor.info(request.getId(), "trained model deleted"); + listener.onResponse(AcknowledgedResponse.TRUE); + }, listener::onFailure)), listener::onFailure ); @@ -142,9 +159,9 @@ public ClusterState execute(final ClusterState currentState) { logger.info("[{}] delete model model_aliases {}", request.getId(), modelAliases); modelAliases.forEach(newMetadata::remove); final ModelAliasMetadata modelAliasMetadata = new ModelAliasMetadata(newMetadata); - builder.metadata(Metadata.builder(currentState.getMetadata()) - .putCustom(ModelAliasMetadata.NAME, modelAliasMetadata) - .build()); + builder.metadata( + Metadata.builder(currentState.getMetadata()).putCustom(ModelAliasMetadata.NAME, modelAliasMetadata).build() + ); return builder.build(); } }); @@ -155,15 +172,18 @@ static Set getReferencedModelKeys(IngestMetadata ingestMetadata, IngestS if (ingestMetadata == null) { return allReferencedModelKeys; } - for(Map.Entry entry : ingestMetadata.getPipelines().entrySet()) { + for (Map.Entry entry : ingestMetadata.getPipelines().entrySet()) { String pipelineId = entry.getKey(); Map config = entry.getValue().getConfigAsMap(); try { - Pipeline pipeline = Pipeline.create(pipelineId, + Pipeline pipeline = Pipeline.create( + pipelineId, config, ingestService.getProcessorFactories(), - ingestService.getScriptService()); - pipeline.getProcessors().stream() + ingestService.getScriptService() + ); + pipeline.getProcessors() + .stream() .filter(p -> p instanceof InferenceProcessor) .map(p -> (InferenceProcessor) p) .map(InferenceProcessor::getModelId) @@ -175,7 +195,6 @@ static Set getReferencedModelKeys(IngestMetadata ingestMetadata, IngestS return allReferencedModelKeys; } - @Override protected ClusterBlockException checkBlock(DeleteTrainedModelAction.Request request, ClusterState state) { return state.blocks().globalBlockedException(ClusterBlockLevel.METADATA_WRITE); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteTrainedModelAliasAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteTrainedModelAliasAction.java index 7bf79a35060d6..9398acc09595f 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteTrainedModelAliasAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteTrainedModelAliasAction.java @@ -54,7 +54,8 @@ public TransportDeleteTrainedModelAliasAction( ActionFilters actionFilters, InferenceAuditor auditor, IngestService ingestService, - IndexNameExpressionResolver indexNameExpressionResolver) { + IndexNameExpressionResolver indexNameExpressionResolver + ) { super( DeleteTrainedModelAliasAction.NAME, transportService, @@ -84,10 +85,12 @@ public ClusterState execute(final ClusterState currentState) { }); } - static ClusterState deleteModelAlias(final ClusterState currentState, - final IngestService ingestService, - final InferenceAuditor inferenceAuditor, - final DeleteTrainedModelAliasAction.Request request) { + static ClusterState deleteModelAlias( + final ClusterState currentState, + final IngestService ingestService, + final InferenceAuditor inferenceAuditor, + final DeleteTrainedModelAliasAction.Request request + ) { final ModelAliasMetadata currentMetadata = ModelAliasMetadata.fromState(currentState); final String referencedModel = currentMetadata.getModelId(request.getModelAlias()); if (referencedModel == null) { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportEstimateModelMemoryAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportEstimateModelMemoryAction.java index 96980347e931a..0fef12abdef42 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportEstimateModelMemoryAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportEstimateModelMemoryAction.java @@ -32,23 +32,25 @@ * bytes then the job will be impossible to run successfully, so this is not a * major limitation.) */ -public class TransportEstimateModelMemoryAction - extends HandledTransportAction { +public class TransportEstimateModelMemoryAction extends HandledTransportAction< + EstimateModelMemoryAction.Request, + EstimateModelMemoryAction.Response> { static final ByteSizeValue BASIC_REQUIREMENT = ByteSizeValue.ofMb(10); static final long BYTES_PER_INFLUENCER_VALUE = ByteSizeValue.ofKb(10).getBytes(); private static final long BYTES_IN_MB = ByteSizeValue.ofMb(1).getBytes(); @Inject - public TransportEstimateModelMemoryAction(TransportService transportService, - ActionFilters actionFilters) { + public TransportEstimateModelMemoryAction(TransportService transportService, ActionFilters actionFilters) { super(EstimateModelMemoryAction.NAME, transportService, actionFilters, EstimateModelMemoryAction.Request::new); } @Override - protected void doExecute(Task task, - EstimateModelMemoryAction.Request request, - ActionListener listener) { + protected void doExecute( + Task task, + EstimateModelMemoryAction.Request request, + ActionListener listener + ) { AnalysisConfig analysisConfig = request.getAnalysisConfig(); Map overallCardinality = request.getOverallCardinality(); @@ -64,7 +66,8 @@ protected void doExecute(Task task, static long calculateDetectorsRequirementBytes(AnalysisConfig analysisConfig, Map overallCardinality) { long bucketSpanSeconds = analysisConfig.getBucketSpan().getSeconds(); - return analysisConfig.getDetectors().stream() + return analysisConfig.getDetectors() + .stream() .map(detector -> calculateDetectorRequirementBytes(detector, bucketSpanSeconds, overallCardinality)) .reduce(0L, TransportEstimateModelMemoryAction::addNonNegativeLongsWithMaxValueCap); } @@ -139,28 +142,40 @@ static long calculateDetectorRequirementBytes(Detector detector, long bucketSpan long partitionFieldCardinalityEstimate = 1; String partitionFieldName = detector.getPartitionFieldName(); if (partitionFieldName != null) { - partitionFieldCardinalityEstimate = Math.max(1, - cardinalityEstimate(Detector.PARTITION_FIELD_NAME_FIELD.getPreferredName(), partitionFieldName, overallCardinality, true)); + partitionFieldCardinalityEstimate = Math.max( + 1, + cardinalityEstimate(Detector.PARTITION_FIELD_NAME_FIELD.getPreferredName(), partitionFieldName, overallCardinality, true) + ); } String byFieldName = detector.getByFieldName(); if (byFieldName != null) { - long byFieldCardinalityEstimate = - cardinalityEstimate(Detector.BY_FIELD_NAME_FIELD.getPreferredName(), byFieldName, overallCardinality, true); + long byFieldCardinalityEstimate = cardinalityEstimate( + Detector.BY_FIELD_NAME_FIELD.getPreferredName(), + byFieldName, + overallCardinality, + true + ); // Assume the number of by field values in each partition reduces if the cardinality of both by and partition fields is high // The memory cost of a by field is about 2/3rds that of a partition field - double multiplier = - Math.ceil(reducedCardinality(byFieldCardinalityEstimate, partitionFieldCardinalityEstimate, bucketSpanSeconds) * 2.0 / 3.0); + double multiplier = Math.ceil( + reducedCardinality(byFieldCardinalityEstimate, partitionFieldCardinalityEstimate, bucketSpanSeconds) * 2.0 / 3.0 + ); answer = multiplyNonNegativeLongsWithMaxValueCap(answer, (long) multiplier); } String overFieldName = detector.getOverFieldName(); if (overFieldName != null) { - long overFieldCardinalityEstimate = - cardinalityEstimate(Detector.OVER_FIELD_NAME_FIELD.getPreferredName(), overFieldName, overallCardinality, true); + long overFieldCardinalityEstimate = cardinalityEstimate( + Detector.OVER_FIELD_NAME_FIELD.getPreferredName(), + overFieldName, + overallCardinality, + true + ); // Assume the number of over field values in each partition reduces if the cardinality of both over and partition fields is high - double multiplier = - Math.ceil(reducedCardinality(overFieldCardinalityEstimate, partitionFieldCardinalityEstimate, bucketSpanSeconds)); + double multiplier = Math.ceil( + reducedCardinality(overFieldCardinalityEstimate, partitionFieldCardinalityEstimate, bucketSpanSeconds) + ); // Over fields don't multiply the whole estimate, just add a small amount (estimate 768 bytes) per value answer = addNonNegativeLongsWithMaxValueCap(answer, multiplyNonNegativeLongsWithMaxValueCap(768, (long) multiplier)); } @@ -202,7 +217,7 @@ static long calculateCategorizationRequirementBytes(AnalysisConfig analysisConfi // 20MB is a pretty conservative estimate of the memory requirement for categorization, // providing categorization is working well and not creating large numbers of inappropriate - // categories. Often it is considerably less, but it's very hard to predict from simple + // categories. Often it is considerably less, but it's very hard to predict from simple // statistics, and we have seen some data sets that legitimately create hundreds of // categories, so it's best to allow for this. long memoryPerPartitionMb = 20; @@ -214,8 +229,15 @@ static long calculateCategorizationRequirementBytes(AnalysisConfig analysisConfi for (Detector detector : analysisConfig.getDetectors()) { String partitionFieldName = detector.getPartitionFieldName(); if (partitionFieldName != null) { - relevantPartitionFieldCardinalityEstimate = Math.max(1, cardinalityEstimate( - Detector.PARTITION_FIELD_NAME_FIELD.getPreferredName(), partitionFieldName, overallCardinality, true)); + relevantPartitionFieldCardinalityEstimate = Math.max( + 1, + cardinalityEstimate( + Detector.PARTITION_FIELD_NAME_FIELD.getPreferredName(), + partitionFieldName, + overallCardinality, + true + ) + ); break; } } @@ -234,8 +256,12 @@ static long calculateCategorizationRequirementBytes(AnalysisConfig analysisConfi return ByteSizeValue.ofMb(memoryPerPartitionMb * relevantPartitionFieldCardinalityEstimate).getBytes(); } - static long cardinalityEstimate(String description, String fieldName, Map suppliedCardinailityEstimates, - boolean isOverall) { + static long cardinalityEstimate( + String description, + String fieldName, + Map suppliedCardinailityEstimates, + boolean isOverall + ) { Long suppliedEstimate = suppliedCardinailityEstimates.get(fieldName); if (suppliedEstimate != null) { return suppliedEstimate; @@ -244,8 +270,15 @@ static long cardinalityEstimate(String description, String fieldName, Map { private final ThreadPool threadPool; @@ -41,17 +42,20 @@ public class TransportEvaluateDataFrameAction extends HandledTransportAction listener) { - ActionListener> resultsListener = ActionListener.wrap( - unused -> { - EvaluateDataFrameAction.Response response = - new EvaluateDataFrameAction.Response(request.getEvaluation().getName(), request.getEvaluation().getResults()); - listener.onResponse(response); - }, - listener::onFailure - ); + protected void doExecute( + Task task, + EvaluateDataFrameAction.Request request, + ActionListener listener + ) { + ActionListener> resultsListener = ActionListener.wrap(unused -> { + EvaluateDataFrameAction.Response response = new EvaluateDataFrameAction.Response( + request.getEvaluation().getName(), + request.getEvaluation().getResults() + ); + listener.onResponse(response); + }, listener::onFailure); // Create an immutable collection of parameters to be used by evaluation metrics. EvaluationParameters parameters = new EvaluationParameters(maxBuckets.get()); @@ -100,11 +105,13 @@ private static final class EvaluationExecutor extends TypedChainTaskExecutor true, unused -> true); this.client = client; this.parameters = parameters; @@ -119,19 +126,16 @@ private TypedChainTaskExecutor.ChainTask nextTask() { return listener -> { SearchSourceBuilder searchSourceBuilder = evaluation.buildSearch(parameters, request.getParsedQuery()); SearchRequest searchRequest = new SearchRequest(request.getIndices()).source(searchSourceBuilder); - useSecondaryAuthIfAvailable(securityContext, - () -> client.execute( - SearchAction.INSTANCE, - searchRequest, - ActionListener.wrap( - searchResponse -> { - evaluation.process(searchResponse); - if (evaluation.hasAllResults() == false) { - add(nextTask()); - } - listener.onResponse(null); - }, - listener::onFailure))); + useSecondaryAuthIfAvailable( + securityContext, + () -> client.execute(SearchAction.INSTANCE, searchRequest, ActionListener.wrap(searchResponse -> { + evaluation.process(searchResponse); + if (evaluation.hasAllResults() == false) { + add(nextTask()); + } + listener.onResponse(null); + }, listener::onFailure)) + ); }; } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportExplainDataFrameAnalyticsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportExplainDataFrameAnalyticsAction.java index d751d32bccf0d..cbc4ded936971 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportExplainDataFrameAnalyticsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportExplainDataFrameAnalyticsAction.java @@ -18,10 +18,10 @@ import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.node.DiscoveryNodes; import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.core.Tuple; import org.elasticsearch.license.LicenseUtils; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.tasks.Task; @@ -55,8 +55,9 @@ * Provides explanations on aspects of the given data frame analytics spec like memory estimation, field selection, etc. * Redirects to a different node if the current node is *not* an ML node. */ -public class TransportExplainDataFrameAnalyticsAction - extends HandledTransportAction { +public class TransportExplainDataFrameAnalyticsAction extends HandledTransportAction< + PutDataFrameAnalyticsAction.Request, + ExplainDataFrameAnalyticsAction.Response> { private static final Logger logger = LogManager.getLogger(TransportExplainDataFrameAnalyticsAction.class); private final XPackLicenseState licenseState; @@ -69,14 +70,16 @@ public class TransportExplainDataFrameAnalyticsAction private final Settings settings; @Inject - public TransportExplainDataFrameAnalyticsAction(TransportService transportService, - ActionFilters actionFilters, - ClusterService clusterService, - NodeClient client, - XPackLicenseState licenseState, - MemoryUsageEstimationProcessManager processManager, - Settings settings, - ThreadPool threadPool) { + public TransportExplainDataFrameAnalyticsAction( + TransportService transportService, + ActionFilters actionFilters, + ClusterService clusterService, + NodeClient client, + XPackLicenseState licenseState, + MemoryUsageEstimationProcessManager processManager, + Settings settings, + ThreadPool threadPool + ) { super(ExplainDataFrameAnalyticsAction.NAME, transportService, actionFilters, PutDataFrameAnalyticsAction.Request::new); this.transportService = transportService; this.clusterService = Objects.requireNonNull(clusterService); @@ -85,24 +88,26 @@ public TransportExplainDataFrameAnalyticsAction(TransportService transportServic this.processManager = Objects.requireNonNull(processManager); this.threadPool = threadPool; this.settings = settings; - this.securityContext = XPackSettings.SECURITY_ENABLED.get(settings) ? - new SecurityContext(settings, threadPool.getThreadContext()) : - null; + this.securityContext = XPackSettings.SECURITY_ENABLED.get(settings) + ? new SecurityContext(settings, threadPool.getThreadContext()) + : null; } @Override - protected void doExecute(Task task, - PutDataFrameAnalyticsAction.Request request, - ActionListener listener) { + protected void doExecute( + Task task, + PutDataFrameAnalyticsAction.Request request, + ActionListener listener + ) { if (MachineLearningField.ML_API_FEATURE.check(licenseState) == false) { listener.onFailure(LicenseUtils.newComplianceException(XPackField.MACHINE_LEARNING)); return; } // Since the data_frame_analyzer program will be so short-lived and use so little memory when run - // purely for memory estimation we are happy to run it on nodes that might not be ML nodes. This + // purely for memory estimation we are happy to run it on nodes that might not be ML nodes. This // also helps with the case where there are no ML nodes in the cluster, but lazy ML nodes can be - // added. We know the ML plugin is enabled on the current node, because this code is in it! + // added. We know the ML plugin is enabled on the current node, because this code is in it! DiscoveryNode localNode = clusterService.localNode(); boolean isMlNode = MachineLearning.isMlNode(localNode); if (isMlNode || localNode.isMasterNode() || localNode.canContainData() || localNode.isIngestNode()) { @@ -115,9 +120,11 @@ protected void doExecute(Task task, } } - private void explain(Task task, - PutDataFrameAnalyticsAction.Request request, - ActionListener listener) { + private void explain( + Task task, + PutDataFrameAnalyticsAction.Request request, + ActionListener listener + ) { final ExtractedFieldsDetectorFactory extractedFieldsDetectorFactory = new ExtractedFieldsDetectorFactory( new ParentTaskAssigningClient(client, task.getParentTaskId()) @@ -126,9 +133,9 @@ private void explain(Task task, useSecondaryAuthIfAvailable(this.securityContext, () -> { // Set the auth headers (preferring the secondary headers) to the caller's. // Regardless if the config was previously stored or not. - DataFrameAnalyticsConfig config = new DataFrameAnalyticsConfig.Builder(request.getConfig()) - .setHeaders(filterSecurityHeaders(threadPool.getThreadContext().getHeaders())) - .build(); + DataFrameAnalyticsConfig config = new DataFrameAnalyticsConfig.Builder(request.getConfig()).setHeaders( + filterSecurityHeaders(threadPool.getThreadContext().getHeaders()) + ).build(); extractedFieldsDetectorFactory.createFromSource( config, ActionListener.wrap( @@ -148,16 +155,20 @@ private void explain(Task task, } } - private void explain(Task task, - DataFrameAnalyticsConfig config, - ExtractedFieldsDetector extractedFieldsDetector, - ActionListener listener) { + private void explain( + Task task, + DataFrameAnalyticsConfig config, + ExtractedFieldsDetector extractedFieldsDetector, + ActionListener listener + ) { Tuple> fieldExtraction = extractedFieldsDetector.detect(); if (fieldExtraction.v1().getAllFields().isEmpty()) { - listener.onResponse(new ExplainDataFrameAnalyticsAction.Response( - fieldExtraction.v2(), - new MemoryEstimation(ByteSizeValue.ZERO, ByteSizeValue.ZERO) - )); + listener.onResponse( + new ExplainDataFrameAnalyticsAction.Response( + fieldExtraction.v2(), + new MemoryEstimation(ByteSizeValue.ZERO, ByteSizeValue.ZERO) + ) + ); return; } @@ -174,20 +185,27 @@ private void explain(Task task, * Memory usage estimation spawns an ML C++ process which is * only available on nodes where the ML plugin is enabled. */ - private void estimateMemoryUsage(Task task, - DataFrameAnalyticsConfig config, - ExtractedFields extractedFields, - ActionListener listener) { + private void estimateMemoryUsage( + Task task, + DataFrameAnalyticsConfig config, + ExtractedFields extractedFields, + ActionListener listener + ) { final String estimateMemoryTaskId = "memory_usage_estimation_" + task.getId(); DataFrameDataExtractorFactory extractorFactory = DataFrameDataExtractorFactory.createForSourceIndices( - new ParentTaskAssigningClient(client, task.getParentTaskId()), estimateMemoryTaskId, config, extractedFields); + new ParentTaskAssigningClient(client, task.getParentTaskId()), + estimateMemoryTaskId, + config, + extractedFields + ); processManager.runJobAsync( estimateMemoryTaskId, config, extractorFactory, ActionListener.wrap( result -> listener.onResponse( - new MemoryEstimation(result.getExpectedMemoryWithoutDisk(), result.getExpectedMemoryWithDisk())), + new MemoryEstimation(result.getExpectedMemoryWithoutDisk(), result.getExpectedMemoryWithDisk()) + ), listener::onFailure ) ); @@ -197,12 +215,18 @@ private void estimateMemoryUsage(Task task, * Find a suitable node in the cluster that we can run the memory * estimation process on, and redirect the request to this node. */ - private void redirectToSuitableNode(PutDataFrameAnalyticsAction.Request request, - ActionListener listener) { + private void redirectToSuitableNode( + PutDataFrameAnalyticsAction.Request request, + ActionListener listener + ) { Optional node = findSuitableNode(clusterService.state()); if (node.isPresent()) { - transportService.sendRequest(node.get(), actionName, request, - new ActionListenerResponseHandler<>(listener, ExplainDataFrameAnalyticsAction.Response::new)); + transportService.sendRequest( + node.get(), + actionName, + request, + new ActionListenerResponseHandler<>(listener, ExplainDataFrameAnalyticsAction.Response::new) + ); } else { listener.onFailure(ExceptionsHelper.badRequestException("No ML, data or ingest node to run on")); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportFinalizeJobExecutionAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportFinalizeJobExecutionAction.java index 33cdd7caa97b7..869d2d97857fd 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportFinalizeJobExecutionAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportFinalizeJobExecutionAction.java @@ -45,46 +45,65 @@ public class TransportFinalizeJobExecutionAction extends AcknowledgedTransportMa private final Client client; @Inject - public TransportFinalizeJobExecutionAction(TransportService transportService, ClusterService clusterService, ThreadPool threadPool, - ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver, - Client client) { - super(FinalizeJobExecutionAction.NAME, transportService, clusterService, threadPool, actionFilters, - FinalizeJobExecutionAction.Request::new, indexNameExpressionResolver, ThreadPool.Names.SAME); + public TransportFinalizeJobExecutionAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver, + Client client + ) { + super( + FinalizeJobExecutionAction.NAME, + transportService, + clusterService, + threadPool, + actionFilters, + FinalizeJobExecutionAction.Request::new, + indexNameExpressionResolver, + ThreadPool.Names.SAME + ); this.client = client; } @Override - protected void masterOperation(Task task, FinalizeJobExecutionAction.Request request, ClusterState state, - ActionListener listener) { + protected void masterOperation( + Task task, + FinalizeJobExecutionAction.Request request, + ClusterState state, + ActionListener listener + ) { String jobIdString = String.join(",", request.getJobIds()); logger.debug("finalizing jobs [{}]", jobIdString); - VoidChainTaskExecutor voidChainTaskExecutor = new VoidChainTaskExecutor(threadPool.executor( - MachineLearning.UTILITY_THREAD_POOL_NAME), true); + VoidChainTaskExecutor voidChainTaskExecutor = new VoidChainTaskExecutor( + threadPool.executor(MachineLearning.UTILITY_THREAD_POOL_NAME), + true + ); Map update = Collections.singletonMap(Job.FINISHED_TIME.getPreferredName(), new Date()); - for (String jobId: request.getJobIds()) { + for (String jobId : request.getJobIds()) { UpdateRequest updateRequest = new UpdateRequest(MlConfigIndex.indexName(), Job.documentId(jobId)); updateRequest.retryOnConflict(3); updateRequest.doc(update); updateRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); voidChainTaskExecutor.add(chainedListener -> { - executeAsyncWithOrigin(client, ML_ORIGIN, UpdateAction.INSTANCE, updateRequest, ActionListener.wrap( - updateResponse -> chainedListener.onResponse(null), - chainedListener::onFailure - )); + executeAsyncWithOrigin( + client, + ML_ORIGIN, + UpdateAction.INSTANCE, + updateRequest, + ActionListener.wrap(updateResponse -> chainedListener.onResponse(null), chainedListener::onFailure) + ); }); } - voidChainTaskExecutor.execute(ActionListener.wrap( - aVoids -> { - logger.debug("finalized job [{}]", jobIdString); - listener.onResponse(AcknowledgedResponse.TRUE); - }, - listener::onFailure - )); + voidChainTaskExecutor.execute(ActionListener.wrap(aVoids -> { + logger.debug("finalized job [{}]", jobIdString); + listener.onResponse(AcknowledgedResponse.TRUE); + }, listener::onFailure)); } @Override diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportFlushJobAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportFlushJobAction.java index a684e91b456ee..c75df66598736 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportFlushJobAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportFlushJobAction.java @@ -21,10 +21,22 @@ public class TransportFlushJobAction extends TransportJobTaskAction { @Inject - public TransportFlushJobAction(TransportService transportService, ClusterService clusterService, ActionFilters actionFilters, - AutodetectProcessManager processManager) { - super(FlushJobAction.NAME, clusterService, transportService, actionFilters, - FlushJobAction.Request::new, FlushJobAction.Response::new, ThreadPool.Names.SAME, processManager); + public TransportFlushJobAction( + TransportService transportService, + ClusterService clusterService, + ActionFilters actionFilters, + AutodetectProcessManager processManager + ) { + super( + FlushJobAction.NAME, + clusterService, + transportService, + actionFilters, + FlushJobAction.Request::new, + FlushJobAction.Response::new, + ThreadPool.Names.SAME, + processManager + ); // ThreadPool.Names.SAME, because operations is executed by autodetect worker thread } @@ -47,11 +59,10 @@ protected void taskOperation(FlushJobAction.Request request, JobTask task, Actio timeRangeBuilder.endTime(request.getEnd()); } paramsBuilder.forTimeRange(timeRangeBuilder.build()); - processManager.flushJob(task, paramsBuilder.build(), ActionListener.wrap( - flushAcknowledgement -> { - listener.onResponse(new FlushJobAction.Response(true, - flushAcknowledgement == null ? null : flushAcknowledgement.getLastFinalizedBucketEnd())); - }, listener::onFailure - )); + processManager.flushJob(task, paramsBuilder.build(), ActionListener.wrap(flushAcknowledgement -> { + listener.onResponse( + new FlushJobAction.Response(true, flushAcknowledgement == null ? null : flushAcknowledgement.getLastFinalizedBucketEnd()) + ); + }, listener::onFailure)); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportForecastJobAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportForecastJobAction.java index e6a35dd49fa53..77f20867fc3c9 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportForecastJobAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportForecastJobAction.java @@ -42,8 +42,7 @@ import static org.elasticsearch.xpack.core.ml.action.ForecastJobAction.Request.DURATION; import static org.elasticsearch.xpack.core.ml.action.ForecastJobAction.Request.FORECAST_LOCAL_STORAGE_LIMIT; -public class TransportForecastJobAction extends TransportJobTaskAction { +public class TransportForecastJobAction extends TransportJobTaskAction { private static final Logger logger = LogManager.getLogger(TransportForecastJobAction.class); @@ -54,14 +53,28 @@ public class TransportForecastJobAction extends TransportJobTaskAction listener) { - jobManager.getJob(task.getJobId(), ActionListener.wrap( - job -> { - validate(job, request); + jobManager.getJob(task.getJobId(), ActionListener.wrap(job -> { + validate(job, request); - ForecastParams.Builder paramsBuilder = ForecastParams.builder(); + ForecastParams.Builder paramsBuilder = ForecastParams.builder(); - if (request.getDuration() != null) { - paramsBuilder.duration(request.getDuration()); - } + if (request.getDuration() != null) { + paramsBuilder.duration(request.getDuration()); + } - if (request.getExpiresIn() != null) { - paramsBuilder.expiresIn(request.getExpiresIn()); - } + if (request.getExpiresIn() != null) { + paramsBuilder.expiresIn(request.getExpiresIn()); + } - Long adjustedLimit = getAdjustedMemoryLimit(job, request.getMaxModelMemory(), auditor); - if (adjustedLimit != null) { - paramsBuilder.maxModelMemory(adjustedLimit); - } + Long adjustedLimit = getAdjustedMemoryLimit(job, request.getMaxModelMemory(), auditor); + if (adjustedLimit != null) { + paramsBuilder.maxModelMemory(adjustedLimit); + } - // tmp storage might be null, we do not log here, because it might not be - // required - Path tmpStorage = nativeStorageProvider.tryGetLocalTmpStorage(task.getDescription(), FORECAST_LOCAL_STORAGE_LIMIT); - if (tmpStorage != null) { - paramsBuilder.tmpStorage(tmpStorage.toString()); - } + // tmp storage might be null, we do not log here, because it might not be + // required + Path tmpStorage = nativeStorageProvider.tryGetLocalTmpStorage(task.getDescription(), FORECAST_LOCAL_STORAGE_LIMIT); + if (tmpStorage != null) { + paramsBuilder.tmpStorage(tmpStorage.toString()); + } - if (cppMinAvailableDiskSpaceBytes >= 0) { - paramsBuilder.minAvailableDiskSpace(cppMinAvailableDiskSpaceBytes); - } + if (cppMinAvailableDiskSpaceBytes >= 0) { + paramsBuilder.minAvailableDiskSpace(cppMinAvailableDiskSpaceBytes); + } - ForecastParams params = paramsBuilder.build(); - processManager.forecastJob(task, params, e -> { - if (e == null) { - getForecastRequestStats(request.getJobId(), params.getForecastId(), listener); - } else { - listener.onFailure(e); - } - }); - }, - listener::onFailure - )); + ForecastParams params = paramsBuilder.build(); + processManager.forecastJob(task, params, e -> { + if (e == null) { + getForecastRequestStats(request.getJobId(), params.getForecastId(), listener); + } else { + listener.onFailure(e); + } + }); + }, listener::onFailure)); } private void getForecastRequestStats(String jobId, String forecastId, ActionListener listener) { Consumer forecastRequestStatsHandler = forecastRequestStats -> { if (forecastRequestStats == null) { // paranoia case, it should not happen that we do not retrieve a result - listener.onFailure(new ElasticsearchException( - "Cannot run forecast: internal error, please check the logs")); + listener.onFailure(new ElasticsearchException("Cannot run forecast: internal error, please check the logs")); } else if (forecastRequestStats.getStatus() == ForecastRequestStats.ForecastRequestStatus.FAILED) { List messages = forecastRequestStats.getMessages(); if (messages.size() > 0) { @@ -131,13 +140,10 @@ private void getForecastRequestStats(String jobId, String forecastId, ActionList message += " Minimum disk space required: [" + processManager.getMinLocalStorageAvailable() + "]"; } - listener.onFailure(ExceptionsHelper.badRequestException("Cannot run forecast: " - + message)); + listener.onFailure(ExceptionsHelper.badRequestException("Cannot run forecast: " + message)); } else { // paranoia case, it should not be possible to have an empty message list - listener.onFailure( - new ElasticsearchException( - "Cannot run forecast: internal error, please check the logs")); + listener.onFailure(new ElasticsearchException("Cannot run forecast: internal error, please check the logs")); } } else { listener.onResponse(new ForecastJobAction.Response(true, forecastId)); @@ -151,16 +157,19 @@ static Long getAdjustedMemoryLimit(Job job, Long requestedLimit, AbstractAuditor if (requestedLimit == null) { return null; } - long jobLimitMegaBytes = job.getAnalysisLimits() == null || job.getAnalysisLimits().getModelMemoryLimit() == null ? - AnalysisLimits.PRE_6_1_DEFAULT_MODEL_MEMORY_LIMIT_MB : - job.getAnalysisLimits().getModelMemoryLimit(); - long allowedMax = (long)(ByteSizeValue.ofMb(jobLimitMegaBytes).getBytes() * 0.40); + long jobLimitMegaBytes = job.getAnalysisLimits() == null || job.getAnalysisLimits().getModelMemoryLimit() == null + ? AnalysisLimits.PRE_6_1_DEFAULT_MODEL_MEMORY_LIMIT_MB + : job.getAnalysisLimits().getModelMemoryLimit(); + long allowedMax = (long) (ByteSizeValue.ofMb(jobLimitMegaBytes).getBytes() * 0.40); long adjustedMax = Math.min(requestedLimit, allowedMax - 1); if (adjustedMax != requestedLimit) { - String msg = "requested forecast memory limit [" + - requestedLimit + - "] bytes is greater than or equal to [" + allowedMax + - "] bytes (40% of the job memory limit). Reducing to [" + adjustedMax + "]."; + String msg = "requested forecast memory limit [" + + requestedLimit + + "] bytes is greater than or equal to [" + + allowedMax + + "] bytes (40% of the job memory limit). Reducing to [" + + adjustedMax + + "]."; logger.warn("[{}] {}", job.getId(), msg); auditor.warning(job.getId(), msg); } @@ -169,8 +178,7 @@ static Long getAdjustedMemoryLimit(Job job, Long requestedLimit, AbstractAuditor static void validate(Job job, ForecastJobAction.Request request) { if (job.getJobVersion() == null || job.getJobVersion().before(Version.fromString("6.1.0"))) { - throw ExceptionsHelper.badRequestException( - "Cannot run forecast because jobs created prior to version 6.1 are not supported"); + throw ExceptionsHelper.badRequestException("Cannot run forecast because jobs created prior to version 6.1 are not supported"); } if (request.getDuration() != null) { @@ -179,8 +187,14 @@ static void validate(Job job, ForecastJobAction.Request request) { if (duration.compareTo(bucketSpan) < 0) { throw ExceptionsHelper.badRequestException( - "[" + DURATION.getPreferredName() + "] must be greater or equal to the bucket span: [" - + duration.getStringRep() + "/" + bucketSpan.getStringRep() + "]"); + "[" + + DURATION.getPreferredName() + + "] must be greater or equal to the bucket span: [" + + duration.getStringRep() + + "/" + + bucketSpan.getStringRep() + + "]" + ); } } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetBucketsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetBucketsAction.java index c73782c8d2024..82ae485642d2c 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetBucketsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetBucketsAction.java @@ -25,8 +25,13 @@ public class TransportGetBucketsAction extends HandledTransportAction listener) { - jobManager.jobExists(request.getJobId(), ActionListener.wrap( - ok -> { - BucketsQueryBuilder query = - new BucketsQueryBuilder().expand(request.isExpand()) - .includeInterim(request.isExcludeInterim() == false) - .start(request.getStart()) - .end(request.getEnd()) - .anomalyScoreThreshold(request.getAnomalyScore()) - .sortField(request.getSort()) - .sortDescending(request.isDescending()); + jobManager.jobExists(request.getJobId(), ActionListener.wrap(ok -> { + BucketsQueryBuilder query = new BucketsQueryBuilder().expand(request.isExpand()) + .includeInterim(request.isExcludeInterim() == false) + .start(request.getStart()) + .end(request.getEnd()) + .anomalyScoreThreshold(request.getAnomalyScore()) + .sortField(request.getSort()) + .sortDescending(request.isDescending()); - if (request.getPageParams() != null) { - query.from(request.getPageParams().getFrom()) - .size(request.getPageParams().getSize()); - } - if (request.getTimestamp() != null) { - query.timestamp(request.getTimestamp()); - } else { - query.start(request.getStart()); - query.end(request.getEnd()); - } - jobResultsProvider.buckets(request.getJobId(), query, q -> - listener.onResponse(new GetBucketsAction.Response(q)), listener::onFailure, client); + if (request.getPageParams() != null) { + query.from(request.getPageParams().getFrom()).size(request.getPageParams().getSize()); + } + if (request.getTimestamp() != null) { + query.timestamp(request.getTimestamp()); + } else { + query.start(request.getStart()); + query.end(request.getEnd()); + } + jobResultsProvider.buckets( + request.getJobId(), + query, + q -> listener.onResponse(new GetBucketsAction.Response(q)), + listener::onFailure, + client + ); - }, - listener::onFailure + }, + listener::onFailure )); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetCalendarEventsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetCalendarEventsAction.java index 71c598e699eee..116bde09cab6f 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetCalendarEventsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetCalendarEventsAction.java @@ -25,72 +25,69 @@ import java.util.Collections; -public class TransportGetCalendarEventsAction extends HandledTransportAction { +public class TransportGetCalendarEventsAction extends HandledTransportAction< + GetCalendarEventsAction.Request, + GetCalendarEventsAction.Response> { private final JobResultsProvider jobResultsProvider; private final JobConfigProvider jobConfigProvider; @Inject - public TransportGetCalendarEventsAction(TransportService transportService, - ActionFilters actionFilters, JobResultsProvider jobResultsProvider, - JobConfigProvider jobConfigProvider) { + public TransportGetCalendarEventsAction( + TransportService transportService, + ActionFilters actionFilters, + JobResultsProvider jobResultsProvider, + JobConfigProvider jobConfigProvider + ) { super(GetCalendarEventsAction.NAME, transportService, actionFilters, GetCalendarEventsAction.Request::new); this.jobResultsProvider = jobResultsProvider; this.jobConfigProvider = jobConfigProvider; } @Override - protected void doExecute(Task task, GetCalendarEventsAction.Request request, - ActionListener listener) { + protected void doExecute( + Task task, + GetCalendarEventsAction.Request request, + ActionListener listener + ) { final String[] calendarId = Strings.splitStringByCommaToArray(request.getCalendarId()); - ActionListener calendarExistsListener = ActionListener.wrap( - r -> { - ScheduledEventsQueryBuilder query = new ScheduledEventsQueryBuilder() - .start(request.getStart()) - .end(request.getEnd()) - .from(request.getPageParams().getFrom()) - .size(request.getPageParams().getSize()) - .calendarIds(calendarId); + ActionListener calendarExistsListener = ActionListener.wrap(r -> { + ScheduledEventsQueryBuilder query = new ScheduledEventsQueryBuilder().start(request.getStart()) + .end(request.getEnd()) + .from(request.getPageParams().getFrom()) + .size(request.getPageParams().getSize()) + .calendarIds(calendarId); - ActionListener> eventsListener = ActionListener.wrap( - events -> { - listener.onResponse(new GetCalendarEventsAction.Response(events)); - }, - listener::onFailure - ); + ActionListener> eventsListener = ActionListener.wrap( + events -> { listener.onResponse(new GetCalendarEventsAction.Response(events)); }, + listener::onFailure + ); - if (request.getJobId() != null) { + if (request.getJobId() != null) { - jobConfigProvider.getJob(request.getJobId(), ActionListener.wrap( - jobBuilder -> { - Job job = jobBuilder.build(); - jobResultsProvider.scheduledEventsForJob(request.getJobId(), job.getGroups(), query, eventsListener); + jobConfigProvider.getJob(request.getJobId(), ActionListener.wrap(jobBuilder -> { + Job job = jobBuilder.build(); + jobResultsProvider.scheduledEventsForJob(request.getJobId(), job.getGroups(), query, eventsListener); - }, - jobNotFound -> { - // is the request Id a group? - jobConfigProvider.groupExists(request.getJobId(), ActionListener.wrap( - groupExists -> { - if (groupExists) { - jobResultsProvider.scheduledEventsForJob( - null, - Collections.singletonList(request.getJobId()), - query, - eventsListener); - } else { - listener.onFailure(ExceptionsHelper.missingJobException(request.getJobId())); - } - }, - listener::onFailure - )); - } - )); - } else { - jobResultsProvider.scheduledEvents(query, eventsListener); - } - }, - listener::onFailure); + }, jobNotFound -> { + // is the request Id a group? + jobConfigProvider.groupExists(request.getJobId(), ActionListener.wrap(groupExists -> { + if (groupExists) { + jobResultsProvider.scheduledEventsForJob( + null, + Collections.singletonList(request.getJobId()), + query, + eventsListener + ); + } else { + listener.onFailure(ExceptionsHelper.missingJobException(request.getJobId())); + } + }, listener::onFailure)); + })); + } else { + jobResultsProvider.scheduledEvents(query, eventsListener); + } + }, listener::onFailure); checkCalendarExists(calendarId, calendarExistsListener); } @@ -101,9 +98,9 @@ private void checkCalendarExists(String[] calendarId, ActionListener li return; } - jobResultsProvider.calendars(CalendarQueryBuilder.builder().calendarIdTokens(calendarId), ActionListener.wrap( - c -> listener.onResponse(true), - listener::onFailure - )); + jobResultsProvider.calendars( + CalendarQueryBuilder.builder().calendarIdTokens(calendarId), + ActionListener.wrap(c -> listener.onResponse(true), listener::onFailure) + ); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetCalendarsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetCalendarsAction.java index 09f4ad536b56b..8c41697cda38e 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetCalendarsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetCalendarsAction.java @@ -13,19 +13,21 @@ import org.elasticsearch.common.inject.Inject; import org.elasticsearch.tasks.Task; import org.elasticsearch.transport.TransportService; -import org.elasticsearch.xpack.core.ml.action.GetCalendarsAction; import org.elasticsearch.xpack.core.action.util.PageParams; +import org.elasticsearch.xpack.core.ml.action.GetCalendarsAction; import org.elasticsearch.xpack.ml.job.persistence.CalendarQueryBuilder; import org.elasticsearch.xpack.ml.job.persistence.JobResultsProvider; - public class TransportGetCalendarsAction extends HandledTransportAction { private final JobResultsProvider jobResultsProvider; @Inject - public TransportGetCalendarsAction(TransportService transportService, ActionFilters actionFilters, - JobResultsProvider jobResultsProvider) { + public TransportGetCalendarsAction( + TransportService transportService, + ActionFilters actionFilters, + JobResultsProvider jobResultsProvider + ) { super(GetCalendarsAction.NAME, transportService, actionFilters, GetCalendarsAction.Request::new); this.jobResultsProvider = jobResultsProvider; } @@ -42,9 +44,9 @@ protected void doExecute(Task task, GetCalendarsAction.Request request, ActionLi private void getCalendars(String[] idTokens, PageParams pageParams, ActionListener listener) { CalendarQueryBuilder query = new CalendarQueryBuilder().pageParams(pageParams).calendarIdTokens(idTokens).sort(true); - jobResultsProvider.calendars(query, ActionListener.wrap( - calendars -> listener.onResponse(new GetCalendarsAction.Response(calendars)), - listener::onFailure - )); + jobResultsProvider.calendars( + query, + ActionListener.wrap(calendars -> listener.onResponse(new GetCalendarsAction.Response(calendars)), listener::onFailure) + ); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetCategoriesAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetCategoriesAction.java index f19a97372fd41..08a9dbd1accc7 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetCategoriesAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetCategoriesAction.java @@ -24,8 +24,13 @@ public class TransportGetCategoriesAction extends HandledTransportAction listener) { - jobManager.jobExists(request.getJobId(), ActionListener.wrap( - jobExists -> { - Integer from = request.getPageParams() != null ? request.getPageParams().getFrom() : null; - Integer size = request.getPageParams() != null ? request.getPageParams().getSize() : null; - jobResultsProvider.categoryDefinitions(request.getJobId(), request.getCategoryId(), request.getPartitionFieldValue(), - true, from, size, r -> listener.onResponse(new GetCategoriesAction.Response(r)), listener::onFailure, client); - }, - listener::onFailure - )); + jobManager.jobExists(request.getJobId(), ActionListener.wrap(jobExists -> { + Integer from = request.getPageParams() != null ? request.getPageParams().getFrom() : null; + Integer size = request.getPageParams() != null ? request.getPageParams().getSize() : null; + jobResultsProvider.categoryDefinitions( + request.getJobId(), + request.getCategoryId(), + request.getPartitionFieldValue(), + true, + from, + size, + r -> listener.onResponse(new GetCategoriesAction.Response(r)), + listener::onFailure, + client + ); + }, listener::onFailure)); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetDataFrameAnalyticsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetDataFrameAnalyticsAction.java index ae8b8c9f57fed..5d0889ec133f0 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetDataFrameAnalyticsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetDataFrameAnalyticsAction.java @@ -10,15 +10,15 @@ import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.support.ActionFilters; import org.elasticsearch.client.Client; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.inject.Inject; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.core.Nullable; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.tasks.Task; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.action.AbstractTransportGetResourcesAction; import org.elasticsearch.xpack.core.ml.MlConfigIndex; import org.elasticsearch.xpack.core.ml.action.GetDataFrameAnalyticsAction; @@ -27,14 +27,26 @@ import static org.elasticsearch.xpack.core.ClientHelper.ML_ORIGIN; -public class TransportGetDataFrameAnalyticsAction extends AbstractTransportGetResourcesAction { +public class TransportGetDataFrameAnalyticsAction extends AbstractTransportGetResourcesAction< + DataFrameAnalyticsConfig, + GetDataFrameAnalyticsAction.Request, + GetDataFrameAnalyticsAction.Response> { @Inject - public TransportGetDataFrameAnalyticsAction(TransportService transportService, ActionFilters actionFilters, Client client, - NamedXContentRegistry xContentRegistry) { - super(GetDataFrameAnalyticsAction.NAME, transportService, actionFilters, GetDataFrameAnalyticsAction.Request::new, client, - xContentRegistry); + public TransportGetDataFrameAnalyticsAction( + TransportService transportService, + ActionFilters actionFilters, + Client client, + NamedXContentRegistry xContentRegistry + ) { + super( + GetDataFrameAnalyticsAction.NAME, + transportService, + actionFilters, + GetDataFrameAnalyticsAction.Request::new, + client, + xContentRegistry + ); } @Override @@ -58,12 +70,15 @@ protected ResourceNotFoundException notFoundException(String resourceId) { } @Override - protected void doExecute(Task task, GetDataFrameAnalyticsAction.Request request, - ActionListener listener) { - searchResources(request, ActionListener.wrap( - queryPage -> listener.onResponse(new GetDataFrameAnalyticsAction.Response(queryPage)), - listener::onFailure - )); + protected void doExecute( + Task task, + GetDataFrameAnalyticsAction.Request request, + ActionListener listener + ) { + searchResources( + request, + ActionListener.wrap(queryPage -> listener.onResponse(new GetDataFrameAnalyticsAction.Response(queryPage)), listener::onFailure) + ); } @Nullable diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetDataFrameAnalyticsStatsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetDataFrameAnalyticsStatsAction.java index 403f73a7236e5..bdc60cc427c66 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetDataFrameAnalyticsStatsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetDataFrameAnalyticsStatsAction.java @@ -71,95 +71,120 @@ import static org.elasticsearch.xpack.core.ClientHelper.ML_ORIGIN; import static org.elasticsearch.xpack.core.ClientHelper.executeAsyncWithOrigin; -public class TransportGetDataFrameAnalyticsStatsAction - extends TransportTasksAction> { +public class TransportGetDataFrameAnalyticsStatsAction extends TransportTasksAction< + DataFrameAnalyticsTask, + GetDataFrameAnalyticsStatsAction.Request, + GetDataFrameAnalyticsStatsAction.Response, + QueryPage> { private static final Logger logger = LogManager.getLogger(TransportGetDataFrameAnalyticsStatsAction.class); private final Client client; @Inject - public TransportGetDataFrameAnalyticsStatsAction(TransportService transportService, ClusterService clusterService, Client client, - ActionFilters actionFilters) { - super(GetDataFrameAnalyticsStatsAction.NAME, clusterService, transportService, actionFilters, - GetDataFrameAnalyticsStatsAction.Request::new, GetDataFrameAnalyticsStatsAction.Response::new, - in -> new QueryPage<>(in, GetDataFrameAnalyticsStatsAction.Response.Stats::new), ThreadPool.Names.MANAGEMENT); + public TransportGetDataFrameAnalyticsStatsAction( + TransportService transportService, + ClusterService clusterService, + Client client, + ActionFilters actionFilters + ) { + super( + GetDataFrameAnalyticsStatsAction.NAME, + clusterService, + transportService, + actionFilters, + GetDataFrameAnalyticsStatsAction.Request::new, + GetDataFrameAnalyticsStatsAction.Response::new, + in -> new QueryPage<>(in, GetDataFrameAnalyticsStatsAction.Response.Stats::new), + ThreadPool.Names.MANAGEMENT + ); this.client = client; } @Override - protected GetDataFrameAnalyticsStatsAction.Response newResponse(GetDataFrameAnalyticsStatsAction.Request request, - List> tasks, - List taskFailures, - List nodeFailures) { + protected GetDataFrameAnalyticsStatsAction.Response newResponse( + GetDataFrameAnalyticsStatsAction.Request request, + List> tasks, + List taskFailures, + List nodeFailures + ) { List stats = new ArrayList<>(); for (QueryPage task : tasks) { stats.addAll(task.results()); } Collections.sort(stats, Comparator.comparing(Stats::getId)); - return new GetDataFrameAnalyticsStatsAction.Response(taskFailures, nodeFailures, new QueryPage<>(stats, stats.size(), - GetDataFrameAnalyticsAction.Response.RESULTS_FIELD)); + return new GetDataFrameAnalyticsStatsAction.Response( + taskFailures, + nodeFailures, + new QueryPage<>(stats, stats.size(), GetDataFrameAnalyticsAction.Response.RESULTS_FIELD) + ); } @Override - protected void taskOperation(GetDataFrameAnalyticsStatsAction.Request request, DataFrameAnalyticsTask task, - ActionListener> listener) { + protected void taskOperation( + GetDataFrameAnalyticsStatsAction.Request request, + DataFrameAnalyticsTask task, + ActionListener> listener + ) { logger.debug("Get stats for running task [{}]", task.getParams().getId()); - ActionListener updateProgressListener = ActionListener.wrap( - aVoid -> { - StatsHolder statsHolder = task.getStatsHolder(); - if (statsHolder == null) { - // The task has just been assigned and has not been initialized with its stats holder yet. - // We return empty result here so that we treat it as a stopped task and return its stored stats. - listener.onResponse(new QueryPage<>(Collections.emptyList(), 0, GetDataFrameAnalyticsAction.Response.RESULTS_FIELD)); - return; - } - Stats stats = buildStats( - task.getParams().getId(), - statsHolder.getProgressTracker().report(), - statsHolder.getDataCountsTracker().report(), - statsHolder.getMemoryUsage(), - statsHolder.getAnalysisStats() - ); - listener.onResponse(new QueryPage<>(Collections.singletonList(stats), 1, - GetDataFrameAnalyticsAction.Response.RESULTS_FIELD)); - }, listener::onFailure - ); + ActionListener updateProgressListener = ActionListener.wrap(aVoid -> { + StatsHolder statsHolder = task.getStatsHolder(); + if (statsHolder == null) { + // The task has just been assigned and has not been initialized with its stats holder yet. + // We return empty result here so that we treat it as a stopped task and return its stored stats. + listener.onResponse(new QueryPage<>(Collections.emptyList(), 0, GetDataFrameAnalyticsAction.Response.RESULTS_FIELD)); + return; + } + Stats stats = buildStats( + task.getParams().getId(), + statsHolder.getProgressTracker().report(), + statsHolder.getDataCountsTracker().report(), + statsHolder.getMemoryUsage(), + statsHolder.getAnalysisStats() + ); + listener.onResponse(new QueryPage<>(Collections.singletonList(stats), 1, GetDataFrameAnalyticsAction.Response.RESULTS_FIELD)); + }, listener::onFailure); // We must update the progress of the reindexing task as it might be stale task.updateTaskProgress(updateProgressListener); } @Override - protected void doExecute(Task task, GetDataFrameAnalyticsStatsAction.Request request, - ActionListener listener) { + protected void doExecute( + Task task, + GetDataFrameAnalyticsStatsAction.Request request, + ActionListener listener + ) { logger.debug("Get stats for data frame analytics [{}]", request.getId()); - ActionListener getResponseListener = ActionListener.wrap( - getResponse -> { - List expandedIds = getResponse.getResources().results().stream().map(DataFrameAnalyticsConfig::getId) - .collect(Collectors.toList()); - request.setExpandedIds(expandedIds); - ActionListener runningTasksStatsListener = ActionListener.wrap( - runningTasksStatsResponse -> gatherStatsForStoppedTasks(getResponse.getResources().results(), runningTasksStatsResponse, - ActionListener.wrap( - finalResponse -> { + ActionListener getResponseListener = ActionListener.wrap(getResponse -> { + List expandedIds = getResponse.getResources() + .results() + .stream() + .map(DataFrameAnalyticsConfig::getId) + .collect(Collectors.toList()); + request.setExpandedIds(expandedIds); + ActionListener runningTasksStatsListener = ActionListener.wrap( + runningTasksStatsResponse -> gatherStatsForStoppedTasks( + getResponse.getResources().results(), + runningTasksStatsResponse, + ActionListener.wrap(finalResponse -> { - // While finalResponse has all the stats objects we need, we should report the count - // from the get response - QueryPage finalStats = new QueryPage<>(finalResponse.getResponse().results(), - getResponse.getResources().count(), GetDataFrameAnalyticsAction.Response.RESULTS_FIELD); - listener.onResponse(new GetDataFrameAnalyticsStatsAction.Response(finalStats)); - }, - listener::onFailure)), - listener::onFailure - ); - super.doExecute(task, request, runningTasksStatsListener); - }, - listener::onFailure - ); + // While finalResponse has all the stats objects we need, we should report the count + // from the get response + QueryPage finalStats = new QueryPage<>( + finalResponse.getResponse().results(), + getResponse.getResources().count(), + GetDataFrameAnalyticsAction.Response.RESULTS_FIELD + ); + listener.onResponse(new GetDataFrameAnalyticsStatsAction.Response(finalStats)); + }, listener::onFailure) + ), + listener::onFailure + ); + super.doExecute(task, request, runningTasksStatsListener); + }, listener::onFailure); GetDataFrameAnalyticsAction.Request getRequest = new GetDataFrameAnalyticsAction.Request(); getRequest.setResourceId(request.getId()); @@ -168,8 +193,11 @@ protected void doExecute(Task task, GetDataFrameAnalyticsStatsAction.Request req executeAsyncWithOrigin(client, ML_ORIGIN, GetDataFrameAnalyticsAction.INSTANCE, getRequest, getResponseListener); } - void gatherStatsForStoppedTasks(List configs, GetDataFrameAnalyticsStatsAction.Response runningTasksResponse, - ActionListener listener) { + void gatherStatsForStoppedTasks( + List configs, + GetDataFrameAnalyticsStatsAction.Response runningTasksResponse, + ActionListener listener + ) { List stoppedConfigs = determineStoppedConfigs(configs, runningTasksResponse.getResponse().results()); if (stoppedConfigs.isEmpty()) { listener.onResponse(runningTasksResponse); @@ -182,29 +210,29 @@ void gatherStatsForStoppedTasks(List configs, GetDataF for (int i = 0; i < stoppedConfigs.size(); i++) { final int slot = i; DataFrameAnalyticsConfig config = stoppedConfigs.get(i); - searchStats(config, ActionListener.wrap( - stats -> { - jobStats.set(slot, stats); - if (counter.decrementAndGet() == 0) { - if (searchException.get() != null) { - listener.onFailure(searchException.get()); - return; - } - List allTasksStats = new ArrayList<>(runningTasksResponse.getResponse().results()); - allTasksStats.addAll(jobStats.asList()); - Collections.sort(allTasksStats, Comparator.comparing(Stats::getId)); - listener.onResponse(new GetDataFrameAnalyticsStatsAction.Response(new QueryPage<>( - allTasksStats, allTasksStats.size(), GetDataFrameAnalyticsAction.Response.RESULTS_FIELD))); - } - }, - e -> { - // take the first error - searchException.compareAndSet(null, e); - if (counter.decrementAndGet() == 0) { - listener.onFailure(e); + searchStats(config, ActionListener.wrap(stats -> { + jobStats.set(slot, stats); + if (counter.decrementAndGet() == 0) { + if (searchException.get() != null) { + listener.onFailure(searchException.get()); + return; } - }) - ); + List allTasksStats = new ArrayList<>(runningTasksResponse.getResponse().results()); + allTasksStats.addAll(jobStats.asList()); + Collections.sort(allTasksStats, Comparator.comparing(Stats::getId)); + listener.onResponse( + new GetDataFrameAnalyticsStatsAction.Response( + new QueryPage<>(allTasksStats, allTasksStats.size(), GetDataFrameAnalyticsAction.Response.RESULTS_FIELD) + ) + ); + } + }, e -> { + // take the first error + searchException.compareAndSet(null, e); + if (counter.decrementAndGet() == 0) { + listener.onFailure(e); + } + })); } } @@ -217,7 +245,8 @@ private void searchStats(DataFrameAnalyticsConfig config, ActionListener logger.debug("[{}] Gathering stats for stopped task", config.getId()); RetrievedStatsHolder retrievedStatsHolder = new RetrievedStatsHolder( - ProgressTracker.fromZeroes(config.getAnalysis().getProgressPhases(), config.getAnalysis().supportsInference()).report()); + ProgressTracker.fromZeroes(config.getAnalysis().getProgressPhases(), config.getAnalysis().supportsInference()).report() + ); MultiSearchRequest multiSearchRequest = new MultiSearchRequest(); multiSearchRequest.add(buildStoredProgressSearch(config.getId())); @@ -227,8 +256,12 @@ private void searchStats(DataFrameAnalyticsConfig config, ActionListener multiSearchRequest.add(buildStatsDocSearch(config.getId(), ClassificationStats.TYPE_VALUE)); multiSearchRequest.add(buildStatsDocSearch(config.getId(), RegressionStats.TYPE_VALUE)); - executeAsyncWithOrigin(client, ML_ORIGIN, MultiSearchAction.INSTANCE, multiSearchRequest, ActionListener.wrap( - multiSearchResponse -> { + executeAsyncWithOrigin( + client, + ML_ORIGIN, + MultiSearchAction.INSTANCE, + multiSearchRequest, + ActionListener.wrap(multiSearchResponse -> { MultiSearchResponse.Item[] itemResponses = multiSearchResponse.getResponses(); for (int i = 0; i < itemResponses.length; ++i) { MultiSearchResponse.Item itemResponse = itemResponses[i]; @@ -237,8 +270,13 @@ private void searchStats(DataFrameAnalyticsConfig config, ActionListener logger.error( new ParameterizedMessage( "[{}] Item failure encountered during multi search for request [indices={}, source={}]: {}", - config.getId(), itemRequest.indices(), itemRequest.source(), itemResponse.getFailureMessage()), - itemResponse.getFailure()); + config.getId(), + itemRequest.indices(), + itemRequest.source(), + itemResponse.getFailureMessage() + ), + itemResponse.getFailure() + ); listener.onFailure(ExceptionsHelper.serverError(itemResponse.getFailureMessage(), itemResponse.getFailure())); return; } else { @@ -252,15 +290,17 @@ private void searchStats(DataFrameAnalyticsConfig config, ActionListener } } } - listener.onResponse(buildStats(config.getId(), - retrievedStatsHolder.progress.get(), - retrievedStatsHolder.dataCounts, - retrievedStatsHolder.memoryUsage, - retrievedStatsHolder.analysisStats - )); - }, - e -> listener.onFailure(ExceptionsHelper.serverError("Error searching for stats", e)) - )); + listener.onResponse( + buildStats( + config.getId(), + retrievedStatsHolder.progress.get(), + retrievedStatsHolder.dataCounts, + retrievedStatsHolder.memoryUsage, + retrievedStatsHolder.analysisStats + ) + ); + }, e -> listener.onFailure(ExceptionsHelper.serverError("Error searching for stats", e))) + ); } private static SearchRequest buildStoredProgressSearch(String configId) { @@ -279,9 +319,13 @@ private static SearchRequest buildStatsDocSearch(String configId, String statsTy .filter(QueryBuilders.termQuery(Fields.JOB_ID.getPreferredName(), configId)) .filter(QueryBuilders.termQuery(Fields.TYPE.getPreferredName(), statsType)); searchRequest.source().query(query); - searchRequest.source().sort(SortBuilders.fieldSort(Fields.TIMESTAMP.getPreferredName()).order(SortOrder.DESC) - // We need this for the search not to fail when there are no mappings yet in the index - .unmappedType("long")); + searchRequest.source() + .sort( + SortBuilders.fieldSort(Fields.TIMESTAMP.getPreferredName()) + .order(SortOrder.DESC) + // We need this for the search not to fail when there are no mappings yet in the index + .unmappedType("long") + ); return searchRequest; } @@ -304,11 +348,13 @@ private static void parseHit(SearchHit hit, String configId, RetrievedStatsHolde } } - private GetDataFrameAnalyticsStatsAction.Response.Stats buildStats(String concreteAnalyticsId, - List progress, - DataCounts dataCounts, - MemoryUsage memoryUsage, - AnalysisStats analysisStats) { + private GetDataFrameAnalyticsStatsAction.Response.Stats buildStats( + String concreteAnalyticsId, + List progress, + DataCounts dataCounts, + MemoryUsage memoryUsage, + AnalysisStats analysisStats + ) { ClusterState clusterState = clusterService.state(); PersistentTasksCustomMetadata tasks = clusterState.getMetadata().custom(PersistentTasksCustomMetadata.TYPE); PersistentTasksCustomMetadata.PersistentTask analyticsTask = MlTasks.getDataFrameAnalyticsTask(concreteAnalyticsId, tasks); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetDatafeedRunningStateAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetDatafeedRunningStateAction.java index 575efd9527aae..6a9a46f9f8cec 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetDatafeedRunningStateAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetDatafeedRunningStateAction.java @@ -60,10 +60,12 @@ public TransportGetDatafeedRunningStateAction( } @Override - protected Response newResponse(Request request, - List tasks, - List taskOperationFailures, - List failedNodeExceptions) { + protected Response newResponse( + Request request, + List tasks, + List taskOperationFailures, + List failedNodeExceptions + ) { org.elasticsearch.ExceptionsHelper.rethrowAndSuppress( taskOperationFailures.stream() .map(t -> org.elasticsearch.ExceptionsHelper.convertToElastic(t.getCause())) @@ -101,38 +103,41 @@ protected void doExecute(Task task, Request request, ActionListener li } // Do this to catch datafeed tasks that have been created but are currently not assigned to a node. - ActionListener taskResponseListener = ActionListener.wrap( - actionResponses -> { - Map runningStateMap = actionResponses.getDatafeedRunningState(); - if (runningStateMap.size() == datafeedTasks.size()) { - listener.onResponse(actionResponses); - return; - } - List missingResponses = new ArrayList<>(); - missingResponses.add(actionResponses); - missingResponses.add(new Response(datafeedTasks.stream() - .map(t -> (StartDatafeedAction.DatafeedParams)t.getParams()) - .filter(datafeedParams -> runningStateMap.containsKey(datafeedParams.getDatafeedId()) == false) - .collect(Collectors.toMap( - StartDatafeedAction.DatafeedParams::getDatafeedId, - // If it isn't assigned to a node, assume that look back hasn't completed yet - params -> new Response.RunningState(params.getEndTime() == null, false) - )))); - listener.onResponse(Response.fromResponses(missingResponses)); - }, - listener::onFailure - ); + ActionListener taskResponseListener = ActionListener.wrap(actionResponses -> { + Map runningStateMap = actionResponses.getDatafeedRunningState(); + if (runningStateMap.size() == datafeedTasks.size()) { + listener.onResponse(actionResponses); + return; + } + List missingResponses = new ArrayList<>(); + missingResponses.add(actionResponses); + missingResponses.add( + new Response( + datafeedTasks.stream() + .map(t -> (StartDatafeedAction.DatafeedParams) t.getParams()) + .filter(datafeedParams -> runningStateMap.containsKey(datafeedParams.getDatafeedId()) == false) + .collect( + Collectors.toMap( + StartDatafeedAction.DatafeedParams::getDatafeedId, + // If it isn't assigned to a node, assume that look back hasn't completed yet + params -> new Response.RunningState(params.getEndTime() == null, false) + ) + ) + ) + ); + listener.onResponse(Response.fromResponses(missingResponses)); + }, listener::onFailure); - String[] nodesOfConcern = datafeedTasks.stream().map(PersistentTasksCustomMetadata.PersistentTask::getExecutorNode) + String[] nodesOfConcern = datafeedTasks.stream() + .map(PersistentTasksCustomMetadata.PersistentTask::getExecutorNode) .filter(Objects::nonNull) .filter(nodes::nodeExists) .toArray(String[]::new); if (nodesOfConcern.length == 0) { - logger.debug(() -> new ParameterizedMessage( - "Unable to find executor nodes for datafeed tasks {}", - request.getDatafeedTaskIds() - )); + logger.debug( + () -> new ParameterizedMessage("Unable to find executor nodes for datafeed tasks {}", request.getDatafeedTaskIds()) + ); taskResponseListener.onResponse(new Response(Collections.emptyMap())); return; diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetDatafeedsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetDatafeedsAction.java index 51480630c961e..bf2b2220bb2b6 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetDatafeedsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetDatafeedsAction.java @@ -30,26 +30,43 @@ public class TransportGetDatafeedsAction extends TransportMasterNodeReadAction listener) { + protected void masterOperation( + Task task, + GetDatafeedsAction.Request request, + ClusterState state, + ActionListener listener + ) { logger.debug("Get datafeed '{}'", request.getDatafeedId()); - datafeedManager.getDatafeeds(request, state, ActionListener.wrap( - datafeeds -> listener.onResponse(new GetDatafeedsAction.Response(datafeeds)), - listener::onFailure - )); + datafeedManager.getDatafeeds( + request, + state, + ActionListener.wrap(datafeeds -> listener.onResponse(new GetDatafeedsAction.Response(datafeeds)), listener::onFailure) + ); } @Override diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetDatafeedsStatsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetDatafeedsStatsAction.java index e6b15091e3bda..0811b875d72ab 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetDatafeedsStatsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetDatafeedsStatsAction.java @@ -26,8 +26,8 @@ import org.elasticsearch.transport.TransportService; import org.elasticsearch.xpack.core.ml.action.GetDatafeedRunningStateAction; import org.elasticsearch.xpack.core.ml.action.GetDatafeedsStatsAction; -import org.elasticsearch.xpack.core.ml.action.GetDatafeedsStatsAction.Response; import org.elasticsearch.xpack.core.ml.action.GetDatafeedsStatsAction.Request; +import org.elasticsearch.xpack.core.ml.action.GetDatafeedsStatsAction.Response; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfig; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedTimingStats; import org.elasticsearch.xpack.ml.datafeed.persistence.DatafeedConfigProvider; @@ -50,11 +50,16 @@ public class TransportGetDatafeedsStatsAction extends TransportMasterNodeReadAct private final OriginSettingClient client; @Inject - public TransportGetDatafeedsStatsAction(TransportService transportService, ClusterService clusterService, - ThreadPool threadPool, ActionFilters actionFilters, - IndexNameExpressionResolver indexNameExpressionResolver, - DatafeedConfigProvider datafeedConfigProvider, JobResultsProvider jobResultsProvider, - Client client) { + public TransportGetDatafeedsStatsAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver, + DatafeedConfigProvider datafeedConfigProvider, + JobResultsProvider jobResultsProvider, + Client client + ) { super( GetDatafeedsStatsAction.NAME, transportService, @@ -78,59 +83,43 @@ protected void masterOperation(Task task, Request request, ClusterState state, A final Response.Builder responseBuilder = new Response.Builder(); // 5. Build response - ActionListener runtimeStateListener = ActionListener.wrap( - runtimeStateResponse -> { - responseBuilder.setDatafeedRuntimeState(runtimeStateResponse); - listener.onResponse(responseBuilder.build(tasksInProgress, state)); - }, - listener::onFailure - ); + ActionListener runtimeStateListener = ActionListener.wrap(runtimeStateResponse -> { + responseBuilder.setDatafeedRuntimeState(runtimeStateResponse); + listener.onResponse(responseBuilder.build(tasksInProgress, state)); + }, listener::onFailure); // 4. Grab runtime state - ActionListener> datafeedTimingStatsListener = ActionListener.wrap( - timingStatsByJobId -> { - responseBuilder.setTimingStatsMap(timingStatsByJobId); - client.execute( - GetDatafeedRunningStateAction.INSTANCE, - new GetDatafeedRunningStateAction.Request(responseBuilder.getDatafeedIds()), - runtimeStateListener - ); - }, - listener::onFailure - ); + ActionListener> datafeedTimingStatsListener = ActionListener.wrap(timingStatsByJobId -> { + responseBuilder.setTimingStatsMap(timingStatsByJobId); + client.execute( + GetDatafeedRunningStateAction.INSTANCE, + new GetDatafeedRunningStateAction.Request(responseBuilder.getDatafeedIds()), + runtimeStateListener + ); + }, listener::onFailure); // 3. Grab timing stats - ActionListener> expandedConfigsListener = ActionListener.wrap( - datafeedBuilders -> { - Map datafeedIdsToJobIds = datafeedBuilders.stream() - .collect(Collectors.toMap(DatafeedConfig.Builder::getId, DatafeedConfig.Builder::getJobId)); - responseBuilder.setDatafeedToJobId(datafeedIdsToJobIds); - jobResultsProvider.datafeedTimingStats(new ArrayList<>(datafeedIdsToJobIds.values()), datafeedTimingStatsListener); - }, - listener::onFailure - ); + ActionListener> expandedConfigsListener = ActionListener.wrap(datafeedBuilders -> { + Map datafeedIdsToJobIds = datafeedBuilders.stream() + .collect(Collectors.toMap(DatafeedConfig.Builder::getId, DatafeedConfig.Builder::getJobId)); + responseBuilder.setDatafeedToJobId(datafeedIdsToJobIds); + jobResultsProvider.datafeedTimingStats(new ArrayList<>(datafeedIdsToJobIds.values()), datafeedTimingStatsListener); + }, listener::onFailure); // 2. Now that we have the ids, grab the datafeed configs - ActionListener> expandIdsListener = ActionListener.wrap( - expandedIds -> { - responseBuilder.setDatafeedIds(expandedIds); - datafeedConfigProvider.expandDatafeedConfigs( - request.getDatafeedId(), - // Already took into account the request parameter when we expanded the IDs with the tasks earlier - // Should allow for no datafeeds in case the config is gone - true, - expandedConfigsListener - ); - }, - listener::onFailure - ); + ActionListener> expandIdsListener = ActionListener.wrap(expandedIds -> { + responseBuilder.setDatafeedIds(expandedIds); + datafeedConfigProvider.expandDatafeedConfigs( + request.getDatafeedId(), + // Already took into account the request parameter when we expanded the IDs with the tasks earlier + // Should allow for no datafeeds in case the config is gone + true, + expandedConfigsListener + ); + }, listener::onFailure); // 1. This might also include datafeed tasks that exist but no longer have a config - datafeedConfigProvider.expandDatafeedIds(request.getDatafeedId(), - request.allowNoMatch(), - tasksInProgress, - true, - expandIdsListener); + datafeedConfigProvider.expandDatafeedIds(request.getDatafeedId(), request.allowNoMatch(), tasksInProgress, true, expandIdsListener); } @Override diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetDeploymentStatsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetDeploymentStatsAction.java index 65efa734da147..6df6c0bd75f2e 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetDeploymentStatsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetDeploymentStatsAction.java @@ -46,47 +46,63 @@ import java.util.function.Function; import java.util.stream.Collectors; -public class TransportGetDeploymentStatsAction extends TransportTasksAction { +public class TransportGetDeploymentStatsAction extends TransportTasksAction< + TrainedModelDeploymentTask, + GetDeploymentStatsAction.Request, + GetDeploymentStatsAction.Response, + GetDeploymentStatsAction.Response.AllocationStats> { @Inject - public TransportGetDeploymentStatsAction(TransportService transportService, - ActionFilters actionFilters, - ClusterService clusterService) { - super(GetDeploymentStatsAction.NAME, clusterService, transportService, actionFilters, GetDeploymentStatsAction.Request::new, - GetDeploymentStatsAction.Response::new, GetDeploymentStatsAction.Response.AllocationStats::new, ThreadPool.Names.MANAGEMENT); + public TransportGetDeploymentStatsAction( + TransportService transportService, + ActionFilters actionFilters, + ClusterService clusterService + ) { + super( + GetDeploymentStatsAction.NAME, + clusterService, + transportService, + actionFilters, + GetDeploymentStatsAction.Request::new, + GetDeploymentStatsAction.Response::new, + GetDeploymentStatsAction.Response.AllocationStats::new, + ThreadPool.Names.MANAGEMENT + ); } @Override - protected GetDeploymentStatsAction.Response newResponse(GetDeploymentStatsAction.Request request, - List taskResponse, - List taskOperationFailures, - List failedNodeExceptions) { + protected GetDeploymentStatsAction.Response newResponse( + GetDeploymentStatsAction.Request request, + List taskResponse, + List taskOperationFailures, + List failedNodeExceptions + ) { // group the stats by model and merge individual node stats - var mergedNodeStatsByModel = - taskResponse.stream().collect(Collectors.toMap(GetDeploymentStatsAction.Response.AllocationStats::getModelId, - Function.identity(), - (l, r) -> { - l.getNodeStats().addAll(r.getNodeStats()); - return l; - }, - TreeMap::new)); + var mergedNodeStatsByModel = taskResponse.stream() + .collect(Collectors.toMap(GetDeploymentStatsAction.Response.AllocationStats::getModelId, Function.identity(), (l, r) -> { + l.getNodeStats().addAll(r.getNodeStats()); + return l; + }, TreeMap::new)); List bunchedAndSorted = new ArrayList<>(mergedNodeStatsByModel.values()); - return new GetDeploymentStatsAction.Response(taskOperationFailures, + return new GetDeploymentStatsAction.Response( + taskOperationFailures, failedNodeExceptions, bunchedAndSorted, - bunchedAndSorted.size()); + bunchedAndSorted.size() + ); } @Override - protected void doExecute(Task task, GetDeploymentStatsAction.Request request, - ActionListener listener) { + protected void doExecute( + Task task, + GetDeploymentStatsAction.Request request, + ActionListener listener + ) { String[] tokenizedRequestIds = Strings.tokenizeToStringArray(request.getDeploymentId(), ","); - ExpandedIdsMatcher.SimpleIdsMatcher idsMatcher = - new ExpandedIdsMatcher.SimpleIdsMatcher(tokenizedRequestIds); + ExpandedIdsMatcher.SimpleIdsMatcher idsMatcher = new ExpandedIdsMatcher.SimpleIdsMatcher(tokenizedRequestIds); TrainedModelAllocationMetadata allocation = TrainedModelAllocationMetadata.fromState(clusterService.state()); List matchedDeploymentIds = new ArrayList<>(); @@ -99,7 +115,9 @@ protected void doExecute(Task task, GetDeploymentStatsAction.Request request, taskNodes.addAll(Arrays.asList(allocationEntry.getValue().getStartedNodes())); - Map routings = allocationEntry.getValue().getNodeRoutingTable().entrySet() + Map routings = allocationEntry.getValue() + .getNodeRoutingTable() + .entrySet() .stream() .filter(routingEntry -> RoutingState.STARTED.equals(routingEntry.getValue().getState()) == false) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); @@ -116,77 +134,86 @@ protected void doExecute(Task task, GetDeploymentStatsAction.Request request, return; } if (matchedDeploymentIds.isEmpty()) { - listener.onResponse(new GetDeploymentStatsAction.Response( - Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), 0L)); + listener.onResponse( + new GetDeploymentStatsAction.Response(Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), 0L) + ); return; } request.setNodes(taskNodes.toArray(String[]::new)); request.setExpandedIds(matchedDeploymentIds); - ActionListener addFailedListener = listener.delegateFailure( - (l, response) -> { - var updatedResponse= GetDeploymentStatsAction.Response.addFailedRoutes(response, - nonStartedAllocationsForModel, - clusterService.state().nodes() - ); - ClusterState latestState = clusterService.state(); - Set nodesShuttingDown = TransportStartTrainedModelDeploymentAction.nodesShuttingDown(latestState); - List nodes = latestState.getNodes() - .getAllNodes() - .stream() - .filter(d -> nodesShuttingDown.contains(d.getId()) == false) - .filter(StartTrainedModelDeploymentAction.TaskParams::mayAllocateToNode) - .collect(Collectors.toList()); - // Set the allocation state and reason if we have it - for (GetDeploymentStatsAction.Response.AllocationStats stats : updatedResponse.getStats().results()) { - Optional modelAllocation = Optional.ofNullable( - allocation.getModelAllocation(stats.getModelId()) - ); - TrainedModelAllocation trainedModelAllocation = modelAllocation.orElse(null); - if (trainedModelAllocation != null) { - stats.setState(trainedModelAllocation.getAllocationState()) - .setReason(trainedModelAllocation.getReason().orElse(null)); - if (trainedModelAllocation.getAllocationState().isAnyOf(AllocationState.STARTED, AllocationState.STARTING)) { - stats.setAllocationStatus(trainedModelAllocation.calculateAllocationStatus(nodes).orElse(null)); - } + ActionListener addFailedListener = listener.delegateFailure((l, response) -> { + var updatedResponse = GetDeploymentStatsAction.Response.addFailedRoutes( + response, + nonStartedAllocationsForModel, + clusterService.state().nodes() + ); + ClusterState latestState = clusterService.state(); + Set nodesShuttingDown = TransportStartTrainedModelDeploymentAction.nodesShuttingDown(latestState); + List nodes = latestState.getNodes() + .getAllNodes() + .stream() + .filter(d -> nodesShuttingDown.contains(d.getId()) == false) + .filter(StartTrainedModelDeploymentAction.TaskParams::mayAllocateToNode) + .collect(Collectors.toList()); + // Set the allocation state and reason if we have it + for (GetDeploymentStatsAction.Response.AllocationStats stats : updatedResponse.getStats().results()) { + Optional modelAllocation = Optional.ofNullable(allocation.getModelAllocation(stats.getModelId())); + TrainedModelAllocation trainedModelAllocation = modelAllocation.orElse(null); + if (trainedModelAllocation != null) { + stats.setState(trainedModelAllocation.getAllocationState()).setReason(trainedModelAllocation.getReason().orElse(null)); + if (trainedModelAllocation.getAllocationState().isAnyOf(AllocationState.STARTED, AllocationState.STARTING)) { + stats.setAllocationStatus(trainedModelAllocation.calculateAllocationStatus(nodes).orElse(null)); } } - l.onResponse(updatedResponse); } - ); + l.onResponse(updatedResponse); + }); super.doExecute(task, request, addFailedListener); } @Override - protected void taskOperation(GetDeploymentStatsAction.Request request, TrainedModelDeploymentTask task, - ActionListener listener) { + protected void taskOperation( + GetDeploymentStatsAction.Request request, + TrainedModelDeploymentTask task, + ActionListener listener + ) { Optional stats = task.modelStats(); List nodeStats = new ArrayList<>(); if (stats.isPresent()) { - nodeStats.add(GetDeploymentStatsAction.Response.AllocationStats.NodeStats.forStartedState( - clusterService.localNode(), - stats.get().getTimingStats().getCount(), - stats.get().getTimingStats().getAverage(), - stats.get().getLastUsed())); + nodeStats.add( + GetDeploymentStatsAction.Response.AllocationStats.NodeStats.forStartedState( + clusterService.localNode(), + stats.get().getTimingStats().getCount(), + stats.get().getTimingStats().getAverage(), + stats.get().getLastUsed() + ) + ); } else { // if there are no stats the process is missing. // Either because it is starting or stopped - nodeStats.add(GetDeploymentStatsAction.Response.AllocationStats.NodeStats.forNotStartedState( - clusterService.localNode(), - RoutingState.STOPPED, "")); + nodeStats.add( + GetDeploymentStatsAction.Response.AllocationStats.NodeStats.forNotStartedState( + clusterService.localNode(), + RoutingState.STOPPED, + "" + ) + ); } - listener.onResponse(new GetDeploymentStatsAction.Response.AllocationStats( - task.getModelId(), - ByteSizeValue.ofBytes(task.getParams().getModelBytes()), - task.getParams().getInferenceThreads(), - task.getParams().getModelThreads(), - task.getParams().getQueueCapacity(), - nodeStats) + listener.onResponse( + new GetDeploymentStatsAction.Response.AllocationStats( + task.getModelId(), + ByteSizeValue.ofBytes(task.getParams().getModelBytes()), + task.getParams().getInferenceThreads(), + task.getParams().getModelThreads(), + task.getParams().getQueueCapacity(), + nodeStats + ) ); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetFiltersAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetFiltersAction.java index 2e8cba210ff4e..a51443d5911e4 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetFiltersAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetFiltersAction.java @@ -10,14 +10,14 @@ import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.support.ActionFilters; import org.elasticsearch.client.Client; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.inject.Inject; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.tasks.Task; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.action.AbstractTransportGetResourcesAction; import org.elasticsearch.xpack.core.ml.MlMetaIndex; import org.elasticsearch.xpack.core.ml.action.GetFiltersAction; @@ -27,25 +27,28 @@ import static org.elasticsearch.xpack.core.ClientHelper.ML_ORIGIN; -public class TransportGetFiltersAction extends AbstractTransportGetResourcesAction { +public class TransportGetFiltersAction extends AbstractTransportGetResourcesAction< + MlFilter, + GetFiltersAction.Request, + GetFiltersAction.Response> { @Inject - public TransportGetFiltersAction(TransportService transportService, - ActionFilters actionFilters, - Client client, - NamedXContentRegistry xContentRegistry) { + public TransportGetFiltersAction( + TransportService transportService, + ActionFilters actionFilters, + Client client, + NamedXContentRegistry xContentRegistry + ) { super(GetFiltersAction.NAME, transportService, actionFilters, GetFiltersAction.Request::new, client, xContentRegistry); } @Override protected void doExecute(Task task, GetFiltersAction.Request request, ActionListener listener) { request.setAllowNoResources(true); - searchResources(request, ActionListener.wrap( - filters -> listener.onResponse(new GetFiltersAction.Response(filters)), - listener::onFailure - )); + searchResources( + request, + ActionListener.wrap(filters -> listener.onResponse(new GetFiltersAction.Response(filters)), listener::onFailure) + ); } @Override @@ -55,7 +58,7 @@ protected ParseField getResultsField() { @Override protected String[] getIndices() { - return new String[]{MlMetaIndex.indexName()}; + return new String[] { MlMetaIndex.indexName() }; } @Override @@ -65,7 +68,7 @@ protected MlFilter parse(XContentParser parser) throws IOException { @Override protected ResourceNotFoundException notFoundException(String resourceId) { - return new ResourceNotFoundException("Unable to find filter [" + resourceId +"]"); + return new ResourceNotFoundException("Unable to find filter [" + resourceId + "]"); } @Override diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetInfluencersAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetInfluencersAction.java index c4cbc0eb0753f..633902037f7e8 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetInfluencersAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetInfluencersAction.java @@ -25,8 +25,13 @@ public class TransportGetInfluencersAction extends HandledTransportAction listener) { - jobManager.jobExists(request.getJobId(), ActionListener.wrap( - jobExists -> { - InfluencersQueryBuilder.InfluencersQuery query = new InfluencersQueryBuilder() - .includeInterim(request.isExcludeInterim() == false) - .start(request.getStart()) - .end(request.getEnd()) - .from(request.getPageParams().getFrom()) - .size(request.getPageParams().getSize()) - .influencerScoreThreshold(request.getInfluencerScore()) - .sortField(request.getSort()) - .sortDescending(request.isDescending()).build(); - jobResultsProvider.influencers(request.getJobId(), query, - page -> listener.onResponse(new GetInfluencersAction.Response(page)), listener::onFailure, client); - }, - listener::onFailure) - ); + jobManager.jobExists(request.getJobId(), ActionListener.wrap(jobExists -> { + InfluencersQueryBuilder.InfluencersQuery query = new InfluencersQueryBuilder().includeInterim( + request.isExcludeInterim() == false + ) + .start(request.getStart()) + .end(request.getEnd()) + .from(request.getPageParams().getFrom()) + .size(request.getPageParams().getSize()) + .influencerScoreThreshold(request.getInfluencerScore()) + .sortField(request.getSort()) + .sortDescending(request.isDescending()) + .build(); + jobResultsProvider.influencers( + request.getJobId(), + query, + page -> listener.onResponse(new GetInfluencersAction.Response(page)), + listener::onFailure, + client + ); + }, listener::onFailure)); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetJobsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetJobsAction.java index 807bbe99fa738..aa9204843da18 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetJobsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetJobsAction.java @@ -37,40 +37,56 @@ public class TransportGetJobsAction extends TransportMasterNodeReadAction listener) { + protected void masterOperation( + Task task, + GetJobsAction.Request request, + ClusterState state, + ActionListener listener + ) { logger.debug("Get job '{}'", request.getJobId()); - jobManager.expandJobBuilders(request.getJobId(), request.allowNoMatch(), ActionListener.wrap( + jobManager.expandJobBuilders( + request.getJobId(), + request.allowNoMatch(), + ActionListener.wrap( jobs -> datafeedManager.getDatafeedsByJobIds( - jobs.stream().map(Job.Builder::getId).collect(Collectors.toSet()), - state, - ActionListener.wrap( - dfsByJobId -> - listener.onResponse(new GetJobsAction.Response( - new QueryPage<>( - jobs.stream().map(jb -> { - Optional.ofNullable(dfsByJobId.get(jb.getId())).ifPresent(jb::setDatafeed); - return jb.build(); - }).collect(Collectors.toList()), - jobs.size(), - Job.RESULTS_FIELD - ) - )), - listener::onFailure - )), + jobs.stream().map(Job.Builder::getId).collect(Collectors.toSet()), + state, + ActionListener.wrap( + dfsByJobId -> listener.onResponse(new GetJobsAction.Response(new QueryPage<>(jobs.stream().map(jb -> { + Optional.ofNullable(dfsByJobId.get(jb.getId())).ifPresent(jb::setDatafeed); + return jb.build(); + }).collect(Collectors.toList()), jobs.size(), Job.RESULTS_FIELD))), + listener::onFailure + ) + ), listener::onFailure - )); + ) + ); } @Override diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetJobsStatsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetJobsStatsAction.java index da409ba375d6f..149b923faaf48 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetJobsStatsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetJobsStatsAction.java @@ -17,18 +17,18 @@ import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.TriConsumer; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.inject.Inject; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.AtomicArray; +import org.elasticsearch.core.TimeValue; +import org.elasticsearch.core.Tuple; import org.elasticsearch.persistent.PersistentTasksCustomMetadata; import org.elasticsearch.tasks.Task; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xpack.core.action.util.QueryPage; import org.elasticsearch.xpack.core.ml.MlTasks; import org.elasticsearch.xpack.core.ml.action.GetJobsStatsAction; import org.elasticsearch.xpack.core.ml.action.GetJobsStatsAction.Response.JobStats; -import org.elasticsearch.xpack.core.action.util.QueryPage; import org.elasticsearch.xpack.core.ml.job.config.Job; import org.elasticsearch.xpack.core.ml.job.config.JobState; import org.elasticsearch.xpack.core.ml.job.process.autodetect.state.DataCounts; @@ -52,8 +52,11 @@ import java.util.function.Consumer; import java.util.stream.Collectors; -public class TransportGetJobsStatsAction extends TransportTasksAction> { +public class TransportGetJobsStatsAction extends TransportTasksAction< + JobTask, + GetJobsStatsAction.Request, + GetJobsStatsAction.Response, + QueryPage> { private static final Logger logger = LogManager.getLogger(TransportGetJobsStatsAction.class); @@ -63,11 +66,24 @@ public class TransportGetJobsStatsAction extends TransportTasksAction new QueryPage<>(in, JobStats::new), ThreadPool.Names.MANAGEMENT); + public TransportGetJobsStatsAction( + TransportService transportService, + ActionFilters actionFilters, + ClusterService clusterService, + AutodetectProcessManager processManager, + JobResultsProvider jobResultsProvider, + JobConfigProvider jobConfigProvider + ) { + super( + GetJobsStatsAction.NAME, + clusterService, + transportService, + actionFilters, + GetJobsStatsAction.Request::new, + GetJobsStatsAction.Response::new, + in -> new QueryPage<>(in, JobStats::new), + ThreadPool.Names.MANAGEMENT + ); this.clusterService = clusterService; this.processManager = processManager; this.jobResultsProvider = jobResultsProvider; @@ -81,31 +97,33 @@ protected void doExecute(Task task, GetJobsStatsAction.Request request, ActionLi ClusterState state = clusterService.state(); PersistentTasksCustomMetadata tasks = state.getMetadata().custom(PersistentTasksCustomMetadata.TYPE); // If there are deleted configs, but the task is still around, we probably want to return the tasks in the stats call - jobConfigProvider.expandJobsIds(request.getJobId(), request.allowNoMatch(), true, tasks, true, ActionListener.wrap( - expandedIds -> { - request.setExpandedJobsIds(new ArrayList<>(expandedIds)); - ActionListener jobStatsListener = ActionListener.wrap( - response -> gatherStatsForClosedJobs(request, response, finalListener), - finalListener::onFailure - ); - super.doExecute(task, request, jobStatsListener); - }, + jobConfigProvider.expandJobsIds(request.getJobId(), request.allowNoMatch(), true, tasks, true, ActionListener.wrap(expandedIds -> { + request.setExpandedJobsIds(new ArrayList<>(expandedIds)); + ActionListener jobStatsListener = ActionListener.wrap( + response -> gatherStatsForClosedJobs(request, response, finalListener), finalListener::onFailure - )); + ); + super.doExecute(task, request, jobStatsListener); + }, finalListener::onFailure)); } @Override - protected GetJobsStatsAction.Response newResponse(GetJobsStatsAction.Request request, - List> tasks, - List taskOperationFailures, - List failedNodeExceptions) { + protected GetJobsStatsAction.Response newResponse( + GetJobsStatsAction.Request request, + List> tasks, + List taskOperationFailures, + List failedNodeExceptions + ) { List stats = new ArrayList<>(); for (QueryPage task : tasks) { stats.addAll(task.results()); } Collections.sort(stats, Comparator.comparing(GetJobsStatsAction.Response.JobStats::getJobId)); - return new GetJobsStatsAction.Response(taskOperationFailures, failedNodeExceptions, new QueryPage<>(stats, stats.size(), - Job.RESULTS_FIELD)); + return new GetJobsStatsAction.Response( + taskOperationFailures, + failedNodeExceptions, + new QueryPage<>(stats, stats.size(), Job.RESULTS_FIELD) + ); } @Override @@ -125,7 +143,16 @@ protected void taskOperation(GetJobsStatsAction.Request request, JobTask task, A TimeValue openTime = durationToTimeValue(processManager.jobOpenTime(task)); gatherForecastStats(jobId, forecastStats -> { JobStats jobStats = new JobStats( - jobId, dataCounts, modelSizeStats, forecastStats, jobState, node, assignmentExplanation, openTime, timingStats); + jobId, + dataCounts, + modelSizeStats, + forecastStats, + jobState, + node, + assignmentExplanation, + openTime, + timingStats + ); listener.onResponse(new QueryPage<>(Collections.singletonList(jobStats), 1, Job.RESULTS_FIELD)); }, listener::onFailure); @@ -136,8 +163,11 @@ protected void taskOperation(GetJobsStatsAction.Request request, JobTask task, A // Up until now we gathered the stats for jobs that were open, // This method will fetch the stats for missing jobs, that was stored in the jobs index - void gatherStatsForClosedJobs(GetJobsStatsAction.Request request, GetJobsStatsAction.Response response, - ActionListener listener) { + void gatherStatsForClosedJobs( + GetJobsStatsAction.Request request, + GetJobsStatsAction.Response response, + ActionListener listener + ) { List closedJobIds = determineJobIdsWithoutLiveStats(request.getExpandedJobsIds(), response.getResponse().results()); if (closedJobIds.isEmpty()) { listener.onResponse(response); @@ -168,8 +198,20 @@ void gatherStatsForClosedJobs(GetJobsStatsAction.Request request, GetJobsStatsAc if (pTask != null) { assignmentExplanation = pTask.getAssignment().getExplanation(); } - jobStats.set(slot, new JobStats(jobId, dataCounts, modelSizeStats, forecastStats, jobState, - null, assignmentExplanation, null, timingStats)); + jobStats.set( + slot, + new JobStats( + jobId, + dataCounts, + modelSizeStats, + forecastStats, + jobState, + null, + assignmentExplanation, + null, + timingStats + ) + ); if (counter.decrementAndGet() == 0) { if (searchException.get() != null) { // there was an error @@ -179,8 +221,13 @@ void gatherStatsForClosedJobs(GetJobsStatsAction.Request request, GetJobsStatsAc List results = response.getResponse().results(); results.addAll(jobStats.asList()); Collections.sort(results, Comparator.comparing(GetJobsStatsAction.Response.JobStats::getJobId)); - listener.onResponse(new GetJobsStatsAction.Response(response.getTaskFailures(), response.getNodeFailures(), - new QueryPage<>(results, results.size(), Job.RESULTS_FIELD))); + listener.onResponse( + new GetJobsStatsAction.Response( + response.getTaskFailures(), + response.getNodeFailures(), + new QueryPage<>(results, results.size(), Job.RESULTS_FIELD) + ) + ); } }, errorHandler); }, errorHandler); @@ -192,13 +239,22 @@ void gatherForecastStats(String jobId, Consumer handler, Consumer } void gatherDataCountsModelSizeStatsAndTimingStats( - String jobId, TriConsumer handler, Consumer errorHandler) { + String jobId, + TriConsumer handler, + Consumer errorHandler + ) { jobResultsProvider.dataCounts(jobId, dataCounts -> { - jobResultsProvider.modelSizeStats(jobId, modelSizeStats -> { - jobResultsProvider.timingStats(jobId, timingStats -> { - handler.apply(dataCounts, modelSizeStats, timingStats); - }, errorHandler); - }, errorHandler); + jobResultsProvider.modelSizeStats( + jobId, + modelSizeStats -> { + jobResultsProvider.timingStats( + jobId, + timingStats -> { handler.apply(dataCounts, modelSizeStats, timingStats); }, + errorHandler + ); + }, + errorHandler + ); }, errorHandler); } @@ -210,8 +266,7 @@ static TimeValue durationToTimeValue(Optional duration) { } } - static List determineJobIdsWithoutLiveStats(List requestedJobIds, - List stats) { + static List determineJobIdsWithoutLiveStats(List requestedJobIds, List stats) { Set excludeJobIds = stats.stream().map(GetJobsStatsAction.Response.JobStats::getJobId).collect(Collectors.toSet()); return requestedJobIds.stream().filter(jobId -> excludeJobIds.contains(jobId) == false).collect(Collectors.toList()); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetModelSnapshotsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetModelSnapshotsAction.java index 1b31ec642db46..75d76fdb7939b 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetModelSnapshotsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetModelSnapshotsAction.java @@ -20,8 +20,9 @@ import org.elasticsearch.xpack.ml.job.JobManager; import org.elasticsearch.xpack.ml.job.persistence.JobResultsProvider; -public class TransportGetModelSnapshotsAction extends HandledTransportAction { +public class TransportGetModelSnapshotsAction extends HandledTransportAction< + GetModelSnapshotsAction.Request, + GetModelSnapshotsAction.Response> { private static final Logger logger = LogManager.getLogger(TransportGetModelSnapshotsAction.class); @@ -29,16 +30,23 @@ public class TransportGetModelSnapshotsAction extends HandledTransportAction listener) { + protected void doExecute( + Task task, + GetModelSnapshotsAction.Request request, + ActionListener listener + ) { logger.debug( () -> new ParameterizedMessage( "Get model snapshots for job {} snapshot ID {}. from = {}, size = {} start = '{}', end='{}', sort={} descending={}", @@ -49,20 +57,20 @@ protected void doExecute(Task task, GetModelSnapshotsAction.Request request, request.getStart(), request.getEnd(), request.getSort(), - request.getDescOrder())); + request.getDescOrder() + ) + ); if (Strings.isAllOrWildcard(request.getJobId())) { getModelSnapshots(request, listener); return; } - jobManager.jobExists(request.getJobId(), ActionListener.wrap( - ok -> getModelSnapshots(request, listener), - listener::onFailure - )); + jobManager.jobExists(request.getJobId(), ActionListener.wrap(ok -> getModelSnapshots(request, listener), listener::onFailure)); } private void getModelSnapshots(GetModelSnapshotsAction.Request request, ActionListener listener) { - jobResultsProvider.modelSnapshots(request.getJobId(), + jobResultsProvider.modelSnapshots( + request.getJobId(), request.getPageParams().getFrom(), request.getPageParams().getSize(), request.getStart(), @@ -71,6 +79,7 @@ private void getModelSnapshots(GetModelSnapshotsAction.Request request, ActionLi request.getDescOrder(), request.getSnapshotId(), page -> listener.onResponse(new GetModelSnapshotsAction.Response(page)), - listener::onFailure); + listener::onFailure + ); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetOverallBucketsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetOverallBucketsAction.java index e38e6deb92202..91ea1ed52fd80 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetOverallBucketsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetOverallBucketsAction.java @@ -54,8 +54,9 @@ import static org.elasticsearch.xpack.core.ClientHelper.ML_ORIGIN; import static org.elasticsearch.xpack.core.ClientHelper.executeAsyncWithOrigin; -public class TransportGetOverallBucketsAction extends HandledTransportAction { +public class TransportGetOverallBucketsAction extends HandledTransportAction< + GetOverallBucketsAction.Request, + GetOverallBucketsAction.Response> { private static final Logger logger = LogManager.getLogger(TransportGetOverallBucketsAction.class); @@ -68,8 +69,14 @@ public class TransportGetOverallBucketsAction extends HandledTransportAction listener) { - jobManager.expandJobs(request.getJobId(), request.allowNoMatch(), ActionListener.wrap( - jobPage -> { - if (jobPage.count() == 0) { - listener.onResponse(new GetOverallBucketsAction.Response( - new QueryPage<>(Collections.emptyList(), 0, OverallBucket.RESULTS_FIELD))); - return; - } + protected void doExecute( + Task task, + GetOverallBucketsAction.Request request, + ActionListener listener + ) { + jobManager.expandJobs(request.getJobId(), request.allowNoMatch(), ActionListener.wrap(jobPage -> { + if (jobPage.count() == 0) { + listener.onResponse( + new GetOverallBucketsAction.Response(new QueryPage<>(Collections.emptyList(), 0, OverallBucket.RESULTS_FIELD)) + ); + return; + } - // As computing and potentially aggregating overall buckets might take a while, - // we run in a different thread to avoid blocking the network thread. - threadPool.executor(MachineLearning.UTILITY_THREAD_POOL_NAME).execute(() -> { - try { - getOverallBuckets(request, jobPage.results(), listener); - } catch (Exception e) { - listener.onFailure(e); - } - }); - }, - listener::onFailure - )); + // As computing and potentially aggregating overall buckets might take a while, + // we run in a different thread to avoid blocking the network thread. + threadPool.executor(MachineLearning.UTILITY_THREAD_POOL_NAME).execute(() -> { + try { + getOverallBuckets(request, jobPage.results(), listener); + } catch (Exception e) { + listener.onFailure(e); + } + }); + }, listener::onFailure)); } - private void getOverallBuckets(GetOverallBucketsAction.Request request, List jobs, - ActionListener listener) { + private void getOverallBuckets( + GetOverallBucketsAction.Request request, + List jobs, + ActionListener listener + ) { JobsContext jobsContext = JobsContext.build(jobs, request); ActionListener> overallBucketsListener = ActionListener.wrap(overallBuckets -> { - listener.onResponse(new GetOverallBucketsAction.Response(new QueryPage<>(overallBuckets, overallBuckets.size(), - OverallBucket.RESULTS_FIELD))); + listener.onResponse( + new GetOverallBucketsAction.Response(new QueryPage<>(overallBuckets, overallBuckets.size(), OverallBucket.RESULTS_FIELD)) + ); }, listener::onFailure); ActionListener chunkedBucketSearcherListener = ActionListener.wrap(searcher -> { if (searcher == null) { - listener.onResponse(new GetOverallBucketsAction.Response( - new QueryPage<>(Collections.emptyList(), 0, OverallBucket.RESULTS_FIELD))); + listener.onResponse( + new GetOverallBucketsAction.Response(new QueryPage<>(Collections.emptyList(), 0, OverallBucket.RESULTS_FIELD)) + ); return; } searcher.searchAndComputeOverallBuckets(overallBucketsListener); }, listener::onFailure); - OverallBucketsProvider overallBucketsProvider = new OverallBucketsProvider(jobsContext.maxBucketSpan, request.getTopN(), - request.getOverallScore()); - OverallBucketsProcessor overallBucketsProcessor = requiresAggregation(request, jobsContext.maxBucketSpan) ? - new OverallBucketsAggregator(request.getBucketSpan()): new OverallBucketsCollector(); + OverallBucketsProvider overallBucketsProvider = new OverallBucketsProvider( + jobsContext.maxBucketSpan, + request.getTopN(), + request.getOverallScore() + ); + OverallBucketsProcessor overallBucketsProcessor = requiresAggregation(request, jobsContext.maxBucketSpan) + ? new OverallBucketsAggregator(request.getBucketSpan()) + : new OverallBucketsCollector(); initChunkedBucketSearcher(request, jobsContext, overallBucketsProvider, overallBucketsProcessor, chunkedBucketSearcherListener); } @@ -133,36 +150,59 @@ private static boolean requiresAggregation(GetOverallBucketsAction.Request reque private static void checkValidBucketSpan(TimeValue bucketSpan, TimeValue maxBucketSpan) { if (bucketSpan != null && bucketSpan.compareTo(maxBucketSpan) < 0) { - throw ExceptionsHelper.badRequestException("Param [{}] must be greater or equal to the max bucket_span [{}]", - GetOverallBucketsAction.Request.BUCKET_SPAN, maxBucketSpan.getStringRep()); + throw ExceptionsHelper.badRequestException( + "Param [{}] must be greater or equal to the max bucket_span [{}]", + GetOverallBucketsAction.Request.BUCKET_SPAN, + maxBucketSpan.getStringRep() + ); } } - private void initChunkedBucketSearcher(GetOverallBucketsAction.Request request, JobsContext jobsContext, - OverallBucketsProvider overallBucketsProvider, - OverallBucketsProcessor overallBucketsProcessor, - ActionListener listener) { + private void initChunkedBucketSearcher( + GetOverallBucketsAction.Request request, + JobsContext jobsContext, + OverallBucketsProvider overallBucketsProvider, + OverallBucketsProcessor overallBucketsProcessor, + ActionListener listener + ) { long maxBucketSpanMillis = jobsContext.maxBucketSpan.millis(); - SearchRequest searchRequest = buildSearchRequest(request.getStart(), request.getEnd(), request.isExcludeInterim(), - maxBucketSpanMillis, jobsContext.indices); + SearchRequest searchRequest = buildSearchRequest( + request.getStart(), + request.getEnd(), + request.isExcludeInterim(), + maxBucketSpanMillis, + jobsContext.indices + ); searchRequest.source().aggregation(AggregationBuilders.min(EARLIEST_TIME).field(Result.TIMESTAMP.getPreferredName())); searchRequest.source().aggregation(AggregationBuilders.max(LATEST_TIME).field(Result.TIMESTAMP.getPreferredName())); - executeAsyncWithOrigin(client.threadPool().getThreadContext(), ML_ORIGIN, searchRequest, - ActionListener.wrap(searchResponse -> { - long totalHits = searchResponse.getHits().getTotalHits().value; - if (totalHits > 0) { - Aggregations aggregations = searchResponse.getAggregations(); - Min min = aggregations.get(EARLIEST_TIME); - long earliestTime = Intervals.alignToFloor((long) min.getValue(), maxBucketSpanMillis); - Max max = aggregations.get(LATEST_TIME); - long latestTime = Intervals.alignToCeil((long) max.getValue() + 1, maxBucketSpanMillis); - listener.onResponse(new ChunkedBucketSearcher(jobsContext, earliestTime, latestTime, request.isExcludeInterim(), - overallBucketsProvider, overallBucketsProcessor)); - } else { - listener.onResponse(null); - } - }, listener::onFailure), - client::search); + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + ML_ORIGIN, + searchRequest, + ActionListener.wrap(searchResponse -> { + long totalHits = searchResponse.getHits().getTotalHits().value; + if (totalHits > 0) { + Aggregations aggregations = searchResponse.getAggregations(); + Min min = aggregations.get(EARLIEST_TIME); + long earliestTime = Intervals.alignToFloor((long) min.getValue(), maxBucketSpanMillis); + Max max = aggregations.get(LATEST_TIME); + long latestTime = Intervals.alignToCeil((long) max.getValue() + 1, maxBucketSpanMillis); + listener.onResponse( + new ChunkedBucketSearcher( + jobsContext, + earliestTime, + latestTime, + request.isExcludeInterim(), + overallBucketsProvider, + overallBucketsProcessor + ) + ); + } else { + listener.onResponse(null); + } + }, listener::onFailure), + client::search + ); } private static class JobsContext { @@ -212,9 +252,14 @@ private class ChunkedBucketSearcher { private final OverallBucketsProvider overallBucketsProvider; private final OverallBucketsProcessor overallBucketsProcessor; - ChunkedBucketSearcher(JobsContext jobsContext, long startTime, long endTime, - boolean excludeInterim, OverallBucketsProvider overallBucketsProvider, - OverallBucketsProcessor overallBucketsProcessor) { + ChunkedBucketSearcher( + JobsContext jobsContext, + long startTime, + long endTime, + boolean excludeInterim, + OverallBucketsProvider overallBucketsProvider, + OverallBucketsProcessor overallBucketsProcessor + ) { this.indices = jobsContext.indices; this.maxBucketSpanMillis = jobsContext.maxBucketSpan.millis(); this.chunkMillis = BUCKETS_PER_CHUNK * maxBucketSpanMillis; @@ -231,20 +276,28 @@ void searchAndComputeOverallBuckets(ActionListener> listener listener.onResponse(overallBucketsProcessor.finish()); return; } - executeAsyncWithOrigin(client.threadPool().getThreadContext(), ML_ORIGIN, nextSearch(), - ActionListener.wrap(searchResponse -> { - Histogram histogram = searchResponse.getAggregations().get(Result.TIMESTAMP.getPreferredName()); - overallBucketsProcessor.process(overallBucketsProvider.computeOverallBuckets(histogram)); - if (overallBucketsProcessor.size() > MAX_RESULT_COUNT) { - listener.onFailure( - ExceptionsHelper.badRequestException("Unable to return more than [{}] results; please use " + - "parameters [{}] and [{}] to limit the time range", MAX_RESULT_COUNT, - GetOverallBucketsAction.Request.START, GetOverallBucketsAction.Request.END)); - return; - } - searchAndComputeOverallBuckets(listener); - }, listener::onFailure), - client::search); + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + ML_ORIGIN, + nextSearch(), + ActionListener.wrap(searchResponse -> { + Histogram histogram = searchResponse.getAggregations().get(Result.TIMESTAMP.getPreferredName()); + overallBucketsProcessor.process(overallBucketsProvider.computeOverallBuckets(histogram)); + if (overallBucketsProcessor.size() > MAX_RESULT_COUNT) { + listener.onFailure( + ExceptionsHelper.badRequestException( + "Unable to return more than [{}] results; please use " + "parameters [{}] and [{}] to limit the time range", + MAX_RESULT_COUNT, + GetOverallBucketsAction.Request.START, + GetOverallBucketsAction.Request.END + ) + ); + return; + } + searchAndComputeOverallBuckets(listener); + }, listener::onFailure), + client::search + ); } SearchRequest nextSearch() { @@ -257,17 +310,15 @@ SearchRequest nextSearch() { } } - private static SearchRequest buildSearchRequest(Long start, Long end, boolean excludeInterim, long bucketSpanMillis, - String[] indices) { + private static SearchRequest buildSearchRequest(Long start, Long end, boolean excludeInterim, long bucketSpanMillis, String[] indices) { String startTime = start == null ? null : String.valueOf(Intervals.alignToCeil(start, bucketSpanMillis)); String endTime = end == null ? null : String.valueOf(Intervals.alignToFloor(end, bucketSpanMillis)); - SearchSourceBuilder searchSourceBuilder = new BucketsQueryBuilder() - .size(0) - .includeInterim(excludeInterim == false) - .start(startTime) - .end(endTime) - .build(); + SearchSourceBuilder searchSourceBuilder = new BucketsQueryBuilder().size(0) + .includeInterim(excludeInterim == false) + .start(startTime) + .end(endTime) + .build(); searchSourceBuilder.trackTotalHits(true); SearchRequest searchRequest = new SearchRequest(indices); @@ -278,15 +329,17 @@ private static SearchRequest buildSearchRequest(Long start, Long end, boolean ex private static AggregationBuilder buildAggregations(long maxBucketSpanMillis, int jobCount) { AggregationBuilder overallScoreAgg = AggregationBuilders.max(OverallBucket.OVERALL_SCORE.getPreferredName()) - .field(Bucket.ANOMALY_SCORE.getPreferredName()); + .field(Bucket.ANOMALY_SCORE.getPreferredName()); AggregationBuilder jobsAgg = AggregationBuilders.terms(Job.ID.getPreferredName()) - .field(Job.ID.getPreferredName()).size(jobCount).subAggregation(overallScoreAgg); + .field(Job.ID.getPreferredName()) + .size(jobCount) + .subAggregation(overallScoreAgg); AggregationBuilder interimAgg = AggregationBuilders.max(Result.IS_INTERIM.getPreferredName()) - .field(Result.IS_INTERIM.getPreferredName()); + .field(Result.IS_INTERIM.getPreferredName()); return AggregationBuilders.dateHistogram(Result.TIMESTAMP.getPreferredName()) - .field(Result.TIMESTAMP.getPreferredName()) - .fixedInterval(new DateHistogramInterval(maxBucketSpanMillis + "ms")) - .subAggregation(jobsAgg) - .subAggregation(interimAgg); + .field(Result.TIMESTAMP.getPreferredName()) + .fixedInterval(new DateHistogramInterval(maxBucketSpanMillis + "ms")) + .subAggregation(jobsAgg) + .subAggregation(interimAgg); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetRecordsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetRecordsAction.java index 4c5ae3a60e4e6..ef82267685fb5 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetRecordsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetRecordsAction.java @@ -25,8 +25,13 @@ public class TransportGetRecordsAction extends HandledTransportAction listener) { - jobManager.jobExists(request.getJobId(), ActionListener.wrap( - jobExists -> { - RecordsQueryBuilder query = new RecordsQueryBuilder() - .includeInterim(request.isExcludeInterim() == false) - .epochStart(request.getStart()) - .epochEnd(request.getEnd()) - .from(request.getPageParams().getFrom()) - .size(request.getPageParams().getSize()) - .recordScore(request.getRecordScoreFilter()) - .sortField(request.getSort()) - .sortDescending(request.isDescending()); - jobResultsProvider.records(request.getJobId(), query, page -> - listener.onResponse(new GetRecordsAction.Response(page)), listener::onFailure, client); - }, - listener::onFailure - )); + jobManager.jobExists(request.getJobId(), ActionListener.wrap(jobExists -> { + RecordsQueryBuilder query = new RecordsQueryBuilder().includeInterim(request.isExcludeInterim() == false) + .epochStart(request.getStart()) + .epochEnd(request.getEnd()) + .from(request.getPageParams().getFrom()) + .size(request.getPageParams().getSize()) + .recordScore(request.getRecordScoreFilter()) + .sortField(request.getSort()) + .sortDescending(request.isDescending()); + jobResultsProvider.records( + request.getJobId(), + query, + page -> listener.onResponse(new GetRecordsAction.Response(page)), + listener::onFailure, + client + ); + }, listener::onFailure)); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetTrainedModelsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetTrainedModelsAction.java index 2edd20ee276af..d6b6bfeb0bf0d 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetTrainedModelsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetTrainedModelsAction.java @@ -10,8 +10,8 @@ import org.elasticsearch.action.support.ActionFilters; import org.elasticsearch.action.support.HandledTransportAction; import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.inject.Inject; +import org.elasticsearch.core.Tuple; import org.elasticsearch.tasks.Task; import org.elasticsearch.transport.TransportService; import org.elasticsearch.xpack.core.ml.action.GetTrainedModelsAction; @@ -27,16 +27,18 @@ import java.util.Map; import java.util.Set; - public class TransportGetTrainedModelsAction extends HandledTransportAction { private final TrainedModelProvider provider; private final ClusterService clusterService; + @Inject - public TransportGetTrainedModelsAction(TransportService transportService, - ActionFilters actionFilters, - ClusterService clusterService, - TrainedModelProvider trainedModelProvider) { + public TransportGetTrainedModelsAction( + TransportService transportService, + ActionFilters actionFilters, + ClusterService clusterService, + TrainedModelProvider trainedModelProvider + ) { super(GetTrainedModelsAction.NAME, transportService, actionFilters, GetTrainedModelsAction.Request::new); this.provider = trainedModelProvider; this.clusterService = clusterService; @@ -47,53 +49,47 @@ protected void doExecute(Task task, Request request, ActionListener li Response.Builder responseBuilder = Response.builder(); - ActionListener>>> idExpansionListener = ActionListener.wrap( - totalAndIds -> { - responseBuilder.setTotalCount(totalAndIds.v1()); + ActionListener>>> idExpansionListener = ActionListener.wrap(totalAndIds -> { + responseBuilder.setTotalCount(totalAndIds.v1()); - if (totalAndIds.v2().isEmpty()) { - listener.onResponse(responseBuilder.build()); - return; - } + if (totalAndIds.v2().isEmpty()) { + listener.onResponse(responseBuilder.build()); + return; + } - if (request.getIncludes().isIncludeModelDefinition() && totalAndIds.v2().size() > 1) { - listener.onFailure( - ExceptionsHelper.badRequestException(Messages.INFERENCE_TOO_MANY_DEFINITIONS_REQUESTED) - ); - return; - } + if (request.getIncludes().isIncludeModelDefinition() && totalAndIds.v2().size() > 1) { + listener.onFailure(ExceptionsHelper.badRequestException(Messages.INFERENCE_TOO_MANY_DEFINITIONS_REQUESTED)); + return; + } - if (request.getIncludes().isIncludeModelDefinition()) { - Map.Entry> modelIdAndAliases = totalAndIds.v2().entrySet().iterator().next(); - provider.getTrainedModel( - modelIdAndAliases.getKey(), - modelIdAndAliases.getValue(), - request.getIncludes(), - ActionListener.wrap( - config -> listener.onResponse(responseBuilder.setModels(Collections.singletonList(config)).build()), - listener::onFailure - ) - ); - } else { - provider.getTrainedModels( - totalAndIds.v2(), - request.getIncludes(), - request.isAllowNoResources(), - ActionListener.wrap( - configs -> listener.onResponse(responseBuilder.setModels(configs).build()), - listener::onFailure - ) - ); - } - }, - listener::onFailure - ); - provider.expandIds(request.getResourceId(), + if (request.getIncludes().isIncludeModelDefinition()) { + Map.Entry> modelIdAndAliases = totalAndIds.v2().entrySet().iterator().next(); + provider.getTrainedModel( + modelIdAndAliases.getKey(), + modelIdAndAliases.getValue(), + request.getIncludes(), + ActionListener.wrap( + config -> listener.onResponse(responseBuilder.setModels(Collections.singletonList(config)).build()), + listener::onFailure + ) + ); + } else { + provider.getTrainedModels( + totalAndIds.v2(), + request.getIncludes(), + request.isAllowNoResources(), + ActionListener.wrap(configs -> listener.onResponse(responseBuilder.setModels(configs).build()), listener::onFailure) + ); + } + }, listener::onFailure); + provider.expandIds( + request.getResourceId(), request.isAllowNoResources(), request.getPageParams(), new HashSet<>(request.getTags()), ModelAliasMetadata.fromState(clusterService.state()), - idExpansionListener); + idExpansionListener + ); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetTrainedModelsStatsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetTrainedModelsStatsAction.java index 01775e27a74cc..e211b975c5149 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetTrainedModelsStatsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportGetTrainedModelsStatsAction.java @@ -48,8 +48,9 @@ import static org.elasticsearch.xpack.core.ClientHelper.ML_ORIGIN; import static org.elasticsearch.xpack.core.ClientHelper.executeAsyncWithOrigin; -public class TransportGetTrainedModelsStatsAction extends HandledTransportAction { +public class TransportGetTrainedModelsStatsAction extends HandledTransportAction< + GetTrainedModelsStatsAction.Request, + GetTrainedModelsStatsAction.Response> { private final Client client; private final ClusterService clusterService; @@ -57,12 +58,14 @@ public class TransportGetTrainedModelsStatsAction extends HandledTransportAction private final TrainedModelProvider trainedModelProvider; @Inject - public TransportGetTrainedModelsStatsAction(TransportService transportService, - ActionFilters actionFilters, - ClusterService clusterService, - IngestService ingestService, - TrainedModelProvider trainedModelProvider, - Client client) { + public TransportGetTrainedModelsStatsAction( + TransportService transportService, + ActionFilters actionFilters, + ClusterService clusterService, + IngestService ingestService, + TrainedModelProvider trainedModelProvider, + Client client + ) { super(GetTrainedModelsStatsAction.NAME, transportService, actionFilters, GetTrainedModelsStatsAction.Request::new); this.client = client; this.clusterService = clusterService; @@ -71,76 +74,75 @@ public TransportGetTrainedModelsStatsAction(TransportService transportService, } @Override - protected void doExecute(Task task, - GetTrainedModelsStatsAction.Request request, - ActionListener listener) { + protected void doExecute( + Task task, + GetTrainedModelsStatsAction.Request request, + ActionListener listener + ) { final ModelAliasMetadata currentMetadata = ModelAliasMetadata.fromState(clusterService.state()); GetTrainedModelsStatsAction.Response.Builder responseBuilder = new GetTrainedModelsStatsAction.Response.Builder(); ActionListener> inferenceStatsListener = ActionListener.wrap( - inferenceStats -> listener.onResponse(responseBuilder.setInferenceStatsByModelId(inferenceStats.stream() - .collect(Collectors.toMap(InferenceStats::getModelId, Function.identity()))) - .build()), + inferenceStats -> listener.onResponse( + responseBuilder.setInferenceStatsByModelId( + inferenceStats.stream().collect(Collectors.toMap(InferenceStats::getModelId, Function.identity())) + ).build() + ), listener::onFailure ); - ActionListener nodesStatsListener = ActionListener.wrap( - nodesStatsResponse -> { - Set allPossiblePipelineReferences = responseBuilder.getExpandedIdsWithAliases() - .entrySet() - .stream() - .flatMap(entry -> Stream.concat(entry.getValue().stream(), Stream.of(entry.getKey()))) - .collect(Collectors.toSet()); - Map> pipelineIdsByModelIdsOrAliases = pipelineIdsByModelIdsOrAliases(clusterService.state(), - ingestService, - allPossiblePipelineReferences); - Map modelIdIngestStats = inferenceIngestStatsByModelId(nodesStatsResponse, - currentMetadata, - pipelineIdsByModelIdsOrAliases - ); - responseBuilder.setIngestStatsByModelId(modelIdIngestStats); - trainedModelProvider.getInferenceStats( - responseBuilder.getExpandedIdsWithAliases().keySet().toArray(new String[0]), - inferenceStatsListener - ); - }, - listener::onFailure - ); - - ActionListener>>> idsListener = ActionListener.wrap( - tuple -> { - responseBuilder.setExpandedIdsWithAliases(tuple.v2()).setTotalModelCount(tuple.v1()); - String[] ingestNodes = ingestNodes(clusterService.state()); - NodesStatsRequest nodesStatsRequest = new NodesStatsRequest(ingestNodes).clear() - .addMetric(NodesStatsRequest.Metric.INGEST.metricName()); - executeAsyncWithOrigin(client, ML_ORIGIN, NodesStatsAction.INSTANCE, nodesStatsRequest, nodesStatsListener); - }, - listener::onFailure - ); - trainedModelProvider.expandIds(request.getResourceId(), + ActionListener nodesStatsListener = ActionListener.wrap(nodesStatsResponse -> { + Set allPossiblePipelineReferences = responseBuilder.getExpandedIdsWithAliases() + .entrySet() + .stream() + .flatMap(entry -> Stream.concat(entry.getValue().stream(), Stream.of(entry.getKey()))) + .collect(Collectors.toSet()); + Map> pipelineIdsByModelIdsOrAliases = pipelineIdsByModelIdsOrAliases( + clusterService.state(), + ingestService, + allPossiblePipelineReferences + ); + Map modelIdIngestStats = inferenceIngestStatsByModelId( + nodesStatsResponse, + currentMetadata, + pipelineIdsByModelIdsOrAliases + ); + responseBuilder.setIngestStatsByModelId(modelIdIngestStats); + trainedModelProvider.getInferenceStats( + responseBuilder.getExpandedIdsWithAliases().keySet().toArray(new String[0]), + inferenceStatsListener + ); + }, listener::onFailure); + + ActionListener>>> idsListener = ActionListener.wrap(tuple -> { + responseBuilder.setExpandedIdsWithAliases(tuple.v2()).setTotalModelCount(tuple.v1()); + String[] ingestNodes = ingestNodes(clusterService.state()); + NodesStatsRequest nodesStatsRequest = new NodesStatsRequest(ingestNodes).clear() + .addMetric(NodesStatsRequest.Metric.INGEST.metricName()); + executeAsyncWithOrigin(client, ML_ORIGIN, NodesStatsAction.INSTANCE, nodesStatsRequest, nodesStatsListener); + }, listener::onFailure); + trainedModelProvider.expandIds( + request.getResourceId(), request.isAllowNoResources(), request.getPageParams(), Collections.emptySet(), currentMetadata, - idsListener); + idsListener + ); } - static Map inferenceIngestStatsByModelId(NodesStatsResponse response, - ModelAliasMetadata currentMetadata, - Map> modelIdToPipelineId) { + static Map inferenceIngestStatsByModelId( + NodesStatsResponse response, + ModelAliasMetadata currentMetadata, + Map> modelIdToPipelineId + ) { Map ingestStatsMap = new HashMap<>(); - Map> trueModelIdToPipelines = modelIdToPipelineId.entrySet() - .stream() - .collect(Collectors.toMap( - entry -> { - String maybeModelId = currentMetadata.getModelId(entry.getKey()); - return maybeModelId == null ? entry.getKey() : maybeModelId; - }, - Map.Entry::getValue, - Sets::union - )); + Map> trueModelIdToPipelines = modelIdToPipelineId.entrySet().stream().collect(Collectors.toMap(entry -> { + String maybeModelId = currentMetadata.getModelId(entry.getKey()); + return maybeModelId == null ? entry.getKey() : maybeModelId; + }, Map.Entry::getValue, Sets::union)); trueModelIdToPipelines.forEach((modelId, pipelineIds) -> { List collectedStats = response.getNodes() .stream() @@ -164,16 +166,18 @@ static Map> pipelineIdsByModelIdsOrAliases(ClusterState stat ingestMetadata.getPipelines().forEach((pipelineId, pipelineConfiguration) -> { try { - Pipeline pipeline = Pipeline.create(pipelineId, + Pipeline pipeline = Pipeline.create( + pipelineId, pipelineConfiguration.getConfigAsMap(), ingestService.getProcessorFactories(), - ingestService.getScriptService()); + ingestService.getScriptService() + ); pipeline.getProcessors().forEach(processor -> { if (processor instanceof InferenceProcessor) { InferenceProcessor inferenceProcessor = (InferenceProcessor) processor; if (modelIds.contains(inferenceProcessor.getModelId())) { - pipelineIdsByModelIds.computeIfAbsent(inferenceProcessor.getModelId(), - m -> new LinkedHashSet<>()).add(pipelineId); + pipelineIdsByModelIds.computeIfAbsent(inferenceProcessor.getModelId(), m -> new LinkedHashSet<>()) + .add(pipelineId); } } }); @@ -209,7 +213,8 @@ static IngestStats ingestStatsForPipelineIds(NodeStats nodeStats, Set pi return new IngestStats( new IngestStats.Stats(ingestCount.count(), ingestTimeInMillis.count(), ingestCurrent.count(), ingestFailedCount.count()), filteredPipelineStats, - filteredProcessorStats); + filteredProcessorStats + ); } private static IngestStats mergeStats(List ingestStatsList) { @@ -220,31 +225,35 @@ private static IngestStats mergeStats(List ingestStatsList) { ingestStatsList.forEach(ingestStats -> { ingestStats.getPipelineStats() - .forEach(pipelineStat -> - pipelineStatsAcc.computeIfAbsent(pipelineStat.getPipelineId(), - p -> new IngestStatsAccumulator()).inc(pipelineStat.getStats())); - - ingestStats.getProcessorStats() - .forEach((pipelineId, processorStat) -> { - Map processorAcc = processorStatsAcc.computeIfAbsent(pipelineId, - k -> new LinkedHashMap<>()); - processorStat.forEach(p -> - processorAcc.computeIfAbsent(p.getName(), - k -> new IngestStatsAccumulator(p.getType())).inc(p.getStats())); - }); + .forEach( + pipelineStat -> pipelineStatsAcc.computeIfAbsent(pipelineStat.getPipelineId(), p -> new IngestStatsAccumulator()) + .inc(pipelineStat.getStats()) + ); + + ingestStats.getProcessorStats().forEach((pipelineId, processorStat) -> { + Map processorAcc = processorStatsAcc.computeIfAbsent( + pipelineId, + k -> new LinkedHashMap<>() + ); + processorStat.forEach( + p -> processorAcc.computeIfAbsent(p.getName(), k -> new IngestStatsAccumulator(p.getType())).inc(p.getStats()) + ); + }); totalStats.inc(ingestStats.getTotalStats()); }); List pipelineStatList = new ArrayList<>(pipelineStatsAcc.size()); - pipelineStatsAcc.forEach((pipelineId, accumulator) -> - pipelineStatList.add(new IngestStats.PipelineStat(pipelineId, accumulator.build()))); + pipelineStatsAcc.forEach( + (pipelineId, accumulator) -> pipelineStatList.add(new IngestStats.PipelineStat(pipelineId, accumulator.build())) + ); Map> processorStatList = new LinkedHashMap<>(processorStatsAcc.size()); processorStatsAcc.forEach((pipelineId, accumulatorMap) -> { List processorStats = new ArrayList<>(accumulatorMap.size()); - accumulatorMap.forEach((processorName, acc) -> - processorStats.add(new IngestStats.ProcessorStat(processorName, acc.type, acc.build()))); + accumulatorMap.forEach( + (processorName, acc) -> processorStats.add(new IngestStats.ProcessorStat(processorName, acc.type, acc.build())) + ); processorStatList.put(pipelineId, processorStats); }); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportInferTrainedModelDeploymentAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportInferTrainedModelDeploymentAction.java index 554001f5229d9..21ed3245ab1be 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportInferTrainedModelDeploymentAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportInferTrainedModelDeploymentAction.java @@ -28,26 +28,43 @@ import java.util.List; -public class TransportInferTrainedModelDeploymentAction extends TransportTasksAction { +public class TransportInferTrainedModelDeploymentAction extends TransportTasksAction< + TrainedModelDeploymentTask, + InferTrainedModelDeploymentAction.Request, + InferTrainedModelDeploymentAction.Response, + InferTrainedModelDeploymentAction.Response> { @Inject - public TransportInferTrainedModelDeploymentAction(ClusterService clusterService, TransportService transportService, - ActionFilters actionFilters) { - super(InferTrainedModelDeploymentAction.NAME, clusterService, transportService, actionFilters, - InferTrainedModelDeploymentAction.Request::new, InferTrainedModelDeploymentAction.Response::new, - InferTrainedModelDeploymentAction.Response::new, ThreadPool.Names.SAME); + public TransportInferTrainedModelDeploymentAction( + ClusterService clusterService, + TransportService transportService, + ActionFilters actionFilters + ) { + super( + InferTrainedModelDeploymentAction.NAME, + clusterService, + transportService, + actionFilters, + InferTrainedModelDeploymentAction.Request::new, + InferTrainedModelDeploymentAction.Response::new, + InferTrainedModelDeploymentAction.Response::new, + ThreadPool.Names.SAME + ); } @Override - protected void doExecute(Task task, InferTrainedModelDeploymentAction.Request request, - ActionListener listener) { + protected void doExecute( + Task task, + InferTrainedModelDeploymentAction.Request request, + ActionListener listener + ) { String deploymentId = request.getDeploymentId(); // We need to check whether there is at least an assigned task here, otherwise we cannot redirect to the // node running the job task. - TrainedModelAllocation allocation = TrainedModelAllocationMetadata - .allocationForModelId(clusterService.state(), request.getDeploymentId()) - .orElse(null); + TrainedModelAllocation allocation = TrainedModelAllocationMetadata.allocationForModelId( + clusterService.state(), + request.getDeploymentId() + ).orElse(null); if (allocation == null) { String message = "Cannot perform requested action because deployment [" + deploymentId + "] is not started"; listener.onFailure(ExceptionsHelper.conflictStatusException(message)); @@ -66,10 +83,12 @@ protected void doExecute(Task task, InferTrainedModelDeploymentAction.Request re } @Override - protected InferTrainedModelDeploymentAction.Response newResponse(InferTrainedModelDeploymentAction.Request request, - List tasks, - List taskOperationFailures, - List failedNodeExceptions) { + protected InferTrainedModelDeploymentAction.Response newResponse( + InferTrainedModelDeploymentAction.Request request, + List tasks, + List taskOperationFailures, + List failedNodeExceptions + ) { if (taskOperationFailures.isEmpty() == false) { throw org.elasticsearch.ExceptionsHelper.convertToElastic(taskOperationFailures.get(0).getCause()); } else if (failedNodeExceptions.isEmpty() == false) { @@ -86,8 +105,11 @@ protected InferTrainedModelDeploymentAction.Response newResponse(InferTrainedMod } @Override - protected void taskOperation(InferTrainedModelDeploymentAction.Request request, TrainedModelDeploymentTask task, - ActionListener listener) { + protected void taskOperation( + InferTrainedModelDeploymentAction.Request request, + TrainedModelDeploymentTask task, + ActionListener listener + ) { task.infer( request.getDocs().get(0), request.getUpdate(), diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportInternalInferModelAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportInternalInferModelAction.java index 91f54e4f2e407..c3742b670bd61 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportInternalInferModelAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportInternalInferModelAction.java @@ -43,7 +43,6 @@ import static org.elasticsearch.xpack.core.ClientHelper.ML_ORIGIN; import static org.elasticsearch.xpack.core.ClientHelper.executeAsyncWithOrigin; - public class TransportInternalInferModelAction extends HandledTransportAction { private final ModelLoadingService modelLoadingService; @@ -53,13 +52,15 @@ public class TransportInternalInferModelAction extends HandledTransportAction li responseBuilder.setLicensed(true); doInfer(request, responseBuilder, listener); } else { - trainedModelProvider.getTrainedModel(request.getModelId(), GetTrainedModelsAction.Includes.empty(), ActionListener.wrap( - trainedModelConfig -> { + trainedModelProvider.getTrainedModel( + request.getModelId(), + GetTrainedModelsAction.Includes.empty(), + ActionListener.wrap(trainedModelConfig -> { responseBuilder.setLicensed(licenseState.isAllowedByLicense(trainedModelConfig.getLicenseLevel())); if (licenseState.isAllowedByLicense(trainedModelConfig.getLicenseLevel()) || request.isPreviouslyLicensed()) { doInfer(request, responseBuilder, listener); } else { listener.onFailure(LicenseUtils.newComplianceException(XPackField.MACHINE_LEARNING)); } - }, - listener::onFailure - )); + }, listener::onFailure) + ); } } @@ -105,53 +107,61 @@ private boolean isAllocatedModel(String modelId) { } private void getModelAndInfer(Request request, Response.Builder responseBuilder, ActionListener listener) { - ActionListener getModelListener = ActionListener.wrap( - model -> { - TypedChainTaskExecutor typedChainTaskExecutor = - new TypedChainTaskExecutor<>(client.threadPool().executor(ThreadPool.Names.SAME), - // run through all tasks - r -> true, - // Always fail immediately and return an error - ex -> true); - request.getObjectsToInfer().forEach(stringObjectMap -> - typedChainTaskExecutor.add(chainedTask -> - model.infer(stringObjectMap, request.getUpdate(), chainedTask))); - - typedChainTaskExecutor.execute(ActionListener.wrap( - inferenceResultsInterfaces -> { - model.release(); - listener.onResponse(responseBuilder.setInferenceResults(inferenceResultsInterfaces) - .setModelId(model.getModelId()) - .build()); - }, - e -> { - model.release(); - listener.onFailure(e); - } - )); - }, - listener::onFailure - ); + ActionListener getModelListener = ActionListener.wrap(model -> { + TypedChainTaskExecutor typedChainTaskExecutor = new TypedChainTaskExecutor<>( + client.threadPool().executor(ThreadPool.Names.SAME), + // run through all tasks + r -> true, + // Always fail immediately and return an error + ex -> true + ); + request.getObjectsToInfer() + .forEach( + stringObjectMap -> typedChainTaskExecutor.add( + chainedTask -> model.infer(stringObjectMap, request.getUpdate(), chainedTask) + ) + ); + + typedChainTaskExecutor.execute(ActionListener.wrap(inferenceResultsInterfaces -> { + model.release(); + listener.onResponse(responseBuilder.setInferenceResults(inferenceResultsInterfaces).setModelId(model.getModelId()).build()); + }, e -> { + model.release(); + listener.onFailure(e); + })); + }, listener::onFailure); modelLoadingService.getModelForPipeline(request.getModelId(), getModelListener); } private void inferAgainstAllocatedModel(Request request, Response.Builder responseBuilder, ActionListener listener) { - TypedChainTaskExecutor typedChainTaskExecutor = - new TypedChainTaskExecutor<>(client.threadPool().executor(ThreadPool.Names.SAME), - // run through all tasks - r -> true, - // Always fail immediately and return an error - ex -> true); - request.getObjectsToInfer().forEach(stringObjectMap -> typedChainTaskExecutor.add( - chainedTask -> inferSingleDocAgainstAllocatedModel(request.getModelId(), request.getUpdate(), stringObjectMap, chainedTask))); - - typedChainTaskExecutor.execute(ActionListener.wrap( - inferenceResults -> listener.onResponse(responseBuilder.setInferenceResults(inferenceResults) - .setModelId(request.getModelId()) - .build()), - listener::onFailure - )); + TypedChainTaskExecutor typedChainTaskExecutor = new TypedChainTaskExecutor<>( + client.threadPool().executor(ThreadPool.Names.SAME), + // run through all tasks + r -> true, + // Always fail immediately and return an error + ex -> true + ); + request.getObjectsToInfer() + .forEach( + stringObjectMap -> typedChainTaskExecutor.add( + chainedTask -> inferSingleDocAgainstAllocatedModel( + request.getModelId(), + request.getUpdate(), + stringObjectMap, + chainedTask + ) + ) + ); + + typedChainTaskExecutor.execute( + ActionListener.wrap( + inferenceResults -> listener.onResponse( + responseBuilder.setInferenceResults(inferenceResults).setModelId(request.getModelId()).build() + ), + listener::onFailure + ) + ); } private void inferSingleDocAgainstAllocatedModel( @@ -160,27 +170,29 @@ private void inferSingleDocAgainstAllocatedModel( Map doc, ActionListener listener ) { - executeAsyncWithOrigin(client, + executeAsyncWithOrigin( + client, ML_ORIGIN, InferTrainedModelDeploymentAction.INSTANCE, - new InferTrainedModelDeploymentAction.Request(modelId, inferenceConfigUpdate, Collections.singletonList(doc), - TimeValue.MAX_VALUE), - ActionListener.wrap( - r -> listener.onResponse(r.getResults()), - e -> { - Throwable unwrapped = ExceptionsHelper.unwrapCause(e); - if (unwrapped instanceof ElasticsearchStatusException) { - ElasticsearchStatusException ex = (ElasticsearchStatusException) unwrapped; - if (ex.status().equals(RestStatus.TOO_MANY_REQUESTS)) { - listener.onFailure(ex); - } else { - listener.onResponse(new WarningInferenceResults(ex.getMessage())); - } + new InferTrainedModelDeploymentAction.Request( + modelId, + inferenceConfigUpdate, + Collections.singletonList(doc), + TimeValue.MAX_VALUE + ), + ActionListener.wrap(r -> listener.onResponse(r.getResults()), e -> { + Throwable unwrapped = ExceptionsHelper.unwrapCause(e); + if (unwrapped instanceof ElasticsearchStatusException) { + ElasticsearchStatusException ex = (ElasticsearchStatusException) unwrapped; + if (ex.status().equals(RestStatus.TOO_MANY_REQUESTS)) { + listener.onFailure(ex); } else { - listener.onResponse(new WarningInferenceResults(e.getMessage())); + listener.onResponse(new WarningInferenceResults(ex.getMessage())); } + } else { + listener.onResponse(new WarningInferenceResults(e.getMessage())); } - ) + }) ); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportIsolateDatafeedAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportIsolateDatafeedAction.java index 303607f55e791..268cc51afe5fc 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportIsolateDatafeedAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportIsolateDatafeedAction.java @@ -23,13 +23,24 @@ import java.util.List; -public class TransportIsolateDatafeedAction extends TransportTasksAction { +public class TransportIsolateDatafeedAction extends TransportTasksAction< + TransportStartDatafeedAction.DatafeedTask, + IsolateDatafeedAction.Request, + IsolateDatafeedAction.Response, + IsolateDatafeedAction.Response> { @Inject public TransportIsolateDatafeedAction(TransportService transportService, ActionFilters actionFilters, ClusterService clusterService) { - super(IsolateDatafeedAction.NAME, clusterService, transportService, actionFilters, IsolateDatafeedAction.Request::new, - IsolateDatafeedAction.Response::new, IsolateDatafeedAction.Response::new, MachineLearning.UTILITY_THREAD_POOL_NAME); + super( + IsolateDatafeedAction.NAME, + clusterService, + transportService, + actionFilters, + IsolateDatafeedAction.Request::new, + IsolateDatafeedAction.Response::new, + IsolateDatafeedAction.Response::new, + MachineLearning.UTILITY_THREAD_POOL_NAME + ); } @Override @@ -49,19 +60,20 @@ protected void doExecute(Task task, IsolateDatafeedAction.Request request, Actio } @Override - protected IsolateDatafeedAction.Response newResponse(IsolateDatafeedAction.Request request, List tasks, - List taskOperationFailures, - List failedNodeExceptions) { + protected IsolateDatafeedAction.Response newResponse( + IsolateDatafeedAction.Request request, + List tasks, + List taskOperationFailures, + List failedNodeExceptions + ) { // We only let people isolate one datafeed at a time, so each list will be empty or contain one item assert tasks.size() <= 1 : "more than 1 item in tasks: " + tasks.size(); assert taskOperationFailures.size() <= 1 : "more than 1 item in taskOperationFailures: " + taskOperationFailures.size(); assert failedNodeExceptions.size() <= 1 : "more than 1 item in failedNodeExceptions: " + failedNodeExceptions.size(); if (taskOperationFailures.isEmpty() == false) { - throw org.elasticsearch.ExceptionsHelper - .convertToElastic(taskOperationFailures.get(0).getCause()); + throw org.elasticsearch.ExceptionsHelper.convertToElastic(taskOperationFailures.get(0).getCause()); } else if (failedNodeExceptions.isEmpty() == false) { - throw org.elasticsearch.ExceptionsHelper - .convertToElastic(failedNodeExceptions.get(0)); + throw org.elasticsearch.ExceptionsHelper.convertToElastic(failedNodeExceptions.get(0)); } else if (tasks.isEmpty() == false) { return tasks.get(0); } @@ -69,8 +81,11 @@ protected IsolateDatafeedAction.Response newResponse(IsolateDatafeedAction.Reque } @Override - protected void taskOperation(IsolateDatafeedAction.Request request, TransportStartDatafeedAction.DatafeedTask datafeedTask, - ActionListener listener) { + protected void taskOperation( + IsolateDatafeedAction.Request request, + TransportStartDatafeedAction.DatafeedTask datafeedTask, + ActionListener listener + ) { datafeedTask.isolate(); listener.onResponse(new IsolateDatafeedAction.Response(true)); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportJobTaskAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportJobTaskAction.java index 0eba7484c4f84..91494d9e11b5e 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportJobTaskAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportJobTaskAction.java @@ -30,18 +30,22 @@ */ // TODO: Hacking around here with TransportTasksAction. Ideally we should have another base class in core that // redirects to a single node only -public abstract class TransportJobTaskAction, - Response extends BaseTasksResponse & Writeable> - extends TransportTasksAction { +public abstract class TransportJobTaskAction, Response extends BaseTasksResponse & Writeable> + extends TransportTasksAction { protected final AutodetectProcessManager processManager; - TransportJobTaskAction(String actionName, ClusterService clusterService, - TransportService transportService, ActionFilters actionFilters, - Writeable.Reader requestReader, Writeable.Reader responseReader, - String nodeExecutor, AutodetectProcessManager processManager) { - super(actionName, clusterService, transportService, actionFilters, - requestReader, responseReader, responseReader, nodeExecutor); + TransportJobTaskAction( + String actionName, + ClusterService clusterService, + TransportService transportService, + ActionFilters actionFilters, + Writeable.Reader requestReader, + Writeable.Reader responseReader, + String nodeExecutor, + AutodetectProcessManager processManager + ) { + super(actionName, clusterService, transportService, actionFilters, requestReader, responseReader, responseReader, nodeExecutor); this.processManager = processManager; } @@ -62,15 +66,21 @@ protected void doExecute(Task task, Request request, ActionListener li } @Override - protected Response newResponse(Request request, List tasks, List taskOperationFailures, - List failedNodeExceptions) { + protected Response newResponse( + Request request, + List tasks, + List taskOperationFailures, + List failedNodeExceptions + ) { return selectFirst(tasks, taskOperationFailures, failedNodeExceptions); } - static Response selectFirst(List tasks, - List taskOperationFailures, - List failedNodeExceptions) { + static Response selectFirst( + List tasks, + List taskOperationFailures, + List failedNodeExceptions + ) { // no need to accumulate sub responses, since we only perform an operation on one task only // not ideal, but throwing exceptions here works, because higher up the stack there is a try-catch block delegating to // the actionlistener's onFailure @@ -84,8 +94,7 @@ static Response selectFirst(List } } else { if (tasks.size() > 1) { - throw new IllegalStateException( - "Expected one node level response, but got [" + tasks.size() + "]"); + throw new IllegalStateException("Expected one node level response, but got [" + tasks.size() + "]"); } return tasks.get(0); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportKillProcessAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportKillProcessAction.java index 44dfa4d2e47f1..d49675855360c 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportKillProcessAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportKillProcessAction.java @@ -32,7 +32,8 @@ import java.util.List; import java.util.stream.Collectors; -public class TransportKillProcessAction extends TransportTasksAction { @@ -42,20 +43,32 @@ public class TransportKillProcessAction extends TransportTasksAction tasks, - List taskOperationFailures, - List failedNodeExceptions) { + protected KillProcessAction.Response newResponse( + KillProcessAction.Request request, + List tasks, + List taskOperationFailures, + List failedNodeExceptions + ) { org.elasticsearch.ExceptionsHelper.rethrowAndSuppress( taskOperationFailures.stream() .map(t -> org.elasticsearch.ExceptionsHelper.convertToElastic(t.getCause())) @@ -83,10 +96,7 @@ protected void doExecute(Task task, KillProcessAction.Request request, ActionLis PersistentTasksCustomMetadata tasks = clusterService.state().getMetadata().custom(PersistentTasksCustomMetadata.TYPE); List> jobTasks; if (Strings.isAllOrWildcard(request.getJobId())) { - jobTasks = MlTasks.openJobTasks(tasks) - .stream() - .filter(t -> t.getExecutorNode() != null) - .collect(Collectors.toList()); + jobTasks = MlTasks.openJobTasks(tasks).stream().filter(t -> t.getExecutorNode() != null).collect(Collectors.toList()); } else { PersistentTasksCustomMetadata.PersistentTask jobTask = MlTasks.getJobTask(request.getJobId(), tasks); @@ -103,14 +113,21 @@ protected void doExecute(Task task, KillProcessAction.Request request, ActionLis return; } if (jobTasks.stream().allMatch(t -> nodes.get(t.getExecutorNode()) == null)) { - listener.onFailure(ExceptionsHelper.conflictStatusException("Cannot kill process for job {} as" + - "executor node {} cannot be found", request.getJobId(), jobTasks.get(0).getExecutorNode())); + listener.onFailure( + ExceptionsHelper.conflictStatusException( + "Cannot kill process for job {} as" + "executor node {} cannot be found", + request.getJobId(), + jobTasks.get(0).getExecutorNode() + ) + ); return; } - request.setNodes(jobTasks.stream() - .filter(t -> t.getExecutorNode() != null && nodes.get(t.getExecutorNode()) != null) - .map(PersistentTasksCustomMetadata.PersistentTask::getExecutorNode) - .toArray(String[]::new)); + request.setNodes( + jobTasks.stream() + .filter(t -> t.getExecutorNode() != null && nodes.get(t.getExecutorNode()) != null) + .map(PersistentTasksCustomMetadata.PersistentTask::getExecutorNode) + .toArray(String[]::new) + ); super.doExecute(task, request, listener); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportMlInfoAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportMlInfoAction.java index 5fd68664dce27..8c3a6097f0134 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportMlInfoAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportMlInfoAction.java @@ -18,9 +18,9 @@ import org.elasticsearch.common.settings.ClusterSettings; import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.tasks.Task; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.ml.MachineLearningField; import org.elasticsearch.xpack.core.ml.MlMetadata; import org.elasticsearch.xpack.core.ml.action.MlInfoAction; @@ -55,8 +55,13 @@ public class TransportMlInfoAction extends HandledTransportAction nativeCodeInfo; @Inject - public TransportMlInfoAction(TransportService transportService, ActionFilters actionFilters, ClusterService clusterService, - NamedXContentRegistry xContentRegistry, MlControllerHolder mlControllerHolder) { + public TransportMlInfoAction( + TransportService transportService, + ActionFilters actionFilters, + ClusterService clusterService, + NamedXContentRegistry xContentRegistry, + MlControllerHolder mlControllerHolder + ) { super(MlInfoAction.NAME, transportService, actionFilters, MlInfoAction.Request::new); this.clusterService = clusterService; this.xContentRegistry = xContentRegistry; @@ -94,12 +99,17 @@ private Map anomalyDetectorsDefaults() { defaults.put(AnalysisLimits.MODEL_MEMORY_LIMIT.getPreferredName(), defaultModelMemoryLimit()); defaults.put(AnalysisLimits.CATEGORIZATION_EXAMPLES_LIMIT.getPreferredName(), AnalysisLimits.DEFAULT_CATEGORIZATION_EXAMPLES_LIMIT); defaults.put(Job.MODEL_SNAPSHOT_RETENTION_DAYS.getPreferredName(), Job.DEFAULT_MODEL_SNAPSHOT_RETENTION_DAYS); - defaults.put(Job.DAILY_MODEL_SNAPSHOT_RETENTION_AFTER_DAYS.getPreferredName(), - Job.DEFAULT_DAILY_MODEL_SNAPSHOT_RETENTION_AFTER_DAYS); + defaults.put( + Job.DAILY_MODEL_SNAPSHOT_RETENTION_AFTER_DAYS.getPreferredName(), + Job.DEFAULT_DAILY_MODEL_SNAPSHOT_RETENTION_AFTER_DAYS + ); try { - defaults.put(CategorizationAnalyzerConfig.CATEGORIZATION_ANALYZER.getPreferredName(), + defaults.put( + CategorizationAnalyzerConfig.CATEGORIZATION_ANALYZER.getPreferredName(), CategorizationAnalyzerConfig.buildStandardCategorizationAnalyzer(Collections.emptyList()) - .asMap(xContentRegistry).get(CategorizationAnalyzerConfig.CATEGORIZATION_ANALYZER.getPreferredName())); + .asMap(xContentRegistry) + .get(CategorizationAnalyzerConfig.CATEGORIZATION_ANALYZER.getPreferredName()) + ); } catch (IOException e) { logger.error("failed to convert default categorization analyzer to map", e); } @@ -109,8 +119,7 @@ private Map anomalyDetectorsDefaults() { private ByteSizeValue defaultModelMemoryLimit() { ByteSizeValue defaultLimit = ByteSizeValue.ofMb(AnalysisLimits.DEFAULT_MODEL_MEMORY_LIMIT_MB); ByteSizeValue maxModelMemoryLimit = clusterService.getClusterSettings().get(MachineLearningField.MAX_MODEL_MEMORY_LIMIT); - if (maxModelMemoryLimit != null && maxModelMemoryLimit.getBytes() > 0 - && maxModelMemoryLimit.getBytes() < defaultLimit.getBytes()) { + if (maxModelMemoryLimit != null && maxModelMemoryLimit.getBytes() > 0 && maxModelMemoryLimit.getBytes() < defaultLimit.getBytes()) { return maxModelMemoryLimit; } return defaultLimit; @@ -158,10 +167,14 @@ static ByteSizeValue calculateEffectiveMaxModelMemoryLimit(ClusterSettings clust long maxMlNodeSize = clusterSettings.get(MAX_ML_NODE_SIZE).getBytes(); int maxLazyNodes = clusterSettings.get(MAX_LAZY_ML_NODES); if (maxMlNodeSize > 0 && numMlNodes < maxLazyNodes) { - maxMlMemory = Math.max(maxMlMemory, NativeMemoryCalculator.allowedBytesForMl( - maxMlNodeSize, - clusterSettings.get(MAX_MACHINE_MEMORY_PERCENT), - clusterSettings.get(USE_AUTO_MACHINE_MEMORY_PERCENT))); + maxMlMemory = Math.max( + maxMlMemory, + NativeMemoryCalculator.allowedBytesForMl( + maxMlNodeSize, + clusterSettings.get(MAX_MACHINE_MEMORY_PERCENT), + clusterSettings.get(USE_AUTO_MACHINE_MEMORY_PERCENT) + ) + ); } if (maxMlMemory <= 0) { @@ -180,7 +193,8 @@ private Map limits() { Map limits = new HashMap<>(); ByteSizeValue effectiveMaxModelMemoryLimit = calculateEffectiveMaxModelMemoryLimit( clusterService.getClusterSettings(), - clusterService.state().getNodes()); + clusterService.state().getNodes() + ); ByteSizeValue maxModelMemoryLimit = clusterService.getClusterSettings().get(MachineLearningField.MAX_MODEL_MEMORY_LIMIT); if (maxModelMemoryLimit != null && maxModelMemoryLimit.getBytes() > 0) { limits.put("max_model_memory_limit", maxModelMemoryLimit.getStringRep()); @@ -191,8 +205,10 @@ private Map limits() { if (effectiveMaxModelMemoryLimit != null) { limits.put("effective_max_model_memory_limit", effectiveMaxModelMemoryLimit.getStringRep()); } - limits.put("total_ml_memory", - calculateTotalMlMemory(clusterService.getClusterSettings(), clusterService.state().getNodes()).getStringRep()); + limits.put( + "total_ml_memory", + calculateTotalMlMemory(clusterService.getClusterSettings(), clusterService.state().getNodes()).getStringRep() + ); return limits; } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportOpenJobAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportOpenJobAction.java index bbe43044652f3..d1f9c9e07d4c0 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportOpenJobAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportOpenJobAction.java @@ -80,13 +80,30 @@ public class TransportOpenJobAction extends TransportMasterNodeAction listener) { + protected void masterOperation( + Task task, + OpenJobAction.Request request, + ClusterState state, + ActionListener listener + ) { if (migrationEligibilityCheck.jobIsEligibleForMigration(request.getJobParams().getJobId(), state)) { listener.onFailure(ExceptionsHelper.configHasNotBeenMigrated("open job", request.getJobParams().getJobId())); return; @@ -115,20 +136,17 @@ protected void masterOperation(Task task, OpenJobAction.Request request, Cluster if (MachineLearningField.ML_API_FEATURE.check(licenseState)) { // Clear job finished time once the job is started and respond - ActionListener clearJobFinishTime = ActionListener.wrap( - response -> { - if (response.isAcknowledged()) { - clearJobFinishedTime(response, state, jobParams.getJobId(), request.masterNodeTimeout(), listener); - } else { - listener.onResponse(response); - } - }, - listener::onFailure - ); + ActionListener clearJobFinishTime = ActionListener.wrap(response -> { + if (response.isAcknowledged()) { + clearJobFinishedTime(response, state, jobParams.getJobId(), request.masterNodeTimeout(), listener); + } else { + listener.onResponse(response); + } + }, listener::onFailure); // Wait for job to be started - ActionListener> waitForJobToStart = - new ActionListener>() { + ActionListener> waitForJobToStart = new ActionListener< + PersistentTasksCustomMetadata.PersistentTask>() { @Override public void onResponse(PersistentTasksCustomMetadata.PersistentTask task) { waitForJobStarted(task.getId(), jobParams, clearJobFinishTime); @@ -141,7 +159,8 @@ public void onFailure(Exception e) { "Cannot open job [{}] because it has already been opened", RestStatus.CONFLICT, e, - jobParams.getJobId()); + jobParams.getJobId() + ); } listener.onFailure(e); } @@ -149,70 +168,67 @@ public void onFailure(Exception e) { // Start job task ActionListener memoryRequirementRefreshListener = ActionListener.wrap( - mem -> persistentTasksService.sendStartRequest(MlTasks.jobTaskId(jobParams.getJobId()), MlTasks.JOB_TASK_NAME, jobParams, - waitForJobToStart), + mem -> persistentTasksService.sendStartRequest( + MlTasks.jobTaskId(jobParams.getJobId()), + MlTasks.JOB_TASK_NAME, + jobParams, + waitForJobToStart + ), listener::onFailure ); // Tell the job tracker to refresh the memory requirement for this job and all other jobs that have persistent tasks ActionListener modelSnapshotValidationListener = ActionListener.wrap( - response -> memoryTracker.refreshAnomalyDetectorJobMemoryAndAllOthers(jobParams.getJobId(), - memoryRequirementRefreshListener), + response -> memoryTracker.refreshAnomalyDetectorJobMemoryAndAllOthers( + jobParams.getJobId(), + memoryRequirementRefreshListener + ), listener::onFailure ); // Validate the model snapshot is supported - ActionListener getJobHandler = ActionListener.wrap( - response -> { - if (jobParams.getJob().getModelSnapshotId() == null) { - modelSnapshotValidationListener.onResponse(true); - return; - } - client.execute( - GetModelSnapshotsAction.INSTANCE, - new GetModelSnapshotsAction.Request(jobParams.getJobId(), jobParams.getJob().getModelSnapshotId()), - ActionListener.wrap( - modelSnapshot -> { - if (modelSnapshot.getPage().results().isEmpty()) { - modelSnapshotValidationListener.onResponse(true); - return; - } - assert modelSnapshot.getPage().results().size() == 1; - if (modelSnapshot.getPage().results().get(0).getMinVersion().onOrAfter(MIN_SUPPORTED_SNAPSHOT_VERSION)) { - modelSnapshotValidationListener.onResponse(true); - return; - } - listener.onFailure( - ExceptionsHelper.serverError( - "[{}] job snapshot [{}] has min version before [{}], " + - "please revert to a newer model snapshot or reset the job", - jobParams.getJobId(), - jobParams.getJob().getModelSnapshotId(), - MIN_SUPPORTED_SNAPSHOT_VERSION.toString() - ) - ); - }, - failure -> { - if (ExceptionsHelper.unwrapCause(failure) instanceof ResourceNotFoundException) { - modelSnapshotValidationListener.onResponse(true); - return; - } - listener.onFailure(ExceptionsHelper.serverError("Unable to validate model snapshot", failure)); - } - ) - ); - }, - listener::onFailure - ); + ActionListener getJobHandler = ActionListener.wrap(response -> { + if (jobParams.getJob().getModelSnapshotId() == null) { + modelSnapshotValidationListener.onResponse(true); + return; + } + client.execute( + GetModelSnapshotsAction.INSTANCE, + new GetModelSnapshotsAction.Request(jobParams.getJobId(), jobParams.getJob().getModelSnapshotId()), + ActionListener.wrap(modelSnapshot -> { + if (modelSnapshot.getPage().results().isEmpty()) { + modelSnapshotValidationListener.onResponse(true); + return; + } + assert modelSnapshot.getPage().results().size() == 1; + if (modelSnapshot.getPage().results().get(0).getMinVersion().onOrAfter(MIN_SUPPORTED_SNAPSHOT_VERSION)) { + modelSnapshotValidationListener.onResponse(true); + return; + } + listener.onFailure( + ExceptionsHelper.serverError( + "[{}] job snapshot [{}] has min version before [{}], " + + "please revert to a newer model snapshot or reset the job", + jobParams.getJobId(), + jobParams.getJob().getModelSnapshotId(), + MIN_SUPPORTED_SNAPSHOT_VERSION.toString() + ) + ); + }, failure -> { + if (ExceptionsHelper.unwrapCause(failure) instanceof ResourceNotFoundException) { + modelSnapshotValidationListener.onResponse(true); + return; + } + listener.onFailure(ExceptionsHelper.serverError("Unable to validate model snapshot", failure)); + }) + ); + }, listener::onFailure); // Get the job config - jobConfigProvider.getJob(jobParams.getJobId(), ActionListener.wrap( - builder -> { - jobParams.setJob(builder.build()); - getJobHandler.onResponse(null); - }, - listener::onFailure - )); + jobConfigProvider.getJob(jobParams.getJobId(), ActionListener.wrap(builder -> { + jobParams.setJob(builder.build()); + getJobHandler.onResponse(null); + }, listener::onFailure)); } else { listener.onFailure(LicenseUtils.newComplianceException(XPackField.MACHINE_LEARNING)); } @@ -220,49 +236,52 @@ public void onFailure(Exception e) { private void waitForJobStarted(String taskId, OpenJobAction.JobParams jobParams, ActionListener listener) { JobPredicate predicate = new JobPredicate(); - persistentTasksService.waitForPersistentTaskCondition(taskId, predicate, jobParams.getTimeout(), - new PersistentTasksService.WaitForPersistentTaskListener() { - @Override - public void onResponse(PersistentTasksCustomMetadata.PersistentTask persistentTask) { - if (predicate.exception != null) { - if (predicate.shouldCancel) { - // We want to return to the caller without leaving an unassigned persistent task, to match - // what would have happened if the error had been detected in the "fast fail" validation - cancelJobStart(persistentTask, predicate.exception, listener); + persistentTasksService.waitForPersistentTaskCondition( + taskId, + predicate, + jobParams.getTimeout(), + new PersistentTasksService.WaitForPersistentTaskListener() { + @Override + public void onResponse(PersistentTasksCustomMetadata.PersistentTask persistentTask) { + if (predicate.exception != null) { + if (predicate.shouldCancel) { + // We want to return to the caller without leaving an unassigned persistent task, to match + // what would have happened if the error had been detected in the "fast fail" validation + cancelJobStart(persistentTask, predicate.exception, listener); + } else { + listener.onFailure(predicate.exception); + } } else { - listener.onFailure(predicate.exception); + listener.onResponse(new NodeAcknowledgedResponse(true, predicate.node)); } - } else { - listener.onResponse(new NodeAcknowledgedResponse(true, predicate.node)); } - } - @Override - public void onFailure(Exception e) { - listener.onFailure(e); - } + @Override + public void onFailure(Exception e) { + listener.onFailure(e); + } - @Override - public void onTimeout(TimeValue timeout) { - listener.onFailure(new ElasticsearchException("Opening job [{}] timed out after [{}]", jobParams.getJob(), timeout)); + @Override + public void onTimeout(TimeValue timeout) { + listener.onFailure(new ElasticsearchException("Opening job [{}] timed out after [{}]", jobParams.getJob(), timeout)); + } } - }); + ); } - private void clearJobFinishedTime(NodeAcknowledgedResponse response, - ClusterState clusterState, - String jobId, - TimeValue masterNodeTimeout, - ActionListener listener) { + private void clearJobFinishedTime( + NodeAcknowledgedResponse response, + ClusterState clusterState, + String jobId, + TimeValue masterNodeTimeout, + ActionListener listener + ) { final JobUpdate update = new JobUpdate.Builder(jobId).setClearFinishTime(true).build(); - ActionListener clearedTimeListener = ActionListener.wrap( - job -> listener.onResponse(response), - e -> { - logger.error(new ParameterizedMessage("[{}] Failed to clear finished_time", jobId), e); - // Not a critical error so continue - listener.onResponse(response); - } - ); + ActionListener clearedTimeListener = ActionListener.wrap(job -> listener.onResponse(response), e -> { + logger.error(new ParameterizedMessage("[{}] Failed to clear finished_time", jobId), e); + // Not a critical error so continue + listener.onResponse(response); + }); ActionListener mappingsUpdatedListener = ActionListener.wrap( mappingUpdateResponse -> jobConfigProvider.updateJob(jobId, update, null, clearedTimeListener), e -> { @@ -277,31 +296,38 @@ private void clearJobFinishedTime(NodeAcknowledgedResponse response, client, clusterState, masterNodeTimeout, - mappingsUpdatedListener); + mappingsUpdatedListener + ); } - private void cancelJobStart(PersistentTasksCustomMetadata.PersistentTask persistentTask, Exception exception, - ActionListener listener) { - persistentTasksService.sendRemoveRequest(persistentTask.getId(), - new ActionListener>() { - @Override - public void onResponse(PersistentTasksCustomMetadata.PersistentTask task) { - // We succeeded in cancelling the persistent task, but the - // problem that caused us to cancel it is the overall result - listener.onFailure(exception); - } + private void cancelJobStart( + PersistentTasksCustomMetadata.PersistentTask persistentTask, + Exception exception, + ActionListener listener + ) { + persistentTasksService.sendRemoveRequest( + persistentTask.getId(), + new ActionListener>() { + @Override + public void onResponse(PersistentTasksCustomMetadata.PersistentTask task) { + // We succeeded in cancelling the persistent task, but the + // problem that caused us to cancel it is the overall result + listener.onFailure(exception); + } - @Override - public void onFailure(Exception e) { - logger.error( - () -> new ParameterizedMessage( - "[{}] Failed to cancel persistent task that could not be assigned due to [{}]", - persistentTask.getParams().getJobId(), - exception.getMessage()), - e); - listener.onFailure(exception); - } + @Override + public void onFailure(Exception e) { + logger.error( + () -> new ParameterizedMessage( + "[{}] Failed to cancel persistent task that could not be assigned due to [{}]", + persistentTask.getParams().getJobId(), + exception.getMessage() + ), + e + ); + listener.onFailure(exception); } + } ); } @@ -350,7 +376,7 @@ public boolean test(PersistentTasksCustomMetadata.PersistentTask persistentTa switch (jobState) { // The OPENING case here is expected to be incredibly short-lived, just occurring during the // time period when a job has successfully been assigned to a node but the request to update - // its task state is still in-flight. (The long-lived OPENING case when a lazy node needs to + // its task state is still in-flight. (The long-lived OPENING case when a lazy node needs to // be added to the cluster to accommodate the job was dealt with higher up this method when the // magic AWAITING_LAZY_ASSIGNMENT assignment was checked for.) case OPENING: @@ -363,7 +389,8 @@ public boolean test(PersistentTasksCustomMetadata.PersistentTask persistentTa exception = ExceptionsHelper.conflictStatusException( "The job has been {} while waiting to be {}", JobState.CLOSED, - JobState.OPENED); + JobState.OPENED + ); return true; case FAILED: default: diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPersistJobAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPersistJobAction.java index 3f4647c3480ed..c6442b353e7cc 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPersistJobAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPersistJobAction.java @@ -19,10 +19,22 @@ public class TransportPersistJobAction extends TransportJobTaskAction { @Inject - public TransportPersistJobAction(TransportService transportService, ClusterService clusterService, ActionFilters actionFilters, - AutodetectProcessManager processManager) { - super(PersistJobAction.NAME, clusterService, transportService, actionFilters, - PersistJobAction.Request::new, PersistJobAction.Response::new, ThreadPool.Names.SAME, processManager); + public TransportPersistJobAction( + TransportService transportService, + ClusterService clusterService, + ActionFilters actionFilters, + AutodetectProcessManager processManager + ) { + super( + PersistJobAction.NAME, + clusterService, + transportService, + actionFilters, + PersistJobAction.Request::new, + PersistJobAction.Response::new, + ThreadPool.Names.SAME, + processManager + ); // ThreadPool.Names.SAME, because operations is executed by autodetect worker thread } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPostCalendarEventsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPostCalendarEventsAction.java index 291c248855a3a..f648a3b123636 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPostCalendarEventsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPostCalendarEventsAction.java @@ -16,11 +16,11 @@ import org.elasticsearch.action.support.WriteRequest; import org.elasticsearch.client.Client; import org.elasticsearch.common.inject.Inject; +import org.elasticsearch.tasks.Task; +import org.elasticsearch.transport.TransportService; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.tasks.Task; -import org.elasticsearch.transport.TransportService; import org.elasticsearch.xpack.core.ml.MlMetaIndex; import org.elasticsearch.xpack.core.ml.action.PostCalendarEventsAction; import org.elasticsearch.xpack.core.ml.calendars.Calendar; @@ -37,16 +37,22 @@ import static org.elasticsearch.xpack.core.ClientHelper.ML_ORIGIN; import static org.elasticsearch.xpack.core.ClientHelper.executeAsyncWithOrigin; -public class TransportPostCalendarEventsAction extends HandledTransportAction { +public class TransportPostCalendarEventsAction extends HandledTransportAction< + PostCalendarEventsAction.Request, + PostCalendarEventsAction.Response> { private final Client client; private final JobResultsProvider jobResultsProvider; private final JobManager jobManager; @Inject - public TransportPostCalendarEventsAction(TransportService transportService, ActionFilters actionFilters, Client client, - JobResultsProvider jobResultsProvider, JobManager jobManager) { + public TransportPostCalendarEventsAction( + TransportService transportService, + ActionFilters actionFilters, + Client client, + JobResultsProvider jobResultsProvider, + JobManager jobManager + ) { super(PostCalendarEventsAction.NAME, transportService, actionFilters, PostCalendarEventsAction.Request::new); this.client = client; this.jobResultsProvider = jobResultsProvider; @@ -54,45 +60,57 @@ public TransportPostCalendarEventsAction(TransportService transportService, Acti } @Override - protected void doExecute(Task task, PostCalendarEventsAction.Request request, - ActionListener listener) { + protected void doExecute( + Task task, + PostCalendarEventsAction.Request request, + ActionListener listener + ) { List events = request.getScheduledEvents(); - ActionListener calendarListener = ActionListener.wrap( - calendar -> { - BulkRequestBuilder bulkRequestBuilder = client.prepareBulk(); + ActionListener calendarListener = ActionListener.wrap(calendar -> { + BulkRequestBuilder bulkRequestBuilder = client.prepareBulk(); - for (ScheduledEvent event: events) { - IndexRequest indexRequest = new IndexRequest(MlMetaIndex.indexName()); - try (XContentBuilder builder = XContentFactory.jsonBuilder()) { - indexRequest.source(event.toXContent(builder, - new ToXContent.MapParams(Collections.singletonMap(ToXContentParams.FOR_INTERNAL_STORAGE, - "true")))); - } catch (IOException e) { - throw new IllegalStateException("Failed to serialise event", e); - } - bulkRequestBuilder.add(indexRequest); - } + for (ScheduledEvent event : events) { + IndexRequest indexRequest = new IndexRequest(MlMetaIndex.indexName()); + try (XContentBuilder builder = XContentFactory.jsonBuilder()) { + indexRequest.source( + event.toXContent( + builder, + new ToXContent.MapParams(Collections.singletonMap(ToXContentParams.FOR_INTERNAL_STORAGE, "true")) + ) + ); + } catch (IOException e) { + throw new IllegalStateException("Failed to serialise event", e); + } + bulkRequestBuilder.add(indexRequest); + } - bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); + bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - executeAsyncWithOrigin(client, ML_ORIGIN, BulkAction.INSTANCE, bulkRequestBuilder.request(), - new ActionListener() { - @Override - public void onResponse(BulkResponse response) { - jobManager.updateProcessOnCalendarChanged(calendar.getJobIds(), ActionListener.wrap( - r -> listener.onResponse(new PostCalendarEventsAction.Response(events)), - listener::onFailure - )); - } + executeAsyncWithOrigin( + client, + ML_ORIGIN, + BulkAction.INSTANCE, + bulkRequestBuilder.request(), + new ActionListener() { + @Override + public void onResponse(BulkResponse response) { + jobManager.updateProcessOnCalendarChanged( + calendar.getJobIds(), + ActionListener.wrap( + r -> listener.onResponse(new PostCalendarEventsAction.Response(events)), + listener::onFailure + ) + ); + } - @Override - public void onFailure(Exception e) { - listener.onFailure(ExceptionsHelper.serverError("Error indexing event", e)); - } - }); - }, - listener::onFailure); + @Override + public void onFailure(Exception e) { + listener.onFailure(ExceptionsHelper.serverError("Error indexing event", e)); + } + } + ); + }, listener::onFailure); jobResultsProvider.calendar(request.getCalendarId(), calendarListener); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPostDataAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPostDataAction.java index 980dac5dca0f1..3b61c8b516d5f 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPostDataAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPostDataAction.java @@ -27,10 +27,23 @@ public class TransportPostDataAction extends TransportJobTaskAction { + processManager.processData(task, analysisRegistry, contentStream, request.getXContentType(), params, (dataCounts, e) -> { if (dataCounts != null) { listener.onResponse(new PostDataAction.Response(dataCounts)); } else { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPreviewDataFrameAnalyticsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPreviewDataFrameAnalyticsAction.java index a4ec0f62609e8..89c5493bf230c 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPreviewDataFrameAnalyticsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPreviewDataFrameAnalyticsAction.java @@ -107,13 +107,10 @@ void preview(Task task, DataFrameAnalyticsConfig config, ActionListener { - List fieldNames = extractor.getFieldNames(); - listener.onResponse(new Response(rows.stream().map((r) -> mergeRow(r, fieldNames)).collect(Collectors.toList()))); - }, - listener::onFailure - )); + extractor.preview(ActionListener.wrap(rows -> { + List fieldNames = extractor.getFieldNames(); + listener.onResponse(new Response(rows.stream().map((r) -> mergeRow(r, fieldNames)).collect(Collectors.toList()))); + }, listener::onFailure)); }, listener::onFailure)); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPreviewDatafeedAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPreviewDatafeedAction.java index 5e5fd5d73cc4c..c7d40b14e51a9 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPreviewDatafeedAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPreviewDatafeedAction.java @@ -17,11 +17,11 @@ import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.time.DateUtils; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.index.mapper.DateFieldMapper; import org.elasticsearch.tasks.Task; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.xpack.core.ml.action.PreviewDatafeedAction; import org.elasticsearch.xpack.core.ml.datafeed.ChunkingConfig; @@ -59,48 +59,50 @@ public class TransportPreviewDatafeedAction extends HandledTransportAction listener) { - ActionListener datafeedConfigActionListener = ActionListener.wrap( - datafeedConfig -> { - if (request.getJobConfig() != null) { - previewDatafeed(datafeedConfig, request.getJobConfig().build(new Date()), listener); - return; - } - jobConfigProvider.getJob(datafeedConfig.getJobId(), ActionListener.wrap( - jobBuilder -> previewDatafeed(datafeedConfig, jobBuilder.build(), listener), - listener::onFailure)); - }, - listener::onFailure - ); + ActionListener datafeedConfigActionListener = ActionListener.wrap(datafeedConfig -> { + if (request.getJobConfig() != null) { + previewDatafeed(datafeedConfig, request.getJobConfig().build(new Date()), listener); + return; + } + jobConfigProvider.getJob( + datafeedConfig.getJobId(), + ActionListener.wrap(jobBuilder -> previewDatafeed(datafeedConfig, jobBuilder.build(), listener), listener::onFailure) + ); + }, listener::onFailure); if (request.getDatafeedConfig() != null) { datafeedConfigActionListener.onResponse(request.getDatafeedConfig()); } else { datafeedConfigProvider.getDatafeedConfig( request.getDatafeedId(), - ActionListener.wrap(builder -> datafeedConfigActionListener.onResponse(builder.build()), listener::onFailure)); + ActionListener.wrap(builder -> datafeedConfigActionListener.onResponse(builder.build()), listener::onFailure) + ); } } - private void previewDatafeed( - DatafeedConfig datafeedConfig, - Job job, - ActionListener listener - ) { + private void previewDatafeed(DatafeedConfig datafeedConfig, Job job, ActionListener listener) { DatafeedConfig.Builder previewDatafeedBuilder = buildPreviewDatafeed(datafeedConfig); useSecondaryAuthIfAvailable(securityContext, () -> { previewDatafeedBuilder.setHeaders(filterSecurityHeaders(threadPool.getThreadContext().getHeaders())); @@ -116,13 +118,19 @@ private void previewDatafeed( // Fake DatafeedTimingStatsReporter that does not have access to results index new DatafeedTimingStatsReporter(new DatafeedTimingStats(datafeedConfig.getJobId()), (ts, refreshPolicy) -> {}), listener.delegateFailure((l, dataExtractorFactory) -> { - isDateNanos(previewDatafeedConfig.getHeaders(), job.getDataDescription().getTimeField(), + isDateNanos( + previewDatafeedConfig.getHeaders(), + job.getDataDescription().getTimeField(), listener.delegateFailure((l2, isDateNanos) -> { - DataExtractor dataExtractor = dataExtractorFactory.newExtractor(0, - isDateNanos ? DateUtils.MAX_NANOSECOND_INSTANT.toEpochMilli() : Long.MAX_VALUE); + DataExtractor dataExtractor = dataExtractorFactory.newExtractor( + 0, + isDateNanos ? DateUtils.MAX_NANOSECOND_INSTANT.toEpochMilli() : Long.MAX_VALUE + ); threadPool.generic().execute(() -> previewDatafeed(dataExtractor, l)); - })); - })); + }) + ); + }) + ); }); } @@ -150,13 +158,10 @@ private void isDateNanos(Map headers, String timeField, ActionLi client, FieldCapabilitiesAction.INSTANCE, new FieldCapabilitiesRequest().fields(timeField), - ActionListener.wrap( - fieldCapsResponse -> { - Map timeFieldCaps = fieldCapsResponse.getField(timeField); - listener.onResponse(timeFieldCaps.keySet().contains(DateFieldMapper.DATE_NANOS_CONTENT_TYPE)); - }, - listener::onFailure - ) + ActionListener.wrap(fieldCapsResponse -> { + Map timeFieldCaps = fieldCapsResponse.getField(timeField); + listener.onResponse(timeFieldCaps.keySet().contains(DateFieldMapper.DATE_NANOS_CONTENT_TYPE)); + }, listener::onFailure) ); } @@ -177,8 +182,9 @@ static void previewDatafeed(DataExtractor dataExtractor, ActionListener() { - @Override - public void onResponse(IndexResponse indexResponse) { - listener.onResponse(new PutCalendarAction.Response(calendar)); - } + executeAsyncWithOrigin(client, ML_ORIGIN, IndexAction.INSTANCE, indexRequest, new ActionListener() { + @Override + public void onResponse(IndexResponse indexResponse) { + listener.onResponse(new PutCalendarAction.Response(calendar)); + } - @Override - public void onFailure(Exception e) { - if (ExceptionsHelper.unwrapCause(e) instanceof VersionConflictEngineException) { - listener.onFailure(ExceptionsHelper.badRequestException("Cannot create calendar with id [" + - calendar.getId() + "] as it already exists")); - } else { - listener.onFailure( - ExceptionsHelper.serverError("Error putting calendar with id [" + calendar.getId() + "]", e)); - } - } - }); + @Override + public void onFailure(Exception e) { + if (ExceptionsHelper.unwrapCause(e) instanceof VersionConflictEngineException) { + listener.onFailure( + ExceptionsHelper.badRequestException( + "Cannot create calendar with id [" + calendar.getId() + "] as it already exists" + ) + ); + } else { + listener.onFailure(ExceptionsHelper.serverError("Error putting calendar with id [" + calendar.getId() + "]", e)); + } + } + }); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPutDataFrameAnalyticsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPutDataFrameAnalyticsAction.java index e3813ce5b593f..e23aadaec2ac2 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPutDataFrameAnalyticsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPutDataFrameAnalyticsAction.java @@ -22,8 +22,6 @@ import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeValue; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.core.TimeValue; import org.elasticsearch.license.License; import org.elasticsearch.license.LicenseUtils; @@ -31,6 +29,8 @@ import org.elasticsearch.tasks.Task; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.XPackField; import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.xpack.core.common.validation.SourceDestValidator; @@ -58,8 +58,9 @@ import static org.elasticsearch.xpack.ml.utils.SecondaryAuthorizationUtils.useSecondaryAuthIfAvailable; -public class TransportPutDataFrameAnalyticsAction - extends TransportMasterNodeAction { +public class TransportPutDataFrameAnalyticsAction extends TransportMasterNodeAction< + PutDataFrameAnalyticsAction.Request, + PutDataFrameAnalyticsAction.Response> { private static final Logger logger = LogManager.getLogger(TransportPutDataFrameAnalyticsAction.class); @@ -74,17 +75,34 @@ public class TransportPutDataFrameAnalyticsAction private volatile ByteSizeValue maxModelMemoryLimit; @Inject - public TransportPutDataFrameAnalyticsAction(Settings settings, TransportService transportService, ActionFilters actionFilters, - XPackLicenseState licenseState, Client client, ThreadPool threadPool, - ClusterService clusterService, IndexNameExpressionResolver indexNameExpressionResolver, - DataFrameAnalyticsConfigProvider configProvider, DataFrameAnalyticsAuditor auditor) { - super(PutDataFrameAnalyticsAction.NAME, transportService, clusterService, threadPool, actionFilters, - PutDataFrameAnalyticsAction.Request::new, indexNameExpressionResolver, PutDataFrameAnalyticsAction.Response::new, - ThreadPool.Names.SAME); + public TransportPutDataFrameAnalyticsAction( + Settings settings, + TransportService transportService, + ActionFilters actionFilters, + XPackLicenseState licenseState, + Client client, + ThreadPool threadPool, + ClusterService clusterService, + IndexNameExpressionResolver indexNameExpressionResolver, + DataFrameAnalyticsConfigProvider configProvider, + DataFrameAnalyticsAuditor auditor + ) { + super( + PutDataFrameAnalyticsAction.NAME, + transportService, + clusterService, + threadPool, + actionFilters, + PutDataFrameAnalyticsAction.Request::new, + indexNameExpressionResolver, + PutDataFrameAnalyticsAction.Response::new, + ThreadPool.Names.SAME + ); this.licenseState = licenseState; this.configProvider = configProvider; - this.securityContext = XPackSettings.SECURITY_ENABLED.get(settings) ? - new SecurityContext(settings, threadPool.getThreadContext()) : null; + this.securityContext = XPackSettings.SECURITY_ENABLED.get(settings) + ? new SecurityContext(settings, threadPool.getThreadContext()) + : null; this.client = client; this.auditor = Objects.requireNonNull(auditor); this.settings = settings; @@ -113,8 +131,12 @@ protected ClusterBlockException checkBlock(PutDataFrameAnalyticsAction.Request r } @Override - protected void masterOperation(Task task, PutDataFrameAnalyticsAction.Request request, ClusterState state, - ActionListener listener) { + protected void masterOperation( + Task task, + PutDataFrameAnalyticsAction.Request request, + ClusterState state, + ActionListener listener + ) { final DataFrameAnalyticsConfig config = request.getConfig(); @@ -123,17 +145,24 @@ protected void masterOperation(Task task, PutDataFrameAnalyticsAction.Request re listener::onFailure ); - sourceDestValidator.validate(clusterService.state(), config.getSource().getIndex(), config.getDest().getIndex(), null, - SourceDestValidations.ALL_VALIDATIONS, sourceDestValidationListener); + sourceDestValidator.validate( + clusterService.state(), + config.getSource().getIndex(), + config.getDest().getIndex(), + null, + SourceDestValidations.ALL_VALIDATIONS, + sourceDestValidationListener + ); } - private void putValidatedConfig(DataFrameAnalyticsConfig config, TimeValue masterNodeTimeout, - ActionListener listener) { - DataFrameAnalyticsConfig preparedForPutConfig = - new DataFrameAnalyticsConfig.Builder(config, maxModelMemoryLimit) - .setCreateTime(Instant.now()) - .setVersion(Version.CURRENT) - .build(); + private void putValidatedConfig( + DataFrameAnalyticsConfig config, + TimeValue masterNodeTimeout, + ActionListener listener + ) { + DataFrameAnalyticsConfig preparedForPutConfig = new DataFrameAnalyticsConfig.Builder(config, maxModelMemoryLimit).setCreateTime( + Instant.now() + ).setVersion(Version.CURRENT).build(); if (XPackSettings.SECURITY_ENABLED.get(settings)) { useSecondaryAuthIfAvailable(securityContext, () -> { @@ -155,7 +184,8 @@ private void putValidatedConfig(DataFrameAnalyticsConfig config, TimeValue maste ActionListener privResponseListener = ActionListener.wrap( r -> handlePrivsResponse(username, preparedForPutConfig, r, masterNodeTimeout, listener), - listener::onFailure); + listener::onFailure + ); client.execute(HasPrivilegesAction.INSTANCE, privRequest, privResponseListener); }); @@ -167,14 +197,18 @@ private void putValidatedConfig(DataFrameAnalyticsConfig config, TimeValue maste ActionListener.wrap( unused -> listener.onResponse(new PutDataFrameAnalyticsAction.Response(preparedForPutConfig)), listener::onFailure - )); + ) + ); } } - private void handlePrivsResponse(String username, DataFrameAnalyticsConfig memoryCappedConfig, - HasPrivilegesResponse response, - TimeValue masterNodeTimeout, - ActionListener listener) throws IOException { + private void handlePrivsResponse( + String username, + DataFrameAnalyticsConfig memoryCappedConfig, + HasPrivilegesResponse response, + TimeValue masterNodeTimeout, + ActionListener listener + ) throws IOException { if (response.isCompleteMatch()) { updateDocMappingAndPutConfig( memoryCappedConfig, @@ -183,7 +217,8 @@ private void handlePrivsResponse(String username, DataFrameAnalyticsConfig memor ActionListener.wrap( unused -> listener.onResponse(new PutDataFrameAnalyticsAction.Response(memoryCappedConfig)), listener::onFailure - )); + ) + ); } else { XContentBuilder builder = JsonXContent.contentBuilder(); builder.startObject(); @@ -193,16 +228,23 @@ private void handlePrivsResponse(String username, DataFrameAnalyticsConfig memor } builder.endObject(); - listener.onFailure(Exceptions.authorizationError("Cannot create data frame analytics [{}]" + - " because user {} lacks permissions on the indices: {}", - memoryCappedConfig.getId(), username, Strings.toString(builder))); + listener.onFailure( + Exceptions.authorizationError( + "Cannot create data frame analytics [{}]" + " because user {} lacks permissions on the indices: {}", + memoryCappedConfig.getId(), + username, + Strings.toString(builder) + ) + ); } } - private void updateDocMappingAndPutConfig(DataFrameAnalyticsConfig config, - Map headers, - TimeValue masterNodeTimeout, - ActionListener listener) { + private void updateDocMappingAndPutConfig( + DataFrameAnalyticsConfig config, + Map headers, + TimeValue masterNodeTimeout, + ActionListener listener + ) { ClusterState clusterState = clusterService.state(); if (clusterState == null) { logger.warn("Cannot update doc mapping because clusterState == null"); @@ -215,21 +257,22 @@ private void updateDocMappingAndPutConfig(DataFrameAnalyticsConfig config, client, clusterState, masterNodeTimeout, - ActionListener.wrap( - unused -> configProvider.put(config, headers, masterNodeTimeout, ActionListener.wrap( - indexResponse -> { - auditor.info( - config.getId(), - Messages.getMessage(Messages.DATA_FRAME_ANALYTICS_AUDIT_CREATED, config.getAnalysis().getWriteableName())); - listener.onResponse(config); - }, - listener::onFailure)), - listener::onFailure)); + ActionListener.wrap(unused -> configProvider.put(config, headers, masterNodeTimeout, ActionListener.wrap(indexResponse -> { + auditor.info( + config.getId(), + Messages.getMessage(Messages.DATA_FRAME_ANALYTICS_AUDIT_CREATED, config.getAnalysis().getWriteableName()) + ); + listener.onResponse(config); + }, listener::onFailure)), listener::onFailure) + ); } @Override - protected void doExecute(Task task, PutDataFrameAnalyticsAction.Request request, - ActionListener listener) { + protected void doExecute( + Task task, + PutDataFrameAnalyticsAction.Request request, + ActionListener listener + ) { if (MachineLearningField.ML_API_FEATURE.check(licenseState)) { super.doExecute(task, request, listener); } else { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPutDatafeedAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPutDatafeedAction.java index 2728632573588..7d19e1f6fdd15 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPutDatafeedAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPutDatafeedAction.java @@ -28,7 +28,6 @@ import org.elasticsearch.xpack.core.security.SecurityContext; import org.elasticsearch.xpack.ml.datafeed.DatafeedManager; - public class TransportPutDatafeedAction extends TransportMasterNodeAction { private final XPackLicenseState licenseState; @@ -36,22 +35,41 @@ public class TransportPutDatafeedAction extends TransportMasterNodeAction listener) { + protected void masterOperation( + Task task, + PutDatafeedAction.Request request, + ClusterState state, + ActionListener listener + ) { datafeedManager.putDatafeed(request, state, licenseState, securityContext, threadPool, listener); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPutFilterAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPutFilterAction.java index 5bf20c047d517..5dc3a3e00680e 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPutFilterAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPutFilterAction.java @@ -17,12 +17,12 @@ import org.elasticsearch.action.support.WriteRequest; import org.elasticsearch.client.Client; import org.elasticsearch.common.inject.Inject; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.index.engine.VersionConflictEngineException; import org.elasticsearch.tasks.Task; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xpack.core.ml.MlMetaIndex; import org.elasticsearch.xpack.core.ml.action.PutFilterAction; import org.elasticsearch.xpack.core.ml.job.config.MlFilter; @@ -58,24 +58,22 @@ protected void doExecute(Task task, PutFilterAction.Request request, ActionListe throw new IllegalStateException("Failed to serialise filter with id [" + filter.getId() + "]", e); } - executeAsyncWithOrigin(client, ML_ORIGIN, IndexAction.INSTANCE, indexRequest, - new ActionListener() { - @Override - public void onResponse(IndexResponse indexResponse) { - listener.onResponse(new PutFilterAction.Response(filter)); - } + executeAsyncWithOrigin(client, ML_ORIGIN, IndexAction.INSTANCE, indexRequest, new ActionListener() { + @Override + public void onResponse(IndexResponse indexResponse) { + listener.onResponse(new PutFilterAction.Response(filter)); + } - @Override - public void onFailure(Exception e) { - Exception reportedException; - if (ExceptionsHelper.unwrapCause(e) instanceof VersionConflictEngineException) { - reportedException = new ResourceAlreadyExistsException("A filter with id [" + filter.getId() - + "] already exists"); - } else { - reportedException = ExceptionsHelper.serverError("Error putting filter with id [" + filter.getId() + "]", e); - } - listener.onFailure(reportedException); - } - }); + @Override + public void onFailure(Exception e) { + Exception reportedException; + if (ExceptionsHelper.unwrapCause(e) instanceof VersionConflictEngineException) { + reportedException = new ResourceAlreadyExistsException("A filter with id [" + filter.getId() + "] already exists"); + } else { + reportedException = ExceptionsHelper.serverError("Error putting filter with id [" + filter.getId() + "]", e); + } + listener.onFailure(reportedException); + } + }); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPutJobAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPutJobAction.java index 153f35aeb4c97..38686c4f5cf28 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPutJobAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPutJobAction.java @@ -45,65 +45,82 @@ public class TransportPutJobAction extends TransportMasterNodeAction listener) throws Exception { - jobManager.putJob(request, analysisRegistry, state, ActionListener.wrap( - jobCreated -> { - if (jobCreated.getResponse().getDatafeedConfig().isPresent() == false) { - listener.onResponse(jobCreated); - return; - } - datafeedManager.putDatafeed( - new PutDatafeedAction.Request(jobCreated.getResponse().getDatafeedConfig().get()), - // Use newer state from cluster service as the job creation may have created shared indexes - clusterService.state(), - licenseState, - securityContext, - threadPool, - ActionListener.wrap( - createdDatafeed -> listener.onResponse(jobCreated), - failed -> jobManager.deleteJob( - new DeleteJobAction.Request(request.getJobBuilder().getId()), - state, - ActionListener.wrap( - deleted -> listener.onFailure(failed), - deleteFailed -> { - logger.warn( - () -> new ParameterizedMessage( - "[{}] failed to cleanup job after datafeed creation failure", - request.getJobBuilder().getId() - ), - deleteFailed); - ElasticsearchException ex = new ElasticsearchException( - "failed to cleanup job after datafeed creation failure", - failed - ); - ex.addSuppressed(deleteFailed); - listener.onFailure(ex); - } - ) - ) - )); - }, - listener::onFailure - )); + protected void masterOperation( + Task task, + PutJobAction.Request request, + ClusterState state, + ActionListener listener + ) throws Exception { + jobManager.putJob(request, analysisRegistry, state, ActionListener.wrap(jobCreated -> { + if (jobCreated.getResponse().getDatafeedConfig().isPresent() == false) { + listener.onResponse(jobCreated); + return; + } + datafeedManager.putDatafeed( + new PutDatafeedAction.Request(jobCreated.getResponse().getDatafeedConfig().get()), + // Use newer state from cluster service as the job creation may have created shared indexes + clusterService.state(), + licenseState, + securityContext, + threadPool, + ActionListener.wrap( + createdDatafeed -> listener.onResponse(jobCreated), + failed -> jobManager.deleteJob( + new DeleteJobAction.Request(request.getJobBuilder().getId()), + state, + ActionListener.wrap(deleted -> listener.onFailure(failed), deleteFailed -> { + logger.warn( + () -> new ParameterizedMessage( + "[{}] failed to cleanup job after datafeed creation failure", + request.getJobBuilder().getId() + ), + deleteFailed + ); + ElasticsearchException ex = new ElasticsearchException( + "failed to cleanup job after datafeed creation failure", + failed + ); + ex.addSuppressed(deleteFailed); + listener.onFailure(ex); + }) + ) + ) + ); + }, listener::onFailure)); } @Override diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPutTrainedModelAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPutTrainedModelAction.java index 05056c081c481..f5187821093ae 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPutTrainedModelAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPutTrainedModelAction.java @@ -25,7 +25,6 @@ import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.unit.ByteSizeValue; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.license.License; @@ -36,6 +35,7 @@ import org.elasticsearch.tasks.Task; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.XPackField; import org.elasticsearch.xpack.core.ml.MachineLearningField; import org.elasticsearch.xpack.core.ml.action.PutTrainedModelAction; @@ -67,12 +67,28 @@ public class TransportPutTrainedModelAction extends TransportMasterNodeAction listener) { + protected void masterOperation( + Task task, + PutTrainedModelAction.Request request, + ClusterState state, + ActionListener listener + ) { TrainedModelConfig config = request.getTrainedModelConfig(); try { if (request.isDeferDefinitionDecompression() == false) { config.ensureParsedDefinition(xContentRegistry); } } catch (IOException ex) { - listener.onFailure(ExceptionsHelper.badRequestException("Failed to parse definition for [{}]", - ex, - config.getModelId())); + listener.onFailure(ExceptionsHelper.badRequestException("Failed to parse definition for [{}]", ex, config.getModelId())); return; } @@ -102,17 +118,20 @@ protected void masterOperation(Task task, try { config.getModelDefinition().getTrainedModel().validate(); } catch (ElasticsearchException ex) { - listener.onFailure(ExceptionsHelper.badRequestException("Definition for [{}] has validation failures.", - ex, - config.getModelId())); + listener.onFailure( + ExceptionsHelper.badRequestException("Definition for [{}] has validation failures.", ex, config.getModelId()) + ); return; } - TrainedModelType trainedModelType = - TrainedModelType.typeFromTrainedModel(config.getModelDefinition().getTrainedModel()); + TrainedModelType trainedModelType = TrainedModelType.typeFromTrainedModel(config.getModelDefinition().getTrainedModel()); if (trainedModelType == null) { - listener.onFailure(ExceptionsHelper.badRequestException("Unknown trained model definition class [{}]", - config.getModelDefinition().getTrainedModel().getName())); + listener.onFailure( + ExceptionsHelper.badRequestException( + "Unknown trained model definition class [{}]", + config.getModelDefinition().getTrainedModel().getName() + ) + ); return; } @@ -120,41 +139,43 @@ protected void masterOperation(Task task, // Set the model type from the definition config = new TrainedModelConfig.Builder(config).setModelType(trainedModelType).build(); } else if (trainedModelType != config.getModelType()) { - listener.onFailure(ExceptionsHelper.badRequestException( - "{} [{}] does not match the model definition type [{}]", - TrainedModelConfig.MODEL_TYPE.getPreferredName(), config.getModelType(), - trainedModelType)); + listener.onFailure( + ExceptionsHelper.badRequestException( + "{} [{}] does not match the model definition type [{}]", + TrainedModelConfig.MODEL_TYPE.getPreferredName(), + config.getModelType(), + trainedModelType + ) + ); return; } - if (config.getInferenceConfig() - .isTargetTypeSupported(config - .getModelDefinition() - .getTrainedModel() - .targetType()) == false) { - listener.onFailure(ExceptionsHelper.badRequestException( - "Model [{}] inference config type [{}] does not support definition target type [{}]", - config.getModelId(), - config.getInferenceConfig().getName(), - config.getModelDefinition().getTrainedModel().targetType())); + if (config.getInferenceConfig().isTargetTypeSupported(config.getModelDefinition().getTrainedModel().targetType()) == false) { + listener.onFailure( + ExceptionsHelper.badRequestException( + "Model [{}] inference config type [{}] does not support definition target type [{}]", + config.getModelId(), + config.getInferenceConfig().getName(), + config.getModelDefinition().getTrainedModel().targetType() + ) + ); return; } - Version minCompatibilityVersion = config - .getModelDefinition() - .getTrainedModel() - .getMinimalCompatibilityVersion(); + Version minCompatibilityVersion = config.getModelDefinition().getTrainedModel().getMinimalCompatibilityVersion(); if (state.nodes().getMinNodeVersion().before(minCompatibilityVersion)) { - listener.onFailure(ExceptionsHelper.badRequestException( - "Definition for [{}] requires that all nodes are at least version [{}]", - config.getModelId(), - minCompatibilityVersion.toString())); + listener.onFailure( + ExceptionsHelper.badRequestException( + "Definition for [{}] requires that all nodes are at least version [{}]", + config.getModelId(), + minCompatibilityVersion.toString() + ) + ); return; } } - TrainedModelConfig.Builder trainedModelConfig = new TrainedModelConfig.Builder(config) - .setVersion(Version.CURRENT) + TrainedModelConfig.Builder trainedModelConfig = new TrainedModelConfig.Builder(config).setVersion(Version.CURRENT) .setCreateTime(Instant.now()) .setCreatedBy("api_user") .setLicenseLevel(License.OperationMode.PLATINUM.description()); @@ -169,73 +190,66 @@ protected void masterOperation(Task task, } if (ModelAliasMetadata.fromState(state).getModelId(trainedModelConfig.getModelId()) != null) { - listener.onFailure(ExceptionsHelper.badRequestException( - "requested model_id [{}] is the same as an existing model_alias. Model model_aliases and ids must be unique", - config.getModelId() - )); + listener.onFailure( + ExceptionsHelper.badRequestException( + "requested model_id [{}] is the same as an existing model_alias. Model model_aliases and ids must be unique", + config.getModelId() + ) + ); return; } ActionListener checkStorageIndexSizeListener = ActionListener.wrap( - r -> trainedModelProvider.storeTrainedModel(trainedModelConfig.build(), ActionListener.wrap( - bool -> { - TrainedModelConfig configToReturn = trainedModelConfig.clearDefinition().build(); - listener.onResponse(new PutTrainedModelAction.Response(configToReturn)); - }, - listener::onFailure - )), + r -> trainedModelProvider.storeTrainedModel(trainedModelConfig.build(), ActionListener.wrap(bool -> { + TrainedModelConfig configToReturn = trainedModelConfig.clearDefinition().build(); + listener.onResponse(new PutTrainedModelAction.Response(configToReturn)); + }, listener::onFailure)), listener::onFailure ); - ActionListener tagsModelIdCheckListener = ActionListener.wrap( - r -> { - if (TrainedModelType.PYTORCH.equals(trainedModelConfig.getModelType())) { - client.admin() - .indices() - .prepareStats(InferenceIndexConstants.nativeDefinitionStore()) - .clear() - .setStore(true) - .execute( - ActionListener.wrap( - stats -> { - IndexStats indexStats = stats.getIndices().get(InferenceIndexConstants.nativeDefinitionStore()); - if (indexStats == null) { - checkStorageIndexSizeListener.onResponse(null); - return; - } - if (indexStats.getTotal().getStore().getSizeInBytes() > MAX_NATIVE_DEFINITION_INDEX_SIZE.getBytes()) { - listener.onFailure(new ElasticsearchStatusException( - "Native model store has exceeded the maximum acceptable size of {}, " + - "please delete older unused pytorch models", - RestStatus.CONFLICT, - MAX_NATIVE_DEFINITION_INDEX_SIZE.toString() - )); - return; - } - checkStorageIndexSizeListener.onResponse(null); - }, - e -> { - if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) { - checkStorageIndexSizeListener.onResponse(null); - return; - } - listener.onFailure( - new ElasticsearchStatusException( - "Unable to calculate stats for definition storage index [{}], please try again later", - RestStatus.SERVICE_UNAVAILABLE, - e, - InferenceIndexConstants.nativeDefinitionStore() - ) - ); - } + ActionListener tagsModelIdCheckListener = ActionListener.wrap(r -> { + if (TrainedModelType.PYTORCH.equals(trainedModelConfig.getModelType())) { + client.admin() + .indices() + .prepareStats(InferenceIndexConstants.nativeDefinitionStore()) + .clear() + .setStore(true) + .execute(ActionListener.wrap(stats -> { + IndexStats indexStats = stats.getIndices().get(InferenceIndexConstants.nativeDefinitionStore()); + if (indexStats == null) { + checkStorageIndexSizeListener.onResponse(null); + return; + } + if (indexStats.getTotal().getStore().getSizeInBytes() > MAX_NATIVE_DEFINITION_INDEX_SIZE.getBytes()) { + listener.onFailure( + new ElasticsearchStatusException( + "Native model store has exceeded the maximum acceptable size of {}, " + + "please delete older unused pytorch models", + RestStatus.CONFLICT, + MAX_NATIVE_DEFINITION_INDEX_SIZE.toString() + ) + ); + return; + } + checkStorageIndexSizeListener.onResponse(null); + }, e -> { + if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) { + checkStorageIndexSizeListener.onResponse(null); + return; + } + listener.onFailure( + new ElasticsearchStatusException( + "Unable to calculate stats for definition storage index [{}], please try again later", + RestStatus.SERVICE_UNAVAILABLE, + e, + InferenceIndexConstants.nativeDefinitionStore() ) ); - return; - } - checkStorageIndexSizeListener.onResponse(null); - }, - listener::onFailure - ); + })); + return; + } + checkStorageIndexSizeListener.onResponse(null); + }, listener::onFailure); ActionListener modelIdTagCheckListener = ActionListener.wrap( r -> checkTagsAgainstModelIds(request.getTrainedModelConfig().getTags(), tagsModelIdCheckListener), @@ -247,26 +261,25 @@ protected void masterOperation(Task task, private void checkModelIdAgainstTags(String modelId, ActionListener listener) { QueryBuilder builder = QueryBuilders.constantScoreQuery( - QueryBuilders.boolQuery() - .filter(QueryBuilders.termQuery(TrainedModelConfig.TAGS.getPreferredName(), modelId))); + QueryBuilders.boolQuery().filter(QueryBuilders.termQuery(TrainedModelConfig.TAGS.getPreferredName(), modelId)) + ); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(builder).size(0).trackTotalHitsUpTo(1); SearchRequest searchRequest = new SearchRequest(InferenceIndexConstants.INDEX_PATTERN).source(sourceBuilder); - executeAsyncWithOrigin(client.threadPool().getThreadContext(), + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), ML_ORIGIN, searchRequest, - ActionListener.wrap( - response -> { - if (response.getHits().getTotalHits().value > 0) { - listener.onFailure( - ExceptionsHelper.badRequestException( - Messages.getMessage(Messages.INFERENCE_MODEL_ID_AND_TAGS_UNIQUE, modelId))); - return; - } - listener.onResponse(null); - }, - listener::onFailure - ), - client::search); + ActionListener.wrap(response -> { + if (response.getHits().getTotalHits().value > 0) { + listener.onFailure( + ExceptionsHelper.badRequestException(Messages.getMessage(Messages.INFERENCE_MODEL_ID_AND_TAGS_UNIQUE, modelId)) + ); + return; + } + listener.onResponse(null); + }, listener::onFailure), + client::search + ); } private void checkTagsAgainstModelIds(List tags, ActionListener listener) { @@ -276,25 +289,25 @@ private void checkTagsAgainstModelIds(List tags, ActionListener li } QueryBuilder builder = QueryBuilders.constantScoreQuery( - QueryBuilders.boolQuery() - .filter(QueryBuilders.termsQuery(TrainedModelConfig.MODEL_ID.getPreferredName(), tags))); + QueryBuilders.boolQuery().filter(QueryBuilders.termsQuery(TrainedModelConfig.MODEL_ID.getPreferredName(), tags)) + ); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(builder).size(0).trackTotalHitsUpTo(1); SearchRequest searchRequest = new SearchRequest(InferenceIndexConstants.INDEX_PATTERN).source(sourceBuilder); - executeAsyncWithOrigin(client.threadPool().getThreadContext(), + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), ML_ORIGIN, searchRequest, - ActionListener.wrap( - response -> { - if (response.getHits().getTotalHits().value > 0) { - listener.onFailure( - ExceptionsHelper.badRequestException(Messages.getMessage(Messages.INFERENCE_TAGS_AND_MODEL_IDS_UNIQUE, tags))); - return; - } - listener.onResponse(null); - }, - listener::onFailure - ), - client::search); + ActionListener.wrap(response -> { + if (response.getHits().getTotalHits().value > 0) { + listener.onFailure( + ExceptionsHelper.badRequestException(Messages.getMessage(Messages.INFERENCE_TAGS_AND_MODEL_IDS_UNIQUE, tags)) + ); + return; + } + listener.onResponse(null); + }, listener::onFailure), + client::search + ); } @Override diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPutTrainedModelAliasAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPutTrainedModelAliasAction.java index ff92d882d535e..d0c6611296b1c 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPutTrainedModelAliasAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPutTrainedModelAliasAction.java @@ -66,7 +66,8 @@ public TransportPutTrainedModelAliasAction( XPackLicenseState licenseState, ActionFilters actionFilters, InferenceAuditor auditor, - IndexNameExpressionResolver indexNameExpressionResolver) { + IndexNameExpressionResolver indexNameExpressionResolver + ) { super( PutTrainedModelAliasAction.NAME, transportService, @@ -94,13 +95,15 @@ protected void masterOperation( final String oldModelId = ModelAliasMetadata.fromState(state).getModelId(request.getModelAlias()); if (oldModelId != null && (request.isReassign() == false)) { - listener.onFailure(ExceptionsHelper.badRequestException( - "cannot assign model_alias [{}] to model_id [{}] as model_alias already refers to [{}]. " - + - "Set parameter [reassign] to [true] if model_alias should be reassigned.", - request.getModelAlias(), - request.getModelId(), - oldModelId)); + listener.onFailure( + ExceptionsHelper.badRequestException( + "cannot assign model_alias [{}] to model_id [{}] as model_alias already refers to [{}]. " + + "Set parameter [reassign] to [true] if model_alias should be reassigned.", + request.getModelAlias(), + request.getModelId(), + oldModelId + ) + ); return; } Set modelIds = new HashSet<>(); @@ -109,83 +112,75 @@ protected void masterOperation( if (oldModelId != null) { modelIds.add(oldModelId); } - trainedModelProvider.getTrainedModels(modelIds, GetTrainedModelsAction.Includes.empty(), true, ActionListener.wrap( - models -> { - TrainedModelConfig newModel = null; - TrainedModelConfig oldModel = null; - for (TrainedModelConfig config : models) { - if (config.getModelId().equals(request.getModelId())) { - newModel = config; - } - if (config.getModelId().equals(oldModelId)) { - oldModel = config; - } - if (config.getModelId().equals(request.getModelAlias())) { - listener.onFailure( - ExceptionsHelper.badRequestException("model_alias cannot be the same as an existing trained model_id") - ); - return; - } + trainedModelProvider.getTrainedModels(modelIds, GetTrainedModelsAction.Includes.empty(), true, ActionListener.wrap(models -> { + TrainedModelConfig newModel = null; + TrainedModelConfig oldModel = null; + for (TrainedModelConfig config : models) { + if (config.getModelId().equals(request.getModelId())) { + newModel = config; } - if (newModel == null) { + if (config.getModelId().equals(oldModelId)) { + oldModel = config; + } + if (config.getModelId().equals(request.getModelAlias())) { listener.onFailure( - ExceptionsHelper.missingTrainedModel(request.getModelId()) + ExceptionsHelper.badRequestException("model_alias cannot be the same as an existing trained model_id") ); return; } - if (isLicensed.test(newModel) == false) { - listener.onFailure(LicenseUtils.newComplianceException(XPackField.MACHINE_LEARNING)); - return; - } - if (newModel.getModelType() == TrainedModelType.PYTORCH) { - listener.onFailure(ExceptionsHelper.badRequestException("model_alias is not supported on pytorch models")); - return; - } - // if old model is null, none of these validations matter - // we should still allow reassignment even if the old model was some how deleted and the alias still refers to it - if (oldModel != null) { - // validate inference configs are the same type. Moving an alias from regression -> classification seems dangerous - if (newModel.getInferenceConfig() != null && oldModel.getInferenceConfig() != null) { - if (newModel.getInferenceConfig().getName().equals(oldModel.getInferenceConfig().getName()) == false) { - listener.onFailure( - ExceptionsHelper.badRequestException( - "cannot reassign model_alias [{}] to model [{}] " + } + if (newModel == null) { + listener.onFailure(ExceptionsHelper.missingTrainedModel(request.getModelId())); + return; + } + if (isLicensed.test(newModel) == false) { + listener.onFailure(LicenseUtils.newComplianceException(XPackField.MACHINE_LEARNING)); + return; + } + if (newModel.getModelType() == TrainedModelType.PYTORCH) { + listener.onFailure(ExceptionsHelper.badRequestException("model_alias is not supported on pytorch models")); + return; + } + // if old model is null, none of these validations matter + // we should still allow reassignment even if the old model was some how deleted and the alias still refers to it + if (oldModel != null) { + // validate inference configs are the same type. Moving an alias from regression -> classification seems dangerous + if (newModel.getInferenceConfig() != null && oldModel.getInferenceConfig() != null) { + if (newModel.getInferenceConfig().getName().equals(oldModel.getInferenceConfig().getName()) == false) { + listener.onFailure( + ExceptionsHelper.badRequestException( + "cannot reassign model_alias [{}] to model [{}] " + "with inference config type [{}] from model [{}] with type [{}]", - request.getModelAlias(), - newModel.getModelId(), - newModel.getInferenceConfig().getName(), - oldModel.getModelId(), - oldModel.getInferenceConfig().getName() - ) - ); - return; - } + request.getModelAlias(), + newModel.getModelId(), + newModel.getInferenceConfig().getName(), + oldModel.getModelId(), + oldModel.getInferenceConfig().getName() + ) + ); + return; } + } - Set oldInputFields = new HashSet<>(oldModel.getInput().getFieldNames()); - Set newInputFields = new HashSet<>(newModel.getInput().getFieldNames()); - // TODO should we fail in this case??? - if (Sets.difference(oldInputFields, newInputFields).size() > (oldInputFields.size() / 2) + Set oldInputFields = new HashSet<>(oldModel.getInput().getFieldNames()); + Set newInputFields = new HashSet<>(newModel.getInput().getFieldNames()); + // TODO should we fail in this case??? + if (Sets.difference(oldInputFields, newInputFields).size() > (oldInputFields.size() / 2) || Sets.intersection(newInputFields, oldInputFields).size() < (oldInputFields.size() / 2)) { - String warning = Messages.getMessage( - TRAINED_MODEL_INPUTS_DIFFER_SIGNIFICANTLY, - request.getModelId(), - oldModelId); - auditor.warning(oldModelId, warning); - logger.warn("[{}] {}", oldModelId, warning); - HeaderWarning.addWarning(DeprecationLogger.CRITICAL, warning); - } + String warning = Messages.getMessage(TRAINED_MODEL_INPUTS_DIFFER_SIGNIFICANTLY, request.getModelId(), oldModelId); + auditor.warning(oldModelId, warning); + logger.warn("[{}] {}", oldModelId, warning); + HeaderWarning.addWarning(DeprecationLogger.CRITICAL, warning); } - clusterService.submitStateUpdateTask("update-model-alias", new AckedClusterStateUpdateTask(request, listener) { - @Override - public ClusterState execute(final ClusterState currentState) { - return updateModelAlias(currentState, request); - } - }); + } + clusterService.submitStateUpdateTask("update-model-alias", new AckedClusterStateUpdateTask(request, listener) { + @Override + public ClusterState execute(final ClusterState currentState) { + return updateModelAlias(currentState, request); + } + }); - }, - listener::onFailure - )); + }, listener::onFailure)); } static ClusterState updateModelAlias(final ClusterState currentState, final PutTrainedModelAliasAction.Request request) { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPutTrainedModelDefinitionPartAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPutTrainedModelDefinitionPartAction.java index 39b3c91f6bc88..8353d8c20e7fc 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPutTrainedModelDefinitionPartAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPutTrainedModelDefinitionPartAction.java @@ -125,7 +125,7 @@ protected void masterOperation(Task task, Request request, ClusterState state, A @Override protected ClusterBlockException checkBlock(Request request, ClusterState state) { - //TODO do we really need to do this??? + // TODO do we really need to do this??? return null; } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPutTrainedModelVocabularyAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPutTrainedModelVocabularyAction.java index 7ef4cdcad6f82..bd23cb6703938 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPutTrainedModelVocabularyAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPutTrainedModelVocabularyAction.java @@ -33,7 +33,6 @@ import org.elasticsearch.xpack.ml.inference.nlp.Vocabulary; import org.elasticsearch.xpack.ml.inference.persistence.TrainedModelProvider; - public class TransportPutTrainedModelVocabularyAction extends TransportMasterNodeAction { private final TrainedModelProvider trainedModelProvider; @@ -81,7 +80,7 @@ protected void masterOperation(Task task, Request request, ClusterState state, A } trainedModelProvider.storeTrainedModelVocabulary( request.getModelId(), - ((NlpConfig)inferenceConfig).getVocabularyConfig(), + ((NlpConfig) inferenceConfig).getVocabularyConfig(), new Vocabulary(request.getVocabulary(), request.getModelId()), ActionListener.wrap(stored -> listener.onResponse(AcknowledgedResponse.TRUE), listener::onFailure) ); @@ -92,7 +91,7 @@ protected void masterOperation(Task task, Request request, ClusterState state, A @Override protected ClusterBlockException checkBlock(Request request, ClusterState state) { - //TODO do we really need to do this??? + // TODO do we really need to do this??? return null; } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportResetJobAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportResetJobAction.java index df3ce34e0cd03..3d5338a846dc5 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportResetJobAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportResetJobAction.java @@ -61,12 +61,27 @@ public class TransportResetJobAction extends AcknowledgedTransportMasterNodeActi private final AnomalyDetectionAuditor auditor; @Inject - public TransportResetJobAction(TransportService transportService, ClusterService clusterService, ThreadPool threadPool, - ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver, Client client, - JobConfigProvider jobConfigProvider, JobResultsProvider jobResultsProvider, - AnomalyDetectionAuditor auditor) { - super(ResetJobAction.NAME, transportService, clusterService, threadPool, actionFilters, ResetJobAction.Request::new, - indexNameExpressionResolver, ThreadPool.Names.SAME); + public TransportResetJobAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver, + Client client, + JobConfigProvider jobConfigProvider, + JobResultsProvider jobResultsProvider, + AnomalyDetectionAuditor auditor + ) { + super( + ResetJobAction.NAME, + transportService, + clusterService, + threadPool, + actionFilters, + ResetJobAction.Request::new, + indexNameExpressionResolver, + ThreadPool.Names.SAME + ); this.client = Objects.requireNonNull(client); this.jobConfigProvider = Objects.requireNonNull(jobConfigProvider); this.jobResultsProvider = Objects.requireNonNull(jobResultsProvider); @@ -74,8 +89,12 @@ public TransportResetJobAction(TransportService transportService, ClusterService } @Override - protected void masterOperation(Task task, ResetJobAction.Request request, ClusterState state, - ActionListener listener) throws Exception { + protected void masterOperation( + Task task, + ResetJobAction.Request request, + ClusterState state, + ActionListener listener + ) throws Exception { if (MlMetadata.getMlMetadata(state).isUpgradeMode()) { listener.onFailure(ExceptionsHelper.conflictStatusException("cannot reset job while indices are being upgraded")); return; @@ -83,98 +102,105 @@ protected void masterOperation(Task task, ResetJobAction.Request request, Cluste final TaskId taskId = new TaskId(clusterService.localNode().getId(), task.getId()); - ActionListener jobListener = ActionListener.wrap( - jobBuilder -> { - Job job = jobBuilder.build(); - PersistentTasksCustomMetadata tasks = state.getMetadata().custom(PersistentTasksCustomMetadata.TYPE); - JobState jobState = MlTasks.getJobState(job.getId(), tasks); - if (request.isSkipJobStateValidation() == false && jobState != JobState.CLOSED) { - listener.onFailure(ExceptionsHelper.conflictStatusException(Messages.getMessage(Messages.REST_JOB_NOT_CLOSED_RESET))); - return; - } - if (job.getBlocked().getReason() != Blocked.Reason.NONE && job.getBlocked().getReason() != Blocked.Reason.RESET) { - listener.onFailure(ExceptionsHelper.conflictStatusException( - "cannot reset job while it is blocked with [" + job.getBlocked().getReason() + "]")); - return; - } + ActionListener jobListener = ActionListener.wrap(jobBuilder -> { + Job job = jobBuilder.build(); + PersistentTasksCustomMetadata tasks = state.getMetadata().custom(PersistentTasksCustomMetadata.TYPE); + JobState jobState = MlTasks.getJobState(job.getId(), tasks); + if (request.isSkipJobStateValidation() == false && jobState != JobState.CLOSED) { + listener.onFailure(ExceptionsHelper.conflictStatusException(Messages.getMessage(Messages.REST_JOB_NOT_CLOSED_RESET))); + return; + } + if (job.getBlocked().getReason() != Blocked.Reason.NONE && job.getBlocked().getReason() != Blocked.Reason.RESET) { + listener.onFailure( + ExceptionsHelper.conflictStatusException( + "cannot reset job while it is blocked with [" + job.getBlocked().getReason() + "]" + ) + ); + return; + } - if (job.getBlocked().getReason() == Blocked.Reason.RESET) { - waitExistingResetTaskToComplete(job.getBlocked().getTaskId(), request, ActionListener.wrap( - r -> resetIfJobIsStillBlockedOnReset(task, request, listener), - listener::onFailure - )); - } else { - ParentTaskAssigningClient taskClient = new ParentTaskAssigningClient(client, taskId); - jobConfigProvider.updateJobBlockReason(job.getId(), new Blocked(Blocked.Reason.RESET, taskId), ActionListener.wrap( - r -> resetJob(taskClient, (CancellableTask) task, request, listener), - listener::onFailure - )); - } - }, - listener::onFailure - ); + if (job.getBlocked().getReason() == Blocked.Reason.RESET) { + waitExistingResetTaskToComplete( + job.getBlocked().getTaskId(), + request, + ActionListener.wrap(r -> resetIfJobIsStillBlockedOnReset(task, request, listener), listener::onFailure) + ); + } else { + ParentTaskAssigningClient taskClient = new ParentTaskAssigningClient(client, taskId); + jobConfigProvider.updateJobBlockReason( + job.getId(), + new Blocked(Blocked.Reason.RESET, taskId), + ActionListener.wrap(r -> resetJob(taskClient, (CancellableTask) task, request, listener), listener::onFailure) + ); + } + }, listener::onFailure); jobConfigProvider.getJob(request.getJobId(), jobListener); } - private void waitExistingResetTaskToComplete(TaskId existingTaskId, ResetJobAction.Request request, - ActionListener listener) { - logger.debug(() -> new ParameterizedMessage( - "[{}] Waiting on existing reset task: {}", request.getJobId(), existingTaskId)); + private void waitExistingResetTaskToComplete( + TaskId existingTaskId, + ResetJobAction.Request request, + ActionListener listener + ) { + logger.debug(() -> new ParameterizedMessage("[{}] Waiting on existing reset task: {}", request.getJobId(), existingTaskId)); GetTaskRequest getTaskRequest = new GetTaskRequest(); getTaskRequest.setTaskId(existingTaskId); getTaskRequest.setWaitForCompletion(true); getTaskRequest.setTimeout(request.timeout()); - executeAsyncWithOrigin(client, ML_ORIGIN, GetTaskAction.INSTANCE, getTaskRequest, ActionListener.wrap( - getTaskResponse -> { - TaskResult taskResult = getTaskResponse.getTask(); - if (taskResult.isCompleted()) { - listener.onResponse(AcknowledgedResponse.of(true)); + executeAsyncWithOrigin(client, ML_ORIGIN, GetTaskAction.INSTANCE, getTaskRequest, ActionListener.wrap(getTaskResponse -> { + TaskResult taskResult = getTaskResponse.getTask(); + if (taskResult.isCompleted()) { + listener.onResponse(AcknowledgedResponse.of(true)); + } else { + BytesReference taskError = taskResult.getError(); + if (taskError != null) { + listener.onFailure(ExceptionsHelper.serverError("reset failed to complete; error [{}]", taskError.utf8ToString())); } else { - BytesReference taskError = taskResult.getError(); - if (taskError != null) { - listener.onFailure(ExceptionsHelper.serverError("reset failed to complete; error [{}]", - taskError.utf8ToString())); - } else { - listener.onFailure(ExceptionsHelper.serverError("reset failed to complete")); - } + listener.onFailure(ExceptionsHelper.serverError("reset failed to complete")); } - }, - listener::onFailure - )); + } + }, listener::onFailure)); } private void resetIfJobIsStillBlockedOnReset(Task task, ResetJobAction.Request request, ActionListener listener) { - ActionListener jobListener = ActionListener.wrap( - jobResponse -> { - Job job = jobResponse.build(); - if (job.getBlocked().getReason() == Blocked.Reason.NONE) { - // This means the previous reset task finished successfully as it managed to unset the blocked reason. - logger.debug(() -> new ParameterizedMessage("[{}] Existing reset task finished successfully", request.getJobId())); - listener.onResponse(AcknowledgedResponse.TRUE); - } else if (job.getBlocked().getReason() == Blocked.Reason.RESET){ - // Seems like the task was removed abruptly as it hasn't unset the block on reset. - // Let us try reset again. - logger.debug(() -> new ParameterizedMessage("[{}] Existing reset task was interrupted; retrying reset", - request.getJobId())); - ParentTaskAssigningClient taskClient = new ParentTaskAssigningClient(client, - new TaskId(clusterService.localNode().getId(), task.getId())); - resetJob(taskClient, (CancellableTask) task, request, listener); - } else { - // Blocked reason is now different. Let us just communicate the conflict. - listener.onFailure(ExceptionsHelper.conflictStatusException( - "cannot reset job while it is blocked with [" + job.getBlocked().getReason() + "]")); - } - }, - listener::onFailure - ); + ActionListener jobListener = ActionListener.wrap(jobResponse -> { + Job job = jobResponse.build(); + if (job.getBlocked().getReason() == Blocked.Reason.NONE) { + // This means the previous reset task finished successfully as it managed to unset the blocked reason. + logger.debug(() -> new ParameterizedMessage("[{}] Existing reset task finished successfully", request.getJobId())); + listener.onResponse(AcknowledgedResponse.TRUE); + } else if (job.getBlocked().getReason() == Blocked.Reason.RESET) { + // Seems like the task was removed abruptly as it hasn't unset the block on reset. + // Let us try reset again. + logger.debug( + () -> new ParameterizedMessage("[{}] Existing reset task was interrupted; retrying reset", request.getJobId()) + ); + ParentTaskAssigningClient taskClient = new ParentTaskAssigningClient( + client, + new TaskId(clusterService.localNode().getId(), task.getId()) + ); + resetJob(taskClient, (CancellableTask) task, request, listener); + } else { + // Blocked reason is now different. Let us just communicate the conflict. + listener.onFailure( + ExceptionsHelper.conflictStatusException( + "cannot reset job while it is blocked with [" + job.getBlocked().getReason() + "]" + ) + ); + } + }, listener::onFailure); // Get job again to check if it is still blocked jobConfigProvider.getJob(request.getJobId(), jobListener); } - private void resetJob(ParentTaskAssigningClient taskClient, CancellableTask task, ResetJobAction.Request request, - ActionListener listener) { + private void resetJob( + ParentTaskAssigningClient taskClient, + CancellableTask task, + ResetJobAction.Request request, + ActionListener listener + ) { String jobId = request.getJobId(); // Now that we have updated the job's block reason, we should check again @@ -182,59 +208,61 @@ private void resetJob(ParentTaskAssigningClient taskClient, CancellableTask task PersistentTasksCustomMetadata tasks = clusterService.state().getMetadata().custom(PersistentTasksCustomMetadata.TYPE); JobState jobState = MlTasks.getJobState(jobId, tasks); if (request.isSkipJobStateValidation() == false && jobState != JobState.CLOSED) { - jobConfigProvider.updateJobBlockReason(jobId, null, ActionListener.wrap( - clearResetResponse -> listener.onFailure(ExceptionsHelper.conflictStatusException( - Messages.getMessage(Messages.REST_JOB_NOT_CLOSED_RESET))), - e -> listener.onFailure(ExceptionsHelper.conflictStatusException(Messages.getMessage(Messages.REST_JOB_NOT_CLOSED_RESET))) - )); + jobConfigProvider.updateJobBlockReason( + jobId, + null, + ActionListener.wrap( + clearResetResponse -> listener.onFailure( + ExceptionsHelper.conflictStatusException(Messages.getMessage(Messages.REST_JOB_NOT_CLOSED_RESET)) + ), + e -> listener.onFailure( + ExceptionsHelper.conflictStatusException(Messages.getMessage(Messages.REST_JOB_NOT_CLOSED_RESET)) + ) + ) + ); return; } logger.info("[{}] Resetting job", jobId); - ActionListener resultsIndexCreatedListener = ActionListener.wrap( - resultsIndexCreatedResponse -> { - if (task.isCancelled()) { - listener.onResponse(AcknowledgedResponse.of(false)); - return; - } - finishSuccessfulReset(jobId, listener); - }, - listener::onFailure - ); + ActionListener resultsIndexCreatedListener = ActionListener.wrap(resultsIndexCreatedResponse -> { + if (task.isCancelled()) { + listener.onResponse(AcknowledgedResponse.of(false)); + return; + } + finishSuccessfulReset(jobId, listener); + }, listener::onFailure); CheckedConsumer jobDocsDeletionListener = response -> { if (task.isCancelled()) { listener.onResponse(AcknowledgedResponse.of(false)); return; } - jobConfigProvider.getJob(jobId, ActionListener.wrap( - jobBuilder -> { - if (task.isCancelled()) { - listener.onResponse(AcknowledgedResponse.of(false)); - return; - } - jobResultsProvider.createJobResultIndex( - jobBuilder.build(), clusterService.state(), resultsIndexCreatedListener); - }, - listener::onFailure - )); + jobConfigProvider.getJob(jobId, ActionListener.wrap(jobBuilder -> { + if (task.isCancelled()) { + listener.onResponse(AcknowledgedResponse.of(false)); + return; + } + jobResultsProvider.createJobResultIndex(jobBuilder.build(), clusterService.state(), resultsIndexCreatedListener); + }, listener::onFailure)); }; JobDataDeleter jobDataDeleter = new JobDataDeleter(taskClient, jobId); - jobDataDeleter.deleteJobDocuments(jobConfigProvider, indexNameExpressionResolver, - clusterService.state(), jobDocsDeletionListener, listener::onFailure); + jobDataDeleter.deleteJobDocuments( + jobConfigProvider, + indexNameExpressionResolver, + clusterService.state(), + jobDocsDeletionListener, + listener::onFailure + ); } private void finishSuccessfulReset(String jobId, ActionListener listener) { - jobConfigProvider.updateJobAfterReset(jobId, ActionListener.wrap( - blockReasonUpdatedResponse -> { - logger.info("[{}] Reset has successfully completed", jobId); - auditor.info(jobId, Messages.getMessage(Messages.JOB_AUDIT_RESET)); - listener.onResponse(AcknowledgedResponse.of(true)); - }, - listener::onFailure - )); + jobConfigProvider.updateJobAfterReset(jobId, ActionListener.wrap(blockReasonUpdatedResponse -> { + logger.info("[{}] Reset has successfully completed", jobId); + auditor.info(jobId, Messages.getMessage(Messages.JOB_AUDIT_RESET)); + listener.onResponse(AcknowledgedResponse.of(true)); + }, listener::onFailure)); } @Override diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportRevertModelSnapshotAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportRevertModelSnapshotAction.java index 853b03433c36d..0c174dbd3dd3d 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportRevertModelSnapshotAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportRevertModelSnapshotAction.java @@ -53,8 +53,9 @@ import static org.elasticsearch.xpack.core.ClientHelper.ML_ORIGIN; import static org.elasticsearch.xpack.core.ClientHelper.executeAsyncWithOrigin; -public class TransportRevertModelSnapshotAction extends TransportMasterNodeAction { +public class TransportRevertModelSnapshotAction extends TransportMasterNodeAction< + RevertModelSnapshotAction.Request, + RevertModelSnapshotAction.Response> { private static final Logger logger = LogManager.getLogger(TransportRevertModelSnapshotAction.class); @@ -65,13 +66,29 @@ public class TransportRevertModelSnapshotAction extends TransportMasterNodeActio private final MlConfigMigrationEligibilityCheck migrationEligibilityCheck; @Inject - public TransportRevertModelSnapshotAction(Settings settings, ThreadPool threadPool, TransportService transportService, - ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver, - JobManager jobManager, JobResultsProvider jobResultsProvider, - ClusterService clusterService, Client client, JobDataCountsPersister jobDataCountsPersister) { - super(RevertModelSnapshotAction.NAME, transportService, clusterService, threadPool, actionFilters, - RevertModelSnapshotAction.Request::new, indexNameExpressionResolver, RevertModelSnapshotAction.Response::new, - ThreadPool.Names.SAME); + public TransportRevertModelSnapshotAction( + Settings settings, + ThreadPool threadPool, + TransportService transportService, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver, + JobManager jobManager, + JobResultsProvider jobResultsProvider, + ClusterService clusterService, + Client client, + JobDataCountsPersister jobDataCountsPersister + ) { + super( + RevertModelSnapshotAction.NAME, + transportService, + clusterService, + threadPool, + actionFilters, + RevertModelSnapshotAction.Request::new, + indexNameExpressionResolver, + RevertModelSnapshotAction.Response::new, + ThreadPool.Names.SAME + ); this.client = client; this.jobManager = jobManager; this.jobResultsProvider = jobResultsProvider; @@ -80,8 +97,12 @@ public TransportRevertModelSnapshotAction(Settings settings, ThreadPool threadPo } @Override - protected void masterOperation(Task task, RevertModelSnapshotAction.Request request, ClusterState state, - ActionListener listener) { + protected void masterOperation( + Task task, + RevertModelSnapshotAction.Request request, + ClusterState state, + ActionListener listener + ) { final String jobId = request.getJobId(); final TaskId taskId = new TaskId(clusterService.localNode().getId(), task.getId()); @@ -90,65 +111,76 @@ protected void masterOperation(Task task, RevertModelSnapshotAction.Request requ return; } - logger.debug("Received request to revert to snapshot id '{}' for job '{}', deleting intervening results: {}", - request.getSnapshotId(), jobId, request.getDeleteInterveningResults()); + logger.debug( + "Received request to revert to snapshot id '{}' for job '{}', deleting intervening results: {}", + request.getSnapshotId(), + jobId, + request.getDeleteInterveningResults() + ); // 5. Revert the state - ActionListener annotationsIndexUpdateListener = ActionListener.wrap( - r -> { - ActionListener jobListener = ActionListener.wrap( - job -> { - PersistentTasksCustomMetadata tasks = state.getMetadata().custom(PersistentTasksCustomMetadata.TYPE); - JobState jobState = MlTasks.getJobState(job.getId(), tasks); - if (request.isForce() == false && jobState.equals(JobState.CLOSED) == false) { - listener.onFailure(ExceptionsHelper.conflictStatusException( - Messages.getMessage(Messages.REST_JOB_NOT_CLOSED_REVERT))); - return; - } - if (MlTasks.getSnapshotUpgraderTask(jobId, request.getSnapshotId(), tasks) != null) { - listener.onFailure(ExceptionsHelper.conflictStatusException( - "Cannot revert job [{}] to snapshot [{}] as it is being upgraded", + ActionListener annotationsIndexUpdateListener = ActionListener.wrap(r -> { + ActionListener jobListener = ActionListener.wrap(job -> { + PersistentTasksCustomMetadata tasks = state.getMetadata().custom(PersistentTasksCustomMetadata.TYPE); + JobState jobState = MlTasks.getJobState(job.getId(), tasks); + if (request.isForce() == false && jobState.equals(JobState.CLOSED) == false) { + listener.onFailure(ExceptionsHelper.conflictStatusException(Messages.getMessage(Messages.REST_JOB_NOT_CLOSED_REVERT))); + return; + } + if (MlTasks.getSnapshotUpgraderTask(jobId, request.getSnapshotId(), tasks) != null) { + listener.onFailure( + ExceptionsHelper.conflictStatusException( + "Cannot revert job [{}] to snapshot [{}] as it is being upgraded", + jobId, + request.getSnapshotId() + ) + ); + return; + } + isBlocked(job, request, ActionListener.wrap(isBlocked -> { + if (isBlocked) { + listener.onFailure( + ExceptionsHelper.conflictStatusException( + "cannot revert job [{}] to snapshot [{}] while it is blocked with [{}]", jobId, - request.getSnapshotId() - )); - return; - } - isBlocked(job, request, ActionListener.wrap( - isBlocked -> { - if (isBlocked) { - listener.onFailure(ExceptionsHelper.conflictStatusException( - "cannot revert job [{}] to snapshot [{}] while it is blocked with [{}]", - jobId, request.getSnapshotId(), job.getBlocked().getReason()) - ); - } else { - jobManager.updateJobBlockReason(jobId, new Blocked(Blocked.Reason.REVERT, taskId), ActionListener.wrap( - aBoolean -> revertSnapshot(jobId, request, listener), - listener::onFailure - )); - } - }, - listener::onFailure - )); - }, - listener::onFailure - ); - - jobManager.getJob(jobId, jobListener); - }, - listener::onFailure - ); + request.getSnapshotId(), + job.getBlocked().getReason() + ) + ); + } else { + jobManager.updateJobBlockReason( + jobId, + new Blocked(Blocked.Reason.REVERT, taskId), + ActionListener.wrap(aBoolean -> revertSnapshot(jobId, request, listener), listener::onFailure) + ); + } + }, listener::onFailure)); + }, listener::onFailure); + + jobManager.getJob(jobId, jobListener); + }, listener::onFailure); // 4. Ensure the annotations index mappings are up to date ActionListener configMappingUpdateListener = ActionListener.wrap( - r -> AnnotationIndex.createAnnotationsIndexIfNecessaryAndWaitForYellow(client, state, request.masterNodeTimeout(), - annotationsIndexUpdateListener), + r -> AnnotationIndex.createAnnotationsIndexIfNecessaryAndWaitForYellow( + client, + state, + request.masterNodeTimeout(), + annotationsIndexUpdateListener + ), listener::onFailure ); // 3. Ensure the config index mappings are up to date ActionListener jobExistsListener = ActionListener.wrap( - r -> ElasticsearchMappings.addDocMappingIfMissing(MlConfigIndex.indexName(), MlConfigIndex::mapping, - client, state, request.masterNodeTimeout(), configMappingUpdateListener), + r -> ElasticsearchMappings.addDocMappingIfMissing( + MlConfigIndex.indexName(), + MlConfigIndex::mapping, + client, + state, + request.masterNodeTimeout(), + configMappingUpdateListener + ), listener::onFailure ); @@ -159,8 +191,13 @@ protected void masterOperation(Task task, RevertModelSnapshotAction.Request requ ); // 1. Verify/Create the state index and its alias exists - AnomalyDetectorsIndex.createStateIndexAndAliasIfNecessary(client, state, indexNameExpressionResolver, request.masterNodeTimeout(), - createStateIndexListener); + AnomalyDetectorsIndex.createStateIndexAndAliasIfNecessary( + client, + state, + indexNameExpressionResolver, + request.masterNodeTimeout(), + createStateIndexListener + ); } private void isBlocked(Job job, RevertModelSnapshotAction.Request request, ActionListener listener) { @@ -183,32 +220,40 @@ private void isBlocked(Job job, RevertModelSnapshotAction.Request request, Actio getTaskRequest.setWaitForCompletion(request.isForce()); getTaskRequest.setTimeout(request.timeout()); - executeAsyncWithOrigin(client, ML_ORIGIN, GetTaskAction.INSTANCE, getTaskRequest, ActionListener.wrap( - r -> listener.onResponse(r.getTask().isCompleted() == false), - e -> { + executeAsyncWithOrigin( + client, + ML_ORIGIN, + GetTaskAction.INSTANCE, + getTaskRequest, + ActionListener.wrap(r -> listener.onResponse(r.getTask().isCompleted() == false), e -> { if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) { listener.onResponse(false); } else { listener.onFailure(e); } - } - )); + }) + ); } else { listener.onResponse(true); } } - private void revertSnapshot(String jobId, RevertModelSnapshotAction.Request request, - ActionListener listener) { + private void revertSnapshot( + String jobId, + RevertModelSnapshotAction.Request request, + ActionListener listener + ) { ActionListener finalListener = ActionListener.wrap( - r -> jobManager.updateJobBlockReason(jobId, Blocked.none(), ActionListener.wrap( - aBoolean -> listener.onResponse(r), - listener::onFailure - )) - , e -> jobManager.updateJobBlockReason(jobId, Blocked.none(), ActionListener.wrap( - aBoolean -> listener.onFailure(e), - listener::onFailure - )) + r -> jobManager.updateJobBlockReason( + jobId, + Blocked.none(), + ActionListener.wrap(aBoolean -> listener.onResponse(r), listener::onFailure) + ), + e -> jobManager.updateJobBlockReason( + jobId, + Blocked.none(), + ActionListener.wrap(aBoolean -> listener.onFailure(e), listener::onFailure) + ) ); getModelSnapshot(request, jobResultsProvider, modelSnapshot -> { @@ -222,8 +267,12 @@ private void revertSnapshot(String jobId, RevertModelSnapshotAction.Request requ }, listener::onFailure); } - private void getModelSnapshot(RevertModelSnapshotAction.Request request, JobResultsProvider provider, Consumer handler, - Consumer errorHandler) { + private void getModelSnapshot( + RevertModelSnapshotAction.Request request, + JobResultsProvider provider, + Consumer handler, + Consumer errorHandler + ) { logger.info("Reverting to snapshot '" + request.getSnapshotId() + "'"); if (ModelSnapshot.isTheEmptySnapshot(request.getSnapshotId())) { @@ -240,36 +289,43 @@ private void getModelSnapshot(RevertModelSnapshotAction.Request request, JobResu } private static ResourceNotFoundException missingSnapshotException(RevertModelSnapshotAction.Request request) { - return new ResourceNotFoundException(Messages.getMessage(Messages.REST_NO_SUCH_MODEL_SNAPSHOT, request.getSnapshotId(), - request.getJobId())); + return new ResourceNotFoundException( + Messages.getMessage(Messages.REST_NO_SUCH_MODEL_SNAPSHOT, request.getSnapshotId(), request.getJobId()) + ); } private ActionListener wrapDeleteOldAnnotationsListener( - ActionListener listener, - ModelSnapshot modelSnapshot, - String jobId) { + ActionListener listener, + ModelSnapshot modelSnapshot, + String jobId + ) { return ActionListener.wrap(response -> { Date deleteAfter = modelSnapshot.getLatestResultTimeStamp() == null ? new Date(0) : modelSnapshot.getLatestResultTimeStamp(); logger.info("[{}] Removing intervening annotations after reverting model: deleting annotations after [{}]", jobId, deleteAfter); JobDataDeleter dataDeleter = new JobDataDeleter(client, jobId); - Set eventsToDelete = - Set.of( - // Because the results based on the delayed data are being deleted, the fact that the data was originally delayed is - // not relevant - Annotation.Event.DELAYED_DATA.toString(), - // Because the model that changed is no longer in use as it has been rolled back to a time before those changes occurred - Annotation.Event.MODEL_CHANGE.toString()); - dataDeleter.deleteAnnotations(deleteAfter.getTime() + 1, null, eventsToDelete, - listener.delegateFailure((l, r) -> l.onResponse(response))); + Set eventsToDelete = Set.of( + // Because the results based on the delayed data are being deleted, the fact that the data was originally delayed is + // not relevant + Annotation.Event.DELAYED_DATA.toString(), + // Because the model that changed is no longer in use as it has been rolled back to a time before those changes occurred + Annotation.Event.MODEL_CHANGE.toString() + ); + dataDeleter.deleteAnnotations( + deleteAfter.getTime() + 1, + null, + eventsToDelete, + listener.delegateFailure((l, r) -> l.onResponse(response)) + ); }, listener::onFailure); } private ActionListener wrapDeleteOldDataListener( - ActionListener listener, - ModelSnapshot modelSnapshot, - String jobId) { + ActionListener listener, + ModelSnapshot modelSnapshot, + String jobId + ) { // If we need to delete buckets that occurred after the snapshot, we // wrap the listener with one that invokes the OldDataRemover on @@ -284,9 +340,10 @@ private ActionListener wrapDeleteOldDataList } private ActionListener wrapRevertDataCountsListener( - ActionListener listener, - ModelSnapshot modelSnapshot, - String jobId) { + ActionListener listener, + ModelSnapshot modelSnapshot, + String jobId + ) { return ActionListener.wrap(response -> jobResultsProvider.dataCounts(jobId, counts -> { counts.setLatestRecordTimeStamp(modelSnapshot.getLatestRecordTimeStamp()); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportSetResetModeAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportSetResetModeAction.java index 2f4c9826da444..2cb8fc847bb62 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportSetResetModeAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportSetResetModeAction.java @@ -20,12 +20,16 @@ import org.elasticsearch.xpack.core.ml.action.SetResetModeAction; import org.elasticsearch.xpack.ml.inference.ModelAliasMetadata; - public class TransportSetResetModeAction extends AbstractTransportSetResetModeAction { @Inject - public TransportSetResetModeAction(TransportService transportService, ThreadPool threadPool, ClusterService clusterService, - ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver) { + public TransportSetResetModeAction( + TransportService transportService, + ThreadPool threadPool, + ClusterService clusterService, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver + ) { super(SetResetModeAction.NAME, transportService, threadPool, clusterService, actionFilters, indexNameExpressionResolver); } @@ -44,17 +48,13 @@ protected ClusterState setState(ClusterState oldState, SetResetModeActionRequest ClusterState.Builder newState = ClusterState.builder(oldState); if (request.shouldDeleteMetadata()) { assert request.isEnabled() == false; // SetResetModeActionRequest should have enforced this - newState.metadata(Metadata.builder(oldState.getMetadata()) - .removeCustom(MlMetadata.TYPE) - .removeCustom(ModelAliasMetadata.NAME) - .build()); + newState.metadata( + Metadata.builder(oldState.getMetadata()).removeCustom(MlMetadata.TYPE).removeCustom(ModelAliasMetadata.NAME).build() + ); } else { - MlMetadata.Builder builder = MlMetadata.Builder - .from(oldState.metadata().custom(MlMetadata.TYPE)) + MlMetadata.Builder builder = MlMetadata.Builder.from(oldState.metadata().custom(MlMetadata.TYPE)) .isResetMode(request.isEnabled()); - newState.metadata(Metadata.builder(oldState.getMetadata()) - .putCustom(MlMetadata.TYPE, builder.build()) - .build()); + newState.metadata(Metadata.builder(oldState.getMetadata()).putCustom(MlMetadata.TYPE, builder.build()).build()); } return newState.build(); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportSetUpgradeModeAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportSetUpgradeModeAction.java index 499c016c5be22..3f4acfbba1d84 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportSetUpgradeModeAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportSetUpgradeModeAction.java @@ -51,8 +51,8 @@ import static org.elasticsearch.xpack.core.ClientHelper.ML_ORIGIN; import static org.elasticsearch.xpack.core.ml.MlTasks.AWAITING_UPGRADE; import static org.elasticsearch.xpack.core.ml.MlTasks.DATAFEED_TASK_NAME; -import static org.elasticsearch.xpack.core.ml.MlTasks.JOB_TASK_NAME; import static org.elasticsearch.xpack.core.ml.MlTasks.DATA_FRAME_ANALYTICS_TASK_NAME; +import static org.elasticsearch.xpack.core.ml.MlTasks.JOB_TASK_NAME; public class TransportSetUpgradeModeAction extends AcknowledgedTransportMasterNodeAction { @@ -66,12 +66,26 @@ public class TransportSetUpgradeModeAction extends AcknowledgedTransportMasterNo private final OriginSettingClient client; @Inject - public TransportSetUpgradeModeAction(TransportService transportService, ThreadPool threadPool, ClusterService clusterService, - PersistentTasksClusterService persistentTasksClusterService, ActionFilters actionFilters, - IndexNameExpressionResolver indexNameExpressionResolver, Client client, - PersistentTasksService persistentTasksService) { - super(SetUpgradeModeAction.NAME, transportService, clusterService, threadPool, actionFilters, SetUpgradeModeAction.Request::new, - indexNameExpressionResolver, ThreadPool.Names.SAME); + public TransportSetUpgradeModeAction( + TransportService transportService, + ThreadPool threadPool, + ClusterService clusterService, + PersistentTasksClusterService persistentTasksClusterService, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver, + Client client, + PersistentTasksService persistentTasksService + ) { + super( + SetUpgradeModeAction.NAME, + transportService, + clusterService, + threadPool, + actionFilters, + SetUpgradeModeAction.Request::new, + indexNameExpressionResolver, + ThreadPool.Names.SAME + ); this.persistentTasksClusterService = persistentTasksClusterService; this.clusterService = clusterService; this.client = new OriginSettingClient(client, ML_ORIGIN); @@ -79,20 +93,29 @@ public TransportSetUpgradeModeAction(TransportService transportService, ThreadPo } @Override - protected void masterOperation(Task task, SetUpgradeModeAction.Request request, ClusterState state, - ActionListener listener) throws Exception { + protected void masterOperation( + Task task, + SetUpgradeModeAction.Request request, + ClusterState state, + ActionListener listener + ) throws Exception { // Don't want folks spamming this endpoint while it is in progress, only allow one request to be handled at a time if (isRunning.compareAndSet(false, true) == false) { - String msg = "Attempted to set [upgrade_mode] to [" + - request.isEnabled() + "] from [" + MlMetadata.getMlMetadata(state).isUpgradeMode() + - "] while previous request was processing."; + String msg = "Attempted to set [upgrade_mode] to [" + + request.isEnabled() + + "] from [" + + MlMetadata.getMlMetadata(state).isUpgradeMode() + + "] while previous request was processing."; logger.info(msg); Exception detail = new IllegalStateException(msg); - listener.onFailure(new ElasticsearchStatusException( - "Cannot change [upgrade_mode]. Previous request is still being processed.", - RestStatus.TOO_MANY_REQUESTS, - detail)); + listener.onFailure( + new ElasticsearchStatusException( + "Cannot change [upgrade_mode]. Previous request is still being processed.", + RestStatus.TOO_MANY_REQUESTS, + detail + ) + ); return; } @@ -104,63 +127,54 @@ protected void masterOperation(Task task, SetUpgradeModeAction.Request request, return; } - logger.info("Starting to set [upgrade_mode] to [" + request.isEnabled() + - "] from [" + MlMetadata.getMlMetadata(state).isUpgradeMode() + "]"); - - ActionListener wrappedListener = ActionListener.wrap( - r -> { - logger.info("Completed upgrade mode request"); - isRunning.set(false); - listener.onResponse(r); - }, - e -> { - logger.info("Completed upgrade mode request but with failure", e); - isRunning.set(false); - listener.onFailure(e); - } + logger.info( + "Starting to set [upgrade_mode] to [" + request.isEnabled() + "] from [" + MlMetadata.getMlMetadata(state).isUpgradeMode() + "]" ); + + ActionListener wrappedListener = ActionListener.wrap(r -> { + logger.info("Completed upgrade mode request"); + isRunning.set(false); + listener.onResponse(r); + }, e -> { + logger.info("Completed upgrade mode request but with failure", e); + isRunning.set(false); + listener.onFailure(e); + }); final PersistentTasksCustomMetadata tasksCustomMetadata = state.metadata().custom(PersistentTasksCustomMetadata.TYPE); // <4> We have unassigned the tasks, respond to the listener. - ActionListener>> unassignPersistentTasksListener = ActionListener.wrap( - unassignedPersistentTasks -> { - // Wait for our tasks to all stop - client.admin() - .cluster() - .prepareListTasks() - .setActions(ML_TASK_NAMES.stream().map(taskName -> taskName + "[c]").toArray(String[]::new)) - // There is a chance that we failed un-allocating a task due to allocation_id being changed - // This call will timeout in that case and return an error - .setWaitForCompletion(true) - .setTimeout(request.timeout()).execute(ActionListener.wrap( - r -> { - try { - // Handle potential node timeouts, - // these should be considered failures as tasks as still potentially executing - logger.info("Waited for tasks to be unassigned"); - if (r.getNodeFailures().isEmpty() == false) { - logger.info("There were node failures waiting for tasks", r.getNodeFailures().get(0)); - } - rethrowAndSuppress(r.getNodeFailures()); - wrappedListener.onResponse(AcknowledgedResponse.TRUE); - } catch (ElasticsearchException ex) { - logger.info("Caught node failures waiting for tasks to be unassigned", ex); - wrappedListener.onFailure(ex); - } - }, - wrappedListener::onFailure)); - }, - wrappedListener::onFailure - ); + ActionListener>> unassignPersistentTasksListener = ActionListener.wrap(unassignedPersistentTasks -> { + // Wait for our tasks to all stop + client.admin() + .cluster() + .prepareListTasks() + .setActions(ML_TASK_NAMES.stream().map(taskName -> taskName + "[c]").toArray(String[]::new)) + // There is a chance that we failed un-allocating a task due to allocation_id being changed + // This call will timeout in that case and return an error + .setWaitForCompletion(true) + .setTimeout(request.timeout()) + .execute(ActionListener.wrap(r -> { + try { + // Handle potential node timeouts, + // these should be considered failures as tasks as still potentially executing + logger.info("Waited for tasks to be unassigned"); + if (r.getNodeFailures().isEmpty() == false) { + logger.info("There were node failures waiting for tasks", r.getNodeFailures().get(0)); + } + rethrowAndSuppress(r.getNodeFailures()); + wrappedListener.onResponse(AcknowledgedResponse.TRUE); + } catch (ElasticsearchException ex) { + logger.info("Caught node failures waiting for tasks to be unassigned", ex); + wrappedListener.onFailure(ex); + } + }, wrappedListener::onFailure)); + }, wrappedListener::onFailure); // <3> After isolating the datafeeds, unassign the tasks - ActionListener> isolateDatafeedListener = ActionListener.wrap( - isolatedDatafeeds -> { - logger.info("Isolated the datafeeds"); - unassignPersistentTasks(tasksCustomMetadata, unassignPersistentTasksListener); - }, - wrappedListener::onFailure - ); + ActionListener> isolateDatafeedListener = ActionListener.wrap(isolatedDatafeeds -> { + logger.info("Isolated the datafeeds"); + unassignPersistentTasks(tasksCustomMetadata, unassignPersistentTasksListener); + }, wrappedListener::onFailure); /* <2> Handle the cluster response and act accordingly @@ -185,65 +199,61 @@ protected void masterOperation(Task task, SetUpgradeModeAction.Request request, */ - ActionListener clusterStateUpdateListener = ActionListener.wrap( - acknowledgedResponse -> { - // State change was not acknowledged, we either timed out or ran into some exception - // We should not continue and alert failure to the end user - if (acknowledgedResponse.isAcknowledged() == false) { - logger.info("Cluster state update is NOT acknowledged"); - wrappedListener.onFailure(new ElasticsearchTimeoutException("Unknown error occurred while updating cluster state")); - return; - } - - // There are no tasks to worry about starting/stopping - if (tasksCustomMetadata == null || tasksCustomMetadata.tasks().isEmpty()) { - logger.info("No tasks to worry about after state update"); - wrappedListener.onResponse(AcknowledgedResponse.TRUE); - return; - } - - // Did we change from disabled -> enabled? - if (request.isEnabled()) { - logger.info("Enabling upgrade mode, must isolate datafeeds"); - isolateDatafeeds(tasksCustomMetadata, isolateDatafeedListener); - } else { - logger.info("Disabling upgrade mode, must wait for tasks to not have AWAITING_UPGRADE assignment"); - persistentTasksService.waitForPersistentTasksCondition( - // Wait for jobs, datafeeds and analytics not to be "Awaiting upgrade" - persistentTasksCustomMetadata -> - persistentTasksCustomMetadata.tasks().stream() - .noneMatch(t -> ML_TASK_NAMES.contains(t.getTaskName()) && t.getAssignment().equals(AWAITING_UPGRADE)), - request.timeout(), - ActionListener.wrap(r -> { - logger.info("Done waiting for tasks to be out of AWAITING_UPGRADE"); - wrappedListener.onResponse(AcknowledgedResponse.TRUE); - }, wrappedListener::onFailure) - ); - } - }, - wrappedListener::onFailure - ); + ActionListener clusterStateUpdateListener = ActionListener.wrap(acknowledgedResponse -> { + // State change was not acknowledged, we either timed out or ran into some exception + // We should not continue and alert failure to the end user + if (acknowledgedResponse.isAcknowledged() == false) { + logger.info("Cluster state update is NOT acknowledged"); + wrappedListener.onFailure(new ElasticsearchTimeoutException("Unknown error occurred while updating cluster state")); + return; + } - //<1> Change MlMetadata to indicate that upgrade_mode is now enabled - clusterService.submitStateUpdateTask("ml-set-upgrade-mode", - new AckedClusterStateUpdateTask(request, clusterStateUpdateListener) { - - @Override - protected AcknowledgedResponse newResponse(boolean acknowledged) { - logger.trace("Cluster update response built: " + acknowledged); - return AcknowledgedResponse.of(acknowledged); - } - - @Override - public ClusterState execute(ClusterState currentState) throws Exception { - logger.trace("Executing cluster state update"); - MlMetadata.Builder builder = new MlMetadata.Builder(currentState.metadata().custom(MlMetadata.TYPE)); - builder.isUpgradeMode(request.isEnabled()); - ClusterState.Builder newState = ClusterState.builder(currentState); - newState.metadata(Metadata.builder(currentState.getMetadata()).putCustom(MlMetadata.TYPE, builder.build()).build()); - return newState.build(); - } - }); + // There are no tasks to worry about starting/stopping + if (tasksCustomMetadata == null || tasksCustomMetadata.tasks().isEmpty()) { + logger.info("No tasks to worry about after state update"); + wrappedListener.onResponse(AcknowledgedResponse.TRUE); + return; + } + + // Did we change from disabled -> enabled? + if (request.isEnabled()) { + logger.info("Enabling upgrade mode, must isolate datafeeds"); + isolateDatafeeds(tasksCustomMetadata, isolateDatafeedListener); + } else { + logger.info("Disabling upgrade mode, must wait for tasks to not have AWAITING_UPGRADE assignment"); + persistentTasksService.waitForPersistentTasksCondition( + // Wait for jobs, datafeeds and analytics not to be "Awaiting upgrade" + persistentTasksCustomMetadata -> persistentTasksCustomMetadata.tasks() + .stream() + .noneMatch(t -> ML_TASK_NAMES.contains(t.getTaskName()) && t.getAssignment().equals(AWAITING_UPGRADE)), + request.timeout(), + ActionListener.wrap(r -> { + logger.info("Done waiting for tasks to be out of AWAITING_UPGRADE"); + wrappedListener.onResponse(AcknowledgedResponse.TRUE); + }, wrappedListener::onFailure) + ); + } + }, wrappedListener::onFailure); + + // <1> Change MlMetadata to indicate that upgrade_mode is now enabled + clusterService.submitStateUpdateTask("ml-set-upgrade-mode", new AckedClusterStateUpdateTask(request, clusterStateUpdateListener) { + + @Override + protected AcknowledgedResponse newResponse(boolean acknowledged) { + logger.trace("Cluster update response built: " + acknowledged); + return AcknowledgedResponse.of(acknowledged); + } + + @Override + public ClusterState execute(ClusterState currentState) throws Exception { + logger.trace("Executing cluster state update"); + MlMetadata.Builder builder = new MlMetadata.Builder(currentState.metadata().custom(MlMetadata.TYPE)); + builder.isUpgradeMode(request.isEnabled()); + ClusterState.Builder newState = ClusterState.builder(currentState); + newState.metadata(Metadata.builder(currentState.getMetadata()).putCustom(MlMetadata.TYPE, builder.build()).build()); + return newState.build(); + } + }); } @Override @@ -264,10 +274,11 @@ protected ClusterBlockException checkBlock(SetUpgradeModeAction.Request request, * @param tasksCustomMetadata Current state of persistent tasks * @param listener Alerted when tasks are unassignd */ - private void unassignPersistentTasks(PersistentTasksCustomMetadata tasksCustomMetadata, - ActionListener>> listener) { - List> mlTasks = tasksCustomMetadata - .tasks() + private void unassignPersistentTasks( + PersistentTasksCustomMetadata tasksCustomMetadata, + ActionListener>> listener + ) { + List> mlTasks = tasksCustomMetadata.tasks() .stream() .filter(persistentTask -> ML_TASK_NAMES.contains(persistentTask.getTaskName())) // We want to always have the same ordering of which tasks we un-allocate first. @@ -275,41 +286,50 @@ private void unassignPersistentTasks(PersistentTasksCustomMetadata tasksCustomMe .sorted(Comparator.comparing(PersistentTask::getTaskName)) .collect(Collectors.toList()); - logger.info("Un-assigning persistent tasks : " + - mlTasks.stream().map(PersistentTask::getId).collect(Collectors.joining(", ", "[ ", " ]"))); + logger.info( + "Un-assigning persistent tasks : " + mlTasks.stream().map(PersistentTask::getId).collect(Collectors.joining(", ", "[ ", " ]")) + ); - TypedChainTaskExecutor> chainTaskExecutor = - new TypedChainTaskExecutor<>(client.threadPool().executor(executor), - r -> true, - // Another process could modify tasks and thus we cannot find them via the allocation_id and name - // If the task was removed from the node, all is well - // We handle the case of allocation_id changing later in this transport class by timing out waiting for task completion - // Consequently, if the exception is ResourceNotFoundException, continue execution; circuit break otherwise. - ex -> ExceptionsHelper.unwrapCause(ex) instanceof ResourceNotFoundException == false); + TypedChainTaskExecutor> chainTaskExecutor = new TypedChainTaskExecutor<>( + client.threadPool().executor(executor), + r -> true, + // Another process could modify tasks and thus we cannot find them via the allocation_id and name + // If the task was removed from the node, all is well + // We handle the case of allocation_id changing later in this transport class by timing out waiting for task completion + // Consequently, if the exception is ResourceNotFoundException, continue execution; circuit break otherwise. + ex -> ExceptionsHelper.unwrapCause(ex) instanceof ResourceNotFoundException == false + ); for (PersistentTask task : mlTasks) { chainTaskExecutor.add( - chainedTask -> persistentTasksClusterService.unassignPersistentTask(task.getId(), + chainedTask -> persistentTasksClusterService.unassignPersistentTask( + task.getId(), task.getAllocationId(), AWAITING_UPGRADE.getExplanation(), - chainedTask) + chainedTask + ) ); } chainTaskExecutor.execute(listener); } - private void isolateDatafeeds(PersistentTasksCustomMetadata tasksCustomMetadata, - ActionListener> listener) { + private void isolateDatafeeds( + PersistentTasksCustomMetadata tasksCustomMetadata, + ActionListener> listener + ) { Set datafeedsToIsolate = MlTasks.startedDatafeedIds(tasksCustomMetadata); logger.info("Isolating datafeeds: " + datafeedsToIsolate.toString()); - TypedChainTaskExecutor isolateDatafeedsExecutor = - new TypedChainTaskExecutor<>(client.threadPool().executor(executor), r -> true, ex -> true); + TypedChainTaskExecutor isolateDatafeedsExecutor = new TypedChainTaskExecutor<>( + client.threadPool().executor(executor), + r -> true, + ex -> true + ); datafeedsToIsolate.forEach(datafeedId -> { IsolateDatafeedAction.Request isolationRequest = new IsolateDatafeedAction.Request(datafeedId); - isolateDatafeedsExecutor.add(isolateListener -> - client.execute(IsolateDatafeedAction.INSTANCE, isolationRequest, isolateListener) + isolateDatafeedsExecutor.add( + isolateListener -> client.execute(IsolateDatafeedAction.INSTANCE, isolationRequest, isolateListener) ); }); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportStartDataFrameAnalyticsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportStartDataFrameAnalyticsAction.java index 344c39f1aa023..9f4adf3ce6089 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportStartDataFrameAnalyticsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportStartDataFrameAnalyticsAction.java @@ -99,8 +99,9 @@ /** * Starts the persistent task for running data frame analytics. */ -public class TransportStartDataFrameAnalyticsAction - extends TransportMasterNodeAction { +public class TransportStartDataFrameAnalyticsAction extends TransportMasterNodeAction< + StartDataFrameAnalyticsAction.Request, + NodeAcknowledgedResponse> { private static final Logger logger = LogManager.getLogger(TransportStartDataFrameAnalyticsAction.class); private static final String PRIMARY_SHARDS_INACTIVE = "not all primary shards are active"; @@ -114,14 +115,30 @@ public class TransportStartDataFrameAnalyticsAction private final SourceDestValidator sourceDestValidator; @Inject - public TransportStartDataFrameAnalyticsAction(TransportService transportService, Client client, ClusterService clusterService, - ThreadPool threadPool, ActionFilters actionFilters, XPackLicenseState licenseState, - IndexNameExpressionResolver indexNameExpressionResolver, - PersistentTasksService persistentTasksService, - DataFrameAnalyticsConfigProvider configProvider, MlMemoryTracker memoryTracker, - DataFrameAnalyticsAuditor auditor) { - super(StartDataFrameAnalyticsAction.NAME, transportService, clusterService, threadPool, actionFilters, - StartDataFrameAnalyticsAction.Request::new, indexNameExpressionResolver, NodeAcknowledgedResponse::new, ThreadPool.Names.SAME); + public TransportStartDataFrameAnalyticsAction( + TransportService transportService, + Client client, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + XPackLicenseState licenseState, + IndexNameExpressionResolver indexNameExpressionResolver, + PersistentTasksService persistentTasksService, + DataFrameAnalyticsConfigProvider configProvider, + MlMemoryTracker memoryTracker, + DataFrameAnalyticsAuditor auditor + ) { + super( + StartDataFrameAnalyticsAction.NAME, + transportService, + clusterService, + threadPool, + actionFilters, + StartDataFrameAnalyticsAction.Request::new, + indexNameExpressionResolver, + NodeAcknowledgedResponse::new, + ThreadPool.Names.SAME + ); this.licenseState = licenseState; this.client = client; this.persistentTasksService = persistentTasksService; @@ -148,8 +165,12 @@ protected ClusterBlockException checkBlock(StartDataFrameAnalyticsAction.Request } @Override - protected void masterOperation(Task task, StartDataFrameAnalyticsAction.Request request, ClusterState state, - ActionListener listener) { + protected void masterOperation( + Task task, + StartDataFrameAnalyticsAction.Request request, + ClusterState state, + ActionListener listener + ) { logger.debug(() -> new ParameterizedMessage("[{}] received start request", request.getId())); if (MachineLearningField.ML_API_FEATURE.check(licenseState) == false) { listener.onFailure(LicenseUtils.newComplianceException(XPackField.MACHINE_LEARNING)); @@ -157,42 +178,41 @@ protected void masterOperation(Task task, StartDataFrameAnalyticsAction.Request } // Wait for analytics to be started - ActionListener> waitForAnalyticsToStart = - new ActionListener>() { - @Override - public void onResponse(PersistentTasksCustomMetadata.PersistentTask task) { - waitForAnalyticsStarted(task, request.getTimeout(), listener); - } + ActionListener> waitForAnalyticsToStart = new ActionListener< + PersistentTasksCustomMetadata.PersistentTask>() { + @Override + public void onResponse(PersistentTasksCustomMetadata.PersistentTask task) { + waitForAnalyticsStarted(task, request.getTimeout(), listener); + } - @Override - public void onFailure(Exception e) { - if (ExceptionsHelper.unwrapCause(e) instanceof ResourceAlreadyExistsException) { - e = new ElasticsearchStatusException( - "Cannot start data frame analytics [{}] because it has already been started", - RestStatus.CONFLICT, - e, - request.getId()); - } - listener.onFailure(e); + @Override + public void onFailure(Exception e) { + if (ExceptionsHelper.unwrapCause(e) instanceof ResourceAlreadyExistsException) { + e = new ElasticsearchStatusException( + "Cannot start data frame analytics [{}] because it has already been started", + RestStatus.CONFLICT, + e, + request.getId() + ); } - }; + listener.onFailure(e); + } + }; // Start persistent task - ActionListener memoryUsageHandledListener = ActionListener.wrap( - startContext -> { - TaskParams taskParams = - new TaskParams( - request.getId(), - startContext.config.getVersion(), - startContext.config.isAllowLazyStart()); - persistentTasksService.sendStartRequest( - MlTasks.dataFrameAnalyticsTaskId(request.getId()), - MlTasks.DATA_FRAME_ANALYTICS_TASK_NAME, - taskParams, - waitForAnalyticsToStart); - }, - listener::onFailure - ); + ActionListener memoryUsageHandledListener = ActionListener.wrap(startContext -> { + TaskParams taskParams = new TaskParams( + request.getId(), + startContext.config.getVersion(), + startContext.config.isAllowLazyStart() + ); + persistentTasksService.sendStartRequest( + MlTasks.dataFrameAnalyticsTaskId(request.getId()), + MlTasks.DATA_FRAME_ANALYTICS_TASK_NAME, + taskParams, + waitForAnalyticsToStart + ); + }, listener::onFailure); // Perform memory usage estimation for this config ActionListener startContextListener = ActionListener.wrap( @@ -208,30 +228,28 @@ private void estimateMemoryUsageAndUpdateMemoryTracker(StartContext startContext final String jobId = startContext.config.getId(); // Tell the job tracker to refresh the memory requirement for this job and all other jobs that have persistent tasks - ActionListener explainListener = ActionListener.wrap( - explainResponse -> { - ByteSizeValue expectedMemoryWithoutDisk = explainResponse.getMemoryEstimation().getExpectedMemoryWithoutDisk(); - auditor.info(jobId, - Messages.getMessage(Messages.DATA_FRAME_ANALYTICS_AUDIT_ESTIMATED_MEMORY_USAGE, expectedMemoryWithoutDisk)); - // Validate that model memory limit is sufficient to run the analysis - // We will only warn the caller if the configured limit is too low. - if (startContext.config.getModelMemoryLimit() - .compareTo(expectedMemoryWithoutDisk) < 0) { - String warning = Messages.getMessage( - Messages.DATA_FRAME_ANALYTICS_AUDIT_ESTIMATED_MEMORY_USAGE_HIGHER_THAN_CONFIGURED, - startContext.config.getModelMemoryLimit(), - expectedMemoryWithoutDisk); - auditor.warning(jobId, warning); - logger.warn("[{}] {}", jobId, warning); - HeaderWarning.addWarning(DeprecationLogger.CRITICAL, warning); - } - // Refresh memory requirement for jobs - memoryTracker.addDataFrameAnalyticsJobMemoryAndRefreshAllOthers( - jobId, startContext.config.getModelMemoryLimit().getBytes(), ActionListener.wrap( - aVoid -> listener.onResponse(startContext), listener::onFailure)); - }, - listener::onFailure - ); + ActionListener explainListener = ActionListener.wrap(explainResponse -> { + ByteSizeValue expectedMemoryWithoutDisk = explainResponse.getMemoryEstimation().getExpectedMemoryWithoutDisk(); + auditor.info(jobId, Messages.getMessage(Messages.DATA_FRAME_ANALYTICS_AUDIT_ESTIMATED_MEMORY_USAGE, expectedMemoryWithoutDisk)); + // Validate that model memory limit is sufficient to run the analysis + // We will only warn the caller if the configured limit is too low. + if (startContext.config.getModelMemoryLimit().compareTo(expectedMemoryWithoutDisk) < 0) { + String warning = Messages.getMessage( + Messages.DATA_FRAME_ANALYTICS_AUDIT_ESTIMATED_MEMORY_USAGE_HIGHER_THAN_CONFIGURED, + startContext.config.getModelMemoryLimit(), + expectedMemoryWithoutDisk + ); + auditor.warning(jobId, warning); + logger.warn("[{}] {}", jobId, warning); + HeaderWarning.addWarning(DeprecationLogger.CRITICAL, warning); + } + // Refresh memory requirement for jobs + memoryTracker.addDataFrameAnalyticsJobMemoryAndRefreshAllOthers( + jobId, + startContext.config.getModelMemoryLimit().getBytes(), + ActionListener.wrap(aVoid -> listener.onResponse(startContext), listener::onFailure) + ); + }, listener::onFailure); PutDataFrameAnalyticsAction.Request explainRequest = new PutDataFrameAnalyticsAction.Request(startContext.config); ClientHelper.executeAsyncWithOrigin( @@ -239,7 +257,8 @@ private void estimateMemoryUsageAndUpdateMemoryTracker(StartContext startContext ClientHelper.ML_ORIGIN, ExplainDataFrameAnalyticsAction.INSTANCE, explainRequest, - explainListener); + explainListener + ); } @@ -254,71 +273,72 @@ private void getStartContext(String id, Task task, ActionListener // Step 6. Validate mappings can be merged ActionListener toValidateMappingsListener = ActionListener.wrap( - startContext -> MappingsMerger.mergeMappings(parentTaskClient, startContext.config.getHeaders(), - startContext.config.getSource(), ActionListener.wrap( - mappings -> validateMappingsMergeListener.onResponse(startContext), finalListener::onFailure)), + startContext -> MappingsMerger.mergeMappings( + parentTaskClient, + startContext.config.getHeaders(), + startContext.config.getSource(), + ActionListener.wrap(mappings -> validateMappingsMergeListener.onResponse(startContext), finalListener::onFailure) + ), finalListener::onFailure ); // Step 5. Validate dest index is empty if task is starting for first time - ActionListener toValidateDestEmptyListener = ActionListener.wrap( - startContext -> { - switch (startContext.startingState) { - case FIRST_TIME: - checkDestIndexIsEmptyIfExists(parentTaskClient, startContext, toValidateMappingsListener); - break; - case RESUMING_REINDEXING: - case RESUMING_ANALYZING: - case RESUMING_INFERENCE: - toValidateMappingsListener.onResponse(startContext); - break; - case FINISHED: - logger.info("[{}] Job has already finished", startContext.config.getId()); - finalListener.onFailure(ExceptionsHelper.badRequestException( - "Cannot start because the job has already finished")); - break; - default: - finalListener.onFailure(ExceptionsHelper.serverError( - "Unexpected starting state {}", - startContext.startingState)); - break; - } - }, - finalListener::onFailure - ); + ActionListener toValidateDestEmptyListener = ActionListener.wrap(startContext -> { + switch (startContext.startingState) { + case FIRST_TIME: + checkDestIndexIsEmptyIfExists(parentTaskClient, startContext, toValidateMappingsListener); + break; + case RESUMING_REINDEXING: + case RESUMING_ANALYZING: + case RESUMING_INFERENCE: + toValidateMappingsListener.onResponse(startContext); + break; + case FINISHED: + logger.info("[{}] Job has already finished", startContext.config.getId()); + finalListener.onFailure(ExceptionsHelper.badRequestException("Cannot start because the job has already finished")); + break; + default: + finalListener.onFailure(ExceptionsHelper.serverError("Unexpected starting state {}", startContext.startingState)); + break; + } + }, finalListener::onFailure); // Step 4. Check data extraction is possible - ActionListener toValidateExtractionPossibleListener = ActionListener.wrap( - startContext -> { - new ExtractedFieldsDetectorFactory(parentTaskClient).createFromSource(startContext.config, ActionListener.wrap( - extractedFieldsDetector -> { - startContext.extractedFields = extractedFieldsDetector.detect().v1(); - toValidateDestEmptyListener.onResponse(startContext); - }, - finalListener::onFailure) - ); - }, - finalListener::onFailure - ); + ActionListener toValidateExtractionPossibleListener = ActionListener.wrap(startContext -> { + new ExtractedFieldsDetectorFactory(parentTaskClient).createFromSource( + startContext.config, + ActionListener.wrap(extractedFieldsDetector -> { + startContext.extractedFields = extractedFieldsDetector.detect().v1(); + toValidateDestEmptyListener.onResponse(startContext); + }, finalListener::onFailure) + ); + }, finalListener::onFailure); // Step 3. Validate source and dest - ActionListener startContextListener = ActionListener.wrap( - startContext -> { - // Validate the query parses - startContext.config.getSource().getParsedQuery(); - - // Validate source/dest are valid - sourceDestValidator.validate(clusterService.state(), startContext.config.getSource().getIndex(), - startContext.config.getDest().getIndex(), null, SourceDestValidations.ALL_VALIDATIONS, ActionListener.wrap( - aBoolean -> toValidateExtractionPossibleListener.onResponse(startContext), finalListener::onFailure)); - }, - finalListener::onFailure - ); + ActionListener startContextListener = ActionListener.wrap(startContext -> { + // Validate the query parses + startContext.config.getSource().getParsedQuery(); + + // Validate source/dest are valid + sourceDestValidator.validate( + clusterService.state(), + startContext.config.getSource().getIndex(), + startContext.config.getDest().getIndex(), + null, + SourceDestValidations.ALL_VALIDATIONS, + ActionListener.wrap(aBoolean -> toValidateExtractionPossibleListener.onResponse(startContext), finalListener::onFailure) + ); + }, finalListener::onFailure); // Step 2. Get stats to recover progress ActionListener getConfigListener = ActionListener.wrap( - config -> getProgress(config, ActionListener.wrap( - progress -> startContextListener.onResponse(new StartContext(config, progress)), finalListener::onFailure)), + config -> getProgress( + config, + ActionListener.wrap( + progress -> startContextListener.onResponse(new StartContext(config, progress)), + finalListener::onFailure + ) + ), finalListener::onFailure ); @@ -336,20 +356,21 @@ private void validateSourceIndexHasAnalyzableData(StartContext startContext, Act } private void validateSourceIndexHasAtLeastOneAnalyzedField(StartContext startContext, ActionListener listener) { - Set requiredFields = startContext.config.getAnalysis().getRequiredFields().stream() + Set requiredFields = startContext.config.getAnalysis() + .getRequiredFields() + .stream() .map(RequiredField::getName) .collect(Collectors.toSet()); // We assume here that required fields are not features - long nonRequiredFieldsCount = startContext.extractedFields.getAllFields().stream() + long nonRequiredFieldsCount = startContext.extractedFields.getAllFields() + .stream() .filter(extractedField -> requiredFields.contains(extractedField.getName()) == false) .count(); if (nonRequiredFieldsCount == 0) { StringBuilder msgBuilder = new StringBuilder("at least one field must be included in the analysis"); if (requiredFields.isEmpty() == false) { - msgBuilder.append(" (excluding fields ") - .append(requiredFields) - .append(")"); + msgBuilder.append(" (excluding fields ").append(requiredFields).append(")"); } listener.onFailure(ExceptionsHelper.badRequestException(msgBuilder.toString())); } else { @@ -358,38 +379,46 @@ private void validateSourceIndexHasAtLeastOneAnalyzedField(StartContext startCon } private void validateSourceIndexRowsCount(StartContext startContext, ActionListener listener) { - DataFrameDataExtractorFactory extractorFactory = DataFrameDataExtractorFactory.createForSourceIndices(client, + DataFrameDataExtractorFactory extractorFactory = DataFrameDataExtractorFactory.createForSourceIndices( + client, "validate_source_index_has_rows-" + startContext.config.getId(), startContext.config, - startContext.extractedFields); - extractorFactory.newExtractor(false) - .collectDataSummaryAsync(ActionListener.wrap( - dataSummary -> { - if (dataSummary.rows == 0) { - listener.onFailure(ExceptionsHelper.badRequestException( - "Unable to start {} as no documents in the source indices [{}] contained all the fields " - + "selected for analysis. If you are relying on automatic field selection then there are " - + "currently mapped fields that do not exist in any indexed documents, and you will have " - + "to switch to explicit field selection and include only fields that exist in indexed " - + "documents.", - startContext.config.getId(), - Strings.arrayToCommaDelimitedString(startContext.config.getSource().getIndex()) - )); - } else if (Math.floor(startContext.config.getAnalysis().getTrainingPercent() * dataSummary.rows) >= Math.pow(2, 32)) { - listener.onFailure(ExceptionsHelper.badRequestException("Unable to start because too many documents " + - "(more than 2^32) are included in the analysis. Consider downsampling.")); - } else { - listener.onResponse(startContext); - } - }, - listener::onFailure - )); + startContext.extractedFields + ); + extractorFactory.newExtractor(false).collectDataSummaryAsync(ActionListener.wrap(dataSummary -> { + if (dataSummary.rows == 0) { + listener.onFailure( + ExceptionsHelper.badRequestException( + "Unable to start {} as no documents in the source indices [{}] contained all the fields " + + "selected for analysis. If you are relying on automatic field selection then there are " + + "currently mapped fields that do not exist in any indexed documents, and you will have " + + "to switch to explicit field selection and include only fields that exist in indexed " + + "documents.", + startContext.config.getId(), + Strings.arrayToCommaDelimitedString(startContext.config.getSource().getIndex()) + ) + ); + } else if (Math.floor(startContext.config.getAnalysis().getTrainingPercent() * dataSummary.rows) >= Math.pow(2, 32)) { + listener.onFailure( + ExceptionsHelper.badRequestException( + "Unable to start because too many documents " + + "(more than 2^32) are included in the analysis. Consider downsampling." + ) + ); + } else { + listener.onResponse(startContext); + } + }, listener::onFailure)); } private void getProgress(DataFrameAnalyticsConfig config, ActionListener> listener) { GetDataFrameAnalyticsStatsAction.Request getStatsRequest = new GetDataFrameAnalyticsStatsAction.Request(config.getId()); - executeAsyncWithOrigin(client, ML_ORIGIN, GetDataFrameAnalyticsStatsAction.INSTANCE, getStatsRequest, ActionListener.wrap( - statsResponse -> { + executeAsyncWithOrigin( + client, + ML_ORIGIN, + GetDataFrameAnalyticsStatsAction.INSTANCE, + getStatsRequest, + ActionListener.wrap(statsResponse -> { List stats = statsResponse.getResponse().results(); if (stats.isEmpty()) { // The job has been deleted in between @@ -397,41 +426,51 @@ private void getProgress(DataFrameAnalyticsConfig config, ActionListener listener) { + private void checkDestIndexIsEmptyIfExists( + ParentTaskAssigningClient parentTaskClient, + StartContext startContext, + ActionListener listener + ) { String destIndex = startContext.config.getDest().getIndex(); SearchRequest destEmptySearch = new SearchRequest(destIndex); destEmptySearch.source().size(0); destEmptySearch.allowPartialSearchResults(false); - ClientHelper.executeWithHeadersAsync(startContext.config.getHeaders(), ClientHelper.ML_ORIGIN, parentTaskClient, - SearchAction.INSTANCE, destEmptySearch, ActionListener.wrap( - searchResponse -> { - if (searchResponse.getHits().getTotalHits().value > 0) { - listener.onFailure(ExceptionsHelper.badRequestException("dest index [{}] must be empty", destIndex)); - } else { - listener.onResponse(startContext); - } - }, - e -> { - if (ExceptionsHelper.unwrapCause(e) instanceof IndexNotFoundException) { - listener.onResponse(startContext); - } else { - listener.onFailure(e); - } + ClientHelper.executeWithHeadersAsync( + startContext.config.getHeaders(), + ClientHelper.ML_ORIGIN, + parentTaskClient, + SearchAction.INSTANCE, + destEmptySearch, + ActionListener.wrap(searchResponse -> { + if (searchResponse.getHits().getTotalHits().value > 0) { + listener.onFailure(ExceptionsHelper.badRequestException("dest index [{}] must be empty", destIndex)); + } else { + listener.onResponse(startContext); + } + }, e -> { + if (ExceptionsHelper.unwrapCause(e) instanceof IndexNotFoundException) { + listener.onResponse(startContext); + } else { + listener.onFailure(e); } - ) + }) ); } - private void waitForAnalyticsStarted(PersistentTasksCustomMetadata.PersistentTask task, - TimeValue timeout, ActionListener listener) { + private void waitForAnalyticsStarted( + PersistentTasksCustomMetadata.PersistentTask task, + TimeValue timeout, + ActionListener listener + ) { AnalyticsPredicate predicate = new AnalyticsPredicate(); - persistentTasksService.waitForPersistentTaskCondition(task.getId(), predicate, timeout, + persistentTasksService.waitForPersistentTaskCondition( + task.getId(), + predicate, + timeout, new PersistentTasksService.WaitForPersistentTaskListener() { @@ -455,27 +494,37 @@ public void onFailure(Exception e) { @Override public void onTimeout(TimeValue timeout) { logger.error( - new ParameterizedMessage("[{}] timed out when starting task after [{}]. Assignment explanation [{}]", + new ParameterizedMessage( + "[{}] timed out when starting task after [{}]. Assignment explanation [{}]", task.getParams().getId(), timeout, - predicate.assignmentExplanation)); + predicate.assignmentExplanation + ) + ); if (predicate.assignmentExplanation != null) { - cancelAnalyticsStart(task, + cancelAnalyticsStart( + task, new ElasticsearchStatusException( "Could not start data frame analytics task, timed out after [{}] waiting for task assignment. " + "Assignment explanation [{}]", RestStatus.TOO_MANY_REQUESTS, timeout, - predicate.assignmentExplanation), - listener); + predicate.assignmentExplanation + ), + listener + ); } else { - listener.onFailure(new ElasticsearchException( - "Starting data frame analytics [{}] timed out after [{}]", - task.getParams().getId(), - timeout)); + listener.onFailure( + new ElasticsearchException( + "Starting data frame analytics [{}] timed out after [{}]", + task.getParams().getId(), + timeout + ) + ); } } - }); + } + ); } private static class StartContext { @@ -525,7 +574,8 @@ public boolean test(PersistentTasksCustomMetadata.PersistentTask persistentTa exception = new ElasticsearchStatusException( "Could not start data frame analytics task, allocation explanation [{}]", RestStatus.TOO_MANY_REQUESTS, - assignment.getExplanation()); + assignment.getExplanation() + ); return true; } DataFrameAnalyticsTaskState taskState = (DataFrameAnalyticsTaskState) persistentTask.getState(); @@ -540,7 +590,7 @@ public boolean test(PersistentTasksCustomMetadata.PersistentTask persistentTa return true; // The STARTING case here is expected to be incredibly short-lived, just occurring during the // time period when a job has successfully been assigned to a node but the request to update - // its task state is still in-flight. (The long-lived STARTING case when a lazy node needs to + // its task state is still in-flight. (The long-lived STARTING case when a lazy node needs to // be added to the cluster to accommodate the job was dealt with higher up this method when the // magic AWAITING_LAZY_ASSIGNMENT assignment was checked for.) case STARTING: @@ -551,16 +601,20 @@ public boolean test(PersistentTasksCustomMetadata.PersistentTask persistentTa exception = ExceptionsHelper.serverError( "Unexpected task state [{}] {}while waiting to be started", analyticsState, - reason == null ? "" : "with reason [" + reason + "] "); + reason == null ? "" : "with reason [" + reason + "] " + ); return true; } } } private void cancelAnalyticsStart( - PersistentTasksCustomMetadata.PersistentTask persistentTask, Exception exception, - ActionListener listener) { - persistentTasksService.sendRemoveRequest(persistentTask.getId(), + PersistentTasksCustomMetadata.PersistentTask persistentTask, + Exception exception, + ActionListener listener + ) { + persistentTasksService.sendRemoveRequest( + persistentTask.getId(), new ActionListener>() { @Override public void onResponse(PersistentTasksCustomMetadata.PersistentTask task) { @@ -575,8 +629,10 @@ public void onFailure(Exception e) { new ParameterizedMessage( "[{}] Failed to cancel persistent task that could not be assigned due to [{}]", persistentTask.getParams().getId(), - exception.getMessage()), - e); + exception.getMessage() + ), + e + ); listener.onFailure(exception); } } @@ -592,15 +648,24 @@ public static class TaskExecutor extends AbstractJobPersistentTasksExecutor persistentTask, - Map headers) { + Map headers + ) { return new DataFrameAnalyticsTask( - id, type, action, parentTaskId, headers, client, manager, auditor, persistentTask.getParams(), licenseState); + id, + type, + action, + parentTaskId, + headers, + client, + manager, + auditor, + persistentTask.getParams(), + licenseState + ); } @Override - public PersistentTasksCustomMetadata.Assignment getAssignment(TaskParams params, - Collection candidateNodes, - ClusterState clusterState) { + public PersistentTasksCustomMetadata.Assignment getAssignment( + TaskParams params, + Collection candidateNodes, + ClusterState clusterState + ) { boolean isMemoryTrackerRecentlyRefreshed = memoryTracker.isRecentlyRefreshed(); - Optional optionalAssignment = - getPotentialAssignment(params, clusterState, isMemoryTrackerRecentlyRefreshed); + Optional optionalAssignment = getPotentialAssignment( + params, + clusterState, + isMemoryTrackerRecentlyRefreshed + ); // NOTE: this will return here if isMemoryTrackerRecentlyRefreshed is false, we don't allow assignment with stale memory if (optionalAssignment.isPresent()) { return optionalAssignment.get(); } - JobNodeSelector jobNodeSelector = - new JobNodeSelector( - clusterState, - candidateNodes, - params.getId(), - MlTasks.DATA_FRAME_ANALYTICS_TASK_NAME, - memoryTracker, - params.isAllowLazyStart() ? Integer.MAX_VALUE : maxLazyMLNodes, - node -> nodeFilter(node, params)); + JobNodeSelector jobNodeSelector = new JobNodeSelector( + clusterState, + candidateNodes, + params.getId(), + MlTasks.DATA_FRAME_ANALYTICS_TASK_NAME, + memoryTracker, + params.isAllowLazyStart() ? Integer.MAX_VALUE : maxLazyMLNodes, + node -> nodeFilter(node, params) + ); // Pass an effectively infinite value for max concurrent opening jobs, because data frame analytics jobs do // not have an "opening" state so would never be rejected for causing too many jobs in the "opening" state PersistentTasksCustomMetadata.Assignment assignment = jobNodeSelector.selectNode( @@ -654,7 +738,8 @@ public PersistentTasksCustomMetadata.Assignment getAssignment(TaskParams params, protected void nodeOperation(AllocatedPersistentTask task, TaskParams params, PersistentTaskState state) { DataFrameAnalyticsTask dfaTask = (DataFrameAnalyticsTask) task; DataFrameAnalyticsTaskState analyticsTaskState = (DataFrameAnalyticsTaskState) state; - DataFrameAnalyticsState analyticsState = analyticsTaskState == null ? DataFrameAnalyticsState.STOPPED + DataFrameAnalyticsState analyticsState = analyticsTaskState == null + ? DataFrameAnalyticsState.STOPPED : analyticsTaskState.getState(); logger.info("[{}] Starting data frame analytics from state [{}]", params.getId(), analyticsState); @@ -670,60 +755,88 @@ protected void nodeOperation(AllocatedPersistentTask task, TaskParams params, Pe } // Execute task - ActionListener statsListener = ActionListener.wrap( - statsResponse -> { - GetDataFrameAnalyticsStatsAction.Response.Stats stats = statsResponse.getResponse().results().get(0); - dfaTask.setStatsHolder( - new StatsHolder(stats.getProgress(), stats.getMemoryUsage(), stats.getAnalysisStats(), stats.getDataCounts())); - executeTask(dfaTask); - }, - dfaTask::setFailed - ); + ActionListener statsListener = ActionListener.wrap(statsResponse -> { + GetDataFrameAnalyticsStatsAction.Response.Stats stats = statsResponse.getResponse().results().get(0); + dfaTask.setStatsHolder( + new StatsHolder(stats.getProgress(), stats.getMemoryUsage(), stats.getAnalysisStats(), stats.getDataCounts()) + ); + executeTask(dfaTask); + }, dfaTask::setFailed); // Get stats to initialize in memory stats tracking ActionListener indexCheckListener = ActionListener.wrap( - ok -> executeAsyncWithOrigin(client, ML_ORIGIN, GetDataFrameAnalyticsStatsAction.INSTANCE, - new GetDataFrameAnalyticsStatsAction.Request(params.getId()), statsListener), + ok -> executeAsyncWithOrigin( + client, + ML_ORIGIN, + GetDataFrameAnalyticsStatsAction.INSTANCE, + new GetDataFrameAnalyticsStatsAction.Request(params.getId()), + statsListener + ), error -> { Throwable cause = ExceptionsHelper.unwrapCause(error); logger.error( new ParameterizedMessage( "[{}] failed to create internal index [{}]", params.getId(), - InferenceIndexConstants.LATEST_INDEX_NAME), - cause); + InferenceIndexConstants.LATEST_INDEX_NAME + ), + cause + ); dfaTask.setFailed(error); } ); - // Create the system index explicitly. Although the master node would create it automatically on first use, + // Create the system index explicitly. Although the master node would create it automatically on first use, // in a mixed version cluster where the master node is on an older version than this node relying on auto-creation // might use outdated mappings. - MlIndexAndAlias.createSystemIndexIfNecessary(client, clusterState, MachineLearning.getInferenceIndexSystemIndexDescriptor(), - MlTasks.PERSISTENT_TASK_MASTER_NODE_TIMEOUT, indexCheckListener); + MlIndexAndAlias.createSystemIndexIfNecessary( + client, + clusterState, + MachineLearning.getInferenceIndexSystemIndexDescriptor(), + MlTasks.PERSISTENT_TASK_MASTER_NODE_TIMEOUT, + indexCheckListener + ); } private void executeTask(DataFrameAnalyticsTask task) { - DataFrameAnalyticsTaskState startedState = new DataFrameAnalyticsTaskState(DataFrameAnalyticsState.STARTED, - task.getAllocationId(), null); - task.updatePersistentTaskState(startedState, ActionListener.wrap( - response -> manager.execute(task, clusterState, MlTasks.PERSISTENT_TASK_MASTER_NODE_TIMEOUT), - task::markAsFailed)); + DataFrameAnalyticsTaskState startedState = new DataFrameAnalyticsTaskState( + DataFrameAnalyticsState.STARTED, + task.getAllocationId(), + null + ); + task.updatePersistentTaskState( + startedState, + ActionListener.wrap( + response -> manager.execute(task, clusterState, MlTasks.PERSISTENT_TASK_MASTER_NODE_TIMEOUT), + task::markAsFailed + ) + ); } public static String nodeFilter(DiscoveryNode node, TaskParams params) { String id = params.getId(); if (node.getVersion().before(TaskParams.VERSION_INTRODUCED)) { - return "Not opening job [" + id + "] on node [" + JobNodeSelector.nodeNameAndVersion(node) + return "Not opening job [" + + id + + "] on node [" + + JobNodeSelector.nodeNameAndVersion(node) + "], because the data frame analytics requires a node of version [" - + TaskParams.VERSION_INTRODUCED + "] or higher"; + + TaskParams.VERSION_INTRODUCED + + "] or higher"; } if (node.getVersion().before(TaskParams.VERSION_DESTINATION_INDEX_MAPPINGS_CHANGED) && params.getVersion().onOrAfter(TaskParams.VERSION_DESTINATION_INDEX_MAPPINGS_CHANGED)) { - return "Not opening job [" + id + "] on node [" + JobNodeSelector.nodeNameAndVersion(node) - + "], because the data frame analytics created for version [" + params.getVersion() + "] requires a node of version " - + "[" + TaskParams.VERSION_DESTINATION_INDEX_MAPPINGS_CHANGED + "] or higher"; + return "Not opening job [" + + id + + "] on node [" + + JobNodeSelector.nodeNameAndVersion(node) + + "], because the data frame analytics created for version [" + + params.getVersion() + + "] requires a node of version " + + "[" + + TaskParams.VERSION_DESTINATION_INDEX_MAPPINGS_CHANGED + + "] or higher"; } return null; @@ -731,9 +844,7 @@ public static String nodeFilter(DiscoveryNode node, TaskParams params) { @Override protected String[] indicesOfInterest(TaskParams params) { - return new String[]{MlConfigIndex.indexName(), - MlStatsIndex.indexPattern(), - AnomalyDetectorsIndex.jobStateIndexPattern()}; + return new String[] { MlConfigIndex.indexName(), MlStatsIndex.indexPattern(), AnomalyDetectorsIndex.jobStateIndexPattern() }; } @Override diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportStartDatafeedAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportStartDatafeedAction.java index 1521d5b9cd1be..e25e52d6d3a07 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportStartDatafeedAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportStartDatafeedAction.java @@ -22,13 +22,12 @@ import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.core.Tuple; import org.elasticsearch.license.LicenseUtils; import org.elasticsearch.license.RemoteClusterLicenseChecker; import org.elasticsearch.license.XPackLicenseState; @@ -43,6 +42,7 @@ import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.RemoteClusterService; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.XPackField; import org.elasticsearch.xpack.core.ml.MachineLearningField; import org.elasticsearch.xpack.core.ml.MlTasks; @@ -59,8 +59,8 @@ import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; import org.elasticsearch.xpack.ml.MachineLearning; import org.elasticsearch.xpack.ml.MlConfigMigrationEligibilityCheck; -import org.elasticsearch.xpack.ml.datafeed.DatafeedRunner; import org.elasticsearch.xpack.ml.datafeed.DatafeedNodeSelector; +import org.elasticsearch.xpack.ml.datafeed.DatafeedRunner; import org.elasticsearch.xpack.ml.datafeed.DatafeedTimingStatsReporter; import org.elasticsearch.xpack.ml.datafeed.extractor.DataExtractorFactory; import org.elasticsearch.xpack.ml.datafeed.persistence.DatafeedConfigProvider; @@ -106,14 +106,32 @@ public class TransportStartDatafeedAction extends TransportMasterNodeAction deprecationWarnings = new ArrayList<>(); deprecationWarnings.addAll(datafeed.getAggDeprecations(xContentRegistry)); deprecationWarnings.addAll(datafeed.getQueryDeprecations(xContentRegistry)); if (deprecationWarnings.isEmpty() == false) { - String msg = "datafeed [" + datafeed.getId() +"] configuration has deprecations. [" + - Strings.collectionToDelimitedString(deprecationWarnings, ", ") + "]"; + String msg = "datafeed [" + + datafeed.getId() + + "] configuration has deprecations. [" + + Strings.collectionToDelimitedString(deprecationWarnings, ", ") + + "]"; auditor.warning(job.getId(), msg); } } @Override - protected void masterOperation(Task task, StartDatafeedAction.Request request, ClusterState state, - ActionListener listener) { + protected void masterOperation( + Task task, + StartDatafeedAction.Request request, + ClusterState state, + ActionListener listener + ) { StartDatafeedAction.DatafeedParams params = request.getParams(); if (MachineLearningField.ML_API_FEATURE.check(licenseState) == false) { listener.onFailure(LicenseUtils.newComplianceException(XPackField.MACHINE_LEARNING)); @@ -170,121 +202,129 @@ protected void masterOperation(Task task, StartDatafeedAction.Request request, C PersistentTasksCustomMetadata tasks = state.getMetadata().custom(PersistentTasksCustomMetadata.TYPE); ActionListener> waitForTaskListener = - new ActionListener>() { - @Override - public void onResponse(PersistentTasksCustomMetadata.PersistentTask - persistentTask) { - waitForDatafeedStarted(persistentTask.getId(), params, listener); - } + new ActionListener>() { + @Override + public void onResponse(PersistentTasksCustomMetadata.PersistentTask persistentTask) { + waitForDatafeedStarted(persistentTask.getId(), params, listener); + } - @Override - public void onFailure(Exception e) { - if (ExceptionsHelper.unwrapCause(e) instanceof ResourceAlreadyExistsException) { - logger.debug("datafeed already started", e); - e = new ElasticsearchStatusException("cannot start datafeed [" + params.getDatafeedId() + - "] because it has already been started", RestStatus.CONFLICT); - } - listener.onFailure(e); + @Override + public void onFailure(Exception e) { + if (ExceptionsHelper.unwrapCause(e) instanceof ResourceAlreadyExistsException) { + logger.debug("datafeed already started", e); + e = new ElasticsearchStatusException( + "cannot start datafeed [" + params.getDatafeedId() + "] because it has already been started", + RestStatus.CONFLICT + ); } - }; + listener.onFailure(e); + } + }; // Verify data extractor factory can be created, then start persistent task Consumer createDataExtractor = job -> { final List remoteIndices = RemoteClusterLicenseChecker.remoteIndices(params.getDatafeedIndices()); - if (remoteIndices.isEmpty() == false) { - final RemoteClusterLicenseChecker remoteClusterLicenseChecker = - new RemoteClusterLicenseChecker(client, XPackLicenseState::isMachineLearningAllowedForOperationMode); - remoteClusterLicenseChecker.checkRemoteClusterLicenses( - RemoteClusterLicenseChecker.remoteClusterAliases( - transportService.getRemoteClusterService().getRegisteredRemoteClusterNames(), - params.getDatafeedIndices()), - ActionListener.wrap( - response -> { - if (response.isSuccess() == false) { - listener.onFailure(createUnlicensedError(params.getDatafeedId(), response)); - } else if (remoteClusterClient == false) { - listener.onFailure( - ExceptionsHelper.badRequestException(Messages.getMessage( - Messages.DATAFEED_NEEDS_REMOTE_CLUSTER_SEARCH, - datafeedConfigHolder.get().getId(), - RemoteClusterLicenseChecker.remoteIndices(datafeedConfigHolder.get().getIndices()), - clusterService.getNodeName()))); - } else { - final RemoteClusterService remoteClusterService = transportService.getRemoteClusterService(); - List remoteAliases = RemoteClusterLicenseChecker.remoteClusterAliases( - remoteClusterService.getRegisteredRemoteClusterNames(), - remoteIndices - ); - checkRemoteClusterVersions( - datafeedConfigHolder.get(), - remoteAliases, - (cn) -> remoteClusterService.getConnection(cn).getVersion() - ); - createDataExtractor(job, datafeedConfigHolder.get(), params, waitForTaskListener); - } - }, - e -> listener.onFailure( - createUnknownLicenseError( - params.getDatafeedId(), - RemoteClusterLicenseChecker.remoteIndices(params.getDatafeedIndices()), e)) - ) - ); - } else { - createDataExtractor(job, datafeedConfigHolder.get(), params, waitForTaskListener); - } - }; - - ActionListener jobListener = ActionListener.wrap( - jobBuilder -> { - try { - Job job = jobBuilder.build(); - validate(job, datafeedConfigHolder.get(), tasks, xContentRegistry); - auditDeprecations(datafeedConfigHolder.get(), job, auditor, xContentRegistry); - createDataExtractor.accept(job); - } catch (Exception e) { - listener.onFailure(e); - } - }, - listener::onFailure - ); - - ActionListener datafeedListener = ActionListener.wrap( - datafeedBuilder -> { - try { - DatafeedConfig datafeedConfig = datafeedBuilder.build(); - params.setDatafeedIndices(datafeedConfig.getIndices()); - params.setJobId(datafeedConfig.getJobId()); - params.setIndicesOptions(datafeedConfig.getIndicesOptions()); - datafeedConfigHolder.set(datafeedConfig); - if (datafeedConfig.hasCompositeAgg(xContentRegistry)) { - if (state.nodes() - .mastersFirstStream() - .filter(MachineLearning::isMlNode) - .map(DiscoveryNode::getVersion) - .anyMatch(COMPOSITE_AGG_SUPPORT::after)) { - listener.onFailure(ExceptionsHelper.badRequestException( - "cannot start datafeed [{}] as [{}] requires all machine learning nodes to be at least version [{}]", - datafeedConfig.getId(), - "composite aggs", - COMPOSITE_AGG_SUPPORT - )); - return; - } + if (remoteIndices.isEmpty() == false) { + final RemoteClusterLicenseChecker remoteClusterLicenseChecker = new RemoteClusterLicenseChecker( + client, + XPackLicenseState::isMachineLearningAllowedForOperationMode + ); + remoteClusterLicenseChecker.checkRemoteClusterLicenses( + RemoteClusterLicenseChecker.remoteClusterAliases( + transportService.getRemoteClusterService().getRegisteredRemoteClusterNames(), + params.getDatafeedIndices() + ), + ActionListener.wrap(response -> { + if (response.isSuccess() == false) { + listener.onFailure(createUnlicensedError(params.getDatafeedId(), response)); + } else if (remoteClusterClient == false) { + listener.onFailure( + ExceptionsHelper.badRequestException( + Messages.getMessage( + Messages.DATAFEED_NEEDS_REMOTE_CLUSTER_SEARCH, + datafeedConfigHolder.get().getId(), + RemoteClusterLicenseChecker.remoteIndices(datafeedConfigHolder.get().getIndices()), + clusterService.getNodeName() + ) + ) + ); + } else { + final RemoteClusterService remoteClusterService = transportService.getRemoteClusterService(); + List remoteAliases = RemoteClusterLicenseChecker.remoteClusterAliases( + remoteClusterService.getRegisteredRemoteClusterNames(), + remoteIndices + ); + checkRemoteClusterVersions( + datafeedConfigHolder.get(), + remoteAliases, + (cn) -> remoteClusterService.getConnection(cn).getVersion() + ); + createDataExtractor(job, datafeedConfigHolder.get(), params, waitForTaskListener); } - jobConfigProvider.getJob(datafeedConfig.getJobId(), jobListener); - } catch (Exception e) { - listener.onFailure(e); + }, + e -> listener.onFailure( + createUnknownLicenseError( + params.getDatafeedId(), + RemoteClusterLicenseChecker.remoteIndices(params.getDatafeedIndices()), + e + ) + ) + ) + ); + } else { + createDataExtractor(job, datafeedConfigHolder.get(), params, waitForTaskListener); + } + }; + + ActionListener jobListener = ActionListener.wrap(jobBuilder -> { + try { + Job job = jobBuilder.build(); + validate(job, datafeedConfigHolder.get(), tasks, xContentRegistry); + auditDeprecations(datafeedConfigHolder.get(), job, auditor, xContentRegistry); + createDataExtractor.accept(job); + } catch (Exception e) { + listener.onFailure(e); + } + }, listener::onFailure); + + ActionListener datafeedListener = ActionListener.wrap(datafeedBuilder -> { + try { + DatafeedConfig datafeedConfig = datafeedBuilder.build(); + params.setDatafeedIndices(datafeedConfig.getIndices()); + params.setJobId(datafeedConfig.getJobId()); + params.setIndicesOptions(datafeedConfig.getIndicesOptions()); + datafeedConfigHolder.set(datafeedConfig); + if (datafeedConfig.hasCompositeAgg(xContentRegistry)) { + if (state.nodes() + .mastersFirstStream() + .filter(MachineLearning::isMlNode) + .map(DiscoveryNode::getVersion) + .anyMatch(COMPOSITE_AGG_SUPPORT::after)) { + listener.onFailure( + ExceptionsHelper.badRequestException( + "cannot start datafeed [{}] as [{}] requires all machine learning nodes to be at least version [{}]", + datafeedConfig.getId(), + "composite aggs", + COMPOSITE_AGG_SUPPORT + ) + ); + return; } - }, - listener::onFailure - ); + } + jobConfigProvider.getJob(datafeedConfig.getJobId(), jobListener); + } catch (Exception e) { + listener.onFailure(e); + } + }, listener::onFailure); datafeedConfigProvider.getDatafeedConfig(params.getDatafeedId(), datafeedListener); } - static void checkRemoteClusterVersions(DatafeedConfig config, - List remoteClusters, - Function clusterVersionSupplier) { + static void checkRemoteClusterVersions( + DatafeedConfig config, + List remoteClusters, + Function clusterVersionSupplier + ) { Optional> minVersionAndReason = config.minRequiredClusterVersion(); if (minVersionAndReason.isPresent() == false) { return; @@ -310,9 +350,12 @@ static void checkRemoteClusterVersions(DatafeedConfig config, } /** Creates {@link DataExtractorFactory} solely for the purpose of validation i.e. verifying that it can be created. */ - private void createDataExtractor(Job job, DatafeedConfig datafeed, StartDatafeedAction.DatafeedParams params, - ActionListener> - listener) { + private void createDataExtractor( + Job job, + DatafeedConfig datafeed, + StartDatafeedAction.DatafeedParams params, + ActionListener> listener + ) { DataExtractorFactory.create( client, datafeed, @@ -321,10 +364,15 @@ private void createDataExtractor(Job job, DatafeedConfig datafeed, StartDatafeed // Fake DatafeedTimingStatsReporter that does not have access to results index new DatafeedTimingStatsReporter(new DatafeedTimingStats(job.getId()), (ts, refreshPolicy) -> {}), ActionListener.wrap( - unused -> - persistentTasksService.sendStartRequest( - MlTasks.datafeedTaskId(params.getDatafeedId()), MlTasks.DATAFEED_TASK_NAME, params, listener), - listener::onFailure)); + unused -> persistentTasksService.sendStartRequest( + MlTasks.datafeedTaskId(params.getDatafeedId()), + MlTasks.DATAFEED_TASK_NAME, + params, + listener + ), + listener::onFailure + ) + ); } @Override @@ -335,85 +383,113 @@ protected ClusterBlockException checkBlock(StartDatafeedAction.Request request, return state.blocks().globalBlockedException(ClusterBlockLevel.METADATA_WRITE); } - private void waitForDatafeedStarted(String taskId, StartDatafeedAction.DatafeedParams params, - ActionListener listener) { + private void waitForDatafeedStarted( + String taskId, + StartDatafeedAction.DatafeedParams params, + ActionListener listener + ) { DatafeedPredicate predicate = new DatafeedPredicate(); - persistentTasksService.waitForPersistentTaskCondition(taskId, predicate, params.getTimeout(), - new PersistentTasksService.WaitForPersistentTaskListener() { - @Override - public void onResponse(PersistentTasksCustomMetadata.PersistentTask - persistentTask) { - if (predicate.exception != null) { - // We want to return to the caller without leaving an unassigned persistent task, to match - // what would have happened if the error had been detected in the "fast fail" validation - cancelDatafeedStart(persistentTask, predicate.exception, listener); - } else { - listener.onResponse(new NodeAcknowledgedResponse(true, predicate.node)); - } + persistentTasksService.waitForPersistentTaskCondition( + taskId, + predicate, + params.getTimeout(), + new PersistentTasksService.WaitForPersistentTaskListener() { + @Override + public void onResponse(PersistentTasksCustomMetadata.PersistentTask persistentTask) { + if (predicate.exception != null) { + // We want to return to the caller without leaving an unassigned persistent task, to match + // what would have happened if the error had been detected in the "fast fail" validation + cancelDatafeedStart(persistentTask, predicate.exception, listener); + } else { + listener.onResponse(new NodeAcknowledgedResponse(true, predicate.node)); } + } - @Override - public void onFailure(Exception e) { - listener.onFailure(e); - } + @Override + public void onFailure(Exception e) { + listener.onFailure(e); + } - @Override - public void onTimeout(TimeValue timeout) { - listener.onFailure(new ElasticsearchException("Starting datafeed [" - + params.getDatafeedId() + "] timed out after [" + timeout + "]")); - } - }); + @Override + public void onTimeout(TimeValue timeout) { + listener.onFailure( + new ElasticsearchException("Starting datafeed [" + params.getDatafeedId() + "] timed out after [" + timeout + "]") + ); + } + } + ); } - private void cancelDatafeedStart(PersistentTasksCustomMetadata.PersistentTask persistentTask, - Exception exception, ActionListener listener) { - persistentTasksService.sendRemoveRequest(persistentTask.getId(), - new ActionListener>() { - @Override - public void onResponse(PersistentTasksCustomMetadata.PersistentTask task) { - // We succeeded in cancelling the persistent task, but the - // problem that caused us to cancel it is the overall result - listener.onFailure(exception); - } + private void cancelDatafeedStart( + PersistentTasksCustomMetadata.PersistentTask persistentTask, + Exception exception, + ActionListener listener + ) { + persistentTasksService.sendRemoveRequest( + persistentTask.getId(), + new ActionListener>() { + @Override + public void onResponse(PersistentTasksCustomMetadata.PersistentTask task) { + // We succeeded in cancelling the persistent task, but the + // problem that caused us to cancel it is the overall result + listener.onFailure(exception); + } - @Override - public void onFailure(Exception e) { - logger.error("[" + persistentTask.getParams().getDatafeedId() + "] Failed to cancel persistent task that could " + - "not be assigned due to [" + exception.getMessage() + "]", e); - listener.onFailure(exception); - } + @Override + public void onFailure(Exception e) { + logger.error( + "[" + + persistentTask.getParams().getDatafeedId() + + "] Failed to cancel persistent task that could " + + "not be assigned due to [" + + exception.getMessage() + + "]", + e + ); + listener.onFailure(exception); } + } ); } private ElasticsearchStatusException createUnlicensedError( - final String datafeedId, final RemoteClusterLicenseChecker.LicenseCheck licenseCheck) { + final String datafeedId, + final RemoteClusterLicenseChecker.LicenseCheck licenseCheck + ) { final String message = String.format( - Locale.ROOT, - "cannot start datafeed [%s] as it is configured to use indices on remote cluster [%s] that is not licensed for ml; %s", - datafeedId, - licenseCheck.remoteClusterLicenseInfo().clusterAlias(), - RemoteClusterLicenseChecker.buildErrorMessage( - "ml", - licenseCheck.remoteClusterLicenseInfo(), - RemoteClusterLicenseChecker::isAllowedByLicense)); + Locale.ROOT, + "cannot start datafeed [%s] as it is configured to use indices on remote cluster [%s] that is not licensed for ml; %s", + datafeedId, + licenseCheck.remoteClusterLicenseInfo().clusterAlias(), + RemoteClusterLicenseChecker.buildErrorMessage( + "ml", + licenseCheck.remoteClusterLicenseInfo(), + RemoteClusterLicenseChecker::isAllowedByLicense + ) + ); return new ElasticsearchStatusException(message, RestStatus.BAD_REQUEST); } private ElasticsearchStatusException createUnknownLicenseError( - final String datafeedId, final List remoteIndices, final Exception cause) { + final String datafeedId, + final List remoteIndices, + final Exception cause + ) { final int numberOfRemoteClusters = RemoteClusterLicenseChecker.remoteClusterAliases( - transportService.getRemoteClusterService().getRegisteredRemoteClusterNames(), remoteIndices).size(); + transportService.getRemoteClusterService().getRegisteredRemoteClusterNames(), + remoteIndices + ).size(); assert numberOfRemoteClusters > 0; final String remoteClusterQualifier = numberOfRemoteClusters == 1 ? "a remote cluster" : "remote clusters"; final String licenseTypeQualifier = numberOfRemoteClusters == 1 ? "" : "s"; final String message = String.format( - Locale.ROOT, - "cannot start datafeed [%s] as it uses indices on %s %s but the license type%s could not be verified", - datafeedId, - remoteClusterQualifier, - remoteIndices, - licenseTypeQualifier); + Locale.ROOT, + "cannot start datafeed [%s] as it uses indices on %s %s but the license type%s could not be verified", + datafeedId, + remoteClusterQualifier, + remoteIndices, + licenseTypeQualifier + ); return new ElasticsearchStatusException(message, RestStatus.BAD_REQUEST, cause); } @@ -429,32 +505,43 @@ public StartDatafeedPersistentTasksExecutor(DatafeedRunner datafeedRunner, Index } @Override - public PersistentTasksCustomMetadata.Assignment getAssignment(StartDatafeedAction.DatafeedParams params, - Collection candidateNodes, - ClusterState clusterState) { - return new DatafeedNodeSelector(clusterState, resolver, params.getDatafeedId(), params.getJobId(), - params.getDatafeedIndices(), params.getIndicesOptions()).selectNode(candidateNodes); + public PersistentTasksCustomMetadata.Assignment getAssignment( + StartDatafeedAction.DatafeedParams params, + Collection candidateNodes, + ClusterState clusterState + ) { + return new DatafeedNodeSelector( + clusterState, + resolver, + params.getDatafeedId(), + params.getJobId(), + params.getDatafeedIndices(), + params.getIndicesOptions() + ).selectNode(candidateNodes); } @Override public void validate(StartDatafeedAction.DatafeedParams params, ClusterState clusterState) { - new DatafeedNodeSelector(clusterState, + new DatafeedNodeSelector( + clusterState, resolver, params.getDatafeedId(), params.getJobId(), params.getDatafeedIndices(), - params.getIndicesOptions()) - .checkDatafeedTaskCanBeCreated(); + params.getIndicesOptions() + ).checkDatafeedTaskCanBeCreated(); } @Override - protected void nodeOperation(final AllocatedPersistentTask allocatedPersistentTask, - final StartDatafeedAction.DatafeedParams params, - final PersistentTaskState state) { + protected void nodeOperation( + final AllocatedPersistentTask allocatedPersistentTask, + final StartDatafeedAction.DatafeedParams params, + final PersistentTaskState state + ) { DatafeedTask datafeedTask = (DatafeedTask) allocatedPersistentTask; DatafeedState datafeedState = (DatafeedState) state; - // If we are stopping, stopped or isolated we should not start the runner. Due to + // If we are stopping, stopped or isolated we should not start the runner. Due to // races in the way messages pass between nodes via cluster state or direct action calls // we need to detect stopped/stopping by both considering the persistent task state in // cluster state and also whether an explicit request to stop has been received on this @@ -480,16 +567,24 @@ protected void nodeOperation(final AllocatedPersistentTask allocatedPersistentTa @Override protected AllocatedPersistentTask createTask( - long id, String type, String action, TaskId parentTaskId, - PersistentTasksCustomMetadata.PersistentTask persistentTask, - Map headers) { + long id, + String type, + String action, + TaskId parentTaskId, + PersistentTasksCustomMetadata.PersistentTask persistentTask, + Map headers + ) { return new DatafeedTask(id, type, action, parentTaskId, persistentTask.getParams(), headers); } } public static class DatafeedTask extends AllocatedPersistentTask implements StartDatafeedAction.DatafeedTaskMatcher { - public enum StoppedOrIsolatedBeforeRunning { NEITHER, ISOLATED, STOPPED } + public enum StoppedOrIsolatedBeforeRunning { + NEITHER, + ISOLATED, + STOPPED + } private final String datafeedId; private final long startTime; @@ -501,8 +596,14 @@ public enum StoppedOrIsolatedBeforeRunning { NEITHER, ISOLATED, STOPPED } private DatafeedRunner datafeedRunner; private StoppedOrIsolatedBeforeRunning stoppedOrIsolatedBeforeRunning = StoppedOrIsolatedBeforeRunning.NEITHER; - DatafeedTask(long id, String type, String action, TaskId parentTaskId, StartDatafeedAction.DatafeedParams params, - Map headers) { + DatafeedTask( + long id, + String type, + String action, + TaskId parentTaskId, + StartDatafeedAction.DatafeedParams params, + Map headers + ) { super(id, type, action, "datafeed-" + params.getDatafeedId(), parentTaskId, headers); this.datafeedId = params.getDatafeedId(); this.startTime = params.getStartTime(); @@ -600,13 +701,13 @@ void completeOrFailIfRequired(Exception error) { public GetDatafeedRunningStateAction.Response.RunningState getRunningState() { synchronized (this) { if (datafeedRunner == null) { - // In this case we don't know for sure if lookback has completed. It may be that the + // In this case we don't know for sure if lookback has completed. It may be that the // datafeed has just moved nodes, but with so little delay that there's no lookback to - // do on the new node. However, there _might_ be some catching up required, so it's - // reasonable to say real-time running hasn't started yet. The state will quickly + // do on the new node. However, there _might_ be some catching up required, so it's + // reasonable to say real-time running hasn't started yet. The state will quickly // change once the datafeed runner gets going and determines where the datafeed is up // to. - return new GetDatafeedRunningStateAction.Response.RunningState(endTime == null,false); + return new GetDatafeedRunningStateAction.Response.RunningState(endTime == null, false); } } return new GetDatafeedRunningStateAction.Response.RunningState(endTime == null, datafeedRunner.finishedLookBack(this)); @@ -639,8 +740,10 @@ public boolean test(PersistentTasksCustomMetadata.PersistentTask persistentTa } if (assignment.equals(PersistentTasksCustomMetadata.INITIAL_ASSIGNMENT) == false && assignment.isAssigned() == false) { // Assignment has failed despite passing our "fast fail" validation - exception = new ElasticsearchStatusException("Could not start datafeed, allocation explanation [" + - assignment.getExplanation() + "]", RestStatus.TOO_MANY_REQUESTS); + exception = new ElasticsearchStatusException( + "Could not start datafeed, allocation explanation [" + assignment.getExplanation() + "]", + RestStatus.TOO_MANY_REQUESTS + ); return true; } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportStartTrainedModelDeploymentAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportStartTrainedModelDeploymentAction.java index f6d260c61fd2f..c2f219b53f356 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportStartTrainedModelDeploymentAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportStartTrainedModelDeploymentAction.java @@ -66,8 +66,9 @@ import static org.elasticsearch.xpack.core.ClientHelper.ML_ORIGIN; -public class TransportStartTrainedModelDeploymentAction - extends TransportMasterNodeAction { +public class TransportStartTrainedModelDeploymentAction extends TransportMasterNodeAction< + StartTrainedModelDeploymentAction.Request, + CreateTrainedModelAllocationAction.Response> { private static final Logger logger = LogManager.getLogger(TransportStartTrainedModelDeploymentAction.class); @@ -79,14 +80,30 @@ public class TransportStartTrainedModelDeploymentAction protected volatile int maxLazyMLNodes; @Inject - public TransportStartTrainedModelDeploymentAction(TransportService transportService, Client client, ClusterService clusterService, - ThreadPool threadPool, ActionFilters actionFilters, XPackLicenseState licenseState, - IndexNameExpressionResolver indexNameExpressionResolver, Settings settings, - TrainedModelAllocationService trainedModelAllocationService, - NamedXContentRegistry xContentRegistry, MlMemoryTracker memoryTracker) { - super(StartTrainedModelDeploymentAction.NAME, transportService, clusterService, threadPool, actionFilters, - StartTrainedModelDeploymentAction.Request::new, indexNameExpressionResolver, CreateTrainedModelAllocationAction.Response::new, - ThreadPool.Names.SAME); + public TransportStartTrainedModelDeploymentAction( + TransportService transportService, + Client client, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + XPackLicenseState licenseState, + IndexNameExpressionResolver indexNameExpressionResolver, + Settings settings, + TrainedModelAllocationService trainedModelAllocationService, + NamedXContentRegistry xContentRegistry, + MlMemoryTracker memoryTracker + ) { + super( + StartTrainedModelDeploymentAction.NAME, + transportService, + clusterService, + threadPool, + actionFilters, + StartTrainedModelDeploymentAction.Request::new, + indexNameExpressionResolver, + CreateTrainedModelAllocationAction.Response::new, + ThreadPool.Names.SAME + ); this.licenseState = Objects.requireNonNull(licenseState); this.client = new OriginSettingClient(Objects.requireNonNull(client), ML_ORIGIN); this.xContentRegistry = Objects.requireNonNull(xContentRegistry); @@ -101,110 +118,108 @@ private void setMaxLazyMLNodes(int value) { } @Override - protected void masterOperation(Task task, StartTrainedModelDeploymentAction.Request request, ClusterState state, - ActionListener listener) throws Exception { + protected void masterOperation( + Task task, + StartTrainedModelDeploymentAction.Request request, + ClusterState state, + ActionListener listener + ) throws Exception { logger.trace(() -> new ParameterizedMessage("[{}] received deploy request", request.getModelId())); if (MachineLearningField.ML_API_FEATURE.check(licenseState) == false) { listener.onFailure(LicenseUtils.newComplianceException(XPackField.MACHINE_LEARNING)); return; } - ActionListener waitForDeploymentToStart = - ActionListener.wrap( - modelAllocation -> waitForDeploymentState( - request.getModelId(), - request.getTimeout(), - request.getWaitForState(), - listener - ), - e -> { - logger.warn(() -> new ParameterizedMessage("[{}] creating new allocation failed", request.getModelId()), e); - if (ExceptionsHelper.unwrapCause(e) instanceof ResourceAlreadyExistsException) { - e = new ElasticsearchStatusException( - "Cannot start deployment [{}] because it has already been started", - RestStatus.CONFLICT, - e, - request.getModelId() - ); - } - listener.onFailure(e); + ActionListener waitForDeploymentToStart = ActionListener.wrap( + modelAllocation -> waitForDeploymentState(request.getModelId(), request.getTimeout(), request.getWaitForState(), listener), + e -> { + logger.warn(() -> new ParameterizedMessage("[{}] creating new allocation failed", request.getModelId()), e); + if (ExceptionsHelper.unwrapCause(e) instanceof ResourceAlreadyExistsException) { + e = new ElasticsearchStatusException( + "Cannot start deployment [{}] because it has already been started", + RestStatus.CONFLICT, + e, + request.getModelId() + ); } - ); + listener.onFailure(e); + } + ); - ActionListener getModelListener = ActionListener.wrap( - getModelResponse -> { - if (getModelResponse.getResources().results().size() > 1) { - listener.onFailure(ExceptionsHelper.badRequestException( + ActionListener getModelListener = ActionListener.wrap(getModelResponse -> { + if (getModelResponse.getResources().results().size() > 1) { + listener.onFailure( + ExceptionsHelper.badRequestException( "cannot deploy more than one models at the same time; [{}] matches [{}] models]", - request.getModelId(), getModelResponse.getResources().results().size())); - return; - } - + request.getModelId(), + getModelResponse.getResources().results().size() + ) + ); + return; + } - TrainedModelConfig trainedModelConfig = getModelResponse.getResources().results().get(0); - if (trainedModelConfig.getModelType() != TrainedModelType.PYTORCH) { - listener.onFailure(ExceptionsHelper.badRequestException( + TrainedModelConfig trainedModelConfig = getModelResponse.getResources().results().get(0); + if (trainedModelConfig.getModelType() != TrainedModelType.PYTORCH) { + listener.onFailure( + ExceptionsHelper.badRequestException( "model [{}] of type [{}] cannot be deployed. Only PyTorch models can be deployed", - trainedModelConfig.getModelId(), trainedModelConfig.getModelType())); - return; - } + trainedModelConfig.getModelId(), + trainedModelConfig.getModelType() + ) + ); + return; + } - if (trainedModelConfig.getLocation() == null) { - listener.onFailure(ExceptionsHelper.serverError( - "model [{}] does not have location", trainedModelConfig.getModelId())); - return; - } + if (trainedModelConfig.getLocation() == null) { + listener.onFailure(ExceptionsHelper.serverError("model [{}] does not have location", trainedModelConfig.getModelId())); + return; + } - getModelBytes(trainedModelConfig, ActionListener.wrap( - modelBytes -> { - TaskParams taskParams = new TaskParams( - trainedModelConfig.getModelId(), - modelBytes, - request.getInferenceThreads(), - request.getModelThreads(), - request.getQueueCapacity() - ); - PersistentTasksCustomMetadata persistentTasks = clusterService.state().getMetadata().custom( - PersistentTasksCustomMetadata.TYPE); - memoryTracker.refresh(persistentTasks, ActionListener.wrap( - aVoid -> trainedModelAllocationService.createNewModelAllocation( - taskParams, - waitForDeploymentToStart - ), - listener::onFailure - ) - ); - }, - listener::onFailure - )); - - }, - listener::onFailure - ); + getModelBytes(trainedModelConfig, ActionListener.wrap(modelBytes -> { + TaskParams taskParams = new TaskParams( + trainedModelConfig.getModelId(), + modelBytes, + request.getInferenceThreads(), + request.getModelThreads(), + request.getQueueCapacity() + ); + PersistentTasksCustomMetadata persistentTasks = clusterService.state() + .getMetadata() + .custom(PersistentTasksCustomMetadata.TYPE); + memoryTracker.refresh( + persistentTasks, + ActionListener.wrap( + aVoid -> trainedModelAllocationService.createNewModelAllocation(taskParams, waitForDeploymentToStart), + listener::onFailure + ) + ); + }, listener::onFailure)); + + }, listener::onFailure); GetTrainedModelsAction.Request getModelRequest = new GetTrainedModelsAction.Request(request.getModelId()); client.execute(GetTrainedModelsAction.INSTANCE, getModelRequest, getModelListener); } private void getModelBytes(TrainedModelConfig trainedModelConfig, ActionListener listener) { - ChunkedTrainedModelRestorer restorer = new ChunkedTrainedModelRestorer(trainedModelConfig.getModelId(), - client, threadPool.executor(MachineLearning.UTILITY_THREAD_POOL_NAME), xContentRegistry); + ChunkedTrainedModelRestorer restorer = new ChunkedTrainedModelRestorer( + trainedModelConfig.getModelId(), + client, + threadPool.executor(MachineLearning.UTILITY_THREAD_POOL_NAME), + xContentRegistry + ); restorer.setSearchIndex(trainedModelConfig.getLocation().getResourceName()); restorer.setSearchSize(1); - restorer.restoreModelDefinition( - doc -> { - // The in-memory size of the model was found to be approximately equal - // to the size of the model on disk in experiments for BERT models. However, - // this might not always be the case. - // TODO Improve heuristic for in-memory model size. - listener.onResponse(doc.getTotalDefinitionLength()); - - // Return false to stop the restorer as we only need the first doc - return false; - }, - success -> { /* nothing to do */ }, - listener::onFailure - ); + restorer.restoreModelDefinition(doc -> { + // The in-memory size of the model was found to be approximately equal + // to the size of the model on disk in experiments for BERT models. However, + // this might not always be the case. + // TODO Improve heuristic for in-memory model size. + listener.onResponse(doc.getTotalDefinitionLength()); + + // Return false to stop the restorer as we only need the first doc + return false; + }, success -> { /* nothing to do */ }, listener::onFailure); } private void waitForDeploymentState( @@ -214,7 +229,10 @@ private void waitForDeploymentState( ActionListener listener ) { DeploymentStartedPredicate predicate = new DeploymentStartedPredicate(modelId, state, maxLazyMLNodes); - trainedModelAllocationService.waitForAllocationCondition(modelId, predicate, timeout, + trainedModelAllocationService.waitForAllocationCondition( + modelId, + predicate, + timeout, new TrainedModelAllocationService.WaitForAllocationListener() { @Override public void onResponse(TrainedModelAllocation allocation) { @@ -229,7 +247,8 @@ public void onResponse(TrainedModelAllocation allocation) { public void onFailure(Exception e) { listener.onFailure(e); } - }); + } + ); } private void deleteFailedDeployment( @@ -237,20 +256,17 @@ private void deleteFailedDeployment( Exception exception, ActionListener listener ) { - trainedModelAllocationService.deleteModelAllocation(modelId, ActionListener.wrap( - pTask -> listener.onFailure(exception), - e -> { - logger.error( - new ParameterizedMessage( - "[{}] Failed to delete model allocation that had failed with the reason [{}]", - modelId, - exception.getMessage() - ), - e - ); - listener.onFailure(exception); - } - )); + trainedModelAllocationService.deleteModelAllocation(modelId, ActionListener.wrap(pTask -> listener.onFailure(exception), e -> { + logger.error( + new ParameterizedMessage( + "[{}] Failed to delete model allocation that had failed with the reason [{}]", + modelId, + exception.getMessage() + ), + e + ); + listener.onFailure(exception); + })); } @@ -286,9 +302,7 @@ public boolean test(ClusterState clusterState) { return true; } - final Set> nodesAndState = trainedModelAllocation - .getNodeRoutingTable() - .entrySet(); + final Set> nodesAndState = trainedModelAllocation.getNodeRoutingTable().entrySet(); Map nodeFailuresAndReasons = new HashMap<>(); Set nodesStillInitializing = new LinkedHashSet<>(); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportStopDataFrameAnalyticsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportStopDataFrameAnalyticsAction.java index 880e0243dfeee..1098551da413d 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportStopDataFrameAnalyticsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportStopDataFrameAnalyticsAction.java @@ -57,9 +57,11 @@ /** * Stops the persistent task for running data frame analytics. */ -public class TransportStopDataFrameAnalyticsAction - extends TransportTasksAction { +public class TransportStopDataFrameAnalyticsAction extends TransportTasksAction< + DataFrameAnalyticsTask, + StopDataFrameAnalyticsAction.Request, + StopDataFrameAnalyticsAction.Response, + StopDataFrameAnalyticsAction.Response> { private static final Logger logger = LogManager.getLogger(TransportStopDataFrameAnalyticsAction.class); @@ -69,13 +71,25 @@ public class TransportStopDataFrameAnalyticsAction private final DataFrameAnalyticsAuditor auditor; @Inject - public TransportStopDataFrameAnalyticsAction(TransportService transportService, ActionFilters actionFilters, - ClusterService clusterService, ThreadPool threadPool, - PersistentTasksService persistentTasksService, - DataFrameAnalyticsConfigProvider configProvider, - DataFrameAnalyticsAuditor auditor) { - super(StopDataFrameAnalyticsAction.NAME, clusterService, transportService, actionFilters, StopDataFrameAnalyticsAction.Request::new, - StopDataFrameAnalyticsAction.Response::new, StopDataFrameAnalyticsAction.Response::new, ThreadPool.Names.SAME); + public TransportStopDataFrameAnalyticsAction( + TransportService transportService, + ActionFilters actionFilters, + ClusterService clusterService, + ThreadPool threadPool, + PersistentTasksService persistentTasksService, + DataFrameAnalyticsConfigProvider configProvider, + DataFrameAnalyticsAuditor auditor + ) { + super( + StopDataFrameAnalyticsAction.NAME, + clusterService, + transportService, + actionFilters, + StopDataFrameAnalyticsAction.Request::new, + StopDataFrameAnalyticsAction.Response::new, + StopDataFrameAnalyticsAction.Response::new, + ThreadPool.Names.SAME + ); this.threadPool = threadPool; this.persistentTasksService = persistentTasksService; this.configProvider = configProvider; @@ -83,8 +97,11 @@ public TransportStopDataFrameAnalyticsAction(TransportService transportService, } @Override - protected void doExecute(Task task, StopDataFrameAnalyticsAction.Request request, - ActionListener listener) { + protected void doExecute( + Task task, + StopDataFrameAnalyticsAction.Request request, + ActionListener listener + ) { ClusterState state = clusterService.state(); DiscoveryNodes nodes = state.nodes(); if (nodes.isLocalNodeElectedMaster() == false) { @@ -94,63 +111,71 @@ protected void doExecute(Task task, StopDataFrameAnalyticsAction.Request request logger.debug("Received request to stop data frame analytics [{}]", request.getId()); - ActionListener> expandedIdsListener = ActionListener.wrap( - idsToStop -> { - logger.debug("Resolved data frame analytics to stop: {}", idsToStop); + ActionListener> expandedIdsListener = ActionListener.wrap(idsToStop -> { + logger.debug("Resolved data frame analytics to stop: {}", idsToStop); - PersistentTasksCustomMetadata tasks = state.getMetadata().custom(PersistentTasksCustomMetadata.TYPE); - AnalyticsByTaskState analyticsByTaskState = AnalyticsByTaskState.build(idsToStop, tasks); + PersistentTasksCustomMetadata tasks = state.getMetadata().custom(PersistentTasksCustomMetadata.TYPE); + AnalyticsByTaskState analyticsByTaskState = AnalyticsByTaskState.build(idsToStop, tasks); - if (analyticsByTaskState.isEmpty()) { - listener.onResponse(new StopDataFrameAnalyticsAction.Response(true)); - return; - } + if (analyticsByTaskState.isEmpty()) { + listener.onResponse(new StopDataFrameAnalyticsAction.Response(true)); + return; + } - if (request.isForce()) { - forceStop(request, listener, tasks, analyticsByTaskState.getNonStopped()); - } else { - normalStop(task, request, listener, tasks, analyticsByTaskState); - } - }, - listener::onFailure - ); + if (request.isForce()) { + forceStop(request, listener, tasks, analyticsByTaskState.getNonStopped()); + } else { + normalStop(task, request, listener, tasks, analyticsByTaskState); + } + }, listener::onFailure); findIdsToStop(state, request, expandedIdsListener); } - private void findIdsToStop(ClusterState clusterState, StopDataFrameAnalyticsAction.Request request, - ActionListener> expandedIdsListener) { + private void findIdsToStop( + ClusterState clusterState, + StopDataFrameAnalyticsAction.Request request, + ActionListener> expandedIdsListener + ) { Set startedIds = getAllStartedIds(clusterState); - ActionListener> matchingIdsListener = ActionListener.wrap( - matchingIds -> { - startedIds.retainAll(matchingIds); - expandedIdsListener.onResponse(startedIds); - }, - expandedIdsListener::onFailure - ); + ActionListener> matchingIdsListener = ActionListener.wrap(matchingIds -> { + startedIds.retainAll(matchingIds); + expandedIdsListener.onResponse(startedIds); + }, expandedIdsListener::onFailure); if (request.isForce()) { matchAllStartedIds(request, startedIds, matchingIdsListener); } else { - configProvider.getMultiple(request.getId(), request.allowNoMatch(), ActionListener.wrap( - configs -> matchingIdsListener.onResponse( - configs.stream().map(DataFrameAnalyticsConfig::getId).collect(Collectors.toSet())), - matchingIdsListener::onFailure - )); + configProvider.getMultiple( + request.getId(), + request.allowNoMatch(), + ActionListener.wrap( + configs -> matchingIdsListener.onResponse( + configs.stream().map(DataFrameAnalyticsConfig::getId).collect(Collectors.toSet()) + ), + matchingIdsListener::onFailure + ) + ); } } private static Set getAllStartedIds(ClusterState clusterState) { PersistentTasksCustomMetadata tasksMetadata = clusterState.getMetadata().custom(PersistentTasksCustomMetadata.TYPE); - return tasksMetadata == null ? Collections.emptySet() : tasksMetadata.tasks().stream() - .filter(t -> t.getId().startsWith(MlTasks.DATA_FRAME_ANALYTICS_TASK_ID_PREFIX)) - .map(t -> t.getId().replaceFirst(MlTasks.DATA_FRAME_ANALYTICS_TASK_ID_PREFIX, "")) - .collect(Collectors.toSet()); + return tasksMetadata == null + ? Collections.emptySet() + : tasksMetadata.tasks() + .stream() + .filter(t -> t.getId().startsWith(MlTasks.DATA_FRAME_ANALYTICS_TASK_ID_PREFIX)) + .map(t -> t.getId().replaceFirst(MlTasks.DATA_FRAME_ANALYTICS_TASK_ID_PREFIX, "")) + .collect(Collectors.toSet()); } - private void matchAllStartedIds(StopDataFrameAnalyticsAction.Request request, Set startedIds, - ActionListener> matchingIdsListener) { + private void matchAllStartedIds( + StopDataFrameAnalyticsAction.Request request, + Set startedIds, + ActionListener> matchingIdsListener + ) { String[] tokens = ExpandedIdsMatcher.tokenizeExpression(request.getId()); ExpandedIdsMatcher expandedIdsMatcher = new ExpandedIdsMatcher(tokens, request.allowNoMatch()); expandedIdsMatcher.filterMatchedIds(startedIds); @@ -158,23 +183,33 @@ private void matchAllStartedIds(StopDataFrameAnalyticsAction.Request request, Se // There are expressions that did not match any started task. // If there are no configs for those either, we should error. // We check this by trying a get with the unmatched expressions. - configProvider.getMultiple(expandedIdsMatcher.unmatchedIdsString(), request.allowNoMatch(), ActionListener.wrap( - configs -> matchingIdsListener.onResponse(MlStrings.findMatching(tokens, startedIds)), - matchingIdsListener::onFailure - )); + configProvider.getMultiple( + expandedIdsMatcher.unmatchedIdsString(), + request.allowNoMatch(), + ActionListener.wrap( + configs -> matchingIdsListener.onResponse(MlStrings.findMatching(tokens, startedIds)), + matchingIdsListener::onFailure + ) + ); } else { matchingIdsListener.onResponse(MlStrings.findMatching(tokens, startedIds)); } } - private void normalStop(Task task, StopDataFrameAnalyticsAction.Request request, - ActionListener listener, - PersistentTasksCustomMetadata tasks, AnalyticsByTaskState analyticsByTaskState) { + private void normalStop( + Task task, + StopDataFrameAnalyticsAction.Request request, + ActionListener listener, + PersistentTasksCustomMetadata tasks, + AnalyticsByTaskState analyticsByTaskState + ) { if (analyticsByTaskState.failed.isEmpty() == false) { - ElasticsearchStatusException e = analyticsByTaskState.failed.size() == 1 ? ExceptionsHelper.conflictStatusException( - "cannot close data frame analytics [{}] because it failed, use force stop instead", - analyticsByTaskState.failed.iterator().next()) : - ExceptionsHelper.conflictStatusException("one or more data frame analytics are in failed state, use force stop instead"); + ElasticsearchStatusException e = analyticsByTaskState.failed.size() == 1 + ? ExceptionsHelper.conflictStatusException( + "cannot close data frame analytics [{}] because it failed, use force stop instead", + analyticsByTaskState.failed.iterator().next() + ) + : ExceptionsHelper.conflictStatusException("one or more data frame analytics are in failed state, use force stop instead"); listener.onFailure(e); return; } @@ -184,9 +219,9 @@ private void normalStop(Task task, StopDataFrameAnalyticsAction.Request request, // Wait for started and stopping analytics Set allAnalyticsToWaitFor = Stream.concat( - analyticsByTaskState.started.stream().map(MlTasks::dataFrameAnalyticsTaskId), - analyticsByTaskState.stopping.stream().map(MlTasks::dataFrameAnalyticsTaskId) - ).collect(Collectors.toSet()); + analyticsByTaskState.started.stream().map(MlTasks::dataFrameAnalyticsTaskId), + analyticsByTaskState.stopping.stream().map(MlTasks::dataFrameAnalyticsTaskId) + ).collect(Collectors.toSet()); ActionListener finalListener = ActionListener.wrap( r -> waitForTaskRemoved(allAnalyticsToWaitFor, request, r, listener), @@ -207,8 +242,12 @@ private void normalStop(Task task, StopDataFrameAnalyticsAction.Request request, super.doExecute(task, request, finalListener); } - private void forceStop(StopDataFrameAnalyticsAction.Request request, ActionListener listener, - PersistentTasksCustomMetadata tasks, List nonStoppedAnalytics) { + private void forceStop( + StopDataFrameAnalyticsAction.Request request, + ActionListener listener, + PersistentTasksCustomMetadata tasks, + List nonStoppedAnalytics + ) { final AtomicInteger counter = new AtomicInteger(); final AtomicArray failures = new AtomicArray<>(nonStoppedAnalytics.size()); @@ -216,26 +255,23 @@ private void forceStop(StopDataFrameAnalyticsAction.Request request, ActionListe for (String analyticsId : nonStoppedAnalytics) { PersistentTasksCustomMetadata.PersistentTask analyticsTask = MlTasks.getDataFrameAnalyticsTask(analyticsId, tasks); if (analyticsTask != null) { - persistentTasksService.sendRemoveRequest(analyticsTask.getId(), ActionListener.wrap( - removedTask -> { - auditor.info(analyticsId, Messages.DATA_FRAME_ANALYTICS_AUDIT_FORCE_STOPPED); - if (counter.incrementAndGet() == nonStoppedAnalytics.size()) { - sendResponseOrFailure(request.getId(), listener, failures); - } - }, - e -> { - final int slot = counter.incrementAndGet(); - // We validated that the analytics ids supplied in the request existed when we started processing the action. - // If the related tasks don't exist at this point then they must have been stopped by a simultaneous stop request. - // This is not an error. - if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException == false) { - failures.set(slot - 1, e); - } - if (slot == nonStoppedAnalytics.size()) { - sendResponseOrFailure(request.getId(), listener, failures); - } + persistentTasksService.sendRemoveRequest(analyticsTask.getId(), ActionListener.wrap(removedTask -> { + auditor.info(analyticsId, Messages.DATA_FRAME_ANALYTICS_AUDIT_FORCE_STOPPED); + if (counter.incrementAndGet() == nonStoppedAnalytics.size()) { + sendResponseOrFailure(request.getId(), listener, failures); + } + }, e -> { + final int slot = counter.incrementAndGet(); + // We validated that the analytics ids supplied in the request existed when we started processing the action. + // If the related tasks don't exist at this point then they must have been stopped by a simultaneous stop request. + // This is not an error. + if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException == false) { + failures.set(slot - 1, e); + } + if (slot == nonStoppedAnalytics.size()) { + sendResponseOrFailure(request.getId(), listener, failures); } - )); + })); } else { // This should not happen, because nonStoppedAnalytics // were derived from the same tasks that were passed to this method @@ -251,15 +287,21 @@ private void forceStop(StopDataFrameAnalyticsAction.Request request, ActionListe } } - private void sendResponseOrFailure(String analyticsId, ActionListener listener, - AtomicArray failures) { + private void sendResponseOrFailure( + String analyticsId, + ActionListener listener, + AtomicArray failures + ) { List caughtExceptions = failures.asList(); if (caughtExceptions.size() == 0) { listener.onResponse(new StopDataFrameAnalyticsAction.Response(true)); return; } - String msg = "Failed to stop data frame analytics [" + analyticsId + "] with [" + caughtExceptions.size() + String msg = "Failed to stop data frame analytics [" + + analyticsId + + "] with [" + + caughtExceptions.size() + "] failures, rethrowing last, all Exceptions: [" + caughtExceptions.stream().map(Exception::getMessage).collect(Collectors.joining(", ")) + "]"; @@ -288,21 +330,30 @@ private String[] findAllocatedNodesAndRemoveUnassignedTasks(List analyti return nodes.toArray(new String[0]); } - private void redirectToMasterNode(DiscoveryNode masterNode, StopDataFrameAnalyticsAction.Request request, - ActionListener listener) { + private void redirectToMasterNode( + DiscoveryNode masterNode, + StopDataFrameAnalyticsAction.Request request, + ActionListener listener + ) { if (masterNode == null) { listener.onFailure(new MasterNotDiscoveredException()); } else { - transportService.sendRequest(masterNode, actionName, request, - new ActionListenerResponseHandler<>(listener, StopDataFrameAnalyticsAction.Response::new)); + transportService.sendRequest( + masterNode, + actionName, + request, + new ActionListenerResponseHandler<>(listener, StopDataFrameAnalyticsAction.Response::new) + ); } } @Override - protected StopDataFrameAnalyticsAction.Response newResponse(StopDataFrameAnalyticsAction.Request request, - List tasks, - List taskOperationFailures, - List failedNodeExceptions) { + protected StopDataFrameAnalyticsAction.Response newResponse( + StopDataFrameAnalyticsAction.Request request, + List tasks, + List taskOperationFailures, + List failedNodeExceptions + ) { if (request.getExpandedIds().size() != tasks.size()) { if (taskOperationFailures.isEmpty() == false) { throw org.elasticsearch.ExceptionsHelper.convertToElastic(taskOperationFailures.get(0).getCause()); @@ -318,48 +369,55 @@ protected StopDataFrameAnalyticsAction.Response newResponse(StopDataFrameAnalyti } @Override - protected void taskOperation(StopDataFrameAnalyticsAction.Request request, - DataFrameAnalyticsTask task, - ActionListener listener) { - DataFrameAnalyticsTaskState stoppingState = - new DataFrameAnalyticsTaskState(DataFrameAnalyticsState.STOPPING, task.getAllocationId(), null); + protected void taskOperation( + StopDataFrameAnalyticsAction.Request request, + DataFrameAnalyticsTask task, + ActionListener listener + ) { + DataFrameAnalyticsTaskState stoppingState = new DataFrameAnalyticsTaskState( + DataFrameAnalyticsState.STOPPING, + task.getAllocationId(), + null + ); task.updatePersistentTaskState(stoppingState, ActionListener.wrap(pTask -> { - threadPool.executor(MachineLearning.UTILITY_THREAD_POOL_NAME).execute(new AbstractRunnable() { - @Override - public void onFailure(Exception e) { - listener.onFailure(e); - } + threadPool.executor(MachineLearning.UTILITY_THREAD_POOL_NAME).execute(new AbstractRunnable() { + @Override + public void onFailure(Exception e) { + listener.onFailure(e); + } - @Override - protected void doRun() { - logger.info("[{}] Stopping task with force [{}]", task.getParams().getId(), request.isForce()); - task.stop("stop_data_frame_analytics (api)", request.getTimeout()); - listener.onResponse(new StopDataFrameAnalyticsAction.Response(true)); - } - }); - }, - e -> { - if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) { - // the task has disappeared so must have stopped + @Override + protected void doRun() { + logger.info("[{}] Stopping task with force [{}]", task.getParams().getId(), request.isForce()); + task.stop("stop_data_frame_analytics (api)", request.getTimeout()); listener.onResponse(new StopDataFrameAnalyticsAction.Response(true)); - } else { - listener.onFailure(e); } - })); + }); + }, e -> { + if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) { + // the task has disappeared so must have stopped + listener.onResponse(new StopDataFrameAnalyticsAction.Response(true)); + } else { + listener.onFailure(e); + } + })); } - void waitForTaskRemoved(Set taskIds, StopDataFrameAnalyticsAction.Request request, - StopDataFrameAnalyticsAction.Response response, - ActionListener listener) { - persistentTasksService.waitForPersistentTasksCondition(persistentTasks -> - persistentTasks.findTasks(MlTasks.DATA_FRAME_ANALYTICS_TASK_NAME, t -> taskIds.contains(t.getId())).isEmpty(), - request.getTimeout(), ActionListener.wrap( - booleanResponse -> { - auditor.info(request.getId(), Messages.DATA_FRAME_ANALYTICS_AUDIT_STOPPED); - listener.onResponse(response); - }, - listener::onFailure - )); + void waitForTaskRemoved( + Set taskIds, + StopDataFrameAnalyticsAction.Request request, + StopDataFrameAnalyticsAction.Response response, + ActionListener listener + ) { + persistentTasksService.waitForPersistentTasksCondition( + persistentTasks -> persistentTasks.findTasks(MlTasks.DATA_FRAME_ANALYTICS_TASK_NAME, t -> taskIds.contains(t.getId())) + .isEmpty(), + request.getTimeout(), + ActionListener.wrap(booleanResponse -> { + auditor.info(request.getId(), Messages.DATA_FRAME_ANALYTICS_AUDIT_STOPPED); + listener.onResponse(response); + }, listener::onFailure) + ); } // Visible for testing diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportStopDatafeedAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportStopDatafeedAction.java index 1e1b909a5ccf2..a8ffd191a9525 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportStopDatafeedAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportStopDatafeedAction.java @@ -56,8 +56,11 @@ import static org.elasticsearch.xpack.core.ClientHelper.ML_ORIGIN; -public class TransportStopDatafeedAction extends TransportTasksAction { +public class TransportStopDatafeedAction extends TransportTasksAction< + TransportStartDatafeedAction.DatafeedTask, + StopDatafeedAction.Request, + StopDatafeedAction.Response, + StopDatafeedAction.Response> { private static final int MAX_ATTEMPTS = 10; @@ -70,12 +73,26 @@ public class TransportStopDatafeedAction extends TransportTasksAction expandedDatafeedIds, - PersistentTasksCustomMetadata tasks, - List startedDatafeedIds, - List stoppingDatafeedIds, - List notStoppedDatafeedIds) { + static void sortDatafeedIdsByTaskState( + Collection expandedDatafeedIds, + PersistentTasksCustomMetadata tasks, + List startedDatafeedIds, + List stoppingDatafeedIds, + List notStoppedDatafeedIds + ) { for (String expandedDatafeedId : expandedDatafeedIds) { - addDatafeedTaskIdAccordingToState(expandedDatafeedId, MlTasks.getDatafeedState(expandedDatafeedId, tasks), - startedDatafeedIds, stoppingDatafeedIds, notStoppedDatafeedIds); + addDatafeedTaskIdAccordingToState( + expandedDatafeedId, + MlTasks.getDatafeedState(expandedDatafeedId, tasks), + startedDatafeedIds, + stoppingDatafeedIds, + notStoppedDatafeedIds + ); } } - private static void addDatafeedTaskIdAccordingToState(String datafeedId, - DatafeedState datafeedState, - List startedDatafeedIds, - List stoppingDatafeedIds, - List notStoppedDatafeedIds) { + private static void addDatafeedTaskIdAccordingToState( + String datafeedId, + DatafeedState datafeedState, + List startedDatafeedIds, + List stoppingDatafeedIds, + List notStoppedDatafeedIds + ) { switch (datafeedState) { // Treat STARTING like STARTED for stop API behaviour. case STARTING: @@ -134,8 +160,12 @@ protected void doExecute(Task task, StopDatafeedAction.Request request, ActionLi doExecute(task, request, listener, 1); } - private void doExecute(Task task, StopDatafeedAction.Request request, ActionListener listener, - int attempt) { + private void doExecute( + Task task, + StopDatafeedAction.Request request, + ActionListener listener, + int attempt + ) { final ClusterState state = clusterService.state(); final DiscoveryNodes nodes = state.nodes(); if (nodes.isLocalNodeElectedMaster() == false) { @@ -144,40 +174,50 @@ private void doExecute(Task task, StopDatafeedAction.Request request, ActionList if (nodes.getMasterNode() == null) { listener.onFailure(new MasterNotDiscoveredException()); } else { - transportService.sendRequest(nodes.getMasterNode(), actionName, request, - new ActionListenerResponseHandler<>(listener, StopDatafeedAction.Response::new)); + transportService.sendRequest( + nodes.getMasterNode(), + actionName, + request, + new ActionListenerResponseHandler<>(listener, StopDatafeedAction.Response::new) + ); } } else { PersistentTasksCustomMetadata tasks = state.getMetadata().custom(PersistentTasksCustomMetadata.TYPE); - datafeedConfigProvider.expandDatafeedIds(request.getDatafeedId(), + datafeedConfigProvider.expandDatafeedIds( + request.getDatafeedId(), request.allowNoMatch(), tasks, request.isForce(), - ActionListener.wrap( - expandedIds -> { - List startedDatafeeds = new ArrayList<>(); - List stoppingDatafeeds = new ArrayList<>(); - List notStoppedDatafeeds = new ArrayList<>(); - sortDatafeedIdsByTaskState(expandedIds, tasks, startedDatafeeds, stoppingDatafeeds, notStoppedDatafeeds); - if (startedDatafeeds.isEmpty() && stoppingDatafeeds.isEmpty()) { - listener.onResponse(new StopDatafeedAction.Response(true)); - return; - } + ActionListener.wrap(expandedIds -> { + List startedDatafeeds = new ArrayList<>(); + List stoppingDatafeeds = new ArrayList<>(); + List notStoppedDatafeeds = new ArrayList<>(); + sortDatafeedIdsByTaskState(expandedIds, tasks, startedDatafeeds, stoppingDatafeeds, notStoppedDatafeeds); + if (startedDatafeeds.isEmpty() && stoppingDatafeeds.isEmpty()) { + listener.onResponse(new StopDatafeedAction.Response(true)); + return; + } - if (request.isForce()) { - forceStopDatafeed(request, listener, tasks, nodes, notStoppedDatafeeds); - } else { - normalStopDatafeed(task, request, listener, tasks, nodes, startedDatafeeds, stoppingDatafeeds, attempt); - } - }, - listener::onFailure - )); + if (request.isForce()) { + forceStopDatafeed(request, listener, tasks, nodes, notStoppedDatafeeds); + } else { + normalStopDatafeed(task, request, listener, tasks, nodes, startedDatafeeds, stoppingDatafeeds, attempt); + } + }, listener::onFailure) + ); } } - private void normalStopDatafeed(Task task, StopDatafeedAction.Request request, ActionListener listener, - PersistentTasksCustomMetadata tasks, DiscoveryNodes nodes, - List startedDatafeeds, List stoppingDatafeeds, int attempt) { + private void normalStopDatafeed( + Task task, + StopDatafeedAction.Request request, + ActionListener listener, + PersistentTasksCustomMetadata tasks, + DiscoveryNodes nodes, + List startedDatafeeds, + List stoppingDatafeeds, + int attempt + ) { final Set executorNodes = new HashSet<>(); final List startedDatafeedsJobs = new ArrayList<>(); final List resolvedStartedDatafeeds = new ArrayList<>(); @@ -196,14 +236,17 @@ private void normalStopDatafeed(Task task, StopDatafeedAction.Request request, A allDataFeedsToWaitFor.add(datafeedTask); } else { // This is the easy case - the datafeed is not currently assigned to a valid node, - // so can be gracefully stopped simply by removing its persistent task. (Usually + // so can be gracefully stopped simply by removing its persistent task. (Usually // a graceful stop cannot be achieved by simply removing the persistent task, but // if the datafeed has no running code then graceful/forceful are the same.) // The listener here doesn't need to call the final listener, as waitForDatafeedStopped() // already waits for these persistent tasks to disappear. - persistentTasksService.sendRemoveRequest(datafeedTask.getId(), ActionListener.wrap( - r -> auditDatafeedStopped(datafeedTask), - e -> logger.error("[" + datafeedId + "] failed to remove task to stop unassigned datafeed", e)) + persistentTasksService.sendRemoveRequest( + datafeedTask.getId(), + ActionListener.wrap( + r -> auditDatafeedStopped(datafeedTask), + e -> logger.error("[" + datafeedId + "] failed to remove task to stop unassigned datafeed", e) + ) ); allDataFeedsToWaitFor.add(datafeedTask); } @@ -221,129 +264,145 @@ private void normalStopDatafeed(Task task, StopDatafeedAction.Request request, A final Set movedDatafeeds = Sets.newConcurrentHashSet(); ActionListener finalListener = ActionListener.wrap( - response -> waitForDatafeedStopped(allDataFeedsToWaitFor, request, response, ActionListener.wrap( - finished -> { - for (String datafeedId : movedDatafeeds) { - PersistentTasksCustomMetadata.PersistentTask datafeedTask = MlTasks.getDatafeedTask(datafeedId, tasks); - persistentTasksService.sendRemoveRequest(datafeedTask.getId(), ActionListener.wrap( - r -> auditDatafeedStopped(datafeedTask), - e -> { - if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) { - logger.debug("[{}] relocated datafeed task already removed", datafeedId); - } else { - logger.error("[" + datafeedId + "] failed to remove task to stop relocated datafeed", e); - } - }) - ); - } - if (startedDatafeedsJobs.isEmpty()) { - listener.onResponse(finished); - return; - } - client.admin().indices().prepareRefresh(startedDatafeedsJobs - .stream() - .map(AnomalyDetectorsIndex::jobResultsAliasedName) - .toArray(String[]::new)) - .execute(ActionListener.wrap( - _unused -> listener.onResponse(finished), - ex -> { - logger.warn( - () -> new ParameterizedMessage( - "failed to refresh job [{}] results indices when stopping datafeeds [{}]", - startedDatafeedsJobs, - startedDatafeeds - ), - ex); - listener.onResponse(finished); - } - )); - }, - listener::onFailure - ), movedDatafeeds), - e -> { - Throwable unwrapped = ExceptionsHelper.unwrapCause(e); - if (unwrapped instanceof FailedNodeException) { - // A node has dropped out of the cluster since we started executing the requests. - // Since stopping an already stopped datafeed is not an error we can try again. - // The datafeeds that were running on the node that dropped out of the cluster - // will just have their persistent tasks cancelled. Datafeeds that were stopped - // by the previous attempt will be noops in the subsequent attempt. - if (attempt <= MAX_ATTEMPTS) { - logger.warn("Node [{}] failed while processing stop datafeed request - retrying", - ((FailedNodeException) unwrapped).nodeId()); - doExecute(task, request, listener, attempt + 1); - } else { - listener.onFailure(e); - } - } else if (unwrapped instanceof RetryStopDatafeedException) { - // This is for the case where a local task wasn't yet running at the moment a - // request to stop it arrived at its node. This can happen when the cluster - // state says a persistent task should be running on a particular node but that - // node hasn't yet had time to start the corresponding local task. - if (attempt <= MAX_ATTEMPTS) { - logger.info("Insufficient responses while processing stop datafeed request [{}] - retrying", - unwrapped.getMessage()); - // Unlike the failed node case above, in this case we should wait a little - // before retrying because we need to allow time for the local task to - // start on the node it's supposed to be running on. - threadPool.schedule(() -> doExecute(task, request, listener, attempt + 1), - TimeValue.timeValueMillis(100L * attempt), ThreadPool.Names.SAME); - } else { - listener.onFailure(ExceptionsHelper.serverError("Failed to stop datafeed [" + request.getDatafeedId() - + "] after " + MAX_ATTEMPTS - + " due to inconsistencies between local and persistent tasks within the cluster")); - } + response -> waitForDatafeedStopped(allDataFeedsToWaitFor, request, response, ActionListener.wrap(finished -> { + for (String datafeedId : movedDatafeeds) { + PersistentTasksCustomMetadata.PersistentTask datafeedTask = MlTasks.getDatafeedTask(datafeedId, tasks); + persistentTasksService.sendRemoveRequest( + datafeedTask.getId(), + ActionListener.wrap(r -> auditDatafeedStopped(datafeedTask), e -> { + if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) { + logger.debug("[{}] relocated datafeed task already removed", datafeedId); + } else { + logger.error("[" + datafeedId + "] failed to remove task to stop relocated datafeed", e); + } + }) + ); + } + if (startedDatafeedsJobs.isEmpty()) { + listener.onResponse(finished); + return; + } + client.admin() + .indices() + .prepareRefresh(startedDatafeedsJobs.stream().map(AnomalyDetectorsIndex::jobResultsAliasedName).toArray(String[]::new)) + .execute(ActionListener.wrap(_unused -> listener.onResponse(finished), ex -> { + logger.warn( + () -> new ParameterizedMessage( + "failed to refresh job [{}] results indices when stopping datafeeds [{}]", + startedDatafeedsJobs, + startedDatafeeds + ), + ex + ); + listener.onResponse(finished); + })); + }, listener::onFailure), movedDatafeeds), + e -> { + Throwable unwrapped = ExceptionsHelper.unwrapCause(e); + if (unwrapped instanceof FailedNodeException) { + // A node has dropped out of the cluster since we started executing the requests. + // Since stopping an already stopped datafeed is not an error we can try again. + // The datafeeds that were running on the node that dropped out of the cluster + // will just have their persistent tasks cancelled. Datafeeds that were stopped + // by the previous attempt will be noops in the subsequent attempt. + if (attempt <= MAX_ATTEMPTS) { + logger.warn( + "Node [{}] failed while processing stop datafeed request - retrying", + ((FailedNodeException) unwrapped).nodeId() + ); + doExecute(task, request, listener, attempt + 1); } else { listener.onFailure(e); } - }); + } else if (unwrapped instanceof RetryStopDatafeedException) { + // This is for the case where a local task wasn't yet running at the moment a + // request to stop it arrived at its node. This can happen when the cluster + // state says a persistent task should be running on a particular node but that + // node hasn't yet had time to start the corresponding local task. + if (attempt <= MAX_ATTEMPTS) { + logger.info( + "Insufficient responses while processing stop datafeed request [{}] - retrying", + unwrapped.getMessage() + ); + // Unlike the failed node case above, in this case we should wait a little + // before retrying because we need to allow time for the local task to + // start on the node it's supposed to be running on. + threadPool.schedule( + () -> doExecute(task, request, listener, attempt + 1), + TimeValue.timeValueMillis(100L * attempt), + ThreadPool.Names.SAME + ); + } else { + listener.onFailure( + ExceptionsHelper.serverError( + "Failed to stop datafeed [" + + request.getDatafeedId() + + "] after " + + MAX_ATTEMPTS + + " due to inconsistencies between local and persistent tasks within the cluster" + ) + ); + } + } else { + listener.onFailure(e); + } + } + ); super.doExecute(task, request, finalListener); } private void auditDatafeedStopped(PersistentTasksCustomMetadata.PersistentTask datafeedTask) { @SuppressWarnings("unchecked") - String jobId = - ((PersistentTasksCustomMetadata.PersistentTask) datafeedTask).getParams().getJobId(); + String jobId = ((PersistentTasksCustomMetadata.PersistentTask) datafeedTask).getParams() + .getJobId(); auditor.info(jobId, Messages.getMessage(Messages.JOB_AUDIT_DATAFEED_STOPPED)); } - private void forceStopDatafeed(final StopDatafeedAction.Request request, final ActionListener listener, - PersistentTasksCustomMetadata tasks, DiscoveryNodes nodes, final List notStoppedDatafeeds) { + private void forceStopDatafeed( + final StopDatafeedAction.Request request, + final ActionListener listener, + PersistentTasksCustomMetadata tasks, + DiscoveryNodes nodes, + final List notStoppedDatafeeds + ) { final AtomicInteger counter = new AtomicInteger(); final AtomicArray failures = new AtomicArray<>(notStoppedDatafeeds.size()); for (String datafeedId : notStoppedDatafeeds) { PersistentTasksCustomMetadata.PersistentTask datafeedTask = MlTasks.getDatafeedTask(datafeedId, tasks); if (datafeedTask != null) { - persistentTasksService.sendRemoveRequest(datafeedTask.getId(), - new ActionListener>() { - @Override - public void onResponse(PersistentTasksCustomMetadata.PersistentTask persistentTask) { - // For force stop, only audit here if the datafeed was unassigned at the time of the stop, hence inactive. - // If the datafeed was active then it audits itself on being cancelled. - if (PersistentTasksClusterService.needsReassignment(datafeedTask.getAssignment(), nodes)) { - auditDatafeedStopped(datafeedTask); - } - if (counter.incrementAndGet() == notStoppedDatafeeds.size()) { - sendResponseOrFailure(request.getDatafeedId(), listener, failures); + persistentTasksService.sendRemoveRequest( + datafeedTask.getId(), + new ActionListener>() { + @Override + public void onResponse(PersistentTasksCustomMetadata.PersistentTask persistentTask) { + // For force stop, only audit here if the datafeed was unassigned at the time of the stop, hence inactive. + // If the datafeed was active then it audits itself on being cancelled. + if (PersistentTasksClusterService.needsReassignment(datafeedTask.getAssignment(), nodes)) { + auditDatafeedStopped(datafeedTask); + } + if (counter.incrementAndGet() == notStoppedDatafeeds.size()) { + sendResponseOrFailure(request.getDatafeedId(), listener, failures); + } } - } - @Override - public void onFailure(Exception e) { - final int slot = counter.incrementAndGet(); - // We validated that the datafeed names supplied in the request existed when we started processing the action. - // If the related tasks don't exist at this point then they must have been stopped by a simultaneous stop request. - // This is not an error. - if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException == false) { - failures.set(slot - 1, e); - } - if (slot == notStoppedDatafeeds.size()) { - sendResponseOrFailure(request.getDatafeedId(), listener, failures); + @Override + public void onFailure(Exception e) { + final int slot = counter.incrementAndGet(); + // We validated that the datafeed names supplied in the request existed when we started processing the action. + // If the related tasks don't exist at this point then they must have been stopped by a simultaneous stop + // request. + // This is not an error. + if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException == false) { + failures.set(slot - 1, e); + } + if (slot == notStoppedDatafeeds.size()) { + sendResponseOrFailure(request.getDatafeedId(), listener, failures); + } } } - }); + ); } else { // This should not happen, because startedDatafeeds and stoppingDatafeeds // were derived from the same tasks that were passed to this method @@ -360,53 +419,58 @@ public void onFailure(Exception e) { } @Override - protected void taskOperation(StopDatafeedAction.Request request, TransportStartDatafeedAction.DatafeedTask datafeedTask, - ActionListener listener) { + protected void taskOperation( + StopDatafeedAction.Request request, + TransportStartDatafeedAction.DatafeedTask datafeedTask, + ActionListener listener + ) { DatafeedState taskState = DatafeedState.STOPPING; - datafeedTask.updatePersistentTaskState(taskState, - ActionListener.wrap( - task -> { - // we need to fork because we are now on a network threadpool - threadPool.executor(MachineLearning.UTILITY_THREAD_POOL_NAME).execute(new AbstractRunnable() { - @Override - public void onFailure(Exception e) { - // We validated that the datafeed names supplied in the request existed when we started processing the action. - // If the related task for one of them doesn't exist at this point then it must have been removed by a - // simultaneous force stop request. This is not an error. - if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) { - listener.onResponse(new StopDatafeedAction.Response(true)); - } else { - listener.onFailure(e); - } - } - - @Override - protected void doRun() { - datafeedTask.stop("stop_datafeed (api)", request.getStopTimeout()); - listener.onResponse(new StopDatafeedAction.Response(true)); - } - }); - }, - e -> { + datafeedTask.updatePersistentTaskState(taskState, ActionListener.wrap(task -> { + // we need to fork because we are now on a network threadpool + threadPool.executor(MachineLearning.UTILITY_THREAD_POOL_NAME).execute(new AbstractRunnable() { + @Override + public void onFailure(Exception e) { + // We validated that the datafeed names supplied in the request existed when we started processing the action. + // If the related task for one of them doesn't exist at this point then it must have been removed by a + // simultaneous force stop request. This is not an error. if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) { - // the task has disappeared so must have stopped listener.onResponse(new StopDatafeedAction.Response(true)); } else { listener.onFailure(e); } } - )); + + @Override + protected void doRun() { + datafeedTask.stop("stop_datafeed (api)", request.getStopTimeout()); + listener.onResponse(new StopDatafeedAction.Response(true)); + } + }); + }, e -> { + if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) { + // the task has disappeared so must have stopped + listener.onResponse(new StopDatafeedAction.Response(true)); + } else { + listener.onFailure(e); + } + })); } - private void sendResponseOrFailure(String datafeedId, ActionListener listener, - AtomicArray failures) { + private void sendResponseOrFailure( + String datafeedId, + ActionListener listener, + AtomicArray failures + ) { List caughtExceptions = failures.asList(); if (caughtExceptions.size() == 0) { listener.onResponse(new StopDatafeedAction.Response(true)); return; } - String msg = "Failed to stop datafeed [" + datafeedId + "] with [" + caughtExceptions.size() + String msg = "Failed to stop datafeed [" + + datafeedId + + "] with [" + + caughtExceptions.size() + "] failures, rethrowing last, all Exceptions: [" + caughtExceptions.stream().map(Exception::getMessage).collect(Collectors.joining(", ")) + "]"; @@ -424,16 +488,19 @@ private void sendResponseOrFailure(String datafeedId, ActionListener> datafeedPersistentTasks, - StopDatafeedAction.Request request, - StopDatafeedAction.Response response, - ActionListener listener, - Set movedDatafeeds) { + void waitForDatafeedStopped( + List> datafeedPersistentTasks, + StopDatafeedAction.Request request, + StopDatafeedAction.Response response, + ActionListener listener, + Set movedDatafeeds + ) { persistentTasksService.waitForPersistentTasksCondition(persistentTasksCustomMetadata -> { for (PersistentTasksCustomMetadata.PersistentTask originalPersistentTask : datafeedPersistentTasks) { String originalPersistentTaskId = originalPersistentTask.getId(); - PersistentTasksCustomMetadata.PersistentTask currentPersistentTask = - persistentTasksCustomMetadata.getTask(originalPersistentTaskId); + PersistentTasksCustomMetadata.PersistentTask currentPersistentTask = persistentTasksCustomMetadata.getTask( + originalPersistentTaskId + ); if (currentPersistentTask != null) { if (Objects.equals(originalPersistentTask.getExecutorNode(), currentPersistentTask.getExecutorNode()) && originalPersistentTask.getAllocationId() == currentPersistentTask.getAllocationId()) { @@ -450,25 +517,26 @@ void waitForDatafeedStopped(List } @Override - protected StopDatafeedAction.Response newResponse(StopDatafeedAction.Request request, List tasks, - List taskOperationFailures, - List failedNodeExceptions) { + protected StopDatafeedAction.Response newResponse( + StopDatafeedAction.Request request, + List tasks, + List taskOperationFailures, + List failedNodeExceptions + ) { // number of resolved (i.e. running on a node) started data feeds should be equal to the number of // tasks, otherwise something went wrong if (request.getResolvedStartedDatafeedIds().length != tasks.size()) { if (taskOperationFailures.isEmpty() == false) { - throw org.elasticsearch.ExceptionsHelper - .convertToElastic(taskOperationFailures.get(0).getCause()); + throw org.elasticsearch.ExceptionsHelper.convertToElastic(taskOperationFailures.get(0).getCause()); } else if (failedNodeExceptions.isEmpty() == false) { - throw org.elasticsearch.ExceptionsHelper - .convertToElastic(failedNodeExceptions.get(0)); + throw org.elasticsearch.ExceptionsHelper.convertToElastic(failedNodeExceptions.get(0)); } else { // This can happen when the local task in the node no longer exists, - // which means the datafeed(s) have already been stopped. It can + // which means the datafeed(s) have already been stopped. It can // also happen if the local task hadn't yet been created when the - // stop request hit the executor node. In this second case we need + // stop request hit the executor node. In this second case we need // to retry, otherwise the wait for completion will wait until it - // times out. We cannot tell which case it is, but it doesn't hurt + // times out. We cannot tell which case it is, but it doesn't hurt // to retry in both cases since stopping a stopped datafeed is a // no-op. throw new RetryStopDatafeedException(request.getResolvedStartedDatafeedIds().length, tasks.size()); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportStopTrainedModelDeploymentAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportStopTrainedModelDeploymentAction.java index c70d2b959968a..0c2f20ca36b4f 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportStopTrainedModelDeploymentAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportStopTrainedModelDeploymentAction.java @@ -56,8 +56,11 @@ * NOTE: this class gets routed to each individual deployment running on the nodes. This way when the request returns, we are assured * that the model is not running any longer on any node. */ -public class TransportStopTrainedModelDeploymentAction extends TransportTasksAction { +public class TransportStopTrainedModelDeploymentAction extends TransportTasksAction< + TrainedModelDeploymentTask, + StopTrainedModelDeploymentAction.Request, + StopTrainedModelDeploymentAction.Response, + StopTrainedModelDeploymentAction.Response> { private static final Logger logger = LogManager.getLogger(TransportStopTrainedModelDeploymentAction.class); @@ -67,13 +70,25 @@ public class TransportStopTrainedModelDeploymentAction extends TransportTasksAct private final TrainedModelAllocationClusterService trainedModelAllocationClusterService; @Inject - public TransportStopTrainedModelDeploymentAction(ClusterService clusterService, TransportService transportService, - ActionFilters actionFilters, Client client, IngestService ingestService, - TrainedModelAllocationService trainedModelAllocationService, - TrainedModelAllocationClusterService trainedModelAllocationClusterService) { - super(StopTrainedModelDeploymentAction.NAME, clusterService, transportService, actionFilters, - StopTrainedModelDeploymentAction.Request::new, StopTrainedModelDeploymentAction.Response::new, - StopTrainedModelDeploymentAction.Response::new, ThreadPool.Names.SAME); + public TransportStopTrainedModelDeploymentAction( + ClusterService clusterService, + TransportService transportService, + ActionFilters actionFilters, + Client client, + IngestService ingestService, + TrainedModelAllocationService trainedModelAllocationService, + TrainedModelAllocationClusterService trainedModelAllocationClusterService + ) { + super( + StopTrainedModelDeploymentAction.NAME, + clusterService, + transportService, + actionFilters, + StopTrainedModelDeploymentAction.Request::new, + StopTrainedModelDeploymentAction.Response::new, + StopTrainedModelDeploymentAction.Response::new, + ThreadPool.Names.SAME + ); this.client = new OriginSettingClient(client, ML_ORIGIN); this.ingestService = ingestService; this.trainedModelAllocationService = trainedModelAllocationService; @@ -81,8 +96,11 @@ public TransportStopTrainedModelDeploymentAction(ClusterService clusterService, } @Override - protected void doExecute(Task task, StopTrainedModelDeploymentAction.Request request, - ActionListener listener) { + protected void doExecute( + Task task, + StopTrainedModelDeploymentAction.Request request, + ActionListener listener + ) { ClusterState state = clusterService.state(); DiscoveryNodes nodes = state.nodes(); // Master node is required for initial pre-checks and deletion preparation @@ -93,128 +111,135 @@ protected void doExecute(Task task, StopTrainedModelDeploymentAction.Request req logger.debug("[{}] Received request to undeploy", request.getId()); - ActionListener getModelListener = ActionListener.wrap( - getModelsResponse -> { - List models = getModelsResponse.getResources().results(); - if (models.isEmpty()) { - listener.onResponse(new StopTrainedModelDeploymentAction.Response(true)); - return; - } - if (models.size() > 1) { - listener.onFailure(ExceptionsHelper.badRequestException("cannot undeploy multiple models at the same time")); - return; - } - - Optional maybeAllocation = TrainedModelAllocationMetadata.allocationForModelId( - clusterService.state(), - models.get(0).getModelId() - ); + ActionListener getModelListener = ActionListener.wrap(getModelsResponse -> { + List models = getModelsResponse.getResources().results(); + if (models.isEmpty()) { + listener.onResponse(new StopTrainedModelDeploymentAction.Response(true)); + return; + } + if (models.size() > 1) { + listener.onFailure(ExceptionsHelper.badRequestException("cannot undeploy multiple models at the same time")); + return; + } - if (maybeAllocation.isEmpty()) { - listener.onResponse(new StopTrainedModelDeploymentAction.Response(true)); - return; - } - final String modelId = models.get(0).getModelId(); + Optional maybeAllocation = TrainedModelAllocationMetadata.allocationForModelId( + clusterService.state(), + models.get(0).getModelId() + ); - IngestMetadata currentIngestMetadata = state.metadata().custom(IngestMetadata.TYPE); - Set referencedModels = getReferencedModelKeys(currentIngestMetadata, ingestService); + if (maybeAllocation.isEmpty()) { + listener.onResponse(new StopTrainedModelDeploymentAction.Response(true)); + return; + } + final String modelId = models.get(0).getModelId(); - if (referencedModels.contains(modelId)) { - listener.onFailure(new ElasticsearchStatusException( - "Cannot stop allocation for model [{}] as it is still referenced by ingest processors", - RestStatus.CONFLICT, modelId) - ); - return; - } + IngestMetadata currentIngestMetadata = state.metadata().custom(IngestMetadata.TYPE); + Set referencedModels = getReferencedModelKeys(currentIngestMetadata, ingestService); - // NOTE, should only run on Master node - trainedModelAllocationClusterService.setModelAllocationToStopping( - modelId, - ActionListener.wrap( - setToStopping -> normalUndeploy(task, models.get(0).getModelId(), maybeAllocation.get(), request, listener), - failure -> { - if (ExceptionsHelper.unwrapCause(failure) instanceof ResourceNotFoundException) { - listener.onResponse(new StopTrainedModelDeploymentAction.Response(true)); - return; - } - listener.onFailure(failure); - } + if (referencedModels.contains(modelId)) { + listener.onFailure( + new ElasticsearchStatusException( + "Cannot stop allocation for model [{}] as it is still referenced by ingest processors", + RestStatus.CONFLICT, + modelId ) ); - }, - listener::onFailure - ); + return; + } - GetTrainedModelsAction.Request getModelRequest = new GetTrainedModelsAction.Request( - request.getId(), null, Collections.emptySet()); + // NOTE, should only run on Master node + trainedModelAllocationClusterService.setModelAllocationToStopping( + modelId, + ActionListener.wrap( + setToStopping -> normalUndeploy(task, models.get(0).getModelId(), maybeAllocation.get(), request, listener), + failure -> { + if (ExceptionsHelper.unwrapCause(failure) instanceof ResourceNotFoundException) { + listener.onResponse(new StopTrainedModelDeploymentAction.Response(true)); + return; + } + listener.onFailure(failure); + } + ) + ); + }, listener::onFailure); + + GetTrainedModelsAction.Request getModelRequest = new GetTrainedModelsAction.Request(request.getId(), null, Collections.emptySet()); getModelRequest.setAllowNoResources(request.isAllowNoMatch()); client.execute(GetTrainedModelsAction.INSTANCE, getModelRequest, getModelListener); } - private void redirectToMasterNode(DiscoveryNode masterNode, StopTrainedModelDeploymentAction.Request request, - ActionListener listener) { + private void redirectToMasterNode( + DiscoveryNode masterNode, + StopTrainedModelDeploymentAction.Request request, + ActionListener listener + ) { if (masterNode == null) { listener.onFailure(new MasterNotDiscoveredException()); } else { - transportService.sendRequest(masterNode, actionName, request, - new ActionListenerResponseHandler<>(listener, StopTrainedModelDeploymentAction.Response::new)); + transportService.sendRequest( + masterNode, + actionName, + request, + new ActionListenerResponseHandler<>(listener, StopTrainedModelDeploymentAction.Response::new) + ); } } - private void normalUndeploy(Task task, - String modelId, - TrainedModelAllocation modelAllocation, - StopTrainedModelDeploymentAction.Request request, - ActionListener listener) { + private void normalUndeploy( + Task task, + String modelId, + TrainedModelAllocation modelAllocation, + StopTrainedModelDeploymentAction.Request request, + ActionListener listener + ) { request.setNodes(modelAllocation.getNodeRoutingTable().keySet().toArray(String[]::new)); - ActionListener finalListener = ActionListener.wrap( - r -> { - waitForTaskRemoved(modelId, modelAllocation, request, r, ActionListener.wrap( - waited -> { - trainedModelAllocationService.deleteModelAllocation( - modelId, - ActionListener.wrap( - deleted -> listener.onResponse(r), - deletionFailed -> { - logger.error( - () -> new ParameterizedMessage( - "[{}] failed to delete model allocation after nodes unallocated the deployment", - modelId - ),deletionFailed); - listener.onFailure(ExceptionsHelper.serverError( - "failed to delete model allocation after nodes unallocated the deployment. Attempt to stop again", - deletionFailed - )); - } + ActionListener finalListener = ActionListener.wrap(r -> { + waitForTaskRemoved(modelId, modelAllocation, request, r, ActionListener.wrap(waited -> { + trainedModelAllocationService.deleteModelAllocation( + modelId, + ActionListener.wrap(deleted -> listener.onResponse(r), deletionFailed -> { + logger.error( + () -> new ParameterizedMessage( + "[{}] failed to delete model allocation after nodes unallocated the deployment", + modelId + ), + deletionFailed + ); + listener.onFailure( + ExceptionsHelper.serverError( + "failed to delete model allocation after nodes unallocated the deployment. Attempt to stop again", + deletionFailed ) ); - }, - // TODO should we attempt to delete the deployment here? - listener::onFailure - )); - + }) + ); }, - e -> { - if (ExceptionsHelper.unwrapCause(e) instanceof FailedNodeException) { - // A node has dropped out of the cluster since we started executing the requests. - // Since undeploying an already undeployed trained model is not an error we can try again. - // The tasks that were running on the node that dropped out of the cluster - // will just have their persistent tasks cancelled. Tasks that were stopped - // by the previous attempt will be noops in the subsequent attempt. - doExecute(task, request, listener); - } else { - listener.onFailure(e); - } + // TODO should we attempt to delete the deployment here? + listener::onFailure + )); + + }, e -> { + if (ExceptionsHelper.unwrapCause(e) instanceof FailedNodeException) { + // A node has dropped out of the cluster since we started executing the requests. + // Since undeploying an already undeployed trained model is not an error we can try again. + // The tasks that were running on the node that dropped out of the cluster + // will just have their persistent tasks cancelled. Tasks that were stopped + // by the previous attempt will be noops in the subsequent attempt. + doExecute(task, request, listener); + } else { + listener.onFailure(e); } - ); + }); super.doExecute(task, request, finalListener); } - void waitForTaskRemoved(String modelId, - TrainedModelAllocation trainedModelAllocation, - StopTrainedModelDeploymentAction.Request request, - StopTrainedModelDeploymentAction.Response response, - ActionListener listener) { + void waitForTaskRemoved( + String modelId, + TrainedModelAllocation trainedModelAllocation, + StopTrainedModelDeploymentAction.Request request, + StopTrainedModelDeploymentAction.Response response, + ActionListener listener + ) { final Set nodesOfConcern = trainedModelAllocation.getNodeRoutingTable().keySet(); client.admin() .cluster() @@ -223,17 +248,16 @@ void waitForTaskRemoved(String modelId, .setWaitForCompletion(true) .setActions(modelId) .setTimeout(request.getTimeout()) - .execute(ActionListener.wrap( - complete -> listener.onResponse(response), - listener::onFailure - )); + .execute(ActionListener.wrap(complete -> listener.onResponse(response), listener::onFailure)); } @Override - protected StopTrainedModelDeploymentAction.Response newResponse(StopTrainedModelDeploymentAction.Request request, - List tasks, - List taskOperationFailures, - List failedNodeExceptions) { + protected StopTrainedModelDeploymentAction.Response newResponse( + StopTrainedModelDeploymentAction.Request request, + List tasks, + List taskOperationFailures, + List failedNodeExceptions + ) { if (taskOperationFailures.isEmpty() == false) { throw org.elasticsearch.ExceptionsHelper.convertToElastic(taskOperationFailures.get(0).getCause()); } else if (failedNodeExceptions.isEmpty() == false) { @@ -244,8 +268,11 @@ protected StopTrainedModelDeploymentAction.Response newResponse(StopTrainedModel } @Override - protected void taskOperation(StopTrainedModelDeploymentAction.Request request, TrainedModelDeploymentTask task, - ActionListener listener) { + protected void taskOperation( + StopTrainedModelDeploymentAction.Request request, + TrainedModelDeploymentTask task, + ActionListener listener + ) { task.stop("undeploy_trained_model (api)"); listener.onResponse(new StopTrainedModelDeploymentAction.Response(true)); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportUpdateCalendarJobAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportUpdateCalendarJobAction.java index 561b35dca582b..9edf7c7753405 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportUpdateCalendarJobAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportUpdateCalendarJobAction.java @@ -26,8 +26,12 @@ public class TransportUpdateCalendarJobAction extends HandledTransportAction jobIdsToAdd = Strings.tokenizeByCommaToSet(request.getJobIdsToAddExpression()); Set jobIdsToRemove = Strings.tokenizeByCommaToSet(request.getJobIdsToRemoveExpression()); - jobResultsProvider.updateCalendar(request.getCalendarId(), jobIdsToAdd, jobIdsToRemove, - c -> { - jobManager.updateProcessOnCalendarChanged(c.getJobIds(), ActionListener.wrap( - r -> listener.onResponse(new PutCalendarAction.Response(c)), - listener::onFailure - )); - }, listener::onFailure); + jobResultsProvider.updateCalendar(request.getCalendarId(), jobIdsToAdd, jobIdsToRemove, c -> { + jobManager.updateProcessOnCalendarChanged( + c.getJobIds(), + ActionListener.wrap(r -> listener.onResponse(new PutCalendarAction.Response(c)), listener::onFailure) + ); + }, listener::onFailure); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportUpdateDataFrameAnalyticsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportUpdateDataFrameAnalyticsAction.java index 45f3c7fe58977..00428e260f23e 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportUpdateDataFrameAnalyticsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportUpdateDataFrameAnalyticsAction.java @@ -36,8 +36,9 @@ import static org.elasticsearch.xpack.ml.utils.SecondaryAuthorizationUtils.useSecondaryAuthIfAvailable; -public class TransportUpdateDataFrameAnalyticsAction - extends TransportMasterNodeAction { +public class TransportUpdateDataFrameAnalyticsAction extends TransportMasterNodeAction< + UpdateDataFrameAnalyticsAction.Request, + PutDataFrameAnalyticsAction.Response> { private final XPackLicenseState licenseState; private final DataFrameAnalyticsConfigProvider configProvider; @@ -45,13 +46,28 @@ public class TransportUpdateDataFrameAnalyticsAction private final Client client; @Inject - public TransportUpdateDataFrameAnalyticsAction(Settings settings, TransportService transportService, ActionFilters actionFilters, - XPackLicenseState licenseState, ThreadPool threadPool, Client client, - ClusterService clusterService, IndexNameExpressionResolver indexNameExpressionResolver, - DataFrameAnalyticsConfigProvider configProvider) { - super(UpdateDataFrameAnalyticsAction.NAME, transportService, clusterService, threadPool, actionFilters, - UpdateDataFrameAnalyticsAction.Request::new, indexNameExpressionResolver, PutDataFrameAnalyticsAction.Response::new, - ThreadPool.Names.SAME); + public TransportUpdateDataFrameAnalyticsAction( + Settings settings, + TransportService transportService, + ActionFilters actionFilters, + XPackLicenseState licenseState, + ThreadPool threadPool, + Client client, + ClusterService clusterService, + IndexNameExpressionResolver indexNameExpressionResolver, + DataFrameAnalyticsConfigProvider configProvider + ) { + super( + UpdateDataFrameAnalyticsAction.NAME, + transportService, + clusterService, + threadPool, + actionFilters, + UpdateDataFrameAnalyticsAction.Request::new, + indexNameExpressionResolver, + PutDataFrameAnalyticsAction.Response::new, + ThreadPool.Names.SAME + ); this.licenseState = licenseState; this.configProvider = configProvider; this.securityContext = XPackSettings.SECURITY_ENABLED.get(settings) @@ -66,31 +82,44 @@ protected ClusterBlockException checkBlock(UpdateDataFrameAnalyticsAction.Reques } @Override - protected void masterOperation(Task task, UpdateDataFrameAnalyticsAction.Request request, ClusterState state, - ActionListener listener) { + protected void masterOperation( + Task task, + UpdateDataFrameAnalyticsAction.Request request, + ClusterState state, + ActionListener listener + ) { - Runnable doUpdate = () -> - useSecondaryAuthIfAvailable(securityContext, () -> { - Map headers = threadPool.getThreadContext().getHeaders(); - configProvider.update( - request.getUpdate(), - headers, - state, - ActionListener.wrap( - updatedConfig -> listener.onResponse(new PutDataFrameAnalyticsAction.Response(updatedConfig)), - listener::onFailure)); - }); + Runnable doUpdate = () -> useSecondaryAuthIfAvailable(securityContext, () -> { + Map headers = threadPool.getThreadContext().getHeaders(); + configProvider.update( + request.getUpdate(), + headers, + state, + ActionListener.wrap( + updatedConfig -> listener.onResponse(new PutDataFrameAnalyticsAction.Response(updatedConfig)), + listener::onFailure + ) + ); + }); // Obviously if we're updating a job it's impossible that the config index has no mappings at // all, but if we rewrite the job config we may add new fields that require the latest mappings ElasticsearchMappings.addDocMappingIfMissing( - MlConfigIndex.indexName(), MlConfigIndex::mapping, client, state, request.masterNodeTimeout(), - ActionListener.wrap(bool -> doUpdate.run(), listener::onFailure)); + MlConfigIndex.indexName(), + MlConfigIndex::mapping, + client, + state, + request.masterNodeTimeout(), + ActionListener.wrap(bool -> doUpdate.run(), listener::onFailure) + ); } @Override - protected void doExecute(Task task, UpdateDataFrameAnalyticsAction.Request request, - ActionListener listener) { + protected void doExecute( + Task task, + UpdateDataFrameAnalyticsAction.Request request, + ActionListener listener + ) { if (MachineLearningField.ML_API_FEATURE.checkWithoutTracking(licenseState)) { super.doExecute(task, request, listener); } else { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportUpdateDatafeedAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportUpdateDatafeedAction.java index 7f0ad7fdafb90..3b74b751dc705 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportUpdateDatafeedAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportUpdateDatafeedAction.java @@ -25,29 +25,46 @@ import org.elasticsearch.xpack.core.security.SecurityContext; import org.elasticsearch.xpack.ml.datafeed.DatafeedManager; - -public class TransportUpdateDatafeedAction extends - TransportMasterNodeAction { +public class TransportUpdateDatafeedAction extends TransportMasterNodeAction { private final DatafeedManager datafeedManager; private final SecurityContext securityContext; @Inject - public TransportUpdateDatafeedAction(Settings settings, TransportService transportService, ClusterService clusterService, - ThreadPool threadPool, ActionFilters actionFilters, - IndexNameExpressionResolver indexNameExpressionResolver, - DatafeedManager datafeedManager) { - super(UpdateDatafeedAction.NAME, transportService, clusterService, threadPool, actionFilters, UpdateDatafeedAction.Request::new, - indexNameExpressionResolver, PutDatafeedAction.Response::new, ThreadPool.Names.SAME); + public TransportUpdateDatafeedAction( + Settings settings, + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver, + DatafeedManager datafeedManager + ) { + super( + UpdateDatafeedAction.NAME, + transportService, + clusterService, + threadPool, + actionFilters, + UpdateDatafeedAction.Request::new, + indexNameExpressionResolver, + PutDatafeedAction.Response::new, + ThreadPool.Names.SAME + ); this.datafeedManager = datafeedManager; - this.securityContext = XPackSettings.SECURITY_ENABLED.get(settings) ? - new SecurityContext(settings, threadPool.getThreadContext()) : null; + this.securityContext = XPackSettings.SECURITY_ENABLED.get(settings) + ? new SecurityContext(settings, threadPool.getThreadContext()) + : null; } @Override - protected void masterOperation(Task task, UpdateDatafeedAction.Request request, ClusterState state, - ActionListener listener) { + protected void masterOperation( + Task task, + UpdateDatafeedAction.Request request, + ClusterState state, + ActionListener listener + ) { datafeedManager.updateDatafeed(request, state, securityContext, threadPool, listener); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportUpdateFilterAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportUpdateFilterAction.java index 457d1ebeeb03e..77b9a2c122f60 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportUpdateFilterAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportUpdateFilterAction.java @@ -22,15 +22,15 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.index.engine.VersionConflictEngineException; +import org.elasticsearch.tasks.Task; +import org.elasticsearch.transport.TransportService; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.index.engine.VersionConflictEngineException; -import org.elasticsearch.tasks.Task; -import org.elasticsearch.transport.TransportService; import org.elasticsearch.xpack.core.ml.MlMetaIndex; import org.elasticsearch.xpack.core.ml.action.PutFilterAction; import org.elasticsearch.xpack.core.ml.action.UpdateFilterAction; @@ -55,8 +55,13 @@ public class TransportUpdateFilterAction extends HandledTransportAction listener) { - ActionListener filterListener = ActionListener.wrap(filterWithVersion -> { - updateFilter(filterWithVersion, request, listener); - }, listener::onFailure); + ActionListener filterListener = ActionListener.wrap( + filterWithVersion -> { updateFilter(filterWithVersion, request, listener); }, + listener::onFailure + ); getFilterWithVersion(request.getFilterId(), filterListener); } - private void updateFilter(FilterWithSeqNo filterWithVersion, UpdateFilterAction.Request request, - ActionListener listener) { + private void updateFilter( + FilterWithSeqNo filterWithVersion, + UpdateFilterAction.Request request, + ActionListener listener + ) { MlFilter filter = filterWithVersion.filter; if (request.isNoop()) { @@ -88,20 +97,26 @@ private void updateFilter(FilterWithSeqNo filterWithVersion, UpdateFilterAction. for (String toRemove : request.getRemoveItems()) { boolean wasPresent = items.remove(toRemove); if (wasPresent == false) { - listener.onFailure(ExceptionsHelper.badRequestException("Cannot remove item [" + toRemove - + "] as it is not present in filter [" + filter.getId() + "]")); + listener.onFailure( + ExceptionsHelper.badRequestException( + "Cannot remove item [" + toRemove + "] as it is not present in filter [" + filter.getId() + "]" + ) + ); return; } } MlFilter updatedFilter = MlFilter.builder(filter.getId()).setDescription(description).setItems(items).build(); - indexUpdatedFilter( - updatedFilter, filterWithVersion.seqNo, filterWithVersion.primaryTerm, request, listener); + indexUpdatedFilter(updatedFilter, filterWithVersion.seqNo, filterWithVersion.primaryTerm, request, listener); } - private void indexUpdatedFilter(MlFilter filter, final long seqNo, final long primaryTerm, - UpdateFilterAction.Request request, - ActionListener listener) { + private void indexUpdatedFilter( + MlFilter filter, + final long seqNo, + final long primaryTerm, + UpdateFilterAction.Request request, + ActionListener listener + ) { IndexRequest indexRequest = new IndexRequest(MlMetaIndex.indexName()).id(filter.documentId()); indexRequest.setIfSeqNo(seqNo); indexRequest.setIfPrimaryTerm(primaryTerm); @@ -117,18 +132,22 @@ private void indexUpdatedFilter(MlFilter filter, final long seqNo, final long pr executeAsyncWithOrigin(client, ML_ORIGIN, IndexAction.INSTANCE, indexRequest, new ActionListener() { @Override public void onResponse(IndexResponse indexResponse) { - jobManager.notifyFilterChanged(filter, request.getAddItems(), request.getRemoveItems(), ActionListener.wrap( - response -> listener.onResponse(new PutFilterAction.Response(filter)), - listener::onFailure - )); + jobManager.notifyFilterChanged( + filter, + request.getAddItems(), + request.getRemoveItems(), + ActionListener.wrap(response -> listener.onResponse(new PutFilterAction.Response(filter)), listener::onFailure) + ); } @Override public void onFailure(Exception e) { Exception reportedException; if (ExceptionsHelper.unwrapCause(e) instanceof VersionConflictEngineException) { - reportedException = ExceptionsHelper.conflictStatusException("Error updating filter with id [" + filter.getId() - + "] because it was modified while the update was in progress", e); + reportedException = ExceptionsHelper.conflictStatusException( + "Error updating filter with id [" + filter.getId() + "] because it was modified while the update was in progress", + e + ); } else { reportedException = ExceptionsHelper.serverError("Error updating filter with id [" + filter.getId() + "]", e); } @@ -143,9 +162,11 @@ private void getFilterWithVersion(String filterId, ActionListener listener) { + protected void masterOperation( + Task task, + UpdateJobAction.Request request, + ClusterState state, + ActionListener listener + ) { jobManager.updateJob(request, listener); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportUpdateModelSnapshotAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportUpdateModelSnapshotAction.java index aaf0e2141c89c..152fcc436ee59 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportUpdateModelSnapshotAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportUpdateModelSnapshotAction.java @@ -19,11 +19,11 @@ import org.elasticsearch.action.support.WriteRequest; import org.elasticsearch.client.Client; import org.elasticsearch.common.inject.Inject; +import org.elasticsearch.tasks.Task; +import org.elasticsearch.transport.TransportService; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.tasks.Task; -import org.elasticsearch.transport.TransportService; import org.elasticsearch.xpack.core.ml.action.UpdateModelSnapshotAction; import org.elasticsearch.xpack.core.ml.job.messages.Messages; import org.elasticsearch.xpack.core.ml.job.process.autodetect.state.ModelSnapshot; @@ -36,8 +36,9 @@ import static org.elasticsearch.xpack.core.ClientHelper.ML_ORIGIN; import static org.elasticsearch.xpack.core.ClientHelper.executeAsyncWithOrigin; -public class TransportUpdateModelSnapshotAction extends HandledTransportAction { +public class TransportUpdateModelSnapshotAction extends HandledTransportAction< + UpdateModelSnapshotAction.Request, + UpdateModelSnapshotAction.Response> { private static final Logger logger = LogManager.getLogger(TransportUpdateModelSnapshotAction.class); @@ -45,28 +46,39 @@ public class TransportUpdateModelSnapshotAction extends HandledTransportAction listener) { + protected void doExecute( + Task task, + UpdateModelSnapshotAction.Request request, + ActionListener listener + ) { logger.debug("Received request to update model snapshot [{}] for job [{}]", request.getSnapshotId(), request.getJobId()); jobResultsProvider.getModelSnapshot(request.getJobId(), request.getSnapshotId(), modelSnapshot -> { if (modelSnapshot == null) { - listener.onFailure(new ResourceNotFoundException(Messages.getMessage( - Messages.REST_NO_SUCH_MODEL_SNAPSHOT, request.getSnapshotId(), request.getJobId()))); + listener.onFailure( + new ResourceNotFoundException( + Messages.getMessage(Messages.REST_NO_SUCH_MODEL_SNAPSHOT, request.getSnapshotId(), request.getJobId()) + ) + ); } else { Result updatedSnapshot = applyUpdate(request, modelSnapshot); indexModelSnapshot(updatedSnapshot, b -> { // The quantiles can be large, and totally dominate the output - // it's clearer to remove them - listener.onResponse(new UpdateModelSnapshotAction.Response( - new ModelSnapshot.Builder(updatedSnapshot.result).setQuantiles(null).build())); + listener.onResponse( + new UpdateModelSnapshotAction.Response(new ModelSnapshot.Builder(updatedSnapshot.result).setQuantiles(null).build()) + ); }, listener::onFailure); } }, listener::onFailure); @@ -95,17 +107,16 @@ private void indexModelSnapshot(Result modelSnapshot, Consumer() { - @Override - public void onResponse(BulkResponse indexResponse) { - handler.accept(true); - } + executeAsyncWithOrigin(client, ML_ORIGIN, BulkAction.INSTANCE, bulkRequestBuilder.request(), new ActionListener() { + @Override + public void onResponse(BulkResponse indexResponse) { + handler.accept(true); + } - @Override - public void onFailure(Exception e) { - errorHandler.accept(e); - } - }); + @Override + public void onFailure(Exception e) { + errorHandler.accept(e); + } + }); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportUpdateProcessAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportUpdateProcessAction.java index f6dfc06534eed..7a8d5f989d3f0 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportUpdateProcessAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportUpdateProcessAction.java @@ -20,32 +20,43 @@ public class TransportUpdateProcessAction extends TransportJobTaskAction { @Inject - public TransportUpdateProcessAction(TransportService transportService, ClusterService clusterService, - ActionFilters actionFilters, AutodetectProcessManager processManager) { - super(UpdateProcessAction.NAME, clusterService, transportService, actionFilters, - UpdateProcessAction.Request::new, UpdateProcessAction.Response::new, ThreadPool.Names.SAME, processManager); + public TransportUpdateProcessAction( + TransportService transportService, + ClusterService clusterService, + ActionFilters actionFilters, + AutodetectProcessManager processManager + ) { + super( + UpdateProcessAction.NAME, + clusterService, + transportService, + actionFilters, + UpdateProcessAction.Request::new, + UpdateProcessAction.Response::new, + ThreadPool.Names.SAME, + processManager + ); // ThreadPool.Names.SAME, because operations is executed by autodetect worker thread } @Override protected void taskOperation(UpdateProcessAction.Request request, JobTask task, ActionListener listener) { UpdateParams updateParams = UpdateParams.builder(request.getJobId()) - .modelPlotConfig(request.getModelPlotConfig()) - .perPartitionCategorizationConfig(request.getPerPartitionCategorizationConfig()) - .detectorUpdates(request.getDetectorUpdates()) - .filter(request.getFilter()) - .updateScheduledEvents(request.isUpdateScheduledEvents()) - .build(); + .modelPlotConfig(request.getModelPlotConfig()) + .perPartitionCategorizationConfig(request.getPerPartitionCategorizationConfig()) + .detectorUpdates(request.getDetectorUpdates()) + .filter(request.getFilter()) + .updateScheduledEvents(request.isUpdateScheduledEvents()) + .build(); try { - processManager.writeUpdateProcessMessage(task, updateParams, - e -> { - if (e == null) { - listener.onResponse(new UpdateProcessAction.Response()); - } else { - listener.onFailure(e); - } - }); + processManager.writeUpdateProcessMessage(task, updateParams, e -> { + if (e == null) { + listener.onResponse(new UpdateProcessAction.Response()); + } else { + listener.onFailure(e); + } + }); } catch (Exception e) { listener.onFailure(e); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportUpgradeJobModelSnapshotAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportUpgradeJobModelSnapshotAction.java index 09d7b3070ec06..03e8b837e8bd9 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportUpgradeJobModelSnapshotAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportUpgradeJobModelSnapshotAction.java @@ -54,7 +54,6 @@ import org.elasticsearch.xpack.ml.job.snapshot.upgrader.SnapshotUpgradeTaskParams; import org.elasticsearch.xpack.ml.process.MlMemoryTracker; - public class TransportUpgradeJobModelSnapshotAction extends TransportMasterNodeAction { // If the snapshot is from any version other than the current major, we consider it for upgrade. @@ -72,14 +71,31 @@ public class TransportUpgradeJobModelSnapshotAction extends TransportMasterNodeA private final Client client; @Inject - public TransportUpgradeJobModelSnapshotAction(Settings settings, TransportService transportService, ThreadPool threadPool, - XPackLicenseState licenseState, ClusterService clusterService, - PersistentTasksService persistentTasksService, ActionFilters actionFilters, - IndexNameExpressionResolver indexNameExpressionResolver, - JobConfigProvider jobConfigProvider, MlMemoryTracker memoryTracker, - JobResultsProvider jobResultsProvider, Client client) { - super(UpgradeJobModelSnapshotAction.NAME, transportService, clusterService, threadPool, actionFilters, Request::new, - indexNameExpressionResolver, Response::new, ThreadPool.Names.SAME); + public TransportUpgradeJobModelSnapshotAction( + Settings settings, + TransportService transportService, + ThreadPool threadPool, + XPackLicenseState licenseState, + ClusterService clusterService, + PersistentTasksService persistentTasksService, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver, + JobConfigProvider jobConfigProvider, + MlMemoryTracker memoryTracker, + JobResultsProvider jobResultsProvider, + Client client + ) { + super( + UpgradeJobModelSnapshotAction.NAME, + transportService, + clusterService, + threadPool, + actionFilters, + Request::new, + indexNameExpressionResolver, + Response::new, + ThreadPool.Names.SAME + ); this.licenseState = licenseState; this.persistentTasksService = persistentTasksService; this.jobConfigProvider = jobConfigProvider; @@ -95,8 +111,7 @@ protected ClusterBlockException checkBlock(Request request, ClusterState state) } @Override - protected void masterOperation(Task task, Request request, ClusterState state, - ActionListener listener) { + protected void masterOperation(Task task, Request request, ClusterState state, ActionListener listener) { if (migrationEligibilityCheck.jobIsEligibleForMigration(request.getJobId(), state)) { listener.onFailure(ExceptionsHelper.configHasNotBeenMigrated("upgrade job snapshot", request.getJobId())); return; @@ -108,23 +123,31 @@ protected void masterOperation(Task task, Request request, ClusterState state, } if (state.nodes().getMaxNodeVersion().after(state.nodes().getMinNodeVersion())) { - listener.onFailure(ExceptionsHelper.conflictStatusException( - "Cannot upgrade job [{}] snapshot [{}] as not all nodes are on version {}. All nodes must be the same version", - request.getJobId(), - request.getSnapshotId(), - state.nodes().getMaxNodeVersion().toString())); + listener.onFailure( + ExceptionsHelper.conflictStatusException( + "Cannot upgrade job [{}] snapshot [{}] as not all nodes are on version {}. All nodes must be the same version", + request.getJobId(), + request.getSnapshotId(), + state.nodes().getMaxNodeVersion().toString() + ) + ); return; } PersistentTasksCustomMetadata customMetadata = state.getMetadata().custom(PersistentTasksCustomMetadata.TYPE); - if (customMetadata != null && (customMetadata.findTasks( - MlTasks.JOB_SNAPSHOT_UPGRADE_TASK_NAME, - t -> t.getParams() instanceof SnapshotUpgradeTaskParams - && ((SnapshotUpgradeTaskParams)t.getParams()).getJobId().equals(request.getJobId())).isEmpty() == false)) { - listener.onFailure(ExceptionsHelper.conflictStatusException( - "Cannot upgrade job [{}] snapshot [{}] as there is currently a snapshot for this job being upgraded", - request.getJobId(), - request.getSnapshotId())); + if (customMetadata != null + && (customMetadata.findTasks( + MlTasks.JOB_SNAPSHOT_UPGRADE_TASK_NAME, + t -> t.getParams() instanceof SnapshotUpgradeTaskParams + && ((SnapshotUpgradeTaskParams) t.getParams()).getJobId().equals(request.getJobId()) + ).isEmpty() == false)) { + listener.onFailure( + ExceptionsHelper.conflictStatusException( + "Cannot upgrade job [{}] snapshot [{}] as there is currently a snapshot for this job being upgraded", + request.getJobId(), + request.getSnapshotId() + ) + ); return; } @@ -138,23 +161,23 @@ protected void masterOperation(Task task, Request request, ClusterState state, "Cannot upgrade job [{}] snapshot [{}] because upgrade is already in progress", e, request.getJobId(), - request.getSnapshotId()); + request.getSnapshotId() + ); } listener.onFailure(e); - }); + } + ); // Start job task - ActionListener configIndexMappingUpdaterListener = ActionListener.wrap( - _unused -> { - logger.info("[{}] [{}] sending start upgrade request", params.getJobId(), params.getSnapshotId()); - persistentTasksService.sendStartRequest( - MlTasks.snapshotUpgradeTaskId(params.getJobId(), params.getSnapshotId()), - MlTasks.JOB_SNAPSHOT_UPGRADE_TASK_NAME, - params, - waitForJobToStart); - }, - listener::onFailure - ); + ActionListener configIndexMappingUpdaterListener = ActionListener.wrap(_unused -> { + logger.info("[{}] [{}] sending start upgrade request", params.getJobId(), params.getSnapshotId()); + persistentTasksService.sendStartRequest( + MlTasks.snapshotUpgradeTaskId(params.getJobId(), params.getSnapshotId()), + MlTasks.JOB_SNAPSHOT_UPGRADE_TASK_NAME, + params, + waitForJobToStart + ); + }, listener::onFailure); // Update config index if necessary ActionListener memoryRequirementRefreshListener = ActionListener.wrap( @@ -164,67 +187,75 @@ protected void masterOperation(Task task, Request request, ClusterState state, client, state, request.masterNodeTimeout(), - configIndexMappingUpdaterListener), + configIndexMappingUpdaterListener + ), listener::onFailure ); // Check that model snapshot exists and should actually be upgraded // Then refresh the memory - ActionListener> getSnapshotHandler = ActionListener.wrap( - response -> { - if (response == null) { - listener.onFailure( - new ResourceNotFoundException( - Messages.getMessage(Messages.REST_NO_SUCH_MODEL_SNAPSHOT, request.getSnapshotId(), request.getJobId()))); - return; - } - if (Version.CURRENT.equals(response.result.getMinVersion())) { - listener.onFailure(ExceptionsHelper.conflictStatusException( + ActionListener> getSnapshotHandler = ActionListener.wrap(response -> { + if (response == null) { + listener.onFailure( + new ResourceNotFoundException( + Messages.getMessage(Messages.REST_NO_SUCH_MODEL_SNAPSHOT, request.getSnapshotId(), request.getJobId()) + ) + ); + return; + } + if (Version.CURRENT.equals(response.result.getMinVersion())) { + listener.onFailure( + ExceptionsHelper.conflictStatusException( "Cannot upgrade job [{}] snapshot [{}] as it is already compatible with current version {}", request.getJobId(), request.getSnapshotId(), - Version.CURRENT)); - return; - } - memoryTracker.refreshAnomalyDetectorJobMemoryAndAllOthers(params.getJobId(), memoryRequirementRefreshListener); - }, - listener::onFailure - ); + Version.CURRENT + ) + ); + return; + } + memoryTracker.refreshAnomalyDetectorJobMemoryAndAllOthers(params.getJobId(), memoryRequirementRefreshListener); + }, listener::onFailure); - ActionListener getJobHandler = ActionListener.wrap( - job -> { - if (request.getSnapshotId().equals(job.getModelSnapshotId()) - && (JobState.CLOSED.equals(MlTasks.getJobState(request.getJobId(), customMetadata)) == false)) { - listener.onFailure(ExceptionsHelper.conflictStatusException( + ActionListener getJobHandler = ActionListener.wrap(job -> { + if (request.getSnapshotId().equals(job.getModelSnapshotId()) + && (JobState.CLOSED.equals(MlTasks.getJobState(request.getJobId(), customMetadata)) == false)) { + listener.onFailure( + ExceptionsHelper.conflictStatusException( "Cannot upgrade snapshot [{}] for job [{}] as it is the current primary job snapshot and the job's state is [{}]", request.getSnapshotId(), request.getJobId(), MlTasks.getJobState(request.getJobId(), customMetadata) - )); - return; - } - jobResultsProvider.getModelSnapshot( - request.getJobId(), - request.getSnapshotId(), - getSnapshotHandler::onResponse, - getSnapshotHandler::onFailure); - }, - listener::onFailure - ); + ) + ); + return; + } + jobResultsProvider.getModelSnapshot( + request.getJobId(), + request.getSnapshotId(), + getSnapshotHandler::onResponse, + getSnapshotHandler::onFailure + ); + }, listener::onFailure); // Get the job config to verify it exists - jobConfigProvider.getJob(request.getJobId(), ActionListener.wrap( - builder -> getJobHandler.onResponse(builder.build()), - listener::onFailure - )); + jobConfigProvider.getJob( + request.getJobId(), + ActionListener.wrap(builder -> getJobHandler.onResponse(builder.build()), listener::onFailure) + ); } - private void waitForJobStarted(String taskId, - SnapshotUpgradeTaskParams params, - Request request, - ActionListener listener) { + private void waitForJobStarted( + String taskId, + SnapshotUpgradeTaskParams params, + Request request, + ActionListener listener + ) { SnapshotUpgradePredicate predicate = new SnapshotUpgradePredicate(request.isWaitForCompletion(), logger); - persistentTasksService.waitForPersistentTaskCondition(taskId, predicate, request.getTimeout(), + persistentTasksService.waitForPersistentTaskCondition( + taskId, + predicate, + request.getTimeout(), new PersistentTasksService.WaitForPersistentTaskListener() { @Override public void onResponse(PersistentTask persistentTask) { @@ -238,43 +269,46 @@ public void onResponse(PersistentTask persistentTask) } } else { listener.onResponse(new Response(predicate.isCompleted(), predicate.getNode())); + } } - } - @Override - public void onFailure(Exception e) { - listener.onFailure(e); - } + @Override + public void onFailure(Exception e) { + listener.onFailure(e); + } - @Override - public void onTimeout(TimeValue timeout) { - listener.onFailure(new ElasticsearchException( - "snapshot upgrader request [{}] [{}] timed out after [{}]", - params.getJobId(), - params.getSnapshotId(), - timeout)); + @Override + public void onTimeout(TimeValue timeout) { + listener.onFailure( + new ElasticsearchException( + "snapshot upgrader request [{}] [{}] timed out after [{}]", + params.getJobId(), + params.getSnapshotId(), + timeout + ) + ); + } } - }); + ); } - private void cancelJobStart(PersistentTask persistentTask, - Exception exception, - ActionListener listener) { - persistentTasksService.sendRemoveRequest(persistentTask.getId(), - ActionListener.wrap( - t -> listener.onFailure(exception), - e -> { - logger.error( - new ParameterizedMessage( - "[{}] [{}] Failed to cancel persistent task that could not be assigned due to {}", - persistentTask.getParams().getJobId(), - persistentTask.getParams().getSnapshotId(), - exception.getMessage() - ), - e); - listener.onFailure(exception); - } - )); + private void cancelJobStart( + PersistentTask persistentTask, + Exception exception, + ActionListener listener + ) { + persistentTasksService.sendRemoveRequest(persistentTask.getId(), ActionListener.wrap(t -> listener.onFailure(exception), e -> { + logger.error( + new ParameterizedMessage( + "[{}] [{}] Failed to cancel persistent task that could not be assigned due to {}", + persistentTask.getParams().getJobId(), + persistentTask.getParams().getSnapshotId(), + exception.getMessage() + ), + e + ); + listener.onFailure(exception); + })); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportValidateJobConfigAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportValidateJobConfigAction.java index 6c8886906fe26..c5ad4de4f66a7 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportValidateJobConfigAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportValidateJobConfigAction.java @@ -23,8 +23,7 @@ public TransportValidateJobConfigAction(TransportService transportService, Actio } @Override - protected void doExecute(Task task, ValidateJobConfigAction.Request request, - ActionListener listener) { + protected void doExecute(Task task, ValidateJobConfigAction.Request request, ActionListener listener) { listener.onResponse(AcknowledgedResponse.TRUE); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/DoubleArray.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/DoubleArray.java index 725bf8ba34397..1c83145f31bc7 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/DoubleArray.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/DoubleArray.java @@ -11,7 +11,7 @@ public final class DoubleArray { - private DoubleArray() { } + private DoubleArray() {} /** * Returns a NEW {@link double[]} that is the cumulative sum of the passed array diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/MlAggsHelper.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/MlAggsHelper.java index 96df87f76fb1f..87c950e54ccf9 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/MlAggsHelper.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/MlAggsHelper.java @@ -21,12 +21,10 @@ public final class MlAggsHelper { - private MlAggsHelper() { } + private MlAggsHelper() {} public static InvalidAggregationPathException invalidPathException(List path, String aggType, String aggName) { - return new InvalidAggregationPathException( - "unknown property " + path + " for " + aggType + " aggregation [" + aggName + "]" - ); + return new InvalidAggregationPathException("unknown property " + path + " for " + aggType + " aggregation [" + aggName + "]"); } /** @@ -65,10 +63,12 @@ public static Optional extractDoubleBucketedValues(String bu values.add(bucketValue); docCounts.add(bucket.getDocCount()); } - return Optional.of(new DoubleBucketValues( - docCounts.stream().mapToLong(Long::longValue).toArray(), - values.stream().mapToDouble(Double::doubleValue).toArray() - )); + return Optional.of( + new DoubleBucketValues( + docCounts.stream().mapToLong(Long::longValue).toArray(), + values.stream().mapToDouble(Double::doubleValue).toArray() + ) + ); } } return Optional.empty(); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/categorization/CategorizationTokenTree.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/categorization/CategorizationTokenTree.java index f5b5e6daea956..75560ec70555d 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/categorization/CategorizationTokenTree.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/categorization/CategorizationTokenTree.java @@ -18,7 +18,6 @@ import java.util.Optional; import java.util.stream.Collectors; - /** * Categorized semi-structured text utilizing the drain algorithm: https://arxiv.org/pdf/1806.04356.pdf * With the following key differences diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/categorization/CategorizeTextAggregationBuilder.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/categorization/CategorizeTextAggregationBuilder.java index 66938aad1a819..536258bf97c12 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/categorization/CategorizeTextAggregationBuilder.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/categorization/CategorizeTextAggregationBuilder.java @@ -9,10 +9,6 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.xcontent.ObjectParser; -import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.aggregations.AbstractAggregationBuilder; import org.elasticsearch.search.aggregations.AggregationBuilder; import org.elasticsearch.search.aggregations.AggregatorFactories; @@ -20,6 +16,10 @@ import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregator; import org.elasticsearch.search.aggregations.support.AggregationContext; +import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.job.config.CategorizationAnalyzerConfig; import org.elasticsearch.xpack.core.ml.job.messages.Messages; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/categorization/CategorizeTextAggregator.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/categorization/CategorizeTextAggregator.java index 386c41255747d..fccd1bdddb753 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/categorization/CategorizeTextAggregator.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/categorization/CategorizeTextAggregator.java @@ -198,12 +198,8 @@ private void collectFromSource(int doc, long owningBucketOrd, CategorizationToke } } - private void processTokenStream( - long owningBucketOrd, - TokenStream ts, - int doc, - CategorizationTokenTree categorizer - ) throws IOException { + private void processTokenStream(long owningBucketOrd, TokenStream ts, int doc, CategorizationTokenTree categorizer) + throws IOException { ArrayList tokens = new ArrayList<>(); try { CharTermAttribute termAtt = ts.addAttribute(CharTermAttribute.class); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/categorization/InternalCategorizationAggregation.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/categorization/InternalCategorizationAggregation.java index 17d46ad5d86dd..63ccda95b4c34 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/categorization/InternalCategorizationAggregation.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/categorization/InternalCategorizationAggregation.java @@ -13,14 +13,14 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.util.BytesRefHash; -import org.elasticsearch.xcontent.ToXContentFragment; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.search.aggregations.AggregationExecutionException; import org.elasticsearch.search.aggregations.Aggregations; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.InternalAggregations; import org.elasticsearch.search.aggregations.InternalMultiBucketAggregation; import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation; +import org.elasticsearch.xcontent.ToXContentFragment; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.ArrayList; @@ -32,7 +32,6 @@ import static org.elasticsearch.xpack.ml.aggs.categorization.CategorizationBytesRefHash.WILD_CARD_REF; - public class InternalCategorizationAggregation extends InternalMultiBucketAggregation< InternalCategorizationAggregation, InternalCategorizationAggregation.Bucket> { @@ -212,7 +211,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws return builder; } - BucketKey getRawKey() { + BucketKey getRawKey() { return key; } @@ -359,7 +358,7 @@ public InternalAggregation reduce(List aggregations, Reduce similarityThreshold ); // TODO: Could we do a merge sort similar to terms? - // It would require us returning partial reductions sorted by key, not by doc_count + // It would require us returning partial reductions sorted by key, not by doc_count // First, make sure we have all the counts for equal categorizations Map reduced = new HashMap<>(); for (InternalAggregation aggregation : aggregations) { @@ -369,13 +368,9 @@ public InternalAggregation reduce(List aggregations, Reduce } } - reduced.values() - .stream() - .sorted(Comparator.comparing(DelayedCategorizationBucket::getDocCount).reversed()) - .forEach(bucket -> - // Parse tokens takes document count into account and merging on smallest groups - categorizationTokenTree.parseTokens(hash.getIds(bucket.key.keyAsTokens()), bucket.docCount) - ); + reduced.values().stream().sorted(Comparator.comparing(DelayedCategorizationBucket::getDocCount).reversed()).forEach(bucket -> + // Parse tokens takes document count into account and merging on smallest groups + categorizationTokenTree.parseTokens(hash.getIds(bucket.key.keyAsTokens()), bucket.docCount)); categorizationTokenTree.mergeSmallestChildren(); Map mergedBuckets = new HashMap<>(); for (DelayedCategorizationBucket delayedBucket : reduced.values()) { @@ -387,13 +382,13 @@ public InternalAggregation reduce(List aggregations, Reduce ); BytesRef[] categoryTokens = hash.getDeeps(group.getCategorization()); - BucketKey key = reduceContext.isFinalReduce() ? - BucketKey.withCollapsedWildcards(categoryTokens) : - new BucketKey(categoryTokens); + BucketKey key = reduceContext.isFinalReduce() + ? BucketKey.withCollapsedWildcards(categoryTokens) + : new BucketKey(categoryTokens); mergedBuckets.computeIfAbsent( - key, - k -> new DelayedCategorizationBucket(k, new ArrayList<>(delayedBucket.toReduce.size()), 0L) - ).add(delayedBucket); + key, + k -> new DelayedCategorizationBucket(k, new ArrayList<>(delayedBucket.toReduce.size()), 0L) + ).add(delayedBucket); } final int size = reduceContext.isFinalReduce() == false ? mergedBuckets.size() : Math.min(requiredSize, mergedBuckets.size()); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/categorization/TextCategorization.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/categorization/TextCategorization.java index 7ea72f489ae2d..76ec8b59487f4 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/categorization/TextCategorization.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/categorization/TextCategorization.java @@ -82,8 +82,7 @@ void addTokens(int[] tokenIds, long docCount) { @Override public long ramBytesUsed() { - return SHALLOW_SIZE - + RamUsageEstimator.sizeOf(categorization) // categorization token Ids + return SHALLOW_SIZE + RamUsageEstimator.sizeOf(categorization) // categorization token Ids + RamUsageEstimator.sizeOf(tokenCounts); // tokenCounts } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/categorization/TreeNode.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/categorization/TreeNode.java index e74dbe8fe76ce..603ad5f98fe71 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/categorization/TreeNode.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/categorization/TreeNode.java @@ -148,13 +148,13 @@ private Optional> getBestCategorization(int[] } if (textCategorizations.size() == 1) { return Optional.of( - new Tuple<>(textCategorizations.get(0), textCategorizations.get(0).calculateSimilarity( tokenIds).getSimilarity()) + new Tuple<>(textCategorizations.get(0), textCategorizations.get(0).calculateSimilarity(tokenIds).getSimilarity()) ); } TextCategorization.Similarity maxSimilarity = null; TextCategorization bestGroup = null; for (TextCategorization textCategorization : this.textCategorizations) { - TextCategorization.Similarity groupSimilarity = textCategorization.calculateSimilarity( tokenIds); + TextCategorization.Similarity groupSimilarity = textCategorization.calculateSimilarity(tokenIds); if (maxSimilarity == null || groupSimilarity.compareTo(maxSimilarity) > 0) { maxSimilarity = groupSimilarity; bestGroup = textCategorization; @@ -173,8 +173,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; LeafTreeNode that = (LeafTreeNode) o; - return that.similarityThreshold == similarityThreshold - && Objects.equals(textCategorizations, that.textCategorizations); + return that.similarityThreshold == similarityThreshold && Objects.equals(textCategorizations, that.textCategorizations); } @Override diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/categorization/UnmappedCategorizationAggregation.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/categorization/UnmappedCategorizationAggregation.java index ae1081f66d09f..1edb9560608a1 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/categorization/UnmappedCategorizationAggregation.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/categorization/UnmappedCategorizationAggregation.java @@ -13,7 +13,6 @@ import java.util.List; import java.util.Map; - class UnmappedCategorizationAggregation extends InternalCategorizationAggregation { protected UnmappedCategorizationAggregation( String name, diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/correlation/BucketCorrelationAggregationBuilder.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/correlation/BucketCorrelationAggregationBuilder.java index c7c4c671b2364..6608bc32a4536 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/correlation/BucketCorrelationAggregationBuilder.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/correlation/BucketCorrelationAggregationBuilder.java @@ -7,17 +7,17 @@ package org.elasticsearch.xpack.ml.aggs.correlation; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.xcontent.ConstructingObjectParser; -import org.elasticsearch.xcontent.ObjectParser; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.plugins.SearchPlugin; import org.elasticsearch.search.aggregations.pipeline.BucketHelpers; import org.elasticsearch.search.aggregations.pipeline.BucketMetricsPipelineAggregationBuilder; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; +import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.utils.NamedXContentObjectHelper; import java.io.IOException; @@ -38,9 +38,9 @@ public class BucketCorrelationAggregationBuilder extends BucketMetricsPipelineAg false, (args, context) -> new BucketCorrelationAggregationBuilder( context, - (String)args[0], - (CorrelationFunction)args[1], - (BucketHelpers.GapPolicy)args[2] + (String) args[0], + (CorrelationFunction) args[1], + (BucketHelpers.GapPolicy) args[2] ) ); static { @@ -81,7 +81,7 @@ private BucketCorrelationAggregationBuilder( super( name, NAME.getPreferredName(), - new String[] {bucketsPath}, + new String[] { bucketsPath }, null, gapPolicy == null ? BucketHelpers.GapPolicy.INSERT_ZEROS : gapPolicy ); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/correlation/BucketCorrelationAggregator.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/correlation/BucketCorrelationAggregator.java index f595e634dd80e..4b40d24fcfaf9 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/correlation/BucketCorrelationAggregator.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/correlation/BucketCorrelationAggregator.java @@ -22,19 +22,21 @@ public class BucketCorrelationAggregator extends SiblingPipelineAggregator { private final CorrelationFunction correlationFunction; - public BucketCorrelationAggregator(String name, - CorrelationFunction correlationFunction, - String bucketsPath, - Map metadata) { - super(name, new String[]{ bucketsPath }, metadata); + public BucketCorrelationAggregator( + String name, + CorrelationFunction correlationFunction, + String bucketsPath, + Map metadata + ) { + super(name, new String[] { bucketsPath }, metadata); this.correlationFunction = correlationFunction; } @Override public InternalAggregation doReduce(Aggregations aggregations, InternalAggregation.ReduceContext context) { CountCorrelationIndicator bucketPathValue = MlAggsHelper.extractDoubleBucketedValues(bucketsPaths()[0], aggregations) - .map(doubleBucketValues -> - new CountCorrelationIndicator( + .map( + doubleBucketValues -> new CountCorrelationIndicator( doubleBucketValues.getValues(), null, LongStream.of(doubleBucketValues.getDocCounts()).sum() diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/correlation/CorrelationFunction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/correlation/CorrelationFunction.java index 9258d8f784988..37e228ba10382 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/correlation/CorrelationFunction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/correlation/CorrelationFunction.java @@ -11,7 +11,6 @@ import org.elasticsearch.search.aggregations.PipelineAggregationBuilder; import org.elasticsearch.xpack.core.ml.utils.NamedXContentObject; - public interface CorrelationFunction extends NamedWriteable, NamedXContentObject { double execute(CountCorrelationIndicator y); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/correlation/CorrelationNamedContentProvider.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/correlation/CorrelationNamedContentProvider.java index 41e3a4ea2066e..27f075f297328 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/correlation/CorrelationNamedContentProvider.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/correlation/CorrelationNamedContentProvider.java @@ -8,8 +8,8 @@ package org.elasticsearch.xpack.ml.aggs.correlation; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.plugins.spi.NamedXContentProvider; +import org.elasticsearch.xcontent.NamedXContentRegistry; import java.util.Arrays; import java.util.List; diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/correlation/CountCorrelationFunction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/correlation/CountCorrelationFunction.java index 021328cc3a419..8908fe303aa01 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/correlation/CountCorrelationFunction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/correlation/CountCorrelationFunction.java @@ -7,15 +7,15 @@ package org.elasticsearch.xpack.ml.aggs.correlation; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.xcontent.ConstructingObjectParser; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.aggregations.AggregationExecutionException; import org.elasticsearch.search.aggregations.PipelineAggregationBuilder; import org.elasticsearch.search.aggregations.pipeline.MovingFunctions; +import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.Objects; @@ -28,7 +28,7 @@ public class CountCorrelationFunction implements CorrelationFunction { private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( "count_correlation_function", false, - a -> new CountCorrelationFunction((CountCorrelationIndicator)a[0]) + a -> new CountCorrelationFunction((CountCorrelationIndicator) a[0]) ); static { @@ -134,7 +134,7 @@ public double execute(CountCorrelationIndicator y) { } xVar = var; } - final double weight = MovingFunctions.sum(y.getExpectations())/indicator.getDocCount(); + final double weight = MovingFunctions.sum(y.getExpectations()) / indicator.getDocCount(); if (weight > 1.0) { throw new AggregationExecutionException( "doc_count of indicator must be larger than the total count of the correlating values indicator count [" @@ -152,18 +152,14 @@ public double execute(CountCorrelationIndicator y) { for (int i = 0; i < indicator.getExpectations().length; i++) { final double xVal = indicator.getExpectations()[i]; final double nX = y.getExpectations()[i]; - xyCov = xyCov - - (indicator.getDocCount() * fraction - nX) * (xVal - xMean) * yMean - + nX * (xVal - xMean) * (1 - yMean); + xyCov = xyCov - (indicator.getDocCount() * fraction - nX) * (xVal - xMean) * yMean + nX * (xVal - xMean) * (1 - yMean); } } else { for (int i = 0; i < indicator.getExpectations().length; i++) { final double fraction = indicator.getFractions()[i]; final double xVal = indicator.getExpectations()[i]; final double nX = y.getExpectations()[i]; - xyCov = xyCov - - (indicator.getDocCount() * fraction - nX) * (xVal - xMean) * yMean - + nX * (xVal - xMean) * (1 - yMean); + xyCov = xyCov - (indicator.getDocCount() * fraction - nX) * (xVal - xMean) * yMean + nX * (xVal - xMean) * (1 - yMean); } } xyCov /= indicator.getDocCount(); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/correlation/CountCorrelationIndicator.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/correlation/CountCorrelationIndicator.java index 4bf5b80766203..f2f909a11959a 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/correlation/CountCorrelationIndicator.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/correlation/CountCorrelationIndicator.java @@ -7,12 +7,12 @@ package org.elasticsearch.xpack.ml.aggs.correlation; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -32,11 +32,10 @@ public class CountCorrelationIndicator implements Writeable, ToXContentObject { private static final ParseField DOC_COUNT = new ParseField("doc_count"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>( - "correlative_value", - a -> new CountCorrelationIndicator((List) a[0], (List) a[2], (Long) a[1]) - ); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "correlative_value", + a -> new CountCorrelationIndicator((List) a[0], (List) a[2], (Long) a[1]) + ); static { PARSER.declareDoubleArray(ConstructingObjectParser.constructorArg(), EXPECTATIONS); PARSER.declareLong(ConstructingObjectParser.constructorArg(), DOC_COUNT); @@ -46,6 +45,7 @@ public class CountCorrelationIndicator implements Writeable, ToXContentObject { private final double[] expectations; private final double[] fractions; private final long docCount; + private CountCorrelationIndicator(List values, List fractions, long docCount) { this( values.stream().mapToDouble(Double::doubleValue).toArray(), @@ -108,8 +108,7 @@ public long getDocCount() { public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - CountCorrelationIndicator that = - (CountCorrelationIndicator) o; + CountCorrelationIndicator that = (CountCorrelationIndicator) o; return docCount == that.docCount && Arrays.equals(expectations, that.expectations) && Arrays.equals(fractions, that.fractions); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/heuristic/LongBinomialDistribution.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/heuristic/LongBinomialDistribution.java index 8cccb1e4aa9ab..c8207872da38e 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/heuristic/LongBinomialDistribution.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/heuristic/LongBinomialDistribution.java @@ -16,7 +16,6 @@ */ package org.elasticsearch.xpack.ml.aggs.heuristic; - import org.apache.commons.math3.special.Gamma; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathUtils; @@ -32,7 +31,8 @@ public class LongBinomialDistribution { private static final double HALF_LOG_2_PI = 0.5 * FastMath.log(MathUtils.TWO_PI); /** exact Stirling expansion error for certain values. */ - private static final double[] EXACT_STIRLING_ERRORS = { 0.0, /* 0.0 */ + private static final double[] EXACT_STIRLING_ERRORS = { + 0.0, /* 0.0 */ 0.1534264097200273452913848, /* 0.5 */ 0.0810614667953272582196702, /* 1.0 */ 0.0548141210519176538961390, /* 1.5 */ @@ -88,9 +88,7 @@ public double logProbability(long x) { if (x < 0 || x > numberOfTrials) { ret = Double.NEGATIVE_INFINITY; } else { - ret = logBinomialProbability(x, - numberOfTrials, probabilityOfSuccess, - 1.0 - probabilityOfSuccess); + ret = logBinomialProbability(x, numberOfTrials, probabilityOfSuccess, 1.0 - probabilityOfSuccess); } return ret; } @@ -142,17 +140,12 @@ static double getStirlingError(double z) { if (FastMath.floor(z2) == z2) { ret = EXACT_STIRLING_ERRORS[(int) z2]; } else { - ret = Gamma.logGamma(z + 1.0) - (z + 0.5) * FastMath.log(z) + - z - HALF_LOG_2_PI; + ret = Gamma.logGamma(z + 1.0) - (z + 0.5) * FastMath.log(z) + z - HALF_LOG_2_PI; } } else { double z2 = z * z; - ret = (0.083333333333333333333 - - (0.00277777777777777777778 - - (0.00079365079365079365079365 - - (0.000595238095238095238095238 - - 0.0008417508417508417508417508 / - z2) / z2) / z2) / z2) / z; + ret = (0.083333333333333333333 - (0.00277777777777777777778 - (0.00079365079365079365079365 - (0.000595238095238095238095238 + - 0.0008417508417508417508417508 / z2) / z2) / z2) / z2) / z; } return ret; } @@ -181,9 +174,10 @@ static double logBinomialProbability(long x, long n, double p, double q) { ret = n * FastMath.log(p); } } else { - ret = getStirlingError(n) - getStirlingError(x) - - getStirlingError(n - x) - getDeviancePart(x, n * p) - - getDeviancePart(n - x, n * q); + ret = getStirlingError(n) - getStirlingError(x) - getStirlingError(n - x) - getDeviancePart(x, n * p) - getDeviancePart( + n - x, + n * q + ); double f = (MathUtils.TWO_PI * x * (n - x)) / n; ret = -0.5 * FastMath.log(f) + ret; } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/heuristic/MlChiSquaredDistribution.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/heuristic/MlChiSquaredDistribution.java index 53ec4d9212414..296c2e80f4ec1 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/heuristic/MlChiSquaredDistribution.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/heuristic/MlChiSquaredDistribution.java @@ -19,9 +19,7 @@ public MlChiSquaredDistribution(double degreesOfFreedom) { } public double survivalFunction(double x) { - return x <= 0 ? - 1 : - Gamma.regularizedGammaQ(gamma.getShape(), x / gamma.getScale()); + return x <= 0 ? 1 : Gamma.regularizedGammaQ(gamma.getShape(), x / gamma.getScale()); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/heuristic/PValueScore.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/heuristic/PValueScore.java index 7bd67ba9e58b5..402f9d2eb9d22 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/heuristic/PValueScore.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/heuristic/PValueScore.java @@ -5,20 +5,18 @@ * 2.0. */ - package org.elasticsearch.xpack.ml.aggs.heuristic; - import org.apache.commons.math3.util.FastMath; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.search.aggregations.AggregationExecutionException; +import org.elasticsearch.search.aggregations.bucket.terms.heuristic.NXYSignificanceHeuristic; +import org.elasticsearch.search.aggregations.bucket.terms.heuristic.SignificanceHeuristic; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.search.aggregations.AggregationExecutionException; -import org.elasticsearch.search.aggregations.bucket.terms.heuristic.NXYSignificanceHeuristic; -import org.elasticsearch.search.aggregations.bucket.terms.heuristic.SignificanceHeuristic; import java.io.IOException; import java.util.Objects; @@ -39,7 +37,7 @@ public class PValueScore extends NXYSignificanceHeuristic { public static final ParseField NORMALIZE_ABOVE = new ParseField("normalize_above"); public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(NAME, args -> { boolean backgroundIsSuperset = args[0] == null || (boolean) args[0]; - return new PValueScore(backgroundIsSuperset, (Long)args[1]); + return new PValueScore(backgroundIsSuperset, (Long) args[1]); }); static { PARSER.declareBoolean(optionalConstructorArg(), BACKGROUND_IS_SUPERSET); @@ -147,19 +145,19 @@ public double getScore(long subsetFreq, long subsetSize, long supersetFreq, long if (normalizeAbove > 0L) { if (allDocsInClass > normalizeAbove) { double factor = (double) normalizeAbove / allDocsInClass; - allDocsInClass = (long)(allDocsInClass * factor); - docsContainTermInClass = (long)(docsContainTermInClass * factor); + allDocsInClass = (long) (allDocsInClass * factor); + docsContainTermInClass = (long) (docsContainTermInClass * factor); } if (allDocsNotInClass > normalizeAbove) { double factor = (double) normalizeAbove / allDocsNotInClass; - allDocsNotInClass = (long)(allDocsNotInClass * factor); - docsContainTermNotInClass = (long)(docsContainTermNotInClass * factor); + allDocsNotInClass = (long) (allDocsNotInClass * factor); + docsContainTermNotInClass = (long) (docsContainTermNotInClass * factor); } } // casting to `long` to round down to nearest whole number - double epsAllDocsInClass = (long)eps(allDocsInClass); - double epsAllDocsNotInClass = (long)eps(allDocsNotInClass); + double epsAllDocsInClass = (long) eps(allDocsInClass); + double epsAllDocsNotInClass = (long) eps(allDocsNotInClass); docsContainTermInClass += epsAllDocsInClass; docsContainTermNotInClass += epsAllDocsNotInClass; @@ -168,7 +166,7 @@ public double getScore(long subsetFreq, long subsetSize, long supersetFreq, long // Adjust counts to ignore ratio changes which are less than 5% // casting to `long` to round down to nearest whole number - docsContainTermNotInClass = (long)(Math.min( + docsContainTermNotInClass = (long) (Math.min( 1.05 * docsContainTermNotInClass, docsContainTermInClass / allDocsInClass * allDocsNotInClass ) + 0.5); @@ -182,21 +180,19 @@ public double getScore(long subsetFreq, long subsetSize, long supersetFreq, long ); } - double v1 = new LongBinomialDistribution( - (long)allDocsInClass, docsContainTermInClass / allDocsInClass - ).logProbability((long)docsContainTermInClass); + double v1 = new LongBinomialDistribution((long) allDocsInClass, docsContainTermInClass / allDocsInClass).logProbability( + (long) docsContainTermInClass + ); - double v2 = new LongBinomialDistribution( - (long)allDocsNotInClass, docsContainTermNotInClass / allDocsNotInClass - ).logProbability((long)docsContainTermNotInClass); + double v2 = new LongBinomialDistribution((long) allDocsNotInClass, docsContainTermNotInClass / allDocsNotInClass).logProbability( + (long) docsContainTermNotInClass + ); double p2 = (docsContainTermInClass + docsContainTermNotInClass) / (allDocsInClass + allDocsNotInClass); - double v3 = new LongBinomialDistribution((long)allDocsInClass, p2) - .logProbability((long)docsContainTermInClass); + double v3 = new LongBinomialDistribution((long) allDocsInClass, p2).logProbability((long) docsContainTermInClass); - double v4 = new LongBinomialDistribution((long)allDocsNotInClass, p2) - .logProbability((long)docsContainTermNotInClass); + double v4 = new LongBinomialDistribution((long) allDocsNotInClass, p2).logProbability((long) docsContainTermNotInClass); double logLikelihoodRatio = v1 + v2 - v3 - v4; double pValue = CHI_SQUARED_DISTRIBUTION.survivalFunction(2.0 * logLikelihoodRatio); @@ -232,4 +228,3 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws } } } - diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/inference/InferencePipelineAggregationBuilder.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/inference/InferencePipelineAggregationBuilder.java index 9e6b0734595ee..e321d8ccf28e4 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/inference/InferencePipelineAggregationBuilder.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/inference/InferencePipelineAggregationBuilder.java @@ -10,15 +10,10 @@ import org.apache.lucene.util.SetOnce; import org.elasticsearch.action.ActionListener; import org.elasticsearch.client.Client; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.ConstructingObjectParser; -import org.elasticsearch.xcontent.ContextParser; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.index.query.QueryRewriteContext; import org.elasticsearch.license.LicenseUtils; import org.elasticsearch.license.XPackLicenseState; @@ -26,6 +21,11 @@ import org.elasticsearch.search.aggregations.PipelineAggregationBuilder; import org.elasticsearch.search.aggregations.pipeline.AbstractPipelineAggregationBuilder; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; +import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ContextParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.XPackField; import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.xpack.core.ml.MachineLearningField; @@ -64,24 +64,43 @@ public class InferencePipelineAggregationBuilder extends AbstractPipelineAggrega @SuppressWarnings("unchecked") private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(NAME, false, - (args, context) -> new InferencePipelineAggregationBuilder(context.name, context.modelLoadingService, - context.licenseState, context.settings, (Map) args[0]) - ); + new ConstructingObjectParser<>( + NAME, + false, + (args, context) -> new InferencePipelineAggregationBuilder( + context.name, + context.modelLoadingService, + context.licenseState, + context.settings, + (Map) args[0] + ) + ); static { PARSER.declareObject(constructorArg(), (p, c) -> p.mapStrings(), BUCKETS_PATH_FIELD); PARSER.declareString(InferencePipelineAggregationBuilder::setModelId, MODEL_ID); - PARSER.declareNamedObject(InferencePipelineAggregationBuilder::setInferenceConfig, - (p, c, n) -> p.namedObject(InferenceConfigUpdate.class, n, c), INFERENCE_CONFIG); + PARSER.declareNamedObject( + InferencePipelineAggregationBuilder::setInferenceConfig, + (p, c, n) -> p.namedObject(InferenceConfigUpdate.class, n, c), + INFERENCE_CONFIG + ); } - public static SearchPlugin.PipelineAggregationSpec buildSpec(SetOnce modelLoadingService, - XPackLicenseState xPackLicenseState, Settings settings) { - SearchPlugin.PipelineAggregationSpec spec = new SearchPlugin.PipelineAggregationSpec(InferencePipelineAggregationBuilder.NAME, + public static SearchPlugin.PipelineAggregationSpec buildSpec( + SetOnce modelLoadingService, + XPackLicenseState xPackLicenseState, + Settings settings + ) { + SearchPlugin.PipelineAggregationSpec spec = new SearchPlugin.PipelineAggregationSpec( + InferencePipelineAggregationBuilder.NAME, in -> new InferencePipelineAggregationBuilder(in, xPackLicenseState, settings, modelLoadingService), - (ContextParser) - (parser, name) -> InferencePipelineAggregationBuilder.parse(modelLoadingService, xPackLicenseState, settings, name, parser) + (ContextParser) (parser, name) -> InferencePipelineAggregationBuilder.parse( + modelLoadingService, + xPackLicenseState, + settings, + name, + parser + ) ); spec.addResultReader(InternalInferenceAggregation::new); return spec; @@ -111,19 +130,24 @@ private static class ParserSupplement { this.modelLoadingService = modelLoadingService; } } - public static InferencePipelineAggregationBuilder parse(SetOnce modelLoadingService, - XPackLicenseState licenseState, - Settings settings, - String pipelineAggregatorName, - XContentParser parser) { + + public static InferencePipelineAggregationBuilder parse( + SetOnce modelLoadingService, + XPackLicenseState licenseState, + Settings settings, + String pipelineAggregatorName, + XContentParser parser + ) { return PARSER.apply(parser, new ParserSupplement(pipelineAggregatorName, licenseState, settings, modelLoadingService)); } - public InferencePipelineAggregationBuilder(String name, - SetOnce modelLoadingService, - XPackLicenseState licenseState, - Settings settings, - Map bucketsPath) { + public InferencePipelineAggregationBuilder( + String name, + SetOnce modelLoadingService, + XPackLicenseState licenseState, + Settings settings, + Map bucketsPath + ) { super(name, NAME, new TreeMap<>(bucketsPath).values().toArray(new String[] {})); this.modelLoadingService = modelLoadingService; this.bucketPathMap = bucketsPath; @@ -132,10 +156,12 @@ public InferencePipelineAggregationBuilder(String name, this.settings = settings; } - public InferencePipelineAggregationBuilder(StreamInput in, - XPackLicenseState licenseState, - Settings settings, - SetOnce modelLoadingService) throws IOException { + public InferencePipelineAggregationBuilder( + StreamInput in, + XPackLicenseState licenseState, + Settings settings, + SetOnce modelLoadingService + ) throws IOException { super(in, NAME); modelId = in.readString(); bucketPathMap = in.readMap(StreamInput::readString, StreamInput::readString); @@ -194,19 +220,29 @@ protected void validate(ValidationContext context) { // error if the results field is set and not equal to the only acceptable value String resultsField = inferenceConfig.getResultsField(); if (Strings.isNullOrEmpty(resultsField) == false && AGGREGATIONS_RESULTS_FIELD.equals(resultsField) == false) { - context.addValidationError("setting option [" + ClassificationConfig.RESULTS_FIELD.getPreferredName() - + "] to [" + resultsField + "] is not valid for inference aggregations"); + context.addValidationError( + "setting option [" + + ClassificationConfig.RESULTS_FIELD.getPreferredName() + + "] to [" + + resultsField + + "] is not valid for inference aggregations" + ); } if (inferenceConfig instanceof ClassificationConfigUpdate) { - ClassificationConfigUpdate classUpdate = (ClassificationConfigUpdate)inferenceConfig; + ClassificationConfigUpdate classUpdate = (ClassificationConfigUpdate) inferenceConfig; // error if the top classes result field is set and not equal to the only acceptable value String topClassesField = classUpdate.getTopClassesResultsField(); - if (Strings.isNullOrEmpty(topClassesField) == false && - ClassificationConfig.DEFAULT_TOP_CLASSES_RESULTS_FIELD.equals(topClassesField) == false) { - context.addValidationError("setting option [" + ClassificationConfig.DEFAULT_TOP_CLASSES_RESULTS_FIELD - + "] to [" + topClassesField + "] is not valid for inference aggregations"); + if (Strings.isNullOrEmpty(topClassesField) == false + && ClassificationConfig.DEFAULT_TOP_CLASSES_RESULTS_FIELD.equals(topClassesField) == false) { + context.addValidationError( + "setting option [" + + ClassificationConfig.DEFAULT_TOP_CLASSES_RESULTS_FIELD + + "] to [" + + topClassesField + + "] is not valid for inference aggregations" + ); } } } @@ -226,12 +262,12 @@ public InferencePipelineAggregationBuilder rewrite(QueryRewriteContext context) } SetOnce loadedModel = new SetOnce<>(); - BiConsumer> modelLoadAction = (client, listener) -> - modelLoadingService.get().getModelForSearch(modelId, listener.delegateFailure((delegate, model) -> { + BiConsumer> modelLoadAction = (client, listener) -> modelLoadingService.get() + .getModelForSearch(modelId, listener.delegateFailure((delegate, model) -> { loadedModel.set(model); - boolean isLicensed = MachineLearningField.ML_API_FEATURE.check(licenseState) || - licenseState.isAllowedByLicense(model.getLicenseLevel()); + boolean isLicensed = MachineLearningField.ML_API_FEATURE.check(licenseState) + || licenseState.isAllowedByLicense(model.getLicenseLevel()); if (isLicensed) { delegate.onResponse(null); } else { @@ -239,7 +275,6 @@ public InferencePipelineAggregationBuilder rewrite(QueryRewriteContext context) } })); - context.registerAsyncAction((client, listener) -> { if (XPackSettings.SECURITY_ENABLED.get(settings)) { // check the user has ml privileges @@ -249,19 +284,20 @@ public InferencePipelineAggregationBuilder rewrite(QueryRewriteContext context) final HasPrivilegesRequest privRequest = new HasPrivilegesRequest(); privRequest.username(username); privRequest.clusterPrivileges(GetTrainedModelsAction.NAME); - privRequest.indexPrivileges(new RoleDescriptor.IndicesPrivileges[]{}); - privRequest.applicationPrivileges(new RoleDescriptor.ApplicationResourcePrivileges[]{}); - - ActionListener privResponseListener = ActionListener.wrap( - r -> { - if (r.isCompleteMatch()) { - modelLoadAction.accept(client, listener); - } else { - listener.onFailure(Exceptions.authorizationError("user [" + username - + "] does not have the privilege to get trained models so cannot use ml inference")); - } - }, - listener::onFailure); + privRequest.indexPrivileges(new RoleDescriptor.IndicesPrivileges[] {}); + privRequest.applicationPrivileges(new RoleDescriptor.ApplicationResourcePrivileges[] {}); + + ActionListener privResponseListener = ActionListener.wrap(r -> { + if (r.isCompleteMatch()) { + modelLoadAction.accept(client, listener); + } else { + listener.onFailure( + Exceptions.authorizationError( + "user [" + username + "] does not have the privilege to get trained models so cannot use ml inference" + ) + ); + } + }, listener::onFailure); client.execute(HasPrivilegesAction.INSTANCE, privRequest, privResponseListener); }); @@ -269,8 +305,15 @@ public InferencePipelineAggregationBuilder rewrite(QueryRewriteContext context) modelLoadAction.accept(client, listener); } }); - return new InferencePipelineAggregationBuilder(name, bucketPathMap, loadedModel::get, modelId, inferenceConfig, licenseState, - settings); + return new InferencePipelineAggregationBuilder( + name, + bucketPathMap, + loadedModel::get, + modelId, + inferenceConfig, + licenseState, + settings + ); } @Override diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/inference/InferencePipelineAggregator.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/inference/InferencePipelineAggregator.java index 4c6399748fcee..5543b480d6bb6 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/inference/InferencePipelineAggregator.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/inference/InferencePipelineAggregator.java @@ -28,25 +28,26 @@ import java.util.Map; import java.util.stream.Collectors; - public class InferencePipelineAggregator extends PipelineAggregator { private final Map bucketPathMap; private final InferenceConfigUpdate configUpdate; private final LocalModel model; - public InferencePipelineAggregator(String name, Map bucketPathMap, - Map metaData, - InferenceConfigUpdate configUpdate, - LocalModel model) { + public InferencePipelineAggregator( + String name, + Map bucketPathMap, + Map metaData, + InferenceConfigUpdate configUpdate, + LocalModel model + ) { super(name, bucketPathMap.values().toArray(new String[] {}), metaData); this.bucketPathMap = bucketPathMap; this.configUpdate = configUpdate; this.model = model; } - @SuppressWarnings({"rawtypes", "unchecked"}) + @SuppressWarnings({ "rawtypes", "unchecked" }) @Override public InternalAggregation reduce(InternalAggregation aggregation, InternalAggregation.ReduceContext reduceContext) { @@ -96,7 +97,6 @@ public InternalAggregation reduce(InternalAggregation aggregation, InternalAggre } } - InferenceResults inference; try { inference = model.infer(inputFields, configUpdate); @@ -104,8 +104,11 @@ public InternalAggregation reduce(InternalAggregation aggregation, InternalAggre inference = new WarningInferenceResults(e.getMessage()); } - final List aggs = bucket.getAggregations().asList().stream().map( - (p) -> (InternalAggregation) p).collect(Collectors.toList()); + final List aggs = bucket.getAggregations() + .asList() + .stream() + .map((p) -> (InternalAggregation) p) + .collect(Collectors.toList()); InternalInferenceAggregation aggResult = new InternalInferenceAggregation(name(), metadata(), inference); aggs.add(aggResult); @@ -120,9 +123,11 @@ public InternalAggregation reduce(InternalAggregation aggregation, InternalAggre } } - public static Object resolveBucketValue(MultiBucketsAggregation agg, - InternalMultiBucketAggregation.InternalBucket bucket, - String aggPath) { + public static Object resolveBucketValue( + MultiBucketsAggregation agg, + InternalMultiBucketAggregation.InternalBucket bucket, + String aggPath + ) { List aggPathsList = AggregationPath.parse(aggPath).getPathElementsAsStringList(); return bucket.getProperty(agg.getName(), aggPathsList); @@ -130,10 +135,15 @@ public static Object resolveBucketValue(MultiBucketsAggregation agg, private static AggregationExecutionException invalidAggTypeError(String aggPath, Object propertyValue) { - String msg = AbstractPipelineAggregationBuilder.BUCKETS_PATH_FIELD.getPreferredName() + - " must reference either a number value, a single value numeric metric aggregation or a string: got [" + - propertyValue + "] of type [" + propertyValue.getClass().getSimpleName() + "] " + - "] at aggregation [" + aggPath + "]"; + String msg = AbstractPipelineAggregationBuilder.BUCKETS_PATH_FIELD.getPreferredName() + + " must reference either a number value, a single value numeric metric aggregation or a string: got [" + + propertyValue + + "] of type [" + + propertyValue.getClass().getSimpleName() + + "] " + + "] at aggregation [" + + aggPath + + "]"; return new AggregationExecutionException(msg); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/inference/InternalInferenceAggregation.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/inference/InternalInferenceAggregation.java index 57ea71b3ea32f..732d79a503737 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/inference/InternalInferenceAggregation.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/inference/InternalInferenceAggregation.java @@ -9,8 +9,8 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.search.aggregations.InternalAggregation; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ml.inference.results.InferenceResults; import java.io.IOException; @@ -20,12 +20,11 @@ import static org.elasticsearch.xpack.ml.aggs.MlAggsHelper.invalidPathException; -public class InternalInferenceAggregation extends InternalAggregation { +public class InternalInferenceAggregation extends InternalAggregation { private final InferenceResults inferenceResult; - protected InternalInferenceAggregation(String name, Map metadata, - InferenceResults inferenceResult) { + protected InternalInferenceAggregation(String name, Map metadata, InferenceResults inferenceResult) { super(name, metadata); this.inferenceResult = inferenceResult; } @@ -73,7 +72,6 @@ public Object getProperty(List path) { return propertyValue; } - @Override public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException { return inferenceResult.toXContent(builder, params); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/kstest/BucketCountKSTestAggregationBuilder.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/kstest/BucketCountKSTestAggregationBuilder.java index a98c19042fcaa..4c6623eb55c6b 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/kstest/BucketCountKSTestAggregationBuilder.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/kstest/BucketCountKSTestAggregationBuilder.java @@ -7,18 +7,18 @@ package org.elasticsearch.xpack.ml.aggs.kstest; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.xcontent.ConstructingObjectParser; -import org.elasticsearch.xcontent.ObjectParser; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.core.Nullable; import org.elasticsearch.plugins.SearchPlugin; import org.elasticsearch.search.aggregations.pipeline.BucketHelpers; import org.elasticsearch.search.aggregations.pipeline.BucketMetricsPipelineAggregationBuilder; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; +import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.Arrays; @@ -72,6 +72,7 @@ public class BucketCountKSTestAggregationBuilder extends BucketMetricsPipelineAg private final double[] fractions; private final EnumSet alternative; private final SamplingMethod samplingMethod; + private BucketCountKSTestAggregationBuilder( String name, String bucketsPath, diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/kstest/BucketCountKSTestAggregator.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/kstest/BucketCountKSTestAggregator.java index 25b23d34c3f98..9d404697028d6 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/kstest/BucketCountKSTestAggregator.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/kstest/BucketCountKSTestAggregator.java @@ -8,8 +8,8 @@ package org.elasticsearch.xpack.ml.aggs.kstest; import org.apache.commons.math3.stat.inference.KolmogorovSmirnovTest; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Randomness; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.search.aggregations.AggregationExecutionException; import org.elasticsearch.search.aggregations.Aggregations; diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/kstest/InternalKSTestAggregation.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/kstest/InternalKSTestAggregation.java index f42e0be73d87a..3378779c1557b 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/kstest/InternalKSTestAggregation.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/aggs/kstest/InternalKSTestAggregation.java @@ -9,8 +9,8 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.search.aggregations.InternalAggregation; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.List; diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/annotations/AnnotationPersister.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/annotations/AnnotationPersister.java index d7e3c19656d9b..0ff4061d108f0 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/annotations/AnnotationPersister.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/annotations/AnnotationPersister.java @@ -111,10 +111,12 @@ public BulkResponse executeRequest() { return null; } logger.trace("[{}] ES API CALL: bulk request with {} actions", () -> jobId, () -> bulkRequest.numberOfActions()); - BulkResponse bulkResponse = - resultsPersisterService.bulkIndexWithRetry( - bulkRequest, jobId, shouldRetry, - retryMessage -> logger.debug("[{}] Bulk indexing of annotations failed {}", jobId, retryMessage)); + BulkResponse bulkResponse = resultsPersisterService.bulkIndexWithRetry( + bulkRequest, + jobId, + shouldRetry, + retryMessage -> logger.debug("[{}] Bulk indexing of annotations failed {}", jobId, retryMessage) + ); bulkRequest = new BulkRequest(AnnotationIndex.WRITE_ALIAS_NAME); return bulkResponse; } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/autoscaling/MlAutoscalingDeciderService.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/autoscaling/MlAutoscalingDeciderService.java index b61e3ce55a6cd..d37eb73ccfdfd 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/autoscaling/MlAutoscalingDeciderService.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/autoscaling/MlAutoscalingDeciderService.java @@ -69,8 +69,7 @@ import static org.elasticsearch.xpack.ml.MachineLearning.NATIVE_EXECUTABLE_CODE_OVERHEAD; import static org.elasticsearch.xpack.ml.job.JobNodeSelector.AWAITING_LAZY_ASSIGNMENT; -public class MlAutoscalingDeciderService implements AutoscalingDeciderService, - LocalNodeMasterListener { +public class MlAutoscalingDeciderService implements AutoscalingDeciderService, LocalNodeMasterListener { private static final Logger logger = LogManager.getLogger(MlAutoscalingDeciderService.class); private static final Duration DEFAULT_MEMORY_REFRESH_RATE = Duration.ofMinutes(15); @@ -100,10 +99,12 @@ public MlAutoscalingDeciderService(MlMemoryTracker memoryTracker, Settings setti this(new NodeLoadDetector(memoryTracker), settings, clusterService, System::currentTimeMillis); } - MlAutoscalingDeciderService(NodeLoadDetector nodeLoadDetector, - Settings settings, - ClusterService clusterService, - LongSupplier timeSupplier) { + MlAutoscalingDeciderService( + NodeLoadDetector nodeLoadDetector, + Settings settings, + ClusterService clusterService, + LongSupplier timeSupplier + ) { this.nodeLoadDetector = nodeLoadDetector; this.mlMemoryTracker = nodeLoadDetector.getMlMemoryTracker(); this.maxMachineMemoryPercent = MachineLearning.MAX_MACHINE_MEMORY_PERCENT.get(settings); @@ -111,8 +112,8 @@ public MlAutoscalingDeciderService(MlMemoryTracker memoryTracker, Settings setti this.useAuto = MachineLearning.USE_AUTO_MACHINE_MEMORY_PERCENT.get(settings); this.timeSupplier = timeSupplier; this.scaleDownDetected = NO_SCALE_DOWN_POSSIBLE; - clusterService.getClusterSettings().addSettingsUpdateConsumer(MachineLearning.MAX_MACHINE_MEMORY_PERCENT, - this::setMaxMachineMemoryPercent); + clusterService.getClusterSettings() + .addSettingsUpdateConsumer(MachineLearning.MAX_MACHINE_MEMORY_PERCENT, this::setMaxMachineMemoryPercent); clusterService.getClusterSettings().addSettingsUpdateConsumer(MAX_OPEN_JOBS_PER_NODE, this::setMaxOpenJobs); clusterService.getClusterSettings().addSettingsUpdateConsumer(MachineLearning.USE_AUTO_MACHINE_MEMORY_PERCENT, this::setUseAuto); clusterService.addLocalNodeMasterListener(this); @@ -136,19 +137,19 @@ static OptionalLong getNodeJvmSize(DiscoveryNode node) { try { value = OptionalLong.of(Long.parseLong(valueStr)); } catch (NumberFormatException e) { - logger.debug(() -> new ParameterizedMessage( - "could not parse stored string value [{}] in node attribute [{}]", - valueStr, - MachineLearning.MAX_JVM_SIZE_NODE_ATTR)); + logger.debug( + () -> new ParameterizedMessage( + "could not parse stored string value [{}] in node attribute [{}]", + valueStr, + MachineLearning.MAX_JVM_SIZE_NODE_ATTR + ) + ); } return value; } static List getNodes(final ClusterState clusterState) { - return clusterState.nodes() - .mastersFirstStream() - .filter(MachineLearning::isMlNode) - .collect(Collectors.toList()); + return clusterState.nodes().mastersFirstStream().filter(MachineLearning::isMlNode).collect(Collectors.toList()); } /** @@ -157,14 +158,15 @@ static List getNodes(final ClusterState clusterState) { * @param maxNumInQueue The number of unassigned jobs allowed. * @return The capacity needed to reduce the length of `unassignedJobs` to `maxNumInQueue` */ - static Optional requiredCapacityForUnassignedJobs(List unassignedJobs, - Function sizeFunction, - int maxNumInQueue) { + static Optional requiredCapacityForUnassignedJobs( + List unassignedJobs, + Function sizeFunction, + int maxNumInQueue + ) { if (unassignedJobs.isEmpty()) { return Optional.empty(); } - List jobSizes = unassignedJobs - .stream() + List jobSizes = unassignedJobs.stream() .map(sizeFunction) .map(l -> l == null ? 0L : l) .sorted(Comparator.comparingLong(Long::longValue).reversed()) @@ -181,10 +183,12 @@ static Optional requiredCapacityForUnassignedJobs(List>> determineUnassignableJobs(List unassignedJobs, - Function sizeFunction, - int maxNumInQueue, - List nodeLoads) { + static Optional>> determineUnassignableJobs( + List unassignedJobs, + Function sizeFunction, + int maxNumInQueue, + List nodeLoads + ) { if (unassignedJobs.isEmpty()) { return Optional.empty(); } @@ -199,8 +203,7 @@ static Optional>> determineUnassignab for (NodeLoad load : nodeLoads) { mostFreeMemoryFirst.add(NodeLoad.builder(load)); } - List jobSizes = unassignedJobs - .stream() + List jobSizes = unassignedJobs.stream() .map(sizeFunction) .map(l -> l == null ? 0L : l) .sorted(Comparator.comparingLong(Long::longValue).reversed()) @@ -235,14 +238,16 @@ static Optional>> determineUnassignab // We don't need to scale but we have adjusted node load given what we could assign return Optional.of(Tuple.tuple(NativeMemoryCapacity.ZERO, adjustedLoads)); } - return Optional.of(Tuple.tuple( - new NativeMemoryCapacity( - unassignableMemory.stream().mapToLong(Long::longValue).sum(), - // Node memory needs to be AT LEAST the size of the largest job + the required overhead. - unassignableMemory.get(0) + NATIVE_EXECUTABLE_CODE_OVERHEAD.getBytes() - ), - adjustedLoads - )); + return Optional.of( + Tuple.tuple( + new NativeMemoryCapacity( + unassignableMemory.stream().mapToLong(Long::longValue).sum(), + // Node memory needs to be AT LEAST the size of the largest job + the required overhead. + unassignableMemory.get(0) + NATIVE_EXECUTABLE_CODE_OVERHEAD.getBytes() + ), + adjustedLoads + ) + ); } private static Collection> anomalyDetectionTasks(PersistentTasksCustomMetadata tasksCustomMetadata) { @@ -298,9 +303,11 @@ private boolean newScaleDownCheck() { return scaleDownDetected == NO_SCALE_DOWN_POSSIBLE; } - public static NativeMemoryCapacity currentScale(final List machineLearningNodes, - int maxMachineMemoryPercent, - boolean useAuto) { + public static NativeMemoryCapacity currentScale( + final List machineLearningNodes, + int maxMachineMemoryPercent, + boolean useAuto + ) { long[] mlMemory = machineLearningNodes.stream() .mapToLong(node -> NativeMemoryCalculator.allowedBytesForMl(node, maxMachineMemoryPercent, useAuto).orElse(0L)) .toArray(); @@ -355,8 +362,7 @@ public AutoscalingDeciderResult scale(Settings configuration, AutoscalingDecider .filter(t -> AWAITING_LAZY_ASSIGNMENT.equals(t.getAssignment())) .map(t -> MlTasks.dataFrameAnalyticsId(t.getId())) .collect(Collectors.toList()); - final List waitingAllocatedModels = modelAllocations - .entrySet() + final List waitingAllocatedModels = modelAllocations.entrySet() .stream() // TODO: Eventually care about those that are STARTED but not FULLY_ALLOCATED .filter(e -> e.getValue().getAllocationState().equals(AllocationState.STARTING) && e.getValue().getNodeRoutingTable().isEmpty()) @@ -379,8 +385,8 @@ public AutoscalingDeciderResult scale(Settings configuration, AutoscalingDecider // There are no ML nodes, scale up as quick as possible, no matter if memory is stale or not if (nodes.isEmpty() && (waitingAnomalyJobs.isEmpty() == false - || waitingAnalyticsJobs.isEmpty() == false - || waitingAllocatedModels.isEmpty() == false)) { + || waitingAnalyticsJobs.isEmpty() == false + || waitingAllocatedModels.isEmpty() == false)) { return scaleUpFromZero(waitingAnomalyJobs, waitingAnalyticsJobs, waitingAllocatedModels, reasonBuilder); } @@ -392,55 +398,54 @@ public AutoscalingDeciderResult scale(Settings configuration, AutoscalingDecider if (msLeftToScale > 0) { return new AutoscalingDeciderResult( context.currentCapacity(), - reasonBuilder - .setSimpleReason( - String.format( - Locale.ROOT, - "Passing currently perceived capacity as down scale delay has not been satisfied; configured delay [%s]" - + "last detected scale down event [%s]. Will request scale down in approximately [%s]", - DOWN_SCALE_DELAY.get(configuration).getStringRep(), - XContentElasticsearchExtension.DEFAULT_FORMATTER.format(Instant.ofEpochMilli(scaleDownDetected)), - TimeValue.timeValueMillis(msLeftToScale).getStringRep() - ) + reasonBuilder.setSimpleReason( + String.format( + Locale.ROOT, + "Passing currently perceived capacity as down scale delay has not been satisfied; configured delay [%s]" + + "last detected scale down event [%s]. Will request scale down in approximately [%s]", + DOWN_SCALE_DELAY.get(configuration).getStringRep(), + XContentElasticsearchExtension.DEFAULT_FORMATTER.format(Instant.ofEpochMilli(scaleDownDetected)), + TimeValue.timeValueMillis(msLeftToScale).getStringRep() ) - .build()); + ).build() + ); } return new AutoscalingDeciderResult( AutoscalingCapacity.ZERO, - reasonBuilder - .setRequiredCapacity(AutoscalingCapacity.ZERO) + reasonBuilder.setRequiredCapacity(AutoscalingCapacity.ZERO) .setSimpleReason("Requesting scale down as tier and/or node size could be smaller") .build() ); } if (mlMemoryTracker.isRecentlyRefreshed(memoryTrackingStale) == false) { - logger.debug(() -> new ParameterizedMessage( - "view of job memory is stale given duration [{}]. Not attempting to make scaling decision", - memoryTrackingStale)); + logger.debug( + () -> new ParameterizedMessage( + "view of job memory is stale given duration [{}]. Not attempting to make scaling decision", + memoryTrackingStale + ) + ); return buildDecisionAndRequestRefresh(reasonBuilder); } // We need the current node loads to determine if we need to scale up or down List nodeLoads = new ArrayList<>(nodes.size()); boolean nodeIsMemoryAccurate = true; for (DiscoveryNode node : nodes) { - NodeLoad nodeLoad = nodeLoadDetector.detectNodeLoad(clusterState, - node, - maxOpenJobs, - maxMachineMemoryPercent, - useAuto); + NodeLoad nodeLoad = nodeLoadDetector.detectNodeLoad(clusterState, node, maxOpenJobs, maxMachineMemoryPercent, useAuto); if (nodeLoad.getError() != null) { - logger.warn("[{}] failed to gather node load limits, failure [{}]. Returning no scale", - node.getId(), - nodeLoad.getError()); - return noScaleResultOrRefresh(reasonBuilder, true, new AutoscalingDeciderResult(context.currentCapacity(), - reasonBuilder - .setSimpleReason( + logger.warn("[{}] failed to gather node load limits, failure [{}]. Returning no scale", node.getId(), nodeLoad.getError()); + return noScaleResultOrRefresh( + reasonBuilder, + true, + new AutoscalingDeciderResult( + context.currentCapacity(), + reasonBuilder.setSimpleReason( "Passing currently perceived capacity as there was a failure gathering node limits [" + nodeLoad.getError() + "]" - ) - .build())); + ).build() + ) + ); } nodeLoads.add(nodeLoad); nodeIsMemoryAccurate = nodeIsMemoryAccurate && nodeLoad.isUseMemory(); @@ -448,12 +453,16 @@ public AutoscalingDeciderResult scale(Settings configuration, AutoscalingDecider // This is an exceptional case, the memory tracking became stale between us checking previously and calculating the loads // We should return a no scale in this case if (nodeIsMemoryAccurate == false) { - return noScaleResultOrRefresh(reasonBuilder, true, new AutoscalingDeciderResult(context.currentCapacity(), - reasonBuilder - .setSimpleReason( + return noScaleResultOrRefresh( + reasonBuilder, + true, + new AutoscalingDeciderResult( + context.currentCapacity(), + reasonBuilder.setSimpleReason( "Passing currently perceived capacity as nodes were unable to provide an accurate view of their memory usage" - ) - .build())); + ).build() + ) + ); } Optional futureFreedCapacity = calculateFutureAvailableCapacity( @@ -482,22 +491,23 @@ public AutoscalingDeciderResult scale(Settings configuration, AutoscalingDecider if (waitingAnalyticsJobs.isEmpty() == false || waitingAnomalyJobs.isEmpty() == false) { // We don't want to continue to consider a scale down if there are now waiting jobs resetScaleDownCoolDown(); - return noScaleResultOrRefresh(reasonBuilder, + return noScaleResultOrRefresh( + reasonBuilder, mlMemoryTracker.isRecentlyRefreshed(memoryTrackingStale) == false, new AutoscalingDeciderResult( context.currentCapacity(), - reasonBuilder - .setSimpleReason( - String.format( - Locale.ROOT, - "Passing currently perceived capacity as there are [%d] analytics and [%d] anomaly jobs in the queue, " - + "but the number in the queue is less than the configured maximum allowed " - + " or the queued jobs will eventually be assignable at the current size. ", - waitingAnalyticsJobs.size(), - waitingAnomalyJobs.size() + reasonBuilder.setSimpleReason( + String.format( + Locale.ROOT, + "Passing currently perceived capacity as there are [%d] analytics and [%d] anomaly jobs in the queue, " + + "but the number in the queue is less than the configured maximum allowed " + + " or the queued jobs will eventually be assignable at the current size. ", + waitingAnalyticsJobs.size(), + waitingAnomalyJobs.size() ) - ) - .build())); + ).build() + ) + ); } long largestJobOrModel = Math.max( @@ -520,7 +530,8 @@ public AutoscalingDeciderResult scale(Settings configuration, AutoscalingDecider return mem; }) .max() - .orElse(0L)); + .orElse(0L) + ); largestJobOrModel = Math.max( largestJobOrModel, modelAllocations.values().stream().mapToLong(t -> t.getTaskParams().estimateMemoryUsageBytes()).max().orElse(0L) @@ -536,12 +547,17 @@ public AutoscalingDeciderResult scale(Settings configuration, AutoscalingDecider dataframeAnalyticsTasks.size(), modelAllocations.size() ); - return noScaleResultOrRefresh(reasonBuilder, true, new AutoscalingDeciderResult( - context.currentCapacity(), - reasonBuilder - .setSimpleReason("Passing currently perceived capacity as there are running analytics and anomaly jobs, " + - "but their memory usage estimates are inaccurate.") - .build())); + return noScaleResultOrRefresh( + reasonBuilder, + true, + new AutoscalingDeciderResult( + context.currentCapacity(), + reasonBuilder.setSimpleReason( + "Passing currently perceived capacity as there are running analytics and anomaly jobs, " + + "but their memory usage estimates are inaccurate." + ).build() + ) + ); } final Optional maybeScaleDown = checkForScaleDown( @@ -572,16 +588,22 @@ public AutoscalingDeciderResult scale(Settings configuration, AutoscalingDecider // one volatile read long maxOpenJobs = this.maxOpenJobs; if (totalAssignedJobs > maxOpenJobs) { - String msg = String.format(Locale.ROOT, + String msg = String.format( + Locale.ROOT, "not scaling down as the total number of jobs [%d] exceeds the setting [%s (%d)]. " + " To allow a scale down [%s] must be increased.", totalAssignedJobs, MAX_OPEN_JOBS_PER_NODE.getKey(), maxOpenJobs, - MAX_OPEN_JOBS_PER_NODE.getKey()); - logger.info(() -> new ParameterizedMessage("{} Calculated potential scaled down capacity [{}] ", - msg, - scaleDownDecisionResult.requiredCapacity())); + MAX_OPEN_JOBS_PER_NODE.getKey() + ); + logger.info( + () -> new ParameterizedMessage( + "{} Calculated potential scaled down capacity [{}] ", + msg, + scaleDownDecisionResult.requiredCapacity() + ) + ); return new AutoscalingDeciderResult(context.currentCapacity(), reasonBuilder.setSimpleReason(msg).build()); } } @@ -591,34 +613,39 @@ public AutoscalingDeciderResult scale(Settings configuration, AutoscalingDecider return scaleDownDecisionResult; } TimeValue downScaleDelay = DOWN_SCALE_DELAY.get(configuration); - logger.debug(() -> new ParameterizedMessage( - "not scaling down as the current scale down delay [{}] is not satisfied." + - " The last time scale down was detected [{}]. Calculated scaled down capacity [{}] ", - downScaleDelay.getStringRep(), - XContentElasticsearchExtension.DEFAULT_FORMATTER.format(Instant.ofEpochMilli(scaleDownDetected)), - scaleDownDecisionResult.requiredCapacity())); + logger.debug( + () -> new ParameterizedMessage( + "not scaling down as the current scale down delay [{}] is not satisfied." + + " The last time scale down was detected [{}]. Calculated scaled down capacity [{}] ", + downScaleDelay.getStringRep(), + XContentElasticsearchExtension.DEFAULT_FORMATTER.format(Instant.ofEpochMilli(scaleDownDetected)), + scaleDownDecisionResult.requiredCapacity() + ) + ); return new AutoscalingDeciderResult( context.currentCapacity(), - reasonBuilder - .setSimpleReason( - String.format( - Locale.ROOT, - "Passing currently perceived capacity as down scale delay has not been satisfied; configured delay [%s]" - + "last detected scale down event [%s]. Will request scale down in approximately [%s]", - downScaleDelay.getStringRep(), - XContentElasticsearchExtension.DEFAULT_FORMATTER.format(Instant.ofEpochMilli(scaleDownDetected)), - TimeValue.timeValueMillis(msLeftToScale).getStringRep() - ) + reasonBuilder.setSimpleReason( + String.format( + Locale.ROOT, + "Passing currently perceived capacity as down scale delay has not been satisfied; configured delay [%s]" + + "last detected scale down event [%s]. Will request scale down in approximately [%s]", + downScaleDelay.getStringRep(), + XContentElasticsearchExtension.DEFAULT_FORMATTER.format(Instant.ofEpochMilli(scaleDownDetected)), + TimeValue.timeValueMillis(msLeftToScale).getStringRep() ) - .build()); + ).build() + ); } - return noScaleResultOrRefresh(reasonBuilder, + return noScaleResultOrRefresh( + reasonBuilder, mlMemoryTracker.isRecentlyRefreshed(memoryTrackingStale) == false, - new AutoscalingDeciderResult(context.currentCapacity(), - reasonBuilder - .setSimpleReason("Passing currently perceived capacity as no scaling changes were detected to be possible") - .build())); + new AutoscalingDeciderResult( + context.currentCapacity(), + reasonBuilder.setSimpleReason("Passing currently perceived capacity as no scaling changes were detected to be possible") + .build() + ) + ); } static AutoscalingCapacity ensureScaleDown(AutoscalingCapacity scaleDownResult, AutoscalingCapacity currentCapacity) { @@ -646,9 +673,11 @@ static AutoscalingCapacity ensureScaleDown(AutoscalingCapacity scaleDownResult, return newCapacity; } - AutoscalingDeciderResult noScaleResultOrRefresh(MlScalingReason.Builder reasonBuilder, - boolean memoryTrackingStale, - AutoscalingDeciderResult potentialResult) { + AutoscalingDeciderResult noScaleResultOrRefresh( + MlScalingReason.Builder reasonBuilder, + boolean memoryTrackingStale, + AutoscalingDeciderResult potentialResult + ) { if (memoryTrackingStale) { logger.debug("current view of job memory is stale given. Returning a no scale event"); return buildDecisionAndRequestRefresh(reasonBuilder); @@ -659,39 +688,50 @@ AutoscalingDeciderResult noScaleResultOrRefresh(MlScalingReason.Builder reasonBu // This doesn't allow any jobs to wait in the queue, this is because in a "normal" scaling event, we also verify if a job // can eventually start, and given the current cluster, no job can eventually start. - AutoscalingDeciderResult scaleUpFromZero(List waitingAnomalyJobs, - List waitingAnalyticsJobs, - List waitingAllocatedModels, - MlScalingReason.Builder reasonBuilder) { - final Optional analyticsCapacity = requiredCapacityForUnassignedJobs(waitingAnalyticsJobs, + AutoscalingDeciderResult scaleUpFromZero( + List waitingAnomalyJobs, + List waitingAnalyticsJobs, + List waitingAllocatedModels, + MlScalingReason.Builder reasonBuilder + ) { + final Optional analyticsCapacity = requiredCapacityForUnassignedJobs( + waitingAnalyticsJobs, this::getAnalyticsMemoryRequirement, - 0); - final Optional anomalyCapacity = requiredCapacityForUnassignedJobs(waitingAnomalyJobs, + 0 + ); + final Optional anomalyCapacity = requiredCapacityForUnassignedJobs( + waitingAnomalyJobs, this::getAnomalyMemoryRequirement, - 0); - final Optional allocatedModelCapacity = requiredCapacityForUnassignedJobs(waitingAllocatedModels, + 0 + ); + final Optional allocatedModelCapacity = requiredCapacityForUnassignedJobs( + waitingAllocatedModels, this::getAllocatedModelRequirement, - 0); - NativeMemoryCapacity updatedCapacity = NativeMemoryCapacity.ZERO - .merge(anomalyCapacity.orElse(NativeMemoryCapacity.ZERO)) + 0 + ); + NativeMemoryCapacity updatedCapacity = NativeMemoryCapacity.ZERO.merge(anomalyCapacity.orElse(NativeMemoryCapacity.ZERO)) .merge(analyticsCapacity.orElse(NativeMemoryCapacity.ZERO)) .merge(allocatedModelCapacity.orElse(NativeMemoryCapacity.ZERO)); // If we still have calculated zero, this means the ml memory tracker does not have the required info. // So, request a scale for the default. This is only for the 0 -> N scaling case. if (updatedCapacity.getNode() == 0L) { - updatedCapacity.merge(new NativeMemoryCapacity( - ByteSizeValue.ofMb(AnalysisLimits.DEFAULT_MODEL_MEMORY_LIMIT_MB).getBytes(), - ByteSizeValue.ofMb(AnalysisLimits.DEFAULT_MODEL_MEMORY_LIMIT_MB).getBytes() - )); - } - updatedCapacity.merge(new NativeMemoryCapacity( - MachineLearning.NATIVE_EXECUTABLE_CODE_OVERHEAD.getBytes(), MachineLearning.NATIVE_EXECUTABLE_CODE_OVERHEAD.getBytes() - )); + updatedCapacity.merge( + new NativeMemoryCapacity( + ByteSizeValue.ofMb(AnalysisLimits.DEFAULT_MODEL_MEMORY_LIMIT_MB).getBytes(), + ByteSizeValue.ofMb(AnalysisLimits.DEFAULT_MODEL_MEMORY_LIMIT_MB).getBytes() + ) + ); + } + updatedCapacity.merge( + new NativeMemoryCapacity( + MachineLearning.NATIVE_EXECUTABLE_CODE_OVERHEAD.getBytes(), + MachineLearning.NATIVE_EXECUTABLE_CODE_OVERHEAD.getBytes() + ) + ); AutoscalingCapacity requiredCapacity = updatedCapacity.autoscalingCapacity(maxMachineMemoryPercent, useAuto); return new AutoscalingDeciderResult( requiredCapacity, - reasonBuilder - .setRequiredCapacity(requiredCapacity) + reasonBuilder.setRequiredCapacity(requiredCapacity) .setSimpleReason( "requesting scale up as number of jobs in queues exceeded configured limit and there are no machine learning nodes" ) @@ -699,15 +739,17 @@ AutoscalingDeciderResult scaleUpFromZero(List waitingAnomalyJobs, ); } - Optional checkForScaleUp(int numAnomalyJobsInQueue, - int numAnalyticsJobsInQueue, - List nodeLoads, - List waitingAnomalyJobs, - List waitingAnalyticsJobs, - List waitingAllocatedModels, - @Nullable NativeMemoryCapacity futureFreedCapacity, - NativeMemoryCapacity currentScale, - MlScalingReason.Builder reasonBuilder) { + Optional checkForScaleUp( + int numAnomalyJobsInQueue, + int numAnalyticsJobsInQueue, + List nodeLoads, + List waitingAnomalyJobs, + List waitingAnalyticsJobs, + List waitingAllocatedModels, + @Nullable NativeMemoryCapacity futureFreedCapacity, + NativeMemoryCapacity currentScale, + MlScalingReason.Builder reasonBuilder + ) { // Are we in breach of maximum waiting jobs? if (waitingAnalyticsJobs.size() > numAnalyticsJobsInQueue @@ -718,19 +760,22 @@ Optional checkForScaleUp(int numAnomalyJobsInQueue, waitingAnomalyJobs, this::getAnomalyMemoryRequirement, numAnomalyJobsInQueue, - nodeLoads).orElse(Tuple.tuple(NativeMemoryCapacity.ZERO, nodeLoads)); + nodeLoads + ).orElse(Tuple.tuple(NativeMemoryCapacity.ZERO, nodeLoads)); Tuple> analyticsCapacityAndNewLoad = determineUnassignableJobs( waitingAnalyticsJobs, this::getAnalyticsMemoryRequirement, numAnalyticsJobsInQueue, - anomalyCapacityAndNewLoad.v2()).orElse(Tuple.tuple(NativeMemoryCapacity.ZERO, anomalyCapacityAndNewLoad.v2())); + anomalyCapacityAndNewLoad.v2() + ).orElse(Tuple.tuple(NativeMemoryCapacity.ZERO, anomalyCapacityAndNewLoad.v2())); Tuple> modelCapacityAndNewLoad = determineUnassignableJobs( waitingAllocatedModels, this::getAllocatedModelRequirement, 0, - analyticsCapacityAndNewLoad.v2()).orElse(Tuple.tuple(NativeMemoryCapacity.ZERO, analyticsCapacityAndNewLoad.v2())); + analyticsCapacityAndNewLoad.v2() + ).orElse(Tuple.tuple(NativeMemoryCapacity.ZERO, analyticsCapacityAndNewLoad.v2())); if (analyticsCapacityAndNewLoad.v1().equals(NativeMemoryCapacity.ZERO) && anomalyCapacityAndNewLoad.v1().equals(NativeMemoryCapacity.ZERO) @@ -747,17 +792,18 @@ Optional checkForScaleUp(int numAnomalyJobsInQueue, // We should account for overhead in the tier capacity just in case. .merge(new NativeMemoryCapacity(MachineLearning.NATIVE_EXECUTABLE_CODE_OVERHEAD.getBytes(), 0)); AutoscalingCapacity requiredCapacity = updatedCapacity.autoscalingCapacity(maxMachineMemoryPercent, useAuto); - return Optional.of(new AutoscalingDeciderResult( - requiredCapacity, - reasonBuilder - .setRequiredCapacity(requiredCapacity) - .setSimpleReason( - "requesting scale up as number of jobs in queues exceeded configured limit " - + "or there is at least one trained model waiting for allocation " - + "and current capacity is not large enough for waiting jobs or models" - ) - .build() - )); + return Optional.of( + new AutoscalingDeciderResult( + requiredCapacity, + reasonBuilder.setRequiredCapacity(requiredCapacity) + .setSimpleReason( + "requesting scale up as number of jobs in queues exceeded configured limit " + + "or there is at least one trained model waiting for allocation " + + "and current capacity is not large enough for waiting jobs or models" + ) + .build() + ) + ); } // Could the currently waiting jobs ever be assigned? @@ -767,20 +813,21 @@ Optional checkForScaleUp(int numAnomalyJobsInQueue, if (futureFreedCapacity == null) { Optional maxSize = Stream.concat( waitingAnalyticsJobs.stream().map(mlMemoryTracker::getDataFrameAnalyticsJobMemoryRequirement), - waitingAnomalyJobs.stream().map(mlMemoryTracker::getAnomalyDetectorJobMemoryRequirement)) - .filter(Objects::nonNull) - .max(Long::compareTo); + waitingAnomalyJobs.stream().map(mlMemoryTracker::getAnomalyDetectorJobMemoryRequirement) + ).filter(Objects::nonNull).max(Long::compareTo); if (maxSize.isPresent() && maxSize.get() > currentScale.getNode()) { AutoscalingCapacity requiredCapacity = new NativeMemoryCapacity( Math.max(currentScale.getTier(), maxSize.get()), maxSize.get() ).autoscalingCapacity(maxMachineMemoryPercent, useAuto); - return Optional.of(new AutoscalingDeciderResult( - requiredCapacity, - reasonBuilder - .setSimpleReason("requesting scale up as there is no node large enough to handle queued jobs") - .setRequiredCapacity(requiredCapacity) - .build())); + return Optional.of( + new AutoscalingDeciderResult( + requiredCapacity, + reasonBuilder.setSimpleReason("requesting scale up as there is no node large enough to handle queued jobs") + .setRequiredCapacity(requiredCapacity) + .build() + ) + ); } // we have no info, allow the caller to make the appropriate action, probably returning a no_scale return Optional.empty(); @@ -814,18 +861,18 @@ Optional checkForScaleUp(int numAnomalyJobsInQueue, } if (newNodeMax > currentScale.getNode() || newTierNeeded > 0L) { NativeMemoryCapacity newCapacity = new NativeMemoryCapacity(newTierNeeded, newNodeMax); - AutoscalingCapacity requiredCapacity = NativeMemoryCapacity - .from(currentScale) + AutoscalingCapacity requiredCapacity = NativeMemoryCapacity.from(currentScale) .merge(newCapacity) .autoscalingCapacity(maxMachineMemoryPercent, useAuto); - return Optional.of(new AutoscalingDeciderResult( - // We need more memory in the tier, or our individual node size requirements has increased - requiredCapacity, - reasonBuilder - .setSimpleReason("scaling up as adequate space would not automatically become available when running jobs finish") - .setRequiredCapacity(requiredCapacity) - .build() - )); + return Optional.of( + new AutoscalingDeciderResult( + // We need more memory in the tier, or our individual node size requirements has increased + requiredCapacity, + reasonBuilder.setSimpleReason( + "scaling up as adequate space would not automatically become available when running jobs finish" + ).setRequiredCapacity(requiredCapacity).build() + ) + ); } } @@ -837,13 +884,15 @@ Optional checkForScaleUp(int numAnomalyJobsInQueue, // we can assume (without user intervention) that these will eventually stop and free their currently occupied resources. // // The capacity is as follows: - // tier: The sum total of the resources that will be eventually be available - // node: The largest block of memory that will be free on a given node. - // - If > 1 "batch" ml tasks are running on the same node, we sum their resources. - Optional calculateFutureAvailableCapacity(PersistentTasksCustomMetadata tasks, - Duration jobMemoryExpiry, - List mlNodes, - ClusterState clusterState) { + // tier: The sum total of the resources that will be eventually be available + // node: The largest block of memory that will be free on a given node. + // - If > 1 "batch" ml tasks are running on the same node, we sum their resources. + Optional calculateFutureAvailableCapacity( + PersistentTasksCustomMetadata tasks, + Duration jobMemoryExpiry, + List mlNodes, + ClusterState clusterState + ) { if (mlMemoryTracker.isRecentlyRefreshed(jobMemoryExpiry) == false) { return Optional.empty(); } @@ -857,11 +906,7 @@ Optional calculateFutureAvailableCapacity(PersistentTasksC // what is the future freed capacity, knowing the current capacity and what could be freed up in the future Map freeMemoryByNodeId = new HashMap<>(); for (DiscoveryNode node : mlNodes) { - NodeLoad nodeLoad = nodeLoadDetector.detectNodeLoad(clusterState, - node, - maxOpenJobs, - maxMachineMemoryPercent, - useAuto); + NodeLoad nodeLoad = nodeLoadDetector.detectNodeLoad(clusterState, node, maxOpenJobs, maxMachineMemoryPercent, useAuto); if (nodeLoad.getError() != null || nodeLoad.isUseMemory() == false) { return Optional.empty(); } @@ -881,9 +926,12 @@ Optional calculateFutureAvailableCapacity(PersistentTasksC } freeMemoryByNodeId.compute(task.getExecutorNode(), (_k, v) -> v == null ? jobSize : jobSize + v); } - return Optional.of(new NativeMemoryCapacity( - freeMemoryByNodeId.values().stream().mapToLong(Long::longValue).sum(), - freeMemoryByNodeId.values().stream().mapToLong(Long::longValue).max().orElse(0L))); + return Optional.of( + new NativeMemoryCapacity( + freeMemoryByNodeId.values().stream().mapToLong(Long::longValue).sum(), + freeMemoryByNodeId.values().stream().mapToLong(Long::longValue).max().orElse(0L) + ) + ); } private AutoscalingDeciderResult buildDecisionAndRequestRefresh(MlScalingReason.Builder reasonBuilder) { @@ -911,10 +959,12 @@ private Long getAnomalyMemoryRequirement(PersistentTask task) { return getAnomalyMemoryRequirement(MlTasks.jobId(task.getId())); } - Optional checkForScaleDown(List nodeLoads, - long largestJob, - NativeMemoryCapacity currentCapacity, - MlScalingReason.Builder reasonBuilder) { + Optional checkForScaleDown( + List nodeLoads, + long largestJob, + NativeMemoryCapacity currentCapacity, + MlScalingReason.Builder reasonBuilder + ) { long currentlyNecessaryTier = nodeLoads.stream().mapToLong(NodeLoad::getAssignedJobMemory).sum(); // The required NATIVE node memory is the largest job and our static overhead. long currentlyNecessaryNode = largestJob == 0 ? 0 : largestJob + MachineLearning.NATIVE_EXECUTABLE_CODE_OVERHEAD.getBytes(); @@ -928,13 +978,13 @@ Optional checkForScaleDown(List nodeLoads, Math.min(currentlyNecessaryTier, currentCapacity.getTier()), Math.min(currentlyNecessaryNode, currentCapacity.getNode()), // If our newly suggested native capacity is the same, we can use the previously stored jvm size - currentlyNecessaryNode == currentCapacity.getNode() ? currentCapacity.getJvmSize() : null); + currentlyNecessaryNode == currentCapacity.getNode() ? currentCapacity.getJvmSize() : null + ); AutoscalingCapacity requiredCapacity = nativeMemoryCapacity.autoscalingCapacity(maxMachineMemoryPercent, useAuto); return Optional.of( new AutoscalingDeciderResult( requiredCapacity, - reasonBuilder - .setRequiredCapacity(requiredCapacity) + reasonBuilder.setRequiredCapacity(requiredCapacity) .setSimpleReason("Requesting scale down as tier and/or node size could be smaller") .build() ) diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/autoscaling/MlAutoscalingNamedWritableProvider.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/autoscaling/MlAutoscalingNamedWritableProvider.java index 0ad06034786c5..6527ca4d7f3f8 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/autoscaling/MlAutoscalingNamedWritableProvider.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/autoscaling/MlAutoscalingNamedWritableProvider.java @@ -15,13 +15,11 @@ public final class MlAutoscalingNamedWritableProvider { - private MlAutoscalingNamedWritableProvider() { } + private MlAutoscalingNamedWritableProvider() {} public static List getNamedWriteables() { return Arrays.asList( - new NamedWriteableRegistry.Entry(AutoscalingDeciderResult.Reason.class, - MlScalingReason.NAME, - MlScalingReason::new) + new NamedWriteableRegistry.Entry(AutoscalingDeciderResult.Reason.class, MlScalingReason.NAME, MlScalingReason::new) ); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/autoscaling/MlScalingReason.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/autoscaling/MlScalingReason.java index e5861de515957..1654624a8df28 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/autoscaling/MlScalingReason.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/autoscaling/MlScalingReason.java @@ -52,7 +52,8 @@ public MlScalingReason(StreamInput in) throws IOException { } else { this.waitingModels = List.of(); } - this.passedConfiguration = Settings.readSettingsFromStream(in);; + this.passedConfiguration = Settings.readSettingsFromStream(in); + ; this.currentMlCapacity = new AutoscalingCapacity(in); this.requiredCapacity = in.readOptionalWriteable(AutoscalingCapacity::new); this.largestWaitingAnalyticsJob = in.readOptionalVLong(); @@ -60,15 +61,17 @@ public MlScalingReason(StreamInput in) throws IOException { this.simpleReason = in.readString(); } - MlScalingReason(List waitingAnalyticsJobs, - List waitingAnomalyJobs, - List waitingModels, - Settings passedConfiguration, - Long largestWaitingAnalyticsJob, - Long largestWaitingAnomalyJob, - AutoscalingCapacity currentMlCapacity, - AutoscalingCapacity requiredCapacity, - String simpleReason) { + MlScalingReason( + List waitingAnalyticsJobs, + List waitingAnomalyJobs, + List waitingModels, + Settings passedConfiguration, + Long largestWaitingAnalyticsJob, + Long largestWaitingAnomalyJob, + AutoscalingCapacity currentMlCapacity, + AutoscalingCapacity requiredCapacity, + String simpleReason + ) { this.waitingAnalyticsJobs = waitingAnalyticsJobs == null ? Collections.emptyList() : waitingAnalyticsJobs; this.waitingAnomalyJobs = waitingAnomalyJobs == null ? Collections.emptyList() : waitingAnomalyJobs; this.waitingModels = waitingModels == null ? List.of() : waitingModels; @@ -89,20 +92,21 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; MlScalingReason that = (MlScalingReason) o; - return Objects.equals(waitingAnalyticsJobs, that.waitingAnalyticsJobs) && - Objects.equals(waitingAnomalyJobs, that.waitingAnomalyJobs) && - Objects.equals(waitingModels, that.waitingModels) && - Objects.equals(passedConfiguration, that.passedConfiguration) && - Objects.equals(largestWaitingAnalyticsJob, that.largestWaitingAnalyticsJob) && - Objects.equals(largestWaitingAnomalyJob, that.largestWaitingAnomalyJob) && - Objects.equals(currentMlCapacity, that.currentMlCapacity) && - Objects.equals(requiredCapacity, that.requiredCapacity) && - Objects.equals(simpleReason, that.simpleReason); + return Objects.equals(waitingAnalyticsJobs, that.waitingAnalyticsJobs) + && Objects.equals(waitingAnomalyJobs, that.waitingAnomalyJobs) + && Objects.equals(waitingModels, that.waitingModels) + && Objects.equals(passedConfiguration, that.passedConfiguration) + && Objects.equals(largestWaitingAnalyticsJob, that.largestWaitingAnalyticsJob) + && Objects.equals(largestWaitingAnomalyJob, that.largestWaitingAnomalyJob) + && Objects.equals(currentMlCapacity, that.currentMlCapacity) + && Objects.equals(requiredCapacity, that.requiredCapacity) + && Objects.equals(simpleReason, that.simpleReason); } @Override public int hashCode() { - return Objects.hash(waitingAnalyticsJobs, + return Objects.hash( + waitingAnalyticsJobs, waitingAnomalyJobs, passedConfiguration, largestWaitingAnalyticsJob, @@ -110,7 +114,8 @@ public int hashCode() { largestWaitingAnomalyJob, currentMlCapacity, requiredCapacity, - simpleReason); + simpleReason + ); } @Override diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/autoscaling/NativeMemoryCapacity.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/autoscaling/NativeMemoryCapacity.java index f79e6e709b17a..03d395820e904 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/autoscaling/NativeMemoryCapacity.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/autoscaling/NativeMemoryCapacity.java @@ -18,7 +18,7 @@ // Used for storing native memory capacity and then transforming it into an autoscaling capacity // which takes into account the whole node size -public class NativeMemoryCapacity { +public class NativeMemoryCapacity { static final NativeMemoryCapacity ZERO = new NativeMemoryCapacity(0L, 0L); @@ -54,9 +54,9 @@ NativeMemoryCapacity merge(NativeMemoryCapacity nativeMemoryCapacity) { public AutoscalingCapacity autoscalingCapacity(int maxMemoryPercent, boolean useAuto) { // We calculate the JVM size here first to ensure it stays the same given the rest of the calculations - final Long jvmSize = useAuto ? - Optional.ofNullable(this.jvmSize).orElse(dynamicallyCalculateJvmSizeFromNativeMemorySize(node)) : - null; + final Long jvmSize = useAuto + ? Optional.ofNullable(this.jvmSize).orElse(dynamicallyCalculateJvmSizeFromNativeMemorySize(node)) + : null; // We first need to calculate the actual node size given the current native memory size. // This way we can accurately determine the required node size AND what the overall memory percentage will be long actualNodeSize = NativeMemoryCalculator.calculateApproxNecessaryNodeSize(node, jvmSize, maxMemoryPercent, useAuto); @@ -64,12 +64,7 @@ public AutoscalingCapacity autoscalingCapacity(int maxMemoryPercent, boolean use // This simplifies calculating the tier as it means that each node in the tier // will have the same dynamic memory calculation. And thus the tier is simply the sum of the memory necessary // times that scaling factor. - double memoryPercentForMl = NativeMemoryCalculator.modelMemoryPercent( - actualNodeSize, - jvmSize, - maxMemoryPercent, - useAuto - ); + double memoryPercentForMl = NativeMemoryCalculator.modelMemoryPercent(actualNodeSize, jvmSize, maxMemoryPercent, useAuto); double inverseScale = memoryPercentForMl <= 0 ? 0 : 100.0 / memoryPercentForMl; long actualTier = Math.round(tier * inverseScale); return new AutoscalingCapacity( @@ -94,10 +89,12 @@ public Long getJvmSize() { @Override public String toString() { - return "NativeMemoryCapacity{" + - "total bytes=" + ByteSizeValue.ofBytes(tier) + - ", largest node bytes=" + ByteSizeValue.ofBytes(node) + - '}'; + return "NativeMemoryCapacity{" + + "total bytes=" + + ByteSizeValue.ofBytes(tier) + + ", largest node bytes=" + + ByteSizeValue.ofBytes(node) + + '}'; } @Override diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/DatafeedConfigAutoUpdater.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/DatafeedConfigAutoUpdater.java index b338f19cb569d..f0f4d7ca35b33 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/DatafeedConfigAutoUpdater.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/DatafeedConfigAutoUpdater.java @@ -46,9 +46,11 @@ public boolean isMinNodeVersionSupported(Version minNodeVersion) { @Override public boolean isAbleToRun(ClusterState latestState) { - String[] indices = expressionResolver.concreteIndexNames(latestState, + String[] indices = expressionResolver.concreteIndexNames( + latestState, IndicesOptions.lenientExpandOpenHidden(), - MlConfigIndex.indexName()); + MlConfigIndex.indexName() + ); for (String index : indices) { if (latestState.metadata().hasIndex(index) == false) { continue; @@ -74,26 +76,33 @@ public void runUpdate() { List updates = datafeedConfigBuilders.stream() .map(DatafeedConfig.Builder::build) .filter(DatafeedConfig::aggsRewritten) - .map(datafeedConfig -> new DatafeedUpdate.Builder() - .setAggregations(datafeedConfig.getAggProvider()) - .setId(datafeedConfig.getId()) - .build()) + .map( + datafeedConfig -> new DatafeedUpdate.Builder().setAggregations(datafeedConfig.getAggProvider()) + .setId(datafeedConfig.getId()) + .build() + ) .collect(Collectors.toList()); if (updates.isEmpty()) { return; } - logger.debug(() -> new ParameterizedMessage("{} datafeeds are currently being updated", - updates.stream().map(DatafeedUpdate::getId).collect(Collectors.toList()))); + logger.debug( + () -> new ParameterizedMessage( + "{} datafeeds are currently being updated", + updates.stream().map(DatafeedUpdate::getId).collect(Collectors.toList()) + ) + ); List failures = new ArrayList<>(); for (DatafeedUpdate update : updates) { PlainActionFuture updateDatafeeds = PlainActionFuture.newFuture(); - provider.updateDatefeedConfig(update.getId(), + provider.updateDatefeedConfig( + update.getId(), update, Collections.emptyMap(), (updatedConfig, listener) -> listener.onResponse(Boolean.TRUE), - updateDatafeeds); + updateDatafeeds + ); try { updateDatafeeds.actionGet(); logger.debug(() -> new ParameterizedMessage("[{}] datafeed successfully updated", update.getId())); @@ -103,8 +112,12 @@ public void runUpdate() { } } if (failures.isEmpty()) { - logger.debug(() -> new ParameterizedMessage("{} datafeeds are finished being updated", - updates.stream().map(DatafeedUpdate::getId).collect(Collectors.toList()))); + logger.debug( + () -> new ParameterizedMessage( + "{} datafeeds are finished being updated", + updates.stream().map(DatafeedUpdate::getId).collect(Collectors.toList()) + ) + ); return; } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/DatafeedContextProvider.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/DatafeedContextProvider.java index 2b66c478924cd..e7afd943e9e16 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/DatafeedContextProvider.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/DatafeedContextProvider.java @@ -25,8 +25,11 @@ public class DatafeedContextProvider { private final DatafeedConfigProvider datafeedConfigProvider; private final JobResultsProvider resultsProvider; - public DatafeedContextProvider(JobConfigProvider jobConfigProvider, DatafeedConfigProvider datafeedConfigProvider, - JobResultsProvider jobResultsProvider) { + public DatafeedContextProvider( + JobConfigProvider jobConfigProvider, + DatafeedConfigProvider datafeedConfigProvider, + JobResultsProvider jobResultsProvider + ) { this.jobConfigProvider = Objects.requireNonNull(jobConfigProvider); this.datafeedConfigProvider = Objects.requireNonNull(datafeedConfigProvider); this.resultsProvider = Objects.requireNonNull(jobResultsProvider); @@ -40,30 +43,21 @@ public void buildDatafeedContext(String datafeedId, ActionListener restartTimeInfoListener = ActionListener.wrap( - restartTimeInfo -> { - context.setRestartTimeInfo(restartTimeInfo); - resultsProvider.datafeedTimingStats(context.getJob().getId(), timingStatsListener, listener::onFailure); - }, - listener::onFailure - ); + ActionListener restartTimeInfoListener = ActionListener.wrap(restartTimeInfo -> { + context.setRestartTimeInfo(restartTimeInfo); + resultsProvider.datafeedTimingStats(context.getJob().getId(), timingStatsListener, listener::onFailure); + }, listener::onFailure); - ActionListener jobConfigListener = ActionListener.wrap( - jobBuilder -> { - context.setJob(jobBuilder.build()); - resultsProvider.getRestartTimeInfo(jobBuilder.getId(), restartTimeInfoListener); - }, - listener::onFailure - ); + ActionListener jobConfigListener = ActionListener.wrap(jobBuilder -> { + context.setJob(jobBuilder.build()); + resultsProvider.getRestartTimeInfo(jobBuilder.getId(), restartTimeInfoListener); + }, listener::onFailure); - ActionListener datafeedListener = ActionListener.wrap( - datafeedConfigBuilder -> { - DatafeedConfig datafeedConfig = datafeedConfigBuilder.build(); - context.setDatafeedConfig(datafeedConfig); - jobConfigProvider.getJob(datafeedConfig.getJobId(), jobConfigListener); - }, - listener::onFailure - ); + ActionListener datafeedListener = ActionListener.wrap(datafeedConfigBuilder -> { + DatafeedConfig datafeedConfig = datafeedConfigBuilder.build(); + context.setDatafeedConfig(datafeedConfig); + jobConfigProvider.getJob(datafeedConfig.getJobId(), jobConfigListener); + }, listener::onFailure); datafeedConfigProvider.getDatafeedConfig(datafeedId, datafeedListener); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/DatafeedJob.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/DatafeedJob.java index da978ff60f9a9..cfbd1b4a380d2 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/DatafeedJob.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/DatafeedJob.java @@ -13,14 +13,14 @@ import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.ElasticsearchWrapperException; import org.elasticsearch.client.Client; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.io.Streams; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.common.xcontent.XContentElasticsearchExtension; -import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.core.TimeValue; +import org.elasticsearch.core.Tuple; import org.elasticsearch.index.mapper.DateFieldMapper; import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.ml.action.FlushJobAction; import org.elasticsearch.xpack.core.ml.action.PersistJobAction; import org.elasticsearch.xpack.core.ml.action.PostDataAction; @@ -78,11 +78,24 @@ class DatafeedJob { private volatile boolean haveEverSeenData; private volatile long consecutiveDelayedDataBuckets; - DatafeedJob(String jobId, DataDescription dataDescription, long frequencyMs, long queryDelayMs, - DataExtractorFactory dataExtractorFactory, DatafeedTimingStatsReporter timingStatsReporter, Client client, - AnomalyDetectionAuditor auditor, AnnotationPersister annotationPersister, Supplier currentTimeSupplier, - DelayedDataDetector delayedDataDetector, Integer maxEmptySearches, long latestFinalBucketEndTimeMs, long latestRecordTimeMs, - boolean haveSeenDataPreviously, long delayedDataCheckFreq) { + DatafeedJob( + String jobId, + DataDescription dataDescription, + long frequencyMs, + long queryDelayMs, + DataExtractorFactory dataExtractorFactory, + DatafeedTimingStatsReporter timingStatsReporter, + Client client, + AnomalyDetectionAuditor auditor, + AnnotationPersister annotationPersister, + Supplier currentTimeSupplier, + DelayedDataDetector delayedDataDetector, + Integer maxEmptySearches, + long latestFinalBucketEndTimeMs, + long latestRecordTimeMs, + boolean haveSeenDataPreviously, + long delayedDataCheckFreq + ) { this.jobId = jobId; this.dataDescription = Objects.requireNonNull(dataDescription); this.frequencyMs = frequencyMs; @@ -139,10 +152,12 @@ Long runLookBack(long startTime, Long endTime) throws Exception { } } - String msg = Messages.getMessage(Messages.JOB_AUDIT_DATAFEED_STARTED_FROM_TO, - DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.formatMillis(lookbackStartTimeMs), - endTime == null ? "real-time" : DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.formatMillis(lookbackEnd), - TimeValue.timeValueMillis(frequencyMs).getStringRep()); + String msg = Messages.getMessage( + Messages.JOB_AUDIT_DATAFEED_STARTED_FROM_TO, + DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.formatMillis(lookbackStartTimeMs), + endTime == null ? "real-time" : DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.formatMillis(lookbackEnd), + TimeValue.timeValueMillis(frequencyMs).getStringRep() + ); auditor.info(jobId, msg); LOGGER.info("[{}] {}", jobId, msg); @@ -205,15 +220,16 @@ private void checkForMissingDataIfNecessary() { this.lastDataCheckTimeMs = this.currentTimeSupplier.get(); List missingDataBuckets = delayedDataDetector.detectMissingData(latestFinalBucketEndTimeMs); if (missingDataBuckets.isEmpty() == false) { - long totalRecordsMissing = missingDataBuckets.stream() - .mapToLong(BucketWithMissingData::getMissingDocumentCount) - .sum(); + long totalRecordsMissing = missingDataBuckets.stream().mapToLong(BucketWithMissingData::getMissingDocumentCount).sum(); Bucket lastBucket = missingDataBuckets.get(missingDataBuckets.size() - 1).getBucket(); // Get the end of the last bucket and make it milliseconds Date endTime = new Date((lastBucket.getEpoch() + lastBucket.getBucketSpan()) * 1000); - String msg = Messages.getMessage(Messages.JOB_AUDIT_DATAFEED_MISSING_DATA, totalRecordsMissing, - XContentElasticsearchExtension.DEFAULT_FORMATTER.format(lastBucket.getTimestamp().toInstant())); + String msg = Messages.getMessage( + Messages.JOB_AUDIT_DATAFEED_MISSING_DATA, + totalRecordsMissing, + XContentElasticsearchExtension.DEFAULT_FORMATTER.format(lastBucket.getTimestamp().toInstant()) + ); Annotation annotation = createDelayedDataAnnotation(missingDataBuckets.get(0).getBucket().getTimestamp(), endTime, msg); @@ -269,24 +285,22 @@ private boolean shouldWriteDelayedDataAudit() { } private Annotation createDelayedDataAnnotation(Date startTime, Date endTime, String msg) { - Date currentTime = new Date(currentTimeSupplier.get()); - return new Annotation.Builder() - .setAnnotation(msg) - .setCreateTime(currentTime) - .setCreateUsername(XPackUser.NAME) - .setTimestamp(startTime) - .setEndTimestamp(endTime) - .setJobId(jobId) - .setModifiedTime(currentTime) - .setModifiedUsername(XPackUser.NAME) - .setType(Annotation.Type.ANNOTATION) - .setEvent(Annotation.Event.DELAYED_DATA) - .build(); + Date currentTime = new Date(currentTimeSupplier.get()); + return new Annotation.Builder().setAnnotation(msg) + .setCreateTime(currentTime) + .setCreateUsername(XPackUser.NAME) + .setTimestamp(startTime) + .setEndTimestamp(endTime) + .setJobId(jobId) + .setModifiedTime(currentTime) + .setModifiedUsername(XPackUser.NAME) + .setType(Annotation.Type.ANNOTATION) + .setEvent(Annotation.Event.DELAYED_DATA) + .build(); } private Annotation updateAnnotation(Annotation annotation) { - return new Annotation.Builder(lastDataCheckAnnotationWithId.v2()) - .setAnnotation(annotation.getAnnotation()) + return new Annotation.Builder(lastDataCheckAnnotationWithId.v2()).setAnnotation(annotation.getAnnotation()) .setTimestamp(annotation.getTimestamp()) .setEndTimestamp(annotation.getEndTimestamp()) .setModifiedTime(new Date(currentTimeSupplier.get())) @@ -356,9 +370,13 @@ private void run(long start, long end, FlushJobAction.Request flushRequest) thro // Unfortunately, there are no great ways to identify the issue but search for 'doc values' // deep in the exception. if (e.toString().contains("doc values")) { - throw new ExtractionProblemException(nextRealtimeTimestamp(), new IllegalArgumentException( - "One or more fields do not have doc values; please enable doc values for all analysis fields for datafeeds" + - " using aggregations")); + throw new ExtractionProblemException( + nextRealtimeTimestamp(), + new IllegalArgumentException( + "One or more fields do not have doc values; please enable doc values for all analysis fields for datafeeds" + + " using aggregations" + ) + ); } throw new ExtractionProblemException(nextRealtimeTimestamp(), e); } @@ -369,10 +387,14 @@ private void run(long start, long end, FlushJobAction.Request flushRequest) thro DataCounts counts; try (InputStream in = extractedData.get()) { counts = postData(in, XContentType.JSON); - LOGGER.trace(() -> new ParameterizedMessage("[{}] Processed another {} records with latest timestamp [{}]", - jobId, - counts.getProcessedRecordCount(), - counts.getLatestRecordTimeStamp())); + LOGGER.trace( + () -> new ParameterizedMessage( + "[{}] Processed another {} records with latest timestamp [{}]", + jobId, + counts.getProcessedRecordCount(), + counts.getLatestRecordTimeStamp() + ) + ); timingStatsReporter.reportDataCounts(counts); } catch (Exception e) { if (e instanceof InterruptedException) { @@ -403,8 +425,15 @@ private void run(long start, long end, FlushJobAction.Request flushRequest) thro } lastEndTimeMs = Math.max(lastEndTimeMs == null ? 0 : lastEndTimeMs, dataExtractor.getEndTime() - 1); - LOGGER.debug("[{}] Complete iterating data extractor [{}], [{}], [{}], [{}], [{}]", jobId, error, recordCount, - lastEndTimeMs, isRunning(), dataExtractor.isCancelled()); + LOGGER.debug( + "[{}] Complete iterating data extractor [{}], [{}], [{}], [{}], [{}]", + jobId, + error, + recordCount, + lastEndTimeMs, + isRunning(), + dataExtractor.isCancelled() + ); // We can now throw any stored error as we have updated time. if (error != null) { @@ -426,8 +455,7 @@ private void run(long start, long end, FlushJobAction.Request flushRequest) thro } } - private DataCounts postData(InputStream inputStream, XContentType xContentType) - throws IOException { + private DataCounts postData(InputStream inputStream, XContentType xContentType) throws IOException { PostDataAction.Request request = new PostDataAction.Request(jobId); request.setDataDescription(dataDescription); request.setContent(Streams.readFully(inputStream), xContentType); @@ -438,8 +466,7 @@ private DataCounts postData(InputStream inputStream, XContentType xContentType) } private boolean isConflictException(Exception e) { - return e instanceof ElasticsearchStatusException - && ((ElasticsearchStatusException) e).status() == RestStatus.CONFLICT; + return e instanceof ElasticsearchStatusException && ((ElasticsearchStatusException) e).status() == RestStatus.CONFLICT; } private long nextRealtimeTimestamp() { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/DatafeedJobBuilder.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/DatafeedJobBuilder.java index a68f896db2245..d2f8d08de294e 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/DatafeedJobBuilder.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/DatafeedJobBuilder.java @@ -13,8 +13,8 @@ import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.license.RemoteClusterLicenseChecker; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfig; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedJobValidator; import org.elasticsearch.xpack.core.ml.job.config.DataDescription; @@ -48,9 +48,16 @@ public class DatafeedJobBuilder { private volatile long delayedDataCheckFreq; - public DatafeedJobBuilder(Client client, NamedXContentRegistry xContentRegistry, AnomalyDetectionAuditor auditor, - AnnotationPersister annotationPersister, Supplier currentTimeSupplier, - JobResultsPersister jobResultsPersister, Settings settings, ClusterService clusterService) { + public DatafeedJobBuilder( + Client client, + NamedXContentRegistry xContentRegistry, + AnomalyDetectionAuditor auditor, + AnnotationPersister annotationPersister, + Supplier currentTimeSupplier, + JobResultsPersister jobResultsPersister, + Settings settings, + ClusterService clusterService + ) { this.client = client; this.xContentRegistry = Objects.requireNonNull(xContentRegistry); this.auditor = Objects.requireNonNull(auditor); @@ -71,12 +78,16 @@ void build(TransportStartDatafeedAction.DatafeedTask task, DatafeedContext conte final ParentTaskAssigningClient parentTaskAssigningClient = new ParentTaskAssigningClient(client, task.getParentTaskId()); final DatafeedConfig datafeedConfig = context.getDatafeedConfig(); final Job job = context.getJob(); - final long latestFinalBucketEndMs = context.getRestartTimeInfo().getLatestFinalBucketTimeMs() == null ? - -1 : context.getRestartTimeInfo().getLatestFinalBucketTimeMs() + job.getAnalysisConfig().getBucketSpan().millis() - 1; - final long latestRecordTimeMs = context.getRestartTimeInfo().getLatestRecordTimeMs() == null ? - -1 : context.getRestartTimeInfo().getLatestRecordTimeMs(); - final DatafeedTimingStatsReporter timingStatsReporter = new DatafeedTimingStatsReporter(context.getTimingStats(), - jobResultsPersister::persistDatafeedTimingStats); + final long latestFinalBucketEndMs = context.getRestartTimeInfo().getLatestFinalBucketTimeMs() == null + ? -1 + : context.getRestartTimeInfo().getLatestFinalBucketTimeMs() + job.getAnalysisConfig().getBucketSpan().millis() - 1; + final long latestRecordTimeMs = context.getRestartTimeInfo().getLatestRecordTimeMs() == null + ? -1 + : context.getRestartTimeInfo().getLatestRecordTimeMs(); + final DatafeedTimingStatsReporter timingStatsReporter = new DatafeedTimingStatsReporter( + context.getTimingStats(), + jobResultsPersister::persistDatafeedTimingStats + ); // Validate remote indices are available and get the job try { @@ -95,37 +106,39 @@ void build(TransportStartDatafeedAction.DatafeedTask task, DatafeedContext conte return; } - ActionListener dataExtractorFactoryHandler = ActionListener.wrap( - dataExtractorFactory -> { - TimeValue frequency = getFrequencyOrDefault(datafeedConfig, job, xContentRegistry); - TimeValue queryDelay = datafeedConfig.getQueryDelay(); - DelayedDataDetector delayedDataDetector = DelayedDataDetectorFactory.buildDetector(job, - datafeedConfig, parentTaskAssigningClient, xContentRegistry); - DatafeedJob datafeedJob = new DatafeedJob( - job.getId(), - buildDataDescription(job), - frequency.millis(), - queryDelay.millis(), - dataExtractorFactory, - timingStatsReporter, - parentTaskAssigningClient, - auditor, - annotationPersister, - currentTimeSupplier, - delayedDataDetector, - datafeedConfig.getMaxEmptySearches(), - latestFinalBucketEndMs, - latestRecordTimeMs, - context.getRestartTimeInfo().haveSeenDataPreviously(), - delayedDataCheckFreq - ); - - listener.onResponse(datafeedJob); - }, e -> { - auditor.error(job.getId(), e.getMessage()); - listener.onFailure(e); - } - ); + ActionListener dataExtractorFactoryHandler = ActionListener.wrap(dataExtractorFactory -> { + TimeValue frequency = getFrequencyOrDefault(datafeedConfig, job, xContentRegistry); + TimeValue queryDelay = datafeedConfig.getQueryDelay(); + DelayedDataDetector delayedDataDetector = DelayedDataDetectorFactory.buildDetector( + job, + datafeedConfig, + parentTaskAssigningClient, + xContentRegistry + ); + DatafeedJob datafeedJob = new DatafeedJob( + job.getId(), + buildDataDescription(job), + frequency.millis(), + queryDelay.millis(), + dataExtractorFactory, + timingStatsReporter, + parentTaskAssigningClient, + auditor, + annotationPersister, + currentTimeSupplier, + delayedDataDetector, + datafeedConfig.getMaxEmptySearches(), + latestFinalBucketEndMs, + latestRecordTimeMs, + context.getRestartTimeInfo().haveSeenDataPreviously(), + delayedDataCheckFreq + ); + + listener.onResponse(datafeedJob); + }, e -> { + auditor.error(job.getId(), e.getMessage()); + listener.onFailure(e); + }); DataExtractorFactory.create( parentTaskAssigningClient, @@ -133,18 +146,17 @@ void build(TransportStartDatafeedAction.DatafeedTask task, DatafeedContext conte job, xContentRegistry, timingStatsReporter, - dataExtractorFactoryHandler); + dataExtractorFactoryHandler + ); } private void checkRemoteIndicesAreAvailable(DatafeedConfig datafeedConfig) { if (remoteClusterClient == false) { List remoteIndices = RemoteClusterLicenseChecker.remoteIndices(datafeedConfig.getIndices()); if (remoteIndices.isEmpty() == false) { - throw ExceptionsHelper.badRequestException(Messages.getMessage( - Messages.DATAFEED_NEEDS_REMOTE_CLUSTER_SEARCH, - datafeedConfig.getId(), - remoteIndices, - nodeName)); + throw ExceptionsHelper.badRequestException( + Messages.getMessage(Messages.DATAFEED_NEEDS_REMOTE_CLUSTER_SEARCH, datafeedConfig.getId(), remoteIndices, nodeName) + ); } } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/DatafeedManager.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/DatafeedManager.java index 216d8b2a04a37..d71f480a953e4 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/DatafeedManager.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/DatafeedManager.java @@ -18,9 +18,6 @@ import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.core.CheckedConsumer; import org.elasticsearch.core.Nullable; import org.elasticsearch.index.IndexNotFoundException; @@ -28,6 +25,9 @@ import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.persistent.PersistentTasksCustomMetadata; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.xpack.core.action.util.QueryPage; import org.elasticsearch.xpack.core.ml.MlConfigIndex; @@ -90,12 +90,14 @@ public final class DatafeedManager { private final MlConfigMigrationEligibilityCheck migrationEligibilityCheck; private final Settings settings; - public DatafeedManager(DatafeedConfigProvider datafeedConfigProvider, - JobConfigProvider jobConfigProvider, - NamedXContentRegistry xContentRegistry, - ClusterService clusterService, - Settings settings, - Client client) { + public DatafeedManager( + DatafeedConfigProvider datafeedConfigProvider, + JobConfigProvider jobConfigProvider, + NamedXContentRegistry xContentRegistry, + ClusterService clusterService, + Settings settings, + Client client + ) { this.datafeedConfigProvider = datafeedConfigProvider; this.jobConfigProvider = jobConfigProvider; this.xContentRegistry = xContentRegistry; @@ -128,36 +130,36 @@ public void putDatafeed( ActionListener privResponseListener = ActionListener.wrap( r -> handlePrivsResponse(username, request, r, state, threadPool, listener), - listener::onFailure); - - ActionListener getRollupIndexCapsActionHandler = ActionListener.wrap( - response -> { - if (response.getJobs().isEmpty()) { // This means no rollup indexes are in the config - indicesPrivilegesBuilder.privileges(SearchAction.NAME); - } else { - indicesPrivilegesBuilder.privileges(SearchAction.NAME, RollupSearchAction.NAME); - } + listener::onFailure + ); + + ActionListener getRollupIndexCapsActionHandler = ActionListener.wrap(response -> { + if (response.getJobs().isEmpty()) { // This means no rollup indexes are in the config + indicesPrivilegesBuilder.privileges(SearchAction.NAME); + } else { + indicesPrivilegesBuilder.privileges(SearchAction.NAME, RollupSearchAction.NAME); + } + privRequest.indexPrivileges(indicesPrivilegesBuilder.build()); + client.execute(HasPrivilegesAction.INSTANCE, privRequest, privResponseListener); + }, e -> { + if (ExceptionsHelper.unwrapCause(e) instanceof IndexNotFoundException) { + indicesPrivilegesBuilder.privileges(SearchAction.NAME); privRequest.indexPrivileges(indicesPrivilegesBuilder.build()); client.execute(HasPrivilegesAction.INSTANCE, privRequest, privResponseListener); - }, - e -> { - if (ExceptionsHelper.unwrapCause(e) instanceof IndexNotFoundException) { - indicesPrivilegesBuilder.privileges(SearchAction.NAME); - privRequest.indexPrivileges(indicesPrivilegesBuilder.build()); - client.execute(HasPrivilegesAction.INSTANCE, privRequest, privResponseListener); - } else { - listener.onFailure(e); - } + } else { + listener.onFailure(e); } - ); + }); if (RemoteClusterLicenseChecker.containsRemoteIndex(request.getDatafeed().getIndices())) { getRollupIndexCapsActionHandler.onResponse(new GetRollupIndexCapsAction.Response()); } else { - executeAsyncWithOrigin(client, + executeAsyncWithOrigin( + client, ML_ORIGIN, GetRollupIndexCapsAction.INSTANCE, new GetRollupIndexCapsAction.Request(indices), - getRollupIndexCapsActionHandler); + getRollupIndexCapsActionHandler + ); } }); } else { @@ -176,48 +178,55 @@ public void getDatafeeds(GetDatafeedsAction.Request request, ClusterState state, state ); - datafeedConfigProvider.expandDatafeedConfigs(request.getDatafeedId(), request.allowNoMatch(), ActionListener.wrap( - datafeedBuilders -> { + datafeedConfigProvider.expandDatafeedConfigs( + request.getDatafeedId(), + request.allowNoMatch(), + ActionListener.wrap(datafeedBuilders -> { // Check for duplicate datafeeds for (DatafeedConfig.Builder datafeed : datafeedBuilders) { if (clusterStateConfigs.containsKey(datafeed.getId())) { - listener.onFailure(new IllegalStateException("Datafeed [" + datafeed.getId() + "] configuration " + - "exists in both clusterstate and index")); + listener.onFailure( + new IllegalStateException( + "Datafeed [" + datafeed.getId() + "] configuration " + "exists in both clusterstate and index" + ) + ); return; } } // Merge cluster state and index configs List datafeeds = new ArrayList<>(datafeedBuilders.size() + clusterStateConfigs.values().size()); - for (DatafeedConfig.Builder builder: datafeedBuilders) { + for (DatafeedConfig.Builder builder : datafeedBuilders) { datafeeds.add(builder.build()); } datafeeds.addAll(clusterStateConfigs.values()); Collections.sort(datafeeds, Comparator.comparing(DatafeedConfig::getId)); listener.onResponse(new QueryPage<>(datafeeds, datafeeds.size(), DatafeedConfig.RESULTS_FIELD)); - }, - listener::onFailure - )); + }, listener::onFailure) + ); } public void getDatafeedsByJobIds(Set jobIds, ClusterState state, ActionListener> listener) { - datafeedConfigProvider.findDatafeedsByJobIds(jobIds, ActionListener.wrap( - datafeeds -> { - Map response = new HashMap<>(datafeeds); - Map fromState = MlMetadata.getMlMetadata(state).getDatafeedsByJobIds(jobIds); - for (Map.Entry datafeedConfigEntry : fromState.entrySet()) { - DatafeedConfig.Builder alreadyExistingDatafeed = response.get(datafeedConfigEntry.getKey()); - if (alreadyExistingDatafeed != null) { - if (alreadyExistingDatafeed.getId().equals(datafeedConfigEntry.getValue().getId())) { - listener.onFailure(new IllegalStateException( + datafeedConfigProvider.findDatafeedsByJobIds(jobIds, ActionListener.wrap(datafeeds -> { + Map response = new HashMap<>(datafeeds); + Map fromState = MlMetadata.getMlMetadata(state).getDatafeedsByJobIds(jobIds); + for (Map.Entry datafeedConfigEntry : fromState.entrySet()) { + DatafeedConfig.Builder alreadyExistingDatafeed = response.get(datafeedConfigEntry.getKey()); + if (alreadyExistingDatafeed != null) { + if (alreadyExistingDatafeed.getId().equals(datafeedConfigEntry.getValue().getId())) { + listener.onFailure( + new IllegalStateException( "Datafeed [" + alreadyExistingDatafeed.getId() + "] configuration " - + "exists in both clusterstate and index")); - return; - } - listener.onFailure(new IllegalStateException( + + "exists in both clusterstate and index" + ) + ); + return; + } + listener.onFailure( + new IllegalStateException( "datafeed [" + datafeedConfigEntry.getValue().getId() + "] configuration in cluster state and [" @@ -225,15 +234,14 @@ public void getDatafeedsByJobIds(Set jobIds, ClusterState state, ActionL + "] in the configuration index both refer to job [" + datafeedConfigEntry.getKey() + "]" - )); - return; - } - response.put(datafeedConfigEntry.getKey(), new DatafeedConfig.Builder(datafeedConfigEntry.getValue())); + ) + ); + return; } - listener.onResponse(response); - }, - listener::onFailure - )); + response.put(datafeedConfigEntry.getKey(), new DatafeedConfig.Builder(datafeedConfigEntry.getValue())); + } + listener.onResponse(response); + }, listener::onFailure)); } public void updateDatafeed( @@ -249,67 +257,70 @@ public void updateDatafeed( } // Check datafeed is stopped if (getDatafeedTask(state, request.getUpdate().getId()) != null) { - listener.onFailure(ExceptionsHelper.conflictStatusException( - Messages.getMessage(Messages.DATAFEED_CANNOT_UPDATE_IN_CURRENT_STATE, - request.getUpdate().getId(), DatafeedState.STARTED))); + listener.onFailure( + ExceptionsHelper.conflictStatusException( + Messages.getMessage( + Messages.DATAFEED_CANNOT_UPDATE_IN_CURRENT_STATE, + request.getUpdate().getId(), + DatafeedState.STARTED + ) + ) + ); return; } - Runnable doUpdate = () -> - useSecondaryAuthIfAvailable(securityContext, () -> { - final Map headers = threadPool.getThreadContext().getHeaders(); - datafeedConfigProvider.updateDatefeedConfig( - request.getUpdate().getId(), - request.getUpdate(), - headers, - jobConfigProvider::validateDatafeedJob, - ActionListener.wrap( - updatedConfig -> listener.onResponse(new PutDatafeedAction.Response(updatedConfig)), - listener::onFailure)); - }); + Runnable doUpdate = () -> useSecondaryAuthIfAvailable(securityContext, () -> { + final Map headers = threadPool.getThreadContext().getHeaders(); + datafeedConfigProvider.updateDatefeedConfig( + request.getUpdate().getId(), + request.getUpdate(), + headers, + jobConfigProvider::validateDatafeedJob, + ActionListener.wrap( + updatedConfig -> listener.onResponse(new PutDatafeedAction.Response(updatedConfig)), + listener::onFailure + ) + ); + }); // Obviously if we're updating a datafeed it's impossible that the config index has no mappings at // all, but if we rewrite the datafeed config we may add new fields that require the latest mappings ElasticsearchMappings.addDocMappingIfMissing( - MlConfigIndex.indexName(), MlConfigIndex::mapping, client, state, request.masterNodeTimeout(), - ActionListener.wrap(bool -> doUpdate.run(), listener::onFailure)); + MlConfigIndex.indexName(), + MlConfigIndex::mapping, + client, + state, + request.masterNodeTimeout(), + ActionListener.wrap(bool -> doUpdate.run(), listener::onFailure) + ); } - public void deleteDatafeed( - DeleteDatafeedAction.Request request, - ClusterState state, - ActionListener listener - ) { + public void deleteDatafeed(DeleteDatafeedAction.Request request, ClusterState state, ActionListener listener) { if (getDatafeedTask(state, request.getDatafeedId()) != null) { - listener.onFailure(ExceptionsHelper.conflictStatusException( - Messages.getMessage(Messages.DATAFEED_CANNOT_DELETE_IN_CURRENT_STATE, request.getDatafeedId(), DatafeedState.STARTED))); + listener.onFailure( + ExceptionsHelper.conflictStatusException( + Messages.getMessage(Messages.DATAFEED_CANNOT_DELETE_IN_CURRENT_STATE, request.getDatafeedId(), DatafeedState.STARTED) + ) + ); return; } String datafeedId = request.getDatafeedId(); - datafeedConfigProvider.getDatafeedConfig( - datafeedId, - ActionListener.wrap( - datafeedConfigBuilder -> { - String jobId = datafeedConfigBuilder.build().getJobId(); - JobDataDeleter jobDataDeleter = new JobDataDeleter(client, jobId); - jobDataDeleter.deleteDatafeedTimingStats( - ActionListener.wrap( - unused1 -> { - datafeedConfigProvider.deleteDatafeedConfig( - datafeedId, - ActionListener.wrap( - unused2 -> listener.onResponse(AcknowledgedResponse.TRUE), - listener::onFailure)); - }, - listener::onFailure)); - }, - listener::onFailure)); + datafeedConfigProvider.getDatafeedConfig(datafeedId, ActionListener.wrap(datafeedConfigBuilder -> { + String jobId = datafeedConfigBuilder.build().getJobId(); + JobDataDeleter jobDataDeleter = new JobDataDeleter(client, jobId); + jobDataDeleter.deleteDatafeedTimingStats(ActionListener.wrap(unused1 -> { + datafeedConfigProvider.deleteDatafeedConfig( + datafeedId, + ActionListener.wrap(unused2 -> listener.onResponse(AcknowledgedResponse.TRUE), listener::onFailure) + ); + }, listener::onFailure)); + }, listener::onFailure)); } - private PersistentTasksCustomMetadata.PersistentTask getDatafeedTask(ClusterState state, String datafeedId) { + private PersistentTasksCustomMetadata.PersistentTask getDatafeedTask(ClusterState state, String datafeedId) { PersistentTasksCustomMetadata tasks = state.getMetadata().custom(PersistentTasksCustomMetadata.TYPE); return MlTasks.getDatafeedTask(datafeedId, tasks); } @@ -328,19 +339,21 @@ private Map expandClusterStateDatafeeds( for (String expandedDatafeedId : expandedDatafeedIds) { configById.put(expandedDatafeedId, mlMetadata.getDatafeed(expandedDatafeedId)); } - } catch (Exception e){ + } catch (Exception e) { // ignore } return configById; } - private void handlePrivsResponse(String username, - PutDatafeedAction.Request request, - HasPrivilegesResponse response, - ClusterState clusterState, - ThreadPool threadPool, - ActionListener listener) throws IOException { + private void handlePrivsResponse( + String username, + PutDatafeedAction.Request request, + HasPrivilegesResponse response, + ClusterState clusterState, + ThreadPool threadPool, + ActionListener listener + ) throws IOException { if (response.isCompleteMatch()) { putDatafeed(request, threadPool.getThreadContext().getHeaders(), clusterState, listener); } else { @@ -352,16 +365,23 @@ private void handlePrivsResponse(String username, } builder.endObject(); - listener.onFailure(Exceptions.authorizationError("Cannot create datafeed [{}]" + - " because user {} lacks permissions on the indices: {}", - request.getDatafeed().getId(), username, Strings.toString(builder))); + listener.onFailure( + Exceptions.authorizationError( + "Cannot create datafeed [{}]" + " because user {} lacks permissions on the indices: {}", + request.getDatafeed().getId(), + username, + Strings.toString(builder) + ) + ); } } - private void putDatafeed(PutDatafeedAction.Request request, - Map headers, - ClusterState clusterState, - ActionListener listener) { + private void putDatafeed( + PutDatafeedAction.Request request, + Map headers, + ClusterState clusterState, + ActionListener listener + ) { String datafeedId = request.getDatafeed().getId(); String jobId = request.getDatafeed().getJobId(); @@ -379,7 +399,8 @@ private void putDatafeed(PutDatafeedAction.Request request, ActionListener.wrap( indexResponse -> listener.onResponse(new PutDatafeedAction.Response(request.getDatafeed())), listener::onFailure - )); + ) + ); }; CheckedConsumer validationOk = ok -> { @@ -394,11 +415,14 @@ private void putDatafeed(PutDatafeedAction.Request request, client, clusterState, request.masterNodeTimeout(), - ActionListener.wrap(mappingsUpdated, listener::onFailure)); + ActionListener.wrap(mappingsUpdated, listener::onFailure) + ); }; - CheckedConsumer jobOk = ok -> - jobConfigProvider.validateDatafeedJob(request.getDatafeed(), ActionListener.wrap(validationOk, listener::onFailure)); + CheckedConsumer jobOk = ok -> jobConfigProvider.validateDatafeedJob( + request.getDatafeed(), + ActionListener.wrap(validationOk, listener::onFailure) + ); checkJobDoesNotHaveADatafeed(jobId, ActionListener.wrap(jobOk, listener::onFailure)); } @@ -417,24 +441,30 @@ private ElasticsearchException checkConfigsAreNotDefinedInClusterState(String da } if (mlMetadata.getDatafeedByJobId(jobId).isPresent()) { - return ExceptionsHelper.conflictStatusException("Cannot create datafeed [" + datafeedId + "] as a " + - "job [" + jobId + "] defined in the cluster state references a datafeed with the same Id"); + return ExceptionsHelper.conflictStatusException( + "Cannot create datafeed [" + + datafeedId + + "] as a " + + "job [" + + jobId + + "] defined in the cluster state references a datafeed with the same Id" + ); } return null; } private void checkJobDoesNotHaveADatafeed(String jobId, ActionListener listener) { - datafeedConfigProvider.findDatafeedIdsForJobIds(Collections.singletonList(jobId), ActionListener.wrap( - datafeedIds -> { - if (datafeedIds.isEmpty()) { - listener.onResponse(Boolean.TRUE); - } else { - listener.onFailure(ExceptionsHelper.conflictStatusException("A datafeed [" + datafeedIds.iterator().next() - + "] already exists for job [" + jobId + "]")); - } - }, - listener::onFailure - )); + datafeedConfigProvider.findDatafeedIdsForJobIds(Collections.singletonList(jobId), ActionListener.wrap(datafeedIds -> { + if (datafeedIds.isEmpty()) { + listener.onResponse(Boolean.TRUE); + } else { + listener.onFailure( + ExceptionsHelper.conflictStatusException( + "A datafeed [" + datafeedIds.iterator().next() + "] already exists for job [" + jobId + "]" + ) + ); + } + }, listener::onFailure)); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/DatafeedNodeSelector.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/DatafeedNodeSelector.java index 8bc8533db33cc..c3d333b7d142b 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/DatafeedNodeSelector.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/DatafeedNodeSelector.java @@ -15,8 +15,8 @@ import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.routing.IndexRoutingTable; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; +import org.elasticsearch.core.Nullable; import org.elasticsearch.license.RemoteClusterLicenseChecker; import org.elasticsearch.persistent.PersistentTasksCustomMetadata; import org.elasticsearch.rest.RestStatus; @@ -37,10 +37,14 @@ public class DatafeedNodeSelector { private static final Logger LOGGER = LogManager.getLogger(DatafeedNodeSelector.class); - public static final PersistentTasksCustomMetadata.Assignment AWAITING_JOB_ASSIGNMENT = - new PersistentTasksCustomMetadata.Assignment(null, "datafeed awaiting job assignment."); - public static final PersistentTasksCustomMetadata.Assignment AWAITING_JOB_RELOCATION = - new PersistentTasksCustomMetadata.Assignment(null, "datafeed awaiting job relocation."); + public static final PersistentTasksCustomMetadata.Assignment AWAITING_JOB_ASSIGNMENT = new PersistentTasksCustomMetadata.Assignment( + null, + "datafeed awaiting job assignment." + ); + public static final PersistentTasksCustomMetadata.Assignment AWAITING_JOB_RELOCATION = new PersistentTasksCustomMetadata.Assignment( + null, + "datafeed awaiting job relocation." + ); private final String datafeedId; private final String jobId; @@ -50,8 +54,14 @@ public class DatafeedNodeSelector { private final IndexNameExpressionResolver resolver; private final IndicesOptions indicesOptions; - public DatafeedNodeSelector(ClusterState clusterState, IndexNameExpressionResolver resolver, String datafeedId, - String jobId, List datafeedIndices, IndicesOptions indicesOptions) { + public DatafeedNodeSelector( + ClusterState clusterState, + IndexNameExpressionResolver resolver, + String datafeedId, + String jobId, + List datafeedIndices, + IndicesOptions indicesOptions + ) { PersistentTasksCustomMetadata tasks = clusterState.getMetadata().custom(PersistentTasksCustomMetadata.TYPE); this.datafeedId = datafeedId; this.jobId = jobId; @@ -64,16 +74,23 @@ public DatafeedNodeSelector(ClusterState clusterState, IndexNameExpressionResolv public void checkDatafeedTaskCanBeCreated() { if (MlMetadata.getMlMetadata(clusterState).isUpgradeMode()) { - String msg = "Unable to start datafeed [" + datafeedId +"] explanation [" + AWAITING_UPGRADE.getExplanation() + "]"; + String msg = "Unable to start datafeed [" + datafeedId + "] explanation [" + AWAITING_UPGRADE.getExplanation() + "]"; LOGGER.debug(msg); Exception detail = new IllegalStateException(msg); - throw new ElasticsearchStatusException("Could not start datafeed [" + datafeedId +"] as indices are being upgraded", - RestStatus.TOO_MANY_REQUESTS, detail); + throw new ElasticsearchStatusException( + "Could not start datafeed [" + datafeedId + "] as indices are being upgraded", + RestStatus.TOO_MANY_REQUESTS, + detail + ); } AssignmentFailure assignmentFailure = checkAssignment(); if (assignmentFailure != null && assignmentFailure.isCriticalForTaskCreation) { - String msg = "No node found to start datafeed [" + datafeedId + "], " + - "allocation explanation [" + assignmentFailure.reason + "]"; + String msg = "No node found to start datafeed [" + + datafeedId + + "], " + + "allocation explanation [" + + assignmentFailure.reason + + "]"; LOGGER.debug(msg); throw ExceptionsHelper.conflictStatusException(msg); } @@ -102,7 +119,7 @@ public PersistentTasksCustomMetadata.Assignment selectNode(Collection candidateNode.getId().equals(jobNode)) == false) { @@ -129,14 +146,20 @@ private AssignmentFailure checkAssignment() { if (jobState.isAnyOf(JobState.OPENING, JobState.OPENED) == false) { // lets try again later when the job has been opened: - String reason = "cannot start datafeed [" + datafeedId + "], because the job's [" + jobId - + "] state is [" + jobState + "] while state [" + JobState.OPENED + "] is required"; + String reason = "cannot start datafeed [" + + datafeedId + + "], because the job's [" + + jobId + + "] state is [" + + jobState + + "] while state [" + + JobState.OPENED + + "] is required"; priorityFailureCollector.add(new AssignmentFailure(reason, true)); } if (jobTaskState != null && jobTaskState.isStatusStale(jobTask)) { - String reason = "cannot start datafeed [" + datafeedId + "], because the job's [" + jobId - + "] state is stale"; + String reason = "cannot start datafeed [" + datafeedId + "], because the job's [" + jobId + "] state is stale"; priorityFailureCollector.add(new AssignmentFailure(reason, true)); } @@ -155,24 +178,39 @@ private AssignmentFailure verifyIndicesActive() { try { concreteIndices = resolver.concreteIndexNames(clusterState, indicesOptions, true, index); if (concreteIndices.length == 0) { - return new AssignmentFailure("cannot start datafeed [" + datafeedId + "] because index [" - + Strings.arrayToCommaDelimitedString(index) + "] does not exist, is closed, or is still initializing.", true); + return new AssignmentFailure( + "cannot start datafeed [" + + datafeedId + + "] because index [" + + Strings.arrayToCommaDelimitedString(index) + + "] does not exist, is closed, or is still initializing.", + true + ); } } catch (Exception e) { - String msg = new ParameterizedMessage("failed resolving indices given [{}] and indices_options [{}]", + String msg = new ParameterizedMessage( + "failed resolving indices given [{}] and indices_options [{}]", Strings.arrayToCommaDelimitedString(index), - indicesOptions).getFormattedMessage(); + indicesOptions + ).getFormattedMessage(); LOGGER.debug("[" + datafeedId + "] " + msg, e); return new AssignmentFailure( "cannot start datafeed [" + datafeedId + "] because it " + msg + " with exception [" + e.getMessage() + "]", - true); + true + ); } for (String concreteIndex : concreteIndices) { IndexRoutingTable routingTable = clusterState.getRoutingTable().index(concreteIndex); if (routingTable == null || routingTable.allPrimaryShardsActive() == false) { - return new AssignmentFailure("cannot start datafeed [" + datafeedId + "] because index [" - + concreteIndex + "] does not have all primary shards active yet.", false); + return new AssignmentFailure( + "cannot start datafeed [" + + datafeedId + + "] because index [" + + concreteIndex + + "] does not have all primary shards active yet.", + false + ); } } return null; diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/DatafeedRunner.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/DatafeedRunner.java index 23cc0d48e8eda..8993c07c093f4 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/DatafeedRunner.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/DatafeedRunner.java @@ -16,9 +16,9 @@ import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.ClusterStateListener; import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.AbstractRunnable; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.persistent.PersistentTasksCustomMetadata; import org.elasticsearch.persistent.PersistentTasksCustomMetadata.PersistentTask; import org.elasticsearch.rest.RestStatus; @@ -71,9 +71,16 @@ public class DatafeedRunner { private final AutodetectProcessManager autodetectProcessManager; private final DatafeedContextProvider datafeedContextProvider; - public DatafeedRunner(ThreadPool threadPool, Client client, ClusterService clusterService, DatafeedJobBuilder datafeedJobBuilder, - LongSupplier currentTimeSupplier, AnomalyDetectionAuditor auditor, - AutodetectProcessManager autodetectProcessManager, DatafeedContextProvider datafeedContextProvider) { + public DatafeedRunner( + ThreadPool threadPool, + Client client, + ClusterService clusterService, + DatafeedJobBuilder datafeedJobBuilder, + LongSupplier currentTimeSupplier, + AnomalyDetectionAuditor auditor, + AutodetectProcessManager autodetectProcessManager, + DatafeedContextProvider datafeedContextProvider + ) { this.client = Objects.requireNonNull(client); this.clusterService = Objects.requireNonNull(clusterService); this.threadPool = Objects.requireNonNull(threadPool); @@ -86,50 +93,51 @@ public DatafeedRunner(ThreadPool threadPool, Client client, ClusterService clust } public void run(TransportStartDatafeedAction.DatafeedTask task, Consumer finishHandler) { - ActionListener datafeedJobHandler = ActionListener.wrap( - datafeedJob -> { - String jobId = datafeedJob.getJobId(); - Holder holder = new Holder(task, task.getDatafeedId(), datafeedJob, new ProblemTracker(auditor, jobId), finishHandler); - if (task.getStoppedOrIsolatedBeforeRunning() == StoppedOrIsolatedBeforeRunning.NEITHER) { - runningDatafeedsOnThisNode.put(task.getAllocationId(), holder); - task.updatePersistentTaskState(DatafeedState.STARTED, new ActionListener>() { - @Override - public void onResponse(PersistentTask persistentTask) { - taskRunner.runWhenJobIsOpened(task, jobId); - } + ActionListener datafeedJobHandler = ActionListener.wrap(datafeedJob -> { + String jobId = datafeedJob.getJobId(); + Holder holder = new Holder(task, task.getDatafeedId(), datafeedJob, new ProblemTracker(auditor, jobId), finishHandler); + if (task.getStoppedOrIsolatedBeforeRunning() == StoppedOrIsolatedBeforeRunning.NEITHER) { + runningDatafeedsOnThisNode.put(task.getAllocationId(), holder); + task.updatePersistentTaskState(DatafeedState.STARTED, new ActionListener>() { + @Override + public void onResponse(PersistentTask persistentTask) { + taskRunner.runWhenJobIsOpened(task, jobId); + } - @Override - public void onFailure(Exception e) { - if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) { - // The task was stopped in the meantime, no need to do anything - logger.info("[{}] Aborting as datafeed has been stopped", task.getDatafeedId()); - runningDatafeedsOnThisNode.remove(task.getAllocationId()); - finishHandler.accept(null); - } else { - finishHandler.accept(e); - } + @Override + public void onFailure(Exception e) { + if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) { + // The task was stopped in the meantime, no need to do anything + logger.info("[{}] Aborting as datafeed has been stopped", task.getDatafeedId()); + runningDatafeedsOnThisNode.remove(task.getAllocationId()); + finishHandler.accept(null); + } else { + finishHandler.accept(e); } - }); - } else { - logger.info("[{}] Datafeed has been {} before running", task.getDatafeedId(), - task.getStoppedOrIsolatedBeforeRunning().toString().toLowerCase(Locale.ROOT)); - finishHandler.accept(null); - } - }, finishHandler - ); + } + }); + } else { + logger.info( + "[{}] Datafeed has been {} before running", + task.getDatafeedId(), + task.getStoppedOrIsolatedBeforeRunning().toString().toLowerCase(Locale.ROOT) + ); + finishHandler.accept(null); + } + }, finishHandler); - ActionListener datafeedContextListener = ActionListener.wrap( - datafeedContext -> { - if (task.getStoppedOrIsolatedBeforeRunning() == StoppedOrIsolatedBeforeRunning.NEITHER) { - datafeedJobBuilder.build(task, datafeedContext, datafeedJobHandler); - } else { - logger.info("[{}] Datafeed has been {} while building context", task.getDatafeedId(), - task.getStoppedOrIsolatedBeforeRunning().toString().toLowerCase(Locale.ROOT)); - finishHandler.accept(null); - } - }, - finishHandler - ); + ActionListener datafeedContextListener = ActionListener.wrap(datafeedContext -> { + if (task.getStoppedOrIsolatedBeforeRunning() == StoppedOrIsolatedBeforeRunning.NEITHER) { + datafeedJobBuilder.build(task, datafeedContext, datafeedJobHandler); + } else { + logger.info( + "[{}] Datafeed has been {} while building context", + task.getDatafeedId(), + task.getStoppedOrIsolatedBeforeRunning().toString().toLowerCase(Locale.ROOT) + ); + finishHandler.accept(null); + } + }, finishHandler); datafeedContextProvider.buildDatafeedContext(task.getDatafeedId(), datafeedContextListener); } @@ -208,60 +216,61 @@ public boolean finishedLookBack(TransportStartDatafeedAction.DatafeedTask task) // otherwise if a stop datafeed call is made immediately after the start datafeed call we could cancel // the DatafeedTask without stopping datafeed, which causes the datafeed to keep on running. private void innerRun(Holder holder, long startTime, Long endTime) { - holder.cancellable = - Scheduler.wrapAsCancellable(threadPool.executor(MachineLearning.DATAFEED_THREAD_POOL_NAME).submit(new AbstractRunnable() { - - @Override - public void onFailure(Exception e) { - logger.error("Failed lookback import for job [" + holder.datafeedJob.getJobId() + "]", e); - holder.stop("general_lookback_failure", TimeValue.timeValueSeconds(20), e); - } + holder.cancellable = Scheduler.wrapAsCancellable( + threadPool.executor(MachineLearning.DATAFEED_THREAD_POOL_NAME).submit(new AbstractRunnable() { - @Override - protected void doRun() { - Long next = null; - try { - next = holder.executeLookBack(startTime, endTime); - } catch (DatafeedJob.ExtractionProblemException e) { - if (endTime == null) { - next = e.nextDelayInMsSinceEpoch; - } - holder.problemTracker.reportExtractionProblem(e); - } catch (DatafeedJob.AnalysisProblemException e) { - if (endTime == null) { - next = e.nextDelayInMsSinceEpoch; - } - holder.problemTracker.reportAnalysisProblem(e); - if (e.shouldStop) { - holder.stop("lookback_analysis_error", TimeValue.timeValueSeconds(20), e); - return; - } - } catch (DatafeedJob.EmptyDataCountException e) { - if (endTime == null) { - holder.problemTracker.reportEmptyDataCount(); - next = e.nextDelayInMsSinceEpoch; - } else { - // Notify that a lookback-only run found no data - String lookbackNoDataMsg = Messages.getMessage(Messages.JOB_AUDIT_DATAFEED_LOOKBACK_NO_DATA); - logger.warn("[{}] {}", holder.datafeedJob.getJobId(), lookbackNoDataMsg); - auditor.warning(holder.datafeedJob.getJobId(), lookbackNoDataMsg); - } - } catch (Exception e) { + @Override + public void onFailure(Exception e) { logger.error("Failed lookback import for job [" + holder.datafeedJob.getJobId() + "]", e); holder.stop("general_lookback_failure", TimeValue.timeValueSeconds(20), e); - return; } - holder.finishedLookback(true); - if (holder.isIsolated() == false) { - if (next != null) { - doDatafeedRealtime(next, holder.datafeedJob.getJobId(), holder); - } else { - holder.stop("no_realtime", TimeValue.timeValueSeconds(20), null); - holder.problemTracker.finishReport(); + + @Override + protected void doRun() { + Long next = null; + try { + next = holder.executeLookBack(startTime, endTime); + } catch (DatafeedJob.ExtractionProblemException e) { + if (endTime == null) { + next = e.nextDelayInMsSinceEpoch; + } + holder.problemTracker.reportExtractionProblem(e); + } catch (DatafeedJob.AnalysisProblemException e) { + if (endTime == null) { + next = e.nextDelayInMsSinceEpoch; + } + holder.problemTracker.reportAnalysisProblem(e); + if (e.shouldStop) { + holder.stop("lookback_analysis_error", TimeValue.timeValueSeconds(20), e); + return; + } + } catch (DatafeedJob.EmptyDataCountException e) { + if (endTime == null) { + holder.problemTracker.reportEmptyDataCount(); + next = e.nextDelayInMsSinceEpoch; + } else { + // Notify that a lookback-only run found no data + String lookbackNoDataMsg = Messages.getMessage(Messages.JOB_AUDIT_DATAFEED_LOOKBACK_NO_DATA); + logger.warn("[{}] {}", holder.datafeedJob.getJobId(), lookbackNoDataMsg); + auditor.warning(holder.datafeedJob.getJobId(), lookbackNoDataMsg); + } + } catch (Exception e) { + logger.error("Failed lookback import for job [" + holder.datafeedJob.getJobId() + "]", e); + holder.stop("general_lookback_failure", TimeValue.timeValueSeconds(20), e); + return; + } + holder.finishedLookback(true); + if (holder.isIsolated() == false) { + if (next != null) { + doDatafeedRealtime(next, holder.datafeedJob.getJobId(), holder); + } else { + holder.stop("no_realtime", TimeValue.timeValueSeconds(20), null); + holder.problemTracker.finishReport(); + } } } - } - })); + }) + ); } void doDatafeedRealtime(long delayInMsSinceEpoch, String jobId, Holder holder) { @@ -295,8 +304,13 @@ protected void doRun() { } catch (DatafeedJob.EmptyDataCountException e) { int emptyDataCount = holder.problemTracker.reportEmptyDataCount(); if (e.haveEverSeenData == false && holder.shouldStopAfterEmptyData(emptyDataCount)) { - logger.warn("Datafeed for [" + jobId + "] has seen no data in [" + emptyDataCount - + "] attempts, and never seen any data previously, so stopping..."); + logger.warn( + "Datafeed for [" + + jobId + + "] has seen no data in [" + + emptyDataCount + + "] attempts, and never seen any data previously, so stopping..." + ); // In this case we auto-close the job, as though a lookback-only datafeed stopped holder.stop("no_data", TimeValue.timeValueSeconds(20), e, true); return; @@ -371,8 +385,13 @@ public class Holder { private volatile boolean isNodeShuttingDown; private volatile boolean lookbackFinished; - Holder(TransportStartDatafeedAction.DatafeedTask task, String datafeedId, DatafeedJob datafeedJob, - ProblemTracker problemTracker, Consumer finishHandler) { + Holder( + TransportStartDatafeedAction.DatafeedTask task, + String datafeedId, + DatafeedJob datafeedJob, + ProblemTracker problemTracker, + Consumer finishHandler + ) { this.task = task; this.allocationId = task.getAllocationId(); this.datafeedId = datafeedId; @@ -416,24 +435,41 @@ public void stop(String source, TimeValue timeout, Exception e, boolean autoClos if (datafeedJob.stop()) { boolean acquired = false; try { - logger.info("[{}] try lock [{}] to stop datafeed [{}] for job [{}]...", source, timeout, datafeedId, - datafeedJob.getJobId()); + logger.info( + "[{}] try lock [{}] to stop datafeed [{}] for job [{}]...", + source, + timeout, + datafeedId, + datafeedJob.getJobId() + ); acquired = datafeedJobLock.tryLock(timeout.millis(), TimeUnit.MILLISECONDS); } catch (InterruptedException e1) { Thread.currentThread().interrupt(); } finally { // It is crucial that none of the calls this "finally" block makes throws an exception for minor problems. - logger.info("[{}] stopping datafeed [{}] for job [{}], acquired [{}]...", source, datafeedId, - datafeedJob.getJobId(), acquired); + logger.info( + "[{}] stopping datafeed [{}] for job [{}], acquired [{}]...", + source, + datafeedId, + datafeedJob.getJobId(), + acquired + ); runningDatafeedsOnThisNode.remove(allocationId); if (cancellable != null) { cancellable.cancel(); } - auditor.info(datafeedJob.getJobId(), - Messages.getMessage(isIsolated() ? Messages.JOB_AUDIT_DATAFEED_ISOLATED : Messages.JOB_AUDIT_DATAFEED_STOPPED)); + auditor.info( + datafeedJob.getJobId(), + Messages.getMessage(isIsolated() ? Messages.JOB_AUDIT_DATAFEED_ISOLATED : Messages.JOB_AUDIT_DATAFEED_STOPPED) + ); datafeedJob.finishReportingTimingStats(); finishHandler.accept(e); - logger.info("[{}] datafeed [{}] for job [{}] has been stopped{}", source, datafeedId, datafeedJob.getJobId(), - acquired ? "" : ", but there may be pending tasks as the timeout [" + timeout.getStringRep() + "] expired"); + logger.info( + "[{}] datafeed [{}] for job [{}] has been stopped{}", + source, + datafeedId, + datafeedJob.getJobId(), + acquired ? "" : ", but there may be pending tasks as the timeout [" + timeout.getStringRep() + "] expired" + ); if (autoCloseJob && isIsolated() == false) { closeJob(); } @@ -519,26 +555,32 @@ private void closeJob() { return; } - task.waitForPersistentTask(Objects::isNull, TimeValue.timeValueSeconds(20), - new WaitForPersistentTaskListener() { - @Override - public void onResponse(PersistentTask persistentTask) { - CloseJobAction.Request closeJobRequest = new CloseJobAction.Request(getJobId()); - /* - Enforces that for the close job api call the current node is the coordinating node. - If we are in this callback then the local node's cluster state doesn't contain a persistent task - for the datafeed and therefor the datafeed is stopped, so there is no need for the master node to - be to coordinating node. - - Normally close job and stop datafeed are both executed via master node and both apis use master - node's local cluster state for validation purposes. In case of auto close this isn't the case and - if the job runs on a regular node then it may see the update before the close job api does in - the master node's local cluster state. This can cause the close job api the fail with a validation - error that the datafeed isn't stopped. To avoid this we use the current node as coordinating node - for the close job api call. - */ - closeJobRequest.setLocal(true); - executeAsyncWithOrigin(client, ML_ORIGIN, CloseJobAction.INSTANCE, closeJobRequest, + task.waitForPersistentTask( + Objects::isNull, + TimeValue.timeValueSeconds(20), + new WaitForPersistentTaskListener() { + @Override + public void onResponse(PersistentTask persistentTask) { + CloseJobAction.Request closeJobRequest = new CloseJobAction.Request(getJobId()); + /* + Enforces that for the close job api call the current node is the coordinating node. + If we are in this callback then the local node's cluster state doesn't contain a persistent task + for the datafeed and therefor the datafeed is stopped, so there is no need for the master node to + be to coordinating node. + + Normally close job and stop datafeed are both executed via master node and both apis use master + node's local cluster state for validation purposes. In case of auto close this isn't the case and + if the job runs on a regular node then it may see the update before the close job api does in + the master node's local cluster state. This can cause the close job api the fail with a validation + error that the datafeed isn't stopped. To avoid this we use the current node as coordinating node + for the close job api call. + */ + closeJobRequest.setLocal(true); + executeAsyncWithOrigin( + client, + ML_ORIGIN, + CloseJobAction.INSTANCE, + closeJobRequest, new ActionListener() { @Override @@ -553,21 +595,23 @@ public void onFailure(Exception e) { // Given that the UI force-deletes the datafeed and then force-deletes the job, it's // quite likely that the auto-close here will get interrupted by a process kill request, // and it's misleading/worrying to log an error in this case. - if (e instanceof ElasticsearchStatusException && - ((ElasticsearchStatusException) e).status() == RestStatus.CONFLICT) { + if (e instanceof ElasticsearchStatusException + && ((ElasticsearchStatusException) e).status() == RestStatus.CONFLICT) { logger.debug("[{}] {}", getJobId(), e.getMessage()); } else { logger.error("[" + getJobId() + "] failed to auto-close job", e); } } - }); - } + } + ); + } - @Override - public void onFailure(Exception e) { - logger.error("Failed to remove datafeed persistent task - will not auto close job [" + getJobId() + "]", e); + @Override + public void onFailure(Exception e) { + logger.error("Failed to remove datafeed persistent task - will not auto close job [" + getJobId() + "]", e); + } } - }); + ); } } @@ -581,18 +625,17 @@ private void runWhenJobIsOpened(TransportStartDatafeedAction.DatafeedTask datafe if (getJobState(tasks, jobId) == JobState.OPENED && jobHasOpenAutodetectCommunicator(tasks, jobId)) { runTask(datafeedTask); } else { - logger.info("Datafeed [{}] is waiting for job [{}] to be opened", - datafeedTask.getDatafeedId(), jobId); + logger.info("Datafeed [{}] is waiting for job [{}] to be opened", datafeedTask.getDatafeedId(), jobId); tasksToRun.add(datafeedTask); } } private void runTask(TransportStartDatafeedAction.DatafeedTask task) { - // This clearing of the thread context is not strictly necessary. Every action performed by the + // This clearing of the thread context is not strictly necessary. Every action performed by the // datafeed _should_ be done using the MlClientHelper, which will set the appropriate thread - // context. However, by clearing the thread context here if anyone forgets to use MlClientHelper + // context. However, by clearing the thread context here if anyone forgets to use MlClientHelper // somewhere else in the datafeed code then it should cause a failure in the same way in single - // and multi node clusters. If we didn't clear the thread context here then there's a risk that + // and multi node clusters. If we didn't clear the thread context here then there's a risk that // a context with sufficient permissions would coincidentally be in force in some single node // tests, leading to bugs not caught in CI due to many tests running in single node test clusters. try (ThreadContext.StoredContext ignore = threadPool.getThreadContext().stashContext()) { @@ -629,8 +672,7 @@ public void clusterChanged(ClusterChangedEvent event) { } else if (jobState == JobState.OPENED) { runTask(datafeedTask); } else { - logger.warn("Datafeed [{}] is stopping because job [{}] state is [{}]", - datafeedTask.getDatafeedId(), jobId, jobState); + logger.warn("Datafeed [{}] is stopping because job [{}] state is [{}]", datafeedTask.getDatafeedId(), jobId, jobState); datafeedTask.stop("job_never_opened", TimeValue.timeValueSeconds(20)); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/DatafeedTimingStatsReporter.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/DatafeedTimingStatsReporter.java index f90816944d967..1bb7972c4454f 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/DatafeedTimingStatsReporter.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/DatafeedTimingStatsReporter.java @@ -25,6 +25,7 @@ public class DatafeedTimingStatsReporter { private static final Logger LOGGER = LogManager.getLogger(DatafeedTimingStatsReporter.class); + /** Interface used for persisting current timing stats to the results index. */ @FunctionalInterface public interface DatafeedTimingStatsPersister { @@ -107,7 +108,8 @@ private void flush(WriteRequest.RefreshPolicy refreshPolicy) { // Since persisting datafeed timing stats is not critical, we just log a warning here. LOGGER.warn( () -> new ParameterizedMessage("[{}] failed to report datafeed timing stats", currentTimingStats.getJobId()), - ex); + ex + ); } } } @@ -127,8 +129,7 @@ public static boolean differSignificantly(DatafeedTimingStats stats1, DatafeedTi * This can be interpreted as values { value1, value2 } differing significantly from each other. */ private static boolean countsDifferSignificantly(long value1, long value2) { - return (((double) value2) / value1 < MIN_VALID_RATIO) - || (((double) value1) / value2 < MIN_VALID_RATIO); + return (((double) value2) / value1 < MIN_VALID_RATIO) || (((double) value1) / value2 < MIN_VALID_RATIO); } /** diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/delayeddatacheck/DatafeedDelayedDataDetector.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/delayeddatacheck/DatafeedDelayedDataDetector.java index e2ab9bbba3983..143c5abc75abf 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/delayeddatacheck/DatafeedDelayedDataDetector.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/delayeddatacheck/DatafeedDelayedDataDetector.java @@ -34,7 +34,6 @@ import static org.elasticsearch.xpack.core.ClientHelper.ML_ORIGIN; - /** * This class will search the buckets and indices over a given window to determine if any data is missing */ @@ -52,9 +51,17 @@ public class DatafeedDelayedDataDetector implements DelayedDataDetector { private final IndicesOptions indicesOptions; private final Map runtimeMappings; - DatafeedDelayedDataDetector(long bucketSpan, long window, String jobId, String timeField, QueryBuilder datafeedQuery, - String[] datafeedIndices, IndicesOptions indicesOptions, Map runtimeMappings, - Client client) { + DatafeedDelayedDataDetector( + long bucketSpan, + long window, + String jobId, + String timeField, + QueryBuilder datafeedQuery, + String[] datafeedIndices, + IndicesOptions indicesOptions, + Map runtimeMappings, + Client client + ) { this.bucketSpan = bucketSpan; this.window = window; this.jobId = jobId; @@ -108,7 +115,7 @@ private List checkBucketEvents(long start, long end) { request.setSort("timestamp"); request.setDescending(false); request.setExcludeInterim(true); - request.setPageParams(new PageParams(0, (int)((end - start)/bucketSpan))); + request.setPageParams(new PageParams(0, (int) ((end - start) / bucketSpan))); try (ThreadContext.StoredContext ignore = client.threadPool().getThreadContext().stashWithOrigin(ML_ORIGIN)) { GetBucketsAction.Response response = client.execute(GetBucketsAction.INSTANCE, request).actionGet(); @@ -117,17 +124,18 @@ private List checkBucketEvents(long start, long end) { } private Map checkCurrentBucketEventCount(long start, long end) { - SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder() - .size(0) - .aggregation(new DateHistogramAggregationBuilder(DATE_BUCKETS) - .fixedInterval(new DateHistogramInterval(bucketSpan + "ms")).field(timeField)) + SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().size(0) + .aggregation( + new DateHistogramAggregationBuilder(DATE_BUCKETS).fixedInterval(new DateHistogramInterval(bucketSpan + "ms")) + .field(timeField) + ) .query(ExtractorUtils.wrapInTimeRangeQuery(datafeedQuery, timeField, start, end)) .runtimeMappings(runtimeMappings); SearchRequest searchRequest = new SearchRequest(datafeedIndices).source(searchSourceBuilder).indicesOptions(indicesOptions); try (ThreadContext.StoredContext ignore = client.threadPool().getThreadContext().stashWithOrigin(ML_ORIGIN)) { SearchResponse response = client.execute(SearchAction.INSTANCE, searchRequest).actionGet(); - List buckets = ((Histogram)response.getAggregations().get(DATE_BUCKETS)).getBuckets(); + List buckets = ((Histogram) response.getAggregations().get(DATE_BUCKETS)).getBuckets(); Map hashMap = new HashMap<>(buckets.size()); for (Histogram.Bucket bucket : buckets) { long bucketTime = toHistogramKeyToEpoch(bucket.getKey()); @@ -142,11 +150,11 @@ private Map checkCurrentBucketEventCount(long start, long end) { private static long toHistogramKeyToEpoch(Object key) { if (key instanceof ZonedDateTime) { - return ((ZonedDateTime)key).toInstant().toEpochMilli(); + return ((ZonedDateTime) key).toInstant().toEpochMilli(); } else if (key instanceof Double) { - return ((Double)key).longValue(); - } else if (key instanceof Long){ - return (Long)key; + return ((Double) key).longValue(); + } else if (key instanceof Long) { + return (Long) key; } else { return -1L; } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/delayeddatacheck/DelayedDataDetectorFactory.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/delayeddatacheck/DelayedDataDetectorFactory.java index 341fb56563bd7..ad212be97eb1b 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/delayeddatacheck/DelayedDataDetectorFactory.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/delayeddatacheck/DelayedDataDetectorFactory.java @@ -38,15 +38,20 @@ public class DelayedDataDetectorFactory { * @param xContentRegistry The current NamedXContentRegistry with which to parse the query * @return A new {@link DelayedDataDetector} */ - public static DelayedDataDetector buildDetector(Job job, - DatafeedConfig datafeedConfig, - Client client, - NamedXContentRegistry xContentRegistry) { + public static DelayedDataDetector buildDetector( + Job job, + DatafeedConfig datafeedConfig, + Client client, + NamedXContentRegistry xContentRegistry + ) { if (datafeedConfig.getDelayedDataCheckConfig().isEnabled()) { - long window = validateAndCalculateWindowLength(job.getAnalysisConfig().getBucketSpan(), - datafeedConfig.getDelayedDataCheckConfig().getCheckWindow()); + long window = validateAndCalculateWindowLength( + job.getAnalysisConfig().getBucketSpan(), + datafeedConfig.getDelayedDataCheckConfig().getCheckWindow() + ); long bucketSpan = job.getAnalysisConfig().getBucketSpan() == null ? 0 : job.getAnalysisConfig().getBucketSpan().millis(); - return new DatafeedDelayedDataDetector(bucketSpan, + return new DatafeedDelayedDataDetector( + bucketSpan, window, job.getId(), job.getDataDescription().getTimeField(), @@ -54,7 +59,8 @@ public static DelayedDataDetector buildDetector(Job job, datafeedConfig.getIndices().toArray(new String[0]), datafeedConfig.getIndicesOptions(), datafeedConfig.getRuntimeMappings(), - client); + client + ); } else { return new NullDelayedDataDetector(); } @@ -69,12 +75,20 @@ private static long validateAndCalculateWindowLength(TimeValue bucketSpan, TimeV } if (currentWindow.compareTo(bucketSpan) < 0) { throw new IllegalArgumentException( - Messages.getMessage(Messages.DATAFEED_CONFIG_DELAYED_DATA_CHECK_TOO_SMALL, currentWindow.getStringRep(), - bucketSpan.getStringRep())); + Messages.getMessage( + Messages.DATAFEED_CONFIG_DELAYED_DATA_CHECK_TOO_SMALL, + currentWindow.getStringRep(), + bucketSpan.getStringRep() + ) + ); } else if (currentWindow.millis() > bucketSpan.millis() * DelayedDataCheckConfig.MAX_NUMBER_SPANABLE_BUCKETS) { throw new IllegalArgumentException( - Messages.getMessage(Messages.DATAFEED_CONFIG_DELAYED_DATA_CHECK_SPANS_TOO_MANY_BUCKETS, currentWindow.getStringRep(), - bucketSpan.getStringRep())); + Messages.getMessage( + Messages.DATAFEED_CONFIG_DELAYED_DATA_CHECK_SPANS_TOO_MANY_BUCKETS, + currentWindow.getStringRep(), + bucketSpan.getStringRep() + ) + ); } return currentWindow.millis(); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/DataExtractorFactory.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/DataExtractorFactory.java index 11e0f886b5993..23e316e7fdadb 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/DataExtractorFactory.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/DataExtractorFactory.java @@ -10,9 +10,9 @@ import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.client.Client; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.index.IndexNotFoundException; import org.elasticsearch.license.RemoteClusterLicenseChecker; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.ClientHelper; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfig; import org.elasticsearch.xpack.core.ml.datafeed.extractor.DataExtractor; @@ -33,77 +33,97 @@ public interface DataExtractorFactory { /** * Creates a {@code DataExtractorFactory} for the given datafeed-job combination. */ - static void create(Client client, - DatafeedConfig datafeed, - Job job, - NamedXContentRegistry xContentRegistry, - DatafeedTimingStatsReporter timingStatsReporter, - ActionListener listener) { + static void create( + Client client, + DatafeedConfig datafeed, + Job job, + NamedXContentRegistry xContentRegistry, + DatafeedTimingStatsReporter timingStatsReporter, + ActionListener listener + ) { final boolean hasAggs = datafeed.hasAggregations(); final boolean isComposite = hasAggs && datafeed.hasCompositeAgg(xContentRegistry); ActionListener factoryHandler = ActionListener.wrap( - factory -> listener.onResponse(datafeed.getChunkingConfig().isEnabled() - ? new ChunkedDataExtractorFactory(client, datafeed, job, xContentRegistry, factory, timingStatsReporter) : factory) - , listener::onFailure + factory -> listener.onResponse( + datafeed.getChunkingConfig().isEnabled() + ? new ChunkedDataExtractorFactory(client, datafeed, job, xContentRegistry, factory, timingStatsReporter) + : factory + ), + listener::onFailure ); - ActionListener getRollupIndexCapsActionHandler = ActionListener.wrap( - response -> { - final boolean hasRollup = response.getJobs().isEmpty() == false; - if (hasRollup && hasAggs == false) { - listener.onFailure(new IllegalArgumentException("Aggregations are required when using Rollup indices")); - return; - } - if (hasAggs == false) { - ScrollDataExtractorFactory.create(client, datafeed, job, xContentRegistry, timingStatsReporter, factoryHandler); - return; - } - if (hasRollup && datafeed.getRuntimeMappings().isEmpty() == false) { - // TODO Rollup V2 will support runtime fields - listener.onFailure(new IllegalArgumentException("The datafeed has runtime_mappings defined, " - + "runtime fields are not supported in rollup searches")); - return; - } - if (isComposite) { - String[] indices = datafeed.getIndices().toArray(new String[0]); - IndicesOptions indicesOptions = datafeed.getIndicesOptions(); - AggregatedSearchRequestBuilder aggregatedSearchRequestBuilder = hasRollup ? - RollupDataExtractorFactory.requestBuilder(client, indices, indicesOptions) : - AggregationDataExtractorFactory.requestBuilder(client, indices, indicesOptions); - final DataExtractorFactory dataExtractorFactory = new CompositeAggregationDataExtractorFactory( - client, - datafeed, - job, - xContentRegistry, - timingStatsReporter, - aggregatedSearchRequestBuilder - ); - if (datafeed.getChunkingConfig().isManual()) { - factoryHandler.onResponse(dataExtractorFactory); - } else { - listener.onResponse(dataExtractorFactory); - } - return; - } - - if (hasRollup) { - RollupDataExtractorFactory.create( - client, datafeed, job, response.getJobs(), xContentRegistry, timingStatsReporter, factoryHandler); - } else { - factoryHandler.onResponse( - new AggregationDataExtractorFactory(client, datafeed, job, xContentRegistry, timingStatsReporter)); - } - }, - e -> { - Throwable cause = ExceptionsHelper.unwrapCause(e); - if (cause instanceof IndexNotFoundException) { - listener.onFailure(new ResourceNotFoundException("datafeed [" + datafeed.getId() - + "] cannot retrieve data because index " + ((IndexNotFoundException) cause).getIndex() + " does not exist")); + ActionListener getRollupIndexCapsActionHandler = ActionListener.wrap(response -> { + final boolean hasRollup = response.getJobs().isEmpty() == false; + if (hasRollup && hasAggs == false) { + listener.onFailure(new IllegalArgumentException("Aggregations are required when using Rollup indices")); + return; + } + if (hasAggs == false) { + ScrollDataExtractorFactory.create(client, datafeed, job, xContentRegistry, timingStatsReporter, factoryHandler); + return; + } + if (hasRollup && datafeed.getRuntimeMappings().isEmpty() == false) { + // TODO Rollup V2 will support runtime fields + listener.onFailure( + new IllegalArgumentException( + "The datafeed has runtime_mappings defined, " + "runtime fields are not supported in rollup searches" + ) + ); + return; + } + if (isComposite) { + String[] indices = datafeed.getIndices().toArray(new String[0]); + IndicesOptions indicesOptions = datafeed.getIndicesOptions(); + AggregatedSearchRequestBuilder aggregatedSearchRequestBuilder = hasRollup + ? RollupDataExtractorFactory.requestBuilder(client, indices, indicesOptions) + : AggregationDataExtractorFactory.requestBuilder(client, indices, indicesOptions); + final DataExtractorFactory dataExtractorFactory = new CompositeAggregationDataExtractorFactory( + client, + datafeed, + job, + xContentRegistry, + timingStatsReporter, + aggregatedSearchRequestBuilder + ); + if (datafeed.getChunkingConfig().isManual()) { + factoryHandler.onResponse(dataExtractorFactory); } else { - listener.onFailure(e); + listener.onResponse(dataExtractorFactory); } + return; } - ); + + if (hasRollup) { + RollupDataExtractorFactory.create( + client, + datafeed, + job, + response.getJobs(), + xContentRegistry, + timingStatsReporter, + factoryHandler + ); + } else { + factoryHandler.onResponse( + new AggregationDataExtractorFactory(client, datafeed, job, xContentRegistry, timingStatsReporter) + ); + } + }, e -> { + Throwable cause = ExceptionsHelper.unwrapCause(e); + if (cause instanceof IndexNotFoundException) { + listener.onFailure( + new ResourceNotFoundException( + "datafeed [" + + datafeed.getId() + + "] cannot retrieve data because index " + + ((IndexNotFoundException) cause).getIndex() + + " does not exist" + ) + ); + } else { + listener.onFailure(e); + } + }); if (RemoteClusterLicenseChecker.containsRemoteIndex(datafeed.getIndices())) { // If we have remote indices in the data feed, don't bother checking for rollup support @@ -115,7 +135,8 @@ static void create(Client client, ClientHelper.ML_ORIGIN, GetRollupIndexCapsAction.INSTANCE, new GetRollupIndexCapsAction.Request(datafeed.getIndices().toArray(new String[0]), datafeed.getIndicesOptions()), - getRollupIndexCapsActionHandler); + getRollupIndexCapsActionHandler + ); } } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AbstractAggregationDataExtractor.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AbstractAggregationDataExtractor.java index 2189ab5217e3c..b3ba4754648b7 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AbstractAggregationDataExtractor.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AbstractAggregationDataExtractor.java @@ -36,8 +36,7 @@ * * @param The request builder type for getting data from ElasticSearch */ -abstract class AbstractAggregationDataExtractor> - implements DataExtractor { +abstract class AbstractAggregationDataExtractor> implements DataExtractor { private static final Logger LOGGER = LogManager.getLogger(AbstractAggregationDataExtractor.class); @@ -50,7 +49,10 @@ abstract class AbstractAggregationDataExtractor 1) { - throw new IllegalArgumentException("Multiple top level aggregations not supported; found: " - + aggsAsList.stream().map(Aggregation::getName).collect(Collectors.toList())); + throw new IllegalArgumentException( + "Multiple top level aggregations not supported; found: " + + aggsAsList.stream().map(Aggregation::getName).collect(Collectors.toList()) + ); } return aggs; diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AggregatedSearchRequestBuilder.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AggregatedSearchRequestBuilder.java index a6c6c3fb27a4f..d2257ecf1e310 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AggregatedSearchRequestBuilder.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AggregatedSearchRequestBuilder.java @@ -12,7 +12,6 @@ import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.search.builder.SearchSourceBuilder; - /** * This is used when building search actions for aggregated data. * diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AggregationDataExtractor.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AggregationDataExtractor.java index 5e41f205620c8..b540d261f5749 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AggregationDataExtractor.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AggregationDataExtractor.java @@ -21,14 +21,16 @@ class AggregationDataExtractor extends AbstractAggregationDataExtractor { AggregationDataExtractor( - Client client, AggregationDataExtractorContext dataExtractorContext, DatafeedTimingStatsReporter timingStatsReporter) { + Client client, + AggregationDataExtractorContext dataExtractorContext, + DatafeedTimingStatsReporter timingStatsReporter + ) { super(client, dataExtractorContext, timingStatsReporter); } @Override protected SearchRequestBuilder buildSearchRequest(SearchSourceBuilder searchSourceBuilder) { - return new SearchRequestBuilder(client, SearchAction.INSTANCE) - .setSource(searchSourceBuilder) + return new SearchRequestBuilder(client, SearchAction.INSTANCE).setSource(searchSourceBuilder) .setIndicesOptions(context.indicesOptions) .setAllowPartialSearchResults(false) .setIndices(context.indices); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AggregationDataExtractorContext.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AggregationDataExtractorContext.java index bfcf4a8cbd98b..8b11a2e52980a 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AggregationDataExtractorContext.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AggregationDataExtractorContext.java @@ -30,9 +30,20 @@ class AggregationDataExtractorContext { final IndicesOptions indicesOptions; final Map runtimeMappings; - AggregationDataExtractorContext(String jobId, String timeField, Set fields, List indices, QueryBuilder query, - AggregatorFactories.Builder aggs, long start, long end, boolean includeDocCount, - Map headers, IndicesOptions indicesOptions, Map runtimeMappings) { + AggregationDataExtractorContext( + String jobId, + String timeField, + Set fields, + List indices, + QueryBuilder query, + AggregatorFactories.Builder aggs, + long start, + long end, + boolean includeDocCount, + Map headers, + IndicesOptions indicesOptions, + Map runtimeMappings + ) { this.jobId = Objects.requireNonNull(jobId); this.timeField = Objects.requireNonNull(timeField); this.fields = Objects.requireNonNull(fields); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AggregationDataExtractorFactory.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AggregationDataExtractorFactory.java index 3bc294be358aa..b18cba8b5d567 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AggregationDataExtractorFactory.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AggregationDataExtractorFactory.java @@ -28,25 +28,20 @@ public class AggregationDataExtractorFactory implements DataExtractorFactory { private final NamedXContentRegistry xContentRegistry; private final DatafeedTimingStatsReporter timingStatsReporter; - public static AggregatedSearchRequestBuilder requestBuilder( - Client client, - String[] indices, - IndicesOptions indicesOptions - ) { - return (searchSourceBuilder) -> - new SearchRequestBuilder(client, SearchAction.INSTANCE) - .setSource(searchSourceBuilder) - .setIndicesOptions(indicesOptions) - .setAllowPartialSearchResults(false) - .setIndices(indices); + public static AggregatedSearchRequestBuilder requestBuilder(Client client, String[] indices, IndicesOptions indicesOptions) { + return (searchSourceBuilder) -> new SearchRequestBuilder(client, SearchAction.INSTANCE).setSource(searchSourceBuilder) + .setIndicesOptions(indicesOptions) + .setAllowPartialSearchResults(false) + .setIndices(indices); } public AggregationDataExtractorFactory( - Client client, - DatafeedConfig datafeedConfig, - Job job, - NamedXContentRegistry xContentRegistry, - DatafeedTimingStatsReporter timingStatsReporter) { + Client client, + DatafeedConfig datafeedConfig, + Job job, + NamedXContentRegistry xContentRegistry, + DatafeedTimingStatsReporter timingStatsReporter + ) { this.client = Objects.requireNonNull(client); this.datafeedConfig = Objects.requireNonNull(datafeedConfig); this.job = Objects.requireNonNull(job); @@ -58,18 +53,19 @@ public AggregationDataExtractorFactory( public DataExtractor newExtractor(long start, long end) { long histogramInterval = datafeedConfig.getHistogramIntervalMillis(xContentRegistry); AggregationDataExtractorContext dataExtractorContext = new AggregationDataExtractorContext( - job.getId(), - job.getDataDescription().getTimeField(), - job.getAnalysisConfig().analysisFields(), - datafeedConfig.getIndices(), - datafeedConfig.getParsedQuery(xContentRegistry), - datafeedConfig.getParsedAggregations(xContentRegistry), - Intervals.alignToCeil(start, histogramInterval), - Intervals.alignToFloor(end, histogramInterval), - job.getAnalysisConfig().getSummaryCountFieldName().equals(DatafeedConfig.DOC_COUNT), - datafeedConfig.getHeaders(), - datafeedConfig.getIndicesOptions(), - datafeedConfig.getRuntimeMappings()); + job.getId(), + job.getDataDescription().getTimeField(), + job.getAnalysisConfig().analysisFields(), + datafeedConfig.getIndices(), + datafeedConfig.getParsedQuery(xContentRegistry), + datafeedConfig.getParsedAggregations(xContentRegistry), + Intervals.alignToCeil(start, histogramInterval), + Intervals.alignToFloor(end, histogramInterval), + job.getAnalysisConfig().getSummaryCountFieldName().equals(DatafeedConfig.DOC_COUNT), + datafeedConfig.getHeaders(), + datafeedConfig.getIndicesOptions(), + datafeedConfig.getRuntimeMappings() + ); return new AggregationDataExtractor(client, dataExtractorContext, timingStatsReporter); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AggregationToJsonProcessor.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AggregationToJsonProcessor.java index f3a73005a8433..f05fa5247f2fa 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AggregationToJsonProcessor.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AggregationToJsonProcessor.java @@ -9,10 +9,8 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.message.ParameterizedMessage; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.util.set.Sets; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.json.JsonXContent; +import org.elasticsearch.core.Nullable; import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.Aggregations; import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation; @@ -24,6 +22,8 @@ import org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregation; import org.elasticsearch.search.aggregations.metrics.Percentile; import org.elasticsearch.search.aggregations.metrics.Percentiles; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfig; import org.elasticsearch.xpack.core.ml.job.messages.Messages; @@ -71,11 +71,13 @@ class AggregationToJsonProcessor { * @param startTime buckets with a timestamp before this time are discarded * @param compositeAggDateValueSourceName the value source for the date_histogram source in the composite agg, if it exists */ - AggregationToJsonProcessor(String timeField, - Set fields, - boolean includeDocCount, - long startTime, - @Nullable String compositeAggDateValueSourceName) { + AggregationToJsonProcessor( + String timeField, + Set fields, + boolean includeDocCount, + long startTime, + @Nullable String compositeAggDateValueSourceName + ) { this.timeField = Objects.requireNonNull(timeField); this.fields = Objects.requireNonNull(fields); this.includeDocCount = includeDocCount; @@ -114,11 +116,11 @@ private void processAggs(long docCount, List aggregations) throws I // The leaf aggregations will be processed first. for (Aggregation agg : aggregations) { if (agg instanceof MultiBucketsAggregation) { - bucketAggregations.add((MultiBucketsAggregation)agg); - } else if (agg instanceof SingleBucketAggregation){ + bucketAggregations.add((MultiBucketsAggregation) agg); + } else if (agg instanceof SingleBucketAggregation) { // Skip a level down for single bucket aggs, if they have a sub-agg that is not // a bucketed agg we should treat it like a leaf in this bucket - SingleBucketAggregation singleBucketAggregation = (SingleBucketAggregation)agg; + SingleBucketAggregation singleBucketAggregation = (SingleBucketAggregation) agg; for (Aggregation subAgg : singleBucketAggregation.getAggregations()) { if (subAgg instanceof MultiBucketsAggregation || subAgg instanceof SingleBucketAggregation) { singleBucketAggregations.add(singleBucketAggregation); @@ -135,10 +137,13 @@ private void processAggs(long docCount, List aggregations) throws I // we have more than 1 `MultiBucketsAggregation`, we should error out. // We need to make the check in this way as each of the items in `singleBucketAggregations` is treated as a separate branch // in the recursive handling of this method. - int bucketAggLevelCount = Math.max(bucketAggregations.size(), (int)singleBucketAggregations.stream() - .flatMap(s -> asList(s.getAggregations()).stream()) - .filter(MultiBucketsAggregation.class::isInstance) - .count()); + int bucketAggLevelCount = Math.max( + bucketAggregations.size(), + (int) singleBucketAggregations.stream() + .flatMap(s -> asList(s.getAggregations()).stream()) + .filter(MultiBucketsAggregation.class::isInstance) + .count() + ); if (bucketAggLevelCount > 1) { throw new IllegalArgumentException("Multiple bucket aggregations at the same level are not supported"); @@ -181,12 +186,14 @@ private void processAggs(long docCount, List aggregations) throws I // However, we only want to recurse with multi/single bucket aggs. // Non-bucketed sub-aggregations were handle as leaf aggregations at this level for (SingleBucketAggregation singleBucketAggregation : singleBucketAggregations) { - processAggs(singleBucketAggregation.getDocCount(), - asList(singleBucketAggregation.getAggregations()) - .stream() + processAggs( + singleBucketAggregation.getDocCount(), + asList(singleBucketAggregation.getAggregations()).stream() .filter( - aggregation -> (aggregation instanceof MultiBucketsAggregation || aggregation instanceof SingleBucketAggregation)) - .collect(Collectors.toList())); + aggregation -> (aggregation instanceof MultiBucketsAggregation || aggregation instanceof SingleBucketAggregation) + ) + .collect(Collectors.toList()) + ); } // If there are no more bucket aggregations to process we've reached the end @@ -200,8 +207,12 @@ private void processAggs(long docCount, List aggregations) throws I private void processDateHistogram(Histogram agg) throws IOException { if (keyValuePairs.containsKey(timeField)) { - throw new IllegalArgumentException("More than one composite or date_histogram cannot be used in the aggregation. " + - "[" + agg.getName() + "] is another instance of a composite or date_histogram aggregation"); + throw new IllegalArgumentException( + "More than one composite or date_histogram cannot be used in the aggregation. " + + "[" + + agg.getName() + + "] is another instance of a composite or date_histogram aggregation" + ); } // buckets are ordered by time, once we get to a bucket past the @@ -227,14 +238,18 @@ private void processDateHistogram(Histogram agg) throws IOException { private void processCompositeAgg(CompositeAggregation agg) throws IOException { if (keyValuePairs.containsKey(timeField)) { - throw new IllegalArgumentException("More than one composite or date_histogram cannot be used in the aggregation. " + - "[" + agg.getName() + "] is another instance of a composite or date_histogram aggregation"); + throw new IllegalArgumentException( + "More than one composite or date_histogram cannot be used in the aggregation. " + + "[" + + agg.getName() + + "] is another instance of a composite or date_histogram aggregation" + ); } // Shouldn't ever happen if (compositeAggDateValueSourceName == null) { - throw new IllegalArgumentException("attempted to process composite agg [" - + agg.getName() - + "] but does not contain date_histogram value source"); + throw new IllegalArgumentException( + "attempted to process composite agg [" + agg.getName() + "] but does not contain date_histogram value source" + ); } // Composite aggs have multiple items in the bucket. It is possible that within the current @@ -287,11 +302,11 @@ private Collection processCompositeAggBucketKeys(Map buc */ private long toHistogramKeyToEpoch(Object key) { if (key instanceof ZonedDateTime) { - return ((ZonedDateTime)key).toInstant().toEpochMilli(); + return ((ZonedDateTime) key).toInstant().toEpochMilli(); } else if (key instanceof Double) { - return ((Double)key).longValue(); + return ((Double) key).longValue(); } else if (key instanceof Long) { - return (Long)key; + return (Long) key; } else { throw new IllegalStateException("Histogram key [" + key + "] cannot be converted to a timestamp"); } @@ -346,7 +361,7 @@ private void processBucket(MultiBucketsAggregation bucketAgg, boolean addField) keyValuePairs.put(bucketAgg.getName(), bucket.getKey()); } if (bucket instanceof CompositeAggregation.Bucket) { - addedFields.addAll(processCompositeAggBucketKeys(((CompositeAggregation.Bucket)bucket).getKey())); + addedFields.addAll(processCompositeAggBucketKeys(((CompositeAggregation.Bucket) bucket).getKey())); } processAggs(bucket.getDocCount(), asList(bucket.getAggregations())); for (String fieldName : addedFields) { @@ -364,7 +379,7 @@ private boolean processLeaf(Aggregation agg) { return processSingleValue((NumericMetricsAggregation.SingleValue) agg); } else if (agg instanceof Percentiles) { return processPercentiles((Percentiles) agg); - } else if (agg instanceof GeoCentroid){ + } else if (agg instanceof GeoCentroid) { return processGeoCentroid((GeoCentroid) agg); } else { throw new IllegalArgumentException("Unsupported aggregation type [" + agg.getName() + "]"); @@ -410,7 +425,8 @@ private void queueDocToWrite(Map doc, long docCount) { Long timeStamp = (Long) copy.get(timeField); if (timeStamp == null) { throw new IllegalArgumentException( - Messages.getMessage(Messages.DATAFEED_MISSING_MAX_AGGREGATION_FOR_TIME_FIELD, timeField)); + Messages.getMessage(Messages.DATAFEED_MISSING_MAX_AGGREGATION_FOR_TIME_FIELD, timeField) + ); } docsByBucketTimestamp.computeIfAbsent(timeStamp, (t) -> new ArrayList<>()).add(copy); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/CompositeAggregationDataExtractor.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/CompositeAggregationDataExtractor.java index 990b441a1df57..9e8c677617792 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/CompositeAggregationDataExtractor.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/CompositeAggregationDataExtractor.java @@ -123,8 +123,7 @@ private Aggregations search() { context.end ) ); - SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder() - .size(0) + SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().size(0) .query(ExtractorUtils.wrapInTimeRangeQuery(context.query, context.timeField, context.start, context.end)); if (context.runtimeMappings.isEmpty() == false) { @@ -161,42 +160,45 @@ private InputStream processAggs(Aggregations aggs) throws IOException { context.start, context.compositeAggDateHistogramGroupSourceName ); - LOGGER.trace(() -> new ParameterizedMessage( - "[{}] got [{}] composite buckets", - context.jobId, - ((CompositeAggregation)aggs.get(compositeAggregationBuilder.getName())).getBuckets().size() - )); + LOGGER.trace( + () -> new ParameterizedMessage( + "[{}] got [{}] composite buckets", + context.jobId, + ((CompositeAggregation) aggs.get(compositeAggregationBuilder.getName())).getBuckets().size() + ) + ); aggregationToJsonProcessor.process(aggs); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - final Long afterKeyTimeBucket = afterKey != null ? (Long)afterKey.get(context.compositeAggDateHistogramGroupSourceName) : null ; - boolean cancellable = aggregationToJsonProcessor.writeAllDocsCancellable( - timestamp -> { - if (isCancelled) { - // If we have not processed a single composite agg page yet and we are cancelled - // We should not process anything - if (afterKeyTimeBucket == null) { - return true; - } - // We want to stop processing once a timestamp enters the next time bucket. - // This could occur in any page. One benefit we have is that even though the paging order is not sorted - // by max timestamp, our iteration of the page results is. So, once we cross over to the next bucket within - // a given page, we know the previous bucket has been exhausted. - if (nextBucketOnCancel == 0L) { - // This simple equation handles two unique scenarios: - // If the timestamp is the current floor, this means we need to keep processing until the next timebucket - // If we are not matching the current bucket floor, then this simply aligns to the next bucket - nextBucketOnCancel = Intervals.alignToFloor(timestamp + interval, interval); - LOGGER.debug(() -> new ParameterizedMessage( + final Long afterKeyTimeBucket = afterKey != null ? (Long) afterKey.get(context.compositeAggDateHistogramGroupSourceName) : null; + boolean cancellable = aggregationToJsonProcessor.writeAllDocsCancellable(timestamp -> { + if (isCancelled) { + // If we have not processed a single composite agg page yet and we are cancelled + // We should not process anything + if (afterKeyTimeBucket == null) { + return true; + } + // We want to stop processing once a timestamp enters the next time bucket. + // This could occur in any page. One benefit we have is that even though the paging order is not sorted + // by max timestamp, our iteration of the page results is. So, once we cross over to the next bucket within + // a given page, we know the previous bucket has been exhausted. + if (nextBucketOnCancel == 0L) { + // This simple equation handles two unique scenarios: + // If the timestamp is the current floor, this means we need to keep processing until the next timebucket + // If we are not matching the current bucket floor, then this simply aligns to the next bucket + nextBucketOnCancel = Intervals.alignToFloor(timestamp + interval, interval); + LOGGER.debug( + () -> new ParameterizedMessage( "[{}] set future timestamp cancel to [{}] via timestamp [{}]", context.jobId, nextBucketOnCancel, timestamp - )); - } - return timestamp >= nextBucketOnCancel; + ) + ); } - return false; - }, outputStream); + return timestamp >= nextBucketOnCancel; + } + return false; + }, outputStream); // If the process is canceled and cancelable, then we can indicate that there are no more buckets to process. if (isCancelled && cancellable) { LOGGER.debug( diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/CompositeAggregationDataExtractorContext.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/CompositeAggregationDataExtractorContext.java index 9650fd949526b..5fd5b58c5556d 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/CompositeAggregationDataExtractorContext.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/CompositeAggregationDataExtractorContext.java @@ -31,19 +31,21 @@ class CompositeAggregationDataExtractorContext { final Map runtimeMappings; final String compositeAggDateHistogramGroupSourceName; - CompositeAggregationDataExtractorContext(String jobId, - String timeField, - Set fields, - List indices, - QueryBuilder query, - CompositeAggregationBuilder compositeAggregationBuilder, - String compositeAggDateHistogramGroupSourceName, - long start, - long end, - boolean includeDocCount, - Map headers, - IndicesOptions indicesOptions, - Map runtimeMappings) { + CompositeAggregationDataExtractorContext( + String jobId, + String timeField, + Set fields, + List indices, + QueryBuilder query, + CompositeAggregationBuilder compositeAggregationBuilder, + String compositeAggDateHistogramGroupSourceName, + long start, + long end, + boolean includeDocCount, + Map headers, + IndicesOptions indicesOptions, + Map runtimeMappings + ) { this.jobId = Objects.requireNonNull(jobId); this.timeField = Objects.requireNonNull(timeField); this.fields = Objects.requireNonNull(fields); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/CompositeAggregationDataExtractorFactory.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/CompositeAggregationDataExtractorFactory.java index df86b8b0f498d..a1337b8e69ab8 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/CompositeAggregationDataExtractorFactory.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/CompositeAggregationDataExtractorFactory.java @@ -7,13 +7,13 @@ package org.elasticsearch.xpack.ml.datafeed.extractor.aggregation; import org.elasticsearch.client.Client; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.search.aggregations.AggregationBuilder; import org.elasticsearch.search.aggregations.PipelineAggregationBuilder; import org.elasticsearch.search.aggregations.bucket.composite.CompositeAggregationBuilder; import org.elasticsearch.search.aggregations.bucket.composite.CompositeValuesSourceBuilder; import org.elasticsearch.search.aggregations.bucket.composite.DateHistogramValuesSourceBuilder; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfig; import org.elasticsearch.xpack.core.ml.datafeed.extractor.DataExtractor; import org.elasticsearch.xpack.core.ml.datafeed.extractor.ExtractorUtils; @@ -43,12 +43,12 @@ public class CompositeAggregationDataExtractorFactory implements DataExtractorFa private final QueryBuilder parsedQuery; public CompositeAggregationDataExtractorFactory( - Client client, - DatafeedConfig datafeedConfig, - Job job, - NamedXContentRegistry xContentRegistry, - DatafeedTimingStatsReporter timingStatsReporter, - AggregatedSearchRequestBuilder requestBuilder + Client client, + DatafeedConfig datafeedConfig, + Job job, + NamedXContentRegistry xContentRegistry, + DatafeedTimingStatsReporter timingStatsReporter, + AggregatedSearchRequestBuilder requestBuilder ) { this.client = Objects.requireNonNull(client); this.datafeedConfig = Objects.requireNonNull(datafeedConfig); @@ -100,19 +100,20 @@ public DataExtractor newExtractor(long start, long end) { subPipelineAggs.forEach(compositeAggregationBuilder::subAggregation); long histogramInterval = ExtractorUtils.getHistogramIntervalMillis(compositeAggregationBuilder); CompositeAggregationDataExtractorContext dataExtractorContext = new CompositeAggregationDataExtractorContext( - job.getId(), - job.getDataDescription().getTimeField(), - job.getAnalysisConfig().analysisFields(), - datafeedConfig.getIndices(), - parsedQuery, - compositeAggregationBuilder, - this.dateHistogramGroupSourceName, - Intervals.alignToCeil(start, histogramInterval), - Intervals.alignToFloor(end, histogramInterval), - job.getAnalysisConfig().getSummaryCountFieldName().equals(DatafeedConfig.DOC_COUNT), - datafeedConfig.getHeaders(), - datafeedConfig.getIndicesOptions(), - datafeedConfig.getRuntimeMappings()); + job.getId(), + job.getDataDescription().getTimeField(), + job.getAnalysisConfig().analysisFields(), + datafeedConfig.getIndices(), + parsedQuery, + compositeAggregationBuilder, + this.dateHistogramGroupSourceName, + Intervals.alignToCeil(start, histogramInterval), + Intervals.alignToFloor(end, histogramInterval), + job.getAnalysisConfig().getSummaryCountFieldName().equals(DatafeedConfig.DOC_COUNT), + datafeedConfig.getHeaders(), + datafeedConfig.getIndicesOptions(), + datafeedConfig.getRuntimeMappings() + ); return new CompositeAggregationDataExtractor( compositeAggregationBuilder, client, diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/RollupDataExtractor.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/RollupDataExtractor.java index 4dd00950792e7..a183b89d2bdba 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/RollupDataExtractor.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/RollupDataExtractor.java @@ -21,7 +21,10 @@ class RollupDataExtractor extends AbstractAggregationDataExtractor { RollupDataExtractor( - Client client, AggregationDataExtractorContext dataExtractorContext, DatafeedTimingStatsReporter timingStatsReporter) { + Client client, + AggregationDataExtractorContext dataExtractorContext, + DatafeedTimingStatsReporter timingStatsReporter + ) { super(client, dataExtractorContext, timingStatsReporter); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/RollupDataExtractorFactory.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/RollupDataExtractorFactory.java index e97e6ef896362..20b48a485cd2a 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/RollupDataExtractorFactory.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/RollupDataExtractorFactory.java @@ -11,12 +11,12 @@ import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.client.Client; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.search.aggregations.AggregationBuilder; import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder; import org.elasticsearch.search.aggregations.bucket.histogram.HistogramAggregationBuilder; import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfig; import org.elasticsearch.xpack.core.ml.datafeed.extractor.DataExtractor; import org.elasticsearch.xpack.core.ml.job.config.Job; @@ -53,11 +53,12 @@ public class RollupDataExtractorFactory implements DataExtractorFactory { private final DatafeedTimingStatsReporter timingStatsReporter; private RollupDataExtractorFactory( - Client client, - DatafeedConfig datafeedConfig, - Job job, - NamedXContentRegistry xContentRegistry, - DatafeedTimingStatsReporter timingStatsReporter) { + Client client, + DatafeedConfig datafeedConfig, + Job job, + NamedXContentRegistry xContentRegistry, + DatafeedTimingStatsReporter timingStatsReporter + ) { this.client = Objects.requireNonNull(client); this.datafeedConfig = Objects.requireNonNull(datafeedConfig); this.job = Objects.requireNonNull(job); @@ -65,11 +66,7 @@ private RollupDataExtractorFactory( this.timingStatsReporter = Objects.requireNonNull(timingStatsReporter); } - public static AggregatedSearchRequestBuilder requestBuilder( - Client client, - String[] indices, - IndicesOptions indicesOptions - ) { + public static AggregatedSearchRequestBuilder requestBuilder(Client client, String[] indices, IndicesOptions indicesOptions) { return (searchSourceBuilder) -> { SearchRequest searchRequest = new SearchRequest().indices(indices) .indicesOptions(indicesOptions) @@ -94,24 +91,31 @@ public DataExtractor newExtractor(long start, long end) { job.getAnalysisConfig().getSummaryCountFieldName().equals(DatafeedConfig.DOC_COUNT), datafeedConfig.getHeaders(), datafeedConfig.getIndicesOptions(), - datafeedConfig.getRuntimeMappings()); + datafeedConfig.getRuntimeMappings() + ); return new RollupDataExtractor(client, dataExtractorContext, timingStatsReporter); } - public static void create(Client client, - DatafeedConfig datafeed, - Job job, - Map rollupJobsWithCaps, - NamedXContentRegistry xContentRegistry, - DatafeedTimingStatsReporter timingStatsReporter, - ActionListener listener) { + public static void create( + Client client, + DatafeedConfig datafeed, + Job job, + Map rollupJobsWithCaps, + NamedXContentRegistry xContentRegistry, + DatafeedTimingStatsReporter timingStatsReporter, + ActionListener listener + ) { final AggregationBuilder datafeedHistogramAggregation = getHistogramAggregation( - datafeed.getParsedAggregations(xContentRegistry).getAggregatorFactories()); + datafeed.getParsedAggregations(xContentRegistry).getAggregatorFactories() + ); if ((datafeedHistogramAggregation instanceof DateHistogramAggregationBuilder) == false) { listener.onFailure( - new IllegalArgumentException("Rollup requires that the datafeed configuration use a [date_histogram] aggregation," + - " not a [histogram] aggregation over the time field.")); + new IllegalArgumentException( + "Rollup requires that the datafeed configuration use a [date_histogram] aggregation," + + " not a [histogram] aggregation over the time field." + ) + ); return; } @@ -132,14 +136,18 @@ public static void create(Client client, if (validIntervalCaps.isEmpty()) { listener.onFailure( new IllegalArgumentException( - "Rollup capabilities do not have a [date_histogram] aggregation with an interval " + - "that is a multiple of the datafeed's interval.") + "Rollup capabilities do not have a [date_histogram] aggregation with an interval " + + "that is a multiple of the datafeed's interval." + ) ); return; } final List> flattenedAggs = new ArrayList<>(); - flattenAggregations(datafeed.getParsedAggregations(xContentRegistry) - .getAggregatorFactories(), datafeedHistogramAggregation, flattenedAggs); + flattenAggregations( + datafeed.getParsedAggregations(xContentRegistry).getAggregatorFactories(), + datafeedHistogramAggregation, + flattenedAggs + ); if (validIntervalCaps.stream().noneMatch(rollupJobConfig -> hasAggregations(rollupJobConfig, flattenedAggs))) { listener.onFailure( @@ -166,12 +174,14 @@ private static boolean validInterval(long datafeedInterval, ParsedRollupCaps rol } } - private static void flattenAggregations(final Collection datafeedAggregations, - final AggregationBuilder datafeedHistogramAggregation, - final List> flattenedAggregations) { + private static void flattenAggregations( + final Collection datafeedAggregations, + final AggregationBuilder datafeedHistogramAggregation, + final List> flattenedAggregations + ) { for (AggregationBuilder aggregationBuilder : datafeedAggregations) { if (aggregationBuilder.equals(datafeedHistogramAggregation) == false) { - flattenedAggregations.add((ValuesSourceAggregationBuilder)aggregationBuilder); + flattenedAggregations.add((ValuesSourceAggregationBuilder) aggregationBuilder); } flattenAggregations(aggregationBuilder.getSubAggregations(), datafeedHistogramAggregation, flattenedAggregations); } @@ -198,14 +208,16 @@ private static class ParsedRollupCaps { private final Set supportedMetrics; private final Set supportedTerms; private final Map datehistogramAgg; - private static final List aggsToIgnore = - Arrays.asList(HistogramAggregationBuilder.NAME, DateHistogramAggregationBuilder.NAME); + private static final List aggsToIgnore = Arrays.asList( + HistogramAggregationBuilder.NAME, + DateHistogramAggregationBuilder.NAME + ); private static ParsedRollupCaps fromJobFieldCaps(Map rollupFieldCaps, String timeField) { Map datehistogram = null; RollupFieldCaps timeFieldCaps = rollupFieldCaps.get(timeField); if (timeFieldCaps != null) { - for(Map agg : timeFieldCaps.getAggs()) { + for (Map agg : timeFieldCaps.getAggs()) { if (agg.get("agg").equals(DateHistogramAggregationBuilder.NAME)) { datehistogram = agg; } @@ -215,7 +227,7 @@ private static ParsedRollupCaps fromJobFieldCaps(Map ro Set supportedTerms = new HashSet<>(); rollupFieldCaps.forEach((field, fieldCaps) -> { fieldCaps.getAggs().forEach(agg -> { - String type = (String)agg.get("agg"); + String type = (String) agg.get("agg"); if (type.equals(TermsAggregationBuilder.NAME)) { supportedTerms.add(field); } else if (aggsToIgnore.contains(type) == false) { @@ -237,13 +249,13 @@ private String getInterval() { return null; } if (datehistogramAgg.get(DateHistogramGroupConfig.INTERVAL) != null) { - return (String)datehistogramAgg.get(DateHistogramGroupConfig.INTERVAL); + return (String) datehistogramAgg.get(DateHistogramGroupConfig.INTERVAL); } if (datehistogramAgg.get(DateHistogramGroupConfig.CALENDAR_INTERVAL) != null) { - return (String)datehistogramAgg.get(DateHistogramGroupConfig.CALENDAR_INTERVAL); + return (String) datehistogramAgg.get(DateHistogramGroupConfig.CALENDAR_INTERVAL); } if (datehistogramAgg.get(DateHistogramGroupConfig.FIXED_INTERVAL) != null) { - return (String)datehistogramAgg.get(DateHistogramGroupConfig.FIXED_INTERVAL); + return (String) datehistogramAgg.get(DateHistogramGroupConfig.FIXED_INTERVAL); } return null; } @@ -252,7 +264,7 @@ private String getTimezone() { if (datehistogramAgg == null) { return null; } - return (String)datehistogramAgg.get(DateHistogramGroupConfig.TIME_ZONE); + return (String) datehistogramAgg.get(DateHistogramGroupConfig.TIME_ZONE); } private boolean hasDatehistogram() { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/chunked/ChunkedDataExtractor.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/chunked/ChunkedDataExtractor.java index 523d8de0db3a1..6248461cd588e 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/chunked/ChunkedDataExtractor.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/chunked/ChunkedDataExtractor.java @@ -53,8 +53,11 @@ public class ChunkedDataExtractor implements DataExtractor { interface DataSummary { long estimateChunk(); + boolean hasData(); + long earliestTime(); + long getDataTimeSpread(); } @@ -78,10 +81,11 @@ interface DataSummary { private DataExtractor currentExtractor; public ChunkedDataExtractor( - Client client, - DataExtractorFactory dataExtractorFactory, - ChunkedDataExtractorContext context, - DatafeedTimingStatsReporter timingStatsReporter) { + Client client, + DataExtractorFactory dataExtractorFactory, + ChunkedDataExtractorContext context, + DatafeedTimingStatsReporter timingStatsReporter + ) { this.client = Objects.requireNonNull(client); this.dataExtractorFactory = Objects.requireNonNull(dataExtractorFactory); this.context = Objects.requireNonNull(context); @@ -98,7 +102,7 @@ public boolean hasNext() { if (isCancelled()) { return currentHasNext; } - return currentHasNext || currentEnd < context.end; + return currentHasNext || currentEnd < context.end; } @Override @@ -122,8 +126,13 @@ private void setUpChunkedSearch() { currentEnd = currentStart; chunkSpan = context.chunkSpan == null ? dataSummary.estimateChunk() : context.chunkSpan.getMillis(); chunkSpan = context.timeAligner.alignToCeil(chunkSpan); - LOGGER.debug("[{}] Chunked search configured: kind = {}, dataTimeSpread = {} ms, chunk span = {} ms", - context.jobId, dataSummary.getClass().getSimpleName(), dataSummary.getDataTimeSpread(), chunkSpan); + LOGGER.debug( + "[{}] Chunked search configured: kind = {}, dataTimeSpread = {} ms, chunk span = {} ms", + context.jobId, + dataSummary.getClass().getSimpleName(), + dataSummary.getDataTimeSpread(), + chunkSpan + ); } else { // search is over currentEnd = context.end; @@ -244,8 +253,7 @@ private DataSummary newAggregatedDataSummary() { } private SearchSourceBuilder rangeSearchBuilder() { - return new SearchSourceBuilder() - .size(0) + return new SearchSourceBuilder().size(0) .query(ExtractorUtils.wrapInTimeRangeQuery(context.query, context.timeField, currentStart, context.end)) .runtimeMappings(context.runtimeMappings) .aggregation(AggregationBuilders.min(EARLIEST_TIME).field(context.timeField)) @@ -253,8 +261,7 @@ private SearchSourceBuilder rangeSearchBuilder() { } private SearchRequestBuilder rangeSearchRequest() { - return new SearchRequestBuilder(client, SearchAction.INSTANCE) - .setIndices(context.indices) + return new SearchRequestBuilder(client, SearchAction.INSTANCE).setIndices(context.indices) .setIndicesOptions(context.indicesOptions) .setSource(rangeSearchBuilder()) .setAllowPartialSearchResults(false) @@ -348,12 +355,12 @@ public boolean hasData() { @Override public long earliestTime() { - return (long)earliestTime; + return (long) earliestTime; } @Override public long getDataTimeSpread() { - return (long)latestTime - (long)earliestTime; + return (long) latestTime - (long) earliestTime; } } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/chunked/ChunkedDataExtractorContext.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/chunked/ChunkedDataExtractorContext.java index e93b943278572..2989ddb40d370 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/chunked/ChunkedDataExtractorContext.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/chunked/ChunkedDataExtractorContext.java @@ -19,6 +19,7 @@ class ChunkedDataExtractorContext { interface TimeAligner { long alignToFloor(long value); + long alignToCeil(long value); } @@ -37,10 +38,22 @@ interface TimeAligner { final IndicesOptions indicesOptions; final Map runtimeMappings; - ChunkedDataExtractorContext(String jobId, String timeField, List indices, QueryBuilder query, int scrollSize, long start, - long end, @Nullable TimeValue chunkSpan, TimeAligner timeAligner, Map headers, - boolean hasAggregations, @Nullable Long histogramInterval, IndicesOptions indicesOptions, - Map runtimeMappings) { + ChunkedDataExtractorContext( + String jobId, + String timeField, + List indices, + QueryBuilder query, + int scrollSize, + long start, + long end, + @Nullable TimeValue chunkSpan, + TimeAligner timeAligner, + Map headers, + boolean hasAggregations, + @Nullable Long histogramInterval, + IndicesOptions indicesOptions, + Map runtimeMappings + ) { this.jobId = Objects.requireNonNull(jobId); this.timeField = Objects.requireNonNull(timeField); this.indices = indices.toArray(new String[indices.size()]); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/chunked/ChunkedDataExtractorFactory.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/chunked/ChunkedDataExtractorFactory.java index 85a2e6bb78fae..aad344881b359 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/chunked/ChunkedDataExtractorFactory.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/chunked/ChunkedDataExtractorFactory.java @@ -10,10 +10,10 @@ import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfig; import org.elasticsearch.xpack.core.ml.datafeed.extractor.DataExtractor; -import org.elasticsearch.xpack.ml.datafeed.DatafeedTimingStatsReporter; -import org.elasticsearch.xpack.ml.datafeed.extractor.DataExtractorFactory; import org.elasticsearch.xpack.core.ml.job.config.Job; import org.elasticsearch.xpack.core.ml.utils.Intervals; +import org.elasticsearch.xpack.ml.datafeed.DatafeedTimingStatsReporter; +import org.elasticsearch.xpack.ml.datafeed.extractor.DataExtractorFactory; import java.util.Objects; @@ -26,12 +26,14 @@ public class ChunkedDataExtractorFactory implements DataExtractorFactory { private final NamedXContentRegistry xContentRegistry; private final DatafeedTimingStatsReporter timingStatsReporter; - public ChunkedDataExtractorFactory(Client client, - DatafeedConfig datafeedConfig, - Job job, - NamedXContentRegistry xContentRegistry, - DataExtractorFactory dataExtractorFactory, - DatafeedTimingStatsReporter timingStatsReporter) { + public ChunkedDataExtractorFactory( + Client client, + DatafeedConfig datafeedConfig, + Job job, + NamedXContentRegistry xContentRegistry, + DataExtractorFactory dataExtractorFactory, + DatafeedTimingStatsReporter timingStatsReporter + ) { this.client = Objects.requireNonNull(client); this.datafeedConfig = Objects.requireNonNull(datafeedConfig); this.job = Objects.requireNonNull(job); @@ -44,21 +46,21 @@ public ChunkedDataExtractorFactory(Client client, public DataExtractor newExtractor(long start, long end) { ChunkedDataExtractorContext.TimeAligner timeAligner = newTimeAligner(); ChunkedDataExtractorContext dataExtractorContext = new ChunkedDataExtractorContext( - job.getId(), - job.getDataDescription().getTimeField(), - datafeedConfig.getIndices(), - datafeedConfig.getParsedQuery(xContentRegistry), - datafeedConfig.getScrollSize(), - timeAligner.alignToCeil(start), - timeAligner.alignToFloor(end), - datafeedConfig.getChunkingConfig().getTimeSpan(), - timeAligner, - datafeedConfig.getHeaders(), - datafeedConfig.hasAggregations(), - datafeedConfig.hasAggregations() ? datafeedConfig.getHistogramIntervalMillis(xContentRegistry) : null, - datafeedConfig.getIndicesOptions(), - datafeedConfig.getRuntimeMappings() - ); + job.getId(), + job.getDataDescription().getTimeField(), + datafeedConfig.getIndices(), + datafeedConfig.getParsedQuery(xContentRegistry), + datafeedConfig.getScrollSize(), + timeAligner.alignToCeil(start), + timeAligner.alignToFloor(end), + datafeedConfig.getChunkingConfig().getTimeSpan(), + timeAligner, + datafeedConfig.getHeaders(), + datafeedConfig.hasAggregations(), + datafeedConfig.hasAggregations() ? datafeedConfig.getHistogramIntervalMillis(xContentRegistry) : null, + datafeedConfig.getIndicesOptions(), + datafeedConfig.getRuntimeMappings() + ); return new ChunkedDataExtractor(client, dataExtractorFactory, dataExtractorContext, timingStatsReporter); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/ScrollDataExtractor.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/ScrollDataExtractor.java index 28597cb2e47b9..df711b88926d5 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/ScrollDataExtractor.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/ScrollDataExtractor.java @@ -101,8 +101,7 @@ public Optional next() throws IOException { private Optional tryNextStream() throws IOException { try { - return scrollId == null ? - Optional.ofNullable(initScroll(context.start)) : Optional.ofNullable(continueScroll()); + return scrollId == null ? Optional.ofNullable(initScroll(context.start)) : Optional.ofNullable(continueScroll()); } catch (Exception e) { scrollId = null; if (searchHasShardFailure) { @@ -127,19 +126,16 @@ protected SearchResponse executeSearchRequest(SearchRequestBuilder searchRequest } private SearchRequestBuilder buildSearchRequest(long start) { - SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder() - .size(context.scrollSize) + SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().size(context.scrollSize) .sort(context.extractedFields.timeField(), SortOrder.ASC) - .query(ExtractorUtils.wrapInTimeRangeQuery( - context.query, context.extractedFields.timeField(), start, context.end)) + .query(ExtractorUtils.wrapInTimeRangeQuery(context.query, context.extractedFields.timeField(), start, context.end)) .runtimeMappings(context.runtimeMappings); - SearchRequestBuilder searchRequestBuilder = new SearchRequestBuilder(client, SearchAction.INSTANCE) - .setScroll(SCROLL_TIMEOUT) - .setIndices(context.indices) - .setIndicesOptions(context.indicesOptions) - .setAllowPartialSearchResults(false) - .setSource(searchSourceBuilder); + SearchRequestBuilder searchRequestBuilder = new SearchRequestBuilder(client, SearchAction.INSTANCE).setScroll(SCROLL_TIMEOUT) + .setIndices(context.indices) + .setIndicesOptions(context.indicesOptions) + .setAllowPartialSearchResults(false) + .setSource(searchSourceBuilder); for (ExtractedField docValueField : context.extractedFields.getDocValueFields()) { searchRequestBuilder.addDocValueField(docValueField.getSearchField(), docValueField.getDocValueFormat()); @@ -181,7 +177,7 @@ private InputStream processSearchResponse(SearchResponse searchResponse) throws } hitProcessor.process(hit); } - SearchHit lastHit = searchResponse.getHits().getHits()[searchResponse.getHits().getHits().length -1]; + SearchHit lastHit = searchResponse.getHits().getHits()[searchResponse.getHits().getHits().length - 1]; lastTimestamp = context.extractedFields.timeFieldValue(lastHit); } return new ByteArrayInputStream(outputStream.toByteArray()); @@ -191,13 +187,12 @@ private InputStream continueScroll() throws IOException { LOGGER.debug("[{}] Continuing scroll with id [{}]", context.jobId, scrollId); SearchResponse searchResponse; try { - searchResponse = executeSearchScrollRequest(scrollId); + searchResponse = executeSearchScrollRequest(scrollId); } catch (SearchPhaseExecutionException searchExecutionException) { if (searchHasShardFailure) { throw searchExecutionException; } - LOGGER.debug("[{}] search failed due to SearchPhaseExecutionException. Will attempt again with new scroll", - context.jobId); + LOGGER.debug("[{}] search failed due to SearchPhaseExecutionException. Will attempt again with new scroll", context.jobId); markScrollAsErrored(); searchResponse = executeSearchRequest(buildSearchRequest(lastTimestamp == null ? context.start : lastTimestamp)); } @@ -217,19 +212,24 @@ void markScrollAsErrored() { } protected SearchResponse executeSearchScrollRequest(String scrollId) { - return ClientHelper.executeWithHeaders(context.headers, ClientHelper.ML_ORIGIN, client, - () -> new SearchScrollRequestBuilder(client, SearchScrollAction.INSTANCE) - .setScroll(SCROLL_TIMEOUT) - .setScrollId(scrollId) - .get()); + return ClientHelper.executeWithHeaders( + context.headers, + ClientHelper.ML_ORIGIN, + client, + () -> new SearchScrollRequestBuilder(client, SearchScrollAction.INSTANCE).setScroll(SCROLL_TIMEOUT).setScrollId(scrollId).get() + ); } private void clearScroll() { if (scrollId != null) { ClearScrollRequest request = new ClearScrollRequest(); request.addScrollId(scrollId); - ClientHelper.executeWithHeaders(context.headers, ClientHelper.ML_ORIGIN, client, - () -> client.execute(ClearScrollAction.INSTANCE, request).actionGet()); + ClientHelper.executeWithHeaders( + context.headers, + ClientHelper.ML_ORIGIN, + client, + () -> client.execute(ClearScrollAction.INSTANCE, request).actionGet() + ); scrollId = null; } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/ScrollDataExtractorContext.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/ScrollDataExtractorContext.java index 058dedf62372d..58c0c5b485742 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/ScrollDataExtractorContext.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/ScrollDataExtractorContext.java @@ -28,9 +28,19 @@ class ScrollDataExtractorContext { final IndicesOptions indicesOptions; final Map runtimeMappings; - ScrollDataExtractorContext(String jobId, TimeBasedExtractedFields extractedFields, List indices, QueryBuilder query, - List scriptFields, int scrollSize, long start, long end, - Map headers, IndicesOptions indicesOptions, Map runtimeMappings) { + ScrollDataExtractorContext( + String jobId, + TimeBasedExtractedFields extractedFields, + List indices, + QueryBuilder query, + List scriptFields, + int scrollSize, + long start, + long end, + Map headers, + IndicesOptions indicesOptions, + Map runtimeMappings + ) { this.jobId = Objects.requireNonNull(jobId); this.extractedFields = Objects.requireNonNull(extractedFields); this.indices = indices.toArray(new String[indices.size()]); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/ScrollDataExtractorFactory.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/ScrollDataExtractorFactory.java index 5966486dd5c14..dc117ba5ffe04 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/ScrollDataExtractorFactory.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/ScrollDataExtractorFactory.java @@ -12,8 +12,8 @@ import org.elasticsearch.action.fieldcaps.FieldCapabilitiesRequest; import org.elasticsearch.action.fieldcaps.FieldCapabilitiesResponse; import org.elasticsearch.client.Client; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.index.IndexNotFoundException; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.ClientHelper; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfig; import org.elasticsearch.xpack.core.ml.datafeed.extractor.DataExtractor; @@ -34,8 +34,14 @@ public class ScrollDataExtractorFactory implements DataExtractorFactory { private final NamedXContentRegistry xContentRegistry; private final DatafeedTimingStatsReporter timingStatsReporter; - private ScrollDataExtractorFactory(Client client, DatafeedConfig datafeedConfig, Job job, TimeBasedExtractedFields extractedFields, - NamedXContentRegistry xContentRegistry, DatafeedTimingStatsReporter timingStatsReporter) { + private ScrollDataExtractorFactory( + Client client, + DatafeedConfig datafeedConfig, + Job job, + TimeBasedExtractedFields extractedFields, + NamedXContentRegistry xContentRegistry, + DatafeedTimingStatsReporter timingStatsReporter + ) { this.client = Objects.requireNonNull(client); this.datafeedConfig = Objects.requireNonNull(datafeedConfig); this.job = Objects.requireNonNull(job); @@ -47,47 +53,54 @@ private ScrollDataExtractorFactory(Client client, DatafeedConfig datafeedConfig, @Override public DataExtractor newExtractor(long start, long end) { ScrollDataExtractorContext dataExtractorContext = new ScrollDataExtractorContext( - job.getId(), - extractedFields, - datafeedConfig.getIndices(), - datafeedConfig.getParsedQuery(xContentRegistry), - datafeedConfig.getScriptFields(), - datafeedConfig.getScrollSize(), - start, - end, - datafeedConfig.getHeaders(), - datafeedConfig.getIndicesOptions(), - datafeedConfig.getRuntimeMappings() + job.getId(), + extractedFields, + datafeedConfig.getIndices(), + datafeedConfig.getParsedQuery(xContentRegistry), + datafeedConfig.getScriptFields(), + datafeedConfig.getScrollSize(), + start, + end, + datafeedConfig.getHeaders(), + datafeedConfig.getIndicesOptions(), + datafeedConfig.getRuntimeMappings() ); return new ScrollDataExtractor(client, dataExtractorContext, timingStatsReporter); } - public static void create(Client client, - DatafeedConfig datafeed, - Job job, - NamedXContentRegistry xContentRegistry, - DatafeedTimingStatsReporter timingStatsReporter, - ActionListener listener) { + public static void create( + Client client, + DatafeedConfig datafeed, + Job job, + NamedXContentRegistry xContentRegistry, + DatafeedTimingStatsReporter timingStatsReporter, + ActionListener listener + ) { // Step 2. Contruct the factory and notify listener - ActionListener fieldCapabilitiesHandler = ActionListener.wrap( - fieldCapabilitiesResponse -> { - TimeBasedExtractedFields extractedFields = TimeBasedExtractedFields.build(job, datafeed, fieldCapabilitiesResponse); - listener.onResponse( - new ScrollDataExtractorFactory(client, datafeed, job, extractedFields, xContentRegistry, timingStatsReporter)); - }, - e -> { - Throwable cause = ExceptionsHelper.unwrapCause(e); - if (cause instanceof IndexNotFoundException) { - listener.onFailure(new ResourceNotFoundException("datafeed [" + datafeed.getId() - + "] cannot retrieve data because index " + ((IndexNotFoundException) cause).getIndex() + " does not exist")); - } else if (e instanceof IllegalArgumentException) { - listener.onFailure(ExceptionsHelper.badRequestException("[" + datafeed.getId() + "] " + e.getMessage())); - } else { - listener.onFailure(e); - } + ActionListener fieldCapabilitiesHandler = ActionListener.wrap(fieldCapabilitiesResponse -> { + TimeBasedExtractedFields extractedFields = TimeBasedExtractedFields.build(job, datafeed, fieldCapabilitiesResponse); + listener.onResponse( + new ScrollDataExtractorFactory(client, datafeed, job, extractedFields, xContentRegistry, timingStatsReporter) + ); + }, e -> { + Throwable cause = ExceptionsHelper.unwrapCause(e); + if (cause instanceof IndexNotFoundException) { + listener.onFailure( + new ResourceNotFoundException( + "datafeed [" + + datafeed.getId() + + "] cannot retrieve data because index " + + ((IndexNotFoundException) cause).getIndex() + + " does not exist" + ) + ); + } else if (e instanceof IllegalArgumentException) { + listener.onFailure(ExceptionsHelper.badRequestException("[" + datafeed.getId() + "] " + e.getMessage())); + } else { + listener.onFailure(e); } - ); + }); // Step 1. Get field capabilities necessary to build the information of how to extract fields FieldCapabilitiesRequest fieldCapabilitiesRequest = new FieldCapabilitiesRequest(); @@ -104,7 +117,7 @@ public static void create(Client client, .filter(f -> runtimefields.contains(f) == false) .toArray(String[]::new); fieldCapabilitiesRequest.fields(requestFields); - ClientHelper. executeWithHeaders(datafeed.getHeaders(), ClientHelper.ML_ORIGIN, client, () -> { + ClientHelper.executeWithHeaders(datafeed.getHeaders(), ClientHelper.ML_ORIGIN, client, () -> { client.execute(FieldCapabilitiesAction.INSTANCE, fieldCapabilitiesRequest, fieldCapabilitiesHandler); // This response gets discarded - the listener handles the real response return null; diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/SearchHitToJsonProcessor.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/SearchHitToJsonProcessor.java index eccadc58cba0f..c2353d71a71da 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/SearchHitToJsonProcessor.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/SearchHitToJsonProcessor.java @@ -7,9 +7,9 @@ package org.elasticsearch.xpack.ml.datafeed.extractor.scroll; import org.elasticsearch.core.Releasable; +import org.elasticsearch.search.SearchHit; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.search.SearchHit; import org.elasticsearch.xpack.ml.extractor.ExtractedField; import org.elasticsearch.xpack.ml.extractor.ExtractedFields; diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/TimeBasedExtractedFields.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/TimeBasedExtractedFields.java index d873764da05fe..be9e339243bf0 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/TimeBasedExtractedFields.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/TimeBasedExtractedFields.java @@ -29,9 +29,7 @@ public class TimeBasedExtractedFields extends ExtractedFields { private final ExtractedField timeField; public TimeBasedExtractedFields(ExtractedField timeField, List allFields) { - super(allFields, - Collections.emptyList(), - Collections.emptyMap()); + super(allFields, Collections.emptyList(), Collections.emptyMap()); if (allFields.contains(timeField) == false) { throw new IllegalArgumentException("timeField should also be contained in allFields"); } @@ -45,8 +43,9 @@ public String timeField() { public Long timeFieldValue(SearchHit hit) { Object[] value = timeField.value(hit); if (value.length != 1) { - throw new RuntimeException("Time field [" + timeField.getName() + "] expected a single value; actual was: " - + Arrays.toString(value)); + throw new RuntimeException( + "Time field [" + timeField.getName() + "] expected a single value; actual was: " + Arrays.toString(value) + ); } if (value[0] instanceof Long) { return (Long) value[0]; @@ -58,8 +57,11 @@ public static TimeBasedExtractedFields build(Job job, DatafeedConfig datafeed, F Set scriptFields = datafeed.getScriptFields().stream().map(sf -> sf.fieldName()).collect(Collectors.toSet()); Set searchRuntimeFields = datafeed.getRuntimeMappings().keySet(); - ExtractionMethodDetector extractionMethodDetector = - new ExtractionMethodDetector(scriptFields, fieldsCapabilities, searchRuntimeFields); + ExtractionMethodDetector extractionMethodDetector = new ExtractionMethodDetector( + scriptFields, + fieldsCapabilities, + searchRuntimeFields + ); String timeField = job.getDataDescription().getTimeField(); if (scriptFields.contains(timeField) == false && extractionMethodDetector.isAggregatable(timeField) == false) { throw new IllegalArgumentException("cannot retrieve time field [" + timeField + "] because it is not aggregatable"); @@ -74,7 +76,8 @@ public static TimeBasedExtractedFields build(Job job, DatafeedConfig datafeed, F } private static ExtractedField extractedTimeField(String timeField, Set scriptFields) { - ExtractedField.Method method = scriptFields.contains(timeField) ? ExtractedField.Method.SCRIPT_FIELD + ExtractedField.Method method = scriptFields.contains(timeField) + ? ExtractedField.Method.SCRIPT_FIELD : ExtractedField.Method.DOC_VALUE; return ExtractedFields.newTimeField(timeField, method); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/persistence/DatafeedConfigProvider.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/persistence/DatafeedConfigProvider.java index 7e4ae0ebea54f..374ec7162967b 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/persistence/DatafeedConfigProvider.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/persistence/DatafeedConfigProvider.java @@ -30,12 +30,6 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.regex.Regex; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.IndexNotFoundException; import org.elasticsearch.index.engine.VersionConflictEngineException; import org.elasticsearch.index.query.BoolQueryBuilder; @@ -46,6 +40,12 @@ import org.elasticsearch.persistent.PersistentTasksCustomMetadata; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.builder.SearchSourceBuilder; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.action.util.ExpandedIdsMatcher; import org.elasticsearch.xpack.core.ml.MlConfigIndex; import org.elasticsearch.xpack.core.ml.MlTasks; @@ -88,8 +88,7 @@ public class DatafeedConfigProvider { private final Client client; private final NamedXContentRegistry xContentRegistry; - public static final Map TO_XCONTENT_PARAMS = Map.of( - ToXContentParams.FOR_INTERNAL_STORAGE, "true"); + public static final Map TO_XCONTENT_PARAMS = Map.of(ToXContentParams.FOR_INTERNAL_STORAGE, "true"); public DatafeedConfigProvider(Client client, NamedXContentRegistry xContentRegistry) { this.client = client; @@ -108,9 +107,7 @@ public void putDatafeedConfig(DatafeedConfig config, Map headers if (headers.isEmpty() == false) { // Filter any values in headers that aren't security fields - config = new DatafeedConfig.Builder(config) - .setHeaders(filterSecurityHeaders(headers)) - .build(); + config = new DatafeedConfig.Builder(config).setHeaders(filterSecurityHeaders(headers)).build(); } final String datafeedId = config.getId(); @@ -118,23 +115,19 @@ public void putDatafeedConfig(DatafeedConfig config, Map headers try (XContentBuilder builder = XContentFactory.jsonBuilder()) { XContentBuilder source = config.toXContent(builder, new ToXContent.MapParams(TO_XCONTENT_PARAMS)); - IndexRequest indexRequest = new IndexRequest(MlConfigIndex.indexName()) - .id(DatafeedConfig.documentId(datafeedId)) - .source(source) - .opType(DocWriteRequest.OpType.CREATE) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - - executeAsyncWithOrigin(client, ML_ORIGIN, IndexAction.INSTANCE, indexRequest, ActionListener.wrap( - listener::onResponse, - e -> { - if (ExceptionsHelper.unwrapCause(e) instanceof VersionConflictEngineException) { - // the dafafeed already exists - listener.onFailure(ExceptionsHelper.datafeedAlreadyExists(datafeedId)); - } else { - listener.onFailure(e); - } - } - )); + IndexRequest indexRequest = new IndexRequest(MlConfigIndex.indexName()).id(DatafeedConfig.documentId(datafeedId)) + .source(source) + .opType(DocWriteRequest.OpType.CREATE) + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); + + executeAsyncWithOrigin(client, ML_ORIGIN, IndexAction.INSTANCE, indexRequest, ActionListener.wrap(listener::onResponse, e -> { + if (ExceptionsHelper.unwrapCause(e) instanceof VersionConflictEngineException) { + // the dafafeed already exists + listener.onFailure(ExceptionsHelper.datafeedAlreadyExists(datafeedId)); + } else { + listener.onFailure(e); + } + })); } catch (IOException e) { listener.onFailure(new ElasticsearchParseException("Failed to serialise datafeed config with id [" + config.getId() + "]", e)); @@ -164,6 +157,7 @@ public void onResponse(GetResponse getResponse) { BytesReference source = getResponse.getSourceAsBytesRef(); parseLenientlyFromSource(source, datafeedConfigListener); } + @Override public void onFailure(Exception e) { if (e.getClass() == IndexNotFoundException.class) { @@ -191,49 +185,53 @@ public void findDatafeedIdsForJobIds(Collection jobIds, ActionListenerwrap( - response -> { - Set datafeedIds = new HashSet<>(); - // There cannot be more than one datafeed per job - assert response.getHits().getTotalHits().value <= jobIds.size(); - SearchHit[] hits = response.getHits().getHits(); - - for (SearchHit hit : hits) { - datafeedIds.add(hit.field(DatafeedConfig.ID.getPreferredName()).getValue()); - } - - listener.onResponse(datafeedIds); - }, - listener::onFailure) - , client::search); + .setIndicesOptions(IndicesOptions.lenientExpandOpen()) + .setSize(jobIds.size()) + .setSource(sourceBuilder) + .request(); + + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + ML_ORIGIN, + searchRequest, + ActionListener.wrap(response -> { + Set datafeedIds = new HashSet<>(); + // There cannot be more than one datafeed per job + assert response.getHits().getTotalHits().value <= jobIds.size(); + SearchHit[] hits = response.getHits().getHits(); + + for (SearchHit hit : hits) { + datafeedIds.add(hit.field(DatafeedConfig.ID.getPreferredName()).getValue()); + } + + listener.onResponse(datafeedIds); + }, listener::onFailure), + client::search + ); } public void findDatafeedsByJobIds(Collection jobIds, ActionListener> listener) { SearchRequest searchRequest = client.prepareSearch(MlConfigIndex.indexName()) .setIndicesOptions(IndicesOptions.lenientExpandOpen()) .setSize(jobIds.size()) - .setSource(new SearchSourceBuilder().query(buildDatafeedJobIdsQuery(jobIds))).request(); - - executeAsyncWithOrigin(client.threadPool().getThreadContext(), ML_ORIGIN, searchRequest, - ActionListener.wrap( - response -> { - Map datafeedsByJobId = new HashMap<>(); - // There cannot be more than one datafeed per job - assert response.getHits().getTotalHits().value <= jobIds.size(); - SearchHit[] hits = response.getHits().getHits(); - for (SearchHit hit : hits) { - DatafeedConfig.Builder builder = parseLenientlyFromSource(hit.getSourceRef()); - datafeedsByJobId.put(builder.getJobId(), builder); - } - listener.onResponse(datafeedsByJobId); - }, - listener::onFailure) - , + .setSource(new SearchSourceBuilder().query(buildDatafeedJobIdsQuery(jobIds))) + .request(); + + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + ML_ORIGIN, + searchRequest, + ActionListener.wrap(response -> { + Map datafeedsByJobId = new HashMap<>(); + // There cannot be more than one datafeed per job + assert response.getHits().getTotalHits().value <= jobIds.size(); + SearchHit[] hits = response.getHits().getHits(); + for (SearchHit hit : hits) { + DatafeedConfig.Builder builder = parseLenientlyFromSource(hit.getSourceRef()); + datafeedsByJobId.put(builder.getJobId(), builder); + } + listener.onResponse(datafeedsByJobId); + }, listener::onFailure), client::search ); } @@ -244,7 +242,7 @@ public void findDatafeedsByJobIds(Collection jobIds, ActionListener actionListener) { + public void deleteDatafeedConfig(String datafeedId, ActionListener actionListener) { DeleteRequest request = new DeleteRequest(MlConfigIndex.indexName(), DatafeedConfig.documentId(datafeedId)); request.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); executeAsyncWithOrigin(client, ML_ORIGIN, DeleteAction.INSTANCE, request, actionListener.delegateFailure((l, deleteResponse) -> { @@ -272,9 +270,13 @@ public void deleteDatafeedConfig(String datafeedId, ActionListener headers, - BiConsumer> validator, - ActionListener updatedConfigListener) { + public void updateDatefeedConfig( + String datafeedId, + DatafeedUpdate update, + Map headers, + BiConsumer> validator, + ActionListener updatedConfigListener + ) { GetRequest getRequest = new GetRequest(MlConfigIndex.indexName(), DatafeedConfig.documentId(datafeedId)); executeAsyncWithOrigin(client, ML_ORIGIN, GetAction.INSTANCE, getRequest, new ActionListener.Delegating<>(updatedConfigListener) { @@ -291,8 +293,7 @@ public void onResponse(GetResponse getResponse) { try { configBuilder = parseLenientlyFromSource(source); } catch (IOException e) { - delegate.onFailure( - new ElasticsearchParseException("Failed to parse datafeed config [" + datafeedId + "]", e)); + delegate.onFailure(new ElasticsearchParseException("Failed to parse datafeed config [" + datafeedId + "]", e)); return; } @@ -304,24 +305,24 @@ public void onResponse(GetResponse getResponse) { return; } - ActionListener validatedListener = ActionListener.wrap(ok -> indexUpdatedConfig(updatedConfig, seqNo, primaryTerm, - ActionListener.wrap(indexResponse -> { - assert indexResponse.getResult() == DocWriteResponse.Result.UPDATED; - delegate.onResponse(updatedConfig); - }, delegate::onFailure)), delegate::onFailure); + ActionListener validatedListener = ActionListener.wrap( + ok -> indexUpdatedConfig(updatedConfig, seqNo, primaryTerm, ActionListener.wrap(indexResponse -> { + assert indexResponse.getResult() == DocWriteResponse.Result.UPDATED; + delegate.onResponse(updatedConfig); + }, delegate::onFailure)), + delegate::onFailure + ); validator.accept(updatedConfig, validatedListener); } }); } - private void indexUpdatedConfig(DatafeedConfig updatedConfig, long seqNo, long primaryTerm, - ActionListener listener) { + private void indexUpdatedConfig(DatafeedConfig updatedConfig, long seqNo, long primaryTerm, ActionListener listener) { try (XContentBuilder builder = XContentFactory.jsonBuilder()) { XContentBuilder updatedSource = updatedConfig.toXContent(builder, new ToXContent.MapParams(TO_XCONTENT_PARAMS)); - IndexRequest indexRequest = new IndexRequest(MlConfigIndex.indexName()) - .id(DatafeedConfig.documentId(updatedConfig.getId())) - .source(updatedSource) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); + IndexRequest indexRequest = new IndexRequest(MlConfigIndex.indexName()).id(DatafeedConfig.documentId(updatedConfig.getId())) + .source(updatedSource) + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); indexRequest.setIfSeqNo(seqNo); indexRequest.setIfPrimaryTerm(primaryTerm); @@ -330,7 +331,8 @@ private void indexUpdatedConfig(DatafeedConfig updatedConfig, long seqNo, long p } catch (IOException e) { listener.onFailure( - new ElasticsearchParseException("Failed to serialise datafeed config with id [" + updatedConfig.getId() + "]", e)); + new ElasticsearchParseException("Failed to serialise datafeed config with id [" + updatedConfig.getId() + "]", e) + ); } } @@ -360,49 +362,53 @@ private void indexUpdatedConfig(DatafeedConfig updatedConfig, long seqNo, long p * @param allowMissingConfigs If a datafeed has a task, but is missing a config, allow the ID to be expanded via the existing task * @param listener The expanded datafeed IDs listener */ - public void expandDatafeedIds(String expression, - boolean allowNoMatch, - PersistentTasksCustomMetadata tasks, - boolean allowMissingConfigs, - ActionListener> listener) { - String [] tokens = ExpandedIdsMatcher.tokenizeExpression(expression); + public void expandDatafeedIds( + String expression, + boolean allowNoMatch, + PersistentTasksCustomMetadata tasks, + boolean allowMissingConfigs, + ActionListener> listener + ) { + String[] tokens = ExpandedIdsMatcher.tokenizeExpression(expression); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(buildDatafeedIdQuery(tokens)); sourceBuilder.sort(DatafeedConfig.ID.getPreferredName()); sourceBuilder.fetchSource(false); sourceBuilder.docValueField(DatafeedConfig.ID.getPreferredName(), null); SearchRequest searchRequest = client.prepareSearch(MlConfigIndex.indexName()) - .setIndicesOptions(IndicesOptions.lenientExpandOpen()) - .setSource(sourceBuilder) - .setSize(MlConfigIndex.CONFIG_INDEX_MAX_RESULTS_WINDOW) - .request(); + .setIndicesOptions(IndicesOptions.lenientExpandOpen()) + .setSource(sourceBuilder) + .setSize(MlConfigIndex.CONFIG_INDEX_MAX_RESULTS_WINDOW) + .request(); ExpandedIdsMatcher requiredMatches = new ExpandedIdsMatcher(tokens, allowNoMatch); Collection matchingStartedDatafeedIds = matchingDatafeedIdsWithTasks(tokens, tasks); - executeAsyncWithOrigin(client.threadPool().getThreadContext(), ML_ORIGIN, searchRequest, - ActionListener.wrap( - response -> { - SortedSet datafeedIds = new TreeSet<>(); - SearchHit[] hits = response.getHits().getHits(); - for (SearchHit hit : hits) { - datafeedIds.add(hit.field(DatafeedConfig.ID.getPreferredName()).getValue()); - } - if (allowMissingConfigs) { - datafeedIds.addAll(matchingStartedDatafeedIds); - } - - requiredMatches.filterMatchedIds(datafeedIds); - if (requiredMatches.hasUnmatchedIds()) { - // some required datafeeds were not found - listener.onFailure(ExceptionsHelper.missingDatafeedException(requiredMatches.unmatchedIdsString())); - return; - } - - listener.onResponse(datafeedIds); - }, - listener::onFailure) - , client::search); + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + ML_ORIGIN, + searchRequest, + ActionListener.wrap(response -> { + SortedSet datafeedIds = new TreeSet<>(); + SearchHit[] hits = response.getHits().getHits(); + for (SearchHit hit : hits) { + datafeedIds.add(hit.field(DatafeedConfig.ID.getPreferredName()).getValue()); + } + if (allowMissingConfigs) { + datafeedIds.addAll(matchingStartedDatafeedIds); + } + + requiredMatches.filterMatchedIds(datafeedIds); + if (requiredMatches.hasUnmatchedIds()) { + // some required datafeeds were not found + listener.onFailure(ExceptionsHelper.missingDatafeedException(requiredMatches.unmatchedIdsString())); + return; + } + + listener.onResponse(datafeedIds); + }, listener::onFailure), + client::search + ); } @@ -419,51 +425,53 @@ public void expandDatafeedIds(String expression, * @param listener The expanded datafeed config listener */ public void expandDatafeedConfigs(String expression, boolean allowNoMatch, ActionListener> listener) { - String [] tokens = ExpandedIdsMatcher.tokenizeExpression(expression); + String[] tokens = ExpandedIdsMatcher.tokenizeExpression(expression); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(buildDatafeedIdQuery(tokens)); sourceBuilder.sort(DatafeedConfig.ID.getPreferredName()); SearchRequest searchRequest = client.prepareSearch(MlConfigIndex.indexName()) - .setIndicesOptions(IndicesOptions.lenientExpandOpen()) - .setSource(sourceBuilder) - .setSize(MlConfigIndex.CONFIG_INDEX_MAX_RESULTS_WINDOW) - .request(); + .setIndicesOptions(IndicesOptions.lenientExpandOpen()) + .setSource(sourceBuilder) + .setSize(MlConfigIndex.CONFIG_INDEX_MAX_RESULTS_WINDOW) + .request(); ExpandedIdsMatcher requiredMatches = new ExpandedIdsMatcher(tokens, allowNoMatch); - executeAsyncWithOrigin(client.threadPool().getThreadContext(), ML_ORIGIN, searchRequest, - ActionListener.wrap( - response -> { - List datafeeds = new ArrayList<>(); - Set datafeedIds = new HashSet<>(); - SearchHit[] hits = response.getHits().getHits(); - for (SearchHit hit : hits) { - try { - BytesReference source = hit.getSourceRef(); - DatafeedConfig.Builder datafeed = parseLenientlyFromSource(source); - datafeeds.add(datafeed); - datafeedIds.add(datafeed.getId()); - } catch (IOException e) { - // TODO A better way to handle this rather than just ignoring the error? - logger.error("Error parsing datafeed configuration [" + hit.getId() + "]", e); - } - } - - requiredMatches.filterMatchedIds(datafeedIds); - if (requiredMatches.hasUnmatchedIds()) { - // some required datafeeds were not found - listener.onFailure(ExceptionsHelper.missingDatafeedException(requiredMatches.unmatchedIdsString())); - return; - } - - listener.onResponse(datafeeds); - }, - listener::onFailure) - , client::search); + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + ML_ORIGIN, + searchRequest, + ActionListener.wrap(response -> { + List datafeeds = new ArrayList<>(); + Set datafeedIds = new HashSet<>(); + SearchHit[] hits = response.getHits().getHits(); + for (SearchHit hit : hits) { + try { + BytesReference source = hit.getSourceRef(); + DatafeedConfig.Builder datafeed = parseLenientlyFromSource(source); + datafeeds.add(datafeed); + datafeedIds.add(datafeed.getId()); + } catch (IOException e) { + // TODO A better way to handle this rather than just ignoring the error? + logger.error("Error parsing datafeed configuration [" + hit.getId() + "]", e); + } + } + + requiredMatches.filterMatchedIds(datafeedIds); + if (requiredMatches.hasUnmatchedIds()) { + // some required datafeeds were not found + listener.onFailure(ExceptionsHelper.missingDatafeedException(requiredMatches.unmatchedIdsString())); + return; + } + + listener.onResponse(datafeeds); + }, listener::onFailure), + client::search + ); } - private QueryBuilder buildDatafeedIdQuery(String [] tokens) { + private QueryBuilder buildDatafeedIdQuery(String[] tokens) { QueryBuilder datafeedQuery = new TermQueryBuilder(DatafeedConfig.CONFIG_TYPE.getPreferredName(), DatafeedConfig.TYPE); if (Strings.isAllOrWildcard(tokens)) { // match all @@ -505,10 +513,12 @@ private QueryBuilder buildDatafeedJobIdsQuery(Collection jobIds) { return boolQueryBuilder; } - private void parseLenientlyFromSource(BytesReference source, ActionListener datafeedConfigListener) { - try (InputStream stream = source.streamInput(); - XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, stream)) { + private void parseLenientlyFromSource(BytesReference source, ActionListener datafeedConfigListener) { + try ( + InputStream stream = source.streamInput(); + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, stream) + ) { datafeedConfigListener.onResponse(DatafeedConfig.LENIENT_PARSER.apply(parser, null)); } catch (Exception e) { datafeedConfigListener.onFailure(e); @@ -516,9 +526,11 @@ private void parseLenientlyFromSource(BytesReference source, ActionListener configListener = ActionListener.wrap( - config -> { - // Check if existing destination index is incompatible. - // If it is, we delete it and start from reindexing. - IndexMetadata destIndex = clusterState.getMetadata().index(config.getDest().getIndex()); - if (destIndex != null) { - MappingMetadata destIndexMapping = clusterState.getMetadata().index(config.getDest().getIndex()).mapping(); - DestinationIndex.Metadata metadata = DestinationIndex.readMetadata(config.getId(), destIndexMapping); - if (metadata.hasMetadata() && (metadata.isCompatible() == false)) { - LOGGER.info("[{}] Destination index was created in version [{}] but minimum supported version is [{}]. " + - "Deleting index and starting from scratch.", config.getId(), metadata.getVersion(), - DestinationIndex.MIN_COMPATIBLE_VERSION); - task.getStatsHolder().resetProgressTracker(config.getAnalysis().getProgressPhases(), - config.getAnalysis().supportsInference()); - executeJobInMiddleOfReindexing(task, config); - return; - } + ActionListener configListener = ActionListener.wrap(config -> { + // Check if existing destination index is incompatible. + // If it is, we delete it and start from reindexing. + IndexMetadata destIndex = clusterState.getMetadata().index(config.getDest().getIndex()); + if (destIndex != null) { + MappingMetadata destIndexMapping = clusterState.getMetadata().index(config.getDest().getIndex()).mapping(); + DestinationIndex.Metadata metadata = DestinationIndex.readMetadata(config.getId(), destIndexMapping); + if (metadata.hasMetadata() && (metadata.isCompatible() == false)) { + LOGGER.info( + "[{}] Destination index was created in version [{}] but minimum supported version is [{}]. " + + "Deleting index and starting from scratch.", + config.getId(), + metadata.getVersion(), + DestinationIndex.MIN_COMPATIBLE_VERSION + ); + task.getStatsHolder() + .resetProgressTracker(config.getAnalysis().getProgressPhases(), config.getAnalysis().supportsInference()); + executeJobInMiddleOfReindexing(task, config); + return; } + } - task.getStatsHolder().adjustProgressTracker(config.getAnalysis().getProgressPhases(), - config.getAnalysis().supportsInference()); + task.getStatsHolder().adjustProgressTracker(config.getAnalysis().getProgressPhases(), config.getAnalysis().supportsInference()); - determineProgressAndResume(task, config); + determineProgressAndResume(task, config); - }, - task::setFailed - ); + }, task::setFailed); // Retrieve configuration ActionListener statsIndexListener = ActionListener.wrap( @@ -125,8 +133,13 @@ public void execute(DataFrameAnalyticsTask task, ClusterState clusterState, Time // Make sure the stats index and alias exist ActionListener stateAliasListener = ActionListener.wrap( - aBoolean -> createStatsIndexAndUpdateMappingsIfNecessary(new ParentTaskAssigningClient(client, task.getParentTaskId()), - clusterState, masterNodeTimeout, statsIndexListener), configListener::onFailure + aBoolean -> createStatsIndexAndUpdateMappingsIfNecessary( + new ParentTaskAssigningClient(client, task.getParentTaskId()), + clusterState, + masterNodeTimeout, + statsIndexListener + ), + configListener::onFailure ); // Make sure the state index and alias exist @@ -139,17 +152,22 @@ public void execute(DataFrameAnalyticsTask task, ClusterState clusterState, Time ); } - private void createStatsIndexAndUpdateMappingsIfNecessary(Client client, ClusterState clusterState, TimeValue masterNodeTimeout, - ActionListener listener) { + private void createStatsIndexAndUpdateMappingsIfNecessary( + Client client, + ClusterState clusterState, + TimeValue masterNodeTimeout, + ActionListener listener + ) { ActionListener createIndexListener = ActionListener.wrap( aBoolean -> ElasticsearchMappings.addDocMappingIfMissing( - MlStatsIndex.writeAlias(), - MlStatsIndex::wrappedMapping, - client, - clusterState, - masterNodeTimeout, - listener) - , listener::onFailure + MlStatsIndex.writeAlias(), + MlStatsIndex::wrappedMapping, + client, + clusterState, + masterNodeTimeout, + listener + ), + listener::onFailure ); MlStatsIndex.createStatsIndexAndAliasIfNecessary(client, clusterState, expressionResolver, masterNodeTimeout, createIndexListener); @@ -170,10 +188,11 @@ private void determineProgressAndResume(DataFrameAnalyticsTask task, DataFrameAn executeStep(task, config, new AnalysisStep(client, task, auditor, config, processManager)); break; case RESUMING_INFERENCE: - buildInferenceStep(task, config, ActionListener.wrap( - inferenceStep -> executeStep(task, config, inferenceStep), - task::setFailed - )); + buildInferenceStep( + task, + config, + ActionListener.wrap(inferenceStep -> executeStep(task, config, inferenceStep), task::setFailed) + ); break; case FINISHED: default: @@ -184,36 +203,34 @@ private void determineProgressAndResume(DataFrameAnalyticsTask task, DataFrameAn private void executeStep(DataFrameAnalyticsTask task, DataFrameAnalyticsConfig config, DataFrameAnalyticsStep step) { task.setStep(step); - ActionListener stepListener = ActionListener.wrap( - stepResponse -> { - if (stepResponse.isTaskComplete()) { - // We always want to perform the final step as it tidies things up + ActionListener stepListener = ActionListener.wrap(stepResponse -> { + if (stepResponse.isTaskComplete()) { + // We always want to perform the final step as it tidies things up + executeStep(task, config, new FinalStep(client, task, auditor, config)); + return; + } + switch (step.name()) { + case REINDEXING: + executeStep(task, config, new AnalysisStep(client, task, auditor, config, processManager)); + break; + case ANALYSIS: + buildInferenceStep( + task, + config, + ActionListener.wrap(inferenceStep -> executeStep(task, config, inferenceStep), task::setFailed) + ); + break; + case INFERENCE: executeStep(task, config, new FinalStep(client, task, auditor, config)); - return; - } - switch (step.name()) { - case REINDEXING: - executeStep(task, config, new AnalysisStep(client, task, auditor, config, processManager)); - break; - case ANALYSIS: - buildInferenceStep(task, config, ActionListener.wrap( - inferenceStep -> executeStep(task, config, inferenceStep), - task::setFailed - )); - break; - case INFERENCE: - executeStep(task, config, new FinalStep(client, task, auditor, config)); - break; - case FINAL: - LOGGER.info("[{}] Marking task completed", config.getId()); - task.markAsCompleted(); - break; - default: - task.markAsFailed(ExceptionsHelper.serverError("Unknown step [{}]", step)); - } - }, - task::setFailed - ); + break; + case FINAL: + LOGGER.info("[{}] Marking task completed", config.getId()); + task.markAsCompleted(); + break; + default: + task.markAsFailed(ExceptionsHelper.serverError("Unknown step [{}]", step)); + } + }, task::setFailed); step.execute(stepListener); } @@ -224,37 +241,41 @@ private void executeJobInMiddleOfReindexing(DataFrameAnalyticsTask task, DataFra task.markAsCompleted(); return; } - ClientHelper.executeAsyncWithOrigin(new ParentTaskAssigningClient(client, task.getParentTaskId()), + ClientHelper.executeAsyncWithOrigin( + new ParentTaskAssigningClient(client, task.getParentTaskId()), ML_ORIGIN, DeleteIndexAction.INSTANCE, new DeleteIndexRequest(config.getDest().getIndex()), - ActionListener.wrap( - r-> executeStep(task, config, new ReindexingStep(clusterService, client, task, auditor, config)), - e -> { - Throwable cause = ExceptionsHelper.unwrapCause(e); - if (cause instanceof IndexNotFoundException) { - executeStep(task, config, new ReindexingStep(clusterService, client, task, auditor, config)); - } else { - task.setFailed(e); - } + ActionListener.wrap(r -> executeStep(task, config, new ReindexingStep(clusterService, client, task, auditor, config)), e -> { + Throwable cause = ExceptionsHelper.unwrapCause(e); + if (cause instanceof IndexNotFoundException) { + executeStep(task, config, new ReindexingStep(clusterService, client, task, auditor, config)); + } else { + task.setFailed(e); } - )); + }) + ); } private void buildInferenceStep(DataFrameAnalyticsTask task, DataFrameAnalyticsConfig config, ActionListener listener) { ParentTaskAssigningClient parentTaskClient = new ParentTaskAssigningClient(client, task.getParentTaskId()); - ActionListener extractedFieldsDetectorListener = ActionListener.wrap( - extractedFieldsDetector -> { - ExtractedFields extractedFields = extractedFieldsDetector.detect().v1(); - InferenceRunner inferenceRunner = new InferenceRunner(settings, parentTaskClient, modelLoadingService, - resultsPersisterService, task.getParentTaskId(), config, extractedFields, task.getStatsHolder().getProgressTracker(), - task.getStatsHolder().getDataCountsTracker()); - InferenceStep inferenceStep = new InferenceStep(client, task, auditor, config, threadPool, inferenceRunner); - listener.onResponse(inferenceStep); - }, - listener::onFailure - ); + ActionListener extractedFieldsDetectorListener = ActionListener.wrap(extractedFieldsDetector -> { + ExtractedFields extractedFields = extractedFieldsDetector.detect().v1(); + InferenceRunner inferenceRunner = new InferenceRunner( + settings, + parentTaskClient, + modelLoadingService, + resultsPersisterService, + task.getParentTaskId(), + config, + extractedFields, + task.getStatsHolder().getProgressTracker(), + task.getStatsHolder().getDataCountsTracker() + ); + InferenceStep inferenceStep = new InferenceStep(client, task, auditor, config, threadPool, inferenceRunner); + listener.onResponse(inferenceStep); + }, listener::onFailure); new ExtractedFieldsDetectorFactory(parentTaskClient).createFromDest(config, extractedFieldsDetectorListener); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/DataFrameAnalyticsTask.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/DataFrameAnalyticsTask.java index e5875d1948bca..f3eb9f6a954e4 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/DataFrameAnalyticsTask.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/DataFrameAnalyticsTask.java @@ -22,13 +22,13 @@ import org.elasticsearch.client.ParentTaskAssigningClient; import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.index.query.IdsQueryBuilder; import org.elasticsearch.license.LicensedAllocatedPersistentTask; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.tasks.TaskId; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.ml.MlTasks; import org.elasticsearch.xpack.core.ml.action.StartDataFrameAnalyticsAction; import org.elasticsearch.xpack.core.ml.action.StopDataFrameAnalyticsAction; @@ -66,9 +66,18 @@ public class DataFrameAnalyticsTask extends LicensedAllocatedPersistentTask impl private volatile StatsHolder statsHolder; private volatile DataFrameAnalyticsStep currentStep; - public DataFrameAnalyticsTask(long id, String type, String action, TaskId parentTask, Map headers, - Client client, DataFrameAnalyticsManager analyticsManager, DataFrameAnalyticsAuditor auditor, - StartDataFrameAnalyticsAction.TaskParams taskParams, XPackLicenseState licenseState) { + public DataFrameAnalyticsTask( + long id, + String type, + String action, + TaskId parentTask, + Map headers, + Client client, + DataFrameAnalyticsManager analyticsManager, + DataFrameAnalyticsAuditor auditor, + StartDataFrameAnalyticsAction.TaskParams taskParams, + XPackLicenseState licenseState + ) { super( id, type, @@ -147,15 +156,14 @@ public void stop(String reason, TimeValue timeout) { LOGGER.debug(() -> new ParameterizedMessage("[{}] Stopping task due to reason [{}]", getParams().getId(), reason)); DataFrameAnalyticsStep cachedCurrentStep = currentStep; - ActionListener stepProgressListener = ActionListener.wrap( - aVoid -> cachedCurrentStep.cancel(reason, timeout), - e -> { - LOGGER.error(new ParameterizedMessage("[{}] Error updating progress for step [{}]", - taskParams.getId(), cachedCurrentStep.name()), e); - // We should log the error but it shouldn't stop us from stopping the task - cachedCurrentStep.cancel(reason, timeout); - } - ); + ActionListener stepProgressListener = ActionListener.wrap(aVoid -> cachedCurrentStep.cancel(reason, timeout), e -> { + LOGGER.error( + new ParameterizedMessage("[{}] Error updating progress for step [{}]", taskParams.getId(), cachedCurrentStep.name()), + e + ); + // We should log the error but it shouldn't stop us from stopping the task + cachedCurrentStep.cancel(reason, timeout); + }); if (cachedCurrentStep != null) { cachedCurrentStep.updateProgress(stepProgressListener); @@ -166,27 +174,37 @@ public void setFailed(Exception error) { if (analyticsManager.isNodeShuttingDown()) { LOGGER.warn( new ParameterizedMessage("[{}] *Not* setting task to failed because the node is being shutdown", taskParams.getId()), - error); + error + ); return; } persistProgress(client, taskParams.getId(), () -> { LOGGER.error(new ParameterizedMessage("[{}] Setting task to failed", taskParams.getId()), error); String reason = ExceptionsHelper.unwrapCause(error).getMessage(); - DataFrameAnalyticsTaskState newTaskState = - new DataFrameAnalyticsTaskState(DataFrameAnalyticsState.FAILED, getAllocationId(), reason); - updatePersistentTaskState( - newTaskState, - ActionListener.wrap( - updatedTask -> { - String message = Messages.getMessage(Messages.DATA_FRAME_ANALYTICS_AUDIT_UPDATED_STATE_WITH_REASON, - DataFrameAnalyticsState.FAILED, reason); - auditor.info(getParams().getId(), message); - LOGGER.info("[{}] {}", getParams().getId(), message); - }, - e -> LOGGER.error(new ParameterizedMessage("[{}] Could not update task state to [{}] with reason [{}]", - getParams().getId(), DataFrameAnalyticsState.FAILED, reason), e) - ) + DataFrameAnalyticsTaskState newTaskState = new DataFrameAnalyticsTaskState( + DataFrameAnalyticsState.FAILED, + getAllocationId(), + reason ); + updatePersistentTaskState(newTaskState, ActionListener.wrap(updatedTask -> { + String message = Messages.getMessage( + Messages.DATA_FRAME_ANALYTICS_AUDIT_UPDATED_STATE_WITH_REASON, + DataFrameAnalyticsState.FAILED, + reason + ); + auditor.info(getParams().getId(), message); + LOGGER.info("[{}] {}", getParams().getId(), message); + }, + e -> LOGGER.error( + new ParameterizedMessage( + "[{}] Could not update task state to [{}] with reason [{}]", + getParams().getId(), + DataFrameAnalyticsState.FAILED, + reason + ), + e + ) + )); }); } @@ -203,79 +221,79 @@ void persistProgress(Client client, String jobId, Runnable runnable) { String progressDocId = StoredProgress.documentId(jobId); // Step 4: Run the runnable provided as the argument - ActionListener indexProgressDocListener = ActionListener.wrap( - indexResponse -> { - LOGGER.debug("[{}] Successfully indexed progress document: {}", jobId, storedProgress.get().get()); - runnable.run(); - }, - indexError -> { - LOGGER.error(new ParameterizedMessage( - "[{}] cannot persist progress as an error occurred while indexing", jobId), indexError); - runnable.run(); - } - ); + ActionListener indexProgressDocListener = ActionListener.wrap(indexResponse -> { + LOGGER.debug("[{}] Successfully indexed progress document: {}", jobId, storedProgress.get().get()); + runnable.run(); + }, indexError -> { + LOGGER.error(new ParameterizedMessage("[{}] cannot persist progress as an error occurred while indexing", jobId), indexError); + runnable.run(); + }); // Step 3: Create or update the progress document: - // - if the document did not exist, create the new one in the current write index - // - if the document did exist, update it in the index where it resides (not necessarily the current write index) - ActionListener searchFormerProgressDocListener = ActionListener.wrap( - searchResponse -> { - String indexOrAlias = AnomalyDetectorsIndex.jobStateIndexWriteAlias(); - StoredProgress previous = null; - if (searchResponse.getHits().getHits().length > 0) { - indexOrAlias = searchResponse.getHits().getHits()[0].getIndex(); - try { - previous = MlParserUtils.parse(searchResponse.getHits().getHits()[0], StoredProgress.PARSER); - } catch (Exception ex) { - LOGGER.warn(new ParameterizedMessage("[{}] failed to parse previously stored progress", jobId), ex); - } + // - if the document did not exist, create the new one in the current write index + // - if the document did exist, update it in the index where it resides (not necessarily the current write index) + ActionListener searchFormerProgressDocListener = ActionListener.wrap(searchResponse -> { + String indexOrAlias = AnomalyDetectorsIndex.jobStateIndexWriteAlias(); + StoredProgress previous = null; + if (searchResponse.getHits().getHits().length > 0) { + indexOrAlias = searchResponse.getHits().getHits()[0].getIndex(); + try { + previous = MlParserUtils.parse(searchResponse.getHits().getHits()[0], StoredProgress.PARSER); + } catch (Exception ex) { + LOGGER.warn(new ParameterizedMessage("[{}] failed to parse previously stored progress", jobId), ex); } + } - List progress = statsHolder.getProgressTracker().report(); - storedProgress.set(new StoredProgress(progress)); - if (storedProgress.get().equals(previous)) { - LOGGER.debug(() -> new ParameterizedMessage( + List progress = statsHolder.getProgressTracker().report(); + storedProgress.set(new StoredProgress(progress)); + if (storedProgress.get().equals(previous)) { + LOGGER.debug( + () -> new ParameterizedMessage( "[{}] new progress is the same as previously persisted progress. Skipping storage of progress: {}", - jobId, progress)); - runnable.run(); - return; - } - - IndexRequest indexRequest = new IndexRequest(indexOrAlias) - .id(progressDocId) - .setRequireAlias(AnomalyDetectorsIndex.jobStateIndexWriteAlias().equals(indexOrAlias)) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - try (XContentBuilder jsonBuilder = JsonXContent.contentBuilder()) { - LOGGER.debug(() -> new ParameterizedMessage("[{}] Persisting progress is: {}", jobId, progress)); - storedProgress.get().toXContent(jsonBuilder, Payload.XContent.EMPTY_PARAMS); - indexRequest.source(jsonBuilder); - } - executeAsyncWithOrigin(client, ML_ORIGIN, IndexAction.INSTANCE, indexRequest, indexProgressDocListener); - }, - e -> { - LOGGER.error(new ParameterizedMessage( - "[{}] cannot persist progress as an error occurred while retrieving former progress document", jobId), e); + jobId, + progress + ) + ); runnable.run(); + return; } - ); - // Step 2: Search for existing progress document in .ml-state* - ActionListener stepProgressUpdateListener = ActionListener.wrap( - aVoid -> { - SearchRequest searchRequest = - new SearchRequest(AnomalyDetectorsIndex.jobStateIndexPattern()) - .source( - new SearchSourceBuilder() - .size(1) - .query(new IdsQueryBuilder().addIds(progressDocId))); - executeAsyncWithOrigin(client, ML_ORIGIN, SearchAction.INSTANCE, searchRequest, searchFormerProgressDocListener); - }, - e -> { - LOGGER.error(new ParameterizedMessage( - "[{}] cannot persist progress as an error occurred while updating task progress", taskParams.getId()), e); - runnable.run(); + IndexRequest indexRequest = new IndexRequest(indexOrAlias).id(progressDocId) + .setRequireAlias(AnomalyDetectorsIndex.jobStateIndexWriteAlias().equals(indexOrAlias)) + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); + try (XContentBuilder jsonBuilder = JsonXContent.contentBuilder()) { + LOGGER.debug(() -> new ParameterizedMessage("[{}] Persisting progress is: {}", jobId, progress)); + storedProgress.get().toXContent(jsonBuilder, Payload.XContent.EMPTY_PARAMS); + indexRequest.source(jsonBuilder); } - ); + executeAsyncWithOrigin(client, ML_ORIGIN, IndexAction.INSTANCE, indexRequest, indexProgressDocListener); + }, e -> { + LOGGER.error( + new ParameterizedMessage( + "[{}] cannot persist progress as an error occurred while retrieving former progress document", + jobId + ), + e + ); + runnable.run(); + }); + + // Step 2: Search for existing progress document in .ml-state* + ActionListener stepProgressUpdateListener = ActionListener.wrap(aVoid -> { + SearchRequest searchRequest = new SearchRequest(AnomalyDetectorsIndex.jobStateIndexPattern()).source( + new SearchSourceBuilder().size(1).query(new IdsQueryBuilder().addIds(progressDocId)) + ); + executeAsyncWithOrigin(client, ML_ORIGIN, SearchAction.INSTANCE, searchRequest, searchFormerProgressDocListener); + }, e -> { + LOGGER.error( + new ParameterizedMessage( + "[{}] cannot persist progress as an error occurred while updating task progress", + taskParams.getId() + ), + e + ); + runnable.run(); + }); // Step 1: Update reindexing progress as it could be stale updateTaskProgress(stepProgressUpdateListener); @@ -299,7 +317,11 @@ public void updateTaskProgress(ActionListener updateProgressListener) { * {@code FINISHED} means the job had finished. */ public enum StartingState { - FIRST_TIME, RESUMING_REINDEXING, RESUMING_ANALYZING, RESUMING_INFERENCE, FINISHED + FIRST_TIME, + RESUMING_REINDEXING, + RESUMING_ANALYZING, + RESUMING_INFERENCE, + FINISHED } public StartingState determineStartingState() { @@ -319,8 +341,12 @@ public static StartingState determineStartingState(String jobId, List listener) { + public static void createDestinationIndex( + Client client, + Clock clock, + DataFrameAnalyticsConfig analyticsConfig, + ActionListener listener + ) { ActionListener createIndexRequestListener = ActionListener.wrap( - createIndexRequest -> ClientHelper.executeWithHeadersAsync(analyticsConfig.getHeaders(), ClientHelper.ML_ORIGIN, client, - CreateIndexAction.INSTANCE, createIndexRequest, listener), + createIndexRequest -> ClientHelper.executeWithHeadersAsync( + analyticsConfig.getHeaders(), + ClientHelper.ML_ORIGIN, + client, + CreateIndexAction.INSTANCE, + createIndexRequest, + listener + ), listener::onFailure ); prepareCreateIndexRequest(client, clock, analyticsConfig, createIndexRequestListener); } - private static void prepareCreateIndexRequest(Client client, Clock clock, DataFrameAnalyticsConfig config, - ActionListener listener) { + private static void prepareCreateIndexRequest( + Client client, + Clock clock, + DataFrameAnalyticsConfig config, + ActionListener listener + ) { AtomicReference settingsHolder = new AtomicReference<>(); AtomicReference mappingsHolder = new AtomicReference<>(); ActionListener fieldCapabilitiesListener = ActionListener.wrap( fieldCapabilitiesResponse -> { listener.onResponse( - createIndexRequest(clock, config, settingsHolder.get(), mappingsHolder.get(), fieldCapabilitiesResponse)); + createIndexRequest(clock, config, settingsHolder.get(), mappingsHolder.get(), fieldCapabilitiesResponse) + ); }, listener::onFailure ); - ActionListener mappingsListener = ActionListener.wrap( - mappings -> { - mappingsHolder.set(mappings); - getFieldCapsForRequiredFields(client, config, fieldCapabilitiesListener); - }, - listener::onFailure - ); + ActionListener mappingsListener = ActionListener.wrap(mappings -> { + mappingsHolder.set(mappings); + getFieldCapsForRequiredFields(client, config, fieldCapabilitiesListener); + }, listener::onFailure); - ActionListener settingsListener = ActionListener.wrap( - settings -> { - settingsHolder.set(settings); - MappingsMerger.mergeMappings(client, config.getHeaders(), config.getSource(), mappingsListener); - }, - listener::onFailure - ); + ActionListener settingsListener = ActionListener.wrap(settings -> { + settingsHolder.set(settings); + MappingsMerger.mergeMappings(client, config.getHeaders(), config.getSource(), mappingsListener); + }, listener::onFailure); ActionListener getSettingsResponseListener = ActionListener.wrap( settingsResponse -> settingsListener.onResponse(settings(settingsResponse)), listener::onFailure ); - GetSettingsRequest getSettingsRequest = - new GetSettingsRequest() - .indices(config.getSource().getIndex()) - .indicesOptions(IndicesOptions.lenientExpandOpen()) - .names(PRESERVED_SETTINGS); + GetSettingsRequest getSettingsRequest = new GetSettingsRequest().indices(config.getSource().getIndex()) + .indicesOptions(IndicesOptions.lenientExpandOpen()) + .names(PRESERVED_SETTINGS); ClientHelper.executeWithHeadersAsync( - config.getHeaders(), ML_ORIGIN, client, GetSettingsAction.INSTANCE, getSettingsRequest, getSettingsResponseListener); + config.getHeaders(), + ML_ORIGIN, + client, + GetSettingsAction.INSTANCE, + getSettingsRequest, + getSettingsResponseListener + ); } - private static void getFieldCapsForRequiredFields(Client client, DataFrameAnalyticsConfig config, - ActionListener listener) { + private static void getFieldCapsForRequiredFields( + Client client, + DataFrameAnalyticsConfig config, + ActionListener listener + ) { List requiredFields = config.getAnalysis().getRequiredFields(); if (requiredFields.isEmpty()) { listener.onResponse(null); return; } - FieldCapabilitiesRequest fieldCapabilitiesRequest = - new FieldCapabilitiesRequest() - .indices(config.getSource().getIndex()) - .fields(requiredFields.stream().map(RequiredField::getName).toArray(String[]::new)) - .runtimeFields(config.getSource().getRuntimeMappings()); + FieldCapabilitiesRequest fieldCapabilitiesRequest = new FieldCapabilitiesRequest().indices(config.getSource().getIndex()) + .fields(requiredFields.stream().map(RequiredField::getName).toArray(String[]::new)) + .runtimeFields(config.getSource().getRuntimeMappings()); ClientHelper.executeWithHeadersAsync( config.getHeaders(), ML_ORIGIN, client, FieldCapabilitiesAction.INSTANCE, fieldCapabilitiesRequest, - listener); + listener + ); } - private static CreateIndexRequest createIndexRequest(Clock clock, - DataFrameAnalyticsConfig config, - Settings settings, - MappingMetadata mappings, - FieldCapabilitiesResponse fieldCapabilitiesResponse) { + private static CreateIndexRequest createIndexRequest( + Clock clock, + DataFrameAnalyticsConfig config, + Settings settings, + MappingMetadata mappings, + FieldCapabilitiesResponse fieldCapabilitiesResponse + ) { String destinationIndex = config.getDest().getIndex(); Map mappingsAsMap = mappings.sourceAsMap(); Map properties = getOrPutDefault(mappingsAsMap, PROPERTIES, HashMap::new); @@ -217,13 +232,13 @@ private static Integer findMaxSettingValue(GetSettingsResponse settingsResponse, return maxValue; } - private static Map createAdditionalMappings(DataFrameAnalyticsConfig config, - FieldCapabilitiesResponse fieldCapabilitiesResponse) { + private static Map createAdditionalMappings( + DataFrameAnalyticsConfig config, + FieldCapabilitiesResponse fieldCapabilitiesResponse + ) { Map properties = new HashMap<>(); properties.put(INCREMENTAL_ID, Map.of("type", NumberFieldMapper.NumberType.LONG.typeName())); - properties.putAll( - config.getAnalysis().getResultMappings( - config.getDest().getResultsField(), fieldCapabilitiesResponse)); + properties.putAll(config.getAnalysis().getResultMappings(config.getDest().getResultsField(), fieldCapabilitiesResponse)); return properties; } @@ -248,42 +263,44 @@ private static V getOrPutDefault(Map map, K key, Supplier v } @SuppressWarnings("unchecked") - public static void updateMappingsToDestIndex(Client client, - DataFrameAnalyticsConfig config, - GetIndexResponse getIndexResponse, - ActionListener listener) { + public static void updateMappingsToDestIndex( + Client client, + DataFrameAnalyticsConfig config, + GetIndexResponse getIndexResponse, + ActionListener listener + ) { // We have validated the destination index should match a single index assert getIndexResponse.indices().length == 1; // Fetch mappings from destination index Map destMappingsAsMap = getIndexResponse.mappings().valuesIt().next().sourceAsMap(); - Map destPropertiesAsMap = - (Map)destMappingsAsMap.getOrDefault(PROPERTIES, Collections.emptyMap()); + Map destPropertiesAsMap = (Map) destMappingsAsMap.getOrDefault(PROPERTIES, Collections.emptyMap()); // Verify that the results field does not exist in the dest index checkResultsFieldIsNotPresentInProperties(config, destPropertiesAsMap); - ActionListener fieldCapabilitiesListener = ActionListener.wrap( - fieldCapabilitiesResponse -> { - Map addedMappings = new HashMap<>(); - - // Determine mappings to be added to the destination index - addedMappings.put(PROPERTIES, createAdditionalMappings(config, fieldCapabilitiesResponse)); - - // Also add runtime mappings - if (config.getSource().getRuntimeMappings().isEmpty() == false) { - addedMappings.put(RUNTIME, config.getSource().getRuntimeMappings()); - } - - // Add the mappings to the destination index - PutMappingRequest putMappingRequest = - new PutMappingRequest(getIndexResponse.indices()) - .source(addedMappings); - ClientHelper.executeWithHeadersAsync( - config.getHeaders(), ML_ORIGIN, client, PutMappingAction.INSTANCE, putMappingRequest, listener); - }, - listener::onFailure - ); + ActionListener fieldCapabilitiesListener = ActionListener.wrap(fieldCapabilitiesResponse -> { + Map addedMappings = new HashMap<>(); + + // Determine mappings to be added to the destination index + addedMappings.put(PROPERTIES, createAdditionalMappings(config, fieldCapabilitiesResponse)); + + // Also add runtime mappings + if (config.getSource().getRuntimeMappings().isEmpty() == false) { + addedMappings.put(RUNTIME, config.getSource().getRuntimeMappings()); + } + + // Add the mappings to the destination index + PutMappingRequest putMappingRequest = new PutMappingRequest(getIndexResponse.indices()).source(addedMappings); + ClientHelper.executeWithHeadersAsync( + config.getHeaders(), + ML_ORIGIN, + client, + PutMappingAction.INSTANCE, + putMappingRequest, + listener + ); + }, listener::onFailure); getFieldCapsForRequiredFields(client, config, fieldCapabilitiesListener); } @@ -296,7 +313,8 @@ private static void checkResultsFieldIsNotPresentInProperties(DataFrameAnalytics DataFrameAnalyticsConfig.DEST.getPreferredName(), DataFrameAnalyticsDest.RESULTS_FIELD.getPreferredName(), resultsField, - DataFrameAnalyticsDest.RESULTS_FIELD.getPreferredName()); + DataFrameAnalyticsDest.RESULTS_FIELD.getPreferredName() + ); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/MappingsMerger.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/MappingsMerger.java index d777b8d759281..8adb65e215d30 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/MappingsMerger.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/MappingsMerger.java @@ -7,6 +7,7 @@ package org.elasticsearch.xpack.ml.dataframe; import com.carrotsearch.hppc.cursors.ObjectObjectCursor; + import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsAction; import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest; @@ -35,8 +36,12 @@ public final class MappingsMerger { private MappingsMerger() {} - public static void mergeMappings(Client client, Map headers, DataFrameAnalyticsSource source, - ActionListener listener) { + public static void mergeMappings( + Client client, + Map headers, + DataFrameAnalyticsSource source, + ActionListener listener + ) { ActionListener mappingsListener = ActionListener.wrap( getMappingsResponse -> listener.onResponse(MappingsMerger.mergeMappings(source, getMappingsResponse)), listener::onFailure @@ -55,17 +60,21 @@ static MappingMetadata mergeMappings(DataFrameAnalyticsSource source, GetMapping for (MappingsType mappingsType : MappingsType.values()) { Map mergedMappingsForType = mergeAcrossIndices(source, indexToMappings, mappingsType); if (mergedMappingsForType.isEmpty() == false) { - mappings.put(mappingsType.type, - mergedMappingsForType.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().mapping))); + mappings.put( + mappingsType.type, + mergedMappingsForType.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().mapping)) + ); } } return new MappingMetadata(MapperService.SINGLE_MAPPING_NAME, mappings); } - private static Map mergeAcrossIndices(DataFrameAnalyticsSource source, - ImmutableOpenMap indexToMappings, - MappingsType mappingsType) { + private static Map mergeAcrossIndices( + DataFrameAnalyticsSource source, + ImmutableOpenMap indexToMappings, + MappingsType mappingsType + ) { Map mergedMappings = new HashMap<>(); Iterator> iterator = indexToMappings.iterator(); @@ -86,9 +95,15 @@ private static Map mergeAcrossIndices(DataFrameAnalytic IndexAndMapping existingIndexAndMapping = mergedMappings.get(field); if (existingIndexAndMapping.mapping.equals(fieldMapping.getValue()) == false) { throw ExceptionsHelper.badRequestException( - "cannot merge [{}] mappings because of differences for field [{}]; mapped as [{}] in index [{}]; " + - "mapped as [{}] in index [{}]", mappingsType.type, field, fieldMapping.getValue(), - indexMappings.key, existingIndexAndMapping.mapping, existingIndexAndMapping.index); + "cannot merge [{}] mappings because of differences for field [{}]; mapped as [{}] in index [{}]; " + + "mapped as [{}] in index [{}]", + mappingsType.type, + field, + fieldMapping.getValue(), + indexMappings.key, + existingIndexAndMapping.mapping, + existingIndexAndMapping.index + ); } } else { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/StoredProgress.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/StoredProgress.java index a9a355c6f10c6..88d4b2ea37f04 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/StoredProgress.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/StoredProgress.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.ml.dataframe; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ml.utils.PhaseProgress; @@ -25,7 +25,10 @@ public class StoredProgress implements ToXContentObject { @SuppressWarnings("unchecked") public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - PROGRESS.getPreferredName(), true, a -> new StoredProgress((List) a[0])); + PROGRESS.getPreferredName(), + true, + a -> new StoredProgress((List) a[0]) + ); static { PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), PhaseProgress.PARSER, PROGRESS); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/extractor/DataFrameDataExtractor.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/extractor/DataFrameDataExtractor.java index ee6dd8f3dd85a..6e442559a8605 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/extractor/DataFrameDataExtractor.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/extractor/DataFrameDataExtractor.java @@ -14,8 +14,8 @@ import org.elasticsearch.action.search.SearchRequestBuilder; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.Client; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.util.CachedSupplier; +import org.elasticsearch.core.Nullable; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; @@ -146,26 +146,20 @@ public void preview(ActionListener> listener) { client, SearchAction.INSTANCE, searchRequestBuilder.request(), - ActionListener.wrap( - searchResponse -> { - if (searchResponse.getHits().getHits().length == 0) { - listener.onResponse(Collections.emptyList()); - return; - } - - final SearchHit[] hits = searchResponse.getHits().getHits(); - List rows = new ArrayList<>(hits.length); - for (SearchHit hit : hits) { - String[] extractedValues = extractValues(hit); - rows.add(extractedValues == null ? - new Row(null, hit, true) : - new Row(extractedValues, hit, false) - ); - } - listener.onResponse(rows); - }, - listener::onFailure - ) + ActionListener.wrap(searchResponse -> { + if (searchResponse.getHits().getHits().length == 0) { + listener.onResponse(Collections.emptyList()); + return; + } + + final SearchHit[] hits = searchResponse.getHits().getHits(); + List rows = new ArrayList<>(hits.length); + for (SearchHit hit : hits) { + String[] extractedValues = extractValues(hit); + rows.add(extractedValues == null ? new Row(null, hit, true) : new Row(extractedValues, hit, false)); + } + listener.onResponse(rows); + }, listener::onFailure) ); } @@ -205,15 +199,22 @@ private SearchRequestBuilder buildSearchRequest() { long from = lastSortKey + 1; long to = from + context.scrollSize; - LOGGER.trace(() -> new ParameterizedMessage( - "[{}] Searching docs with [{}] in [{}, {})", context.jobId, DestinationIndex.INCREMENTAL_ID, from, to)); + LOGGER.trace( + () -> new ParameterizedMessage( + "[{}] Searching docs with [{}] in [{}, {})", + context.jobId, + DestinationIndex.INCREMENTAL_ID, + from, + to + ) + ); SearchRequestBuilder searchRequestBuilder = new SearchRequestBuilder(client, SearchAction.INSTANCE) - // This ensures the search throws if there are failures and the scroll context gets cleared automatically - .setAllowPartialSearchResults(false) - .addSort(DestinationIndex.INCREMENTAL_ID, SortOrder.ASC) - .setIndices(context.indices) - .setSize(context.scrollSize); + // This ensures the search throws if there are failures and the scroll context gets cleared automatically + .setAllowPartialSearchResults(false) + .addSort(DestinationIndex.INCREMENTAL_ID, SortOrder.ASC) + .setIndices(context.indices) + .setSize(context.scrollSize); searchRequestBuilder.setQuery( QueryBuilders.boolQuery() @@ -298,7 +299,8 @@ private String[] extractProcessedValue(ProcessedField processedField, SearchHit "field_processor [{}] output size expected to be [{}], instead it was [{}]", processedField.getProcessorName(), processedField.getOutputFieldNames().size(), - values.length); + values.length + ); } for (int i = 0; i < processedField.getOutputFieldNames().size(); ++i) { @@ -323,8 +325,15 @@ private Row createRow(SearchHit hit) { } boolean isTraining = trainTestSplitter.get().isTraining(extractedValues); Row row = new Row(extractedValues, hit, isTraining); - LOGGER.trace(() -> new ParameterizedMessage("[{}] Extracted row: sort key = [{}], is_training = [{}], values = {}", - context.jobId, row.getSortKey(), isTraining, Arrays.toString(row.values))); + LOGGER.trace( + () -> new ParameterizedMessage( + "[{}] Extracted row: sort key = [{}], is_training = [{}], values = {}", + context.jobId, + row.getSortKey(), + isTraining, + Arrays.toString(row.values) + ) + ); return row; } @@ -376,29 +385,29 @@ public void collectDataSummaryAsync(ActionListener dataSummaryActio SearchRequestBuilder searchRequestBuilder = buildDataSummarySearchRequestBuilder(); final int numberOfFields = organicFeatures.length + processedFeatures.length; - ClientHelper.executeWithHeadersAsync(context.headers, + ClientHelper.executeWithHeadersAsync( + context.headers, ClientHelper.ML_ORIGIN, client, SearchAction.INSTANCE, searchRequestBuilder.request(), ActionListener.wrap( searchResponse -> dataSummaryActionListener.onResponse( - new DataSummary(searchResponse.getHits().getTotalHits().value, numberOfFields)), - dataSummaryActionListener::onFailure - )); + new DataSummary(searchResponse.getHits().getTotalHits().value, numberOfFields) + ), + dataSummaryActionListener::onFailure + ) + ); } private SearchRequestBuilder buildDataSummarySearchRequestBuilder() { QueryBuilder summaryQuery = context.query; if (context.supportsRowsWithMissingValues == false) { - summaryQuery = QueryBuilders.boolQuery() - .filter(summaryQuery) - .filter(allExtractedFieldsExistQuery()); + summaryQuery = QueryBuilders.boolQuery().filter(summaryQuery).filter(allExtractedFieldsExistQuery()); } - return new SearchRequestBuilder(client, SearchAction.INSTANCE) - .setAllowPartialSearchResults(false) + return new SearchRequestBuilder(client, SearchAction.INSTANCE).setAllowPartialSearchResults(false) .setIndices(context.indices) .setSize(0) .setQuery(summaryQuery) diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/extractor/DataFrameDataExtractorContext.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/extractor/DataFrameDataExtractorContext.java index 57fe032c107f4..3fa121f570bd0 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/extractor/DataFrameDataExtractorContext.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/extractor/DataFrameDataExtractorContext.java @@ -31,9 +31,18 @@ public class DataFrameDataExtractorContext { // fields should be mapped in the index. final Map runtimeMappings; - DataFrameDataExtractorContext(String jobId, ExtractedFields extractedFields, List indices, QueryBuilder query, int scrollSize, - Map headers, boolean includeSource, boolean supportsRowsWithMissingValues, - TrainTestSplitterFactory trainTestSplitterFactory, Map runtimeMappings) { + DataFrameDataExtractorContext( + String jobId, + ExtractedFields extractedFields, + List indices, + QueryBuilder query, + int scrollSize, + Map headers, + boolean includeSource, + boolean supportsRowsWithMissingValues, + TrainTestSplitterFactory trainTestSplitterFactory, + Map runtimeMappings + ) { this.jobId = Objects.requireNonNull(jobId); this.extractedFields = Objects.requireNonNull(extractedFields); this.indices = indices.toArray(new String[indices.size()]); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/extractor/DataFrameDataExtractorFactory.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/extractor/DataFrameDataExtractorFactory.java index 9786e04655e3f..712e08143a37b 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/extractor/DataFrameDataExtractorFactory.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/extractor/DataFrameDataExtractorFactory.java @@ -37,10 +37,18 @@ public class DataFrameDataExtractorFactory { private final TrainTestSplitterFactory trainTestSplitterFactory; private final Map runtimeMappings; - private DataFrameDataExtractorFactory(Client client, String analyticsId, List indices, QueryBuilder sourceQuery, - ExtractedFields extractedFields, List requiredFields, Map headers, - boolean supportsRowsWithMissingValues, TrainTestSplitterFactory trainTestSplitterFactory, - Map runtimeMappings) { + private DataFrameDataExtractorFactory( + Client client, + String analyticsId, + List indices, + QueryBuilder sourceQuery, + ExtractedFields extractedFields, + List requiredFields, + Map headers, + boolean supportsRowsWithMissingValues, + TrainTestSplitterFactory trainTestSplitterFactory, + Map runtimeMappings + ) { this.client = Objects.requireNonNull(client); this.analyticsId = Objects.requireNonNull(analyticsId); this.indices = Objects.requireNonNull(indices); @@ -55,17 +63,17 @@ private DataFrameDataExtractorFactory(Client client, String analyticsId, List listener) { + public static void createForDestinationIndex( + Client client, + DataFrameAnalyticsConfig config, + ActionListener listener + ) { ExtractedFieldsDetectorFactory extractedFieldsDetectorFactory = new ExtractedFieldsDetectorFactory(client); - extractedFieldsDetectorFactory.createFromDest(config, ActionListener.wrap( - extractedFieldsDetector -> { - ExtractedFields extractedFields = extractedFieldsDetector.detect().v1(); + extractedFieldsDetectorFactory.createFromDest(config, ActionListener.wrap(extractedFieldsDetector -> { + ExtractedFields extractedFields = extractedFieldsDetector.detect().v1(); - DataFrameDataExtractorFactory extractorFactory = new DataFrameDataExtractorFactory(client, config.getId(), - Collections.singletonList(config.getDest().getIndex()), config.getSource().getParsedQuery(), extractedFields, - config.getAnalysis().getRequiredFields(), config.getHeaders(), config.getAnalysis().supportsMissingValues(), - createTrainTestSplitterFactory(client, config, extractedFields), Collections.emptyMap()); - listener.onResponse(extractorFactory); - }, - listener::onFailure - )); + DataFrameDataExtractorFactory extractorFactory = new DataFrameDataExtractorFactory( + client, + config.getId(), + Collections.singletonList(config.getDest().getIndex()), + config.getSource().getParsedQuery(), + extractedFields, + config.getAnalysis().getRequiredFields(), + config.getHeaders(), + config.getAnalysis().supportsMissingValues(), + createTrainTestSplitterFactory(client, config, extractedFields), + Collections.emptyMap() + ); + listener.onResponse(extractorFactory); + }, listener::onFailure)); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/extractor/ExtractedFieldsDetector.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/extractor/ExtractedFieldsDetector.java index 0844d142359c7..f4c0d047231fb 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/extractor/ExtractedFieldsDetector.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/extractor/ExtractedFieldsDetector.java @@ -12,9 +12,9 @@ import org.elasticsearch.action.fieldcaps.FieldCapabilities; import org.elasticsearch.action.fieldcaps.FieldCapabilitiesResponse; import org.elasticsearch.common.Strings; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.regex.Regex; import org.elasticsearch.common.util.set.Sets; +import org.elasticsearch.core.Tuple; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.mapper.BooleanFieldMapper; import org.elasticsearch.index.mapper.NestedObjectMapper; @@ -69,10 +69,12 @@ public class ExtractedFieldsDetector { private final Map cardinalitiesForFieldsWithConstraints; private final List topNestedFieldPrefixes; - ExtractedFieldsDetector(DataFrameAnalyticsConfig config, - int docValueFieldsLimit, - FieldCapabilitiesResponse fieldCapabilitiesResponse, - Map cardinalitiesForFieldsWithConstraints) { + ExtractedFieldsDetector( + DataFrameAnalyticsConfig config, + int docValueFieldsLimit, + FieldCapabilitiesResponse fieldCapabilitiesResponse, + Map cardinalitiesForFieldsWithConstraints + ) { this.config = Objects.requireNonNull(config); this.docValueFieldsLimit = docValueFieldsLimit; this.fieldCapabilitiesResponse = Objects.requireNonNull(fieldCapabilitiesResponse); @@ -81,7 +83,9 @@ public class ExtractedFieldsDetector { } private List findTopNestedFieldPrefixes(FieldCapabilitiesResponse fieldCapabilitiesResponse) { - List sortedNestedFieldPrefixes = fieldCapabilitiesResponse.get().keySet().stream() + List sortedNestedFieldPrefixes = fieldCapabilitiesResponse.get() + .keySet() + .stream() .filter(field -> isNested(getMappingTypes(field))) .map(field -> field + ".") .sorted() @@ -100,16 +104,12 @@ private List findTopNestedFieldPrefixes(FieldCapabilitiesResponse fieldC } public Tuple> detect() { - List processedFields = extractFeatureProcessors() - .stream() - .map(ProcessedField::new) - .collect(Collectors.toList()); + List processedFields = extractFeatureProcessors().stream().map(ProcessedField::new).collect(Collectors.toList()); TreeSet fieldSelection = new TreeSet<>(Comparator.comparing(FieldSelection::getName)); - Set fields = getIncludedFields(fieldSelection, - processedFields.stream() - .map(ProcessedField::getInputFieldNames) - .flatMap(List::stream) - .collect(Collectors.toSet())); + Set fields = getIncludedFields( + fieldSelection, + processedFields.stream().map(ProcessedField::getInputFieldNames).flatMap(List::stream).collect(Collectors.toSet()) + ); checkFieldsHaveCompatibleTypes(fields); checkRequiredFields(fields); checkFieldsWithCardinalityLimit(); @@ -125,9 +125,10 @@ private Set getIncludedFields(Set fieldSelection, Set fields = new TreeSet<>(); // filter metadata field - fieldCapabilitiesResponse.get().keySet().stream() - .filter(f -> fieldCapabilitiesResponse.isMetadataField(f) == false - && IGNORE_FIELDS.contains(f) == false) + fieldCapabilitiesResponse.get() + .keySet() + .stream() + .filter(f -> fieldCapabilitiesResponse.isMetadataField(f) == false && IGNORE_FIELDS.contains(f) == false) .forEach(fields::add); removeFieldsUnderResultsField(fields); removeObjects(fields); @@ -135,7 +136,8 @@ private Set getIncludedFields(Set fieldSelection, Set getIncludedFields(Set fieldSelection, Set processorFields) { Set fieldsForProcessor = new HashSet<>(processorFields); removeFieldsUnderResultsField(fieldsForProcessor); if (fieldsForProcessor.size() < processorFields.size()) { - throw ExceptionsHelper.badRequestException("fields contained in results field [{}] cannot be used in a feature_processor", - config.getDest().getResultsField()); + throw ExceptionsHelper.badRequestException( + "fields contained in results field [{}] cannot be used in a feature_processor", + config.getDest().getResultsField() + ); } removeObjects(fieldsForProcessor); if (fieldsForProcessor.size() < processorFields.size()) { @@ -167,8 +172,10 @@ private void validateFieldsRequireForProcessors(Set processorFields) { for (String field : fieldsForProcessor) { Optional matchingNestedFieldPattern = findMatchingNestedFieldPattern(field); if (matchingNestedFieldPattern.isPresent()) { - throw ExceptionsHelper.badRequestException("nested fields [{}] cannot be used in a feature_processor", - matchingNestedFieldPattern.get()); + throw ExceptionsHelper.badRequestException( + "nested fields [{}] cannot be used in a feature_processor", + matchingNestedFieldPattern.get() + ); } } Collection errorFields = new ArrayList<>(); @@ -188,7 +195,8 @@ private void validateFieldsRequireForProcessors(Set processorFields) { "the fields {} were not found in the field capabilities of the source indices [{}]. " + "Fields must exist and be mapped to be used in feature_processors.", fieldsMissingInMapping, - Strings.arrayToCommaDelimitedString(config.getSource().getIndex())); + Strings.arrayToCommaDelimitedString(config.getSource().getIndex()) + ); } List processedRequiredFields = config.getAnalysis() .getRequiredFields() @@ -199,7 +207,8 @@ private void validateFieldsRequireForProcessors(Set processorFields) { if (processedRequiredFields.isEmpty() == false) { throw ExceptionsHelper.badRequestException( "required analysis fields {} cannot be used in a feature_processor", - processedRequiredFields); + processedRequiredFields + ); } } @@ -241,8 +250,9 @@ private void addExcludedField(String field, String reason, Set f } private void addExcludedNestedPattern(String pattern, Set fieldSelection) { - fieldSelection.add(FieldSelection.excluded( - pattern, Collections.singleton(NestedObjectMapper.CONTENT_TYPE), "nested fields are not supported")); + fieldSelection.add( + FieldSelection.excluded(pattern, Collections.singleton(NestedObjectMapper.CONTENT_TYPE), "nested fields are not supported") + ); } private Set getMappingTypes(String field) { @@ -317,16 +327,16 @@ private void includeAndExcludeFields(Set fields, Set fie try { // If the inclusion set does not match anything, that means the user's desired fields cannot be found in // the collection of supported field types. We should let the user know. - Set includedSet = NameResolver.newUnaliased(fields, - (ex) -> new ResourceNotFoundException( - Messages.getMessage(Messages.DATA_FRAME_ANALYTICS_BAD_FIELD_FILTER, ex))) - .expand(includes, false); + Set includedSet = NameResolver.newUnaliased( + fields, + (ex) -> new ResourceNotFoundException(Messages.getMessage(Messages.DATA_FRAME_ANALYTICS_BAD_FIELD_FILTER, ex)) + ).expand(includes, false); // If the exclusion set does not match anything, that means the fields are already not present // no need to raise if nothing matched - Set excludedSet = NameResolver.newUnaliased(fieldCapabilitiesResponse.get().keySet(), - (ex) -> new ResourceNotFoundException( - Messages.getMessage(Messages.DATA_FRAME_ANALYTICS_BAD_FIELD_FILTER, ex))) - .expand(excludes, true); + Set excludedSet = NameResolver.newUnaliased( + fieldCapabilitiesResponse.get().keySet(), + (ex) -> new ResourceNotFoundException(Messages.getMessage(Messages.DATA_FRAME_ANALYTICS_BAD_FIELD_FILTER, ex)) + ).expand(excludes, true); applyIncludesExcludes(fields, includedSet, excludedSet, fieldSelection); } catch (ResourceNotFoundException ex) { @@ -340,13 +350,15 @@ private void checkIncludesExcludesAreNotObjects(FetchSourceContext analyzedField .filter(field -> isObject(getMappingTypes(field)) || isNested(getMappingTypes(field))) .collect(Collectors.toList()); if (objectFields.isEmpty() == false) { - throw ExceptionsHelper.badRequestException("{} must not include or exclude object or nested fields: {}", - DataFrameAnalyticsConfig.ANALYZED_FIELDS.getPreferredName(), objectFields); + throw ExceptionsHelper.badRequestException( + "{} must not include or exclude object or nested fields: {}", + DataFrameAnalyticsConfig.ANALYZED_FIELDS.getPreferredName(), + objectFields + ); } } - private void applyIncludesExcludes(Set fields, Set includes, Set excludes, - Set fieldSelection) { + private void applyIncludesExcludes(Set fields, Set includes, Set excludes, Set fieldSelection) { Iterator fieldsIterator = fields.iterator(); while (fieldsIterator.hasNext()) { String field = fieldsIterator.next(); @@ -382,8 +394,12 @@ private void checkFieldsHaveCompatibleTypes(Set fields) { } if (hasCompatibleType(field) == false) { - throw ExceptionsHelper.badRequestException("field [{}] has unsupported type {}. Supported types are {}.", field, - fieldCaps.keySet(), getSupportedTypes()); + throw ExceptionsHelper.badRequestException( + "field [{}] has unsupported type {}. Supported types are {}.", + field, + fieldCaps.keySet(), + getSupportedTypes() + ); } Optional matchingNestedFieldPattern = findMatchingNestedFieldPattern(field); if (matchingNestedFieldPattern.isPresent()) { @@ -398,13 +414,20 @@ private void checkRequiredFields(Set fields) { Map fieldCaps = fieldCapabilitiesResponse.getField(requiredField.getName()); if (fields.contains(requiredField.getName()) == false || fieldCaps == null || fieldCaps.isEmpty()) { List requiredFieldNames = requiredFields.stream().map(RequiredField::getName).collect(Collectors.toList()); - throw ExceptionsHelper.badRequestException("required field [{}] is missing; analysis requires fields {}", - requiredField.getName(), requiredFieldNames); + throw ExceptionsHelper.badRequestException( + "required field [{}] is missing; analysis requires fields {}", + requiredField.getName(), + requiredFieldNames + ); } Set fieldTypes = fieldCaps.keySet(); if (requiredField.getTypes().containsAll(fieldTypes) == false) { - throw ExceptionsHelper.badRequestException("invalid types {} for required field [{}]; expected types are {}", - fieldTypes, requiredField.getName(), requiredField.getTypes()); + throw ExceptionsHelper.badRequestException( + "invalid types {} for required field [{}]; expected types are {}", + fieldTypes, + requiredField.getName(), + requiredField.getTypes() + ); } } } @@ -417,41 +440,48 @@ private void checkFieldsWithCardinalityLimit() { private List extractFeatureProcessors() { if (config.getAnalysis() instanceof Classification) { - return ((Classification)config.getAnalysis()).getFeatureProcessors(); + return ((Classification) config.getAnalysis()).getFeatureProcessors(); } else if (config.getAnalysis() instanceof Regression) { - return ((Regression)config.getAnalysis()).getFeatureProcessors(); + return ((Regression) config.getAnalysis()).getFeatureProcessors(); } return Collections.emptyList(); } - private ExtractedFields detectExtractedFields(Set fields, - Set fieldSelection, - List processedFields) { - ExtractedFields extractedFields = ExtractedFields.build(fields, + private ExtractedFields detectExtractedFields( + Set fields, + Set fieldSelection, + List processedFields + ) { + ExtractedFields extractedFields = ExtractedFields.build( + fields, Collections.emptySet(), Collections.emptySet(), fieldCapabilitiesResponse, cardinalitiesForFieldsWithConstraints, - processedFields); + processedFields + ); boolean preferSource = extractedFields.getDocValueFields().size() > docValueFieldsLimit; extractedFields = deduplicateMultiFields(extractedFields, preferSource, fieldSelection); if (preferSource) { extractedFields = fetchFromSourceIfSupported(extractedFields); if (extractedFields.getDocValueFields().size() > docValueFieldsLimit) { throw ExceptionsHelper.badRequestException( - "[{}] fields must be retrieved from doc_values and this is greater than the configured limit. " + - "Please adjust the index level setting [{}]", + "[{}] fields must be retrieved from doc_values and this is greater than the configured limit. " + + "Please adjust the index level setting [{}]", extractedFields.getDocValueFields().size(), - IndexSettings.MAX_DOCVALUE_FIELDS_SEARCH_SETTING.getKey()); + IndexSettings.MAX_DOCVALUE_FIELDS_SEARCH_SETTING.getKey() + ); } } extractedFields = fetchBooleanFieldsAsIntegers(extractedFields); return extractedFields; } - private ExtractedFields deduplicateMultiFields(ExtractedFields extractedFields, - boolean preferSource, - Set fieldSelection) { + private ExtractedFields deduplicateMultiFields( + ExtractedFields extractedFields, + boolean preferSource, + Set fieldSelection + ) { Set requiredFields = config.getAnalysis() .getRequiredFields() .stream() @@ -472,30 +502,38 @@ private ExtractedFields deduplicateMultiFields(ExtractedFields extractedFields, throw ExceptionsHelper.badRequestException( "feature_processors cannot be applied to required fields for analysis; multi-field [{}] parent [{}]", multiField.getName(), - parent.getName()); + parent.getName() + ); } // If processor input fields have BOTH, we need to keep both. if (processorInputFields.contains(parent.getName()) && processorInputFields.contains(multiField.getName())) { throw ExceptionsHelper.badRequestException( "feature_processors refer to both multi-field [{}] and parent [{}]. Please only refer to one or the other", multiField.getName(), - parent.getName()); + parent.getName() + ); } - nameOrParentToField.put(nameOrParent, - chooseMultiFieldOrParent(preferSource, requiredFields, processorInputFields, parent, multiField, fieldSelection)); + nameOrParentToField.put( + nameOrParent, + chooseMultiFieldOrParent(preferSource, requiredFields, processorInputFields, parent, multiField, fieldSelection) + ); } } - return new ExtractedFields(new ArrayList<>(nameOrParentToField.values()), + return new ExtractedFields( + new ArrayList<>(nameOrParentToField.values()), extractedFields.getProcessedFields(), - cardinalitiesForFieldsWithConstraints); + cardinalitiesForFieldsWithConstraints + ); } - private ExtractedField chooseMultiFieldOrParent(boolean preferSource, - Set requiredFields, - Set processorInputFields, - ExtractedField parent, - ExtractedField multiField, - Set fieldSelection) { + private ExtractedField chooseMultiFieldOrParent( + boolean preferSource, + Set requiredFields, + Set processorInputFields, + ExtractedField parent, + ExtractedField multiField, + Set fieldSelection + ) { // Check requirements first if (requiredFields.contains(parent.getName())) { addExcludedField(multiField.getName(), "[" + parent.getName() + "] is required instead", fieldSelection); @@ -507,15 +545,19 @@ private ExtractedField chooseMultiFieldOrParent(boolean preferSource, } // Choose the one required by our processors if (processorInputFields.contains(parent.getName())) { - addExcludedField(multiField.getName(), + addExcludedField( + multiField.getName(), "[" + parent.getName() + "] is referenced by feature_processors instead", - fieldSelection); + fieldSelection + ); return parent; } if (processorInputFields.contains(multiField.getName())) { - addExcludedField(parent.getName(), + addExcludedField( + parent.getName(), "[" + multiField.getName() + "] is referenced by feature_processors instead", - fieldSelection); + fieldSelection + ); return multiField; } @@ -528,8 +570,11 @@ private ExtractedField chooseMultiFieldOrParent(boolean preferSource, // If we prefer source only the parent may support it. If it does we pick it immediately. if (preferSource && parent.supportsFromSource()) { - addExcludedField(multiField.getName(), "[" + parent.getName() + "] is preferred because it supports fetching from source", - fieldSelection); + addExcludedField( + multiField.getName(), + "[" + parent.getName() + "] is preferred because it supports fetching from source", + fieldSelection + ); return parent; } @@ -545,8 +590,11 @@ private ExtractedField chooseMultiFieldOrParent(boolean preferSource, } // None is aggregatable. Let's pick the parent for its shorter name. - addExcludedField(multiField.getName(), "[" + parent.getName() + "] is preferred because none of the multi-fields are aggregatable", - fieldSelection); + addExcludedField( + multiField.getName(), + "[" + parent.getName() + "] is preferred because none of the multi-fields are aggregatable", + fieldSelection + ); return parent; } @@ -555,9 +603,7 @@ private ExtractedFields fetchFromSourceIfSupported(ExtractedFields extractedFiel for (ExtractedField field : extractedFields.getAllFields()) { adjusted.add(field.supportsFromSource() ? field.newFromSource() : field); } - return new ExtractedFields(adjusted, - extractedFields.getProcessedFields(), - cardinalitiesForFieldsWithConstraints); + return new ExtractedFields(adjusted, extractedFields.getProcessedFields(), cardinalitiesForFieldsWithConstraints); } private ExtractedFields fetchBooleanFieldsAsIntegers(ExtractedFields extractedFields) { @@ -566,28 +612,36 @@ private ExtractedFields fetchBooleanFieldsAsIntegers(ExtractedFields extractedFi if (isBoolean(field.getTypes())) { // We convert boolean fields to integers with values 0, 1 as this is the preferred // way to consume such features in the analytics process regardless of: - // - analysis type - // - whether or not the field is categorical - // - whether or not the field is a dependent variable + // - analysis type + // - whether or not the field is categorical + // - whether or not the field is a dependent variable adjusted.add(ExtractedFields.applyBooleanMapping(field)); } else { adjusted.add(field); } } - return new ExtractedFields(adjusted, - extractedFields.getProcessedFields(), - cardinalitiesForFieldsWithConstraints); + return new ExtractedFields(adjusted, extractedFields.getProcessedFields(), cardinalitiesForFieldsWithConstraints); } private void addIncludedFields(ExtractedFields extractedFields, Set fieldSelection) { - Set requiredFields = config.getAnalysis().getRequiredFields().stream().map(RequiredField::getName) + Set requiredFields = config.getAnalysis() + .getRequiredFields() + .stream() + .map(RequiredField::getName) .collect(Collectors.toSet()); Set categoricalFields = getCategoricalInputFields(extractedFields, config.getAnalysis()); for (ExtractedField includedField : extractedFields.getAllFields()) { - FieldSelection.FeatureType featureType = categoricalFields.contains(includedField.getName()) ? - FieldSelection.FeatureType.CATEGORICAL : FieldSelection.FeatureType.NUMERICAL; - fieldSelection.add(FieldSelection.included(includedField.getName(), includedField.getTypes(), - requiredFields.contains(includedField.getName()), featureType)); + FieldSelection.FeatureType featureType = categoricalFields.contains(includedField.getName()) + ? FieldSelection.FeatureType.CATEGORICAL + : FieldSelection.FeatureType.NUMERICAL; + fieldSelection.add( + FieldSelection.included( + includedField.getName(), + includedField.getTypes(), + requiredFields.contains(includedField.getName()), + featureType + ) + ); } } @@ -612,40 +666,40 @@ static void checkOutputFeatureUniqueness(List processedFields, S if (duplicatedFields.isEmpty() == false) { throw ExceptionsHelper.badRequestException( "feature_processors must define unique output field names; duplicate fields {}", - duplicatedFields); + duplicatedFields + ); } Set duplicateOrganicAndProcessed = Sets.intersection(organicFields, processedFeatures); if (duplicateOrganicAndProcessed.isEmpty() == false) { throw ExceptionsHelper.badRequestException( "feature_processors output fields must not include non-processed analysis fields; duplicate fields {}", - duplicateOrganicAndProcessed); + duplicateOrganicAndProcessed + ); } } static Set getCategoricalInputFields(ExtractedFields extractedFields, DataFrameAnalysis analysis) { - return extractedFields.getAllFields().stream() - .filter(extractedField -> analysis.getAllowedCategoricalTypes(extractedField.getName()) - .containsAll(extractedField.getTypes())) + return extractedFields.getAllFields() + .stream() + .filter(extractedField -> analysis.getAllowedCategoricalTypes(extractedField.getName()).containsAll(extractedField.getTypes())) .map(ExtractedField::getName) .collect(Collectors.toSet()); } static Set getCategoricalOutputFields(ExtractedFields extractedFields, DataFrameAnalysis analysis) { Set processInputFields = extractedFields.getProcessedFieldInputs(); - Set categoricalFields = extractedFields.getAllFields().stream() - .filter(extractedField -> analysis.getAllowedCategoricalTypes(extractedField.getName()) - .containsAll(extractedField.getTypes())) + Set categoricalFields = extractedFields.getAllFields() + .stream() + .filter(extractedField -> analysis.getAllowedCategoricalTypes(extractedField.getName()).containsAll(extractedField.getTypes())) .map(ExtractedField::getName) .filter(name -> processInputFields.contains(name) == false) .collect(Collectors.toSet()); - extractedFields.getProcessedFields().forEach(processedField -> - processedField.getOutputFieldNames().forEach(outputField -> { - if (analysis.getAllowedCategoricalTypes(outputField).containsAll(processedField.getOutputFieldType(outputField))) { - categoricalFields.add(outputField); - } - }) - ); + extractedFields.getProcessedFields().forEach(processedField -> processedField.getOutputFieldNames().forEach(outputField -> { + if (analysis.getAllowedCategoricalTypes(outputField).containsAll(processedField.getOutputFieldType(outputField))) { + categoricalFields.add(outputField); + } + })); return Collections.unmodifiableSet(categoricalFields); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/extractor/ExtractedFieldsDetectorFactory.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/extractor/ExtractedFieldsDetectorFactory.java index ee4e8bfa1c474..9ef0674b8a4da 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/extractor/ExtractedFieldsDetectorFactory.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/extractor/ExtractedFieldsDetectorFactory.java @@ -7,6 +7,7 @@ package org.elasticsearch.xpack.ml.dataframe.extractor; import com.carrotsearch.hppc.cursors.ObjectObjectCursor; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.message.ParameterizedMessage; @@ -68,7 +69,7 @@ public void createFromSource(DataFrameAnalyticsConfig config, ActionListener listener) { - create(new String[] {config.getDest().getIndex()}, config, listener); + create(new String[] { config.getDest().getIndex() }, config, listener); } private void create(String[] index, DataFrameAnalyticsConfig config, ActionListener listener) { @@ -76,43 +77,39 @@ private void create(String[] index, DataFrameAnalyticsConfig config, ActionListe AtomicReference fieldCapsResponseHolder = new AtomicReference<>(); // Step 4. Create cardinality by field map and build detector - ActionListener> fieldCardinalitiesHandler = ActionListener.wrap( - fieldCardinalities -> { - ExtractedFieldsDetector detector = - new ExtractedFieldsDetector(config, docValueFieldsLimitHolder.get(), fieldCapsResponseHolder.get(), fieldCardinalities); - listener.onResponse(detector); - }, - listener::onFailure - ); + ActionListener> fieldCardinalitiesHandler = ActionListener.wrap(fieldCardinalities -> { + ExtractedFieldsDetector detector = new ExtractedFieldsDetector( + config, + docValueFieldsLimitHolder.get(), + fieldCapsResponseHolder.get(), + fieldCardinalities + ); + listener.onResponse(detector); + }, listener::onFailure); // Step 3. Get cardinalities for fields with constraints - ActionListener fieldCapabilitiesHandler = ActionListener.wrap( - fieldCapabilitiesResponse -> { - LOGGER.debug(() -> new ParameterizedMessage( - "[{}] Field capabilities response: {}", config.getId(), fieldCapabilitiesResponse)); - fieldCapsResponseHolder.set(fieldCapabilitiesResponse); - getCardinalitiesForFieldsWithConstraints(index, config, fieldCapabilitiesResponse, fieldCardinalitiesHandler); - }, - listener::onFailure - ); + ActionListener fieldCapabilitiesHandler = ActionListener.wrap(fieldCapabilitiesResponse -> { + LOGGER.debug(() -> new ParameterizedMessage("[{}] Field capabilities response: {}", config.getId(), fieldCapabilitiesResponse)); + fieldCapsResponseHolder.set(fieldCapabilitiesResponse); + getCardinalitiesForFieldsWithConstraints(index, config, fieldCapabilitiesResponse, fieldCardinalitiesHandler); + }, listener::onFailure); // Step 2. Get field capabilities necessary to build the information of how to extract fields - ActionListener docValueFieldsLimitListener = ActionListener.wrap( - docValueFieldsLimit -> { - docValueFieldsLimitHolder.set(docValueFieldsLimit); - getFieldCaps(index, config, fieldCapabilitiesHandler); - }, - listener::onFailure - ); + ActionListener docValueFieldsLimitListener = ActionListener.wrap(docValueFieldsLimit -> { + docValueFieldsLimitHolder.set(docValueFieldsLimit); + getFieldCaps(index, config, fieldCapabilitiesHandler); + }, listener::onFailure); // Step 1. Get doc value fields limit getDocValueFieldsLimit(index, docValueFieldsLimitListener); } - private void getCardinalitiesForFieldsWithConstraints(String[] index, - DataFrameAnalyticsConfig config, - FieldCapabilitiesResponse fieldCapabilitiesResponse, - ActionListener> listener) { + private void getCardinalitiesForFieldsWithConstraints( + String[] index, + DataFrameAnalyticsConfig config, + FieldCapabilitiesResponse fieldCapabilitiesResponse, + ActionListener> listener + ) { List fieldCardinalityConstraints = config.getAnalysis().getFieldCardinalityConstraints(); if (fieldCardinalityConstraints.isEmpty()) { listener.onResponse(Collections.emptyMap()); @@ -124,8 +121,7 @@ private void getCardinalitiesForFieldsWithConstraints(String[] index, listener::onFailure ); - SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder() - .size(0) + SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().size(0) .query(config.getSource().getParsedQuery()) .runtimeMappings(config.getSource().getRuntimeMappings()); for (FieldCardinalityConstraint constraint : fieldCardinalityConstraints) { @@ -135,22 +131,28 @@ private void getCardinalitiesForFieldsWithConstraints(String[] index, } for (FieldCapabilities fieldCaps : fieldCapsPerType.values()) { if (fieldCaps.isAggregatable() == false) { - throw ExceptionsHelper.badRequestException("field [{}] of type [{}] is non-aggregatable", - fieldCaps.getName(), fieldCaps.getType()); + throw ExceptionsHelper.badRequestException( + "field [{}] of type [{}] is non-aggregatable", + fieldCaps.getName(), + fieldCaps.getType() + ); } } searchSourceBuilder.aggregation( AggregationBuilders.cardinality(constraint.getField()) .field(constraint.getField()) - .precisionThreshold(constraint.getUpperBound() + 1)); + .precisionThreshold(constraint.getUpperBound() + 1) + ); } SearchRequest searchRequest = new SearchRequest(index).source(searchSourceBuilder); - ClientHelper.executeWithHeadersAsync( - config.getHeaders(), ML_ORIGIN, client, SearchAction.INSTANCE, searchRequest, searchListener); + ClientHelper.executeWithHeadersAsync(config.getHeaders(), ML_ORIGIN, client, SearchAction.INSTANCE, searchRequest, searchListener); } - private void buildFieldCardinalitiesMap(DataFrameAnalyticsConfig config, SearchResponse searchResponse, - ActionListener> listener) { + private void buildFieldCardinalitiesMap( + DataFrameAnalyticsConfig config, + SearchResponse searchResponse, + ActionListener> listener + ) { Aggregations aggs = searchResponse.getAggregations(); if (aggs == null) { listener.onFailure(ExceptionsHelper.serverError("Unexpected null response when gathering field cardinalities")); @@ -175,8 +177,7 @@ private void getFieldCaps(String[] index, DataFrameAnalyticsConfig config, Actio fieldCapabilitiesRequest.indicesOptions(IndicesOptions.lenientExpandOpen()); fieldCapabilitiesRequest.fields("*"); fieldCapabilitiesRequest.runtimeFields(config.getSource().getRuntimeMappings()); - LOGGER.debug(() -> new ParameterizedMessage( - "[{}] Requesting field caps for index {}", config.getId(), Arrays.toString(index))); + LOGGER.debug(() -> new ParameterizedMessage("[{}] Requesting field caps for index {}", config.getId(), Arrays.toString(index))); ClientHelper.executeWithHeaders(config.getHeaders(), ML_ORIGIN, client, () -> { client.execute(FieldCapabilitiesAction.INSTANCE, fieldCapabilitiesRequest, listener); // This response gets discarded - the listener handles the real response @@ -186,29 +187,30 @@ private void getFieldCaps(String[] index, DataFrameAnalyticsConfig config, Actio private void getDocValueFieldsLimit(String[] index, ActionListener docValueFieldsLimitListener) { ActionListener settingsListener = ActionListener.wrap(getSettingsResponse -> { - Integer minDocValueFieldsLimit = Integer.MAX_VALUE; - - ImmutableOpenMap indexToSettings = getSettingsResponse.getIndexToSettings(); - Iterator> iterator = indexToSettings.iterator(); - while (iterator.hasNext()) { - ObjectObjectCursor indexSettings = iterator.next(); - Integer indexMaxDocValueFields = IndexSettings.MAX_DOCVALUE_FIELDS_SEARCH_SETTING.get(indexSettings.value); - if (indexMaxDocValueFields < minDocValueFieldsLimit) { - minDocValueFieldsLimit = indexMaxDocValueFields; - } - } - docValueFieldsLimitListener.onResponse(minDocValueFieldsLimit); - }, - e -> { - Throwable cause = ExceptionsHelper.unwrapCause(e); - if (cause instanceof IndexNotFoundException) { - docValueFieldsLimitListener.onFailure(new ResourceNotFoundException("cannot retrieve data because index " - + ((IndexNotFoundException) cause).getIndex() + " does not exist")); - } else { - docValueFieldsLimitListener.onFailure(e); + Integer minDocValueFieldsLimit = Integer.MAX_VALUE; + + ImmutableOpenMap indexToSettings = getSettingsResponse.getIndexToSettings(); + Iterator> iterator = indexToSettings.iterator(); + while (iterator.hasNext()) { + ObjectObjectCursor indexSettings = iterator.next(); + Integer indexMaxDocValueFields = IndexSettings.MAX_DOCVALUE_FIELDS_SEARCH_SETTING.get(indexSettings.value); + if (indexMaxDocValueFields < minDocValueFieldsLimit) { + minDocValueFieldsLimit = indexMaxDocValueFields; } } - ); + docValueFieldsLimitListener.onResponse(minDocValueFieldsLimit); + }, e -> { + Throwable cause = ExceptionsHelper.unwrapCause(e); + if (cause instanceof IndexNotFoundException) { + docValueFieldsLimitListener.onFailure( + new ResourceNotFoundException( + "cannot retrieve data because index " + ((IndexNotFoundException) cause).getIndex() + " does not exist" + ) + ); + } else { + docValueFieldsLimitListener.onFailure(e); + } + }); GetSettingsRequest getSettingsRequest = new GetSettingsRequest(); getSettingsRequest.indices(index); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/inference/InferenceRunner.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/inference/InferenceRunner.java index ceea0ae5fb07d..52d619a70bc34 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/inference/InferenceRunner.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/inference/InferenceRunner.java @@ -19,8 +19,8 @@ import org.elasticsearch.action.support.PlainActionFuture; import org.elasticsearch.client.Client; import org.elasticsearch.client.OriginSettingClient; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.Nullable; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.aggregations.AggregationBuilders; @@ -65,9 +65,17 @@ public class InferenceRunner { private final DataCountsTracker dataCountsTracker; private volatile boolean isCancelled; - public InferenceRunner(Settings settings, Client client, ModelLoadingService modelLoadingService, - ResultsPersisterService resultsPersisterService, TaskId parentTaskId, DataFrameAnalyticsConfig config, - ExtractedFields extractedFields, ProgressTracker progressTracker, DataCountsTracker dataCountsTracker) { + public InferenceRunner( + Settings settings, + Client client, + ModelLoadingService modelLoadingService, + ResultsPersisterService resultsPersisterService, + TaskId parentTaskId, + DataFrameAnalyticsConfig config, + ExtractedFields extractedFields, + ProgressTracker progressTracker, + DataCountsTracker dataCountsTracker + ) { this.settings = Objects.requireNonNull(settings); this.client = Objects.requireNonNull(client); this.modelLoadingService = Objects.requireNonNull(modelLoadingService); @@ -94,8 +102,12 @@ public void run(String modelId) { modelLoadingService.getModelForInternalInference(modelId, localModelPlainActionFuture); InferenceState inferenceState = restoreInferenceState(); dataCountsTracker.setTestDocsCount(inferenceState.processedTestDocsCount); - TestDocsIterator testDocsIterator = new TestDocsIterator(new OriginSettingClient(client, ClientHelper.ML_ORIGIN), config, - extractedFields, inferenceState.lastIncrementalId); + TestDocsIterator testDocsIterator = new TestDocsIterator( + new OriginSettingClient(client, ClientHelper.ML_ORIGIN), + config, + extractedFields, + inferenceState.lastIncrementalId + ); try (LocalModel localModel = localModelPlainActionFuture.actionGet()) { LOGGER.debug("Loaded inference model [{}]", localModel); inferTestDocs(localModel, testDocsIterator, inferenceState.processedTestDocsCount); @@ -105,36 +117,56 @@ public void run(String modelId) { if (e instanceof ElasticsearchException) { Throwable rootCause = ((ElasticsearchException) e).getRootCause(); - throw new ElasticsearchException("[{}] failed running inference on model [{}]; cause was [{}]", rootCause, config.getId(), - modelId, rootCause.getMessage()); + throw new ElasticsearchException( + "[{}] failed running inference on model [{}]; cause was [{}]", + rootCause, + config.getId(), + modelId, + rootCause.getMessage() + ); } - throw ExceptionsHelper.serverError("[{}] failed running inference on model [{}]; cause was [{}]", e, config.getId(), modelId, - e.getMessage()); + throw ExceptionsHelper.serverError( + "[{}] failed running inference on model [{}]; cause was [{}]", + e, + config.getId(), + modelId, + e.getMessage() + ); } } private InferenceState restoreInferenceState() { SearchRequest searchRequest = new SearchRequest(config.getDest().getIndex()); searchRequest.indicesOptions(MlIndicesUtils.addIgnoreUnavailable(SearchRequest.DEFAULT_INDICES_OPTIONS)); - SearchSourceBuilder sourceBuilder = (new SearchSourceBuilder() - .size(0) - .query(QueryBuilders.boolQuery().filter( - QueryBuilders.termQuery(config.getDest().getResultsField() + "." + DestinationIndex.IS_TRAINING, false))) + SearchSourceBuilder sourceBuilder = (new SearchSourceBuilder().size(0) + .query( + QueryBuilders.boolQuery() + .filter(QueryBuilders.termQuery(config.getDest().getResultsField() + "." + DestinationIndex.IS_TRAINING, false)) + ) .fetchSource(false) .aggregation(AggregationBuilders.max(DestinationIndex.INCREMENTAL_ID).field(DestinationIndex.INCREMENTAL_ID)) - .trackTotalHits(true) - ); + .trackTotalHits(true)); searchRequest.source(sourceBuilder); - SearchResponse searchResponse = ClientHelper.executeWithHeaders(config.getHeaders(), ClientHelper.ML_ORIGIN, client, - () -> client.search(searchRequest).actionGet()); + SearchResponse searchResponse = ClientHelper.executeWithHeaders( + config.getHeaders(), + ClientHelper.ML_ORIGIN, + client, + () -> client.search(searchRequest).actionGet() + ); Max maxIncrementalIdAgg = searchResponse.getAggregations().get(DestinationIndex.INCREMENTAL_ID); long processedTestDocCount = searchResponse.getHits().getTotalHits().value; Long lastIncrementalId = processedTestDocCount == 0 ? null : (long) maxIncrementalIdAgg.getValue(); if (lastIncrementalId != null) { - LOGGER.debug(() -> new ParameterizedMessage("[{}] Resuming inference; last incremental id [{}]; processed test doc count [{}]", - config.getId(), lastIncrementalId, processedTestDocCount)); + LOGGER.debug( + () -> new ParameterizedMessage( + "[{}] Resuming inference; last incremental id [{}]; processed test doc count [{}]", + config.getId(), + lastIncrementalId, + processedTestDocCount + ) + ); } return new InferenceState(lastIncrementalId, processedTestDocCount); } @@ -204,7 +236,8 @@ private void executeBulkRequest(BulkRequest bulkRequest) { bulkRequest, config.getId(), () -> isCancelled == false, - retryMessage -> {}); + retryMessage -> {} + ); } private static class InferenceState { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/inference/TestDocsIterator.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/inference/TestDocsIterator.java index 33b1abfc2851c..8bac1a20ff567 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/inference/TestDocsIterator.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/inference/TestDocsIterator.java @@ -50,8 +50,8 @@ private static Map buildDocValueFieldAndFormatPairs(ExtractedFie @Override protected QueryBuilder getQuery() { - return QueryBuilders.boolQuery().mustNot( - QueryBuilders.termQuery(config.getDest().getResultsField() + "." + DestinationIndex.IS_TRAINING, true)); + return QueryBuilders.boolQuery() + .mustNot(QueryBuilders.termQuery(config.getDest().getResultsField() + "." + DestinationIndex.IS_TRAINING, true)); } @Override @@ -66,7 +66,7 @@ protected SearchHit map(SearchHit hit) { @Override protected Object[] searchAfterFields() { - return lastDocId == null ? null : new Object[] {lastDocId}; + return lastDocId == null ? null : new Object[] { lastDocId }; } @Override @@ -76,8 +76,12 @@ protected void extractSearchAfterFields(SearchHit lastSearchHit) { @Override protected SearchResponse executeSearchRequest(SearchRequest searchRequest) { - return ClientHelper.executeWithHeaders(config.getHeaders(), ClientHelper.ML_ORIGIN, client(), - () -> client().search(searchRequest).actionGet()); + return ClientHelper.executeWithHeaders( + config.getHeaders(), + ClientHelper.ML_ORIGIN, + client(), + () -> client().search(searchRequest).actionGet() + ); } @Override diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/persistence/DataFrameAnalyticsConfigProvider.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/persistence/DataFrameAnalyticsConfigProvider.java index c98b2ad8e65df..d1d5c663543cd 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/persistence/DataFrameAnalyticsConfigProvider.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/persistence/DataFrameAnalyticsConfigProvider.java @@ -26,12 +26,6 @@ import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.IndexNotFoundException; @@ -41,6 +35,12 @@ import org.elasticsearch.persistent.PersistentTasksCustomMetadata; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.fetch.subphase.FetchSourceContext; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.action.util.PageParams; import org.elasticsearch.xpack.core.ml.MlConfigIndex; import org.elasticsearch.xpack.core.ml.MlTasks; @@ -89,45 +89,41 @@ public DataFrameAnalyticsConfigProvider(Client client, NamedXContentRegistry xCo /** * Puts the given {@link DataFrameAnalyticsConfig} document into the config index. */ - public void put(final DataFrameAnalyticsConfig config, Map headers, TimeValue timeout, - ActionListener listener) { + public void put( + final DataFrameAnalyticsConfig config, + Map headers, + TimeValue timeout, + ActionListener listener + ) { ActionListener deleteLeftOverDocsListener = ActionListener.wrap( r -> index(prepareConfigForIndex(config, headers), null, listener), listener::onFailure ); - ActionListener existsListener = ActionListener.wrap( - exists -> { - if (exists) { - listener.onFailure(ExceptionsHelper.dataFrameAnalyticsAlreadyExists(config.getId())); - } else { - deleteLeftOverDocs(config, timeout, deleteLeftOverDocsListener); - } - }, - listener::onFailure - ); + ActionListener existsListener = ActionListener.wrap(exists -> { + if (exists) { + listener.onFailure(ExceptionsHelper.dataFrameAnalyticsAlreadyExists(config.getId())); + } else { + deleteLeftOverDocs(config, timeout, deleteLeftOverDocsListener); + } + }, listener::onFailure); exists(config.getId(), existsListener); } private DataFrameAnalyticsConfig prepareConfigForIndex(DataFrameAnalyticsConfig config, Map headers) { - return headers.isEmpty() ? config : new DataFrameAnalyticsConfig.Builder(config) - .setHeaders(filterSecurityHeaders(headers)) - .build(); + return headers.isEmpty() ? config : new DataFrameAnalyticsConfig.Builder(config).setHeaders(filterSecurityHeaders(headers)).build(); } private void exists(String jobId, ActionListener listener) { - ActionListener getListener = ActionListener.wrap( - getResponse -> listener.onResponse(getResponse.isExists()), - e -> { - if (ExceptionsHelper.unwrapCause(e) instanceof IndexNotFoundException) { - listener.onResponse(false); - } else { - listener.onFailure(e); - } + ActionListener getListener = ActionListener.wrap(getResponse -> listener.onResponse(getResponse.isExists()), e -> { + if (ExceptionsHelper.unwrapCause(e) instanceof IndexNotFoundException) { + listener.onResponse(false); + } else { + listener.onFailure(e); } - ); + }); GetRequest getRequest = new GetRequest(MlConfigIndex.indexName(), DataFrameAnalyticsConfig.documentId(jobId)); getRequest.fetchSourceContext(FetchSourceContext.DO_NOT_FETCH_SOURCE); @@ -136,78 +132,74 @@ private void exists(String jobId, ActionListener listener) { private void deleteLeftOverDocs(DataFrameAnalyticsConfig config, TimeValue timeout, ActionListener listener) { DataFrameAnalyticsDeleter deleter = new DataFrameAnalyticsDeleter(client, auditor); - deleter.deleteAllDocuments(config, timeout, ActionListener.wrap( - r -> listener.onResponse(r), - e -> { - if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) { - // This is expected - listener.onResponse(AcknowledgedResponse.TRUE); - } else { - listener.onFailure(ExceptionsHelper.serverError("error deleting prior documents", e)); - } + deleter.deleteAllDocuments(config, timeout, ActionListener.wrap(r -> listener.onResponse(r), e -> { + if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) { + // This is expected + listener.onResponse(AcknowledgedResponse.TRUE); + } else { + listener.onFailure(ExceptionsHelper.serverError("error deleting prior documents", e)); } - )); + })); } /** * Updates the {@link DataFrameAnalyticsConfig} document in the config index using given {@link DataFrameAnalyticsConfigUpdate}. */ - public void update(DataFrameAnalyticsConfigUpdate update, - Map headers, - ClusterState clusterState, - ActionListener listener) { + public void update( + DataFrameAnalyticsConfigUpdate update, + Map headers, + ClusterState clusterState, + ActionListener listener + ) { String id = update.getId(); GetRequest getRequest = new GetRequest(MlConfigIndex.indexName(), DataFrameAnalyticsConfig.documentId(id)); - executeAsyncWithOrigin(client, ML_ORIGIN, GetAction.INSTANCE, getRequest, ActionListener.wrap( - getResponse -> { + executeAsyncWithOrigin(client, ML_ORIGIN, GetAction.INSTANCE, getRequest, ActionListener.wrap(getResponse -> { - // Fail the update request if the config to be updated doesn't exist - if (getResponse.isExists() == false) { - listener.onFailure(ExceptionsHelper.missingDataFrameAnalytics(id)); - return; - } + // Fail the update request if the config to be updated doesn't exist + if (getResponse.isExists() == false) { + listener.onFailure(ExceptionsHelper.missingDataFrameAnalytics(id)); + return; + } - // Parse the original config - DataFrameAnalyticsConfig originalConfig; - try { - try (InputStream stream = getResponse.getSourceAsBytesRef().streamInput(); - XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, stream)) { - originalConfig = DataFrameAnalyticsConfig.LENIENT_PARSER.apply(parser, null).build(); - } - } catch (IOException e) { - listener.onFailure( - new ElasticsearchParseException("Failed to parse data frame analytics configuration [" + id + "]", e)); - return; + // Parse the original config + DataFrameAnalyticsConfig originalConfig; + try { + try ( + InputStream stream = getResponse.getSourceAsBytesRef().streamInput(); + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, stream) + ) { + originalConfig = DataFrameAnalyticsConfig.LENIENT_PARSER.apply(parser, null).build(); } + } catch (IOException e) { + listener.onFailure(new ElasticsearchParseException("Failed to parse data frame analytics configuration [" + id + "]", e)); + return; + } - // Check that the update can be applied given current analytics state - checkUpdateCanBeApplied(originalConfig, update, clusterState); + // Check that the update can be applied given current analytics state + checkUpdateCanBeApplied(originalConfig, update, clusterState); - // Merge the original config with the given update object - DataFrameAnalyticsConfig.Builder updatedConfigBuilder = update.mergeWithConfig(originalConfig); - if (headers.isEmpty() == false) { - updatedConfigBuilder.setHeaders(filterSecurityHeaders(headers)); - } - DataFrameAnalyticsConfig updatedConfig = updatedConfigBuilder.build(); - - // Index the update config - index(updatedConfig, getResponse, ActionListener.wrap( - indexedConfig -> { - auditor.info(id, Messages.getMessage(Messages.DATA_FRAME_ANALYTICS_AUDIT_UPDATED, update.getUpdatedFields())); - listener.onResponse(indexedConfig); - }, - listener::onFailure - )); - }, - listener::onFailure - )); + // Merge the original config with the given update object + DataFrameAnalyticsConfig.Builder updatedConfigBuilder = update.mergeWithConfig(originalConfig); + if (headers.isEmpty() == false) { + updatedConfigBuilder.setHeaders(filterSecurityHeaders(headers)); + } + DataFrameAnalyticsConfig updatedConfig = updatedConfigBuilder.build(); + + // Index the update config + index(updatedConfig, getResponse, ActionListener.wrap(indexedConfig -> { + auditor.info(id, Messages.getMessage(Messages.DATA_FRAME_ANALYTICS_AUDIT_UPDATED, update.getUpdatedFields())); + listener.onResponse(indexedConfig); + }, listener::onFailure)); + }, listener::onFailure)); } - private static void checkUpdateCanBeApplied(DataFrameAnalyticsConfig originalConfig, - DataFrameAnalyticsConfigUpdate update, - ClusterState clusterState) { + private static void checkUpdateCanBeApplied( + DataFrameAnalyticsConfig originalConfig, + DataFrameAnalyticsConfigUpdate update, + ClusterState clusterState + ) { String analyticsId = update.getId(); PersistentTasksCustomMetadata tasks = clusterState.getMetadata().custom(PersistentTasksCustomMetadata.TYPE); DataFrameAnalyticsState analyticsState = MlTasks.getDataFrameAnalyticsState(analyticsId, tasks); @@ -217,7 +209,8 @@ private static void checkUpdateCanBeApplied(DataFrameAnalyticsConfig originalCon } if (update.requiresRestart(originalConfig)) { throw ExceptionsHelper.conflictStatusException( - Messages.getMessage(Messages.DATA_FRAME_ANALYTICS_CANNOT_UPDATE_IN_CURRENT_STATE, analyticsId, analyticsState)); + Messages.getMessage(Messages.DATA_FRAME_ANALYTICS_CANNOT_UPDATE_IN_CURRENT_STATE, analyticsId, analyticsState) + ); } } @@ -229,55 +222,61 @@ private static void checkUpdateCanBeApplied(DataFrameAnalyticsConfig originalCon * If null, this config is indexed for the first time * @param listener listener to be called after indexing */ - private void index(DataFrameAnalyticsConfig config, - @Nullable GetResponse getResponse, - ActionListener listener) { + private void index( + DataFrameAnalyticsConfig config, + @Nullable GetResponse getResponse, + ActionListener listener + ) { try (XContentBuilder builder = XContentFactory.jsonBuilder()) { config.toXContent(builder, new ToXContent.MapParams(TO_XCONTENT_PARAMS)); - IndexRequest indexRequest = new IndexRequest(MlConfigIndex.indexName()) - .id(DataFrameAnalyticsConfig.documentId(config.getId())) + IndexRequest indexRequest = new IndexRequest(MlConfigIndex.indexName()).id(DataFrameAnalyticsConfig.documentId(config.getId())) .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) .source(builder); if (getResponse == null) { indexRequest.opType(DocWriteRequest.OpType.CREATE); } else { - indexRequest - .opType(DocWriteRequest.OpType.INDEX) + indexRequest.opType(DocWriteRequest.OpType.INDEX) .setIfSeqNo(getResponse.getSeqNo()) .setIfPrimaryTerm(getResponse.getPrimaryTerm()); } - executeAsyncWithOrigin(client, ML_ORIGIN, IndexAction.INSTANCE, indexRequest, ActionListener.wrap( - indexResponse -> listener.onResponse(config), - e -> { + executeAsyncWithOrigin( + client, + ML_ORIGIN, + IndexAction.INSTANCE, + indexRequest, + ActionListener.wrap(indexResponse -> listener.onResponse(config), e -> { if (ExceptionsHelper.unwrapCause(e) instanceof VersionConflictEngineException) { listener.onFailure(ExceptionsHelper.dataFrameAnalyticsAlreadyExists(config.getId())); } else { listener.onFailure(e); } - } - )); + }) + ); } catch (IOException e) { - listener.onFailure(new ElasticsearchParseException("Failed to serialise data frame analytics with id [" + config.getId() - + "]")); + listener.onFailure( + new ElasticsearchParseException("Failed to serialise data frame analytics with id [" + config.getId() + "]") + ); } } public void get(String id, ActionListener listener) { GetDataFrameAnalyticsAction.Request request = new GetDataFrameAnalyticsAction.Request(); request.setResourceId(id); - executeAsyncWithOrigin(client, ML_ORIGIN, GetDataFrameAnalyticsAction.INSTANCE, request, ActionListener.wrap( - response -> { - List analytics = response.getResources().results(); - if (analytics.size() != 1) { - listener.onFailure(ExceptionsHelper.badRequestException("Expected a single match for data frame analytics [{}] " + - "but got [{}]", id, analytics.size())); - } else { - listener.onResponse(analytics.get(0)); - } - }, - listener::onFailure - )); + executeAsyncWithOrigin(client, ML_ORIGIN, GetDataFrameAnalyticsAction.INSTANCE, request, ActionListener.wrap(response -> { + List analytics = response.getResources().results(); + if (analytics.size() != 1) { + listener.onFailure( + ExceptionsHelper.badRequestException( + "Expected a single match for data frame analytics [{}] " + "but got [{}]", + id, + analytics.size() + ) + ); + } else { + listener.onResponse(analytics.get(0)); + } + }, listener::onFailure)); } /** @@ -288,8 +287,13 @@ public void getMultiple(String ids, boolean allowNoMatch, ActionListener listener.onResponse(response.getResources().results()), listener::onFailure)); + executeAsyncWithOrigin( + client, + ML_ORIGIN, + GetDataFrameAnalyticsAction.INSTANCE, + request, + ActionListener.wrap(response -> listener.onResponse(response.getResources().results()), listener::onFailure) + ); } /** @@ -306,7 +310,8 @@ public void getConfigsForJobsWithTasksLeniently(Set jobsWithTask, Action searchRequest.source().size(DataFrameAnalyticsConfigProvider.MAX_CONFIGS_SIZE); searchRequest.source().query(query); - executeAsyncWithOrigin(client.threadPool().getThreadContext(), + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), ML_ORIGIN, searchRequest, new ActionListener.Delegating>(listener) { @@ -316,16 +321,17 @@ public void onResponse(SearchResponse searchResponse) { List configs = new ArrayList<>(hits.length); for (SearchHit hit : hits) { BytesReference sourceBytes = hit.getSourceRef(); - try (InputStream stream = sourceBytes.streamInput(); - XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser( - xContentRegistry, LoggingDeprecationHandler.INSTANCE, stream)) { + try ( + InputStream stream = sourceBytes.streamInput(); + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, stream) + ) { configs.add(DataFrameAnalyticsConfig.LENIENT_PARSER.apply(parser, null).build()); } catch (IOException e) { delegate.onFailure(e); } } - Set tasksWithoutConfigs = new HashSet<>(jobsWithTask); tasksWithoutConfigs.removeAll(configs.stream().map(DataFrameAnalyticsConfig::getId).collect(Collectors.toList())); if (tasksWithoutConfigs.isEmpty() == false) { @@ -333,6 +339,8 @@ public void onResponse(SearchResponse searchResponse) { } delegate.onResponse(configs); } - }, client::search); + }, + client::search + ); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/persistence/DataFrameAnalyticsDeleter.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/persistence/DataFrameAnalyticsDeleter.java index eeebeb65c1f26..49cf59bd40fd0 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/persistence/DataFrameAnalyticsDeleter.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/persistence/DataFrameAnalyticsDeleter.java @@ -60,43 +60,45 @@ public void deleteAllDocuments(DataFrameAnalyticsConfig config, TimeValue timeou final String id = config.getId(); // Step 3. Delete the config - ActionListener deleteStatsHandler = ActionListener.wrap( - bulkByScrollResponse -> { - if (bulkByScrollResponse.isTimedOut()) { - logger.warn("[{}] DeleteByQuery for stats timed out", id); - } - if (bulkByScrollResponse.getBulkFailures().isEmpty() == false) { - logger.warn("[{}] {} failures and {} conflicts encountered while running DeleteByQuery for stats", id, - bulkByScrollResponse.getBulkFailures().size(), bulkByScrollResponse.getVersionConflicts()); - for (BulkItemResponse.Failure failure : bulkByScrollResponse.getBulkFailures()) { - logger.warn("[{}] DBQ failure: {}", id, failure); - } + ActionListener deleteStatsHandler = ActionListener.wrap(bulkByScrollResponse -> { + if (bulkByScrollResponse.isTimedOut()) { + logger.warn("[{}] DeleteByQuery for stats timed out", id); + } + if (bulkByScrollResponse.getBulkFailures().isEmpty() == false) { + logger.warn( + "[{}] {} failures and {} conflicts encountered while running DeleteByQuery for stats", + id, + bulkByScrollResponse.getBulkFailures().size(), + bulkByScrollResponse.getVersionConflicts() + ); + for (BulkItemResponse.Failure failure : bulkByScrollResponse.getBulkFailures()) { + logger.warn("[{}] DBQ failure: {}", id, failure); } - deleteConfig(id, listener); - }, - failure -> { - logger.warn(new ParameterizedMessage("[{}] failed to remove stats", id), ExceptionsHelper.unwrapCause(failure)); - deleteConfig(id, listener); } - ); + deleteConfig(id, listener); + }, failure -> { + logger.warn(new ParameterizedMessage("[{}] failed to remove stats", id), ExceptionsHelper.unwrapCause(failure)); + deleteConfig(id, listener); + }); // Step 2. Delete job docs from stats index - ActionListener deleteStateHandler = ActionListener.wrap( - bulkByScrollResponse -> { - if (bulkByScrollResponse.isTimedOut()) { - logger.warn("[{}] DeleteByQuery for state timed out", id); - } - if (bulkByScrollResponse.getBulkFailures().isEmpty() == false) { - logger.warn("[{}] {} failures and {} conflicts encountered while running DeleteByQuery for state", id, - bulkByScrollResponse.getBulkFailures().size(), bulkByScrollResponse.getVersionConflicts()); - for (BulkItemResponse.Failure failure : bulkByScrollResponse.getBulkFailures()) { - logger.warn("[{}] DBQ failure: {}", id, failure); - } + ActionListener deleteStateHandler = ActionListener.wrap(bulkByScrollResponse -> { + if (bulkByScrollResponse.isTimedOut()) { + logger.warn("[{}] DeleteByQuery for state timed out", id); + } + if (bulkByScrollResponse.getBulkFailures().isEmpty() == false) { + logger.warn( + "[{}] {} failures and {} conflicts encountered while running DeleteByQuery for state", + id, + bulkByScrollResponse.getBulkFailures().size(), + bulkByScrollResponse.getVersionConflicts() + ); + for (BulkItemResponse.Failure failure : bulkByScrollResponse.getBulkFailures()) { + logger.warn("[{}] DBQ failure: {}", id, failure); } - deleteStats(id, timeout, deleteStatsHandler); - }, - listener::onFailure - ); + } + deleteStats(id, timeout, deleteStatsHandler); + }, listener::onFailure); // Step 1. Delete state deleteState(config, timeout, deleteStateHandler); @@ -106,25 +108,22 @@ private void deleteConfig(String id, ActionListener listen DeleteRequest deleteRequest = new DeleteRequest(MlConfigIndex.indexName()); deleteRequest.id(DataFrameAnalyticsConfig.documentId(id)); deleteRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - executeAsyncWithOrigin(client, ML_ORIGIN, DeleteAction.INSTANCE, deleteRequest, ActionListener.wrap( - deleteResponse -> { - if (deleteResponse.getResult() == DocWriteResponse.Result.NOT_FOUND) { - listener.onFailure(ExceptionsHelper.missingDataFrameAnalytics(id)); - return; - } - assert deleteResponse.getResult() == DocWriteResponse.Result.DELETED; - logger.info("[{}] Deleted", id); - auditor.info(id, Messages.DATA_FRAME_ANALYTICS_AUDIT_DELETED); - listener.onResponse(AcknowledgedResponse.TRUE); - }, - e -> { - if (ExceptionsHelper.unwrapCause(e) instanceof IndexNotFoundException) { - listener.onFailure(ExceptionsHelper.missingDataFrameAnalytics(id)); - } else { - listener.onFailure(e); - } + executeAsyncWithOrigin(client, ML_ORIGIN, DeleteAction.INSTANCE, deleteRequest, ActionListener.wrap(deleteResponse -> { + if (deleteResponse.getResult() == DocWriteResponse.Result.NOT_FOUND) { + listener.onFailure(ExceptionsHelper.missingDataFrameAnalytics(id)); + return; + } + assert deleteResponse.getResult() == DocWriteResponse.Result.DELETED; + logger.info("[{}] Deleted", id); + auditor.info(id, Messages.DATA_FRAME_ANALYTICS_AUDIT_DELETED); + listener.onResponse(AcknowledgedResponse.TRUE); + }, e -> { + if (ExceptionsHelper.unwrapCause(e) instanceof IndexNotFoundException) { + listener.onFailure(ExceptionsHelper.missingDataFrameAnalytics(id)); + } else { + listener.onFailure(e); } - )); + })); } private void deleteState(DataFrameAnalyticsConfig config, TimeValue timeout, ActionListener listener) { @@ -134,8 +133,8 @@ private void deleteState(DataFrameAnalyticsConfig config, TimeValue timeout, Act QueryBuilders.idsQuery().addIds(StoredProgress.documentId(config.getId())), timeout, listener - ) - , listener::onFailure + ), + listener::onFailure ); deleteModelState(config, timeout, 1, deleteModelStateListener); @@ -148,21 +147,13 @@ private void deleteModelState(DataFrameAnalyticsConfig config, TimeValue timeout } IdsQueryBuilder query = QueryBuilders.idsQuery().addIds(config.getAnalysis().getStateDocIdPrefix(config.getId()) + docNum); - executeDeleteByQuery( - AnomalyDetectorsIndex.jobStateIndexPattern(), - query, - timeout, - ActionListener.wrap( - response -> { - if (response.getDeleted() > 0) { - deleteModelState(config, timeout, docNum + 1, listener); - return; - } - listener.onResponse(true); - }, - listener::onFailure - ) - ); + executeDeleteByQuery(AnomalyDetectorsIndex.jobStateIndexPattern(), query, timeout, ActionListener.wrap(response -> { + if (response.getDeleted() > 0) { + deleteModelState(config, timeout, docNum + 1, listener); + return; + } + listener.onResponse(true); + }, listener::onFailure)); } private void deleteStats(String jobId, TimeValue timeout, ActionListener listener) { @@ -174,8 +165,7 @@ private void deleteStats(String jobId, TimeValue timeout, ActionListener listener) { + private void executeDeleteByQuery(String index, QueryBuilder query, TimeValue timeout, ActionListener listener) { DeleteByQueryRequest request = new DeleteByQueryRequest(index); request.setQuery(query); request.setIndicesOptions(MlIndicesUtils.addIgnoreUnavailable(IndicesOptions.lenientExpandOpen())); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/AbstractNativeAnalyticsProcess.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/AbstractNativeAnalyticsProcess.java index 35e2cbf58e939..1a10835270bb8 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/AbstractNativeAnalyticsProcess.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/AbstractNativeAnalyticsProcess.java @@ -25,10 +25,17 @@ abstract class AbstractNativeAnalyticsProcess extends AbstractNativeProc private final String name; private final ProcessResultsParser resultsParser; - protected AbstractNativeAnalyticsProcess(String name, ConstructingObjectParser resultParser, String jobId, - NativeController nativeController, ProcessPipes processPipes, - int numberOfFields, List filesToDelete, Consumer onProcessCrash, - NamedXContentRegistry namedXContentRegistry) { + protected AbstractNativeAnalyticsProcess( + String name, + ConstructingObjectParser resultParser, + String jobId, + NativeController nativeController, + ProcessPipes processPipes, + int numberOfFields, + List filesToDelete, + Consumer onProcessCrash, + NamedXContentRegistry namedXContentRegistry + ) { super(jobId, nativeController, processPipes, numberOfFields, filesToDelete, onProcessCrash); this.name = Objects.requireNonNull(name); this.resultsParser = new ProcessResultsParser<>(Objects.requireNonNull(resultParser), namedXContentRegistry); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/AnalysisFieldInfo.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/AnalysisFieldInfo.java index 94a20387b1ed5..d66f289766364 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/AnalysisFieldInfo.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/AnalysisFieldInfo.java @@ -25,9 +25,7 @@ public class AnalysisFieldInfo implements DataFrameAnalysis.FieldInfo { @Override public Set getTypes(String field) { - Optional extractedField = extractedFields.getAllFields().stream() - .filter(f -> f.getName().equals(field)) - .findAny(); + Optional extractedField = extractedFields.getAllFields().stream().filter(f -> f.getName().equals(field)).findAny(); return extractedField.isPresent() ? extractedField.get().getTypes() : null; } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/AnalyticsBuilder.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/AnalyticsBuilder.java index beb0cdcb58508..a085f0ac9d950 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/AnalyticsBuilder.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/AnalyticsBuilder.java @@ -40,8 +40,13 @@ public class AnalyticsBuilder { private final List filesToDelete; private boolean performMemoryUsageEstimationOnly; - public AnalyticsBuilder(Supplier tempDirPathSupplier, NativeController nativeController, - ProcessPipes processPipes, AnalyticsProcessConfig config, List filesToDelete) { + public AnalyticsBuilder( + Supplier tempDirPathSupplier, + NativeController nativeController, + ProcessPipes processPipes, + AnalyticsProcessConfig config, + List filesToDelete + ) { this.tempDirPathSupplier = Objects.requireNonNull(tempDirPathSupplier); this.nativeController = Objects.requireNonNull(nativeController); this.processPipes = Objects.requireNonNull(processPipes); @@ -77,8 +82,10 @@ private void addConfigFile(List command) throws IOException { Path tempDir = tempDirPathSupplier.get(); Path configFile = Files.createTempFile(tempDir, "analysis", ".conf"); filesToDelete.add(configFile); - try (OutputStreamWriter osw = new OutputStreamWriter(Files.newOutputStream(configFile),StandardCharsets.UTF_8); - XContentBuilder jsonBuilder = JsonXContent.contentBuilder()) { + try ( + OutputStreamWriter osw = new OutputStreamWriter(Files.newOutputStream(configFile), StandardCharsets.UTF_8); + XContentBuilder jsonBuilder = JsonXContent.contentBuilder() + ) { config.toXContent(jsonBuilder, ToXContent.EMPTY_PARAMS); osw.write(Strings.toString(jsonBuilder)); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/AnalyticsProcessConfig.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/AnalyticsProcessConfig.java index dcff09339b99c..674834fe8870e 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/AnalyticsProcessConfig.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/AnalyticsProcessConfig.java @@ -37,8 +37,17 @@ public class AnalyticsProcessConfig implements ToXContentObject { private final DataFrameAnalysis analysis; private final ExtractedFields extractedFields; - public AnalyticsProcessConfig(String jobId, long rows, int cols, ByteSizeValue memoryLimit, int threads, String resultsField, - Set categoricalFields, DataFrameAnalysis analysis, ExtractedFields extractedFields) { + public AnalyticsProcessConfig( + String jobId, + long rows, + int cols, + ByteSizeValue memoryLimit, + int threads, + String resultsField, + Set categoricalFields, + DataFrameAnalysis analysis, + ExtractedFields extractedFields + ) { this.jobId = Objects.requireNonNull(jobId); this.rows = rows; this.cols = cols; diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/AnalyticsProcessFactory.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/AnalyticsProcessFactory.java index c85da753b5e25..00e9d70dffc2a 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/AnalyticsProcessFactory.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/AnalyticsProcessFactory.java @@ -23,7 +23,11 @@ public interface AnalyticsProcessFactory { * @param onProcessCrash Callback to execute if the process stops unexpectedly * @return The process */ - AnalyticsProcess createAnalyticsProcess(DataFrameAnalyticsConfig config, AnalyticsProcessConfig analyticsProcessConfig, - boolean hasState, ExecutorService executorService, - Consumer onProcessCrash); + AnalyticsProcess createAnalyticsProcess( + DataFrameAnalyticsConfig config, + AnalyticsProcessConfig analyticsProcessConfig, + boolean hasState, + ExecutorService executorService, + Consumer onProcessCrash + ); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/AnalyticsProcessManager.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/AnalyticsProcessManager.java index e1feb5c524221..7e797924da1a2 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/AnalyticsProcessManager.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/AnalyticsProcessManager.java @@ -63,14 +63,16 @@ public class AnalyticsProcessManager { private final ResultsPersisterService resultsPersisterService; private final int numAllocatedProcessors; - public AnalyticsProcessManager(Settings settings, - Client client, - ThreadPool threadPool, - AnalyticsProcessFactory analyticsProcessFactory, - DataFrameAnalyticsAuditor auditor, - TrainedModelProvider trainedModelProvider, - ResultsPersisterService resultsPersisterService, - int numAllocatedProcessors) { + public AnalyticsProcessManager( + Settings settings, + Client client, + ThreadPool threadPool, + AnalyticsProcessFactory analyticsProcessFactory, + DataFrameAnalyticsAuditor auditor, + TrainedModelProvider trainedModelProvider, + ResultsPersisterService resultsPersisterService, + int numAllocatedProcessors + ) { this( settings, client, @@ -80,19 +82,22 @@ public AnalyticsProcessManager(Settings settings, auditor, trainedModelProvider, resultsPersisterService, - numAllocatedProcessors); + numAllocatedProcessors + ); } // Visible for testing - public AnalyticsProcessManager(Settings settings, - Client client, - ExecutorService executorServiceForJob, - ExecutorService executorServiceForProcess, - AnalyticsProcessFactory analyticsProcessFactory, - DataFrameAnalyticsAuditor auditor, - TrainedModelProvider trainedModelProvider, - ResultsPersisterService resultsPersisterService, - int numAllocatedProcessors) { + public AnalyticsProcessManager( + Settings settings, + Client client, + ExecutorService executorServiceForJob, + ExecutorService executorServiceForProcess, + AnalyticsProcessFactory analyticsProcessFactory, + DataFrameAnalyticsAuditor auditor, + TrainedModelProvider trainedModelProvider, + ResultsPersisterService resultsPersisterService, + int numAllocatedProcessors + ) { this.settings = Objects.requireNonNull(settings); this.client = Objects.requireNonNull(client); this.executorServiceForJob = Objects.requireNonNull(executorServiceForJob); @@ -104,22 +109,26 @@ public AnalyticsProcessManager(Settings settings, this.numAllocatedProcessors = numAllocatedProcessors; } - public void runJob(DataFrameAnalyticsTask task, DataFrameAnalyticsConfig config, DataFrameDataExtractorFactory dataExtractorFactory, - ActionListener listener) { + public void runJob( + DataFrameAnalyticsTask task, + DataFrameAnalyticsConfig config, + DataFrameDataExtractorFactory dataExtractorFactory, + ActionListener listener + ) { executorServiceForJob.execute(() -> { ProcessContext processContext = new ProcessContext(config); synchronized (processContextByAllocation) { if (task.isStopping()) { - LOGGER.debug("[{}] task is stopping. Marking as complete before creating process context.", - task.getParams().getId()); + LOGGER.debug("[{}] task is stopping. Marking as complete before creating process context.", task.getParams().getId()); // The task was requested to stop before we created the process context auditor.info(config.getId(), Messages.DATA_FRAME_ANALYTICS_AUDIT_FINISHED_ANALYSIS); listener.onResponse(new StepResponse(true)); return; } if (processContextByAllocation.putIfAbsent(task.getAllocationId(), processContext) != null) { - listener.onFailure(ExceptionsHelper.serverError( - "[" + config.getId() + "] Could not create process as one already exists")); + listener.onFailure( + ExceptionsHelper.serverError("[" + config.getId() + "] Could not create process as one already exists") + ); return; } } @@ -133,8 +142,9 @@ public void runJob(DataFrameAnalyticsTask task, DataFrameAnalyticsConfig config, } catch (Exception e) { processContext.stop(); processContextByAllocation.remove(task.getAllocationId()); - listener.onFailure(processContext.getFailureReason() == null ? - e : ExceptionsHelper.serverError(processContext.getFailureReason())); + listener.onFailure( + processContext.getFailureReason() == null ? e : ExceptionsHelper.serverError(processContext.getFailureReason()) + ); return; } @@ -164,8 +174,12 @@ private boolean hasModelState(DataFrameAnalyticsConfig config) { } } - private void processData(DataFrameAnalyticsTask task, ProcessContext processContext, boolean hasState, - ActionListener listener) { + private void processData( + DataFrameAnalyticsTask task, + ProcessContext processContext, + boolean hasState, + ActionListener listener + ) { LOGGER.info("[{}] Started loading data", processContext.config.getId()); auditor.info(processContext.config.getId(), Messages.getMessage(Messages.DATA_FRAME_ANALYTICS_AUDIT_STARTED_LOADING_DATA)); @@ -193,13 +207,15 @@ private void processData(DataFrameAnalyticsTask task, ProcessContext processCont } catch (Exception e) { if (task.isStopping()) { // Errors during task stopping are expected but we still want to log them just in case. - String errorMsg = - new ParameterizedMessage( - "[{}] Error while processing data [{}]; task is stopping", config.getId(), e.getMessage()).getFormattedMessage(); + String errorMsg = new ParameterizedMessage( + "[{}] Error while processing data [{}]; task is stopping", + config.getId(), + e.getMessage() + ).getFormattedMessage(); LOGGER.debug(errorMsg, e); } else { - String errorMsg = - new ParameterizedMessage("[{}] Error while processing data [{}]", config.getId(), e.getMessage()).getFormattedMessage(); + String errorMsg = new ParameterizedMessage("[{}] Error while processing data [{}]", config.getId(), e.getMessage()) + .getFormattedMessage(); LOGGER.error(errorMsg, e); processContext.setFailureReason(errorMsg); } @@ -207,8 +223,11 @@ private void processData(DataFrameAnalyticsTask task, ProcessContext processCont closeProcess(task); processContextByAllocation.remove(task.getAllocationId()); - LOGGER.debug("Removed process context for task [{}]; [{}] processes still running", config.getId(), - processContextByAllocation.size()); + LOGGER.debug( + "Removed process context for task [{}]; [{}] processes still running", + config.getId(), + processContextByAllocation.size() + ); if (processContext.getFailureReason() == null) { auditor.info(config.getId(), Messages.DATA_FRAME_ANALYTICS_AUDIT_FINISHED_ANALYSIS); @@ -221,8 +240,8 @@ private void processData(DataFrameAnalyticsTask task, ProcessContext processCont } } - private void writeDataRows(DataFrameDataExtractor dataExtractor, AnalyticsProcess process, - DataFrameAnalyticsTask task) throws IOException { + private void writeDataRows(DataFrameDataExtractor dataExtractor, AnalyticsProcess process, DataFrameAnalyticsTask task) + throws IOException { ProgressTracker progressTracker = task.getStatsHolder().getProgressTracker(); DataCountsTracker dataCountsTracker = task.getStatsHolder().getDataCountsTracker(); @@ -256,15 +275,17 @@ private void writeDataRows(DataFrameDataExtractor dataExtractor, AnalyticsProces } } - private void writeHeaderRecord(DataFrameDataExtractor dataExtractor, - AnalyticsProcess process, - DataFrameAnalyticsTask task) throws IOException { + private void writeHeaderRecord( + DataFrameDataExtractor dataExtractor, + AnalyticsProcess process, + DataFrameAnalyticsTask task + ) throws IOException { List fieldNames = dataExtractor.getFieldNames(); LOGGER.debug(() -> new ParameterizedMessage("[{}] header row fields {}", task.getParams().getId(), fieldNames)); // We add 2 extra fields, both named dot: - // - the document hash - // - the control message + // - the document hash + // - the control message String[] headerRecord = new String[fieldNames.size() + 2]; for (int i = 0; i < fieldNames.size(); i++) { headerRecord[i] = fieldNames.get(i); @@ -297,10 +318,19 @@ private void restoreState(DataFrameAnalyticsConfig config, AnalyticsProcess createProcess(DataFrameAnalyticsTask task, DataFrameAnalyticsConfig config, - AnalyticsProcessConfig analyticsProcessConfig, boolean hasState) { + private AnalyticsProcess createProcess( + DataFrameAnalyticsTask task, + DataFrameAnalyticsConfig config, + AnalyticsProcessConfig analyticsProcessConfig, + boolean hasState + ) { AnalyticsProcess process = processFactory.createAnalyticsProcess( - config, analyticsProcessConfig, hasState, executorServiceForProcess, onProcessCrash(task)); + config, + analyticsProcessConfig, + hasState, + executorServiceForProcess, + onProcessCrash(task) + ); if (process.isProcessAlive() == false) { throw ExceptionsHelper.serverError("Failed to start data frame analytics process"); } @@ -327,13 +357,18 @@ private void closeProcess(DataFrameAnalyticsTask task) { LOGGER.info("[{}] Closed process", configId); } catch (Exception e) { if (task.isStopping()) { - LOGGER.debug(() -> new ParameterizedMessage( - "[{}] Process closing was interrupted by kill request due to the task being stopped", configId), e); + LOGGER.debug( + () -> new ParameterizedMessage( + "[{}] Process closing was interrupted by kill request due to the task being stopped", + configId + ), + e + ); LOGGER.info("[{}] Closed process", configId); } else { LOGGER.error("[" + configId + "] Error closing data frame analyzer process", e); - String errorMsg = new ParameterizedMessage( - "[{}] Error closing data frame analyzer process [{}]", configId, e.getMessage()).getFormattedMessage(); + String errorMsg = new ParameterizedMessage("[{}] Error closing data frame analyzer process [{}]", configId, e.getMessage()) + .getFormattedMessage(); processContext.setFailureReason(errorMsg); } } @@ -401,16 +436,21 @@ synchronized void stop() { /** * @return {@code true} if the process was started or {@code false} if it was not because it was stopped in the meantime */ - synchronized boolean startProcess(DataFrameDataExtractorFactory dataExtractorFactory, DataFrameAnalyticsTask task, - boolean hasState) { + synchronized boolean startProcess( + DataFrameDataExtractorFactory dataExtractorFactory, + DataFrameAnalyticsTask task, + boolean hasState + ) { if (task.isStopping()) { // The job was stopped before we started the process so no need to start it return false; } dataExtractor.set(dataExtractorFactory.newExtractor(false)); - AnalyticsProcessConfig analyticsProcessConfig = - createProcessConfig(dataExtractor.get(), dataExtractorFactory.getExtractedFields()); + AnalyticsProcessConfig analyticsProcessConfig = createProcessConfig( + dataExtractor.get(), + dataExtractorFactory.getExtractedFields() + ); LOGGER.debug("[{}] creating analytics process with config [{}]", config.getId(), Strings.toString(analyticsProcessConfig)); // If we have no rows, that means there is no data so no point in starting the native process // just finish the task @@ -423,8 +463,7 @@ synchronized boolean startProcess(DataFrameDataExtractorFactory dataExtractorFac return true; } - private AnalyticsProcessConfig createProcessConfig(DataFrameDataExtractor dataExtractor, - ExtractedFields extractedFields) { + private AnalyticsProcessConfig createProcessConfig(DataFrameDataExtractor dataExtractor, ExtractedFields extractedFields) { DataFrameDataExtractor.DataSummary dataSummary = dataExtractor.collectDataSummary(); Set categoricalFields = dataExtractor.getCategoricalFields(config.getAnalysis()); int threads = Math.min(config.getMaxNumThreads(), numAllocatedProcessors); @@ -437,18 +476,31 @@ private AnalyticsProcessConfig createProcessConfig(DataFrameDataExtractor dataEx config.getDest().getResultsField(), categoricalFields, config.getAnalysis(), - extractedFields); + extractedFields + ); } - private AnalyticsResultProcessor createResultProcessor(DataFrameAnalyticsTask task, - DataFrameDataExtractorFactory dataExtractorFactory) { - DataFrameRowsJoiner dataFrameRowsJoiner = - new DataFrameRowsJoiner(config.getId(), settings, task.getParentTaskId(), - dataExtractorFactory.newExtractor(true), resultsPersisterService); + private AnalyticsResultProcessor createResultProcessor( + DataFrameAnalyticsTask task, + DataFrameDataExtractorFactory dataExtractorFactory + ) { + DataFrameRowsJoiner dataFrameRowsJoiner = new DataFrameRowsJoiner( + config.getId(), + settings, + task.getParentTaskId(), + dataExtractorFactory.newExtractor(true), + resultsPersisterService + ); StatsPersister statsPersister = new StatsPersister(config.getId(), resultsPersisterService, auditor); return new AnalyticsResultProcessor( - config, dataFrameRowsJoiner, task.getStatsHolder(), trainedModelProvider, auditor, statsPersister, - dataExtractor.get().getExtractedFields()); + config, + dataFrameRowsJoiner, + task.getStatsHolder(), + trainedModelProvider, + auditor, + statsPersister, + dataExtractor.get().getExtractedFields() + ); } } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/AnalyticsResultProcessor.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/AnalyticsResultProcessor.java index b7ec7e4cccfd2..ee56b28c9591e 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/AnalyticsResultProcessor.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/AnalyticsResultProcessor.java @@ -25,8 +25,8 @@ import org.elasticsearch.xpack.ml.dataframe.process.results.TrainedModelDefinitionChunk; import org.elasticsearch.xpack.ml.dataframe.stats.StatsHolder; import org.elasticsearch.xpack.ml.dataframe.stats.StatsPersister; -import org.elasticsearch.xpack.ml.inference.modelsize.ModelSizeInfo; import org.elasticsearch.xpack.ml.extractor.ExtractedFields; +import org.elasticsearch.xpack.ml.inference.modelsize.ModelSizeInfo; import org.elasticsearch.xpack.ml.inference.persistence.TrainedModelProvider; import org.elasticsearch.xpack.ml.notifications.DataFrameAnalyticsAuditor; @@ -63,9 +63,15 @@ public class AnalyticsResultProcessor { private volatile String latestModelId; - public AnalyticsResultProcessor(DataFrameAnalyticsConfig analytics, DataFrameRowsJoiner dataFrameRowsJoiner, - StatsHolder statsHolder, TrainedModelProvider trainedModelProvider, - DataFrameAnalyticsAuditor auditor, StatsPersister statsPersister, ExtractedFields extractedFields) { + public AnalyticsResultProcessor( + DataFrameAnalyticsConfig analytics, + DataFrameRowsJoiner dataFrameRowsJoiner, + StatsHolder statsHolder, + TrainedModelProvider trainedModelProvider, + DataFrameAnalyticsAuditor auditor, + StatsPersister statsPersister, + ExtractedFields extractedFields + ) { this.analytics = Objects.requireNonNull(analytics); this.dataFrameRowsJoiner = Objects.requireNonNull(dataFrameRowsJoiner); this.statsHolder = Objects.requireNonNull(statsHolder); @@ -137,8 +143,12 @@ private void processResult(AnalyticsResult result, DataFrameRowsJoiner resultsJo } PhaseProgress phaseProgress = result.getPhaseProgress(); if (phaseProgress != null) { - LOGGER.debug("[{}] progress for phase [{}] updated to [{}]", analytics.getId(), phaseProgress.getPhase(), - phaseProgress.getProgressPercent()); + LOGGER.debug( + "[{}] progress for phase [{}] updated to [{}]", + analytics.getId(), + phaseProgress.getPhase(), + phaseProgress.getProgressPercent() + ); statsHolder.getProgressTracker().updatePhase(phaseProgress); } ModelSizeInfo modelSize = result.getModelSizeInfo(); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/ChunkedTrainedModelPersister.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/ChunkedTrainedModelPersister.java index bf047d01e45ea..8e7fe295d2782 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/ChunkedTrainedModelPersister.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/ChunkedTrainedModelPersister.java @@ -17,16 +17,16 @@ import org.elasticsearch.action.admin.indices.refresh.RefreshResponse; import org.elasticsearch.common.Strings; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.license.License; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsConfig; import org.elasticsearch.xpack.core.ml.dataframe.analyses.Classification; import org.elasticsearch.xpack.core.ml.dataframe.analyses.Regression; import org.elasticsearch.xpack.core.ml.inference.TrainedModelConfig; import org.elasticsearch.xpack.core.ml.inference.TrainedModelInput; import org.elasticsearch.xpack.core.ml.inference.TrainedModelType; -import org.elasticsearch.xpack.core.ml.inference.trainedmodel.metadata.TrainedModelMetadata; import org.elasticsearch.xpack.core.ml.inference.preprocessing.PreProcessor; +import org.elasticsearch.xpack.core.ml.inference.trainedmodel.metadata.TrainedModelMetadata; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; import org.elasticsearch.xpack.core.security.user.XPackUser; import org.elasticsearch.xpack.ml.dataframe.process.results.ModelMetadata; @@ -65,11 +65,13 @@ public class ChunkedTrainedModelPersister { private final ExtractedFields extractedFields; private final AtomicBoolean readyToStoreNewModel = new AtomicBoolean(true); - public ChunkedTrainedModelPersister(TrainedModelProvider provider, - DataFrameAnalyticsConfig analytics, - DataFrameAnalyticsAuditor auditor, - Consumer failureHandler, - ExtractedFields extractedFields) { + public ChunkedTrainedModelPersister( + TrainedModelProvider provider, + DataFrameAnalyticsConfig analytics, + DataFrameAnalyticsAuditor auditor, + Consumer failureHandler, + ExtractedFields extractedFields + ) { this.provider = provider; this.currentModelId = new AtomicReference<>(""); this.analytics = analytics; @@ -80,9 +82,11 @@ public ChunkedTrainedModelPersister(TrainedModelProvider provider, public void createAndIndexInferenceModelDoc(TrainedModelDefinitionChunk trainedModelDefinitionChunk) { if (readyToStoreNewModel.get()) { - failureHandler.accept(ExceptionsHelper.serverError( - "chunked inference model definition is attempting to be stored before trained model configuration" - )); + failureHandler.accept( + ExceptionsHelper.serverError( + "chunked inference model definition is attempting to be stored before trained model configuration" + ) + ); return; } TrainedModelDefinitionDoc trainedModelDefinitionDoc = trainedModelDefinitionChunk.createTrainedModelDoc(this.currentModelId.get()); @@ -104,9 +108,9 @@ public void createAndIndexInferenceModelDoc(TrainedModelDefinitionChunk trainedM public String createAndIndexInferenceModelConfig(ModelSizeInfo inferenceModelSize, TrainedModelType trainedModelType) { if (readyToStoreNewModel.compareAndSet(true, false) == false) { - failureHandler.accept(ExceptionsHelper.serverError( - "new inference model is attempting to be stored before completion previous model storage" - )); + failureHandler.accept( + ExceptionsHelper.serverError("new inference model is attempting to be stored before completion previous model storage") + ); return null; } TrainedModelConfig trainedModelConfig = createTrainedModelConfig(trainedModelType, inferenceModelSize); @@ -125,16 +129,17 @@ public String createAndIndexInferenceModelConfig(ModelSizeInfo inferenceModelSiz public void createAndIndexInferenceModelMetadata(ModelMetadata modelMetadata) { if (Strings.isNullOrEmpty(this.currentModelId.get())) { - failureHandler.accept(ExceptionsHelper.serverError( - "inference model metadata is attempting to be stored before trained model configuration" - )); + failureHandler.accept( + ExceptionsHelper.serverError("inference model metadata is attempting to be stored before trained model configuration") + ); return; } - TrainedModelMetadata trainedModelMetadata = new TrainedModelMetadata(this.currentModelId.get(), + TrainedModelMetadata trainedModelMetadata = new TrainedModelMetadata( + this.currentModelId.get(), modelMetadata.getFeatureImportances(), modelMetadata.getFeatureImportanceBaseline(), - modelMetadata.getHyperparameters()); - + modelMetadata.getHyperparameters() + ); CountDownLatch latch = storeTrainedModelMetadata(trainedModelMetadata); try { @@ -151,58 +156,53 @@ private CountDownLatch storeTrainedModelDoc(TrainedModelDefinitionDoc trainedMod CountDownLatch latch = new CountDownLatch(1); // Latch is attached to this action as it is the last one to execute. - ActionListener refreshListener = new LatchedActionListener<>(ActionListener.wrap( - refreshed -> { - if (refreshed != null) { - LOGGER.debug(() -> new ParameterizedMessage( - "[{}] refreshed inference index after model store", - analytics.getId() - )); - } - }, - e -> LOGGER.warn( - new ParameterizedMessage("[{}] failed to refresh inference index after model store", analytics.getId()), - e) - ), latch); + ActionListener refreshListener = new LatchedActionListener<>(ActionListener.wrap(refreshed -> { + if (refreshed != null) { + LOGGER.debug(() -> new ParameterizedMessage("[{}] refreshed inference index after model store", analytics.getId())); + } + }, e -> LOGGER.warn(new ParameterizedMessage("[{}] failed to refresh inference index after model store", analytics.getId()), e)), + latch + ); // First, store the model and refresh is necessary - ActionListener storeListener = ActionListener.wrap( - r -> { - LOGGER.debug(() -> new ParameterizedMessage( + ActionListener storeListener = ActionListener.wrap(r -> { + LOGGER.debug( + () -> new ParameterizedMessage( "[{}] stored trained model definition chunk [{}] [{}]", analytics.getId(), trainedModelDefinitionDoc.getModelId(), - trainedModelDefinitionDoc.getDocNum())); - if (trainedModelDefinitionDoc.isEos() == false) { - refreshListener.onResponse(null); - return; - } - LOGGER.info( - "[{}] finished storing trained model with id [{}]", - analytics.getId(), - this.currentModelId.get()); - auditor.info(analytics.getId(), "Stored trained model with id [" + this.currentModelId.get() + "]"); - readyToStoreNewModel.set(true); - provider.refreshInferenceIndex(refreshListener); - }, - e -> { - LOGGER.error(new ParameterizedMessage( + trainedModelDefinitionDoc.getDocNum() + ) + ); + if (trainedModelDefinitionDoc.isEos() == false) { + refreshListener.onResponse(null); + return; + } + LOGGER.info("[{}] finished storing trained model with id [{}]", analytics.getId(), this.currentModelId.get()); + auditor.info(analytics.getId(), "Stored trained model with id [" + this.currentModelId.get() + "]"); + readyToStoreNewModel.set(true); + provider.refreshInferenceIndex(refreshListener); + }, e -> { + LOGGER.error( + new ParameterizedMessage( "[{}] error storing trained model definition chunk [{}] with id [{}]", - analytics.getId(), - trainedModelDefinitionDoc.getDocNum(), - trainedModelDefinitionDoc.getModelId() - ), - e); - this.readyToStoreNewModel.set(true); - failureHandler.accept(ExceptionsHelper.serverError( + analytics.getId(), + trainedModelDefinitionDoc.getDocNum(), + trainedModelDefinitionDoc.getModelId() + ), + e + ); + this.readyToStoreNewModel.set(true); + failureHandler.accept( + ExceptionsHelper.serverError( "error storing trained model definition chunk [{}] with id [{}]", e, trainedModelDefinitionDoc.getDocNum(), trainedModelDefinitionDoc.getModelId() - )); - refreshListener.onResponse(null); - } - ); + ) + ); + refreshListener.onResponse(null); + }); provider.storeTrainedModelDefinitionDoc(trainedModelDefinitionDoc, storeListener); return latch; } @@ -211,76 +211,67 @@ private CountDownLatch storeTrainedModelMetadata(TrainedModelMetadata trainedMod CountDownLatch latch = new CountDownLatch(1); // Latch is attached to this action as it is the last one to execute. - ActionListener refreshListener = new LatchedActionListener<>(ActionListener.wrap( - refreshed -> { - if (refreshed != null) { - LOGGER.debug(() -> new ParameterizedMessage( - "[{}] refreshed inference index after model metadata store", - analytics.getId() - )); - } - }, + ActionListener refreshListener = new LatchedActionListener<>(ActionListener.wrap(refreshed -> { + if (refreshed != null) { + LOGGER.debug( + () -> new ParameterizedMessage("[{}] refreshed inference index after model metadata store", analytics.getId()) + ); + } + }, e -> LOGGER.warn( new ParameterizedMessage("[{}] failed to refresh inference index after model metadata store", analytics.getId()), - e) + e + ) ), latch); // First, store the model and refresh is necessary - ActionListener storeListener = ActionListener.wrap( - r -> { - LOGGER.debug( - "[{}] stored trained model metadata with id [{}]", + ActionListener storeListener = ActionListener.wrap(r -> { + LOGGER.debug("[{}] stored trained model metadata with id [{}]", analytics.getId(), this.currentModelId.get()); + readyToStoreNewModel.set(true); + provider.refreshInferenceIndex(refreshListener); + }, e -> { + LOGGER.error( + new ParameterizedMessage( + "[{}] error storing trained model metadata with id [{}]", analytics.getId(), - this.currentModelId.get()); - readyToStoreNewModel.set(true); - provider.refreshInferenceIndex(refreshListener); - }, - e -> { - LOGGER.error( - new ParameterizedMessage( - "[{}] error storing trained model metadata with id [{}]", - analytics.getId(), - trainedModelMetadata.getModelId() - ), - e); - this.readyToStoreNewModel.set(true); - failureHandler.accept(ExceptionsHelper.serverError( - "error storing trained model metadata with id [{}]", - e, - trainedModelMetadata.getModelId())); - refreshListener.onResponse(null); - } - ); + trainedModelMetadata.getModelId() + ), + e + ); + this.readyToStoreNewModel.set(true); + failureHandler.accept( + ExceptionsHelper.serverError("error storing trained model metadata with id [{}]", e, trainedModelMetadata.getModelId()) + ); + refreshListener.onResponse(null); + }); provider.storeTrainedModelMetadata(trainedModelMetadata, storeListener); return latch; } private CountDownLatch storeTrainedModelConfig(TrainedModelConfig trainedModelConfig) { CountDownLatch latch = new CountDownLatch(1); - ActionListener storeListener = ActionListener.wrap( - aBoolean -> { - if (aBoolean == false) { - LOGGER.error("[{}] Storing trained model config responded false", analytics.getId()); - readyToStoreNewModel.set(true); - failureHandler.accept(ExceptionsHelper.serverError("storing trained model config false")); - } else { - LOGGER.debug("[{}] Stored trained model config with id [{}]", analytics.getId(), trainedModelConfig.getModelId()); - } - }, - e -> { - LOGGER.error( - new ParameterizedMessage( - "[{}] error storing trained model config with id [{}]", - analytics.getId(), - trainedModelConfig.getModelId() - ), - e); + ActionListener storeListener = ActionListener.wrap(aBoolean -> { + if (aBoolean == false) { + LOGGER.error("[{}] Storing trained model config responded false", analytics.getId()); readyToStoreNewModel.set(true); - failureHandler.accept(ExceptionsHelper.serverError("error storing trained model config with id [{}]", - e, - trainedModelConfig.getModelId())); + failureHandler.accept(ExceptionsHelper.serverError("storing trained model config false")); + } else { + LOGGER.debug("[{}] Stored trained model config with id [{}]", analytics.getId(), trainedModelConfig.getModelId()); } - ); + }, e -> { + LOGGER.error( + new ParameterizedMessage( + "[{}] error storing trained model config with id [{}]", + analytics.getId(), + trainedModelConfig.getModelId() + ), + e + ); + readyToStoreNewModel.set(true); + failureHandler.accept( + ExceptionsHelper.serverError("error storing trained model config with id [{}]", e, trainedModelConfig.getModelId()) + ); + }); provider.storeTrainedModelConfig(trainedModelConfig, new LatchedActionListener<>(storeListener, latch)); return latch; } @@ -292,8 +283,8 @@ private long customProcessorSize() { } else if (analytics.getAnalysis() instanceof Regression) { preProcessors = ((Regression) analytics.getAnalysis()).getFeatureProcessors(); } - return preProcessors.stream().mapToLong(PreProcessor::ramBytesUsed).sum() - + RamUsageEstimator.NUM_BYTES_OBJECT_REF * preProcessors.size(); + return preProcessors.stream().mapToLong(PreProcessor::ramBytesUsed).sum() + RamUsageEstimator.NUM_BYTES_OBJECT_REF * preProcessors + .size(); } private TrainedModelConfig createTrainedModelConfig(TrainedModelType trainedModelType, ModelSizeInfo modelSize) { @@ -320,8 +311,12 @@ private TrainedModelConfig createTrainedModelConfig(TrainedModelType trainedMode // NOTE: GET _cat/ml/trained_models relies on the creating analytics ID being in the tags .setTags(Collections.singletonList(analytics.getId())) .setDescription(analytics.getDescription()) - .setMetadata(Collections.singletonMap("analytics_config", - XContentHelper.convertToMap(JsonXContent.jsonXContent, analytics.toString(), true))) + .setMetadata( + Collections.singletonMap( + "analytics_config", + XContentHelper.convertToMap(JsonXContent.jsonXContent, analytics.toString(), true) + ) + ) .setEstimatedHeapMemory(modelSize.ramBytesUsed() + customProcessorSize) .setEstimatedOperations(modelSize.numOperations()) .setInput(new TrainedModelInput(fieldNamesWithoutDependentVariable)) @@ -333,10 +328,10 @@ private TrainedModelConfig createTrainedModelConfig(TrainedModelType trainedMode private String getDependentVariable() { if (analytics.getAnalysis() instanceof Classification) { - return ((Classification)analytics.getAnalysis()).getDependentVariable(); + return ((Classification) analytics.getAnalysis()).getDependentVariable(); } if (analytics.getAnalysis() instanceof Regression) { - return ((Regression)analytics.getAnalysis()).getDependentVariable(); + return ((Regression) analytics.getAnalysis()).getDependentVariable(); } return null; } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/DataFrameRowsJoiner.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/DataFrameRowsJoiner.java index fe649840084db..05fd7054b14a4 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/DataFrameRowsJoiner.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/DataFrameRowsJoiner.java @@ -12,8 +12,8 @@ import org.elasticsearch.action.DocWriteRequest; import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.index.IndexRequest; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.Nullable; import org.elasticsearch.search.SearchHit; import org.elasticsearch.tasks.TaskId; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; @@ -48,8 +48,13 @@ class DataFrameRowsJoiner implements AutoCloseable { private volatile String failure; private volatile boolean isCancelled; - DataFrameRowsJoiner(String analyticsId, Settings settings, TaskId parentTaskId, DataFrameDataExtractor dataExtractor, - ResultsPersisterService resultsPersisterService) { + DataFrameRowsJoiner( + String analyticsId, + Settings settings, + TaskId parentTaskId, + DataFrameDataExtractor dataExtractor, + ResultsPersisterService resultsPersisterService + ) { this.analyticsId = Objects.requireNonNull(analyticsId); this.settings = Objects.requireNonNull(settings); this.parentTaskId = Objects.requireNonNull(parentTaskId); @@ -110,7 +115,8 @@ private void executeBulkRequest(BulkRequest bulkRequest) { bulkRequest, analyticsId, () -> isCancelled == false, - retryMessage -> {}); + retryMessage -> {} + ); } private void checkChecksumsMatch(DataFrameDataExtractor.Row row, RowResults result) { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/MemoryUsageEstimationProcessManager.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/MemoryUsageEstimationProcessManager.java index 154744568e52b..2ca810f8750fc 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/MemoryUsageEstimationProcessManager.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/MemoryUsageEstimationProcessManager.java @@ -31,18 +31,22 @@ public class MemoryUsageEstimationProcessManager { private final ExecutorService executorServiceForProcess; private final AnalyticsProcessFactory processFactory; - public MemoryUsageEstimationProcessManager(ExecutorService executorServiceForJob, - ExecutorService executorServiceForProcess, - AnalyticsProcessFactory processFactory) { + public MemoryUsageEstimationProcessManager( + ExecutorService executorServiceForJob, + ExecutorService executorServiceForProcess, + AnalyticsProcessFactory processFactory + ) { this.executorServiceForJob = Objects.requireNonNull(executorServiceForJob); this.executorServiceForProcess = Objects.requireNonNull(executorServiceForProcess); this.processFactory = Objects.requireNonNull(processFactory); } - public void runJobAsync(String jobId, - DataFrameAnalyticsConfig config, - DataFrameDataExtractorFactory dataExtractorFactory, - ActionListener listener) { + public void runJobAsync( + String jobId, + DataFrameAnalyticsConfig config, + DataFrameDataExtractorFactory dataExtractorFactory, + ActionListener listener + ) { executorServiceForJob.execute(() -> { try { MemoryUsageEstimationResult result = runJob(jobId, config, dataExtractorFactory); @@ -53,9 +57,11 @@ public void runJobAsync(String jobId, }); } - private MemoryUsageEstimationResult runJob(String jobId, - DataFrameAnalyticsConfig config, - DataFrameDataExtractorFactory dataExtractorFactory) { + private MemoryUsageEstimationResult runJob( + String jobId, + DataFrameAnalyticsConfig config, + DataFrameDataExtractorFactory dataExtractorFactory + ) { DataFrameDataExtractor dataExtractor = dataExtractorFactory.newExtractor(false); DataFrameDataExtractor.DataSummary dataSummary = dataExtractor.collectDataSummary(); if (dataSummary.rows == 0) { @@ -65,38 +71,41 @@ private MemoryUsageEstimationResult runJob(String jobId, + "in any indexed documents, and you will have to switch to explicit field selection and include only fields that " + "exist in indexed documents.", jobId, - Strings.arrayToCommaDelimitedString(config.getSource().getIndex())); + Strings.arrayToCommaDelimitedString(config.getSource().getIndex()) + ); } Set categoricalFields = dataExtractor.getCategoricalFields(config.getAnalysis()); - AnalyticsProcessConfig processConfig = - new AnalyticsProcessConfig( - jobId, - dataSummary.rows, - dataSummary.cols, - // For memory estimation the model memory limit here should be set high enough not to trigger an error when C++ code - // compares the limit to the result of estimation. - ByteSizeValue.ofPb(1), - 1, - "", - categoricalFields, - config.getAnalysis(), - dataExtractorFactory.getExtractedFields()); - AnalyticsProcess process = - processFactory.createAnalyticsProcess( - config, - processConfig, - false, - executorServiceForProcess, - // The handler passed here will never be called as AbstractNativeProcess.detectCrash method returns early when - // (processInStream == null) which is the case for MemoryUsageEstimationProcess. - reason -> {}); + AnalyticsProcessConfig processConfig = new AnalyticsProcessConfig( + jobId, + dataSummary.rows, + dataSummary.cols, + // For memory estimation the model memory limit here should be set high enough not to trigger an error when C++ code + // compares the limit to the result of estimation. + ByteSizeValue.ofPb(1), + 1, + "", + categoricalFields, + config.getAnalysis(), + dataExtractorFactory.getExtractedFields() + ); + AnalyticsProcess process = processFactory.createAnalyticsProcess( + config, + processConfig, + false, + executorServiceForProcess, + // The handler passed here will never be called as AbstractNativeProcess.detectCrash method returns early when + // (processInStream == null) which is the case for MemoryUsageEstimationProcess. + reason -> {} + ); try { return readResult(jobId, process); } catch (Exception e) { - String errorMsg = - new ParameterizedMessage( - "[{}] Error while processing process output [{}], process errors: [{}]", - jobId, e.getMessage(), process.readError()).getFormattedMessage(); + String errorMsg = new ParameterizedMessage( + "[{}] Error while processing process output [{}], process errors: [{}]", + jobId, + e.getMessage(), + process.readError() + ).getFormattedMessage(); throw ExceptionsHelper.serverError(errorMsg, e); } finally { try { @@ -104,10 +113,12 @@ private MemoryUsageEstimationResult runJob(String jobId, process.close(); LOGGER.debug("[{}] Closed process", jobId); } catch (Exception e) { - String errorMsg = - new ParameterizedMessage( - "[{}] Error while closing process [{}], process errors: [{}]", - jobId, e.getMessage(), process.readError()).getFormattedMessage(); + String errorMsg = new ParameterizedMessage( + "[{}] Error while closing process [{}], process errors: [{}]", + jobId, + e.getMessage(), + process.readError() + ).getFormattedMessage(); throw ExceptionsHelper.serverError(errorMsg, e); } } @@ -119,14 +130,14 @@ private MemoryUsageEstimationResult runJob(String jobId, private static MemoryUsageEstimationResult readResult(String jobId, AnalyticsProcess process) { Iterator iterator = process.readAnalyticsResults(); if (iterator.hasNext() == false) { - String errorMsg = - new ParameterizedMessage("[{}] Memory usage estimation process returned no results", jobId).getFormattedMessage(); + String errorMsg = new ParameterizedMessage("[{}] Memory usage estimation process returned no results", jobId) + .getFormattedMessage(); throw ExceptionsHelper.serverError(errorMsg); } MemoryUsageEstimationResult result = iterator.next(); if (iterator.hasNext()) { - String errorMsg = - new ParameterizedMessage("[{}] Memory usage estimation process returned more than one result", jobId).getFormattedMessage(); + String errorMsg = new ParameterizedMessage("[{}] Memory usage estimation process returned more than one result", jobId) + .getFormattedMessage(); throw ExceptionsHelper.serverError(errorMsg); } return result; diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/NativeAnalyticsProcess.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/NativeAnalyticsProcess.java index b0a804f68dad6..2ff1bcd0becd4 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/NativeAnalyticsProcess.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/NativeAnalyticsProcess.java @@ -11,9 +11,9 @@ import org.apache.logging.log4j.message.ParameterizedMessage; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.Client; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.ml.job.persistence.AnomalyDetectorsIndex; import org.elasticsearch.xpack.ml.dataframe.process.results.AnalyticsResult; import org.elasticsearch.xpack.ml.process.NativeController; @@ -35,12 +35,27 @@ public class NativeAnalyticsProcess extends AbstractNativeAnalyticsProcess filesToDelete, Consumer onProcessCrash, - AnalyticsProcessConfig config, - NamedXContentRegistry namedXContentRegistry) { - super(NAME, AnalyticsResult.PARSER, jobId, nativeController, processPipes, numberOfFields, filesToDelete, onProcessCrash, - namedXContentRegistry); + protected NativeAnalyticsProcess( + String jobId, + NativeController nativeController, + ProcessPipes processPipes, + int numberOfFields, + List filesToDelete, + Consumer onProcessCrash, + AnalyticsProcessConfig config, + NamedXContentRegistry namedXContentRegistry + ) { + super( + NAME, + AnalyticsResult.PARSER, + jobId, + nativeController, + processPipes, + numberOfFields, + filesToDelete, + onProcessCrash, + namedXContentRegistry + ); this.config = Objects.requireNonNull(config); } @@ -55,8 +70,7 @@ public void persistState() { } @Override - public void persistState(long snapshotTimestamp, String snapshotId, String snapshotDescription) { - } + public void persistState(long snapshotTimestamp, String snapshotId, String snapshotDescription) {} @Override public void writeEndOfDataMessage() throws IOException { @@ -81,7 +95,8 @@ public void restoreState(Client client, String stateDocIdPrefix) throws IOExcept // We fetch the documents one at a time because all together they can amount to too much memory SearchResponse stateResponse = client.prepareSearch(AnomalyDetectorsIndex.jobStateIndexPattern()) .setSize(1) - .setQuery(QueryBuilders.idsQuery().addIds(stateDocIdPrefix + ++docNum)).get(); + .setQuery(QueryBuilders.idsQuery().addIds(stateDocIdPrefix + ++docNum)) + .get(); if (stateResponse.getHits().getHits().length == 0) { break; } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/NativeAnalyticsProcessFactory.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/NativeAnalyticsProcessFactory.java index d5179cbc8f75f..6288256234958 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/NativeAnalyticsProcessFactory.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/NativeAnalyticsProcessFactory.java @@ -9,11 +9,11 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException; -import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.env.Environment; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsConfig; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; import org.elasticsearch.xpack.ml.MachineLearning; @@ -50,12 +50,14 @@ public class NativeAnalyticsProcessFactory implements AnalyticsProcessFactory onProcessCrash) { + public NativeAnalyticsProcess createAnalyticsProcess( + DataFrameAnalyticsConfig config, + AnalyticsProcessConfig analyticsProcessConfig, + boolean hasState, + ExecutorService executorService, + Consumer onProcessCrash + ) { String jobId = config.getId(); List filesToDelete = new ArrayList<>(); // When the stop API is called the process is killed. As it may take some time for the OS (especially Windows) // to delete the named pipes, we use a unique identifier to avoid reusing an older named pipe if the task // gets restarted immediately after stopping. - ProcessPipes processPipes = new ProcessPipes(env, NAMED_PIPE_HELPER, processConnectTimeout, AnalyticsBuilder.ANALYTICS, jobId, - counter.incrementAndGet(), false, true, true, hasState, config.getAnalysis().persistsState()); + ProcessPipes processPipes = new ProcessPipes( + env, + NAMED_PIPE_HELPER, + processConnectTimeout, + AnalyticsBuilder.ANALYTICS, + jobId, + counter.incrementAndGet(), + false, + true, + true, + hasState, + config.getAnalysis().persistsState() + ); // The extra 2 are for the checksum and the control field int numberOfFields = analyticsProcessConfig.cols() + 2; createNativeProcess(jobId, analyticsProcessConfig, filesToDelete, processPipes); - NativeAnalyticsProcess analyticsProcess = - new NativeAnalyticsProcess( - jobId, nativeController, processPipes, numberOfFields, filesToDelete, - onProcessCrash, analyticsProcessConfig, namedXContentRegistry); + NativeAnalyticsProcess analyticsProcess = new NativeAnalyticsProcess( + jobId, + nativeController, + processPipes, + numberOfFields, + filesToDelete, + onProcessCrash, + analyticsProcessConfig, + namedXContentRegistry + ); try { startProcess(config, executorService, analyticsProcess); @@ -109,8 +132,8 @@ public NativeAnalyticsProcess createAnalyticsProcess(DataFrameAnalyticsConfig co } } - private void startProcess(DataFrameAnalyticsConfig config, ExecutorService executorService, - NativeAnalyticsProcess process) throws IOException { + private void startProcess(DataFrameAnalyticsConfig config, ExecutorService executorService, NativeAnalyticsProcess process) + throws IOException { if (config.getAnalysis().persistsState()) { IndexingStateProcessor stateProcessor = new IndexingStateProcessor(config.getId(), resultsPersisterService, auditor); process.start(executorService, stateProcessor); @@ -119,10 +142,19 @@ private void startProcess(DataFrameAnalyticsConfig config, ExecutorService execu } } - private void createNativeProcess(String jobId, AnalyticsProcessConfig analyticsProcessConfig, List filesToDelete, - ProcessPipes processPipes) { - AnalyticsBuilder analyticsBuilder = - new AnalyticsBuilder(env::tmpFile, nativeController, processPipes, analyticsProcessConfig, filesToDelete); + private void createNativeProcess( + String jobId, + AnalyticsProcessConfig analyticsProcessConfig, + List filesToDelete, + ProcessPipes processPipes + ) { + AnalyticsBuilder analyticsBuilder = new AnalyticsBuilder( + env::tmpFile, + nativeController, + processPipes, + analyticsProcessConfig, + filesToDelete + ); try { analyticsBuilder.build(); } catch (InterruptedException e) { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/NativeMemoryUsageEstimationProcess.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/NativeMemoryUsageEstimationProcess.java index f9f15845f35bd..3786cccab82a4 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/NativeMemoryUsageEstimationProcess.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/NativeMemoryUsageEstimationProcess.java @@ -20,10 +20,25 @@ public class NativeMemoryUsageEstimationProcess extends AbstractNativeAnalyticsP private static final String NAME = "memory_usage_estimation"; - protected NativeMemoryUsageEstimationProcess(String jobId, NativeController nativeController, ProcessPipes processPipes, - int numberOfFields, List filesToDelete, Consumer onProcessCrash) { - super(NAME, MemoryUsageEstimationResult.PARSER, jobId, nativeController, processPipes, numberOfFields, filesToDelete, - onProcessCrash, NamedXContentRegistry.EMPTY); + protected NativeMemoryUsageEstimationProcess( + String jobId, + NativeController nativeController, + ProcessPipes processPipes, + int numberOfFields, + List filesToDelete, + Consumer onProcessCrash + ) { + super( + NAME, + MemoryUsageEstimationResult.PARSER, + jobId, + nativeController, + processPipes, + numberOfFields, + filesToDelete, + onProcessCrash, + NamedXContentRegistry.EMPTY + ); } @Override diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/NativeMemoryUsageEstimationProcessFactory.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/NativeMemoryUsageEstimationProcessFactory.java index dc4d94d918ad0..c4b738a182d03 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/NativeMemoryUsageEstimationProcessFactory.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/NativeMemoryUsageEstimationProcessFactory.java @@ -9,8 +9,8 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.env.Environment; import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsConfig; @@ -49,8 +49,8 @@ public NativeMemoryUsageEstimationProcessFactory(Environment env, NativeControll this.nodeName = clusterService.getNodeName(); this.counter = new AtomicLong(0); setProcessConnectTimeout(MachineLearning.PROCESS_CONNECT_TIMEOUT.get(env.settings())); - clusterService.getClusterSettings().addSettingsUpdateConsumer( - MachineLearning.PROCESS_CONNECT_TIMEOUT, this::setProcessConnectTimeout); + clusterService.getClusterSettings() + .addSettingsUpdateConsumer(MachineLearning.PROCESS_CONNECT_TIMEOUT, this::setProcessConnectTimeout); } void setProcessConnectTimeout(TimeValue processConnectTimeout) { @@ -59,18 +59,29 @@ void setProcessConnectTimeout(TimeValue processConnectTimeout) { @Override public NativeMemoryUsageEstimationProcess createAnalyticsProcess( - DataFrameAnalyticsConfig config, - AnalyticsProcessConfig analyticsProcessConfig, - boolean hasState, - ExecutorService executorService, - Consumer onProcessCrash) { + DataFrameAnalyticsConfig config, + AnalyticsProcessConfig analyticsProcessConfig, + boolean hasState, + ExecutorService executorService, + Consumer onProcessCrash + ) { List filesToDelete = new ArrayList<>(); // Since memory estimation can be called many times in quick succession for the same config the config ID alone is not - // sufficient to guarantee that the memory estimation process pipe names are unique. Therefore an increasing counter + // sufficient to guarantee that the memory estimation process pipe names are unique. Therefore an increasing counter // value is passed as well as the config ID to ensure uniqueness between calls. ProcessPipes processPipes = new ProcessPipes( - env, NAMED_PIPE_HELPER, processConnectTimeout, AnalyticsBuilder.ANALYTICS, config.getId(), counter.incrementAndGet(), - false, false, true, false, false); + env, + NAMED_PIPE_HELPER, + processConnectTimeout, + AnalyticsBuilder.ANALYTICS, + config.getId(), + counter.incrementAndGet(), + false, + false, + true, + false, + false + ); createNativeProcess(config.getId(), analyticsProcessConfig, filesToDelete, processPipes); @@ -80,7 +91,8 @@ public NativeMemoryUsageEstimationProcess createAnalyticsProcess( processPipes, 0, filesToDelete, - onProcessCrash); + onProcessCrash + ); try { process.start(executorService); @@ -97,11 +109,19 @@ public NativeMemoryUsageEstimationProcess createAnalyticsProcess( } } - private void createNativeProcess(String jobId, AnalyticsProcessConfig analyticsProcessConfig, List filesToDelete, - ProcessPipes processPipes) { - AnalyticsBuilder analyticsBuilder = - new AnalyticsBuilder(env::tmpFile, nativeController, processPipes, analyticsProcessConfig, filesToDelete) - .performMemoryUsageEstimationOnly(); + private void createNativeProcess( + String jobId, + AnalyticsProcessConfig analyticsProcessConfig, + List filesToDelete, + ProcessPipes processPipes + ) { + AnalyticsBuilder analyticsBuilder = new AnalyticsBuilder( + env::tmpFile, + nativeController, + processPipes, + analyticsProcessConfig, + filesToDelete + ).performMemoryUsageEstimationOnly(); try { analyticsBuilder.build(); } catch (InterruptedException e) { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/results/AnalyticsResult.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/results/AnalyticsResult.java index 9457fe35d35a8..85976d760f28c 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/results/AnalyticsResult.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/results/AnalyticsResult.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.ml.dataframe.process.results; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ml.dataframe.stats.classification.ClassificationStats; @@ -36,18 +36,20 @@ public class AnalyticsResult implements ToXContentObject { private static final ParseField REGRESSION_STATS = new ParseField("regression_stats"); private static final ParseField MODEL_METADATA = new ParseField("model_metadata"); - public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(TYPE.getPreferredName(), - a -> new AnalyticsResult( - (RowResults) a[0], - (PhaseProgress) a[1], - (MemoryUsage) a[2], - (OutlierDetectionStats) a[3], - (ClassificationStats) a[4], - (RegressionStats) a[5], - (ModelSizeInfo) a[6], - (TrainedModelDefinitionChunk) a[7], - (ModelMetadata) a[8] - )); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + TYPE.getPreferredName(), + a -> new AnalyticsResult( + (RowResults) a[0], + (PhaseProgress) a[1], + (MemoryUsage) a[2], + (OutlierDetectionStats) a[3], + (ClassificationStats) a[4], + (RegressionStats) a[5], + (ModelSizeInfo) a[6], + (TrainedModelDefinitionChunk) a[7], + (ModelMetadata) a[8] + ) + ); static { PARSER.declareObject(optionalConstructorArg(), RowResults.PARSER, RowResults.TYPE); @@ -71,15 +73,17 @@ public class AnalyticsResult implements ToXContentObject { private final TrainedModelDefinitionChunk trainedModelDefinitionChunk; private final ModelMetadata modelMetadata; - private AnalyticsResult(@Nullable RowResults rowResults, - @Nullable PhaseProgress phaseProgress, - @Nullable MemoryUsage memoryUsage, - @Nullable OutlierDetectionStats outlierDetectionStats, - @Nullable ClassificationStats classificationStats, - @Nullable RegressionStats regressionStats, - @Nullable ModelSizeInfo modelSizeInfo, - @Nullable TrainedModelDefinitionChunk trainedModelDefinitionChunk, - @Nullable ModelMetadata modelMetadata) { + private AnalyticsResult( + @Nullable RowResults rowResults, + @Nullable PhaseProgress phaseProgress, + @Nullable MemoryUsage memoryUsage, + @Nullable OutlierDetectionStats outlierDetectionStats, + @Nullable ClassificationStats classificationStats, + @Nullable RegressionStats regressionStats, + @Nullable ModelSizeInfo modelSizeInfo, + @Nullable TrainedModelDefinitionChunk trainedModelDefinitionChunk, + @Nullable ModelMetadata modelMetadata + ) { this.rowResults = rowResults; this.phaseProgress = phaseProgress; this.memoryUsage = memoryUsage; @@ -184,8 +188,17 @@ public boolean equals(Object other) { @Override public int hashCode() { - return Objects.hash(rowResults, phaseProgress, memoryUsage, outlierDetectionStats, classificationStats, - regressionStats, modelSizeInfo, trainedModelDefinitionChunk, modelMetadata); + return Objects.hash( + rowResults, + phaseProgress, + memoryUsage, + outlierDetectionStats, + classificationStats, + regressionStats, + modelSizeInfo, + trainedModelDefinitionChunk, + modelMetadata + ); } public static Builder builder() { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/results/MemoryUsageEstimationResult.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/results/MemoryUsageEstimationResult.java index 1073a7741ee0e..0dcefe382cd7a 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/results/MemoryUsageEstimationResult.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/results/MemoryUsageEstimationResult.java @@ -6,11 +6,11 @@ */ package org.elasticsearch.xpack.ml.dataframe.process.results; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -26,23 +26,25 @@ public class MemoryUsageEstimationResult implements ToXContentObject { public static final ParseField EXPECTED_MEMORY_WITHOUT_DISK = new ParseField("expected_memory_without_disk"); public static final ParseField EXPECTED_MEMORY_WITH_DISK = new ParseField("expected_memory_with_disk"); - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>( - TYPE.getPreferredName(), - true, - args -> new MemoryUsageEstimationResult((ByteSizeValue) args[0], (ByteSizeValue) args[1])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + TYPE.getPreferredName(), + true, + args -> new MemoryUsageEstimationResult((ByteSizeValue) args[0], (ByteSizeValue) args[1]) + ); static { PARSER.declareField( optionalConstructorArg(), (p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), EXPECTED_MEMORY_WITHOUT_DISK.getPreferredName()), EXPECTED_MEMORY_WITHOUT_DISK, - ObjectParser.ValueType.VALUE); + ObjectParser.ValueType.VALUE + ); PARSER.declareField( optionalConstructorArg(), (p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), EXPECTED_MEMORY_WITH_DISK.getPreferredName()), EXPECTED_MEMORY_WITH_DISK, - ObjectParser.ValueType.VALUE); + ObjectParser.ValueType.VALUE + ); } private final ByteSizeValue expectedMemoryWithoutDisk; diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/results/ModelMetadata.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/results/ModelMetadata.java index 6abb4e7a11531..eef55cd411a15 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/results/ModelMetadata.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/results/ModelMetadata.java @@ -7,13 +7,13 @@ package org.elasticsearch.xpack.ml.dataframe.process.results; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.metadata.FeatureImportanceBaseline; -import org.elasticsearch.xpack.core.ml.inference.trainedmodel.metadata.TotalFeatureImportance; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.metadata.Hyperparameters; +import org.elasticsearch.xpack.core.ml.inference.trainedmodel.metadata.TotalFeatureImportance; import java.io.IOException; import java.util.List; @@ -31,7 +31,8 @@ public class ModelMetadata implements ToXContentObject { @SuppressWarnings("unchecked") public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( "trained_model_metadata", - a -> new ModelMetadata((List) a[0], (FeatureImportanceBaseline) a[1], (List) a[2])); + a -> new ModelMetadata((List) a[0], (FeatureImportanceBaseline) a[1], (List) a[2]) + ); static { PARSER.declareObjectArray(constructorArg(), TotalFeatureImportance.STRICT_PARSER, TOTAL_FEATURE_IMPORTANCE); @@ -43,9 +44,11 @@ public class ModelMetadata implements ToXContentObject { private final FeatureImportanceBaseline featureImportanceBaseline; private final List hyperparameters; - public ModelMetadata(List featureImportances, + public ModelMetadata( + List featureImportances, FeatureImportanceBaseline featureImportanceBaseline, - List hyperparameters) { + List hyperparameters + ) { this.featureImportances = featureImportances; this.featureImportanceBaseline = featureImportanceBaseline; this.hyperparameters = hyperparameters; diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/results/RowResults.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/results/RowResults.java index e1e5062d9fc85..e375b3d27cc60 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/results/RowResults.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/results/RowResults.java @@ -6,8 +6,8 @@ */ package org.elasticsearch.xpack.ml.dataframe.process.results; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -24,8 +24,10 @@ public class RowResults implements ToXContentObject { public static final ParseField RESULTS = new ParseField("results"); @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(TYPE.getPreferredName(), - a -> new RowResults((Integer) a[0], (Map) a[1])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + TYPE.getPreferredName(), + a -> new RowResults((Integer) a[0], (Map) a[1]) + ); static { PARSER.declareInt(constructorArg(), CHECKSUM); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/results/TrainedModelDefinitionChunk.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/results/TrainedModelDefinitionChunk.java index 56a3da58f61b5..652134fd27108 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/results/TrainedModelDefinitionChunk.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/process/results/TrainedModelDefinitionChunk.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.ml.dataframe.process.results; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ml.inference.TrainedModelConfig; @@ -28,7 +28,8 @@ public class TrainedModelDefinitionChunk implements ToXContentObject { public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( "chunked_trained_model_definition", - a -> new TrainedModelDefinitionChunk((String) a[0], (Integer) a[1], (Boolean) a[2])); + a -> new TrainedModelDefinitionChunk((String) a[0], (Integer) a[1], (Boolean) a[2]) + ); static { PARSER.declareString(constructorArg(), DEFINITION); @@ -47,8 +48,7 @@ public TrainedModelDefinitionChunk(String definition, int docNum, Boolean eos) { } public TrainedModelDefinitionDoc createTrainedModelDoc(String modelId) { - return new TrainedModelDefinitionDoc.Builder() - .setCompressionVersion(TrainedModelConfig.CURRENT_DEFINITION_COMPRESSION_VERSION) + return new TrainedModelDefinitionDoc.Builder().setCompressionVersion(TrainedModelConfig.CURRENT_DEFINITION_COMPRESSION_VERSION) .setModelId(modelId) .setDefinitionLength(definition.length()) .setDocNum(docNum) @@ -78,9 +78,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; TrainedModelDefinitionChunk that = (TrainedModelDefinitionChunk) o; - return docNum == that.docNum - && Objects.equals(definition, that.definition) - && Objects.equals(eos, that.eos); + return docNum == that.docNum && Objects.equals(definition, that.definition) && Objects.equals(eos, that.eos); } @Override diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/stats/DataCountsTracker.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/stats/DataCountsTracker.java index 8fdae7f0fc92c..33361d6297de4 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/stats/DataCountsTracker.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/stats/DataCountsTracker.java @@ -38,12 +38,7 @@ public void incrementSkippedDocsCount() { } public DataCounts report() { - return new DataCounts( - jobId, - trainingDocsCount, - testDocsCount, - skippedDocsCount - ); + return new DataCounts(jobId, trainingDocsCount, testDocsCount, skippedDocsCount); } public void reset() { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/stats/StatsHolder.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/stats/StatsHolder.java index 5153ac524d4e7..063a11b5ff570 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/stats/StatsHolder.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/stats/StatsHolder.java @@ -26,8 +26,12 @@ public class StatsHolder { private final AtomicReference analysisStatsHolder; private final DataCountsTracker dataCountsTracker; - public StatsHolder(List progress, @Nullable MemoryUsage memoryUsage, @Nullable AnalysisStats analysisStats, - DataCounts dataCounts) { + public StatsHolder( + List progress, + @Nullable MemoryUsage memoryUsage, + @Nullable AnalysisStats analysisStats, + DataCounts dataCounts + ) { progressTracker = new ProgressTracker(progress); memoryUsageHolder = new AtomicReference<>(memoryUsage); analysisStatsHolder = new AtomicReference<>(analysisStats); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/stats/StatsPersister.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/stats/StatsPersister.java index 0457c9d9d7936..e9729d159465c 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/stats/StatsPersister.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/stats/StatsPersister.java @@ -39,7 +39,8 @@ public StatsPersister(String jobId, ResultsPersisterService resultsPersisterServ public void persistWithRetry(ToXContentObject result, Function docIdSupplier) { try { - resultsPersisterService.indexWithRetry(jobId, + resultsPersisterService.indexWithRetry( + jobId, MlStatsIndex.writeAlias(), result, new ToXContent.MapParams(Collections.singletonMap(ToXContentParams.FOR_INTERNAL_STORAGE, "true")), @@ -47,8 +48,12 @@ public void persistWithRetry(ToXContentObject result, Function d docIdSupplier.apply(jobId), true, () -> true, - retryMessage -> - LOGGER.debug("[{}] failed to persist result with id [{}]; {}", jobId, docIdSupplier.apply(jobId), retryMessage) + retryMessage -> LOGGER.debug( + "[{}] failed to persist result with id [{}]; {}", + jobId, + docIdSupplier.apply(jobId), + retryMessage + ) ); } catch (IOException ioe) { LOGGER.error(() -> new ParameterizedMessage("[{}] Failed serializing stats result", jobId), ioe); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/steps/AbstractDataFrameAnalyticsStep.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/steps/AbstractDataFrameAnalyticsStep.java index e41c573df9527..a08ddb1de5aad 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/steps/AbstractDataFrameAnalyticsStep.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/steps/AbstractDataFrameAnalyticsStep.java @@ -35,8 +35,12 @@ abstract class AbstractDataFrameAnalyticsStep implements DataFrameAnalyticsStep protected final DataFrameAnalyticsAuditor auditor; protected final DataFrameAnalyticsConfig config; - AbstractDataFrameAnalyticsStep(NodeClient client, DataFrameAnalyticsTask task, DataFrameAnalyticsAuditor auditor, - DataFrameAnalyticsConfig config) { + AbstractDataFrameAnalyticsStep( + NodeClient client, + DataFrameAnalyticsTask task, + DataFrameAnalyticsAuditor auditor, + DataFrameAnalyticsConfig config + ) { this.client = Objects.requireNonNull(client); this.task = Objects.requireNonNull(task); this.auditor = Objects.requireNonNull(auditor); @@ -63,22 +67,25 @@ public final void execute(ActionListener listener) { listener.onResponse(new StepResponse(true)); return; } - doExecute(ActionListener.wrap( - stepResponse -> { - // We persist progress at the end of each step to ensure we do not have - // to repeat the step in case the node goes down without getting a chance to persist progress. - task.persistProgress(() -> listener.onResponse(stepResponse)); - }, - listener::onFailure - )); + doExecute(ActionListener.wrap(stepResponse -> { + // We persist progress at the end of each step to ensure we do not have + // to repeat the step in case the node goes down without getting a chance to persist progress. + task.persistProgress(() -> listener.onResponse(stepResponse)); + }, listener::onFailure)); } protected abstract void doExecute(ActionListener listener); protected void refreshDestAsync(ActionListener refreshListener) { ParentTaskAssigningClient parentTaskClient = parentTaskClient(); - executeWithHeadersAsync(config.getHeaders(), ML_ORIGIN, parentTaskClient, RefreshAction.INSTANCE, - new RefreshRequest(config.getDest().getIndex()), refreshListener); + executeWithHeadersAsync( + config.getHeaders(), + ML_ORIGIN, + parentTaskClient, + RefreshAction.INSTANCE, + new RefreshRequest(config.getDest().getIndex()), + refreshListener + ); } protected boolean shouldSkipIfTaskIsStopping() { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/steps/AnalysisStep.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/steps/AnalysisStep.java index 860a5188a48fd..8c40fc64b0f41 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/steps/AnalysisStep.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/steps/AnalysisStep.java @@ -24,8 +24,13 @@ public class AnalysisStep extends AbstractDataFrameAnalyticsStep { private final AnalyticsProcessManager processManager; - public AnalysisStep(NodeClient client, DataFrameAnalyticsTask task, DataFrameAnalyticsAuditor auditor, DataFrameAnalyticsConfig config, - AnalyticsProcessManager processManager) { + public AnalysisStep( + NodeClient client, + DataFrameAnalyticsTask task, + DataFrameAnalyticsAuditor auditor, + DataFrameAnalyticsConfig config, + AnalyticsProcessManager processManager + ) { super(client, task, auditor, config); this.processManager = Objects.requireNonNull(processManager); } @@ -58,15 +63,12 @@ protected void doExecute(ActionListener listener) { listener::onFailure ); - ActionListener refreshListener = ActionListener.wrap( - refreshResponse -> { - // TODO This could fail with errors. In that case we get stuck with the copied index. - // We could delete the index in case of failure or we could try building the factory before reindexing - // to catch the error early on. - DataFrameDataExtractorFactory.createForDestinationIndex(parentTaskClient, config, dataExtractorFactoryListener); - }, - dataExtractorFactoryListener::onFailure - ); + ActionListener refreshListener = ActionListener.wrap(refreshResponse -> { + // TODO This could fail with errors. In that case we get stuck with the copied index. + // We could delete the index in case of failure or we could try building the factory before reindexing + // to catch the error early on. + DataFrameDataExtractorFactory.createForDestinationIndex(parentTaskClient, config, dataExtractorFactoryListener); + }, dataExtractorFactoryListener::onFailure); // First we need to refresh the dest index to ensure data is searchable in case the job // was stopped after reindexing was complete but before the index was refreshed. diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/steps/DataFrameAnalyticsStep.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/steps/DataFrameAnalyticsStep.java index 40dc102945fd4..7797e2f25e6b4 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/steps/DataFrameAnalyticsStep.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/steps/DataFrameAnalyticsStep.java @@ -15,7 +15,10 @@ public interface DataFrameAnalyticsStep { enum Name { - REINDEXING, ANALYSIS, INFERENCE, FINAL; + REINDEXING, + ANALYSIS, + INFERENCE, + FINAL; @Override public String toString() { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/steps/FinalStep.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/steps/FinalStep.java index 2581a29f0d555..372c75276607f 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/steps/FinalStep.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/steps/FinalStep.java @@ -76,10 +76,11 @@ protected void doExecute(ActionListener listener) { private void indexDataCounts(ActionListener listener) { DataCounts dataCounts = task.getStatsHolder().getDataCountsTracker().report(); try (XContentBuilder builder = XContentFactory.jsonBuilder()) { - dataCounts.toXContent(builder, new ToXContent.MapParams( - Collections.singletonMap(ToXContentParams.FOR_INTERNAL_STORAGE, "true"))); - IndexRequest indexRequest = new IndexRequest(MlStatsIndex.writeAlias()) - .id(DataCounts.documentId(config.getId())) + dataCounts.toXContent( + builder, + new ToXContent.MapParams(Collections.singletonMap(ToXContentParams.FOR_INTERNAL_STORAGE, "true")) + ); + IndexRequest indexRequest = new IndexRequest(MlStatsIndex.writeAlias()).id(DataCounts.documentId(config.getId())) .setRequireAlias(true) .source(builder); executeAsyncWithOrigin(parentTaskClient(), ML_ORIGIN, IndexAction.INSTANCE, indexRequest, listener); @@ -96,8 +97,9 @@ private void refreshIndices(ActionListener listener) { ); refreshRequest.indicesOptions(IndicesOptions.lenientExpandOpen()); - LOGGER.debug(() -> new ParameterizedMessage("[{}] Refreshing indices {}", config.getId(), - Arrays.toString(refreshRequest.indices()))); + LOGGER.debug( + () -> new ParameterizedMessage("[{}] Refreshing indices {}", config.getId(), Arrays.toString(refreshRequest.indices())) + ); executeAsyncWithOrigin(parentTaskClient(), ML_ORIGIN, RefreshAction.INSTANCE, refreshRequest, listener); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/steps/InferenceStep.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/steps/InferenceStep.java index d0a7fd2452183..f2053b33cc38e 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/steps/InferenceStep.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/steps/InferenceStep.java @@ -44,8 +44,14 @@ public class InferenceStep extends AbstractDataFrameAnalyticsStep { private final ThreadPool threadPool; private final InferenceRunner inferenceRunner; - public InferenceStep(NodeClient client, DataFrameAnalyticsTask task, DataFrameAnalyticsAuditor auditor, DataFrameAnalyticsConfig config, - ThreadPool threadPool, InferenceRunner inferenceRunner) { + public InferenceStep( + NodeClient client, + DataFrameAnalyticsTask task, + DataFrameAnalyticsAuditor auditor, + DataFrameAnalyticsConfig config, + ThreadPool threadPool, + InferenceRunner inferenceRunner + ) { super(client, task, auditor, config); this.threadPool = Objects.requireNonNull(threadPool); this.inferenceRunner = Objects.requireNonNull(inferenceRunner); @@ -59,33 +65,32 @@ public Name name() { @Override protected void doExecute(ActionListener listener) { if (config.getAnalysis().supportsInference() == false) { - LOGGER.debug(() -> new ParameterizedMessage( - "[{}] Inference step completed immediately as analysis does not support inference", config.getId())); + LOGGER.debug( + () -> new ParameterizedMessage( + "[{}] Inference step completed immediately as analysis does not support inference", + config.getId() + ) + ); listener.onResponse(new StepResponse(false)); return; } - ActionListener modelIdListener = ActionListener.wrap( - modelId -> runInference(modelId, listener), - listener::onFailure - ); - - ActionListener testDocsExistListener = ActionListener.wrap( - testDocsExist -> { - if (testDocsExist) { - getModelId(modelIdListener); - } else { - // no need to run inference at all so let us skip - // loading the model in memory. - LOGGER.debug(() -> new ParameterizedMessage( - "[{}] Inference step completed immediately as there are no test docs", config.getId())); - task.getStatsHolder().getProgressTracker().updateInferenceProgress(100); - listener.onResponse(new StepResponse(isTaskStopping())); - return; - } - }, - listener::onFailure - ); + ActionListener modelIdListener = ActionListener.wrap(modelId -> runInference(modelId, listener), listener::onFailure); + + ActionListener testDocsExistListener = ActionListener.wrap(testDocsExist -> { + if (testDocsExist) { + getModelId(modelIdListener); + } else { + // no need to run inference at all so let us skip + // loading the model in memory. + LOGGER.debug( + () -> new ParameterizedMessage("[{}] Inference step completed immediately as there are no test docs", config.getId()) + ); + task.getStatsHolder().getProgressTracker().updateInferenceProgress(100); + listener.onResponse(new StepResponse(isTaskStopping())); + return; + } + }, listener::onFailure); ActionListener refreshDestListener = ActionListener.wrap( refreshResponse -> searchIfTestDocsExist(testDocsExistListener), @@ -113,39 +118,45 @@ private void runInference(String modelId, ActionListener listener) private void searchIfTestDocsExist(ActionListener listener) { SearchRequest searchRequest = new SearchRequest(config.getDest().getIndex()); searchRequest.indicesOptions(MlIndicesUtils.addIgnoreUnavailable(SearchRequest.DEFAULT_INDICES_OPTIONS)); - searchRequest.source().query(QueryBuilders.boolQuery().mustNot( - QueryBuilders.termQuery(config.getDest().getResultsField() + "." + DestinationIndex.IS_TRAINING, true))); + searchRequest.source() + .query( + QueryBuilders.boolQuery() + .mustNot(QueryBuilders.termQuery(config.getDest().getResultsField() + "." + DestinationIndex.IS_TRAINING, true)) + ); searchRequest.source().size(0); searchRequest.source().trackTotalHitsUpTo(1); - executeAsyncWithOrigin(client, ML_ORIGIN, SearchAction.INSTANCE, searchRequest, ActionListener.wrap( - searchResponse -> listener.onResponse(searchResponse.getHits().getTotalHits().value > 0), - listener::onFailure - )); + executeAsyncWithOrigin( + client, + ML_ORIGIN, + SearchAction.INSTANCE, + searchRequest, + ActionListener.wrap( + searchResponse -> listener.onResponse(searchResponse.getHits().getTotalHits().value > 0), + listener::onFailure + ) + ); } private void getModelId(ActionListener listener) { SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.size(1); searchSourceBuilder.fetchSource(false); - searchSourceBuilder.query(QueryBuilders.boolQuery() - .filter(QueryBuilders.termQuery(TrainedModelConfig.TAGS.getPreferredName(), config.getId())) + searchSourceBuilder.query( + QueryBuilders.boolQuery().filter(QueryBuilders.termQuery(TrainedModelConfig.TAGS.getPreferredName(), config.getId())) ); searchSourceBuilder.sort(TrainedModelConfig.CREATE_TIME.getPreferredName(), SortOrder.DESC); SearchRequest searchRequest = new SearchRequest(InferenceIndexConstants.INDEX_PATTERN); searchRequest.source(searchSourceBuilder); - executeAsyncWithOrigin(client, ML_ORIGIN, SearchAction.INSTANCE, searchRequest, ActionListener.wrap( - searchResponse -> { - SearchHit[] hits = searchResponse.getHits().getHits(); - if (hits.length == 0) { - listener.onFailure(new ResourceNotFoundException("No model could be found to perform inference")); - } else { - listener.onResponse(hits[0].getId()); - } - }, - listener::onFailure - )); + executeAsyncWithOrigin(client, ML_ORIGIN, SearchAction.INSTANCE, searchRequest, ActionListener.wrap(searchResponse -> { + SearchHit[] hits = searchResponse.getHits().getHits(); + if (hits.length == 0) { + listener.onFailure(new ResourceNotFoundException("No model could be found to perform inference")); + } else { + listener.onResponse(hits[0].getId()); + } + }, listener::onFailure)); } @Override diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/steps/ReindexingStep.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/steps/ReindexingStep.java index 7ee553c1cfe36..a93ff43a4dfa3 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/steps/ReindexingStep.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/steps/ReindexingStep.java @@ -24,9 +24,9 @@ import org.elasticsearch.client.ParentTaskAssigningClient; import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.cluster.service.ClusterService; +import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.index.IndexNotFoundException; import org.elasticsearch.index.mapper.SeqNoFieldMapper; import org.elasticsearch.index.reindex.BulkByScrollResponse; @@ -65,8 +65,13 @@ public class ReindexingStep extends AbstractDataFrameAnalyticsStep { private volatile Long reindexingTaskId; private volatile boolean isReindexingFinished; - public ReindexingStep(ClusterService clusterService, NodeClient client, DataFrameAnalyticsTask task, DataFrameAnalyticsAuditor auditor, - DataFrameAnalyticsConfig config) { + public ReindexingStep( + ClusterService clusterService, + NodeClient client, + DataFrameAnalyticsTask task, + DataFrameAnalyticsAuditor auditor, + DataFrameAnalyticsConfig config + ) { super(client, task, auditor, config); this.clusterService = Objects.requireNonNull(clusterService); } @@ -83,134 +88,149 @@ protected void doExecute(ActionListener listener) { final ParentTaskAssigningClient parentTaskClient = parentTaskClient(); // Reindexing is complete - ActionListener reindexCompletedListener = ActionListener.wrap( - reindexResponse -> { - - // If the reindex task is canceled, this listener is called. - // Consequently, we should not signal reindex completion. - if (isTaskStopping()) { - LOGGER.debug("[{}] task is stopping. Stopping reindexing before it is finished.", config.getId()); - listener.onResponse(new StepResponse(true)); - return; - } - - synchronized (this) { - reindexingTaskId = null; - } + ActionListener reindexCompletedListener = ActionListener.wrap(reindexResponse -> { + + // If the reindex task is canceled, this listener is called. + // Consequently, we should not signal reindex completion. + if (isTaskStopping()) { + LOGGER.debug("[{}] task is stopping. Stopping reindexing before it is finished.", config.getId()); + listener.onResponse(new StepResponse(true)); + return; + } - Exception reindexError = getReindexError(config.getId(), reindexResponse); - if (reindexError != null) { - listener.onFailure(reindexError); - return; - } + synchronized (this) { + reindexingTaskId = null; + } - auditor.info( - config.getId(), - Messages.getMessage(Messages.DATA_FRAME_ANALYTICS_AUDIT_FINISHED_REINDEXING, config.getDest().getIndex(), - reindexResponse.getTook())); - - isReindexingFinished = true; - task.getStatsHolder().getProgressTracker().updateReindexingProgress(100); - - LOGGER.debug("[{}] Reindex completed; created [{}]; retries [{}]", config.getId(), reindexResponse.getCreated(), - reindexResponse.getBulkRetries()); - - listener.onResponse(new StepResponse(false)); - }, - error -> { - if (isTaskStopping() && isTaskCancelledException(error)) { - LOGGER.debug(new ParameterizedMessage("[{}] Caught task cancelled exception while task is stopping", - config.getId()), error); - listener.onResponse(new StepResponse(true)); - } else { - listener.onFailure(error); - } + Exception reindexError = getReindexError(config.getId(), reindexResponse); + if (reindexError != null) { + listener.onFailure(reindexError); + return; } - ); - // Reindex - ActionListener copyIndexCreatedListener = ActionListener.wrap( - createIndexResponse -> { - ReindexRequest reindexRequest = new ReindexRequest(); - reindexRequest.setRefresh(true); - reindexRequest.setSourceIndices(config.getSource().getIndex()); - reindexRequest.setSourceQuery(config.getSource().getParsedQuery()); - reindexRequest.getSearchRequest().allowPartialSearchResults(false); - reindexRequest.getSearchRequest().source().fetchSource(config.getSource().getSourceFiltering()); - reindexRequest.getSearchRequest().source().sort(SeqNoFieldMapper.NAME, SortOrder.ASC); - reindexRequest.setDestIndex(config.getDest().getIndex()); - - // We explicitly set slices to 1 as we cannot parallelize in order to have the incremental id - reindexRequest.setSlices(1); - Map counterValueParam = new HashMap<>(); - counterValueParam.put("value", -1); - reindexRequest.setScript( - new Script( - Script.DEFAULT_SCRIPT_TYPE, - Script.DEFAULT_SCRIPT_LANG, - // We use indirection here because top level params are immutable. - // This is a work around at the moment but the plan is to make this a feature of reindex API. - "ctx._source." + DestinationIndex.INCREMENTAL_ID + " = ++params.counter.value", - Collections.singletonMap("counter", counterValueParam) - ) + auditor.info( + config.getId(), + Messages.getMessage( + Messages.DATA_FRAME_ANALYTICS_AUDIT_FINISHED_REINDEXING, + config.getDest().getIndex(), + reindexResponse.getTook() + ) + ); + + isReindexingFinished = true; + task.getStatsHolder().getProgressTracker().updateReindexingProgress(100); + + LOGGER.debug( + "[{}] Reindex completed; created [{}]; retries [{}]", + config.getId(), + reindexResponse.getCreated(), + reindexResponse.getBulkRetries() + ); + + listener.onResponse(new StepResponse(false)); + }, error -> { + if (isTaskStopping() && isTaskCancelledException(error)) { + LOGGER.debug( + new ParameterizedMessage("[{}] Caught task cancelled exception while task is stopping", config.getId()), + error ); + listener.onResponse(new StepResponse(true)); + } else { + listener.onFailure(error); + } + }); - reindexRequest.setParentTask(getParentTaskId()); - - final ThreadContext threadContext = parentTaskClient.threadPool().getThreadContext(); - final Supplier supplier = threadContext.newRestorableContext(false); - try (ThreadContext.StoredContext ignore = threadContext.stashWithOrigin(ML_ORIGIN)) { - synchronized (this) { - if (isTaskStopping()) { - LOGGER.debug("[{}] task is stopping. Stopping reindexing before it is finished.", config.getId()); - listener.onResponse(new StepResponse(true)); - return; - } - LOGGER.info("[{}] Started reindexing", config.getId()); - Task reindexTask = client.executeLocally(ReindexAction.INSTANCE, reindexRequest, - new ContextPreservingActionListener<>(supplier, reindexCompletedListener)); - reindexingTaskId = reindexTask.getId(); + // Reindex + ActionListener copyIndexCreatedListener = ActionListener.wrap(createIndexResponse -> { + ReindexRequest reindexRequest = new ReindexRequest(); + reindexRequest.setRefresh(true); + reindexRequest.setSourceIndices(config.getSource().getIndex()); + reindexRequest.setSourceQuery(config.getSource().getParsedQuery()); + reindexRequest.getSearchRequest().allowPartialSearchResults(false); + reindexRequest.getSearchRequest().source().fetchSource(config.getSource().getSourceFiltering()); + reindexRequest.getSearchRequest().source().sort(SeqNoFieldMapper.NAME, SortOrder.ASC); + reindexRequest.setDestIndex(config.getDest().getIndex()); + + // We explicitly set slices to 1 as we cannot parallelize in order to have the incremental id + reindexRequest.setSlices(1); + Map counterValueParam = new HashMap<>(); + counterValueParam.put("value", -1); + reindexRequest.setScript( + new Script( + Script.DEFAULT_SCRIPT_TYPE, + Script.DEFAULT_SCRIPT_LANG, + // We use indirection here because top level params are immutable. + // This is a work around at the moment but the plan is to make this a feature of reindex API. + "ctx._source." + DestinationIndex.INCREMENTAL_ID + " = ++params.counter.value", + Collections.singletonMap("counter", counterValueParam) + ) + ); + + reindexRequest.setParentTask(getParentTaskId()); + + final ThreadContext threadContext = parentTaskClient.threadPool().getThreadContext(); + final Supplier supplier = threadContext.newRestorableContext(false); + try (ThreadContext.StoredContext ignore = threadContext.stashWithOrigin(ML_ORIGIN)) { + synchronized (this) { + if (isTaskStopping()) { + LOGGER.debug("[{}] task is stopping. Stopping reindexing before it is finished.", config.getId()); + listener.onResponse(new StepResponse(true)); + return; } - auditor.info(config.getId(), - Messages.getMessage(Messages.DATA_FRAME_ANALYTICS_AUDIT_STARTED_REINDEXING, config.getDest().getIndex())); + LOGGER.info("[{}] Started reindexing", config.getId()); + Task reindexTask = client.executeLocally( + ReindexAction.INSTANCE, + reindexRequest, + new ContextPreservingActionListener<>(supplier, reindexCompletedListener) + ); + reindexingTaskId = reindexTask.getId(); } - }, - reindexCompletedListener::onFailure - ); + auditor.info( + config.getId(), + Messages.getMessage(Messages.DATA_FRAME_ANALYTICS_AUDIT_STARTED_REINDEXING, config.getDest().getIndex()) + ); + } + }, reindexCompletedListener::onFailure); // Create destination index if it does not exist - ActionListener destIndexListener = ActionListener.wrap( - indexResponse -> { + ActionListener destIndexListener = ActionListener.wrap(indexResponse -> { + auditor.info( + config.getId(), + Messages.getMessage(Messages.DATA_FRAME_ANALYTICS_AUDIT_REUSING_DEST_INDEX, indexResponse.indices()[0]) + ); + LOGGER.info("[{}] Using existing destination index [{}]", config.getId(), indexResponse.indices()[0]); + DestinationIndex.updateMappingsToDestIndex( + parentTaskClient, + config, + indexResponse, + ActionListener.wrap(acknowledgedResponse -> copyIndexCreatedListener.onResponse(null), copyIndexCreatedListener::onFailure) + ); + }, e -> { + if (ExceptionsHelper.unwrapCause(e) instanceof IndexNotFoundException) { auditor.info( config.getId(), - Messages.getMessage(Messages.DATA_FRAME_ANALYTICS_AUDIT_REUSING_DEST_INDEX, indexResponse.indices()[0])); - LOGGER.info("[{}] Using existing destination index [{}]", config.getId(), indexResponse.indices()[0]); - DestinationIndex.updateMappingsToDestIndex(parentTaskClient, config, indexResponse, ActionListener.wrap( - acknowledgedResponse -> copyIndexCreatedListener.onResponse(null), - copyIndexCreatedListener::onFailure - )); - }, - e -> { - if (ExceptionsHelper.unwrapCause(e) instanceof IndexNotFoundException) { - auditor.info( - config.getId(), - Messages.getMessage(Messages.DATA_FRAME_ANALYTICS_AUDIT_CREATING_DEST_INDEX, config.getDest().getIndex())); - LOGGER.info("[{}] Creating destination index [{}]", config.getId(), config.getDest().getIndex()); - DestinationIndex.createDestinationIndex(parentTaskClient, Clock.systemUTC(), config, copyIndexCreatedListener); - } else { - copyIndexCreatedListener.onFailure(e); - } + Messages.getMessage(Messages.DATA_FRAME_ANALYTICS_AUDIT_CREATING_DEST_INDEX, config.getDest().getIndex()) + ); + LOGGER.info("[{}] Creating destination index [{}]", config.getId(), config.getDest().getIndex()); + DestinationIndex.createDestinationIndex(parentTaskClient, Clock.systemUTC(), config, copyIndexCreatedListener); + } else { + copyIndexCreatedListener.onFailure(e); } + }); + + ClientHelper.executeWithHeadersAsync( + config.getHeaders(), + ML_ORIGIN, + parentTaskClient, + GetIndexAction.INSTANCE, + new GetIndexRequest().indices(config.getDest().getIndex()), + destIndexListener ); - - ClientHelper.executeWithHeadersAsync(config.getHeaders(), ML_ORIGIN, parentTaskClient, GetIndexAction.INSTANCE, - new GetIndexRequest().indices(config.getDest().getIndex()), destIndexListener); } private static Exception getReindexError(String jobId, BulkByScrollResponse reindexResponse) { if (reindexResponse.getBulkFailures().isEmpty() == false) { - LOGGER.error("[{}] reindexing encountered {} failures", jobId, - reindexResponse.getBulkFailures().size()); + LOGGER.error("[{}] reindexing encountered {} failures", jobId, reindexResponse.getBulkFailures().size()); for (BulkItemResponse.Failure failure : reindexResponse.getBulkFailures()) { LOGGER.error("[{}] reindexing failure: {}", jobId, failure); } @@ -280,15 +300,17 @@ private CancelTasksResponse cancelTaskWithinMlOriginContext(CancelTasksRequest c @Override public void updateProgress(ActionListener listener) { - getReindexTaskProgress(ActionListener.wrap( - // We set reindexing progress at least to 1 for a running process to be able to - // distinguish a job that is running for the first time against a job that is restarting. - reindexTaskProgress -> { - task.getStatsHolder().getProgressTracker().updateReindexingProgress(Math.max(1, reindexTaskProgress)); - listener.onResponse(null); - }, - listener::onFailure - )); + getReindexTaskProgress( + ActionListener.wrap( + // We set reindexing progress at least to 1 for a running process to be able to + // distinguish a job that is running for the first time against a job that is restarting. + reindexTaskProgress -> { + task.getStatsHolder().getProgressTracker().updateReindexingProgress(Math.max(1, reindexTaskProgress)); + listener.onResponse(null); + }, + listener::onFailure + ) + ); } private void getReindexTaskProgress(ActionListener listener) { @@ -300,22 +322,19 @@ private void getReindexTaskProgress(ActionListener listener) { GetTaskRequest getTaskRequest = new GetTaskRequest(); getTaskRequest.setTaskId(reindexTaskId); - client.admin().cluster().getTask(getTaskRequest, ActionListener.wrap( - taskResponse -> { - TaskResult taskResult = taskResponse.getTask(); - BulkByScrollTask.Status taskStatus = (BulkByScrollTask.Status) taskResult.getTask().getStatus(); - int progress = (int) (taskStatus.getCreated() * 100.0 / taskStatus.getTotal()); - listener.onResponse(progress); - }, - error -> { - if (ExceptionsHelper.unwrapCause(error) instanceof ResourceNotFoundException) { - // The task is not present which means either it has not started yet or it finished. - listener.onResponse(isReindexingFinished ? 100 : 0); - } else { - listener.onFailure(error); - } + client.admin().cluster().getTask(getTaskRequest, ActionListener.wrap(taskResponse -> { + TaskResult taskResult = taskResponse.getTask(); + BulkByScrollTask.Status taskStatus = (BulkByScrollTask.Status) taskResult.getTask().getStatus(); + int progress = (int) (taskStatus.getCreated() * 100.0 / taskStatus.getTotal()); + listener.onResponse(progress); + }, error -> { + if (ExceptionsHelper.unwrapCause(error) instanceof ResourceNotFoundException) { + // The task is not present which means either it has not started yet or it finished. + listener.onResponse(isReindexingFinished ? 100 : 0); + } else { + listener.onFailure(error); } - )); + })); } @Nullable diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/traintestsplit/AbstractReservoirTrainTestSplitter.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/traintestsplit/AbstractReservoirTrainTestSplitter.java index e2033926f9581..7dfda17ad056a 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/traintestsplit/AbstractReservoirTrainTestSplitter.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/traintestsplit/AbstractReservoirTrainTestSplitter.java @@ -24,8 +24,7 @@ abstract class AbstractReservoirTrainTestSplitter implements TrainTestSplitter { private final double samplingRatio; private final Random random; - AbstractReservoirTrainTestSplitter(List fieldNames, String dependentVariable, double trainingPercent, - long randomizeSeed) { + AbstractReservoirTrainTestSplitter(List fieldNames, String dependentVariable, double trainingPercent, long randomizeSeed) { assert trainingPercent >= 1.0 && trainingPercent <= 100.0; this.dependentVariableIndex = findDependentVariableIndex(fieldNames, dependentVariable); this.samplingRatio = trainingPercent / 100.0; diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/traintestsplit/SingleClassReservoirTrainTestSplitter.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/traintestsplit/SingleClassReservoirTrainTestSplitter.java index eeb195bb1b3fd..ea96bb3b99a20 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/traintestsplit/SingleClassReservoirTrainTestSplitter.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/traintestsplit/SingleClassReservoirTrainTestSplitter.java @@ -13,8 +13,13 @@ public class SingleClassReservoirTrainTestSplitter extends AbstractReservoirTrai private final SampleInfo sampleInfo; - SingleClassReservoirTrainTestSplitter(List fieldNames, String dependentVariable, double trainingPercent, - long randomizeSeed, long classCount) { + SingleClassReservoirTrainTestSplitter( + List fieldNames, + String dependentVariable, + double trainingPercent, + long randomizeSeed, + long classCount + ) { super(fieldNames, dependentVariable, trainingPercent, randomizeSeed); sampleInfo = new SampleInfo(classCount); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/traintestsplit/StratifiedTrainTestSplitter.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/traintestsplit/StratifiedTrainTestSplitter.java index 5edc71c8929e9..33de5c2e54eb8 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/traintestsplit/StratifiedTrainTestSplitter.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/traintestsplit/StratifiedTrainTestSplitter.java @@ -19,8 +19,13 @@ public class StratifiedTrainTestSplitter extends AbstractReservoirTrainTestSplit private final Map classSamples; - public StratifiedTrainTestSplitter(List fieldNames, String dependentVariable, Map classCounts, - double trainingPercent, long randomizeSeed) { + public StratifiedTrainTestSplitter( + List fieldNames, + String dependentVariable, + Map classCounts, + double trainingPercent, + long randomizeSeed + ) { super(fieldNames, dependentVariable, trainingPercent, randomizeSeed); this.classSamples = new HashMap<>(); classCounts.entrySet().forEach(entry -> classSamples.put(entry.getKey(), new SampleInfo(entry.getValue()))); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/traintestsplit/TrainTestSplitterFactory.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/traintestsplit/TrainTestSplitterFactory.java index 69fc5fc707fbc..900a2904fd563 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/traintestsplit/TrainTestSplitterFactory.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/traintestsplit/TrainTestSplitterFactory.java @@ -59,10 +59,19 @@ private TrainTestSplitter createSingleClassSplitter(Regression regression) { .setQuery(QueryBuilders.existsQuery(regression.getDependentVariable())); try { - SearchResponse searchResponse = ClientHelper.executeWithHeaders(config.getHeaders(), ClientHelper.ML_ORIGIN, client, - searchRequestBuilder::get); - return new SingleClassReservoirTrainTestSplitter(fieldNames, regression.getDependentVariable(), - regression.getTrainingPercent(), regression.getRandomizeSeed(), searchResponse.getHits().getTotalHits().value); + SearchResponse searchResponse = ClientHelper.executeWithHeaders( + config.getHeaders(), + ClientHelper.ML_ORIGIN, + client, + searchRequestBuilder::get + ); + return new SingleClassReservoirTrainTestSplitter( + fieldNames, + regression.getDependentVariable(), + regression.getTrainingPercent(), + regression.getRandomizeSeed(), + searchResponse.getHits().getTotalHits().value + ); } catch (Exception e) { ParameterizedMessage msg = new ParameterizedMessage("[{}] Error searching total number of training docs", config.getId()); LOGGER.error(msg, e); @@ -75,13 +84,19 @@ private TrainTestSplitter createStratifiedSplitter(Classification classification SearchRequestBuilder searchRequestBuilder = client.prepareSearch(config.getDest().getIndex()) .setSize(0) .setAllowPartialSearchResults(false) - .addAggregation(AggregationBuilders.terms(aggName) - .field(classification.getDependentVariable()) - .size(Classification.MAX_DEPENDENT_VARIABLE_CARDINALITY)); + .addAggregation( + AggregationBuilders.terms(aggName) + .field(classification.getDependentVariable()) + .size(Classification.MAX_DEPENDENT_VARIABLE_CARDINALITY) + ); try { - SearchResponse searchResponse = ClientHelper.executeWithHeaders(config.getHeaders(), ClientHelper.ML_ORIGIN, client, - searchRequestBuilder::get); + SearchResponse searchResponse = ClientHelper.executeWithHeaders( + config.getHeaders(), + ClientHelper.ML_ORIGIN, + client, + searchRequestBuilder::get + ); Aggregations aggs = searchResponse.getAggregations(); Terms terms = aggs.get(aggName); Map classCounts = new HashMap<>(); @@ -89,8 +104,13 @@ private TrainTestSplitter createStratifiedSplitter(Classification classification classCounts.put(String.valueOf(bucket.getKey()), bucket.getDocCount()); } - return new StratifiedTrainTestSplitter(fieldNames, classification.getDependentVariable(), classCounts, - classification.getTrainingPercent(), classification.getRandomizeSeed()); + return new StratifiedTrainTestSplitter( + fieldNames, + classification.getDependentVariable(), + classCounts, + classification.getTrainingPercent(), + classification.getRandomizeSeed() + ); } catch (Exception e) { ParameterizedMessage msg = new ParameterizedMessage("[{}] Dependent variable terms search failed", config.getId()); LOGGER.error(msg, e); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/extractor/ExtractedField.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/extractor/ExtractedField.java index 2446df57ff497..988263745e415 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/extractor/ExtractedField.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/extractor/ExtractedField.java @@ -16,7 +16,9 @@ public interface ExtractedField { enum Method { - SOURCE, DOC_VALUE, SCRIPT_FIELD + SOURCE, + DOC_VALUE, + SCRIPT_FIELD } /** diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/extractor/ExtractedFields.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/extractor/ExtractedFields.java index d9365850ffdfa..1f9c24ed83883 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/extractor/ExtractedFields.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/extractor/ExtractedFields.java @@ -32,12 +32,15 @@ public class ExtractedFields { private final String[] sourceFields; private final Map cardinalitiesForFieldsWithConstraints; - public ExtractedFields(List allFields, - List processedFields, - Map cardinalitiesForFieldsWithConstraints) { + public ExtractedFields( + List allFields, + List processedFields, + Map cardinalitiesForFieldsWithConstraints + ) { this.allFields = new ArrayList<>(allFields); this.docValueFields = filterFields(ExtractedField.Method.DOC_VALUE, allFields); - this.sourceFields = filterFields(ExtractedField.Method.SOURCE, allFields).stream().map(ExtractedField::getSearchField) + this.sourceFields = filterFields(ExtractedField.Method.SOURCE, allFields).stream() + .map(ExtractedField::getSearchField) .toArray(String[]::new); this.cardinalitiesForFieldsWithConstraints = Collections.unmodifiableMap(cardinalitiesForFieldsWithConstraints); this.processedFields = processedFields == null ? Collections.emptyList() : processedFields; @@ -69,46 +72,55 @@ public Map getCardinalitiesForFieldsWithConstraints() { public String[] extractOrganicFeatureNames() { Set processedFieldInputs = getProcessedFieldInputs(); - return allFields - .stream() + return allFields.stream() .map(ExtractedField::getName) .filter(f -> processedFieldInputs.contains(f) == false) .toArray(String[]::new); } public String[] extractProcessedFeatureNames() { - return processedFields - .stream() - .map(ProcessedField::getOutputFieldNames) - .flatMap(List::stream) - .toArray(String[]::new); + return processedFields.stream().map(ProcessedField::getOutputFieldNames).flatMap(List::stream).toArray(String[]::new); } private static List filterFields(ExtractedField.Method method, List fields) { return fields.stream().filter(field -> field.getMethod() == method).collect(Collectors.toList()); } - public static ExtractedFields build(Set allFields, - Set scriptFields, - Set searchRuntimeFields, - FieldCapabilitiesResponse fieldsCapabilities, - Map cardinalitiesForFieldsWithConstraints, - List processedFields) { - ExtractionMethodDetector extractionMethodDetector = - new ExtractionMethodDetector(scriptFields, fieldsCapabilities, searchRuntimeFields); + public static ExtractedFields build( + Set allFields, + Set scriptFields, + Set searchRuntimeFields, + FieldCapabilitiesResponse fieldsCapabilities, + Map cardinalitiesForFieldsWithConstraints, + List processedFields + ) { + ExtractionMethodDetector extractionMethodDetector = new ExtractionMethodDetector( + scriptFields, + fieldsCapabilities, + searchRuntimeFields + ); return new ExtractedFields( allFields.stream().map(extractionMethodDetector::detect).collect(Collectors.toList()), processedFields, - cardinalitiesForFieldsWithConstraints); + cardinalitiesForFieldsWithConstraints + ); } - public static ExtractedFields build(Set allFields, - Set scriptFields, - FieldCapabilitiesResponse fieldsCapabilities, - Map cardinalitiesForFieldsWithConstraints, - List processedFields) { - return build(allFields, scriptFields, Collections.emptySet(), fieldsCapabilities, - cardinalitiesForFieldsWithConstraints, processedFields); + public static ExtractedFields build( + Set allFields, + Set scriptFields, + FieldCapabilitiesResponse fieldsCapabilities, + Map cardinalitiesForFieldsWithConstraints, + List processedFields + ) { + return build( + allFields, + scriptFields, + Collections.emptySet(), + fieldsCapabilities, + cardinalitiesForFieldsWithConstraints, + processedFields + ); } public static TimeField newTimeField(String name, ExtractedField.Method method) { @@ -125,8 +137,11 @@ public static class ExtractionMethodDetector { private final Set searchRuntimeFields; private final FieldCapabilitiesResponse fieldsCapabilities; - public ExtractionMethodDetector(Set scriptFields, FieldCapabilitiesResponse fieldsCapabilities, - Set searchRuntimeFields) { + public ExtractionMethodDetector( + Set scriptFields, + FieldCapabilitiesResponse fieldsCapabilities, + Set searchRuntimeFields + ) { this.scriptFields = scriptFields; this.fieldsCapabilities = fieldsCapabilities; this.searchRuntimeFields = searchRuntimeFields; diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/extractor/GeoShapeField.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/extractor/GeoShapeField.java index 07b8b19677841..6161728b05208 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/extractor/GeoShapeField.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/extractor/GeoShapeField.java @@ -61,7 +61,7 @@ private String handleString(String geoString) { if (geometry.type() != ShapeType.POINT) { throw new IllegalArgumentException("Unexpected non-point geo_shape type: " + geometry.type().name()); } - Point pt = ((Point)geometry); + Point pt = ((Point) geometry); return pt.getY() + "," + pt.getX(); } else { throw new IllegalArgumentException("Unexpected value for a geo_shape field: " + geoString); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/extractor/SourceField.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/extractor/SourceField.java index 1bb5294b0ea4a..463e9b174db6b 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/extractor/SourceField.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/extractor/SourceField.java @@ -42,7 +42,7 @@ public Object[] value(SearchHit hit) { List asList = (List) values; return asList.toArray(new Object[0]); } else { - return new Object[]{values}; + return new Object[] { values }; } } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/extractor/TimeField.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/extractor/TimeField.java index 2ef89813384f1..b8e87d0dcfb66 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/extractor/TimeField.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/extractor/TimeField.java @@ -24,7 +24,7 @@ public class TimeField extends AbstractField { public TimeField(String name, Method method) { // This class intentionally reports the possible types rather than the types reported by - // field caps at the point of construction. This means that it will continue to work if, + // field caps at the point of construction. This means that it will continue to work if, // for example, a newly created index has a "date_nanos" time field when in all the indices // that matched the pattern when this constructor was called the field had type "date". super(name, TYPES); @@ -46,7 +46,7 @@ public Object[] value(SearchHit hit) { return value; } if (value[0] instanceof String) { // doc_value field with the epoch_millis format - value[0] = TimeUtils.parseToEpochMs((String)value[0]); + value[0] = TimeUtils.parseToEpochMs((String) value[0]); } else if (value[0] instanceof Long == false) { // pre-6.0 field throw new IllegalStateException("Unexpected value for a time field: " + value[0].getClass()); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/ModelAliasMetadata.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/ModelAliasMetadata.java index 9dbfe78b75897..844f1905736d4 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/ModelAliasMetadata.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/ModelAliasMetadata.java @@ -14,10 +14,10 @@ import org.elasticsearch.cluster.DiffableUtils; import org.elasticsearch.cluster.NamedDiff; import org.elasticsearch.cluster.metadata.Metadata; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -55,7 +55,7 @@ public static NamedDiff readDiffFrom(StreamInput in) throws IOE NAME, // to protect BWC serialization true, - args -> new ModelAliasMetadata((Map)args[0]) + args -> new ModelAliasMetadata((Map) args[0]) ); static { @@ -139,8 +139,12 @@ static class ModelAliasMetadataDiff implements NamedDiff { } ModelAliasMetadataDiff(StreamInput in) throws IOException { - this.modelAliasesDiff = DiffableUtils.readJdkMapDiff(in, DiffableUtils.getStringKeySerializer(), - ModelAliasEntry::new, ModelAliasEntry::readDiffFrom); + this.modelAliasesDiff = DiffableUtils.readJdkMapDiff( + in, + DiffableUtils.getStringKeySerializer(), + ModelAliasEntry::new, + ModelAliasEntry::readDiffFrom + ); } @Override @@ -164,7 +168,7 @@ public static class ModelAliasEntry extends AbstractDiffable im "model_alias_metadata_alias_entry", // to protect BWC serialization true, - args -> new ModelAliasEntry((String)args[0]) + args -> new ModelAliasEntry((String) args[0]) ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), MODEL_ID); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/TrainedModelStatsService.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/TrainedModelStatsService.java index e8707accf4b6f..1a2c97c082e7b 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/TrainedModelStatsService.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/TrainedModelStatsService.java @@ -25,14 +25,14 @@ import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.component.LifecycleListener; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.indices.InvalidAliasNameException; import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptType; import org.elasticsearch.threadpool.Scheduler; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xpack.core.ml.MlMetadata; import org.elasticsearch.xpack.core.ml.MlStatsIndex; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.InferenceStats; @@ -54,27 +54,29 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; - public class TrainedModelStatsService { private static final Logger logger = LogManager.getLogger(TrainedModelStatsService.class); private static final TimeValue PERSISTENCE_INTERVAL = TimeValue.timeValueSeconds(1); - private static final String STATS_UPDATE_SCRIPT_TEMPLATE = "" + - " ctx._source.{0} += params.{0};\n" + - " ctx._source.{1} += params.{1};\n" + - " ctx._source.{2} += params.{2};\n" + - " ctx._source.{3} += params.{3};\n" + - " ctx._source.{4} = params.{4};"; + private static final String STATS_UPDATE_SCRIPT_TEMPLATE = "" + + " ctx._source.{0} += params.{0};\n" + + " ctx._source.{1} += params.{1};\n" + + " ctx._source.{2} += params.{2};\n" + + " ctx._source.{3} += params.{3};\n" + + " ctx._source.{4} = params.{4};"; // Script to only update if stats have increased since last persistence - private static final String STATS_UPDATE_SCRIPT = Messages.getMessage(STATS_UPDATE_SCRIPT_TEMPLATE, + private static final String STATS_UPDATE_SCRIPT = Messages.getMessage( + STATS_UPDATE_SCRIPT_TEMPLATE, InferenceStats.MISSING_ALL_FIELDS_COUNT.getPreferredName(), InferenceStats.INFERENCE_COUNT.getPreferredName(), InferenceStats.FAILURE_COUNT.getPreferredName(), InferenceStats.CACHE_MISS_COUNT.getPreferredName(), - InferenceStats.TIMESTAMP.getPreferredName()); - private static final ToXContent.Params FOR_INTERNAL_STORAGE_PARAMS = - new ToXContent.MapParams(Collections.singletonMap(ToXContentParams.FOR_INTERNAL_STORAGE, "true")); + InferenceStats.TIMESTAMP.getPreferredName() + ); + private static final ToXContent.Params FOR_INTERNAL_STORAGE_PARAMS = new ToXContent.MapParams( + Collections.singletonMap(ToXContentParams.FOR_INTERNAL_STORAGE, "true") + ); private final Map statsQueue; private final ResultsPersisterService resultsPersisterService; @@ -85,11 +87,13 @@ public class TrainedModelStatsService { private volatile boolean stopped; private volatile ClusterState clusterState; - public TrainedModelStatsService(ResultsPersisterService resultsPersisterService, - OriginSettingClient client, - IndexNameExpressionResolver indexNameExpressionResolver, - ClusterService clusterService, - ThreadPool threadPool) { + public TrainedModelStatsService( + ResultsPersisterService resultsPersisterService, + OriginSettingClient client, + IndexNameExpressionResolver indexNameExpressionResolver, + ClusterService clusterService, + ThreadPool threadPool + ) { this.resultsPersisterService = resultsPersisterService; this.client = client; this.indexNameExpressionResolver = indexNameExpressionResolver; @@ -123,10 +127,12 @@ void setClusterState(ClusterChangedEvent event) { */ public void queueStats(InferenceStats stats, boolean flush) { if (stats.hasStats()) { - statsQueue.compute(InferenceStats.docId(stats.getModelId(), stats.getNodeId()), - (k, previousStats) -> previousStats == null ? - stats : - InferenceStats.accumulator(stats).merge(previousStats).currentStats(stats.getTimeStamp())); + statsQueue.compute( + InferenceStats.docId(stats.getModelId(), stats.getNodeId()), + (k, previousStats) -> previousStats == null + ? stats + : InferenceStats.accumulator(stats).merge(previousStats).currentStats(stats.getTimeStamp()) + ); } if (flush) { threadPool.executor(MachineLearning.UTILITY_THREAD_POOL_NAME).execute(this::updateStats); @@ -151,9 +157,11 @@ private boolean shouldStop() { void start() { logger.debug("About to start TrainedModelStatsService"); stopped = false; - scheduledFuture = threadPool.scheduleWithFixedDelay(this::updateStats, + scheduledFuture = threadPool.scheduleWithFixedDelay( + this::updateStats, PERSISTENCE_INTERVAL, - MachineLearning.UTILITY_THREAD_POOL_NAME); + MachineLearning.UTILITY_THREAD_POOL_NAME + ); } void updateStats() { @@ -176,7 +184,7 @@ void updateStats() { try { logger.debug("About to create the stats index as it does not exist yet"); createStatsIndexIfNecessary(); - } catch(Exception e){ + } catch (Exception e) { // This exception occurs if, for some reason, the `createStatsIndexAndAliasIfNecessary` fails due to // a concrete index of the alias name already existing. This error is recoverable eventually, but // should NOT cause us to lose statistics. @@ -191,7 +199,7 @@ void updateStats() { // We want a copy as the underlying concurrent map could be changed while iterating // We don't want to accidentally grab updates twice Set keys = new HashSet<>(statsQueue.keySet()); - for(String k : keys) { + for (String k : keys) { InferenceStats inferenceStats = statsQueue.remove(k); if (inferenceStats != null) { stats.add(inferenceStats); @@ -211,19 +219,18 @@ void updateStats() { } String jobPattern = stats.stream().map(InferenceStats::getModelId).collect(Collectors.joining(",")); try { - resultsPersisterService.bulkIndexWithRetry(bulkRequest, - jobPattern, - () -> shouldStop() == false, - (msg) -> {}); + resultsPersisterService.bulkIndexWithRetry(bulkRequest, jobPattern, () -> shouldStop() == false, (msg) -> {}); } catch (ElasticsearchException ex) { logger.warn(() -> new ParameterizedMessage("failed to store stats for [{}]", jobPattern), ex); } } static boolean verifyIndicesExistAndPrimaryShardsAreActive(ClusterState clusterState, IndexNameExpressionResolver expressionResolver) { - String[] indices = expressionResolver.concreteIndexNames(clusterState, + String[] indices = expressionResolver.concreteIndexNames( + clusterState, IndicesOptions.LENIENT_EXPAND_OPEN_HIDDEN, - MlStatsIndex.writeAlias()); + MlStatsIndex.writeAlias() + ); // If there are no indices, we need to make sure we attempt to create it properly if (indices.length == 0) { return false; @@ -242,18 +249,23 @@ static boolean verifyIndicesExistAndPrimaryShardsAreActive(ClusterState clusterS private void createStatsIndexIfNecessary() { final PlainActionFuture listener = new PlainActionFuture<>(); - MlStatsIndex.createStatsIndexAndAliasIfNecessary(client, clusterState, indexNameExpressionResolver, + MlStatsIndex.createStatsIndexAndAliasIfNecessary( + client, + clusterState, + indexNameExpressionResolver, MasterNodeRequest.DEFAULT_MASTER_NODE_TIMEOUT, ActionListener.wrap( - r -> ElasticsearchMappings.addDocMappingIfMissing( - MlStatsIndex.writeAlias(), - MlStatsIndex::wrappedMapping, - client, - clusterState, - MasterNodeRequest.DEFAULT_MASTER_NODE_TIMEOUT, - listener), - listener::onFailure - )); + r -> ElasticsearchMappings.addDocMappingIfMissing( + MlStatsIndex.writeAlias(), + MlStatsIndex::wrappedMapping, + client, + clusterState, + MasterNodeRequest.DEFAULT_MASTER_NODE_TIMEOUT, + listener + ), + listener::onFailure + ) + ); listener.actionGet(); logger.debug("Created stats index"); } @@ -279,10 +291,9 @@ static UpdateRequest buildUpdateRequest(InferenceStats stats) { return updateRequest; } catch (IOException ex) { logger.error( - () -> new ParameterizedMessage("[{}] [{}] failed to serialize stats for update.", - stats.getModelId(), - stats.getNodeId()), - ex); + () -> new ParameterizedMessage("[{}] [{}] failed to serialize stats for update.", stats.getModelId(), stats.getNodeId()), + ex + ); } return null; } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/allocation/TrainedModelAllocationClusterService.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/allocation/TrainedModelAllocationClusterService.java index 56b5b4c45d5da..07c424124ea56 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/allocation/TrainedModelAllocationClusterService.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/allocation/TrainedModelAllocationClusterService.java @@ -359,8 +359,10 @@ ClusterState addRemoveAllocationNodes(ClusterState currentState) { .getAllNodes() .stream() // TODO: Change when we update `mayAllocateToNode` - .filter(node -> shuttingDownNodes.contains(node.getId()) == false - && StartTrainedModelDeploymentAction.TaskParams.mayAllocateToNode(node)) + .filter( + node -> shuttingDownNodes.contains(node.getId()) == false + && StartTrainedModelDeploymentAction.TaskParams.mayAllocateToNode(node) + ) .collect(Collectors.toMap(DiscoveryNode::getId, Function.identity())); // TODO: make more efficient, we iterate every entry, sorting by nodes routed (fewest to most) previousState.modelAllocations() @@ -374,9 +376,9 @@ ClusterState addRemoveAllocationNodes(ClusterState currentState) { for (DiscoveryNode node : currentEligibleNodes.values()) { if (modelAllocationEntry.getValue().isRoutedToNode(node.getId()) == false) { Optional failure = builder.isChanged() ? - // We use the builder only if we have changed, there is no point in creating a new object if we haven't changed - nodeHasCapacity(currentState, builder, modelAllocationEntry.getValue().getTaskParams(), node) : - nodeHasCapacity(currentState, modelAllocationEntry.getValue().getTaskParams(), node); + // We use the builder only if we have changed, there is no point in creating a new object if we haven't changed + nodeHasCapacity(currentState, builder, modelAllocationEntry.getValue().getTaskParams(), node) + : nodeHasCapacity(currentState, modelAllocationEntry.getValue().getTaskParams(), node); if (failure.isPresent()) { nodeToReason.put(node.getName(), failure.get()); } else { @@ -457,14 +459,7 @@ Optional nodeHasCapacity( StartTrainedModelDeploymentAction.TaskParams params, DiscoveryNode node ) { - NodeLoad load = nodeLoadDetector.detectNodeLoad( - state, - builder.build(), - node, - maxOpenJobs, - maxMemoryPercentage, - useAuto - ); + NodeLoad load = nodeLoadDetector.detectNodeLoad(state, builder.build(), node, maxOpenJobs, maxMemoryPercentage, useAuto); return handleNodeLoad(load, node.getId(), params); } @@ -477,11 +472,7 @@ Optional handleNodeLoad(NodeLoad load, String nodeId, StartTrainedModelD return Optional.of( ParameterizedMessage.format( "This node is full. Number of opened jobs and allocated native inference processes [{}], {} [{}].", - new Object[] { - load.getNumAssignedJobs(), - MachineLearning.MAX_OPEN_JOBS_PER_NODE.getKey(), - maxOpenJobs - } + new Object[] { load.getNumAssignedJobs(), MachineLearning.MAX_OPEN_JOBS_PER_NODE.getKey(), maxOpenJobs } ) ); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/allocation/TrainedModelAllocationMetadata.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/allocation/TrainedModelAllocationMetadata.java index ab38c24dafe97..a93d3ae53da59 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/allocation/TrainedModelAllocationMetadata.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/allocation/TrainedModelAllocationMetadata.java @@ -127,7 +127,7 @@ public int hashCode() { public static class Builder { - public static Builder empty(){ + public static Builder empty() { return new Builder(); } @@ -155,10 +155,8 @@ public boolean hasModel(String modelId) { public Builder addNewAllocation(String modelId, TrainedModelAllocation.Builder allocation) { if (modelRoutingEntries.containsKey(modelId)) { - throw new ResourceAlreadyExistsException( - "[{}] allocation already exists", - modelId - ); } + throw new ResourceAlreadyExistsException("[{}] allocation already exists", modelId); + } modelRoutingEntries.put(modelId, allocation); isChanged = true; return this; diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/allocation/TrainedModelAllocationNodeService.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/allocation/TrainedModelAllocationNodeService.java index 1c07ed6d1bea3..ffd84f273808d 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/allocation/TrainedModelAllocationNodeService.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/allocation/TrainedModelAllocationNodeService.java @@ -240,16 +240,18 @@ public void stopDeploymentAndNotify(TrainedModelDeploymentTask task, String reas e ); } - stopDeploymentAsync(task, reason, notifyDeploymentOfStopped); + stopDeploymentAsync(task, reason, notifyDeploymentOfStopped); }) ); } - public void infer(TrainedModelDeploymentTask task, - InferenceConfig config, - Map doc, - TimeValue timeout, - ActionListener listener) { + public void infer( + TrainedModelDeploymentTask task, + InferenceConfig config, + Map doc, + TimeValue timeout, + ActionListener listener + ) { deploymentManager.infer(task, config, doc, timeout, listener); } @@ -301,7 +303,7 @@ public void clusterChanged(ClusterChangedEvent event) { && routingStateAndReason.getState().isAnyOf(RoutingState.STARTING, RoutingState.STARTED) // This means we don't already have a task and should attempt creating one and starting the model loading // If we don't have a task but are STARTED, this means the cluster state had a started allocation, - // the node crashed and then started again + // the node crashed and then started again && modelIdToTask.containsKey(trainedModelAllocation.getTaskParams().getModelId()) == false // If we are in reset mode, don't start loading a new model on this node. && isResetMode == false) { @@ -349,8 +351,9 @@ TrainedModelDeploymentTask getTask(String modelId) { } void prepareModelToLoad(StartTrainedModelDeploymentAction.TaskParams taskParams) { - logger.debug(() -> new ParameterizedMessage("[{}] preparing to load model with task params: {}", - taskParams.getModelId(), taskParams)); + logger.debug( + () -> new ParameterizedMessage("[{}] preparing to load model with task params: {}", taskParams.getModelId(), taskParams) + ); TrainedModelDeploymentTask task = (TrainedModelDeploymentTask) taskManager.register( TRAINED_MODEL_ALLOCATION_TASK_TYPE, TRAINED_MODEL_ALLOCATION_TASK_NAME_PREFIX + taskParams.getModelId(), @@ -383,23 +386,20 @@ private void handleLoadSuccess(TrainedModelDeploymentTask task) { updateStoredState( modelId, new RoutingStateAndReason(RoutingState.STARTED, ""), - ActionListener.wrap( - r -> logger.debug(() -> new ParameterizedMessage("[{}] model loaded and accepting routes", modelId)), - e -> { - // This means that either the allocation has been deleted, or this node's particular route has been removed - if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) { - logger.debug( - () -> new ParameterizedMessage( - "[{}] model loaded but failed to start accepting routes as allocation to this node was removed", - modelId - ), - e - ); - } - // this is an unexpected error - logger.warn(() -> new ParameterizedMessage("[{}] model loaded but failed to start accepting routes", modelId), e); + ActionListener.wrap(r -> logger.debug(() -> new ParameterizedMessage("[{}] model loaded and accepting routes", modelId)), e -> { + // This means that either the allocation has been deleted, or this node's particular route has been removed + if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) { + logger.debug( + () -> new ParameterizedMessage( + "[{}] model loaded but failed to start accepting routes as allocation to this node was removed", + modelId + ), + e + ); } - ) + // this is an unexpected error + logger.warn(() -> new ParameterizedMessage("[{}] model loaded but failed to start accepting routes", modelId), e); + }) ); } @@ -418,35 +418,38 @@ private void updateStoredState( () -> new ParameterizedMessage("[{}] model is [{}] and master notified", modelId, routingStateAndReason.getState()) ); listener.onResponse(AcknowledgedResponse.TRUE); - }, - error -> { - logger.warn( - () -> new ParameterizedMessage( - "[{}] model is [{}] but failed to notify master", - modelId, - routingStateAndReason.getState() - ), - error - ); - listener.onFailure(error); - } - ) + }, error -> { + logger.warn( + () -> new ParameterizedMessage( + "[{}] model is [{}] but failed to notify master", + modelId, + routingStateAndReason.getState() + ), + error + ); + listener.onFailure(error); + }) ); } private void handleLoadFailure(TrainedModelDeploymentTask task, Exception ex) { logger.error(() -> new ParameterizedMessage("[{}] model failed to load", task.getModelId()), ex); if (task.isStopped()) { - logger.debug(() -> new ParameterizedMessage( - "[{}] model failed to load, but is now stopped; reason [{}]", - task.getModelId(), - task.stoppedReason().orElse("_unknown_") - )); + logger.debug( + () -> new ParameterizedMessage( + "[{}] model failed to load, but is now stopped; reason [{}]", + task.getModelId(), + task.stoppedReason().orElse("_unknown_") + ) + ); } // TODO: Do we want to stop the task? This would cause it to be reloaded by state updates on INITIALIZING // We should stop the local task so that future task actions won't get routed to the older one. - Runnable stopTask = () -> stopDeploymentAsync(task, "model failed to load; reason [" + ex.getMessage() + "]", - ActionListener.wrap(r -> {}, e -> {})); + Runnable stopTask = () -> stopDeploymentAsync( + task, + "model failed to load; reason [" + ex.getMessage() + "]", + ActionListener.wrap(r -> {}, e -> {}) + ); updateStoredState( task.getModelId(), new RoutingStateAndReason(RoutingState.FAILED, ExceptionsHelper.unwrapCause(ex).getMessage()), @@ -458,11 +461,24 @@ public void failAllocation(TrainedModelDeploymentTask task, String reason) { updateStoredState( task.getModelId(), new RoutingStateAndReason(RoutingState.FAILED, reason), - ActionListener.wrap(r -> logger.debug( - new ParameterizedMessage("[{}] Successfully updating allocation state to [{}] with reason [{}]", - task.getModelId(), RoutingState.FAILED, reason)) - , e -> logger.error(new ParameterizedMessage("[{}] Error while updating allocation state to [{}] with reason [{}]", - task.getModelId(), RoutingState.FAILED, reason), e) + ActionListener.wrap( + r -> logger.debug( + new ParameterizedMessage( + "[{}] Successfully updating allocation state to [{}] with reason [{}]", + task.getModelId(), + RoutingState.FAILED, + reason + ) + ), + e -> logger.error( + new ParameterizedMessage( + "[{}] Error while updating allocation state to [{}] with reason [{}]", + task.getModelId(), + RoutingState.FAILED, + reason + ), + e + ) ) ); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/allocation/TrainedModelAllocationService.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/allocation/TrainedModelAllocationService.java index 1ebc9dd9b6f07..e9216f7f2a4ef 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/allocation/TrainedModelAllocationService.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/allocation/TrainedModelAllocationService.java @@ -27,9 +27,9 @@ import org.elasticsearch.node.NodeClosedException; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.ConnectTransportException; -import org.elasticsearch.xpack.core.ml.action.StartTrainedModelDeploymentAction; import org.elasticsearch.xpack.core.ml.action.CreateTrainedModelAllocationAction; import org.elasticsearch.xpack.core.ml.action.DeleteTrainedModelAllocationAction; +import org.elasticsearch.xpack.core.ml.action.StartTrainedModelDeploymentAction; import org.elasticsearch.xpack.core.ml.action.UpdateTrainedModelAllocationStateAction; import org.elasticsearch.xpack.core.ml.inference.allocation.TrainedModelAllocation; diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/deployment/DeploymentManager.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/deployment/DeploymentManager.java index c19c0b8776179..dae5b83f4332e 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/deployment/DeploymentManager.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/deployment/DeploymentManager.java @@ -80,8 +80,12 @@ public class DeploymentManager { private final ThreadPool threadPool; private final ConcurrentMap processContextByAllocation = new ConcurrentHashMap<>(); - public DeploymentManager(Client client, NamedXContentRegistry xContentRegistry, - ThreadPool threadPool, PyTorchProcessFactory pyTorchProcessFactory) { + public DeploymentManager( + Client client, + NamedXContentRegistry xContentRegistry, + ThreadPool threadPool, + PyTorchProcessFactory pyTorchProcessFactory + ) { this.client = Objects.requireNonNull(client); this.xContentRegistry = Objects.requireNonNull(xContentRegistry); this.pyTorchProcessFactory = Objects.requireNonNull(pyTorchProcessFactory); @@ -96,9 +100,11 @@ public void startDeployment(TrainedModelDeploymentTask task, ActionListener getStats(TrainedModelDeploymentTask task) { return Optional.ofNullable(processContextByAllocation.get(task.getId())) - .map(processContext -> - new ModelStats(processContext.getResultProcessor().getTimingStats(), - processContext.getResultProcessor().getLastUsed()) + .map( + processContext -> new ModelStats( + processContext.getResultProcessor().getTimingStats(), + processContext.getResultProcessor().getLastUsed() + ) ); } @@ -112,59 +118,58 @@ private void doStartDeployment(TrainedModelDeploymentTask task, ActionListener listener = ActionListener.wrap( - finalListener::onResponse, - failure -> { - processContextByAllocation.remove(task.getId()); - finalListener.onFailure(failure); - } - ); + ActionListener listener = ActionListener.wrap(finalListener::onResponse, failure -> { + processContextByAllocation.remove(task.getId()); + finalListener.onFailure(failure); + }); - ActionListener modelLoadedListener = ActionListener.wrap( - success -> { - executorServiceForProcess.execute(() -> processContext.getResultProcessor().process(processContext.process.get())); - listener.onResponse(task); - }, - listener::onFailure - ); + ActionListener modelLoadedListener = ActionListener.wrap(success -> { + executorServiceForProcess.execute(() -> processContext.getResultProcessor().process(processContext.process.get())); + listener.onResponse(task); + }, listener::onFailure); - ActionListener getModelListener = ActionListener.wrap( - getModelResponse -> { - assert getModelResponse.getResources().results().size() == 1; - TrainedModelConfig modelConfig = getModelResponse.getResources().results().get(0); - processContext.modelInput.set(modelConfig.getInput()); - - assert modelConfig.getInferenceConfig() instanceof NlpConfig; - NlpConfig nlpConfig = (NlpConfig) modelConfig.getInferenceConfig(); - task.init(nlpConfig); - - SearchRequest searchRequest = vocabSearchRequest(nlpConfig.getVocabularyConfig(), modelConfig.getModelId()); - executeAsyncWithOrigin(client, ML_ORIGIN, SearchAction.INSTANCE, searchRequest, ActionListener.wrap( - searchVocabResponse -> { - if (searchVocabResponse.getHits().getHits().length == 0) { - listener.onFailure(new ResourceNotFoundException(Messages.getMessage( - Messages.VOCABULARY_NOT_FOUND, task.getModelId(), VocabularyConfig.docId(modelConfig.getModelId())))); - return; - } - - Vocabulary vocabulary = parseVocabularyDocLeniently(searchVocabResponse.getHits().getAt(0)); - NlpTask nlpTask = new NlpTask(nlpConfig, vocabulary); - NlpTask.Processor processor = nlpTask.createProcessor(); - processContext.nlpTaskProcessor.set(processor); - // here, we are being called back on the searching thread, which MAY be a network thread - // `startAndLoad` creates named pipes, blocking the calling thread, better to execute that in our utility - // executor. - executorServiceForDeployment.execute( - () -> startAndLoad(processContext, modelConfig.getLocation(), modelLoadedListener)); - }, - listener::onFailure - )); - }, - listener::onFailure - ); + ActionListener getModelListener = ActionListener.wrap(getModelResponse -> { + assert getModelResponse.getResources().results().size() == 1; + TrainedModelConfig modelConfig = getModelResponse.getResources().results().get(0); + processContext.modelInput.set(modelConfig.getInput()); + + assert modelConfig.getInferenceConfig() instanceof NlpConfig; + NlpConfig nlpConfig = (NlpConfig) modelConfig.getInferenceConfig(); + task.init(nlpConfig); + + SearchRequest searchRequest = vocabSearchRequest(nlpConfig.getVocabularyConfig(), modelConfig.getModelId()); + executeAsyncWithOrigin(client, ML_ORIGIN, SearchAction.INSTANCE, searchRequest, ActionListener.wrap(searchVocabResponse -> { + if (searchVocabResponse.getHits().getHits().length == 0) { + listener.onFailure( + new ResourceNotFoundException( + Messages.getMessage( + Messages.VOCABULARY_NOT_FOUND, + task.getModelId(), + VocabularyConfig.docId(modelConfig.getModelId()) + ) + ) + ); + return; + } - executeAsyncWithOrigin(client, ML_ORIGIN, GetTrainedModelsAction.INSTANCE, new GetTrainedModelsAction.Request(task.getModelId()), - getModelListener); + Vocabulary vocabulary = parseVocabularyDocLeniently(searchVocabResponse.getHits().getAt(0)); + NlpTask nlpTask = new NlpTask(nlpConfig, vocabulary); + NlpTask.Processor processor = nlpTask.createProcessor(); + processContext.nlpTaskProcessor.set(processor); + // here, we are being called back on the searching thread, which MAY be a network thread + // `startAndLoad` creates named pipes, blocking the calling thread, better to execute that in our utility + // executor. + executorServiceForDeployment.execute(() -> startAndLoad(processContext, modelConfig.getLocation(), modelLoadedListener)); + }, listener::onFailure)); + }, listener::onFailure); + + executeAsyncWithOrigin( + client, + ML_ORIGIN, + GetTrainedModelsAction.INSTANCE, + new GetTrainedModelsAction.Request(task.getModelId()), + getModelListener + ); } private SearchRequest vocabSearchRequest(VocabularyConfig vocabularyConfig, String modelId) { @@ -176,9 +181,11 @@ private SearchRequest vocabSearchRequest(VocabularyConfig vocabularyConfig, Stri } Vocabulary parseVocabularyDocLeniently(SearchHit hit) throws IOException { - try (InputStream stream = hit.getSourceRef().streamInput(); - XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, stream)) { + try ( + InputStream stream = hit.getSourceRef().streamInput(); + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, stream) + ) { return Vocabulary.createParser(true).apply(parser, null); } catch (IOException e) { logger.error(new ParameterizedMessage("failed to parse vocabulary [{}]", hit.getId()), e); @@ -208,18 +215,17 @@ public void stopDeployment(TrainedModelDeploymentTask task) { } } - public void infer(TrainedModelDeploymentTask task, - InferenceConfig config, - Map doc, - TimeValue timeout, - ActionListener listener) { + public void infer( + TrainedModelDeploymentTask task, + InferenceConfig config, + Map doc, + TimeValue timeout, + ActionListener listener + ) { if (task.isStopped()) { listener.onFailure( - new IllegalStateException("[" - + task.getModelId() - + "] is stopping or stopped due to [" - + task.stoppedReason().orElse("") - + "]" + new IllegalStateException( + "[" + task.getModelId() + "] is stopping or stopped due to [" + task.stoppedReason().orElse("") + "]" ) ); return; @@ -299,10 +305,7 @@ public void onFailure(Exception e) { listener.onFailure(e); return; } - logger.debug( - () -> new ParameterizedMessage("request [{}] received failure but listener already notified", requestId), - e - ); + logger.debug(() -> new ParameterizedMessage("request [{}] received failure but listener already notified", requestId), e); } @Override @@ -317,7 +320,7 @@ protected void doRun() throws Exception { processor.validateInputs(text); assert config instanceof NlpConfig; NlpTask.Request request = processor.getRequestBuilder((NlpConfig) config).buildRequest(text, requestIdStr); - logger.trace(() -> "Inference Request "+ request.processInput.utf8ToString()); + logger.trace(() -> "Inference Request " + request.processInput.utf8ToString()); PyTorchResultProcessor.PendingResult pendingResult = processContext.getResultProcessor().registerRequest(requestIdStr); processContext.process.get().writeInferenceRequest(request.processInput); waitForResult( @@ -327,7 +330,7 @@ protected void doRun() throws Exception { requestIdStr, timeout, processor.getResultProcessor((NlpConfig) config), - ActionListener.wrap(this::onSuccess,this::onFailure) + ActionListener.wrap(this::onSuccess, this::onFailure) ); } catch (IOException e) { logger.error(new ParameterizedMessage("[{}] error writing to process", processContext.task.getModelId()), e); @@ -339,20 +342,18 @@ protected void doRun() throws Exception { } } - private void waitForResult(ProcessContext processContext, - PyTorchResultProcessor.PendingResult pendingResult, - TokenizationResult tokenization, - String requestId, - TimeValue timeout, - NlpTask.ResultProcessor inferenceResultsProcessor, - ActionListener listener) { + private void waitForResult( + ProcessContext processContext, + PyTorchResultProcessor.PendingResult pendingResult, + TokenizationResult tokenization, + String requestId, + TimeValue timeout, + NlpTask.ResultProcessor inferenceResultsProcessor, + ActionListener listener + ) { try { - PyTorchResult pyTorchResult = processContext.getResultProcessor().waitForResult( - processContext.process.get(), - requestId, - pendingResult, - timeout - ); + PyTorchResult pyTorchResult = processContext.getResultProcessor() + .waitForResult(processContext.process.get(), requestId, pendingResult, timeout); if (pyTorchResult == null) { listener.onFailure( new ElasticsearchStatusException("timeout [{}] waiting for inference result", RestStatus.TOO_MANY_REQUESTS, timeout) @@ -361,16 +362,17 @@ private void waitForResult(ProcessContext processContext, } if (pyTorchResult.isError()) { - listener.onFailure(new ElasticsearchStatusException(pyTorchResult.getError(), - RestStatus.INTERNAL_SERVER_ERROR)); + listener.onFailure(new ElasticsearchStatusException(pyTorchResult.getError(), RestStatus.INTERNAL_SERVER_ERROR)); return; } - logger.debug(() -> new ParameterizedMessage( - "[{}] retrieved result for request [{}]", processContext.task.getModelId(), requestId)); + logger.debug( + () -> new ParameterizedMessage("[{}] retrieved result for request [{}]", processContext.task.getModelId(), requestId) + ); InferenceResults results = inferenceResultsProcessor.processResult(tokenization, pyTorchResult); - logger.debug(() -> new ParameterizedMessage( - "[{}] processed result for request [{}]", processContext.task.getModelId(), requestId)); + logger.debug( + () -> new ParameterizedMessage("[{}] processed result for request [{}]", processContext.task.getModelId(), requestId) + ); listener.onResponse(results); } catch (InterruptedException e) { listener.onFailure(e); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/deployment/PyTorchResult.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/deployment/PyTorchResult.java index 50c9855a7f038..e209f9d1f5bf0 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/deployment/PyTorchResult.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/deployment/PyTorchResult.java @@ -10,13 +10,13 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.core.Nullable; import org.elasticsearch.xpack.core.ml.utils.MlParserUtils; import java.io.IOException; @@ -35,14 +35,16 @@ public class PyTorchResult implements ToXContentObject, Writeable { private static final ParseField ERROR = new ParseField("error"); private static final ParseField TIME_MS = new ParseField("time_ms"); - public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("pytorch_result", - a -> new PyTorchResult((String) a[0], (double[][][]) a[1], (Long) a[2], (String) a[3])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "pytorch_result", + a -> new PyTorchResult((String) a[0], (double[][][]) a[1], (Long) a[2], (String) a[3]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), REQUEST_ID); - PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(), - (p, c) -> - MlParserUtils.parse3DArrayOfDoubles(INFERENCE.getPreferredName(), p), + PARSER.declareField( + ConstructingObjectParser.optionalConstructorArg(), + (p, c) -> MlParserUtils.parse3DArrayOfDoubles(INFERENCE.getPreferredName(), p), INFERENCE, ObjectParser.ValueType.VALUE_ARRAY ); @@ -59,10 +61,7 @@ public static PyTorchResult fromXContent(XContentParser parser) throws IOExcepti private final Long timeMs; private final String error; - public PyTorchResult(String requestId, - @Nullable double[][][] inference, - @Nullable Long timeMs, - @Nullable String error) { + public PyTorchResult(String requestId, @Nullable double[][][] inference, @Nullable Long timeMs, @Nullable String error) { this.requestId = Objects.requireNonNull(requestId); this.inference = inference; this.timeMs = timeMs; @@ -109,8 +108,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.startArray(INFERENCE.getPreferredName()); for (int i = 0; i < inference.length; i++) { builder.startArray(); - for (int j = 0; j < inference[0].length; j++) - { + for (int j = 0; j < inference[0].length; j++) { builder.startArray(); for (int k = 0; k < inference[0][0].length; k++) { builder.value(inference[i][j][k]); @@ -138,9 +136,7 @@ public void writeTo(StreamOutput out) throws IOException { out.writeBoolean(false); } else { out.writeBoolean(true); - out.writeArray( - (out2, arr) -> out2.writeArray(StreamOutput::writeDoubleArray, arr), - inference); + out.writeArray((out2, arr) -> out2.writeArray(StreamOutput::writeDoubleArray, arr), inference); } out.writeOptionalLong(timeMs); out.writeOptionalString(error); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/deployment/TrainedModelDeploymentTask.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/deployment/TrainedModelDeploymentTask.java index a65e85ab05da0..bbe78b7445029 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/deployment/TrainedModelDeploymentTask.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/deployment/TrainedModelDeploymentTask.java @@ -28,7 +28,6 @@ import java.util.Map; import java.util.Optional; - public class TrainedModelDeploymentTask extends CancellableTask implements StartTrainedModelDeploymentAction.TaskMatcher { private static final Logger logger = LogManager.getLogger(TrainedModelDeploymentTask.class); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/ingest/InferenceProcessor.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/ingest/InferenceProcessor.java index 4cf05c48221ab..8271cf88c2509 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/ingest/InferenceProcessor.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/ingest/InferenceProcessor.java @@ -73,11 +73,13 @@ public class InferenceProcessor extends AbstractProcessor { // How many total inference processors are allowed to be used in the cluster. - public static final Setting MAX_INFERENCE_PROCESSORS = Setting.intSetting("xpack.ml.max_inference_processors", + public static final Setting MAX_INFERENCE_PROCESSORS = Setting.intSetting( + "xpack.ml.max_inference_processors", 50, 1, Setting.Property.Dynamic, - Setting.Property.NodeScope); + Setting.Property.NodeScope + ); public static final String TYPE = "inference"; public static final String INFERENCE_CONFIG = "inference_config"; @@ -96,14 +98,16 @@ public class InferenceProcessor extends AbstractProcessor { private volatile boolean previouslyLicensed; private final AtomicBoolean shouldAudit = new AtomicBoolean(true); - public InferenceProcessor(Client client, - InferenceAuditor auditor, - String tag, - String description, - String targetField, - String modelId, - InferenceConfigUpdate inferenceConfig, - Map fieldMap) { + public InferenceProcessor( + Client client, + InferenceAuditor auditor, + String tag, + String description, + String targetField, + String modelId, + InferenceConfigUpdate inferenceConfig, + Map fieldMap + ) { super(tag, description); this.client = ExceptionsHelper.requireNonNull(client, "client"); this.targetField = ExceptionsHelper.requireNonNull(targetField, TARGET_FIELD); @@ -119,19 +123,20 @@ public String getModelId() { @Override public void execute(IngestDocument ingestDocument, BiConsumer handler) { - executeAsyncWithOrigin(client, + executeAsyncWithOrigin( + client, ML_ORIGIN, InternalInferModelAction.INSTANCE, this.buildRequest(ingestDocument), - ActionListener.wrap( - r -> handleResponse(r, ingestDocument, handler), - e -> handler.accept(ingestDocument, e) - )); + ActionListener.wrap(r -> handleResponse(r, ingestDocument, handler), e -> handler.accept(ingestDocument, e)) + ); } - void handleResponse(InternalInferModelAction.Response response, - IngestDocument ingestDocument, - BiConsumer handler) { + void handleResponse( + InternalInferModelAction.Response response, + IngestDocument ingestDocument, + BiConsumer handler + ) { if (previouslyLicensed == false) { previouslyLicensed = true; } @@ -141,7 +146,7 @@ void handleResponse(InternalInferModelAction.Response response, try { mutateDocument(response, ingestDocument); handler.accept(ingestDocument, null); - } catch(ElasticsearchException ex) { + } catch (ElasticsearchException ex) { handler.accept(ingestDocument, ex); } } @@ -160,8 +165,9 @@ void auditWarningAboutLicenseIfNecessary() { if (shouldAudit.compareAndSet(true, false)) { auditor.warning( modelId, - "This cluster is no longer licensed to use this model in the inference ingest processor. " + - "Please update your license information."); + "This cluster is no longer licensed to use this model in the inference ingest processor. " + + "Please update your license information." + ); } } @@ -191,7 +197,7 @@ public String getType() { public static final class Factory implements Processor.Factory, Consumer { private static final String FOREACH_PROCESSOR_NAME = "foreach"; - //Any more than 10 nestings of processors, we stop searching for inference processor definitions + // Any more than 10 nestings of processors, we stop searching for inference processor definitions private static final int MAX_INFERENCE_PROCESSOR_SEARCH_RECURSIONS = 10; private static final Logger logger = LogManager.getLogger(Factory.class); @@ -238,7 +244,8 @@ public static int countNumberInferenceProcessors(ClusterState state) { } catch (Exception ex) { logger.debug( () -> new ParameterizedMessage("failed gathering processors for pipeline [{}]", configuration.getId()), - ex); + ex + ); } } return count; @@ -246,7 +253,7 @@ public static int countNumberInferenceProcessors(ClusterState state) { @SuppressWarnings("unchecked") static int numInferenceProcessors(String processorType, Object processorDefinition) { - return numInferenceProcessors(processorType, (Map)processorDefinition, 0); + return numInferenceProcessors(processorType, (Map) processorDefinition, 0); } @SuppressWarnings("unchecked") @@ -263,13 +270,15 @@ static int numInferenceProcessors(String processorType, Map proc count++; } if (FOREACH_PROCESSOR_NAME.equals(processorType)) { - Map innerProcessor = (Map)processorDefinition.get("processor"); + Map innerProcessor = (Map) processorDefinition.get("processor"); if (innerProcessor != null) { // a foreach processor should only have a SINGLE nested processor. Iteration is for simplicity's sake. for (Map.Entry innerProcessorWithName : innerProcessor.entrySet()) { - count += numInferenceProcessors(innerProcessorWithName.getKey(), + count += numInferenceProcessors( + innerProcessorWithName.getKey(), (Map) innerProcessorWithName.getValue(), - level + 1); + level + 1 + ); } } } @@ -278,10 +287,12 @@ static int numInferenceProcessors(String processorType, Map proc null, null, processorDefinition, - Pipeline.ON_FAILURE_KEY); + Pipeline.ON_FAILURE_KEY + ); count += onFailureConfigs.stream() .flatMap(map -> map.entrySet().stream()) - .mapToInt(entry -> numInferenceProcessors(entry.getKey(), (Map)entry.getValue(), level + 1)).sum(); + .mapToInt(entry -> numInferenceProcessors(entry.getKey(), (Map) entry.getValue(), level + 1)) + .sum(); } return count; } @@ -292,16 +303,22 @@ int numInferenceProcessors() { } @Override - public InferenceProcessor create(Map processorFactories, String tag, String description, - Map config) { + public InferenceProcessor create( + Map processorFactories, + String tag, + String description, + Map config + ) { if (this.maxIngestProcessors <= currentInferenceProcessors) { - throw new ElasticsearchStatusException("Max number of inference processors reached, total inference processors [{}]. " + - "Adjust the setting [{}]: [{}] if a greater number is desired.", + throw new ElasticsearchStatusException( + "Max number of inference processors reached, total inference processors [{}]. " + + "Adjust the setting [{}]: [{}] if a greater number is desired.", RestStatus.CONFLICT, currentInferenceProcessors, MAX_INFERENCE_PROCESSORS.getKey(), - maxIngestProcessors); + maxIngestProcessors + ); } String modelId = ConfigurationUtils.readStringProperty(TYPE, tag, config, MODEL_ID_RESULTS_FIELD); @@ -312,7 +329,7 @@ public InferenceProcessor create(Map processorFactori Map fieldMap = ConfigurationUtils.readOptionalMap(TYPE, tag, config, FIELD_MAP); if (fieldMap == null) { fieldMap = ConfigurationUtils.readOptionalMap(TYPE, tag, config, FIELD_MAPPINGS); - //TODO Remove in 8.x + // TODO Remove in 8.x if (fieldMap != null) { LoggingDeprecationHandler.INSTANCE.logRenamedField(null, () -> null, FIELD_MAPPINGS, FIELD_MAP); } @@ -334,14 +351,7 @@ public InferenceProcessor create(Map processorFactori inferenceConfigUpdate = inferenceConfigUpdateFromMap(inferenceConfigMap); } - return new InferenceProcessor(client, - auditor, - tag, - description, - targetField, - modelId, - inferenceConfigUpdate, - fieldMap); + return new InferenceProcessor(client, auditor, tag, description, targetField, modelId, inferenceConfigUpdate, fieldMap); } // Package private for testing @@ -353,17 +363,21 @@ void setMaxIngestProcessors(int maxIngestProcessors) { InferenceConfigUpdate inferenceConfigUpdateFromMap(Map configMap) { ExceptionsHelper.requireNonNull(configMap, INFERENCE_CONFIG); if (configMap.size() != 1) { - throw ExceptionsHelper.badRequestException("{} must be an object with one inference type mapped to an object.", - INFERENCE_CONFIG); + throw ExceptionsHelper.badRequestException( + "{} must be an object with one inference type mapped to an object.", + INFERENCE_CONFIG + ); } Object value = configMap.values().iterator().next(); if ((value instanceof Map) == false) { - throw ExceptionsHelper.badRequestException("{} must be an object with one inference type mapped to an object.", - INFERENCE_CONFIG); + throw ExceptionsHelper.badRequestException( + "{} must be an object with one inference type mapped to an object.", + INFERENCE_CONFIG + ); } @SuppressWarnings("unchecked") - Map valueMap = (Map)value; + Map valueMap = (Map) value; if (configMap.containsKey(ClassificationConfig.NAME.getPreferredName())) { checkSupportedVersion(ClassificationConfig.EMPTY_PARAMS); @@ -392,27 +406,37 @@ InferenceConfigUpdate inferenceConfigUpdateFromMap(Map configMap } // TODO missing update types else { - throw ExceptionsHelper.badRequestException("unrecognized inference configuration type {}. Supported types {}", + throw ExceptionsHelper.badRequestException( + "unrecognized inference configuration type {}. Supported types {}", configMap.keySet(), - Arrays.asList(ClassificationConfig.NAME.getPreferredName(), RegressionConfig.NAME.getPreferredName())); + Arrays.asList(ClassificationConfig.NAME.getPreferredName(), RegressionConfig.NAME.getPreferredName()) + ); } } void checkNlpSupported(String taskType) { if (NlpConfig.MINIMUM_NLP_SUPPORTED_VERSION.after(minNodeVersion)) { - throw ExceptionsHelper.badRequestException(Messages.getMessage(Messages.INFERENCE_CONFIG_NOT_SUPPORTED_ON_VERSION, - taskType, - NlpConfig.MINIMUM_NLP_SUPPORTED_VERSION, - minNodeVersion)); + throw ExceptionsHelper.badRequestException( + Messages.getMessage( + Messages.INFERENCE_CONFIG_NOT_SUPPORTED_ON_VERSION, + taskType, + NlpConfig.MINIMUM_NLP_SUPPORTED_VERSION, + minNodeVersion + ) + ); } } void checkSupportedVersion(InferenceConfig config) { if (config.getMinimalSupportedVersion().after(minNodeVersion)) { - throw ExceptionsHelper.badRequestException(Messages.getMessage(Messages.INFERENCE_CONFIG_NOT_SUPPORTED_ON_VERSION, - config.getName(), - config.getMinimalSupportedVersion(), - minNodeVersion)); + throw ExceptionsHelper.badRequestException( + Messages.getMessage( + Messages.INFERENCE_CONFIG_NOT_SUPPORTED_ON_VERSION, + config.getName(), + config.getMinimalSupportedVersion(), + minNodeVersion + ) + ); } } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/loadingservice/LocalModel.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/loadingservice/LocalModel.java index 423667f61af2c..4cc5cfadcd49c 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/loadingservice/LocalModel.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/loadingservice/LocalModel.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.ml.inference.loadingservice; import org.elasticsearch.action.ActionListener; -import org.elasticsearch.license.License; import org.elasticsearch.common.breaker.CircuitBreaker; +import org.elasticsearch.license.License; import org.elasticsearch.xpack.core.ml.inference.TrainedModelInput; import org.elasticsearch.xpack.core.ml.inference.results.InferenceResults; import org.elasticsearch.xpack.core.ml.inference.results.WarningInferenceResults; @@ -59,15 +59,17 @@ public class LocalModel implements Closeable { private final AtomicLong referenceCount; private final long cachedRamBytesUsed; - LocalModel(String modelId, - String nodeId, - InferenceDefinition trainedModelDefinition, - TrainedModelInput input, - Map defaultFieldMap, - InferenceConfig modelInferenceConfig, - License.OperationMode licenseLevel, - TrainedModelStatsService trainedModelStatsService, - CircuitBreaker trainedModelCircuitBreaker) { + LocalModel( + String modelId, + String nodeId, + InferenceDefinition trainedModelDefinition, + TrainedModelInput input, + Map defaultFieldMap, + InferenceConfig modelInferenceConfig, + License.OperationMode licenseLevel, + TrainedModelStatsService trainedModelStatsService, + CircuitBreaker trainedModelCircuitBreaker + ) { this.trainedModelDefinition = trainedModelDefinition; this.cachedRamBytesUsed = trainedModelDefinition.ramBytesUsed(); this.modelId = modelId; @@ -129,11 +131,14 @@ public InferenceResults inferNoStats(Map fields) { public void infer(Map fields, InferenceConfigUpdate update, ActionListener listener) { if (update.isSupported(this.inferenceConfig) == false) { - listener.onFailure(ExceptionsHelper.badRequestException( - "Model [{}] has inference config of type [{}] which is not supported by inference request of type [{}]", - this.modelId, - this.inferenceConfig.getName(), - update.getName())); + listener.onFailure( + ExceptionsHelper.badRequestException( + "Model [{}] has inference config of type [{}] which is not supported by inference request of type [{}]", + this.modelId, + this.inferenceConfig.getName(), + update.getName() + ) + ); return; } try { @@ -167,10 +172,7 @@ public void infer(Map fields, InferenceConfigUpdate update, Acti public InferenceResults infer(Map fields, InferenceConfigUpdate update) throws Exception { AtomicReference result = new AtomicReference<>(); AtomicReference exception = new AtomicReference<>(); - ActionListener listener = ActionListener.wrap( - result::set, - exception::set - ); + ActionListener listener = ActionListener.wrap(result::set, exception::set); infer(fields, update, listener); if (exception.get() != null) { @@ -237,19 +239,32 @@ public void close() { @Override public String toString() { - return "LocalModel{" + - "trainedModelDefinition=" + trainedModelDefinition + - ", modelId='" + modelId + '\'' + - ", fieldNames=" + fieldNames + - ", defaultFieldMap=" + defaultFieldMap + - ", statsAccumulator=" + statsAccumulator + - ", trainedModelStatsService=" + trainedModelStatsService + - ", persistenceQuotient=" + persistenceQuotient + - ", currentInferenceCount=" + currentInferenceCount + - ", inferenceConfig=" + inferenceConfig + - ", licenseLevel=" + licenseLevel + - ", trainedModelCircuitBreaker=" + trainedModelCircuitBreaker + - ", referenceCount=" + referenceCount + - '}'; + return "LocalModel{" + + "trainedModelDefinition=" + + trainedModelDefinition + + ", modelId='" + + modelId + + '\'' + + ", fieldNames=" + + fieldNames + + ", defaultFieldMap=" + + defaultFieldMap + + ", statsAccumulator=" + + statsAccumulator + + ", trainedModelStatsService=" + + trainedModelStatsService + + ", persistenceQuotient=" + + persistenceQuotient + + ", currentInferenceCount=" + + currentInferenceCount + + ", inferenceConfig=" + + inferenceConfig + + ", licenseLevel=" + + licenseLevel + + ", trainedModelCircuitBreaker=" + + trainedModelCircuitBreaker + + ", referenceCount=" + + referenceCount + + '}'; } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/loadingservice/ModelLoadingService.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/loadingservice/ModelLoadingService.java index 38332c4c1333b..44fe947a570d3 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/loadingservice/ModelLoadingService.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/loadingservice/ModelLoadingService.java @@ -83,10 +83,11 @@ public class ModelLoadingService implements ClusterStateListener { *

    * Once the limit is reached, LRU models are evicted in favor of new models */ - public static final Setting INFERENCE_MODEL_CACHE_SIZE = - Setting.memorySizeSetting("xpack.ml.inference_model.cache_size", - "40%", - Setting.Property.NodeScope); + public static final Setting INFERENCE_MODEL_CACHE_SIZE = Setting.memorySizeSetting( + "xpack.ml.inference_model.cache_size", + "40%", + Setting.Property.NodeScope + ); /** * How long should a model stay in the cache since its last access @@ -96,15 +97,18 @@ public class ModelLoadingService implements ClusterStateListener { * Specifically, in the ingest scenario, a processor will call getModel whenever it needs to run inference. So, if a processor is not * executed for an extended period of time, the model will be evicted and will have to be loaded again when getModel is called. */ - public static final Setting INFERENCE_MODEL_CACHE_TTL = - Setting.timeSetting("xpack.ml.inference_model.time_to_live", - new TimeValue(5, TimeUnit.MINUTES), - new TimeValue(1, TimeUnit.MILLISECONDS), - Setting.Property.NodeScope); + public static final Setting INFERENCE_MODEL_CACHE_TTL = Setting.timeSetting( + "xpack.ml.inference_model.time_to_live", + new TimeValue(5, TimeUnit.MINUTES), + new TimeValue(1, TimeUnit.MILLISECONDS), + Setting.Property.NodeScope + ); // The feature requesting the model public enum Consumer { - PIPELINE, SEARCH, INTERNAL + PIPELINE, + SEARCH, + INTERNAL } private static class ModelAndConsumer { @@ -135,15 +139,17 @@ private ModelAndConsumer(LocalModel model, Consumer consumer) { private final CircuitBreaker trainedModelCircuitBreaker; private final XPackLicenseState licenseState; - public ModelLoadingService(TrainedModelProvider trainedModelProvider, - InferenceAuditor auditor, - ThreadPool threadPool, - ClusterService clusterService, - TrainedModelStatsService modelStatsService, - Settings settings, - String localNode, - CircuitBreaker trainedModelCircuitBreaker, - XPackLicenseState licenseState) { + public ModelLoadingService( + TrainedModelProvider trainedModelProvider, + InferenceAuditor auditor, + ThreadPool threadPool, + ClusterService clusterService, + TrainedModelStatsService modelStatsService, + Settings settings, + String localNode, + CircuitBreaker trainedModelCircuitBreaker, + XPackLicenseState licenseState + ) { this.provider = trainedModelProvider; this.threadPool = threadPool; this.maxCacheSize = INFERENCE_MODEL_CACHE_SIZE.get(settings); @@ -246,11 +252,13 @@ private void getModel(String modelIdOrAlias, Consumer consumer, ActionListener new ParameterizedMessage( - "[{}] (model_alias [{}]) is loading or loaded, added new listener to queue", - modelId, - modelIdOrAlias - )); + logger.trace( + () -> new ParameterizedMessage( + "[{}] (model_alias [{}]) is loading or loaded, added new listener to queue", + modelId, + modelIdOrAlias + ) + ); } } @@ -282,8 +290,10 @@ private boolean loadModelIfNecessary(String modelIdOrAlias, Consumer consumer, A } // Add the listener to the queue if the model is loading - Queue> listeners = loadingListeners.computeIfPresent(modelId, - (storedModelKey, listenerQueue) -> addFluently(listenerQueue, modelActionListener)); + Queue> listeners = loadingListeners.computeIfPresent( + modelId, + (storedModelKey, listenerQueue) -> addFluently(listenerQueue, modelActionListener) + ); // The cachedModel entry is null, but there are listeners present, that means it is being loaded if (listeners != null) { @@ -293,18 +303,18 @@ private boolean loadModelIfNecessary(String modelIdOrAlias, Consumer consumer, A if (Consumer.SEARCH != consumer && referencedModels.contains(modelId) == false) { // The model is requested by a pipeline but not referenced by any ingest pipelines. // This means it is a simulate call and the model should not be cached - logger.trace(() -> new ParameterizedMessage( - "[{}] (model_alias [{}]) not actively loading, eager loading without cache", - modelId, - modelIdOrAlias - )); + logger.trace( + () -> new ParameterizedMessage( + "[{}] (model_alias [{}]) not actively loading, eager loading without cache", + modelId, + modelIdOrAlias + ) + ); loadWithoutCaching(modelId, consumer, modelActionListener); } else { - logger.trace(() -> new ParameterizedMessage( - "[{}] (model_alias [{}]) attempting to load and cache", - modelId, - modelIdOrAlias - )); + logger.trace( + () -> new ParameterizedMessage("[{}] (model_alias [{}]) attempting to load and cache", modelId, modelIdOrAlias) + ); loadingListeners.put(modelId, addFluently(new ArrayDeque<>(), modelActionListener)); loadModel(modelId, consumer); } @@ -313,86 +323,82 @@ private boolean loadModelIfNecessary(String modelIdOrAlias, Consumer consumer, A } private void loadModel(String modelId, Consumer consumer) { - provider.getTrainedModel(modelId, GetTrainedModelsAction.Includes.empty(), ActionListener.wrap( - trainedModelConfig -> { - if (trainedModelConfig.isAllocateOnly()) { - handleLoadFailure(modelId, new ElasticsearchException("model [{}] is allocate only", modelId)); + provider.getTrainedModel(modelId, GetTrainedModelsAction.Includes.empty(), ActionListener.wrap(trainedModelConfig -> { + if (trainedModelConfig.isAllocateOnly()) { + handleLoadFailure(modelId, new ElasticsearchException("model [{}] is allocate only", modelId)); + return; + } + auditNewReferencedModel(modelId); + trainedModelCircuitBreaker.addEstimateBytesAndMaybeBreak(trainedModelConfig.getEstimatedHeapMemory(), modelId); + provider.getTrainedModelForInference(modelId, consumer == Consumer.INTERNAL, ActionListener.wrap(inferenceDefinition -> { + try { + // Since we have used the previously stored estimate to help guard against OOM we need + // to adjust the memory so that the memory this model uses in the circuit breaker + // is the most accurate estimate. + updateCircuitBreakerEstimate(modelId, inferenceDefinition, trainedModelConfig); + } catch (CircuitBreakingException ex) { + handleLoadFailure(modelId, ex); return; } - auditNewReferencedModel(modelId); - trainedModelCircuitBreaker.addEstimateBytesAndMaybeBreak(trainedModelConfig.getEstimatedHeapMemory(), modelId); - provider.getTrainedModelForInference(modelId, consumer == Consumer.INTERNAL, ActionListener.wrap( - inferenceDefinition -> { - try { - // Since we have used the previously stored estimate to help guard against OOM we need - // to adjust the memory so that the memory this model uses in the circuit breaker - // is the most accurate estimate. - updateCircuitBreakerEstimate(modelId, inferenceDefinition, trainedModelConfig); - } catch (CircuitBreakingException ex) { - handleLoadFailure(modelId, ex); - return; - } - handleLoadSuccess(modelId, consumer, trainedModelConfig, inferenceDefinition); - }, - failure -> { - // We failed to get the definition, remove the initial estimation. - trainedModelCircuitBreaker.addWithoutBreaking(-trainedModelConfig.getEstimatedHeapMemory()); - logger.warn(new ParameterizedMessage("[{}] failed to load model definition", modelId), failure); - handleLoadFailure(modelId, failure); - } - )); - }, - failure -> { - logger.warn(new ParameterizedMessage("[{}] failed to load model configuration", modelId), failure); + handleLoadSuccess(modelId, consumer, trainedModelConfig, inferenceDefinition); + }, failure -> { + // We failed to get the definition, remove the initial estimation. + trainedModelCircuitBreaker.addWithoutBreaking(-trainedModelConfig.getEstimatedHeapMemory()); + logger.warn(new ParameterizedMessage("[{}] failed to load model definition", modelId), failure); handleLoadFailure(modelId, failure); - } - )); + })); + }, failure -> { + logger.warn(new ParameterizedMessage("[{}] failed to load model configuration", modelId), failure); + handleLoadFailure(modelId, failure); + })); } private void loadWithoutCaching(String modelId, Consumer consumer, ActionListener modelActionListener) { // If we the model is not loaded and we did not kick off a new loading attempt, this means that we may be getting called // by a simulated pipeline - provider.getTrainedModel(modelId, GetTrainedModelsAction.Includes.empty(), ActionListener.wrap( - trainedModelConfig -> { - // Verify we can pull the model into memory without causing OOM - trainedModelCircuitBreaker.addEstimateBytesAndMaybeBreak(trainedModelConfig.getEstimatedHeapMemory(), modelId); - provider.getTrainedModelForInference(modelId, consumer == Consumer.INTERNAL, ActionListener.wrap( - inferenceDefinition -> { - InferenceConfig inferenceConfig = trainedModelConfig.getInferenceConfig() == null ? - inferenceConfigFromTargetType(inferenceDefinition.getTargetType()) : - trainedModelConfig.getInferenceConfig(); - try { - updateCircuitBreakerEstimate(modelId, inferenceDefinition, trainedModelConfig); - } catch (CircuitBreakingException ex) { - modelActionListener.onFailure(ex); - return; - } + provider.getTrainedModel(modelId, GetTrainedModelsAction.Includes.empty(), ActionListener.wrap(trainedModelConfig -> { + // Verify we can pull the model into memory without causing OOM + trainedModelCircuitBreaker.addEstimateBytesAndMaybeBreak(trainedModelConfig.getEstimatedHeapMemory(), modelId); + provider.getTrainedModelForInference(modelId, consumer == Consumer.INTERNAL, ActionListener.wrap(inferenceDefinition -> { + InferenceConfig inferenceConfig = trainedModelConfig.getInferenceConfig() == null + ? inferenceConfigFromTargetType(inferenceDefinition.getTargetType()) + : trainedModelConfig.getInferenceConfig(); + try { + updateCircuitBreakerEstimate(modelId, inferenceDefinition, trainedModelConfig); + } catch (CircuitBreakingException ex) { + modelActionListener.onFailure(ex); + return; + } - modelActionListener.onResponse(new LocalModel( - trainedModelConfig.getModelId(), - localNode, - inferenceDefinition, - trainedModelConfig.getInput(), - trainedModelConfig.getDefaultFieldMap(), - inferenceConfig, - trainedModelConfig.getLicenseLevel(), - modelStatsService, - trainedModelCircuitBreaker)); - }, - // Failure getting the definition, remove the initial estimation value - e -> { - trainedModelCircuitBreaker.addWithoutBreaking(-trainedModelConfig.getEstimatedHeapMemory()); - modelActionListener.onFailure(e); - } - )); + modelActionListener.onResponse( + new LocalModel( + trainedModelConfig.getModelId(), + localNode, + inferenceDefinition, + trainedModelConfig.getInput(), + trainedModelConfig.getDefaultFieldMap(), + inferenceConfig, + trainedModelConfig.getLicenseLevel(), + modelStatsService, + trainedModelCircuitBreaker + ) + ); }, - modelActionListener::onFailure - )); + // Failure getting the definition, remove the initial estimation value + e -> { + trainedModelCircuitBreaker.addWithoutBreaking(-trainedModelConfig.getEstimatedHeapMemory()); + modelActionListener.onFailure(e); + } + )); + }, modelActionListener::onFailure)); } - private void updateCircuitBreakerEstimate(String modelId, InferenceDefinition inferenceDefinition, - TrainedModelConfig trainedModelConfig) throws CircuitBreakingException { + private void updateCircuitBreakerEstimate( + String modelId, + InferenceDefinition inferenceDefinition, + TrainedModelConfig trainedModelConfig + ) throws CircuitBreakingException { long estimateDiff = inferenceDefinition.ramBytesUsed() - trainedModelConfig.getEstimatedHeapMemory(); if (estimateDiff < 0) { trainedModelCircuitBreaker.addWithoutBreaking(estimateDiff); @@ -406,14 +412,16 @@ private void updateCircuitBreakerEstimate(String modelId, InferenceDefinition in } } - private void handleLoadSuccess(String modelId, - Consumer consumer, - TrainedModelConfig trainedModelConfig, - InferenceDefinition inferenceDefinition) { + private void handleLoadSuccess( + String modelId, + Consumer consumer, + TrainedModelConfig trainedModelConfig, + InferenceDefinition inferenceDefinition + ) { Queue> listeners; - InferenceConfig inferenceConfig = trainedModelConfig.getInferenceConfig() == null ? - inferenceConfigFromTargetType(inferenceDefinition.getTargetType()) : - trainedModelConfig.getInferenceConfig(); + InferenceConfig inferenceConfig = trainedModelConfig.getInferenceConfig() == null + ? inferenceConfigFromTargetType(inferenceDefinition.getTargetType()) + : trainedModelConfig.getInferenceConfig(); LocalModel loadedModel = new LocalModel( trainedModelConfig.getModelId(), localNode, @@ -423,7 +431,8 @@ private void handleLoadSuccess(String modelId, inferenceConfig, trainedModelConfig.getLicenseLevel(), modelStatsService, - trainedModelCircuitBreaker); + trainedModelCircuitBreaker + ); final ModelAndConsumerLoader modelAndConsumerLoader = new ModelAndConsumerLoader(new ModelAndConsumer(loadedModel, consumer)); synchronized (loadingListeners) { populateNewModelAlias(modelId); @@ -440,8 +449,8 @@ private void handleLoadSuccess(String modelId, // We should start tracking on successful load. It will stop being tracked once it evacuates the cache and is no // longer a referenced model // NOTE: It is not possible to change the referenced models without locking on `loadingListeners` - // So, if the model is evacuated from cache immediately after checking that it was present, - // the feature usage will still be tracked. + // So, if the model is evacuated from cache immediately after checking that it was present, + // the feature usage will still be tracked. if (License.OperationMode.BASIC.equals(trainedModelConfig.getLicenseLevel()) == false) { ML_MODEL_INFERENCE_FEATURE.startTracking(licenseState, modelId); } @@ -455,7 +464,7 @@ private void handleLoadSuccess(String modelId, if (listeners == null) { // If we newly added it into cache, release the model so that the circuit breaker can still accurately keep track // of memory - if(modelAndConsumerLoader.isLoaded()) { + if (modelAndConsumerLoader.isLoaded()) { loadedModel.release(); } return; @@ -480,8 +489,8 @@ private void handleLoadFailure(String modelId, Exception failure) { return; } } // synchronized (loadingListeners) - // If we failed to load and there were listeners present, that means that this model is referenced by a processor - // Alert the listeners to the failure + // If we failed to load and there were listeners present, that means that this model is referenced by a processor + // Alert the listeners to the failure for (ActionListener listener = listeners.poll(); listener != null; listener = listeners.poll()) { listener.onFailure(failure); } @@ -490,12 +499,10 @@ private void handleLoadFailure(String modelId, Exception failure) { private void populateNewModelAlias(String modelId) { Set newModelAliases = modelIdToUpdatedModelAliases.remove(modelId); if (newModelAliases != null && newModelAliases.isEmpty() == false) { - logger.trace(() -> new ParameterizedMessage( - "[{}] model is now loaded, setting new model_aliases {}", - modelId, - newModelAliases - )); - for (String modelAlias: newModelAliases) { + logger.trace( + () -> new ParameterizedMessage("[{}] model is now loaded, setting new model_aliases {}", modelId, newModelAliases) + ); + for (String modelAlias : newModelAliases) { modelAliasToId.put(modelAlias, modelId); } } @@ -505,22 +512,25 @@ private void cacheEvictionListener(RemovalNotification try { if (notification.getRemovalReason() == RemovalNotification.RemovalReason.EVICTED) { MessageSupplier msg = () -> new ParameterizedMessage( - "model cache entry evicted." + - "current cache [{}] current max [{}] model size [{}]. " + - "If this is undesired, consider updating setting [{}] or [{}].", + "model cache entry evicted." + + "current cache [{}] current max [{}] model size [{}]. " + + "If this is undesired, consider updating setting [{}] or [{}].", ByteSizeValue.ofBytes(localModelCache.weight()).getStringRep(), maxCacheSize.getStringRep(), ByteSizeValue.ofBytes(notification.getValue().model.ramBytesUsed()).getStringRep(), INFERENCE_MODEL_CACHE_SIZE.getKey(), - INFERENCE_MODEL_CACHE_TTL.getKey()); + INFERENCE_MODEL_CACHE_TTL.getKey() + ); auditIfNecessary(notification.getKey(), msg); } String modelId = modelAliasToId.getOrDefault(notification.getKey(), notification.getKey()); - logger.trace(() -> new ParameterizedMessage( - "Persisting stats for evicted model [{}] (model_aliases {})", - modelId, - modelIdToModelAliases.getOrDefault(modelId, new HashSet<>()) - )); + logger.trace( + () -> new ParameterizedMessage( + "Persisting stats for evicted model [{}] (model_aliases {})", + modelId, + modelIdToModelAliases.getOrDefault(modelId, new HashSet<>()) + ) + ); // If it's not referenced in a pipeline, stop tracking it on this node if (referencedModels.contains(modelId) == false) { ML_MODEL_INFERENCE_FEATURE.stopTracking(licenseState, modelId); @@ -545,9 +555,9 @@ public void clusterChanged(ClusterChangedEvent event) { ClusterState state = event.state(); IngestMetadata currentIngestMetadata = state.metadata().custom(IngestMetadata.TYPE); - Set allReferencedModelKeys = event.changedCustomMetadataSet().contains(IngestMetadata.TYPE) ? - getReferencedModelKeys(currentIngestMetadata) : - new HashSet<>(referencedModels); + Set allReferencedModelKeys = event.changedCustomMetadataSet().contains(IngestMetadata.TYPE) + ? getReferencedModelKeys(currentIngestMetadata) + : new HashSet<>(referencedModels); Set referencedModelsBeforeClusterState; Set loadingModelBeforeClusterState = null; Set removedModels; @@ -582,13 +592,17 @@ public void clusterChanged(ClusterChangedEvent event) { String modelId = changedAliases.getOrDefault(modelAliasOrId, modelAliasToId.getOrDefault(modelAliasOrId, modelAliasOrId)); // If the "old" model_alias is referenced, we don't want to invalidate. This way the model that now has the model_alias // can be loaded in first - boolean oldModelAliasesNotReferenced = Sets.haveEmptyIntersection(referencedModels, - oldIdToAliases.getOrDefault(modelId, Collections.emptySet())); + boolean oldModelAliasesNotReferenced = Sets.haveEmptyIntersection( + referencedModels, + oldIdToAliases.getOrDefault(modelId, Collections.emptySet()) + ); // If the model itself is referenced, we shouldn't evict. boolean modelIsNotReferenced = referencedModels.contains(modelId) == false; // If a model_alias change causes it to NOW be referenced, we shouldn't attempt to evict it - boolean newModelAliasesNotReferenced = Sets.haveEmptyIntersection(referencedModels, - modelIdToModelAliases.getOrDefault(modelId, Collections.emptySet())); + boolean newModelAliasesNotReferenced = Sets.haveEmptyIntersection( + referencedModels, + modelIdToModelAliases.getOrDefault(modelId, Collections.emptySet()) + ); if (oldModelAliasesNotReferenced && newModelAliasesNotReferenced && modelIsNotReferenced) { ModelAndConsumer modelAndConsumer = localModelCache.get(modelId); if (modelAndConsumer != null && modelAndConsumer.consumers.contains(Consumer.SEARCH) == false) { @@ -611,10 +625,7 @@ public void clusterChanged(ClusterChangedEvent event) { String modelId = changedAliases.getOrDefault( newlyReferencedModel, // If the model_alias hasn't changed, get the model id IF it is a model_alias, otherwise we assume it is an id - modelAliasToId.getOrDefault( - newlyReferencedModel, - newlyReferencedModel - ) + modelAliasToId.getOrDefault(newlyReferencedModel, newlyReferencedModel) ); // Verify that it isn't an old model id but just a new model_alias if (referencedModels.contains(modelId) == false) { @@ -653,18 +664,26 @@ public void clusterChanged(ClusterChangedEvent event) { } // synchronized (loadingListeners) if (logger.isTraceEnabled()) { if (loadingListeners.keySet().equals(loadingModelBeforeClusterState) == false) { - logger.trace("cluster state event changed loading models: before {} after {}", loadingModelBeforeClusterState, - loadingListeners.keySet()); + logger.trace( + "cluster state event changed loading models: before {} after {}", + loadingModelBeforeClusterState, + loadingListeners.keySet() + ); } if (referencedModels.equals(referencedModelsBeforeClusterState) == false) { - logger.trace("cluster state event changed referenced models: before {} after {}", referencedModelsBeforeClusterState, - referencedModels); + logger.trace( + "cluster state event changed referenced models: before {} after {}", + referencedModelsBeforeClusterState, + referencedModels + ); } if (oldIdToAliases.equals(modelIdToModelAliases) == false) { - logger.trace("model id to alias mappings changed. before {} after {}. Model alias to IDs {}", + logger.trace( + "model id to alias mappings changed. before {} after {}. Model alias to IDs {}", oldIdToAliases, modelIdToModelAliases, - modelAliasToId); + modelAliasToId + ); } if (addedModelViaAliases.isEmpty() == false) { logger.trace("adding new models via model_aliases and ids: {}", addedModelViaAliases); @@ -677,9 +696,11 @@ public void clusterChanged(ClusterChangedEvent event) { loadModelsForPipeline(addedModelViaAliases.keySet()); } - private Map gatherLazyChangedAliasesAndUpdateModelAliases(ClusterChangedEvent event, - boolean prefetchModels, - Set allReferencedModelKeys) { + private Map gatherLazyChangedAliasesAndUpdateModelAliases( + ClusterChangedEvent event, + boolean prefetchModels, + Set allReferencedModelKeys + ) { Map changedAliases = new HashMap<>(); if (event.changedCustomMetadataSet().contains(ModelAliasMetadata.NAME)) { final Map modelAliasesToIds = new HashMap<>( @@ -689,8 +710,7 @@ private Map gatherLazyChangedAliasesAndUpdateModelAliases(Cluste for (Map.Entry aliasToId : modelAliasesToIds.entrySet()) { modelIdToModelAliases.computeIfAbsent(aliasToId.getValue().getModelId(), k -> new HashSet<>()).add(aliasToId.getKey()); java.lang.String modelId = modelAliasToId.get(aliasToId.getKey()); - if (modelId != null - && modelId.equals(aliasToId.getValue().getModelId()) == false) { + if (modelId != null && modelId.equals(aliasToId.getValue().getModelId()) == false) { if (prefetchModels && allReferencedModelKeys.contains(aliasToId.getKey())) { changedAliases.put(aliasToId.getKey(), aliasToId.getValue().getModelId()); } else { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/modelsize/EnsembleSizeInfo.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/modelsize/EnsembleSizeInfo.java index 1a21f3600d0a5..aa1c40584b8c5 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/modelsize/EnsembleSizeInfo.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/modelsize/EnsembleSizeInfo.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.ml.inference.modelsize; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.ensemble.LogisticRegression; @@ -40,12 +40,14 @@ public class EnsembleSizeInfo implements TrainedModelSizeInfo { static ConstructingObjectParser PARSER = new ConstructingObjectParser<>( "ensemble_size", false, - a -> new EnsembleSizeInfo((List)a[0], - (Integer)a[1], - (List)a[2], - a[3] == null ? 0 : (Integer)a[3], - a[4] == null ? 0 : (Integer)a[4], - a[5] == null ? 0 : (Integer)a[5]) + a -> new EnsembleSizeInfo( + (List) a[0], + (Integer) a[1], + (List) a[2], + a[3] == null ? 0 : (Integer) a[3], + a[4] == null ? 0 : (Integer) a[4], + a[5] == null ? 0 : (Integer) a[5] + ) ); static { PARSER.declareObjectArray(constructorArg(), TreeSizeInfo.PARSER::apply, TREE_SIZES); @@ -60,7 +62,6 @@ public static EnsembleSizeInfo fromXContent(XContentParser parser) { return PARSER.apply(parser, null); } - private final List treeSizeInfos; private final int numOperations; private final int[] featureNameLengths; @@ -68,12 +69,14 @@ public static EnsembleSizeInfo fromXContent(XContentParser parser) { private final int numClassificationWeights; private final int numClasses; - public EnsembleSizeInfo(List treeSizeInfos, - int numOperations, - List featureNameLengths, - int numOutputProcessorWeights, - int numClassificationWeights, - int numClasses) { + public EnsembleSizeInfo( + List treeSizeInfos, + int numOperations, + List featureNameLengths, + int numOutputProcessorWeights, + int numClassificationWeights, + int numClasses + ) { this.treeSizeInfos = treeSizeInfos; this.numOperations = numOperations; this.featureNameLengths = featureNameLengths.stream().mapToInt(Integer::intValue).toArray(); @@ -115,12 +118,12 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; EnsembleSizeInfo that = (EnsembleSizeInfo) o; - return numOperations == that.numOperations && - numOutputProcessorWeights == that.numOutputProcessorWeights && - numClassificationWeights == that.numClassificationWeights && - numClasses == that.numClasses && - Objects.equals(treeSizeInfos, that.treeSizeInfos) && - Arrays.equals(featureNameLengths, that.featureNameLengths); + return numOperations == that.numOperations + && numOutputProcessorWeights == that.numOutputProcessorWeights + && numClassificationWeights == that.numClassificationWeights + && numClasses == that.numClasses + && Objects.equals(treeSizeInfos, that.treeSizeInfos) + && Arrays.equals(featureNameLengths, that.featureNameLengths); } @Override diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/modelsize/FrequencyEncodingSize.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/modelsize/FrequencyEncodingSize.java index 015935085af1f..b9e6567aee56b 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/modelsize/FrequencyEncodingSize.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/modelsize/FrequencyEncodingSize.java @@ -31,7 +31,7 @@ public class FrequencyEncodingSize implements PreprocessorSize { private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( "frequency_encoding_size", false, - a -> new FrequencyEncodingSize((Integer)a[0], (Integer)a[1], (List)a[2]) + a -> new FrequencyEncodingSize((Integer) a[0], (Integer) a[1], (List) a[2]) ); static { PARSER.declareInt(constructorArg(), FIELD_LENGTH); @@ -61,7 +61,8 @@ public long ramBytesUsed() { size += sizeOfString(featureNameLength); size += sizeOfHashMap( Arrays.stream(fieldValueLengths).mapToLong(SizeEstimatorHelper::sizeOfString).boxed().collect(Collectors.toList()), - Stream.generate(() -> sizeOfDoubleObject).limit(fieldValueLengths.length).collect(Collectors.toList())); + Stream.generate(() -> sizeOfDoubleObject).limit(fieldValueLengths.length).collect(Collectors.toList()) + ); return alignObjectSize(size); } @@ -85,9 +86,9 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; FrequencyEncodingSize that = (FrequencyEncodingSize) o; - return fieldLength == that.fieldLength && - featureNameLength == that.featureNameLength && - Arrays.equals(fieldValueLengths, that.fieldValueLengths); + return fieldLength == that.fieldLength + && featureNameLength == that.featureNameLength + && Arrays.equals(fieldValueLengths, that.fieldValueLengths); } @Override diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/modelsize/MlModelSizeNamedXContentProvider.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/modelsize/MlModelSizeNamedXContentProvider.java index bada85ceae641..3b96bbe8b12a3 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/modelsize/MlModelSizeNamedXContentProvider.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/modelsize/MlModelSizeNamedXContentProvider.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.ml.inference.modelsize; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.plugins.spi.NamedXContentProvider; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.ml.inference.preprocessing.FrequencyEncoding; import org.elasticsearch.xpack.core.ml.inference.preprocessing.OneHotEncoding; import org.elasticsearch.xpack.core.ml.inference.preprocessing.TargetMeanEncoding; @@ -20,18 +20,10 @@ public class MlModelSizeNamedXContentProvider implements NamedXContentProvider { @Override public List getNamedXContentParsers() { return Arrays.asList( - new NamedXContentRegistry.Entry(PreprocessorSize.class, - FrequencyEncoding.NAME, - FrequencyEncodingSize::fromXContent), - new NamedXContentRegistry.Entry(PreprocessorSize.class, - OneHotEncoding.NAME, - OneHotEncodingSize::fromXContent), - new NamedXContentRegistry.Entry(PreprocessorSize.class, - TargetMeanEncoding.NAME, - TargetMeanEncodingSize::fromXContent), - new NamedXContentRegistry.Entry(TrainedModelSizeInfo.class, - EnsembleSizeInfo.NAME, - EnsembleSizeInfo::fromXContent) + new NamedXContentRegistry.Entry(PreprocessorSize.class, FrequencyEncoding.NAME, FrequencyEncodingSize::fromXContent), + new NamedXContentRegistry.Entry(PreprocessorSize.class, OneHotEncoding.NAME, OneHotEncodingSize::fromXContent), + new NamedXContentRegistry.Entry(PreprocessorSize.class, TargetMeanEncoding.NAME, TargetMeanEncodingSize::fromXContent), + new NamedXContentRegistry.Entry(TrainedModelSizeInfo.class, EnsembleSizeInfo.NAME, EnsembleSizeInfo::fromXContent) ); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/modelsize/ModelSizeInfo.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/modelsize/ModelSizeInfo.java index ef512b949e639..587e7e3815c2d 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/modelsize/ModelSizeInfo.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/modelsize/ModelSizeInfo.java @@ -8,8 +8,8 @@ package org.elasticsearch.xpack.ml.inference.modelsize; import org.apache.lucene.util.Accountable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.inference.InferenceDefinition; @@ -33,16 +33,16 @@ public class ModelSizeInfo implements Accountable, ToXContentObject { public static ConstructingObjectParser PARSER = new ConstructingObjectParser<>( "model_size", false, - a -> new ModelSizeInfo((EnsembleSizeInfo)a[0], (List)a[1]) + a -> new ModelSizeInfo((EnsembleSizeInfo) a[0], (List) a[1]) ); static { - PARSER.declareNamedObject(constructorArg(), - (p, c, n) -> p.namedObject(TrainedModelSizeInfo.class, n, null), - TRAINED_MODEL_SIZE); - PARSER.declareNamedObjects(optionalConstructorArg(), + PARSER.declareNamedObject(constructorArg(), (p, c, n) -> p.namedObject(TrainedModelSizeInfo.class, n, null), TRAINED_MODEL_SIZE); + PARSER.declareNamedObjects( + optionalConstructorArg(), (p, c, n) -> p.namedObject(PreprocessorSize.class, n, null), (val) -> {}, - PREPROCESSORS); + PREPROCESSORS + ); } private final EnsembleSizeInfo ensembleSizeInfo; @@ -81,8 +81,8 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; ModelSizeInfo modelSizeInfo = (ModelSizeInfo) o; - return Objects.equals(ensembleSizeInfo, modelSizeInfo.ensembleSizeInfo) && - Objects.equals(preprocessorSizes, modelSizeInfo.preprocessorSizes); + return Objects.equals(ensembleSizeInfo, modelSizeInfo.ensembleSizeInfo) + && Objects.equals(preprocessorSizes, modelSizeInfo.preprocessorSizes); } @Override diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/modelsize/OneHotEncodingSize.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/modelsize/OneHotEncodingSize.java index f8c0cb82880f9..b898822b4a15f 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/modelsize/OneHotEncodingSize.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/modelsize/OneHotEncodingSize.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.ml.inference.modelsize; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.inference.preprocessing.OneHotEncoding; @@ -32,7 +32,7 @@ public class OneHotEncodingSize implements PreprocessorSize { private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( "one_hot_encoding_size", false, - a -> new OneHotEncodingSize((Integer)a[0], (List)a[1], (List)a[2]) + a -> new OneHotEncodingSize((Integer) a[0], (List) a[1], (List) a[2]) ); static { PARSER.declareInt(constructorArg(), FIELD_LENGTH); @@ -86,9 +86,9 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; OneHotEncodingSize that = (OneHotEncodingSize) o; - return fieldLength == that.fieldLength && - Arrays.equals(featureNameLengths, that.featureNameLengths) && - Arrays.equals(fieldValueLengths, that.fieldValueLengths); + return fieldLength == that.fieldLength + && Arrays.equals(featureNameLengths, that.featureNameLengths) + && Arrays.equals(fieldValueLengths, that.fieldValueLengths); } @Override diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/modelsize/PreprocessorSize.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/modelsize/PreprocessorSize.java index 4bc681602ca89..0cf0f8d01bd93 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/modelsize/PreprocessorSize.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/modelsize/PreprocessorSize.java @@ -11,7 +11,6 @@ import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xpack.core.ml.utils.NamedXContentObject; - public interface PreprocessorSize extends Accountable, NamedXContentObject { ParseField FIELD_LENGTH = new ParseField("field_length"); ParseField FEATURE_NAME_LENGTH = new ParseField("feature_name_length"); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/modelsize/SizeEstimatorHelper.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/modelsize/SizeEstimatorHelper.java index 580ee05e07cd8..440988b8270c2 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/modelsize/SizeEstimatorHelper.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/modelsize/SizeEstimatorHelper.java @@ -25,7 +25,7 @@ private SizeEstimatorHelper() {} static long sizeOfString(int stringLength) { // Technically, each value counted in a String.length is 2 bytes. But, this is how `RamUsageEstimator` calculates it - return alignObjectSize(STRING_SIZE + (long)NUM_BYTES_ARRAY_HEADER + (long)(Character.BYTES) * stringLength); + return alignObjectSize(STRING_SIZE + (long) NUM_BYTES_ARRAY_HEADER + (long) (Character.BYTES) * stringLength); } static long sizeOfStringCollection(int[] stringSizes) { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/modelsize/TargetMeanEncodingSize.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/modelsize/TargetMeanEncodingSize.java index 334abf3b76775..777184d879f00 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/modelsize/TargetMeanEncodingSize.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/modelsize/TargetMeanEncodingSize.java @@ -30,7 +30,7 @@ public class TargetMeanEncodingSize implements PreprocessorSize { private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( "target_mean_encoding_size", false, - a -> new TargetMeanEncodingSize((Integer)a[0], (Integer)a[1], (List)a[2]) + a -> new TargetMeanEncodingSize((Integer) a[0], (Integer) a[1], (List) a[2]) ); static { PARSER.declareInt(constructorArg(), FIELD_LENGTH); @@ -85,9 +85,9 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; TargetMeanEncodingSize that = (TargetMeanEncodingSize) o; - return fieldLength == that.fieldLength && - featureNameLength == that.featureNameLength && - Arrays.equals(fieldValueLengths, that.fieldValueLengths); + return fieldLength == that.fieldLength + && featureNameLength == that.featureNameLength + && Arrays.equals(fieldValueLengths, that.fieldValueLengths); } @Override diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/modelsize/TrainedModelSizeInfo.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/modelsize/TrainedModelSizeInfo.java index 532ec1aecb900..6828439374eb1 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/modelsize/TrainedModelSizeInfo.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/modelsize/TrainedModelSizeInfo.java @@ -10,5 +10,4 @@ import org.apache.lucene.util.Accountable; import org.elasticsearch.xpack.core.ml.utils.NamedXContentObject; -interface TrainedModelSizeInfo extends Accountable, NamedXContentObject { -} +interface TrainedModelSizeInfo extends Accountable, NamedXContentObject {} diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/modelsize/TreeSizeInfo.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/modelsize/TreeSizeInfo.java index 21d23bdf14927..f644c06d45a3d 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/modelsize/TreeSizeInfo.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/modelsize/TreeSizeInfo.java @@ -8,14 +8,13 @@ package org.elasticsearch.xpack.ml.inference.modelsize; import org.apache.lucene.util.Accountable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.inference.TreeInferenceModel; - import java.io.IOException; import java.util.Objects; @@ -35,7 +34,7 @@ public class TreeSizeInfo implements Accountable, ToXContentObject { static ConstructingObjectParser PARSER = new ConstructingObjectParser<>( "tree_size", false, - a -> new TreeSizeInfo((Integer)a[0], a[1] == null ? 0 : (Integer)a[1], a[2] == null ? 0 : (Integer)a[2]) + a -> new TreeSizeInfo((Integer) a[0], a[1] == null ? 0 : (Integer) a[1], a[2] == null ? 0 : (Integer) a[2]) ); static { PARSER.declareInt(constructorArg(), NUM_LEAVES); @@ -90,9 +89,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; TreeSizeInfo treeSizeInfo = (TreeSizeInfo) o; - return numNodes == treeSizeInfo.numNodes && - numLeaves == treeSizeInfo.numLeaves && - numClasses == treeSizeInfo.numClasses; + return numNodes == treeSizeInfo.numNodes && numLeaves == treeSizeInfo.numLeaves && numClasses == treeSizeInfo.numClasses; } @Override diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/BertRequestBuilder.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/BertRequestBuilder.java index 2bac127ff4d9f..9988caed48481 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/BertRequestBuilder.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/BertRequestBuilder.java @@ -34,8 +34,7 @@ public BertRequestBuilder(BertTokenizer tokenizer) { @Override public NlpTask.Request buildRequest(List inputs, String requestId) throws IOException { if (tokenizer.getPadToken().isEmpty()) { - throw new IllegalStateException("The input tokenizer does not have a " + BertTokenizer.PAD_TOKEN + - " token in its vocabulary"); + throw new IllegalStateException("The input tokenizer does not have a " + BertTokenizer.PAD_TOKEN + " token in its vocabulary"); } TokenizationResult tokenization = tokenizer.buildTokenizationResult( @@ -47,15 +46,12 @@ public NlpTask.Request buildRequest(List inputs, String requestId) throw @Override public NlpTask.Request buildRequest(TokenizationResult tokenization, String requestId) throws IOException { if (tokenizer.getPadToken().isEmpty()) { - throw new IllegalStateException("The input tokenizer does not have a " + BertTokenizer.PAD_TOKEN + - " token in its vocabulary"); + throw new IllegalStateException("The input tokenizer does not have a " + BertTokenizer.PAD_TOKEN + " token in its vocabulary"); } return new NlpTask.Request(tokenization, jsonRequest(tokenization, tokenizer.getPadToken().getAsInt(), requestId)); } - static BytesReference jsonRequest(TokenizationResult tokenization, - int padToken, - String requestId) throws IOException { + static BytesReference jsonRequest(TokenizationResult tokenization, int padToken, String requestId) throws IOException { XContentBuilder builder = XContentFactory.jsonBuilder(); builder.startObject(); builder.field(REQUEST_ID, requestId); @@ -71,5 +67,4 @@ static BytesReference jsonRequest(TokenizationResult tokenization, return BytesReference.bytes(builder); } - } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/FillMaskProcessor.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/FillMaskProcessor.java index 3cc96fc165985..e5d9b111b292c 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/FillMaskProcessor.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/FillMaskProcessor.java @@ -79,8 +79,7 @@ static InferenceResults processResult( int numResults, String resultsField ) { - if (tokenization.getTokenizations().isEmpty() || - tokenization.getTokenizations().get(0).getTokens().length == 0) { + if (tokenization.getTokenizations().isEmpty() || tokenization.getTokenizations().get(0).getTokens().length == 0) { return new WarningInferenceResults("No valid tokens for inference"); } @@ -103,10 +102,10 @@ static InferenceResults processResult( return new FillMaskResults( scoreAndIndices[0].index, tokenization.getFromVocab(scoreAndIndices[0].index), - tokenization.getTokenizations().get(0).getInput().replace( - BertTokenizer.MASK_TOKEN, - tokenization.getFromVocab(scoreAndIndices[0].index) - ), + tokenization.getTokenizations() + .get(0) + .getInput() + .replace(BertTokenizer.MASK_TOKEN, tokenization.getFromVocab(scoreAndIndices[0].index)), results, DEFAULT_TOP_CLASSES_RESULTS_FIELD, Optional.ofNullable(resultsField).orElse(DEFAULT_RESULTS_FIELD), diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/NerProcessor.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/NerProcessor.java index 9370849a74b81..238c5550b0367 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/NerProcessor.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/NerProcessor.java @@ -33,7 +33,11 @@ public class NerProcessor implements NlpTask.Processor { public enum Entity implements Writeable { - NONE, MISC, PER, ORG, LOC; + NONE, + MISC, + PER, + ORG, + LOC; @Override public void writeTo(StreamOutput out) throws IOException { @@ -189,8 +193,7 @@ static class NerResultProcessor implements NlpTask.ResultProcessor { @Override public InferenceResults processResult(TokenizationResult tokenization, PyTorchResult pyTorchResult) { - if (tokenization.getTokenizations().isEmpty() || - tokenization.getTokenizations().get(0).getTokens().length == 0) { + if (tokenization.getTokenizations().isEmpty() || tokenization.getTokenizations().get(0).getTokens().length == 0) { return new WarningInferenceResults("no valid tokens to build result"); } // TODO - process all results in the batch @@ -206,9 +209,9 @@ public InferenceResults processResult(TokenizationResult tokenization, PyTorchRe List entities = groupTaggedTokens( taggedTokens, - ignoreCase ? - tokenization.getTokenizations().get(0).getInput().toLowerCase(Locale.ROOT) : - tokenization.getTokenizations().get(0).getInput() + ignoreCase + ? tokenization.getTokenizations().get(0).getInput().toLowerCase(Locale.ROOT) + : tokenization.getTokenizations().get(0).getInput() ); return new NerResults(resultsField, buildAnnotatedText(tokenization.getTokenizations().get(0).getInput(), entities), entities); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/NlpHelpers.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/NlpHelpers.java index 1509f5172e8f7..f6f9762898e90 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/NlpHelpers.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/NlpHelpers.java @@ -65,7 +65,6 @@ static int argmax(double[] arr) { return maxIndex; } - /** * Find the top K highest values in {@code arr} and their * index positions. Similar to {@link #argmax(double[])} @@ -86,7 +85,7 @@ static ScoreAndIndex[] topK(int k, double[] arr) { PriorityQueue minHeap = new PriorityQueue<>(k, Comparator.comparingDouble(o -> o.score)); // initialise with the first k values - for (int i=0; i=0; i--) { + for (int i = k - 1; i >= 0; i--) { result[i] = minHeap.poll(); } return result; diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/NlpTask.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/NlpTask.java index 23f2be2867481..393c48d3e7377 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/NlpTask.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/NlpTask.java @@ -9,8 +9,8 @@ import org.elasticsearch.common.ValidationException; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.support.XContentMapValues; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ml.inference.TrainedModelInput; import org.elasticsearch.xpack.core.ml.inference.results.InferenceResults; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.NlpConfig; @@ -58,11 +58,13 @@ interface TokenLookupFunction { Request buildRequest(TokenizationResult tokenizationResult, String requestId) throws IOException; - static void writePaddedTokens(String fieldName, - TokenizationResult tokenization, - int padToken, - TokenLookupFunction generator, - XContentBuilder builder) throws IOException { + static void writePaddedTokens( + String fieldName, + TokenizationResult tokenization, + int padToken, + TokenLookupFunction generator, + XContentBuilder builder + ) throws IOException { builder.startArray(fieldName); for (var inputTokens : tokenization.getTokenizations()) { builder.startArray(); @@ -79,10 +81,13 @@ static void writePaddedTokens(String fieldName, builder.endArray(); } - static void writeNonPaddedArguments(String fieldName, - int numTokenizations, int longestSequenceLength, - IntToIntFunction generator, - XContentBuilder builder) throws IOException { + static void writeNonPaddedArguments( + String fieldName, + int numTokenizations, + int longestSequenceLength, + IntToIntFunction generator, + XContentBuilder builder + ) throws IOException { builder.startArray(fieldName); for (int i = 0; i < numTokenizations; i++) { builder.startArray(); @@ -109,6 +114,7 @@ public interface Processor { void validateInputs(List inputs); RequestBuilder getRequestBuilder(NlpConfig config); + ResultProcessor getResultProcessor(NlpConfig config); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/TaskType.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/TaskType.java index e47846e488080..2f011600faa86 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/TaskType.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/TaskType.java @@ -7,10 +7,10 @@ package org.elasticsearch.xpack.ml.inference.nlp; -import org.elasticsearch.xpack.core.ml.inference.trainedmodel.PassThroughConfig; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.FillMaskConfig; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.NerConfig; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.NlpConfig; +import org.elasticsearch.xpack.core.ml.inference.trainedmodel.PassThroughConfig; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.TextClassificationConfig; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.TextEmbeddingConfig; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.ZeroShotClassificationConfig; diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/TextClassificationProcessor.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/TextClassificationProcessor.java index 5e233f64cdbc5..8b8f9fa53d0a8 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/TextClassificationProcessor.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/TextClassificationProcessor.java @@ -65,9 +65,9 @@ public NlpTask.ResultProcessor getResultProcessor(NlpConfig config) { return (tokenization, pytorchResult) -> processResult( tokenization, pytorchResult, - textClassificationConfig.getNumTopClasses() < 0 ? - textClassificationConfig.getClassificationLabels().size() : - textClassificationConfig.getNumTopClasses(), + textClassificationConfig.getNumTopClasses() < 0 + ? textClassificationConfig.getClassificationLabels().size() + : textClassificationConfig.getNumTopClasses(), textClassificationConfig.getClassificationLabels(), textClassificationConfig.getResultsField() ); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/Vocabulary.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/Vocabulary.java index 64b988f3dc0d4..0217667161a03 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/Vocabulary.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/Vocabulary.java @@ -28,10 +28,13 @@ public class Vocabulary implements Writeable, ToXContentObject { private static final String NAME = "vocabulary"; private static final ParseField VOCAB = new ParseField("vocab"); - @SuppressWarnings({ "unchecked"}) + @SuppressWarnings({ "unchecked" }) public static ConstructingObjectParser createParser(boolean ignoreUnkownFields) { - ConstructingObjectParser parser = new ConstructingObjectParser<>("vocabulary", ignoreUnkownFields, - a -> new Vocabulary((List) a[0], (String) a[1])); + ConstructingObjectParser parser = new ConstructingObjectParser<>( + "vocabulary", + ignoreUnkownFields, + a -> new Vocabulary((List) a[0], (String) a[1]) + ); parser.declareStringArray(ConstructingObjectParser.constructorArg(), VOCAB); parser.declareString(ConstructingObjectParser.constructorArg(), TrainedModelConfig.MODEL_ID); return parser; diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/ZeroShotClassificationProcessor.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/ZeroShotClassificationProcessor.java index accaabd2d5eee..69e87ee64b3bc 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/ZeroShotClassificationProcessor.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/ZeroShotClassificationProcessor.java @@ -174,7 +174,7 @@ public InferenceResults processResult(TokenizationResult tokenization, PyTorchRe } // assume entailment is `0`, softmax between entailment and contradiction normalizedScores[v++] = NlpHelpers.convertToProbabilitiesBySoftMax( - new double[]{vals[entailmentPos], vals[contraPos]} + new double[] { vals[entailmentPos], vals[contraPos] } )[0]; } } else { @@ -194,16 +194,14 @@ public InferenceResults processResult(TokenizationResult tokenization, PyTorchRe } int[] sortedIndices = IntStream.range(0, normalizedScores.length) .boxed() - .sorted(Comparator.comparing(i -> normalizedScores[(Integer)i]).reversed()) + .sorted(Comparator.comparing(i -> normalizedScores[(Integer) i]).reversed()) .mapToInt(i -> i) .toArray(); return new ClassificationInferenceResults( sortedIndices[0], labels[sortedIndices[0]], - Arrays.stream(sortedIndices) - .mapToObj(i -> new TopClassEntry(labels[i], normalizedScores[i])) - .collect(Collectors.toList()), + Arrays.stream(sortedIndices).mapToObj(i -> new TopClassEntry(labels[i], normalizedScores[i])).collect(Collectors.toList()), List.of(), DEFAULT_TOP_CLASSES_RESULTS_FIELD, Optional.ofNullable(resultsField).orElse(DEFAULT_RESULTS_FIELD), diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/tokenizers/BasicTokenizer.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/tokenizers/BasicTokenizer.java index 207b2d88dd603..25f00bcc6dad6 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/tokenizers/BasicTokenizer.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/tokenizers/BasicTokenizer.java @@ -42,8 +42,7 @@ public class BasicTokenizer { * @param isStripAccents Strip all accents * @param neverSplit The set of tokens that should not be split */ - public BasicTokenizer(boolean isLowerCase, boolean isTokenizeCjkChars, boolean isStripAccents, - Set neverSplit) { + public BasicTokenizer(boolean isLowerCase, boolean isTokenizeCjkChars, boolean isStripAccents, Set neverSplit) { this.isLowerCase = isLowerCase; this.isTokenizeCjkChars = isTokenizeCjkChars; this.isStripAccents = isStripAccents; @@ -85,7 +84,7 @@ public List tokenize(String text) { text = tokenizeCjkChars(text); } - String [] tokens = whiteSpaceTokenize(text); + String[] tokens = whiteSpaceTokenize(text); List processedTokens = new ArrayList<>(tokens.length); for (String token : tokens) { @@ -102,7 +101,7 @@ public List tokenize(String text) { // At this point text has been tokenized by whitespace // but one of the special never split tokens could be adjacent // to one or more punctuation characters. - if (isCommonPunctuation(token.codePointAt(token.length() -1))) { + if (isCommonPunctuation(token.codePointAt(token.length() - 1))) { int lastNonPunctuationIndex = findLastNonPunctuationIndex(token); if (lastNonPunctuationIndex >= 0 && neverSplit.contains(token.substring(0, lastNonPunctuationIndex + 1))) { processedTokens.add(token.substring(0, lastNonPunctuationIndex + 1)); @@ -146,7 +145,7 @@ public boolean isTokenizeCjkChars() { return isTokenizeCjkChars; } - static String [] whiteSpaceTokenize(String text) { + static String[] whiteSpaceTokenize(String text) { text = text.trim(); return text.split(" "); } @@ -165,7 +164,7 @@ public boolean isTokenizeCjkChars() { static String stripAccents(String word) { String normalizedString = Normalizer.normalize(word, Normalizer.Form.NFD); - int [] codePoints = normalizedString.codePoints() + int[] codePoints = normalizedString.codePoints() .filter(codePoint -> Character.getType(codePoint) != Character.NON_SPACING_MARK) .toArray(); @@ -178,10 +177,10 @@ static List splitOnPunctuation(String word) { static List splitOnPredicate(String word, Predicate test) { List split = new ArrayList<>(); - int [] codePoints = word.codePoints().toArray(); + int[] codePoints = word.codePoints().toArray(); int lastSplit = 0; - for (int i=0; i 0) { @@ -189,7 +188,7 @@ static List splitOnPredicate(String word, Predicate test) { split.add(new String(codePoints, lastSplit, i - lastSplit)); } split.add(new String(codePoints, i, 1)); - lastSplit = i+1; + lastSplit = i + 1; } } @@ -234,7 +233,7 @@ static String tokenizeCjkChars(String text) { * @return Cleaned text */ static String cleanText(String text) { - int [] codePoints = text.codePoints() + int[] codePoints = text.codePoints() .filter(codePoint -> (codePoint == 0x00 || codePoint == 0xFFFD || isControlChar(codePoint)) == false) .map(codePoint -> isWhiteSpace(codePoint) ? ' ' : codePoint) .toArray(); @@ -245,14 +244,14 @@ static String cleanText(String text) { static boolean isCjkChar(int codePoint) { // https://en.wikipedia.org/wiki/CJK_Unified_Ideographs_(Unicode_block) Character.UnicodeBlock block = Character.UnicodeBlock.of(codePoint); - return Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS.equals(block) || - Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS.equals(block) || - Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A.equals(block) || - Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B.equals(block) || - Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_C.equals(block) || - Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_D.equals(block) || - Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_E.equals(block) || - Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS_SUPPLEMENT.equals(block); + return Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS.equals(block) + || Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS.equals(block) + || Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A.equals(block) + || Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B.equals(block) + || Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_C.equals(block) + || Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_D.equals(block) + || Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_E.equals(block) + || Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS_SUPPLEMENT.equals(block); } /** @@ -263,7 +262,7 @@ static boolean isCjkChar(int codePoint) { * @return is control char */ static boolean isControlChar(int codePoint) { - if (codePoint == '\n' || codePoint == '\r' || codePoint == '\t' ) { + if (codePoint == '\n' || codePoint == '\r' || codePoint == '\t') { return false; } int category = Character.getType(codePoint); @@ -280,7 +279,7 @@ static boolean isControlChar(int codePoint) { * @return is white space */ static boolean isWhiteSpace(int codePoint) { - if (codePoint == '\n' || codePoint == '\r' || codePoint == '\t' ) { + if (codePoint == '\n' || codePoint == '\r' || codePoint == '\t') { return true; } return Character.getType(codePoint) == Character.SPACE_SEPARATOR; @@ -295,10 +294,10 @@ static boolean isWhiteSpace(int codePoint) { * @return true if is punctuation */ static boolean isPunctuationMark(int codePoint) { - if ((codePoint >= 33 && codePoint <= 47) || - (codePoint >= 58 && codePoint <= 64) || - (codePoint >= 91 && codePoint <= 96) || - (codePoint >= 123 && codePoint <= 126)) { + if ((codePoint >= 33 && codePoint <= 47) + || (codePoint >= 58 && codePoint <= 64) + || (codePoint >= 91 && codePoint <= 96) + || (codePoint >= 123 && codePoint <= 126)) { return true; } @@ -314,8 +313,7 @@ static boolean isPunctuationMark(int codePoint) { * @return true if codepoint is punctuation */ static boolean isCommonPunctuation(int codePoint) { - if ((codePoint >= 33 && codePoint <= 47) || - (codePoint >= 58 && codePoint <= 64) ) { + if ((codePoint >= 33 && codePoint <= 47) || (codePoint >= 58 && codePoint <= 64)) { return true; } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/tokenizers/BertTokenizer.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/tokenizers/BertTokenizer.java index 56158feb6dd38..16bc124e6e44a 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/tokenizers/BertTokenizer.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/tokenizers/BertTokenizer.java @@ -42,7 +42,7 @@ public class BertTokenizer implements NlpTokenizer { public static final int DEFAULT_MAX_INPUT_CHARS_PER_WORD = 100; - private final Set NEVER_SPLIT = Set.of(MASK_TOKEN); + private final Set NEVER_SPLIT = Set.of(MASK_TOKEN); private final WordPieceTokenizer wordPieceTokenizer; private final List originalVocab; @@ -57,16 +57,18 @@ public class BertTokenizer implements NlpTokenizer { private final int maxSequenceLength; private final NlpTask.RequestBuilder requestBuilder; - protected BertTokenizer(List originalVocab, - SortedMap vocab, - boolean doLowerCase, - boolean doTokenizeCjKChars, - boolean doStripAccents, - boolean withSpecialTokens, - Tokenization.Truncate truncate, - int maxSequenceLength, - Function requestBuilderFactory, - Set neverSplit) { + protected BertTokenizer( + List originalVocab, + SortedMap vocab, + boolean doLowerCase, + boolean doTokenizeCjKChars, + boolean doStripAccents, + boolean withSpecialTokens, + Tokenization.Truncate truncate, + int maxSequenceLength, + Function requestBuilderFactory, + Set neverSplit + ) { wordPieceTokenizer = new WordPieceTokenizer(vocab, UNKNOWN_TOKEN, DEFAULT_MAX_INPUT_CHARS_PER_WORD); this.originalVocab = originalVocab; this.vocab = vocab; @@ -167,7 +169,7 @@ public TokenizationResult.Tokenization tokenize(String seq1, String seq2) { innerResult = innerTokenize(seq2); List wordPieceTokenSeq2s = innerResult.v1(); List tokenPositionMapSeq2 = innerResult.v2(); - if (withSpecialTokens == false) { + if (withSpecialTokens == false) { throw new IllegalArgumentException("Unable to do sequence pair tokenization without special tokens"); } // [CLS] seq1 [SEP] seq2 [SEP] @@ -178,9 +180,9 @@ public TokenizationResult.Tokenization tokenize(String seq1, String seq2) { case FIRST: if (wordPieceTokenSeq2s.size() > maxSequenceLength - 3) { throw ExceptionsHelper.badRequestException( - "Attempting truncation [{}] but input is too large for the second sequence. " + - "The tokenized input length [{}] exceeds the maximum sequence length [{}], " + - "when taking special tokens into account", + "Attempting truncation [{}] but input is too large for the second sequence. " + + "The tokenized input length [{}] exceeds the maximum sequence length [{}], " + + "when taking special tokens into account", truncate.toString(), wordPieceTokenSeq2s.size(), maxSequenceLength - 3 @@ -191,9 +193,9 @@ public TokenizationResult.Tokenization tokenize(String seq1, String seq2) { case SECOND: if (wordPieceTokenSeq1s.size() > maxSequenceLength - 3) { throw ExceptionsHelper.badRequestException( - "Attempting truncation [{}] but input is too large for the first sequence. " + - "The tokenized input length [{}] exceeds the maximum sequence length [{}], " + - "when taking special tokens into account", + "Attempting truncation [{}] but input is too large for the first sequence. " + + "The tokenized input length [{}] exceeds the maximum sequence length [{}], " + + "when taking special tokens into account", truncate.toString(), wordPieceTokenSeq2s.size(), maxSequenceLength - 3 diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/tokenizers/WordPieceTokenizer.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/tokenizers/WordPieceTokenizer.java index 23634d200a771..a566007a8c5ae 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/tokenizers/WordPieceTokenizer.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/tokenizers/WordPieceTokenizer.java @@ -110,7 +110,7 @@ public List tokenize(String text) { if (isBad) { output.add(new TokenAndId(unknownToken, vocab.get(unknownToken))); - } else { + } else { output.addAll(subTokens); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/tokenizers/WordPieceVocabulary.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/tokenizers/WordPieceVocabulary.java index c7f2320c67f49..39345fc9b2b9a 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/tokenizers/WordPieceVocabulary.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/nlp/tokenizers/WordPieceVocabulary.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.ml.inference.nlp.tokenizers; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -30,9 +30,11 @@ public class WordPieceVocabulary implements ToXContentObject { @SuppressWarnings("unchecked") private static ConstructingObjectParser createParser(boolean ignoreUnknownFields) { - ConstructingObjectParser parser = new ConstructingObjectParser<>(NAME, + ConstructingObjectParser parser = new ConstructingObjectParser<>( + NAME, ignoreUnknownFields, - a -> new WordPieceVocabulary((List) a[0], (Integer) a[1])); + a -> new WordPieceVocabulary((List) a[0], (Integer) a[1]) + ); parser.declareStringArray(ConstructingObjectParser.constructorArg(), VOCAB); parser.declareInt(ConstructingObjectParser.optionalConstructorArg(), UNKNOWN_TOKEN); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/persistence/ChunkedTrainedModelRestorer.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/persistence/ChunkedTrainedModelRestorer.java index 7063307294f4f..4417a1427a797 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/persistence/ChunkedTrainedModelRestorer.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/persistence/ChunkedTrainedModelRestorer.java @@ -16,17 +16,17 @@ import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchRequestBuilder; import org.elasticsearch.client.Client; -import org.elasticsearch.core.CheckedFunction; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.core.CheckedFunction; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.sort.SortBuilders; import org.elasticsearch.search.sort.SortOrder; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.ml.inference.TrainedModelConfig; import org.elasticsearch.xpack.core.ml.inference.persistence.InferenceIndexConstants; import org.elasticsearch.xpack.core.ml.job.messages.Messages; @@ -64,10 +64,12 @@ public class ChunkedTrainedModelRestorer { private int searchSize = 10; private int numDocsWritten = 0; - public ChunkedTrainedModelRestorer(String modelId, - Client client, - ExecutorService executorService, - NamedXContentRegistry xContentRegistry) { + public ChunkedTrainedModelRestorer( + String modelId, + Client client, + ExecutorService executorService, + NamedXContentRegistry xContentRegistry + ) { this.client = client; this.executorService = executorService; this.xContentRegistry = xContentRegistry; @@ -78,7 +80,7 @@ public void setSearchSize(int searchSize) { if (searchSize > MAX_NUM_DEFINITION_DOCS) { throw new IllegalArgumentException("search size [" + searchSize + "] cannot be bigger than [" + MAX_NUM_DEFINITION_DOCS + "]"); } - if (searchSize <=0) { + if (searchSize <= 0) { throw new IllegalArgumentException("search size [" + searchSize + "] must be greater than 0"); } this.searchSize = searchSize; @@ -111,9 +113,11 @@ public int getNumDocsWritten() { * @param successConsumer Called when all docs have been returned or the loading is cancelled * @param errorConsumer In the event of an error */ - public void restoreModelDefinition(CheckedFunction modelConsumer, - Consumer successConsumer, - Consumer errorConsumer) { + public void restoreModelDefinition( + CheckedFunction modelConsumer, + Consumer successConsumer, + Consumer errorConsumer + ) { logger.debug("[{}] restoring model", modelId); SearchRequest searchRequest = buildSearch(client, modelId, index, searchSize); @@ -121,101 +125,107 @@ public void restoreModelDefinition(CheckedFunction doSearch(searchRequest, modelConsumer, successConsumer, errorConsumer)); } - private void doSearch(SearchRequest searchRequest, - CheckedFunction modelConsumer, - Consumer successConsumer, - Consumer errorConsumer) { - - executeAsyncWithOrigin(client, ML_ORIGIN, SearchAction.INSTANCE, searchRequest, ActionListener.wrap( - searchResponse -> { - if (searchResponse.getHits().getHits().length == 0) { - errorConsumer.accept(new ResourceNotFoundException( - Messages.getMessage(Messages.MODEL_DEFINITION_NOT_FOUND, modelId))); - return; - } + private void doSearch( + SearchRequest searchRequest, + CheckedFunction modelConsumer, + Consumer successConsumer, + Consumer errorConsumer + ) { + + executeAsyncWithOrigin(client, ML_ORIGIN, SearchAction.INSTANCE, searchRequest, ActionListener.wrap(searchResponse -> { + if (searchResponse.getHits().getHits().length == 0) { + errorConsumer.accept(new ResourceNotFoundException(Messages.getMessage(Messages.MODEL_DEFINITION_NOT_FOUND, modelId))); + return; + } - // Set lastNum to a non-zero to prevent an infinite loop of - // search after requests in the absolute worse case where - // it has all gone wrong. - // Docs are numbered 0..N. we must have seen at least - // this many docs so far. - int lastNum = numDocsWritten -1; - for (SearchHit hit : searchResponse.getHits().getHits()) { - try { - TrainedModelDefinitionDoc doc = - parseModelDefinitionDocLenientlyFromSource(hit.getSourceRef(), modelId, xContentRegistry); - lastNum = doc.getDocNum(); - - boolean continueSearching = modelConsumer.apply(doc); - if (continueSearching == false) { - // signal the search has finished early - successConsumer.accept(Boolean.FALSE); - return; - } - - } catch (IOException e) { - logger.error(new ParameterizedMessage("[{}] error writing model definition", modelId), e); - errorConsumer.accept(e); + // Set lastNum to a non-zero to prevent an infinite loop of + // search after requests in the absolute worse case where + // it has all gone wrong. + // Docs are numbered 0..N. we must have seen at least + // this many docs so far. + int lastNum = numDocsWritten - 1; + for (SearchHit hit : searchResponse.getHits().getHits()) { + try { + TrainedModelDefinitionDoc doc = parseModelDefinitionDocLenientlyFromSource( + hit.getSourceRef(), + modelId, + xContentRegistry + ); + lastNum = doc.getDocNum(); + + boolean continueSearching = modelConsumer.apply(doc); + if (continueSearching == false) { + // signal the search has finished early + successConsumer.accept(Boolean.FALSE); return; } + + } catch (IOException e) { + logger.error(new ParameterizedMessage("[{}] error writing model definition", modelId), e); + errorConsumer.accept(e); + return; } + } - numDocsWritten += searchResponse.getHits().getHits().length; + numDocsWritten += searchResponse.getHits().getHits().length; - boolean endOfSearch = searchResponse.getHits().getHits().length < searchSize || - searchResponse.getHits().getTotalHits().value == numDocsWritten; + boolean endOfSearch = searchResponse.getHits().getHits().length < searchSize + || searchResponse.getHits().getTotalHits().value == numDocsWritten; - if (endOfSearch) { - successConsumer.accept(Boolean.TRUE); - } else { - // search again with after - SearchHit lastHit = searchResponse.getHits().getAt(searchResponse.getHits().getHits().length -1); - SearchRequestBuilder searchRequestBuilder = buildSearchBuilder(client, modelId, index, searchSize); - searchRequestBuilder.searchAfter(new Object[]{lastHit.getIndex(), lastNum}); - executorService.execute(() -> - doSearch(searchRequestBuilder.request(), modelConsumer, successConsumer, errorConsumer)); - } - }, - e -> { - if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) { - errorConsumer.accept(new ResourceNotFoundException( - Messages.getMessage(Messages.MODEL_DEFINITION_NOT_FOUND, modelId))); - } else { - errorConsumer.accept(e); - } + if (endOfSearch) { + successConsumer.accept(Boolean.TRUE); + } else { + // search again with after + SearchHit lastHit = searchResponse.getHits().getAt(searchResponse.getHits().getHits().length - 1); + SearchRequestBuilder searchRequestBuilder = buildSearchBuilder(client, modelId, index, searchSize); + searchRequestBuilder.searchAfter(new Object[] { lastHit.getIndex(), lastNum }); + executorService.execute(() -> doSearch(searchRequestBuilder.request(), modelConsumer, successConsumer, errorConsumer)); } - )); + }, e -> { + if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) { + errorConsumer.accept(new ResourceNotFoundException(Messages.getMessage(Messages.MODEL_DEFINITION_NOT_FOUND, modelId))); + } else { + errorConsumer.accept(e); + } + })); } private static SearchRequestBuilder buildSearchBuilder(Client client, String modelId, String index, int searchSize) { return client.prepareSearch(index) - .setQuery(QueryBuilders.constantScoreQuery(QueryBuilders - .boolQuery() - .filter(QueryBuilders.termQuery(TrainedModelConfig.MODEL_ID.getPreferredName(), modelId)) - .filter(QueryBuilders.termQuery(InferenceIndexConstants.DOC_TYPE.getPreferredName(), - TrainedModelDefinitionDoc.NAME)))) + .setQuery( + QueryBuilders.constantScoreQuery( + QueryBuilders.boolQuery() + .filter(QueryBuilders.termQuery(TrainedModelConfig.MODEL_ID.getPreferredName(), modelId)) + .filter( + QueryBuilders.termQuery(InferenceIndexConstants.DOC_TYPE.getPreferredName(), TrainedModelDefinitionDoc.NAME) + ) + ) + ) .setSize(searchSize) .setTrackTotalHits(true) // First find the latest index .addSort("_index", SortOrder.DESC) // Then, sort by doc_num - .addSort(SortBuilders.fieldSort(TrainedModelDefinitionDoc.DOC_NUM.getPreferredName()) - .order(SortOrder.ASC) - .unmappedType("long")); + .addSort( + SortBuilders.fieldSort(TrainedModelDefinitionDoc.DOC_NUM.getPreferredName()).order(SortOrder.ASC).unmappedType("long") + ); } public static SearchRequest buildSearch(Client client, String modelId, String index, int searchSize) { return buildSearchBuilder(client, modelId, index, searchSize).request(); } - public static TrainedModelDefinitionDoc parseModelDefinitionDocLenientlyFromSource(BytesReference source, - String modelId, - NamedXContentRegistry xContentRegistry) - throws IOException { - - try (InputStream stream = source.streamInput(); - XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, stream)) { + public static TrainedModelDefinitionDoc parseModelDefinitionDocLenientlyFromSource( + BytesReference source, + String modelId, + NamedXContentRegistry xContentRegistry + ) throws IOException { + + try ( + InputStream stream = source.streamInput(); + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, stream) + ) { return TrainedModelDefinitionDoc.fromXContent(parser, true).build(); } catch (IOException e) { logger.error(new ParameterizedMessage("[{}] failed to parse model definition", modelId), e); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/persistence/TrainedModelDefinitionDoc.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/persistence/TrainedModelDefinitionDoc.java index 02eb95323c76f..8c65495f4caed 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/persistence/TrainedModelDefinitionDoc.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/persistence/TrainedModelDefinitionDoc.java @@ -6,11 +6,11 @@ */ package org.elasticsearch.xpack.ml.inference.persistence; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -46,14 +46,20 @@ public class TrainedModelDefinitionDoc implements ToXContentObject { public static final ObjectParser STRICT_PARSER = createParser(false); private static ObjectParser createParser(boolean ignoreUnknownFields) { - ObjectParser parser = new ObjectParser<>(NAME, + ObjectParser parser = new ObjectParser<>( + NAME, ignoreUnknownFields, - TrainedModelDefinitionDoc.Builder::new); + TrainedModelDefinitionDoc.Builder::new + ); parser.declareString((a, b) -> {}, InferenceIndexConstants.DOC_TYPE); // type is hard coded but must be parsed parser.declareString(TrainedModelDefinitionDoc.Builder::setModelId, TrainedModelConfig.MODEL_ID); parser.declareString(TrainedModelDefinitionDoc.Builder::setCompressedString, DEFINITION); - parser.declareField(TrainedModelDefinitionDoc.Builder::setBinaryData, (p, c) -> new BytesArray(p.binaryValue()), - BINARY_DEFINITION, ObjectParser.ValueType.VALUE_OBJECT_ARRAY); + parser.declareField( + TrainedModelDefinitionDoc.Builder::setBinaryData, + (p, c) -> new BytesArray(p.binaryValue()), + BINARY_DEFINITION, + ObjectParser.ValueType.VALUE_OBJECT_ARRAY + ); parser.declareInt(TrainedModelDefinitionDoc.Builder::setDocNum, DOC_NUM); parser.declareInt(TrainedModelDefinitionDoc.Builder::setCompressionVersion, COMPRESSION_VERSION); parser.declareLong(TrainedModelDefinitionDoc.Builder::setDefinitionLength, DEFINITION_LENGTH); @@ -80,13 +86,15 @@ public static String docId(String modelId, int docNum) { private final int compressionVersion; private final boolean eos; - private TrainedModelDefinitionDoc(BytesReference binaryData, - String modelId, - int docNum, - Long totalDefinitionLength, - long definitionLength, - int compressionVersion, - boolean eos) { + private TrainedModelDefinitionDoc( + BytesReference binaryData, + String modelId, + int docNum, + Long totalDefinitionLength, + long definitionLength, + int compressionVersion, + boolean eos + ) { this.binaryData = ExceptionsHelper.requireNonNull(binaryData, BINARY_DEFINITION); this.modelId = ExceptionsHelper.requireNonNull(modelId, TrainedModelConfig.MODEL_ID); if (docNum < 0) { @@ -164,13 +172,13 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; TrainedModelDefinitionDoc that = (TrainedModelDefinitionDoc) o; - return Objects.equals(modelId, that.modelId) && - Objects.equals(docNum, that.docNum) && - Objects.equals(definitionLength, that.definitionLength) && - Objects.equals(totalDefinitionLength, that.totalDefinitionLength) && - Objects.equals(compressionVersion, that.compressionVersion) && - Objects.equals(eos, that.eos) && - Objects.equals(binaryData, that.binaryData); + return Objects.equals(modelId, that.modelId) + && Objects.equals(docNum, that.docNum) + && Objects.equals(definitionLength, that.definitionLength) + && Objects.equals(totalDefinitionLength, that.totalDefinitionLength) + && Objects.equals(compressionVersion, that.compressionVersion) + && Objects.equals(eos, that.eos) + && Objects.equals(binaryData, that.binaryData); } @Override @@ -194,8 +202,7 @@ public Builder setModelId(String modelId) { } public Builder setCompressedString(String compressedString) { - this.binaryData = new BytesArray(Base64.getDecoder() - .decode(compressedString.getBytes(StandardCharsets.UTF_8))); + this.binaryData = new BytesArray(Base64.getDecoder().decode(compressedString.getBytes(StandardCharsets.UTF_8))); return this; } @@ -237,7 +244,8 @@ public TrainedModelDefinitionDoc build() { this.totalDefinitionLength, this.definitionLength, this.compressionVersion, - this.eos); + this.eos + ); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/persistence/TrainedModelProvider.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/persistence/TrainedModelProvider.java index b796760c5c5c1..e5360776ffdd8 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/persistence/TrainedModelProvider.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/persistence/TrainedModelProvider.java @@ -39,18 +39,10 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.CompositeBytesReference; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.regex.Regex; import org.elasticsearch.common.util.set.Sets; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.ToXContentObject; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.xcontent.json.JsonXContent; +import org.elasticsearch.core.Tuple; import org.elasticsearch.index.IndexNotFoundException; import org.elasticsearch.index.engine.VersionConflictEngineException; import org.elasticsearch.index.query.BoolQueryBuilder; @@ -66,6 +58,14 @@ import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.sort.SortBuilders; import org.elasticsearch.search.sort.SortOrder; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.ToXContentObject; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.action.util.ExpandedIdsMatcher; import org.elasticsearch.xpack.core.action.util.PageParams; import org.elasticsearch.xpack.core.ml.MlStatsIndex; @@ -122,19 +122,22 @@ public class TrainedModelProvider { private static final Logger logger = LogManager.getLogger(TrainedModelProvider.class); private final Client client; private final NamedXContentRegistry xContentRegistry; - private static final ToXContent.Params FOR_INTERNAL_STORAGE_PARAMS = - new ToXContent.MapParams(Collections.singletonMap(ToXContentParams.FOR_INTERNAL_STORAGE, "true")); + private static final ToXContent.Params FOR_INTERNAL_STORAGE_PARAMS = new ToXContent.MapParams( + Collections.singletonMap(ToXContentParams.FOR_INTERNAL_STORAGE, "true") + ); public TrainedModelProvider(Client client, NamedXContentRegistry xContentRegistry) { this.client = client; this.xContentRegistry = xContentRegistry; } - public void storeTrainedModel(TrainedModelConfig trainedModelConfig, - ActionListener listener) { + public void storeTrainedModel(TrainedModelConfig trainedModelConfig, ActionListener listener) { if (MODELS_STORED_AS_RESOURCE.contains(trainedModelConfig.getModelId())) { - listener.onFailure(new ResourceAlreadyExistsException( - Messages.getMessage(Messages.INFERENCE_TRAINED_MODEL_EXISTS, trainedModelConfig.getModelId()))); + listener.onFailure( + new ResourceAlreadyExistsException( + Messages.getMessage(Messages.INFERENCE_TRAINED_MODEL_EXISTS, trainedModelConfig.getModelId()) + ) + ); return; } @@ -142,18 +145,25 @@ public void storeTrainedModel(TrainedModelConfig trainedModelConfig, try { definition = trainedModelConfig.getCompressedDefinition(); } catch (IOException ex) { - listener.onFailure(ExceptionsHelper.serverError( - "Unexpected IOException while serializing definition for storage for model [{}]", - ex, - trainedModelConfig.getModelId())); + listener.onFailure( + ExceptionsHelper.serverError( + "Unexpected IOException while serializing definition for storage for model [{}]", + ex, + trainedModelConfig.getModelId() + ) + ); return; } TrainedModelLocation location = trainedModelConfig.getLocation(); if (definition == null && location == null) { - listener.onFailure(ExceptionsHelper.badRequestException("Unable to store [{}]. [{}] or [{}] is required", - trainedModelConfig.getModelId(), - TrainedModelConfig.DEFINITION.getPreferredName(), - TrainedModelConfig.LOCATION.getPreferredName())); + listener.onFailure( + ExceptionsHelper.badRequestException( + "Unable to store [{}]. [{}] or [{}] is required", + trainedModelConfig.getModelId(), + TrainedModelConfig.DEFINITION.getPreferredName(), + TrainedModelConfig.LOCATION.getPreferredName() + ) + ); return; } @@ -166,34 +176,45 @@ public void storeTrainedModel(TrainedModelConfig trainedModelConfig, public void storeTrainedModelConfig(TrainedModelConfig trainedModelConfig, ActionListener listener) { if (MODELS_STORED_AS_RESOURCE.contains(trainedModelConfig.getModelId())) { - listener.onFailure(new ResourceAlreadyExistsException( - Messages.getMessage(Messages.INFERENCE_TRAINED_MODEL_EXISTS, trainedModelConfig.getModelId()))); + listener.onFailure( + new ResourceAlreadyExistsException( + Messages.getMessage(Messages.INFERENCE_TRAINED_MODEL_EXISTS, trainedModelConfig.getModelId()) + ) + ); return; } assert trainedModelConfig.getModelDefinition() == null; - IndexRequest request = - createRequest(trainedModelConfig.getModelId(), InferenceIndexConstants.LATEST_INDEX_NAME, trainedModelConfig); + IndexRequest request = createRequest( + trainedModelConfig.getModelId(), + InferenceIndexConstants.LATEST_INDEX_NAME, + trainedModelConfig + ); request.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - executeAsyncWithOrigin(client, + executeAsyncWithOrigin( + client, ML_ORIGIN, IndexAction.INSTANCE, request, - ActionListener.wrap( - indexResponse -> listener.onResponse(true), - e -> { - if (ExceptionsHelper.unwrapCause(e) instanceof VersionConflictEngineException) { - listener.onFailure(new ResourceAlreadyExistsException( - Messages.getMessage(Messages.INFERENCE_TRAINED_MODEL_EXISTS, trainedModelConfig.getModelId()))); - } else { - listener.onFailure( - new ElasticsearchStatusException( - Messages.getMessage(Messages.INFERENCE_FAILED_TO_STORE_MODEL, trainedModelConfig.getModelId()), - RestStatus.INTERNAL_SERVER_ERROR, e)); - } + ActionListener.wrap(indexResponse -> listener.onResponse(true), e -> { + if (ExceptionsHelper.unwrapCause(e) instanceof VersionConflictEngineException) { + listener.onFailure( + new ResourceAlreadyExistsException( + Messages.getMessage(Messages.INFERENCE_TRAINED_MODEL_EXISTS, trainedModelConfig.getModelId()) + ) + ); + } else { + listener.onFailure( + new ElasticsearchStatusException( + Messages.getMessage(Messages.INFERENCE_FAILED_TO_STORE_MODEL, trainedModelConfig.getModelId()), + RestStatus.INTERNAL_SERVER_ERROR, + e + ) + ); } - )); + }) + ); } public void storeTrainedModelDefinitionDoc(TrainedModelDefinitionDoc trainedModelDefinitionDoc, ActionListener listener) { @@ -207,33 +228,31 @@ public void storeTrainedModelVocabulary( ActionListener listener ) { if (MODELS_STORED_AS_RESOURCE.contains(modelId)) { - listener.onFailure(new ResourceAlreadyExistsException( - Messages.getMessage(Messages.INFERENCE_TRAINED_MODEL_EXISTS, modelId))); + listener.onFailure(new ResourceAlreadyExistsException(Messages.getMessage(Messages.INFERENCE_TRAINED_MODEL_EXISTS, modelId))); return; } - executeAsyncWithOrigin(client, + executeAsyncWithOrigin( + client, ML_ORIGIN, IndexAction.INSTANCE, - createRequest(VocabularyConfig.docId(modelId), vocabularyConfig.getIndex(), vocabulary) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), - ActionListener.wrap( - indexResponse -> listener.onResponse(null), - e -> { - if (ExceptionsHelper.unwrapCause(e) instanceof VersionConflictEngineException) { - listener.onFailure(new ResourceAlreadyExistsException( - Messages.getMessage(Messages.INFERENCE_TRAINED_MODEL_VOCAB_EXISTS, modelId)) - ); - } else { - listener.onFailure( - new ElasticsearchStatusException( - Messages.getMessage(Messages.INFERENCE_FAILED_TO_STORE_MODEL_VOCAB, modelId), - RestStatus.INTERNAL_SERVER_ERROR, - e - ) - ); - } + createRequest(VocabularyConfig.docId(modelId), vocabularyConfig.getIndex(), vocabulary).setRefreshPolicy( + WriteRequest.RefreshPolicy.IMMEDIATE + ), + ActionListener.wrap(indexResponse -> listener.onResponse(null), e -> { + if (ExceptionsHelper.unwrapCause(e) instanceof VersionConflictEngineException) { + listener.onFailure( + new ResourceAlreadyExistsException(Messages.getMessage(Messages.INFERENCE_TRAINED_MODEL_VOCAB_EXISTS, modelId)) + ); + } else { + listener.onFailure( + new ElasticsearchStatusException( + Messages.getMessage(Messages.INFERENCE_FAILED_TO_STORE_MODEL_VOCAB, modelId), + RestStatus.INTERNAL_SERVER_ERROR, + e + ) + ); } - ) + }) ); } @@ -243,114 +262,125 @@ public void storeTrainedModelDefinitionDoc( ActionListener listener ) { if (MODELS_STORED_AS_RESOURCE.contains(trainedModelDefinitionDoc.getModelId())) { - listener.onFailure(new ResourceAlreadyExistsException( - Messages.getMessage(Messages.INFERENCE_TRAINED_MODEL_EXISTS, trainedModelDefinitionDoc.getModelId()))); + listener.onFailure( + new ResourceAlreadyExistsException( + Messages.getMessage(Messages.INFERENCE_TRAINED_MODEL_EXISTS, trainedModelDefinitionDoc.getModelId()) + ) + ); return; } - executeAsyncWithOrigin(client, + executeAsyncWithOrigin( + client, ML_ORIGIN, IndexAction.INSTANCE, createRequest(trainedModelDefinitionDoc.getDocId(), index, trainedModelDefinitionDoc), - ActionListener.wrap( - indexResponse -> listener.onResponse(null), - e -> { - if (ExceptionsHelper.unwrapCause(e) instanceof VersionConflictEngineException) { - listener.onFailure(new ResourceAlreadyExistsException( - Messages.getMessage(Messages.INFERENCE_TRAINED_MODEL_DOC_EXISTS, + ActionListener.wrap(indexResponse -> listener.onResponse(null), e -> { + if (ExceptionsHelper.unwrapCause(e) instanceof VersionConflictEngineException) { + listener.onFailure( + new ResourceAlreadyExistsException( + Messages.getMessage( + Messages.INFERENCE_TRAINED_MODEL_DOC_EXISTS, trainedModelDefinitionDoc.getModelId(), - trainedModelDefinitionDoc.getDocNum()))); - } else { - listener.onFailure( - new ElasticsearchStatusException( - Messages.getMessage( - Messages.INFERENCE_FAILED_TO_STORE_MODEL_DEFINITION, - trainedModelDefinitionDoc.getModelId(), - trainedModelDefinitionDoc.getDocNum() - ), - RestStatus.INTERNAL_SERVER_ERROR, - e + trainedModelDefinitionDoc.getDocNum() ) - ); - } + ) + ); + } else { + listener.onFailure( + new ElasticsearchStatusException( + Messages.getMessage( + Messages.INFERENCE_FAILED_TO_STORE_MODEL_DEFINITION, + trainedModelDefinitionDoc.getModelId(), + trainedModelDefinitionDoc.getDocNum() + ), + RestStatus.INTERNAL_SERVER_ERROR, + e + ) + ); } - ) + }) ); } public void storeTrainedModelMetadata(TrainedModelMetadata trainedModelMetadata, ActionListener listener) { if (MODELS_STORED_AS_RESOURCE.contains(trainedModelMetadata.getModelId())) { - listener.onFailure(new ResourceAlreadyExistsException( - Messages.getMessage(Messages.INFERENCE_TRAINED_MODEL_EXISTS, trainedModelMetadata.getModelId()))); + listener.onFailure( + new ResourceAlreadyExistsException( + Messages.getMessage(Messages.INFERENCE_TRAINED_MODEL_EXISTS, trainedModelMetadata.getModelId()) + ) + ); return; } - executeAsyncWithOrigin(client, + executeAsyncWithOrigin( + client, ML_ORIGIN, IndexAction.INSTANCE, createRequest(trainedModelMetadata.getDocId(), InferenceIndexConstants.LATEST_INDEX_NAME, trainedModelMetadata), - ActionListener.wrap( - indexResponse -> listener.onResponse(null), - e -> { - if (ExceptionsHelper.unwrapCause(e) instanceof VersionConflictEngineException) { - listener.onFailure(new ResourceAlreadyExistsException( - Messages.getMessage(Messages.INFERENCE_TRAINED_MODEL_METADATA_EXISTS, - trainedModelMetadata.getModelId()))); - } else { - listener.onFailure( - new ElasticsearchStatusException( - Messages.getMessage(Messages.INFERENCE_FAILED_TO_STORE_MODEL_METADATA, trainedModelMetadata.getModelId()), - RestStatus.INTERNAL_SERVER_ERROR, e)); - } + ActionListener.wrap(indexResponse -> listener.onResponse(null), e -> { + if (ExceptionsHelper.unwrapCause(e) instanceof VersionConflictEngineException) { + listener.onFailure( + new ResourceAlreadyExistsException( + Messages.getMessage(Messages.INFERENCE_TRAINED_MODEL_METADATA_EXISTS, trainedModelMetadata.getModelId()) + ) + ); + } else { + listener.onFailure( + new ElasticsearchStatusException( + Messages.getMessage(Messages.INFERENCE_FAILED_TO_STORE_MODEL_METADATA, trainedModelMetadata.getModelId()), + RestStatus.INTERNAL_SERVER_ERROR, + e + ) + ); } - )); + }) + ); } public void getTrainedModelMetadata(Collection modelIds, ActionListener> listener) { SearchRequest searchRequest = client.prepareSearch(InferenceIndexConstants.INDEX_PATTERN) - .setQuery(QueryBuilders.constantScoreQuery(QueryBuilders - .boolQuery() - .filter(QueryBuilders.termsQuery(TrainedModelConfig.MODEL_ID.getPreferredName(), modelIds)) - .filter(QueryBuilders.termQuery(InferenceIndexConstants.DOC_TYPE.getPreferredName(), - TrainedModelMetadata.NAME)))) + .setQuery( + QueryBuilders.constantScoreQuery( + QueryBuilders.boolQuery() + .filter(QueryBuilders.termsQuery(TrainedModelConfig.MODEL_ID.getPreferredName(), modelIds)) + .filter(QueryBuilders.termQuery(InferenceIndexConstants.DOC_TYPE.getPreferredName(), TrainedModelMetadata.NAME)) + ) + ) .setSize(10_000) // First find the latest index .addSort("_index", SortOrder.DESC) .request(); - executeAsyncWithOrigin(client, ML_ORIGIN, SearchAction.INSTANCE, searchRequest, ActionListener.wrap( - searchResponse -> { - if (searchResponse.getHits().getHits().length == 0) { - listener.onFailure(new ResourceNotFoundException( - Messages.getMessage(Messages.MODEL_METADATA_NOT_FOUND, modelIds))); - return; - } - HashMap map = new HashMap<>(); - for (SearchHit hit : searchResponse.getHits().getHits()) { - String modelId = TrainedModelMetadata.modelId(Objects.requireNonNull(hit.getId())); - map.putIfAbsent(modelId, parseMetadataLenientlyFromSource(hit.getSourceRef(), modelId)); - } - listener.onResponse(map); - }, - e -> { - if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) { - listener.onFailure(new ResourceNotFoundException( - Messages.getMessage(Messages.MODEL_METADATA_NOT_FOUND, modelIds))); - return; - } - listener.onFailure(e); + executeAsyncWithOrigin(client, ML_ORIGIN, SearchAction.INSTANCE, searchRequest, ActionListener.wrap(searchResponse -> { + if (searchResponse.getHits().getHits().length == 0) { + listener.onFailure(new ResourceNotFoundException(Messages.getMessage(Messages.MODEL_METADATA_NOT_FOUND, modelIds))); + return; + } + HashMap map = new HashMap<>(); + for (SearchHit hit : searchResponse.getHits().getHits()) { + String modelId = TrainedModelMetadata.modelId(Objects.requireNonNull(hit.getId())); + map.putIfAbsent(modelId, parseMetadataLenientlyFromSource(hit.getSourceRef(), modelId)); + } + listener.onResponse(map); + }, e -> { + if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) { + listener.onFailure(new ResourceNotFoundException(Messages.getMessage(Messages.MODEL_METADATA_NOT_FOUND, modelIds))); + return; } - )); + listener.onFailure(e); + })); } public void refreshInferenceIndex(ActionListener listener) { - executeAsyncWithOrigin(client, + executeAsyncWithOrigin( + client, ML_ORIGIN, RefreshAction.INSTANCE, new RefreshRequest(InferenceIndexConstants.INDEX_PATTERN), - listener); + listener + ); } - private void storeTrainedModelAndDefinition(TrainedModelConfig trainedModelConfig, - ActionListener listener) { + private void storeTrainedModelAndDefinition(TrainedModelConfig trainedModelConfig, ActionListener listener) { List trainedModelDefinitionDocs = new ArrayList<>(); try { @@ -360,80 +390,92 @@ private void storeTrainedModelAndDefinition(TrainedModelConfig trainedModelConfi ExceptionsHelper.badRequestException( "Unable to store model as compressed definition of size [{}] bytes the limit is [{}] bytes", compressedDefinition.length(), - MAX_COMPRESSED_MODEL_SIZE)); + MAX_COMPRESSED_MODEL_SIZE + ) + ); return; } List chunkedDefinition = chunkDefinitionWithSize(compressedDefinition, COMPRESSED_MODEL_CHUNK_SIZE); - for(int i = 0; i < chunkedDefinition.size(); ++i) { - trainedModelDefinitionDocs.add(new TrainedModelDefinitionDoc.Builder() - .setDocNum(i) - .setModelId(trainedModelConfig.getModelId()) - .setBinaryData(chunkedDefinition.get(i)) - .setCompressionVersion(TrainedModelConfig.CURRENT_DEFINITION_COMPRESSION_VERSION) - .setDefinitionLength(chunkedDefinition.get(i).length()) - // If it is the last doc, it is the EOS - .setEos(i == chunkedDefinition.size() - 1) - .build()); + for (int i = 0; i < chunkedDefinition.size(); ++i) { + trainedModelDefinitionDocs.add( + new TrainedModelDefinitionDoc.Builder().setDocNum(i) + .setModelId(trainedModelConfig.getModelId()) + .setBinaryData(chunkedDefinition.get(i)) + .setCompressionVersion(TrainedModelConfig.CURRENT_DEFINITION_COMPRESSION_VERSION) + .setDefinitionLength(chunkedDefinition.get(i).length()) + // If it is the last doc, it is the EOS + .setEos(i == chunkedDefinition.size() - 1) + .build() + ); } } catch (IOException ex) { - listener.onFailure(ExceptionsHelper.serverError( - "Unexpected IOException while serializing definition for storage for model [{}]", - ex, - trainedModelConfig.getModelId())); + listener.onFailure( + ExceptionsHelper.serverError( + "Unexpected IOException while serializing definition for storage for model [{}]", + ex, + trainedModelConfig.getModelId() + ) + ); return; } BulkRequestBuilder bulkRequest = client.prepareBulk(InferenceIndexConstants.LATEST_INDEX_NAME) .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) .add(createRequest(trainedModelConfig.getModelId(), trainedModelConfig)); - trainedModelDefinitionDocs.forEach(defDoc -> - bulkRequest.add(createRequest(TrainedModelDefinitionDoc.docId(trainedModelConfig.getModelId(), defDoc.getDocNum()), defDoc))); + trainedModelDefinitionDocs.forEach( + defDoc -> bulkRequest.add( + createRequest(TrainedModelDefinitionDoc.docId(trainedModelConfig.getModelId(), defDoc.getDocNum()), defDoc) + ) + ); - ActionListener wrappedListener = ActionListener.wrap( - listener::onResponse, - e -> { - if (ExceptionsHelper.unwrapCause(e) instanceof VersionConflictEngineException) { - listener.onFailure(new ResourceAlreadyExistsException( - Messages.getMessage(Messages.INFERENCE_TRAINED_MODEL_EXISTS, trainedModelConfig.getModelId()))); - } else { - listener.onFailure( - new ElasticsearchStatusException( - Messages.getMessage(Messages.INFERENCE_FAILED_TO_STORE_MODEL, trainedModelConfig.getModelId()), - RestStatus.INTERNAL_SERVER_ERROR, e)); - } + ActionListener wrappedListener = ActionListener.wrap(listener::onResponse, e -> { + if (ExceptionsHelper.unwrapCause(e) instanceof VersionConflictEngineException) { + listener.onFailure( + new ResourceAlreadyExistsException( + Messages.getMessage(Messages.INFERENCE_TRAINED_MODEL_EXISTS, trainedModelConfig.getModelId()) + ) + ); + } else { + listener.onFailure( + new ElasticsearchStatusException( + Messages.getMessage(Messages.INFERENCE_FAILED_TO_STORE_MODEL, trainedModelConfig.getModelId()), + RestStatus.INTERNAL_SERVER_ERROR, + e + ) + ); } - ); + }); - ActionListener bulkResponseActionListener = ActionListener.wrap( - r -> { - assert r.getItems().length == trainedModelDefinitionDocs.size() + 1; - if (r.getItems()[0].isFailed()) { - logger.error(new ParameterizedMessage( - "[{}] failed to store trained model config for inference", - trainedModelConfig.getModelId()), - r.getItems()[0].getFailure().getCause()); + ActionListener bulkResponseActionListener = ActionListener.wrap(r -> { + assert r.getItems().length == trainedModelDefinitionDocs.size() + 1; + if (r.getItems()[0].isFailed()) { + logger.error( + new ParameterizedMessage("[{}] failed to store trained model config for inference", trainedModelConfig.getModelId()), + r.getItems()[0].getFailure().getCause() + ); - wrappedListener.onFailure(r.getItems()[0].getFailure().getCause()); - return; - } - if (r.hasFailures()) { - Exception firstFailure = Arrays.stream(r.getItems()) - .filter(BulkItemResponse::isFailed) - .map(BulkItemResponse::getFailure) - .map(BulkItemResponse.Failure::getCause) - .findFirst() - .orElse(new Exception("unknown failure")); - logger.error(new ParameterizedMessage( - "[{}] failed to store trained model definition for inference", - trainedModelConfig.getModelId()), - firstFailure); - wrappedListener.onFailure(firstFailure); - return; - } - wrappedListener.onResponse(true); - }, - wrappedListener::onFailure - ); + wrappedListener.onFailure(r.getItems()[0].getFailure().getCause()); + return; + } + if (r.hasFailures()) { + Exception firstFailure = Arrays.stream(r.getItems()) + .filter(BulkItemResponse::isFailed) + .map(BulkItemResponse::getFailure) + .map(BulkItemResponse.Failure::getCause) + .findFirst() + .orElse(new Exception("unknown failure")); + logger.error( + new ParameterizedMessage( + "[{}] failed to store trained model definition for inference", + trainedModelConfig.getModelId() + ), + firstFailure + ); + wrappedListener.onFailure(firstFailure); + return; + } + wrappedListener.onResponse(true); + }, wrappedListener::onFailure); executeAsyncWithOrigin(client, ML_ORIGIN, BulkAction.INSTANCE, bulkRequest.request(), bulkResponseActionListener); } @@ -454,63 +496,66 @@ public void getTrainedModelForInference(final String modelId, boolean unsafe, fi // TODO Change this when we get more than just langIdent stored if (MODELS_STORED_AS_RESOURCE.contains(modelId)) { try { - TrainedModelConfig config = loadModelFromResource(modelId, false) - .build() - .ensureParsedDefinitionUnsafe(xContentRegistry); + TrainedModelConfig config = loadModelFromResource(modelId, false).build().ensureParsedDefinitionUnsafe(xContentRegistry); assert config.getModelDefinition().getTrainedModel() instanceof LangIdentNeuralNetwork; assert config.getModelType() == TrainedModelType.LANG_IDENT; listener.onResponse( InferenceDefinition.builder() .setPreProcessors(config.getModelDefinition().getPreProcessors()) - .setTrainedModel((LangIdentNeuralNetwork)config.getModelDefinition().getTrainedModel()) - .build()); + .setTrainedModel((LangIdentNeuralNetwork) config.getModelDefinition().getTrainedModel()) + .build() + ); return; - } catch (ElasticsearchException|IOException ex) { + } catch (ElasticsearchException | IOException ex) { listener.onFailure(ex); return; } } List docs = new ArrayList<>(); - ChunkedTrainedModelRestorer modelRestorer = - new ChunkedTrainedModelRestorer(modelId, client, - client.threadPool().executor(MachineLearning.UTILITY_THREAD_POOL_NAME), xContentRegistry); + ChunkedTrainedModelRestorer modelRestorer = new ChunkedTrainedModelRestorer( + modelId, + client, + client.threadPool().executor(MachineLearning.UTILITY_THREAD_POOL_NAME), + xContentRegistry + ); // TODO how could we stream in the model definition WHILE parsing it? // This would reduce the overall memory usage as we won't have to load the whole compressed string // XContentParser supports streams. - modelRestorer.restoreModelDefinition(docs::add, - success -> { - try { - BytesReference compressedData = getDefinitionFromDocs(docs, modelId); - InferenceDefinition inferenceDefinition = unsafe ? - InferenceToXContentCompressor.inflateUnsafe(compressedData, InferenceDefinition::fromXContent, xContentRegistry) : - InferenceToXContentCompressor.inflate(compressedData, InferenceDefinition::fromXContent, xContentRegistry); - - listener.onResponse(inferenceDefinition); - } catch (Exception e) { - listener.onFailure(e); - } - }, - e -> { - if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) { - listener.onFailure(new ResourceNotFoundException( - Messages.getMessage(Messages.MODEL_DEFINITION_NOT_FOUND, modelId))); - } + modelRestorer.restoreModelDefinition(docs::add, success -> { + try { + BytesReference compressedData = getDefinitionFromDocs(docs, modelId); + InferenceDefinition inferenceDefinition = unsafe + ? InferenceToXContentCompressor.inflateUnsafe(compressedData, InferenceDefinition::fromXContent, xContentRegistry) + : InferenceToXContentCompressor.inflate(compressedData, InferenceDefinition::fromXContent, xContentRegistry); + + listener.onResponse(inferenceDefinition); + } catch (Exception e) { listener.onFailure(e); - }); + } + }, e -> { + if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) { + listener.onFailure(new ResourceNotFoundException(Messages.getMessage(Messages.MODEL_DEFINITION_NOT_FOUND, modelId))); + } + listener.onFailure(e); + }); } - public void getTrainedModel(final String modelId, - final GetTrainedModelsAction.Includes includes, - final ActionListener finalListener) { + public void getTrainedModel( + final String modelId, + final GetTrainedModelsAction.Includes includes, + final ActionListener finalListener + ) { getTrainedModel(modelId, Collections.emptySet(), includes, finalListener); } - public void getTrainedModel(final String modelId, - final Set modelAliases, - final GetTrainedModelsAction.Includes includes, - final ActionListener finalListener) { + public void getTrainedModel( + final String modelId, + final Set modelAliases, + final GetTrainedModelsAction.Includes includes, + final ActionListener finalListener + ) { if (MODELS_STORED_AS_RESOURCE.contains(modelId)) { try { @@ -522,115 +567,118 @@ public void getTrainedModel(final String modelId, } } - ActionListener getTrainedModelListener = ActionListener.wrap( - modelBuilder -> { - modelBuilder.setModelAliases(modelAliases); - if ((includes.isIncludeFeatureImportanceBaseline() || includes.isIncludeTotalFeatureImportance() - || includes.isIncludeHyperparameters()) == false) { + ActionListener getTrainedModelListener = ActionListener.wrap(modelBuilder -> { + modelBuilder.setModelAliases(modelAliases); + if ((includes.isIncludeFeatureImportanceBaseline() + || includes.isIncludeTotalFeatureImportance() + || includes.isIncludeHyperparameters()) == false) { + finalListener.onResponse(modelBuilder.build()); + return; + } + this.getTrainedModelMetadata(Collections.singletonList(modelId), ActionListener.wrap(metadata -> { + TrainedModelMetadata modelMetadata = metadata.get(modelId); + if (modelMetadata != null) { + if (includes.isIncludeTotalFeatureImportance()) { + modelBuilder.setFeatureImportance(modelMetadata.getTotalFeatureImportances()); + } + if (includes.isIncludeFeatureImportanceBaseline()) { + modelBuilder.setBaselineFeatureImportance(modelMetadata.getFeatureImportanceBaselines()); + } + if (includes.isIncludeHyperparameters()) { + modelBuilder.setHyperparameters(modelMetadata.getHyperparameters()); + } + } + finalListener.onResponse(modelBuilder.build()); + }, failure -> { + // total feature importance is not necessary for a model to be valid + // we shouldn't fail if it is not found + if (ExceptionsHelper.unwrapCause(failure) instanceof ResourceNotFoundException) { finalListener.onResponse(modelBuilder.build()); return; } - this.getTrainedModelMetadata(Collections.singletonList(modelId), ActionListener.wrap( - metadata -> { - TrainedModelMetadata modelMetadata = metadata.get(modelId); - if (modelMetadata != null) { - if (includes.isIncludeTotalFeatureImportance()) { - modelBuilder.setFeatureImportance(modelMetadata.getTotalFeatureImportances()); - } - if (includes.isIncludeFeatureImportanceBaseline()) { - modelBuilder.setBaselineFeatureImportance(modelMetadata.getFeatureImportanceBaselines()); - } - if (includes.isIncludeHyperparameters()) { - modelBuilder.setHyperparameters(modelMetadata.getHyperparameters()); - } - } - finalListener.onResponse(modelBuilder.build()); - }, - failure -> { - // total feature importance is not necessary for a model to be valid - // we shouldn't fail if it is not found - if (ExceptionsHelper.unwrapCause(failure) instanceof ResourceNotFoundException) { - finalListener.onResponse(modelBuilder.build()); - return; - } - finalListener.onFailure(failure); - } - )); + finalListener.onFailure(failure); + })); - }, - finalListener::onFailure - ); + }, finalListener::onFailure); - QueryBuilder queryBuilder = QueryBuilders.constantScoreQuery(QueryBuilders - .idsQuery() - .addIds(modelId)); + QueryBuilder queryBuilder = QueryBuilders.constantScoreQuery(QueryBuilders.idsQuery().addIds(modelId)); MultiSearchRequestBuilder multiSearchRequestBuilder = client.prepareMultiSearch() - .add(client.prepareSearch(InferenceIndexConstants.INDEX_PATTERN) - .setQuery(queryBuilder) - // use sort to get the last - .addSort("_index", SortOrder.DESC) - .setSize(1) - .request()); + .add( + client.prepareSearch(InferenceIndexConstants.INDEX_PATTERN) + .setQuery(queryBuilder) + // use sort to get the last + .addSort("_index", SortOrder.DESC) + .setSize(1) + .request() + ); if (includes.isIncludeModelDefinition()) { multiSearchRequestBuilder.add( - ChunkedTrainedModelRestorer.buildSearch(client, modelId, InferenceIndexConstants.INDEX_PATTERN, MAX_NUM_DEFINITION_DOCS)); + ChunkedTrainedModelRestorer.buildSearch(client, modelId, InferenceIndexConstants.INDEX_PATTERN, MAX_NUM_DEFINITION_DOCS) + ); } - ActionListener multiSearchResponseActionListener = ActionListener.wrap( - multiSearchResponse -> { - TrainedModelConfig.Builder builder; + ActionListener multiSearchResponseActionListener = ActionListener.wrap(multiSearchResponse -> { + TrainedModelConfig.Builder builder; + try { + builder = handleSearchItem(multiSearchResponse.getResponses()[0], modelId, this::parseModelConfigLenientlyFromSource); + } catch (ResourceNotFoundException ex) { + getTrainedModelListener.onFailure( + new ResourceNotFoundException(Messages.getMessage(Messages.INFERENCE_NOT_FOUND, modelId)) + ); + return; + } catch (Exception ex) { + getTrainedModelListener.onFailure(ex); + return; + } + + if (includes.isIncludeModelDefinition()) { try { - builder = handleSearchItem(multiSearchResponse.getResponses()[0], modelId, this::parseModelConfigLenientlyFromSource); + List docs = handleSearchItems( + multiSearchResponse.getResponses()[1], + modelId, + (bytes, resourceId) -> ChunkedTrainedModelRestorer.parseModelDefinitionDocLenientlyFromSource( + bytes, + resourceId, + xContentRegistry + ) + ); + try { + BytesReference compressedData = getDefinitionFromDocs(docs, modelId); + builder.setDefinitionFromBytes(compressedData); + } catch (ElasticsearchException elasticsearchException) { + getTrainedModelListener.onFailure(elasticsearchException); + return; + } + } catch (ResourceNotFoundException ex) { - getTrainedModelListener.onFailure(new ResourceNotFoundException( - Messages.getMessage(Messages.INFERENCE_NOT_FOUND, modelId))); + getTrainedModelListener.onFailure( + new ResourceNotFoundException(Messages.getMessage(Messages.MODEL_DEFINITION_NOT_FOUND, modelId)) + ); return; } catch (Exception ex) { getTrainedModelListener.onFailure(ex); return; } + } + getTrainedModelListener.onResponse(builder); + }, getTrainedModelListener::onFailure); - if (includes.isIncludeModelDefinition()) { - try { - List docs = handleSearchItems(multiSearchResponse.getResponses()[1], - modelId, - (bytes, resourceId) -> - ChunkedTrainedModelRestorer.parseModelDefinitionDocLenientlyFromSource( - bytes, resourceId, xContentRegistry)); - try { - BytesReference compressedData = getDefinitionFromDocs(docs, modelId); - builder.setDefinitionFromBytes(compressedData); - } catch (ElasticsearchException elasticsearchException) { - getTrainedModelListener.onFailure(elasticsearchException); - return; - } - - } catch (ResourceNotFoundException ex) { - getTrainedModelListener.onFailure(new ResourceNotFoundException( - Messages.getMessage(Messages.MODEL_DEFINITION_NOT_FOUND, modelId))); - return; - } catch (Exception ex) { - getTrainedModelListener.onFailure(ex); - return; - } - } - getTrainedModelListener.onResponse(builder); - }, - getTrainedModelListener::onFailure - ); - - executeAsyncWithOrigin(client, + executeAsyncWithOrigin( + client, ML_ORIGIN, MultiSearchAction.INSTANCE, multiSearchRequestBuilder.request(), - multiSearchResponseActionListener); + multiSearchResponseActionListener + ); } - public void getTrainedModels(Set modelIds, - GetTrainedModelsAction.Includes includes, - boolean allowNoResources, - final ActionListener> finalListener) { + public void getTrainedModels( + Set modelIds, + GetTrainedModelsAction.Includes includes, + boolean allowNoResources, + final ActionListener> finalListener + ) { getTrainedModels( modelIds.stream().collect(Collectors.toMap(Function.identity(), _k -> Collections.emptySet())), includes, @@ -646,14 +694,14 @@ public void getTrainedModels(Set modelIds, * This does no expansion on the ids. * It assumes that there are fewer than 10k. */ - public void getTrainedModels(Map> modelIds, - GetTrainedModelsAction.Includes includes, - boolean allowNoResources, - final ActionListener> finalListener) { + public void getTrainedModels( + Map> modelIds, + GetTrainedModelsAction.Includes includes, + boolean allowNoResources, + final ActionListener> finalListener + ) { QueryBuilder queryBuilder = QueryBuilders.constantScoreQuery( - QueryBuilders - .idsQuery() - .addIds(modelIds.keySet().toArray(new String[0])) + QueryBuilders.idsQuery().addIds(modelIds.keySet().toArray(new String[0])) ); SearchRequest searchRequest = client.prepareSearch(InferenceIndexConstants.INDEX_PATTERN) @@ -665,7 +713,7 @@ public void getTrainedModels(Map> modelIds, List configs = new ArrayList<>(modelIds.size()); Set modelsInIndex = Sets.difference(modelIds.keySet(), MODELS_STORED_AS_RESOURCE); Set modelsAsResource = Sets.intersection(MODELS_STORED_AS_RESOURCE, modelIds.keySet()); - for(String modelId : modelsAsResource) { + for (String modelId : modelsAsResource) { try { configs.add(loadModelFromResource(modelId, true)); } catch (ElasticsearchException ex) { @@ -674,104 +722,96 @@ public void getTrainedModels(Map> modelIds, } } if (modelsInIndex.isEmpty()) { - finalListener.onResponse(configs.stream() - .map(TrainedModelConfig.Builder::build) - .sorted(Comparator.comparing(TrainedModelConfig::getModelId)) - .collect(Collectors.toList())); + finalListener.onResponse( + configs.stream() + .map(TrainedModelConfig.Builder::build) + .sorted(Comparator.comparing(TrainedModelConfig::getModelId)) + .collect(Collectors.toList()) + ); return; } - ActionListener> getTrainedModelListener = ActionListener.wrap( - modelBuilders -> { - if ((includes.isIncludeFeatureImportanceBaseline() || includes.isIncludeTotalFeatureImportance() - || includes.isIncludeHyperparameters()) == false) { - finalListener.onResponse(modelBuilders.stream() + ActionListener> getTrainedModelListener = ActionListener.wrap(modelBuilders -> { + if ((includes.isIncludeFeatureImportanceBaseline() + || includes.isIncludeTotalFeatureImportance() + || includes.isIncludeHyperparameters()) == false) { + finalListener.onResponse( + modelBuilders.stream() .map(b -> b.setModelAliases(modelIds.get(b.getModelId())).build()) .sorted(Comparator.comparing(TrainedModelConfig::getModelId)) - .collect(Collectors.toList())); - return; - } - this.getTrainedModelMetadata(modelIds.keySet(), ActionListener.wrap( - metadata -> - finalListener.onResponse(modelBuilders.stream() - .map(builder -> { - TrainedModelMetadata modelMetadata = metadata.get(builder.getModelId()); - if (modelMetadata != null) { - if (includes.isIncludeTotalFeatureImportance()) { - builder.setFeatureImportance(modelMetadata.getTotalFeatureImportances()); - } - if (includes.isIncludeFeatureImportanceBaseline()) { - builder.setBaselineFeatureImportance(modelMetadata.getFeatureImportanceBaselines()); - } - if (includes.isIncludeHyperparameters()) { - builder.setHyperparameters(modelMetadata.getHyperparameters()); - } - } - return builder.setModelAliases(modelIds.get(builder.getModelId())).build(); - }) - .sorted(Comparator.comparing(TrainedModelConfig::getModelId)) - .collect(Collectors.toList())), - failure -> { - // total feature importance is not necessary for a model to be valid - // we shouldn't fail if it is not found - if (ExceptionsHelper.unwrapCause(failure) instanceof ResourceNotFoundException) { - finalListener.onResponse(modelBuilders.stream() - .map(TrainedModelConfig.Builder::build) - .sorted(Comparator.comparing(TrainedModelConfig::getModelId)) - .collect(Collectors.toList())); - return; + .collect(Collectors.toList()) + ); + return; + } + this.getTrainedModelMetadata( + modelIds.keySet(), + ActionListener.wrap(metadata -> finalListener.onResponse(modelBuilders.stream().map(builder -> { + TrainedModelMetadata modelMetadata = metadata.get(builder.getModelId()); + if (modelMetadata != null) { + if (includes.isIncludeTotalFeatureImportance()) { + builder.setFeatureImportance(modelMetadata.getTotalFeatureImportances()); } - finalListener.onFailure(failure); - } - )); - }, - finalListener::onFailure - ); - - ActionListener configSearchHandler = ActionListener.wrap( - searchResponse -> { - Set observedIds = new HashSet<>( - searchResponse.getHits().getHits().length + modelsAsResource.size(), - 1.0f); - observedIds.addAll(modelsAsResource); - for(SearchHit searchHit : searchResponse.getHits().getHits()) { - try { - if (observedIds.contains(searchHit.getId()) == false) { - configs.add( - parseModelConfigLenientlyFromSource(searchHit.getSourceRef(), searchHit.getId()) - ); - observedIds.add(searchHit.getId()); + if (includes.isIncludeFeatureImportanceBaseline()) { + builder.setBaselineFeatureImportance(modelMetadata.getFeatureImportanceBaselines()); } - } catch (IOException ex) { - getTrainedModelListener.onFailure( - ExceptionsHelper.serverError(INFERENCE_FAILED_TO_DESERIALIZE, ex, searchHit.getId())); + if (includes.isIncludeHyperparameters()) { + builder.setHyperparameters(modelMetadata.getHyperparameters()); + } + } + return builder.setModelAliases(modelIds.get(builder.getModelId())).build(); + }).sorted(Comparator.comparing(TrainedModelConfig::getModelId)).collect(Collectors.toList())), failure -> { + // total feature importance is not necessary for a model to be valid + // we shouldn't fail if it is not found + if (ExceptionsHelper.unwrapCause(failure) instanceof ResourceNotFoundException) { + finalListener.onResponse( + modelBuilders.stream() + .map(TrainedModelConfig.Builder::build) + .sorted(Comparator.comparing(TrainedModelConfig::getModelId)) + .collect(Collectors.toList()) + ); return; } - } - // We previously expanded the IDs. - // If the config has gone missing between then and now we should throw if allowNoResources is false - // Otherwise, treat it as if it was never expanded to begin with. - Set missingConfigs = Sets.difference(modelIds.keySet(), observedIds); - if (missingConfigs.isEmpty() == false && allowNoResources == false) { - getTrainedModelListener.onFailure(new ResourceNotFoundException( - Messages.getMessage(Messages.INFERENCE_NOT_FOUND_MULTIPLE, missingConfigs))); + finalListener.onFailure(failure); + }) + ); + }, finalListener::onFailure); + + ActionListener configSearchHandler = ActionListener.wrap(searchResponse -> { + Set observedIds = new HashSet<>(searchResponse.getHits().getHits().length + modelsAsResource.size(), 1.0f); + observedIds.addAll(modelsAsResource); + for (SearchHit searchHit : searchResponse.getHits().getHits()) { + try { + if (observedIds.contains(searchHit.getId()) == false) { + configs.add(parseModelConfigLenientlyFromSource(searchHit.getSourceRef(), searchHit.getId())); + observedIds.add(searchHit.getId()); + } + } catch (IOException ex) { + getTrainedModelListener.onFailure(ExceptionsHelper.serverError(INFERENCE_FAILED_TO_DESERIALIZE, ex, searchHit.getId())); return; } - // Ensure sorted even with the injection of locally resourced models - getTrainedModelListener.onResponse(configs); - }, - getTrainedModelListener::onFailure - ); + } + // We previously expanded the IDs. + // If the config has gone missing between then and now we should throw if allowNoResources is false + // Otherwise, treat it as if it was never expanded to begin with. + Set missingConfigs = Sets.difference(modelIds.keySet(), observedIds); + if (missingConfigs.isEmpty() == false && allowNoResources == false) { + getTrainedModelListener.onFailure( + new ResourceNotFoundException(Messages.getMessage(Messages.INFERENCE_NOT_FOUND_MULTIPLE, missingConfigs)) + ); + return; + } + // Ensure sorted even with the injection of locally resourced models + getTrainedModelListener.onResponse(configs); + }, getTrainedModelListener::onFailure); executeAsyncWithOrigin(client, ML_ORIGIN, SearchAction.INSTANCE, searchRequest, configSearchHandler); } public void deleteTrainedModel(String modelId, ActionListener listener) { if (MODELS_STORED_AS_RESOURCE.contains(modelId)) { - listener.onFailure(ExceptionsHelper.badRequestException(Messages.getMessage( - Messages.INFERENCE_CANNOT_DELETE_ML_MANAGED_MODEL, - modelId - ))); + listener.onFailure( + ExceptionsHelper.badRequestException(Messages.getMessage(Messages.INFERENCE_CANNOT_DELETE_ML_MANAGED_MODEL, modelId)) + ); return; } DeleteByQueryRequest request = new DeleteByQueryRequest().setAbortOnVersionConflict(false); @@ -783,27 +823,27 @@ public void deleteTrainedModel(String modelId, ActionListener listener) executeAsyncWithOrigin(client, ML_ORIGIN, DeleteByQueryAction.INSTANCE, request, ActionListener.wrap(deleteResponse -> { if (deleteResponse.getDeleted() == 0) { - listener.onFailure(new ResourceNotFoundException( - Messages.getMessage(Messages.INFERENCE_NOT_FOUND, modelId))); + listener.onFailure(new ResourceNotFoundException(Messages.getMessage(Messages.INFERENCE_NOT_FOUND, modelId))); return; } listener.onResponse(true); }, e -> { if (e.getClass() == IndexNotFoundException.class) { - listener.onFailure(new ResourceNotFoundException( - Messages.getMessage(Messages.INFERENCE_NOT_FOUND, modelId))); + listener.onFailure(new ResourceNotFoundException(Messages.getMessage(Messages.INFERENCE_NOT_FOUND, modelId))); } else { listener.onFailure(e); } })); } - public void expandIds(String idExpression, - boolean allowNoResources, - PageParams pageParams, - Set tags, - ModelAliasMetadata modelAliasMetadata, - ActionListener>>> idsListener) { + public void expandIds( + String idExpression, + boolean allowNoResources, + PageParams pageParams, + Set tags, + ModelAliasMetadata modelAliasMetadata, + ActionListener>>> idsListener + ) { String[] tokens = Strings.tokenizeToStringArray(idExpression, ","); Set expandedIdsFromAliases = new HashSet<>(); if (Strings.isAllOrWildcard(tokens) == false) { @@ -825,7 +865,7 @@ public void expandIds(String idExpression, foundResourceIds = matchedResourceIds; } else { foundResourceIds = new HashSet<>(); - for(String resourceId : matchedResourceIds) { + for (String resourceId : matchedResourceIds) { // Does the model as a resource have all the tags? if (Sets.newHashSet(loadModelFromResource(resourceId, true).build().getTags()).containsAll(tags)) { foundResourceIds.add(resourceId); @@ -837,11 +877,12 @@ public void expandIds(String idExpression, // We need to include the translated model alias, and ANY tokens that were not translated String[] tokensForQuery = expandedIdsFromAliases.toArray(new String[0]); - SearchSourceBuilder sourceBuilder = new SearchSourceBuilder() - .sort(SortBuilders.fieldSort(TrainedModelConfig.MODEL_ID.getPreferredName()) + SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().sort( + SortBuilders.fieldSort(TrainedModelConfig.MODEL_ID.getPreferredName()) // If there are no resources, there might be no mapping for the id field. // This makes sure we don't get an error if that happens. - .unmappedType("long")) + .unmappedType("long") + ) .query(buildExpandIdsQuery(tokensForQuery, tags)) // We "buffer" the from and size to take into account models stored as resources. // This is so we handle the edge cases when the model that is stored as a resource is at the start/end of @@ -853,62 +894,62 @@ public void expandIds(String idExpression, .fetchSource(TrainedModelConfig.MODEL_ID.getPreferredName(), null); IndicesOptions indicesOptions = SearchRequest.DEFAULT_INDICES_OPTIONS; - SearchRequest searchRequest = new SearchRequest(InferenceIndexConstants.INDEX_PATTERN) - .indicesOptions(IndicesOptions.fromOptions(true, + SearchRequest searchRequest = new SearchRequest(InferenceIndexConstants.INDEX_PATTERN).indicesOptions( + IndicesOptions.fromOptions( + true, indicesOptions.allowNoIndices(), indicesOptions.expandWildcardsOpen(), indicesOptions.expandWildcardsClosed(), - indicesOptions)) - .source(sourceBuilder); + indicesOptions + ) + ).source(sourceBuilder); - executeAsyncWithOrigin(client.threadPool().getThreadContext(), + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), ML_ORIGIN, searchRequest, - ActionListener.wrap( - response -> { - long totalHitCount = response.getHits().getTotalHits().value + foundResourceIds.size(); - Set foundFromDocs = new HashSet<>(); - for (SearchHit hit : response.getHits().getHits()) { - Map docSource = hit.getSourceAsMap(); - if (docSource == null) { - continue; - } - Object idValue = docSource.get(TrainedModelConfig.MODEL_ID.getPreferredName()); - if (idValue instanceof String) { - foundFromDocs.add(idValue.toString()); - } + ActionListener.wrap(response -> { + long totalHitCount = response.getHits().getTotalHits().value + foundResourceIds.size(); + Set foundFromDocs = new HashSet<>(); + for (SearchHit hit : response.getHits().getHits()) { + Map docSource = hit.getSourceAsMap(); + if (docSource == null) { + continue; } - Map> allFoundIds = collectIds(pageParams, foundResourceIds, foundFromDocs) - .stream() - .collect(Collectors.toMap(Function.identity(), k -> new HashSet<>())); - - // We technically have matched on model tokens and any reversed referenced aliases - // We may end up with "over matching" on the aliases (matching on an alias that was not provided) - // But the expanded ID matcher does not care. - Set matchedTokens = new HashSet<>(allFoundIds.keySet()); - - // We should gather ALL model aliases referenced by the given model IDs - // This way the callers have access to them - modelAliasMetadata.modelAliases().forEach((alias, modelIdEntry) -> { - final String modelId = modelIdEntry.getModelId(); - if (allFoundIds.containsKey(modelId)) { - allFoundIds.get(modelId).add(alias); - matchedTokens.add(alias); - } - }); - - // Reverse lookup to see what model aliases were matched by their found trained model IDs - ExpandedIdsMatcher requiredMatches = new ExpandedIdsMatcher(tokens, allowNoResources); - requiredMatches.filterMatchedIds(matchedTokens); - if (requiredMatches.hasUnmatchedIds()) { - idsListener.onFailure(ExceptionsHelper.missingTrainedModel(requiredMatches.unmatchedIdsString())); - } else { - idsListener.onResponse(Tuple.tuple(totalHitCount, allFoundIds)); + Object idValue = docSource.get(TrainedModelConfig.MODEL_ID.getPreferredName()); + if (idValue instanceof String) { + foundFromDocs.add(idValue.toString()); } - }, - idsListener::onFailure - ), - client::search); + } + Map> allFoundIds = collectIds(pageParams, foundResourceIds, foundFromDocs).stream() + .collect(Collectors.toMap(Function.identity(), k -> new HashSet<>())); + + // We technically have matched on model tokens and any reversed referenced aliases + // We may end up with "over matching" on the aliases (matching on an alias that was not provided) + // But the expanded ID matcher does not care. + Set matchedTokens = new HashSet<>(allFoundIds.keySet()); + + // We should gather ALL model aliases referenced by the given model IDs + // This way the callers have access to them + modelAliasMetadata.modelAliases().forEach((alias, modelIdEntry) -> { + final String modelId = modelIdEntry.getModelId(); + if (allFoundIds.containsKey(modelId)) { + allFoundIds.get(modelId).add(alias); + matchedTokens.add(alias); + } + }); + + // Reverse lookup to see what model aliases were matched by their found trained model IDs + ExpandedIdsMatcher requiredMatches = new ExpandedIdsMatcher(tokens, allowNoResources); + requiredMatches.filterMatchedIds(matchedTokens); + if (requiredMatches.hasUnmatchedIds()) { + idsListener.onFailure(ExceptionsHelper.missingTrainedModel(requiredMatches.unmatchedIdsString())); + } else { + idsListener.onResponse(Tuple.tuple(totalHitCount, allFoundIds)); + } + }, idsListener::onFailure), + client::search + ); } public void getInferenceStats(String[] modelIds, ActionListener> listener) { @@ -918,73 +959,87 @@ public void getInferenceStats(String[] modelIds, ActionListenerwrap( - responses -> { - List allStats = new ArrayList<>(modelIds.length); - int modelIndex = 0; - assert responses.getResponses().length == modelIds.length : - "mismatch between search response size and models requested"; - for (MultiSearchResponse.Item response : responses.getResponses()) { - if (response.isFailure()) { - if (ExceptionsHelper.unwrapCause(response.getFailure()) instanceof ResourceNotFoundException) { - modelIndex++; - continue; - } - logger.error(new ParameterizedMessage("[{}] search failed for models", - Strings.arrayToCommaDelimitedString(modelIds)), - response.getFailure()); - listener.onFailure(ExceptionsHelper.serverError("Searching for stats for models [{}] failed", - response.getFailure(), - Strings.arrayToCommaDelimitedString(modelIds))); - return; - } - try { - InferenceStats inferenceStats = handleMultiNodeStatsResponse(response.getResponse(), modelIds[modelIndex++]); - if (inferenceStats != null) { - allStats.add(inferenceStats); - } - } catch (Exception e) { - listener.onFailure(e); - return; + ActionListener.wrap(responses -> { + List allStats = new ArrayList<>(modelIds.length); + int modelIndex = 0; + assert responses.getResponses().length == modelIds.length : "mismatch between search response size and models requested"; + for (MultiSearchResponse.Item response : responses.getResponses()) { + if (response.isFailure()) { + if (ExceptionsHelper.unwrapCause(response.getFailure()) instanceof ResourceNotFoundException) { + modelIndex++; + continue; } + logger.error( + new ParameterizedMessage("[{}] search failed for models", Strings.arrayToCommaDelimitedString(modelIds)), + response.getFailure() + ); + listener.onFailure( + ExceptionsHelper.serverError( + "Searching for stats for models [{}] failed", + response.getFailure(), + Strings.arrayToCommaDelimitedString(modelIds) + ) + ); + return; } - listener.onResponse(allStats); - }, - e -> { - Throwable unwrapped = ExceptionsHelper.unwrapCause(e); - if (unwrapped instanceof ResourceNotFoundException) { - listener.onResponse(Collections.emptyList()); + try { + InferenceStats inferenceStats = handleMultiNodeStatsResponse(response.getResponse(), modelIds[modelIndex++]); + if (inferenceStats != null) { + allStats.add(inferenceStats); + } + } catch (Exception e) { + listener.onFailure(e); return; } - listener.onFailure((Exception)unwrapped); } - ), - client::multiSearch); + listener.onResponse(allStats); + }, e -> { + Throwable unwrapped = ExceptionsHelper.unwrapCause(e); + if (unwrapped instanceof ResourceNotFoundException) { + listener.onResponse(Collections.emptyList()); + return; + } + listener.onFailure((Exception) unwrapped); + }), + client::multiSearch + ); } private SearchRequest buildStatsSearchRequest(String modelId) { BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery() .filter(QueryBuilders.termQuery(InferenceStats.MODEL_ID.getPreferredName(), modelId)) .filter(QueryBuilders.termQuery(InferenceStats.TYPE.getPreferredName(), InferenceStats.NAME)); - return new SearchRequest(MlStatsIndex.indexPattern()) - .indicesOptions(IndicesOptions.lenientExpandOpen()) + return new SearchRequest(MlStatsIndex.indexPattern()).indicesOptions(IndicesOptions.lenientExpandOpen()) .allowPartialSearchResults(false) - .source(SearchSourceBuilder.searchSource() - .size(0) - .aggregation(AggregationBuilders.sum(InferenceStats.FAILURE_COUNT.getPreferredName()) - .field(InferenceStats.FAILURE_COUNT.getPreferredName())) - .aggregation(AggregationBuilders.sum(InferenceStats.MISSING_ALL_FIELDS_COUNT.getPreferredName()) - .field(InferenceStats.MISSING_ALL_FIELDS_COUNT.getPreferredName())) - .aggregation(AggregationBuilders.sum(InferenceStats.INFERENCE_COUNT.getPreferredName()) - .field(InferenceStats.INFERENCE_COUNT.getPreferredName())) - .aggregation(AggregationBuilders.sum(InferenceStats.CACHE_MISS_COUNT.getPreferredName()) - .field(InferenceStats.CACHE_MISS_COUNT.getPreferredName())) - .aggregation(AggregationBuilders.max(InferenceStats.TIMESTAMP.getPreferredName()) - .field(InferenceStats.TIMESTAMP.getPreferredName())) - .query(queryBuilder)); + .source( + SearchSourceBuilder.searchSource() + .size(0) + .aggregation( + AggregationBuilders.sum(InferenceStats.FAILURE_COUNT.getPreferredName()) + .field(InferenceStats.FAILURE_COUNT.getPreferredName()) + ) + .aggregation( + AggregationBuilders.sum(InferenceStats.MISSING_ALL_FIELDS_COUNT.getPreferredName()) + .field(InferenceStats.MISSING_ALL_FIELDS_COUNT.getPreferredName()) + ) + .aggregation( + AggregationBuilders.sum(InferenceStats.INFERENCE_COUNT.getPreferredName()) + .field(InferenceStats.INFERENCE_COUNT.getPreferredName()) + ) + .aggregation( + AggregationBuilders.sum(InferenceStats.CACHE_MISS_COUNT.getPreferredName()) + .field(InferenceStats.CACHE_MISS_COUNT.getPreferredName()) + ) + .aggregation( + AggregationBuilders.max(InferenceStats.TIMESTAMP.getPreferredName()) + .field(InferenceStats.TIMESTAMP.getPreferredName()) + ) + .query(queryBuilder) + ); } private InferenceStats handleMultiNodeStatsResponse(SearchResponse response, String modelId) { @@ -1004,9 +1059,9 @@ private InferenceStats handleMultiNodeStatsResponse(SearchResponse response, Str cacheMiss == null ? 0L : Double.valueOf(cacheMiss.getValue()).longValue(), modelId, null, - timeStamp == null || (Numbers.isValidDouble(timeStamp.getValue()) == false) ? - Instant.now() : - Instant.ofEpochMilli(Double.valueOf(timeStamp.getValue()).longValue()) + timeStamp == null || (Numbers.isValidDouble(timeStamp.getValue()) == false) + ? Instant.now() + : Instant.ofEpochMilli(Double.valueOf(timeStamp.getValue()).longValue()) ); } @@ -1039,7 +1094,7 @@ static Set collectIds(PageParams pageParams, Set foundFromResour static QueryBuilder buildExpandIdsQuery(String[] tokens, Collection tags) { BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery() .filter(buildQueryIdExpressionQuery(tokens, TrainedModelConfig.MODEL_ID.getPreferredName())); - for(String tag : tags) { + for (String tag : tags) { boolQueryBuilder.filter(QueryBuilders.termQuery(TrainedModelConfig.TAGS.getPreferredName(), tag)); } return QueryBuilders.constantScoreQuery(boolQueryBuilder); @@ -1049,13 +1104,15 @@ TrainedModelConfig.Builder loadModelFromResource(String modelId, boolean nullOut URL resource = getClass().getResource(MODEL_RESOURCE_PATH + modelId + MODEL_RESOURCE_FILE_EXT); if (resource == null) { logger.error("[{}] presumed stored as a resource but not found", modelId); - throw new ResourceNotFoundException( - Messages.getMessage(Messages.INFERENCE_NOT_FOUND, modelId)); + throw new ResourceNotFoundException(Messages.getMessage(Messages.INFERENCE_NOT_FOUND, modelId)); } - try (XContentParser parser = JsonXContent.jsonXContent.createParser( + try ( + XContentParser parser = JsonXContent.jsonXContent.createParser( xContentRegistry, LoggingDeprecationHandler.INSTANCE, - getClass().getResourceAsStream(MODEL_RESOURCE_PATH + modelId + MODEL_RESOURCE_FILE_EXT))) { + getClass().getResourceAsStream(MODEL_RESOURCE_PATH + modelId + MODEL_RESOURCE_FILE_EXT) + ) + ) { TrainedModelConfig.Builder builder = TrainedModelConfig.fromXContent(parser, true); if (nullOutDefinition) { builder.clearDefinition(); @@ -1105,7 +1162,7 @@ private Set matchedResourceIds(String[] tokens) { for (String token : tokens) { if (Regex.isSimpleMatchPattern(token)) { for (String modelId : MODELS_STORED_AS_RESOURCE) { - if(Regex.simpleMatch(token, modelId)) { + if (Regex.simpleMatch(token, modelId)) { matchedModels.add(modelId); } } @@ -1118,16 +1175,20 @@ private Set matchedResourceIds(String[] tokens) { return Collections.unmodifiableSet(matchedModels); } - private static T handleSearchItem(MultiSearchResponse.Item item, - String resourceId, - CheckedBiFunction parseLeniently) throws Exception { + private static T handleSearchItem( + MultiSearchResponse.Item item, + String resourceId, + CheckedBiFunction parseLeniently + ) throws Exception { return handleSearchItems(item, resourceId, parseLeniently).get(0); } // NOTE: This ignores any results that are in a different index than the first one seen in the search response. - private static List handleSearchItems(MultiSearchResponse.Item item, - String resourceId, - CheckedBiFunction parseLeniently) throws Exception { + private static List handleSearchItems( + MultiSearchResponse.Item item, + String resourceId, + CheckedBiFunction parseLeniently + ) throws Exception { if (item.isFailure()) { throw item.getFailure(); } @@ -1138,9 +1199,11 @@ private static List handleSearchItems(MultiSearchResponse.Item item, } - private static List handleHits(SearchHit[] hits, - String resourceId, - CheckedBiFunction parseLeniently) throws Exception { + private static List handleHits( + SearchHit[] hits, + String resourceId, + CheckedBiFunction parseLeniently + ) throws Exception { List results = new ArrayList<>(hits.length); String initialIndex = hits[0].getIndex(); for (SearchHit hit : hits) { @@ -1152,8 +1215,8 @@ private static List handleHits(SearchHit[] hits, return results; } - private static BytesReference getDefinitionFromDocs(List docs, - String modelId) throws ElasticsearchException { + private static BytesReference getDefinitionFromDocs(List docs, String modelId) + throws ElasticsearchException { BytesReference[] bb = new BytesReference[docs.size()]; for (int i = 0; i < docs.size(); i++) { @@ -1176,8 +1239,8 @@ private static BytesReference getDefinitionFromDocs(List chunkDefinitionWithSize(BytesReference definition, int chunkSize) { - List chunks = new ArrayList<>((int)Math.ceil(definition.length()/(double)chunkSize)); - for (int i = 0; i < definition.length();i += chunkSize) { + List chunks = new ArrayList<>((int) Math.ceil(definition.length() / (double) chunkSize)); + for (int i = 0; i < definition.length(); i += chunkSize) { BytesReference chunk = definition.slice(i, Math.min(chunkSize, definition.length() - i)); chunks.add(chunk); } @@ -1185,9 +1248,11 @@ public static List chunkDefinitionWithSize(BytesReference defini } private TrainedModelConfig.Builder parseModelConfigLenientlyFromSource(BytesReference source, String modelId) throws IOException { - try (InputStream stream = source.streamInput(); - XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, stream)) { + try ( + InputStream stream = source.streamInput(); + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, stream) + ) { TrainedModelConfig.Builder builder = TrainedModelConfig.fromXContent(parser, true); if (builder.getModelType() == null) { @@ -1206,9 +1271,11 @@ private TrainedModelConfig.Builder parseModelConfigLenientlyFromSource(BytesRefe } private TrainedModelMetadata parseMetadataLenientlyFromSource(BytesReference source, String modelId) throws IOException { - try (InputStream stream = source.streamInput(); - XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, stream)) { + try ( + InputStream stream = source.streamInput(); + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, stream) + ) { return TrainedModelMetadata.fromXContent(parser, true); } catch (IOException e) { logger.error(new ParameterizedMessage("[{}] failed to parse model metadata", modelId), e); @@ -1233,7 +1300,8 @@ private IndexRequest createRequest(IndexRequest request, String docId, ToXConten // that is not the users fault. We did something wrong and should throw. throw ExceptionsHelper.serverError( new ParameterizedMessage("Unexpected serialization exception for [{}]", docId).getFormattedMessage(), - ex); + ex + ); } } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/pytorch/process/NativePyTorchProcess.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/pytorch/process/NativePyTorchProcess.java index 538233cfcef3e..3827e0087746f 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/pytorch/process/NativePyTorchProcess.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/pytorch/process/NativePyTorchProcess.java @@ -28,8 +28,14 @@ public class NativePyTorchProcess extends AbstractNativeProcess { private final ProcessResultsParser resultsParser; - protected NativePyTorchProcess(String jobId, NativeController nativeController, ProcessPipes processPipes, int numberOfFields, - List filesToDelete, Consumer onProcessCrash) { + protected NativePyTorchProcess( + String jobId, + NativeController nativeController, + ProcessPipes processPipes, + int numberOfFields, + List filesToDelete, + Consumer onProcessCrash + ) { super(jobId, nativeController, processPipes, numberOfFields, filesToDelete, onProcessCrash); this.resultsParser = new ProcessResultsParser<>(PyTorchResult.PARSER, NamedXContentRegistry.EMPTY); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/pytorch/process/NativePyTorchProcessFactory.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/pytorch/process/NativePyTorchProcessFactory.java index fb99cbe53a0dc..bb0c2e7e0e2bb 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/pytorch/process/NativePyTorchProcessFactory.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/pytorch/process/NativePyTorchProcessFactory.java @@ -39,15 +39,13 @@ public class NativePyTorchProcessFactory implements PyTorchProcessFactory { private final String nodeName; private volatile Duration processConnectTimeout; - public NativePyTorchProcessFactory(Environment env, - NativeController nativeController, - ClusterService clusterService) { + public NativePyTorchProcessFactory(Environment env, NativeController nativeController, ClusterService clusterService) { this.env = Objects.requireNonNull(env); this.nativeController = Objects.requireNonNull(nativeController); this.nodeName = clusterService.getNodeName(); setProcessConnectTimeout(MachineLearning.PROCESS_CONNECT_TIMEOUT.get(env.settings())); - clusterService.getClusterSettings().addSettingsUpdateConsumer(MachineLearning.PROCESS_CONNECT_TIMEOUT, - this::setProcessConnectTimeout); + clusterService.getClusterSettings() + .addSettingsUpdateConsumer(MachineLearning.PROCESS_CONNECT_TIMEOUT, this::setProcessConnectTimeout); } void setProcessConnectTimeout(TimeValue processConnectTimeout) { @@ -55,8 +53,11 @@ void setProcessConnectTimeout(TimeValue processConnectTimeout) { } @Override - public NativePyTorchProcess createProcess(TrainedModelDeploymentTask task, ExecutorService executorService, - Consumer onProcessCrash) { + public NativePyTorchProcess createProcess( + TrainedModelDeploymentTask task, + ExecutorService executorService, + Consumer onProcessCrash + ) { ProcessPipes processPipes = new ProcessPipes( env, NAMED_PIPE_HELPER, @@ -73,12 +74,18 @@ public NativePyTorchProcess createProcess(TrainedModelDeploymentTask task, Execu executeProcess(processPipes, task); - NativePyTorchProcess process = new NativePyTorchProcess(task.getModelId(), nativeController, processPipes, 0, - Collections.emptyList(), onProcessCrash); + NativePyTorchProcess process = new NativePyTorchProcess( + task.getModelId(), + nativeController, + processPipes, + 0, + Collections.emptyList(), + onProcessCrash + ); try { process.start(executorService); - } catch(IOException | EsRejectedExecutionException e) { + } catch (IOException | EsRejectedExecutionException e) { String msg = "Failed to connect to pytorch process for job " + task.getModelId(); logger.error(msg); try { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/pytorch/process/PyTorchBuilder.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/pytorch/process/PyTorchBuilder.java index 4bf4926116d76..fd8829940a0ee 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/pytorch/process/PyTorchBuilder.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/pytorch/process/PyTorchBuilder.java @@ -29,10 +29,7 @@ public class PyTorchBuilder { private final int inferenceThreads; private final int modelThreads; - public PyTorchBuilder(NativeController nativeController, - ProcessPipes processPipes, - int inferenceThreads, - int modelThreads) { + public PyTorchBuilder(NativeController nativeController, ProcessPipes processPipes, int inferenceThreads, int modelThreads) { this.nativeController = Objects.requireNonNull(nativeController); this.processPipes = Objects.requireNonNull(processPipes); this.inferenceThreads = inferenceThreads; diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/pytorch/process/PyTorchResultProcessor.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/pytorch/process/PyTorchResultProcessor.java index 3d578678a5c91..fe7bd95ec3611 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/pytorch/process/PyTorchResultProcessor.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/pytorch/process/PyTorchResultProcessor.java @@ -77,13 +77,17 @@ public void process(NativePyTorchProcess process) { logger.error(new ParameterizedMessage("[{}] Error processing results", deploymentId), e); } pendingResults.forEach((id, pendingResults) -> { - if (pendingResults.result.compareAndSet(null, new PyTorchResult( - id, + if (pendingResults.result.compareAndSet( null, - null, - isStopping ? - "inference canceled as process is stopping" : - "inference native process died unexpectedly with failure [" + e.getMessage() + "]"))) { + new PyTorchResult( + id, + null, + null, + isStopping + ? "inference canceled as process is stopping" + : "inference native process died unexpectedly with failure [" + e.getMessage() + "]" + ) + )) { pendingResults.latch.countDown(); } }); @@ -91,11 +95,10 @@ public void process(NativePyTorchProcess process) { } finally { pendingResults.forEach((id, pendingResults) -> { // Only set the result if it has not already been set - if (pendingResults.result.compareAndSet(null, new PyTorchResult( - id, - null, + if (pendingResults.result.compareAndSet( null, - "inference canceled as process is stopping"))) { + new PyTorchResult(id, null, null, "inference canceled as process is stopping") + )) { pendingResults.latch.countDown(); } }); @@ -106,13 +109,9 @@ public void process(NativePyTorchProcess process) { } public synchronized LongSummaryStatistics getTimingStats() { - return new LongSummaryStatistics(timingStats.getCount(), - timingStats.getMin(), - timingStats.getMax(), - timingStats.getSum()); + return new LongSummaryStatistics(timingStats.getCount(), timingStats.getMin(), timingStats.getMax(), timingStats.getSum()); } - private synchronized void processResult(PyTorchResult result) { if (result.isError() == false) { timingStats.accept(result.getTimeMs()); @@ -120,17 +119,11 @@ private synchronized void processResult(PyTorchResult result) { } } - public PyTorchResult waitForResult( - NativePyTorchProcess process, - String requestId, - PendingResult pendingResult, - TimeValue timeout - ) throws InterruptedException { + public PyTorchResult waitForResult(NativePyTorchProcess process, String requestId, PendingResult pendingResult, TimeValue timeout) + throws InterruptedException { if (process == null || stoppedProcessing || process.isProcessAlive() == false) { PyTorchResult storedResult = pendingResult.result.get(); - return storedResult == null ? - new PyTorchResult(requestId, null, null, "native process no longer started") : - storedResult; + return storedResult == null ? new PyTorchResult(requestId, null, null, "native process no longer started") : storedResult; } if (pendingResult.latch.await(timeout.millis(), TimeUnit.MILLISECONDS)) { return pendingResult.result.get(); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/pytorch/process/PyTorchStateStreamer.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/pytorch/process/PyTorchStateStreamer.java index 7e67f891be2d4..7a0784536833a 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/pytorch/process/PyTorchStateStreamer.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/pytorch/process/PyTorchStateStreamer.java @@ -68,14 +68,10 @@ public void writeStateToStream(String modelId, String index, OutputStream restor ChunkedTrainedModelRestorer restorer = new ChunkedTrainedModelRestorer(modelId, client, executorService, xContentRegistry); restorer.setSearchIndex(index); restorer.setSearchSize(1); - restorer.restoreModelDefinition( - doc -> writeChunk(doc, restoreStream), - success -> { - logger.debug("model [{}] state restored in [{}] documents from index [{}]", modelId, restorer.getNumDocsWritten(), index); - listener.onResponse(success); - }, - listener::onFailure - ); + restorer.restoreModelDefinition(doc -> writeChunk(doc, restoreStream), success -> { + logger.debug("model [{}] state restored in [{}] documents from index [{}]", modelId, restorer.getNumDocsWritten(), index); + listener.onResponse(success); + }, listener::onFailure); } private boolean writeChunk(TrainedModelDefinitionDoc doc, OutputStream outputStream) throws IOException { @@ -95,20 +91,25 @@ private boolean writeChunk(TrainedModelDefinitionDoc doc, OutputStream outputStr private int writeModelSize(String modelId, Long modelSizeBytes, OutputStream outputStream) throws IOException { if (modelSizeBytes == null) { - String message = String.format(Locale.ROOT, + String message = String.format( + Locale.ROOT, "The definition doc for model [%s] has a null value for field [%s]", - modelId, TrainedModelDefinitionDoc.TOTAL_DEFINITION_LENGTH.getPreferredName()); + modelId, + TrainedModelDefinitionDoc.TOTAL_DEFINITION_LENGTH.getPreferredName() + ); logger.error(message); throw new IllegalStateException(message); } if (modelSizeBytes <= 0) { // The other end expects an unsigned 32 bit int a -ve value is invalid. // ByteSizeValue allows -1 bytes as a valid value so this check is still required - String message = String.format(Locale.ROOT, + String message = String.format( + Locale.ROOT, "The definition doc for model [%s] has a negative value [%s] for field [%s]", modelId, modelSizeBytes, - TrainedModelDefinitionDoc.TOTAL_DEFINITION_LENGTH.getPreferredName()); + TrainedModelDefinitionDoc.TOTAL_DEFINITION_LENGTH.getPreferredName() + ); logger.error(message); throw new IllegalStateException(message); @@ -116,9 +117,13 @@ private int writeModelSize(String modelId, Long modelSizeBytes, OutputStream out if (modelSizeBytes > Integer.MAX_VALUE) { // TODO use a long in case models are larger than 2^31 bytes - String message = String.format(Locale.ROOT, + String message = String.format( + Locale.ROOT, "model [%s] has a size [%s] larger than the max size [%s]", - modelId, modelSizeBytes, Integer.MAX_VALUE); + modelId, + modelSizeBytes, + Integer.MAX_VALUE + ); logger.error(message); throw new IllegalStateException(message); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/JobManager.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/JobManager.java index fbeff0abd45b1..e5f008f13ac35 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/JobManager.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/JobManager.java @@ -23,15 +23,15 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeValue; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.core.CheckedConsumer; import org.elasticsearch.env.Environment; import org.elasticsearch.index.analysis.AnalysisRegistry; import org.elasticsearch.persistent.PersistentTasksCustomMetadata; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xpack.core.action.util.QueryPage; import org.elasticsearch.xpack.core.ml.MachineLearningField; import org.elasticsearch.xpack.core.ml.MlConfigIndex; @@ -124,7 +124,7 @@ public JobManager( UpdateJobProcessNotifier updateJobProcessNotifier, NamedXContentRegistry xContentRegistry, IndexNameExpressionResolver indexNameExpressionResolver - ) { + ) { this.jobResultsProvider = Objects.requireNonNull(jobResultsProvider); this.jobResultsPersister = Objects.requireNonNull(jobResultsPersister); this.clusterService = Objects.requireNonNull(clusterService); @@ -138,7 +138,7 @@ public JobManager( this.indexNameExpressionResolver = Objects.requireNonNull(indexNameExpressionResolver); maxModelMemoryLimit = MachineLearningField.MAX_MODEL_MEMORY_LIMIT.get(settings); clusterService.getClusterSettings() - .addSettingsUpdateConsumer(MachineLearningField.MAX_MODEL_MEMORY_LIMIT, this::setMaxModelMemoryLimit); + .addSettingsUpdateConsumer(MachineLearningField.MAX_MODEL_MEMORY_LIMIT, this::setMaxModelMemoryLimit); } private void setMaxModelMemoryLimit(ByteSizeValue maxModelMemoryLimit) { @@ -157,7 +157,9 @@ public void jobExists(String jobId, ActionListener listener) { * a ResourceNotFoundException is returned */ public void getJob(String jobId, ActionListener jobListener) { - jobConfigProvider.getJob(jobId, ActionListener.wrap( + jobConfigProvider.getJob( + jobId, + ActionListener.wrap( r -> jobListener.onResponse(r.build()), // TODO JIndex we shouldn't be building the job here e -> { if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) { @@ -167,7 +169,8 @@ public void getJob(String jobId, ActionListener jobListener) { jobListener.onFailure(e); } } - )); + ) + ); } /** @@ -197,24 +200,22 @@ private void getJobFromClusterState(String jobId, ActionListener jobListene */ public void expandJobBuilders(String expression, boolean allowNoMatch, ActionListener> jobsListener) { Map clusterStateJobs = expandJobsFromClusterState(expression, allowNoMatch, clusterService.state()); - jobConfigProvider.expandJobs(expression, allowNoMatch, false, ActionListener.wrap( - jobBuilders -> { - // Check for duplicate jobs - for (Job.Builder jb : jobBuilders) { - if (clusterStateJobs.containsKey(jb.getId())) { - jobsListener.onFailure(new IllegalStateException("Job [" + jb.getId() + "] configuration " + - "exists in both clusterstate and index")); - return; - } + jobConfigProvider.expandJobs(expression, allowNoMatch, false, ActionListener.wrap(jobBuilders -> { + // Check for duplicate jobs + for (Job.Builder jb : jobBuilders) { + if (clusterStateJobs.containsKey(jb.getId())) { + jobsListener.onFailure( + new IllegalStateException("Job [" + jb.getId() + "] configuration " + "exists in both clusterstate and index") + ); + return; } - // Merge cluster state and index jobs - List jobs = new ArrayList<>(jobBuilders); - jobs.addAll(clusterStateJobs.values().stream().map(Job.Builder::new).collect(Collectors.toList())); - jobs.sort(Comparator.comparing(Job.Builder::getId)); - jobsListener.onResponse(jobs); - }, - jobsListener::onFailure - )); + } + // Merge cluster state and index jobs + List jobs = new ArrayList<>(jobBuilders); + jobs.addAll(clusterStateJobs.values().stream().map(Job.Builder::new).collect(Collectors.toList())); + jobs.sort(Comparator.comparing(Job.Builder::getId)); + jobsListener.onResponse(jobs); + }, jobsListener::onFailure)); } /** @@ -226,16 +227,20 @@ public void expandJobBuilders(String expression, boolean allowNoMatch, ActionLis * @param jobsListener The jobs listener */ public void expandJobs(String expression, boolean allowNoMatch, ActionListener> jobsListener) { - expandJobBuilders(expression, allowNoMatch, ActionListener.wrap( - jobBuilders -> jobsListener.onResponse( - new QueryPage<>( - jobBuilders.stream().map(Job.Builder::build).collect(Collectors.toList()), - jobBuilders.size(), - Job.RESULTS_FIELD - ) - ), - jobsListener::onFailure - )); + expandJobBuilders( + expression, + allowNoMatch, + ActionListener.wrap( + jobBuilders -> jobsListener.onResponse( + new QueryPage<>( + jobBuilders.stream().map(Job.Builder::build).collect(Collectors.toList()), + jobBuilders.size(), + Job.RESULTS_FIELD + ) + ), + jobsListener::onFailure + ) + ); } private Map expandJobsFromClusterState(String expression, boolean allowNoMatch, ClusterState clusterState) { @@ -262,31 +267,39 @@ private Map expandJobsFromClusterState(String expression, boolean a * analysis modules/plugins. (The overall structure can be validated at parse time, but the exact names need * to be checked separately, as plugins that provide the functionality can be installed/uninstalled.) */ - static void validateCategorizationAnalyzerOrSetDefault(Job.Builder jobBuilder, AnalysisRegistry analysisRegistry, - Version minNodeVersion) throws IOException { + static void validateCategorizationAnalyzerOrSetDefault( + Job.Builder jobBuilder, + AnalysisRegistry analysisRegistry, + Version minNodeVersion + ) throws IOException { AnalysisConfig analysisConfig = jobBuilder.getAnalysisConfig(); CategorizationAnalyzerConfig categorizationAnalyzerConfig = analysisConfig.getCategorizationAnalyzerConfig(); if (categorizationAnalyzerConfig != null) { - CategorizationAnalyzer.verifyConfigBuilder(new CategorizationAnalyzerConfig.Builder(categorizationAnalyzerConfig), - analysisRegistry); + CategorizationAnalyzer.verifyConfigBuilder( + new CategorizationAnalyzerConfig.Builder(categorizationAnalyzerConfig), + analysisRegistry + ); } else if (analysisConfig.getCategorizationFieldName() != null && minNodeVersion.onOrAfter(MIN_NODE_VERSION_FOR_STANDARD_CATEGORIZATION_ANALYZER)) { - // Any supplied categorization filters are transferred into the new categorization analyzer. - // The user supplied categorization filters will already have been validated when the put job - // request was built, so we know they're valid. - AnalysisConfig.Builder analysisConfigBuilder = new AnalysisConfig.Builder(analysisConfig) - .setCategorizationAnalyzerConfig( - CategorizationAnalyzerConfig.buildStandardCategorizationAnalyzer(analysisConfig.getCategorizationFilters())) - .setCategorizationFilters(null); - jobBuilder.setAnalysisConfig(analysisConfigBuilder); - } + // Any supplied categorization filters are transferred into the new categorization analyzer. + // The user supplied categorization filters will already have been validated when the put job + // request was built, so we know they're valid. + AnalysisConfig.Builder analysisConfigBuilder = new AnalysisConfig.Builder(analysisConfig).setCategorizationAnalyzerConfig( + CategorizationAnalyzerConfig.buildStandardCategorizationAnalyzer(analysisConfig.getCategorizationFilters()) + ).setCategorizationFilters(null); + jobBuilder.setAnalysisConfig(analysisConfigBuilder); + } } /** * Stores the anomaly job configuration */ - public void putJob(PutJobAction.Request request, AnalysisRegistry analysisRegistry, ClusterState state, - ActionListener actionListener) throws IOException { + public void putJob( + PutJobAction.Request request, + AnalysisRegistry analysisRegistry, + ClusterState state, + ActionListener actionListener + ) throws IOException { Version minNodeVersion = state.getNodes().getMinNodeVersion(); @@ -308,13 +321,10 @@ public void putJob(PutJobAction.Request request, AnalysisRegistry analysisRegist @Override public void onResponse(Boolean mappingsUpdated) { - jobConfigProvider.putJob(job, ActionListener.wrap( - response -> { - auditor.info(job.getId(), Messages.getMessage(Messages.JOB_AUDIT_CREATED)); - actionListener.onResponse(new PutJobAction.Response(job)); - }, - actionListener::onFailure - )); + jobConfigProvider.putJob(job, ActionListener.wrap(response -> { + auditor.info(job.getId(), Messages.getMessage(Messages.JOB_AUDIT_CREATED)); + actionListener.onResponse(new PutJobAction.Response(job)); + }, actionListener::onFailure)); } @Override @@ -333,72 +343,68 @@ public void onFailure(Exception e) { } }; - ActionListener addDocMappingsListener = ActionListener.wrap( - indicesCreated -> { - if (state == null) { - logger.warn("Cannot update doc mapping because clusterState == null"); - putJobListener.onResponse(false); - return; - } - ElasticsearchMappings.addDocMappingIfMissing( - MlConfigIndex.indexName(), MlConfigIndex::mapping, client, state, request.masterNodeTimeout(), putJobListener); - }, - putJobListener::onFailure - ); - - ActionListener> checkForLeftOverDocs = ActionListener.wrap( - matchedIds -> { - if (matchedIds.isEmpty()) { - if (job.getDatafeedConfig().isPresent()) { - try { - DatafeedJobValidator.validate(job.getDatafeedConfig().get(), job, xContentRegistry); - } catch (Exception e) { - actionListener.onFailure(e); - return; - } - } - jobResultsProvider.createJobResultIndex(job, state, addDocMappingsListener); - } else { - // A job has the same Id as one of the group names - // error with the first in the list - actionListener.onFailure(new ResourceAlreadyExistsException( - Messages.getMessage(Messages.JOB_AND_GROUP_NAMES_MUST_BE_UNIQUE, matchedIds.get(0)))); - } - }, - actionListener::onFailure - ); - - ActionListener checkNoJobsWithGroupId = ActionListener.wrap( - groupExists -> { - if (groupExists) { - actionListener.onFailure(new ResourceAlreadyExistsException( - Messages.getMessage(Messages.JOB_AND_GROUP_NAMES_MUST_BE_UNIQUE, job.getId()))); + ActionListener addDocMappingsListener = ActionListener.wrap(indicesCreated -> { + if (state == null) { + logger.warn("Cannot update doc mapping because clusterState == null"); + putJobListener.onResponse(false); + return; + } + ElasticsearchMappings.addDocMappingIfMissing( + MlConfigIndex.indexName(), + MlConfigIndex::mapping, + client, + state, + request.masterNodeTimeout(), + putJobListener + ); + }, putJobListener::onFailure); + + ActionListener> checkForLeftOverDocs = ActionListener.wrap(matchedIds -> { + if (matchedIds.isEmpty()) { + if (job.getDatafeedConfig().isPresent()) { + try { + DatafeedJobValidator.validate(job.getDatafeedConfig().get(), job, xContentRegistry); + } catch (Exception e) { + actionListener.onFailure(e); return; } - if (job.getGroups().isEmpty()) { - checkForLeftOverDocs.onResponse(Collections.emptyList()); - } else { - jobConfigProvider.jobIdMatches(job.getGroups(), checkForLeftOverDocs); - } - }, - actionListener::onFailure - ); + } + jobResultsProvider.createJobResultIndex(job, state, addDocMappingsListener); + } else { + // A job has the same Id as one of the group names + // error with the first in the list + actionListener.onFailure( + new ResourceAlreadyExistsException(Messages.getMessage(Messages.JOB_AND_GROUP_NAMES_MUST_BE_UNIQUE, matchedIds.get(0))) + ); + } + }, actionListener::onFailure); + + ActionListener checkNoJobsWithGroupId = ActionListener.wrap(groupExists -> { + if (groupExists) { + actionListener.onFailure( + new ResourceAlreadyExistsException(Messages.getMessage(Messages.JOB_AND_GROUP_NAMES_MUST_BE_UNIQUE, job.getId())) + ); + return; + } + if (job.getGroups().isEmpty()) { + checkForLeftOverDocs.onResponse(Collections.emptyList()); + } else { + jobConfigProvider.jobIdMatches(job.getGroups(), checkForLeftOverDocs); + } + }, actionListener::onFailure); ActionListener checkNoGroupWithTheJobId = ActionListener.wrap( - ok -> jobConfigProvider.groupExists(job.getId(), checkNoJobsWithGroupId), - actionListener::onFailure + ok -> jobConfigProvider.groupExists(job.getId(), checkNoJobsWithGroupId), + actionListener::onFailure ); - jobConfigProvider.jobExists(job.getId(), false, ActionListener.wrap( - jobExists -> { - if (jobExists) { - actionListener.onFailure(ExceptionsHelper.jobAlreadyExists(job.getId())); - } else { - jobResultsProvider.checkForLeftOverDocuments(job, checkNoGroupWithTheJobId); - } - }, - actionListener::onFailure - )); + jobConfigProvider.jobExists(job.getId(), false, ActionListener.wrap(jobExists -> { + if (jobExists) { + actionListener.onFailure(ExceptionsHelper.jobAlreadyExists(job.getId())); + } else { + jobResultsProvider.checkForLeftOverDocuments(job, checkNoGroupWithTheJobId); + } + }, actionListener::onFailure)); } public void updateJob(UpdateJobAction.Request request, ActionListener actionListener) { @@ -409,34 +415,39 @@ public void updateJob(UpdateJobAction.Request request, ActionListener - jobConfigProvider.updateJobWithValidation(request.getJobId(), request.getJobUpdate(), maxModelMemoryLimit, - this::validate, ActionListener.wrap( - updatedJob -> postJobUpdate(request, updatedJob, actionListener), - actionListener::onFailure - )); + Runnable doUpdate = () -> jobConfigProvider.updateJobWithValidation( + request.getJobId(), + request.getJobUpdate(), + maxModelMemoryLimit, + this::validate, + ActionListener.wrap(updatedJob -> postJobUpdate(request, updatedJob, actionListener), actionListener::onFailure) + ); // Obviously if we're updating a job it's impossible that the config index has no mappings at // all, but if we rewrite the job config we may add new fields that require the latest mappings - Runnable checkMappingsAreUpToDate = () -> - ElasticsearchMappings.addDocMappingIfMissing( - MlConfigIndex.indexName(), MlConfigIndex::mapping, client, clusterState, request.masterNodeTimeout(), - ActionListener.wrap(bool -> doUpdate.run(), actionListener::onFailure)); + Runnable checkMappingsAreUpToDate = () -> ElasticsearchMappings.addDocMappingIfMissing( + MlConfigIndex.indexName(), + MlConfigIndex::mapping, + client, + clusterState, + request.masterNodeTimeout(), + ActionListener.wrap(bool -> doUpdate.run(), actionListener::onFailure) + ); if (request.getJobUpdate().getGroups() != null && request.getJobUpdate().getGroups().isEmpty() == false) { // check the new groups are not job Ids - jobConfigProvider.jobIdMatches(request.getJobUpdate().getGroups(), ActionListener.wrap( - matchingIds -> { - if (matchingIds.isEmpty()) { - checkMappingsAreUpToDate.run(); - } else { - actionListener.onFailure(new ResourceAlreadyExistsException( - Messages.getMessage(Messages.JOB_AND_GROUP_NAMES_MUST_BE_UNIQUE, matchingIds.get(0)))); - } - }, - actionListener::onFailure - )); + jobConfigProvider.jobIdMatches(request.getJobUpdate().getGroups(), ActionListener.wrap(matchingIds -> { + if (matchingIds.isEmpty()) { + checkMappingsAreUpToDate.run(); + } else { + actionListener.onFailure( + new ResourceAlreadyExistsException( + Messages.getMessage(Messages.JOB_AND_GROUP_NAMES_MUST_BE_UNIQUE, matchingIds.get(0)) + ) + ); + } + }, actionListener::onFailure)); } else { checkMappingsAreUpToDate.run(); } @@ -472,10 +483,7 @@ public void deleteJob( CheckedConsumer deleteJobStateHandler = response -> jobConfigProvider.deleteJob( jobId, false, - ActionListener.wrap( - deleteResponse -> apiResponseHandler.accept(Boolean.TRUE), - listener::onFailure - ) + ActionListener.wrap(deleteResponse -> apiResponseHandler.accept(Boolean.TRUE), listener::onFailure) ); // Step 2. Remove the job from any calendars @@ -484,7 +492,6 @@ public void deleteJob( ActionListener.wrap(deleteJobStateHandler, listener::onFailure) ); - // Step 1. Delete the physical storage new JobDataDeleter(client, jobId).deleteJobDocuments( jobConfigProvider, @@ -500,15 +507,13 @@ private void postJobUpdate(UpdateJobAction.Request request, Job updatedJob, Acti if (request.getJobUpdate().isAutodetectProcessUpdate()) { JobUpdate jobUpdate = request.getJobUpdate(); if (isJobOpen(clusterService.state(), request.getJobId())) { - updateJobProcessNotifier.submitJobUpdate(UpdateParams.fromJobUpdate(jobUpdate), ActionListener.wrap( - isUpdated -> { - if (isUpdated) { - auditJobUpdatedIfNotInternal(request); - } - }, e -> { - // No need to do anything - } - )); + updateJobProcessNotifier.submitJobUpdate(UpdateParams.fromJobUpdate(jobUpdate), ActionListener.wrap(isUpdated -> { + if (isUpdated) { + auditJobUpdatedIfNotInternal(request); + } + }, e -> { + // No need to do anything + })); } } else { logger.debug("[{}] No process update required for job update: {}", request::getJobId, () -> { @@ -528,8 +533,10 @@ private void postJobUpdate(UpdateJobAction.Request request, Job updatedJob, Acti } private void validate(Job job, JobUpdate jobUpdate, ActionListener handler) { - VoidChainTaskExecutor voidChainTaskExecutor = new VoidChainTaskExecutor(client.threadPool().executor( - MachineLearning.UTILITY_THREAD_POOL_NAME), true); + VoidChainTaskExecutor voidChainTaskExecutor = new VoidChainTaskExecutor( + client.threadPool().executor(MachineLearning.UTILITY_THREAD_POOL_NAME), + true + ); validateModelSnapshotIdUpdate(job, jobUpdate.getModelSnapshotId(), voidChainTaskExecutor); validateAnalysisLimitsUpdate(job, jobUpdate.getAnalysisLimits(), voidChainTaskExecutor); voidChainTaskExecutor.execute(ActionListener.wrap(aVoids -> handler.onResponse(null), handler::onFailure)); @@ -540,16 +547,18 @@ private void validateModelSnapshotIdUpdate(Job job, String modelSnapshotId, Void voidChainTaskExecutor.add(listener -> { jobResultsProvider.getModelSnapshot(job.getId(), modelSnapshotId, newModelSnapshot -> { if (newModelSnapshot == null) { - String message = Messages.getMessage(Messages.REST_NO_SUCH_MODEL_SNAPSHOT, modelSnapshotId, - job.getId()); + String message = Messages.getMessage(Messages.REST_NO_SUCH_MODEL_SNAPSHOT, modelSnapshotId, job.getId()); listener.onFailure(new ResourceNotFoundException(message)); return; } jobResultsProvider.getModelSnapshot(job.getId(), job.getModelSnapshotId(), oldModelSnapshot -> { if (oldModelSnapshot != null - && newModelSnapshot.result.getTimestamp().before(oldModelSnapshot.result.getTimestamp())) { - String message = "Job [" + job.getId() + "] has a more recent model snapshot [" + - oldModelSnapshot.result.getSnapshotId() + "]"; + && newModelSnapshot.result.getTimestamp().before(oldModelSnapshot.result.getTimestamp())) { + String message = "Job [" + + job.getId() + + "] has a more recent model snapshot [" + + oldModelSnapshot.result.getSnapshotId() + + "]"; listener.onFailure(new IllegalArgumentException(message)); } listener.onResponse(null); @@ -566,18 +575,26 @@ private void validateAnalysisLimitsUpdate(Job job, AnalysisLimits newLimits, Voi Long newModelMemoryLimit = newLimits.getModelMemoryLimit(); voidChainTaskExecutor.add(listener -> { if (isJobOpen(clusterService.state(), job.getId())) { - listener.onFailure(ExceptionsHelper.badRequestException("Cannot update " + Job.ANALYSIS_LIMITS.getPreferredName() - + " while the job is open")); + listener.onFailure( + ExceptionsHelper.badRequestException( + "Cannot update " + Job.ANALYSIS_LIMITS.getPreferredName() + " while the job is open" + ) + ); return; } jobResultsProvider.modelSizeStats(job.getId(), modelSizeStats -> { if (modelSizeStats != null) { ByteSizeValue modelSize = ByteSizeValue.ofBytes(modelSizeStats.getModelBytes()); if (newModelMemoryLimit < modelSize.getMb()) { - listener.onFailure(ExceptionsHelper.badRequestException( - Messages.getMessage(Messages.JOB_CONFIG_UPDATE_ANALYSIS_LIMITS_MODEL_MEMORY_LIMIT_CANNOT_BE_DECREASED, - ByteSizeValue.ofMb(modelSize.getMb()), - ByteSizeValue.ofMb(newModelMemoryLimit)))); + listener.onFailure( + ExceptionsHelper.badRequestException( + Messages.getMessage( + Messages.JOB_CONFIG_UPDATE_ANALYSIS_LIMITS_MODEL_MEMORY_LIMIT_CANNOT_BE_DECREASED, + ByteSizeValue.ofMb(modelSize.getMb()), + ByteSizeValue.ofMb(newModelMemoryLimit) + ) + ) + ); return; } } @@ -603,37 +620,40 @@ private Set openJobIds(ClusterState clusterState) { return MlTasks.openJobIds(persistentTasks); } - public void notifyFilterChanged(MlFilter filter, Set addedItems, Set removedItems, - ActionListener updatedListener) { + public void notifyFilterChanged( + MlFilter filter, + Set addedItems, + Set removedItems, + ActionListener updatedListener + ) { if (addedItems.isEmpty() && removedItems.isEmpty()) { updatedListener.onResponse(Boolean.TRUE); return; } - jobConfigProvider.findJobsWithCustomRules(ActionListener.wrap( - jobBuilders -> { - threadPool.executor(MachineLearning.UTILITY_THREAD_POOL_NAME).execute(() -> { - for (Job job: jobBuilders) { - Set jobFilters = job.getAnalysisConfig().extractReferencedFilters(); - ClusterState clusterState = clusterService.state(); - if (jobFilters.contains(filter.getId())) { - if (isJobOpen(clusterState, job.getId())) { - updateJobProcessNotifier.submitJobUpdate(UpdateParams.filterUpdate(job.getId(), filter), - ActionListener.wrap(isUpdated -> { - auditFilterChanges(job.getId(), filter.getId(), addedItems, removedItems); - }, e -> { - })); - } else { - auditFilterChanges(job.getId(), filter.getId(), addedItems, removedItems); - } - } + jobConfigProvider.findJobsWithCustomRules(ActionListener.wrap(jobBuilders -> { + threadPool.executor(MachineLearning.UTILITY_THREAD_POOL_NAME).execute(() -> { + for (Job job : jobBuilders) { + Set jobFilters = job.getAnalysisConfig().extractReferencedFilters(); + ClusterState clusterState = clusterService.state(); + if (jobFilters.contains(filter.getId())) { + if (isJobOpen(clusterState, job.getId())) { + updateJobProcessNotifier.submitJobUpdate( + UpdateParams.filterUpdate(job.getId(), filter), + ActionListener.wrap( + isUpdated -> { auditFilterChanges(job.getId(), filter.getId(), addedItems, removedItems); }, + e -> {} + ) + ); + } else { + auditFilterChanges(job.getId(), filter.getId(), addedItems, removedItems); } + } + } - updatedListener.onResponse(Boolean.TRUE); - }); - }, - updatedListener::onFailure - )); + updatedListener.onResponse(Boolean.TRUE); + }); + }, updatedListener::onFailure)); } private void auditFilterChanges(String jobId, String filterId, Set addedItems, Set removedItems) { @@ -672,35 +692,35 @@ public void updateProcessOnCalendarChanged(List calendarJobIds, ActionLi } // calendarJobIds may be a group or job - jobConfigProvider.expandGroupIds(calendarJobIds, ActionListener.wrap( - expandedIds -> { - threadPool.executor(MachineLearning.UTILITY_THREAD_POOL_NAME).execute(() -> { - // Merge the expended group members with the request Ids. - // Ids that aren't jobs will be filtered by isJobOpen() - expandedIds.addAll(calendarJobIds); - - for (String jobId : expandedIds) { - if (isJobOpen(clusterState, jobId)) { - updateJobProcessNotifier.submitJobUpdate(UpdateParams.scheduledEventsUpdate(jobId), ActionListener.wrap( - isUpdated -> { - if (isUpdated) { - auditor.info(jobId, Messages.getMessage(Messages.JOB_AUDIT_CALENDARS_UPDATED_ON_PROCESS)); - } - }, - e -> logger.error("[" + jobId + "] failed submitting process update on calendar change", e) - )); - } - } + jobConfigProvider.expandGroupIds(calendarJobIds, ActionListener.wrap(expandedIds -> { + threadPool.executor(MachineLearning.UTILITY_THREAD_POOL_NAME).execute(() -> { + // Merge the expended group members with the request Ids. + // Ids that aren't jobs will be filtered by isJobOpen() + expandedIds.addAll(calendarJobIds); + + for (String jobId : expandedIds) { + if (isJobOpen(clusterState, jobId)) { + updateJobProcessNotifier.submitJobUpdate( + UpdateParams.scheduledEventsUpdate(jobId), + ActionListener.wrap(isUpdated -> { + if (isUpdated) { + auditor.info(jobId, Messages.getMessage(Messages.JOB_AUDIT_CALENDARS_UPDATED_ON_PROCESS)); + } + }, e -> logger.error("[" + jobId + "] failed submitting process update on calendar change", e)) + ); + } + } - updateListener.onResponse(Boolean.TRUE); - }); - }, - updateListener::onFailure - )); + updateListener.onResponse(Boolean.TRUE); + }); + }, updateListener::onFailure)); } - public void revertSnapshot(RevertModelSnapshotAction.Request request, ActionListener actionListener, - ModelSnapshot modelSnapshot) { + public void revertSnapshot( + RevertModelSnapshotAction.Request request, + ActionListener actionListener, + ModelSnapshot modelSnapshot + ) { final ModelSizeStats modelSizeStats = modelSnapshot.getModelSizeStats(); @@ -712,13 +732,16 @@ public void revertSnapshot(RevertModelSnapshotAction.Request request, ActionList actionListener.onResponse(new RevertModelSnapshotAction.Response(modelSnapshot)); return; } - jobResultsPersister.persistQuantiles(modelSnapshot.getQuantiles(), WriteRequest.RefreshPolicy.IMMEDIATE, - ActionListener.wrap(quantilesResponse -> { - // The quantiles can be large, and totally dominate the output - - // it's clearer to remove them as they are not necessary for the revert op - ModelSnapshot snapshotWithoutQuantiles = new ModelSnapshot.Builder(modelSnapshot).setQuantiles(null).build(); - actionListener.onResponse(new RevertModelSnapshotAction.Response(snapshotWithoutQuantiles)); - }, actionListener::onFailure)); + jobResultsPersister.persistQuantiles( + modelSnapshot.getQuantiles(), + WriteRequest.RefreshPolicy.IMMEDIATE, + ActionListener.wrap(quantilesResponse -> { + // The quantiles can be large, and totally dominate the output - + // it's clearer to remove them as they are not necessary for the revert op + ModelSnapshot snapshotWithoutQuantiles = new ModelSnapshot.Builder(modelSnapshot).setQuantiles(null).build(); + actionListener.onResponse(new RevertModelSnapshotAction.Response(snapshotWithoutQuantiles)); + }, actionListener::onFailure) + ); }; // Step 2. When the model_snapshot_id is updated on the job, persist the snapshot's model size stats with a touched log time @@ -727,25 +750,22 @@ public void revertSnapshot(RevertModelSnapshotAction.Request request, ActionList CheckedConsumer updateHandler = response -> { if (response) { ModelSizeStats revertedModelSizeStats = new ModelSizeStats.Builder(modelSizeStats).setLogTime(new Date()).build(); - jobResultsPersister.persistModelSizeStats(revertedModelSizeStats, WriteRequest.RefreshPolicy.IMMEDIATE, ActionListener.wrap( - modelSizeStatsResponseHandler, actionListener::onFailure)); + jobResultsPersister.persistModelSizeStats( + revertedModelSizeStats, + WriteRequest.RefreshPolicy.IMMEDIATE, + ActionListener.wrap(modelSizeStatsResponseHandler, actionListener::onFailure) + ); } }; // Step 1. update the job // ------- - JobUpdate update = new JobUpdate.Builder(request.getJobId()) - .setModelSnapshotId(modelSnapshot.getSnapshotId()) - .build(); - - jobConfigProvider.updateJob(request.getJobId(), update, maxModelMemoryLimit, - ActionListener.wrap(job -> { - auditor.info(request.getJobId(), - Messages.getMessage(Messages.JOB_AUDIT_REVERTED, modelSnapshot.getDescription())); - updateHandler.accept(Boolean.TRUE); - }, - actionListener::onFailure - )); + JobUpdate update = new JobUpdate.Builder(request.getJobId()).setModelSnapshotId(modelSnapshot.getSnapshotId()).build(); + + jobConfigProvider.updateJob(request.getJobId(), update, maxModelMemoryLimit, ActionListener.wrap(job -> { + auditor.info(request.getJobId(), Messages.getMessage(Messages.JOB_AUDIT_REVERTED, modelSnapshot.getDescription())); + updateHandler.accept(Boolean.TRUE); + }, actionListener::onFailure)); } public void updateJobBlockReason(String jobId, Blocked blocked, ActionListener listener) { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/JobNodeSelector.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/JobNodeSelector.java index 42a9c3cf90703..1c7d34f83b06b 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/JobNodeSelector.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/JobNodeSelector.java @@ -12,8 +12,8 @@ import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.common.Strings; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.core.Tuple; import org.elasticsearch.persistent.PersistentTasksCustomMetadata; import org.elasticsearch.xpack.ml.MachineLearning; import org.elasticsearch.xpack.ml.autoscaling.MlAutoscalingDeciderService; @@ -52,19 +52,18 @@ */ public class JobNodeSelector { - public static final PersistentTasksCustomMetadata.Assignment AWAITING_LAZY_ASSIGNMENT = - new PersistentTasksCustomMetadata.Assignment(null, "persistent task is awaiting node assignment."); + public static final PersistentTasksCustomMetadata.Assignment AWAITING_LAZY_ASSIGNMENT = new PersistentTasksCustomMetadata.Assignment( + null, + "persistent task is awaiting node assignment." + ); private static final Logger logger = LogManager.getLogger(JobNodeSelector.class); private static String createReason(String job, String node, String msg, Object... params) { - String preamble = String.format( - Locale.ROOT, - "Not opening job [%s] on node [%s]. Reason: ", - job, - node); + String preamble = String.format(Locale.ROOT, "Not opening job [%s] on node [%s]. Reason: ", job, node); return preamble + ParameterizedMessage.format(msg, params); } + private final String jobId; private final String taskName; private final ClusterState clusterState; @@ -79,13 +78,15 @@ private static String createReason(String job, String node, String msg, Object.. * reasons why a job cannot be assigned to a particular node. May * be null if no such function is needed. */ - public JobNodeSelector(ClusterState clusterState, - Collection candidateNodes, - String jobId, - String taskName, - MlMemoryTracker memoryTracker, - int maxLazyNodes, - Function nodeFilter) { + public JobNodeSelector( + ClusterState clusterState, + Collection candidateNodes, + String jobId, + String taskName, + MlMemoryTracker memoryTracker, + int maxLazyNodes, + Function nodeFilter + ) { this.jobId = Objects.requireNonNull(jobId); this.taskName = Objects.requireNonNull(taskName); this.clusterState = Objects.requireNonNull(clusterState); @@ -101,9 +102,11 @@ public JobNodeSelector(ClusterState clusterState, }; } - public Tuple perceivedCapacityAndMaxFreeMemory(int maxMachineMemoryPercent, - boolean useAutoMemoryPercentage, - int maxOpenJobs) { + public Tuple perceivedCapacityAndMaxFreeMemory( + int maxMachineMemoryPercent, + boolean useAutoMemoryPercentage, + int maxOpenJobs + ) { List capableNodes = candidateNodes.stream() .filter(n -> this.nodeFilter.apply(n) == null) .collect(Collectors.toList()); @@ -113,13 +116,7 @@ public Tuple perceivedCapacityAndMaxFreeMemory(int m useAutoMemoryPercentage ); long mostAvailableMemory = capableNodes.stream() - .map(n -> nodeLoadDetector.detectNodeLoad( - clusterState, - n, - maxOpenJobs, - maxMachineMemoryPercent, - useAutoMemoryPercentage) - ) + .map(n -> nodeLoadDetector.detectNodeLoad(clusterState, n, maxOpenJobs, maxMachineMemoryPercent, useAutoMemoryPercentage)) .filter(nl -> nl.remainingJobs() > 0) .mapToLong(NodeLoad::getFreeMemory) .max() @@ -127,11 +124,13 @@ public Tuple perceivedCapacityAndMaxFreeMemory(int m return Tuple.tuple(currentCapacityForMl, mostAvailableMemory); } - public PersistentTasksCustomMetadata.Assignment selectNode(int dynamicMaxOpenJobs, - int maxConcurrentJobAllocations, - int maxMachineMemoryPercent, - long maxNodeSize, - boolean useAutoMemoryPercentage) { + public PersistentTasksCustomMetadata.Assignment selectNode( + int dynamicMaxOpenJobs, + int maxConcurrentJobAllocations, + int maxMachineMemoryPercent, + long maxNodeSize, + boolean useAutoMemoryPercentage + ) { final Long estimatedMemoryFootprint = memoryTracker.getJobMemoryRequirement(taskName, jobId); return selectNode( estimatedMemoryFootprint, @@ -143,12 +142,14 @@ public PersistentTasksCustomMetadata.Assignment selectNode(int dynamicMaxOpenJob ); } - public PersistentTasksCustomMetadata.Assignment selectNode(Long estimatedMemoryFootprint, - int dynamicMaxOpenJobs, - int maxConcurrentJobAllocations, - int maxMachineMemoryPercent, - long maxNodeSize, - boolean useAutoMemoryPercentage) { + public PersistentTasksCustomMetadata.Assignment selectNode( + Long estimatedMemoryFootprint, + int dynamicMaxOpenJobs, + int maxConcurrentJobAllocations, + int maxMachineMemoryPercent, + long maxNodeSize, + boolean useAutoMemoryPercentage + ) { if (estimatedMemoryFootprint == null) { memoryTracker.asyncRefresh(); String reason = "Not opening job [" + jobId + "] because job memory requirements are stale - refresh requested"; @@ -186,43 +187,51 @@ public PersistentTasksCustomMetadata.Assignment selectNode(Long estimatedMemoryF int maxNumberOfOpenJobs = currentLoad.getMaxJobs(); if (currentLoad.getNumAllocatingJobs() >= maxConcurrentJobAllocations) { - reason = createReason(jobId, + reason = createReason( + jobId, nodeNameAndMlAttributes(node), "Node exceeds [{}] the maximum number of jobs [{}] in opening state.", currentLoad.getNumAllocatingJobs(), - maxConcurrentJobAllocations); + maxConcurrentJobAllocations + ); logger.trace(reason); reasons.put(node.getName(), reason); continue; } if (currentLoad.remainingJobs() == 0) { - reason = createReason(jobId, + reason = createReason( + jobId, nodeNameAndMlAttributes(node), "This node is full. Number of opened jobs and allocated native inference processes [{}], {} [{}].", currentLoad.getNumAssignedJobs(), MAX_OPEN_JOBS_PER_NODE.getKey(), - maxNumberOfOpenJobs); + maxNumberOfOpenJobs + ); logger.trace(reason); reasons.put(node.getName(), reason); continue; } if (canAllocateByMemory == false) { - reason = createReason(jobId, + reason = createReason( + jobId, nodeNameAndMlAttributes(node), - "This node is not providing accurate information to determine is load by memory."); + "This node is not providing accurate information to determine is load by memory." + ); logger.trace(reason); - reasons.put(node.getName(),reason); + reasons.put(node.getName(), reason); continue; } if (currentLoad.getMaxMlMemory() <= 0) { - reason = createReason(jobId, + reason = createReason( + jobId, nodeNameAndMlAttributes(node), - "This node is indicating that it has no native memory for machine learning."); + "This node is indicating that it has no native memory for machine learning." + ); logger.trace(reason); - reasons.put(node.getName(),reason); + reasons.put(node.getName(), reason); continue; } @@ -233,7 +242,8 @@ public PersistentTasksCustomMetadata.Assignment selectNode(Long estimatedMemoryF } long availableMemory = currentLoad.getMaxMlMemory() - currentLoad.getAssignedJobMemory(); if (requiredMemoryForJob > availableMemory) { - reason = createReason(jobId, + reason = createReason( + jobId, nodeNameAndMlAttributes(node), "This node has insufficient available memory. Available memory for ML [{} ({})], " + "memory required by existing jobs [{} ({})], " @@ -243,9 +253,10 @@ public PersistentTasksCustomMetadata.Assignment selectNode(Long estimatedMemoryF currentLoad.getAssignedJobMemory(), ByteSizeValue.ofBytes(currentLoad.getAssignedJobMemory()).toString(), requiredMemoryForJob, - ByteSizeValue.ofBytes(requiredMemoryForJob).toString()); + ByteSizeValue.ofBytes(requiredMemoryForJob).toString() + ); logger.trace(reason); - reasons.put(node.getName(),reason); + reasons.put(node.getName(), reason); continue; } @@ -259,26 +270,29 @@ public PersistentTasksCustomMetadata.Assignment selectNode(Long estimatedMemoryF estimatedMemoryFootprint, minLoadedNodeByMemory, reasons.values(), - maxNodeSize > 0L ? - NativeMemoryCalculator.allowedBytesForMl(maxNodeSize, maxMachineMemoryPercent, useAutoMemoryPercentage) : - Long.MAX_VALUE); + maxNodeSize > 0L + ? NativeMemoryCalculator.allowedBytesForMl(maxNodeSize, maxMachineMemoryPercent, useAutoMemoryPercentage) + : Long.MAX_VALUE + ); } - PersistentTasksCustomMetadata.Assignment createAssignment(long estimatedMemoryUsage, - DiscoveryNode minLoadedNode, - Collection reasons, - long biggestPossibleJob) { + PersistentTasksCustomMetadata.Assignment createAssignment( + long estimatedMemoryUsage, + DiscoveryNode minLoadedNode, + Collection reasons, + long biggestPossibleJob + ) { if (minLoadedNode == null) { String explanation = String.join("|", reasons); - PersistentTasksCustomMetadata.Assignment currentAssignment = - new PersistentTasksCustomMetadata.Assignment(null, explanation); + PersistentTasksCustomMetadata.Assignment currentAssignment = new PersistentTasksCustomMetadata.Assignment(null, explanation); logger.debug("no node selected for job [{}], reasons [{}]", jobId, explanation); if ((MachineLearning.NATIVE_EXECUTABLE_CODE_OVERHEAD.getBytes() + estimatedMemoryUsage) > biggestPossibleJob) { ParameterizedMessage message = new ParameterizedMessage( "[{}] not waiting for node assignment as estimated job size [{}] is greater than largest possible job size [{}]", jobId, MachineLearning.NATIVE_EXECUTABLE_CODE_OVERHEAD.getBytes() + estimatedMemoryUsage, - biggestPossibleJob); + biggestPossibleJob + ); logger.info(message); List newReasons = new ArrayList<>(reasons); newReasons.add(message.getFormattedMessage()); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/NodeLoad.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/NodeLoad.java index c86dba425f0da..af4bfec8b042b 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/NodeLoad.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/NodeLoad.java @@ -28,14 +28,16 @@ public class NodeLoad { private final long assignedJobMemory; private final long numAllocatingJobs; - NodeLoad(long maxMemory, - int maxJobs, - String nodeId, - boolean useMemory, - String error, - long numAssignedJobs, - long assignedJobMemory, - long numAllocatingJobs) { + NodeLoad( + long maxMemory, + int maxJobs, + String nodeId, + boolean useMemory, + String error, + long numAssignedJobs, + long assignedJobMemory, + long numAllocatingJobs + ) { this.maxMemory = maxMemory; this.maxJobs = maxJobs; this.nodeId = nodeId; @@ -99,7 +101,7 @@ public long getFreeMemory() { * @return The number of jobs that can still be assigned to the node */ public int remainingJobs() { - return Math.max(maxJobs - (int)numAssignedJobs, 0); + return Math.max(maxJobs - (int) numAssignedJobs, 0); } /** @@ -122,14 +124,14 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; NodeLoad nodeLoad = (NodeLoad) o; - return maxMemory == nodeLoad.maxMemory && - maxJobs == nodeLoad.maxJobs && - useMemory == nodeLoad.useMemory && - numAssignedJobs == nodeLoad.numAssignedJobs && - assignedJobMemory == nodeLoad.assignedJobMemory && - numAllocatingJobs == nodeLoad.numAllocatingJobs && - Objects.equals(nodeId, nodeLoad.nodeId) && - Objects.equals(error, nodeLoad.error); + return maxMemory == nodeLoad.maxMemory + && maxJobs == nodeLoad.maxJobs + && useMemory == nodeLoad.useMemory + && numAssignedJobs == nodeLoad.numAssignedJobs + && assignedJobMemory == nodeLoad.assignedJobMemory + && numAllocatingJobs == nodeLoad.numAllocatingJobs + && Objects.equals(nodeId, nodeLoad.nodeId) + && Objects.equals(error, nodeLoad.error); } @Override @@ -175,7 +177,7 @@ public long getFreeMemory() { } public int remainingJobs() { - return Math.max(maxJobs - (int)numAssignedJobs, 0); + return Math.max(maxJobs - (int) numAssignedJobs, 0); } public String getNodeId() { @@ -229,24 +231,19 @@ void addTask(String taskName, String taskId, boolean isAllocating, MlMemoryTrack Long jobMemoryRequirement = memoryTracker.getJobMemoryRequirement(taskName, taskId); if (jobMemoryRequirement == null) { useMemory = false; - logger.debug(() -> new ParameterizedMessage( - "[{}] memory requirement was not available. Calculating load by number of assigned jobs.", - taskId - )); + logger.debug( + () -> new ParameterizedMessage( + "[{}] memory requirement was not available. Calculating load by number of assigned jobs.", + taskId + ) + ); } else { assignedJobMemory += jobMemoryRequirement; } } public NodeLoad build() { - return new NodeLoad(maxMemory, - maxJobs, - nodeId, - useMemory, - error, - numAssignedJobs, - assignedJobMemory, - numAllocatingJobs); + return new NodeLoad(maxMemory, maxJobs, nodeId, useMemory, error, numAssignedJobs, assignedJobMemory, numAllocatingJobs); } } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/NodeLoadDetector.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/NodeLoadDetector.java index f160b0dcb921f..7c78024c528f2 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/NodeLoadDetector.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/NodeLoadDetector.java @@ -29,7 +29,6 @@ import java.util.OptionalLong; import java.util.stream.Collectors; - public class NodeLoadDetector { private final MlMemoryTracker mlMemoryTracker; @@ -42,11 +41,13 @@ public MlMemoryTracker getMlMemoryTracker() { return mlMemoryTracker; } - public NodeLoad detectNodeLoad(ClusterState clusterState, - DiscoveryNode node, - int dynamicMaxOpenJobs, - int maxMachineMemoryPercent, - boolean useAutoMachineMemoryCalculation) { + public NodeLoad detectNodeLoad( + ClusterState clusterState, + DiscoveryNode node, + int dynamicMaxOpenJobs, + int maxMachineMemoryPercent, + boolean useAutoMachineMemoryCalculation + ) { return detectNodeLoad( clusterState, TrainedModelAllocationMetadata.fromState(clusterState), @@ -57,23 +58,25 @@ public NodeLoad detectNodeLoad(ClusterState clusterState, ); } - public NodeLoad detectNodeLoad(ClusterState clusterState, - TrainedModelAllocationMetadata allocationMetadata, - DiscoveryNode node, - int maxNumberOfOpenJobs, - int maxMachineMemoryPercent, - boolean useAutoMachineMemoryCalculation) { + public NodeLoad detectNodeLoad( + ClusterState clusterState, + TrainedModelAllocationMetadata allocationMetadata, + DiscoveryNode node, + int maxNumberOfOpenJobs, + int maxMachineMemoryPercent, + boolean useAutoMachineMemoryCalculation + ) { PersistentTasksCustomMetadata persistentTasks = clusterState.getMetadata().custom(PersistentTasksCustomMetadata.TYPE); Map nodeAttributes = node.getAttributes(); List errors = new ArrayList<>(); - OptionalLong maxMlMemory = NativeMemoryCalculator.allowedBytesForMl(node, - maxMachineMemoryPercent, - useAutoMachineMemoryCalculation); + OptionalLong maxMlMemory = NativeMemoryCalculator.allowedBytesForMl(node, maxMachineMemoryPercent, useAutoMachineMemoryCalculation); if (maxMlMemory.isEmpty()) { - errors.add(MachineLearning.MACHINE_MEMORY_NODE_ATTR - + " attribute [" - + nodeAttributes.get(MachineLearning.MACHINE_MEMORY_NODE_ATTR) - + "] is not a long"); + errors.add( + MachineLearning.MACHINE_MEMORY_NODE_ATTR + + " attribute [" + + nodeAttributes.get(MachineLearning.MACHINE_MEMORY_NODE_ATTR) + + "] is not a long" + ); } NodeLoad.Builder nodeLoad = NodeLoad.builder(node.getId()) @@ -91,7 +94,9 @@ public NodeLoad detectNodeLoad(ClusterState clusterState, private void updateLoadGivenTasks(NodeLoad.Builder nodeLoad, PersistentTasksCustomMetadata persistentTasks) { if (persistentTasks != null) { Collection> memoryTrackedTasks = findAllMemoryTrackedTasks( - persistentTasks, nodeLoad.getNodeId()); + persistentTasks, + nodeLoad.getNodeId() + ); for (PersistentTasksCustomMetadata.PersistentTask task : memoryTrackedTasks) { MemoryTrackedTaskState state = MlTasks.getMemoryTrackedTaskState(task); if (state == null || state.consumesMemory()) { @@ -123,8 +128,11 @@ private void updateLoadGivenModelAllocations(NodeLoad.Builder nodeLoad, TrainedM } private static Collection> findAllMemoryTrackedTasks( - PersistentTasksCustomMetadata persistentTasks, String nodeId) { - return persistentTasks.tasks().stream() + PersistentTasksCustomMetadata persistentTasks, + String nodeId + ) { + return persistentTasks.tasks() + .stream() .filter(NodeLoadDetector::isMemoryTrackedTask) .filter(task -> nodeId.equals(task.getExecutorNode())) .collect(Collectors.toList()); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/UpdateJobProcessNotifier.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/UpdateJobProcessNotifier.java index ee10eda35e03f..b03b8bf87c24d 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/UpdateJobProcessNotifier.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/UpdateJobProcessNotifier.java @@ -110,45 +110,55 @@ void executeProcessUpdates(Iterator updatesIterator) { if (update.isJobUpdate() && clusterService.localNode().isMasterNode() == false) { assert clusterService.localNode().isMasterNode(); - logger.error("Job update was submitted to non-master node [" + clusterService.getNodeName() + "]; update for job [" - + update.getJobId() + "] will be ignored"); + logger.error( + "Job update was submitted to non-master node [" + + clusterService.getNodeName() + + "]; update for job [" + + update.getJobId() + + "] will be ignored" + ); executeProcessUpdates(updatesIterator); return; } - Request request = new Request(update.getJobId(), update.getModelPlotConfig(), update.getPerPartitionCategorizationConfig(), - update.getDetectorUpdates(), update.getFilter(), update.isUpdateScheduledEvents()); - - executeAsyncWithOrigin(client, ML_ORIGIN, UpdateProcessAction.INSTANCE, request, - new ActionListener() { - @Override - public void onResponse(Response response) { - if (response.isUpdated()) { - logger.info("Successfully updated remote job [{}]", update.getJobId()); - updateHolder.listener.onResponse(true); - } else { - String msg = "Failed to update remote job [" + update.getJobId() + "]"; - logger.error(msg); - updateHolder.listener.onFailure(ExceptionsHelper.serverError(msg)); - } - executeProcessUpdates(updatesIterator); - } + Request request = new Request( + update.getJobId(), + update.getModelPlotConfig(), + update.getPerPartitionCategorizationConfig(), + update.getDetectorUpdates(), + update.getFilter(), + update.isUpdateScheduledEvents() + ); + + executeAsyncWithOrigin(client, ML_ORIGIN, UpdateProcessAction.INSTANCE, request, new ActionListener() { + @Override + public void onResponse(Response response) { + if (response.isUpdated()) { + logger.info("Successfully updated remote job [{}]", update.getJobId()); + updateHolder.listener.onResponse(true); + } else { + String msg = "Failed to update remote job [" + update.getJobId() + "]"; + logger.error(msg); + updateHolder.listener.onFailure(ExceptionsHelper.serverError(msg)); + } + executeProcessUpdates(updatesIterator); + } - @Override - public void onFailure(Exception e) { - Throwable cause = ExceptionsHelper.unwrapCause(e); - if (cause instanceof ResourceNotFoundException) { - logger.debug("Remote job [{}] not updated as it has been deleted", update.getJobId()); - } else if (cause.getMessage().contains("because job [" + update.getJobId() + "] is not open") - && cause instanceof ElasticsearchStatusException) { - logger.debug("Remote job [{}] not updated as it is no longer open", update.getJobId()); - } else { - logger.error("Failed to update remote job [" + update.getJobId() + "]", cause); - } - updateHolder.listener.onFailure(e); - executeProcessUpdates(updatesIterator); + @Override + public void onFailure(Exception e) { + Throwable cause = ExceptionsHelper.unwrapCause(e); + if (cause instanceof ResourceNotFoundException) { + logger.debug("Remote job [{}] not updated as it has been deleted", update.getJobId()); + } else if (cause.getMessage().contains("because job [" + update.getJobId() + "] is not open") + && cause instanceof ElasticsearchStatusException) { + logger.debug("Remote job [{}] not updated as it is no longer open", update.getJobId()); + } else { + logger.error("Failed to update remote job [" + update.getJobId() + "]", cause); } - }); + updateHolder.listener.onFailure(e); + executeProcessUpdates(updatesIterator); + } + }); } private static class UpdateHolder { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/categorization/AbstractMlTokenizer.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/categorization/AbstractMlTokenizer.java index cb200dd083050..c701216b1984b 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/categorization/AbstractMlTokenizer.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/categorization/AbstractMlTokenizer.java @@ -27,8 +27,7 @@ public abstract class AbstractMlTokenizer extends Tokenizer { protected int nextOffset; protected int skippedPositions; - protected AbstractMlTokenizer() { - } + protected AbstractMlTokenizer() {} @Override public final void end() throws IOException { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/categorization/CategorizationAnalyzer.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/categorization/CategorizationAnalyzer.java index 6147bc0256ca5..fb7b30b8ffd91 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/categorization/CategorizationAnalyzer.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/categorization/CategorizationAnalyzer.java @@ -30,8 +30,8 @@ public class CategorizationAnalyzer implements Releasable { private final Analyzer analyzer; private final boolean closeAnalyzer; - public CategorizationAnalyzer(AnalysisRegistry analysisRegistry, - CategorizationAnalyzerConfig categorizationAnalyzerConfig) throws IOException { + public CategorizationAnalyzer(AnalysisRegistry analysisRegistry, CategorizationAnalyzerConfig categorizationAnalyzerConfig) + throws IOException { Tuple tuple = makeAnalyzer(categorizationAnalyzerConfig, analysisRegistry); analyzer = tuple.v1(); @@ -43,8 +43,7 @@ public CategorizationAnalyzer(Analyzer analyzer, boolean closeAnalyzer) { this.closeAnalyzer = closeAnalyzer; } - public final TokenStream tokenStream(final String fieldName, - final String text) { + public final TokenStream tokenStream(final String fieldName, final String text) { return analyzer.tokenStream(fieldName, text); } @@ -112,8 +111,10 @@ private static Tuple makeAnalyzer(CategorizationAnalyzerConfi } return new Tuple<>(globalAnalyzer, Boolean.FALSE); } else { - return new Tuple<>(analysisRegistry.buildCustomAnalyzer(null, false, - config.getTokenizer(), config.getCharFilters(), config.getTokenFilters()), Boolean.TRUE); + return new Tuple<>( + analysisRegistry.buildCustomAnalyzer(null, false, config.getTokenizer(), config.getCharFilters(), config.getTokenFilters()), + Boolean.TRUE + ); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/categorization/GrokPatternCreator.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/categorization/GrokPatternCreator.java index b97d5b4215374..98e80cbc28a2b 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/categorization/GrokPatternCreator.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/categorization/GrokPatternCreator.java @@ -20,7 +20,6 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; - /** * Creates Grok patterns that will match all the examples in a given category_definition. * @@ -39,45 +38,44 @@ public final class GrokPatternCreator { * such that more generic patterns come after more specific patterns. */ private static final List ORDERED_CANDIDATE_GROK_PATTERNS = Arrays.asList( - new GrokPatternCandidate("TOMCAT_DATESTAMP", "timestamp"), - new GrokPatternCandidate("TIMESTAMP_ISO8601", "timestamp"), - new GrokPatternCandidate("DATESTAMP_RFC822", "timestamp"), - new GrokPatternCandidate("DATESTAMP_RFC2822", "timestamp"), - new GrokPatternCandidate("DATESTAMP_OTHER", "timestamp"), - new GrokPatternCandidate("DATESTAMP_EVENTLOG", "timestamp"), - new GrokPatternCandidate("SYSLOGTIMESTAMP", "timestamp"), - new GrokPatternCandidate("HTTPDATE", "timestamp"), - new GrokPatternCandidate("CATALINA_DATESTAMP", "timestamp"), - new GrokPatternCandidate("CISCOTIMESTAMP", "timestamp"), - new GrokPatternCandidate("DATE", "date"), - new GrokPatternCandidate("TIME", "time"), - new GrokPatternCandidate("LOGLEVEL", "loglevel"), - new GrokPatternCandidate("URI", "uri"), - new GrokPatternCandidate("UUID", "uuid"), - new GrokPatternCandidate("MAC", "macaddress"), - // Can't use \b as the breaks, because slashes are not "word" characters - new GrokPatternCandidate("PATH", "path", "(? examples) { // The first string in this array will end up being the empty string, and it doesn't correspond - // to an "in between" bit. Although it could be removed for "neatness", it actually makes the + // to an "in between" bit. Although it could be removed for "neatness", it actually makes the // loops below slightly neater if it's left in. // // E.g., ".*?cat.+?sat.+?mat.*" -> [ "", "cat", "sat", "mat" ] @@ -121,13 +119,18 @@ public static String findBestGrokMatchFromExamples(String jobId, String regex, C } } else { // If we get here it implies the original categorization has produced a - // regex that doesn't match one of the examples. This can happen when - // the message was very long, and the example was truncated. In this + // regex that doesn't match one of the examples. This can happen when + // the message was very long, and the example was truncated. In this // case we will have appended an ellipsis to indicate truncation. assert example.endsWith("...") : exampleProcessor.pattern() + " did not match non-truncated example " + example; if (example.endsWith("...")) { - logger.trace(() -> new ParameterizedMessage("[{}] Pattern [{}] did not match truncated example", - jobId, exampleProcessor.pattern())); + logger.trace( + () -> new ParameterizedMessage( + "[{}] Pattern [{}] did not match truncated example", + jobId, + exampleProcessor.pattern() + ) + ); } else { logger.warn("[{}] Pattern [{}] did not match non-truncated example [{}]", jobId, exampleProcessor.pattern(), example); } @@ -142,19 +145,27 @@ public static String findBestGrokMatchFromExamples(String jobId, String regex, C // Remember (from the first comment in this method) that the first element in this array is // always the empty string overallGrokPatternBuilder.append(fixedRegexBits[inBetweenBitNum]); - appendBestGrokMatchForStrings(jobId, fieldNameCountStore, overallGrokPatternBuilder, inBetweenBitNum == 0, - inBetweenBitNum == fixedRegexBits.length - 1, groupsMatchesFromExamples.get(inBetweenBitNum)); + appendBestGrokMatchForStrings( + jobId, + fieldNameCountStore, + overallGrokPatternBuilder, + inBetweenBitNum == 0, + inBetweenBitNum == fixedRegexBits.length - 1, + groupsMatchesFromExamples.get(inBetweenBitNum) + ); } return overallGrokPatternBuilder.toString(); } - private static void appendBestGrokMatchForStrings(String jobId, - Map fieldNameCountStore, - StringBuilder overallGrokPatternBuilder, - boolean isFirst, - boolean isLast, - Collection mustMatchStrings, - int numRecurse) { + private static void appendBestGrokMatchForStrings( + String jobId, + Map fieldNameCountStore, + StringBuilder overallGrokPatternBuilder, + boolean isFirst, + boolean isLast, + Collection mustMatchStrings, + int numRecurse + ) { GrokPatternCandidate bestCandidate = null; if (mustMatchStrings.isEmpty() == false) { @@ -181,36 +192,29 @@ private static void appendBestGrokMatchForStrings(String jobId, Collection prefaces = new ArrayList<>(); Collection epilogues = new ArrayList<>(); populatePrefacesAndEpilogues(mustMatchStrings, bestCandidate.grok, prefaces, epilogues); - appendBestGrokMatchForStrings(jobId, - fieldNameCountStore, - overallGrokPatternBuilder, - isFirst, - false, - prefaces, - numRecurse + 1); - overallGrokPatternBuilder.append("%{").append(bestCandidate.grokPatternName).append(':') - .append(buildFieldName(fieldNameCountStore, bestCandidate.fieldName)).append('}'); - appendBestGrokMatchForStrings(jobId, - fieldNameCountStore, - overallGrokPatternBuilder, - false, isLast, - epilogues, - numRecurse + 1); + appendBestGrokMatchForStrings(jobId, fieldNameCountStore, overallGrokPatternBuilder, isFirst, false, prefaces, numRecurse + 1); + overallGrokPatternBuilder.append("%{") + .append(bestCandidate.grokPatternName) + .append(':') + .append(buildFieldName(fieldNameCountStore, bestCandidate.fieldName)) + .append('}'); + appendBestGrokMatchForStrings(jobId, fieldNameCountStore, overallGrokPatternBuilder, false, isLast, epilogues, numRecurse + 1); } } - /** * Given a collection of strings, work out which (if any) of the grok patterns we're allowed * to use matches it best. Then append the appropriate grok language to represent that finding * onto the supplied string builder. */ - static void appendBestGrokMatchForStrings(String jobId, - Map fieldNameCountStore, - StringBuilder overallGrokPatternBuilder, - boolean isFirst, - boolean isLast, - Collection mustMatchStrings) { + static void appendBestGrokMatchForStrings( + String jobId, + Map fieldNameCountStore, + StringBuilder overallGrokPatternBuilder, + boolean isFirst, + boolean isLast, + Collection mustMatchStrings + ) { appendBestGrokMatchForStrings(jobId, fieldNameCountStore, overallGrokPatternBuilder, isFirst, isLast, mustMatchStrings, 0); } @@ -219,11 +223,15 @@ static void appendBestGrokMatchForStrings(String jobId, * return collections of the bits that come before (prefaces) and after (epilogues) the * bit that matches. */ - static void populatePrefacesAndEpilogues(Collection matchingStrings, Grok grok, Collection prefaces, - Collection epilogues) { + static void populatePrefacesAndEpilogues( + Collection matchingStrings, + Grok grok, + Collection prefaces, + Collection epilogues + ) { for (String s : matchingStrings) { Map captures = grok.captures(s); - // If the pattern doesn't match then captures will be null. But we expect this + // If the pattern doesn't match then captures will be null. But we expect this // method to only be called after validating that the pattern does match. assert captures != null; prefaces.add(captures.getOrDefault(PREFACE, "").toString()); @@ -280,8 +288,11 @@ static class GrokPatternCandidate { GrokPatternCandidate(String grokPatternName, String fieldName, String preBreak, String postBreak) { this.grokPatternName = grokPatternName; this.fieldName = fieldName; - this.grok = new Grok(Grok.getBuiltinPatterns(false), "%{DATA:" + PREFACE + "}" + preBreak + "%{" + grokPatternName + ":this}" + - postBreak + "%{GREEDYDATA:" + EPILOGUE + "}", logger::warn); + this.grok = new Grok( + Grok.getBuiltinPatterns(false), + "%{DATA:" + PREFACE + "}" + preBreak + "%{" + grokPatternName + ":this}" + postBreak + "%{GREEDYDATA:" + EPILOGUE + "}", + logger::warn + ); } } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/categorization/MlClassicTokenizer.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/categorization/MlClassicTokenizer.java index d5d0c8d61bbc2..89533255528f2 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/categorization/MlClassicTokenizer.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/categorization/MlClassicTokenizer.java @@ -8,7 +8,6 @@ import java.io.IOException; - /** * Java port of the classic ML categorization tokenizer, as implemented in the ML C++ code. * @@ -18,8 +17,7 @@ public class MlClassicTokenizer extends AbstractMlTokenizer { public static String NAME = "ml_classic"; - MlClassicTokenizer() { - } + MlClassicTokenizer() {} /** * Basically tokenize into [a-zA-Z0-9]+ strings, but also allowing underscores, dots and dashes in the middle. @@ -47,8 +45,8 @@ public final boolean incrementToken() throws IOException { // We don't return tokens that are hex numbers, and it's most efficient to keep a running note of this haveNonHex = haveNonHex || - // Count dots and dashes as numeric - (Character.digit(curChar, 16) == -1 && curChar != '.' && curChar != '-'); + // Count dots and dashes as numeric + (Character.digit(curChar, 16) == -1 && curChar != '.' && curChar != '-'); } else if (length > 0) { // If we get here, we've found a separator character having built up a candidate token diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/categorization/MlClassicTokenizerFactory.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/categorization/MlClassicTokenizerFactory.java index c01492a0d2761..0ad3444884f14 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/categorization/MlClassicTokenizerFactory.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/categorization/MlClassicTokenizerFactory.java @@ -12,7 +12,6 @@ import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.analysis.AbstractTokenizerFactory; - /** * Factory for the classic ML categorization tokenizer, as implemented in the ML C++ code. * diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/categorization/MlStandardTokenizer.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/categorization/MlStandardTokenizer.java index eec2ff54332a3..ebb1b646a4c54 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/categorization/MlStandardTokenizer.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/categorization/MlStandardTokenizer.java @@ -22,8 +22,7 @@ public class MlStandardTokenizer extends AbstractMlTokenizer { private int putBackChar = -1; - MlStandardTokenizer() { - } + MlStandardTokenizer() {} /** * Basically tokenize into [a-zA-Z0-9]+ strings, but also allowing forward slashes, and underscores, dots and dashes in the middle. @@ -50,8 +49,12 @@ public final boolean incrementToken() throws IOException { while ((curChar = getNextChar()) >= 0) { ++nextOffset; if (Character.isLetterOrDigit(curChar) - || (length > 0 && (curChar == '_' || curChar == '.' || curChar == '-' || curChar == '@' || - (curChar == ':' && lettersBeforeColon == length))) + || (length > 0 + && (curChar == '_' + || curChar == '.' + || curChar == '-' + || curChar == '@' + || (curChar == ':' && lettersBeforeColon == length))) || curChar == '/' || (curChar == '\\' && (length == 0 || (haveColon && lettersBeforeColon == 1) || firstBackslashPos == 0))) { if (length == 0) { @@ -91,7 +94,7 @@ public final boolean incrementToken() throws IOException { // We don't return tokens that are hex numbers, and it's most efficient to keep a running note of this haveNonHex = haveNonHex || - // Count dots, dashes, at symbols and colons as numeric + // Count dots, dashes, at symbols and colons as numeric (Character.digit(curChar, 16) == -1 && curChar != '.' && curChar != '-' && curChar != '@' && curChar != ':'); } else if (length > 0) { // If we get here, we've found a separator character having built up a candidate token diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/BatchedBucketsIterator.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/BatchedBucketsIterator.java index 6668ea6d02c12..878d94beea695 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/BatchedBucketsIterator.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/BatchedBucketsIterator.java @@ -10,11 +10,11 @@ import org.elasticsearch.client.OriginSettingClient; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.search.SearchHit; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.search.SearchHit; import org.elasticsearch.xpack.core.ml.job.results.Bucket; import org.elasticsearch.xpack.core.ml.job.results.Result; @@ -30,9 +30,11 @@ class BatchedBucketsIterator extends BatchedResultsIterator { @Override protected Result map(SearchHit hit) { BytesReference source = hit.getSourceRef(); - try (InputStream stream = source.streamInput(); - XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(NamedXContentRegistry.EMPTY, - LoggingDeprecationHandler.INSTANCE, stream)) { + try ( + InputStream stream = source.streamInput(); + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream) + ) { Bucket bucket = Bucket.LENIENT_PARSER.apply(parser, null); return new Result<>(hit.getIndex(), bucket); } catch (IOException e) { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/BatchedInfluencersIterator.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/BatchedInfluencersIterator.java index 00a38699a7969..727086292c6b9 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/BatchedInfluencersIterator.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/BatchedInfluencersIterator.java @@ -10,11 +10,11 @@ import org.elasticsearch.client.OriginSettingClient; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.search.SearchHit; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.search.SearchHit; import org.elasticsearch.xpack.core.ml.job.results.Influencer; import org.elasticsearch.xpack.core.ml.job.results.Result; @@ -29,9 +29,11 @@ class BatchedInfluencersIterator extends BatchedResultsIterator { @Override protected Result map(SearchHit hit) { BytesReference source = hit.getSourceRef(); - try (InputStream stream = source.streamInput(); - XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(NamedXContentRegistry.EMPTY, - LoggingDeprecationHandler.INSTANCE, stream)) { + try ( + InputStream stream = source.streamInput(); + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream) + ) { Influencer influencer = Influencer.LENIENT_PARSER.apply(parser, null); return new Result<>(hit.getIndex(), influencer); } catch (IOException e) { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/BatchedRecordsIterator.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/BatchedRecordsIterator.java index b91436c2d1d8e..5c506b3d8537f 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/BatchedRecordsIterator.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/BatchedRecordsIterator.java @@ -10,11 +10,11 @@ import org.elasticsearch.client.OriginSettingClient; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.search.SearchHit; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.search.SearchHit; import org.elasticsearch.xpack.core.ml.job.results.AnomalyRecord; import org.elasticsearch.xpack.core.ml.job.results.Result; @@ -30,9 +30,11 @@ class BatchedRecordsIterator extends BatchedResultsIterator { @Override protected Result map(SearchHit hit) { BytesReference source = hit.getSourceRef(); - try (InputStream stream = source.streamInput(); - XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(NamedXContentRegistry.EMPTY, - LoggingDeprecationHandler.INSTANCE, stream)){ + try ( + InputStream stream = source.streamInput(); + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream) + ) { AnomalyRecord record = AnomalyRecord.LENIENT_PARSER.apply(parser, null); return new Result<>(hit.getIndex(), record); } catch (IOException e) { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/BucketsQueryBuilder.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/BucketsQueryBuilder.java index bea5787091a3f..99fd6430b3ace 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/BucketsQueryBuilder.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/BucketsQueryBuilder.java @@ -132,16 +132,14 @@ public SearchSourceBuilder build() { rfb.timeRange(Result.TIMESTAMP.getPreferredName(), timestamp); } else { rfb.timeRange(Result.TIMESTAMP.getPreferredName(), start, end) - .score(Bucket.ANOMALY_SCORE.getPreferredName(), anomalyScoreFilter) - .interim(includeInterim); + .score(Bucket.ANOMALY_SCORE.getPreferredName(), anomalyScoreFilter) + .interim(includeInterim); } - SortBuilder sortBuilder = new FieldSortBuilder(sortField) - .order(sortDescending ? SortOrder.DESC : SortOrder.ASC); + SortBuilder sortBuilder = new FieldSortBuilder(sortField).order(sortDescending ? SortOrder.DESC : SortOrder.ASC); - QueryBuilder boolQuery = new BoolQueryBuilder() - .filter(rfb.build()) - .filter(QueryBuilders.termQuery(Result.RESULT_TYPE.getPreferredName(), Bucket.RESULT_TYPE_VALUE)); + QueryBuilder boolQuery = new BoolQueryBuilder().filter(rfb.build()) + .filter(QueryBuilders.termQuery(Result.RESULT_TYPE.getPreferredName(), Bucket.RESULT_TYPE_VALUE)); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.sort(sortBuilder); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/CalendarQueryBuilder.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/CalendarQueryBuilder.java index 563c3e5933fcd..4b951f664a2bd 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/CalendarQueryBuilder.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/CalendarQueryBuilder.java @@ -93,9 +93,7 @@ public SearchSourceBuilder build() { jobIdAndGroups.add(Metadata.ALL); qb.filter(new TermsQueryBuilder(Calendar.JOB_IDS.getPreferredName(), jobIdAndGroups)); } - QueryBuilderHelper - .buildTokenFilterQuery(Calendar.ID.getPreferredName(), idTokens) - .ifPresent(qb::filter); + QueryBuilderHelper.buildTokenFilterQuery(Calendar.ID.getPreferredName(), idTokens).ifPresent(qb::filter); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(qb); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/InfluencersQueryBuilder.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/InfluencersQueryBuilder.java index 286034cedc11a..59930f0c95150 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/InfluencersQueryBuilder.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/InfluencersQueryBuilder.java @@ -87,7 +87,6 @@ public void clear() { influencersQuery = new InfluencersQueryBuilder.InfluencersQuery(); } - public class InfluencersQuery { private int from = 0; private int size = DEFAULT_SIZE; @@ -135,7 +134,6 @@ public int hashCode() { return Objects.hash(from, size, includeInterim, influencerScoreFilter, start, end, sortField, sortDescending); } - @Override public boolean equals(Object obj) { if (this == obj) { @@ -149,14 +147,14 @@ public boolean equals(Object obj) { } InfluencersQuery other = (InfluencersQuery) obj; - return Objects.equals(from, other.from) && - Objects.equals(size, other.size) && - Objects.equals(includeInterim, other.includeInterim) && - Objects.equals(start, other.start) && - Objects.equals(end, other.end) && - Objects.equals(influencerScoreFilter, other.influencerScoreFilter) && - Objects.equals(sortField, other.sortField) && - this.sortDescending == other.sortDescending; + return Objects.equals(from, other.from) + && Objects.equals(size, other.size) + && Objects.equals(includeInterim, other.includeInterim) + && Objects.equals(start, other.start) + && Objects.equals(end, other.end) + && Objects.equals(influencerScoreFilter, other.influencerScoreFilter) + && Objects.equals(sortField, other.sortField) + && this.sortDescending == other.sortDescending; } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/JobConfigProvider.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/JobConfigProvider.java index b27c98b1b91e8..c22e9bc7af746 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/JobConfigProvider.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/JobConfigProvider.java @@ -32,12 +32,6 @@ import org.elasticsearch.common.regex.Regex; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.core.Nullable; import org.elasticsearch.index.IndexNotFoundException; import org.elasticsearch.index.engine.VersionConflictEngineException; @@ -53,6 +47,12 @@ import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.fetch.subphase.FetchSourceContext; import org.elasticsearch.search.sort.SortOrder; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.action.util.ExpandedIdsMatcher; import org.elasticsearch.xpack.core.ml.MlConfigIndex; import org.elasticsearch.xpack.core.ml.MlTasks; @@ -121,22 +121,19 @@ public JobConfigProvider(Client client, NamedXContentRegistry xContentRegistry) public void putJob(Job job, ActionListener listener) { try (XContentBuilder builder = XContentFactory.jsonBuilder()) { XContentBuilder source = job.toXContent(builder, new ToXContent.MapParams(TO_XCONTENT_PARAMS)); - IndexRequest indexRequest = new IndexRequest(MlConfigIndex.indexName()) - .id(Job.documentId(job.getId())) - .source(source) - .opType(DocWriteRequest.OpType.CREATE) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - - executeAsyncWithOrigin(client, ML_ORIGIN, IndexAction.INSTANCE, indexRequest, ActionListener.wrap( - listener::onResponse, - e -> { - if (ExceptionsHelper.unwrapCause(e) instanceof VersionConflictEngineException) { - // the job already exists - listener.onFailure(ExceptionsHelper.jobAlreadyExists(job.getId())); - } else { - listener.onFailure(e); - } - })); + IndexRequest indexRequest = new IndexRequest(MlConfigIndex.indexName()).id(Job.documentId(job.getId())) + .source(source) + .opType(DocWriteRequest.OpType.CREATE) + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); + + executeAsyncWithOrigin(client, ML_ORIGIN, IndexAction.INSTANCE, indexRequest, ActionListener.wrap(listener::onResponse, e -> { + if (ExceptionsHelper.unwrapCause(e) instanceof VersionConflictEngineException) { + // the job already exists + listener.onFailure(ExceptionsHelper.jobAlreadyExists(job.getId())); + } else { + listener.onFailure(e); + } + })); } catch (IOException e) { listener.onFailure(new ElasticsearchParseException("Failed to serialise job with id [" + job.getId() + "]", e)); @@ -235,7 +232,7 @@ public void onResponse(GetResponse getResponse) { BytesReference source = getResponse.getSourceAsBytesRef(); Job.Builder jobBuilder; try { - jobBuilder = parseJobLenientlyFromSource(source); + jobBuilder = parseJobLenientlyFromSource(source); } catch (IOException e) { delegate.onFailure(new ElasticsearchParseException("Failed to parse job configuration [" + jobId + "]", e)); return; @@ -275,76 +272,71 @@ public interface UpdateValidator { * @param validator The job update validator * @param listener Updated job listener */ - public void updateJobWithValidation(String jobId, JobUpdate update, ByteSizeValue maxModelMemoryLimit, - UpdateValidator validator, ActionListener listener) { + public void updateJobWithValidation( + String jobId, + JobUpdate update, + ByteSizeValue maxModelMemoryLimit, + UpdateValidator validator, + ActionListener listener + ) { GetRequest getRequest = new GetRequest(MlConfigIndex.indexName(), Job.documentId(jobId)); - executeAsyncWithOrigin(client, ML_ORIGIN, GetAction.INSTANCE, getRequest, ActionListener.wrap( - getResponse -> { - if (getResponse.isExists() == false) { - listener.onFailure(ExceptionsHelper.missingJobException(jobId)); - return; - } + executeAsyncWithOrigin(client, ML_ORIGIN, GetAction.INSTANCE, getRequest, ActionListener.wrap(getResponse -> { + if (getResponse.isExists() == false) { + listener.onFailure(ExceptionsHelper.missingJobException(jobId)); + return; + } - final long seqNo = getResponse.getSeqNo(); - final long primaryTerm = getResponse.getPrimaryTerm(); - BytesReference source = getResponse.getSourceAsBytesRef(); - Job originalJob; + final long seqNo = getResponse.getSeqNo(); + final long primaryTerm = getResponse.getPrimaryTerm(); + BytesReference source = getResponse.getSourceAsBytesRef(); + Job originalJob; + try { + originalJob = parseJobLenientlyFromSource(source).build(); + } catch (Exception e) { + listener.onFailure(new ElasticsearchParseException("Failed to parse job configuration [" + jobId + "]", e)); + return; + } + + validator.validate(originalJob, update, ActionListener.wrap(validated -> { + Job updatedJob; try { - originalJob = parseJobLenientlyFromSource(source).build(); + // Applying the update may result in a validation error + updatedJob = update.mergeWithJob(originalJob, maxModelMemoryLimit); } catch (Exception e) { - listener.onFailure(new ElasticsearchParseException("Failed to parse job configuration [" + jobId + "]", e)); + listener.onFailure(e); return; } - validator.validate(originalJob, update, ActionListener.wrap( - validated -> { - Job updatedJob; - try { - // Applying the update may result in a validation error - updatedJob = update.mergeWithJob(originalJob, maxModelMemoryLimit); - } catch (Exception e) { - listener.onFailure(e); - return; - } - - indexUpdatedJob(updatedJob, seqNo, primaryTerm, listener); - }, - listener::onFailure - )); - }, - e -> { - if (ExceptionsHelper.unwrapCause(e) instanceof IndexNotFoundException) { - listener.onFailure(ExceptionsHelper.missingJobException(jobId)); - } else { - listener.onFailure(e); - } + indexUpdatedJob(updatedJob, seqNo, primaryTerm, listener); + }, listener::onFailure)); + }, e -> { + if (ExceptionsHelper.unwrapCause(e) instanceof IndexNotFoundException) { + listener.onFailure(ExceptionsHelper.missingJobException(jobId)); + } else { + listener.onFailure(e); } - )); + })); } - private void indexUpdatedJob(Job updatedJob, long seqNo, long primaryTerm, - ActionListener updatedJobListener) { + private void indexUpdatedJob(Job updatedJob, long seqNo, long primaryTerm, ActionListener updatedJobListener) { try (XContentBuilder builder = XContentFactory.jsonBuilder()) { XContentBuilder updatedSource = updatedJob.toXContent(builder, ToXContent.EMPTY_PARAMS); - IndexRequest indexRequest = new IndexRequest(MlConfigIndex.indexName()) - .id(Job.documentId(updatedJob.getId())) - .source(updatedSource) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); + IndexRequest indexRequest = new IndexRequest(MlConfigIndex.indexName()).id(Job.documentId(updatedJob.getId())) + .source(updatedSource) + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); indexRequest.setIfSeqNo(seqNo); indexRequest.setIfPrimaryTerm(primaryTerm); - executeAsyncWithOrigin(client, ML_ORIGIN, IndexAction.INSTANCE, indexRequest, ActionListener.wrap( - indexResponse -> { - assert indexResponse.getResult() == DocWriteResponse.Result.UPDATED; - updatedJobListener.onResponse(updatedJob); - }, - updatedJobListener::onFailure - )); + executeAsyncWithOrigin(client, ML_ORIGIN, IndexAction.INSTANCE, indexRequest, ActionListener.wrap(indexResponse -> { + assert indexResponse.getResult() == DocWriteResponse.Result.UPDATED; + updatedJobListener.onResponse(updatedJob); + }, updatedJobListener::onFailure)); } catch (IOException e) { updatedJobListener.onFailure( - new ElasticsearchParseException("Failed to serialise job with id [" + updatedJob.getId() + "]", e)); + new ElasticsearchParseException("Failed to serialise job with id [" + updatedJob.getId() + "]", e) + ); } } @@ -412,23 +404,25 @@ public void jobIdMatches(List ids, ActionListener> listener sourceBuilder.docValueField(Job.ID.getPreferredName(), null); SearchRequest searchRequest = client.prepareSearch(MlConfigIndex.indexName()) - .setIndicesOptions(IndicesOptions.lenientExpandOpen()) - .setSource(sourceBuilder) - .setSize(ids.size()) - .request(); - - executeAsyncWithOrigin(client.threadPool().getThreadContext(), ML_ORIGIN, searchRequest, - ActionListener.wrap( - response -> { - SearchHit[] hits = response.getHits().getHits(); - List matchedIds = new ArrayList<>(); - for (SearchHit hit : hits) { - matchedIds.add(hit.field(Job.ID.getPreferredName()).getValue()); - } - listener.onResponse(matchedIds); - }, - listener::onFailure) - , client::search); + .setIndicesOptions(IndicesOptions.lenientExpandOpen()) + .setSource(sourceBuilder) + .setSize(ids.size()) + .request(); + + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + ML_ORIGIN, + searchRequest, + ActionListener.wrap(response -> { + SearchHit[] hits = response.getHits().getHits(); + List matchedIds = new ArrayList<>(); + for (SearchHit hit : hits) { + matchedIds.add(hit.field(Job.ID.getPreferredName()).getValue()); + } + listener.onResponse(matchedIds); + }, listener::onFailure), + client::search + ); } public void updateJobBlockReason(String jobId, Blocked blocked, ActionListener listener) { @@ -437,8 +431,7 @@ public void updateJobBlockReason(String jobId, Blocked blocked, ActionListener

    listener) { - JobUpdate jobUpdate = new JobUpdate.Builder(jobId) - .setModelSnapshotId(ModelSnapshot.EMPTY_SNAPSHOT_ID) + JobUpdate jobUpdate = new JobUpdate.Builder(jobId).setModelSnapshotId(ModelSnapshot.EMPTY_SNAPSHOT_ID) .setBlocked(Blocked.none()) .setClearFinishTime(true) .build(); @@ -477,13 +470,15 @@ public void updateJobAfterReset(String jobId, ActionListener> listener) { - String [] tokens = ExpandedIdsMatcher.tokenizeExpression(expression); + public void expandJobsIds( + String expression, + boolean allowNoMatch, + boolean excludeDeleting, + @Nullable PersistentTasksCustomMetadata tasksCustomMetadata, + boolean allowMissingConfigs, + ActionListener> listener + ) { + String[] tokens = ExpandedIdsMatcher.tokenizeExpression(expression); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(buildJobWildcardQuery(tokens, excludeDeleting)); sourceBuilder.sort(Job.ID.getPreferredName()); sourceBuilder.fetchSource(false); @@ -491,42 +486,44 @@ public void expandJobsIds(String expression, sourceBuilder.docValueField(Job.GROUPS.getPreferredName(), null); SearchRequest searchRequest = client.prepareSearch(MlConfigIndex.indexName()) - .setIndicesOptions(IndicesOptions.lenientExpandOpen()) - .setSource(sourceBuilder) - .setSize(MlConfigIndex.CONFIG_INDEX_MAX_RESULTS_WINDOW) - .request(); + .setIndicesOptions(IndicesOptions.lenientExpandOpen()) + .setSource(sourceBuilder) + .setSize(MlConfigIndex.CONFIG_INDEX_MAX_RESULTS_WINDOW) + .request(); ExpandedIdsMatcher requiredMatches = new ExpandedIdsMatcher(tokens, allowNoMatch); Collection openMatchingJobs = matchingJobIdsWithTasks(tokens, tasksCustomMetadata); - executeAsyncWithOrigin(client.threadPool().getThreadContext(), ML_ORIGIN, searchRequest, - ActionListener.wrap( - response -> { - SortedSet jobIds = new TreeSet<>(); - SortedSet groupsIds = new TreeSet<>(); - SearchHit[] hits = response.getHits().getHits(); - for (SearchHit hit : hits) { - jobIds.add(hit.field(Job.ID.getPreferredName()).getValue()); - List groups = hit.field(Job.GROUPS.getPreferredName()).getValues(); - if (groups != null) { - groupsIds.addAll(groups.stream().map(Object::toString).collect(Collectors.toList())); - } - } - if (allowMissingConfigs) { - jobIds.addAll(openMatchingJobs); - } - groupsIds.addAll(jobIds); - requiredMatches.filterMatchedIds(groupsIds); - if (requiredMatches.hasUnmatchedIds()) { - // some required jobs were not found - listener.onFailure(ExceptionsHelper.missingJobException(requiredMatches.unmatchedIdsString())); - return; - } - - listener.onResponse(jobIds); - }, - listener::onFailure) - , client::search); + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + ML_ORIGIN, + searchRequest, + ActionListener.wrap(response -> { + SortedSet jobIds = new TreeSet<>(); + SortedSet groupsIds = new TreeSet<>(); + SearchHit[] hits = response.getHits().getHits(); + for (SearchHit hit : hits) { + jobIds.add(hit.field(Job.ID.getPreferredName()).getValue()); + List groups = hit.field(Job.GROUPS.getPreferredName()).getValues(); + if (groups != null) { + groupsIds.addAll(groups.stream().map(Object::toString).collect(Collectors.toList())); + } + } + if (allowMissingConfigs) { + jobIds.addAll(openMatchingJobs); + } + groupsIds.addAll(jobIds); + requiredMatches.filterMatchedIds(groupsIds); + if (requiredMatches.hasUnmatchedIds()) { + // some required jobs were not found + listener.onFailure(ExceptionsHelper.missingJobException(requiredMatches.unmatchedIdsString())); + return; + } + + listener.onResponse(jobIds); + }, listener::onFailure), + client::search + ); } @@ -544,49 +541,51 @@ public void expandJobsIds(String expression, * @param listener The expanded jobs listener */ public void expandJobs(String expression, boolean allowNoMatch, boolean excludeDeleting, ActionListener> listener) { - String [] tokens = ExpandedIdsMatcher.tokenizeExpression(expression); + String[] tokens = ExpandedIdsMatcher.tokenizeExpression(expression); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(buildJobWildcardQuery(tokens, excludeDeleting)); sourceBuilder.sort(Job.ID.getPreferredName()); SearchRequest searchRequest = client.prepareSearch(MlConfigIndex.indexName()) - .setIndicesOptions(IndicesOptions.lenientExpandOpen()) - .setSource(sourceBuilder) - .setSize(MlConfigIndex.CONFIG_INDEX_MAX_RESULTS_WINDOW) - .request(); + .setIndicesOptions(IndicesOptions.lenientExpandOpen()) + .setSource(sourceBuilder) + .setSize(MlConfigIndex.CONFIG_INDEX_MAX_RESULTS_WINDOW) + .request(); ExpandedIdsMatcher requiredMatches = new ExpandedIdsMatcher(tokens, allowNoMatch); - executeAsyncWithOrigin(client.threadPool().getThreadContext(), ML_ORIGIN, searchRequest, - ActionListener.wrap( - response -> { - List jobs = new ArrayList<>(); - Set jobAndGroupIds = new HashSet<>(); - - SearchHit[] hits = response.getHits().getHits(); - for (SearchHit hit : hits) { - try { - BytesReference source = hit.getSourceRef(); - Job.Builder job = parseJobLenientlyFromSource(source); - jobs.add(job); - jobAndGroupIds.add(job.getId()); - jobAndGroupIds.addAll(job.getGroups()); - } catch (IOException e) { - // TODO A better way to handle this rather than just ignoring the error? - logger.error("Error parsing anomaly detector job configuration [" + hit.getId() + "]", e); - } - } - - requiredMatches.filterMatchedIds(jobAndGroupIds); - if (requiredMatches.hasUnmatchedIds()) { - // some required jobs were not found - listener.onFailure(ExceptionsHelper.missingJobException(requiredMatches.unmatchedIdsString())); - return; - } - - listener.onResponse(jobs); - }, - listener::onFailure) - , client::search); + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + ML_ORIGIN, + searchRequest, + ActionListener.wrap(response -> { + List jobs = new ArrayList<>(); + Set jobAndGroupIds = new HashSet<>(); + + SearchHit[] hits = response.getHits().getHits(); + for (SearchHit hit : hits) { + try { + BytesReference source = hit.getSourceRef(); + Job.Builder job = parseJobLenientlyFromSource(source); + jobs.add(job); + jobAndGroupIds.add(job.getId()); + jobAndGroupIds.addAll(job.getGroups()); + } catch (IOException e) { + // TODO A better way to handle this rather than just ignoring the error? + logger.error("Error parsing anomaly detector job configuration [" + hit.getId() + "]", e); + } + } + + requiredMatches.filterMatchedIds(jobAndGroupIds); + if (requiredMatches.hasUnmatchedIds()) { + // some required jobs were not found + listener.onFailure(ExceptionsHelper.missingJobException(requiredMatches.unmatchedIdsString())); + return; + } + + listener.onResponse(jobs); + }, listener::onFailure), + client::search + ); } @@ -600,31 +599,32 @@ public void expandJobs(String expression, boolean allowNoMatch, boolean excludeD * @param listener Expanded job Ids listener */ public void expandGroupIds(List groupIds, ActionListener> listener) { - SearchSourceBuilder sourceBuilder = new SearchSourceBuilder() - .query(new TermsQueryBuilder(Job.GROUPS.getPreferredName(), groupIds)); + SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(new TermsQueryBuilder(Job.GROUPS.getPreferredName(), groupIds)); sourceBuilder.sort(Job.ID.getPreferredName(), SortOrder.DESC); sourceBuilder.fetchSource(false); sourceBuilder.docValueField(Job.ID.getPreferredName(), null); SearchRequest searchRequest = client.prepareSearch(MlConfigIndex.indexName()) - .setIndicesOptions(IndicesOptions.lenientExpandOpen()) - .setSource(sourceBuilder) - .setSize(MlConfigIndex.CONFIG_INDEX_MAX_RESULTS_WINDOW) - .request(); - - executeAsyncWithOrigin(client.threadPool().getThreadContext(), ML_ORIGIN, searchRequest, - ActionListener.wrap( - response -> { - SortedSet jobIds = new TreeSet<>(); - SearchHit[] hits = response.getHits().getHits(); - for (SearchHit hit : hits) { - jobIds.add(hit.field(Job.ID.getPreferredName()).getValue()); - } - - listener.onResponse(jobIds); - }, - listener::onFailure) - , client::search); + .setIndicesOptions(IndicesOptions.lenientExpandOpen()) + .setSource(sourceBuilder) + .setSize(MlConfigIndex.CONFIG_INDEX_MAX_RESULTS_WINDOW) + .request(); + + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + ML_ORIGIN, + searchRequest, + ActionListener.wrap(response -> { + SortedSet jobIds = new TreeSet<>(); + SearchHit[] hits = response.getHits().getHits(); + for (SearchHit hit : hits) { + jobIds.add(hit.field(Job.ID.getPreferredName()).getValue()); + } + + listener.onResponse(jobIds); + }, listener::onFailure), + client::search + ); } /** @@ -640,22 +640,25 @@ public void groupExists(String groupId, ActionListener listener) { boolQueryBuilder.filter(new TermQueryBuilder(Job.JOB_TYPE.getPreferredName(), Job.ANOMALY_DETECTOR_JOB_TYPE)); boolQueryBuilder.filter(new TermQueryBuilder(Job.GROUPS.getPreferredName(), groupId)); - SearchSourceBuilder sourceBuilder = new SearchSourceBuilder() - .query(boolQueryBuilder); + SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(boolQueryBuilder); sourceBuilder.fetchSource(false); SearchRequest searchRequest = client.prepareSearch(MlConfigIndex.indexName()) - .setSize(0) - .setIndicesOptions(IndicesOptions.lenientExpandOpen()) - .setSource(sourceBuilder).request(); - - executeAsyncWithOrigin(client.threadPool().getThreadContext(), ML_ORIGIN, searchRequest, - ActionListener.wrap( - response -> { - listener.onResponse(response.getHits().getTotalHits().value > 0); - }, - listener::onFailure) - , client::search); + .setSize(0) + .setIndicesOptions(IndicesOptions.lenientExpandOpen()) + .setSource(sourceBuilder) + .request(); + + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + ML_ORIGIN, + searchRequest, + ActionListener.wrap( + response -> { listener.onResponse(response.getHits().getTotalHits().value > 0); }, + listener::onFailure + ), + client::search + ); } /** @@ -663,38 +666,47 @@ public void groupExists(String groupId, ActionListener listener) { * @param listener Jobs listener */ public void findJobsWithCustomRules(ActionListener> listener) { - String customRulesPath = Strings.collectionToDelimitedString(Arrays.asList(Job.ANALYSIS_CONFIG.getPreferredName(), - AnalysisConfig.DETECTORS.getPreferredName(), Detector.CUSTOM_RULES_FIELD.getPreferredName()), "."); - SearchSourceBuilder sourceBuilder = new SearchSourceBuilder() - .query(QueryBuilders.nestedQuery(customRulesPath, QueryBuilders.existsQuery(customRulesPath), ScoreMode.None)); + String customRulesPath = Strings.collectionToDelimitedString( + Arrays.asList( + Job.ANALYSIS_CONFIG.getPreferredName(), + AnalysisConfig.DETECTORS.getPreferredName(), + Detector.CUSTOM_RULES_FIELD.getPreferredName() + ), + "." + ); + SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query( + QueryBuilders.nestedQuery(customRulesPath, QueryBuilders.existsQuery(customRulesPath), ScoreMode.None) + ); SearchRequest searchRequest = client.prepareSearch(MlConfigIndex.indexName()) - .setIndicesOptions(IndicesOptions.lenientExpandOpen()) - .setSource(sourceBuilder) - .setSize(MlConfigIndex.CONFIG_INDEX_MAX_RESULTS_WINDOW) - .request(); - - executeAsyncWithOrigin(client.threadPool().getThreadContext(), ML_ORIGIN, searchRequest, - ActionListener.wrap( - response -> { - List jobs = new ArrayList<>(); - - SearchHit[] hits = response.getHits().getHits(); - for (SearchHit hit : hits) { - try { - BytesReference source = hit.getSourceRef(); - Job job = parseJobLenientlyFromSource(source).build(); - jobs.add(job); - } catch (IOException e) { - // TODO A better way to handle this rather than just ignoring the error? - logger.error("Error parsing anomaly detector job configuration [" + hit.getId() + "]", e); - } - } - - listener.onResponse(jobs); - }, - listener::onFailure) - , client::search); + .setIndicesOptions(IndicesOptions.lenientExpandOpen()) + .setSource(sourceBuilder) + .setSize(MlConfigIndex.CONFIG_INDEX_MAX_RESULTS_WINDOW) + .request(); + + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + ML_ORIGIN, + searchRequest, + ActionListener.wrap(response -> { + List jobs = new ArrayList<>(); + + SearchHit[] hits = response.getHits().getHits(); + for (SearchHit hit : hits) { + try { + BytesReference source = hit.getSourceRef(); + Job job = parseJobLenientlyFromSource(source).build(); + jobs.add(job); + } catch (IOException e) { + // TODO A better way to handle this rather than just ignoring the error? + logger.error("Error parsing anomaly detector job configuration [" + hit.getId() + "]", e); + } + } + + listener.onResponse(jobs); + }, listener::onFailure), + client::search + ); } /** @@ -703,28 +715,26 @@ public void findJobsWithCustomRules(ActionListener> listener) { * @param listener Validation listener */ public void validateDatafeedJob(DatafeedConfig config, ActionListener listener) { - getJob(config.getJobId(), ActionListener.wrap( - jobBuilder -> { - try { - DatafeedJobValidator.validate(config, jobBuilder.build(), xContentRegistry); - listener.onResponse(Boolean.TRUE); - } catch (Exception e) { - listener.onFailure(e); - } - }, - listener::onFailure - )); + getJob(config.getJobId(), ActionListener.wrap(jobBuilder -> { + try { + DatafeedJobValidator.validate(config, jobBuilder.build(), xContentRegistry); + listener.onResponse(Boolean.TRUE); + } catch (Exception e) { + listener.onFailure(e); + } + }, listener::onFailure)); } static Collection matchingJobIdsWithTasks(String[] jobIdPatterns, PersistentTasksCustomMetadata tasksMetadata) { return MlStrings.findMatching(jobIdPatterns, MlTasks.openJobIds(tasksMetadata)); } - - private void parseJobLenientlyFromSource(BytesReference source, ActionListener jobListener) { - try (InputStream stream = source.streamInput(); - XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) { + private void parseJobLenientlyFromSource(BytesReference source, ActionListener jobListener) { + try ( + InputStream stream = source.streamInput(); + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream) + ) { jobListener.onResponse(Job.LENIENT_PARSER.apply(parser, null)); } catch (Exception e) { jobListener.onFailure(e); @@ -732,14 +742,16 @@ private void parseJobLenientlyFromSource(BytesReference source, ActionListener true, - retryMessage -> logger.debug("[{}] Job data_counts {}", jobId, retryMessage)); + retryMessage -> logger.debug("[{}] Job data_counts {}", jobId, retryMessage) + ); } catch (IOException ioe) { logger.error(() -> new ParameterizedMessage("[{}] Failed writing data_counts stats", jobId), ioe); } catch (Exception ex) { @@ -91,13 +93,17 @@ public void persistDataCounts(String jobId, DataCounts counts) { public void persistDataCountsAsync(String jobId, DataCounts counts, ActionListener listener) { counts.setLogTime(Instant.now()); try (XContentBuilder content = serialiseCounts(counts)) { - final IndexRequest request = new IndexRequest(AnomalyDetectorsIndex.resultsWriteAlias(jobId)) - .id(DataCounts.documentId(jobId)) + final IndexRequest request = new IndexRequest(AnomalyDetectorsIndex.resultsWriteAlias(jobId)).id(DataCounts.documentId(jobId)) .setRequireAlias(true) .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) .source(content); - executeAsyncWithOrigin(client, ML_ORIGIN, IndexAction.INSTANCE, request, - listener.delegateFailure((l, r) -> l.onResponse(true))); + executeAsyncWithOrigin( + client, + ML_ORIGIN, + IndexAction.INSTANCE, + request, + listener.delegateFailure((l, r) -> l.onResponse(true)) + ); } catch (IOException ioe) { String msg = new ParameterizedMessage("[{}] Failed writing data_counts stats", jobId).getFormattedMessage(); logger.error(msg, ioe); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/JobDataDeleter.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/JobDataDeleter.java index b9e6a0a61c77a..85e4c8123493e 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/JobDataDeleter.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/JobDataDeleter.java @@ -97,11 +97,15 @@ public JobDataDeleter(Client client, String jobId) { */ public void deleteModelSnapshots(List modelSnapshots, ActionListener listener) { if (modelSnapshots.isEmpty()) { - listener.onResponse(new BulkByScrollResponse(TimeValue.ZERO, - new BulkByScrollTask.Status(Collections.emptyList(), null), - Collections.emptyList(), - Collections.emptyList(), - false)); + listener.onResponse( + new BulkByScrollResponse( + TimeValue.ZERO, + new BulkByScrollTask.Status(Collections.emptyList(), null), + Collections.emptyList(), + Collections.emptyList(), + false + ) + ); return; } @@ -118,8 +122,7 @@ public void deleteModelSnapshots(List modelSnapshots, ActionListe indices.add(AnomalyDetectorsIndex.jobResultsAliasedName(modelSnapshot.getJobId())); } - DeleteByQueryRequest deleteByQueryRequest = new DeleteByQueryRequest(indices.toArray(new String[0])) - .setRefresh(true) + DeleteByQueryRequest deleteByQueryRequest = new DeleteByQueryRequest(indices.toArray(new String[0])).setRefresh(true) .setIndicesOptions(IndicesOptions.lenientExpandOpen()) .setQuery(QueryBuilders.idsQuery().addIds(idsToDelete.toArray(new String[0]))); @@ -147,14 +150,15 @@ public void deleteAllAnnotations(ActionListener listener) { * If {@code null} or empty, no event-related filtering is applied * @param listener Response listener */ - public void deleteAnnotations(@Nullable Long fromEpochMs, - @Nullable Long toEpochMs, - @Nullable Set eventsToDelete, - ActionListener listener) { - BoolQueryBuilder boolQuery = - QueryBuilders.boolQuery() - .filter(QueryBuilders.termQuery(Job.ID.getPreferredName(), jobId)) - .filter(QueryBuilders.termQuery(Annotation.CREATE_USERNAME.getPreferredName(), XPackUser.NAME)); + public void deleteAnnotations( + @Nullable Long fromEpochMs, + @Nullable Long toEpochMs, + @Nullable Set eventsToDelete, + ActionListener listener + ) { + BoolQueryBuilder boolQuery = QueryBuilders.boolQuery() + .filter(QueryBuilders.termQuery(Job.ID.getPreferredName(), jobId)) + .filter(QueryBuilders.termQuery(Annotation.CREATE_USERNAME.getPreferredName(), XPackUser.NAME)); if (fromEpochMs != null || toEpochMs != null) { boolQuery.filter(QueryBuilders.rangeQuery(Annotation.TIMESTAMP.getPreferredName()).gte(fromEpochMs).lt(toEpochMs)); } @@ -162,8 +166,7 @@ public void deleteAnnotations(@Nullable Long fromEpochMs, boolQuery.filter(QueryBuilders.termsQuery(Annotation.EVENT.getPreferredName(), eventsToDelete)); } QueryBuilder query = QueryBuilders.constantScoreQuery(boolQuery); - DeleteByQueryRequest dbqRequest = new DeleteByQueryRequest(AnnotationIndex.READ_ALIAS_NAME) - .setQuery(query) + DeleteByQueryRequest dbqRequest = new DeleteByQueryRequest(AnnotationIndex.READ_ALIAS_NAME).setQuery(query) .setIndicesOptions(IndicesOptions.lenientExpandOpen()) .setAbortOnVersionConflict(false) .setRefresh(true) @@ -177,7 +180,8 @@ public void deleteAnnotations(@Nullable Long fromEpochMs, ML_ORIGIN, DeleteByQueryAction.INSTANCE, dbqRequest, - ActionListener.wrap(r -> listener.onResponse(true), listener::onFailure)); + ActionListener.wrap(r -> listener.onResponse(true), listener::onFailure) + ); } /** @@ -190,8 +194,7 @@ public void deleteResultsFromTime(long cutoffEpochMs, ActionListener li QueryBuilder query = QueryBuilders.boolQuery() .filter(QueryBuilders.existsQuery(Result.RESULT_TYPE.getPreferredName())) .filter(QueryBuilders.rangeQuery(Result.TIMESTAMP.getPreferredName()).gte(cutoffEpochMs)); - DeleteByQueryRequest dbqRequest = new DeleteByQueryRequest(AnomalyDetectorsIndex.jobResultsAliasedName(jobId)) - .setQuery(query) + DeleteByQueryRequest dbqRequest = new DeleteByQueryRequest(AnomalyDetectorsIndex.jobResultsAliasedName(jobId)).setQuery(query) .setIndicesOptions(IndicesOptions.lenientExpandOpen()) .setAbortOnVersionConflict(false) .setRefresh(true) @@ -205,7 +208,8 @@ public void deleteResultsFromTime(long cutoffEpochMs, ActionListener li ML_ORIGIN, DeleteByQueryAction.INSTANCE, dbqRequest, - ActionListener.wrap(r -> listener.onResponse(true), listener::onFailure)); + ActionListener.wrap(r -> listener.onResponse(true), listener::onFailure) + ); } /** @@ -213,8 +217,7 @@ public void deleteResultsFromTime(long cutoffEpochMs, ActionListener li */ public void deleteInterimResults() { QueryBuilder query = QueryBuilders.constantScoreQuery(QueryBuilders.termQuery(Result.IS_INTERIM.getPreferredName(), true)); - DeleteByQueryRequest dbqRequest = new DeleteByQueryRequest(AnomalyDetectorsIndex.jobResultsAliasedName(jobId)) - .setQuery(query) + DeleteByQueryRequest dbqRequest = new DeleteByQueryRequest(AnomalyDetectorsIndex.jobResultsAliasedName(jobId)).setQuery(query) .setIndicesOptions(IndicesOptions.lenientExpandOpen()) .setAbortOnVersionConflict(false) .setRefresh(false) @@ -236,8 +239,9 @@ public void deleteInterimResults() { * @param listener Response listener */ public void deleteDatafeedTimingStats(ActionListener listener) { - DeleteByQueryRequest deleteByQueryRequest = new DeleteByQueryRequest(AnomalyDetectorsIndex.jobResultsAliasedName(jobId)) - .setRefresh(true) + DeleteByQueryRequest deleteByQueryRequest = new DeleteByQueryRequest(AnomalyDetectorsIndex.jobResultsAliasedName(jobId)).setRefresh( + true + ) .setIndicesOptions(IndicesOptions.lenientExpandOpen()) .setQuery(QueryBuilders.idsQuery().addIds(DatafeedTimingStats.documentId(jobId))); @@ -250,161 +254,168 @@ public void deleteDatafeedTimingStats(ActionListener liste /** * Deletes all documents associated with a job except user annotations and notifications */ - public void deleteJobDocuments(JobConfigProvider jobConfigProvider, IndexNameExpressionResolver indexNameExpressionResolver, - ClusterState clusterState, CheckedConsumer finishedHandler, - Consumer failureHandler) { + public void deleteJobDocuments( + JobConfigProvider jobConfigProvider, + IndexNameExpressionResolver indexNameExpressionResolver, + ClusterState clusterState, + CheckedConsumer finishedHandler, + Consumer failureHandler + ) { AtomicReference indexNames = new AtomicReference<>(); final ActionListener completionHandler = ActionListener.wrap( response -> finishedHandler.accept(response.isAcknowledged()), - failureHandler); + failureHandler + ); // Step 9. If we did not drop the indices and after DBQ state done, we delete the aliases - ActionListener dbqHandler = ActionListener.wrap( - bulkByScrollResponse -> { - if (bulkByScrollResponse == null) { // no action was taken by DBQ, assume indices were deleted - completionHandler.onResponse(AcknowledgedResponse.TRUE); - } else { - if (bulkByScrollResponse.isTimedOut()) { - logger.warn("[{}] DeleteByQuery for indices [{}] timed out.", jobId, String.join(", ", indexNames.get())); - } - if (bulkByScrollResponse.getBulkFailures().isEmpty() == false) { - logger.warn("[{}] {} failures and {} conflicts encountered while running DeleteByQuery on indices [{}].", - jobId, bulkByScrollResponse.getBulkFailures().size(), bulkByScrollResponse.getVersionConflicts(), - String.join(", ", indexNames.get())); - for (BulkItemResponse.Failure failure : bulkByScrollResponse.getBulkFailures()) { - logger.warn("DBQ failure: " + failure); - } + ActionListener dbqHandler = ActionListener.wrap(bulkByScrollResponse -> { + if (bulkByScrollResponse == null) { // no action was taken by DBQ, assume indices were deleted + completionHandler.onResponse(AcknowledgedResponse.TRUE); + } else { + if (bulkByScrollResponse.isTimedOut()) { + logger.warn("[{}] DeleteByQuery for indices [{}] timed out.", jobId, String.join(", ", indexNames.get())); + } + if (bulkByScrollResponse.getBulkFailures().isEmpty() == false) { + logger.warn( + "[{}] {} failures and {} conflicts encountered while running DeleteByQuery on indices [{}].", + jobId, + bulkByScrollResponse.getBulkFailures().size(), + bulkByScrollResponse.getVersionConflicts(), + String.join(", ", indexNames.get()) + ); + for (BulkItemResponse.Failure failure : bulkByScrollResponse.getBulkFailures()) { + logger.warn("DBQ failure: " + failure); } - deleteAliases(jobId, completionHandler); } - }, - failureHandler); + deleteAliases(jobId, completionHandler); + } + }, failureHandler); // Step 8. If we did not delete the indices, we run a delete by query - ActionListener deleteByQueryExecutor = ActionListener.wrap( - response -> { - if (response && indexNames.get().length > 0) { - deleteResultsByQuery(jobId, indexNames.get(), dbqHandler); - } else { // We did not execute DBQ, no need to delete aliases or check the response - dbqHandler.onResponse(null); - } - }, - failureHandler); + ActionListener deleteByQueryExecutor = ActionListener.wrap(response -> { + if (response && indexNames.get().length > 0) { + deleteResultsByQuery(jobId, indexNames.get(), dbqHandler); + } else { // We did not execute DBQ, no need to delete aliases or check the response + dbqHandler.onResponse(null); + } + }, failureHandler); // Step 7. Handle each multi-search response. There should be one response for each underlying index. // For each underlying index that contains results ONLY for the current job, we will delete that index. // If there exists at least 1 index that has another job's results, we will run DBQ. - ActionListener customIndexSearchHandler = ActionListener.wrap( - multiSearchResponse -> { - if (multiSearchResponse == null) { - deleteByQueryExecutor.onResponse(true); // We need to run DBQ and alias deletion - return; - } - String defaultSharedIndex = AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + - AnomalyDetectorsIndexFields.RESULTS_INDEX_DEFAULT; - List indicesToDelete = new ArrayList<>(); - boolean needToRunDBQTemp = false; - assert multiSearchResponse.getResponses().length == indexNames.get().length; - int i = 0; - for (MultiSearchResponse.Item item : multiSearchResponse.getResponses()) { - if (item.isFailure()) { - ++i; - if (ExceptionsHelper.unwrapCause(item.getFailure()) instanceof IndexNotFoundException) { - // index is already deleted, no need to take action against it - continue; - } else { - failureHandler.accept(item.getFailure()); - return; - } - } - SearchResponse searchResponse = item.getResponse(); - if (searchResponse.getHits().getTotalHits().value > 0 || indexNames.get()[i].equals(defaultSharedIndex)) { - needToRunDBQTemp = true; + ActionListener customIndexSearchHandler = ActionListener.wrap(multiSearchResponse -> { + if (multiSearchResponse == null) { + deleteByQueryExecutor.onResponse(true); // We need to run DBQ and alias deletion + return; + } + String defaultSharedIndex = AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + + AnomalyDetectorsIndexFields.RESULTS_INDEX_DEFAULT; + List indicesToDelete = new ArrayList<>(); + boolean needToRunDBQTemp = false; + assert multiSearchResponse.getResponses().length == indexNames.get().length; + int i = 0; + for (MultiSearchResponse.Item item : multiSearchResponse.getResponses()) { + if (item.isFailure()) { + ++i; + if (ExceptionsHelper.unwrapCause(item.getFailure()) instanceof IndexNotFoundException) { + // index is already deleted, no need to take action against it + continue; } else { - indicesToDelete.add(indexNames.get()[i]); + failureHandler.accept(item.getFailure()); + return; } - ++i; - } - final boolean needToRunDBQ = needToRunDBQTemp; - if (indicesToDelete.isEmpty()) { - deleteByQueryExecutor.onResponse(needToRunDBQ); - return; } - logger.info("[{}] deleting the following indices directly {}", jobId, indicesToDelete); - DeleteIndexRequest request = new DeleteIndexRequest(indicesToDelete.toArray(String[]::new)); - request.indicesOptions(IndicesOptions.lenientExpandOpenHidden()); - executeAsyncWithOrigin( - client.threadPool().getThreadContext(), - ML_ORIGIN, - request, - ActionListener.wrap( - response -> deleteByQueryExecutor.onResponse(needToRunDBQ), // only run DBQ if there is a shared index - failureHandler), - client.admin().indices()::delete); - }, - failure -> { - if (ExceptionsHelper.unwrapCause(failure) instanceof IndexNotFoundException) { // assume the index is already deleted - deleteByQueryExecutor.onResponse(false); // skip DBQ && Alias + SearchResponse searchResponse = item.getResponse(); + if (searchResponse.getHits().getTotalHits().value > 0 || indexNames.get()[i].equals(defaultSharedIndex)) { + needToRunDBQTemp = true; } else { - failureHandler.accept(failure); + indicesToDelete.add(indexNames.get()[i]); } + ++i; } - ); + final boolean needToRunDBQ = needToRunDBQTemp; + if (indicesToDelete.isEmpty()) { + deleteByQueryExecutor.onResponse(needToRunDBQ); + return; + } + logger.info("[{}] deleting the following indices directly {}", jobId, indicesToDelete); + DeleteIndexRequest request = new DeleteIndexRequest(indicesToDelete.toArray(String[]::new)); + request.indicesOptions(IndicesOptions.lenientExpandOpenHidden()); + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + ML_ORIGIN, + request, + ActionListener.wrap( + response -> deleteByQueryExecutor.onResponse(needToRunDBQ), // only run DBQ if there is a shared index + failureHandler + ), + client.admin().indices()::delete + ); + }, failure -> { + if (ExceptionsHelper.unwrapCause(failure) instanceof IndexNotFoundException) { // assume the index is already deleted + deleteByQueryExecutor.onResponse(false); // skip DBQ && Alias + } else { + failureHandler.accept(failure); + } + }); // Step 6. If we successfully find a job, gather information about its result indices. // This will execute a multi-search action for every concrete index behind the job results alias. // If there are no concrete indices, take no action and go to the next step. - ActionListener getJobHandler = ActionListener.wrap( - builder -> { - indexNames.set(indexNameExpressionResolver.concreteIndexNames(clusterState, - IndicesOptions.lenientExpandOpen(), AnomalyDetectorsIndex.jobResultsAliasedName(jobId))); - if (indexNames.get().length == 0) { - // don't bother searching the index any further - it's already been closed or deleted - customIndexSearchHandler.onResponse(null); - return; - } - MultiSearchRequest multiSearchRequest = new MultiSearchRequest(); - // It is important that the requests are in the same order as the index names. - // This is because responses are ordered according to their requests. - for (String indexName : indexNames.get()) { - SearchSourceBuilder source = new SearchSourceBuilder() - .size(0) - // if we have just one hit we cannot delete the index - .trackTotalHitsUpTo(1) - .query(QueryBuilders.boolQuery().filter( - QueryBuilders.boolQuery().mustNot(QueryBuilders.termQuery(Job.ID.getPreferredName(), jobId)))); - multiSearchRequest.add(new SearchRequest(indexName).source(source)); - } - executeAsyncWithOrigin(client, - ML_ORIGIN, - MultiSearchAction.INSTANCE, - multiSearchRequest, - customIndexSearchHandler); - }, - failureHandler - ); + ActionListener getJobHandler = ActionListener.wrap(builder -> { + indexNames.set( + indexNameExpressionResolver.concreteIndexNames( + clusterState, + IndicesOptions.lenientExpandOpen(), + AnomalyDetectorsIndex.jobResultsAliasedName(jobId) + ) + ); + if (indexNames.get().length == 0) { + // don't bother searching the index any further - it's already been closed or deleted + customIndexSearchHandler.onResponse(null); + return; + } + MultiSearchRequest multiSearchRequest = new MultiSearchRequest(); + // It is important that the requests are in the same order as the index names. + // This is because responses are ordered according to their requests. + for (String indexName : indexNames.get()) { + SearchSourceBuilder source = new SearchSourceBuilder().size(0) + // if we have just one hit we cannot delete the index + .trackTotalHitsUpTo(1) + .query( + QueryBuilders.boolQuery() + .filter(QueryBuilders.boolQuery().mustNot(QueryBuilders.termQuery(Job.ID.getPreferredName(), jobId))) + ); + multiSearchRequest.add(new SearchRequest(indexName).source(source)); + } + executeAsyncWithOrigin(client, ML_ORIGIN, MultiSearchAction.INSTANCE, multiSearchRequest, customIndexSearchHandler); + }, failureHandler); // Step 5. Get the job as the initial result index name is required ActionListener deleteAnnotationsHandler = ActionListener.wrap( response -> jobConfigProvider.getJob(jobId, getJobHandler), - failureHandler); + failureHandler + ); // Step 4. Delete annotations associated with the job ActionListener deleteCategorizerStateHandler = ActionListener.wrap( response -> deleteAllAnnotations(deleteAnnotationsHandler), - failureHandler); + failureHandler + ); // Step 3. Delete quantiles done, delete the categorizer state ActionListener deleteQuantilesHandler = ActionListener.wrap( response -> deleteCategorizerState(jobId, 1, deleteCategorizerStateHandler), - failureHandler); + failureHandler + ); // Step 2. Delete state done, delete the quantiles ActionListener deleteStateHandler = ActionListener.wrap( bulkResponse -> deleteQuantiles(jobId, deleteQuantilesHandler), - failureHandler); + failureHandler + ); // Step 1. Delete the model state deleteModelState(jobId, deleteStateHandler); @@ -413,22 +424,17 @@ public void deleteJobDocuments(JobConfigProvider jobConfigProvider, IndexNameExp private void deleteResultsByQuery(String jobId, String[] indices, ActionListener listener) { assert indices.length > 0; - ActionListener refreshListener = ActionListener.wrap( - refreshResponse -> { - logger.info("[{}] running delete by query on [{}]", jobId, String.join(", ", indices)); - ConstantScoreQueryBuilder query = - new ConstantScoreQueryBuilder(new TermQueryBuilder(Job.ID.getPreferredName(), jobId)); - DeleteByQueryRequest request = new DeleteByQueryRequest(indices) - .setQuery(query) - .setIndicesOptions(MlIndicesUtils.addIgnoreUnavailable(IndicesOptions.lenientExpandOpenHidden())) - .setSlices(AbstractBulkByScrollRequest.AUTO_SLICES) - .setAbortOnVersionConflict(false) - .setRefresh(true); - - executeAsyncWithOrigin(client, ML_ORIGIN, DeleteByQueryAction.INSTANCE, request, listener); - }, - listener::onFailure - ); + ActionListener refreshListener = ActionListener.wrap(refreshResponse -> { + logger.info("[{}] running delete by query on [{}]", jobId, String.join(", ", indices)); + ConstantScoreQueryBuilder query = new ConstantScoreQueryBuilder(new TermQueryBuilder(Job.ID.getPreferredName(), jobId)); + DeleteByQueryRequest request = new DeleteByQueryRequest(indices).setQuery(query) + .setIndicesOptions(MlIndicesUtils.addIgnoreUnavailable(IndicesOptions.lenientExpandOpenHidden())) + .setSlices(AbstractBulkByScrollRequest.AUTO_SLICES) + .setAbortOnVersionConflict(false) + .setRefresh(true); + + executeAsyncWithOrigin(client, ML_ORIGIN, DeleteByQueryAction.INSTANCE, request, listener); + }, listener::onFailure); // First, we refresh the indices to ensure any in-flight docs become visible RefreshRequest refreshRequest = new RefreshRequest(indices); @@ -443,22 +449,29 @@ private void deleteAliases(String jobId, ActionListener fi // first find the concrete indices associated with the aliases GetAliasesRequest aliasesRequest = new GetAliasesRequest().aliases(readAliasName, writeAliasName) .indicesOptions(IndicesOptions.lenientExpandOpenHidden()); - executeAsyncWithOrigin(client.threadPool().getThreadContext(), ML_ORIGIN, aliasesRequest, - ActionListener.wrap( - getAliasesResponse -> { - // remove the aliases from the concrete indices found in the first step - IndicesAliasesRequest removeRequest = buildRemoveAliasesRequest(getAliasesResponse); - if (removeRequest == null) { - // don't error if the job's aliases have already been deleted - carry on and delete the - // rest of the job's data - finishedHandler.onResponse(AcknowledgedResponse.TRUE); - return; - } - executeAsyncWithOrigin(client.threadPool().getThreadContext(), ML_ORIGIN, removeRequest, - finishedHandler, - client.admin().indices()::aliases); - }, - finishedHandler::onFailure), client.admin().indices()::getAliases); + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + ML_ORIGIN, + aliasesRequest, + ActionListener.wrap(getAliasesResponse -> { + // remove the aliases from the concrete indices found in the first step + IndicesAliasesRequest removeRequest = buildRemoveAliasesRequest(getAliasesResponse); + if (removeRequest == null) { + // don't error if the job's aliases have already been deleted - carry on and delete the + // rest of the job's data + finishedHandler.onResponse(AcknowledgedResponse.TRUE); + return; + } + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + ML_ORIGIN, + removeRequest, + finishedHandler, + client.admin().indices()::aliases + ); + }, finishedHandler::onFailure), + client.admin().indices()::getAliases + ); } private IndicesAliasesRequest buildRemoveAliasesRequest(GetAliasesResponse getAliasesResponse) { @@ -472,58 +485,59 @@ private IndicesAliasesRequest buildRemoveAliasesRequest(GetAliasesResponse getAl entry.value.forEach(metadata -> aliases.add(metadata.getAlias())); } } - return aliases.isEmpty() ? null : new IndicesAliasesRequest().addAliasAction( - IndicesAliasesRequest.AliasActions.remove() - .aliases(aliases.toArray(new String[aliases.size()])) - .indices(indices.toArray(new String[indices.size()]))); + return aliases.isEmpty() + ? null + : new IndicesAliasesRequest().addAliasAction( + IndicesAliasesRequest.AliasActions.remove() + .aliases(aliases.toArray(new String[aliases.size()])) + .indices(indices.toArray(new String[indices.size()])) + ); } private void deleteQuantiles(String jobId, ActionListener finishedHandler) { // Just use ID here, not type, as trying to delete different types spams the logs with an exception stack trace IdsQueryBuilder query = new IdsQueryBuilder().addIds(Quantiles.documentId(jobId)); - DeleteByQueryRequest request = new DeleteByQueryRequest(AnomalyDetectorsIndex.jobStateIndexPattern()) - .setQuery(query) + DeleteByQueryRequest request = new DeleteByQueryRequest(AnomalyDetectorsIndex.jobStateIndexPattern()).setQuery(query) .setIndicesOptions(MlIndicesUtils.addIgnoreUnavailable(IndicesOptions.lenientExpandOpen())) .setAbortOnVersionConflict(false) .setRefresh(true); - executeAsyncWithOrigin(client, ML_ORIGIN, DeleteByQueryAction.INSTANCE, request, ActionListener.wrap( - response -> finishedHandler.onResponse(true), - ignoreIndexNotFoundException(finishedHandler))); + executeAsyncWithOrigin( + client, + ML_ORIGIN, + DeleteByQueryAction.INSTANCE, + request, + ActionListener.wrap(response -> finishedHandler.onResponse(true), ignoreIndexNotFoundException(finishedHandler)) + ); } private void deleteModelState(String jobId, ActionListener listener) { GetModelSnapshotsAction.Request request = new GetModelSnapshotsAction.Request(jobId, null); request.setPageParams(new PageParams(0, MAX_SNAPSHOTS_TO_DELETE)); - executeAsyncWithOrigin(client, ML_ORIGIN, GetModelSnapshotsAction.INSTANCE, request, ActionListener.wrap( - response -> { - List deleteCandidates = response.getPage().results(); - deleteModelSnapshots(deleteCandidates, listener); - }, - listener::onFailure)); + executeAsyncWithOrigin(client, ML_ORIGIN, GetModelSnapshotsAction.INSTANCE, request, ActionListener.wrap(response -> { + List deleteCandidates = response.getPage().results(); + deleteModelSnapshots(deleteCandidates, listener); + }, listener::onFailure)); } private void deleteCategorizerState(String jobId, int docNum, ActionListener finishedHandler) { // Just use ID here, not type, as trying to delete different types spams the logs with an exception stack trace IdsQueryBuilder query = new IdsQueryBuilder().addIds(CategorizerState.documentId(jobId, docNum)); - DeleteByQueryRequest request = new DeleteByQueryRequest(AnomalyDetectorsIndex.jobStateIndexPattern()) - .setQuery(query) + DeleteByQueryRequest request = new DeleteByQueryRequest(AnomalyDetectorsIndex.jobStateIndexPattern()).setQuery(query) .setIndicesOptions(MlIndicesUtils.addIgnoreUnavailable(IndicesOptions.lenientExpandOpen())) .setAbortOnVersionConflict(false) .setRefresh(true); - executeAsyncWithOrigin(client, ML_ORIGIN, DeleteByQueryAction.INSTANCE, request, ActionListener.wrap( - response -> { - // If we successfully deleted a document try the next one; if not we're done - if (response.getDeleted() > 0) { - // There's an assumption here that there won't be very many categorizer - // state documents, so the recursion won't go more than, say, 5 levels deep - deleteCategorizerState(jobId, docNum + 1, finishedHandler); - return; - } - finishedHandler.onResponse(true); - }, - ignoreIndexNotFoundException(finishedHandler))); + executeAsyncWithOrigin(client, ML_ORIGIN, DeleteByQueryAction.INSTANCE, request, ActionListener.wrap(response -> { + // If we successfully deleted a document try the next one; if not we're done + if (response.getDeleted() > 0) { + // There's an assumption here that there won't be very many categorizer + // state documents, so the recursion won't go more than, say, 5 levels deep + deleteCategorizerState(jobId, docNum + 1, finishedHandler); + return; + } + finishedHandler.onResponse(true); + }, ignoreIndexNotFoundException(finishedHandler))); } private static Consumer ignoreIndexNotFoundException(ActionListener finishedHandler) { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/JobRenormalizedResultsPersister.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/JobRenormalizedResultsPersister.java index 18347b0ac665c..dccabdf095dde 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/JobRenormalizedResultsPersister.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/JobRenormalizedResultsPersister.java @@ -27,7 +27,6 @@ import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.xpack.core.ClientHelper.ML_ORIGIN; - /** * Interface for classes that update {@linkplain Bucket Buckets} * for a particular job with new normalized anomaly scores and @@ -115,4 +114,3 @@ BulkRequest getBulkRequest() { return bulkRequest; } } - diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/JobResultsPersister.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/JobResultsPersister.java index 8ee8af4ef8891..f9c9079b797e0 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/JobResultsPersister.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/JobResultsPersister.java @@ -24,11 +24,11 @@ import org.elasticsearch.action.support.WriteRequest; import org.elasticsearch.client.OriginSettingClient; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.IdsQueryBuilder; import org.elasticsearch.search.builder.SearchSourceBuilder; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ml.annotations.AnnotationIndex; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedTimingStats; import org.elasticsearch.xpack.core.ml.job.persistence.AnomalyDetectorsIndex; @@ -82,8 +82,7 @@ public class JobResultsPersister { private final OriginSettingClient client; private final ResultsPersisterService resultsPersisterService; - public JobResultsPersister(OriginSettingClient client, - ResultsPersisterService resultsPersisterService) { + public JobResultsPersister(OriginSettingClient client, ResultsPersisterService resultsPersisterService) { this.client = client; this.resultsPersisterService = resultsPersisterService; } @@ -154,7 +153,8 @@ public Builder persistTimingStats(TimingStats timingStats) { TimingStats.documentId(timingStats.getJobId()), timingStats, new ToXContent.MapParams(Collections.singletonMap(ToXContentParams.FOR_INTERNAL_STORAGE, "true")), - TimingStats.TYPE.getPreferredName()); + TimingStats.TYPE.getPreferredName() + ); return this; } @@ -196,8 +196,12 @@ public Builder persistModelPlot(ModelPlot modelPlot) { } public Builder persistCategorizerStats(CategorizerStats categorizerStats) { - logger.trace("[{}] ES BULK ACTION: index categorizer stats to index [{}] with ID [{}]", - jobId, indexName, categorizerStats.getId()); + logger.trace( + "[{}] ES BULK ACTION: index categorizer stats to index [{}] with ID [{}]", + jobId, + indexName, + categorizerStats.getId() + ); indexResult(categorizerStats.getId(), categorizerStats, "categorizer stats"); return this; } @@ -209,8 +213,12 @@ public Builder persistForecast(Forecast forecast) { } public Builder persistForecastRequestStats(ForecastRequestStats forecastRequestStats) { - logger.trace("[{}] ES BULK ACTION: index forecast request stats to index [{}] with ID [{}]", jobId, indexName, - forecastRequestStats.getId()); + logger.trace( + "[{}] ES BULK ACTION: index forecast request stats to index [{}] with ID [{}]", + jobId, + indexName, + forecastRequestStats.getId() + ); indexResult(forecastRequestStats.getId(), forecastRequestStats, Forecast.RESULT_TYPE_VALUE); return this; } @@ -239,7 +247,10 @@ public void executeRequest() { return; } logger.trace("[{}] ES API CALL: bulk request with {} actions", jobId, bulkRequest.numberOfActions()); - resultsPersisterService.bulkIndexWithRetry(bulkRequest, jobId, shouldRetry, + resultsPersisterService.bulkIndexWithRetry( + bulkRequest, + jobId, + shouldRetry, retryMessage -> logger.debug("[{}] Bulk indexing of results failed {}", jobId, retryMessage) ); bulkRequest = new BulkRequest(); @@ -261,8 +272,12 @@ BulkRequest getBulkRequest() { * @param category The category to be persisted */ public void persistCategoryDefinition(CategoryDefinition category, Supplier shouldRetry) { - Persistable persistable = - new Persistable(AnomalyDetectorsIndex.resultsWriteAlias(category.getJobId()), category.getJobId(), category, category.getId()); + Persistable persistable = new Persistable( + AnomalyDetectorsIndex.resultsWriteAlias(category.getJobId()), + category.getJobId(), + category, + category.getId() + ); persistable.persist(shouldRetry, true); // Don't commit as we expect masses of these updates and they're not // read again by this process @@ -275,16 +290,15 @@ public void persistQuantiles(Quantiles quantiles, Supplier shouldRetry) String jobId = quantiles.getJobId(); String quantilesDocId = Quantiles.documentId(jobId); SearchRequest searchRequest = buildQuantilesDocIdSearch(quantilesDocId); - SearchResponse searchResponse = - resultsPersisterService.searchWithRetry( - searchRequest, - jobId, - shouldRetry, - retryMessage -> logger.debug("[{}] {} {}", jobId, quantilesDocId, retryMessage)); - String indexOrAlias = - searchResponse.getHits().getHits().length > 0 - ? searchResponse.getHits().getHits()[0].getIndex() - : AnomalyDetectorsIndex.jobStateIndexWriteAlias(); + SearchResponse searchResponse = resultsPersisterService.searchWithRetry( + searchRequest, + jobId, + shouldRetry, + retryMessage -> logger.debug("[{}] {} {}", jobId, quantilesDocId, retryMessage) + ); + String indexOrAlias = searchResponse.getHits().getHits().length > 0 + ? searchResponse.getHits().getHits()[0].getIndex() + : AnomalyDetectorsIndex.jobStateIndexWriteAlias(); Persistable persistable = new Persistable(indexOrAlias, quantiles.getJobId(), quantiles, quantilesDocId); persistable.persist(shouldRetry, AnomalyDetectorsIndex.jobStateIndexWriteAlias().equals(indexOrAlias)); @@ -297,50 +311,52 @@ public void persistQuantiles(Quantiles quantiles, WriteRequest.RefreshPolicy ref String quantilesDocId = Quantiles.documentId(quantiles.getJobId()); // Step 2: Create or update the quantiles document: - // - if the document did not exist, create the new one in the current write index - // - if the document did exist, update it in the index where it resides (not necessarily the current write index) - ActionListener searchFormerQuantilesDocListener = ActionListener.wrap( - searchResponse -> { - String indexOrAlias = - searchResponse.getHits().getHits().length > 0 - ? searchResponse.getHits().getHits()[0].getIndex() - : AnomalyDetectorsIndex.jobStateIndexWriteAlias(); - - Persistable persistable = new Persistable(indexOrAlias, quantiles.getJobId(), quantiles, quantilesDocId); - persistable.setRefreshPolicy(refreshPolicy); - persistable.persist(listener, AnomalyDetectorsIndex.jobStateIndexWriteAlias().equals(indexOrAlias)); - }, - listener::onFailure - ); + // - if the document did not exist, create the new one in the current write index + // - if the document did exist, update it in the index where it resides (not necessarily the current write index) + ActionListener searchFormerQuantilesDocListener = ActionListener.wrap(searchResponse -> { + String indexOrAlias = searchResponse.getHits().getHits().length > 0 + ? searchResponse.getHits().getHits()[0].getIndex() + : AnomalyDetectorsIndex.jobStateIndexWriteAlias(); + + Persistable persistable = new Persistable(indexOrAlias, quantiles.getJobId(), quantiles, quantilesDocId); + persistable.setRefreshPolicy(refreshPolicy); + persistable.persist(listener, AnomalyDetectorsIndex.jobStateIndexWriteAlias().equals(indexOrAlias)); + }, listener::onFailure); // Step 1: Search for existing quantiles document in .ml-state* SearchRequest searchRequest = buildQuantilesDocIdSearch(quantilesDocId); executeAsyncWithOrigin( - client.threadPool().getThreadContext(), ML_ORIGIN, searchRequest, searchFormerQuantilesDocListener, client::search); + client.threadPool().getThreadContext(), + ML_ORIGIN, + searchRequest, + searchFormerQuantilesDocListener, + client::search + ); } private static SearchRequest buildQuantilesDocIdSearch(String quantilesDocId) { - return new SearchRequest(AnomalyDetectorsIndex.jobStateIndexPattern()) - .allowPartialSearchResults(false) + return new SearchRequest(AnomalyDetectorsIndex.jobStateIndexPattern()).allowPartialSearchResults(false) .source( - new SearchSourceBuilder() - .size(1) + new SearchSourceBuilder().size(1) .trackTotalHits(false) - .query(new BoolQueryBuilder().filter(new IdsQueryBuilder().addIds(quantilesDocId)))); + .query(new BoolQueryBuilder().filter(new IdsQueryBuilder().addIds(quantilesDocId))) + ); } /** * Persist a model snapshot description */ - public BulkResponse persistModelSnapshot(ModelSnapshot modelSnapshot, - WriteRequest.RefreshPolicy refreshPolicy, - Supplier shouldRetry) { - Persistable persistable = - new Persistable( - AnomalyDetectorsIndex.resultsWriteAlias(modelSnapshot.getJobId()), - modelSnapshot.getJobId(), - modelSnapshot, - ModelSnapshot.documentId(modelSnapshot)); + public BulkResponse persistModelSnapshot( + ModelSnapshot modelSnapshot, + WriteRequest.RefreshPolicy refreshPolicy, + Supplier shouldRetry + ) { + Persistable persistable = new Persistable( + AnomalyDetectorsIndex.resultsWriteAlias(modelSnapshot.getJobId()), + modelSnapshot.getJobId(), + modelSnapshot, + ModelSnapshot.documentId(modelSnapshot) + ); persistable.setRefreshPolicy(refreshPolicy); return persistable.persist(shouldRetry, true); } @@ -351,20 +367,31 @@ public BulkResponse persistModelSnapshot(ModelSnapshot modelSnapshot, public void persistModelSizeStats(ModelSizeStats modelSizeStats, Supplier shouldRetry) { String jobId = modelSizeStats.getJobId(); logger.trace("[{}] Persisting model size stats, for size {}", jobId, modelSizeStats.getModelBytes()); - Persistable persistable = - new Persistable(AnomalyDetectorsIndex.resultsWriteAlias(jobId), jobId, modelSizeStats, modelSizeStats.getId()); + Persistable persistable = new Persistable( + AnomalyDetectorsIndex.resultsWriteAlias(jobId), + jobId, + modelSizeStats, + modelSizeStats.getId() + ); persistable.persist(shouldRetry, true); } /** * Persist the memory usage data */ - public void persistModelSizeStats(ModelSizeStats modelSizeStats, WriteRequest.RefreshPolicy refreshPolicy, - ActionListener listener) { + public void persistModelSizeStats( + ModelSizeStats modelSizeStats, + WriteRequest.RefreshPolicy refreshPolicy, + ActionListener listener + ) { String jobId = modelSizeStats.getJobId(); logger.trace("[{}] Persisting model size stats, for size {}", jobId, modelSizeStats.getModelBytes()); - Persistable persistable = - new Persistable(AnomalyDetectorsIndex.resultsWriteAlias(jobId), jobId, modelSizeStats, modelSizeStats.getId()); + Persistable persistable = new Persistable( + AnomalyDetectorsIndex.resultsWriteAlias(jobId), + jobId, + modelSizeStats, + modelSizeStats.getId() + ); persistable.setRefreshPolicy(refreshPolicy); persistable.persist(listener, true); } @@ -437,13 +464,13 @@ public void commitStateWrites(String jobId) { public BulkResponse persistDatafeedTimingStats(DatafeedTimingStats timingStats, WriteRequest.RefreshPolicy refreshPolicy) { String jobId = timingStats.getJobId(); logger.trace("[{}] Persisting datafeed timing stats", jobId); - Persistable persistable = - new Persistable( - AnomalyDetectorsIndex.resultsWriteAlias(jobId), - jobId, - timingStats, - new ToXContent.MapParams(Collections.singletonMap(ToXContentParams.FOR_INTERNAL_STORAGE, "true")), - DatafeedTimingStats.documentId(timingStats.getJobId())); + Persistable persistable = new Persistable( + AnomalyDetectorsIndex.resultsWriteAlias(jobId), + jobId, + timingStats, + new ToXContent.MapParams(Collections.singletonMap(ToXContentParams.FOR_INTERNAL_STORAGE, "true")), + DatafeedTimingStats.documentId(timingStats.getJobId()) + ); persistable.setRefreshPolicy(refreshPolicy); return persistable.persist(() -> true, true); } @@ -483,7 +510,8 @@ void setRefreshPolicy(WriteRequest.RefreshPolicy refreshPolicy) { BulkResponse persist(Supplier shouldRetry, boolean requireAlias) { logCall(indexName); try { - return resultsPersisterService.indexWithRetry(jobId, + return resultsPersisterService.indexWithRetry( + jobId, indexName, object, params, @@ -491,14 +519,16 @@ BulkResponse persist(Supplier shouldRetry, boolean requireAlias) { id, requireAlias, shouldRetry, - retryMessage -> logger.debug("[{}] {} {}", jobId, id, retryMessage)); + retryMessage -> logger.debug("[{}] {} {}", jobId, id, retryMessage) + ); } catch (IOException e) { logger.error(new ParameterizedMessage("[{}] Error writing [{}]", jobId, (id == null) ? "auto-generated ID" : id), e); IndexResponse.Builder notCreatedResponse = new IndexResponse.Builder(); notCreatedResponse.setResult(Result.NOOP); return new BulkResponse( - new BulkItemResponse[]{BulkItemResponse.success(0, DocWriteRequest.OpType.INDEX, notCreatedResponse.build())}, - 0); + new BulkItemResponse[] { BulkItemResponse.success(0, DocWriteRequest.OpType.INDEX, notCreatedResponse.build()) }, + 0 + ); } } @@ -506,8 +536,7 @@ void persist(ActionListener listener, boolean requireAlias) { logCall(indexName); try (XContentBuilder content = toXContentBuilder(object, params)) { - IndexRequest indexRequest = new IndexRequest(indexName) - .id(id) + IndexRequest indexRequest = new IndexRequest(indexName).id(id) .source(content) .setRefreshPolicy(refreshPolicy) .setRequireAlias(requireAlias); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/JobResultsProvider.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/JobResultsProvider.java index 1c55fd2b489e6..e8b1ddb44cfd5 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/JobResultsProvider.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/JobResultsProvider.java @@ -46,20 +46,13 @@ import org.elasticsearch.cluster.block.ClusterBlockException; import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; import org.elasticsearch.cluster.metadata.MappingMetadata; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.CollectionUtils; import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.xcontent.json.JsonXContent; +import org.elasticsearch.core.Nullable; import org.elasticsearch.index.IndexNotFoundException; import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.mapper.NumberFieldMapper; @@ -84,6 +77,13 @@ import org.elasticsearch.search.sort.FieldSortBuilder; import org.elasticsearch.search.sort.SortBuilders; import org.elasticsearch.search.sort.SortOrder; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.action.util.QueryPage; import org.elasticsearch.xpack.core.ml.MlMetaIndex; import org.elasticsearch.xpack.core.ml.action.GetBucketsAction; @@ -187,26 +187,24 @@ public JobResultsProvider(Client client, Settings settings, IndexNameExpressionR public void checkForLeftOverDocuments(Job job, ActionListener listener) { SearchRequestBuilder stateDocSearch = client.prepareSearch(AnomalyDetectorsIndex.jobStateIndexPattern()) - .setQuery(QueryBuilders.idsQuery().addIds(CategorizerState.documentId(job.getId(), 1), - CategorizerState.v54DocumentId(job.getId(), 1))) - .setTrackTotalHits(false) - .setIndicesOptions(IndicesOptions.strictExpand()); + .setQuery( + QueryBuilders.idsQuery().addIds(CategorizerState.documentId(job.getId(), 1), CategorizerState.v54DocumentId(job.getId(), 1)) + ) + .setTrackTotalHits(false) + .setIndicesOptions(IndicesOptions.strictExpand()); SearchRequestBuilder quantilesDocSearch = client.prepareSearch(AnomalyDetectorsIndex.jobStateIndexPattern()) - .setQuery(QueryBuilders.idsQuery().addIds(Quantiles.documentId(job.getId()), Quantiles.v54DocumentId(job.getId()))) - .setTrackTotalHits(false) - .setIndicesOptions(IndicesOptions.strictExpand()); + .setQuery(QueryBuilders.idsQuery().addIds(Quantiles.documentId(job.getId()), Quantiles.v54DocumentId(job.getId()))) + .setTrackTotalHits(false) + .setIndicesOptions(IndicesOptions.strictExpand()); SearchRequestBuilder resultDocSearch = client.prepareSearch(AnomalyDetectorsIndex.jobResultsIndexPrefix() + "*") - .setIndicesOptions(IndicesOptions.lenientExpandHidden()) - .setQuery(QueryBuilders.termQuery(Job.ID.getPreferredName(), job.getId())) - .setTrackTotalHits(false) - .setSize(1); + .setIndicesOptions(IndicesOptions.lenientExpandHidden()) + .setQuery(QueryBuilders.termQuery(Job.ID.getPreferredName(), job.getId())) + .setTrackTotalHits(false) + .setSize(1); - MultiSearchRequestBuilder msearch = client.prepareMultiSearch() - .add(stateDocSearch) - .add(resultDocSearch) - .add(quantilesDocSearch); + MultiSearchRequestBuilder msearch = client.prepareMultiSearch().add(stateDocSearch).add(resultDocSearch).add(quantilesDocSearch); ActionListener searchResponseActionListener = new ActionListener.Delegating<>(listener) { @Override @@ -225,8 +223,11 @@ public void onResponse(MultiSearchResponse response) { SearchRequest searchRequest = msearch.request().requests().get(i); // Don't wrap the original exception, because then it would be the root cause // and Kibana would display it in preference to the friendlier exception - e = ExceptionsHelper.badRequestException("Cannot create job [{}] as it requires closed index {}", - job.getId(), searchRequest.indices()); + e = ExceptionsHelper.badRequestException( + "Cannot create job [{}] as it requires closed index {}", + job.getId(), + searchRequest.indices() + ); } } } @@ -243,33 +244,50 @@ public void onResponse(MultiSearchResponse response) { int categorizerStateDocCount = 0; int resultDocCount = 0; for (SearchHit hit : searchHits) { - if (hit.getId().equals(Quantiles.documentId(job.getId())) || - hit.getId().equals(Quantiles.v54DocumentId(job.getId()))) { + if (hit.getId().equals(Quantiles.documentId(job.getId())) + || hit.getId().equals(Quantiles.v54DocumentId(job.getId()))) { quantileDocCount++; - } else if (hit.getId().startsWith(CategorizerState.documentPrefix(job.getId())) || - hit.getId().startsWith(CategorizerState.v54DocumentPrefix(job.getId()))) { - categorizerStateDocCount++; - } else { - resultDocCount++; - } + } else if (hit.getId().startsWith(CategorizerState.documentPrefix(job.getId())) + || hit.getId().startsWith(CategorizerState.v54DocumentPrefix(job.getId()))) { + categorizerStateDocCount++; + } else { + resultDocCount++; + } } - LOGGER.warn("{} result, {} quantile state and {} categorizer state documents exist for a prior job with Id [{}]", - resultDocCount, quantileDocCount, categorizerStateDocCount, job.getId()); + LOGGER.warn( + "{} result, {} quantile state and {} categorizer state documents exist for a prior job with Id [{}]", + resultDocCount, + quantileDocCount, + categorizerStateDocCount, + job.getId() + ); - delegate.onFailure(ExceptionsHelper.conflictStatusException( - "[" + resultDocCount + "] result and [" + (quantileDocCount + categorizerStateDocCount) + - "] state documents exist for a prior job with Id [" + job.getId() + "]. " + - "Please create the job with a different Id")); + delegate.onFailure( + ExceptionsHelper.conflictStatusException( + "[" + + resultDocCount + + "] result and [" + + (quantileDocCount + categorizerStateDocCount) + + "] state documents exist for a prior job with Id [" + + job.getId() + + "]. " + + "Please create the job with a different Id" + ) + ); } } }; - executeAsyncWithOrigin(client.threadPool().getThreadContext(), ML_ORIGIN, msearch.request(), searchResponseActionListener, - client::multiSearch); + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + ML_ORIGIN, + msearch.request(), + searchResponseActionListener, + client::multiSearch + ); } - /** * Create the Elasticsearch index and the mappings */ @@ -289,8 +307,12 @@ public void createJobResultIndex(Job job, ClusterState state, final ActionListen // if it is closed, we bailout and return an error if (concreteIndices.length == 0) { finalListener.onFailure( - ExceptionsHelper.badRequestException("Cannot create job [{}] as it requires closed index {}", job.getId(), - tempIndexName)); + ExceptionsHelper.badRequestException( + "Cannot create job [{}] as it requires closed index {}", + job.getId(), + tempIndexName + ) + ); return; } tempIndexName = concreteIndices[0]; @@ -298,43 +320,54 @@ public void createJobResultIndex(Job job, ClusterState state, final ActionListen final String indexName = tempIndexName; ActionListener indexAndMappingsListener = ActionListener.wrap(success -> { - final IndicesAliasesRequest request = - client.admin().indices().prepareAliases() - .addAliasAction( - IndicesAliasesRequest.AliasActions.add() - .index(indexName) - .alias(readAliasName) - .isHidden(true) - .filter(QueryBuilders.termQuery(Job.ID.getPreferredName(), job.getId()))) - .addAliasAction(IndicesAliasesRequest.AliasActions.add().index(indexName).alias(writeAliasName).isHidden(true)) - .request(); - executeAsyncWithOrigin(client.threadPool().getThreadContext(), ML_ORIGIN, request, - ActionListener.wrap(r -> finalListener.onResponse(true), finalListener::onFailure), - client.admin().indices()::aliases); - }, finalListener::onFailure); + final IndicesAliasesRequest request = client.admin() + .indices() + .prepareAliases() + .addAliasAction( + IndicesAliasesRequest.AliasActions.add() + .index(indexName) + .alias(readAliasName) + .isHidden(true) + .filter(QueryBuilders.termQuery(Job.ID.getPreferredName(), job.getId())) + ) + .addAliasAction(IndicesAliasesRequest.AliasActions.add().index(indexName).alias(writeAliasName).isHidden(true)) + .request(); + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + ML_ORIGIN, + request, + ActionListener.wrap(r -> finalListener.onResponse(true), finalListener::onFailure), + client.admin().indices()::aliases + ); + }, finalListener::onFailure); // Indices can be shared, so only create if it doesn't exist already. Saves us a roundtrip if // already in the CS if (state.getMetadata().hasIndex(indexName) == false) { LOGGER.trace("ES API CALL: create index {}", indexName); CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName); - executeAsyncWithOrigin(client.threadPool().getThreadContext(), ML_ORIGIN, createIndexRequest, - ActionListener.wrap( - // Add the term field mappings and alias. The complication is that the state at the - // beginning of the operation doesn't have any knowledge of the index, as it's only - // just been created. So we need yet another operation to get the mappings for it. - r -> getLatestIndexMappingsAndAddTerms(indexName, termFields, indexAndMappingsListener), - e -> { - // Possible that the index was created while the request was executing, - // so we need to handle that possibility - if (ExceptionsHelper.unwrapCause(e) instanceof ResourceAlreadyExistsException) { - LOGGER.info("Index [{}] already exists", indexName); - getLatestIndexMappingsAndAddTerms(indexName, termFields, indexAndMappingsListener); - } else { - finalListener.onFailure(e); - } - } - ), client.admin().indices()::create); + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + ML_ORIGIN, + createIndexRequest, + ActionListener.wrap( + // Add the term field mappings and alias. The complication is that the state at the + // beginning of the operation doesn't have any knowledge of the index, as it's only + // just been created. So we need yet another operation to get the mappings for it. + r -> getLatestIndexMappingsAndAddTerms(indexName, termFields, indexAndMappingsListener), + e -> { + // Possible that the index was created while the request was executing, + // so we need to handle that possibility + if (ExceptionsHelper.unwrapCause(e) instanceof ResourceAlreadyExistsException) { + LOGGER.info("Index [{}] already exists", indexName); + getLatestIndexMappingsAndAddTerms(indexName, termFields, indexAndMappingsListener); + } else { + finalListener.onFailure(e); + } + } + ), + client.admin().indices()::create + ); } else { MappingMetadata indexMappings = state.metadata().index(indexName).mapping(); addTermsMapping(indexMappings, indexName, termFields, indexAndMappingsListener); @@ -343,29 +376,38 @@ public void createJobResultIndex(Job job, ClusterState state, final ActionListen private void getLatestIndexMappingsAndAddTerms(String indexName, Collection termFields, ActionListener listener) { - ActionListener getMappingsListener = ActionListener.wrap( - getMappingsResponse -> { - // Expect one index. If this is not the case then it means the - // index has been deleted almost immediately after being created, and this is - // so unlikely that it's reasonable to fail the whole operation. - MappingMetadata indexMappings = getMappingsResponse.getMappings().iterator().next().value; - addTermsMapping(indexMappings, indexName, termFields, listener); - }, - listener::onFailure - ); + ActionListener getMappingsListener = ActionListener.wrap(getMappingsResponse -> { + // Expect one index. If this is not the case then it means the + // index has been deleted almost immediately after being created, and this is + // so unlikely that it's reasonable to fail the whole operation. + MappingMetadata indexMappings = getMappingsResponse.getMappings().iterator().next().value; + addTermsMapping(indexMappings, indexName, termFields, listener); + }, listener::onFailure); GetMappingsRequest getMappingsRequest = client.admin().indices().prepareGetMappings(indexName).request(); - executeAsyncWithOrigin(client.threadPool().getThreadContext(), ML_ORIGIN, getMappingsRequest, getMappingsListener, - client.admin().indices()::getMappings); + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + ML_ORIGIN, + getMappingsRequest, + getMappingsListener, + client.admin().indices()::getMappings + ); } - private void addTermsMapping(MappingMetadata mapping, String indexName, Collection termFields, - ActionListener listener) { + private void addTermsMapping( + MappingMetadata mapping, + String indexName, + Collection termFields, + ActionListener listener + ) { long fieldCountLimit = MapperService.INDEX_MAPPING_TOTAL_FIELDS_LIMIT_SETTING.get(settings); if (violatedFieldCountLimit(termFields.size(), fieldCountLimit, mapping)) { - String message = "Cannot create job in index '" + indexName + "' as the " + - MapperService.INDEX_MAPPING_TOTAL_FIELDS_LIMIT_SETTING.getKey() + " setting will be violated"; + String message = "Cannot create job in index '" + + indexName + + "' as the " + + MapperService.INDEX_MAPPING_TOTAL_FIELDS_LIMIT_SETTING.getKey() + + " setting will be violated"; listener.onFailure(new IllegalArgumentException(message)); } else { updateIndexMappingWithTermFields(indexName, termFields, listener); @@ -398,16 +440,20 @@ public static int countFields(Map mapping) { return count; } - private void updateIndexMappingWithTermFields(String indexName, Collection termFields, - ActionListener listener) { + private void updateIndexMappingWithTermFields(String indexName, Collection termFields, ActionListener listener) { try (XContentBuilder termFieldsMapping = JsonXContent.contentBuilder()) { createTermFieldsMapping(termFieldsMapping, termFields); - final PutMappingRequest request = client.admin().indices().preparePutMapping(indexName) - .setSource(termFieldsMapping).request(); - executeAsyncWithOrigin(client.threadPool().getThreadContext(), ML_ORIGIN, request, - listener.delegateFailure((l, putMappingResponse) -> - l.onResponse(putMappingResponse.isAcknowledged())), client.admin().indices()::putMapping); + final PutMappingRequest request = client.admin().indices().preparePutMapping(indexName).setSource(termFieldsMapping).request(); + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + ML_ORIGIN, + request, + listener.delegateFailure( + (l, putMappingResponse) -> l.onResponse(putMappingResponse.isAcknowledged()) + ), + client.admin().indices()::putMapping + ); } catch (IOException e) { listener.onFailure(e); } @@ -433,24 +479,33 @@ static void createTermFieldsMapping(XContentBuilder builder, Collection */ public void dataCounts(String jobId, Consumer handler, Consumer errorHandler) { String indexName = AnomalyDetectorsIndex.jobResultsAliasedName(jobId); - searchSingleResult(jobId, DataCounts.TYPE.getPreferredName(), createLatestDataCountsSearch(indexName, jobId), - DataCounts.PARSER, result -> handler.accept(result.result), errorHandler, () -> new DataCounts(jobId)); + searchSingleResult( + jobId, + DataCounts.TYPE.getPreferredName(), + createLatestDataCountsSearch(indexName, jobId), + DataCounts.PARSER, + result -> handler.accept(result.result), + errorHandler, + () -> new DataCounts(jobId) + ); } private SearchRequestBuilder createLatestDataCountsSearch(String indexName, String jobId) { return client.prepareSearch(indexName) - .setSize(1) - .setIndicesOptions(IndicesOptions.lenientExpandOpen()) - // look for both old and new formats - .setQuery(QueryBuilders.idsQuery().addIds(DataCounts.documentId(jobId), DataCounts.v54DocumentId(jobId))) - // We want to sort on log_time. However, this was added a long time later and before that we used to - // sort on latest_record_time. Thus we handle older data counts where no log_time exists and we fall back - // to the prior behaviour. - .addSort(SortBuilders.fieldSort(DataCounts.LOG_TIME.getPreferredName()) + .setSize(1) + .setIndicesOptions(IndicesOptions.lenientExpandOpen()) + // look for both old and new formats + .setQuery(QueryBuilders.idsQuery().addIds(DataCounts.documentId(jobId), DataCounts.v54DocumentId(jobId))) + // We want to sort on log_time. However, this was added a long time later and before that we used to + // sort on latest_record_time. Thus we handle older data counts where no log_time exists and we fall back + // to the prior behaviour. + .addSort( + SortBuilders.fieldSort(DataCounts.LOG_TIME.getPreferredName()) .order(SortOrder.DESC) .unmappedType(NumberFieldMapper.NumberType.LONG.typeName()) - .missing(0L)) - .addSort(SortBuilders.fieldSort(DataCounts.LATEST_RECORD_TIME.getPreferredName()).order(SortOrder.DESC)); + .missing(0L) + ) + .addSort(SortBuilders.fieldSort(DataCounts.LATEST_RECORD_TIME.getPreferredName()).order(SortOrder.DESC)); } /** @@ -467,7 +522,8 @@ public void timingStats(String jobId, Consumer handler, Consumer handler.accept(result.result), errorHandler, - () -> new TimingStats(jobId)); + () -> new TimingStats(jobId) + ); } private SearchRequestBuilder createLatestTimingStatsSearch(String indexName, String jobId) { @@ -494,54 +550,51 @@ public void datafeedTimingStats(List jobIds, ActionListenerwrap( - msearchResponse -> { - Map timingStatsByJobId = new HashMap<>(); - for (int i = 0; i < msearchResponse.getResponses().length; i++) { - String jobId = jobIds.get(i); - MultiSearchResponse.Item itemResponse = msearchResponse.getResponses()[i]; - if (itemResponse.isFailure()) { - listener.onFailure(itemResponse.getFailure()); - return; - } - SearchResponse searchResponse = itemResponse.getResponse(); - ShardSearchFailure[] shardFailures = searchResponse.getShardFailures(); - int unavailableShards = searchResponse.getTotalShards() - searchResponse.getSuccessfulShards(); - if (CollectionUtils.isEmpty(shardFailures) == false) { - LOGGER.error("[{}] Search request returned shard failures: {}", jobId, Arrays.toString(shardFailures)); - listener.onFailure( - new ElasticsearchException(ExceptionsHelper.shardFailuresToErrorMsg(jobId, shardFailures))); - return; - } - if (unavailableShards > 0) { - listener.onFailure( - new ElasticsearchException( - "[" + jobId + "] Search request encountered [" + unavailableShards + "] unavailable shards")); - return; - } - SearchHits hits = searchResponse.getHits(); - long hitsCount = hits.getHits().length; - if (hitsCount == 0 || hitsCount > 1) { - SearchRequest searchRequest = msearchRequest.requests().get(i); - LOGGER.debug("Found {} hits for [{}]", - hitsCount == 0 ? "0" : "multiple", - new Object[]{searchRequest.indices()}); - continue; - } - SearchHit hit = hits.getHits()[0]; - try { - DatafeedTimingStats timingStats = MlParserUtils.parse(hit, DatafeedTimingStats.PARSER); - timingStatsByJobId.put(jobId, timingStats); - } catch (Exception e) { - listener.onFailure(e); - return; - } + ActionListener.wrap(msearchResponse -> { + Map timingStatsByJobId = new HashMap<>(); + for (int i = 0; i < msearchResponse.getResponses().length; i++) { + String jobId = jobIds.get(i); + MultiSearchResponse.Item itemResponse = msearchResponse.getResponses()[i]; + if (itemResponse.isFailure()) { + listener.onFailure(itemResponse.getFailure()); + return; + } + SearchResponse searchResponse = itemResponse.getResponse(); + ShardSearchFailure[] shardFailures = searchResponse.getShardFailures(); + int unavailableShards = searchResponse.getTotalShards() - searchResponse.getSuccessfulShards(); + if (CollectionUtils.isEmpty(shardFailures) == false) { + LOGGER.error("[{}] Search request returned shard failures: {}", jobId, Arrays.toString(shardFailures)); + listener.onFailure(new ElasticsearchException(ExceptionsHelper.shardFailuresToErrorMsg(jobId, shardFailures))); + return; } - listener.onResponse(timingStatsByJobId); - }, - listener::onFailure - ), - client::multiSearch); + if (unavailableShards > 0) { + listener.onFailure( + new ElasticsearchException( + "[" + jobId + "] Search request encountered [" + unavailableShards + "] unavailable shards" + ) + ); + return; + } + SearchHits hits = searchResponse.getHits(); + long hitsCount = hits.getHits().length; + if (hitsCount == 0 || hitsCount > 1) { + SearchRequest searchRequest = msearchRequest.requests().get(i); + LOGGER.debug("Found {} hits for [{}]", hitsCount == 0 ? "0" : "multiple", new Object[] { searchRequest.indices() }); + continue; + } + SearchHit hit = hits.getHits()[0]; + try { + DatafeedTimingStats timingStats = MlParserUtils.parse(hit, DatafeedTimingStats.PARSER); + timingStatsByJobId.put(jobId, timingStats); + } catch (Exception e) { + listener.onFailure(e); + return; + } + } + listener.onResponse(timingStatsByJobId); + }, listener::onFailure), + client::multiSearch + ); } public void datafeedTimingStats(String jobId, Consumer handler, Consumer errorHandler) { @@ -553,7 +606,8 @@ public void datafeedTimingStats(String jobId, Consumer hand DatafeedTimingStats.PARSER, result -> handler.accept(result.result), errorHandler, - () -> new DatafeedTimingStats(jobId)); + () -> new DatafeedTimingStats(jobId) + ); } private SearchRequestBuilder createLatestDatafeedTimingStatsSearch(String indexName, String jobId) { @@ -561,27 +615,24 @@ private SearchRequestBuilder createLatestDatafeedTimingStatsSearch(String indexN .setSize(1) .setIndicesOptions(IndicesOptions.lenientExpandOpen()) .setQuery(QueryBuilders.idsQuery().addIds(DatafeedTimingStats.documentId(jobId))) - .addSort(SortBuilders.fieldSort(DatafeedTimingStats.TOTAL_SEARCH_TIME_MS.getPreferredName()) - .unmappedType("double").order(SortOrder.DESC)); + .addSort( + SortBuilders.fieldSort(DatafeedTimingStats.TOTAL_SEARCH_TIME_MS.getPreferredName()) + .unmappedType("double") + .order(SortOrder.DESC) + ); } - public void getAutodetectParams(Job job, String snapshotId, Consumer consumer, Consumer errorHandler) { + public void getAutodetectParams(Job job, String snapshotId, Consumer consumer, Consumer errorHandler) { String jobId = job.getId(); - ActionListener getScheduledEventsListener = ActionListener.wrap( - paramsBuilder -> { - ScheduledEventsQueryBuilder scheduledEventsQueryBuilder = new ScheduledEventsQueryBuilder(); - scheduledEventsQueryBuilder.start(job.earliestValidTimestamp(paramsBuilder.getDataCounts())); - scheduledEventsForJob(jobId, job.getGroups(), scheduledEventsQueryBuilder, ActionListener.wrap( - events -> { - paramsBuilder.setScheduledEvents(events.results()); - consumer.accept(paramsBuilder.build()); - }, - errorHandler - )); - }, - errorHandler - ); + ActionListener getScheduledEventsListener = ActionListener.wrap(paramsBuilder -> { + ScheduledEventsQueryBuilder scheduledEventsQueryBuilder = new ScheduledEventsQueryBuilder(); + scheduledEventsQueryBuilder.start(job.earliestValidTimestamp(paramsBuilder.getDataCounts())); + scheduledEventsForJob(jobId, job.getGroups(), scheduledEventsQueryBuilder, ActionListener.wrap(events -> { + paramsBuilder.setScheduledEvents(events.results()); + consumer.accept(paramsBuilder.build()); + }, errorHandler)); + }, errorHandler); AutodetectParams.Builder paramsBuilder = new AutodetectParams.Builder(job.getId()); String resultsIndex = AnomalyDetectorsIndex.jobResultsAliasedName(jobId); @@ -601,49 +652,52 @@ public void getAutodetectParams(Job job, String snapshotId, Consumerwrap( - response -> { - for (int i = 0; i < response.getResponses().length; i++) { - MultiSearchResponse.Item itemResponse = response.getResponses()[i]; - if (itemResponse.isFailure()) { - errorHandler.accept(itemResponse.getFailure()); - return; - } - SearchResponse searchResponse = itemResponse.getResponse(); - ShardSearchFailure[] shardFailures = searchResponse.getShardFailures(); - int unavailableShards = searchResponse.getTotalShards() - searchResponse.getSuccessfulShards(); - if (CollectionUtils.isEmpty(shardFailures) == false) { - LOGGER.error("[{}] Search request returned shard failures: {}", jobId, - Arrays.toString(shardFailures)); - errorHandler.accept(new ElasticsearchException( - ExceptionsHelper.shardFailuresToErrorMsg(jobId, shardFailures))); - return; - } - if (unavailableShards > 0) { - errorHandler.accept(new ElasticsearchException("[" + jobId - + "] Search request encountered [" + unavailableShards + "] unavailable shards")); + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + ML_ORIGIN, + msearch.request(), + ActionListener.wrap(response -> { + for (int i = 0; i < response.getResponses().length; i++) { + MultiSearchResponse.Item itemResponse = response.getResponses()[i]; + if (itemResponse.isFailure()) { + errorHandler.accept(itemResponse.getFailure()); + return; + } + SearchResponse searchResponse = itemResponse.getResponse(); + ShardSearchFailure[] shardFailures = searchResponse.getShardFailures(); + int unavailableShards = searchResponse.getTotalShards() - searchResponse.getSuccessfulShards(); + if (CollectionUtils.isEmpty(shardFailures) == false) { + LOGGER.error("[{}] Search request returned shard failures: {}", jobId, Arrays.toString(shardFailures)); + errorHandler.accept(new ElasticsearchException(ExceptionsHelper.shardFailuresToErrorMsg(jobId, shardFailures))); + return; + } + if (unavailableShards > 0) { + errorHandler.accept( + new ElasticsearchException( + "[" + jobId + "] Search request encountered [" + unavailableShards + "] unavailable shards" + ) + ); + return; + } + SearchHits hits = searchResponse.getHits(); + long hitsCount = hits.getHits().length; + if (hitsCount == 0) { + SearchRequest searchRequest = msearch.request().requests().get(i); + LOGGER.debug("Found 0 hits for [{}]", new Object[] { searchRequest.indices() }); + } + for (SearchHit hit : hits) { + try { + parseAutodetectParamSearchHit(jobId, paramsBuilder, hit); + } catch (Exception e) { + errorHandler.accept(e); return; } - SearchHits hits = searchResponse.getHits(); - long hitsCount = hits.getHits().length; - if (hitsCount == 0) { - SearchRequest searchRequest = msearch.request().requests().get(i); - LOGGER.debug("Found 0 hits for [{}]", new Object[]{searchRequest.indices()}); - } - for (SearchHit hit : hits) { - try { - parseAutodetectParamSearchHit(jobId, paramsBuilder, hit); - } catch (Exception e) { - errorHandler.accept(e); - return; - } - } } - getScheduledEventsListener.onResponse(paramsBuilder); - }, - errorHandler - ), client::multiSearch); + } + getScheduledEventsListener.onResponse(paramsBuilder); + }, errorHandler), + client::multiSearch + ); } public void getAutodetectParams(Job job, Consumer consumer, Consumer errorHandler) { @@ -651,19 +705,18 @@ public void getAutodetectParams(Job job, Consumer consumer, Co } private SearchRequestBuilder createDocIdSearch(String index, String id) { - return client.prepareSearch(index).setSize(1) - .setIndicesOptions(IndicesOptions.lenientExpandOpen()) - .setQuery(QueryBuilders.idsQuery().addIds(id)) - .setRouting(id); + return client.prepareSearch(index) + .setSize(1) + .setIndicesOptions(IndicesOptions.lenientExpandOpen()) + .setQuery(QueryBuilders.idsQuery().addIds(id)) + .setRouting(id); } /** * @throws ElasticsearchException when search hit cannot be parsed * @throws IllegalStateException when search hit has an unexpected ID */ - private static void parseAutodetectParamSearchHit(String jobId, - AutodetectParams.Builder paramsBuilder, - SearchHit hit) { + private static void parseAutodetectParamSearchHit(String jobId, AutodetectParams.Builder paramsBuilder, SearchHit hit) { String hitId = hit.getId(); if (DataCounts.documentId(jobId).equals(hitId)) { paramsBuilder.setDataCounts(MlParserUtils.parse(hit, DataCounts.PARSER)); @@ -688,8 +741,12 @@ private static void parseAutodetectParamSearchHit(String jobId, * Search for buckets with the parameters in the {@link BucketsQueryBuilder} * Uses the internal client, so runs as the _xpack user */ - public void bucketsViaInternalClient(String jobId, BucketsQueryBuilder query, Consumer> handler, - Consumer errorHandler) { + public void bucketsViaInternalClient( + String jobId, + BucketsQueryBuilder query, + Consumer> handler, + Consumer errorHandler + ) { buckets(jobId, query, handler, errorHandler, client); } @@ -697,49 +754,69 @@ public void bucketsViaInternalClient(String jobId, BucketsQueryBuilder query, Co * Search for buckets with the parameters in the {@link BucketsQueryBuilder} * Uses a supplied client, so may run as the currently authenticated user */ - public void buckets(String jobId, BucketsQueryBuilder query, Consumer> handler, Consumer errorHandler, - Client client) throws ResourceNotFoundException { + public void buckets( + String jobId, + BucketsQueryBuilder query, + Consumer> handler, + Consumer errorHandler, + Client client + ) throws ResourceNotFoundException { String indexName = AnomalyDetectorsIndex.jobResultsAliasedName(jobId); SearchRequest searchRequest = new SearchRequest(indexName); searchRequest.source(query.build().trackTotalHits(true)); searchRequest.indicesOptions(MlIndicesUtils.addIgnoreUnavailable(SearchRequest.DEFAULT_INDICES_OPTIONS)); - executeAsyncWithOrigin(client.threadPool().getThreadContext(), ML_ORIGIN, searchRequest, - ActionListener.wrap(searchResponse -> { - SearchHits hits = searchResponse.getHits(); - List results = new ArrayList<>(); - for (SearchHit hit : hits.getHits()) { - BytesReference source = hit.getSourceRef(); - try (InputStream stream = source.streamInput(); - XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) { - Bucket bucket = Bucket.LENIENT_PARSER.apply(parser, null); - results.add(bucket); - } catch (IOException e) { - throw new ElasticsearchParseException("failed to parse bucket", e); - } + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + ML_ORIGIN, + searchRequest, + ActionListener.wrap(searchResponse -> { + SearchHits hits = searchResponse.getHits(); + List results = new ArrayList<>(); + for (SearchHit hit : hits.getHits()) { + BytesReference source = hit.getSourceRef(); + try ( + InputStream stream = source.streamInput(); + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream) + ) { + Bucket bucket = Bucket.LENIENT_PARSER.apply(parser, null); + results.add(bucket); + } catch (IOException e) { + throw new ElasticsearchParseException("failed to parse bucket", e); } + } - if (query.hasTimestamp() && results.isEmpty()) { - throw QueryPage.emptyQueryPage(Bucket.RESULTS_FIELD); - } + if (query.hasTimestamp() && results.isEmpty()) { + throw QueryPage.emptyQueryPage(Bucket.RESULTS_FIELD); + } - QueryPage buckets = new QueryPage<>(results, - searchResponse.getHits().getTotalHits().value, Bucket.RESULTS_FIELD); + QueryPage buckets = new QueryPage<>(results, searchResponse.getHits().getTotalHits().value, Bucket.RESULTS_FIELD); - if (query.isExpand()) { - Iterator bucketsToExpand = buckets.results().stream() - .filter(bucket -> bucket.getBucketInfluencers().size() > 0).iterator(); - expandBuckets(jobId, query, buckets, bucketsToExpand, handler, errorHandler, client); - } else { - handler.accept(buckets); - } - }, e -> errorHandler.accept(mapAuthFailure(e, jobId, GetBucketsAction.NAME))), client::search); + if (query.isExpand()) { + Iterator bucketsToExpand = buckets.results() + .stream() + .filter(bucket -> bucket.getBucketInfluencers().size() > 0) + .iterator(); + expandBuckets(jobId, query, buckets, bucketsToExpand, handler, errorHandler, client); + } else { + handler.accept(buckets); + } + }, e -> errorHandler.accept(mapAuthFailure(e, jobId, GetBucketsAction.NAME))), + client::search + ); } - private void expandBuckets(String jobId, BucketsQueryBuilder query, QueryPage buckets, Iterator bucketsToExpand, - Consumer> handler, Consumer errorHandler, Client client) { + private void expandBuckets( + String jobId, + BucketsQueryBuilder query, + QueryPage buckets, + Iterator bucketsToExpand, + Consumer> handler, + Consumer errorHandler, + Client client + ) { if (bucketsToExpand.hasNext()) { Consumer c = i -> expandBuckets(jobId, query, buckets, bucketsToExpand, handler, errorHandler, client); expandBucket(jobId, query.isIncludeInterim(), bucketsToExpand.next(), c, errorHandler, client); @@ -778,31 +855,55 @@ public BatchedResultsIterator newBatchedRecordsIterator(String jo // This now gets the first 10K records for a bucket. The rate of records per bucket // is controlled by parameter in the c++ process and its default value is 500. Users may // change that. Issue elastic/machine-learning-cpp#73 is open to prevent this. - public void expandBucket(String jobId, boolean includeInterim, Bucket bucket, Consumer consumer, - Consumer errorHandler, Client client) { + public void expandBucket( + String jobId, + boolean includeInterim, + Bucket bucket, + Consumer consumer, + Consumer errorHandler, + Client client + ) { Consumer> h = page -> { bucket.getRecords().addAll(page.results()); consumer.accept(bucket.getRecords().size()); }; - bucketRecords(jobId, bucket, 0, RECORDS_SIZE_PARAM, includeInterim, AnomalyRecord.PROBABILITY.getPreferredName(), - false, h, errorHandler, client); + bucketRecords( + jobId, + bucket, + 0, + RECORDS_SIZE_PARAM, + includeInterim, + AnomalyRecord.PROBABILITY.getPreferredName(), + false, + h, + errorHandler, + client + ); } - public void bucketRecords(String jobId, Bucket bucket, int from, int size, boolean includeInterim, String sortField, - boolean descending, Consumer> handler, - Consumer errorHandler, Client client) { + public void bucketRecords( + String jobId, + Bucket bucket, + int from, + int size, + boolean includeInterim, + String sortField, + boolean descending, + Consumer> handler, + Consumer errorHandler, + Client client + ) { // Find the records using the time stamp rather than a parent-child - // relationship. The parent-child filter involves two queries behind + // relationship. The parent-child filter involves two queries behind // the scenes, and Elasticsearch documentation claims it's significantly - // slower. Here we rely on the record timestamps being identical to the + // slower. Here we rely on the record timestamps being identical to the // bucket timestamp. - RecordsQueryBuilder recordsQueryBuilder = new RecordsQueryBuilder() - .timestamp(bucket.getTimestamp()) - .from(from) - .size(size) - .includeInterim(includeInterim) - .sortField(sortField) - .sortDescending(descending); + RecordsQueryBuilder recordsQueryBuilder = new RecordsQueryBuilder().timestamp(bucket.getTimestamp()) + .from(from) + .size(size) + .includeInterim(includeInterim) + .sortField(sortField) + .sortDescending(descending); records(jobId, recordsQueryBuilder, handler, errorHandler, client); } @@ -817,16 +918,29 @@ public void bucketRecords(String jobId, Bucket bucket, int from, int size, boole * @param from Skip the first N categories. This parameter is for paging * @param size Take only this number of categories */ - public void categoryDefinitions(String jobId, Long categoryId, String partitionFieldValue, boolean augment, Integer from, Integer size, - Consumer> handler, - Consumer errorHandler, Client client) { + public void categoryDefinitions( + String jobId, + Long categoryId, + String partitionFieldValue, + boolean augment, + Integer from, + Integer size, + Consumer> handler, + Consumer errorHandler, + Client client + ) { if (categoryId != null && (from != null || size != null)) { throw new IllegalStateException("Both categoryId and pageParams are specified"); } String indexName = AnomalyDetectorsIndex.jobResultsAliasedName(jobId); - LOGGER.trace("ES API CALL: search all of category definitions from index {} sort ascending {} from {} size {}", - indexName, CategoryDefinition.CATEGORY_ID.getPreferredName(), from, size); + LOGGER.trace( + "ES API CALL: search all of category definitions from index {} sort ascending {} from {} size {}", + indexName, + CategoryDefinition.CATEGORY_ID.getPreferredName(), + from, + size + ); SearchRequest searchRequest = new SearchRequest(indexName); searchRequest.indicesOptions(MlIndicesUtils.addIgnoreUnavailable(searchRequest.indicesOptions())); @@ -838,14 +952,17 @@ public void categoryDefinitions(String jobId, Long categoryId, String partitionF // Note: Even though category definitions currently have a result_type field, this was not the case for older versions // So, until at least 9.x, this existsQuery is still the preferred way to gather category definition objects categoryIdQuery = QueryBuilders.existsQuery(CategoryDefinition.CATEGORY_ID.getPreferredName()); - sourceBuilder.from(from).size(size) - .sort(new FieldSortBuilder(CategoryDefinition.CATEGORY_ID.getPreferredName()).order(SortOrder.ASC)); + sourceBuilder.from(from) + .size(size) + .sort(new FieldSortBuilder(CategoryDefinition.CATEGORY_ID.getPreferredName()).order(SortOrder.ASC)); } else { throw new IllegalStateException("Both categoryId and pageParams are not specified"); } if (partitionFieldValue != null) { - QueryBuilder partitionQuery = - QueryBuilders.termQuery(CategoryDefinition.PARTITION_FIELD_VALUE.getPreferredName(), partitionFieldValue); + QueryBuilder partitionQuery = QueryBuilders.termQuery( + CategoryDefinition.PARTITION_FIELD_VALUE.getPreferredName(), + partitionFieldValue + ); QueryBuilder combinedQuery = QueryBuilders.boolQuery().must(categoryIdQuery).must(partitionQuery); sourceBuilder.query(combinedQuery); } else { @@ -853,28 +970,38 @@ public void categoryDefinitions(String jobId, Long categoryId, String partitionF } sourceBuilder.trackTotalHits(true); searchRequest.source(sourceBuilder); - executeAsyncWithOrigin(client.threadPool().getThreadContext(), ML_ORIGIN, searchRequest, - ActionListener.wrap(searchResponse -> { - SearchHit[] hits = searchResponse.getHits().getHits(); - List results = new ArrayList<>(hits.length); - for (SearchHit hit : hits) { - BytesReference source = hit.getSourceRef(); - try (InputStream stream = source.streamInput(); - XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) { - CategoryDefinition categoryDefinition = CategoryDefinition.LENIENT_PARSER.apply(parser, null); - if (augment) { - augmentWithGrokPattern(categoryDefinition); - } - results.add(categoryDefinition); - } catch (IOException e) { - throw new ElasticsearchParseException("failed to parse category definition", e); + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + ML_ORIGIN, + searchRequest, + ActionListener.wrap(searchResponse -> { + SearchHit[] hits = searchResponse.getHits().getHits(); + List results = new ArrayList<>(hits.length); + for (SearchHit hit : hits) { + BytesReference source = hit.getSourceRef(); + try ( + InputStream stream = source.streamInput(); + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream) + ) { + CategoryDefinition categoryDefinition = CategoryDefinition.LENIENT_PARSER.apply(parser, null); + if (augment) { + augmentWithGrokPattern(categoryDefinition); } + results.add(categoryDefinition); + } catch (IOException e) { + throw new ElasticsearchParseException("failed to parse category definition", e); } - QueryPage result = - new QueryPage<>(results, searchResponse.getHits().getTotalHits().value, CategoryDefinition.RESULTS_FIELD); - handler.accept(result); - }, e -> errorHandler.accept(mapAuthFailure(e, jobId, GetCategoriesAction.NAME))), client::search); + } + QueryPage result = new QueryPage<>( + results, + searchResponse.getHits().getTotalHits().value, + CategoryDefinition.RESULTS_FIELD + ); + handler.accept(result); + }, e -> errorHandler.accept(mapAuthFailure(e, jobId, GetCategoriesAction.NAME))), + client::search + ); } void augmentWithGrokPattern(CategoryDefinition categoryDefinition) { @@ -883,8 +1010,9 @@ void augmentWithGrokPattern(CategoryDefinition categoryDefinition) { if (examples.isEmpty() || regex.isEmpty()) { categoryDefinition.setGrokPattern(""); } else { - categoryDefinition.setGrokPattern(GrokPatternCreator.findBestGrokMatchFromExamples(categoryDefinition.getJobId(), - regex, examples)); + categoryDefinition.setGrokPattern( + GrokPatternCreator.findBestGrokMatchFromExamples(categoryDefinition.getJobId(), regex, examples) + ); } } @@ -893,8 +1021,13 @@ void augmentWithGrokPattern(CategoryDefinition categoryDefinition) { * {@link RecordsQueryBuilder} * Uses a supplied client, so may run as the currently authenticated user */ - public void records(String jobId, RecordsQueryBuilder recordsQueryBuilder, Consumer> handler, - Consumer errorHandler, Client client) { + public void records( + String jobId, + RecordsQueryBuilder recordsQueryBuilder, + Consumer> handler, + Consumer errorHandler, + Client client + ) { String indexName = AnomalyDetectorsIndex.jobResultsAliasedName(jobId); SearchSourceBuilder searchSourceBuilder = recordsQueryBuilder.build(); @@ -903,23 +1036,33 @@ public void records(String jobId, RecordsQueryBuilder recordsQueryBuilder, Consu searchRequest.source(recordsQueryBuilder.build().trackTotalHits(true)); LOGGER.trace("ES API CALL: search all of records from index {} with query {}", indexName, searchSourceBuilder); - executeAsyncWithOrigin(client.threadPool().getThreadContext(), ML_ORIGIN, searchRequest, - ActionListener.wrap(searchResponse -> { - List results = new ArrayList<>(); - for (SearchHit hit : searchResponse.getHits().getHits()) { - BytesReference source = hit.getSourceRef(); - try (InputStream stream = source.streamInput(); - XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) { - results.add(AnomalyRecord.LENIENT_PARSER.apply(parser, null)); - } catch (IOException e) { - throw new ElasticsearchParseException("failed to parse records", e); - } + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + ML_ORIGIN, + searchRequest, + ActionListener.wrap(searchResponse -> { + List results = new ArrayList<>(); + for (SearchHit hit : searchResponse.getHits().getHits()) { + BytesReference source = hit.getSourceRef(); + try ( + InputStream stream = source.streamInput(); + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream) + ) { + results.add(AnomalyRecord.LENIENT_PARSER.apply(parser, null)); + } catch (IOException e) { + throw new ElasticsearchParseException("failed to parse records", e); } - QueryPage queryPage = - new QueryPage<>(results, searchResponse.getHits().getTotalHits().value, AnomalyRecord.RESULTS_FIELD); - handler.accept(queryPage); - }, e -> errorHandler.accept(mapAuthFailure(e, jobId, GetRecordsAction.NAME))), client::search); + } + QueryPage queryPage = new QueryPage<>( + results, + searchResponse.getHits().getTotalHits().value, + AnomalyRecord.RESULTS_FIELD + ); + handler.accept(queryPage); + }, e -> errorHandler.accept(mapAuthFailure(e, jobId, GetRecordsAction.NAME))), + client::search + ); } /** @@ -928,47 +1071,66 @@ public void records(String jobId, RecordsQueryBuilder recordsQueryBuilder, Consu * @param jobId The job ID for which influencers are requested * @param query the query */ - public void influencers(String jobId, InfluencersQuery query, Consumer> handler, - Consumer errorHandler, Client client) { - QueryBuilder fb = new ResultsFilterBuilder() - .timeRange(Result.TIMESTAMP.getPreferredName(), query.getStart(), query.getEnd()) - .score(Influencer.INFLUENCER_SCORE.getPreferredName(), query.getInfluencerScoreFilter()) - .interim(query.isIncludeInterim()) - .build(); + public void influencers( + String jobId, + InfluencersQuery query, + Consumer> handler, + Consumer errorHandler, + Client client + ) { + QueryBuilder fb = new ResultsFilterBuilder().timeRange(Result.TIMESTAMP.getPreferredName(), query.getStart(), query.getEnd()) + .score(Influencer.INFLUENCER_SCORE.getPreferredName(), query.getInfluencerScoreFilter()) + .interim(query.isIncludeInterim()) + .build(); String indexName = AnomalyDetectorsIndex.jobResultsAliasedName(jobId); - LOGGER.trace("ES API CALL: search all of influencers from index {}{} with filter from {} size {}", () -> indexName, - () -> (query.getSortField() != null) ? - " with sort " + (query.isSortDescending() ? "descending" : "ascending") + " on field " + query.getSortField() : "", - query::getFrom, query::getSize); + LOGGER.trace( + "ES API CALL: search all of influencers from index {}{} with filter from {} size {}", + () -> indexName, + () -> (query.getSortField() != null) + ? " with sort " + (query.isSortDescending() ? "descending" : "ascending") + " on field " + query.getSortField() + : "", + query::getFrom, + query::getSize + ); - QueryBuilder qb = new BoolQueryBuilder() - .filter(fb) - .filter(new TermsQueryBuilder(Result.RESULT_TYPE.getPreferredName(), Influencer.RESULT_TYPE_VALUE)); + QueryBuilder qb = new BoolQueryBuilder().filter(fb) + .filter(new TermsQueryBuilder(Result.RESULT_TYPE.getPreferredName(), Influencer.RESULT_TYPE_VALUE)); SearchRequest searchRequest = new SearchRequest(indexName); searchRequest.indicesOptions(MlIndicesUtils.addIgnoreUnavailable(searchRequest.indicesOptions())); - FieldSortBuilder sb = query.getSortField() == null ? SortBuilders.fieldSort(ElasticsearchMappings.ES_DOC) - : new FieldSortBuilder(query.getSortField()).order(query.isSortDescending() ? SortOrder.DESC : SortOrder.ASC); + FieldSortBuilder sb = query.getSortField() == null + ? SortBuilders.fieldSort(ElasticsearchMappings.ES_DOC) + : new FieldSortBuilder(query.getSortField()).order(query.isSortDescending() ? SortOrder.DESC : SortOrder.ASC); searchRequest.source(new SearchSourceBuilder().query(qb).from(query.getFrom()).size(query.getSize()).sort(sb).trackTotalHits(true)); - executeAsyncWithOrigin(client.threadPool().getThreadContext(), ML_ORIGIN, searchRequest, - ActionListener.wrap(response -> { - List influencers = new ArrayList<>(); - for (SearchHit hit : response.getHits().getHits()) { - BytesReference source = hit.getSourceRef(); - try (InputStream stream = source.streamInput(); - XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) { - influencers.add(Influencer.LENIENT_PARSER.apply(parser, null)); - } catch (IOException e) { - throw new ElasticsearchParseException("failed to parse influencer", e); - } + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + ML_ORIGIN, + searchRequest, + ActionListener.wrap(response -> { + List influencers = new ArrayList<>(); + for (SearchHit hit : response.getHits().getHits()) { + BytesReference source = hit.getSourceRef(); + try ( + InputStream stream = source.streamInput(); + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream) + ) { + influencers.add(Influencer.LENIENT_PARSER.apply(parser, null)); + } catch (IOException e) { + throw new ElasticsearchParseException("failed to parse influencer", e); } - QueryPage result = - new QueryPage<>(influencers, response.getHits().getTotalHits().value, Influencer.RESULTS_FIELD); - handler.accept(result); - }, e -> errorHandler.accept(mapAuthFailure(e, jobId, GetInfluencersAction.NAME))), client::search); + } + QueryPage result = new QueryPage<>( + influencers, + response.getHits().getTotalHits().value, + Influencer.RESULTS_FIELD + ); + handler.accept(result); + }, e -> errorHandler.accept(mapAuthFailure(e, jobId, GetInfluencersAction.NAME))), + client::search + ); } /** @@ -985,17 +1147,27 @@ public BatchedResultsIterator newBatchedInfluencersIterator(String j /** * Get a job's model snapshot by its id */ - public void getModelSnapshot(String jobId, @Nullable String modelSnapshotId, Consumer> handler, - Consumer errorHandler) { + public void getModelSnapshot( + String jobId, + @Nullable String modelSnapshotId, + Consumer> handler, + Consumer errorHandler + ) { if (modelSnapshotId == null) { handler.accept(null); return; } String resultsIndex = AnomalyDetectorsIndex.jobResultsAliasedName(jobId); SearchRequestBuilder search = createDocIdSearch(resultsIndex, ModelSnapshot.documentId(jobId, modelSnapshotId)); - searchSingleResult(jobId, ModelSnapshot.TYPE.getPreferredName(), search, ModelSnapshot.LENIENT_PARSER, - result -> handler.accept(result.result == null ? null : new Result(result.index, result.result.build())), - errorHandler, () -> null); + searchSingleResult( + jobId, + ModelSnapshot.TYPE.getPreferredName(), + search, + ModelSnapshot.LENIENT_PARSER, + result -> handler.accept(result.result == null ? null : new Result(result.index, result.result.build())), + errorHandler, + () -> null + ); } /** @@ -1007,8 +1179,13 @@ public void getModelSnapshot(String jobId, @Nullable String modelSnapshotId, Con * @param from number of snapshots to from * @param size number of snapshots to retrieve */ - public void modelSnapshots(String jobId, int from, int size, Consumer> handler, - Consumer errorHandler) { + public void modelSnapshots( + String jobId, + int from, + int size, + Consumer> handler, + Consumer errorHandler + ) { modelSnapshots(jobId, from, size, null, true, QueryBuilders.matchAllQuery(), handler, errorHandler); } @@ -1026,43 +1203,45 @@ public void modelSnapshots(String jobId, int from, int size, Consumer> handler, - Consumer errorHandler) { + public void modelSnapshots( + String jobId, + int from, + int size, + String startEpochMs, + String endEpochMs, + String sortField, + boolean sortDescending, + String snapshotId, + Consumer> handler, + Consumer errorHandler + ) { String[] snapshotIds = Strings.splitStringByCommaToArray(snapshotId); - QueryBuilder qb = new ResultsFilterBuilder() - .resourceTokenFilters(ModelSnapshotField.SNAPSHOT_ID.getPreferredName(), snapshotIds) + QueryBuilder qb = new ResultsFilterBuilder().resourceTokenFilters(ModelSnapshotField.SNAPSHOT_ID.getPreferredName(), snapshotIds) .timeRange(Result.TIMESTAMP.getPreferredName(), startEpochMs, endEpochMs) .build(); modelSnapshots(jobId, from, size, sortField, sortDescending, qb, handler, errorHandler); } - private void modelSnapshots(String jobId, - int from, - int size, - String sortField, - boolean sortDescending, - QueryBuilder qb, - Consumer> handler, - Consumer errorHandler) { + private void modelSnapshots( + String jobId, + int from, + int size, + String sortField, + boolean sortDescending, + QueryBuilder qb, + Consumer> handler, + Consumer errorHandler + ) { if (Strings.isEmpty(sortField)) { sortField = ModelSnapshot.TIMESTAMP.getPreferredName(); } QueryBuilder finalQuery = QueryBuilders.boolQuery() - .filter(QueryBuilders.existsQuery(ModelSnapshot.SNAPSHOT_DOC_COUNT.getPreferredName())) - .must(qb); + .filter(QueryBuilders.existsQuery(ModelSnapshot.SNAPSHOT_DOC_COUNT.getPreferredName())) + .must(qb); - FieldSortBuilder sb = new FieldSortBuilder(sortField) - .order(sortDescending ? SortOrder.DESC : SortOrder.ASC); + FieldSortBuilder sb = new FieldSortBuilder(sortField).order(sortDescending ? SortOrder.DESC : SortOrder.ASC); // `min_version` might not be present in very early snapshots. // Consequently, we should treat it as being at least from 6.3.0 or before // Also, if no jobs have been opened since the previous versions, the .ml-anomalies-* index may not have @@ -1072,8 +1251,13 @@ private void modelSnapshots(String jobId, } String indexName = AnomalyDetectorsIndex.jobResultsAliasedName(jobId); - LOGGER.trace("ES API CALL: search all model snapshots from index {} sort ascending {} with filter after sort from {} size {}", - indexName, sortField, from, size); + LOGGER.trace( + "ES API CALL: search all model snapshots from index {} sort ascending {} with filter after sort from {} size {}", + indexName, + sortField, + from, + size + ); SearchRequest searchRequest = new SearchRequest(indexName); searchRequest.indicesOptions(MlIndicesUtils.addIgnoreUnavailable(searchRequest.indicesOptions())); @@ -1085,17 +1269,25 @@ private void modelSnapshots(String jobId, sourceBuilder.trackTotalHits(true); sourceBuilder.fetchSource(REMOVE_QUANTILES_FROM_SOURCE); searchRequest.source(sourceBuilder); - executeAsyncWithOrigin(client.threadPool().getThreadContext(), ML_ORIGIN, searchRequest, - ActionListener.wrap(searchResponse -> { - List results = new ArrayList<>(); - for (SearchHit hit : searchResponse.getHits().getHits()) { - results.add(ModelSnapshot.fromJson(hit.getSourceRef())); - } + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + ML_ORIGIN, + searchRequest, + ActionListener.wrap(searchResponse -> { + List results = new ArrayList<>(); + for (SearchHit hit : searchResponse.getHits().getHits()) { + results.add(ModelSnapshot.fromJson(hit.getSourceRef())); + } - QueryPage result = - new QueryPage<>(results, searchResponse.getHits().getTotalHits().value, ModelSnapshot.RESULTS_FIELD); - handler.accept(result); - }, errorHandler), client::search); + QueryPage result = new QueryPage<>( + results, + searchResponse.getHits().getTotalHits().value, + ModelSnapshot.RESULTS_FIELD + ); + handler.accept(result); + }, errorHandler), + client::search + ); } public QueryPage modelPlot(String jobId, int from, int size) { @@ -1105,20 +1297,23 @@ public QueryPage modelPlot(String jobId, int from, int size) { try (ThreadContext.StoredContext ignore = client.threadPool().getThreadContext().stashWithOrigin(ML_ORIGIN)) { searchResponse = client.prepareSearch(indexName) - .setIndicesOptions(MlIndicesUtils.addIgnoreUnavailable(SearchRequest.DEFAULT_INDICES_OPTIONS)) - .setQuery(new TermsQueryBuilder(Result.RESULT_TYPE.getPreferredName(), ModelPlot.RESULT_TYPE_VALUE)) - .setFrom(from).setSize(size) - .setTrackTotalHits(true) - .get(); + .setIndicesOptions(MlIndicesUtils.addIgnoreUnavailable(SearchRequest.DEFAULT_INDICES_OPTIONS)) + .setQuery(new TermsQueryBuilder(Result.RESULT_TYPE.getPreferredName(), ModelPlot.RESULT_TYPE_VALUE)) + .setFrom(from) + .setSize(size) + .setTrackTotalHits(true) + .get(); } List results = new ArrayList<>(); for (SearchHit hit : searchResponse.getHits().getHits()) { BytesReference source = hit.getSourceRef(); - try (InputStream stream = source.streamInput(); - XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) { + try ( + InputStream stream = source.streamInput(); + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream) + ) { ModelPlot modelPlot = ModelPlot.LENIENT_PARSER.apply(parser, null); results.add(modelPlot); } catch (IOException e) { @@ -1138,7 +1333,8 @@ public QueryPage categorizerStats(String jobId, int from, int searchResponse = client.prepareSearch(indexName) .setIndicesOptions(MlIndicesUtils.addIgnoreUnavailable(SearchRequest.DEFAULT_INDICES_OPTIONS)) .setQuery(new TermsQueryBuilder(Result.RESULT_TYPE.getPreferredName(), CategorizerStats.RESULT_TYPE_VALUE)) - .setFrom(from).setSize(size) + .setFrom(from) + .setSize(size) .setTrackTotalHits(true) .get(); } @@ -1147,9 +1343,11 @@ public QueryPage categorizerStats(String jobId, int from, int for (SearchHit hit : searchResponse.getHits().getHits()) { BytesReference source = hit.getSourceRef(); - try (InputStream stream = source.streamInput(); - XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) { + try ( + InputStream stream = source.streamInput(); + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream) + ) { CategorizerStats categorizerStats = CategorizerStats.LENIENT_PARSER.apply(parser, null).build(); results.add(categorizerStats); } catch (IOException e) { @@ -1167,43 +1365,60 @@ public void modelSizeStats(String jobId, Consumer handler, Consu LOGGER.trace("ES API CALL: search latest {} for job {}", ModelSizeStats.RESULT_TYPE_VALUE, jobId); String indexName = AnomalyDetectorsIndex.jobResultsAliasedName(jobId); - searchSingleResult(jobId, ModelSizeStats.RESULT_TYPE_VALUE, createLatestModelSizeStatsSearch(indexName), - ModelSizeStats.LENIENT_PARSER, - result -> handler.accept(result.result.build()), errorHandler, - () -> new ModelSizeStats.Builder(jobId)); + searchSingleResult( + jobId, + ModelSizeStats.RESULT_TYPE_VALUE, + createLatestModelSizeStatsSearch(indexName), + ModelSizeStats.LENIENT_PARSER, + result -> handler.accept(result.result.build()), + errorHandler, + () -> new ModelSizeStats.Builder(jobId) + ); } - private void searchSingleResult(String jobId, String resultDescription, SearchRequestBuilder search, - BiFunction objectParser, Consumer> handler, - Consumer errorHandler, Supplier notFoundSupplier) { - executeAsyncWithOrigin(client.threadPool().getThreadContext(), ML_ORIGIN, search.request(), - ActionListener.wrap( - response -> { - SearchHit[] hits = response.getHits().getHits(); - if (hits.length == 0) { - LOGGER.trace("No {} for job with id {}", resultDescription, jobId); - handler.accept(new Result<>(null, notFoundSupplier.get())); - } else if (hits.length == 1) { - try { - T result = MlParserUtils.parse(hits[0], objectParser); - handler.accept(new Result<>(hits[0].getIndex(), result)); - } catch (Exception e) { - errorHandler.accept(e); - } - } else { - errorHandler.accept(new IllegalStateException("Search for unique [" + resultDescription + "] returned [" - + hits.length + "] hits even though size was 1")); - } - }, errorHandler - ), client::search); + private void searchSingleResult( + String jobId, + String resultDescription, + SearchRequestBuilder search, + BiFunction objectParser, + Consumer> handler, + Consumer errorHandler, + Supplier notFoundSupplier + ) { + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + ML_ORIGIN, + search.request(), + ActionListener.wrap(response -> { + SearchHit[] hits = response.getHits().getHits(); + if (hits.length == 0) { + LOGGER.trace("No {} for job with id {}", resultDescription, jobId); + handler.accept(new Result<>(null, notFoundSupplier.get())); + } else if (hits.length == 1) { + try { + T result = MlParserUtils.parse(hits[0], objectParser); + handler.accept(new Result<>(hits[0].getIndex(), result)); + } catch (Exception e) { + errorHandler.accept(e); + } + } else { + errorHandler.accept( + new IllegalStateException( + "Search for unique [" + resultDescription + "] returned [" + hits.length + "] hits even though size was 1" + ) + ); + } + }, errorHandler), + client::search + ); } private SearchRequestBuilder createLatestModelSizeStatsSearch(String indexName) { return client.prepareSearch(indexName) - .setSize(1) - .setIndicesOptions(IndicesOptions.lenientExpandOpen()) - .setQuery(QueryBuilders.termQuery(Result.RESULT_TYPE.getPreferredName(), ModelSizeStats.RESULT_TYPE_VALUE)) - .addSort(SortBuilders.fieldSort(ModelSizeStats.LOG_TIME_FIELD.getPreferredName()).order(SortOrder.DESC)); + .setSize(1) + .setIndicesOptions(IndicesOptions.lenientExpandOpen()) + .setQuery(QueryBuilders.termQuery(Result.RESULT_TYPE.getPreferredName(), ModelSizeStats.RESULT_TYPE_VALUE)) + .addSort(SortBuilders.fieldSort(ModelSizeStats.LOG_TIME_FIELD.getPreferredName()).order(SortOrder.DESC)); } /** @@ -1226,20 +1441,32 @@ private SearchRequestBuilder createLatestModelSizeStatsSearch(String indexName) * specified job, or 0 if memory usage is not yet established * @param errorHandler if a problem occurs, the exception will be passed to this handler */ - public void getEstablishedMemoryUsage(String jobId, Date latestBucketTimestamp, ModelSizeStats latestModelSizeStats, - Consumer handler, Consumer errorHandler) { + public void getEstablishedMemoryUsage( + String jobId, + Date latestBucketTimestamp, + ModelSizeStats latestModelSizeStats, + Consumer handler, + Consumer errorHandler + ) { if (latestModelSizeStats != null) { calculateEstablishedMemoryUsage(jobId, latestBucketTimestamp, latestModelSizeStats, handler, errorHandler); } else { - modelSizeStats(jobId, + modelSizeStats( + jobId, modelSizeStats -> calculateEstablishedMemoryUsage(jobId, latestBucketTimestamp, modelSizeStats, handler, errorHandler), - errorHandler); + errorHandler + ); } } - void calculateEstablishedMemoryUsage(String jobId, Date latestBucketTimestamp, ModelSizeStats latestModelSizeStats, - Consumer handler, Consumer errorHandler) { + void calculateEstablishedMemoryUsage( + String jobId, + Date latestBucketTimestamp, + ModelSizeStats latestModelSizeStats, + Consumer handler, + Consumer errorHandler + ) { assert latestModelSizeStats != null; @@ -1262,48 +1489,55 @@ void calculateEstablishedMemoryUsage(String jobId, Date latestBucketTimestamp, M String indexName = AnomalyDetectorsIndex.jobResultsAliasedName(jobId); // Step 2. Find the count, mean and standard deviation of memory usage over the time span of the last N bucket results, - // where N is the number of buckets required to consider memory usage "established" + // where N is the number of buckets required to consider memory usage "established" Consumer> bucketHandler = buckets -> { if (buckets.results().size() == 1) { String searchFromTimeMs = Long.toString(buckets.results().get(0).getTimestamp().getTime()); SearchRequestBuilder search = client.prepareSearch(indexName) - .setSize(0) - .setIndicesOptions(IndicesOptions.lenientExpandOpen()) - .setQuery(new BoolQueryBuilder() - .filter(QueryBuilders.rangeQuery(Result.TIMESTAMP.getPreferredName()).gte(searchFromTimeMs)) - .filter(QueryBuilders.termQuery(Result.RESULT_TYPE.getPreferredName(), ModelSizeStats.RESULT_TYPE_VALUE))) - .addAggregation(AggregationBuilders.extendedStats("es").field(ModelSizeStats.MODEL_BYTES_FIELD.getPreferredName())); - - executeAsyncWithOrigin(client.threadPool().getThreadContext(), ML_ORIGIN, search.request(), - ActionListener.wrap( - response -> { - List aggregations = response.getAggregations().asList(); - if (aggregations.size() == 1) { - ExtendedStats extendedStats = (ExtendedStats) aggregations.get(0); - long count = extendedStats.getCount(); - if (count <= 1) { - // model size stats either haven't changed in the last N buckets, - // so the latest (older) ones are established, or have only changed - // once, so again there's no recent variation - handler.accept(latestModelSizeStats.getModelBytes()); - } else { - double coefficientOfVaration = extendedStats.getStdDeviation() / extendedStats.getAvg(); - LOGGER.trace("[{}] Coefficient of variation [{}] when calculating established memory use", - jobId, coefficientOfVaration); - // is there sufficient stability in the latest model size stats readings? - if (coefficientOfVaration <= ESTABLISHED_MEMORY_CV_THRESHOLD) { - // yes, so return the latest model size as established - handler.accept(latestModelSizeStats.getModelBytes()); - } else { - // no - we don't have an established model size - handler.accept(0L); - } - } - } else { - handler.accept(0L); - } - }, errorHandler - ), client::search); + .setSize(0) + .setIndicesOptions(IndicesOptions.lenientExpandOpen()) + .setQuery( + new BoolQueryBuilder().filter(QueryBuilders.rangeQuery(Result.TIMESTAMP.getPreferredName()).gte(searchFromTimeMs)) + .filter(QueryBuilders.termQuery(Result.RESULT_TYPE.getPreferredName(), ModelSizeStats.RESULT_TYPE_VALUE)) + ) + .addAggregation(AggregationBuilders.extendedStats("es").field(ModelSizeStats.MODEL_BYTES_FIELD.getPreferredName())); + + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + ML_ORIGIN, + search.request(), + ActionListener.wrap(response -> { + List aggregations = response.getAggregations().asList(); + if (aggregations.size() == 1) { + ExtendedStats extendedStats = (ExtendedStats) aggregations.get(0); + long count = extendedStats.getCount(); + if (count <= 1) { + // model size stats either haven't changed in the last N buckets, + // so the latest (older) ones are established, or have only changed + // once, so again there's no recent variation + handler.accept(latestModelSizeStats.getModelBytes()); + } else { + double coefficientOfVaration = extendedStats.getStdDeviation() / extendedStats.getAvg(); + LOGGER.trace( + "[{}] Coefficient of variation [{}] when calculating established memory use", + jobId, + coefficientOfVaration + ); + // is there sufficient stability in the latest model size stats readings? + if (coefficientOfVaration <= ESTABLISHED_MEMORY_CV_THRESHOLD) { + // yes, so return the latest model size as established + handler.accept(latestModelSizeStats.getModelBytes()); + } else { + // no - we don't have an established model size + handler.accept(0L); + } + } + } else { + handler.accept(0L); + } + }, errorHandler), + client::search + ); } else { LOGGER.trace("[{}] Insufficient history to calculate established memory use", jobId); handler.accept(0L); @@ -1311,12 +1545,15 @@ void calculateEstablishedMemoryUsage(String jobId, Date latestBucketTimestamp, M }; // Step 1. Find the time span of the most recent N bucket results, where N is the number of buckets - // required to consider memory usage "established" - BucketsQueryBuilder bucketQuery = new BucketsQueryBuilder() - .end(latestBucketTimestamp != null ? Long.toString(latestBucketTimestamp.getTime() + 1) : null) - .sortField(Result.TIMESTAMP.getPreferredName()) - .sortDescending(true).from(BUCKETS_FOR_ESTABLISHED_MEMORY_SIZE - 1).size(1) - .includeInterim(false); + // required to consider memory usage "established" + BucketsQueryBuilder bucketQuery = new BucketsQueryBuilder().end( + latestBucketTimestamp != null ? Long.toString(latestBucketTimestamp.getTime() + 1) : null + ) + .sortField(Result.TIMESTAMP.getPreferredName()) + .sortDescending(true) + .from(BUCKETS_FOR_ESTABLISHED_MEMORY_SIZE - 1) + .size(1) + .includeInterim(false); bucketsViaInternalClient(jobId, bucketQuery, bucketHandler, e -> { if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) { handler.accept(0L); @@ -1326,22 +1563,23 @@ void calculateEstablishedMemoryUsage(String jobId, Date latestBucketTimestamp, M }); } - public void scheduledEventsForJob(String jobId, List jobGroups, ScheduledEventsQueryBuilder queryBuilder, - ActionListener> handler) { + public void scheduledEventsForJob( + String jobId, + List jobGroups, + ScheduledEventsQueryBuilder queryBuilder, + ActionListener> handler + ) { // Find all the calendars used by the job then the events for those calendars - ActionListener> calendarsListener = ActionListener.wrap( - calendars -> { - if (calendars.results().isEmpty()) { - handler.onResponse(new QueryPage<>(Collections.emptyList(), 0, ScheduledEvent.RESULTS_FIELD)); - return; - } - String[] calendarIds = calendars.results().stream().map(Calendar::getId).toArray(String[]::new); - queryBuilder.calendarIds(calendarIds); - scheduledEvents(queryBuilder, handler); - }, - handler::onFailure - ); + ActionListener> calendarsListener = ActionListener.wrap(calendars -> { + if (calendars.results().isEmpty()) { + handler.onResponse(new QueryPage<>(Collections.emptyList(), 0, ScheduledEvent.RESULTS_FIELD)); + return; + } + String[] calendarIds = calendars.results().stream().map(Calendar::getId).toArray(String[]::new); + queryBuilder.calendarIds(calendarIds); + scheduledEvents(queryBuilder, handler); + }, handler::onFailure); CalendarQueryBuilder query = new CalendarQueryBuilder().jobId(jobId).jobGroups(jobGroups); calendars(query, calendarsListener); @@ -1349,79 +1587,91 @@ public void scheduledEventsForJob(String jobId, List jobGroups, Schedule public void scheduledEvents(ScheduledEventsQueryBuilder query, ActionListener> handler) { SearchRequestBuilder request = client.prepareSearch(MlMetaIndex.indexName()) - .setIndicesOptions(IndicesOptions.lenientExpandOpen()) - .setSource(query.build()) - .setTrackTotalHits(true); - - executeAsyncWithOrigin(client.threadPool().getThreadContext(), ML_ORIGIN, request.request(), - ActionListener.wrap( - response -> { - List events = new ArrayList<>(); - SearchHit[] hits = response.getHits().getHits(); - try { - for (SearchHit hit : hits) { - ScheduledEvent.Builder event = MlParserUtils.parse(hit, ScheduledEvent.LENIENT_PARSER); - - event.eventId(hit.getId()); - events.add(event.build()); - } - handler.onResponse(new QueryPage<>(events, response.getHits().getTotalHits().value, - ScheduledEvent.RESULTS_FIELD)); - } catch (Exception e) { - handler.onFailure(e); - } - }, - handler::onFailure), - client::search); + .setIndicesOptions(IndicesOptions.lenientExpandOpen()) + .setSource(query.build()) + .setTrackTotalHits(true); + + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + ML_ORIGIN, + request.request(), + ActionListener.wrap(response -> { + List events = new ArrayList<>(); + SearchHit[] hits = response.getHits().getHits(); + try { + for (SearchHit hit : hits) { + ScheduledEvent.Builder event = MlParserUtils.parse(hit, ScheduledEvent.LENIENT_PARSER); + + event.eventId(hit.getId()); + events.add(event.build()); + } + handler.onResponse(new QueryPage<>(events, response.getHits().getTotalHits().value, ScheduledEvent.RESULTS_FIELD)); + } catch (Exception e) { + handler.onFailure(e); + } + }, handler::onFailure), + client::search + ); } public void setRunningForecastsToFailed(String jobId, ActionListener listener) { QueryBuilder forecastQuery = QueryBuilders.boolQuery() .filter(QueryBuilders.termQuery(Result.RESULT_TYPE.getPreferredName(), ForecastRequestStats.RESULT_TYPE_VALUE)) .filter(QueryBuilders.termQuery(Job.ID.getPreferredName(), jobId)) - .filter(QueryBuilders.termsQuery(ForecastRequestStats.STATUS.getPreferredName(), - ForecastRequestStats.ForecastRequestStatus.SCHEDULED.toString(), - ForecastRequestStats.ForecastRequestStatus.STARTED.toString())); + .filter( + QueryBuilders.termsQuery( + ForecastRequestStats.STATUS.getPreferredName(), + ForecastRequestStats.ForecastRequestStatus.SCHEDULED.toString(), + ForecastRequestStats.ForecastRequestStatus.STARTED.toString() + ) + ); - UpdateByQueryRequest request = new UpdateByQueryRequest(AnomalyDetectorsIndex.resultsWriteAlias(jobId)) - .setQuery(forecastQuery) + UpdateByQueryRequest request = new UpdateByQueryRequest(AnomalyDetectorsIndex.resultsWriteAlias(jobId)).setQuery(forecastQuery) .setIndicesOptions(IndicesOptions.lenientExpandOpen()) .setAbortOnVersionConflict(false) .setMaxRetries(3) .setRefresh(true) - .setScript(new Script("ctx._source.forecast_status='failed';" + - "ctx._source.forecast_messages=['" + JOB_FORECAST_NATIVE_PROCESS_KILLED + "']")); + .setScript( + new Script( + "ctx._source.forecast_status='failed';" + "ctx._source.forecast_messages=['" + JOB_FORECAST_NATIVE_PROCESS_KILLED + "']" + ) + ); - executeAsyncWithOrigin(client, ML_ORIGIN, UpdateByQueryAction.INSTANCE, request, ActionListener.wrap( - response -> { - if (response.getUpdated() > 0) { - LOGGER.warn("[{}] set [{}] forecasts to failed", jobId, response.getUpdated()); - } - if (response.getBulkFailures().size() > 0) { - LOGGER.warn( - "[{}] failed to set [{}] forecasts to failed. Bulk failures experienced {}", - jobId, - response.getTotal() - response.getUpdated(), - response.getBulkFailures().stream().map(BulkItemResponse.Failure::getMessage).collect(Collectors.toList()) - ); - } - listener.onResponse(true); - }, - listener::onFailure - )); + executeAsyncWithOrigin(client, ML_ORIGIN, UpdateByQueryAction.INSTANCE, request, ActionListener.wrap(response -> { + if (response.getUpdated() > 0) { + LOGGER.warn("[{}] set [{}] forecasts to failed", jobId, response.getUpdated()); + } + if (response.getBulkFailures().size() > 0) { + LOGGER.warn( + "[{}] failed to set [{}] forecasts to failed. Bulk failures experienced {}", + jobId, + response.getTotal() - response.getUpdated(), + response.getBulkFailures().stream().map(BulkItemResponse.Failure::getMessage).collect(Collectors.toList()) + ); + } + listener.onResponse(true); + }, listener::onFailure)); } - public void getForecastRequestStats(String jobId, String forecastId, Consumer handler, - Consumer errorHandler) { + public void getForecastRequestStats( + String jobId, + String forecastId, + Consumer handler, + Consumer errorHandler + ) { String indexName = AnomalyDetectorsIndex.jobResultsAliasedName(jobId); SearchRequestBuilder forecastSearch = client.prepareSearch(indexName) .setQuery(QueryBuilders.idsQuery().addIds(ForecastRequestStats.documentId(jobId, forecastId))); - searchSingleResult(jobId, + searchSingleResult( + jobId, ForecastRequestStats.RESULTS_FIELD.getPreferredName(), forecastSearch, - ForecastRequestStats.LENIENT_PARSER,result -> handler.accept(result.result), - errorHandler, () -> null); + ForecastRequestStats.LENIENT_PARSER, + result -> handler.accept(result.result), + errorHandler, + () -> null + ); } public void getForecastStats(String jobId, Consumer handler, Consumer errorHandler) { @@ -1436,147 +1686,162 @@ public void getForecastStats(String jobId, Consumer handler, Cons SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder.query(finalQuery); sourceBuilder.aggregation( - AggregationBuilders.stats(ForecastStats.Fields.MEMORY).field(ForecastRequestStats.MEMORY_USAGE.getPreferredName())); - sourceBuilder.aggregation(AggregationBuilders.stats(ForecastStats.Fields.RECORDS) - .field(ForecastRequestStats.PROCESSED_RECORD_COUNT.getPreferredName())); + AggregationBuilders.stats(ForecastStats.Fields.MEMORY).field(ForecastRequestStats.MEMORY_USAGE.getPreferredName()) + ); + sourceBuilder.aggregation( + AggregationBuilders.stats(ForecastStats.Fields.RECORDS).field(ForecastRequestStats.PROCESSED_RECORD_COUNT.getPreferredName()) + ); sourceBuilder.aggregation( - AggregationBuilders.stats(ForecastStats.Fields.RUNTIME).field(ForecastRequestStats.PROCESSING_TIME_MS.getPreferredName())); + AggregationBuilders.stats(ForecastStats.Fields.RUNTIME).field(ForecastRequestStats.PROCESSING_TIME_MS.getPreferredName()) + ); sourceBuilder.aggregation( - AggregationBuilders.terms(ForecastStats.Fields.STATUSES).field(ForecastRequestStats.STATUS.getPreferredName())); + AggregationBuilders.terms(ForecastStats.Fields.STATUSES).field(ForecastRequestStats.STATUS.getPreferredName()) + ); sourceBuilder.size(0); sourceBuilder.trackTotalHits(true); searchRequest.source(sourceBuilder); - executeAsyncWithOrigin(client.threadPool().getThreadContext(), ML_ORIGIN, searchRequest, - ActionListener.wrap(searchResponse -> { - long totalHits = searchResponse.getHits().getTotalHits().value; - Aggregations aggregations = searchResponse.getAggregations(); - if (totalHits == 0 || aggregations == null) { - handler.accept(new ForecastStats()); - return; - } - Map aggregationsAsMap = aggregations.asMap(); - StatsAccumulator memoryStats = StatsAccumulator - .fromStatsAggregation((Stats) aggregationsAsMap.get(ForecastStats.Fields.MEMORY)); - StatsAccumulator recordStats = StatsAccumulator - .fromStatsAggregation((Stats) aggregationsAsMap.get(ForecastStats.Fields.RECORDS)); - StatsAccumulator runtimeStats = StatsAccumulator - .fromStatsAggregation((Stats) aggregationsAsMap.get(ForecastStats.Fields.RUNTIME)); - CountAccumulator statusCount = CountAccumulator - .fromTermsAggregation((StringTerms) aggregationsAsMap.get(ForecastStats.Fields.STATUSES)); - - ForecastStats forecastStats = new ForecastStats(totalHits, memoryStats, recordStats, runtimeStats, statusCount); - handler.accept(forecastStats); - }, errorHandler), client::search); + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + ML_ORIGIN, + searchRequest, + ActionListener.wrap(searchResponse -> { + long totalHits = searchResponse.getHits().getTotalHits().value; + Aggregations aggregations = searchResponse.getAggregations(); + if (totalHits == 0 || aggregations == null) { + handler.accept(new ForecastStats()); + return; + } + Map aggregationsAsMap = aggregations.asMap(); + StatsAccumulator memoryStats = StatsAccumulator.fromStatsAggregation( + (Stats) aggregationsAsMap.get(ForecastStats.Fields.MEMORY) + ); + StatsAccumulator recordStats = StatsAccumulator.fromStatsAggregation( + (Stats) aggregationsAsMap.get(ForecastStats.Fields.RECORDS) + ); + StatsAccumulator runtimeStats = StatsAccumulator.fromStatsAggregation( + (Stats) aggregationsAsMap.get(ForecastStats.Fields.RUNTIME) + ); + CountAccumulator statusCount = CountAccumulator.fromTermsAggregation( + (StringTerms) aggregationsAsMap.get(ForecastStats.Fields.STATUSES) + ); + + ForecastStats forecastStats = new ForecastStats(totalHits, memoryStats, recordStats, runtimeStats, statusCount); + handler.accept(forecastStats); + }, errorHandler), + client::search + ); } - public void updateCalendar(String calendarId, Set jobIdsToAdd, Set jobIdsToRemove, - Consumer handler, Consumer errorHandler) { - - ActionListener getCalendarListener = ActionListener.wrap( - calendar -> { - Set currentJobs = new HashSet<>(calendar.getJobIds()); - - for (String jobToRemove : jobIdsToRemove) { - if (currentJobs.contains(jobToRemove) == false) { - errorHandler.accept(ExceptionsHelper.badRequestException("Cannot remove [" + jobToRemove - + "] as it is not present in calendar [" + calendarId + "]")); - return; - } - } + public void updateCalendar( + String calendarId, + Set jobIdsToAdd, + Set jobIdsToRemove, + Consumer handler, + Consumer errorHandler + ) { + + ActionListener getCalendarListener = ActionListener.wrap(calendar -> { + Set currentJobs = new HashSet<>(calendar.getJobIds()); + + for (String jobToRemove : jobIdsToRemove) { + if (currentJobs.contains(jobToRemove) == false) { + errorHandler.accept( + ExceptionsHelper.badRequestException( + "Cannot remove [" + jobToRemove + "] as it is not present in calendar [" + calendarId + "]" + ) + ); + return; + } + } - currentJobs.addAll(jobIdsToAdd); - currentJobs.removeAll(jobIdsToRemove); - Calendar updatedCalendar = new Calendar(calendar.getId(), new ArrayList<>(currentJobs), calendar.getDescription()); + currentJobs.addAll(jobIdsToAdd); + currentJobs.removeAll(jobIdsToRemove); + Calendar updatedCalendar = new Calendar(calendar.getId(), new ArrayList<>(currentJobs), calendar.getDescription()); - UpdateRequest updateRequest = new UpdateRequest(MlMetaIndex.indexName(), updatedCalendar.documentId()); - updateRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); + UpdateRequest updateRequest = new UpdateRequest(MlMetaIndex.indexName(), updatedCalendar.documentId()); + updateRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - try (XContentBuilder builder = XContentFactory.jsonBuilder()) { - updateRequest.doc(updatedCalendar.toXContent(builder, ToXContent.EMPTY_PARAMS)); - } catch (IOException e) { - throw new IllegalStateException("Failed to serialise calendar with id [" + updatedCalendar.getId() + "]", e); - } + try (XContentBuilder builder = XContentFactory.jsonBuilder()) { + updateRequest.doc(updatedCalendar.toXContent(builder, ToXContent.EMPTY_PARAMS)); + } catch (IOException e) { + throw new IllegalStateException("Failed to serialise calendar with id [" + updatedCalendar.getId() + "]", e); + } - executeAsyncWithOrigin(client.threadPool().getThreadContext(), ML_ORIGIN, updateRequest, - ActionListener.wrap( - response -> handler.accept(updatedCalendar), - errorHandler) - , client::update); + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + ML_ORIGIN, + updateRequest, + ActionListener.wrap(response -> handler.accept(updatedCalendar), errorHandler), + client::update + ); - }, - errorHandler - ); + }, errorHandler); calendar(calendarId, getCalendarListener); } public void calendars(CalendarQueryBuilder queryBuilder, ActionListener> listener) { SearchRequest searchRequest = client.prepareSearch(MlMetaIndex.indexName()) - .setIndicesOptions(IndicesOptions.lenientExpandOpen()) - .setTrackTotalHits(true) - .setSource(queryBuilder.build()).request(); - - executeAsyncWithOrigin(client.threadPool().getThreadContext(), ML_ORIGIN, searchRequest, - ActionListener.wrap( - response -> { - List calendars = new ArrayList<>(); - SearchHit[] hits = response.getHits().getHits(); - try { - if (queryBuilder.isForAllCalendars() == false && hits.length == 0) { - listener.onFailure(queryBuilder.buildNotFoundException()); - return; - } - for (SearchHit hit : hits) { - calendars.add(MlParserUtils.parse(hit, Calendar.LENIENT_PARSER).build()); - } - listener.onResponse(new QueryPage<>(calendars, response.getHits().getTotalHits().value, - Calendar.RESULTS_FIELD)); - } catch (Exception e) { - listener.onFailure(e); - } - }, - listener::onFailure) - , client::search); + .setIndicesOptions(IndicesOptions.lenientExpandOpen()) + .setTrackTotalHits(true) + .setSource(queryBuilder.build()) + .request(); + + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + ML_ORIGIN, + searchRequest, + ActionListener.wrap(response -> { + List calendars = new ArrayList<>(); + SearchHit[] hits = response.getHits().getHits(); + try { + if (queryBuilder.isForAllCalendars() == false && hits.length == 0) { + listener.onFailure(queryBuilder.buildNotFoundException()); + return; + } + for (SearchHit hit : hits) { + calendars.add(MlParserUtils.parse(hit, Calendar.LENIENT_PARSER).build()); + } + listener.onResponse(new QueryPage<>(calendars, response.getHits().getTotalHits().value, Calendar.RESULTS_FIELD)); + } catch (Exception e) { + listener.onFailure(e); + } + }, listener::onFailure), + client::search + ); } public void removeJobFromCalendars(String jobId, ActionListener listener) { ActionListener updateCalendarsListener = ActionListener.wrap( - r -> listener.onResponse(r.hasFailures() == false), - listener::onFailure + r -> listener.onResponse(r.hasFailures() == false), + listener::onFailure ); - ActionListener> getCalendarsListener = ActionListener.wrap( - r -> { - BulkRequestBuilder bulkUpdate = client.prepareBulk(); - bulkUpdate.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - for (Calendar calendar : r.results()) { - List ids = calendar.getJobIds() - .stream() - .filter(jId -> jobId.equals(jId) == false) - .collect(Collectors.toList()); - Calendar newCalendar = new Calendar(calendar.getId(), ids, calendar.getDescription()); - UpdateRequest updateRequest = new UpdateRequest(MlMetaIndex.indexName(), newCalendar.documentId()); - try (XContentBuilder builder = XContentFactory.jsonBuilder()) { - updateRequest.doc(newCalendar.toXContent(builder, ToXContent.EMPTY_PARAMS)); - } catch (IOException e) { - listener.onFailure( - new IllegalStateException("Failed to serialise calendar with id [" + newCalendar.getId() + "]", e)); - return; - } - bulkUpdate.add(updateRequest); - } - if (bulkUpdate.numberOfActions() > 0) { - executeAsyncWithOrigin(client, ML_ORIGIN, BulkAction.INSTANCE, bulkUpdate.request(), updateCalendarsListener); - } else { - listener.onResponse(true); - } - }, - listener::onFailure - ); + ActionListener> getCalendarsListener = ActionListener.wrap(r -> { + BulkRequestBuilder bulkUpdate = client.prepareBulk(); + bulkUpdate.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); + for (Calendar calendar : r.results()) { + List ids = calendar.getJobIds().stream().filter(jId -> jobId.equals(jId) == false).collect(Collectors.toList()); + Calendar newCalendar = new Calendar(calendar.getId(), ids, calendar.getDescription()); + UpdateRequest updateRequest = new UpdateRequest(MlMetaIndex.indexName(), newCalendar.documentId()); + try (XContentBuilder builder = XContentFactory.jsonBuilder()) { + updateRequest.doc(newCalendar.toXContent(builder, ToXContent.EMPTY_PARAMS)); + } catch (IOException e) { + listener.onFailure(new IllegalStateException("Failed to serialise calendar with id [" + newCalendar.getId() + "]", e)); + return; + } + bulkUpdate.add(updateRequest); + } + if (bulkUpdate.numberOfActions() > 0) { + executeAsyncWithOrigin(client, ML_ORIGIN, BulkAction.INSTANCE, bulkUpdate.request(), updateCalendarsListener); + } else { + listener.onResponse(true); + } + }, listener::onFailure); CalendarQueryBuilder query = new CalendarQueryBuilder().jobId(jobId); calendars(query, getCalendarsListener); @@ -1591,9 +1856,11 @@ public void onResponse(GetResponse getDocResponse) { if (getDocResponse.isExists()) { BytesReference docSource = getDocResponse.getSourceAsBytesRef(); - try (InputStream stream = docSource.streamInput(); - XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) { + try ( + InputStream stream = docSource.streamInput(); + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream) + ) { Calendar calendar = Calendar.LENIENT_PARSER.apply(parser, null).build(); listener.onResponse(calendar); } @@ -1613,8 +1880,7 @@ public void onFailure(Exception e) { listener.onFailure(e); } } - }, - client::get); + }, client::get); } /** @@ -1629,36 +1895,30 @@ public void getRestartTimeInfo(String jobId, ActionListener lis new RestartTimeInfo( latestFinalBucketHolder.get() == null ? null : latestFinalBucketHolder.get().getTimestamp().getTime(), dataCounts.getLatestRecordTimeStamp() == null ? null : dataCounts.getLatestRecordTimeStamp().getTime(), - dataCounts.getInputRecordCount() > 0) - ); - - ActionListener latestFinalBucketListener = ActionListener.wrap( - latestFinalBucket -> { - latestFinalBucketHolder.set(latestFinalBucket); - dataCounts(jobId, dataCountsHandler, listener::onFailure); - }, - listener::onFailure + dataCounts.getInputRecordCount() > 0 + ) ); + ActionListener latestFinalBucketListener = ActionListener.wrap(latestFinalBucket -> { + latestFinalBucketHolder.set(latestFinalBucket); + dataCounts(jobId, dataCountsHandler, listener::onFailure); + }, listener::onFailure); + getLatestFinalBucket(jobId, latestFinalBucketListener); } private void getLatestFinalBucket(String jobId, ActionListener listener) { - BucketsQueryBuilder latestBucketQuery = new BucketsQueryBuilder() - .sortField(Result.TIMESTAMP.getPreferredName()) + BucketsQueryBuilder latestBucketQuery = new BucketsQueryBuilder().sortField(Result.TIMESTAMP.getPreferredName()) .sortDescending(true) .size(1) .includeInterim(false); - bucketsViaInternalClient(jobId, latestBucketQuery, - queryPage -> { - if (queryPage.results().isEmpty()) { - listener.onResponse(null); - } else { - listener.onResponse(queryPage.results().get(0)); - } - }, - listener::onFailure - ); + bucketsViaInternalClient(jobId, latestBucketQuery, queryPage -> { + if (queryPage.results().isEmpty()) { + listener.onResponse(null); + } else { + listener.onResponse(queryPage.results().get(0)); + } + }, listener::onFailure); } /** @@ -1681,9 +1941,11 @@ private void getLatestFinalBucket(String jobId, ActionListener listener) */ static Exception mapAuthFailure(Exception e, String jobId, String mappedActionName) { if (e instanceof ElasticsearchStatusException) { - if (((ElasticsearchStatusException)e).status() == RestStatus.FORBIDDEN) { + if (((ElasticsearchStatusException) e).status() == RestStatus.FORBIDDEN) { e = Exceptions.authorizationError( - e.getMessage().replaceFirst("action \\[.*?\\]", "action [" + mappedActionName + "]") + " for job [{}]", jobId); + e.getMessage().replaceFirst("action \\[.*?\\]", "action [" + mappedActionName + "]") + " for job [{}]", + jobId + ); } } return e; diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/RecordsQueryBuilder.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/RecordsQueryBuilder.java index f3ecd75677e96..865660edb8242 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/RecordsQueryBuilder.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/RecordsQueryBuilder.java @@ -49,12 +49,12 @@ public final class RecordsQueryBuilder { public static final int DEFAULT_SIZE = 100; private static final List SECONDARY_SORT = Arrays.asList( - AnomalyRecord.RECORD_SCORE.getPreferredName(), - AnomalyRecord.OVER_FIELD_VALUE.getPreferredName(), - AnomalyRecord.PARTITION_FIELD_VALUE.getPreferredName(), - AnomalyRecord.BY_FIELD_VALUE.getPreferredName(), - AnomalyRecord.FIELD_NAME.getPreferredName(), - AnomalyRecord.FUNCTION.getPreferredName() + AnomalyRecord.RECORD_SCORE.getPreferredName(), + AnomalyRecord.OVER_FIELD_VALUE.getPreferredName(), + AnomalyRecord.PARTITION_FIELD_VALUE.getPreferredName(), + AnomalyRecord.BY_FIELD_VALUE.getPreferredName(), + AnomalyRecord.FIELD_NAME.getPreferredName(), + AnomalyRecord.FUNCTION.getPreferredName() ); private int from = 0; @@ -113,34 +113,29 @@ public RecordsQueryBuilder timestamp(Date timestamp) { } public SearchSourceBuilder build() { - QueryBuilder query = new ResultsFilterBuilder() - .timeRange(Result.TIMESTAMP.getPreferredName(), start, end) - .score(AnomalyRecord.RECORD_SCORE.getPreferredName(), recordScore) - .interim(includeInterim) - .build(); + QueryBuilder query = new ResultsFilterBuilder().timeRange(Result.TIMESTAMP.getPreferredName(), start, end) + .score(AnomalyRecord.RECORD_SCORE.getPreferredName(), recordScore) + .interim(includeInterim) + .build(); FieldSortBuilder sb; if (sortField != null) { - sb = new FieldSortBuilder(sortField) - .missing("_last") - .order(sortDescending ? SortOrder.DESC : SortOrder.ASC); + sb = new FieldSortBuilder(sortField).missing("_last").order(sortDescending ? SortOrder.DESC : SortOrder.ASC); } else { sb = SortBuilders.fieldSort(ElasticsearchMappings.ES_DOC); } - BoolQueryBuilder recordFilter = new BoolQueryBuilder() - .filter(query) - .filter(new TermsQueryBuilder(Result.RESULT_TYPE.getPreferredName(), AnomalyRecord.RESULT_TYPE_VALUE)); + BoolQueryBuilder recordFilter = new BoolQueryBuilder().filter(query) + .filter(new TermsQueryBuilder(Result.RESULT_TYPE.getPreferredName(), AnomalyRecord.RESULT_TYPE_VALUE)); if (timestamp != null) { recordFilter.filter(QueryBuilders.termQuery(Result.TIMESTAMP.getPreferredName(), timestamp.getTime())); } - SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder() - .from(from) - .size(size) - .query(recordFilter) - .sort(sb) - .fetchSource(true); + SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().from(from) + .size(size) + .query(recordFilter) + .sort(sb) + .fetchSource(true); for (String sortField : SECONDARY_SORT) { searchSourceBuilder.sort(sortField, sortDescending ? SortOrder.DESC : SortOrder.ASC); @@ -149,5 +144,3 @@ public SearchSourceBuilder build() { return searchSourceBuilder; } } - - diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/ResultsFilterBuilder.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/ResultsFilterBuilder.java index 493dba96f4c68..0d8750fe8ddd0 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/ResultsFilterBuilder.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/ResultsFilterBuilder.java @@ -71,7 +71,7 @@ public ResultsFilterBuilder interim(boolean includeInterim) { } // Implemented as "NOT isInterim == true" so that not present and null - // are equivalent to false. This improves backwards compatibility. + // are equivalent to false. This improves backwards compatibility. // Also, note how for a boolean field, unlike numeric term queries, the // term value is supplied as a string. TermQueryBuilder interimFilter = QueryBuilders.termQuery(Result.IS_INTERIM.getPreferredName(), true); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/ScheduledEventsQueryBuilder.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/ScheduledEventsQueryBuilder.java index f8a5f98c68ab6..08e76f49200eb 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/ScheduledEventsQueryBuilder.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/ScheduledEventsQueryBuilder.java @@ -14,7 +14,6 @@ import org.elasticsearch.xpack.core.ml.calendars.ScheduledEvent; import org.elasticsearch.xpack.ml.utils.QueryBuilderHelper; - /** * Query builder for {@link ScheduledEvent}s * If calendarIds are not set then all calendars will match. diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/SearchAfterJobsIterator.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/SearchAfterJobsIterator.java index 2eb7b27b220ee..d8fbf18d8a477 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/SearchAfterJobsIterator.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/SearchAfterJobsIterator.java @@ -10,14 +10,14 @@ import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.client.OriginSettingClient; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.TermQueryBuilder; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.sort.FieldSortBuilder; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.ml.MlConfigIndex; import org.elasticsearch.xpack.core.ml.job.config.Job; import org.elasticsearch.xpack.ml.utils.persistence.SearchAfterDocumentsIterator; @@ -48,7 +48,7 @@ protected Object[] searchAfterFields() { if (lastJobId == null) { return null; } else { - return new Object[] {lastJobId}; + return new Object[] { lastJobId }; } } @@ -59,9 +59,11 @@ protected void extractSearchAfterFields(SearchHit lastSearchHit) { @Override protected Job.Builder map(SearchHit hit) { - try (InputStream stream = hit.getSourceRef().streamInput(); - XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) { + try ( + InputStream stream = hit.getSourceRef().streamInput(); + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream) + ) { return Job.LENIENT_PARSER.apply(parser, null); } catch (IOException e) { throw new ElasticsearchParseException("failed to parse job document [" + hit.getId() + "]", e); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/StateStreamer.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/StateStreamer.java index 55596b34be80e..51fa5aa07c7b2 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/StateStreamer.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/StateStreamer.java @@ -74,10 +74,16 @@ public void restoreStateToStream(String jobId, ModelSnapshot modelSnapshot, Outp try (ThreadContext.StoredContext ignore = client.threadPool().getThreadContext().stashWithOrigin(ML_ORIGIN)) { SearchResponse stateResponse = client.prepareSearch(indexName) .setSize(1) - .setQuery(QueryBuilders.idsQuery().addIds(stateDocId)).get(); + .setQuery(QueryBuilders.idsQuery().addIds(stateDocId)) + .get(); if (stateResponse.getHits().getHits().length == 0) { - LOGGER.error("Expected {} documents for model state for {} snapshot {} but failed to find {}", - modelSnapshot.getSnapshotDocCount(), jobId, modelSnapshot.getSnapshotId(), stateDocId); + LOGGER.error( + "Expected {} documents for model state for {} snapshot {} but failed to find {}", + modelSnapshot.getSnapshotDocCount(), + jobId, + modelSnapshot.getSnapshotId(), + stateDocId + ); break; } writeStateToStream(stateResponse.getHits().getAt(0).getSourceRef(), restoreStream); @@ -85,8 +91,8 @@ public void restoreStateToStream(String jobId, ModelSnapshot modelSnapshot, Outp } // Secondly try to restore categorizer state. This must come after model state because that's - // the order the C++ process expects. There are no snapshots for this, so the IDs simply - // count up until a document is not found. It's NOT an error to have no categorizer state. + // the order the C++ process expects. There are no snapshots for this, so the IDs simply + // count up until a document is not found. It's NOT an error to have no categorizer state. int docNum = 0; while (true) { if (isCancelled) { @@ -100,7 +106,8 @@ public void restoreStateToStream(String jobId, ModelSnapshot modelSnapshot, Outp try (ThreadContext.StoredContext ignore = client.threadPool().getThreadContext().stashWithOrigin(ML_ORIGIN)) { SearchResponse stateResponse = client.prepareSearch(indexName) .setSize(1) - .setQuery(QueryBuilders.idsQuery().addIds(docId)).get(); + .setQuery(QueryBuilders.idsQuery().addIds(docId)) + .get(); if (stateResponse.getHits().getHits().length == 0) { break; } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/overallbuckets/OverallBucketsAggregator.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/overallbuckets/OverallBucketsAggregator.java index febb6afe5d373..f0690f8aab9c8 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/overallbuckets/OverallBucketsAggregator.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/overallbuckets/OverallBucketsAggregator.java @@ -52,8 +52,7 @@ public synchronized void process(List buckets) { private OverallBucket outputBucket() { List jobs = new ArrayList<>(maxScoreByJob.size()); - maxScoreByJob.entrySet().stream().forEach(entry -> jobs.add( - new OverallBucket.JobInfo(entry.getKey(), entry.getValue()))); + maxScoreByJob.entrySet().stream().forEach(entry -> jobs.add(new OverallBucket.JobInfo(entry.getKey(), entry.getValue()))); return new OverallBucket(new Date(startTime), bucketSpanSeconds, maxOverallScore, jobs, isInterim); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/overallbuckets/OverallBucketsProcessor.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/overallbuckets/OverallBucketsProcessor.java index 65452d61927a8..dcecbc6f386e8 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/overallbuckets/OverallBucketsProcessor.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/overallbuckets/OverallBucketsProcessor.java @@ -13,6 +13,8 @@ public interface OverallBucketsProcessor { void process(List overallBuckets); + List finish(); + int size(); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/overallbuckets/OverallBucketsProvider.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/overallbuckets/OverallBucketsProvider.java index ef06d16092e00..886ebe6afa0ef 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/overallbuckets/OverallBucketsProvider.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/overallbuckets/OverallBucketsProvider.java @@ -58,8 +58,15 @@ public List computeOverallBuckets(Histogram histogram) { Max interimAgg = histogramBucketAggs.get(Result.IS_INTERIM.getPreferredName()); boolean isInterim = interimAgg.getValue() > 0; - overallBuckets.add(new OverallBucket(getHistogramBucketTimestamp(histogramBucket), - maxJobBucketSpanSeconds, overallScore, new ArrayList<>(jobs), isInterim)); + overallBuckets.add( + new OverallBucket( + getHistogramBucketTimestamp(histogramBucket), + maxJobBucketSpanSeconds, + overallScore, + new ArrayList<>(jobs), + isInterim + ) + ); } return overallBuckets; } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/DataCountsReporter.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/DataCountsReporter.java index 4c1df15799b49..306425c50d3ca 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/DataCountsReporter.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/DataCountsReporter.java @@ -17,7 +17,6 @@ import java.util.Locale; import java.util.function.Predicate; - /** * Status reporter for tracking counts of the good/bad records written to the API. * Call one of the reportXXX() methods to update the records counts. @@ -228,7 +227,6 @@ public long getAnalysedFieldsPerRecord() { return analyzedFieldsPerRecord; } - /** * Report the counts now regardless of whether or not we are at a reporting boundary. */ @@ -251,9 +249,15 @@ protected boolean logStatus(long totalRecords) { return false; } - String status = String.format(Locale.ROOT, - "[%s] %d records written to autodetect; missingFieldCount=%d, invalidDateCount=%d, outOfOrderCount=%d", job.getId(), - getProcessedRecordCount(), getMissingFieldErrorCount(), getDateParseErrorsCount(), getOutOfOrderRecordCount()); + String status = String.format( + Locale.ROOT, + "[%s] %d records written to autodetect; missingFieldCount=%d, invalidDateCount=%d, outOfOrderCount=%d", + job.getId(), + getProcessedRecordCount(), + getMissingFieldErrorCount(), + getDateParseErrorsCount(), + getOutOfOrderRecordCount() + ); logger.info(status); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/ProcessWorkerExecutorService.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/ProcessWorkerExecutorService.java index ffa35a1549aaa..6119ee9ad9449 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/ProcessWorkerExecutorService.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/ProcessWorkerExecutorService.java @@ -120,7 +120,7 @@ public void start() { String msg = "unable to process as " + processName + " worker service has shutdown"; for (Runnable runnable : notExecuted) { if (runnable instanceof AbstractRunnable) { - ((AbstractRunnable) runnable).onRejection( new EsRejectedExecutionException(msg, true)); + ((AbstractRunnable) runnable).onRejection(new EsRejectedExecutionException(msg, true)); } } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/AutodetectBuilder.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/AutodetectBuilder.java index 3f5c8d77cdf88..b21ac6f47410e 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/AutodetectBuilder.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/AutodetectBuilder.java @@ -10,18 +10,18 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.env.Environment; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.env.Environment; import org.elasticsearch.xpack.core.XPackPlugin; import org.elasticsearch.xpack.core.ml.calendars.ScheduledEvent; import org.elasticsearch.xpack.core.ml.job.config.Job; import org.elasticsearch.xpack.core.ml.job.config.MlFilter; import org.elasticsearch.xpack.core.ml.job.process.autodetect.state.Quantiles; +import org.elasticsearch.xpack.ml.job.process.ProcessBuilderUtils; import org.elasticsearch.xpack.ml.job.process.autodetect.writer.ScheduledEventToRuleWriter; import org.elasticsearch.xpack.ml.process.NativeController; -import org.elasticsearch.xpack.ml.job.process.ProcessBuilderUtils; import org.elasticsearch.xpack.ml.process.ProcessPipes; import java.io.BufferedWriter; @@ -72,8 +72,12 @@ public class AutodetectBuilder { * The maximum number of anomaly records that will be written each bucket */ // Though this setting is dynamic, it is only set when a new job is opened. So, already running jobs will not get the updated value. - public static final Setting MAX_ANOMALY_RECORDS_SETTING_DYNAMIC = Setting.intSetting("xpack.ml.max_anomaly_records", - DEFAULT_MAX_NUM_RECORDS, Setting.Property.NodeScope, Setting.Property.Dynamic); + public static final Setting MAX_ANOMALY_RECORDS_SETTING_DYNAMIC = Setting.intSetting( + "xpack.ml.max_anomaly_records", + DEFAULT_MAX_NUM_RECORDS, + Setting.Property.NodeScope, + Setting.Property.Dynamic + ); /** * Persisted quantiles are written to disk so they can be read by @@ -100,8 +104,15 @@ public class AutodetectBuilder { * deleted when the process completes * @param logger The job's logger */ - public AutodetectBuilder(Job job, List filesToDelete, Logger logger, Environment env, Settings settings, - NativeController controller, ProcessPipes processPipes) { + public AutodetectBuilder( + Job job, + List filesToDelete, + Logger logger, + Environment env, + Settings settings, + NativeController controller, + ProcessPipes processPipes + ) { this.env = env; this.settings = settings; this.controller = controller; @@ -193,13 +204,15 @@ private void buildQuantiles(List command) throws IOException { /** * Write the normalizer init state to file. */ - public static Path writeNormalizerInitState(String jobId, String state, Environment env) - throws IOException { + public static Path writeNormalizerInitState(String jobId, String state, Environment env) throws IOException { // createTempFile has a race condition where it may return the same // temporary file name to different threads if called simultaneously // from multiple threads, hence add the thread ID to avoid this - Path stateFile = Files.createTempFile(env.tmpFile(), jobId + "_quantiles_" + Thread.currentThread().getId(), - QUANTILES_FILE_EXTENSION); + Path stateFile = Files.createTempFile( + env.tmpFile(), + jobId + "_quantiles_" + Thread.currentThread().getId(), + QUANTILES_FILE_EXTENSION + ); try (BufferedWriter osw = Files.newBufferedWriter(stateFile, StandardCharsets.UTF_8)) { osw.write(state); @@ -219,12 +232,17 @@ private void buildScheduledEventsConfig(List command) throws IOException .map(x -> new ScheduledEventToRuleWriter(x.getDescription(), x.toDetectionRule(job.getAnalysisConfig().getBucketSpan()))) .collect(Collectors.toList()); - try (OutputStreamWriter osw = new OutputStreamWriter(Files.newOutputStream(eventsConfigFile),StandardCharsets.UTF_8); - XContentBuilder jsonBuilder = JsonXContent.contentBuilder()) { - osw.write(Strings.toString( - jsonBuilder.startObject() - .field(ScheduledEvent.RESULTS_FIELD.getPreferredName(), scheduledEventToRuleWriters) - .endObject())); + try ( + OutputStreamWriter osw = new OutputStreamWriter(Files.newOutputStream(eventsConfigFile), StandardCharsets.UTF_8); + XContentBuilder jsonBuilder = JsonXContent.contentBuilder() + ) { + osw.write( + Strings.toString( + jsonBuilder.startObject() + .field(ScheduledEvent.RESULTS_FIELD.getPreferredName(), scheduledEventToRuleWriters) + .endObject() + ) + ); } command.add(EVENTS_CONFIG_ARG + eventsConfigFile.toString()); @@ -233,8 +251,10 @@ private void buildScheduledEventsConfig(List command) throws IOException private void buildJobConfig(List command) throws IOException { Path configFile = Files.createTempFile(env.tmpFile(), "config", JSON_EXTENSION); filesToDelete.add(configFile); - try (OutputStreamWriter osw = new OutputStreamWriter(Files.newOutputStream(configFile),StandardCharsets.UTF_8); - XContentBuilder jsonBuilder = JsonXContent.contentBuilder()) { + try ( + OutputStreamWriter osw = new OutputStreamWriter(Files.newOutputStream(configFile), StandardCharsets.UTF_8); + XContentBuilder jsonBuilder = JsonXContent.contentBuilder() + ) { job.toXContent(jsonBuilder, ToXContent.EMPTY_PARAMS); osw.write(Strings.toString(jsonBuilder)); @@ -250,12 +270,13 @@ private void buildFiltersConfig(List command) throws IOException { Path filtersConfigFile = Files.createTempFile(env.tmpFile(), "filtersConfig", JSON_EXTENSION); filesToDelete.add(filtersConfigFile); - try (OutputStreamWriter osw = new OutputStreamWriter(Files.newOutputStream(filtersConfigFile),StandardCharsets.UTF_8); - XContentBuilder jsonBuilder = JsonXContent.contentBuilder()) { - osw.write(Strings.toString( - jsonBuilder.startObject() - .field(MlFilter.RESULTS_FIELD.getPreferredName(), referencedFilters) - .endObject())); + try ( + OutputStreamWriter osw = new OutputStreamWriter(Files.newOutputStream(filtersConfigFile), StandardCharsets.UTF_8); + XContentBuilder jsonBuilder = JsonXContent.contentBuilder() + ) { + osw.write( + Strings.toString(jsonBuilder.startObject().field(MlFilter.RESULTS_FIELD.getPreferredName(), referencedFilters).endObject()) + ); } command.add(FILTERS_CONFIG_ARG + filtersConfigFile.toString()); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/AutodetectCommunicator.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/AutodetectCommunicator.java index 601af15523a11..140ddb46951ac 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/AutodetectCommunicator.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/AutodetectCommunicator.java @@ -11,12 +11,12 @@ import org.apache.logging.log4j.message.ParameterizedMessage; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.CheckedSupplier; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.util.concurrent.AbstractRunnable; import org.elasticsearch.common.util.concurrent.FutureUtils; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.index.analysis.AnalysisRegistry; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.index.analysis.AnalysisRegistry; import org.elasticsearch.xpack.core.ml.job.config.AnalysisConfig; import org.elasticsearch.xpack.core.ml.job.config.CategorizationAnalyzerConfig; import org.elasticsearch.xpack.core.ml.job.config.DataDescription; @@ -71,10 +71,16 @@ public class AutodetectCommunicator implements Closeable { private volatile CategorizationAnalyzer categorizationAnalyzer; private volatile boolean processKilled; - AutodetectCommunicator(Job job, AutodetectProcess process, StateStreamer stateStreamer, - DataCountsReporter dataCountsReporter, AutodetectResultProcessor autodetectResultProcessor, - BiConsumer onFinishHandler, NamedXContentRegistry xContentRegistry, - ExecutorService autodetectWorkerExecutor) { + AutodetectCommunicator( + Job job, + AutodetectProcess process, + StateStreamer stateStreamer, + DataCountsReporter dataCountsReporter, + AutodetectResultProcessor autodetectResultProcessor, + BiConsumer onFinishHandler, + NamedXContentRegistry xContentRegistry, + ExecutorService autodetectWorkerExecutor + ) { this.job = job; this.autodetectProcess = process; this.stateStreamer = stateStreamer; @@ -84,7 +90,7 @@ public class AutodetectCommunicator implements Closeable { this.xContentRegistry = xContentRegistry; this.autodetectWorkerExecutor = autodetectWorkerExecutor; this.includeTokensField = MachineLearning.CATEGORIZATION_TOKENIZATION_IN_JAVA - && job.getAnalysisConfig().getCategorizationFieldName() != null; + && job.getAnalysisConfig().getCategorizationFieldName() != null; } public void restoreState(ModelSnapshot modelSnapshot) { @@ -92,8 +98,15 @@ public void restoreState(ModelSnapshot modelSnapshot) { } private DataToProcessWriter createProcessWriter(DataDescription dataDescription) { - return new JsonDataToProcessWriter(true, includeTokensField, autodetectProcess, - dataDescription, job.getAnalysisConfig(), dataCountsReporter, xContentRegistry); + return new JsonDataToProcessWriter( + true, + includeTokensField, + autodetectProcess, + dataDescription, + job.getAnalysisConfig(), + dataCountsReporter, + xContentRegistry + ); } /** @@ -107,8 +120,13 @@ public void writeHeader() throws IOException { /** * Call {@link #writeHeader()} exactly once before using this method */ - public void writeToJob(InputStream inputStream, AnalysisRegistry analysisRegistry, XContentType xContentType, - DataLoadParams params, BiConsumer handler) { + public void writeToJob( + InputStream inputStream, + AnalysisRegistry analysisRegistry, + XContentType xContentType, + DataLoadParams params, + BiConsumer handler + ) { submitOperation(() -> { if (params.isResettingBuckets()) { autodetectProcess.writeResetBucketsControlMessage(params); @@ -138,8 +156,7 @@ public void writeToJob(InputStream inputStream, AnalysisRegistry analysisRegistr } else { return dataCountsAtomicReference.get(); } - }, - handler); + }, handler); } /** @@ -357,8 +374,13 @@ private void submitOperation(CheckedSupplier operation, BiCons @Override public void onFailure(Exception e) { if (processKilled) { - handler.accept(null, ExceptionsHelper.conflictStatusException( - "[{}] Could not submit operation to process as it has been killed", job.getId())); + handler.accept( + null, + ExceptionsHelper.conflictStatusException( + "[{}] Could not submit operation to process as it has been killed", + job.getId() + ) + ); } else { logger.error(new ParameterizedMessage("[{}] Unexpected exception writing to process", job.getId()), e); handler.accept(null, e); @@ -368,8 +390,13 @@ public void onFailure(Exception e) { @Override protected void doRun() throws Exception { if (processKilled) { - handler.accept(null, ExceptionsHelper.conflictStatusException( - "[{}] Could not submit operation to process as it has been killed", job.getId())); + handler.accept( + null, + ExceptionsHelper.conflictStatusException( + "[{}] Could not submit operation to process as it has been killed", + job.getId() + ) + ); } else { checkProcessIsAlive(); handler.accept(operation.get(), null); @@ -382,8 +409,9 @@ private void createCategorizationAnalyzer(AnalysisRegistry analysisRegistry) thr AnalysisConfig analysisConfig = job.getAnalysisConfig(); CategorizationAnalyzerConfig categorizationAnalyzerConfig = analysisConfig.getCategorizationAnalyzerConfig(); if (categorizationAnalyzerConfig == null) { - categorizationAnalyzerConfig = - CategorizationAnalyzerConfig.buildDefaultCategorizationAnalyzer(analysisConfig.getCategorizationFilters()); + categorizationAnalyzerConfig = CategorizationAnalyzerConfig.buildDefaultCategorizationAnalyzer( + analysisConfig.getCategorizationFilters() + ); } categorizationAnalyzer = new CategorizationAnalyzer(analysisRegistry, categorizationAnalyzerConfig); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/AutodetectProcessFactory.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/AutodetectProcessFactory.java index 6e72d1376f5e5..30fb2a5380877 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/AutodetectProcessFactory.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/AutodetectProcessFactory.java @@ -27,16 +27,20 @@ public interface AutodetectProcessFactory { * @param onProcessCrash Callback to execute if the process stops unexpectedly * @return The process */ - default AutodetectProcess createAutodetectProcess(Job job, - AutodetectParams autodetectParams, - ExecutorService executorService, - Consumer onProcessCrash) { + default AutodetectProcess createAutodetectProcess( + Job job, + AutodetectParams autodetectParams, + ExecutorService executorService, + Consumer onProcessCrash + ) { return createAutodetectProcess(job.getId(), job, autodetectParams, executorService, onProcessCrash); } - AutodetectProcess createAutodetectProcess(String pipelineId, - Job job, - AutodetectParams autodetectParams, - ExecutorService executorService, - Consumer onProcessCrash); + AutodetectProcess createAutodetectProcess( + String pipelineId, + Job job, + AutodetectParams autodetectParams, + ExecutorService executorService, + Consumer onProcessCrash + ); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/AutodetectProcessManager.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/AutodetectProcessManager.java index cdd8f6820d459..4dcd3954488f7 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/AutodetectProcessManager.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/AutodetectProcessManager.java @@ -131,12 +131,23 @@ public class AutodetectProcessManager implements ClusterStateListener { private volatile boolean resetInProgress; private volatile boolean nodeDying; - public AutodetectProcessManager(Settings settings, Client client, ThreadPool threadPool, - NamedXContentRegistry xContentRegistry, AnomalyDetectionAuditor auditor, ClusterService clusterService, - JobManager jobManager, JobResultsProvider jobResultsProvider, JobResultsPersister jobResultsPersister, - JobDataCountsPersister jobDataCountsPersister, AnnotationPersister annotationPersister, - AutodetectProcessFactory autodetectProcessFactory, NormalizerFactory normalizerFactory, - NativeStorageProvider nativeStorageProvider, IndexNameExpressionResolver expressionResolver) { + public AutodetectProcessManager( + Settings settings, + Client client, + ThreadPool threadPool, + NamedXContentRegistry xContentRegistry, + AnomalyDetectionAuditor auditor, + ClusterService clusterService, + JobManager jobManager, + JobResultsProvider jobResultsProvider, + JobResultsPersister jobResultsPersister, + JobDataCountsPersister jobDataCountsPersister, + AnnotationPersister annotationPersister, + AutodetectProcessFactory autodetectProcessFactory, + NormalizerFactory normalizerFactory, + NativeStorageProvider nativeStorageProvider, + IndexNameExpressionResolver expressionResolver + ) { this.client = client; this.threadPool = threadPool; this.xContentRegistry = xContentRegistry; @@ -177,12 +188,14 @@ public synchronized void closeAllJobsOnThisNode(String reason) { } public void killProcess(JobTask jobTask, boolean awaitCompletion, String reason) { - logger.trace(() -> new ParameterizedMessage( - "[{}] Killing process: awaitCompletion = [{}]; reason = [{}]", - jobTask.getJobId(), - awaitCompletion, - reason - )); + logger.trace( + () -> new ParameterizedMessage( + "[{}] Killing process: awaitCompletion = [{}]; reason = [{}]", + jobTask.getJobId(), + awaitCompletion, + reason + ) + ); ProcessContext processContext = processByAllocation.remove(jobTask.getAllocationId()); if (processContext != null) { processContext.newKillBuilder() @@ -215,11 +228,7 @@ public void killAllProcessesOnThisNode() { Iterator iterator = processByAllocation.values().iterator(); while (iterator.hasNext()) { ProcessContext processContext = iterator.next(); - processContext.newKillBuilder() - .setAwaitCompletion(false) - .setFinish(false) - .setSilent(true) - .kill(); + processContext.newKillBuilder().setAwaitCompletion(false).setFinish(false).setSilent(true).kill(); iterator.remove(); } } @@ -241,15 +250,15 @@ public synchronized void vacateOpenJobsOnThisNode() { // We ignore jobs that either don't have a running process yet or already closing. // - The ones that don't yet have a running process will get picked up on a subsequent call to this - // method. This is simpler than trying to interact with a job before its process is started, - // and importantly, when it eventually does get picked up it will be fast to shut down again - // since it will only just have been started. + // method. This is simpler than trying to interact with a job before its process is started, + // and importantly, when it eventually does get picked up it will be fast to shut down again + // since it will only just have been started. // - For jobs that are already closing we might as well let them close on the current node - // rather than trying to vacate them to a different node first. + // rather than trying to vacate them to a different node first. if (processContext.getState() == ProcessContext.ProcessStateName.RUNNING && processContext.getJobTask().triggerVacate()) { // We need to fork here, as persisting state is a potentially long-running operation - threadPool.executor(MachineLearning.UTILITY_THREAD_POOL_NAME).execute( - () -> closeProcessAndTask(processContext, processContext.getJobTask(), "node is shutting down")); + threadPool.executor(MachineLearning.UTILITY_THREAD_POOL_NAME) + .execute(() -> closeProcessAndTask(processContext, processContext.getJobTask(), "node is shutting down")); } } } @@ -262,8 +271,11 @@ public synchronized void vacateOpenJobsOnThisNode() { public void persistJob(JobTask jobTask, Consumer handler) { AutodetectCommunicator communicator = getOpenAutodetectCommunicator(jobTask); if (communicator == null) { - String message = String.format(Locale.ROOT, "Cannot persist because job [%s] does not have a corresponding autodetect process", - jobTask.getJobId()); + String message = String.format( + Locale.ROOT, + "Cannot persist because job [%s] does not have a corresponding autodetect process", + jobTask.getJobId() + ); logger.debug(message); handler.accept(ExceptionsHelper.conflictStatusException(message)); return; @@ -291,12 +303,19 @@ public void persistJob(JobTask jobTask, Consumer handler) { * @param params Data processing parameters * @param handler Delegate error or datacount results (Count of records, fields, bytes, etc written as a result of this call) */ - public void processData(JobTask jobTask, AnalysisRegistry analysisRegistry, InputStream input, - XContentType xContentType, DataLoadParams params, BiConsumer handler) { + public void processData( + JobTask jobTask, + AnalysisRegistry analysisRegistry, + InputStream input, + XContentType xContentType, + DataLoadParams params, + BiConsumer handler + ) { AutodetectCommunicator communicator = getOpenAutodetectCommunicator(jobTask); if (communicator == null) { - throw ExceptionsHelper.conflictStatusException("Cannot process data because job [" + jobTask.getJobId() + - "] does not have a corresponding autodetect process"); + throw ExceptionsHelper.conflictStatusException( + "Cannot process data because job [" + jobTask.getJobId() + "] does not have a corresponding autodetect process" + ); } communicator.writeToJob(input, analysisRegistry, xContentType, params, handler); } @@ -314,8 +333,11 @@ public void flushJob(JobTask jobTask, FlushJobParams params, ActionListener handler) { AutodetectCommunicator communicator = getOpenAutodetectCommunicator(jobTask); if (communicator == null) { - String message = "Cannot update the job config because job [" + jobTask.getJobId() + - "] does not have a corresponding autodetect process"; + String message = "Cannot update the job config because job [" + + jobTask.getJobId() + + "] does not have a corresponding autodetect process"; logger.debug(message); handler.accept(ExceptionsHelper.conflictStatusException(message)); return; @@ -376,45 +402,40 @@ public void writeUpdateProcessMessage(JobTask jobTask, UpdateParams updateParams updateProcessMessage.setDetectorUpdates(updateParams.getDetectorUpdates()); // Step 3. Set scheduled events on message and write update process message - ActionListener> eventsListener = ActionListener.wrap( - events -> { - updateProcessMessage.setScheduledEvents(events == null ? null : events.results()); - communicator.writeUpdateProcessMessage(updateProcessMessage.build(), (aVoid, e) -> { - if (e == null) { - handler.accept(null); - } else { - handler.accept(e); - } - }); - }, handler - ); + ActionListener> eventsListener = ActionListener.wrap(events -> { + updateProcessMessage.setScheduledEvents(events == null ? null : events.results()); + communicator.writeUpdateProcessMessage(updateProcessMessage.build(), (aVoid, e) -> { + if (e == null) { + handler.accept(null); + } else { + handler.accept(e); + } + }); + }, handler); // Step 2. Set the filters on the message and get scheduled events - ActionListener> filtersListener = ActionListener.wrap( - filters -> { - updateProcessMessage.setFilters(filters); - - if (updateParams.isUpdateScheduledEvents()) { - jobManager.getJob(jobTask.getJobId(), new ActionListener<>() { - @Override - public void onResponse(Job job) { - Optional>> stats = getStatistics(jobTask); - DataCounts dataCounts = stats.isPresent() ? stats.get().v1() : new DataCounts(job.getId()); - ScheduledEventsQueryBuilder query = new ScheduledEventsQueryBuilder() - .start(job.earliestValidTimestamp(dataCounts)); - jobResultsProvider.scheduledEventsForJob(jobTask.getJobId(), job.getGroups(), query, eventsListener); - } + ActionListener> filtersListener = ActionListener.wrap(filters -> { + updateProcessMessage.setFilters(filters); + + if (updateParams.isUpdateScheduledEvents()) { + jobManager.getJob(jobTask.getJobId(), new ActionListener<>() { + @Override + public void onResponse(Job job) { + Optional>> stats = getStatistics(jobTask); + DataCounts dataCounts = stats.isPresent() ? stats.get().v1() : new DataCounts(job.getId()); + ScheduledEventsQueryBuilder query = new ScheduledEventsQueryBuilder().start(job.earliestValidTimestamp(dataCounts)); + jobResultsProvider.scheduledEventsForJob(jobTask.getJobId(), job.getGroups(), query, eventsListener); + } - @Override - public void onFailure(Exception e) { - handler.accept(e); - } - }); - } else { - eventsListener.onResponse(null); + @Override + public void onFailure(Exception e) { + handler.accept(e); } - }, handler - ); + }); + } else { + eventsListener.onResponse(null); + } + }, handler); // All referenced filters must also be updated Set filterIds = updateParams.extractReferencedFilters(); @@ -425,76 +446,95 @@ public void onFailure(Exception e) { } else { GetFiltersAction.Request getFilterRequest = new GetFiltersAction.Request(Strings.join(filterIds, ",")); getFilterRequest.setPageParams(new PageParams(0, filterIds.size())); - executeAsyncWithOrigin(client, ML_ORIGIN, GetFiltersAction.INSTANCE, getFilterRequest, ActionListener.wrap( - getFilterResponse -> filtersListener.onResponse(getFilterResponse.getFilters().results()), - handler - )); + executeAsyncWithOrigin( + client, + ML_ORIGIN, + GetFiltersAction.INSTANCE, + getFilterRequest, + ActionListener.wrap(getFilterResponse -> filtersListener.onResponse(getFilterResponse.getFilters().results()), handler) + ); } } public void upgradeSnapshot(SnapshotUpgradeTask task, Consumer closeHandler) { final String jobId = task.getJobId(); final String snapshotId = task.getSnapshotId(); - final Function failureBuilder = - (reason) -> new SnapshotUpgradeTaskState(SnapshotUpgradeState.FAILED, task.getAllocationId(), reason); + final Function failureBuilder = (reason) -> new SnapshotUpgradeTaskState( + SnapshotUpgradeState.FAILED, + task.getAllocationId(), + reason + ); // Start the process - jobManager.getJob(jobId, ActionListener.wrap( - job -> { - if (job.getJobVersion() == null) { - closeHandler.accept(ExceptionsHelper.badRequestException("Cannot open job [" + jobId - + "] because jobs created prior to version 5.5 are not supported")); - return; - } - jobResultsProvider.getAutodetectParams(job, snapshotId, params -> { - if (params.modelSnapshot() == null) { - closeHandler.accept(new ElasticsearchStatusException( + jobManager.getJob(jobId, ActionListener.wrap(job -> { + if (job.getJobVersion() == null) { + closeHandler.accept( + ExceptionsHelper.badRequestException( + "Cannot open job [" + jobId + "] because jobs created prior to version 5.5 are not supported" + ) + ); + return; + } + jobResultsProvider.getAutodetectParams(job, snapshotId, params -> { + if (params.modelSnapshot() == null) { + closeHandler.accept( + new ElasticsearchStatusException( "cannot find snapshot [{}] for job [{}] to upgrade", RestStatus.NOT_FOUND, jobId, - snapshotId)); - return; + snapshotId + ) + ); + return; + } + // We need to fork, otherwise we restore model state from a network thread (several GET api calls): + threadPool.executor(MachineLearning.UTILITY_THREAD_POOL_NAME).execute(new AbstractRunnable() { + @Override + public void onFailure(Exception e) { + closeHandler.accept(e); } - // We need to fork, otherwise we restore model state from a network thread (several GET api calls): - threadPool.executor(MachineLearning.UTILITY_THREAD_POOL_NAME).execute(new AbstractRunnable() { - @Override - public void onFailure(Exception e) { - closeHandler.accept(e); - } - @Override - protected void doRun() { - if (nodeDying) { - logger.info(() -> new ParameterizedMessage( + @Override + protected void doRun() { + if (nodeDying) { + logger.info( + () -> new ParameterizedMessage( "Aborted upgrading snapshot [{}] for job [{}] as node is dying", snapshotId, - jobId)); - closeHandler.accept(null); - return; - } - runSnapshotUpgrade(task, job, params, closeHandler); - } - }); - }, e1 -> { - logger.warn(() -> new ParameterizedMessage( - "[{}] [{}] Failed to gather information required to upgrade snapshot job", - jobId, - snapshotId), - e1); - task.updatePersistentTaskState(failureBuilder.apply(e1.getMessage()), ActionListener.wrap( - t -> closeHandler.accept(e1), - e2 -> { - logger.warn(() -> new ParameterizedMessage("[{}] [{}] failed to set task to failed", jobId, snapshotId), e2); - closeHandler.accept(e1); + jobId + ) + ); + closeHandler.accept(null); + return; } - )); + runSnapshotUpgrade(task, job, params, closeHandler); + } }); - }, - closeHandler - )); + }, e1 -> { + logger.warn( + () -> new ParameterizedMessage( + "[{}] [{}] Failed to gather information required to upgrade snapshot job", + jobId, + snapshotId + ), + e1 + ); + task.updatePersistentTaskState( + failureBuilder.apply(e1.getMessage()), + ActionListener.wrap(t -> closeHandler.accept(e1), e2 -> { + logger.warn(() -> new ParameterizedMessage("[{}] [{}] failed to set task to failed", jobId, snapshotId), e2); + closeHandler.accept(e1); + }) + ); + }); + }, closeHandler)); } - public void openJob(JobTask jobTask, ClusterState clusterState, TimeValue masterNodeTimeout, - BiConsumer closeHandler) { + public void openJob( + JobTask jobTask, + ClusterState clusterState, + TimeValue masterNodeTimeout, + BiConsumer closeHandler + ) { String jobId = jobTask.getJobId(); if (jobTask.isClosing()) { logger.info("Aborting opening of job [{}] as it is being closed", jobId); @@ -506,10 +546,10 @@ public void openJob(JobTask jobTask, ClusterState clusterState, TimeValue master // Start the process ActionListener stateAliasHandler = ActionListener.wrap( r -> { - jobManager.getJob(jobId, ActionListener.wrap( - job -> startProcess(jobTask, job, closeHandler), - e -> closeHandler.accept(e, true) - )); + jobManager.getJob( + jobId, + ActionListener.wrap(job -> startProcess(jobTask, job, closeHandler), e -> closeHandler.accept(e, true)) + ); }, e -> { if (ExceptionsHelper.unwrapCause(e) instanceof InvalidAliasNameException) { @@ -542,26 +582,46 @@ public void openJob(JobTask jobTask, ClusterState clusterState, TimeValue master // Try adding the results doc mapping - this updates to the latest version if an old mapping is present ActionListener annotationsIndexUpdateHandler = ActionListener.wrap( - ack -> ElasticsearchMappings.addDocMappingIfMissing(AnomalyDetectorsIndex.jobResultsAliasedName(jobId), - AnomalyDetectorsIndex::wrappedResultsMapping, client, clusterState, masterNodeTimeout, resultsMappingUpdateHandler), + ack -> ElasticsearchMappings.addDocMappingIfMissing( + AnomalyDetectorsIndex.jobResultsAliasedName(jobId), + AnomalyDetectorsIndex::wrappedResultsMapping, + client, + clusterState, + masterNodeTimeout, + resultsMappingUpdateHandler + ), e -> { // Due to a bug in 7.9.0 it's possible that the annotations index already has incorrect mappings // and it would cause more harm than good to block jobs from opening in subsequent releases logger.warn(new ParameterizedMessage("[{}] ML annotations index could not be updated with latest mappings", jobId), e); - ElasticsearchMappings.addDocMappingIfMissing(AnomalyDetectorsIndex.jobResultsAliasedName(jobId), - AnomalyDetectorsIndex::wrappedResultsMapping, client, clusterState, masterNodeTimeout, resultsMappingUpdateHandler); + ElasticsearchMappings.addDocMappingIfMissing( + AnomalyDetectorsIndex.jobResultsAliasedName(jobId), + AnomalyDetectorsIndex::wrappedResultsMapping, + client, + clusterState, + masterNodeTimeout, + resultsMappingUpdateHandler + ); } ); // Create the annotations index if necessary - this also updates the mappings if an old mapping is present - AnnotationIndex.createAnnotationsIndexIfNecessaryAndWaitForYellow(client, clusterState, masterNodeTimeout, - annotationsIndexUpdateHandler); + AnnotationIndex.createAnnotationsIndexIfNecessaryAndWaitForYellow( + client, + clusterState, + masterNodeTimeout, + annotationsIndexUpdateHandler + ); } private void startProcess(JobTask jobTask, Job job, BiConsumer closeHandler) { if (job.getJobVersion() == null) { - closeHandler.accept(ExceptionsHelper.badRequestException("Cannot open job [" + job.getId() - + "] because jobs created prior to version 5.5 are not supported"), true); + closeHandler.accept( + ExceptionsHelper.badRequestException( + "Cannot open job [" + job.getId() + "] because jobs created prior to version 5.5 are not supported" + ), + true + ); return; } @@ -596,8 +656,10 @@ protected void doRun() { // basically identical, i.e. the process has done so little work that making it exit by closing // its input stream will not result in side effects. if (processContext.getJobTask().isClosing()) { - logger.debug("Aborted opening job [{}] as it is being closed or killed (after starting process)", - job.getId()); + logger.debug( + "Aborted opening job [{}] as it is being closed or killed (after starting process)", + job.getId() + ); closeProcessAndTask(processContext, jobTask, "job is already closing"); return; } @@ -607,10 +669,7 @@ protected void doRun() { logSetJobStateFailure(JobState.OPENED, job.getId(), e); if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) { // Don't leave a process with no persistent task hanging around - processContext.newKillBuilder() - .setAwaitCompletion(false) - .setFinish(false) - .kill(); + processContext.newKillBuilder().setAwaitCompletion(false).setFinish(false).kill(); processByAllocation.remove(jobTask.getAllocationId()); } } @@ -620,10 +679,7 @@ protected void doRun() { // No need to log here as the persistent task framework will log it try { // Don't leave a partially initialised process hanging around - processContext.newKillBuilder() - .setAwaitCompletion(false) - .setFinish(false) - .kill(); + processContext.newKillBuilder().setAwaitCompletion(false).setFinish(false).kill(); processByAllocation.remove(jobTask.getAllocationId()); } finally { setJobState(jobTask, JobState.FAILED, e1.getMessage(), e2 -> closeHandler.accept(e1, true)); @@ -638,7 +694,8 @@ protected void doRun() { } private void runSnapshotUpgrade(SnapshotUpgradeTask task, Job job, AutodetectParams params, Consumer handler) { - JobModelSnapshotUpgrader jobModelSnapshotUpgrader = new JobModelSnapshotUpgrader(task, + JobModelSnapshotUpgrader jobModelSnapshotUpgrader = new JobModelSnapshotUpgrader( + task, job, params, threadPool, @@ -647,14 +704,17 @@ private void runSnapshotUpgrade(SnapshotUpgradeTask task, Job job, AutodetectPar client, nativeStorageProvider, handler, - () -> nodeDying == false); + () -> nodeDying == false + ); jobModelSnapshotUpgrader.start(); } - private boolean createProcessAndSetRunning(ProcessContext processContext, - Job job, - AutodetectParams params, - BiConsumer handler) throws IOException { + private boolean createProcessAndSetRunning( + ProcessContext processContext, + Job job, + AutodetectParams params, + BiConsumer handler + ) throws IOException { // At this point we lock the process context until the process has been started. // The reason behind this is to ensure closing the job does not happen before // the process is started as that can result to the job getting seemingly closed @@ -662,8 +722,7 @@ private boolean createProcessAndSetRunning(ProcessContext processContext, processContext.tryLock(); try { if (processContext.getState() != ProcessContext.ProcessStateName.NOT_RUNNING) { - logger.debug("Cannot open job [{}] when its state is [{}]", - job.getId(), processContext.getState().getClass().getName()); + logger.debug("Cannot open job [{}] when its state is [{}]", job.getId(), processContext.getState().getClass().getName()); return false; } if (processContext.getJobTask().isClosing()) { @@ -691,8 +750,10 @@ AutodetectCommunicator create(JobTask jobTask, Job job, AutodetectParams autodet int currentRunningJobs = processByAllocation.size(); // TODO: in future this will also need to consider jobs that are not anomaly detector jobs if (currentRunningJobs > localMaxAllowedRunningJobs) { - throw new ElasticsearchStatusException("max running job capacity [" + localMaxAllowedRunningJobs + "] reached", - RestStatus.TOO_MANY_REQUESTS); + throw new ElasticsearchStatusException( + "max running job capacity [" + localMaxAllowedRunningJobs + "] reached", + RestStatus.TOO_MANY_REQUESTS + ); } String jobId = jobTask.getJobId(); @@ -714,25 +775,32 @@ AutodetectCommunicator create(JobTask jobTask, Job job, AutodetectParams autodet // A TP with no queue, so that we fail immediately if there are no threads available ExecutorService autodetectExecutorService = threadPool.executor(MachineLearning.JOB_COMMS_THREAD_POOL_NAME); DataCountsReporter dataCountsReporter = new DataCountsReporter(job, autodetectParams.dataCounts(), jobDataCountsPersister); - ScoresUpdater scoresUpdater = new ScoresUpdater(job, jobResultsProvider, - new JobRenormalizedResultsPersister(job.getId(), client), normalizerFactory); + ScoresUpdater scoresUpdater = new ScoresUpdater( + job, + jobResultsProvider, + new JobRenormalizedResultsPersister(job.getId(), client), + normalizerFactory + ); ExecutorService renormalizerExecutorService = threadPool.executor(MachineLearning.UTILITY_THREAD_POOL_NAME); - Renormalizer renormalizer = new ShortCircuitingRenormalizer(jobId, scoresUpdater, - renormalizerExecutorService); + Renormalizer renormalizer = new ShortCircuitingRenormalizer(jobId, scoresUpdater, renormalizerExecutorService); - AutodetectProcess process = autodetectProcessFactory.createAutodetectProcess(job, autodetectParams, autodetectExecutorService, - onProcessCrash(jobTask)); - AutodetectResultProcessor processor = - new AutodetectResultProcessor( - client, - auditor, - jobId, - renormalizer, - jobResultsPersister, - annotationPersister, - process, - autodetectParams.modelSizeStats(), - autodetectParams.timingStats()); + AutodetectProcess process = autodetectProcessFactory.createAutodetectProcess( + job, + autodetectParams, + autodetectExecutorService, + onProcessCrash(jobTask) + ); + AutodetectResultProcessor processor = new AutodetectResultProcessor( + client, + auditor, + jobId, + renormalizer, + jobResultsPersister, + annotationPersister, + process, + autodetectParams.modelSizeStats(), + autodetectParams.timingStats() + ); ExecutorService autodetectWorkerExecutor; try (ThreadContext.StoredContext ignore = threadPool.getThreadContext().stashContext()) { autodetectWorkerExecutor = createAutodetectExecutorService(autodetectExecutorService); @@ -747,8 +815,16 @@ AutodetectCommunicator create(JobTask jobTask, Job job, AutodetectParams autodet } throw e; } - return new AutodetectCommunicator(job, process, new StateStreamer(client), dataCountsReporter, processor, handler, - xContentRegistry, autodetectWorkerExecutor); + return new AutodetectCommunicator( + job, + process, + new StateStreamer(client), + dataCountsReporter, + processor, + handler, + xContentRegistry, + autodetectWorkerExecutor + ); } private void notifyLoadingSnapshot(String jobId, AutodetectParams autodetectParams) { @@ -760,13 +836,19 @@ private void notifyLoadingSnapshot(String jobId, AutodetectParams autodetectPara msgBuilder.append(modelSnapshot.getSnapshotId()); msgBuilder.append("] with latest_record_timestamp ["); Date snapshotLatestRecordTimestamp = modelSnapshot.getLatestRecordTimeStamp(); - msgBuilder.append(snapshotLatestRecordTimestamp == null ? "N/A" : - XContentElasticsearchExtension.DEFAULT_FORMATTER.format(snapshotLatestRecordTimestamp.toInstant())); + msgBuilder.append( + snapshotLatestRecordTimestamp == null + ? "N/A" + : XContentElasticsearchExtension.DEFAULT_FORMATTER.format(snapshotLatestRecordTimestamp.toInstant()) + ); } msgBuilder.append("], job latest_record_timestamp ["); Date jobLatestRecordTimestamp = autodetectParams.dataCounts().getLatestRecordTimeStamp(); - msgBuilder.append(jobLatestRecordTimestamp == null ? "N/A" - : XContentElasticsearchExtension.DEFAULT_FORMATTER.format(jobLatestRecordTimestamp.toInstant())); + msgBuilder.append( + jobLatestRecordTimestamp == null + ? "N/A" + : XContentElasticsearchExtension.DEFAULT_FORMATTER.format(jobLatestRecordTimestamp.toInstant()) + ); msgBuilder.append("]"); String msg = msgBuilder.toString(); logger.info("[{}] {}", jobId, msg); @@ -794,10 +876,10 @@ private Consumer onProcessCrash(JobTask jobTask) { private void closeProcessAndTask(ProcessContext processContext, JobTask jobTask, String reason) { String jobId = jobTask.getJobId(); long allocationId = jobTask.getAllocationId(); - // We use a lock to prevent simultaneous open and close from conflicting. However, we found + // We use a lock to prevent simultaneous open and close from conflicting. However, we found // that we could not use the lock to stop kill from conflicting because that could lead to // a kill taking an unacceptably long time to have an effect, which largely defeats the point - // of having an option to quickly kill a process. Therefore we have to deal with the effects + // of having an option to quickly kill a process. Therefore we have to deal with the effects // of kill running simultaneously with open and close. boolean jobKilled = false; processContext.tryLock(); @@ -834,7 +916,7 @@ private void closeProcessAndTask(ProcessContext processContext, JobTask jobTask, communicator.killProcess(true, false, false); } else { // communicator.close() may take a long time to run, if the job persists a large model state as a - // result of calling it. We want to leave open the option to kill the job during this time, which + // result of calling it. We want to leave open the option to kill the job during this time, which // is why the allocation ID must remain in the map until after the close is complete. communicator.close(); processByAllocation.remove(allocationId); @@ -844,11 +926,16 @@ private void closeProcessAndTask(ProcessContext processContext, JobTask jobTask, // If the close failed because the process has explicitly been killed by us then just pass on that exception. // (Note that jobKilled may be false in this case, if the kill is executed while communicator.close() is running.) if (e instanceof ElasticsearchStatusException && ((ElasticsearchStatusException) e).status() == RestStatus.CONFLICT) { - logger.trace("[{}] Conflict between kill and {} during autodetect process cleanup - job {} before cleanup started", - jobId, jobTask.isVacating() ? "vacate" : "close", jobKilled ? "killed" : "not killed"); + logger.trace( + "[{}] Conflict between kill and {} during autodetect process cleanup - job {} before cleanup started", + jobId, + jobTask.isVacating() ? "vacate" : "close", + jobKilled ? "killed" : "not killed" + ); throw (ElasticsearchStatusException) e; } - String msg = jobKilled ? "Exception cleaning up autodetect process started after kill" + String msg = jobKilled + ? "Exception cleaning up autodetect process started after kill" : "Exception " + (jobTask.isVacating() ? "vacating" : "closing") + " autodetect process"; logger.warn("[" + jobId + "] " + msg, e); setJobState(jobTask, JobState.FAILED, e.getMessage()); @@ -888,9 +975,7 @@ public void closeJob(JobTask jobTask, String reason) { } int numberOfOpenJobs() { - return (int) processByAllocation.values().stream() - .filter(p -> p.getState() != ProcessContext.ProcessStateName.DYING) - .count(); + return (int) processByAllocation.values().stream().filter(p -> p.getState() != ProcessContext.ProcessStateName.DYING).count(); } boolean jobHasActiveAutodetectProcess(JobTask jobTask) { @@ -927,10 +1012,13 @@ public Optional jobOpenTime(JobTask jobTask) { void setJobState(JobTask jobTask, JobState state, String reason) { JobTaskState jobTaskState = new JobTaskState(state, jobTask.getAllocationId(), reason); - jobTask.updatePersistentTaskState(jobTaskState, ActionListener.wrap( - persistentTask -> logger.info("Successfully set job state to [{}] for job [{}]", state, jobTask.getJobId()), - e -> logSetJobStateFailure(state, jobTask.getJobId(), e) - )); + jobTask.updatePersistentTaskState( + jobTaskState, + ActionListener.wrap( + persistentTask -> logger.info("Successfully set job state to [{}] for job [{}]", state, jobTask.getJobId()), + e -> logSetJobStateFailure(state, jobTask.getJobId(), e) + ) + ); } private void logSetJobStateFailure(JobState state, String jobId, Exception e) { @@ -943,21 +1031,19 @@ private void logSetJobStateFailure(JobState state, String jobId, Exception e) { void setJobState(JobTask jobTask, JobState state, String reason, CheckedConsumer handler) { JobTaskState jobTaskState = new JobTaskState(state, jobTask.getAllocationId(), reason); - jobTask.updatePersistentTaskState(jobTaskState, ActionListener.wrap( - persistentTask -> { - try { - handler.accept(null); - } catch (IOException e1) { - logger.warn("Error while delegating response", e1); - } - }, - e -> { - try { - handler.accept(e); - } catch (IOException e1) { - logger.warn("Error while delegating exception [" + e.getMessage() + "]", e1); - } - })); + jobTask.updatePersistentTaskState(jobTaskState, ActionListener.wrap(persistentTask -> { + try { + handler.accept(null); + } catch (IOException e1) { + logger.warn("Error while delegating response", e1); + } + }, e -> { + try { + handler.accept(e); + } catch (IOException e1) { + logger.warn("Error while delegating exception [" + e.getMessage() + "]", e1); + } + })); } public Optional>> getStatistics(JobTask jobTask) { @@ -966,7 +1052,8 @@ public Optional>> getStatis return Optional.empty(); } return Optional.of( - new Tuple<>(communicator.getDataCounts(), new Tuple<>(communicator.getModelSizeStats(), communicator.getTimingStats()))); + new Tuple<>(communicator.getDataCounts(), new Tuple<>(communicator.getModelSizeStats(), communicator.getTimingStats())) + ); } ExecutorService createAutodetectExecutorService(ExecutorService executorService) { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/BlackHoleAutodetectProcess.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/BlackHoleAutodetectProcess.java index 7a332cd50fbae..51ea051875c92 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/BlackHoleAutodetectProcess.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/BlackHoleAutodetectProcess.java @@ -12,13 +12,13 @@ import org.elasticsearch.xpack.core.ml.job.config.MlFilter; import org.elasticsearch.xpack.core.ml.job.config.ModelPlotConfig; import org.elasticsearch.xpack.core.ml.job.config.PerPartitionCategorizationConfig; -import org.elasticsearch.xpack.ml.job.persistence.StateStreamer; import org.elasticsearch.xpack.core.ml.job.process.autodetect.output.FlushAcknowledgement; +import org.elasticsearch.xpack.core.ml.job.process.autodetect.state.ModelSnapshot; +import org.elasticsearch.xpack.core.ml.job.process.autodetect.state.Quantiles; +import org.elasticsearch.xpack.ml.job.persistence.StateStreamer; import org.elasticsearch.xpack.ml.job.process.autodetect.params.DataLoadParams; import org.elasticsearch.xpack.ml.job.process.autodetect.params.FlushJobParams; import org.elasticsearch.xpack.ml.job.process.autodetect.params.ForecastParams; -import org.elasticsearch.xpack.core.ml.job.process.autodetect.state.ModelSnapshot; -import org.elasticsearch.xpack.core.ml.job.process.autodetect.state.Quantiles; import org.elasticsearch.xpack.ml.job.results.AutodetectResult; import java.time.ZonedDateTime; @@ -59,8 +59,7 @@ public BlackHoleAutodetectProcess(String jobId, Consumer onProcessCrash) } @Override - public void restoreState(StateStreamer stateStreamer, ModelSnapshot modelSnapshot) { - } + public void restoreState(StateStreamer stateStreamer, ModelSnapshot modelSnapshot) {} @Override public boolean isReady() { @@ -78,28 +77,22 @@ public void writeRecord(String[] record) { } @Override - public void writeResetBucketsControlMessage(DataLoadParams params) { - } + public void writeResetBucketsControlMessage(DataLoadParams params) {} @Override - public void writeUpdateModelPlotMessage(ModelPlotConfig modelPlotConfig) { - } + public void writeUpdateModelPlotMessage(ModelPlotConfig modelPlotConfig) {} @Override - public void writeUpdatePerPartitionCategorizationMessage(PerPartitionCategorizationConfig perPartitionCategorizationConfig) { - } + public void writeUpdatePerPartitionCategorizationMessage(PerPartitionCategorizationConfig perPartitionCategorizationConfig) {} @Override - public void writeUpdateDetectorRulesMessage(int detectorIndex, List rules) { - } + public void writeUpdateDetectorRulesMessage(int detectorIndex, List rules) {} @Override - public void writeUpdateFiltersMessage(List filters) { - } + public void writeUpdateFiltersMessage(List filters) {} @Override - public void writeUpdateScheduledEventsMessage(List events, TimeValue bucketSpan) { - } + public void writeUpdateScheduledEventsMessage(List events, TimeValue bucketSpan) {} /** * Accept the request do nothing with it but write the flush acknowledgement to {@link #readAutodetectResults()} @@ -109,30 +102,53 @@ public void writeUpdateScheduledEventsMessage(List events, TimeV @Override public String flushJob(FlushJobParams params) { FlushAcknowledgement flushAcknowledgement = new FlushAcknowledgement(FLUSH_ID, 0L); - AutodetectResult result = - new AutodetectResult(null, null, null, null, null, null, null, null, null, null, null, null, flushAcknowledgement); + AutodetectResult result = new AutodetectResult( + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + flushAcknowledgement + ); results.add(result); return FLUSH_ID; } @Override - public void persistState() { - } + public void persistState() {} @Override - public void persistState(long snapshotTimestamp, String snapshotId, String snapshotDescription) { - } + public void persistState(long snapshotTimestamp, String snapshotId, String snapshotDescription) {} @Override - public void flushStream() { - } + public void flushStream() {} @Override public void close() { if (open) { Quantiles quantiles = new Quantiles(jobId, new Date(), "black hole quantiles"); - AutodetectResult result = - new AutodetectResult(null, null, null, quantiles, null, null, null, null, null, null, null, null, null); + AutodetectResult result = new AutodetectResult( + null, + null, + null, + quantiles, + null, + null, + null, + null, + null, + null, + null, + null, + null + ); results.add(result); open = false; } @@ -175,8 +191,7 @@ public AutodetectResult next() { } @Override - public void consumeAndCloseOutputStream() { - } + public void consumeAndCloseOutputStream() {} @Override public ZonedDateTime getProcessStartTime() { @@ -204,6 +219,5 @@ public String readError() { } @Override - public void forecastJob(ForecastParams params) { - } + public void forecastJob(ForecastParams params) {} } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/JobModelSnapshotUpgrader.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/JobModelSnapshotUpgrader.java index bf1f2ed16ff70..85e95e9cacfae 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/JobModelSnapshotUpgrader.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/JobModelSnapshotUpgrader.java @@ -69,16 +69,18 @@ public final class JobModelSnapshotUpgrader { private final JobResultsPersister jobResultsPersister; private final NativeStorageProvider nativeStorageProvider; - JobModelSnapshotUpgrader(SnapshotUpgradeTask task, - Job job, - AutodetectParams params, - ThreadPool threadPool, - AutodetectProcessFactory autodetectProcessFactory, - JobResultsPersister jobResultsPersister, - Client client, - NativeStorageProvider nativeStorageProvider, - Consumer onFinish, - Supplier continueRunning) { + JobModelSnapshotUpgrader( + SnapshotUpgradeTask task, + Job job, + AutodetectParams params, + ThreadPool threadPool, + AutodetectProcessFactory autodetectProcessFactory, + JobResultsPersister jobResultsPersister, + Client client, + NativeStorageProvider nativeStorageProvider, + Consumer onFinish, + Supplier continueRunning + ) { this.task = Objects.requireNonNull(task); this.job = Objects.requireNonNull(job); this.params = Objects.requireNonNull(params); @@ -97,27 +99,29 @@ void start() { // A TP with no queue, so that we fail immediately if there are no threads available ExecutorService autodetectExecutorService = threadPool.executor(MachineLearning.JOB_COMMS_THREAD_POOL_NAME); - AutodetectProcess process = autodetectProcessFactory.createAutodetectProcess(jobId + "-" + snapshotId, + AutodetectProcess process = autodetectProcessFactory.createAutodetectProcess( + jobId + "-" + snapshotId, job, params, autodetectExecutorService, (reason) -> { - setTaskToFailed(reason, ActionListener.wrap(t -> { - }, f -> { - })); + setTaskToFailed(reason, ActionListener.wrap(t -> {}, f -> {})); try { nativeStorageProvider.cleanupLocalTmpStorage(task.getDescription()); } catch (IOException e) { logger.error( new ParameterizedMessage("[{}] [{}] failed to delete temporary files snapshot upgrade", jobId, snapshotId), - e); + e + ); } - }); + } + ); JobSnapshotUpgraderResultProcessor processor = new JobSnapshotUpgraderResultProcessor( jobId, snapshotId, jobResultsPersister, - process); + process + ); ProcessWorkerExecutorService autodetectWorkerExecutor; try (ThreadContext.StoredContext ignore = threadPool.getThreadContext().stashContext()) { autodetectWorkerExecutor = new AutodetectWorkerExecutorService(threadPool.getThreadContext()); @@ -145,19 +149,11 @@ void start() { } void setTaskToFailed(String reason, ActionListener> listener) { - SnapshotUpgradeTaskState taskState = new SnapshotUpgradeTaskState( - SnapshotUpgradeState.FAILED, - task.getAllocationId(), - reason); - task.updatePersistentTaskState(taskState, ActionListener.wrap( - listener::onResponse, - f -> { - logger.warn( - () -> new ParameterizedMessage("[{}] [{}] failed to set task to failed", task.getJobId(), task.getSnapshotId()), - f); - listener.onFailure(f); - } - )); + SnapshotUpgradeTaskState taskState = new SnapshotUpgradeTaskState(SnapshotUpgradeState.FAILED, task.getAllocationId(), reason); + task.updatePersistentTaskState(taskState, ActionListener.wrap(listener::onResponse, f -> { + logger.warn(() -> new ParameterizedMessage("[{}] [{}] failed to set task to failed", task.getJobId(), task.getSnapshotId()), f); + listener.onFailure(f); + })); } private class Executor { @@ -167,10 +163,12 @@ private class Executor { private final ExecutorService autodetectWorkerExecutor; private final AutodetectProcess process; - Executor(StateStreamer stateStreamer, - JobSnapshotUpgraderResultProcessor processor, - ExecutorService autodetectWorkerExecutor, - AutodetectProcess process) { + Executor( + StateStreamer stateStreamer, + JobSnapshotUpgraderResultProcessor processor, + ExecutorService autodetectWorkerExecutor, + AutodetectProcess process + ) { this.stateStreamer = stateStreamer; this.processor = processor; this.autodetectWorkerExecutor = autodetectWorkerExecutor; @@ -236,8 +234,10 @@ void restoreState() { process.restoreState(stateStreamer, params.modelSnapshot()); } catch (Exception e) { logger.error(() -> new ParameterizedMessage("[{}] [{}] failed to write old state", jobId, snapshotId), e); - setTaskToFailed("Failed to write old state due to: " + e.getMessage(), - ActionListener.wrap(t -> shutdown(e), f -> shutdown(e))); + setTaskToFailed( + "Failed to write old state due to: " + e.getMessage(), + ActionListener.wrap(t -> shutdown(e), f -> shutdown(e)) + ); return; } submitOperation(() -> { @@ -247,24 +247,20 @@ void restoreState() { }, (flushAcknowledgement, e) -> { Runnable nextStep; if (e != null) { - logger.error( - () -> new ParameterizedMessage( - "[{}] [{}] failed to flush after writing old state", - jobId, - snapshotId - ), - e); + logger.error(() -> new ParameterizedMessage("[{}] [{}] failed to flush after writing old state", jobId, snapshotId), e); nextStep = () -> setTaskToFailed( "Failed to flush after writing old state due to: " + e.getMessage(), ActionListener.wrap(t -> shutdown(e), f -> shutdown(e)) ); } else { - logger.debug(() -> new ParameterizedMessage( - "[{}] [{}] flush [{}] acknowledged requesting state write", - jobId, - snapshotId, - flushAcknowledgement.getId() - )); + logger.debug( + () -> new ParameterizedMessage( + "[{}] [{}] flush [{}] acknowledged requesting state write", + jobId, + snapshotId, + flushAcknowledgement.getId() + ) + ); nextStep = this::requestStateWrite; } threadPool.executor(UTILITY_THREAD_POOL_NAME).execute(nextStep); @@ -274,41 +270,41 @@ void restoreState() { private void requestStateWrite() { task.updatePersistentTaskState( new SnapshotUpgradeTaskState(SnapshotUpgradeState.SAVING_NEW_STATE, task.getAllocationId(), ""), - ActionListener.wrap( - readingNewState -> { - if (continueRunning.get() == false) { - shutdown(null); - return; - } - submitOperation( - () -> { - process.persistState( - params.modelSnapshot().getTimestamp().getTime(), - params.modelSnapshot().getSnapshotId(), - params.modelSnapshot().getDescription()); - return null; - }, - // Execute callback in the UTILITY thread pool, as the current thread in the callback will be one in the - // autodetectWorkerExecutor. Trying to run the callback in that executor will cause a dead lock as that - // executor has a single processing queue. - (aVoid, e) -> threadPool.executor(UTILITY_THREAD_POOL_NAME).execute(() -> shutdown(e))); - logger.info("asked for state to be persisted"); + ActionListener.wrap(readingNewState -> { + if (continueRunning.get() == false) { + shutdown(null); + return; + } + submitOperation(() -> { + process.persistState( + params.modelSnapshot().getTimestamp().getTime(), + params.modelSnapshot().getSnapshotId(), + params.modelSnapshot().getDescription() + ); + return null; }, - f -> { - logger.error( - () -> new ParameterizedMessage( - "[{}] [{}] failed to update snapshot upgrader task to started", - jobId, - snapshotId), - f); - shutdown(new ElasticsearchStatusException( + // Execute callback in the UTILITY thread pool, as the current thread in the callback will be one in the + // autodetectWorkerExecutor. Trying to run the callback in that executor will cause a dead lock as that + // executor has a single processing queue. + (aVoid, e) -> threadPool.executor(UTILITY_THREAD_POOL_NAME).execute(() -> shutdown(e)) + ); + logger.info("asked for state to be persisted"); + }, f -> { + logger.error( + () -> new ParameterizedMessage("[{}] [{}] failed to update snapshot upgrader task to started", jobId, snapshotId), + f + ); + shutdown( + new ElasticsearchStatusException( "Failed to start snapshot upgrade [{}] for job [{}]", RestStatus.INTERNAL_SERVER_ERROR, f, snapshotId, - jobId)); - } - )); + jobId + ) + ); + }) + ); } private void submitOperation(CheckedSupplier operation, BiConsumer handler) { @@ -316,8 +312,13 @@ private void submitOperation(CheckedSupplier operation, BiCons @Override public void onFailure(Exception e) { if (continueRunning.get() == false) { - handler.accept(null, ExceptionsHelper.conflictStatusException( - "[{}] Could not submit operation to process as it has been killed", job.getId())); + handler.accept( + null, + ExceptionsHelper.conflictStatusException( + "[{}] Could not submit operation to process as it has been killed", + job.getId() + ) + ); } else { logger.error(new ParameterizedMessage("[{}] Unexpected exception writing to process", job.getId()), e); handler.accept(null, e); @@ -327,8 +328,13 @@ public void onFailure(Exception e) { @Override protected void doRun() throws Exception { if (continueRunning.get() == false) { - handler.accept(null, ExceptionsHelper.conflictStatusException( - "[{}] Could not submit operation to process as it has been killed", job.getId())); + handler.accept( + null, + ExceptionsHelper.conflictStatusException( + "[{}] Could not submit operation to process as it has been killed", + job.getId() + ) + ); } else { checkProcessIsAlive(); handler.accept(operation.get(), null); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/NativeAutodetectProcess.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/NativeAutodetectProcess.java index 7f40fccc80cc2..ff7c507c08462 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/NativeAutodetectProcess.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/NativeAutodetectProcess.java @@ -22,9 +22,9 @@ import org.elasticsearch.xpack.ml.job.process.autodetect.writer.AutodetectControlMsgWriter; import org.elasticsearch.xpack.ml.job.results.AutodetectResult; import org.elasticsearch.xpack.ml.process.AbstractNativeProcess; +import org.elasticsearch.xpack.ml.process.NativeController; import org.elasticsearch.xpack.ml.process.ProcessPipes; import org.elasticsearch.xpack.ml.process.ProcessResultsParser; -import org.elasticsearch.xpack.ml.process.NativeController; import java.io.IOException; import java.io.OutputStream; @@ -44,9 +44,15 @@ class NativeAutodetectProcess extends AbstractNativeProcess implements Autodetec private final ProcessResultsParser resultsParser; - NativeAutodetectProcess(String jobId, NativeController nativeController, ProcessPipes processPipes, - int numberOfFields, List filesToDelete, ProcessResultsParser resultsParser, - Consumer onProcessCrash) { + NativeAutodetectProcess( + String jobId, + NativeController nativeController, + ProcessPipes processPipes, + int numberOfFields, + List filesToDelete, + ProcessResultsParser resultsParser, + Consumer onProcessCrash + ) { super(jobId, nativeController, processPipes, numberOfFields, filesToDelete, onProcessCrash); this.resultsParser = resultsParser; } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/NativeAutodetectProcessFactory.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/NativeAutodetectProcessFactory.java index 9434087f90588..6e7511771e42c 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/NativeAutodetectProcessFactory.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/NativeAutodetectProcessFactory.java @@ -10,11 +10,11 @@ import org.apache.logging.log4j.Logger; import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException; -import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.env.Environment; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.ml.job.config.Job; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; import org.elasticsearch.xpack.ml.MachineLearning; @@ -50,12 +50,14 @@ public class NativeAutodetectProcessFactory implements AutodetectProcessFactory private final AnomalyDetectionAuditor auditor; private volatile Duration processConnectTimeout; - public NativeAutodetectProcessFactory(Environment env, - Settings settings, - NativeController nativeController, - ClusterService clusterService, - ResultsPersisterService resultsPersisterService, - AnomalyDetectionAuditor auditor) { + public NativeAutodetectProcessFactory( + Environment env, + Settings settings, + NativeController nativeController, + ClusterService clusterService, + ResultsPersisterService resultsPersisterService, + AnomalyDetectionAuditor auditor + ) { this.env = Objects.requireNonNull(env); this.settings = Objects.requireNonNull(settings); this.nativeController = Objects.requireNonNull(nativeController); @@ -63,8 +65,8 @@ public NativeAutodetectProcessFactory(Environment env, this.resultsPersisterService = Objects.requireNonNull(resultsPersisterService); this.auditor = Objects.requireNonNull(auditor); setProcessConnectTimeout(MachineLearning.PROCESS_CONNECT_TIMEOUT.get(settings)); - clusterService.getClusterSettings().addSettingsUpdateConsumer(MachineLearning.PROCESS_CONNECT_TIMEOUT, - this::setProcessConnectTimeout); + clusterService.getClusterSettings() + .addSettingsUpdateConsumer(MachineLearning.PROCESS_CONNECT_TIMEOUT, this::setProcessConnectTimeout); } void setProcessConnectTimeout(TimeValue processConnectTimeout) { @@ -72,14 +74,27 @@ void setProcessConnectTimeout(TimeValue processConnectTimeout) { } @Override - public AutodetectProcess createAutodetectProcess(String pipelineId, - Job job, - AutodetectParams params, - ExecutorService executorService, - Consumer onProcessCrash) { + public AutodetectProcess createAutodetectProcess( + String pipelineId, + Job job, + AutodetectParams params, + ExecutorService executorService, + Consumer onProcessCrash + ) { List filesToDelete = new ArrayList<>(); - ProcessPipes processPipes = new ProcessPipes(env, NAMED_PIPE_HELPER, processConnectTimeout, AutodetectBuilder.AUTODETECT, - pipelineId, null, false, true, true, params.modelSnapshot() != null, true); + ProcessPipes processPipes = new ProcessPipes( + env, + NAMED_PIPE_HELPER, + processConnectTimeout, + AutodetectBuilder.AUTODETECT, + pipelineId, + null, + false, + true, + true, + params.modelSnapshot() != null, + true + ); createNativeProcess(job, params, processPipes, filesToDelete); boolean includeTokensField = MachineLearning.CATEGORIZATION_TOKENIZATION_IN_JAVA && job.getAnalysisConfig().getCategorizationFieldName() != null; @@ -87,11 +102,19 @@ public AutodetectProcess createAutodetectProcess(String pipelineId, int numberOfFields = job.allInputFields().size() + (includeTokensField ? 1 : 0) + 1; IndexingStateProcessor stateProcessor = new IndexingStateProcessor(job.getId(), resultsPersisterService, auditor); - ProcessResultsParser resultsParser = new ProcessResultsParser<>(AutodetectResult.PARSER, - NamedXContentRegistry.EMPTY); + ProcessResultsParser resultsParser = new ProcessResultsParser<>( + AutodetectResult.PARSER, + NamedXContentRegistry.EMPTY + ); NativeAutodetectProcess autodetect = new NativeAutodetectProcess( - job.getId(), nativeController, processPipes, numberOfFields, - filesToDelete, resultsParser, onProcessCrash); + job.getId(), + nativeController, + processPipes, + numberOfFields, + filesToDelete, + resultsParser, + onProcessCrash + ); try { autodetect.start(executorService, stateProcessor); return autodetect; @@ -107,20 +130,26 @@ public AutodetectProcess createAutodetectProcess(String pipelineId, } } - void createNativeProcess(Job job, AutodetectParams autodetectParams, ProcessPipes processPipes, - List filesToDelete) { + void createNativeProcess(Job job, AutodetectParams autodetectParams, ProcessPipes processPipes, List filesToDelete) { try { Settings updatedSettings = Settings.builder() .put(settings) - .put(AutodetectBuilder.MAX_ANOMALY_RECORDS_SETTING_DYNAMIC.getKey(), - clusterService.getClusterSettings().get(AutodetectBuilder.MAX_ANOMALY_RECORDS_SETTING_DYNAMIC)) + .put( + AutodetectBuilder.MAX_ANOMALY_RECORDS_SETTING_DYNAMIC.getKey(), + clusterService.getClusterSettings().get(AutodetectBuilder.MAX_ANOMALY_RECORDS_SETTING_DYNAMIC) + ) .build(); - AutodetectBuilder autodetectBuilder = new AutodetectBuilder(job, filesToDelete, logger, env, - updatedSettings, nativeController, processPipes) - .referencedFilters(autodetectParams.filters()) - .scheduledEvents(autodetectParams.scheduledEvents()); + AutodetectBuilder autodetectBuilder = new AutodetectBuilder( + job, + filesToDelete, + logger, + env, + updatedSettings, + nativeController, + processPipes + ).referencedFilters(autodetectParams.filters()).scheduledEvents(autodetectParams.scheduledEvents()); // if state is null or empty it will be ignored // else it is used to restore the quantiles @@ -139,4 +168,3 @@ void createNativeProcess(Job job, AutodetectParams autodetectParams, ProcessPipe } } - diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/ProcessContext.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/ProcessContext.java index 2440c9a761d8a..4d79873d01831 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/ProcessContext.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/ProcessContext.java @@ -144,7 +144,9 @@ void kill() { } enum ProcessStateName { - NOT_RUNNING, RUNNING, DYING + NOT_RUNNING, + RUNNING, + DYING } private interface ProcessState { @@ -152,10 +154,12 @@ private interface ProcessState { * @return was a state change made? * */ boolean setRunning(ProcessContext processContext, AutodetectCommunicator autodetectCommunicator); + /** * @return was a state change made? */ boolean setDying(ProcessContext processContext); + ProcessStateName getName(); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/UpdateParams.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/UpdateParams.java index 7c75cd6c6ec6e..af69e13204024 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/UpdateParams.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/UpdateParams.java @@ -26,10 +26,14 @@ public final class UpdateParams { private final MlFilter filter; private final boolean updateScheduledEvents; - private UpdateParams(String jobId, @Nullable ModelPlotConfig modelPlotConfig, - @Nullable PerPartitionCategorizationConfig perPartitionCategorizationConfig, - @Nullable List detectorUpdates, - @Nullable MlFilter filter, boolean updateScheduledEvents) { + private UpdateParams( + String jobId, + @Nullable ModelPlotConfig modelPlotConfig, + @Nullable PerPartitionCategorizationConfig perPartitionCategorizationConfig, + @Nullable List detectorUpdates, + @Nullable MlFilter filter, + boolean updateScheduledEvents + ) { this.jobId = Objects.requireNonNull(jobId); this.modelPlotConfig = modelPlotConfig; this.perPartitionCategorizationConfig = perPartitionCategorizationConfig; @@ -86,19 +90,18 @@ public Set extractReferencedFilters() { } if (detectorUpdates != null) { detectorUpdates.forEach( - detectorUpdate -> detectorUpdate.getRules().forEach( - rule -> filterIds.addAll(rule.extractReferencedFilters()))); + detectorUpdate -> detectorUpdate.getRules().forEach(rule -> filterIds.addAll(rule.extractReferencedFilters())) + ); } return filterIds; } public static UpdateParams fromJobUpdate(JobUpdate jobUpdate) { - return new Builder(jobUpdate.getJobId()) - .modelPlotConfig(jobUpdate.getModelPlotConfig()) - .perPartitionCategorizationConfig(jobUpdate.getPerPartitionCategorizationConfig()) - .detectorUpdates(jobUpdate.getDetectorUpdates()) - .updateScheduledEvents(jobUpdate.getGroups() != null) - .build(); + return new Builder(jobUpdate.getJobId()).modelPlotConfig(jobUpdate.getModelPlotConfig()) + .perPartitionCategorizationConfig(jobUpdate.getPerPartitionCategorizationConfig()) + .detectorUpdates(jobUpdate.getDetectorUpdates()) + .updateScheduledEvents(jobUpdate.getGroups() != null) + .build(); } public static UpdateParams filterUpdate(String jobId, MlFilter filter) { @@ -152,8 +155,14 @@ public Builder updateScheduledEvents(boolean updateScheduledEvents) { } public UpdateParams build() { - return new UpdateParams(jobId, modelPlotConfig, perPartitionCategorizationConfig, detectorUpdates, filter, - updateScheduledEvents); + return new UpdateParams( + jobId, + modelPlotConfig, + perPartitionCategorizationConfig, + detectorUpdates, + filter, + updateScheduledEvents + ); } } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/UpdateProcessMessage.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/UpdateProcessMessage.java index 1136a1c5c7421..4c833d115f0da 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/UpdateProcessMessage.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/UpdateProcessMessage.java @@ -17,16 +17,24 @@ public final class UpdateProcessMessage { - @Nullable private final ModelPlotConfig modelPlotConfig; - @Nullable private final PerPartitionCategorizationConfig perPartitionCategorizationConfig; - @Nullable private final List detectorUpdates; - @Nullable private final List filters; - @Nullable private final List scheduledEvents; - - private UpdateProcessMessage(@Nullable ModelPlotConfig modelPlotConfig, - @Nullable PerPartitionCategorizationConfig perPartitionCategorizationConfig, - @Nullable List detectorUpdates, - @Nullable List filters, List scheduledEvents) { + @Nullable + private final ModelPlotConfig modelPlotConfig; + @Nullable + private final PerPartitionCategorizationConfig perPartitionCategorizationConfig; + @Nullable + private final List detectorUpdates; + @Nullable + private final List filters; + @Nullable + private final List scheduledEvents; + + private UpdateProcessMessage( + @Nullable ModelPlotConfig modelPlotConfig, + @Nullable PerPartitionCategorizationConfig perPartitionCategorizationConfig, + @Nullable List detectorUpdates, + @Nullable List filters, + List scheduledEvents + ) { this.modelPlotConfig = modelPlotConfig; this.perPartitionCategorizationConfig = perPartitionCategorizationConfig; this.detectorUpdates = detectorUpdates; @@ -61,11 +69,16 @@ public List getScheduledEvents() { public static class Builder { - @Nullable private ModelPlotConfig modelPlotConfig; - @Nullable private PerPartitionCategorizationConfig perPartitionCategorizationConfig; - @Nullable private List detectorUpdates; - @Nullable private List filters; - @Nullable private List scheduledEvents; + @Nullable + private ModelPlotConfig modelPlotConfig; + @Nullable + private PerPartitionCategorizationConfig perPartitionCategorizationConfig; + @Nullable + private List detectorUpdates; + @Nullable + private List filters; + @Nullable + private List scheduledEvents; public Builder setModelPlotConfig(ModelPlotConfig modelPlotConfig) { this.modelPlotConfig = modelPlotConfig; diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/output/AutodetectResultProcessor.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/output/AutodetectResultProcessor.java index 0aeb0010a6a1e..fe5fc477bb238 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/output/AutodetectResultProcessor.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/output/AutodetectResultProcessor.java @@ -15,17 +15,16 @@ import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.support.WriteRequest; import org.elasticsearch.client.Client; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xpack.core.ml.MachineLearningField; import org.elasticsearch.xpack.core.ml.action.PutJobAction; import org.elasticsearch.xpack.core.ml.action.UpdateJobAction; import org.elasticsearch.xpack.core.ml.annotations.Annotation; -import org.elasticsearch.xpack.core.ml.job.process.autodetect.state.CategorizerStats; -import org.elasticsearch.xpack.ml.annotations.AnnotationPersister; import org.elasticsearch.xpack.core.ml.job.config.JobUpdate; import org.elasticsearch.xpack.core.ml.job.messages.Messages; import org.elasticsearch.xpack.core.ml.job.process.autodetect.output.FlushAcknowledgement; +import org.elasticsearch.xpack.core.ml.job.process.autodetect.state.CategorizerStats; import org.elasticsearch.xpack.core.ml.job.process.autodetect.state.ModelSizeStats; import org.elasticsearch.xpack.core.ml.job.process.autodetect.state.ModelSnapshot; import org.elasticsearch.xpack.core.ml.job.process.autodetect.state.Quantiles; @@ -38,6 +37,7 @@ import org.elasticsearch.xpack.core.ml.job.results.Influencer; import org.elasticsearch.xpack.core.ml.job.results.ModelPlot; import org.elasticsearch.xpack.core.security.user.XPackUser; +import org.elasticsearch.xpack.ml.annotations.AnnotationPersister; import org.elasticsearch.xpack.ml.job.persistence.JobResultsPersister; import org.elasticsearch.xpack.ml.job.persistence.TimingStatsReporter; import org.elasticsearch.xpack.ml.job.process.autodetect.AutodetectProcess; @@ -109,23 +109,46 @@ public class AutodetectResultProcessor { */ private volatile ModelSizeStats latestModelSizeStats; - public AutodetectResultProcessor(Client client, - AnomalyDetectionAuditor auditor, - String jobId, - Renormalizer renormalizer, - JobResultsPersister persister, - AnnotationPersister annotationPersister, - AutodetectProcess process, - ModelSizeStats latestModelSizeStats, - TimingStats timingStats) { - this(client, auditor, jobId, renormalizer, persister, annotationPersister, process, latestModelSizeStats, timingStats, - Clock.systemUTC(), new FlushListener()); + public AutodetectResultProcessor( + Client client, + AnomalyDetectionAuditor auditor, + String jobId, + Renormalizer renormalizer, + JobResultsPersister persister, + AnnotationPersister annotationPersister, + AutodetectProcess process, + ModelSizeStats latestModelSizeStats, + TimingStats timingStats + ) { + this( + client, + auditor, + jobId, + renormalizer, + persister, + annotationPersister, + process, + latestModelSizeStats, + timingStats, + Clock.systemUTC(), + new FlushListener() + ); } // Visible for testing - AutodetectResultProcessor(Client client, AnomalyDetectionAuditor auditor, String jobId, Renormalizer renormalizer, - JobResultsPersister persister, AnnotationPersister annotationPersister, AutodetectProcess autodetectProcess, - ModelSizeStats latestModelSizeStats, TimingStats timingStats, Clock clock, FlushListener flushListener) { + AutodetectResultProcessor( + Client client, + AnomalyDetectionAuditor auditor, + String jobId, + Renormalizer renormalizer, + JobResultsPersister persister, + AnnotationPersister annotationPersister, + AutodetectProcess autodetectProcess, + ModelSizeStats latestModelSizeStats, + TimingStats timingStats, + Clock clock, + FlushListener flushListener + ) { this.client = Objects.requireNonNull(client); this.auditor = Objects.requireNonNull(auditor); this.jobId = Objects.requireNonNull(jobId); @@ -166,7 +189,7 @@ public void process() { failed = true; if (processKilled) { - // Don't log the stack trace in this case. Log just enough to hint + // Don't log the stack trace in this case. Log just enough to hint // that it would have been better to close jobs before shutting down, // but we now fully expect jobs to move between nodes without doing // all their graceful close activities. @@ -217,9 +240,7 @@ public void setProcessKilled() { void handleOpenForecasts() { try { if (runningForecasts.isEmpty() == false) { - LOGGER.warn("[{}] still had forecasts {} executing. Attempting to set them to failed.", - jobId, - runningForecasts.keySet()); + LOGGER.warn("[{}] still had forecasts {} executing. Attempting to set them to failed.", jobId, runningForecasts.keySet()); // There may be many docs in the results persistence queue. But we only want to bother updating the running forecasts bulkResultsPersister.clearBulkRequest(); for (ForecastRequestStats forecastRequestStats : runningForecasts.values()) { @@ -325,7 +346,9 @@ void processResult(AutodetectResult result) { updateModelSnapshotOnJob(modelSnapshot); } bulkAnnotationsPersister.persistAnnotation( - ModelSnapshot.annotationDocumentId(modelSnapshot), createModelSnapshotAnnotation(modelSnapshot)); + ModelSnapshot.annotationDocumentId(modelSnapshot), + createModelSnapshotAnnotation(modelSnapshot) + ); } Quantiles quantiles = result.getQuantiles(); if (quantiles != null) { @@ -355,9 +378,12 @@ void processResult(AutodetectResult result) { LOGGER.debug("[{}] Flush acknowledgement sent to listener for ID {}", jobId, flushAcknowledgement.getId()); } catch (Exception e) { LOGGER.error( - "[" + jobId + "] failed to bulk persist results and commit writes during flush acknowledgement for ID " + - flushAcknowledgement.getId(), - e); + "[" + + jobId + + "] failed to bulk persist results and commit writes during flush acknowledgement for ID " + + flushAcknowledgement.getId(), + e + ); exception = e; throw e; } finally { @@ -373,8 +399,9 @@ void processResult(AutodetectResult result) { private Annotation createModelSnapshotAnnotation(ModelSnapshot modelSnapshot) { assert modelSnapshot != null; Date currentTime = new Date(clock.millis()); - return new Annotation.Builder() - .setAnnotation(Messages.getMessage(Messages.JOB_AUDIT_SNAPSHOT_STORED, modelSnapshot.getSnapshotId())) + return new Annotation.Builder().setAnnotation( + Messages.getMessage(Messages.JOB_AUDIT_SNAPSHOT_STORED, modelSnapshot.getSnapshotId()) + ) .setCreateTime(currentTime) .setCreateUsername(XPackUser.NAME) .setTimestamp(modelSnapshot.getLatestResultTimeStamp()) @@ -388,10 +415,16 @@ private Annotation createModelSnapshotAnnotation(ModelSnapshot modelSnapshot) { } private void processModelSizeStats(ModelSizeStats modelSizeStats) { - LOGGER.trace("[{}] Parsed ModelSizeStats: {} / {} / {} / {} / {} / {}", - jobId, modelSizeStats.getModelBytes(), modelSizeStats.getTotalByFieldCount(), - modelSizeStats.getTotalOverFieldCount(), modelSizeStats.getTotalPartitionFieldCount(), - modelSizeStats.getBucketAllocationFailuresCount(), modelSizeStats.getMemoryStatus()); + LOGGER.trace( + "[{}] Parsed ModelSizeStats: {} / {} / {} / {} / {} / {}", + jobId, + modelSizeStats.getModelBytes(), + modelSizeStats.getTotalByFieldCount(), + modelSizeStats.getTotalOverFieldCount(), + modelSizeStats.getTotalPartitionFieldCount(), + modelSizeStats.getBucketAllocationFailuresCount(), + modelSizeStats.getMemoryStatus() + ); persister.persistModelSizeStats(modelSizeStats, this::isAlive); notifyModelMemoryStatusChange(modelSizeStats); @@ -406,12 +439,22 @@ private void notifyModelMemoryStatusChange(ModelSizeStats modelSizeStats) { auditor.warning(jobId, Messages.getMessage(Messages.JOB_AUDIT_MEMORY_STATUS_SOFT_LIMIT)); } else if (memoryStatus == ModelSizeStats.MemoryStatus.HARD_LIMIT) { if (modelSizeStats.getModelBytesMemoryLimit() == null || modelSizeStats.getModelBytesExceeded() == null) { - auditor.error(jobId, Messages.getMessage(Messages.JOB_AUDIT_MEMORY_STATUS_HARD_LIMIT_PRE_7_2, - ByteSizeValue.ofBytes(modelSizeStats.getModelBytes()).toString())); + auditor.error( + jobId, + Messages.getMessage( + Messages.JOB_AUDIT_MEMORY_STATUS_HARD_LIMIT_PRE_7_2, + ByteSizeValue.ofBytes(modelSizeStats.getModelBytes()).toString() + ) + ); } else { - auditor.error(jobId, Messages.getMessage(Messages.JOB_AUDIT_MEMORY_STATUS_HARD_LIMIT, - ByteSizeValue.ofBytes(modelSizeStats.getModelBytesMemoryLimit()).toString(), - ByteSizeValue.ofBytes(modelSizeStats.getModelBytesExceeded()).toString())); + auditor.error( + jobId, + Messages.getMessage( + Messages.JOB_AUDIT_MEMORY_STATUS_HARD_LIMIT, + ByteSizeValue.ofBytes(modelSizeStats.getModelBytesMemoryLimit()).toString(), + ByteSizeValue.ofBytes(modelSizeStats.getModelBytesExceeded()).toString() + ) + ); } } } @@ -420,8 +463,7 @@ private void notifyModelMemoryStatusChange(ModelSizeStats modelSizeStats) { private void notifyCategorizationStatusChange(Annotation annotation) { if (annotation.getEvent() == Annotation.Event.CATEGORIZATION_STATUS_CHANGE) { long bucketCount = priorRunsBucketCount + currentRunBucketCount; - auditor.warning(jobId, annotation.getAnnotation() + " after " - + bucketCount + ((bucketCount == 1) ? " bucket" : " buckets")); + auditor.warning(jobId, annotation.getAnnotation() + " after " + bucketCount + ((bucketCount == 1) ? " bucket" : " buckets")); } } @@ -450,8 +492,7 @@ public void onResponse(PutJobAction.Response response) { @Override public void onFailure(Exception e) { updateModelSnapshotSemaphore.release(); - LOGGER.error("[" + jobId + "] Failed to update job with new model snapshot id [" + - modelSnapshot.getSnapshotId() + "]", e); + LOGGER.error("[" + jobId + "] Failed to update job with new model snapshot id [" + modelSnapshot.getSnapshotId() + "]", e); } }); } @@ -460,8 +501,7 @@ public void awaitCompletion() throws TimeoutException { try { // Although the results won't take 30 minutes to finish, the pipe won't be closed // until the state is persisted, and that can take a while - if (completionLatch.await(MachineLearningField.STATE_PERSIST_RESTORE_TIMEOUT.getMinutes(), - TimeUnit.MINUTES) == false) { + if (completionLatch.await(MachineLearningField.STATE_PERSIST_RESTORE_TIMEOUT.getMinutes(), TimeUnit.MINUTES) == false) { throw new TimeoutException("Timed out waiting for results processor to complete for job " + jobId); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/output/FlushListener.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/output/FlushListener.java index d992fba2e555c..5f6fe747945ef 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/output/FlushListener.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/output/FlushListener.java @@ -6,8 +6,8 @@ */ package org.elasticsearch.xpack.ml.job.process.autodetect.output; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.util.concurrent.RunOnce; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xpack.core.ml.job.process.autodetect.output.FlushAcknowledgement; import java.time.Duration; diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/output/JobSnapshotUpgraderResultProcessor.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/output/JobSnapshotUpgraderResultProcessor.java index ea880692eac4e..87f264604e9dd 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/output/JobSnapshotUpgraderResultProcessor.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/output/JobSnapshotUpgraderResultProcessor.java @@ -38,7 +38,6 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; - /** * A runnable class that reads the autodetect process output in the * {@link #process()} method and persists parsed @@ -59,10 +58,12 @@ public class JobSnapshotUpgraderResultProcessor { private volatile boolean processKilled; private volatile boolean failed; - public JobSnapshotUpgraderResultProcessor(String jobId, - String snapshotId, - JobResultsPersister persister, - AutodetectProcess autodetectProcess) { + public JobSnapshotUpgraderResultProcessor( + String jobId, + String snapshotId, + JobResultsPersister persister, + AutodetectProcess autodetectProcess + ) { this.jobId = Objects.requireNonNull(jobId); this.snapshotId = Objects.requireNonNull(snapshotId); this.persister = Objects.requireNonNull(persister); @@ -83,32 +84,32 @@ public void process() { bulkResultsPersister.executeRequest(); } } catch (Exception e) { - LOGGER.warn(new ParameterizedMessage( - "[{}] [{}] Error persisting model snapshot upgrade results", jobId, snapshotId), e); + LOGGER.warn(new ParameterizedMessage("[{}] [{}] Error persisting model snapshot upgrade results", jobId, snapshotId), e); } } catch (Exception e) { failed = true; if (processKilled) { - // Don't log the stack trace in this case. Log just enough to hint + // Don't log the stack trace in this case. Log just enough to hint // that it would have been better to close jobs before shutting down, // but we now fully expect jobs to move between nodes without doing // all their graceful close activities. LOGGER.warn( "[{}] [{}] some model snapshot upgrade results not processed due to the process being killed", jobId, - snapshotId); + snapshotId + ); } else if (process.isProcessAliveAfterWaiting() == false) { // Don't log the stack trace to not shadow the root cause. LOGGER.warn( "[{}] [{}] some model snapshot upgrade results not processed due to the termination of autodetect", jobId, - snapshotId); + snapshotId + ); } else { // We should only get here if the iterator throws in which // case parsing the autodetect output has failed. - LOGGER.error(new ParameterizedMessage( - "[{}] [{}] error parsing model snapshot upgrade output", jobId, snapshotId), e); + LOGGER.error(new ParameterizedMessage("[{}] [{}] error parsing model snapshot upgrade output", jobId, snapshotId), e); } } finally { completionLatch.countDown(); @@ -126,9 +127,7 @@ private void readResults() { if (isAlive() == false) { throw e; } - LOGGER.warn( - new ParameterizedMessage("[{}] [{}] Error processing model snapshot upgrade result", jobId, snapshotId), - e); + LOGGER.warn(new ParameterizedMessage("[{}] [{}] Error processing model snapshot upgrade result", jobId, snapshotId), e); } } } finally { @@ -141,13 +140,7 @@ public void setProcessKilled() { } private void logUnexpectedResult(String resultType) { - String msg = "[" - + jobId - + "] [" - + snapshotId - + "] unexpected result read [" - + resultType - + "]"; + String msg = "[" + jobId + "] [" + snapshotId + "] unexpected result read [" + resultType + "]"; // This should never happen, but we definitely want to fail if -ea is provided (e.g. during tests) assert true : msg; LOGGER.info(msg); @@ -241,13 +234,13 @@ public void awaitCompletion() throws TimeoutException { try { // Although the results won't take 30 minutes to finish, the pipe won't be closed // until the state is persisted, and that can take a while - if (completionLatch.await(MachineLearningField.STATE_PERSIST_RESTORE_TIMEOUT.getMinutes(), - TimeUnit.MINUTES) == false) { + if (completionLatch.await(MachineLearningField.STATE_PERSIST_RESTORE_TIMEOUT.getMinutes(), TimeUnit.MINUTES) == false) { throw new TimeoutException( "Timed out waiting for model snapshot upgrader results processor to complete for job " + jobId + " and snapshot " - + snapshotId); + + snapshotId + ); } // These lines ensure that the "completion" we're awaiting includes making the results searchable @@ -275,5 +268,4 @@ private boolean isAlive() { return process.isProcessAliveAfterWaiting(); } - } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/params/AutodetectParams.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/params/AutodetectParams.java index de7f32c6765cb..7bdf49fe600fe 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/params/AutodetectParams.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/params/AutodetectParams.java @@ -34,11 +34,15 @@ public class AutodetectParams { private final Set filters; private final List scheduledEvents; - - private AutodetectParams(DataCounts dataCounts, ModelSizeStats modelSizeStats, TimingStats timingStats, - @Nullable ModelSnapshot modelSnapshot, - @Nullable Quantiles quantiles, Set filters, - List scheduledEvents) { + private AutodetectParams( + DataCounts dataCounts, + ModelSizeStats modelSizeStats, + TimingStats timingStats, + @Nullable ModelSnapshot modelSnapshot, + @Nullable Quantiles quantiles, + Set filters, + List scheduledEvents + ) { this.dataCounts = Objects.requireNonNull(dataCounts); this.modelSizeStats = Objects.requireNonNull(modelSizeStats); this.timingStats = timingStats; @@ -91,12 +95,12 @@ public boolean equals(Object other) { AutodetectParams that = (AutodetectParams) other; return Objects.equals(this.dataCounts, that.dataCounts) - && Objects.equals(this.modelSizeStats, that.modelSizeStats) - && Objects.equals(this.timingStats, that.timingStats) - && Objects.equals(this.modelSnapshot, that.modelSnapshot) - && Objects.equals(this.quantiles, that.quantiles) - && Objects.equals(this.filters, that.filters) - && Objects.equals(this.scheduledEvents, that.scheduledEvents); + && Objects.equals(this.modelSizeStats, that.modelSizeStats) + && Objects.equals(this.timingStats, that.timingStats) + && Objects.equals(this.modelSnapshot, that.modelSnapshot) + && Objects.equals(this.quantiles, that.quantiles) + && Objects.equals(this.filters, that.filters) + && Objects.equals(this.scheduledEvents, that.scheduledEvents); } @Override @@ -161,8 +165,7 @@ public Builder addFilter(MlFilter filter) { } public AutodetectParams build() { - return new AutodetectParams(dataCounts, modelSizeStats, timingStats, modelSnapshot, quantiles, - filters, scheduledEvents); + return new AutodetectParams(dataCounts, modelSizeStats, timingStats, modelSnapshot, quantiles, filters, scheduledEvents); } } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/params/DataLoadParams.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/params/DataLoadParams.java index 7f5474c0593c4..727bf42d6cecc 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/params/DataLoadParams.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/params/DataLoadParams.java @@ -36,4 +36,3 @@ public Optional getDataDescription() { return dataDescription; } } - diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/params/FlushJobParams.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/params/FlushJobParams.java index a61f60dbc80fc..1a81c6079532b 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/params/FlushJobParams.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/params/FlushJobParams.java @@ -8,9 +8,9 @@ import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.Strings; +import org.elasticsearch.xpack.core.common.time.TimeUtils; import org.elasticsearch.xpack.core.ml.job.messages.Messages; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; -import org.elasticsearch.xpack.core.common.time.TimeUtils; import java.util.Objects; @@ -41,11 +41,13 @@ public class FlushJobParams { */ private final boolean waitForNormalization; - private FlushJobParams(boolean calcInterim, - TimeRange timeRange, - Long advanceTimeSeconds, - Long skipTimeSeconds, - boolean waitForNormalization) { + private FlushJobParams( + boolean calcInterim, + TimeRange timeRange, + Long advanceTimeSeconds, + Long skipTimeSeconds, + boolean waitForNormalization + ) { this.calcInterim = calcInterim; this.timeRange = Objects.requireNonNull(timeRange); this.advanceTimeSeconds = advanceTimeSeconds; @@ -100,10 +102,10 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; FlushJobParams that = (FlushJobParams) o; - return calcInterim == that.calcInterim && - Objects.equals(timeRange, that.timeRange) && - Objects.equals(advanceTimeSeconds, that.advanceTimeSeconds) && - Objects.equals(skipTimeSeconds, that.skipTimeSeconds); + return calcInterim == that.calcInterim + && Objects.equals(timeRange, that.timeRange) + && Objects.equals(advanceTimeSeconds, that.advanceTimeSeconds) + && Objects.equals(skipTimeSeconds, that.skipTimeSeconds); } @Override @@ -148,8 +150,9 @@ public FlushJobParams build() { Long advanceTimeSeconds = parseTimeParam("advance_time", advanceTime); Long skipTimeSeconds = parseTimeParam("skip_time", skipTime); if (skipTimeSeconds != null && advanceTimeSeconds != null && advanceTimeSeconds <= skipTimeSeconds) { - throw ExceptionsHelper.badRequestException("advance_time [" + advanceTime + "] must be later than skip_time [" - + skipTime + "]"); + throw ExceptionsHelper.badRequestException( + "advance_time [" + advanceTime + "] must be later than skip_time [" + skipTime + "]" + ); } return new FlushJobParams(calcInterim, timeRange, advanceTimeSeconds, skipTimeSeconds, waitForNormalization); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/params/ForecastParams.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/params/ForecastParams.java index 771cf1906f21c..fa5f7c56afa8a 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/params/ForecastParams.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/params/ForecastParams.java @@ -21,8 +21,15 @@ public class ForecastParams { private final Long maxModelMemory; private final Long minAvailableDiskSpace; - private ForecastParams(String forecastId, long createTime, long duration, long expiresIn, String tmpStorage, Long maxModelMemory, - Long minAvailableDiskSpace) { + private ForecastParams( + String forecastId, + long createTime, + long duration, + long expiresIn, + String tmpStorage, + Long maxModelMemory, + Long minAvailableDiskSpace + ) { this.forecastId = forecastId; this.createTime = createTime; this.duration = duration; @@ -92,12 +99,12 @@ public boolean equals(Object obj) { } ForecastParams other = (ForecastParams) obj; return Objects.equals(forecastId, other.forecastId) - && Objects.equals(createTime, other.createTime) - && Objects.equals(duration, other.duration) - && Objects.equals(expiresIn, other.expiresIn) - && Objects.equals(tmpStorage, other.tmpStorage) - && Objects.equals(maxModelMemory, other.maxModelMemory) - && Objects.equals(minAvailableDiskSpace, other.minAvailableDiskSpace); + && Objects.equals(createTime, other.createTime) + && Objects.equals(duration, other.duration) + && Objects.equals(expiresIn, other.expiresIn) + && Objects.equals(tmpStorage, other.tmpStorage) + && Objects.equals(maxModelMemory, other.maxModelMemory) + && Objects.equals(minAvailableDiskSpace, other.minAvailableDiskSpace); } public static Builder builder() { @@ -148,9 +155,15 @@ public Builder minAvailableDiskSpace(long minAvailableDiskSpace) { } public ForecastParams build() { - return new ForecastParams(forecastId, createTimeEpochSecs, durationSecs, expiresInSecs, tmpStorage, maxModelMemory, - minAvailableDiskSpace); + return new ForecastParams( + forecastId, + createTimeEpochSecs, + durationSecs, + expiresInSecs, + tmpStorage, + maxModelMemory, + minAvailableDiskSpace + ); } } } - diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/params/TimeRange.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/params/TimeRange.java index 88c8d0df93b20..a344b2cd41eb0 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/params/TimeRange.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/params/TimeRange.java @@ -8,9 +8,9 @@ import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.ElasticsearchStatusException; +import org.elasticsearch.xpack.core.common.time.TimeUtils; import org.elasticsearch.xpack.core.ml.job.messages.Messages; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; -import org.elasticsearch.xpack.core.common.time.TimeUtils; import java.util.Objects; @@ -46,8 +46,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; TimeRange timeRange = (TimeRange) o; - return Objects.equals(start, timeRange.start) && - Objects.equals(end, timeRange.end); + return Objects.equals(start, timeRange.start) && Objects.equals(end, timeRange.end); } @Override @@ -60,8 +59,7 @@ public static class Builder { private String start = ""; private String end = ""; - private Builder() { - } + private Builder() {} public Builder startTime(String start) { this.start = ExceptionsHelper.requireNonNull(start, "start"); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/AbstractDataToProcessWriter.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/AbstractDataToProcessWriter.java index 4bacb48dbc1ed..d2a1cea439c1b 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/AbstractDataToProcessWriter.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/AbstractDataToProcessWriter.java @@ -8,9 +8,9 @@ import org.apache.logging.log4j.Logger; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.xpack.ml.job.categorization.CategorizationAnalyzer; import org.elasticsearch.xpack.core.ml.job.config.AnalysisConfig; import org.elasticsearch.xpack.core.ml.job.config.DataDescription; +import org.elasticsearch.xpack.ml.job.categorization.CategorizationAnalyzer; import org.elasticsearch.xpack.ml.job.process.DataCountsReporter; import org.elasticsearch.xpack.ml.job.process.autodetect.AutodetectProcess; import org.elasticsearch.xpack.ml.process.writer.LengthEncodedWriter; @@ -56,9 +56,15 @@ public abstract class AbstractDataToProcessWriter implements DataToProcessWriter private long latestEpochMs; private long latestEpochMsThisUpload; - protected AbstractDataToProcessWriter(boolean includeControlField, boolean includeTokensField, AutodetectProcess autodetectProcess, - DataDescription dataDescription, AnalysisConfig analysisConfig, - DataCountsReporter dataCountsReporter, Logger logger) { + protected AbstractDataToProcessWriter( + boolean includeControlField, + boolean includeTokensField, + AutodetectProcess autodetectProcess, + DataDescription dataDescription, + AnalysisConfig analysisConfig, + DataCountsReporter dataCountsReporter, + Logger logger + ) { this.includeControlField = includeControlField; this.includeTokensField = includeTokensField; this.autodetectProcess = Objects.requireNonNull(autodetectProcess); @@ -128,19 +134,28 @@ public void writeHeader() throws IOException { * @param categorizationFieldValue The value of the categorization field to be tokenized * @param record The record to be sent to the process */ - protected void tokenizeForCategorization(CategorizationAnalyzer categorizationAnalyzer, String categorizationFieldValue, - String[] record) { + protected void tokenizeForCategorization( + CategorizationAnalyzer categorizationAnalyzer, + String categorizationFieldValue, + String[] record + ) { assert includeTokensField; // -2 because last field is the control field, and last but one is the pre-tokenized tokens field - record[record.length - 2] = tokenizeForCategorization(categorizationAnalyzer, analysisConfig.getCategorizationFieldName(), - categorizationFieldValue); + record[record.length - 2] = tokenizeForCategorization( + categorizationAnalyzer, + analysisConfig.getCategorizationFieldName(), + categorizationFieldValue + ); } /** * Accessible for testing only. */ - static String tokenizeForCategorization(CategorizationAnalyzer categorizationAnalyzer, String categorizationFieldName, - String categorizationFieldValue) { + static String tokenizeForCategorization( + CategorizationAnalyzer categorizationAnalyzer, + String categorizationFieldName, + String categorizationFieldValue + ) { StringBuilder builder = new StringBuilder(); boolean first = true; for (String token : categorizationAnalyzer.tokenizeField(categorizationFieldName, categorizationFieldValue)) { @@ -311,8 +326,7 @@ private List createInputOutputMap(Map inFieldIn int outIndex = TIME_FIELD_OUT_INDEX; Integer inIndex = inFieldIndexes.get(dataDescription.getTimeField()); if (inIndex == null) { - throw new IllegalStateException( - String.format(Locale.ROOT, "Input time field '%s' not found", dataDescription.getTimeField())); + throw new IllegalStateException(String.format(Locale.ROOT, "Input time field '%s' not found", dataDescription.getTimeField())); } inputOutputMap.add(new InputOutputMap(inIndex, outIndex)); @@ -340,8 +354,11 @@ protected List getInputOutputMap() { * Every input field should have an entry in inputFieldIndexes * otherwise the field cannot be found. */ - protected abstract boolean checkForMissingFields(Collection inputFields, Map inputFieldIndexes, - String[] header); + protected abstract boolean checkForMissingFields( + Collection inputFields, + Map inputFieldIndexes, + String[] header + ); /** * Input and output array indexes map diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/AutodetectControlMsgWriter.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/AutodetectControlMsgWriter.java index 26c4d2fd7e4b3..7ec932d50ff61 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/AutodetectControlMsgWriter.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/AutodetectControlMsgWriter.java @@ -195,8 +195,7 @@ public void writeResetBucketsMessage(DataLoadParams params) throws IOException { writeControlCodeFollowedByTimeRange(RESET_BUCKETS_MESSAGE_CODE, params.getStart(), params.getEnd()); } - private void writeControlCodeFollowedByTimeRange(String code, String start, String end) - throws IOException { + private void writeControlCodeFollowedByTimeRange(String code, String start, String end) throws IOException { StringBuilder message = new StringBuilder(code); if (start.isEmpty() == false) { message.append(start); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/DataToProcessWriter.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/DataToProcessWriter.java index 91bdd84959430..8f0b015f44ed3 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/DataToProcessWriter.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/DataToProcessWriter.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.ml.job.process.autodetect.writer; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.xpack.ml.job.categorization.CategorizationAnalyzer; import org.elasticsearch.xpack.core.ml.job.process.autodetect.state.DataCounts; +import org.elasticsearch.xpack.ml.job.categorization.CategorizationAnalyzer; import java.io.IOException; import java.io.InputStream; @@ -33,8 +33,12 @@ public interface DataToProcessWriter { * DataDescriptions timeField is missing * a MissingFieldException is thrown */ - void write(InputStream inputStream, CategorizationAnalyzer categorizationAnalyzer, XContentType xContentType, - BiConsumer handler) throws IOException; + void write( + InputStream inputStream, + CategorizationAnalyzer categorizationAnalyzer, + XContentType xContentType, + BiConsumer handler + ) throws IOException; /** * Flush the outputstream diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/JsonDataToProcessWriter.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/JsonDataToProcessWriter.java index 172ea0a8d9da2..d89266a4e7a52 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/JsonDataToProcessWriter.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/JsonDataToProcessWriter.java @@ -13,12 +13,12 @@ import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.xpack.ml.job.categorization.CategorizationAnalyzer; import org.elasticsearch.xpack.core.ml.job.config.AnalysisConfig; import org.elasticsearch.xpack.core.ml.job.config.DataDescription; +import org.elasticsearch.xpack.core.ml.job.process.autodetect.state.DataCounts; +import org.elasticsearch.xpack.ml.job.categorization.CategorizationAnalyzer; import org.elasticsearch.xpack.ml.job.process.DataCountsReporter; import org.elasticsearch.xpack.ml.job.process.autodetect.AutodetectProcess; -import org.elasticsearch.xpack.core.ml.job.process.autodetect.state.DataCounts; import java.io.IOException; import java.io.InputStream; @@ -40,11 +40,16 @@ public class JsonDataToProcessWriter extends AbstractDataToProcessWriter { private static final Logger LOGGER = LogManager.getLogger(JsonDataToProcessWriter.class); private final NamedXContentRegistry xContentRegistry; - public JsonDataToProcessWriter(boolean includeControlField, boolean includeTokensField, AutodetectProcess autodetectProcess, - DataDescription dataDescription, AnalysisConfig analysisConfig, - DataCountsReporter dataCountsReporter, NamedXContentRegistry xContentRegistry) { - super(includeControlField, includeTokensField, autodetectProcess, dataDescription, analysisConfig, - dataCountsReporter, LOGGER); + public JsonDataToProcessWriter( + boolean includeControlField, + boolean includeTokensField, + AutodetectProcess autodetectProcess, + DataDescription dataDescription, + AnalysisConfig analysisConfig, + DataCountsReporter dataCountsReporter, + NamedXContentRegistry xContentRegistry + ) { + super(includeControlField, includeTokensField, autodetectProcess, dataDescription, analysisConfig, dataCountsReporter, LOGGER); this.xContentRegistry = xContentRegistry; } @@ -56,9 +61,12 @@ public JsonDataToProcessWriter(boolean includeControlField, boolean includeToken * timeField is missing from the JSON inputIndex an exception is thrown */ @Override - public void write(InputStream inputStream, CategorizationAnalyzer categorizationAnalyzer, XContentType xContentType, - BiConsumer handler) - throws IOException { + public void write( + InputStream inputStream, + CategorizationAnalyzer categorizationAnalyzer, + XContentType xContentType, + BiConsumer handler + ) throws IOException { dataCountsReporter.startNewIncrementalCount(); if (xContentType.canonical() == XContentType.JSON) { @@ -66,8 +74,7 @@ public void write(InputStream inputStream, CategorizationAnalyzer categorization } else if (xContentType.canonical() == XContentType.SMILE) { writeSmileXContent(categorizationAnalyzer, inputStream); } else { - throw new RuntimeException("XContentType [" + xContentType - + "] is not supported by JsonDataToProcessWriter"); + throw new RuntimeException("XContentType [" + xContentType + "] is not supported by JsonDataToProcessWriter"); } dataCountsReporter.finishReporting(); @@ -75,8 +82,10 @@ public void write(InputStream inputStream, CategorizationAnalyzer categorization } private void writeJsonXContent(CategorizationAnalyzer categorizationAnalyzer, InputStream inputStream) throws IOException { - try (XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, inputStream)) { + try ( + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, inputStream) + ) { writeJson(categorizationAnalyzer, parser); } } @@ -87,8 +96,10 @@ private void writeSmileXContent(CategorizationAnalyzer categorizationAnalyzer, I if (nextObject.length == 0) { break; } - try (XContentParser parser = XContentFactory.xContent(XContentType.SMILE) - .createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, nextObject)) { + try ( + XContentParser parser = XContentFactory.xContent(XContentType.SMILE) + .createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, nextObject) + ) { writeJson(categorizationAnalyzer, parser); } } @@ -96,10 +107,10 @@ private void writeSmileXContent(CategorizationAnalyzer categorizationAnalyzer, I private byte[] findNextObject(byte marker, InputStream data) throws IOException { // The underlying stream, MarkSupportingStreamInputWrapper, doesn't care about - // readlimit, so just set to -1. We could pick a value, but I worry that if the + // readlimit, so just set to -1. We could pick a value, but I worry that if the // underlying implementation changes it may cause strange behavior, whereas -1 should // blow up immediately - assert(data.markSupported()); + assert (data.markSupported()); data.mark(-1); int nextByte; @@ -164,9 +175,7 @@ private void writeJson(CategorizationAnalyzer categorizationAnalyzer, XContentPa * Always returns true */ @Override - protected boolean checkForMissingFields(Collection inputFields, - Map inputFieldIndexes, - String[] header) { + protected boolean checkForMissingFields(Collection inputFields, Map inputFieldIndexes, String[] header) { return true; } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/XContentRecordReader.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/XContentRecordReader.java index f3d319df98574..2643ef69ba895 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/XContentRecordReader.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/XContentRecordReader.java @@ -7,6 +7,7 @@ package org.elasticsearch.xpack.ml.job.process.autodetect.writer; import com.fasterxml.jackson.core.JsonParseException; + import org.apache.logging.log4j.Logger; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.xcontent.XContentParser; @@ -134,7 +135,7 @@ private void parseFieldValuePair(String[] record, boolean[] gotFields) throws IO private String parseSingleFieldValue(XContentParser.Token token) throws IOException { if (token == XContentParser.Token.START_ARRAY) { // Convert any scalar values in the array to a comma delimited - // string. (Arrays of more complex objects are ignored.) + // string. (Arrays of more complex objects are ignored.) StringBuilder strBuilder = new StringBuilder(); boolean needComma = false; while (token != XContentParser.Token.END_ARRAY) { @@ -167,8 +168,7 @@ private void skipSingleFieldValue(XContentParser.Token token) throws IOException } else if (token == XContentParser.Token.START_ARRAY) { ++arrayDepth; } - } - while (token != null && arrayDepth > 0); + } while (token != null && arrayDepth > 0); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/diagnostics/DataStreamDiagnostics.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/diagnostics/DataStreamDiagnostics.java index ed16b905c41e8..4bd4f4178d799 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/diagnostics/DataStreamDiagnostics.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/diagnostics/DataStreamDiagnostics.java @@ -53,8 +53,13 @@ private BucketDiagnostics.BucketFlushListener createBucketFlushListener() { double sparsityScore = logAverageBucketSize - logBucketSize; if (sparsityScore > DATA_SPARSITY_THRESHOLD) { - LOGGER.debug("Sparse bucket {}, this bucket: {} average: {}, sparsity score: {}", flushedBucketStartMs, - flushedBucketCount, averageBucketSize, sparsityScore); + LOGGER.debug( + "Sparse bucket {}, this bucket: {} average: {}, sparsity score: {}", + flushedBucketStartMs, + flushedBucketCount, + averageBucketSize, + sparsityScore + ); ++sparseBucketCount; latestSparseBucketTime = flushedBucketStartMs; } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/BucketInfluencerNormalizable.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/BucketInfluencerNormalizable.java index 2224e0eeac634..732bfdccd4989 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/BucketInfluencerNormalizable.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/BucketInfluencerNormalizable.java @@ -12,7 +12,6 @@ import java.io.IOException; import java.util.Objects; - class BucketInfluencerNormalizable extends AbstractLeafNormalizable { private final BucketInfluencer bucketInfluencer; @@ -28,8 +27,7 @@ public String getId() { @Override public Level getLevel() { - return BucketInfluencer.BUCKET_TIME.equals(bucketInfluencer.getInfluencerFieldName()) ? - Level.ROOT : Level.BUCKET_INFLUENCER; + return BucketInfluencer.BUCKET_TIME.equals(bucketInfluencer.getInfluencerFieldName()) ? Level.ROOT : Level.BUCKET_INFLUENCER; } @Override diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/BucketNormalizable.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/BucketNormalizable.java index a827f5e1f6f50..c35e758564bce 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/BucketNormalizable.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/BucketNormalizable.java @@ -112,9 +112,12 @@ public List getChildren(ChildType type) { List children = new ArrayList<>(); switch (type) { case BUCKET_INFLUENCER: - children.addAll(bucket.getBucketInfluencers().stream() + children.addAll( + bucket.getBucketInfluencers() + .stream() .map(bi -> new BucketInfluencerNormalizable(bi, getOriginatingIndex())) - .collect(Collectors.toList())); + .collect(Collectors.toList()) + ); break; default: throw new IllegalArgumentException("Invalid type: " + type); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/Level.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/Level.java index 1829ff012ba20..2351f0fa4a4ce 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/Level.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/Level.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.xpack.ml.job.process.normalizer; - /** * An enumeration of the different normalization levels. * The string value of each level has to match the equivalent diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/MultiplyingNormalizerProcess.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/MultiplyingNormalizerProcess.java index b5cc04829b1b3..1329c1a4bcd03 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/MultiplyingNormalizerProcess.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/MultiplyingNormalizerProcess.java @@ -86,8 +86,7 @@ public void persistState() { } @Override - public void persistState(long snapshotTimestamp, String snapshotId, String snapshotDescription) { - } + public void persistState(long snapshotTimestamp, String snapshotId, String snapshotDescription) {} @Override public void flushStream() { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/NativeNormalizerProcess.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/NativeNormalizerProcess.java index c0f710c7a708a..74dfe4cc22c5e 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/NativeNormalizerProcess.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/NativeNormalizerProcess.java @@ -40,8 +40,7 @@ public void persistState() { } @Override - public void persistState(long snapshotTimestamp, String snapshotId, String snapshotDescription) { - } + public void persistState(long snapshotTimestamp, String snapshotId, String snapshotDescription) {} @Override public NormalizerResultHandler createNormalizedResultsHandler() { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/NativeNormalizerProcessFactory.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/NativeNormalizerProcessFactory.java index 22e32668598cd..4715a620409c6 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/NativeNormalizerProcessFactory.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/NativeNormalizerProcessFactory.java @@ -9,8 +9,8 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.env.Environment; import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; @@ -43,8 +43,8 @@ public NativeNormalizerProcessFactory(Environment env, NativeController nativeCo this.nodeName = clusterService.getNodeName(); this.counter = new AtomicLong(0); setProcessConnectTimeout(MachineLearning.PROCESS_CONNECT_TIMEOUT.get(env.settings())); - clusterService.getClusterSettings().addSettingsUpdateConsumer(MachineLearning.PROCESS_CONNECT_TIMEOUT, - this::setProcessConnectTimeout); + clusterService.getClusterSettings() + .addSettingsUpdateConsumer(MachineLearning.PROCESS_CONNECT_TIMEOUT, this::setProcessConnectTimeout); } void setProcessConnectTimeout(TimeValue processConnectTimeout) { @@ -52,13 +52,28 @@ void setProcessConnectTimeout(TimeValue processConnectTimeout) { } @Override - public NormalizerProcess createNormalizerProcess(String jobId, String quantilesState, Integer bucketSpan, - ExecutorService executorService) { + public NormalizerProcess createNormalizerProcess( + String jobId, + String quantilesState, + Integer bucketSpan, + ExecutorService executorService + ) { // Since normalize can get run many times in quick succession for the same job the job ID alone is not sufficient to - // guarantee that the normalizer process pipe names are unique. Therefore an increasing counter value is passed as + // guarantee that the normalizer process pipe names are unique. Therefore an increasing counter value is passed as // well as the job ID to ensure uniqueness between calls. - ProcessPipes processPipes = new ProcessPipes(env, NAMED_PIPE_HELPER, processConnectTimeout, NormalizerBuilder.NORMALIZE, - jobId, counter.incrementAndGet(), false, true, true, false, false); + ProcessPipes processPipes = new ProcessPipes( + env, + NAMED_PIPE_HELPER, + processConnectTimeout, + NormalizerBuilder.NORMALIZE, + jobId, + counter.incrementAndGet(), + false, + true, + true, + false, + false + ); createNativeProcess(jobId, quantilesState, processPipes, bucketSpan); NativeNormalizerProcess normalizerProcess = new NativeNormalizerProcess(jobId, nativeController, processPipes); @@ -94,4 +109,3 @@ private void createNativeProcess(String jobId, String quantilesState, ProcessPip } } } - diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/Normalizable.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/Normalizable.java index 6ee4af900c053..b543c9d2533d8 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/Normalizable.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/Normalizable.java @@ -12,7 +12,10 @@ import java.util.Objects; public abstract class Normalizable implements ToXContentObject { - public enum ChildType {BUCKET_INFLUENCER, RECORD} + public enum ChildType { + BUCKET_INFLUENCER, + RECORD + } private final String indexName; private boolean hadBigNormalizedUpdate; diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/Normalizer.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/Normalizer.java index d59a636095af2..29954d1f3ab16 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/Normalizer.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/Normalizer.java @@ -51,10 +51,8 @@ public Normalizer(String jobId, NormalizerProcessFactory processFactory, Executo * @param quantilesState The state to be used to seed the system change * normalizer */ - public void normalize(Integer bucketSpan, - List results, String quantilesState) { - NormalizerProcess process = processFactory.createNormalizerProcess(jobId, quantilesState, bucketSpan, - executorService); + public void normalize(Integer bucketSpan, List results, String quantilesState) { + NormalizerProcess process = processFactory.createNormalizerProcess(jobId, quantilesState, bucketSpan, executorService); NormalizerResultHandler resultsHandler = process.createNormalizedResultsHandler(); Future resultsHandlerFuture = executorService.submit(() -> { try { @@ -65,7 +63,8 @@ public void normalize(Integer bucketSpan, }); try { - process.writeRecord(new String[] { + process.writeRecord( + new String[] { NormalizerResult.LEVEL_FIELD.getPreferredName(), NormalizerResult.PARTITION_FIELD_NAME_FIELD.getPreferredName(), NormalizerResult.PARTITION_FIELD_VALUE_FIELD.getPreferredName(), @@ -74,8 +73,8 @@ public void normalize(Integer bucketSpan, NormalizerResult.FUNCTION_NAME_FIELD.getPreferredName(), NormalizerResult.VALUE_FIELD_NAME_FIELD.getPreferredName(), NormalizerResult.PROBABILITY_FIELD.getPreferredName(), - NormalizerResult.NORMALIZED_SCORE_FIELD.getPreferredName() - }); + NormalizerResult.NORMALIZED_SCORE_FIELD.getPreferredName() } + ); for (Normalizable result : results) { writeNormalizableAndChildrenRecursively(result, process); @@ -101,10 +100,10 @@ public void normalize(Integer bucketSpan, } } - private static void writeNormalizableAndChildrenRecursively(Normalizable normalizable, - NormalizerProcess process) throws IOException { + private static void writeNormalizableAndChildrenRecursively(Normalizable normalizable, NormalizerProcess process) throws IOException { if (normalizable.isContainerOnly() == false) { - process.writeRecord(new String[] { + process.writeRecord( + new String[] { normalizable.getLevel().asString(), Strings.coalesceToEmpty(normalizable.getPartitionFieldName()), Strings.coalesceToEmpty(normalizable.getPartitionFieldValue()), @@ -113,8 +112,8 @@ private static void writeNormalizableAndChildrenRecursively(Normalizable normali Strings.coalesceToEmpty(normalizable.getFunctionName()), Strings.coalesceToEmpty(normalizable.getValueFieldName()), Double.toString(normalizable.getProbability()), - Double.toString(normalizable.getNormalizedScore()) - }); + Double.toString(normalizable.getNormalizedScore()) } + ); } for (Normalizable child : normalizable.getChildren()) { writeNormalizableAndChildrenRecursively(child, process); @@ -124,15 +123,18 @@ private static void writeNormalizableAndChildrenRecursively(Normalizable normali /** * Updates the normalized scores on the results. */ - private void mergeNormalizedScoresIntoResults(List normalizedScores, - List results) { + private void mergeNormalizedScoresIntoResults(List normalizedScores, List results) { Iterator scoresIter = normalizedScores.iterator(); for (Normalizable result : results) { mergeRecursively(scoresIter, null, false, result); } if (scoresIter.hasNext()) { - LOGGER.error("[{}] Unused normalized scores remain after updating all results: {} for {}", - jobId, normalizedScores.size(), results.size()); + LOGGER.error( + "[{}] Unused normalized scores remain after updating all results: {} for {}", + jobId, + normalizedScores.size(), + results.size() + ); } } @@ -145,8 +147,12 @@ private void mergeNormalizedScoresIntoResults(List normalizedS * @param result the result to be updated * @return the effective normalized score of the given result */ - private double mergeRecursively(Iterator scoresIter, Normalizable parent, - boolean parentHadBigChange, Normalizable result) { + private double mergeRecursively( + Iterator scoresIter, + Normalizable parent, + boolean parentHadBigChange, + Normalizable result + ) { boolean hasBigChange = false; if (result.isContainerOnly() == false) { if (scoresIter.hasNext() == false) { @@ -177,9 +183,7 @@ private double mergeRecursively(Iterator scoresIter, Normaliza if (children.isEmpty() == false) { double maxChildrenScore = 0.0; for (Normalizable child : children) { - maxChildrenScore = Math.max( - mergeRecursively(scoresIter, result, hasBigChange, child), - maxChildrenScore); + maxChildrenScore = Math.max(mergeRecursively(scoresIter, result, hasBigChange, child), maxChildrenScore); } hasBigChange |= result.setMaxChildrenScore(childrenType, maxChildrenScore); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/NormalizerResult.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/NormalizerResult.java index cfa50b3c208f7..0c332a2cbe5a1 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/NormalizerResult.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/NormalizerResult.java @@ -6,11 +6,11 @@ */ package org.elasticsearch.xpack.ml.job.process.normalizer; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -34,7 +34,9 @@ public class NormalizerResult implements ToXContentObject, Writeable { static final ParseField NORMALIZED_SCORE_FIELD = new ParseField("normalized_score"); public static final ObjectParser PARSER = new ObjectParser<>( - LEVEL_FIELD.getPreferredName(), NormalizerResult::new); + LEVEL_FIELD.getPreferredName(), + NormalizerResult::new + ); static { PARSER.declareString(NormalizerResult::setLevel, LEVEL_FIELD); @@ -58,8 +60,7 @@ public class NormalizerResult implements ToXContentObject, Writeable { private double probability; private double normalizedScore; - public NormalizerResult() { - } + public NormalizerResult() {} public NormalizerResult(StreamInput in) throws IOException { level = in.readOptionalString(); @@ -176,8 +177,17 @@ public void setNormalizedScore(double normalizedScore) { @Override public int hashCode() { - return Objects.hash(level, partitionFieldName, partitionFieldValue, personFieldName, personFieldValue, - functionName, valueFieldName, probability, normalizedScore); + return Objects.hash( + level, + partitionFieldName, + partitionFieldValue, + personFieldName, + personFieldValue, + functionName, + valueFieldName, + probability, + normalizedScore + ); } /** @@ -193,16 +203,16 @@ public boolean equals(Object other) { return false; } - NormalizerResult that = (NormalizerResult)other; + NormalizerResult that = (NormalizerResult) other; return Objects.equals(this.level, that.level) - && Objects.equals(this.partitionFieldName, that.partitionFieldName) - && Objects.equals(this.partitionFieldValue, that.partitionFieldValue) - && Objects.equals(this.personFieldName, that.personFieldName) - && Objects.equals(this.personFieldValue, that.personFieldValue) - && Objects.equals(this.functionName, that.functionName) - && Objects.equals(this.valueFieldName, that.valueFieldName) - && this.probability == that.probability - && this.normalizedScore == that.normalizedScore; + && Objects.equals(this.partitionFieldName, that.partitionFieldName) + && Objects.equals(this.partitionFieldValue, that.partitionFieldValue) + && Objects.equals(this.personFieldName, that.personFieldName) + && Objects.equals(this.personFieldValue, that.personFieldValue) + && Objects.equals(this.functionName, that.functionName) + && Objects.equals(this.valueFieldName, that.valueFieldName) + && this.probability == that.probability + && this.normalizedScore == that.normalizedScore; } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/RecordNormalizable.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/RecordNormalizable.java index 46a0c8582acc6..10e38dd9911e0 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/RecordNormalizable.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/RecordNormalizable.java @@ -12,7 +12,6 @@ import java.io.IOException; import java.util.Objects; - class RecordNormalizable extends AbstractLeafNormalizable { private final AnomalyRecord record; diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/ScoresUpdater.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/ScoresUpdater.java index c503acc4521f1..ddb1e88e82eaa 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/ScoresUpdater.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/ScoresUpdater.java @@ -13,9 +13,9 @@ import org.elasticsearch.xpack.core.ml.job.results.Bucket; import org.elasticsearch.xpack.core.ml.job.results.Influencer; import org.elasticsearch.xpack.core.ml.job.results.Result; -import org.elasticsearch.xpack.ml.utils.persistence.BatchedDocumentsIterator; import org.elasticsearch.xpack.ml.job.persistence.JobRenormalizedResultsPersister; import org.elasticsearch.xpack.ml.job.persistence.JobResultsProvider; +import org.elasticsearch.xpack.ml.utils.persistence.BatchedDocumentsIterator; import java.util.ArrayList; import java.util.Deque; @@ -51,8 +51,12 @@ public class ScoresUpdater { private long normalizationWindow; private volatile boolean shutdown; - public ScoresUpdater(Job job, JobResultsProvider jobResultsProvider, JobRenormalizedResultsPersister jobRenormalizedResultsPersister, - NormalizerFactory normalizerFactory) { + public ScoresUpdater( + Job job, + JobResultsProvider jobResultsProvider, + JobRenormalizedResultsPersister jobRenormalizedResultsPersister, + NormalizerFactory normalizerFactory + ) { jobId = job.getId(); this.jobResultsProvider = Objects.requireNonNull(jobResultsProvider); updatesPersister = Objects.requireNonNull(jobRenormalizedResultsPersister); @@ -72,8 +76,7 @@ private long getNormalizationWindowOrDefault(Job job) { if (job.getRenormalizationWindowDays() != null) { return job.getRenormalizationWindowDays() * SECONDS_IN_DAY * MILLISECONDS_IN_SECOND; } - return Math.max(DEFAULT_RENORMALIZATION_WINDOW_MS, - DEFAULT_BUCKETS_IN_RENORMALIZATION_WINDOW * bucketSpan * MILLISECONDS_IN_SECOND); + return Math.max(DEFAULT_RENORMALIZATION_WINDOW_MS, DEFAULT_BUCKETS_IN_RENORMALIZATION_WINDOW * bucketSpan * MILLISECONDS_IN_SECOND); } /** @@ -82,7 +85,7 @@ private long getNormalizationWindowOrDefault(Job job) { */ public void update(String quantilesState, long endBucketEpochMs, long windowExtensionMs) { Normalizer normalizer = normalizerFactory.create(jobId); - int[] counts = {0, 0}; + int[] counts = { 0, 0 }; updateBuckets(normalizer, quantilesState, endBucketEpochMs, windowExtensionMs, counts); updateRecords(normalizer, quantilesState, endBucketEpochMs, windowExtensionMs, counts); updateInfluencers(normalizer, quantilesState, endBucketEpochMs, windowExtensionMs, counts); @@ -94,12 +97,10 @@ public void update(String quantilesState, long endBucketEpochMs, long windowExte LOGGER.debug("[{}] Normalization resulted in: {} updates, {} no-ops", jobId, counts[0], counts[1]); } - private void updateBuckets(Normalizer normalizer, String quantilesState, long endBucketEpochMs, - long windowExtensionMs, int[] counts) { - BatchedDocumentsIterator> bucketsIterator = - jobResultsProvider.newBatchedBucketsIterator(jobId) - .timeRange(calcNormalizationWindowStart(endBucketEpochMs, windowExtensionMs), endBucketEpochMs) - .includeInterim(false); + private void updateBuckets(Normalizer normalizer, String quantilesState, long endBucketEpochMs, long windowExtensionMs, int[] counts) { + BatchedDocumentsIterator> bucketsIterator = jobResultsProvider.newBatchedBucketsIterator(jobId) + .timeRange(calcNormalizationWindowStart(endBucketEpochMs, windowExtensionMs), endBucketEpochMs) + .includeInterim(false); List bucketsToRenormalize = new ArrayList<>(); @@ -130,8 +131,12 @@ private long calcNormalizationWindowStart(long endEpochMs, long windowExtensionM return Math.max(0, endEpochMs - normalizationWindow - windowExtensionMs); } - private void normalizeBuckets(Normalizer normalizer, List normalizableBuckets, - String quantilesState, int[] counts) { + private void normalizeBuckets( + Normalizer normalizer, + List normalizableBuckets, + String quantilesState, + int[] counts + ) { normalizer.normalize(bucketSpan, normalizableBuckets, quantilesState); for (BucketNormalizable bucketNormalizable : normalizableBuckets) { @@ -144,11 +149,10 @@ private void normalizeBuckets(Normalizer normalizer, List no } } - private void updateRecords(Normalizer normalizer, String quantilesState, long endBucketEpochMs, - long windowExtensionMs, int[] counts) { + private void updateRecords(Normalizer normalizer, String quantilesState, long endBucketEpochMs, long windowExtensionMs, int[] counts) { BatchedDocumentsIterator> recordsIterator = jobResultsProvider.newBatchedRecordsIterator(jobId) - .timeRange(calcNormalizationWindowStart(endBucketEpochMs, windowExtensionMs), endBucketEpochMs) - .includeInterim(false); + .timeRange(calcNormalizationWindowStart(endBucketEpochMs, windowExtensionMs), endBucketEpochMs) + .includeInterim(false); while (recordsIterator.hasNext() && shutdown == false) { Deque> records = recordsIterator.next(); @@ -159,19 +163,24 @@ private void updateRecords(Normalizer normalizer, String quantilesState, long en LOGGER.debug("[{}] Will renormalize a batch of {} records", jobId, records.size()); List asNormalizables = records.stream() - .map(recordResultIndex -> new RecordNormalizable(recordResultIndex.result, recordResultIndex.index)) - .collect(Collectors.toList()); + .map(recordResultIndex -> new RecordNormalizable(recordResultIndex.result, recordResultIndex.index)) + .collect(Collectors.toList()); normalizer.normalize(bucketSpan, asNormalizables, quantilesState); persistChanged(counts, asNormalizables); } } - private void updateInfluencers(Normalizer normalizer, String quantilesState, long endBucketEpochMs, - long windowExtensionMs, int[] counts) { + private void updateInfluencers( + Normalizer normalizer, + String quantilesState, + long endBucketEpochMs, + long windowExtensionMs, + int[] counts + ) { BatchedDocumentsIterator> influencersIterator = jobResultsProvider.newBatchedInfluencersIterator(jobId) - .timeRange(calcNormalizationWindowStart(endBucketEpochMs, windowExtensionMs), endBucketEpochMs) - .includeInterim(false); + .timeRange(calcNormalizationWindowStart(endBucketEpochMs, windowExtensionMs), endBucketEpochMs) + .includeInterim(false); while (influencersIterator.hasNext() && shutdown == false) { Deque> influencers = influencersIterator.next(); @@ -182,8 +191,8 @@ private void updateInfluencers(Normalizer normalizer, String quantilesState, lon LOGGER.debug("[{}] Will renormalize a batch of {} influencers", jobId, influencers.size()); List asNormalizables = influencers.stream() - .map(influencerResultIndex -> new InfluencerNormalizable(influencerResultIndex.result, influencerResultIndex.index)) - .collect(Collectors.toList()); + .map(influencerResultIndex -> new InfluencerNormalizable(influencerResultIndex.result, influencerResultIndex.index)) + .collect(Collectors.toList()); normalizer.normalize(bucketSpan, asNormalizables, quantilesState); persistChanged(counts, asNormalizables); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/ShortCircuitingRenormalizer.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/ShortCircuitingRenormalizer.java index 573e6a0b0970e..3de12ec5e1038 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/ShortCircuitingRenormalizer.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/ShortCircuitingRenormalizer.java @@ -82,7 +82,7 @@ public void waitUntilIdle() { public void shutdown() { scoresUpdater.shutdown(); // We have to wait until idle to avoid a raft of exceptions as other parts of the - // system are stopped after this method returns. However, shutting down the + // system are stopped after this method returns. However, shutting down the // scoresUpdater first means it won't do all pending work; it will stop as soon // as it can without causing further errors. waitUntilIdle(); @@ -96,8 +96,8 @@ private Quantiles getEarliestQuantiles() { private QuantilesWithLatch getLatestQuantilesWithLatchAndClear() { // We discard all but the latest quantiles QuantilesWithLatch latestQuantilesWithLatch = null; - for (QuantilesWithLatch quantilesWithLatch = quantilesDeque.pollFirst(); quantilesWithLatch != null; - quantilesWithLatch = quantilesDeque.pollFirst()) { + for (QuantilesWithLatch quantilesWithLatch = quantilesDeque.pollFirst(); quantilesWithLatch != null; quantilesWithLatch = + quantilesDeque.pollFirst()) { // Count down the latches associated with any discarded quantiles if (latestQuantilesWithLatch != null) { latestQuantilesWithLatch.getLatch().countDown(); @@ -127,8 +127,8 @@ private void forceFinishWork() { synchronized (quantilesDeque) { // We discard all but the earliest quantiles, if they exist QuantilesWithLatch earliestQuantileWithLatch = null; - for (QuantilesWithLatch quantilesWithLatch = quantilesDeque.pollFirst(); quantilesWithLatch != null; - quantilesWithLatch = quantilesDeque.pollFirst()) { + for (QuantilesWithLatch quantilesWithLatch = quantilesDeque.pollFirst(); quantilesWithLatch != null; quantilesWithLatch = + quantilesDeque.pollFirst()) { if (earliestQuantileWithLatch == null) { earliestQuantileWithLatch = quantilesWithLatch; } @@ -146,7 +146,7 @@ private void forceFinishWork() { } private void doRenormalizations() { - // Exit immediately if another normalization is in progress. This means we don't hog threads. + // Exit immediately if another normalization is in progress. This means we don't hog threads. if (tryStartWork() == false) { return; } @@ -174,8 +174,12 @@ private void doRenormalizations() { // over the time ranges implied by all quantiles that were provided. long windowExtensionMs = latestBucketTimeMs - earliestBucketTimeMs; if (windowExtensionMs < 0) { - LOGGER.warn("[{}] Quantiles not supplied in time order - {} after {}", - jobId, latestBucketTimeMs, earliestBucketTimeMs); + LOGGER.warn( + "[{}] Quantiles not supplied in time order - {} after {}", + jobId, + latestBucketTimeMs, + earliestBucketTimeMs + ); windowExtensionMs = 0; } scoresUpdater.update(latestQuantiles.getQuantileState(), latestBucketTimeMs, windowExtensionMs); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/output/NormalizerResultHandler.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/output/NormalizerResultHandler.java index d2ce76d209ade..7c3ac8d6fab79 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/output/NormalizerResultHandler.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/process/normalizer/output/NormalizerResultHandler.java @@ -78,9 +78,10 @@ private BytesReference parseResults(XContent xContent, BytesReference bytesRef) } private void parseResult(XContent xContent, BytesReference bytesRef) throws IOException { - try (InputStream stream = bytesRef.streamInput(); - XContentParser parser = xContent - .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) { + try ( + InputStream stream = bytesRef.streamInput(); + XContentParser parser = xContent.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream) + ) { NormalizerResult result = NormalizerResult.PARSER.apply(parser, null); normalizedResults.add(result); } @@ -95,4 +96,3 @@ private static int findNextMarker(byte marker, BytesReference bytesRef, int from return -1; } } - diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/results/AutodetectResult.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/results/AutodetectResult.java index 1294eb6f2b42e..4099ede5ef1d1 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/results/AutodetectResult.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/results/AutodetectResult.java @@ -6,11 +6,11 @@ */ package org.elasticsearch.xpack.ml.job.results; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -38,29 +38,53 @@ public class AutodetectResult implements ToXContentObject, Writeable { @SuppressWarnings("unchecked") public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - TYPE.getPreferredName(), a -> new AutodetectResult((Bucket) a[0], (List) a[1], (List) a[2], - (Quantiles) a[3], a[4] == null ? null : ((ModelSnapshot.Builder) a[4]).build(), - a[5] == null ? null : ((ModelSizeStats.Builder) a[5]).build(), (ModelPlot) a[6], (Annotation) a[7], - (Forecast) a[8], (ForecastRequestStats) a[9], (CategoryDefinition) a[10], - a[11] == null ? null : ((CategorizerStats.Builder) a[11]).build(), (FlushAcknowledgement) a[12])); + TYPE.getPreferredName(), + a -> new AutodetectResult( + (Bucket) a[0], + (List) a[1], + (List) a[2], + (Quantiles) a[3], + a[4] == null ? null : ((ModelSnapshot.Builder) a[4]).build(), + a[5] == null ? null : ((ModelSizeStats.Builder) a[5]).build(), + (ModelPlot) a[6], + (Annotation) a[7], + (Forecast) a[8], + (ForecastRequestStats) a[9], + (CategoryDefinition) a[10], + a[11] == null ? null : ((CategorizerStats.Builder) a[11]).build(), + (FlushAcknowledgement) a[12] + ) + ); static { PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), Bucket.STRICT_PARSER, Bucket.RESULT_TYPE_FIELD); - PARSER.declareObjectArray(ConstructingObjectParser.optionalConstructorArg(), AnomalyRecord.STRICT_PARSER, - AnomalyRecord.RESULTS_FIELD); + PARSER.declareObjectArray( + ConstructingObjectParser.optionalConstructorArg(), + AnomalyRecord.STRICT_PARSER, + AnomalyRecord.RESULTS_FIELD + ); PARSER.declareObjectArray(ConstructingObjectParser.optionalConstructorArg(), Influencer.LENIENT_PARSER, Influencer.RESULTS_FIELD); PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), Quantiles.STRICT_PARSER, Quantiles.TYPE); PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), ModelSnapshot.STRICT_PARSER, ModelSnapshot.TYPE); - PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), ModelSizeStats.STRICT_PARSER, - ModelSizeStats.RESULT_TYPE_FIELD); + PARSER.declareObject( + ConstructingObjectParser.optionalConstructorArg(), + ModelSizeStats.STRICT_PARSER, + ModelSizeStats.RESULT_TYPE_FIELD + ); PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), ModelPlot.STRICT_PARSER, ModelPlot.RESULTS_FIELD); PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), Annotation::fromXContent, Annotation.RESULTS_FIELD); PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), Forecast.STRICT_PARSER, Forecast.RESULTS_FIELD); - PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), ForecastRequestStats.STRICT_PARSER, - ForecastRequestStats.RESULTS_FIELD); + PARSER.declareObject( + ConstructingObjectParser.optionalConstructorArg(), + ForecastRequestStats.STRICT_PARSER, + ForecastRequestStats.RESULTS_FIELD + ); PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), CategoryDefinition.STRICT_PARSER, CategoryDefinition.TYPE); - PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), CategorizerStats.STRICT_PARSER, - CategorizerStats.RESULT_TYPE_FIELD); + PARSER.declareObject( + ConstructingObjectParser.optionalConstructorArg(), + CategorizerStats.STRICT_PARSER, + CategorizerStats.RESULT_TYPE_FIELD + ); PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), FlushAcknowledgement.PARSER, FlushAcknowledgement.TYPE); } @@ -78,10 +102,21 @@ public class AutodetectResult implements ToXContentObject, Writeable { private final CategorizerStats categorizerStats; private final FlushAcknowledgement flushAcknowledgement; - public AutodetectResult(Bucket bucket, List records, List influencers, Quantiles quantiles, - ModelSnapshot modelSnapshot, ModelSizeStats modelSizeStats, ModelPlot modelPlot, Annotation annotation, - Forecast forecast, ForecastRequestStats forecastRequestStats, CategoryDefinition categoryDefinition, - CategorizerStats categorizerStats, FlushAcknowledgement flushAcknowledgement) { + public AutodetectResult( + Bucket bucket, + List records, + List influencers, + Quantiles quantiles, + ModelSnapshot modelSnapshot, + ModelSizeStats modelSizeStats, + ModelPlot modelPlot, + Annotation annotation, + Forecast forecast, + ForecastRequestStats forecastRequestStats, + CategoryDefinition categoryDefinition, + CategorizerStats categorizerStats, + FlushAcknowledgement flushAcknowledgement + ) { this.bucket = bucket; this.records = records; this.influencers = influencers; @@ -280,8 +315,21 @@ public FlushAcknowledgement getFlushAcknowledgement() { @Override public int hashCode() { - return Objects.hash(bucket, records, influencers, categoryDefinition, categorizerStats, flushAcknowledgement, modelPlot, annotation, - forecast, forecastRequestStats, modelSizeStats, modelSnapshot, quantiles); + return Objects.hash( + bucket, + records, + influencers, + categoryDefinition, + categorizerStats, + flushAcknowledgement, + modelPlot, + annotation, + forecast, + forecastRequestStats, + modelSizeStats, + modelSnapshot, + quantiles + ); } @Override @@ -293,18 +341,18 @@ public boolean equals(Object obj) { return false; } AutodetectResult other = (AutodetectResult) obj; - return Objects.equals(bucket, other.bucket) && - Objects.equals(records, other.records) && - Objects.equals(influencers, other.influencers) && - Objects.equals(categoryDefinition, other.categoryDefinition) && - Objects.equals(categorizerStats, other.categorizerStats) && - Objects.equals(flushAcknowledgement, other.flushAcknowledgement) && - Objects.equals(modelPlot, other.modelPlot) && - Objects.equals(annotation, other.annotation) && - Objects.equals(forecast, other.forecast) && - Objects.equals(forecastRequestStats, other.forecastRequestStats) && - Objects.equals(modelSizeStats, other.modelSizeStats) && - Objects.equals(modelSnapshot, other.modelSnapshot) && - Objects.equals(quantiles, other.quantiles); + return Objects.equals(bucket, other.bucket) + && Objects.equals(records, other.records) + && Objects.equals(influencers, other.influencers) + && Objects.equals(categoryDefinition, other.categoryDefinition) + && Objects.equals(categorizerStats, other.categorizerStats) + && Objects.equals(flushAcknowledgement, other.flushAcknowledgement) + && Objects.equals(modelPlot, other.modelPlot) + && Objects.equals(annotation, other.annotation) + && Objects.equals(forecast, other.forecast) + && Objects.equals(forecastRequestStats, other.forecastRequestStats) + && Objects.equals(modelSizeStats, other.modelSizeStats) + && Objects.equals(modelSnapshot, other.modelSnapshot) + && Objects.equals(quantiles, other.quantiles); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/retention/AbstractExpiredJobDataRemover.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/retention/AbstractExpiredJobDataRemover.java index cc0045c25ab56..16e9798f9872a 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/retention/AbstractExpiredJobDataRemover.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/retention/AbstractExpiredJobDataRemover.java @@ -39,16 +39,16 @@ protected TaskId getParentTaskId() { } @Override - public void remove(float requestsPerSecond, - ActionListener listener, - BooleanSupplier isTimedOutSupplier) { + public void remove(float requestsPerSecond, ActionListener listener, BooleanSupplier isTimedOutSupplier) { removeData(jobIterator, requestsPerSecond, listener, isTimedOutSupplier); } - private void removeData(Iterator jobIterator, - float requestsPerSecond, - ActionListener listener, - BooleanSupplier isTimedOutSupplier) { + private void removeData( + Iterator jobIterator, + float requestsPerSecond, + ActionListener listener, + BooleanSupplier isTimedOutSupplier + ) { if (jobIterator.hasNext() == false) { listener.onResponse(true); return; @@ -71,18 +71,19 @@ private void removeData(Iterator jobIterator, return; } - calcCutoffEpochMs(job.getId(), retentionDays, ActionListener.wrap( - response -> { - if (response == null) { - removeData(jobIterator, requestsPerSecond, listener, isTimedOutSupplier); - } else { - removeDataBefore(job, requestsPerSecond, response.latestTimeMs, response.cutoffEpochMs, ActionListener.wrap( - r -> removeData(jobIterator, requestsPerSecond, listener, isTimedOutSupplier), - listener::onFailure)); - } - }, - listener::onFailure - )); + calcCutoffEpochMs(job.getId(), retentionDays, ActionListener.wrap(response -> { + if (response == null) { + removeData(jobIterator, requestsPerSecond, listener, isTimedOutSupplier); + } else { + removeDataBefore( + job, + requestsPerSecond, + response.latestTimeMs, + response.cutoffEpochMs, + ActionListener.wrap(r -> removeData(jobIterator, requestsPerSecond, listener, isTimedOutSupplier), listener::onFailure) + ); + } + }, listener::onFailure)); } abstract void calcCutoffEpochMs(String jobId, long retentionDays, ActionListener listener); @@ -131,8 +132,7 @@ public boolean equals(Object other) { return false; } CutoffDetails that = (CutoffDetails) other; - return this.latestTimeMs == that.latestTimeMs && - this.cutoffEpochMs == that.cutoffEpochMs; + return this.latestTimeMs == that.latestTimeMs && this.cutoffEpochMs == that.cutoffEpochMs; } } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/retention/EmptyStateIndexRemover.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/retention/EmptyStateIndexRemover.java index a7414a4bd2d7e..b56c0c26a7ccb 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/retention/EmptyStateIndexRemover.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/retention/EmptyStateIndexRemover.java @@ -42,30 +42,20 @@ public void remove(float requestsPerSec, ActionListener listener, Boole listener.onResponse(false); return; } - getEmptyStateIndices( - ActionListener.wrap( - emptyStateIndices -> { - if (emptyStateIndices.isEmpty()) { - listener.onResponse(true); - return; - } - getCurrentStateIndices( - ActionListener.wrap( - currentStateIndices -> { - Set stateIndicesToRemove = Sets.difference(emptyStateIndices, currentStateIndices); - if (stateIndicesToRemove.isEmpty()) { - listener.onResponse(true); - return; - } - executeDeleteEmptyStateIndices(stateIndicesToRemove, listener); - }, - listener::onFailure - ) - ); - }, - listener::onFailure - ) - ); + getEmptyStateIndices(ActionListener.wrap(emptyStateIndices -> { + if (emptyStateIndices.isEmpty()) { + listener.onResponse(true); + return; + } + getCurrentStateIndices(ActionListener.wrap(currentStateIndices -> { + Set stateIndicesToRemove = Sets.difference(emptyStateIndices, currentStateIndices); + if (stateIndicesToRemove.isEmpty()) { + listener.onResponse(true); + return; + } + executeDeleteEmptyStateIndices(stateIndicesToRemove, listener); + }, listener::onFailure)); + }, listener::onFailure)); } catch (Exception e) { listener.onFailure(e); } @@ -74,43 +64,36 @@ public void remove(float requestsPerSec, ActionListener listener, Boole private void getEmptyStateIndices(ActionListener> listener) { IndicesStatsRequest indicesStatsRequest = new IndicesStatsRequest().indices(AnomalyDetectorsIndex.jobStateIndexPattern()); indicesStatsRequest.setParentTask(parentTaskId); - client.admin().indices().stats( - indicesStatsRequest, - ActionListener.wrap( - indicesStatsResponse -> { - Set emptyStateIndices = - indicesStatsResponse.getIndices().values().stream() - .filter(stats -> stats.getTotal().getDocs().getCount() == 0) - .map(IndexStats::getIndex) - .collect(toSet()); - listener.onResponse(emptyStateIndices); - }, - listener::onFailure - ) - ); + client.admin().indices().stats(indicesStatsRequest, ActionListener.wrap(indicesStatsResponse -> { + Set emptyStateIndices = indicesStatsResponse.getIndices() + .values() + .stream() + .filter(stats -> stats.getTotal().getDocs().getCount() == 0) + .map(IndexStats::getIndex) + .collect(toSet()); + listener.onResponse(emptyStateIndices); + }, listener::onFailure)); } private void getCurrentStateIndices(ActionListener> listener) { GetIndexRequest getIndexRequest = new GetIndexRequest().indices(AnomalyDetectorsIndex.jobStateIndexWriteAlias()); getIndexRequest.setParentTask(parentTaskId); - client.admin().indices().getIndex( - getIndexRequest, - ActionListener.wrap( - getIndexResponse -> listener.onResponse(Set.of(getIndexResponse.getIndices())), - listener::onFailure - ) - ); + client.admin() + .indices() + .getIndex( + getIndexRequest, + ActionListener.wrap(getIndexResponse -> listener.onResponse(Set.of(getIndexResponse.getIndices())), listener::onFailure) + ); } private void executeDeleteEmptyStateIndices(Set emptyStateIndices, ActionListener listener) { DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(emptyStateIndices.toArray(new String[0])); deleteIndexRequest.setParentTask(parentTaskId); - client.admin().indices().delete( - deleteIndexRequest, - ActionListener.wrap( - deleteResponse -> listener.onResponse(deleteResponse.isAcknowledged()), - listener::onFailure - ) - ); + client.admin() + .indices() + .delete( + deleteIndexRequest, + ActionListener.wrap(deleteResponse -> listener.onResponse(deleteResponse.isAcknowledged()), listener::onFailure) + ); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/retention/ExpiredAnnotationsRemover.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/retention/ExpiredAnnotationsRemover.java index 4df014cef6aac..7539d3928df10 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/retention/ExpiredAnnotationsRemover.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/retention/ExpiredAnnotationsRemover.java @@ -54,8 +54,13 @@ public class ExpiredAnnotationsRemover extends AbstractExpiredJobDataRemover { private final AnomalyDetectionAuditor auditor; private final ThreadPool threadPool; - public ExpiredAnnotationsRemover(OriginSettingClient client, Iterator jobIterator, TaskId parentTaskId, - AnomalyDetectionAuditor auditor, ThreadPool threadPool) { + public ExpiredAnnotationsRemover( + OriginSettingClient client, + Iterator jobIterator, + TaskId parentTaskId, + AnomalyDetectionAuditor auditor, + ThreadPool threadPool + ) { super(client, jobIterator, parentTaskId); this.auditor = Objects.requireNonNull(auditor); this.threadPool = Objects.requireNonNull(threadPool); @@ -105,8 +110,9 @@ private static DeleteByQueryRequest createDBQRequest(Job job, float requestsPerS .filter(QueryBuilders.termQuery(Job.ID.getPreferredName(), job.getId())) .filter(QueryBuilders.rangeQuery(Annotation.TIMESTAMP.getPreferredName()).lt(cutoffEpochMs).format("epoch_millis")) .filter(QueryBuilders.termQuery(Annotation.CREATE_USERNAME.getPreferredName(), XPackUser.NAME)); - DeleteByQueryRequest request = new DeleteByQueryRequest(AnnotationIndex.READ_ALIAS_NAME) - .setSlices(AbstractBulkByScrollRequest.AUTO_SLICES) + DeleteByQueryRequest request = new DeleteByQueryRequest(AnnotationIndex.READ_ALIAS_NAME).setSlices( + AbstractBulkByScrollRequest.AUTO_SLICES + ) .setBatchSize(AbstractBulkByScrollRequest.DEFAULT_SCROLL_SIZE) // We are deleting old data, we should simply proceed as a version conflict could mean that another deletion is taking place .setAbortOnVersionConflict(false) @@ -118,19 +124,21 @@ private static DeleteByQueryRequest createDBQRequest(Job job, float requestsPerS @Override void calcCutoffEpochMs(String jobId, long retentionDays, ActionListener listener) { - ThreadedActionListener threadedActionListener = new ThreadedActionListener<>(LOGGER, threadPool, - MachineLearning.UTILITY_THREAD_POOL_NAME, listener, false); - latestBucketTime(client, getParentTaskId(), jobId, ActionListener.wrap( - latestTime -> { - if (latestTime == null) { - threadedActionListener.onResponse(null); - } else { - long cutoff = latestTime - new TimeValue(retentionDays, TimeUnit.DAYS).getMillis(); - threadedActionListener.onResponse(new CutoffDetails(latestTime, cutoff)); - } - }, - listener::onFailure - )); + ThreadedActionListener threadedActionListener = new ThreadedActionListener<>( + LOGGER, + threadPool, + MachineLearning.UTILITY_THREAD_POOL_NAME, + listener, + false + ); + latestBucketTime(client, getParentTaskId(), jobId, ActionListener.wrap(latestTime -> { + if (latestTime == null) { + threadedActionListener.onResponse(null); + } else { + long cutoff = latestTime - new TimeValue(retentionDays, TimeUnit.DAYS).getMillis(); + threadedActionListener.onResponse(new CutoffDetails(latestTime, cutoff)); + } + }, listener::onFailure)); } private void auditAnnotationsWereDeleted(String jobId, long cutoffEpochMs) { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/retention/ExpiredForecastsRemover.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/retention/ExpiredForecastsRemover.java index a1b4396586436..d93905f0678ec 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/retention/ExpiredForecastsRemover.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/retention/ExpiredForecastsRemover.java @@ -56,7 +56,7 @@ public class ExpiredForecastsRemover implements MlDataRemover { private static final Logger LOGGER = LogManager.getLogger(ExpiredForecastsRemover.class); private static final int MAX_FORECASTS = 10000; - private static final String RESULTS_INDEX_PATTERN = AnomalyDetectorsIndex.jobResultsIndexPrefix() + "*"; + private static final String RESULTS_INDEX_PATTERN = AnomalyDetectorsIndex.jobResultsIndexPrefix() + "*"; private final OriginSettingClient client; private final ThreadPool threadPool; @@ -74,13 +74,16 @@ public ExpiredForecastsRemover(OriginSettingClient client, ThreadPool threadPool public void remove(float requestsPerSec, ActionListener listener, BooleanSupplier isTimedOutSupplier) { LOGGER.debug("Removing forecasts that expire before [{}]", cutoffEpochMs); ActionListener forecastStatsHandler = ActionListener.wrap( - searchResponse -> deleteForecasts(searchResponse, requestsPerSec, listener, isTimedOutSupplier), - e -> listener.onFailure(new ElasticsearchException("An error occurred while searching forecasts to delete", e))); + searchResponse -> deleteForecasts(searchResponse, requestsPerSec, listener, isTimedOutSupplier), + e -> listener.onFailure(new ElasticsearchException("An error occurred while searching forecasts to delete", e)) + ); SearchSourceBuilder source = new SearchSourceBuilder(); - source.query(QueryBuilders.boolQuery() + source.query( + QueryBuilders.boolQuery() .filter(QueryBuilders.termQuery(Result.RESULT_TYPE.getPreferredName(), ForecastRequestStats.RESULT_TYPE_VALUE)) - .filter(QueryBuilders.existsQuery(ForecastRequestStats.EXPIRY_TIME.getPreferredName()))); + .filter(QueryBuilders.existsQuery(ForecastRequestStats.EXPIRY_TIME.getPreferredName())) + ); source.size(MAX_FORECASTS); source.trackTotalHits(true); source.fetchSource(false); @@ -88,15 +91,17 @@ public void remove(float requestsPerSec, ActionListener listener, Boole source.docValueField(ForecastRequestStats.FORECAST_ID.getPreferredName(), null); source.docValueField(ForecastRequestStats.EXPIRY_TIME.getPreferredName(), "epoch_millis"); - // _doc is the most efficient sort order and will also disable scoring source.sort(ElasticsearchMappings.ES_DOC); SearchRequest searchRequest = new SearchRequest(RESULTS_INDEX_PATTERN); searchRequest.source(source); searchRequest.setParentTask(parentTaskId); - client.execute(SearchAction.INSTANCE, searchRequest, new ThreadedActionListener<>(LOGGER, threadPool, - MachineLearning.UTILITY_THREAD_POOL_NAME, forecastStatsHandler, false)); + client.execute( + SearchAction.INSTANCE, + searchRequest, + new ThreadedActionListener<>(LOGGER, threadPool, MachineLearning.UTILITY_THREAD_POOL_NAME, forecastStatsHandler, false) + ); } private void deleteForecasts( @@ -116,8 +121,7 @@ private void deleteForecasts( return; } - DeleteByQueryRequest request = buildDeleteByQuery(forecastsToDelete) - .setRequestsPerSecond(requestsPerSec) + DeleteByQueryRequest request = buildDeleteByQuery(forecastsToDelete).setRequestsPerSecond(requestsPerSec) .setAbortOnVersionConflict(false); request.setParentTask(parentTaskId); client.execute(DeleteByQueryAction.INSTANCE, request, new ActionListener<>() { @@ -125,8 +129,11 @@ private void deleteForecasts( public void onResponse(BulkByScrollResponse bulkByScrollResponse) { try { if (bulkByScrollResponse.getDeleted() > 0) { - LOGGER.info("Deleted [{}] documents corresponding to [{}] expired forecasts", - bulkByScrollResponse.getDeleted(), forecastsToDelete.size()); + LOGGER.info( + "Deleted [{}] documents corresponding to [{}] expired forecasts", + bulkByScrollResponse.getDeleted(), + forecastsToDelete.size() + ); } listener.onResponse(true); } catch (Exception e) { @@ -152,15 +159,19 @@ private List findForecastsToDelete(SearchResponse searchResponse) for (SearchHit hit : hits.getHits()) { String expiryTime = stringFieldValueOrNull(hit, ForecastRequestStats.EXPIRY_TIME.getPreferredName()); if (expiryTime == null) { - LOGGER.warn("Forecast request stats document [{}] has a null [{}] field", hit.getId(), - ForecastRequestStats.EXPIRY_TIME.getPreferredName()); + LOGGER.warn( + "Forecast request stats document [{}] has a null [{}] field", + hit.getId(), + ForecastRequestStats.EXPIRY_TIME.getPreferredName() + ); continue; } long expiryMs = TimeUtils.parseToEpochMs(expiryTime); if (expiryMs < cutoffEpochMs) { JobForecastId idPair = new JobForecastId( stringFieldValueOrNull(hit, Job.ID.getPreferredName()), - stringFieldValueOrNull(hit, Forecast.FORECAST_ID.getPreferredName())); + stringFieldValueOrNull(hit, Forecast.FORECAST_ID.getPreferredName()) + ); if (idPair.hasNullValue() == false) { forecastsToDelete.add(idPair); @@ -179,13 +190,20 @@ private DeleteByQueryRequest buildDeleteByQuery(List ids) { request.indices(RESULTS_INDEX_PATTERN); BoolQueryBuilder boolQuery = QueryBuilders.boolQuery().minimumShouldMatch(1); - boolQuery.must(QueryBuilders.termsQuery(Result.RESULT_TYPE.getPreferredName(), - ForecastRequestStats.RESULT_TYPE_VALUE, Forecast.RESULT_TYPE_VALUE)); + boolQuery.must( + QueryBuilders.termsQuery( + Result.RESULT_TYPE.getPreferredName(), + ForecastRequestStats.RESULT_TYPE_VALUE, + Forecast.RESULT_TYPE_VALUE + ) + ); for (JobForecastId jobForecastId : ids) { if (jobForecastId.hasNullValue() == false) { - boolQuery.should(QueryBuilders.boolQuery() - .must(QueryBuilders.termQuery(Job.ID.getPreferredName(), jobForecastId.jobId)) - .must(QueryBuilders.termQuery(Forecast.FORECAST_ID.getPreferredName(), jobForecastId.forecastId))); + boolQuery.should( + QueryBuilders.boolQuery() + .must(QueryBuilders.termQuery(Job.ID.getPreferredName(), jobForecastId.jobId)) + .must(QueryBuilders.termQuery(Forecast.FORECAST_ID.getPreferredName(), jobForecastId.forecastId)) + ); } } QueryBuilder query = QueryBuilders.boolQuery().filter(boolQuery); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/retention/ExpiredModelSnapshotsRemover.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/retention/ExpiredModelSnapshotsRemover.java index fc05e6d3f2243..290a76b11479e 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/retention/ExpiredModelSnapshotsRemover.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/retention/ExpiredModelSnapshotsRemover.java @@ -70,9 +70,14 @@ public class ExpiredModelSnapshotsRemover extends AbstractExpiredJobDataRemover private final JobResultsProvider jobResultsProvider; private final AnomalyDetectionAuditor auditor; - public ExpiredModelSnapshotsRemover(OriginSettingClient client, Iterator jobIterator, - ThreadPool threadPool, TaskId parentTaskId, JobResultsProvider jobResultsProvider, - AnomalyDetectionAuditor auditor) { + public ExpiredModelSnapshotsRemover( + OriginSettingClient client, + Iterator jobIterator, + ThreadPool threadPool, + TaskId parentTaskId, + JobResultsProvider jobResultsProvider, + AnomalyDetectionAuditor auditor + ) { super(client, jobIterator, parentTaskId); this.threadPool = Objects.requireNonNull(threadPool); this.jobResultsProvider = jobResultsProvider; @@ -93,27 +98,29 @@ Long getRetentionDays(Job job) { @Override void calcCutoffEpochMs(String jobId, long retentionDays, ActionListener listener) { - ThreadedActionListener threadedActionListener = new ThreadedActionListener<>(LOGGER, threadPool, - MachineLearning.UTILITY_THREAD_POOL_NAME, listener, false); - - latestSnapshotTimeStamp(jobId, ActionListener.wrap( - latestTime -> { - if (latestTime == null) { - threadedActionListener.onResponse(null); - } else { - long cutoff = latestTime - new TimeValue(retentionDays, TimeUnit.DAYS).getMillis(); - threadedActionListener.onResponse(new CutoffDetails(latestTime, cutoff)); - } - }, - listener::onFailure - )); + ThreadedActionListener threadedActionListener = new ThreadedActionListener<>( + LOGGER, + threadPool, + MachineLearning.UTILITY_THREAD_POOL_NAME, + listener, + false + ); + + latestSnapshotTimeStamp(jobId, ActionListener.wrap(latestTime -> { + if (latestTime == null) { + threadedActionListener.onResponse(null); + } else { + long cutoff = latestTime - new TimeValue(retentionDays, TimeUnit.DAYS).getMillis(); + threadedActionListener.onResponse(new CutoffDetails(latestTime, cutoff)); + } + }, listener::onFailure)); } private void latestSnapshotTimeStamp(String jobId, ActionListener listener) { SortBuilder sortBuilder = new FieldSortBuilder(ModelSnapshot.TIMESTAMP.getPreferredName()).order(SortOrder.DESC); QueryBuilder snapshotQuery = QueryBuilders.boolQuery() - .filter(QueryBuilders.existsQuery(ModelSnapshot.SNAPSHOT_DOC_COUNT.getPreferredName())) - .filter(QueryBuilders.existsQuery(ModelSnapshot.TIMESTAMP.getPreferredName())); + .filter(QueryBuilders.existsQuery(ModelSnapshot.SNAPSHOT_DOC_COUNT.getPreferredName())) + .filter(QueryBuilders.existsQuery(ModelSnapshot.TIMESTAMP.getPreferredName())); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.sort(sortBuilder); @@ -129,25 +136,22 @@ private void latestSnapshotTimeStamp(String jobId, ActionListener listener searchRequest.indicesOptions(MlIndicesUtils.addIgnoreUnavailable(SearchRequest.DEFAULT_INDICES_OPTIONS)); searchRequest.setParentTask(getParentTaskId()); - client.search(searchRequest, ActionListener.wrap( - response -> { - SearchHit[] hits = response.getHits().getHits(); - if (hits.length == 0) { - // no snapshots found - listener.onResponse(null); - } else { - String timestamp = stringFieldValueOrNull(hits[0], ModelSnapshot.TIMESTAMP.getPreferredName()); - if (timestamp == null) { - LOGGER.warn("Model snapshot document [{}] has a null timestamp field", hits[0].getId()); - listener.onResponse(null); - } else { - long timestampMs = TimeUtils.parseToEpochMs(timestamp); - listener.onResponse(timestampMs); - } - } - }, - listener::onFailure) - ); + client.search(searchRequest, ActionListener.wrap(response -> { + SearchHit[] hits = response.getHits().getHits(); + if (hits.length == 0) { + // no snapshots found + listener.onResponse(null); + } else { + String timestamp = stringFieldValueOrNull(hits[0], ModelSnapshot.TIMESTAMP.getPreferredName()); + if (timestamp == null) { + LOGGER.warn("Model snapshot document [{}] has a null timestamp field", hits[0].getId()); + listener.onResponse(null); + } else { + long timestampMs = TimeUtils.parseToEpochMs(timestamp); + listener.onResponse(timestampMs); + } + } + }, listener::onFailure)); } @Override @@ -164,15 +168,20 @@ protected void removeDataBefore( listener.onResponse(true); return; } - LOGGER.debug(() -> new ParameterizedMessage( - "Considering model snapshots of job [{}] that have a timestamp before [{}] for removal", - job.getId(), - cutoffEpochMs)); + LOGGER.debug( + () -> new ParameterizedMessage( + "Considering model snapshots of job [{}] that have a timestamp before [{}] for removal", + job.getId(), + cutoffEpochMs + ) + ); long deleteAllBeforeMs = (job.getModelSnapshotRetentionDays() == null) - ? 0 : latestTimeMs - TimeValue.timeValueDays(job.getModelSnapshotRetentionDays()).getMillis(); + ? 0 + : latestTimeMs - TimeValue.timeValueDays(job.getModelSnapshotRetentionDays()).getMillis(); ActionListener> snapshotsListener = expiredSnapshotsListener(job, deleteAllBeforeMs, listener); - jobResultsProvider.modelSnapshots(job.getId(), + jobResultsProvider.modelSnapshots( + job.getId(), 0, MODEL_SNAPSHOT_SEARCH_SIZE, null, @@ -181,19 +190,22 @@ protected void removeDataBefore( false, null, snapshotsListener::onResponse, - snapshotsListener::onFailure); + snapshotsListener::onFailure + ); } - private ActionListener> expiredSnapshotsListener(Job job, - long deleteAllBeforeMs, - ActionListener listener) { + private ActionListener> expiredSnapshotsListener( + Job job, + long deleteAllBeforeMs, + ActionListener listener + ) { return new ActionListener<>() { @Override public void onResponse(QueryPage searchResponse) { long nextToKeepMs = deleteAllBeforeMs; try { List snapshots = new ArrayList<>(); - for (ModelSnapshot snapshot: searchResponse.results()) { + for (ModelSnapshot snapshot : searchResponse.results()) { // We don't want to delete the currently used snapshot or a snapshot marked to be retained if (snapshot.getSnapshotId().equals(job.getModelSnapshotId()) || snapshot.isRetain()) { continue; @@ -230,19 +242,18 @@ private void deleteModelSnapshots(List modelSnapshots, String job return; } JobDataDeleter deleter = new JobDataDeleter(client, jobId); - deleter.deleteModelSnapshots(modelSnapshots, ActionListener.wrap( - bulkResponse -> { - auditor.info(jobId, Messages.getMessage(Messages.JOB_AUDIT_SNAPSHOTS_DELETED, modelSnapshots.size())); - LOGGER.debug(() -> new ParameterizedMessage( + deleter.deleteModelSnapshots(modelSnapshots, ActionListener.wrap(bulkResponse -> { + auditor.info(jobId, Messages.getMessage(Messages.JOB_AUDIT_SNAPSHOTS_DELETED, modelSnapshots.size())); + LOGGER.debug( + () -> new ParameterizedMessage( "[{}] deleted model snapshots {} with descriptions {}", jobId, modelSnapshots.stream().map(ModelSnapshot::getSnapshotId).collect(Collectors.toList()), modelSnapshots.stream().map(ModelSnapshot::getDescription).collect(Collectors.toList()) - )); - listener.onResponse(true); - }, - listener::onFailure - )); + ) + ); + listener.onResponse(true); + }, listener::onFailure)); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/retention/ExpiredResultsRemover.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/retention/ExpiredResultsRemover.java index ef1efa7608016..d6bfc07daf0d6 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/retention/ExpiredResultsRemover.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/retention/ExpiredResultsRemover.java @@ -15,10 +15,6 @@ import org.elasticsearch.action.support.ThreadedActionListener; import org.elasticsearch.client.OriginSettingClient; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; @@ -33,6 +29,10 @@ import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.tasks.TaskId; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.ml.job.config.Job; import org.elasticsearch.xpack.core.ml.job.messages.Messages; import org.elasticsearch.xpack.core.ml.job.persistence.AnomalyDetectorsIndex; @@ -71,8 +71,13 @@ public class ExpiredResultsRemover extends AbstractExpiredJobDataRemover { private final AnomalyDetectionAuditor auditor; private final ThreadPool threadPool; - public ExpiredResultsRemover(OriginSettingClient client, Iterator jobIterator, TaskId parentTaskId, - AnomalyDetectionAuditor auditor, ThreadPool threadPool) { + public ExpiredResultsRemover( + OriginSettingClient client, + Iterator jobIterator, + TaskId parentTaskId, + AnomalyDetectionAuditor auditor, + ThreadPool threadPool + ) { super(client, jobIterator, parentTaskId); this.auditor = Objects.requireNonNull(auditor); this.threadPool = Objects.requireNonNull(threadPool); @@ -120,14 +125,16 @@ private DeleteByQueryRequest createDBQRequest(Job job, float requestsPerSec, lon Result.RESULT_TYPE.getPreferredName(), ModelSizeStats.RESULT_TYPE_VALUE, ForecastRequestStats.RESULT_TYPE_VALUE, - Forecast.RESULT_TYPE_VALUE); + Forecast.RESULT_TYPE_VALUE + ); QueryBuilder query = QueryBuilders.boolQuery() .filter(QueryBuilders.termQuery(Job.ID.getPreferredName(), job.getId())) .filter(QueryBuilders.rangeQuery(Result.TIMESTAMP.getPreferredName()).lt(cutoffEpochMs).format("epoch_millis")) .filter(QueryBuilders.existsQuery(Result.RESULT_TYPE.getPreferredName())) .mustNot(excludeFilter); - DeleteByQueryRequest request = new DeleteByQueryRequest(AnomalyDetectorsIndex.jobResultsAliasedName(job.getId())) - .setSlices(AbstractBulkByScrollRequest.AUTO_SLICES) + DeleteByQueryRequest request = new DeleteByQueryRequest(AnomalyDetectorsIndex.jobResultsAliasedName(job.getId())).setSlices( + AbstractBulkByScrollRequest.AUTO_SLICES + ) .setBatchSize(AbstractBulkByScrollRequest.DEFAULT_SCROLL_SIZE) // We are deleting old data, we should simply proceed as a version conflict could mean that another deletion is taking place .setAbortOnVersionConflict(false) @@ -142,19 +149,21 @@ private DeleteByQueryRequest createDBQRequest(Job job, float requestsPerSec, lon @Override void calcCutoffEpochMs(String jobId, long retentionDays, ActionListener listener) { - ThreadedActionListener threadedActionListener = new ThreadedActionListener<>(LOGGER, threadPool, - MachineLearning.UTILITY_THREAD_POOL_NAME, listener, false); - latestBucketTime(client, getParentTaskId(), jobId, ActionListener.wrap( - latestTime -> { - if (latestTime == null) { - threadedActionListener.onResponse(null); - } else { - long cutoff = latestTime - new TimeValue(retentionDays, TimeUnit.DAYS).getMillis(); - threadedActionListener.onResponse(new CutoffDetails(latestTime, cutoff)); - } - }, - listener::onFailure - )); + ThreadedActionListener threadedActionListener = new ThreadedActionListener<>( + LOGGER, + threadPool, + MachineLearning.UTILITY_THREAD_POOL_NAME, + listener, + false + ); + latestBucketTime(client, getParentTaskId(), jobId, ActionListener.wrap(latestTime -> { + if (latestTime == null) { + threadedActionListener.onResponse(null); + } else { + long cutoff = latestTime - new TimeValue(retentionDays, TimeUnit.DAYS).getMillis(); + threadedActionListener.onResponse(new CutoffDetails(latestTime, cutoff)); + } + }, listener::onFailure)); } static void latestBucketTime(OriginSettingClient client, TaskId parentTaskId, String jobId, ActionListener listener) { @@ -173,25 +182,25 @@ static void latestBucketTime(OriginSettingClient client, TaskId parentTaskId, St searchRequest.indicesOptions(MlIndicesUtils.addIgnoreUnavailable(SearchRequest.DEFAULT_INDICES_OPTIONS)); searchRequest.setParentTask(parentTaskId); - client.search(searchRequest, ActionListener.wrap( - response -> { - SearchHit[] hits = response.getHits().getHits(); - if (hits.length == 0) { - // no buckets found - listener.onResponse(null); - } else { - - try (InputStream stream = hits[0].getSourceRef().streamInput(); - XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) { - Bucket bucket = Bucket.LENIENT_PARSER.apply(parser, null); - listener.onResponse(bucket.getTimestamp().getTime()); - } catch (IOException e) { - listener.onFailure(new ElasticsearchParseException("failed to parse bucket", e)); - } - } - }, listener::onFailure - )); + client.search(searchRequest, ActionListener.wrap(response -> { + SearchHit[] hits = response.getHits().getHits(); + if (hits.length == 0) { + // no buckets found + listener.onResponse(null); + } else { + + try ( + InputStream stream = hits[0].getSourceRef().streamInput(); + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream) + ) { + Bucket bucket = Bucket.LENIENT_PARSER.apply(parser, null); + listener.onResponse(bucket.getTimestamp().getTime()); + } catch (IOException e) { + listener.onFailure(new ElasticsearchParseException("failed to parse bucket", e)); + } + } + }, listener::onFailure)); } private void auditResultsWereDeleted(String jobId, long cutoffEpochMs) { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/retention/MlDataRemover.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/retention/MlDataRemover.java index 45630ce68384c..1693daf466312 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/retention/MlDataRemover.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/retention/MlDataRemover.java @@ -30,7 +30,7 @@ default String stringFieldValueOrNull(SearchHit hit, String fieldName) { if (docField != null) { Object value = docField.getValue(); if (value instanceof String) { - return (String)value; + return (String) value; } } return null; diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/retention/UnusedStateRemover.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/retention/UnusedStateRemover.java index 22ea0dbcf9234..c7ccf167aff30 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/retention/UnusedStateRemover.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/retention/UnusedStateRemover.java @@ -78,8 +78,10 @@ public void remove(float requestsPerSec, ActionListener listener, Boole private List findUnusedStateDocIds() { Set jobIds = getJobIds(); List stateDocIdsToDelete = new ArrayList<>(); - BatchedStateDocIdsIterator stateDocIdsIterator = new BatchedStateDocIdsIterator(client, - AnomalyDetectorsIndex.jobStateIndexPattern()); + BatchedStateDocIdsIterator stateDocIdsIterator = new BatchedStateDocIdsIterator( + client, + AnomalyDetectorsIndex.jobStateIndexPattern() + ); while (stateDocIdsIterator.hasNext()) { Deque stateDocIds = stateDocIdsIterator.next(); for (String stateDocId : stateDocIds) { @@ -106,8 +108,11 @@ private Set getJobIds() { private Set getAnomalyDetectionJobIds() { Set jobIds = new HashSet<>(); - DocIdBatchedDocumentIterator iterator = new DocIdBatchedDocumentIterator(client, MlConfigIndex.indexName(), - QueryBuilders.termQuery(Job.JOB_TYPE.getPreferredName(), Job.ANOMALY_DETECTOR_JOB_TYPE)); + DocIdBatchedDocumentIterator iterator = new DocIdBatchedDocumentIterator( + client, + MlConfigIndex.indexName(), + QueryBuilders.termQuery(Job.JOB_TYPE.getPreferredName(), Job.ANOMALY_DETECTOR_JOB_TYPE) + ); while (iterator.hasNext()) { Deque docIds = iterator.next(); docIds.stream().map(Job::extractJobIdFromDocumentId).filter(Objects::nonNull).forEach(jobIds::add); @@ -118,8 +123,11 @@ private Set getAnomalyDetectionJobIds() { private Set getDataFrameAnalyticsJobIds() { Set jobIds = new HashSet<>(); - DocIdBatchedDocumentIterator iterator = new DocIdBatchedDocumentIterator(client, MlConfigIndex.indexName(), - QueryBuilders.termQuery(DataFrameAnalyticsConfig.CONFIG_TYPE.getPreferredName(), DataFrameAnalyticsConfig.TYPE)); + DocIdBatchedDocumentIterator iterator = new DocIdBatchedDocumentIterator( + client, + MlConfigIndex.indexName(), + QueryBuilders.termQuery(DataFrameAnalyticsConfig.CONFIG_TYPE.getPreferredName(), DataFrameAnalyticsConfig.TYPE) + ); while (iterator.hasNext()) { Deque docIds = iterator.next(); docIds.stream().map(DataFrameAnalyticsConfig::extractJobIdFromDocId).filter(Objects::nonNull).forEach(jobIds::add); @@ -128,8 +136,7 @@ private Set getDataFrameAnalyticsJobIds() { } private void executeDeleteUnusedStateDocs(List unusedDocIds, float requestsPerSec, ActionListener listener) { - LOGGER.info("Found [{}] unused state documents; attempting to delete", - unusedDocIds.size()); + LOGGER.info("Found [{}] unused state documents; attempting to delete", unusedDocIds.size()); DeleteByQueryRequest deleteByQueryRequest = new DeleteByQueryRequest(AnomalyDetectorsIndex.jobStateIndexPattern()) .setIndicesOptions(IndicesOptions.lenientExpandOpen()) .setAbortOnVersionConflict(false) @@ -141,22 +148,22 @@ private void executeDeleteUnusedStateDocs(List unusedDocIds, float reque deleteByQueryRequest.getSearchRequest().source().sort(ElasticsearchMappings.ES_DOC); deleteByQueryRequest.setParentTask(parentTaskId); - client.execute(DeleteByQueryAction.INSTANCE, deleteByQueryRequest, ActionListener.wrap( - response -> { - if (response.getBulkFailures().size() > 0 || response.getSearchFailures().size() > 0) { - LOGGER.error("Some unused state documents could not be deleted due to failures: {}", - Strings.collectionToCommaDelimitedString(response.getBulkFailures()) + - "," + Strings.collectionToCommaDelimitedString(response.getSearchFailures())); - } else { - LOGGER.info("Successfully deleted all unused state documents"); - } - listener.onResponse(true); - }, - e -> { - LOGGER.error("Error deleting unused model state documents: ", e); - listener.onFailure(e); + client.execute(DeleteByQueryAction.INSTANCE, deleteByQueryRequest, ActionListener.wrap(response -> { + if (response.getBulkFailures().size() > 0 || response.getSearchFailures().size() > 0) { + LOGGER.error( + "Some unused state documents could not be deleted due to failures: {}", + Strings.collectionToCommaDelimitedString(response.getBulkFailures()) + + "," + + Strings.collectionToCommaDelimitedString(response.getSearchFailures()) + ); + } else { + LOGGER.info("Successfully deleted all unused state documents"); } - )); + listener.onResponse(true); + }, e -> { + LOGGER.error("Error deleting unused model state documents: ", e); + listener.onFailure(e); + })); } private static class JobIdExtractor { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/retention/UnusedStatsRemover.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/retention/UnusedStatsRemover.java index e0ac66708be01..14d92ee84a642 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/retention/UnusedStatsRemover.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/retention/UnusedStatsRemover.java @@ -74,8 +74,11 @@ public void remove(float requestsPerSec, ActionListener listener, Boole private Set getDataFrameAnalyticsJobIds() { Set jobIds = new HashSet<>(); - DocIdBatchedDocumentIterator iterator = new DocIdBatchedDocumentIterator(client, MlConfigIndex.indexName(), - QueryBuilders.termQuery(DataFrameAnalyticsConfig.CONFIG_TYPE.getPreferredName(), DataFrameAnalyticsConfig.TYPE)); + DocIdBatchedDocumentIterator iterator = new DocIdBatchedDocumentIterator( + client, + MlConfigIndex.indexName(), + QueryBuilders.termQuery(DataFrameAnalyticsConfig.CONFIG_TYPE.getPreferredName(), DataFrameAnalyticsConfig.TYPE) + ); while (iterator.hasNext()) { Deque docIds = iterator.next(); docIds.stream().map(DataFrameAnalyticsConfig::extractJobIdFromDocId).filter(Objects::nonNull).forEach(jobIds::add); @@ -86,8 +89,11 @@ private Set getDataFrameAnalyticsJobIds() { private Set getTrainedModelIds() { Set modelIds = new HashSet<>(TrainedModelProvider.MODELS_STORED_AS_RESOURCE); - DocIdBatchedDocumentIterator iterator = new DocIdBatchedDocumentIterator(client, InferenceIndexConstants.INDEX_PATTERN, - QueryBuilders.termQuery(InferenceIndexConstants.DOC_TYPE.getPreferredName(), TrainedModelConfig.NAME)); + DocIdBatchedDocumentIterator iterator = new DocIdBatchedDocumentIterator( + client, + InferenceIndexConstants.INDEX_PATTERN, + QueryBuilders.termQuery(InferenceIndexConstants.DOC_TYPE.getPreferredName(), TrainedModelConfig.NAME) + ); while (iterator.hasNext()) { Deque docIds = iterator.next(); docIds.stream().filter(Objects::nonNull).forEach(modelIds::add); @@ -96,29 +102,26 @@ private Set getTrainedModelIds() { } private void executeDeleteUnusedStatsDocs(QueryBuilder dbq, float requestsPerSec, ActionListener listener) { - DeleteByQueryRequest deleteByQueryRequest = new DeleteByQueryRequest(MlStatsIndex.indexPattern()) - .setIndicesOptions(IndicesOptions.lenientExpandOpen()) - .setAbortOnVersionConflict(false) - .setRequestsPerSecond(requestsPerSec) - .setTimeout(DEFAULT_MAX_DURATION) - .setQuery(dbq); + DeleteByQueryRequest deleteByQueryRequest = new DeleteByQueryRequest(MlStatsIndex.indexPattern()).setIndicesOptions( + IndicesOptions.lenientExpandOpen() + ).setAbortOnVersionConflict(false).setRequestsPerSecond(requestsPerSec).setTimeout(DEFAULT_MAX_DURATION).setQuery(dbq); deleteByQueryRequest.setParentTask(parentTaskId); - client.execute(DeleteByQueryAction.INSTANCE, deleteByQueryRequest, ActionListener.wrap( - response -> { - if (response.getBulkFailures().size() > 0 || response.getSearchFailures().size() > 0) { - LOGGER.error("Some unused stats documents could not be deleted due to failures: {}", - Strings.collectionToCommaDelimitedString(response.getBulkFailures()) + - "," + Strings.collectionToCommaDelimitedString(response.getSearchFailures())); - } else { - LOGGER.info("Successfully deleted [{}] unused stats documents", response.getDeleted()); - } - listener.onResponse(true); - }, - e -> { - LOGGER.error("Error deleting unused model stats documents: ", e); - listener.onFailure(e); + client.execute(DeleteByQueryAction.INSTANCE, deleteByQueryRequest, ActionListener.wrap(response -> { + if (response.getBulkFailures().size() > 0 || response.getSearchFailures().size() > 0) { + LOGGER.error( + "Some unused stats documents could not be deleted due to failures: {}", + Strings.collectionToCommaDelimitedString(response.getBulkFailures()) + + "," + + Strings.collectionToCommaDelimitedString(response.getSearchFailures()) + ); + } else { + LOGGER.info("Successfully deleted [{}] unused stats documents", response.getDeleted()); } - )); + listener.onResponse(true); + }, e -> { + LOGGER.error("Error deleting unused model stats documents: ", e); + listener.onFailure(e); + })); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/snapshot/upgrader/SnapshotUpgradePredicate.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/snapshot/upgrader/SnapshotUpgradePredicate.java index 76412e7986385..4a86136bf47fd 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/snapshot/upgrader/SnapshotUpgradePredicate.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/snapshot/upgrader/SnapshotUpgradePredicate.java @@ -56,9 +56,9 @@ public boolean test(PersistentTasksCustomMetadata.PersistentTask persistentTa return true; } SnapshotUpgradeTaskState snapshotUpgradeTaskState = (SnapshotUpgradeTaskState) persistentTask.getState(); - SnapshotUpgradeState snapshotUpgradeState = snapshotUpgradeTaskState == null ? - SnapshotUpgradeState.STOPPED : - snapshotUpgradeTaskState.getState(); + SnapshotUpgradeState snapshotUpgradeState = snapshotUpgradeTaskState == null + ? SnapshotUpgradeState.STOPPED + : snapshotUpgradeTaskState.getState(); String reason = snapshotUpgradeTaskState == null ? "" : snapshotUpgradeTaskState.getReason(); PersistentTasksCustomMetadata.Assignment assignment = persistentTask.getAssignment(); // This logic is only appropriate when opening a job, not when reallocating following a failure, @@ -71,8 +71,13 @@ public boolean test(PersistentTasksCustomMetadata.PersistentTask persistentTa return true; } if (snapshotUpgradeState == SnapshotUpgradeState.FAILED) { - exception = ExceptionsHelper.serverError("Unexpected state [" + snapshotUpgradeState - + "] while waiting for to be assigned to a node; recorded reason [" + reason + "]"); + exception = ExceptionsHelper.serverError( + "Unexpected state [" + + snapshotUpgradeState + + "] while waiting for to be assigned to a node; recorded reason [" + + reason + + "]" + ); shouldCancel = true; return true; } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/snapshot/upgrader/SnapshotUpgradeTaskExecutor.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/snapshot/upgrader/SnapshotUpgradeTaskExecutor.java index c5b931c829f45..3219039a26a32 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/snapshot/upgrader/SnapshotUpgradeTaskExecutor.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/snapshot/upgrader/SnapshotUpgradeTaskExecutor.java @@ -49,7 +49,6 @@ import java.util.Map; import java.util.Optional; - public class SnapshotUpgradeTaskExecutor extends AbstractJobPersistentTasksExecutor { private static final Logger logger = LogManager.getLogger(SnapshotUpgradeTaskExecutor.class); @@ -60,19 +59,23 @@ public class SnapshotUpgradeTaskExecutor extends AbstractJobPersistentTasksExecu private volatile ClusterState clusterState; private final Client client; - public SnapshotUpgradeTaskExecutor(Settings settings, - ClusterService clusterService, - AutodetectProcessManager autodetectProcessManager, - MlMemoryTracker memoryTracker, - IndexNameExpressionResolver expressionResolver, - Client client, - XPackLicenseState licenseState) { - super(MlTasks.JOB_SNAPSHOT_UPGRADE_TASK_NAME, + public SnapshotUpgradeTaskExecutor( + Settings settings, + ClusterService clusterService, + AutodetectProcessManager autodetectProcessManager, + MlMemoryTracker memoryTracker, + IndexNameExpressionResolver expressionResolver, + Client client, + XPackLicenseState licenseState + ) { + super( + MlTasks.JOB_SNAPSHOT_UPGRADE_TASK_NAME, MachineLearning.UTILITY_THREAD_POOL_NAME, settings, clusterService, memoryTracker, - expressionResolver); + expressionResolver + ); this.autodetectProcessManager = autodetectProcessManager; this.auditor = new AnomalyDetectionAuditor(client, clusterService); this.jobResultsProvider = new JobResultsProvider(client, settings, expressionResolver); @@ -82,12 +85,17 @@ public SnapshotUpgradeTaskExecutor(Settings settings, } @Override - public PersistentTasksCustomMetadata.Assignment getAssignment(SnapshotUpgradeTaskParams params, - Collection candidateNodes, - ClusterState clusterState) { + public PersistentTasksCustomMetadata.Assignment getAssignment( + SnapshotUpgradeTaskParams params, + Collection candidateNodes, + ClusterState clusterState + ) { boolean isMemoryTrackerRecentlyRefreshed = memoryTracker.isRecentlyRefreshed(); - Optional optionalAssignment = - getPotentialAssignment(params, clusterState, isMemoryTrackerRecentlyRefreshed); + Optional optionalAssignment = getPotentialAssignment( + params, + clusterState, + isMemoryTrackerRecentlyRefreshed + ); // NOTE: this will return here if isMemoryTrackerRecentlyRefreshed is false, we don't allow assignment with stale memory if (optionalAssignment.isPresent()) { return optionalAssignment.get(); @@ -100,22 +108,22 @@ public PersistentTasksCustomMetadata.Assignment getAssignment(SnapshotUpgradeTas MlTasks.JOB_TASK_NAME, memoryTracker, 0, - node -> null); + node -> null + ); return jobNodeSelector.selectNode( Integer.MAX_VALUE, Integer.MAX_VALUE, maxMachineMemoryPercent, Long.MAX_VALUE, - useAutoMemoryPercentage); + useAutoMemoryPercentage + ); } @Override protected void nodeOperation(AllocatedPersistentTask task, SnapshotUpgradeTaskParams params, PersistentTaskState state) { SnapshotUpgradeTaskState jobTaskState = (SnapshotUpgradeTaskState) state; SnapshotUpgradeState jobState = jobTaskState == null ? null : jobTaskState.getState(); - logger.info("[{}] [{}] starting to execute task", - params.getJobId(), - params.getSnapshotId()); + logger.info("[{}] [{}] starting to execute task", params.getJobId(), params.getSnapshotId()); // This means that we have loaded the snapshot and possibly snapshot was partially updated // This is no good, we should remove the snapshot @@ -125,39 +133,33 @@ protected void nodeOperation(AllocatedPersistentTask task, SnapshotUpgradeTaskPa } // if the task is failed, that means it was set that way purposefully. So, assuming there is no bad snapshot state if (SnapshotUpgradeState.FAILED.equals(jobState)) { - logger.warn( - "[{}] [{}] upgrade task reassigned to another node while failed", - params.getJobId(), - params.getSnapshotId()); - task.markAsFailed(new ElasticsearchStatusException( - "Task to upgrade job [{}] snapshot [{}] got reassigned while failed. Reason [{}]", - RestStatus.INTERNAL_SERVER_ERROR, - params.getJobId(), - params.getSnapshotId(), - jobTaskState.getReason() == null ? "__unknown__" : jobTaskState.getReason())); + logger.warn("[{}] [{}] upgrade task reassigned to another node while failed", params.getJobId(), params.getSnapshotId()); + task.markAsFailed( + new ElasticsearchStatusException( + "Task to upgrade job [{}] snapshot [{}] got reassigned while failed. Reason [{}]", + RestStatus.INTERNAL_SERVER_ERROR, + params.getJobId(), + params.getSnapshotId(), + jobTaskState.getReason() == null ? "__unknown__" : jobTaskState.getReason() + ) + ); return; } final String jobId = params.getJobId(); final String snapshotId = params.getSnapshotId(); ActionListener stateAliasHandler = ActionListener.wrap( - r -> autodetectProcessManager.upgradeSnapshot((SnapshotUpgradeTask)task, e -> { + r -> autodetectProcessManager.upgradeSnapshot((SnapshotUpgradeTask) task, e -> { if (e == null) { auditor.info(jobId, "Finished upgrading snapshot [" + snapshotId + "]"); logger.info("[{}] [{}] finished upgrading snapshot", jobId, snapshotId); task.markAsCompleted(); } else { - logger.warn( - () -> new ParameterizedMessage( - "[{}] failed upgrading snapshot [{}]", - jobId, - snapshotId), - e); - auditor.warning(jobId, - "failed upgrading snapshot [" - + snapshotId - + "] with exception " - + ExceptionsHelper.unwrapCause(e).getMessage()); + logger.warn(() -> new ParameterizedMessage("[{}] failed upgrading snapshot [{}]", jobId, snapshotId), e); + auditor.warning( + jobId, + "failed upgrading snapshot [" + snapshotId + "] with exception " + ExceptionsHelper.unwrapCause(e).getMessage() + ); task.markAsFailed(e); } }), @@ -166,36 +168,35 @@ protected void nodeOperation(AllocatedPersistentTask task, SnapshotUpgradeTaskPa () -> new ParameterizedMessage( "[{}] failed upgrading snapshot [{}] as ml state alias creation failed", jobId, - snapshotId), - e); - auditor.warning(jobId, - "failed upgrading snapshot [" - + snapshotId - + "] with exception " - + ExceptionsHelper.unwrapCause(e).getMessage()); + snapshotId + ), + e + ); + auditor.warning( + jobId, + "failed upgrading snapshot [" + snapshotId + "] with exception " + ExceptionsHelper.unwrapCause(e).getMessage() + ); // We need to update cluster state so the API caller can be notified and exit // As we have not set the task state to STARTED, it might still be waiting. task.updatePersistentTaskState( new SnapshotUpgradeTaskState(SnapshotUpgradeState.FAILED, -1, e.getMessage()), - ActionListener.wrap( - r -> task.markAsFailed(e), - failure -> { - logger.warn( - new ParameterizedMessage( - "[{}] [{}] failed to set task to failed", - jobId, - snapshotId), - failure); - task.markAsFailed(e); - } - )); + ActionListener.wrap(r -> task.markAsFailed(e), failure -> { + logger.warn(new ParameterizedMessage("[{}] [{}] failed to set task to failed", jobId, snapshotId), failure); + task.markAsFailed(e); + }) + ); } ); // Make sure the state index and alias exist ActionListener resultsMappingUpdateHandler = ActionListener.wrap( - ack -> AnomalyDetectorsIndex.createStateIndexAndAliasIfNecessary(client, clusterState, expressionResolver, - MlTasks.PERSISTENT_TASK_MASTER_NODE_TIMEOUT, stateAliasHandler), + ack -> AnomalyDetectorsIndex.createStateIndexAndAliasIfNecessary( + client, + clusterState, + expressionResolver, + MlTasks.PERSISTENT_TASK_MASTER_NODE_TIMEOUT, + stateAliasHandler + ), task::markAsFailed ); @@ -207,7 +208,8 @@ protected void nodeOperation(AllocatedPersistentTask task, SnapshotUpgradeTaskPa client, clusterState, MlTasks.PERSISTENT_TASK_MASTER_NODE_TIMEOUT, - resultsMappingUpdateHandler), + resultsMappingUpdateHandler + ), e -> { // Due to a bug in 7.9.0 it's possible that the annotations index already has incorrect mappings // and it would cause more harm than good to block jobs from opening in subsequent releases @@ -218,27 +220,39 @@ protected void nodeOperation(AllocatedPersistentTask task, SnapshotUpgradeTaskPa client, clusterState, MlTasks.PERSISTENT_TASK_MASTER_NODE_TIMEOUT, - resultsMappingUpdateHandler); + resultsMappingUpdateHandler + ); } ); // Create the annotations index if necessary - this also updates the mappings if an old mapping is present - AnnotationIndex.createAnnotationsIndexIfNecessaryAndWaitForYellow(client, clusterState, MlTasks.PERSISTENT_TASK_MASTER_NODE_TIMEOUT, - annotationsIndexUpdateHandler); + AnnotationIndex.createAnnotationsIndexIfNecessaryAndWaitForYellow( + client, + clusterState, + MlTasks.PERSISTENT_TASK_MASTER_NODE_TIMEOUT, + annotationsIndexUpdateHandler + ); } @Override - protected AllocatedPersistentTask createTask(long id, String type, String action, TaskId parentTaskId, - PersistentTasksCustomMetadata.PersistentTask persistentTask, - Map headers) { - return new SnapshotUpgradeTask(persistentTask.getParams().getJobId(), + protected AllocatedPersistentTask createTask( + long id, + String type, + String action, + TaskId parentTaskId, + PersistentTasksCustomMetadata.PersistentTask persistentTask, + Map headers + ) { + return new SnapshotUpgradeTask( + persistentTask.getParams().getJobId(), persistentTask.getParams().getSnapshotId(), id, type, action, parentTaskId, headers, - licenseState); + licenseState + ); } @Override @@ -248,11 +262,10 @@ protected boolean allowsMissingIndices() { @Override protected String[] indicesOfInterest(SnapshotUpgradeTaskParams params) { - return new String[]{ + return new String[] { AnomalyDetectorsIndex.jobStateIndexPattern(), MlConfigIndex.indexName(), - AnomalyDetectorsIndex.resultsWriteAlias(params.getJobId()) - }; + AnomalyDetectorsIndex.resultsWriteAlias(params.getJobId()) }; } @Override @@ -261,73 +274,72 @@ protected String getJobId(SnapshotUpgradeTaskParams params) { } private void deleteSnapshotAndFailTask(AllocatedPersistentTask task, String jobId, String snapshotId) { - ActionListener> modelSnapshotListener = ActionListener.wrap( - result -> { - if (result == null) { - task.markAsFailed(new ElasticsearchStatusException( - "Task to upgrade job [{}] snapshot [{}] got reassigned while running leaving an unknown snapshot state. " + - "Snapshot is deleted", + ActionListener> modelSnapshotListener = ActionListener.wrap(result -> { + if (result == null) { + task.markAsFailed( + new ElasticsearchStatusException( + "Task to upgrade job [{}] snapshot [{}] got reassigned while running leaving an unknown snapshot state. " + + "Snapshot is deleted", RestStatus.INTERNAL_SERVER_ERROR, jobId, - snapshotId)); - return; - } - ModelSnapshot snapshot = result.result; - JobDataDeleter jobDataDeleter = new JobDataDeleter(client, jobId); - jobDataDeleter.deleteModelSnapshots(Collections.singletonList(snapshot), ActionListener.wrap( - deleteResponse -> { - auditor.warning( - jobId, - "Task to upgrade snapshot exited in unknown state. Deleted snapshot [" + snapshotId + "]"); - task.markAsFailed(new ElasticsearchStatusException( - "Task to upgrade job [{}] snapshot [{}] got reassigned while running leaving an unknown snapshot state. " + - "Corrupted snapshot deleted", - RestStatus.INTERNAL_SERVER_ERROR, - jobId, - snapshotId)); - }, - failure -> { - logger.warn( - () -> new ParameterizedMessage( - "[{}] [{}] failed to clean up potentially bad snapshot", - jobId, - snapshotId), - failure); - task.markAsFailed(new ElasticsearchStatusException( - "Task to upgrade job [{}] snapshot [{}] got reassigned while running leaving an unknown snapshot state. " + - "Unable to cleanup potentially corrupted snapshot", - RestStatus.INTERNAL_SERVER_ERROR, - jobId, - snapshotId)); - } - )); - }, - e -> { - if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) { - task.markAsFailed(new ElasticsearchStatusException( - "Task to upgrade job [{}] snapshot [{}] got reassigned while running leaving an unknown snapshot state. " + - "Snapshot is deleted", + snapshotId + ) + ); + return; + } + ModelSnapshot snapshot = result.result; + JobDataDeleter jobDataDeleter = new JobDataDeleter(client, jobId); + jobDataDeleter.deleteModelSnapshots(Collections.singletonList(snapshot), ActionListener.wrap(deleteResponse -> { + auditor.warning(jobId, "Task to upgrade snapshot exited in unknown state. Deleted snapshot [" + snapshotId + "]"); + task.markAsFailed( + new ElasticsearchStatusException( + "Task to upgrade job [{}] snapshot [{}] got reassigned while running leaving an unknown snapshot state. " + + "Corrupted snapshot deleted", RestStatus.INTERNAL_SERVER_ERROR, jobId, - snapshotId)); - return; - } + snapshotId + ) + ); + }, failure -> { logger.warn( - () -> new ParameterizedMessage( - "[{}] [{}] failed to load bad snapshot for deletion", + () -> new ParameterizedMessage("[{}] [{}] failed to clean up potentially bad snapshot", jobId, snapshotId), + failure + ); + task.markAsFailed( + new ElasticsearchStatusException( + "Task to upgrade job [{}] snapshot [{}] got reassigned while running leaving an unknown snapshot state. " + + "Unable to cleanup potentially corrupted snapshot", + RestStatus.INTERNAL_SERVER_ERROR, jobId, snapshotId - ), - e); - task.markAsFailed(new ElasticsearchStatusException( - "Task to upgrade job [{}] snapshot [{}] got reassigned while running leaving an unknown snapshot state. " + - "Unable to cleanup potentially corrupted snapshot", + ) + ); + })); + }, e -> { + if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) { + task.markAsFailed( + new ElasticsearchStatusException( + "Task to upgrade job [{}] snapshot [{}] got reassigned while running leaving an unknown snapshot state. " + + "Snapshot is deleted", + RestStatus.INTERNAL_SERVER_ERROR, + jobId, + snapshotId + ) + ); + return; + } + logger.warn(() -> new ParameterizedMessage("[{}] [{}] failed to load bad snapshot for deletion", jobId, snapshotId), e); + task.markAsFailed( + new ElasticsearchStatusException( + "Task to upgrade job [{}] snapshot [{}] got reassigned while running leaving an unknown snapshot state. " + + "Unable to cleanup potentially corrupted snapshot", RestStatus.INTERNAL_SERVER_ERROR, jobId, - snapshotId)); + snapshotId + ) + ); - } - ); + }); jobResultsProvider.getModelSnapshot(jobId, snapshotId, modelSnapshotListener::onResponse, modelSnapshotListener::onFailure); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/snapshot/upgrader/SnapshotUpgradeTaskParams.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/snapshot/upgrader/SnapshotUpgradeTaskParams.java index f05688ddd6d5c..7f3c8914271fb 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/snapshot/upgrader/SnapshotUpgradeTaskParams.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/snapshot/upgrader/SnapshotUpgradeTaskParams.java @@ -8,12 +8,12 @@ package org.elasticsearch.xpack.ml.job.snapshot.upgrader; import org.elasticsearch.Version; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.persistent.PersistentTaskParams; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.persistent.PersistentTaskParams; import org.elasticsearch.xpack.core.ml.job.config.Job; import org.elasticsearch.xpack.core.ml.utils.MlTaskParams; @@ -29,7 +29,8 @@ public class SnapshotUpgradeTaskParams implements PersistentTaskParams, MlTaskPa public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( JOB_SNAPSHOT_UPGRADE_TASK_NAME, true, - a -> new SnapshotUpgradeTaskParams((String) a[0], (String) a[1])); + a -> new SnapshotUpgradeTaskParams((String) a[0], (String) a[1]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), Job.ID); @@ -89,8 +90,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; SnapshotUpgradeTaskParams params = (SnapshotUpgradeTaskParams) o; - return Objects.equals(jobId, params.jobId) && - Objects.equals(snapshotId, params.snapshotId); + return Objects.equals(jobId, params.jobId) && Objects.equals(snapshotId, params.snapshotId); } @Override @@ -103,5 +103,3 @@ public String getMlId() { return jobId; } } - - diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/task/JobTask.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/task/JobTask.java index 0cf287eebd4f3..5857992ed0c80 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/task/JobTask.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/task/JobTask.java @@ -26,7 +26,9 @@ public class JobTask extends LicensedAllocatedPersistentTask implements OpenJobA * We should only progress forwards through these states: close takes precedence over vacate */ enum ClosingOrVacating { - NEITHER, VACATING, CLOSING + NEITHER, + VACATING, + CLOSING } private static final Logger logger = LogManager.getLogger(JobTask.class); @@ -75,7 +77,7 @@ public boolean isVacating() { public void closeJob(String reason) { // If a job is vacating the node when a close request arrives, convert that vacate to a close. // This may be too late, if the vacate operation has already gone past the point of unassigning - // the persistent task instead of completing it. But in general a close should take precedence + // the persistent task instead of completing it. But in general a close should take precedence // over a vacate. if (closingOrVacating.getAndSet(ClosingOrVacating.CLOSING) == ClosingOrVacating.VACATING) { logger.info("[{}] Close request for job while it was vacating the node", jobId); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/task/OpenJobPersistentTasksExecutor.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/task/OpenJobPersistentTasksExecutor.java index 5c55be11b5e09..ec6eaa8d72e26 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/task/OpenJobPersistentTasksExecutor.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/task/OpenJobPersistentTasksExecutor.java @@ -81,11 +81,13 @@ public class OpenJobPersistentTasksExecutor extends AbstractJobPersistentTasksEx public static String[] indicesOfInterest(String resultsIndex) { if (resultsIndex == null) { - return new String[]{AnomalyDetectorsIndex.jobStateIndexPattern(), MlMetaIndex.indexName(), - MlConfigIndex.indexName()}; + return new String[] { AnomalyDetectorsIndex.jobStateIndexPattern(), MlMetaIndex.indexName(), MlConfigIndex.indexName() }; } - return new String[]{AnomalyDetectorsIndex.jobStateIndexPattern(), resultsIndex, MlMetaIndex.indexName(), - MlConfigIndex.indexName()}; + return new String[] { + AnomalyDetectorsIndex.jobStateIndexPattern(), + resultsIndex, + MlMetaIndex.indexName(), + MlConfigIndex.indexName() }; } private final AutodetectProcessManager autodetectProcessManager; @@ -97,14 +99,16 @@ public static String[] indicesOfInterest(String resultsIndex) { private volatile ClusterState clusterState; - public OpenJobPersistentTasksExecutor(Settings settings, - ClusterService clusterService, - AutodetectProcessManager autodetectProcessManager, - DatafeedConfigProvider datafeedConfigProvider, - MlMemoryTracker memoryTracker, - Client client, - IndexNameExpressionResolver expressionResolver, - XPackLicenseState licenseState) { + public OpenJobPersistentTasksExecutor( + Settings settings, + ClusterService clusterService, + AutodetectProcessManager autodetectProcessManager, + DatafeedConfigProvider datafeedConfigProvider, + MlMemoryTracker memoryTracker, + Client client, + IndexNameExpressionResolver expressionResolver, + XPackLicenseState licenseState + ) { super(MlTasks.JOB_TASK_NAME, MachineLearning.UTILITY_THREAD_POOL_NAME, settings, clusterService, memoryTracker, expressionResolver); this.autodetectProcessManager = Objects.requireNonNull(autodetectProcessManager); this.datafeedConfigProvider = Objects.requireNonNull(datafeedConfigProvider); @@ -130,14 +134,22 @@ public Assignment getAssignment(OpenJobAction.JobParams params, Collection nodeFilter(node, job)); + JobNodeSelector jobNodeSelector = new JobNodeSelector( + clusterState, + candidateNodes, + params.getJobId(), + MlTasks.JOB_TASK_NAME, + memoryTracker, + job.allowLazyOpen() ? Integer.MAX_VALUE : maxLazyMLNodes, + node -> nodeFilter(node, job) + ); Assignment assignment = jobNodeSelector.selectNode( maxOpenJobs, maxConcurrentJobAllocations, maxMachineMemoryPercent, maxNodeMemory, - useAutoMemoryPercentage); + useAutoMemoryPercentage + ); auditRequireMemoryIfNecessary(params.getJobId(), auditor, assignment, jobNodeSelector, isMemoryTrackerRecentlyRefreshed); return assignment; } @@ -156,14 +168,23 @@ public static String nodeFilter(DiscoveryNode node, Job job) { String jobId = job.getId(); if (nodeSupportsModelSnapshotVersion(node, job) == false) { - return "Not opening job [" + jobId + "] on node [" + JobNodeSelector.nodeNameAndVersion(node) + return "Not opening job [" + + jobId + + "] on node [" + + JobNodeSelector.nodeNameAndVersion(node) + "], because the job's model snapshot requires a node of version [" - + job.getModelSnapshotMinVersion() + "] or higher"; + + job.getModelSnapshotMinVersion() + + "] or higher"; } if (Job.getCompatibleJobTypes(node.getVersion()).contains(job.getJobType()) == false) { - return "Not opening job [" + jobId + "] on node [" + JobNodeSelector.nodeNameAndVersion(node) + - "], because this node does not support jobs of type [" + job.getJobType() + "]"; + return "Not opening job [" + + jobId + + "] on node [" + + JobNodeSelector.nodeNameAndVersion(node) + + "], because this node does not support jobs of type [" + + job.getJobType() + + "]"; } return null; @@ -174,13 +195,17 @@ static void validateJobAndId(String jobId, Job job) { throw ExceptionsHelper.missingJobException(jobId); } if (job.getBlocked().getReason() != Blocked.Reason.NONE) { - throw ExceptionsHelper.conflictStatusException("Cannot open job [{}] because it is executing [{}]", jobId, - job.getBlocked().getReason()); + throw ExceptionsHelper.conflictStatusException( + "Cannot open job [{}] because it is executing [{}]", + jobId, + job.getBlocked().getReason() + ); } if (job.getJobVersion() == null) { throw ExceptionsHelper.badRequestException( "Cannot open job [{}] because jobs created prior to version 5.5 are not supported", - jobId); + jobId + ); } } @@ -203,7 +228,7 @@ public void validate(OpenJobAction.JobParams params, ClusterState clusterState) @Override // Exceptions that occur while the node is dying, i.e. after the JVM has received a SIGTERM, - // are ignored. Core services will be stopping in response to the SIGTERM and we want the + // are ignored. Core services will be stopping in response to the SIGTERM and we want the // job to try to open again on another node, not spuriously fail on the dying node. protected void nodeOperation(AllocatedPersistentTask task, OpenJobAction.JobParams params, PersistentTaskState state) { JobTask jobTask = (JobTask) task; @@ -211,15 +236,15 @@ protected void nodeOperation(AllocatedPersistentTask task, OpenJobAction.JobPara JobTaskState jobTaskState = (JobTaskState) state; JobState jobState = jobTaskState == null ? null : jobTaskState.getState(); ActionListener checkSnapshotVersionListener = ActionListener.wrap( - mappingsUpdate -> jobResultsProvider.setRunningForecastsToFailed(params.getJobId(), ActionListener.wrap( - r -> runJob(jobTask, jobState, params), - e -> { + mappingsUpdate -> jobResultsProvider.setRunningForecastsToFailed( + params.getJobId(), + ActionListener.wrap(r -> runJob(jobTask, jobState, params), e -> { if (autodetectProcessManager.isNodeDying() == false) { logger.warn(new ParameterizedMessage("[{}] failed to set forecasts to failed", params.getJobId()), e); runJob(jobTask, jobState, params); } - } - )), + }) + ), e -> { if (autodetectProcessManager.isNodeDying() == false) { logger.error(new ParameterizedMessage("[{}] Failed verifying snapshot version", params.getJobId()), e); @@ -246,11 +271,12 @@ protected void nodeOperation(AllocatedPersistentTask task, OpenJobAction.JobPara client, clusterState, PERSISTENT_TASK_MASTER_NODE_TIMEOUT, - resultsMappingUpdateHandler); + resultsMappingUpdateHandler + ); } // Exceptions that occur while the node is dying, i.e. after the JVM has received a SIGTERM, - // are ignored. Core services will be stopping in response to the SIGTERM and we want the + // are ignored. Core services will be stopping in response to the SIGTERM and we want the // job to try to open again on another node, not spuriously fail on the dying node. private void runJob(JobTask jobTask, JobState jobState, OpenJobAction.JobParams params) { // If the node is already running its exit handlers then do nothing - shortly @@ -273,46 +299,48 @@ private void runJob(JobTask jobTask, JobState jobState, OpenJobAction.JobParams return; } - ActionListener hasRunningDatafeedTaskListener = ActionListener.wrap( - hasRunningDatafeed -> { - if (hasRunningDatafeed && isMasterNodeVersionOnOrAfter(MIN_MASTER_NODE_VERSION_FOR_REVERTING_TO_CURRENT_SNAPSHOT)) { - - // This job has a running datafeed attached to it. - // In order to prevent gaps in the model we revert to the current snapshot deleting intervening results. - revertToCurrentSnapshot(jobTask.getJobId(), ActionListener.wrap( - response -> openJob(jobTask), - e -> { - if (autodetectProcessManager.isNodeDying() == false) { - logger.error(new ParameterizedMessage("[{}] failed to revert to current snapshot", jobTask.getJobId()), e); - failTask(jobTask, "failed to revert to current snapshot"); - } - } - )); - } else { - openJob(jobTask); - } - }, - e -> { - if (autodetectProcessManager.isNodeDying() == false) { - logger.error(new ParameterizedMessage("[{}] failed to search for associated datafeed", jobTask.getJobId()), e); - failTask(jobTask, "failed to search for associated datafeed"); - } + ActionListener hasRunningDatafeedTaskListener = ActionListener.wrap(hasRunningDatafeed -> { + if (hasRunningDatafeed && isMasterNodeVersionOnOrAfter(MIN_MASTER_NODE_VERSION_FOR_REVERTING_TO_CURRENT_SNAPSHOT)) { + + // This job has a running datafeed attached to it. + // In order to prevent gaps in the model we revert to the current snapshot deleting intervening results. + revertToCurrentSnapshot(jobTask.getJobId(), ActionListener.wrap(response -> openJob(jobTask), e -> { + if (autodetectProcessManager.isNodeDying() == false) { + logger.error(new ParameterizedMessage("[{}] failed to revert to current snapshot", jobTask.getJobId()), e); + failTask(jobTask, "failed to revert to current snapshot"); + } + })); + } else { + openJob(jobTask); } - ); + }, e -> { + if (autodetectProcessManager.isNodeDying() == false) { + logger.error(new ParameterizedMessage("[{}] failed to search for associated datafeed", jobTask.getJobId()), e); + failTask(jobTask, "failed to search for associated datafeed"); + } + }); hasRunningDatafeedTask(jobTask.getJobId(), hasRunningDatafeedTaskListener); } private void failTask(JobTask jobTask, String reason) { JobTaskState failedState = new JobTaskState(JobState.FAILED, jobTask.getAllocationId(), reason); - jobTask.updatePersistentTaskState(failedState, ActionListener.wrap( - r -> logger.debug(() -> new ParameterizedMessage("[{}] updated task state to failed", jobTask.getJobId())), - e -> { - logger.error(new ParameterizedMessage("[{}] error while setting task state to failed; marking task as failed", - jobTask.getJobId()), e); - jobTask.markAsFailed(e); - } - )); + jobTask.updatePersistentTaskState( + failedState, + ActionListener.wrap( + r -> logger.debug(() -> new ParameterizedMessage("[{}] updated task state to failed", jobTask.getJobId())), + e -> { + logger.error( + new ParameterizedMessage( + "[{}] error while setting task state to failed; marking task as failed", + jobTask.getJobId() + ), + e + ); + jobTask.markAsFailed(e); + } + ) + ); } private boolean isMasterNodeVersionOnOrAfter(Version version) { @@ -320,115 +348,111 @@ private boolean isMasterNodeVersionOnOrAfter(Version version) { } private void hasRunningDatafeedTask(String jobId, ActionListener listener) { - ActionListener> datafeedListener = ActionListener.wrap( - datafeeds -> { - assert datafeeds.size() <= 1; - if (datafeeds.isEmpty()) { - listener.onResponse(false); - return; - } + ActionListener> datafeedListener = ActionListener.wrap(datafeeds -> { + assert datafeeds.size() <= 1; + if (datafeeds.isEmpty()) { + listener.onResponse(false); + return; + } - String datafeedId = datafeeds.iterator().next(); - PersistentTasksCustomMetadata tasks = clusterState.getMetadata().custom(PersistentTasksCustomMetadata.TYPE); - PersistentTasksCustomMetadata.PersistentTask datafeedTask = MlTasks.getDatafeedTask(datafeedId, tasks); - listener.onResponse(datafeedTask != null); - }, - listener::onFailure - ); + String datafeedId = datafeeds.iterator().next(); + PersistentTasksCustomMetadata tasks = clusterState.getMetadata().custom(PersistentTasksCustomMetadata.TYPE); + PersistentTasksCustomMetadata.PersistentTask datafeedTask = MlTasks.getDatafeedTask(datafeedId, tasks); + listener.onResponse(datafeedTask != null); + }, listener::onFailure); datafeedConfigProvider.findDatafeedIdsForJobIds(Collections.singleton(jobId), datafeedListener); } private void verifyCurrentSnapshotVersion(String jobId, ActionListener listener) { - ActionListener jobListener = ActionListener.wrap( - jobResponse -> { - List jobPage = jobResponse.getResponse().results(); - // We requested a single concrete job so if it didn't exist we would get an error - assert jobPage.size() == 1; - String jobSnapshotId = jobPage.get(0).getModelSnapshotId(); - if (jobSnapshotId == null) { - listener.onResponse(true); - return; - } - executeAsyncWithOrigin( - client, - ML_ORIGIN, - GetModelSnapshotsAction.INSTANCE, - new GetModelSnapshotsAction.Request(jobId, jobSnapshotId), - ActionListener.wrap( - snapshot -> { - if (snapshot.getPage().count() == 0) { - listener.onResponse(true); - return; - } - assert snapshot.getPage().results().size() == 1; - ModelSnapshot snapshotObj = snapshot.getPage().results().get(0); - if (snapshotObj.getMinVersion().onOrAfter(MIN_SUPPORTED_SNAPSHOT_VERSION)) { - listener.onResponse(true); - return; - } - listener.onFailure( - ExceptionsHelper.serverError( - "[{}] job snapshot [{}] has min version before [{}], " + - "please revert to a newer model snapshot or reset the job", - jobId, - jobSnapshotId, - MIN_SUPPORTED_SNAPSHOT_VERSION.toString() - ) - ); - }, - snapshotFailure -> { - if (ExceptionsHelper.unwrapCause(snapshotFailure) instanceof ResourceNotFoundException) { - listener.onResponse(true); - return; - } - listener.onFailure( - ExceptionsHelper.serverError("[{}] failed finding snapshot [{}]", snapshotFailure, jobId, jobSnapshotId) - ); - } - ) - ); - }, - error -> listener.onFailure(ExceptionsHelper.serverError("[{}] error getting job", error, jobId)) - ); + ActionListener jobListener = ActionListener.wrap(jobResponse -> { + List jobPage = jobResponse.getResponse().results(); + // We requested a single concrete job so if it didn't exist we would get an error + assert jobPage.size() == 1; + String jobSnapshotId = jobPage.get(0).getModelSnapshotId(); + if (jobSnapshotId == null) { + listener.onResponse(true); + return; + } + executeAsyncWithOrigin( + client, + ML_ORIGIN, + GetModelSnapshotsAction.INSTANCE, + new GetModelSnapshotsAction.Request(jobId, jobSnapshotId), + ActionListener.wrap(snapshot -> { + if (snapshot.getPage().count() == 0) { + listener.onResponse(true); + return; + } + assert snapshot.getPage().results().size() == 1; + ModelSnapshot snapshotObj = snapshot.getPage().results().get(0); + if (snapshotObj.getMinVersion().onOrAfter(MIN_SUPPORTED_SNAPSHOT_VERSION)) { + listener.onResponse(true); + return; + } + listener.onFailure( + ExceptionsHelper.serverError( + "[{}] job snapshot [{}] has min version before [{}], " + + "please revert to a newer model snapshot or reset the job", + jobId, + jobSnapshotId, + MIN_SUPPORTED_SNAPSHOT_VERSION.toString() + ) + ); + }, snapshotFailure -> { + if (ExceptionsHelper.unwrapCause(snapshotFailure) instanceof ResourceNotFoundException) { + listener.onResponse(true); + return; + } + listener.onFailure( + ExceptionsHelper.serverError("[{}] failed finding snapshot [{}]", snapshotFailure, jobId, jobSnapshotId) + ); + }) + ); + }, error -> listener.onFailure(ExceptionsHelper.serverError("[{}] error getting job", error, jobId))); GetJobsAction.Request request = new GetJobsAction.Request(jobId).masterNodeTimeout(PERSISTENT_TASK_MASTER_NODE_TIMEOUT); executeAsyncWithOrigin(client, ML_ORIGIN, GetJobsAction.INSTANCE, request, jobListener); } private void revertToCurrentSnapshot(String jobId, ActionListener listener) { - ActionListener jobListener = ActionListener.wrap( - jobResponse -> { - List jobPage = jobResponse.getResponse().results(); - // We requested a single concrete job so if it didn't exist we would get an error - assert jobPage.size() == 1; - - String jobSnapshotId = jobPage.get(0).getModelSnapshotId(); - if (jobSnapshotId == null && isMasterNodeVersionOnOrAfter(ResetJobAction.VERSION_INTRODUCED)) { - logger.info("[{}] job has running datafeed task; resetting as no snapshot exists", jobId); - ResetJobAction.Request request = new ResetJobAction.Request(jobId); - request.setSkipJobStateValidation(true); - request.masterNodeTimeout(PERSISTENT_TASK_MASTER_NODE_TIMEOUT); - request.timeout(PERSISTENT_TASK_MASTER_NODE_TIMEOUT); - executeAsyncWithOrigin(client, ML_ORIGIN, ResetJobAction.INSTANCE, request, ActionListener.wrap( - response -> listener.onResponse(true), - listener::onFailure - )); - } else { - logger.info("[{}] job has running datafeed task; reverting to current snapshot", jobId); - RevertModelSnapshotAction.Request request = new RevertModelSnapshotAction.Request(jobId, - jobSnapshotId == null ? ModelSnapshot.EMPTY_SNAPSHOT_ID : jobSnapshotId); - request.setForce(true); - request.setDeleteInterveningResults(true); - request.masterNodeTimeout(PERSISTENT_TASK_MASTER_NODE_TIMEOUT); - request.timeout(PERSISTENT_TASK_MASTER_NODE_TIMEOUT); - executeAsyncWithOrigin(client, ML_ORIGIN, RevertModelSnapshotAction.INSTANCE, request, ActionListener.wrap( - response -> listener.onResponse(true), - listener::onFailure - )); - } - }, - error -> listener.onFailure(ExceptionsHelper.serverError("[{}] error getting job", error, jobId)) - ); + ActionListener jobListener = ActionListener.wrap(jobResponse -> { + List jobPage = jobResponse.getResponse().results(); + // We requested a single concrete job so if it didn't exist we would get an error + assert jobPage.size() == 1; + + String jobSnapshotId = jobPage.get(0).getModelSnapshotId(); + if (jobSnapshotId == null && isMasterNodeVersionOnOrAfter(ResetJobAction.VERSION_INTRODUCED)) { + logger.info("[{}] job has running datafeed task; resetting as no snapshot exists", jobId); + ResetJobAction.Request request = new ResetJobAction.Request(jobId); + request.setSkipJobStateValidation(true); + request.masterNodeTimeout(PERSISTENT_TASK_MASTER_NODE_TIMEOUT); + request.timeout(PERSISTENT_TASK_MASTER_NODE_TIMEOUT); + executeAsyncWithOrigin( + client, + ML_ORIGIN, + ResetJobAction.INSTANCE, + request, + ActionListener.wrap(response -> listener.onResponse(true), listener::onFailure) + ); + } else { + logger.info("[{}] job has running datafeed task; reverting to current snapshot", jobId); + RevertModelSnapshotAction.Request request = new RevertModelSnapshotAction.Request( + jobId, + jobSnapshotId == null ? ModelSnapshot.EMPTY_SNAPSHOT_ID : jobSnapshotId + ); + request.setForce(true); + request.setDeleteInterveningResults(true); + request.masterNodeTimeout(PERSISTENT_TASK_MASTER_NODE_TIMEOUT); + request.timeout(PERSISTENT_TASK_MASTER_NODE_TIMEOUT); + executeAsyncWithOrigin( + client, + ML_ORIGIN, + RevertModelSnapshotAction.INSTANCE, + request, + ActionListener.wrap(response -> listener.onResponse(true), listener::onFailure) + ); + } + }, error -> listener.onFailure(ExceptionsHelper.serverError("[{}] error getting job", error, jobId))); // We need to refetch the job in order to learn what is its current model snapshot // as the one that exists in the task params is outdated. @@ -438,7 +462,7 @@ private void revertToCurrentSnapshot(String jobId, ActionListener liste } // Exceptions that occur while the node is dying, i.e. after the JVM has received a SIGTERM, - // are ignored. Core services will be stopping in response to the SIGTERM and we want the + // are ignored. Core services will be stopping in response to the SIGTERM and we want the // job to try to open again on another node, not spuriously fail on the dying node. private void openJob(JobTask jobTask) { String jobId = jobTask.getJobId(); @@ -447,33 +471,36 @@ private void openJob(JobTask jobTask) { // Beyond this point it's too late to change our minds about whether we're closing or vacating if (jobTask.isVacating()) { jobTask.markAsLocallyAborted( - "previously assigned node [" + clusterState.nodes().getLocalNode().getName() + "] is shutting down"); + "previously assigned node [" + clusterState.nodes().getLocalNode().getName() + "] is shutting down" + ); } else if (shouldFinalizeJob) { - FinalizeJobExecutionAction.Request finalizeRequest = new FinalizeJobExecutionAction.Request(new String[]{jobId}); + FinalizeJobExecutionAction.Request finalizeRequest = new FinalizeJobExecutionAction.Request(new String[] { jobId }); finalizeRequest.masterNodeTimeout(PERSISTENT_TASK_MASTER_NODE_TIMEOUT); - executeAsyncWithOrigin(client, ML_ORIGIN, FinalizeJobExecutionAction.INSTANCE, finalizeRequest, - ActionListener.wrap( - response -> jobTask.markAsCompleted(), - e -> { - // This error is logged even if the node is dying. This is a nasty place for the node to get killed, - // as most of the job's close sequence has executed, just not the finalization step. The job will - // restart on a different node. If the coordinating node for the close request notices that the job - // changed nodes while waiting for it to close then it will remove the persistent task, which should - // stop the job doing anything significant on its new node. However, the finish time of the job will - // not be set correctly. - logger.error(new ParameterizedMessage("[{}] error finalizing job", jobId), e); - Throwable unwrapped = ExceptionsHelper.unwrapCause(e); - if (unwrapped instanceof DocumentMissingException || unwrapped instanceof ResourceNotFoundException) { - jobTask.markAsCompleted(); - } else if (autodetectProcessManager.isNodeDying() == false) { - // In this case we prefer to mark the task as failed, which means the job - // will appear closed. The reason is that the job closed successfully and - // we just failed to update some fields like the finish time. It is preferable - // to let the job close than setting it to failed. - jobTask.markAsFailed(e); - } + executeAsyncWithOrigin( + client, + ML_ORIGIN, + FinalizeJobExecutionAction.INSTANCE, + finalizeRequest, + ActionListener.wrap(response -> jobTask.markAsCompleted(), e -> { + // This error is logged even if the node is dying. This is a nasty place for the node to get killed, + // as most of the job's close sequence has executed, just not the finalization step. The job will + // restart on a different node. If the coordinating node for the close request notices that the job + // changed nodes while waiting for it to close then it will remove the persistent task, which should + // stop the job doing anything significant on its new node. However, the finish time of the job will + // not be set correctly. + logger.error(new ParameterizedMessage("[{}] error finalizing job", jobId), e); + Throwable unwrapped = ExceptionsHelper.unwrapCause(e); + if (unwrapped instanceof DocumentMissingException || unwrapped instanceof ResourceNotFoundException) { + jobTask.markAsCompleted(); + } else if (autodetectProcessManager.isNodeDying() == false) { + // In this case we prefer to mark the task as failed, which means the job + // will appear closed. The reason is that the job closed successfully and + // we just failed to update some fields like the finish time. It is preferable + // to let the job close than setting it to failed. + jobTask.markAsFailed(e); } - )); + }) + ); } else { jobTask.markAsCompleted(); } @@ -485,15 +512,22 @@ private void openJob(JobTask jobTask) { } @Override - protected AllocatedPersistentTask createTask(long id, String type, String action, TaskId parentTaskId, - PersistentTasksCustomMetadata.PersistentTask persistentTask, - Map headers) { + protected AllocatedPersistentTask createTask( + long id, + String type, + String action, + TaskId parentTaskId, + PersistentTasksCustomMetadata.PersistentTask persistentTask, + Map headers + ) { return new JobTask(persistentTask.getParams().getJobId(), id, type, action, parentTaskId, headers, licenseState); } - public static Optional checkAssignmentState(PersistentTasksCustomMetadata.Assignment assignment, - String jobId, - Logger logger) { + public static Optional checkAssignmentState( + PersistentTasksCustomMetadata.Assignment assignment, + String jobId, + Logger logger + ) { if (assignment != null && assignment.equals(PersistentTasksCustomMetadata.INITIAL_ASSIGNMENT) == false && assignment.isAssigned() == false) { @@ -513,13 +547,17 @@ static ElasticsearchException makeNoSuitableNodesException(Logger logger, String String msg = "Could not open job because no suitable nodes were found, allocation explanation [" + explanation + "]"; logger.warn("[{}] {}", jobId, msg); Exception detail = new IllegalStateException(msg); - return new ElasticsearchStatusException("Could not open job because no ML nodes with sufficient capacity were found", - RestStatus.TOO_MANY_REQUESTS, detail); + return new ElasticsearchStatusException( + "Could not open job because no ML nodes with sufficient capacity were found", + RestStatus.TOO_MANY_REQUESTS, + detail + ); } static ElasticsearchException makeAssignmentsNotAllowedException(Logger logger, String jobId) { String msg = "Cannot open jobs because persistent task assignment is disabled by the [" - + EnableAssignmentDecider.CLUSTER_TASKS_ALLOCATION_ENABLE_SETTING.getKey() + "] setting"; + + EnableAssignmentDecider.CLUSTER_TASKS_ALLOCATION_ENABLE_SETTING.getKey() + + "] setting"; logger.warn("[{}] {}", jobId, msg); return new ElasticsearchStatusException(msg, RestStatus.TOO_MANY_REQUESTS); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/process/AbstractNativeProcess.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/process/AbstractNativeProcess.java index 5c3a1d75d0ed6..14894ead9691f 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/process/AbstractNativeProcess.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/process/AbstractNativeProcess.java @@ -61,8 +61,14 @@ public abstract class AbstractNativeProcess implements NativeProcess { private volatile boolean processKilled; private volatile boolean isReady; - protected AbstractNativeProcess(String jobId, NativeController nativeController, ProcessPipes processPipes, - int numberOfFields, List filesToDelete, Consumer onProcessCrash) { + protected AbstractNativeProcess( + String jobId, + NativeController nativeController, + ProcessPipes processPipes, + int numberOfFields, + List filesToDelete, + Consumer onProcessCrash + ) { this.jobId = jobId; this.nativeController = nativeController; this.processPipes = processPipes; @@ -124,8 +130,8 @@ private void detectCrash() { String errors = cppLogHandler().getErrors(); long pid = cppLogHandler().tryGetPid(); - String fullError = pid > 0 ? - String.format(Locale.ROOT, "[%s] %s/%d process stopped unexpectedly: %s", jobId, getName(), pid, errors) + String fullError = pid > 0 + ? String.format(Locale.ROOT, "[%s] %s/%d process stopped unexpectedly: %s", jobId, getName(), pid, errors) : String.format(Locale.ROOT, "[%s] %s process stopped unexpectedly before logging started: %s", jobId, getName(), errors); LOGGER.error(fullError); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/process/ControllerResponse.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/process/ControllerResponse.java index b509ca77dca86..ff361864b4ef0 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/process/ControllerResponse.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/process/ControllerResponse.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.ml.process; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -24,7 +24,9 @@ public class ControllerResponse implements ToXContentObject { public static final ParseField REASON = new ParseField("reason"); public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - TYPE.getPreferredName(), a -> new ControllerResponse((int) a[0], (boolean) a[1], (String) a[2])); + TYPE.getPreferredName(), + a -> new ControllerResponse((int) a[0], (boolean) a[1], (String) a[2]) + ); static { PARSER.declareInt(ConstructingObjectParser.constructorArg(), COMMAND_ID); @@ -75,9 +77,7 @@ public boolean equals(Object o) { return false; } ControllerResponse that = (ControllerResponse) o; - return this.commandId == that.commandId && - this.success == that.success && - Objects.equals(this.reason, that.reason); + return this.commandId == that.commandId && this.success == that.success && Objects.equals(this.reason, that.reason); } @Override diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/process/IndexingStateProcessor.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/process/IndexingStateProcessor.java index 0ec8a86a1a177..cd3e699a9d064 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/process/IndexingStateProcessor.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/process/IndexingStateProcessor.java @@ -17,13 +17,13 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.CompositeBytesReference; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.index.query.BoolQueryBuilder; +import org.elasticsearch.index.query.IdsQueryBuilder; +import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.index.query.BoolQueryBuilder; -import org.elasticsearch.index.query.IdsQueryBuilder; -import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.xpack.core.common.notifications.AbstractAuditMessage; import org.elasticsearch.xpack.core.common.notifications.AbstractAuditor; import org.elasticsearch.xpack.core.ml.job.persistence.AnomalyDetectorsIndex; @@ -62,9 +62,11 @@ public class IndexingStateProcessor implements StateProcessor { private final AbstractAuditor auditor; private final ResultsPersisterService resultsPersisterService; - public IndexingStateProcessor(String jobId, - ResultsPersisterService resultsPersisterService, - AbstractAuditor auditor) { + public IndexingStateProcessor( + String jobId, + ResultsPersisterService resultsPersisterService, + AbstractAuditor auditor + ) { this.jobId = jobId; this.resultsPersisterService = resultsPersisterService; this.auditor = auditor; @@ -138,17 +140,18 @@ void findAppropriateIndexOrAliasAndPersist(BytesReference bytes) throws IOExcept } void persist(String indexOrAlias, BytesReference bytes) throws IOException { - BulkRequest bulkRequest = new BulkRequest() - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + BulkRequest bulkRequest = new BulkRequest().setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) .requireAlias(AnomalyDetectorsIndex.jobStateIndexWriteAlias().equals(indexOrAlias)); bulkRequest.add(bytes, indexOrAlias, XContentType.JSON); if (bulkRequest.numberOfActions() > 0) { LOGGER.trace("[{}] Persisting job state document: index [{}], length [{}]", jobId, indexOrAlias, bytes.length()); try { - resultsPersisterService.bulkIndexWithRetry(bulkRequest, + resultsPersisterService.bulkIndexWithRetry( + bulkRequest, jobId, () -> true, - retryMessage -> LOGGER.debug("[{}] Bulk indexing of state failed {}", jobId, retryMessage)); + retryMessage -> LOGGER.debug("[{}] Bulk indexing of state failed {}", jobId, retryMessage) + ); } catch (Exception ex) { String msg = "failed indexing updated state docs"; LOGGER.error(() -> new ParameterizedMessage("[{}] {}", jobId, msg), ex); @@ -158,7 +161,7 @@ void persist(String indexOrAlias, BytesReference bytes) throws IOException { } private static int findNextZeroByte(BytesReference bytesRef, int searchFrom, int splitFrom) { - return bytesRef.indexOf((byte)0, Math.max(searchFrom, splitFrom)); + return bytesRef.indexOf((byte) 0, Math.max(searchFrom, splitFrom)); } @SuppressWarnings("unchecked") @@ -167,18 +170,22 @@ private static int findNextZeroByte(BytesReference bytesRef, int searchFrom, int * Only first non-blank line is parsed and document id is assumed to be a nested "index._id" field of type String. */ static String extractDocId(String firstNonBlankLine) throws IOException { - try (XContentParser parser = - JsonXContent.jsonXContent.createParser( - NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, firstNonBlankLine)) { + try ( + XContentParser parser = JsonXContent.jsonXContent.createParser( + NamedXContentRegistry.EMPTY, + LoggingDeprecationHandler.INSTANCE, + firstNonBlankLine + ) + ) { Map map = parser.map(); if ((map.get("index") instanceof Map) == false) { throw new IllegalStateException("Could not extract \"index\" field out of [" + firstNonBlankLine + "]"); } - map = (Map)map.get("index"); + map = (Map) map.get("index"); if ((map.get("_id") instanceof String) == false) { throw new IllegalStateException("Could not extract \"index._id\" field out of [" + firstNonBlankLine + "]"); } - return (String)map.get("_id"); + return (String) map.get("_id"); } } @@ -214,23 +221,20 @@ private static boolean isBlank(BytesReference bytesRef, int from, int to) { private String getConcreteIndexOrWriteAlias(String documentId) { Objects.requireNonNull(documentId); - SearchRequest searchRequest = - new SearchRequest(AnomalyDetectorsIndex.jobStateIndexPattern()) - .allowPartialSearchResults(false) - .source( - new SearchSourceBuilder() - .size(1) - .trackTotalHits(false) - .query(new BoolQueryBuilder().filter(new IdsQueryBuilder().addIds(documentId)))); - SearchResponse searchResponse = - resultsPersisterService.searchWithRetry( - searchRequest, - jobId, - () -> true, - retryMessage -> LOGGER.debug("[{}] {} {}", jobId, documentId, retryMessage)); + SearchRequest searchRequest = new SearchRequest(AnomalyDetectorsIndex.jobStateIndexPattern()).allowPartialSearchResults(false) + .source( + new SearchSourceBuilder().size(1) + .trackTotalHits(false) + .query(new BoolQueryBuilder().filter(new IdsQueryBuilder().addIds(documentId))) + ); + SearchResponse searchResponse = resultsPersisterService.searchWithRetry( + searchRequest, + jobId, + () -> true, + retryMessage -> LOGGER.debug("[{}] {} {}", jobId, documentId, retryMessage) + ); return searchResponse.getHits().getHits().length > 0 ? searchResponse.getHits().getHits()[0].getIndex() : AnomalyDetectorsIndex.jobStateIndexWriteAlias(); } } - diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/process/MlMemoryTracker.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/process/MlMemoryTracker.java index aab5bd105c71f..661f228761365 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/process/MlMemoryTracker.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/process/MlMemoryTracker.java @@ -84,8 +84,14 @@ public class MlMemoryTracker implements LocalNodeMasterListener { private volatile Instant lastUpdateTime; private volatile Duration reassignmentRecheckInterval; - public MlMemoryTracker(Settings settings, ClusterService clusterService, ThreadPool threadPool, JobManager jobManager, - JobResultsProvider jobResultsProvider, DataFrameAnalyticsConfigProvider configProvider) { + public MlMemoryTracker( + Settings settings, + ClusterService clusterService, + ThreadPool threadPool, + JobManager jobManager, + JobResultsProvider jobResultsProvider, + DataFrameAnalyticsConfigProvider configProvider + ) { this.threadPool = threadPool; this.clusterService = clusterService; this.jobManager = jobManager; @@ -100,8 +106,11 @@ public MlMemoryTracker(Settings settings, ClusterService clusterService, ThreadP setReassignmentRecheckInterval(PersistentTasksClusterService.CLUSTER_TASKS_ALLOCATION_RECHECK_INTERVAL_SETTING.get(settings)); clusterService.addLocalNodeMasterListener(this); - clusterService.getClusterSettings().addSettingsUpdateConsumer( - PersistentTasksClusterService.CLUSTER_TASKS_ALLOCATION_RECHECK_INTERVAL_SETTING, this::setReassignmentRecheckInterval); + clusterService.getClusterSettings() + .addSettingsUpdateConsumer( + PersistentTasksClusterService.CLUSTER_TASKS_ALLOCATION_RECHECK_INTERVAL_SETTING, + this::setReassignmentRecheckInterval + ); } private void setReassignmentRecheckInterval(TimeValue recheckInterval) { @@ -134,23 +143,21 @@ public void awaitAndClear(ActionListener listener) { // in the register/arrive/unregister logic in another method that uses the phaser assert stopPhaser.getRegisteredParties() > 0; assert stopPhaser.getUnarrivedParties() > 0; - threadPool.executor(MachineLearning.UTILITY_THREAD_POOL_NAME).execute( - () -> { - try { - // We await all current refreshes to complete, this increments the "current phase" and prevents - // further interaction while we clear contents - int newPhase = stopPhaser.arriveAndAwaitAdvance(); - assert newPhase > 0; - clear(); - phase.incrementAndGet(); - logger.trace("completed awaiting and clearing memory tracker"); - listener.onResponse(null); - } catch (Exception e) { - logger.warn("failed to wait for all refresh requests to complete", e); - listener.onFailure(e); - } + threadPool.executor(MachineLearning.UTILITY_THREAD_POOL_NAME).execute(() -> { + try { + // We await all current refreshes to complete, this increments the "current phase" and prevents + // further interaction while we clear contents + int newPhase = stopPhaser.arriveAndAwaitAdvance(); + assert newPhase > 0; + clear(); + phase.incrementAndGet(); + logger.trace("completed awaiting and clearing memory tracker"); + listener.onResponse(null); + } catch (Exception e) { + logger.warn("failed to wait for all refresh requests to complete", e); + listener.onFailure(e); } - ); + }); } @@ -194,8 +201,9 @@ public boolean isRecentlyRefreshed() { */ public boolean isRecentlyRefreshed(Duration customDuration) { Instant localLastUpdateTime = lastUpdateTime; - return isMaster && localLastUpdateTime != null && - localLastUpdateTime.plus(RECENT_UPDATE_THRESHOLD).plus(customDuration).isAfter(Instant.now()); + return isMaster + && localLastUpdateTime != null + && localLastUpdateTime.plus(RECENT_UPDATE_THRESHOLD).plus(customDuration).isAfter(Instant.now()); } /** @@ -254,7 +262,7 @@ public Long getJobMemoryRequirement(String taskName, String id) { Map memoryRequirementByJob = memoryRequirementByTaskName.get(taskName); if (memoryRequirementByJob == null) { - assert false: "Unknown taskName type [" + taskName +"]"; + assert false : "Unknown taskName type [" + taskName + "]"; return null; } return memoryRequirementByJob.get(id); @@ -292,8 +300,8 @@ public boolean asyncRefresh() { aVoid -> logger.trace("Job memory requirement refresh request completed successfully"), e -> logIfNecessary(() -> logger.warn("Failed to refresh job memory requirements", e)) ); - threadPool.executor(MachineLearning.UTILITY_THREAD_POOL_NAME).execute( - () -> refresh(clusterService.state().getMetadata().custom(PersistentTasksCustomMetadata.TYPE), listener)); + threadPool.executor(MachineLearning.UTILITY_THREAD_POOL_NAME) + .execute(() -> refresh(clusterService.state().getMetadata().custom(PersistentTasksCustomMetadata.TYPE), listener)); return true; } catch (EsRejectedExecutionException e) { logger.warn("Couldn't schedule ML memory update - node might be shutting down", e); @@ -320,8 +328,7 @@ public void refreshAnomalyDetectorJobMemoryAndAllOthers(String jobId, ActionList } PersistentTasksCustomMetadata persistentTasks = clusterService.state().getMetadata().custom(PersistentTasksCustomMetadata.TYPE); - refresh(persistentTasks, - ActionListener.wrap(aVoid -> refreshAnomalyDetectorJobMemory(jobId, listener), listener::onFailure)); + refresh(persistentTasks, ActionListener.wrap(aVoid -> refreshAnomalyDetectorJobMemory(jobId, listener), listener::onFailure)); } /** @@ -378,8 +385,7 @@ public void refresh(PersistentTasksCustomMetadata persistentTasks, ActionListene } fullRefreshCompletionListeners.clear(); } - }, - e -> { + }, e -> { synchronized (fullRefreshCompletionListeners) { assert fullRefreshCompletionListeners.isEmpty() == false; for (ActionListener listener : fullRefreshCompletionListeners) { @@ -396,23 +402,31 @@ public void refresh(PersistentTasksCustomMetadata persistentTasks, ActionListene if (persistentTasks == null) { refreshComplete.onResponse(null); } else { - List> mlDataFrameAnalyticsJobTasks = persistentTasks.tasks().stream() - .filter(task -> MlTasks.DATA_FRAME_ANALYTICS_TASK_NAME.equals(task.getTaskName())).collect(Collectors.toList()); - ActionListener refreshDataFrameAnalyticsJobs = - ActionListener.wrap(aVoid -> refreshAllDataFrameAnalyticsJobTasks(mlDataFrameAnalyticsJobTasks, refreshComplete), - refreshComplete::onFailure); - - List> mlAnomalyDetectorJobTasks = persistentTasks.tasks().stream() - .filter(task -> MlTasks.JOB_TASK_NAME.equals(task.getTaskName())).collect(Collectors.toList()); + List> mlDataFrameAnalyticsJobTasks = persistentTasks.tasks() + .stream() + .filter(task -> MlTasks.DATA_FRAME_ANALYTICS_TASK_NAME.equals(task.getTaskName())) + .collect(Collectors.toList()); + ActionListener refreshDataFrameAnalyticsJobs = ActionListener.wrap( + aVoid -> refreshAllDataFrameAnalyticsJobTasks(mlDataFrameAnalyticsJobTasks, refreshComplete), + refreshComplete::onFailure + ); + + List> mlAnomalyDetectorJobTasks = persistentTasks.tasks() + .stream() + .filter(task -> MlTasks.JOB_TASK_NAME.equals(task.getTaskName())) + .collect(Collectors.toList()); iterateAnomalyDetectorJobTasks(mlAnomalyDetectorJobTasks.iterator(), refreshDataFrameAnalyticsJobs); } } - private void iterateAnomalyDetectorJobTasks(Iterator> iterator, - ActionListener refreshComplete) { + private void iterateAnomalyDetectorJobTasks( + Iterator> iterator, + ActionListener refreshComplete + ) { if (iterator.hasNext()) { OpenJobAction.JobParams jobParams = (OpenJobAction.JobParams) iterator.next().getParams(); - refreshAnomalyDetectorJobMemory(jobParams.getJobId(), + refreshAnomalyDetectorJobMemory( + jobParams.getJobId(), ActionListener.wrap( // Do the next iteration in a different thread, otherwise stack overflow // can occur if the searches happen to be on the local node, as the huge @@ -420,32 +434,36 @@ private void iterateAnomalyDetectorJobTasks(Iterator threadPool.executor(MachineLearning.UTILITY_THREAD_POOL_NAME) .execute(() -> iterateAnomalyDetectorJobTasks(iterator, refreshComplete)), - refreshComplete::onFailure)); + refreshComplete::onFailure + ) + ); } else { refreshComplete.onResponse(null); } } - private void refreshAllDataFrameAnalyticsJobTasks(List> mlDataFrameAnalyticsJobTasks, - ActionListener listener) { + private void refreshAllDataFrameAnalyticsJobTasks( + List> mlDataFrameAnalyticsJobTasks, + ActionListener listener + ) { if (mlDataFrameAnalyticsJobTasks.isEmpty()) { listener.onResponse(null); return; } - Set jobsWithTasks = mlDataFrameAnalyticsJobTasks.stream().map( - task -> ((StartDataFrameAnalyticsAction.TaskParams) task.getParams()).getId()).collect(Collectors.toSet()); + Set jobsWithTasks = mlDataFrameAnalyticsJobTasks.stream() + .map(task -> ((StartDataFrameAnalyticsAction.TaskParams) task.getParams()).getId()) + .collect(Collectors.toSet()); - configProvider.getConfigsForJobsWithTasksLeniently(jobsWithTasks, ActionListener.wrap( - analyticsConfigs -> { - for (DataFrameAnalyticsConfig analyticsConfig : analyticsConfigs) { - memoryRequirementByDataFrameAnalyticsJob.put(analyticsConfig.getId(), - analyticsConfig.getModelMemoryLimit().getBytes() + DataFrameAnalyticsConfig.PROCESS_MEMORY_OVERHEAD.getBytes()); - } - listener.onResponse(null); - }, - listener::onFailure - )); + configProvider.getConfigsForJobsWithTasksLeniently(jobsWithTasks, ActionListener.wrap(analyticsConfigs -> { + for (DataFrameAnalyticsConfig analyticsConfig : analyticsConfigs) { + memoryRequirementByDataFrameAnalyticsJob.put( + analyticsConfig.getId(), + analyticsConfig.getModelMemoryLimit().getBytes() + DataFrameAnalyticsConfig.PROCESS_MEMORY_OVERHEAD.getBytes() + ); + } + listener.onResponse(null); + }, listener::onFailure)); } /** @@ -466,47 +484,39 @@ public void refreshAnomalyDetectorJobMemory(String jobId, ActionListener l if (stopPhaser.register() != phase.get()) { // Phases above not equal to `phase` mean we've been stopped, so don't do any operations that involve external interaction stopPhaser.arriveAndDeregister(); - logger.info( - () -> new ParameterizedMessage("[{}] not refreshing anomaly detector memory as node is shutting down", jobId) - ); + logger.info(() -> new ParameterizedMessage("[{}] not refreshing anomaly detector memory as node is shutting down", jobId)); listener.onFailure(new EsRejectedExecutionException("Couldn't run ML memory update - node is shutting down")); return; } - ActionListener phaserListener = ActionListener.wrap( - r -> { - stopPhaser.arriveAndDeregister(); - listener.onResponse(r); - }, - e -> { - stopPhaser.arriveAndDeregister(); - listener.onFailure(e); - } - ); + ActionListener phaserListener = ActionListener.wrap(r -> { + stopPhaser.arriveAndDeregister(); + listener.onResponse(r); + }, e -> { + stopPhaser.arriveAndDeregister(); + listener.onFailure(e); + }); try { - jobResultsProvider.getEstablishedMemoryUsage(jobId, null, null, - establishedModelMemoryBytes -> { - if (establishedModelMemoryBytes <= 0L) { - setAnomalyDetectorJobMemoryToLimit(jobId, phaserListener); - } else { - Long memoryRequirementBytes = establishedModelMemoryBytes + Job.PROCESS_MEMORY_OVERHEAD.getBytes(); - memoryRequirementByAnomalyDetectorJob.put(jobId, memoryRequirementBytes); - phaserListener.onResponse(memoryRequirementBytes); - } - }, - e -> { - logIfNecessary( - () -> logger.error( - () -> new ParameterizedMessage( - "[{}] failed to calculate anomaly detector job established model memory requirement", - jobId - ), - e - ) - ); + jobResultsProvider.getEstablishedMemoryUsage(jobId, null, null, establishedModelMemoryBytes -> { + if (establishedModelMemoryBytes <= 0L) { setAnomalyDetectorJobMemoryToLimit(jobId, phaserListener); + } else { + Long memoryRequirementBytes = establishedModelMemoryBytes + Job.PROCESS_MEMORY_OVERHEAD.getBytes(); + memoryRequirementByAnomalyDetectorJob.put(jobId, memoryRequirementBytes); + phaserListener.onResponse(memoryRequirementBytes); } - ); + }, e -> { + logIfNecessary( + () -> logger.error( + () -> new ParameterizedMessage( + "[{}] failed to calculate anomaly detector job established model memory requirement", + jobId + ), + e + ) + ); + setAnomalyDetectorJobMemoryToLimit(jobId, phaserListener); + }); } catch (Exception e) { logIfNecessary( () -> logger.error( diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/process/NativeController.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/process/NativeController.java index 0d87d5b3be3f2..a26ec9742a8de 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/process/NativeController.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/process/NativeController.java @@ -10,8 +10,8 @@ import org.apache.logging.log4j.Logger; import org.apache.lucene.util.SetOnce; import org.elasticsearch.ElasticsearchException; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.env.Environment; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.ml.process.logging.CppLogMessageHandler; import org.elasticsearch.xpack.ml.utils.NamedPipeHelper; @@ -28,7 +28,6 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeoutException; - /** * Maintains the connection to the native controller daemon that can start other processes. */ @@ -55,9 +54,9 @@ public class NativeController implements MlController { private final NamedXContentRegistry xContentRegistry; private final Map responseTrackers = new ConcurrentHashMap<>(); // The response iterator cannot be constructed until something is expected to be in the stream it's reading from, - // otherwise it will block while it tries to read a few bytes to determine the character set. It could be created + // otherwise it will block while it tries to read a few bytes to determine the character set. It could be created // immediately in a dedicated thread, but that's wasteful as we can reuse the threads that are sending the commands - // to the controller to read the responses. So we create it in the first thread that wants to know the response to + // to the controller to read the responses. So we create it in the first thread that wants to know the response to // a command. private final SetOnce> responseIteratorHolder = new SetOnce<>(); private int nextCommandId = 1; // synchronized on commandStream so doesn't need to be volatile @@ -70,8 +69,19 @@ public static NativeController makeNativeController(String localNodeName, Enviro NativeController(String localNodeName, Environment env, NamedPipeHelper namedPipeHelper, NamedXContentRegistry xContentRegistry) throws IOException { this.localNodeName = localNodeName; - ProcessPipes processPipes = new ProcessPipes(env, namedPipeHelper, CONTROLLER_CONNECT_TIMEOUT, CONTROLLER, null, null, - true, false, true, false, false); + ProcessPipes processPipes = new ProcessPipes( + env, + namedPipeHelper, + CONTROLLER_CONNECT_TIMEOUT, + CONTROLLER, + null, + null, + true, + false, + true, + false, + false + ); processPipes.connectLogStream(); this.cppLogHandler = processPipes.getLogStreamHandler(); tailLogsInThread(cppLogHandler); @@ -82,16 +92,14 @@ public static NativeController makeNativeController(String localNodeName, Enviro } static void tailLogsInThread(CppLogMessageHandler cppLogHandler) { - final Thread logTailThread = new Thread( - () -> { - try (CppLogMessageHandler h = cppLogHandler) { - h.tailStream(); - } catch (IOException e) { - LOGGER.error("Error tailing C++ controller logs", e); - } - LOGGER.info("Native controller process has stopped - no new native processes can be started"); - }, - "ml-cpp-log-tail-thread"); + final Thread logTailThread = new Thread(() -> { + try (CppLogMessageHandler h = cppLogHandler) { + h.tailStream(); + } catch (IOException e) { + LOGGER.error("Error tailing C++ controller logs", e); + } + LOGGER.info("Native controller process has stopped - no new native processes can be started"); + }, "ml-cpp-log-tail-thread"); /* * This thread is created on the main thread so would default to being a user thread which could prevent the JVM from exiting if * this thread were to still be running during shutdown. As such, we mark it as a daemon thread. @@ -125,8 +133,11 @@ public void startProcess(List command) throws IOException, InterruptedEx } if (cppLogHandler.hasLogStreamEnded()) { - String msg = "Cannot start process [" + command.get(0) + "]: native controller process has stopped on node [" - + localNodeName + "]"; + String msg = "Cannot start process [" + + command.get(0) + + "]: native controller process has stopped on node [" + + localNodeName + + "]"; LOGGER.error(msg); throw new ElasticsearchException(msg); } @@ -162,8 +173,11 @@ public void killProcess(long pid, boolean awaitCompletion) throws TimeoutExcepti } if (cppLogHandler.hasLogStreamEnded()) { - String msg = "Cannot kill process with PID [" + pid + "]: native controller process has stopped on node [" - + localNodeName + "]"; + String msg = "Cannot kill process with PID [" + + pid + + "]: native controller process has stopped on node [" + + localNodeName + + "]"; LOGGER.error(msg); throw new ElasticsearchException(msg); } @@ -217,7 +231,7 @@ private void awaitCompletion(int commandId) throws IOException, InterruptedExcep // If our response has not been seen already (by another thread), parse messages under lock until it is seen. // This approach means that of all the threads waiting for controller responses, one is parsing the messages - // on behalf of all of them, and the others are blocked. When the thread that is parsing gets the response + // on behalf of all of them, and the others are blocked. When the thread that is parsing gets the response // it needs another thread will pick up the parsing. if (ourResponseTracker.hasResponded() == false) { synchronized (responseIteratorHolder) { @@ -229,8 +243,9 @@ private void awaitCompletion(int commandId) throws IOException, InterruptedExcep } while (ourResponseTracker.hasResponded() == false) { if (responseIterator.hasNext() == false) { - throw new IOException("ML controller response stream ended while awaiting response for command [" + - commandId + "]"); + throw new IOException( + "ML controller response stream ended while awaiting response for command [" + commandId + "]" + ); } ControllerResponse response = responseIterator.next(); ResponseTracker respondedTracker = responseTrackers.get(response.getCommandId()); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/process/ProcessPipes.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/process/ProcessPipes.java index a733a52333549..fca64a32cd499 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/process/ProcessPipes.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/process/ProcessPipes.java @@ -76,15 +76,25 @@ public class ProcessPipes { * @param jobId The job ID of the process to which pipes are to be opened, if the process is associated with a specific job. * May be null or empty for processes not associated with a specific job. */ - public ProcessPipes(Environment env, NamedPipeHelper namedPipeHelper, Duration timeout, String processName, String jobId, - Long uniqueId, boolean wantCommandPipe, boolean wantProcessInPipe, boolean wantProcessOutPipe, - boolean wantRestorePipe, boolean wantPersistPipe) { + public ProcessPipes( + Environment env, + NamedPipeHelper namedPipeHelper, + Duration timeout, + String processName, + String jobId, + Long uniqueId, + boolean wantCommandPipe, + boolean wantProcessInPipe, + boolean wantProcessOutPipe, + boolean wantRestorePipe, + boolean wantPersistPipe + ) { this.namedPipeHelper = namedPipeHelper; this.jobId = jobId; this.timeout = timeout; // The way the pipe names are formed MUST match what is done in the controller main() - // function, as it does not get any command line arguments when started as a daemon. If + // function, as it does not get any command line arguments when started as a daemon. If // you change the code here then you MUST also change the C++ code in controller's // main() function. StringBuilder prefixBuilder = new StringBuilder(); @@ -152,8 +162,8 @@ public void connectOtherStreams() throws IOException { if (logStreamHandler == null) { throw new NullPointerException("Must connect log stream before other streams"); } - // The order here is important. It must match the order that the C++ process tries to connect to the pipes, otherwise - // a timeout is guaranteed. Also change api::CIoManager in the C++ code if changing the order here. + // The order here is important. It must match the order that the C++ process tries to connect to the pipes, otherwise + // a timeout is guaranteed. Also change api::CIoManager in the C++ code if changing the order here. try { if (commandPipeName != null) { commandStream = namedPipeHelper.openNamedPipeOutputStream(commandPipeName, timeout); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/process/ProcessResultsParser.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/process/ProcessResultsParser.java index 893636cdb778a..b9c7a533a45cf 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/process/ProcessResultsParser.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/process/ProcessResultsParser.java @@ -9,8 +9,8 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.ElasticsearchParseException; -import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; @@ -21,7 +21,6 @@ import java.util.Iterator; import java.util.Objects; - /** * Parses the JSON output of a process. *

    @@ -43,7 +42,7 @@ public ProcessResultsParser(ConstructingObjectParser resultParser, Name public Iterator parseResults(InputStream in) throws ElasticsearchParseException { try { XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(namedXContentRegistry, LoggingDeprecationHandler.INSTANCE, in); + .createParser(namedXContentRegistry, LoggingDeprecationHandler.INSTANCE, in); XContentParser.Token token = parser.nextToken(); // if start of an array ignore it, we expect an array of results if (token != XContentParser.Token.START_ARRAY) { @@ -88,4 +87,3 @@ public T next() { } } } - diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/process/StateToProcessWriterHelper.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/process/StateToProcessWriterHelper.java index 4703c05cc168b..baac0aa301087 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/process/StateToProcessWriterHelper.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/process/StateToProcessWriterHelper.java @@ -21,7 +21,7 @@ public final class StateToProcessWriterHelper { private StateToProcessWriterHelper() {} public static void writeStateToStream(BytesReference source, OutputStream stream) throws IOException { - // The source bytes are already UTF-8. The C++ process wants UTF-8, so we + // The source bytes are already UTF-8. The C++ process wants UTF-8, so we // can avoid converting to a Java String only to convert back again. BytesRefIterator iterator = source.iterator(); for (BytesRef ref = iterator.next(); ref != null; ref = iterator.next()) { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/process/logging/CppLogMessage.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/process/logging/CppLogMessage.java index 74d359310c2c5..96cd1f754736e 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/process/logging/CppLogMessage.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/process/logging/CppLogMessage.java @@ -6,12 +6,12 @@ */ package org.elasticsearch.xpack.ml.process.logging; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.xcontent.ObjectParser; import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -38,7 +38,9 @@ public class CppLogMessage implements ToXContentObject, Writeable { public static final ParseField LINE_FIELD = new ParseField("line"); public static final ObjectParser PARSER = new ObjectParser<>( - LOGGER_FIELD.getPreferredName(), () -> new CppLogMessage(Instant.now())); + LOGGER_FIELD.getPreferredName(), + () -> new CppLogMessage(Instant.now()) + ); static { PARSER.declareString(CppLogMessage::setLogger, LOGGER_FIELD); @@ -236,12 +238,17 @@ public boolean equals(Object other) { return false; } - CppLogMessage that = (CppLogMessage)other; - - return Objects.equals(this.logger, that.logger) && Objects.equals(this.timestamp, that.timestamp) - && Objects.equals(this.level, that.level) && this.pid == that.pid - && Objects.equals(this.thread, that.thread) && Objects.equals(this.message, that.message) - && Objects.equals(this.clazz, that.clazz) && Objects.equals(this.method, that.method) - && Objects.equals(this.file, that.file) && this.line == that.line; + CppLogMessage that = (CppLogMessage) other; + + return Objects.equals(this.logger, that.logger) + && Objects.equals(this.timestamp, that.timestamp) + && Objects.equals(this.level, that.level) + && this.pid == that.pid + && Objects.equals(this.thread, that.thread) + && Objects.equals(this.message, that.message) + && Objects.equals(this.clazz, that.clazz) + && Objects.equals(this.method, that.method) + && Objects.equals(this.file, that.file) + && this.line == that.line; } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/process/logging/CppLogMessageHandler.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/process/logging/CppLogMessageHandler.java index ebf4148149322..641a0762654b3 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/process/logging/CppLogMessageHandler.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/process/logging/CppLogMessageHandler.java @@ -151,8 +151,8 @@ public boolean waitForLogStreamClose(Duration timeout) { * available instantly after the process starts. */ public long getPid(Duration timeout) throws TimeoutException { - // There's an assumption here that 0 is not a valid PID. This is certainly true for - // userland processes. On Windows the "System Idle Process" has PID 0 and on *nix + // There's an assumption here that 0 is not a valid PID. This is certainly true for + // userland processes. On Windows the "System Idle Process" has PID 0 and on *nix // PID 0 is for "sched", which is part of the kernel. if (pid == 0) { try { @@ -250,7 +250,7 @@ private BytesReference parseMessages(XContent xContent, BytesReference bytesRef) from = nextMarker + 1; if (from < bytesRef.length() && bytesRef.get(from) == (byte) 0) { // This is to work around the problem of log4cxx on Windows - // outputting UTF-16 instead of UTF-8. For full details see + // outputting UTF-16 instead of UTF-8. For full details see // https://github.com/elastic/machine-learning-cpp/issues/385 ++from; } @@ -262,9 +262,10 @@ private BytesReference parseMessages(XContent xContent, BytesReference bytesRef) } private void parseMessage(XContent xContent, BytesReference bytesRef) { - try (InputStream stream = bytesRef.streamInput(); - XContentParser parser = xContent - .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) { + try ( + InputStream stream = bytesRef.streamInput(); + XContentParser parser = xContent.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream) + ) { CppLogMessage msg = CppLogMessage.PARSER.apply(parser, null); Level level = Level.getLevel(msg.getLevel()); if (level == null) { @@ -298,7 +299,7 @@ private void parseMessage(XContent xContent, BytesReference bytesRef) { // log summarization: log 1st message, count all consecutive messages arriving // in a certain time window and summarize them as 1 message if (msg.isSimilarTo(lastMessageSummary.message) - && (lastMessageSummary.timestamp.until(msg.getTimestamp(), ChronoUnit.SECONDS) < MAX_MESSAGE_INTERVAL_SECONDS)) { + && (lastMessageSummary.timestamp.until(msg.getTimestamp(), ChronoUnit.SECONDS) < MAX_MESSAGE_INTERVAL_SECONDS)) { // this is a repeated message, so do not log it, but count lastMessageSummary.count++; @@ -314,8 +315,16 @@ private void parseMessage(XContent xContent, BytesReference bytesRef) { } // TODO: Is there a way to preserve the original timestamp when re-logging? if (jobId != null) { - LOGGER.log(level, "[{}] [{}/{}] [{}@{}] {}", jobId, msg.getLogger(), latestPid, msg.getFile(), msg.getLine(), - latestMessage); + LOGGER.log( + level, + "[{}] [{}/{}] [{}@{}] {}", + jobId, + msg.getLogger(), + latestPid, + msg.getFile(), + msg.getLine(), + latestMessage + ); } else { LOGGER.log(level, "[{}/{}] [{}@{}] {}", msg.getLogger(), latestPid, msg.getFile(), msg.getLine(), latestMessage); } @@ -338,11 +347,18 @@ private void parseMessage(XContent xContent, BytesReference bytesRef) { seenFatalError = true; } catch (IOException e) { if (jobId != null) { - LOGGER.warn(new ParameterizedMessage("[{}] IO failure receiving C++ log message: {}", - new Object[] {jobId, bytesRef.utf8ToString()}), e); + LOGGER.warn( + new ParameterizedMessage( + "[{}] IO failure receiving C++ log message: {}", + new Object[] { jobId, bytesRef.utf8ToString() } + ), + e + ); } else { - LOGGER.warn(new ParameterizedMessage("IO failure receiving C++ log message: {}", - new Object[] {bytesRef.utf8ToString()}), e); + LOGGER.warn( + new ParameterizedMessage("IO failure receiving C++ log message: {}", new Object[] { bytesRef.utf8ToString() }), + e + ); } } } @@ -351,23 +367,51 @@ private void logSummarizedMessage() { // edge case: for 1 repeat, only log the message as is if (lastMessageSummary.count > 1) { if (jobId != null) { - LOGGER.log(lastMessageSummary.level, "[{}] [{}/{}] [{}@{}] {} | repeated [{}]", jobId, - lastMessageSummary.message.getLogger(), lastMessageSummary.message.getPid(), lastMessageSummary.message.getFile(), - lastMessageSummary.message.getLine(), lastMessageSummary.message.getMessage(), lastMessageSummary.count); + LOGGER.log( + lastMessageSummary.level, + "[{}] [{}/{}] [{}@{}] {} | repeated [{}]", + jobId, + lastMessageSummary.message.getLogger(), + lastMessageSummary.message.getPid(), + lastMessageSummary.message.getFile(), + lastMessageSummary.message.getLine(), + lastMessageSummary.message.getMessage(), + lastMessageSummary.count + ); } else { - LOGGER.log(lastMessageSummary.level, "[{}/{}] [{}@{}] {} | repeated [{}]", lastMessageSummary.message.getLogger(), - lastMessageSummary.message.getPid(), lastMessageSummary.message.getFile(), lastMessageSummary.message.getLine(), - lastMessageSummary.message.getMessage(), lastMessageSummary.count); + LOGGER.log( + lastMessageSummary.level, + "[{}/{}] [{}@{}] {} | repeated [{}]", + lastMessageSummary.message.getLogger(), + lastMessageSummary.message.getPid(), + lastMessageSummary.message.getFile(), + lastMessageSummary.message.getLine(), + lastMessageSummary.message.getMessage(), + lastMessageSummary.count + ); } } else { if (jobId != null) { - LOGGER.log(lastMessageSummary.level, "[{}] [{}/{}] [{}@{}] {}", jobId, lastMessageSummary.message.getLogger(), - lastMessageSummary.message.getPid(), lastMessageSummary.message.getFile(), lastMessageSummary.message.getLine(), - lastMessageSummary.message.getMessage()); + LOGGER.log( + lastMessageSummary.level, + "[{}] [{}/{}] [{}@{}] {}", + jobId, + lastMessageSummary.message.getLogger(), + lastMessageSummary.message.getPid(), + lastMessageSummary.message.getFile(), + lastMessageSummary.message.getLine(), + lastMessageSummary.message.getMessage() + ); } else { - LOGGER.log(lastMessageSummary.level, "[{}/{}] [{}@{}] {}", lastMessageSummary.message.getLogger(), - lastMessageSummary.message.getPid(), lastMessageSummary.message.getFile(), lastMessageSummary.message.getLine(), - lastMessageSummary.message.getMessage()); + LOGGER.log( + lastMessageSummary.level, + "[{}/{}] [{}@{}] {}", + lastMessageSummary.message.getLogger(), + lastMessageSummary.message.getPid(), + lastMessageSummary.message.getFile(), + lastMessageSummary.message.getLine(), + lastMessageSummary.message.getMessage() + ); } } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/process/writer/LengthEncodedWriter.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/process/writer/LengthEncodedWriter.java index 12f2e46f69c66..82a44c4f3b075 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/process/writer/LengthEncodedWriter.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/process/writer/LengthEncodedWriter.java @@ -39,7 +39,6 @@ public LengthEncodedWriter(OutputStream os) { lengthBuffer = ByteBuffer.allocate(4); // 4 == sizeof(int) } - /** * Convert each String in the record array to a length/value encoded pair * and write to the outputstream. @@ -66,7 +65,6 @@ public void writeRecord(List record) throws IOException { } } - /** * Lower level functions to write records individually. * After this function is called {@link #writeField(String)} @@ -79,7 +77,6 @@ public void writeNumFields(int numFields) throws IOException { outputStream.write(lengthBuffer.array()); } - /** * Lower level functions to write record fields individually. * {@linkplain #writeNumFields(int)} must be called first diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/RestDeleteExpiredDataAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/RestDeleteExpiredDataAction.java index 784ccd4d2ed85..86a8eb683ecb0 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/RestDeleteExpiredDataAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/RestDeleteExpiredDataAction.java @@ -27,9 +27,11 @@ public class RestDeleteExpiredDataAction extends BaseRestHandler { public List routes() { return List.of( Route.builder(DELETE, BASE_PATH + "_delete_expired_data/{" + Job.ID + "}") - .replaces(DELETE, PRE_V7_BASE_PATH + "_delete_expired_data/{" + Job.ID + "}", RestApiVersion.V_7).build(), + .replaces(DELETE, PRE_V7_BASE_PATH + "_delete_expired_data/{" + Job.ID + "}", RestApiVersion.V_7) + .build(), Route.builder(DELETE, BASE_PATH + "_delete_expired_data") - .replaces(DELETE, PRE_V7_BASE_PATH + "_delete_expired_data", RestApiVersion.V_7).build() + .replaces(DELETE, PRE_V7_BASE_PATH + "_delete_expired_data", RestApiVersion.V_7) + .build() ); } @@ -54,9 +56,14 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient try { request.setRequestsPerSecond(Float.parseFloat(perSecondParam)); } catch (NumberFormatException e) { - throw new IllegalArgumentException("Failed to parse float parameter [" + - DeleteExpiredDataAction.Request.REQUESTS_PER_SECOND.getPreferredName() + - "] with value [" + perSecondParam + "]", e); + throw new IllegalArgumentException( + "Failed to parse float parameter [" + + DeleteExpiredDataAction.Request.REQUESTS_PER_SECOND.getPreferredName() + + "] with value [" + + perSecondParam + + "]", + e + ); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/RestMlInfoAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/RestMlInfoAction.java index c144bbeab40b1..29f82b2e57655 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/RestMlInfoAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/RestMlInfoAction.java @@ -24,10 +24,7 @@ public class RestMlInfoAction extends BaseRestHandler { @Override public List routes() { - return List.of( - Route.builder(GET, BASE_PATH + "info") - .replaces(GET, PRE_V7_BASE_PATH + "info", RestApiVersion.V_7).build() - ); + return List.of(Route.builder(GET, BASE_PATH + "info").replaces(GET, PRE_V7_BASE_PATH + "info", RestApiVersion.V_7).build()); } @Override diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/RestSetUpgradeModeAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/RestSetUpgradeModeAction.java index d78dbfde26a80..11097851ae7d5 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/RestSetUpgradeModeAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/RestSetUpgradeModeAction.java @@ -26,7 +26,8 @@ public class RestSetUpgradeModeAction extends BaseRestHandler { public List routes() { return List.of( Route.builder(POST, BASE_PATH + "set_upgrade_mode") - .replaces(POST, PRE_V7_BASE_PATH + "set_upgrade_mode", RestApiVersion.V_7).build() + .replaces(POST, PRE_V7_BASE_PATH + "set_upgrade_mode", RestApiVersion.V_7) + .build() ); } @@ -37,8 +38,7 @@ public String getName() { @Override protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) throws IOException { - SetUpgradeModeAction.Request request = - new SetUpgradeModeAction.Request(restRequest.paramAsBoolean("enabled", false)); + SetUpgradeModeAction.Request request = new SetUpgradeModeAction.Request(restRequest.paramAsBoolean("enabled", false)); request.timeout(restRequest.paramAsTime("timeout", request.timeout())); request.masterNodeTimeout(restRequest.paramAsTime("master_timeout", request.masterNodeTimeout())); return channel -> client.execute(SetUpgradeModeAction.INSTANCE, request, new RestToXContentListener<>(channel)); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/calendar/RestDeleteCalendarAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/calendar/RestDeleteCalendarAction.java index adc283f21a1d1..0f37815e037eb 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/calendar/RestDeleteCalendarAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/calendar/RestDeleteCalendarAction.java @@ -27,7 +27,8 @@ public class RestDeleteCalendarAction extends BaseRestHandler { public List routes() { return List.of( Route.builder(DELETE, BASE_PATH + "calendars/{" + Calendar.ID + "}") - .replaces(DELETE, PRE_V7_BASE_PATH + "calendars/{" + Calendar.ID + "}", RestApiVersion.V_7).build() + .replaces(DELETE, PRE_V7_BASE_PATH + "calendars/{" + Calendar.ID + "}", RestApiVersion.V_7) + .build() ); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/calendar/RestDeleteCalendarEventAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/calendar/RestDeleteCalendarEventAction.java index e169f1d7c5804..b962f5027f39f 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/calendar/RestDeleteCalendarEventAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/calendar/RestDeleteCalendarEventAction.java @@ -24,13 +24,16 @@ public class RestDeleteCalendarEventAction extends BaseRestHandler { - @Override public List routes() { return List.of( Route.builder(DELETE, BASE_PATH + "calendars/{" + Calendar.ID + "}/events/{" + ScheduledEvent.EVENT_ID + "}") - .replaces(DELETE, PRE_V7_BASE_PATH + "calendars/{" + Calendar.ID + "}/events/{" + ScheduledEvent.EVENT_ID + "}", - RestApiVersion.V_7).build() + .replaces( + DELETE, + PRE_V7_BASE_PATH + "calendars/{" + Calendar.ID + "}/events/{" + ScheduledEvent.EVENT_ID + "}", + RestApiVersion.V_7 + ) + .build() ); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/calendar/RestDeleteCalendarJobAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/calendar/RestDeleteCalendarJobAction.java index 6f072a8939ffb..bec9eb3f46ebd 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/calendar/RestDeleteCalendarJobAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/calendar/RestDeleteCalendarJobAction.java @@ -28,7 +28,8 @@ public class RestDeleteCalendarJobAction extends BaseRestHandler { public List routes() { return List.of( Route.builder(DELETE, BASE_PATH + "calendars/{" + Calendar.ID + "}/jobs/{" + Job.ID + "}") - .replaces(DELETE, PRE_V7_BASE_PATH + "calendars/{" + Calendar.ID + "}/jobs/{" + Job.ID + "}", RestApiVersion.V_7).build() + .replaces(DELETE, PRE_V7_BASE_PATH + "calendars/{" + Calendar.ID + "}/jobs/{" + Job.ID + "}", RestApiVersion.V_7) + .build() ); } @@ -41,8 +42,7 @@ public String getName() { protected BaseRestHandler.RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) throws IOException { String calendarId = restRequest.param(Calendar.ID.getPreferredName()); String jobId = restRequest.param(Job.ID.getPreferredName()); - UpdateCalendarJobAction.Request request = - new UpdateCalendarJobAction.Request(calendarId, null, jobId); + UpdateCalendarJobAction.Request request = new UpdateCalendarJobAction.Request(calendarId, null, jobId); return channel -> client.execute(UpdateCalendarJobAction.INSTANCE, request, new RestToXContentListener<>(channel)); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/calendar/RestGetCalendarEventsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/calendar/RestGetCalendarEventsAction.java index f545fa1ad6809..fefe9017a1034 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/calendar/RestGetCalendarEventsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/calendar/RestGetCalendarEventsAction.java @@ -7,11 +7,11 @@ package org.elasticsearch.xpack.ml.rest.calendar; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestToXContentListener; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.action.util.PageParams; import org.elasticsearch.xpack.core.ml.action.GetCalendarEventsAction; import org.elasticsearch.xpack.core.ml.calendars.Calendar; @@ -30,7 +30,8 @@ public class RestGetCalendarEventsAction extends BaseRestHandler { public List routes() { return List.of( Route.builder(GET, BASE_PATH + "calendars/{" + Calendar.ID + "}/events") - .replaces(GET, PRE_V7_BASE_PATH + "calendars/{" + Calendar.ID + "}/events", RestApiVersion.V_7).build() + .replaces(GET, PRE_V7_BASE_PATH + "calendars/{" + Calendar.ID + "}/events", RestApiVersion.V_7) + .build() ); } @@ -56,8 +57,12 @@ protected BaseRestHandler.RestChannelConsumer prepareRequest(RestRequest restReq request.setJobId(restRequest.param(Job.ID.getPreferredName(), null)); if (restRequest.hasParam(PageParams.FROM.getPreferredName()) || restRequest.hasParam(PageParams.SIZE.getPreferredName())) { - request.setPageParams(new PageParams(restRequest.paramAsInt(PageParams.FROM.getPreferredName(), PageParams.DEFAULT_FROM), - restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE))); + request.setPageParams( + new PageParams( + restRequest.paramAsInt(PageParams.FROM.getPreferredName(), PageParams.DEFAULT_FROM), + restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE) + ) + ); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/calendar/RestGetCalendarsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/calendar/RestGetCalendarsAction.java index 9e109872b79bc..aa910a9ed1c70 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/calendar/RestGetCalendarsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/calendar/RestGetCalendarsAction.java @@ -8,11 +8,11 @@ import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.common.Strings; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestStatusToXContentListener; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.action.util.PageParams; import org.elasticsearch.xpack.core.ml.action.GetCalendarsAction; import org.elasticsearch.xpack.core.ml.calendars.Calendar; @@ -31,13 +31,13 @@ public class RestGetCalendarsAction extends BaseRestHandler { public List routes() { return List.of( Route.builder(GET, BASE_PATH + "calendars/{" + Calendar.ID + "}") - .replaces(GET, PRE_V7_BASE_PATH + "calendars/{" + Calendar.ID + "}", RestApiVersion.V_7).build(), - Route.builder(GET, BASE_PATH + "calendars/") - .replaces(GET, PRE_V7_BASE_PATH + "calendars/", RestApiVersion.V_7).build(), + .replaces(GET, PRE_V7_BASE_PATH + "calendars/{" + Calendar.ID + "}", RestApiVersion.V_7) + .build(), + Route.builder(GET, BASE_PATH + "calendars/").replaces(GET, PRE_V7_BASE_PATH + "calendars/", RestApiVersion.V_7).build(), Route.builder(POST, BASE_PATH + "calendars/{" + Calendar.ID + "}") - .replaces(POST, PRE_V7_BASE_PATH + "calendars/{" + Calendar.ID + "}", RestApiVersion.V_7).build(), - Route.builder(POST, BASE_PATH + "calendars/") - .replaces(POST, PRE_V7_BASE_PATH + "calendars/", RestApiVersion.V_7).build() + .replaces(POST, PRE_V7_BASE_PATH + "calendars/{" + Calendar.ID + "}", RestApiVersion.V_7) + .build(), + Route.builder(POST, BASE_PATH + "calendars/").replaces(POST, PRE_V7_BASE_PATH + "calendars/", RestApiVersion.V_7).build() ); } @@ -56,14 +56,18 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient try (XContentParser parser = restRequest.contentOrSourceParamParser()) { request = GetCalendarsAction.Request.parseRequest(calendarId, parser); } - } else { + } else { request = new GetCalendarsAction.Request(); if (Strings.isNullOrEmpty(calendarId) == false) { request.setCalendarId(calendarId); } if (restRequest.hasParam(PageParams.FROM.getPreferredName()) || restRequest.hasParam(PageParams.SIZE.getPreferredName())) { - request.setPageParams(new PageParams(restRequest.paramAsInt(PageParams.FROM.getPreferredName(), PageParams.DEFAULT_FROM), - restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE))); + request.setPageParams( + new PageParams( + restRequest.paramAsInt(PageParams.FROM.getPreferredName(), PageParams.DEFAULT_FROM), + restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE) + ) + ); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/calendar/RestPostCalendarEventAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/calendar/RestPostCalendarEventAction.java index 2c2c866ef79a3..11f9b98207d3a 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/calendar/RestPostCalendarEventAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/calendar/RestPostCalendarEventAction.java @@ -7,11 +7,11 @@ package org.elasticsearch.xpack.ml.rest.calendar; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestToXContentListener; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.PostCalendarEventsAction; import org.elasticsearch.xpack.core.ml.calendars.Calendar; @@ -28,7 +28,8 @@ public class RestPostCalendarEventAction extends BaseRestHandler { public List routes() { return List.of( Route.builder(POST, BASE_PATH + "calendars/{" + Calendar.ID + "}/events") - .replaces(POST, PRE_V7_BASE_PATH + "calendars/{" + Calendar.ID + "}/events", RestApiVersion.V_7).build() + .replaces(POST, PRE_V7_BASE_PATH + "calendars/{" + Calendar.ID + "}/events", RestApiVersion.V_7) + .build() ); } @@ -42,8 +43,7 @@ protected BaseRestHandler.RestChannelConsumer prepareRequest(RestRequest restReq String calendarId = restRequest.param(Calendar.ID.getPreferredName()); XContentParser parser = restRequest.contentOrSourceParamParser(); - PostCalendarEventsAction.Request request = - PostCalendarEventsAction.Request.parseRequest(calendarId, parser); + PostCalendarEventsAction.Request request = PostCalendarEventsAction.Request.parseRequest(calendarId, parser); return channel -> client.execute(PostCalendarEventsAction.INSTANCE, request, new RestToXContentListener<>(channel)); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/calendar/RestPutCalendarAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/calendar/RestPutCalendarAction.java index b11857ad15108..6cdca33329bd9 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/calendar/RestPutCalendarAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/calendar/RestPutCalendarAction.java @@ -7,11 +7,11 @@ package org.elasticsearch.xpack.ml.rest.calendar; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestToXContentListener; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.PutCalendarAction; import org.elasticsearch.xpack.core.ml.calendars.Calendar; @@ -29,7 +29,8 @@ public class RestPutCalendarAction extends BaseRestHandler { public List routes() { return List.of( Route.builder(PUT, BASE_PATH + "calendars/{" + Calendar.ID + "}") - .replaces(PUT, PRE_V7_BASE_PATH + "calendars/{" + Calendar.ID + "}", RestApiVersion.V_7).build() + .replaces(PUT, PRE_V7_BASE_PATH + "calendars/{" + Calendar.ID + "}", RestApiVersion.V_7) + .build() ); } @@ -54,4 +55,3 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient return channel -> client.execute(PutCalendarAction.INSTANCE, putCalendarRequest, new RestToXContentListener<>(channel)); } } - diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/calendar/RestPutCalendarJobAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/calendar/RestPutCalendarJobAction.java index 3c19e451c0158..f0665cb6ad93d 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/calendar/RestPutCalendarJobAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/calendar/RestPutCalendarJobAction.java @@ -28,7 +28,8 @@ public class RestPutCalendarJobAction extends BaseRestHandler { public List routes() { return List.of( Route.builder(PUT, BASE_PATH + "calendars/{" + Calendar.ID + "}/jobs/{" + Job.ID + "}") - .replaces(PUT, PRE_V7_BASE_PATH + "calendars/{" + Calendar.ID + "}/jobs/{" + Job.ID + "}", RestApiVersion.V_7).build() + .replaces(PUT, PRE_V7_BASE_PATH + "calendars/{" + Calendar.ID + "}/jobs/{" + Job.ID + "}", RestApiVersion.V_7) + .build() ); } @@ -41,8 +42,7 @@ public String getName() { protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) throws IOException { String calendarId = restRequest.param(Calendar.ID.getPreferredName()); String jobId = restRequest.param(Job.ID.getPreferredName()); - UpdateCalendarJobAction.Request putCalendarRequest = - new UpdateCalendarJobAction.Request(calendarId, jobId, null); + UpdateCalendarJobAction.Request putCalendarRequest = new UpdateCalendarJobAction.Request(calendarId, jobId, null); return channel -> client.execute(UpdateCalendarJobAction.INSTANCE, putCalendarRequest, new RestToXContentListener<>(channel)); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/cat/RestCatDataFrameAnalyticsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/cat/RestCatDataFrameAnalyticsAction.java index 8ed7132c9b9dd..be664d8d66d43 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/cat/RestCatDataFrameAnalyticsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/cat/RestCatDataFrameAnalyticsAction.java @@ -57,7 +57,10 @@ protected RestChannelConsumer doCatRequest(RestRequest restRequest, NodeClient c GetDataFrameAnalyticsAction.Request getRequest = new GetDataFrameAnalyticsAction.Request(dataFrameAnalyticsId); getRequest.setAllowNoResources( restRequest.paramAsBoolean( - GetDataFrameAnalyticsAction.Request.ALLOW_NO_MATCH.getPreferredName(), getRequest.isAllowNoResources())); + GetDataFrameAnalyticsAction.Request.ALLOW_NO_MATCH.getPreferredName(), + getRequest.isAllowNoResources() + ) + ); GetDataFrameAnalyticsStatsAction.Request getStatsRequest = new GetDataFrameAnalyticsStatsAction.Request(dataFrameAnalyticsId); getStatsRequest.setAllowNoMatch(true); @@ -87,85 +90,70 @@ protected Table getTableWithHeader(RestRequest unused) { } private static Table getTableWithHeader() { - return new Table() - .startHeaders() + return new Table().startHeaders() // DFA config info .addCell("id", TableColumnAttributeBuilder.builder("the id").build()) - .addCell("type", - TableColumnAttributeBuilder.builder("analysis type") - .setAliases("t") - .build()) - .addCell("create_time", - TableColumnAttributeBuilder.builder("job creation time") - .setAliases("ct", "createTime") - .build()) - .addCell("version", + .addCell("type", TableColumnAttributeBuilder.builder("analysis type").setAliases("t").build()) + .addCell("create_time", TableColumnAttributeBuilder.builder("job creation time").setAliases("ct", "createTime").build()) + .addCell( + "version", TableColumnAttributeBuilder.builder("the version of Elasticsearch when the analytics was created", false) .setAliases("v") - .build()) - .addCell("source_index", - TableColumnAttributeBuilder.builder("source index", false) - .setAliases("si", "sourceIndex") - .build()) - .addCell("dest_index", - TableColumnAttributeBuilder.builder("destination index", false) - .setAliases("di", "destIndex") - .build()) - .addCell("description", - TableColumnAttributeBuilder.builder("description", false) - .setAliases("d") - .build()) - .addCell("model_memory_limit", - TableColumnAttributeBuilder.builder("model memory limit", false) - .setAliases("mml", "modelMemoryLimit") - .build()) + .build() + ) + .addCell("source_index", TableColumnAttributeBuilder.builder("source index", false).setAliases("si", "sourceIndex").build()) + .addCell("dest_index", TableColumnAttributeBuilder.builder("destination index", false).setAliases("di", "destIndex").build()) + .addCell("description", TableColumnAttributeBuilder.builder("description", false).setAliases("d").build()) + .addCell( + "model_memory_limit", + TableColumnAttributeBuilder.builder("model memory limit", false).setAliases("mml", "modelMemoryLimit").build() + ) // DFA stats info - .addCell("state", + .addCell( + "state", TableColumnAttributeBuilder.builder("job state") .setAliases("s") .setTextAlignment(TableColumnAttributeBuilder.TextAlign.RIGHT) - .build()) - .addCell("failure_reason", - TableColumnAttributeBuilder.builder("failure reason", false) - .setAliases("fr", "failureReason") - .build()) - .addCell("progress", - TableColumnAttributeBuilder.builder("progress", false) - .setAliases("p") - .build()) - .addCell("assignment_explanation", + .build() + ) + .addCell( + "failure_reason", + TableColumnAttributeBuilder.builder("failure reason", false).setAliases("fr", "failureReason").build() + ) + .addCell("progress", TableColumnAttributeBuilder.builder("progress", false).setAliases("p").build()) + .addCell( + "assignment_explanation", TableColumnAttributeBuilder.builder("why the job is or is not assigned to a node", false) .setAliases("ae", "assignmentExplanation") - .build()) + .build() + ) // Node info - .addCell("node.id", - TableColumnAttributeBuilder.builder("id of the assigned node", false) - .setAliases("ni", "nodeId") - .build()) - .addCell("node.name", - TableColumnAttributeBuilder.builder("name of the assigned node", false) - .setAliases("nn", "nodeName") - .build()) - .addCell("node.ephemeral_id", - TableColumnAttributeBuilder.builder("ephemeral id of the assigned node", false) - .setAliases("ne", "nodeEphemeralId") - .build()) - .addCell("node.address", - TableColumnAttributeBuilder.builder("network address of the assigned node", false) - .setAliases("na", "nodeAddress") - .build()) + .addCell("node.id", TableColumnAttributeBuilder.builder("id of the assigned node", false).setAliases("ni", "nodeId").build()) + .addCell( + "node.name", + TableColumnAttributeBuilder.builder("name of the assigned node", false).setAliases("nn", "nodeName").build() + ) + .addCell( + "node.ephemeral_id", + TableColumnAttributeBuilder.builder("ephemeral id of the assigned node", false).setAliases("ne", "nodeEphemeralId").build() + ) + .addCell( + "node.address", + TableColumnAttributeBuilder.builder("network address of the assigned node", false).setAliases("na", "nodeAddress").build() + ) .endHeaders(); } - private static Table buildTable(GetDataFrameAnalyticsAction.Response getResponse, - GetDataFrameAnalyticsStatsAction.Response getStatsResponse) { + private static Table buildTable( + GetDataFrameAnalyticsAction.Response getResponse, + GetDataFrameAnalyticsStatsAction.Response getStatsResponse + ) { Map statsById = getStatsResponse.getResponse().results().stream().collect(toMap(Stats::getId, Function.identity())); Table table = getTableWithHeader(); for (DataFrameAnalyticsConfig config : getResponse.getResources().results()) { Stats stats = statsById.get(config.getId()); DiscoveryNode node = stats == null ? null : stats.getNode(); - table - .startRow() + table.startRow() .addCell(config.getId()) .addCell(config.getAnalysis().getWriteableName()) .addCell(config.getCreateTime()) diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/cat/RestCatDatafeedsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/cat/RestCatDatafeedsAction.java index 61581149aa7d1..05f1e2b4cbb8c 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/cat/RestCatDatafeedsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/cat/RestCatDatafeedsAction.java @@ -11,13 +11,13 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.Table; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; -import org.elasticsearch.xpack.core.common.table.TableColumnAttributeBuilder; import org.elasticsearch.core.TimeValue; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.action.RestResponseListener; import org.elasticsearch.rest.action.cat.AbstractCatAction; import org.elasticsearch.rest.action.cat.RestTable; +import org.elasticsearch.xpack.core.common.table.TableColumnAttributeBuilder; import org.elasticsearch.xpack.core.ml.action.GetDatafeedsStatsAction; import org.elasticsearch.xpack.core.ml.action.GetDatafeedsStatsAction.Request; import org.elasticsearch.xpack.core.ml.action.GetDatafeedsStatsAction.Response; @@ -32,9 +32,7 @@ public class RestCatDatafeedsAction extends AbstractCatAction { @Override public List routes() { - return List.of( - new Route(GET, "_cat/ml/datafeeds/{" + DatafeedConfig.ID + "}"), - new Route(GET, "_cat/ml/datafeeds")); + return List.of(new Route(GET, "_cat/ml/datafeeds/{" + DatafeedConfig.ID + "}"), new Route(GET, "_cat/ml/datafeeds")); } @Override @@ -55,7 +53,9 @@ protected RestChannelConsumer doCatRequest(RestRequest restRequest, NodeClient c request.setAllowNoMatch( restRequest.paramAsBoolean( Request.ALLOW_NO_MATCH, - restRequest.paramAsBoolean(Request.ALLOW_NO_DATAFEEDS, request.allowNoMatch()))); + restRequest.paramAsBoolean(Request.ALLOW_NO_DATAFEEDS, request.allowNoMatch()) + ) + ); return channel -> client.execute(GetDatafeedsStatsAction.INSTANCE, request, new RestResponseListener<>(channel) { @Override public RestResponse buildResponse(Response getDatafeedsStatsRespons) throws Exception { @@ -77,55 +77,55 @@ protected Table getTableWithHeader(RestRequest request) { // Datafeed Info table.addCell("id", TableColumnAttributeBuilder.builder("the datafeed_id").build()); - table.addCell("state", + table.addCell( + "state", TableColumnAttributeBuilder.builder("the datafeed state") .setAliases("s") .setTextAlignment(TableColumnAttributeBuilder.TextAlign.RIGHT) - .build()); - table.addCell("assignment_explanation", - TableColumnAttributeBuilder.builder("why the datafeed is or is not assigned to a node", false) - .setAliases("ae") - .build()); + .build() + ); + table.addCell( + "assignment_explanation", + TableColumnAttributeBuilder.builder("why the datafeed is or is not assigned to a node", false).setAliases("ae").build() + ); // Timing stats - table.addCell("buckets.count", - TableColumnAttributeBuilder.builder("bucket count") - .setAliases("bc", "bucketsCount") - .build()); - table.addCell("search.count", - TableColumnAttributeBuilder.builder("number of searches ran by the datafeed") - .setAliases("sc", "searchCount") - .build()); - table.addCell("search.time", - TableColumnAttributeBuilder.builder("the total search time", false) - .setAliases("st", "searchTime") - .build()); - table.addCell("search.bucket_avg", + table.addCell("buckets.count", TableColumnAttributeBuilder.builder("bucket count").setAliases("bc", "bucketsCount").build()); + table.addCell( + "search.count", + TableColumnAttributeBuilder.builder("number of searches ran by the datafeed").setAliases("sc", "searchCount").build() + ); + table.addCell( + "search.time", + TableColumnAttributeBuilder.builder("the total search time", false).setAliases("st", "searchTime").build() + ); + table.addCell( + "search.bucket_avg", TableColumnAttributeBuilder.builder("the average search time per bucket (millisecond)", false) .setAliases("sba", "searchBucketAvg") - .build()); - table.addCell("search.exp_avg_hour", + .build() + ); + table.addCell( + "search.exp_avg_hour", TableColumnAttributeBuilder.builder("the exponential average search time per hour (millisecond)", false) .setAliases("seah", "searchExpAvgHour") - .build()); - - //Node info - table.addCell("node.id", - TableColumnAttributeBuilder.builder("id of the assigned node", false) - .setAliases("ni", "nodeId") - .build()); - table.addCell("node.name", - TableColumnAttributeBuilder.builder("name of the assigned node", false) - .setAliases("nn", "nodeName") - .build()); - table.addCell("node.ephemeral_id", - TableColumnAttributeBuilder.builder("ephemeral id of the assigned node", false) - .setAliases("ne", "nodeEphemeralId") - .build()); - table.addCell("node.address", - TableColumnAttributeBuilder.builder("network address of the assigned node", false) - .setAliases("na", "nodeAddress") - .build()); + .build() + ); + + // Node info + table.addCell("node.id", TableColumnAttributeBuilder.builder("id of the assigned node", false).setAliases("ni", "nodeId").build()); + table.addCell( + "node.name", + TableColumnAttributeBuilder.builder("name of the assigned node", false).setAliases("nn", "nodeName").build() + ); + table.addCell( + "node.ephemeral_id", + TableColumnAttributeBuilder.builder("ephemeral id of the assigned node", false).setAliases("ne", "nodeEphemeralId").build() + ); + table.addCell( + "node.address", + TableColumnAttributeBuilder.builder("network address of the assigned node", false).setAliases("na", "nodeAddress").build() + ); table.endHeaders(); return table; @@ -142,9 +142,9 @@ private Table buildTable(RestRequest request, Response dfStats) { DatafeedTimingStats timingStats = df.getTimingStats(); table.addCell(timingStats == null ? 0 : timingStats.getBucketCount()); table.addCell(timingStats == null ? 0 : timingStats.getSearchCount()); - table.addCell(timingStats == null ? - TimeValue.timeValueMillis(0) : - TimeValue.timeValueMillis((long)timingStats.getTotalSearchTimeMs())); + table.addCell( + timingStats == null ? TimeValue.timeValueMillis(0) : TimeValue.timeValueMillis((long) timingStats.getTotalSearchTimeMs()) + ); table.addCell(timingStats == null || timingStats.getBucketCount() == 0 ? 0.0 : timingStats.getAvgSearchTimePerBucketMs()); table.addCell(timingStats == null ? 0.0 : timingStats.getExponentialAvgSearchTimePerHourMs()); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/cat/RestCatJobsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/cat/RestCatJobsAction.java index df18a2813656e..4073db45c14b5 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/cat/RestCatJobsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/cat/RestCatJobsAction.java @@ -11,15 +11,15 @@ import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.common.Strings; import org.elasticsearch.common.Table; -import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; -import org.elasticsearch.xpack.core.common.table.TableColumnAttributeBuilder; import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; import org.elasticsearch.core.TimeValue; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.action.RestResponseListener; import org.elasticsearch.rest.action.cat.AbstractCatAction; import org.elasticsearch.rest.action.cat.RestTable; +import org.elasticsearch.xpack.core.common.table.TableColumnAttributeBuilder; import org.elasticsearch.xpack.core.ml.action.GetJobsStatsAction; import org.elasticsearch.xpack.core.ml.action.GetJobsStatsAction.Request; import org.elasticsearch.xpack.core.ml.action.GetJobsStatsAction.Response; @@ -37,9 +37,7 @@ public class RestCatJobsAction extends AbstractCatAction { @Override public List routes() { - return List.of( - new Route(GET, "_cat/ml/anomaly_detectors/{" + Job.ID + "}"), - new Route(GET, "_cat/ml/anomaly_detectors")); + return List.of(new Route(GET, "_cat/ml/anomaly_detectors/{" + Job.ID + "}"), new Route(GET, "_cat/ml/anomaly_detectors")); } @Override @@ -58,9 +56,8 @@ protected RestChannelConsumer doCatRequest(RestRequest restRequest, NodeClient c LoggingDeprecationHandler.INSTANCE.logRenamedField(null, () -> null, Request.ALLOW_NO_JOBS, Request.ALLOW_NO_MATCH); } request.setAllowNoMatch( - restRequest.paramAsBoolean( - Request.ALLOW_NO_MATCH, - restRequest.paramAsBoolean(Request.ALLOW_NO_JOBS, request.allowNoMatch()))); + restRequest.paramAsBoolean(Request.ALLOW_NO_MATCH, restRequest.paramAsBoolean(Request.ALLOW_NO_JOBS, request.allowNoMatch())) + ); return channel -> client.execute(GetJobsStatsAction.INSTANCE, request, new RestResponseListener<>(channel) { @Override public RestResponse buildResponse(Response getJobStatsResponse) throws Exception { @@ -82,250 +79,286 @@ protected Table getTableWithHeader(RestRequest request) { // Job Info table.addCell("id", TableColumnAttributeBuilder.builder("the job_id").build()); - table.addCell("state", + table.addCell( + "state", TableColumnAttributeBuilder.builder("the job state") .setAliases("s") .setTextAlignment(TableColumnAttributeBuilder.TextAlign.RIGHT) - .build()); - table.addCell("opened_time", - TableColumnAttributeBuilder.builder("the amount of time the job has been opened", false) - .setAliases("ot") - .build()); - table.addCell("assignment_explanation", - TableColumnAttributeBuilder.builder("why the job is or is not assigned to a node", false) - .setAliases("ae") - .build()); + .build() + ); + table.addCell( + "opened_time", + TableColumnAttributeBuilder.builder("the amount of time the job has been opened", false).setAliases("ot").build() + ); + table.addCell( + "assignment_explanation", + TableColumnAttributeBuilder.builder("why the job is or is not assigned to a node", false).setAliases("ae").build() + ); // Data Counts - table.addCell("data.processed_records", - TableColumnAttributeBuilder.builder("number of processed records") - .setAliases("dpr", "dataProcessedRecords") - .build()); - table.addCell("data.processed_fields", - TableColumnAttributeBuilder.builder("number of processed fields", false) - .setAliases("dpf", "dataProcessedFields") - .build()); - table.addCell("data.input_bytes", - TableColumnAttributeBuilder.builder("total input bytes", false) - .setAliases("dib", "dataInputBytes") - .build()); - table.addCell("data.input_records", - TableColumnAttributeBuilder.builder("total record count", false) - .setAliases("dir", "dataInputRecords") - .build()); - table.addCell("data.input_fields", - TableColumnAttributeBuilder.builder("total field count", false) - .setAliases("dif", "dataInputFields") - .build()); - table.addCell("data.invalid_dates", - TableColumnAttributeBuilder.builder("number of records with invalid dates", false) - .setAliases("did", "dataInvalidDates") - .build()); - table.addCell("data.missing_fields", + table.addCell( + "data.processed_records", + TableColumnAttributeBuilder.builder("number of processed records").setAliases("dpr", "dataProcessedRecords").build() + ); + table.addCell( + "data.processed_fields", + TableColumnAttributeBuilder.builder("number of processed fields", false).setAliases("dpf", "dataProcessedFields").build() + ); + table.addCell( + "data.input_bytes", + TableColumnAttributeBuilder.builder("total input bytes", false).setAliases("dib", "dataInputBytes").build() + ); + table.addCell( + "data.input_records", + TableColumnAttributeBuilder.builder("total record count", false).setAliases("dir", "dataInputRecords").build() + ); + table.addCell( + "data.input_fields", + TableColumnAttributeBuilder.builder("total field count", false).setAliases("dif", "dataInputFields").build() + ); + table.addCell( + "data.invalid_dates", + TableColumnAttributeBuilder.builder("number of records with invalid dates", false).setAliases("did", "dataInvalidDates").build() + ); + table.addCell( + "data.missing_fields", TableColumnAttributeBuilder.builder("number of records with missing fields", false) .setAliases("dmf", "dataMissingFields") - .build()); - table.addCell("data.out_of_order_timestamps", + .build() + ); + table.addCell( + "data.out_of_order_timestamps", TableColumnAttributeBuilder.builder("number of records handled out of order", false) .setAliases("doot", "dataOutOfOrderTimestamps") - .build()); - table.addCell("data.empty_buckets", - TableColumnAttributeBuilder.builder("number of empty buckets", false) - .setAliases("deb", "dataEmptyBuckets") - .build()); - table.addCell("data.sparse_buckets", - TableColumnAttributeBuilder.builder("number of sparse buckets", false) - .setAliases("dsb", "dataSparseBuckets") - .build()); - table.addCell("data.buckets", - TableColumnAttributeBuilder.builder("total bucket count", false) - .setAliases("db", "dataBuckets") - .build()); - table.addCell("data.earliest_record", - TableColumnAttributeBuilder.builder("earliest record time", false) - .setAliases("der", "dataEarliestRecord") - .build()); - table.addCell("data.latest_record", - TableColumnAttributeBuilder.builder("latest record time", false) - .setAliases("dlr", "dataLatestRecord") - .build()); - table.addCell("data.last", - TableColumnAttributeBuilder.builder("last time data was seen", false) - .setAliases("dl", "dataLast") - .build()); - table.addCell("data.last_empty_bucket", + .build() + ); + table.addCell( + "data.empty_buckets", + TableColumnAttributeBuilder.builder("number of empty buckets", false).setAliases("deb", "dataEmptyBuckets").build() + ); + table.addCell( + "data.sparse_buckets", + TableColumnAttributeBuilder.builder("number of sparse buckets", false).setAliases("dsb", "dataSparseBuckets").build() + ); + table.addCell( + "data.buckets", + TableColumnAttributeBuilder.builder("total bucket count", false).setAliases("db", "dataBuckets").build() + ); + table.addCell( + "data.earliest_record", + TableColumnAttributeBuilder.builder("earliest record time", false).setAliases("der", "dataEarliestRecord").build() + ); + table.addCell( + "data.latest_record", + TableColumnAttributeBuilder.builder("latest record time", false).setAliases("dlr", "dataLatestRecord").build() + ); + table.addCell( + "data.last", + TableColumnAttributeBuilder.builder("last time data was seen", false).setAliases("dl", "dataLast").build() + ); + table.addCell( + "data.last_empty_bucket", TableColumnAttributeBuilder.builder("last time an empty bucket occurred", false) .setAliases("dleb", "dataLastEmptyBucket") - .build()); - table.addCell("data.last_sparse_bucket", + .build() + ); + table.addCell( + "data.last_sparse_bucket", TableColumnAttributeBuilder.builder("last time a sparse bucket occurred", false) .setAliases("dlsb", "dataLastSparseBucket") - .build()); + .build() + ); // Model Size stats - table.addCell("model.bytes", - TableColumnAttributeBuilder.builder("model size").setAliases("mb", "modelBytes").build()); - table.addCell("model.memory_status", + table.addCell("model.bytes", TableColumnAttributeBuilder.builder("model size").setAliases("mb", "modelBytes").build()); + table.addCell( + "model.memory_status", TableColumnAttributeBuilder.builder("current memory status") .setAliases("mms", "modelMemoryStatus") .setTextAlignment(TableColumnAttributeBuilder.TextAlign.RIGHT) - .build()); - table.addCell("model.bytes_exceeded", + .build() + ); + table.addCell( + "model.bytes_exceeded", TableColumnAttributeBuilder.builder("how much the model has exceeded the limit", false) .setAliases("mbe", "modelBytesExceeded") - .build()); - table.addCell("model.memory_limit", - TableColumnAttributeBuilder.builder("model memory limit", false) - .setAliases("mml", "modelMemoryLimit") - .build()); - table.addCell("model.by_fields", - TableColumnAttributeBuilder.builder("count of 'by' fields", false) - .setAliases("mbf", "modelByFields") - .build()); - table.addCell("model.over_fields", - TableColumnAttributeBuilder.builder("count of 'over' fields", false) - .setAliases("mof", "modelOverFields") - .build()); - table.addCell("model.partition_fields", - TableColumnAttributeBuilder.builder("count of 'partition' fields", false) - .setAliases("mpf", "modelPartitionFields") - .build()); - table.addCell("model.bucket_allocation_failures", + .build() + ); + table.addCell( + "model.memory_limit", + TableColumnAttributeBuilder.builder("model memory limit", false).setAliases("mml", "modelMemoryLimit").build() + ); + table.addCell( + "model.by_fields", + TableColumnAttributeBuilder.builder("count of 'by' fields", false).setAliases("mbf", "modelByFields").build() + ); + table.addCell( + "model.over_fields", + TableColumnAttributeBuilder.builder("count of 'over' fields", false).setAliases("mof", "modelOverFields").build() + ); + table.addCell( + "model.partition_fields", + TableColumnAttributeBuilder.builder("count of 'partition' fields", false).setAliases("mpf", "modelPartitionFields").build() + ); + table.addCell( + "model.bucket_allocation_failures", TableColumnAttributeBuilder.builder("number of bucket allocation failures", false) .setAliases("mbaf", "modelBucketAllocationFailures") - .build()); - table.addCell("model.categorization_status", + .build() + ); + table.addCell( + "model.categorization_status", TableColumnAttributeBuilder.builder("current categorization status", false) .setAliases("mcs", "modelCategorizationStatus") .setTextAlignment(TableColumnAttributeBuilder.TextAlign.RIGHT) - .build()); - table.addCell("model.categorized_doc_count", + .build() + ); + table.addCell( + "model.categorized_doc_count", TableColumnAttributeBuilder.builder("count of categorized documents", false) .setAliases("mcdc", "modelCategorizedDocCount") - .build()); - table.addCell("model.total_category_count", - TableColumnAttributeBuilder.builder("count of categories", false) - .setAliases("mtcc", "modelTotalCategoryCount") - .build()); - table.addCell("model.frequent_category_count", + .build() + ); + table.addCell( + "model.total_category_count", + TableColumnAttributeBuilder.builder("count of categories", false).setAliases("mtcc", "modelTotalCategoryCount").build() + ); + table.addCell( + "model.frequent_category_count", TableColumnAttributeBuilder.builder("count of frequent categories", false) .setAliases("mfcc", "modelFrequentCategoryCount") - .build()); - table.addCell("model.rare_category_count", - TableColumnAttributeBuilder.builder("count of rare categories", false) - .setAliases("mrcc", "modelRareCategoryCount") - .build()); - table.addCell("model.dead_category_count", - TableColumnAttributeBuilder.builder("count of dead categories", false) - .setAliases("mdcc", "modelDeadCategoryCount") - .build()); - table.addCell("model.failed_category_count", - TableColumnAttributeBuilder.builder("count of failed categories", false) - .setAliases("mfcc", "modelFailedCategoryCount") - .build()); - table.addCell("model.log_time", - TableColumnAttributeBuilder.builder("when the model stats were gathered", false) - .setAliases("mlt", "modelLogTime") - .build()); - table.addCell("model.timestamp", + .build() + ); + table.addCell( + "model.rare_category_count", + TableColumnAttributeBuilder.builder("count of rare categories", false).setAliases("mrcc", "modelRareCategoryCount").build() + ); + table.addCell( + "model.dead_category_count", + TableColumnAttributeBuilder.builder("count of dead categories", false).setAliases("mdcc", "modelDeadCategoryCount").build() + ); + table.addCell( + "model.failed_category_count", + TableColumnAttributeBuilder.builder("count of failed categories", false).setAliases("mfcc", "modelFailedCategoryCount").build() + ); + table.addCell( + "model.log_time", + TableColumnAttributeBuilder.builder("when the model stats were gathered", false).setAliases("mlt", "modelLogTime").build() + ); + table.addCell( + "model.timestamp", TableColumnAttributeBuilder.builder("the time of the last record when the model stats were gathered", false) .setAliases("mt", "modelTimestamp") - .build()); + .build() + ); // Forecast Stats - table.addCell("forecasts." + ForecastStats.Fields.TOTAL, - TableColumnAttributeBuilder.builder("total number of forecasts").setAliases("ft", "forecastsTotal").build()); - table.addCell("forecasts.memory.min", - TableColumnAttributeBuilder.builder("minimum memory used by forecasts", false) - .setAliases("fmmin", "forecastsMemoryMin") - .build()); - table.addCell("forecasts.memory.max", - TableColumnAttributeBuilder.builder("maximum memory used by forecasts", false) - .setAliases("fmmax", "forecastsMemoryMax") - .build()); - table.addCell("forecasts.memory.avg", - TableColumnAttributeBuilder.builder("average memory used by forecasts", false) - .setAliases("fmavg", "forecastsMemoryAvg") - .build()); - table.addCell("forecasts.memory.total", + table.addCell( + "forecasts." + ForecastStats.Fields.TOTAL, + TableColumnAttributeBuilder.builder("total number of forecasts").setAliases("ft", "forecastsTotal").build() + ); + table.addCell( + "forecasts.memory.min", + TableColumnAttributeBuilder.builder("minimum memory used by forecasts", false).setAliases("fmmin", "forecastsMemoryMin").build() + ); + table.addCell( + "forecasts.memory.max", + TableColumnAttributeBuilder.builder("maximum memory used by forecasts", false).setAliases("fmmax", "forecastsMemoryMax").build() + ); + table.addCell( + "forecasts.memory.avg", + TableColumnAttributeBuilder.builder("average memory used by forecasts", false).setAliases("fmavg", "forecastsMemoryAvg").build() + ); + table.addCell( + "forecasts.memory.total", TableColumnAttributeBuilder.builder("total memory used by all forecasts", false) .setAliases("fmt", "forecastsMemoryTotal") - .build()); - table.addCell("forecasts." + ForecastStats.Fields.RECORDS + ".min", + .build() + ); + table.addCell( + "forecasts." + ForecastStats.Fields.RECORDS + ".min", TableColumnAttributeBuilder.builder("minimum record count for forecasts", false) .setAliases("frmin", "forecastsRecordsMin") - .build()); - table.addCell("forecasts." + ForecastStats.Fields.RECORDS + ".max", + .build() + ); + table.addCell( + "forecasts." + ForecastStats.Fields.RECORDS + ".max", TableColumnAttributeBuilder.builder("maximum record count for forecasts", false) .setAliases("frmax", "forecastsRecordsMax") - .build()); - table.addCell("forecasts." + ForecastStats.Fields.RECORDS + ".avg", + .build() + ); + table.addCell( + "forecasts." + ForecastStats.Fields.RECORDS + ".avg", TableColumnAttributeBuilder.builder("average record count for forecasts", false) .setAliases("fravg", "forecastsRecordsAvg") - .build()); - table.addCell("forecasts." + ForecastStats.Fields.RECORDS + ".total", + .build() + ); + table.addCell( + "forecasts." + ForecastStats.Fields.RECORDS + ".total", TableColumnAttributeBuilder.builder("total record count for all forecasts", false) .setAliases("frt", "forecastsRecordsTotal") - .build()); - table.addCell("forecasts.time.min", - TableColumnAttributeBuilder.builder("minimum runtime for forecasts", false) - .setAliases("ftmin", "forecastsTimeMin") - .build()); - table.addCell("forecasts.time.max", - TableColumnAttributeBuilder.builder("maximum run time for forecasts", false) - .setAliases("ftmax", "forecastsTimeMax") - .build()); - table.addCell("forecasts.time.avg", + .build() + ); + table.addCell( + "forecasts.time.min", + TableColumnAttributeBuilder.builder("minimum runtime for forecasts", false).setAliases("ftmin", "forecastsTimeMin").build() + ); + table.addCell( + "forecasts.time.max", + TableColumnAttributeBuilder.builder("maximum run time for forecasts", false).setAliases("ftmax", "forecastsTimeMax").build() + ); + table.addCell( + "forecasts.time.avg", TableColumnAttributeBuilder.builder("average runtime for all forecasts (milliseconds)", false) .setAliases("ftavg", "forecastsTimeAvg") - .build()); - table.addCell("forecasts.time.total", - TableColumnAttributeBuilder.builder("total runtime for all forecasts", false) - .setAliases("ftt", "forecastsTimeTotal").build()); + .build() + ); + table.addCell( + "forecasts.time.total", + TableColumnAttributeBuilder.builder("total runtime for all forecasts", false).setAliases("ftt", "forecastsTimeTotal").build() + ); - //Node info - table.addCell("node.id", - TableColumnAttributeBuilder.builder("id of the assigned node", false) - .setAliases("ni", "nodeId") - .build()); - table.addCell("node.name", - TableColumnAttributeBuilder.builder("name of the assigned node", false) - .setAliases("nn", "nodeName") - .build()); - table.addCell("node.ephemeral_id", - TableColumnAttributeBuilder.builder("ephemeral id of the assigned node", false) - .setAliases("ne", "nodeEphemeralId") - .build()); - table.addCell("node.address", - TableColumnAttributeBuilder.builder("network address of the assigned node", false) - .setAliases("na", "nodeAddress") - .build()); + // Node info + table.addCell("node.id", TableColumnAttributeBuilder.builder("id of the assigned node", false).setAliases("ni", "nodeId").build()); + table.addCell( + "node.name", + TableColumnAttributeBuilder.builder("name of the assigned node", false).setAliases("nn", "nodeName").build() + ); + table.addCell( + "node.ephemeral_id", + TableColumnAttributeBuilder.builder("ephemeral id of the assigned node", false).setAliases("ne", "nodeEphemeralId").build() + ); + table.addCell( + "node.address", + TableColumnAttributeBuilder.builder("network address of the assigned node", false).setAliases("na", "nodeAddress").build() + ); - //Timing Stats - table.addCell("buckets.count", - TableColumnAttributeBuilder.builder("bucket count") - .setAliases("bc", "bucketsCount") - .build()); - table.addCell("buckets.time.total", - TableColumnAttributeBuilder.builder("total bucket processing time", false) - .setAliases("btt", "bucketsTimeTotal") - .build()); - table.addCell("buckets.time.min", - TableColumnAttributeBuilder.builder("minimum bucket processing time", false) - .setAliases("btmin", "bucketsTimeMin") - .build()); - table.addCell("buckets.time.max", - TableColumnAttributeBuilder.builder("maximum bucket processing time", false) - .setAliases("btmax", "bucketsTimeMax") - .build()); - table.addCell("buckets.time.exp_avg", + // Timing Stats + table.addCell("buckets.count", TableColumnAttributeBuilder.builder("bucket count").setAliases("bc", "bucketsCount").build()); + table.addCell( + "buckets.time.total", + TableColumnAttributeBuilder.builder("total bucket processing time", false).setAliases("btt", "bucketsTimeTotal").build() + ); + table.addCell( + "buckets.time.min", + TableColumnAttributeBuilder.builder("minimum bucket processing time", false).setAliases("btmin", "bucketsTimeMin").build() + ); + table.addCell( + "buckets.time.max", + TableColumnAttributeBuilder.builder("maximum bucket processing time", false).setAliases("btmax", "bucketsTimeMax").build() + ); + table.addCell( + "buckets.time.exp_avg", TableColumnAttributeBuilder.builder("exponential average bucket processing time (milliseconds)", false) .setAliases("btea", "bucketsTimeExpAvg") - .build()); - table.addCell("buckets.time.exp_avg_hour", + .build() + ); + table.addCell( + "buckets.time.exp_avg_hour", TableColumnAttributeBuilder.builder("exponential average bucket processing time by hour (milliseconds)", false) .setAliases("bteah", "bucketsTimeExpAvgHour") - .build()); + .build() + ); table.endHeaders(); return table; @@ -361,12 +394,16 @@ private Table buildTable(RestRequest request, Response jobStats) { ModelSizeStats modelSizeStats = job.getModelSizeStats(); table.addCell(modelSizeStats == null ? null : ByteSizeValue.ofBytes(modelSizeStats.getModelBytes())); table.addCell(modelSizeStats == null ? null : modelSizeStats.getMemoryStatus().toString()); - table.addCell(modelSizeStats == null || modelSizeStats.getModelBytesExceeded() == null ? - null : - ByteSizeValue.ofBytes(modelSizeStats.getModelBytesExceeded())); - table.addCell(modelSizeStats == null || modelSizeStats.getModelBytesMemoryLimit() == null ? - null : - ByteSizeValue.ofBytes(modelSizeStats.getModelBytesMemoryLimit())); + table.addCell( + modelSizeStats == null || modelSizeStats.getModelBytesExceeded() == null + ? null + : ByteSizeValue.ofBytes(modelSizeStats.getModelBytesExceeded()) + ); + table.addCell( + modelSizeStats == null || modelSizeStats.getModelBytesMemoryLimit() == null + ? null + : ByteSizeValue.ofBytes(modelSizeStats.getModelBytesMemoryLimit()) + ); table.addCell(modelSizeStats == null ? null : modelSizeStats.getTotalByFieldCount()); table.addCell(modelSizeStats == null ? null : modelSizeStats.getTotalOverFieldCount()); table.addCell(modelSizeStats == null ? null : modelSizeStats.getTotalPartitionFieldCount()); @@ -384,18 +421,18 @@ private Table buildTable(RestRequest request, Response jobStats) { ForecastStats forecastStats = job.getForecastStats(); boolean missingForecastStats = forecastStats == null || forecastStats.getTotal() <= 0L; table.addCell(forecastStats == null ? null : forecastStats.getTotal()); - table.addCell(missingForecastStats ? null : ByteSizeValue.ofBytes((long)forecastStats.getMemoryStats().getMin())); - table.addCell(missingForecastStats ? null : ByteSizeValue.ofBytes((long)forecastStats.getMemoryStats().getMax())); + table.addCell(missingForecastStats ? null : ByteSizeValue.ofBytes((long) forecastStats.getMemoryStats().getMin())); + table.addCell(missingForecastStats ? null : ByteSizeValue.ofBytes((long) forecastStats.getMemoryStats().getMax())); table.addCell(missingForecastStats ? null : ByteSizeValue.ofBytes(Math.round(forecastStats.getMemoryStats().getAvg()))); - table.addCell(missingForecastStats ? null : ByteSizeValue.ofBytes((long)forecastStats.getMemoryStats().getTotal())); + table.addCell(missingForecastStats ? null : ByteSizeValue.ofBytes((long) forecastStats.getMemoryStats().getTotal())); table.addCell(missingForecastStats ? null : forecastStats.getRecordStats().getMin()); table.addCell(missingForecastStats ? null : forecastStats.getRecordStats().getMax()); table.addCell(missingForecastStats ? null : forecastStats.getRecordStats().getAvg()); table.addCell(missingForecastStats ? null : forecastStats.getRecordStats().getTotal()); - table.addCell(missingForecastStats ? null : TimeValue.timeValueMillis((long)forecastStats.getRuntimeStats().getMin())); - table.addCell(missingForecastStats ? null : TimeValue.timeValueMillis((long)forecastStats.getRuntimeStats().getMax())); + table.addCell(missingForecastStats ? null : TimeValue.timeValueMillis((long) forecastStats.getRuntimeStats().getMin())); + table.addCell(missingForecastStats ? null : TimeValue.timeValueMillis((long) forecastStats.getRuntimeStats().getMax())); table.addCell(missingForecastStats ? null : forecastStats.getRuntimeStats().getAvg()); - table.addCell(missingForecastStats ? null : TimeValue.timeValueMillis((long)forecastStats.getRuntimeStats().getTotal())); + table.addCell(missingForecastStats ? null : TimeValue.timeValueMillis((long) forecastStats.getRuntimeStats().getTotal())); DiscoveryNode node = job.getNode(); table.addCell(node == null ? null : node.getId()); @@ -405,13 +442,17 @@ private Table buildTable(RestRequest request, Response jobStats) { TimingStats timingStats = job.getTimingStats(); table.addCell(timingStats == null ? null : timingStats.getBucketCount()); - table.addCell(timingStats == null ? null : TimeValue.timeValueMillis((long)timingStats.getTotalBucketProcessingTimeMs())); - table.addCell(timingStats == null || timingStats.getMinBucketProcessingTimeMs() == null ? - null : - TimeValue.timeValueMillis(timingStats.getMinBucketProcessingTimeMs().longValue())); - table.addCell(timingStats == null || timingStats.getMaxBucketProcessingTimeMs() == null ? - null : - TimeValue.timeValueMillis(timingStats.getMaxBucketProcessingTimeMs().longValue())); + table.addCell(timingStats == null ? null : TimeValue.timeValueMillis((long) timingStats.getTotalBucketProcessingTimeMs())); + table.addCell( + timingStats == null || timingStats.getMinBucketProcessingTimeMs() == null + ? null + : TimeValue.timeValueMillis(timingStats.getMinBucketProcessingTimeMs().longValue()) + ); + table.addCell( + timingStats == null || timingStats.getMaxBucketProcessingTimeMs() == null + ? null + : TimeValue.timeValueMillis(timingStats.getMaxBucketProcessingTimeMs().longValue()) + ); table.addCell(timingStats == null ? null : timingStats.getExponentialAvgBucketProcessingTimeMs()); table.addCell(timingStats == null ? null : timingStats.getExponentialAvgBucketProcessingTimePerHourMs()); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/cat/RestCatTrainedModelsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/cat/RestCatTrainedModelsAction.java index 46e1df96a73bb..55020b4b9e8f8 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/cat/RestCatTrainedModelsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/cat/RestCatTrainedModelsAction.java @@ -13,7 +13,6 @@ import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.common.Strings; import org.elasticsearch.common.Table; -import org.elasticsearch.xpack.core.common.table.TableColumnAttributeBuilder; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.core.TimeValue; import org.elasticsearch.rest.RestRequest; @@ -22,6 +21,7 @@ import org.elasticsearch.rest.action.cat.AbstractCatAction; import org.elasticsearch.rest.action.cat.RestTable; import org.elasticsearch.xpack.core.action.util.PageParams; +import org.elasticsearch.xpack.core.common.table.TableColumnAttributeBuilder; import org.elasticsearch.xpack.core.ml.action.GetDataFrameAnalyticsAction; import org.elasticsearch.xpack.core.ml.action.GetTrainedModelsAction; import org.elasticsearch.xpack.core.ml.action.GetTrainedModelsStatsAction; @@ -48,7 +48,8 @@ public class RestCatTrainedModelsAction extends AbstractCatAction { public List routes() { return List.of( new Route(GET, "_cat/ml/trained_models"), - new Route(GET, "_cat/ml/trained_models/{" + TrainedModelConfig.MODEL_ID + "}")); + new Route(GET, "_cat/ml/trained_models/{" + TrainedModelConfig.MODEL_ID + "}") + ); } @Override @@ -65,14 +66,21 @@ protected RestChannelConsumer doCatRequest(RestRequest restRequest, NodeClient c GetTrainedModelsStatsAction.Request statsRequest = new GetTrainedModelsStatsAction.Request(modelId); GetTrainedModelsAction.Request modelsAction = new GetTrainedModelsAction.Request(modelId, null, Collections.emptySet()); if (restRequest.hasParam(PageParams.FROM.getPreferredName()) || restRequest.hasParam(PageParams.SIZE.getPreferredName())) { - statsRequest.setPageParams(new PageParams(restRequest.paramAsInt(PageParams.FROM.getPreferredName(), PageParams.DEFAULT_FROM), - restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE))); - modelsAction.setPageParams(new PageParams(restRequest.paramAsInt(PageParams.FROM.getPreferredName(), PageParams.DEFAULT_FROM), - restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE))); + statsRequest.setPageParams( + new PageParams( + restRequest.paramAsInt(PageParams.FROM.getPreferredName(), PageParams.DEFAULT_FROM), + restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE) + ) + ); + modelsAction.setPageParams( + new PageParams( + restRequest.paramAsInt(PageParams.FROM.getPreferredName(), PageParams.DEFAULT_FROM), + restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE) + ) + ); } statsRequest.setAllowNoResources(true); - modelsAction.setAllowNoResources(restRequest.paramAsBoolean(ALLOW_NO_MATCH.getPreferredName(), - statsRequest.isAllowNoResources())); + modelsAction.setAllowNoResources(restRequest.paramAsBoolean(ALLOW_NO_MATCH.getPreferredName(), statsRequest.isAllowNoResources())); return channel -> { final ActionListener listener = ActionListener.notifyOnce(new RestResponseListener<>(channel) { @@ -82,39 +90,40 @@ public RestResponse buildResponse(final Table table) throws Exception { } }); - client.execute(GetTrainedModelsAction.INSTANCE, modelsAction, ActionListener.wrap( - trainedModels -> { - final List trainedModelConfigs = trainedModels.getResources().results(); + client.execute(GetTrainedModelsAction.INSTANCE, modelsAction, ActionListener.wrap(trainedModels -> { + final List trainedModelConfigs = trainedModels.getResources().results(); - Set potentialAnalyticsIds = new HashSet<>(); - // Analytics Configs are created by the XPackUser - trainedModelConfigs.stream() - .filter(c -> XPackUser.NAME.equals(c.getCreatedBy())) - .forEach(c -> potentialAnalyticsIds.addAll(c.getTags())); + Set potentialAnalyticsIds = new HashSet<>(); + // Analytics Configs are created by the XPackUser + trainedModelConfigs.stream() + .filter(c -> XPackUser.NAME.equals(c.getCreatedBy())) + .forEach(c -> potentialAnalyticsIds.addAll(c.getTags())); + // Find the related DataFrameAnalyticsConfigs + String requestIdPattern = Strings.collectionToDelimitedString(potentialAnalyticsIds, "*,") + "*"; - // Find the related DataFrameAnalyticsConfigs - String requestIdPattern = Strings.collectionToDelimitedString(potentialAnalyticsIds, "*,") + "*"; + final GroupedActionListener groupedListener = createGroupedListener( + restRequest, + 2, + trainedModels.getResources().results(), + listener + ); - final GroupedActionListener groupedListener = createGroupedListener(restRequest, - 2, - trainedModels.getResources().results(), - listener); + client.execute( + GetTrainedModelsStatsAction.INSTANCE, + statsRequest, + ActionListener.wrap(groupedListener::onResponse, groupedListener::onFailure) + ); - client.execute(GetTrainedModelsStatsAction.INSTANCE, - statsRequest, - ActionListener.wrap(groupedListener::onResponse, groupedListener::onFailure)); - - GetDataFrameAnalyticsAction.Request dataFrameAnalyticsRequest = - new GetDataFrameAnalyticsAction.Request(requestIdPattern); - dataFrameAnalyticsRequest.setAllowNoResources(true); - dataFrameAnalyticsRequest.setPageParams(new PageParams(0, potentialAnalyticsIds.size())); - client.execute(GetDataFrameAnalyticsAction.INSTANCE, - dataFrameAnalyticsRequest, - ActionListener.wrap(groupedListener::onResponse, groupedListener::onFailure)); - }, - listener::onFailure - )); + GetDataFrameAnalyticsAction.Request dataFrameAnalyticsRequest = new GetDataFrameAnalyticsAction.Request(requestIdPattern); + dataFrameAnalyticsRequest.setAllowNoResources(true); + dataFrameAnalyticsRequest.setPageParams(new PageParams(0, potentialAnalyticsIds.size())); + client.execute( + GetDataFrameAnalyticsAction.INSTANCE, + dataFrameAnalyticsRequest, + ActionListener.wrap(groupedListener::onResponse, groupedListener::onFailure) + ); + }, listener::onFailure)); }; } @@ -131,98 +140,119 @@ protected Table getTableWithHeader(RestRequest request) { // Trained Model Info table.addCell("id", TableColumnAttributeBuilder.builder("the trained model id").build()); - table.addCell("created_by", TableColumnAttributeBuilder.builder("who created the model", false) - .setAliases("c", "createdBy") - .setTextAlignment(TableColumnAttributeBuilder.TextAlign.RIGHT) - .build()); - table.addCell("heap_size", TableColumnAttributeBuilder.builder("the estimated heap size to keep the model in memory") - .setAliases("hs","modelHeapSize") - .build()); - table.addCell("operations", TableColumnAttributeBuilder.builder("the estimated number of operations to use the model") - .setAliases("o", "modelOperations") - .build()); - table.addCell("license", TableColumnAttributeBuilder.builder("The license level of the model", false) - .setAliases("l") - .build()); - table.addCell("create_time", TableColumnAttributeBuilder.builder("The time the model was created") - .setAliases("ct") - .build()); - table.addCell("version", TableColumnAttributeBuilder.builder("The version of Elasticsearch when the model was created", false) - .setAliases("v") - .build()); - table.addCell("description", TableColumnAttributeBuilder.builder("The model description", false) - .setAliases("d") - .build()); - table.addCell("type", TableColumnAttributeBuilder.builder("The model type") - .setAliases("t") - .build()); + table.addCell( + "created_by", + TableColumnAttributeBuilder.builder("who created the model", false) + .setAliases("c", "createdBy") + .setTextAlignment(TableColumnAttributeBuilder.TextAlign.RIGHT) + .build() + ); + table.addCell( + "heap_size", + TableColumnAttributeBuilder.builder("the estimated heap size to keep the model in memory") + .setAliases("hs", "modelHeapSize") + .build() + ); + table.addCell( + "operations", + TableColumnAttributeBuilder.builder("the estimated number of operations to use the model") + .setAliases("o", "modelOperations") + .build() + ); + table.addCell("license", TableColumnAttributeBuilder.builder("The license level of the model", false).setAliases("l").build()); + table.addCell("create_time", TableColumnAttributeBuilder.builder("The time the model was created").setAliases("ct").build()); + table.addCell( + "version", + TableColumnAttributeBuilder.builder("The version of Elasticsearch when the model was created", false).setAliases("v").build() + ); + table.addCell("description", TableColumnAttributeBuilder.builder("The model description", false).setAliases("d").build()); + table.addCell("type", TableColumnAttributeBuilder.builder("The model type").setAliases("t").build()); // Trained Model Stats - table.addCell("ingest.pipelines", TableColumnAttributeBuilder.builder("The number of pipelines referencing the model") - .setAliases("ip", "ingestPipelines") - .build()); - table.addCell("ingest.count", TableColumnAttributeBuilder.builder("The total number of docs processed by the model", false) - .setAliases("ic", "ingestCount") - .build()); - table.addCell("ingest.time", TableColumnAttributeBuilder.builder( - "The total time spent processing docs with this model", - false) - .setAliases("it", "ingestTime") - .build()); - table.addCell("ingest.current", TableColumnAttributeBuilder.builder( - "The total documents currently being handled by the model", - false) - .setAliases("icurr", "ingestCurrent") - .build()); - table.addCell("ingest.failed", TableColumnAttributeBuilder.builder( - "The total count of failed ingest attempts with this model", - false) - .setAliases("if", "ingestFailed") - .build()); + table.addCell( + "ingest.pipelines", + TableColumnAttributeBuilder.builder("The number of pipelines referencing the model").setAliases("ip", "ingestPipelines").build() + ); + table.addCell( + "ingest.count", + TableColumnAttributeBuilder.builder("The total number of docs processed by the model", false) + .setAliases("ic", "ingestCount") + .build() + ); + table.addCell( + "ingest.time", + TableColumnAttributeBuilder.builder("The total time spent processing docs with this model", false) + .setAliases("it", "ingestTime") + .build() + ); + table.addCell( + "ingest.current", + TableColumnAttributeBuilder.builder("The total documents currently being handled by the model", false) + .setAliases("icurr", "ingestCurrent") + .build() + ); + table.addCell( + "ingest.failed", + TableColumnAttributeBuilder.builder("The total count of failed ingest attempts with this model", false) + .setAliases("if", "ingestFailed") + .build() + ); - table.addCell("data_frame.id", TableColumnAttributeBuilder.builder( - "The data frame analytics config id that created the model (if still available)") - .setAliases("dfid", "dataFrameAnalytics") - .build()); - table.addCell("data_frame.create_time", TableColumnAttributeBuilder.builder( - "The time the data frame analytics config was created", - false) - .setAliases("dft", "dataFrameAnalyticsTime") - .build()); - table.addCell("data_frame.source_index", TableColumnAttributeBuilder.builder( - "The source index used to train in the data frame analysis", - false) - .setAliases("dfsi", "dataFrameAnalyticsSrcIndex") - .build()); - table.addCell("data_frame.analysis", TableColumnAttributeBuilder.builder( - "The analysis used by the data frame to build the model", - false) - .setAliases("dfa", "dataFrameAnalyticsAnalysis") - .build()); + table.addCell( + "data_frame.id", + TableColumnAttributeBuilder.builder("The data frame analytics config id that created the model (if still available)") + .setAliases("dfid", "dataFrameAnalytics") + .build() + ); + table.addCell( + "data_frame.create_time", + TableColumnAttributeBuilder.builder("The time the data frame analytics config was created", false) + .setAliases("dft", "dataFrameAnalyticsTime") + .build() + ); + table.addCell( + "data_frame.source_index", + TableColumnAttributeBuilder.builder("The source index used to train in the data frame analysis", false) + .setAliases("dfsi", "dataFrameAnalyticsSrcIndex") + .build() + ); + table.addCell( + "data_frame.analysis", + TableColumnAttributeBuilder.builder("The analysis used by the data frame to build the model", false) + .setAliases("dfa", "dataFrameAnalyticsAnalysis") + .build() + ); table.endHeaders(); return table; } - private GroupedActionListener createGroupedListener(final RestRequest request, - final int size, - final List configs, - final ActionListener
    listener) { + private GroupedActionListener createGroupedListener( + final RestRequest request, + final int size, + final List configs, + final ActionListener
    listener + ) { return new GroupedActionListener<>(listener.delegateFailure((l, responses) -> { GetTrainedModelsStatsAction.Response statsResponse = extractResponse(responses, GetTrainedModelsStatsAction.Response.class); GetDataFrameAnalyticsAction.Response analytics = extractResponse(responses, GetDataFrameAnalyticsAction.Response.class); - l.onResponse(buildTable(request, + l.onResponse( + buildTable( + request, statsResponse.getResources().results(), configs, - analytics == null ? Collections.emptyList() : analytics.getResources().results())); + analytics == null ? Collections.emptyList() : analytics.getResources().results() + ) + ); }), size); } - - private Table buildTable(RestRequest request, - List stats, - List configs, - List analyticsConfigs) { + private Table buildTable( + RestRequest request, + List stats, + List configs, + List analyticsConfigs + ) { Table table = getTableWithHeader(request); assert configs.size() == stats.size(); @@ -248,9 +278,11 @@ private Table buildTable(RestRequest request, table.addCell(modelStats.getPipelineCount()); boolean hasIngestStats = modelStats != null && modelStats.getIngestStats() != null; table.addCell(hasIngestStats ? modelStats.getIngestStats().getTotalStats().getIngestCount() : 0); - table.addCell(hasIngestStats ? - TimeValue.timeValueMillis(modelStats.getIngestStats().getTotalStats().getIngestTimeInMillis()) : - TimeValue.timeValueMillis(0)); + table.addCell( + hasIngestStats + ? TimeValue.timeValueMillis(modelStats.getIngestStats().getTotalStats().getIngestTimeInMillis()) + : TimeValue.timeValueMillis(0) + ); table.addCell(hasIngestStats ? modelStats.getIngestStats().getTotalStats().getIngestCurrent() : 0); table.addCell(hasIngestStats ? modelStats.getIngestStats().getTotalStats().getIngestFailedCount() : 0); @@ -262,9 +294,11 @@ private Table buildTable(RestRequest request, .orElse(null); table.addCell(dataFrameAnalyticsConfig == null ? "__none__" : dataFrameAnalyticsConfig.getId()); table.addCell(dataFrameAnalyticsConfig == null ? null : dataFrameAnalyticsConfig.getCreateTime()); - table.addCell(dataFrameAnalyticsConfig == null ? - null : - Strings.arrayToCommaDelimitedString(dataFrameAnalyticsConfig.getSource().getIndex())); + table.addCell( + dataFrameAnalyticsConfig == null + ? null + : Strings.arrayToCommaDelimitedString(dataFrameAnalyticsConfig.getSource().getIndex()) + ); DataFrameAnalysis analysis = dataFrameAnalyticsConfig == null ? null : dataFrameAnalyticsConfig.getAnalysis(); table.addCell(analysis == null ? null : analysis.getWriteableName()); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/datafeeds/RestDeleteDatafeedAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/datafeeds/RestDeleteDatafeedAction.java index c719311b61d90..0c96618accbd7 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/datafeeds/RestDeleteDatafeedAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/datafeeds/RestDeleteDatafeedAction.java @@ -28,7 +28,8 @@ public class RestDeleteDatafeedAction extends BaseRestHandler { public List routes() { return List.of( Route.builder(DELETE, BASE_PATH + "datafeeds/{" + DatafeedConfig.ID + "}") - .replaces(DELETE, PRE_V7_BASE_PATH + "datafeeds/{" + DatafeedConfig.ID + "}", RestApiVersion.V_7).build() + .replaces(DELETE, PRE_V7_BASE_PATH + "datafeeds/{" + DatafeedConfig.ID + "}", RestApiVersion.V_7) + .build() ); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/datafeeds/RestGetDatafeedStatsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/datafeeds/RestGetDatafeedStatsAction.java index c0745c20e7a75..83f4a66a18d87 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/datafeeds/RestGetDatafeedStatsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/datafeeds/RestGetDatafeedStatsAction.java @@ -26,13 +26,15 @@ public class RestGetDatafeedStatsAction extends BaseRestHandler { - @Override + @Override public List routes() { return List.of( Route.builder(GET, BASE_PATH + "datafeeds/{" + DatafeedConfig.ID + "}/_stats") - .replaces(GET, PRE_V7_BASE_PATH + "datafeeds/{" + DatafeedConfig.ID + "}/_stats", RestApiVersion.V_7).build(), + .replaces(GET, PRE_V7_BASE_PATH + "datafeeds/{" + DatafeedConfig.ID + "}/_stats", RestApiVersion.V_7) + .build(), Route.builder(GET, BASE_PATH + "datafeeds/_stats") - .replaces(GET, PRE_V7_BASE_PATH + "datafeeds/_stats", RestApiVersion.V_7).build() + .replaces(GET, PRE_V7_BASE_PATH + "datafeeds/_stats", RestApiVersion.V_7) + .build() ); } @@ -54,7 +56,9 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient request.setAllowNoMatch( restRequest.paramAsBoolean( Request.ALLOW_NO_MATCH, - restRequest.paramAsBoolean(Request.ALLOW_NO_DATAFEEDS, request.allowNoMatch()))); + restRequest.paramAsBoolean(Request.ALLOW_NO_DATAFEEDS, request.allowNoMatch()) + ) + ); return channel -> client.execute(GetDatafeedsStatsAction.INSTANCE, request, new RestToXContentListener<>(channel)); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/datafeeds/RestGetDatafeedsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/datafeeds/RestGetDatafeedsAction.java index fe0b0b8bbcfe2..7452e23ad7245 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/datafeeds/RestGetDatafeedsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/datafeeds/RestGetDatafeedsAction.java @@ -28,13 +28,13 @@ public class RestGetDatafeedsAction extends BaseRestHandler { - @Override + @Override public List routes() { return List.of( Route.builder(GET, BASE_PATH + "datafeeds/{" + DatafeedConfig.ID + "}") - .replaces(GET, PRE_V7_BASE_PATH + "datafeeds/{" + DatafeedConfig.ID + "}", RestApiVersion.V_7).build(), - Route.builder(GET, BASE_PATH + "datafeeds") - .replaces(GET, PRE_V7_BASE_PATH + "datafeeds", RestApiVersion.V_7).build() + .replaces(GET, PRE_V7_BASE_PATH + "datafeeds/{" + DatafeedConfig.ID + "}", RestApiVersion.V_7) + .build(), + Route.builder(GET, BASE_PATH + "datafeeds").replaces(GET, PRE_V7_BASE_PATH + "datafeeds", RestApiVersion.V_7).build() ); } @@ -56,7 +56,9 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient request.setAllowNoMatch( restRequest.paramAsBoolean( Request.ALLOW_NO_MATCH, - restRequest.paramAsBoolean(Request.ALLOW_NO_DATAFEEDS, request.allowNoMatch()))); + restRequest.paramAsBoolean(Request.ALLOW_NO_DATAFEEDS, request.allowNoMatch()) + ) + ); return channel -> client.execute(GetDatafeedsAction.INSTANCE, request, new RestToXContentListener<>(channel)); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/datafeeds/RestPreviewDatafeedAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/datafeeds/RestPreviewDatafeedAction.java index 84aafe9cd3c75..ec931d7f283ee 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/datafeeds/RestPreviewDatafeedAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/datafeeds/RestPreviewDatafeedAction.java @@ -28,13 +28,17 @@ public class RestPreviewDatafeedAction extends BaseRestHandler { public List routes() { return List.of( Route.builder(GET, BASE_PATH + "datafeeds/{" + DatafeedConfig.ID + "}/_preview") - .replaces(GET, PRE_V7_BASE_PATH + "datafeeds/{" + DatafeedConfig.ID + "}/_preview", RestApiVersion.V_7).build(), + .replaces(GET, PRE_V7_BASE_PATH + "datafeeds/{" + DatafeedConfig.ID + "}/_preview", RestApiVersion.V_7) + .build(), Route.builder(GET, BASE_PATH + "datafeeds/_preview") - .replaces(GET, PRE_V7_BASE_PATH + "datafeeds/_preview", RestApiVersion.V_7).build(), + .replaces(GET, PRE_V7_BASE_PATH + "datafeeds/_preview", RestApiVersion.V_7) + .build(), Route.builder(POST, BASE_PATH + "datafeeds/{" + DatafeedConfig.ID + "}/_preview") - .replaces(POST, PRE_V7_BASE_PATH + "datafeeds/{" + DatafeedConfig.ID + "}/_preview", RestApiVersion.V_7).build(), + .replaces(POST, PRE_V7_BASE_PATH + "datafeeds/{" + DatafeedConfig.ID + "}/_preview", RestApiVersion.V_7) + .build(), Route.builder(POST, BASE_PATH + "datafeeds/_preview") - .replaces(POST, PRE_V7_BASE_PATH + "datafeeds/_preview", RestApiVersion.V_7).build() + .replaces(POST, PRE_V7_BASE_PATH + "datafeeds/_preview", RestApiVersion.V_7) + .build() ); } @@ -45,12 +49,12 @@ public String getName() { @Override protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) throws IOException { - PreviewDatafeedAction.Request request = restRequest.hasContentOrSourceParam() ? - PreviewDatafeedAction.Request.fromXContent( + PreviewDatafeedAction.Request request = restRequest.hasContentOrSourceParam() + ? PreviewDatafeedAction.Request.fromXContent( restRequest.contentOrSourceParamParser(), restRequest.param(DatafeedConfig.ID.getPreferredName(), null) - ) : - new PreviewDatafeedAction.Request(restRequest.param(DatafeedConfig.ID.getPreferredName())); + ) + : new PreviewDatafeedAction.Request(restRequest.param(DatafeedConfig.ID.getPreferredName())); return channel -> client.execute(PreviewDatafeedAction.INSTANCE, request, new RestToXContentListener<>(channel)); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/datafeeds/RestPutDatafeedAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/datafeeds/RestPutDatafeedAction.java index d07abd84537ff..4e36a41af5cf7 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/datafeeds/RestPutDatafeedAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/datafeeds/RestPutDatafeedAction.java @@ -9,11 +9,11 @@ import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestToXContentListener; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.PutDatafeedAction; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfig; @@ -30,7 +30,8 @@ public class RestPutDatafeedAction extends BaseRestHandler { public List routes() { return List.of( Route.builder(PUT, BASE_PATH + "datafeeds/{" + DatafeedConfig.ID + "}") - .replaces(PUT, PRE_V7_BASE_PATH + "datafeeds/{" + DatafeedConfig.ID + "}", RestApiVersion.V_7).build() + .replaces(PUT, PRE_V7_BASE_PATH + "datafeeds/{" + DatafeedConfig.ID + "}", RestApiVersion.V_7) + .build() ); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/datafeeds/RestStartDatafeedAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/datafeeds/RestStartDatafeedAction.java index db37cf0539d68..02807808035bf 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/datafeeds/RestStartDatafeedAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/datafeeds/RestStartDatafeedAction.java @@ -9,14 +9,14 @@ import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.NodeAcknowledgedResponse; import org.elasticsearch.xpack.core.ml.action.StartDatafeedAction; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfig; @@ -36,7 +36,8 @@ public class RestStartDatafeedAction extends BaseRestHandler { public List routes() { return List.of( Route.builder(POST, BASE_PATH + "datafeeds/{" + DatafeedConfig.ID + "}/_start") - .replaces(POST, PRE_V7_BASE_PATH + "datafeeds/{" + DatafeedConfig.ID + "}/_start", RestApiVersion.V_7).build() + .replaces(POST, PRE_V7_BASE_PATH + "datafeeds/{" + DatafeedConfig.ID + "}/_start", RestApiVersion.V_7) + .build() ); } @@ -60,25 +61,26 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient } if (restRequest.hasParam(StartDatafeedAction.TIMEOUT.getPreferredName())) { TimeValue openTimeout = restRequest.paramAsTime( - StartDatafeedAction.TIMEOUT.getPreferredName(), TimeValue.timeValueSeconds(20)); + StartDatafeedAction.TIMEOUT.getPreferredName(), + TimeValue.timeValueSeconds(20) + ); datafeedParams.setTimeout(openTimeout); } jobDatafeedRequest = new StartDatafeedAction.Request(datafeedParams); } return channel -> { - client.execute(StartDatafeedAction.INSTANCE, jobDatafeedRequest, - new RestBuilderListener(channel) { + client.execute(StartDatafeedAction.INSTANCE, jobDatafeedRequest, new RestBuilderListener(channel) { - @Override - public RestResponse buildResponse(NodeAcknowledgedResponse r, XContentBuilder builder) throws Exception { - // This doesn't use the toXContent of the response object because we rename "acknowledged" to "started" - builder.startObject(); - builder.field("started", r.isAcknowledged()); - builder.field(NodeAcknowledgedResponse.NODE_FIELD, r.getNode()); - builder.endObject(); - return new BytesRestResponse(RestStatus.OK, builder); - } - }); + @Override + public RestResponse buildResponse(NodeAcknowledgedResponse r, XContentBuilder builder) throws Exception { + // This doesn't use the toXContent of the response object because we rename "acknowledged" to "started" + builder.startObject(); + builder.field("started", r.isAcknowledged()); + builder.field(NodeAcknowledgedResponse.NODE_FIELD, r.getNode()); + builder.endObject(); + return new BytesRestResponse(RestStatus.OK, builder); + } + }); }; } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/datafeeds/RestStopDatafeedAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/datafeeds/RestStopDatafeedAction.java index b6833b1327a27..f5f7557f6b8a2 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/datafeeds/RestStopDatafeedAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/datafeeds/RestStopDatafeedAction.java @@ -7,17 +7,17 @@ package org.elasticsearch.xpack.ml.rest.datafeeds; import org.elasticsearch.client.node.NodeClient; +import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.StopDatafeedAction; import org.elasticsearch.xpack.core.ml.action.StopDatafeedAction.Request; import org.elasticsearch.xpack.core.ml.action.StopDatafeedAction.Response; @@ -36,7 +36,8 @@ public class RestStopDatafeedAction extends BaseRestHandler { public List routes() { return List.of( Route.builder(POST, BASE_PATH + "datafeeds/{" + DatafeedConfig.ID + "}/_stop") - .replaces(POST, PRE_V7_BASE_PATH + "datafeeds/{" + DatafeedConfig.ID + "}/_stop", RestApiVersion.V_7).build() + .replaces(POST, PRE_V7_BASE_PATH + "datafeeds/{" + DatafeedConfig.ID + "}/_stop", RestApiVersion.V_7) + .build() ); } @@ -63,12 +64,18 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient } if (restRequest.hasParam(Request.ALLOW_NO_DATAFEEDS)) { LoggingDeprecationHandler.INSTANCE.logRenamedField( - null, () -> null, Request.ALLOW_NO_DATAFEEDS, Request.ALLOW_NO_MATCH.getPreferredName()); + null, + () -> null, + Request.ALLOW_NO_DATAFEEDS, + Request.ALLOW_NO_MATCH.getPreferredName() + ); } request.setAllowNoMatch( restRequest.paramAsBoolean( Request.ALLOW_NO_MATCH.getPreferredName(), - restRequest.paramAsBoolean(Request.ALLOW_NO_DATAFEEDS, request.allowNoMatch()))); + restRequest.paramAsBoolean(Request.ALLOW_NO_DATAFEEDS, request.allowNoMatch()) + ) + ); } return channel -> client.execute(StopDatafeedAction.INSTANCE, request, new RestBuilderListener(channel) { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/datafeeds/RestUpdateDatafeedAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/datafeeds/RestUpdateDatafeedAction.java index 30dd834ebed5f..08bd97c62f787 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/datafeeds/RestUpdateDatafeedAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/datafeeds/RestUpdateDatafeedAction.java @@ -9,11 +9,11 @@ import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestToXContentListener; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.UpdateDatafeedAction; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfig; @@ -30,7 +30,8 @@ public class RestUpdateDatafeedAction extends BaseRestHandler { public List routes() { return List.of( Route.builder(POST, BASE_PATH + "datafeeds/{" + DatafeedConfig.ID + "}/_update") - .replaces(POST, PRE_V7_BASE_PATH + "datafeeds/{" + DatafeedConfig.ID + "}/_update", RestApiVersion.V_7).build() + .replaces(POST, PRE_V7_BASE_PATH + "datafeeds/{" + DatafeedConfig.ID + "}/_update", RestApiVersion.V_7) + .build() ); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/dataframe/RestDeleteDataFrameAnalyticsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/dataframe/RestDeleteDataFrameAnalyticsAction.java index 67129fb5f11d6..4ac68777051cc 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/dataframe/RestDeleteDataFrameAnalyticsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/dataframe/RestDeleteDataFrameAnalyticsAction.java @@ -23,9 +23,7 @@ public class RestDeleteDataFrameAnalyticsAction extends BaseRestHandler { @Override public List routes() { - return List.of( - new Route(DELETE, BASE_PATH + "data_frame/analytics/{" + DataFrameAnalyticsConfig.ID + "}") - ); + return List.of(new Route(DELETE, BASE_PATH + "data_frame/analytics/{" + DataFrameAnalyticsConfig.ID + "}")); } @Override diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/dataframe/RestExplainDataFrameAnalyticsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/dataframe/RestExplainDataFrameAnalyticsAction.java index ede996aed527d..42b0556f2349e 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/dataframe/RestExplainDataFrameAnalyticsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/dataframe/RestExplainDataFrameAnalyticsAction.java @@ -48,18 +48,23 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient final String jobId = restRequest.param(DataFrameAnalyticsConfig.ID.getPreferredName()); if (Strings.isNullOrEmpty(jobId) && restRequest.hasContentOrSourceParam() == false) { - throw ExceptionsHelper.badRequestException("Please provide a job [{}] or the config object", - DataFrameAnalyticsConfig.ID.getPreferredName()); + throw ExceptionsHelper.badRequestException( + "Please provide a job [{}] or the config object", + DataFrameAnalyticsConfig.ID.getPreferredName() + ); } if (Strings.isNullOrEmpty(jobId) == false && restRequest.hasContentOrSourceParam()) { - throw ExceptionsHelper.badRequestException("Please provide either a job [{}] or the config object but not both", - DataFrameAnalyticsConfig.ID.getPreferredName()); + throw ExceptionsHelper.badRequestException( + "Please provide either a job [{}] or the config object but not both", + DataFrameAnalyticsConfig.ID.getPreferredName() + ); } // We need to consume the body before returning - PutDataFrameAnalyticsAction.Request explainRequestFromBody = Strings.isNullOrEmpty(jobId) ? - PutDataFrameAnalyticsAction.Request.parseRequestForExplain(restRequest.contentOrSourceParamParser()) : null; + PutDataFrameAnalyticsAction.Request explainRequestFromBody = Strings.isNullOrEmpty(jobId) + ? PutDataFrameAnalyticsAction.Request.parseRequestForExplain(restRequest.contentOrSourceParamParser()) + : null; return channel -> { RestToXContentListener listener = new RestToXContentListener<>(channel); @@ -69,19 +74,20 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient } else { GetDataFrameAnalyticsAction.Request getRequest = new GetDataFrameAnalyticsAction.Request(jobId); getRequest.setAllowNoResources(false); - client.execute(GetDataFrameAnalyticsAction.INSTANCE, getRequest, ActionListener.wrap( - getResponse -> { - List jobs = getResponse.getResources().results(); - if (jobs.size() > 1) { - listener.onFailure(ExceptionsHelper.badRequestException("expected only one config but matched {}", - jobs.stream().map(DataFrameAnalyticsConfig::getId).collect(Collectors.toList()))); - } else { - PutDataFrameAnalyticsAction.Request explainRequest = new PutDataFrameAnalyticsAction.Request(jobs.get(0)); - client.execute(ExplainDataFrameAnalyticsAction.INSTANCE, explainRequest, listener); - } - }, - listener::onFailure - )); + client.execute(GetDataFrameAnalyticsAction.INSTANCE, getRequest, ActionListener.wrap(getResponse -> { + List jobs = getResponse.getResources().results(); + if (jobs.size() > 1) { + listener.onFailure( + ExceptionsHelper.badRequestException( + "expected only one config but matched {}", + jobs.stream().map(DataFrameAnalyticsConfig::getId).collect(Collectors.toList()) + ) + ); + } else { + PutDataFrameAnalyticsAction.Request explainRequest = new PutDataFrameAnalyticsAction.Request(jobs.get(0)); + client.execute(ExplainDataFrameAnalyticsAction.INSTANCE, explainRequest, listener); + } + }, listener::onFailure)); } }; } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/dataframe/RestGetDataFrameAnalyticsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/dataframe/RestGetDataFrameAnalyticsAction.java index 353400cb1e7f5..2cb4667a3213c 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/dataframe/RestGetDataFrameAnalyticsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/dataframe/RestGetDataFrameAnalyticsAction.java @@ -30,7 +30,8 @@ public class RestGetDataFrameAnalyticsAction extends BaseRestHandler { public List routes() { return List.of( new Route(GET, BASE_PATH + "data_frame/analytics"), - new Route(GET, BASE_PATH + "data_frame/analytics/{" + DataFrameAnalyticsConfig.ID + "}")); + new Route(GET, BASE_PATH + "data_frame/analytics/{" + DataFrameAnalyticsConfig.ID + "}") + ); } @Override @@ -46,11 +47,16 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient request.setResourceId(id); } if (restRequest.hasParam(PageParams.FROM.getPreferredName()) || restRequest.hasParam(PageParams.SIZE.getPreferredName())) { - request.setPageParams(new PageParams(restRequest.paramAsInt(PageParams.FROM.getPreferredName(), PageParams.DEFAULT_FROM), - restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE))); + request.setPageParams( + new PageParams( + restRequest.paramAsInt(PageParams.FROM.getPreferredName(), PageParams.DEFAULT_FROM), + restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE) + ) + ); } - request.setAllowNoResources(restRequest.paramAsBoolean(GetDataFrameAnalyticsAction.Request.ALLOW_NO_MATCH.getPreferredName(), - request.isAllowNoResources())); + request.setAllowNoResources( + restRequest.paramAsBoolean(GetDataFrameAnalyticsAction.Request.ALLOW_NO_MATCH.getPreferredName(), request.isAllowNoResources()) + ); return channel -> client.execute(GetDataFrameAnalyticsAction.INSTANCE, request, new RestToXContentListener<>(channel)); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/dataframe/RestGetDataFrameAnalyticsStatsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/dataframe/RestGetDataFrameAnalyticsStatsAction.java index 3cfdf0edceecb..ac6d31950b7c6 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/dataframe/RestGetDataFrameAnalyticsStatsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/dataframe/RestGetDataFrameAnalyticsStatsAction.java @@ -46,12 +46,16 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient request.setId(id); } if (restRequest.hasParam(PageParams.FROM.getPreferredName()) || restRequest.hasParam(PageParams.SIZE.getPreferredName())) { - request.setPageParams(new PageParams(restRequest.paramAsInt(PageParams.FROM.getPreferredName(), PageParams.DEFAULT_FROM), - restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE))); + request.setPageParams( + new PageParams( + restRequest.paramAsInt(PageParams.FROM.getPreferredName(), PageParams.DEFAULT_FROM), + restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE) + ) + ); } request.setAllowNoMatch( - restRequest.paramAsBoolean( - GetDataFrameAnalyticsStatsAction.Request.ALLOW_NO_MATCH.getPreferredName(), request.isAllowNoMatch())); + restRequest.paramAsBoolean(GetDataFrameAnalyticsStatsAction.Request.ALLOW_NO_MATCH.getPreferredName(), request.isAllowNoMatch()) + ); return channel -> client.execute(GetDataFrameAnalyticsStatsAction.INSTANCE, request, new RestToXContentListener<>(channel)); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/dataframe/RestPostDataFrameAnalyticsUpdateAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/dataframe/RestPostDataFrameAnalyticsUpdateAction.java index 641c6d8c28a8a..0aec4ba562eeb 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/dataframe/RestPostDataFrameAnalyticsUpdateAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/dataframe/RestPostDataFrameAnalyticsUpdateAction.java @@ -7,10 +7,10 @@ package org.elasticsearch.xpack.ml.rest.dataframe; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestToXContentListener; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.UpdateDataFrameAnalyticsAction; import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsConfig; @@ -24,9 +24,7 @@ public class RestPostDataFrameAnalyticsUpdateAction extends BaseRestHandler { @Override public List routes() { - return List.of( - new Route(POST, BASE_PATH + "data_frame/analytics/{" + DataFrameAnalyticsConfig.ID + "}/_update") - ); + return List.of(new Route(POST, BASE_PATH + "data_frame/analytics/{" + DataFrameAnalyticsConfig.ID + "}/_update")); } @Override diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/dataframe/RestPreviewDataFrameAnalyticsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/dataframe/RestPreviewDataFrameAnalyticsAction.java index af5e924b45d92..1e8e8b872758c 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/dataframe/RestPreviewDataFrameAnalyticsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/dataframe/RestPreviewDataFrameAnalyticsAction.java @@ -59,9 +59,9 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient DataFrameAnalyticsConfig.ID.getPreferredName() ); } - final PreviewDataFrameAnalyticsAction.Request.Builder requestBuilder = Strings.isNullOrEmpty(jobId) ? - PreviewDataFrameAnalyticsAction.Request.fromXContent(restRequest.contentOrSourceParamParser()) : - new PreviewDataFrameAnalyticsAction.Request.Builder(); + final PreviewDataFrameAnalyticsAction.Request.Builder requestBuilder = Strings.isNullOrEmpty(jobId) + ? PreviewDataFrameAnalyticsAction.Request.fromXContent(restRequest.contentOrSourceParamParser()) + : new PreviewDataFrameAnalyticsAction.Request.Builder(); return channel -> { RestToXContentListener listener = new RestToXContentListener<>(channel); @@ -81,11 +81,7 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient ) ); } else { - client.execute( - PreviewDataFrameAnalyticsAction.INSTANCE, - requestBuilder.setConfig(jobs.get(0)).build(), - listener - ); + client.execute(PreviewDataFrameAnalyticsAction.INSTANCE, requestBuilder.setConfig(jobs.get(0)).build(), listener); } }, listener::onFailure)); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/dataframe/RestPutDataFrameAnalyticsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/dataframe/RestPutDataFrameAnalyticsAction.java index f74b1820338cf..9600e98dc4bd7 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/dataframe/RestPutDataFrameAnalyticsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/dataframe/RestPutDataFrameAnalyticsAction.java @@ -7,10 +7,10 @@ package org.elasticsearch.xpack.ml.rest.dataframe; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestToXContentListener; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.PutDataFrameAnalyticsAction; import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsConfig; @@ -24,9 +24,7 @@ public class RestPutDataFrameAnalyticsAction extends BaseRestHandler { @Override public List routes() { - return List.of( - new Route(PUT, BASE_PATH + "data_frame/analytics/{" + DataFrameAnalyticsConfig.ID + "}") - ); + return List.of(new Route(PUT, BASE_PATH + "data_frame/analytics/{" + DataFrameAnalyticsConfig.ID + "}")); } @Override diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/dataframe/RestStartDataFrameAnalyticsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/dataframe/RestStartDataFrameAnalyticsAction.java index 13662c2ccd2b8..d9348b4794f95 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/dataframe/RestStartDataFrameAnalyticsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/dataframe/RestStartDataFrameAnalyticsAction.java @@ -24,9 +24,7 @@ public class RestStartDataFrameAnalyticsAction extends BaseRestHandler { @Override public List routes() { - return List.of( - new Route(POST, BASE_PATH + "data_frame/analytics/{" + DataFrameAnalyticsConfig.ID + "}/_start") - ); + return List.of(new Route(POST, BASE_PATH + "data_frame/analytics/{" + DataFrameAnalyticsConfig.ID + "}/_start")); } @Override @@ -43,8 +41,10 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient } else { request = new StartDataFrameAnalyticsAction.Request(id); if (restRequest.hasParam(StartDataFrameAnalyticsAction.Request.TIMEOUT.getPreferredName())) { - TimeValue timeout = restRequest.paramAsTime(StartDataFrameAnalyticsAction.Request.TIMEOUT.getPreferredName(), - request.getTimeout()); + TimeValue timeout = restRequest.paramAsTime( + StartDataFrameAnalyticsAction.Request.TIMEOUT.getPreferredName(), + request.getTimeout() + ); request.setTimeout(timeout); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/dataframe/RestStopDataFrameAnalyticsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/dataframe/RestStopDataFrameAnalyticsAction.java index a177494a6910a..072a7ecb8b6d5 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/dataframe/RestStopDataFrameAnalyticsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/dataframe/RestStopDataFrameAnalyticsAction.java @@ -23,9 +23,7 @@ public class RestStopDataFrameAnalyticsAction extends BaseRestHandler { @Override public List routes() { - return List.of( - new Route(POST, BASE_PATH + "data_frame/analytics/{" + DataFrameAnalyticsConfig.ID + "}/_stop") - ); + return List.of(new Route(POST, BASE_PATH + "data_frame/analytics/{" + DataFrameAnalyticsConfig.ID + "}/_stop")); } @Override @@ -41,10 +39,12 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient request = StopDataFrameAnalyticsAction.Request.parseRequest(id, restRequest.contentOrSourceParamParser()); } else { request = new StopDataFrameAnalyticsAction.Request(id); - request.setTimeout(restRequest.paramAsTime(StopDataFrameAnalyticsAction.Request.TIMEOUT.getPreferredName(), - request.getTimeout())); - request.setAllowNoMatch(restRequest.paramAsBoolean(StopDataFrameAnalyticsAction.Request.ALLOW_NO_MATCH.getPreferredName(), - request.allowNoMatch())); + request.setTimeout( + restRequest.paramAsTime(StopDataFrameAnalyticsAction.Request.TIMEOUT.getPreferredName(), request.getTimeout()) + ); + request.setAllowNoMatch( + restRequest.paramAsBoolean(StopDataFrameAnalyticsAction.Request.ALLOW_NO_MATCH.getPreferredName(), request.allowNoMatch()) + ); request.setForce(restRequest.paramAsBoolean(StopDataFrameAnalyticsAction.Request.FORCE.getPreferredName(), request.isForce())); } return channel -> client.execute(StopDataFrameAnalyticsAction.INSTANCE, request, new RestToXContentListener<>(channel)); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/filter/RestDeleteFilterAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/filter/RestDeleteFilterAction.java index 5a6a2a9825261..4d2db8163eac3 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/filter/RestDeleteFilterAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/filter/RestDeleteFilterAction.java @@ -27,7 +27,8 @@ public class RestDeleteFilterAction extends BaseRestHandler { public List routes() { return List.of( Route.builder(DELETE, BASE_PATH + "filters/{" + Request.FILTER_ID + "}") - .replaces(DELETE, PRE_V7_BASE_PATH + "filters/{" + Request.FILTER_ID + "}", RestApiVersion.V_7).build() + .replaces(DELETE, PRE_V7_BASE_PATH + "filters/{" + Request.FILTER_ID + "}", RestApiVersion.V_7) + .build() ); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/filter/RestGetFiltersAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/filter/RestGetFiltersAction.java index f762f86a5bc70..5505dc3446b9f 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/filter/RestGetFiltersAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/filter/RestGetFiltersAction.java @@ -29,9 +29,9 @@ public class RestGetFiltersAction extends BaseRestHandler { public List routes() { return List.of( Route.builder(GET, BASE_PATH + "filters/{" + MlFilter.ID + "}") - .replaces(GET, PRE_V7_BASE_PATH + "filters/{" + MlFilter.ID + "}", RestApiVersion.V_7).build(), - Route.builder(GET, BASE_PATH + "filters/") - .replaces(GET, PRE_V7_BASE_PATH + "filters/", RestApiVersion.V_7).build() + .replaces(GET, PRE_V7_BASE_PATH + "filters/{" + MlFilter.ID + "}", RestApiVersion.V_7) + .build(), + Route.builder(GET, BASE_PATH + "filters/").replaces(GET, PRE_V7_BASE_PATH + "filters/", RestApiVersion.V_7).build() ); } @@ -49,8 +49,11 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient } if (restRequest.hasParam(PageParams.FROM.getPreferredName()) || restRequest.hasParam(PageParams.SIZE.getPreferredName())) { request.setPageParams( - new PageParams(restRequest.paramAsInt(PageParams.FROM.getPreferredName(), PageParams.DEFAULT_FROM), - restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE))); + new PageParams( + restRequest.paramAsInt(PageParams.FROM.getPreferredName(), PageParams.DEFAULT_FROM), + restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE) + ) + ); } return channel -> client.execute(GetFiltersAction.INSTANCE, request, new RestStatusToXContentListener<>(channel)); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/filter/RestPutFilterAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/filter/RestPutFilterAction.java index 15970e413583d..5327a967e1c09 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/filter/RestPutFilterAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/filter/RestPutFilterAction.java @@ -7,11 +7,11 @@ package org.elasticsearch.xpack.ml.rest.filter; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestToXContentListener; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.PutFilterAction; import org.elasticsearch.xpack.core.ml.job.config.MlFilter; @@ -28,7 +28,8 @@ public class RestPutFilterAction extends BaseRestHandler { public List routes() { return List.of( Route.builder(PUT, BASE_PATH + "filters/{" + MlFilter.ID + "}") - .replaces(PUT, PRE_V7_BASE_PATH + "filters/{" + MlFilter.ID + "}", RestApiVersion.V_7).build() + .replaces(PUT, PRE_V7_BASE_PATH + "filters/{" + MlFilter.ID + "}", RestApiVersion.V_7) + .build() ); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/filter/RestUpdateFilterAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/filter/RestUpdateFilterAction.java index e32c02d03d065..09c7f17ccc0f8 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/filter/RestUpdateFilterAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/filter/RestUpdateFilterAction.java @@ -7,11 +7,11 @@ package org.elasticsearch.xpack.ml.rest.filter; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestToXContentListener; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.UpdateFilterAction; import org.elasticsearch.xpack.core.ml.job.config.MlFilter; @@ -24,11 +24,12 @@ public class RestUpdateFilterAction extends BaseRestHandler { - @Override + @Override public List routes() { return List.of( Route.builder(POST, BASE_PATH + "filters/{" + MlFilter.ID + "}/_update") - .replaces(POST, PRE_V7_BASE_PATH + "filters/{" + MlFilter.ID + "}/_update", RestApiVersion.V_7).build() + .replaces(POST, PRE_V7_BASE_PATH + "filters/{" + MlFilter.ID + "}/_update", RestApiVersion.V_7) + .build() ); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestDeleteTrainedModelAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestDeleteTrainedModelAction.java index d4d8ab3d2b09d..d78d7339b63f2 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestDeleteTrainedModelAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestDeleteTrainedModelAction.java @@ -29,7 +29,8 @@ public class RestDeleteTrainedModelAction extends BaseRestHandler { public List routes() { return List.of( Route.builder(DELETE, BASE_PATH + "trained_models/{" + TrainedModelConfig.MODEL_ID + "}") - .replaces(DELETE, BASE_PATH + "inference/{" + TrainedModelConfig.MODEL_ID + "}", RestApiVersion.V_8).build() + .replaces(DELETE, BASE_PATH + "inference/{" + TrainedModelConfig.MODEL_ID + "}", RestApiVersion.V_8) + .build() ); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestGetTrainedModelDeploymentStatsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestGetTrainedModelDeploymentStatsAction.java index c6c1ca8ca754a..3c866b49f3b7f 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestGetTrainedModelDeploymentStatsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestGetTrainedModelDeploymentStatsAction.java @@ -31,9 +31,14 @@ public String getName() { @Override public List routes() { return Collections.singletonList( - new Route(GET, - MachineLearning.BASE_PATH + "trained_models/{" + - StartTrainedModelDeploymentAction.Request.MODEL_ID.getPreferredName() + "}/deployment/_stats")); + new Route( + GET, + MachineLearning.BASE_PATH + + "trained_models/{" + + StartTrainedModelDeploymentAction.Request.MODEL_ID.getPreferredName() + + "}/deployment/_stats" + ) + ); } @Override @@ -41,9 +46,7 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient String modelId = restRequest.param(StartTrainedModelDeploymentAction.Request.MODEL_ID.getPreferredName()); GetDeploymentStatsAction.Request request = new GetDeploymentStatsAction.Request(modelId); - request.setAllowNoMatch( - restRequest.paramAsBoolean( - GetDeploymentStatsAction.Request.ALLOW_NO_MATCH, request.isAllowNoMatch())); + request.setAllowNoMatch(restRequest.paramAsBoolean(GetDeploymentStatsAction.Request.ALLOW_NO_MATCH, request.isAllowNoMatch())); return channel -> client.execute(GetDeploymentStatsAction.INSTANCE, request, new RestToXContentListener<>(channel)); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestGetTrainedModelsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestGetTrainedModelsAction.java index 1ede539da7310..8348fae322010 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestGetTrainedModelsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestGetTrainedModelsAction.java @@ -9,18 +9,18 @@ import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.common.Strings; -import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.common.logging.DeprecationCategory; import org.elasticsearch.common.logging.DeprecationLogger; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.ToXContentObject; -import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestChannel; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.action.RestToXContentListener; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.ToXContentObject; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.action.util.PageParams; import org.elasticsearch.xpack.core.ml.action.GetTrainedModelsAction; import org.elasticsearch.xpack.core.ml.inference.TrainedModelConfig; @@ -48,14 +48,16 @@ public class RestGetTrainedModelsAction extends BaseRestHandler { public List routes() { return List.of( Route.builder(GET, BASE_PATH + "trained_models/{" + TrainedModelConfig.MODEL_ID + "}") - .replaces(GET, BASE_PATH + "inference/{" + TrainedModelConfig.MODEL_ID + "}", RestApiVersion.V_8).build(), - Route.builder(GET, BASE_PATH + "trained_models") - .replaces(GET, BASE_PATH + "inference", RestApiVersion.V_8).build() + .replaces(GET, BASE_PATH + "inference/{" + TrainedModelConfig.MODEL_ID + "}", RestApiVersion.V_8) + .build(), + Route.builder(GET, BASE_PATH + "trained_models").replaces(GET, BASE_PATH + "inference", RestApiVersion.V_8).build() ); } - private static final Map DEFAULT_TO_XCONTENT_VALUES = - Collections.singletonMap(TrainedModelConfig.DECOMPRESS_DEFINITION, Boolean.toString(true)); + private static final Map DEFAULT_TO_XCONTENT_VALUES = Collections.singletonMap( + TrainedModelConfig.DECOMPRESS_DEFINITION, + Boolean.toString(true) + ); @Override public String getName() { @@ -70,31 +72,34 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient } List tags = asList(restRequest.paramAsStringArray(TrainedModelConfig.TAGS.getPreferredName(), Strings.EMPTY_ARRAY)); Set includes = new HashSet<>( - asList( - restRequest.paramAsStringArray( - GetTrainedModelsAction.Request.INCLUDE.getPreferredName(), - Strings.EMPTY_ARRAY))); + asList(restRequest.paramAsStringArray(GetTrainedModelsAction.Request.INCLUDE.getPreferredName(), Strings.EMPTY_ARRAY)) + ); final GetTrainedModelsAction.Request request; if (restRequest.hasParam(INCLUDE_MODEL_DEFINITION)) { deprecationLogger.critical( DeprecationCategory.API, INCLUDE_MODEL_DEFINITION, "[{}] parameter is deprecated! Use [include=definition] instead.", - INCLUDE_MODEL_DEFINITION); - request = new GetTrainedModelsAction.Request(modelId, - restRequest.paramAsBoolean(INCLUDE_MODEL_DEFINITION, false), - tags); + INCLUDE_MODEL_DEFINITION + ); + request = new GetTrainedModelsAction.Request(modelId, restRequest.paramAsBoolean(INCLUDE_MODEL_DEFINITION, false), tags); } else { request = new GetTrainedModelsAction.Request(modelId, tags, includes); } if (restRequest.hasParam(PageParams.FROM.getPreferredName()) || restRequest.hasParam(PageParams.SIZE.getPreferredName())) { - request.setPageParams(new PageParams(restRequest.paramAsInt(PageParams.FROM.getPreferredName(), PageParams.DEFAULT_FROM), - restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE))); + request.setPageParams( + new PageParams( + restRequest.paramAsInt(PageParams.FROM.getPreferredName(), PageParams.DEFAULT_FROM), + restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE) + ) + ); } request.setAllowNoResources(restRequest.paramAsBoolean(ALLOW_NO_MATCH.getPreferredName(), request.isAllowNoResources())); - return channel -> client.execute(GetTrainedModelsAction.INSTANCE, + return channel -> client.execute( + GetTrainedModelsAction.INSTANCE, request, - new RestToXContentListenerWithDefaultValues<>(channel, DEFAULT_TO_XCONTENT_VALUES)); + new RestToXContentListenerWithDefaultValues<>(channel, DEFAULT_TO_XCONTENT_VALUES) + ); } @Override @@ -112,11 +117,9 @@ private RestToXContentListenerWithDefaultValues(RestChannel channel, Map params = new HashMap<>(channel.request().params()); - defaultToXContentParamValues.forEach((k, v) -> - params.computeIfAbsent(k, defaultToXContentParamValues::get) - ); + defaultToXContentParamValues.forEach((k, v) -> params.computeIfAbsent(k, defaultToXContentParamValues::get)); response.toXContent(builder, new ToXContent.MapParams(params)); return new BytesRestResponse(getStatus(response), builder); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestGetTrainedModelsStatsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestGetTrainedModelsStatsAction.java index 8c3cd9be0d874..f30d65c1a9181 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestGetTrainedModelsStatsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestGetTrainedModelsStatsAction.java @@ -30,9 +30,11 @@ public class RestGetTrainedModelsStatsAction extends BaseRestHandler { public List routes() { return List.of( Route.builder(GET, BASE_PATH + "trained_models/{" + TrainedModelConfig.MODEL_ID + "}/_stats") - .replaces(GET, BASE_PATH + "inference/{" + TrainedModelConfig.MODEL_ID + "}/_stats", RestApiVersion.V_8).build(), + .replaces(GET, BASE_PATH + "inference/{" + TrainedModelConfig.MODEL_ID + "}/_stats", RestApiVersion.V_8) + .build(), Route.builder(GET, BASE_PATH + "trained_models/_stats") - .replaces(GET, BASE_PATH + "inference/_stats", RestApiVersion.V_8).build() + .replaces(GET, BASE_PATH + "inference/_stats", RestApiVersion.V_8) + .build() ); } @@ -49,8 +51,12 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient } GetTrainedModelsStatsAction.Request request = new GetTrainedModelsStatsAction.Request(modelId); if (restRequest.hasParam(PageParams.FROM.getPreferredName()) || restRequest.hasParam(PageParams.SIZE.getPreferredName())) { - request.setPageParams(new PageParams(restRequest.paramAsInt(PageParams.FROM.getPreferredName(), PageParams.DEFAULT_FROM), - restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE))); + request.setPageParams( + new PageParams( + restRequest.paramAsInt(PageParams.FROM.getPreferredName(), PageParams.DEFAULT_FROM), + restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE) + ) + ); } request.setAllowNoResources(restRequest.paramAsBoolean(ALLOW_NO_MATCH.getPreferredName(), request.isAllowNoResources())); return channel -> client.execute(GetTrainedModelsStatsAction.INSTANCE, request, new RestToXContentListener<>(channel)); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestInferTrainedModelDeploymentAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestInferTrainedModelDeploymentAction.java index 61c9d089de4b0..6ced28e2bea2e 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestInferTrainedModelDeploymentAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestInferTrainedModelDeploymentAction.java @@ -33,9 +33,7 @@ public String getName() { @Override public List routes() { return Collections.singletonList( - new Route( - POST, - BASE_PATH + "trained_models/{" + TrainedModelConfig.MODEL_ID.getPreferredName() + "}/deployment/_infer") + new Route(POST, BASE_PATH + "trained_models/{" + TrainedModelConfig.MODEL_ID.getPreferredName() + "}/deployment/_infer") ); } @@ -45,12 +43,16 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient if (restRequest.hasContent() == false) { throw ExceptionsHelper.badRequestException("requires body"); } - InferTrainedModelDeploymentAction.Request.Builder request = - InferTrainedModelDeploymentAction.Request.parseRequest(deploymentId, restRequest.contentParser()); + InferTrainedModelDeploymentAction.Request.Builder request = InferTrainedModelDeploymentAction.Request.parseRequest( + deploymentId, + restRequest.contentParser() + ); if (restRequest.hasParam(InferTrainedModelDeploymentAction.Request.TIMEOUT.getPreferredName())) { - TimeValue inferTimeout = restRequest.paramAsTime(InferTrainedModelDeploymentAction.Request.TIMEOUT.getPreferredName(), - InferTrainedModelDeploymentAction.Request.DEFAULT_TIMEOUT); + TimeValue inferTimeout = restRequest.paramAsTime( + InferTrainedModelDeploymentAction.Request.TIMEOUT.getPreferredName(), + InferTrainedModelDeploymentAction.Request.DEFAULT_TIMEOUT + ); request.setInferenceTimeout(inferTimeout); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestPutTrainedModelAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestPutTrainedModelAction.java index 47dcc99fb6bff..b9d9ad3cc9b92 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestPutTrainedModelAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestPutTrainedModelAction.java @@ -8,10 +8,10 @@ import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.core.RestApiVersion; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestToXContentListener; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.PutTrainedModelAction; import org.elasticsearch.xpack.core.ml.inference.TrainedModelConfig; @@ -27,7 +27,8 @@ public class RestPutTrainedModelAction extends BaseRestHandler { public List routes() { return List.of( Route.builder(PUT, BASE_PATH + "trained_models/{" + TrainedModelConfig.MODEL_ID + "}") - .replaces(PUT, BASE_PATH + "inference/{" + TrainedModelConfig.MODEL_ID + "}", RestApiVersion.V_8).build() + .replaces(PUT, BASE_PATH + "inference/{" + TrainedModelConfig.MODEL_ID + "}", RestApiVersion.V_8) + .build() ); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestPutTrainedModelDefinitionPartAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestPutTrainedModelDefinitionPartAction.java index 517d2ed783d23..f81683651c34d 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestPutTrainedModelDefinitionPartAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestPutTrainedModelDefinitionPartAction.java @@ -7,10 +7,10 @@ package org.elasticsearch.xpack.ml.rest.inference; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestToXContentListener; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.PutTrainedModelDefinitionPartAction; import org.elasticsearch.xpack.core.ml.inference.TrainedModelConfig; diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestPutTrainedModelVocabularyAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestPutTrainedModelVocabularyAction.java index 3e35677383c53..daf050dcc3fe0 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestPutTrainedModelVocabularyAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestPutTrainedModelVocabularyAction.java @@ -7,10 +7,10 @@ package org.elasticsearch.xpack.ml.rest.inference; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestToXContentListener; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.PutTrainedModelVocabularyAction; import org.elasticsearch.xpack.core.ml.inference.TrainedModelConfig; @@ -25,13 +25,7 @@ public class RestPutTrainedModelVocabularyAction extends BaseRestHandler { @Override public List routes() { return List.of( - Route.builder( - PUT, - BASE_PATH - + "trained_models/{" - + TrainedModelConfig.MODEL_ID.getPreferredName() - + "}/vocabulary" - ).build() + Route.builder(PUT, BASE_PATH + "trained_models/{" + TrainedModelConfig.MODEL_ID.getPreferredName() + "}/vocabulary").build() ); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestStartTrainedModelDeploymentAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestStartTrainedModelDeploymentAction.java index f3cf06bf70684..d9d80dad29285 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestStartTrainedModelDeploymentAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestStartTrainedModelDeploymentAction.java @@ -37,9 +37,14 @@ public String getName() { @Override public List routes() { return Collections.singletonList( - new Route(POST, - MachineLearning.BASE_PATH + "trained_models/{" + - StartTrainedModelDeploymentAction.Request.MODEL_ID.getPreferredName() + "}/deployment/_start")); + new Route( + POST, + MachineLearning.BASE_PATH + + "trained_models/{" + + StartTrainedModelDeploymentAction.Request.MODEL_ID.getPreferredName() + + "}/deployment/_start" + ) + ); } @Override @@ -51,13 +56,15 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient } else { request = new StartTrainedModelDeploymentAction.Request(modelId); if (restRequest.hasParam(TIMEOUT.getPreferredName())) { - TimeValue openTimeout = restRequest.paramAsTime(TIMEOUT.getPreferredName(), - StartTrainedModelDeploymentAction.DEFAULT_TIMEOUT); + TimeValue openTimeout = restRequest.paramAsTime( + TIMEOUT.getPreferredName(), + StartTrainedModelDeploymentAction.DEFAULT_TIMEOUT + ); request.setTimeout(openTimeout); } - request.setWaitForState(AllocationStatus.State.fromString( - restRequest.param(WAIT_FOR.getPreferredName(), AllocationStatus.State.STARTED.toString()) - )); + request.setWaitForState( + AllocationStatus.State.fromString(restRequest.param(WAIT_FOR.getPreferredName(), AllocationStatus.State.STARTED.toString())) + ); request.setInferenceThreads(restRequest.paramAsInt(INFERENCE_THREADS.getPreferredName(), request.getInferenceThreads())); request.setModelThreads(restRequest.paramAsInt(MODEL_THREADS.getPreferredName(), request.getModelThreads())); request.setQueueCapacity(restRequest.paramAsInt(QUEUE_CAPACITY.getPreferredName(), request.getQueueCapacity())); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestStopTrainedModelDeploymentAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestStopTrainedModelDeploymentAction.java index 90ca93371255a..dd83734554c6f 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestStopTrainedModelDeploymentAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestStopTrainedModelDeploymentAction.java @@ -31,9 +31,7 @@ public String getName() { @Override public List routes() { return Collections.singletonList( - new Route( - POST, - BASE_PATH + "trained_models/{" + TrainedModelConfig.MODEL_ID.getPreferredName() + "}/deployment/_stop") + new Route(POST, BASE_PATH + "trained_models/{" + TrainedModelConfig.MODEL_ID.getPreferredName() + "}/deployment/_stop") ); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestCloseJobAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestCloseJobAction.java index 6af821f3cd82a..dd02cf632e768 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestCloseJobAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestCloseJobAction.java @@ -7,9 +7,9 @@ package org.elasticsearch.xpack.ml.rest.job; import org.elasticsearch.client.node.NodeClient; +import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestToXContentListener; @@ -30,7 +30,8 @@ public class RestCloseJobAction extends BaseRestHandler { public List routes() { return List.of( Route.builder(POST, BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/_close") - .replaces(POST, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/_close", RestApiVersion.V_7).build() + .replaces(POST, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/_close", RestApiVersion.V_7) + .build() ); } @@ -47,20 +48,27 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient } else { request = new Request(restRequest.param(Job.ID.getPreferredName())); if (restRequest.hasParam(Request.TIMEOUT.getPreferredName())) { - request.setCloseTimeout(TimeValue.parseTimeValue( - restRequest.param(Request.TIMEOUT.getPreferredName()), Request.TIMEOUT.getPreferredName())); + request.setCloseTimeout( + TimeValue.parseTimeValue(restRequest.param(Request.TIMEOUT.getPreferredName()), Request.TIMEOUT.getPreferredName()) + ); } if (restRequest.hasParam(Request.FORCE.getPreferredName())) { request.setForce(restRequest.paramAsBoolean(Request.FORCE.getPreferredName(), request.isForce())); } if (restRequest.hasParam(Request.ALLOW_NO_JOBS)) { LoggingDeprecationHandler.INSTANCE.logRenamedField( - null, () -> null, Request.ALLOW_NO_JOBS, Request.ALLOW_NO_MATCH.getPreferredName()); + null, + () -> null, + Request.ALLOW_NO_JOBS, + Request.ALLOW_NO_MATCH.getPreferredName() + ); } request.setAllowNoMatch( restRequest.paramAsBoolean( Request.ALLOW_NO_MATCH.getPreferredName(), - restRequest.paramAsBoolean(Request.ALLOW_NO_JOBS, request.allowNoMatch()))); + restRequest.paramAsBoolean(Request.ALLOW_NO_JOBS, request.allowNoMatch()) + ) + ); } return channel -> client.execute(CloseJobAction.INSTANCE, request, new RestToXContentListener<>(channel)); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestDeleteForecastAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestDeleteForecastAction.java index 752fd656e8abe..1a0591383b524 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestDeleteForecastAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestDeleteForecastAction.java @@ -29,10 +29,15 @@ public class RestDeleteForecastAction extends BaseRestHandler { public List routes() { return List.of( Route.builder(DELETE, BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/_forecast/") - .replaces(DELETE, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/_forecast/", RestApiVersion.V_7).build(), + .replaces(DELETE, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/_forecast/", RestApiVersion.V_7) + .build(), Route.builder(DELETE, BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/_forecast/{" + Forecast.FORECAST_ID + "}") - .replaces(DELETE, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/_forecast/{" + Forecast.FORECAST_ID + "}", - RestApiVersion.V_7).build() + .replaces( + DELETE, + PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/_forecast/{" + Forecast.FORECAST_ID + "}", + RestApiVersion.V_7 + ) + .build() ); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestDeleteJobAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestDeleteJobAction.java index c84ea38d316ea..0b7a96512b76f 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestDeleteJobAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestDeleteJobAction.java @@ -7,7 +7,6 @@ package org.elasticsearch.xpack.ml.rest.job; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.BytesRestResponse; @@ -16,6 +15,7 @@ import org.elasticsearch.rest.action.RestToXContentListener; import org.elasticsearch.tasks.Task; import org.elasticsearch.tasks.TaskListener; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ml.action.CloseJobAction; import org.elasticsearch.xpack.core.ml.action.DeleteJobAction; import org.elasticsearch.xpack.core.ml.job.config.Job; @@ -33,7 +33,8 @@ public class RestDeleteJobAction extends BaseRestHandler { public List routes() { return List.of( Route.builder(DELETE, BASE_PATH + "anomaly_detectors/{" + Job.ID + "}") - .replaces(DELETE, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}", RestApiVersion.V_7).build() + .replaces(DELETE, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}", RestApiVersion.V_7) + .build() ); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestEstimateModelMemoryAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestEstimateModelMemoryAction.java index 09755fd14290c..5fa453744f851 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestEstimateModelMemoryAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestEstimateModelMemoryAction.java @@ -32,8 +32,9 @@ public String getName() { @Override protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) throws IOException { - EstimateModelMemoryAction.Request request = - EstimateModelMemoryAction.Request.parseRequest(restRequest.contentOrSourceParamParser()); + EstimateModelMemoryAction.Request request = EstimateModelMemoryAction.Request.parseRequest( + restRequest.contentOrSourceParamParser() + ); return channel -> client.execute(EstimateModelMemoryAction.INSTANCE, request, new RestToXContentListener<>(channel)); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestFlushJobAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestFlushJobAction.java index 501f427c8bfd6..0518c50761052 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestFlushJobAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestFlushJobAction.java @@ -7,11 +7,11 @@ package org.elasticsearch.xpack.ml.rest.job; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestToXContentListener; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.FlushJobAction; import org.elasticsearch.xpack.core.ml.job.config.Job; @@ -34,7 +34,8 @@ public class RestFlushJobAction extends BaseRestHandler { public List routes() { return List.of( Route.builder(POST, BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/_flush") - .replaces(POST, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/_flush", RestApiVersion.V_7).build() + .replaces(POST, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/_flush", RestApiVersion.V_7) + .build() ); } @@ -52,8 +53,9 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient request = FlushJobAction.Request.parseRequest(jobId, parser); } else { request = new FlushJobAction.Request(restRequest.param(Job.ID.getPreferredName())); - request.setCalcInterim(restRequest.paramAsBoolean(FlushJobAction.Request.CALC_INTERIM.getPreferredName(), - DEFAULT_CALC_INTERIM)); + request.setCalcInterim( + restRequest.paramAsBoolean(FlushJobAction.Request.CALC_INTERIM.getPreferredName(), DEFAULT_CALC_INTERIM) + ); request.setStart(restRequest.param(FlushJobAction.Request.START.getPreferredName(), DEFAULT_START)); request.setEnd(restRequest.param(FlushJobAction.Request.END.getPreferredName(), DEFAULT_END)); request.setAdvanceTime(restRequest.param(FlushJobAction.Request.ADVANCE_TIME.getPreferredName(), DEFAULT_ADVANCE_TIME)); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestForecastJobAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestForecastJobAction.java index 1249b583f2630..748d860b4f6fb 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestForecastJobAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestForecastJobAction.java @@ -8,11 +8,11 @@ import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.common.unit.ByteSizeValue; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestToXContentListener; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.ForecastJobAction; import org.elasticsearch.xpack.core.ml.job.config.Job; @@ -29,7 +29,8 @@ public class RestForecastJobAction extends BaseRestHandler { public List routes() { return List.of( Route.builder(POST, BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/_forecast") - .replaces(POST, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/_forecast", RestApiVersion.V_7).build() + .replaces(POST, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/_forecast", RestApiVersion.V_7) + .build() ); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestGetJobStatsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestGetJobStatsAction.java index 282cfb6151880..b9261487a0880 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestGetJobStatsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestGetJobStatsAction.java @@ -31,9 +31,11 @@ public class RestGetJobStatsAction extends BaseRestHandler { public List routes() { return List.of( Route.builder(GET, BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/_stats") - .replaces(GET, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/_stats", RestApiVersion.V_7).build(), + .replaces(GET, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/_stats", RestApiVersion.V_7) + .build(), Route.builder(GET, BASE_PATH + "anomaly_detectors/_stats") - .replaces(GET, PRE_V7_BASE_PATH + "anomaly_detectors/_stats", RestApiVersion.V_7).build() + .replaces(GET, PRE_V7_BASE_PATH + "anomaly_detectors/_stats", RestApiVersion.V_7) + .build() ); } @@ -53,9 +55,8 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient LoggingDeprecationHandler.INSTANCE.logRenamedField(null, () -> null, Request.ALLOW_NO_JOBS, Request.ALLOW_NO_MATCH); } request.setAllowNoMatch( - restRequest.paramAsBoolean( - Request.ALLOW_NO_MATCH, - restRequest.paramAsBoolean(Request.ALLOW_NO_JOBS, request.allowNoMatch()))); + restRequest.paramAsBoolean(Request.ALLOW_NO_MATCH, restRequest.paramAsBoolean(Request.ALLOW_NO_JOBS, request.allowNoMatch())) + ); return channel -> client.execute(GetJobsStatsAction.INSTANCE, request, new RestToXContentListener<>(channel)); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestGetJobsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestGetJobsAction.java index 9cc0bde4ef2d2..e8d5bff820ddc 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestGetJobsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestGetJobsAction.java @@ -34,9 +34,11 @@ public class RestGetJobsAction extends BaseRestHandler { public List routes() { return List.of( Route.builder(GET, BASE_PATH + "anomaly_detectors/{" + Job.ID + "}") - .replaces(GET, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}", RestApiVersion.V_7).build(), + .replaces(GET, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}", RestApiVersion.V_7) + .build(), Route.builder(GET, BASE_PATH + "anomaly_detectors") - .replaces(GET, PRE_V7_BASE_PATH + "anomaly_detectors", RestApiVersion.V_7).build() + .replaces(GET, PRE_V7_BASE_PATH + "anomaly_detectors", RestApiVersion.V_7) + .build() ); } @@ -56,9 +58,8 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient LoggingDeprecationHandler.INSTANCE.logRenamedField(null, () -> null, Request.ALLOW_NO_JOBS, Request.ALLOW_NO_MATCH); } request.setAllowNoMatch( - restRequest.paramAsBoolean( - Request.ALLOW_NO_MATCH, - restRequest.paramAsBoolean(Request.ALLOW_NO_JOBS, request.allowNoMatch()))); + restRequest.paramAsBoolean(Request.ALLOW_NO_MATCH, restRequest.paramAsBoolean(Request.ALLOW_NO_JOBS, request.allowNoMatch())) + ); return channel -> client.execute(GetJobsAction.INSTANCE, request, new RestToXContentListener<>(channel)); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestOpenJobAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestOpenJobAction.java index 732f1361bf357..92c1fe8646830 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestOpenJobAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestOpenJobAction.java @@ -9,13 +9,13 @@ import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ml.action.NodeAcknowledgedResponse; import org.elasticsearch.xpack.core.ml.action.OpenJobAction; import org.elasticsearch.xpack.core.ml.job.config.Job; @@ -33,7 +33,8 @@ public class RestOpenJobAction extends BaseRestHandler { public List routes() { return List.of( Route.builder(POST, BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/_open") - .replaces(POST, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/_open", RestApiVersion.V_7).build() + .replaces(POST, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/_open", RestApiVersion.V_7) + .build() ); } @@ -50,8 +51,10 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient } else { OpenJobAction.JobParams jobParams = new OpenJobAction.JobParams(restRequest.param(Job.ID.getPreferredName())); if (restRequest.hasParam(OpenJobAction.JobParams.TIMEOUT.getPreferredName())) { - TimeValue openTimeout = restRequest.paramAsTime(OpenJobAction.JobParams.TIMEOUT.getPreferredName(), - TimeValue.timeValueSeconds(20)); + TimeValue openTimeout = restRequest.paramAsTime( + OpenJobAction.JobParams.TIMEOUT.getPreferredName(), + TimeValue.timeValueSeconds(20) + ); jobParams.setTimeout(openTimeout); } request = new OpenJobAction.Request(jobParams); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestPostDataAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestPostDataAction.java index eda9032b548d5..f1522cf7f62ba 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestPostDataAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestPostDataAction.java @@ -28,8 +28,8 @@ public class RestPostDataAction extends BaseRestHandler { @Override public List routes() { - final String msg = "Posting data directly to anomaly detection jobs is deprecated, " + - "in a future major version it will be compulsory to use a datafeed"; + final String msg = "Posting data directly to anomaly detection jobs is deprecated, " + + "in a future major version it will be compulsory to use a datafeed"; return List.of( Route.builder(POST, BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/_data").deprecated(msg, RestApiVersion.V_8).build(), Route.builder(POST, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/_data").deprecated(msg, RestApiVersion.V_7).build() diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestPostJobUpdateAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestPostJobUpdateAction.java index 97bb44205b498..7af9dbd420f91 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestPostJobUpdateAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestPostJobUpdateAction.java @@ -7,11 +7,11 @@ package org.elasticsearch.xpack.ml.rest.job; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestToXContentListener; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.UpdateJobAction; import org.elasticsearch.xpack.core.ml.job.config.Job; @@ -28,7 +28,8 @@ public class RestPostJobUpdateAction extends BaseRestHandler { public List routes() { return List.of( Route.builder(POST, BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/_update") - .replaces(POST, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/_update", RestApiVersion.V_7).build() + .replaces(POST, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/_update", RestApiVersion.V_7) + .build() ); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestPutJobAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestPutJobAction.java index f3b4244f246a8..bc8726335be0c 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestPutJobAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestPutJobAction.java @@ -9,11 +9,11 @@ import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestToXContentListener; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.PutJobAction; import org.elasticsearch.xpack.core.ml.job.config.Job; @@ -30,7 +30,8 @@ public class RestPutJobAction extends BaseRestHandler { public List routes() { return List.of( Route.builder(PUT, BASE_PATH + "anomaly_detectors/{" + Job.ID + "}") - .replaces(PUT, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}", RestApiVersion.V_7).build() + .replaces(PUT, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}", RestApiVersion.V_7) + .build() ); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestResetJobAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestResetJobAction.java index 392add35d85c2..4fda344026486 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestResetJobAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestResetJobAction.java @@ -8,7 +8,6 @@ package org.elasticsearch.xpack.ml.rest.job; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; @@ -16,6 +15,7 @@ import org.elasticsearch.rest.action.RestToXContentListener; import org.elasticsearch.tasks.Task; import org.elasticsearch.tasks.TaskListener; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ml.action.ResetJobAction; import org.elasticsearch.xpack.core.ml.job.config.Job; @@ -29,9 +29,7 @@ public class RestResetJobAction extends BaseRestHandler { @Override public List routes() { - return List.of( - new Route(POST, BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/_reset") - ); + return List.of(new Route(POST, BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/_reset")); } @Override diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/modelsnapshots/RestDeleteModelSnapshotAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/modelsnapshots/RestDeleteModelSnapshotAction.java index 960715ad8504a..974c800b72d9a 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/modelsnapshots/RestDeleteModelSnapshotAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/modelsnapshots/RestDeleteModelSnapshotAction.java @@ -24,12 +24,16 @@ public class RestDeleteModelSnapshotAction extends BaseRestHandler { - @Override + @Override public List routes() { return List.of( Route.builder(DELETE, BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/model_snapshots/{" + SNAPSHOT_ID + "}") - .replaces(DELETE, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/model_snapshots/{" + SNAPSHOT_ID + "}", - RestApiVersion.V_7).build() + .replaces( + DELETE, + PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/model_snapshots/{" + SNAPSHOT_ID + "}", + RestApiVersion.V_7 + ) + .build() ); } @@ -41,8 +45,9 @@ public String getName() { @Override protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) throws IOException { DeleteModelSnapshotAction.Request deleteModelSnapshot = new DeleteModelSnapshotAction.Request( - restRequest.param(Job.ID.getPreferredName()), - restRequest.param(SNAPSHOT_ID.getPreferredName())); + restRequest.param(Job.ID.getPreferredName()), + restRequest.param(SNAPSHOT_ID.getPreferredName()) + ); return channel -> client.execute(DeleteModelSnapshotAction.INSTANCE, deleteModelSnapshot, new RestToXContentListener<>(channel)); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/modelsnapshots/RestGetModelSnapshotsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/modelsnapshots/RestGetModelSnapshotsAction.java index 13037a98d98f7..e0afb43a1a963 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/modelsnapshots/RestGetModelSnapshotsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/modelsnapshots/RestGetModelSnapshotsAction.java @@ -8,11 +8,11 @@ import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.common.Strings; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestToXContentListener; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.action.util.PageParams; import org.elasticsearch.xpack.core.ml.action.GetModelSnapshotsAction; import org.elasticsearch.xpack.core.ml.action.GetModelSnapshotsAction.Request; @@ -41,15 +41,25 @@ public class RestGetModelSnapshotsAction extends BaseRestHandler { public List routes() { return List.of( Route.builder(GET, BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/model_snapshots/{" + Request.SNAPSHOT_ID + "}") - .replaces(GET, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/model_snapshots/{" + Request.SNAPSHOT_ID + "}", - RestApiVersion.V_7).build(), + .replaces( + GET, + PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/model_snapshots/{" + Request.SNAPSHOT_ID + "}", + RestApiVersion.V_7 + ) + .build(), Route.builder(POST, BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/model_snapshots/{" + Request.SNAPSHOT_ID + "}") - .replaces(POST, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/model_snapshots/{" + Request.SNAPSHOT_ID + "}", - RestApiVersion.V_7).build(), + .replaces( + POST, + PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/model_snapshots/{" + Request.SNAPSHOT_ID + "}", + RestApiVersion.V_7 + ) + .build(), Route.builder(GET, BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/model_snapshots") - .replaces(GET, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/model_snapshots", RestApiVersion.V_7).build(), + .replaces(GET, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/model_snapshots", RestApiVersion.V_7) + .build(), Route.builder(POST, BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/model_snapshots") - .replaces(POST, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/model_snapshots", RestApiVersion.V_7).build() + .replaces(POST, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/model_snapshots", RestApiVersion.V_7) + .build() ); } @@ -79,9 +89,12 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient getModelSnapshots.setEnd(restRequest.param(Request.END.getPreferredName(), DEFAULT_END)); } getModelSnapshots.setDescOrder(restRequest.paramAsBoolean(Request.DESC.getPreferredName(), DEFAULT_DESC_ORDER)); - getModelSnapshots.setPageParams(new PageParams( + getModelSnapshots.setPageParams( + new PageParams( restRequest.paramAsInt(PageParams.FROM.getPreferredName(), PageParams.DEFAULT_FROM), - restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE))); + restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE) + ) + ); } return channel -> client.execute(GetModelSnapshotsAction.INSTANCE, getModelSnapshots, new RestToXContentListener<>(channel)); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/modelsnapshots/RestRevertModelSnapshotAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/modelsnapshots/RestRevertModelSnapshotAction.java index 126bf9a030985..55c33a8b04ef1 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/modelsnapshots/RestRevertModelSnapshotAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/modelsnapshots/RestRevertModelSnapshotAction.java @@ -7,11 +7,11 @@ package org.elasticsearch.xpack.ml.rest.modelsnapshots; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestStatusToXContentListener; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.RevertModelSnapshotAction; import org.elasticsearch.xpack.core.ml.job.config.Job; @@ -31,8 +31,12 @@ public class RestRevertModelSnapshotAction extends BaseRestHandler { public List routes() { return List.of( Route.builder(POST, BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/model_snapshots/{" + SNAPSHOT_ID + "}/_revert") - .replaces(POST, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/model_snapshots/{" + SNAPSHOT_ID + "}/_revert", - RestApiVersion.V_7).build() + .replaces( + POST, + PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/model_snapshots/{" + SNAPSHOT_ID + "}/_revert", + RestApiVersion.V_7 + ) + .build() ); } @@ -51,8 +55,12 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient request = RevertModelSnapshotAction.Request.parseRequest(jobId, snapshotId, parser); } else { request = new RevertModelSnapshotAction.Request(jobId, snapshotId); - request.setDeleteInterveningResults(restRequest - .paramAsBoolean(RevertModelSnapshotAction.Request.DELETE_INTERVENING.getPreferredName(), DELETE_INTERVENING_DEFAULT)); + request.setDeleteInterveningResults( + restRequest.paramAsBoolean( + RevertModelSnapshotAction.Request.DELETE_INTERVENING.getPreferredName(), + DELETE_INTERVENING_DEFAULT + ) + ); } request.timeout(restRequest.paramAsTime("timeout", request.timeout())); request.masterNodeTimeout(restRequest.paramAsTime("master_timeout", request.masterNodeTimeout())); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/modelsnapshots/RestUpdateModelSnapshotAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/modelsnapshots/RestUpdateModelSnapshotAction.java index 0607d37ff4846..e8118b6c507d0 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/modelsnapshots/RestUpdateModelSnapshotAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/modelsnapshots/RestUpdateModelSnapshotAction.java @@ -7,11 +7,11 @@ package org.elasticsearch.xpack.ml.rest.modelsnapshots; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestStatusToXContentListener; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.UpdateModelSnapshotAction; import org.elasticsearch.xpack.core.ml.job.config.Job; @@ -28,9 +28,13 @@ public class RestUpdateModelSnapshotAction extends BaseRestHandler { @Override public List routes() { return List.of( - Route.builder(POST, BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/model_snapshots/{" + SNAPSHOT_ID +"}/_update") - .replaces(POST, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/model_snapshots/{" + SNAPSHOT_ID +"}/_update", - RestApiVersion.V_7).build() + Route.builder(POST, BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/model_snapshots/{" + SNAPSHOT_ID + "}/_update") + .replaces( + POST, + PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/model_snapshots/{" + SNAPSHOT_ID + "}/_update", + RestApiVersion.V_7 + ) + .build() ); } @@ -43,11 +47,15 @@ public String getName() { protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) throws IOException { XContentParser parser = restRequest.contentParser(); UpdateModelSnapshotAction.Request updateModelSnapshot = UpdateModelSnapshotAction.Request.parseRequest( - restRequest.param(Job.ID.getPreferredName()), - restRequest.param(SNAPSHOT_ID.getPreferredName()), - parser); + restRequest.param(Job.ID.getPreferredName()), + restRequest.param(SNAPSHOT_ID.getPreferredName()), + parser + ); - return channel -> - client.execute(UpdateModelSnapshotAction.INSTANCE, updateModelSnapshot, new RestStatusToXContentListener<>(channel)); + return channel -> client.execute( + UpdateModelSnapshotAction.INSTANCE, + updateModelSnapshot, + new RestStatusToXContentListener<>(channel) + ); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/modelsnapshots/RestUpgradeJobModelSnapshotAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/modelsnapshots/RestUpgradeJobModelSnapshotAction.java index d03c193c19394..33e5a992e488f 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/modelsnapshots/RestUpgradeJobModelSnapshotAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/modelsnapshots/RestUpgradeJobModelSnapshotAction.java @@ -26,9 +26,7 @@ public class RestUpgradeJobModelSnapshotAction extends BaseRestHandler { @Override public List routes() { - return List.of( - new Route(POST, BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/model_snapshots/{" + SNAPSHOT_ID + "}/_upgrade") - ); + return List.of(new Route(POST, BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/model_snapshots/{" + SNAPSHOT_ID + "}/_upgrade")); } @Override @@ -43,13 +41,18 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient String snapshotId = restRequest.param(SNAPSHOT_ID.getPreferredName()); TimeValue timeout = TimeValue.parseTimeValue( restRequest.param(UpgradeJobModelSnapshotAction.Request.TIMEOUT.getPreferredName(), DEFAULT_TIMEOUT.getStringRep()), - UpgradeJobModelSnapshotAction.Request.TIMEOUT.getPreferredName()); - boolean waitForCompletion = restRequest.paramAsBoolean(UpgradeJobModelSnapshotAction.Request.WAIT_FOR_COMPLETION.getPreferredName(), - false); - UpgradeJobModelSnapshotAction.Request request = new UpgradeJobModelSnapshotAction.Request(jobId, + UpgradeJobModelSnapshotAction.Request.TIMEOUT.getPreferredName() + ); + boolean waitForCompletion = restRequest.paramAsBoolean( + UpgradeJobModelSnapshotAction.Request.WAIT_FOR_COMPLETION.getPreferredName(), + false + ); + UpgradeJobModelSnapshotAction.Request request = new UpgradeJobModelSnapshotAction.Request( + jobId, snapshotId, timeout, - waitForCompletion); + waitForCompletion + ); return channel -> client.execute(UpgradeJobModelSnapshotAction.INSTANCE, request, new RestToXContentListener<>(channel)); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/results/RestGetBucketsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/results/RestGetBucketsAction.java index 434dc5dd7d590..27093856ab6f5 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/results/RestGetBucketsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/results/RestGetBucketsAction.java @@ -8,11 +8,11 @@ import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.common.Strings; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestToXContentListener; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.action.util.PageParams; import org.elasticsearch.xpack.core.ml.action.GetBucketsAction; import org.elasticsearch.xpack.core.ml.job.config.Job; @@ -32,17 +32,25 @@ public class RestGetBucketsAction extends BaseRestHandler { public List routes() { return List.of( Route.builder(GET, BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/results/buckets/{" + Result.TIMESTAMP + "}") - .replaces(GET, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/results/buckets/{" + Result.TIMESTAMP + "}", - RestApiVersion.V_7).build(), + .replaces( + GET, + PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/results/buckets/{" + Result.TIMESTAMP + "}", + RestApiVersion.V_7 + ) + .build(), Route.builder(POST, BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/results/buckets/{" + Result.TIMESTAMP + "}") - .replaces(POST, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/results/buckets/{" + Result.TIMESTAMP + "}", - RestApiVersion.V_7).build(), + .replaces( + POST, + PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/results/buckets/{" + Result.TIMESTAMP + "}", + RestApiVersion.V_7 + ) + .build(), Route.builder(GET, BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/results/buckets") - .replaces(GET, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/results/buckets", - RestApiVersion.V_7).build(), + .replaces(GET, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/results/buckets", RestApiVersion.V_7) + .build(), Route.builder(POST, BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/results/buckets") - .replaces(POST, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/results/buckets", - RestApiVersion.V_7).build() + .replaces(POST, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/results/buckets", RestApiVersion.V_7) + .build() ); } @@ -75,8 +83,11 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient // multiple bucket options if (restRequest.hasParam(PageParams.FROM.getPreferredName()) || restRequest.hasParam(PageParams.SIZE.getPreferredName())) { request.setPageParams( - new PageParams(restRequest.paramAsInt(PageParams.FROM.getPreferredName(), PageParams.DEFAULT_FROM), - restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE))); + new PageParams( + restRequest.paramAsInt(PageParams.FROM.getPreferredName(), PageParams.DEFAULT_FROM), + restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE) + ) + ); } if (restRequest.hasParam(GetBucketsAction.Request.START.getPreferredName())) { request.setStart(restRequest.param(GetBucketsAction.Request.START.getPreferredName())); @@ -86,13 +97,15 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient } if (restRequest.hasParam(GetBucketsAction.Request.ANOMALY_SCORE.getPreferredName())) { request.setAnomalyScore( - Double.parseDouble(restRequest.param(GetBucketsAction.Request.ANOMALY_SCORE.getPreferredName(), "0.0"))); + Double.parseDouble(restRequest.param(GetBucketsAction.Request.ANOMALY_SCORE.getPreferredName(), "0.0")) + ); } if (restRequest.hasParam(GetBucketsAction.Request.SORT.getPreferredName())) { request.setSort(restRequest.param(GetBucketsAction.Request.SORT.getPreferredName())); } - request.setDescending(restRequest.paramAsBoolean(GetBucketsAction.Request.DESCENDING.getPreferredName(), - request.isDescending())); + request.setDescending( + restRequest.paramAsBoolean(GetBucketsAction.Request.DESCENDING.getPreferredName(), request.isDescending()) + ); // single and multiple bucket options request.setExpand(restRequest.paramAsBoolean(GetBucketsAction.Request.EXPAND.getPreferredName(), false)); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/results/RestGetCategoriesAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/results/RestGetCategoriesAction.java index 427ee825c873b..2e89b0e05f129 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/results/RestGetCategoriesAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/results/RestGetCategoriesAction.java @@ -7,11 +7,11 @@ package org.elasticsearch.xpack.ml.rest.results; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestToXContentListener; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.action.util.PageParams; import org.elasticsearch.xpack.core.ml.action.GetCategoriesAction; import org.elasticsearch.xpack.core.ml.action.GetCategoriesAction.Request; @@ -32,17 +32,25 @@ public class RestGetCategoriesAction extends BaseRestHandler { public List routes() { return List.of( Route.builder(GET, BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/results/categories/{" + CATEGORY_ID + "}") - .replaces(GET, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/results/categories/{" + CATEGORY_ID + "}", - RestApiVersion.V_7).build(), + .replaces( + GET, + PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/results/categories/{" + CATEGORY_ID + "}", + RestApiVersion.V_7 + ) + .build(), Route.builder(POST, BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/results/categories/{" + CATEGORY_ID + "}") - .replaces(POST, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/results/categories/{" + CATEGORY_ID + "}", - RestApiVersion.V_7).build(), + .replaces( + POST, + PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/results/categories/{" + CATEGORY_ID + "}", + RestApiVersion.V_7 + ) + .build(), Route.builder(GET, BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/results/categories") - .replaces(GET, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/results/categories", - RestApiVersion.V_7).build(), + .replaces(GET, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/results/categories", RestApiVersion.V_7) + .build(), Route.builder(POST, BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/results/categories") - .replaces(POST, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/results/categories", - RestApiVersion.V_7).build() + .replaces(POST, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/results/categories", RestApiVersion.V_7) + .build() ); } @@ -55,8 +63,9 @@ public String getName() { protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) throws IOException { Request request; String jobId = restRequest.param(Job.ID.getPreferredName()); - Long categoryId = restRequest.hasParam(CATEGORY_ID.getPreferredName()) ? Long.parseLong( - restRequest.param(CATEGORY_ID.getPreferredName())) : null; + Long categoryId = restRequest.hasParam(CATEGORY_ID.getPreferredName()) + ? Long.parseLong(restRequest.param(CATEGORY_ID.getPreferredName())) + : null; if (restRequest.hasContentOrSourceParam()) { XContentParser parser = restRequest.contentOrSourceParamParser(); @@ -70,13 +79,15 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient request.setCategoryId(categoryId); } if (restRequest.hasParam(Request.FROM.getPreferredName()) - || restRequest.hasParam(Request.SIZE.getPreferredName()) - || categoryId == null){ + || restRequest.hasParam(Request.SIZE.getPreferredName()) + || categoryId == null) { - request.setPageParams(new PageParams( + request.setPageParams( + new PageParams( restRequest.paramAsInt(Request.FROM.getPreferredName(), PageParams.DEFAULT_FROM), restRequest.paramAsInt(Request.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE) - )); + ) + ); } request.setPartitionFieldValue(restRequest.param(Request.PARTITION_FIELD_VALUE.getPreferredName())); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/results/RestGetInfluencersAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/results/RestGetInfluencersAction.java index 096c8be1c1ef1..e42f4b2dd6e7f 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/results/RestGetInfluencersAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/results/RestGetInfluencersAction.java @@ -7,11 +7,11 @@ package org.elasticsearch.xpack.ml.rest.results; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestToXContentListener; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.action.util.PageParams; import org.elasticsearch.xpack.core.ml.action.GetInfluencersAction; import org.elasticsearch.xpack.core.ml.job.config.Job; @@ -30,9 +30,11 @@ public class RestGetInfluencersAction extends BaseRestHandler { public List routes() { return List.of( Route.builder(GET, BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/results/influencers") - .replaces(GET, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/results/influencers", RestApiVersion.V_7).build(), + .replaces(GET, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/results/influencers", RestApiVersion.V_7) + .build(), Route.builder(POST, BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/results/influencers") - .replaces(POST, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/results/influencers", RestApiVersion.V_7).build() + .replaces(POST, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/results/influencers", RestApiVersion.V_7) + .build() ); } @@ -54,16 +56,27 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient request = new GetInfluencersAction.Request(jobId); request.setStart(start); request.setEnd(end); - request.setExcludeInterim(restRequest.paramAsBoolean(GetInfluencersAction.Request.EXCLUDE_INTERIM.getPreferredName(), - request.isExcludeInterim())); - request.setPageParams(new PageParams(restRequest.paramAsInt(PageParams.FROM.getPreferredName(), PageParams.DEFAULT_FROM), - restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE))); + request.setExcludeInterim( + restRequest.paramAsBoolean(GetInfluencersAction.Request.EXCLUDE_INTERIM.getPreferredName(), request.isExcludeInterim()) + ); + request.setPageParams( + new PageParams( + restRequest.paramAsInt(PageParams.FROM.getPreferredName(), PageParams.DEFAULT_FROM), + restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE) + ) + ); request.setInfluencerScore( - Double.parseDouble(restRequest.param(GetInfluencersAction.Request.INFLUENCER_SCORE.getPreferredName(), - String.valueOf(request.getInfluencerScore())))); + Double.parseDouble( + restRequest.param( + GetInfluencersAction.Request.INFLUENCER_SCORE.getPreferredName(), + String.valueOf(request.getInfluencerScore()) + ) + ) + ); request.setSort(restRequest.param(GetInfluencersAction.Request.SORT_FIELD.getPreferredName(), request.getSort())); - request.setDescending(restRequest.paramAsBoolean(GetInfluencersAction.Request.DESCENDING_SORT.getPreferredName(), - request.isDescending())); + request.setDescending( + restRequest.paramAsBoolean(GetInfluencersAction.Request.DESCENDING_SORT.getPreferredName(), request.isDescending()) + ); } return channel -> client.execute(GetInfluencersAction.INSTANCE, request, new RestToXContentListener<>(channel)); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/results/RestGetOverallBucketsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/results/RestGetOverallBucketsAction.java index edd146ef81636..59f41dfa33431 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/results/RestGetOverallBucketsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/results/RestGetOverallBucketsAction.java @@ -8,11 +8,11 @@ import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestToXContentListener; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.GetOverallBucketsAction; import org.elasticsearch.xpack.core.ml.action.GetOverallBucketsAction.Request; import org.elasticsearch.xpack.core.ml.job.config.Job; @@ -31,9 +31,11 @@ public class RestGetOverallBucketsAction extends BaseRestHandler { public List routes() { return List.of( Route.builder(GET, BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/results/overall_buckets") - .replaces(GET, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/results/overall_buckets", RestApiVersion.V_7).build(), + .replaces(GET, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/results/overall_buckets", RestApiVersion.V_7) + .build(), Route.builder(POST, BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/results/overall_buckets") - .replaces(POST, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/results/overall_buckets", RestApiVersion.V_7).build() + .replaces(POST, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/results/overall_buckets", RestApiVersion.V_7) + .build() ); } @@ -65,12 +67,18 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient } if (restRequest.hasParam(Request.ALLOW_NO_JOBS)) { LoggingDeprecationHandler.INSTANCE.logRenamedField( - null, () -> null, Request.ALLOW_NO_JOBS, Request.ALLOW_NO_MATCH.getPreferredName()); + null, + () -> null, + Request.ALLOW_NO_JOBS, + Request.ALLOW_NO_MATCH.getPreferredName() + ); } request.setAllowNoMatch( restRequest.paramAsBoolean( Request.ALLOW_NO_MATCH.getPreferredName(), - restRequest.paramAsBoolean(Request.ALLOW_NO_JOBS, request.allowNoMatch()))); + restRequest.paramAsBoolean(Request.ALLOW_NO_JOBS, request.allowNoMatch()) + ) + ); } return channel -> client.execute(GetOverallBucketsAction.INSTANCE, request, new RestToXContentListener<>(channel)); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/results/RestGetRecordsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/results/RestGetRecordsAction.java index 1fcbd548b1044..001778ea598d2 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/results/RestGetRecordsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/results/RestGetRecordsAction.java @@ -7,11 +7,11 @@ package org.elasticsearch.xpack.ml.rest.results; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestToXContentListener; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.action.util.PageParams; import org.elasticsearch.xpack.core.ml.action.GetRecordsAction; import org.elasticsearch.xpack.core.ml.job.config.Job; @@ -30,9 +30,11 @@ public class RestGetRecordsAction extends BaseRestHandler { public List routes() { return List.of( Route.builder(GET, BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/results/records") - .replaces(GET, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/results/records", RestApiVersion.V_7).build(), + .replaces(GET, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/results/records", RestApiVersion.V_7) + .build(), Route.builder(POST, BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/results/records") - .replaces(POST, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/results/records", RestApiVersion.V_7).build() + .replaces(POST, PRE_V7_BASE_PATH + "anomaly_detectors/{" + Job.ID + "}/results/records", RestApiVersion.V_7) + .build() ); } @@ -48,21 +50,31 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient if (restRequest.hasContentOrSourceParam()) { XContentParser parser = restRequest.contentOrSourceParamParser(); request = GetRecordsAction.Request.parseRequest(jobId, parser); - } - else { + } else { request = new GetRecordsAction.Request(jobId); request.setStart(restRequest.param(GetRecordsAction.Request.START.getPreferredName())); request.setEnd(restRequest.param(GetRecordsAction.Request.END.getPreferredName())); - request.setExcludeInterim(restRequest.paramAsBoolean(GetRecordsAction.Request.EXCLUDE_INTERIM.getPreferredName(), - request.isExcludeInterim())); - request.setPageParams(new PageParams(restRequest.paramAsInt(PageParams.FROM.getPreferredName(), PageParams.DEFAULT_FROM), - restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE))); + request.setExcludeInterim( + restRequest.paramAsBoolean(GetRecordsAction.Request.EXCLUDE_INTERIM.getPreferredName(), request.isExcludeInterim()) + ); + request.setPageParams( + new PageParams( + restRequest.paramAsInt(PageParams.FROM.getPreferredName(), PageParams.DEFAULT_FROM), + restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE) + ) + ); request.setRecordScore( - Double.parseDouble(restRequest.param(GetRecordsAction.Request.RECORD_SCORE_FILTER.getPreferredName(), - String.valueOf(request.getRecordScoreFilter())))); + Double.parseDouble( + restRequest.param( + GetRecordsAction.Request.RECORD_SCORE_FILTER.getPreferredName(), + String.valueOf(request.getRecordScoreFilter()) + ) + ) + ); request.setSort(restRequest.param(GetRecordsAction.Request.SORT.getPreferredName(), request.getSort())); - request.setDescending(restRequest.paramAsBoolean(GetRecordsAction.Request.DESCENDING.getPreferredName(), - request.isDescending())); + request.setDescending( + restRequest.paramAsBoolean(GetRecordsAction.Request.DESCENDING.getPreferredName(), request.isDescending()) + ); } return channel -> client.execute(GetRecordsAction.INSTANCE, request, new RestToXContentListener<>(channel)); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/validate/RestValidateDetectorAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/validate/RestValidateDetectorAction.java index 1094dee740fcd..e3d3780917d8f 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/validate/RestValidateDetectorAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/validate/RestValidateDetectorAction.java @@ -7,11 +7,11 @@ package org.elasticsearch.xpack.ml.rest.validate; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestToXContentListener; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.ValidateDetectorAction; import java.io.IOException; @@ -27,7 +27,8 @@ public class RestValidateDetectorAction extends BaseRestHandler { public List routes() { return List.of( Route.builder(POST, BASE_PATH + "anomaly_detectors/_validate/detector") - .replaces(POST, PRE_V7_BASE_PATH + "anomaly_detectors/_validate/detector", RestApiVersion.V_7).build() + .replaces(POST, PRE_V7_BASE_PATH + "anomaly_detectors/_validate/detector", RestApiVersion.V_7) + .build() ); } @@ -40,8 +41,7 @@ public String getName() { protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) throws IOException { XContentParser parser = restRequest.contentOrSourceParamParser(); ValidateDetectorAction.Request validateDetectorRequest = ValidateDetectorAction.Request.parseRequest(parser); - return channel -> - client.execute(ValidateDetectorAction.INSTANCE, validateDetectorRequest, new RestToXContentListener<>(channel)); + return channel -> client.execute(ValidateDetectorAction.INSTANCE, validateDetectorRequest, new RestToXContentListener<>(channel)); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/validate/RestValidateJobConfigAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/validate/RestValidateJobConfigAction.java index 2d3aada108ef7..b63f27b34f455 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/validate/RestValidateJobConfigAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/validate/RestValidateJobConfigAction.java @@ -7,11 +7,11 @@ package org.elasticsearch.xpack.ml.rest.validate; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestToXContentListener; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.ValidateJobConfigAction; import java.io.IOException; @@ -27,7 +27,8 @@ public class RestValidateJobConfigAction extends BaseRestHandler { public List routes() { return List.of( Route.builder(POST, BASE_PATH + "anomaly_detectors/_validate") - .replaces(POST, PRE_V7_BASE_PATH + "anomaly_detectors/_validate", RestApiVersion.V_7).build() + .replaces(POST, PRE_V7_BASE_PATH + "anomaly_detectors/_validate", RestApiVersion.V_7) + .build() ); } @@ -40,8 +41,7 @@ public String getName() { protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) throws IOException { XContentParser parser = restRequest.contentOrSourceParamParser(); ValidateJobConfigAction.Request validateConfigRequest = ValidateJobConfigAction.Request.parseRequest(parser); - return channel -> - client.execute(ValidateJobConfigAction.INSTANCE, validateConfigRequest, new RestToXContentListener<>(channel)); + return channel -> client.execute(ValidateJobConfigAction.INSTANCE, validateConfigRequest, new RestToXContentListener<>(channel)); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/task/AbstractJobPersistentTasksExecutor.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/task/AbstractJobPersistentTasksExecutor.java index 0dc19db9b6927..edf4ea00233f4 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/task/AbstractJobPersistentTasksExecutor.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/task/AbstractJobPersistentTasksExecutor.java @@ -16,10 +16,10 @@ import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.cache.Cache; import org.elasticsearch.common.cache.CacheBuilder; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.core.Tuple; import org.elasticsearch.persistent.PersistentTaskParams; import org.elasticsearch.persistent.PersistentTasksCustomMetadata; import org.elasticsearch.persistent.PersistentTasksExecutor; @@ -47,13 +47,17 @@ public abstract class AbstractJobPersistentTasksExecutor extends PersistentTasksExecutor { private static final Logger logger = LogManager.getLogger(AbstractJobPersistentTasksExecutor.class); - public static final PersistentTasksCustomMetadata.Assignment AWAITING_MIGRATION = - new PersistentTasksCustomMetadata.Assignment(null, "job cannot be assigned until it has been migrated."); + public static final PersistentTasksCustomMetadata.Assignment AWAITING_MIGRATION = new PersistentTasksCustomMetadata.Assignment( + null, + "job cannot be assigned until it has been migrated." + ); - public static List verifyIndicesPrimaryShardsAreActive(ClusterState clusterState, - IndexNameExpressionResolver expressionResolver, - boolean allowMissing, - String... indicesOfInterest) { + public static List verifyIndicesPrimaryShardsAreActive( + ClusterState clusterState, + IndexNameExpressionResolver expressionResolver, + boolean allowMissing, + String... indicesOfInterest + ) { String[] indices = expressionResolver.concreteIndexNames(clusterState, IndicesOptions.lenientExpandOpen(), indicesOfInterest); List unavailableIndices = new ArrayList<>(indices.length); for (String index : indices) { @@ -73,7 +77,6 @@ public static List verifyIndicesPrimaryShardsAreActive(ClusterState clus return unavailableIndices; } - protected final MlMemoryTracker memoryTracker; protected final IndexNameExpressionResolver expressionResolver; protected final Cache auditedJobCapacity = CacheBuilder.builder() @@ -89,12 +92,14 @@ public static List verifyIndicesPrimaryShardsAreActive(ClusterState clus protected volatile long maxNodeMemory; protected volatile int maxOpenJobs; - protected AbstractJobPersistentTasksExecutor(String taskName, - String executor, - Settings settings, - ClusterService clusterService, - MlMemoryTracker memoryTracker, - IndexNameExpressionResolver expressionResolver) { + protected AbstractJobPersistentTasksExecutor( + String taskName, + String executor, + Settings settings, + ClusterService clusterService, + MlMemoryTracker memoryTracker, + IndexNameExpressionResolver expressionResolver + ) { super(taskName, executor); this.memoryTracker = Objects.requireNonNull(memoryTracker); this.expressionResolver = Objects.requireNonNull(expressionResolver); @@ -118,11 +123,13 @@ protected String getUniqueId(String jobId) { return getTaskName() + "-" + jobId; } - protected void auditRequireMemoryIfNecessary(String jobId, - AbstractAuditor auditor, - PersistentTasksCustomMetadata.Assignment assignment, - JobNodeSelector jobNodeSelector, - boolean isMemoryTrackerRecentlyRefreshed) { + protected void auditRequireMemoryIfNecessary( + String jobId, + AbstractAuditor auditor, + PersistentTasksCustomMetadata.Assignment assignment, + JobNodeSelector jobNodeSelector, + boolean isMemoryTrackerRecentlyRefreshed + ) { if (assignment.equals(AWAITING_LAZY_ASSIGNMENT)) { if (isMemoryTrackerRecentlyRefreshed) { Tuple capacityAndFreeMemory = jobNodeSelector.perceivedCapacityAndMaxFreeMemory( @@ -132,12 +139,16 @@ protected void auditRequireMemoryIfNecessary(String jobId, ); Long previouslyAuditedFreeMemory = auditedJobCapacity.get(getUniqueId(jobId)); if (capacityAndFreeMemory.v2().equals(previouslyAuditedFreeMemory) == false) { - auditor.info(jobId, - Messages.getMessage(JOB_AUDIT_REQUIRES_MORE_MEMORY_TO_RUN, + auditor.info( + jobId, + Messages.getMessage( + JOB_AUDIT_REQUIRES_MORE_MEMORY_TO_RUN, ByteSizeValue.ofBytes(memoryTracker.getJobMemoryRequirement(getTaskName(), jobId)), ByteSizeValue.ofBytes(capacityAndFreeMemory.v2()), ByteSizeValue.ofBytes(capacityAndFreeMemory.v1().getTier()), - ByteSizeValue.ofBytes(capacityAndFreeMemory.v1().getNode()))); + ByteSizeValue.ofBytes(capacityAndFreeMemory.v1().getNode()) + ) + ); auditedJobCapacity.put(getUniqueId(jobId), capacityAndFreeMemory.v2()); } } @@ -147,13 +158,18 @@ protected void auditRequireMemoryIfNecessary(String jobId, } protected abstract String[] indicesOfInterest(Params params); + protected abstract String getJobId(Params params); + protected boolean allowsMissingIndices() { return true; } - public Optional getPotentialAssignment(Params params, ClusterState clusterState, - boolean isMemoryTrackerRecentlyRefreshed) { + public Optional getPotentialAssignment( + Params params, + ClusterState clusterState, + boolean isMemoryTrackerRecentlyRefreshed + ) { // If we are waiting for an upgrade or reset to complete, we should not assign to a node if (MlMetadata.getMlMetadata(clusterState).isUpgradeMode()) { return Optional.of(AWAITING_UPGRADE); @@ -163,9 +179,11 @@ public Optional getPotentialAssignment } String jobId = getJobId(params); - Optional missingIndices = checkRequiredIndices(jobId, + Optional missingIndices = checkRequiredIndices( + jobId, clusterState, - indicesOfInterest(params)); + indicesOfInterest(params) + ); if (missingIndices.isPresent()) { return missingIndices; } @@ -200,16 +218,23 @@ void setMaxNodeSize(ByteSizeValue maxNodeSize) { this.maxNodeMemory = maxNodeSize.getBytes(); } - public Optional checkRequiredIndices(String jobId, - ClusterState clusterState, - String... indicesOfInterest) { - List unavailableIndices = verifyIndicesPrimaryShardsAreActive(clusterState, + public Optional checkRequiredIndices( + String jobId, + ClusterState clusterState, + String... indicesOfInterest + ) { + List unavailableIndices = verifyIndicesPrimaryShardsAreActive( + clusterState, expressionResolver, allowsMissingIndices(), - indicesOfInterest); + indicesOfInterest + ); if (unavailableIndices.size() != 0) { - String reason = "Not opening [" + jobId + "], because not all primary shards are active for the following indices [" + - String.join(",", unavailableIndices) + "]"; + String reason = "Not opening [" + + jobId + + "], because not all primary shards are active for the following indices [" + + String.join(",", unavailableIndices) + + "]"; logger.debug(reason); return Optional.of(new PersistentTasksCustomMetadata.Assignment(null, reason)); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/DomainSplitFunction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/DomainSplitFunction.java index f56a5899e1b33..932c847b4e241 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/DomainSplitFunction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/DomainSplitFunction.java @@ -60,23 +60,35 @@ public final class DomainSplitFunction { entry("pg", "i"), entry("ni", "i"), entry("kawasaki.jp", "i"), - entry("zw", "i")); - - private static final Map excluded = - Map.of( - "city.yokohama.jp", "i", - "teledata.mz", "i", - "city.kobe.jp", "i", - "city.sapporo.jp", "i", - "city.kawasaki.jp", "i", - "city.nagoya.jp", "i", - "www.ck", "i", - "city.sendai.jp", "i", - "city.kitakyushu.jp", "i"); + entry("zw", "i") + ); + + private static final Map excluded = Map.of( + "city.yokohama.jp", + "i", + "teledata.mz", + "i", + "city.kobe.jp", + "i", + "city.sapporo.jp", + "i", + "city.kawasaki.jp", + "i", + "city.nagoya.jp", + "i", + "www.ck", + "i", + "city.sendai.jp", + "i", + "city.kitakyushu.jp", + "i" + ); static { - try (var stream = - DomainSplitFunction.class.getClassLoader().getResourceAsStream("org/elasticsearch/xpack/ml/transforms/exact.properties")) { + try ( + var stream = DomainSplitFunction.class.getClassLoader() + .getResourceAsStream("org/elasticsearch/xpack/ml/transforms/exact.properties") + ) { exact = Streams.readAllLines(stream) .stream() .map(line -> line.split("=")) @@ -124,7 +136,7 @@ private static int findPublicSuffix(List parts) { if (excluded.containsKey(ancestorName)) { return i + 1; } - String [] pieces = ancestorName.split("\\."); + String[] pieces = ancestorName.split("\\."); if (pieces.length >= 2 && under.containsKey(pieces[1])) { return i; } @@ -157,8 +169,11 @@ private static String topPrivateDomain(String name, List parts, int publ public static List domainSplit(String host, Map params) { // NOTE: we don't check SpecialPermission because this will be called (indirectly) from scripts AccessController.doPrivileged((PrivilegedAction) () -> { - deprecationLogger.critical(DeprecationCategory.API, "domainSplit", - "Method [domainSplit] taking params is deprecated. Remove the params argument."); + deprecationLogger.critical( + DeprecationCategory.API, + "domainSplit", + "Method [domainSplit] taking params is deprecated. Remove the params argument." + ); return null; }); return domainSplit(host); @@ -178,7 +193,7 @@ public static List domainSplit(String host) { return Arrays.asList("", host); } boolean tentativeIP = true; - for(int i = 0; i < host.length(); i++) { + for (int i = 0; i < host.length(); i++) { if ((Character.isDigit(host.charAt(i)) || host.charAt(i) == '.') == false) { tentativeIP = false; break; @@ -187,7 +202,7 @@ public static List domainSplit(String host) { if (tentativeIP) { /* special-snowflake rules now... */ if (host.equals(".")) { - return Arrays.asList("",""); + return Arrays.asList("", ""); } return Arrays.asList("", host); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/MlIndicesUtils.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/MlIndicesUtils.java index 27b5cb753731f..6c8fc746753e7 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/MlIndicesUtils.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/MlIndicesUtils.java @@ -13,11 +13,15 @@ */ public final class MlIndicesUtils { - private MlIndicesUtils() { - } + private MlIndicesUtils() {} public static IndicesOptions addIgnoreUnavailable(IndicesOptions indicesOptions) { - return IndicesOptions.fromOptions(true, indicesOptions.allowNoIndices(), indicesOptions.expandWildcardsOpen(), - indicesOptions.expandWildcardsClosed(), indicesOptions); + return IndicesOptions.fromOptions( + true, + indicesOptions.allowNoIndices(), + indicesOptions.expandWildcardsOpen(), + indicesOptions.expandWildcardsClosed(), + indicesOptions + ); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/NamedPipeHelper.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/NamedPipeHelper.java index bf2a1befb15a9..a8a1215625c7d 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/NamedPipeHelper.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/NamedPipeHelper.java @@ -9,8 +9,8 @@ import org.apache.lucene.util.Constants; import org.elasticsearch.ExceptionsHelper; import org.elasticsearch.SpecialPermission; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.core.PathUtils; +import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.env.Environment; import java.io.FileInputStream; @@ -25,7 +25,6 @@ import java.security.PrivilegedAction; import java.time.Duration; - /** * Opens named pipes that are created elsewhere. * @@ -73,10 +72,10 @@ public String getDefaultPipeDirectoryPrefix(Environment env) { if (Constants.WINDOWS) { return WIN_PIPE_PREFIX; } - // Use the Java temporary directory. The Elasticsearch bootstrap sets up the security - // manager to allow this to be read from and written to. Also, the code that spawns our + // Use the Java temporary directory. The Elasticsearch bootstrap sets up the security + // manager to allow this to be read from and written to. Also, the code that spawns our // daemon passes on this location to the C++ code using the $TMPDIR environment variable. - // All these factors need to align for everything to work in production. If any changes + // All these factors need to align for everything to work in production. If any changes // are made here then CNamedPipeFactory::defaultPath() in the C++ code will probably // also need to be changed. return env.tmpFile().toString() + PathUtils.getDefaultFileSystem().getSeparator(); @@ -251,7 +250,7 @@ private OutputStream openNamedPipeOutputStreamUnix(Path file, Duration timeout) } // There's a race condition here in that somebody could delete the named pipe at this point - // causing the line below to create a regular file. Not sure what can be done about this + // causing the line below to create a regular file. Not sure what can be done about this // without using low level OS calls... return Files.newOutputStream(file); @@ -267,7 +266,7 @@ private OutputStream openNamedPipeOutputStreamUnix(Path file, Duration timeout) private void propagatePrivilegedException(RuntimeException e) throws IOException { Throwable ioe = ExceptionsHelper.unwrap(e, IOException.class); if (ioe != null) { - throw (IOException)ioe; + throw (IOException) ioe; } throw e; } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/NativeMemoryCalculator.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/NativeMemoryCalculator.java index e4183e981a5c3..bf6b958f7cb3e 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/NativeMemoryCalculator.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/NativeMemoryCalculator.java @@ -27,14 +27,15 @@ public final class NativeMemoryCalculator { static final long MINIMUM_AUTOMATIC_NODE_SIZE = ByteSizeValue.ofGb(1).getBytes(); private static final long OS_OVERHEAD = ByteSizeValue.ofMb(200L).getBytes(); - private NativeMemoryCalculator() { } + private NativeMemoryCalculator() {} public static OptionalLong allowedBytesForMl(DiscoveryNode node, Settings settings) { return allowedBytesForMl( node.getAttributes().get(MACHINE_MEMORY_NODE_ATTR), node.getAttributes().get(MAX_JVM_SIZE_NODE_ATTR), MAX_MACHINE_MEMORY_PERCENT.get(settings), - USE_AUTO_MACHINE_MEMORY_PERCENT.get(settings)); + USE_AUTO_MACHINE_MEMORY_PERCENT.get(settings) + ); } public static OptionalLong allowedBytesForMl(DiscoveryNode node, ClusterSettings settings) { @@ -42,7 +43,8 @@ public static OptionalLong allowedBytesForMl(DiscoveryNode node, ClusterSettings node.getAttributes().get(MACHINE_MEMORY_NODE_ATTR), node.getAttributes().get(MAX_JVM_SIZE_NODE_ATTR), settings.get(MAX_MACHINE_MEMORY_PERCENT), - settings.get(USE_AUTO_MACHINE_MEMORY_PERCENT)); + settings.get(USE_AUTO_MACHINE_MEMORY_PERCENT) + ); } public static OptionalLong allowedBytesForMl(DiscoveryNode node, int maxMemoryPercent, boolean useAutoPercent) { @@ -50,7 +52,8 @@ public static OptionalLong allowedBytesForMl(DiscoveryNode node, int maxMemoryPe node.getAttributes().get(MACHINE_MEMORY_NODE_ATTR), node.getAttributes().get(MAX_JVM_SIZE_NODE_ATTR), maxMemoryPercent, - useAutoPercent); + useAutoPercent + ); } private static OptionalLong allowedBytesForMl(String nodeBytes, String jvmBytes, int maxMemoryPercent, boolean useAuto) { @@ -82,25 +85,26 @@ public static long calculateApproxNecessaryNodeSize(long nativeMachineMemory, Lo // TODO utilize official ergonomic JVM size calculations when available. jvmSize = jvmSize == null ? dynamicallyCalculateJvmSizeFromNativeMemorySize(nativeMachineMemory) : jvmSize; // We haven't reached our 90% threshold, so, simply summing up the values is adequate - if ((jvmSize + OS_OVERHEAD)/(double)nativeMachineMemory > 0.1) { + if ((jvmSize + OS_OVERHEAD) / (double) nativeMachineMemory > 0.1) { return Math.max(nativeMachineMemory + jvmSize + OS_OVERHEAD, MINIMUM_AUTOMATIC_NODE_SIZE); } - return Math.round((nativeMachineMemory/0.9)); + return Math.round((nativeMachineMemory / 0.9)); } - return (long) ((100.0/maxMemoryPercent) * nativeMachineMemory); + return (long) ((100.0 / maxMemoryPercent) * nativeMachineMemory); } public static double modelMemoryPercent(long machineMemory, Long jvmSize, int maxMemoryPercent, boolean useAuto) { if (useAuto) { jvmSize = jvmSize == null ? dynamicallyCalculateJvmSizeFromNodeSize(machineMemory) : jvmSize; if (machineMemory - jvmSize < OS_OVERHEAD || machineMemory == 0) { - assert false: String.format( - Locale.ROOT, - "machine memory [%d] minus jvm [%d] is less than overhead [%d]", - machineMemory, - jvmSize, - OS_OVERHEAD - ); + assert false + : String.format( + Locale.ROOT, + "machine memory [%d] minus jvm [%d] is less than overhead [%d]", + machineMemory, + jvmSize, + OS_OVERHEAD + ); return maxMemoryPercent; } // This calculation is dynamic and designed to maximally take advantage of the underlying machine for machine learning @@ -110,7 +114,7 @@ public static double modelMemoryPercent(long machineMemory, Long jvmSize, int ma // 2GB node -> 66% // 16GB node -> 87% // 64GB node -> 90% - return Math.min(90.0, ((machineMemory - jvmSize - OS_OVERHEAD) / (double)machineMemory) * 100.0D); + return Math.min(90.0, ((machineMemory - jvmSize - OS_OVERHEAD) / (double) machineMemory) * 100.0D); } return maxMemoryPercent; } @@ -129,18 +133,20 @@ static long allowedBytesForMl(long machineMemory, Long jvmSize, int maxMemoryPer // 2GB node -> 66% // 16GB node -> 87% // 64GB node -> 90% - double memoryProportion = Math.min(0.90, (machineMemory - jvmSize - OS_OVERHEAD) / (double)machineMemory); + double memoryProportion = Math.min(0.90, (machineMemory - jvmSize - OS_OVERHEAD) / (double) machineMemory); return Math.round(machineMemory * memoryProportion); } - return (long)(machineMemory * (maxMemoryPercent / 100.0)); + return (long) (machineMemory * (maxMemoryPercent / 100.0)); } public static long allowedBytesForMl(long machineMemory, int maxMemoryPercent, boolean useAuto) { - return allowedBytesForMl(machineMemory, + return allowedBytesForMl( + machineMemory, useAuto ? dynamicallyCalculateJvmSizeFromNodeSize(machineMemory) : machineMemory / 2, maxMemoryPercent, - useAuto); + useAuto + ); } // TODO replace with official ergonomic calculation @@ -149,10 +155,10 @@ public static long dynamicallyCalculateJvmSizeFromNodeSize(long nodeSize) { // 2GB and 8GB cause weird issues where the JVM size will "jump the gap" from one to the other when // considering true tier sizes in elastic cloud. if (nodeSize < ByteSizeValue.ofMb(1280).getBytes()) { - return (long)(nodeSize * 0.40); + return (long) (nodeSize * 0.40); } if (nodeSize < ByteSizeValue.ofGb(8).getBytes()) { - return (long)(nodeSize * 0.25); + return (long) (nodeSize * 0.25); } return STATIC_JVM_UPPER_THRESHOLD; } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/QueryBuilderHelper.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/QueryBuilderHelper.java index e26322250101c..3e780b874dbf3 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/QueryBuilderHelper.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/QueryBuilderHelper.java @@ -20,7 +20,7 @@ public final class QueryBuilderHelper { - private QueryBuilderHelper() { } + private QueryBuilderHelper() {} /** * Helper function for adding OR type queries for a given identity field. diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/TypedChainTaskExecutor.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/TypedChainTaskExecutor.java index dbe853997ba96..59c45f8b7843d 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/TypedChainTaskExecutor.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/TypedChainTaskExecutor.java @@ -23,7 +23,7 @@ */ public class TypedChainTaskExecutor { - public interface ChainTask { + public interface ChainTask { void run(ActionListener listener); } @@ -47,9 +47,11 @@ public interface ChainTask { * {@code true} means that no more tasks should execute and the listener::onFailure should be * called. */ - public TypedChainTaskExecutor(ExecutorService executorService, - Predicate continuationPredicate, - Predicate failureShortCircuitPredicate) { + public TypedChainTaskExecutor( + ExecutorService executorService, + Predicate continuationPredicate, + Predicate failureShortCircuitPredicate + ) { this.executorService = Objects.requireNonNull(executorService); this.continuationPredicate = continuationPredicate; this.failureShortCircuitPredicate = failureShortCircuitPredicate; @@ -66,7 +68,7 @@ private synchronized void execute(T previousValue, ActionListener> liste if (tasks.isEmpty()) { // noinspection Java9CollectionFactory (because the list can contain null entries) listener.onResponse(Collections.unmodifiableList(new ArrayList<>(collectedResponses))); - return; + return; } ChainTask task = tasks.pop(); executorService.execute(new AbstractRunnable() { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/VoidChainTaskExecutor.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/VoidChainTaskExecutor.java index 54be047d0a43d..d5d7767a7e7a1 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/VoidChainTaskExecutor.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/VoidChainTaskExecutor.java @@ -19,9 +19,11 @@ public VoidChainTaskExecutor(ExecutorService executorService, boolean shortCircu this(executorService, (a) -> true, (e) -> shortCircuit); } - VoidChainTaskExecutor(ExecutorService executorService, - Predicate continuationPredicate, - Predicate failureShortCircuitPredicate) { + VoidChainTaskExecutor( + ExecutorService executorService, + Predicate continuationPredicate, + Predicate failureShortCircuitPredicate + ) { super(executorService, continuationPredicate, failureShortCircuitPredicate); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/persistence/BatchedDocumentsIterator.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/persistence/BatchedDocumentsIterator.java index 8b974e518daa0..04ba368f40839 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/persistence/BatchedDocumentsIterator.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/persistence/BatchedDocumentsIterator.java @@ -29,7 +29,7 @@ * An iterator useful to fetch a big number of documents of type T * and iterate through them in batches. */ -public abstract class BatchedDocumentsIterator implements BatchedIterator { +public abstract class BatchedDocumentsIterator implements BatchedIterator { private static final Logger LOGGER = LogManager.getLogger(BatchedDocumentsIterator.class); private static final String CONTEXT_ALIVE_DURATION = "5m"; @@ -97,12 +97,13 @@ private SearchResponse initScroll() { SearchRequest searchRequest = new SearchRequest(index); searchRequest.indicesOptions(MlIndicesUtils.addIgnoreUnavailable(SearchRequest.DEFAULT_INDICES_OPTIONS)); searchRequest.scroll(CONTEXT_ALIVE_DURATION); - searchRequest.source(new SearchSourceBuilder() - .size(BATCH_SIZE) + searchRequest.source( + new SearchSourceBuilder().size(BATCH_SIZE) .query(getQuery()) .fetchSource(shouldFetchSource()) .trackTotalHits(true) - .sort(SortBuilders.fieldSort(ElasticsearchMappings.ES_DOC))); + .sort(SortBuilders.fieldSort(ElasticsearchMappings.ES_DOC)) + ); SearchResponse searchResponse = client.search(searchRequest).actionGet(); totalHits = searchResponse.getHits().getTotalHits().value; diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/persistence/LimitAwareBulkIndexer.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/persistence/LimitAwareBulkIndexer.java index b008038b3a317..b69fc5944021c 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/persistence/LimitAwareBulkIndexer.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/persistence/LimitAwareBulkIndexer.java @@ -52,8 +52,12 @@ public void addAndExecuteIfNeeded(IndexRequest indexRequest) { private void execute() { if (currentBulkRequest.numberOfActions() > 0) { - LOGGER.debug("Executing bulk request; current bytes [{}]; bytes limit [{}]; number of actions [{}]", - currentRamBytes, bytesLimit, currentBulkRequest.numberOfActions()); + LOGGER.debug( + "Executing bulk request; current bytes [{}]; bytes limit [{}]; number of actions [{}]", + currentRamBytes, + bytesLimit, + currentBulkRequest.numberOfActions() + ); executor.accept(currentBulkRequest); currentBulkRequest = new BulkRequest(); currentRamBytes = 0; diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/persistence/MlParserUtils.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/persistence/MlParserUtils.java index ce7b420695a28..8661497593815 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/persistence/MlParserUtils.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/persistence/MlParserUtils.java @@ -10,11 +10,11 @@ import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.search.SearchHit; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.search.SearchHit; import java.io.IOException; import java.io.InputStream; @@ -32,9 +32,11 @@ private MlParserUtils() {} */ public static T parse(SearchHit hit, BiFunction objectParser) { BytesReference source = hit.getSourceRef(); - try (InputStream stream = source.streamInput(); - XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) { + try ( + InputStream stream = source.streamInput(); + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream) + ) { return objectParser.apply(parser, null); } catch (IOException e) { throw new ElasticsearchParseException("failed to parse " + hit.getId(), e); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/persistence/ResultsPersisterService.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/persistence/ResultsPersisterService.java index af81e108133ec..a78d62a7a3fa3 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/persistence/ResultsPersisterService.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/persistence/ResultsPersisterService.java @@ -28,14 +28,14 @@ import org.elasticsearch.common.component.LifecycleListener; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.CancellableThreads; import org.elasticsearch.common.util.concurrent.ConcurrentCollections; +import org.elasticsearch.core.TimeValue; +import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.rest.RestStatus; -import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.xpack.core.ClientHelper; import org.elasticsearch.xpack.core.ml.MlMetadata; @@ -58,19 +58,21 @@ public class ResultsPersisterService { /** * List of rest statuses that we consider irrecoverable */ - public static final Set IRRECOVERABLE_REST_STATUSES = Collections.unmodifiableSet(new HashSet<>( - Arrays.asList( - RestStatus.GONE, - RestStatus.NOT_IMPLEMENTED, - // Not found is returned when we require an alias but the index is NOT an alias. - RestStatus.NOT_FOUND, - RestStatus.BAD_REQUEST, - RestStatus.UNAUTHORIZED, - RestStatus.FORBIDDEN, - RestStatus.METHOD_NOT_ALLOWED, - RestStatus.NOT_ACCEPTABLE + public static final Set IRRECOVERABLE_REST_STATUSES = Collections.unmodifiableSet( + new HashSet<>( + Arrays.asList( + RestStatus.GONE, + RestStatus.NOT_IMPLEMENTED, + // Not found is returned when we require an alias but the index is NOT an alias. + RestStatus.NOT_FOUND, + RestStatus.BAD_REQUEST, + RestStatus.UNAUTHORIZED, + RestStatus.FORBIDDEN, + RestStatus.METHOD_NOT_ALLOWED, + RestStatus.NOT_ACCEPTABLE + ) ) - )); + ); private static final Logger LOGGER = LogManager.getLogger(ResultsPersisterService.class); @@ -80,8 +82,9 @@ public class ResultsPersisterService { 0, 50, Setting.Property.OperatorDynamic, - Setting.Property.NodeScope); - private static final int MAX_RETRY_SLEEP_MILLIS = (int)Duration.ofMinutes(15).toMillis(); + Setting.Property.NodeScope + ); + private static final int MAX_RETRY_SLEEP_MILLIS = (int) Duration.ofMinutes(15).toMillis(); private static final int MIN_RETRY_SLEEP_MILLIS = 50; // Having an exponent higher than this causes integer overflow private static final int MAX_RETRY_EXPONENT = 24; @@ -95,15 +98,11 @@ public class ResultsPersisterService { private volatile boolean isResetMode = false; // Visible for testing - public ResultsPersisterService(ThreadPool threadPool, - OriginSettingClient client, - ClusterService clusterService, - Settings settings) { + public ResultsPersisterService(ThreadPool threadPool, OriginSettingClient client, ClusterService clusterService, Settings settings) { this.threadPool = threadPool; this.client = client; this.maxFailureRetries = PERSIST_RESULTS_MAX_RETRIES.get(settings); - clusterService.getClusterSettings() - .addSettingsUpdateConsumer(PERSIST_RESULTS_MAX_RETRIES, this::setMaxFailureRetries); + clusterService.getClusterSettings().addSettingsUpdateConsumer(PERSIST_RESULTS_MAX_RETRIES, this::setMaxFailureRetries); clusterService.addLifecycleListener(new LifecycleListener() { @Override public void beforeStop() { @@ -144,15 +143,17 @@ void setMaxFailureRetries(int value) { this.maxFailureRetries = value; } - public BulkResponse indexWithRetry(String jobId, - String indexName, - ToXContent object, - ToXContent.Params params, - WriteRequest.RefreshPolicy refreshPolicy, - String id, - boolean requireAlias, - Supplier shouldRetry, - Consumer retryMsgHandler) throws IOException { + public BulkResponse indexWithRetry( + String jobId, + String indexName, + ToXContent object, + ToXContent.Params params, + WriteRequest.RefreshPolicy refreshPolicy, + String id, + boolean requireAlias, + Supplier shouldRetry, + Consumer retryMsgHandler + ) throws IOException { BulkRequest bulkRequest = new BulkRequest().setRefreshPolicy(refreshPolicy); try (XContentBuilder content = object.toXContent(XContentFactory.jsonBuilder(), params)) { bulkRequest.add(new IndexRequest(indexName).id(id).source(content).setRequireAlias(requireAlias)); @@ -160,23 +161,24 @@ public BulkResponse indexWithRetry(String jobId, return bulkIndexWithRetry(bulkRequest, jobId, shouldRetry, retryMsgHandler); } - public BulkResponse bulkIndexWithRetry(BulkRequest bulkRequest, - String jobId, - Supplier shouldRetry, - Consumer retryMsgHandler) { - return bulkIndexWithRetry(bulkRequest, - jobId, - shouldRetry, - retryMsgHandler, - client::bulk); + public BulkResponse bulkIndexWithRetry( + BulkRequest bulkRequest, + String jobId, + Supplier shouldRetry, + Consumer retryMsgHandler + ) { + return bulkIndexWithRetry(bulkRequest, jobId, shouldRetry, retryMsgHandler, client::bulk); } - public BulkResponse bulkIndexWithHeadersWithRetry(Map headers, - BulkRequest bulkRequest, - String jobId, - Supplier shouldRetry, - Consumer retryMsgHandler) { - return bulkIndexWithRetry(bulkRequest, + public BulkResponse bulkIndexWithHeadersWithRetry( + Map headers, + BulkRequest bulkRequest, + String jobId, + Supplier shouldRetry, + Consumer retryMsgHandler + ) { + return bulkIndexWithRetry( + bulkRequest, jobId, shouldRetry, retryMsgHandler, @@ -186,14 +188,18 @@ public BulkResponse bulkIndexWithHeadersWithRetry(Map headers, client, BulkAction.INSTANCE, providedBulkRequest, - listener)); + listener + ) + ); } - private BulkResponse bulkIndexWithRetry(BulkRequest bulkRequest, - String jobId, - Supplier shouldRetry, - Consumer retryMsgHandler, - BiConsumer> actionExecutor) { + private BulkResponse bulkIndexWithRetry( + BulkRequest bulkRequest, + String jobId, + Supplier shouldRetry, + Consumer retryMsgHandler, + BiConsumer> actionExecutor + ) { if (isShutdown || isResetMode) { throw new ElasticsearchException( "Bulk indexing has failed as {}", @@ -217,17 +223,21 @@ private BulkResponse bulkIndexWithRetry(BulkRequest bulkRequest, onGoingRetryableBulkActions.put(key, bulkRetryableAction); bulkRetryableAction.run(); if (isShutdown || isResetMode) { - bulkRetryableAction.cancel(new CancellableThreads.ExecutionCancelledException( - isShutdown ? "Node is shutting down" : "Machine learning feature is being reset" - )); + bulkRetryableAction.cancel( + new CancellableThreads.ExecutionCancelledException( + isShutdown ? "Node is shutting down" : "Machine learning feature is being reset" + ) + ); } return getResponse.actionGet(); } - public SearchResponse searchWithRetry(SearchRequest searchRequest, - String jobId, - Supplier shouldRetry, - Consumer retryMsgHandler) { + public SearchResponse searchWithRetry( + SearchRequest searchRequest, + String jobId, + Supplier shouldRetry, + Consumer retryMsgHandler + ) { final PlainActionFuture getResponse = PlainActionFuture.newFuture(); final Object key = new Object(); final ActionListener removeListener = ActionListener.runBefore( @@ -240,7 +250,8 @@ public SearchResponse searchWithRetry(SearchRequest searchRequest, client, () -> (isShutdown == false) && shouldRetry.get(), retryMsgHandler, - removeListener); + removeListener + ); onGoingRetryableSearchActions.put(key, mlRetryableAction); mlRetryableAction.run(); if (isShutdown) { @@ -249,12 +260,14 @@ public SearchResponse searchWithRetry(SearchRequest searchRequest, return getResponse.actionGet(); } - static class RecoverableException extends Exception { } + static class RecoverableException extends Exception {} + static class IrrecoverableException extends ElasticsearchStatusException { IrrecoverableException(String msg, RestStatus status, Throwable cause, Object... args) { super(msg, status, cause, args); } } + /** * @param ex The exception to check * @return true when the failure will persist no matter how many times we retry. @@ -267,6 +280,7 @@ private static boolean isIrrecoverable(Exception ex) { @SuppressWarnings("NonAtomicOperationOnVolatileField") private static class BulkRequestRewriter { private volatile BulkRequest bulkRequest; + BulkRequestRewriter(BulkRequest initialRequest) { this.bulkRequest = initialRequest; } @@ -286,44 +300,52 @@ BulkRequest getBulkRequest() { private class BulkRetryableAction extends MlRetryableAction { private final BulkRequestRewriter bulkRequestRewriter; - BulkRetryableAction(String jobId, - BulkRequestRewriter bulkRequestRewriter, - Supplier shouldRetry, - Consumer msgHandler, - BiConsumer> actionExecutor, - ActionListener listener) { - super(jobId, + + BulkRetryableAction( + String jobId, + BulkRequestRewriter bulkRequestRewriter, + Supplier shouldRetry, + Consumer msgHandler, + BiConsumer> actionExecutor, + ActionListener listener + ) { + super( + jobId, shouldRetry, msgHandler, - (request, retryableListener) -> actionExecutor.accept(request, ActionListener.wrap( - bulkResponse -> { - if (bulkResponse.hasFailures() == false) { - retryableListener.onResponse(bulkResponse); - return; - } - for (BulkItemResponse itemResponse : bulkResponse.getItems()) { - if (itemResponse.isFailed() && isIrrecoverable(itemResponse.getFailure().getCause())) { - Throwable unwrappedParticular = ExceptionsHelper.unwrapCause(itemResponse.getFailure().getCause()); - LOGGER.warn(new ParameterizedMessage( - "[{}] experienced failure that cannot be automatically retried. Bulk failure message [{}]", - jobId, - bulkResponse.buildFailureMessage()), - unwrappedParticular); - retryableListener.onFailure(new IrrecoverableException( + (request, retryableListener) -> actionExecutor.accept(request, ActionListener.wrap(bulkResponse -> { + if (bulkResponse.hasFailures() == false) { + retryableListener.onResponse(bulkResponse); + return; + } + for (BulkItemResponse itemResponse : bulkResponse.getItems()) { + if (itemResponse.isFailed() && isIrrecoverable(itemResponse.getFailure().getCause())) { + Throwable unwrappedParticular = ExceptionsHelper.unwrapCause(itemResponse.getFailure().getCause()); + LOGGER.warn( + new ParameterizedMessage( + "[{}] experienced failure that cannot be automatically retried. Bulk failure message [{}]", + jobId, + bulkResponse.buildFailureMessage() + ), + unwrappedParticular + ); + retryableListener.onFailure( + new IrrecoverableException( "{} experienced failure that cannot be automatically retried. See logs for bulk failures", status(unwrappedParticular), unwrappedParticular, - jobId)); - return; - } + jobId + ) + ); + return; } - bulkRequestRewriter.rewriteRequest(bulkResponse); - // Let the listener attempt again with the new bulk request - retryableListener.onFailure(new RecoverableException()); - }, - retryableListener::onFailure - )), - listener); + } + bulkRequestRewriter.rewriteRequest(bulkResponse); + // Let the listener attempt again with the new bulk request + retryableListener.onFailure(new RecoverableException()); + }, retryableListener::onFailure)), + listener + ); this.bulkRequestRewriter = bulkRequestRewriter; } @@ -342,32 +364,31 @@ public String getName() { private class SearchRetryableAction extends MlRetryableAction { private final SearchRequest searchRequest; - SearchRetryableAction(String jobId, - SearchRequest searchRequest, - // Pass the client to work around https://bugs.eclipse.org/bugs/show_bug.cgi?id=569557 - OriginSettingClient client, - Supplier shouldRetry, - Consumer msgHandler, - ActionListener listener) { - super(jobId, + + SearchRetryableAction( + String jobId, + SearchRequest searchRequest, + // Pass the client to work around https://bugs.eclipse.org/bugs/show_bug.cgi?id=569557 + OriginSettingClient client, + Supplier shouldRetry, + Consumer msgHandler, + ActionListener listener + ) { + super( + jobId, shouldRetry, msgHandler, - (request, retryableListener) -> client.search(request, ActionListener.wrap( - searchResponse -> { - if (RestStatus.OK.equals(searchResponse.status())) { - retryableListener.onResponse(searchResponse); - return; - } - retryableListener.onFailure( - new ElasticsearchStatusException( - "search failed with status {}", - searchResponse.status(), - searchResponse.status()) - ); - }, - retryableListener::onFailure - )), - listener); + (request, retryableListener) -> client.search(request, ActionListener.wrap(searchResponse -> { + if (RestStatus.OK.equals(searchResponse.status())) { + retryableListener.onResponse(searchResponse); + return; + } + retryableListener.onFailure( + new ElasticsearchStatusException("search failed with status {}", searchResponse.status(), searchResponse.status()) + ); + }, retryableListener::onFailure)), + listener + ); this.searchRequest = searchRequest; } @@ -392,18 +413,21 @@ private abstract class MlRetryableAction extends RetryableAct volatile int currentAttempt = 0; volatile long currentMax = MIN_RETRY_SLEEP_MILLIS; - MlRetryableAction(String jobId, - Supplier shouldRetry, - Consumer msgHandler, - BiConsumer> action, - ActionListener listener) { + MlRetryableAction( + String jobId, + Supplier shouldRetry, + Consumer msgHandler, + BiConsumer> action, + ActionListener listener + ) { super( LOGGER, threadPool, TimeValue.timeValueMillis(MIN_RETRY_SLEEP_MILLIS), TimeValue.MAX_VALUE, listener, - UTILITY_THREAD_POOL_NAME); + UTILITY_THREAD_POOL_NAME + ); this.jobId = jobId; this.shouldRetry = shouldRetry; this.msgHandler = msgHandler; @@ -429,23 +453,16 @@ public boolean shouldRetry(Exception e) { // If the outside conditions have changed and retries are no longer needed, do not retry. if (shouldRetry.get() == false) { - LOGGER.info(() -> new ParameterizedMessage( - "[{}] should not retry {} after [{}] attempts", - jobId, - getName(), - currentAttempt - ), e); + LOGGER.info( + () -> new ParameterizedMessage("[{}] should not retry {} after [{}] attempts", jobId, getName(), currentAttempt), + e + ); return false; } // If the configured maximum number of retries has been reached, do not retry. if (currentAttempt > maxFailureRetries) { - LOGGER.warn(() -> new ParameterizedMessage( - "[{}] failed to {} after [{}] attempts.", - jobId, - getName(), - currentAttempt - ), e); + LOGGER.warn(() -> new ParameterizedMessage("[{}] failed to {} after [{}] attempts.", jobId, getName(), currentAttempt), e); return false; } return true; @@ -456,10 +473,7 @@ protected long calculateDelayBound(long previousDelayBound) { // Exponential backoff calculation taken from: https://en.wikipedia.org/wiki/Exponential_backoff int uncappedBackoff = ((1 << Math.min(currentAttempt, MAX_RETRY_EXPONENT)) - 1) * (50); currentMax = Math.min(uncappedBackoff, MAX_RETRY_SLEEP_MILLIS); - String msg = new ParameterizedMessage( - "failed to {} after [{}] attempts. Will attempt again.", - getName(), - currentAttempt) + String msg = new ParameterizedMessage("failed to {} after [{}] attempts. Will attempt again.", getName(), currentAttempt) .getFormattedMessage(); LOGGER.warn(() -> new ParameterizedMessage("[{}] {}", jobId, msg)); msgHandler.accept(msg); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/persistence/SearchAfterDocumentsIterator.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/persistence/SearchAfterDocumentsIterator.java index 9dd8860d4bc23..41131dcb7c40e 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/persistence/SearchAfterDocumentsIterator.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/utils/persistence/SearchAfterDocumentsIterator.java @@ -114,11 +114,10 @@ public Deque next() { return mapHits(searchResponse); } - private SearchResponse doSearch(Object [] searchAfterValues) { + private SearchResponse doSearch(Object[] searchAfterValues) { SearchRequest searchRequest = new SearchRequest(index); searchRequest.indicesOptions(MlIndicesUtils.addIgnoreUnavailable(SearchRequest.DEFAULT_INDICES_OPTIONS)); - SearchSourceBuilder sourceBuilder = (new SearchSourceBuilder() - .size(batchSize) + SearchSourceBuilder sourceBuilder = (new SearchSourceBuilder().size(batchSize) .query(getQuery()) .fetchSource(shouldFetchSource()) .sort(sortField())); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/LocalStateMachineLearning.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/LocalStateMachineLearning.java index 2cf44ef00f50f..6952f1e0f50b0 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/LocalStateMachineLearning.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/LocalStateMachineLearning.java @@ -40,10 +40,11 @@ public class LocalStateMachineLearning extends LocalStateCompositeXPackPlugin { private final MachineLearning mlPlugin; + public LocalStateMachineLearning(final Settings settings, final Path configPath) { super(settings, configPath); LocalStateMachineLearning thisVar = this; - mlPlugin = new MachineLearning(settings){ + mlPlugin = new MachineLearning(settings) { @Override protected XPackLicenseState getLicenseState() { return thisVar.getLicenseState(); @@ -69,10 +70,14 @@ protected XPackLicenseState getLicenseState() { }); plugins.add(new Security(settings, configPath) { @Override - protected SSLService getSslService() { return thisVar.getSslService(); } + protected SSLService getSslService() { + return thisVar.getSslService(); + } @Override - protected XPackLicenseState getLicenseState() { return thisVar.getLicenseState(); } + protected XPackLicenseState getLicenseState() { + return thisVar.getLicenseState(); + } }); plugins.add(new MockedRollupPlugin()); } @@ -102,13 +107,12 @@ public static class MockedRollupPlugin extends Plugin implements ActionPlugin { @Override public List> getActions() { - return Collections.singletonList( - new ActionHandler<>(GetRollupIndexCapsAction.INSTANCE, MockedRollupIndexCapsTransport.class) - ); + return Collections.singletonList(new ActionHandler<>(GetRollupIndexCapsAction.INSTANCE, MockedRollupIndexCapsTransport.class)); } - public static class MockedRollupIndexCapsTransport - extends TransportAction { + public static class MockedRollupIndexCapsTransport extends TransportAction< + GetRollupIndexCapsAction.Request, + GetRollupIndexCapsAction.Response> { @Inject public MockedRollupIndexCapsTransport(TransportService transportService) { @@ -116,9 +120,11 @@ public MockedRollupIndexCapsTransport(TransportService transportService) { } @Override - protected void doExecute(Task task, - GetRollupIndexCapsAction.Request request, - ActionListener listener) { + protected void doExecute( + Task task, + GetRollupIndexCapsAction.Request request, + ActionListener listener + ) { listener.onResponse(new GetRollupIndexCapsAction.Response()); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MachineLearningInfoTransportActionTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MachineLearningInfoTransportActionTests.java index e06a6df5eba7b..0972ce51e6b0c 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MachineLearningInfoTransportActionTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MachineLearningInfoTransportActionTests.java @@ -26,15 +26,15 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.TransportAddress; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.env.Environment; import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.ingest.IngestStats; import org.elasticsearch.license.MockLicenseState; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xpack.core.XPackFeatureSet; import org.elasticsearch.xpack.core.XPackField; import org.elasticsearch.xpack.core.action.XPackUsageFeatureResponse; @@ -105,9 +105,9 @@ public class MachineLearningInfoTransportActionTests extends ESTestCase { @Before public void init() { commonSettings = Settings.builder() - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toAbsolutePath()) - .put(MachineLearningField.AUTODETECT_PROCESS.getKey(), false) - .build(); + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toAbsolutePath()) + .put(MachineLearningField.AUTODETECT_PROCESS.getKey(), false) + .build(); clusterService = mock(ClusterService.class); client = mock(Client.class); jobManager = mock(JobManager.class); @@ -123,14 +123,26 @@ public void init() { } private MachineLearningUsageTransportAction newUsageAction(Settings settings) { - return new MachineLearningUsageTransportAction(mock(TransportService.class), clusterService, - null, mock(ActionFilters.class), mock(IndexNameExpressionResolver.class), - TestEnvironment.newEnvironment(settings), client, licenseState, jobManagerHolder); + return new MachineLearningUsageTransportAction( + mock(TransportService.class), + clusterService, + null, + mock(ActionFilters.class), + mock(IndexNameExpressionResolver.class), + TestEnvironment.newEnvironment(settings), + client, + licenseState, + jobManagerHolder + ); } public void testAvailable() throws Exception { MachineLearningInfoTransportAction featureSet = new MachineLearningInfoTransportAction( - mock(TransportService.class), mock(ActionFilters.class), commonSettings, licenseState); + mock(TransportService.class), + mock(ActionFilters.class), + commonSettings, + licenseState + ); boolean available = randomBoolean(); when(licenseState.isAllowed(MachineLearningField.ML_API_FEATURE)).thenReturn(available); assertThat(featureSet.available(), is(available)); @@ -156,7 +168,11 @@ public void testEnabled() throws Exception { } boolean expected = enabled || useDefault; MachineLearningInfoTransportAction featureSet = new MachineLearningInfoTransportAction( - mock(TransportService.class), mock(ActionFilters.class), settings.build(), licenseState); + mock(TransportService.class), + mock(ActionFilters.class), + settings.build(), + licenseState + ); assertThat(featureSet.enabled(), is(expected)); var usageAction = newUsageAction(settings.build()); PlainActionFuture future = new PlainActionFuture<>(); @@ -175,32 +191,39 @@ public void testUsage() throws Exception { Settings.Builder settings = Settings.builder().put(commonSettings); settings.put("xpack.ml.enabled", true); - Job opened1 = buildJob("opened1", Collections.singletonList(buildMinDetector("foo")), - Collections.singletonMap("created_by", randomFrom("a-cool-module", "a_cool_module", "a cool module"))); + Job opened1 = buildJob( + "opened1", + Collections.singletonList(buildMinDetector("foo")), + Collections.singletonMap("created_by", randomFrom("a-cool-module", "a_cool_module", "a cool module")) + ); GetJobsStatsAction.Response.JobStats opened1JobStats = buildJobStats("opened1", JobState.OPENED, 100L, 3L); Job opened2 = buildJob("opened2", Arrays.asList(buildMinDetector("foo"), buildMinDetector("bar"))); GetJobsStatsAction.Response.JobStats opened2JobStats = buildJobStats("opened2", JobState.OPENED, 200L, 8L); Job closed1 = buildJob("closed1", Arrays.asList(buildMinDetector("foo"), buildMinDetector("bar"), buildMinDetector("foobar"))); GetJobsStatsAction.Response.JobStats closed1JobStats = buildJobStats("closed1", JobState.CLOSED, 300L, 0); - givenJobs(Arrays.asList(opened1, opened2, closed1), - Arrays.asList(opened1JobStats, opened2JobStats, closed1JobStats)); + givenJobs(Arrays.asList(opened1, opened2, closed1), Arrays.asList(opened1JobStats, opened2JobStats, closed1JobStats)); - givenDatafeeds(Arrays.asList( + givenDatafeeds( + Arrays.asList( buildDatafeedStats(DatafeedState.STARTED), buildDatafeedStats(DatafeedState.STARTED), buildDatafeedStats(DatafeedState.STOPPED) - )); + ) + ); DataFrameAnalyticsConfig dfa1 = DataFrameAnalyticsConfigTests.createRandom("dfa_1"); DataFrameAnalyticsConfig dfa2 = DataFrameAnalyticsConfigTests.createRandom("dfa_2"); DataFrameAnalyticsConfig dfa3 = DataFrameAnalyticsConfigTests.createRandom("dfa_3"); List dataFrameAnalytics = Arrays.asList(dfa1, dfa2, dfa3); - givenDataFrameAnalytics(dataFrameAnalytics, Arrays.asList( - buildDataFrameAnalyticsStats(dfa1.getId(), DataFrameAnalyticsState.STOPPED, null), - buildDataFrameAnalyticsStats(dfa2.getId(), DataFrameAnalyticsState.STOPPED, 100L), - buildDataFrameAnalyticsStats(dfa3.getId(), DataFrameAnalyticsState.STARTED, 200L) - )); + givenDataFrameAnalytics( + dataFrameAnalytics, + Arrays.asList( + buildDataFrameAnalyticsStats(dfa1.getId(), DataFrameAnalyticsState.STOPPED, null), + buildDataFrameAnalyticsStats(dfa2.getId(), DataFrameAnalyticsState.STOPPED, 100L), + buildDataFrameAnalyticsStats(dfa3.getId(), DataFrameAnalyticsState.STARTED, 200L) + ) + ); Map expectedDfaCountByAnalysis = new HashMap<>(); dataFrameAnalytics.forEach(dfa -> { @@ -209,43 +232,64 @@ public void testUsage() throws Exception { expectedDfaCountByAnalysis.put(analysisName, ++analysisCount); }); - givenProcessorStats(Arrays.asList( - buildNodeStats( - Arrays.asList("pipeline1", "pipeline2", "pipeline3"), - Arrays.asList( - Arrays.asList( - new IngestStats.ProcessorStat(InferenceProcessor.TYPE, InferenceProcessor.TYPE, new IngestStats.Stats(10, 1, 0, 0)), - new IngestStats.ProcessorStat("grok", "grok", new IngestStats.Stats(10, 1, 0, 0)), - new IngestStats.ProcessorStat( - InferenceProcessor.TYPE, - InferenceProcessor.TYPE, - new IngestStats.Stats(100, 10, 0, 1)) - ), - Arrays.asList( - new IngestStats.ProcessorStat(InferenceProcessor.TYPE, InferenceProcessor.TYPE, new IngestStats.Stats(5, 1, 0, 0)), - new IngestStats.ProcessorStat("grok", "grok", new IngestStats.Stats(10, 1, 0, 0)) - ), + givenProcessorStats( + Arrays.asList( + buildNodeStats( + Arrays.asList("pipeline1", "pipeline2", "pipeline3"), Arrays.asList( - new IngestStats.ProcessorStat("grok", "grok", new IngestStats.Stats(10, 1, 0, 0)) + Arrays.asList( + new IngestStats.ProcessorStat( + InferenceProcessor.TYPE, + InferenceProcessor.TYPE, + new IngestStats.Stats(10, 1, 0, 0) + ), + new IngestStats.ProcessorStat("grok", "grok", new IngestStats.Stats(10, 1, 0, 0)), + new IngestStats.ProcessorStat( + InferenceProcessor.TYPE, + InferenceProcessor.TYPE, + new IngestStats.Stats(100, 10, 0, 1) + ) + ), + Arrays.asList( + new IngestStats.ProcessorStat( + InferenceProcessor.TYPE, + InferenceProcessor.TYPE, + new IngestStats.Stats(5, 1, 0, 0) + ), + new IngestStats.ProcessorStat("grok", "grok", new IngestStats.Stats(10, 1, 0, 0)) + ), + Arrays.asList(new IngestStats.ProcessorStat("grok", "grok", new IngestStats.Stats(10, 1, 0, 0))) ) - )), - buildNodeStats( - Arrays.asList("pipeline1", "pipeline2", "pipeline3"), - Arrays.asList( - Arrays.asList( - new IngestStats.ProcessorStat(InferenceProcessor.TYPE, InferenceProcessor.TYPE, new IngestStats.Stats(0, 0, 0, 0)), - new IngestStats.ProcessorStat("grok", "grok", new IngestStats.Stats(0, 0, 0, 0)), - new IngestStats.ProcessorStat(InferenceProcessor.TYPE, InferenceProcessor.TYPE, new IngestStats.Stats(10, 1, 0, 0)) - ), - Arrays.asList( - new IngestStats.ProcessorStat(InferenceProcessor.TYPE, InferenceProcessor.TYPE, new IngestStats.Stats(5, 1, 0, 0)), - new IngestStats.ProcessorStat("grok", "grok", new IngestStats.Stats(10, 1, 0, 0)) - ), + ), + buildNodeStats( + Arrays.asList("pipeline1", "pipeline2", "pipeline3"), Arrays.asList( - new IngestStats.ProcessorStat("grok", "grok", new IngestStats.Stats(10, 1, 0, 0)) + Arrays.asList( + new IngestStats.ProcessorStat( + InferenceProcessor.TYPE, + InferenceProcessor.TYPE, + new IngestStats.Stats(0, 0, 0, 0) + ), + new IngestStats.ProcessorStat("grok", "grok", new IngestStats.Stats(0, 0, 0, 0)), + new IngestStats.ProcessorStat( + InferenceProcessor.TYPE, + InferenceProcessor.TYPE, + new IngestStats.Stats(10, 1, 0, 0) + ) + ), + Arrays.asList( + new IngestStats.ProcessorStat( + InferenceProcessor.TYPE, + InferenceProcessor.TYPE, + new IngestStats.Stats(5, 1, 0, 0) + ), + new IngestStats.ProcessorStat("grok", "grok", new IngestStats.Stats(10, 1, 0, 0)) + ), + Arrays.asList(new IngestStats.ProcessorStat("grok", "grok", new IngestStats.Stats(10, 1, 0, 0))) ) - )) - )); + ) + ) + ); TrainedModelConfig trainedModel1 = TrainedModelConfigTests.createTestInstance("model_1") .setEstimatedHeapMemory(100) @@ -371,10 +415,14 @@ public void testUsage() throws Exception { assertThat(source.getValue("inference.trained_models.estimated_operations.total"), equalTo(1200.0)); assertThat(source.getValue("inference.trained_models.estimated_operations.avg"), equalTo(400.0)); assertThat(source.getValue("inference.trained_models.count.total"), equalTo(4)); - assertThat(source.getValue("inference.trained_models.count.classification"), - equalTo(trainedModelsCountByAnalysis.get("classification"))); - assertThat(source.getValue("inference.trained_models.count.regression"), - equalTo(trainedModelsCountByAnalysis.get("regression"))); + assertThat( + source.getValue("inference.trained_models.count.classification"), + equalTo(trainedModelsCountByAnalysis.get("classification")) + ); + assertThat( + source.getValue("inference.trained_models.count.regression"), + equalTo(trainedModelsCountByAnalysis.get("regression")) + ); assertThat(source.getValue("inference.trained_models.count.prepackaged"), equalTo(1)); assertThat(source.getValue("inference.trained_models.count.other"), equalTo(1)); @@ -396,8 +444,11 @@ public void testUsageWithOrphanedTask() throws Exception { Settings.Builder settings = Settings.builder().put(commonSettings); settings.put("xpack.ml.enabled", true); - Job opened1 = buildJob("opened1", Collections.singletonList(buildMinDetector("foo")), - Collections.singletonMap("created_by", randomFrom("a-cool-module", "a_cool_module", "a cool module"))); + Job opened1 = buildJob( + "opened1", + Collections.singletonList(buildMinDetector("foo")), + Collections.singletonMap("created_by", randomFrom("a-cool-module", "a_cool_module", "a cool module")) + ); GetJobsStatsAction.Response.JobStats opened1JobStats = buildJobStats("opened1", JobState.OPENED, 100L, 3L); // NB: we have JobStats but no Job for "opened2" GetJobsStatsAction.Response.JobStats opened2JobStats = buildJobStats("opened2", JobState.OPENED, 200L, 8L); @@ -522,19 +573,16 @@ public void testUsageGivenMlMetadataNotInstalled() throws Exception { private void givenJobs(List jobs, List jobsStats) { doAnswer(invocationOnMock -> { @SuppressWarnings("unchecked") - ActionListener> jobListener = - (ActionListener>) invocationOnMock.getArguments()[2]; - jobListener.onResponse( - new QueryPage<>(jobs, jobs.size(), Job.RESULTS_FIELD)); + ActionListener> jobListener = (ActionListener>) invocationOnMock.getArguments()[2]; + jobListener.onResponse(new QueryPage<>(jobs, jobs.size(), Job.RESULTS_FIELD)); return Void.TYPE; }).when(jobManager).expandJobs(eq(Metadata.ALL), eq(true), any()); doAnswer(invocationOnMock -> { @SuppressWarnings("unchecked") - ActionListener listener = - (ActionListener) invocationOnMock.getArguments()[2]; - listener.onResponse(new GetJobsStatsAction.Response( - new QueryPage<>(jobsStats, jobsStats.size(), Job.RESULTS_FIELD))); + ActionListener listener = (ActionListener) invocationOnMock + .getArguments()[2]; + listener.onResponse(new GetJobsStatsAction.Response(new QueryPage<>(jobsStats, jobsStats.size(), Job.RESULTS_FIELD))); return Void.TYPE; }).when(client).execute(same(GetJobsStatsAction.INSTANCE), any(), any()); } @@ -543,13 +591,21 @@ private ClusterState givenNodeCount(int nodeCount) { DiscoveryNodes.Builder nodesBuilder = DiscoveryNodes.builder(); for (int i = 0; i < nodeCount; i++) { Map attrs = Map.of(MachineLearning.MACHINE_MEMORY_NODE_ATTR, "1000000000"); - Set roles = Set.of(DiscoveryNodeRole.DATA_ROLE, DiscoveryNodeRole.MASTER_ROLE, - DiscoveryNodeRole.INGEST_ROLE, DiscoveryNodeRole.ML_ROLE); - nodesBuilder.add(new DiscoveryNode("ml-feature-set-given-ml-node-" + i, - new TransportAddress(TransportAddress.META_ADDRESS, 9100 + i), - attrs, - roles, - Version.CURRENT)); + Set roles = Set.of( + DiscoveryNodeRole.DATA_ROLE, + DiscoveryNodeRole.MASTER_ROLE, + DiscoveryNodeRole.INGEST_ROLE, + DiscoveryNodeRole.ML_ROLE + ); + nodesBuilder.add( + new DiscoveryNode( + "ml-feature-set-given-ml-node-" + i, + new TransportAddress(TransportAddress.META_ADDRESS, 9100 + i), + attrs, + roles, + Version.CURRENT + ) + ); } for (int i = 0; i < randomIntBetween(1, 3); i++) { Map attrs = new HashMap<>(); @@ -557,11 +613,15 @@ private ClusterState givenNodeCount(int nodeCount) { roles.add(DiscoveryNodeRole.DATA_ROLE); roles.add(DiscoveryNodeRole.MASTER_ROLE); roles.add(DiscoveryNodeRole.INGEST_ROLE); - nodesBuilder.add(new DiscoveryNode("ml-feature-set-given-non-ml-node-" + i, - new TransportAddress(TransportAddress.META_ADDRESS, 9300 + i), - attrs, - roles, - Version.CURRENT)); + nodesBuilder.add( + new DiscoveryNode( + "ml-feature-set-given-non-ml-node-" + i, + new TransportAddress(TransportAddress.META_ADDRESS, 9300 + i), + attrs, + roles, + Version.CURRENT + ) + ); } return new ClusterState.Builder(ClusterState.EMPTY_STATE).nodes(nodesBuilder.build()).build(); } @@ -569,37 +629,42 @@ private ClusterState givenNodeCount(int nodeCount) { private void givenDatafeeds(List datafeedStats) { doAnswer(invocationOnMock -> { @SuppressWarnings("unchecked") - ActionListener listener = - (ActionListener) invocationOnMock.getArguments()[2]; - listener.onResponse(new GetDatafeedsStatsAction.Response( - new QueryPage<>(datafeedStats, datafeedStats.size(), DatafeedConfig.RESULTS_FIELD))); + ActionListener listener = (ActionListener) invocationOnMock + .getArguments()[2]; + listener.onResponse( + new GetDatafeedsStatsAction.Response(new QueryPage<>(datafeedStats, datafeedStats.size(), DatafeedConfig.RESULTS_FIELD)) + ); return Void.TYPE; }).when(client).execute(same(GetDatafeedsStatsAction.INSTANCE), any(), any()); } - private void givenDataFrameAnalytics(List configs, - List stats) { + private void givenDataFrameAnalytics( + List configs, + List stats + ) { assert configs.size() == stats.size(); doAnswer(invocationOnMock -> { @SuppressWarnings("unchecked") - ActionListener listener = - (ActionListener) invocationOnMock.getArguments()[2]; - listener.onResponse(new GetDataFrameAnalyticsAction.Response( - new QueryPage<>(configs, - configs.size(), - GetDataFrameAnalyticsAction.Response.RESULTS_FIELD))); + ActionListener listener = (ActionListener< + GetDataFrameAnalyticsAction.Response>) invocationOnMock.getArguments()[2]; + listener.onResponse( + new GetDataFrameAnalyticsAction.Response( + new QueryPage<>(configs, configs.size(), GetDataFrameAnalyticsAction.Response.RESULTS_FIELD) + ) + ); return Void.TYPE; }).when(client).execute(same(GetDataFrameAnalyticsAction.INSTANCE), any(), any()); doAnswer(invocationOnMock -> { @SuppressWarnings("unchecked") - ActionListener listener = - (ActionListener) invocationOnMock.getArguments()[2]; - listener.onResponse(new GetDataFrameAnalyticsStatsAction.Response( - new QueryPage<>(stats, - stats.size(), - GetDataFrameAnalyticsAction.Response.RESULTS_FIELD))); + ActionListener listener = (ActionListener< + GetDataFrameAnalyticsStatsAction.Response>) invocationOnMock.getArguments()[2]; + listener.onResponse( + new GetDataFrameAnalyticsStatsAction.Response( + new QueryPage<>(stats, stats.size(), GetDataFrameAnalyticsAction.Response.RESULTS_FIELD) + ) + ); return Void.TYPE; }).when(client).execute(same(GetDataFrameAnalyticsStatsAction.INSTANCE), any(), any()); } @@ -607,8 +672,7 @@ private void givenDataFrameAnalytics(List configs, private void givenProcessorStats(List stats) { doAnswer(invocationOnMock -> { @SuppressWarnings("unchecked") - ActionListener listener = - (ActionListener) invocationOnMock.getArguments()[2]; + ActionListener listener = (ActionListener) invocationOnMock.getArguments()[2]; listener.onResponse(new NodesStatsResponse(new ClusterName("_name"), stats, Collections.emptyList())); return Void.TYPE; }).when(client).execute(same(NodesStatsAction.INSTANCE), any(), any()); @@ -617,12 +681,13 @@ private void givenProcessorStats(List stats) { private void givenTrainedModels(List trainedModels) { doAnswer(invocationOnMock -> { @SuppressWarnings("unchecked") - ActionListener listener = - (ActionListener) invocationOnMock.getArguments()[2]; - listener.onResponse(new GetTrainedModelsAction.Response( - new QueryPage<>(trainedModels, - trainedModels.size(), - GetDataFrameAnalyticsAction.Response.RESULTS_FIELD))); + ActionListener listener = (ActionListener) invocationOnMock + .getArguments()[2]; + listener.onResponse( + new GetTrainedModelsAction.Response( + new QueryPage<>(trainedModels, trainedModels.size(), GetDataFrameAnalyticsAction.Response.RESULTS_FIELD) + ) + ); return Void.TYPE; }).when(client).execute(same(GetTrainedModelsAction.INSTANCE), any(), any()); } @@ -640,15 +705,18 @@ private static Job buildJob(String jobId, List detectors) { private static Job buildJob(String jobId, List detectors, Map customSettings) { AnalysisConfig.Builder analysisConfig = new AnalysisConfig.Builder(detectors); - return new Job.Builder(jobId) - .setAnalysisConfig(analysisConfig) - .setDataDescription(new DataDescription.Builder()) - .setCustomSettings(customSettings) - .build(new Date(randomNonNegativeLong())); + return new Job.Builder(jobId).setAnalysisConfig(analysisConfig) + .setDataDescription(new DataDescription.Builder()) + .setCustomSettings(customSettings) + .build(new Date(randomNonNegativeLong())); } - private static GetJobsStatsAction.Response.JobStats buildJobStats(String jobId, JobState state, long modelBytes, - long numberOfForecasts) { + private static GetJobsStatsAction.Response.JobStats buildJobStats( + String jobId, + JobState state, + long modelBytes, + long numberOfForecasts + ) { ModelSizeStats.Builder modelSizeStats = new ModelSizeStats.Builder(jobId); modelSizeStats.setModelBytes(modelBytes); GetJobsStatsAction.Response.JobStats jobStats = mock(GetJobsStatsAction.Response.JobStats.class); @@ -667,8 +735,11 @@ private static GetDatafeedsStatsAction.Response.DatafeedStats buildDatafeedStats return stats; } - private static GetDataFrameAnalyticsStatsAction.Response.Stats buildDataFrameAnalyticsStats(String jobId, - DataFrameAnalyticsState state, @Nullable Long peakUsageBytes) { + private static GetDataFrameAnalyticsStatsAction.Response.Stats buildDataFrameAnalyticsStats( + String jobId, + DataFrameAnalyticsState state, + @Nullable Long peakUsageBytes + ) { GetDataFrameAnalyticsStatsAction.Response.Stats stats = mock(GetDataFrameAnalyticsStatsAction.Response.Stats.class); when(stats.getState()).thenReturn(state); if (peakUsageBytes != null) { @@ -679,12 +750,29 @@ private static GetDataFrameAnalyticsStatsAction.Response.Stats buildDataFrameAna private static NodeStats buildNodeStats(List pipelineNames, List> processorStats) { IngestStats ingestStats = new IngestStats( - new IngestStats.Stats(0,0,0,0), + new IngestStats.Stats(0, 0, 0, 0), Collections.emptyList(), - IntStream.range(0, pipelineNames.size()).boxed().collect(Collectors.toMap(pipelineNames::get, processorStats::get))); - return new NodeStats(mock(DiscoveryNode.class), - Instant.now().toEpochMilli(), null, null, null, null, null, null, null, null, - null, null, null, ingestStats, null, null, null); + IntStream.range(0, pipelineNames.size()).boxed().collect(Collectors.toMap(pipelineNames::get, processorStats::get)) + ); + return new NodeStats( + mock(DiscoveryNode.class), + Instant.now().toEpochMilli(), + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + ingestStats, + null, + null, + null + ); } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MachineLearningTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MachineLearningTests.java index 6bd8638e6e757..349b6fd8bbd8e 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MachineLearningTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MachineLearningTests.java @@ -55,18 +55,17 @@ public void testPrePostSystemIndexUpgrade_givenNotInUpgradeMode() { MachineLearning machineLearning = createMachineLearning(Settings.EMPTY); SetOnce> response = new SetOnce<>(); - machineLearning.prepareForIndicesMigration(clusterService, client, ActionListener.wrap( - response::set, - e -> fail(e.getMessage()) - )); + machineLearning.prepareForIndicesMigration(clusterService, client, ActionListener.wrap(response::set, e -> fail(e.getMessage()))); assertThat(response.get(), equalTo(Collections.singletonMap("already_in_upgrade_mode", false))); verify(client).execute(same(SetUpgradeModeAction.INSTANCE), eq(new SetUpgradeModeAction.Request(true)), any(ActionListener.class)); - machineLearning.indicesMigrationComplete(response.get(), clusterService, client, ActionListener.wrap( - ESTestCase::assertTrue, - e -> fail(e.getMessage()) - )); + machineLearning.indicesMigrationComplete( + response.get(), + clusterService, + client, + ActionListener.wrap(ESTestCase::assertTrue, e -> fail(e.getMessage())) + ); verify(client).execute(same(SetUpgradeModeAction.INSTANCE), eq(new SetUpgradeModeAction.Request(false)), any(ActionListener.class)); @@ -77,24 +76,25 @@ public void testPrePostSystemIndexUpgrade_givenAlreadyInUpgradeMode() { ClusterService clusterService = mock(ClusterService.class); when(clusterService.state()).thenReturn( ClusterState.builder(ClusterName.DEFAULT) - .metadata(Metadata.builder().putCustom(MlMetadata.TYPE, new MlMetadata.Builder().isUpgradeMode(true).build())).build()); + .metadata(Metadata.builder().putCustom(MlMetadata.TYPE, new MlMetadata.Builder().isUpgradeMode(true).build())) + .build() + ); Client client = mock(Client.class); MachineLearning machineLearning = createMachineLearning(Settings.EMPTY); SetOnce> response = new SetOnce<>(); - machineLearning.prepareForIndicesMigration(clusterService, client, ActionListener.wrap( - response::set, - e -> fail(e.getMessage()) - )); + machineLearning.prepareForIndicesMigration(clusterService, client, ActionListener.wrap(response::set, e -> fail(e.getMessage()))); assertThat(response.get(), equalTo(Collections.singletonMap("already_in_upgrade_mode", true))); verifyZeroInteractions(client); - machineLearning.indicesMigrationComplete(response.get(), clusterService, client, ActionListener.wrap( - ESTestCase::assertTrue, - e -> fail(e.getMessage()) - )); + machineLearning.indicesMigrationComplete( + response.get(), + clusterService, + client, + ActionListener.wrap(ESTestCase::assertTrue, e -> fail(e.getMessage())) + ); // Neither pre nor post should have called any action verifyZeroInteractions(client); @@ -129,10 +129,16 @@ public void testMaxMachineMemoryPercent_givenInvalidSetting() { Settings.Builder settings = Settings.builder(); int invalidMaxMachineMemoryPercent = randomFrom(4, 201); settings.put(MachineLearning.MAX_MACHINE_MEMORY_PERCENT.getKey(), invalidMaxMachineMemoryPercent); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> MachineLearning.MAX_MACHINE_MEMORY_PERCENT.get(settings.build())); - assertThat(e.getMessage(), startsWith("Failed to parse value [" + invalidMaxMachineMemoryPercent - + "] for setting [xpack.ml.max_machine_memory_percent] must be")); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> MachineLearning.MAX_MACHINE_MEMORY_PERCENT.get(settings.build()) + ); + assertThat( + e.getMessage(), + startsWith( + "Failed to parse value [" + invalidMaxMachineMemoryPercent + "] for setting [xpack.ml.max_machine_memory_percent] must be" + ) + ); } public void testNoAttributes_givenNoClash() { @@ -168,8 +174,13 @@ public void testNoAttributes_givenClash() { MachineLearning machineLearning = createMachineLearning(builder.put("path.home", createTempDir()).build()); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, machineLearning::additionalSettings); assertThat(e.getMessage(), startsWith("Directly setting [node.attr.ml.")); - assertThat(e.getMessage(), containsString("] is not permitted - " + - "it is reserved for machine learning. If your intention was to customize machine learning, set the [xpack.ml.")); + assertThat( + e.getMessage(), + containsString( + "] is not permitted - " + + "it is reserved for machine learning. If your intention was to customize machine learning, set the [xpack.ml." + ) + ); } private MachineLearning createMachineLearning(Settings settings) { diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MlAssignmentNotifierTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MlAssignmentNotifierTests.java index 7ebc39a3b1cf1..c3a73e713fac4 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MlAssignmentNotifierTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MlAssignmentNotifierTests.java @@ -59,42 +59,56 @@ public void setupMocks() { } public void testClusterChanged_assign() { - MlAssignmentNotifier notifier = new MlAssignmentNotifier(anomalyDetectionAuditor, dataFrameAnalyticsAuditor, threadPool, - clusterService); + MlAssignmentNotifier notifier = new MlAssignmentNotifier( + anomalyDetectionAuditor, + dataFrameAnalyticsAuditor, + threadPool, + clusterService + ); ClusterState previous = ClusterState.builder(new ClusterName("_name")) - .metadata(Metadata.builder().putCustom(PersistentTasksCustomMetadata.TYPE, - new PersistentTasksCustomMetadata(0L, Collections.emptyMap()))) - .build(); + .metadata( + Metadata.builder() + .putCustom(PersistentTasksCustomMetadata.TYPE, new PersistentTasksCustomMetadata(0L, Collections.emptyMap())) + ) + .build(); - PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); + PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); addJobTask("job_id", "_node_id", null, tasksBuilder); Metadata metadata = Metadata.builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build()).build(); ClusterState newState = ClusterState.builder(new ClusterName("_name")) - .metadata(metadata) - // set local node master - .nodes(DiscoveryNodes.builder() - .add(new DiscoveryNode("_node_id", new TransportAddress(InetAddress.getLoopbackAddress(), 9300), Version.CURRENT)) - .localNodeId("_node_id") - .masterNodeId("_node_id")) - .build(); + .metadata(metadata) + // set local node master + .nodes( + DiscoveryNodes.builder() + .add(new DiscoveryNode("_node_id", new TransportAddress(InetAddress.getLoopbackAddress(), 9300), Version.CURRENT)) + .localNodeId("_node_id") + .masterNodeId("_node_id") + ) + .build(); notifier.clusterChanged(new ClusterChangedEvent("_test", newState, previous)); verify(anomalyDetectionAuditor, times(1)).info("job_id", "Opening job on node [_node_id]"); // no longer master newState = ClusterState.builder(new ClusterName("_name")) - .metadata(metadata) - .nodes(DiscoveryNodes.builder() + .metadata(metadata) + .nodes( + DiscoveryNodes.builder() .add(new DiscoveryNode("_node_id", new TransportAddress(InetAddress.getLoopbackAddress(), 9300), Version.CURRENT)) - .localNodeId("_node_id")) - .build(); + .localNodeId("_node_id") + ) + .build(); notifier.clusterChanged(new ClusterChangedEvent("_test", newState, previous)); verifyNoMoreInteractions(anomalyDetectionAuditor); } public void testClusterChanged_unassign() { - MlAssignmentNotifier notifier = new MlAssignmentNotifier(anomalyDetectionAuditor, dataFrameAnalyticsAuditor, threadPool, - clusterService); + MlAssignmentNotifier notifier = new MlAssignmentNotifier( + anomalyDetectionAuditor, + dataFrameAnalyticsAuditor, + threadPool, + clusterService + ); PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); addJobTask("job_id", "_node_id", null, tasksBuilder); @@ -102,10 +116,12 @@ public void testClusterChanged_unassign() { ClusterState previous = ClusterState.builder(new ClusterName("_name")) .metadata(metadata) // set local node master - .nodes(DiscoveryNodes.builder() - .add(new DiscoveryNode("_node_id", new TransportAddress(InetAddress.getLoopbackAddress(), 9200), Version.CURRENT)) - .localNodeId("_node_id") - .masterNodeId("_node_id")) + .nodes( + DiscoveryNodes.builder() + .add(new DiscoveryNode("_node_id", new TransportAddress(InetAddress.getLoopbackAddress(), 9200), Version.CURRENT)) + .localNodeId("_node_id") + .masterNodeId("_node_id") + ) .build(); tasksBuilder = PersistentTasksCustomMetadata.builder(); @@ -114,10 +130,12 @@ public void testClusterChanged_unassign() { ClusterState newState = ClusterState.builder(new ClusterName("_name")) .metadata(metadata) // set local node master - .nodes(DiscoveryNodes.builder() - .add(new DiscoveryNode("_node_id", new TransportAddress(InetAddress.getLoopbackAddress(), 9200), Version.CURRENT)) - .localNodeId("_node_id") - .masterNodeId("_node_id")) + .nodes( + DiscoveryNodes.builder() + .add(new DiscoveryNode("_node_id", new TransportAddress(InetAddress.getLoopbackAddress(), 9200), Version.CURRENT)) + .localNodeId("_node_id") + .masterNodeId("_node_id") + ) .build(); notifier.clusterChanged(new ClusterChangedEvent("_test", newState, previous)); verify(anomalyDetectionAuditor, times(1)).info("job_id", "Job unassigned from node [_node_id]"); @@ -125,9 +143,11 @@ public void testClusterChanged_unassign() { // no longer master newState = ClusterState.builder(new ClusterName("_name")) .metadata(metadata) - .nodes(DiscoveryNodes.builder() - .add(new DiscoveryNode("_node_id", new TransportAddress(InetAddress.getLoopbackAddress(), 9200), Version.CURRENT)) - .localNodeId("_node_id")) + .nodes( + DiscoveryNodes.builder() + .add(new DiscoveryNode("_node_id", new TransportAddress(InetAddress.getLoopbackAddress(), 9200), Version.CURRENT)) + .localNodeId("_node_id") + ) .build(); notifier.clusterChanged(new ClusterChangedEvent("_test", newState, previous)); @@ -135,55 +155,66 @@ public void testClusterChanged_unassign() { } public void testClusterChanged_noPersistentTaskChanges() { - MlAssignmentNotifier notifier = new MlAssignmentNotifier(anomalyDetectionAuditor, dataFrameAnalyticsAuditor, threadPool, - clusterService); + MlAssignmentNotifier notifier = new MlAssignmentNotifier( + anomalyDetectionAuditor, + dataFrameAnalyticsAuditor, + threadPool, + clusterService + ); - PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); + PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); addJobTask("job_id", null, null, tasksBuilder); Metadata metadata = Metadata.builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build()).build(); - ClusterState previous = ClusterState.builder(new ClusterName("_name")) - .metadata(metadata) - .build(); + ClusterState previous = ClusterState.builder(new ClusterName("_name")).metadata(metadata).build(); ClusterState newState = ClusterState.builder(new ClusterName("_name")) - .metadata(metadata) - // set local node master - .nodes(DiscoveryNodes.builder() - .add(new DiscoveryNode("_node_id", new TransportAddress(InetAddress.getLoopbackAddress(), 9200), Version.CURRENT)) - .localNodeId("_node_id") - .masterNodeId("_node_id")) - .build(); + .metadata(metadata) + // set local node master + .nodes( + DiscoveryNodes.builder() + .add(new DiscoveryNode("_node_id", new TransportAddress(InetAddress.getLoopbackAddress(), 9200), Version.CURRENT)) + .localNodeId("_node_id") + .masterNodeId("_node_id") + ) + .build(); notifier.clusterChanged(new ClusterChangedEvent("_test", newState, previous)); verifyNoMoreInteractions(anomalyDetectionAuditor); // no longer master newState = ClusterState.builder(new ClusterName("_name")) - .metadata(metadata) - .nodes(DiscoveryNodes.builder() + .metadata(metadata) + .nodes( + DiscoveryNodes.builder() .add(new DiscoveryNode("_node_id", new TransportAddress(InetAddress.getLoopbackAddress(), 9200), Version.CURRENT)) - .localNodeId("_node_id")) - .build(); + .localNodeId("_node_id") + ) + .build(); notifier.clusterChanged(new ClusterChangedEvent("_test", newState, previous)); } public void testAuditUnassignedMlTasks() { - MlAssignmentNotifier notifier = new MlAssignmentNotifier(anomalyDetectionAuditor, dataFrameAnalyticsAuditor, threadPool, - clusterService); + MlAssignmentNotifier notifier = new MlAssignmentNotifier( + anomalyDetectionAuditor, + dataFrameAnalyticsAuditor, + threadPool, + clusterService + ); - PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); + PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); addJobTask("job_id", null, null, tasksBuilder); Metadata metadata = Metadata.builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build()).build(); ClusterState newState = ClusterState.builder(new ClusterName("_name")) .metadata(metadata) // set local node master - .nodes(DiscoveryNodes.builder() - .add(new DiscoveryNode("_node_id", new TransportAddress(InetAddress.getLoopbackAddress(), 9200), Version.CURRENT)) - .localNodeId("_node_id") - .masterNodeId("_node_id")) + .nodes( + DiscoveryNodes.builder() + .add(new DiscoveryNode("_node_id", new TransportAddress(InetAddress.getLoopbackAddress(), 9200), Version.CURRENT)) + .localNodeId("_node_id") + .masterNodeId("_node_id") + ) .build(); notifier.auditUnassignedMlTasks(newState.nodes(), newState.metadata().custom(PersistentTasksCustomMetadata.TYPE)); - verify(anomalyDetectionAuditor, times(1)) - .warning("job_id", "No node found to open job. Reasons [test assignment]"); + verify(anomalyDetectionAuditor, times(1)).warning("job_id", "No node found to open job. Reasons [test assignment]"); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MlConfigMigrationEligibilityCheckTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MlConfigMigrationEligibilityCheckTests.java index 2fbb98f012144..508060292c85f 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MlConfigMigrationEligibilityCheckTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MlConfigMigrationEligibilityCheckTests.java @@ -20,12 +20,12 @@ import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.settings.ClusterSettings; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.index.Index; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.persistent.PersistentTasksCustomMetadata; import org.elasticsearch.search.SearchModule; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.ml.MlConfigIndex; import org.elasticsearch.xpack.core.ml.MlMetadata; import org.elasticsearch.xpack.core.ml.MlTasks; @@ -71,8 +71,7 @@ public void testCanStartMigration_givenMissingIndex() { Settings settings = newSettings(true); givenClusterSettings(settings); - ClusterState clusterState = ClusterState.builder(new ClusterName("migratortests")) - .build(); + ClusterState clusterState = ClusterState.builder(new ClusterName("migratortests")).build(); MlConfigMigrationEligibilityCheck check = new MlConfigMigrationEligibilityCheck(settings, clusterService); assertFalse(check.canStartMigration(clusterState)); @@ -86,9 +85,7 @@ public void testCanStartMigration_givenInactiveShards() { Metadata.Builder metadata = Metadata.builder(); RoutingTable.Builder routingTable = RoutingTable.builder(); addMlConfigIndex(metadata, routingTable); - ClusterState clusterState = ClusterState.builder(new ClusterName("migratortests")) - .metadata(metadata) - .build(); + ClusterState clusterState = ClusterState.builder(new ClusterName("migratortests")).metadata(metadata).build(); MlConfigMigrationEligibilityCheck check = new MlConfigMigrationEligibilityCheck(settings, clusterService); assertFalse(check.canStartMigration(clusterState)); @@ -96,7 +93,8 @@ public void testCanStartMigration_givenInactiveShards() { private void addMlConfigIndex(Metadata.Builder metadata, RoutingTable.Builder routingTable) { IndexMetadata.Builder indexMetadata = IndexMetadata.builder(MlConfigIndex.indexName()); - indexMetadata.settings(Settings.builder() + indexMetadata.settings( + Settings.builder() .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) @@ -104,12 +102,17 @@ private void addMlConfigIndex(Metadata.Builder metadata, RoutingTable.Builder ro metadata.put(indexMetadata); Index index = new Index(MlConfigIndex.indexName(), "_uuid"); ShardId shardId = new ShardId(index, 0); - ShardRouting shardRouting = ShardRouting.newUnassigned(shardId, true, RecoverySource.EmptyStoreRecoverySource.INSTANCE, - new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "")); + ShardRouting shardRouting = ShardRouting.newUnassigned( + shardId, + true, + RecoverySource.EmptyStoreRecoverySource.INSTANCE, + new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "") + ); shardRouting = shardRouting.initialize("node_id", null, 0L); shardRouting = shardRouting.moveToStarted(); - routingTable.add(IndexRoutingTable.builder(index) - .addIndexShard(new IndexShardRoutingTable.Builder(shardId).addShard(shardRouting).build())); + routingTable.add( + IndexRoutingTable.builder(index).addIndexShard(new IndexShardRoutingTable.Builder(shardId).addShard(shardRouting).build()) + ); } public void testJobIsEligibleForMigration_givenJobNotInClusterState() { @@ -128,14 +131,18 @@ public void testJobIsEligibleForMigration_givenDeletingJob() { MlMetadata.Builder mlMetadata = new MlMetadata.Builder().putJob(deletingJob, false); PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); - tasksBuilder.addTask(MlTasks.jobTaskId(deletingJob.getId()), - MlTasks.JOB_TASK_NAME, new OpenJobAction.JobParams(deletingJob.getId()), - new PersistentTasksCustomMetadata.Assignment("node-1", "test assignment")); + tasksBuilder.addTask( + MlTasks.jobTaskId(deletingJob.getId()), + MlTasks.JOB_TASK_NAME, + new OpenJobAction.JobParams(deletingJob.getId()), + new PersistentTasksCustomMetadata.Assignment("node-1", "test assignment") + ); ClusterState clusterState = ClusterState.builder(new ClusterName("migratortests")) - .metadata(Metadata.builder() - .putCustom(MlMetadata.TYPE, mlMetadata.build()) - .putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build()) + .metadata( + Metadata.builder() + .putCustom(MlMetadata.TYPE, mlMetadata.build()) + .putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build()) ) .build(); @@ -152,13 +159,18 @@ public void testJobIsEligibleForMigration_givenOpenJob() { MlMetadata.Builder mlMetadata = new MlMetadata.Builder().putJob(openJob, false); PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); - tasksBuilder.addTask(MlTasks.jobTaskId(openJob.getId()), MlTasks.JOB_TASK_NAME, new OpenJobAction.JobParams(openJob.getId()), - new PersistentTasksCustomMetadata.Assignment("node-1", "test assignment")); + tasksBuilder.addTask( + MlTasks.jobTaskId(openJob.getId()), + MlTasks.JOB_TASK_NAME, + new OpenJobAction.JobParams(openJob.getId()), + new PersistentTasksCustomMetadata.Assignment("node-1", "test assignment") + ); ClusterState clusterState = ClusterState.builder(new ClusterName("migratortests")) - .metadata(Metadata.builder() - .putCustom(MlMetadata.TYPE, mlMetadata.build()) - .putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build()) + .metadata( + Metadata.builder() + .putCustom(MlMetadata.TYPE, mlMetadata.build()) + .putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build()) ) .build(); @@ -175,13 +187,18 @@ public void testJobIsEligibleForMigration_givenOpenJobAndAndMigrationIsDisabled( MlMetadata.Builder mlMetadata = new MlMetadata.Builder().putJob(openJob, false); PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); - tasksBuilder.addTask(MlTasks.jobTaskId(openJob.getId()), MlTasks.JOB_TASK_NAME, new OpenJobAction.JobParams(openJob.getId()), - new PersistentTasksCustomMetadata.Assignment("node-1", "test assignment")); + tasksBuilder.addTask( + MlTasks.jobTaskId(openJob.getId()), + MlTasks.JOB_TASK_NAME, + new OpenJobAction.JobParams(openJob.getId()), + new PersistentTasksCustomMetadata.Assignment("node-1", "test assignment") + ); ClusterState clusterState = ClusterState.builder(new ClusterName("migratortests")) - .metadata(Metadata.builder() - .putCustom(MlMetadata.TYPE, mlMetadata.build()) - .putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build()) + .metadata( + Metadata.builder() + .putCustom(MlMetadata.TYPE, mlMetadata.build()) + .putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build()) ) .build(); @@ -202,9 +219,9 @@ public void testJobIsEligibleForMigration_givenClosedJob() { addMlConfigIndex(metadata, routingTable); ClusterState clusterState = ClusterState.builder(new ClusterName("migratortests")) - .metadata(metadata.putCustom(MlMetadata.TYPE, mlMetadata.build())) - .routingTable(routingTable.build()) - .build(); + .metadata(metadata.putCustom(MlMetadata.TYPE, mlMetadata.build())) + .routingTable(routingTable.build()) + .build(); Settings settings = newSettings(true); givenClusterSettings(settings); @@ -219,20 +236,23 @@ public void testJobIsEligibleForMigration_givenOpenAndUnallocatedJob() { MlMetadata.Builder mlMetadata = new MlMetadata.Builder().putJob(openJob, false); PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); - tasksBuilder.addTask(MlTasks.jobTaskId(openJob.getId()), MlTasks.JOB_TASK_NAME, new OpenJobAction.JobParams(openJob.getId()), - new PersistentTasksCustomMetadata.Assignment(null, "no assignment")); + tasksBuilder.addTask( + MlTasks.jobTaskId(openJob.getId()), + MlTasks.JOB_TASK_NAME, + new OpenJobAction.JobParams(openJob.getId()), + new PersistentTasksCustomMetadata.Assignment(null, "no assignment") + ); Metadata.Builder metadata = Metadata.builder(); RoutingTable.Builder routingTable = RoutingTable.builder(); addMlConfigIndex(metadata, routingTable); ClusterState clusterState = ClusterState.builder(new ClusterName("migratortests")) - .metadata(metadata - .putCustom(MlMetadata.TYPE, mlMetadata.build()) - .putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build()) - ) - .routingTable(routingTable.build()) - .build(); + .metadata( + metadata.putCustom(MlMetadata.TYPE, mlMetadata.build()).putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build()) + ) + .routingTable(routingTable.build()) + .build(); Settings settings = newSettings(true); givenClusterSettings(settings); @@ -259,14 +279,18 @@ public void testDatafeedIsEligibleForMigration_givenStartedDatafeed() { String datafeedId = "df-" + openJob.getId(); PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); - tasksBuilder.addTask(MlTasks.datafeedTaskId(datafeedId), MlTasks.DATAFEED_TASK_NAME, + tasksBuilder.addTask( + MlTasks.datafeedTaskId(datafeedId), + MlTasks.DATAFEED_TASK_NAME, new StartDatafeedAction.DatafeedParams(datafeedId, 0L), - new PersistentTasksCustomMetadata.Assignment("node-1", "test assignment")); + new PersistentTasksCustomMetadata.Assignment("node-1", "test assignment") + ); ClusterState clusterState = ClusterState.builder(new ClusterName("migratortests")) - .metadata(Metadata.builder() - .putCustom(MlMetadata.TYPE, mlMetadata.build()) - .putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build()) + .metadata( + Metadata.builder() + .putCustom(MlMetadata.TYPE, mlMetadata.build()) + .putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build()) ) .build(); @@ -285,14 +309,18 @@ public void testDatafeedIsEligibleForMigration_givenStartedDatafeedAndMigrationI String datafeedId = "df-" + openJob.getId(); PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); - tasksBuilder.addTask(MlTasks.datafeedTaskId(datafeedId), MlTasks.DATAFEED_TASK_NAME, + tasksBuilder.addTask( + MlTasks.datafeedTaskId(datafeedId), + MlTasks.DATAFEED_TASK_NAME, new StartDatafeedAction.DatafeedParams(datafeedId, 0L), - new PersistentTasksCustomMetadata.Assignment("node-1", "test assignment")); + new PersistentTasksCustomMetadata.Assignment("node-1", "test assignment") + ); ClusterState clusterState = ClusterState.builder(new ClusterName("migratortests")) - .metadata(Metadata.builder() - .putCustom(MlMetadata.TYPE, mlMetadata.build()) - .putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build()) + .metadata( + Metadata.builder() + .putCustom(MlMetadata.TYPE, mlMetadata.build()) + .putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build()) ) .build(); @@ -315,9 +343,9 @@ public void testDatafeedIsEligibleForMigration_givenStoppedDatafeed() { addMlConfigIndex(metadata, routingTable); ClusterState clusterState = ClusterState.builder(new ClusterName("migratortests")) - .metadata(metadata.putCustom(MlMetadata.TYPE, mlMetadata.build())) - .routingTable(routingTable.build()) - .build(); + .metadata(metadata.putCustom(MlMetadata.TYPE, mlMetadata.build())) + .routingTable(routingTable.build()) + .build(); Settings settings = newSettings(true); givenClusterSettings(settings); @@ -338,16 +366,19 @@ public void testDatafeedIsEligibleForMigration_givenUnallocatedDatafeed() { addMlConfigIndex(metadata, routingTable); PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); - tasksBuilder.addTask(MlTasks.datafeedTaskId(datafeedId), MlTasks.DATAFEED_TASK_NAME, - new StartDatafeedAction.DatafeedParams(datafeedId, 0L), - new PersistentTasksCustomMetadata.Assignment(null, "no assignment")); + tasksBuilder.addTask( + MlTasks.datafeedTaskId(datafeedId), + MlTasks.DATAFEED_TASK_NAME, + new StartDatafeedAction.DatafeedParams(datafeedId, 0L), + new PersistentTasksCustomMetadata.Assignment(null, "no assignment") + ); ClusterState clusterState = ClusterState.builder(new ClusterName("migratortests")) - .metadata(metadata - .putCustom(MlMetadata.TYPE, mlMetadata.build()) - .putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())) - .routingTable(routingTable.build()) - .build(); + .metadata( + metadata.putCustom(MlMetadata.TYPE, mlMetadata.build()).putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build()) + ) + .routingTable(routingTable.build()) + .build(); Settings settings = newSettings(true); givenClusterSettings(settings); @@ -358,15 +389,15 @@ public void testDatafeedIsEligibleForMigration_givenUnallocatedDatafeed() { } private void givenClusterSettings(Settings settings) { - ClusterSettings clusterSettings = new ClusterSettings(settings, new HashSet<>(Collections.singletonList( - MlConfigMigrationEligibilityCheck.ENABLE_CONFIG_MIGRATION))); + ClusterSettings clusterSettings = new ClusterSettings( + settings, + new HashSet<>(Collections.singletonList(MlConfigMigrationEligibilityCheck.ENABLE_CONFIG_MIGRATION)) + ); when(clusterService.getClusterSettings()).thenReturn(clusterSettings); } private static Settings newSettings(boolean migrationEnabled) { - return Settings.builder() - .put(MlConfigMigrationEligibilityCheck.ENABLE_CONFIG_MIGRATION.getKey(), migrationEnabled) - .build(); + return Settings.builder().put(MlConfigMigrationEligibilityCheck.ENABLE_CONFIG_MIGRATION.getKey(), migrationEnabled).build(); } private DatafeedConfig createCompatibleDatafeed(String jobId) { diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MlConfigMigratorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MlConfigMigratorTests.java index 1d3efd6f72b4f..e47b3826c9591 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MlConfigMigratorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MlConfigMigratorTests.java @@ -17,11 +17,11 @@ import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.TransportAddress; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.persistent.PersistentTasksCustomMetadata; import org.elasticsearch.search.SearchModule; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.VersionUtils; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.ml.MlMetadata; import org.elasticsearch.xpack.core.ml.MlTasks; import org.elasticsearch.xpack.core.ml.action.OpenJobAction; @@ -71,33 +71,39 @@ public void testClosedOrUnallocatedJobs() { Job jobWithoutAllocation = JobTests.buildJobBuilder("jobwithoutallocation").build(); Job openJob = JobTests.buildJobBuilder("openjob").build(); - MlMetadata.Builder mlMetadata = new MlMetadata.Builder() - .putJob(closedJob, false) - .putJob(jobWithoutAllocation, false) - .putJob(openJob, false) - .putDatafeed(createCompatibleDatafeed(closedJob.getId()), Collections.emptyMap(), xContentRegistry()); - - PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); - tasksBuilder.addTask(MlTasks.jobTaskId("jobwithoutallocation"), MlTasks.JOB_TASK_NAME, - new OpenJobAction.JobParams("jobwithoutallocation"), - new PersistentTasksCustomMetadata.Assignment(null, "test assignment")); - tasksBuilder.addTask(MlTasks.jobTaskId("openjob"), MlTasks.JOB_TASK_NAME, - new OpenJobAction.JobParams("openjob"), - new PersistentTasksCustomMetadata.Assignment("node1", "test assignment")); + MlMetadata.Builder mlMetadata = new MlMetadata.Builder().putJob(closedJob, false) + .putJob(jobWithoutAllocation, false) + .putJob(openJob, false) + .putDatafeed(createCompatibleDatafeed(closedJob.getId()), Collections.emptyMap(), xContentRegistry()); + + PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); + tasksBuilder.addTask( + MlTasks.jobTaskId("jobwithoutallocation"), + MlTasks.JOB_TASK_NAME, + new OpenJobAction.JobParams("jobwithoutallocation"), + new PersistentTasksCustomMetadata.Assignment(null, "test assignment") + ); + tasksBuilder.addTask( + MlTasks.jobTaskId("openjob"), + MlTasks.JOB_TASK_NAME, + new OpenJobAction.JobParams("openjob"), + new PersistentTasksCustomMetadata.Assignment("node1", "test assignment") + ); DiscoveryNodes nodes = DiscoveryNodes.builder() - .add(new DiscoveryNode("node1", new TransportAddress(InetAddress.getLoopbackAddress(), 9300), Version.CURRENT)) - .localNodeId("node1") - .masterNodeId("node1") - .build(); + .add(new DiscoveryNode("node1", new TransportAddress(InetAddress.getLoopbackAddress(), 9300), Version.CURRENT)) + .localNodeId("node1") + .masterNodeId("node1") + .build(); ClusterState clusterState = ClusterState.builder(new ClusterName("migratortests")) - .metadata(Metadata.builder() - .putCustom(MlMetadata.TYPE, mlMetadata.build()) - .putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build()) - ) - .nodes(nodes) - .build(); + .metadata( + Metadata.builder() + .putCustom(MlMetadata.TYPE, mlMetadata.build()) + .putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build()) + ) + .nodes(nodes) + .build(); assertThat(MlConfigMigrator.closedOrUnallocatedJobs(clusterState), containsInAnyOrder(closedJob, jobWithoutAllocation)); } @@ -110,38 +116,46 @@ public void testStoppedDatafeedConfigs() { DatafeedConfig datafeedWithoutAllocation = createCompatibleDatafeed(job2.getId()); DatafeedConfig startedDatafeed = createCompatibleDatafeed(job3.getId()); - MlMetadata.Builder mlMetadata = new MlMetadata.Builder() - .putJob(job1, false) - .putJob(job2, false) - .putJob(job3, false) - .putDatafeed(stopppedDatafeed, Collections.emptyMap(), xContentRegistry()) - .putDatafeed(datafeedWithoutAllocation, Collections.emptyMap(), xContentRegistry()) - .putDatafeed(startedDatafeed, Collections.emptyMap(), xContentRegistry()); - - PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); - tasksBuilder.addTask(MlTasks.datafeedTaskId(stopppedDatafeed.getId()), MlTasks.DATAFEED_TASK_NAME, - new StartDatafeedAction.DatafeedParams(stopppedDatafeed.getId(), 0L), - new PersistentTasksCustomMetadata.Assignment(null, "test assignment")); - tasksBuilder.addTask(MlTasks.datafeedTaskId(startedDatafeed.getId()), MlTasks.DATAFEED_TASK_NAME, - new StartDatafeedAction.DatafeedParams(stopppedDatafeed.getId(), 0L), - new PersistentTasksCustomMetadata.Assignment("node1", "test assignment")); + MlMetadata.Builder mlMetadata = new MlMetadata.Builder().putJob(job1, false) + .putJob(job2, false) + .putJob(job3, false) + .putDatafeed(stopppedDatafeed, Collections.emptyMap(), xContentRegistry()) + .putDatafeed(datafeedWithoutAllocation, Collections.emptyMap(), xContentRegistry()) + .putDatafeed(startedDatafeed, Collections.emptyMap(), xContentRegistry()); + + PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); + tasksBuilder.addTask( + MlTasks.datafeedTaskId(stopppedDatafeed.getId()), + MlTasks.DATAFEED_TASK_NAME, + new StartDatafeedAction.DatafeedParams(stopppedDatafeed.getId(), 0L), + new PersistentTasksCustomMetadata.Assignment(null, "test assignment") + ); + tasksBuilder.addTask( + MlTasks.datafeedTaskId(startedDatafeed.getId()), + MlTasks.DATAFEED_TASK_NAME, + new StartDatafeedAction.DatafeedParams(stopppedDatafeed.getId(), 0L), + new PersistentTasksCustomMetadata.Assignment("node1", "test assignment") + ); DiscoveryNodes nodes = DiscoveryNodes.builder() - .add(new DiscoveryNode("node1", new TransportAddress(InetAddress.getLoopbackAddress(), 9300), Version.CURRENT)) - .localNodeId("node1") - .masterNodeId("node1") - .build(); + .add(new DiscoveryNode("node1", new TransportAddress(InetAddress.getLoopbackAddress(), 9300), Version.CURRENT)) + .localNodeId("node1") + .masterNodeId("node1") + .build(); ClusterState clusterState = ClusterState.builder(new ClusterName("migratortests")) - .metadata(Metadata.builder() - .putCustom(MlMetadata.TYPE, mlMetadata.build()) - .putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build()) - ) - .nodes(nodes) - .build(); - - assertThat(MlConfigMigrator.stoppedOrUnallocatedDatafeeds(clusterState), - containsInAnyOrder(stopppedDatafeed, datafeedWithoutAllocation)); + .metadata( + Metadata.builder() + .putCustom(MlMetadata.TYPE, mlMetadata.build()) + .putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build()) + ) + .nodes(nodes) + .build(); + + assertThat( + MlConfigMigrator.stoppedOrUnallocatedDatafeeds(clusterState), + containsInAnyOrder(stopppedDatafeed, datafeedWithoutAllocation) + ); } public void testUpdateJobForMigration() { @@ -178,8 +192,10 @@ public void testFilterFailedJobConfigWrites() { jobs.add(JobTests.buildJobBuilder("baz").build()); assertThat(MlConfigMigrator.filterFailedJobConfigWrites(Collections.emptySet(), jobs), hasSize(3)); - assertThat(MlConfigMigrator.filterFailedJobConfigWrites(Collections.singleton(Job.documentId("bar")), jobs), - contains(jobs.get(0), jobs.get(2))); + assertThat( + MlConfigMigrator.filterFailedJobConfigWrites(Collections.singleton(Job.documentId("bar")), jobs), + contains(jobs.get(0), jobs.get(2)) + ); } public void testFilterFailedDatafeedConfigWrites() { @@ -189,8 +205,10 @@ public void testFilterFailedDatafeedConfigWrites() { datafeeds.add(createCompatibleDatafeed("baz")); assertThat(MlConfigMigrator.filterFailedDatafeedConfigWrites(Collections.emptySet(), datafeeds), hasSize(3)); - assertThat(MlConfigMigrator.filterFailedDatafeedConfigWrites(Collections.singleton(DatafeedConfig.documentId("df-foo")), datafeeds), - contains(datafeeds.get(1), datafeeds.get(2))); + assertThat( + MlConfigMigrator.filterFailedDatafeedConfigWrites(Collections.singleton(DatafeedConfig.documentId("df-foo")), datafeeds), + contains(datafeeds.get(1), datafeeds.get(2)) + ); } public void testDocumentsNotWritten() { @@ -204,7 +222,7 @@ public void testDocumentsNotWritten() { when(failure.getCause()).thenReturn(mock(IllegalStateException.class)); when(failed.getFailure()).thenReturn(failure); - BulkResponse bulkResponse = new BulkResponse(new BulkItemResponse[] {ok, failed}, 1L); + BulkResponse bulkResponse = new BulkResponse(new BulkItemResponse[] { ok, failed }, 1L); Set docsIds = MlConfigMigrator.documentsNotWritten(bulkResponse); assertThat(docsIds, contains("failed-doc-id")); } @@ -214,14 +232,16 @@ public void testRemoveJobsAndDatafeeds_removeAll() { Job job2 = JobTests.buildJobBuilder("job2").build(); DatafeedConfig datafeedConfig1 = createCompatibleDatafeed(job1.getId()); DatafeedConfig datafeedConfig2 = createCompatibleDatafeed(job2.getId()); - MlMetadata.Builder mlMetadata = new MlMetadata.Builder() - .putJob(job1, false) - .putJob(job2, false) - .putDatafeed(datafeedConfig1, Collections.emptyMap(), xContentRegistry()) - .putDatafeed(datafeedConfig2, Collections.emptyMap(), xContentRegistry()); + MlMetadata.Builder mlMetadata = new MlMetadata.Builder().putJob(job1, false) + .putJob(job2, false) + .putDatafeed(datafeedConfig1, Collections.emptyMap(), xContentRegistry()) + .putDatafeed(datafeedConfig2, Collections.emptyMap(), xContentRegistry()); MlConfigMigrator.RemovalResult removalResult = MlConfigMigrator.removeJobsAndDatafeeds( - Arrays.asList(job1, job2), Arrays.asList(datafeedConfig1, datafeedConfig2), mlMetadata.build()); + Arrays.asList(job1, job2), + Arrays.asList(datafeedConfig1, datafeedConfig2), + mlMetadata.build() + ); assertThat(removalResult.mlMetadata.getJobs().keySet(), empty()); assertThat(removalResult.mlMetadata.getDatafeeds().keySet(), empty()); @@ -233,14 +253,15 @@ public void testRemoveJobsAndDatafeeds_removeSome() { Job job1 = JobTests.buildJobBuilder("job1").build(); Job job2 = JobTests.buildJobBuilder("job2").build(); DatafeedConfig datafeedConfig1 = createCompatibleDatafeed(job1.getId()); - MlMetadata.Builder mlMetadata = new MlMetadata.Builder() - .putJob(job1, false) - .putJob(job2, false) - .putDatafeed(datafeedConfig1, Collections.emptyMap(), xContentRegistry()); + MlMetadata.Builder mlMetadata = new MlMetadata.Builder().putJob(job1, false) + .putJob(job2, false) + .putDatafeed(datafeedConfig1, Collections.emptyMap(), xContentRegistry()); MlConfigMigrator.RemovalResult removalResult = MlConfigMigrator.removeJobsAndDatafeeds( - Arrays.asList(job1, JobTests.buildJobBuilder("job-none").build()), - Collections.singletonList(createCompatibleDatafeed("job-none")), mlMetadata.build()); + Arrays.asList(job1, JobTests.buildJobBuilder("job-none").build()), + Collections.singletonList(createCompatibleDatafeed("job-none")), + mlMetadata.build() + ); assertThat(removalResult.mlMetadata.getJobs().keySet(), contains("job2")); assertThat(removalResult.mlMetadata.getDatafeeds().keySet(), contains("df-job1")); @@ -257,7 +278,7 @@ public void testLimitWrites_GivenBelowLimit() { Map jobs = new HashMap<>(); int numDatafeeds = MlConfigMigrator.MAX_BULK_WRITE_SIZE / 2; - for (int i=0; i jobs = new HashMap<>(); int numDatafeeds = MlConfigMigrator.MAX_BULK_WRITE_SIZE / 2 + 10; - for (int i=0; i jobs = new HashMap<>(); int numDatafeeds = MlConfigMigrator.MAX_BULK_WRITE_SIZE / 2 - 10; - for (int i=0; i(Collections.emptyList(), 0, new ParseField(""))))) - .when(client).execute(same(GetJobsAction.INSTANCE), any(), any()); + doAnswer(withResponse(new DeleteExpiredDataAction.Response(true))).when(client) + .execute(same(DeleteExpiredDataAction.INSTANCE), any(), any()); + doAnswer(withResponse(new GetJobsAction.Response(new QueryPage<>(Collections.emptyList(), 0, new ParseField(""))))).when(client) + .execute(same(GetJobsAction.INSTANCE), any(), any()); int triggerCount = randomIntBetween(2, 4); CountDownLatch latch = new CountDownLatch(triggerCount); @@ -106,31 +106,36 @@ public void testScheduledTriggeringWhileUpgradeModeIsEnabled() throws Interrupte public void testBothTasksAreTriggered_BothTasksSucceed() throws InterruptedException { assertThatBothTasksAreTriggered( withResponse(new DeleteExpiredDataAction.Response(true)), - withResponse(new GetJobsAction.Response(new QueryPage<>(Collections.emptyList(), 0, new ParseField(""))))); + withResponse(new GetJobsAction.Response(new QueryPage<>(Collections.emptyList(), 0, new ParseField("")))) + ); } public void testBothTasksAreTriggered_DeleteExpiredDataTaskFails() throws InterruptedException { assertThatBothTasksAreTriggered( withResponse(new DeleteExpiredDataAction.Response(false)), - withResponse(new GetJobsAction.Response(new QueryPage<>(Collections.emptyList(), 0, new ParseField(""))))); + withResponse(new GetJobsAction.Response(new QueryPage<>(Collections.emptyList(), 0, new ParseField("")))) + ); } public void testBothTasksAreTriggered_DeleteExpiredDataTaskFailsWithException() throws InterruptedException { assertThatBothTasksAreTriggered( withException(new ElasticsearchException("exception thrown by DeleteExpiredDataAction")), - withResponse(new GetJobsAction.Response(new QueryPage<>(Collections.emptyList(), 0, new ParseField(""))))); + withResponse(new GetJobsAction.Response(new QueryPage<>(Collections.emptyList(), 0, new ParseField("")))) + ); } public void testBothTasksAreTriggered_DeleteJobsTaskFails() throws InterruptedException { assertThatBothTasksAreTriggered( withResponse(new DeleteExpiredDataAction.Response(true)), - withException(new ElasticsearchException("exception thrown by GetJobsAction"))); + withException(new ElasticsearchException("exception thrown by GetJobsAction")) + ); } public void testBothTasksAreTriggered_BothTasksFail() throws InterruptedException { assertThatBothTasksAreTriggered( withException(new ElasticsearchException("exception thrown by DeleteExpiredDataAction")), - withException(new ElasticsearchException("exception thrown by GetJobsAction"))); + withException(new ElasticsearchException("exception thrown by GetJobsAction")) + ); } private void assertThatBothTasksAreTriggered(Answer deleteExpiredDataAnswer, Answer getJobsAnswer) throws InterruptedException { @@ -153,30 +158,32 @@ private void assertThatBothTasksAreTriggered(Answer deleteExpiredDataAnswer, public void testJobInDeletingStateAlreadyHasDeletionTask() throws InterruptedException { String jobId = "job-in-state-deleting"; - TaskInfo taskInfo = - new TaskInfo( - new TaskId("test", 123), - "test", - DeleteJobAction.NAME, - "delete-job-" + jobId, - null, - 0, - 0, - true, - false, - new TaskId("test", 456), - Collections.emptyMap()); + TaskInfo taskInfo = new TaskInfo( + new TaskId("test", 123), + "test", + DeleteJobAction.NAME, + "delete-job-" + jobId, + null, + 0, + 0, + true, + false, + new TaskId("test", 456), + Collections.emptyMap() + ); when(clusterService.state()).thenReturn(createClusterState(false)); - doAnswer(withResponse(new DeleteExpiredDataAction.Response(true))) - .when(client).execute(same(DeleteExpiredDataAction.INSTANCE), any(), any()); + doAnswer(withResponse(new DeleteExpiredDataAction.Response(true))).when(client) + .execute(same(DeleteExpiredDataAction.INSTANCE), any(), any()); Job job = mock(Job.class); when(job.getId()).thenReturn(jobId); when(job.isDeleting()).thenReturn(true); - doAnswer(withResponse(new GetJobsAction.Response(new QueryPage<>(Collections.singletonList(job), 1, new ParseField(""))))) - .when(client).execute(same(GetJobsAction.INSTANCE), any(), any()); + doAnswer(withResponse(new GetJobsAction.Response(new QueryPage<>(Collections.singletonList(job), 1, new ParseField(""))))).when( + client + ).execute(same(GetJobsAction.INSTANCE), any(), any()); doAnswer(withResponse(new ListTasksResponse(Collections.singletonList(taskInfo), Collections.emptyList(), Collections.emptyList()))) - .when(client).execute(same(ListTasksAction.INSTANCE), any(), any()); + .when(client) + .execute(same(ListTasksAction.INSTANCE), any(), any()); CountDownLatch latch = new CountDownLatch(2); try (MlDailyMaintenanceService service = createService(latch, client)) { @@ -203,17 +210,18 @@ public void testJobDoesNotGetDeleted() throws InterruptedException { private void testJobInDeletingStateDoesNotHaveDeletionTask(boolean deleted) throws InterruptedException { String jobId = "job-in-state-deleting"; when(clusterService.state()).thenReturn(createClusterState(false)); - doAnswer(withResponse(new DeleteExpiredDataAction.Response(true))) - .when(client).execute(same(DeleteExpiredDataAction.INSTANCE), any(), any()); + doAnswer(withResponse(new DeleteExpiredDataAction.Response(true))).when(client) + .execute(same(DeleteExpiredDataAction.INSTANCE), any(), any()); Job job = mock(Job.class); when(job.getId()).thenReturn(jobId); when(job.isDeleting()).thenReturn(true); - doAnswer(withResponse(new GetJobsAction.Response(new QueryPage<>(Collections.singletonList(job), 1, new ParseField(""))))) - .when(client).execute(same(GetJobsAction.INSTANCE), any(), any()); - doAnswer(withResponse(new ListTasksResponse(Collections.emptyList(), Collections.emptyList(), Collections.emptyList()))) - .when(client).execute(same(ListTasksAction.INSTANCE), any(), any()); - doAnswer(withResponse(AcknowledgedResponse.of(deleted))) - .when(client).execute(same(DeleteJobAction.INSTANCE), any(), any()); + doAnswer(withResponse(new GetJobsAction.Response(new QueryPage<>(Collections.singletonList(job), 1, new ParseField(""))))).when( + client + ).execute(same(GetJobsAction.INSTANCE), any(), any()); + doAnswer(withResponse(new ListTasksResponse(Collections.emptyList(), Collections.emptyList(), Collections.emptyList()))).when( + client + ).execute(same(ListTasksAction.INSTANCE), any(), any()); + doAnswer(withResponse(AcknowledgedResponse.of(deleted))).when(client).execute(same(DeleteJobAction.INSTANCE), any(), any()); CountDownLatch latch = new CountDownLatch(2); try (MlDailyMaintenanceService service = createService(latch, client)) { @@ -232,24 +240,26 @@ private void testJobInDeletingStateDoesNotHaveDeletionTask(boolean deleted) thro private MlDailyMaintenanceService createService(CountDownLatch latch, Client client) { return new MlDailyMaintenanceService(Settings.EMPTY, threadPool, client, clusterService, mlAssignmentNotifier, () -> { - // We need to be careful that an unexpected iteration doesn't get squeezed in by the maintenance threadpool in - // between the latch getting counted down to zero and the main test thread stopping the maintenance service. - // This could happen if the main test thread happens to be waiting for a CPU for the whole 100ms after the - // latch counts down to zero. - if (latch.getCount() > 0) { - latch.countDown(); - return TimeValue.timeValueMillis(100); - } else { - return TimeValue.timeValueHours(1); - } - }); + // We need to be careful that an unexpected iteration doesn't get squeezed in by the maintenance threadpool in + // between the latch getting counted down to zero and the main test thread stopping the maintenance service. + // This could happen if the main test thread happens to be waiting for a CPU for the whole 100ms after the + // latch counts down to zero. + if (latch.getCount() > 0) { + latch.countDown(); + return TimeValue.timeValueMillis(100); + } else { + return TimeValue.timeValueHours(1); + } + }); } private static ClusterState createClusterState(boolean isUpgradeMode) { return ClusterState.builder(new ClusterName("MlDailyMaintenanceServiceTests")) - .metadata(Metadata.builder() - .putCustom(PersistentTasksCustomMetadata.TYPE, PersistentTasksCustomMetadata.builder().build()) - .putCustom(MlMetadata.TYPE, new MlMetadata.Builder().isUpgradeMode(isUpgradeMode).build())) + .metadata( + Metadata.builder() + .putCustom(PersistentTasksCustomMetadata.TYPE, PersistentTasksCustomMetadata.builder().build()) + .putCustom(MlMetadata.TYPE, new MlMetadata.Builder().isUpgradeMode(isUpgradeMode).build()) + ) .nodes(DiscoveryNodes.builder().build()) .build(); } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MlIndexTemplateRegistryTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MlIndexTemplateRegistryTests.java index 36722124d0f01..0ea3326a28127 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MlIndexTemplateRegistryTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MlIndexTemplateRegistryTests.java @@ -21,14 +21,14 @@ import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.node.DiscoveryNodes; import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.CollectionUtils; import org.elasticsearch.common.util.concurrent.EsExecutors; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xpack.core.ilm.LifecycleAction; import org.elasticsearch.xpack.core.ilm.RolloverAction; import org.elasticsearch.xpack.core.ml.MlStatsIndex; @@ -37,8 +37,6 @@ import org.mockito.ArgumentCaptor; import org.mockito.stubbing.Answer; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; import static org.hamcrest.Matchers.equalTo; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyObject; @@ -46,6 +44,8 @@ import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; public class MlIndexTemplateRegistryTests extends ESTestCase { @@ -74,22 +74,35 @@ public void setUpMocks() { clusterService = mock(ClusterService.class); - xContentRegistry = new NamedXContentRegistry(CollectionUtils.appendToCopy(ClusterModule.getNamedXWriteables(), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(RolloverAction.NAME), RolloverAction::parse))); + xContentRegistry = new NamedXContentRegistry( + CollectionUtils.appendToCopy( + ClusterModule.getNamedXWriteables(), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(RolloverAction.NAME), RolloverAction::parse) + ) + ); putIndexTemplateRequestCaptor = ArgumentCaptor.forClass(PutComposableIndexTemplateAction.Request.class); } public void testStateTemplate() { - MlIndexTemplateRegistry registry = - new MlIndexTemplateRegistry(Settings.EMPTY, clusterService, threadPool, client, xContentRegistry); + MlIndexTemplateRegistry registry = new MlIndexTemplateRegistry( + Settings.EMPTY, + clusterService, + threadPool, + client, + xContentRegistry + ); registry.clusterChanged(createClusterChangedEvent(nodes)); - verify(client, times(4)) - .execute(same(PutComposableIndexTemplateAction.INSTANCE), putIndexTemplateRequestCaptor.capture(), anyObject()); + verify(client, times(4)).execute( + same(PutComposableIndexTemplateAction.INSTANCE), + putIndexTemplateRequestCaptor.capture(), + anyObject() + ); - PutComposableIndexTemplateAction.Request req = putIndexTemplateRequestCaptor.getAllValues().stream() + PutComposableIndexTemplateAction.Request req = putIndexTemplateRequestCaptor.getAllValues() + .stream() .filter(r -> r.name().equals(AnomalyDetectorsIndexFields.STATE_INDEX_PREFIX)) .findFirst() .orElseThrow(() -> new AssertionError("expected the ml state index template to be put")); @@ -99,15 +112,24 @@ public void testStateTemplate() { } public void testStatsTemplate() { - MlIndexTemplateRegistry registry = - new MlIndexTemplateRegistry(Settings.EMPTY, clusterService, threadPool, client, xContentRegistry); + MlIndexTemplateRegistry registry = new MlIndexTemplateRegistry( + Settings.EMPTY, + clusterService, + threadPool, + client, + xContentRegistry + ); registry.clusterChanged(createClusterChangedEvent(nodes)); - verify(client, times(4)) - .execute(same(PutComposableIndexTemplateAction.INSTANCE), putIndexTemplateRequestCaptor.capture(), anyObject()); + verify(client, times(4)).execute( + same(PutComposableIndexTemplateAction.INSTANCE), + putIndexTemplateRequestCaptor.capture(), + anyObject() + ); - PutComposableIndexTemplateAction.Request req = putIndexTemplateRequestCaptor.getAllValues().stream() + PutComposableIndexTemplateAction.Request req = putIndexTemplateRequestCaptor.getAllValues() + .stream() .filter(r -> r.name().equals(MlStatsIndex.TEMPLATE_NAME)) .findFirst() .orElseThrow(() -> new AssertionError("expected the ml stats index template to be put")); @@ -129,6 +151,7 @@ private static ClusterChangedEvent createClusterChangedEvent(DiscoveryNodes node return new ClusterChangedEvent( "created-from-test", ClusterState.builder(new ClusterName("test")).nodes(nodes).build(), - ClusterState.builder(new ClusterName("test")).build()); + ClusterState.builder(new ClusterName("test")).build() + ); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MlInitializationServiceTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MlInitializationServiceTests.java index 0558a83ceff47..9b55cafcd75de 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MlInitializationServiceTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MlInitializationServiceTests.java @@ -23,10 +23,10 @@ import java.util.concurrent.ExecutorService; -import static org.mockito.Mockito.doAnswer; import static org.hamcrest.Matchers.is; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -76,15 +76,25 @@ public void setUpMocks() { } public void testInitialize() { - MlInitializationService initializationService = - new MlInitializationService(Settings.EMPTY, threadPool, clusterService, client, mlAssignmentNotifier); + MlInitializationService initializationService = new MlInitializationService( + Settings.EMPTY, + threadPool, + clusterService, + client, + mlAssignmentNotifier + ); initializationService.onMaster(); assertThat(initializationService.getDailyMaintenanceService().isStarted(), is(true)); } public void testInitialize_noMasterNode() { - MlInitializationService initializationService = - new MlInitializationService(Settings.EMPTY, threadPool, clusterService, client, mlAssignmentNotifier); + MlInitializationService initializationService = new MlInitializationService( + Settings.EMPTY, + threadPool, + clusterService, + client, + mlAssignmentNotifier + ); initializationService.offMaster(); assertThat(initializationService.getDailyMaintenanceService().isStarted(), is(false)); } @@ -92,8 +102,12 @@ public void testInitialize_noMasterNode() { public void testNodeGoesFromMasterToNonMasterAndBack() { MlDailyMaintenanceService initialDailyMaintenanceService = mock(MlDailyMaintenanceService.class); - MlInitializationService initializationService = - new MlInitializationService(client, threadPool, initialDailyMaintenanceService, clusterService); + MlInitializationService initializationService = new MlInitializationService( + client, + threadPool, + initialDailyMaintenanceService, + clusterService + ); initializationService.offMaster(); verify(initialDailyMaintenanceService).stop(); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MlLifeCycleServiceTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MlLifeCycleServiceTests.java index 55ee77165c651..3d22715b477bf 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MlLifeCycleServiceTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MlLifeCycleServiceTests.java @@ -74,17 +74,30 @@ public void setupMocks() { public void testIsNodeSafeToShutdown() { PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); - tasksBuilder.addTask(MlTasks.jobTaskId("job-1"), MlTasks.JOB_TASK_NAME, new OpenJobAction.JobParams("job-1"), - new PersistentTasksCustomMetadata.Assignment("node-1", "test assignment")); - tasksBuilder.addTask(MlTasks.datafeedTaskId("df1"), MlTasks.DATAFEED_TASK_NAME, + tasksBuilder.addTask( + MlTasks.jobTaskId("job-1"), + MlTasks.JOB_TASK_NAME, + new OpenJobAction.JobParams("job-1"), + new PersistentTasksCustomMetadata.Assignment("node-1", "test assignment") + ); + tasksBuilder.addTask( + MlTasks.datafeedTaskId("df1"), + MlTasks.DATAFEED_TASK_NAME, new StartDatafeedAction.DatafeedParams("df1", 0L), - new PersistentTasksCustomMetadata.Assignment("node-1", "test assignment")); - tasksBuilder.addTask(MlTasks.dataFrameAnalyticsTaskId("job-2"), MlTasks.DATA_FRAME_ANALYTICS_TASK_NAME, + new PersistentTasksCustomMetadata.Assignment("node-1", "test assignment") + ); + tasksBuilder.addTask( + MlTasks.dataFrameAnalyticsTaskId("job-2"), + MlTasks.DATA_FRAME_ANALYTICS_TASK_NAME, new StartDataFrameAnalyticsAction.TaskParams("foo-2", Version.CURRENT, true), - new PersistentTasksCustomMetadata.Assignment("node-2", "test assignment")); - tasksBuilder.addTask(MlTasks.snapshotUpgradeTaskId("job-3", "snapshot-3"), MlTasks.JOB_SNAPSHOT_UPGRADE_TASK_NAME, + new PersistentTasksCustomMetadata.Assignment("node-2", "test assignment") + ); + tasksBuilder.addTask( + MlTasks.snapshotUpgradeTaskId("job-3", "snapshot-3"), + MlTasks.JOB_SNAPSHOT_UPGRADE_TASK_NAME, new SnapshotUpgradeTaskParams("job-3", "snapshot-3"), - new PersistentTasksCustomMetadata.Assignment("node-3", "test assignment")); + new PersistentTasksCustomMetadata.Assignment("node-3", "test assignment") + ); Metadata metadata = Metadata.builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build()).build(); ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE).metadata(metadata).build(); @@ -122,19 +135,33 @@ public void testIsNodeSafeToShutdown() { public void testIsNodeSafeToShutdownGivenFailedTasks() { PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); - tasksBuilder.addTask(MlTasks.jobTaskId("job-1"), MlTasks.JOB_TASK_NAME, new OpenJobAction.JobParams("job-1"), - new PersistentTasksCustomMetadata.Assignment("node-1", "test assignment")); + tasksBuilder.addTask( + MlTasks.jobTaskId("job-1"), + MlTasks.JOB_TASK_NAME, + new OpenJobAction.JobParams("job-1"), + new PersistentTasksCustomMetadata.Assignment("node-1", "test assignment") + ); tasksBuilder.updateTaskState(MlTasks.jobTaskId("job-1"), new JobTaskState(JobState.FAILED, 1, "testing")); - tasksBuilder.addTask(MlTasks.dataFrameAnalyticsTaskId("job-2"), MlTasks.DATA_FRAME_ANALYTICS_TASK_NAME, + tasksBuilder.addTask( + MlTasks.dataFrameAnalyticsTaskId("job-2"), + MlTasks.DATA_FRAME_ANALYTICS_TASK_NAME, new StartDataFrameAnalyticsAction.TaskParams("foo-2", Version.CURRENT, true), - new PersistentTasksCustomMetadata.Assignment("node-2", "test assignment")); - tasksBuilder.updateTaskState(MlTasks.dataFrameAnalyticsTaskId("job-2"), - new DataFrameAnalyticsTaskState(DataFrameAnalyticsState.FAILED, 2, "testing")); - tasksBuilder.addTask(MlTasks.snapshotUpgradeTaskId("job-3", "snapshot-3"), MlTasks.JOB_SNAPSHOT_UPGRADE_TASK_NAME, + new PersistentTasksCustomMetadata.Assignment("node-2", "test assignment") + ); + tasksBuilder.updateTaskState( + MlTasks.dataFrameAnalyticsTaskId("job-2"), + new DataFrameAnalyticsTaskState(DataFrameAnalyticsState.FAILED, 2, "testing") + ); + tasksBuilder.addTask( + MlTasks.snapshotUpgradeTaskId("job-3", "snapshot-3"), + MlTasks.JOB_SNAPSHOT_UPGRADE_TASK_NAME, new SnapshotUpgradeTaskParams("job-3", "snapshot-3"), - new PersistentTasksCustomMetadata.Assignment("node-3", "test assignment")); - tasksBuilder.updateTaskState(MlTasks.snapshotUpgradeTaskId("job-3", "snapshot-3"), - new SnapshotUpgradeTaskState(SnapshotUpgradeState.FAILED, 3, "testing")); + new PersistentTasksCustomMetadata.Assignment("node-3", "test assignment") + ); + tasksBuilder.updateTaskState( + MlTasks.snapshotUpgradeTaskId("job-3", "snapshot-3"), + new SnapshotUpgradeTaskState(SnapshotUpgradeState.FAILED, 3, "testing") + ); Metadata metadata = Metadata.builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build()).build(); ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE).metadata(metadata).build(); @@ -150,22 +177,53 @@ public void testIsNodeSafeToShutdownGivenFailedTasks() { public void testSignalGracefulShutdownIncludingLocalNode() { - MlLifeCycleService mlLifeCycleService = new MlLifeCycleService(clusterService, datafeedRunner, mlController, - autodetectProcessManager, analyticsManager, memoryTracker); + MlLifeCycleService mlLifeCycleService = new MlLifeCycleService( + clusterService, + datafeedRunner, + mlController, + autodetectProcessManager, + analyticsManager, + memoryTracker + ); DiscoveryNodes.Builder nodesBuilder = DiscoveryNodes.builder() - .add(new DiscoveryNode("node-1-name", "node-1", new TransportAddress(InetAddress.getLoopbackAddress(), 9300), - Collections.emptyMap(), DiscoveryNodeRole.roles(), Version.CURRENT)) - .add(new DiscoveryNode("node-2-name", "node-2", new TransportAddress(InetAddress.getLoopbackAddress(), 9301), - Collections.emptyMap(), DiscoveryNodeRole.roles(), Version.CURRENT)) - .add(new DiscoveryNode("node-3-name", "node-3", new TransportAddress(InetAddress.getLoopbackAddress(), 9302), - Collections.emptyMap(), DiscoveryNodeRole.roles(), Version.CURRENT)) + .add( + new DiscoveryNode( + "node-1-name", + "node-1", + new TransportAddress(InetAddress.getLoopbackAddress(), 9300), + Collections.emptyMap(), + DiscoveryNodeRole.roles(), + Version.CURRENT + ) + ) + .add( + new DiscoveryNode( + "node-2-name", + "node-2", + new TransportAddress(InetAddress.getLoopbackAddress(), 9301), + Collections.emptyMap(), + DiscoveryNodeRole.roles(), + Version.CURRENT + ) + ) + .add( + new DiscoveryNode( + "node-3-name", + "node-3", + new TransportAddress(InetAddress.getLoopbackAddress(), 9302), + Collections.emptyMap(), + DiscoveryNodeRole.roles(), + Version.CURRENT + ) + ) .masterNodeId("node-1") .localNodeId("node-2"); ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE).nodes(nodesBuilder).build(); - Collection shutdownNodeIds = - randomBoolean() ? Collections.singleton("node-2") : Arrays.asList("node-1", "node-2", "node-3"); + Collection shutdownNodeIds = randomBoolean() + ? Collections.singleton("node-2") + : Arrays.asList("node-1", "node-2", "node-3"); final Clock clock = Clock.fixed(Instant.now(), ZoneId.systemDefault()); mlLifeCycleService.signalGracefulShutdown(clusterState, shutdownNodeIds, clock); @@ -183,21 +241,50 @@ public void testSignalGracefulShutdownIncludingLocalNode() { public void testSignalGracefulShutdownExcludingLocalNode() { - MlLifeCycleService mlLifeCycleService = new MlLifeCycleService(clusterService, datafeedRunner, mlController, - autodetectProcessManager, analyticsManager, memoryTracker); + MlLifeCycleService mlLifeCycleService = new MlLifeCycleService( + clusterService, + datafeedRunner, + mlController, + autodetectProcessManager, + analyticsManager, + memoryTracker + ); DiscoveryNodes.Builder nodesBuilder = DiscoveryNodes.builder() - .add(new DiscoveryNode("node-1-name", "node-1", new TransportAddress(InetAddress.getLoopbackAddress(), 9300), - Collections.emptyMap(), DiscoveryNodeRole.roles(), Version.CURRENT)) - .add(new DiscoveryNode("node-2-name", "node-2", new TransportAddress(InetAddress.getLoopbackAddress(), 9301), - Collections.emptyMap(), DiscoveryNodeRole.roles(), Version.CURRENT)) - .add(new DiscoveryNode("node-3-name", "node-3", new TransportAddress(InetAddress.getLoopbackAddress(), 9302), - Collections.emptyMap(), DiscoveryNodeRole.roles(), Version.CURRENT)) + .add( + new DiscoveryNode( + "node-1-name", + "node-1", + new TransportAddress(InetAddress.getLoopbackAddress(), 9300), + Collections.emptyMap(), + DiscoveryNodeRole.roles(), + Version.CURRENT + ) + ) + .add( + new DiscoveryNode( + "node-2-name", + "node-2", + new TransportAddress(InetAddress.getLoopbackAddress(), 9301), + Collections.emptyMap(), + DiscoveryNodeRole.roles(), + Version.CURRENT + ) + ) + .add( + new DiscoveryNode( + "node-3-name", + "node-3", + new TransportAddress(InetAddress.getLoopbackAddress(), 9302), + Collections.emptyMap(), + DiscoveryNodeRole.roles(), + Version.CURRENT + ) + ) .masterNodeId("node-1") .localNodeId("node-2"); ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE).nodes(nodesBuilder).build(); - Collection shutdownNodeIds = - randomBoolean() ? Collections.singleton("node-1") : Arrays.asList("node-1", "node-3"); + Collection shutdownNodeIds = randomBoolean() ? Collections.singleton("node-1") : Arrays.asList("node-1", "node-3"); mlLifeCycleService.signalGracefulShutdown(clusterState, shutdownNodeIds, Clock.systemUTC()); assertThat(mlLifeCycleService.getShutdownStartTime(), nullValue()); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MlMetadataTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MlMetadataTests.java index ed8135f8763aa..68157686d5353 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MlMetadataTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MlMetadataTests.java @@ -10,10 +10,10 @@ import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.SearchModule; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.MlMetadata; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfig; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfigTests; @@ -31,9 +31,9 @@ import static org.elasticsearch.xpack.core.ml.job.config.JobTests.buildJobBuilder; import static org.elasticsearch.xpack.ml.datafeed.DatafeedRunnerTests.createDatafeedConfig; import static org.hamcrest.Matchers.allOf; -import static org.hamcrest.Matchers.hasKey; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasKey; import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.Matchers.sameInstance; @@ -51,7 +51,9 @@ protected MlMetadata createTestInstance() { AnalysisConfig.Builder analysisConfig = new AnalysisConfig.Builder(job.getAnalysisConfig()); analysisConfig.setLatency(null); DatafeedConfig datafeedConfig = DatafeedConfigTests.createRandomizedDatafeedConfig( - job.getId(), job.getAnalysisConfig().getBucketSpan().millis()); + job.getId(), + job.getAnalysisConfig().getBucketSpan().millis() + ); if (datafeedConfig.hasAggregations()) { analysisConfig.setSummaryCountFieldName("doc_count"); } @@ -188,33 +190,38 @@ protected MlMetadata mutateInstance(MlMetadata instance) { } switch (between(0, 3)) { - case 0: - metadataBuilder.putJob(JobTests.createRandomizedJob(), true); - break; - case 1: - // Because we check if the job for the datafeed exists and we don't - // allow two datafeeds to exist for a single job we have to add both - // a job and a datafeed here - Job randomJob = JobTests.createRandomizedJob(); - AnalysisConfig.Builder analysisConfig = new AnalysisConfig.Builder(randomJob.getAnalysisConfig()); - analysisConfig.setLatency(null); - DatafeedConfig datafeedConfig = DatafeedConfigTests.createRandomizedDatafeedConfig(randomJob.getId(), - randomJob.getAnalysisConfig().getBucketSpan().millis()); - if (datafeedConfig.hasAggregations()) { - analysisConfig.setSummaryCountFieldName("doc_count"); - } - randomJob = new Job.Builder(randomJob).setAnalysisConfig(analysisConfig).setDeleting(false).setBlocked(Blocked.none()).build(); - metadataBuilder.putJob(randomJob, false); - metadataBuilder.putDatafeed(datafeedConfig, Collections.emptyMap(), xContentRegistry()); - break; - case 2: - metadataBuilder.isUpgradeMode(isUpgrade == false); - break; - case 3: - metadataBuilder.isResetMode(isReset == false); - break; - default: - throw new AssertionError("Illegal randomisation branch"); + case 0: + metadataBuilder.putJob(JobTests.createRandomizedJob(), true); + break; + case 1: + // Because we check if the job for the datafeed exists and we don't + // allow two datafeeds to exist for a single job we have to add both + // a job and a datafeed here + Job randomJob = JobTests.createRandomizedJob(); + AnalysisConfig.Builder analysisConfig = new AnalysisConfig.Builder(randomJob.getAnalysisConfig()); + analysisConfig.setLatency(null); + DatafeedConfig datafeedConfig = DatafeedConfigTests.createRandomizedDatafeedConfig( + randomJob.getId(), + randomJob.getAnalysisConfig().getBucketSpan().millis() + ); + if (datafeedConfig.hasAggregations()) { + analysisConfig.setSummaryCountFieldName("doc_count"); + } + randomJob = new Job.Builder(randomJob).setAnalysisConfig(analysisConfig) + .setDeleting(false) + .setBlocked(Blocked.none()) + .build(); + metadataBuilder.putJob(randomJob, false); + metadataBuilder.putDatafeed(datafeedConfig, Collections.emptyMap(), xContentRegistry()); + break; + case 2: + metadataBuilder.isUpgradeMode(isUpgrade == false); + break; + case 3: + metadataBuilder.isResetMode(isReset == false); + break; + default: + throw new AssertionError("Illegal randomisation branch"); } return metadataBuilder.build(); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MlSingleNodeTestCase.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MlSingleNodeTestCase.java index b19b317264387..8a41701a9b641 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MlSingleNodeTestCase.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MlSingleNodeTestCase.java @@ -13,11 +13,10 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.reindex.ReindexPlugin; import org.elasticsearch.ingest.common.IngestCommonPlugin; import org.elasticsearch.license.LicenseService; import org.elasticsearch.plugins.Plugin; +import org.elasticsearch.reindex.ReindexPlugin; import org.elasticsearch.script.IngestScript; import org.elasticsearch.script.MockDeterministicScript; import org.elasticsearch.script.MockScriptEngine; @@ -28,6 +27,7 @@ import org.elasticsearch.search.SearchModule; import org.elasticsearch.test.ESSingleNodeTestCase; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.xpack.core.ilm.LifecycleSettings; import org.elasticsearch.xpack.core.ml.MachineLearningField; @@ -96,7 +96,8 @@ protected Collection> getPlugins() { IngestCommonPlugin.class, MockPainlessScriptEngine.TestPlugin.class, // ILM is required for .ml-state template index settings - IndexLifecycle.class); + IndexLifecycle.class + ); } @Override @@ -113,24 +114,20 @@ protected void waitForMlTemplates() throws Exception { // block until the templates are installed assertBusy(() -> { ClusterState state = client().admin().cluster().prepareState().get().getState(); - assertTrue("Timed out waiting for the ML templates to be installed", - MachineLearning.criticalTemplatesInstalled(state)); + assertTrue("Timed out waiting for the ML templates to be installed", MachineLearning.criticalTemplatesInstalled(state)); }); } - protected void blockingCall(Consumer> function, AtomicReference response, - AtomicReference error) throws InterruptedException { + protected void blockingCall(Consumer> function, AtomicReference response, AtomicReference error) + throws InterruptedException { CountDownLatch latch = new CountDownLatch(1); - ActionListener listener = ActionListener.wrap( - r -> { - response.set(r); - latch.countDown(); - }, - e -> { - error.set(e); - latch.countDown(); - } - ); + ActionListener listener = ActionListener.wrap(r -> { + response.set(r); + latch.countDown(); + }, e -> { + error.set(e); + latch.countDown(); + }); function.accept(listener); latch.await(); @@ -157,13 +154,10 @@ protected static ThreadPool mockThreadPool() { doAnswer(invocationOnMock -> { ((Runnable) invocationOnMock.getArguments()[0]).run(); return null; - }).when(tp).schedule( - any(Runnable.class), any(TimeValue.class), any(String.class) - ); + }).when(tp).schedule(any(Runnable.class), any(TimeValue.class), any(String.class)); return tp; } - public static void assertNoException(AtomicReference error) throws Exception { if (error.get() == null) { return; @@ -200,8 +194,7 @@ public T compile(String name, String script, ScriptContext context, Map new IngestScript(vars) { @Override - public void execute(Map ctx) { - } + public void execute(Map ctx) {} }; return context.factoryClazz.cast(factory); } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MlUpgradeModeActionFilterTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MlUpgradeModeActionFilterTests.java index 3b11c063f7ffb..7ecf98cd7a6dd 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MlUpgradeModeActionFilterTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MlUpgradeModeActionFilterTests.java @@ -72,10 +72,10 @@ public void testApply_ActionDisallowedInUpgradeMode() { filter.apply(task, action, request, listener, chain); filter.setUpgradeResetFlags(createClusterChangedEvent(createClusterState(true, false))); - ElasticsearchStatusException e = - expectThrows( - ElasticsearchStatusException.class, - () -> filter.apply(task, action, request, listener, chain)); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> filter.apply(task, action, request, listener, chain) + ); filter.setUpgradeResetFlags(createClusterChangedEvent(createClusterState(false, false))); filter.apply(task, action, request, listener, chain); @@ -126,8 +126,10 @@ private static ClusterChangedEvent createClusterChangedEvent(ClusterState cluste private static ClusterState createClusterState(boolean isUpgradeMode, boolean isResetMode) { return ClusterState.builder(new ClusterName("MlUpgradeModeActionFilterTests")) - .metadata(Metadata.builder().putCustom(MlMetadata.TYPE, - new MlMetadata.Builder().isUpgradeMode(isUpgradeMode).isResetMode(isResetMode).build())) + .metadata( + Metadata.builder() + .putCustom(MlMetadata.TYPE, new MlMetadata.Builder().isUpgradeMode(isUpgradeMode).isResetMode(isResetMode).build()) + ) .build(); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportCloseJobActionTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportCloseJobActionTests.java index c636d8af64bf9..1ba8759cd8ef8 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportCloseJobActionTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportCloseJobActionTests.java @@ -89,14 +89,14 @@ public void testAddJobAccordingToState() { List closingJobIds = new ArrayList<>(); List failedJobIds = new ArrayList<>(); - PersistentTasksCustomMetadata.Builder taskBuilder = PersistentTasksCustomMetadata.builder(); + PersistentTasksCustomMetadata.Builder taskBuilder = PersistentTasksCustomMetadata.builder(); addJobTask("open-job", null, JobState.OPENED, taskBuilder); addJobTask("failed-job", null, JobState.FAILED, taskBuilder); addJobTask("closing-job", null, JobState.CLOSING, taskBuilder); addJobTask("opening-job", null, JobState.OPENING, taskBuilder); PersistentTasksCustomMetadata tasks = taskBuilder.build(); - for (String id : new String [] {"open-job", "closing-job", "opening-job", "failed-job"}) { + for (String id : new String[] { "open-job", "closing-job", "opening-job", "failed-job" }) { TransportCloseJobAction.addJobAccordingToState(id, tasks, openJobIds, closingJobIds, failedJobIds); } assertThat(openJobIds, containsInAnyOrder("open-job", "opening-job")); @@ -192,10 +192,7 @@ public void testValidate_givenFailedJob() { AtomicReference exceptionHolder = new AtomicReference<>(); AtomicReference responseHolder = new AtomicReference<>(); - ActionListener listener = ActionListener.wrap( - responseHolder::set, - exceptionHolder::set - ); + ActionListener listener = ActionListener.wrap(responseHolder::set, exceptionHolder::set); // force close so not an error for the failed job closeJobAction.validate(Collections.singletonList("job_id_failed"), true, tasksBuilder.build(), listener); @@ -216,7 +213,7 @@ public void testValidate_givenFailedJob() { } public void testValidate_withSpecificJobIds() { - PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); + PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); addJobTask("job_id_closing", null, JobState.CLOSING, tasksBuilder); addJobTask("job_id_open-1", null, JobState.OPENED, tasksBuilder); addJobTask("job_id_open-2", null, JobState.OPENED, tasksBuilder); @@ -226,10 +223,7 @@ public void testValidate_withSpecificJobIds() { AtomicReference exceptionHolder = new AtomicReference<>(); AtomicReference responseHolder = new AtomicReference<>(); - ActionListener listener = ActionListener.wrap( - responseHolder::set, - exceptionHolder::set - ); + ActionListener listener = ActionListener.wrap(responseHolder::set, exceptionHolder::set); TransportCloseJobAction closeJobAction = createAction(); closeJobAction.validate(Arrays.asList("job_id_closing", "job_id_open-1", "job_id_open-2"), false, tasks, listener); @@ -261,12 +255,12 @@ public void testDoExecute_whenNothingToClose() { MlMetadata.Builder mlBuilder = new MlMetadata.Builder(); mlBuilder.putJob(BaseMlIntegTestCase.createFareQuoteJob("foo").build(new Date()), false); - PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); + PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); addJobTask("foo", null, JobState.CLOSED, tasksBuilder); ClusterState clusterState = ClusterState.builder(new ClusterName("_name")) - .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())) - .build(); + .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())) + .build(); TransportCloseJobAction transportAction = createAction(); when(clusterService.state()).thenReturn(clusterState); @@ -300,37 +294,65 @@ public void testBuildWaitForCloseRequest() { List openJobIds = Arrays.asList("openjob1", "openjob2"); List closingJobIds = Collections.singletonList("closingjob1"); - PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); + PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); addJobTask("openjob1", null, JobState.OPENED, tasksBuilder); addJobTask("openjob2", null, JobState.OPENED, tasksBuilder); addJobTask("closingjob1", null, JobState.CLOSING, tasksBuilder); - TransportCloseJobAction.WaitForCloseRequest waitForCloseRequest = - TransportCloseJobAction.buildWaitForCloseRequest( - openJobIds, closingJobIds, tasksBuilder.build(), mock(AnomalyDetectionAuditor.class)); + TransportCloseJobAction.WaitForCloseRequest waitForCloseRequest = TransportCloseJobAction.buildWaitForCloseRequest( + openJobIds, + closingJobIds, + tasksBuilder.build(), + mock(AnomalyDetectionAuditor.class) + ); assertEquals(waitForCloseRequest.jobsToFinalize, Arrays.asList("openjob1", "openjob2")); - assertThat(waitForCloseRequest.persistentTasks, containsInAnyOrder( - hasProperty("id", equalTo("job-openjob1")), - hasProperty("id", equalTo("job-openjob2")), - hasProperty("id", equalTo("job-closingjob1")))); + assertThat( + waitForCloseRequest.persistentTasks, + containsInAnyOrder( + hasProperty("id", equalTo("job-openjob1")), + hasProperty("id", equalTo("job-openjob2")), + hasProperty("id", equalTo("job-closingjob1")) + ) + ); assertTrue(waitForCloseRequest.hasJobsToWaitFor()); - waitForCloseRequest = TransportCloseJobAction.buildWaitForCloseRequest(Collections.emptyList(), Collections.emptyList(), - tasksBuilder.build(), mock(AnomalyDetectionAuditor.class)); + waitForCloseRequest = TransportCloseJobAction.buildWaitForCloseRequest( + Collections.emptyList(), + Collections.emptyList(), + tasksBuilder.build(), + mock(AnomalyDetectionAuditor.class) + ); assertFalse(waitForCloseRequest.hasJobsToWaitFor()); } - public static void addTask(String datafeedId, long startTime, String nodeId, DatafeedState state, - PersistentTasksCustomMetadata.Builder tasks) { - tasks.addTask(MlTasks.datafeedTaskId(datafeedId), MlTasks.DATAFEED_TASK_NAME, - new StartDatafeedAction.DatafeedParams(datafeedId, startTime), new Assignment(nodeId, "test assignment")); + public static void addTask( + String datafeedId, + long startTime, + String nodeId, + DatafeedState state, + PersistentTasksCustomMetadata.Builder tasks + ) { + tasks.addTask( + MlTasks.datafeedTaskId(datafeedId), + MlTasks.DATAFEED_TASK_NAME, + new StartDatafeedAction.DatafeedParams(datafeedId, startTime), + new Assignment(nodeId, "test assignment") + ); tasks.updateTaskState(MlTasks.datafeedTaskId(datafeedId), state); } private TransportCloseJobAction createAction() { - return new TransportCloseJobAction(mock(TransportService.class), client, mock(ThreadPool.class), - mock(ActionFilters.class), clusterService, mock(AnomalyDetectionAuditor.class), mock(PersistentTasksService.class), - jobConfigProvider, datafeedConfigProvider); + return new TransportCloseJobAction( + mock(TransportService.class), + client, + mock(ThreadPool.class), + mock(ActionFilters.class), + clusterService, + mock(AnomalyDetectionAuditor.class), + mock(PersistentTasksService.class), + jobConfigProvider, + datafeedConfigProvider + ); } @SuppressWarnings("unchecked") @@ -356,8 +378,8 @@ private void mockJobConfigProviderExpandIds(Set expandedIds) { @SuppressWarnings("unchecked") private void mockClientStopDatafeed() { doAnswer(invocation -> { - ActionListener listener = - (ActionListener) invocation.getArguments()[2]; + ActionListener listener = (ActionListener) invocation + .getArguments()[2]; listener.onResponse(new StopDatafeedAction.Response(true)); return null; @@ -367,8 +389,8 @@ private void mockClientStopDatafeed() { @SuppressWarnings("unchecked") private void mockClientIsolateDatafeed() { doAnswer(invocation -> { - ActionListener listener = - (ActionListener) invocation.getArguments()[2]; + ActionListener listener = (ActionListener) invocation + .getArguments()[2]; listener.onResponse(new IsolateDatafeedAction.Response(true)); return null; diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportDeleteExpiredDataActionTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportDeleteExpiredDataActionTests.java index 938c667893c8f..c49ab9a5e324f 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportDeleteExpiredDataActionTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportDeleteExpiredDataActionTests.java @@ -49,11 +49,7 @@ public class TransportDeleteExpiredDataActionTests extends ESTestCase { */ private static class DummyDataRemover implements MlDataRemover { - public void remove( - float requestsPerSec, - ActionListener listener, - BooleanSupplier isTimedOutSupplier - ) { + public void remove(float requestsPerSec, ActionListener listener, BooleanSupplier isTimedOutSupplier) { listener.onResponse(isTimedOutSupplier.getAsBoolean() == false); } } @@ -65,10 +61,18 @@ public void setup() { Client client = mock(Client.class); ClusterService clusterService = mock(ClusterService.class); auditor = mock(AnomalyDetectionAuditor.class); - transportDeleteExpiredDataAction = new TransportDeleteExpiredDataAction(threadPool, ThreadPool.Names.SAME, transportService, - new ActionFilters(Collections.emptySet()), client, clusterService, mock(JobConfigProvider.class), - mock(JobResultsProvider.class), auditor, - Clock.systemUTC()); + transportDeleteExpiredDataAction = new TransportDeleteExpiredDataAction( + threadPool, + ThreadPool.Names.SAME, + transportService, + new ActionFilters(Collections.emptySet()), + client, + clusterService, + mock(JobConfigProvider.class), + mock(JobResultsProvider.class), + auditor, + Clock.systemUTC() + ); } @After @@ -116,10 +120,12 @@ public void testDeleteExpiredDataIterationWithTimeout() { transportDeleteExpiredDataAction.deleteExpiredData(request, removers.iterator(), 1.0f, finalListener, isTimedOutSupplier, true); assertFalse(succeeded.get()); - verify(auditor, times(1)).warning("", - "Deleting expired ML data was cancelled after the timeout period of [8h] was exceeded. " + - "The setting [xpack.ml.nightly_maintenance_requests_per_second] " + - "controls the deletion rate, consider increasing the value to assist in pruning old data"); + verify(auditor, times(1)).warning( + "", + "Deleting expired ML data was cancelled after the timeout period of [8h] was exceeded. " + + "The setting [xpack.ml.nightly_maintenance_requests_per_second] " + + "controls the deletion rate, consider increasing the value to assist in pruning old data" + ); verifyNoMoreInteractions(auditor); } @@ -140,7 +146,7 @@ public void testDeleteExpiredDataIterationWithTimeout_GivenJobIds() { DeleteExpiredDataAction.Request request = new DeleteExpiredDataAction.Request(null, null); request.setJobId("foo*"); - request.setExpandedJobIds(new String[] {"foo1", "foo2"}); + request.setExpandedJobIds(new String[] { "foo1", "foo2" }); transportDeleteExpiredDataAction.deleteExpiredData(request, removers.iterator(), 1.0f, finalListener, isTimedOutSupplier, true); assertFalse(succeeded.get()); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportDeleteForecastActionTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportDeleteForecastActionTests.java index f7d1cad0ae36c..7171794b12316 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportDeleteForecastActionTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportDeleteForecastActionTests.java @@ -12,6 +12,7 @@ import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.ml.job.config.JobState; import org.elasticsearch.xpack.core.ml.job.results.ForecastRequestStats; + import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -19,7 +20,6 @@ import java.util.stream.Collectors; import java.util.stream.Stream; - public class TransportDeleteForecastActionTests extends ESTestCase { private static final int TEST_RUNS = 10; @@ -27,29 +27,25 @@ public class TransportDeleteForecastActionTests extends ESTestCase { public void testValidateForecastStateWithAllFailedFinished() { for (int i = 0; i < TEST_RUNS; ++i) { List forecastRequestStatsHits = Stream.generate( - () -> createForecastStatsHit(randomFrom( - ForecastRequestStats.ForecastRequestStatus.FAILED, - ForecastRequestStats.ForecastRequestStatus.FINISHED - ))) - .limit(randomInt(10)) - .collect(Collectors.toList()); + () -> createForecastStatsHit( + randomFrom(ForecastRequestStats.ForecastRequestStatus.FAILED, ForecastRequestStats.ForecastRequestStatus.FINISHED) + ) + ).limit(randomInt(10)).collect(Collectors.toList()); // This should not throw. TransportDeleteForecastAction.extractForecastIds( forecastRequestStatsHits.toArray(new SearchHit[0]), randomFrom(JobState.values()), - randomAlphaOfLength(10)); + randomAlphaOfLength(10) + ); } } public void testValidateForecastStateWithSomeFailedFinished() { for (int i = 0; i < TEST_RUNS; ++i) { List forecastRequestStatsHits = Stream.generate( - () -> createForecastStatsHit(randomFrom( - ForecastRequestStats.ForecastRequestStatus.values() - ))) - .limit(randomInt(10)) - .collect(Collectors.toList()); + () -> createForecastStatsHit(randomFrom(ForecastRequestStats.ForecastRequestStatus.values())) + ).limit(randomInt(10)).collect(Collectors.toList()); forecastRequestStatsHits.add(createForecastStatsHit(ForecastRequestStats.ForecastRequestStatus.STARTED)); @@ -59,7 +55,8 @@ public void testValidateForecastStateWithSomeFailedFinished() { TransportDeleteForecastAction.extractForecastIds( forecastRequestStatsHits.toArray(new SearchHit[0]), jobState, - randomAlphaOfLength(10)); + randomAlphaOfLength(10) + ); } catch (Exception ex) { fail("Should not have thrown: " + ex.getMessage()); } @@ -71,21 +68,23 @@ public void testValidateForecastStateWithSomeFailedFinished() { () -> TransportDeleteForecastAction.extractForecastIds( forecastRequestStatsHits.toArray(new SearchHit[0]), jobState, - randomAlphaOfLength(10)) + randomAlphaOfLength(10) + ) ); } } } - private static SearchHit createForecastStatsHit(ForecastRequestStats.ForecastRequestStatus status) { Map documentFields = new HashMap<>(2); documentFields.put( ForecastRequestStats.FORECAST_ID.getPreferredName(), - new DocumentField(ForecastRequestStats.FORECAST_ID.getPreferredName(), Collections.singletonList(""))); + new DocumentField(ForecastRequestStats.FORECAST_ID.getPreferredName(), Collections.singletonList("")) + ); documentFields.put( ForecastRequestStats.STATUS.getPreferredName(), - new DocumentField(ForecastRequestStats.STATUS.getPreferredName(), Collections.singletonList(status.toString()))); + new DocumentField(ForecastRequestStats.STATUS.getPreferredName(), Collections.singletonList(status.toString())) + ); return new SearchHit(0, "", documentFields, Collections.emptyMap()); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportEstimateModelMemoryActionTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportEstimateModelMemoryActionTests.java index 4382de6079976..4f72e947ae47d 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportEstimateModelMemoryActionTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportEstimateModelMemoryActionTests.java @@ -17,9 +17,9 @@ import java.util.HashMap; import java.util.Map; +import static org.hamcrest.Matchers.closeTo; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.closeTo; import static org.hamcrest.Matchers.lessThan; public class TransportEstimateModelMemoryActionTests extends ESTestCase { @@ -34,20 +34,25 @@ public void testCalculateDetectorRequirementBytes() { String function = randomFrom("mean", "min", "max", "sum"); Detector noSplit = createDetector(function, "field", null, null, null); - assertThat(TransportEstimateModelMemoryAction.calculateDetectorRequirementBytes(noSplit, 900, - overallCardinality), is(49152L)); + assertThat(TransportEstimateModelMemoryAction.calculateDetectorRequirementBytes(noSplit, 900, overallCardinality), is(49152L)); Detector withByField = createDetector(function, "field", "buy", null, null); - assertThat(TransportEstimateModelMemoryAction.calculateDetectorRequirementBytes(withByField, 900, - overallCardinality), is(134 * 49152L)); + assertThat( + TransportEstimateModelMemoryAction.calculateDetectorRequirementBytes(withByField, 900, overallCardinality), + is(134 * 49152L) + ); Detector withPartitionField = createDetector(function, "field", null, null, "part"); - assertThat(TransportEstimateModelMemoryAction.calculateDetectorRequirementBytes(withPartitionField, 900, - overallCardinality), is(100 * 49152L)); + assertThat( + TransportEstimateModelMemoryAction.calculateDetectorRequirementBytes(withPartitionField, 900, overallCardinality), + is(100 * 49152L) + ); Detector withByAndPartitionFields = createDetector(function, "field", "buy", null, "part"); - assertThat(TransportEstimateModelMemoryAction.calculateDetectorRequirementBytes(withByAndPartitionFields, 900, - overallCardinality), is((long) Math.ceil(200 / Math.sqrt(100) * 2 / 3) * 100 * 49152L)); + assertThat( + TransportEstimateModelMemoryAction.calculateDetectorRequirementBytes(withByAndPartitionFields, 900, overallCardinality), + is((long) Math.ceil(200 / Math.sqrt(100) * 2 / 3) * 100 * 49152L) + ); } public void testCalculateInfluencerRequirementBytes() { @@ -58,24 +63,34 @@ public void testCalculateInfluencerRequirementBytes() { maxBucketCardinality.put("inf2", 300L); AnalysisConfig noInfluencers = createCountAnalysisConfig(null, null); - assertThat(TransportEstimateModelMemoryAction.calculateInfluencerRequirementBytes(noInfluencers, - maxBucketCardinality), is(0L)); + assertThat(TransportEstimateModelMemoryAction.calculateInfluencerRequirementBytes(noInfluencers, maxBucketCardinality), is(0L)); AnalysisConfig influencerAlsoPartitionField = createCountAnalysisConfig(null, "part", "part"); - assertThat(TransportEstimateModelMemoryAction.calculateInfluencerRequirementBytes(influencerAlsoPartitionField, - maxBucketCardinality), is(0L)); + assertThat( + TransportEstimateModelMemoryAction.calculateInfluencerRequirementBytes(influencerAlsoPartitionField, maxBucketCardinality), + is(0L) + ); AnalysisConfig influencerNotPartitionField = createCountAnalysisConfig(null, "part", "inf1"); - assertThat(TransportEstimateModelMemoryAction.calculateInfluencerRequirementBytes(influencerNotPartitionField, - maxBucketCardinality), is(200 * TransportEstimateModelMemoryAction.BYTES_PER_INFLUENCER_VALUE)); + assertThat( + TransportEstimateModelMemoryAction.calculateInfluencerRequirementBytes(influencerNotPartitionField, maxBucketCardinality), + is(200 * TransportEstimateModelMemoryAction.BYTES_PER_INFLUENCER_VALUE) + ); AnalysisConfig otherInfluencerAsWellAsPartitionField = createCountAnalysisConfig(null, "part", "part", "inf1"); - assertThat(TransportEstimateModelMemoryAction.calculateInfluencerRequirementBytes(otherInfluencerAsWellAsPartitionField, - maxBucketCardinality), is(200 * TransportEstimateModelMemoryAction.BYTES_PER_INFLUENCER_VALUE)); + assertThat( + TransportEstimateModelMemoryAction.calculateInfluencerRequirementBytes( + otherInfluencerAsWellAsPartitionField, + maxBucketCardinality + ), + is(200 * TransportEstimateModelMemoryAction.BYTES_PER_INFLUENCER_VALUE) + ); AnalysisConfig twoInfluencersNotPartitionField = createCountAnalysisConfig(null, "part", "part", "inf1", "inf2"); - assertThat(TransportEstimateModelMemoryAction.calculateInfluencerRequirementBytes(twoInfluencersNotPartitionField, - maxBucketCardinality), is((200 + 300) * TransportEstimateModelMemoryAction.BYTES_PER_INFLUENCER_VALUE)); + assertThat( + TransportEstimateModelMemoryAction.calculateInfluencerRequirementBytes(twoInfluencersNotPartitionField, maxBucketCardinality), + is((200 + 300) * TransportEstimateModelMemoryAction.BYTES_PER_INFLUENCER_VALUE) + ); } public void testCalculateCategorizationRequirementBytesNoCategorization() { @@ -92,10 +107,11 @@ public void testCalculateCategorizationRequirementBytesSimpleCategorization() { Map overallCardinality = new HashMap<>(); overallCardinality.put("part", randomLongBetween(10, 1000)); - AnalysisConfig analysisConfig = - createCountAnalysisConfig(randomAlphaOfLength(10), randomBoolean() ? "part" : null); - assertThat(TransportEstimateModelMemoryAction.calculateCategorizationRequirementBytes(analysisConfig, overallCardinality), - is(40L * 1024 * 1024)); + AnalysisConfig analysisConfig = createCountAnalysisConfig(randomAlphaOfLength(10), randomBoolean() ? "part" : null); + assertThat( + TransportEstimateModelMemoryAction.calculateCategorizationRequirementBytes(analysisConfig, overallCardinality), + is(40L * 1024 * 1024) + ); } public void testCalculateCategorizationRequirementBytesPerPartitionCategorization() { @@ -106,29 +122,28 @@ public void testCalculateCategorizationRequirementBytesPerPartitionCategorizatio boolean isStopOnWarn = randomBoolean(); AnalysisConfig analysisConfig = createCountAnalysisConfigBuilder(randomAlphaOfLength(10), "part") - .setPerPartitionCategorizationConfig(new PerPartitionCategorizationConfig(true, isStopOnWarn)).build(); - assertThat(TransportEstimateModelMemoryAction.calculateCategorizationRequirementBytes(analysisConfig, overallCardinality), - is(partitionCardinality * 20L * (isStopOnWarn ? 1 : 2) * 1024 * 1024)); + .setPerPartitionCategorizationConfig(new PerPartitionCategorizationConfig(true, isStopOnWarn)) + .build(); + assertThat( + TransportEstimateModelMemoryAction.calculateCategorizationRequirementBytes(analysisConfig, overallCardinality), + is(partitionCardinality * 20L * (isStopOnWarn ? 1 : 2) * 1024 * 1024) + ); } public void testRoundUpToNextMb() { - assertThat(TransportEstimateModelMemoryAction.roundUpToNextMb(0), - equalTo(ByteSizeValue.ofBytes(0))); - assertThat(TransportEstimateModelMemoryAction.roundUpToNextMb(1), - equalTo(ByteSizeValue.ofMb(1))); - assertThat(TransportEstimateModelMemoryAction.roundUpToNextMb(randomIntBetween(1, 1024 * 1024)), - equalTo(ByteSizeValue.ofMb(1))); - assertThat(TransportEstimateModelMemoryAction.roundUpToNextMb(1024 * 1024), - equalTo(ByteSizeValue.ofMb(1))); - assertThat(TransportEstimateModelMemoryAction.roundUpToNextMb(1024 * 1024 + 1), - equalTo(ByteSizeValue.ofMb(2))); - assertThat(TransportEstimateModelMemoryAction.roundUpToNextMb(2 * 1024 * 1024), - equalTo(ByteSizeValue.ofMb(2))); + assertThat(TransportEstimateModelMemoryAction.roundUpToNextMb(0), equalTo(ByteSizeValue.ofBytes(0))); + assertThat(TransportEstimateModelMemoryAction.roundUpToNextMb(1), equalTo(ByteSizeValue.ofMb(1))); + assertThat(TransportEstimateModelMemoryAction.roundUpToNextMb(randomIntBetween(1, 1024 * 1024)), equalTo(ByteSizeValue.ofMb(1))); + assertThat(TransportEstimateModelMemoryAction.roundUpToNextMb(1024 * 1024), equalTo(ByteSizeValue.ofMb(1))); + assertThat(TransportEstimateModelMemoryAction.roundUpToNextMb(1024 * 1024 + 1), equalTo(ByteSizeValue.ofMb(2))); + assertThat(TransportEstimateModelMemoryAction.roundUpToNextMb(2 * 1024 * 1024), equalTo(ByteSizeValue.ofMb(2))); // We don't round up at the extremes, to ensure that the resulting value can be represented as bytes in a long // (At such extreme scale it won't be possible to actually run the analysis, so ease of use trumps precision) - assertThat(TransportEstimateModelMemoryAction.roundUpToNextMb(Long.MAX_VALUE - randomIntBetween(0, 1000000)), - equalTo(ByteSizeValue.ofMb(Long.MAX_VALUE / ByteSizeValue.ofMb(1).getBytes() ))); + assertThat( + TransportEstimateModelMemoryAction.roundUpToNextMb(Long.MAX_VALUE - randomIntBetween(0, 1000000)), + equalTo(ByteSizeValue.ofMb(Long.MAX_VALUE / ByteSizeValue.ofMb(1).getBytes())) + ); } public void testReducedCardinality() { @@ -136,17 +151,27 @@ public void testReducedCardinality() { long cardinalityToReduce = randomIntBetween(1001, Integer.MAX_VALUE); long saneBucketSpan = randomFrom(1, 30, 60, 300, 600, 900, 1800, 3600, 10800, 21600, 43200, 86400); - assertThat(TransportEstimateModelMemoryAction.reducedCardinality(0, randomNonNegativeLong(), saneBucketSpan), - closeTo(0.0, 1e-15)); - assertThat(TransportEstimateModelMemoryAction.reducedCardinality(cardinalityToReduce, 1, saneBucketSpan), - closeTo(cardinalityToReduce, 1e-6)); - assertThat(TransportEstimateModelMemoryAction.reducedCardinality(cardinalityToReduce, 1000, 900), - closeTo(cardinalityToReduce / Math.sqrt(1000), cardinalityToReduce / 20.0)); - assertThat(TransportEstimateModelMemoryAction.reducedCardinality( - cardinalityToReduce, randomIntBetween(2, Integer.MAX_VALUE), saneBucketSpan), - lessThan((double) cardinalityToReduce)); - assertThat(TransportEstimateModelMemoryAction.reducedCardinality(cardinalityToReduce, 1000, 10000000), - closeTo(cardinalityToReduce / 1000.0, 1e-4)); + assertThat(TransportEstimateModelMemoryAction.reducedCardinality(0, randomNonNegativeLong(), saneBucketSpan), closeTo(0.0, 1e-15)); + assertThat( + TransportEstimateModelMemoryAction.reducedCardinality(cardinalityToReduce, 1, saneBucketSpan), + closeTo(cardinalityToReduce, 1e-6) + ); + assertThat( + TransportEstimateModelMemoryAction.reducedCardinality(cardinalityToReduce, 1000, 900), + closeTo(cardinalityToReduce / Math.sqrt(1000), cardinalityToReduce / 20.0) + ); + assertThat( + TransportEstimateModelMemoryAction.reducedCardinality( + cardinalityToReduce, + randomIntBetween(2, Integer.MAX_VALUE), + saneBucketSpan + ), + lessThan((double) cardinalityToReduce) + ); + assertThat( + TransportEstimateModelMemoryAction.reducedCardinality(cardinalityToReduce, 1000, 10000000), + closeTo(cardinalityToReduce / 1000.0, 1e-4) + ); } public void testAddNonNegativeLongsWithMaxValueCap() { @@ -155,11 +180,17 @@ public void testAddNonNegativeLongsWithMaxValueCap() { assertThat(TransportEstimateModelMemoryAction.addNonNegativeLongsWithMaxValueCap(0, 1), is(1L)); assertThat(TransportEstimateModelMemoryAction.addNonNegativeLongsWithMaxValueCap(1, 0), is(1L)); assertThat(TransportEstimateModelMemoryAction.addNonNegativeLongsWithMaxValueCap(1, 1), is(2L)); - assertThat(TransportEstimateModelMemoryAction.addNonNegativeLongsWithMaxValueCap(Long.MAX_VALUE, Long.MAX_VALUE), - is(Long.MAX_VALUE)); - assertThat(TransportEstimateModelMemoryAction.addNonNegativeLongsWithMaxValueCap( - Long.MAX_VALUE - randomIntBetween(1, Integer.MAX_VALUE), Long.MAX_VALUE - randomIntBetween(1, Integer.MAX_VALUE)), - is(Long.MAX_VALUE)); + assertThat( + TransportEstimateModelMemoryAction.addNonNegativeLongsWithMaxValueCap(Long.MAX_VALUE, Long.MAX_VALUE), + is(Long.MAX_VALUE) + ); + assertThat( + TransportEstimateModelMemoryAction.addNonNegativeLongsWithMaxValueCap( + Long.MAX_VALUE - randomIntBetween(1, Integer.MAX_VALUE), + Long.MAX_VALUE - randomIntBetween(1, Integer.MAX_VALUE) + ), + is(Long.MAX_VALUE) + ); } public void testMultiplyNonNegativeLongsWithMaxValueCap() { @@ -168,20 +199,35 @@ public void testMultiplyNonNegativeLongsWithMaxValueCap() { assertThat(TransportEstimateModelMemoryAction.multiplyNonNegativeLongsWithMaxValueCap(randomNonNegativeLong(), 0), is(0L)); assertThat(TransportEstimateModelMemoryAction.multiplyNonNegativeLongsWithMaxValueCap(0, randomNonNegativeLong()), is(0L)); assertThat(TransportEstimateModelMemoryAction.multiplyNonNegativeLongsWithMaxValueCap(1, 1), is(1L)); - assertThat(TransportEstimateModelMemoryAction.multiplyNonNegativeLongsWithMaxValueCap(Long.MAX_VALUE, Long.MAX_VALUE), - is(Long.MAX_VALUE)); - assertThat(TransportEstimateModelMemoryAction.multiplyNonNegativeLongsWithMaxValueCap( - Long.MAX_VALUE, Math.max(1L, randomNonNegativeLong())), - is(Long.MAX_VALUE)); - assertThat(TransportEstimateModelMemoryAction.multiplyNonNegativeLongsWithMaxValueCap( - Math.max(1L, randomNonNegativeLong()), Long.MAX_VALUE), - is(Long.MAX_VALUE)); + assertThat( + TransportEstimateModelMemoryAction.multiplyNonNegativeLongsWithMaxValueCap(Long.MAX_VALUE, Long.MAX_VALUE), + is(Long.MAX_VALUE) + ); + assertThat( + TransportEstimateModelMemoryAction.multiplyNonNegativeLongsWithMaxValueCap( + Long.MAX_VALUE, + Math.max(1L, randomNonNegativeLong()) + ), + is(Long.MAX_VALUE) + ); + assertThat( + TransportEstimateModelMemoryAction.multiplyNonNegativeLongsWithMaxValueCap( + Math.max(1L, randomNonNegativeLong()), + Long.MAX_VALUE + ), + is(Long.MAX_VALUE) + ); assertThat(TransportEstimateModelMemoryAction.multiplyNonNegativeLongsWithMaxValueCap(0, Long.MAX_VALUE), is(0L)); assertThat(TransportEstimateModelMemoryAction.multiplyNonNegativeLongsWithMaxValueCap(Long.MAX_VALUE, 0), is(0L)); } - public static Detector createDetector(String function, String fieldName, String byFieldName, - String overFieldName, String partitionFieldName) { + public static Detector createDetector( + String function, + String fieldName, + String byFieldName, + String overFieldName, + String partitionFieldName + ) { Detector.Builder detectorBuilder = new Detector.Builder(function, fieldName); detectorBuilder.setByFieldName(byFieldName); @@ -190,13 +236,19 @@ public static Detector createDetector(String function, String fieldName, String return detectorBuilder.build(); } - public static AnalysisConfig createCountAnalysisConfig(String categorizationFieldName, String partitionFieldName, - String... influencerFieldNames) { + public static AnalysisConfig createCountAnalysisConfig( + String categorizationFieldName, + String partitionFieldName, + String... influencerFieldNames + ) { return createCountAnalysisConfigBuilder(categorizationFieldName, partitionFieldName, influencerFieldNames).build(); } - public static AnalysisConfig.Builder createCountAnalysisConfigBuilder(String categorizationFieldName, String partitionFieldName, - String... influencerFieldNames) { + public static AnalysisConfig.Builder createCountAnalysisConfigBuilder( + String categorizationFieldName, + String partitionFieldName, + String... influencerFieldNames + ) { Detector.Builder detectorBuilder = new Detector.Builder("count", null); detectorBuilder.setByFieldName((categorizationFieldName != null) ? AnalysisConfig.ML_CATEGORY_FIELD : null); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportFinalizeJobExecutionActionTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportFinalizeJobExecutionActionTests.java index b306f173c22d3..c341a021873b8 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportFinalizeJobExecutionActionTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportFinalizeJobExecutionActionTests.java @@ -43,7 +43,7 @@ public class TransportFinalizeJobExecutionActionTests extends ESTestCase { private Client client; @Before - @SuppressWarnings({"unchecked", "rawtypes"}) + @SuppressWarnings({ "unchecked", "rawtypes" }) private void setupMocks() { ExecutorService executorService = mock(ExecutorService.class); threadPool = mock(ThreadPool.class); @@ -54,7 +54,7 @@ private void setupMocks() { when(threadPool.executor(MachineLearning.UTILITY_THREAD_POOL_NAME)).thenReturn(executorService); client = mock(Client.class); - doAnswer( invocationOnMock -> { + doAnswer(invocationOnMock -> { ActionListener listener = (ActionListener) invocationOnMock.getArguments()[2]; listener.onResponse(null); return null; @@ -70,12 +70,9 @@ public void testOperation() { ClusterState clusterState = ClusterState.builder(new ClusterName("finalize-job-action-tests")).build(); - FinalizeJobExecutionAction.Request request = new FinalizeJobExecutionAction.Request(new String[]{"job1", "job2"}); + FinalizeJobExecutionAction.Request request = new FinalizeJobExecutionAction.Request(new String[] { "job1", "job2" }); AtomicReference ack = new AtomicReference<>(); - action.masterOperation(null, request, clusterState, ActionListener.wrap( - ack::set, - e -> assertNull(e.getMessage()) - )); + action.masterOperation(null, request, clusterState, ActionListener.wrap(ack::set, e -> assertNull(e.getMessage()))); assertTrue(ack.get().isAcknowledged()); verify(client, times(2)).execute(eq(UpdateAction.INSTANCE), any(), any()); @@ -83,8 +80,14 @@ public void testOperation() { } private TransportFinalizeJobExecutionAction createAction(ClusterService clusterService) { - return new TransportFinalizeJobExecutionAction(mock(TransportService.class), clusterService, - threadPool, mock(ActionFilters.class), mock(IndexNameExpressionResolver.class), client); + return new TransportFinalizeJobExecutionAction( + mock(TransportService.class), + clusterService, + threadPool, + mock(ActionFilters.class), + mock(IndexNameExpressionResolver.class), + client + ); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportForecastJobActionRequestTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportForecastJobActionRequestTests.java index 8dd9c7ae84cd7..e56e5bf11dfd8 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportForecastJobActionRequestTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportForecastJobActionRequestTests.java @@ -34,22 +34,22 @@ public void testValidate_jobVersionCannonBeBefore61() { jobBuilder.setJobVersion(Version.fromString("6.0.1")); ForecastJobAction.Request request = new ForecastJobAction.Request(); - Exception e = expectThrows(ElasticsearchStatusException.class, - () -> TransportForecastJobAction.validate(jobBuilder.build(), request)); - assertEquals( - "Cannot run forecast because jobs created prior to version 6.1 are not supported", - e.getMessage()); + Exception e = expectThrows( + ElasticsearchStatusException.class, + () -> TransportForecastJobAction.validate(jobBuilder.build(), request) + ); + assertEquals("Cannot run forecast because jobs created prior to version 6.1 are not supported", e.getMessage()); } public void testValidate_jobVersionCannonBeBefore61NoJobVersion() { Job.Builder jobBuilder = createTestJob("forecast-it-test-job-version"); ForecastJobAction.Request request = new ForecastJobAction.Request(); - Exception e = expectThrows(ElasticsearchStatusException.class, - () -> TransportForecastJobAction.validate(jobBuilder.build(), request)); - assertEquals( - "Cannot run forecast because jobs created prior to version 6.1 are not supported", - e.getMessage()); + Exception e = expectThrows( + ElasticsearchStatusException.class, + () -> TransportForecastJobAction.validate(jobBuilder.build(), request) + ); + assertEquals("Cannot run forecast because jobs created prior to version 6.1 are not supported", e.getMessage()); } public void testValidate_DurationCannotBeLessThanBucketSpan() { @@ -57,8 +57,10 @@ public void testValidate_DurationCannotBeLessThanBucketSpan() { ForecastJobAction.Request request = new ForecastJobAction.Request(); request.setDuration(TimeValue.timeValueMinutes(1)); - Exception e = expectThrows(ElasticsearchStatusException.class, - () -> TransportForecastJobAction.validate(jobBuilder.build(new Date()), request)); + Exception e = expectThrows( + ElasticsearchStatusException.class, + () -> TransportForecastJobAction.validate(jobBuilder.build(new Date()), request) + ); assertEquals("[duration] must be greater or equal to the bucket span: [1m/1h]", e.getMessage()); } @@ -67,45 +69,52 @@ public void testAdjustLimit() { AnomalyDetectionAuditor auditor = mock(AnomalyDetectionAuditor.class); { assertThat(TransportForecastJobAction.getAdjustedMemoryLimit(jobBuilder.build(), null, auditor), is(nullValue())); - assertThat(TransportForecastJobAction.getAdjustedMemoryLimit( - jobBuilder.build(), - ByteSizeValue.ofMb(20).getBytes(), - auditor), - equalTo(ByteSizeValue.ofMb(20).getBytes())); - assertThat(TransportForecastJobAction.getAdjustedMemoryLimit( - jobBuilder.build(), - ByteSizeValue.ofMb(499).getBytes(), - auditor), - equalTo(ByteSizeValue.ofMb(499).getBytes())); + assertThat( + TransportForecastJobAction.getAdjustedMemoryLimit(jobBuilder.build(), ByteSizeValue.ofMb(20).getBytes(), auditor), + equalTo(ByteSizeValue.ofMb(20).getBytes()) + ); + assertThat( + TransportForecastJobAction.getAdjustedMemoryLimit(jobBuilder.build(), ByteSizeValue.ofMb(499).getBytes(), auditor), + equalTo(ByteSizeValue.ofMb(499).getBytes()) + ); } { long limit = ByteSizeValue.ofMb(100).getBytes(); - assertThat(TransportForecastJobAction.getAdjustedMemoryLimit( - jobBuilder.setAnalysisLimits(new AnalysisLimits(1L)).build(), - limit, - auditor), - equalTo(104857600L)); + assertThat( + TransportForecastJobAction.getAdjustedMemoryLimit( + jobBuilder.setAnalysisLimits(new AnalysisLimits(1L)).build(), + limit, + auditor + ), + equalTo(104857600L) + ); } { long limit = 429496732L; - assertThat(TransportForecastJobAction.getAdjustedMemoryLimit( - jobBuilder.setAnalysisLimits(new AnalysisLimits(1L)).build(), - limit, - auditor), - equalTo(429496728L)); + assertThat( + TransportForecastJobAction.getAdjustedMemoryLimit( + jobBuilder.setAnalysisLimits(new AnalysisLimits(1L)).build(), + limit, + auditor + ), + equalTo(429496728L) + ); } { long limit = ByteSizeValue.ofMb(200).getBytes(); assertThat(TransportForecastJobAction.getAdjustedMemoryLimit(jobBuilder.build(), limit, auditor), equalTo(limit)); // gets adjusted down due to job analysis limits - assertThat(TransportForecastJobAction.getAdjustedMemoryLimit( - jobBuilder.setAnalysisLimits(new AnalysisLimits(200L, null)).build(), - limit, - auditor), - equalTo(ByteSizeValue.ofMb(80).getBytes() - 1L)); + assertThat( + TransportForecastJobAction.getAdjustedMemoryLimit( + jobBuilder.setAnalysisLimits(new AnalysisLimits(200L, null)).build(), + limit, + auditor + ), + equalTo(ByteSizeValue.ofMb(80).getBytes() - 1L) + ); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportGetJobsStatsActionTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportGetJobsStatsActionTests.java index 85f1f092abe6d..61ffd15132ba6 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportGetJobsStatsActionTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportGetJobsStatsActionTests.java @@ -33,7 +33,18 @@ public void testDetermineJobIds() { Collections.singletonList("id1"), Collections.singletonList( new GetJobsStatsAction.Response.JobStats( - "id1", new DataCounts("id1"), null, null, JobState.OPENED, null, null, null, new TimingStats("id1")))); + "id1", + new DataCounts("id1"), + null, + null, + JobState.OPENED, + null, + null, + null, + new TimingStats("id1") + ) + ) + ); assertEquals(0, result.size()); result = determineJobIdsWithoutLiveStats(Arrays.asList("id1", "id2", "id3"), Collections.emptyList()); @@ -42,30 +53,94 @@ public void testDetermineJobIds() { assertEquals("id2", result.get(1)); assertEquals("id3", result.get(2)); - result = determineJobIdsWithoutLiveStats(Arrays.asList("id1", "id2", "id3"), - Collections.singletonList(new GetJobsStatsAction.Response.JobStats("id1", new DataCounts("id1"), null, null, - JobState.OPENED, null, null, null, new TimingStats("id1"))) + result = determineJobIdsWithoutLiveStats( + Arrays.asList("id1", "id2", "id3"), + Collections.singletonList( + new GetJobsStatsAction.Response.JobStats( + "id1", + new DataCounts("id1"), + null, + null, + JobState.OPENED, + null, + null, + null, + new TimingStats("id1") + ) + ) ); assertEquals(2, result.size()); assertEquals("id2", result.get(0)); assertEquals("id3", result.get(1)); - result = determineJobIdsWithoutLiveStats(Arrays.asList("id1", "id2", "id3"), Arrays.asList( + result = determineJobIdsWithoutLiveStats( + Arrays.asList("id1", "id2", "id3"), + Arrays.asList( new GetJobsStatsAction.Response.JobStats( - "id1", new DataCounts("id1"), null, null, JobState.OPENED, null, null, null, new TimingStats("id1")), + "id1", + new DataCounts("id1"), + null, + null, + JobState.OPENED, + null, + null, + null, + new TimingStats("id1") + ), new GetJobsStatsAction.Response.JobStats( - "id3", new DataCounts("id3"), null, null, JobState.OPENED, null, null, null, new TimingStats("id3")) - )); + "id3", + new DataCounts("id3"), + null, + null, + JobState.OPENED, + null, + null, + null, + new TimingStats("id3") + ) + ) + ); assertEquals(1, result.size()); assertEquals("id2", result.get(0)); - result = determineJobIdsWithoutLiveStats(Arrays.asList("id1", "id2", "id3"), Arrays.asList( + result = determineJobIdsWithoutLiveStats( + Arrays.asList("id1", "id2", "id3"), + Arrays.asList( new GetJobsStatsAction.Response.JobStats( - "id1", new DataCounts("id1"), null, null, JobState.OPENED, null, null, null, new TimingStats("id1")), + "id1", + new DataCounts("id1"), + null, + null, + JobState.OPENED, + null, + null, + null, + new TimingStats("id1") + ), new GetJobsStatsAction.Response.JobStats( - "id2", new DataCounts("id2"), null, null, JobState.OPENED, null, null, null, new TimingStats("id2")), + "id2", + new DataCounts("id2"), + null, + null, + JobState.OPENED, + null, + null, + null, + new TimingStats("id2") + ), new GetJobsStatsAction.Response.JobStats( - "id3", new DataCounts("id3"), null, null, JobState.OPENED, null, null, null, new TimingStats("id3")))); + "id3", + new DataCounts("id3"), + null, + null, + JobState.OPENED, + null, + null, + null, + new TimingStats("id3") + ) + ) + ); assertEquals(0, result.size()); } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportGetTrainedModelsStatsActionTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportGetTrainedModelsStatsActionTests.java index d694fda60ec19..76829f607cbe2 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportGetTrainedModelsStatsActionTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportGetTrainedModelsStatsActionTests.java @@ -21,9 +21,6 @@ import org.elasticsearch.common.settings.ClusterSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.EsExecutors; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.ingest.IngestDocument; import org.elasticsearch.ingest.IngestMetadata; import org.elasticsearch.ingest.IngestService; @@ -34,6 +31,9 @@ import org.elasticsearch.plugins.IngestPlugin; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.ml.MachineLearningField; import org.elasticsearch.xpack.core.ml.inference.results.InferenceResults; import org.elasticsearch.xpack.ml.inference.ModelAliasMetadata; @@ -84,8 +84,12 @@ public String getDescription() { static class Factory implements Processor.Factory { @Override - public Processor create(Map processorFactories, String tag, String description, - Map config) { + public Processor create( + Map processorFactories, + String tag, + String description, + Map config + ) { return new NotInferenceProcessor(); } } @@ -97,10 +101,10 @@ public Map getProcessors(Processor.Parameters paramet Map factoryMap = new HashMap<>(); MockLicenseState licenseState = mock(MockLicenseState.class); when(licenseState.isAllowed(MachineLearningField.ML_API_FEATURE)).thenReturn(true); - factoryMap.put(InferenceProcessor.TYPE, - new InferenceProcessor.Factory(parameters.client, - parameters.ingestService.getClusterService(), - Settings.EMPTY)); + factoryMap.put( + InferenceProcessor.TYPE, + new InferenceProcessor.Factory(parameters.client, parameters.ingestService.getClusterService(), Settings.EMPTY) + ); factoryMap.put("not_inference", new NotInferenceProcessor.Factory()); @@ -118,15 +122,20 @@ public void setUpVariables() { when(tp.generic()).thenReturn(EsExecutors.DIRECT_EXECUTOR_SERVICE); client = mock(Client.class); Settings settings = Settings.builder().put("node.name", "InferenceProcessorFactoryTests_node").build(); - ClusterSettings clusterSettings = new ClusterSettings(settings, - new HashSet<>(Arrays.asList(InferenceProcessor.MAX_INFERENCE_PROCESSORS, - MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING, - OperationRouting.USE_ADAPTIVE_REPLICA_SELECTION_SETTING, - ClusterService.USER_DEFINED_METADATA, - ClusterApplierService.CLUSTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING))); + ClusterSettings clusterSettings = new ClusterSettings( + settings, + new HashSet<>( + Arrays.asList( + InferenceProcessor.MAX_INFERENCE_PROCESSORS, + MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING, + OperationRouting.USE_ADAPTIVE_REPLICA_SELECTION_SETTING, + ClusterService.USER_DEFINED_METADATA, + ClusterApplierService.CLUSTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING + ) + ) + ); clusterService = new ClusterService(settings, clusterSettings, tp); - ingestService = new IngestService(clusterService, tp, null, null, - null, Collections.singletonList(SKINNY_INGEST_PLUGIN), client); + ingestService = new IngestService(clusterService, tp, null, null, null, Collections.singletonList(SKINNY_INGEST_PLUGIN), client); } public void testInferenceIngestStatsByModelId() { @@ -134,16 +143,10 @@ public void testInferenceIngestStatsByModelId() { buildNodeStats( new IngestStats.Stats(2, 2, 3, 4), Arrays.asList( - new IngestStats.PipelineStat( - "pipeline1", - new IngestStats.Stats(0, 0, 3, 1)), - new IngestStats.PipelineStat( - "pipeline2", - new IngestStats.Stats(1, 1, 0, 1)), - new IngestStats.PipelineStat( - "pipeline3", - new IngestStats.Stats(2, 1, 1, 1)) - ), + new IngestStats.PipelineStat("pipeline1", new IngestStats.Stats(0, 0, 3, 1)), + new IngestStats.PipelineStat("pipeline2", new IngestStats.Stats(1, 1, 0, 1)), + new IngestStats.PipelineStat("pipeline3", new IngestStats.Stats(2, 1, 1, 1)) + ), Arrays.asList( Arrays.asList( new IngestStats.ProcessorStat(InferenceProcessor.TYPE, InferenceProcessor.TYPE, new IngestStats.Stats(10, 1, 0, 0)), @@ -151,28 +154,22 @@ public void testInferenceIngestStatsByModelId() { new IngestStats.ProcessorStat( InferenceProcessor.TYPE, InferenceProcessor.TYPE, - new IngestStats.Stats(100, 10, 0, 1)) + new IngestStats.Stats(100, 10, 0, 1) + ) ), Arrays.asList( new IngestStats.ProcessorStat(InferenceProcessor.TYPE, InferenceProcessor.TYPE, new IngestStats.Stats(5, 1, 0, 0)), new IngestStats.ProcessorStat("grok", "grok", new IngestStats.Stats(10, 1, 0, 0)) ), - Arrays.asList( - new IngestStats.ProcessorStat("grok", "grok", new IngestStats.Stats(10, 1, 0, 0)) - ) - )), + Arrays.asList(new IngestStats.ProcessorStat("grok", "grok", new IngestStats.Stats(10, 1, 0, 0))) + ) + ), buildNodeStats( new IngestStats.Stats(15, 5, 3, 4), Arrays.asList( - new IngestStats.PipelineStat( - "pipeline1", - new IngestStats.Stats(10, 1, 3, 1)), - new IngestStats.PipelineStat( - "pipeline2", - new IngestStats.Stats(1, 1, 0, 1)), - new IngestStats.PipelineStat( - "pipeline3", - new IngestStats.Stats(2, 1, 1, 1)) + new IngestStats.PipelineStat("pipeline1", new IngestStats.Stats(10, 1, 3, 1)), + new IngestStats.PipelineStat("pipeline2", new IngestStats.Stats(1, 1, 0, 1)), + new IngestStats.PipelineStat("pipeline3", new IngestStats.Stats(2, 1, 1, 1)) ), Arrays.asList( Arrays.asList( @@ -184,45 +181,63 @@ public void testInferenceIngestStatsByModelId() { new IngestStats.ProcessorStat(InferenceProcessor.TYPE, InferenceProcessor.TYPE, new IngestStats.Stats(5, 1, 0, 0)), new IngestStats.ProcessorStat("grok", "grok", new IngestStats.Stats(10, 1, 0, 0)) ), - Arrays.asList( - new IngestStats.ProcessorStat("grok", "grok", new IngestStats.Stats(10, 1, 0, 0)) - ) - )) + Arrays.asList(new IngestStats.ProcessorStat("grok", "grok", new IngestStats.Stats(10, 1, 0, 0))) + ) + ) ); NodesStatsResponse response = new NodesStatsResponse(new ClusterName("_name"), nodeStatsList, Collections.emptyList()); - Map> pipelineIdsByModelIds = new HashMap<>(){{ - put("trained_model_1", Collections.singleton("pipeline1")); - put("trained_model_2", new HashSet<>(Arrays.asList("pipeline1", "pipeline2"))); - }}; - Map ingestStatsMap = TransportGetTrainedModelsStatsAction.inferenceIngestStatsByModelId(response, + Map> pipelineIdsByModelIds = new HashMap<>() { + { + put("trained_model_1", Collections.singleton("pipeline1")); + put("trained_model_2", new HashSet<>(Arrays.asList("pipeline1", "pipeline2"))); + } + }; + Map ingestStatsMap = TransportGetTrainedModelsStatsAction.inferenceIngestStatsByModelId( + response, ModelAliasMetadata.EMPTY, - pipelineIdsByModelIds); + pipelineIdsByModelIds + ); assertThat(ingestStatsMap.keySet(), equalTo(new HashSet<>(Arrays.asList("trained_model_1", "trained_model_2")))); IngestStats expectedStatsModel1 = new IngestStats( new IngestStats.Stats(10, 1, 6, 2), Collections.singletonList(new IngestStats.PipelineStat("pipeline1", new IngestStats.Stats(10, 1, 6, 2))), - Collections.singletonMap("pipeline1", Arrays.asList( - new IngestStats.ProcessorStat("inference", "inference", new IngestStats.Stats(120, 12, 0, 1)), - new IngestStats.ProcessorStat("grok", "grok", new IngestStats.Stats(10, 1, 0, 0)))) + Collections.singletonMap( + "pipeline1", + Arrays.asList( + new IngestStats.ProcessorStat("inference", "inference", new IngestStats.Stats(120, 12, 0, 1)), + new IngestStats.ProcessorStat("grok", "grok", new IngestStats.Stats(10, 1, 0, 0)) + ) + ) ); IngestStats expectedStatsModel2 = new IngestStats( new IngestStats.Stats(12, 3, 6, 4), Arrays.asList( new IngestStats.PipelineStat("pipeline1", new IngestStats.Stats(10, 1, 6, 2)), - new IngestStats.PipelineStat("pipeline2", new IngestStats.Stats(2, 2, 0, 2))), - new HashMap<>() {{ - put("pipeline2", Arrays.asList( - new IngestStats.ProcessorStat("inference", "inference", new IngestStats.Stats(10, 2, 0, 0)), - new IngestStats.ProcessorStat("grok", "grok", new IngestStats.Stats(20, 2, 0, 0)))); - put("pipeline1", Arrays.asList( - new IngestStats.ProcessorStat("inference", "inference", new IngestStats.Stats(120, 12, 0, 1)), - new IngestStats.ProcessorStat("grok", "grok", new IngestStats.Stats(10, 1, 0, 0)))); - }} + new IngestStats.PipelineStat("pipeline2", new IngestStats.Stats(2, 2, 0, 2)) + ), + new HashMap<>() { + { + put( + "pipeline2", + Arrays.asList( + new IngestStats.ProcessorStat("inference", "inference", new IngestStats.Stats(10, 2, 0, 0)), + new IngestStats.ProcessorStat("grok", "grok", new IngestStats.Stats(20, 2, 0, 0)) + ) + ); + put( + "pipeline1", + Arrays.asList( + new IngestStats.ProcessorStat("inference", "inference", new IngestStats.Stats(120, 12, 0, 1)), + new IngestStats.ProcessorStat("grok", "grok", new IngestStats.Stats(10, 1, 0, 0)) + ) + ); + } + } ); assertThat(ingestStatsMap, hasEntry("trained_model_1", expectedStatsModel1)); @@ -237,16 +252,25 @@ public void testPipelineIdsByModelIds() throws IOException { ClusterState clusterState = buildClusterStateWithModelReferences(modelId1, modelId2, modelId3); - Map> pipelineIdsByModelIds = - TransportGetTrainedModelsStatsAction.pipelineIdsByModelIdsOrAliases(clusterState, ingestService, modelIds); + Map> pipelineIdsByModelIds = TransportGetTrainedModelsStatsAction.pipelineIdsByModelIdsOrAliases( + clusterState, + ingestService, + modelIds + ); assertThat(pipelineIdsByModelIds.keySet(), equalTo(modelIds)); - assertThat(pipelineIdsByModelIds, - hasEntry(modelId1, new HashSet<>(Arrays.asList("pipeline_with_model_" + modelId1 + 0, "pipeline_with_model_" + modelId1 + 1)))); - assertThat(pipelineIdsByModelIds, - hasEntry(modelId2, new HashSet<>(Arrays.asList("pipeline_with_model_" + modelId2 + 0, "pipeline_with_model_" + modelId2 + 1)))); - assertThat(pipelineIdsByModelIds, - hasEntry(modelId3, new HashSet<>(Arrays.asList("pipeline_with_model_" + modelId3 + 0, "pipeline_with_model_" + modelId3 + 1)))); + assertThat( + pipelineIdsByModelIds, + hasEntry(modelId1, new HashSet<>(Arrays.asList("pipeline_with_model_" + modelId1 + 0, "pipeline_with_model_" + modelId1 + 1))) + ); + assertThat( + pipelineIdsByModelIds, + hasEntry(modelId2, new HashSet<>(Arrays.asList("pipeline_with_model_" + modelId2 + 0, "pipeline_with_model_" + modelId2 + 1))) + ); + assertThat( + pipelineIdsByModelIds, + hasEntry(modelId3, new HashSet<>(Arrays.asList("pipeline_with_model_" + modelId3 + 0, "pipeline_with_model_" + modelId3 + 1))) + ); } @@ -267,39 +291,74 @@ private static ClusterState buildClusterStateWithModelReferences(String... model } private static PipelineConfiguration newConfigurationWithInferenceProcessor(String modelId, int num) throws IOException { - try(XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().map(Collections.singletonMap("processors", - Collections.singletonList( - Collections.singletonMap(InferenceProcessor.TYPE, - new HashMap() {{ - put(InferenceResults.MODEL_ID_RESULTS_FIELD, modelId); - put("inference_config", Collections.singletonMap("regression", Collections.emptyMap())); - put("field_map", Collections.emptyMap()); - put("target_field", randomAlphaOfLength(10)); - }}))))) { - return new PipelineConfiguration("pipeline_with_model_" + modelId + num, + try ( + XContentBuilder xContentBuilder = XContentFactory.jsonBuilder() + .map( + Collections.singletonMap( + "processors", + Collections.singletonList(Collections.singletonMap(InferenceProcessor.TYPE, new HashMap() { + { + put(InferenceResults.MODEL_ID_RESULTS_FIELD, modelId); + put("inference_config", Collections.singletonMap("regression", Collections.emptyMap())); + put("field_map", Collections.emptyMap()); + put("target_field", randomAlphaOfLength(10)); + } + })) + ) + ) + ) { + return new PipelineConfiguration( + "pipeline_with_model_" + modelId + num, BytesReference.bytes(xContentBuilder), - XContentType.JSON); + XContentType.JSON + ); } } private static PipelineConfiguration newConfigurationWithOutInferenceProcessor(int i) throws IOException { - try(XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().map(Collections.singletonMap("processors", - Collections.singletonList(Collections.singletonMap("not_inference", Collections.emptyMap()))))) { + try ( + XContentBuilder xContentBuilder = XContentFactory.jsonBuilder() + .map( + Collections.singletonMap( + "processors", + Collections.singletonList(Collections.singletonMap("not_inference", Collections.emptyMap())) + ) + ) + ) { return new PipelineConfiguration("pipeline_without_model_" + i, BytesReference.bytes(xContentBuilder), XContentType.JSON); } } - private static NodeStats buildNodeStats(IngestStats.Stats overallStats, - List pipelineNames, - List> processorStats) { + private static NodeStats buildNodeStats( + IngestStats.Stats overallStats, + List pipelineNames, + List> processorStats + ) { List pipelineids = pipelineNames.stream().map(IngestStats.PipelineStat::getPipelineId).collect(Collectors.toList()); IngestStats ingestStats = new IngestStats( overallStats, pipelineNames, - IntStream.range(0, pipelineids.size()).boxed().collect(Collectors.toMap(pipelineids::get, processorStats::get))); - return new NodeStats(mock(DiscoveryNode.class), - Instant.now().toEpochMilli(), null, null, null, null, null, null, null, null, - null, null, null, ingestStats, null, null, null); + IntStream.range(0, pipelineids.size()).boxed().collect(Collectors.toMap(pipelineids::get, processorStats::get)) + ); + return new NodeStats( + mock(DiscoveryNode.class), + Instant.now().toEpochMilli(), + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + ingestStats, + null, + null, + null + ); } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportMlInfoActionTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportMlInfoActionTests.java index ea90a4482d6fc..5b823fefccee4 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportMlInfoActionTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportMlInfoActionTests.java @@ -43,7 +43,8 @@ public void testCalculateEffectiveMaxModelMemoryLimitWithoutMaxMlNodeSize() { int numNonMlNodes = randomIntBetween(0, 10); ClusterSettings clusterSettings = new ClusterSettings( Settings.builder().put(MAX_MACHINE_MEMORY_PERCENT.getKey(), mlMemoryPercent).build(), - Sets.newHashSet(MAX_LAZY_ML_NODES, MAX_MACHINE_MEMORY_PERCENT, MAX_ML_NODE_SIZE, USE_AUTO_MACHINE_MEMORY_PERCENT)); + Sets.newHashSet(MAX_LAZY_ML_NODES, MAX_MACHINE_MEMORY_PERCENT, MAX_ML_NODE_SIZE, USE_AUTO_MACHINE_MEMORY_PERCENT) + ); long totalMlMemoryBytes = numMlNodes * mlMachineMemory * mlMemoryPercent / 100; DiscoveryNodes nodes = randomNodes(numMlNodes, numNonMlNodes, mlMachineMemory); @@ -56,10 +57,13 @@ public void testCalculateEffectiveMaxModelMemoryLimitWithoutMaxMlNodeSize() { } else { // Expect configured percentage of current node size (allowing for small rounding errors) assertThat(effectiveMaxModelMemoryLimit, notNullValue()); - assertThat(effectiveMaxModelMemoryLimit.getBytes() - + Math.max(Job.PROCESS_MEMORY_OVERHEAD.getBytes(), DataFrameAnalyticsConfig.PROCESS_MEMORY_OVERHEAD.getBytes()) - + MachineLearning.NATIVE_EXECUTABLE_CODE_OVERHEAD.getBytes(), - lessThanOrEqualTo(mlMachineMemory * mlMemoryPercent / 100)); + assertThat( + effectiveMaxModelMemoryLimit.getBytes() + Math.max( + Job.PROCESS_MEMORY_OVERHEAD.getBytes(), + DataFrameAnalyticsConfig.PROCESS_MEMORY_OVERHEAD.getBytes() + ) + MachineLearning.NATIVE_EXECUTABLE_CODE_OVERHEAD.getBytes(), + lessThanOrEqualTo(mlMachineMemory * mlMemoryPercent / 100) + ); } ByteSizeValue totalMlMemory = TransportMlInfoAction.calculateTotalMlMemory(clusterSettings, nodes); @@ -74,10 +78,13 @@ public void testCalculateEffectiveMaxModelMemoryLimitNoMlNodesButMaxMlNodeSizeAn long mlMaxNodeSize = randomLongBetween(2000000000L, 100000000000L); int numNonMlNodes = randomIntBetween(0, 10); ClusterSettings clusterSettings = new ClusterSettings( - Settings.builder().put(MAX_ML_NODE_SIZE.getKey(), mlMaxNodeSize + "b") + Settings.builder() + .put(MAX_ML_NODE_SIZE.getKey(), mlMaxNodeSize + "b") .put(MAX_LAZY_ML_NODES.getKey(), randomIntBetween(1, 100)) - .put(MAX_MACHINE_MEMORY_PERCENT.getKey(), mlMemoryPercent).build(), - Sets.newHashSet(MAX_LAZY_ML_NODES, MAX_MACHINE_MEMORY_PERCENT, MAX_ML_NODE_SIZE, USE_AUTO_MACHINE_MEMORY_PERCENT)); + .put(MAX_MACHINE_MEMORY_PERCENT.getKey(), mlMemoryPercent) + .build(), + Sets.newHashSet(MAX_LAZY_ML_NODES, MAX_MACHINE_MEMORY_PERCENT, MAX_ML_NODE_SIZE, USE_AUTO_MACHINE_MEMORY_PERCENT) + ); DiscoveryNodes nodes = randomNodes(0, numNonMlNodes, 0); @@ -85,10 +92,13 @@ public void testCalculateEffectiveMaxModelMemoryLimitNoMlNodesButMaxMlNodeSizeAn // Expect configured percentage of maximum declared node size (allowing for small rounding errors) assertThat(effectiveMaxModelMemoryLimit, notNullValue()); - assertThat(effectiveMaxModelMemoryLimit.getBytes() - + Math.max(Job.PROCESS_MEMORY_OVERHEAD.getBytes(), DataFrameAnalyticsConfig.PROCESS_MEMORY_OVERHEAD.getBytes()) - + MachineLearning.NATIVE_EXECUTABLE_CODE_OVERHEAD.getBytes(), - lessThanOrEqualTo(mlMaxNodeSize * mlMemoryPercent / 100)); + assertThat( + effectiveMaxModelMemoryLimit.getBytes() + Math.max( + Job.PROCESS_MEMORY_OVERHEAD.getBytes(), + DataFrameAnalyticsConfig.PROCESS_MEMORY_OVERHEAD.getBytes() + ) + MachineLearning.NATIVE_EXECUTABLE_CODE_OVERHEAD.getBytes(), + lessThanOrEqualTo(mlMaxNodeSize * mlMemoryPercent / 100) + ); ByteSizeValue totalMlMemory = TransportMlInfoAction.calculateTotalMlMemory(clusterSettings, nodes); @@ -104,10 +114,13 @@ public void testCalculateEffectiveMaxModelMemoryLimitSmallMlNodesButMaxMlNodeSiz int numMlNodes = randomIntBetween(1, 10); int numNonMlNodes = randomIntBetween(0, 10); ClusterSettings clusterSettings = new ClusterSettings( - Settings.builder().put(MAX_ML_NODE_SIZE.getKey(), mlMaxNodeSize + "b") + Settings.builder() + .put(MAX_ML_NODE_SIZE.getKey(), mlMaxNodeSize + "b") .put(MAX_LAZY_ML_NODES.getKey(), randomIntBetween(numMlNodes + 1, 100)) - .put(MAX_MACHINE_MEMORY_PERCENT.getKey(), mlMemoryPercent).build(), - Sets.newHashSet(MAX_LAZY_ML_NODES, MAX_MACHINE_MEMORY_PERCENT, MAX_ML_NODE_SIZE, USE_AUTO_MACHINE_MEMORY_PERCENT)); + .put(MAX_MACHINE_MEMORY_PERCENT.getKey(), mlMemoryPercent) + .build(), + Sets.newHashSet(MAX_LAZY_ML_NODES, MAX_MACHINE_MEMORY_PERCENT, MAX_ML_NODE_SIZE, USE_AUTO_MACHINE_MEMORY_PERCENT) + ); long totalMlMemoryBytes = numMlNodes * mlMachineMemory * mlMemoryPercent / 100; DiscoveryNodes nodes = randomNodes(numMlNodes, numNonMlNodes, mlMachineMemory); @@ -116,14 +129,20 @@ public void testCalculateEffectiveMaxModelMemoryLimitSmallMlNodesButMaxMlNodeSiz // Expect configured percentage of maximum declared node size (allowing for small rounding errors) - bigger than current node size assertThat(effectiveMaxModelMemoryLimit, notNullValue()); - assertThat(effectiveMaxModelMemoryLimit.getBytes() - + Math.max(Job.PROCESS_MEMORY_OVERHEAD.getBytes(), DataFrameAnalyticsConfig.PROCESS_MEMORY_OVERHEAD.getBytes()) - + MachineLearning.NATIVE_EXECUTABLE_CODE_OVERHEAD.getBytes(), - lessThanOrEqualTo(mlMaxNodeSize * mlMemoryPercent / 100)); - assertThat(effectiveMaxModelMemoryLimit.getBytes() - + Math.max(Job.PROCESS_MEMORY_OVERHEAD.getBytes(), DataFrameAnalyticsConfig.PROCESS_MEMORY_OVERHEAD.getBytes()) - + MachineLearning.NATIVE_EXECUTABLE_CODE_OVERHEAD.getBytes(), - greaterThan(2 * mlMachineMemory * mlMemoryPercent / 100)); + assertThat( + effectiveMaxModelMemoryLimit.getBytes() + Math.max( + Job.PROCESS_MEMORY_OVERHEAD.getBytes(), + DataFrameAnalyticsConfig.PROCESS_MEMORY_OVERHEAD.getBytes() + ) + MachineLearning.NATIVE_EXECUTABLE_CODE_OVERHEAD.getBytes(), + lessThanOrEqualTo(mlMaxNodeSize * mlMemoryPercent / 100) + ); + assertThat( + effectiveMaxModelMemoryLimit.getBytes() + Math.max( + Job.PROCESS_MEMORY_OVERHEAD.getBytes(), + DataFrameAnalyticsConfig.PROCESS_MEMORY_OVERHEAD.getBytes() + ) + MachineLearning.NATIVE_EXECUTABLE_CODE_OVERHEAD.getBytes(), + greaterThan(2 * mlMachineMemory * mlMemoryPercent / 100) + ); ByteSizeValue totalMlMemory = TransportMlInfoAction.calculateTotalMlMemory(clusterSettings, nodes); @@ -139,10 +158,13 @@ public void testCalculateEffectiveMaxModelMemoryLimitSmallMlNodesButMaxMlNodeSiz int numMlNodes = randomIntBetween(2, 10); int numNonMlNodes = randomIntBetween(0, 10); ClusterSettings clusterSettings = new ClusterSettings( - Settings.builder().put(MAX_ML_NODE_SIZE.getKey(), mlMaxNodeSize + "b") + Settings.builder() + .put(MAX_ML_NODE_SIZE.getKey(), mlMaxNodeSize + "b") .put(MAX_LAZY_ML_NODES.getKey(), randomIntBetween(1, numMlNodes - 1)) - .put(MAX_MACHINE_MEMORY_PERCENT.getKey(), mlMemoryPercent).build(), - Sets.newHashSet(MAX_LAZY_ML_NODES, MAX_MACHINE_MEMORY_PERCENT, MAX_ML_NODE_SIZE, USE_AUTO_MACHINE_MEMORY_PERCENT)); + .put(MAX_MACHINE_MEMORY_PERCENT.getKey(), mlMemoryPercent) + .build(), + Sets.newHashSet(MAX_LAZY_ML_NODES, MAX_MACHINE_MEMORY_PERCENT, MAX_ML_NODE_SIZE, USE_AUTO_MACHINE_MEMORY_PERCENT) + ); long totalMlMemoryBytes = numMlNodes * mlMachineMemory * mlMemoryPercent / 100; DiscoveryNodes nodes = randomNodes(numMlNodes, numNonMlNodes, mlMachineMemory); @@ -151,10 +173,13 @@ public void testCalculateEffectiveMaxModelMemoryLimitSmallMlNodesButMaxMlNodeSiz // Expect configured percentage of current node size (allowing for small rounding errors) - max is bigger but can't be added assertThat(effectiveMaxModelMemoryLimit, notNullValue()); - assertThat(effectiveMaxModelMemoryLimit.getBytes() - + Math.max(Job.PROCESS_MEMORY_OVERHEAD.getBytes(), DataFrameAnalyticsConfig.PROCESS_MEMORY_OVERHEAD.getBytes()) - + MachineLearning.NATIVE_EXECUTABLE_CODE_OVERHEAD.getBytes(), - lessThanOrEqualTo(mlMachineMemory * mlMemoryPercent / 100)); + assertThat( + effectiveMaxModelMemoryLimit.getBytes() + Math.max( + Job.PROCESS_MEMORY_OVERHEAD.getBytes(), + DataFrameAnalyticsConfig.PROCESS_MEMORY_OVERHEAD.getBytes() + ) + MachineLearning.NATIVE_EXECUTABLE_CODE_OVERHEAD.getBytes(), + lessThanOrEqualTo(mlMachineMemory * mlMemoryPercent / 100) + ); ByteSizeValue totalMlMemory = TransportMlInfoAction.calculateTotalMlMemory(clusterSettings, nodes); @@ -172,9 +197,16 @@ DiscoveryNodes randomNodes(int numMlNodes, int numNonMlNodes, long mlMachineMemo TransportAddress ta = new TransportAddress(InetAddress.getLoopbackAddress(), 9300 + i); if (i < numMlNodes) { // ML node - builder.add(new DiscoveryNode(nodeName, nodeId, ta, - Collections.singletonMap(MachineLearning.MACHINE_MEMORY_NODE_ATTR, String.valueOf(mlMachineMemory)), - Collections.emptySet(), Version.CURRENT)); + builder.add( + new DiscoveryNode( + nodeName, + nodeId, + ta, + Collections.singletonMap(MachineLearning.MACHINE_MEMORY_NODE_ATTR, String.valueOf(mlMachineMemory)), + Collections.emptySet(), + Version.CURRENT + ) + ); } else { // Not an ML node builder.add(new DiscoveryNode(nodeName, nodeId, ta, Collections.emptyMap(), Collections.emptySet(), Version.CURRENT)); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportPreviewDatafeedActionTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportPreviewDatafeedActionTests.java index 4abecdbd973e3..a28bdf214b43a 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportPreviewDatafeedActionTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportPreviewDatafeedActionTests.java @@ -83,8 +83,10 @@ public void testBuildPreviewDatafeed_GivenAggregations() { DatafeedConfig.Builder datafeed = new DatafeedConfig.Builder("no_aggs_feed", "job_foo"); datafeed.setIndices(Collections.singletonList("my_index")); MaxAggregationBuilder maxTime = AggregationBuilders.max("time").field("time"); - datafeed.setParsedAggregations(AggregatorFactories.builder().addAggregator( - AggregationBuilders.histogram("time").interval(300000).subAggregation(maxTime).field("time"))); + datafeed.setParsedAggregations( + AggregatorFactories.builder() + .addAggregator(AggregationBuilders.histogram("time").interval(300000).subAggregation(maxTime).field("time")) + ); datafeed.setChunkingConfig(ChunkingConfig.newManual(TimeValue.timeValueHours(1))); DatafeedConfig previewDatafeed = TransportPreviewDatafeedAction.buildPreviewDatafeed(datafeed.build()).build(); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportStartDataFrameAnalyticsActionTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportStartDataFrameAnalyticsActionTests.java index a1ce3bd76abb0..abb33d60d6c91 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportStartDataFrameAnalyticsActionTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportStartDataFrameAnalyticsActionTests.java @@ -53,10 +53,9 @@ public class TransportStartDataFrameAnalyticsActionTests extends ESTestCase { public void testGetAssignment_UpgradeModeIsEnabled() { TaskExecutor executor = createTaskExecutor(); TaskParams params = new TaskParams(JOB_ID, Version.CURRENT, false); - ClusterState clusterState = - ClusterState.builder(new ClusterName("_name")) - .metadata(Metadata.builder().putCustom(MlMetadata.TYPE, new MlMetadata.Builder().isUpgradeMode(true).build())) - .build(); + ClusterState clusterState = ClusterState.builder(new ClusterName("_name")) + .metadata(Metadata.builder().putCustom(MlMetadata.TYPE, new MlMetadata.Builder().isUpgradeMode(true).build())) + .build(); Assignment assignment = executor.getAssignment(params, clusterState.nodes().getAllNodes(), clusterState); assertThat(assignment.getExecutorNode(), is(nullValue())); @@ -67,10 +66,9 @@ public void testGetAssignment_UpgradeModeIsEnabled() { public void testGetAssignment_NoNodes() { TaskExecutor executor = createTaskExecutor(); TaskParams params = new TaskParams(JOB_ID, Version.CURRENT, false); - ClusterState clusterState = - ClusterState.builder(new ClusterName("_name")) - .metadata(Metadata.builder().putCustom(MlMetadata.TYPE, new MlMetadata.Builder().build())) - .build(); + ClusterState clusterState = ClusterState.builder(new ClusterName("_name")) + .metadata(Metadata.builder().putCustom(MlMetadata.TYPE, new MlMetadata.Builder().build())) + .build(); Assignment assignment = executor.getAssignment(params, clusterState.nodes().getAllNodes(), clusterState); assertThat(assignment.getExecutorNode(), is(nullValue())); @@ -81,14 +79,15 @@ public void testGetAssignment_NoNodes() { public void testGetAssignment_NoMlNodes() { TaskExecutor executor = createTaskExecutor(); TaskParams params = new TaskParams(JOB_ID, Version.CURRENT, false); - ClusterState clusterState = - ClusterState.builder(new ClusterName("_name")) - .metadata(Metadata.builder().putCustom(MlMetadata.TYPE, new MlMetadata.Builder().build())) - .nodes(DiscoveryNodes.builder() + ClusterState clusterState = ClusterState.builder(new ClusterName("_name")) + .metadata(Metadata.builder().putCustom(MlMetadata.TYPE, new MlMetadata.Builder().build())) + .nodes( + DiscoveryNodes.builder() .add(createNode(0, false, Version.CURRENT)) .add(createNode(1, false, Version.CURRENT)) - .add(createNode(2, false, Version.CURRENT))) - .build(); + .add(createNode(2, false, Version.CURRENT)) + ) + .build(); Assignment assignment = executor.getAssignment(params, clusterState.nodes().getAllNodes(), clusterState); assertThat(assignment.getExecutorNode(), is(nullValue())); @@ -97,36 +96,47 @@ public void testGetAssignment_NoMlNodes() { allOf( containsString("Not opening job [data_frame_id] on node [_node_name0]. Reason: This node isn't a machine learning node."), containsString("Not opening job [data_frame_id] on node [_node_name1]. Reason: This node isn't a machine learning node."), - containsString("Not opening job [data_frame_id] on node [_node_name2]. Reason: This node isn't a machine learning node."))); + containsString("Not opening job [data_frame_id] on node [_node_name2]. Reason: This node isn't a machine learning node.") + ) + ); } // Cannot assign the node because none of the existing nodes is appropriate: - // - _node_name0 is too old (version 7.2.0) - // - _node_name1 is too old (version 7.9.1) - // - _node_name2 is too old (version 7.9.2) + // - _node_name0 is too old (version 7.2.0) + // - _node_name1 is too old (version 7.9.1) + // - _node_name2 is too old (version 7.9.2) public void testGetAssignment_MlNodesAreTooOld() { TaskExecutor executor = createTaskExecutor(); TaskParams params = new TaskParams(JOB_ID, Version.CURRENT, false); - ClusterState clusterState = - ClusterState.builder(new ClusterName("_name")) - .metadata(Metadata.builder().putCustom(MlMetadata.TYPE, new MlMetadata.Builder().build())) - .nodes(DiscoveryNodes.builder() + ClusterState clusterState = ClusterState.builder(new ClusterName("_name")) + .metadata(Metadata.builder().putCustom(MlMetadata.TYPE, new MlMetadata.Builder().build())) + .nodes( + DiscoveryNodes.builder() .add(createNode(0, true, Version.V_7_2_0)) .add(createNode(1, true, Version.V_7_9_1)) - .add(createNode(2, true, Version.V_7_9_2))) - .build(); + .add(createNode(2, true, Version.V_7_9_2)) + ) + .build(); Assignment assignment = executor.getAssignment(params, clusterState.nodes().getAllNodes(), clusterState); assertThat(assignment.getExecutorNode(), is(nullValue())); assertThat( assignment.getExplanation(), allOf( - containsString("Not opening job [data_frame_id] on node [{_node_name0}{version=7.2.0}], " - + "because the data frame analytics requires a node of version [7.3.0] or higher"), - containsString("Not opening job [data_frame_id] on node [{_node_name1}{version=7.9.1}], " - + "because the data frame analytics created for version [8.0.0] requires a node of version [7.10.0] or higher"), - containsString("Not opening job [data_frame_id] on node [{_node_name2}{version=7.9.2}], " - + "because the data frame analytics created for version [8.0.0] requires a node of version [7.10.0] or higher"))); + containsString( + "Not opening job [data_frame_id] on node [{_node_name0}{version=7.2.0}], " + + "because the data frame analytics requires a node of version [7.3.0] or higher" + ), + containsString( + "Not opening job [data_frame_id] on node [{_node_name1}{version=7.9.1}], " + + "because the data frame analytics created for version [8.0.0] requires a node of version [7.10.0] or higher" + ), + containsString( + "Not opening job [data_frame_id] on node [{_node_name2}{version=7.9.2}], " + + "because the data frame analytics created for version [8.0.0] requires a node of version [7.10.0] or higher" + ) + ) + ); } // The node can be assigned despite being newer than the job. @@ -134,12 +144,10 @@ public void testGetAssignment_MlNodesAreTooOld() { public void testGetAssignment_MlNodeIsNewerThanTheMlJobButTheAssignmentSuceeds() { TaskExecutor executor = createTaskExecutor(); TaskParams params = new TaskParams(JOB_ID, Version.V_7_9_0, false); - ClusterState clusterState = - ClusterState.builder(new ClusterName("_name")) - .metadata(Metadata.builder().putCustom(MlMetadata.TYPE, new MlMetadata.Builder().build())) - .nodes(DiscoveryNodes.builder() - .add(createNode(0, true, Version.V_7_10_0))) - .build(); + ClusterState clusterState = ClusterState.builder(new ClusterName("_name")) + .metadata(Metadata.builder().putCustom(MlMetadata.TYPE, new MlMetadata.Builder().build())) + .nodes(DiscoveryNodes.builder().add(createNode(0, true, Version.V_7_10_0))) + .build(); Assignment assignment = executor.getAssignment(params, clusterState.nodes().getAllNodes(), clusterState); assertThat(assignment.getExecutorNode(), is(equalTo("_node_id0"))); @@ -148,16 +156,17 @@ public void testGetAssignment_MlNodeIsNewerThanTheMlJobButTheAssignmentSuceeds() private static TaskExecutor createTaskExecutor() { ClusterService clusterService = mock(ClusterService.class); - ClusterSettings clusterSettings = - new ClusterSettings( - Settings.EMPTY, - Sets.newHashSet( - MachineLearning.CONCURRENT_JOB_ALLOCATIONS, - MachineLearning.MAX_MACHINE_MEMORY_PERCENT, - MachineLearning.USE_AUTO_MACHINE_MEMORY_PERCENT, - MachineLearning.MAX_ML_NODE_SIZE, - MachineLearning.MAX_LAZY_ML_NODES, - MachineLearning.MAX_OPEN_JOBS_PER_NODE)); + ClusterSettings clusterSettings = new ClusterSettings( + Settings.EMPTY, + Sets.newHashSet( + MachineLearning.CONCURRENT_JOB_ALLOCATIONS, + MachineLearning.MAX_MACHINE_MEMORY_PERCENT, + MachineLearning.USE_AUTO_MACHINE_MEMORY_PERCENT, + MachineLearning.MAX_ML_NODE_SIZE, + MachineLearning.MAX_LAZY_ML_NODES, + MachineLearning.MAX_OPEN_JOBS_PER_NODE + ) + ); when(clusterService.getClusterSettings()).thenReturn(clusterSettings); return new TaskExecutor( @@ -178,8 +187,10 @@ private static DiscoveryNode createNode(int i, boolean isMlNode, Version nodeVer "_node_id" + i, new TransportAddress(InetAddress.getLoopbackAddress(), 9300 + i), isMlNode ? Map.of("ml.machine_memory", String.valueOf(ByteSizeValue.ofGb(1).getBytes())) : Map.of(), - isMlNode ? Set.of(DiscoveryNodeRole.MASTER_ROLE, DiscoveryNodeRole.DATA_ROLE, DiscoveryNodeRole.ML_ROLE) + isMlNode + ? Set.of(DiscoveryNodeRole.MASTER_ROLE, DiscoveryNodeRole.DATA_ROLE, DiscoveryNodeRole.ML_ROLE) : Set.of(DiscoveryNodeRole.MASTER_ROLE, DiscoveryNodeRole.DATA_ROLE), - nodeVersion); + nodeVersion + ); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportStartDatafeedActionTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportStartDatafeedActionTests.java index 815678593096f..0a787ddb32c78 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportStartDatafeedActionTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportStartDatafeedActionTests.java @@ -11,11 +11,11 @@ import org.elasticsearch.Version; import org.elasticsearch.common.collect.MapBuilder; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.persistent.PersistentTasksCustomMetadata; import org.elasticsearch.search.SearchModule; import org.elasticsearch.tasks.TaskId; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.ml.action.StartDatafeedAction; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfig; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfigTests; @@ -54,8 +54,10 @@ public void testValidate_jobClosed() { Job job1 = DatafeedRunnerTests.createDatafeedJob().build(new Date()); PersistentTasksCustomMetadata tasks = PersistentTasksCustomMetadata.builder().build(); DatafeedConfig datafeedConfig1 = DatafeedRunnerTests.createDatafeedConfig("foo-datafeed", "job_id").build(); - Exception e = expectThrows(ElasticsearchStatusException.class, - () -> TransportStartDatafeedAction.validate(job1, datafeedConfig1, tasks, xContentRegistry())); + Exception e = expectThrows( + ElasticsearchStatusException.class, + () -> TransportStartDatafeedAction.validate(job1, datafeedConfig1, tasks, xContentRegistry()) + ); assertThat(e.getMessage(), equalTo("cannot start datafeed [foo-datafeed] because job [job_id] is closed")); } @@ -90,8 +92,10 @@ public void testDeprecationsLogged() { TransportStartDatafeedAction.auditDeprecations(config, job1, auditor, xContentRegistry()); - verify(auditor).warning(job1.getId(), - "datafeed [start-data-feed-test] configuration has deprecations. [Deprecated Agg, Deprecated Query]"); + verify(auditor).warning( + job1.getId(), + "datafeed [start-data-feed-test] configuration has deprecations. [Deprecated Agg, Deprecated Query]" + ); } public void testNoDeprecationsLogged() { @@ -117,15 +121,14 @@ public void testRemoteClusterVersionCheck() { Map field = Collections.singletonMap( "runtime_field_foo", - MapBuilder.newMapBuilder() - .put("type", "keyword") - .put("script", "") - .map()); + MapBuilder.newMapBuilder().put("type", "keyword").put("script", "").map() + ); - DatafeedConfig config = new DatafeedConfig.Builder(DatafeedConfigTests.createRandomizedDatafeedConfig("foo")) - .setRuntimeMappings(field) - .build(); - ElasticsearchStatusException ex = expectThrows(ElasticsearchStatusException.class, + DatafeedConfig config = new DatafeedConfig.Builder(DatafeedConfigTests.createRandomizedDatafeedConfig("foo")).setRuntimeMappings( + field + ).build(); + ElasticsearchStatusException ex = expectThrows( + ElasticsearchStatusException.class, () -> TransportStartDatafeedAction.checkRemoteClusterVersions( config, Arrays.asList("old_cluster_1", "modern_cluster_2"), @@ -147,10 +150,10 @@ public void testRemoteClusterVersionCheck() { clusterVersions::get ); - DatafeedConfig configWithoutRuntimeMappings = new DatafeedConfig.Builder() - .setId("foo-datafeed") + DatafeedConfig configWithoutRuntimeMappings = new DatafeedConfig.Builder().setId("foo-datafeed") .setIndices(Collections.singletonList("bar")) - .setJobId("foo").build(); + .setJobId("foo") + .build(); TransportStartDatafeedAction.checkRemoteClusterVersions( configWithoutRuntimeMappings, Arrays.asList("old_cluster_1", "modern_cluster_2"), @@ -158,14 +161,26 @@ public void testRemoteClusterVersionCheck() { ); } - public static TransportStartDatafeedAction.DatafeedTask createDatafeedTask(long id, String type, String action, - TaskId parentTaskId, - StartDatafeedAction.DatafeedParams params, - DatafeedRunner datafeedRunner) { - TransportStartDatafeedAction.DatafeedTask task = new TransportStartDatafeedAction.DatafeedTask(id, type, action, parentTaskId, - params, Collections.emptyMap()); - assertThat(task.setDatafeedRunner(datafeedRunner), - is(TransportStartDatafeedAction.DatafeedTask.StoppedOrIsolatedBeforeRunning.NEITHER)); + public static TransportStartDatafeedAction.DatafeedTask createDatafeedTask( + long id, + String type, + String action, + TaskId parentTaskId, + StartDatafeedAction.DatafeedParams params, + DatafeedRunner datafeedRunner + ) { + TransportStartDatafeedAction.DatafeedTask task = new TransportStartDatafeedAction.DatafeedTask( + id, + type, + action, + parentTaskId, + params, + Collections.emptyMap() + ); + assertThat( + task.setDatafeedRunner(datafeedRunner), + is(TransportStartDatafeedAction.DatafeedTask.StoppedOrIsolatedBeforeRunning.NEITHER) + ); return task; } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportStopDataFrameAnalyticsActionTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportStopDataFrameAnalyticsActionTests.java index 2d2170747a1d7..3f40cde230935 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportStopDataFrameAnalyticsActionTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportStopDataFrameAnalyticsActionTests.java @@ -26,7 +26,7 @@ public class TransportStopDataFrameAnalyticsActionTests extends ESTestCase { public void testAnalyticsByTaskState_GivenEmpty() { - PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); + PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); AnalyticsByTaskState analyticsByTaskState = AnalyticsByTaskState.build(Collections.emptySet(), tasksBuilder.build()); @@ -34,7 +34,7 @@ public void testAnalyticsByTaskState_GivenEmpty() { } public void testAnalyticsByTaskState_GivenAllStates() { - PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); + PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); addAnalyticsTask(tasksBuilder, "starting", "foo-node", null); addAnalyticsTask(tasksBuilder, "started", "foo-node", DataFrameAnalyticsState.STARTED); addAnalyticsTask(tasksBuilder, "reindexing", "foo-node", DataFrameAnalyticsState.REINDEXING); @@ -51,24 +51,41 @@ public void testAnalyticsByTaskState_GivenAllStates() { assertThat(analyticsByTaskState.started, containsInAnyOrder("starting", "started", "reindexing", "analyzing")); assertThat(analyticsByTaskState.stopping, containsInAnyOrder("stopping")); assertThat(analyticsByTaskState.failed, containsInAnyOrder("failed")); - assertThat(analyticsByTaskState.getNonStopped(), containsInAnyOrder( - "starting", "started", "reindexing", "analyzing", "stopping", "failed"));; + assertThat( + analyticsByTaskState.getNonStopped(), + containsInAnyOrder("starting", "started", "reindexing", "analyzing", "stopping", "failed") + ); + ; } - private static void addAnalyticsTask(PersistentTasksCustomMetadata.Builder builder, String analyticsId, String nodeId, - DataFrameAnalyticsState state) { + private static void addAnalyticsTask( + PersistentTasksCustomMetadata.Builder builder, + String analyticsId, + String nodeId, + DataFrameAnalyticsState state + ) { addAnalyticsTask(builder, analyticsId, nodeId, state, false); } - private static void addAnalyticsTask(PersistentTasksCustomMetadata.Builder builder, String analyticsId, String nodeId, - DataFrameAnalyticsState state, boolean allowLazyStart) { - builder.addTask(MlTasks.dataFrameAnalyticsTaskId(analyticsId), MlTasks.DATA_FRAME_ANALYTICS_TASK_NAME, + private static void addAnalyticsTask( + PersistentTasksCustomMetadata.Builder builder, + String analyticsId, + String nodeId, + DataFrameAnalyticsState state, + boolean allowLazyStart + ) { + builder.addTask( + MlTasks.dataFrameAnalyticsTaskId(analyticsId), + MlTasks.DATA_FRAME_ANALYTICS_TASK_NAME, new StartDataFrameAnalyticsAction.TaskParams(analyticsId, Version.CURRENT, allowLazyStart), - new PersistentTasksCustomMetadata.Assignment(nodeId, "test assignment")); + new PersistentTasksCustomMetadata.Assignment(nodeId, "test assignment") + ); if (state != null) { - builder.updateTaskState(MlTasks.dataFrameAnalyticsTaskId(analyticsId), - new DataFrameAnalyticsTaskState(state, builder.getLastAllocationId(), null)); + builder.updateTaskState( + MlTasks.dataFrameAnalyticsTaskId(analyticsId), + new DataFrameAnalyticsTaskState(state, builder.getLastAllocationId(), null) + ); } } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportStopDatafeedActionTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportStopDatafeedActionTests.java index eddc5b57ed595..c1dd5ce07e569 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportStopDatafeedActionTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/action/TransportStopDatafeedActionTests.java @@ -29,7 +29,12 @@ public void testSortDatafeedIdsByTaskState_GivenDatafeedId() { List stoppingDatafeeds = new ArrayList<>(); List notStoppedDatafeeds = new ArrayList<>(); TransportStopDatafeedAction.sortDatafeedIdsByTaskState( - Collections.singleton("datafeed_1"), tasks, startedDatafeeds, stoppingDatafeeds, notStoppedDatafeeds); + Collections.singleton("datafeed_1"), + tasks, + startedDatafeeds, + stoppingDatafeeds, + notStoppedDatafeeds + ); assertEquals(Collections.singletonList("datafeed_1"), startedDatafeeds); assertEquals(Collections.emptyList(), stoppingDatafeeds); assertEquals(Collections.singletonList("datafeed_1"), notStoppedDatafeeds); @@ -38,7 +43,12 @@ public void testSortDatafeedIdsByTaskState_GivenDatafeedId() { stoppingDatafeeds.clear(); notStoppedDatafeeds.clear(); TransportStopDatafeedAction.sortDatafeedIdsByTaskState( - Collections.singleton("datafeed_2"), tasks, startedDatafeeds, stoppingDatafeeds, notStoppedDatafeeds); + Collections.singleton("datafeed_2"), + tasks, + startedDatafeeds, + stoppingDatafeeds, + notStoppedDatafeeds + ); assertEquals(Collections.emptyList(), startedDatafeeds); assertEquals(Collections.emptyList(), stoppingDatafeeds); assertEquals(Collections.emptyList(), notStoppedDatafeeds); @@ -56,24 +66,42 @@ public void testSortDatafeedIdsByTaskState_GivenAll() { List stoppingDatafeeds = new ArrayList<>(); List notStoppedDatafeeds = new ArrayList<>(); TransportStopDatafeedAction.sortDatafeedIdsByTaskState( - Arrays.asList("datafeed_1", "datafeed_2", "datafeed_3"), tasks, startedDatafeeds, stoppingDatafeeds, notStoppedDatafeeds); + Arrays.asList("datafeed_1", "datafeed_2", "datafeed_3"), + tasks, + startedDatafeeds, + stoppingDatafeeds, + notStoppedDatafeeds + ); assertEquals(Collections.singletonList("datafeed_1"), startedDatafeeds); assertEquals(Collections.singletonList("datafeed_3"), stoppingDatafeeds); assertEquals(Arrays.asList("datafeed_1", "datafeed_3"), notStoppedDatafeeds); startedDatafeeds.clear(); stoppingDatafeeds.clear(); - TransportStopDatafeedAction.sortDatafeedIdsByTaskState(Collections.singleton("datafeed_2"), tasks, startedDatafeeds, - stoppingDatafeeds, notStoppedDatafeeds); + TransportStopDatafeedAction.sortDatafeedIdsByTaskState( + Collections.singleton("datafeed_2"), + tasks, + startedDatafeeds, + stoppingDatafeeds, + notStoppedDatafeeds + ); assertEquals(Collections.emptyList(), startedDatafeeds); assertEquals(Collections.emptyList(), stoppingDatafeeds); } - public static void addTask(String datafeedId, long startTime, String nodeId, DatafeedState state, - PersistentTasksCustomMetadata.Builder taskBuilder) { - taskBuilder.addTask(MlTasks.datafeedTaskId(datafeedId), MlTasks.DATAFEED_TASK_NAME, - new StartDatafeedAction.DatafeedParams(datafeedId, startTime), - new PersistentTasksCustomMetadata.Assignment(nodeId, "test assignment")); + public static void addTask( + String datafeedId, + long startTime, + String nodeId, + DatafeedState state, + PersistentTasksCustomMetadata.Builder taskBuilder + ) { + taskBuilder.addTask( + MlTasks.datafeedTaskId(datafeedId), + MlTasks.DATAFEED_TASK_NAME, + new StartDatafeedAction.DatafeedParams(datafeedId, startTime), + new PersistentTasksCustomMetadata.Assignment(nodeId, "test assignment") + ); taskBuilder.updateTaskState(MlTasks.datafeedTaskId(datafeedId), state); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/DoubleArrayTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/DoubleArrayTests.java index ce125c29fbbe4..31a80e39b3184 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/DoubleArrayTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/DoubleArrayTests.java @@ -30,27 +30,18 @@ public void testCumulativeSum() { public void testDivMut() { double[] zeros = DoubleStream.generate(() -> 0.0).limit(10).toArray(); DoubleArray.divMut(zeros, randomDouble()); - assertThat( - boxed(zeros), - arrayContaining(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0) - ); + assertThat(boxed(zeros), arrayContaining(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)); double[] ones = DoubleStream.generate(() -> 1.0).limit(10).toArray(); DoubleArray.divMut(ones, 2.0); - assertThat( - boxed(ones), - arrayContaining(0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5) - ); + assertThat(boxed(ones), arrayContaining(0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5)); } public void testDivMut_validation() { expectThrows( IllegalArgumentException.class, - () -> DoubleArray.divMut( - DoubleStream.generate(ESTestCase::randomDouble).limit(10).toArray(), - 0.0 - ) + () -> DoubleArray.divMut(DoubleStream.generate(ESTestCase::randomDouble).limit(10).toArray(), 0.0) ); expectThrows( IllegalArgumentException.class, diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/categorization/CategorizeTextAggregatorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/categorization/CategorizeTextAggregatorTests.java index eb62b6cec225a..480c5f3cccd72 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/categorization/CategorizeTextAggregatorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/categorization/CategorizeTextAggregatorTests.java @@ -79,8 +79,8 @@ public void testCategorizationWithoutSubAggs() throws Exception { public void testCategorizationWithSubAggs() throws Exception { CategorizeTextAggregationBuilder aggBuilder = new CategorizeTextAggregationBuilder("my_agg", TEXT_FIELD_NAME).subAggregation( - new MaxAggregationBuilder("max").field(NUMERIC_FIELD_NAME) - ) + new MaxAggregationBuilder("max").field(NUMERIC_FIELD_NAME) + ) .subAggregation(new AvgAggregationBuilder("avg").field(NUMERIC_FIELD_NAME)) .subAggregation(new MinAggregationBuilder("min").field(NUMERIC_FIELD_NAME)); testCase( @@ -163,69 +163,62 @@ public void testCategorizationAsSubAgg() throws Exception { .interval(2) .subAggregation( new CategorizeTextAggregationBuilder("my_agg", TEXT_FIELD_NAME).subAggregation( - new MaxAggregationBuilder("max").field(NUMERIC_FIELD_NAME) - ) + new MaxAggregationBuilder("max").field(NUMERIC_FIELD_NAME) + ) .subAggregation(new AvgAggregationBuilder("avg").field(NUMERIC_FIELD_NAME)) .subAggregation(new MinAggregationBuilder("min").field(NUMERIC_FIELD_NAME)) ); - testCase( - aggBuilder, - new MatchAllDocsQuery(), - CategorizeTextAggregatorTests::writeTestDocs, - (InternalHistogram result) -> { - assertThat(result.getBuckets(), hasSize(3)); + testCase(aggBuilder, new MatchAllDocsQuery(), CategorizeTextAggregatorTests::writeTestDocs, (InternalHistogram result) -> { + assertThat(result.getBuckets(), hasSize(3)); - // First histo bucket - assertThat(result.getBuckets().get(0).getDocCount(), equalTo(3L)); - InternalCategorizationAggregation categorizationAggregation = result.getBuckets().get(0).getAggregations().get("my_agg"); - assertThat(categorizationAggregation.getBuckets(), hasSize(2)); - assertThat(categorizationAggregation.getBuckets().get(0).docCount, equalTo(2L)); - assertThat(categorizationAggregation.getBuckets().get(0).getKeyAsString(), equalTo("Node started")); - assertThat(((Max) categorizationAggregation.getBuckets().get(0).aggregations.get("max")).getValue(), equalTo(1.0)); - assertThat(((Min) categorizationAggregation.getBuckets().get(0).aggregations.get("min")).getValue(), equalTo(0.0)); - assertThat(((Avg) categorizationAggregation.getBuckets().get(0).aggregations.get("avg")).getValue(), equalTo(0.5)); + // First histo bucket + assertThat(result.getBuckets().get(0).getDocCount(), equalTo(3L)); + InternalCategorizationAggregation categorizationAggregation = result.getBuckets().get(0).getAggregations().get("my_agg"); + assertThat(categorizationAggregation.getBuckets(), hasSize(2)); + assertThat(categorizationAggregation.getBuckets().get(0).docCount, equalTo(2L)); + assertThat(categorizationAggregation.getBuckets().get(0).getKeyAsString(), equalTo("Node started")); + assertThat(((Max) categorizationAggregation.getBuckets().get(0).aggregations.get("max")).getValue(), equalTo(1.0)); + assertThat(((Min) categorizationAggregation.getBuckets().get(0).aggregations.get("min")).getValue(), equalTo(0.0)); + assertThat(((Avg) categorizationAggregation.getBuckets().get(0).aggregations.get("avg")).getValue(), equalTo(0.5)); - assertThat(categorizationAggregation.getBuckets().get(1).docCount, equalTo(1L)); - assertThat( - categorizationAggregation.getBuckets().get(1).getKeyAsString(), - equalTo("Failed to shutdown error org.aaaa.bbbb.Cccc line caused by foo exception") - ); - assertThat(((Max) categorizationAggregation.getBuckets().get(1).aggregations.get("max")).getValue(), equalTo(0.0)); - assertThat(((Min) categorizationAggregation.getBuckets().get(1).aggregations.get("min")).getValue(), equalTo(0.0)); - assertThat(((Avg) categorizationAggregation.getBuckets().get(1).aggregations.get("avg")).getValue(), equalTo(0.0)); + assertThat(categorizationAggregation.getBuckets().get(1).docCount, equalTo(1L)); + assertThat( + categorizationAggregation.getBuckets().get(1).getKeyAsString(), + equalTo("Failed to shutdown error org.aaaa.bbbb.Cccc line caused by foo exception") + ); + assertThat(((Max) categorizationAggregation.getBuckets().get(1).aggregations.get("max")).getValue(), equalTo(0.0)); + assertThat(((Min) categorizationAggregation.getBuckets().get(1).aggregations.get("min")).getValue(), equalTo(0.0)); + assertThat(((Avg) categorizationAggregation.getBuckets().get(1).aggregations.get("avg")).getValue(), equalTo(0.0)); - // Second histo bucket - assertThat(result.getBuckets().get(1).getDocCount(), equalTo(2L)); - categorizationAggregation = result.getBuckets().get(1).getAggregations().get("my_agg"); - assertThat(categorizationAggregation.getBuckets(), hasSize(1)); - assertThat(categorizationAggregation.getBuckets().get(0).docCount, equalTo(2L)); - assertThat(categorizationAggregation.getBuckets().get(0).getKeyAsString(), equalTo("Node started")); - assertThat(((Max) categorizationAggregation.getBuckets().get(0).aggregations.get("max")).getValue(), equalTo(3.0)); - assertThat(((Min) categorizationAggregation.getBuckets().get(0).aggregations.get("min")).getValue(), equalTo(2.0)); - assertThat(((Avg) categorizationAggregation.getBuckets().get(0).aggregations.get("avg")).getValue(), equalTo(2.5)); + // Second histo bucket + assertThat(result.getBuckets().get(1).getDocCount(), equalTo(2L)); + categorizationAggregation = result.getBuckets().get(1).getAggregations().get("my_agg"); + assertThat(categorizationAggregation.getBuckets(), hasSize(1)); + assertThat(categorizationAggregation.getBuckets().get(0).docCount, equalTo(2L)); + assertThat(categorizationAggregation.getBuckets().get(0).getKeyAsString(), equalTo("Node started")); + assertThat(((Max) categorizationAggregation.getBuckets().get(0).aggregations.get("max")).getValue(), equalTo(3.0)); + assertThat(((Min) categorizationAggregation.getBuckets().get(0).aggregations.get("min")).getValue(), equalTo(2.0)); + assertThat(((Avg) categorizationAggregation.getBuckets().get(0).aggregations.get("avg")).getValue(), equalTo(2.5)); - // Third histo bucket - assertThat(result.getBuckets().get(2).getDocCount(), equalTo(3L)); - categorizationAggregation = result.getBuckets().get(2).getAggregations().get("my_agg"); - assertThat(categorizationAggregation.getBuckets(), hasSize(2)); - assertThat(categorizationAggregation.getBuckets().get(0).docCount, equalTo(2L)); - assertThat(categorizationAggregation.getBuckets().get(0).getKeyAsString(), equalTo("Node started")); - assertThat(((Max) categorizationAggregation.getBuckets().get(0).aggregations.get("max")).getValue(), equalTo(5.0)); - assertThat(((Min) categorizationAggregation.getBuckets().get(0).aggregations.get("min")).getValue(), equalTo(4.0)); - assertThat(((Avg) categorizationAggregation.getBuckets().get(0).aggregations.get("avg")).getValue(), equalTo(4.5)); + // Third histo bucket + assertThat(result.getBuckets().get(2).getDocCount(), equalTo(3L)); + categorizationAggregation = result.getBuckets().get(2).getAggregations().get("my_agg"); + assertThat(categorizationAggregation.getBuckets(), hasSize(2)); + assertThat(categorizationAggregation.getBuckets().get(0).docCount, equalTo(2L)); + assertThat(categorizationAggregation.getBuckets().get(0).getKeyAsString(), equalTo("Node started")); + assertThat(((Max) categorizationAggregation.getBuckets().get(0).aggregations.get("max")).getValue(), equalTo(5.0)); + assertThat(((Min) categorizationAggregation.getBuckets().get(0).aggregations.get("min")).getValue(), equalTo(4.0)); + assertThat(((Avg) categorizationAggregation.getBuckets().get(0).aggregations.get("avg")).getValue(), equalTo(4.5)); - assertThat(categorizationAggregation.getBuckets().get(1).docCount, equalTo(1L)); - assertThat( - categorizationAggregation.getBuckets().get(1).getKeyAsString(), - equalTo("Failed to shutdown error org.aaaa.bbbb.Cccc line caused by foo exception") - ); - assertThat(((Max) categorizationAggregation.getBuckets().get(1).aggregations.get("max")).getValue(), equalTo(4.0)); - assertThat(((Min) categorizationAggregation.getBuckets().get(1).aggregations.get("min")).getValue(), equalTo(4.0)); - assertThat(((Avg) categorizationAggregation.getBuckets().get(1).aggregations.get("avg")).getValue(), equalTo(4.0)); - }, - new TextFieldMapper.TextFieldType(TEXT_FIELD_NAME), - longField(NUMERIC_FIELD_NAME) - ); + assertThat(categorizationAggregation.getBuckets().get(1).docCount, equalTo(1L)); + assertThat( + categorizationAggregation.getBuckets().get(1).getKeyAsString(), + equalTo("Failed to shutdown error org.aaaa.bbbb.Cccc line caused by foo exception") + ); + assertThat(((Max) categorizationAggregation.getBuckets().get(1).aggregations.get("max")).getValue(), equalTo(4.0)); + assertThat(((Min) categorizationAggregation.getBuckets().get(1).aggregations.get("min")).getValue(), equalTo(4.0)); + assertThat(((Avg) categorizationAggregation.getBuckets().get(1).aggregations.get("avg")).getValue(), equalTo(4.0)); + }, new TextFieldMapper.TextFieldType(TEXT_FIELD_NAME), longField(NUMERIC_FIELD_NAME)); } public void testCategorizationWithSubAggsManyDocs() throws Exception { diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/categorization/InternalCategorizationAggregationTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/categorization/InternalCategorizationAggregationTests.java index 8441f4a0142dc..6c4b43bb08acb 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/categorization/InternalCategorizationAggregationTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/categorization/InternalCategorizationAggregationTests.java @@ -10,13 +10,13 @@ import org.apache.lucene.util.BytesRef; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.CollectionUtils; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.plugins.SearchPlugin; import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.InternalAggregations; import org.elasticsearch.search.aggregations.ParsedMultiBucketAggregation; import org.elasticsearch.test.InternalMultiBucketAggregationTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xpack.ml.MachineLearning; import java.util.ArrayList; diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/categorization/ParsedCategorization.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/categorization/ParsedCategorization.java index 17ee466132214..283c200c27d0f 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/categorization/ParsedCategorization.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/categorization/ParsedCategorization.java @@ -9,12 +9,12 @@ import org.apache.lucene.util.BytesRef; import org.elasticsearch.common.Strings; -import org.elasticsearch.xcontent.ObjectParser; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParserUtils; import org.elasticsearch.search.aggregations.ParsedMultiBucketAggregation; import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation; +import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.Arrays; diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/correlation/BucketCorrelationAggregationBuilderTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/correlation/BucketCorrelationAggregationBuilderTests.java index 0428b4dc697d8..e80689d537d2d 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/correlation/BucketCorrelationAggregationBuilderTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/correlation/BucketCorrelationAggregationBuilderTests.java @@ -9,13 +9,13 @@ import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.plugins.SearchPlugin; import org.elasticsearch.search.aggregations.AggregationBuilder; import org.elasticsearch.search.aggregations.BasePipelineAggregationTestCase; import org.elasticsearch.search.aggregations.bucket.global.GlobalAggregationBuilder; import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; import org.elasticsearch.search.aggregations.support.ValueType; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.ml.MachineLearning; import java.util.Collections; @@ -47,11 +47,7 @@ protected List additionalNamedWriteables() { @Override protected BucketCorrelationAggregationBuilder createTestAggregatorFactory() { CorrelationFunction function = new CountCorrelationFunction(CountCorrelationIndicatorTests.randomInstance()); - return new BucketCorrelationAggregationBuilder( - NAME, - randomAlphaOfLength(8), - function - ); + return new BucketCorrelationAggregationBuilder(NAME, randomAlphaOfLength(8), function); } public void testValidate() { diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/correlation/CountCorrelationFunctionTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/correlation/CountCorrelationFunctionTests.java index 7ad5b08ad35aa..a717d7f6bfc45 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/correlation/CountCorrelationFunctionTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/correlation/CountCorrelationFunctionTests.java @@ -29,25 +29,23 @@ public class CountCorrelationFunctionTests extends ESTestCase { public void testExecute() { AtomicLong xs = new AtomicLong(1); CountCorrelationIndicator x = new CountCorrelationIndicator( - Stream.generate(xs::incrementAndGet) - .limit(100) - .mapToDouble(l -> (double)l).toArray(), + Stream.generate(xs::incrementAndGet).limit(100).mapToDouble(l -> (double) l).toArray(), null, 1000 ); CountCorrelationFunction countCorrelationFunction = new CountCorrelationFunction(x); AtomicLong ys = new AtomicLong(0); CountCorrelationIndicator yValues = new CountCorrelationIndicator( - Stream.generate(() -> Math.min(ys.incrementAndGet(), 10)).limit(100).mapToDouble(l -> (double)l).toArray(), + Stream.generate(() -> Math.min(ys.incrementAndGet(), 10)).limit(100).mapToDouble(l -> (double) l).toArray(), x.getFractions(), - 1000 + 1000 ); double value = countCorrelationFunction.execute(yValues); assertThat(value, greaterThan(0.0)); AtomicLong otherYs = new AtomicLong(0); CountCorrelationIndicator lesserYValues = new CountCorrelationIndicator( - Stream.generate(() -> Math.min(otherYs.incrementAndGet(), 5)).limit(100).mapToDouble(l -> (double)l).toArray(), + Stream.generate(() -> Math.min(otherYs.incrementAndGet(), 5)).limit(100).mapToDouble(l -> (double) l).toArray(), x.getFractions(), 1000 ); @@ -59,8 +57,11 @@ public void testValidation() { final Set aggBuilders = new HashSet<>(); aggBuilders.add(multiBucketAgg); CountCorrelationFunction function = new CountCorrelationFunction(CountCorrelationIndicatorTests.randomInstance()); - PipelineAggregationBuilder.ValidationContext validationContext = - PipelineAggregationBuilder.ValidationContext.forTreeRoot(aggBuilders, Collections.emptyList(), null); + PipelineAggregationBuilder.ValidationContext validationContext = PipelineAggregationBuilder.ValidationContext.forTreeRoot( + aggBuilders, + Collections.emptyList(), + null + ); function.validate(validationContext, "terms>metric_agg"); assertThat( diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/correlation/CountCorrelationIndicatorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/correlation/CountCorrelationIndicatorTests.java index 9858462cb97bc..4e1a8a82d8a88 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/correlation/CountCorrelationIndicatorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/correlation/CountCorrelationIndicatorTests.java @@ -8,9 +8,9 @@ package org.elasticsearch.xpack.ml.aggs.correlation; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.stream.Stream; @@ -20,10 +20,12 @@ public class CountCorrelationIndicatorTests extends AbstractSerializingTestCase< public static CountCorrelationIndicator randomInstance() { double[] expectations = Stream.generate(ESTestCase::randomDouble) .limit(randomIntBetween(5, 100)) - .mapToDouble(Double::doubleValue).toArray(); + .mapToDouble(Double::doubleValue) + .toArray(); double[] fractions = Stream.generate(ESTestCase::randomDouble) .limit(expectations.length) - .mapToDouble(Double::doubleValue).toArray(); + .mapToDouble(Double::doubleValue) + .toArray(); return new CountCorrelationIndicator(expectations, randomBoolean() ? null : fractions, randomLongBetween(1, Long.MAX_VALUE - 1)); } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/heuristic/MlChiSquaredDistributionTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/heuristic/MlChiSquaredDistributionTests.java index d2e329fc6abdb..56f2f7fa14eeb 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/heuristic/MlChiSquaredDistributionTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/heuristic/MlChiSquaredDistributionTests.java @@ -14,9 +14,20 @@ public class MlChiSquaredDistributionTests extends ESTestCase { public void testSurvivalFunction() { - double[] inputs = new double[] {0.210212602629, 0.554298076728, 0.831211613487, 1.14547622606, 1.61030798696, - 20.5150056524, 15.0862724694, 12.8325019940, 11.0704976935, 9.23635689978, 0.0, -1.0}; - double[] results = new double[] {0.001, 0.01, 0.025, 0.05, 0.1, 0.999, 0.990, 0.975, 0.950, 0.900, 0.0, 0.0}; + double[] inputs = new double[] { + 0.210212602629, + 0.554298076728, + 0.831211613487, + 1.14547622606, + 1.61030798696, + 20.5150056524, + 15.0862724694, + 12.8325019940, + 11.0704976935, + 9.23635689978, + 0.0, + -1.0 }; + double[] results = new double[] { 0.001, 0.01, 0.025, 0.05, 0.1, 0.999, 0.990, 0.975, 0.950, 0.900, 0.0, 0.0 }; MlChiSquaredDistribution mlChiSquaredDistribution = new MlChiSquaredDistribution(5.0); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/heuristic/PValueScoreTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/heuristic/PValueScoreTests.java index 3a22584c8cb48..9964d18d7cdc8 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/heuristic/PValueScoreTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/heuristic/PValueScoreTests.java @@ -11,10 +11,10 @@ import org.elasticsearch.Version; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.search.SearchModule; import org.elasticsearch.search.aggregations.bucket.AbstractNXYSignificanceHeuristicTestCase; import org.elasticsearch.search.aggregations.bucket.terms.heuristic.SignificanceHeuristic; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.ml.MachineLearning; import java.util.List; @@ -77,11 +77,11 @@ public void testPValueScore_WhenAllDocsContainTerm() { public void testHighPValueScore() { boolean backgroundIsSuperset = randomBoolean(); // supersetFreqCount needs to at less than 20% ratio - long supersetCount = randomLongBetween(0L, Long.MAX_VALUE/2); - long supersetFreqCount = randomLongBetween(0L, (long)(supersetCount/5.0)); + long supersetCount = randomLongBetween(0L, Long.MAX_VALUE / 2); + long supersetFreqCount = randomLongBetween(0L, (long) (supersetCount / 5.0)); // subsetFreqCount needs to be at least 25% ratio - long subsetCount = randomLongBetween((long)(supersetCount/4.0), supersetCount); - long subsetFreqCount = randomLongBetween((long)(subsetCount/4.0), subsetCount); + long subsetCount = randomLongBetween((long) (supersetCount / 4.0), supersetCount); + long subsetFreqCount = randomLongBetween((long) (subsetCount / 4.0), subsetCount); if (backgroundIsSuperset) { supersetCount += subsetCount; supersetFreqCount += subsetFreqCount; @@ -94,11 +94,11 @@ public void testHighPValueScore() { public void testLowPValueScore() { boolean backgroundIsSuperset = randomBoolean(); // supersetFreqCount needs to at least be 20% ratio - long supersetCount = randomLongBetween(0L, Long.MAX_VALUE/2); - long supersetFreqCount = randomLongBetween((long)(supersetCount/5.0), supersetCount); + long supersetCount = randomLongBetween(0L, Long.MAX_VALUE / 2); + long supersetFreqCount = randomLongBetween((long) (supersetCount / 5.0), supersetCount); // subsetFreqCount needs to be less than 16% ratio - long subsetCount = randomLongBetween((long)(supersetCount/5.0), supersetCount); - long subsetFreqCount = randomLongBetween(0L, (long)(subsetCount/6.0)); + long subsetCount = randomLongBetween((long) (supersetCount / 5.0), supersetCount); + long subsetFreqCount = randomLongBetween(0L, (long) (subsetCount / 6.0)); if (backgroundIsSuperset) { supersetCount += subsetCount; supersetFreqCount += subsetFreqCount; @@ -112,125 +112,55 @@ public void testLowPValueScore() { } public void testPValueScore() { - assertThat( - FastMath.exp(-new PValueScore(false, null).getScore(10, 100, 100, 1000)), - closeTo(1.0, eps) - ); - assertThat( - FastMath.exp(-new PValueScore(false, 200L).getScore(10, 100, 100, 1000)), - closeTo(1.0, eps) - ); - assertThat( - FastMath.exp(-new PValueScore(false, null).getScore(10, 100, 10, 1000)), - closeTo(0.003972388976814195, eps) - ); - assertThat( - FastMath.exp(-new PValueScore(false, 200L).getScore(10, 100, 10, 1000)), - closeTo(0.020890782016496683, eps) - ); - assertThat( - FastMath.exp(-new PValueScore(false, null).getScore(10, 100, 200, 1000)), - closeTo(1.0, eps) - ); - assertThat( - FastMath.exp(-new PValueScore(false, 200L).getScore(10, 100, 200, 1000)), - closeTo(1.0, eps) - ); - assertThat( - FastMath.exp(-new PValueScore(false, null).getScore(20, 10000, 5, 10000)), - closeTo(1.0, eps) - ); - assertThat( - FastMath.exp(-new PValueScore(false, 200L).getScore(20, 10000, 5, 10000)), - closeTo(1.0, eps) - ); + assertThat(FastMath.exp(-new PValueScore(false, null).getScore(10, 100, 100, 1000)), closeTo(1.0, eps)); + assertThat(FastMath.exp(-new PValueScore(false, 200L).getScore(10, 100, 100, 1000)), closeTo(1.0, eps)); + assertThat(FastMath.exp(-new PValueScore(false, null).getScore(10, 100, 10, 1000)), closeTo(0.003972388976814195, eps)); + assertThat(FastMath.exp(-new PValueScore(false, 200L).getScore(10, 100, 10, 1000)), closeTo(0.020890782016496683, eps)); + assertThat(FastMath.exp(-new PValueScore(false, null).getScore(10, 100, 200, 1000)), closeTo(1.0, eps)); + assertThat(FastMath.exp(-new PValueScore(false, 200L).getScore(10, 100, 200, 1000)), closeTo(1.0, eps)); + assertThat(FastMath.exp(-new PValueScore(false, null).getScore(20, 10000, 5, 10000)), closeTo(1.0, eps)); + assertThat(FastMath.exp(-new PValueScore(false, 200L).getScore(20, 10000, 5, 10000)), closeTo(1.0, eps)); } public void testSmallChanges() { - assertThat( - FastMath.exp(-new PValueScore(false, null).getScore(1, 4205, 0, 821496)), - closeTo(0.9999037287868853, eps) - ); + assertThat(FastMath.exp(-new PValueScore(false, null).getScore(1, 4205, 0, 821496)), closeTo(0.9999037287868853, eps)); // Same(ish) ratios - assertThat( - FastMath.exp(-new PValueScore(false, null).getScore(10, 4205, 195, 82149)), - closeTo(0.9995943820612134, eps) - ); - assertThat( - FastMath.exp(-new PValueScore(false, 100L).getScore(10, 4205, 195, 82149)), - closeTo(0.9876284079864467, eps) - ); + assertThat(FastMath.exp(-new PValueScore(false, null).getScore(10, 4205, 195, 82149)), closeTo(0.9995943820612134, eps)); + assertThat(FastMath.exp(-new PValueScore(false, 100L).getScore(10, 4205, 195, 82149)), closeTo(0.9876284079864467, eps)); - assertThat( - FastMath.exp(-new PValueScore(false, null).getScore(10, 4205, 1950, 821496)), - closeTo(0.9999942565428899, eps) - ); - assertThat( - FastMath.exp(-new PValueScore(false, 100L).getScore(10, 4205, 1950, 821496)), - closeTo(1.0, eps) - ); + assertThat(FastMath.exp(-new PValueScore(false, null).getScore(10, 4205, 1950, 821496)), closeTo(0.9999942565428899, eps)); + assertThat(FastMath.exp(-new PValueScore(false, 100L).getScore(10, 4205, 1950, 821496)), closeTo(1.0, eps)); // 4% vs 0% - assertThat( - FastMath.exp(-new PValueScore(false, null).getScore(168, 4205, 0, 821496)), - closeTo(1.2680918648731284e-26, eps) - ); - assertThat( - FastMath.exp(-new PValueScore(false, 100L).getScore(168, 4205, 0, 821496)), - closeTo(0.3882951183744724, eps) - ); + assertThat(FastMath.exp(-new PValueScore(false, null).getScore(168, 4205, 0, 821496)), closeTo(1.2680918648731284e-26, eps)); + assertThat(FastMath.exp(-new PValueScore(false, 100L).getScore(168, 4205, 0, 821496)), closeTo(0.3882951183744724, eps)); // 4% vs 2% - assertThat( - FastMath.exp(-new PValueScore(false, null).getScore(168, 4205, 16429, 821496)), - closeTo(8.542608559219833e-5, eps) - ); - assertThat( - FastMath.exp(-new PValueScore(false, 100L).getScore(168, 4205, 16429, 821496)), - closeTo(0.579463586350363, eps) - ); + assertThat(FastMath.exp(-new PValueScore(false, null).getScore(168, 4205, 16429, 821496)), closeTo(8.542608559219833e-5, eps)); + assertThat(FastMath.exp(-new PValueScore(false, 100L).getScore(168, 4205, 16429, 821496)), closeTo(0.579463586350363, eps)); // 4% vs 3.5% - assertThat( - FastMath.exp(-new PValueScore(false, null).getScore(168, 4205, 28752, 821496)), - closeTo(0.8833950526957098, eps) - ); - assertThat( - FastMath.exp(-new PValueScore(false, 100L).getScore(168, 4205, 28752, 821496)), - closeTo(1.0, eps) - ); + assertThat(FastMath.exp(-new PValueScore(false, null).getScore(168, 4205, 28752, 821496)), closeTo(0.8833950526957098, eps)); + assertThat(FastMath.exp(-new PValueScore(false, 100L).getScore(168, 4205, 28752, 821496)), closeTo(1.0, eps)); } public void testLargerValues() { - assertThat( - FastMath.exp(-new PValueScore(false, null).getScore(101000, 1000000, 500000, 5000000)), - closeTo(1.0, eps) - ); - assertThat( - FastMath.exp(-new PValueScore(false, null).getScore(102000, 1000000, 500000, 5000000)), - closeTo(1.0, eps) - ); - assertThat( - FastMath.exp(-new PValueScore(false, null).getScore(103000, 1000000, 500000, 5000000)), - closeTo(1.0, eps) - ); + assertThat(FastMath.exp(-new PValueScore(false, null).getScore(101000, 1000000, 500000, 5000000)), closeTo(1.0, eps)); + assertThat(FastMath.exp(-new PValueScore(false, null).getScore(102000, 1000000, 500000, 5000000)), closeTo(1.0, eps)); + assertThat(FastMath.exp(-new PValueScore(false, null).getScore(103000, 1000000, 500000, 5000000)), closeTo(1.0, eps)); } public void testScoreIsZero() { for (int j = 0; j < 10; j++) { - assertThat( - new PValueScore(false, null).getScore((j + 1)*5, (j + 10)*100, (j + 1)*10, (j + 10)*100), - equalTo(0.0) - ); + assertThat(new PValueScore(false, null).getScore((j + 1) * 5, (j + 10) * 100, (j + 1) * 10, (j + 10) * 100), equalTo(0.0)); } } public void testIncreasedSubsetIncreasedScore() { - final Function getScore = (subsetFreq) -> - new PValueScore(false, null).getScore(subsetFreq, 5000, 5, 5000); + final Function getScore = (subsetFreq) -> new PValueScore(false, null).getScore(subsetFreq, 5000, 5, 5000); double priorScore = getScore.apply(5L); assertThat(priorScore, greaterThanOrEqualTo(0.0)); for (int j = 1; j < 11; j++) { - double nextScore = getScore.apply(j*10L); + double nextScore = getScore.apply(j * 10L); assertThat(nextScore, greaterThanOrEqualTo(0.0)); assertThat(nextScore, greaterThanOrEqualTo(priorScore)); priorScore = nextScore; diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/inference/InferencePipelineAggregationBuilderTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/inference/InferencePipelineAggregationBuilderTests.java index 58068a177d063..4aed2261b89ef 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/inference/InferencePipelineAggregationBuilderTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/inference/InferencePipelineAggregationBuilderTests.java @@ -10,12 +10,12 @@ import org.apache.lucene.util.SetOnce; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.plugins.SearchPlugin; import org.elasticsearch.search.aggregations.AggregationBuilder; import org.elasticsearch.search.aggregations.BasePipelineAggregationTestCase; import org.elasticsearch.search.aggregations.PipelineAggregationBuilder; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.ml.inference.MlInferenceNamedXContentProvider; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.ClassificationConfigUpdate; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.ClassificationConfigUpdateTests; @@ -62,9 +62,13 @@ protected InferencePipelineAggregationBuilder createTestAggregatorFactory() { .limit(randomIntBetween(1, 4)) .collect(Collectors.toMap(Function.identity(), (t) -> randomAlphaOfLength(5))); - InferencePipelineAggregationBuilder builder = - new InferencePipelineAggregationBuilder(NAME, new SetOnce<>(mock(ModelLoadingService.class)), - mock(XPackLicenseState.class), Settings.EMPTY, bucketPaths); + InferencePipelineAggregationBuilder builder = new InferencePipelineAggregationBuilder( + NAME, + new SetOnce<>(mock(ModelLoadingService.class)), + mock(XPackLicenseState.class), + Settings.EMPTY, + bucketPaths + ); builder.setModelId(randomAlphaOfLength(6)); if (randomBoolean()) { @@ -98,8 +102,10 @@ public void testAdaptForAggregation() { public void testValidate() { InferencePipelineAggregationBuilder aggregationBuilder = createTestAggregatorFactory(); - PipelineAggregationBuilder.ValidationContext validationContext = - PipelineAggregationBuilder.ValidationContext.forInsideTree(mock(AggregationBuilder.class), null); + PipelineAggregationBuilder.ValidationContext validationContext = PipelineAggregationBuilder.ValidationContext.forInsideTree( + mock(AggregationBuilder.class), + null + ); aggregationBuilder.setModelId(null); aggregationBuilder.validate(validationContext); @@ -109,8 +115,10 @@ public void testValidate() { public void testValidate_invalidResultsField() { InferencePipelineAggregationBuilder aggregationBuilder = createTestAggregatorFactory(); - PipelineAggregationBuilder.ValidationContext validationContext = - PipelineAggregationBuilder.ValidationContext.forInsideTree(mock(AggregationBuilder.class), null); + PipelineAggregationBuilder.ValidationContext validationContext = PipelineAggregationBuilder.ValidationContext.forInsideTree( + mock(AggregationBuilder.class), + null + ); RegressionConfigUpdate regressionConfigUpdate = new RegressionConfigUpdate("foo", null); aggregationBuilder.setInferenceConfig(regressionConfigUpdate); @@ -121,8 +129,10 @@ public void testValidate_invalidResultsField() { public void testValidate_invalidTopClassesField() { InferencePipelineAggregationBuilder aggregationBuilder = createTestAggregatorFactory(); - PipelineAggregationBuilder.ValidationContext validationContext = - PipelineAggregationBuilder.ValidationContext.forInsideTree(mock(AggregationBuilder.class), null); + PipelineAggregationBuilder.ValidationContext validationContext = PipelineAggregationBuilder.ValidationContext.forInsideTree( + mock(AggregationBuilder.class), + null + ); ClassificationConfigUpdate configUpdate = new ClassificationConfigUpdate(1, null, "some_other_field", null, null); aggregationBuilder.setInferenceConfig(configUpdate); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/inference/InternalInferenceAggregationTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/inference/InternalInferenceAggregationTests.java index ad5a63845d3f2..5211a591b5b29 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/inference/InternalInferenceAggregationTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/inference/InternalInferenceAggregationTests.java @@ -7,15 +7,15 @@ package org.elasticsearch.xpack.ml.aggs.inference; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.CollectionUtils; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.plugins.SearchPlugin; import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.InvalidAggregationPathException; import org.elasticsearch.search.aggregations.ParsedAggregation; import org.elasticsearch.test.InternalAggregationTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xpack.core.ml.inference.results.ClassificationFeatureImportance; import org.elasticsearch.xpack.core.ml.inference.results.ClassificationInferenceResults; import org.elasticsearch.xpack.core.ml.inference.results.ClassificationInferenceResultsTests; @@ -45,8 +45,14 @@ protected SearchPlugin registerPlugin() { @Override protected List getNamedXContents() { - return CollectionUtils.appendToCopy(super.getNamedXContents(), new NamedXContentRegistry.Entry(Aggregation.class, - new ParseField(InferencePipelineAggregationBuilder.NAME), (p, c) -> ParsedInference.fromXContent(p, (String) c))); + return CollectionUtils.appendToCopy( + super.getNamedXContents(), + new NamedXContentRegistry.Entry( + Aggregation.class, + new ParseField(InferencePipelineAggregationBuilder.NAME), + (p, c) -> ParsedInference.fromXContent(p, (String) c) + ) + ); } @Override @@ -73,10 +79,7 @@ protected InternalInferenceAggregation createTestInstance(String name, Map internalAgg.getProperty(Arrays.asList("one", "two"))); + InvalidAggregationPathException e = expectThrows( + InvalidAggregationPathException.class, + () -> internalAgg.getProperty(Arrays.asList("one", "two")) + ); String message = "unknown property [one, two] for inference aggregation [" + internalAgg.getName() + "]"; assertEquals(message, e.getMessage()); @@ -144,8 +149,10 @@ public void testGetProperty_givenTooLongPath() { public void testGetProperty_givenWrongPath() { InternalInferenceAggregation internalAgg = createTestInstance(); - InvalidAggregationPathException e = expectThrows(InvalidAggregationPathException.class, - () -> internalAgg.getProperty(Collections.singletonList("bar"))); + InvalidAggregationPathException e = expectThrows( + InvalidAggregationPathException.class, + () -> internalAgg.getProperty(Collections.singletonList("bar")) + ); String message = "unknown property [bar] for inference aggregation [" + internalAgg.getName() + "]"; assertEquals(message, e.getMessage()); @@ -175,22 +182,28 @@ public void testGetProperty_featureImportance() { { ClassificationInferenceResults results = ClassificationInferenceResultsTests.createRandomResults(); InternalInferenceAggregation internalAgg = new InternalInferenceAggregation("foo", Collections.emptyMap(), results); - expectThrows(InvalidAggregationPathException.class, - () -> internalAgg.getProperty(Collections.singletonList("feature_importance"))); + expectThrows( + InvalidAggregationPathException.class, + () -> internalAgg.getProperty(Collections.singletonList("feature_importance")) + ); } { RegressionInferenceResults results = RegressionInferenceResultsTests.createRandomResults(); InternalInferenceAggregation internalAgg = new InternalInferenceAggregation("foo", Collections.emptyMap(), results); - expectThrows(InvalidAggregationPathException.class, - () -> internalAgg.getProperty(Collections.singletonList("feature_importance"))); + expectThrows( + InvalidAggregationPathException.class, + () -> internalAgg.getProperty(Collections.singletonList("feature_importance")) + ); } { WarningInferenceResults results = new WarningInferenceResults("a warning from history"); InternalInferenceAggregation internalAgg = new InternalInferenceAggregation("foo", Collections.emptyMap(), results); - expectThrows(InvalidAggregationPathException.class, - () -> internalAgg.getProperty(Collections.singletonList("feature_importance"))); + expectThrows( + InvalidAggregationPathException.class, + () -> internalAgg.getProperty(Collections.singletonList("feature_importance")) + ); } } @@ -198,22 +211,19 @@ public void testGetProperty_topClasses() { { ClassificationInferenceResults results = ClassificationInferenceResultsTests.createRandomResults(); InternalInferenceAggregation internalAgg = new InternalInferenceAggregation("foo", Collections.emptyMap(), results); - expectThrows(InvalidAggregationPathException.class, - () -> internalAgg.getProperty(Collections.singletonList("top_classes"))); + expectThrows(InvalidAggregationPathException.class, () -> internalAgg.getProperty(Collections.singletonList("top_classes"))); } { RegressionInferenceResults results = RegressionInferenceResultsTests.createRandomResults(); InternalInferenceAggregation internalAgg = new InternalInferenceAggregation("foo", Collections.emptyMap(), results); - expectThrows(InvalidAggregationPathException.class, - () -> internalAgg.getProperty(Collections.singletonList("top_classes"))); + expectThrows(InvalidAggregationPathException.class, () -> internalAgg.getProperty(Collections.singletonList("top_classes"))); } { WarningInferenceResults results = new WarningInferenceResults("a warning from history"); InternalInferenceAggregation internalAgg = new InternalInferenceAggregation("foo", Collections.emptyMap(), results); - expectThrows(InvalidAggregationPathException.class, - () -> internalAgg.getProperty(Collections.singletonList("top_classes"))); + expectThrows(InvalidAggregationPathException.class, () -> internalAgg.getProperty(Collections.singletonList("top_classes"))); } } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/inference/ParsedInference.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/inference/ParsedInference.java index 8009e9a4b8533..fa275b3f9f400 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/inference/ParsedInference.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/inference/ParsedInference.java @@ -7,13 +7,13 @@ package org.elasticsearch.xpack.ml.aggs.inference; -import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.search.aggregations.ParsedAggregation; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParseException; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.search.aggregations.ParsedAggregation; import org.elasticsearch.xpack.core.ml.inference.results.SingleValueInferenceResults; import org.elasticsearch.xpack.core.ml.inference.results.TopClassEntry; import org.elasticsearch.xpack.core.ml.inference.results.WarningInferenceResults; @@ -28,7 +28,6 @@ import static org.elasticsearch.xpack.core.ml.inference.results.ClassificationInferenceResults.PREDICTION_SCORE; import static org.elasticsearch.xpack.core.ml.inference.results.SingleValueInferenceResults.FEATURE_IMPORTANCE; - /** * There isn't enough information in toXContent representation of the * {@link org.elasticsearch.xpack.core.ml.inference.results.InferenceResults} @@ -44,10 +43,18 @@ public class ParsedInference extends ParsedAggregation { @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(ParsedInference.class.getSimpleName(), true, - args -> new ParsedInference(args[0], (List>) args[1], - (List) args[2], (String) args[3], (Double) args[4], (Double) args[5])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + ParsedInference.class.getSimpleName(), + true, + args -> new ParsedInference( + args[0], + (List>) args[1], + (List) args[2], + (String) args[3], + (Double) args[4], + (Double) args[5] + ) + ); static { PARSER.declareField(optionalConstructorArg(), (p, n) -> { @@ -60,16 +67,30 @@ public class ParsedInference extends ParsedAggregation { } else if (token == XContentParser.Token.VALUE_NUMBER) { o = p.doubleValue(); } else { - throw new XContentParseException(p.getTokenLocation(), - "[" + ParsedInference.class.getSimpleName() + "] failed to parse field [" + CommonFields.VALUE + "] " - + "value [" + token + "] is not a string, boolean or number"); + throw new XContentParseException( + p.getTokenLocation(), + "[" + + ParsedInference.class.getSimpleName() + + "] failed to parse field [" + + CommonFields.VALUE + + "] " + + "value [" + + token + + "] is not a string, boolean or number" + ); } return o; }, CommonFields.VALUE, ObjectParser.ValueType.VALUE); - PARSER.declareObjectArray(optionalConstructorArg(), (p, c) -> p.map(), - new ParseField(SingleValueInferenceResults.FEATURE_IMPORTANCE)); - PARSER.declareObjectArray(optionalConstructorArg(), (p, c) -> TopClassEntry.fromXContent(p), - new ParseField(ClassificationConfig.DEFAULT_TOP_CLASSES_RESULTS_FIELD)); + PARSER.declareObjectArray( + optionalConstructorArg(), + (p, c) -> p.map(), + new ParseField(SingleValueInferenceResults.FEATURE_IMPORTANCE) + ); + PARSER.declareObjectArray( + optionalConstructorArg(), + (p, c) -> TopClassEntry.fromXContent(p), + new ParseField(ClassificationConfig.DEFAULT_TOP_CLASSES_RESULTS_FIELD) + ); PARSER.declareString(optionalConstructorArg(), new ParseField(WarningInferenceResults.NAME)); PARSER.declareDouble(optionalConstructorArg(), new ParseField(PREDICTION_PROBABILITY)); PARSER.declareDouble(optionalConstructorArg(), new ParseField(PREDICTION_SCORE)); @@ -89,12 +110,14 @@ public static ParsedInference fromXContent(XContentParser parser, final String n private final Double predictionProbability; private final Double predictionScore; - ParsedInference(Object value, - List> featureImportance, - List topClasses, - String warning, - Double predictionProbability, - Double predictionScore) { + ParsedInference( + Object value, + List> featureImportance, + List topClasses, + String warning, + Double predictionProbability, + Double predictionScore + ) { this.value = value; this.warning = warning; this.featureImportance = featureImportance; diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/kstest/BucketCountKSTestAggregatorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/kstest/BucketCountKSTestAggregatorTests.java index 424cc52beb680..696b448ca904a 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/kstest/BucketCountKSTestAggregatorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/kstest/BucketCountKSTestAggregatorTests.java @@ -45,10 +45,7 @@ public class BucketCountKSTestAggregatorTests extends ESTestCase { new double[] { 1, 2, 2, 6, 7, 7, 7, 6, 6, 7 } ); - private static Map runKsTestAndValidate( - MlAggsHelper.DoubleBucketValues bucketValues, - SamplingMethod samplingMethod - ) { + private static Map runKsTestAndValidate(MlAggsHelper.DoubleBucketValues bucketValues, SamplingMethod samplingMethod) { Map ksTestValues = BucketCountKSTestAggregator.ksTest( UNIFORM_FRACTIONS, bucketValues, @@ -115,15 +112,9 @@ public void testKsTest_LowerTailedValues() { // its difficult to make sure things are super close in the sparser case as the sparser data is more "uniform" // Having error of 0.25 allows for this. But, the two values should be similar as the distributions are "close" for (String alternative : Arrays.stream(Alternative.values()).map(Alternative::toString).collect(Collectors.toList())) { - assertThat(alternative, - lessValsLowerSampled.get(alternative), - closeTo(lessValsLowerSampledSparsed.get(alternative), 0.25)); - assertThat(alternative, - lessValsUpperSampled.get(alternative), - closeTo(lessValsUpperSampledSparsed.get(alternative), 0.25)); - assertThat(alternative, - lessValsUniformSampled.get(alternative), - closeTo(lessValsUniformSampledSparsed.get(alternative), 0.25)); + assertThat(alternative, lessValsLowerSampled.get(alternative), closeTo(lessValsLowerSampledSparsed.get(alternative), 0.25)); + assertThat(alternative, lessValsUpperSampled.get(alternative), closeTo(lessValsUpperSampledSparsed.get(alternative), 0.25)); + assertThat(alternative, lessValsUniformSampled.get(alternative), closeTo(lessValsUniformSampledSparsed.get(alternative), 0.25)); } } @@ -160,15 +151,21 @@ public void testKsTest_UpperTailedValues() { // its difficult to make sure things are super close in the sparser case as the sparser data is more "uniform" // Having error of 0.25 allows for this. But, the two values should be similar as the distributions are "close" for (String alternative : Arrays.stream(Alternative.values()).map(Alternative::toString).collect(Collectors.toList())) { - assertThat(alternative, + assertThat( + alternative, greaterValsLowerSampled.get(alternative), - closeTo(greaterValsLowerSampledSparsed.get(alternative), 0.25)); - assertThat(alternative, + closeTo(greaterValsLowerSampledSparsed.get(alternative), 0.25) + ); + assertThat( + alternative, greaterValsUpperSampled.get(alternative), - closeTo(greaterValsUpperSampledSparsed.get(alternative), 0.25)); - assertThat(alternative, + closeTo(greaterValsUpperSampledSparsed.get(alternative), 0.25) + ); + assertThat( + alternative, greaterValsUniformSampled.get(alternative), - closeTo(greaterValsUniformSampledSparsed.get(alternative), 0.25)); + closeTo(greaterValsUniformSampledSparsed.get(alternative), 0.25) + ); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/kstest/InternalKSTestAggregationTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/kstest/InternalKSTestAggregationTests.java index 8f3718b56dc19..3a7712cf304b4 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/kstest/InternalKSTestAggregationTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/kstest/InternalKSTestAggregationTests.java @@ -37,7 +37,8 @@ protected List getNamedXContents() { super.getNamedXContents(), new NamedXContentRegistry.Entry( Aggregation.class, - BucketCountKSTestAggregationBuilder.NAME, (p, c) -> ParsedKSTest.fromXContent(p, (String) c) + BucketCountKSTestAggregationBuilder.NAME, + (p, c) -> ParsedKSTest.fromXContent(p, (String) c) ) ); } @@ -46,7 +47,9 @@ protected List getNamedXContents() { protected InternalKSTestAggregation createTestInstance(String name, Map metadata) { List modes = randomSubsetOf(Arrays.stream(Alternative.values()).map(Alternative::toString).collect(Collectors.toList())); return new InternalKSTestAggregation( - name, metadata, modes.stream().collect(Collectors.toMap(Function.identity(), a -> randomDouble())) + name, + metadata, + modes.stream().collect(Collectors.toMap(Function.identity(), a -> randomDouble())) ); } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/kstest/ParsedKSTest.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/kstest/ParsedKSTest.java index ed47b5d52498e..bd3c45d8181de 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/kstest/ParsedKSTest.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/aggs/kstest/ParsedKSTest.java @@ -16,7 +16,6 @@ import java.util.HashMap; import java.util.Map; - public class ParsedKSTest extends ParsedAggregation { @SuppressWarnings("unchecked") @@ -24,14 +23,14 @@ public static ParsedKSTest fromXContent(XContentParser parser, final String name Map values = parser.map(); Map doubleValues = new HashMap<>(values.size(), 1.0f); for (Alternative alternative : Alternative.values()) { - Double value = (Double)values.get(alternative.toString()); + Double value = (Double) values.get(alternative.toString()); if (value != null) { doubleValues.put(alternative.toString(), value); } } ParsedKSTest parsed = new ParsedKSTest( doubleValues, - (Map)values.get(InternalAggregation.CommonFields.META.getPreferredName()) + (Map) values.get(InternalAggregation.CommonFields.META.getPreferredName()) ); parsed.setName(name); return parsed; diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/annotations/AnnotationPersisterTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/annotations/AnnotationPersisterTests.java index 09afae243dbf7..053eebd3705bb 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/annotations/AnnotationPersisterTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/annotations/AnnotationPersisterTests.java @@ -18,13 +18,13 @@ import org.elasticsearch.client.Client; import org.elasticsearch.client.OriginSettingClient; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.core.Tuple; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ClientHelper; import org.elasticsearch.xpack.core.ml.annotations.Annotation; import org.elasticsearch.xpack.core.ml.annotations.AnnotationIndex; @@ -87,8 +87,8 @@ public void verifyNoMoreInteractionsWithMocks() { } public void testPersistAnnotation_Create() throws IOException { - doAnswer(withResponse(new BulkResponse(new BulkItemResponse[]{ bulkItemSuccess(ANNOTATION_ID) }, 0L))) - .when(client).execute(eq(BulkAction.INSTANCE), any(), any()); + doAnswer(withResponse(new BulkResponse(new BulkItemResponse[] { bulkItemSuccess(ANNOTATION_ID) }, 0L))).when(client) + .execute(eq(BulkAction.INSTANCE), any(), any()); AnnotationPersister persister = new AnnotationPersister(resultsPersisterService); Annotation annotation = AnnotationTests.randomAnnotation(JOB_ID); @@ -110,8 +110,8 @@ public void testPersistAnnotation_Create() throws IOException { } public void testPersistAnnotation_Update() throws IOException { - doAnswer(withResponse(new BulkResponse(new BulkItemResponse[]{ bulkItemSuccess(ANNOTATION_ID) }, 0L))) - .when(client).execute(eq(BulkAction.INSTANCE), any(), any()); + doAnswer(withResponse(new BulkResponse(new BulkItemResponse[] { bulkItemSuccess(ANNOTATION_ID) }, 0L))).when(client) + .execute(eq(BulkAction.INSTANCE), any(), any()); AnnotationPersister persister = new AnnotationPersister(resultsPersisterService); Annotation annotation = AnnotationTests.randomAnnotation(JOB_ID); @@ -133,8 +133,8 @@ public void testPersistAnnotation_Update() throws IOException { } public void testPersistMultipleAnnotationsWithBulk() { - doAnswer(withResponse(new BulkResponse(new BulkItemResponse[]{ bulkItemSuccess(ANNOTATION_ID) }, 0L))) - .when(client).execute(eq(BulkAction.INSTANCE), any(), any()); + doAnswer(withResponse(new BulkResponse(new BulkItemResponse[] { bulkItemSuccess(ANNOTATION_ID) }, 0L))).when(client) + .execute(eq(BulkAction.INSTANCE), any(), any()); AnnotationPersister persister = new AnnotationPersister(resultsPersisterService); persister.bulkPersisterBuilder(JOB_ID) @@ -153,8 +153,8 @@ public void testPersistMultipleAnnotationsWithBulk() { } public void testPersistMultipleAnnotationsWithBulk_LowBulkLimit() { - doAnswer(withResponse(new BulkResponse(new BulkItemResponse[]{ bulkItemSuccess(ANNOTATION_ID) }, 0L))) - .when(client).execute(eq(BulkAction.INSTANCE), any(), any()); + doAnswer(withResponse(new BulkResponse(new BulkItemResponse[] { bulkItemSuccess(ANNOTATION_ID) }, 0L))).when(client) + .execute(eq(BulkAction.INSTANCE), any(), any()); AnnotationPersister persister = new AnnotationPersister(resultsPersisterService, 2); persister.bulkPersisterBuilder(JOB_ID) @@ -180,10 +180,11 @@ public void testPersistMultipleAnnotationsWithBulk_EmptyRequest() { } public void testPersistMultipleAnnotationsWithBulk_Failure() { - doAnswer(withResponse(new BulkResponse(new BulkItemResponse[]{bulkItemFailure("1"), bulkItemFailure("2")}, 0L))) // (1) - .doAnswer(withResponse(new BulkResponse(new BulkItemResponse[]{bulkItemSuccess("1"), bulkItemFailure("2")}, 0L))) // (2) - .doAnswer(withResponse(new BulkResponse(new BulkItemResponse[]{bulkItemFailure("2")}, 0L))) // (3) - .when(client).execute(eq(BulkAction.INSTANCE), any(), any()); + doAnswer(withResponse(new BulkResponse(new BulkItemResponse[] { bulkItemFailure("1"), bulkItemFailure("2") }, 0L))) // (1) + .doAnswer(withResponse(new BulkResponse(new BulkItemResponse[] { bulkItemSuccess("1"), bulkItemFailure("2") }, 0L))) // (2) + .doAnswer(withResponse(new BulkResponse(new BulkItemResponse[] { bulkItemFailure("2") }, 0L))) // (3) + .when(client) + .execute(eq(BulkAction.INSTANCE), any(), any()); AnnotationPersister persister = new AnnotationPersister(resultsPersisterService); AnnotationPersister.Builder persisterBuilder = persister.bulkPersisterBuilder(JOB_ID) @@ -215,14 +216,16 @@ private static BulkItemResponse bulkItemSuccess(String docId) { return BulkItemResponse.success( 1, DocWriteRequest.OpType.INDEX, - new IndexResponse(new ShardId(AnnotationIndex.WRITE_ALIAS_NAME, "uuid", 1), docId, 0, 0, 1, true)); + new IndexResponse(new ShardId(AnnotationIndex.WRITE_ALIAS_NAME, "uuid", 1), docId, 0, 0, 1, true) + ); } private static BulkItemResponse bulkItemFailure(String docId) { return BulkItemResponse.failure( 2, DocWriteRequest.OpType.INDEX, - new BulkItemResponse.Failure("my-index", docId, new Exception("boom"))); + new BulkItemResponse.Failure("my-index", docId, new Exception("boom")) + ); } private Annotation parseAnnotation(BytesReference source) throws IOException { diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/autoscaling/MlAutoscalingDeciderServiceTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/autoscaling/MlAutoscalingDeciderServiceTests.java index 9f5f82ce848de..ea9d8359b4c8e 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/autoscaling/MlAutoscalingDeciderServiceTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/autoscaling/MlAutoscalingDeciderServiceTests.java @@ -67,7 +67,7 @@ public class MlAutoscalingDeciderServiceTests extends ESTestCase { private static final long DEFAULT_NODE_SIZE = ByteSizeValue.ofGb(20).getBytes(); - private static final long DEFAULT_JVM_SIZE = ByteSizeValue.ofMb((long)(DEFAULT_NODE_SIZE * 0.25)).getBytes(); + private static final long DEFAULT_JVM_SIZE = ByteSizeValue.ofMb((long) (DEFAULT_NODE_SIZE * 0.25)).getBytes(); private static final long DEFAULT_JOB_SIZE = ByteSizeValue.ofMb(200).getBytes(); private static final long DEFAULT_MODEL_SIZE = ByteSizeValue.ofMb(200).getBytes(); private static final long OVERHEAD = ByteSizeValue.ofMb(30).getBytes(); @@ -87,28 +87,30 @@ public void setup() { when(mlMemoryTracker.getTrainedModelAllocationMemoryRequirement(any())).thenReturn(DEFAULT_JOB_SIZE); nodeLoadDetector = mock(NodeLoadDetector.class); when(nodeLoadDetector.getMlMemoryTracker()).thenReturn(mlMemoryTracker); - when(nodeLoadDetector.detectNodeLoad(any(), any(), anyInt(), anyInt(), anyBoolean())) - .thenReturn(NodeLoad.builder("any") - .setUseMemory(true) - .incAssignedJobMemory(ByteSizeValue.ofGb(1).getBytes()) - .build()); + when(nodeLoadDetector.detectNodeLoad(any(), any(), anyInt(), anyInt(), anyBoolean())).thenReturn( + NodeLoad.builder("any").setUseMemory(true).incAssignedJobMemory(ByteSizeValue.ofGb(1).getBytes()).build() + ); clusterService = mock(ClusterService.class); settings = Settings.EMPTY; timeSupplier = System::currentTimeMillis; ClusterSettings cSettings = new ClusterSettings( Settings.EMPTY, - Set.of(MachineLearning.MAX_MACHINE_MEMORY_PERCENT, + Set.of( + MachineLearning.MAX_MACHINE_MEMORY_PERCENT, MachineLearning.MAX_OPEN_JOBS_PER_NODE, - MachineLearning.USE_AUTO_MACHINE_MEMORY_PERCENT)); + MachineLearning.USE_AUTO_MACHINE_MEMORY_PERCENT + ) + ); when(clusterService.getClusterSettings()).thenReturn(cSettings); } public void testScale_whenNotOnMaster() { MlAutoscalingDeciderService service = buildService(); service.offMaster(); - IllegalArgumentException iae = expectThrows(IllegalArgumentException.class, - () -> service.scale(Settings.EMPTY, - mock(AutoscalingDeciderContext.class))); + IllegalArgumentException iae = expectThrows( + IllegalArgumentException.class, + () -> service.scale(Settings.EMPTY, mock(AutoscalingDeciderContext.class)) + ); assertThat(iae.getMessage(), equalTo("request for scaling information is only allowed on the master node")); } @@ -116,17 +118,20 @@ public void testScaleUp_withNoJobsWaiting() { MlAutoscalingDeciderService service = buildService(); service.onMaster(); - assertThat(service.checkForScaleUp( - 0, - 0, - Collections.emptyList(), - Collections.emptyList(), - Collections.emptyList(), - Collections.emptyList(), - null, - NativeMemoryCapacity.ZERO, - MlScalingReason.builder()), - equalTo(Optional.empty())); + assertThat( + service.checkForScaleUp( + 0, + 0, + Collections.emptyList(), + Collections.emptyList(), + Collections.emptyList(), + Collections.emptyList(), + null, + NativeMemoryCapacity.ZERO, + MlScalingReason.builder() + ), + equalTo(Optional.empty()) + ); } public void testScaleUp_withWaitingJobsAndAutoMemoryAndNoRoomInNodes() { @@ -134,12 +139,14 @@ public void testScaleUp_withWaitingJobsAndAutoMemoryAndNoRoomInNodes() { when(mlMemoryTracker.getDataFrameAnalyticsJobMemoryRequirement(any())).thenReturn(ByteSizeValue.ofGb(2).getBytes()); List jobTasks = Arrays.asList("waiting_job", "waiting_job_2"); List analytics = Arrays.asList("analytics_waiting"); - List fullyLoadedNode = Arrays.asList(NodeLoad.builder("any") - .setMaxMemory(ByteSizeValue.ofGb(1).getBytes()) - .setUseMemory(true) - .incAssignedJobMemory(ByteSizeValue.ofGb(1).getBytes()).build()); - MlScalingReason.Builder reasonBuilder = new MlScalingReason.Builder() - .setPassedConfiguration(Settings.EMPTY) + List fullyLoadedNode = Arrays.asList( + NodeLoad.builder("any") + .setMaxMemory(ByteSizeValue.ofGb(1).getBytes()) + .setUseMemory(true) + .incAssignedJobMemory(ByteSizeValue.ofGb(1).getBytes()) + .build() + ); + MlScalingReason.Builder reasonBuilder = new MlScalingReason.Builder().setPassedConfiguration(Settings.EMPTY) .setCurrentMlCapacity(AutoscalingCapacity.ZERO); MlAutoscalingDeciderService service = buildService(); service.setUseAuto(true); @@ -153,7 +160,8 @@ public void testScaleUp_withWaitingJobsAndAutoMemoryAndNoRoomInNodes() { Collections.emptyList(), null, NativeMemoryCapacity.ZERO, - reasonBuilder); + reasonBuilder + ); assertFalse(decision.isEmpty()); AutoscalingDeciderResult result = decision.get(); long allowedBytesForMlNode = NativeMemoryCalculator.allowedBytesForMl( @@ -170,14 +178,17 @@ public void testScaleUp_withWaitingJobsAndAutoMemoryAndNoRoomInNodes() { assertThat(allowedBytesForMlTier, greaterThanOrEqualTo(ByteSizeValue.ofGb(2).getBytes() * 3 + OVERHEAD)); } { // we allow one job in the analytics queue - Optional decision = service.checkForScaleUp(0, 1, + Optional decision = service.checkForScaleUp( + 0, + 1, fullyLoadedNode, jobTasks, analytics, Collections.emptyList(), null, NativeMemoryCapacity.ZERO, - reasonBuilder); + reasonBuilder + ); assertFalse(decision.isEmpty()); AutoscalingDeciderResult result = decision.get(); long allowedBytesForMlNode = NativeMemoryCalculator.allowedBytesForMl( @@ -194,14 +205,17 @@ public void testScaleUp_withWaitingJobsAndAutoMemoryAndNoRoomInNodes() { assertThat(allowedBytesForMlTier, greaterThanOrEqualTo(ByteSizeValue.ofGb(2).getBytes() * 2 + OVERHEAD)); } { // we allow one job in the anomaly queue and analytics queue - Optional decision = service.checkForScaleUp(1, 1, + Optional decision = service.checkForScaleUp( + 1, + 1, fullyLoadedNode, jobTasks, analytics, Collections.emptyList(), null, NativeMemoryCapacity.ZERO, - reasonBuilder); + reasonBuilder + ); assertFalse(decision.isEmpty()); AutoscalingDeciderResult result = decision.get(); long allowedBytesForMlNode = NativeMemoryCalculator.allowedBytesForMl( @@ -222,55 +236,62 @@ public void testScaleUp_withWaitingJobsAndAutoMemoryAndNoRoomInNodes() { public void testScaleUp_withWaitingJobsAndRoomInNodes() { List jobTasks = Arrays.asList("waiting_job", "waiting_job_2"); List analytics = Arrays.asList("analytics_waiting"); - MlScalingReason.Builder reasonBuilder = new MlScalingReason.Builder() - .setPassedConfiguration(Settings.EMPTY) + MlScalingReason.Builder reasonBuilder = new MlScalingReason.Builder().setPassedConfiguration(Settings.EMPTY) .setCurrentMlCapacity(AutoscalingCapacity.ZERO); - List nodesWithRoom = Arrays.asList(NodeLoad.builder("partially_filled") - .setMaxMemory(ByteSizeValue.ofMb(430).getBytes()) - .setUseMemory(true) - .setMaxJobs(10) - .incNumAssignedJobs() - .incAssignedJobMemory(ByteSizeValue.ofMb(230).getBytes()).build(), - NodeLoad.builder("not_filled") - .setMaxMemory(ByteSizeValue.ofMb(230).getBytes()) + List nodesWithRoom = Arrays.asList( + NodeLoad.builder("partially_filled") + .setMaxMemory(ByteSizeValue.ofMb(430).getBytes()) + .setUseMemory(true) .setMaxJobs(10) - .setUseMemory(true).build()); + .incNumAssignedJobs() + .incAssignedJobMemory(ByteSizeValue.ofMb(230).getBytes()) + .build(), + NodeLoad.builder("not_filled").setMaxMemory(ByteSizeValue.ofMb(230).getBytes()).setMaxJobs(10).setUseMemory(true).build() + ); MlAutoscalingDeciderService service = buildService(); service.setMaxMachineMemoryPercent(25); { // No time in queue, should be able to assign all but one job given the current node load - Optional decision = service.checkForScaleUp(0, 0, + Optional decision = service.checkForScaleUp( + 0, + 0, nodesWithRoom, jobTasks, analytics, Collections.emptyList(), null, NativeMemoryCapacity.ZERO, - reasonBuilder); + reasonBuilder + ); assertTrue(decision.isPresent()); assertThat(decision.get().requiredCapacity().node().memory().getBytes(), equalTo((DEFAULT_JOB_SIZE + OVERHEAD) * 4)); - assertThat(decision.get().requiredCapacity().total().memory().getBytes(), - equalTo(4 * (DEFAULT_JOB_SIZE + OVERHEAD))); + assertThat(decision.get().requiredCapacity().total().memory().getBytes(), equalTo(4 * (DEFAULT_JOB_SIZE + OVERHEAD))); } { // we allow one job in the analytics queue - Optional decision = service.checkForScaleUp(0, 1, + Optional decision = service.checkForScaleUp( + 0, + 1, nodesWithRoom, jobTasks, analytics, Collections.emptyList(), null, NativeMemoryCapacity.ZERO, - reasonBuilder); + reasonBuilder + ); assertFalse(decision.isPresent()); } { // we allow one job in the anomaly queue - Optional decision = service.checkForScaleUp(1, 0, + Optional decision = service.checkForScaleUp( + 1, + 0, nodesWithRoom, jobTasks, analytics, Collections.emptyList(), null, NativeMemoryCapacity.ZERO, - reasonBuilder); + reasonBuilder + ); assertFalse(decision.isPresent()); } } @@ -278,52 +299,61 @@ public void testScaleUp_withWaitingJobsAndRoomInNodes() { public void testScaleUp_withWaitingJobsAndNoRoomInNodes() { List jobTasks = Arrays.asList("waiting_job", "waiting_job_2"); List analytics = Arrays.asList("analytics_waiting"); - MlScalingReason.Builder reasonBuilder = new MlScalingReason.Builder() - .setPassedConfiguration(Settings.EMPTY) + MlScalingReason.Builder reasonBuilder = new MlScalingReason.Builder().setPassedConfiguration(Settings.EMPTY) .setCurrentMlCapacity(AutoscalingCapacity.ZERO); - List fullyLoadedNode = Arrays.asList(NodeLoad.builder("any") - .setMaxMemory(ByteSizeValue.ofGb(1).getBytes()) - .setUseMemory(true) - .incAssignedJobMemory(ByteSizeValue.ofGb(1).getBytes()).build()); + List fullyLoadedNode = Arrays.asList( + NodeLoad.builder("any") + .setMaxMemory(ByteSizeValue.ofGb(1).getBytes()) + .setUseMemory(true) + .incAssignedJobMemory(ByteSizeValue.ofGb(1).getBytes()) + .build() + ); MlAutoscalingDeciderService service = buildService(); service.setMaxMachineMemoryPercent(25); { // No time in queue - Optional decision = service.checkForScaleUp(0, 0, + Optional decision = service.checkForScaleUp( + 0, + 0, fullyLoadedNode, jobTasks, analytics, Collections.emptyList(), null, NativeMemoryCapacity.ZERO, - reasonBuilder); + reasonBuilder + ); assertFalse(decision.isEmpty()); assertThat(decision.get().requiredCapacity().node().memory().getBytes(), equalTo((DEFAULT_JOB_SIZE + OVERHEAD) * 4)); - assertThat(decision.get().requiredCapacity().total().memory().getBytes(), - equalTo(4 * (3 * DEFAULT_JOB_SIZE + OVERHEAD))); + assertThat(decision.get().requiredCapacity().total().memory().getBytes(), equalTo(4 * (3 * DEFAULT_JOB_SIZE + OVERHEAD))); } { // we allow one job in the analytics queue - Optional decision = service.checkForScaleUp(0, 1, + Optional decision = service.checkForScaleUp( + 0, + 1, fullyLoadedNode, jobTasks, analytics, Collections.emptyList(), null, NativeMemoryCapacity.ZERO, - reasonBuilder); + reasonBuilder + ); assertFalse(decision.isEmpty()); assertThat(decision.get().requiredCapacity().node().memory().getBytes(), equalTo(4 * (DEFAULT_JOB_SIZE + OVERHEAD))); - assertThat(decision.get().requiredCapacity().total().memory().getBytes(), - equalTo(4 * (2 * DEFAULT_JOB_SIZE + OVERHEAD))); + assertThat(decision.get().requiredCapacity().total().memory().getBytes(), equalTo(4 * (2 * DEFAULT_JOB_SIZE + OVERHEAD))); } { // we allow one job in the anomaly queue and analytics queue - Optional decision = service.checkForScaleUp(1, 1, + Optional decision = service.checkForScaleUp( + 1, + 1, fullyLoadedNode, jobTasks, analytics, Collections.emptyList(), null, NativeMemoryCapacity.ZERO, - reasonBuilder); + reasonBuilder + ); assertFalse(decision.isEmpty()); assertThat(decision.get().requiredCapacity().node().memory().getBytes(), equalTo(4 * (DEFAULT_JOB_SIZE + OVERHEAD))); assertThat(decision.get().requiredCapacity().total().memory().getBytes(), equalTo(4 * (DEFAULT_JOB_SIZE + OVERHEAD))); @@ -333,48 +363,59 @@ public void testScaleUp_withWaitingJobsAndNoRoomInNodes() { public void testScaleUp_withWaitingJobs_WithFutureCapacity() { List jobTasks = Arrays.asList("waiting_job", "waiting_job_2"); List analytics = Arrays.asList("analytics_waiting"); - MlScalingReason.Builder reasonBuilder = new MlScalingReason.Builder() - .setPassedConfiguration(Settings.EMPTY) + MlScalingReason.Builder reasonBuilder = new MlScalingReason.Builder().setPassedConfiguration(Settings.EMPTY) .setCurrentMlCapacity(AutoscalingCapacity.ZERO); - List fullyLoadedNode = Arrays.asList(NodeLoad.builder("any") - .setMaxMemory(ByteSizeValue.ofGb(1).getBytes()) - .setUseMemory(true) - .incAssignedJobMemory(ByteSizeValue.ofGb(1).getBytes()).build()); + List fullyLoadedNode = Arrays.asList( + NodeLoad.builder("any") + .setMaxMemory(ByteSizeValue.ofGb(1).getBytes()) + .setUseMemory(true) + .incAssignedJobMemory(ByteSizeValue.ofGb(1).getBytes()) + .build() + ); MlAutoscalingDeciderService service = buildService(); service.setMaxMachineMemoryPercent(25); { // with null future capacity and current capacity has a small node - Optional decision = service.checkForScaleUp(2, 1, + Optional decision = service.checkForScaleUp( + 2, + 1, fullyLoadedNode, jobTasks, analytics, Collections.emptyList(), null, NativeMemoryCapacity.ZERO, - reasonBuilder); + reasonBuilder + ); assertFalse(decision.isEmpty()); assertThat(decision.get().requiredCapacity().node().memory().getBytes(), equalTo(DEFAULT_JOB_SIZE * 4)); assertThat(decision.get().requiredCapacity().total().memory().getBytes(), equalTo(DEFAULT_JOB_SIZE * 4)); } { - Optional decision = service.checkForScaleUp(2, 1, + Optional decision = service.checkForScaleUp( + 2, + 1, fullyLoadedNode, jobTasks, analytics, Collections.emptyList(), new NativeMemoryCapacity(ByteSizeValue.ofGb(3).getBytes(), ByteSizeValue.ofGb(1).getBytes()), new NativeMemoryCapacity(ByteSizeValue.ofGb(2).getBytes(), ByteSizeValue.ofGb(2).getBytes()), - reasonBuilder); + reasonBuilder + ); assertTrue(decision.isEmpty()); } { - Optional decision = service.checkForScaleUp(2, 1, + Optional decision = service.checkForScaleUp( + 2, + 1, fullyLoadedNode, jobTasks, analytics, Collections.emptyList(), new NativeMemoryCapacity(ByteSizeValue.ofMb(1).getBytes(), ByteSizeValue.ofMb(1).getBytes()), new NativeMemoryCapacity(ByteSizeValue.ofGb(2).getBytes(), ByteSizeValue.ofGb(2).getBytes()), - reasonBuilder); + reasonBuilder + ); assertFalse(decision.isEmpty()); assertThat(decision.get().requiredCapacity().node().memory().getBytes(), equalTo(ByteSizeValue.ofGb(8).getBytes())); assertThat(decision.get().requiredCapacity().total().memory().getBytes(), equalTo(ByteSizeValue.ofMb(8992).getBytes())); @@ -474,38 +515,45 @@ public void testScaleDown() { MlAutoscalingDeciderService service = buildService(); service.setMaxMachineMemoryPercent(25); - MlScalingReason.Builder reasonBuilder = new MlScalingReason.Builder() - .setPassedConfiguration(Settings.EMPTY) + MlScalingReason.Builder reasonBuilder = new MlScalingReason.Builder().setPassedConfiguration(Settings.EMPTY) .setCurrentMlCapacity(AutoscalingCapacity.ZERO); - {//Current capacity allows for smaller node - Optional result = service.checkForScaleDown(nodeLoads, + {// Current capacity allows for smaller node + Optional result = service.checkForScaleDown( + nodeLoads, ByteSizeValue.ofMb(100).getBytes(), new NativeMemoryCapacity(ByteSizeValue.ofGb(3).getBytes(), ByteSizeValue.ofGb(1).getBytes()), - reasonBuilder); + reasonBuilder + ); assertThat(result.isEmpty(), is(false)); AutoscalingDeciderResult autoscalingDeciderResult = result.get(); - assertThat(autoscalingDeciderResult.requiredCapacity().node().memory().getBytes(), - equalTo((ByteSizeValue.ofMb(100).getBytes() + OVERHEAD) * 4)); - assertThat(autoscalingDeciderResult.requiredCapacity().total().memory().getBytes(), - equalTo(ByteSizeValue.ofGb(12).getBytes())); + assertThat( + autoscalingDeciderResult.requiredCapacity().node().memory().getBytes(), + equalTo((ByteSizeValue.ofMb(100).getBytes() + OVERHEAD) * 4) + ); + assertThat(autoscalingDeciderResult.requiredCapacity().total().memory().getBytes(), equalTo(ByteSizeValue.ofGb(12).getBytes())); } {// Current capacity allows for smaller tier - Optional result = service.checkForScaleDown(nodeLoads, + Optional result = service.checkForScaleDown( + nodeLoads, ByteSizeValue.ofMb(100).getBytes(), new NativeMemoryCapacity(ByteSizeValue.ofGb(4).getBytes(), ByteSizeValue.ofGb(1).getBytes()), - reasonBuilder); + reasonBuilder + ); assertThat(result.isEmpty(), is(false)); AutoscalingDeciderResult autoscalingDeciderResult = result.get(); - assertThat(autoscalingDeciderResult.requiredCapacity().node().memory().getBytes(), - equalTo((ByteSizeValue.ofMb(100).getBytes() + OVERHEAD) * 4)); - assertThat(autoscalingDeciderResult.requiredCapacity().total().memory().getBytes(), - equalTo(ByteSizeValue.ofGb(12).getBytes())); + assertThat( + autoscalingDeciderResult.requiredCapacity().node().memory().getBytes(), + equalTo((ByteSizeValue.ofMb(100).getBytes() + OVERHEAD) * 4) + ); + assertThat(autoscalingDeciderResult.requiredCapacity().total().memory().getBytes(), equalTo(ByteSizeValue.ofGb(12).getBytes())); } {// Scale down is not really possible - Optional result = service.checkForScaleDown(nodeLoads, + Optional result = service.checkForScaleDown( + nodeLoads, ByteSizeValue.ofMb(100).getBytes(), new NativeMemoryCapacity(ByteSizeValue.ofGb(3).getBytes(), ByteSizeValue.ofMb(100).getBytes()), - reasonBuilder); + reasonBuilder + ); assertThat(result.isEmpty(), is(true)); } } @@ -521,10 +569,13 @@ public void testEnsureScaleDown() { new AutoscalingCapacity.AutoscalingResources(null, ByteSizeValue.ofGb(4)), new AutoscalingCapacity.AutoscalingResources(null, ByteSizeValue.ofGb(2)) ) - ), equalTo(new AutoscalingCapacity( - new AutoscalingCapacity.AutoscalingResources(null, ByteSizeValue.ofGb(4)), - new AutoscalingCapacity.AutoscalingResources(null, ByteSizeValue.ofGb(1)) - )) + ), + equalTo( + new AutoscalingCapacity( + new AutoscalingCapacity.AutoscalingResources(null, ByteSizeValue.ofGb(4)), + new AutoscalingCapacity.AutoscalingResources(null, ByteSizeValue.ofGb(1)) + ) + ) ); assertThat( @@ -537,10 +588,13 @@ public void testEnsureScaleDown() { new AutoscalingCapacity.AutoscalingResources(null, ByteSizeValue.ofGb(4)), new AutoscalingCapacity.AutoscalingResources(null, ByteSizeValue.ofGb(2)) ) - ), equalTo(new AutoscalingCapacity( - new AutoscalingCapacity.AutoscalingResources(null, ByteSizeValue.ofGb(4)), - new AutoscalingCapacity.AutoscalingResources(null, ByteSizeValue.ofGb(2)) - )) + ), + equalTo( + new AutoscalingCapacity( + new AutoscalingCapacity.AutoscalingResources(null, ByteSizeValue.ofGb(4)), + new AutoscalingCapacity.AutoscalingResources(null, ByteSizeValue.ofGb(2)) + ) + ) ); assertThat( @@ -553,10 +607,13 @@ public void testEnsureScaleDown() { new AutoscalingCapacity.AutoscalingResources(null, ByteSizeValue.ofGb(3)), new AutoscalingCapacity.AutoscalingResources(null, ByteSizeValue.ofGb(2)) ) - ), equalTo(new AutoscalingCapacity( - new AutoscalingCapacity.AutoscalingResources(null, ByteSizeValue.ofGb(3)), - new AutoscalingCapacity.AutoscalingResources(null, ByteSizeValue.ofGb(2)) - )) + ), + equalTo( + new AutoscalingCapacity( + new AutoscalingCapacity.AutoscalingResources(null, ByteSizeValue.ofGb(3)), + new AutoscalingCapacity.AutoscalingResources(null, ByteSizeValue.ofGb(2)) + ) + ) ); } @@ -588,12 +645,18 @@ public void testFutureAvailableCapacity() { ); assertThat(nativeMemoryCapacity.isEmpty(), is(false)); assertThat(nativeMemoryCapacity.get().getNode(), greaterThanOrEqualTo(DEFAULT_JOB_SIZE)); - assertThat(nativeMemoryCapacity.get().getNode(), - lessThanOrEqualTo(NativeMemoryCalculator.allowedBytesForMl(DEFAULT_NODE_SIZE, 20, true))); - assertThat(nativeMemoryCapacity.get().getTier(), - greaterThanOrEqualTo(DEFAULT_JOB_SIZE * (assignedAnalyticsJobs.size() + batchAnomalyJobs.size()))); - assertThat(nativeMemoryCapacity.get().getTier(), - lessThanOrEqualTo(3 * (NativeMemoryCalculator.allowedBytesForMl(DEFAULT_NODE_SIZE, 20, true)))); + assertThat( + nativeMemoryCapacity.get().getNode(), + lessThanOrEqualTo(NativeMemoryCalculator.allowedBytesForMl(DEFAULT_NODE_SIZE, 20, true)) + ); + assertThat( + nativeMemoryCapacity.get().getTier(), + greaterThanOrEqualTo(DEFAULT_JOB_SIZE * (assignedAnalyticsJobs.size() + batchAnomalyJobs.size())) + ); + assertThat( + nativeMemoryCapacity.get().getTier(), + lessThanOrEqualTo(3 * (NativeMemoryCalculator.allowedBytesForMl(DEFAULT_NODE_SIZE, 20, true))) + ); } public void testScale_WithNoScaleUpButWaitingJobs() { @@ -625,10 +688,7 @@ public void testScale_WithNoScaleUpButWaitingJobs() { DeciderContext deciderContext = new DeciderContext(clusterState, autoscalingCapacity); AutoscalingDeciderResult result = service.scale(settings, deciderContext); - assertThat( - result.reason().summary(), - containsString("but the number in the queue is less than the configured maximum allowed") - ); + assertThat(result.reason().summary(), containsString("but the number in the queue is less than the configured maximum allowed")); assertThat(result.requiredCapacity(), equalTo(autoscalingCapacity)); } @@ -636,11 +696,13 @@ private MlAutoscalingDeciderService buildService() { return new MlAutoscalingDeciderService(nodeLoadDetector, settings, clusterService, timeSupplier); } - private static ClusterState clusterState(List anomalyTasks, - List batchAnomalyTasks, - List analyticsTasks, - List waitingAnomalyTasks, - List waitingAnalyticsTasks) { + private static ClusterState clusterState( + List anomalyTasks, + List batchAnomalyTasks, + List analyticsTasks, + List waitingAnomalyTasks, + List waitingAnalyticsTasks + ) { List nodeNames = Arrays.asList("_node_id1", "_node_id2", "_node_id3"); List nodeList = withMlNodes(nodeNames.toArray(String[]::new)); DiscoveryNodes.Builder nodesBuilder = DiscoveryNodes.builder(); @@ -649,27 +711,33 @@ private static ClusterState clusterState(List anomalyTasks, } PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); for (String jobId : anomalyTasks) { - OpenJobPersistentTasksExecutorTests.addJobTask(jobId, + OpenJobPersistentTasksExecutorTests.addJobTask( + jobId, randomFrom(nodeNames), randomFrom(JobState.CLOSING, JobState.OPENED, JobState.OPENING, null), - tasksBuilder); + tasksBuilder + ); } for (String jobId : batchAnomalyTasks) { String nodeAssignment = randomFrom(nodeNames); - OpenJobPersistentTasksExecutorTests.addJobTask(jobId, + OpenJobPersistentTasksExecutorTests.addJobTask( + jobId, nodeAssignment, randomFrom(JobState.CLOSING, JobState.OPENED, JobState.OPENING, null), - tasksBuilder); - StartDatafeedAction.DatafeedParams dfParams =new StartDatafeedAction.DatafeedParams(jobId + "-datafeed", 0); + tasksBuilder + ); + StartDatafeedAction.DatafeedParams dfParams = new StartDatafeedAction.DatafeedParams(jobId + "-datafeed", 0); dfParams.setEndTime(new Date().getTime()); tasksBuilder.addTask( MlTasks.datafeedTaskId(jobId + "-datafeed"), MlTasks.DATAFEED_TASK_NAME, dfParams, - new PersistentTasksCustomMetadata.Assignment(nodeAssignment, "test")); + new PersistentTasksCustomMetadata.Assignment(nodeAssignment, "test") + ); } for (String analyticsId : analyticsTasks) { - addAnalyticsTask(analyticsId, + addAnalyticsTask( + analyticsId, randomFrom(nodeNames), randomFrom( DataFrameAnalyticsState.STARTED, @@ -678,7 +746,8 @@ private static ClusterState clusterState(List anomalyTasks, DataFrameAnalyticsState.STOPPING, DataFrameAnalyticsState.STARTING ), - tasksBuilder); + tasksBuilder + ); } for (String job : waitingAnalyticsTasks) { addAnalyticsTask(job, null, null, tasksBuilder); @@ -697,22 +766,27 @@ private static ClusterState clusterState(List anomalyTasks, private static List withMlNodes(String... nodeName) { return Arrays.stream(nodeName) - .map(n -> new DiscoveryNode( - n, - buildNewFakeTransportAddress(), - MapBuilder.newMapBuilder() - .put(MachineLearning.MACHINE_MEMORY_NODE_ATTR, String.valueOf(DEFAULT_NODE_SIZE)) - .put(MachineLearning.MAX_JVM_SIZE_NODE_ATTR, String.valueOf(DEFAULT_JVM_SIZE)) - .map(), - Set.of(DiscoveryNodeRole.ML_ROLE), - Version.CURRENT)) + .map( + n -> new DiscoveryNode( + n, + buildNewFakeTransportAddress(), + MapBuilder.newMapBuilder() + .put(MachineLearning.MACHINE_MEMORY_NODE_ATTR, String.valueOf(DEFAULT_NODE_SIZE)) + .put(MachineLearning.MAX_JVM_SIZE_NODE_ATTR, String.valueOf(DEFAULT_JVM_SIZE)) + .map(), + Set.of(DiscoveryNodeRole.ML_ROLE), + Version.CURRENT + ) + ) .collect(Collectors.toList()); } - public static void addAnalyticsTask(String jobId, - String nodeId, - DataFrameAnalyticsState jobState, - PersistentTasksCustomMetadata.Builder builder) { + public static void addAnalyticsTask( + String jobId, + String nodeId, + DataFrameAnalyticsState jobState, + PersistentTasksCustomMetadata.Builder builder + ) { builder.addTask( MlTasks.dataFrameAnalyticsTaskId(jobId), MlTasks.DATA_FRAME_ANALYTICS_TASK_NAME, @@ -727,18 +801,15 @@ public static void addAnalyticsTask(String jobId, } } - public static void addJobTask(String jobId, - String nodeId, - JobState jobState, - PersistentTasksCustomMetadata.Builder builder) { + public static void addJobTask(String jobId, String nodeId, JobState jobState, PersistentTasksCustomMetadata.Builder builder) { builder.addTask( MlTasks.jobTaskId(jobId), MlTasks.JOB_TASK_NAME, new OpenJobAction.JobParams(jobId), - nodeId == null ? AWAITING_LAZY_ASSIGNMENT : new PersistentTasksCustomMetadata.Assignment(nodeId, "test assignment")); + nodeId == null ? AWAITING_LAZY_ASSIGNMENT : new PersistentTasksCustomMetadata.Assignment(nodeId, "test assignment") + ); if (jobState != null) { - builder.updateTaskState(MlTasks.jobTaskId(jobId), - new JobTaskState(jobState, builder.getLastAllocationId(), null)); + builder.updateTaskState(MlTasks.jobTaskId(jobId), new JobTaskState(jobState, builder.getLastAllocationId(), null)); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/autoscaling/MlScalingReasonTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/autoscaling/MlScalingReasonTests.java index 38530cdc31b5d..f7098e29e26d3 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/autoscaling/MlScalingReasonTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/autoscaling/MlScalingReasonTests.java @@ -44,7 +44,7 @@ protected MlScalingReason createTestInstance() { new AutoscalingCapacity(randomAutoscalingResources(), randomAutoscalingResources()), randomBoolean() ? null : new AutoscalingCapacity(randomAutoscalingResources(), randomAutoscalingResources()), randomAlphaOfLength(10) - ); + ); } protected static AutoscalingCapacity.AutoscalingResources randomAutoscalingResources() { @@ -53,6 +53,7 @@ protected static AutoscalingCapacity.AutoscalingResources randomAutoscalingResou ByteSizeValue.ofBytes(randomLongBetween(10, ByteSizeValue.ofGb(10).getBytes())) ); } + @Override protected NamedWriteableRegistry getNamedWriteableRegistry() { return new NamedWriteableRegistry(MlAutoscalingNamedWritableProvider.getNamedWriteables()); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/autoscaling/NativeMemoryCapacityTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/autoscaling/NativeMemoryCapacityTests.java index 63c72ae414531..64fffa20b2bba 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/autoscaling/NativeMemoryCapacityTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/autoscaling/NativeMemoryCapacityTests.java @@ -25,25 +25,23 @@ public class NativeMemoryCapacityTests extends ESTestCase { private static final int NUM_TEST_RUNS = 10; public void testMerge() { - NativeMemoryCapacity capacity = new NativeMemoryCapacity(ByteSizeValue.ofGb(1).getBytes(), + NativeMemoryCapacity capacity = new NativeMemoryCapacity( + ByteSizeValue.ofGb(1).getBytes(), ByteSizeValue.ofMb(200).getBytes(), ByteSizeValue.ofMb(50).getBytes() - ); - capacity.merge(new NativeMemoryCapacity(ByteSizeValue.ofGb(1).getBytes(), - ByteSizeValue.ofMb(100).getBytes())); + ); + capacity.merge(new NativeMemoryCapacity(ByteSizeValue.ofGb(1).getBytes(), ByteSizeValue.ofMb(100).getBytes())); assertThat(capacity.getTier(), equalTo(ByteSizeValue.ofGb(1).getBytes() * 2L)); assertThat(capacity.getNode(), equalTo(ByteSizeValue.ofMb(200).getBytes())); assertThat(capacity.getJvmSize(), equalTo(ByteSizeValue.ofMb(50).getBytes())); - capacity.merge(new NativeMemoryCapacity(ByteSizeValue.ofGb(1).getBytes(), - ByteSizeValue.ofMb(300).getBytes())); + capacity.merge(new NativeMemoryCapacity(ByteSizeValue.ofGb(1).getBytes(), ByteSizeValue.ofMb(300).getBytes())); assertThat(capacity.getTier(), equalTo(ByteSizeValue.ofGb(1).getBytes() * 3L)); assertThat(capacity.getNode(), equalTo(ByteSizeValue.ofMb(300).getBytes())); assertThat(capacity.getJvmSize(), is(nullValue())); } - public void testAutoscalingCapacity() { // TODO adjust once future JVM capacity is known NativeMemoryCapacity capacity = new NativeMemoryCapacity( @@ -63,10 +61,7 @@ public void testAutoscalingCapacity() { assertThat(autoscalingCapacity.total().memory().getBytes(), equalTo(5343543296L)); } { // auto is true with unknown jvm size - capacity = new NativeMemoryCapacity( - ByteSizeValue.ofGb(4).getBytes(), - ByteSizeValue.ofGb(1).getBytes() - ); + capacity = new NativeMemoryCapacity(ByteSizeValue.ofGb(4).getBytes(), ByteSizeValue.ofGb(1).getBytes()); AutoscalingCapacity autoscalingCapacity = capacity.autoscalingCapacity(25, true); assertThat(autoscalingCapacity.node().memory().getBytes(), equalTo(2139095040L)); assertThat(autoscalingCapacity.total().memory().getBytes(), equalTo(8556380160L)); @@ -78,21 +73,26 @@ public void testAutoscalingCapacityConsistency() { AutoscalingCapacity autoscalingCapacity = nativeMemory.autoscalingCapacity(25, true); assertThat(autoscalingCapacity.total().memory().getBytes(), greaterThan(nativeMemory.getTier())); assertThat(autoscalingCapacity.node().memory().getBytes(), greaterThan(nativeMemory.getNode())); - assertThat(autoscalingCapacity.total().memory().getBytes(), - greaterThanOrEqualTo(autoscalingCapacity.node().memory().getBytes())); + assertThat( + autoscalingCapacity.total().memory().getBytes(), + greaterThanOrEqualTo(autoscalingCapacity.node().memory().getBytes()) + ); }; { // 0 memory - assertThat(NativeMemoryCalculator.calculateApproxNecessaryNodeSize( - 0L, - randomLongBetween(0L, ByteSizeValue.ofGb(100).getBytes()), - randomIntBetween(0, 100), - randomBoolean() + assertThat( + NativeMemoryCalculator.calculateApproxNecessaryNodeSize( + 0L, + randomLongBetween(0L, ByteSizeValue.ofGb(100).getBytes()), + randomIntBetween(0, 100), + randomBoolean() ), - equalTo(0L)); + equalTo(0L) + ); assertThat( NativeMemoryCalculator.calculateApproxNecessaryNodeSize(0L, null, randomIntBetween(0, 100), randomBoolean()), - equalTo(0L)); + equalTo(0L) + ); } for (int i = 0; i < NUM_TEST_RUNS; i++) { int memoryPercentage = randomIntBetween(5, 200); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedConfigAutoUpdaterTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedConfigAutoUpdaterTests.java index 46c1b3aa97bc8..695ee487d0298 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedConfigAutoUpdaterTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedConfigAutoUpdaterTests.java @@ -81,21 +81,27 @@ public void testWithSuccessfulUpdates() { DatafeedConfigAutoUpdater updater = new DatafeedConfigAutoUpdater(provider, indexNameExpressionResolver); updater.runUpdate(); - verify(provider, times(1)).updateDatefeedConfig(eq(datafeedWithRewrite1), + verify(provider, times(1)).updateDatefeedConfig( + eq(datafeedWithRewrite1), any(DatafeedUpdate.class), eq(Collections.emptyMap()), any(), - any()); - verify(provider, times(1)).updateDatefeedConfig(eq(datafeedWithRewrite2), + any() + ); + verify(provider, times(1)).updateDatefeedConfig( + eq(datafeedWithRewrite2), any(DatafeedUpdate.class), eq(Collections.emptyMap()), any(), - any()); - verify(provider, times(0)).updateDatefeedConfig(eq(datafeedWithoutRewrite), + any() + ); + verify(provider, times(0)).updateDatefeedConfig( + eq(datafeedWithoutRewrite), any(DatafeedUpdate.class), eq(Collections.emptyMap()), any(), - any()); + any() + ); } public void testWithUpdateFailures() { @@ -119,21 +125,27 @@ public void testWithUpdateFailures() { assertThat(ex.getSuppressed().length, equalTo(1)); assertThat(ex.getSuppressed()[0].getMessage(), equalTo("Failed to update datafeed " + datafeedWithRewriteFailure)); - verify(provider, times(1)).updateDatefeedConfig(eq(datafeedWithRewrite1), + verify(provider, times(1)).updateDatefeedConfig( + eq(datafeedWithRewrite1), any(DatafeedUpdate.class), eq(Collections.emptyMap()), any(), - any()); - verify(provider, times(1)).updateDatefeedConfig(eq(datafeedWithRewriteFailure), + any() + ); + verify(provider, times(1)).updateDatefeedConfig( + eq(datafeedWithRewriteFailure), any(DatafeedUpdate.class), eq(Collections.emptyMap()), any(), - any()); - verify(provider, times(0)).updateDatefeedConfig(eq(datafeedWithoutRewrite), + any() + ); + verify(provider, times(0)).updateDatefeedConfig( + eq(datafeedWithoutRewrite), any(DatafeedUpdate.class), eq(Collections.emptyMap()), any(), - any()); + any() + ); } public void testWithNoUpdates() { @@ -145,31 +157,33 @@ public void testWithNoUpdates() { DatafeedConfigAutoUpdater updater = new DatafeedConfigAutoUpdater(provider, indexNameExpressionResolver); updater.runUpdate(); - verify(provider, times(0)).updateDatefeedConfig(any(), - any(DatafeedUpdate.class), - eq(Collections.emptyMap()), - any(), - any()); + verify(provider, times(0)).updateDatefeedConfig(any(), any(DatafeedUpdate.class), eq(Collections.emptyMap()), any(), any()); } public void testIsAbleToRun() { Metadata.Builder metadata = Metadata.builder(); RoutingTable.Builder routingTable = RoutingTable.builder(); IndexMetadata.Builder indexMetadata = IndexMetadata.builder(MlConfigIndex.indexName()); - indexMetadata.settings(Settings.builder() - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + indexMetadata.settings( + Settings.builder() + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) ); metadata.put(indexMetadata); Index index = new Index(MlConfigIndex.indexName(), "_uuid"); ShardId shardId = new ShardId(index, 0); - ShardRouting shardRouting = ShardRouting.newUnassigned(shardId, true, RecoverySource.EmptyStoreRecoverySource.INSTANCE, - new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "")); + ShardRouting shardRouting = ShardRouting.newUnassigned( + shardId, + true, + RecoverySource.EmptyStoreRecoverySource.INSTANCE, + new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "") + ); shardRouting = shardRouting.initialize("node_id", null, 0L); shardRouting = shardRouting.moveToStarted(); - routingTable.add(IndexRoutingTable.builder(index) - .addIndexShard(new IndexShardRoutingTable.Builder(shardId).addShard(shardRouting).build())); + routingTable.add( + IndexRoutingTable.builder(index).addIndexShard(new IndexShardRoutingTable.Builder(shardId).addShard(shardRouting).build()) + ); ClusterState.Builder csBuilder = ClusterState.builder(new ClusterName("_name")); csBuilder.routingTable(routingTable.build()); @@ -186,11 +200,16 @@ public void testIsAbleToRun() { } else { index = new Index(MlConfigIndex.indexName(), "_uuid"); shardId = new ShardId(index, 0); - shardRouting = ShardRouting.newUnassigned(shardId, true, RecoverySource.EmptyStoreRecoverySource.INSTANCE, - new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "")); + shardRouting = ShardRouting.newUnassigned( + shardId, + true, + RecoverySource.EmptyStoreRecoverySource.INSTANCE, + new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "") + ); shardRouting = shardRouting.initialize("node_id", null, 0L); - routingTable.add(IndexRoutingTable.builder(index) - .addIndexShard(new IndexShardRoutingTable.Builder(shardId).addShard(shardRouting).build())); + routingTable.add( + IndexRoutingTable.builder(index).addIndexShard(new IndexShardRoutingTable.Builder(shardId).addShard(shardRouting).build()) + ); } csBuilder = ClusterState.builder(clusterState); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedJobBuilderTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedJobBuilderTests.java index 385b381d3f621..a99f43a7b446c 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedJobBuilderTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedJobBuilderTests.java @@ -66,12 +66,18 @@ public void init() { auditor = mock(AnomalyDetectionAuditor.class); annotationPersister = mock(AnnotationPersister.class); jobResultsPersister = mock(JobResultsPersister.class); - ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, - new HashSet<>(Arrays.asList(MachineLearning.DELAYED_DATA_CHECK_FREQ, - MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING, - OperationRouting.USE_ADAPTIVE_REPLICA_SELECTION_SETTING, - ClusterService.USER_DEFINED_METADATA, - ClusterApplierService.CLUSTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING))); + ClusterSettings clusterSettings = new ClusterSettings( + Settings.EMPTY, + new HashSet<>( + Arrays.asList( + MachineLearning.DELAYED_DATA_CHECK_FREQ, + MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING, + OperationRouting.USE_ADAPTIVE_REPLICA_SELECTION_SETTING, + ClusterService.USER_DEFINED_METADATA, + ClusterApplierService.CLUSTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING + ) + ) + ); clusterService = new ClusterService( Settings.builder().put(Node.NODE_NAME_SETTING.getKey(), "test_node").build(), clusterSettings, @@ -79,14 +85,14 @@ public void init() { ); datafeedJobBuilder = new DatafeedJobBuilder( - client, - xContentRegistry(), - auditor, - annotationPersister, - System::currentTimeMillis, - jobResultsPersister, - Settings.EMPTY, - clusterService + client, + xContentRegistry(), + auditor, + annotationPersister, + System::currentTimeMillis, + jobResultsPersister, + Settings.EMPTY, + clusterService ); } @@ -99,14 +105,12 @@ public void testBuild_GivenScrollDatafeedAndNewJob() throws Exception { DatafeedConfig.Builder datafeed = DatafeedRunnerTests.createDatafeedConfig("datafeed1", jobBuilder.getId()); AtomicBoolean wasHandlerCalled = new AtomicBoolean(false); - ActionListener datafeedJobHandler = ActionListener.wrap( - datafeedJob -> { - assertThat(datafeedJob.isRunning(), is(true)); - assertThat(datafeedJob.isIsolated(), is(false)); - assertThat(datafeedJob.lastEndTimeMs(), is(nullValue())); - wasHandlerCalled.compareAndSet(false, true); - }, e -> fail() - ); + ActionListener datafeedJobHandler = ActionListener.wrap(datafeedJob -> { + assertThat(datafeedJob.isRunning(), is(true)); + assertThat(datafeedJob.isIsolated(), is(false)); + assertThat(datafeedJob.lastEndTimeMs(), is(nullValue())); + wasHandlerCalled.compareAndSet(false, true); + }, e -> fail()); DatafeedContext datafeedContext = DatafeedContext.builder() .setDatafeedConfig(datafeed.build()) @@ -131,14 +135,12 @@ public void testBuild_GivenScrollDatafeedAndOldJobWithLatestRecordTimestampAfter DatafeedConfig.Builder datafeed = DatafeedRunnerTests.createDatafeedConfig("datafeed1", jobBuilder.getId()); AtomicBoolean wasHandlerCalled = new AtomicBoolean(false); - ActionListener datafeedJobHandler = ActionListener.wrap( - datafeedJob -> { - assertThat(datafeedJob.isRunning(), is(true)); - assertThat(datafeedJob.isIsolated(), is(false)); - assertThat(datafeedJob.lastEndTimeMs(), equalTo(7_200_000L)); - wasHandlerCalled.compareAndSet(false, true); - }, e -> fail() - ); + ActionListener datafeedJobHandler = ActionListener.wrap(datafeedJob -> { + assertThat(datafeedJob.isRunning(), is(true)); + assertThat(datafeedJob.isIsolated(), is(false)); + assertThat(datafeedJob.lastEndTimeMs(), equalTo(7_200_000L)); + wasHandlerCalled.compareAndSet(false, true); + }, e -> fail()); DatafeedContext datafeedContext = DatafeedContext.builder() .setDatafeedConfig(datafeed.build()) @@ -163,14 +165,12 @@ public void testBuild_GivenScrollDatafeedAndOldJobWithLatestBucketAfterLatestRec DatafeedConfig.Builder datafeed = DatafeedRunnerTests.createDatafeedConfig("datafeed1", jobBuilder.getId()); AtomicBoolean wasHandlerCalled = new AtomicBoolean(false); - ActionListener datafeedJobHandler = ActionListener.wrap( - datafeedJob -> { - assertThat(datafeedJob.isRunning(), is(true)); - assertThat(datafeedJob.isIsolated(), is(false)); - assertThat(datafeedJob.lastEndTimeMs(), equalTo(7_199_999L)); - wasHandlerCalled.compareAndSet(false, true); - }, e -> fail() - ); + ActionListener datafeedJobHandler = ActionListener.wrap(datafeedJob -> { + assertThat(datafeedJob.isRunning(), is(true)); + assertThat(datafeedJob.isIsolated(), is(false)); + assertThat(datafeedJob.lastEndTimeMs(), equalTo(7_199_999L)); + wasHandlerCalled.compareAndSet(false, true); + }, e -> fail()); DatafeedContext datafeedContext = DatafeedContext.builder() .setDatafeedConfig(datafeed.build()) @@ -209,10 +209,17 @@ public void testBuildGivenRemoteIndicesButNoRemoteSearching() throws Exception { ActionListener datafeedJobHandler = ActionListener.wrap( datafeedJob -> fail("datafeed builder did not fail when remote index was given and remote clusters were not enabled"), e -> { - assertThat(e.getMessage(), equalTo(Messages.getMessage(Messages.DATAFEED_NEEDS_REMOTE_CLUSTER_SEARCH, - "datafeed1", - "[remotecluster:index-*]", - "test_node"))); + assertThat( + e.getMessage(), + equalTo( + Messages.getMessage( + Messages.DATAFEED_NEEDS_REMOTE_CLUSTER_SEARCH, + "datafeed1", + "[remotecluster:index-*]", + "test_node" + ) + ) + ); wasHandlerCalled.compareAndSet(false, true); } ); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedJobTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedJobTests.java index 116f14e7b0993..933134fdef145 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedJobTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedJobTests.java @@ -21,15 +21,14 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentElasticsearchExtension; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.shard.ShardId; -import org.mockito.Mockito; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.ClientHelper; import org.elasticsearch.xpack.core.ml.action.FlushJobAction; import org.elasticsearch.xpack.core.ml.action.PersistJobAction; @@ -52,6 +51,7 @@ import org.elasticsearch.xpack.ml.utils.persistence.ResultsPersisterServiceTests; import org.junit.Before; import org.mockito.ArgumentCaptor; +import org.mockito.Mockito; import org.mockito.stubbing.Answer; import java.io.ByteArrayInputStream; @@ -119,8 +119,9 @@ public void setup() throws Exception { ThreadPool threadPool = mock(ThreadPool.class); when(client.threadPool()).thenReturn(threadPool); when(threadPool.getThreadContext()).thenReturn(new ThreadContext(Settings.EMPTY)); - resultsPersisterService = - ResultsPersisterServiceTests.buildResultsPersisterService(new OriginSettingClient(client, ClientHelper.ML_ORIGIN)); + resultsPersisterService = ResultsPersisterServiceTests.buildResultsPersisterService( + new OriginSettingClient(client, ClientHelper.ML_ORIGIN) + ); dataDescription = new DataDescription.Builder(); postDataFuture = mock(ActionFuture.class); flushJobFuture = mock(ActionFuture.class); @@ -133,8 +134,25 @@ public void setup() throws Exception { byte[] contentBytes = "content".getBytes(StandardCharsets.UTF_8); InputStream inputStream = new ByteArrayInputStream(contentBytes); when(dataExtractor.next()).thenReturn(Optional.of(inputStream)); - DataCounts dataCounts = new DataCounts(jobId, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, new Date(0), new Date(0), - new Date(0), new Date(0), new Date(0), Instant.now()); + DataCounts dataCounts = new DataCounts( + jobId, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + new Date(0), + new Date(0), + new Date(0), + new Date(0), + new Date(0), + Instant.now() + ); PostDataAction.Request expectedRequest = new PostDataAction.Request(jobId); expectedRequest.setDataDescription(dataDescription.build()); @@ -146,8 +164,8 @@ public void setup() throws Exception { when(flushJobFuture.actionGet()).thenReturn(flushJobResponse); when(client.execute(same(FlushJobAction.INSTANCE), flushJobRequests.capture())).thenReturn(flushJobFuture); - doAnswer(withResponse(new BulkResponse(new BulkItemResponse[]{ bulkItemSuccess(annotationDocId) }, 0L))) - .when(client).execute(eq(BulkAction.INSTANCE), any(), any()); + doAnswer(withResponse(new BulkResponse(new BulkItemResponse[] { bulkItemSuccess(annotationDocId) }, 0L))).when(client) + .execute(eq(BulkAction.INSTANCE), any(), any()); } public void testLookBackRunWithEndTime() throws Exception { @@ -251,8 +269,9 @@ public void testRealtimeRun() throws Exception { when(bucket.getBucketSpan()).thenReturn(4L); when(flushJobFuture.actionGet()).thenReturn(flushJobResponse); when(client.execute(same(FlushJobAction.INSTANCE), flushJobRequests.capture())).thenReturn(flushJobFuture); - when(delayedDataDetector.detectMissingData(2000)) - .thenReturn(Collections.singletonList(BucketWithMissingData.fromMissingAndBucket(10, bucket))); + when(delayedDataDetector.detectMissingData(2000)).thenReturn( + Collections.singletonList(BucketWithMissingData.fromMissingAndBucket(10, bucket)) + ); currentTime = DELAYED_DATA_FREQ_HALF; long frequencyMs = 100; long queryDelayMs = 1000; @@ -286,14 +305,15 @@ public void testRealtimeRun() throws Exception { when(dataExtractorFactory.newExtractor(anyLong(), anyLong())).thenReturn(dataExtractor); datafeedJob.runRealtime(); - String msg = Messages.getMessage(Messages.JOB_AUDIT_DATAFEED_MISSING_DATA, + String msg = Messages.getMessage( + Messages.JOB_AUDIT_DATAFEED_MISSING_DATA, 10, - XContentElasticsearchExtension.DEFAULT_FORMATTER.format(Instant.ofEpochMilli(2000))); + XContentElasticsearchExtension.DEFAULT_FORMATTER.format(Instant.ofEpochMilli(2000)) + ); long annotationCreateTime = currentTime; { // What we expect the created annotation to be indexed as - Annotation expectedAnnotation = new Annotation.Builder() - .setAnnotation(msg) + Annotation expectedAnnotation = new Annotation.Builder().setAnnotation(msg) .setCreateTime(new Date(annotationCreateTime)) .setCreateUsername(XPackUser.NAME) .setTimestamp(bucket.getTimestamp()) @@ -304,8 +324,9 @@ public void testRealtimeRun() throws Exception { .setType(Annotation.Type.ANNOTATION) .setEvent(Annotation.Event.DELAYED_DATA) .build(); - BytesReference expectedSource = - BytesReference.bytes(expectedAnnotation.toXContent(XContentFactory.jsonBuilder(), ToXContent.EMPTY_PARAMS)); + BytesReference expectedSource = BytesReference.bytes( + expectedAnnotation.toXContent(XContentFactory.jsonBuilder(), ToXContent.EMPTY_PARAMS) + ); ArgumentCaptor bulkRequestArgumentCaptor = ArgumentCaptor.forClass(BulkRequest.class); verify(client, atMost(2)).execute(eq(BulkAction.INSTANCE), bulkRequestArgumentCaptor.capture(), any()); @@ -323,9 +344,9 @@ public void testRealtimeRun() throws Exception { when(bucket2.getTimestamp()).thenReturn(new Date(6000)); when(bucket2.getEpoch()).thenReturn(6L); when(bucket2.getBucketSpan()).thenReturn(4L); - when(delayedDataDetector.detectMissingData(2000)) - .thenReturn(Arrays.asList(BucketWithMissingData.fromMissingAndBucket(10, bucket), - BucketWithMissingData.fromMissingAndBucket(5, bucket2))); + when(delayedDataDetector.detectMissingData(2000)).thenReturn( + Arrays.asList(BucketWithMissingData.fromMissingAndBucket(10, bucket), BucketWithMissingData.fromMissingAndBucket(5, bucket2)) + ); currentTime = currentTime + DELAYED_DATA_WINDOW + 1; inputStream = new ByteArrayInputStream(contentBytes); when(dataExtractor.hasNext()).thenReturn(true).thenReturn(false); @@ -333,14 +354,15 @@ public void testRealtimeRun() throws Exception { when(dataExtractorFactory.newExtractor(anyLong(), anyLong())).thenReturn(dataExtractor); datafeedJob.runRealtime(); - msg = Messages.getMessage(Messages.JOB_AUDIT_DATAFEED_MISSING_DATA, + msg = Messages.getMessage( + Messages.JOB_AUDIT_DATAFEED_MISSING_DATA, 15, - XContentElasticsearchExtension.DEFAULT_FORMATTER.format(Instant.ofEpochMilli(6000))); + XContentElasticsearchExtension.DEFAULT_FORMATTER.format(Instant.ofEpochMilli(6000)) + ); long annotationUpdateTime = currentTime; { // What we expect the updated annotation to be indexed as - Annotation expectedUpdatedAnnotation = new Annotation.Builder() - .setAnnotation(msg) + Annotation expectedUpdatedAnnotation = new Annotation.Builder().setAnnotation(msg) .setCreateTime(new Date(annotationCreateTime)) .setCreateUsername(XPackUser.NAME) .setTimestamp(bucket.getTimestamp()) @@ -351,8 +373,9 @@ public void testRealtimeRun() throws Exception { .setType(Annotation.Type.ANNOTATION) .setEvent(Annotation.Event.DELAYED_DATA) .build(); - BytesReference expectedSource = - BytesReference.bytes(expectedUpdatedAnnotation.toXContent(XContentFactory.jsonBuilder(), ToXContent.EMPTY_PARAMS)); + BytesReference expectedSource = BytesReference.bytes( + expectedUpdatedAnnotation.toXContent(XContentFactory.jsonBuilder(), ToXContent.EMPTY_PARAMS) + ); ArgumentCaptor bulkRequestArgumentCaptor = ArgumentCaptor.forClass(BulkRequest.class); verify(client, atMost(2)).execute(eq(BulkAction.INSTANCE), bulkRequestArgumentCaptor.capture(), any()); @@ -419,8 +442,10 @@ public void testPostAnalysisProblem() { when(dataExtractor.getEndTime()).thenReturn(1000L); DatafeedJob datafeedJob = createDatafeedJob(1000, 500, -1, -1, randomBoolean()); - DatafeedJob.AnalysisProblemException analysisProblemException = - expectThrows(DatafeedJob.AnalysisProblemException.class, () -> datafeedJob.runLookBack(0L, 1000L)); + DatafeedJob.AnalysisProblemException analysisProblemException = expectThrows( + DatafeedJob.AnalysisProblemException.class, + () -> datafeedJob.runLookBack(0L, 1000L) + ); assertThat(analysisProblemException.shouldStop, is(false)); currentTime = 3001; @@ -448,8 +473,10 @@ public void testPostAnalysisProblemIsConflict() { when(dataExtractor.getEndTime()).thenReturn(1000L); DatafeedJob datafeedJob = createDatafeedJob(1000, 500, -1, -1, randomBoolean()); - DatafeedJob.AnalysisProblemException analysisProblemException = - expectThrows(DatafeedJob.AnalysisProblemException.class, () -> datafeedJob.runLookBack(0L, 1000L)); + DatafeedJob.AnalysisProblemException analysisProblemException = expectThrows( + DatafeedJob.AnalysisProblemException.class, + () -> datafeedJob.runLookBack(0L, 1000L) + ); assertThat(analysisProblemException.shouldStop, is(true)); currentTime = 3001; @@ -473,8 +500,10 @@ public void testFlushAnalysisProblem() { long frequencyMs = 100; long queryDelayMs = 1000; DatafeedJob datafeedJob = createDatafeedJob(frequencyMs, queryDelayMs, 1000, -1, randomBoolean()); - DatafeedJob.AnalysisProblemException analysisProblemException = - expectThrows(DatafeedJob.AnalysisProblemException.class, () -> datafeedJob.runRealtime()); + DatafeedJob.AnalysisProblemException analysisProblemException = expectThrows( + DatafeedJob.AnalysisProblemException.class, + () -> datafeedJob.runRealtime() + ); assertThat(analysisProblemException.shouldStop, is(false)); } @@ -485,23 +514,57 @@ public void testFlushAnalysisProblemIsConflict() { long frequencyMs = 100; long queryDelayMs = 1000; DatafeedJob datafeedJob = createDatafeedJob(frequencyMs, queryDelayMs, 1000, -1, randomBoolean()); - DatafeedJob.AnalysisProblemException analysisProblemException = - expectThrows(DatafeedJob.AnalysisProblemException.class, () -> datafeedJob.runRealtime()); + DatafeedJob.AnalysisProblemException analysisProblemException = expectThrows( + DatafeedJob.AnalysisProblemException.class, + () -> datafeedJob.runRealtime() + ); assertThat(analysisProblemException.shouldStop, is(true)); } - private DatafeedJob createDatafeedJob(long frequencyMs, long queryDelayMs, long latestFinalBucketEndTimeMs, - long latestRecordTimeMs, boolean haveSeenDataPreviously) { - return createDatafeedJob(frequencyMs, queryDelayMs, latestFinalBucketEndTimeMs, latestRecordTimeMs, haveSeenDataPreviously, - DELAYED_DATA_CHECK_FREQ.get(Settings.EMPTY).millis()); + private DatafeedJob createDatafeedJob( + long frequencyMs, + long queryDelayMs, + long latestFinalBucketEndTimeMs, + long latestRecordTimeMs, + boolean haveSeenDataPreviously + ) { + return createDatafeedJob( + frequencyMs, + queryDelayMs, + latestFinalBucketEndTimeMs, + latestRecordTimeMs, + haveSeenDataPreviously, + DELAYED_DATA_CHECK_FREQ.get(Settings.EMPTY).millis() + ); } - private DatafeedJob createDatafeedJob(long frequencyMs, long queryDelayMs, long latestFinalBucketEndTimeMs, - long latestRecordTimeMs, boolean haveSeenDataPreviously, long delayedDataFreq) { + private DatafeedJob createDatafeedJob( + long frequencyMs, + long queryDelayMs, + long latestFinalBucketEndTimeMs, + long latestRecordTimeMs, + boolean haveSeenDataPreviously, + long delayedDataFreq + ) { Supplier currentTimeSupplier = () -> currentTime; - return new DatafeedJob(jobId, dataDescription.build(), frequencyMs, queryDelayMs, dataExtractorFactory, timingStatsReporter, - client, auditor, new AnnotationPersister(resultsPersisterService), currentTimeSupplier, - delayedDataDetector, null, latestFinalBucketEndTimeMs, latestRecordTimeMs, haveSeenDataPreviously, delayedDataFreq); + return new DatafeedJob( + jobId, + dataDescription.build(), + frequencyMs, + queryDelayMs, + dataExtractorFactory, + timingStatsReporter, + client, + auditor, + new AnnotationPersister(resultsPersisterService), + currentTimeSupplier, + delayedDataDetector, + null, + latestFinalBucketEndTimeMs, + latestRecordTimeMs, + haveSeenDataPreviously, + delayedDataFreq + ); } @SuppressWarnings("unchecked") @@ -517,6 +580,7 @@ private static BulkItemResponse bulkItemSuccess(String docId) { return BulkItemResponse.success( 1, DocWriteRequest.OpType.INDEX, - new IndexResponse(new ShardId(AnnotationIndex.WRITE_ALIAS_NAME, "uuid", 1), docId, 0, 0, 1, true)); + new IndexResponse(new ShardId(AnnotationIndex.WRITE_ALIAS_NAME, "uuid", 1), docId, 0, 0, 1, true) + ); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedJobValidatorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedJobValidatorTests.java index 2b242ff17a2a1..0e0463f2e2b2a 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedJobValidatorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedJobValidatorTests.java @@ -9,13 +9,13 @@ import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.search.SearchModule; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.AggregatorFactories; import org.elasticsearch.search.aggregations.bucket.histogram.HistogramAggregationBuilder; import org.elasticsearch.search.aggregations.metrics.MaxAggregationBuilder; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfig; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedJobValidator; import org.elasticsearch.xpack.core.ml.datafeed.DelayedDataCheckConfig; @@ -50,8 +50,10 @@ public void testValidate_GivenNonZeroLatency() { Job job = builder.build(new Date()); DatafeedConfig datafeedConfig = createValidDatafeedConfig().build(); - ElasticsearchStatusException e = ESTestCase.expectThrows(ElasticsearchStatusException.class, - () -> DatafeedJobValidator.validate(datafeedConfig, job, xContentRegistry())); + ElasticsearchStatusException e = ESTestCase.expectThrows( + ElasticsearchStatusException.class, + () -> DatafeedJobValidator.validate(datafeedConfig, job, xContentRegistry()) + ); assertEquals(errorMessage, e.getMessage()); } @@ -80,8 +82,10 @@ public void testVerify_GivenNoLatency() { } public void testVerify_GivenAggsAndNoSummaryCountField() throws IOException { - String errorMessage = Messages.getMessage(Messages.DATAFEED_AGGREGATIONS_REQUIRES_JOB_WITH_SUMMARY_COUNT_FIELD, - DatafeedConfig.DOC_COUNT); + String errorMessage = Messages.getMessage( + Messages.DATAFEED_AGGREGATIONS_REQUIRES_JOB_WITH_SUMMARY_COUNT_FIELD, + DatafeedConfig.DOC_COUNT + ); Job.Builder builder = buildJobBuilder("foo"); AnalysisConfig.Builder ac = createAnalysisConfig(); ac.setSummaryCountFieldName(null); @@ -90,15 +94,19 @@ public void testVerify_GivenAggsAndNoSummaryCountField() throws IOException { Job job = builder.build(new Date()); DatafeedConfig datafeedConfig = createValidDatafeedConfigWithAggs(1800.0).build(); - ElasticsearchStatusException e = ESTestCase.expectThrows(ElasticsearchStatusException.class, - () -> DatafeedJobValidator.validate(datafeedConfig, job, xContentRegistry())); + ElasticsearchStatusException e = ESTestCase.expectThrows( + ElasticsearchStatusException.class, + () -> DatafeedJobValidator.validate(datafeedConfig, job, xContentRegistry()) + ); assertEquals(errorMessage, e.getMessage()); } public void testVerify_GivenAggsAndEmptySummaryCountField() throws IOException { - String errorMessage = Messages.getMessage(Messages.DATAFEED_AGGREGATIONS_REQUIRES_JOB_WITH_SUMMARY_COUNT_FIELD, - DatafeedConfig.DOC_COUNT); + String errorMessage = Messages.getMessage( + Messages.DATAFEED_AGGREGATIONS_REQUIRES_JOB_WITH_SUMMARY_COUNT_FIELD, + DatafeedConfig.DOC_COUNT + ); Job.Builder builder = buildJobBuilder("foo"); AnalysisConfig.Builder ac = createAnalysisConfig(); ac.setSummaryCountFieldName(""); @@ -107,8 +115,10 @@ public void testVerify_GivenAggsAndEmptySummaryCountField() throws IOException { Job job = builder.build(new Date()); DatafeedConfig datafeedConfig = createValidDatafeedConfigWithAggs(1800.0).build(); - ElasticsearchStatusException e = ESTestCase.expectThrows(ElasticsearchStatusException.class, - () -> DatafeedJobValidator.validate(datafeedConfig, job, xContentRegistry())); + ElasticsearchStatusException e = ESTestCase.expectThrows( + ElasticsearchStatusException.class, + () -> DatafeedJobValidator.validate(datafeedConfig, job, xContentRegistry()) + ); assertEquals(errorMessage, e.getMessage()); } @@ -133,8 +143,10 @@ public void testVerify_GivenHistogramIntervalGreaterThanBucketSpan() throws IOEx Job job = builder.build(new Date()); DatafeedConfig datafeedConfig = createValidDatafeedConfigWithAggs(1800001.0).build(); - ElasticsearchStatusException e = ESTestCase.expectThrows(ElasticsearchStatusException.class, - () -> DatafeedJobValidator.validate(datafeedConfig, job, xContentRegistry())); + ElasticsearchStatusException e = ESTestCase.expectThrows( + ElasticsearchStatusException.class, + () -> DatafeedJobValidator.validate(datafeedConfig, job, xContentRegistry()) + ); assertEquals("Aggregation interval [1800001ms] must be less than or equal to the bucket_span [1800000ms]", e.getMessage()); } @@ -148,8 +160,10 @@ public void testVerify_HistogramIntervalIsDivisorOfBucketSpan() throws IOExcepti Job job = builder.build(new Date()); DatafeedConfig datafeedConfig = createValidDatafeedConfigWithAggs(37 * 1000).build(); - ElasticsearchStatusException e = ESTestCase.expectThrows(ElasticsearchStatusException.class, - () -> DatafeedJobValidator.validate(datafeedConfig, job, xContentRegistry())); + ElasticsearchStatusException e = ESTestCase.expectThrows( + ElasticsearchStatusException.class, + () -> DatafeedJobValidator.validate(datafeedConfig, job, xContentRegistry()) + ); assertEquals("Aggregation interval [37000ms] must be a divisor of the bucket_span [300000ms]", e.getMessage()); DatafeedConfig goodDatafeedConfig = createValidDatafeedConfigWithAggs(60 * 1000).build(); @@ -179,13 +193,17 @@ public void testVerify_FrequencyIsMultipleOfHistogramInterval() throws IOExcepti // Now non-multiples datafeedBuilder.setFrequency(TimeValue.timeValueSeconds(30)); - ElasticsearchStatusException e = ESTestCase.expectThrows(ElasticsearchStatusException.class, - () -> DatafeedJobValidator.validate(datafeedBuilder.build(), job, xContentRegistry())); + ElasticsearchStatusException e = ESTestCase.expectThrows( + ElasticsearchStatusException.class, + () -> DatafeedJobValidator.validate(datafeedBuilder.build(), job, xContentRegistry()) + ); assertEquals("Datafeed frequency [30s] must be a multiple of the aggregation interval [60000ms]", e.getMessage()); datafeedBuilder.setFrequency(TimeValue.timeValueSeconds(90)); - e = ESTestCase.expectThrows(ElasticsearchStatusException.class, - () -> DatafeedJobValidator.validate(datafeedBuilder.build(), job, xContentRegistry())); + e = ESTestCase.expectThrows( + ElasticsearchStatusException.class, + () -> DatafeedJobValidator.validate(datafeedBuilder.build(), job, xContentRegistry()) + ); assertEquals("Datafeed frequency [1.5m] must be a multiple of the aggregation interval [60000ms]", e.getMessage()); } @@ -202,15 +220,18 @@ public void testVerify_BucketIntervalAndDataCheckWindowAreValid() { DatafeedJobValidator.validate(datafeedBuilder.build(), job, xContentRegistry()); datafeedBuilder.setDelayedDataCheckConfig(DelayedDataCheckConfig.enabledDelayedDataCheckConfig(TimeValue.timeValueSeconds(1))); - ElasticsearchStatusException e = ESTestCase.expectThrows(ElasticsearchStatusException.class, - () -> DatafeedJobValidator.validate(datafeedBuilder.build(), job, xContentRegistry())); + ElasticsearchStatusException e = ESTestCase.expectThrows( + ElasticsearchStatusException.class, + () -> DatafeedJobValidator.validate(datafeedBuilder.build(), job, xContentRegistry()) + ); assertEquals(Messages.getMessage(Messages.DATAFEED_CONFIG_DELAYED_DATA_CHECK_TOO_SMALL, "1s", "2s"), e.getMessage()); datafeedBuilder.setDelayedDataCheckConfig(DelayedDataCheckConfig.enabledDelayedDataCheckConfig(TimeValue.timeValueHours(24))); - e = ESTestCase.expectThrows(ElasticsearchStatusException.class, - () -> DatafeedJobValidator.validate(datafeedBuilder.build(), job, xContentRegistry())); - assertEquals(Messages.getMessage( - Messages.DATAFEED_CONFIG_DELAYED_DATA_CHECK_SPANS_TOO_MANY_BUCKETS, "1d", "2s"), e.getMessage()); + e = ESTestCase.expectThrows( + ElasticsearchStatusException.class, + () -> DatafeedJobValidator.validate(datafeedBuilder.build(), job, xContentRegistry()) + ); + assertEquals(Messages.getMessage(Messages.DATAFEED_CONFIG_DELAYED_DATA_CHECK_SPANS_TOO_MANY_BUCKETS, "1d", "2s"), e.getMessage()); } public void testVerify_WithRuntimeTimeField() { @@ -232,8 +253,10 @@ public void testVerify_WithRuntimeTimeField() { datafeedBuilder.setRuntimeMappings(runtimeMappings); Job job = jobBuilder.build(new Date()); - Exception e = ESTestCase.expectThrows(ElasticsearchStatusException.class, - () -> DatafeedJobValidator.validate(datafeedBuilder.build(), jobBuilder.build(), xContentRegistry())); + Exception e = ESTestCase.expectThrows( + ElasticsearchStatusException.class, + () -> DatafeedJobValidator.validate(datafeedBuilder.build(), jobBuilder.build(), xContentRegistry()) + ); assertEquals("data_description.time_field [" + timeField + "] cannot be a runtime field", e.getMessage()); runtimeMappings.remove(timeField); @@ -258,8 +281,10 @@ public static AnalysisConfig.Builder createAnalysisConfig() { private static DatafeedConfig.Builder createValidDatafeedConfigWithAggs(double interval) throws IOException { MaxAggregationBuilder maxTime = AggregationBuilders.max("time").field("time"); - HistogramAggregationBuilder histogram = - AggregationBuilders.histogram("time").interval(interval).field("time").subAggregation(maxTime); + HistogramAggregationBuilder histogram = AggregationBuilders.histogram("time") + .interval(interval) + .field("time") + .subAggregation(maxTime); DatafeedConfig.Builder datafeedConfig = createValidDatafeedConfig(); datafeedConfig.setParsedAggregations(new AggregatorFactories.Builder().addAggregator(histogram)); return datafeedConfig; diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedNodeSelectorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedNodeSelectorTests.java index d1cc3c1bc4663..fe6bb9e34683c 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedNodeSelectorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedNodeSelectorTests.java @@ -26,8 +26,8 @@ import org.elasticsearch.cluster.routing.ShardRoutingState; import org.elasticsearch.cluster.routing.TestShardRouting; import org.elasticsearch.cluster.routing.UnassignedInfo; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.transport.TransportAddress; +import org.elasticsearch.core.Tuple; import org.elasticsearch.index.Index; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.indices.TestIndexNameExpressionResolver; @@ -49,8 +49,8 @@ import java.util.Date; import java.util.List; -import static org.elasticsearch.cluster.metadata.DataStreamTestHelper.createTimestampField; import static org.elasticsearch.cluster.metadata.DataStream.getDefaultBackingIndexName; +import static org.elasticsearch.cluster.metadata.DataStreamTestHelper.createTimestampField; import static org.elasticsearch.cluster.metadata.IndexMetadata.INDEX_UUID_NA_VALUE; import static org.elasticsearch.xpack.ml.job.task.OpenJobPersistentTasksExecutorTests.addJobTask; import static org.elasticsearch.xpack.ml.support.BaseMlIntegTestCase.createDatafeed; @@ -70,9 +70,17 @@ public class DatafeedNodeSelectorTests extends ESTestCase { public void init() { resolver = TestIndexNameExpressionResolver.newInstance(); nodes = DiscoveryNodes.builder() - .add(new DiscoveryNode("node_name", "node_id", new TransportAddress(InetAddress.getLoopbackAddress(), 9300), - Collections.emptyMap(), Collections.emptySet(), Version.CURRENT)) - .build(); + .add( + new DiscoveryNode( + "node_name", + "node_id", + new TransportAddress(InetAddress.getLoopbackAddress(), 9300), + Collections.emptyMap(), + Collections.emptySet(), + Version.CURRENT + ) + ) + .build(); mlMetadata = new MlMetadata.Builder().build(); } @@ -80,78 +88,69 @@ public void testSelectNode_GivenJobIsOpened() { Job job = createScheduledJob("job_id").build(new Date()); DatafeedConfig df = createDatafeed("datafeed_id", job.getId(), Collections.singletonList("foo")); - PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); + PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); addJobTask(job.getId(), "node_id", JobState.OPENED, tasksBuilder); tasks = tasksBuilder.build(); givenClusterState("foo", 1, 0); - PersistentTasksCustomMetadata.Assignment result = new DatafeedNodeSelector(clusterState, + PersistentTasksCustomMetadata.Assignment result = new DatafeedNodeSelector( + clusterState, resolver, df.getId(), df.getJobId(), df.getIndices(), - SearchRequest.DEFAULT_INDICES_OPTIONS).selectNode(makeCandidateNodes("node_id", "other_node_id")); + SearchRequest.DEFAULT_INDICES_OPTIONS + ).selectNode(makeCandidateNodes("node_id", "other_node_id")); assertEquals("node_id", result.getExecutorNode()); - new DatafeedNodeSelector(clusterState, - resolver, - df.getId(), - df.getJobId(), - df.getIndices(), - SearchRequest.DEFAULT_INDICES_OPTIONS).checkDatafeedTaskCanBeCreated(); + new DatafeedNodeSelector(clusterState, resolver, df.getId(), df.getJobId(), df.getIndices(), SearchRequest.DEFAULT_INDICES_OPTIONS) + .checkDatafeedTaskCanBeCreated(); } public void testSelectNode_GivenJobIsOpenedAndDataStream() { Job job = createScheduledJob("job_id").build(new Date()); DatafeedConfig df = createDatafeed("datafeed_id", job.getId(), Collections.singletonList("foo")); - PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); + PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); addJobTask(job.getId(), "node_id", JobState.OPENED, tasksBuilder); tasks = tasksBuilder.build(); - givenClusterStateWithDatastream("foo", - 1, - 0, - Collections.singletonList(new Tuple<>(0, ShardRoutingState.STARTED))); + givenClusterStateWithDatastream("foo", 1, 0, Collections.singletonList(new Tuple<>(0, ShardRoutingState.STARTED))); - PersistentTasksCustomMetadata.Assignment result = new DatafeedNodeSelector(clusterState, + PersistentTasksCustomMetadata.Assignment result = new DatafeedNodeSelector( + clusterState, resolver, df.getId(), df.getJobId(), df.getIndices(), - SearchRequest.DEFAULT_INDICES_OPTIONS).selectNode(makeCandidateNodes("node_id", "other_node_id")); + SearchRequest.DEFAULT_INDICES_OPTIONS + ).selectNode(makeCandidateNodes("node_id", "other_node_id")); assertEquals("node_id", result.getExecutorNode()); - new DatafeedNodeSelector(clusterState, - resolver, - df.getId(), - df.getJobId(), - df.getIndices(), - SearchRequest.DEFAULT_INDICES_OPTIONS).checkDatafeedTaskCanBeCreated(); + new DatafeedNodeSelector(clusterState, resolver, df.getId(), df.getJobId(), df.getIndices(), SearchRequest.DEFAULT_INDICES_OPTIONS) + .checkDatafeedTaskCanBeCreated(); } public void testSelectNode_GivenJobIsOpening() { Job job = createScheduledJob("job_id").build(new Date()); DatafeedConfig df = createDatafeed("datafeed_id", job.getId(), Collections.singletonList("foo")); - PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); + PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); addJobTask(job.getId(), "node_id", null, tasksBuilder); tasks = tasksBuilder.build(); givenClusterState("foo", 1, 0); - PersistentTasksCustomMetadata.Assignment result = new DatafeedNodeSelector(clusterState, + PersistentTasksCustomMetadata.Assignment result = new DatafeedNodeSelector( + clusterState, resolver, df.getId(), df.getJobId(), df.getIndices(), - SearchRequest.DEFAULT_INDICES_OPTIONS).selectNode(makeCandidateNodes("node_id", "other_node_id")); + SearchRequest.DEFAULT_INDICES_OPTIONS + ).selectNode(makeCandidateNodes("node_id", "other_node_id")); assertEquals("node_id", result.getExecutorNode()); - new DatafeedNodeSelector(clusterState, - resolver, - df.getId(), - df.getJobId(), - df.getIndices(), - SearchRequest.DEFAULT_INDICES_OPTIONS).checkDatafeedTaskCanBeCreated(); + new DatafeedNodeSelector(clusterState, resolver, df.getId(), df.getJobId(), df.getIndices(), SearchRequest.DEFAULT_INDICES_OPTIONS) + .checkDatafeedTaskCanBeCreated(); } public void testNoJobTask() { @@ -164,58 +163,87 @@ public void testNoJobTask() { givenClusterState("foo", 1, 0); - PersistentTasksCustomMetadata.Assignment result = new DatafeedNodeSelector(clusterState, + PersistentTasksCustomMetadata.Assignment result = new DatafeedNodeSelector( + clusterState, resolver, df.getId(), df.getJobId(), df.getIndices(), - SearchRequest.DEFAULT_INDICES_OPTIONS).selectNode(makeCandidateNodes("node_id", "other_node_id")); + SearchRequest.DEFAULT_INDICES_OPTIONS + ).selectNode(makeCandidateNodes("node_id", "other_node_id")); assertNull(result.getExecutorNode()); - assertThat(result.getExplanation(), equalTo("cannot start datafeed [datafeed_id], because the job's [job_id] state is " + - "[closed] while state [opened] is required")); - - ElasticsearchException e = expectThrows(ElasticsearchException.class, - () -> new DatafeedNodeSelector(clusterState, - resolver, - df.getId(), - df.getJobId(), - df.getIndices(), - SearchRequest.DEFAULT_INDICES_OPTIONS).checkDatafeedTaskCanBeCreated()); - assertThat(e.getMessage(), containsString("No node found to start datafeed [datafeed_id], allocation explanation " - + "[cannot start datafeed [datafeed_id], because the job's [job_id] state is [closed] while state [opened] is required]")); + assertThat( + result.getExplanation(), + equalTo( + "cannot start datafeed [datafeed_id], because the job's [job_id] state is " + "[closed] while state [opened] is required" + ) + ); + + ElasticsearchException e = expectThrows( + ElasticsearchException.class, + () -> new DatafeedNodeSelector( + clusterState, + resolver, + df.getId(), + df.getJobId(), + df.getIndices(), + SearchRequest.DEFAULT_INDICES_OPTIONS + ).checkDatafeedTaskCanBeCreated() + ); + assertThat( + e.getMessage(), + containsString( + "No node found to start datafeed [datafeed_id], allocation explanation " + + "[cannot start datafeed [datafeed_id], because the job's [job_id] state is [closed] while state [opened] is required]" + ) + ); } public void testSelectNode_GivenJobFailedOrClosed() { Job job = createScheduledJob("job_id").build(new Date()); DatafeedConfig df = createDatafeed("datafeed_id", job.getId(), Collections.singletonList("foo")); - PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); + PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); JobState jobState = randomFrom(JobState.FAILED, JobState.CLOSED); addJobTask(job.getId(), "node_id", jobState, tasksBuilder); tasks = tasksBuilder.build(); givenClusterState("foo", 1, 0); - PersistentTasksCustomMetadata.Assignment result = new DatafeedNodeSelector(clusterState, + PersistentTasksCustomMetadata.Assignment result = new DatafeedNodeSelector( + clusterState, resolver, df.getId(), df.getJobId(), df.getIndices(), - SearchRequest.DEFAULT_INDICES_OPTIONS).selectNode(makeCandidateNodes("node_id", "other_node_id")); + SearchRequest.DEFAULT_INDICES_OPTIONS + ).selectNode(makeCandidateNodes("node_id", "other_node_id")); assertNull(result.getExecutorNode()); - assertEquals("cannot start datafeed [datafeed_id], because the job's [job_id] state is [" + jobState + - "] while state [opened] is required", result.getExplanation()); - - ElasticsearchException e = expectThrows(ElasticsearchException.class, - () -> new DatafeedNodeSelector(clusterState, - resolver, - df.getId(), - df.getJobId(), - df.getIndices(), - SearchRequest.DEFAULT_INDICES_OPTIONS).checkDatafeedTaskCanBeCreated()); - assertThat(e.getMessage(), containsString("No node found to start datafeed [datafeed_id], allocation explanation " - + "[cannot start datafeed [datafeed_id], because the job's [job_id] state is [" + jobState - + "] while state [opened] is required]")); + assertEquals( + "cannot start datafeed [datafeed_id], because the job's [job_id] state is [" + jobState + "] while state [opened] is required", + result.getExplanation() + ); + + ElasticsearchException e = expectThrows( + ElasticsearchException.class, + () -> new DatafeedNodeSelector( + clusterState, + resolver, + df.getId(), + df.getJobId(), + df.getIndices(), + SearchRequest.DEFAULT_INDICES_OPTIONS + ).checkDatafeedTaskCanBeCreated() + ); + assertThat( + e.getMessage(), + containsString( + "No node found to start datafeed [datafeed_id], allocation explanation " + + "[cannot start datafeed [datafeed_id], because the job's [job_id] state is [" + + jobState + + "] while state [opened] is required]" + ) + ); } public void testShardUnassigned() { @@ -224,7 +252,7 @@ public void testShardUnassigned() { // Using wildcard index name to test for index resolving as well DatafeedConfig df = createDatafeed("datafeed_id", job.getId(), Collections.singletonList("fo*")); - PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); + PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); addJobTask(job.getId(), "node_id", JobState.OPENED, tasksBuilder); tasks = tasksBuilder.build(); @@ -233,22 +261,22 @@ public void testShardUnassigned() { givenClusterState("foo", 1, 0, states); - PersistentTasksCustomMetadata.Assignment result = new DatafeedNodeSelector(clusterState, + PersistentTasksCustomMetadata.Assignment result = new DatafeedNodeSelector( + clusterState, resolver, df.getId(), df.getJobId(), df.getIndices(), - SearchRequest.DEFAULT_INDICES_OPTIONS).selectNode(makeCandidateNodes("node_id", "other_node_id")); + SearchRequest.DEFAULT_INDICES_OPTIONS + ).selectNode(makeCandidateNodes("node_id", "other_node_id")); assertNull(result.getExecutorNode()); - assertThat(result.getExplanation(), equalTo("cannot start datafeed [datafeed_id] because index [foo] " + - "does not have all primary shards active yet.")); + assertThat( + result.getExplanation(), + equalTo("cannot start datafeed [datafeed_id] because index [foo] " + "does not have all primary shards active yet.") + ); - new DatafeedNodeSelector(clusterState, - resolver, - df.getId(), - df.getJobId(), - df.getIndices(), - SearchRequest.DEFAULT_INDICES_OPTIONS).checkDatafeedTaskCanBeCreated(); + new DatafeedNodeSelector(clusterState, resolver, df.getId(), df.getJobId(), df.getIndices(), SearchRequest.DEFAULT_INDICES_OPTIONS) + .checkDatafeedTaskCanBeCreated(); } public void testShardNotAllActive() { @@ -257,7 +285,7 @@ public void testShardNotAllActive() { // Using wildcard index name to test for index resolving as well DatafeedConfig df = createDatafeed("datafeed_id", job.getId(), Collections.singletonList("fo*")); - PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); + PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); addJobTask(job.getId(), "node_id", JobState.OPENED, tasksBuilder); tasks = tasksBuilder.build(); @@ -267,104 +295,119 @@ public void testShardNotAllActive() { givenClusterState("foo", 2, 0, states); - PersistentTasksCustomMetadata.Assignment result = new DatafeedNodeSelector(clusterState, + PersistentTasksCustomMetadata.Assignment result = new DatafeedNodeSelector( + clusterState, resolver, df.getId(), df.getJobId(), df.getIndices(), - SearchRequest.DEFAULT_INDICES_OPTIONS).selectNode(makeCandidateNodes("node_id", "other_node_id")); + SearchRequest.DEFAULT_INDICES_OPTIONS + ).selectNode(makeCandidateNodes("node_id", "other_node_id")); assertNull(result.getExecutorNode()); - assertThat(result.getExplanation(), equalTo("cannot start datafeed [datafeed_id] because index [foo] " + - "does not have all primary shards active yet.")); + assertThat( + result.getExplanation(), + equalTo("cannot start datafeed [datafeed_id] because index [foo] " + "does not have all primary shards active yet.") + ); - new DatafeedNodeSelector(clusterState, - resolver, - df.getId(), - df.getJobId(), - df.getIndices(), - SearchRequest.DEFAULT_INDICES_OPTIONS).checkDatafeedTaskCanBeCreated(); + new DatafeedNodeSelector(clusterState, resolver, df.getId(), df.getJobId(), df.getIndices(), SearchRequest.DEFAULT_INDICES_OPTIONS) + .checkDatafeedTaskCanBeCreated(); } public void testIndexDoesntExist() { Job job = createScheduledJob("job_id").build(new Date()); DatafeedConfig df = createDatafeed("datafeed_id", job.getId(), Collections.singletonList("not_foo")); - PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); + PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); addJobTask(job.getId(), "node_id", JobState.OPENED, tasksBuilder); tasks = tasksBuilder.build(); givenClusterState("foo", 1, 0); - PersistentTasksCustomMetadata.Assignment result = new DatafeedNodeSelector(clusterState, + PersistentTasksCustomMetadata.Assignment result = new DatafeedNodeSelector( + clusterState, resolver, df.getId(), df.getJobId(), df.getIndices(), - SearchRequest.DEFAULT_INDICES_OPTIONS).selectNode(makeCandidateNodes("node_id", "other_node_id")); + SearchRequest.DEFAULT_INDICES_OPTIONS + ).selectNode(makeCandidateNodes("node_id", "other_node_id")); assertNull(result.getExecutorNode()); - assertThat(result.getExplanation(), - equalTo("cannot start datafeed [datafeed_id] because it failed resolving indices given [not_foo] and " + - "indices_options [IndicesOptions[ignore_unavailable=false, allow_no_indices=true, expand_wildcards_open=true, " + - "expand_wildcards_closed=false, expand_wildcards_hidden=false, allow_aliases_to_multiple_indices=true, " + - "forbid_closed_indices=true, ignore_aliases=false, ignore_throttled=true]] " + - "with exception [no such index [not_foo]]")); - - ElasticsearchException e = expectThrows(ElasticsearchException.class, - () -> new DatafeedNodeSelector(clusterState, - resolver, - df.getId(), - df.getJobId(), - df.getIndices(), - SearchRequest.DEFAULT_INDICES_OPTIONS).checkDatafeedTaskCanBeCreated()); - assertThat(e.getMessage(), containsString("No node found to start datafeed [datafeed_id], allocation explanation " - + "[cannot start datafeed [datafeed_id] because it failed resolving " + - "indices given [not_foo] and indices_options [IndicesOptions[ignore_unavailable=false, allow_no_indices=true, " + - "expand_wildcards_open=true, expand_wildcards_closed=false, expand_wildcards_hidden=false, " + - "allow_aliases_to_multiple_indices=true, forbid_closed_indices=true, ignore_aliases=false, ignore_throttled=true" + - "]] with exception [no such index [not_foo]]]")); + assertThat( + result.getExplanation(), + equalTo( + "cannot start datafeed [datafeed_id] because it failed resolving indices given [not_foo] and " + + "indices_options [IndicesOptions[ignore_unavailable=false, allow_no_indices=true, expand_wildcards_open=true, " + + "expand_wildcards_closed=false, expand_wildcards_hidden=false, allow_aliases_to_multiple_indices=true, " + + "forbid_closed_indices=true, ignore_aliases=false, ignore_throttled=true]] " + + "with exception [no such index [not_foo]]" + ) + ); + + ElasticsearchException e = expectThrows( + ElasticsearchException.class, + () -> new DatafeedNodeSelector( + clusterState, + resolver, + df.getId(), + df.getJobId(), + df.getIndices(), + SearchRequest.DEFAULT_INDICES_OPTIONS + ).checkDatafeedTaskCanBeCreated() + ); + assertThat( + e.getMessage(), + containsString( + "No node found to start datafeed [datafeed_id], allocation explanation " + + "[cannot start datafeed [datafeed_id] because it failed resolving " + + "indices given [not_foo] and indices_options [IndicesOptions[ignore_unavailable=false, allow_no_indices=true, " + + "expand_wildcards_open=true, expand_wildcards_closed=false, expand_wildcards_hidden=false, " + + "allow_aliases_to_multiple_indices=true, forbid_closed_indices=true, ignore_aliases=false, ignore_throttled=true" + + "]] with exception [no such index [not_foo]]]" + ) + ); } public void testIndexPatternDoesntExist() { Job job = createScheduledJob("job_id").build(new Date()); DatafeedConfig df = createDatafeed("datafeed_id", job.getId(), Arrays.asList("missing-*", "foo*")); - PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); + PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); addJobTask(job.getId(), "node_id", JobState.OPENED, tasksBuilder); tasks = tasksBuilder.build(); givenClusterState("foo", 1, 0); - PersistentTasksCustomMetadata.Assignment result = new DatafeedNodeSelector(clusterState, + PersistentTasksCustomMetadata.Assignment result = new DatafeedNodeSelector( + clusterState, resolver, df.getId(), df.getJobId(), df.getIndices(), - SearchRequest.DEFAULT_INDICES_OPTIONS).selectNode(makeCandidateNodes("node_id", "other_node_id")); + SearchRequest.DEFAULT_INDICES_OPTIONS + ).selectNode(makeCandidateNodes("node_id", "other_node_id")); assertEquals("node_id", result.getExecutorNode()); - new DatafeedNodeSelector(clusterState, - resolver, - df.getId(), - df.getJobId(), - df.getIndices(), - SearchRequest.DEFAULT_INDICES_OPTIONS).checkDatafeedTaskCanBeCreated(); + new DatafeedNodeSelector(clusterState, resolver, df.getId(), df.getJobId(), df.getIndices(), SearchRequest.DEFAULT_INDICES_OPTIONS) + .checkDatafeedTaskCanBeCreated(); } public void testRemoteIndex() { Job job = createScheduledJob("job_id").build(new Date()); DatafeedConfig df = createDatafeed("datafeed_id", job.getId(), Collections.singletonList("remote:foo")); - PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); + PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); addJobTask(job.getId(), "node_id", JobState.OPENED, tasksBuilder); tasks = tasksBuilder.build(); givenClusterState("foo", 1, 0); - PersistentTasksCustomMetadata.Assignment result = new DatafeedNodeSelector(clusterState, + PersistentTasksCustomMetadata.Assignment result = new DatafeedNodeSelector( + clusterState, resolver, df.getId(), df.getJobId(), df.getIndices(), - SearchRequest.DEFAULT_INDICES_OPTIONS).selectNode(makeCandidateNodes("node_id", "other_node_id")); + SearchRequest.DEFAULT_INDICES_OPTIONS + ).selectNode(makeCandidateNodes("node_id", "other_node_id")); assertNotNull(result.getExecutorNode()); } @@ -373,7 +416,7 @@ public void testSelectNode_jobTaskStale() { DatafeedConfig df = createDatafeed("datafeed_id", job.getId(), Collections.singletonList("foo")); String nodeId = randomBoolean() ? "node_id2" : null; - PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); + PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); addJobTask(job.getId(), nodeId, JobState.OPENED, tasksBuilder); // Set to lower allocationId, so job task is stale: tasksBuilder.updateTaskState(MlTasks.jobTaskId(job.getId()), new JobTaskState(JobState.OPENED, 0, null)); @@ -383,43 +426,51 @@ public void testSelectNode_jobTaskStale() { Collection candidateNodes = makeCandidateNodes("node_id1", "node_id2", "node_id3"); - PersistentTasksCustomMetadata.Assignment result = new DatafeedNodeSelector(clusterState, + PersistentTasksCustomMetadata.Assignment result = new DatafeedNodeSelector( + clusterState, resolver, df.getId(), df.getJobId(), df.getIndices(), - SearchRequest.DEFAULT_INDICES_OPTIONS).selectNode(candidateNodes); + SearchRequest.DEFAULT_INDICES_OPTIONS + ).selectNode(candidateNodes); assertNull(result.getExecutorNode()); - assertEquals("cannot start datafeed [datafeed_id], because the job's [job_id] state is stale", - result.getExplanation()); - - ElasticsearchException e = expectThrows(ElasticsearchException.class, - () -> new DatafeedNodeSelector(clusterState, - resolver, - df.getId(), - df.getJobId(), - df.getIndices(), - SearchRequest.DEFAULT_INDICES_OPTIONS).checkDatafeedTaskCanBeCreated()); - assertThat(e.getMessage(), containsString("No node found to start datafeed [datafeed_id], allocation explanation " - + "[cannot start datafeed [datafeed_id], because the job's [job_id] state is stale]")); - - tasksBuilder = PersistentTasksCustomMetadata.builder(); + assertEquals("cannot start datafeed [datafeed_id], because the job's [job_id] state is stale", result.getExplanation()); + + ElasticsearchException e = expectThrows( + ElasticsearchException.class, + () -> new DatafeedNodeSelector( + clusterState, + resolver, + df.getId(), + df.getJobId(), + df.getIndices(), + SearchRequest.DEFAULT_INDICES_OPTIONS + ).checkDatafeedTaskCanBeCreated() + ); + assertThat( + e.getMessage(), + containsString( + "No node found to start datafeed [datafeed_id], allocation explanation " + + "[cannot start datafeed [datafeed_id], because the job's [job_id] state is stale]" + ) + ); + + tasksBuilder = PersistentTasksCustomMetadata.builder(); addJobTask(job.getId(), "node_id1", JobState.OPENED, tasksBuilder); tasks = tasksBuilder.build(); givenClusterState("foo", 1, 0); - result = new DatafeedNodeSelector(clusterState, + result = new DatafeedNodeSelector( + clusterState, resolver, df.getId(), df.getJobId(), df.getIndices(), - SearchRequest.DEFAULT_INDICES_OPTIONS).selectNode(candidateNodes); + SearchRequest.DEFAULT_INDICES_OPTIONS + ).selectNode(candidateNodes); assertEquals("node_id1", result.getExecutorNode()); - new DatafeedNodeSelector(clusterState, - resolver, - df.getId(), - df.getJobId(), - df.getIndices(), - SearchRequest.DEFAULT_INDICES_OPTIONS).checkDatafeedTaskCanBeCreated(); + new DatafeedNodeSelector(clusterState, resolver, df.getId(), df.getJobId(), df.getIndices(), SearchRequest.DEFAULT_INDICES_OPTIONS) + .checkDatafeedTaskCanBeCreated(); } public void testSelectNode_GivenJobOpeningAndIndexDoesNotExist() { @@ -435,38 +486,49 @@ public void testSelectNode_GivenJobOpeningAndIndexDoesNotExist() { givenClusterState("foo", 1, 0); - ElasticsearchException e = expectThrows(ElasticsearchException.class, - () -> new DatafeedNodeSelector(clusterState, - resolver, - df.getId(), - df.getJobId(), - df.getIndices(), - SearchRequest.DEFAULT_INDICES_OPTIONS).checkDatafeedTaskCanBeCreated()); - assertThat(e.getMessage(), containsString("No node found to start datafeed [datafeed_id], allocation explanation " - + "[cannot start datafeed [datafeed_id] because it failed resolving indices given [not_foo] and " + - "indices_options [IndicesOptions[ignore_unavailable=false, allow_no_indices=true, expand_wildcards_open=true, " + - "expand_wildcards_closed=false, expand_wildcards_hidden=false, allow_aliases_to_multiple_indices=true, " + - "forbid_closed_indices=true, ignore_aliases=false, ignore_throttled=true]] " + - "with exception [no such index [not_foo]]]")); + ElasticsearchException e = expectThrows( + ElasticsearchException.class, + () -> new DatafeedNodeSelector( + clusterState, + resolver, + df.getId(), + df.getJobId(), + df.getIndices(), + SearchRequest.DEFAULT_INDICES_OPTIONS + ).checkDatafeedTaskCanBeCreated() + ); + assertThat( + e.getMessage(), + containsString( + "No node found to start datafeed [datafeed_id], allocation explanation " + + "[cannot start datafeed [datafeed_id] because it failed resolving indices given [not_foo] and " + + "indices_options [IndicesOptions[ignore_unavailable=false, allow_no_indices=true, expand_wildcards_open=true, " + + "expand_wildcards_closed=false, expand_wildcards_hidden=false, allow_aliases_to_multiple_indices=true, " + + "forbid_closed_indices=true, ignore_aliases=false, ignore_throttled=true]] " + + "with exception [no such index [not_foo]]]" + ) + ); } public void testSelectNode_GivenMlUpgradeMode() { Job job = createScheduledJob("job_id").build(new Date()); DatafeedConfig df = createDatafeed("datafeed_id", job.getId(), Collections.singletonList("foo")); - PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); + PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); addJobTask(job.getId(), "node_id", JobState.OPENED, tasksBuilder); tasks = tasksBuilder.build(); mlMetadata = new MlMetadata.Builder().isUpgradeMode(true).build(); givenClusterState("foo", 1, 0); - PersistentTasksCustomMetadata.Assignment result = new DatafeedNodeSelector(clusterState, + PersistentTasksCustomMetadata.Assignment result = new DatafeedNodeSelector( + clusterState, resolver, df.getId(), df.getJobId(), df.getIndices(), - SearchRequest.DEFAULT_INDICES_OPTIONS).selectNode(makeCandidateNodes("node_id", "other_node_id")); + SearchRequest.DEFAULT_INDICES_OPTIONS + ).selectNode(makeCandidateNodes("node_id", "other_node_id")); assertThat(result, equalTo(MlTasks.AWAITING_UPGRADE)); } @@ -474,19 +536,21 @@ public void testSelectNode_GivenResetInProgress() { Job job = createScheduledJob("job_id").build(new Date()); DatafeedConfig df = createDatafeed("datafeed_id", job.getId(), Collections.singletonList("foo")); - PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); + PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); addJobTask(job.getId(), "node_id", JobState.OPENED, tasksBuilder); tasks = tasksBuilder.build(); mlMetadata = new MlMetadata.Builder().isResetMode(true).build(); givenClusterState("foo", 1, 0); - PersistentTasksCustomMetadata.Assignment result = new DatafeedNodeSelector(clusterState, + PersistentTasksCustomMetadata.Assignment result = new DatafeedNodeSelector( + clusterState, resolver, df.getId(), df.getJobId(), df.getIndices(), - SearchRequest.DEFAULT_INDICES_OPTIONS).selectNode(makeCandidateNodes("node_id", "other_node_id")); + SearchRequest.DEFAULT_INDICES_OPTIONS + ).selectNode(makeCandidateNodes("node_id", "other_node_id")); assertThat(result, equalTo(MlTasks.RESET_IN_PROGRESS)); } @@ -494,20 +558,24 @@ public void testCheckDatafeedTaskCanBeCreated_GivenMlUpgradeMode() { Job job = createScheduledJob("job_id").build(new Date()); DatafeedConfig df = createDatafeed("datafeed_id", job.getId(), Collections.singletonList("foo")); - PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); + PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); addJobTask(job.getId(), "node_id", JobState.OPENED, tasksBuilder); tasks = tasksBuilder.build(); mlMetadata = new MlMetadata.Builder().isUpgradeMode(true).build(); givenClusterState("foo", 1, 0); - ElasticsearchException e = expectThrows(ElasticsearchException.class, - () -> new DatafeedNodeSelector(clusterState, + ElasticsearchException e = expectThrows( + ElasticsearchException.class, + () -> new DatafeedNodeSelector( + clusterState, resolver, df.getId(), df.getJobId(), df.getIndices(), - SearchRequest.DEFAULT_INDICES_OPTIONS).checkDatafeedTaskCanBeCreated()); + SearchRequest.DEFAULT_INDICES_OPTIONS + ).checkDatafeedTaskCanBeCreated() + ); assertThat(e.getMessage(), equalTo("Could not start datafeed [datafeed_id] as indices are being upgraded")); } @@ -515,32 +583,30 @@ public void testSelectNode_GivenJobIsOpenedAndNodeIsShuttingDown() { Job job = createScheduledJob("job_id").build(new Date()); DatafeedConfig df = createDatafeed("datafeed_id", job.getId(), Collections.singletonList("foo")); - PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); + PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); addJobTask(job.getId(), "node_id", JobState.OPENED, tasksBuilder); tasks = tasksBuilder.build(); givenClusterState("foo", 1, 0); - PersistentTasksCustomMetadata.Assignment result = new DatafeedNodeSelector(clusterState, + PersistentTasksCustomMetadata.Assignment result = new DatafeedNodeSelector( + clusterState, resolver, df.getId(), df.getJobId(), df.getIndices(), - SearchRequest.DEFAULT_INDICES_OPTIONS).selectNode(makeCandidateNodes("other_node_id")); + SearchRequest.DEFAULT_INDICES_OPTIONS + ).selectNode(makeCandidateNodes("other_node_id")); assertNull(result.getExecutorNode()); assertEquals("datafeed awaiting job relocation.", result.getExplanation()); // This is different to the pattern of the other tests - we allow the datafeed task to be - // created even though it cannot be assigned. The reason is that it would be perverse for + // created even though it cannot be assigned. The reason is that it would be perverse for // start datafeed to throw an error just because a user was unlucky and opened a job just // before a node got shut down, such that their subsequent call to start its datafeed arrived // after that node was shutting down. - new DatafeedNodeSelector(clusterState, - resolver, - df.getId(), - df.getJobId(), - df.getIndices(), - SearchRequest.DEFAULT_INDICES_OPTIONS).checkDatafeedTaskCanBeCreated(); + new DatafeedNodeSelector(clusterState, resolver, df.getId(), df.getJobId(), df.getIndices(), SearchRequest.DEFAULT_INDICES_OPTIONS) + .checkDatafeedTaskCanBeCreated(); } private void givenClusterState(String index, int numberOfShards, int numberOfReplicas) { @@ -551,25 +617,28 @@ private void givenClusterState(String index, int numberOfShards, int numberOfRep private void givenClusterState(String index, int numberOfShards, int numberOfReplicas, List> states) { IndexMetadata indexMetadata = IndexMetadata.builder(index) - .settings(settings(Version.CURRENT)) - .numberOfShards(numberOfShards) - .numberOfReplicas(numberOfReplicas) - .build(); + .settings(settings(Version.CURRENT)) + .numberOfShards(numberOfShards) + .numberOfReplicas(numberOfReplicas) + .build(); clusterState = ClusterState.builder(new ClusterName("cluster_name")) - .metadata(new Metadata.Builder() - .putCustom(PersistentTasksCustomMetadata.TYPE, tasks) - .putCustom(MlMetadata.TYPE, mlMetadata) - .put(indexMetadata, false)) - .nodes(nodes) - .routingTable(generateRoutingTable(indexMetadata, states)) - .build(); + .metadata( + new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasks) + .putCustom(MlMetadata.TYPE, mlMetadata) + .put(indexMetadata, false) + ) + .nodes(nodes) + .routingTable(generateRoutingTable(indexMetadata, states)) + .build(); } - private void givenClusterStateWithDatastream(String dataStreamName, - int numberOfShards, - int numberOfReplicas, - List> states) { + private void givenClusterStateWithDatastream( + String dataStreamName, + int numberOfShards, + int numberOfReplicas, + List> states + ) { Index index = new Index(getDefaultBackingIndexName(dataStreamName, 1), INDEX_UUID_NA_VALUE); IndexMetadata indexMetadata = IndexMetadata.builder(index.getName()) .settings(settings(Version.CURRENT)) @@ -578,11 +647,11 @@ private void givenClusterStateWithDatastream(String dataStreamName, .build(); clusterState = ClusterState.builder(new ClusterName("cluster_name")) - .metadata(new Metadata.Builder() - .put(new DataStream(dataStreamName, createTimestampField("@timestamp"), Collections.singletonList(index))) - .putCustom(PersistentTasksCustomMetadata.TYPE, tasks) - .putCustom(MlMetadata.TYPE, mlMetadata) - .put(indexMetadata, false)) + .metadata( + new Metadata.Builder().put( + new DataStream(dataStreamName, createTimestampField("@timestamp"), Collections.singletonList(index)) + ).putCustom(PersistentTasksCustomMetadata.TYPE, tasks).putCustom(MlMetadata.TYPE, mlMetadata).put(indexMetadata, false) + ) .nodes(nodes) .routingTable(generateRoutingTable(indexMetadata, states)) .build(); @@ -599,19 +668,39 @@ private static RoutingTable generateRoutingTable(IndexMetadata indexMetadata, Li ShardRouting shardRouting; if (state.v2().equals(ShardRoutingState.STARTED)) { - shardRouting = TestShardRouting.newShardRouting(index, shardId.getId(), - "node_" + Integer.toString(state.v1()), null, true, ShardRoutingState.STARTED); + shardRouting = TestShardRouting.newShardRouting( + index, + shardId.getId(), + "node_" + Integer.toString(state.v1()), + null, + true, + ShardRoutingState.STARTED + ); } else if (state.v2().equals(ShardRoutingState.INITIALIZING)) { - shardRouting = TestShardRouting.newShardRouting(index, shardId.getId(), - "node_" + Integer.toString(state.v1()), null, true, ShardRoutingState.INITIALIZING); + shardRouting = TestShardRouting.newShardRouting( + index, + shardId.getId(), + "node_" + Integer.toString(state.v1()), + null, + true, + ShardRoutingState.INITIALIZING + ); } else if (state.v2().equals(ShardRoutingState.RELOCATING)) { - shardRouting = TestShardRouting.newShardRouting(index, shardId.getId(), - "node_" + Integer.toString(state.v1()), "node_" + Integer.toString((state.v1() + 1) % 3), - true, ShardRoutingState.RELOCATING); + shardRouting = TestShardRouting.newShardRouting( + index, + shardId.getId(), + "node_" + Integer.toString(state.v1()), + "node_" + Integer.toString((state.v1() + 1) % 3), + true, + ShardRoutingState.RELOCATING + ); } else { - shardRouting = ShardRouting.newUnassigned(shardId, true, - RecoverySource.EmptyStoreRecoverySource.INSTANCE, - new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "")); + shardRouting = ShardRouting.newUnassigned( + shardId, + true, + RecoverySource.EmptyStoreRecoverySource.INSTANCE, + new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "") + ); } shardRTBuilder.addShard(shardRouting); @@ -626,8 +715,16 @@ Collection makeCandidateNodes(String... nodeIds) { List candidateNodes = new ArrayList<>(); int port = 9300; for (String nodeId : nodeIds) { - candidateNodes.add(new DiscoveryNode(nodeId + "-name", nodeId, new TransportAddress(InetAddress.getLoopbackAddress(), port++), - Collections.emptyMap(), DiscoveryNodeRole.roles(), Version.CURRENT)); + candidateNodes.add( + new DiscoveryNode( + nodeId + "-name", + nodeId, + new TransportAddress(InetAddress.getLoopbackAddress(), port++), + Collections.emptyMap(), + DiscoveryNodeRole.roles(), + Version.CURRENT + ) + ); } return candidateNodes; } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedRunnerTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedRunnerTests.java index 44cf23bcb5320..1a13b185f9fbe 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedRunnerTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedRunnerTests.java @@ -55,11 +55,11 @@ import static org.elasticsearch.xpack.ml.job.task.OpenJobPersistentTasksExecutorTests.addJobTask; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; -import static org.mockito.ArgumentMatchers.nullable; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.nullable; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; @@ -93,12 +93,20 @@ public void setUpTests() { addJobTask(job.getId(), "node_id", JobState.OPENED, tasksBuilder); PersistentTasksCustomMetadata tasks = tasksBuilder.build(); DiscoveryNodes nodes = DiscoveryNodes.builder() - .add(new DiscoveryNode("node_name", "node_id", new TransportAddress(InetAddress.getLoopbackAddress(), 9300), - Collections.emptyMap(), Collections.emptySet(), Version.CURRENT)) - .build(); + .add( + new DiscoveryNode( + "node_name", + "node_id", + new TransportAddress(InetAddress.getLoopbackAddress(), 9300), + Collections.emptyMap(), + Collections.emptySet(), + Version.CURRENT + ) + ) + .build(); ClusterState.Builder cs = ClusterState.builder(new ClusterName("cluster_name")) - .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasks)) - .nodes(nodes); + .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasks)) + .nodes(nodes); clusterService = mock(ClusterService.class); when(clusterService.state()).thenReturn(cs.build()); @@ -139,8 +147,16 @@ public void setUpTests() { DatafeedConfig.Builder datafeedConfig = createDatafeedConfig(DATAFEED_ID, job.getId()); givenDatafeedHasNeverRunBefore(job.build(), datafeedConfig.build()); - datafeedRunner = new DatafeedRunner(threadPool, mock(Client.class), clusterService, datafeedJobBuilder, - () -> currentTime, auditor, autodetectProcessManager, datafeedContextProvider); + datafeedRunner = new DatafeedRunner( + threadPool, + mock(Client.class), + clusterService, + datafeedJobBuilder, + () -> currentTime, + auditor, + autodetectProcessManager, + datafeedContextProvider + ); verify(clusterService).addListener(capturedClusterStateListener.capture()); } @@ -179,7 +195,7 @@ public void testStart_extractionProblem() throws Exception { public void testStart_emptyDataCountException() throws Exception { currentTime = 6000000; - int[] counter = new int[] {0}; + int[] counter = new int[] { 0 }; doAnswer(invocationOnMock -> { if (counter[0]++ < 10) { Runnable r = (Runnable) invocationOnMock.getArguments()[0]; @@ -206,13 +222,13 @@ public void testRealTime_GivenStoppingAnalysisProblem() throws Exception { Consumer handler = mockConsumer(); StartDatafeedAction.DatafeedParams params = new StartDatafeedAction.DatafeedParams(DATAFEED_ID, 0L); - DatafeedTask task = TransportStartDatafeedActionTests.createDatafeedTask(1, "type", "action", null, - params, datafeedRunner); + DatafeedTask task = TransportStartDatafeedActionTests.createDatafeedTask(1, "type", "action", null, params, datafeedRunner); task = spyDatafeedTask(task); datafeedRunner.run(task, handler); - ArgumentCaptor analysisProblemCaptor = - ArgumentCaptor.forClass(DatafeedJob.AnalysisProblemException.class); + ArgumentCaptor analysisProblemCaptor = ArgumentCaptor.forClass( + DatafeedJob.AnalysisProblemException.class + ); verify(handler).accept(analysisProblemCaptor.capture()); assertThat(analysisProblemCaptor.getValue().getCause(), equalTo(cause)); verify(auditor).error(JOB_ID, "Datafeed is encountering errors submitting data for analysis: stopping"); @@ -221,13 +237,13 @@ public void testRealTime_GivenStoppingAnalysisProblem() throws Exception { public void testRealTime_GivenNonStoppingAnalysisProblem() throws Exception { Exception cause = new RuntimeException("non-stopping"); - when(datafeedJob.runLookBack(anyLong(), nullable(Long.class))) - .thenThrow(new DatafeedJob.AnalysisProblemException(0L, false, cause)); + when(datafeedJob.runLookBack(anyLong(), nullable(Long.class))).thenThrow( + new DatafeedJob.AnalysisProblemException(0L, false, cause) + ); Consumer handler = mockConsumer(); StartDatafeedAction.DatafeedParams params = new StartDatafeedAction.DatafeedParams(DATAFEED_ID, 0L); - DatafeedTask task = TransportStartDatafeedActionTests.createDatafeedTask(1, "type", "action", null, - params, datafeedRunner); + DatafeedTask task = TransportStartDatafeedActionTests.createDatafeedTask(1, "type", "action", null, params, datafeedRunner); task = spyDatafeedTask(task); datafeedRunner.run(task, handler); @@ -242,8 +258,7 @@ public void testStart_GivenNewlyCreatedJobLookBackAndRealtime() throws Exception Consumer handler = mockConsumer(); boolean cancelled = randomBoolean(); StartDatafeedAction.DatafeedParams params = new StartDatafeedAction.DatafeedParams(DATAFEED_ID, 0L); - DatafeedTask task = TransportStartDatafeedActionTests.createDatafeedTask(1, "type", "action", null, - params, datafeedRunner); + DatafeedTask task = TransportStartDatafeedActionTests.createDatafeedTask(1, "type", "action", null, params, datafeedRunner); task = spyDatafeedTask(task); datafeedRunner.run(task, handler); @@ -262,7 +277,8 @@ public void testDatafeedTaskWaitsUntilJobIsOpened() { PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); addJobTask(JOB_ID, "node_id", JobState.OPENING, tasksBuilder); ClusterState cs = ClusterState.builder(clusterService.state()) - .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())).build(); + .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())) + .build(); when(clusterService.state()).thenReturn(cs); Consumer handler = mockConsumer(); @@ -276,7 +292,8 @@ public void testDatafeedTaskWaitsUntilJobIsOpened() { addJobTask(JOB_ID, "node_id", JobState.OPENING, tasksBuilder); addJobTask("another_job", "node_id", JobState.OPENED, tasksBuilder); ClusterState anotherJobCs = ClusterState.builder(clusterService.state()) - .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())).build(); + .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())) + .build(); capturedClusterStateListener.getValue().clusterChanged(new ClusterChangedEvent("_source", anotherJobCs, cs)); @@ -286,10 +303,9 @@ public void testDatafeedTaskWaitsUntilJobIsOpened() { tasksBuilder = PersistentTasksCustomMetadata.builder(); addJobTask(JOB_ID, "node_id", JobState.OPENED, tasksBuilder); ClusterState.Builder jobOpenedCs = ClusterState.builder(clusterService.state()) - .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())); + .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())); - capturedClusterStateListener.getValue().clusterChanged( - new ClusterChangedEvent("_source", jobOpenedCs.build(), anotherJobCs)); + capturedClusterStateListener.getValue().clusterChanged(new ClusterChangedEvent("_source", jobOpenedCs.build(), anotherJobCs)); // Now it should run as the job state changed to OPENED verify(threadPool, times(1)).executor(MachineLearning.DATAFEED_THREAD_POOL_NAME); @@ -302,7 +318,8 @@ public void testDatafeedTaskWaitsUntilAutodetectCommunicatorIsOpen() { PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); addJobTask(JOB_ID, "node_id", JobState.OPENED, tasksBuilder); ClusterState cs = ClusterState.builder(clusterService.state()) - .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())).build(); + .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())) + .build(); when(clusterService.state()).thenReturn(cs); Consumer handler = mockConsumer(); @@ -316,7 +333,8 @@ public void testDatafeedTaskWaitsUntilAutodetectCommunicatorIsOpen() { addJobTask(JOB_ID, "node_id", JobState.OPENED, tasksBuilder); addJobTask("another_job", "node_id", JobState.OPENED, tasksBuilder); ClusterState anotherJobCs = ClusterState.builder(clusterService.state()) - .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())).build(); + .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())) + .build(); capturedClusterStateListener.getValue().clusterChanged(new ClusterChangedEvent("_source", anotherJobCs, cs)); @@ -325,8 +343,7 @@ public void testDatafeedTaskWaitsUntilAutodetectCommunicatorIsOpen() { hasOpenAutodetectCommunicator.set(true); - capturedClusterStateListener.getValue().clusterChanged( - new ClusterChangedEvent("_source", cs, anotherJobCs)); + capturedClusterStateListener.getValue().clusterChanged(new ClusterChangedEvent("_source", cs, anotherJobCs)); // Now it should run as the autodetect communicator is open verify(threadPool, times(1)).executor(MachineLearning.DATAFEED_THREAD_POOL_NAME); @@ -336,7 +353,8 @@ public void testDatafeedTaskWaitsUntilJobIsNotStale() { PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); addJobTask(JOB_ID, "node_id", JobState.OPENED, tasksBuilder, true); ClusterState cs = ClusterState.builder(clusterService.state()) - .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())).build(); + .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())) + .build(); when(clusterService.state()).thenReturn(cs); Consumer handler = mockConsumer(); @@ -350,7 +368,8 @@ public void testDatafeedTaskWaitsUntilJobIsNotStale() { addJobTask(JOB_ID, "node_id", JobState.OPENED, tasksBuilder, true); addJobTask("another_job", "node_id", JobState.OPENED, tasksBuilder); ClusterState anotherJobCs = ClusterState.builder(clusterService.state()) - .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())).build(); + .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())) + .build(); capturedClusterStateListener.getValue().clusterChanged(new ClusterChangedEvent("_source", anotherJobCs, cs)); @@ -362,8 +381,7 @@ public void testDatafeedTaskWaitsUntilJobIsNotStale() { ClusterState.Builder jobOpenedCs = ClusterState.builder(clusterService.state()) .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())); - capturedClusterStateListener.getValue().clusterChanged( - new ClusterChangedEvent("_source", jobOpenedCs.build(), anotherJobCs)); + capturedClusterStateListener.getValue().clusterChanged(new ClusterChangedEvent("_source", jobOpenedCs.build(), anotherJobCs)); // Now it should run as the job state chanded to OPENED verify(threadPool, times(1)).executor(MachineLearning.DATAFEED_THREAD_POOL_NAME); @@ -373,7 +391,8 @@ public void testDatafeedTaskStopsBecauseJobFailedWhileOpening() { PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); addJobTask(JOB_ID, "node_id", JobState.OPENING, tasksBuilder); ClusterState cs = ClusterState.builder(clusterService.state()) - .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())).build(); + .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())) + .build(); when(clusterService.state()).thenReturn(cs); Consumer handler = mockConsumer(); @@ -386,7 +405,7 @@ public void testDatafeedTaskStopsBecauseJobFailedWhileOpening() { tasksBuilder = PersistentTasksCustomMetadata.builder(); addJobTask(JOB_ID, "node_id", JobState.FAILED, tasksBuilder); ClusterState.Builder updatedCs = ClusterState.builder(clusterService.state()) - .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())); + .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())); capturedClusterStateListener.getValue().clusterChanged(new ClusterChangedEvent("_source", updatedCs.build(), cs)); @@ -399,7 +418,8 @@ public void testDatafeedGetsStoppedWhileWaitingForJobToOpen() { PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); addJobTask(JOB_ID, "node_id", JobState.OPENING, tasksBuilder); ClusterState cs = ClusterState.builder(clusterService.state()) - .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())).build(); + .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())) + .build(); when(clusterService.state()).thenReturn(cs); Consumer handler = mockConsumer(); @@ -416,7 +436,7 @@ public void testDatafeedGetsStoppedWhileWaitingForJobToOpen() { tasksBuilder = PersistentTasksCustomMetadata.builder(); addJobTask(JOB_ID, "node_id", JobState.OPENED, tasksBuilder); ClusterState.Builder updatedCs = ClusterState.builder(clusterService.state()) - .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())); + .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())); capturedClusterStateListener.getValue().clusterChanged(new ClusterChangedEvent("_source", cs, updatedCs.build())); @@ -428,7 +448,8 @@ public void testDatafeedGetsStoppedWhileStarting() { PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); addJobTask(JOB_ID, "node_id", JobState.OPENED, tasksBuilder); ClusterState cs = ClusterState.builder(clusterService.state()) - .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())).build(); + .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())) + .build(); when(clusterService.state()).thenReturn(cs); Consumer handler = mockConsumer(); @@ -449,8 +470,9 @@ public static DatafeedConfig.Builder createDatafeedConfig(String datafeedId, Str } public static Job.Builder createDatafeedJob() { - AnalysisConfig.Builder acBuilder = new AnalysisConfig.Builder(Collections.singletonList( - new Detector.Builder("metric", "field").build())); + AnalysisConfig.Builder acBuilder = new AnalysisConfig.Builder( + Collections.singletonList(new Detector.Builder("metric", "field").build()) + ); acBuilder.setBucketSpan(TimeValue.timeValueHours(1)); acBuilder.setDetectors(Collections.singletonList(new Detector.Builder("metric", "field").build())); @@ -461,7 +483,7 @@ public static Job.Builder createDatafeedJob() { return builder; } - @SuppressWarnings({"rawtypes", "unchecked"}) + @SuppressWarnings({ "rawtypes", "unchecked" }) private static DatafeedTask createDatafeedTask(String datafeedId, long startTime, Long endTime) { DatafeedTask task = mock(DatafeedTask.class); when(task.getDatafeedId()).thenReturn(datafeedId); @@ -481,7 +503,7 @@ private Consumer mockConsumer() { return mock(Consumer.class); } - @SuppressWarnings({"rawtypes", "unchecked"}) + @SuppressWarnings({ "rawtypes", "unchecked" }) private DatafeedTask spyDatafeedTask(DatafeedTask task) { task = spy(task); doAnswer(invocationOnMock -> { diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedTimingStatsReporterTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedTimingStatsReporterTests.java index f206fee3fd258..cfd71dd54368d 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedTimingStatsReporterTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedTimingStatsReporterTests.java @@ -20,10 +20,10 @@ import java.sql.Date; import java.time.Instant; -import static org.mockito.Mockito.doThrow; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.inOrder; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -81,10 +81,10 @@ public void testReportSearchDuration() { assertThat(reporter.getCurrentTimingStats(), equalTo(createDatafeedTimingStats(JOB_ID, 17, 10, 14000.0, 14000.0))); InOrder inOrder = inOrder(timingStatsPersister); - inOrder.verify(timingStatsPersister).persistDatafeedTimingStats( - createDatafeedTimingStats(JOB_ID, 15, 10, 12000.0, 12000.0), RefreshPolicy.NONE); - inOrder.verify(timingStatsPersister).persistDatafeedTimingStats( - createDatafeedTimingStats(JOB_ID, 17, 10, 14000.0, 14000.0), RefreshPolicy.NONE); + inOrder.verify(timingStatsPersister) + .persistDatafeedTimingStats(createDatafeedTimingStats(JOB_ID, 15, 10, 12000.0, 12000.0), RefreshPolicy.NONE); + inOrder.verify(timingStatsPersister) + .persistDatafeedTimingStats(createDatafeedTimingStats(JOB_ID, 17, 10, 14000.0, 14000.0), RefreshPolicy.NONE); verifyNoMoreInteractions(timingStatsPersister); } @@ -112,8 +112,8 @@ public void testReportDataCounts() { assertThat(reporter.getCurrentTimingStats(), equalTo(createDatafeedTimingStats(JOB_ID, 3, 23, 10000.0))); InOrder inOrder = inOrder(timingStatsPersister); - inOrder.verify(timingStatsPersister).persistDatafeedTimingStats( - createDatafeedTimingStats(JOB_ID, 3, 23, 10000.0), RefreshPolicy.NONE); + inOrder.verify(timingStatsPersister) + .persistDatafeedTimingStats(createDatafeedTimingStats(JOB_ID, 3, 23, 10000.0), RefreshPolicy.NONE); verifyNoMoreInteractions(timingStatsPersister); } @@ -132,7 +132,8 @@ public void testFinishReporting_WithChange() { verify(timingStatsPersister).persistDatafeedTimingStats( new DatafeedTimingStats(JOB_ID, 0, 0, 0.0, new ExponentialAverageCalculationContext(0.0, TIMESTAMP, null)), - RefreshPolicy.IMMEDIATE); + RefreshPolicy.IMMEDIATE + ); verifyNoMoreInteractions(timingStatsPersister); } @@ -148,36 +149,60 @@ public void testDisallowPersisting() { public void testTimingStatsDifferSignificantly() { assertThat( DatafeedTimingStatsReporter.differSignificantly( - createDatafeedTimingStats(JOB_ID, 5, 10, 1000.0), createDatafeedTimingStats(JOB_ID, 5, 10, 1000.0)), - is(false)); + createDatafeedTimingStats(JOB_ID, 5, 10, 1000.0), + createDatafeedTimingStats(JOB_ID, 5, 10, 1000.0) + ), + is(false) + ); assertThat( DatafeedTimingStatsReporter.differSignificantly( - createDatafeedTimingStats(JOB_ID, 5, 10, 1000.0), createDatafeedTimingStats(JOB_ID, 5, 10, 1100.0)), - is(false)); + createDatafeedTimingStats(JOB_ID, 5, 10, 1000.0), + createDatafeedTimingStats(JOB_ID, 5, 10, 1100.0) + ), + is(false) + ); assertThat( DatafeedTimingStatsReporter.differSignificantly( - createDatafeedTimingStats(JOB_ID, 5, 10, 1000.0), createDatafeedTimingStats(JOB_ID, 5, 10, 1120.0)), - is(true)); + createDatafeedTimingStats(JOB_ID, 5, 10, 1000.0), + createDatafeedTimingStats(JOB_ID, 5, 10, 1120.0) + ), + is(true) + ); assertThat( DatafeedTimingStatsReporter.differSignificantly( - createDatafeedTimingStats(JOB_ID, 5, 10, 10000.0), createDatafeedTimingStats(JOB_ID, 5, 10, 11000.0)), - is(false)); + createDatafeedTimingStats(JOB_ID, 5, 10, 10000.0), + createDatafeedTimingStats(JOB_ID, 5, 10, 11000.0) + ), + is(false) + ); assertThat( DatafeedTimingStatsReporter.differSignificantly( - createDatafeedTimingStats(JOB_ID, 5, 10, 10000.0), createDatafeedTimingStats(JOB_ID, 5, 10, 11200.0)), - is(true)); + createDatafeedTimingStats(JOB_ID, 5, 10, 10000.0), + createDatafeedTimingStats(JOB_ID, 5, 10, 11200.0) + ), + is(true) + ); assertThat( DatafeedTimingStatsReporter.differSignificantly( - createDatafeedTimingStats(JOB_ID, 5, 10, 100000.0), createDatafeedTimingStats(JOB_ID, 5, 10, 110000.0)), - is(false)); + createDatafeedTimingStats(JOB_ID, 5, 10, 100000.0), + createDatafeedTimingStats(JOB_ID, 5, 10, 110000.0) + ), + is(false) + ); assertThat( DatafeedTimingStatsReporter.differSignificantly( - createDatafeedTimingStats(JOB_ID, 5, 10, 100000.0), createDatafeedTimingStats(JOB_ID, 5, 10, 110001.0)), - is(true)); + createDatafeedTimingStats(JOB_ID, 5, 10, 100000.0), + createDatafeedTimingStats(JOB_ID, 5, 10, 110001.0) + ), + is(true) + ); assertThat( DatafeedTimingStatsReporter.differSignificantly( - createDatafeedTimingStats(JOB_ID, 5, 10, 100000.0), createDatafeedTimingStats(JOB_ID, 50, 10, 100000.0)), - is(true)); + createDatafeedTimingStats(JOB_ID, 5, 10, 100000.0), + createDatafeedTimingStats(JOB_ID, 50, 10, 100000.0) + ), + is(true) + ); } public void testFinishReportingTimingStatsException() { @@ -197,19 +222,21 @@ private DatafeedTimingStatsReporter createReporter(DatafeedTimingStats timingSta } private static DatafeedTimingStats createDatafeedTimingStats( - String jobId, - long searchCount, - long bucketCount, - double totalSearchTimeMs) { + String jobId, + long searchCount, + long bucketCount, + double totalSearchTimeMs + ) { return createDatafeedTimingStats(jobId, searchCount, bucketCount, totalSearchTimeMs, 0.0); } private static DatafeedTimingStats createDatafeedTimingStats( - String jobId, - long searchCount, - long bucketCount, - double totalSearchTimeMs, - double incrementalSearchTimeMs) { + String jobId, + long searchCount, + long bucketCount, + double totalSearchTimeMs, + double incrementalSearchTimeMs + ) { ExponentialAverageCalculationContext context = new ExponentialAverageCalculationContext(incrementalSearchTimeMs, null, null); return new DatafeedTimingStats(jobId, searchCount, bucketCount, totalSearchTimeMs, context); } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/ProblemTrackerTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/ProblemTrackerTests.java index bc6e3003bd81a..37abb900138f4 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/ProblemTrackerTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/ProblemTrackerTests.java @@ -39,8 +39,11 @@ public void testReportExtractionProblem() { } public void testReportExtractionProblem_GivenSearchPhaseExecutionException() { - SearchPhaseExecutionException searchPhaseExecutionException = new SearchPhaseExecutionException("test-phase", - "partial shards failure", new ShardSearchFailure[] { new ShardSearchFailure(new ElasticsearchException("for the cause!")) }); + SearchPhaseExecutionException searchPhaseExecutionException = new SearchPhaseExecutionException( + "test-phase", + "partial shards failure", + new ShardSearchFailure[] { new ShardSearchFailure(new ElasticsearchException("for the cause!")) } + ); problemTracker.reportExtractionProblem(new DatafeedJob.ExtractionProblemException(0L, searchPhaseExecutionException)); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/delayeddatacheck/DelayedDataDetectorFactoryTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/delayeddatacheck/DelayedDataDetectorFactoryTests.java index 41324a4f4748a..f7699586e7538 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/delayeddatacheck/DelayedDataDetectorFactoryTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/delayeddatacheck/DelayedDataDetectorFactoryTests.java @@ -9,9 +9,9 @@ import org.elasticsearch.client.Client; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.search.SearchModule; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfig; import org.elasticsearch.xpack.core.ml.datafeed.DelayedDataCheckConfig; import org.elasticsearch.xpack.core.ml.job.config.AnalysisConfig; @@ -27,7 +27,6 @@ import static org.hamcrest.Matchers.instanceOf; import static org.mockito.Mockito.mock; - public class DelayedDataDetectorFactoryTests extends ESTestCase { @Override @@ -42,39 +41,49 @@ public void testBuilder() { DatafeedConfig datafeedConfig = createDatafeed(false, null); // Should not throw - assertThat(DelayedDataDetectorFactory.buildDetector(job, datafeedConfig, mock(Client.class), xContentRegistry()), - instanceOf(NullDelayedDataDetector.class)); + assertThat( + DelayedDataDetectorFactory.buildDetector(job, datafeedConfig, mock(Client.class), xContentRegistry()), + instanceOf(NullDelayedDataDetector.class) + ); datafeedConfig = createDatafeed(true, TimeValue.timeValueMinutes(10)); // Should not throw - assertThat(DelayedDataDetectorFactory.buildDetector(job, datafeedConfig, mock(Client.class), xContentRegistry()), - instanceOf(DatafeedDelayedDataDetector.class)); + assertThat( + DelayedDataDetectorFactory.buildDetector(job, datafeedConfig, mock(Client.class), xContentRegistry()), + instanceOf(DatafeedDelayedDataDetector.class) + ); DatafeedConfig tooSmallDatafeedConfig = createDatafeed(true, TimeValue.timeValueSeconds(1)); - IllegalArgumentException e = ESTestCase.expectThrows(IllegalArgumentException.class, - () -> DelayedDataDetectorFactory.buildDetector(job, tooSmallDatafeedConfig, mock(Client.class), xContentRegistry())); + IllegalArgumentException e = ESTestCase.expectThrows( + IllegalArgumentException.class, + () -> DelayedDataDetectorFactory.buildDetector(job, tooSmallDatafeedConfig, mock(Client.class), xContentRegistry()) + ); assertEquals(Messages.getMessage(Messages.DATAFEED_CONFIG_DELAYED_DATA_CHECK_TOO_SMALL, "1s", "2s"), e.getMessage()); DatafeedConfig tooBigDatafeedConfig = createDatafeed(true, TimeValue.timeValueHours(12)); - e = ESTestCase.expectThrows(IllegalArgumentException.class, - () -> DelayedDataDetectorFactory.buildDetector(job, tooBigDatafeedConfig, mock(Client.class), xContentRegistry())); - assertEquals(Messages.getMessage( - Messages.DATAFEED_CONFIG_DELAYED_DATA_CHECK_SPANS_TOO_MANY_BUCKETS, "12h", "2s"), e.getMessage()); + e = ESTestCase.expectThrows( + IllegalArgumentException.class, + () -> DelayedDataDetectorFactory.buildDetector(job, tooBigDatafeedConfig, mock(Client.class), xContentRegistry()) + ); + assertEquals(Messages.getMessage(Messages.DATAFEED_CONFIG_DELAYED_DATA_CHECK_SPANS_TOO_MANY_BUCKETS, "12h", "2s"), e.getMessage()); Job withBigBucketSpan = createJob(TimeValue.timeValueHours(1)); datafeedConfig = createDatafeed(true, null); // Should not throw - DelayedDataDetector delayedDataDetector = - DelayedDataDetectorFactory.buildDetector(withBigBucketSpan, datafeedConfig, mock(Client.class), xContentRegistry()); + DelayedDataDetector delayedDataDetector = DelayedDataDetectorFactory.buildDetector( + withBigBucketSpan, + datafeedConfig, + mock(Client.class), + xContentRegistry() + ); assertThat(delayedDataDetector.getWindow(), equalTo(TimeValue.timeValueHours(1).millis() * 8)); datafeedConfig = createDatafeed(true, null); // Should not throw - delayedDataDetector = - DelayedDataDetectorFactory.buildDetector(job, datafeedConfig, mock(Client.class), xContentRegistry()); + delayedDataDetector = DelayedDataDetectorFactory.buildDetector(job, datafeedConfig, mock(Client.class), xContentRegistry()); assertThat(delayedDataDetector.getWindow(), equalTo(TimeValue.timeValueHours(2).millis())); } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/DataExtractorFactoryTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/DataExtractorFactoryTests.java index e09c287c552c2..25abb69f70a37 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/DataExtractorFactoryTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/DataExtractorFactoryTests.java @@ -13,7 +13,6 @@ import org.elasticsearch.client.Client; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.search.SearchModule; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.AggregatorFactories; @@ -22,6 +21,7 @@ import org.elasticsearch.search.aggregations.metrics.MaxAggregationBuilder; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.ml.datafeed.ChunkingConfig; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfig; import org.elasticsearch.xpack.core.ml.job.config.DataDescription; @@ -37,8 +37,8 @@ import org.elasticsearch.xpack.ml.datafeed.DatafeedRunnerTests; import org.elasticsearch.xpack.ml.datafeed.DatafeedTimingStatsReporter; import org.elasticsearch.xpack.ml.datafeed.extractor.aggregation.AggregationDataExtractorFactory; -import org.elasticsearch.xpack.ml.datafeed.extractor.chunked.ChunkedDataExtractorFactory; import org.elasticsearch.xpack.ml.datafeed.extractor.aggregation.RollupDataExtractorFactory; +import org.elasticsearch.xpack.ml.datafeed.extractor.chunked.ChunkedDataExtractorFactory; import org.elasticsearch.xpack.ml.datafeed.extractor.scroll.ScrollDataExtractorFactory; import org.junit.Before; @@ -73,7 +73,7 @@ protected NamedXContentRegistry xContentRegistry() { } @Before - @SuppressWarnings({"rawtypes", "unchecked"}) + @SuppressWarnings({ "rawtypes", "unchecked" }) public void setUpTests() { client = mock(Client.class); timingStatsReporter = mock(DatafeedTimingStatsReporter.class); @@ -108,12 +108,18 @@ public void testCreateDataExtractorFactoryGivenDefaultScroll() { DatafeedConfig datafeedConfig = DatafeedRunnerTests.createDatafeedConfig("datafeed1", "foo").build(); ActionListener listener = ActionListener.wrap( - dataExtractorFactory -> assertThat(dataExtractorFactory, instanceOf(ChunkedDataExtractorFactory.class)), - e -> fail() + dataExtractorFactory -> assertThat(dataExtractorFactory, instanceOf(ChunkedDataExtractorFactory.class)), + e -> fail() ); DataExtractorFactory.create( - client, datafeedConfig, jobBuilder.build(new Date()), xContentRegistry(), timingStatsReporter, listener); + client, + datafeedConfig, + jobBuilder.build(new Date()), + xContentRegistry(), + timingStatsReporter, + listener + ); } public void testCreateDataExtractorFactoryGivenScrollWithAutoChunk() { @@ -125,12 +131,18 @@ public void testCreateDataExtractorFactoryGivenScrollWithAutoChunk() { datafeedConfig.setChunkingConfig(ChunkingConfig.newAuto()); ActionListener listener = ActionListener.wrap( - dataExtractorFactory -> assertThat(dataExtractorFactory, instanceOf(ChunkedDataExtractorFactory.class)), - e -> fail() + dataExtractorFactory -> assertThat(dataExtractorFactory, instanceOf(ChunkedDataExtractorFactory.class)), + e -> fail() ); DataExtractorFactory.create( - client, datafeedConfig.build(), jobBuilder.build(new Date()), xContentRegistry(), timingStatsReporter, listener); + client, + datafeedConfig.build(), + jobBuilder.build(new Date()), + xContentRegistry(), + timingStatsReporter, + listener + ); } public void testCreateDataExtractorFactoryGivenScrollWithOffChunk() { @@ -142,12 +154,18 @@ public void testCreateDataExtractorFactoryGivenScrollWithOffChunk() { datafeedConfig.setChunkingConfig(ChunkingConfig.newOff()); ActionListener listener = ActionListener.wrap( - dataExtractorFactory -> assertThat(dataExtractorFactory, instanceOf(ScrollDataExtractorFactory.class)), - e -> fail() + dataExtractorFactory -> assertThat(dataExtractorFactory, instanceOf(ScrollDataExtractorFactory.class)), + e -> fail() ); DataExtractorFactory.create( - client, datafeedConfig.build(), jobBuilder.build(new Date()), xContentRegistry(), timingStatsReporter, listener); + client, + datafeedConfig.build(), + jobBuilder.build(new Date()), + xContentRegistry(), + timingStatsReporter, + listener + ); } public void testCreateDataExtractorFactoryGivenDefaultAggregation() { @@ -157,16 +175,24 @@ public void testCreateDataExtractorFactoryGivenDefaultAggregation() { jobBuilder.setDataDescription(dataDescription); DatafeedConfig.Builder datafeedConfig = DatafeedRunnerTests.createDatafeedConfig("datafeed1", "foo"); MaxAggregationBuilder maxTime = AggregationBuilders.max("time").field("time"); - datafeedConfig.setParsedAggregations(AggregatorFactories.builder().addAggregator( - AggregationBuilders.histogram("time").interval(300000).subAggregation(maxTime).field("time"))); + datafeedConfig.setParsedAggregations( + AggregatorFactories.builder() + .addAggregator(AggregationBuilders.histogram("time").interval(300000).subAggregation(maxTime).field("time")) + ); ActionListener listener = ActionListener.wrap( - dataExtractorFactory -> assertThat(dataExtractorFactory, instanceOf(ChunkedDataExtractorFactory.class)), - e -> fail() + dataExtractorFactory -> assertThat(dataExtractorFactory, instanceOf(ChunkedDataExtractorFactory.class)), + e -> fail() ); DataExtractorFactory.create( - client, datafeedConfig.build(), jobBuilder.build(new Date()), xContentRegistry(), timingStatsReporter, listener); + client, + datafeedConfig.build(), + jobBuilder.build(new Date()), + xContentRegistry(), + timingStatsReporter, + listener + ); } public void testCreateDataExtractorFactoryGivenAggregationWithOffChunk() { @@ -177,16 +203,24 @@ public void testCreateDataExtractorFactoryGivenAggregationWithOffChunk() { DatafeedConfig.Builder datafeedConfig = DatafeedRunnerTests.createDatafeedConfig("datafeed1", "foo"); datafeedConfig.setChunkingConfig(ChunkingConfig.newOff()); MaxAggregationBuilder maxTime = AggregationBuilders.max("time").field("time"); - datafeedConfig.setParsedAggregations(AggregatorFactories.builder().addAggregator( - AggregationBuilders.histogram("time").interval(300000).subAggregation(maxTime).field("time"))); + datafeedConfig.setParsedAggregations( + AggregatorFactories.builder() + .addAggregator(AggregationBuilders.histogram("time").interval(300000).subAggregation(maxTime).field("time")) + ); ActionListener listener = ActionListener.wrap( - dataExtractorFactory -> assertThat(dataExtractorFactory, instanceOf(AggregationDataExtractorFactory.class)), - e -> fail() + dataExtractorFactory -> assertThat(dataExtractorFactory, instanceOf(AggregationDataExtractorFactory.class)), + e -> fail() ); DataExtractorFactory.create( - client, datafeedConfig.build(), jobBuilder.build(new Date()), xContentRegistry(), timingStatsReporter, listener); + client, + datafeedConfig.build(), + jobBuilder.build(new Date()), + xContentRegistry(), + timingStatsReporter, + listener + ); } public void testCreateDataExtractorFactoryGivenDefaultAggregationWithAutoChunk() { @@ -196,17 +230,25 @@ public void testCreateDataExtractorFactoryGivenDefaultAggregationWithAutoChunk() jobBuilder.setDataDescription(dataDescription); DatafeedConfig.Builder datafeedConfig = DatafeedRunnerTests.createDatafeedConfig("datafeed1", "foo"); MaxAggregationBuilder maxTime = AggregationBuilders.max("time").field("time"); - datafeedConfig.setParsedAggregations(AggregatorFactories.builder().addAggregator( - AggregationBuilders.histogram("time").interval(300000).subAggregation(maxTime).field("time"))); + datafeedConfig.setParsedAggregations( + AggregatorFactories.builder() + .addAggregator(AggregationBuilders.histogram("time").interval(300000).subAggregation(maxTime).field("time")) + ); datafeedConfig.setChunkingConfig(ChunkingConfig.newAuto()); ActionListener listener = ActionListener.wrap( - dataExtractorFactory -> assertThat(dataExtractorFactory, instanceOf(ChunkedDataExtractorFactory.class)), - e -> fail() + dataExtractorFactory -> assertThat(dataExtractorFactory, instanceOf(ChunkedDataExtractorFactory.class)), + e -> fail() ); DataExtractorFactory.create( - client, datafeedConfig.build(), jobBuilder.build(new Date()), xContentRegistry(), timingStatsReporter, listener); + client, + datafeedConfig.build(), + jobBuilder.build(new Date()), + xContentRegistry(), + timingStatsReporter, + listener + ); } public void testCreateDataExtractorFactoryGivenRollupAndValidAggregationAndRuntimeFields() { @@ -225,28 +267,33 @@ public void testCreateDataExtractorFactoryGivenRollupAndValidAggregationAndRunti settings.put("script", ""); Map field = new HashMap<>(); field.put("runtime_field_bar", settings); - datafeedConfig.setParsedAggregations(AggregatorFactories.builder().addAggregator( - AggregationBuilders.dateHistogram("time") - .fixedInterval(new DateHistogramInterval("600000ms")) - .subAggregation(maxTime) - .subAggregation(myTerm) - .field("time"))) - .setRuntimeMappings(field); - ActionListener listener = ActionListener.wrap( - dataExtractorFactory -> fail(), - e -> { - assertThat( - e.getMessage(), - equalTo("The datafeed has runtime_mappings defined, runtime fields are not supported in rollup searches") - ); - assertThat(e, instanceOf(IllegalArgumentException.class)); - } - ); + datafeedConfig.setParsedAggregations( + AggregatorFactories.builder() + .addAggregator( + AggregationBuilders.dateHistogram("time") + .fixedInterval(new DateHistogramInterval("600000ms")) + .subAggregation(maxTime) + .subAggregation(myTerm) + .field("time") + ) + ).setRuntimeMappings(field); + ActionListener listener = ActionListener.wrap(dataExtractorFactory -> fail(), e -> { + assertThat( + e.getMessage(), + equalTo("The datafeed has runtime_mappings defined, runtime fields are not supported in rollup searches") + ); + assertThat(e, instanceOf(IllegalArgumentException.class)); + }); DataExtractorFactory.create( - client, datafeedConfig.build(), jobBuilder.build(new Date()), xContentRegistry(), timingStatsReporter, listener); + client, + datafeedConfig.build(), + jobBuilder.build(new Date()), + xContentRegistry(), + timingStatsReporter, + listener + ); } - public void testCreateDataExtractorFactoryGivenRollupAndValidAggregation() { givenAggregatableRollup("myField", "max", 5, "termField"); DataDescription.Builder dataDescription = new DataDescription.Builder(); @@ -258,20 +305,28 @@ public void testCreateDataExtractorFactoryGivenRollupAndValidAggregation() { MaxAggregationBuilder maxTime = AggregationBuilders.max("time").field("time"); MaxAggregationBuilder myField = AggregationBuilders.max("myField").field("myField"); TermsAggregationBuilder myTerm = AggregationBuilders.terms("termAgg").field("termField").subAggregation(myField); - datafeedConfig.setParsedAggregations(AggregatorFactories.builder().addAggregator( - AggregationBuilders.dateHistogram("time") - .fixedInterval(new DateHistogramInterval("600000ms")) - .subAggregation(maxTime) - .subAggregation(myTerm) - .field("time"))); + datafeedConfig.setParsedAggregations( + AggregatorFactories.builder() + .addAggregator( + AggregationBuilders.dateHistogram("time") + .fixedInterval(new DateHistogramInterval("600000ms")) + .subAggregation(maxTime) + .subAggregation(myTerm) + .field("time") + ) + ); ActionListener listener = ActionListener.wrap( - dataExtractorFactory -> { - assertThat(dataExtractorFactory, instanceOf(RollupDataExtractorFactory.class)); - }, + dataExtractorFactory -> { assertThat(dataExtractorFactory, instanceOf(RollupDataExtractorFactory.class)); }, e -> fail() ); DataExtractorFactory.create( - client, datafeedConfig.build(), jobBuilder.build(new Date()), xContentRegistry(), timingStatsReporter, listener); + client, + datafeedConfig.build(), + jobBuilder.build(new Date()), + xContentRegistry(), + timingStatsReporter, + listener + ); } public void testCreateDataExtractorFactoryGivenRollupAndRemoteIndex() { @@ -286,22 +341,30 @@ public void testCreateDataExtractorFactoryGivenRollupAndRemoteIndex() { MaxAggregationBuilder maxTime = AggregationBuilders.max("time").field("time"); MaxAggregationBuilder myField = AggregationBuilders.max("myField").field("myField"); TermsAggregationBuilder myTerm = AggregationBuilders.terms("termAgg").field("termField").subAggregation(myField); - datafeedConfig.setParsedAggregations(AggregatorFactories.builder().addAggregator( - AggregationBuilders.dateHistogram("time") - .fixedInterval(new DateHistogramInterval("600000ms")) - .subAggregation(maxTime) - .subAggregation(myTerm) - .field("time"))); + datafeedConfig.setParsedAggregations( + AggregatorFactories.builder() + .addAggregator( + AggregationBuilders.dateHistogram("time") + .fixedInterval(new DateHistogramInterval("600000ms")) + .subAggregation(maxTime) + .subAggregation(myTerm) + .field("time") + ) + ); // Test with remote index, aggregation, and no chunking ActionListener listener = ActionListener.wrap( - dataExtractorFactory -> { - assertThat(dataExtractorFactory, instanceOf(AggregationDataExtractorFactory.class)); - }, + dataExtractorFactory -> { assertThat(dataExtractorFactory, instanceOf(AggregationDataExtractorFactory.class)); }, e -> fail() ); DataExtractorFactory.create( - client, datafeedConfig.build(), jobBuilder.build(new Date()), xContentRegistry(), timingStatsReporter, listener); + client, + datafeedConfig.build(), + jobBuilder.build(new Date()), + xContentRegistry(), + timingStatsReporter, + listener + ); // Test with remote index, aggregation, and chunking datafeedConfig.setChunkingConfig(ChunkingConfig.newAuto()); @@ -310,7 +373,13 @@ public void testCreateDataExtractorFactoryGivenRollupAndRemoteIndex() { e -> fail() ); DataExtractorFactory.create( - client, datafeedConfig.build(), jobBuilder.build(new Date()), xContentRegistry(), timingStatsReporter, listener); + client, + datafeedConfig.build(), + jobBuilder.build(new Date()), + xContentRegistry(), + timingStatsReporter, + listener + ); // Test with remote index, no aggregation, and no chunking datafeedConfig = DatafeedRunnerTests.createDatafeedConfig("datafeed1", "foo"); @@ -323,7 +392,13 @@ public void testCreateDataExtractorFactoryGivenRollupAndRemoteIndex() { ); DataExtractorFactory.create( - client, datafeedConfig.build(), jobBuilder.build(new Date()), xContentRegistry(), timingStatsReporter, listener); + client, + datafeedConfig.build(), + jobBuilder.build(new Date()), + xContentRegistry(), + timingStatsReporter, + listener + ); // Test with remote index, no aggregation, and chunking datafeedConfig.setChunkingConfig(ChunkingConfig.newAuto()); @@ -332,7 +407,13 @@ public void testCreateDataExtractorFactoryGivenRollupAndRemoteIndex() { e -> fail() ); DataExtractorFactory.create( - client, datafeedConfig.build(), jobBuilder.build(new Date()), xContentRegistry(), timingStatsReporter, listener); + client, + datafeedConfig.build(), + jobBuilder.build(new Date()), + xContentRegistry(), + timingStatsReporter, + listener + ); } public void testCreateDataExtractorFactoryGivenRollupAndValidAggregationAndAutoChunk() { @@ -346,20 +427,28 @@ public void testCreateDataExtractorFactoryGivenRollupAndValidAggregationAndAutoC MaxAggregationBuilder maxTime = AggregationBuilders.max("time").field("time"); MaxAggregationBuilder myField = AggregationBuilders.max("myField").field("myField"); TermsAggregationBuilder myTerm = AggregationBuilders.terms("termAgg").field("termField").subAggregation(myField); - datafeedConfig.setParsedAggregations(AggregatorFactories.builder().addAggregator( - AggregationBuilders.dateHistogram("time") - .fixedInterval(new DateHistogramInterval("600000ms")) - .subAggregation(maxTime) - .subAggregation(myTerm) - .field("time"))); + datafeedConfig.setParsedAggregations( + AggregatorFactories.builder() + .addAggregator( + AggregationBuilders.dateHistogram("time") + .fixedInterval(new DateHistogramInterval("600000ms")) + .subAggregation(maxTime) + .subAggregation(myTerm) + .field("time") + ) + ); ActionListener listener = ActionListener.wrap( - dataExtractorFactory -> { - assertThat(dataExtractorFactory, instanceOf(ChunkedDataExtractorFactory.class)); - }, + dataExtractorFactory -> { assertThat(dataExtractorFactory, instanceOf(ChunkedDataExtractorFactory.class)); }, e -> fail() ); DataExtractorFactory.create( - client, datafeedConfig.build(), jobBuilder.build(new Date()), xContentRegistry(), timingStatsReporter, listener); + client, + datafeedConfig.build(), + jobBuilder.build(new Date()), + xContentRegistry(), + timingStatsReporter, + listener + ); } public void testCreateDataExtractorFactoryGivenRollupButNoAggregations() { @@ -371,16 +460,19 @@ public void testCreateDataExtractorFactoryGivenRollupButNoAggregations() { DatafeedConfig.Builder datafeedConfig = DatafeedRunnerTests.createDatafeedConfig("datafeed1", "foo"); datafeedConfig.setChunkingConfig(ChunkingConfig.newOff()); - ActionListener listener = ActionListener.wrap( - dataExtractorFactory -> fail(), - e -> { - assertThat(e.getMessage(), equalTo("Aggregations are required when using Rollup indices")); - assertThat(e, instanceOf(IllegalArgumentException.class)); - } - ); + ActionListener listener = ActionListener.wrap(dataExtractorFactory -> fail(), e -> { + assertThat(e.getMessage(), equalTo("Aggregations are required when using Rollup indices")); + assertThat(e, instanceOf(IllegalArgumentException.class)); + }); DataExtractorFactory.create( - client, datafeedConfig.build(), jobBuilder.build(new Date()), xContentRegistry(), timingStatsReporter, listener); + client, + datafeedConfig.build(), + jobBuilder.build(new Date()), + xContentRegistry(), + timingStatsReporter, + listener + ); } public void testCreateDataExtractorFactoryGivenRollupWithBadInterval() { @@ -394,23 +486,34 @@ public void testCreateDataExtractorFactoryGivenRollupWithBadInterval() { MaxAggregationBuilder maxTime = AggregationBuilders.max("time").field("time"); MaxAggregationBuilder myField = AggregationBuilders.max("myField").field("myField"); TermsAggregationBuilder myTerm = AggregationBuilders.terms("termAgg").field("termField").subAggregation(myField); - datafeedConfig.setParsedAggregations(AggregatorFactories.builder().addAggregator( - AggregationBuilders.dateHistogram("time") - .fixedInterval(new DateHistogramInterval("600000ms")) - .subAggregation(maxTime) - .subAggregation(myTerm) - .field("time"))); - ActionListener listener = ActionListener.wrap( - dataExtractorFactory -> fail(), - e -> { - assertThat(e.getMessage(), - containsString("Rollup capabilities do not have a [date_histogram] aggregation with an interval " + - "that is a multiple of the datafeed's interval.")); - assertThat(e, instanceOf(IllegalArgumentException.class)); - } + datafeedConfig.setParsedAggregations( + AggregatorFactories.builder() + .addAggregator( + AggregationBuilders.dateHistogram("time") + .fixedInterval(new DateHistogramInterval("600000ms")) + .subAggregation(maxTime) + .subAggregation(myTerm) + .field("time") + ) ); + ActionListener listener = ActionListener.wrap(dataExtractorFactory -> fail(), e -> { + assertThat( + e.getMessage(), + containsString( + "Rollup capabilities do not have a [date_histogram] aggregation with an interval " + + "that is a multiple of the datafeed's interval." + ) + ); + assertThat(e, instanceOf(IllegalArgumentException.class)); + }); DataExtractorFactory.create( - client, datafeedConfig.build(), jobBuilder.build(new Date()), xContentRegistry(), timingStatsReporter, listener); + client, + datafeedConfig.build(), + jobBuilder.build(new Date()), + xContentRegistry(), + timingStatsReporter, + listener + ); } public void testCreateDataExtractorFactoryGivenRollupMissingTerms() { @@ -424,22 +527,31 @@ public void testCreateDataExtractorFactoryGivenRollupMissingTerms() { MaxAggregationBuilder maxTime = AggregationBuilders.max("time").field("time"); MaxAggregationBuilder myField = AggregationBuilders.max("myField").field("myField"); TermsAggregationBuilder myTerm = AggregationBuilders.terms("termAgg").field("termField").subAggregation(myField); - datafeedConfig.setParsedAggregations(AggregatorFactories.builder().addAggregator( - AggregationBuilders.dateHistogram("time") - .fixedInterval(new DateHistogramInterval("600000ms")) - .subAggregation(maxTime) - .subAggregation(myTerm) - .field("time"))); - ActionListener listener = ActionListener.wrap( - dataExtractorFactory -> fail(), - e -> { - assertThat(e.getMessage(), - containsString("Rollup capabilities do not support all the datafeed aggregations at the desired interval.")); - assertThat(e, instanceOf(IllegalArgumentException.class)); - } + datafeedConfig.setParsedAggregations( + AggregatorFactories.builder() + .addAggregator( + AggregationBuilders.dateHistogram("time") + .fixedInterval(new DateHistogramInterval("600000ms")) + .subAggregation(maxTime) + .subAggregation(myTerm) + .field("time") + ) ); + ActionListener listener = ActionListener.wrap(dataExtractorFactory -> fail(), e -> { + assertThat( + e.getMessage(), + containsString("Rollup capabilities do not support all the datafeed aggregations at the desired interval.") + ); + assertThat(e, instanceOf(IllegalArgumentException.class)); + }); DataExtractorFactory.create( - client, datafeedConfig.build(), jobBuilder.build(new Date()), xContentRegistry(), timingStatsReporter, listener); + client, + datafeedConfig.build(), + jobBuilder.build(new Date()), + xContentRegistry(), + timingStatsReporter, + listener + ); } public void testCreateDataExtractorFactoryGivenRollupMissingMetric() { @@ -453,32 +565,44 @@ public void testCreateDataExtractorFactoryGivenRollupMissingMetric() { MaxAggregationBuilder maxTime = AggregationBuilders.max("time").field("time"); MaxAggregationBuilder myField = AggregationBuilders.max("myField").field("otherField"); TermsAggregationBuilder myTerm = AggregationBuilders.terms("termAgg").field("termField").subAggregation(myField); - datafeedConfig.setParsedAggregations(AggregatorFactories.builder().addAggregator( - AggregationBuilders.dateHistogram("time") - .fixedInterval(new DateHistogramInterval("600000ms")) - .subAggregation(maxTime) - .subAggregation(myTerm) - .field("time"))); - ActionListener listener = ActionListener.wrap( - dataExtractorFactory -> fail(), - e -> { - assertThat(e.getMessage(), - containsString("Rollup capabilities do not support all the datafeed aggregations at the desired interval.")); - assertThat(e, instanceOf(IllegalArgumentException.class)); - } + datafeedConfig.setParsedAggregations( + AggregatorFactories.builder() + .addAggregator( + AggregationBuilders.dateHistogram("time") + .fixedInterval(new DateHistogramInterval("600000ms")) + .subAggregation(maxTime) + .subAggregation(myTerm) + .field("time") + ) ); + ActionListener listener = ActionListener.wrap(dataExtractorFactory -> fail(), e -> { + assertThat( + e.getMessage(), + containsString("Rollup capabilities do not support all the datafeed aggregations at the desired interval.") + ); + assertThat(e, instanceOf(IllegalArgumentException.class)); + }); DataExtractorFactory.create( - client, datafeedConfig.build(), jobBuilder.build(new Date()), xContentRegistry(), timingStatsReporter, listener); + client, + datafeedConfig.build(), + jobBuilder.build(new Date()), + xContentRegistry(), + timingStatsReporter, + listener + ); } private void givenAggregatableRollup(String field, String type, int minuteInterval, String... groupByTerms) { - List metricConfigs = Arrays.asList(new MetricConfig(field, Collections.singletonList(type)), - new MetricConfig("time", Arrays.asList("min", "max"))); + List metricConfigs = Arrays.asList( + new MetricConfig(field, Collections.singletonList(type)), + new MetricConfig("time", Arrays.asList("min", "max")) + ); TermsGroupConfig termsGroupConfig = null; if (groupByTerms.length > 0) { termsGroupConfig = new TermsGroupConfig(groupByTerms); } - RollupJobConfig rollupJobConfig = new RollupJobConfig("rollupJob1", + RollupJobConfig rollupJobConfig = new RollupJobConfig( + "rollupJob1", "myIndexes*", "myIndex_rollup", "*/30 * * * * ?", @@ -486,9 +610,11 @@ private void givenAggregatableRollup(String field, String type, int minuteInterv new GroupConfig( new DateHistogramGroupConfig.FixedInterval("time", DateHistogramInterval.minutes(minuteInterval)), null, - termsGroupConfig), + termsGroupConfig + ), metricConfigs, - null); + null + ); RollupJobCaps rollupJobCaps = new RollupJobCaps(rollupJobConfig); RollableIndexCaps rollableIndexCaps = new RollableIndexCaps("myIndex_rollup", Collections.singletonList(rollupJobCaps)); Map jobs = new HashMap<>(1); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AggregationDataExtractorFactoryTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AggregationDataExtractorFactoryTests.java index 9044551b085cc..2a667043902c1 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AggregationDataExtractorFactoryTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AggregationDataExtractorFactoryTests.java @@ -8,11 +8,11 @@ import org.elasticsearch.client.Client; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.search.SearchModule; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.AggregatorFactories; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfig; import org.elasticsearch.xpack.core.ml.job.config.AnalysisConfig; import org.elasticsearch.xpack.core.ml.job.config.DataDescription; @@ -65,8 +65,11 @@ public void testNewExtractor_GivenNonAlignedTimes() { private AggregationDataExtractorFactory createFactory(long histogramInterval) { AggregatorFactories.Builder aggs = new AggregatorFactories.Builder().addAggregator( - AggregationBuilders.histogram("time").field("time").interval(histogramInterval).subAggregation( - AggregationBuilders.max("time").field("time"))); + AggregationBuilders.histogram("time") + .field("time") + .interval(histogramInterval) + .subAggregation(AggregationBuilders.max("time").field("time")) + ); DataDescription.Builder dataDescription = new DataDescription.Builder(); dataDescription.setTimeField("time"); Detector.Builder detectorBuilder = new Detector.Builder(); @@ -81,6 +84,11 @@ private AggregationDataExtractorFactory createFactory(long histogramInterval) { datafeedConfigBuilder.setParsedAggregations(aggs); datafeedConfigBuilder.setIndices(Arrays.asList("my_index")); return new AggregationDataExtractorFactory( - client, datafeedConfigBuilder.build(), jobBuilder.build(new Date()), xContentRegistry(), timingStatsReporter); + client, + datafeedConfigBuilder.build(), + jobBuilder.build(new Date()), + xContentRegistry(), + timingStatsReporter + ); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AggregationDataExtractorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AggregationDataExtractorTests.java index ffadaef8cbdcf..e1cc13726dfa3 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AggregationDataExtractorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AggregationDataExtractorTests.java @@ -102,23 +102,39 @@ public void setUpTests() { fields.addAll(Arrays.asList("time", "airline", "responsetime")); indices = Arrays.asList("index-1", "index-2"); query = QueryBuilders.matchAllQuery(); - aggs = new AggregatorFactories.Builder() - .addAggregator(AggregationBuilders.histogram("time").field("time").interval(1000).subAggregation( - AggregationBuilders.terms("airline").field("airline").subAggregation( - AggregationBuilders.avg("responsetime").field("responsetime")))); + aggs = new AggregatorFactories.Builder().addAggregator( + AggregationBuilders.histogram("time") + .field("time") + .interval(1000) + .subAggregation( + AggregationBuilders.terms("airline") + .field("airline") + .subAggregation(AggregationBuilders.avg("responsetime").field("responsetime")) + ) + ); runtimeMappings = Collections.emptyMap(); timingStatsReporter = new DatafeedTimingStatsReporter(new DatafeedTimingStats(jobId), mock(DatafeedTimingStatsPersister.class)); } public void testExtraction() throws IOException { List histogramBuckets = Arrays.asList( - createHistogramBucket(1000L, 3, Arrays.asList( + createHistogramBucket( + 1000L, + 3, + Arrays.asList( createMax("time", 1999), - createTerms("airline", new Term("a", 1, "responsetime", 11.0), new Term("b", 2, "responsetime", 12.0)))), + createTerms("airline", new Term("a", 1, "responsetime", 11.0), new Term("b", 2, "responsetime", 12.0)) + ) + ), createHistogramBucket(2000L, 0, Collections.emptyList()), - createHistogramBucket(3000L, 7, Arrays.asList( + createHistogramBucket( + 3000L, + 7, + Arrays.asList( createMax("time", 3999), - createTerms("airline", new Term("c", 4, "responsetime", 31.0), new Term("b", 3, "responsetime", 32.0)))) + createTerms("airline", new Term("c", 4, "responsetime", 31.0), new Term("b", 3, "responsetime", 32.0)) + ) + ) ); TestDataExtractor extractor = new TestDataExtractor(1000L, 4000L); @@ -130,20 +146,27 @@ public void testExtraction() throws IOException { Optional stream = extractor.next(); assertThat(stream.isPresent(), is(true)); String expectedStream = "{\"time\":1999,\"airline\":\"a\",\"responsetime\":11.0,\"doc_count\":1} " - + "{\"time\":1999,\"airline\":\"b\",\"responsetime\":12.0,\"doc_count\":2} " - + "{\"time\":3999,\"airline\":\"c\",\"responsetime\":31.0,\"doc_count\":4} " - + "{\"time\":3999,\"airline\":\"b\",\"responsetime\":32.0,\"doc_count\":3}"; + + "{\"time\":1999,\"airline\":\"b\",\"responsetime\":12.0,\"doc_count\":2} " + + "{\"time\":3999,\"airline\":\"c\",\"responsetime\":31.0,\"doc_count\":4} " + + "{\"time\":3999,\"airline\":\"b\",\"responsetime\":32.0,\"doc_count\":3}"; assertThat(asString(stream.get()), equalTo(expectedStream)); assertThat(extractor.hasNext(), is(false)); assertThat(capturedSearchRequests.size(), equalTo(1)); String searchRequest = capturedSearchRequests.get(0).toString().replaceAll("\\s", ""); assertThat(searchRequest, containsString("\"size\":0")); - assertThat(searchRequest, containsString("\"query\":{\"bool\":{\"filter\":[{\"match_all\":{\"boost\":1.0}}," + - "{\"range\":{\"time\":{\"from\":0,\"to\":4000,\"include_lower\":true,\"include_upper\":false," + - "\"format\":\"epoch_millis\",\"boost\":1.0}}}]")); - assertThat(searchRequest, - stringContainsInOrder(Arrays.asList("aggregations", "histogram", "time", "terms", "airline", "avg", "responsetime"))); + assertThat( + searchRequest, + containsString( + "\"query\":{\"bool\":{\"filter\":[{\"match_all\":{\"boost\":1.0}}," + + "{\"range\":{\"time\":{\"from\":0,\"to\":4000,\"include_lower\":true,\"include_upper\":false," + + "\"format\":\"epoch_millis\",\"boost\":1.0}}}]" + ) + ); + assertThat( + searchRequest, + stringContainsInOrder(Arrays.asList("aggregations", "histogram", "time", "terms", "airline", "avg", "responsetime")) + ); } public void testExtractionGivenResponseHasNullAggs() throws IOException { @@ -203,8 +226,16 @@ public void testExtractionGivenCancelHalfWay() throws IOException { List histogramBuckets = new ArrayList<>(buckets); long timestamp = 1000; for (int i = 0; i < buckets; i++) { - histogramBuckets.add(createHistogramBucket(timestamp, 3, Arrays.asList(createMax("time", timestamp), - createTerms("airline", new Term("c", 4, "responsetime", 31.0), new Term("b", 3, "responsetime", 32.0))))); + histogramBuckets.add( + createHistogramBucket( + timestamp, + 3, + Arrays.asList( + createMax("time", timestamp), + createTerms("airline", new Term("c", 4, "responsetime", 31.0), new Term("b", 3, "responsetime", 32.0)) + ) + ) + ); timestamp += 1000L; } @@ -217,8 +248,16 @@ public void testExtractionGivenCancelHalfWay() throws IOException { assertThat(countMatches('{', asString(extractor.next().get())), equalTo(2400L)); histogramBuckets = new ArrayList<>(buckets); for (int i = 0; i < buckets; i++) { - histogramBuckets.add(createHistogramBucket(timestamp, 3, Arrays.asList(createMax("time", timestamp), - createTerms("airline", new Term("c", 4, "responsetime", 31.0), new Term("b", 3, "responsetime", 32.0))))); + histogramBuckets.add( + createHistogramBucket( + timestamp, + 3, + Arrays.asList( + createMax("time", timestamp), + createTerms("airline", new Term("c", 4, "responsetime", 31.0), new Term("b", 3, "responsetime", 32.0)) + ) + ) + ); timestamp += 1000L; } response = createSearchResponse("time", histogramBuckets); @@ -239,15 +278,27 @@ public void testExtractionGivenSearchResponseHasError() { } private AggregationDataExtractorContext createContext(long start, long end) { - return new AggregationDataExtractorContext(jobId, timeField, fields, indices, query, aggs, start, end, true, - Collections.emptyMap(), SearchRequest.DEFAULT_INDICES_OPTIONS, runtimeMappings); + return new AggregationDataExtractorContext( + jobId, + timeField, + fields, + indices, + query, + aggs, + start, + end, + true, + Collections.emptyMap(), + SearchRequest.DEFAULT_INDICES_OPTIONS, + runtimeMappings + ); } @SuppressWarnings("unchecked") private SearchResponse createSearchResponse(String histogramName, List histogramBuckets) { Histogram histogram = mock(Histogram.class); when(histogram.getName()).thenReturn(histogramName); - when((List)histogram.getBuckets()).thenReturn(histogramBuckets); + when((List) histogram.getBuckets()).thenReturn(histogramBuckets); Aggregations searchAggs = AggregationTestUtils.createAggs(Collections.singletonList(histogram)); return createSearchResponse(searchAggs); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AggregationTestUtils.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AggregationTestUtils.java index bf844d6d14675..61c70d62d7ea6 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AggregationTestUtils.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AggregationTestUtils.java @@ -6,8 +6,8 @@ */ package org.elasticsearch.xpack.ml.datafeed.extractor.aggregation; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.geo.GeoPoint; +import org.elasticsearch.core.Tuple; import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.Aggregations; import org.elasticsearch.search.aggregations.bucket.SingleBucketAggregation; @@ -17,8 +17,8 @@ import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.aggregations.metrics.Avg; import org.elasticsearch.search.aggregations.metrics.GeoCentroid; -import org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregation; import org.elasticsearch.search.aggregations.metrics.Max; +import org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregation; import org.elasticsearch.search.aggregations.metrics.Percentile; import org.elasticsearch.search.aggregations.metrics.Percentiles; @@ -44,11 +44,13 @@ static Histogram.Bucket createHistogramBucket(long timestamp, long docCount, Lis return bucket; } - static CompositeAggregation.Bucket createCompositeBucket(long timestamp, - String dateValueSource, - long docCount, - List subAggregations, - List> termValues) { + static CompositeAggregation.Bucket createCompositeBucket( + long timestamp, + String dateValueSource, + long docCount, + List subAggregations, + List> termValues + ) { CompositeAggregation.Bucket bucket = mock(CompositeAggregation.Bucket.class); when(bucket.getDocCount()).thenReturn(docCount); Aggregations aggs = createAggs(subAggregations); @@ -81,7 +83,7 @@ static Aggregations createAggs(List aggsList) { @SuppressWarnings("unchecked") static Histogram createHistogramAggregation(String name, List histogramBuckets) { Histogram histogram = mock(Histogram.class); - when((List)histogram.getBuckets()).thenReturn(histogramBuckets); + when((List) histogram.getBuckets()).thenReturn(histogramBuckets); when(histogram.getName()).thenReturn(name); return histogram; } @@ -89,7 +91,7 @@ static Histogram createHistogramAggregation(String name, List @SuppressWarnings("unchecked") static CompositeAggregation createCompositeAggregation(String name, List buckets) { CompositeAggregation compositeAggregation = mock(CompositeAggregation.class); - when((List)compositeAggregation.getBuckets()).thenReturn(buckets); + when((List) compositeAggregation.getBuckets()).thenReturn(buckets); when(compositeAggregation.getName()).thenReturn(name); return compositeAggregation; @@ -132,7 +134,7 @@ static Terms createTerms(String name, Term... terms) { Terms termsAgg = mock(Terms.class); when(termsAgg.getName()).thenReturn(name); List buckets = new ArrayList<>(); - for (Term term: terms) { + for (Term term : terms) { StringTerms.Bucket bucket = mock(StringTerms.Bucket.class); when(bucket.getKey()).thenReturn(term.key); when(bucket.getDocCount()).thenReturn(term.count); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AggregationToJsonProcessorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AggregationToJsonProcessorTests.java index 31188a29efc39..37e4d447521a5 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AggregationToJsonProcessorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AggregationToJsonProcessorTests.java @@ -6,8 +6,8 @@ */ package org.elasticsearch.xpack.ml.datafeed.extractor.aggregation; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.util.set.Sets; +import org.elasticsearch.core.Tuple; import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.Aggregations; import org.elasticsearch.search.aggregations.bucket.composite.CompositeAggregation; @@ -69,80 +69,73 @@ public void testProcessGivenMultipleDateHistogramsOrComposite() { nestedBucket = createHistogramAggregation("buckets", nestedHistogramBuckets); } else { List nestedCompositebuckets = Arrays.asList( - createCompositeBucket( - 1000L, - "time", - 3, - Collections.singletonList(createMax("metric1", 1200)), - Collections.emptyList() - ), - createCompositeBucket( - 2000L, - "time", - 5, - Collections.singletonList(createMax("metric1", 2800)), - Collections.emptyList() - ) + createCompositeBucket(1000L, "time", 3, Collections.singletonList(createMax("metric1", 1200)), Collections.emptyList()), + createCompositeBucket(2000L, "time", 5, Collections.singletonList(createMax("metric1", 2800)), Collections.emptyList()) ); nestedBucket = createCompositeAggregation("buckets", nestedCompositebuckets); } List histogramBuckets = Arrays.asList( - createHistogramBucket(1000L, 3, Arrays.asList(createMax("time", 1000L), nestedBucket)) + createHistogramBucket(1000L, 3, Arrays.asList(createMax("time", 1000L), nestedBucket)) ); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> aggToString(Sets.newHashSet("my_field"), histogramBuckets)); - assertThat(e.getMessage(), containsString("More than one composite or date_histogram cannot be used in the aggregation." - + " [buckets] is another instance of a composite or date_histogram aggregation")); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> aggToString(Sets.newHashSet("my_field"), histogramBuckets) + ); + assertThat( + e.getMessage(), + containsString( + "More than one composite or date_histogram cannot be used in the aggregation." + + " [buckets] is another instance of a composite or date_histogram aggregation" + ) + ); } public void testProcessGivenMaxTimeIsMissing() { - List histogramBuckets = Arrays.asList( - createHistogramBucket(1000L, 3), - createHistogramBucket(2000L, 5) - ); + List histogramBuckets = Arrays.asList(createHistogramBucket(1000L, 3), createHistogramBucket(2000L, 5)); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> aggToString(Collections.emptySet(), histogramBuckets)); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> aggToString(Collections.emptySet(), histogramBuckets) + ); assertThat(e.getMessage(), containsString("Missing max aggregation for time_field [time]")); List compositeBuckets = Arrays.asList( - createCompositeBucket(1000L, "time",3, Collections.emptyList(), Collections.emptyList()), - createCompositeBucket(2000L, "time",5, Collections.emptyList(), Collections.emptyList()) + createCompositeBucket(1000L, "time", 3, Collections.emptyList(), Collections.emptyList()), + createCompositeBucket(2000L, "time", 5, Collections.emptyList(), Collections.emptyList()) ); - e = expectThrows(IllegalArgumentException.class, - () -> aggToStringComposite(Collections.emptySet(), compositeBuckets)); + e = expectThrows(IllegalArgumentException.class, () -> aggToStringComposite(Collections.emptySet(), compositeBuckets)); assertThat(e.getMessage(), containsString("Missing max aggregation for time_field [time]")); } public void testProcessGivenNonMaxTimeAgg() { List aggs = Collections.singletonList(createTerms("time", new Term("a", 1), new Term("b", 2))); List histogramBuckets = Arrays.asList( - createHistogramBucket(1000L, 3, aggs), - createHistogramBucket(2000L, 5, aggs) + createHistogramBucket(1000L, 3, aggs), + createHistogramBucket(2000L, 5, aggs) ); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> aggToString(Collections.emptySet(), histogramBuckets)); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> aggToString(Collections.emptySet(), histogramBuckets) + ); assertThat(e.getMessage(), containsString("Missing max aggregation for time_field [time]")); - List compositeBuckets = Arrays.asList( createCompositeBucket(1000L, "time", 3, aggs, Collections.emptyList()), - createCompositeBucket(2000L, "time",5, aggs, Collections.emptyList()) + createCompositeBucket(2000L, "time", 5, aggs, Collections.emptyList()) ); - e = expectThrows(IllegalArgumentException.class, - () -> aggToStringComposite(Collections.emptySet(), compositeBuckets)); + e = expectThrows(IllegalArgumentException.class, () -> aggToStringComposite(Collections.emptySet(), compositeBuckets)); assertThat(e.getMessage(), containsString("Missing max aggregation for time_field [time]")); } public void testProcessGivenHistogramOnly() throws IOException { List histogramBuckets = Arrays.asList( - createHistogramBucket(1000L, 3, Collections.singletonList(createMax("timestamp", 1200))), - createHistogramBucket(2000L, 5, Collections.singletonList(createMax("timestamp", 2800))) + createHistogramBucket(1000L, 3, Collections.singletonList(createMax("timestamp", 1200))), + createHistogramBucket(2000L, 5, Collections.singletonList(createMax("timestamp", 2800))) ); timeField = "timestamp"; @@ -154,8 +147,8 @@ public void testProcessGivenHistogramOnly() throws IOException { public void testProcessGivenHistogramOnlyAndNoDocCount() throws IOException { List histogramBuckets = Arrays.asList( - createHistogramBucket(1000L, 3, Collections.singletonList(createMax("time", 1000))), - createHistogramBucket(2000L, 5, Collections.singletonList(createMax("time", 2000))) + createHistogramBucket(1000L, 3, Collections.singletonList(createMax("time", 1000))), + createHistogramBucket(2000L, 5, Collections.singletonList(createMax("time", 2000))) ); includeDocCount = false; @@ -165,7 +158,6 @@ public void testProcessGivenHistogramOnlyAndNoDocCount() throws IOException { assertThat(keyValuePairsWritten, equalTo(2L)); } - public void testProcessGivenCompositeOnly() throws IOException { compositeAggValueSource = "timestamp"; List compositeBuckets = Arrays.asList( @@ -196,13 +188,15 @@ public void testProcessGivenCompositeOnlyAndNoDocCount() throws IOException { public void testProcessGivenCompositeWithDocAndTerms() throws IOException { compositeAggValueSource = "timestamp"; List compositeBuckets = Arrays.asList( - createCompositeBucket(1000L, + createCompositeBucket( + 1000L, "timestamp", 3, Collections.singletonList(createMax("timestamp", 1200)), Arrays.asList(Tuple.tuple("foo", "value1"), Tuple.tuple("bar", "value1")) ), - createCompositeBucket(2000L, + createCompositeBucket( + 2000L, "timestamp", 5, Collections.singletonList(createMax("timestamp", 2800)), @@ -213,7 +207,8 @@ public void testProcessGivenCompositeWithDocAndTerms() throws IOException { timeField = "timestamp"; String json = aggToStringComposite(Sets.newHashSet("foo", "bar"), compositeBuckets); - assertThat(json, + assertThat( + json, equalTo( "{\"bar\":\"value1\",\"foo\":\"value1\",\"timestamp\":1200,\"doc_count\":3}" + " {\"bar\":\"value2\",\"foo\":\"value2\",\"timestamp\":2800,\"doc_count\":5}" @@ -225,105 +220,146 @@ public void testProcessGivenCompositeWithDocAndTerms() throws IOException { public void testProcessGivenTopLevelAggIsNotHistogram() throws IOException { List histogramABuckets = Arrays.asList( - createHistogramBucket(1000L, 3, Arrays.asList( - createMax("time", 1000), createSingleValue("my_value", 1.0))), - createHistogramBucket(2000L, 4, Arrays.asList( - createMax("time", 2000), createSingleValue("my_value", 2.0))), - createHistogramBucket(3000L, 5, Arrays.asList( - createMax("time", 3000), createSingleValue("my_value", 3.0))) + createHistogramBucket(1000L, 3, Arrays.asList(createMax("time", 1000), createSingleValue("my_value", 1.0))), + createHistogramBucket(2000L, 4, Arrays.asList(createMax("time", 2000), createSingleValue("my_value", 2.0))), + createHistogramBucket(3000L, 5, Arrays.asList(createMax("time", 3000), createSingleValue("my_value", 3.0))) ); Histogram histogramA = createHistogramAggregation("buckets", histogramABuckets); List histogramBBuckets = Arrays.asList( - createHistogramBucket(1000L, 6, Arrays.asList( - createMax("time", 1000), createSingleValue("my_value", 10.0))), - createHistogramBucket(2000L, 7, Arrays.asList( - createMax("time", 2000), createSingleValue("my_value", 20.0))), - createHistogramBucket(3000L, 8, Arrays.asList( - createMax("time", 3000), createSingleValue("my_value", 30.0))) + createHistogramBucket(1000L, 6, Arrays.asList(createMax("time", 1000), createSingleValue("my_value", 10.0))), + createHistogramBucket(2000L, 7, Arrays.asList(createMax("time", 2000), createSingleValue("my_value", 20.0))), + createHistogramBucket(3000L, 8, Arrays.asList(createMax("time", 3000), createSingleValue("my_value", 30.0))) ); Histogram histogramB = createHistogramAggregation("buckets", histogramBBuckets); - Terms terms = createTerms("my_field", new Term("A", 20, Collections.singletonList(histogramA)), - new Term("B", 2, Collections.singletonList(histogramB))); - + Terms terms = createTerms( + "my_field", + new Term("A", 20, Collections.singletonList(histogramA)), + new Term("B", 2, Collections.singletonList(histogramB)) + ); String json = aggToString(Sets.newHashSet("my_value", "my_field"), createAggs(Collections.singletonList(terms))); - assertThat(json, equalTo("{\"my_field\":\"A\",\"time\":1000,\"my_value\":1.0,\"doc_count\":3} " + - "{\"my_field\":\"B\",\"time\":1000,\"my_value\":10.0,\"doc_count\":6} " + - "{\"my_field\":\"A\",\"time\":2000,\"my_value\":2.0,\"doc_count\":4} " + - "{\"my_field\":\"B\",\"time\":2000,\"my_value\":20.0,\"doc_count\":7} " + - "{\"my_field\":\"A\",\"time\":3000,\"my_value\":3.0,\"doc_count\":5} " + - "{\"my_field\":\"B\",\"time\":3000,\"my_value\":30.0,\"doc_count\":8}" - )); + assertThat( + json, + equalTo( + "{\"my_field\":\"A\",\"time\":1000,\"my_value\":1.0,\"doc_count\":3} " + + "{\"my_field\":\"B\",\"time\":1000,\"my_value\":10.0,\"doc_count\":6} " + + "{\"my_field\":\"A\",\"time\":2000,\"my_value\":2.0,\"doc_count\":4} " + + "{\"my_field\":\"B\",\"time\":2000,\"my_value\":20.0,\"doc_count\":7} " + + "{\"my_field\":\"A\",\"time\":3000,\"my_value\":3.0,\"doc_count\":5} " + + "{\"my_field\":\"B\",\"time\":3000,\"my_value\":30.0,\"doc_count\":8}" + ) + ); } public void testProcessGivenSingleMetricPerHistogram() throws IOException { List histogramBuckets = Arrays.asList( - createHistogramBucket(1000L, 3, Arrays.asList( - createMax("time", 1000), createSingleValue("my_value", 1.0))), - createHistogramBucket(2000L, 3, Arrays.asList( - createMax("time", 2000), createSingleValue("my_value", Double.NEGATIVE_INFINITY))), - createHistogramBucket(3000L, 5, Arrays.asList( - createMax("time", 3000), createSingleValue("my_value", 3.0))) + createHistogramBucket(1000L, 3, Arrays.asList(createMax("time", 1000), createSingleValue("my_value", 1.0))), + createHistogramBucket( + 2000L, + 3, + Arrays.asList(createMax("time", 2000), createSingleValue("my_value", Double.NEGATIVE_INFINITY)) + ), + createHistogramBucket(3000L, 5, Arrays.asList(createMax("time", 3000), createSingleValue("my_value", 3.0))) ); String json = aggToString(Sets.newHashSet("my_value"), histogramBuckets); - assertThat(json, equalTo("{\"time\":1000,\"my_value\":1.0,\"doc_count\":3} " + - "{\"time\":2000,\"doc_count\":3} " + - "{\"time\":3000,\"my_value\":3.0,\"doc_count\":5}")); + assertThat( + json, + equalTo( + "{\"time\":1000,\"my_value\":1.0,\"doc_count\":3} " + + "{\"time\":2000,\"doc_count\":3} " + + "{\"time\":3000,\"my_value\":3.0,\"doc_count\":5}" + ) + ); } public void testProcessGivenTermsPerHistogram() throws IOException { List histogramBuckets = Arrays.asList( - createHistogramBucket(1000L, 4, Arrays.asList( - createMax("time", 1100), - createTerms("my_field", new Term("a", 1), new Term("b", 2), new Term("c", 1)))), - createHistogramBucket(2000L, 5, Arrays.asList( - createMax("time", 2200), - createTerms("my_field", new Term("a", 5), new Term("b", 2)))), - createHistogramBucket(3000L, 0, Collections.singletonList(createMax("time", -1))), - createHistogramBucket(4000L, 7, Arrays.asList( - createMax("time", 4400), - createTerms("my_field", new Term("c", 4), new Term("b", 3)))) + createHistogramBucket( + 1000L, + 4, + Arrays.asList(createMax("time", 1100), createTerms("my_field", new Term("a", 1), new Term("b", 2), new Term("c", 1))) + ), + createHistogramBucket( + 2000L, + 5, + Arrays.asList(createMax("time", 2200), createTerms("my_field", new Term("a", 5), new Term("b", 2))) + ), + createHistogramBucket(3000L, 0, Collections.singletonList(createMax("time", -1))), + createHistogramBucket( + 4000L, + 7, + Arrays.asList(createMax("time", 4400), createTerms("my_field", new Term("c", 4), new Term("b", 3))) + ) ); String json = aggToString(Sets.newHashSet("time", "my_field"), histogramBuckets); - assertThat(json, equalTo("{\"time\":1100,\"my_field\":\"a\",\"doc_count\":1} " + - "{\"time\":1100,\"my_field\":\"b\",\"doc_count\":2} " + - "{\"time\":1100,\"my_field\":\"c\",\"doc_count\":1} " + - "{\"time\":2200,\"my_field\":\"a\",\"doc_count\":5} " + - "{\"time\":2200,\"my_field\":\"b\",\"doc_count\":2} " + - "{\"time\":4400,\"my_field\":\"c\",\"doc_count\":4} " + - "{\"time\":4400,\"my_field\":\"b\",\"doc_count\":3}")); + assertThat( + json, + equalTo( + "{\"time\":1100,\"my_field\":\"a\",\"doc_count\":1} " + + "{\"time\":1100,\"my_field\":\"b\",\"doc_count\":2} " + + "{\"time\":1100,\"my_field\":\"c\",\"doc_count\":1} " + + "{\"time\":2200,\"my_field\":\"a\",\"doc_count\":5} " + + "{\"time\":2200,\"my_field\":\"b\",\"doc_count\":2} " + + "{\"time\":4400,\"my_field\":\"c\",\"doc_count\":4} " + + "{\"time\":4400,\"my_field\":\"b\",\"doc_count\":3}" + ) + ); } public void testProcessGivenSingleMetricPerSingleTermsPerHistogram() throws IOException { List histogramBuckets = Arrays.asList( - createHistogramBucket(1000L, 4, Arrays.asList( - createMax("time", 1000), - createTerms("my_field", new Term("a", 1, "my_value", 11.0), - new Term("b", 2, "my_value", 12.0), new Term("c", 1, "my_value", 13.0)))), - createHistogramBucket(2000L, 5, Arrays.asList( - createMax("time", 2000), - createTerms("my_field", new Term("a", 5, "my_value", 21.0), new Term("b", 2, "my_value", 22.0)))), - createHistogramBucket(3000L, 0, Collections.singletonList(createMax("time", 3000))), - createHistogramBucket(4000L, 7, Arrays.asList( - createMax("time", 4000), - createTerms("my_field", new Term("c", 4, "my_value", 41.0), new Term("b", 3, "my_value", 42.0)))) + createHistogramBucket( + 1000L, + 4, + Arrays.asList( + createMax("time", 1000), + createTerms( + "my_field", + new Term("a", 1, "my_value", 11.0), + new Term("b", 2, "my_value", 12.0), + new Term("c", 1, "my_value", 13.0) + ) + ) + ), + createHistogramBucket( + 2000L, + 5, + Arrays.asList( + createMax("time", 2000), + createTerms("my_field", new Term("a", 5, "my_value", 21.0), new Term("b", 2, "my_value", 22.0)) + ) + ), + createHistogramBucket(3000L, 0, Collections.singletonList(createMax("time", 3000))), + createHistogramBucket( + 4000L, + 7, + Arrays.asList( + createMax("time", 4000), + createTerms("my_field", new Term("c", 4, "my_value", 41.0), new Term("b", 3, "my_value", 42.0)) + ) + ) ); String json = aggToString(Sets.newHashSet("my_field", "my_value"), histogramBuckets); - assertThat(json, equalTo("{\"time\":1000,\"my_field\":\"a\",\"my_value\":11.0,\"doc_count\":1} " + - "{\"time\":1000,\"my_field\":\"b\",\"my_value\":12.0,\"doc_count\":2} " + - "{\"time\":1000,\"my_field\":\"c\",\"my_value\":13.0,\"doc_count\":1} " + - "{\"time\":2000,\"my_field\":\"a\",\"my_value\":21.0,\"doc_count\":5} " + - "{\"time\":2000,\"my_field\":\"b\",\"my_value\":22.0,\"doc_count\":2} " + - "{\"time\":4000,\"my_field\":\"c\",\"my_value\":41.0,\"doc_count\":4} " + - "{\"time\":4000,\"my_field\":\"b\",\"my_value\":42.0,\"doc_count\":3}")); + assertThat( + json, + equalTo( + "{\"time\":1000,\"my_field\":\"a\",\"my_value\":11.0,\"doc_count\":1} " + + "{\"time\":1000,\"my_field\":\"b\",\"my_value\":12.0,\"doc_count\":2} " + + "{\"time\":1000,\"my_field\":\"c\",\"my_value\":13.0,\"doc_count\":1} " + + "{\"time\":2000,\"my_field\":\"a\",\"my_value\":21.0,\"doc_count\":5} " + + "{\"time\":2000,\"my_field\":\"b\",\"my_value\":22.0,\"doc_count\":2} " + + "{\"time\":4000,\"my_field\":\"c\",\"my_value\":41.0,\"doc_count\":4} " + + "{\"time\":4000,\"my_field\":\"b\",\"my_value\":42.0,\"doc_count\":3}" + ) + ); } public void testProcessGivenMultipleSingleMetricPerSingleTermsPerHistogram() throws IOException { @@ -349,29 +385,53 @@ public void testProcessGivenMultipleSingleMetricPerSingleTermsPerHistogram() thr b4NumericAggs.put("my_value", 421.0); b4NumericAggs.put("my_value2", 422.0); List histogramBuckets = Arrays.asList( - createHistogramBucket(1000L, 4, Arrays.asList( - createMax("time", 1000), - createTerms("my_field", new Term("a", 1, a1NumericAggs), - new Term("b", 2, b1NumericAggs), new Term("c", 1, c1NumericAggs)))), - createHistogramBucket(2000L, 5, Arrays.asList( - createMax("time", 2000), - createTerms("my_field", new Term("a", 5, a2NumericAggs), new Term("b", 2, b2NumericAggs)))), - createHistogramBucket(3000L, 0, Collections.singletonList(createMax("time", 3000))), - createHistogramBucket(4000L, 7, Arrays.asList( - createMax("time", 4000), - createTerms("my_field", new Term("c", 4, c4NumericAggs), new Term("b", 3, b4NumericAggs)))) + createHistogramBucket( + 1000L, + 4, + Arrays.asList( + createMax("time", 1000), + createTerms( + "my_field", + new Term("a", 1, a1NumericAggs), + new Term("b", 2, b1NumericAggs), + new Term("c", 1, c1NumericAggs) + ) + ) + ), + createHistogramBucket( + 2000L, + 5, + Arrays.asList( + createMax("time", 2000), + createTerms("my_field", new Term("a", 5, a2NumericAggs), new Term("b", 2, b2NumericAggs)) + ) + ), + createHistogramBucket(3000L, 0, Collections.singletonList(createMax("time", 3000))), + createHistogramBucket( + 4000L, + 7, + Arrays.asList( + createMax("time", 4000), + createTerms("my_field", new Term("c", 4, c4NumericAggs), new Term("b", 3, b4NumericAggs)) + ) + ) ); includeDocCount = false; String json = aggToString(Sets.newHashSet("my_field", "my_value", "my_value2"), histogramBuckets); - assertThat(json, equalTo("{\"time\":1000,\"my_field\":\"a\",\"my_value\":111.0,\"my_value2\":112.0} " + - "{\"time\":1000,\"my_field\":\"b\",\"my_value2\":122.0} " + - "{\"time\":1000,\"my_field\":\"c\",\"my_value\":131.0,\"my_value2\":132.0} " + - "{\"time\":2000,\"my_field\":\"a\",\"my_value\":211.0,\"my_value2\":212.0} " + - "{\"time\":2000,\"my_field\":\"b\",\"my_value\":221.0,\"my_value2\":222.0} " + - "{\"time\":4000,\"my_field\":\"c\",\"my_value\":411.0,\"my_value2\":412.0} " + - "{\"time\":4000,\"my_field\":\"b\",\"my_value\":421.0,\"my_value2\":422.0}")); + assertThat( + json, + equalTo( + "{\"time\":1000,\"my_field\":\"a\",\"my_value\":111.0,\"my_value2\":112.0} " + + "{\"time\":1000,\"my_field\":\"b\",\"my_value2\":122.0} " + + "{\"time\":1000,\"my_field\":\"c\",\"my_value\":131.0,\"my_value2\":132.0} " + + "{\"time\":2000,\"my_field\":\"a\",\"my_value\":211.0,\"my_value2\":212.0} " + + "{\"time\":2000,\"my_field\":\"b\",\"my_value\":221.0,\"my_value2\":222.0} " + + "{\"time\":4000,\"my_field\":\"c\",\"my_value\":411.0,\"my_value2\":412.0} " + + "{\"time\":4000,\"my_field\":\"b\",\"my_value\":421.0,\"my_value2\":422.0}" + ) + ); } public void testProcessGivenUnsupportedAggregationUnderHistogram() { @@ -381,8 +441,10 @@ public void testProcessGivenUnsupportedAggregationUnderHistogram() { Aggregations subAggs = createAggs(Arrays.asList(createMax("time", 1000), anotherHistogram)); when(histogramBucket.getAggregations()).thenReturn(subAggs); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> aggToString(Sets.newHashSet("nested-agg"), histogramBucket)); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> aggToString(Sets.newHashSet("nested-agg"), histogramBucket) + ); assertThat(e.getMessage(), containsString("Unsupported aggregation type [nested-agg]")); } @@ -395,8 +457,10 @@ public void testProcessGivenMultipleBucketAggregations() { Aggregations subAggs = createAggs(Arrays.asList(createMax("time", 1000), terms1, terms2)); when(histogramBucket.getAggregations()).thenReturn(subAggs); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> aggToString(Sets.newHashSet("terms_1", "terms_2"), histogramBucket)); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> aggToString(Sets.newHashSet("terms_1", "terms_2"), histogramBucket) + ); assertThat(e.getMessage(), containsString("Multiple bucket aggregations at the same level are not supported")); } @@ -406,8 +470,13 @@ public void testProcessGivenMixedBucketAndLeafAggregationsAtSameLevel_BucketFirs Histogram.Bucket histogramBucket = createHistogramBucket(1000L, 2, Arrays.asList(terms, createMax("time", 1000), maxAgg)); String json = aggToString(Sets.newHashSet("terms", "max_value"), histogramBucket); - assertThat(json, equalTo("{\"time\":1000,\"max_value\":1200.0,\"terms\":\"a\",\"doc_count\":1} " + - "{\"time\":1000,\"max_value\":1200.0,\"terms\":\"b\",\"doc_count\":2}")); + assertThat( + json, + equalTo( + "{\"time\":1000,\"max_value\":1200.0,\"terms\":\"a\",\"doc_count\":1} " + + "{\"time\":1000,\"max_value\":1200.0,\"terms\":\"b\",\"doc_count\":2}" + ) + ); } public void testProcessGivenMixedBucketAndLeafAggregationsAtSameLevel_LeafFirst() throws IOException { @@ -416,68 +485,96 @@ public void testProcessGivenMixedBucketAndLeafAggregationsAtSameLevel_LeafFirst( Histogram.Bucket histogramBucket = createHistogramBucket(1000L, 2, Arrays.asList(createMax("time", 1000), maxAgg, terms)); String json = aggToString(Sets.newHashSet("terms", "max_value"), histogramBucket); - assertThat(json, equalTo("{\"time\":1000,\"max_value\":1200.0,\"terms\":\"a\",\"doc_count\":1} " + - "{\"time\":1000,\"max_value\":1200.0,\"terms\":\"b\",\"doc_count\":2}")); + assertThat( + json, + equalTo( + "{\"time\":1000,\"max_value\":1200.0,\"terms\":\"a\",\"doc_count\":1} " + + "{\"time\":1000,\"max_value\":1200.0,\"terms\":\"b\",\"doc_count\":2}" + ) + ); } public void testProcessGivenBucketAndLeafAggregationsButBucketNotInFields() throws IOException { List histogramBuckets = Arrays.asList( - createHistogramBucket(1000L, 4, Arrays.asList( - createMax("time", 1100), - createMax("my_value", 1), - createTerms("my_field", new Term("a", 1), new Term("b", 2), new Term("c", 1)))), - createHistogramBucket(2000L, 5, Arrays.asList( - createMax("time", 2200), - createMax("my_value", 2), - createTerms("my_field", new Term("a", 5), new Term("b", 2)))), - createHistogramBucket(3000L, 0, Collections.singletonList(createMax("time", -1))), - createHistogramBucket(4000L, 7, Arrays.asList( - createMax("time", 4400), - createMax("my_value", 4), - createTerms("my_field", new Term("c", 4), new Term("b", 3)))) + createHistogramBucket( + 1000L, + 4, + Arrays.asList( + createMax("time", 1100), + createMax("my_value", 1), + createTerms("my_field", new Term("a", 1), new Term("b", 2), new Term("c", 1)) + ) + ), + createHistogramBucket( + 2000L, + 5, + Arrays.asList( + createMax("time", 2200), + createMax("my_value", 2), + createTerms("my_field", new Term("a", 5), new Term("b", 2)) + ) + ), + createHistogramBucket(3000L, 0, Collections.singletonList(createMax("time", -1))), + createHistogramBucket( + 4000L, + 7, + Arrays.asList( + createMax("time", 4400), + createMax("my_value", 4), + createTerms("my_field", new Term("c", 4), new Term("b", 3)) + ) + ) ); String json = aggToString(Sets.newHashSet("time", "my_value"), histogramBuckets); - assertThat(json, equalTo("{\"time\":1100,\"my_value\":1.0,\"doc_count\":4} " + - "{\"time\":2200,\"my_value\":2.0,\"doc_count\":5} " + - "{\"time\":4400,\"my_value\":4.0,\"doc_count\":7}")); + assertThat( + json, + equalTo( + "{\"time\":1100,\"my_value\":1.0,\"doc_count\":4} " + + "{\"time\":2200,\"my_value\":2.0,\"doc_count\":5} " + + "{\"time\":4400,\"my_value\":4.0,\"doc_count\":7}" + ) + ); } public void testProcessGivenSinglePercentilesPerHistogram() throws IOException { List histogramBuckets = Arrays.asList( - createHistogramBucket(1000L, 4, Arrays.asList( - createMax("time", 1000), createPercentiles("my_field", 1.0))), - createHistogramBucket(2000L, 7, Arrays.asList( - createMax("time", 2000), createPercentiles("my_field", 2.0))), - createHistogramBucket(3000L, 10, Arrays.asList( - createMax("time", 3000), createPercentiles("my_field", Double.NEGATIVE_INFINITY))), - createHistogramBucket(4000L, 14, Arrays.asList( - createMax("time", 4000), createPercentiles("my_field", 4.0))) + createHistogramBucket(1000L, 4, Arrays.asList(createMax("time", 1000), createPercentiles("my_field", 1.0))), + createHistogramBucket(2000L, 7, Arrays.asList(createMax("time", 2000), createPercentiles("my_field", 2.0))), + createHistogramBucket( + 3000L, + 10, + Arrays.asList(createMax("time", 3000), createPercentiles("my_field", Double.NEGATIVE_INFINITY)) + ), + createHistogramBucket(4000L, 14, Arrays.asList(createMax("time", 4000), createPercentiles("my_field", 4.0))) ); String json = aggToString(Sets.newHashSet("my_field"), histogramBuckets); - assertThat(json, equalTo("{\"time\":1000,\"my_field\":1.0,\"doc_count\":4} " + - "{\"time\":2000,\"my_field\":2.0,\"doc_count\":7} " + - "{\"time\":3000,\"doc_count\":10} " + - "{\"time\":4000,\"my_field\":4.0,\"doc_count\":14}")); + assertThat( + json, + equalTo( + "{\"time\":1000,\"my_field\":1.0,\"doc_count\":4} " + + "{\"time\":2000,\"my_field\":2.0,\"doc_count\":7} " + + "{\"time\":3000,\"doc_count\":10} " + + "{\"time\":4000,\"my_field\":4.0,\"doc_count\":14}" + ) + ); } public void testProcessGivenMultiplePercentilesPerHistogram() { List histogramBuckets = Arrays.asList( - createHistogramBucket(1000L, 4, Arrays.asList( - createMax("time", 1000), createPercentiles("my_field", 1.0))), - createHistogramBucket(2000L, 7, Arrays.asList( - createMax("time", 2000), createPercentiles("my_field", 2.0, 5.0))), - createHistogramBucket(3000L, 10, Arrays.asList( - createMax("time", 3000), createPercentiles("my_field", 3.0))), - createHistogramBucket(4000L, 14, Arrays.asList( - createMax("time", 4000), createPercentiles("my_field", 4.0))) - ); - - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> aggToString(Sets.newHashSet("my_field"), histogramBuckets)); + createHistogramBucket(1000L, 4, Arrays.asList(createMax("time", 1000), createPercentiles("my_field", 1.0))), + createHistogramBucket(2000L, 7, Arrays.asList(createMax("time", 2000), createPercentiles("my_field", 2.0, 5.0))), + createHistogramBucket(3000L, 10, Arrays.asList(createMax("time", 3000), createPercentiles("my_field", 3.0))), + createHistogramBucket(4000L, 14, Arrays.asList(createMax("time", 4000), createPercentiles("my_field", 4.0))) + ); + + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> aggToString(Sets.newHashSet("my_field"), histogramBuckets) + ); assertThat(e.getMessage(), containsString("Multi-percentile aggregation [my_field] is not supported")); } @@ -514,87 +611,108 @@ public void testBucketAggContainsRequiredAgg() { public void testBucketBeforeStartIsPruned() throws IOException { List histogramBuckets = Arrays.asList( - createHistogramBucket(1000L, 4, Arrays.asList( - createMax("time", 1000), createPercentiles("my_field", 1.0))), - createHistogramBucket(2000L, 7, Arrays.asList( - createMax("time", 2000), createPercentiles("my_field", 2.0))), - createHistogramBucket(3000L, 10, Arrays.asList( - createMax("time", 3000), createPercentiles("my_field", 3.0))), - createHistogramBucket(4000L, 14, Arrays.asList( - createMax("time", 4000), createPercentiles("my_field", 4.0))) + createHistogramBucket(1000L, 4, Arrays.asList(createMax("time", 1000), createPercentiles("my_field", 1.0))), + createHistogramBucket(2000L, 7, Arrays.asList(createMax("time", 2000), createPercentiles("my_field", 2.0))), + createHistogramBucket(3000L, 10, Arrays.asList(createMax("time", 3000), createPercentiles("my_field", 3.0))), + createHistogramBucket(4000L, 14, Arrays.asList(createMax("time", 4000), createPercentiles("my_field", 4.0))) ); startTime = 2000; String json = aggToString(Sets.newHashSet("my_field"), histogramBuckets); - assertThat(json, equalTo("{\"time\":2000,\"my_field\":2.0,\"doc_count\":7} " + - "{\"time\":3000,\"my_field\":3.0,\"doc_count\":10} " + - "{\"time\":4000,\"my_field\":4.0,\"doc_count\":14}")); + assertThat( + json, + equalTo( + "{\"time\":2000,\"my_field\":2.0,\"doc_count\":7} " + + "{\"time\":3000,\"my_field\":3.0,\"doc_count\":10} " + + "{\"time\":4000,\"my_field\":4.0,\"doc_count\":14}" + ) + ); } public void testBucketsBeforeStartArePruned() throws IOException { List histogramBuckets = Arrays.asList( - createHistogramBucket(1000L, 4, Arrays.asList( - createMax("time", 1000), createPercentiles("my_field", 1.0))), - createHistogramBucket(2000L, 7, Arrays.asList( - createMax("time", 2000), createPercentiles("my_field", 2.0))), - createHistogramBucket(3000L, 10, Arrays.asList( - createMax("time", 3000), createPercentiles("my_field", 3.0))), - createHistogramBucket(4000L, 14, Arrays.asList( - createMax("time", 4000), createPercentiles("my_field", 4.0))) + createHistogramBucket(1000L, 4, Arrays.asList(createMax("time", 1000), createPercentiles("my_field", 1.0))), + createHistogramBucket(2000L, 7, Arrays.asList(createMax("time", 2000), createPercentiles("my_field", 2.0))), + createHistogramBucket(3000L, 10, Arrays.asList(createMax("time", 3000), createPercentiles("my_field", 3.0))), + createHistogramBucket(4000L, 14, Arrays.asList(createMax("time", 4000), createPercentiles("my_field", 4.0))) ); startTime = 3000; String json = aggToString(Sets.newHashSet("my_field"), histogramBuckets); - assertThat(json, equalTo("{\"time\":3000,\"my_field\":3.0,\"doc_count\":10} " + - "{\"time\":4000,\"my_field\":4.0,\"doc_count\":14}")); + assertThat( + json, + equalTo("{\"time\":3000,\"my_field\":3.0,\"doc_count\":10} " + "{\"time\":4000,\"my_field\":4.0,\"doc_count\":14}") + ); } public void testSingleBucketAgg() throws IOException { List histogramBuckets = Arrays.asList( - createHistogramBucket(1000L, 4, Arrays.asList( - createMax("time", 1000), - createSingleBucketAgg("agg1", 3, Collections.singletonList(createMax("field1", 5.0))), - createSingleBucketAgg("agg2", 1, Collections.singletonList(createMax("field2", 3.0))))), - createHistogramBucket(2000L, 7, Arrays.asList( - createMax("time", 2000), - createSingleBucketAgg("agg2", 3, Collections.singletonList(createMax("field2", 1.0))), - createSingleBucketAgg("agg1", 4, Collections.singletonList(createMax("field1", 7.0)))))); + createHistogramBucket( + 1000L, + 4, + Arrays.asList( + createMax("time", 1000), + createSingleBucketAgg("agg1", 3, Collections.singletonList(createMax("field1", 5.0))), + createSingleBucketAgg("agg2", 1, Collections.singletonList(createMax("field2", 3.0))) + ) + ), + createHistogramBucket( + 2000L, + 7, + Arrays.asList( + createMax("time", 2000), + createSingleBucketAgg("agg2", 3, Collections.singletonList(createMax("field2", 1.0))), + createSingleBucketAgg("agg1", 4, Collections.singletonList(createMax("field1", 7.0))) + ) + ) + ); String json = aggToString(Sets.newHashSet("field1", "field2"), histogramBuckets); - assertThat(json, equalTo("{\"time\":1000,\"field1\":5.0,\"field2\":3.0,\"doc_count\":4}" + - " {\"time\":2000,\"field2\":1.0,\"field1\":7.0,\"doc_count\":7}")); + assertThat( + json, + equalTo( + "{\"time\":1000,\"field1\":5.0,\"field2\":3.0,\"doc_count\":4}" + + " {\"time\":2000,\"field2\":1.0,\"field1\":7.0,\"doc_count\":7}" + ) + ); } public void testSingleBucketAgg_failureWithSubMultiBucket() { List histogramBuckets = Collections.singletonList( - createHistogramBucket(1000L, 4, Arrays.asList( - createMax("time", 1000), - createSingleBucketAgg("agg1", 3, - Arrays.asList(createHistogramAggregation("histo", Collections.emptyList()),createMax("field1", 5.0))), - createSingleBucketAgg("agg2", 1, - Arrays.asList(createHistogramAggregation("histo", Collections.emptyList()),createMax("field1", 3.0)))))); - + createHistogramBucket( + 1000L, + 4, + Arrays.asList( + createMax("time", 1000), + createSingleBucketAgg( + "agg1", + 3, + Arrays.asList(createHistogramAggregation("histo", Collections.emptyList()), createMax("field1", 5.0)) + ), + createSingleBucketAgg( + "agg2", + 1, + Arrays.asList(createHistogramAggregation("histo", Collections.emptyList()), createMax("field1", 3.0)) + ) + ) + ) + ); - expectThrows(IllegalArgumentException.class, - () -> aggToString(Sets.newHashSet("my_field"), histogramBuckets)); + expectThrows(IllegalArgumentException.class, () -> aggToString(Sets.newHashSet("my_field"), histogramBuckets)); } public void testGeoCentroidAgg() throws IOException { List histogramBuckets = Arrays.asList( - createHistogramBucket(1000L, 4, Arrays.asList( - createMax("time", 1000), - createGeoCentroid("geo_field", 4, 92.1, 93.1))), - createHistogramBucket(2000L, 7, Arrays.asList( - createMax("time", 2000), - createGeoCentroid("geo_field", 0, -1, -1)))); + createHistogramBucket(1000L, 4, Arrays.asList(createMax("time", 1000), createGeoCentroid("geo_field", 4, 92.1, 93.1))), + createHistogramBucket(2000L, 7, Arrays.asList(createMax("time", 2000), createGeoCentroid("geo_field", 0, -1, -1))) + ); String json = aggToString(Sets.newHashSet("geo_field"), histogramBuckets); - assertThat(json, equalTo("{\"time\":1000,\"geo_field\":\"92.1,93.1\",\"doc_count\":4}" + - " {\"time\":2000,\"doc_count\":7}")); + assertThat(json, equalTo("{\"time\":1000,\"geo_field\":\"92.1,93.1\",\"doc_count\":4}" + " {\"time\":2000,\"doc_count\":7}")); } private String aggToString(Set fields, Histogram.Bucket bucket) throws IOException { diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/CompositeAggregationDataExtractorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/CompositeAggregationDataExtractorTests.java index ce2b6f612db40..df5389a4a3c0a 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/CompositeAggregationDataExtractorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/CompositeAggregationDataExtractorTests.java @@ -15,8 +15,8 @@ import org.elasticsearch.action.search.ShardSearchFailure; import org.elasticsearch.client.Client; import org.elasticsearch.common.collect.MapBuilder; -import org.elasticsearch.core.Tuple; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.core.Tuple; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.rest.RestStatus; @@ -112,19 +112,18 @@ public void setUpTests() { compositeAggregationBuilder = AggregationBuilders.composite( "buckets", Arrays.asList( - new DateHistogramValuesSourceBuilder("time_bucket") - .field("time") - .fixedInterval(new DateHistogramInterval("1000ms")), - new TermsValuesSourceBuilder("airline").field("airline"))) + new DateHistogramValuesSourceBuilder("time_bucket").field("time").fixedInterval(new DateHistogramInterval("1000ms")), + new TermsValuesSourceBuilder("airline").field("airline") + ) + ) .size(10) .subAggregation(AggregationBuilders.max("time").field("time")) .subAggregation(AggregationBuilders.avg("responsetime").field("responsetime")); runtimeMappings = Collections.emptyMap(); timingStatsReporter = new DatafeedTimingStatsReporter(new DatafeedTimingStats(jobId), mock(DatafeedTimingStatsPersister.class)); - aggregatedSearchRequestBuilder = (searchSourceBuilder) -> new SearchRequestBuilder(testClient, SearchAction.INSTANCE) - .setSource(searchSourceBuilder) - .setAllowPartialSearchResults(false) - .setIndices(indices.toArray(String[]::new)); + aggregatedSearchRequestBuilder = (searchSourceBuilder) -> new SearchRequestBuilder(testClient, SearchAction.INSTANCE).setSource( + searchSourceBuilder + ).setAllowPartialSearchResults(false).setIndices(indices.toArray(String[]::new)); } public void testExtraction() throws IOException { @@ -143,13 +142,7 @@ public void testExtraction() throws IOException { Arrays.asList(createMax("time", 1999), createAvg("responsetime", 12.0)), Collections.singletonList(Tuple.tuple("airline", "b")) ), - createCompositeBucket( - 2000L, - "time_bucket", - 0, - Collections.emptyList(), - Collections.emptyList() - ), + createCompositeBucket(2000L, "time_bucket", 0, Collections.emptyList(), Collections.emptyList()), createCompositeBucket( 3000L, "time_bucket", @@ -163,16 +156,15 @@ public void testExtraction() throws IOException { 3, Arrays.asList(createMax("time", 3999), createAvg("responsetime", 32.0)), Collections.singletonList(Tuple.tuple("airline", "b")) - )); + ) + ); TestDataExtractor extractor = new TestDataExtractor(1000L, 4000L); - SearchResponse response = createSearchResponse("buckets", + SearchResponse response = createSearchResponse( + "buckets", compositeBucket, - MapBuilder.newMapBuilder() - .put("time_bucket", 4000L) - .put("airline", "d") - .map() + MapBuilder.newMapBuilder().put("time_bucket", 4000L).put("airline", "d").map() ); extractor.setNextResponse(response); @@ -180,19 +172,26 @@ public void testExtraction() throws IOException { Optional stream = extractor.next(); assertThat(stream.isPresent(), is(true)); String expectedStream = "{\"airline\":\"a\",\"time\":1999,\"responsetime\":11.0,\"doc_count\":1} " - + "{\"airline\":\"b\",\"time\":1999,\"responsetime\":12.0,\"doc_count\":2} " - + "{\"airline\":\"c\",\"time\":3999,\"responsetime\":31.0,\"doc_count\":4} " - + "{\"airline\":\"b\",\"time\":3999,\"responsetime\":32.0,\"doc_count\":3}"; + + "{\"airline\":\"b\",\"time\":1999,\"responsetime\":12.0,\"doc_count\":2} " + + "{\"airline\":\"c\",\"time\":3999,\"responsetime\":31.0,\"doc_count\":4} " + + "{\"airline\":\"b\",\"time\":3999,\"responsetime\":32.0,\"doc_count\":3}"; assertThat(asString(stream.get()), equalTo(expectedStream)); assertThat(capturedSearchRequests.size(), equalTo(1)); String searchRequest = capturedSearchRequests.get(0).toString().replaceAll("\\s", ""); assertThat(searchRequest, containsString("\"size\":0")); - assertThat(searchRequest, containsString("\"query\":{\"bool\":{\"filter\":[{\"match_all\":{\"boost\":1.0}}," + - "{\"range\":{\"time\":{\"from\":1000,\"to\":4000,\"include_lower\":true,\"include_upper\":false," + - "\"format\":\"epoch_millis\",\"boost\":1.0}}}]")); - assertThat(searchRequest, - stringContainsInOrder(Arrays.asList("aggregations", "composite", "time", "terms", "airline", "avg", "responsetime"))); + assertThat( + searchRequest, + containsString( + "\"query\":{\"bool\":{\"filter\":[{\"match_all\":{\"boost\":1.0}}," + + "{\"range\":{\"time\":{\"from\":1000,\"to\":4000,\"include_lower\":true,\"include_upper\":false," + + "\"format\":\"epoch_millis\",\"boost\":1.0}}}]" + ) + ); + assertThat( + searchRequest, + stringContainsInOrder(Arrays.asList("aggregations", "composite", "time", "terms", "airline", "avg", "responsetime")) + ); } public void testExtractionGivenResponseHasNullAggs() throws IOException { @@ -252,10 +251,7 @@ public void testExtractionCancelOnFirstPage() throws IOException { SearchResponse response = createSearchResponse( "buckets", buckets, - MapBuilder.newMapBuilder() - .put("time_bucket", 1000L) - .put("airline", "d") - .map() + MapBuilder.newMapBuilder().put("time_bucket", 1000L).put("airline", "d").map() ); extractor.setNextResponse(response); extractor.cancel(); @@ -288,10 +284,7 @@ public void testExtractionGivenCancelHalfWay() throws IOException { SearchResponse response = createSearchResponse( "buckets", buckets, - MapBuilder.newMapBuilder() - .put("time_bucket", 1000L) - .put("airline", "d") - .map() + MapBuilder.newMapBuilder().put("time_bucket", 1000L).put("airline", "d").map() ); extractor.setNextResponse(response); @@ -316,18 +309,16 @@ public void testExtractionGivenCancelHalfWay() throws IOException { timestamp, "time_bucket", 3, - Arrays.asList(createMax("time", randomLongBetween(timestamp, timestamp + 999)), - createAvg("responsetime", 32.0)), + Arrays.asList(createMax("time", randomLongBetween(timestamp, timestamp + 999)), createAvg("responsetime", 32.0)), Collections.singletonList(Tuple.tuple("airline", "c")) ) ); } - response = createSearchResponse("buckets", + response = createSearchResponse( + "buckets", buckets, - MapBuilder.newMapBuilder() - .put("time_bucket", 3000L) - .put("airline", "a") - .map()); + MapBuilder.newMapBuilder().put("time_bucket", 3000L).put("airline", "a").map() + ); extractor.setNextResponse(response); extractor.cancel(); assertThat(extractor.hasNext(), is(true)); @@ -362,7 +353,8 @@ private CompositeAggregationDataExtractorContext createContext(long start, long true, Collections.emptyMap(), SearchRequest.DEFAULT_INDICES_OPTIONS, - runtimeMappings); + runtimeMappings + ); } @SuppressWarnings("unchecked") @@ -370,7 +362,7 @@ private SearchResponse createSearchResponse(String aggName, List)compositeAggregation.getBuckets()).thenReturn(buckets); + when((List) compositeAggregation.getBuckets()).thenReturn(buckets); Aggregations searchAggs = AggregationTestUtils.createAggs(Collections.singletonList(compositeAggregation)); return createSearchResponse(searchAggs); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/chunked/ChunkedDataExtractorFactoryTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/chunked/ChunkedDataExtractorFactoryTests.java index 7362038446405..f782e09b0107f 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/chunked/ChunkedDataExtractorFactoryTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/chunked/ChunkedDataExtractorFactoryTests.java @@ -8,18 +8,18 @@ import org.elasticsearch.client.Client; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.search.SearchModule; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.AggregatorFactories; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfig; -import org.elasticsearch.xpack.ml.datafeed.DatafeedTimingStatsReporter; -import org.elasticsearch.xpack.ml.datafeed.extractor.DataExtractorFactory; import org.elasticsearch.xpack.core.ml.job.config.AnalysisConfig; import org.elasticsearch.xpack.core.ml.job.config.DataDescription; import org.elasticsearch.xpack.core.ml.job.config.Detector; import org.elasticsearch.xpack.core.ml.job.config.Job; +import org.elasticsearch.xpack.ml.datafeed.DatafeedTimingStatsReporter; +import org.elasticsearch.xpack.ml.datafeed.extractor.DataExtractorFactory; import org.junit.Before; import java.util.Arrays; @@ -92,8 +92,11 @@ public void testIdentityTimeAligner() { private ChunkedDataExtractorFactory createFactory(long histogramInterval) { AggregatorFactories.Builder aggs = new AggregatorFactories.Builder().addAggregator( - AggregationBuilders.histogram("time").field("time").interval(histogramInterval).subAggregation( - AggregationBuilders.max("time").field("time"))); + AggregationBuilders.histogram("time") + .field("time") + .interval(histogramInterval) + .subAggregation(AggregationBuilders.max("time").field("time")) + ); DataDescription.Builder dataDescription = new DataDescription.Builder(); dataDescription.setTimeField("time"); Detector.Builder detectorBuilder = new Detector.Builder(); @@ -113,6 +116,7 @@ private ChunkedDataExtractorFactory createFactory(long histogramInterval) { jobBuilder.build(new Date()), xContentRegistry(), dataExtractorFactory, - timingStatsReporter); + timingStatsReporter + ); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/chunked/ChunkedDataExtractorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/chunked/ChunkedDataExtractorTests.java index 7f4affe3b994a..500f8d414f76d 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/chunked/ChunkedDataExtractorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/chunked/ChunkedDataExtractorTests.java @@ -16,7 +16,6 @@ import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; -import org.mockito.Mockito; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; @@ -31,6 +30,7 @@ import org.elasticsearch.xpack.ml.datafeed.DatafeedTimingStatsReporter.DatafeedTimingStatsPersister; import org.elasticsearch.xpack.ml.datafeed.extractor.DataExtractorFactory; import org.junit.Before; +import org.mockito.Mockito; import java.io.IOException; import java.io.InputStream; @@ -147,11 +147,20 @@ public void testExtractionGivenSpecifiedChunk() throws IOException { assertThat(capturedSearchRequests.size(), equalTo(1)); String searchRequest = capturedSearchRequests.get(0).toString().replaceAll("\\s", ""); assertThat(searchRequest, containsString("\"size\":0")); - assertThat(searchRequest, containsString("\"query\":{\"bool\":{\"filter\":[{\"match_all\":{\"boost\":1.0}}," + - "{\"range\":{\"time\":{\"from\":1000,\"to\":2300,\"include_lower\":true,\"include_upper\":false," + - "\"format\":\"epoch_millis\",\"boost\":1.0}}}]")); - assertThat(searchRequest, containsString("\"aggregations\":{\"earliest_time\":{\"min\":{\"field\":\"time\"}}," + - "\"latest_time\":{\"max\":{\"field\":\"time\"}}}}")); + assertThat( + searchRequest, + containsString( + "\"query\":{\"bool\":{\"filter\":[{\"match_all\":{\"boost\":1.0}}," + + "{\"range\":{\"time\":{\"from\":1000,\"to\":2300,\"include_lower\":true,\"include_upper\":false," + + "\"format\":\"epoch_millis\",\"boost\":1.0}}}]" + ) + ); + assertThat( + searchRequest, + containsString( + "\"aggregations\":{\"earliest_time\":{\"min\":{\"field\":\"time\"}}," + "\"latest_time\":{\"max\":{\"field\":\"time\"}}}}" + ) + ); assertThat(searchRequest, not(containsString("\"track_total_hits\":false"))); assertThat(searchRequest, not(containsString("\"sort\""))); } @@ -188,11 +197,20 @@ public void testExtractionGivenSpecifiedChunkAndAggs() throws IOException { assertThat(capturedSearchRequests.size(), equalTo(1)); String searchRequest = capturedSearchRequests.get(0).toString().replaceAll("\\s", ""); assertThat(searchRequest, containsString("\"size\":0")); - assertThat(searchRequest, containsString("\"query\":{\"bool\":{\"filter\":[{\"match_all\":{\"boost\":1.0}}," + - "{\"range\":{\"time\":{\"from\":1000,\"to\":2300,\"include_lower\":true,\"include_upper\":false," + - "\"format\":\"epoch_millis\",\"boost\":1.0}}}]")); - assertThat(searchRequest, containsString("\"aggregations\":{\"earliest_time\":{\"min\":{\"field\":\"time\"}}," + - "\"latest_time\":{\"max\":{\"field\":\"time\"}}}}")); + assertThat( + searchRequest, + containsString( + "\"query\":{\"bool\":{\"filter\":[{\"match_all\":{\"boost\":1.0}}," + + "{\"range\":{\"time\":{\"from\":1000,\"to\":2300,\"include_lower\":true,\"include_upper\":false," + + "\"format\":\"epoch_millis\",\"boost\":1.0}}}]" + ) + ); + assertThat( + searchRequest, + containsString( + "\"aggregations\":{\"earliest_time\":{\"min\":{\"field\":\"time\"}}," + "\"latest_time\":{\"max\":{\"field\":\"time\"}}}}" + ) + ); assertThat(searchRequest, not(containsString("\"track_total_hits\":false"))); assertThat(searchRequest, not(containsString("\"sort\""))); } @@ -514,7 +532,7 @@ public void testNoDataSummaryHasNoData() { private SearchResponse createSearchResponse(long totalHits, long earliestTime, long latestTime) { SearchResponse searchResponse = mock(SearchResponse.class); when(searchResponse.status()).thenReturn(RestStatus.OK); - SearchHit[] hits = new SearchHit[(int)totalHits]; + SearchHit[] hits = new SearchHit[(int) totalHits]; SearchHits searchHits = new SearchHits(hits, new TotalHits(totalHits, TotalHits.Relation.EQUAL_TO), 1); when(searchResponse.getHits()).thenReturn(searchHits); @@ -527,7 +545,8 @@ private SearchResponse createSearchResponse(long totalHits, long earliestTime, l when(max.getValue()).thenReturn((double) latestTime); when(max.getName()).thenReturn("latest_time"); aggs.add(max); - Aggregations aggregations = new Aggregations(aggs) {}; + Aggregations aggregations = new Aggregations(aggs) { + }; when(searchResponse.getAggregations()).thenReturn(aggregations); return searchResponse; } @@ -548,7 +567,8 @@ private SearchResponse createNullSearchResponse() { when(max.getValue()).thenReturn(Double.POSITIVE_INFINITY); when(max.getName()).thenReturn("latest_time"); aggs.add(max); - Aggregations aggregations = new Aggregations(aggs) {}; + Aggregations aggregations = new Aggregations(aggs) { + }; when(searchResponse.getAggregations()).thenReturn(aggregations); return searchResponse; } @@ -558,9 +578,22 @@ private ChunkedDataExtractorContext createContext(long start, long end) { } private ChunkedDataExtractorContext createContext(long start, long end, boolean hasAggregations, Long histogramInterval) { - return new ChunkedDataExtractorContext(jobId, timeField, indices, query, scrollSize, start, end, chunkSpan, - ChunkedDataExtractorFactory.newIdentityTimeAligner(), Collections.emptyMap(), hasAggregations, histogramInterval, - SearchRequest.DEFAULT_INDICES_OPTIONS, Collections.emptyMap()); + return new ChunkedDataExtractorContext( + jobId, + timeField, + indices, + query, + scrollSize, + start, + end, + chunkSpan, + ChunkedDataExtractorFactory.newIdentityTimeAligner(), + Collections.emptyMap(), + hasAggregations, + histogramInterval, + SearchRequest.DEFAULT_INDICES_OPTIONS, + Collections.emptyMap() + ); } private static class StubSubExtractor implements DataExtractor { diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/ScrollDataExtractorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/ScrollDataExtractorTests.java index 87a52171fb5d5..bc8b0e6320388 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/ScrollDataExtractorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/ScrollDataExtractorTests.java @@ -18,11 +18,11 @@ import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.ShardSearchFailure; import org.elasticsearch.client.Client; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.document.DocumentField; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.TimeValue; +import org.elasticsearch.core.Tuple; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.rest.RestStatus; @@ -159,8 +159,10 @@ public void setUpTests() { capturedContinueScrollIds = new ArrayList<>(); jobId = "test-job"; ExtractedField timeField = new TimeField("time", ExtractedField.Method.DOC_VALUE); - extractedFields = new TimeBasedExtractedFields(timeField, - Arrays.asList(timeField, new DocValueField("field_1", Collections.singleton("keyword")))); + extractedFields = new TimeBasedExtractedFields( + timeField, + Arrays.asList(timeField, new DocValueField("field_1", Collections.singleton("keyword"))) + ); indices = Arrays.asList("index-1", "index-2"); query = QueryBuilders.matchAllQuery(); scriptFields = Collections.emptyList(); @@ -175,11 +177,7 @@ public void setUpTests() { public void testSinglePageExtraction() throws IOException { TestDataExtractor extractor = new TestDataExtractor(1000L, 2000L); - SearchResponse response1 = createSearchResponse( - Arrays.asList(1100L, 1200L), - Arrays.asList("a1", "a2"), - Arrays.asList("b1", "b2") - ); + SearchResponse response1 = createSearchResponse(Arrays.asList(1100L, 1200L), Arrays.asList("a1", "a2"), Arrays.asList("b1", "b2")); extractor.setNextResponse(response1); assertThat(extractor.hasNext(), is(true)); @@ -197,9 +195,14 @@ public void testSinglePageExtraction() throws IOException { String searchRequest = capturedSearchRequests.get(0).toString().replaceAll("\\s", ""); assertThat(searchRequest, containsString("\"size\":1000")); - assertThat(searchRequest, containsString("\"query\":{\"bool\":{\"filter\":[{\"match_all\":{\"boost\":1.0}}," + - "{\"range\":{\"time\":{\"from\":1000,\"to\":2000,\"include_lower\":true,\"include_upper\":false," + - "\"format\":\"epoch_millis\",\"boost\":1.0}}}]")); + assertThat( + searchRequest, + containsString( + "\"query\":{\"bool\":{\"filter\":[{\"match_all\":{\"boost\":1.0}}," + + "{\"range\":{\"time\":{\"from\":1000,\"to\":2000,\"include_lower\":true,\"include_upper\":false," + + "\"format\":\"epoch_millis\",\"boost\":1.0}}}]" + ) + ); assertThat(searchRequest, containsString("\"sort\":[{\"time\":{\"order\":\"asc\"}}]")); assertThat(searchRequest, containsString("\"stored_fields\":\"_none_\"")); @@ -214,11 +217,7 @@ public void testSinglePageExtraction() throws IOException { public void testMultiplePageExtraction() throws IOException { TestDataExtractor extractor = new TestDataExtractor(1000L, 10000L); - SearchResponse response1 = createSearchResponse( - Arrays.asList(1000L, 2000L), - Arrays.asList("a1", "a2"), - Arrays.asList("b1", "b2") - ); + SearchResponse response1 = createSearchResponse(Arrays.asList(1000L, 2000L), Arrays.asList("a1", "a2"), Arrays.asList("b1", "b2")); extractor.setNextResponse(response1); assertThat(extractor.hasNext(), is(true)); @@ -227,11 +226,7 @@ public void testMultiplePageExtraction() throws IOException { String expectedStream = "{\"time\":1000,\"field_1\":\"a1\"} {\"time\":2000,\"field_1\":\"a2\"}"; assertThat(asString(stream.get()), equalTo(expectedStream)); - SearchResponse response2 = createSearchResponse( - Arrays.asList(3000L, 4000L), - Arrays.asList("a3", "a4"), - Arrays.asList("b3", "b4") - ); + SearchResponse response2 = createSearchResponse(Arrays.asList(3000L, 4000L), Arrays.asList("a3", "a4"), Arrays.asList("b3", "b4")); extractor.setNextResponse(response2); assertThat(extractor.hasNext(), is(true)); @@ -249,9 +244,14 @@ public void testMultiplePageExtraction() throws IOException { String searchRequest1 = capturedSearchRequests.get(0).toString().replaceAll("\\s", ""); assertThat(searchRequest1, containsString("\"size\":1000")); - assertThat(searchRequest1, containsString("\"query\":{\"bool\":{\"filter\":[{\"match_all\":{\"boost\":1.0}}," + - "{\"range\":{\"time\":{\"from\":1000,\"to\":10000,\"include_lower\":true,\"include_upper\":false," + - "\"format\":\"epoch_millis\",\"boost\":1.0}}}]")); + assertThat( + searchRequest1, + containsString( + "\"query\":{\"bool\":{\"filter\":[{\"match_all\":{\"boost\":1.0}}," + + "{\"range\":{\"time\":{\"from\":1000,\"to\":10000,\"include_lower\":true,\"include_upper\":false," + + "\"format\":\"epoch_millis\",\"boost\":1.0}}}]" + ) + ); assertThat(searchRequest1, containsString("\"sort\":[{\"time\":{\"order\":\"asc\"}}]")); assertThat(capturedContinueScrollIds.size(), equalTo(2)); @@ -266,11 +266,7 @@ public void testMultiplePageExtraction() throws IOException { public void testMultiplePageExtractionGivenCancel() throws IOException { TestDataExtractor extractor = new TestDataExtractor(1000L, 10000L); - SearchResponse response1 = createSearchResponse( - Arrays.asList(1000L, 2000L), - Arrays.asList("a1", "a2"), - Arrays.asList("b1", "b2") - ); + SearchResponse response1 = createSearchResponse(Arrays.asList(1000L, 2000L), Arrays.asList("a1", "a2"), Arrays.asList("b1", "b2")); extractor.setNextResponse(response1); assertThat(extractor.hasNext(), is(true)); @@ -282,9 +278,9 @@ public void testMultiplePageExtractionGivenCancel() throws IOException { extractor.cancel(); SearchResponse response2 = createSearchResponse( - Arrays.asList(2000L, 2000L, 3000L), - Arrays.asList("a3", "a4", "a5"), - Arrays.asList("b3", "b4", "b5") + Arrays.asList(2000L, 2000L, 3000L), + Arrays.asList("a3", "a4", "a5"), + Arrays.asList("b3", "b4", "b5") ); extractor.setNextResponse(response2); @@ -313,11 +309,7 @@ public void testExtractionGivenInitSearchResponseHasError() { public void testExtractionGivenContinueScrollResponseHasError() throws IOException { TestDataExtractor extractor = new TestDataExtractor(1000L, 10000L); - SearchResponse response1 = createSearchResponse( - Arrays.asList(1000L, 2000L), - Arrays.asList("a1", "a2"), - Arrays.asList("b1", "b2") - ); + SearchResponse response1 = createSearchResponse(Arrays.asList(1000L, 2000L), Arrays.asList("a1", "a2"), Arrays.asList("b1", "b2")); extractor.setNextResponse(response1); assertThat(extractor.hasNext(), is(true)); @@ -345,9 +337,9 @@ public void testResetScrollAfterShardFailure() throws IOException { TestDataExtractor extractor = new TestDataExtractor(1000L, 2000L); SearchResponse goodResponse = createSearchResponse( - Arrays.asList(1100L, 1200L), - Arrays.asList("a1", "a2"), - Arrays.asList("b1", "b2") + Arrays.asList(1100L, 1200L), + Arrays.asList("a1", "a2"), + Arrays.asList("b1", "b2") ); extractor.setNextResponse(goodResponse); extractor.setNextResponseToError(new SearchPhaseExecutionException("search phase 1", "boom", ShardSearchFailure.EMPTY_ARRAY)); @@ -373,9 +365,9 @@ public void testResetScrollUsesLastResultTimestamp() throws IOException { TestDataExtractor extractor = new TestDataExtractor(1000L, 2000L); SearchResponse goodResponse = createSearchResponse( - Arrays.asList(1100L, 1200L), - Arrays.asList("a1", "a2"), - Arrays.asList("b1", "b2") + Arrays.asList(1100L, 1200L), + Arrays.asList("a1", "a2"), + Arrays.asList("b1", "b2") ); extractor.setNextResponse(goodResponse); @@ -394,15 +386,15 @@ public void testResetScrollUsesLastResultTimestamp() throws IOException { public void testResetScrollAfterSearchPhaseExecutionException() throws IOException { TestDataExtractor extractor = new TestDataExtractor(1000L, 2000L); SearchResponse firstResponse = createSearchResponse( - Arrays.asList(1100L, 1200L), - Arrays.asList("a1", "a2"), - Arrays.asList("b1", "b2") + Arrays.asList(1100L, 1200L), + Arrays.asList("a1", "a2"), + Arrays.asList("b1", "b2") ); SearchResponse secondResponse = createSearchResponse( - Arrays.asList(1300L, 1400L), - Arrays.asList("a1", "a2"), - Arrays.asList("b1", "b2") + Arrays.asList(1300L, 1400L), + Arrays.asList("a1", "a2"), + Arrays.asList("b1", "b2") ); extractor.setNextResponse(firstResponse); @@ -410,7 +402,6 @@ public void testResetScrollAfterSearchPhaseExecutionException() throws IOExcepti extractor.setNextResponse(secondResponse); extractor.setNextResponseToError(new SearchPhaseExecutionException("search phase 1", "boom", ShardSearchFailure.EMPTY_ARRAY)); - // first response is good assertThat(extractor.hasNext(), is(true)); Optional output = extractor.next(); @@ -440,23 +431,31 @@ public void testSearchPhaseExecutionExceptionOnInitScroll() { public void testDomainSplitScriptField() throws IOException { - SearchSourceBuilder.ScriptField withoutSplit = new SearchSourceBuilder.ScriptField( - "script1", mockScript("return 1+1;"), false); + SearchSourceBuilder.ScriptField withoutSplit = new SearchSourceBuilder.ScriptField("script1", mockScript("return 1+1;"), false); SearchSourceBuilder.ScriptField withSplit = new SearchSourceBuilder.ScriptField( - "script2", new Script(ScriptType.INLINE, "painless", "return domainSplit('foo.com', params);", emptyMap()), false); + "script2", + new Script(ScriptType.INLINE, "painless", "return domainSplit('foo.com', params);", emptyMap()), + false + ); List sFields = Arrays.asList(withoutSplit, withSplit); - ScrollDataExtractorContext context = new ScrollDataExtractorContext(jobId, extractedFields, indices, - query, sFields, scrollSize, 1000, 2000, Collections.emptyMap(), SearchRequest.DEFAULT_INDICES_OPTIONS, - Collections.emptyMap()); + ScrollDataExtractorContext context = new ScrollDataExtractorContext( + jobId, + extractedFields, + indices, + query, + sFields, + scrollSize, + 1000, + 2000, + Collections.emptyMap(), + SearchRequest.DEFAULT_INDICES_OPTIONS, + Collections.emptyMap() + ); TestDataExtractor extractor = new TestDataExtractor(context); - SearchResponse response1 = createSearchResponse( - Arrays.asList(1100L, 1200L), - Arrays.asList("a1", "a2"), - Arrays.asList("b1", "b2") - ); + SearchResponse response1 = createSearchResponse(Arrays.asList(1100L, 1200L), Arrays.asList("a1", "a2"), Arrays.asList("b1", "b2")); extractor.setNextResponse(response1); assertThat(extractor.hasNext(), is(true)); @@ -474,15 +473,22 @@ public void testDomainSplitScriptField() throws IOException { String searchRequest = capturedSearchRequests.get(0).toString().replaceAll("\\s", ""); assertThat(searchRequest, containsString("\"size\":1000")); - assertThat(searchRequest, containsString("\"query\":{\"bool\":{\"filter\":[{\"match_all\":{\"boost\":1.0}}," + - "{\"range\":{\"time\":{\"from\":1000,\"to\":2000,\"include_lower\":true,\"include_upper\":false," + - "\"format\":\"epoch_millis\",\"boost\":1.0}}}]")); + assertThat( + searchRequest, + containsString( + "\"query\":{\"bool\":{\"filter\":[{\"match_all\":{\"boost\":1.0}}," + + "{\"range\":{\"time\":{\"from\":1000,\"to\":2000,\"include_lower\":true,\"include_upper\":false," + + "\"format\":\"epoch_millis\",\"boost\":1.0}}}]" + ) + ); assertThat(searchRequest, containsString("\"sort\":[{\"time\":{\"order\":\"asc\"}}]")); assertThat(searchRequest, containsString("\"stored_fields\":\"_none_\"")); // Check for the scripts - assertThat(searchRequest, containsString("{\"script\":{\"source\":\"return 1 + 1;\",\"lang\":\"mockscript\"}" - .replaceAll("\\s", ""))); + assertThat( + searchRequest, + containsString("{\"script\":{\"source\":\"return 1 + 1;\",\"lang\":\"mockscript\"}".replaceAll("\\s", "")) + ); assertThat(capturedContinueScrollIds.size(), equalTo(1)); assertThat(capturedContinueScrollIds.get(0), equalTo(response1.getScrollId())); @@ -493,8 +499,19 @@ public void testDomainSplitScriptField() throws IOException { } private ScrollDataExtractorContext createContext(long start, long end) { - return new ScrollDataExtractorContext(jobId, extractedFields, indices, query, scriptFields, scrollSize, start, end, - Collections.emptyMap(), SearchRequest.DEFAULT_INDICES_OPTIONS, Collections.emptyMap()); + return new ScrollDataExtractorContext( + jobId, + extractedFields, + indices, + query, + scriptFields, + scrollSize, + start, + end, + Collections.emptyMap(), + SearchRequest.DEFAULT_INDICES_OPTIONS, + Collections.emptyMap() + ); } private SearchResponse createEmptySearchResponse() { @@ -514,8 +531,7 @@ private SearchResponse createSearchResponse(List timestamps, List SearchHit hit = new SearchHit(randomInt(), null, fields, null); hits.add(hit); } - SearchHits searchHits = new SearchHits(hits.toArray(new SearchHit[0]), - new TotalHits(hits.size(), TotalHits.Relation.EQUAL_TO), 1); + SearchHits searchHits = new SearchHits(hits.toArray(new SearchHit[0]), new TotalHits(hits.size(), TotalHits.Relation.EQUAL_TO), 1); when(searchResponse.getHits()).thenReturn(searchHits); when(searchResponse.getTook()).thenReturn(TimeValue.timeValueMillis(randomNonNegativeLong())); return searchResponse; diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/SearchHitToJsonProcessorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/SearchHitToJsonProcessorTests.java index b3ee08b9d2cd6..e47621bfecc1d 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/SearchHitToJsonProcessorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/SearchHitToJsonProcessorTests.java @@ -29,14 +29,15 @@ public void testProcessGivenSingleHit() throws IOException { ExtractedField missingField = new DocValueField("missing", Collections.singleton("float")); ExtractedField singleField = new DocValueField("single", Collections.singleton("keyword")); ExtractedField arrayField = new DocValueField("array", Collections.singleton("keyword")); - TimeBasedExtractedFields extractedFields = new TimeBasedExtractedFields(timeField, - Arrays.asList(timeField, missingField, singleField, arrayField)); + TimeBasedExtractedFields extractedFields = new TimeBasedExtractedFields( + timeField, + Arrays.asList(timeField, missingField, singleField, arrayField) + ); - SearchHit hit = new SearchHitBuilder(8) - .addField("time", 1000L) - .addField("single", "a") - .addField("array", Arrays.asList("b", "c")) - .build(); + SearchHit hit = new SearchHitBuilder(8).addField("time", 1000L) + .addField("single", "a") + .addField("array", Arrays.asList("b", "c")) + .build(); String json = searchHitToString(extractedFields, hit); @@ -48,25 +49,30 @@ public void testProcessGivenMultipleHits() throws IOException { ExtractedField missingField = new DocValueField("missing", Collections.singleton("float")); ExtractedField singleField = new DocValueField("single", Collections.singleton("keyword")); ExtractedField arrayField = new DocValueField("array", Collections.singleton("keyword")); - TimeBasedExtractedFields extractedFields = new TimeBasedExtractedFields(timeField, - Arrays.asList(timeField, missingField, singleField, arrayField)); + TimeBasedExtractedFields extractedFields = new TimeBasedExtractedFields( + timeField, + Arrays.asList(timeField, missingField, singleField, arrayField) + ); - SearchHit hit1 = new SearchHitBuilder(8) - .addField("time", 1000L) - .addField("single", "a1") - .addField("array", Arrays.asList("b1", "c1")) - .build(); + SearchHit hit1 = new SearchHitBuilder(8).addField("time", 1000L) + .addField("single", "a1") + .addField("array", Arrays.asList("b1", "c1")) + .build(); - SearchHit hit2 = new SearchHitBuilder(8) - .addField("time", 2000L) - .addField("single", "a2") - .addField("array", Arrays.asList("b2", "c2")) - .build(); + SearchHit hit2 = new SearchHitBuilder(8).addField("time", 2000L) + .addField("single", "a2") + .addField("array", Arrays.asList("b2", "c2")) + .build(); String json = searchHitToString(extractedFields, hit1, hit2); - assertThat(json, equalTo("{\"time\":1000,\"single\":\"a1\",\"array\":[\"b1\",\"c1\"]} " + - "{\"time\":2000,\"single\":\"a2\",\"array\":[\"b2\",\"c2\"]}")); + assertThat( + json, + equalTo( + "{\"time\":1000,\"single\":\"a1\",\"array\":[\"b1\",\"c1\"]} " + + "{\"time\":2000,\"single\":\"a2\",\"array\":[\"b2\",\"c2\"]}" + ) + ); } private String searchHitToString(ExtractedFields fields, SearchHit... searchHits) throws IOException { diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/TimeBasedExtractedFieldsTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/TimeBasedExtractedFieldsTests.java index 0d71a51cdd67e..a2264a4cf7a66 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/TimeBasedExtractedFieldsTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/TimeBasedExtractedFieldsTests.java @@ -47,8 +47,10 @@ public void testTimeFieldOnly() { assertThat(extractedFields.getAllFields(), equalTo(Arrays.asList(timeField))); assertThat(extractedFields.timeField(), equalTo("time")); - assertThat(extractedFields.getDocValueFields().stream().map(ExtractedField::getName).toArray(String[]::new), - equalTo(new String[] { timeField.getName() })); + assertThat( + extractedFields.getDocValueFields().stream().map(ExtractedField::getName).toArray(String[]::new), + equalTo(new String[] { timeField.getName() }) + ); assertThat(extractedFields.getSourceFields().length, equalTo(0)); } @@ -59,14 +61,18 @@ public void testAllTypesOfFields() { ExtractedField scriptField2 = new ScriptField("scripted2"); ExtractedField sourceField1 = new SourceField("src1", Collections.singleton("text")); ExtractedField sourceField2 = new SourceField("src2", Collections.singleton("text")); - TimeBasedExtractedFields extractedFields = new TimeBasedExtractedFields(timeField, Arrays.asList(timeField, - docValue1, docValue2, scriptField1, scriptField2, sourceField1, sourceField2)); + TimeBasedExtractedFields extractedFields = new TimeBasedExtractedFields( + timeField, + Arrays.asList(timeField, docValue1, docValue2, scriptField1, scriptField2, sourceField1, sourceField2) + ); assertThat(extractedFields.getAllFields().size(), equalTo(7)); assertThat(extractedFields.timeField(), equalTo("time")); - assertThat(extractedFields.getDocValueFields().stream().map(ExtractedField::getName).toArray(String[]::new), - equalTo(new String[] {"time", "doc1", "doc2"})); - assertThat(extractedFields.getSourceFields(), equalTo(new String[] {"src1", "src2"})); + assertThat( + extractedFields.getDocValueFields().stream().map(ExtractedField::getName).toArray(String[]::new), + equalTo(new String[] { "time", "doc1", "doc2" }) + ); + assertThat(extractedFields.getSourceFields(), equalTo(new String[] { "src1", "src2" })); } public void testStringTimeFieldValue() { @@ -132,8 +138,11 @@ public void testBuildGivenMixtureOfTypes() { when(fieldCapabilitiesResponse.getField("value")).thenReturn(valueCaps); when(fieldCapabilitiesResponse.getField("airline")).thenReturn(airlineCaps); - TimeBasedExtractedFields extractedFields = TimeBasedExtractedFields.build(jobBuilder.build(new Date()), datafeedBuilder.build(), - fieldCapabilitiesResponse); + TimeBasedExtractedFields extractedFields = TimeBasedExtractedFields.build( + jobBuilder.build(new Date()), + datafeedBuilder.build(), + fieldCapabilitiesResponse + ); assertThat(extractedFields.timeField(), equalTo("time")); assertThat(extractedFields.getDocValueFields().size(), equalTo(2)); @@ -170,8 +179,11 @@ public void testBuildGivenMultiFields() { when(fieldCapabilitiesResponse.getField("airport")).thenReturn(text); when(fieldCapabilitiesResponse.getField("airport.keyword")).thenReturn(keyword); - TimeBasedExtractedFields extractedFields = TimeBasedExtractedFields.build(jobBuilder.build(new Date()), datafeedBuilder.build(), - fieldCapabilitiesResponse); + TimeBasedExtractedFields extractedFields = TimeBasedExtractedFields.build( + jobBuilder.build(new Date()), + datafeedBuilder.build(), + fieldCapabilitiesResponse + ); assertThat(extractedFields.timeField(), equalTo("time")); assertThat(extractedFields.getDocValueFields().size(), equalTo(2)); @@ -200,8 +212,10 @@ public void testBuildGivenTimeFieldIsNotAggregatable() { FieldCapabilitiesResponse fieldCapabilitiesResponse = mock(FieldCapabilitiesResponse.class); when(fieldCapabilitiesResponse.getField("time")).thenReturn(timeCaps); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> TimeBasedExtractedFields.build(jobBuilder.build(new Date()), datafeedBuilder.build(), fieldCapabilitiesResponse)); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> TimeBasedExtractedFields.build(jobBuilder.build(new Date()), datafeedBuilder.build(), fieldCapabilitiesResponse) + ); assertThat(e.getMessage(), equalTo("cannot retrieve time field [time] because it is not aggregatable")); } @@ -220,8 +234,10 @@ public void testBuildGivenTimeFieldIsNotAggregatableInSomeIndices() { FieldCapabilitiesResponse fieldCapabilitiesResponse = mock(FieldCapabilitiesResponse.class); when(fieldCapabilitiesResponse.getField("time")).thenReturn(timeCaps); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> TimeBasedExtractedFields.build(jobBuilder.build(new Date()), datafeedBuilder.build(), fieldCapabilitiesResponse)); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> TimeBasedExtractedFields.build(jobBuilder.build(new Date()), datafeedBuilder.build(), fieldCapabilitiesResponse) + ); assertThat(e.getMessage(), equalTo("cannot retrieve time field [time] because it is not aggregatable")); } @@ -239,8 +255,10 @@ public void testBuildGivenFieldWithoutMappings() { FieldCapabilitiesResponse fieldCapabilitiesResponse = mock(FieldCapabilitiesResponse.class); when(fieldCapabilitiesResponse.getField("time")).thenReturn(timeCaps); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> TimeBasedExtractedFields.build(jobBuilder.build(new Date()), datafeedBuilder.build(), fieldCapabilitiesResponse)); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> TimeBasedExtractedFields.build(jobBuilder.build(new Date()), datafeedBuilder.build(), fieldCapabilitiesResponse) + ); assertThat(e.getMessage(), equalTo("cannot retrieve field [value] because it has no mappings")); } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/DataFrameAnalyticsTaskTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/DataFrameAnalyticsTaskTests.java index 7bf02eab6a0b0..d74b7e276d977 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/DataFrameAnalyticsTaskTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/DataFrameAnalyticsTaskTests.java @@ -16,12 +16,8 @@ import org.elasticsearch.client.Client; import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.DeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.json.JsonXContent; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.persistent.PersistentTasksService; import org.elasticsearch.persistent.UpdatePersistentTaskStatusAction; @@ -30,6 +26,10 @@ import org.elasticsearch.tasks.TaskManager; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.DeprecationHandler; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.ml.action.StartDataFrameAnalyticsAction; import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsState; import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsTaskState; @@ -68,10 +68,12 @@ public class DataFrameAnalyticsTaskTests extends ESTestCase { public void testDetermineStartingState_GivenZeroProgress() { - List progress = Arrays.asList(new PhaseProgress("reindexing", 0), + List progress = Arrays.asList( + new PhaseProgress("reindexing", 0), new PhaseProgress("loading_data", 0), new PhaseProgress("analyzing", 0), - new PhaseProgress("writing_results", 0)); + new PhaseProgress("writing_results", 0) + ); StartingState startingState = DataFrameAnalyticsTask.determineStartingState("foo", progress); @@ -79,10 +81,12 @@ public void testDetermineStartingState_GivenZeroProgress() { } public void testDetermineStartingState_GivenReindexingIsIncomplete() { - List progress = Arrays.asList(new PhaseProgress("reindexing", 99), + List progress = Arrays.asList( + new PhaseProgress("reindexing", 99), new PhaseProgress("loading_data", 0), new PhaseProgress("analyzing", 0), - new PhaseProgress("writing_results", 0)); + new PhaseProgress("writing_results", 0) + ); StartingState startingState = DataFrameAnalyticsTask.determineStartingState("foo", progress); @@ -90,10 +94,12 @@ public void testDetermineStartingState_GivenReindexingIsIncomplete() { } public void testDetermineStartingState_GivenLoadingDataIsIncomplete() { - List progress = Arrays.asList(new PhaseProgress("reindexing", 100), + List progress = Arrays.asList( + new PhaseProgress("reindexing", 100), new PhaseProgress("loading_data", 1), new PhaseProgress("analyzing", 0), - new PhaseProgress("writing_results", 0)); + new PhaseProgress("writing_results", 0) + ); StartingState startingState = DataFrameAnalyticsTask.determineStartingState("foo", progress); @@ -101,10 +107,12 @@ public void testDetermineStartingState_GivenLoadingDataIsIncomplete() { } public void testDetermineStartingState_GivenAnalyzingIsIncomplete() { - List progress = Arrays.asList(new PhaseProgress("reindexing", 100), + List progress = Arrays.asList( + new PhaseProgress("reindexing", 100), new PhaseProgress("loading_data", 100), new PhaseProgress("analyzing", 99), - new PhaseProgress("writing_results", 0)); + new PhaseProgress("writing_results", 0) + ); StartingState startingState = DataFrameAnalyticsTask.determineStartingState("foo", progress); @@ -112,10 +120,12 @@ public void testDetermineStartingState_GivenAnalyzingIsIncomplete() { } public void testDetermineStartingState_GivenWritingResultsIsIncomplete() { - List progress = Arrays.asList(new PhaseProgress("reindexing", 100), + List progress = Arrays.asList( + new PhaseProgress("reindexing", 100), new PhaseProgress("loading_data", 100), new PhaseProgress("analyzing", 100), - new PhaseProgress("writing_results", 1)); + new PhaseProgress("writing_results", 1) + ); StartingState startingState = DataFrameAnalyticsTask.determineStartingState("foo", progress); @@ -123,11 +133,13 @@ public void testDetermineStartingState_GivenWritingResultsIsIncomplete() { } public void testDetermineStartingState_GivenInferenceIsIncomplete() { - List progress = Arrays.asList(new PhaseProgress("reindexing", 100), + List progress = Arrays.asList( + new PhaseProgress("reindexing", 100), new PhaseProgress("loading_data", 100), new PhaseProgress("analyzing", 100), new PhaseProgress("writing_results", 100), - new PhaseProgress("inference", 40)); + new PhaseProgress("inference", 40) + ); StartingState startingState = DataFrameAnalyticsTask.determineStartingState("foo", progress); @@ -135,10 +147,12 @@ public void testDetermineStartingState_GivenInferenceIsIncomplete() { } public void testDetermineStartingState_GivenFinished() { - List progress = Arrays.asList(new PhaseProgress("reindexing", 100), + List progress = Arrays.asList( + new PhaseProgress("reindexing", 100), new PhaseProgress("loading_data", 100), new PhaseProgress("analyzing", 100), - new PhaseProgress("writing_results", 100)); + new PhaseProgress("writing_results", 100) + ); StartingState startingState = DataFrameAnalyticsTask.determineStartingState("foo", progress); @@ -164,10 +178,14 @@ private void testPersistProgress(SearchHits searchHits, String expectedIndexOrAl List progress = List.of( new PhaseProgress(ProgressTracker.REINDEXING, 100), new PhaseProgress(ProgressTracker.LOADING_DATA, 50), - new PhaseProgress(ProgressTracker.WRITING_RESULTS, 0)); + new PhaseProgress(ProgressTracker.WRITING_RESULTS, 0) + ); StartDataFrameAnalyticsAction.TaskParams taskParams = new StartDataFrameAnalyticsAction.TaskParams( - "task_id", Version.CURRENT, false); + "task_id", + Version.CURRENT, + false + ); SearchResponse searchResponse = mock(SearchResponse.class); when(searchResponse.getHits()).thenReturn(searchHits); @@ -180,9 +198,18 @@ private void testPersistProgress(SearchHits searchHits, String expectedIndexOrAl Runnable runnable = mock(Runnable.class); - DataFrameAnalyticsTask task = - new DataFrameAnalyticsTask( - 123, "type", "action", null, Map.of(), client, analyticsManager, auditor, taskParams, mock(XPackLicenseState.class)); + DataFrameAnalyticsTask task = new DataFrameAnalyticsTask( + 123, + "type", + "action", + null, + Map.of(), + client, + analyticsManager, + auditor, + taskParams, + mock(XPackLicenseState.class) + ); task.init(persistentTasksService, taskManager, "task-id", 42); task.setStatsHolder(new StatsHolder(progress, null, null, new DataCounts("test_job"))); @@ -201,8 +228,13 @@ private void testPersistProgress(SearchHits searchHits, String expectedIndexOrAl assertThat(indexRequest.isRequireAlias(), equalTo(".ml-state-write".equals(expectedIndexOrAlias))); assertThat(indexRequest.id(), equalTo("data_frame_analytics-task_id-progress")); - try (XContentParser parser = JsonXContent.jsonXContent.createParser( - NamedXContentRegistry.EMPTY, DeprecationHandler.IGNORE_DEPRECATIONS, indexRequest.source().utf8ToString())) { + try ( + XContentParser parser = JsonXContent.jsonXContent.createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.IGNORE_DEPRECATIONS, + indexRequest.source().utf8ToString() + ) + ) { StoredProgress parsedProgress = StoredProgress.PARSER.apply(parser, null); assertThat(parsedProgress.get(), equalTo(progress)); } @@ -214,8 +246,9 @@ public void testPersistProgress_ProgressDocumentCreated() throws IOException { public void testPersistProgress_ProgressDocumentUpdated() throws IOException { testPersistProgress( - new SearchHits(new SearchHit[]{ SearchHit.createFromMap(Map.of("_index", ".ml-state-dummy")) }, null, 0.0f), - ".ml-state-dummy"); + new SearchHits(new SearchHit[] { SearchHit.createFromMap(Map.of("_index", ".ml-state-dummy")) }, null, 0.0f), + ".ml-state-dummy" + ); } public void testSetFailed() throws IOException { @@ -242,13 +275,14 @@ private void testSetFailed(boolean nodeShuttingDown) throws IOException { List progress = List.of( new PhaseProgress(ProgressTracker.REINDEXING, 0), new PhaseProgress(ProgressTracker.LOADING_DATA, 100), - new PhaseProgress(ProgressTracker.WRITING_RESULTS, 30)); + new PhaseProgress(ProgressTracker.WRITING_RESULTS, 30) + ); - StartDataFrameAnalyticsAction.TaskParams taskParams = - new StartDataFrameAnalyticsAction.TaskParams( - "job-id", - Version.CURRENT, - false); + StartDataFrameAnalyticsAction.TaskParams taskParams = new StartDataFrameAnalyticsAction.TaskParams( + "job-id", + Version.CURRENT, + false + ); SearchResponse searchResponse = mock(SearchResponse.class); when(searchResponse.getHits()).thenReturn(SearchHits.empty()); @@ -257,9 +291,18 @@ private void testSetFailed(boolean nodeShuttingDown) throws IOException { IndexResponse indexResponse = mock(IndexResponse.class); doAnswer(withResponse(indexResponse)).when(client).execute(eq(IndexAction.INSTANCE), any(), any()); - DataFrameAnalyticsTask task = - new DataFrameAnalyticsTask( - 123, "type", "action", null, Map.of(), client, analyticsManager, auditor, taskParams, mock(XPackLicenseState.class)); + DataFrameAnalyticsTask task = new DataFrameAnalyticsTask( + 123, + "type", + "action", + null, + Map.of(), + client, + analyticsManager, + auditor, + taskParams, + mock(XPackLicenseState.class) + ); task.init(persistentTasksService, taskManager, "task-id", 42); task.setStatsHolder(new StatsHolder(progress, null, null, new DataCounts("test_job"))); task.setStep(new StubReindexingStep(task.getStatsHolder().getProgressTracker())); @@ -281,8 +324,13 @@ private void testSetFailed(boolean nodeShuttingDown) throws IOException { assertThat(indexRequest.index(), equalTo(AnomalyDetectorsIndex.jobStateIndexWriteAlias())); assertThat(indexRequest.id(), equalTo("data_frame_analytics-job-id-progress")); - try (XContentParser parser = JsonXContent.jsonXContent.createParser( - NamedXContentRegistry.EMPTY, DeprecationHandler.IGNORE_DEPRECATIONS, indexRequest.source().utf8ToString())) { + try ( + XContentParser parser = JsonXContent.jsonXContent.createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.IGNORE_DEPRECATIONS, + indexRequest.source().utf8ToString() + ) + ) { StoredProgress parsedProgress = StoredProgress.PARSER.apply(parser, null); assertThat(parsedProgress.get(), hasSize(3)); assertThat(parsedProgress.get().get(0), equalTo(new PhaseProgress("reindexing", 100))); @@ -290,9 +338,15 @@ private void testSetFailed(boolean nodeShuttingDown) throws IOException { verify(client).execute( same(UpdatePersistentTaskStatusAction.INSTANCE), - eq(new UpdatePersistentTaskStatusAction.Request( - "task-id", 42, new DataFrameAnalyticsTaskState(DataFrameAnalyticsState.FAILED, 42, "some exception"))), - any()); + eq( + new UpdatePersistentTaskStatusAction.Request( + "task-id", + 42, + new DataFrameAnalyticsTaskState(DataFrameAnalyticsState.FAILED, 42, "some exception") + ) + ), + any() + ); } verifyNoMoreInteractions(client, analyticsManager, auditor, taskManager); } @@ -320,12 +374,10 @@ public Name name() { } @Override - public void execute(ActionListener listener) { - } + public void execute(ActionListener listener) {} @Override - public void cancel(String reason, TimeValue timeout) { - } + public void cancel(String reason, TimeValue timeout) {} @Override public void updateProgress(ActionListener listener) { diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/DestinationIndexTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/DestinationIndexTests.java index 815a02539a88c..76dee669b066d 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/DestinationIndexTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/DestinationIndexTests.java @@ -32,10 +32,10 @@ import org.elasticsearch.common.collect.ImmutableOpenMap; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsConfig; import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsDest; import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsSource; @@ -78,7 +78,7 @@ public class DestinationIndexTests extends ESTestCase { private static final String ANALYTICS_ID = "some-analytics-id"; - private static final String[] SOURCE_INDEX = new String[] {"source-index"}; + private static final String[] SOURCE_INDEX = new String[] { "source-index" }; private static final String DEST_INDEX = "dest-index"; private static final String NUMERICAL_FIELD = "numerical-field"; private static final String OUTER_FIELD = "outer-field"; @@ -102,8 +102,8 @@ private Map testCreateDestinationIndex(DataFrameAnalysis analysi DataFrameAnalyticsConfig config = createConfig(analysis); ArgumentCaptor createIndexRequestCaptor = ArgumentCaptor.forClass(CreateIndexRequest.class); - doAnswer(callListenerOnResponse(null)) - .when(client).execute(eq(CreateIndexAction.INSTANCE), createIndexRequestCaptor.capture(), any()); + doAnswer(callListenerOnResponse(null)).when(client) + .execute(eq(CreateIndexAction.INSTANCE), createIndexRequestCaptor.capture(), any()); Settings index1Settings = Settings.builder() .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) @@ -127,19 +127,26 @@ private Map testCreateDestinationIndex(DataFrameAnalysis analysi GetSettingsResponse getSettingsResponse = new GetSettingsResponse(indexToSettings.build(), ImmutableOpenMap.of()); - doAnswer(callListenerOnResponse(getSettingsResponse)) - .when(client).execute(eq(GetSettingsAction.INSTANCE), getSettingsRequestCaptor.capture(), any()); + doAnswer(callListenerOnResponse(getSettingsResponse)).when(client) + .execute(eq(GetSettingsAction.INSTANCE), getSettingsRequestCaptor.capture(), any()); - Map indexMappings = + Map indexMappings = Map.of( + "properties", Map.of( - "properties", - Map.of( - "field_1", "field_1_mappings", - "field_2", "field_2_mappings", - NUMERICAL_FIELD, Map.of("type", "integer"), - OUTER_FIELD, Map.of("properties", Map.of(INNER_FIELD, Map.of("type", "integer"))), - ALIAS_TO_NUMERICAL_FIELD, Map.of("type", "alias", "path", NUMERICAL_FIELD), - ALIAS_TO_NESTED_FIELD, Map.of("type", "alias", "path", "outer-field.inner-field"))); + "field_1", + "field_1_mappings", + "field_2", + "field_2_mappings", + NUMERICAL_FIELD, + Map.of("type", "integer"), + OUTER_FIELD, + Map.of("properties", Map.of(INNER_FIELD, Map.of("type", "integer"))), + ALIAS_TO_NUMERICAL_FIELD, + Map.of("type", "alias", "path", NUMERICAL_FIELD), + ALIAS_TO_NESTED_FIELD, + Map.of("type", "alias", "path", "outer-field.inner-field") + ) + ); MappingMetadata index1MappingMetadata = new MappingMetadata("_doc", indexMappings); MappingMetadata index2MappingMetadata = new MappingMetadata("_doc", indexMappings); @@ -149,31 +156,22 @@ private Map testCreateDestinationIndex(DataFrameAnalysis analysi GetMappingsResponse getMappingsResponse = new GetMappingsResponse(mappings.build()); - doAnswer(callListenerOnResponse(getMappingsResponse)) - .when(client).execute(eq(GetMappingsAction.INSTANCE), getMappingsRequestCaptor.capture(), any()); + doAnswer(callListenerOnResponse(getMappingsResponse)).when(client) + .execute(eq(GetMappingsAction.INSTANCE), getMappingsRequestCaptor.capture(), any()); - FieldCapabilitiesResponse fieldCapabilitiesResponse = - new FieldCapabilitiesResponse( - new String[0], - new HashMap<>() {{ - put(NUMERICAL_FIELD, singletonMap("integer", createFieldCapabilities(NUMERICAL_FIELD, "integer"))); - put(OUTER_FIELD + "." + INNER_FIELD, singletonMap("integer", createFieldCapabilities(NUMERICAL_FIELD, "integer"))); - put(ALIAS_TO_NUMERICAL_FIELD, singletonMap("integer", createFieldCapabilities(NUMERICAL_FIELD, "integer"))); - put(ALIAS_TO_NESTED_FIELD, singletonMap("integer", createFieldCapabilities(NUMERICAL_FIELD, "integer"))); - }}); + FieldCapabilitiesResponse fieldCapabilitiesResponse = new FieldCapabilitiesResponse(new String[0], new HashMap<>() { + { + put(NUMERICAL_FIELD, singletonMap("integer", createFieldCapabilities(NUMERICAL_FIELD, "integer"))); + put(OUTER_FIELD + "." + INNER_FIELD, singletonMap("integer", createFieldCapabilities(NUMERICAL_FIELD, "integer"))); + put(ALIAS_TO_NUMERICAL_FIELD, singletonMap("integer", createFieldCapabilities(NUMERICAL_FIELD, "integer"))); + put(ALIAS_TO_NESTED_FIELD, singletonMap("integer", createFieldCapabilities(NUMERICAL_FIELD, "integer"))); + } + }); - doAnswer(callListenerOnResponse(fieldCapabilitiesResponse)) - .when(client).execute(eq(FieldCapabilitiesAction.INSTANCE), fieldCapabilitiesRequestCaptor.capture(), any()); + doAnswer(callListenerOnResponse(fieldCapabilitiesResponse)).when(client) + .execute(eq(FieldCapabilitiesAction.INSTANCE), fieldCapabilitiesRequestCaptor.capture(), any()); - DestinationIndex.createDestinationIndex( - client, - clock, - config, - ActionListener.wrap( - response -> {}, - e -> fail(e.getMessage()) - ) - ); + DestinationIndex.createDestinationIndex(client, clock, config, ActionListener.wrap(response -> {}, e -> fail(e.getMessage()))); GetSettingsRequest capturedGetSettingsRequest = getSettingsRequestCaptor.getValue(); assertThat(capturedGetSettingsRequest.indices(), equalTo(SOURCE_INDEX)); @@ -257,7 +255,8 @@ public void testCreateDestinationIndex_ResultsFieldsExistsInSourceIndex() { response -> fail("should not succeed"), e -> assertThat( e.getMessage(), - equalTo("A field that matches the dest.results_field [ml] already exists; please set a different results_field")) + equalTo("A field that matches the dest.results_field [ml] already exists; please set a different results_field") + ) ) ); } @@ -266,44 +265,49 @@ private Map testUpdateMappingsToDestIndex(DataFrameAnalysis anal DataFrameAnalyticsConfig config = createConfig(analysis); Map properties = Map.of( - NUMERICAL_FIELD, Map.of("type", "integer"), - OUTER_FIELD, Map.of("properties", Map.of(INNER_FIELD, Map.of("type", "integer"))), - ALIAS_TO_NUMERICAL_FIELD, Map.of("type", "alias", "path", NUMERICAL_FIELD), - ALIAS_TO_NESTED_FIELD, Map.of("type", "alias", "path", OUTER_FIELD + "." + INNER_FIELD) + NUMERICAL_FIELD, + Map.of("type", "integer"), + OUTER_FIELD, + Map.of("properties", Map.of(INNER_FIELD, Map.of("type", "integer"))), + ALIAS_TO_NUMERICAL_FIELD, + Map.of("type", "alias", "path", NUMERICAL_FIELD), + ALIAS_TO_NESTED_FIELD, + Map.of("type", "alias", "path", OUTER_FIELD + "." + INNER_FIELD) ); ImmutableOpenMap.Builder mappings = ImmutableOpenMap.builder(); mappings.put("", new MappingMetadata("_doc", Map.of("properties", properties))); - GetIndexResponse getIndexResponse = - new GetIndexResponse(new String[] { DEST_INDEX }, mappings.build(), ImmutableOpenMap.of(), ImmutableOpenMap.of(), - ImmutableOpenMap.of(), ImmutableOpenMap.of()); + GetIndexResponse getIndexResponse = new GetIndexResponse( + new String[] { DEST_INDEX }, + mappings.build(), + ImmutableOpenMap.of(), + ImmutableOpenMap.of(), + ImmutableOpenMap.of(), + ImmutableOpenMap.of() + ); ArgumentCaptor putMappingRequestCaptor = ArgumentCaptor.forClass(PutMappingRequest.class); ArgumentCaptor fieldCapabilitiesRequestCaptor = ArgumentCaptor.forClass(FieldCapabilitiesRequest.class); - doAnswer(callListenerOnResponse(AcknowledgedResponse.TRUE)) - .when(client).execute(eq(PutMappingAction.INSTANCE), putMappingRequestCaptor.capture(), any()); + doAnswer(callListenerOnResponse(AcknowledgedResponse.TRUE)).when(client) + .execute(eq(PutMappingAction.INSTANCE), putMappingRequestCaptor.capture(), any()); - FieldCapabilitiesResponse fieldCapabilitiesResponse = - new FieldCapabilitiesResponse( - new String[0], - new HashMap<>() {{ - put(NUMERICAL_FIELD, singletonMap("integer", createFieldCapabilities(NUMERICAL_FIELD, "integer"))); - put(OUTER_FIELD + "." + INNER_FIELD, singletonMap("integer", createFieldCapabilities(NUMERICAL_FIELD, "integer"))); - put(ALIAS_TO_NUMERICAL_FIELD, singletonMap("integer", createFieldCapabilities(NUMERICAL_FIELD, "integer"))); - put(ALIAS_TO_NESTED_FIELD, singletonMap("integer", createFieldCapabilities(NUMERICAL_FIELD, "integer"))); - }}); + FieldCapabilitiesResponse fieldCapabilitiesResponse = new FieldCapabilitiesResponse(new String[0], new HashMap<>() { + { + put(NUMERICAL_FIELD, singletonMap("integer", createFieldCapabilities(NUMERICAL_FIELD, "integer"))); + put(OUTER_FIELD + "." + INNER_FIELD, singletonMap("integer", createFieldCapabilities(NUMERICAL_FIELD, "integer"))); + put(ALIAS_TO_NUMERICAL_FIELD, singletonMap("integer", createFieldCapabilities(NUMERICAL_FIELD, "integer"))); + put(ALIAS_TO_NESTED_FIELD, singletonMap("integer", createFieldCapabilities(NUMERICAL_FIELD, "integer"))); + } + }); - doAnswer(callListenerOnResponse(fieldCapabilitiesResponse)) - .when(client).execute(eq(FieldCapabilitiesAction.INSTANCE), fieldCapabilitiesRequestCaptor.capture(), any()); + doAnswer(callListenerOnResponse(fieldCapabilitiesResponse)).when(client) + .execute(eq(FieldCapabilitiesAction.INSTANCE), fieldCapabilitiesRequestCaptor.capture(), any()); DestinationIndex.updateMappingsToDestIndex( client, config, getIndexResponse, - ActionListener.wrap( - response -> assertThat(response.isAcknowledged(), is(true)), - e -> fail(e.getMessage()) - ) + ActionListener.wrap(response -> assertThat(response.isAcknowledged(), is(true)), e -> fail(e.getMessage())) ); verify(client, atLeastOnce()).threadPool(); @@ -358,18 +362,23 @@ public void testUpdateMappingsToDestIndex_ResultsFieldsExistsInSourceIndex() { ImmutableOpenMap.Builder mappings = ImmutableOpenMap.builder(); mappings.put("", new MappingMetadata("_doc", Map.of("properties", Map.of("ml", "some-mapping")))); - GetIndexResponse getIndexResponse = - new GetIndexResponse(new String[] { DEST_INDEX }, mappings.build(), ImmutableOpenMap.of(), ImmutableOpenMap.of(), - ImmutableOpenMap.of(), ImmutableOpenMap.of()); - - ElasticsearchStatusException e = - expectThrows( - ElasticsearchStatusException.class, - () -> DestinationIndex.updateMappingsToDestIndex( - client, config, getIndexResponse, ActionListener.wrap(Assert::fail))); + GetIndexResponse getIndexResponse = new GetIndexResponse( + new String[] { DEST_INDEX }, + mappings.build(), + ImmutableOpenMap.of(), + ImmutableOpenMap.of(), + ImmutableOpenMap.of(), + ImmutableOpenMap.of() + ); + + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> DestinationIndex.updateMappingsToDestIndex(client, config, getIndexResponse, ActionListener.wrap(Assert::fail)) + ); assertThat( e.getMessage(), - equalTo("A field that matches the dest.results_field [ml] already exists; please set a different results_field")); + equalTo("A field that matches the dest.results_field [ml] already exists; please set a different results_field") + ); verifyZeroInteractions(client); } @@ -461,8 +470,7 @@ private static Answer callListenerOnResponse(Response respo } private static DataFrameAnalyticsConfig createConfig(DataFrameAnalysis analysis) { - return new DataFrameAnalyticsConfig.Builder() - .setId(ANALYTICS_ID) + return new DataFrameAnalyticsConfig.Builder().setId(ANALYTICS_ID) .setSource(new DataFrameAnalyticsSource(SOURCE_INDEX, null, null, null)) .setDest(new DataFrameAnalyticsDest(DEST_INDEX, null)) .setAnalysis(analysis) diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/MappingsMergerTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/MappingsMergerTests.java index 5fec5cce2bbe0..41a6ea84b01ef 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/MappingsMergerTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/MappingsMergerTests.java @@ -59,22 +59,21 @@ public void testMergeMappings_GivenPropertyFieldWithDifferentMapping() { GetMappingsResponse getMappingsResponse = new GetMappingsResponse(mappings.build()); - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> MappingsMerger.mergeMappings(newSource(), getMappingsResponse)); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> MappingsMerger.mergeMappings(newSource(), getMappingsResponse) + ); assertThat(e.status(), equalTo(RestStatus.BAD_REQUEST)); - assertThat(e.getMessage(), - containsString("cannot merge [properties] mappings because of differences for field [field_1]; ")); + assertThat(e.getMessage(), containsString("cannot merge [properties] mappings because of differences for field [field_1]; ")); assertThat(e.getMessage(), containsString("mapped as [different_field_1_mappings] in index [index_2]")); assertThat(e.getMessage(), containsString("mapped as [field_1_mappings] in index [index_1]")); } public void testMergeMappings_GivenIndicesWithDifferentPropertiesButNoConflicts() { - Map index1Mappings = Map.of("properties", - Map.of("field_1", "field_1_mappings", "field_2", "field_2_mappings")); + Map index1Mappings = Map.of("properties", Map.of("field_1", "field_1_mappings", "field_2", "field_2_mappings")); MappingMetadata index1MappingMetadata = new MappingMetadata("_doc", index1Mappings); - Map index2Mappings = Map.of("properties", - Map.of("field_1", "field_1_mappings", "field_3", "field_3_mappings")); + Map index2Mappings = Map.of("properties", Map.of("field_1", "field_1_mappings", "field_3", "field_3_mappings")); MappingMetadata index2MappingMetadata = new MappingMetadata("_doc", index2Mappings); ImmutableOpenMap.Builder mappings = ImmutableOpenMap.builder(); @@ -132,22 +131,21 @@ public void testMergeMappings_GivenRuntimeFieldWithDifferentMapping() { GetMappingsResponse getMappingsResponse = new GetMappingsResponse(mappings.build()); - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> MappingsMerger.mergeMappings(newSource(), getMappingsResponse)); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> MappingsMerger.mergeMappings(newSource(), getMappingsResponse) + ); assertThat(e.status(), equalTo(RestStatus.BAD_REQUEST)); - assertThat(e.getMessage(), - containsString("cannot merge [runtime] mappings because of differences for field [field_1]; ")); + assertThat(e.getMessage(), containsString("cannot merge [runtime] mappings because of differences for field [field_1]; ")); assertThat(e.getMessage(), containsString("mapped as [different_field_1_mappings] in index [index_2]")); assertThat(e.getMessage(), containsString("mapped as [field_1_mappings] in index [index_1]")); } public void testMergeMappings_GivenIndicesWithDifferentRuntimeFieldsButNoConflicts() { - Map index1Mappings = Map.of("runtime", - Map.of("field_1", "field_1_mappings", "field_2", "field_2_mappings")); + Map index1Mappings = Map.of("runtime", Map.of("field_1", "field_1_mappings", "field_2", "field_2_mappings")); MappingMetadata index1MappingMetadata = new MappingMetadata("_doc", index1Mappings); - Map index2Mappings = Map.of("runtime", - Map.of("field_1", "field_1_mappings", "field_3", "field_3_mappings")); + Map index2Mappings = Map.of("runtime", Map.of("field_1", "field_1_mappings", "field_3", "field_3_mappings")); MappingMetadata index2MappingMetadata = new MappingMetadata("_doc", index2Mappings); ImmutableOpenMap.Builder mappings = ImmutableOpenMap.builder(); @@ -237,7 +235,9 @@ public void testMergeMappings_GivenSourceFiltering() { GetMappingsResponse getMappingsResponse = new GetMappingsResponse(mappings.build()); MappingMetadata mergedMappings = MappingsMerger.mergeMappings( - newSourceWithExcludes("field_1", "runtime_field_2"), getMappingsResponse); + newSourceWithExcludes("field_1", "runtime_field_2"), + getMappingsResponse + ); Map mappingsAsMap = mergedMappings.getSourceAsMap(); @@ -251,11 +251,10 @@ public void testMergeMappings_GivenSourceFiltering() { } private static DataFrameAnalyticsSource newSource() { - return new DataFrameAnalyticsSource(new String[] {"index"}, null, null, null); + return new DataFrameAnalyticsSource(new String[] { "index" }, null, null, null); } private static DataFrameAnalyticsSource newSourceWithExcludes(String... excludes) { - return new DataFrameAnalyticsSource(new String[] {"index"}, null, - new FetchSourceContext(true, null, excludes), null); + return new DataFrameAnalyticsSource(new String[] { "index" }, null, new FetchSourceContext(true, null, excludes), null); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/StoredProgressTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/StoredProgressTests.java index 53c4b13587d62..b72e2eb32716c 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/StoredProgressTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/StoredProgressTests.java @@ -6,8 +6,8 @@ */ package org.elasticsearch.xpack.ml.dataframe; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.utils.PhaseProgress; import java.io.IOException; @@ -44,7 +44,9 @@ public void testDocumentId() { public void testExtractJobIdFromDocId() { assertThat(StoredProgress.extractJobIdFromDocId("data_frame_analytics-foo-progress"), equalTo("foo")); - assertThat(StoredProgress.extractJobIdFromDocId("data_frame_analytics-data_frame_analytics-bar-progress-progress"), - equalTo("data_frame_analytics-bar-progress")); + assertThat( + StoredProgress.extractJobIdFromDocId("data_frame_analytics-data_frame_analytics-bar-progress-progress"), + equalTo("data_frame_analytics-bar-progress") + ); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/extractor/DataFrameDataExtractorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/extractor/DataFrameDataExtractorTests.java index a52051cda5f62..a8c96461d3c3c 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/extractor/DataFrameDataExtractorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/extractor/DataFrameDataExtractorTests.java @@ -11,11 +11,10 @@ import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.ShardSearchFailure; import org.elasticsearch.client.Client; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.core.Nullable; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.rest.RestStatus; @@ -23,6 +22,7 @@ import org.elasticsearch.search.SearchHits; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ml.dataframe.analyses.Classification; import org.elasticsearch.xpack.core.ml.dataframe.analyses.OutlierDetectionTests; import org.elasticsearch.xpack.core.ml.dataframe.analyses.Regression; @@ -84,11 +84,14 @@ public void setUpTests() { indices = Arrays.asList("index-1", "index-2"); query = QueryBuilders.matchAllQuery(); - extractedFields = new ExtractedFields(Arrays.asList( - new DocValueField("field_1", Collections.singleton("keyword")), - new DocValueField("field_2", Collections.singleton("keyword"))), + extractedFields = new ExtractedFields( + Arrays.asList( + new DocValueField("field_1", Collections.singleton("keyword")), + new DocValueField("field_2", Collections.singleton("keyword")) + ), Collections.emptyList(), - Collections.emptyMap()); + Collections.emptyMap() + ); scrollSize = 1000; headers = Collections.emptyMap(); @@ -117,16 +120,16 @@ public void testTwoPageExtraction() throws IOException { Optional> rows = dataExtractor.next(); assertThat(rows.isPresent(), is(true)); assertThat(rows.get().size(), equalTo(3)); - assertThat(rows.get().get(0).getValues(), equalTo(new String[] {"11", "21"})); - assertThat(rows.get().get(1).getValues(), equalTo(new String[] {"12", "22"})); - assertThat(rows.get().get(2).getValues(), equalTo(new String[] {"13", "23"})); + assertThat(rows.get().get(0).getValues(), equalTo(new String[] { "11", "21" })); + assertThat(rows.get().get(1).getValues(), equalTo(new String[] { "12", "22" })); + assertThat(rows.get().get(2).getValues(), equalTo(new String[] { "13", "23" })); assertThat(dataExtractor.hasNext(), is(true)); // Second batch rows = dataExtractor.next(); assertThat(rows.isPresent(), is(true)); assertThat(rows.get().size(), equalTo(1)); - assertThat(rows.get().get(0).getValues(), equalTo(new String[] {"31", "41"})); + assertThat(rows.get().get(0).getValues(), equalTo(new String[] { "31", "41" })); assertThat(dataExtractor.hasNext(), is(true)); // Third batch should return empty @@ -140,19 +143,34 @@ public void testTwoPageExtraction() throws IOException { assertThat(searchRequest, containsString("allowPartialSearchResults=false")); assertThat(searchRequest, containsString("indices=[index-1,index-2]")); assertThat(searchRequest, containsString("\"size\":1000")); - assertThat(searchRequest, containsString("\"query\":{\"bool\":{\"filter\":[{\"match_all\":{\"boost\":1.0}},{\"range\":" + - "{\"ml__incremental_id\":{\"from\":0,\"to\":1000,\"include_lower\":true,\"include_upper\":false,\"boost\":1.0}}}]")); + assertThat( + searchRequest, + containsString( + "\"query\":{\"bool\":{\"filter\":[{\"match_all\":{\"boost\":1.0}},{\"range\":" + + "{\"ml__incremental_id\":{\"from\":0,\"to\":1000,\"include_lower\":true,\"include_upper\":false,\"boost\":1.0}}}]" + ) + ); assertThat(searchRequest, containsString("\"docvalue_fields\":[{\"field\":\"field_1\"},{\"field\":\"field_2\"}]")); assertThat(searchRequest, containsString("\"_source\":{\"includes\":[],\"excludes\":[]}")); assertThat(searchRequest, containsString("\"sort\":[{\"ml__incremental_id\":{\"order\":\"asc\"}}]")); searchRequest = dataExtractor.capturedSearchRequests.get(1).request().toString().replaceAll("\\s", ""); - assertThat(searchRequest, containsString("\"query\":{\"bool\":{\"filter\":[{\"match_all\":{\"boost\":1.0}},{\"range\":" + - "{\"ml__incremental_id\":{\"from\":3,\"to\":1003,\"include_lower\":true,\"include_upper\":false,\"boost\":1.0}}}]")); + assertThat( + searchRequest, + containsString( + "\"query\":{\"bool\":{\"filter\":[{\"match_all\":{\"boost\":1.0}},{\"range\":" + + "{\"ml__incremental_id\":{\"from\":3,\"to\":1003,\"include_lower\":true,\"include_upper\":false,\"boost\":1.0}}}]" + ) + ); searchRequest = dataExtractor.capturedSearchRequests.get(2).request().toString().replaceAll("\\s", ""); - assertThat(searchRequest, containsString("\"query\":{\"bool\":{\"filter\":[{\"match_all\":{\"boost\":1.0}},{\"range\":" + - "{\"ml__incremental_id\":{\"from\":4,\"to\":1004,\"include_lower\":true,\"include_upper\":false,\"boost\":1.0}}}]")); + assertThat( + searchRequest, + containsString( + "\"query\":{\"bool\":{\"filter\":[{\"match_all\":{\"boost\":1.0}},{\"range\":" + + "{\"ml__incremental_id\":{\"from\":4,\"to\":1004,\"include_lower\":true,\"include_upper\":false,\"boost\":1.0}}}]" + ) + ); } public void testErrorOnSearchTwiceLeadsToFailure() { @@ -192,15 +210,15 @@ public void testRecoveryFromErrorOnSearch() throws IOException { Optional> rows = dataExtractor.next(); assertThat(rows.isPresent(), is(true)); assertThat(rows.get().size(), equalTo(2)); - assertThat(rows.get().get(0).getValues(), equalTo(new String[] {"11", "21"})); - assertThat(rows.get().get(1).getValues(), equalTo(new String[] {"12", "22"})); + assertThat(rows.get().get(0).getValues(), equalTo(new String[] { "11", "21" })); + assertThat(rows.get().get(1).getValues(), equalTo(new String[] { "12", "22" })); assertThat(dataExtractor.hasNext(), is(true)); // We get second batch as we retried after the error rows = dataExtractor.next(); assertThat(rows.isPresent(), is(true)); assertThat(rows.get().size(), equalTo(1)); - assertThat(rows.get().get(0).getValues(), equalTo(new String[] {"13", "23"})); + assertThat(rows.get().get(0).getValues(), equalTo(new String[] { "13", "23" })); assertThat(dataExtractor.hasNext(), is(true)); // Next batch should return empty @@ -214,28 +232,36 @@ public void testRecoveryFromErrorOnSearch() throws IOException { String searchRequest = dataExtractor.capturedSearchRequests.get(0).request().toString().replaceAll("\\s", ""); assertThat(searchRequest, containsString("\"query\":{\"bool\":{")); assertThat(searchRequest, containsString("{\"match_all\":{\"boost\":1.0}")); - assertThat(searchRequest, containsString( - "{\"range\":{\"ml__incremental_id\":{\"from\":0,\"to\":1000,\"include_lower\":true,\"include_upper\":false")); + assertThat( + searchRequest, + containsString("{\"range\":{\"ml__incremental_id\":{\"from\":0,\"to\":1000,\"include_lower\":true,\"include_upper\":false") + ); // Assert the second search continued from the latest successfully processed doc searchRequest = dataExtractor.capturedSearchRequests.get(1).request().toString().replaceAll("\\s", ""); assertThat(searchRequest, containsString("\"query\":{\"bool\":{")); assertThat(searchRequest, containsString("{\"match_all\":{\"boost\":1.0}")); - assertThat(searchRequest, containsString( - "{\"range\":{\"ml__incremental_id\":{\"from\":2,\"to\":1002,\"include_lower\":true,\"include_upper\":false")); + assertThat( + searchRequest, + containsString("{\"range\":{\"ml__incremental_id\":{\"from\":2,\"to\":1002,\"include_lower\":true,\"include_upper\":false") + ); // Assert the third search continued from the latest successfully processed doc searchRequest = dataExtractor.capturedSearchRequests.get(2).request().toString().replaceAll("\\s", ""); assertThat(searchRequest, containsString("\"query\":{\"bool\":{")); assertThat(searchRequest, containsString("{\"match_all\":{\"boost\":1.0}")); - assertThat(searchRequest, containsString( - "{\"range\":{\"ml__incremental_id\":{\"from\":2,\"to\":1002,\"include_lower\":true,\"include_upper\":false")); + assertThat( + searchRequest, + containsString("{\"range\":{\"ml__incremental_id\":{\"from\":2,\"to\":1002,\"include_lower\":true,\"include_upper\":false") + ); searchRequest = dataExtractor.capturedSearchRequests.get(3).request().toString().replaceAll("\\s", ""); assertThat(searchRequest, containsString("\"query\":{\"bool\":{")); assertThat(searchRequest, containsString("{\"match_all\":{\"boost\":1.0}")); - assertThat(searchRequest, containsString( - "{\"range\":{\"ml__incremental_id\":{\"from\":3,\"to\":1003,\"include_lower\":true,\"include_upper\":false")); + assertThat( + searchRequest, + containsString("{\"range\":{\"ml__incremental_id\":{\"from\":3,\"to\":1003,\"include_lower\":true,\"include_upper\":false") + ); } public void testIncludeSourceIsFalseAndNoSourceFields() throws IOException { @@ -250,7 +276,7 @@ public void testIncludeSourceIsFalseAndNoSourceFields() throws IOException { Optional> rows = dataExtractor.next(); assertThat(rows.isPresent(), is(true)); assertThat(rows.get().size(), equalTo(1)); - assertThat(rows.get().get(0).getValues(), equalTo(new String[] {"11", "21"})); + assertThat(rows.get().get(0).getValues(), equalTo(new String[] { "11", "21" })); assertThat(dataExtractor.hasNext(), is(true)); assertThat(dataExtractor.next().isEmpty(), is(true)); @@ -264,11 +290,14 @@ public void testIncludeSourceIsFalseAndNoSourceFields() throws IOException { public void testIncludeSourceIsFalseAndAtLeastOneSourceField() throws IOException { // Explicit cast of ExtractedField args necessary for Eclipse due to https://bugs.eclipse.org/bugs/show_bug.cgi?id=530915 - extractedFields = new ExtractedFields(Arrays.asList( - (ExtractedField) new DocValueField("field_1", Collections.singleton("keyword")), - (ExtractedField) new SourceField("field_2", Collections.singleton("text"))), + extractedFields = new ExtractedFields( + Arrays.asList( + (ExtractedField) new DocValueField("field_1", Collections.singleton("keyword")), + (ExtractedField) new SourceField("field_2", Collections.singleton("text")) + ), Collections.emptyList(), - Collections.emptyMap()); + Collections.emptyMap() + ); TestExtractor dataExtractor = createExtractor(false, false); @@ -281,7 +310,7 @@ public void testIncludeSourceIsFalseAndAtLeastOneSourceField() throws IOExceptio Optional> rows = dataExtractor.next(); assertThat(rows.isPresent(), is(true)); assertThat(rows.get().size(), equalTo(1)); - assertThat(rows.get().get(0).getValues(), equalTo(new String[] {"11", "21"})); + assertThat(rows.get().get(0).getValues(), equalTo(new String[] { "11", "21" })); assertThat(dataExtractor.hasNext(), is(true)); assertThat(dataExtractor.next().isEmpty(), is(true)); @@ -324,10 +353,14 @@ public void testCollectDataSummary_GivenAnalysisDoesNotSupportMissingFields() { assertThat(dataExtractor.capturedSearchRequests.size(), equalTo(1)); String searchRequest = dataExtractor.capturedSearchRequests.get(0).request().toString().replaceAll("\\s", ""); - assertThat(searchRequest, containsString( - "\"query\":{\"bool\":{\"filter\":[{\"match_all\":{\"boost\":1.0}},{\"bool\":{\"filter\":" + - "[{\"exists\":{\"field\":\"field_1\",\"boost\":1.0}},{\"exists\":{\"field\":\"field_2\",\"boost\":1.0}}]," + - "\"boost\":1.0}}],\"boost\":1.0}")); + assertThat( + searchRequest, + containsString( + "\"query\":{\"bool\":{\"filter\":[{\"match_all\":{\"boost\":1.0}},{\"bool\":{\"filter\":" + + "[{\"exists\":{\"field\":\"field_1\",\"boost\":1.0}},{\"exists\":{\"field\":\"field_2\",\"boost\":1.0}}]," + + "\"boost\":1.0}}],\"boost\":1.0}" + ) + ); } public void testMissingValues_GivenSupported() throws IOException { @@ -348,10 +381,10 @@ public void testMissingValues_GivenSupported() throws IOException { assertThat(rows.isPresent(), is(true)); assertThat(rows.get().size(), equalTo(3)); - assertThat(rows.get().get(0).getValues(), equalTo(new String[] {"11", "21"})); + assertThat(rows.get().get(0).getValues(), equalTo(new String[] { "11", "21" })); assertThat(rows.get().get(1).getValues()[0], equalTo(DataFrameDataExtractor.NULL_VALUE)); assertThat(rows.get().get(1).getValues()[1], equalTo("22")); - assertThat(rows.get().get(2).getValues(), equalTo(new String[] {"13", "23"})); + assertThat(rows.get().get(2).getValues(), equalTo(new String[] { "13", "23" })); assertThat(rows.get().get(0).shouldSkip(), is(false)); assertThat(rows.get().get(1).shouldSkip(), is(false)); @@ -383,9 +416,9 @@ public void testMissingValues_GivenNotSupported() throws IOException { assertThat(rows.isPresent(), is(true)); assertThat(rows.get().size(), equalTo(3)); - assertThat(rows.get().get(0).getValues(), equalTo(new String[] {"11", "21"})); + assertThat(rows.get().get(0).getValues(), equalTo(new String[] { "11", "21" })); assertThat(rows.get().get(1).getValues(), is(nullValue())); - assertThat(rows.get().get(2).getValues(), equalTo(new String[] {"13", "23"})); + assertThat(rows.get().get(2).getValues(), equalTo(new String[] { "13", "23" })); assertThat(rows.get().get(0).shouldSkip(), is(false)); assertThat(rows.get().get(1).shouldSkip(), is(true)); @@ -401,18 +434,21 @@ public void testMissingValues_GivenNotSupported() throws IOException { public void testGetCategoricalFields() { // Explicit cast of ExtractedField args necessary for Eclipse due to https://bugs.eclipse.org/bugs/show_bug.cgi?id=530915 - extractedFields = new ExtractedFields(Arrays.asList( - (ExtractedField) new DocValueField("field_boolean", Collections.singleton("boolean")), - (ExtractedField) new DocValueField("field_float", Collections.singleton("float")), - (ExtractedField) new DocValueField("field_double", Collections.singleton("double")), - (ExtractedField) new DocValueField("field_byte", Collections.singleton("byte")), - (ExtractedField) new DocValueField("field_short", Collections.singleton("short")), - (ExtractedField) new DocValueField("field_integer", Collections.singleton("integer")), - (ExtractedField) new DocValueField("field_long", Collections.singleton("long")), - (ExtractedField) new DocValueField("field_keyword", Collections.singleton("keyword")), - (ExtractedField) new SourceField("field_text", Collections.singleton("text"))), + extractedFields = new ExtractedFields( + Arrays.asList( + (ExtractedField) new DocValueField("field_boolean", Collections.singleton("boolean")), + (ExtractedField) new DocValueField("field_float", Collections.singleton("float")), + (ExtractedField) new DocValueField("field_double", Collections.singleton("double")), + (ExtractedField) new DocValueField("field_byte", Collections.singleton("byte")), + (ExtractedField) new DocValueField("field_short", Collections.singleton("short")), + (ExtractedField) new DocValueField("field_integer", Collections.singleton("integer")), + (ExtractedField) new DocValueField("field_long", Collections.singleton("long")), + (ExtractedField) new DocValueField("field_keyword", Collections.singleton("keyword")), + (ExtractedField) new SourceField("field_text", Collections.singleton("text")) + ), Collections.emptyList(), - Collections.emptyMap()); + Collections.emptyMap() + ); TestExtractor dataExtractor = createExtractor(true, true); assertThat(dataExtractor.getCategoricalFields(OutlierDetectionTests.createRandom()), empty()); @@ -423,65 +459,84 @@ public void testGetCategoricalFields() { assertThat( dataExtractor.getCategoricalFields(new Classification("field_keyword")), - containsInAnyOrder("field_keyword", "field_text")); + containsInAnyOrder("field_keyword", "field_text") + ); assertThat( dataExtractor.getCategoricalFields(new Classification("field_long")), - containsInAnyOrder("field_keyword", "field_text", "field_long")); + containsInAnyOrder("field_keyword", "field_text", "field_long") + ); assertThat( dataExtractor.getCategoricalFields(new Classification("field_boolean")), - containsInAnyOrder("field_keyword", "field_text", "field_boolean")); + containsInAnyOrder("field_keyword", "field_text", "field_boolean") + ); } public void testGetFieldNames_GivenProcessesFeatures() { // Explicit cast of ExtractedField args necessary for Eclipse due to https://bugs.eclipse.org/bugs/show_bug.cgi?id=530915 - extractedFields = new ExtractedFields(Arrays.asList( - (ExtractedField) new DocValueField("field_boolean", Collections.singleton("boolean")), - (ExtractedField) new DocValueField("field_float", Collections.singleton("float")), - (ExtractedField) new DocValueField("field_double", Collections.singleton("double")), - (ExtractedField) new DocValueField("field_byte", Collections.singleton("byte")), - (ExtractedField) new DocValueField("field_short", Collections.singleton("short")), - (ExtractedField) new DocValueField("field_integer", Collections.singleton("integer")), - (ExtractedField) new DocValueField("field_long", Collections.singleton("long")), - (ExtractedField) new DocValueField("field_keyword", Collections.singleton("keyword")), - (ExtractedField) new SourceField("field_text", Collections.singleton("text"))), + extractedFields = new ExtractedFields( + Arrays.asList( + (ExtractedField) new DocValueField("field_boolean", Collections.singleton("boolean")), + (ExtractedField) new DocValueField("field_float", Collections.singleton("float")), + (ExtractedField) new DocValueField("field_double", Collections.singleton("double")), + (ExtractedField) new DocValueField("field_byte", Collections.singleton("byte")), + (ExtractedField) new DocValueField("field_short", Collections.singleton("short")), + (ExtractedField) new DocValueField("field_integer", Collections.singleton("integer")), + (ExtractedField) new DocValueField("field_long", Collections.singleton("long")), + (ExtractedField) new DocValueField("field_keyword", Collections.singleton("keyword")), + (ExtractedField) new SourceField("field_text", Collections.singleton("text")) + ), Arrays.asList( new ProcessedField(new CategoricalPreProcessor("field_long", "animal")), buildProcessedField("field_short", "field_1", "field_2") ), - Collections.emptyMap()); + Collections.emptyMap() + ); TestExtractor dataExtractor = createExtractor(true, true); - assertThat(dataExtractor.getCategoricalFields(new Regression("field_double")), - containsInAnyOrder("field_keyword", "field_text", "animal")); + assertThat( + dataExtractor.getCategoricalFields(new Regression("field_double")), + containsInAnyOrder("field_keyword", "field_text", "animal") + ); List fieldNames = dataExtractor.getFieldNames(); - assertThat(fieldNames, containsInAnyOrder( - "animal", - "field_1", - "field_2", - "field_boolean", - "field_float", - "field_double", - "field_byte", - "field_integer", - "field_keyword", - "field_text")); + assertThat( + fieldNames, + containsInAnyOrder( + "animal", + "field_1", + "field_2", + "field_boolean", + "field_float", + "field_double", + "field_byte", + "field_integer", + "field_keyword", + "field_text" + ) + ); assertThat(dataExtractor.getFieldNames(), contains(fieldNames.toArray(String[]::new))); } public void testExtractionWithProcessedFeatures() throws IOException { - extractedFields = new ExtractedFields(Arrays.asList( - new DocValueField("field_1", Collections.singleton("keyword")), - new DocValueField("field_2", Collections.singleton("keyword"))), + extractedFields = new ExtractedFields( + Arrays.asList( + new DocValueField("field_1", Collections.singleton("keyword")), + new DocValueField("field_2", Collections.singleton("keyword")) + ), Arrays.asList( new ProcessedField(new CategoricalPreProcessor("field_1", "animal")), - new ProcessedField(new OneHotEncoding("field_1", - Arrays.asList("11", "12") - .stream() - .collect(Collectors.toMap(Function.identity(), s -> s.equals("11") ? "field_11" : "field_12")), - true)) + new ProcessedField( + new OneHotEncoding( + "field_1", + Arrays.asList("11", "12") + .stream() + .collect(Collectors.toMap(Function.identity(), s -> s.equals("11") ? "field_11" : "field_12")), + true + ) + ) ), - Collections.emptyMap()); + Collections.emptyMap() + ); TestExtractor dataExtractor = createExtractor(true, true); @@ -500,10 +555,12 @@ public void testExtractionWithProcessedFeatures() throws IOException { assertThat(rows.isPresent(), is(true)); assertThat(rows.get().size(), equalTo(3)); - assertThat(rows.get().get(0).getValues(), equalTo(new String[] {"21", "dog", "1", "0"})); - assertThat(rows.get().get(1).getValues(), - equalTo(new String[] {"22", "dog", DataFrameDataExtractor.NULL_VALUE, DataFrameDataExtractor.NULL_VALUE})); - assertThat(rows.get().get(2).getValues(), equalTo(new String[] {"23", "dog", "0", "0"})); + assertThat(rows.get().get(0).getValues(), equalTo(new String[] { "21", "dog", "1", "0" })); + assertThat( + rows.get().get(1).getValues(), + equalTo(new String[] { "22", "dog", DataFrameDataExtractor.NULL_VALUE, DataFrameDataExtractor.NULL_VALUE }) + ); + assertThat(rows.get().get(2).getValues(), equalTo(new String[] { "23", "dog", "0", "0" })); assertThat(rows.get().get(0).shouldSkip(), is(false)); assertThat(rows.get().get(1).shouldSkip(), is(false)); @@ -511,11 +568,14 @@ public void testExtractionWithProcessedFeatures() throws IOException { } public void testExtractionWithMultipleScalarTypesInSource() throws IOException { - extractedFields = new ExtractedFields(Arrays.asList( - new DocValueField("field_1", Collections.singleton("keyword")), - new DocValueField("field_2", Collections.singleton("keyword"))), + extractedFields = new ExtractedFields( + Arrays.asList( + new DocValueField("field_1", Collections.singleton("keyword")), + new DocValueField("field_2", Collections.singleton("keyword")) + ), Collections.emptyList(), - Collections.emptyMap()); + Collections.emptyMap() + ); TestExtractor dataExtractor = createExtractor(true, true); @@ -534,9 +594,9 @@ public void testExtractionWithMultipleScalarTypesInSource() throws IOException { assertThat(rows.isPresent(), is(true)); assertThat(rows.get().size(), equalTo(3)); - assertThat(rows.get().get(0).getValues(), equalTo(new String[] {"1", "21",})); - assertThat(rows.get().get(1).getValues(), equalTo(new String[] {"true", "22"})); - assertThat(rows.get().get(2).getValues(), equalTo(new String[] {"false", "23"})); + assertThat(rows.get().get(0).getValues(), equalTo(new String[] { "1", "21", })); + assertThat(rows.get().get(1).getValues(), equalTo(new String[] { "true", "22" })); + assertThat(rows.get().get(2).getValues(), equalTo(new String[] { "false", "23" })); assertThat(rows.get().get(0).shouldSkip(), is(false)); assertThat(rows.get().get(1).shouldSkip(), is(false)); @@ -547,11 +607,14 @@ public void testExtractionWithProcessedFieldThrows() { ProcessedField processedField = mock(ProcessedField.class); doThrow(new RuntimeException("process field error")).when(processedField).value(any(), any()); - extractedFields = new ExtractedFields(Arrays.asList( - new DocValueField("field_1", Collections.singleton("keyword")), - new DocValueField("field_2", Collections.singleton("keyword"))), + extractedFields = new ExtractedFields( + Arrays.asList( + new DocValueField("field_1", Collections.singleton("keyword")), + new DocValueField("field_2", Collections.singleton("keyword")) + ), Collections.singletonList(processedField), - Collections.emptyMap()); + Collections.emptyMap() + ); TestExtractor dataExtractor = createExtractor(true, true); @@ -564,8 +627,18 @@ public void testExtractionWithProcessedFieldThrows() { } private TestExtractor createExtractor(boolean includeSource, boolean supportsRowsWithMissingValues) { - DataFrameDataExtractorContext context = new DataFrameDataExtractorContext(JOB_ID, extractedFields, indices, query, scrollSize, - headers, includeSource, supportsRowsWithMissingValues, trainTestSplitterFactory, Collections.emptyMap()); + DataFrameDataExtractorContext context = new DataFrameDataExtractorContext( + JOB_ID, + extractedFields, + indices, + query, + scrollSize, + headers, + includeSource, + supportsRowsWithMissingValues, + trainTestSplitterFactory, + Collections.emptyMap() + ); return new TestExtractor(client, context); } @@ -574,9 +647,11 @@ private static ProcessedField buildProcessedField(String inputField, String... o } private static PreProcessor buildPreProcessor(String inputField, String... outputFields) { - return new OneHotEncoding(inputField, + return new OneHotEncoding( + inputField, Arrays.stream(outputFields).collect(Collectors.toMap((s) -> randomAlphaOfLength(10), Function.identity())), - true); + true + ); } private SearchResponse createSearchResponse(List field1Values, List field2Values) { @@ -608,7 +683,8 @@ private SearchResponse createResponseWithShardFailures() { SearchResponse searchResponse = mock(SearchResponse.class); when(searchResponse.status()).thenReturn(RestStatus.OK); when(searchResponse.getShardFailures()).thenReturn( - new ShardSearchFailure[] { new ShardSearchFailure(new RuntimeException("shard failed"))}); + new ShardSearchFailure[] { new ShardSearchFailure(new RuntimeException("shard failed")) } + ); when(searchResponse.getFailedShards()).thenReturn(1); when(searchResponse.getScrollId()).thenReturn(randomAlphaOfLength(1000)); return searchResponse; @@ -638,7 +714,7 @@ void setAlwaysResponse(SearchResponse searchResponse) { @Override protected SearchResponse executeSearchRequest(SearchRequestBuilder searchRequestBuilder) { capturedSearchRequests.add(searchRequestBuilder); - SearchResponse searchResponse = alwaysResponse == null ? responses.remove() : alwaysResponse; + SearchResponse searchResponse = alwaysResponse == null ? responses.remove() : alwaysResponse; if (searchResponse.getShardFailures() != null) { throw new RuntimeException(searchResponse.getShardFailures()[0].getCause()); } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/extractor/ExtractedFieldsDetectorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/extractor/ExtractedFieldsDetectorTests.java index 9fe27e3baf95d..74ddb6732acde 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/extractor/ExtractedFieldsDetectorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/extractor/ExtractedFieldsDetectorTests.java @@ -56,11 +56,15 @@ public class ExtractedFieldsDetectorTests extends ESTestCase { private FetchSourceContext analyzedFields; public void testDetect_GivenFloatField() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addAggregatableField("some_float", "float").build(); + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addAggregatableField("some_float", "float") + .build(); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildOutlierDetectionConfig(), 100, fieldCapabilities, Collections.emptyMap()); + buildOutlierDetectionConfig(), + 100, + fieldCapabilities, + Collections.emptyMap() + ); Tuple> fieldExtraction = extractedFieldsDetector.detect(); List allFields = fieldExtraction.v1().getAllFields(); @@ -68,17 +72,31 @@ public void testDetect_GivenFloatField() { assertThat(allFields.get(0).getName(), equalTo("some_float")); assertThat(allFields.get(0).getMethod(), equalTo(ExtractedField.Method.DOC_VALUE)); - assertFieldSelectionContains(fieldExtraction.v2(), - FieldSelection.included("some_float", Collections.singleton("float"), false, FieldSelection.FeatureType.NUMERICAL)); + assertFieldSelectionContains( + fieldExtraction.v2(), + FieldSelection.included("some_float", Collections.singleton("float"), false, FieldSelection.FeatureType.NUMERICAL) + ); } public void testDetect_GivenNumericFieldWithMultipleTypes() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addAggregatableField("some_number", "long", "integer", "short", "byte", "double", "float", "half_float", "scaled_float") - .build(); + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addAggregatableField( + "some_number", + "long", + "integer", + "short", + "byte", + "double", + "float", + "half_float", + "scaled_float" + ).build(); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildOutlierDetectionConfig(), 100, fieldCapabilities, Collections.emptyMap()); + buildOutlierDetectionConfig(), + 100, + fieldCapabilities, + Collections.emptyMap() + ); Tuple> fieldExtraction = extractedFieldsDetector.detect(); List allFields = fieldExtraction.v1().getAllFields(); @@ -86,74 +104,112 @@ public void testDetect_GivenNumericFieldWithMultipleTypes() { assertThat(allFields.get(0).getName(), equalTo("some_number")); assertThat(allFields.get(0).getMethod(), equalTo(ExtractedField.Method.DOC_VALUE)); - assertFieldSelectionContains(fieldExtraction.v2(), FieldSelection.included("some_number", - new HashSet<>(Arrays.asList("long", "integer", "short", "byte", "double", "float", "half_float", "scaled_float")), false, - FieldSelection.FeatureType.NUMERICAL)); + assertFieldSelectionContains( + fieldExtraction.v2(), + FieldSelection.included( + "some_number", + new HashSet<>(Arrays.asList("long", "integer", "short", "byte", "double", "float", "half_float", "scaled_float")), + false, + FieldSelection.FeatureType.NUMERICAL + ) + ); } public void testDetect_GivenOutlierDetectionAndNonNumericField() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addAggregatableField("some_keyword", "keyword").build(); + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addAggregatableField("some_keyword", "keyword") + .build(); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildOutlierDetectionConfig(), 100, fieldCapabilities, Collections.emptyMap()); + buildOutlierDetectionConfig(), + 100, + fieldCapabilities, + Collections.emptyMap() + ); Tuple> fieldExtraction = extractedFieldsDetector.detect(); assertThat(fieldExtraction.v1().getAllFields().isEmpty(), is(true)); assertThat(fieldExtraction.v2().size(), equalTo(1)); assertThat(fieldExtraction.v2().get(0).getName(), equalTo("some_keyword")); assertThat(fieldExtraction.v2().get(0).isIncluded(), is(false)); - assertThat(fieldExtraction.v2().get(0).getReason(), equalTo("unsupported type; supported types are " + - "[boolean, byte, double, float, half_float, integer, long, scaled_float, short, unsigned_long]")); + assertThat( + fieldExtraction.v2().get(0).getReason(), + equalTo( + "unsupported type; supported types are " + + "[boolean, byte, double, float, half_float, integer, long, scaled_float, short, unsigned_long]" + ) + ); } public void testDetect_GivenOutlierDetectionAndFieldWithNumericAndNonNumericTypes() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addAggregatableField("indecisive_field", "float", "keyword").build(); + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addAggregatableField( + "indecisive_field", + "float", + "keyword" + ).build(); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildOutlierDetectionConfig(), 100, fieldCapabilities, Collections.emptyMap()); + buildOutlierDetectionConfig(), + 100, + fieldCapabilities, + Collections.emptyMap() + ); Tuple> fieldExtraction = extractedFieldsDetector.detect(); assertThat(fieldExtraction.v1().getAllFields().isEmpty(), is(true)); assertThat(fieldExtraction.v2().size(), equalTo(1)); assertThat(fieldExtraction.v2().get(0).getName(), equalTo("indecisive_field")); assertThat(fieldExtraction.v2().get(0).isIncluded(), is(false)); - assertThat(fieldExtraction.v2().get(0).getReason(), equalTo("unsupported type; supported types are " + - "[boolean, byte, double, float, half_float, integer, long, scaled_float, short, unsigned_long]")); + assertThat( + fieldExtraction.v2().get(0).getReason(), + equalTo( + "unsupported type; supported types are " + + "[boolean, byte, double, float, half_float, integer, long, scaled_float, short, unsigned_long]" + ) + ); } public void testDetect_GivenOutlierDetectionAndMultipleFields() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addAggregatableField("some_float", "float") + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addAggregatableField("some_float", "float") .addAggregatableField("some_long", "long") .addAggregatableField("some_keyword", "keyword") .addAggregatableField("some_boolean", "boolean") .build(); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildOutlierDetectionConfig(), 100, fieldCapabilities, Collections.emptyMap()); + buildOutlierDetectionConfig(), + 100, + fieldCapabilities, + Collections.emptyMap() + ); Tuple> fieldExtraction = extractedFieldsDetector.detect(); List allFields = fieldExtraction.v1().getAllFields(); assertThat(allFields, hasSize(3)); - assertThat(allFields.stream().map(ExtractedField::getName).collect(Collectors.toSet()), - containsInAnyOrder("some_float", "some_long", "some_boolean")); - assertThat(allFields.stream().map(ExtractedField::getMethod).collect(Collectors.toSet()), - contains(equalTo(ExtractedField.Method.DOC_VALUE))); + assertThat( + allFields.stream().map(ExtractedField::getName).collect(Collectors.toSet()), + containsInAnyOrder("some_float", "some_long", "some_boolean") + ); + assertThat( + allFields.stream().map(ExtractedField::getMethod).collect(Collectors.toSet()), + contains(equalTo(ExtractedField.Method.DOC_VALUE)) + ); - assertFieldSelectionContains(fieldExtraction.v2(), + assertFieldSelectionContains( + fieldExtraction.v2(), FieldSelection.included("some_boolean", Collections.singleton("boolean"), false, FieldSelection.FeatureType.NUMERICAL), FieldSelection.included("some_float", Collections.singleton("float"), false, FieldSelection.FeatureType.NUMERICAL), - FieldSelection.excluded("some_keyword", Collections.singleton("keyword"), "unsupported type; " + - "supported types are [boolean, byte, double, float, half_float, integer, long, scaled_float, short, unsigned_long]"), + FieldSelection.excluded( + "some_keyword", + Collections.singleton("keyword"), + "unsupported type; " + + "supported types are [boolean, byte, double, float, half_float, integer, long, scaled_float, short, unsigned_long]" + ), FieldSelection.included("some_long", Collections.singleton("long"), false, FieldSelection.FeatureType.NUMERICAL) ); } public void testDetect_GivenRegressionAndMultipleFields() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addAggregatableField("some_float", "float") + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addAggregatableField("some_float", "float") .addAggregatableField("some_long", "long") .addAggregatableField("some_keyword", "keyword") .addAggregatableField("some_boolean", "boolean") @@ -161,17 +217,26 @@ public void testDetect_GivenRegressionAndMultipleFields() { .build(); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildRegressionConfig("foo"), 100, fieldCapabilities, Collections.emptyMap()); + buildRegressionConfig("foo"), + 100, + fieldCapabilities, + Collections.emptyMap() + ); Tuple> fieldExtraction = extractedFieldsDetector.detect(); List allFields = fieldExtraction.v1().getAllFields(); assertThat(allFields, hasSize(5)); - assertThat(allFields.stream().map(ExtractedField::getName).collect(Collectors.toList()), - containsInAnyOrder("foo", "some_float", "some_keyword", "some_long", "some_boolean")); - assertThat(allFields.stream().map(ExtractedField::getMethod).collect(Collectors.toSet()), - contains(equalTo(ExtractedField.Method.DOC_VALUE))); + assertThat( + allFields.stream().map(ExtractedField::getName).collect(Collectors.toList()), + containsInAnyOrder("foo", "some_float", "some_keyword", "some_long", "some_boolean") + ); + assertThat( + allFields.stream().map(ExtractedField::getMethod).collect(Collectors.toSet()), + contains(equalTo(ExtractedField.Method.DOC_VALUE)) + ); - assertFieldSelectionContains(fieldExtraction.v2(), + assertFieldSelectionContains( + fieldExtraction.v2(), FieldSelection.included("foo", Collections.singleton("double"), true, FieldSelection.FeatureType.NUMERICAL), FieldSelection.included("some_boolean", Collections.singleton("boolean"), false, FieldSelection.FeatureType.NUMERICAL), FieldSelection.included("some_float", Collections.singleton("float"), false, FieldSelection.FeatureType.NUMERICAL), @@ -181,145 +246,184 @@ public void testDetect_GivenRegressionAndMultipleFields() { } public void testDetect_GivenRegressionAndRequiredFieldMissing() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addAggregatableField("some_float", "float") + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addAggregatableField("some_float", "float") .addAggregatableField("some_long", "long") .addAggregatableField("some_keyword", "keyword") .build(); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildRegressionConfig("foo"), 100, fieldCapabilities, Collections.emptyMap()); + buildRegressionConfig("foo"), + 100, + fieldCapabilities, + Collections.emptyMap() + ); ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, extractedFieldsDetector::detect); assertThat(e.getMessage(), equalTo("required field [foo] is missing; analysis requires fields [foo]")); } public void testDetect_GivenRegressionAndRequiredFieldExcluded() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addAggregatableField("some_float", "float") + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addAggregatableField("some_float", "float") .addAggregatableField("some_long", "long") .addAggregatableField("some_keyword", "keyword") .addAggregatableField("foo", "float") .build(); - analyzedFields = new FetchSourceContext(true, new String[0], new String[] {"foo"}); + analyzedFields = new FetchSourceContext(true, new String[0], new String[] { "foo" }); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildRegressionConfig("foo"), 100, fieldCapabilities, Collections.emptyMap()); + buildRegressionConfig("foo"), + 100, + fieldCapabilities, + Collections.emptyMap() + ); ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, extractedFieldsDetector::detect); assertThat(e.getMessage(), equalTo("required field [foo] is missing; analysis requires fields [foo]")); } public void testDetect_GivenRegressionAndRequiredFieldNotIncluded() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addAggregatableField("some_float", "float") + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addAggregatableField("some_float", "float") .addAggregatableField("some_long", "long") .addAggregatableField("some_keyword", "keyword") .addAggregatableField("foo", "float") .build(); - analyzedFields = new FetchSourceContext(true, new String[] {"some_float", "some_keyword"}, new String[0]); + analyzedFields = new FetchSourceContext(true, new String[] { "some_float", "some_keyword" }, new String[0]); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildRegressionConfig("foo"), 100, fieldCapabilities, Collections.emptyMap()); + buildRegressionConfig("foo"), + 100, + fieldCapabilities, + Collections.emptyMap() + ); ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, extractedFieldsDetector::detect); assertThat(e.getMessage(), equalTo("required field [foo] is missing; analysis requires fields [foo]")); } public void testDetect_GivenFieldIsBothIncludedAndExcluded() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addAggregatableField("foo", "float") + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addAggregatableField("foo", "float") .addAggregatableField("bar", "float") .build(); - analyzedFields = new FetchSourceContext(true, new String[] {"foo", "bar"}, new String[] {"foo"}); + analyzedFields = new FetchSourceContext(true, new String[] { "foo", "bar" }, new String[] { "foo" }); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildOutlierDetectionConfig(), 100, fieldCapabilities, Collections.emptyMap()); + buildOutlierDetectionConfig(), + 100, + fieldCapabilities, + Collections.emptyMap() + ); Tuple> fieldExtraction = extractedFieldsDetector.detect(); List allFields = fieldExtraction.v1().getAllFields(); assertThat(allFields, hasSize(1)); assertThat(allFields.stream().map(ExtractedField::getName).collect(Collectors.toList()), contains("bar")); - assertFieldSelectionContains(fieldExtraction.v2(), + assertFieldSelectionContains( + fieldExtraction.v2(), FieldSelection.included("bar", Collections.singleton("float"), false, FieldSelection.FeatureType.NUMERICAL), FieldSelection.excluded("foo", Collections.singleton("float"), "field in excludes list") ); } public void testDetect_GivenFieldIsNotIncludedAndIsExcluded() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addAggregatableField("foo", "float") + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addAggregatableField("foo", "float") .addAggregatableField("bar", "float") .build(); - analyzedFields = new FetchSourceContext(true, new String[] {"foo"}, new String[] {"bar"}); + analyzedFields = new FetchSourceContext(true, new String[] { "foo" }, new String[] { "bar" }); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildOutlierDetectionConfig(), 100, fieldCapabilities, Collections.emptyMap()); + buildOutlierDetectionConfig(), + 100, + fieldCapabilities, + Collections.emptyMap() + ); Tuple> fieldExtraction = extractedFieldsDetector.detect(); List allFields = fieldExtraction.v1().getAllFields(); assertThat(allFields, hasSize(1)); assertThat(allFields.stream().map(ExtractedField::getName).collect(Collectors.toList()), contains("foo")); - assertFieldSelectionContains(fieldExtraction.v2(), + assertFieldSelectionContains( + fieldExtraction.v2(), FieldSelection.excluded("bar", Collections.singleton("float"), "field not in includes list"), FieldSelection.included("foo", Collections.singleton("float"), false, FieldSelection.FeatureType.NUMERICAL) ); } public void testDetect_GivenRegressionAndRequiredFieldHasInvalidType() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addAggregatableField("some_float", "float") + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addAggregatableField("some_float", "float") .addAggregatableField("some_long", "long") .addAggregatableField("some_keyword", "keyword") .addAggregatableField("foo", "keyword") .build(); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildRegressionConfig("foo"), 100, fieldCapabilities, Collections.emptyMap()); + buildRegressionConfig("foo"), + 100, + fieldCapabilities, + Collections.emptyMap() + ); ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, extractedFieldsDetector::detect); - assertThat(e.getMessage(), equalTo("invalid types [keyword] for required field [foo]; " + - "expected types are [byte, double, float, half_float, integer, long, scaled_float, short, unsigned_long]")); + assertThat( + e.getMessage(), + equalTo( + "invalid types [keyword] for required field [foo]; " + + "expected types are [byte, double, float, half_float, integer, long, scaled_float, short, unsigned_long]" + ) + ); } public void testDetect_GivenClassificationAndRequiredFieldHasInvalidType() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addAggregatableField("some_float", "float") + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addAggregatableField("some_float", "float") .addAggregatableField("some_long", "long") .addAggregatableField("some_keyword", "keyword") .addAggregatableField("foo", "keyword") .build(); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildClassificationConfig("some_float"), 100, fieldCapabilities, Collections.emptyMap()); + buildClassificationConfig("some_float"), + 100, + fieldCapabilities, + Collections.emptyMap() + ); ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, extractedFieldsDetector::detect); - assertThat(e.getMessage(), equalTo("invalid types [float] for required field [some_float]; " + - "expected types are [boolean, byte, integer, ip, keyword, long, short, text, unsigned_long]")); + assertThat( + e.getMessage(), + equalTo( + "invalid types [float] for required field [some_float]; " + + "expected types are [boolean, byte, integer, ip, keyword, long, short, text, unsigned_long]" + ) + ); } public void testDetect_GivenClassificationAndDependentVariableHasInvalidCardinality() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addAggregatableField("some_long", "long") + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addAggregatableField("some_long", "long") .addAggregatableField("some_keyword", "keyword") .addAggregatableField("foo", "keyword") .build(); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildClassificationConfig("some_keyword"), 100, fieldCapabilities, Collections.singletonMap("some_keyword", 31L)); + buildClassificationConfig("some_keyword"), + 100, + fieldCapabilities, + Collections.singletonMap("some_keyword", 31L) + ); ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, extractedFieldsDetector::detect); assertThat(e.getMessage(), equalTo("Field [some_keyword] must have at most [30] distinct values but there were at least [31]")); } public void testDetect_GivenIgnoredField() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addField("_id", true, true, "float").build(); + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addField("_id", true, true, "float").build(); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildOutlierDetectionConfig(), 100, fieldCapabilities, Collections.emptyMap()); + buildOutlierDetectionConfig(), + 100, + fieldCapabilities, + Collections.emptyMap() + ); Tuple> fieldExtraction = extractedFieldsDetector.detect(); assertThat(fieldExtraction.v1().getAllFields().isEmpty(), is(true)); @@ -327,40 +431,47 @@ public void testDetect_GivenIgnoredField() { } public void testDetect_GivenIncludedIgnoredField() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addField("_id", true, false, "float") - .build(); - analyzedFields = new FetchSourceContext(true, new String[]{"_id"}, new String[0]); + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addField("_id", true, false, "float").build(); + analyzedFields = new FetchSourceContext(true, new String[] { "_id" }, new String[0]); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildOutlierDetectionConfig(), 100, fieldCapabilities, Collections.emptyMap()); + buildOutlierDetectionConfig(), + 100, + fieldCapabilities, + Collections.emptyMap() + ); ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, extractedFieldsDetector::detect); assertThat(e.getMessage(), equalTo("No field [_id] could be detected")); } public void testDetect_GivenExcludedFieldIsMissing() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addAggregatableField("foo", "float") - .build(); - analyzedFields = new FetchSourceContext(true, new String[]{"*"}, new String[] {"bar"}); + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addAggregatableField("foo", "float").build(); + analyzedFields = new FetchSourceContext(true, new String[] { "*" }, new String[] { "bar" }); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildOutlierDetectionConfig(), 100, fieldCapabilities, Collections.emptyMap()); + buildOutlierDetectionConfig(), + 100, + fieldCapabilities, + Collections.emptyMap() + ); ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, extractedFieldsDetector::detect); assertThat(e.getMessage(), equalTo("No field [bar] could be detected")); } public void testDetect_GivenExcludedFieldIsUnsupported() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addAggregatableField("numeric", "float") + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addAggregatableField("numeric", "float") .addAggregatableField("categorical", "keyword") .build(); - analyzedFields = new FetchSourceContext(true, null, new String[] {"categorical"}); + analyzedFields = new FetchSourceContext(true, null, new String[] { "categorical" }); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildOutlierDetectionConfig(), 100, fieldCapabilities, Collections.emptyMap()); + buildOutlierDetectionConfig(), + 100, + fieldCapabilities, + Collections.emptyMap() + ); Tuple> fieldExtraction = extractedFieldsDetector.detect(); @@ -368,10 +479,14 @@ public void testDetect_GivenExcludedFieldIsUnsupported() { assertThat(allFields, hasSize(1)); assertThat(allFields.get(0).getName(), equalTo("numeric")); - assertFieldSelectionContains(fieldExtraction.v2(), - FieldSelection.excluded("categorical", Collections.singleton("keyword"), - "unsupported type; supported types are " + - "[boolean, byte, double, float, half_float, integer, long, scaled_float, short, unsigned_long]"), + assertFieldSelectionContains( + fieldExtraction.v2(), + FieldSelection.excluded( + "categorical", + Collections.singleton("keyword"), + "unsupported type; supported types are " + + "[boolean, byte, double, float, half_float, integer, long, scaled_float, short, unsigned_long]" + ), FieldSelection.included("numeric", Collections.singleton("float"), false, FieldSelection.FeatureType.NUMERICAL) ); } @@ -392,39 +507,52 @@ public void testDetect_ShouldSortFieldsAlphabetically() { FieldCapabilitiesResponse fieldCapabilities = mockFieldCapsResponseBuilder.build(); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildOutlierDetectionConfig(), 100, fieldCapabilities, Collections.emptyMap()); + buildOutlierDetectionConfig(), + 100, + fieldCapabilities, + Collections.emptyMap() + ); Tuple> fieldExtraction = extractedFieldsDetector.detect(); - List extractedFieldNames = fieldExtraction.v1().getAllFields().stream().map(ExtractedField::getName) + List extractedFieldNames = fieldExtraction.v1() + .getAllFields() + .stream() + .map(ExtractedField::getName) .collect(Collectors.toList()); assertThat(extractedFieldNames, equalTo(sortedFields)); } public void testDetect_GivenIncludeWithMissingField() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addAggregatableField("my_field1", "float") + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addAggregatableField("my_field1", "float") .addAggregatableField("my_field2", "float") .build(); - analyzedFields = new FetchSourceContext(true, new String[]{"your_field1", "my*"}, new String[0]); + analyzedFields = new FetchSourceContext(true, new String[] { "your_field1", "my*" }, new String[0]); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildOutlierDetectionConfig(), 100, fieldCapabilities, Collections.emptyMap()); + buildOutlierDetectionConfig(), + 100, + fieldCapabilities, + Collections.emptyMap() + ); ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, extractedFieldsDetector::detect); assertThat(e.getMessage(), equalTo("No field [your_field1] could be detected")); } public void testDetect_GivenExcludeAllValidFields() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addAggregatableField("my_field1", "float") + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addAggregatableField("my_field1", "float") .addAggregatableField("my_field2", "float") .build(); - analyzedFields = new FetchSourceContext(true, new String[0], new String[]{"my_*"}); + analyzedFields = new FetchSourceContext(true, new String[0], new String[] { "my_*" }); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildOutlierDetectionConfig(), 100, fieldCapabilities, Collections.emptyMap()); + buildOutlierDetectionConfig(), + 100, + fieldCapabilities, + Collections.emptyMap() + ); Tuple> fieldExtraction = extractedFieldsDetector.detect(); assertThat(fieldExtraction.v1().getAllFields().isEmpty(), is(true)); @@ -433,23 +561,30 @@ public void testDetect_GivenExcludeAllValidFields() { } public void testDetect_GivenInclusionsAndExclusions() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addAggregatableField("my_field1_nope", "float") + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addAggregatableField("my_field1_nope", "float") .addAggregatableField("my_field1", "float") .addAggregatableField("your_field2", "float") .build(); - analyzedFields = new FetchSourceContext(true, new String[]{"your*", "my_*"}, new String[]{"*nope"}); + analyzedFields = new FetchSourceContext(true, new String[] { "your*", "my_*" }, new String[] { "*nope" }); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildOutlierDetectionConfig(), 100, fieldCapabilities, Collections.emptyMap()); + buildOutlierDetectionConfig(), + 100, + fieldCapabilities, + Collections.emptyMap() + ); Tuple> fieldExtraction = extractedFieldsDetector.detect(); - List extractedFieldNames = fieldExtraction.v1().getAllFields().stream().map(ExtractedField::getName) + List extractedFieldNames = fieldExtraction.v1() + .getAllFields() + .stream() + .map(ExtractedField::getName) .collect(Collectors.toList()); assertThat(extractedFieldNames, equalTo(Arrays.asList("my_field1", "your_field2"))); - assertFieldSelectionContains(fieldExtraction.v2(), + assertFieldSelectionContains( + fieldExtraction.v2(), FieldSelection.included("my_field1", Collections.singleton("float"), false, FieldSelection.FeatureType.NUMERICAL), FieldSelection.excluded("my_field1_nope", Collections.singleton("float"), "field in excludes list"), FieldSelection.included("your_field2", Collections.singleton("float"), false, FieldSelection.FeatureType.NUMERICAL) @@ -457,32 +592,43 @@ public void testDetect_GivenInclusionsAndExclusions() { } public void testDetect_GivenIncludedFieldHasUnsupportedType() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addAggregatableField("my_field1_nope", "float") + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addAggregatableField("my_field1_nope", "float") .addAggregatableField("my_field1", "float") .addAggregatableField("your_field2", "float") .addAggregatableField("your_keyword", "keyword") .build(); - analyzedFields = new FetchSourceContext(true, new String[]{"your*", "my_*"}, new String[]{"*nope"}); + analyzedFields = new FetchSourceContext(true, new String[] { "your*", "my_*" }, new String[] { "*nope" }); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildOutlierDetectionConfig(), 100, fieldCapabilities, Collections.emptyMap()); + buildOutlierDetectionConfig(), + 100, + fieldCapabilities, + Collections.emptyMap() + ); ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, extractedFieldsDetector::detect); - assertThat(e.getMessage(), equalTo("field [your_keyword] has unsupported type [keyword]. " + - "Supported types are [boolean, byte, double, float, half_float, integer, long, scaled_float, short, unsigned_long].")); + assertThat( + e.getMessage(), + equalTo( + "field [your_keyword] has unsupported type [keyword]. " + + "Supported types are [boolean, byte, double, float, half_float, integer, long, scaled_float, short, unsigned_long]." + ) + ); } public void testDetect_GivenNotIncludedFieldHasUnsupportedType() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addAggregatableField("numeric", "float") + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addAggregatableField("numeric", "float") .addAggregatableField("categorical", "keyword") .build(); - analyzedFields = new FetchSourceContext(true, new String[] {"numeric"}, null); + analyzedFields = new FetchSourceContext(true, new String[] { "numeric" }, null); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildOutlierDetectionConfig(), 100, fieldCapabilities, Collections.emptyMap()); + buildOutlierDetectionConfig(), + 100, + fieldCapabilities, + Collections.emptyMap() + ); Tuple> fieldExtraction = extractedFieldsDetector.detect(); @@ -490,114 +636,161 @@ public void testDetect_GivenNotIncludedFieldHasUnsupportedType() { assertThat(allFields, hasSize(1)); assertThat(allFields.get(0).getName(), equalTo("numeric")); - assertFieldSelectionContains(fieldExtraction.v2(), - FieldSelection.excluded("categorical", Collections.singleton("keyword"), - "unsupported type; supported types are " + - "[boolean, byte, double, float, half_float, integer, long, scaled_float, short, unsigned_long]"), + assertFieldSelectionContains( + fieldExtraction.v2(), + FieldSelection.excluded( + "categorical", + Collections.singleton("keyword"), + "unsupported type; supported types are " + + "[boolean, byte, double, float, half_float, integer, long, scaled_float, short, unsigned_long]" + ), FieldSelection.included("numeric", Collections.singleton("float"), false, FieldSelection.FeatureType.NUMERICAL) ); } public void testDetect_GivenIndexContainsResultsField() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addAggregatableField(RESULTS_FIELD + ".outlier_score", "float") + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addAggregatableField( + RESULTS_FIELD + ".outlier_score", + "float" + ) .addAggregatableField("my_field1", "float") .addAggregatableField("your_field2", "float") .addAggregatableField("your_keyword", "keyword") .build(); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildOutlierDetectionConfig(), 100, fieldCapabilities, Collections.emptyMap()); + buildOutlierDetectionConfig(), + 100, + fieldCapabilities, + Collections.emptyMap() + ); Tuple> fieldExtraction = extractedFieldsDetector.detect(); - List extractedFieldNames = fieldExtraction.v1().getAllFields().stream().map(ExtractedField::getName) + List extractedFieldNames = fieldExtraction.v1() + .getAllFields() + .stream() + .map(ExtractedField::getName) .collect(Collectors.toList()); assertThat(extractedFieldNames, equalTo(Arrays.asList("my_field1", "your_field2"))); - assertFieldSelectionContains(fieldExtraction.v2(), + assertFieldSelectionContains( + fieldExtraction.v2(), FieldSelection.included("my_field1", Collections.singleton("float"), false, FieldSelection.FeatureType.NUMERICAL), FieldSelection.included("your_field2", Collections.singleton("float"), false, FieldSelection.FeatureType.NUMERICAL), - FieldSelection.excluded("your_keyword", Collections.singleton("keyword"), "unsupported type; supported types " + - "are [boolean, byte, double, float, half_float, integer, long, scaled_float, short, unsigned_long]") + FieldSelection.excluded( + "your_keyword", + Collections.singleton("keyword"), + "unsupported type; supported types " + + "are [boolean, byte, double, float, half_float, integer, long, scaled_float, short, unsigned_long]" + ) ); } public void testDetect_GivenIncludedResultsField() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addAggregatableField(RESULTS_FIELD + ".outlier_score", "float") + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addAggregatableField( + RESULTS_FIELD + ".outlier_score", + "float" + ) .addAggregatableField("my_field1", "float") .addAggregatableField("your_field2", "float") .addAggregatableField("your_keyword", "keyword") .build(); - analyzedFields = new FetchSourceContext(true, new String[]{RESULTS_FIELD}, new String[0]); + analyzedFields = new FetchSourceContext(true, new String[] { RESULTS_FIELD }, new String[0]); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildOutlierDetectionConfig(), 100, fieldCapabilities, Collections.emptyMap()); + buildOutlierDetectionConfig(), + 100, + fieldCapabilities, + Collections.emptyMap() + ); ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, extractedFieldsDetector::detect); assertThat(e.getMessage(), equalTo("No field [ml] could be detected")); } public void testDetect_GivenLessFieldsThanDocValuesLimit() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addAggregatableField("field_1", "float") + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addAggregatableField("field_1", "float") .addAggregatableField("field_2", "float") .addAggregatableField("field_3", "float") .addAggregatableField("a_keyword", "keyword") .build(); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildOutlierDetectionConfig(), 4, fieldCapabilities, Collections.emptyMap()); + buildOutlierDetectionConfig(), + 4, + fieldCapabilities, + Collections.emptyMap() + ); Tuple> fieldExtraction = extractedFieldsDetector.detect(); - List extractedFieldNames = fieldExtraction.v1().getAllFields().stream().map(ExtractedField::getName) + List extractedFieldNames = fieldExtraction.v1() + .getAllFields() + .stream() + .map(ExtractedField::getName) .collect(Collectors.toList()); assertThat(extractedFieldNames, equalTo(Arrays.asList("field_1", "field_2", "field_3"))); - assertThat(fieldExtraction.v1().getAllFields().stream().map(ExtractedField::getMethod).collect(Collectors.toSet()), - contains(equalTo(ExtractedField.Method.DOC_VALUE))); + assertThat( + fieldExtraction.v1().getAllFields().stream().map(ExtractedField::getMethod).collect(Collectors.toSet()), + contains(equalTo(ExtractedField.Method.DOC_VALUE)) + ); } public void testDetect_GivenEqualFieldsToDocValuesLimit() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addAggregatableField("field_1", "float") + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addAggregatableField("field_1", "float") .addAggregatableField("field_2", "float") .addAggregatableField("field_3", "float") .addAggregatableField("a_keyword", "keyword") .build(); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildOutlierDetectionConfig(), 3, fieldCapabilities, Collections.emptyMap()); + buildOutlierDetectionConfig(), + 3, + fieldCapabilities, + Collections.emptyMap() + ); Tuple> fieldExtraction = extractedFieldsDetector.detect(); - List extractedFieldNames = fieldExtraction.v1().getAllFields().stream().map(ExtractedField::getName) + List extractedFieldNames = fieldExtraction.v1() + .getAllFields() + .stream() + .map(ExtractedField::getName) .collect(Collectors.toList()); assertThat(extractedFieldNames, equalTo(Arrays.asList("field_1", "field_2", "field_3"))); - assertThat(fieldExtraction.v1().getAllFields().stream().map(ExtractedField::getMethod).collect(Collectors.toSet()), - contains(equalTo(ExtractedField.Method.DOC_VALUE))); + assertThat( + fieldExtraction.v1().getAllFields().stream().map(ExtractedField::getMethod).collect(Collectors.toSet()), + contains(equalTo(ExtractedField.Method.DOC_VALUE)) + ); } public void testDetect_GivenMoreFieldsThanDocValuesLimit() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addAggregatableField("field_1", "float") + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addAggregatableField("field_1", "float") .addAggregatableField("field_2", "float") .addAggregatableField("field_3", "float") .addAggregatableField("a_keyword", "keyword") .build(); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildOutlierDetectionConfig(), 2, fieldCapabilities, Collections.emptyMap()); + buildOutlierDetectionConfig(), + 2, + fieldCapabilities, + Collections.emptyMap() + ); Tuple> fieldExtraction = extractedFieldsDetector.detect(); - List extractedFieldNames = fieldExtraction.v1().getAllFields().stream().map(ExtractedField::getName) + List extractedFieldNames = fieldExtraction.v1() + .getAllFields() + .stream() + .map(ExtractedField::getName) .collect(Collectors.toList()); assertThat(extractedFieldNames, equalTo(Arrays.asList("field_1", "field_2", "field_3"))); - assertThat(fieldExtraction.v1().getAllFields().stream().map(ExtractedField::getMethod).collect(Collectors.toSet()), - contains(equalTo(ExtractedField.Method.SOURCE))); + assertThat( + fieldExtraction.v1().getAllFields().stream().map(ExtractedField::getMethod).collect(Collectors.toSet()), + contains(equalTo(ExtractedField.Method.SOURCE)) + ); } private void testDetect_GivenBooleanField(DataFrameAnalyticsConfig config, boolean isRequired, FieldSelection.FeatureType featureType) { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addAggregatableField("some_boolean", "boolean") + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addAggregatableField("some_boolean", "boolean") .addAggregatableField("some_integer", "integer") .build(); @@ -605,9 +798,7 @@ private void testDetect_GivenBooleanField(DataFrameAnalyticsConfig config, boole fieldCardinalities.put("some_boolean", 2L); fieldCardinalities.put("some_integer", 2L); - - ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - config, 100, fieldCapabilities, fieldCardinalities); + ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector(config, 100, fieldCapabilities, fieldCardinalities); Tuple> fieldExtraction = extractedFieldsDetector.detect(); List allFields = fieldExtraction.v1().getAllFields(); @@ -616,7 +807,8 @@ private void testDetect_GivenBooleanField(DataFrameAnalyticsConfig config, boole assertThat(booleanField.getTypes(), contains("boolean")); assertThat(booleanField.getMethod(), equalTo(ExtractedField.Method.DOC_VALUE)); - assertFieldSelectionContains(fieldExtraction.v2().subList(0, 1), + assertFieldSelectionContains( + fieldExtraction.v2().subList(0, 1), FieldSelection.included("some_boolean", Collections.singleton("boolean"), isRequired, featureType) ); @@ -651,8 +843,7 @@ public void testDetect_GivenBooleanField_Classification_BooleanIsDependentVariab } public void testDetect_GivenMultiFields() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addAggregatableField("a_float", "float") + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addAggregatableField("a_float", "float") .addNonAggregatableField("text_without_keyword", "text") .addNonAggregatableField("text_1", "text") .addAggregatableField("text_1.keyword", "keyword") @@ -663,81 +854,96 @@ public void testDetect_GivenMultiFields() { .build(); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildRegressionConfig("a_float"), 100, fieldCapabilities, Collections.emptyMap()); + buildRegressionConfig("a_float"), + 100, + fieldCapabilities, + Collections.emptyMap() + ); Tuple> fieldExtraction = extractedFieldsDetector.detect(); assertThat(fieldExtraction.v1().getAllFields(), hasSize(5)); - List extractedFieldNames = fieldExtraction.v1().getAllFields().stream().map(ExtractedField::getName) + List extractedFieldNames = fieldExtraction.v1() + .getAllFields() + .stream() + .map(ExtractedField::getName) .collect(Collectors.toList()); assertThat(extractedFieldNames, contains("a_float", "keyword_1", "text_1.keyword", "text_2.keyword", "text_without_keyword")); - assertFieldSelectionContains(fieldExtraction.v2(), + assertFieldSelectionContains( + fieldExtraction.v2(), FieldSelection.included("a_float", Collections.singleton("float"), true, FieldSelection.FeatureType.NUMERICAL), FieldSelection.included("keyword_1", Collections.singleton("keyword"), false, FieldSelection.FeatureType.CATEGORICAL), - FieldSelection.excluded("keyword_1.text", Collections.singleton("text"), - "[keyword_1] is preferred because it is aggregatable"), - FieldSelection.excluded("text_1", Collections.singleton("text"), - "[text_1.keyword] is preferred because it is aggregatable"), + FieldSelection.excluded("keyword_1.text", Collections.singleton("text"), "[keyword_1] is preferred because it is aggregatable"), + FieldSelection.excluded("text_1", Collections.singleton("text"), "[text_1.keyword] is preferred because it is aggregatable"), FieldSelection.included("text_1.keyword", Collections.singleton("keyword"), false, FieldSelection.FeatureType.CATEGORICAL), - FieldSelection.excluded("text_2", Collections.singleton("text"), - "[text_2.keyword] is preferred because it is aggregatable"), + FieldSelection.excluded("text_2", Collections.singleton("text"), "[text_2.keyword] is preferred because it is aggregatable"), FieldSelection.included("text_2.keyword", Collections.singleton("keyword"), false, FieldSelection.FeatureType.CATEGORICAL), FieldSelection.included("text_without_keyword", Collections.singleton("text"), false, FieldSelection.FeatureType.CATEGORICAL) ); } public void testDetect_GivenMultiFieldAndParentIsRequired() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addAggregatableField("field_1", "keyword") + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addAggregatableField("field_1", "keyword") .addAggregatableField("field_1.keyword", "keyword") .addAggregatableField("field_2", "float") .build(); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildClassificationConfig("field_1"), 100, fieldCapabilities, Collections.singletonMap("field_1", 2L)); + buildClassificationConfig("field_1"), + 100, + fieldCapabilities, + Collections.singletonMap("field_1", 2L) + ); Tuple> fieldExtraction = extractedFieldsDetector.detect(); assertThat(fieldExtraction.v1().getAllFields(), hasSize(2)); - List extractedFieldNames = fieldExtraction.v1().getAllFields().stream().map(ExtractedField::getName) + List extractedFieldNames = fieldExtraction.v1() + .getAllFields() + .stream() + .map(ExtractedField::getName) .collect(Collectors.toList()); assertThat(extractedFieldNames, contains("field_1", "field_2")); - assertFieldSelectionContains(fieldExtraction.v2(), + assertFieldSelectionContains( + fieldExtraction.v2(), FieldSelection.included("field_1", Collections.singleton("keyword"), true, FieldSelection.FeatureType.CATEGORICAL), - FieldSelection.excluded("field_1.keyword", Collections.singleton("keyword"), - "[field_1] is required instead"), + FieldSelection.excluded("field_1.keyword", Collections.singleton("keyword"), "[field_1] is required instead"), FieldSelection.included("field_2", Collections.singleton("float"), false, FieldSelection.FeatureType.NUMERICAL) ); } public void testDetect_GivenMultiFieldAndMultiFieldIsRequired() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addAggregatableField("field_1", "keyword") + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addAggregatableField("field_1", "keyword") .addAggregatableField("field_1.keyword", "keyword") .addAggregatableField("field_2", "float") .build(); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildClassificationConfig("field_1.keyword"), 100, fieldCapabilities, - Collections.singletonMap("field_1.keyword", 2L)); + buildClassificationConfig("field_1.keyword"), + 100, + fieldCapabilities, + Collections.singletonMap("field_1.keyword", 2L) + ); Tuple> fieldExtraction = extractedFieldsDetector.detect(); assertThat(fieldExtraction.v1().getAllFields(), hasSize(2)); - List extractedFieldNames = fieldExtraction.v1().getAllFields().stream().map(ExtractedField::getName) + List extractedFieldNames = fieldExtraction.v1() + .getAllFields() + .stream() + .map(ExtractedField::getName) .collect(Collectors.toList()); assertThat(extractedFieldNames, contains("field_1.keyword", "field_2")); - assertFieldSelectionContains(fieldExtraction.v2(), - FieldSelection.excluded("field_1", Collections.singleton("keyword"), - "[field_1.keyword] is required instead"), + assertFieldSelectionContains( + fieldExtraction.v2(), + FieldSelection.excluded("field_1", Collections.singleton("keyword"), "[field_1.keyword] is required instead"), FieldSelection.included("field_1.keyword", Collections.singleton("keyword"), true, FieldSelection.FeatureType.CATEGORICAL), FieldSelection.included("field_2", Collections.singleton("float"), false, FieldSelection.FeatureType.NUMERICAL) ); } public void testDetect_GivenSeveralMultiFields_ShouldPickFirstSorted() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addNonAggregatableField("field_1", "text") + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addNonAggregatableField("field_1", "text") .addAggregatableField("field_1.keyword_3", "keyword") .addAggregatableField("field_1.keyword_2", "keyword") .addAggregatableField("field_1.keyword_1", "keyword") @@ -745,17 +951,28 @@ public void testDetect_GivenSeveralMultiFields_ShouldPickFirstSorted() { .build(); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildRegressionConfig("field_2"), 100, fieldCapabilities, Collections.emptyMap()); + buildRegressionConfig("field_2"), + 100, + fieldCapabilities, + Collections.emptyMap() + ); Tuple> fieldExtraction = extractedFieldsDetector.detect(); assertThat(fieldExtraction.v1().getAllFields(), hasSize(2)); - List extractedFieldNames = fieldExtraction.v1().getAllFields().stream().map(ExtractedField::getName) + List extractedFieldNames = fieldExtraction.v1() + .getAllFields() + .stream() + .map(ExtractedField::getName) .collect(Collectors.toList()); assertThat(extractedFieldNames, contains("field_1.keyword_1", "field_2")); - assertFieldSelectionContains(fieldExtraction.v2(), - FieldSelection.excluded("field_1", Collections.singleton("text"), - "[field_1.keyword_1] is preferred because it is aggregatable"), + assertFieldSelectionContains( + fieldExtraction.v2(), + FieldSelection.excluded( + "field_1", + Collections.singleton("text"), + "[field_1.keyword_1] is preferred because it is aggregatable" + ), FieldSelection.included("field_1.keyword_1", Collections.singleton("keyword"), false, FieldSelection.FeatureType.CATEGORICAL), FieldSelection.excluded("field_1.keyword_2", Collections.singleton("keyword"), "[field_1.keyword_1] came first"), FieldSelection.excluded("field_1.keyword_3", Collections.singleton("keyword"), "[field_1.keyword_1] came first"), @@ -764,97 +981,134 @@ public void testDetect_GivenSeveralMultiFields_ShouldPickFirstSorted() { } public void testDetect_GivenMultiFields_OverDocValueLimit() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addNonAggregatableField("field_1", "text") + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addNonAggregatableField("field_1", "text") .addAggregatableField("field_1.keyword_1", "keyword") .addAggregatableField("field_2", "float") .build(); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildRegressionConfig("field_2"), 0, fieldCapabilities, Collections.emptyMap()); + buildRegressionConfig("field_2"), + 0, + fieldCapabilities, + Collections.emptyMap() + ); Tuple> fieldExtraction = extractedFieldsDetector.detect(); assertThat(fieldExtraction.v1().getAllFields(), hasSize(2)); - List extractedFieldNames = fieldExtraction.v1().getAllFields().stream().map(ExtractedField::getName) + List extractedFieldNames = fieldExtraction.v1() + .getAllFields() + .stream() + .map(ExtractedField::getName) .collect(Collectors.toList()); assertThat(extractedFieldNames, contains("field_1", "field_2")); - assertFieldSelectionContains(fieldExtraction.v2(), + assertFieldSelectionContains( + fieldExtraction.v2(), FieldSelection.included("field_1", Collections.singleton("text"), false, FieldSelection.FeatureType.CATEGORICAL), - FieldSelection.excluded("field_1.keyword_1", Collections.singleton("keyword"), - "[field_1] is preferred because it supports fetching from source"), + FieldSelection.excluded( + "field_1.keyword_1", + Collections.singleton("keyword"), + "[field_1] is preferred because it supports fetching from source" + ), FieldSelection.included("field_2", Collections.singleton("float"), true, FieldSelection.FeatureType.NUMERICAL) ); } public void testDetect_GivenParentAndMultiFieldBothAggregatable() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addAggregatableField("field_1", "keyword") + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addAggregatableField("field_1", "keyword") .addAggregatableField("field_1.keyword", "keyword") .addAggregatableField("field_2.keyword", "float") .addAggregatableField("field_2.double", "double") .build(); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildRegressionConfig("field_2.double"), 100, fieldCapabilities, Collections.emptyMap()); + buildRegressionConfig("field_2.double"), + 100, + fieldCapabilities, + Collections.emptyMap() + ); Tuple> fieldExtraction = extractedFieldsDetector.detect(); assertThat(fieldExtraction.v1().getAllFields(), hasSize(3)); - List extractedFieldNames = fieldExtraction.v1().getAllFields().stream().map(ExtractedField::getName) + List extractedFieldNames = fieldExtraction.v1() + .getAllFields() + .stream() + .map(ExtractedField::getName) .collect(Collectors.toList()); assertThat(extractedFieldNames, contains("field_1", "field_2.double", "field_2.keyword")); - assertFieldSelectionContains(fieldExtraction.v2(), + assertFieldSelectionContains( + fieldExtraction.v2(), FieldSelection.included("field_1", Collections.singleton("keyword"), false, FieldSelection.FeatureType.CATEGORICAL), - FieldSelection.excluded("field_1.keyword", Collections.singleton("keyword"), - "[field_1] is preferred because it is aggregatable"), + FieldSelection.excluded( + "field_1.keyword", + Collections.singleton("keyword"), + "[field_1] is preferred because it is aggregatable" + ), FieldSelection.included("field_2.double", Collections.singleton("double"), true, FieldSelection.FeatureType.NUMERICAL), FieldSelection.included("field_2.keyword", Collections.singleton("float"), false, FieldSelection.FeatureType.NUMERICAL) ); } public void testDetect_GivenParentAndMultiFieldNoneAggregatable() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addNonAggregatableField("field_1", "text") + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addNonAggregatableField("field_1", "text") .addNonAggregatableField("field_1.text", "text") .addAggregatableField("field_2", "float") .build(); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildRegressionConfig("field_2"), 100, fieldCapabilities, Collections.emptyMap()); + buildRegressionConfig("field_2"), + 100, + fieldCapabilities, + Collections.emptyMap() + ); Tuple> fieldExtraction = extractedFieldsDetector.detect(); assertThat(fieldExtraction.v1().getAllFields(), hasSize(2)); - List extractedFieldNames = fieldExtraction.v1().getAllFields().stream().map(ExtractedField::getName) + List extractedFieldNames = fieldExtraction.v1() + .getAllFields() + .stream() + .map(ExtractedField::getName) .collect(Collectors.toList()); assertThat(extractedFieldNames, contains("field_1", "field_2")); - assertFieldSelectionContains(fieldExtraction.v2(), + assertFieldSelectionContains( + fieldExtraction.v2(), FieldSelection.included("field_1", Collections.singleton("text"), false, FieldSelection.FeatureType.CATEGORICAL), - FieldSelection.excluded("field_1.text", Collections.singleton("text"), - "[field_1] is preferred because none of the multi-fields are aggregatable"), + FieldSelection.excluded( + "field_1.text", + Collections.singleton("text"), + "[field_1] is preferred because none of the multi-fields are aggregatable" + ), FieldSelection.included("field_2", Collections.singleton("float"), true, FieldSelection.FeatureType.NUMERICAL) ); } public void testDetect_GivenMultiFields_AndExplicitlyIncludedFields() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addNonAggregatableField("field_1", "text") + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addNonAggregatableField("field_1", "text") .addAggregatableField("field_1.keyword", "keyword") .addAggregatableField("field_2", "float") .build(); analyzedFields = new FetchSourceContext(true, new String[] { "field_1", "field_2" }, new String[0]); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildRegressionConfig("field_2"), 100, fieldCapabilities, Collections.emptyMap()); + buildRegressionConfig("field_2"), + 100, + fieldCapabilities, + Collections.emptyMap() + ); Tuple> fieldExtraction = extractedFieldsDetector.detect(); assertThat(fieldExtraction.v1().getAllFields(), hasSize(2)); - List extractedFieldNames = fieldExtraction.v1().getAllFields().stream().map(ExtractedField::getName) + List extractedFieldNames = fieldExtraction.v1() + .getAllFields() + .stream() + .map(ExtractedField::getName) .collect(Collectors.toList()); assertThat(extractedFieldNames, contains("field_1", "field_2")); - assertFieldSelectionContains(fieldExtraction.v2(), + assertFieldSelectionContains( + fieldExtraction.v2(), FieldSelection.included("field_1", Collections.singleton("text"), false, FieldSelection.FeatureType.CATEGORICAL), FieldSelection.excluded("field_1.keyword", Collections.singleton("keyword"), "field not in includes list"), FieldSelection.included("field_2", Collections.singleton("float"), true, FieldSelection.FeatureType.NUMERICAL) @@ -862,16 +1116,20 @@ public void testDetect_GivenMultiFields_AndExplicitlyIncludedFields() { } public void testDetect_GivenSourceFilteringWithIncludes() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addAggregatableField("field_11", "float") + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addAggregatableField("field_11", "float") .addAggregatableField("field_12", "float") .addAggregatableField("field_21", "float") - .addAggregatableField("field_22", "float").build(); + .addAggregatableField("field_22", "float") + .build(); - sourceFiltering = new FetchSourceContext(true, new String[] {"field_1*"}, null); + sourceFiltering = new FetchSourceContext(true, new String[] { "field_1*" }, null); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildOutlierDetectionConfig(), 100, fieldCapabilities, Collections.emptyMap()); + buildOutlierDetectionConfig(), + 100, + fieldCapabilities, + Collections.emptyMap() + ); Tuple> fieldExtraction = extractedFieldsDetector.detect(); List allFields = fieldExtraction.v1().getAllFields(); @@ -879,22 +1137,28 @@ public void testDetect_GivenSourceFilteringWithIncludes() { assertThat(allFields.get(0).getName(), equalTo("field_11")); assertThat(allFields.get(1).getName(), equalTo("field_12")); - assertFieldSelectionContains(fieldExtraction.v2(), + assertFieldSelectionContains( + fieldExtraction.v2(), FieldSelection.included("field_11", Collections.singleton("float"), false, FieldSelection.FeatureType.NUMERICAL), - FieldSelection.included("field_12", Collections.singleton("float"), false, FieldSelection.FeatureType.NUMERICAL)); + FieldSelection.included("field_12", Collections.singleton("float"), false, FieldSelection.FeatureType.NUMERICAL) + ); } public void testDetect_GivenSourceFilteringWithExcludes() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addAggregatableField("field_11", "float") + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addAggregatableField("field_11", "float") .addAggregatableField("field_12", "float") .addAggregatableField("field_21", "float") - .addAggregatableField("field_22", "float").build(); + .addAggregatableField("field_22", "float") + .build(); - sourceFiltering = new FetchSourceContext(true, null, new String[] {"field_1*"}); + sourceFiltering = new FetchSourceContext(true, null, new String[] { "field_1*" }); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildOutlierDetectionConfig(), 100, fieldCapabilities, Collections.emptyMap()); + buildOutlierDetectionConfig(), + 100, + fieldCapabilities, + Collections.emptyMap() + ); Tuple> fieldExtraction = extractedFieldsDetector.detect(); List allFields = fieldExtraction.v1().getAllFields(); @@ -902,19 +1166,25 @@ public void testDetect_GivenSourceFilteringWithExcludes() { assertThat(allFields.get(0).getName(), equalTo("field_21")); assertThat(allFields.get(1).getName(), equalTo("field_22")); - assertFieldSelectionContains(fieldExtraction.v2(), + assertFieldSelectionContains( + fieldExtraction.v2(), FieldSelection.included("field_21", Collections.singleton("float"), false, FieldSelection.FeatureType.NUMERICAL), - FieldSelection.included("field_22", Collections.singleton("float"), false, FieldSelection.FeatureType.NUMERICAL)); + FieldSelection.included("field_22", Collections.singleton("float"), false, FieldSelection.FeatureType.NUMERICAL) + ); } public void testDetect_GivenObjectFields() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addAggregatableField("float_field", "float") + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addAggregatableField("float_field", "float") .addNonAggregatableField("object_field_1", "object") - .addNonAggregatableField("object_field_2", "object").build(); + .addNonAggregatableField("object_field_2", "object") + .build(); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildOutlierDetectionConfig(), 100, fieldCapabilities, Collections.emptyMap()); + buildOutlierDetectionConfig(), + 100, + fieldCapabilities, + Collections.emptyMap() + ); Tuple> fieldExtraction = extractedFieldsDetector.detect(); List allFields = fieldExtraction.v1().getAllFields(); @@ -923,8 +1193,7 @@ public void testDetect_GivenObjectFields() { } public void testDetect_GivenNestedFields() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addAggregatableField("float_field", "float") + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addAggregatableField("float_field", "float") .addNonAggregatableField("nested_field_1", "nested") .addAggregatableField("nested_field_1.a", "float") .addAggregatableField("nested_field_1.b", "float") @@ -935,14 +1204,19 @@ public void testDetect_GivenNestedFields() { .build(); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildOutlierDetectionConfig(), 100, fieldCapabilities, Collections.emptyMap()); + buildOutlierDetectionConfig(), + 100, + fieldCapabilities, + Collections.emptyMap() + ); Tuple> fieldExtraction = extractedFieldsDetector.detect(); List allFields = fieldExtraction.v1().getAllFields(); assertThat(allFields, hasSize(1)); assertThat(allFields.get(0).getName(), equalTo("float_field")); - assertFieldSelectionContains(fieldExtraction.v2(), + assertFieldSelectionContains( + fieldExtraction.v2(), FieldSelection.included("float_field", Collections.singleton("float"), false, FieldSelection.FeatureType.NUMERICAL), FieldSelection.excluded("nested_field_1.*", Collections.singleton("nested"), "nested fields are not supported"), FieldSelection.excluded("nested_field_2.*", Collections.singleton("nested"), "nested fields are not supported") @@ -950,57 +1224,68 @@ public void testDetect_GivenNestedFields() { } public void testDetect_GivenNestedFieldThatAlsoHasIncompatibleType() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addAggregatableField("float_field", "float") + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addAggregatableField("float_field", "float") .addNonAggregatableField("nested_field_1", "nested") .addAggregatableField("nested_field_1.a", "definitely_not_supported") .build(); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildOutlierDetectionConfig(), 100, fieldCapabilities, Collections.emptyMap()); + buildOutlierDetectionConfig(), + 100, + fieldCapabilities, + Collections.emptyMap() + ); Tuple> fieldExtraction = extractedFieldsDetector.detect(); List allFields = fieldExtraction.v1().getAllFields(); assertThat(allFields, hasSize(1)); assertThat(allFields.get(0).getName(), equalTo("float_field")); - assertFieldSelectionContains(fieldExtraction.v2(), + assertFieldSelectionContains( + fieldExtraction.v2(), FieldSelection.included("float_field", Collections.singleton("float"), false, FieldSelection.FeatureType.NUMERICAL), FieldSelection.excluded("nested_field_1.*", Collections.singleton("nested"), "nested fields are not supported") ); } public void testDetect_GivenAnalyzedFieldIncludesObjectField() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addAggregatableField("float_field", "float") - .addNonAggregatableField("object_field", "object").build(); + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addAggregatableField("float_field", "float") + .addNonAggregatableField("object_field", "object") + .build(); analyzedFields = new FetchSourceContext(true, new String[] { "float_field", "object_field" }, null); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildOutlierDetectionConfig(), 100, fieldCapabilities, Collections.emptyMap()); + buildOutlierDetectionConfig(), + 100, + fieldCapabilities, + Collections.emptyMap() + ); ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, extractedFieldsDetector::detect); assertThat(e.getMessage(), equalTo("analyzed_fields must not include or exclude object or nested fields: [object_field]")); } public void testDetect_GivenAnalyzedFieldIncludesNestedField() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addAggregatableField("float_field", "float") - .addNonAggregatableField("nested_field", "nested").build(); + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addAggregatableField("float_field", "float") + .addNonAggregatableField("nested_field", "nested") + .build(); analyzedFields = new FetchSourceContext(true, new String[] { "float_field", "nested_field" }, null); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildOutlierDetectionConfig(), 100, fieldCapabilities, Collections.emptyMap()); + buildOutlierDetectionConfig(), + 100, + fieldCapabilities, + Collections.emptyMap() + ); ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, extractedFieldsDetector::detect); assertThat(e.getMessage(), equalTo("analyzed_fields must not include or exclude object or nested fields: [nested_field]")); } private static FieldCapabilitiesResponse simpleFieldResponse() { - return new MockFieldCapsResponseBuilder() - .addField("_id", true, false, "_id") + return new MockFieldCapsResponseBuilder().addField("_id", true, false, "_id") .addAggregatableField("field_11", "float") .addNonAggregatableField("field_21", "float") .addAggregatableField("field_21.child", "float") @@ -1011,28 +1296,36 @@ private static FieldCapabilitiesResponse simpleFieldResponse() { } public void testDetect_GivenAnalyzedFieldExcludesObjectField() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addAggregatableField("float_field", "float") - .addNonAggregatableField("object_field", "object").build(); + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addAggregatableField("float_field", "float") + .addNonAggregatableField("object_field", "object") + .build(); - analyzedFields = new FetchSourceContext(true, null, new String[]{"object_field"}); + analyzedFields = new FetchSourceContext(true, null, new String[] { "object_field" }); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildOutlierDetectionConfig(), 100, fieldCapabilities, Collections.emptyMap()); + buildOutlierDetectionConfig(), + 100, + fieldCapabilities, + Collections.emptyMap() + ); ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, extractedFieldsDetector::detect); assertThat(e.getMessage(), equalTo("analyzed_fields must not include or exclude object or nested fields: [object_field]")); } public void testDetect_GivenAnalyzedFieldExcludesNestedField() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addAggregatableField("float_field", "float") - .addNonAggregatableField("nested_field", "nested").build(); + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addAggregatableField("float_field", "float") + .addNonAggregatableField("nested_field", "nested") + .build(); - analyzedFields = new FetchSourceContext(true, null, new String[]{"nested_field"}); + analyzedFields = new FetchSourceContext(true, null, new String[] { "nested_field" }); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildOutlierDetectionConfig(), 100, fieldCapabilities, Collections.emptyMap()); + buildOutlierDetectionConfig(), + 100, + fieldCapabilities, + Collections.emptyMap() + ); ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, extractedFieldsDetector::detect); assertThat(e.getMessage(), equalTo("analyzed_fields must not include or exclude object or nested fields: [nested_field]")); @@ -1044,7 +1337,8 @@ public void testDetect_givenFeatureProcessorsFailures_ResultsField() { buildRegressionConfig("field_31", Arrays.asList(buildPreProcessor("ml.result", "foo"))), 100, fieldCapabilities, - Collections.emptyMap()); + Collections.emptyMap() + ); ElasticsearchStatusException ex = expectThrows(ElasticsearchStatusException.class, extractedFieldsDetector::detect); assertThat(ex.getMessage(), equalTo("fields contained in results field [ml] cannot be used in a feature_processor")); } @@ -1055,28 +1349,28 @@ public void testDetect_givenFeatureProcessorsFailures_Objects() { buildRegressionConfig("field_31", Arrays.asList(buildPreProcessor("object_field", "foo"))), 100, fieldCapabilities, - Collections.emptyMap()); + Collections.emptyMap() + ); ElasticsearchStatusException ex = expectThrows(ElasticsearchStatusException.class, extractedFieldsDetector::detect); assertThat(ex.getMessage(), equalTo("fields for feature_processors must not be objects or nested")); } public void testDetect_givenFeatureProcessorsFailures_Nested() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addAggregatableField("some_float", "float") + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addAggregatableField("some_float", "float") .addNonAggregatableField("nested_field", "nested") .build(); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( buildRegressionConfig("some_float", Arrays.asList(buildPreProcessor("nested_field", "foo"))), 100, fieldCapabilities, - Collections.emptyMap()); + Collections.emptyMap() + ); ElasticsearchStatusException ex = expectThrows(ElasticsearchStatusException.class, extractedFieldsDetector::detect); assertThat(ex.getMessage(), equalTo("fields for feature_processors must not be objects or nested")); } public void testDetect_givenFeatureProcessorsFailures_ChildOfNested() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addAggregatableField("some_float", "float") + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addAggregatableField("some_float", "float") .addNonAggregatableField("nested_field", "nested") .addAggregatableField("nested_field.inner_float", "float") .build(); @@ -1084,7 +1378,8 @@ public void testDetect_givenFeatureProcessorsFailures_ChildOfNested() { buildRegressionConfig("some_float", Arrays.asList(buildPreProcessor("nested_field.inner_float", "foo"))), 100, fieldCapabilities, - Collections.emptyMap()); + Collections.emptyMap() + ); ElasticsearchStatusException ex = expectThrows(ElasticsearchStatusException.class, extractedFieldsDetector::detect); assertThat(ex.getMessage(), equalTo("nested fields [nested_field.*] cannot be used in a feature_processor")); } @@ -1095,10 +1390,10 @@ public void testDetect_givenFeatureProcessorsFailures_ReservedFields() { buildRegressionConfig("field_31", Arrays.asList(buildPreProcessor("_id", "foo"))), 100, fieldCapabilities, - Collections.emptyMap()); + Collections.emptyMap() + ); ElasticsearchStatusException ex = expectThrows(ElasticsearchStatusException.class, extractedFieldsDetector::detect); - assertThat(ex.getMessage(), - containsString("the following fields cannot be used in feature_processors")); + assertThat(ex.getMessage(), containsString("the following fields cannot be used in feature_processors")); } public void testDetect_givenFeatureProcessorsFailures_MissingFieldFromIndex() { @@ -1107,10 +1402,10 @@ public void testDetect_givenFeatureProcessorsFailures_MissingFieldFromIndex() { buildRegressionConfig("field_31", Arrays.asList(buildPreProcessor("bar", "foo"))), 100, fieldCapabilities, - Collections.emptyMap()); + Collections.emptyMap() + ); ElasticsearchStatusException ex = expectThrows(ElasticsearchStatusException.class, extractedFieldsDetector::detect); - assertThat(ex.getMessage(), - containsString("the fields [bar] were not found in the field capabilities of the source indices")); + assertThat(ex.getMessage(), containsString("the fields [bar] were not found in the field capabilities of the source indices")); } public void testDetect_givenFeatureProcessorsFailures_UsingRequiredField() { @@ -1119,19 +1414,21 @@ public void testDetect_givenFeatureProcessorsFailures_UsingRequiredField() { buildRegressionConfig("field_31", Arrays.asList(buildPreProcessor("field_31", "foo"))), 100, fieldCapabilities, - Collections.emptyMap()); + Collections.emptyMap() + ); ElasticsearchStatusException ex = expectThrows(ElasticsearchStatusException.class, extractedFieldsDetector::detect); assertThat(ex.getMessage(), equalTo("required analysis fields [field_31] cannot be used in a feature_processor")); } public void testDetect_givenFeatureProcessorsFailures_BadSourceFiltering() { FieldCapabilitiesResponse fieldCapabilities = simpleFieldResponse(); - sourceFiltering = new FetchSourceContext(true, null, new String[]{"field_1*"}); + sourceFiltering = new FetchSourceContext(true, null, new String[] { "field_1*" }); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( buildRegressionConfig("field_31", Arrays.asList(buildPreProcessor("field_11", "foo"))), 100, fieldCapabilities, - Collections.emptyMap()); + Collections.emptyMap() + ); ElasticsearchStatusException ex = expectThrows(ElasticsearchStatusException.class, extractedFieldsDetector::detect); assertThat(ex.getMessage(), equalTo("fields [field_11] required by field_processors are not included in source filtering.")); @@ -1139,12 +1436,13 @@ public void testDetect_givenFeatureProcessorsFailures_BadSourceFiltering() { public void testDetect_givenFeatureProcessorsFailures_MissingAnalyzedField() { FieldCapabilitiesResponse fieldCapabilities = simpleFieldResponse(); - analyzedFields = new FetchSourceContext(true, null, new String[]{"field_1*"}); + analyzedFields = new FetchSourceContext(true, null, new String[] { "field_1*" }); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( buildRegressionConfig("field_31", Arrays.asList(buildPreProcessor("field_11", "foo"))), 100, fieldCapabilities, - Collections.emptyMap()); + Collections.emptyMap() + ); ElasticsearchStatusException ex = expectThrows(ElasticsearchStatusException.class, extractedFieldsDetector::detect); assertThat(ex.getMessage(), equalTo("fields [field_11] required by field_processors are not included in the analyzed_fields.")); @@ -1156,85 +1454,83 @@ public void testDetect_givenFeatureProcessorsFailures_RequiredMultiFields() { buildRegressionConfig("field_31", Arrays.asList(buildPreProcessor("field_31.child", "foo"))), 100, fieldCapabilities, - Collections.emptyMap()); + Collections.emptyMap() + ); ElasticsearchStatusException ex = expectThrows(ElasticsearchStatusException.class, extractedFieldsDetector::detect); - assertThat(ex.getMessage(), - containsString("feature_processors cannot be applied to required fields for analysis; ")); + assertThat(ex.getMessage(), containsString("feature_processors cannot be applied to required fields for analysis; ")); extractedFieldsDetector = new ExtractedFieldsDetector( buildRegressionConfig("field_31.child", Arrays.asList(buildPreProcessor("field_31", "foo"))), 100, fieldCapabilities, - Collections.emptyMap()); + Collections.emptyMap() + ); ex = expectThrows(ElasticsearchStatusException.class, extractedFieldsDetector::detect); - assertThat(ex.getMessage(), - containsString("feature_processors cannot be applied to required fields for analysis; ")); + assertThat(ex.getMessage(), containsString("feature_processors cannot be applied to required fields for analysis; ")); } public void testDetect_givenFeatureProcessorsFailures_BothMultiFields() { FieldCapabilitiesResponse fieldCapabilities = simpleFieldResponse(); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildRegressionConfig("field_31", - Arrays.asList( - buildPreProcessor("field_21", "foo"), - buildPreProcessor("field_21.child", "bar") - )), + buildRegressionConfig( + "field_31", + Arrays.asList(buildPreProcessor("field_21", "foo"), buildPreProcessor("field_21.child", "bar")) + ), 100, fieldCapabilities, - Collections.emptyMap()); + Collections.emptyMap() + ); ElasticsearchStatusException ex = expectThrows(ElasticsearchStatusException.class, extractedFieldsDetector::detect); - assertThat(ex.getMessage(), - containsString("feature_processors refer to both multi-field ")); + assertThat(ex.getMessage(), containsString("feature_processors refer to both multi-field ")); } public void testDetect_givenFeatureProcessorsFailures_DuplicateOutputFields() { FieldCapabilitiesResponse fieldCapabilities = simpleFieldResponse(); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildRegressionConfig("field_31", - Arrays.asList( - buildPreProcessor("field_11", "foo"), - buildPreProcessor("field_21", "foo") - )), + buildRegressionConfig("field_31", Arrays.asList(buildPreProcessor("field_11", "foo"), buildPreProcessor("field_21", "foo"))), 100, fieldCapabilities, - Collections.emptyMap()); + Collections.emptyMap() + ); ElasticsearchStatusException ex = expectThrows(ElasticsearchStatusException.class, extractedFieldsDetector::detect); assertThat(ex.getMessage(), equalTo("feature_processors must define unique output field names; duplicate fields [foo]")); } public void testDetect_withFeatureProcessors() { - FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder() - .addAggregatableField("field_11", "float") + FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder().addAggregatableField("field_11", "float") .addAggregatableField("field_21", "float") .addNonAggregatableField("field_31", "float") .addAggregatableField("field_31.child", "float") .addNonAggregatableField("object_field", "object") .build(); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildRegressionConfig("field_11", - Arrays.asList(buildPreProcessor("field_31", "foo", "bar"))), + buildRegressionConfig("field_11", Arrays.asList(buildPreProcessor("field_31", "foo", "bar"))), 100, fieldCapabilities, - Collections.emptyMap()); + Collections.emptyMap() + ); ExtractedFields extracted = extractedFieldsDetector.detect().v1(); assertThat(extracted.getProcessedFieldInputs(), containsInAnyOrder("field_31")); - assertThat(extracted.getAllFields().stream().map(ExtractedField::getName).collect(Collectors.toSet()), - containsInAnyOrder("field_11", "field_21", "field_31")); + assertThat( + extracted.getAllFields().stream().map(ExtractedField::getName).collect(Collectors.toSet()), + containsInAnyOrder("field_11", "field_21", "field_31") + ); assertThat(extracted.getSourceFields(), arrayContainingInAnyOrder("field_31")); - assertThat(extracted.getDocValueFields().stream().map(ExtractedField::getName).collect(Collectors.toSet()), - containsInAnyOrder("field_21", "field_11")); + assertThat( + extracted.getDocValueFields().stream().map(ExtractedField::getName).collect(Collectors.toSet()), + containsInAnyOrder("field_21", "field_11") + ); assertThat(extracted.getProcessedFields(), hasSize(1)); } private DataFrameAnalyticsConfig buildOutlierDetectionConfig() { - return new DataFrameAnalyticsConfig.Builder() - .setId("foo") + return new DataFrameAnalyticsConfig.Builder().setId("foo") .setSource(new DataFrameAnalyticsSource(SOURCE_INDEX, null, sourceFiltering, null)) .setDest(new DataFrameAnalyticsDest(DEST_INDEX, RESULTS_FIELD)) .setAnalyzedFields(analyzedFields) @@ -1247,8 +1543,7 @@ private DataFrameAnalyticsConfig buildRegressionConfig(String dependentVariable) } private DataFrameAnalyticsConfig buildClassificationConfig(String dependentVariable) { - return new DataFrameAnalyticsConfig.Builder() - .setId("foo") + return new DataFrameAnalyticsConfig.Builder().setId("foo") .setSource(new DataFrameAnalyticsSource(SOURCE_INDEX, null, sourceFiltering, null)) .setDest(new DataFrameAnalyticsDest(DEST_INDEX, RESULTS_FIELD)) .setAnalysis(new Classification(dependentVariable)) @@ -1256,27 +1551,32 @@ private DataFrameAnalyticsConfig buildClassificationConfig(String dependentVaria } private DataFrameAnalyticsConfig buildRegressionConfig(String dependentVariable, List featureprocessors) { - return new DataFrameAnalyticsConfig.Builder() - .setId("foo") + return new DataFrameAnalyticsConfig.Builder().setId("foo") .setSource(new DataFrameAnalyticsSource(SOURCE_INDEX, null, sourceFiltering, null)) .setDest(new DataFrameAnalyticsDest(DEST_INDEX, RESULTS_FIELD)) .setAnalyzedFields(analyzedFields) - .setAnalysis(new Regression(dependentVariable, - BoostedTreeParams.builder().build(), - null, - null, - null, - null, - null, - featureprocessors, - null)) + .setAnalysis( + new Regression( + dependentVariable, + BoostedTreeParams.builder().build(), + null, + null, + null, + null, + null, + featureprocessors, + null + ) + ) .build(); } private static PreProcessor buildPreProcessor(String inputField, String... outputFields) { - return new OneHotEncoding(inputField, + return new OneHotEncoding( + inputField, Arrays.stream(outputFields).collect(Collectors.toMap((s) -> randomAlphaOfLength(10), Function.identity())), - true); + true + ); } /** @@ -1297,17 +1597,17 @@ private static void assertFieldSelectionContains(List actual, Fi public void testDetect_givenFeatureProcessorsFailures_DuplicateOutputFieldsWithUnProcessedField() { FieldCapabilitiesResponse fieldCapabilities = simpleFieldResponse(); ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector( - buildRegressionConfig("field_31", - Arrays.asList( - buildPreProcessor("field_11", "field_21") - )), + buildRegressionConfig("field_31", Arrays.asList(buildPreProcessor("field_11", "field_21"))), 100, fieldCapabilities, - Collections.emptyMap()); + Collections.emptyMap() + ); ElasticsearchStatusException ex = expectThrows(ElasticsearchStatusException.class, extractedFieldsDetector::detect); - assertThat(ex.getMessage(), - equalTo("feature_processors output fields must not include non-processed analysis fields; duplicate fields [field_21]")); + assertThat( + ex.getMessage(), + equalTo("feature_processors output fields must not include non-processed analysis fields; duplicate fields [field_21]") + ); } private static class MockFieldCapsResponseBuilder { @@ -1326,12 +1626,13 @@ private MockFieldCapsResponseBuilder addField(String field, boolean isAggregatab return addField(field, false, isAggregatable, types); } - private MockFieldCapsResponseBuilder addField(String field, boolean isMetadataField, - boolean isAggregatable, String... types) { + private MockFieldCapsResponseBuilder addField(String field, boolean isMetadataField, boolean isAggregatable, String... types) { Map caps = new HashMap<>(); for (String type : types) { - caps.put(type, new FieldCapabilities(field, type, - isMetadataField, true, isAggregatable, null, null, null, Collections.emptyMap())); + caps.put( + type, + new FieldCapabilities(field, type, isMetadataField, true, isAggregatable, null, null, null, Collections.emptyMap()) + ); } fieldCaps.put(field, caps); return this; diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/inference/InferenceRunnerTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/inference/InferenceRunnerTests.java index 1b84e232c8f5f..08409e232bc5e 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/inference/InferenceRunnerTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/inference/InferenceRunnerTests.java @@ -14,11 +14,11 @@ import org.elasticsearch.client.Client; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.search.SearchHit; import org.elasticsearch.tasks.TaskId; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsConfig; import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsDest; import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsSource; @@ -68,10 +68,9 @@ public class InferenceRunnerTests extends ESTestCase { public void setupTests() { client = mock(Client.class); resultsPersisterService = mock(ResultsPersisterService.class); - config = new DataFrameAnalyticsConfig.Builder() - .setId("test") + config = new DataFrameAnalyticsConfig.Builder().setId("test") .setAnalysis(RegressionTests.createRandom()) - .setSource(new DataFrameAnalyticsSource(new String[] {"source_index"}, null, null, null)) + .setSource(new DataFrameAnalyticsSource(new String[] { "source_index" }, null, null, null)) .setDest(new DataFrameAnalyticsDest("dest_index", "test_results_field")) .build(); progressTracker = ProgressTracker.fromZeroes(config.getAnalysis().getProgressPhases(), config.getAnalysis().supportsInference()); @@ -83,7 +82,8 @@ public void testInferTestDocs() { ExtractedFields extractedFields = new ExtractedFields( Collections.singletonList(new SourceField("key", Collections.singleton("integer"))), Collections.emptyList(), - Collections.emptyMap()); + Collections.emptyMap() + ); Map doc1 = new HashMap<>(); doc1.put("key", 1); @@ -95,20 +95,10 @@ public void testInferTestDocs() { when(testDocsIterator.getTotalHits()).thenReturn(2L); InferenceConfig config = ClassificationConfig.EMPTY_PARAMS; - LocalModel localModel = localModelInferences(new ClassificationInferenceResults(1.0, - "foo", - Collections.emptyList(), - Collections.emptyList(), - config, - 1.0, - 1.0), - new ClassificationInferenceResults(0.0, - "bar", - Collections.emptyList(), - Collections.emptyList(), - config, - .5, - .7)); + LocalModel localModel = localModelInferences( + new ClassificationInferenceResults(1.0, "foo", Collections.emptyList(), Collections.emptyList(), config, 1.0, 1.0), + new ClassificationInferenceResults(0.0, "bar", Collections.emptyList(), Collections.emptyList(), config, .5, .7) + ); InferenceRunner inferenceRunner = createInferenceRunner(extractedFields); @@ -121,24 +111,26 @@ public void testInferTestDocs() { BulkRequest bulkRequest = argumentCaptor.getAllValues().get(0); List> indexRequests = bulkRequest.requests(); - Map doc1Source = ((IndexRequest)indexRequests.get(0)).sourceAsMap(); - Map doc2Source = ((IndexRequest)indexRequests.get(1)).sourceAsMap(); + Map doc1Source = ((IndexRequest) indexRequests.get(0)).sourceAsMap(); + Map doc2Source = ((IndexRequest) indexRequests.get(1)).sourceAsMap(); - assertThat(doc1Source.get("test_results_field"), - equalTo(new HashMap<>(){{ + assertThat(doc1Source.get("test_results_field"), equalTo(new HashMap<>() { + { put("predicted_value", "foo"); put("prediction_probability", 1.0); put("prediction_score", 1.0); put("predicted_value", "foo"); put("is_training", false); - }})); - assertThat(doc2Source.get("test_results_field"), - equalTo(new HashMap<>(){{ + } + })); + assertThat(doc2Source.get("test_results_field"), equalTo(new HashMap<>() { + { put("predicted_value", "bar"); put("prediction_probability", 0.5); put("prediction_score", .7); put("is_training", false); - }})); + } + })); } public void testInferTestDocs_GivenCancelWasCalled() { @@ -165,7 +157,7 @@ private static Deque buildSearchHits(List> vals) } private static BytesReference fromMap(Map map) { - try(XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().map(map)) { + try (XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().map(map)) { return BytesReference.bytes(xContentBuilder); } catch (IOException ex) { throw new ElasticsearchException(ex); @@ -179,7 +171,16 @@ private LocalModel localModelInferences(InferenceResults first, InferenceResults } private InferenceRunner createInferenceRunner(ExtractedFields extractedFields) { - return new InferenceRunner(Settings.EMPTY, client, modelLoadingService, resultsPersisterService, parentTaskId, config, - extractedFields, progressTracker, new DataCountsTracker(new DataCounts(config.getId()))); + return new InferenceRunner( + Settings.EMPTY, + client, + modelLoadingService, + resultsPersisterService, + parentTaskId, + config, + extractedFields, + progressTracker, + new DataCountsTracker(new DataCounts(config.getId())) + ); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/process/AnalyticsBuilderTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/process/AnalyticsBuilderTests.java index 32ab1087139e1..18c3752789f45 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/process/AnalyticsBuilderTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/process/AnalyticsBuilderTests.java @@ -58,9 +58,7 @@ public void testBuild_Analytics() throws Exception { } public void testBuild_MemoryUsageEstimation() throws Exception { - analyticsBuilder - .performMemoryUsageEstimationOnly() - .build(); + analyticsBuilder.performMemoryUsageEstimationOnly().build(); assertThat(filesToDelete, hasSize(1)); verify(nativeController).startProcess(commandCaptor.capture()); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/process/AnalyticsProcessConfigTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/process/AnalyticsProcessConfigTests.java index 2564f87fe0a02..eed2f4291d3f5 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/process/AnalyticsProcessConfigTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/process/AnalyticsProcessConfigTests.java @@ -8,11 +8,11 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.ml.dataframe.analyses.Classification; import org.elasticsearch.xpack.core.ml.dataframe.analyses.DataFrameAnalysis; import org.elasticsearch.xpack.core.ml.dataframe.analyses.OutlierDetection; @@ -61,11 +61,14 @@ public void setUpConfigParams() { @SuppressWarnings("unchecked") public void testToXContent_GivenOutlierDetection() throws IOException { - ExtractedFields extractedFields = new ExtractedFields(Arrays.asList( - new DocValueField("field_1", Collections.singleton("double")), - new DocValueField("field_2", Collections.singleton("float"))), + ExtractedFields extractedFields = new ExtractedFields( + Arrays.asList( + new DocValueField("field_1", Collections.singleton("double")), + new DocValueField("field_2", Collections.singleton("float")) + ), Collections.emptyList(), - Collections.emptyMap()); + Collections.emptyMap() + ); DataFrameAnalysis analysis = new OutlierDetection.Builder().build(); AnalyticsProcessConfig processConfig = createProcessConfig(analysis, extractedFields); @@ -81,12 +84,15 @@ public void testToXContent_GivenOutlierDetection() throws IOException { @SuppressWarnings("unchecked") public void testToXContent_GivenRegression() throws IOException { - ExtractedFields extractedFields = new ExtractedFields(Arrays.asList( - new DocValueField("field_1", Collections.singleton("double")), - new DocValueField("field_2", Collections.singleton("float")), - new DocValueField("test_dep_var", Collections.singleton("keyword"))), + ExtractedFields extractedFields = new ExtractedFields( + Arrays.asList( + new DocValueField("field_1", Collections.singleton("double")), + new DocValueField("field_2", Collections.singleton("float")), + new DocValueField("test_dep_var", Collections.singleton("keyword")) + ), Collections.emptyList(), - Collections.emptyMap()); + Collections.emptyMap() + ); DataFrameAnalysis analysis = new Regression("test_dep_var"); AnalyticsProcessConfig processConfig = createProcessConfig(analysis, extractedFields); @@ -104,12 +110,15 @@ public void testToXContent_GivenRegression() throws IOException { @SuppressWarnings("unchecked") public void testToXContent_GivenClassificationAndDepVarIsKeyword() throws IOException { - ExtractedFields extractedFields = new ExtractedFields(Arrays.asList( - new DocValueField("field_1", Collections.singleton("double")), - new DocValueField("field_2", Collections.singleton("float")), - new DocValueField("test_dep_var", Collections.singleton("keyword"))), + ExtractedFields extractedFields = new ExtractedFields( + Arrays.asList( + new DocValueField("field_1", Collections.singleton("double")), + new DocValueField("field_2", Collections.singleton("float")), + new DocValueField("test_dep_var", Collections.singleton("keyword")) + ), Collections.emptyList(), - Collections.singletonMap("test_dep_var", 5L)); + Collections.singletonMap("test_dep_var", 5L) + ); DataFrameAnalysis analysis = new Classification("test_dep_var"); AnalyticsProcessConfig processConfig = createProcessConfig(analysis, extractedFields); @@ -129,12 +138,15 @@ public void testToXContent_GivenClassificationAndDepVarIsKeyword() throws IOExce @SuppressWarnings("unchecked") public void testToXContent_GivenClassificationAndDepVarIsInteger() throws IOException { - ExtractedFields extractedFields = new ExtractedFields(Arrays.asList( - new DocValueField("field_1", Collections.singleton("double")), - new DocValueField("field_2", Collections.singleton("float")), - new DocValueField("test_dep_var", Collections.singleton("integer"))), + ExtractedFields extractedFields = new ExtractedFields( + Arrays.asList( + new DocValueField("field_1", Collections.singleton("double")), + new DocValueField("field_2", Collections.singleton("float")), + new DocValueField("test_dep_var", Collections.singleton("integer")) + ), Collections.emptyList(), - Collections.singletonMap("test_dep_var", 8L)); + Collections.singletonMap("test_dep_var", 8L) + ); DataFrameAnalysis analysis = new Classification("test_dep_var"); AnalyticsProcessConfig processConfig = createProcessConfig(analysis, extractedFields); @@ -153,8 +165,17 @@ public void testToXContent_GivenClassificationAndDepVarIsInteger() throws IOExce } private AnalyticsProcessConfig createProcessConfig(DataFrameAnalysis analysis, ExtractedFields extractedFields) { - return new AnalyticsProcessConfig(jobId, rows, cols, memoryLimit, threads, resultsField, categoricalFields, analysis, - extractedFields); + return new AnalyticsProcessConfig( + jobId, + rows, + cols, + memoryLimit, + threads, + resultsField, + categoricalFields, + analysis, + extractedFields + ); } private static Map toMap(AnalyticsProcessConfig config) throws IOException { diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/process/AnalyticsProcessManagerTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/process/AnalyticsProcessManagerTests.java index d7be390257c5b..7deeb942b1a20 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/process/AnalyticsProcessManagerTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/process/AnalyticsProcessManagerTests.java @@ -99,57 +99,71 @@ public void setUpMocks() { when(task.getAllocationId()).thenReturn(TASK_ALLOCATION_ID); when(task.getStatsHolder()).thenReturn(newStatsHolder()); when(task.getParentTaskId()).thenReturn(new TaskId("")); - dataFrameAnalyticsConfig = DataFrameAnalyticsConfigTests.createRandomBuilder(CONFIG_ID, - false, - OutlierDetectionTests.createRandom()).build(); + dataFrameAnalyticsConfig = DataFrameAnalyticsConfigTests.createRandomBuilder(CONFIG_ID, false, OutlierDetectionTests.createRandom()) + .build(); dataExtractor = mock(DataFrameDataExtractor.class); when(dataExtractor.collectDataSummary()).thenReturn(new DataFrameDataExtractor.DataSummary(NUM_ROWS, NUM_COLS)); - when(dataExtractor.getExtractedFields()).thenReturn(new ExtractedFields(Collections.emptyList(), - Collections.emptyList(), - Collections.emptyMap())); + when(dataExtractor.getExtractedFields()).thenReturn( + new ExtractedFields(Collections.emptyList(), Collections.emptyList(), Collections.emptyMap()) + ); dataExtractorFactory = mock(DataFrameDataExtractorFactory.class); when(dataExtractorFactory.newExtractor(anyBoolean())).thenReturn(dataExtractor); when(dataExtractorFactory.getExtractedFields()).thenReturn(mock(ExtractedFields.class)); resultsPersisterService = mock(ResultsPersisterService.class); - processManager = new AnalyticsProcessManager(Settings.EMPTY, client, EsExecutors.DIRECT_EXECUTOR_SERVICE, executorServiceForProcess, - processFactory, auditor, trainedModelProvider, resultsPersisterService, 1); + processManager = new AnalyticsProcessManager( + Settings.EMPTY, + client, + EsExecutors.DIRECT_EXECUTOR_SERVICE, + executorServiceForProcess, + processFactory, + auditor, + trainedModelProvider, + resultsPersisterService, + 1 + ); } private StatsHolder newStatsHolder() { - return new StatsHolder(ProgressTracker.fromZeroes(Collections.singletonList("analyzing"), false).report(), + return new StatsHolder( + ProgressTracker.fromZeroes(Collections.singletonList("analyzing"), false).report(), null, null, - new DataCounts(CONFIG_ID)); + new DataCounts(CONFIG_ID) + ); } public void testRunJob_TaskIsStopping() { when(task.isStopping()).thenReturn(true); when(task.getParams()).thenReturn(new StartDataFrameAnalyticsAction.TaskParams("data_frame_id", Version.CURRENT, false)); - processManager.runJob(task, dataFrameAnalyticsConfig, dataExtractorFactory, ActionListener.wrap( - stepResponse -> { - assertThat(processManager.getProcessContextCount(), equalTo(0)); - assertThat(stepResponse.isTaskComplete(), is(true)); + processManager.runJob(task, dataFrameAnalyticsConfig, dataExtractorFactory, ActionListener.wrap(stepResponse -> { + assertThat(processManager.getProcessContextCount(), equalTo(0)); + assertThat(stepResponse.isTaskComplete(), is(true)); - InOrder inOrder = inOrder(task); - inOrder.verify(task).isStopping(); - inOrder.verify(task).getParams(); - verifyNoMoreInteractions(task); - }, - e -> fail(e.getMessage()) - )); + InOrder inOrder = inOrder(task); + inOrder.verify(task).isStopping(); + inOrder.verify(task).getParams(); + verifyNoMoreInteractions(task); + }, e -> fail(e.getMessage()))); } public void testRunJob_ProcessContextAlreadyExists() { - processManager.runJob(task, dataFrameAnalyticsConfig, dataExtractorFactory, ActionListener.wrap( - stepResponse -> {}, - e -> fail(e.getMessage()) // First run should not error - )); + processManager.runJob( + task, + dataFrameAnalyticsConfig, + dataExtractorFactory, + ActionListener.wrap( + stepResponse -> {}, + e -> fail(e.getMessage()) // First run should not error + ) + ); assertThat(processManager.getProcessContextCount(), equalTo(1)); - processManager.runJob(task, dataFrameAnalyticsConfig, dataExtractorFactory, ActionListener.wrap( - stepResponse -> fail("Expected error but listener got a response instead"), - e -> { + processManager.runJob( + task, + dataFrameAnalyticsConfig, + dataExtractorFactory, + ActionListener.wrap(stepResponse -> fail("Expected error but listener got a response instead"), e -> { assertThat(processManager.getProcessContextCount(), equalTo(1)); InOrder inOrder = inOrder(task); @@ -169,60 +183,54 @@ public void testRunJob_ProcessContextAlreadyExists() { public void testRunJob_EmptyDataFrame() { when(dataExtractor.collectDataSummary()).thenReturn(new DataFrameDataExtractor.DataSummary(0, NUM_COLS)); - processManager.runJob(task, dataFrameAnalyticsConfig, dataExtractorFactory, ActionListener.wrap( - stepResponse -> { - assertThat(processManager.getProcessContextCount(), equalTo(0)); // Make sure the process context did not leak - assertThat(stepResponse.isTaskComplete(), is(true)); - - InOrder inOrder = inOrder(dataExtractor, executorServiceForProcess, process, task); - inOrder.verify(task).isStopping(); - inOrder.verify(task).getAllocationId(); - inOrder.verify(task).isStopping(); - inOrder.verify(dataExtractor).collectDataSummary(); - inOrder.verify(dataExtractor).getCategoricalFields(dataFrameAnalyticsConfig.getAnalysis()); - inOrder.verify(task).getAllocationId(); - verifyNoMoreInteractions(dataExtractor, executorServiceForProcess, process, task); - }, - e -> fail(e.getMessage()) - )); + processManager.runJob(task, dataFrameAnalyticsConfig, dataExtractorFactory, ActionListener.wrap(stepResponse -> { + assertThat(processManager.getProcessContextCount(), equalTo(0)); // Make sure the process context did not leak + assertThat(stepResponse.isTaskComplete(), is(true)); + + InOrder inOrder = inOrder(dataExtractor, executorServiceForProcess, process, task); + inOrder.verify(task).isStopping(); + inOrder.verify(task).getAllocationId(); + inOrder.verify(task).isStopping(); + inOrder.verify(dataExtractor).collectDataSummary(); + inOrder.verify(dataExtractor).getCategoricalFields(dataFrameAnalyticsConfig.getAnalysis()); + inOrder.verify(task).getAllocationId(); + verifyNoMoreInteractions(dataExtractor, executorServiceForProcess, process, task); + }, e -> fail(e.getMessage()))); } public void testRunJob_Ok() { - processManager.runJob(task, dataFrameAnalyticsConfig, dataExtractorFactory, - ActionListener.wrap( - stepResponse -> { - assertThat(processManager.getProcessContextCount(), equalTo(1)); - assertThat(stepResponse.isTaskComplete(), is(true)); - - InOrder inOrder = inOrder(dataExtractor, executorServiceForProcess, process, task); - inOrder.verify(task).isStopping(); - inOrder.verify(task).getAllocationId(); - inOrder.verify(task).isStopping(); - inOrder.verify(dataExtractor).collectDataSummary(); - inOrder.verify(dataExtractor).getCategoricalFields(dataFrameAnalyticsConfig.getAnalysis()); - inOrder.verify(process).isProcessAlive(); - inOrder.verify(task).getParentTaskId(); - inOrder.verify(task).getStatsHolder(); - inOrder.verify(dataExtractor).getExtractedFields(); - inOrder.verify(executorServiceForProcess, times(2)).execute(any()); // 'processData' and 'processResults' threads - verifyNoMoreInteractions(dataExtractor, executorServiceForProcess, process, task); - }, - e -> fail(e.getMessage()) - )); + processManager.runJob(task, dataFrameAnalyticsConfig, dataExtractorFactory, ActionListener.wrap(stepResponse -> { + assertThat(processManager.getProcessContextCount(), equalTo(1)); + assertThat(stepResponse.isTaskComplete(), is(true)); + + InOrder inOrder = inOrder(dataExtractor, executorServiceForProcess, process, task); + inOrder.verify(task).isStopping(); + inOrder.verify(task).getAllocationId(); + inOrder.verify(task).isStopping(); + inOrder.verify(dataExtractor).collectDataSummary(); + inOrder.verify(dataExtractor).getCategoricalFields(dataFrameAnalyticsConfig.getAnalysis()); + inOrder.verify(process).isProcessAlive(); + inOrder.verify(task).getParentTaskId(); + inOrder.verify(task).getStatsHolder(); + inOrder.verify(dataExtractor).getExtractedFields(); + inOrder.verify(executorServiceForProcess, times(2)).execute(any()); // 'processData' and 'processResults' threads + verifyNoMoreInteractions(dataExtractor, executorServiceForProcess, process, task); + }, e -> fail(e.getMessage()))); } public void testRunJob_ProcessNotAliveAfterStart() { when(process.isProcessAlive()).thenReturn(false); - when(task.getParams()).thenReturn( - new StartDataFrameAnalyticsAction.TaskParams("data_frame_id", Version.CURRENT, false)); + when(task.getParams()).thenReturn(new StartDataFrameAnalyticsAction.TaskParams("data_frame_id", Version.CURRENT, false)); - processManager.runJob(task, dataFrameAnalyticsConfig, dataExtractorFactory, ActionListener.wrap( - stepResponse -> fail("Expected error but listener got a response instead"), - e -> { + processManager.runJob( + task, + dataFrameAnalyticsConfig, + dataExtractorFactory, + ActionListener.wrap(stepResponse -> fail("Expected error but listener got a response instead"), e -> { assertThat(processManager.getProcessContextCount(), equalTo(0)); assertThat(e.getMessage(), equalTo("Failed to start data frame analytics process")); - } - )); + }) + ); } public void testProcessContext_GetSetFailureReason() { diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/process/AnalyticsResultProcessorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/process/AnalyticsResultProcessorTests.java index dd50d01083ebc..55469f4e69785 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/process/AnalyticsResultProcessorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/process/AnalyticsResultProcessorTests.java @@ -65,7 +65,8 @@ public class AnalyticsResultProcessorTests extends ESTestCase { ProgressTracker.fromZeroes(Collections.singletonList("analyzing"), false).report(), null, null, - new DataCounts(JOB_ID)); + new DataCounts(JOB_ID) + ); private TrainedModelProvider trainedModelProvider; private DataFrameAnalyticsAuditor auditor; private StatsPersister statsPersister; @@ -79,10 +80,9 @@ public void setUpMocks() { trainedModelProvider = mock(TrainedModelProvider.class); auditor = mock(DataFrameAnalyticsAuditor.class); statsPersister = mock(StatsPersister.class); - analyticsConfig = new DataFrameAnalyticsConfig.Builder() - .setId(JOB_ID) + analyticsConfig = new DataFrameAnalyticsConfig.Builder().setId(JOB_ID) .setDescription(JOB_DESCRIPTION) - .setSource(new DataFrameAnalyticsSource(new String[] {"my_source"}, null, null, null)) + .setSource(new DataFrameAnalyticsSource(new String[] { "my_source" }, null, null, null)) .setDest(new DataFrameAnalyticsDest("my_dest", null)) .setAnalysis(new Regression("foo")) .build(); @@ -102,9 +102,7 @@ public void testProcess_GivenNoResults() { public void testProcess_GivenEmptyResults() { givenDataFrameRows(2); - givenProcessResults(Arrays.asList( - AnalyticsResult.builder().build(), - AnalyticsResult.builder().build())); + givenProcessResults(Arrays.asList(AnalyticsResult.builder().build(), AnalyticsResult.builder().build())); AnalyticsResultProcessor resultProcessor = createResultProcessor(); resultProcessor.process(process); @@ -119,9 +117,12 @@ public void testProcess_GivenRowResults() { givenDataFrameRows(2); RowResults rowResults1 = mock(RowResults.class); RowResults rowResults2 = mock(RowResults.class); - givenProcessResults(Arrays.asList( - AnalyticsResult.builder().setRowResults(rowResults1).build(), - AnalyticsResult.builder().setRowResults(rowResults2).build())); + givenProcessResults( + Arrays.asList( + AnalyticsResult.builder().setRowResults(rowResults1).build(), + AnalyticsResult.builder().setRowResults(rowResults2).build() + ) + ); AnalyticsResultProcessor resultProcessor = createResultProcessor(); resultProcessor.process(process); @@ -138,9 +139,12 @@ public void testProcess_GivenDataFrameRowsJoinerFails() { givenDataFrameRows(2); RowResults rowResults1 = mock(RowResults.class); RowResults rowResults2 = mock(RowResults.class); - givenProcessResults(Arrays.asList( - AnalyticsResult.builder().setRowResults(rowResults1).build(), - AnalyticsResult.builder().setRowResults(rowResults2).build())); + givenProcessResults( + Arrays.asList( + AnalyticsResult.builder().setRowResults(rowResults1).build(), + AnalyticsResult.builder().setRowResults(rowResults2).build() + ) + ); doThrow(new RuntimeException("some failure")).when(dataFrameRowsJoiner).processRowResults(any(RowResults.class)); @@ -162,9 +166,12 @@ public void testCancel_GivenRowResults() { givenDataFrameRows(2); RowResults rowResults1 = mock(RowResults.class); RowResults rowResults2 = mock(RowResults.class); - givenProcessResults(Arrays.asList( - AnalyticsResult.builder().setRowResults(rowResults1).build(), - AnalyticsResult.builder().setRowResults(rowResults2).build())); + givenProcessResults( + Arrays.asList( + AnalyticsResult.builder().setRowResults(rowResults1).build(), + AnalyticsResult.builder().setRowResults(rowResults2).build() + ) + ); AnalyticsResultProcessor resultProcessor = createResultProcessor(); resultProcessor.cancel(); @@ -211,7 +218,9 @@ public void testCancel_GivenPhaseProgress() { Mockito.verifyNoMoreInteractions(dataFrameRowsJoiner, trainedModelProvider); assertThat(statsHolder.getProgressTracker().getWritingResultsProgressPercent(), equalTo(0)); - Optional testPhaseProgress = statsHolder.getProgressTracker().report().stream() + Optional testPhaseProgress = statsHolder.getProgressTracker() + .report() + .stream() .filter(p -> p.getPhase().equals(phaseProgress.getPhase())) .findAny(); assertThat(testPhaseProgress.isPresent(), is(true)); @@ -304,8 +313,16 @@ private void givenProcessResults(List results) { private void givenDataFrameRows(int rows) { AnalyticsProcessConfig config = new AnalyticsProcessConfig( - "job_id", rows, 1, ByteSizeValue.ZERO, 1, "ml", Collections.emptySet(), mock(DataFrameAnalysis.class), - mock(ExtractedFields.class)); + "job_id", + rows, + 1, + ByteSizeValue.ZERO, + 1, + "ml", + Collections.emptySet(), + mock(DataFrameAnalysis.class), + mock(ExtractedFields.class) + ); when(process.getConfig()).thenReturn(config); } @@ -314,12 +331,14 @@ private AnalyticsResultProcessor createResultProcessor() { } private AnalyticsResultProcessor createResultProcessor(List fieldNames) { - return new AnalyticsResultProcessor(analyticsConfig, + return new AnalyticsResultProcessor( + analyticsConfig, dataFrameRowsJoiner, statsHolder, trainedModelProvider, auditor, statsPersister, - new ExtractedFields(fieldNames, Collections.emptyList(), Collections.emptyMap())); + new ExtractedFields(fieldNames, Collections.emptyList(), Collections.emptyMap()) + ); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/process/ChunkedTrainedModelPersisterTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/process/ChunkedTrainedModelPersisterTests.java index b4cd0ce02cc1d..8c559762cef68 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/process/ChunkedTrainedModelPersisterTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/process/ChunkedTrainedModelPersisterTests.java @@ -11,9 +11,9 @@ import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.admin.indices.refresh.RefreshResponse; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.license.License; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsConfig; import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsDest; import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsSource; @@ -22,8 +22,8 @@ import org.elasticsearch.xpack.core.ml.inference.TrainedModelConfig; import org.elasticsearch.xpack.core.ml.inference.TrainedModelType; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.metadata.FeatureImportanceBaselineTests; -import org.elasticsearch.xpack.core.ml.inference.trainedmodel.metadata.TotalFeatureImportanceTests; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.metadata.HyperparametersTests; +import org.elasticsearch.xpack.core.ml.inference.trainedmodel.metadata.TotalFeatureImportanceTests; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.metadata.TrainedModelMetadata; import org.elasticsearch.xpack.core.security.user.XPackUser; import org.elasticsearch.xpack.ml.dataframe.process.results.ModelMetadata; @@ -75,10 +75,9 @@ public void setUpMocks() { @SuppressWarnings("unchecked") public void testPersistAllDocs() { - DataFrameAnalyticsConfig analyticsConfig = new DataFrameAnalyticsConfig.Builder() - .setId(JOB_ID) + DataFrameAnalyticsConfig analyticsConfig = new DataFrameAnalyticsConfig.Builder().setId(JOB_ID) .setDescription(JOB_DESCRIPTION) - .setSource(new DataFrameAnalyticsSource(new String[] {"my_source"}, null, null, null)) + .setSource(new DataFrameAnalyticsSource(new String[] { "my_source" }, null, null, null)) .setDest(new DataFrameAnalyticsDest("my_dest", null)) .setAnalysis(randomBoolean() ? new Regression("foo") : new Classification("foo")) .build(); @@ -112,13 +111,11 @@ public void testPersistAllDocs() { ModelSizeInfo modelSizeInfo = ModelSizeInfoTests.createRandom(); TrainedModelDefinitionChunk chunk1 = new TrainedModelDefinitionChunk(randomAlphaOfLength(10), 0, false); TrainedModelDefinitionChunk chunk2 = new TrainedModelDefinitionChunk(randomAlphaOfLength(10), 1, true); - ModelMetadata modelMetadata = new ModelMetadata(Stream.generate(TotalFeatureImportanceTests::randomInstance) - .limit(randomIntBetween(1, 10)) - .collect(Collectors.toList()), + ModelMetadata modelMetadata = new ModelMetadata( + Stream.generate(TotalFeatureImportanceTests::randomInstance).limit(randomIntBetween(1, 10)).collect(Collectors.toList()), FeatureImportanceBaselineTests.randomInstance(), - Stream.generate(HyperparametersTests::randomInstance) - .limit(randomIntBetween(1, 10)) - .collect(Collectors.toList())); + Stream.generate(HyperparametersTests::randomInstance).limit(randomIntBetween(1, 10)).collect(Collectors.toList()) + ); resultProcessor.createAndIndexInferenceModelConfig(modelSizeInfo, TrainedModelType.TREE_ENSEMBLE); resultProcessor.createAndIndexInferenceModelDoc(chunk1); @@ -129,12 +126,10 @@ public void testPersistAllDocs() { verify(trainedModelProvider).storeTrainedModelConfig(storedModelCaptor.capture(), any(ActionListener.class)); ArgumentCaptor storedDocCapture = ArgumentCaptor.forClass(TrainedModelDefinitionDoc.class); - verify(trainedModelProvider, times(2)) - .storeTrainedModelDefinitionDoc(storedDocCapture.capture(), any(ActionListener.class)); + verify(trainedModelProvider, times(2)).storeTrainedModelDefinitionDoc(storedDocCapture.capture(), any(ActionListener.class)); ArgumentCaptor storedMetadataCaptor = ArgumentCaptor.forClass(TrainedModelMetadata.class); - verify(trainedModelProvider, times(1)) - .storeTrainedModelMetadata(storedMetadataCaptor.capture(), any(ActionListener.class)); + verify(trainedModelProvider, times(1)).storeTrainedModelMetadata(storedMetadataCaptor.capture(), any(ActionListener.class)); TrainedModelConfig storedModel = storedModelCaptor.getValue(); assertThat(storedModel.getLicenseLevel(), equalTo(License.OperationMode.PLATINUM)); @@ -145,7 +140,7 @@ public void testPersistAllDocs() { assertThat(storedModel.getDescription(), equalTo(JOB_DESCRIPTION)); assertThat(storedModel.getModelDefinition(), is(nullValue())); assertThat(storedModel.getEstimatedHeapMemory(), equalTo(modelSizeInfo.ramBytesUsed())); - assertThat(storedModel.getEstimatedOperations(), equalTo((long)modelSizeInfo.numOperations())); + assertThat(storedModel.getEstimatedOperations(), equalTo((long) modelSizeInfo.numOperations())); if (analyticsConfig.getAnalysis() instanceof Classification) { assertThat(storedModel.getInferenceConfig().getName(), equalTo("classification")); } else { @@ -154,8 +149,7 @@ public void testPersistAllDocs() { Map metadata = storedModel.getMetadata(); assertThat(metadata.size(), equalTo(1)); assertThat(metadata, hasKey("analytics_config")); - Map analyticsConfigAsMap = XContentHelper.convertToMap(JsonXContent.jsonXContent, analyticsConfig.toString(), - true); + Map analyticsConfigAsMap = XContentHelper.convertToMap(JsonXContent.jsonXContent, analyticsConfig.toString(), true); assertThat(analyticsConfigAsMap, equalTo(metadata.get("analytics_config"))); TrainedModelDefinitionDoc storedDoc1 = storedDocCapture.getAllValues().get(0); @@ -175,13 +169,17 @@ public void testPersistAllDocs() { Mockito.verifyNoMoreInteractions(auditor); } - private ChunkedTrainedModelPersister createChunkedTrainedModelPersister(List fieldNames, - DataFrameAnalyticsConfig analyticsConfig) { - return new ChunkedTrainedModelPersister(trainedModelProvider, + private ChunkedTrainedModelPersister createChunkedTrainedModelPersister( + List fieldNames, + DataFrameAnalyticsConfig analyticsConfig + ) { + return new ChunkedTrainedModelPersister( + trainedModelProvider, analyticsConfig, auditor, - (unused)->{}, - new ExtractedFields(fieldNames, Collections.emptyList(), Collections.emptyMap())); + (unused) -> {}, + new ExtractedFields(fieldNames, Collections.emptyList(), Collections.emptyMap()) + ); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/process/DataFrameAnalyticsManagerTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/process/DataFrameAnalyticsManagerTests.java index 0543463345bf5..3b0be3192f21a 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/process/DataFrameAnalyticsManagerTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/process/DataFrameAnalyticsManagerTests.java @@ -24,18 +24,18 @@ public class DataFrameAnalyticsManagerTests extends ESTestCase { public void testNodeShuttingDown() { - DataFrameAnalyticsManager manager = - new DataFrameAnalyticsManager( - Settings.EMPTY, - mock(NodeClient.class), - mock(ThreadPool.class), - mock(ClusterService.class), - mock(DataFrameAnalyticsConfigProvider.class), - mock(AnalyticsProcessManager.class), - mock(DataFrameAnalyticsAuditor.class), - mock(IndexNameExpressionResolver.class), - mock(ResultsPersisterService.class), - mock(ModelLoadingService.class)); + DataFrameAnalyticsManager manager = new DataFrameAnalyticsManager( + Settings.EMPTY, + mock(NodeClient.class), + mock(ThreadPool.class), + mock(ClusterService.class), + mock(DataFrameAnalyticsConfigProvider.class), + mock(AnalyticsProcessManager.class), + mock(DataFrameAnalyticsAuditor.class), + mock(IndexNameExpressionResolver.class), + mock(ResultsPersisterService.class), + mock(ModelLoadingService.class) + ); assertThat(manager.isNodeShuttingDown(), is(false)); manager.markNodeAsShuttingDown(); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/process/DataFrameRowsJoinerTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/process/DataFrameRowsJoinerTests.java index 1a2f49ac6631a..e53b858fbf218 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/process/DataFrameRowsJoinerTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/process/DataFrameRowsJoinerTests.java @@ -67,7 +67,7 @@ public void testProcess_GivenSingleRowAndResult() throws IOException { givenClientHasNoFailures(); String dataDoc = "{\"f_1\": \"foo\", \"f_2\": 42.0}"; - String[] dataValues = {"42.0"}; + String[] dataValues = { "42.0" }; DataFrameDataExtractor.Row row = newTrainingRow(newHit(dataDoc), dataValues, 1); givenDataFrameBatches(List.of(Arrays.asList(row))); @@ -94,7 +94,7 @@ public void testProcess_GivenFullResultsBatch() throws IOException { givenClientHasNoFailures(); String dataDoc = "{\"f_1\": \"foo\", \"f_2\": 42.0}"; - String[] dataValues = {"42.0"}; + String[] dataValues = { "42.0" }; List firstBatch = new ArrayList<>(1000); IntStream.range(0, 1000).forEach(i -> firstBatch.add(newTrainingRow(newHit(dataDoc), dataValues, i))); List secondBatch = new ArrayList<>(1); @@ -118,7 +118,7 @@ public void testProcess_GivenSingleRowAndResultWithMismatchingIdHash() throws IO givenClientHasNoFailures(); String dataDoc = "{\"f_1\": \"foo\", \"f_2\": 42.0}"; - String[] dataValues = {"42.0"}; + String[] dataValues = { "42.0" }; DataFrameDataExtractor.Row row = newTrainingRow(newHit(dataDoc), dataValues, 1); givenDataFrameBatches(List.of(Arrays.asList(row))); @@ -136,7 +136,7 @@ public void testProcess_GivenSingleBatchWithSkippedRows() throws IOException { DataFrameDataExtractor.Row skippedRow = newTrainingRow(newHit("{}"), null, 1); String dataDoc = "{\"f_1\": \"foo\", \"f_2\": 42.0}"; - String[] dataValues = {"42.0"}; + String[] dataValues = { "42.0" }; DataFrameDataExtractor.Row normalRow = newTrainingRow(newHit(dataDoc), dataValues, 2); givenDataFrameBatches(List.of(Arrays.asList(skippedRow, normalRow))); @@ -163,7 +163,7 @@ public void testProcess_GivenTwoBatchesWhereFirstEndsWithSkippedRow() throws IOE givenClientHasNoFailures(); String dataDoc = "{\"f_1\": \"foo\", \"f_2\": 42.0}"; - String[] dataValues = {"42.0"}; + String[] dataValues = { "42.0" }; DataFrameDataExtractor.Row normalRow1 = newTrainingRow(newHit(dataDoc), dataValues, 1); DataFrameDataExtractor.Row normalRow2 = newTrainingRow(newHit(dataDoc), dataValues, 2); DataFrameDataExtractor.Row skippedRow = newTrainingRow(newHit("{}"), null, 3); @@ -195,7 +195,7 @@ public void testProcess_GivenSingleBatchWithTestRows() throws IOException { givenClientHasNoFailures(); String dataDoc = "{\"f_1\": \"foo\", \"f_2\": 42.0}"; - String[] dataValues = {"42.0"}; + String[] dataValues = { "42.0" }; DataFrameDataExtractor.Row testRow = newTestRow(newHit(dataDoc), dataValues, 1); DataFrameDataExtractor.Row normalRow = newTrainingRow(newHit(dataDoc), dataValues, 2); givenDataFrameBatches(Arrays.asList(Arrays.asList(testRow, normalRow))); @@ -223,7 +223,7 @@ public void testProcess_GivenTwoBatchesWhereFirstEndsWithTestRow() throws IOExce givenClientHasNoFailures(); String dataDoc = "{\"f_1\": \"foo\", \"f_2\": 42.0}"; - String[] dataValues = {"42.0"}; + String[] dataValues = { "42.0" }; DataFrameDataExtractor.Row normalRow1 = newTrainingRow(newHit(dataDoc), dataValues, 1); DataFrameDataExtractor.Row normalRow2 = newTrainingRow(newHit(dataDoc), dataValues, 2); DataFrameDataExtractor.Row testRow = newTestRow(newHit(dataDoc), dataValues, 3); @@ -255,7 +255,7 @@ public void testProcess_GivenMoreResultsThanRows() throws IOException { givenClientHasNoFailures(); String dataDoc = "{\"f_1\": \"foo\", \"f_2\": 42.0}"; - String[] dataValues = {"42.0"}; + String[] dataValues = { "42.0" }; DataFrameDataExtractor.Row row = newTrainingRow(newHit(dataDoc), dataValues, 1); givenDataFrameBatches(List.of(List.of(row))); @@ -276,7 +276,7 @@ public void testProcess_GivenNoResults_ShouldCancelAndConsumeExtractor() throws givenClientHasNoFailures(); String dataDoc = "{\"f_1\": \"foo\", \"f_2\": 42.0}"; - String[] dataValues = {"42.0"}; + String[] dataValues = { "42.0" }; DataFrameDataExtractor.Row row1 = newTrainingRow(newHit(dataDoc), dataValues, 1); DataFrameDataExtractor.Row row2 = newTrainingRow(newHit(dataDoc), dataValues, 1); givenDataFrameBatches(List.of(List.of(row1), List.of(row2))); @@ -289,8 +289,15 @@ public void testProcess_GivenNoResults_ShouldCancelAndConsumeExtractor() throws } private void givenProcessResults(List results) { - try (DataFrameRowsJoiner joiner = new DataFrameRowsJoiner(ANALYTICS_ID, Settings.EMPTY, new TaskId(""), dataExtractor, - resultsPersisterService)) { + try ( + DataFrameRowsJoiner joiner = new DataFrameRowsJoiner( + ANALYTICS_ID, + Settings.EMPTY, + new TaskId(""), + dataExtractor, + resultsPersisterService + ) + ) { results.forEach(joiner::processRowResults); } } @@ -326,9 +333,9 @@ private static DataFrameDataExtractor.Row newRow(SearchHit hit, String[] values, } private void givenClientHasNoFailures() { - when(resultsPersisterService.bulkIndexWithHeadersWithRetry( - eq(HEADERS), bulkRequestCaptor.capture(), eq(ANALYTICS_ID), any(), any())) - .thenReturn(new BulkResponse(new BulkItemResponse[0], 0)); + when( + resultsPersisterService.bulkIndexWithHeadersWithRetry(eq(HEADERS), bulkRequestCaptor.capture(), eq(ANALYTICS_ID), any(), any()) + ).thenReturn(new BulkResponse(new BulkItemResponse[0], 0)); } private static class DelegateStubDataExtractor { diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/process/MemoryUsageEstimationProcessManagerTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/process/MemoryUsageEstimationProcessManagerTests.java index fd9f007ea2c90..84239fe8a6b78 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/process/MemoryUsageEstimationProcessManagerTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/process/MemoryUsageEstimationProcessManagerTests.java @@ -44,8 +44,10 @@ public class MemoryUsageEstimationProcessManagerTests extends ESTestCase { private static final String CONFIG_ID = "dummy"; private static final int NUM_ROWS = 100; private static final int NUM_COLS = 4; - private static final MemoryUsageEstimationResult PROCESS_RESULT = - new MemoryUsageEstimationResult(ByteSizeValue.parseBytesSizeValue("20kB", ""), ByteSizeValue.parseBytesSizeValue("10kB", "")); + private static final MemoryUsageEstimationResult PROCESS_RESULT = new MemoryUsageEstimationResult( + ByteSizeValue.parseBytesSizeValue("20kB", ""), + ByteSizeValue.parseBytesSizeValue("10kB", "") + ); private ExecutorService executorServiceForProcess; private AnalyticsProcess process; @@ -76,8 +78,11 @@ public void setUpMocks() { resultCaptor = ArgumentCaptor.forClass(MemoryUsageEstimationResult.class); exceptionCaptor = ArgumentCaptor.forClass(Exception.class); - processManager = - new MemoryUsageEstimationProcessManager(EsExecutors.DIRECT_EXECUTOR_SERVICE, executorServiceForProcess, processFactory); + processManager = new MemoryUsageEstimationProcessManager( + EsExecutors.DIRECT_EXECUTOR_SERVICE, + executorServiceForProcess, + processFactory + ); } public void testRunJob_EmptyDataFrame() { diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/process/results/AnalyticsResultTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/process/results/AnalyticsResultTests.java index 446ac669d1cc9..65059e5277a70 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/process/results/AnalyticsResultTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/process/results/AnalyticsResultTests.java @@ -7,19 +7,19 @@ package org.elasticsearch.xpack.ml.dataframe.process.results; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.search.SearchModule; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.search.SearchModule; -import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xpack.core.ml.dataframe.stats.classification.ClassificationStatsTests; import org.elasticsearch.xpack.core.ml.dataframe.stats.common.MemoryUsageTests; import org.elasticsearch.xpack.core.ml.dataframe.stats.outlierdetection.OutlierDetectionStatsTests; import org.elasticsearch.xpack.core.ml.dataframe.stats.regression.RegressionStatsTests; import org.elasticsearch.xpack.core.ml.inference.MlInferenceNamedXContentProvider; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.metadata.FeatureImportanceBaselineTests; -import org.elasticsearch.xpack.core.ml.inference.trainedmodel.metadata.TotalFeatureImportanceTests; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.metadata.HyperparametersTests; +import org.elasticsearch.xpack.core.ml.inference.trainedmodel.metadata.TotalFeatureImportanceTests; import org.elasticsearch.xpack.core.ml.utils.PhaseProgress; import org.elasticsearch.xpack.core.ml.utils.ToXContentParams; import org.elasticsearch.xpack.ml.inference.modelsize.MlModelSizeNamedXContentProvider; @@ -70,13 +70,15 @@ protected AnalyticsResult createTestInstance() { builder.setTrainedModelDefinitionChunk(new TrainedModelDefinitionChunk(def, randomIntBetween(0, 10), randomBoolean())); } if (randomBoolean()) { - builder.setModelMetadata(new ModelMetadata(Stream.generate(TotalFeatureImportanceTests::randomInstance) - .limit(randomIntBetween(1, 10)) - .collect(Collectors.toList()), - FeatureImportanceBaselineTests.randomInstance(), - Stream.generate(HyperparametersTests::randomInstance) - .limit(randomIntBetween(1, 10)) - .collect(Collectors.toList()))); + builder.setModelMetadata( + new ModelMetadata( + Stream.generate(TotalFeatureImportanceTests::randomInstance) + .limit(randomIntBetween(1, 10)) + .collect(Collectors.toList()), + FeatureImportanceBaselineTests.randomInstance(), + Stream.generate(HyperparametersTests::randomInstance).limit(randomIntBetween(1, 10)).collect(Collectors.toList()) + ) + ); } return builder.build(); } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/process/results/MemoryUsageEstimationResultTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/process/results/MemoryUsageEstimationResultTests.java index 2293ad4348126..a71602a9ce8f3 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/process/results/MemoryUsageEstimationResultTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/process/results/MemoryUsageEstimationResultTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.ml.dataframe.process.results; import org.elasticsearch.common.unit.ByteSizeValue; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -20,7 +20,8 @@ public class MemoryUsageEstimationResultTests extends AbstractXContentTestCase phases = progressTracker.report(); assertThat(phases.size(), equalTo(6)); - assertThat(phases.stream().map(PhaseProgress::getPhase).collect(Collectors.toList()), - contains("reindexing", "loading_data", "a", "b", "c", "writing_results")); + assertThat( + phases.stream().map(PhaseProgress::getPhase).collect(Collectors.toList()), + contains("reindexing", "loading_data", "a", "b", "c", "writing_results") + ); assertThat(phases.stream().map(PhaseProgress::getProgressPercent).allMatch(p -> p == 0), is(true)); } @@ -56,8 +58,10 @@ public void testFromZeroes_GivenAnalysisWithoutInference() { List phaseProgresses = progressTracker.report(); assertThat(phaseProgresses.size(), equalTo(5)); - assertThat(phaseProgresses.stream().map(PhaseProgress::getPhase).collect(Collectors.toList()), - contains("reindexing", "loading_data", "a", "b", "writing_results")); + assertThat( + phaseProgresses.stream().map(PhaseProgress::getPhase).collect(Collectors.toList()), + contains("reindexing", "loading_data", "a", "b", "writing_results") + ); } public void testFromZeroes_GivenAnalysisWithInference() { @@ -66,8 +70,10 @@ public void testFromZeroes_GivenAnalysisWithInference() { List phaseProgresses = progressTracker.report(); assertThat(phaseProgresses.size(), equalTo(6)); - assertThat(phaseProgresses.stream().map(PhaseProgress::getPhase).collect(Collectors.toList()), - contains("reindexing", "loading_data", "a", "b", "writing_results", "inference")); + assertThat( + phaseProgresses.stream().map(PhaseProgress::getPhase).collect(Collectors.toList()), + contains("reindexing", "loading_data", "a", "b", "writing_results", "inference") + ); } public void testUpdates() { @@ -84,8 +90,10 @@ public void testUpdates() { List phases = progressTracker.report(); assertThat(phases.size(), equalTo(4)); - assertThat(phases.stream().map(PhaseProgress::getPhase).collect(Collectors.toList()), - contains("reindexing", "loading_data", "foo", "writing_results")); + assertThat( + phases.stream().map(PhaseProgress::getPhase).collect(Collectors.toList()), + contains("reindexing", "loading_data", "foo", "writing_results") + ); assertThat(phases.get(0).getProgressPercent(), equalTo(1)); assertThat(phases.get(1).getProgressPercent(), equalTo(2)); assertThat(phases.get(2).getProgressPercent(), equalTo(3)); @@ -99,8 +107,10 @@ public void testUpdatePhase_GivenUnknownPhase() { List phases = progressTracker.report(); assertThat(phases.size(), equalTo(4)); - assertThat(phases.stream().map(PhaseProgress::getPhase).collect(Collectors.toList()), - contains("reindexing", "loading_data", "foo", "writing_results")); + assertThat( + phases.stream().map(PhaseProgress::getPhase).collect(Collectors.toList()), + contains("reindexing", "loading_data", "foo", "writing_results") + ); } public void testUpdateReindexingProgress_GivenLowerValueThanCurrentProgress() { @@ -163,14 +173,17 @@ public void testResetForInference_GivenInference() { progressTracker.resetForInference(); List progress = progressTracker.report(); - assertThat(progress, contains( - new PhaseProgress(ProgressTracker.REINDEXING, 100), - new PhaseProgress(ProgressTracker.LOADING_DATA, 100), - new PhaseProgress("a", 100), - new PhaseProgress("b", 100), - new PhaseProgress(ProgressTracker.WRITING_RESULTS, 100), - new PhaseProgress(ProgressTracker.INFERENCE, 0) - )); + assertThat( + progress, + contains( + new PhaseProgress(ProgressTracker.REINDEXING, 100), + new PhaseProgress(ProgressTracker.LOADING_DATA, 100), + new PhaseProgress("a", 100), + new PhaseProgress("b", 100), + new PhaseProgress(ProgressTracker.WRITING_RESULTS, 100), + new PhaseProgress(ProgressTracker.INFERENCE, 0) + ) + ); } public void testResetForInference_GivenNoInference() { @@ -184,13 +197,16 @@ public void testResetForInference_GivenNoInference() { progressTracker.resetForInference(); List progress = progressTracker.report(); - assertThat(progress, contains( - new PhaseProgress(ProgressTracker.REINDEXING, 100), - new PhaseProgress(ProgressTracker.LOADING_DATA, 100), - new PhaseProgress("a", 100), - new PhaseProgress("b", 100), - new PhaseProgress(ProgressTracker.WRITING_RESULTS, 100) - )); + assertThat( + progress, + contains( + new PhaseProgress(ProgressTracker.REINDEXING, 100), + new PhaseProgress(ProgressTracker.LOADING_DATA, 100), + new PhaseProgress("a", 100), + new PhaseProgress("b", 100), + new PhaseProgress(ProgressTracker.WRITING_RESULTS, 100) + ) + ); } public void testAreAllPhasesExceptInferenceComplete_GivenComplete() { @@ -214,8 +230,12 @@ public void testAreAllPhasesExceptInferenceComplete_GivenNotComplete() { phasePerProgress.put(ProgressTracker.INFERENCE, 50); phasePerProgress.put(nonCompletePhase, randomIntBetween(0, 99)); - ProgressTracker progressTracker = new ProgressTracker(phasePerProgress.entrySet().stream() - .map(entry -> new PhaseProgress(entry.getKey(), entry.getValue())).collect(Collectors.toList())); + ProgressTracker progressTracker = new ProgressTracker( + phasePerProgress.entrySet() + .stream() + .map(entry -> new PhaseProgress(entry.getKey(), entry.getValue())) + .collect(Collectors.toList()) + ); assertThat(progressTracker.areAllPhasesExceptInferenceComplete(), is(false)); } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/stats/StatsHolderTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/stats/StatsHolderTests.java index ddeab2a03347e..dcde7dab0bd59 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/stats/StatsHolderTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/stats/StatsHolderTests.java @@ -38,8 +38,10 @@ public void testAdjustProgressTracker_GivenZeroProgress() { List phaseProgresses = statsHolder.getProgressTracker().report(); assertThat(phaseProgresses.size(), equalTo(5)); - assertThat(phaseProgresses.stream().map(PhaseProgress::getPhase).collect(Collectors.toList()), - contains("reindexing", "loading_data", "a", "b", "writing_results")); + assertThat( + phaseProgresses.stream().map(PhaseProgress::getPhase).collect(Collectors.toList()), + contains("reindexing", "loading_data", "a", "b", "writing_results") + ); assertThat(phaseProgresses.get(0).getProgressPercent(), equalTo(0)); assertThat(phaseProgresses.get(1).getProgressPercent(), equalTo(0)); assertThat(phaseProgresses.get(2).getProgressPercent(), equalTo(0)); @@ -64,8 +66,10 @@ public void testAdjustProgressTracker_GivenSameAnalysisPhases() { List phaseProgresses = statsHolder.getProgressTracker().report(); assertThat(phaseProgresses.size(), equalTo(5)); - assertThat(phaseProgresses.stream().map(PhaseProgress::getPhase).collect(Collectors.toList()), - contains("reindexing", "loading_data", "a", "b", "writing_results")); + assertThat( + phaseProgresses.stream().map(PhaseProgress::getPhase).collect(Collectors.toList()), + contains("reindexing", "loading_data", "a", "b", "writing_results") + ); assertThat(phaseProgresses.get(0).getProgressPercent(), equalTo(100)); assertThat(phaseProgresses.get(1).getProgressPercent(), equalTo(0)); assertThat(phaseProgresses.get(2).getProgressPercent(), equalTo(0)); @@ -90,8 +94,10 @@ public void testAdjustProgressTracker_GivenDifferentAnalysisPhases() { List phaseProgresses = statsHolder.getProgressTracker().report(); assertThat(phaseProgresses.size(), equalTo(5)); - assertThat(phaseProgresses.stream().map(PhaseProgress::getPhase).collect(Collectors.toList()), - contains("reindexing", "loading_data", "c", "d", "writing_results")); + assertThat( + phaseProgresses.stream().map(PhaseProgress::getPhase).collect(Collectors.toList()), + contains("reindexing", "loading_data", "c", "d", "writing_results") + ); assertThat(phaseProgresses.get(0).getProgressPercent(), equalTo(100)); assertThat(phaseProgresses.get(1).getProgressPercent(), equalTo(0)); assertThat(phaseProgresses.get(2).getProgressPercent(), equalTo(0)); @@ -116,8 +122,10 @@ public void testAdjustProgressTracker_GivenReindexingProgressIncomplete() { List phaseProgresses = statsHolder.getProgressTracker().report(); assertThat(phaseProgresses.size(), equalTo(5)); - assertThat(phaseProgresses.stream().map(PhaseProgress::getPhase).collect(Collectors.toList()), - contains("reindexing", "loading_data", "a", "b", "writing_results")); + assertThat( + phaseProgresses.stream().map(PhaseProgress::getPhase).collect(Collectors.toList()), + contains("reindexing", "loading_data", "a", "b", "writing_results") + ); assertThat(phaseProgresses.get(0).getProgressPercent(), equalTo(1)); assertThat(phaseProgresses.get(1).getProgressPercent(), equalTo(0)); assertThat(phaseProgresses.get(2).getProgressPercent(), equalTo(0)); @@ -141,14 +149,17 @@ public void testAdjustProgressTracker_GivenAllPhasesCompleteExceptInference() { List phaseProgresses = statsHolder.getProgressTracker().report(); - assertThat(phaseProgresses, contains( - new PhaseProgress("reindexing", 100), - new PhaseProgress("loading_data", 100), - new PhaseProgress("a", 100), - new PhaseProgress("b", 100), - new PhaseProgress("writing_results", 100), - new PhaseProgress("inference", 0) - )); + assertThat( + phaseProgresses, + contains( + new PhaseProgress("reindexing", 100), + new PhaseProgress("loading_data", 100), + new PhaseProgress("a", 100), + new PhaseProgress("b", 100), + new PhaseProgress("writing_results", 100), + new PhaseProgress("inference", 0) + ) + ); } public void testResetProgressTracker() { @@ -168,8 +179,10 @@ public void testResetProgressTracker() { List phaseProgresses = statsHolder.getProgressTracker().report(); assertThat(phaseProgresses.size(), equalTo(5)); - assertThat(phaseProgresses.stream().map(PhaseProgress::getPhase).collect(Collectors.toList()), - contains("reindexing", "loading_data", "a", "b", "writing_results")); + assertThat( + phaseProgresses.stream().map(PhaseProgress::getPhase).collect(Collectors.toList()), + contains("reindexing", "loading_data", "a", "b", "writing_results") + ); assertThat(phaseProgresses.get(0).getProgressPercent(), equalTo(1)); assertThat(phaseProgresses.get(1).getProgressPercent(), equalTo(0)); assertThat(phaseProgresses.get(2).getProgressPercent(), equalTo(0)); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/traintestsplit/StratifiedTrainTestSplitterTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/traintestsplit/StratifiedTrainTestSplitterTests.java index e2a12a8f107a8..3309e444f1781 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/traintestsplit/StratifiedTrainTestSplitterTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/traintestsplit/StratifiedTrainTestSplitterTests.java @@ -51,7 +51,6 @@ public void setUpTests() { long classB = 0; long classC = 0; - classValuesPerRow = new String[ROWS_COUNT]; for (int i = 0; i < classValuesPerRow.length; i++) { double randomDouble = randomDoubleBetween(0.0, 1.0, true); @@ -74,8 +73,10 @@ public void setUpTests() { } public void testConstructor_GivenMissingDependentVariable() { - ElasticsearchException e = expectThrows(ElasticsearchException.class, () -> new StratifiedTrainTestSplitter( - Collections.emptyList(), "foo", Collections.emptyMap(), 100.0, 0)); + ElasticsearchException e = expectThrows( + ElasticsearchException.class, + () -> new StratifiedTrainTestSplitter(Collections.emptyList(), "foo", Collections.emptyMap(), 100.0, 0) + ); assertThat(e.getMessage(), equalTo("Could not find dependent variable [foo] in fields []")); } @@ -87,8 +88,7 @@ public void testIsTraining_GivenUnknownClass() { } row[dependentVariableIndex] = "unknown_class"; - IllegalStateException e = expectThrows(IllegalStateException.class, - () -> splitter.isTraining(row)); + IllegalStateException e = expectThrows(IllegalStateException.class, () -> splitter.isTraining(row)); assertThat(e.getMessage(), equalTo("Unknown class [unknown_class]; expected one of [a, b, c]")); } @@ -227,7 +227,7 @@ public void testIsTraining_GivenTwoClassesWithCountEqualToOne_ShouldUseForTraini TrainTestSplitter splitter = createSplitter(80.0); { - String[] row = new String[]{"class_a", "42.0"}; + String[] row = new String[] { "class_a", "42.0" }; String[] processedRow = Arrays.copyOf(row, row.length); assertThat(splitter.isTraining(processedRow), is(true)); @@ -236,7 +236,7 @@ public void testIsTraining_GivenTwoClassesWithCountEqualToOne_ShouldUseForTraini assertThat(Arrays.equals(processedRow, row), is(true)); } { - String[] row = new String[]{"class_b", "42.0"}; + String[] row = new String[] { "class_b", "42.0" }; String[] processedRow = Arrays.copyOf(row, row.length); assertThat(splitter.isTraining(processedRow), is(true)); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/extractor/ExtractedFieldsTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/extractor/ExtractedFieldsTests.java index c47e0c9e818dc..dcfa4cd1283ac 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/extractor/ExtractedFieldsTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/extractor/ExtractedFieldsTests.java @@ -40,12 +40,15 @@ public void testAllTypesOfFields() { ExtractedFields extractedFields = new ExtractedFields( Arrays.asList(docValue1, docValue2, scriptField1, scriptField2, sourceField1, sourceField2), Collections.emptyList(), - Collections.emptyMap()); + Collections.emptyMap() + ); assertThat(extractedFields.getAllFields().size(), equalTo(6)); - assertThat(extractedFields.getDocValueFields().stream().map(ExtractedField::getName).toArray(String[]::new), - equalTo(new String[] {"doc1", "doc2"})); - assertThat(extractedFields.getSourceFields(), equalTo(new String[] {"src1", "src2"})); + assertThat( + extractedFields.getDocValueFields().stream().map(ExtractedField::getName).toArray(String[]::new), + equalTo(new String[] { "doc1", "doc2" }) + ); + assertThat(extractedFields.getSourceFields(), equalTo(new String[] { "src1", "src2" })); } public void testBuildGivenMixtureOfTypes() { @@ -61,18 +64,20 @@ public void testBuildGivenMixtureOfTypes() { when(fieldCapabilitiesResponse.getField("value")).thenReturn(valueCaps); when(fieldCapabilitiesResponse.getField("airline")).thenReturn(airlineCaps); - ExtractedFields extractedFields = ExtractedFields.build(new TreeSet<>(Arrays.asList("time", "value", "airline", "airport")), + ExtractedFields extractedFields = ExtractedFields.build( + new TreeSet<>(Arrays.asList("time", "value", "airline", "airport")), new HashSet<>(Collections.singletonList("airport")), fieldCapabilitiesResponse, Collections.emptyMap(), - Collections.emptyList()); + Collections.emptyList() + ); assertThat(extractedFields.getDocValueFields().size(), equalTo(2)); assertThat(extractedFields.getDocValueFields().get(0).getName(), equalTo("time")); assertThat(extractedFields.getDocValueFields().get(0).getDocValueFormat(), equalTo("epoch_millis")); assertThat(extractedFields.getDocValueFields().get(1).getName(), equalTo("value")); assertThat(extractedFields.getDocValueFields().get(1).getDocValueFormat(), equalTo(null)); - assertThat(extractedFields.getSourceFields(), equalTo(new String[] {"airline"})); + assertThat(extractedFields.getSourceFields(), equalTo(new String[] { "airline" })); assertThat(extractedFields.getAllFields().size(), equalTo(4)); } @@ -87,8 +92,13 @@ public void testBuildGivenMultiFields() { when(fieldCapabilitiesResponse.getField("airport")).thenReturn(text); when(fieldCapabilitiesResponse.getField("airport.keyword")).thenReturn(keyword); - ExtractedFields extractedFields = ExtractedFields.build(new TreeSet<>(Arrays.asList("airline.text", "airport.keyword")), - Collections.emptySet(), fieldCapabilitiesResponse, Collections.emptyMap(), Collections.emptyList()); + ExtractedFields extractedFields = ExtractedFields.build( + new TreeSet<>(Arrays.asList("airline.text", "airport.keyword")), + Collections.emptySet(), + fieldCapabilitiesResponse, + Collections.emptyMap(), + Collections.emptyList() + ); assertThat(extractedFields.getDocValueFields().size(), equalTo(1)); assertThat(extractedFields.getDocValueFields().get(0).getName(), equalTo("airport.keyword")); @@ -129,12 +139,16 @@ public void testApplyBooleanMapping() { public void testBuildGivenFieldWithoutMappings() { FieldCapabilitiesResponse fieldCapabilitiesResponse = mock(FieldCapabilitiesResponse.class); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> ExtractedFields.build( - Collections.singleton("value"), - Collections.emptySet(), - fieldCapabilitiesResponse, - Collections.emptyMap(), - Collections.emptyList())); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> ExtractedFields.build( + Collections.singleton("value"), + Collections.emptySet(), + fieldCapabilitiesResponse, + Collections.emptyMap(), + Collections.emptyList() + ) + ); assertThat(e.getMessage(), equalTo("cannot retrieve field [value] because it has no mappings")); } @@ -153,10 +167,11 @@ public void testExtractFeatureOrganicAndProcessedNames() { ExtractedFields extractedFields = new ExtractedFields( Arrays.asList(docValue1, docValue2, scriptField1, scriptField2, sourceField1, sourceField2), Arrays.asList( - new ProcessedField(new NGram("doc1", "f", new int[] {1 , 2}, 0, 2, true)), - new ProcessedField(new OneHotEncoding("src1", hotMap, true))), - Collections.emptyMap()); - + new ProcessedField(new NGram("doc1", "f", new int[] { 1, 2 }, 0, 2, true)), + new ProcessedField(new OneHotEncoding("src1", hotMap, true)) + ), + Collections.emptyMap() + ); String[] organic = extractedFields.extractOrganicFeatureNames(); assertThat(organic, arrayContaining("doc2", "scripted1", "scripted2", "src2")); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/extractor/GeoPointFieldTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/extractor/GeoPointFieldTests.java index 7045955891174..4b48b44bcd9d4 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/extractor/GeoPointFieldTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/extractor/GeoPointFieldTests.java @@ -22,7 +22,7 @@ public class GeoPointFieldTests extends ESTestCase { public void testGivenGeoPoint() { double lat = 38.897676; double lon = -77.03653; - String[] expected = new String[] {lat + "," + lon}; + String[] expected = new String[] { lat + "," + lon }; SearchHit hit = new SearchHitBuilder(42).addField("geo", lat + ", " + lon).build(); // doc_value field diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/extractor/GeoShapeFieldTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/extractor/GeoShapeFieldTests.java index 6f3e04c7a004f..efedf918d479f 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/extractor/GeoShapeFieldTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/extractor/GeoShapeFieldTests.java @@ -20,10 +20,9 @@ public class GeoShapeFieldTests extends ESTestCase { public void testObjectFormat() { double lat = 38.897676; double lon = -77.03653; - String[] expected = new String[] {lat + "," + lon}; + String[] expected = new String[] { lat + "," + lon }; - SearchHit hit = new SearchHitBuilder(42) - .setSource("{\"geo\":{\"type\":\"point\", \"coordinates\": [" + lon + ", " + lat + "]}}") + SearchHit hit = new SearchHitBuilder(42).setSource("{\"geo\":{\"type\":\"point\", \"coordinates\": [" + lon + ", " + lat + "]}}") .build(); ExtractedField geo = new GeoShapeField("geo"); @@ -43,9 +42,9 @@ public void testObjectFormat() { public void testWKTFormat() { double lat = 38.897676; double lon = -77.03653; - String[] expected = new String[] {lat + "," + lon}; + String[] expected = new String[] { lat + "," + lon }; - SearchHit hit = new SearchHitBuilder(42).setSource("{\"geo\":\"POINT ("+ lon + " " + lat + ")\"}").build(); + SearchHit hit = new SearchHitBuilder(42).setSource("{\"geo\":\"POINT (" + lon + " " + lat + ")\"}").build(); ExtractedField geo = new GeoShapeField("geo"); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/extractor/ProcessedFieldTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/extractor/ProcessedFieldTests.java index 28464332127d7..b6189bcd4258d 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/extractor/ProcessedFieldTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/extractor/ProcessedFieldTests.java @@ -56,59 +56,45 @@ public void testMissingInputValues() { public void testProcessedFieldFrequencyEncoding() { testProcessedField( - new FrequencyEncoding(randomAlphaOfLength(10), + new FrequencyEncoding( + randomAlphaOfLength(10), randomAlphaOfLength(10), MapBuilder.newMapBuilder().put("bar", 1.0).put("1", 0.5).put("false", 0.0).map(), - randomBoolean()), - new Object[]{"bar", 1, false}, - new Object[][]{ - new Object[]{1.0}, - new Object[]{0.5}, - new Object[]{0.0}, - }); + randomBoolean() + ), + new Object[] { "bar", 1, false }, + new Object[][] { new Object[] { 1.0 }, new Object[] { 0.5 }, new Object[] { 0.0 }, } + ); } public void testProcessedFieldTargetMeanEncoding() { testProcessedField( - new TargetMeanEncoding(randomAlphaOfLength(10), + new TargetMeanEncoding( + randomAlphaOfLength(10), randomAlphaOfLength(10), MapBuilder.newMapBuilder().put("bar", 1.0).put("1", 0.5).put("false", 0.0).map(), 0.8, - randomBoolean()), - new Object[]{"bar", 1, false, "unknown"}, - new Object[][]{ - new Object[]{1.0}, - new Object[]{0.5}, - new Object[]{0.0}, - new Object[]{0.8}, - }); + randomBoolean() + ), + new Object[] { "bar", 1, false, "unknown" }, + new Object[][] { new Object[] { 1.0 }, new Object[] { 0.5 }, new Object[] { 0.0 }, new Object[] { 0.8 }, } + ); } public void testProcessedFieldNGramEncoding() { testProcessedField( - new NGram(randomAlphaOfLength(10), - randomAlphaOfLength(10), - new int[]{1}, - 0, - 3, - randomBoolean()), - new Object[]{"bar", 1, false}, - new Object[][]{ - new Object[]{"b", "a", "r"}, - new Object[]{"1", null, null}, - new Object[]{"f", "a", "l"} - }); + new NGram(randomAlphaOfLength(10), randomAlphaOfLength(10), new int[] { 1 }, 0, 3, randomBoolean()), + new Object[] { "bar", 1, false }, + new Object[][] { new Object[] { "b", "a", "r" }, new Object[] { "1", null, null }, new Object[] { "f", "a", "l" } } + ); } public void testProcessedFieldOneHot() { testProcessedField( makeOneHotPreProcessor(randomAlphaOfLength(10), "bar", "1", "false"), - new Object[]{"bar", 1, false}, - new Object[][]{ - new Object[]{0, 1, 0}, - new Object[]{1, 0, 0}, - new Object[]{0, 0, 1}, - }); + new Object[] { "bar", 1, false }, + new Object[][] { new Object[] { 0, 1, 0 }, new Object[] { 1, 0, 0 }, new Object[] { 0, 0, 1 }, } + ); } public void testProcessedField(PreProcessor preProcessor, Object[] inputs, Object[][] expectedOutputs) { @@ -120,7 +106,8 @@ public void testProcessedField(PreProcessor preProcessor, Object[] inputs, Objec assertThat( "Input [" + input + "] Expected " + Arrays.toString(expectedOutputs[i]) + " but received " + Arrays.toString(result), result, - equalTo(expectedOutputs[i])); + equalTo(expectedOutputs[i]) + ); } } @@ -129,7 +116,7 @@ private static PreProcessor makeOneHotPreProcessor(String inputField, String... for (String v : expectedExtractedValues) { map.put(v, v + "_column"); } - return new OneHotEncoding(inputField, map,true); + return new OneHotEncoding(inputField, map, true); } private static ExtractedField makeExtractedField(Object[] value) { diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/extractor/TimeFieldTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/extractor/TimeFieldTests.java index f383c7f4e03ea..79cf90498cd8a 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/extractor/TimeFieldTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/extractor/TimeFieldTests.java @@ -86,8 +86,10 @@ public void testUnknownFormat() { final ExtractedField timeField = new TimeField("time", ExtractedField.Method.DOC_VALUE); - assertThat(expectThrows(IllegalStateException.class, () -> timeField.value(hit)).getMessage(), - startsWith("Unexpected value for a time field")); + assertThat( + expectThrows(IllegalStateException.class, () -> timeField.value(hit)).getMessage(), + startsWith("Unexpected value for a time field") + ); } public void testSourceNotSupported() { diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/TrainedModelStatsServiceTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/TrainedModelStatsServiceTests.java index 54369ac3f4441..d14cf108eb826 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/TrainedModelStatsServiceTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/TrainedModelStatsServiceTests.java @@ -59,18 +59,18 @@ public void testVerifyIndicesExistAndPrimaryShardsAreActive() { .routingTable(routingTable.build()) .metadata(metadata); - assertThat(TrainedModelStatsService.verifyIndicesExistAndPrimaryShardsAreActive(csBuilder.build(), resolver), - equalTo(false)); + assertThat(TrainedModelStatsService.verifyIndicesExistAndPrimaryShardsAreActive(csBuilder.build(), resolver), equalTo(false)); } { Metadata.Builder metadata = Metadata.builder(); RoutingTable.Builder routingTable = RoutingTable.builder(); // With concrete ONLY IndexMetadata.Builder indexMetadata = IndexMetadata.builder(concreteIndex) - .settings(Settings.builder() - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) ); metadata.put(indexMetadata); addToRoutingTable(concreteIndex, routingTable); @@ -78,8 +78,7 @@ public void testVerifyIndicesExistAndPrimaryShardsAreActive() { ClusterState.Builder csBuilder = ClusterState.builder(new ClusterName("_name")) .routingTable(routingTable.build()) .metadata(metadata); - assertThat(TrainedModelStatsService.verifyIndicesExistAndPrimaryShardsAreActive(csBuilder.build(), resolver), - equalTo(false)); + assertThat(TrainedModelStatsService.verifyIndicesExistAndPrimaryShardsAreActive(csBuilder.build(), resolver), equalTo(false)); } { // With Alias And Concrete index @@ -87,10 +86,11 @@ public void testVerifyIndicesExistAndPrimaryShardsAreActive() { RoutingTable.Builder routingTable = RoutingTable.builder(); IndexMetadata.Builder indexMetadata = IndexMetadata.builder(concreteIndex) .putAlias(AliasMetadata.builder(aliasName).isHidden(true).build()) - .settings(Settings.builder() - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) ); metadata.put(indexMetadata); addToRoutingTable(concreteIndex, routingTable); @@ -98,8 +98,7 @@ public void testVerifyIndicesExistAndPrimaryShardsAreActive() { ClusterState.Builder csBuilder = ClusterState.builder(new ClusterName("_name")) .routingTable(routingTable.build()) .metadata(metadata); - assertThat(TrainedModelStatsService.verifyIndicesExistAndPrimaryShardsAreActive(csBuilder.build(), resolver), - equalTo(true)); + assertThat(TrainedModelStatsService.verifyIndicesExistAndPrimaryShardsAreActive(csBuilder.build(), resolver), equalTo(true)); } { // With Alias And Concrete index but routing is missing or concrete index @@ -107,10 +106,11 @@ public void testVerifyIndicesExistAndPrimaryShardsAreActive() { RoutingTable.Builder routingTable = RoutingTable.builder(); IndexMetadata.Builder indexMetadata = IndexMetadata.builder(concreteIndex) .putAlias(AliasMetadata.builder(aliasName).isHidden(true).build()) - .settings(Settings.builder() - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) ); metadata.put(indexMetadata); addToRoutingTable(concreteIndex, routingTable); @@ -119,18 +119,23 @@ public void testVerifyIndicesExistAndPrimaryShardsAreActive() { } else { Index index = new Index(concreteIndex, "_uuid"); ShardId shardId = new ShardId(index, 0); - ShardRouting shardRouting = ShardRouting.newUnassigned(shardId, true, RecoverySource.EmptyStoreRecoverySource.INSTANCE, - new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "")); + ShardRouting shardRouting = ShardRouting.newUnassigned( + shardId, + true, + RecoverySource.EmptyStoreRecoverySource.INSTANCE, + new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "") + ); shardRouting = shardRouting.initialize("node_id", null, 0L); - routingTable.add(IndexRoutingTable.builder(index) - .addIndexShard(new IndexShardRoutingTable.Builder(shardId).addShard(shardRouting).build())); + routingTable.add( + IndexRoutingTable.builder(index) + .addIndexShard(new IndexShardRoutingTable.Builder(shardId).addShard(shardRouting).build()) + ); } ClusterState.Builder csBuilder = ClusterState.builder(new ClusterName("_name")) .routingTable(routingTable.build()) .metadata(metadata); - assertThat(TrainedModelStatsService.verifyIndicesExistAndPrimaryShardsAreActive(csBuilder.build(), resolver), - equalTo(false)); + assertThat(TrainedModelStatsService.verifyIndicesExistAndPrimaryShardsAreActive(csBuilder.build(), resolver), equalTo(false)); } } @@ -151,18 +156,24 @@ public void testUpdateStatsUpgradeMode() { ThreadPool threadPool = mock(ThreadPool.class); ResultsPersisterService persisterService = mock(ResultsPersisterService.class); - TrainedModelStatsService service = new TrainedModelStatsService(persisterService, - originSettingClient, resolver, clusterService, threadPool); + TrainedModelStatsService service = new TrainedModelStatsService( + persisterService, + originSettingClient, + resolver, + clusterService, + threadPool + ); InferenceStats.Accumulator accumulator = new InferenceStats.Accumulator("testUpdateStatsUpgradeMode", "test-node", 1L); { IndexMetadata.Builder indexMetadata = IndexMetadata.builder(concreteIndex) .putAlias(AliasMetadata.builder(aliasName).isHidden(true).build()) - .settings(Settings.builder() - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) ); Metadata.Builder metadata = Metadata.builder().put(indexMetadata); @@ -185,10 +196,11 @@ public void testUpdateStatsUpgradeMode() { IndexMetadata.Builder indexMetadata = IndexMetadata.builder(concreteIndex) .putAlias(AliasMetadata.builder(aliasName).isHidden(true).build()) - .settings(Settings.builder() - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) ); // now set the upgrade mode @@ -215,10 +227,11 @@ public void testUpdateStatsUpgradeMode() { IndexMetadata.Builder indexMetadata = IndexMetadata.builder(concreteIndex) .putAlias(AliasMetadata.builder(aliasName).isHidden(true).build()) - .settings(Settings.builder() - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) ); Metadata.Builder metadata = Metadata.builder() @@ -255,8 +268,13 @@ public void testUpdateStatsResetMode() { ThreadPool threadPool = mock(ThreadPool.class); ResultsPersisterService persisterService = mock(ResultsPersisterService.class); - TrainedModelStatsService service = new TrainedModelStatsService(persisterService, - originSettingClient, resolver, clusterService, threadPool); + TrainedModelStatsService service = new TrainedModelStatsService( + persisterService, + originSettingClient, + resolver, + clusterService, + threadPool + ); InferenceStats.Accumulator accumulator = new InferenceStats.Accumulator("testUpdateStatsUpgradeMode", "test-node", 1L); @@ -265,10 +283,11 @@ public void testUpdateStatsResetMode() { IndexMetadata.Builder indexMetadata = IndexMetadata.builder(concreteIndex) .putAlias(AliasMetadata.builder(aliasName).isHidden(true).build()) - .settings(Settings.builder() - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) ); // now set the upgrade mode @@ -295,10 +314,11 @@ public void testUpdateStatsResetMode() { IndexMetadata.Builder indexMetadata = IndexMetadata.builder(concreteIndex) .putAlias(AliasMetadata.builder(aliasName).isHidden(true).build()) - .settings(Settings.builder() - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) ); Metadata.Builder metadata = Metadata.builder() @@ -321,11 +341,16 @@ public void testUpdateStatsResetMode() { private static void addToRoutingTable(String concreteIndex, RoutingTable.Builder routingTable) { Index index = new Index(concreteIndex, "_uuid"); ShardId shardId = new ShardId(index, 0); - ShardRouting shardRouting = ShardRouting.newUnassigned(shardId, true, RecoverySource.EmptyStoreRecoverySource.INSTANCE, - new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "")); + ShardRouting shardRouting = ShardRouting.newUnassigned( + shardId, + true, + RecoverySource.EmptyStoreRecoverySource.INSTANCE, + new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "") + ); shardRouting = shardRouting.initialize("node_id", null, 0L); shardRouting = shardRouting.moveToStarted(); - routingTable.add(IndexRoutingTable.builder(index) - .addIndexShard(new IndexShardRoutingTable.Builder(shardId).addShard(shardRouting).build())); + routingTable.add( + IndexRoutingTable.builder(index).addIndexShard(new IndexShardRoutingTable.Builder(shardId).addShard(shardRouting).build()) + ); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/allocation/TrainedModelAllocationClusterServiceTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/allocation/TrainedModelAllocationClusterServiceTests.java index e6b82316b9663..85701c84721f2 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/allocation/TrainedModelAllocationClusterServiceTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/allocation/TrainedModelAllocationClusterServiceTests.java @@ -98,7 +98,8 @@ public void testUpdateModelRoutingTable() { TrainedModelAllocationMetadata.Builder.empty() .addNewAllocation( modelId, - TrainedModelAllocation.Builder.empty(newParams(modelId, 10_000L)).addNewRoutingEntry(nodeId) + TrainedModelAllocation.Builder.empty(newParams(modelId, 10_000L)) + .addNewRoutingEntry(nodeId) .addNewRoutingEntry(startedNode) ) .build() @@ -227,10 +228,7 @@ public void testRemoveAllAllocations() { ClusterState clusterStateWithAllocations = ClusterState.builder(new ClusterName("testRemoveAllAllocations")) .metadata( Metadata.builder() - .putCustom( - TrainedModelAllocationMetadata.NAME, - TrainedModelAllocationMetadataTests.randomInstance() - ) + .putCustom(TrainedModelAllocationMetadata.NAME, TrainedModelAllocationMetadataTests.randomInstance()) .build() ) .build(); @@ -261,10 +259,7 @@ public void testCreateAllocation() { assertThat(createdAllocation.getNodeRoutingTable(), hasKey("ml-node-with-room")); assertThat(createdAllocation.getNodeRoutingTable().get("ml-node-with-room").getState(), equalTo(RoutingState.STARTING)); assertThat(createdAllocation.getReason().isPresent(), is(true)); - assertThat( - createdAllocation.getReason().get(), - containsString("Not allocating on node [ml-node-without-room]") - ); + assertThat(createdAllocation.getReason().get(), containsString("Not allocating on node [ml-node-without-room]")); assertThat(createdAllocation.getAllocationState(), equalTo(AllocationState.STARTING)); expectThrows( @@ -275,11 +270,7 @@ public void testCreateAllocation() { public void testCreateAllocationWhileResetModeIsTrue() { ClusterState currentState = ClusterState.builder(new ClusterName("testCreateAllocation")) - .nodes( - DiscoveryNodes.builder() - .add(buildNode("ml-node-with-room", true, ByteSizeValue.ofGb(4).getBytes())) - .build() - ) + .nodes(DiscoveryNodes.builder().add(buildNode("ml-node-with-room", true, ByteSizeValue.ofGb(4).getBytes())).build()) .metadata(Metadata.builder().putCustom(MlMetadata.TYPE, new MlMetadata.Builder().isResetMode(true).build())) .build(); TrainedModelAllocationClusterService trainedModelAllocationClusterService = createClusterService(); @@ -289,11 +280,7 @@ public void testCreateAllocationWhileResetModeIsTrue() { ); ClusterState stateWithoutReset = ClusterState.builder(new ClusterName("testCreateAllocation")) - .nodes( - DiscoveryNodes.builder() - .add(buildNode("ml-node-with-room", true, ByteSizeValue.ofGb(4).getBytes())) - .build() - ) + .nodes(DiscoveryNodes.builder().add(buildNode("ml-node-with-room", true, ByteSizeValue.ofGb(4).getBytes())).build()) .metadata(Metadata.builder().putCustom(MlMetadata.TYPE, new MlMetadata.Builder().isResetMode(false).build())) .build(); // Shouldn't throw @@ -340,10 +327,7 @@ public void testAddRemoveAllocationNodes() { TrainedModelAllocationClusterService trainedModelAllocationClusterService = createClusterService(); // Stopping shouldn't cause any updates - assertThatStoppingAllocationPreventsMutation( - trainedModelAllocationClusterService::addRemoveAllocationNodes, - currentState - ); + assertThatStoppingAllocationPreventsMutation(trainedModelAllocationClusterService::addRemoveAllocationNodes, currentState); ClusterState modified = trainedModelAllocationClusterService.addRemoveAllocationNodes(currentState); TrainedModelAllocationMetadata trainedModelAllocationMetadata = TrainedModelAllocationMetadata.fromState(modified); @@ -357,10 +341,7 @@ public void testAddRemoveAllocationNodes() { ); assertNodeState(trainedModelAllocationMetadata, "model-1", "ml-node-with-room", RoutingState.STARTED); assertNodeState(trainedModelAllocationMetadata, "model-1", "new-ml-node-with-room", RoutingState.STARTING); - assertThat( - trainedModelAllocationMetadata.modelAllocations().get("model-1").getAllocationState(), - equalTo(AllocationState.STARTED) - ); + assertThat(trainedModelAllocationMetadata.modelAllocations().get("model-1").getAllocationState(), equalTo(AllocationState.STARTED)); assertThat(trainedModelAllocationMetadata.getModelAllocation("model-2").getNodeRoutingTable().keySet(), hasSize(2)); assertThat( @@ -406,7 +387,8 @@ public void testAddRemoveAllocationNodesPrioritizesAllocationsWithFewerNodes() { "model-2", TrainedModelAllocation.Builder.empty(newParams("model-2", ByteSizeValue.ofGb(1).getBytes())) .addNewRoutingEntry("ml-node-with-room") - ).addNewAllocation( + ) + .addNewAllocation( "model-3", TrainedModelAllocation.Builder.empty(newParams("model-3", ByteSizeValue.ofGb(1).getBytes())) ) @@ -421,21 +403,12 @@ public void testAddRemoveAllocationNodesPrioritizesAllocationsWithFewerNodes() { assertThat(trainedModelAllocationMetadata.modelAllocations(), allOf(hasKey("model-1"), hasKey("model-2"), hasKey("model-3"))); assertThat(trainedModelAllocationMetadata.getModelAllocation("model-1").getNodeRoutingTable().keySet(), hasSize(1)); - assertThat( - trainedModelAllocationMetadata.getModelAllocation("model-1").getNodeRoutingTable(), - allOf(hasKey("ml-node-with-room")) - ); + assertThat(trainedModelAllocationMetadata.getModelAllocation("model-1").getNodeRoutingTable(), allOf(hasKey("ml-node-with-room"))); assertNodeState(trainedModelAllocationMetadata, "model-1", "ml-node-with-room", RoutingState.STARTED); - assertThat( - trainedModelAllocationMetadata.modelAllocations().get("model-1").getAllocationState(), - equalTo(AllocationState.STARTED) - ); + assertThat(trainedModelAllocationMetadata.modelAllocations().get("model-1").getAllocationState(), equalTo(AllocationState.STARTED)); assertThat(trainedModelAllocationMetadata.getModelAllocation("model-2").getNodeRoutingTable().keySet(), hasSize(1)); - assertThat( - trainedModelAllocationMetadata.getModelAllocation("model-2").getNodeRoutingTable(), - allOf(hasKey("ml-node-with-room")) - ); + assertThat(trainedModelAllocationMetadata.getModelAllocation("model-2").getNodeRoutingTable(), allOf(hasKey("ml-node-with-room"))); assertNodeState(trainedModelAllocationMetadata, "model-2", "ml-node-with-room", RoutingState.STARTING); assertThat( trainedModelAllocationMetadata.modelAllocations().get("model-2").getAllocationState(), @@ -482,10 +455,7 @@ public void testAddRemoveAllocationNodes_GivenNodeThatReachedMaxOpenJobs() { ) .build() ) - .putCustom( - PersistentTasksCustomMetadata.TYPE, - persistentTasks - ) + .putCustom(PersistentTasksCustomMetadata.TYPE, persistentTasks) ) .build(); TrainedModelAllocationClusterService trainedModelAllocationClusterService = createClusterService(); @@ -495,15 +465,24 @@ public void testAddRemoveAllocationNodes_GivenNodeThatReachedMaxOpenJobs() { assertThat(trainedModelAllocationMetadata.modelAllocations().keySet(), contains("model-1")); assertThat(trainedModelAllocationMetadata.getModelAllocation("model-1").getNodeRoutingTable().keySet(), hasSize(1)); - assertThat(trainedModelAllocationMetadata.getModelAllocation("model-1").getNodeRoutingTable().keySet(), - contains("ml-node-no-load")); - assertThat(trainedModelAllocationMetadata.getModelAllocation("model-1").getNodeRoutingTable().get("ml-node-no-load").getState(), - equalTo(RoutingState.STARTED)); + assertThat( + trainedModelAllocationMetadata.getModelAllocation("model-1").getNodeRoutingTable().keySet(), + contains("ml-node-no-load") + ); + assertThat( + trainedModelAllocationMetadata.getModelAllocation("model-1").getNodeRoutingTable().get("ml-node-no-load").getState(), + equalTo(RoutingState.STARTED) + ); TrainedModelAllocation allocation = trainedModelAllocationMetadata.getModelAllocation("model-1"); - assertThat(allocation.getReason().get(), equalTo("Not allocating on node [ml-node-full-load]." + - " Reason: This node is full. Number of opened jobs and allocated native inference processes [512], " + - "xpack.ml.max_open_jobs [512].")); + assertThat( + allocation.getReason().get(), + equalTo( + "Not allocating on node [ml-node-full-load]." + + " Reason: This node is full. Number of opened jobs and allocated native inference processes [512], " + + "xpack.ml.max_open_jobs [512]." + ) + ); } public void testShouldAllocateModels() { diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/allocation/TrainedModelAllocationMetadataTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/allocation/TrainedModelAllocationMetadataTests.java index ccb9b27be591e..35b75f96f12bc 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/allocation/TrainedModelAllocationMetadataTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/allocation/TrainedModelAllocationMetadataTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.xpack.ml.inference.allocation; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.StartTrainedModelDeploymentAction; import org.elasticsearch.xpack.core.ml.inference.allocation.TrainedModelAllocation; import org.elasticsearch.xpack.core.ml.inference.allocation.TrainedModelAllocationTests; @@ -65,8 +65,7 @@ public void testBuilderChanged_WhenAddingRemovingModel() { public void testBuilderChangedWhenAllocationChanged() { String allocatedModelId = "test_model_id"; TrainedModelAllocationMetadata.Builder builder = TrainedModelAllocationMetadata.Builder.fromMetadata( - TrainedModelAllocationMetadata.Builder - .empty() + TrainedModelAllocationMetadata.Builder.empty() .addNewAllocation(allocatedModelId, TrainedModelAllocation.Builder.empty(randomParams(allocatedModelId))) .build() ); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/allocation/TrainedModelAllocationNodeServiceTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/allocation/TrainedModelAllocationNodeServiceTests.java index 0d69517d86b07..eb646b099d465 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/allocation/TrainedModelAllocationNodeServiceTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/allocation/TrainedModelAllocationNodeServiceTests.java @@ -356,8 +356,7 @@ public void testClusterChanged() throws Exception { ) .addNewAllocation( modelTwo, - TrainedModelAllocation.Builder - .empty(newParams(modelTwo)) + TrainedModelAllocation.Builder.empty(newParams(modelTwo)) .addNewRoutingEntry(NODE_ID) .updateExistingRoutingEntry( NODE_ID, @@ -366,10 +365,10 @@ public void testClusterChanged() throws Exception { randomAlphaOfLength(10) ) ) - ).addNewAllocation( + ) + .addNewAllocation( previouslyUsedModel, - TrainedModelAllocation.Builder - .empty(newParams(modelTwo)) + TrainedModelAllocation.Builder.empty(newParams(modelTwo)) .addNewRoutingEntry(NODE_ID) .updateExistingRoutingEntry( NODE_ID, diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/deployment/DeploymentManagerTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/deployment/DeploymentManagerTests.java index aedc03cf0e0de..775b3f305fe08 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/deployment/DeploymentManagerTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/deployment/DeploymentManagerTests.java @@ -36,8 +36,8 @@ public class DeploymentManagerTests extends ESTestCase { public void managerSetup() { tp = new TestThreadPool( "DeploymentManagerTests", - new ScalingExecutorBuilder(UTILITY_THREAD_POOL_NAME,1, 4, TimeValue.timeValueMinutes(10), "xpack.ml.utility_thread_pool"), - new ScalingExecutorBuilder(JOB_COMMS_THREAD_POOL_NAME,1, 4, TimeValue.timeValueMinutes(10), "xpack.ml.job_comms_thread_pool") + new ScalingExecutorBuilder(UTILITY_THREAD_POOL_NAME, 1, 4, TimeValue.timeValueMinutes(10), "xpack.ml.utility_thread_pool"), + new ScalingExecutorBuilder(JOB_COMMS_THREAD_POOL_NAME, 1, 4, TimeValue.timeValueMinutes(10), "xpack.ml.job_comms_thread_pool") ); } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/deployment/PyTorchResultTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/deployment/PyTorchResultTests.java index 6cf117ebd149b..d7fd2b3c61b64 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/deployment/PyTorchResultTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/deployment/PyTorchResultTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.xpack.ml.inference.deployment; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -34,10 +34,10 @@ protected PyTorchResult createTestInstance() { int rows = randomIntBetween(1, 10); int columns = randomIntBetween(1, 10); int depth = randomIntBetween(1, 10); - double [][][] arr = new double[rows][columns][depth]; - for (int i=0; i(Arrays.asList(InferenceProcessor.MAX_INFERENCE_PROCESSORS, - MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING, - OperationRouting.USE_ADAPTIVE_REPLICA_SELECTION_SETTING, - ClusterService.USER_DEFINED_METADATA, - ClusterApplierService.CLUSTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING))); + ClusterSettings clusterSettings = new ClusterSettings( + settings, + new HashSet<>( + Arrays.asList( + InferenceProcessor.MAX_INFERENCE_PROCESSORS, + MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING, + OperationRouting.USE_ADAPTIVE_REPLICA_SELECTION_SETTING, + ClusterService.USER_DEFINED_METADATA, + ClusterApplierService.CLUSTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING + ) + ) + ); clusterService = new ClusterService(settings, clusterSettings, tp); } public void testNumInferenceProcessors() throws Exception { Metadata metadata = null; - InferenceProcessor.Factory processorFactory = new InferenceProcessor.Factory(client, - clusterService, - Settings.EMPTY); + InferenceProcessor.Factory processorFactory = new InferenceProcessor.Factory(client, clusterService, Settings.EMPTY); processorFactory.accept(buildClusterState(metadata)); assertThat(processorFactory.numInferenceProcessors(), equalTo(0)); @@ -96,44 +100,46 @@ public void testNumInferenceProcessors() throws Exception { public void testNumInferenceProcessorsRecursivelyDefined() throws Exception { Metadata metadata = null; - InferenceProcessor.Factory processorFactory = new InferenceProcessor.Factory(client, - clusterService, - Settings.EMPTY); + InferenceProcessor.Factory processorFactory = new InferenceProcessor.Factory(client, clusterService, Settings.EMPTY); processorFactory.accept(buildClusterState(metadata)); Map configurations = new HashMap<>(); - configurations.put("pipeline_with_model_top_level", - randomBoolean() ? - newConfigurationWithInferenceProcessor("top_level") : - newConfigurationWithForeachProcessorProcessor("top_level")); - try(XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().map(Collections.singletonMap("processors", - Collections.singletonList( - Collections.singletonMap("set", - new HashMap<>() {{ + configurations.put( + "pipeline_with_model_top_level", + randomBoolean() + ? newConfigurationWithInferenceProcessor("top_level") + : newConfigurationWithForeachProcessorProcessor("top_level") + ); + try ( + XContentBuilder xContentBuilder = XContentFactory.jsonBuilder() + .map(Collections.singletonMap("processors", Collections.singletonList(Collections.singletonMap("set", new HashMap<>() { + { put("field", "foo"); put("value", "bar"); - put("on_failure", - Arrays.asList( - inferenceProcessorForModel("second_level"), - forEachProcessorWithInference("third_level"))); - }}))))) { - configurations.put("pipeline_with_model_nested", - new PipelineConfiguration("pipeline_with_model_nested", BytesReference.bytes(xContentBuilder), XContentType.JSON)); + put( + "on_failure", + Arrays.asList(inferenceProcessorForModel("second_level"), forEachProcessorWithInference("third_level")) + ); + } + })))) + ) { + configurations.put( + "pipeline_with_model_nested", + new PipelineConfiguration("pipeline_with_model_nested", BytesReference.bytes(xContentBuilder), XContentType.JSON) + ); } IngestMetadata ingestMetadata = new IngestMetadata(configurations); ClusterState cs = ClusterState.builder(new ClusterName("_name")) .metadata(Metadata.builder().putCustom(IngestMetadata.TYPE, ingestMetadata)) - .nodes(DiscoveryNodes.builder() - .add(new DiscoveryNode("min_node", - new TransportAddress(InetAddress.getLoopbackAddress(), 9300), - Version.CURRENT)) - .add(new DiscoveryNode("current_node", - new TransportAddress(InetAddress.getLoopbackAddress(), 9302), - Version.CURRENT)) - .localNodeId("_node_id") - .masterNodeId("_node_id")) + .nodes( + DiscoveryNodes.builder() + .add(new DiscoveryNode("min_node", new TransportAddress(InetAddress.getLoopbackAddress(), 9300), Version.CURRENT)) + .add(new DiscoveryNode("current_node", new TransportAddress(InetAddress.getLoopbackAddress(), 9302), Version.CURRENT)) + .localNodeId("_node_id") + .masterNodeId("_node_id") + ) .build(); processorFactory.accept(cs); @@ -145,96 +151,130 @@ public void testNumInferenceWhenLevelExceedsMaxRecurions() { } public void testCreateProcessorWithTooManyExisting() throws Exception { - InferenceProcessor.Factory processorFactory = new InferenceProcessor.Factory(client, + InferenceProcessor.Factory processorFactory = new InferenceProcessor.Factory( + client, clusterService, - Settings.builder().put(InferenceProcessor.MAX_INFERENCE_PROCESSORS.getKey(), 1).build()); + Settings.builder().put(InferenceProcessor.MAX_INFERENCE_PROCESSORS.getKey(), 1).build() + ); processorFactory.accept(buildClusterStateWithModelReferences("model1")); - ElasticsearchStatusException ex = expectThrows(ElasticsearchStatusException.class, - () -> processorFactory.create(Collections.emptyMap(), "my_inference_processor", null, Collections.emptyMap())); - - assertThat(ex.getMessage(), equalTo("Max number of inference processors reached, total inference processors [1]. " + - "Adjust the setting [xpack.ml.max_inference_processors]: [1] if a greater number is desired.")); + ElasticsearchStatusException ex = expectThrows( + ElasticsearchStatusException.class, + () -> processorFactory.create(Collections.emptyMap(), "my_inference_processor", null, Collections.emptyMap()) + ); + + assertThat( + ex.getMessage(), + equalTo( + "Max number of inference processors reached, total inference processors [1]. " + + "Adjust the setting [xpack.ml.max_inference_processors]: [1] if a greater number is desired." + ) + ); } public void testCreateProcessorWithInvalidInferenceConfig() { - InferenceProcessor.Factory processorFactory = new InferenceProcessor.Factory(client, - clusterService, - Settings.EMPTY); - - Map config = new HashMap<>() {{ - put(InferenceProcessor.FIELD_MAP, Collections.emptyMap()); - put(InferenceResults.MODEL_ID_RESULTS_FIELD, "my_model"); - put(InferenceProcessor.TARGET_FIELD, "result"); - put(InferenceProcessor.INFERENCE_CONFIG, Collections.singletonMap("unknown_type", Collections.emptyMap())); - }}; - - ElasticsearchStatusException ex = expectThrows(ElasticsearchStatusException.class, - () -> processorFactory.create(Collections.emptyMap(), "my_inference_processor", null, config)); - assertThat(ex.getMessage(), - equalTo("unrecognized inference configuration type [unknown_type]. Supported types [classification, regression]")); - - Map config2 = new HashMap<>() {{ - put(InferenceProcessor.FIELD_MAP, Collections.emptyMap()); - put(InferenceResults.MODEL_ID_RESULTS_FIELD, "my_model"); - put(InferenceProcessor.TARGET_FIELD, "result"); - put(InferenceProcessor.INFERENCE_CONFIG, Collections.singletonMap("regression", "boom")); - }}; - ex = expectThrows(ElasticsearchStatusException.class, - () -> processorFactory.create(Collections.emptyMap(), "my_inference_processor", null, config2)); - assertThat(ex.getMessage(), - equalTo("inference_config must be an object with one inference type mapped to an object.")); - - Map config3 = new HashMap<>() {{ - put(InferenceProcessor.FIELD_MAP, Collections.emptyMap()); - put(InferenceResults.MODEL_ID_RESULTS_FIELD, "my_model"); - put(InferenceProcessor.TARGET_FIELD, "result"); - put(InferenceProcessor.INFERENCE_CONFIG, Collections.emptyMap()); - }}; - ex = expectThrows(ElasticsearchStatusException.class, - () -> processorFactory.create(Collections.emptyMap(), "my_inference_processor", null, config3)); - assertThat(ex.getMessage(), - equalTo("inference_config must be an object with one inference type mapped to an object.")); + InferenceProcessor.Factory processorFactory = new InferenceProcessor.Factory(client, clusterService, Settings.EMPTY); + + Map config = new HashMap<>() { + { + put(InferenceProcessor.FIELD_MAP, Collections.emptyMap()); + put(InferenceResults.MODEL_ID_RESULTS_FIELD, "my_model"); + put(InferenceProcessor.TARGET_FIELD, "result"); + put(InferenceProcessor.INFERENCE_CONFIG, Collections.singletonMap("unknown_type", Collections.emptyMap())); + } + }; + + ElasticsearchStatusException ex = expectThrows( + ElasticsearchStatusException.class, + () -> processorFactory.create(Collections.emptyMap(), "my_inference_processor", null, config) + ); + assertThat( + ex.getMessage(), + equalTo("unrecognized inference configuration type [unknown_type]. Supported types [classification, regression]") + ); + + Map config2 = new HashMap<>() { + { + put(InferenceProcessor.FIELD_MAP, Collections.emptyMap()); + put(InferenceResults.MODEL_ID_RESULTS_FIELD, "my_model"); + put(InferenceProcessor.TARGET_FIELD, "result"); + put(InferenceProcessor.INFERENCE_CONFIG, Collections.singletonMap("regression", "boom")); + } + }; + ex = expectThrows( + ElasticsearchStatusException.class, + () -> processorFactory.create(Collections.emptyMap(), "my_inference_processor", null, config2) + ); + assertThat(ex.getMessage(), equalTo("inference_config must be an object with one inference type mapped to an object.")); + + Map config3 = new HashMap<>() { + { + put(InferenceProcessor.FIELD_MAP, Collections.emptyMap()); + put(InferenceResults.MODEL_ID_RESULTS_FIELD, "my_model"); + put(InferenceProcessor.TARGET_FIELD, "result"); + put(InferenceProcessor.INFERENCE_CONFIG, Collections.emptyMap()); + } + }; + ex = expectThrows( + ElasticsearchStatusException.class, + () -> processorFactory.create(Collections.emptyMap(), "my_inference_processor", null, config3) + ); + assertThat(ex.getMessage(), equalTo("inference_config must be an object with one inference type mapped to an object.")); } public void testCreateProcessorWithTooOldMinNodeVersion() throws IOException { - InferenceProcessor.Factory processorFactory = new InferenceProcessor.Factory(client, - clusterService, - Settings.EMPTY); + InferenceProcessor.Factory processorFactory = new InferenceProcessor.Factory(client, clusterService, Settings.EMPTY); processorFactory.accept(builderClusterStateWithModelReferences(Version.V_7_5_0, "model1")); - Map regression = new HashMap<>() {{ - put(InferenceProcessor.FIELD_MAP, Collections.emptyMap()); - put(InferenceResults.MODEL_ID_RESULTS_FIELD, "my_model"); - put(InferenceProcessor.TARGET_FIELD, "result"); - put(InferenceProcessor.INFERENCE_CONFIG, - Collections.singletonMap(RegressionConfig.NAME.getPreferredName(), Collections.emptyMap())); - }}; - - ElasticsearchException ex = expectThrows(ElasticsearchException.class, - () -> processorFactory.create(Collections.emptyMap(), "my_inference_processor", null, regression)); - assertThat(ex.getMessage(), - equalTo("Configuration [regression] requires minimum node version [7.6.0] (current minimum node version [7.5.0]")); - - Map classification = new HashMap<>() {{ - put(InferenceProcessor.FIELD_MAP, Collections.emptyMap()); - put(InferenceResults.MODEL_ID_RESULTS_FIELD, "my_model"); - put(InferenceProcessor.TARGET_FIELD, "result"); - put(InferenceProcessor.INFERENCE_CONFIG, Collections.singletonMap(ClassificationConfig.NAME.getPreferredName(), - Collections.singletonMap(ClassificationConfig.NUM_TOP_CLASSES.getPreferredName(), 1))); - }}; - - ex = expectThrows(ElasticsearchException.class, - () -> processorFactory.create(Collections.emptyMap(), "my_inference_processor", null, classification)); - assertThat(ex.getMessage(), - equalTo("Configuration [classification] requires minimum node version [7.6.0] (current minimum node version [7.5.0]")); + Map regression = new HashMap<>() { + { + put(InferenceProcessor.FIELD_MAP, Collections.emptyMap()); + put(InferenceResults.MODEL_ID_RESULTS_FIELD, "my_model"); + put(InferenceProcessor.TARGET_FIELD, "result"); + put( + InferenceProcessor.INFERENCE_CONFIG, + Collections.singletonMap(RegressionConfig.NAME.getPreferredName(), Collections.emptyMap()) + ); + } + }; + + ElasticsearchException ex = expectThrows( + ElasticsearchException.class, + () -> processorFactory.create(Collections.emptyMap(), "my_inference_processor", null, regression) + ); + assertThat( + ex.getMessage(), + equalTo("Configuration [regression] requires minimum node version [7.6.0] (current minimum node version [7.5.0]") + ); + + Map classification = new HashMap<>() { + { + put(InferenceProcessor.FIELD_MAP, Collections.emptyMap()); + put(InferenceResults.MODEL_ID_RESULTS_FIELD, "my_model"); + put(InferenceProcessor.TARGET_FIELD, "result"); + put( + InferenceProcessor.INFERENCE_CONFIG, + Collections.singletonMap( + ClassificationConfig.NAME.getPreferredName(), + Collections.singletonMap(ClassificationConfig.NUM_TOP_CLASSES.getPreferredName(), 1) + ) + ); + } + }; + + ex = expectThrows( + ElasticsearchException.class, + () -> processorFactory.create(Collections.emptyMap(), "my_inference_processor", null, classification) + ); + assertThat( + ex.getMessage(), + equalTo("Configuration [classification] requires minimum node version [7.6.0] (current minimum node version [7.5.0]") + ); } public void testCreateProcessorWithTooOldMinNodeVersionNlp() throws IOException { - InferenceProcessor.Factory processorFactory = new InferenceProcessor.Factory(client, - clusterService, - Settings.EMPTY); + InferenceProcessor.Factory processorFactory = new InferenceProcessor.Factory(client, clusterService, Settings.EMPTY); processorFactory.accept(builderClusterStateWithModelReferences(Version.V_7_5_0, "model1")); for (String name : List.of( @@ -251,77 +291,96 @@ public void testCreateProcessorWithTooOldMinNodeVersionNlp() throws IOException ); assertThat( ex.getMessage(), - equalTo("Configuration [" + name +"] requires minimum node version [8.0.0] (current minimum node version [7.5.0]")); + equalTo("Configuration [" + name + "] requires minimum node version [8.0.0] (current minimum node version [7.5.0]") + ); } } public void testCreateProcessorWithEmptyConfigNotSupportedOnOldNode() throws IOException { - InferenceProcessor.Factory processorFactory = new InferenceProcessor.Factory(client, - clusterService, - Settings.EMPTY); + InferenceProcessor.Factory processorFactory = new InferenceProcessor.Factory(client, clusterService, Settings.EMPTY); processorFactory.accept(builderClusterStateWithModelReferences(Version.V_7_5_0, "model1")); - Map minimalConfig = new HashMap<>() {{ - put(InferenceResults.MODEL_ID_RESULTS_FIELD, "my_model"); - put(InferenceProcessor.TARGET_FIELD, "result"); - }}; - - ElasticsearchException ex = expectThrows(ElasticsearchException.class, - () -> processorFactory.create(Collections.emptyMap(), "my_inference_processor", null, minimalConfig)); + Map minimalConfig = new HashMap<>() { + { + put(InferenceResults.MODEL_ID_RESULTS_FIELD, "my_model"); + put(InferenceProcessor.TARGET_FIELD, "result"); + } + }; + + ElasticsearchException ex = expectThrows( + ElasticsearchException.class, + () -> processorFactory.create(Collections.emptyMap(), "my_inference_processor", null, minimalConfig) + ); assertThat(ex.getMessage(), equalTo("[inference_config] required property is missing")); } public void testCreateProcessor() { - InferenceProcessor.Factory processorFactory = new InferenceProcessor.Factory(client, - clusterService, - Settings.EMPTY); + InferenceProcessor.Factory processorFactory = new InferenceProcessor.Factory(client, clusterService, Settings.EMPTY); - Map regression = new HashMap<>() {{ - put(InferenceProcessor.FIELD_MAP, Collections.emptyMap()); - put(InferenceResults.MODEL_ID_RESULTS_FIELD, "my_model"); - put(InferenceProcessor.TARGET_FIELD, "result"); - put(InferenceProcessor.INFERENCE_CONFIG, - Collections.singletonMap(RegressionConfig.NAME.getPreferredName(), Collections.emptyMap())); - }}; + Map regression = new HashMap<>() { + { + put(InferenceProcessor.FIELD_MAP, Collections.emptyMap()); + put(InferenceResults.MODEL_ID_RESULTS_FIELD, "my_model"); + put(InferenceProcessor.TARGET_FIELD, "result"); + put( + InferenceProcessor.INFERENCE_CONFIG, + Collections.singletonMap(RegressionConfig.NAME.getPreferredName(), Collections.emptyMap()) + ); + } + }; processorFactory.create(Collections.emptyMap(), "my_inference_processor", null, regression); - - Map classification = new HashMap<>() {{ - put(InferenceProcessor.FIELD_MAP, Collections.emptyMap()); - put(InferenceResults.MODEL_ID_RESULTS_FIELD, "my_model"); - put(InferenceProcessor.TARGET_FIELD, "result"); - put(InferenceProcessor.INFERENCE_CONFIG, Collections.singletonMap(ClassificationConfig.NAME.getPreferredName(), - Collections.singletonMap(ClassificationConfig.NUM_TOP_CLASSES.getPreferredName(), 1))); - }}; + Map classification = new HashMap<>() { + { + put(InferenceProcessor.FIELD_MAP, Collections.emptyMap()); + put(InferenceResults.MODEL_ID_RESULTS_FIELD, "my_model"); + put(InferenceProcessor.TARGET_FIELD, "result"); + put( + InferenceProcessor.INFERENCE_CONFIG, + Collections.singletonMap( + ClassificationConfig.NAME.getPreferredName(), + Collections.singletonMap(ClassificationConfig.NUM_TOP_CLASSES.getPreferredName(), 1) + ) + ); + } + }; processorFactory.create(Collections.emptyMap(), "my_inference_processor", null, classification); - Map mininmal = new HashMap<>() {{ - put(InferenceResults.MODEL_ID_RESULTS_FIELD, "my_model"); - put(InferenceProcessor.TARGET_FIELD, "result"); - }}; + Map mininmal = new HashMap<>() { + { + put(InferenceResults.MODEL_ID_RESULTS_FIELD, "my_model"); + put(InferenceProcessor.TARGET_FIELD, "result"); + } + }; processorFactory.create(Collections.emptyMap(), "my_inference_processor", null, mininmal); } public void testCreateProcessorWithDuplicateFields() { - InferenceProcessor.Factory processorFactory = new InferenceProcessor.Factory(client, - clusterService, - Settings.EMPTY); - - Map regression = new HashMap<>() {{ - put(InferenceProcessor.FIELD_MAP, Collections.emptyMap()); - put(InferenceResults.MODEL_ID_RESULTS_FIELD, "my_model"); - put(InferenceProcessor.TARGET_FIELD, "ml"); - put(InferenceProcessor.INFERENCE_CONFIG, Collections.singletonMap(RegressionConfig.NAME.getPreferredName(), - Collections.singletonMap(RegressionConfig.RESULTS_FIELD.getPreferredName(), "warning"))); - }}; - - Exception ex = expectThrows(Exception.class, () -> - processorFactory.create(Collections.emptyMap(), "my_inference_processor", null, regression)); - assertThat(ex.getMessage(), equalTo("Invalid inference config. " + - "More than one field is configured as [warning]")); + InferenceProcessor.Factory processorFactory = new InferenceProcessor.Factory(client, clusterService, Settings.EMPTY); + + Map regression = new HashMap<>() { + { + put(InferenceProcessor.FIELD_MAP, Collections.emptyMap()); + put(InferenceResults.MODEL_ID_RESULTS_FIELD, "my_model"); + put(InferenceProcessor.TARGET_FIELD, "ml"); + put( + InferenceProcessor.INFERENCE_CONFIG, + Collections.singletonMap( + RegressionConfig.NAME.getPreferredName(), + Collections.singletonMap(RegressionConfig.RESULTS_FIELD.getPreferredName(), "warning") + ) + ); + } + }; + + Exception ex = expectThrows( + Exception.class, + () -> processorFactory.create(Collections.emptyMap(), "my_inference_processor", null, regression) + ); + assertThat(ex.getMessage(), equalTo("Invalid inference config. " + "More than one field is configured as [warning]")); } public void testParseFromMap() { @@ -341,7 +400,7 @@ public void testParseFromMap() { } private static ClusterState buildClusterState(Metadata metadata) { - return ClusterState.builder(new ClusterName("_name")).metadata(metadata).build(); + return ClusterState.builder(new ClusterName("_name")).metadata(metadata).build(); } private static ClusterState buildClusterStateWithModelReferences(String... modelId) throws IOException { @@ -351,56 +410,64 @@ private static ClusterState buildClusterStateWithModelReferences(String... model private static ClusterState builderClusterStateWithModelReferences(Version minNodeVersion, String... modelId) throws IOException { Map configurations = new HashMap<>(modelId.length); for (String id : modelId) { - configurations.put("pipeline_with_model_" + id, - randomBoolean() ? newConfigurationWithInferenceProcessor(id) : newConfigurationWithForeachProcessorProcessor(id)); + configurations.put( + "pipeline_with_model_" + id, + randomBoolean() ? newConfigurationWithInferenceProcessor(id) : newConfigurationWithForeachProcessorProcessor(id) + ); } IngestMetadata ingestMetadata = new IngestMetadata(configurations); return ClusterState.builder(new ClusterName("_name")) .metadata(Metadata.builder().putCustom(IngestMetadata.TYPE, ingestMetadata)) - .nodes(DiscoveryNodes.builder() - .add(new DiscoveryNode("min_node", - new TransportAddress(InetAddress.getLoopbackAddress(), 9300), - minNodeVersion)) - .add(new DiscoveryNode("current_node", - new TransportAddress(InetAddress.getLoopbackAddress(), 9302), - Version.CURRENT)) - .localNodeId("_node_id") - .masterNodeId("_node_id")) + .nodes( + DiscoveryNodes.builder() + .add(new DiscoveryNode("min_node", new TransportAddress(InetAddress.getLoopbackAddress(), 9300), minNodeVersion)) + .add(new DiscoveryNode("current_node", new TransportAddress(InetAddress.getLoopbackAddress(), 9302), Version.CURRENT)) + .localNodeId("_node_id") + .masterNodeId("_node_id") + ) .build(); } private static PipelineConfiguration newConfigurationWithInferenceProcessor(String modelId) throws IOException { - try(XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().map(Collections.singletonMap("processors", - Collections.singletonList(inferenceProcessorForModel(modelId))))) { + try ( + XContentBuilder xContentBuilder = XContentFactory.jsonBuilder() + .map(Collections.singletonMap("processors", Collections.singletonList(inferenceProcessorForModel(modelId)))) + ) { return new PipelineConfiguration("pipeline_with_model_" + modelId, BytesReference.bytes(xContentBuilder), XContentType.JSON); } } private static PipelineConfiguration newConfigurationWithForeachProcessorProcessor(String modelId) throws IOException { - try(XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().map(Collections.singletonMap("processors", - Collections.singletonList(forEachProcessorWithInference(modelId))))) { + try ( + XContentBuilder xContentBuilder = XContentFactory.jsonBuilder() + .map(Collections.singletonMap("processors", Collections.singletonList(forEachProcessorWithInference(modelId)))) + ) { return new PipelineConfiguration("pipeline_with_model_" + modelId, BytesReference.bytes(xContentBuilder), XContentType.JSON); } } private static Map forEachProcessorWithInference(String modelId) { - return Collections.singletonMap("foreach", - new HashMap<>() {{ + return Collections.singletonMap("foreach", new HashMap<>() { + { put("field", "foo"); put("processor", inferenceProcessorForModel(modelId)); - }}); + } + }); } private static Map inferenceProcessorForModel(String modelId) { - return Collections.singletonMap(InferenceProcessor.TYPE, - new HashMap<>() {{ + return Collections.singletonMap(InferenceProcessor.TYPE, new HashMap<>() { + { put(InferenceResults.MODEL_ID_RESULTS_FIELD, modelId); - put(InferenceProcessor.INFERENCE_CONFIG, - Collections.singletonMap(RegressionConfig.NAME.getPreferredName(), Collections.emptyMap())); + put( + InferenceProcessor.INFERENCE_CONFIG, + Collections.singletonMap(RegressionConfig.NAME.getPreferredName(), Collections.emptyMap()) + ); put(InferenceProcessor.TARGET_FIELD, "new_field"); put(InferenceProcessor.FIELD_MAP, Collections.singletonMap("source", "dest")); - }}); + } + }); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/ingest/InferenceProcessorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/ingest/InferenceProcessorTests.java index ca738ebc834a5..76cc0ee66f5e3 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/ingest/InferenceProcessorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/ingest/InferenceProcessorTests.java @@ -54,32 +54,34 @@ public void setUpVariables() { public void testMutateDocumentWithClassification() { String targetField = "ml.my_processor"; - InferenceProcessor inferenceProcessor = new InferenceProcessor(client, + InferenceProcessor inferenceProcessor = new InferenceProcessor( + client, auditor, "my_processor", - null, targetField, + null, + targetField, "classification_model", ClassificationConfigUpdate.EMPTY_PARAMS, - Collections.emptyMap()); + Collections.emptyMap() + ); Map source = new HashMap<>(); Map ingestMetadata = new HashMap<>(); IngestDocument document = new IngestDocument(source, ingestMetadata); InternalInferModelAction.Response response = new InternalInferModelAction.Response( - Collections.singletonList(new ClassificationInferenceResults(1.0, - "foo", - null, - Collections.emptyList(), - ClassificationConfig.EMPTY_PARAMS, - 1.0, - 1.0)), + Collections.singletonList( + new ClassificationInferenceResults(1.0, "foo", null, Collections.emptyList(), ClassificationConfig.EMPTY_PARAMS, 1.0, 1.0) + ), null, - true); + true + ); inferenceProcessor.mutateDocument(response, document); - assertThat(document.getFieldValue(targetField + "." + ClassificationConfig.EMPTY_PARAMS.getResultsField(), String.class), - equalTo("foo")); + assertThat( + document.getFieldValue(targetField + "." + ClassificationConfig.EMPTY_PARAMS.getResultsField(), String.class), + equalTo("foo") + ); assertThat(document.getFieldValue("ml.my_processor.model_id", String.class), equalTo("classification_model")); } @@ -87,13 +89,16 @@ public void testMutateDocumentWithClassification() { public void testMutateDocumentClassificationTopNClasses() { ClassificationConfigUpdate classificationConfigUpdate = new ClassificationConfigUpdate(2, null, null, null, null); ClassificationConfig classificationConfig = new ClassificationConfig(2, null, null, null, PredictionFieldType.STRING); - InferenceProcessor inferenceProcessor = new InferenceProcessor(client, + InferenceProcessor inferenceProcessor = new InferenceProcessor( + client, auditor, "my_processor", - null, "ml.my_processor", + null, + "ml.my_processor", "classification_model", classificationConfigUpdate, - Collections.emptyMap()); + Collections.emptyMap() + ); Map source = new HashMap<>(); Map ingestMetadata = new HashMap<>(); @@ -104,19 +109,18 @@ public void testMutateDocumentClassificationTopNClasses() { classes.add(new TopClassEntry("bar", 0.4, 0.4)); InternalInferModelAction.Response response = new InternalInferModelAction.Response( - Collections.singletonList(new ClassificationInferenceResults(1.0, - "foo", - classes, - Collections.emptyList(), - classificationConfig, - 0.6, - 0.6)), + Collections.singletonList( + new ClassificationInferenceResults(1.0, "foo", classes, Collections.emptyList(), classificationConfig, 0.6, 0.6) + ), null, - true); + true + ); inferenceProcessor.mutateDocument(response, document); - assertThat((List>)document.getFieldValue("ml.my_processor.top_classes", List.class), - contains(classes.stream().map(TopClassEntry::asValueMap).toArray(Map[]::new))); + assertThat( + (List>) document.getFieldValue("ml.my_processor.top_classes", List.class), + contains(classes.stream().map(TopClassEntry::asValueMap).toArray(Map[]::new)) + ); assertThat(document.getFieldValue("ml.my_processor.model_id", String.class), equalTo("classification_model")); assertThat(document.getFieldValue("ml.my_processor.predicted_value", String.class), equalTo("foo")); } @@ -124,13 +128,16 @@ public void testMutateDocumentClassificationTopNClasses() { public void testMutateDocumentClassificationFeatureInfluence() { ClassificationConfig classificationConfig = new ClassificationConfig(2, null, null, 2, PredictionFieldType.STRING); ClassificationConfigUpdate classificationConfigUpdate = new ClassificationConfigUpdate(2, null, null, 2, null); - InferenceProcessor inferenceProcessor = new InferenceProcessor(client, + InferenceProcessor inferenceProcessor = new InferenceProcessor( + client, auditor, "my_processor", - null, "ml.my_processor", + null, + "ml.my_processor", "classification_model", classificationConfigUpdate, - Collections.emptyMap()); + Collections.emptyMap() + ); Map source = new HashMap<>(); Map ingestMetadata = new HashMap<>(); @@ -141,21 +148,26 @@ public void testMutateDocumentClassificationFeatureInfluence() { classes.add(new TopClassEntry("bar", 0.4, 0.4)); List featureInfluence = new ArrayList<>(); - featureInfluence.add(new ClassificationFeatureImportance("feature_1", - Collections.singletonList(new ClassificationFeatureImportance.ClassImportance("class_a", 1.13)))); - featureInfluence.add(new ClassificationFeatureImportance("feature_2", - Collections.singletonList(new ClassificationFeatureImportance.ClassImportance("class_b", -42.0)))); + featureInfluence.add( + new ClassificationFeatureImportance( + "feature_1", + Collections.singletonList(new ClassificationFeatureImportance.ClassImportance("class_a", 1.13)) + ) + ); + featureInfluence.add( + new ClassificationFeatureImportance( + "feature_2", + Collections.singletonList(new ClassificationFeatureImportance.ClassImportance("class_b", -42.0)) + ) + ); InternalInferModelAction.Response response = new InternalInferModelAction.Response( - Collections.singletonList(new ClassificationInferenceResults(1.0, - "foo", - classes, - featureInfluence, - classificationConfig, - 0.6, - 0.6)), + Collections.singletonList( + new ClassificationInferenceResults(1.0, "foo", classes, featureInfluence, classificationConfig, 0.6, 0.6) + ), null, - true); + true + ); inferenceProcessor.mutateDocument(response, document); assertThat(document.getFieldValue("ml.my_processor.model_id", String.class), equalTo("classification_model")); @@ -172,13 +184,16 @@ public void testMutateDocumentClassificationFeatureInfluence() { public void testMutateDocumentClassificationTopNClassesWithSpecificField() { ClassificationConfig classificationConfig = new ClassificationConfig(2, "result", "tops", null, PredictionFieldType.STRING); ClassificationConfigUpdate classificationConfigUpdate = new ClassificationConfigUpdate(2, "result", "tops", null, null); - InferenceProcessor inferenceProcessor = new InferenceProcessor(client, + InferenceProcessor inferenceProcessor = new InferenceProcessor( + client, auditor, "my_processor", - null, "ml.my_processor", + null, + "ml.my_processor", "classification_model", classificationConfigUpdate, - Collections.emptyMap()); + Collections.emptyMap() + ); Map source = new HashMap<>(); Map ingestMetadata = new HashMap<>(); @@ -189,19 +204,18 @@ public void testMutateDocumentClassificationTopNClassesWithSpecificField() { classes.add(new TopClassEntry("bar", 0.4, 0.4)); InternalInferModelAction.Response response = new InternalInferModelAction.Response( - Collections.singletonList(new ClassificationInferenceResults(1.0, - "foo", - classes, - Collections.emptyList(), - classificationConfig, - 0.6, - 0.6)), + Collections.singletonList( + new ClassificationInferenceResults(1.0, "foo", classes, Collections.emptyList(), classificationConfig, 0.6, 0.6) + ), null, - true); + true + ); inferenceProcessor.mutateDocument(response, document); - assertThat((List>)document.getFieldValue("ml.my_processor.tops", List.class), - contains(classes.stream().map(TopClassEntry::asValueMap).toArray(Map[]::new))); + assertThat( + (List>) document.getFieldValue("ml.my_processor.tops", List.class), + contains(classes.stream().map(TopClassEntry::asValueMap).toArray(Map[]::new)) + ); assertThat(document.getFieldValue("ml.my_processor.model_id", String.class), equalTo("classification_model")); assertThat(document.getFieldValue("ml.my_processor.result", String.class), equalTo("foo")); } @@ -209,13 +223,16 @@ public void testMutateDocumentClassificationTopNClassesWithSpecificField() { public void testMutateDocumentRegression() { RegressionConfig regressionConfig = new RegressionConfig("foo"); RegressionConfigUpdate regressionConfigUpdate = new RegressionConfigUpdate("foo", null); - InferenceProcessor inferenceProcessor = new InferenceProcessor(client, + InferenceProcessor inferenceProcessor = new InferenceProcessor( + client, auditor, "my_processor", - null, "ml.my_processor", + null, + "ml.my_processor", "regression_model", regressionConfigUpdate, - Collections.emptyMap()); + Collections.emptyMap() + ); Map source = new HashMap<>(); Map ingestMetadata = new HashMap<>(); @@ -224,7 +241,8 @@ public void testMutateDocumentRegression() { InternalInferModelAction.Response response = new InternalInferModelAction.Response( Collections.singletonList(new RegressionInferenceResults(0.7, regressionConfig)), null, - true); + true + ); inferenceProcessor.mutateDocument(response, document); assertThat(document.getFieldValue("ml.my_processor.foo", Double.class), equalTo(0.7)); @@ -234,13 +252,16 @@ public void testMutateDocumentRegression() { public void testMutateDocumentRegressionWithTopFeatures() { RegressionConfig regressionConfig = new RegressionConfig("foo", 2); RegressionConfigUpdate regressionConfigUpdate = new RegressionConfigUpdate("foo", 2); - InferenceProcessor inferenceProcessor = new InferenceProcessor(client, + InferenceProcessor inferenceProcessor = new InferenceProcessor( + client, auditor, "my_processor", - null, "ml.my_processor", + null, + "ml.my_processor", "regression_model", regressionConfigUpdate, - Collections.emptyMap()); + Collections.emptyMap() + ); Map source = new HashMap<>(); Map ingestMetadata = new HashMap<>(); @@ -253,7 +274,8 @@ public void testMutateDocumentRegressionWithTopFeatures() { InternalInferModelAction.Response response = new InternalInferModelAction.Response( Collections.singletonList(new RegressionInferenceResults(0.7, regressionConfig, featureInfluence)), null, - true); + true + ); inferenceProcessor.mutateDocument(response, document); assertThat(document.getFieldValue("ml.my_processor.foo", Double.class), equalTo(0.7)); @@ -268,19 +290,24 @@ public void testGenerateRequestWithEmptyMapping() { String modelId = "model"; Integer topNClasses = randomBoolean() ? null : randomIntBetween(1, 10); - InferenceProcessor processor = new InferenceProcessor(client, + InferenceProcessor processor = new InferenceProcessor( + client, auditor, "my_processor", - null, "my_field", + null, + "my_field", modelId, new ClassificationConfigUpdate(topNClasses, null, null, null, null), - Collections.emptyMap()); - - Map source = new HashMap<>(){{ - put("value1", 1); - put("value2", 4); - put("categorical", "foo"); - }}; + Collections.emptyMap() + ); + + Map source = new HashMap<>() { + { + put("value1", 1); + put("value2", 4); + put("categorical", "foo"); + } + }; Map ingestMetadata = new HashMap<>(); IngestDocument document = new IngestDocument(source, ingestMetadata); @@ -298,36 +325,45 @@ public void testGenerateWithMapping() { String modelId = "model"; Integer topNClasses = randomBoolean() ? null : randomIntBetween(1, 10); - Map fieldMapping = new HashMap<>(5) {{ - put("value1", "new_value1"); - put("value2", "new_value2"); - put("categorical", "new_categorical"); - put("_ingest._value", "metafield"); - }}; - - InferenceProcessor processor = new InferenceProcessor(client, + Map fieldMapping = new HashMap<>(5) { + { + put("value1", "new_value1"); + put("value2", "new_value2"); + put("categorical", "new_categorical"); + put("_ingest._value", "metafield"); + } + }; + + InferenceProcessor processor = new InferenceProcessor( + client, auditor, "my_processor", - null, "my_field", + null, + "my_field", modelId, new ClassificationConfigUpdate(topNClasses, null, null, null, null), - fieldMapping); - - Map source = new HashMap<>(5){{ - put("value1", 1); - put("categorical", "foo"); - put("un_touched", "bar"); - }}; + fieldMapping + ); + + Map source = new HashMap<>(5) { + { + put("value1", 1); + put("categorical", "foo"); + put("un_touched", "bar"); + } + }; Map ingestMetadata = new HashMap<>(); IngestDocument document = new IngestDocument(source, ingestMetadata); - Map expectedMap = new HashMap<>(7) {{ - put("new_value1", 1); - put("value1", 1); - put("categorical", "foo"); - put("new_categorical", "foo"); - put("un_touched", "bar"); - }}; + Map expectedMap = new HashMap<>(7) { + { + put("new_value1", 1); + put("value1", 1); + put("categorical", "foo"); + put("new_categorical", "foo"); + put("un_touched", "bar"); + } + }; assertThat(processor.buildRequest(document).getObjectsToInfer().get(0), equalTo(expectedMap)); ingestMetadata = Collections.singletonMap("_value", "baz"); @@ -342,47 +378,59 @@ public void testGenerateWithMappingNestedFields() { String modelId = "model"; Integer topNClasses = randomBoolean() ? null : randomIntBetween(1, 10); - Map fieldMapping = new HashMap<>(5) {{ - put("value1.foo", "new_value1"); - put("value2", "new_value2"); - put("categorical.bar", "new_categorical"); - }}; + Map fieldMapping = new HashMap<>(5) { + { + put("value1.foo", "new_value1"); + put("value2", "new_value2"); + put("categorical.bar", "new_categorical"); + } + }; - InferenceProcessor processor = new InferenceProcessor(client, + InferenceProcessor processor = new InferenceProcessor( + client, auditor, "my_processor", - null, "my_field", + null, + "my_field", modelId, new ClassificationConfigUpdate(topNClasses, null, null, null, null), - fieldMapping); - - Map source = new HashMap<>(5){{ - put("value1", Collections.singletonMap("foo", 1)); - put("categorical.bar", "foo"); - put("un_touched", "bar"); - }}; + fieldMapping + ); + + Map source = new HashMap<>(5) { + { + put("value1", Collections.singletonMap("foo", 1)); + put("categorical.bar", "foo"); + put("un_touched", "bar"); + } + }; Map ingestMetadata = new HashMap<>(); IngestDocument document = new IngestDocument(source, ingestMetadata); - Map expectedMap = new HashMap<>(7) {{ - put("new_value1", 1); - put("value1", Collections.singletonMap("foo", 1)); - put("categorical.bar", "foo"); - put("new_categorical", "foo"); - put("un_touched", "bar"); - }}; + Map expectedMap = new HashMap<>(7) { + { + put("new_value1", 1); + put("value1", Collections.singletonMap("foo", 1)); + put("categorical.bar", "foo"); + put("new_categorical", "foo"); + put("un_touched", "bar"); + } + }; assertThat(processor.buildRequest(document).getObjectsToInfer().get(0), equalTo(expectedMap)); } public void testHandleResponseLicenseChanged() { String targetField = "regression_value"; - InferenceProcessor inferenceProcessor = new InferenceProcessor(client, + InferenceProcessor inferenceProcessor = new InferenceProcessor( + client, auditor, "my_processor", - null, targetField, + null, + targetField, "regression_model", RegressionConfigUpdate.EMPTY_PARAMS, - Collections.emptyMap()); + Collections.emptyMap() + ); Map source = new HashMap<>(); Map ingestMetadata = new HashMap<>(); @@ -393,7 +441,8 @@ public void testHandleResponseLicenseChanged() { InternalInferModelAction.Response response = new InternalInferModelAction.Response( Collections.singletonList(new RegressionInferenceResults(0.7, RegressionConfig.EMPTY_PARAMS)), null, - true); + true + ); inferenceProcessor.handleResponse(response, document, (doc, ex) -> { assertThat(doc, is(not(nullValue()))); assertThat(ex, is(nullValue())); @@ -404,7 +453,8 @@ public void testHandleResponseLicenseChanged() { response = new InternalInferModelAction.Response( Collections.singletonList(new RegressionInferenceResults(0.7, RegressionConfig.EMPTY_PARAMS)), null, - false); + false + ); inferenceProcessor.handleResponse(response, document, (doc, ex) -> { assertThat(doc, is(not(nullValue()))); @@ -423,20 +473,26 @@ public void testHandleResponseLicenseChanged() { public void testMutateDocumentWithWarningResult() { String targetField = "regression_value"; - InferenceProcessor inferenceProcessor = new InferenceProcessor(client, + InferenceProcessor inferenceProcessor = new InferenceProcessor( + client, auditor, "my_processor", - null, "ml", + null, + "ml", "regression_model", RegressionConfigUpdate.EMPTY_PARAMS, - Collections.emptyMap()); + Collections.emptyMap() + ); Map source = new HashMap<>(); Map ingestMetadata = new HashMap<>(); IngestDocument document = new IngestDocument(source, ingestMetadata); InternalInferModelAction.Response response = new InternalInferModelAction.Response( - Collections.singletonList(new WarningInferenceResults("something broke")), null, true); + Collections.singletonList(new WarningInferenceResults("something broke")), + null, + true + ); inferenceProcessor.mutateDocument(response, document); assertThat(document.hasField(targetField), is(false)); @@ -447,14 +503,16 @@ public void testMutateDocumentWithWarningResult() { public void testMutateDocumentWithModelIdResult() { String modelAlias = "special_model"; String modelId = "regression-123"; - InferenceProcessor inferenceProcessor = new InferenceProcessor(client, + InferenceProcessor inferenceProcessor = new InferenceProcessor( + client, auditor, "my_processor", null, "ml.my_processor", modelAlias, new RegressionConfigUpdate("foo", null), - Collections.emptyMap()); + Collections.emptyMap() + ); Map source = new HashMap<>(); Map ingestMetadata = new HashMap<>(); @@ -463,7 +521,8 @@ public void testMutateDocumentWithModelIdResult() { InternalInferModelAction.Response response = new InternalInferModelAction.Response( Collections.singletonList(new RegressionInferenceResults(0.7, new RegressionConfig("foo"))), modelId, - true); + true + ); inferenceProcessor.mutateDocument(response, document); assertThat(document.getFieldValue("ml.my_processor.foo", Double.class), equalTo(0.7)); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/loadingservice/LocalModelTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/loadingservice/LocalModelTests.java index 1794b3fa9dd74..c46cbcecca037 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/loadingservice/LocalModelTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/loadingservice/LocalModelTests.java @@ -72,7 +72,8 @@ public void testClassificationInfer() throws Exception { .setTrainedModel(buildClassificationInference(false)) .build(); - LocalModel model = new LocalModel(modelId, + LocalModel model = new LocalModel( + modelId, "test-node", definition, new TrainedModelInput(inputFields), @@ -80,20 +81,26 @@ public void testClassificationInfer() throws Exception { ClassificationConfig.EMPTY_PARAMS, randomFrom(License.OperationMode.values()), modelStatsService, - mock(CircuitBreaker.class)); - Map fields = new HashMap<>() {{ - put("field.foo", 1.0); - put("field", Collections.singletonMap("bar", 0.5)); - put("categorical", "dog"); - }}; + mock(CircuitBreaker.class) + ); + Map fields = new HashMap<>() { + { + put("field.foo", 1.0); + put("field", Collections.singletonMap("bar", 0.5)); + put("categorical", "dog"); + } + }; SingleValueInferenceResults result = getSingleValue(model, fields, ClassificationConfigUpdate.EMPTY_PARAMS); assertThat(result.value(), equalTo(0.0)); assertThat(result.valueAsString(), is("0")); assertThat(model.getLatestStatsAndReset().getInferenceCount(), equalTo(1L)); - ClassificationInferenceResults classificationResult = - (ClassificationInferenceResults)getSingleValue(model, fields, new ClassificationConfigUpdate(1, null, null, null, null)); + ClassificationInferenceResults classificationResult = (ClassificationInferenceResults) getSingleValue( + model, + fields, + new ClassificationConfigUpdate(1, null, null, null, null) + ); assertThat(classificationResult.getTopClasses().get(0).getProbability(), closeTo(0.5498339973124778, 0.0000001)); assertThat(classificationResult.getTopClasses().get(0).getClassification(), equalTo("0")); assertThat(model.getLatestStatsAndReset().getInferenceCount(), equalTo(1L)); @@ -103,7 +110,8 @@ public void testClassificationInfer() throws Exception { .setPreProcessors(Collections.singletonList(new OneHotEncoding("categorical", oneHotMap(), false))) .setTrainedModel(buildClassificationInference(true)) .build(); - model = new LocalModel(modelId, + model = new LocalModel( + modelId, "test-node", definition, new TrainedModelInput(inputFields), @@ -111,27 +119,34 @@ public void testClassificationInfer() throws Exception { ClassificationConfig.EMPTY_PARAMS, License.OperationMode.PLATINUM, modelStatsService, - mock(CircuitBreaker.class)); + mock(CircuitBreaker.class) + ); result = getSingleValue(model, fields, ClassificationConfigUpdate.EMPTY_PARAMS); assertThat(result.value(), equalTo(0.0)); assertThat(result.valueAsString(), equalTo("no")); - classificationResult = (ClassificationInferenceResults)getSingleValue(model, + classificationResult = (ClassificationInferenceResults) getSingleValue( + model, fields, - new ClassificationConfigUpdate(1, null, null, null, null)); + new ClassificationConfigUpdate(1, null, null, null, null) + ); assertThat(classificationResult.getTopClasses().get(0).getProbability(), closeTo(0.5498339973124778, 0.0000001)); assertThat(classificationResult.getTopClasses().get(0).getClassification(), equalTo("no")); assertThat(model.getLatestStatsAndReset().getInferenceCount(), equalTo(2L)); - classificationResult = (ClassificationInferenceResults)getSingleValue(model, + classificationResult = (ClassificationInferenceResults) getSingleValue( + model, fields, - new ClassificationConfigUpdate(2, null, null, null, null)); + new ClassificationConfigUpdate(2, null, null, null, null) + ); assertThat(classificationResult.getTopClasses(), hasSize(2)); assertThat(model.getLatestStatsAndReset().getInferenceCount(), equalTo(1L)); - classificationResult = (ClassificationInferenceResults)getSingleValue(model, + classificationResult = (ClassificationInferenceResults) getSingleValue( + model, fields, - new ClassificationConfigUpdate(-1, null, null, null, null)); + new ClassificationConfigUpdate(-1, null, null, null, null) + ); assertThat(classificationResult.getTopClasses(), hasSize(2)); assertThat(model.getLatestStatsAndReset().getInferenceCount(), equalTo(1L)); } @@ -147,7 +162,8 @@ public void testClassificationInferWithDifferentPredictionFieldTypes() throws Ex .setTrainedModel(buildClassificationInference(true)) .build(); - LocalModel model = new LocalModel(modelId, + LocalModel model = new LocalModel( + modelId, "test-node", definition, new TrainedModelInput(inputFields), @@ -155,25 +171,29 @@ public void testClassificationInferWithDifferentPredictionFieldTypes() throws Ex ClassificationConfig.EMPTY_PARAMS, License.OperationMode.PLATINUM, modelStatsService, - mock(CircuitBreaker.class)); - Map fields = new HashMap<>() {{ - put("field.foo", 1.0); - put("field.bar", 0.5); - put("categorical", "dog"); - }}; + mock(CircuitBreaker.class) + ); + Map fields = new HashMap<>() { + { + put("field.foo", 1.0); + put("field.bar", 0.5); + put("categorical", "dog"); + } + }; InferenceResults result = getInferenceResult( model, fields, - new ClassificationConfigUpdate(2, null, null, null, PredictionFieldType.STRING)); + new ClassificationConfigUpdate(2, null, null, null, PredictionFieldType.STRING) + ); IngestDocument document = new IngestDocument(new HashMap<>(), new HashMap<>()); writeResult(result, document, "result_field", modelId); assertThat(document.getFieldValue("result_field.predicted_value", String.class), equalTo("no")); List list = document.getFieldValue("result_field.top_classes", List.class); assertThat(list.size(), equalTo(2)); - assertThat(((Map)list.get(0)).get("class_name"), equalTo("no")); - assertThat(((Map)list.get(1)).get("class_name"), equalTo("yes")); + assertThat(((Map) list.get(0)).get("class_name"), equalTo("no")); + assertThat(((Map) list.get(1)).get("class_name"), equalTo("yes")); result = getInferenceResult(model, fields, new ClassificationConfigUpdate(2, null, null, null, PredictionFieldType.NUMBER)); @@ -182,8 +202,8 @@ public void testClassificationInferWithDifferentPredictionFieldTypes() throws Ex assertThat(document.getFieldValue("result_field.predicted_value", Double.class), equalTo(0.0)); list = document.getFieldValue("result_field.top_classes", List.class); assertThat(list.size(), equalTo(2)); - assertThat(((Map)list.get(0)).get("class_name"), equalTo(0.0)); - assertThat(((Map)list.get(1)).get("class_name"), equalTo(1.0)); + assertThat(((Map) list.get(0)).get("class_name"), equalTo(0.0)); + assertThat(((Map) list.get(1)).get("class_name"), equalTo(1.0)); result = getInferenceResult(model, fields, new ClassificationConfigUpdate(2, null, null, null, PredictionFieldType.BOOLEAN)); @@ -192,8 +212,8 @@ public void testClassificationInferWithDifferentPredictionFieldTypes() throws Ex assertThat(document.getFieldValue("result_field.predicted_value", Boolean.class), equalTo(false)); list = document.getFieldValue("result_field.top_classes", List.class); assertThat(list.size(), equalTo(2)); - assertThat(((Map)list.get(0)).get("class_name"), equalTo(false)); - assertThat(((Map)list.get(1)).get("class_name"), equalTo(true)); + assertThat(((Map) list.get(0)).get("class_name"), equalTo(false)); + assertThat(((Map) list.get(1)).get("class_name"), equalTo(true)); } public void testRegression() throws Exception { @@ -204,7 +224,8 @@ public void testRegression() throws Exception { .setPreProcessors(Collections.singletonList(new OneHotEncoding("categorical", oneHotMap(), false))) .setTrainedModel(buildRegressionInference()) .build(); - LocalModel model = new LocalModel("regression_model", + LocalModel model = new LocalModel( + "regression_model", "test-node", trainedModelDefinition, new TrainedModelInput(inputFields), @@ -212,13 +233,16 @@ public void testRegression() throws Exception { RegressionConfig.EMPTY_PARAMS, License.OperationMode.PLATINUM, modelStatsService, - mock(CircuitBreaker.class)); + mock(CircuitBreaker.class) + ); - Map fields = new HashMap<>() {{ - put("foo", 1.0); - put("bar.keyword", 0.5); - put("categorical", "dog"); - }}; + Map fields = new HashMap<>() { + { + put("foo", 1.0); + put("bar.keyword", 0.5); + put("categorical", "dog"); + } + }; SingleValueInferenceResults results = getSingleValue(model, fields, RegressionConfigUpdate.EMPTY_PARAMS); assertThat(results.value(), equalTo(1.3)); @@ -241,17 +265,19 @@ public void testAllFieldsMissing() throws Exception { RegressionConfig.EMPTY_PARAMS, License.OperationMode.PLATINUM, modelStatsService, - mock(CircuitBreaker.class)); + mock(CircuitBreaker.class) + ); - Map fields = new HashMap<>() {{ - put("something", 1.0); - put("other", 0.5); - put("baz", "dog"); - }}; + Map fields = new HashMap<>() { + { + put("something", 1.0); + put("other", 0.5); + put("baz", "dog"); + } + }; - WarningInferenceResults results = (WarningInferenceResults)getInferenceResult(model, fields, RegressionConfigUpdate.EMPTY_PARAMS); - assertThat(results.getWarning(), - equalTo(Messages.getMessage(Messages.INFERENCE_WARNING_ALL_FIELDS_MISSING, "regression_model"))); + WarningInferenceResults results = (WarningInferenceResults) getInferenceResult(model, fields, RegressionConfigUpdate.EMPTY_PARAMS); + assertThat(results.getWarning(), equalTo(Messages.getMessage(Messages.INFERENCE_WARNING_ALL_FIELDS_MISSING, "regression_model"))); assertThat(model.getLatestStatsAndReset().getMissingAllFieldsCount(), equalTo(1L)); } @@ -265,7 +291,8 @@ public void testInferPersistsStatsAfterNumberOfCalls() throws Exception { .setTrainedModel(buildClassificationInference(false)) .build(); - LocalModel model = new LocalModel(modelId, + LocalModel model = new LocalModel( + modelId, "test-node", definition, new TrainedModelInput(inputFields), @@ -275,13 +302,15 @@ public void testInferPersistsStatsAfterNumberOfCalls() throws Exception { modelStatsService, mock(CircuitBreaker.class) ); - Map fields = new HashMap<>() {{ - put("field.foo", 1.0); - put("field.bar", 0.5); - put("categorical", "dog"); - }}; - - for(int i = 0; i < 100; i++) { + Map fields = new HashMap<>() { + { + put("field.foo", 1.0); + put("field.bar", 0.5); + put("categorical", "dog"); + } + }; + + for (int i = 0; i < 100; i++) { getSingleValue(model, fields, ClassificationConfigUpdate.EMPTY_PARAMS); } SingleValueInferenceResults result = getSingleValue(model, fields, ClassificationConfigUpdate.EMPTY_PARAMS); @@ -302,7 +331,6 @@ public void testMapFieldsIfNecessary() { fields.put("a1", "a_value"); fields.put("b1", "b_value"); - LocalModel.mapFieldsIfNecessary(fields, fieldMap); Map expectedMap = new TreeMap<>(); @@ -318,13 +346,12 @@ public void testReferenceCounting() throws IOException { TrainedModelStatsService modelStatsService = mock(TrainedModelStatsService.class); String modelId = "ref-count-model"; List inputFields = Arrays.asList("field.foo", "field.bar"); - InferenceDefinition definition = InferenceDefinition.builder() - .setTrainedModel(buildClassificationInference(false)) - .build(); + InferenceDefinition definition = InferenceDefinition.builder().setTrainedModel(buildClassificationInference(false)).build(); { CircuitBreaker breaker = mock(CircuitBreaker.class); - LocalModel model = new LocalModel(modelId, + LocalModel model = new LocalModel( + modelId, "test-node", definition, new TrainedModelInput(inputFields), @@ -349,7 +376,8 @@ public void testReferenceCounting() throws IOException { { CircuitBreaker breaker = mock(CircuitBreaker.class); - LocalModel model = new LocalModel(modelId, + LocalModel model = new LocalModel( + modelId, "test-node", definition, new TrainedModelInput(inputFields), @@ -371,16 +399,13 @@ public void testReferenceCounting() throws IOException { } } - private static SingleValueInferenceResults getSingleValue(LocalModel model, - Map fields, - InferenceConfigUpdate config) + private static SingleValueInferenceResults getSingleValue(LocalModel model, Map fields, InferenceConfigUpdate config) throws Exception { - return (SingleValueInferenceResults)getInferenceResult(model, fields, config); + return (SingleValueInferenceResults) getInferenceResult(model, fields, config); } - private static InferenceResults getInferenceResult(LocalModel model, - Map fields, - InferenceConfigUpdate config) throws Exception { + private static InferenceResults getInferenceResult(LocalModel model, Map fields, InferenceConfigUpdate config) + throws Exception { PlainActionFuture future = new PlainActionFuture<>(); model.infer(fields, config, future); return future.get(); @@ -394,43 +419,28 @@ private static Map oneHotMap() { } public static InferenceModel buildClassificationInference(boolean includeLables) throws IOException { - return serializeFromTrainedModel((Ensemble)buildClassification(includeLables)); + return serializeFromTrainedModel((Ensemble) buildClassification(includeLables)); } public static TrainedModel buildClassification(boolean includeLabels) { List featureNames = Arrays.asList("field.foo", "field.bar", "animal_cat", "animal_dog"); Tree tree1 = Tree.builder() .setFeatureNames(featureNames) - .setRoot(TreeNode.builder(0) - .setLeftChild(1) - .setRightChild(2) - .setSplitFeature(0) - .setThreshold(0.5)) + .setRoot(TreeNode.builder(0).setLeftChild(1).setRightChild(2).setSplitFeature(0).setThreshold(0.5)) .addNode(TreeNode.builder(1).setLeafValue(1.0)) - .addNode(TreeNode.builder(2) - .setThreshold(0.8) - .setSplitFeature(1) - .setLeftChild(3) - .setRightChild(4)) + .addNode(TreeNode.builder(2).setThreshold(0.8).setSplitFeature(1).setLeftChild(3).setRightChild(4)) .addNode(TreeNode.builder(3).setLeafValue(0.0)) - .addNode(TreeNode.builder(4).setLeafValue(1.0)).build(); + .addNode(TreeNode.builder(4).setLeafValue(1.0)) + .build(); Tree tree2 = Tree.builder() .setFeatureNames(featureNames) - .setRoot(TreeNode.builder(0) - .setLeftChild(1) - .setRightChild(2) - .setSplitFeature(3) - .setThreshold(1.0)) + .setRoot(TreeNode.builder(0).setLeftChild(1).setRightChild(2).setSplitFeature(3).setThreshold(1.0)) .addNode(TreeNode.builder(1).setLeafValue(0.0)) .addNode(TreeNode.builder(2).setLeafValue(1.0)) .build(); Tree tree3 = Tree.builder() .setFeatureNames(featureNames) - .setRoot(TreeNode.builder(0) - .setLeftChild(1) - .setRightChild(2) - .setSplitFeature(0) - .setThreshold(1.0)) + .setRoot(TreeNode.builder(0).setLeftChild(1).setRightChild(2).setSplitFeature(0).setThreshold(1.0)) .addNode(TreeNode.builder(1).setLeafValue(1.0)) .addNode(TreeNode.builder(2).setLeafValue(0.0)) .build(); @@ -439,48 +449,33 @@ public static TrainedModel buildClassification(boolean includeLabels) { .setTargetType(TargetType.CLASSIFICATION) .setFeatureNames(featureNames) .setTrainedModels(Arrays.asList(tree1, tree2, tree3)) - .setOutputAggregator(new WeightedMode(new double[]{0.7, 0.5, 1.0}, 2)) + .setOutputAggregator(new WeightedMode(new double[] { 0.7, 0.5, 1.0 }, 2)) .build(); } public static InferenceModel buildRegressionInference() throws IOException { - return serializeFromTrainedModel((Ensemble)buildRegression()); + return serializeFromTrainedModel((Ensemble) buildRegression()); } public static TrainedModel buildRegression() { List featureNames = Arrays.asList("field.foo", "field.bar", "animal_cat", "animal_dog"); Tree tree1 = Tree.builder() .setFeatureNames(featureNames) - .setRoot(TreeNode.builder(0) - .setLeftChild(1) - .setRightChild(2) - .setSplitFeature(0) - .setThreshold(0.5)) + .setRoot(TreeNode.builder(0).setLeftChild(1).setRightChild(2).setSplitFeature(0).setThreshold(0.5)) .addNode(TreeNode.builder(1).setLeafValue(0.3)) - .addNode(TreeNode.builder(2) - .setThreshold(0.0) - .setSplitFeature(3) - .setLeftChild(3) - .setRightChild(4)) + .addNode(TreeNode.builder(2).setThreshold(0.0).setSplitFeature(3).setLeftChild(3).setRightChild(4)) .addNode(TreeNode.builder(3).setLeafValue(0.1)) - .addNode(TreeNode.builder(4).setLeafValue(0.2)).build(); + .addNode(TreeNode.builder(4).setLeafValue(0.2)) + .build(); Tree tree2 = Tree.builder() .setFeatureNames(featureNames) - .setRoot(TreeNode.builder(0) - .setLeftChild(1) - .setRightChild(2) - .setSplitFeature(2) - .setThreshold(1.0)) + .setRoot(TreeNode.builder(0).setLeftChild(1).setRightChild(2).setSplitFeature(2).setThreshold(1.0)) .addNode(TreeNode.builder(1).setLeafValue(1.5)) .addNode(TreeNode.builder(2).setLeafValue(0.9)) .build(); Tree tree3 = Tree.builder() .setFeatureNames(featureNames) - .setRoot(TreeNode.builder(0) - .setLeftChild(1) - .setRightChild(2) - .setSplitFeature(1) - .setThreshold(0.2)) + .setRoot(TreeNode.builder(0).setLeftChild(1).setRightChild(2).setSplitFeature(1).setThreshold(0.2)) .addNode(TreeNode.builder(1).setLeafValue(1.5)) .addNode(TreeNode.builder(2).setLeafValue(0.9)) .build(); @@ -488,7 +483,7 @@ public static TrainedModel buildRegression() { .setTargetType(TargetType.REGRESSION) .setFeatureNames(featureNames) .setTrainedModels(Arrays.asList(tree1, tree2, tree3)) - .setOutputAggregator(new WeightedSum(new double[]{0.5, 0.5, 0.5})) + .setOutputAggregator(new WeightedSum(new double[] { 0.5, 0.5, 0.5 })) .build(); } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/loadingservice/ModelLoadingServiceTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/loadingservice/ModelLoadingServiceTests.java index f7e4027046f20..8afc14d902cf0 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/loadingservice/ModelLoadingServiceTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/loadingservice/ModelLoadingServiceTests.java @@ -97,8 +97,10 @@ public class ModelLoadingServiceTests extends ESTestCase { @Before public void setUpComponents() { - threadPool = new TestThreadPool("ModelLoadingServiceTests", new ScalingExecutorBuilder(UTILITY_THREAD_POOL_NAME, - 1, 4, TimeValue.timeValueMinutes(10), "xpack.ml.utility_thread_pool")); + threadPool = new TestThreadPool( + "ModelLoadingServiceTests", + new ScalingExecutorBuilder(UTILITY_THREAD_POOL_NAME, 1, 4, TimeValue.timeValueMinutes(10), "xpack.ml.utility_thread_pool") + ); trainedModelProvider = mock(TrainedModelProvider.class); clusterService = mock(ClusterService.class); auditor = mock(InferenceAuditor.class); @@ -124,7 +126,8 @@ public void testGetCachedModels() throws Exception { withTrainedModel(model2, 1L); withTrainedModel(model3, 1L); - ModelLoadingService modelLoadingService = new ModelLoadingService(trainedModelProvider, + ModelLoadingService modelLoadingService = new ModelLoadingService( + trainedModelProvider, auditor, threadPool, clusterService, @@ -132,13 +135,14 @@ public void testGetCachedModels() throws Exception { Settings.EMPTY, "test-node", circuitBreaker, - mock(XPackLicenseState.class)); + mock(XPackLicenseState.class) + ); modelLoadingService.clusterChanged(ingestChangedEvent(model1, model2, model3)); - String[] modelIds = new String[]{model1, model2, model3}; - for(int i = 0; i < 10; i++) { - String model = modelIds[i%3]; + String[] modelIds = new String[] { model1, model2, model3 }; + for (int i = 0; i < 10; i++) { + String model = modelIds[i % 3]; PlainActionFuture future = new PlainActionFuture<>(); modelLoadingService.getModelForPipeline(model, future); assertThat(future.get(), is(not(nullValue()))); @@ -154,8 +158,8 @@ public void testGetCachedModels() throws Exception { // Test invalidate cache for model3 modelLoadingService.clusterChanged(ingestChangedEvent(model1, model2)); - for(int i = 0; i < 10; i++) { - String model = modelIds[i%3]; + for (int i = 0; i < 10; i++) { + String model = modelIds[i % 3]; PlainActionFuture future = new PlainActionFuture<>(); modelLoadingService.getModelForPipeline(model, future); assertThat(future.get(), is(not(nullValue()))); @@ -171,12 +175,13 @@ public void testMaxCachedLimitReached() throws Exception { String model1 = "test-cached-limit-load-model-1"; String model2 = "test-cached-limit-load-model-2"; String model3 = "test-cached-limit-load-model-3"; - String[] modelIds = new String[]{model1, model2, model3}; + String[] modelIds = new String[] { model1, model2, model3 }; withTrainedModel(model1, 10L); withTrainedModel(model2, 6L); withTrainedModel(model3, 15L); - ModelLoadingService modelLoadingService = new ModelLoadingService(trainedModelProvider, + ModelLoadingService modelLoadingService = new ModelLoadingService( + trainedModelProvider, auditor, threadPool, clusterService, @@ -184,7 +189,8 @@ public void testMaxCachedLimitReached() throws Exception { Settings.builder().put(ModelLoadingService.INFERENCE_MODEL_CACHE_SIZE.getKey(), ByteSizeValue.ofBytes(20L)).build(), "test-node", circuitBreaker, - mock(XPackLicenseState.class)); + mock(XPackLicenseState.class) + ); // We want to be notified when the models are loaded which happens in a background thread ModelLoadedTracker loadedTracker = new ModelLoadedTracker(Arrays.asList(modelIds)); @@ -206,9 +212,9 @@ public void testMaxCachedLimitReached() throws Exception { // all models loaded put in the cache assertBusy(() -> assertTrue(loadedTracker.allModelsLoaded()), 2, TimeUnit.SECONDS); - for(int i = 0; i < 10; i++) { + for (int i = 0; i < 10; i++) { // Only reference models 1 and 2, so that cache is only invalidated once for model3 (after initial load) - String model = modelIds[i%2]; + String model = modelIds[i % 2]; PlainActionFuture future = new PlainActionFuture<>(); modelLoadingService.getModelForPipeline(model, future); assertThat(future.get(), is(not(nullValue()))); @@ -226,7 +232,7 @@ public void testMaxCachedLimitReached() throws Exception { verify(trainedModelStatsService, times(1)).queueStats(argThat(o -> o.getModelId().equals(model3)), anyBoolean()); // Load model 3, should invalidate 1 and 2 - for(int i = 0; i < 10; i++) { + for (int i = 0; i < 10; i++) { PlainActionFuture future3 = new PlainActionFuture<>(); modelLoadingService.getModelForPipeline(model3, future3); assertThat(future3.get(), is(not(nullValue()))); @@ -237,7 +243,7 @@ public void testMaxCachedLimitReached() throws Exception { verify(trainedModelStatsService, atMost(2)).queueStats(argThat(o -> o.getModelId().equals(model2)), anyBoolean()); // Load model 1, should invalidate 3 - for(int i = 0; i < 10; i++) { + for (int i = 0; i < 10; i++) { PlainActionFuture future1 = new PlainActionFuture<>(); modelLoadingService.getModelForPipeline(model1, future1); assertThat(future1.get(), is(not(nullValue()))); @@ -246,7 +252,7 @@ public void testMaxCachedLimitReached() throws Exception { verify(trainedModelStatsService, times(2)).queueStats(argThat(o -> o.getModelId().equals(model3)), anyBoolean()); // Load model 2 - for(int i = 0; i < 10; i++) { + for (int i = 0; i < 10; i++) { PlainActionFuture future2 = new PlainActionFuture<>(); modelLoadingService.getModelForPipeline(model2, future2); assertThat(future2.get(), is(not(nullValue()))); @@ -256,8 +262,8 @@ public void testMaxCachedLimitReached() throws Exception { // Test invalidate cache for model3 // Now both model 1 and 2 should fit in cache without issues modelLoadingService.clusterChanged(ingestChangedEvent(model1, model2)); - for(int i = 0; i < 10; i++) { - String model = modelIds[i%3]; + for (int i = 0; i < 10; i++) { + String model = modelIds[i % 3]; PlainActionFuture future = new PlainActionFuture<>(); modelLoadingService.getModelForPipeline(model, future); assertThat(future.get(), is(not(nullValue()))); @@ -272,7 +278,8 @@ public void testWhenCacheEnabledButNotIngestNode() throws Exception { String model1 = "test-uncached-not-ingest-model-1"; withTrainedModel(model1, 1L); - ModelLoadingService modelLoadingService = new ModelLoadingService(trainedModelProvider, + ModelLoadingService modelLoadingService = new ModelLoadingService( + trainedModelProvider, auditor, threadPool, clusterService, @@ -280,11 +287,12 @@ public void testWhenCacheEnabledButNotIngestNode() throws Exception { Settings.EMPTY, "test-node", circuitBreaker, - mock(XPackLicenseState.class)); + mock(XPackLicenseState.class) + ); modelLoadingService.clusterChanged(ingestChangedEvent(false, model1)); - for(int i = 0; i < 10; i++) { + for (int i = 0; i < 10; i++) { PlainActionFuture future = new PlainActionFuture<>(); modelLoadingService.getModelForPipeline(model1, future); assertThat(future.get(), is(not(nullValue()))); @@ -299,7 +307,8 @@ public void testGetCachedMissingModel() throws Exception { String model = "test-load-cached-missing-model"; withMissingModel(model); - ModelLoadingService modelLoadingService =new ModelLoadingService(trainedModelProvider, + ModelLoadingService modelLoadingService = new ModelLoadingService( + trainedModelProvider, auditor, threadPool, clusterService, @@ -307,7 +316,8 @@ public void testGetCachedMissingModel() throws Exception { Settings.EMPTY, "test-node", circuitBreaker, - mock(XPackLicenseState.class)); + mock(XPackLicenseState.class) + ); modelLoadingService.clusterChanged(ingestChangedEvent(model)); PlainActionFuture future = new PlainActionFuture<>(); @@ -329,7 +339,8 @@ public void testGetMissingModel() { String model = "test-load-missing-model"; withMissingModel(model); - ModelLoadingService modelLoadingService = new ModelLoadingService(trainedModelProvider, + ModelLoadingService modelLoadingService = new ModelLoadingService( + trainedModelProvider, auditor, threadPool, clusterService, @@ -337,7 +348,8 @@ public void testGetMissingModel() { Settings.EMPTY, "test-node", circuitBreaker, - mock(XPackLicenseState.class)); + mock(XPackLicenseState.class) + ); PlainActionFuture future = new PlainActionFuture<>(); modelLoadingService.getModelForPipeline(model, future); @@ -354,7 +366,8 @@ public void testGetModelEagerly() throws Exception { String model = "test-get-model-eagerly"; withTrainedModel(model, 1L); - ModelLoadingService modelLoadingService = new ModelLoadingService(trainedModelProvider, + ModelLoadingService modelLoadingService = new ModelLoadingService( + trainedModelProvider, auditor, threadPool, clusterService, @@ -362,9 +375,10 @@ public void testGetModelEagerly() throws Exception { Settings.EMPTY, "test-node", circuitBreaker, - mock(XPackLicenseState.class)); + mock(XPackLicenseState.class) + ); - for(int i = 0; i < 3; i++) { + for (int i = 0; i < 3; i++) { PlainActionFuture future = new PlainActionFuture<>(); modelLoadingService.getModelForPipeline(model, future); assertThat(future.get(), is(not(nullValue()))); @@ -379,7 +393,8 @@ public void testGetModelForSearch() throws Exception { String modelId = "test-get-model-for-search"; withTrainedModel(modelId, 1L); - ModelLoadingService modelLoadingService = new ModelLoadingService(trainedModelProvider, + ModelLoadingService modelLoadingService = new ModelLoadingService( + trainedModelProvider, auditor, threadPool, clusterService, @@ -387,9 +402,10 @@ public void testGetModelForSearch() throws Exception { Settings.EMPTY, "test-node", circuitBreaker, - mock(XPackLicenseState.class)); + mock(XPackLicenseState.class) + ); - for(int i = 0; i < 3; i++) { + for (int i = 0; i < 3; i++) { PlainActionFuture future = new PlainActionFuture<>(); modelLoadingService.getModelForSearch(modelId, future); assertThat(future.get(), is(not(nullValue()))); @@ -409,7 +425,8 @@ public void testCircuitBreakerBreak() throws Exception { withTrainedModel(model2, 5L); withTrainedModel(model3, 12L); CircuitBreaker circuitBreaker = new CustomCircuitBreaker(11); - ModelLoadingService modelLoadingService = new ModelLoadingService(trainedModelProvider, + ModelLoadingService modelLoadingService = new ModelLoadingService( + trainedModelProvider, auditor, threadPool, clusterService, @@ -417,12 +434,16 @@ public void testCircuitBreakerBreak() throws Exception { Settings.EMPTY, "test-node", circuitBreaker, - mock(XPackLicenseState.class)); - - modelLoadingService.addModelLoadedListener(model3, ActionListener.wrap( - r -> fail("Should not have succeeded to load model as breaker should be reached"), - e -> assertThat(e, instanceOf(CircuitBreakingException.class)) - )); + mock(XPackLicenseState.class) + ); + + modelLoadingService.addModelLoadedListener( + model3, + ActionListener.wrap( + r -> fail("Should not have succeeded to load model as breaker should be reached"), + e -> assertThat(e, instanceOf(CircuitBreakingException.class)) + ) + ); modelLoadingService.clusterChanged(ingestChangedEvent(model1, model2, model3)); @@ -441,16 +462,15 @@ public void testCircuitBreakerBreak() throws Exception { modelLoadingService.clusterChanged(ingestChangedEvent(model1)); - assertBusy(() -> { - assertThat(circuitBreaker.getUsed(), equalTo(5L)); - }); + assertBusy(() -> { assertThat(circuitBreaker.getUsed(), equalTo(5L)); }); } public void testReferenceCounting() throws Exception { String modelId = "test-reference-counting"; withTrainedModel(modelId, 1L); - ModelLoadingService modelLoadingService = new ModelLoadingService(trainedModelProvider, + ModelLoadingService modelLoadingService = new ModelLoadingService( + trainedModelProvider, auditor, threadPool, clusterService, @@ -458,7 +478,8 @@ public void testReferenceCounting() throws Exception { Settings.EMPTY, "test-node", circuitBreaker, - mock(XPackLicenseState.class)); + mock(XPackLicenseState.class) + ); modelLoadingService.clusterChanged(ingestChangedEvent(modelId)); @@ -485,7 +506,8 @@ public void testReferenceCountingForPipeline() throws Exception { String modelId = "test-reference-counting-for-pipeline"; withTrainedModel(modelId, 1L); - ModelLoadingService modelLoadingService = new ModelLoadingService(trainedModelProvider, + ModelLoadingService modelLoadingService = new ModelLoadingService( + trainedModelProvider, auditor, threadPool, clusterService, @@ -493,7 +515,8 @@ public void testReferenceCountingForPipeline() throws Exception { Settings.EMPTY, "test-node", circuitBreaker, - mock(XPackLicenseState.class)); + mock(XPackLicenseState.class) + ); modelLoadingService.clusterChanged(ingestChangedEvent(modelId)); @@ -516,7 +539,8 @@ public void testReferenceCounting_ModelIsNotCached() throws ExecutionException, String modelId = "test-reference-counting-not-cached"; withTrainedModel(modelId, 1L); - ModelLoadingService modelLoadingService = new ModelLoadingService(trainedModelProvider, + ModelLoadingService modelLoadingService = new ModelLoadingService( + trainedModelProvider, auditor, threadPool, clusterService, @@ -524,7 +548,8 @@ public void testReferenceCounting_ModelIsNotCached() throws ExecutionException, Settings.EMPTY, "test-node", circuitBreaker, - mock(XPackLicenseState.class)); + mock(XPackLicenseState.class) + ); PlainActionFuture future = new PlainActionFuture<>(); modelLoadingService.getModelForPipeline(modelId, future); @@ -538,7 +563,8 @@ public void testGetCachedModelViaModelAliases() throws Exception { withTrainedModel(model1, 1L); withTrainedModel(model2, 1L); - ModelLoadingService modelLoadingService = new ModelLoadingService(trainedModelProvider, + ModelLoadingService modelLoadingService = new ModelLoadingService( + trainedModelProvider, auditor, threadPool, clusterService, @@ -546,18 +572,16 @@ public void testGetCachedModelViaModelAliases() throws Exception { Settings.EMPTY, "test-node", circuitBreaker, - mock(XPackLicenseState.class)); - - modelLoadingService.clusterChanged(aliasChangeEvent( - true, - new String[]{"loaded_model"}, - true, - Arrays.asList(Tuple.tuple(model1, "loaded_model")) - )); - - String[] modelIds = new String[]{model1, "loaded_model"}; - for(int i = 0; i < 10; i++) { - String model = modelIds[i%2]; + mock(XPackLicenseState.class) + ); + + modelLoadingService.clusterChanged( + aliasChangeEvent(true, new String[] { "loaded_model" }, true, Arrays.asList(Tuple.tuple(model1, "loaded_model"))) + ); + + String[] modelIds = new String[] { model1, "loaded_model" }; + for (int i = 0; i < 10; i++) { + String model = modelIds[i % 2]; PlainActionFuture future = new PlainActionFuture<>(); modelLoadingService.getModelForPipeline(model, future); assertThat(future.get(), is(not(nullValue()))); @@ -569,16 +593,13 @@ public void testGetCachedModelViaModelAliases() throws Exception { assertTrue(modelLoadingService.isModelCached("loaded_model")); // alias change only - modelLoadingService.clusterChanged(aliasChangeEvent( - true, - new String[]{"loaded_model"}, - false, - Arrays.asList(Tuple.tuple(model2, "loaded_model")) - )); - - modelIds = new String[]{model2, "loaded_model"}; - for(int i = 0; i < 10; i++) { - String model = modelIds[i%2]; + modelLoadingService.clusterChanged( + aliasChangeEvent(true, new String[] { "loaded_model" }, false, Arrays.asList(Tuple.tuple(model2, "loaded_model"))) + ); + + modelIds = new String[] { model2, "loaded_model" }; + for (int i = 0; i < 10; i++) { + String model = modelIds[i % 2]; PlainActionFuture future = new PlainActionFuture<>(); modelLoadingService.getModelForPipeline(model, future); assertThat(future.get(), is(not(nullValue()))); @@ -595,7 +616,8 @@ public void testAliasesGetUpdatedEvenWhenNotIngestNode() throws IOException { String model2 = "test-load-model-2"; withTrainedModel(model2, 1L); - ModelLoadingService modelLoadingService = new ModelLoadingService(trainedModelProvider, + ModelLoadingService modelLoadingService = new ModelLoadingService( + trainedModelProvider, auditor, threadPool, clusterService, @@ -603,27 +625,27 @@ public void testAliasesGetUpdatedEvenWhenNotIngestNode() throws IOException { Settings.EMPTY, "test-node", circuitBreaker, - mock(XPackLicenseState.class)); + mock(XPackLicenseState.class) + ); - modelLoadingService.clusterChanged(aliasChangeEvent( - false, - new String[0], - false, - Arrays.asList(Tuple.tuple(model1, "loaded_model")) - )); + modelLoadingService.clusterChanged( + aliasChangeEvent(false, new String[0], false, Arrays.asList(Tuple.tuple(model1, "loaded_model"))) + ); assertThat(modelLoadingService.getModelId("loaded_model"), equalTo(model1)); - modelLoadingService.clusterChanged(aliasChangeEvent( - false, - new String[0], - false, - Arrays.asList( - Tuple.tuple(model1, "loaded_model_again"), - Tuple.tuple(model1, "loaded_model_foo"), - Tuple.tuple(model2, "loaded_model") + modelLoadingService.clusterChanged( + aliasChangeEvent( + false, + new String[0], + false, + Arrays.asList( + Tuple.tuple(model1, "loaded_model_again"), + Tuple.tuple(model1, "loaded_model_foo"), + Tuple.tuple(model2, "loaded_model") + ) ) - )); + ); assertThat(modelLoadingService.getModelId("loaded_model"), equalTo(model2)); assertThat(modelLoadingService.getModelId("loaded_model_foo"), equalTo(model1)); assertThat(modelLoadingService.getModelId("loaded_model_again"), equalTo(model1)); @@ -658,8 +680,7 @@ private void withMissingModel(String modelId) { doAnswer(invocationOnMock -> { @SuppressWarnings("rawtypes") ActionListener listener = (ActionListener) invocationOnMock.getArguments()[2]; - listener.onFailure(new ResourceNotFoundException( - Messages.getMessage(Messages.INFERENCE_NOT_FOUND, modelId))); + listener.onFailure(new ResourceNotFoundException(Messages.getMessage(Messages.INFERENCE_NOT_FOUND, modelId))); return null; }).when(trainedModelProvider).getTrainedModel(eq(modelId), eq(GetTrainedModelsAction.Includes.empty()), any()); } else { @@ -674,16 +695,14 @@ private void withMissingModel(String modelId) { doAnswer(invocationOnMock -> { @SuppressWarnings("rawtypes") ActionListener listener = (ActionListener) invocationOnMock.getArguments()[2]; - listener.onFailure(new ResourceNotFoundException( - Messages.getMessage(Messages.MODEL_DEFINITION_NOT_FOUND, modelId))); + listener.onFailure(new ResourceNotFoundException(Messages.getMessage(Messages.MODEL_DEFINITION_NOT_FOUND, modelId))); return null; }).when(trainedModelProvider).getTrainedModelForInference(eq(modelId), eq(false), any()); } doAnswer(invocationOnMock -> { @SuppressWarnings("rawtypes") ActionListener listener = (ActionListener) invocationOnMock.getArguments()[2]; - listener.onFailure(new ResourceNotFoundException( - Messages.getMessage(Messages.INFERENCE_NOT_FOUND, modelId))); + listener.onFailure(new ResourceNotFoundException(Messages.getMessage(Messages.INFERENCE_NOT_FOUND, modelId))); return null; }).when(trainedModelProvider).getTrainedModelForInference(eq(modelId), eq(false), any()); } @@ -692,10 +711,12 @@ private static ClusterChangedEvent ingestChangedEvent(String... modelId) throws return ingestChangedEvent(true, modelId); } - private static ClusterChangedEvent aliasChangeEvent(boolean isIngestNode, - String[] modelId, - boolean ingestToo, - List> modelIdAndAliases) throws IOException { + private static ClusterChangedEvent aliasChangeEvent( + boolean isIngestNode, + String[] modelId, + boolean ingestToo, + List> modelIdAndAliases + ) throws IOException { ClusterChangedEvent event = mock(ClusterChangedEvent.class); Set set = new HashSet<>(); set.add(ModelAliasMetadata.NAME); @@ -718,23 +739,30 @@ private static ClusterState buildClusterStateWithModelReferences(boolean isInges return builder(isIngestNode).metadata(addIngest(Metadata.builder(), modelId)).build(); } - private static ClusterState withModelReferencesAndAliasChange(boolean isIngestNode, - String[] modelId, - List> modelIdAndAliases) throws IOException { + private static ClusterState withModelReferencesAndAliasChange( + boolean isIngestNode, + String[] modelId, + List> modelIdAndAliases + ) throws IOException { return builder(isIngestNode).metadata(addAliases(addIngest(Metadata.builder(), modelId), modelIdAndAliases)).build(); } private static ClusterState.Builder builder(boolean isIngestNode) { return ClusterState.builder(new ClusterName("_name")) - .nodes(DiscoveryNodes.builder().add( - new DiscoveryNode("node_name", - "node_id", - new TransportAddress(InetAddress.getLoopbackAddress(), 9300), - Collections.emptyMap(), - isIngestNode ? Collections.singleton(DiscoveryNodeRole.INGEST_ROLE) : Collections.emptySet(), - Version.CURRENT)) - .localNodeId("node_id") - .build() + .nodes( + DiscoveryNodes.builder() + .add( + new DiscoveryNode( + "node_name", + "node_id", + new TransportAddress(InetAddress.getLoopbackAddress(), 9300), + Collections.emptyMap(), + isIngestNode ? Collections.singleton(DiscoveryNodeRole.INGEST_ROLE) : Collections.emptySet(), + Version.CURRENT + ) + ) + .localNodeId("node_id") + .build() ); } @@ -748,17 +776,27 @@ private static Metadata.Builder addIngest(Metadata.Builder builder, String... mo } private static Metadata.Builder addAliases(Metadata.Builder builder, List> modelIdAndAliases) { - ModelAliasMetadata modelAliasMetadata = new ModelAliasMetadata(modelIdAndAliases.stream() - .collect(Collectors.toMap(Tuple::v2, t -> new ModelAliasMetadata.ModelAliasEntry(t.v1())))); + ModelAliasMetadata modelAliasMetadata = new ModelAliasMetadata( + modelIdAndAliases.stream().collect(Collectors.toMap(Tuple::v2, t -> new ModelAliasMetadata.ModelAliasEntry(t.v1()))) + ); return builder.putCustom(ModelAliasMetadata.NAME, modelAliasMetadata); } private static PipelineConfiguration newConfigurationWithInferenceProcessor(String modelId) throws IOException { - try(XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().map(Collections.singletonMap("processors", - Collections.singletonList( - Collections.singletonMap(InferenceProcessor.TYPE, - Collections.singletonMap(InferenceResults.MODEL_ID_RESULTS_FIELD, - modelId)))))) { + try ( + XContentBuilder xContentBuilder = XContentFactory.jsonBuilder() + .map( + Collections.singletonMap( + "processors", + Collections.singletonList( + Collections.singletonMap( + InferenceProcessor.TYPE, + Collections.singletonMap(InferenceResults.MODEL_ID_RESULTS_FIELD, modelId) + ) + ) + ) + ) + ) { return new PipelineConfiguration("pipeline_with_model_" + modelId, BytesReference.bytes(xContentBuilder), XContentType.JSON); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/modelsize/EnsembleSizeInfoTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/modelsize/EnsembleSizeInfoTests.java index ad46943bc0028..9bb9308ddd36a 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/modelsize/EnsembleSizeInfoTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/modelsize/EnsembleSizeInfoTests.java @@ -35,24 +35,24 @@ static EnsembleSizeInfo createRandom() { } static EnsembleSizeInfo translateToEstimate(EnsembleInferenceModel ensemble) { - TreeInferenceModel tree = (TreeInferenceModel)ensemble.getModels().get(0); + TreeInferenceModel tree = (TreeInferenceModel) ensemble.getModels().get(0); int numClasses = Arrays.stream(tree.getNodes()) .filter(TreeInferenceModel.Node::isLeaf) - .map(n -> (TreeInferenceModel.LeafNode)n) + .map(n -> (TreeInferenceModel.LeafNode) n) .findFirst() .get() - .getLeafValue() - .length; + .getLeafValue().length; return new EnsembleSizeInfo( ensemble.getModels() .stream() - .map(m -> TreeSizeInfoTests.translateToEstimate((TreeInferenceModel)m)) + .map(m -> TreeSizeInfoTests.translateToEstimate((TreeInferenceModel) m)) .collect(Collectors.toList()), randomIntBetween(0, 10), Arrays.stream(ensemble.getFeatureNames()).map(String::length).collect(Collectors.toList()), ensemble.getOutputAggregator().expectedValueSize() == null ? 0 : ensemble.getOutputAggregator().expectedValueSize(), ensemble.getClassificationWeights() == null ? 0 : ensemble.getClassificationWeights().length, - numClasses); + numClasses + ); } @Override diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/modelsize/FrequencyEncodingSizeTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/modelsize/FrequencyEncodingSizeTests.java index d23ff913de451..71df3c7b7ac1e 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/modelsize/FrequencyEncodingSizeTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/modelsize/FrequencyEncodingSizeTests.java @@ -17,17 +17,19 @@ public class FrequencyEncodingSizeTests extends SizeEstimatorTestCase { static FrequencyEncodingSize createRandom() { - return new FrequencyEncodingSize(randomInt(100), + return new FrequencyEncodingSize( randomInt(100), - Stream.generate(() -> randomIntBetween(5, 10)) - .limit(randomIntBetween(1, 10)) - .collect(Collectors.toList())); + randomInt(100), + Stream.generate(() -> randomIntBetween(5, 10)).limit(randomIntBetween(1, 10)).collect(Collectors.toList()) + ); } static FrequencyEncodingSize translateToEstimate(FrequencyEncoding encoding) { - return new FrequencyEncodingSize(encoding.getField().length(), + return new FrequencyEncodingSize( + encoding.getField().length(), encoding.getFeatureName().length(), - encoding.getFrequencyMap().keySet().stream().map(String::length).collect(Collectors.toList())); + encoding.getFrequencyMap().keySet().stream().map(String::length).collect(Collectors.toList()) + ); } @Override diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/modelsize/ModelSizeInfoTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/modelsize/ModelSizeInfoTests.java index ddda72cc77057..6a6df6634a6fd 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/modelsize/ModelSizeInfoTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/modelsize/ModelSizeInfoTests.java @@ -8,12 +8,12 @@ package org.elasticsearch.xpack.ml.inference.modelsize; import org.elasticsearch.common.bytes.BytesArray; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.AbstractXContentTestCase; import java.io.IOException; import java.util.ArrayList; @@ -24,15 +24,18 @@ public class ModelSizeInfoTests extends AbstractXContentTestCase { public static ModelSizeInfo createRandom() { - return new ModelSizeInfo(EnsembleSizeInfoTests.createRandom(), - randomBoolean() ? - null : - Stream.generate(() -> randomFrom( - FrequencyEncodingSizeTests.createRandom(), - OneHotEncodingSizeTests.createRandom(), - TargetMeanEncodingSizeTests.createRandom())) - .limit(randomIntBetween(1, 10)) - .collect(Collectors.toList())); + return new ModelSizeInfo( + EnsembleSizeInfoTests.createRandom(), + randomBoolean() + ? null + : Stream.generate( + () -> randomFrom( + FrequencyEncodingSizeTests.createRandom(), + OneHotEncodingSizeTests.createRandom(), + TargetMeanEncodingSizeTests.createRandom() + ) + ).limit(randomIntBetween(1, 10)).collect(Collectors.toList()) + ); } @Override @@ -58,68 +61,70 @@ protected boolean supportsUnknownFields() { } public void testParseDescribedFormat() throws IOException { - XContentParser parser = XContentHelper.createParser(xContentRegistry(), + XContentParser parser = XContentHelper.createParser( + xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, new BytesArray(FORMAT), - XContentType.JSON); + XContentType.JSON + ); // Shouldn't throw doParseInstance(parser); } - private static final String FORMAT = "" + - "{\n" + - " \"trained_model_size\": {\n" + - " \"ensemble_model_size\": {\n" + - " \"tree_sizes\": [\n" + - " {\"num_nodes\": 7, \"num_leaves\": 8},\n" + - " {\"num_nodes\": 3, \"num_leaves\": 4},\n" + - " {\"num_leaves\": 1}\n" + - " ],\n" + - " \"feature_name_lengths\": [\n" + - " 14,\n" + - " 10,\n" + - " 11\n" + - " ],\n" + - " \"num_output_processor_weights\": 3,\n" + - " \"num_classification_weights\": 0,\n" + - " \"num_classes\": 0,\n" + - " \"num_operations\": 3\n" + - " }\n" + - " },\n" + - " \"preprocessors\": [\n" + - " {\n" + - " \"one_hot_encoding\": {\n" + - " \"field_length\": 10,\n" + - " \"field_value_lengths\": [\n" + - " 10,\n" + - " 20\n" + - " ],\n" + - " \"feature_name_lengths\": [\n" + - " 15,\n" + - " 25\n" + - " ]\n" + - " }\n" + - " },\n" + - " {\n" + - " \"frequency_encoding\": {\n" + - " \"field_length\": 10,\n" + - " \"feature_name_length\": 5,\n" + - " \"field_value_lengths\": [\n" + - " 10,\n" + - " 20\n" + - " ]\n" + - " }\n" + - " },\n" + - " {\n" + - " \"target_mean_encoding\": {\n" + - " \"field_length\": 6,\n" + - " \"feature_name_length\": 15,\n" + - " \"field_value_lengths\": [\n" + - " 10,\n" + - " 20\n" + - " ]\n" + - " }\n" + - " }\n" + - " ]\n" + - "} "; + private static final String FORMAT = "" + + "{\n" + + " \"trained_model_size\": {\n" + + " \"ensemble_model_size\": {\n" + + " \"tree_sizes\": [\n" + + " {\"num_nodes\": 7, \"num_leaves\": 8},\n" + + " {\"num_nodes\": 3, \"num_leaves\": 4},\n" + + " {\"num_leaves\": 1}\n" + + " ],\n" + + " \"feature_name_lengths\": [\n" + + " 14,\n" + + " 10,\n" + + " 11\n" + + " ],\n" + + " \"num_output_processor_weights\": 3,\n" + + " \"num_classification_weights\": 0,\n" + + " \"num_classes\": 0,\n" + + " \"num_operations\": 3\n" + + " }\n" + + " },\n" + + " \"preprocessors\": [\n" + + " {\n" + + " \"one_hot_encoding\": {\n" + + " \"field_length\": 10,\n" + + " \"field_value_lengths\": [\n" + + " 10,\n" + + " 20\n" + + " ],\n" + + " \"feature_name_lengths\": [\n" + + " 15,\n" + + " 25\n" + + " ]\n" + + " }\n" + + " },\n" + + " {\n" + + " \"frequency_encoding\": {\n" + + " \"field_length\": 10,\n" + + " \"feature_name_length\": 5,\n" + + " \"field_value_lengths\": [\n" + + " 10,\n" + + " 20\n" + + " ]\n" + + " }\n" + + " },\n" + + " {\n" + + " \"target_mean_encoding\": {\n" + + " \"field_length\": 6,\n" + + " \"feature_name_length\": 15,\n" + + " \"field_value_lengths\": [\n" + + " 10,\n" + + " 20\n" + + " ]\n" + + " }\n" + + " }\n" + + " ]\n" + + "} "; } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/modelsize/OneHotEncodingSizeTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/modelsize/OneHotEncodingSizeTests.java index 9cbdde5c877b8..52c4a39392a1d 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/modelsize/OneHotEncodingSizeTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/modelsize/OneHotEncodingSizeTests.java @@ -20,18 +20,17 @@ static OneHotEncodingSize createRandom() { int numFieldEntries = randomIntBetween(1, 10); return new OneHotEncodingSize( randomInt(100), - Stream.generate(() -> randomIntBetween(5, 10)) - .limit(numFieldEntries) - .collect(Collectors.toList()), - Stream.generate(() -> randomIntBetween(5, 10)) - .limit(numFieldEntries) - .collect(Collectors.toList())); + Stream.generate(() -> randomIntBetween(5, 10)).limit(numFieldEntries).collect(Collectors.toList()), + Stream.generate(() -> randomIntBetween(5, 10)).limit(numFieldEntries).collect(Collectors.toList()) + ); } static OneHotEncodingSize translateToEstimate(OneHotEncoding encoding) { - return new OneHotEncodingSize(encoding.getField().length(), + return new OneHotEncodingSize( + encoding.getField().length(), encoding.getHotMap().values().stream().map(String::length).collect(Collectors.toList()), - encoding.getHotMap().keySet().stream().map(String::length).collect(Collectors.toList())); + encoding.getHotMap().keySet().stream().map(String::length).collect(Collectors.toList()) + ); } @Override diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/modelsize/SizeEstimatorTestCase.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/modelsize/SizeEstimatorTestCase.java index 7321d7f4fd557..196f128c633f7 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/modelsize/SizeEstimatorTestCase.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/modelsize/SizeEstimatorTestCase.java @@ -9,13 +9,13 @@ import org.apache.lucene.util.Accountable; import org.elasticsearch.common.unit.ByteSizeValue; -import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.ToXContentObject; import static org.hamcrest.Matchers.is; -public abstract class SizeEstimatorTestCase - extends AbstractXContentTestCase { +public abstract class SizeEstimatorTestCase extends + AbstractXContentTestCase { abstract U generateTrueObject(); @@ -30,9 +30,10 @@ public void testRamUsageEstimationAccuracy() { long estimateBytesUsed = estimateObj.ramBytesUsed(); // If we are over by 2kb that is small enough to not be a concern boolean condition = (Math.abs(obj.ramBytesUsed() - estimateObj.ramBytesUsed()) < bytesEps) || - // If the difference is greater than 2kb, it is better to have overestimated. + // If the difference is greater than 2kb, it is better to have overestimated. originalBytesUsed < estimateBytesUsed; - assertThat("estimation difference greater than 2048 and the estimation is too small. Object [" + assertThat( + "estimation difference greater than 2048 and the estimation is too small. Object [" + obj.toString() + "] estimated [" + originalBytesUsed @@ -40,9 +41,10 @@ public void testRamUsageEstimationAccuracy() { + estimateObj + "] estimated [" + estimateBytesUsed - + "]" , + + "]", condition, - is(true)); + is(true) + ); } } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/modelsize/TargetMeanEncodingSizeTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/modelsize/TargetMeanEncodingSizeTests.java index 9c73c2cb5a37a..c698bb21fdeac 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/modelsize/TargetMeanEncodingSizeTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/modelsize/TargetMeanEncodingSizeTests.java @@ -17,17 +17,19 @@ public class TargetMeanEncodingSizeTests extends SizeEstimatorTestCase { static TargetMeanEncodingSize createRandom() { - return new TargetMeanEncodingSize(randomInt(100), + return new TargetMeanEncodingSize( randomInt(100), - Stream.generate(() -> randomIntBetween(5, 10)) - .limit(randomIntBetween(1, 10)) - .collect(Collectors.toList())); + randomInt(100), + Stream.generate(() -> randomIntBetween(5, 10)).limit(randomIntBetween(1, 10)).collect(Collectors.toList()) + ); } static TargetMeanEncodingSize translateToEstimate(TargetMeanEncoding encoding) { - return new TargetMeanEncodingSize(encoding.getField().length(), + return new TargetMeanEncodingSize( + encoding.getField().length(), encoding.getFeatureName().length(), - encoding.getMeanMap().keySet().stream().map(String::length).collect(Collectors.toList())); + encoding.getMeanMap().keySet().stream().map(String::length).collect(Collectors.toList()) + ); } @Override diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/modelsize/TreeSizeInfoTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/modelsize/TreeSizeInfoTests.java index cd5398b35ab0a..c474ecf49a9d6 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/modelsize/TreeSizeInfoTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/modelsize/TreeSizeInfoTests.java @@ -16,7 +16,6 @@ import java.io.IOException; import java.util.Arrays; - public class TreeSizeInfoTests extends SizeEstimatorTestCase { static TreeSizeInfo createRandom() { @@ -26,14 +25,15 @@ static TreeSizeInfo createRandom() { static TreeSizeInfo translateToEstimate(TreeInferenceModel tree) { int numClasses = Arrays.stream(tree.getNodes()) .filter(TreeInferenceModel.Node::isLeaf) - .map(n -> (TreeInferenceModel.LeafNode)n) + .map(n -> (TreeInferenceModel.LeafNode) n) .findFirst() .get() - .getLeafValue() - .length; - return new TreeSizeInfo((int)Arrays.stream(tree.getNodes()).filter(TreeInferenceModel.Node::isLeaf).count(), - (int)Arrays.stream(tree.getNodes()).filter(t -> t.isLeaf() == false).count(), - numClasses); + .getLeafValue().length; + return new TreeSizeInfo( + (int) Arrays.stream(tree.getNodes()).filter(TreeInferenceModel.Node::isLeaf).count(), + (int) Arrays.stream(tree.getNodes()).filter(t -> t.isLeaf() == false).count(), + numClasses + ); } @Override diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/nlp/BertRequestBuilderTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/nlp/BertRequestBuilderTests.java index 6c8ace40542a8..cab63803686ca 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/nlp/BertRequestBuilderTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/nlp/BertRequestBuilderTests.java @@ -9,8 +9,8 @@ import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.BertTokenization; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.Tokenization; import org.elasticsearch.xpack.ml.inference.nlp.tokenizers.BertTokenizer; @@ -51,7 +51,7 @@ private List firstListItemFromMap(String name, Map json @SuppressWarnings("unchecked") public static List nthListItemFromMap(String name, int n, Map jsonDocAsMap) { - return ((List>)jsonDocAsMap.get(name)).get(n); + return ((List>) jsonDocAsMap.get(name)).get(n); } public void testInputTooLarge() throws IOException { @@ -61,12 +61,18 @@ public void testInputTooLarge() throws IOException { ).build(); { BertRequestBuilder requestBuilder = new BertRequestBuilder(tokenizer); - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> requestBuilder.buildRequest(Collections.singletonList("Elasticsearch fun Elasticsearch fun Elasticsearch fun"), - "request1")); - - assertThat(e.getMessage(), - containsString("Input too large. The tokenized input length [11] exceeds the maximum sequence length [5]")); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> requestBuilder.buildRequest( + Collections.singletonList("Elasticsearch fun Elasticsearch fun Elasticsearch fun"), + "request1" + ) + ); + + assertThat( + e.getMessage(), + containsString("Input too large. The tokenized input length [11] exceeds the maximum sequence length [5]") + ); } { BertRequestBuilder requestBuilder = new BertRequestBuilder(tokenizer); @@ -79,20 +85,27 @@ public void testInputTooLarge() throws IOException { @SuppressWarnings("unchecked") public void testBatchWithPadding() throws IOException { BertTokenizer tokenizer = BertTokenizer.builder( - Arrays.asList(BertTokenizer.PAD_TOKEN, BertTokenizer.CLASS_TOKEN, BertTokenizer.SEPARATOR_TOKEN, - "Elastic", "##search", "fun", - "Pancake", "day", - "my", "little", "red", "car", - "God", "##zilla" - ), + Arrays.asList( + BertTokenizer.PAD_TOKEN, + BertTokenizer.CLASS_TOKEN, + BertTokenizer.SEPARATOR_TOKEN, + "Elastic", + "##search", + "fun", + "Pancake", + "day", + "my", + "little", + "red", + "car", + "God", + "##zilla" + ), new BertTokenization(null, null, 512, Tokenization.Truncate.NONE) ).build(); BertRequestBuilder requestBuilder = new BertRequestBuilder(tokenizer); - NlpTask.Request request = requestBuilder.buildRequest( - List.of("Elasticsearch", - "my little red car", - "Godzilla day"), "request1"); + NlpTask.Request request = requestBuilder.buildRequest(List.of("Elasticsearch", "my little red car", "Godzilla day"), "request1"); Map jsonDocAsMap = XContentHelper.convertToMap(request.processInput, true, XContentType.JSON).v2(); assertThat(jsonDocAsMap.keySet(), hasSize(5)); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/nlp/FillMaskProcessorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/nlp/FillMaskProcessorTests.java index e71d1c89e903f..8b36f2fd6ad6c 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/nlp/FillMaskProcessorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/nlp/FillMaskProcessorTests.java @@ -27,27 +27,27 @@ import static org.hamcrest.Matchers.instanceOf; import static org.mockito.Mockito.mock; -public class -FillMaskProcessorTests extends ESTestCase { +public class FillMaskProcessorTests extends ESTestCase { public void testProcessResults() { // only the scores of the MASK index array // are used the rest is filler - double[][][] scores = {{ - { 0, 0, 0, 0, 0, 0, 0}, // The - { 0, 0, 0, 0, 0, 0, 0}, // capital - { 0, 0, 0, 0, 0, 0, 0}, // of - { 0.01, 0.01, 0.3, 0.1, 0.01, 0.2, 1.2}, // MASK - { 0, 0, 0, 0, 0, 0, 0}, // is - { 0, 0, 0, 0, 0, 0, 0} // paris - }}; + double[][][] scores = { + { + { 0, 0, 0, 0, 0, 0, 0 }, // The + { 0, 0, 0, 0, 0, 0, 0 }, // capital + { 0, 0, 0, 0, 0, 0, 0 }, // of + { 0.01, 0.01, 0.3, 0.1, 0.01, 0.2, 1.2 }, // MASK + { 0, 0, 0, 0, 0, 0, 0 }, // is + { 0, 0, 0, 0, 0, 0, 0 } // paris + } }; String input = "The capital of " + BertTokenizer.MASK_TOKEN + " is Paris"; List vocab = Arrays.asList("The", "capital", "of", BertTokenizer.MASK_TOKEN, "is", "Paris", "France"); String[] tokens = input.split(" "); - int[] tokenMap = new int[] {0, 1, 2, 3, 4, 5}; - int[] tokenIds = new int[] {0, 1, 2, 3, 4, 5}; + int[] tokenMap = new int[] { 0, 1, 2, 3, 4, 5 }; + int[] tokenIds = new int[] { 0, 1, 2, 3, 4, 5 }; TokenizationResult tokenization = new TokenizationResult(vocab); tokenization.addTokenization(input, tokens, tokenIds, tokenMap); @@ -73,9 +73,9 @@ public void testProcessResults() { public void testProcessResults_GivenMissingTokens() { TokenizationResult tokenization = new TokenizationResult(Collections.emptyList()); - tokenization.addTokenization("", new String[]{}, new int[] {}, new int[] {}); + tokenization.addTokenization("", new String[] {}, new int[] {}, new int[] {}); - PyTorchResult pyTorchResult = new PyTorchResult("1", new double[][][]{{{}}}, 0L, null); + PyTorchResult pyTorchResult = new PyTorchResult("1", new double[][][] { { {} } }, 0L, null); assertThat( FillMaskProcessor.processResult(tokenization, pyTorchResult, 5, randomAlphaOfLength(10)), instanceOf(WarningInferenceResults.class) @@ -88,20 +88,17 @@ public void testValidate_GivenMissingMaskToken() { FillMaskConfig config = new FillMaskConfig(new VocabularyConfig("test-index"), null, null, null); FillMaskProcessor processor = new FillMaskProcessor(mock(BertTokenizer.class), config); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> processor.validateInputs(input)); + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> processor.validateInputs(input)); assertThat(e.getMessage(), containsString("no [MASK] token could be found")); } - public void testProcessResults_GivenMultipleMaskTokens() { List input = List.of("The capital of [MASK] is [MASK]"); FillMaskConfig config = new FillMaskConfig(new VocabularyConfig("test-index"), null, null, null); FillMaskProcessor processor = new FillMaskProcessor(mock(BertTokenizer.class), config); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> processor.validateInputs(input)); + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> processor.validateInputs(input)); assertThat(e.getMessage(), containsString("only one [MASK] token should exist in the input")); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/nlp/NerProcessorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/nlp/NerProcessorTests.java index b955f961dfef9..e9f4f2b5643a9 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/nlp/NerProcessorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/nlp/NerProcessorTests.java @@ -203,10 +203,7 @@ public void testGroupTaggedTokens_GivenConsecutiveEntities() { tokens.add(new NerProcessor.NerResultProcessor.TaggedToken("Bob", NerProcessor.IobTag.B_PER, 1.0)); tokens.add(new NerProcessor.NerResultProcessor.TaggedToken("too", NerProcessor.IobTag.O, 1.0)); - List entityGroups = NerProcessor.NerResultProcessor.groupTaggedTokens( - tokens, - "Rita, Sue, and Bob too" - ); + List entityGroups = NerProcessor.NerResultProcessor.groupTaggedTokens(tokens, "Rita, Sue, and Bob too"); assertThat(entityGroups, hasSize(3)); assertThat(entityGroups.get(0).getClassName(), equalTo("PER")); assertThat(entityGroups.get(0).getEntity(), equalTo("Rita")); @@ -239,34 +236,14 @@ public void testGroupTaggedTokens_GivenConsecutiveContinuingEntities() { public void testAnnotatedTextBuilder() { String input = "Alexander, my name is Benjamin Trent, I work at Acme Inc."; List entities = List.of( - new NerResults.EntityGroup( - "alexander", - "PER", - 0.9963429980065166, - 0, - 9 - ), - new NerResults.EntityGroup( - "benjamin trent", - "PER", - 0.9972042749283819, - 22, - 36 - ), - new NerResults.EntityGroup( - "acme inc", - "ORG", - 0.9982026600781208, - 48, - 56 - ) + new NerResults.EntityGroup("alexander", "PER", 0.9963429980065166, 0, 9), + new NerResults.EntityGroup("benjamin trent", "PER", 0.9972042749283819, 22, 36), + new NerResults.EntityGroup("acme inc", "ORG", 0.9982026600781208, 48, 56) ); assertThat( NerProcessor.buildAnnotatedText(input, entities), equalTo( - "[Alexander](PER&Alexander), " - + "my name is [Benjamin Trent](PER&Benjamin+Trent), " - + "I work at [Acme Inc](ORG&Acme+Inc)." + "[Alexander](PER&Alexander), " + "my name is [Benjamin Trent](PER&Benjamin+Trent), " + "I work at [Acme Inc](ORG&Acme+Inc)." ) ); } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/nlp/NlpHelpersTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/nlp/NlpHelpersTests.java index bb72f3ee88069..cfc806fc1ebea 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/nlp/NlpHelpersTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/nlp/NlpHelpersTests.java @@ -23,10 +23,7 @@ public class NlpHelpersTests extends ESTestCase { public void testConvertToProbabilitiesBySoftMax_GivenConcreteExample() { - double[][] scores = { - { 0.1, 0.2, 3}, - { 6, 0.2, 0.1} - }; + double[][] scores = { { 0.1, 0.2, 3 }, { 6, 0.2, 0.1 } }; double[][] probabilities = NlpHelpers.convertToProbabilitiesBySoftMax(scores); @@ -39,7 +36,7 @@ public void testConvertToProbabilitiesBySoftMax_GivenConcreteExample() { } public void testConvertToProbabilitiesBySoftMax_OneDimension() { - double[] scores = { 0.1, 0.2, 3}; + double[] scores = { 0.1, 0.2, 3 }; double[] probabilities = NlpHelpers.convertToProbabilitiesBySoftMax(scores); assertThat(probabilities[0], closeTo(0.04931133, 0.00000001)); @@ -58,8 +55,8 @@ public void testConvertToProbabilitiesBySoftMax_GivenRandom() { double[][] probabilities = NlpHelpers.convertToProbabilitiesBySoftMax(scores); // Assert invariants that - // 1. each row sums to 1 - // 2. all values are in [0-1] + // 1. each row sums to 1 + // 2. all values are in [0-1] assertThat(probabilities.length, equalTo(scores.length)); for (int i = 0; i < probabilities.length; i++) { assertThat(probabilities[i].length, equalTo(scores[i].length)); @@ -74,7 +71,7 @@ public void testConvertToProbabilitiesBySoftMax_GivenRandom() { public void testTopK_SimpleCase() { int k = 3; - double[] data = new double[]{1.0, 0.0, 2.0, 8.0, 9.0, 4.2, 4.2, 3.0}; + double[] data = new double[] { 1.0, 0.0, 2.0, 8.0, 9.0, 4.2, 4.2, 3.0 }; NlpHelpers.ScoreAndIndex[] scoreAndIndices = NlpHelpers.topK(k, data); assertEquals(4, scoreAndIndices[0].index); @@ -96,11 +93,9 @@ public void testTopK() { } AtomicInteger index = new AtomicInteger(0); - List sortedByValue = - Stream.generate(() -> new NlpHelpers.ScoreAndIndex(data[index.get()], index.getAndIncrement())) - .limit(size) - .sorted((o1, o2) -> Double.compare(o2.score, o1.score)) - .collect(Collectors.toList()); + List sortedByValue = Stream.generate( + () -> new NlpHelpers.ScoreAndIndex(data[index.get()], index.getAndIncrement()) + ).limit(size).sorted((o1, o2) -> Double.compare(o2.score, o1.score)).collect(Collectors.toList()); NlpHelpers.ScoreAndIndex[] scoreAndIndices = NlpHelpers.topK(k, data); assertEquals(k, scoreAndIndices.length); @@ -114,7 +109,7 @@ public void testTopK() { public void testTopK_KGreaterThanArrayLength() { int k = 6; - double[] data = new double[]{1.0, 0.0, 2.0, 8.0}; + double[] data = new double[] { 1.0, 0.0, 2.0, 8.0 }; NlpHelpers.ScoreAndIndex[] scoreAndIndices = NlpHelpers.topK(k, data); assertEquals(4, scoreAndIndices.length); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/nlp/NlpTaskTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/nlp/NlpTaskTests.java index 18c6628d0faab..b7908a2b2e18c 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/nlp/NlpTaskTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/nlp/NlpTaskTests.java @@ -36,8 +36,10 @@ public void testExtractInput_GivenFieldIsNotPresent() { Map doc = new HashMap<>(); doc.put("some other field", 42); - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> NlpTask.extractInput(new TrainedModelInput(Collections.singletonList(fieldName)), doc)); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> NlpTask.extractInput(new TrainedModelInput(Collections.singletonList(fieldName)), doc) + ); assertThat(e.status(), equalTo(RestStatus.BAD_REQUEST)); assertThat(e.getMessage(), equalTo("no value could be found for input field [" + fieldName + "]")); @@ -49,8 +51,10 @@ public void testExtractInput_GivenFieldIsNotString() { doc.put(fieldName, 42); doc.put("some other field", 42); - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> NlpTask.extractInput(new TrainedModelInput(Collections.singletonList(fieldName)), doc)); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> NlpTask.extractInput(new TrainedModelInput(Collections.singletonList(fieldName)), doc) + ); assertThat(e.status(), equalTo(RestStatus.BAD_REQUEST)); assertThat(e.getMessage(), equalTo("input value [42] for field [" + fieldName + "] is not a string")); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/nlp/TextClassificationProcessorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/nlp/TextClassificationProcessorTests.java index cb5ec0681c198..9796dc5affa7a 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/nlp/TextClassificationProcessorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/nlp/TextClassificationProcessorTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.xpack.ml.inference.nlp; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.ml.inference.results.InferenceResults; import org.elasticsearch.xpack.core.ml.inference.results.WarningInferenceResults; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.BertTokenization; @@ -64,14 +64,26 @@ public void testInvalidResult() { public void testBuildRequest() throws IOException { NlpTokenizer tokenizer = NlpTokenizer.build( new Vocabulary( - Arrays.asList("Elastic", "##search", "fun", - BertTokenizer.CLASS_TOKEN, BertTokenizer.SEPARATOR_TOKEN, BertTokenizer.PAD_TOKEN), + Arrays.asList( + "Elastic", + "##search", + "fun", + BertTokenizer.CLASS_TOKEN, + BertTokenizer.SEPARATOR_TOKEN, + BertTokenizer.PAD_TOKEN + ), randomAlphaOfLength(10) ), - new BertTokenization(null, null, 512, Tokenization.Truncate.NONE)); + new BertTokenization(null, null, 512, Tokenization.Truncate.NONE) + ); TextClassificationConfig config = new TextClassificationConfig( - new VocabularyConfig("test-index"), null, List.of("a", "b"), null, null); + new VocabularyConfig("test-index"), + null, + List.of("a", "b"), + null, + null + ); TextClassificationProcessor processor = new TextClassificationProcessor(tokenizer, config); @@ -81,7 +93,7 @@ public void testBuildRequest() throws IOException { assertThat(jsonDocAsMap.keySet(), hasSize(5)); assertEquals("request1", jsonDocAsMap.get("request_id")); - assertEquals(Arrays.asList(3, 0, 1, 2, 4), ((List>)jsonDocAsMap.get("tokens")).get(0)); - assertEquals(Arrays.asList(1, 1, 1, 1, 1), ((List>)jsonDocAsMap.get("arg_1")).get(0)); + assertEquals(Arrays.asList(3, 0, 1, 2, 4), ((List>) jsonDocAsMap.get("tokens")).get(0)); + assertEquals(Arrays.asList(1, 1, 1, 1, 1), ((List>) jsonDocAsMap.get("arg_1")).get(0)); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/nlp/ZeroShotClassificationProcessorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/nlp/ZeroShotClassificationProcessorTests.java index f523535da5797..47da91346d3cf 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/nlp/ZeroShotClassificationProcessorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/nlp/ZeroShotClassificationProcessorTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.xpack.ml.inference.nlp; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.BertTokenization; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.NlpConfig; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.Tokenization; @@ -32,11 +32,26 @@ public class ZeroShotClassificationProcessorTests extends ESTestCase { public void testBuildRequest() throws IOException { NlpTokenizer tokenizer = NlpTokenizer.build( new Vocabulary( - Arrays.asList("Elastic", "##search", "fun", "default", "label", "new", "stuff", "This", "example", "is", ".", - BertTokenizer.CLASS_TOKEN, BertTokenizer.SEPARATOR_TOKEN, BertTokenizer.PAD_TOKEN), + Arrays.asList( + "Elastic", + "##search", + "fun", + "default", + "label", + "new", + "stuff", + "This", + "example", + "is", + ".", + BertTokenizer.CLASS_TOKEN, + BertTokenizer.SEPARATOR_TOKEN, + BertTokenizer.PAD_TOKEN + ), randomAlphaOfLength(10) ), - new BertTokenization(null, true, 512, Tokenization.Truncate.NONE)); + new BertTokenization(null, true, 512, Tokenization.Truncate.NONE) + ); ZeroShotClassificationConfig config = new ZeroShotClassificationConfig( List.of("entailment", "neutral", "contradiction"), @@ -50,17 +65,17 @@ public void testBuildRequest() throws IOException { ZeroShotClassificationProcessor processor = new ZeroShotClassificationProcessor(tokenizer, config); NlpTask.Request request = processor.getRequestBuilder( - (NlpConfig)new ZeroShotClassificationConfigUpdate.Builder().setLabels(List.of("new", "stuff")).build().apply(config) + (NlpConfig) new ZeroShotClassificationConfigUpdate.Builder().setLabels(List.of("new", "stuff")).build().apply(config) ).buildRequest(List.of("Elasticsearch fun"), "request1"); Map jsonDocAsMap = XContentHelper.convertToMap(request.processInput, true, XContentType.JSON).v2(); assertThat(jsonDocAsMap.keySet(), hasSize(5)); assertEquals("request1", jsonDocAsMap.get("request_id")); - assertEquals(Arrays.asList(11, 0, 1, 2, 12, 7, 8, 9, 5, 10, 12), ((List>)jsonDocAsMap.get("tokens")).get(0)); - assertEquals(Arrays.asList(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), ((List>)jsonDocAsMap.get("arg_1")).get(0)); - assertEquals(Arrays.asList(11, 0, 1, 2, 12, 7, 8, 9, 6, 10, 12), ((List>)jsonDocAsMap.get("tokens")).get(1)); - assertEquals(Arrays.asList(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), ((List>)jsonDocAsMap.get("arg_1")).get(1)); + assertEquals(Arrays.asList(11, 0, 1, 2, 12, 7, 8, 9, 5, 10, 12), ((List>) jsonDocAsMap.get("tokens")).get(0)); + assertEquals(Arrays.asList(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), ((List>) jsonDocAsMap.get("arg_1")).get(0)); + assertEquals(Arrays.asList(11, 0, 1, 2, 12, 7, 8, 9, 6, 10, 12), ((List>) jsonDocAsMap.get("tokens")).get(1)); + assertEquals(Arrays.asList(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), ((List>) jsonDocAsMap.get("arg_1")).get(1)); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/nlp/tokenizers/BasicTokenizerTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/nlp/tokenizers/BasicTokenizerTests.java index fcf6aa4c635bf..017c3323abf5e 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/nlp/tokenizers/BasicTokenizerTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/nlp/tokenizers/BasicTokenizerTests.java @@ -123,10 +123,8 @@ public void testTokenizeChinese() { } public void testCleanText() { - assertEquals("change these chars to spaces", - BasicTokenizer.cleanText("change\tthese chars\rto\nspaces")); - assertEquals("filter control chars", - BasicTokenizer.cleanText("\u0000filter \uFFFDcontrol chars\u0005")); + assertEquals("change these chars to spaces", BasicTokenizer.cleanText("change\tthese chars\rto\nspaces")); + assertEquals("filter control chars", BasicTokenizer.cleanText("\u0000filter \uFFFDcontrol chars\u0005")); } public void testWhiteSpaceTokenize() { diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/nlp/tokenizers/BertTokenizerTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/nlp/tokenizers/BertTokenizerTests.java index ab8c576a919ca..88df39b5aac5d 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/nlp/tokenizers/BertTokenizerTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/nlp/tokenizers/BertTokenizerTests.java @@ -53,7 +53,7 @@ public void testTokenize() { TokenizationResult.Tokenization tokenization = tokenizer.tokenize("Elasticsearch fun"); assertThat(tokenization.getTokens(), arrayContaining("Elastic", "##search", "fun")); assertArrayEquals(new int[] { 0, 1, 3 }, tokenization.getTokenIds()); - assertArrayEquals(new int[] {0, 0, 1}, tokenization.getTokenMap()); + assertArrayEquals(new int[] { 0, 0, 1 }, tokenization.getTokenMap()); } public void testTokenizeLargeInputNoTruncation() { @@ -91,15 +91,12 @@ public void testTokenizeLargeInputTruncation() { } public void testTokenizeAppendSpecialTokens() { - BertTokenizer tokenizer = BertTokenizer.builder( - TEST_CASED_VOCAB, - Tokenization.createDefault() - ).build(); + BertTokenizer tokenizer = BertTokenizer.builder(TEST_CASED_VOCAB, Tokenization.createDefault()).build(); TokenizationResult.Tokenization tokenization = tokenizer.tokenize("Elasticsearch fun"); assertThat(tokenization.getTokens(), arrayContaining("[CLS]", "Elastic", "##search", "fun", "[SEP]")); assertArrayEquals(new int[] { 12, 0, 1, 3, 13 }, tokenization.getTokenIds()); - assertArrayEquals(new int[] {-1, 0, 0, 1, -1}, tokenization.getTokenMap()); + assertArrayEquals(new int[] { -1, 0, 0, 1, -1 }, tokenization.getTokenMap()); } public void testNeverSplitTokens() { @@ -107,13 +104,13 @@ public void testNeverSplitTokens() { BertTokenizer tokenizer = BertTokenizer.builder(TEST_CASED_VOCAB, Tokenization.createDefault()) .setNeverSplit(Collections.singleton(specialToken)) - .setWithSpecialTokens(false) - .build(); + .setWithSpecialTokens(false) + .build(); TokenizationResult.Tokenization tokenization = tokenizer.tokenize("Elasticsearch " + specialToken + " fun"); assertThat(tokenization.getTokens(), arrayContaining("Elastic", "##search", specialToken, "fun")); assertArrayEquals(new int[] { 0, 1, 15, 3 }, tokenization.getTokenIds()); - assertArrayEquals(new int[] {0, 0, 1, 2}, tokenization.getTokenMap()); + assertArrayEquals(new int[] { 0, 0, 1, 2 }, tokenization.getTokenMap()); } public void testDoLowerCase() { @@ -121,14 +118,12 @@ public void testDoLowerCase() { BertTokenizer tokenizer = BertTokenizer.builder( Arrays.asList("elastic", "##search", "fun", BertTokenizer.UNKNOWN_TOKEN), Tokenization.createDefault() - ).setDoLowerCase(false) - .setWithSpecialTokens(false) - .build(); + ).setDoLowerCase(false).setWithSpecialTokens(false).build(); TokenizationResult.Tokenization tokenization = tokenizer.tokenize("Elasticsearch fun"); assertThat(tokenization.getTokens(), arrayContaining(BertTokenizer.UNKNOWN_TOKEN, "fun")); - assertArrayEquals(new int[] {3, 2}, tokenization.getTokenIds()); - assertArrayEquals(new int[] {0, 1}, tokenization.getTokenMap()); + assertArrayEquals(new int[] { 3, 2 }, tokenization.getTokenIds()); + assertArrayEquals(new int[] { 0, 1 }, tokenization.getTokenMap()); tokenization = tokenizer.tokenize("elasticsearch fun"); assertThat(tokenization.getTokens(), arrayContaining("elastic", "##search", "fun")); @@ -146,20 +141,17 @@ public void testDoLowerCase() { } public void testPunctuation() { - BertTokenizer tokenizer = BertTokenizer.builder( - TEST_CASED_VOCAB, - Tokenization.createDefault() - ).setWithSpecialTokens(false).build(); + BertTokenizer tokenizer = BertTokenizer.builder(TEST_CASED_VOCAB, Tokenization.createDefault()).setWithSpecialTokens(false).build(); TokenizationResult.Tokenization tokenization = tokenizer.tokenize("Elasticsearch, fun."); assertThat(tokenization.getTokens(), arrayContaining("Elastic", "##search", ",", "fun", ".")); assertArrayEquals(new int[] { 0, 1, 11, 3, 10 }, tokenization.getTokenIds()); - assertArrayEquals(new int[] {0, 0, 1, 2, 3}, tokenization.getTokenMap()); + assertArrayEquals(new int[] { 0, 0, 1, 2, 3 }, tokenization.getTokenMap()); tokenization = tokenizer.tokenize("Elasticsearch, fun [MASK]."); assertThat(tokenization.getTokens(), arrayContaining("Elastic", "##search", ",", "fun", "[MASK]", ".")); assertArrayEquals(new int[] { 0, 1, 11, 3, 14, 10 }, tokenization.getTokenIds()); - assertArrayEquals(new int[] {0, 0, 1, 2, 3, 4}, tokenization.getTokenMap()); + assertArrayEquals(new int[] { 0, 0, 1, 2, 3, 4 }, tokenization.getTokenMap()); } public void testBatchInput() { @@ -180,23 +172,23 @@ public void testBatchInput() { TokenizationResult.Tokenization tokenization = tr.getTokenizations().get(0); assertThat(tokenization.getTokens(), arrayContaining("Elastic", "##search")); - assertArrayEquals(new int[] {0, 1}, tokenization.getTokenIds()); - assertArrayEquals(new int[] {0, 0}, tokenization.getTokenMap()); + assertArrayEquals(new int[] { 0, 1 }, tokenization.getTokenIds()); + assertArrayEquals(new int[] { 0, 0 }, tokenization.getTokenMap()); tokenization = tr.getTokenizations().get(1); assertThat(tokenization.getTokens(), arrayContaining("my", "little", "red", "car")); assertArrayEquals(new int[] { 4, 5, 6, 7 }, tokenization.getTokenIds()); - assertArrayEquals(new int[] {0, 1, 2, 3}, tokenization.getTokenMap()); + assertArrayEquals(new int[] { 0, 1, 2, 3 }, tokenization.getTokenMap()); tokenization = tr.getTokenizations().get(2); assertThat(tokenization.getTokens(), arrayContaining("God", "##zilla", "day")); assertArrayEquals(new int[] { 8, 9, 16 }, tokenization.getTokenIds()); - assertArrayEquals(new int[] {0, 0, 1}, tokenization.getTokenMap()); + assertArrayEquals(new int[] { 0, 0, 1 }, tokenization.getTokenMap()); tokenization = tr.getTokenizations().get(3); assertThat(tokenization.getTokens(), arrayContaining("God", "##zilla", "Pancake", "red", "car", "day")); assertArrayEquals(new int[] { 8, 9, 17, 6, 7, 16 }, tokenization.getTokenIds()); - assertArrayEquals(new int[] {0, 0, 1, 2, 3, 4}, tokenization.getTokenMap()); + assertArrayEquals(new int[] { 0, 0, 1, 2, 3, 4 }, tokenization.getTokenMap()); } public void testMultiSeqTokenization() { diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/nlp/tokenizers/WordPieceTokenizerTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/nlp/tokenizers/WordPieceTokenizerTests.java index 671ccd2e4fb27..dd3bf3863d361 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/nlp/tokenizers/WordPieceTokenizerTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/nlp/tokenizers/WordPieceTokenizerTests.java @@ -22,8 +22,18 @@ public class WordPieceTokenizerTests extends ESTestCase { public static final String UNKNOWN_TOKEN = "[UNK]"; public void testTokenize() { - Map vocabMap = - createVocabMap(UNKNOWN_TOKEN, "[CLS]", "[SEP]", "want", "##want", "##ed", "wa", "un", "runn", "##ing"); + Map vocabMap = createVocabMap( + UNKNOWN_TOKEN, + "[CLS]", + "[SEP]", + "want", + "##want", + "##ed", + "wa", + "un", + "runn", + "##ing" + ); WordPieceTokenizer tokenizer = new WordPieceTokenizer(vocabMap, UNKNOWN_TOKEN, 100); List tokenAndIds = tokenizer.tokenize(""); @@ -47,9 +57,9 @@ public void testMaxCharLength() { assertThat(tokens, contains("Some", "UNK", "will", "UNK", "UNK")); } - static Map createVocabMap(String ... words) { + static Map createVocabMap(String... words) { Map vocabMap = new HashMap<>(); - for (int i=0; i future = new PlainActionFuture<>(); - trainedModelProvider.getTrainedModel(modelId, GetTrainedModelsAction.Includes.forModelDefinition(),future); + trainedModelProvider.getTrainedModel(modelId, GetTrainedModelsAction.Includes.forModelDefinition(), future); TrainedModelConfig configWithDefinition = future.actionGet(); assertThat(configWithDefinition.getModelId(), equalTo(modelId)); @@ -82,17 +79,19 @@ public void testGetModelThatExistsAsResource() throws Exception { } public void testExpandIdsQuery() { - QueryBuilder queryBuilder = TrainedModelProvider.buildExpandIdsQuery(new String[]{"model*", "trained_mode"}, - Arrays.asList("tag1", "tag2")); + QueryBuilder queryBuilder = TrainedModelProvider.buildExpandIdsQuery( + new String[] { "model*", "trained_mode" }, + Arrays.asList("tag1", "tag2") + ); assertThat(queryBuilder, is(instanceOf(ConstantScoreQueryBuilder.class))); - QueryBuilder innerQuery = ((ConstantScoreQueryBuilder)queryBuilder).innerQuery(); + QueryBuilder innerQuery = ((ConstantScoreQueryBuilder) queryBuilder).innerQuery(); assertThat(innerQuery, is(instanceOf(BoolQueryBuilder.class))); - ((BoolQueryBuilder)innerQuery).filter().forEach(qb -> { + ((BoolQueryBuilder) innerQuery).filter().forEach(qb -> { if (qb instanceof TermQueryBuilder) { - assertThat(((TermQueryBuilder)qb).fieldName(), equalTo(TrainedModelConfig.TAGS.getPreferredName())); - assertThat(((TermQueryBuilder)qb).value(), is(oneOf("tag1", "tag2"))); + assertThat(((TermQueryBuilder) qb).fieldName(), equalTo(TrainedModelConfig.TAGS.getPreferredName())); + assertThat(((TermQueryBuilder) qb).value(), is(oneOf("tag1", "tag2"))); return; } assertThat(qb, is(instanceOf(BoolQueryBuilder.class))); @@ -102,51 +101,65 @@ public void testExpandIdsQuery() { public void testExpandIdsPagination() { // NOTE: these tests assume that the query pagination results are "buffered" - assertThat(TrainedModelProvider.collectIds(new PageParams(0, 3), - Collections.emptySet(), - new HashSet<>(Arrays.asList("a", "b", "c"))), - equalTo(new TreeSet<>(Arrays.asList("a", "b", "c")))); - - assertThat(TrainedModelProvider.collectIds(new PageParams(0, 3), - Collections.singleton("a"), - new HashSet<>(Arrays.asList("b", "c", "d"))), - equalTo(new TreeSet<>(Arrays.asList("a", "b", "c")))); - - assertThat(TrainedModelProvider.collectIds(new PageParams(1, 3), - Collections.singleton("a"), - new HashSet<>(Arrays.asList("b", "c", "d"))), - equalTo(new TreeSet<>(Arrays.asList("b", "c", "d")))); - - assertThat(TrainedModelProvider.collectIds(new PageParams(1, 1), - Collections.singleton("c"), - new HashSet<>(Arrays.asList("a", "b"))), - equalTo(new TreeSet<>(Arrays.asList("b")))); - - assertThat(TrainedModelProvider.collectIds(new PageParams(1, 1), - Collections.singleton("b"), - new HashSet<>(Arrays.asList("a", "c"))), - equalTo(new TreeSet<>(Arrays.asList("b")))); - - assertThat(TrainedModelProvider.collectIds(new PageParams(1, 2), - new HashSet<>(Arrays.asList("a", "b")), - new HashSet<>(Arrays.asList("c", "d", "e"))), - equalTo(new TreeSet<>(Arrays.asList("b", "c")))); - - assertThat(TrainedModelProvider.collectIds(new PageParams(1, 3), - new HashSet<>(Arrays.asList("a", "b")), - new HashSet<>(Arrays.asList("c", "d", "e"))), - equalTo(new TreeSet<>(Arrays.asList("b", "c", "d")))); - - assertThat(TrainedModelProvider.collectIds(new PageParams(2, 3), - new HashSet<>(Arrays.asList("a", "b")), - new HashSet<>(Arrays.asList("c", "d", "e"))), - equalTo(new TreeSet<>(Arrays.asList("c", "d", "e")))); + assertThat( + TrainedModelProvider.collectIds(new PageParams(0, 3), Collections.emptySet(), new HashSet<>(Arrays.asList("a", "b", "c"))), + equalTo(new TreeSet<>(Arrays.asList("a", "b", "c"))) + ); + + assertThat( + TrainedModelProvider.collectIds(new PageParams(0, 3), Collections.singleton("a"), new HashSet<>(Arrays.asList("b", "c", "d"))), + equalTo(new TreeSet<>(Arrays.asList("a", "b", "c"))) + ); + + assertThat( + TrainedModelProvider.collectIds(new PageParams(1, 3), Collections.singleton("a"), new HashSet<>(Arrays.asList("b", "c", "d"))), + equalTo(new TreeSet<>(Arrays.asList("b", "c", "d"))) + ); + + assertThat( + TrainedModelProvider.collectIds(new PageParams(1, 1), Collections.singleton("c"), new HashSet<>(Arrays.asList("a", "b"))), + equalTo(new TreeSet<>(Arrays.asList("b"))) + ); + + assertThat( + TrainedModelProvider.collectIds(new PageParams(1, 1), Collections.singleton("b"), new HashSet<>(Arrays.asList("a", "c"))), + equalTo(new TreeSet<>(Arrays.asList("b"))) + ); + + assertThat( + TrainedModelProvider.collectIds( + new PageParams(1, 2), + new HashSet<>(Arrays.asList("a", "b")), + new HashSet<>(Arrays.asList("c", "d", "e")) + ), + equalTo(new TreeSet<>(Arrays.asList("b", "c"))) + ); + + assertThat( + TrainedModelProvider.collectIds( + new PageParams(1, 3), + new HashSet<>(Arrays.asList("a", "b")), + new HashSet<>(Arrays.asList("c", "d", "e")) + ), + equalTo(new TreeSet<>(Arrays.asList("b", "c", "d"))) + ); + + assertThat( + TrainedModelProvider.collectIds( + new PageParams(2, 3), + new HashSet<>(Arrays.asList("a", "b")), + new HashSet<>(Arrays.asList("c", "d", "e")) + ), + equalTo(new TreeSet<>(Arrays.asList("c", "d", "e"))) + ); } public void testGetModelThatExistsAsResourceButIsMissing() { TrainedModelProvider trainedModelProvider = new TrainedModelProvider(mock(Client.class), xContentRegistry()); - ElasticsearchException ex = expectThrows(ElasticsearchException.class, - () -> trainedModelProvider.loadModelFromResource("missing_model", randomBoolean())); + ElasticsearchException ex = expectThrows( + ElasticsearchException.class, + () -> trainedModelProvider.loadModelFromResource("missing_model", randomBoolean()) + ); assertThat(ex.getMessage(), equalTo(Messages.getMessage(Messages.INFERENCE_NOT_FOUND, "missing_model"))); } @@ -160,8 +173,10 @@ public void testChunkDefinitionWithSize() { int start = 0; int end = size; for (BytesReference chunk : chunks) { - assertArrayEquals(Arrays.copyOfRange(bytes, start, end), - Arrays.copyOfRange(chunk.array(), chunk.arrayOffset(), chunk.arrayOffset() + chunk.length())); + assertArrayEquals( + Arrays.copyOfRange(bytes, start, end), + Arrays.copyOfRange(chunk.array(), chunk.arrayOffset(), chunk.arrayOffset() + chunk.length()) + ); start += size; end = Math.min(end + size, totalLength); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/pytorch/process/PyTorchBuilderTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/pytorch/process/PyTorchBuilderTests.java index 49a52a8a5bd4f..135739d567cfb 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/pytorch/process/PyTorchBuilderTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/pytorch/process/PyTorchBuilderTests.java @@ -48,12 +48,15 @@ public void testBuild() throws IOException, InterruptedException { verify(nativeController).startProcess(commandCaptor.capture()); - assertThat(commandCaptor.getValue(), contains( - "./pytorch_inference", - "--validElasticLicenseKeyConfirmed=true", - "--inferenceThreads=2", - "--modelThreads=4", - PROCESS_PIPES_ARG) + assertThat( + commandCaptor.getValue(), + contains( + "./pytorch_inference", + "--validElasticLicenseKeyConfirmed=true", + "--inferenceThreads=2", + "--modelThreads=4", + PROCESS_PIPES_ARG + ) ); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/trainedmodels/langident/LangIdentNeuralNetworkInferenceTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/trainedmodels/langident/LangIdentNeuralNetworkInferenceTests.java index 7092ad28592a7..7283c19891e51 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/trainedmodels/langident/LangIdentNeuralNetworkInferenceTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/trainedmodels/langident/LangIdentNeuralNetworkInferenceTests.java @@ -8,8 +8,8 @@ import org.elasticsearch.action.support.PlainActionFuture; import org.elasticsearch.client.Client; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.ml.action.GetTrainedModelsAction; import org.elasticsearch.xpack.core.ml.inference.MlInferenceNamedXContentProvider; import org.elasticsearch.xpack.core.ml.inference.TrainedModelConfig; @@ -41,7 +41,7 @@ public void testLangInference() throws Exception { config.ensureParsedDefinition(xContentRegistry()); TrainedModelDefinition trainedModelDefinition = config.getModelDefinition(); InferenceDefinition inferenceDefinition = new InferenceDefinition( - (LangIdentNeuralNetwork)trainedModelDefinition.getTrainedModel(), + (LangIdentNeuralNetwork) trainedModelDefinition.getTrainedModel(), trainedModelDefinition.getPreProcessors() ); List examples = new LanguageExamples().getLanguageExamples(); @@ -54,13 +54,18 @@ public void testLangInference() throws Exception { Map inferenceFields = new HashMap<>(); inferenceFields.put("text", text); - ClassificationInferenceResults singleValueInferenceResults = - (ClassificationInferenceResults) inferenceDefinition.infer(inferenceFields, classificationConfig); + ClassificationInferenceResults singleValueInferenceResults = (ClassificationInferenceResults) inferenceDefinition.infer( + inferenceFields, + classificationConfig + ); assertThat(singleValueInferenceResults.valueAsString(), equalTo(cld3Actual)); double eps = entry.getLanguage().equals("hr") ? 0.001 : 0.00001; - assertThat("mismatch probability for language " + cld3Actual, - singleValueInferenceResults.getTopClasses().get(0).getProbability(), closeTo(cld3Probability, eps)); + assertThat( + "mismatch probability for language " + cld3Actual, + singleValueInferenceResults.getTopClasses().get(0).getProbability(), + closeTo(cld3Probability, eps) + ); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/JobManagerTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/JobManagerTests.java index 0a47cd0d66502..68a574c26708e 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/JobManagerTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/JobManagerTests.java @@ -29,10 +29,6 @@ import org.elasticsearch.common.document.DocumentField; import org.elasticsearch.common.settings.ClusterSettings; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.env.Environment; import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.index.Index; @@ -43,12 +39,16 @@ import org.elasticsearch.search.SearchModule; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xpack.core.action.util.QueryPage; import org.elasticsearch.xpack.core.ml.MachineLearningField; import org.elasticsearch.xpack.core.ml.MlConfigIndex; import org.elasticsearch.xpack.core.ml.MlMetadata; import org.elasticsearch.xpack.core.ml.action.PutJobAction; import org.elasticsearch.xpack.core.ml.action.UpdateJobAction; -import org.elasticsearch.xpack.core.action.util.QueryPage; import org.elasticsearch.xpack.core.ml.job.config.AnalysisConfig; import org.elasticsearch.xpack.core.ml.job.config.CategorizationAnalyzerConfig; import org.elasticsearch.xpack.core.ml.job.config.DataDescription; @@ -120,9 +120,7 @@ protected NamedXContentRegistry xContentRegistry() { @Before public void setup() throws Exception { - Settings settings = Settings.builder() - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()) - .build(); + Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()).build(); environment = TestEnvironment.newEnvironment(settings); analysisRegistry = CategorizationAnalyzerTests.buildTestAnalysisRegistry(environment); clusterService = mock(ClusterService.class); @@ -146,9 +144,8 @@ public void testGetJobNotInIndexOrCluster() { MlMetadata.Builder mlMetadata = new MlMetadata.Builder(); ClusterState clusterState = ClusterState.builder(new ClusterName("_name")) - .metadata(Metadata.builder() - .putCustom(MlMetadata.TYPE, mlMetadata.build())) - .build(); + .metadata(Metadata.builder().putCustom(MlMetadata.TYPE, mlMetadata.build())) + .build(); when(clusterService.state()).thenReturn(clusterState); // job document does not exist @@ -160,10 +157,7 @@ public void testGetJobNotInIndexOrCluster() { JobManager jobManager = createJobManager(mockClientBuilder.build()); AtomicReference exceptionHolder = new AtomicReference<>(); - jobManager.getJob("non-job", ActionListener.wrap( - job -> fail("Job not expected"), - e -> exceptionHolder.set(e) - )); + jobManager.getJob("non-job", ActionListener.wrap(job -> fail("Job not expected"), e -> exceptionHolder.set(e))); assertNotNull(exceptionHolder.get()); assertThat(exceptionHolder.get(), instanceOf(ResourceNotFoundException.class)); @@ -177,9 +171,8 @@ public void testGetJobFromClusterWhenNotInIndex() { mlMetadata.putJob(clusterJob, false); ClusterState clusterState = ClusterState.builder(new ClusterName("_name")) - .metadata(Metadata.builder() - .putCustom(MlMetadata.TYPE, mlMetadata.build())) - .build(); + .metadata(Metadata.builder().putCustom(MlMetadata.TYPE, mlMetadata.build())) + .build(); when(clusterService.state()).thenReturn(clusterState); // job document does not exist @@ -191,10 +184,7 @@ public void testGetJobFromClusterWhenNotInIndex() { JobManager jobManager = createJobManager(mockClientBuilder.build()); AtomicReference jobHolder = new AtomicReference<>(); - jobManager.getJob(clusterJobId, ActionListener.wrap( - job -> jobHolder.set(job), - e -> fail(e.getMessage()) - )); + jobManager.getJob(clusterJobId, ActionListener.wrap(job -> jobHolder.set(job), e -> fail(e.getMessage()))); assertNotNull(jobHolder.get()); assertEquals(clusterJob, jobHolder.get()); @@ -211,12 +201,10 @@ public void testExpandJobsFromClusterStateAndIndex() throws IOException { mlMetadata.putJob(csJobBar, false); ClusterState clusterState = ClusterState.builder(new ClusterName("_name")) - .metadata(Metadata.builder() - .putCustom(MlMetadata.TYPE, mlMetadata.build())) - .build(); + .metadata(Metadata.builder().putCustom(MlMetadata.TYPE, mlMetadata.build())) + .build(); when(clusterService.state()).thenReturn(clusterState); - List docsAsBytes = new ArrayList<>(); Job.Builder indexJobFoo = buildJobBuilder("foo-index"); @@ -226,12 +214,8 @@ public void testExpandJobsFromClusterStateAndIndex() throws IOException { mockClientBuilder.prepareSearch(MlConfigIndex.indexName(), docsAsBytes); JobManager jobManager = createJobManager(mockClientBuilder.build()); - AtomicReference> jobsHolder = new AtomicReference<>(); - jobManager.expandJobs("_all", true, ActionListener.wrap( - jobs -> jobsHolder.set(jobs), - e -> fail(e.getMessage()) - )); + jobManager.expandJobs("_all", true, ActionListener.wrap(jobs -> jobsHolder.set(jobs), e -> fail(e.getMessage()))); assertNotNull(jobsHolder.get()); assertThat(jobsHolder.get().results(), hasSize(4)); @@ -239,10 +223,7 @@ public void testExpandJobsFromClusterStateAndIndex() throws IOException { assertThat(jobIds, contains("bar-cs", "foo-cs-1", "foo-cs-2", "foo-index")); jobsHolder.set(null); - jobManager.expandJobs("foo*", true, ActionListener.wrap( - jobs -> jobsHolder.set(jobs), - e -> fail(e.getMessage()) - )); + jobManager.expandJobs("foo*", true, ActionListener.wrap(jobs -> jobsHolder.set(jobs), e -> fail(e.getMessage()))); assertNotNull(jobsHolder.get()); assertThat(jobsHolder.get().results(), hasSize(3)); @@ -298,7 +279,8 @@ public void testPutJob_ThrowsIfJobExistsInClusterState() throws IOException { MlMetadata.Builder mlMetadata = new MlMetadata.Builder(); mlMetadata.putJob(buildJobBuilder("foo").build(), false); ClusterState clusterState = ClusterState.builder(new ClusterName("name")) - .metadata(Metadata.builder().putCustom(MlMetadata.TYPE, mlMetadata.build())).build(); + .metadata(Metadata.builder().putCustom(MlMetadata.TYPE, mlMetadata.build())) + .build(); jobManager.putJob(putJobRequest, analysisRegistry, clusterState, new ActionListener() { @Override @@ -318,22 +300,25 @@ public void testNotifyFilterChangedGivenNoop() { MockClientBuilder mockClientBuilder = new MockClientBuilder("cluster-test"); JobManager jobManager = createJobManager(mockClientBuilder.build()); - jobManager.notifyFilterChanged(filter, Collections.emptySet(), Collections.emptySet(), ActionListener.wrap( - r -> {}, - e -> fail(e.getMessage()) - )); + jobManager.notifyFilterChanged( + filter, + Collections.emptySet(), + Collections.emptySet(), + ActionListener.wrap(r -> {}, e -> fail(e.getMessage())) + ); Mockito.verifyNoMoreInteractions(auditor, updateJobProcessNotifier); } - @SuppressWarnings({"unchecked", "rawtypes"}) + @SuppressWarnings({ "unchecked", "rawtypes" }) public void testNotifyFilterChanged() throws IOException { Detector.Builder detectorReferencingFilter = new Detector.Builder("count", null); detectorReferencingFilter.setByFieldName("foo"); DetectionRule filterRule = new DetectionRule.Builder(RuleScope.builder().exclude("foo", "foo_filter")).build(); detectorReferencingFilter.setRules(Collections.singletonList(filterRule)); - AnalysisConfig.Builder filterAnalysisConfig = new AnalysisConfig.Builder(Collections.singletonList( - detectorReferencingFilter.build())); + AnalysisConfig.Builder filterAnalysisConfig = new AnalysisConfig.Builder( + Collections.singletonList(detectorReferencingFilter.build()) + ); List docsAsBytes = new ArrayList<>(); @@ -351,15 +336,14 @@ public void testNotifyFilterChanged() throws IOException { Job.Builder jobWithoutFilter = buildJobBuilder("job-without-filter"); - PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); + PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); addJobTask(jobReferencingFilter1.getId(), "node_id", JobState.OPENED, tasksBuilder); addJobTask(jobReferencingFilter2.getId(), "node_id", JobState.OPENED, tasksBuilder); addJobTask(jobWithoutFilter.getId(), "node_id", JobState.OPENED, tasksBuilder); ClusterState clusterState = ClusterState.builder(new ClusterName("_name")) - .metadata(Metadata.builder() - .putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())) - .build(); + .metadata(Metadata.builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())) + .build(); when(clusterService.state()).thenReturn(clusterState); doAnswer(invocationOnMock -> { @@ -374,11 +358,12 @@ public void testNotifyFilterChanged() throws IOException { MlFilter filter = MlFilter.builder("foo_filter").setItems("a", "b").build(); - jobManager.notifyFilterChanged(filter, new TreeSet<>(Arrays.asList("item 1", "item 2")), - new TreeSet<>(Collections.singletonList("item 3")), ActionListener.wrap( - r -> {}, - e -> fail(e.getMessage()) - )); + jobManager.notifyFilterChanged( + filter, + new TreeSet<>(Arrays.asList("item 1", "item 2")), + new TreeSet<>(Collections.singletonList("item 3")), + ActionListener.wrap(r -> {}, e -> fail(e.getMessage())) + ); ArgumentCaptor updateParamsCaptor = ArgumentCaptor.forClass(UpdateParams.class); verify(updateJobProcessNotifier, times(2)).submitJobUpdate(updateParamsCaptor.capture(), any(ActionListener.class)); @@ -390,12 +375,18 @@ public void testNotifyFilterChanged() throws IOException { assertThat(capturedUpdateParams.get(1).getJobId(), equalTo(jobReferencingFilter2.getId())); assertThat(capturedUpdateParams.get(1).getFilter(), equalTo(filter)); - verify(auditor).info(jobReferencingFilter1.getId(), "Filter [foo_filter] has been modified; added items: " + - "['item 1', 'item 2'], removed items: ['item 3']"); - verify(auditor).info(jobReferencingFilter2.getId(), "Filter [foo_filter] has been modified; added items: " + - "['item 1', 'item 2'], removed items: ['item 3']"); - verify(auditor).info(jobReferencingFilter3.getId(), "Filter [foo_filter] has been modified; added items: " + - "['item 1', 'item 2'], removed items: ['item 3']"); + verify(auditor).info( + jobReferencingFilter1.getId(), + "Filter [foo_filter] has been modified; added items: " + "['item 1', 'item 2'], removed items: ['item 3']" + ); + verify(auditor).info( + jobReferencingFilter2.getId(), + "Filter [foo_filter] has been modified; added items: " + "['item 1', 'item 2'], removed items: ['item 3']" + ); + verify(auditor).info( + jobReferencingFilter3.getId(), + "Filter [foo_filter] has been modified; added items: " + "['item 1', 'item 2'], removed items: ['item 3']" + ); Mockito.verifyNoMoreInteractions(auditor, updateJobProcessNotifier); } @@ -404,8 +395,9 @@ public void testNotifyFilterChangedGivenOnlyAddedItems() throws IOException { detectorReferencingFilter.setByFieldName("foo"); DetectionRule filterRule = new DetectionRule.Builder(RuleScope.builder().exclude("foo", "foo_filter")).build(); detectorReferencingFilter.setRules(Collections.singletonList(filterRule)); - AnalysisConfig.Builder filterAnalysisConfig = new AnalysisConfig.Builder(Collections.singletonList( - detectorReferencingFilter.build())); + AnalysisConfig.Builder filterAnalysisConfig = new AnalysisConfig.Builder( + Collections.singletonList(detectorReferencingFilter.build()) + ); Job.Builder jobReferencingFilter = buildJobBuilder("job-referencing-filter"); jobReferencingFilter.setAnalysisConfig(filterAnalysisConfig); @@ -414,9 +406,8 @@ public void testNotifyFilterChangedGivenOnlyAddedItems() throws IOException { PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); ClusterState clusterState = ClusterState.builder(new ClusterName("_name")) - .metadata(Metadata.builder() - .putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())) - .build(); + .metadata(Metadata.builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())) + .build(); when(clusterService.state()).thenReturn(clusterState); MockClientBuilder mockClientBuilder = new MockClientBuilder("cluster-test"); @@ -425,11 +416,12 @@ public void testNotifyFilterChangedGivenOnlyAddedItems() throws IOException { MlFilter filter = MlFilter.builder("foo_filter").build(); - jobManager.notifyFilterChanged(filter, new TreeSet<>(Arrays.asList("a", "b")), Collections.emptySet(), - ActionListener.wrap( - r -> {}, - e -> fail(e.getMessage()) - )); + jobManager.notifyFilterChanged( + filter, + new TreeSet<>(Arrays.asList("a", "b")), + Collections.emptySet(), + ActionListener.wrap(r -> {}, e -> fail(e.getMessage())) + ); verify(auditor).info(jobReferencingFilter.getId(), "Filter [foo_filter] has been modified; added items: ['a', 'b']"); Mockito.verifyNoMoreInteractions(auditor, updateJobProcessNotifier); @@ -440,8 +432,9 @@ public void testNotifyFilterChangedGivenOnlyRemovedItems() throws IOException { detectorReferencingFilter.setByFieldName("foo"); DetectionRule filterRule = new DetectionRule.Builder(RuleScope.builder().exclude("foo", "foo_filter")).build(); detectorReferencingFilter.setRules(Collections.singletonList(filterRule)); - AnalysisConfig.Builder filterAnalysisConfig = new AnalysisConfig.Builder(Collections.singletonList( - detectorReferencingFilter.build())); + AnalysisConfig.Builder filterAnalysisConfig = new AnalysisConfig.Builder( + Collections.singletonList(detectorReferencingFilter.build()) + ); Job.Builder jobReferencingFilter = buildJobBuilder("job-referencing-filter"); jobReferencingFilter.setAnalysisConfig(filterAnalysisConfig); @@ -449,9 +442,8 @@ public void testNotifyFilterChangedGivenOnlyRemovedItems() throws IOException { PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); ClusterState clusterState = ClusterState.builder(new ClusterName("_name")) - .metadata(Metadata.builder() - .putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())) - .build(); + .metadata(Metadata.builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())) + .build(); when(clusterService.state()).thenReturn(clusterState); when(clusterService.state()).thenReturn(clusterState); @@ -461,11 +453,12 @@ public void testNotifyFilterChangedGivenOnlyRemovedItems() throws IOException { MlFilter filter = MlFilter.builder("foo_filter").build(); - jobManager.notifyFilterChanged(filter, Collections.emptySet(), new TreeSet<>(Arrays.asList("a", "b")), - ActionListener.wrap( - r -> {}, - e -> fail(e.getMessage()) - )); + jobManager.notifyFilterChanged( + filter, + Collections.emptySet(), + new TreeSet<>(Arrays.asList("a", "b")), + ActionListener.wrap(r -> {}, e -> fail(e.getMessage())) + ); verify(auditor).info(jobReferencingFilter.getId(), "Filter [foo_filter] has been modified; removed items: ['a', 'b']"); Mockito.verifyNoMoreInteractions(auditor, updateJobProcessNotifier); @@ -478,7 +471,8 @@ public void testUpdateJob_notAllowedPreMigration() { RoutingTable.Builder routingTable = RoutingTable.builder(); IndexMetadata.Builder indexMetadata = IndexMetadata.builder(MlConfigIndex.indexName()); - indexMetadata.settings(Settings.builder() + indexMetadata.settings( + Settings.builder() .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) @@ -486,39 +480,44 @@ public void testUpdateJob_notAllowedPreMigration() { metadata.put(indexMetadata); Index index = new Index(MlConfigIndex.indexName(), "_uuid"); ShardId shardId = new ShardId(index, 0); - ShardRouting shardRouting = ShardRouting.newUnassigned(shardId, true, RecoverySource.EmptyStoreRecoverySource.INSTANCE, - new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "")); + ShardRouting shardRouting = ShardRouting.newUnassigned( + shardId, + true, + RecoverySource.EmptyStoreRecoverySource.INSTANCE, + new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "") + ); shardRouting = shardRouting.initialize("node_id", null, 0L); shardRouting = shardRouting.moveToStarted(); - routingTable.add(IndexRoutingTable.builder(index) - .addIndexShard(new IndexShardRoutingTable.Builder(shardId).addShard(shardRouting).build())); + routingTable.add( + IndexRoutingTable.builder(index).addIndexShard(new IndexShardRoutingTable.Builder(shardId).addShard(shardRouting).build()) + ); ClusterState clusterState = ClusterState.builder(new ClusterName("_name")) - .metadata(metadata.putCustom(MlMetadata.TYPE, mlmetadata.build())) - .routingTable(routingTable.build()) - .build(); + .metadata(metadata.putCustom(MlMetadata.TYPE, mlmetadata.build())) + .routingTable(routingTable.build()) + .build(); when(clusterService.state()).thenReturn(clusterState); JobManager jobManager = createJobManager(new MockClientBuilder("jobmanager-test").build()); - jobManager.updateJob(new UpdateJobAction.Request("closed-job-not-migrated", null), ActionListener.wrap( + jobManager.updateJob( + new UpdateJobAction.Request("closed-job-not-migrated", null), + ActionListener.wrap( response -> fail("response not expected: " + response), - exception -> { - assertThat(exception, instanceOf(ElasticsearchStatusException.class)); - } - )); + exception -> { assertThat(exception, instanceOf(ElasticsearchStatusException.class)); } + ) + ); } public void testUpdateProcessOnCalendarChanged() { - PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); + PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); addJobTask("job-1", "node_id", JobState.OPENED, tasksBuilder); addJobTask("job-2", "node_id", JobState.OPENED, tasksBuilder); addJobTask("job-3", "node_id", JobState.OPENED, tasksBuilder); ClusterState clusterState = ClusterState.builder(new ClusterName("_name")) - .metadata(Metadata.builder() - .putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())) - .build(); + .metadata(Metadata.builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())) + .build(); when(clusterService.state()).thenReturn(clusterState); MockClientBuilder mockClientBuilder = new MockClientBuilder("cluster-test"); @@ -528,11 +527,10 @@ public void testUpdateProcessOnCalendarChanged() { JobManager jobManager = createJobManager(mockClientBuilder.build()); - jobManager.updateProcessOnCalendarChanged(Arrays.asList("job-1", "job-3", "job-4"), - ActionListener.wrap( - r -> {}, - e -> fail(e.getMessage()) - )); + jobManager.updateProcessOnCalendarChanged( + Arrays.asList("job-1", "job-3", "job-4"), + ActionListener.wrap(r -> {}, e -> fail(e.getMessage())) + ); ArgumentCaptor updateParamsCaptor = ArgumentCaptor.forClass(UpdateParams.class); verify(updateJobProcessNotifier, times(2)).submitJobUpdate(updateParamsCaptor.capture(), any()); @@ -546,35 +544,40 @@ public void testUpdateProcessOnCalendarChanged() { } public void testUpdateProcessOnCalendarChanged_GivenGroups() throws IOException { - PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); + PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); addJobTask("job-1", "node_id", JobState.OPENED, tasksBuilder); addJobTask("job-2", "node_id", JobState.OPENED, tasksBuilder); addJobTask("job-3", "node_id", JobState.OPENED, tasksBuilder); ClusterState clusterState = ClusterState.builder(new ClusterName("_name")) - .metadata(Metadata.builder() - .putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())) - .build(); + .metadata(Metadata.builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())) + .build(); when(clusterService.state()).thenReturn(clusterState); MockClientBuilder mockClientBuilder = new MockClientBuilder("cluster-test"); // For the JobConfigProvider expand groups search. // group-1 will expand to job-1 and job-2 List> fieldHits = new ArrayList<>(); - fieldHits.add(Collections.singletonMap(Job.ID.getPreferredName(), - new DocumentField(Job.ID.getPreferredName(), Collections.singletonList("job-1")))); - fieldHits.add(Collections.singletonMap(Job.ID.getPreferredName(), - new DocumentField(Job.ID.getPreferredName(), Collections.singletonList("job-2")))); - + fieldHits.add( + Collections.singletonMap( + Job.ID.getPreferredName(), + new DocumentField(Job.ID.getPreferredName(), Collections.singletonList("job-1")) + ) + ); + fieldHits.add( + Collections.singletonMap( + Job.ID.getPreferredName(), + new DocumentField(Job.ID.getPreferredName(), Collections.singletonList("job-2")) + ) + ); mockClientBuilder.prepareSearchFields(MlConfigIndex.indexName(), fieldHits); JobManager jobManager = createJobManager(mockClientBuilder.build()); - jobManager.updateProcessOnCalendarChanged(Collections.singletonList("group-1"), - ActionListener.wrap( - r -> {}, - e -> fail(e.getMessage()) - )); + jobManager.updateProcessOnCalendarChanged( + Collections.singletonList("group-1"), + ActionListener.wrap(r -> {}, e -> fail(e.getMessage())) + ); ArgumentCaptor updateParamsCaptor = ArgumentCaptor.forClass(UpdateParams.class); verify(updateJobProcessNotifier, times(2)).submitJobUpdate(updateParamsCaptor.capture(), any()); @@ -595,16 +598,20 @@ public void testValidateCategorizationAnalyzer_GivenValid() throws IOException { JobManager.validateCategorizationAnalyzerOrSetDefault(jobBuilder, analysisRegistry, Version.CURRENT); Job job = jobBuilder.build(new Date()); - assertThat(job.getAnalysisConfig().getCategorizationAnalyzerConfig(), - equalTo(CategorizationAnalyzerConfig.buildDefaultCategorizationAnalyzer(categorizationFilters))); + assertThat( + job.getAnalysisConfig().getCategorizationAnalyzerConfig(), + equalTo(CategorizationAnalyzerConfig.buildDefaultCategorizationAnalyzer(categorizationFilters)) + ); } public void testValidateCategorizationAnalyzer_GivenInvalid() { CategorizationAnalyzerConfig c = new CategorizationAnalyzerConfig.Builder().setAnalyzer("does_not_exist").build(); Job.Builder jobBuilder = createCategorizationJob(c, null); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> JobManager.validateCategorizationAnalyzerOrSetDefault(jobBuilder, analysisRegistry, Version.CURRENT)); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> JobManager.validateCategorizationAnalyzerOrSetDefault(jobBuilder, analysisRegistry, Version.CURRENT) + ); assertThat(e.getMessage(), equalTo("Failed to find global analyzer [does_not_exist]")); } @@ -616,8 +623,10 @@ public void testSetDefaultCategorizationAnalyzer_GivenAllNewNodes() throws IOExc JobManager.validateCategorizationAnalyzerOrSetDefault(jobBuilder, analysisRegistry, Version.CURRENT); Job job = jobBuilder.build(new Date()); - assertThat(job.getAnalysisConfig().getCategorizationAnalyzerConfig(), - equalTo(CategorizationAnalyzerConfig.buildStandardCategorizationAnalyzer(categorizationFilters))); + assertThat( + job.getAnalysisConfig().getCategorizationAnalyzerConfig(), + equalTo(CategorizationAnalyzerConfig.buildStandardCategorizationAnalyzer(categorizationFilters)) + ); } // TODO: This test can be deleted from branches that would never have to talk to a 7.13 node @@ -631,11 +640,12 @@ public void testSetDefaultCategorizationAnalyzer_GivenOldNodeInCluster() throws assertThat(job.getAnalysisConfig().getCategorizationAnalyzerConfig(), nullValue()); } - private Job.Builder createCategorizationJob(CategorizationAnalyzerConfig categorizationAnalyzerConfig, - List categorizationFilters) { + private Job.Builder createCategorizationJob( + CategorizationAnalyzerConfig categorizationAnalyzerConfig, + List categorizationFilters + ) { Detector.Builder d = new Detector.Builder("count", null).setByFieldName("mlcategory"); - AnalysisConfig.Builder ac = new AnalysisConfig.Builder(Collections.singletonList(d.build())) - .setCategorizationFieldName("message") + AnalysisConfig.Builder ac = new AnalysisConfig.Builder(Collections.singletonList(d.build())).setCategorizationFieldName("message") .setCategorizationAnalyzerConfig(categorizationAnalyzerConfig) .setCategorizationFilters(categorizationFilters); @@ -687,9 +697,12 @@ private BytesReference toBytesReference(ToXContent content) throws IOException { } private void givenClusterSettings(Settings settings) { - ClusterSettings clusterSettings = new ClusterSettings(settings, new HashSet<>(Arrays.asList( - MachineLearningField.MAX_MODEL_MEMORY_LIMIT, - MlConfigMigrationEligibilityCheck.ENABLE_CONFIG_MIGRATION))); + ClusterSettings clusterSettings = new ClusterSettings( + settings, + new HashSet<>( + Arrays.asList(MachineLearningField.MAX_MODEL_MEMORY_LIMIT, MlConfigMigrationEligibilityCheck.ENABLE_CONFIG_MIGRATION) + ) + ); when(clusterService.getClusterSettings()).thenReturn(clusterSettings); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/JobNodeSelectorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/JobNodeSelectorTests.java index 4da6b8788108c..38fd059691334 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/JobNodeSelectorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/JobNodeSelectorTests.java @@ -15,9 +15,9 @@ import org.elasticsearch.cluster.node.DiscoveryNodes; import org.elasticsearch.common.Randomness; import org.elasticsearch.common.collect.MapBuilder; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.transport.TransportAddress; import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.core.Tuple; import org.elasticsearch.persistent.PersistentTasksCustomMetadata; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.ml.MlTasks; @@ -61,10 +61,12 @@ public class JobNodeSelectorTests extends ESTestCase { // To simplify the logic in this class all jobs have the same memory requirement private static final long MAX_JOB_BYTES = ByteSizeValue.ofGb(1).getBytes(); private static final ByteSizeValue JOB_MEMORY_REQUIREMENT = ByteSizeValue.ofMb(10); - private static final Set ROLES_WITH_ML = - Set.of(DiscoveryNodeRole.MASTER_ROLE, DiscoveryNodeRole.ML_ROLE, DiscoveryNodeRole.DATA_ROLE); - private static final Set ROLES_WITHOUT_ML = - Set.of(DiscoveryNodeRole.MASTER_ROLE, DiscoveryNodeRole.DATA_ROLE); + private static final Set ROLES_WITH_ML = Set.of( + DiscoveryNodeRole.MASTER_ROLE, + DiscoveryNodeRole.ML_ROLE, + DiscoveryNodeRole.DATA_ROLE + ); + private static final Set ROLES_WITHOUT_ML = Set.of(DiscoveryNodeRole.MASTER_ROLE, DiscoveryNodeRole.DATA_ROLE); private MlMemoryTracker memoryTracker; private boolean isMemoryTrackerRecentlyRefreshed; @@ -119,16 +121,28 @@ public void testSelectLeastLoadedMlNodeForAnomalyDetectorJob_maxCapacityCountLim shuffled(cs.nodes().getAllNodes()), job.getId(), MlTasks.JOB_TASK_NAME, - memoryTracker, 0, node -> nodeFilter(node, job)); - PersistentTasksCustomMetadata.Assignment result = jobNodeSelector.selectNode(maxRunningJobsPerNode, + memoryTracker, + 0, + node -> nodeFilter(node, job) + ); + PersistentTasksCustomMetadata.Assignment result = jobNodeSelector.selectNode( + maxRunningJobsPerNode, 2, maxMachineMemoryPercent, MAX_JOB_BYTES, - false); + false + ); assertNull(result.getExecutorNode()); - assertThat(result.getExplanation(), containsString( - "node is full. Number of opened jobs and allocated native inference processes [" + maxRunningJobsPerNode - + "], xpack.ml.max_open_jobs [" + maxRunningJobsPerNode + "]")); + assertThat( + result.getExplanation(), + containsString( + "node is full. Number of opened jobs and allocated native inference processes [" + + maxRunningJobsPerNode + + "], xpack.ml.max_open_jobs [" + + maxRunningJobsPerNode + + "]" + ) + ); } public void testSelectLeastLoadedMlNodeForDataFrameAnalyticsJob_maxCapacityCountLimiting() { @@ -147,17 +161,29 @@ public void testSelectLeastLoadedMlNodeForDataFrameAnalyticsJob_maxCapacityCount cs.build(), shuffled(cs.nodes().getAllNodes()), dataFrameAnalyticsId, - MlTasks.DATA_FRAME_ANALYTICS_TASK_NAME, memoryTracker, 0, - node -> TransportStartDataFrameAnalyticsAction.TaskExecutor.nodeFilter(node, createTaskParams(dataFrameAnalyticsId))); - PersistentTasksCustomMetadata.Assignment result = jobNodeSelector.selectNode(maxRunningJobsPerNode, + MlTasks.DATA_FRAME_ANALYTICS_TASK_NAME, + memoryTracker, + 0, + node -> TransportStartDataFrameAnalyticsAction.TaskExecutor.nodeFilter(node, createTaskParams(dataFrameAnalyticsId)) + ); + PersistentTasksCustomMetadata.Assignment result = jobNodeSelector.selectNode( + maxRunningJobsPerNode, 2, maxMachineMemoryPercent, MAX_JOB_BYTES, - false); + false + ); assertNull(result.getExecutorNode()); - assertThat(result.getExplanation(), containsString( - "node is full. Number of opened jobs and allocated native inference processes [" + maxRunningJobsPerNode - + "], xpack.ml.max_open_jobs [" + maxRunningJobsPerNode + "]")); + assertThat( + result.getExplanation(), + containsString( + "node is full. Number of opened jobs and allocated native inference processes [" + + maxRunningJobsPerNode + + "], xpack.ml.max_open_jobs [" + + maxRunningJobsPerNode + + "]" + ) + ); } public void testSelectLeastLoadedMlNodeForAnomalyDetectorJob_maxCapacityMemoryLimiting() { @@ -168,8 +194,8 @@ public void testSelectLeastLoadedMlNodeForAnomalyDetectorJob_maxCapacityMemoryLi // the value here must divide exactly into both (JOB_MEMORY_REQUIREMENT.getBytes() * 100) and // MachineLearning.NATIVE_EXECUTABLE_CODE_OVERHEAD.getBytes() int maxMachineMemoryPercent = 20; - long currentlyRunningJobMemory = MachineLearning.NATIVE_EXECUTABLE_CODE_OVERHEAD.getBytes() + - currentlyRunningJobsPerNode * JOB_MEMORY_REQUIREMENT.getBytes(); + long currentlyRunningJobMemory = MachineLearning.NATIVE_EXECUTABLE_CODE_OVERHEAD.getBytes() + currentlyRunningJobsPerNode + * JOB_MEMORY_REQUIREMENT.getBytes(); long machineMemory = currentlyRunningJobMemory * 100 / maxMachineMemoryPercent; Map nodeAttr = Map.of(MachineLearning.MACHINE_MEMORY_NODE_ATTR, Long.toString(machineMemory)); @@ -183,19 +209,37 @@ public void testSelectLeastLoadedMlNodeForAnomalyDetectorJob_maxCapacityMemoryLi shuffled(cs.nodes().getAllNodes()), job.getId(), MlTasks.JOB_TASK_NAME, - memoryTracker, 0, node -> nodeFilter(node, job)); - PersistentTasksCustomMetadata.Assignment result = jobNodeSelector.selectNode(maxRunningJobsPerNode, + memoryTracker, + 0, + node -> nodeFilter(node, job) + ); + PersistentTasksCustomMetadata.Assignment result = jobNodeSelector.selectNode( + maxRunningJobsPerNode, 2, maxMachineMemoryPercent, MAX_JOB_BYTES, - false); + false + ); assertNull(result.getExecutorNode()); - assertThat(result.getExplanation(), containsString("node has insufficient available memory. " - + "Available memory for ML [" + currentlyRunningJobMemory + " (" + ByteSizeValue.ofBytes(currentlyRunningJobMemory) - + ")], memory required by existing jobs [" - + currentlyRunningJobMemory + " (" + ByteSizeValue.ofBytes(currentlyRunningJobMemory) - + ")], estimated memory required for this job [" + JOB_MEMORY_REQUIREMENT.getBytes() - + " (" + ByteSizeValue.ofBytes(JOB_MEMORY_REQUIREMENT.getBytes()) + ")]")); + assertThat( + result.getExplanation(), + containsString( + "node has insufficient available memory. " + + "Available memory for ML [" + + currentlyRunningJobMemory + + " (" + + ByteSizeValue.ofBytes(currentlyRunningJobMemory) + + ")], memory required by existing jobs [" + + currentlyRunningJobMemory + + " (" + + ByteSizeValue.ofBytes(currentlyRunningJobMemory) + + ")], estimated memory required for this job [" + + JOB_MEMORY_REQUIREMENT.getBytes() + + " (" + + ByteSizeValue.ofBytes(JOB_MEMORY_REQUIREMENT.getBytes()) + + ")]" + ) + ); } public void testSelectLeastLoadedMlNodeForDataFrameAnalyticsJob_givenTaskHasNullState() { @@ -213,13 +257,18 @@ public void testSelectLeastLoadedMlNodeForDataFrameAnalyticsJob_givenTaskHasNull cs.build(), shuffled(cs.nodes().getAllNodes()), dataFrameAnalyticsId, - MlTasks.DATA_FRAME_ANALYTICS_TASK_NAME, memoryTracker, 0, - node -> TransportStartDataFrameAnalyticsAction.TaskExecutor.nodeFilter(node, createTaskParams(dataFrameAnalyticsId))); - PersistentTasksCustomMetadata.Assignment result = jobNodeSelector.selectNode(maxRunningJobsPerNode, + MlTasks.DATA_FRAME_ANALYTICS_TASK_NAME, + memoryTracker, + 0, + node -> TransportStartDataFrameAnalyticsAction.TaskExecutor.nodeFilter(node, createTaskParams(dataFrameAnalyticsId)) + ); + PersistentTasksCustomMetadata.Assignment result = jobNodeSelector.selectNode( + maxRunningJobsPerNode, 2, maxMachineMemoryPercent, MAX_JOB_BYTES, - false); + false + ); assertNotNull(result.getExecutorNode()); } @@ -241,17 +290,33 @@ public void testSelectLeastLoadedMlNodeForAnomalyDetectorJob_firstJobTooBigMemor shuffled(cs.nodes().getAllNodes()), job.getId(), MlTasks.JOB_TASK_NAME, - memoryTracker, 0, node -> nodeFilter(node, job)); - PersistentTasksCustomMetadata.Assignment result = jobNodeSelector.selectNode(maxRunningJobsPerNode, + memoryTracker, + 0, + node -> nodeFilter(node, job) + ); + PersistentTasksCustomMetadata.Assignment result = jobNodeSelector.selectNode( + maxRunningJobsPerNode, 2, maxMachineMemoryPercent, MAX_JOB_BYTES, - false); + false + ); assertNull(result.getExecutorNode()); - assertThat(result.getExplanation(), containsString("node has insufficient available memory. " - + "Available memory for ML [" + (firstJobTotalMemory - 1) + " (" + ByteSizeValue.ofBytes((firstJobTotalMemory - 1)) - + ")], memory required by existing jobs [0 (0b)], estimated memory required for this job [" - + firstJobTotalMemory + " (" + ByteSizeValue.ofBytes(firstJobTotalMemory) + ")]")); + assertThat( + result.getExplanation(), + containsString( + "node has insufficient available memory. " + + "Available memory for ML [" + + (firstJobTotalMemory - 1) + + " (" + + ByteSizeValue.ofBytes((firstJobTotalMemory - 1)) + + ")], memory required by existing jobs [0 (0b)], estimated memory required for this job [" + + firstJobTotalMemory + + " (" + + ByteSizeValue.ofBytes(firstJobTotalMemory) + + ")]" + ) + ); } public void testSelectLeastLoadedMlNodeForDataFrameAnalyticsJob_maxCapacityMemoryLimiting() { @@ -262,8 +327,8 @@ public void testSelectLeastLoadedMlNodeForDataFrameAnalyticsJob_maxCapacityMemor // the value here must divide exactly into both (JOB_MEMORY_REQUIREMENT.getBytes() * 100) and // MachineLearning.NATIVE_EXECUTABLE_CODE_OVERHEAD.getBytes() int maxMachineMemoryPercent = 20; - long currentlyRunningJobMemory = MachineLearning.NATIVE_EXECUTABLE_CODE_OVERHEAD.getBytes() + - currentlyRunningJobsPerNode * JOB_MEMORY_REQUIREMENT.getBytes(); + long currentlyRunningJobMemory = MachineLearning.NATIVE_EXECUTABLE_CODE_OVERHEAD.getBytes() + currentlyRunningJobsPerNode + * JOB_MEMORY_REQUIREMENT.getBytes(); long machineMemory = currentlyRunningJobMemory * 100 / maxMachineMemoryPercent; Map nodeAttr = Map.of(MachineLearning.MACHINE_MEMORY_NODE_ATTR, Long.toString(machineMemory)); @@ -276,20 +341,38 @@ public void testSelectLeastLoadedMlNodeForDataFrameAnalyticsJob_maxCapacityMemor cs.build(), shuffled(cs.nodes().getAllNodes()), dataFrameAnalyticsId, - MlTasks.DATA_FRAME_ANALYTICS_TASK_NAME, memoryTracker, 0, - node -> TransportStartDataFrameAnalyticsAction.TaskExecutor.nodeFilter(node, createTaskParams(dataFrameAnalyticsId))); + MlTasks.DATA_FRAME_ANALYTICS_TASK_NAME, + memoryTracker, + 0, + node -> TransportStartDataFrameAnalyticsAction.TaskExecutor.nodeFilter(node, createTaskParams(dataFrameAnalyticsId)) + ); PersistentTasksCustomMetadata.Assignment result = jobNodeSelector.selectNode( maxRunningJobsPerNode, 2, maxMachineMemoryPercent, MAX_JOB_BYTES, - false); + false + ); assertNull(result.getExecutorNode()); - assertThat(result.getExplanation(), containsString("node has insufficient available memory. " - + "Available memory for ML [" + currentlyRunningJobMemory + " (" + ByteSizeValue.ofBytes(currentlyRunningJobMemory) - +")], memory required by existing jobs [" + currentlyRunningJobMemory + " (" + ByteSizeValue.ofBytes(currentlyRunningJobMemory) - +")], estimated memory required for this job [" + JOB_MEMORY_REQUIREMENT.getBytes() + " (" - + ByteSizeValue.ofBytes(JOB_MEMORY_REQUIREMENT.getBytes()) + ")]")); + assertThat( + result.getExplanation(), + containsString( + "node has insufficient available memory. " + + "Available memory for ML [" + + currentlyRunningJobMemory + + " (" + + ByteSizeValue.ofBytes(currentlyRunningJobMemory) + + ")], memory required by existing jobs [" + + currentlyRunningJobMemory + + " (" + + ByteSizeValue.ofBytes(currentlyRunningJobMemory) + + ")], estimated memory required for this job [" + + JOB_MEMORY_REQUIREMENT.getBytes() + + " (" + + ByteSizeValue.ofBytes(JOB_MEMORY_REQUIREMENT.getBytes()) + + ")]" + ) + ); } public void testSelectLeastLoadedMlNodeForDataFrameAnalyticsJob_firstJobTooBigMemoryLimiting() { @@ -309,27 +392,58 @@ public void testSelectLeastLoadedMlNodeForDataFrameAnalyticsJob_firstJobTooBigMe cs.build(), shuffled(cs.nodes().getAllNodes()), dataFrameAnalyticsId, - MlTasks.DATA_FRAME_ANALYTICS_TASK_NAME, memoryTracker, 0, - node -> TransportStartDataFrameAnalyticsAction.TaskExecutor.nodeFilter(node, createTaskParams(dataFrameAnalyticsId))); + MlTasks.DATA_FRAME_ANALYTICS_TASK_NAME, + memoryTracker, + 0, + node -> TransportStartDataFrameAnalyticsAction.TaskExecutor.nodeFilter(node, createTaskParams(dataFrameAnalyticsId)) + ); PersistentTasksCustomMetadata.Assignment result = jobNodeSelector.selectNode( maxRunningJobsPerNode, 2, maxMachineMemoryPercent, MAX_JOB_BYTES, - false); + false + ); assertNull(result.getExecutorNode()); - assertThat(result.getExplanation(), containsString("node has insufficient available memory. " - + "Available memory for ML [" + (firstJobTotalMemory - 1) + " (" + ByteSizeValue.ofBytes(firstJobTotalMemory - 1) - + ")], memory required by existing jobs [0 (0b)], estimated memory required for this job [" - + firstJobTotalMemory + " (" + ByteSizeValue.ofBytes(firstJobTotalMemory) + ")]")); + assertThat( + result.getExplanation(), + containsString( + "node has insufficient available memory. " + + "Available memory for ML [" + + (firstJobTotalMemory - 1) + + " (" + + ByteSizeValue.ofBytes(firstJobTotalMemory - 1) + + ")], memory required by existing jobs [0 (0b)], estimated memory required for this job [" + + firstJobTotalMemory + + " (" + + ByteSizeValue.ofBytes(firstJobTotalMemory) + + ")]" + ) + ); } public void testSelectLeastLoadedMlNode_noMlNodes() { DiscoveryNodes nodes = DiscoveryNodes.builder() - .add(new DiscoveryNode("_node_name1", "_node_id1", new TransportAddress(InetAddress.getLoopbackAddress(), 9300), - Collections.emptyMap(), ROLES_WITHOUT_ML, Version.CURRENT)) - .add(new DiscoveryNode("_node_name2", "_node_id2", new TransportAddress(InetAddress.getLoopbackAddress(), 9301), - Collections.emptyMap(), ROLES_WITHOUT_ML, Version.CURRENT)) + .add( + new DiscoveryNode( + "_node_name1", + "_node_id1", + new TransportAddress(InetAddress.getLoopbackAddress(), 9300), + Collections.emptyMap(), + ROLES_WITHOUT_ML, + Version.CURRENT + ) + ) + .add( + new DiscoveryNode( + "_node_name2", + "_node_id2", + new TransportAddress(InetAddress.getLoopbackAddress(), 9301), + Collections.emptyMap(), + ROLES_WITHOUT_ML, + Version.CURRENT + ) + ) .build(); PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); @@ -349,13 +463,11 @@ public void testSelectLeastLoadedMlNode_noMlNodes() { shuffled(cs.nodes().getAllNodes()), job.getId(), MlTasks.JOB_TASK_NAME, - memoryTracker, 0, node -> nodeFilter(node, job)); - PersistentTasksCustomMetadata.Assignment result = jobNodeSelector.selectNode( - 20, - 2, - 30, - MAX_JOB_BYTES, - false); + memoryTracker, + 0, + node -> nodeFilter(node, job) + ); + PersistentTasksCustomMetadata.Assignment result = jobNodeSelector.selectNode(20, 2, 30, MAX_JOB_BYTES, false); assertTrue(result.getExplanation().contains("node isn't a machine learning node")); assertNull(result.getExecutorNode()); } @@ -363,12 +475,36 @@ public void testSelectLeastLoadedMlNode_noMlNodes() { public void testSelectLeastLoadedMlNode_maxConcurrentOpeningJobs() { Map nodeAttr = Map.of(MachineLearning.MACHINE_MEMORY_NODE_ATTR, "1000000000"); DiscoveryNodes nodes = DiscoveryNodes.builder() - .add(new DiscoveryNode("_node_name1", "_node_id1", new TransportAddress(InetAddress.getLoopbackAddress(), 9300), - nodeAttr, ROLES_WITH_ML, Version.CURRENT)) - .add(new DiscoveryNode("_node_name2", "_node_id2", new TransportAddress(InetAddress.getLoopbackAddress(), 9301), - nodeAttr, ROLES_WITH_ML, Version.CURRENT)) - .add(new DiscoveryNode("_node_name3", "_node_id3", new TransportAddress(InetAddress.getLoopbackAddress(), 9302), - nodeAttr, ROLES_WITH_ML, Version.CURRENT)) + .add( + new DiscoveryNode( + "_node_name1", + "_node_id1", + new TransportAddress(InetAddress.getLoopbackAddress(), 9300), + nodeAttr, + ROLES_WITH_ML, + Version.CURRENT + ) + ) + .add( + new DiscoveryNode( + "_node_name2", + "_node_id2", + new TransportAddress(InetAddress.getLoopbackAddress(), 9301), + nodeAttr, + ROLES_WITH_ML, + Version.CURRENT + ) + ) + .add( + new DiscoveryNode( + "_node_name3", + "_node_id3", + new TransportAddress(InetAddress.getLoopbackAddress(), 9302), + nodeAttr, + ROLES_WITH_ML, + Version.CURRENT + ) + ) .build(); PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); @@ -393,13 +529,11 @@ public void testSelectLeastLoadedMlNode_maxConcurrentOpeningJobs() { shuffled(cs.nodes().getAllNodes()), job6.getId(), MlTasks.JOB_TASK_NAME, - memoryTracker, 0, node -> nodeFilter(node, job6)); - PersistentTasksCustomMetadata.Assignment result = jobNodeSelector.selectNode( - 10, - 2, - 30, - MAX_JOB_BYTES, - false); + memoryTracker, + 0, + node -> nodeFilter(node, job6) + ); + PersistentTasksCustomMetadata.Assignment result = jobNodeSelector.selectNode(10, 2, 30, MAX_JOB_BYTES, false); assertEquals("_node_id3", result.getExecutorNode()); tasksBuilder = PersistentTasksCustomMetadata.builder(tasks); @@ -418,18 +552,17 @@ public void testSelectLeastLoadedMlNode_maxConcurrentOpeningJobs() { MlTasks.JOB_TASK_NAME, memoryTracker, 0, - node -> nodeFilter(node, job7)); - result = jobNodeSelector.selectNode(10, - 2, - 30, - MAX_JOB_BYTES, - false); + node -> nodeFilter(node, job7) + ); + result = jobNodeSelector.selectNode(10, 2, 30, MAX_JOB_BYTES, false); assertNull("no node selected, because OPENING state", result.getExecutorNode()); assertTrue(result.getExplanation().contains("Node exceeds [2] the maximum number of jobs [2] in opening state")); tasksBuilder = PersistentTasksCustomMetadata.builder(tasks); - tasksBuilder.reassignTask(MlTasks.jobTaskId(job6.getId()), - new PersistentTasksCustomMetadata.Assignment("_node_id3", "test assignment")); + tasksBuilder.reassignTask( + MlTasks.jobTaskId(job6.getId()), + new PersistentTasksCustomMetadata.Assignment("_node_id3", "test assignment") + ); tasks = tasksBuilder.build(); csBuilder = ClusterState.builder(cs); @@ -442,7 +575,8 @@ public void testSelectLeastLoadedMlNode_maxConcurrentOpeningJobs() { MlTasks.JOB_TASK_NAME, memoryTracker, 0, - node -> nodeFilter(node, job7)); + node -> nodeFilter(node, job7) + ); result = jobNodeSelector.selectNode(10, 2, 30, MAX_JOB_BYTES, false); assertNull("no node selected, because stale task", result.getExecutorNode()); assertTrue(result.getExplanation().contains("Node exceeds [2] the maximum number of jobs [2] in opening state")); @@ -461,7 +595,8 @@ public void testSelectLeastLoadedMlNode_maxConcurrentOpeningJobs() { MlTasks.JOB_TASK_NAME, memoryTracker, 0, - node -> nodeFilter(node, job7)); + node -> nodeFilter(node, job7) + ); result = jobNodeSelector.selectNode(10, 2, 30, MAX_JOB_BYTES, false); assertNull("no node selected, because null state", result.getExecutorNode()); assertTrue(result.getExplanation().contains("Node exceeds [2] the maximum number of jobs [2] in opening state")); @@ -470,19 +605,45 @@ public void testSelectLeastLoadedMlNode_maxConcurrentOpeningJobs() { public void testSelectLeastLoadedMlNode_concurrentOpeningJobsAndStaleFailedJob() { Map nodeAttr = Map.of(MachineLearning.MACHINE_MEMORY_NODE_ATTR, "1000000000"); DiscoveryNodes nodes = DiscoveryNodes.builder() - .add(new DiscoveryNode("_node_name1", "_node_id1", new TransportAddress(InetAddress.getLoopbackAddress(), 9300), - nodeAttr, ROLES_WITH_ML, Version.CURRENT)) - .add(new DiscoveryNode("_node_name2", "_node_id2", new TransportAddress(InetAddress.getLoopbackAddress(), 9301), - nodeAttr, ROLES_WITH_ML, Version.CURRENT)) - .add(new DiscoveryNode("_node_name3", "_node_id3", new TransportAddress(InetAddress.getLoopbackAddress(), 9302), - nodeAttr, ROLES_WITH_ML, Version.CURRENT)) + .add( + new DiscoveryNode( + "_node_name1", + "_node_id1", + new TransportAddress(InetAddress.getLoopbackAddress(), 9300), + nodeAttr, + ROLES_WITH_ML, + Version.CURRENT + ) + ) + .add( + new DiscoveryNode( + "_node_name2", + "_node_id2", + new TransportAddress(InetAddress.getLoopbackAddress(), 9301), + nodeAttr, + ROLES_WITH_ML, + Version.CURRENT + ) + ) + .add( + new DiscoveryNode( + "_node_name3", + "_node_id3", + new TransportAddress(InetAddress.getLoopbackAddress(), 9302), + nodeAttr, + ROLES_WITH_ML, + Version.CURRENT + ) + ) .build(); PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); OpenJobPersistentTasksExecutorTests.addJobTask("job_id1", "_node_id1", JobState.fromString("failed"), tasksBuilder); // This will make the allocation stale for job_id1 - tasksBuilder.reassignTask(MlTasks.jobTaskId("job_id1"), - new PersistentTasksCustomMetadata.Assignment("_node_id1", "test assignment")); + tasksBuilder.reassignTask( + MlTasks.jobTaskId("job_id1"), + new PersistentTasksCustomMetadata.Assignment("_node_id1", "test assignment") + ); OpenJobPersistentTasksExecutorTests.addJobTask("job_id2", "_node_id1", null, tasksBuilder); OpenJobPersistentTasksExecutorTests.addJobTask("job_id3", "_node_id2", null, tasksBuilder); OpenJobPersistentTasksExecutorTests.addJobTask("job_id4", "_node_id2", null, tasksBuilder); @@ -505,12 +666,11 @@ public void testSelectLeastLoadedMlNode_concurrentOpeningJobsAndStaleFailedJob() shuffled(cs.nodes().getAllNodes()), job7.getId(), MlTasks.JOB_TASK_NAME, - memoryTracker, 0, node -> nodeFilter(node, job7)); - PersistentTasksCustomMetadata.Assignment result = jobNodeSelector.selectNode(10, - 2, - 30, - MAX_JOB_BYTES, - false); + memoryTracker, + 0, + node -> nodeFilter(node, job7) + ); + PersistentTasksCustomMetadata.Assignment result = jobNodeSelector.selectNode(10, 2, 30, MAX_JOB_BYTES, false); assertEquals("_node_id1", result.getExecutorNode()); tasksBuilder = PersistentTasksCustomMetadata.builder(tasks); @@ -528,7 +688,8 @@ public void testSelectLeastLoadedMlNode_concurrentOpeningJobsAndStaleFailedJob() MlTasks.JOB_TASK_NAME, memoryTracker, 0, - node -> nodeFilter(node, job8)); + node -> nodeFilter(node, job8) + ); result = jobNodeSelector.selectNode(10, 2, 30, MAX_JOB_BYTES, false); assertNull("no node selected, because OPENING state", result.getExecutorNode()); assertTrue(result.getExplanation().contains("Node exceeds [2] the maximum number of jobs [2] in opening state")); @@ -537,10 +698,26 @@ public void testSelectLeastLoadedMlNode_concurrentOpeningJobsAndStaleFailedJob() public void testSelectLeastLoadedMlNode_noCompatibleJobTypeNodes() { Map nodeAttr = Map.of(MachineLearning.MACHINE_MEMORY_NODE_ATTR, "1000000000"); DiscoveryNodes nodes = DiscoveryNodes.builder() - .add(new DiscoveryNode("_node_name1", "_node_id1", new TransportAddress(InetAddress.getLoopbackAddress(), 9300), - nodeAttr, ROLES_WITH_ML, Version.CURRENT)) - .add(new DiscoveryNode("_node_name2", "_node_id2", new TransportAddress(InetAddress.getLoopbackAddress(), 9301), - nodeAttr, ROLES_WITH_ML, Version.CURRENT)) + .add( + new DiscoveryNode( + "_node_name1", + "_node_id1", + new TransportAddress(InetAddress.getLoopbackAddress(), 9300), + nodeAttr, + ROLES_WITH_ML, + Version.CURRENT + ) + ) + .add( + new DiscoveryNode( + "_node_name2", + "_node_id2", + new TransportAddress(InetAddress.getLoopbackAddress(), 9301), + nodeAttr, + ROLES_WITH_ML, + Version.CURRENT + ) + ) .build(); PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); @@ -564,12 +741,11 @@ public void testSelectLeastLoadedMlNode_noCompatibleJobTypeNodes() { shuffled(cs.nodes().getAllNodes()), job.getId(), MlTasks.JOB_TASK_NAME, - memoryTracker, 0, node -> nodeFilter(node, job)); - PersistentTasksCustomMetadata.Assignment result = jobNodeSelector.selectNode(10, - 2, - 30, - MAX_JOB_BYTES, - false); + memoryTracker, + 0, + node -> nodeFilter(node, job) + ); + PersistentTasksCustomMetadata.Assignment result = jobNodeSelector.selectNode(10, 2, 30, MAX_JOB_BYTES, false); assertThat(result.getExplanation(), containsString("node does not support jobs of type [incompatible_type]")); assertNull(result.getExecutorNode()); } @@ -644,10 +820,26 @@ public void testSelectLeastLoadedMlNode_reasonsAreInDeterministicOrder() { public void testSelectLeastLoadedMlNode_noNodesMatchingModelSnapshotMinVersion() { Map nodeAttr = Map.of(MachineLearning.MACHINE_MEMORY_NODE_ATTR, "1000000000"); DiscoveryNodes nodes = DiscoveryNodes.builder() - .add(new DiscoveryNode("_node_name1", "_node_id1", new TransportAddress(InetAddress.getLoopbackAddress(), 9300), - nodeAttr, ROLES_WITH_ML, Version.fromString("6.2.0"))) - .add(new DiscoveryNode("_node_name2", "_node_id2", new TransportAddress(InetAddress.getLoopbackAddress(), 9301), - nodeAttr, ROLES_WITH_ML, Version.fromString("6.1.0"))) + .add( + new DiscoveryNode( + "_node_name1", + "_node_id1", + new TransportAddress(InetAddress.getLoopbackAddress(), 9300), + nodeAttr, + ROLES_WITH_ML, + Version.fromString("6.2.0") + ) + ) + .add( + new DiscoveryNode( + "_node_name2", + "_node_id2", + new TransportAddress(InetAddress.getLoopbackAddress(), 9301), + nodeAttr, + ROLES_WITH_ML, + Version.fromString("6.1.0") + ) + ) .build(); PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); @@ -668,24 +860,39 @@ public void testSelectLeastLoadedMlNode_noNodesMatchingModelSnapshotMinVersion() cs.build(), shuffled(cs.nodes().getAllNodes()), job.getId(), - MlTasks.JOB_TASK_NAME, memoryTracker, 0, node -> nodeFilter(node, job)); - PersistentTasksCustomMetadata.Assignment result = jobNodeSelector.selectNode(10, - 2, - 30, - MAX_JOB_BYTES, - false); - assertThat(result.getExplanation(), containsString( - "job's model snapshot requires a node of version [6.3.0] or higher")); + MlTasks.JOB_TASK_NAME, + memoryTracker, + 0, + node -> nodeFilter(node, job) + ); + PersistentTasksCustomMetadata.Assignment result = jobNodeSelector.selectNode(10, 2, 30, MAX_JOB_BYTES, false); + assertThat(result.getExplanation(), containsString("job's model snapshot requires a node of version [6.3.0] or higher")); assertNull(result.getExecutorNode()); } public void testSelectLeastLoadedMlNode_jobWithRules() { Map nodeAttr = Map.of(MachineLearning.MACHINE_MEMORY_NODE_ATTR, "1000000000"); DiscoveryNodes nodes = DiscoveryNodes.builder() - .add(new DiscoveryNode("_node_name1", "_node_id1", new TransportAddress(InetAddress.getLoopbackAddress(), 9300), - nodeAttr, ROLES_WITH_ML, Version.fromString("6.2.0"))) - .add(new DiscoveryNode("_node_name2", "_node_id2", new TransportAddress(InetAddress.getLoopbackAddress(), 9301), - nodeAttr, ROLES_WITH_ML, Version.fromString("6.4.0"))) + .add( + new DiscoveryNode( + "_node_name1", + "_node_id1", + new TransportAddress(InetAddress.getLoopbackAddress(), 9300), + nodeAttr, + ROLES_WITH_ML, + Version.fromString("6.2.0") + ) + ) + .add( + new DiscoveryNode( + "_node_name2", + "_node_id2", + new TransportAddress(InetAddress.getLoopbackAddress(), 9301), + nodeAttr, + ROLES_WITH_ML, + Version.fromString("6.4.0") + ) + ) .build(); PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); @@ -704,22 +911,37 @@ public void testSelectLeastLoadedMlNode_jobWithRules() { shuffled(cs.nodes().getAllNodes()), job.getId(), MlTasks.JOB_TASK_NAME, - memoryTracker, 0, node -> nodeFilter(node, job)); - PersistentTasksCustomMetadata.Assignment result = jobNodeSelector.selectNode(10, - 2, - 30, - MAX_JOB_BYTES, - false); + memoryTracker, + 0, + node -> nodeFilter(node, job) + ); + PersistentTasksCustomMetadata.Assignment result = jobNodeSelector.selectNode(10, 2, 30, MAX_JOB_BYTES, false); assertNotNull(result.getExecutorNode()); } public void testSelectMlNodeOnlyOutOfCandidates() { Map nodeAttr = Map.of(MachineLearning.MACHINE_MEMORY_NODE_ATTR, "1000000000"); DiscoveryNodes nodes = DiscoveryNodes.builder() - .add(new DiscoveryNode("_node_name1", "_node_id1", new TransportAddress(InetAddress.getLoopbackAddress(), 9300), - nodeAttr, ROLES_WITH_ML, Version.CURRENT)) - .add(new DiscoveryNode("_node_name2", "_node_id2", new TransportAddress(InetAddress.getLoopbackAddress(), 9301), - nodeAttr, ROLES_WITH_ML, Version.CURRENT)) + .add( + new DiscoveryNode( + "_node_name1", + "_node_id1", + new TransportAddress(InetAddress.getLoopbackAddress(), 9300), + nodeAttr, + ROLES_WITH_ML, + Version.CURRENT + ) + ) + .add( + new DiscoveryNode( + "_node_name2", + "_node_id2", + new TransportAddress(InetAddress.getLoopbackAddress(), 9301), + nodeAttr, + ROLES_WITH_ML, + Version.CURRENT + ) + ) .build(); PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); @@ -735,25 +957,42 @@ public void testSelectMlNodeOnlyOutOfCandidates() { DiscoveryNode candidate = nodes.getNodes().get(randomBoolean() ? "_node_id1" : "_node_id2"); Job job = jobWithRules("job_with_rules"); - JobNodeSelector jobNodeSelector = new JobNodeSelector(cs.build(), + JobNodeSelector jobNodeSelector = new JobNodeSelector( + cs.build(), Collections.singletonList(candidate), - job.getId(), MlTasks.JOB_TASK_NAME, - memoryTracker, 0, node -> nodeFilter(node, job)); - PersistentTasksCustomMetadata.Assignment result = jobNodeSelector.selectNode(10, - 2, - 30, - MAX_JOB_BYTES, - false); + job.getId(), + MlTasks.JOB_TASK_NAME, + memoryTracker, + 0, + node -> nodeFilter(node, job) + ); + PersistentTasksCustomMetadata.Assignment result = jobNodeSelector.selectNode(10, 2, 30, MAX_JOB_BYTES, false); assertNotNull(result.getExecutorNode()); assertThat(result.getExecutorNode(), equalTo(candidate.getId())); } public void testConsiderLazyAssignmentWithNoLazyNodes() { DiscoveryNodes nodes = DiscoveryNodes.builder() - .add(new DiscoveryNode("_node_name1", "_node_id1", new TransportAddress(InetAddress.getLoopbackAddress(), 9300), - Collections.emptyMap(), ROLES_WITHOUT_ML, Version.CURRENT)) - .add(new DiscoveryNode("_node_name2", "_node_id2", new TransportAddress(InetAddress.getLoopbackAddress(), 9301), - Collections.emptyMap(), ROLES_WITHOUT_ML, Version.CURRENT)) + .add( + new DiscoveryNode( + "_node_name1", + "_node_id1", + new TransportAddress(InetAddress.getLoopbackAddress(), 9300), + Collections.emptyMap(), + ROLES_WITHOUT_ML, + Version.CURRENT + ) + ) + .add( + new DiscoveryNode( + "_node_name2", + "_node_id2", + new TransportAddress(InetAddress.getLoopbackAddress(), 9301), + Collections.emptyMap(), + ROLES_WITHOUT_ML, + Version.CURRENT + ) + ) .build(); ClusterState.Builder cs = ClusterState.builder(new ClusterName("_name")); @@ -765,19 +1004,39 @@ public void testConsiderLazyAssignmentWithNoLazyNodes() { shuffled(cs.nodes().getAllNodes()), job.getId(), MlTasks.JOB_TASK_NAME, - memoryTracker, 0, node -> nodeFilter(node, job)); - PersistentTasksCustomMetadata.Assignment result = - jobNodeSelector.considerLazyAssignment(new PersistentTasksCustomMetadata.Assignment(null, "foo")); + memoryTracker, + 0, + node -> nodeFilter(node, job) + ); + PersistentTasksCustomMetadata.Assignment result = jobNodeSelector.considerLazyAssignment( + new PersistentTasksCustomMetadata.Assignment(null, "foo") + ); assertEquals("foo", result.getExplanation()); assertNull(result.getExecutorNode()); } public void testConsiderLazyAssignmentWithLazyNodes() { DiscoveryNodes nodes = DiscoveryNodes.builder() - .add(new DiscoveryNode("_node_name1", "_node_id1", new TransportAddress(InetAddress.getLoopbackAddress(), 9300), - Collections.emptyMap(), ROLES_WITHOUT_ML, Version.CURRENT)) - .add(new DiscoveryNode("_node_name2", "_node_id2", new TransportAddress(InetAddress.getLoopbackAddress(), 9301), - Collections.emptyMap(), ROLES_WITHOUT_ML, Version.CURRENT)) + .add( + new DiscoveryNode( + "_node_name1", + "_node_id1", + new TransportAddress(InetAddress.getLoopbackAddress(), 9300), + Collections.emptyMap(), + ROLES_WITHOUT_ML, + Version.CURRENT + ) + ) + .add( + new DiscoveryNode( + "_node_name2", + "_node_id2", + new TransportAddress(InetAddress.getLoopbackAddress(), 9301), + Collections.emptyMap(), + ROLES_WITHOUT_ML, + Version.CURRENT + ) + ) .build(); ClusterState.Builder cs = ClusterState.builder(new ClusterName("_name")); @@ -789,9 +1048,13 @@ public void testConsiderLazyAssignmentWithLazyNodes() { shuffled(cs.nodes().getAllNodes()), job.getId(), MlTasks.JOB_TASK_NAME, - memoryTracker, randomIntBetween(1, 3), node -> nodeFilter(node, job)); - PersistentTasksCustomMetadata.Assignment result = - jobNodeSelector.considerLazyAssignment(new PersistentTasksCustomMetadata.Assignment(null, "foo")); + memoryTracker, + randomIntBetween(1, 3), + node -> nodeFilter(node, job) + ); + PersistentTasksCustomMetadata.Assignment result = jobNodeSelector.considerLazyAssignment( + new PersistentTasksCustomMetadata.Assignment(null, "foo") + ); assertEquals(JobNodeSelector.AWAITING_LAZY_ASSIGNMENT.getExplanation(), result.getExplanation()); assertNull(result.getExecutorNode()); } @@ -814,50 +1077,78 @@ public void testMaximumPossibleNodeMemoryTooSmall() { shuffled(cs.nodes().getAllNodes()), job.getId(), MlTasks.JOB_TASK_NAME, - memoryTracker, randomIntBetween(1, 3), node -> nodeFilter(node, job)); - PersistentTasksCustomMetadata.Assignment result = jobNodeSelector.selectNode(maxRunningJobsPerNode, + memoryTracker, + randomIntBetween(1, 3), + node -> nodeFilter(node, job) + ); + PersistentTasksCustomMetadata.Assignment result = jobNodeSelector.selectNode( + maxRunningJobsPerNode, 2, maxMachineMemoryPercent, 10L, - false); + false + ); assertNull(result.getExecutorNode()); - assertThat(result.getExplanation(), - containsString("[job_id1000] not waiting for node assignment as estimated job size " + - "[31458280] is greater than largest possible job size [3]")); + assertThat( + result.getExplanation(), + containsString( + "[job_id1000] not waiting for node assignment as estimated job size " + + "[31458280] is greater than largest possible job size [3]" + ) + ); } public void testPerceivedCapacityAndMaxFreeMemory() { DiscoveryNodes nodes = DiscoveryNodes.builder() - .add(new DiscoveryNode("not_ml_node_name", "_node_id", new TransportAddress(InetAddress.getLoopbackAddress(), 9300), - Collections.emptyMap(), ROLES_WITHOUT_ML, Version.CURRENT)) - .add(new DiscoveryNode( - "filled_ml_node_name", - "filled_ml_node_id", - new TransportAddress(InetAddress.getLoopbackAddress(), 9301), - MapBuilder.newMapBuilder() - .put(MachineLearning.MAX_JVM_SIZE_NODE_ATTR, "10") - .put(MachineLearning.MACHINE_MEMORY_NODE_ATTR, Long.toString(ByteSizeValue.ofGb(30).getBytes())) - .map(), - ROLES_WITH_ML, - Version.CURRENT)) - .add(new DiscoveryNode("not_filled_ml_node", - "not_filled_ml_node_id", - new TransportAddress(InetAddress.getLoopbackAddress(), 9302), - MapBuilder.newMapBuilder() - .put(MachineLearning.MAX_JVM_SIZE_NODE_ATTR, "10") - .put(MachineLearning.MACHINE_MEMORY_NODE_ATTR, Long.toString(ByteSizeValue.ofGb(30).getBytes())) - .map(), - ROLES_WITH_ML, - Version.CURRENT)) - .add(new DiscoveryNode("not_filled_smaller_ml_node", - "not_filled_smaller_ml_node_id", - new TransportAddress(InetAddress.getLoopbackAddress(), 9303), - MapBuilder.newMapBuilder() - .put(MachineLearning.MAX_JVM_SIZE_NODE_ATTR, "10") - .put(MachineLearning.MACHINE_MEMORY_NODE_ATTR, Long.toString(ByteSizeValue.ofGb(10).getBytes())) - .map(), - ROLES_WITH_ML, - Version.CURRENT)) + .add( + new DiscoveryNode( + "not_ml_node_name", + "_node_id", + new TransportAddress(InetAddress.getLoopbackAddress(), 9300), + Collections.emptyMap(), + ROLES_WITHOUT_ML, + Version.CURRENT + ) + ) + .add( + new DiscoveryNode( + "filled_ml_node_name", + "filled_ml_node_id", + new TransportAddress(InetAddress.getLoopbackAddress(), 9301), + MapBuilder.newMapBuilder() + .put(MachineLearning.MAX_JVM_SIZE_NODE_ATTR, "10") + .put(MachineLearning.MACHINE_MEMORY_NODE_ATTR, Long.toString(ByteSizeValue.ofGb(30).getBytes())) + .map(), + ROLES_WITH_ML, + Version.CURRENT + ) + ) + .add( + new DiscoveryNode( + "not_filled_ml_node", + "not_filled_ml_node_id", + new TransportAddress(InetAddress.getLoopbackAddress(), 9302), + MapBuilder.newMapBuilder() + .put(MachineLearning.MAX_JVM_SIZE_NODE_ATTR, "10") + .put(MachineLearning.MACHINE_MEMORY_NODE_ATTR, Long.toString(ByteSizeValue.ofGb(30).getBytes())) + .map(), + ROLES_WITH_ML, + Version.CURRENT + ) + ) + .add( + new DiscoveryNode( + "not_filled_smaller_ml_node", + "not_filled_smaller_ml_node_id", + new TransportAddress(InetAddress.getLoopbackAddress(), 9303), + MapBuilder.newMapBuilder() + .put(MachineLearning.MAX_JVM_SIZE_NODE_ATTR, "10") + .put(MachineLearning.MACHINE_MEMORY_NODE_ATTR, Long.toString(ByteSizeValue.ofGb(10).getBytes())) + .map(), + ROLES_WITH_ML, + Version.CURRENT + ) + ) .build(); PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); @@ -876,14 +1167,16 @@ public void testPerceivedCapacityAndMaxFreeMemory() { shuffled(cs.nodes().getAllNodes()), job.getId(), MlTasks.JOB_TASK_NAME, - memoryTracker, 0, node -> nodeFilter(node, job)); - Tuple capacityAndFreeMemory = jobNodeSelector.perceivedCapacityAndMaxFreeMemory( - 10, - false, - 1); + memoryTracker, + 0, + node -> nodeFilter(node, job) + ); + Tuple capacityAndFreeMemory = jobNodeSelector.perceivedCapacityAndMaxFreeMemory(10, false, 1); assertThat(capacityAndFreeMemory.v2(), equalTo(ByteSizeValue.ofGb(3).getBytes())); - assertThat(capacityAndFreeMemory.v1(), - equalTo(new NativeMemoryCapacity(ByteSizeValue.ofGb(7).getBytes(), ByteSizeValue.ofGb(3).getBytes(), 10L))); + assertThat( + capacityAndFreeMemory.v1(), + equalTo(new NativeMemoryCapacity(ByteSizeValue.ofGb(7).getBytes(), ByteSizeValue.ofGb(3).getBytes(), 10L)) + ); } private ClusterState.Builder fillNodesWithRunningJobs(Map nodeAttr, int numNodes, int numRunningJobsPerNode) { @@ -891,8 +1184,13 @@ private ClusterState.Builder fillNodesWithRunningJobs(Map nodeAt return fillNodesWithRunningJobs(nodeAttr, numNodes, numRunningJobsPerNode, JobState.OPENED, DataFrameAnalyticsState.STARTED); } - private ClusterState.Builder fillNodesWithRunningJobs(Map nodeAttr, int numNodes, int numRunningJobsPerNode, - JobState anomalyDetectionJobState, DataFrameAnalyticsState dfAnalyticsJobState) { + private ClusterState.Builder fillNodesWithRunningJobs( + Map nodeAttr, + int numNodes, + int numRunningJobsPerNode, + JobState anomalyDetectionJobState, + DataFrameAnalyticsState dfAnalyticsJobState + ) { DiscoveryNodes.Builder nodes = DiscoveryNodes.builder(); PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); @@ -930,19 +1228,34 @@ static Collection shuffled(Collection nodes) { return toShuffle; } - static void addDataFrameAnalyticsJobTask(String id, String nodeId, DataFrameAnalyticsState state, - PersistentTasksCustomMetadata.Builder builder) { + static void addDataFrameAnalyticsJobTask( + String id, + String nodeId, + DataFrameAnalyticsState state, + PersistentTasksCustomMetadata.Builder builder + ) { addDataFrameAnalyticsJobTask(id, nodeId, state, builder, false, false); } - static void addDataFrameAnalyticsJobTask(String id, String nodeId, DataFrameAnalyticsState state, - PersistentTasksCustomMetadata.Builder builder, boolean isStale, boolean allowLazyStart) { - builder.addTask(MlTasks.dataFrameAnalyticsTaskId(id), MlTasks.DATA_FRAME_ANALYTICS_TASK_NAME, + static void addDataFrameAnalyticsJobTask( + String id, + String nodeId, + DataFrameAnalyticsState state, + PersistentTasksCustomMetadata.Builder builder, + boolean isStale, + boolean allowLazyStart + ) { + builder.addTask( + MlTasks.dataFrameAnalyticsTaskId(id), + MlTasks.DATA_FRAME_ANALYTICS_TASK_NAME, new StartDataFrameAnalyticsAction.TaskParams(id, Version.CURRENT, allowLazyStart), - new PersistentTasksCustomMetadata.Assignment(nodeId, "test assignment")); + new PersistentTasksCustomMetadata.Assignment(nodeId, "test assignment") + ); if (state != null) { - builder.updateTaskState(MlTasks.dataFrameAnalyticsTaskId(id), - new DataFrameAnalyticsTaskState(state, builder.getLastAllocationId() - (isStale ? 1 : 0), null)); + builder.updateTaskState( + MlTasks.dataFrameAnalyticsTaskId(id), + new DataFrameAnalyticsTaskState(state, builder.getLastAllocationId() - (isStale ? 1 : 0), null) + ); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/NodeLoadDetectorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/NodeLoadDetectorTests.java index 9b18e6920f420..b4a78c7349b1c 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/NodeLoadDetectorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/NodeLoadDetectorTests.java @@ -62,14 +62,46 @@ public void testNodeLoadDetection() { nodeAttr.put(MachineLearning.MACHINE_MEMORY_NODE_ATTR, "-1"); // MachineLearning.MACHINE_MEMORY_NODE_ATTR negative, so this will fall back to allocating by count DiscoveryNodes nodes = DiscoveryNodes.builder() - .add(new DiscoveryNode("_node_name1", "_node_id1", new TransportAddress(InetAddress.getLoopbackAddress(), 9300), - nodeAttr, Collections.emptySet(), Version.CURRENT)) - .add(new DiscoveryNode("_node_name2", "_node_id2", new TransportAddress(InetAddress.getLoopbackAddress(), 9301), - nodeAttr, Collections.emptySet(), Version.CURRENT)) - .add(new DiscoveryNode("_node_name3", "_node_id3", new TransportAddress(InetAddress.getLoopbackAddress(), 9302), - nodeAttr, Collections.emptySet(), Version.CURRENT)) - .add(new DiscoveryNode("_node_name4", "_node_id4", new TransportAddress(InetAddress.getLoopbackAddress(), 9303), - nodeAttr, Collections.emptySet(), Version.CURRENT)) + .add( + new DiscoveryNode( + "_node_name1", + "_node_id1", + new TransportAddress(InetAddress.getLoopbackAddress(), 9300), + nodeAttr, + Collections.emptySet(), + Version.CURRENT + ) + ) + .add( + new DiscoveryNode( + "_node_name2", + "_node_id2", + new TransportAddress(InetAddress.getLoopbackAddress(), 9301), + nodeAttr, + Collections.emptySet(), + Version.CURRENT + ) + ) + .add( + new DiscoveryNode( + "_node_name3", + "_node_id3", + new TransportAddress(InetAddress.getLoopbackAddress(), 9302), + nodeAttr, + Collections.emptySet(), + Version.CURRENT + ) + ) + .add( + new DiscoveryNode( + "_node_name4", + "_node_id4", + new TransportAddress(InetAddress.getLoopbackAddress(), 9303), + nodeAttr, + Collections.emptySet(), + Version.CURRENT + ) + ) .build(); PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); @@ -79,32 +111,31 @@ public void testNodeLoadDetection() { OpenJobPersistentTasksExecutorTests.addJobTask("job_id4", "_node_id4", JobState.OPENED, tasksBuilder); PersistentTasksCustomMetadata tasks = tasksBuilder.build(); - final ClusterState cs = ClusterState.builder(new ClusterName("_name")).nodes(nodes) - .metadata( - Metadata.builder() - .putCustom(PersistentTasksCustomMetadata.TYPE, tasks) - .putCustom( - TrainedModelAllocationMetadata.NAME, - TrainedModelAllocationMetadata.Builder.empty() - .addNewAllocation( - "model1", - TrainedModelAllocation.Builder - .empty(new StartTrainedModelDeploymentAction.TaskParams( - "model1", MODEL_MEMORY_REQUIREMENT, 1, 1, 1024)) - .addNewRoutingEntry("_node_id4") - .addNewFailedRoutingEntry("_node_id2", "test") - .addNewRoutingEntry("_node_id1") - .updateExistingRoutingEntry( - "_node_id1", - new RoutingStateAndReason( - randomFrom(RoutingState.STOPPED, RoutingState.FAILED), - "test" - ) - ) + final ClusterState cs = ClusterState.builder(new ClusterName("_name")) + .nodes(nodes) + .metadata( + Metadata.builder() + .putCustom(PersistentTasksCustomMetadata.TYPE, tasks) + .putCustom( + TrainedModelAllocationMetadata.NAME, + TrainedModelAllocationMetadata.Builder.empty() + .addNewAllocation( + "model1", + TrainedModelAllocation.Builder.empty( + new StartTrainedModelDeploymentAction.TaskParams("model1", MODEL_MEMORY_REQUIREMENT, 1, 1, 1024) ) - .build() - ) - ).build(); + .addNewRoutingEntry("_node_id4") + .addNewFailedRoutingEntry("_node_id2", "test") + .addNewRoutingEntry("_node_id1") + .updateExistingRoutingEntry( + "_node_id1", + new RoutingStateAndReason(randomFrom(RoutingState.STOPPED, RoutingState.FAILED), "test") + ) + ) + .build() + ) + ) + .build(); NodeLoad load = nodeLoadDetector.detectNodeLoad(cs, nodes.get("_node_id1"), 10, 30, false); assertThat(load.getAssignedJobMemory(), equalTo(52428800L)); @@ -139,8 +170,16 @@ public void testNodeLoadDetection_withBadMachineMemoryAttribute() { Map nodeAttr = new HashMap<>(); nodeAttr.put(MachineLearning.MACHINE_MEMORY_NODE_ATTR, "bar"); DiscoveryNodes nodes = DiscoveryNodes.builder() - .add(new DiscoveryNode("_node_name1", "_node_id1", new TransportAddress(InetAddress.getLoopbackAddress(), 9300), - nodeAttr, Collections.emptySet(), Version.CURRENT)) + .add( + new DiscoveryNode( + "_node_name1", + "_node_id1", + new TransportAddress(InetAddress.getLoopbackAddress(), 9300), + nodeAttr, + Collections.emptySet(), + Version.CURRENT + ) + ) .build(); ClusterState.Builder cs = ClusterState.builder(new ClusterName("_name")); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/categorization/CategorizationAnalyzerTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/categorization/CategorizationAnalyzerTests.java index a9d1b82eededc..16b7f757a236b 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/categorization/CategorizationAnalyzerTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/categorization/CategorizationAnalyzerTests.java @@ -13,8 +13,8 @@ import org.elasticsearch.index.analysis.AnalysisRegistry; import org.elasticsearch.indices.analysis.AnalysisModule; import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.xpack.ml.MachineLearning; import org.elasticsearch.xpack.core.ml.job.config.CategorizationAnalyzerConfig; +import org.elasticsearch.xpack.ml.MachineLearning; import org.junit.Before; import java.io.IOException; @@ -26,27 +26,27 @@ public class CategorizationAnalyzerTests extends ESTestCase { private static final String NGINX_ERROR_EXAMPLE = - "a client request body is buffered to a temporary file /tmp/client-body/0000021894, client: 10.8.0.12, " + - "server: apm.35.205.226.121.ip.es.io, request: \"POST /intake/v2/events HTTP/1.1\", host: \"apm.35.205.226.121.ip.es.io\"\n" + - "10.8.0.12 - - [29/Nov/2020:21:34:55 +0000] \"POST /intake/v2/events HTTP/1.1\" 202 0 \"-\" " + - "\"elasticapm-dotnet/1.5.1 System.Net.Http/4.6.28208.02 .NET_Core/2.2.8\" 27821 0.002 [default-apm-apm-server-8200] [] " + - "10.8.1.19:8200 0 0.001 202 f961c776ff732f5c8337530aa22c7216\n" + - "10.8.0.14 - - [29/Nov/2020:21:34:56 +0000] \"POST /intake/v2/events HTTP/1.1\" 202 0 \"-\" " + - "\"elasticapm-python/5.10.0\" 3594 0.002 [default-apm-apm-server-8200] [] 10.8.1.18:8200 0 0.001 202 " + - "61feb8fb9232b1ebe54b588b95771ce4\n" + - "10.8.4.90 - - [29/Nov/2020:21:34:56 +0000] \"OPTIONS /intake/v2/rum/events HTTP/2.0\" 200 0 " + - "\"http://opbeans-frontend:3000/dashboard\" \"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) " + - "Cypress/3.3.1 Chrome/61.0.3163.100 Electron/2.0.18 Safari/537.36\" 292 0.001 [default-apm-apm-server-8200] [] " + - "10.8.1.19:8200 0 0.000 200 5fbe8cd4d217b932def1c17ed381c66b\n" + - "10.8.4.90 - - [29/Nov/2020:21:34:56 +0000] \"POST /intake/v2/rum/events HTTP/2.0\" 202 0 " + - "\"http://opbeans-frontend:3000/dashboard\" \"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) " + - "Cypress/3.3.1 Chrome/61.0.3163.100 Electron/2.0.18 Safari/537.36\" 3004 0.001 [default-apm-apm-server-8200] [] " + - "10.8.1.18:8200 0 0.001 202 4735f571928595744ac6a9545c3ecdf5\n" + - "10.8.0.11 - - [29/Nov/2020:21:34:56 +0000] \"POST /intake/v2/events HTTP/1.1\" 202 0 \"-\" " + - "\"elasticapm-node/3.8.0 elastic-apm-http-client/9.4.2 node/12.20.0\" 4913 10.006 [default-apm-apm-server-8200] [] " + - "10.8.1.18:8200 0 0.002 202 1eac41789ea9a60a8be4e476c54cbbc9\n" + - "10.8.0.14 - - [29/Nov/2020:21:34:57 +0000] \"POST /intake/v2/events HTTP/1.1\" 202 0 \"-\" \"elasticapm-python/5.10.0\" " + - "1025 0.001 [default-apm-apm-server-8200] [] 10.8.1.18:8200 0 0.001 202 d27088936cadd3b8804b68998a5f94fa"; + "a client request body is buffered to a temporary file /tmp/client-body/0000021894, client: 10.8.0.12, " + + "server: apm.35.205.226.121.ip.es.io, request: \"POST /intake/v2/events HTTP/1.1\", host: \"apm.35.205.226.121.ip.es.io\"\n" + + "10.8.0.12 - - [29/Nov/2020:21:34:55 +0000] \"POST /intake/v2/events HTTP/1.1\" 202 0 \"-\" " + + "\"elasticapm-dotnet/1.5.1 System.Net.Http/4.6.28208.02 .NET_Core/2.2.8\" 27821 0.002 [default-apm-apm-server-8200] [] " + + "10.8.1.19:8200 0 0.001 202 f961c776ff732f5c8337530aa22c7216\n" + + "10.8.0.14 - - [29/Nov/2020:21:34:56 +0000] \"POST /intake/v2/events HTTP/1.1\" 202 0 \"-\" " + + "\"elasticapm-python/5.10.0\" 3594 0.002 [default-apm-apm-server-8200] [] 10.8.1.18:8200 0 0.001 202 " + + "61feb8fb9232b1ebe54b588b95771ce4\n" + + "10.8.4.90 - - [29/Nov/2020:21:34:56 +0000] \"OPTIONS /intake/v2/rum/events HTTP/2.0\" 200 0 " + + "\"http://opbeans-frontend:3000/dashboard\" \"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) " + + "Cypress/3.3.1 Chrome/61.0.3163.100 Electron/2.0.18 Safari/537.36\" 292 0.001 [default-apm-apm-server-8200] [] " + + "10.8.1.19:8200 0 0.000 200 5fbe8cd4d217b932def1c17ed381c66b\n" + + "10.8.4.90 - - [29/Nov/2020:21:34:56 +0000] \"POST /intake/v2/rum/events HTTP/2.0\" 202 0 " + + "\"http://opbeans-frontend:3000/dashboard\" \"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) " + + "Cypress/3.3.1 Chrome/61.0.3163.100 Electron/2.0.18 Safari/537.36\" 3004 0.001 [default-apm-apm-server-8200] [] " + + "10.8.1.18:8200 0 0.001 202 4735f571928595744ac6a9545c3ecdf5\n" + + "10.8.0.11 - - [29/Nov/2020:21:34:56 +0000] \"POST /intake/v2/events HTTP/1.1\" 202 0 \"-\" " + + "\"elasticapm-node/3.8.0 elastic-apm-http-client/9.4.2 node/12.20.0\" 4913 10.006 [default-apm-apm-server-8200] [] " + + "10.8.1.18:8200 0 0.002 202 1eac41789ea9a60a8be4e476c54cbbc9\n" + + "10.8.0.14 - - [29/Nov/2020:21:34:57 +0000] \"POST /intake/v2/events HTTP/1.1\" 202 0 \"-\" \"elasticapm-python/5.10.0\" " + + "1025 0.001 [default-apm-apm-server-8200] [] 10.8.1.18:8200 0 0.001 202 d27088936cadd3b8804b68998a5f94fa"; private AnalysisRegistry analysisRegistry; @@ -65,8 +65,10 @@ public void setup() throws Exception { public void testVerifyConfigBuilder_GivenNoConfig() { CategorizationAnalyzerConfig.Builder builder = new CategorizationAnalyzerConfig.Builder(); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> CategorizationAnalyzer.verifyConfigBuilder(builder, analysisRegistry)); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> CategorizationAnalyzer.verifyConfigBuilder(builder, analysisRegistry) + ); assertEquals("categorization_analyzer that is not a global analyzer must specify a [tokenizer] field", e.getMessage()); } @@ -83,8 +85,10 @@ public void testVerifyConfigBuilder_GivenValidAnalyzer() throws IOException { public void testVerifyConfigBuilder_GivenInvalidAnalyzer() { CategorizationAnalyzerConfig.Builder builder = new CategorizationAnalyzerConfig.Builder().setAnalyzer("does not exist"); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> CategorizationAnalyzer.verifyConfigBuilder(builder, analysisRegistry)); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> CategorizationAnalyzer.verifyConfigBuilder(builder, analysisRegistry) + ); assertEquals("Failed to find global analyzer [does not exist]", e.getMessage()); } @@ -95,8 +99,7 @@ public void testVerifyConfigBuilder_GivenValidCustomConfig() throws IOException Map ignoreStuffThatBeginsWithADigit = new HashMap<>(); ignoreStuffThatBeginsWithADigit.put("type", "pattern_replace"); ignoreStuffThatBeginsWithADigit.put("pattern", "^[0-9].*"); - CategorizationAnalyzerConfig.Builder builder = new CategorizationAnalyzerConfig.Builder() - .addCharFilter(ignoreStuffInSqaureBrackets) + CategorizationAnalyzerConfig.Builder builder = new CategorizationAnalyzerConfig.Builder().addCharFilter(ignoreStuffInSqaureBrackets) .setTokenizer("classic") .addTokenFilter("lowercase") .addTokenFilter(ignoreStuffThatBeginsWithADigit) @@ -105,13 +108,14 @@ public void testVerifyConfigBuilder_GivenValidCustomConfig() throws IOException } public void testVerifyConfigBuilder_GivenCustomConfigWithInvalidCharFilter() { - CategorizationAnalyzerConfig.Builder builder = new CategorizationAnalyzerConfig.Builder() - .addCharFilter("wrong!") + CategorizationAnalyzerConfig.Builder builder = new CategorizationAnalyzerConfig.Builder().addCharFilter("wrong!") .setTokenizer("classic") .addTokenFilter("lowercase") .addTokenFilter("snowball"); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> CategorizationAnalyzer.verifyConfigBuilder(builder, analysisRegistry)); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> CategorizationAnalyzer.verifyConfigBuilder(builder, analysisRegistry) + ); assertEquals("failed to find global char_filter under [wrong!]", e.getMessage()); } @@ -119,13 +123,14 @@ public void testVerifyConfigBuilder_GivenCustomConfigWithMisconfiguredCharFilter Map noPattern = new HashMap<>(); noPattern.put("type", "pattern_replace"); noPattern.put("attern", "should have been pattern"); - CategorizationAnalyzerConfig.Builder builder = new CategorizationAnalyzerConfig.Builder() - .addCharFilter(noPattern) + CategorizationAnalyzerConfig.Builder builder = new CategorizationAnalyzerConfig.Builder().addCharFilter(noPattern) .setTokenizer("classic") .addTokenFilter("lowercase") .addTokenFilter("snowball"); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> CategorizationAnalyzer.verifyConfigBuilder(builder, analysisRegistry)); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> CategorizationAnalyzer.verifyConfigBuilder(builder, analysisRegistry) + ); assertEquals("pattern is missing for [__anonymous__pattern_replace] char filter of type 'pattern_replace'", e.getMessage()); } @@ -133,13 +138,14 @@ public void testVerifyConfigBuilder_GivenCustomConfigWithInvalidTokenizer() { Map ignoreStuffInSqaureBrackets = new HashMap<>(); ignoreStuffInSqaureBrackets.put("type", "pattern_replace"); ignoreStuffInSqaureBrackets.put("pattern", "\\[[^\\]]*\\]"); - CategorizationAnalyzerConfig.Builder builder = new CategorizationAnalyzerConfig.Builder() - .addCharFilter(ignoreStuffInSqaureBrackets) + CategorizationAnalyzerConfig.Builder builder = new CategorizationAnalyzerConfig.Builder().addCharFilter(ignoreStuffInSqaureBrackets) .setTokenizer("oops!") .addTokenFilter("lowercase") .addTokenFilter("snowball"); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> CategorizationAnalyzer.verifyConfigBuilder(builder, analysisRegistry)); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> CategorizationAnalyzer.verifyConfigBuilder(builder, analysisRegistry) + ); assertEquals("failed to find global tokenizer under [oops!]", e.getMessage()); } @@ -150,13 +156,14 @@ public void testVerifyConfigBuilder_GivenNoTokenizer() { Map ignoreStuffThatBeginsWithADigit = new HashMap<>(); ignoreStuffThatBeginsWithADigit.put("type", "pattern_replace"); ignoreStuffThatBeginsWithADigit.put("pattern", "^[0-9].*"); - CategorizationAnalyzerConfig.Builder builder = new CategorizationAnalyzerConfig.Builder() - .addCharFilter(ignoreStuffInSqaureBrackets) + CategorizationAnalyzerConfig.Builder builder = new CategorizationAnalyzerConfig.Builder().addCharFilter(ignoreStuffInSqaureBrackets) .addTokenFilter("lowercase") .addTokenFilter(ignoreStuffThatBeginsWithADigit) .addTokenFilter("snowball"); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> CategorizationAnalyzer.verifyConfigBuilder(builder, analysisRegistry)); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> CategorizationAnalyzer.verifyConfigBuilder(builder, analysisRegistry) + ); assertEquals("categorization_analyzer that is not a global analyzer must specify a [tokenizer] field", e.getMessage()); } @@ -164,13 +171,14 @@ public void testVerifyConfigBuilder_GivenCustomConfigWithInvalidTokenFilter() { Map ignoreStuffInSqaureBrackets = new HashMap<>(); ignoreStuffInSqaureBrackets.put("type", "pattern_replace"); ignoreStuffInSqaureBrackets.put("pattern", "\\[[^\\]]*\\]"); - CategorizationAnalyzerConfig.Builder builder = new CategorizationAnalyzerConfig.Builder() - .addCharFilter(ignoreStuffInSqaureBrackets) + CategorizationAnalyzerConfig.Builder builder = new CategorizationAnalyzerConfig.Builder().addCharFilter(ignoreStuffInSqaureBrackets) .setTokenizer("classic") .addTokenFilter("lowercase") .addTokenFilter("oh dear!"); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> CategorizationAnalyzer.verifyConfigBuilder(builder, analysisRegistry)); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> CategorizationAnalyzer.verifyConfigBuilder(builder, analysisRegistry) + ); assertEquals("failed to find global filter under [oh dear!]", e.getMessage()); } @@ -178,40 +186,44 @@ public void testVerifyConfigBuilder_GivenCustomConfigWithMisconfiguredTokenFilte Map noPattern = new HashMap<>(); noPattern.put("type", "pattern_replace"); noPattern.put("attern", "should have been pattern"); - CategorizationAnalyzerConfig.Builder builder = new CategorizationAnalyzerConfig.Builder() - .addCharFilter("html_strip") + CategorizationAnalyzerConfig.Builder builder = new CategorizationAnalyzerConfig.Builder().addCharFilter("html_strip") .setTokenizer("classic") .addTokenFilter("lowercase") .addTokenFilter(noPattern); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> CategorizationAnalyzer.verifyConfigBuilder(builder, analysisRegistry)); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> CategorizationAnalyzer.verifyConfigBuilder(builder, analysisRegistry) + ); assertEquals("pattern is missing for [__anonymous__pattern_replace] token filter of type 'pattern_replace'", e.getMessage()); } public void testVerifyConfigBuilder_GivenAnalyzerAndCharFilter() { - CategorizationAnalyzerConfig.Builder builder = new CategorizationAnalyzerConfig.Builder() - .setAnalyzer("standard") + CategorizationAnalyzerConfig.Builder builder = new CategorizationAnalyzerConfig.Builder().setAnalyzer("standard") .addCharFilter("html_strip"); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> CategorizationAnalyzer.verifyConfigBuilder(builder, analysisRegistry)); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> CategorizationAnalyzer.verifyConfigBuilder(builder, analysisRegistry) + ); assertEquals("categorization_analyzer that is a global analyzer cannot also specify a [char_filter] field", e.getMessage()); } public void testVerifyConfigBuilder_GivenAnalyzerAndTokenizer() { - CategorizationAnalyzerConfig.Builder builder = new CategorizationAnalyzerConfig.Builder() - .setAnalyzer("standard") + CategorizationAnalyzerConfig.Builder builder = new CategorizationAnalyzerConfig.Builder().setAnalyzer("standard") .setTokenizer("classic"); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> CategorizationAnalyzer.verifyConfigBuilder(builder, analysisRegistry)); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> CategorizationAnalyzer.verifyConfigBuilder(builder, analysisRegistry) + ); assertEquals("categorization_analyzer that is a global analyzer cannot also specify a [tokenizer] field", e.getMessage()); } public void testVerifyConfigBuilder_GivenAnalyzerAndTokenFilter() { - CategorizationAnalyzerConfig.Builder builder = new CategorizationAnalyzerConfig.Builder() - .setAnalyzer("standard") + CategorizationAnalyzerConfig.Builder builder = new CategorizationAnalyzerConfig.Builder().setAnalyzer("standard") .addTokenFilter("lowercase"); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> CategorizationAnalyzer.verifyConfigBuilder(builder, analysisRegistry)); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> CategorizationAnalyzer.verifyConfigBuilder(builder, analysisRegistry) + ); assertEquals("categorization_analyzer that is a global analyzer cannot also specify a [filter] field", e.getMessage()); } @@ -220,70 +232,200 @@ public void testDefaultCategorizationAnalyzer() throws IOException { CategorizationAnalyzerConfig defaultConfig = CategorizationAnalyzerConfig.buildDefaultCategorizationAnalyzer(null); try (CategorizationAnalyzer categorizationAnalyzer = new CategorizationAnalyzer(analysisRegistry, defaultConfig)) { - assertEquals(Arrays.asList("ml13-4608.1.p2ps", "Info", "Source", "ML_SERVICE2", "on", "has", "shut", "down"), - categorizationAnalyzer.tokenizeField("p2ps", - " Source ML_SERVICE2 on 13122:867 has shut down.")); - - assertEquals(Arrays.asList("Vpxa", "verbose", "VpxaHalCnxHostagent", "opID", "WFU-ddeadb59", "WaitForUpdatesDone", "Received", - "callback"), - categorizationAnalyzer.tokenizeField("vmware", - "Vpxa: [49EC0B90 verbose 'VpxaHalCnxHostagent' opID=WFU-ddeadb59] [WaitForUpdatesDone] Received callback")); - - assertEquals(Arrays.asList("org.apache.coyote.http11.Http11BaseProtocol", "destroy"), - categorizationAnalyzer.tokenizeField("apache", - "org.apache.coyote.http11.Http11BaseProtocol destroy")); - - assertEquals(Arrays.asList("INFO", "session", "PROXY", "Session", "DESTROYED"), - categorizationAnalyzer.tokenizeField("proxy", - " [1111529792] INFO session <45409105041220090733@192.168.251.123> - " + - "----------------- PROXY Session DESTROYED --------------------")); - - assertEquals(Arrays.asList("PSYoungGen", "total", "used"), - categorizationAnalyzer.tokenizeField("java", - "PSYoungGen total 2572800K, used 1759355K [0x0000000759500000, 0x0000000800000000, 0x0000000800000000)")); - - assertEquals(Arrays.asList("client", "request", "body", "is", "buffered", "to", "temporary", "file", "tmp", "client-body", - "client", "server", "apm.35.205.226.121.ip.es.io", "request", "POST", "intake", "v2", "events", "HTTP", "host", - "apm.35.205.226.121.ip.es.io", "POST", "intake", "v2", "events", "HTTP", "elasticapm-dotnet", "System.Net.Http", "NET_Core", - "default-apm-apm-server-8200", "POST", "intake", "v2", "events", "HTTP", "elasticapm-python", "default-apm-apm-server-8200", - "OPTIONS", "intake", "v2", "rum", "events", "HTTP", "http", "opbeans-frontend", "dashboard", "Mozilla", "X11", "Linux", - "x86_64", "AppleWebKit", "KHTML", "like", "Gecko", "Cypress", "Chrome", "Electron", "Safari", "default-apm-apm-server-8200", - "POST", "intake", "v2", "rum", "events", "HTTP", "http", "opbeans-frontend", "dashboard", "Mozilla", "X11", "Linux", - "x86_64", "AppleWebKit", "KHTML", "like", "Gecko", "Cypress", "Chrome", "Electron", "Safari", "default-apm-apm-server-8200", - "POST", "intake", "v2", "events", "HTTP", "elasticapm-node", "elastic-apm-http-client", "node", - "default-apm-apm-server-8200", "POST", "intake", "v2", "events", "HTTP", "elasticapm-python", - "default-apm-apm-server-8200"), - categorizationAnalyzer.tokenizeField("nginx_error", NGINX_ERROR_EXAMPLE)); + assertEquals( + Arrays.asList("ml13-4608.1.p2ps", "Info", "Source", "ML_SERVICE2", "on", "has", "shut", "down"), + categorizationAnalyzer.tokenizeField("p2ps", " Source ML_SERVICE2 on 13122:867 has shut down.") + ); + + assertEquals( + Arrays.asList( + "Vpxa", + "verbose", + "VpxaHalCnxHostagent", + "opID", + "WFU-ddeadb59", + "WaitForUpdatesDone", + "Received", + "callback" + ), + categorizationAnalyzer.tokenizeField( + "vmware", + "Vpxa: [49EC0B90 verbose 'VpxaHalCnxHostagent' opID=WFU-ddeadb59] [WaitForUpdatesDone] Received callback" + ) + ); + + assertEquals( + Arrays.asList("org.apache.coyote.http11.Http11BaseProtocol", "destroy"), + categorizationAnalyzer.tokenizeField("apache", "org.apache.coyote.http11.Http11BaseProtocol destroy") + ); + + assertEquals( + Arrays.asList("INFO", "session", "PROXY", "Session", "DESTROYED"), + categorizationAnalyzer.tokenizeField( + "proxy", + " [1111529792] INFO session <45409105041220090733@192.168.251.123> - " + + "----------------- PROXY Session DESTROYED --------------------" + ) + ); + + assertEquals( + Arrays.asList("PSYoungGen", "total", "used"), + categorizationAnalyzer.tokenizeField( + "java", + "PSYoungGen total 2572800K, used 1759355K [0x0000000759500000, 0x0000000800000000, 0x0000000800000000)" + ) + ); + + assertEquals( + Arrays.asList( + "client", + "request", + "body", + "is", + "buffered", + "to", + "temporary", + "file", + "tmp", + "client-body", + "client", + "server", + "apm.35.205.226.121.ip.es.io", + "request", + "POST", + "intake", + "v2", + "events", + "HTTP", + "host", + "apm.35.205.226.121.ip.es.io", + "POST", + "intake", + "v2", + "events", + "HTTP", + "elasticapm-dotnet", + "System.Net.Http", + "NET_Core", + "default-apm-apm-server-8200", + "POST", + "intake", + "v2", + "events", + "HTTP", + "elasticapm-python", + "default-apm-apm-server-8200", + "OPTIONS", + "intake", + "v2", + "rum", + "events", + "HTTP", + "http", + "opbeans-frontend", + "dashboard", + "Mozilla", + "X11", + "Linux", + "x86_64", + "AppleWebKit", + "KHTML", + "like", + "Gecko", + "Cypress", + "Chrome", + "Electron", + "Safari", + "default-apm-apm-server-8200", + "POST", + "intake", + "v2", + "rum", + "events", + "HTTP", + "http", + "opbeans-frontend", + "dashboard", + "Mozilla", + "X11", + "Linux", + "x86_64", + "AppleWebKit", + "KHTML", + "like", + "Gecko", + "Cypress", + "Chrome", + "Electron", + "Safari", + "default-apm-apm-server-8200", + "POST", + "intake", + "v2", + "events", + "HTTP", + "elasticapm-node", + "elastic-apm-http-client", + "node", + "default-apm-apm-server-8200", + "POST", + "intake", + "v2", + "events", + "HTTP", + "elasticapm-python", + "default-apm-apm-server-8200" + ), + categorizationAnalyzer.tokenizeField("nginx_error", NGINX_ERROR_EXAMPLE) + ); } } public void testDefaultCategorizationAnalyzerWithCategorizationFilter() throws IOException { // A categorization filter that removes stuff in square brackets - CategorizationAnalyzerConfig defaultConfigWithCategorizationFilter = - CategorizationAnalyzerConfig.buildDefaultCategorizationAnalyzer(Collections.singletonList("\\[[^\\]]*\\]")); - try (CategorizationAnalyzer categorizationAnalyzer = new CategorizationAnalyzer(analysisRegistry, - defaultConfigWithCategorizationFilter)) { - - assertEquals(Arrays.asList("ml13-4608.1.p2ps", "Info", "Source", "ML_SERVICE2", "on", "has", "shut", "down"), - categorizationAnalyzer.tokenizeField("p2ps", - " Source ML_SERVICE2 on 13122:867 has shut down.")); - - assertEquals(Arrays.asList("Vpxa", "Received", "callback"), - categorizationAnalyzer.tokenizeField("vmware", - "Vpxa: [49EC0B90 verbose 'VpxaHalCnxHostagent' opID=WFU-ddeadb59] [WaitForUpdatesDone] Received callback")); - - assertEquals(Arrays.asList("org.apache.coyote.http11.Http11BaseProtocol", "destroy"), - categorizationAnalyzer.tokenizeField("apache", - "org.apache.coyote.http11.Http11BaseProtocol destroy")); - - assertEquals(Arrays.asList("INFO", "session", "PROXY", "Session", "DESTROYED"), - categorizationAnalyzer.tokenizeField("proxy", - " [1111529792] INFO session <45409105041220090733@192.168.251.123> - " + - "----------------- PROXY Session DESTROYED --------------------")); - - assertEquals(Arrays.asList("PSYoungGen", "total", "used"), - categorizationAnalyzer.tokenizeField("java", - "PSYoungGen total 2572800K, used 1759355K [0x0000000759500000, 0x0000000800000000, 0x0000000800000000)")); + CategorizationAnalyzerConfig defaultConfigWithCategorizationFilter = CategorizationAnalyzerConfig + .buildDefaultCategorizationAnalyzer(Collections.singletonList("\\[[^\\]]*\\]")); + try ( + CategorizationAnalyzer categorizationAnalyzer = new CategorizationAnalyzer( + analysisRegistry, + defaultConfigWithCategorizationFilter + ) + ) { + + assertEquals( + Arrays.asList("ml13-4608.1.p2ps", "Info", "Source", "ML_SERVICE2", "on", "has", "shut", "down"), + categorizationAnalyzer.tokenizeField("p2ps", " Source ML_SERVICE2 on 13122:867 has shut down.") + ); + + assertEquals( + Arrays.asList("Vpxa", "Received", "callback"), + categorizationAnalyzer.tokenizeField( + "vmware", + "Vpxa: [49EC0B90 verbose 'VpxaHalCnxHostagent' opID=WFU-ddeadb59] [WaitForUpdatesDone] Received callback" + ) + ); + + assertEquals( + Arrays.asList("org.apache.coyote.http11.Http11BaseProtocol", "destroy"), + categorizationAnalyzer.tokenizeField("apache", "org.apache.coyote.http11.Http11BaseProtocol destroy") + ); + + assertEquals( + Arrays.asList("INFO", "session", "PROXY", "Session", "DESTROYED"), + categorizationAnalyzer.tokenizeField( + "proxy", + " [1111529792] INFO session <45409105041220090733@192.168.251.123> - " + + "----------------- PROXY Session DESTROYED --------------------" + ) + ); + + assertEquals( + Arrays.asList("PSYoungGen", "total", "used"), + categorizationAnalyzer.tokenizeField( + "java", + "PSYoungGen total 2572800K, used 1759355K [0x0000000759500000, 0x0000000800000000, 0x0000000800000000)" + ) + ); } } @@ -291,44 +433,93 @@ public void testMlStandardCategorizationAnalyzer() throws IOException { CategorizationAnalyzerConfig standardConfig = CategorizationAnalyzerConfig.buildStandardCategorizationAnalyzer(null); try (CategorizationAnalyzer categorizationAnalyzer = new CategorizationAnalyzer(analysisRegistry, standardConfig)) { - assertEquals(Arrays.asList("ml13-4608.1.p2ps", "Info", "Source", "ML_SERVICE2", "on", "has", "shut", "down"), - categorizationAnalyzer.tokenizeField("p2ps", - " Source ML_SERVICE2 on 13122:867 has shut down.")); - - assertEquals(Arrays.asList("Vpxa", "verbose", "VpxaHalCnxHostagent", "opID", "WFU-ddeadb59", "WaitForUpdatesDone", "Received", - "callback"), - categorizationAnalyzer.tokenizeField("vmware", - "Vpxa: [49EC0B90 verbose 'VpxaHalCnxHostagent' opID=WFU-ddeadb59] [WaitForUpdatesDone] Received callback")); - - assertEquals(Arrays.asList("org.apache.coyote.http11.Http11BaseProtocol", "destroy"), - categorizationAnalyzer.tokenizeField("apache", - "org.apache.coyote.http11.Http11BaseProtocol destroy")); - - assertEquals(Arrays.asList("INFO", "session", "PROXY", "Session", "DESTROYED"), - categorizationAnalyzer.tokenizeField("proxy", - " [1111529792] INFO session <45409105041220090733@192.168.251.123> - " + - "----------------- PROXY Session DESTROYED --------------------")); - - assertEquals(Arrays.asList("PSYoungGen", "total", "used"), - categorizationAnalyzer.tokenizeField("java", - "PSYoungGen total 2572800K, used 1759355K [0x0000000759500000, 0x0000000800000000, 0x0000000800000000)")); - - assertEquals(Arrays.asList("first", "line"), - categorizationAnalyzer.tokenizeField("multiline", "first line\nsecond line\nthird line")); - - assertEquals(Arrays.asList("first", "line"), - categorizationAnalyzer.tokenizeField("windows_multiline", "first line\r\nsecond line\r\nthird line")); - - assertEquals(Arrays.asList("second", "line"), - categorizationAnalyzer.tokenizeField("multiline_first_blank", "\nsecond line\nthird line")); - - assertEquals(Arrays.asList("second", "line"), - categorizationAnalyzer.tokenizeField("windows_multiline_first_blank", "\r\nsecond line\r\nthird line")); - - assertEquals(Arrays.asList("client", "request", "body", "is", "buffered", "to", "temporary", "file", - "/tmp/client-body/0000021894", "client", "server", "apm.35.205.226.121.ip.es.io", "request", "POST", "/intake/v2/events", - "HTTP/1.1", "host", "apm.35.205.226.121.ip.es.io"), - categorizationAnalyzer.tokenizeField("nginx_error", NGINX_ERROR_EXAMPLE)); + assertEquals( + Arrays.asList("ml13-4608.1.p2ps", "Info", "Source", "ML_SERVICE2", "on", "has", "shut", "down"), + categorizationAnalyzer.tokenizeField("p2ps", " Source ML_SERVICE2 on 13122:867 has shut down.") + ); + + assertEquals( + Arrays.asList( + "Vpxa", + "verbose", + "VpxaHalCnxHostagent", + "opID", + "WFU-ddeadb59", + "WaitForUpdatesDone", + "Received", + "callback" + ), + categorizationAnalyzer.tokenizeField( + "vmware", + "Vpxa: [49EC0B90 verbose 'VpxaHalCnxHostagent' opID=WFU-ddeadb59] [WaitForUpdatesDone] Received callback" + ) + ); + + assertEquals( + Arrays.asList("org.apache.coyote.http11.Http11BaseProtocol", "destroy"), + categorizationAnalyzer.tokenizeField("apache", "org.apache.coyote.http11.Http11BaseProtocol destroy") + ); + + assertEquals( + Arrays.asList("INFO", "session", "PROXY", "Session", "DESTROYED"), + categorizationAnalyzer.tokenizeField( + "proxy", + " [1111529792] INFO session <45409105041220090733@192.168.251.123> - " + + "----------------- PROXY Session DESTROYED --------------------" + ) + ); + + assertEquals( + Arrays.asList("PSYoungGen", "total", "used"), + categorizationAnalyzer.tokenizeField( + "java", + "PSYoungGen total 2572800K, used 1759355K [0x0000000759500000, 0x0000000800000000, 0x0000000800000000)" + ) + ); + + assertEquals( + Arrays.asList("first", "line"), + categorizationAnalyzer.tokenizeField("multiline", "first line\nsecond line\nthird line") + ); + + assertEquals( + Arrays.asList("first", "line"), + categorizationAnalyzer.tokenizeField("windows_multiline", "first line\r\nsecond line\r\nthird line") + ); + + assertEquals( + Arrays.asList("second", "line"), + categorizationAnalyzer.tokenizeField("multiline_first_blank", "\nsecond line\nthird line") + ); + + assertEquals( + Arrays.asList("second", "line"), + categorizationAnalyzer.tokenizeField("windows_multiline_first_blank", "\r\nsecond line\r\nthird line") + ); + + assertEquals( + Arrays.asList( + "client", + "request", + "body", + "is", + "buffered", + "to", + "temporary", + "file", + "/tmp/client-body/0000021894", + "client", + "server", + "apm.35.205.226.121.ip.es.io", + "request", + "POST", + "/intake/v2/events", + "HTTP/1.1", + "host", + "apm.35.205.226.121.ip.es.io" + ), + categorizationAnalyzer.tokenizeField("nginx_error", NGINX_ERROR_EXAMPLE) + ); } } @@ -338,30 +529,60 @@ public void testStandardAnalyzer() throws IOException { CategorizationAnalyzerConfig config = new CategorizationAnalyzerConfig.Builder().setAnalyzer("standard").build(); try (CategorizationAnalyzer categorizationAnalyzer = new CategorizationAnalyzer(analysisRegistry, config)) { - assertEquals(Arrays.asList("ml13", "4608.1", "p2ps", "info", "source", "ml_service2", "on", "13122", "867", "has", "shut", - "down"), - categorizationAnalyzer.tokenizeField("p2ps", - " Source ML_SERVICE2 on 13122:867 has shut down.")); - - assertEquals(Arrays.asList("vpxa", "49ec0b90", "verbose", "vpxahalcnxhostagent", "opid", "wfu", "ddeadb59", - "waitforupdatesdone", "received", "callback"), - categorizationAnalyzer.tokenizeField("vmware", - "Vpxa: [49EC0B90 verbose 'VpxaHalCnxHostagent' opID=WFU-ddeadb59] [WaitForUpdatesDone] Received callback")); - - assertEquals(Arrays.asList("org.apache.coyote.http11", "http11baseprotocol", "destroy"), - categorizationAnalyzer.tokenizeField("apache", - "org.apache.coyote.http11.Http11BaseProtocol destroy")); - - assertEquals(Arrays.asList("1111529792", "info", "session", "45409105041220090733", "192.168.251.123", "proxy", "session", - "destroyed"), - categorizationAnalyzer.tokenizeField("proxy", - " [1111529792] INFO session <45409105041220090733@192.168.251.123> - " + - "----------------- PROXY Session DESTROYED --------------------")); - - assertEquals(Arrays.asList("psyounggen", "total", "2572800k", "used", "1759355k", "0x0000000759500000", "0x0000000800000000", - "0x0000000800000000"), - categorizationAnalyzer.tokenizeField("java", - "PSYoungGen total 2572800K, used 1759355K [0x0000000759500000, 0x0000000800000000, 0x0000000800000000)")); + assertEquals( + Arrays.asList("ml13", "4608.1", "p2ps", "info", "source", "ml_service2", "on", "13122", "867", "has", "shut", "down"), + categorizationAnalyzer.tokenizeField("p2ps", " Source ML_SERVICE2 on 13122:867 has shut down.") + ); + + assertEquals( + Arrays.asList( + "vpxa", + "49ec0b90", + "verbose", + "vpxahalcnxhostagent", + "opid", + "wfu", + "ddeadb59", + "waitforupdatesdone", + "received", + "callback" + ), + categorizationAnalyzer.tokenizeField( + "vmware", + "Vpxa: [49EC0B90 verbose 'VpxaHalCnxHostagent' opID=WFU-ddeadb59] [WaitForUpdatesDone] Received callback" + ) + ); + + assertEquals( + Arrays.asList("org.apache.coyote.http11", "http11baseprotocol", "destroy"), + categorizationAnalyzer.tokenizeField("apache", "org.apache.coyote.http11.Http11BaseProtocol destroy") + ); + + assertEquals( + Arrays.asList("1111529792", "info", "session", "45409105041220090733", "192.168.251.123", "proxy", "session", "destroyed"), + categorizationAnalyzer.tokenizeField( + "proxy", + " [1111529792] INFO session <45409105041220090733@192.168.251.123> - " + + "----------------- PROXY Session DESTROYED --------------------" + ) + ); + + assertEquals( + Arrays.asList( + "psyounggen", + "total", + "2572800k", + "used", + "1759355k", + "0x0000000759500000", + "0x0000000800000000", + "0x0000000800000000" + ), + categorizationAnalyzer.tokenizeField( + "java", + "PSYoungGen total 2572800K, used 1759355K [0x0000000759500000, 0x0000000800000000, 0x0000000800000000)" + ) + ); } } @@ -372,35 +593,48 @@ public void testCustomAnalyzer() throws IOException { Map ignoreStuffThatBeginsWithADigit = new HashMap<>(); ignoreStuffThatBeginsWithADigit.put("type", "pattern_replace"); ignoreStuffThatBeginsWithADigit.put("pattern", "^[0-9].*"); - CategorizationAnalyzerConfig config = new CategorizationAnalyzerConfig.Builder() - .addCharFilter(ignoreStuffInSqaureBrackets) - .setTokenizer("classic") - .addTokenFilter("lowercase") - .addTokenFilter(ignoreStuffThatBeginsWithADigit) - .addTokenFilter("snowball") - .build(); + CategorizationAnalyzerConfig config = new CategorizationAnalyzerConfig.Builder().addCharFilter(ignoreStuffInSqaureBrackets) + .setTokenizer("classic") + .addTokenFilter("lowercase") + .addTokenFilter(ignoreStuffThatBeginsWithADigit) + .addTokenFilter("snowball") + .build(); try (CategorizationAnalyzer categorizationAnalyzer = new CategorizationAnalyzer(analysisRegistry, config)) { - assertEquals(Arrays.asList("ml13-4608.1.p2ps", "info", "sourc", "ml_service2", "on", "has", "shut", "down"), - categorizationAnalyzer.tokenizeField("p2ps", - " Source ML_SERVICE2 on 13122:867 has shut down.")); - - assertEquals(Arrays.asList("vpxa", "receiv", "callback"), - categorizationAnalyzer.tokenizeField("vmware", - "Vpxa: [49EC0B90 verbose 'VpxaHalCnxHostagent' opID=WFU-ddeadb59] [WaitForUpdatesDone] Received callback")); - - assertEquals(Arrays.asList("org.apache.coyote.http11.http11baseprotocol", "destroy"), - categorizationAnalyzer.tokenizeField("apache", - "org.apache.coyote.http11.Http11BaseProtocol destroy")); - - assertEquals(Arrays.asList("info", "session", "proxi", "session", "destroy"), - categorizationAnalyzer.tokenizeField("proxy", - " [1111529792] INFO session <45409105041220090733@192.168.251.123> - " + - "----------------- PROXY Session DESTROYED --------------------")); - - assertEquals(Arrays.asList("psyounggen", "total", "use"), - categorizationAnalyzer.tokenizeField("java", - "PSYoungGen total 2572800K, used 1759355K [0x0000000759500000, 0x0000000800000000, 0x0000000800000000)")); + assertEquals( + Arrays.asList("ml13-4608.1.p2ps", "info", "sourc", "ml_service2", "on", "has", "shut", "down"), + categorizationAnalyzer.tokenizeField("p2ps", " Source ML_SERVICE2 on 13122:867 has shut down.") + ); + + assertEquals( + Arrays.asList("vpxa", "receiv", "callback"), + categorizationAnalyzer.tokenizeField( + "vmware", + "Vpxa: [49EC0B90 verbose 'VpxaHalCnxHostagent' opID=WFU-ddeadb59] [WaitForUpdatesDone] Received callback" + ) + ); + + assertEquals( + Arrays.asList("org.apache.coyote.http11.http11baseprotocol", "destroy"), + categorizationAnalyzer.tokenizeField("apache", "org.apache.coyote.http11.Http11BaseProtocol destroy") + ); + + assertEquals( + Arrays.asList("info", "session", "proxi", "session", "destroy"), + categorizationAnalyzer.tokenizeField( + "proxy", + " [1111529792] INFO session <45409105041220090733@192.168.251.123> - " + + "----------------- PROXY Session DESTROYED --------------------" + ) + ); + + assertEquals( + Arrays.asList("psyounggen", "total", "use"), + categorizationAnalyzer.tokenizeField( + "java", + "PSYoungGen total 2572800K, used 1759355K [0x0000000759500000, 0x0000000800000000, 0x0000000800000000)" + ) + ); } } @@ -416,9 +650,7 @@ public void testThaiAnalyzer() throws IOException { try (CategorizationAnalyzer categorizationAnalyzer = new CategorizationAnalyzer(analysisRegistry, config)) { // An example from the ES docs - no idea what it means or whether it's remotely sensible from a categorization point-of-view - assertEquals(Arrays.asList("แสดง", "งาน", "ดี"), - categorizationAnalyzer.tokenizeField("thai", - "การที่ได้ต้องแสดงว่างานดี")); + assertEquals(Arrays.asList("แสดง", "งาน", "ดี"), categorizationAnalyzer.tokenizeField("thai", "การที่ได้ต้องแสดงว่างานดี")); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/categorization/FirstLineWithLettersCharFilterTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/categorization/FirstLineWithLettersCharFilterTests.java index 3832a8752ee3d..0afb3d731406b 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/categorization/FirstLineWithLettersCharFilterTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/categorization/FirstLineWithLettersCharFilterTests.java @@ -109,11 +109,11 @@ public void testNoLinesWithLetters() throws IOException { public void testCorrect() throws IOException { - String input = " --------------------------------------------------------------------------------\n" + - "\n" + - "Alias 'foo' already exists and this prevents setting up ILM for logs\n" + - "\n" + - "--------------------------------------------------------------------------------"; + String input = " --------------------------------------------------------------------------------\n" + + "\n" + + "Alias 'foo' already exists and this prevents setting up ILM for logs\n" + + "\n" + + "--------------------------------------------------------------------------------"; FirstLineWithLettersCharFilter filter = new FirstLineWithLettersCharFilter(new StringReader(input)); String expectedOutput = "Alias 'foo' already exists and this prevents setting up ILM for logs"; diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/categorization/GrokPatternCreatorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/categorization/GrokPatternCreatorTests.java index 44d486683a454..21bb0948de1b3 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/categorization/GrokPatternCreatorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/categorization/GrokPatternCreatorTests.java @@ -35,10 +35,12 @@ public void testBuildFieldName() { public void testPopulatePrefacesAndEpiloguesGivenTimestamp() { - Collection matchingStrings = Arrays.asList("[2018-01-25T15:33:23] DEBUG ", - "[2018-01-24T12:33:23] ERROR ", - "junk [2018-01-22T07:33:23] INFO ", - "[2018-01-21T03:33:23] DEBUG "); + Collection matchingStrings = Arrays.asList( + "[2018-01-25T15:33:23] DEBUG ", + "[2018-01-24T12:33:23] ERROR ", + "junk [2018-01-22T07:33:23] INFO ", + "[2018-01-21T03:33:23] DEBUG " + ); Grok grok = new GrokPatternCreator.GrokPatternCandidate("TIMESTAMP_ISO8601", "timestamp").grok; Collection prefaces = new ArrayList<>(); Collection epilogues = new ArrayList<>(); @@ -51,9 +53,7 @@ public void testPopulatePrefacesAndEpiloguesGivenTimestamp() { public void testPopulatePrefacesAndEpiloguesGivenEmailAddress() { - Collection matchingStrings = Arrays.asList("before alice@acme.com after", - "abc bob@acme.com xyz", - "carol@acme.com"); + Collection matchingStrings = Arrays.asList("before alice@acme.com after", "abc bob@acme.com xyz", "carol@acme.com"); Grok grok = new GrokPatternCreator.GrokPatternCandidate("EMAILADDRESS", "email").grok; Collection prefaces = new ArrayList<>(); Collection epilogues = new ArrayList<>(); @@ -66,16 +66,24 @@ public void testPopulatePrefacesAndEpiloguesGivenEmailAddress() { public void testAppendBestGrokMatchForStringsGivenTimestampsAndLogLevels() { - Collection mustMatchStrings = Arrays.asList("[2018-01-25T15:33:23] DEBUG ", - "[2018-01-24T12:33:23] ERROR ", - "junk [2018-01-22T07:33:23] INFO ", - "[2018-01-21T03:33:23] DEBUG "); + Collection mustMatchStrings = Arrays.asList( + "[2018-01-25T15:33:23] DEBUG ", + "[2018-01-24T12:33:23] ERROR ", + "junk [2018-01-22T07:33:23] INFO ", + "[2018-01-21T03:33:23] DEBUG " + ); Map fieldNameCountStore = new HashMap<>(); StringBuilder overallGrokPatternBuilder = new StringBuilder(); - GrokPatternCreator.appendBestGrokMatchForStrings("foo", fieldNameCountStore, overallGrokPatternBuilder, false, - false, mustMatchStrings); + GrokPatternCreator.appendBestGrokMatchForStrings( + "foo", + fieldNameCountStore, + overallGrokPatternBuilder, + false, + false, + mustMatchStrings + ); assertEquals(".+?%{TIMESTAMP_ISO8601:timestamp}.+?%{LOGLEVEL:loglevel}.+?", overallGrokPatternBuilder.toString()); } @@ -84,15 +92,23 @@ public void testAppendBestGrokMatchForStringsGivenTomcatDatestamps() { // The first part of the Tomcat datestamp can match as an ISO8601 // timestamp if the ordering of candidate patterns is wrong - Collection mustMatchStrings = Arrays.asList("2018-09-03 17:03:28,269 +0100 | ERROR | ", - "2018-09-03 17:04:27,279 +0100 | DEBUG | ", - "2018-09-03 17:05:26,289 +0100 | ERROR | "); + Collection mustMatchStrings = Arrays.asList( + "2018-09-03 17:03:28,269 +0100 | ERROR | ", + "2018-09-03 17:04:27,279 +0100 | DEBUG | ", + "2018-09-03 17:05:26,289 +0100 | ERROR | " + ); Map fieldNameCountStore = new HashMap<>(); StringBuilder overallGrokPatternBuilder = new StringBuilder(); - GrokPatternCreator.appendBestGrokMatchForStrings("foo", fieldNameCountStore, overallGrokPatternBuilder, false, - false, mustMatchStrings); + GrokPatternCreator.appendBestGrokMatchForStrings( + "foo", + fieldNameCountStore, + overallGrokPatternBuilder, + false, + false, + mustMatchStrings + ); assertEquals(".*?%{TOMCAT_DATESTAMP:timestamp}.+?%{LOGLEVEL:loglevel}.+?", overallGrokPatternBuilder.toString()); } @@ -102,47 +118,57 @@ public void testAppendBestGrokMatchForStringsGivenTrappyFloatCandidates() { // If we're not careful then we might detect the first part of these strings as a // number, e.g. 1.2 in the first example, but this is inappropriate given the // trailing dot and digit - Collection mustMatchStrings = Arrays.asList("1.2.3", - "-2.3.4", - "4.5.6.7", - "-9.8.7.6.5"); + Collection mustMatchStrings = Arrays.asList("1.2.3", "-2.3.4", "4.5.6.7", "-9.8.7.6.5"); Map fieldNameCountStore = new HashMap<>(); StringBuilder overallGrokPatternBuilder = new StringBuilder(); - GrokPatternCreator.appendBestGrokMatchForStrings("foo", fieldNameCountStore, overallGrokPatternBuilder, false, - false, mustMatchStrings); + GrokPatternCreator.appendBestGrokMatchForStrings( + "foo", + fieldNameCountStore, + overallGrokPatternBuilder, + false, + false, + mustMatchStrings + ); assertEquals(".+?", overallGrokPatternBuilder.toString()); } public void testAppendBestGrokMatchForStringsGivenNumbersInBrackets() { - Collection mustMatchStrings = Arrays.asList("(-2)", - " (-3)", - " (4)", - " (-5) "); + Collection mustMatchStrings = Arrays.asList("(-2)", " (-3)", " (4)", " (-5) "); Map fieldNameCountStore = new HashMap<>(); StringBuilder overallGrokPatternBuilder = new StringBuilder(); - GrokPatternCreator.appendBestGrokMatchForStrings("foo", fieldNameCountStore, overallGrokPatternBuilder, false, - false, mustMatchStrings); + GrokPatternCreator.appendBestGrokMatchForStrings( + "foo", + fieldNameCountStore, + overallGrokPatternBuilder, + false, + false, + mustMatchStrings + ); assertEquals(".+?%{NUMBER:field}.+?", overallGrokPatternBuilder.toString()); } public void testAppendBestGrokMatchForStringsGivenNegativeNumbersWithoutBreak() { - Collection mustMatchStrings = Arrays.asList("before-2 ", - "prior to-3", - "-4"); + Collection mustMatchStrings = Arrays.asList("before-2 ", "prior to-3", "-4"); Map fieldNameCountStore = new HashMap<>(); StringBuilder overallGrokPatternBuilder = new StringBuilder(); - GrokPatternCreator.appendBestGrokMatchForStrings("foo", fieldNameCountStore, overallGrokPatternBuilder, false, - false, mustMatchStrings); + GrokPatternCreator.appendBestGrokMatchForStrings( + "foo", + fieldNameCountStore, + overallGrokPatternBuilder, + false, + false, + mustMatchStrings + ); // It seems sensible that we don't detect these suffices as either base 10 or base 16 numbers assertEquals(".+?", overallGrokPatternBuilder.toString()); @@ -150,30 +176,38 @@ public void testAppendBestGrokMatchForStringsGivenNegativeNumbersWithoutBreak() public void testAppendBestGrokMatchForStringsGivenHexNumbers() { - Collection mustMatchStrings = Arrays.asList(" abc", - " 123", - " -123", - "1f is hex"); + Collection mustMatchStrings = Arrays.asList(" abc", " 123", " -123", "1f is hex"); Map fieldNameCountStore = new HashMap<>(); StringBuilder overallGrokPatternBuilder = new StringBuilder(); - GrokPatternCreator.appendBestGrokMatchForStrings("foo", fieldNameCountStore, overallGrokPatternBuilder, false, - false, mustMatchStrings); + GrokPatternCreator.appendBestGrokMatchForStrings( + "foo", + fieldNameCountStore, + overallGrokPatternBuilder, + false, + false, + mustMatchStrings + ); assertEquals(".*?%{BASE16NUM:field}.*?", overallGrokPatternBuilder.toString()); } public void testAppendBestGrokMatchForStringsGivenHostnamesWithNumbers() { - Collection mustMatchStrings = Arrays.asList(" mustMatchStrings = Arrays.asList(" fieldNameCountStore = new HashMap<>(); StringBuilder overallGrokPatternBuilder = new StringBuilder(); - GrokPatternCreator.appendBestGrokMatchForStrings("foo", fieldNameCountStore, overallGrokPatternBuilder, false, - false, mustMatchStrings); + GrokPatternCreator.appendBestGrokMatchForStrings( + "foo", + fieldNameCountStore, + overallGrokPatternBuilder, + false, + false, + mustMatchStrings + ); // We don't want the .1. in the middle to get detected as a hex number assertEquals(".+?", overallGrokPatternBuilder.toString()); @@ -181,45 +215,61 @@ public void testAppendBestGrokMatchForStringsGivenHostnamesWithNumbers() { public void testAppendBestGrokMatchForStringsGivenEmailAddresses() { - Collection mustMatchStrings = Arrays.asList("before alice@acme.com after", - "abc bob@acme.com xyz", - "carol@acme.com"); + Collection mustMatchStrings = Arrays.asList("before alice@acme.com after", "abc bob@acme.com xyz", "carol@acme.com"); Map fieldNameCountStore = new HashMap<>(); StringBuilder overallGrokPatternBuilder = new StringBuilder(); - GrokPatternCreator.appendBestGrokMatchForStrings("foo", fieldNameCountStore, overallGrokPatternBuilder, false, - false, mustMatchStrings); + GrokPatternCreator.appendBestGrokMatchForStrings( + "foo", + fieldNameCountStore, + overallGrokPatternBuilder, + false, + false, + mustMatchStrings + ); assertEquals(".*?%{EMAILADDRESS:email}.*?", overallGrokPatternBuilder.toString()); } public void testAppendBestGrokMatchForStringsGivenUris() { - Collection mustMatchStrings = Arrays.asList("main site https://www.elastic.co/ with trailing slash", - "https://www.elastic.co/guide/en/x-pack/current/ml-configuring-categories.html#ml-configuring-categories is a section", - "download today from https://www.elastic.co/downloads"); + Collection mustMatchStrings = Arrays.asList( + "main site https://www.elastic.co/ with trailing slash", + "https://www.elastic.co/guide/en/x-pack/current/ml-configuring-categories.html#ml-configuring-categories is a section", + "download today from https://www.elastic.co/downloads" + ); Map fieldNameCountStore = new HashMap<>(); StringBuilder overallGrokPatternBuilder = new StringBuilder(); - GrokPatternCreator.appendBestGrokMatchForStrings("foo", fieldNameCountStore, overallGrokPatternBuilder, false, - false, mustMatchStrings); + GrokPatternCreator.appendBestGrokMatchForStrings( + "foo", + fieldNameCountStore, + overallGrokPatternBuilder, + false, + false, + mustMatchStrings + ); assertEquals(".*?%{URI:uri}.*?", overallGrokPatternBuilder.toString()); } public void testAppendBestGrokMatchForStringsGivenPaths() { - Collection mustMatchStrings = Arrays.asList("on Mac /Users/dave", - "on Windows C:\\Users\\dave", - "on Linux /home/dave"); + Collection mustMatchStrings = Arrays.asList("on Mac /Users/dave", "on Windows C:\\Users\\dave", "on Linux /home/dave"); Map fieldNameCountStore = new HashMap<>(); StringBuilder overallGrokPatternBuilder = new StringBuilder(); - GrokPatternCreator.appendBestGrokMatchForStrings("foo", fieldNameCountStore, overallGrokPatternBuilder, false, - false, mustMatchStrings); + GrokPatternCreator.appendBestGrokMatchForStrings( + "foo", + fieldNameCountStore, + overallGrokPatternBuilder, + false, + false, + mustMatchStrings + ); assertEquals(".+?%{PATH:path}.*?", overallGrokPatternBuilder.toString()); } @@ -228,34 +278,40 @@ public void testFindBestGrokMatchFromExamplesGivenNamedLogs() { String regex = ".*?linux.+?named.+?error.+?unexpected.+?RCODE.+?REFUSED.+?resolving.*"; Collection examples = Arrays.asList( - "Sep 8 11:55:06 linux named[22529]: error (unexpected RCODE REFUSED) resolving 'elastic.slack.com/A/IN': 95.110.64.205#53", - "Sep 8 11:55:08 linux named[22529]: error (unexpected RCODE REFUSED) resolving 'slack-imgs.com/A/IN': 95.110.64.205#53", - "Sep 8 11:55:35 linux named[22529]: error (unexpected RCODE REFUSED) resolving 'www.elastic.co/A/IN': 95.110.68.206#53", - "Sep 8 11:55:42 linux named[22529]: error (unexpected RCODE REFUSED) resolving 'b.akamaiedge.net/A/IN': 95.110.64.205#53"); - - assertEquals(".*?%{SYSLOGTIMESTAMP:timestamp}.+?linux.+?named.+?%{NUMBER:field}.+?error.+?" + - "unexpected.+?RCODE.+?REFUSED.+?resolving.+?%{QUOTEDSTRING:field2}.+?%{IP:ipaddress}.+?%{NUMBER:field3}.*", - GrokPatternCreator.findBestGrokMatchFromExamples("foo", regex, examples)); + "Sep 8 11:55:06 linux named[22529]: error (unexpected RCODE REFUSED) resolving 'elastic.slack.com/A/IN': 95.110.64.205#53", + "Sep 8 11:55:08 linux named[22529]: error (unexpected RCODE REFUSED) resolving 'slack-imgs.com/A/IN': 95.110.64.205#53", + "Sep 8 11:55:35 linux named[22529]: error (unexpected RCODE REFUSED) resolving 'www.elastic.co/A/IN': 95.110.68.206#53", + "Sep 8 11:55:42 linux named[22529]: error (unexpected RCODE REFUSED) resolving 'b.akamaiedge.net/A/IN': 95.110.64.205#53" + ); + + assertEquals( + ".*?%{SYSLOGTIMESTAMP:timestamp}.+?linux.+?named.+?%{NUMBER:field}.+?error.+?" + + "unexpected.+?RCODE.+?REFUSED.+?resolving.+?%{QUOTEDSTRING:field2}.+?%{IP:ipaddress}.+?%{NUMBER:field3}.*", + GrokPatternCreator.findBestGrokMatchFromExamples("foo", regex, examples) + ); } public void testFindBestGrokMatchFromExamplesGivenCatalinaLogs() { - String regex = ".*?org\\.apache\\.tomcat\\.util\\.http\\.Parameters.+?processParameters.+?WARNING.+?Parameters.+?" + - "Invalid.+?chunk.+?ignored.*"; + String regex = ".*?org\\.apache\\.tomcat\\.util\\.http\\.Parameters.+?processParameters.+?WARNING.+?Parameters.+?" + + "Invalid.+?chunk.+?ignored.*"; // The embedded newline ensures the regular expressions we're using are compiled with Pattern.DOTALL Collection examples = Arrays.asList( - "Aug 29, 2009 12:03:33 AM org.apache.tomcat.util.http.Parameters processParameters\nWARNING: Parameters: " + - "Invalid chunk ignored.", - "Aug 29, 2009 12:03:40 AM org.apache.tomcat.util.http.Parameters processParameters\nWARNING: Parameters: " + - "Invalid chunk ignored.", - "Aug 29, 2009 12:03:45 AM org.apache.tomcat.util.http.Parameters processParameters\nWARNING: Parameters: " + - "Invalid chunk ignored.", - "Aug 29, 2009 12:03:57 AM org.apache.tomcat.util.http.Parameters processParameters\nWARNING: Parameters: " + - "Invalid chunk ignored."); - - assertEquals(".*?%{CATALINA_DATESTAMP:timestamp}.+?org\\.apache\\.tomcat\\.util\\.http\\.Parameters.+?processParameters.+?" + - "WARNING.+?Parameters.+?Invalid.+?chunk.+?ignored.*", - GrokPatternCreator.findBestGrokMatchFromExamples("foo", regex, examples)); + "Aug 29, 2009 12:03:33 AM org.apache.tomcat.util.http.Parameters processParameters\nWARNING: Parameters: " + + "Invalid chunk ignored.", + "Aug 29, 2009 12:03:40 AM org.apache.tomcat.util.http.Parameters processParameters\nWARNING: Parameters: " + + "Invalid chunk ignored.", + "Aug 29, 2009 12:03:45 AM org.apache.tomcat.util.http.Parameters processParameters\nWARNING: Parameters: " + + "Invalid chunk ignored.", + "Aug 29, 2009 12:03:57 AM org.apache.tomcat.util.http.Parameters processParameters\nWARNING: Parameters: " + + "Invalid chunk ignored." + ); + + assertEquals( + ".*?%{CATALINA_DATESTAMP:timestamp}.+?org\\.apache\\.tomcat\\.util\\.http\\.Parameters.+?processParameters.+?" + + "WARNING.+?Parameters.+?Invalid.+?chunk.+?ignored.*", + GrokPatternCreator.findBestGrokMatchFromExamples("foo", regex, examples) + ); } public void testFindBestGrokMatchFromExamplesGivenMultiTimestampLogs() { @@ -263,77 +319,82 @@ public void testFindBestGrokMatchFromExamplesGivenMultiTimestampLogs() { String regex = ".*?Authpriv.+?Info.+?sshd.+?subsystem.+?request.+?for.+?sftp.*"; // Two timestamps: one local, one UTC Collection examples = Arrays.asList( - "559550912540598297\t2016-04-20T14:06:53\t2016-04-20T21:06:53Z\t38545844\tserv02nw07\t192.168.114.28\tAuthpriv\t" + - "Info\tsshd\tsubsystem request for sftp", - "559550912548986880\t2016-04-20T14:06:53\t2016-04-20T21:06:53Z\t9049724\tserv02nw03\t10.120.48.147\tAuthpriv\t" + - "Info\tsshd\tsubsystem request for sftp", - "559550912548986887\t2016-04-20T14:06:53\t2016-04-20T21:06:53Z\t884343\tserv02tw03\t192.168.121.189\tAuthpriv\t" + - "Info\tsshd\tsubsystem request for sftp", - "559550912603512850\t2016-04-20T14:06:53\t2016-04-20T21:06:53Z\t8907014\tserv02nw01\t192.168.118.208\tAuthpriv\t" + - "Info\tsshd\tsubsystem request for sftp"); - - assertEquals(".*?%{NUMBER:field}.+?%{TIMESTAMP_ISO8601:timestamp}.+?%{TIMESTAMP_ISO8601:timestamp2}.+?%{NUMBER:field2}.+?" + - "%{IP:ipaddress}.+?Authpriv.+?Info.+?sshd.+?subsystem.+?request.+?for.+?sftp.*", - GrokPatternCreator.findBestGrokMatchFromExamples("foo", regex, examples)); + "559550912540598297\t2016-04-20T14:06:53\t2016-04-20T21:06:53Z\t38545844\tserv02nw07\t192.168.114.28\tAuthpriv\t" + + "Info\tsshd\tsubsystem request for sftp", + "559550912548986880\t2016-04-20T14:06:53\t2016-04-20T21:06:53Z\t9049724\tserv02nw03\t10.120.48.147\tAuthpriv\t" + + "Info\tsshd\tsubsystem request for sftp", + "559550912548986887\t2016-04-20T14:06:53\t2016-04-20T21:06:53Z\t884343\tserv02tw03\t192.168.121.189\tAuthpriv\t" + + "Info\tsshd\tsubsystem request for sftp", + "559550912603512850\t2016-04-20T14:06:53\t2016-04-20T21:06:53Z\t8907014\tserv02nw01\t192.168.118.208\tAuthpriv\t" + + "Info\tsshd\tsubsystem request for sftp" + ); + + assertEquals( + ".*?%{NUMBER:field}.+?%{TIMESTAMP_ISO8601:timestamp}.+?%{TIMESTAMP_ISO8601:timestamp2}.+?%{NUMBER:field2}.+?" + + "%{IP:ipaddress}.+?Authpriv.+?Info.+?sshd.+?subsystem.+?request.+?for.+?sftp.*", + GrokPatternCreator.findBestGrokMatchFromExamples("foo", regex, examples) + ); } public void testFindBestGrokMatchFromExamplesGivenAdversarialInputRecurseDepth() { String regex = ".*?combo.+?rpc\\.statd.+?gethostbyname.+?error.+?for.+?X.+?X.+?Z.+?Z.+?hn.+?hn.*"; // Two timestamps: one local, one UTC Collection examples = Arrays.asList( - "combo rpc.statd[1605]: gethostbyname error for ^X^X^Z^Z%8x%8x%8x%8x%8x%8x%8x%8x%8x%62716x%hn%51859x%hn" + - "\\220\\220\\220\\220\\220\\220" + - "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + - "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + - "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + - "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + - "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + - "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + - "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + - "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + - "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + - "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + - "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + - "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + - "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + - "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\22...", - "combo rpc.statd[1608]: gethostbyname error for ^X^X^Z^Z%8x%8x%8x%8x%8x%8x%8x%8x%8x%62716x%hn%51859x%hn" + - "\\220\\220\\220\\220\\220\\220" + - "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + - "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + - "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + - "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + - "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + - "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + - "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + - "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + - "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + - "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + - "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + - "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + - "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + - "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\22...", - "combo rpc.statd[1635]: gethostbyname error for ^X^X^Z^Z%8x%8x%8x%8x%8x%8x%8x%8x%8x%62716x%hn%51859x%hn" + - "\\220\\220\\220\\220\\220\\220" + - "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + - "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + - "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + - "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + - "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + - "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + - "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + - "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + - "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + - "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + - "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + - "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + - "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + - "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\22..."); + "combo rpc.statd[1605]: gethostbyname error for ^X^X^Z^Z%8x%8x%8x%8x%8x%8x%8x%8x%8x%62716x%hn%51859x%hn" + + "\\220\\220\\220\\220\\220\\220" + + "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + + "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + + "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + + "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + + "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + + "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + + "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + + "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + + "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + + "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + + "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + + "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + + "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + + "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\22...", + "combo rpc.statd[1608]: gethostbyname error for ^X^X^Z^Z%8x%8x%8x%8x%8x%8x%8x%8x%8x%62716x%hn%51859x%hn" + + "\\220\\220\\220\\220\\220\\220" + + "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + + "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + + "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + + "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + + "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + + "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + + "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + + "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + + "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + + "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + + "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + + "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + + "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + + "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\22...", + "combo rpc.statd[1635]: gethostbyname error for ^X^X^Z^Z%8x%8x%8x%8x%8x%8x%8x%8x%8x%62716x%hn%51859x%hn" + + "\\220\\220\\220\\220\\220\\220" + + "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + + "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + + "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + + "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + + "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + + "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + + "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + + "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + + "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + + "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + + "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + + "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + + "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220\\220" + + "\\220\\220\\220\\220\\220\\220\\220\\220\\220\\22..." + ); assertEquals( - ".*?combo.+?rpc\\.statd.+?%{NUMBER:field}.+?gethostbyname.+?error.+?for.+?X.+?X.+?Z.+?Z.+?hn.+?hn.+?%{NUMBER:field2}" + - ".+?%{NUMBER:field3}.+?%{NUMBER:field4}.+?%{NUMBER:field5}.+?%{NUMBER:field6}.+?%{NUMBER:field7}.+?%{NUMBER:field8}" + - ".+?%{NUMBER:field9}.+?%{NUMBER:field10}.+?%{NUMBER:field11}.*", - GrokPatternCreator.findBestGrokMatchFromExamples("foo", regex, examples)); + ".*?combo.+?rpc\\.statd.+?%{NUMBER:field}.+?gethostbyname.+?error.+?for.+?X.+?X.+?Z.+?Z.+?hn.+?hn.+?%{NUMBER:field2}" + + ".+?%{NUMBER:field3}.+?%{NUMBER:field4}.+?%{NUMBER:field5}.+?%{NUMBER:field6}.+?%{NUMBER:field7}.+?%{NUMBER:field8}" + + ".+?%{NUMBER:field9}.+?%{NUMBER:field10}.+?%{NUMBER:field11}.*", + GrokPatternCreator.findBestGrokMatchFromExamples("foo", regex, examples) + ); } public void testFindBestGrokMatchFromExamplesGivenMatchAllRegex() { @@ -343,42 +404,45 @@ public void testFindBestGrokMatchFromExamplesGivenMatchAllRegex() { "Killing job [count_tweets]", "Killing job [tweets_by_location]", "[count_tweets] Killing job", - "[tweets_by_location] Killing job"); + "[tweets_by_location] Killing job" + ); assertThat(GrokPatternCreator.findBestGrokMatchFromExamples("foo", regex, examples), equalTo(regex)); } public void testFindBestGrokMatchFromExamplesGivenTruncated() { - String regex = ".*?BST.+?dave.+?bank3.+?CONTEXT.+?SQL.+?statement.+?SELECT.+?time_series_ids_tmp\\.evidence_id" + - ".+?time_series_ids_tmp\\.time_series_id.+?is_delta.+?GREATEST.+?usual_interval.+?FROM.+?time_series_ids_tmp.+?" + - "WHERE.+?found_peak_value.+?FALSE.+?ORDER.+?BY.+?time_series_ids_tmp\\.magnitude.+?DESC.+?" + - "time_series_ids_tmp\\.scaling_factor.+?DESC.+?time_series_ids_tmp\\.significance.+?DESC.+?" + - "time_series_ids_tmp\\.evidence_id.+?DESC.+?LIMIT.+?PL.+?pgSQL.+?function.+?probable_cause_list_common.+?" + - "integer.+?integer.+?integer.+?line.+?at.+?SQL.+?statement.+?SQL.+?statement.+?SELECT.+?" + - "probable_cause_list_common.+?evidenceIdIn.+?linkGroupId.+?timeSpanSeconds.+?PL.+?pgSQL.+?function.+?" + - "probable_cause_list.+?integer.+?integer.+?line.+?at.+?PERFORM.*"; - Collection examples = Collections.singletonList("2013-05-16 12:13:45 BST:192.168.61.59(51438):dave:@bank3:[19084]: " + - "CONTEXT: SQL statement \"SELECT\n" + - " time_series_ids_tmp.evidence_id,\n" + - " time_series_ids_tmp.time_series_id,\n" + - " is_delta,\n" + - " GREATEST(usual_interval, 1)\n" + - " FROM\n" + - " time_series_ids_tmp\n" + - " WHERE\n" + - " found_peak_value = FALSE\n" + - " ORDER BY\n" + - " \n" + - " \n" + - " \n" + - " time_series_ids_tmp.magnitude DESC,\n" + - " time_series_ids_tmp.scaling_factor DESC,\n" + - " time_series_ids_tmp.significance DESC,\n" + - " time_series_ids_tmp.evidence_id DESC\n" + - " LIMIT\n" + - " 1\"\n" + - " PL/pgSQL function probable_cause_list_common(integer,integer,integer) line 255 at SQL statement\n" + - " SQL statement \"SELECT probable_cause_list_common(evidenceIdIn, linkGroupId, timeSpanSeconds)\"\n" + - " PL/pgSQL function probable_cause_list..."); + String regex = ".*?BST.+?dave.+?bank3.+?CONTEXT.+?SQL.+?statement.+?SELECT.+?time_series_ids_tmp\\.evidence_id" + + ".+?time_series_ids_tmp\\.time_series_id.+?is_delta.+?GREATEST.+?usual_interval.+?FROM.+?time_series_ids_tmp.+?" + + "WHERE.+?found_peak_value.+?FALSE.+?ORDER.+?BY.+?time_series_ids_tmp\\.magnitude.+?DESC.+?" + + "time_series_ids_tmp\\.scaling_factor.+?DESC.+?time_series_ids_tmp\\.significance.+?DESC.+?" + + "time_series_ids_tmp\\.evidence_id.+?DESC.+?LIMIT.+?PL.+?pgSQL.+?function.+?probable_cause_list_common.+?" + + "integer.+?integer.+?integer.+?line.+?at.+?SQL.+?statement.+?SQL.+?statement.+?SELECT.+?" + + "probable_cause_list_common.+?evidenceIdIn.+?linkGroupId.+?timeSpanSeconds.+?PL.+?pgSQL.+?function.+?" + + "probable_cause_list.+?integer.+?integer.+?line.+?at.+?PERFORM.*"; + Collection examples = Collections.singletonList( + "2013-05-16 12:13:45 BST:192.168.61.59(51438):dave:@bank3:[19084]: " + + "CONTEXT: SQL statement \"SELECT\n" + + " time_series_ids_tmp.evidence_id,\n" + + " time_series_ids_tmp.time_series_id,\n" + + " is_delta,\n" + + " GREATEST(usual_interval, 1)\n" + + " FROM\n" + + " time_series_ids_tmp\n" + + " WHERE\n" + + " found_peak_value = FALSE\n" + + " ORDER BY\n" + + " \n" + + " \n" + + " \n" + + " time_series_ids_tmp.magnitude DESC,\n" + + " time_series_ids_tmp.scaling_factor DESC,\n" + + " time_series_ids_tmp.significance DESC,\n" + + " time_series_ids_tmp.evidence_id DESC\n" + + " LIMIT\n" + + " 1\"\n" + + " PL/pgSQL function probable_cause_list_common(integer,integer,integer) line 255 at SQL statement\n" + + " SQL statement \"SELECT probable_cause_list_common(evidenceIdIn, linkGroupId, timeSpanSeconds)\"\n" + + " PL/pgSQL function probable_cause_list..." + ); // Our algorithm for converting examples to Grok patterns that pick out useful fields doesn't work in // this case because the regex doesn't match the example (because the example has been truncated and // the regex contains pieces that would match parts of the original message beyond the truncation point) diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/categorization/MlClassicTokenizerTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/categorization/MlClassicTokenizerTests.java index 12822922f985d..9529d7a0da96f 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/categorization/MlClassicTokenizerTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/categorization/MlClassicTokenizerTests.java @@ -14,7 +14,6 @@ import java.io.IOException; import java.io.StringReader; - public class MlClassicTokenizerTests extends ESTestCase { public void testTokenize() throws IOException { diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/config/CategorizationAnalyzerConfigTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/config/CategorizationAnalyzerConfigTests.java index 0e11b6855f601..a15eb51592bca 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/config/CategorizationAnalyzerConfigTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/config/CategorizationAnalyzerConfigTests.java @@ -7,9 +7,9 @@ package org.elasticsearch.xpack.ml.job.config; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xpack.core.ml.job.config.CategorizationAnalyzerConfig; import java.io.IOException; @@ -75,8 +75,9 @@ public void testAsMap() throws IOException { Map map = CategorizationAnalyzerConfig.buildDefaultCategorizationAnalyzer(Collections.emptyList()) .asMap(NamedXContentRegistry.EMPTY); @SuppressWarnings("unchecked") - Map firstLevel = - (Map) map.get(CategorizationAnalyzerConfig.CATEGORIZATION_ANALYZER.getPreferredName()); + Map firstLevel = (Map) map.get( + CategorizationAnalyzerConfig.CATEGORIZATION_ANALYZER.getPreferredName() + ); assertThat(firstLevel, not(nullValue())); String tokenizer = (String) firstLevel.get(CategorizationAnalyzerConfig.TOKENIZER.getPreferredName()); assertThat(tokenizer, is("ml_classic")); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/config/DefaultDetectorDescriptionTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/config/DefaultDetectorDescriptionTests.java index 1dc152e351bb6..741d7e65360d1 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/config/DefaultDetectorDescriptionTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/config/DefaultDetectorDescriptionTests.java @@ -12,21 +12,18 @@ public class DefaultDetectorDescriptionTests extends ESTestCase { - public void testOf_GivenOnlyFunctionAndFieldName() { Detector detector = new Detector.Builder("min", "value").build(); assertEquals("min(value)", DefaultDetectorDescription.of(detector)); } - public void testOf_GivenOnlyFunctionAndFieldNameWithNonWordChars() { Detector detector = new Detector.Builder("min", "val-ue").build(); assertEquals("min(\"val-ue\")", DefaultDetectorDescription.of(detector)); } - public void testOf_GivenFullyPopulatedDetector() { Detector.Builder detector = new Detector.Builder("sum", "value"); detector.setByFieldName("airline"); @@ -35,7 +32,9 @@ public void testOf_GivenFullyPopulatedDetector() { detector.setPartitionFieldName("planet"); detector.setExcludeFrequent(Detector.ExcludeFrequent.ALL); - assertEquals("sum(value) by airline over region usenull=true partitionfield=planet excludefrequent=all", - DefaultDetectorDescription.of(detector.build())); + assertEquals( + "sum(value) by airline over region usenull=true partitionfield=planet excludefrequent=all", + DefaultDetectorDescription.of(detector.build()) + ); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/config/JobBuilderTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/config/JobBuilderTests.java index bfc1a20109ab8..999f1bcd3464f 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/config/JobBuilderTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/config/JobBuilderTests.java @@ -65,8 +65,7 @@ protected Job.Builder createTestInstance() { builder.setResultsRetentionDays(randomNonNegativeLong()); } if (randomBoolean()) { - builder.setCustomSettings(Collections.singletonMap(randomAlphaOfLength(10), - randomAlphaOfLength(10))); + builder.setCustomSettings(Collections.singletonMap(randomAlphaOfLength(10), randomAlphaOfLength(10))); } if (randomBoolean()) { builder.setModelSnapshotId(randomAlphaOfLength(10)); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/config/JobTaskStateTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/config/JobTaskStateTests.java index f4f9862dca8f1..2d215f367f740 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/config/JobTaskStateTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/config/JobTaskStateTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.ml.job.config; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.job.config.JobState; import org.elasticsearch.xpack.core.ml.job.config.JobTaskState; diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/messages/MessagesTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/messages/MessagesTests.java index 8081f4eca1b07..3b14893551f49 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/messages/MessagesTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/messages/MessagesTests.java @@ -11,12 +11,14 @@ public class MessagesTests extends ESTestCase { - public void testGetMessage_NoFormatArgs () { - assertEquals(Messages.DATAFEED_CONFIG_CANNOT_USE_SCRIPT_FIELDS_WITH_AGGS, - Messages.getMessage(Messages.DATAFEED_CONFIG_CANNOT_USE_SCRIPT_FIELDS_WITH_AGGS)); + public void testGetMessage_NoFormatArgs() { + assertEquals( + Messages.DATAFEED_CONFIG_CANNOT_USE_SCRIPT_FIELDS_WITH_AGGS, + Messages.getMessage(Messages.DATAFEED_CONFIG_CANNOT_USE_SCRIPT_FIELDS_WITH_AGGS) + ); } - public void testGetMessage_WithFormatStrings() { + public void testGetMessage_WithFormatStrings() { String formattedMessage = Messages.getMessage(Messages.DATAFEED_CONFIG_INVALID_OPTION_VALUE, "field-name", "field-value"); assertEquals("Invalid field-name value 'field-value' in datafeed configuration", formattedMessage); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/persistence/InfluencersQueryBuilderTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/persistence/InfluencersQueryBuilderTests.java index 76958f827df85..5a0205ce48bbd 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/persistence/InfluencersQueryBuilderTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/persistence/InfluencersQueryBuilderTests.java @@ -25,16 +25,15 @@ public void testDefaultBuild() throws Exception { } public void testAll() { - InfluencersQueryBuilder.InfluencersQuery query = new InfluencersQueryBuilder() - .from(20) - .size(40) - .includeInterim(true) - .influencerScoreThreshold(50.0d) - .start("1000") - .end("2000") - .sortField("anomaly_score") - .sortDescending(true) - .build(); + InfluencersQueryBuilder.InfluencersQuery query = new InfluencersQueryBuilder().from(20) + .size(40) + .includeInterim(true) + .influencerScoreThreshold(50.0d) + .start("1000") + .end("2000") + .sortField("anomaly_score") + .sortDescending(true) + .build(); assertEquals(20, query.getFrom()); assertEquals(40, query.getSize()); @@ -47,42 +46,30 @@ public void testAll() { } public void testEqualsHash() { - InfluencersQueryBuilder query = new InfluencersQueryBuilder() - .from(20) - .size(40) - .includeInterim(true) - .influencerScoreThreshold(50.0d) - .start("1000") - .end("2000"); + InfluencersQueryBuilder query = new InfluencersQueryBuilder().from(20) + .size(40) + .includeInterim(true) + .influencerScoreThreshold(50.0d) + .start("1000") + .end("2000"); - InfluencersQueryBuilder query2 = new InfluencersQueryBuilder() - .from(20) - .size(40) - .includeInterim(true) - .influencerScoreThreshold(50.0d) - .start("1000") - .end("2000"); + InfluencersQueryBuilder query2 = new InfluencersQueryBuilder().from(20) + .size(40) + .includeInterim(true) + .influencerScoreThreshold(50.0d) + .start("1000") + .end("2000"); assertEquals(query.build(), query2.build()); assertEquals(query.build().hashCode(), query2.build().hashCode()); query2.clear(); assertFalse(query.build().equals(query2.build())); - query2.from(20) - .size(40) - .includeInterim(true) - .influencerScoreThreshold(50.0d) - .start("1000") - .end("2000"); + query2.from(20).size(40).includeInterim(true).influencerScoreThreshold(50.0d).start("1000").end("2000"); assertEquals(query.build(), query2.build()); query2.clear(); - query2.from(20) - .size(40) - .includeInterim(true) - .influencerScoreThreshold(50.1d) - .start("1000") - .end("2000"); + query2.from(20).size(40).includeInterim(true).influencerScoreThreshold(50.1d).start("1000").end("2000"); assertFalse(query.build().equals(query2.build())); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/persistence/JobDataDeleterTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/persistence/JobDataDeleterTests.java index 16b8eaf563f7c..478fd5b7ffba7 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/persistence/JobDataDeleterTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/persistence/JobDataDeleterTests.java @@ -60,10 +60,7 @@ public void verifyNoMoreInteractionsWithClient() { public void testDeleteAllAnnotations() { JobDataDeleter jobDataDeleter = new JobDataDeleter(client, JOB_ID); - jobDataDeleter.deleteAllAnnotations(ActionListener.wrap( - deleteResponse -> {}, - e -> fail(e.toString()) - )); + jobDataDeleter.deleteAllAnnotations(ActionListener.wrap(deleteResponse -> {}, e -> fail(e.toString()))); verify(client).execute(eq(DeleteByQueryAction.INSTANCE), deleteRequestCaptor.capture(), any()); @@ -76,15 +73,12 @@ public void testDeleteAllAnnotations() { public void testDeleteAnnotations_TimestampFiltering() { JobDataDeleter jobDataDeleter = new JobDataDeleter(client, JOB_ID); - Tuple range = - randomFrom( - tuple(1_000_000_000L, 2_000_000_000L), - tuple(1_000_000_000L, null), - tuple(null, 2_000_000_000L)); - jobDataDeleter.deleteAnnotations(range.v1(), range.v2(), null, ActionListener.wrap( - deleteResponse -> {}, - e -> fail(e.toString()) - )); + Tuple range = randomFrom( + tuple(1_000_000_000L, 2_000_000_000L), + tuple(1_000_000_000L, null), + tuple(null, 2_000_000_000L) + ); + jobDataDeleter.deleteAnnotations(range.v1(), range.v2(), null, ActionListener.wrap(deleteResponse -> {}, e -> fail(e.toString()))); verify(client).execute(eq(DeleteByQueryAction.INSTANCE), deleteRequestCaptor.capture(), any()); @@ -97,10 +91,12 @@ public void testDeleteAnnotations_TimestampFiltering() { public void testDeleteAnnotations_EventFiltering() { JobDataDeleter jobDataDeleter = new JobDataDeleter(client, JOB_ID); - jobDataDeleter.deleteAnnotations(null, null, Set.of("dummy_event"), ActionListener.wrap( - deleteResponse -> {}, - e -> fail(e.toString()) - )); + jobDataDeleter.deleteAnnotations( + null, + null, + Set.of("dummy_event"), + ActionListener.wrap(deleteResponse -> {}, e -> fail(e.toString())) + ); verify(client).execute(eq(DeleteByQueryAction.INSTANCE), deleteRequestCaptor.capture(), any()); @@ -113,10 +109,7 @@ public void testDeleteAnnotations_EventFiltering() { public void testDeleteDatafeedTimingStats() { JobDataDeleter jobDataDeleter = new JobDataDeleter(client, JOB_ID); - jobDataDeleter.deleteDatafeedTimingStats(ActionListener.wrap( - deleteResponse -> {}, - e -> fail(e.toString()) - )); + jobDataDeleter.deleteDatafeedTimingStats(ActionListener.wrap(deleteResponse -> {}, e -> fail(e.toString()))); verify(client).execute(eq(DeleteByQueryAction.INSTANCE), deleteRequestCaptor.capture(), any()); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/persistence/JobRenormalizedResultsPersisterTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/persistence/JobRenormalizedResultsPersisterTests.java index fa944bace7ae9..425378a754d8c 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/persistence/JobRenormalizedResultsPersisterTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/persistence/JobRenormalizedResultsPersisterTests.java @@ -9,10 +9,10 @@ import org.elasticsearch.action.bulk.BulkResponse; import org.elasticsearch.client.Client; import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.xpack.ml.job.process.normalizer.BucketNormalizable; import org.elasticsearch.xpack.core.ml.job.results.Bucket; import org.elasticsearch.xpack.core.ml.job.results.BucketInfluencer; import org.elasticsearch.xpack.core.ml.job.results.ModelPlot; +import org.elasticsearch.xpack.ml.job.process.normalizer.BucketNormalizable; import java.util.Date; @@ -48,7 +48,7 @@ public void testBulkRequestExecutesWhenReachMaxDocs() { JobRenormalizedResultsPersister persister = new JobRenormalizedResultsPersister("foo", client); ModelPlot modelPlot = new ModelPlot("foo", new Date(), 123456, 0); - for (int i=0; i<=JobRenormalizedResultsPersister.BULK_LIMIT; i++) { + for (int i = 0; i <= JobRenormalizedResultsPersister.BULK_LIMIT; i++) { persister.updateResult("bar", "index-foo", modelPlot); } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/persistence/JobResultsPersisterTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/persistence/JobResultsPersisterTests.java index 4da4e5552419b..d10521f52a01c 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/persistence/JobResultsPersisterTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/persistence/JobResultsPersisterTests.java @@ -69,7 +69,6 @@ import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; - public class JobResultsPersisterTests extends ESTestCase { private static final String JOB_ID = "foo"; @@ -114,7 +113,7 @@ public void testPersistBucket_OneRecord() { BulkRequest bulkRequest = bulkRequestCaptor.getValue(); assertEquals(2, bulkRequest.numberOfActions()); - String s = ((IndexRequest)bulkRequest.requests().get(0)).source().utf8ToString(); + String s = ((IndexRequest) bulkRequest.requests().get(0)).source().utf8ToString(); assertTrue(s.matches(".*anomaly_score.:99\\.9.*")); assertTrue(s.matches(".*initial_anomaly_score.:88\\.8.*")); assertTrue(s.matches(".*event_count.:57.*")); @@ -123,7 +122,7 @@ public void testPersistBucket_OneRecord() { // There should NOT be any nested records assertFalse(s.matches(".*records*")); - s = ((IndexRequest)bulkRequest.requests().get(1)).source().utf8ToString(); + s = ((IndexRequest) bulkRequest.requests().get(1)).source().utf8ToString(); assertTrue(s.matches(".*probability.:0\\.0054.*")); assertTrue(s.matches(".*influencer_field_name.:.biOne.*")); assertTrue(s.matches(".*initial_anomaly_score.:18\\.12.*")); @@ -224,7 +223,7 @@ public void testExecuteRequest_ClearsBulkRequest() { public void testBulkRequestExecutesWhenReachMaxDocs() { JobResultsPersister.Builder bulkBuilder = persister.bulkPersisterBuilder("foo"); ModelPlot modelPlot = new ModelPlot("foo", new Date(), 123456, 0); - for (int i=0; i<=JobRenormalizedResultsPersister.BULK_LIMIT; i++) { + for (int i = 0; i <= JobRenormalizedResultsPersister.BULK_LIMIT; i++) { bulkBuilder.persistModelPlot(modelPlot); } @@ -236,9 +235,15 @@ public void testBulkRequestExecutesWhenReachMaxDocs() { } public void testPersistTimingStats() { - TimingStats timingStats = - new TimingStats( - "foo", 7, 1.0, 2.0, 1.23, 7.89, new ExponentialAverageCalculationContext(600.0, Instant.ofEpochMilli(123456789), 60.0)); + TimingStats timingStats = new TimingStats( + "foo", + 7, + 1.0, + 2.0, + 1.23, + 7.89, + new ExponentialAverageCalculationContext(600.0, Instant.ofEpochMilli(123456789), 60.0) + ); persister.bulkPersisterBuilder(JOB_ID).persistTimingStats(timingStats).executeRequest(); InOrder inOrder = inOrder(client); @@ -256,24 +261,36 @@ public void testPersistTimingStats() { indexRequest.sourceAsMap(), equalTo( Map.of( - "result_type", "timing_stats", - "job_id", "foo", - "bucket_count", 7, - "minimum_bucket_processing_time_ms", 1.0, - "maximum_bucket_processing_time_ms", 2.0, - "average_bucket_processing_time_ms", 1.23, - "exponential_average_bucket_processing_time_ms", 7.89, - "exponential_average_calculation_context", Map.of( - "incremental_metric_value_ms", 600.0, - "previous_exponential_average_ms", 60.0, - "latest_timestamp", 123456789)))); + "result_type", + "timing_stats", + "job_id", + "foo", + "bucket_count", + 7, + "minimum_bucket_processing_time_ms", + 1.0, + "maximum_bucket_processing_time_ms", + 2.0, + "average_bucket_processing_time_ms", + 1.23, + "exponential_average_bucket_processing_time_ms", + 7.89, + "exponential_average_calculation_context", + Map.of("incremental_metric_value_ms", 600.0, "previous_exponential_average_ms", 60.0, "latest_timestamp", 123456789) + ) + ) + ); } @SuppressWarnings("unchecked") public void testPersistDatafeedTimingStats() { - DatafeedTimingStats timingStats = - new DatafeedTimingStats( - "foo", 6, 66, 666.0, new ExponentialAverageCalculationContext(600.0, Instant.ofEpochMilli(123456789), 60.0)); + DatafeedTimingStats timingStats = new DatafeedTimingStats( + "foo", + 6, + 66, + 666.0, + new ExponentialAverageCalculationContext(600.0, Instant.ofEpochMilli(123456789), 60.0) + ); persister.persistDatafeedTimingStats(timingStats, WriteRequest.RefreshPolicy.IMMEDIATE); InOrder inOrder = inOrder(client); @@ -291,15 +308,21 @@ public void testPersistDatafeedTimingStats() { indexRequest.sourceAsMap(), equalTo( Map.of( - "result_type", "datafeed_timing_stats", - "job_id", "foo", - "search_count", 6, - "bucket_count", 66, - "total_search_time_ms", 666.0, - "exponential_average_calculation_context", Map.of( - "incremental_metric_value_ms", 600.0, - "previous_exponential_average_ms", 60.0, - "latest_timestamp", 123456789)))); + "result_type", + "datafeed_timing_stats", + "job_id", + "foo", + "search_count", + 6, + "bucket_count", + 66, + "total_search_time_ms", + 666.0, + "exponential_average_calculation_context", + Map.of("incremental_metric_value_ms", 600.0, "previous_exponential_average_ms", 60.0, "latest_timestamp", 123456789) + ) + ) + ); } @SuppressWarnings("unchecked") @@ -332,8 +355,9 @@ public void testPersistQuantilesSync_QuantilesDocumentCreated() { public void testPersistQuantilesSync_QuantilesDocumentUpdated() { testPersistQuantilesSync( - new SearchHits(new SearchHit[]{ SearchHit.createFromMap(Map.of("_index", ".ml-state-dummy")) }, null, 0.0f), - ".ml-state-dummy"); + new SearchHits(new SearchHit[] { SearchHit.createFromMap(Map.of("_index", ".ml-state-dummy")) }, null, 0.0f), + ".ml-state-dummy" + ); } @SuppressWarnings("unchecked") @@ -370,8 +394,9 @@ public void testPersistQuantilesAsync_QuantilesDocumentCreated() { public void testPersistQuantilesAsync_QuantilesDocumentUpdated() { testPersistQuantilesAsync( - new SearchHits(new SearchHit[]{ SearchHit.createFromMap(Map.of("_index", ".ml-state-dummy")) }, null, 0.0f), - ".ml-state-dummy"); + new SearchHits(new SearchHit[] { SearchHit.createFromMap(Map.of("_index", ".ml-state-dummy")) }, null, 0.0f), + ".ml-state-dummy" + ); } @SuppressWarnings("unchecked") @@ -385,13 +410,19 @@ private static Answer withResponse(Response response) { private ResultsPersisterService buildResultsPersisterService(OriginSettingClient client) { ThreadPool tp = mock(ThreadPool.class); - ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, - new HashSet<>(Arrays.asList(InferenceProcessor.MAX_INFERENCE_PROCESSORS, - MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING, - OperationRouting.USE_ADAPTIVE_REPLICA_SELECTION_SETTING, - ResultsPersisterService.PERSIST_RESULTS_MAX_RETRIES, - ClusterService.USER_DEFINED_METADATA, - ClusterApplierService.CLUSTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING))); + ClusterSettings clusterSettings = new ClusterSettings( + Settings.EMPTY, + new HashSet<>( + Arrays.asList( + InferenceProcessor.MAX_INFERENCE_PROCESSORS, + MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING, + OperationRouting.USE_ADAPTIVE_REPLICA_SELECTION_SETTING, + ResultsPersisterService.PERSIST_RESULTS_MAX_RETRIES, + ClusterService.USER_DEFINED_METADATA, + ClusterApplierService.CLUSTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING + ) + ) + ); ClusterService clusterService = new ClusterService(Settings.EMPTY, clusterSettings, tp); ExecutorService executor = mock(ExecutorService.class); doAnswer(invocationOnMock -> { @@ -402,9 +433,7 @@ private ResultsPersisterService buildResultsPersisterService(OriginSettingClient doAnswer(invocationOnMock -> { ((Runnable) invocationOnMock.getArguments()[0]).run(); return null; - }).when(tp).schedule( - any(Runnable.class), any(TimeValue.class), any(String.class) - ); + }).when(tp).schedule(any(Runnable.class), any(TimeValue.class), any(String.class)); return new ResultsPersisterService(tp, client, clusterService, Settings.EMPTY); } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/persistence/JobResultsProviderTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/persistence/JobResultsProviderTests.java index d251f87681d39..53533eedc0d58 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/persistence/JobResultsProviderTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/persistence/JobResultsProviderTests.java @@ -26,16 +26,16 @@ import org.elasticsearch.common.document.DocumentField; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.indices.TestIndexNameExpressionResolver; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.action.util.QueryPage; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedTimingStats; import org.elasticsearch.xpack.core.ml.job.config.Job; @@ -94,14 +94,12 @@ public void testBuckets_OneBucketNoInterim() throws IOException { BucketsQueryBuilder bq = new BucketsQueryBuilder().from(from).size(size).anomalyScoreThreshold(1.0); SetOnce> holder = new SetOnce<>(); - provider.buckets(jobId, bq, holder::set, e -> {throw new RuntimeException(e);}, client); + provider.buckets(jobId, bq, holder::set, e -> { throw new RuntimeException(e); }, client); QueryPage buckets = holder.get(); assertEquals(1L, buckets.count()); QueryBuilder query = queryBuilderHolder[0]; String queryString = query.toString(); - assertTrue( - queryString.matches("(?s).*anomaly_score[^}]*from. : 1\\.0.*must_not[^}]*term[^}]*is_interim.*value. : true" + - ".*")); + assertTrue(queryString.matches("(?s).*anomaly_score[^}]*from. : 1\\.0.*must_not[^}]*term[^}]*is_interim.*value. : true" + ".*")); } public void testBuckets_OneBucketInterim() throws IOException { @@ -123,11 +121,10 @@ public void testBuckets_OneBucketInterim() throws IOException { Client client = getMockedClient(queryBuilder -> queryBuilderHolder[0] = queryBuilder, response); JobResultsProvider provider = createProvider(client); - BucketsQueryBuilder bq = new BucketsQueryBuilder().from(from).size(size).anomalyScoreThreshold(5.1) - .includeInterim(true); + BucketsQueryBuilder bq = new BucketsQueryBuilder().from(from).size(size).anomalyScoreThreshold(5.1).includeInterim(true); SetOnce> holder = new SetOnce<>(); - provider.buckets(jobId, bq, holder::set, e -> {throw new RuntimeException(e);}, client); + provider.buckets(jobId, bq, holder::set, e -> { throw new RuntimeException(e); }, client); QueryPage buckets = holder.get(); assertEquals(1L, buckets.count()); QueryBuilder query = queryBuilderHolder[0]; @@ -162,7 +159,7 @@ public void testBuckets_UsingBuilder() throws IOException { bq.includeInterim(true); SetOnce> holder = new SetOnce<>(); - provider.buckets(jobId, bq, holder::set, e -> {throw new RuntimeException(e);}, client); + provider.buckets(jobId, bq, holder::set, e -> { throw new RuntimeException(e); }, client); QueryPage buckets = holder.get(); assertEquals(1L, buckets.count()); QueryBuilder query = queryBuilderHolder[0]; @@ -242,9 +239,13 @@ public void testRecords() throws IOException { Client client = getMockedClient(qb -> {}, response); JobResultsProvider provider = createProvider(client); - RecordsQueryBuilder rqb = new RecordsQueryBuilder().from(from).size(size).epochStart(String.valueOf(now.getTime())) - .epochEnd(String.valueOf(now.getTime())).includeInterim(true).sortField(sortfield) - .recordScore(2.2); + RecordsQueryBuilder rqb = new RecordsQueryBuilder().from(from) + .size(size) + .epochStart(String.valueOf(now.getTime())) + .epochEnd(String.valueOf(now.getTime())) + .includeInterim(true) + .sortField(sortfield) + .recordScore(2.2); SetOnce> holder = new SetOnce<>(); provider.records(jobId, rqb, holder::set, e -> { throw new RuntimeException(e); }, client); @@ -343,8 +344,18 @@ public void testBucketRecords() throws IOException { JobResultsProvider provider = createProvider(client); SetOnce> holder = new SetOnce<>(); - provider.bucketRecords(jobId, bucket, from, size, true, sortfield, true, holder::set, - e -> { throw new RuntimeException(e); }, client); + provider.bucketRecords( + jobId, + bucket, + from, + size, + true, + sortfield, + true, + holder::set, + e -> { throw new RuntimeException(e); }, + client + ); QueryPage recordPage = holder.get(); assertEquals(2L, recordPage.count()); List records = recordPage.results(); @@ -403,8 +414,7 @@ public void testCategoryDefinitions() throws IOException { JobResultsProvider provider = createProvider(client); SetOnce> holder = new SetOnce<>(); - provider.categoryDefinitions(jobId, null, null, false, from, size, holder::set, - e -> { throw new RuntimeException(e); }, client); + provider.categoryDefinitions(jobId, null, null, false, from, size, holder::set, e -> { throw new RuntimeException(e); }, client); QueryPage categoryDefinitions = holder.get(); assertEquals(1L, categoryDefinitions.count()); assertEquals(terms, categoryDefinitions.results().get(0).getTerms()); @@ -424,8 +434,17 @@ public void testCategoryDefinition() throws IOException { Client client = getMockedClient(q -> {}, response); JobResultsProvider provider = createProvider(client); SetOnce> holder = new SetOnce<>(); - provider.categoryDefinitions(jobId, categoryId, null, false, null, null, - holder::set, e -> { throw new RuntimeException(e); }, client); + provider.categoryDefinitions( + jobId, + categoryId, + null, + false, + null, + null, + holder::set, + e -> { throw new RuntimeException(e); }, + client + ); QueryPage categoryDefinitions = holder.get(); assertEquals(1L, categoryDefinitions.count()); assertEquals(terms, categoryDefinitions.results().get(0).getTerms()); @@ -524,8 +543,15 @@ public void testInfluencers_WithInterim() throws IOException { JobResultsProvider provider = createProvider(client); SetOnce> holder = new SetOnce<>(); - InfluencersQuery query = new InfluencersQueryBuilder().from(from).size(size).start("0").end("0").sortField("sort") - .sortDescending(true).influencerScoreThreshold(0.0).includeInterim(true).build(); + InfluencersQuery query = new InfluencersQueryBuilder().from(from) + .size(size) + .start("0") + .end("0") + .sortField("sort") + .sortDescending(true) + .influencerScoreThreshold(0.0) + .includeInterim(true) + .build(); provider.influencers(jobId, query, holder::set, e -> { throw new RuntimeException(e); }, client); QueryPage page = holder.get(); assertEquals(2L, page.count()); @@ -605,13 +631,12 @@ public void testViolatedFieldCountLimit() { mapping.put("field" + i, Collections.singletonMap("type", "string")); } - IndexMetadata indexMetadata1 = new IndexMetadata.Builder("index1") - .settings(Settings.builder() - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)) - .putMapping(new MappingMetadata("type1", Collections.singletonMap("properties", mapping))) - .build(); + IndexMetadata indexMetadata1 = new IndexMetadata.Builder("index1").settings( + Settings.builder() + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + ).putMapping(new MappingMetadata("type1", Collections.singletonMap("properties", mapping))).build(); boolean result = JobResultsProvider.violatedFieldCountLimit(0, 10, indexMetadata1.mapping()); assertFalse(result); @@ -622,13 +647,12 @@ public void testViolatedFieldCountLimit() { mapping.put("field" + i, Collections.singletonMap("type", "string")); } - IndexMetadata indexMetadata2 = new IndexMetadata.Builder("index1") - .settings(Settings.builder() - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)) - .putMapping(new MappingMetadata("type1", Collections.singletonMap("properties", mapping))) - .build(); + IndexMetadata indexMetadata2 = new IndexMetadata.Builder("index1").settings( + Settings.builder() + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + ).putMapping(new MappingMetadata("type1", Collections.singletonMap("properties", mapping))).build(); result = JobResultsProvider.violatedFieldCountLimit(0, 19, indexMetadata2.mapping()); assertTrue(result); @@ -655,32 +679,46 @@ public void testCountFields() { public void testTimingStats_Ok() throws IOException { String indexName = AnomalyDetectorsIndex.jobResultsAliasedName("foo"); - List> source = - Collections.singletonList( + List> source = Collections.singletonList( + Map.of( + Job.ID.getPreferredName(), + "foo", + TimingStats.BUCKET_COUNT.getPreferredName(), + 7, + TimingStats.MIN_BUCKET_PROCESSING_TIME_MS.getPreferredName(), + 1.0, + TimingStats.MAX_BUCKET_PROCESSING_TIME_MS.getPreferredName(), + 1000.0, + TimingStats.AVG_BUCKET_PROCESSING_TIME_MS.getPreferredName(), + 666.0, + TimingStats.EXPONENTIAL_AVG_BUCKET_PROCESSING_TIME_MS.getPreferredName(), + 777.0, + TimingStats.EXPONENTIAL_AVG_CALCULATION_CONTEXT.getPreferredName(), Map.of( - Job.ID.getPreferredName(), "foo", - TimingStats.BUCKET_COUNT.getPreferredName(), 7, - TimingStats.MIN_BUCKET_PROCESSING_TIME_MS.getPreferredName(), 1.0, - TimingStats.MAX_BUCKET_PROCESSING_TIME_MS.getPreferredName(), 1000.0, - TimingStats.AVG_BUCKET_PROCESSING_TIME_MS.getPreferredName(), 666.0, - TimingStats.EXPONENTIAL_AVG_BUCKET_PROCESSING_TIME_MS.getPreferredName(), 777.0, - TimingStats.EXPONENTIAL_AVG_CALCULATION_CONTEXT.getPreferredName(), Map.of( - ExponentialAverageCalculationContext.INCREMENTAL_METRIC_VALUE_MS.getPreferredName(), 100.0, - ExponentialAverageCalculationContext.LATEST_TIMESTAMP.getPreferredName(), Instant.ofEpochMilli(1000_000_000), - ExponentialAverageCalculationContext.PREVIOUS_EXPONENTIAL_AVERAGE_MS.getPreferredName(), 200.0))); + ExponentialAverageCalculationContext.INCREMENTAL_METRIC_VALUE_MS.getPreferredName(), + 100.0, + ExponentialAverageCalculationContext.LATEST_TIMESTAMP.getPreferredName(), + Instant.ofEpochMilli(1000_000_000), + ExponentialAverageCalculationContext.PREVIOUS_EXPONENTIAL_AVERAGE_MS.getPreferredName(), + 200.0 + ) + ) + ); SearchResponse response = createSearchResponse(source); - Client client = getMockedClient( - queryBuilder -> assertThat(queryBuilder.getName(), equalTo("ids")), - response); + Client client = getMockedClient(queryBuilder -> assertThat(queryBuilder.getName(), equalTo("ids")), response); when(client.prepareSearch(indexName)).thenReturn(new SearchRequestBuilder(client, SearchAction.INSTANCE).setIndices(indexName)); JobResultsProvider provider = createProvider(client); - ExponentialAverageCalculationContext context = - new ExponentialAverageCalculationContext(100.0, Instant.ofEpochMilli(1000_000_000), 200.0); + ExponentialAverageCalculationContext context = new ExponentialAverageCalculationContext( + 100.0, + Instant.ofEpochMilli(1000_000_000), + 200.0 + ); provider.timingStats( "foo", stats -> assertThat(stats, equalTo(new TimingStats("foo", 7, 1.0, 1000.0, 666.0, 777.0, context))), - e -> { throw new AssertionError("Failure getting timing stats", e); }); + e -> { throw new AssertionError("Failure getting timing stats", e); } + ); verify(client).prepareSearch(indexName); verify(client).threadPool(); @@ -692,16 +730,15 @@ public void testTimingStats_NotFound() throws IOException { String indexName = AnomalyDetectorsIndex.jobResultsAliasedName("foo"); List> source = new ArrayList<>(); SearchResponse response = createSearchResponse(source); - Client client = getMockedClient( - queryBuilder -> assertThat(queryBuilder.getName(), equalTo("ids")), - response); + Client client = getMockedClient(queryBuilder -> assertThat(queryBuilder.getName(), equalTo("ids")), response); when(client.prepareSearch(indexName)).thenReturn(new SearchRequestBuilder(client, SearchAction.INSTANCE).setIndices(indexName)); JobResultsProvider provider = createProvider(client); provider.timingStats( "foo", stats -> assertThat(stats, equalTo(new TimingStats("foo"))), - e -> { throw new AssertionError("Failure getting timing stats", e); }); + e -> { throw new AssertionError("Failure getting timing stats", e); } + ); verify(client).prepareSearch(indexName); verify(client).threadPool(); @@ -717,41 +754,64 @@ public void testDatafeedTimingStats_EmptyJobList() { List.of(), ActionListener.wrap( statsByJobId -> assertThat(statsByJobId, anEmptyMap()), - e -> { throw new AssertionError("Failure getting datafeed timing stats", e); })); + e -> { throw new AssertionError("Failure getting datafeed timing stats", e); } + ) + ); verifyZeroInteractions(client); } public void testDatafeedTimingStats_MultipleDocumentsAtOnce() throws IOException { - List> sourceFoo = - Collections.singletonList( + List> sourceFoo = Collections.singletonList( + Map.of( + Job.ID.getPreferredName(), + "foo", + DatafeedTimingStats.SEARCH_COUNT.getPreferredName(), + 6, + DatafeedTimingStats.BUCKET_COUNT.getPreferredName(), + 66, + DatafeedTimingStats.TOTAL_SEARCH_TIME_MS.getPreferredName(), + 666.0, + DatafeedTimingStats.EXPONENTIAL_AVG_CALCULATION_CONTEXT.getPreferredName(), Map.of( - Job.ID.getPreferredName(), "foo", - DatafeedTimingStats.SEARCH_COUNT.getPreferredName(), 6, - DatafeedTimingStats.BUCKET_COUNT.getPreferredName(), 66, - DatafeedTimingStats.TOTAL_SEARCH_TIME_MS.getPreferredName(), 666.0, - DatafeedTimingStats.EXPONENTIAL_AVG_CALCULATION_CONTEXT.getPreferredName(), Map.of( - ExponentialAverageCalculationContext.INCREMENTAL_METRIC_VALUE_MS.getPreferredName(), 600.0, - ExponentialAverageCalculationContext.LATEST_TIMESTAMP.getPreferredName(), Instant.ofEpochMilli(100000600), - ExponentialAverageCalculationContext.PREVIOUS_EXPONENTIAL_AVERAGE_MS.getPreferredName(), 60.0))); - List> sourceBar = - Collections.singletonList( + ExponentialAverageCalculationContext.INCREMENTAL_METRIC_VALUE_MS.getPreferredName(), + 600.0, + ExponentialAverageCalculationContext.LATEST_TIMESTAMP.getPreferredName(), + Instant.ofEpochMilli(100000600), + ExponentialAverageCalculationContext.PREVIOUS_EXPONENTIAL_AVERAGE_MS.getPreferredName(), + 60.0 + ) + ) + ); + List> sourceBar = Collections.singletonList( + Map.of( + Job.ID.getPreferredName(), + "bar", + DatafeedTimingStats.SEARCH_COUNT.getPreferredName(), + 7, + DatafeedTimingStats.BUCKET_COUNT.getPreferredName(), + 77, + DatafeedTimingStats.TOTAL_SEARCH_TIME_MS.getPreferredName(), + 777.0, + DatafeedTimingStats.EXPONENTIAL_AVG_CALCULATION_CONTEXT.getPreferredName(), Map.of( - Job.ID.getPreferredName(), "bar", - DatafeedTimingStats.SEARCH_COUNT.getPreferredName(), 7, - DatafeedTimingStats.BUCKET_COUNT.getPreferredName(), 77, - DatafeedTimingStats.TOTAL_SEARCH_TIME_MS.getPreferredName(), 777.0, - DatafeedTimingStats.EXPONENTIAL_AVG_CALCULATION_CONTEXT.getPreferredName(), Map.of( - ExponentialAverageCalculationContext.INCREMENTAL_METRIC_VALUE_MS.getPreferredName(), 700.0, - ExponentialAverageCalculationContext.LATEST_TIMESTAMP.getPreferredName(), Instant.ofEpochMilli(100000700), - ExponentialAverageCalculationContext.PREVIOUS_EXPONENTIAL_AVERAGE_MS.getPreferredName(), 70.0))); + ExponentialAverageCalculationContext.INCREMENTAL_METRIC_VALUE_MS.getPreferredName(), + 700.0, + ExponentialAverageCalculationContext.LATEST_TIMESTAMP.getPreferredName(), + Instant.ofEpochMilli(100000700), + ExponentialAverageCalculationContext.PREVIOUS_EXPONENTIAL_AVERAGE_MS.getPreferredName(), + 70.0 + ) + ) + ); SearchResponse responseFoo = createSearchResponse(sourceFoo); SearchResponse responseBar = createSearchResponse(sourceBar); MultiSearchResponse multiSearchResponse = new MultiSearchResponse( - new MultiSearchResponse.Item[]{ + new MultiSearchResponse.Item[] { new MultiSearchResponse.Item(responseFoo, null), - new MultiSearchResponse.Item(responseBar, null)}, - randomNonNegativeLong()); + new MultiSearchResponse.Item(responseBar, null) }, + randomNonNegativeLong() + ); Client client = getBasicMockedClient(); when(client.prepareMultiSearch()).thenReturn(new MultiSearchRequestBuilder(client, MultiSearchAction.INSTANCE)); @@ -765,30 +825,41 @@ public void testDatafeedTimingStats_MultipleDocumentsAtOnce() throws IOException actionListener.onResponse(multiSearchResponse); return null; }).when(client).multiSearch(any(), any()); - when(client.prepareSearch(AnomalyDetectorsIndex.jobResultsAliasedName("foo"))) - .thenReturn( - new SearchRequestBuilder(client, SearchAction.INSTANCE).setIndices(AnomalyDetectorsIndex.jobResultsAliasedName("foo"))); - when(client.prepareSearch(AnomalyDetectorsIndex.jobResultsAliasedName("bar"))) - .thenReturn( - new SearchRequestBuilder(client, SearchAction.INSTANCE).setIndices(AnomalyDetectorsIndex.jobResultsAliasedName("bar"))); + when(client.prepareSearch(AnomalyDetectorsIndex.jobResultsAliasedName("foo"))).thenReturn( + new SearchRequestBuilder(client, SearchAction.INSTANCE).setIndices(AnomalyDetectorsIndex.jobResultsAliasedName("foo")) + ); + when(client.prepareSearch(AnomalyDetectorsIndex.jobResultsAliasedName("bar"))).thenReturn( + new SearchRequestBuilder(client, SearchAction.INSTANCE).setIndices(AnomalyDetectorsIndex.jobResultsAliasedName("bar")) + ); JobResultsProvider provider = createProvider(client); - ExponentialAverageCalculationContext contextFoo = - new ExponentialAverageCalculationContext(600.0, Instant.ofEpochMilli(100000600), 60.0); - ExponentialAverageCalculationContext contextBar = - new ExponentialAverageCalculationContext(700.0, Instant.ofEpochMilli(100000700), 70.0); + ExponentialAverageCalculationContext contextFoo = new ExponentialAverageCalculationContext( + 600.0, + Instant.ofEpochMilli(100000600), + 60.0 + ); + ExponentialAverageCalculationContext contextBar = new ExponentialAverageCalculationContext( + 700.0, + Instant.ofEpochMilli(100000700), + 70.0 + ); provider.datafeedTimingStats( List.of("foo", "bar"), ActionListener.wrap( - statsByJobId -> - assertThat( - statsByJobId, - equalTo( - Map.of( - "foo", new DatafeedTimingStats("foo", 6, 66, 666.0, contextFoo), - "bar", new DatafeedTimingStats("bar", 7, 77, 777.0, contextBar)))), + statsByJobId -> assertThat( + statsByJobId, + equalTo( + Map.of( + "foo", + new DatafeedTimingStats("foo", 6, 66, 666.0, contextFoo), + "bar", + new DatafeedTimingStats("bar", 7, 77, 777.0, contextBar) + ) + ) + ), e -> fail(e.getMessage()) - )); + ) + ); verify(client).threadPool(); verify(client).prepareMultiSearch(); @@ -800,30 +871,42 @@ public void testDatafeedTimingStats_MultipleDocumentsAtOnce() throws IOException public void testDatafeedTimingStats_Ok() throws IOException { String indexName = AnomalyDetectorsIndex.jobResultsAliasedName("foo"); - List> source = - Collections.singletonList( + List> source = Collections.singletonList( + Map.of( + Job.ID.getPreferredName(), + "foo", + DatafeedTimingStats.SEARCH_COUNT.getPreferredName(), + 6, + DatafeedTimingStats.BUCKET_COUNT.getPreferredName(), + 66, + DatafeedTimingStats.TOTAL_SEARCH_TIME_MS.getPreferredName(), + 666.0, + DatafeedTimingStats.EXPONENTIAL_AVG_CALCULATION_CONTEXT.getPreferredName(), Map.of( - Job.ID.getPreferredName(), "foo", - DatafeedTimingStats.SEARCH_COUNT.getPreferredName(), 6, - DatafeedTimingStats.BUCKET_COUNT.getPreferredName(), 66, - DatafeedTimingStats.TOTAL_SEARCH_TIME_MS.getPreferredName(), 666.0, - DatafeedTimingStats.EXPONENTIAL_AVG_CALCULATION_CONTEXT.getPreferredName(), Map.of( - ExponentialAverageCalculationContext.INCREMENTAL_METRIC_VALUE_MS.getPreferredName(), 600.0, - ExponentialAverageCalculationContext.LATEST_TIMESTAMP.getPreferredName(), Instant.ofEpochMilli(100000600), - ExponentialAverageCalculationContext.PREVIOUS_EXPONENTIAL_AVERAGE_MS.getPreferredName(), 60.0))); + ExponentialAverageCalculationContext.INCREMENTAL_METRIC_VALUE_MS.getPreferredName(), + 600.0, + ExponentialAverageCalculationContext.LATEST_TIMESTAMP.getPreferredName(), + Instant.ofEpochMilli(100000600), + ExponentialAverageCalculationContext.PREVIOUS_EXPONENTIAL_AVERAGE_MS.getPreferredName(), + 60.0 + ) + ) + ); SearchResponse response = createSearchResponse(source); - Client client = getMockedClient( - queryBuilder -> assertThat(queryBuilder.getName(), equalTo("ids")), - response); + Client client = getMockedClient(queryBuilder -> assertThat(queryBuilder.getName(), equalTo("ids")), response); when(client.prepareSearch(indexName)).thenReturn(new SearchRequestBuilder(client, SearchAction.INSTANCE).setIndices(indexName)); JobResultsProvider provider = createProvider(client); - ExponentialAverageCalculationContext contextFoo = - new ExponentialAverageCalculationContext(600.0, Instant.ofEpochMilli(100000600), 60.0); + ExponentialAverageCalculationContext contextFoo = new ExponentialAverageCalculationContext( + 600.0, + Instant.ofEpochMilli(100000600), + 60.0 + ); provider.datafeedTimingStats( "foo", stats -> assertThat(stats, equalTo(new DatafeedTimingStats("foo", 6, 66, 666.0, contextFoo))), - e -> { throw new AssertionError("Failure getting datafeed timing stats", e); }); + e -> { throw new AssertionError("Failure getting datafeed timing stats", e); } + ); verify(client).prepareSearch(indexName); verify(client).threadPool(); @@ -835,16 +918,15 @@ public void testDatafeedTimingStats_NotFound() throws IOException { String indexName = AnomalyDetectorsIndex.jobResultsAliasedName("foo"); List> source = new ArrayList<>(); SearchResponse response = createSearchResponse(source); - Client client = getMockedClient( - queryBuilder -> assertThat(queryBuilder.getName(), equalTo("ids")), - response); + Client client = getMockedClient(queryBuilder -> assertThat(queryBuilder.getName(), equalTo("ids")), response); when(client.prepareSearch(indexName)).thenReturn(new SearchRequestBuilder(client, SearchAction.INSTANCE).setIndices(indexName)); JobResultsProvider provider = createProvider(client); provider.datafeedTimingStats( "foo", stats -> assertThat(stats, equalTo(new DatafeedTimingStats("foo"))), - e -> { throw new AssertionError("Failure getting datafeed timing stats", e); }); + e -> { throw new AssertionError("Failure getting datafeed timing stats", e); } + ); verify(client).prepareSearch(indexName); verify(client).threadPool(); @@ -856,20 +938,22 @@ public void testDatafeedTimingStats_NotFound() throws IOException { public void testCreateTermFieldsMapping() throws IOException { XContentBuilder termFieldsMapping = JsonXContent.contentBuilder(); - JobResultsProvider.createTermFieldsMapping(termFieldsMapping, Arrays.asList("apple", "strawberry", - AnomalyRecord.BUCKET_SPAN.getPreferredName())); + JobResultsProvider.createTermFieldsMapping( + termFieldsMapping, + Arrays.asList("apple", "strawberry", AnomalyRecord.BUCKET_SPAN.getPreferredName()) + ); XContentParser parser = createParser(termFieldsMapping); Map properties = (Map) parser.map().get("properties"); Map instanceMapping = (Map) properties.get("apple"); assertNotNull(instanceMapping); - String dataType = (String)instanceMapping.get("type"); + String dataType = (String) instanceMapping.get("type"); assertEquals("keyword", dataType); instanceMapping = (Map) properties.get("strawberry"); assertNotNull(instanceMapping); - dataType = (String)instanceMapping.get("type"); + dataType = (String) instanceMapping.get("type"); assertEquals("keyword", dataType); // check no mapping for the reserved field @@ -892,8 +976,9 @@ private static SearchResponse createSearchResponse(List> sou fields.put("field_1", new DocumentField("field_1", Collections.singletonList("foo"))); fields.put("field_2", new DocumentField("field_2", Collections.singletonList("foo"))); - SearchHit hit = new SearchHit(123, String.valueOf(map.hashCode()), fields, Collections.emptyMap()) - .sourceRef(BytesReference.bytes(XContentFactory.jsonBuilder().map(_source))); + SearchHit hit = new SearchHit(123, String.valueOf(map.hashCode()), fields, Collections.emptyMap()).sourceRef( + BytesReference.bytes(XContentFactory.jsonBuilder().map(_source)) + ); list.add(hit); } @@ -919,8 +1004,9 @@ private Client getMockedClient(Consumer queryBuilderConsumer, Sear @SuppressWarnings("unchecked") ActionListener actionListener = (ActionListener) invocationOnMock.getArguments()[1]; MultiSearchResponse mresponse = new MultiSearchResponse( - new MultiSearchResponse.Item[]{new MultiSearchResponse.Item(response, null)}, - randomNonNegativeLong()); + new MultiSearchResponse.Item[] { new MultiSearchResponse.Item(response, null) }, + randomNonNegativeLong() + ); actionListener.onResponse(mresponse); return null; }).when(client).multiSearch(any(), any()); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/persistence/MockBatchedDocumentsIterator.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/persistence/MockBatchedDocumentsIterator.java index 8f57fe5b6f0e5..99f542f88fb60 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/persistence/MockBatchedDocumentsIterator.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/persistence/MockBatchedDocumentsIterator.java @@ -49,8 +49,9 @@ public BatchedResultsIterator includeInterim(boolean includeInterim) { @Override public Deque> next() { if (requireIncludeInterim != null && requireIncludeInterim != includeInterim) { - throw new IllegalStateException("Required include interim value [" + requireIncludeInterim + "]; actual was [" - + includeInterim + "]"); + throw new IllegalStateException( + "Required include interim value [" + requireIncludeInterim + "]; actual was [" + includeInterim + "]" + ); } if (wasTimeRangeCalled == false || hasNext() == false) { throw new NoSuchElementException(); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/persistence/MockClientBuilder.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/persistence/MockClientBuilder.java index 518faa39ab679..480e995705b70 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/persistence/MockClientBuilder.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/persistence/MockClientBuilder.java @@ -29,13 +29,13 @@ import org.elasticsearch.common.document.DocumentField; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.sort.SortBuilder; import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.XContentBuilder; import org.mockito.ArgumentCaptor; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; @@ -109,8 +109,7 @@ public MockClientBuilder prepareCreate(String index) { return this; } - public MockClientBuilder prepareSearch(String index, int from, int size, SearchResponse response, - ArgumentCaptor filter) { + public MockClientBuilder prepareSearch(String index, int from, int size, SearchResponse response, ArgumentCaptor filter) { SearchRequestBuilder builder = mock(SearchRequestBuilder.class); when(builder.addSort(any(SortBuilder.class))).thenReturn(builder); when(builder.setQuery(filter.capture())).thenReturn(builder); @@ -149,8 +148,8 @@ public MockClientBuilder prepareSearch(String indexName, List do when(client.prepareSearch(eq(indexName))).thenReturn(builder); - SearchHit hits [] = new SearchHit[docs.size()]; - for (int i=0; i modelState2 = new HashMap<>(); modelState2.put("modName2", "modVal2"); - - SearchRequestBuilder builder1 = prepareSearchBuilder(createSearchResponse(Collections.singletonList(modelState1)), - QueryBuilders.idsQuery().addIds(ModelState.documentId(JOB_ID, snapshotId, 1))); - SearchRequestBuilder builder2 = prepareSearchBuilder(createSearchResponse(Collections.singletonList(modelState2)), - QueryBuilders.idsQuery().addIds(ModelState.documentId(JOB_ID, snapshotId, 2))); - SearchRequestBuilder builder3 = prepareSearchBuilder(createSearchResponse(Collections.singletonList(categorizerState)), - QueryBuilders.idsQuery().addIds(CategorizerState.documentId(JOB_ID, 1))); - SearchRequestBuilder builder4 = prepareSearchBuilder(createSearchResponse(Collections.emptyList()), - QueryBuilders.idsQuery().addIds(CategorizerState.documentId(JOB_ID, 2))); - - MockClientBuilder clientBuilder = new MockClientBuilder(CLUSTER_NAME) - .addClusterStatusYellowResponse() + SearchRequestBuilder builder1 = prepareSearchBuilder( + createSearchResponse(Collections.singletonList(modelState1)), + QueryBuilders.idsQuery().addIds(ModelState.documentId(JOB_ID, snapshotId, 1)) + ); + SearchRequestBuilder builder2 = prepareSearchBuilder( + createSearchResponse(Collections.singletonList(modelState2)), + QueryBuilders.idsQuery().addIds(ModelState.documentId(JOB_ID, snapshotId, 2)) + ); + SearchRequestBuilder builder3 = prepareSearchBuilder( + createSearchResponse(Collections.singletonList(categorizerState)), + QueryBuilders.idsQuery().addIds(CategorizerState.documentId(JOB_ID, 1)) + ); + SearchRequestBuilder builder4 = prepareSearchBuilder( + createSearchResponse(Collections.emptyList()), + QueryBuilders.idsQuery().addIds(CategorizerState.documentId(JOB_ID, 2)) + ); + + MockClientBuilder clientBuilder = new MockClientBuilder(CLUSTER_NAME).addClusterStatusYellowResponse() .prepareSearches(AnomalyDetectorsIndex.jobStateIndexPattern(), builder1, builder2, builder3, builder4); ModelSnapshot modelSnapshot = new ModelSnapshot.Builder(JOB_ID).setSnapshotId(snapshotId).setSnapshotDocCount(2).build(); @@ -100,7 +106,7 @@ private static SearchResponse createSearchResponse(List> sou SearchHit hit = new SearchHit(1).sourceRef(BytesReference.bytes(XContentFactory.jsonBuilder().map(s))); hits[i++] = hit; } - SearchHits searchHits = new SearchHits(hits, null, (float)0.0); + SearchHits searchHits = new SearchHits(hits, null, (float) 0.0); when(searchResponse.getHits()).thenReturn(searchHits); return searchResponse; } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/persistence/TimingStatsReporterTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/persistence/TimingStatsReporterTests.java index 44b2254b35f99..ee53a959e779b 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/persistence/TimingStatsReporterTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/persistence/TimingStatsReporterTests.java @@ -107,16 +107,25 @@ public void testFinishReporting_WithChange() { public void testTimingStatsDifferSignificantly() { assertThat( TimingStatsReporter.differSignificantly( - createTimingStats(JOB_ID, 10, 10.0, 10.0, 1.0, 10.0), createTimingStats(JOB_ID, 10, 10.0, 10.0, 1.0, 10.0)), - is(false)); + createTimingStats(JOB_ID, 10, 10.0, 10.0, 1.0, 10.0), + createTimingStats(JOB_ID, 10, 10.0, 10.0, 1.0, 10.0) + ), + is(false) + ); assertThat( TimingStatsReporter.differSignificantly( - createTimingStats(JOB_ID, 10, 10.0, 10.0, 1.0, 10.0), createTimingStats(JOB_ID, 10, 10.0, 11.0, 1.0, 10.0)), - is(false)); + createTimingStats(JOB_ID, 10, 10.0, 10.0, 1.0, 10.0), + createTimingStats(JOB_ID, 10, 10.0, 11.0, 1.0, 10.0) + ), + is(false) + ); assertThat( TimingStatsReporter.differSignificantly( - createTimingStats(JOB_ID, 10, 10.0, 10.0, 1.0, 10.0), createTimingStats(JOB_ID, 10, 10.0, 12.0, 1.0, 10.0)), - is(true)); + createTimingStats(JOB_ID, 10, 10.0, 10.0, 1.0, 10.0), + createTimingStats(JOB_ID, 10, 10.0, 12.0, 1.0, 10.0) + ), + is(true) + ); } public void testValuesDifferSignificantly() { @@ -141,7 +150,8 @@ private static TimingStats createTimingStats( @Nullable Double minBucketProcessingTimeMs, @Nullable Double maxBucketProcessingTimeMs, @Nullable Double avgBucketProcessingTimeMs, - @Nullable Double exponentialAvgBucketProcessingTimeMs) { + @Nullable Double exponentialAvgBucketProcessingTimeMs + ) { return createTimingStats( jobId, bucketCount, @@ -149,19 +159,24 @@ private static TimingStats createTimingStats( maxBucketProcessingTimeMs, avgBucketProcessingTimeMs, exponentialAvgBucketProcessingTimeMs, - 0.0); + 0.0 + ); } private static TimingStats createTimingStats( - String jobId, - long bucketCount, - @Nullable Double minBucketProcessingTimeMs, - @Nullable Double maxBucketProcessingTimeMs, - @Nullable Double avgBucketProcessingTimeMs, - @Nullable Double exponentialAvgBucketProcessingTimeMs, - double incrementalBucketProcessingTimeMs) { - ExponentialAverageCalculationContext context = - new ExponentialAverageCalculationContext(incrementalBucketProcessingTimeMs, TIMESTAMP.plus(BUCKET_SPAN), null); + String jobId, + long bucketCount, + @Nullable Double minBucketProcessingTimeMs, + @Nullable Double maxBucketProcessingTimeMs, + @Nullable Double avgBucketProcessingTimeMs, + @Nullable Double exponentialAvgBucketProcessingTimeMs, + double incrementalBucketProcessingTimeMs + ) { + ExponentialAverageCalculationContext context = new ExponentialAverageCalculationContext( + incrementalBucketProcessingTimeMs, + TIMESTAMP.plus(BUCKET_SPAN), + null + ); return new TimingStats( jobId, bucketCount, @@ -169,7 +184,8 @@ private static TimingStats createTimingStats( maxBucketProcessingTimeMs, avgBucketProcessingTimeMs, exponentialAvgBucketProcessingTimeMs, - context); + context + ); } private static Bucket createBucket(long processingTimeMs) { diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/persistence/overallbuckets/OverallBucketsAggregatorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/persistence/overallbuckets/OverallBucketsAggregatorTests.java index ad39d1027b065..f690f7491511b 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/persistence/overallbuckets/OverallBucketsAggregatorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/persistence/overallbuckets/OverallBucketsAggregatorTests.java @@ -36,27 +36,69 @@ public void testProcess_GivenAggSpanIsTwiceTheBucketSpan() { List rawBuckets1 = new ArrayList<>(); List rawBuckets2 = new ArrayList<>(); - rawBuckets1.add(new OverallBucket(new Date(startTime), 3600L, 10.0, - Arrays.asList(new OverallBucket.JobInfo("job_1", 10.0), - new OverallBucket.JobInfo("job_2", 6.0)), - false)); - rawBuckets1.add(new OverallBucket(new Date(startTime + TimeValue.timeValueHours(1).millis()), 3600L, 20.0, + rawBuckets1.add( + new OverallBucket( + new Date(startTime), + 3600L, + 10.0, + Arrays.asList(new OverallBucket.JobInfo("job_1", 10.0), new OverallBucket.JobInfo("job_2", 6.0)), + false + ) + ); + rawBuckets1.add( + new OverallBucket( + new Date(startTime + TimeValue.timeValueHours(1).millis()), + 3600L, + 20.0, Arrays.asList(new JobInfo("job_1", 20.0), new JobInfo("job_2", 2.0)), - false)); - rawBuckets1.add(new OverallBucket(new Date(startTime + TimeValue.timeValueHours(2).millis()), 3600L, 30.0, + false + ) + ); + rawBuckets1.add( + new OverallBucket( + new Date(startTime + TimeValue.timeValueHours(2).millis()), + 3600L, + 30.0, Arrays.asList(new JobInfo("job_1", 30.0), new JobInfo("job_2", 7.0)), - false)); - rawBuckets1.add(new OverallBucket(new Date(startTime + TimeValue.timeValueHours(3).millis()), 3600L, 40.0, + false + ) + ); + rawBuckets1.add( + new OverallBucket( + new Date(startTime + TimeValue.timeValueHours(3).millis()), + 3600L, + 40.0, Arrays.asList(new JobInfo("job_1", 10.0), new JobInfo("job_2", 40.0)), - false)); - rawBuckets1.add(new OverallBucket(new Date(startTime + TimeValue.timeValueHours(4).millis()), 3600L, 50.0, - Collections.singletonList(new JobInfo("job_1", 50.0)), false)); - rawBuckets1.add(new OverallBucket(new Date(startTime + TimeValue.timeValueHours(5).millis()), 3600L, 60.0, - Collections.singletonList(new JobInfo("job_1", 60.0)), true)); - rawBuckets1.add(new OverallBucket(new Date(startTime + TimeValue.timeValueHours(6).millis()), 3600L, 70.0, + false + ) + ); + rawBuckets1.add( + new OverallBucket( + new Date(startTime + TimeValue.timeValueHours(4).millis()), + 3600L, + 50.0, + Collections.singletonList(new JobInfo("job_1", 50.0)), + false + ) + ); + rawBuckets1.add( + new OverallBucket( + new Date(startTime + TimeValue.timeValueHours(5).millis()), + 3600L, + 60.0, + Collections.singletonList(new JobInfo("job_1", 60.0)), + true + ) + ); + rawBuckets1.add( + new OverallBucket( + new Date(startTime + TimeValue.timeValueHours(6).millis()), + 3600L, + 70.0, Arrays.asList(new JobInfo("job_1", 70.0), new JobInfo("job_2", 0.0)), - true)); - + true + ) + ); TimeValue bucketSpan = TimeValue.timeValueHours(2); OverallBucketsAggregator aggregator = new OverallBucketsAggregator(bucketSpan); @@ -67,13 +109,11 @@ public void testProcess_GivenAggSpanIsTwiceTheBucketSpan() { assertThat(aggregated.size(), equalTo(4)); assertThat(aggregated.get(0).getTimestamp().getTime(), equalTo(startTime)); assertThat(aggregated.get(0).getOverallScore(), equalTo(20.0)); - assertThat(aggregated.get(0).getJobs(), contains(new JobInfo("job_1", 20.0), - new JobInfo("job_2", 6.0))); + assertThat(aggregated.get(0).getJobs(), contains(new JobInfo("job_1", 20.0), new JobInfo("job_2", 6.0))); assertThat(aggregated.get(0).isInterim(), is(false)); assertThat(aggregated.get(1).getTimestamp().getTime(), equalTo(startTime + bucketSpan.millis())); assertThat(aggregated.get(1).getOverallScore(), equalTo(40.0)); - assertThat(aggregated.get(1).getJobs(), contains(new JobInfo("job_1", 30.0), - new JobInfo("job_2", 40.0))); + assertThat(aggregated.get(1).getJobs(), contains(new JobInfo("job_1", 30.0), new JobInfo("job_2", 40.0))); assertThat(aggregated.get(1).isInterim(), is(false)); assertThat(aggregated.get(2).getTimestamp().getTime(), equalTo(startTime + 2 * bucketSpan.millis())); assertThat(aggregated.get(2).getOverallScore(), equalTo(60.0)); @@ -82,8 +122,7 @@ public void testProcess_GivenAggSpanIsTwiceTheBucketSpan() { assertThat(aggregated.get(2).isInterim(), is(true)); assertThat(aggregated.get(3).getTimestamp().getTime(), equalTo(startTime + 3 * bucketSpan.millis())); assertThat(aggregated.get(3).getOverallScore(), equalTo(70.0)); - assertThat(aggregated.get(3).getJobs(), contains(new JobInfo("job_1", 70.0), - new JobInfo("job_2", 0.0))); + assertThat(aggregated.get(3).getJobs(), contains(new JobInfo("job_1", 70.0), new JobInfo("job_2", 0.0))); assertThat(aggregated.get(3).isInterim(), is(true)); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/CountingInputStreamTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/CountingInputStreamTests.java index 86c3e35ac5a57..5013a3408afe3 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/CountingInputStreamTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/CountingInputStreamTests.java @@ -23,7 +23,8 @@ public void testRead_OneByteAtATime() throws IOException { InputStream source = new ByteArrayInputStream(TEXT.getBytes(StandardCharsets.UTF_8)); try (CountingInputStream counting = new CountingInputStream(source, dataCountsReporter)) { - while (counting.read() >= 0) {} + while (counting.read() >= 0) { + } assertEquals(TEXT.length(), dataCountsReporter.incrementalStats().getInputBytes()); } } @@ -37,7 +38,8 @@ public void testRead_WithBuffer() throws IOException { try (CountingInputStream counting = new CountingInputStream(source, dataCountsReporter)) { byte buf[] = new byte[256]; - while (counting.read(buf) >= 0) {} + while (counting.read(buf) >= 0) { + } assertEquals(TEXT.length(), dataCountsReporter.incrementalStats().getInputBytes()); } } @@ -51,7 +53,8 @@ public void testRead_WithTinyBuffer() throws IOException { try (CountingInputStream counting = new CountingInputStream(source, dataCountsReporter)) { byte buf[] = new byte[8]; - while (counting.read(buf, 0, 8) >= 0) {} + while (counting.read(buf, 0, 8) >= 0) { + } assertEquals(TEXT.length(), dataCountsReporter.incrementalStats().getInputBytes()); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/DataCountsReporterTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/DataCountsReporterTests.java index 6f33d0cfcf90b..3784e3ae56239 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/DataCountsReporterTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/DataCountsReporterTests.java @@ -12,8 +12,8 @@ import org.elasticsearch.xpack.core.ml.job.config.DataDescription; import org.elasticsearch.xpack.core.ml.job.config.Detector; import org.elasticsearch.xpack.core.ml.job.config.Job; -import org.elasticsearch.xpack.ml.job.persistence.JobDataCountsPersister; import org.elasticsearch.xpack.core.ml.job.process.autodetect.state.DataCounts; +import org.elasticsearch.xpack.ml.job.persistence.JobDataCountsPersister; import org.junit.Before; import org.mockito.Mockito; @@ -42,7 +42,6 @@ public void setUpMocks() { acBuilder.setLatency(TimeValue.ZERO); acBuilder.setDetectors(Arrays.asList(new Detector.Builder("metric", "field").build())); - Job.Builder builder = new Job.Builder("sr"); builder.setAnalysisConfig(acBuilder); builder.setDataDescription(new DataDescription.Builder()); @@ -59,8 +58,25 @@ public void testSimpleConstructor() { } public void testComplexConstructor() { - DataCounts counts = new DataCounts("foo", 1L, 1L, 2L, 0L, 3L, 4L, 5L, 6L, 7L, 8L, - new Date(), new Date(), new Date(), new Date(), new Date(), Instant.now()); + DataCounts counts = new DataCounts( + "foo", + 1L, + 1L, + 2L, + 0L, + 3L, + 4L, + 5L, + 6L, + 7L, + 8L, + new Date(), + new Date(), + new Date(), + new Date(), + new Date(), + Instant.now() + ); DataCountsReporter dataCountsReporter = new DataCountsReporter(job, counts, jobDataCountsPersister); DataCounts stats = dataCountsReporter.incrementalStats(); @@ -246,14 +262,30 @@ public void testReportRecordsWritten_Given2_000_000Records() { assertEquals(20, dataCountsReporter.getLogStatusCallCount()); } - public void testFinishReporting() { DataCountsReporter dataCountsReporter = new DataCountsReporter(job, new DataCounts(job.getId()), jobDataCountsPersister); dataCountsReporter.setAnalysedFieldsPerRecord(3); Date now = new Date(); - DataCounts dc = new DataCounts(job.getId(), 2L, 5L, 0L, 10L, 0L, 1L, 0L, 0L, 0L, 0L, new Date(2000), new Date(3000), - now, (Date) null, (Date) null, (Instant) null); + DataCounts dc = new DataCounts( + job.getId(), + 2L, + 5L, + 0L, + 10L, + 0L, + 1L, + 0L, + 0L, + 0L, + 0L, + new Date(2000), + new Date(3000), + now, + (Date) null, + (Date) null, + (Instant) null + ); dataCountsReporter.reportRecordWritten(5, 2000, 2000); dataCountsReporter.reportRecordWritten(5, 3000, 3000); dataCountsReporter.reportMissingField(); @@ -261,10 +293,11 @@ public void testFinishReporting() { long lastReportedTimeMs = dataCountsReporter.incrementalStats().getLastDataTimeStamp().getTime(); // check last data time is equal to now give or take a second - assertTrue(lastReportedTimeMs >= now.getTime() - && lastReportedTimeMs <= now.getTime() + TimeUnit.SECONDS.toMillis(1)); - assertEquals(dataCountsReporter.incrementalStats().getLastDataTimeStamp(), - dataCountsReporter.runningTotalStats().getLastDataTimeStamp()); + assertTrue(lastReportedTimeMs >= now.getTime() && lastReportedTimeMs <= now.getTime() + TimeUnit.SECONDS.toMillis(1)); + assertEquals( + dataCountsReporter.incrementalStats().getLastDataTimeStamp(), + dataCountsReporter.runningTotalStats().getLastDataTimeStamp() + ); dc.setLastDataTimeStamp(dataCountsReporter.incrementalStats().getLastDataTimeStamp()); verify(jobDataCountsPersister, times(1)).persistDataCounts(eq("sr"), eq(dc)); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/DummyDataCountsReporter.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/DummyDataCountsReporter.java index f275e8ac5dc57..5143eaedefba1 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/DummyDataCountsReporter.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/DummyDataCountsReporter.java @@ -11,8 +11,8 @@ import org.elasticsearch.xpack.core.ml.job.config.DataDescription; import org.elasticsearch.xpack.core.ml.job.config.Detector; import org.elasticsearch.xpack.core.ml.job.config.Job; -import org.elasticsearch.xpack.ml.job.persistence.JobDataCountsPersister; import org.elasticsearch.xpack.core.ml.job.process.autodetect.state.DataCounts; +import org.elasticsearch.xpack.ml.job.persistence.JobDataCountsPersister; import java.util.Arrays; import java.util.Date; @@ -53,8 +53,7 @@ public int getLogStatusCallCount() { } private static Job createJob() { - AnalysisConfig.Builder acBuilder = new AnalysisConfig.Builder( - Arrays.asList(new Detector.Builder("metric", "field").build())); + AnalysisConfig.Builder acBuilder = new AnalysisConfig.Builder(Arrays.asList(new Detector.Builder("metric", "field").build())); acBuilder.setBucketSpan(TimeValue.timeValueSeconds(300)); acBuilder.setLatency(TimeValue.ZERO); acBuilder.setDetectors(Arrays.asList(new Detector.Builder("metric", "field").build())); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/ProcessWorkerExecutorServiceTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/ProcessWorkerExecutorServiceTests.java index c9455a6f8db0a..bf6807b43274d 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/ProcessWorkerExecutorServiceTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/ProcessWorkerExecutorServiceTests.java @@ -51,8 +51,7 @@ public void testAutodetectWorkerExecutorService_TasksNotExecutedCallHandlerOnShu executor.execute(() -> { try { latch.await(); - } catch (InterruptedException e) { - } + } catch (InterruptedException e) {} }); AtomicBoolean runnableShouldNotBeCalled = new AtomicBoolean(false); @@ -60,7 +59,7 @@ public void testAutodetectWorkerExecutorService_TasksNotExecutedCallHandlerOnShu AtomicInteger onFailureCallCount = new AtomicInteger(); AtomicInteger doRunCallCount = new AtomicInteger(); - for (int i=0; i<2; i++) { + for (int i = 0; i < 2; i++) { executor.execute(new AbstractRunnable() { @Override public void onFailure(Exception e) { @@ -88,13 +87,9 @@ protected void doRun() { public void testAutodetectWorkerExecutorServiceDoesNotSwallowErrors() { ProcessWorkerExecutorService executor = createExecutorService(); if (randomBoolean()) { - executor.submit(() -> { - throw new Error("future error"); - }); + executor.submit(() -> { throw new Error("future error"); }); } else { - executor.execute(() -> { - throw new Error("future error"); - }); + executor.execute(() -> { throw new Error("future error"); }); } Error e = expectThrows(Error.class, () -> executor.start()); assertThat(e.getMessage(), containsString("future error")); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/AutodetectBuilderTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/AutodetectBuilderTests.java index f18cb56f52e51..75e1d69a5ff8f 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/AutodetectBuilderTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/AutodetectBuilderTests.java @@ -50,7 +50,7 @@ public class AutodetectBuilderTests extends ESTestCase { public void setUpTests() { logger = mock(Logger.class); filesToDelete = new ArrayList<>(); - commandCaptor = ArgumentCaptor.forClass((Class)List.class); + commandCaptor = ArgumentCaptor.forClass((Class) List.class); settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build(); env = TestEnvironment.newEnvironment(settings); nativeController = mock(NativeController.class); @@ -76,7 +76,8 @@ public void testBuildAutodetectCommand() { acBuilder.setCategorizationFieldName("bar"); } acBuilder.setPerPartitionCategorizationConfig( - new PerPartitionCategorizationConfig(isPerPartitionCategorization, isPerPartitionCategorization)); + new PerPartitionCategorizationConfig(isPerPartitionCategorization, isPerPartitionCategorization) + ); job.setAnalysisConfig(acBuilder); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/AutodetectCommunicatorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/AutodetectCommunicatorTests.java index 9588e031de9ab..7aee18aeb4ca5 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/AutodetectCommunicatorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/AutodetectCommunicatorTests.java @@ -8,12 +8,12 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.env.Environment; import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.index.analysis.AnalysisRegistry; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.ml.calendars.ScheduledEvent; import org.elasticsearch.xpack.core.ml.calendars.ScheduledEventTests; import org.elasticsearch.xpack.core.ml.job.config.AnalysisConfig; @@ -49,13 +49,13 @@ import java.util.concurrent.atomic.AtomicReference; import java.util.function.BiConsumer; -import static org.mockito.ArgumentMatchers.nullable; -import static org.mockito.Mockito.doAnswer; import static org.hamcrest.Matchers.equalTo; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.nullable; +import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; @@ -80,8 +80,13 @@ public void testWriteResetBucketsControlMessage() throws IOException { DataLoadParams params = new DataLoadParams(TimeRange.builder().startTime("1").endTime("2").build(), Optional.empty()); AutodetectProcess process = mockAutodetectProcessWithOutputStream(); try (AutodetectCommunicator communicator = createAutodetectCommunicator(process, mock(AutodetectResultProcessor.class))) { - communicator.writeToJob(new ByteArrayInputStream(new byte[0]), analysisRegistry, - randomFrom(XContentType.values()), params, (dataCounts, e) -> {}); + communicator.writeToJob( + new ByteArrayInputStream(new byte[0]), + analysisRegistry, + randomFrom(XContentType.values()), + params, + (dataCounts, e) -> {} + ); verify(process).writeResetBucketsControlMessage(params); } } @@ -93,10 +98,10 @@ public void testWriteUpdateProcessMessage() throws IOException { DetectionRule updatedRule = new DetectionRule.Builder(RuleScope.builder().exclude("foo", "bar")).build(); List detectorUpdates = Collections.singletonList( - new JobUpdate.DetectorUpdate(0, "updated description", Collections.singletonList(updatedRule))); + new JobUpdate.DetectorUpdate(0, "updated description", Collections.singletonList(updatedRule)) + ); - List events = Collections.singletonList( - ScheduledEventTests.createScheduledEvent(randomAlphaOfLength(10))); + List events = Collections.singletonList(ScheduledEventTests.createScheduledEvent(randomAlphaOfLength(10))); UpdateProcessMessage.Builder updateProcessMessage = new UpdateProcessMessage.Builder().setDetectorUpdates(detectorUpdates); updateProcessMessage.setScheduledEvents(events); @@ -147,8 +152,8 @@ public void testFlushJob_givenFlushWaitReturnsTrueOnSecondCall() throws Exceptio AutodetectProcess process = mockAutodetectProcessWithOutputStream(); AutodetectResultProcessor autodetectResultProcessor = mock(AutodetectResultProcessor.class); FlushAcknowledgement flushAcknowledgement = mock(FlushAcknowledgement.class); - when(autodetectResultProcessor.waitForFlushAcknowledgement(nullable(String.class), eq(Duration.ofSeconds(1)))) - .thenReturn(null).thenReturn(flushAcknowledgement); + when(autodetectResultProcessor.waitForFlushAcknowledgement(nullable(String.class), eq(Duration.ofSeconds(1)))).thenReturn(null) + .thenReturn(flushAcknowledgement); FlushJobParams params = FlushJobParams.builder().build(); try (AutodetectCommunicator communicator = createAutodetectCommunicator(process, autodetectResultProcessor)) { @@ -190,8 +195,12 @@ public void testKill() throws IOException, TimeoutException { ExecutorService executorService = mock(ExecutorService.class); AtomicBoolean finishCalled = new AtomicBoolean(false); - AutodetectCommunicator communicator = createAutodetectCommunicator(executorService, process, resultProcessor, - (e, b) -> finishCalled.set(true)); + AutodetectCommunicator communicator = createAutodetectCommunicator( + executorService, + process, + resultProcessor, + (e, b) -> finishCalled.set(true) + ); boolean awaitCompletion = randomBoolean(); boolean finish = randomBoolean(); communicator.killProcess(awaitCompletion, finish); @@ -229,19 +238,31 @@ private AutodetectProcess mockAutodetectProcessWithOutputStream() throws IOExcep } @SuppressWarnings("unchecked") - private AutodetectCommunicator createAutodetectCommunicator(ExecutorService executorService, AutodetectProcess autodetectProcess, - AutodetectResultProcessor autodetectResultProcessor, - BiConsumer finishHandler) throws IOException { + private AutodetectCommunicator createAutodetectCommunicator( + ExecutorService executorService, + AutodetectProcess autodetectProcess, + AutodetectResultProcessor autodetectResultProcessor, + BiConsumer finishHandler + ) throws IOException { DataCountsReporter dataCountsReporter = mock(DataCountsReporter.class); doNothing().when(dataCountsReporter).finishReporting(); - return new AutodetectCommunicator(createJobDetails(), autodetectProcess, - stateStreamer, dataCountsReporter, autodetectResultProcessor, finishHandler, - new NamedXContentRegistry(Collections.emptyList()), executorService); + return new AutodetectCommunicator( + createJobDetails(), + autodetectProcess, + stateStreamer, + dataCountsReporter, + autodetectResultProcessor, + finishHandler, + new NamedXContentRegistry(Collections.emptyList()), + executorService + ); } @SuppressWarnings("unchecked") - private AutodetectCommunicator createAutodetectCommunicator(AutodetectProcess autodetectProcess, - AutodetectResultProcessor autodetectResultProcessor) throws IOException { + private AutodetectCommunicator createAutodetectCommunicator( + AutodetectProcess autodetectProcess, + AutodetectResultProcessor autodetectResultProcessor + ) throws IOException { ExecutorService executorService = mock(ExecutorService.class); doAnswer(invocationOnMock -> { Callable runnable = (Callable) invocationOnMock.getArguments()[0]; diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/AutodetectProcessManagerTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/AutodetectProcessManagerTests.java index 101d754c22a82..291f3aad0fc51 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/AutodetectProcessManagerTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/AutodetectProcessManagerTests.java @@ -18,16 +18,14 @@ import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.core.CheckedConsumer; import org.elasticsearch.common.collect.ImmutableOpenMap; import org.elasticsearch.common.settings.ClusterSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.EsExecutors; import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.CheckedConsumer; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.env.Environment; import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.index.analysis.AnalysisRegistry; @@ -38,6 +36,8 @@ import org.elasticsearch.tasks.TaskManager; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.ml.annotations.AnnotationIndex; import org.elasticsearch.xpack.core.ml.job.config.AnalysisConfig; import org.elasticsearch.xpack.core.ml.job.config.DataDescription; @@ -102,13 +102,6 @@ import static org.elasticsearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_REPLICAS; import static org.elasticsearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_SHARDS; import static org.elasticsearch.cluster.metadata.IndexMetadata.SETTING_VERSION_CREATED; -import static org.mockito.Mockito.doAnswer; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; -import static org.mockito.Mockito.when; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; @@ -118,9 +111,16 @@ import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.same; +import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.doCallRealMethod; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.when; /** * Calling the @@ -200,39 +200,45 @@ public void setup() throws Exception { normalizerFactory = mock(NormalizerFactory.class); auditor = mock(AnomalyDetectionAuditor.class); clusterService = mock(ClusterService.class); - ClusterSettings clusterSettings = - new ClusterSettings(Settings.EMPTY, - new HashSet<>(Arrays.asList(MachineLearning.MAX_OPEN_JOBS_PER_NODE, - ResultsPersisterService.PERSIST_RESULTS_MAX_RETRIES))); + ClusterSettings clusterSettings = new ClusterSettings( + Settings.EMPTY, + new HashSet<>(Arrays.asList(MachineLearning.MAX_OPEN_JOBS_PER_NODE, ResultsPersisterService.PERSIST_RESULTS_MAX_RETRIES)) + ); when(clusterService.getClusterSettings()).thenReturn(clusterSettings); Metadata metadata = Metadata.builder() - .indices(ImmutableOpenMap.builder() - .fPut( - AnomalyDetectorsIndexFields.STATE_INDEX_PREFIX + "-000001", - IndexMetadata.builder(AnomalyDetectorsIndexFields.STATE_INDEX_PREFIX + "-000001") - .settings( - Settings.builder() - .put(SETTING_NUMBER_OF_SHARDS, 1) - .put(SETTING_NUMBER_OF_REPLICAS, 0) - .put(SETTING_INDEX_HIDDEN, true) - .put(SETTING_VERSION_CREATED, Version.CURRENT) - .build()) - .putAlias(AliasMetadata.builder(AnomalyDetectorsIndex.jobStateIndexWriteAlias()).isHidden(true).build()) - .build()) - .fPut( - AnnotationIndex.LATEST_INDEX_NAME, - IndexMetadata.builder(AnnotationIndex.LATEST_INDEX_NAME) - .settings( - Settings.builder() - .put(SETTING_NUMBER_OF_SHARDS, 1) - .put(SETTING_NUMBER_OF_REPLICAS, 0) - .put(SETTING_INDEX_HIDDEN, true) - .put(SETTING_VERSION_CREATED, Version.CURRENT) - .build()) - .putAlias(AliasMetadata.builder(AnnotationIndex.READ_ALIAS_NAME).isHidden(true).build()) - .putAlias(AliasMetadata.builder(AnnotationIndex.WRITE_ALIAS_NAME).isHidden(true).build()) - .build()) - .build()) + .indices( + ImmutableOpenMap.builder() + .fPut( + AnomalyDetectorsIndexFields.STATE_INDEX_PREFIX + "-000001", + IndexMetadata.builder(AnomalyDetectorsIndexFields.STATE_INDEX_PREFIX + "-000001") + .settings( + Settings.builder() + .put(SETTING_NUMBER_OF_SHARDS, 1) + .put(SETTING_NUMBER_OF_REPLICAS, 0) + .put(SETTING_INDEX_HIDDEN, true) + .put(SETTING_VERSION_CREATED, Version.CURRENT) + .build() + ) + .putAlias(AliasMetadata.builder(AnomalyDetectorsIndex.jobStateIndexWriteAlias()).isHidden(true).build()) + .build() + ) + .fPut( + AnnotationIndex.LATEST_INDEX_NAME, + IndexMetadata.builder(AnnotationIndex.LATEST_INDEX_NAME) + .settings( + Settings.builder() + .put(SETTING_NUMBER_OF_SHARDS, 1) + .put(SETTING_NUMBER_OF_REPLICAS, 0) + .put(SETTING_INDEX_HIDDEN, true) + .put(SETTING_VERSION_CREATED, Version.CURRENT) + .build() + ) + .putAlias(AliasMetadata.builder(AnnotationIndex.READ_ALIAS_NAME).isHidden(true).build()) + .putAlias(AliasMetadata.builder(AnnotationIndex.WRITE_ALIAS_NAME).isHidden(true).build()) + .build() + ) + .build() + ) .build(); clusterState = mock(ClusterState.class); when(clusterState.getMetadata()).thenReturn(metadata); @@ -297,7 +303,7 @@ public void testOpenJob_withoutVersion() { @SuppressWarnings("unchecked") public void testOpenJob_exceedMaxNumJobs() { - for (String jobId : new String [] {"foo", "bar", "baz", "foobar"}) { + for (String jobId : new String[] { "foo", "bar", "baz", "foobar" }) { doAnswer(invocationOnMock -> { @SuppressWarnings("unchecked") ActionListener listener = (ActionListener) invocationOnMock.getArguments()[1]; @@ -360,7 +366,7 @@ public void testOpenJob_exceedMaxNumJobs() { assertEquals(3, manager.numberOfOpenJobs()); } - public void testProcessData() { + public void testProcessData() { AutodetectProcessManager manager = createSpyManager(); assertEquals(0, manager.numberOfOpenJobs()); @@ -368,8 +374,14 @@ public void testProcessData() { when(jobTask.getJobId()).thenReturn("foo"); DataLoadParams params = new DataLoadParams(TimeRange.builder().build(), Optional.empty()); manager.openJob(jobTask, clusterState, DEFAULT_MASTER_NODE_TIMEOUT, (e, b) -> {}); - manager.processData(jobTask, analysisRegistry, createInputStream(""), randomFrom(XContentType.values()), - params, (dataCounts1, e) -> {}); + manager.processData( + jobTask, + analysisRegistry, + createInputStream(""), + randomFrom(XContentType.values()), + params, + (dataCounts1, e) -> {} + ); assertEquals(1, manager.numberOfOpenJobs()); } @@ -386,7 +398,6 @@ public void testProcessDataThrowsElasticsearchStatusException_onIoException() { return null; }).when(autodetectCommunicator).writeToJob(eq(inputStream), same(analysisRegistry), same(xContentType), eq(params), any()); - JobTask jobTask = mock(JobTask.class); when(jobTask.getJobId()).thenReturn("foo"); manager.openJob(jobTask, clusterState, DEFAULT_MASTER_NODE_TIMEOUT, (e, b) -> {}); @@ -402,8 +413,14 @@ public void testCloseJob() { JobTask jobTask = mock(JobTask.class); when(jobTask.getJobId()).thenReturn("foo"); manager.openJob(jobTask, clusterState, DEFAULT_MASTER_NODE_TIMEOUT, (e, b) -> {}); - manager.processData(jobTask, analysisRegistry, createInputStream(""), randomFrom(XContentType.values()), - mock(DataLoadParams.class), (dataCounts1, e) -> {}); + manager.processData( + jobTask, + analysisRegistry, + createInputStream(""), + randomFrom(XContentType.values()), + mock(DataLoadParams.class), + (dataCounts1, e) -> {} + ); // job is created assertEquals(1, manager.numberOfOpenJobs()); @@ -426,8 +443,14 @@ public void testCanCloseClosingJob() throws Exception { JobTask jobTask = mock(JobTask.class); when(jobTask.getJobId()).thenReturn("foo"); manager.openJob(jobTask, clusterState, DEFAULT_MASTER_NODE_TIMEOUT, (e, b) -> {}); - manager.processData(jobTask, analysisRegistry, createInputStream(""), randomFrom(XContentType.values()), - mock(DataLoadParams.class), (dataCounts1, e) -> {}); + manager.processData( + jobTask, + analysisRegistry, + createInputStream(""), + randomFrom(XContentType.values()), + mock(DataLoadParams.class), + (dataCounts1, e) -> {} + ); assertEquals(1, manager.numberOfOpenJobs()); @@ -439,10 +462,10 @@ public void testCanCloseClosingJob() throws Exception { // Also close the job in the current thread, so that we have two simultaneous close requests manager.closeJob(jobTask, "in main test thread"); - // The 10 second timeout here is usually far in excess of what is required. In the vast - // majority of cases the other thread will exit within a few milliseconds. However, it + // The 10 second timeout here is usually far in excess of what is required. In the vast + // majority of cases the other thread will exit within a few milliseconds. However, it // has been observed that on some VMs the test can fail because the VM stalls at the - // wrong moment. A 10 second timeout is on a par with the length of time assertBusy() + // wrong moment. A 10 second timeout is on a par with the length of time assertBusy() // would wait under these circumstances. closeThread.join(10000); assertFalse(closeThread.isAlive()); @@ -473,8 +496,14 @@ public void testCanKillClosingJob() throws Exception { JobTask jobTask = mock(JobTask.class); when(jobTask.getJobId()).thenReturn("foo"); manager.openJob(jobTask, clusterState, DEFAULT_MASTER_NODE_TIMEOUT, (e, b) -> {}); - manager.processData(jobTask, analysisRegistry, createInputStream(""), randomFrom(XContentType.values()), - mock(DataLoadParams.class), (dataCounts1, e) -> {}); + manager.processData( + jobTask, + analysisRegistry, + createInputStream(""), + randomFrom(XContentType.values()), + mock(DataLoadParams.class), + (dataCounts1, e) -> {} + ); // Close the job in a separate thread so that it can simulate taking a long time to close Thread closeThread = new Thread(() -> manager.closeJob(jobTask, null)); @@ -512,8 +541,14 @@ public void testFlush() { when(jobTask.getJobId()).thenReturn("foo"); InputStream inputStream = createInputStream(""); manager.openJob(jobTask, clusterState, DEFAULT_MASTER_NODE_TIMEOUT, (e, b) -> {}); - manager.processData(jobTask, analysisRegistry, inputStream, randomFrom(XContentType.values()), - mock(DataLoadParams.class), (dataCounts1, e) -> {}); + manager.processData( + jobTask, + analysisRegistry, + inputStream, + randomFrom(XContentType.values()), + mock(DataLoadParams.class), + (dataCounts1, e) -> {} + ); FlushJobParams params = FlushJobParams.builder().build(); manager.flushJob(jobTask, params, ActionListener.wrap(flushAcknowledgement -> {}, e -> fail(e.getMessage()))); @@ -550,9 +585,14 @@ public void testCloseThrows() { JobTask jobTask = mock(JobTask.class); when(jobTask.getJobId()).thenReturn("foo"); manager.openJob(jobTask, clusterState, DEFAULT_MASTER_NODE_TIMEOUT, (e, b) -> {}); - manager.processData(jobTask, analysisRegistry, createInputStream(""), randomFrom(XContentType.values()), mock(DataLoadParams.class), - (dataCounts1, e) -> { - }); + manager.processData( + jobTask, + analysisRegistry, + createInputStream(""), + randomFrom(XContentType.values()), + mock(DataLoadParams.class), + (dataCounts1, e) -> {} + ); verify(manager).setJobState(any(), eq(JobState.OPENED), any(), any()); // job is created assertEquals(1, manager.numberOfOpenJobs()); @@ -588,8 +628,14 @@ public void testJobHasActiveAutodetectProcess() { assertFalse(manager.jobHasActiveAutodetectProcess(jobTask)); manager.openJob(jobTask, clusterState, DEFAULT_MASTER_NODE_TIMEOUT, (e, b) -> {}); - manager.processData(jobTask, analysisRegistry, createInputStream(""), randomFrom(XContentType.values()), - mock(DataLoadParams.class), (dataCounts1, e) -> {}); + manager.processData( + jobTask, + analysisRegistry, + createInputStream(""), + randomFrom(XContentType.values()), + mock(DataLoadParams.class), + (dataCounts1, e) -> {} + ); assertTrue(manager.jobHasActiveAutodetectProcess(jobTask)); jobTask = mock(JobTask.class); @@ -605,8 +651,14 @@ public void testKillKillsAutodetectProcess() throws IOException { assertFalse(manager.jobHasActiveAutodetectProcess(jobTask)); manager.openJob(jobTask, clusterState, DEFAULT_MASTER_NODE_TIMEOUT, (e, b) -> {}); - manager.processData(jobTask, analysisRegistry, createInputStream(""), randomFrom(XContentType.values()), - mock(DataLoadParams.class), (dataCounts1, e) -> {}); + manager.processData( + jobTask, + analysisRegistry, + createInputStream(""), + randomFrom(XContentType.values()), + mock(DataLoadParams.class), + (dataCounts1, e) -> {} + ); assertTrue(manager.jobHasActiveAutodetectProcess(jobTask)); @@ -646,8 +698,14 @@ public void testProcessData_GivenStateNotOpened() { manager.openJob(jobTask, clusterState, DEFAULT_MASTER_NODE_TIMEOUT, (e, b) -> {}); InputStream inputStream = createInputStream(""); DataCounts[] dataCounts = new DataCounts[1]; - manager.processData(jobTask, analysisRegistry, inputStream, - randomFrom(XContentType.values()), mock(DataLoadParams.class), (dataCounts1, e) -> dataCounts[0] = dataCounts1); + manager.processData( + jobTask, + analysisRegistry, + inputStream, + randomFrom(XContentType.values()), + mock(DataLoadParams.class), + (dataCounts1, e) -> dataCounts[0] = dataCounts1 + ); assertThat(dataCounts[0], equalTo(new DataCounts("foo"))); } @@ -673,8 +731,7 @@ public void testCreate_notEnoughThreads() throws IOException { JobTask jobTask = mock(JobTask.class); when(jobTask.getJobId()).thenReturn("my_id"); - expectThrows(EsRejectedExecutionException.class, - () -> manager.create(jobTask, job, buildAutodetectParams(), (e, b) -> {})); + expectThrows(EsRejectedExecutionException.class, () -> manager.create(jobTask, job, buildAutodetectParams(), (e, b) -> {})); verify(autodetectProcess, times(1)).close(); } @@ -692,8 +749,7 @@ public void testCreate_givenFirstTime() { } public void testCreate_givenExistingModelSnapshot() { - modelSnapshot = new ModelSnapshot.Builder("foo").setSnapshotId("snapshot-1") - .setLatestRecordTimeStamp(new Date(0L)).build(); + modelSnapshot = new ModelSnapshot.Builder("foo").setSnapshotId("snapshot-1").setLatestRecordTimeStamp(new Date(0L)).build(); dataCounts = new DataCounts("foo"); dataCounts.setLatestRecordTimeStamp(new Date(1L)); AutodetectProcessManager manager = createNonSpyManager("foo"); @@ -702,9 +758,9 @@ public void testCreate_givenExistingModelSnapshot() { when(jobTask.getJobId()).thenReturn("foo"); manager.create(jobTask, createJobDetails("foo"), buildAutodetectParams(), (e, b) -> {}); - String expectedNotification = "Loading model snapshot [snapshot-1] with " + - "latest_record_timestamp [1970-01-01T00:00:00.000Z], " + - "job latest_record_timestamp [1970-01-01T00:00:00.001Z]"; + String expectedNotification = "Loading model snapshot [snapshot-1] with " + + "latest_record_timestamp [1970-01-01T00:00:00.000Z], " + + "job latest_record_timestamp [1970-01-01T00:00:00.001Z]"; verify(auditor).info("foo", expectedNotification); verifyNoMoreInteractions(auditor); } @@ -721,8 +777,7 @@ public void testCreate_givenNonZeroCountsAndNoModelSnapshotNorQuantiles() { when(jobTask.getJobId()).thenReturn("foo"); manager.create(jobTask, createJobDetails("foo"), buildAutodetectParams(), (e, b) -> {}); - String expectedNotification = "Loading model snapshot [N/A], " + - "job latest_record_timestamp [1970-01-01T00:00:00.000Z]"; + String expectedNotification = "Loading model snapshot [N/A], " + "job latest_record_timestamp [1970-01-01T00:00:00.000Z]"; verify(auditor).info("foo", expectedNotification); verify(auditor).warning("foo", "No model snapshot could be found for a job with processed records"); verify(auditor).warning("foo", "No quantiles could be found for a job with processed records"); @@ -746,12 +801,11 @@ private AutodetectProcessManager createNonSpyManager(String jobId) { } private AutodetectParams buildAutodetectParams() { - return new AutodetectParams.Builder("foo") - .setDataCounts(dataCounts) - .setModelSizeStats(modelSizeStats) - .setModelSnapshot(modelSnapshot) - .setQuantiles(quantiles) - .build(); + return new AutodetectParams.Builder("foo").setDataCounts(dataCounts) + .setModelSizeStats(modelSizeStats) + .setModelSnapshot(modelSnapshot) + .setQuantiles(quantiles) + .build(); } private AutodetectProcessManager createSpyManager() { @@ -766,18 +820,38 @@ private AutodetectProcessManager createSpyManager(Settings settings) { } private AutodetectProcessManager createManager(Settings settings) { - return new AutodetectProcessManager(settings, - client, threadPool, new NamedXContentRegistry(Collections.emptyList()), auditor, clusterService, jobManager, jobResultsProvider, - jobResultsPersister, jobDataCountsPersister, annotationPersister, autodetectFactory, normalizerFactory, nativeStorageProvider, - TestIndexNameExpressionResolver.newInstance()); + return new AutodetectProcessManager( + settings, + client, + threadPool, + new NamedXContentRegistry(Collections.emptyList()), + auditor, + clusterService, + jobManager, + jobResultsProvider, + jobResultsPersister, + jobDataCountsPersister, + annotationPersister, + autodetectFactory, + normalizerFactory, + nativeStorageProvider, + TestIndexNameExpressionResolver.newInstance() + ); } + private AutodetectProcessManager createSpyManagerAndCallProcessData(String jobId) { AutodetectProcessManager manager = createSpyManager(); JobTask jobTask = mock(JobTask.class); when(jobTask.getJobId()).thenReturn(jobId); manager.openJob(jobTask, clusterState, DEFAULT_MASTER_NODE_TIMEOUT, (e, b) -> {}); - manager.processData(jobTask, analysisRegistry, createInputStream(""), randomFrom(XContentType.values()), - mock(DataLoadParams.class), (dataCounts, e) -> {}); + manager.processData( + jobTask, + analysisRegistry, + createInputStream(""), + randomFrom(XContentType.values()), + mock(DataLoadParams.class), + (dataCounts, e) -> {} + ); return manager; } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/BlackHoleAutodetectProcessTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/BlackHoleAutodetectProcessTests.java index cfe1731d1c5f0..f11cabc6b3918 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/BlackHoleAutodetectProcessTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/BlackHoleAutodetectProcessTests.java @@ -34,7 +34,7 @@ public void testSimulatedFailure() throws Exception { AtomicReference failureReason = new AtomicReference<>(); try (BlackHoleAutodetectProcess process = new BlackHoleAutodetectProcess("foo", failureReason::set)) { Iterator iterator = process.readAutodetectResults(); - process.writeRecord(new String[] { BlackHoleAutodetectProcess.MAGIC_FAILURE_VALUE}); + process.writeRecord(new String[] { BlackHoleAutodetectProcess.MAGIC_FAILURE_VALUE }); assertFalse(process.isProcessAlive()); assertTrue(iterator.hasNext()); AutodetectResult result = iterator.next(); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/NativeAutodetectProcessTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/NativeAutodetectProcessTests.java index 6cf2f7ce64945..ad53c34ab7933 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/NativeAutodetectProcessTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/NativeAutodetectProcessTests.java @@ -6,18 +6,18 @@ */ package org.elasticsearch.xpack.ml.job.process.autodetect; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.ml.job.config.ModelPlotConfig; -import org.elasticsearch.xpack.ml.process.IndexingStateProcessor; import org.elasticsearch.xpack.ml.job.process.autodetect.params.DataLoadParams; import org.elasticsearch.xpack.ml.job.process.autodetect.params.FlushJobParams; import org.elasticsearch.xpack.ml.job.process.autodetect.params.TimeRange; import org.elasticsearch.xpack.ml.job.process.autodetect.writer.AutodetectControlMsgWriter; import org.elasticsearch.xpack.ml.job.results.AutodetectResult; +import org.elasticsearch.xpack.ml.process.IndexingStateProcessor; +import org.elasticsearch.xpack.ml.process.NativeController; import org.elasticsearch.xpack.ml.process.ProcessPipes; import org.elasticsearch.xpack.ml.process.ProcessResultsParser; -import org.elasticsearch.xpack.ml.process.NativeController; import org.elasticsearch.xpack.ml.process.logging.CppLogMessageHandler; import org.junit.Assert; import org.junit.Before; @@ -79,9 +79,17 @@ public void initialize() { @SuppressWarnings("unchecked") public void testProcessStartTime() throws Exception { - try (NativeAutodetectProcess process = new NativeAutodetectProcess("foo", mock(NativeController.class), - processPipes, NUMBER_FIELDS, null, - new ProcessResultsParser<>(AutodetectResult.PARSER, NamedXContentRegistry.EMPTY), mock(Consumer.class))) { + try ( + NativeAutodetectProcess process = new NativeAutodetectProcess( + "foo", + mock(NativeController.class), + processPipes, + NUMBER_FIELDS, + null, + new ProcessResultsParser<>(AutodetectResult.PARSER, NamedXContentRegistry.EMPTY), + mock(Consumer.class) + ) + ) { process.start(executorService, mock(IndexingStateProcessor.class)); ZonedDateTime startTime = process.getProcessStartTime(); @@ -96,10 +104,18 @@ public void testProcessStartTime() throws Exception { @SuppressWarnings("unchecked") public void testWriteRecord() throws IOException { - String[] record = {"r1", "r2", "r3", "r4", "r5"}; - try (NativeAutodetectProcess process = new NativeAutodetectProcess("foo", mock(NativeController.class), - processPipes, NUMBER_FIELDS, Collections.emptyList(), - new ProcessResultsParser<>(AutodetectResult.PARSER, NamedXContentRegistry.EMPTY), mock(Consumer.class))) { + String[] record = { "r1", "r2", "r3", "r4", "r5" }; + try ( + NativeAutodetectProcess process = new NativeAutodetectProcess( + "foo", + mock(NativeController.class), + processPipes, + NUMBER_FIELDS, + Collections.emptyList(), + new ProcessResultsParser<>(AutodetectResult.PARSER, NamedXContentRegistry.EMPTY), + mock(Consumer.class) + ) + ) { process.start(executorService, mock(IndexingStateProcessor.class)); process.writeRecord(record); @@ -127,9 +143,17 @@ public void testWriteRecord() throws IOException { @SuppressWarnings("unchecked") public void testFlush() throws IOException { - try (NativeAutodetectProcess process = new NativeAutodetectProcess("foo", mock(NativeController.class), - processPipes, NUMBER_FIELDS, Collections.emptyList(), - new ProcessResultsParser<>(AutodetectResult.PARSER, NamedXContentRegistry.EMPTY), mock(Consumer.class))) { + try ( + NativeAutodetectProcess process = new NativeAutodetectProcess( + "foo", + mock(NativeController.class), + processPipes, + NUMBER_FIELDS, + Collections.emptyList(), + new ProcessResultsParser<>(AutodetectResult.PARSER, NamedXContentRegistry.EMPTY), + mock(Consumer.class) + ) + ) { process.start(executorService, mock(IndexingStateProcessor.class)); FlushJobParams params = FlushJobParams.builder().build(); @@ -156,9 +180,17 @@ public void testPersistJob() throws IOException { @SuppressWarnings("unchecked") public void testConsumeAndCloseOutputStream() throws IOException { - try (NativeAutodetectProcess process = new NativeAutodetectProcess("foo", mock(NativeController.class), - processPipes, NUMBER_FIELDS, Collections.emptyList(), - new ProcessResultsParser<>(AutodetectResult.PARSER, NamedXContentRegistry.EMPTY), mock(Consumer.class))) { + try ( + NativeAutodetectProcess process = new NativeAutodetectProcess( + "foo", + mock(NativeController.class), + processPipes, + NUMBER_FIELDS, + Collections.emptyList(), + new ProcessResultsParser<>(AutodetectResult.PARSER, NamedXContentRegistry.EMPTY), + mock(Consumer.class) + ) + ) { process.start(executorService); process.consumeAndCloseOutputStream(); @@ -168,9 +200,17 @@ public void testConsumeAndCloseOutputStream() throws IOException { @SuppressWarnings("unchecked") private void testWriteMessage(CheckedConsumer writeFunction, String expectedMessageCode) throws IOException { - try (NativeAutodetectProcess process = new NativeAutodetectProcess("foo", mock(NativeController.class), - processPipes, NUMBER_FIELDS, Collections.emptyList(), - new ProcessResultsParser<>(AutodetectResult.PARSER, NamedXContentRegistry.EMPTY), mock(Consumer.class))) { + try ( + NativeAutodetectProcess process = new NativeAutodetectProcess( + "foo", + mock(NativeController.class), + processPipes, + NUMBER_FIELDS, + Collections.emptyList(), + new ProcessResultsParser<>(AutodetectResult.PARSER, NamedXContentRegistry.EMPTY), + mock(Consumer.class) + ) + ) { process.start(executorService, mock(IndexingStateProcessor.class)); writeFunction.accept(process); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/UpdateParamsTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/UpdateParamsTests.java index dbee71a3ef102..d218f459d5ad0 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/UpdateParamsTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/UpdateParamsTests.java @@ -22,18 +22,16 @@ import static org.hamcrest.Matchers.containsInAnyOrder; - public class UpdateParamsTests extends ESTestCase { public void testFromJobUpdate() { String jobId = "foo"; - DetectionRule rule = new DetectionRule.Builder(Collections.singletonList( - new RuleCondition(RuleCondition.AppliesTo.ACTUAL, Operator.GT, 1.0))).build(); + DetectionRule rule = new DetectionRule.Builder( + Collections.singletonList(new RuleCondition(RuleCondition.AppliesTo.ACTUAL, Operator.GT, 1.0)) + ).build(); List rules = Collections.singletonList(rule); - List detectorUpdates = Collections.singletonList( - new JobUpdate.DetectorUpdate(2, null, rules)); - JobUpdate.Builder updateBuilder = new JobUpdate.Builder(jobId) - .setModelPlotConfig(new ModelPlotConfig()) + List detectorUpdates = Collections.singletonList(new JobUpdate.DetectorUpdate(2, null, rules)); + JobUpdate.Builder updateBuilder = new JobUpdate.Builder(jobId).setModelPlotConfig(new ModelPlotConfig()) .setPerPartitionCategorizationConfig(new PerPartitionCategorizationConfig()) .setDetectorUpdates(detectorUpdates); @@ -51,18 +49,21 @@ public void testFromJobUpdate() { } public void testExtractReferencedFilters() { - JobUpdate.DetectorUpdate detectorUpdate1 = new JobUpdate.DetectorUpdate(0, "", + JobUpdate.DetectorUpdate detectorUpdate1 = new JobUpdate.DetectorUpdate( + 0, + "", Arrays.asList( new DetectionRule.Builder(RuleScope.builder().include("a", "filter_1")).build(), new DetectionRule.Builder(RuleScope.builder().include("b", "filter_2")).build() ) ); - JobUpdate.DetectorUpdate detectorUpdate2 = new JobUpdate.DetectorUpdate(0, "", + JobUpdate.DetectorUpdate detectorUpdate2 = new JobUpdate.DetectorUpdate( + 0, + "", Collections.singletonList(new DetectionRule.Builder(RuleScope.builder().include("c", "filter_3")).build()) ); - UpdateParams updateParams = new UpdateParams.Builder("test_job") - .detectorUpdates(Arrays.asList(detectorUpdate1, detectorUpdate2)) + UpdateParams updateParams = new UpdateParams.Builder("test_job").detectorUpdates(Arrays.asList(detectorUpdate1, detectorUpdate2)) .filter(MlFilter.builder("filter_4").build()) .build(); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/output/AutodetectResultProcessorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/output/AutodetectResultProcessorTests.java index 1d145da2f9e1e..74ced7f295be4 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/output/AutodetectResultProcessorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/output/AutodetectResultProcessorTests.java @@ -128,7 +128,8 @@ public void setUpMocks() { new ModelSizeStats.Builder(JOB_ID).setTimestamp(new Date(BUCKET_SPAN_MS)).build(), new TimingStats(JOB_ID), Clock.fixed(CURRENT_TIME, ZoneId.systemDefault()), - flushListener); + flushListener + ); } @After @@ -189,10 +190,10 @@ public void testProcessResult_bucket_deleteInterimRequired() { public void testProcessResult_records() { AutodetectResult result = mock(AutodetectResult.class); - List records = - Arrays.asList( - new AnomalyRecord(JOB_ID, new Date(123), 123), - new AnomalyRecord(JOB_ID, new Date(123), 123)); + List records = Arrays.asList( + new AnomalyRecord(JOB_ID, new Date(123), 123), + new AnomalyRecord(JOB_ID, new Date(123), 123) + ); when(result.getRecords()).thenReturn(records); processorUnderTest.setDeleteInterimRequired(false); @@ -205,10 +206,10 @@ public void testProcessResult_records() { public void testProcessResult_influencers() { AutodetectResult result = mock(AutodetectResult.class); - List influencers = - Arrays.asList( - new Influencer(JOB_ID, "infField", "infValue", new Date(123), 123), - new Influencer(JOB_ID, "infField2", "infValue2", new Date(123), 123)); + List influencers = Arrays.asList( + new Influencer(JOB_ID, "infField", "infValue", new Date(123), 123), + new Influencer(JOB_ID, "infField2", "infValue2", new Date(123), 123) + ); when(result.getInfluencers()).thenReturn(influencers); processorUnderTest.setDeleteInterimRequired(false); @@ -326,11 +327,10 @@ public void testProcessResult_modelSizeStatsWithMemoryStatusChanges() { processorUnderTest.processResult(result); // Now with hard_limit - modelSizeStats = new ModelSizeStats.Builder(JOB_ID) - .setMemoryStatus(ModelSizeStats.MemoryStatus.HARD_LIMIT) - .setModelBytesMemoryLimit(ByteSizeValue.ofMb(512).getBytes()) - .setModelBytesExceeded(ByteSizeValue.ofKb(1).getBytes()) - .build(); + modelSizeStats = new ModelSizeStats.Builder(JOB_ID).setMemoryStatus(ModelSizeStats.MemoryStatus.HARD_LIMIT) + .setModelBytesMemoryLimit(ByteSizeValue.ofMb(512).getBytes()) + .setModelBytesExceeded(ByteSizeValue.ofKb(1).getBytes()) + .build(); when(result.getModelSizeStats()).thenReturn(modelSizeStats); processorUnderTest.processResult(result); @@ -350,8 +350,7 @@ public void testProcessResult_categorizationStatusChangeAnnotationCausesNotifica AutodetectResult result = mock(AutodetectResult.class); processorUnderTest.setDeleteInterimRequired(false); - Annotation annotation = new Annotation.Builder() - .setType(Annotation.Type.ANNOTATION) + Annotation annotation = new Annotation.Builder().setType(Annotation.Type.ANNOTATION) .setJobId(JOB_ID) .setAnnotation("Categorization status changed to 'warn' for partition 'foo'") .setEvent(Annotation.Event.CATEGORIZATION_STATUS_CHANGE) @@ -371,8 +370,7 @@ public void testProcessResult_categorizationStatusChangeAnnotationCausesNotifica public void testProcessResult_modelSnapshot() { AutodetectResult result = mock(AutodetectResult.class); - ModelSnapshot modelSnapshot = new ModelSnapshot.Builder(JOB_ID) - .setSnapshotId("a_snapshot_id") + ModelSnapshot modelSnapshot = new ModelSnapshot.Builder(JOB_ID).setSnapshotId("a_snapshot_id") .setLatestResultTimeStamp(Date.from(Instant.ofEpochMilli(1000_000_000))) .setTimestamp(Date.from(Instant.ofEpochMilli(2000_000_000))) .setMinVersion(Version.CURRENT) @@ -387,21 +385,21 @@ public void testProcessResult_modelSnapshot() { processorUnderTest.setDeleteInterimRequired(false); processorUnderTest.processResult(result); - Annotation expectedAnnotation = - new Annotation.Builder() - .setAnnotation("Job model snapshot with id [a_snapshot_id] stored") - .setCreateTime(Date.from(CURRENT_TIME)) - .setCreateUsername(XPackUser.NAME) - .setTimestamp(Date.from(Instant.ofEpochMilli(1000_000_000))) - .setEndTimestamp(Date.from(Instant.ofEpochMilli(1000_000_000))) - .setJobId(JOB_ID) - .setModifiedTime(Date.from(CURRENT_TIME)) - .setModifiedUsername(XPackUser.NAME) - .setType(Annotation.Type.ANNOTATION) - .setEvent(Annotation.Event.MODEL_SNAPSHOT_STORED) - .build(); - UpdateJobAction.Request expectedJobUpdateRequest = UpdateJobAction.Request.internal(JOB_ID, - new JobUpdate.Builder(JOB_ID).setModelSnapshotId("a_snapshot_id").build()); + Annotation expectedAnnotation = new Annotation.Builder().setAnnotation("Job model snapshot with id [a_snapshot_id] stored") + .setCreateTime(Date.from(CURRENT_TIME)) + .setCreateUsername(XPackUser.NAME) + .setTimestamp(Date.from(Instant.ofEpochMilli(1000_000_000))) + .setEndTimestamp(Date.from(Instant.ofEpochMilli(1000_000_000))) + .setJobId(JOB_ID) + .setModifiedTime(Date.from(CURRENT_TIME)) + .setModifiedUsername(XPackUser.NAME) + .setType(Annotation.Type.ANNOTATION) + .setEvent(Annotation.Event.MODEL_SNAPSHOT_STORED) + .build(); + UpdateJobAction.Request expectedJobUpdateRequest = UpdateJobAction.Request.internal( + JOB_ID, + new JobUpdate.Builder(JOB_ID).setModelSnapshotId("a_snapshot_id").build() + ); verify(persister).bulkPersisterBuilder(eq(JOB_ID)); verify(persister).persistModelSnapshot(eq(modelSnapshot), eq(WriteRequest.RefreshPolicy.IMMEDIATE), any()); @@ -485,8 +483,10 @@ public void testParsingErrorSetsFailed() throws Exception { assertTrue(processorUnderTest.isFailed()); // Wait for flush should return immediately - FlushAcknowledgement flushAcknowledgement = - processorUnderTest.waitForFlushAcknowledgement(JOB_ID, Duration.of(300, ChronoUnit.SECONDS)); + FlushAcknowledgement flushAcknowledgement = processorUnderTest.waitForFlushAcknowledgement( + JOB_ID, + Duration.of(300, ChronoUnit.SECONDS) + ); assertThat(flushAcknowledgement, is(nullValue())); verify(persister).bulkPersisterBuilder(eq(JOB_ID)); @@ -549,7 +549,6 @@ public void testProcessingForecasts() { processorUnderTest.setDeleteInterimRequired(false); processorUnderTest.processResult(result); - result = mock(AutodetectResult.class); forecastRequestStats = new ForecastRequestStats("foo", "forecast"); forecastRequestStats.setStatus(ForecastRequestStats.ForecastRequestStatus.FINISHED); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/output/FlushAcknowledgementTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/output/FlushAcknowledgementTests.java index 214111c343926..2a4c7270c7541 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/output/FlushAcknowledgementTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/output/FlushAcknowledgementTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.ml.job.process.autodetect.output; import org.elasticsearch.common.io.stream.Writeable.Reader; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.job.process.autodetect.output.FlushAcknowledgement; import java.time.Instant; diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/params/AutodetectParamsTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/params/AutodetectParamsTests.java index b84f7e8cb4a46..0fa3983f53604 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/params/AutodetectParamsTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/params/AutodetectParamsTests.java @@ -24,10 +24,12 @@ public void testBuilder_WithTimingStats() { timingStats.updateStats(2000.0); assertThat( timingStats, - equalTo(new TimingStats(JOB_ID, 8, 1.0, 2000.0, 832.75, 1010.0, new ExponentialAverageCalculationContext(2000.0, null, null)))); + equalTo(new TimingStats(JOB_ID, 8, 1.0, 2000.0, 832.75, 1010.0, new ExponentialAverageCalculationContext(2000.0, null, null))) + ); assertThat( params.timingStats(), - equalTo(new TimingStats(JOB_ID, 7, 1.0, 1000.0, 666.0, 1000.0, new ExponentialAverageCalculationContext()))); + equalTo(new TimingStats(JOB_ID, 7, 1.0, 1000.0, 666.0, 1000.0, new ExponentialAverageCalculationContext())) + ); } public void testBuilder_WithoutTimingStats() { diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/params/FlushJobParamsTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/params/FlushJobParamsTests.java index 445c60fb6380a..be28e6b994c91 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/params/FlushJobParamsTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/params/FlushJobParamsTests.java @@ -33,9 +33,9 @@ public void testBuilder_GivenCalcInterim() { public void testBuilder_GivenCalcInterimAndStart() { FlushJobParams params = FlushJobParams.builder() - .calcInterim(true) - .forTimeRange(TimeRange.builder().startTime("42").build()) - .build(); + .calcInterim(true) + .forTimeRange(TimeRange.builder().startTime("42").build()) + .build(); assertTrue(params.shouldCalculateInterim()); assertFalse(params.shouldAdvanceTime()); assertFalse(params.shouldSkipTime()); @@ -44,20 +44,19 @@ public void testBuilder_GivenCalcInterimAndStart() { } public void testBuilder_GivenCalcInterimAndEnd_throws() { - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> FlushJobParams.builder() - .calcInterim(true) - .forTimeRange(TimeRange.builder().endTime("100").build()) - .build()); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> FlushJobParams.builder().calcInterim(true).forTimeRange(TimeRange.builder().endTime("100").build()).build() + ); assertEquals("Invalid flush parameters: 'start' has not been specified.", e.getMessage()); } public void testBuilder_GivenCalcInterimAndStartAndEnd() { FlushJobParams params = FlushJobParams.builder() - .calcInterim(true) - .forTimeRange(TimeRange.builder().startTime("3600").endTime("7200").build()) - .build(); + .calcInterim(true) + .forTimeRange(TimeRange.builder().startTime("3600").endTime("7200").build()) + .build(); assertTrue(params.shouldCalculateInterim()); assertFalse(params.shouldAdvanceTime()); assertEquals("3600", params.getStart()); @@ -74,10 +73,7 @@ public void testBuilder_GivenAdvanceTime() { } public void testBuilder_GivenCalcInterimAndAdvanceTime() { - FlushJobParams params = FlushJobParams.builder() - .calcInterim(true) - .advanceTime("1940") - .build(); + FlushJobParams params = FlushJobParams.builder().calcInterim(true).advanceTime("1940").build(); assertTrue(params.shouldCalculateInterim()); assertEquals("", params.getStart()); assertEquals("", params.getEnd()); @@ -87,10 +83,10 @@ public void testBuilder_GivenCalcInterimAndAdvanceTime() { public void testBuilder_GivenCalcInterimWithTimeRangeAndAdvanceTime() { FlushJobParams params = FlushJobParams.builder() - .calcInterim(true) - .forTimeRange(TimeRange.builder().startTime("1").endTime("2").build()) - .advanceTime("1940") - .build(); + .calcInterim(true) + .forTimeRange(TimeRange.builder().startTime("1").endTime("2").build()) + .advanceTime("1940") + .build(); assertTrue(params.shouldCalculateInterim()); assertEquals("1", params.getStart()); assertEquals("2", params.getEnd()); @@ -99,15 +95,19 @@ public void testBuilder_GivenCalcInterimWithTimeRangeAndAdvanceTime() { } public void testBuilder_GivenAdvanceTimeIsEarlierThanSkipTime() { - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> FlushJobParams.builder().advanceTime("2017-01-01T00:00:00Z").skipTime("2017-02-01T00:00:00Z").build()); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> FlushJobParams.builder().advanceTime("2017-01-01T00:00:00Z").skipTime("2017-02-01T00:00:00Z").build() + ); assertEquals("advance_time [2017-01-01T00:00:00Z] must be later than skip_time [2017-02-01T00:00:00Z]", e.getMessage()); } public void testBuilder_GivenAdvanceTimeIsEqualToSkipTime() { - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> FlushJobParams.builder().advanceTime("2017-01-01T00:00:00Z").skipTime("2017-01-01T00:00:00Z").build()); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> FlushJobParams.builder().advanceTime("2017-01-01T00:00:00Z").skipTime("2017-01-01T00:00:00Z").build() + ); assertEquals("advance_time [2017-01-01T00:00:00Z] must be later than skip_time [2017-01-01T00:00:00Z]", e.getMessage()); } @@ -120,39 +120,48 @@ public void testBuilder_GivenAdvanceTimeIsLaterToSkipTime() { } public void testValidate_GivenOnlyStartSpecified() { - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> FlushJobParams.builder().forTimeRange(TimeRange.builder().startTime("1").build()).build()); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> FlushJobParams.builder().forTimeRange(TimeRange.builder().startTime("1").build()).build() + ); assertEquals("Invalid flush parameters: unexpected 'start'.", e.getMessage()); } public void testFlushUpload_GivenOnlyEndSpecified() { - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> FlushJobParams.builder().forTimeRange(TimeRange.builder().endTime("1").build()).build()); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> FlushJobParams.builder().forTimeRange(TimeRange.builder().endTime("1").build()).build() + ); assertEquals("Invalid flush parameters: unexpected 'end'.", e.getMessage()); } public void testFlushUpload_GivenInterimResultsAndOnlyEndSpecified() { - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> FlushJobParams.builder().calcInterim(true).forTimeRange(TimeRange.builder().endTime("1").build()).build()); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> FlushJobParams.builder().calcInterim(true).forTimeRange(TimeRange.builder().endTime("1").build()).build() + ); assertEquals("Invalid flush parameters: 'start' has not been specified.", e.getMessage()); } public void testFlushUpload_GivenInterimResultsAndStartAndEndSpecifiedAsEpochs() { - FlushJobParams params = FlushJobParams.builder().calcInterim(true) - .forTimeRange(TimeRange.builder().startTime("1428494400").endTime("1428498000").build()).build(); + FlushJobParams params = FlushJobParams.builder() + .calcInterim(true) + .forTimeRange(TimeRange.builder().startTime("1428494400").endTime("1428498000").build()) + .build(); assertTrue(params.shouldCalculateInterim()); assertFalse(params.shouldAdvanceTime()); assertEquals("1428494400", params.getStart()); assertEquals("1428498000", params.getEnd()); } - public void testFlushUpload_GivenInterimResultsAndSameStartAndEnd() { - FlushJobParams params = FlushJobParams.builder().calcInterim(true) - .forTimeRange(TimeRange.builder().startTime("1428494400").endTime("1428494400").build()).build(); + FlushJobParams params = FlushJobParams.builder() + .calcInterim(true) + .forTimeRange(TimeRange.builder().startTime("1428494400").endTime("1428494400").build()) + .build(); assertTrue(params.shouldCalculateInterim()); assertFalse(params.shouldAdvanceTime()); @@ -161,8 +170,10 @@ public void testFlushUpload_GivenInterimResultsAndSameStartAndEnd() { } public void testFlushUpload_GivenInterimResultsAndOnlyStartSpecified() { - FlushJobParams params = FlushJobParams.builder().calcInterim(true) - .forTimeRange(TimeRange.builder().startTime("1428494400").build()).build(); + FlushJobParams params = FlushJobParams.builder() + .calcInterim(true) + .forTimeRange(TimeRange.builder().startTime("1428494400").build()) + .build(); assertTrue(params.shouldCalculateInterim()); assertFalse(params.shouldAdvanceTime()); @@ -189,9 +200,11 @@ public void testFlushUpload_GivenCalcInterimAndAdvanceTime() { } public void testFlushUpload_GivenCalcInterimWithTimeRangeAndAdvanceTime() { - FlushJobParams params = FlushJobParams.builder().calcInterim(true) - .forTimeRange(TimeRange.builder().startTime("150").endTime("300").build()) - .advanceTime("200").build(); + FlushJobParams params = FlushJobParams.builder() + .calcInterim(true) + .forTimeRange(TimeRange.builder().startTime("150").endTime("300").build()) + .advanceTime("200") + .build(); assertTrue(params.shouldCalculateInterim()); assertEquals("150", params.getStart()); assertEquals("300", params.getEnd()); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/params/ForecastParamsTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/params/ForecastParamsTests.java index 6c51d39e3c4bf..ba6e39e5b79fe 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/params/ForecastParamsTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/params/ForecastParamsTests.java @@ -6,9 +6,9 @@ */ package org.elasticsearch.xpack.ml.job.process.autodetect.params; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.core.TimeValue; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.ParseField; import java.util.HashSet; import java.util.Set; @@ -28,9 +28,13 @@ public void testForecastIdsAreUnique() { } public void testDurationFormats() { - assertEquals(34678L, - ForecastParams.builder().duration(TimeValue.parseTimeValue("34678s", DURATION.getPreferredName())).build().getDuration()); - assertEquals(172800L, - ForecastParams.builder().duration(TimeValue.parseTimeValue("2d", DURATION.getPreferredName())).build().getDuration()); + assertEquals( + 34678L, + ForecastParams.builder().duration(TimeValue.parseTimeValue("34678s", DURATION.getPreferredName())).build().getDuration() + ); + assertEquals( + 172800L, + ForecastParams.builder().duration(TimeValue.parseTimeValue("2d", DURATION.getPreferredName())).build().getDuration() + ); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/params/TimeRangeTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/params/TimeRangeTests.java index 2b00368afad31..a891080e5a271 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/params/TimeRangeTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/params/TimeRangeTests.java @@ -24,21 +24,26 @@ public void testGetEnd() { } public void test_UnparseableStartThrows() { - ElasticsearchParseException e = - ESTestCase.expectThrows(ElasticsearchParseException.class, () -> TimeRange.builder().startTime("bad").build()); + ElasticsearchParseException e = ESTestCase.expectThrows( + ElasticsearchParseException.class, + () -> TimeRange.builder().startTime("bad").build() + ); assertEquals(Messages.getMessage(Messages.REST_INVALID_DATETIME_PARAMS, TimeRange.START_PARAM, "bad"), e.getMessage()); } public void test_UnparseableEndThrows() { - ElasticsearchParseException e = - ESTestCase.expectThrows(ElasticsearchParseException.class, () -> TimeRange.builder().endTime("bad").build()); + ElasticsearchParseException e = ESTestCase.expectThrows( + ElasticsearchParseException.class, + () -> TimeRange.builder().endTime("bad").build() + ); assertEquals(Messages.getMessage(Messages.REST_INVALID_DATETIME_PARAMS, TimeRange.END_PARAM, "bad"), e.getMessage()); } public void test_EndComesBeforeStartThrows() { - IllegalArgumentException e = - ESTestCase.expectThrows(IllegalArgumentException.class, - () -> TimeRange.builder().startTime("2016-10-01T10:00:00Z").endTime("2016-09-30T10:00:00Z").build()); + IllegalArgumentException e = ESTestCase.expectThrows( + IllegalArgumentException.class, + () -> TimeRange.builder().startTime("2016-10-01T10:00:00Z").endTime("2016-09-30T10:00:00Z").build() + ); assertEquals(Messages.getMessage(Messages.REST_START_AFTER_END, "2016-09-30T10:00:00Z", "2016-10-01T10:00:00Z"), e.getMessage()); } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/AbstractDataToProcessWriterTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/AbstractDataToProcessWriterTests.java index c4c519d6bad59..68d75e7ec2b7c 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/AbstractDataToProcessWriterTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/AbstractDataToProcessWriterTests.java @@ -7,17 +7,17 @@ package org.elasticsearch.xpack.ml.job.process.autodetect.writer; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.env.Environment; import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.index.analysis.AnalysisRegistry; import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.xpack.ml.job.categorization.CategorizationAnalyzer; -import org.elasticsearch.xpack.ml.job.categorization.CategorizationAnalyzerTests; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.ml.job.config.AnalysisConfig; import org.elasticsearch.xpack.core.ml.job.config.CategorizationAnalyzerConfig; import org.elasticsearch.xpack.core.ml.job.config.DataDescription; import org.elasticsearch.xpack.core.ml.job.config.Detector; +import org.elasticsearch.xpack.ml.job.categorization.CategorizationAnalyzer; +import org.elasticsearch.xpack.ml.job.categorization.CategorizationAnalyzerTests; import org.elasticsearch.xpack.ml.job.process.DataCountsReporter; import org.elasticsearch.xpack.ml.job.process.autodetect.AutodetectProcess; import org.elasticsearch.xpack.ml.job.process.autodetect.writer.AbstractDataToProcessWriter.InputOutputMap; @@ -60,8 +60,15 @@ public void testInputFields() throws IOException { AnalysisConfig ac = new AnalysisConfig.Builder(Collections.singletonList(detector.build())).build(); boolean includeTokensFields = randomBoolean(); - AbstractDataToProcessWriter writer = new JsonDataToProcessWriter(true, includeTokensFields, autodetectProcess, - dd.build(), ac, dataCountsReporter, NamedXContentRegistry.EMPTY); + AbstractDataToProcessWriter writer = new JsonDataToProcessWriter( + true, + includeTokensFields, + autodetectProcess, + dd.build(), + ac, + dataCountsReporter, + NamedXContentRegistry.EMPTY + ); writer.writeHeader(); @@ -111,26 +118,51 @@ public void testTokenizeForCategorization() throws IOException { CategorizationAnalyzerConfig defaultConfig = CategorizationAnalyzerConfig.buildDefaultCategorizationAnalyzer(null); try (CategorizationAnalyzer categorizationAnalyzer = new CategorizationAnalyzer(analysisRegistry, defaultConfig)) { - assertEquals("sol13m-8608.1.p2ps,Info,Source,AES_SERVICE2,on,has,shut,down", - AbstractDataToProcessWriter.tokenizeForCategorization(categorizationAnalyzer, "p2ps", - " Source AES_SERVICE2 on 33122:967 has shut down.")); - - assertEquals("Vpxa,verbose,VpxaHalCnxHostagent,opID,WFU-ddeadb59,WaitForUpdatesDone,Received,callback", - AbstractDataToProcessWriter.tokenizeForCategorization(categorizationAnalyzer, "vmware", - "Vpxa: [49EC0B90 verbose 'VpxaHalCnxHostagent' opID=WFU-ddeadb59] [WaitForUpdatesDone] Received callback")); - - assertEquals("org.apache.coyote.http11.Http11BaseProtocol,destroy", - AbstractDataToProcessWriter.tokenizeForCategorization(categorizationAnalyzer, "apache", - "org.apache.coyote.http11.Http11BaseProtocol destroy")); - - assertEquals("INFO,session,PROXY,Session,DESTROYED", - AbstractDataToProcessWriter.tokenizeForCategorization(categorizationAnalyzer, "proxy", - " [1111529792] INFO session <45409105041220090733@62.218.251.123> - " + - "----------------- PROXY Session DESTROYED --------------------")); - - assertEquals("PSYoungGen,total,used", - AbstractDataToProcessWriter.tokenizeForCategorization(categorizationAnalyzer, "java", - "PSYoungGen total 2572800K, used 1759355K [0x0000000759500000, 0x0000000800000000, 0x0000000800000000)")); + assertEquals( + "sol13m-8608.1.p2ps,Info,Source,AES_SERVICE2,on,has,shut,down", + AbstractDataToProcessWriter.tokenizeForCategorization( + categorizationAnalyzer, + "p2ps", + " Source AES_SERVICE2 on 33122:967 has shut down." + ) + ); + + assertEquals( + "Vpxa,verbose,VpxaHalCnxHostagent,opID,WFU-ddeadb59,WaitForUpdatesDone,Received,callback", + AbstractDataToProcessWriter.tokenizeForCategorization( + categorizationAnalyzer, + "vmware", + "Vpxa: [49EC0B90 verbose 'VpxaHalCnxHostagent' opID=WFU-ddeadb59] [WaitForUpdatesDone] Received callback" + ) + ); + + assertEquals( + "org.apache.coyote.http11.Http11BaseProtocol,destroy", + AbstractDataToProcessWriter.tokenizeForCategorization( + categorizationAnalyzer, + "apache", + "org.apache.coyote.http11.Http11BaseProtocol destroy" + ) + ); + + assertEquals( + "INFO,session,PROXY,Session,DESTROYED", + AbstractDataToProcessWriter.tokenizeForCategorization( + categorizationAnalyzer, + "proxy", + " [1111529792] INFO session <45409105041220090733@62.218.251.123> - " + + "----------------- PROXY Session DESTROYED --------------------" + ) + ); + + assertEquals( + "PSYoungGen,total,used", + AbstractDataToProcessWriter.tokenizeForCategorization( + categorizationAnalyzer, + "java", + "PSYoungGen total 2572800K, used 1759355K [0x0000000759500000, 0x0000000800000000, 0x0000000800000000)" + ) + ); } } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/AutodetectControlMsgWriterTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/AutodetectControlMsgWriterTests.java index db1fabbddcc53..d38b730949599 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/AutodetectControlMsgWriterTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/AutodetectControlMsgWriterTests.java @@ -86,8 +86,7 @@ public void testWriteFlushControlMessage_GivenSkipAndAdvanceTime() throws IOExce public void testWriteFlushControlMessage_GivenCalcInterimResultsWithNoTimeParams() throws IOException { AutodetectControlMsgWriter writer = new AutodetectControlMsgWriter(lengthEncodedWriter, 4); - FlushJobParams flushJobParams = FlushJobParams.builder() - .calcInterim(true).build(); + FlushJobParams flushJobParams = FlushJobParams.builder().calcInterim(true).build(); writer.writeFlushControlMessage(flushJobParams); @@ -110,9 +109,9 @@ public void testWriteFlushControlMessage_GivenPlainFlush() throws IOException { public void testWriteFlushControlMessage_GivenCalcInterimResultsWithTimeParams() throws IOException { AutodetectControlMsgWriter writer = new AutodetectControlMsgWriter(lengthEncodedWriter, 4); FlushJobParams flushJobParams = FlushJobParams.builder() - .calcInterim(true) - .forTimeRange(TimeRange.builder().startTime("120").endTime("180").build()) - .build(); + .calcInterim(true) + .forTimeRange(TimeRange.builder().startTime("120").endTime("180").build()) + .build(); writer.writeFlushControlMessage(flushJobParams); @@ -126,10 +125,10 @@ public void testWriteFlushControlMessage_GivenCalcInterimResultsWithTimeParams() public void testWriteFlushControlMessage_GivenCalcInterimAndAdvanceTime() throws IOException { AutodetectControlMsgWriter writer = new AutodetectControlMsgWriter(lengthEncodedWriter, 4); FlushJobParams flushJobParams = FlushJobParams.builder() - .calcInterim(true) - .forTimeRange(TimeRange.builder().startTime("50").endTime("100").build()) - .advanceTime("180") - .build(); + .calcInterim(true) + .forTimeRange(TimeRange.builder().startTime("50").endTime("100").build()) + .advanceTime("180") + .build(); writer.writeFlushControlMessage(flushJobParams); @@ -169,8 +168,7 @@ public void testWriteFlushMessage() throws IOException { public void testWriteResetBucketsMessage() throws IOException { AutodetectControlMsgWriter writer = new AutodetectControlMsgWriter(lengthEncodedWriter, 4); - writer.writeResetBucketsMessage( - new DataLoadParams(TimeRange.builder().startTime("0").endTime("600").build(), Optional.empty())); + writer.writeResetBucketsMessage(new DataLoadParams(TimeRange.builder().startTime("0").endTime("600").build(), Optional.empty())); InOrder inOrder = inOrder(lengthEncodedWriter); inOrder.verify(lengthEncodedWriter).writeNumFields(4); @@ -202,10 +200,13 @@ public void testWriteUpdateDetectorRulesMessage() throws IOException { InOrder inOrder = inOrder(lengthEncodedWriter); inOrder.verify(lengthEncodedWriter).writeNumFields(4); inOrder.verify(lengthEncodedWriter, times(3)).writeField(""); - inOrder.verify(lengthEncodedWriter).writeField("u{\"detector_rules\":{\"detector_index\":2," + - "\"custom_rules\":[{\"actions\":[\"skip_result\"]," + - "\"conditions\":[{\"applies_to\":\"actual\",\"operator\":\"gt\",\"value\":5.0}]}," + - "{\"actions\":[\"skip_result\"],\"conditions\":[{\"applies_to\":\"actual\",\"operator\":\"gt\",\"value\":5.0}]}]}}"); + inOrder.verify(lengthEncodedWriter) + .writeField( + "u{\"detector_rules\":{\"detector_index\":2," + + "\"custom_rules\":[{\"actions\":[\"skip_result\"]," + + "\"conditions\":[{\"applies_to\":\"actual\",\"operator\":\"gt\",\"value\":5.0}]}," + + "{\"actions\":[\"skip_result\"],\"conditions\":[{\"applies_to\":\"actual\",\"operator\":\"gt\",\"value\":5.0}]}]}}" + ); verifyNoMoreInteractions(lengthEncodedWriter); } @@ -220,8 +221,10 @@ public void testWriteUpdateFiltersMessage() throws IOException { InOrder inOrder = inOrder(lengthEncodedWriter); inOrder.verify(lengthEncodedWriter).writeNumFields(2); inOrder.verify(lengthEncodedWriter, times(1)).writeField(""); - inOrder.verify(lengthEncodedWriter).writeField("u{\"filters\":[{\"filter_id\":\"filter_1\",\"items\":[\"a\"]}," + - "{\"filter_id\":\"filter_2\",\"items\":[\"b\",\"c\"]}]}"); + inOrder.verify(lengthEncodedWriter) + .writeField( + "u{\"filters\":[{\"filter_id\":\"filter_1\",\"items\":[\"a\"]}," + "{\"filter_id\":\"filter_2\",\"items\":[\"b\",\"c\"]}]}" + ); verifyNoMoreInteractions(lengthEncodedWriter); } @@ -247,13 +250,18 @@ public void testWriteUpdateScheduledEventsMessage() throws IOException { inOrder.verify(lengthEncodedWriter, times(1)).writeField(""); ArgumentCaptor capturedMessage = ArgumentCaptor.forClass(String.class); inOrder.verify(lengthEncodedWriter).writeField(capturedMessage.capture()); - assertThat(capturedMessage.getValue(), equalTo("u{\"events\":[{\"description\":\"new year\"," + - "\"rules\":[{\"actions\":[\"skip_result\",\"skip_model_update\"]," + - "\"conditions\":[{\"applies_to\":\"time\",\"operator\":\"gte\",\"value\":1.5147648E9}," + - "{\"applies_to\":\"time\",\"operator\":\"lt\",\"value\":1.5148512E9}]}]}," + - "{\"description\":\"Jan maintenance day\",\"rules\":[{\"actions\":[\"skip_result\",\"skip_model_update\"]," + - "\"conditions\":[{\"applies_to\":\"time\",\"operator\":\"gte\",\"value\":1.5151968E9}," + - "{\"applies_to\":\"time\",\"operator\":\"lt\",\"value\":1.5152832E9}]}]}]}")); + assertThat( + capturedMessage.getValue(), + equalTo( + "u{\"events\":[{\"description\":\"new year\"," + + "\"rules\":[{\"actions\":[\"skip_result\",\"skip_model_update\"]," + + "\"conditions\":[{\"applies_to\":\"time\",\"operator\":\"gte\",\"value\":1.5147648E9}," + + "{\"applies_to\":\"time\",\"operator\":\"lt\",\"value\":1.5148512E9}]}]}," + + "{\"description\":\"Jan maintenance day\",\"rules\":[{\"actions\":[\"skip_result\",\"skip_model_update\"]," + + "\"conditions\":[{\"applies_to\":\"time\",\"operator\":\"gte\",\"value\":1.5151968E9}," + + "{\"applies_to\":\"time\",\"operator\":\"lt\",\"value\":1.5152832E9}]}]}]}" + ) + ); verifyNoMoreInteractions(lengthEncodedWriter); } @@ -308,8 +316,13 @@ public void testWriteForecastParamsMessage() throws IOException { inOrder.verify(lengthEncodedWriter).writeField(capturedMessage.capture()); assertThat(capturedMessage.getValue(), startsWith("p{\"forecast_id\":\"")); - assertThat(capturedMessage.getValue(), endsWith("\"duration\":10800,\"expires_in\":345600,\"tmp_storage\":\"/my_temp_dir\"," - +"\"max_model_memory\":12345,\"min_available_disk_space\":98765}")); + assertThat( + capturedMessage.getValue(), + endsWith( + "\"duration\":10800,\"expires_in\":345600,\"tmp_storage\":\"/my_temp_dir\"," + + "\"max_model_memory\":12345,\"min_available_disk_space\":98765}" + ) + ); inOrder.verify(lengthEncodedWriter).writeNumFields(2); inOrder.verify(lengthEncodedWriter).writeField(""); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/DateFormatDateTransformerTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/DateFormatDateTransformerTests.java index cc81c7aede725..b567772d28455 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/DateFormatDateTransformerTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/DateFormatDateTransformerTests.java @@ -19,8 +19,10 @@ public void testTransform_GivenValidTimestamp() throws CannotParseTimestampExcep public void testTransform_GivenInvalidTimestamp() throws CannotParseTimestampException { DateFormatDateTransformer transformer = new DateFormatDateTransformer("yyyy-MM-dd HH:mm:ssXXX"); - CannotParseTimestampException e = ESTestCase.expectThrows(CannotParseTimestampException.class, - () -> transformer.transform("invalid")); + CannotParseTimestampException e = ESTestCase.expectThrows( + CannotParseTimestampException.class, + () -> transformer.transform("invalid") + ); assertEquals("Cannot parse date 'invalid' with format string 'yyyy-MM-dd HH:mm:ssXXX'", e.getMessage()); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/DoubleDateTransformerTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/DoubleDateTransformerTests.java index 14c4209cea5ef..c225ba4a0d058 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/DoubleDateTransformerTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/DoubleDateTransformerTests.java @@ -25,8 +25,10 @@ public void testTransform_GivenTimestampIsMilliseconds() throws CannotParseTimes public void testTransform_GivenTimestampIsNotValidDouble() throws CannotParseTimestampException { DoubleDateTransformer transformer = new DoubleDateTransformer(false); - CannotParseTimestampException e = ESTestCase.expectThrows(CannotParseTimestampException.class, - () -> transformer.transform("invalid")); + CannotParseTimestampException e = ESTestCase.expectThrows( + CannotParseTimestampException.class, + () -> transformer.transform("invalid") + ); assertEquals("Cannot parse timestamp 'invalid' as epoch value", e.getMessage()); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/JsonDataToProcessWriterTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/JsonDataToProcessWriterTests.java index 7c4d55ba03ea1..d5ea8d30b7644 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/JsonDataToProcessWriterTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/JsonDataToProcessWriterTests.java @@ -11,21 +11,21 @@ import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentGenerator; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.env.Environment; import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.index.analysis.AnalysisRegistry; import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.xpack.ml.MachineLearning; -import org.elasticsearch.xpack.ml.job.categorization.CategorizationAnalyzer; -import org.elasticsearch.xpack.ml.job.categorization.CategorizationAnalyzerTests; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentGenerator; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.ml.job.config.AnalysisConfig; import org.elasticsearch.xpack.core.ml.job.config.CategorizationAnalyzerConfig; import org.elasticsearch.xpack.core.ml.job.config.DataDescription; import org.elasticsearch.xpack.core.ml.job.config.Detector; +import org.elasticsearch.xpack.ml.MachineLearning; +import org.elasticsearch.xpack.ml.job.categorization.CategorizationAnalyzer; +import org.elasticsearch.xpack.ml.job.categorization.CategorizationAnalyzerTests; import org.elasticsearch.xpack.ml.job.process.DataCountsReporter; import org.elasticsearch.xpack.ml.job.process.autodetect.AutodetectProcess; import org.junit.Before; @@ -85,8 +85,7 @@ public Void answer(InvocationOnMock invocation) throws Throwable { dataDescription.setTimeFormat(DataDescription.EPOCH); Detector detector = new Detector.Builder("metric", "value").build(); - analysisConfig = new AnalysisConfig.Builder(Collections.singletonList(detector)) - .setBucketSpan(TimeValue.timeValueSeconds(1)) + analysisConfig = new AnalysisConfig.Builder(Collections.singletonList(detector)).setBucketSpan(TimeValue.timeValueSeconds(1)) .build(); } @@ -102,9 +101,9 @@ public void testWrite_GivenTimeFormatIsEpochAndDataIsValid() throws Exception { List expectedRecords = new ArrayList<>(); // The final field is the control field - expectedRecords.add(new String[]{"time", "value", "."}); - expectedRecords.add(new String[]{"1", "1.0", ""}); - expectedRecords.add(new String[]{"2", "2.0", ""}); + expectedRecords.add(new String[] { "time", "value", "." }); + expectedRecords.add(new String[] { "1", "1.0", "" }); + expectedRecords.add(new String[] { "2", "2.0", "" }); assertWrittenRecordsEqualTo(expectedRecords); verify(dataCountsReporter).finishReporting(); @@ -124,8 +123,12 @@ public void testWrite_GivenTimeFormatIsEpochAndCategorization() throws Exception InputStream inputStream = createInputStream(input.toString()); JsonDataToProcessWriter writer = createWriter(); writer.writeHeader(); - try (CategorizationAnalyzer categorizationAnalyzer = - new CategorizationAnalyzer(analysisRegistry, analysisConfig.getCategorizationAnalyzerConfig())) { + try ( + CategorizationAnalyzer categorizationAnalyzer = new CategorizationAnalyzer( + analysisRegistry, + analysisConfig.getCategorizationAnalyzerConfig() + ) + ) { writer.write(inputStream, categorizationAnalyzer, XContentType.JSON, (r, e) -> {}); } verify(dataCountsReporter, times(1)).startNewIncrementalCount(); @@ -133,13 +136,13 @@ public void testWrite_GivenTimeFormatIsEpochAndCategorization() throws Exception List expectedRecords = new ArrayList<>(); // The "." field is the control field; "..." is the pre-tokenized tokens field if (MachineLearning.CATEGORIZATION_TOKENIZATION_IN_JAVA) { - expectedRecords.add(new String[]{"time", "message", "...", "."}); - expectedRecords.add(new String[]{"1", "Node 1 started", "Node,started", ""}); - expectedRecords.add(new String[]{"2", "Node 2 started", "Node,started", ""}); + expectedRecords.add(new String[] { "time", "message", "...", "." }); + expectedRecords.add(new String[] { "1", "Node 1 started", "Node,started", "" }); + expectedRecords.add(new String[] { "2", "Node 2 started", "Node,started", "" }); } else { - expectedRecords.add(new String[]{"time", "message", "."}); - expectedRecords.add(new String[]{"1", "Node 1 started", ""}); - expectedRecords.add(new String[]{"2", "Node 2 started", ""}); + expectedRecords.add(new String[] { "time", "message", "." }); + expectedRecords.add(new String[] { "1", "Node 1 started", "" }); + expectedRecords.add(new String[] { "2", "Node 2 started", "" }); } assertWrittenRecordsEqualTo(expectedRecords); @@ -159,8 +162,8 @@ public void testWrite_GivenTimeFormatIsEpochAndTimestampsAreOutOfOrder() throws List expectedRecords = new ArrayList<>(); // The final field is the control field - expectedRecords.add(new String[]{"time", "value", "."}); - expectedRecords.add(new String[]{"3", "3.0", ""}); + expectedRecords.add(new String[] { "time", "value", "." }); + expectedRecords.add(new String[] { "3", "3.0", "" }); assertWrittenRecordsEqualTo(expectedRecords); verify(dataCountsReporter, times(2)).reportOutOfOrderRecord(2); @@ -169,10 +172,7 @@ public void testWrite_GivenTimeFormatIsEpochAndTimestampsAreOutOfOrder() throws } public void testWrite_GivenTimeFormatIsEpochAndSomeTimestampsOutOfOrderWithinBucketSpan() throws Exception { - analysisConfig = new AnalysisConfig.Builder( - Collections.singletonList( - new Detector.Builder("metric", "value").build() - )) + analysisConfig = new AnalysisConfig.Builder(Collections.singletonList(new Detector.Builder("metric", "value").build())) .setBucketSpan(TimeValue.timeValueSeconds(10)) .build(); @@ -191,13 +191,13 @@ public void testWrite_GivenTimeFormatIsEpochAndSomeTimestampsOutOfOrderWithinBuc List expectedRecords = new ArrayList<>(); // The final field is the control field - expectedRecords.add(new String[]{"time", "value", "."}); - expectedRecords.add(new String[]{"4", "4.0", ""}); - expectedRecords.add(new String[]{"5", "5.0", ""}); - expectedRecords.add(new String[]{"3", "3.0", ""}); - expectedRecords.add(new String[]{"4", "4.0", ""}); - expectedRecords.add(new String[]{"2", "2.0", ""}); - expectedRecords.add(new String[]{"12", "12.0", ""}); + expectedRecords.add(new String[] { "time", "value", "." }); + expectedRecords.add(new String[] { "4", "4.0", "" }); + expectedRecords.add(new String[] { "5", "5.0", "" }); + expectedRecords.add(new String[] { "3", "3.0", "" }); + expectedRecords.add(new String[] { "4", "4.0", "" }); + expectedRecords.add(new String[] { "2", "2.0", "" }); + expectedRecords.add(new String[] { "12", "12.0", "" }); assertWrittenRecordsEqualTo(expectedRecords); verify(dataCountsReporter, times(1)).reportOutOfOrderRecord(2); @@ -206,13 +206,9 @@ public void testWrite_GivenTimeFormatIsEpochAndSomeTimestampsOutOfOrderWithinBuc } public void testWrite_GivenTimeFormatIsEpochAndSomeTimestampsWithinLatencySomeOutOfOrder() throws Exception { - analysisConfig = new AnalysisConfig.Builder( - Collections.singletonList( - new Detector.Builder("metric", "value").build() - )) - .setLatency(TimeValue.timeValueSeconds(2)) - .setBucketSpan(TimeValue.timeValueSeconds(1)).setLatency(TimeValue.timeValueSeconds(2)) - .build(); + analysisConfig = new AnalysisConfig.Builder(Collections.singletonList(new Detector.Builder("metric", "value").build())).setLatency( + TimeValue.timeValueSeconds(2) + ).setBucketSpan(TimeValue.timeValueSeconds(1)).setLatency(TimeValue.timeValueSeconds(2)).build(); StringBuilder input = new StringBuilder(); input.append("{\"time\":\"4\", \"metric\":\"foo\", \"value\":\"4.0\"}"); @@ -227,11 +223,11 @@ public void testWrite_GivenTimeFormatIsEpochAndSomeTimestampsWithinLatencySomeOu List expectedRecords = new ArrayList<>(); // The final field is the control field - expectedRecords.add(new String[]{"time", "value", "."}); - expectedRecords.add(new String[]{"4", "4.0", ""}); - expectedRecords.add(new String[]{"5", "5.0", ""}); - expectedRecords.add(new String[]{"3", "3.0", ""}); - expectedRecords.add(new String[]{"4", "4.0", ""}); + expectedRecords.add(new String[] { "time", "value", "." }); + expectedRecords.add(new String[] { "4", "4.0", "" }); + expectedRecords.add(new String[] { "5", "5.0", "" }); + expectedRecords.add(new String[] { "3", "3.0", "" }); + expectedRecords.add(new String[] { "4", "4.0", "" }); assertWrittenRecordsEqualTo(expectedRecords); verify(dataCountsReporter, times(1)).reportOutOfOrderRecord(2); @@ -240,8 +236,9 @@ public void testWrite_GivenTimeFormatIsEpochAndSomeTimestampsWithinLatencySomeOu } public void testWrite_GivenMalformedJsonWithoutNestedLevels() throws Exception { - AnalysisConfig.Builder builder = - new AnalysisConfig.Builder(Collections.singletonList(new Detector.Builder("metric", "value").build())); + AnalysisConfig.Builder builder = new AnalysisConfig.Builder( + Collections.singletonList(new Detector.Builder("metric", "value").build()) + ); builder.setLatency(TimeValue.timeValueSeconds(2)); analysisConfig = builder.build(); @@ -257,18 +254,17 @@ public void testWrite_GivenMalformedJsonWithoutNestedLevels() throws Exception { List expectedRecords = new ArrayList<>(); // The final field is the control field - expectedRecords.add(new String[]{"time", "value", "."}); - expectedRecords.add(new String[]{"1", "1.0", ""}); - expectedRecords.add(new String[]{"2", "", ""}); - expectedRecords.add(new String[]{"3", "3.0", ""}); + expectedRecords.add(new String[] { "time", "value", "." }); + expectedRecords.add(new String[] { "1", "1.0", "" }); + expectedRecords.add(new String[] { "2", "", "" }); + expectedRecords.add(new String[] { "3", "3.0", "" }); assertWrittenRecordsEqualTo(expectedRecords); verify(dataCountsReporter).reportMissingFields(1); verify(dataCountsReporter).finishReporting(); } - public void testWrite_GivenMalformedJsonWithNestedLevels() - throws Exception { + public void testWrite_GivenMalformedJsonWithNestedLevels() throws Exception { Detector detector = new Detector.Builder("metric", "nested.value").build(); AnalysisConfig.Builder builder = new AnalysisConfig.Builder(Collections.singletonList(detector)); builder.setLatency(TimeValue.timeValueSeconds(2)); @@ -286,17 +282,16 @@ public void testWrite_GivenMalformedJsonWithNestedLevels() List expectedRecords = new ArrayList<>(); // The final field is the control field - expectedRecords.add(new String[]{"time", "nested.value", "."}); - expectedRecords.add(new String[]{"1", "1.0", ""}); - expectedRecords.add(new String[]{"2", "2.0", ""}); - expectedRecords.add(new String[]{"3", "3.0", ""}); + expectedRecords.add(new String[] { "time", "nested.value", "." }); + expectedRecords.add(new String[] { "1", "1.0", "" }); + expectedRecords.add(new String[] { "2", "2.0", "" }); + expectedRecords.add(new String[] { "3", "3.0", "" }); assertWrittenRecordsEqualTo(expectedRecords); verify(dataCountsReporter).finishReporting(); } - public void testWrite_GivenMalformedJsonThatNeverRecovers() - throws Exception { + public void testWrite_GivenMalformedJsonThatNeverRecovers() throws Exception { AnalysisConfig.Builder builder = new AnalysisConfig.Builder(Collections.singletonList(new Detector.Builder("count", null).build())); builder.setLatency(TimeValue.timeValueSeconds(2)); analysisConfig = builder.build(); @@ -308,13 +303,13 @@ public void testWrite_GivenMalformedJsonThatNeverRecovers() JsonDataToProcessWriter writer = createWriter(); writer.writeHeader(); - ESTestCase.expectThrows(ElasticsearchParseException.class, - () -> writer.write(inputStream, null, XContentType.JSON, (r, e) -> {})); + ESTestCase.expectThrows(ElasticsearchParseException.class, () -> writer.write(inputStream, null, XContentType.JSON, (r, e) -> {})); } public void testWrite_GivenJsonWithArrayField() throws Exception { - AnalysisConfig.Builder builder = - new AnalysisConfig.Builder(Collections.singletonList(new Detector.Builder("metric", "value").build())); + AnalysisConfig.Builder builder = new AnalysisConfig.Builder( + Collections.singletonList(new Detector.Builder("metric", "value").build()) + ); builder.setLatency(TimeValue.timeValueSeconds(2)); analysisConfig = builder.build(); @@ -329,17 +324,18 @@ public void testWrite_GivenJsonWithArrayField() throws Exception { List expectedRecords = new ArrayList<>(); // The final field is the control field - expectedRecords.add(new String[]{"time", "value", "."}); - expectedRecords.add(new String[]{"1", "1.0", ""}); - expectedRecords.add(new String[]{"2", "2.0", ""}); + expectedRecords.add(new String[] { "time", "value", "." }); + expectedRecords.add(new String[] { "1", "1.0", "" }); + expectedRecords.add(new String[] { "2", "2.0", "" }); assertWrittenRecordsEqualTo(expectedRecords); verify(dataCountsReporter).finishReporting(); } public void testWrite_GivenJsonWithMissingFields() throws Exception { - AnalysisConfig.Builder builder = - new AnalysisConfig.Builder(Collections.singletonList(new Detector.Builder("metric", "value").build())); + AnalysisConfig.Builder builder = new AnalysisConfig.Builder( + Collections.singletonList(new Detector.Builder("metric", "value").build()) + ); builder.setLatency(TimeValue.timeValueSeconds(2)); analysisConfig = builder.build(); @@ -358,11 +354,11 @@ public void testWrite_GivenJsonWithMissingFields() throws Exception { List expectedRecords = new ArrayList<>(); // The final field is the control field - expectedRecords.add(new String[]{"time", "value", "."}); - expectedRecords.add(new String[]{"1", "1.0", ""}); - expectedRecords.add(new String[]{"2", "2.0", ""}); - expectedRecords.add(new String[]{"3", "", ""}); - expectedRecords.add(new String[]{"4", "3.0", ""}); + expectedRecords.add(new String[] { "time", "value", "." }); + expectedRecords.add(new String[] { "1", "1.0", "" }); + expectedRecords.add(new String[] { "2", "2.0", "" }); + expectedRecords.add(new String[] { "3", "", "" }); + expectedRecords.add(new String[] { "4", "3.0", "" }); assertWrittenRecordsEqualTo(expectedRecords); verify(dataCountsReporter, times(1)).reportMissingFields(1L); @@ -403,9 +399,9 @@ public void testWrite_Smile() throws Exception { List expectedRecords = new ArrayList<>(); // The final field is the control field - expectedRecords.add(new String[]{"time", "value", "."}); - expectedRecords.add(new String[]{"1", "1.0", ""}); - expectedRecords.add(new String[]{"2", "2.0", ""}); + expectedRecords.add(new String[] { "time", "value", "." }); + expectedRecords.add(new String[] { "1", "1.0", "" }); + expectedRecords.add(new String[] { "2", "2.0", "" }); assertWrittenRecordsEqualTo(expectedRecords); verify(dataCountsReporter).finishReporting(); @@ -416,10 +412,17 @@ private static InputStream createInputStream(String input) { } private JsonDataToProcessWriter createWriter() { - boolean includeTokensField = MachineLearning.CATEGORIZATION_TOKENIZATION_IN_JAVA && - analysisConfig.getCategorizationFieldName() != null; - return new JsonDataToProcessWriter(true, includeTokensField, autodetectProcess, dataDescription.build(), analysisConfig, - dataCountsReporter, new NamedXContentRegistry(Collections.emptyList())); + boolean includeTokensField = MachineLearning.CATEGORIZATION_TOKENIZATION_IN_JAVA + && analysisConfig.getCategorizationFieldName() != null; + return new JsonDataToProcessWriter( + true, + includeTokensField, + autodetectProcess, + dataDescription.build(), + analysisConfig, + dataCountsReporter, + new NamedXContentRegistry(Collections.emptyList()) + ); } private void assertWrittenRecordsEqualTo(List expectedRecords) { diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/XContentRecordReaderTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/XContentRecordReaderTests.java index b323e0edde107..426ee4cd673af 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/XContentRecordReaderTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/XContentRecordReaderTests.java @@ -7,14 +7,15 @@ package org.elasticsearch.xpack.ml.job.process.autodetect.writer; import com.fasterxml.jackson.core.JsonParseException; + import org.apache.logging.log4j.Logger; import org.elasticsearch.ElasticsearchParseException; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.ml.job.process.CountingInputStream; import org.elasticsearch.xpack.ml.job.process.DataCountsReporter; @@ -35,8 +36,7 @@ public void testRead() throws JsonParseException, IOException { XContentParser parser = createParser(data); Map fieldMap = createFieldMap(); - XContentRecordReader reader = new XContentRecordReader(parser, fieldMap, - mock(Logger.class)); + XContentRecordReader reader = new XContentRecordReader(parser, fieldMap, mock(Logger.class)); String record[] = new String[3]; boolean gotFields[] = new boolean[3]; @@ -54,7 +54,6 @@ public void testRead() throws JsonParseException, IOException { assertEquals(-1, reader.read(record, gotFields)); } - public void testRead_GivenNestedField() throws JsonParseException, IOException { String data = "{\"a\":10, \"b\":20, \"c\":{\"d\":30, \"e\":40}}"; XContentParser parser = createParser(data); @@ -63,8 +62,7 @@ public void testRead_GivenNestedField() throws JsonParseException, IOException { fieldMap.put("b", 1); fieldMap.put("c.e", 2); - XContentRecordReader reader = new XContentRecordReader(parser, fieldMap, - mock(Logger.class)); + XContentRecordReader reader = new XContentRecordReader(parser, fieldMap, mock(Logger.class)); String record[] = new String[3]; boolean gotFields[] = new boolean[3]; @@ -77,7 +75,6 @@ public void testRead_GivenNestedField() throws JsonParseException, IOException { assertEquals(-1, reader.read(record, gotFields)); } - public void testRead_GivenSingleValueArrays() throws JsonParseException, IOException { String data = "{\"a\":[10], \"b\":20, \"c\":{\"d\":30, \"e\":[40]}}"; XContentParser parser = createParser(data); @@ -86,8 +83,7 @@ public void testRead_GivenSingleValueArrays() throws JsonParseException, IOExcep fieldMap.put("b", 1); fieldMap.put("c.e", 2); - XContentRecordReader reader = new XContentRecordReader(parser, fieldMap, - mock(Logger.class)); + XContentRecordReader reader = new XContentRecordReader(parser, fieldMap, mock(Logger.class)); String record[] = new String[3]; boolean gotFields[] = new boolean[3]; @@ -100,18 +96,15 @@ public void testRead_GivenSingleValueArrays() throws JsonParseException, IOExcep assertEquals(-1, reader.read(record, gotFields)); } - public void testRead_GivenMultiValueArrays() throws JsonParseException, IOException { - String data = "{\"a\":[10, 11], \"b\":20, \"c\":{\"d\":30, \"e\":[40, 50]}, " - + "\"f\":[\"a\", \"a\", \"a\", \"a\"], \"g\":20}"; + String data = "{\"a\":[10, 11], \"b\":20, \"c\":{\"d\":30, \"e\":[40, 50]}, " + "\"f\":[\"a\", \"a\", \"a\", \"a\"], \"g\":20}"; XContentParser parser = createParser(data); Map fieldMap = new HashMap<>(); fieldMap.put("a", 0); fieldMap.put("g", 1); fieldMap.put("c.e", 2); - XContentRecordReader reader = new XContentRecordReader(parser, fieldMap, - mock(Logger.class)); + XContentRecordReader reader = new XContentRecordReader(parser, fieldMap, mock(Logger.class)); String record[] = new String[3]; boolean gotFields[] = new boolean[3]; @@ -132,13 +125,11 @@ public void testRead_GivenMultiValueArrays() throws JsonParseException, IOExcept public void testRead_RecoverFromBadJson() throws JsonParseException, IOException { // no opening '{' - String data = "\"a\":10, \"b\":20, \"c\":30}\n{\"b\":21, \"a\":11, \"c\":31}\n" - + "{\"c\":32, \"b\":22, \"a\":12}"; + String data = "\"a\":10, \"b\":20, \"c\":30}\n{\"b\":21, \"a\":11, \"c\":31}\n" + "{\"c\":32, \"b\":22, \"a\":12}"; XContentParser parser = createParser(data); Map fieldMap = createFieldMap(); - XContentRecordReader reader = new XContentRecordReader(parser, fieldMap, - mock(Logger.class)); + XContentRecordReader reader = new XContentRecordReader(parser, fieldMap, mock(Logger.class)); String record[] = new String[3]; boolean gotFields[] = new boolean[3]; @@ -152,16 +143,13 @@ public void testRead_RecoverFromBadJson() throws JsonParseException, IOException assertEquals(-1, reader.read(record, gotFields)); } - public void testRead_RecoverFromBadNestedJson() throws JsonParseException, IOException { // nested object 'd' is missing a ',' - String data = "{\"a\":10, \"b\":20, \"c\":30}\n" - + "{\"b\":21, \"d\" : {\"ee\": 1 \"ff\":0}, \"a\":11, \"c\":31}"; + String data = "{\"a\":10, \"b\":20, \"c\":30}\n" + "{\"b\":21, \"d\" : {\"ee\": 1 \"ff\":0}, \"a\":11, \"c\":31}"; XContentParser parser = createParser(data); Map fieldMap = createFieldMap(); - XContentRecordReader reader = new XContentRecordReader(parser, fieldMap, - mock(Logger.class)); + XContentRecordReader reader = new XContentRecordReader(parser, fieldMap, mock(Logger.class)); String record[] = new String[3]; boolean gotFields[] = new boolean[3]; @@ -177,7 +165,6 @@ public void testRead_RecoverFromBadNestedJson() throws JsonParseException, IOExc assertEquals(-1, reader.read(record, gotFields)); } - public void testRead_HitParseErrorsLimit() throws JsonParseException, IOException { // missing a ':' String format = "{\"a\":1%1$d, \"b\"2%1$d, \"c\":3%1$d}\n"; @@ -189,8 +176,7 @@ public void testRead_HitParseErrorsLimit() throws JsonParseException, IOExceptio XContentParser parser = createParser(builder.toString()); Map fieldMap = createFieldMap(); - XContentRecordReader reader = new XContentRecordReader(parser, fieldMap, - mock(Logger.class)); + XContentRecordReader reader = new XContentRecordReader(parser, fieldMap, mock(Logger.class)); ESTestCase.expectThrows(ElasticsearchParseException.class, () -> readUntilError(reader)); } @@ -207,14 +193,16 @@ private void readUntilError(XContentRecordReader reader) throws IOException { public void testRead_givenControlCharacterInData() throws Exception { char controlChar = '\u0002'; - String data = "{\"a\":10, \"" + controlChar + "\" : 5, \"b\":20, \"c\":30}" - + "\n{\"b\":21, \"a\":11, \"c\":31}" + "\n{\"c\":32, \"b\":22, \"a\":12}\n"; + String data = "{\"a\":10, \"" + + controlChar + + "\" : 5, \"b\":20, \"c\":30}" + + "\n{\"b\":21, \"a\":11, \"c\":31}" + + "\n{\"c\":32, \"b\":22, \"a\":12}\n"; XContentParser parser = createParser(data); Map fieldMap = createFieldMap(); - XContentRecordReader reader = new XContentRecordReader(parser, fieldMap, - mock(Logger.class)); + XContentRecordReader reader = new XContentRecordReader(parser, fieldMap, mock(Logger.class)); String record[] = new String[3]; boolean gotFields[] = new boolean[3]; @@ -225,13 +213,10 @@ public void testRead_givenControlCharacterInData() throws Exception { } private XContentParser createParser(String input) throws JsonParseException, IOException { - ByteArrayInputStream inputStream = new ByteArrayInputStream( - input.getBytes(StandardCharsets.UTF_8)); - InputStream inputStream2 = new CountingInputStream(inputStream, - mock(DataCountsReporter.class)); + ByteArrayInputStream inputStream = new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8)); + InputStream inputStream2 = new CountingInputStream(inputStream, mock(DataCountsReporter.class)); return XContentFactory.xContent(XContentType.JSON) - .createParser(new NamedXContentRegistry(Collections.emptyList()), - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, inputStream2); + .createParser(new NamedXContentRegistry(Collections.emptyList()), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, inputStream2); } private Map createFieldMap() { diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/normalizer/BucketInfluencerNormalizableTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/normalizer/BucketInfluencerNormalizableTests.java index 103a57fccca95..8200549058336 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/normalizer/BucketInfluencerNormalizableTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/normalizer/BucketInfluencerNormalizableTests.java @@ -12,7 +12,6 @@ import java.util.Date; - public class BucketInfluencerNormalizableTests extends ESTestCase { private static final double EPSILON = 0.0001; private static final String INDEX_NAME = "foo-index"; @@ -86,8 +85,10 @@ public void testGetChildrenTypes() { } public void testGetChildren_ByType() { - expectThrows(UnsupportedOperationException.class, () -> new BucketInfluencerNormalizable(bucketInfluencer, INDEX_NAME) - .getChildren(Normalizable.ChildType.BUCKET_INFLUENCER)); + expectThrows( + UnsupportedOperationException.class, + () -> new BucketInfluencerNormalizable(bucketInfluencer, INDEX_NAME).getChildren(Normalizable.ChildType.BUCKET_INFLUENCER) + ); } public void testGetChildren() { @@ -95,9 +96,13 @@ public void testGetChildren() { } public void testSetMaxChildrenScore() { - expectThrows(UnsupportedOperationException.class, - () -> new BucketInfluencerNormalizable(bucketInfluencer, INDEX_NAME) - .setMaxChildrenScore(Normalizable.ChildType.BUCKET_INFLUENCER, 42.0)); + expectThrows( + UnsupportedOperationException.class, + () -> new BucketInfluencerNormalizable(bucketInfluencer, INDEX_NAME).setMaxChildrenScore( + Normalizable.ChildType.BUCKET_INFLUENCER, + 42.0 + ) + ); } public void testSetParentScore() { diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/normalizer/BucketNormalizableTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/normalizer/BucketNormalizableTests.java index 053450382682d..c52aec377b0e6 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/normalizer/BucketNormalizableTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/normalizer/BucketNormalizableTests.java @@ -16,7 +16,6 @@ import java.util.Date; import java.util.List; - public class BucketNormalizableTests extends ESTestCase { private static final String INDEX_NAME = "foo-index"; private static final double EPSILON = 0.0001; diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/normalizer/InfluencerNormalizableTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/normalizer/InfluencerNormalizableTests.java index 2c4ba324a59fc..512374502ebf1 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/normalizer/InfluencerNormalizableTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/normalizer/InfluencerNormalizableTests.java @@ -79,8 +79,10 @@ public void testGetChildrenTypes() { } public void testGetChildren_ByType() { - expectThrows(UnsupportedOperationException.class, () -> new InfluencerNormalizable(influencer, INDEX_NAME) - .getChildren(Normalizable.ChildType.BUCKET_INFLUENCER)); + expectThrows( + UnsupportedOperationException.class, + () -> new InfluencerNormalizable(influencer, INDEX_NAME).getChildren(Normalizable.ChildType.BUCKET_INFLUENCER) + ); } public void testGetChildren() { @@ -88,8 +90,10 @@ public void testGetChildren() { } public void testSetMaxChildrenScore() { - expectThrows(UnsupportedOperationException.class, () -> new InfluencerNormalizable(influencer, INDEX_NAME) - .setMaxChildrenScore(Normalizable.ChildType.BUCKET_INFLUENCER, 42.0)); + expectThrows( + UnsupportedOperationException.class, + () -> new InfluencerNormalizable(influencer, INDEX_NAME).setMaxChildrenScore(Normalizable.ChildType.BUCKET_INFLUENCER, 42.0) + ); } public void testSetParentScore() { diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/normalizer/NormalizerBuilderTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/normalizer/NormalizerBuilderTests.java index a28bce965262c..8f66179ed75ef 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/normalizer/NormalizerBuilderTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/normalizer/NormalizerBuilderTests.java @@ -19,7 +19,8 @@ public class NormalizerBuilderTests extends ESTestCase { public void testBuildNormalizerCommand() throws IOException { Environment env = TestEnvironment.newEnvironment( - Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build()); + Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build() + ); String jobId = "unit-test-job"; List command = new NormalizerBuilder(env, jobId, null, 300).build(); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/normalizer/NormalizerResultTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/normalizer/NormalizerResultTests.java index 56060196757b2..ef29e0805d90a 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/normalizer/NormalizerResultTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/normalizer/NormalizerResultTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.ml.job.process.normalizer; import org.elasticsearch.common.io.stream.Writeable.Reader; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; public class NormalizerResultTests extends AbstractSerializingTestCase { diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/normalizer/NormalizerTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/normalizer/NormalizerTests.java index e0690d7693ce7..bab892ac2d5be 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/normalizer/NormalizerTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/normalizer/NormalizerTests.java @@ -23,7 +23,6 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; - public class NormalizerTests extends ESTestCase { private static final String JOB_ID = "foo"; private static final String INDEX_NAME = "foo-index"; @@ -49,8 +48,9 @@ public void testNormalize() throws IOException, InterruptedException { ExecutorService threadpool = Executors.newScheduledThreadPool(1); try { NormalizerProcessFactory processFactory = mock(NormalizerProcessFactory.class); - when(processFactory.createNormalizerProcess(eq(JOB_ID), eq(QUANTILES_STATE), eq(BUCKET_SPAN), any())) - .thenReturn(new MultiplyingNormalizerProcess(FACTOR)); + when(processFactory.createNormalizerProcess(eq(JOB_ID), eq(QUANTILES_STATE), eq(BUCKET_SPAN), any())).thenReturn( + new MultiplyingNormalizerProcess(FACTOR) + ); Normalizer normalizer = new Normalizer(JOB_ID, processFactory, threadpool); Bucket bucket = generateBucket(new Date(0)); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/normalizer/ScoresUpdaterTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/normalizer/ScoresUpdaterTests.java index a76735bab63a6..2363094a1f8ad 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/normalizer/ScoresUpdaterTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/normalizer/ScoresUpdaterTests.java @@ -17,8 +17,8 @@ import org.elasticsearch.xpack.core.ml.job.results.BucketInfluencer; import org.elasticsearch.xpack.core.ml.job.results.Influencer; import org.elasticsearch.xpack.core.ml.job.results.Result; -import org.elasticsearch.xpack.ml.job.persistence.JobResultsProvider; import org.elasticsearch.xpack.ml.job.persistence.JobRenormalizedResultsPersister; +import org.elasticsearch.xpack.ml.job.persistence.JobResultsProvider; import org.elasticsearch.xpack.ml.job.persistence.MockBatchedDocumentsIterator; import org.junit.Before; import org.mockito.MockitoAnnotations; @@ -191,7 +191,7 @@ public void testUpdate_GivenTwoBucketsWithFirstHavingEnoughRecordsToForceSecondN bucket1.addBucketInfluencer(createTimeBucketInfluencer(bucket1.getTimestamp(), 0.04, 42.0)); List> records = new ArrayList<>(); Date date = new Date(); - for (int i=0; i<100000; i++) { + for (int i = 0; i < 100000; i++) { records.add(new Result<>("foo", new AnomalyRecord("foo", date, 1))); } @@ -204,11 +204,12 @@ public void testUpdate_GivenTwoBucketsWithFirstHavingEnoughRecordsToForceSecondN batch.add(bucket2); givenProviderReturnsBuckets(batch); - List>> recordBatches = new ArrayList<>(); recordBatches.add(new ArrayDeque<>(records)); MockBatchedDocumentsIterator recordIter = new MockBatchedDocumentsIterator<>( - recordBatches, AnomalyRecord.RESULT_TYPE_VALUE); + recordBatches, + AnomalyRecord.RESULT_TYPE_VALUE + ); recordIter.requireIncludeInterim(false); when(jobResultsProvider.newBatchedRecordsIterator(JOB_ID)).thenReturn(recordIter); @@ -330,6 +331,7 @@ private static AnomalyRecord createRecord() { private void givenNormalizerFactoryReturnsMock() { when(normalizerFactory.create(JOB_ID)).thenReturn(normalizer); } + private void givenProviderReturnsNoBuckets() { givenBuckets(Collections.emptyList()); } @@ -392,7 +394,9 @@ private void givenProviderReturnsRecords(Deque records) { batches.add(batch); MockBatchedDocumentsIterator recordIter = new MockBatchedDocumentsIterator<>( - batches, AnomalyRecord.RESULT_TYPE_VALUE); + batches, + AnomalyRecord.RESULT_TYPE_VALUE + ); recordIter.requireIncludeInterim(false); when(jobResultsProvider.newBatchedRecordsIterator(JOB_ID)).thenReturn(recordIter); } @@ -415,9 +419,7 @@ private void givenProviderReturnsInfluencers(Deque influencers) { private void verifyNormalizerWasInvoked(int times) throws IOException { int bucketSpan = job.getAnalysisConfig() == null ? 0 : ((Long) job.getAnalysisConfig().getBucketSpan().seconds()).intValue(); - verify(normalizer, times(times)).normalize( - eq(bucketSpan), anyListOf(Normalizable.class), - eq(QUANTILES_STATE)); + verify(normalizer, times(times)).normalize(eq(bucketSpan), anyListOf(Normalizable.class), eq(QUANTILES_STATE)); } private void verifyNothingWasUpdated() { diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/normalizer/ShortCircuitingRenormalizerTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/normalizer/ShortCircuitingRenormalizerTests.java index 716c91f1bdc79..02c4830a6e60f 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/normalizer/ShortCircuitingRenormalizerTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/normalizer/ShortCircuitingRenormalizerTests.java @@ -24,7 +24,6 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; - public class ShortCircuitingRenormalizerTests extends ESTestCase { private static final String JOB_ID = "foo"; @@ -77,8 +76,10 @@ public void testNormalize() throws InterruptedException { // The quantiles immediately before the intermediate wait for idle must have been processed int intermediateWaitPoint = TEST_SIZE / 2 - 1; - assertTrue(quantilesUsed + " should contain " + intermediateWaitPoint, - quantilesUsed.contains(Integer.toString(intermediateWaitPoint))); + assertTrue( + quantilesUsed + " should contain " + intermediateWaitPoint, + quantilesUsed.contains(Integer.toString(intermediateWaitPoint)) + ); } finally { threadpool.shutdown(); } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/normalizer/output/NormalizerResultHandlerTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/normalizer/output/NormalizerResultHandlerTests.java index 0094ad05b779b..c1da240b202a5 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/normalizer/output/NormalizerResultHandlerTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/normalizer/output/NormalizerResultHandlerTests.java @@ -22,14 +22,14 @@ public class NormalizerResultHandlerTests extends ESTestCase { public void testParse() throws IOException { String testData = "{\"level\":\"leaf\",\"partition_field_name\":\"part\",\"partition_field_value\":\"v1\"," - + "\"person_field_name\":\"pers\",\"function_name\":\"f\"," - + "\"value_field_name\":\"x\",\"probability\":0.01,\"normalized_score\":88.88}\n" - + "{\"level\":\"leaf\",\"partition_field_name\":\"part\",\"partition_field_value\":\"v2\"," - + "\"person_field_name\":\"pers\",\"function_name\":\"f\"," - + "\"value_field_name\":\"x\",\"probability\":0.02,\"normalized_score\":44.44}\n" - + "{\"level\":\"leaf\",\"partition_field_name\":\"part\",\"partition_field_value\":\"v3\"," - + "\"person_field_name\":\"pers\",\"function_name\":\"f\"," - + "\"value_field_name\":\"x\",\"probability\":0.03,\"normalized_score\":22.22}\n"; + + "\"person_field_name\":\"pers\",\"function_name\":\"f\"," + + "\"value_field_name\":\"x\",\"probability\":0.01,\"normalized_score\":88.88}\n" + + "{\"level\":\"leaf\",\"partition_field_name\":\"part\",\"partition_field_value\":\"v2\"," + + "\"person_field_name\":\"pers\",\"function_name\":\"f\"," + + "\"value_field_name\":\"x\",\"probability\":0.02,\"normalized_score\":44.44}\n" + + "{\"level\":\"leaf\",\"partition_field_name\":\"part\",\"partition_field_value\":\"v3\"," + + "\"person_field_name\":\"pers\",\"function_name\":\"f\"," + + "\"value_field_name\":\"x\",\"probability\":0.03,\"normalized_score\":22.22}\n"; InputStream is = new ByteArrayInputStream(testData.getBytes(StandardCharsets.UTF_8)); NormalizerResultHandler handler = new NormalizerResultHandler(is); @@ -41,4 +41,3 @@ public void testParse() throws IOException { assertEquals(22.22, results.get(2).getNormalizedScore(), EPSILON); } } - diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/AutodetectResultTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/AutodetectResultTests.java index 74b01812d0b97..43d69be8b5dbe 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/AutodetectResultTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/AutodetectResultTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.ml.job.results; import org.elasticsearch.common.io.stream.Writeable.Reader; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.annotations.Annotation; import org.elasticsearch.xpack.core.ml.annotations.AnnotationTests; import org.elasticsearch.xpack.core.ml.job.process.autodetect.output.FlushAcknowledgement; @@ -71,8 +71,13 @@ protected AutodetectResult createTestInstance() { int size = randomInt(10); influencers = new ArrayList<>(size); for (int i = 0; i < size; i++) { - Influencer influencer = new Influencer(jobId, randomAlphaOfLength(10), randomAlphaOfLength(10), - randomDate(), randomNonNegativeLong()); + Influencer influencer = new Influencer( + jobId, + randomAlphaOfLength(10), + randomAlphaOfLength(10), + randomDate(), + randomNonNegativeLong() + ); influencer.setProbability(randomDoubleBetween(0.0, 1.0, true)); influencers.add(influencer); } @@ -103,8 +108,7 @@ protected AutodetectResult createTestInstance() { annotation = null; } if (randomBoolean()) { - forecast = new Forecast(jobId, randomAlphaOfLength(20), randomDate(), - randomNonNegativeLong(), randomInt()); + forecast = new Forecast(jobId, randomAlphaOfLength(20), randomDate(), randomNonNegativeLong(), randomInt()); } else { forecast = null; } @@ -129,9 +133,21 @@ protected AutodetectResult createTestInstance() { } else { flushAcknowledgement = null; } - return new AutodetectResult(bucket, records, influencers, quantiles, modelSnapshot, - modelSizeStats == null ? null : modelSizeStats.build(), modelPlot, annotation, forecast, forecastRequestStats, - categoryDefinition, categorizerStats == null ? null : categorizerStats.build(), flushAcknowledgement); + return new AutodetectResult( + bucket, + records, + influencers, + quantiles, + modelSnapshot, + modelSizeStats == null ? null : modelSizeStats.build(), + modelPlot, + annotation, + forecast, + forecastRequestStats, + categoryDefinition, + categorizerStats == null ? null : categorizerStats.build(), + flushAcknowledgement + ); } @Override diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/BucketTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/BucketTests.java index b237d0922c8a1..23fd71f529ea6 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/BucketTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/BucketTests.java @@ -7,9 +7,9 @@ package org.elasticsearch.xpack.ml.job.results; import org.elasticsearch.common.io.stream.Writeable.Reader; +import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xpack.core.ml.job.results.AnomalyRecord; import org.elasticsearch.xpack.core.ml.job.results.AnomalyRecordTests; import org.elasticsearch.xpack.core.ml.job.results.Bucket; @@ -147,8 +147,7 @@ public void testEquals_GivenDifferentNumberOfRecords() { Bucket bucket1 = new Bucket("foo", new Date(123), 123); bucket1.setRecords(Collections.singletonList(new AnomalyRecord("foo", new Date(123), 123))); Bucket bucket2 = new Bucket("foo", new Date(123), 123); - bucket2.setRecords(Arrays.asList(new AnomalyRecord("foo", new Date(123), 123), - new AnomalyRecord("foo", new Date(123), 123))); + bucket2.setRecords(Arrays.asList(new AnomalyRecord("foo", new Date(123), 123), new AnomalyRecord("foo", new Date(123), 123))); assertFalse(bucket1.equals(bucket2)); assertFalse(bucket2.equals(bucket1)); @@ -244,7 +243,7 @@ public void testIsNormalizable_GivenAnomalyScoreIsNonZeroAndRecordCountIsNonZero assertTrue(bucket.isNormalizable()); } - public void testId() { + public void testId() { Bucket bucket = new Bucket("foo", new Date(123), 60L); assertEquals("foo_bucket_123_60", bucket.getId()); } @@ -260,8 +259,7 @@ public void testCopyConstructor() { public void testStrictParser() throws IOException { String json = "{\"job_id\":\"job_1\", \"timestamp\": 123544456, \"bucket_span\": 3600, \"foo\":\"bar\"}"; try (XContentParser parser = createParser(JsonXContent.jsonXContent, json)) { - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> Bucket.STRICT_PARSER.apply(parser, null)); + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> Bucket.STRICT_PARSER.apply(parser, null)); assertThat(e.getMessage(), containsString("unknown field [foo]")); } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/CategoryDefinitionTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/CategoryDefinitionTests.java index e6eb545597ee0..eca0c8a342f37 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/CategoryDefinitionTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/CategoryDefinitionTests.java @@ -8,9 +8,9 @@ import org.elasticsearch.Version; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.ml.AbstractBWCSerializationTestCase; import org.elasticsearch.xpack.core.ml.job.results.CategoryDefinition; @@ -155,8 +155,10 @@ private static CategoryDefinition createFullyPopulatedCategoryDefinition() { public void testStrictParser() throws IOException { String json = "{\"job_id\":\"job_1\", \"foo\":\"bar\"}"; try (XContentParser parser = createParser(JsonXContent.jsonXContent, json)) { - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> CategoryDefinition.STRICT_PARSER.apply(parser, null)); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> CategoryDefinition.STRICT_PARSER.apply(parser, null) + ); assertThat(e.getMessage(), containsString("unknown field [foo]")); } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/ForecastRequestStatsTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/ForecastRequestStatsTests.java index 6c39efefaf36b..8361ecaa6b1f4 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/ForecastRequestStatsTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/ForecastRequestStatsTests.java @@ -7,9 +7,9 @@ package org.elasticsearch.xpack.ml.job.results; import org.elasticsearch.common.io.stream.Writeable.Reader; +import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xpack.core.ml.job.results.ForecastRequestStats; import org.elasticsearch.xpack.core.ml.job.results.ForecastRequestStats.ForecastRequestStatus; @@ -86,8 +86,10 @@ protected ForecastRequestStats doParseInstance(XContentParser parser) { public void testStrictParser() throws IOException { String json = "{\"job_id\":\"job_1\", \"forecast_id\":\"forecast_1\", \"foo\":\"bar\"}"; try (XContentParser parser = createParser(JsonXContent.jsonXContent, json)) { - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> ForecastRequestStats.STRICT_PARSER.apply(parser, null)); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> ForecastRequestStats.STRICT_PARSER.apply(parser, null) + ); assertThat(e.getMessage(), containsString("unknown field [foo]")); } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/ForecastTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/ForecastTests.java index df27559c3217c..57586bf86b63e 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/ForecastTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/ForecastTests.java @@ -7,9 +7,9 @@ package org.elasticsearch.xpack.ml.job.results; import org.elasticsearch.common.io.stream.Writeable.Reader; +import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xpack.core.ml.MachineLearningField; import org.elasticsearch.xpack.core.ml.job.results.Forecast; @@ -26,9 +26,7 @@ protected Forecast createTestInstance() { } public Forecast createTestInstance(String jobId) { - Forecast forecast = - new Forecast(jobId, randomAlphaOfLength(20), randomDate(), - randomNonNegativeLong(), randomInt()); + Forecast forecast = new Forecast(jobId, randomAlphaOfLength(20), randomDate(), randomNonNegativeLong(), randomInt()); if (randomBoolean()) { forecast.setByFieldName(randomAlphaOfLengthBetween(1, 20)); @@ -89,11 +87,10 @@ public void testId() { } public void testStrictParser() throws IOException { - String json = "{\"job_id\":\"job_1\", \"forecast_id\":\"forecast_1\", \"timestamp\":12354667, \"bucket_span\": 3600," + - "\"detector_index\":3, \"foo\":\"bar\"}"; + String json = "{\"job_id\":\"job_1\", \"forecast_id\":\"forecast_1\", \"timestamp\":12354667, \"bucket_span\": 3600," + + "\"detector_index\":3, \"foo\":\"bar\"}"; try (XContentParser parser = createParser(JsonXContent.jsonXContent, json)) { - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> Forecast.STRICT_PARSER.apply(parser, null)); + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> Forecast.STRICT_PARSER.apply(parser, null)); assertThat(e.getMessage(), containsString("unknown field [foo]")); } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/InfluenceTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/InfluenceTests.java index 06959551ff730..4e1c59262e835 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/InfluenceTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/InfluenceTests.java @@ -7,9 +7,9 @@ package org.elasticsearch.xpack.ml.job.results; import org.elasticsearch.common.io.stream.Writeable.Reader; +import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xpack.core.ml.job.results.Influence; import java.io.IOException; @@ -43,8 +43,7 @@ protected Influence doParseInstance(XContentParser parser) { public void testStrictParser() throws IOException { String json = "{\"influencer_field_name\":\"influencer_1\", \"influencer_field_values\":[], \"foo\":\"bar\"}"; try (XContentParser parser = createParser(JsonXContent.jsonXContent, json)) { - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> Influence.STRICT_PARSER.apply(parser, null)); + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> Influence.STRICT_PARSER.apply(parser, null)); assertThat(e.getMessage(), containsString("unknown field [foo]")); } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/ModelPlotTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/ModelPlotTests.java index 3463c4c503ede..f14449691142c 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/ModelPlotTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/ModelPlotTests.java @@ -8,11 +8,11 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.Writeable.Reader; +import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xpack.core.ml.MachineLearningField; import org.elasticsearch.xpack.core.ml.job.results.ModelPlot; @@ -34,8 +34,7 @@ protected ModelPlot createTestInstance() { } public ModelPlot createTestInstance(String jobId) { - ModelPlot modelPlot = - new ModelPlot(jobId, randomDate(), randomNonNegativeLong(), randomInt()); + ModelPlot modelPlot = new ModelPlot(jobId, randomDate(), randomNonNegativeLong(), randomInt()); if (randomBoolean()) { modelPlot.setByFieldName(randomAlphaOfLengthBetween(1, 20)); } @@ -77,17 +76,13 @@ protected ModelPlot doParseInstance(XContentParser parser) { } public void testEquals_GivenSameObject() { - ModelPlot modelPlot = - new ModelPlot(randomAlphaOfLength(15), - randomDate(), randomNonNegativeLong(), randomInt()); + ModelPlot modelPlot = new ModelPlot(randomAlphaOfLength(15), randomDate(), randomNonNegativeLong(), randomInt()); assertTrue(modelPlot.equals(modelPlot)); } public void testEquals_GivenObjectOfDifferentClass() { - ModelPlot modelPlot = - new ModelPlot(randomAlphaOfLength(15), - randomDate(), randomNonNegativeLong(), randomInt()); + ModelPlot modelPlot = new ModelPlot(randomAlphaOfLength(15), randomDate(), randomNonNegativeLong(), randomInt()); assertFalse(modelPlot.equals("a string")); } @@ -248,8 +243,7 @@ public void testId() { public void testStrictParser() throws IOException { String json = "{\"job_id\":\"job_1\", \"timestamp\":12354667, \"bucket_span\": 3600, \"detector_index\":3, \"foo\":\"bar\"}"; try (XContentParser parser = createParser(JsonXContent.jsonXContent, json)) { - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> ModelPlot.STRICT_PARSER.apply(parser, null)); + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> ModelPlot.STRICT_PARSER.apply(parser, null)); assertThat(e.getMessage(), containsString("unknown field [foo]")); } @@ -265,16 +259,99 @@ public void testLenientParser() throws IOException { public void testIdUniqueness() { ModelPlot modelPlot = new ModelPlot("foo", new Date(), 3600, 0); - String[] partitionFieldValues = { "730", "132", "358", "552", "888", "236", "224", "674", - "438", "128", "722", "560", "228", "628", "226", "656" }; - String[] byFieldValues = { "S000", "S001", "S002", "S003", "S004", "S005", "S006", "S007", "S008", "S009", - "S010", "S011", "S012", "S013", "S014", "S015", "S016", "S017", "S018", "S019", - "S020", "S021", "S022", "S023", "S024", "S025", "S026", "S027", "S028", "S029", - "S057", "S058", "S059", "M020", "M021", "M026", "M027", "M028", "M029", "M030", - "M031", "M032", "M033", "M056", "M057", "M058", "M059", "M060", "M061", "M062", - "M063", "M086", "M087", "M088", "M089", "M090", "M091", "M092", "M093", "M116", - "M117", "M118", "M119", "L012", "L013", "L014", "L017", "L018", "L019", "L023", - "L024", "L025", "L029", "L030", "L031" }; + String[] partitionFieldValues = { + "730", + "132", + "358", + "552", + "888", + "236", + "224", + "674", + "438", + "128", + "722", + "560", + "228", + "628", + "226", + "656" }; + String[] byFieldValues = { + "S000", + "S001", + "S002", + "S003", + "S004", + "S005", + "S006", + "S007", + "S008", + "S009", + "S010", + "S011", + "S012", + "S013", + "S014", + "S015", + "S016", + "S017", + "S018", + "S019", + "S020", + "S021", + "S022", + "S023", + "S024", + "S025", + "S026", + "S027", + "S028", + "S029", + "S057", + "S058", + "S059", + "M020", + "M021", + "M026", + "M027", + "M028", + "M029", + "M030", + "M031", + "M032", + "M033", + "M056", + "M057", + "M058", + "M059", + "M060", + "M061", + "M062", + "M063", + "M086", + "M087", + "M088", + "M089", + "M090", + "M091", + "M092", + "M093", + "M116", + "M117", + "M118", + "M119", + "L012", + "L013", + "L014", + "L017", + "L018", + "L019", + "L023", + "L024", + "L025", + "L029", + "L030", + "L031" }; Map> uniqueIds = new HashMap<>(); @@ -284,14 +361,14 @@ public void testIdUniqueness() { modelPlot.setByFieldValue(byFieldValue); String id = modelPlot.getId(); uniqueIds.compute(id, (k, v) -> { - if (v == null) { - v = new ArrayList<>(); - } - v.add(partitionFieldValue + "/" + byFieldValue); - if (v.size() > 1) { - logger.error("Duplicates for ID [" + id + "]: " + v); - } - return v; + if (v == null) { + v = new ArrayList<>(); + } + v.add(partitionFieldValue + "/" + byFieldValue); + if (v.size() > 1) { + logger.error("Duplicates for ID [" + id + "]: " + v); + } + return v; }); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/OverallBucketTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/OverallBucketTests.java index 828f2495ed2dd..380357ecc0b59 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/OverallBucketTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/OverallBucketTests.java @@ -27,11 +27,13 @@ protected OverallBucket createTestInstance() { for (int i = 0; i < jobCount; ++i) { jobs.add(new OverallBucket.JobInfo(JobTests.randomValidJobId(), randomDoubleBetween(0.0, 100.0, true))); } - return new OverallBucket(new Date(randomLongBetween(0, 3000000000000L)), - randomIntBetween(60, 24 * 3600), - randomDoubleBetween(0.0, 100.0, true), - jobs, - randomBoolean()); + return new OverallBucket( + new Date(randomLongBetween(0, 3000000000000L)), + randomIntBetween(60, 24 * 3600), + randomDoubleBetween(0.0, 100.0, true), + jobs, + randomBoolean() + ); } @Override diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/retention/AbstractExpiredJobDataRemoverTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/retention/AbstractExpiredJobDataRemoverTests.java index 905a43bf1dd29..da73ff6cefb40 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/retention/AbstractExpiredJobDataRemoverTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/retention/AbstractExpiredJobDataRemoverTests.java @@ -13,13 +13,13 @@ import org.elasticsearch.client.OriginSettingClient; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.tasks.TaskId; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.ClientHelper; import org.elasticsearch.xpack.core.ml.job.config.Job; import org.elasticsearch.xpack.core.ml.job.config.JobTests; @@ -91,8 +91,11 @@ static SearchResponse createSearchResponse(List toXContent } static SearchResponse createSearchResponseFromHits(List hits) { - SearchHits searchHits = new SearchHits(hits.toArray(new SearchHit[]{}), - new TotalHits(hits.size(), TotalHits.Relation.EQUAL_TO), 1.0f); + SearchHits searchHits = new SearchHits( + hits.toArray(new SearchHit[] {}), + new TotalHits(hits.size(), TotalHits.Relation.EQUAL_TO), + 1.0f + ); SearchResponse searchResponse = mock(SearchResponse.class); when(searchResponse.getHits()).thenReturn(searchHits); return searchResponse; diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/retention/EmptyStateIndexRemoverTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/retention/EmptyStateIndexRemoverTests.java index 81895ebd6bdb3..ae075227d8cdb 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/retention/EmptyStateIndexRemoverTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/retention/EmptyStateIndexRemoverTests.java @@ -92,10 +92,16 @@ public void testRemove_NoEmptyStateIndices() { IndicesStatsResponse indicesStatsResponse = mock(IndicesStatsResponse.class); doReturn( Map.of( - ".ml-state-a", indexStats(".ml-state-a", 1), - ".ml-state-b", indexStats(".ml-state-b", 2), - ".ml-state-c", indexStats(".ml-state-c", 1), - ".ml-state-d", indexStats(".ml-state-d", 2))).when(indicesStatsResponse).getIndices(); + ".ml-state-a", + indexStats(".ml-state-a", 1), + ".ml-state-b", + indexStats(".ml-state-b", 2), + ".ml-state-c", + indexStats(".ml-state-c", 1), + ".ml-state-d", + indexStats(".ml-state-d", 2) + ) + ).when(indicesStatsResponse).getIndices(); doAnswer(withResponse(indicesStatsResponse)).when(client).execute(eq(IndicesStatsAction.INSTANCE), any(), any()); remover.remove(1.0f, listener, () -> false); @@ -109,11 +115,18 @@ private void assertDeleteActionExecuted(boolean acknowledged) { IndicesStatsResponse indicesStatsResponse = mock(IndicesStatsResponse.class); doReturn( Map.of( - ".ml-state-a", indexStats(".ml-state-a", 1), - ".ml-state-b", indexStats(".ml-state-b", 0), - ".ml-state-c", indexStats(".ml-state-c", 2), - ".ml-state-d", indexStats(".ml-state-d", 0), - ".ml-state-e", indexStats(".ml-state-e", 0))).when(indicesStatsResponse).getIndices(); + ".ml-state-a", + indexStats(".ml-state-a", 1), + ".ml-state-b", + indexStats(".ml-state-b", 0), + ".ml-state-c", + indexStats(".ml-state-c", 2), + ".ml-state-d", + indexStats(".ml-state-d", 0), + ".ml-state-e", + indexStats(".ml-state-e", 0) + ) + ).when(indicesStatsResponse).getIndices(); doAnswer(withResponse(indicesStatsResponse)).when(client).execute(eq(IndicesStatsAction.INSTANCE), any(), any()); GetIndexResponse getIndexResponse = new GetIndexResponse(new String[] { ".ml-state-e" }, null, null, null, null, null); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/retention/ExpiredAnnotationsRemoverTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/retention/ExpiredAnnotationsRemoverTests.java index 769c2ebf3e14a..d3f8c705d5a24 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/retention/ExpiredAnnotationsRemoverTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/retention/ExpiredAnnotationsRemoverTests.java @@ -72,10 +72,7 @@ public void testRemove_GivenNoJobs() { public void testRemove_GivenJobsWithoutRetentionPolicy() { givenDBQRequestsSucceed(); - List jobs = Arrays.asList( - JobTests.buildJobBuilder("foo").build(), - JobTests.buildJobBuilder("bar").build() - ); + List jobs = Arrays.asList(JobTests.buildJobBuilder("foo").build(), JobTests.buildJobBuilder("bar").build()); createExpiredAnnotationsRemover(jobs.iterator()).remove(1.0f, listener, () -> false); @@ -89,15 +86,16 @@ public void testRemove_GivenJobsWithAndWithoutRetentionPolicy() { List jobs = Arrays.asList( JobTests.buildJobBuilder("none").build(), JobTests.buildJobBuilder("annotations-1").setResultsRetentionDays(10L).build(), - JobTests.buildJobBuilder("annotations-2").setResultsRetentionDays(20L).build()); + JobTests.buildJobBuilder("annotations-2").setResultsRetentionDays(20L).build() + ); createExpiredAnnotationsRemover(jobs.iterator()).remove(1.0f, listener, () -> false); assertThat(capturedDeleteByQueryRequests.size(), equalTo(2)); DeleteByQueryRequest dbqRequest = capturedDeleteByQueryRequests.get(0); - assertThat(dbqRequest.indices(), equalTo(new String[] {AnnotationIndex.READ_ALIAS_NAME})); + assertThat(dbqRequest.indices(), equalTo(new String[] { AnnotationIndex.READ_ALIAS_NAME })); dbqRequest = capturedDeleteByQueryRequests.get(1); - assertThat(dbqRequest.indices(), equalTo(new String[] {AnnotationIndex.READ_ALIAS_NAME})); + assertThat(dbqRequest.indices(), equalTo(new String[] { AnnotationIndex.READ_ALIAS_NAME })); verify(listener).onResponse(true); } @@ -126,12 +124,13 @@ public void testRemove_GivenClientRequestsFailed() { List jobs = Arrays.asList( JobTests.buildJobBuilder("none").build(), JobTests.buildJobBuilder("annotations-1").setResultsRetentionDays(10L).build(), - JobTests.buildJobBuilder("annotations-2").setResultsRetentionDays(20L).build()); + JobTests.buildJobBuilder("annotations-2").setResultsRetentionDays(20L).build() + ); createExpiredAnnotationsRemover(jobs.iterator()).remove(1.0f, listener, () -> false); assertThat(capturedDeleteByQueryRequests.size(), equalTo(1)); DeleteByQueryRequest dbqRequest = capturedDeleteByQueryRequests.get(0); - assertThat(dbqRequest.indices(), equalTo(new String[] {AnnotationIndex.READ_ALIAS_NAME})); + assertThat(dbqRequest.indices(), equalTo(new String[] { AnnotationIndex.READ_ALIAS_NAME })); verify(listener).onFailure(any()); } @@ -162,19 +161,17 @@ private void givenDBQRequestsFailed() { @SuppressWarnings("unchecked") private void givenDBQRequest(boolean shouldSucceed) { doAnswer(invocationOnMock -> { - capturedDeleteByQueryRequests.add((DeleteByQueryRequest) invocationOnMock.getArguments()[1]); - ActionListener listener = - (ActionListener) invocationOnMock.getArguments()[2]; - if (shouldSucceed) { - BulkByScrollResponse bulkByScrollResponse = mock(BulkByScrollResponse.class); - when(bulkByScrollResponse.getDeleted()).thenReturn(42L); - listener.onResponse(bulkByScrollResponse); - } else { - listener.onFailure(new RuntimeException("failed")); - } - return null; + capturedDeleteByQueryRequests.add((DeleteByQueryRequest) invocationOnMock.getArguments()[1]); + ActionListener listener = (ActionListener) invocationOnMock.getArguments()[2]; + if (shouldSucceed) { + BulkByScrollResponse bulkByScrollResponse = mock(BulkByScrollResponse.class); + when(bulkByScrollResponse.getDeleted()).thenReturn(42L); + listener.onResponse(bulkByScrollResponse); + } else { + listener.onFailure(new RuntimeException("failed")); } - ).when(client).execute(same(DeleteByQueryAction.INSTANCE), any(), any()); + return null; + }).when(client).execute(same(DeleteByQueryAction.INSTANCE), any(), any()); } @SuppressWarnings("unchecked") @@ -193,13 +190,17 @@ private ExpiredAnnotationsRemover createExpiredAnnotationsRemover(Iterator when(threadPool.executor(eq(MachineLearning.UTILITY_THREAD_POOL_NAME))).thenReturn(executor); doAnswer(invocationOnMock -> { - Runnable run = (Runnable) invocationOnMock.getArguments()[0]; - run.run(); - return null; - } - ).when(executor).execute(any()); + Runnable run = (Runnable) invocationOnMock.getArguments()[0]; + run.run(); + return null; + }).when(executor).execute(any()); return new ExpiredAnnotationsRemover( - originSettingClient, jobIterator, new TaskId("test", 0L), mock(AnomalyDetectionAuditor.class), threadPool); + originSettingClient, + jobIterator, + new TaskId("test", 0L), + mock(AnomalyDetectionAuditor.class), + threadPool + ); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/retention/ExpiredModelSnapshotsRemoverTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/retention/ExpiredModelSnapshotsRemoverTests.java index 9731834ee8c31..e836f487faf3f 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/retention/ExpiredModelSnapshotsRemoverTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/retention/ExpiredModelSnapshotsRemoverTests.java @@ -13,7 +13,6 @@ import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.client.Client; import org.elasticsearch.client.OriginSettingClient; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.query.IdsQueryBuilder; import org.elasticsearch.index.reindex.DeleteByQueryAction; @@ -22,6 +21,7 @@ import org.elasticsearch.tasks.TaskId; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xpack.core.ClientHelper; import org.elasticsearch.xpack.core.action.util.QueryPage; import org.elasticsearch.xpack.core.ml.annotations.AnnotationIndex; @@ -92,7 +92,8 @@ public void testRemove_GivenJobWithoutActiveSnapshot() throws IOException { List jobs = Collections.singletonList(JobTests.buildJobBuilder("foo").setModelSnapshotRetentionDays(7L).build()); List responses = Collections.singletonList( - AbstractExpiredJobDataRemoverTests.createSearchResponse(Collections.emptyList())); + AbstractExpiredJobDataRemoverTests.createSearchResponse(Collections.emptyList()) + ); givenClientRequestsSucceed(responses, Collections.emptyMap()); createExpiredModelSnapshotsRemover(jobs.iterator()).remove(1.0f, listener, () -> false); @@ -105,8 +106,8 @@ public void testRemove_GivenJobWithoutActiveSnapshot() throws IOException { public void testRemove_GivenJobsWithMixedRetentionPolicies() { List searchResponses = new ArrayList<>(); List jobs = Arrays.asList( - JobTests.buildJobBuilder("job-1").setModelSnapshotRetentionDays(7L).setModelSnapshotId("active").build(), - JobTests.buildJobBuilder("job-2").setModelSnapshotRetentionDays(17L).setModelSnapshotId("active").build() + JobTests.buildJobBuilder("job-1").setModelSnapshotRetentionDays(7L).setModelSnapshotId("active").build(), + JobTests.buildJobBuilder("job-2").setModelSnapshotRetentionDays(17L).setModelSnapshotId("active").build() ); Date now = new Date(); @@ -119,12 +120,14 @@ public void testRemove_GivenJobsWithMixedRetentionPolicies() { // It needs to be strictly more than 7 days before the most recent snapshot, hence the extra millisecond Date eightDaysAndOneMsAgo = new Date(now.getTime() - TimeValue.timeValueDays(8).getMillis() - 1); Map> snapshotResponses = new HashMap<>(); - snapshotResponses.put("job-1", + snapshotResponses.put( + "job-1", Arrays.asList( // Keeping active as its expiration is not known. We can assume "worst case" and verify it is not removed createModelSnapshot("job-1", "active", eightDaysAndOneMsAgo), createModelSnapshot("job-1", "old-snapshot", eightDaysAndOneMsAgo) - )); + ) + ); // Retention days for job-2 is 17 days, consequently, its query should return anything as we don't ask for snapshots // created AFTER 17 days ago snapshotResponses.put("job-2", Collections.emptyList()); @@ -140,14 +143,20 @@ public void testRemove_GivenJobsWithMixedRetentionPolicies() { assertThat(capturedDeleteModelSnapshotRequests.size(), equalTo(1)); DeleteByQueryRequest deleteSnapshotRequest = capturedDeleteModelSnapshotRequests.get(0); - assertThat(deleteSnapshotRequest.indices(), - arrayContainingInAnyOrder(AnomalyDetectorsIndex.jobResultsAliasedName("job-1"), + assertThat( + deleteSnapshotRequest.indices(), + arrayContainingInAnyOrder( + AnomalyDetectorsIndex.jobResultsAliasedName("job-1"), AnomalyDetectorsIndex.jobStateIndexPattern(), - AnnotationIndex.READ_ALIAS_NAME)); + AnnotationIndex.READ_ALIAS_NAME + ) + ); assertThat(deleteSnapshotRequest.getSearchRequest().source().query() instanceof IdsQueryBuilder, is(true)); - IdsQueryBuilder idsQueryBuilder = (IdsQueryBuilder)deleteSnapshotRequest.getSearchRequest().source().query(); - assertTrue("expected ids related to [old-snapshot] but received [" + idsQueryBuilder.ids() + "]", - idsQueryBuilder.ids().stream().allMatch(s -> s.contains("old-snapshot"))); + IdsQueryBuilder idsQueryBuilder = (IdsQueryBuilder) deleteSnapshotRequest.getSearchRequest().source().query(); + assertTrue( + "expected ids related to [old-snapshot] but received [" + idsQueryBuilder.ids() + "]", + idsQueryBuilder.ids().stream().allMatch(s -> s.contains("old-snapshot")) + ); } public void testRemove_GivenTimeout() throws IOException { @@ -158,8 +167,10 @@ public void testRemove_GivenTimeout() throws IOException { ); Date now = new Date(); - List snapshots1JobSnapshots = Arrays.asList(createModelSnapshot("snapshots-1", "snapshots-1_1", now), - createModelSnapshot("snapshots-1", "snapshots-1_2", now)); + List snapshots1JobSnapshots = Arrays.asList( + createModelSnapshot("snapshots-1", "snapshots-1_1", now), + createModelSnapshot("snapshots-1", "snapshots-1_2", now) + ); List snapshots2JobSnapshots = Collections.singletonList(createModelSnapshot("snapshots-2", "snapshots-2_1", now)); searchResponses.add(AbstractExpiredJobDataRemoverTests.createSearchResponse(snapshots1JobSnapshots)); searchResponses.add(AbstractExpiredJobDataRemoverTests.createSearchResponse(snapshots2JobSnapshots)); @@ -183,8 +194,8 @@ public void testRemove_GivenTimeout() throws IOException { public void testRemove_GivenClientSearchRequestsFail() { List searchResponses = new ArrayList<>(); List jobs = Arrays.asList( - JobTests.buildJobBuilder("snapshots-1").setModelSnapshotRetentionDays(7L).setModelSnapshotId("active").build(), - JobTests.buildJobBuilder("snapshots-2").setModelSnapshotRetentionDays(17L).setModelSnapshotId("active").build() + JobTests.buildJobBuilder("snapshots-1").setModelSnapshotRetentionDays(7L).setModelSnapshotId("active").build(), + JobTests.buildJobBuilder("snapshots-2").setModelSnapshotRetentionDays(17L).setModelSnapshotId("active").build() ); givenClientSearchRequestsFail(searchResponses, Collections.emptyMap()); @@ -199,8 +210,8 @@ public void testRemove_GivenClientSearchRequestsFail() { public void testRemove_GivenClientDeleteSnapshotRequestsFail() { List searchResponses = new ArrayList<>(); List jobs = Arrays.asList( - JobTests.buildJobBuilder("snapshots-1").setModelSnapshotRetentionDays(7L).setModelSnapshotId("active").build(), - JobTests.buildJobBuilder("snapshots-2").setModelSnapshotRetentionDays(17L).setModelSnapshotId("active").build() + JobTests.buildJobBuilder("snapshots-1").setModelSnapshotRetentionDays(7L).setModelSnapshotId("active").build(), + JobTests.buildJobBuilder("snapshots-2").setModelSnapshotRetentionDays(17L).setModelSnapshotId("active").build() ); Date now = new Date(); @@ -210,9 +221,7 @@ public void testRemove_GivenClientDeleteSnapshotRequestsFail() { searchResponses.add(AbstractExpiredJobDataRemoverTests.createSearchResponseFromHits(Collections.singletonList(snapshot1_1))); Map> snapshots = new HashMap<>(); // Should only return the one from 8 days ago - snapshots.put("snapshots-1", Collections.singletonList( - createModelSnapshot("snapshots-1", "snapshots-1_2", eightDaysAndOneMsAgo) - )); + snapshots.put("snapshots-1", Collections.singletonList(createModelSnapshot("snapshots-1", "snapshots-1_2", eightDaysAndOneMsAgo))); // Shouldn't return anything as retention is 17 days snapshots.put("snapshots-2", Collections.emptyList()); @@ -230,14 +239,20 @@ public void testRemove_GivenClientDeleteSnapshotRequestsFail() { assertThat(capturedDeleteModelSnapshotRequests.size(), equalTo(1)); DeleteByQueryRequest deleteSnapshotRequest = capturedDeleteModelSnapshotRequests.get(0); - assertThat(deleteSnapshotRequest.indices(), - arrayContainingInAnyOrder(AnomalyDetectorsIndex.jobResultsAliasedName("snapshots-1"), + assertThat( + deleteSnapshotRequest.indices(), + arrayContainingInAnyOrder( + AnomalyDetectorsIndex.jobResultsAliasedName("snapshots-1"), AnomalyDetectorsIndex.jobStateIndexPattern(), - AnnotationIndex.READ_ALIAS_NAME)); + AnnotationIndex.READ_ALIAS_NAME + ) + ); assertThat(deleteSnapshotRequest.getSearchRequest().source().query() instanceof IdsQueryBuilder, is(true)); - IdsQueryBuilder idsQueryBuilder = (IdsQueryBuilder)deleteSnapshotRequest.getSearchRequest().source().query(); - assertTrue("expected ids related to [snapshots-1_2] but received [" + idsQueryBuilder.ids() + "]", - idsQueryBuilder.ids().stream().allMatch(s -> s.contains("snapshots-1_2"))); + IdsQueryBuilder idsQueryBuilder = (IdsQueryBuilder) deleteSnapshotRequest.getSearchRequest().source().query(); + assertTrue( + "expected ids related to [snapshots-1_2] but received [" + idsQueryBuilder.ids() + "]", + idsQueryBuilder.ids().stream().allMatch(s -> s.contains("snapshots-1_2")) + ); } @SuppressWarnings("unchecked") @@ -248,10 +263,12 @@ public void testCalcCutoffEpochMs() { SearchHit snapshot1_1 = createModelSnapshotQueryHit("job-1", "newest-snapshot", oneDayAgo); searchResponses.add(AbstractExpiredJobDataRemoverTests.createSearchResponseFromHits(Collections.singletonList(snapshot1_1))); - givenClientRequests(searchResponses, + givenClientRequests( + searchResponses, true, true, - Collections.singletonMap("job-1", Collections.singletonList(createModelSnapshot("job-1", "newest-snapshot", oneDayAgo)))); + Collections.singletonMap("job-1", Collections.singletonList(createModelSnapshot("job-1", "newest-snapshot", oneDayAgo))) + ); long retentionDays = 3L; ActionListener cutoffListener = mock(ActionListener.class); @@ -269,18 +286,18 @@ private ExpiredModelSnapshotsRemover createExpiredModelSnapshotsRemover(Iterator when(threadPool.executor(eq(MachineLearning.UTILITY_THREAD_POOL_NAME))).thenReturn(executor); doAnswer(invocationOnMock -> { - Runnable run = (Runnable) invocationOnMock.getArguments()[0]; - run.run(); - return null; - } - ).when(executor).execute(any()); + Runnable run = (Runnable) invocationOnMock.getArguments()[0]; + run.run(); + return null; + }).when(executor).execute(any()); return new ExpiredModelSnapshotsRemover( originSettingClient, jobIterator, threadPool, new TaskId("test", 0L), resultsProvider, - mock(AnomalyDetectionAuditor.class)); + mock(AnomalyDetectionAuditor.class) + ); } private static ModelSnapshot createModelSnapshot(String jobId, String snapshotId, Date date) { @@ -296,26 +313,28 @@ private static SearchHit createModelSnapshotQueryHit(String jobId, String snapsh return hitBuilder.build(); } - private void givenClientRequestsSucceed(List searchResponses, - Map> snapshots) { + private void givenClientRequestsSucceed(List searchResponses, Map> snapshots) { givenClientRequests(searchResponses, true, true, snapshots); } - private void givenClientSearchRequestsFail(List searchResponses, - Map> snapshots) { + private void givenClientSearchRequestsFail(List searchResponses, Map> snapshots) { givenClientRequests(searchResponses, false, true, snapshots); } - private void givenClientDeleteModelSnapshotRequestsFail(List searchResponses, - Map> snapshots) { + private void givenClientDeleteModelSnapshotRequestsFail( + List searchResponses, + Map> snapshots + ) { givenClientRequests(searchResponses, true, false, snapshots); } @SuppressWarnings("unchecked") - private void givenClientRequests(List searchResponses, - boolean shouldSearchRequestsSucceed, - boolean shouldDeleteSnapshotRequestsSucceed, - Map> snapshots) { + private void givenClientRequests( + List searchResponses, + boolean shouldSearchRequestsSucceed, + boolean shouldDeleteSnapshotRequestsSucceed, + Map> snapshots + ) { doAnswer(new Answer() { AtomicInteger callCount = new AtomicInteger(); @@ -337,24 +356,23 @@ public Void answer(InvocationOnMock invocationOnMock) { }).when(client).execute(same(SearchAction.INSTANCE), any(), any()); doAnswer(invocationOnMock -> { - capturedDeleteModelSnapshotRequests.add((DeleteByQueryRequest) invocationOnMock.getArguments()[1]); - ActionListener listener = - (ActionListener) invocationOnMock.getArguments()[2]; - if (shouldDeleteSnapshotRequestsSucceed) { - listener.onResponse(null); - } else { - listener.onFailure(new RuntimeException("delete snapshot failed")); - } - return null; + capturedDeleteModelSnapshotRequests.add((DeleteByQueryRequest) invocationOnMock.getArguments()[1]); + ActionListener listener = (ActionListener) invocationOnMock.getArguments()[2]; + if (shouldDeleteSnapshotRequestsSucceed) { + listener.onResponse(null); + } else { + listener.onFailure(new RuntimeException("delete snapshot failed")); } - ).when(client).execute(same(DeleteByQueryAction.INSTANCE), any(), any()); + return null; + }).when(client).execute(same(DeleteByQueryAction.INSTANCE), any(), any()); for (Map.Entry> snapshot : snapshots.entrySet()) { doAnswer(new Answer() { AtomicInteger callCount = new AtomicInteger(); + @Override public Void answer(InvocationOnMock invocationOnMock) throws Throwable { - capturedJobIds.add((String)invocationOnMock.getArguments()[0]); + capturedJobIds.add((String) invocationOnMock.getArguments()[0]); Consumer> listener = (Consumer>) invocationOnMock.getArguments()[8]; Consumer failure = (Consumer) invocationOnMock.getArguments()[9]; if (shouldSearchRequestsSucceed || callCount.get() < snapshots.size()) { @@ -363,17 +381,10 @@ public Void answer(InvocationOnMock invocationOnMock) throws Throwable { } else { failure.accept(new RuntimeException("search failed")); } - return null; } - }).when(resultsProvider).modelSnapshots(eq(snapshot.getKey()), - anyInt(), - anyInt(), - any(), - any(), - any(), - anyBoolean(), - any(), - any(), - any()); + return null; + } + }).when(resultsProvider) + .modelSnapshots(eq(snapshot.getKey()), anyInt(), anyInt(), any(), any(), any(), anyBoolean(), any(), any(), any()); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/retention/ExpiredResultsRemoverTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/retention/ExpiredResultsRemoverTests.java index b632691fc5566..673d3b76797a7 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/retention/ExpiredResultsRemoverTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/retention/ExpiredResultsRemoverTests.java @@ -72,10 +72,7 @@ public void testRemove_GivenNoJobs() { public void testRemove_GivenJobsWithoutRetentionPolicy() { givenDBQRequestsSucceed(); - List jobs = Arrays.asList( - JobTests.buildJobBuilder("foo").build(), - JobTests.buildJobBuilder("bar").build() - ); + List jobs = Arrays.asList(JobTests.buildJobBuilder("foo").build(), JobTests.buildJobBuilder("bar").build()); createExpiredResultsRemover(jobs.iterator()).remove(1.0f, listener, () -> false); @@ -89,15 +86,16 @@ public void testRemove_GivenJobsWithAndWithoutRetentionPolicy() { List jobs = Arrays.asList( JobTests.buildJobBuilder("none").build(), JobTests.buildJobBuilder("results-1").setResultsRetentionDays(10L).build(), - JobTests.buildJobBuilder("results-2").setResultsRetentionDays(20L).build()); + JobTests.buildJobBuilder("results-2").setResultsRetentionDays(20L).build() + ); createExpiredResultsRemover(jobs.iterator()).remove(1.0f, listener, () -> false); assertThat(capturedDeleteByQueryRequests.size(), equalTo(2)); DeleteByQueryRequest dbqRequest = capturedDeleteByQueryRequests.get(0); - assertThat(dbqRequest.indices(), equalTo(new String[] {AnomalyDetectorsIndex.jobResultsAliasedName("results-1")})); + assertThat(dbqRequest.indices(), equalTo(new String[] { AnomalyDetectorsIndex.jobResultsAliasedName("results-1") })); dbqRequest = capturedDeleteByQueryRequests.get(1); - assertThat(dbqRequest.indices(), equalTo(new String[] {AnomalyDetectorsIndex.jobResultsAliasedName("results-2")})); + assertThat(dbqRequest.indices(), equalTo(new String[] { AnomalyDetectorsIndex.jobResultsAliasedName("results-2") })); verify(listener).onResponse(true); } @@ -126,12 +124,13 @@ public void testRemove_GivenClientRequestsFailed() { List jobs = Arrays.asList( JobTests.buildJobBuilder("none").build(), JobTests.buildJobBuilder("results-1").setResultsRetentionDays(10L).build(), - JobTests.buildJobBuilder("results-2").setResultsRetentionDays(20L).build()); + JobTests.buildJobBuilder("results-2").setResultsRetentionDays(20L).build() + ); createExpiredResultsRemover(jobs.iterator()).remove(1.0f, listener, () -> false); assertThat(capturedDeleteByQueryRequests.size(), equalTo(1)); DeleteByQueryRequest dbqRequest = capturedDeleteByQueryRequests.get(0); - assertThat(dbqRequest.indices(), equalTo(new String[] {AnomalyDetectorsIndex.jobResultsAliasedName("results-1")})); + assertThat(dbqRequest.indices(), equalTo(new String[] { AnomalyDetectorsIndex.jobResultsAliasedName("results-1") })); verify(listener).onFailure(any()); } @@ -162,19 +161,17 @@ private void givenDBQRequestsFailed() { @SuppressWarnings("unchecked") private void givenDBQRequest(boolean shouldSucceed) { doAnswer(invocationOnMock -> { - capturedDeleteByQueryRequests.add((DeleteByQueryRequest) invocationOnMock.getArguments()[1]); - ActionListener listener = - (ActionListener) invocationOnMock.getArguments()[2]; - if (shouldSucceed) { - BulkByScrollResponse bulkByScrollResponse = mock(BulkByScrollResponse.class); - when(bulkByScrollResponse.getDeleted()).thenReturn(42L); - listener.onResponse(bulkByScrollResponse); - } else { - listener.onFailure(new RuntimeException("failed")); - } - return null; + capturedDeleteByQueryRequests.add((DeleteByQueryRequest) invocationOnMock.getArguments()[1]); + ActionListener listener = (ActionListener) invocationOnMock.getArguments()[2]; + if (shouldSucceed) { + BulkByScrollResponse bulkByScrollResponse = mock(BulkByScrollResponse.class); + when(bulkByScrollResponse.getDeleted()).thenReturn(42L); + listener.onResponse(bulkByScrollResponse); + } else { + listener.onFailure(new RuntimeException("failed")); } - ).when(client).execute(same(DeleteByQueryAction.INSTANCE), any(), any()); + return null; + }).when(client).execute(same(DeleteByQueryAction.INSTANCE), any(), any()); } @SuppressWarnings("unchecked") @@ -193,13 +190,17 @@ private ExpiredResultsRemover createExpiredResultsRemover(Iterator jobItera when(threadPool.executor(eq(MachineLearning.UTILITY_THREAD_POOL_NAME))).thenReturn(executor); doAnswer(invocationOnMock -> { - Runnable run = (Runnable) invocationOnMock.getArguments()[0]; - run.run(); - return null; - } - ).when(executor).execute(any()); - - return new ExpiredResultsRemover(originSettingClient, jobIterator, new TaskId("test", 0L), - mock(AnomalyDetectionAuditor.class), threadPool); + Runnable run = (Runnable) invocationOnMock.getArguments()[0]; + run.run(); + return null; + }).when(executor).execute(any()); + + return new ExpiredResultsRemover( + originSettingClient, + jobIterator, + new TaskId("test", 0L), + mock(AnomalyDetectionAuditor.class), + threadPool + ); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/retention/MlDataRemoverTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/retention/MlDataRemoverTests.java index 98307ff444cb5..bdbd73c380efd 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/retention/MlDataRemoverTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/retention/MlDataRemoverTests.java @@ -15,7 +15,7 @@ public class MlDataRemoverTests extends ESTestCase { public void testStringOrNull() { - MlDataRemover remover = (requestsPerSecond, listener, isTimedOutSupplier) -> { }; + MlDataRemover remover = (requestsPerSecond, listener, isTimedOutSupplier) -> {}; SearchHitBuilder hitBuilder = new SearchHitBuilder(0); assertNull(remover.stringFieldValueOrNull(hitBuilder.build(), "missing")); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/snapshot/upgrader/SnapshotUpgradePredicateTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/snapshot/upgrader/SnapshotUpgradePredicateTests.java index 76e45445885c5..868de2801d173 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/snapshot/upgrader/SnapshotUpgradePredicateTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/snapshot/upgrader/SnapshotUpgradePredicateTests.java @@ -18,15 +18,16 @@ import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; - public class SnapshotUpgradePredicateTests extends ESTestCase { public void testWhenWaitForCompletionIsTrue() { - final PersistentTask assignedTask = new PersistentTask<>("task_id", + final PersistentTask assignedTask = new PersistentTask<>( + "task_id", MlTasks.JOB_SNAPSHOT_UPGRADE_TASK_NAME, new SnapshotUpgradeTaskParams("job", "snapshot"), 1, - new PersistentTasksCustomMetadata.Assignment("test-node", "")); + new PersistentTasksCustomMetadata.Assignment("test-node", "") + ); { SnapshotUpgradePredicate snapshotUpgradePredicate = new SnapshotUpgradePredicate(true, logger); assertThat(snapshotUpgradePredicate.test(null), is(true)); @@ -40,27 +41,31 @@ public void testWhenWaitForCompletionIsTrue() { } { - PersistentTask failedAssignedTask = new PersistentTask<>(assignedTask, - new SnapshotUpgradeTaskState(SnapshotUpgradeState.FAILED, 1, - "this reason")); + PersistentTask failedAssignedTask = new PersistentTask<>( + assignedTask, + new SnapshotUpgradeTaskState(SnapshotUpgradeState.FAILED, 1, "this reason") + ); SnapshotUpgradePredicate snapshotUpgradePredicate = new SnapshotUpgradePredicate(true, logger); assertThat(snapshotUpgradePredicate.test(failedAssignedTask), is(true)); assertThat(snapshotUpgradePredicate.isCompleted(), is(false)); assertThat(snapshotUpgradePredicate.isShouldCancel(), is(true)); assertThat(snapshotUpgradePredicate.getException(), is(notNullValue())); - assertThat(snapshotUpgradePredicate.getException().getMessage(), - containsString("while waiting for to be assigned to a node; recorded reason [this reason]")); + assertThat( + snapshotUpgradePredicate.getException().getMessage(), + containsString("while waiting for to be assigned to a node; recorded reason [this reason]") + ); } - } public void testWhenWaitForCompletionIsFalse() { - final PersistentTask assignedTask = new PersistentTask<>("task_id", + final PersistentTask assignedTask = new PersistentTask<>( + "task_id", MlTasks.JOB_SNAPSHOT_UPGRADE_TASK_NAME, new SnapshotUpgradeTaskParams("job", "snapshot"), 1, - new PersistentTasksCustomMetadata.Assignment("test-node", "")); + new PersistentTasksCustomMetadata.Assignment("test-node", "") + ); { SnapshotUpgradePredicate snapshotUpgradePredicate = new SnapshotUpgradePredicate(false, logger); assertThat(snapshotUpgradePredicate.test(null), is(true)); @@ -74,16 +79,19 @@ public void testWhenWaitForCompletionIsFalse() { } { - PersistentTask failedAssignedTask = new PersistentTask<>(assignedTask, - new SnapshotUpgradeTaskState(SnapshotUpgradeState.FAILED, 1, - "this reason")); + PersistentTask failedAssignedTask = new PersistentTask<>( + assignedTask, + new SnapshotUpgradeTaskState(SnapshotUpgradeState.FAILED, 1, "this reason") + ); SnapshotUpgradePredicate snapshotUpgradePredicate = new SnapshotUpgradePredicate(false, logger); assertThat(snapshotUpgradePredicate.test(failedAssignedTask), is(true)); assertThat(snapshotUpgradePredicate.isCompleted(), is(false)); assertThat(snapshotUpgradePredicate.isShouldCancel(), is(true)); assertThat(snapshotUpgradePredicate.getException(), is(notNullValue())); - assertThat(snapshotUpgradePredicate.getException().getMessage(), - containsString("while waiting for to be assigned to a node; recorded reason [this reason]")); + assertThat( + snapshotUpgradePredicate.getException().getMessage(), + containsString("while waiting for to be assigned to a node; recorded reason [this reason]") + ); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/task/OpenJobPersistentTasksExecutorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/task/OpenJobPersistentTasksExecutorTests.java index e36cc0d1f664a..1ad399e1678b1 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/task/OpenJobPersistentTasksExecutorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/task/OpenJobPersistentTasksExecutorTests.java @@ -90,18 +90,24 @@ public void setUpMocks() { ThreadPool tp = mock(ThreadPool.class); when(tp.generic()).thenReturn(EsExecutors.DIRECT_EXECUTOR_SERVICE); Settings settings = Settings.builder().put("node.name", "OpenJobPersistentTasksExecutorTests").build(); - ClusterSettings clusterSettings = new ClusterSettings(settings, - new HashSet<>(Arrays.asList(InferenceProcessor.MAX_INFERENCE_PROCESSORS, - MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING, - OperationRouting.USE_ADAPTIVE_REPLICA_SELECTION_SETTING, - ClusterService.USER_DEFINED_METADATA, - ClusterApplierService.CLUSTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING, - MachineLearning.CONCURRENT_JOB_ALLOCATIONS, - MachineLearning.MAX_MACHINE_MEMORY_PERCENT, - MachineLearning.MAX_LAZY_ML_NODES, - MachineLearning.MAX_ML_NODE_SIZE, - MachineLearning.MAX_OPEN_JOBS_PER_NODE, - MachineLearning.USE_AUTO_MACHINE_MEMORY_PERCENT))); + ClusterSettings clusterSettings = new ClusterSettings( + settings, + new HashSet<>( + Arrays.asList( + InferenceProcessor.MAX_INFERENCE_PROCESSORS, + MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING, + OperationRouting.USE_ADAPTIVE_REPLICA_SELECTION_SETTING, + ClusterService.USER_DEFINED_METADATA, + ClusterApplierService.CLUSTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING, + MachineLearning.CONCURRENT_JOB_ALLOCATIONS, + MachineLearning.MAX_MACHINE_MEMORY_PERCENT, + MachineLearning.MAX_LAZY_ML_NODES, + MachineLearning.MAX_ML_NODE_SIZE, + MachineLearning.MAX_OPEN_JOBS_PER_NODE, + MachineLearning.USE_AUTO_MACHINE_MEMORY_PERCENT + ) + ) + ); clusterService = new ClusterService(settings, clusterSettings, tp); autodetectProcessManager = mock(AutodetectProcessManager.class); datafeedConfigProvider = mock(DatafeedConfigProvider.class); @@ -117,23 +123,23 @@ public void testValidate_jobMissing() { public void testValidate_jobMarkedAsDeleting() { Job.Builder jobBuilder = buildJobBuilder("job_id"); jobBuilder.setDeleting(true); - Exception e = expectThrows(ElasticsearchStatusException.class, - () -> validateJobAndId("job_id", jobBuilder.build())); + Exception e = expectThrows(ElasticsearchStatusException.class, () -> validateJobAndId("job_id", jobBuilder.build())); assertEquals("Cannot open job [job_id] because it is executing [delete]", e.getMessage()); } public void testValidate_blockedReset() { Job.Builder jobBuilder = buildJobBuilder("job_id"); jobBuilder.setBlocked(new Blocked(Blocked.Reason.REVERT, null)); - Exception e = expectThrows(ElasticsearchStatusException.class, - () -> validateJobAndId("job_id", jobBuilder.build())); + Exception e = expectThrows(ElasticsearchStatusException.class, () -> validateJobAndId("job_id", jobBuilder.build())); assertEquals("Cannot open job [job_id] because it is executing [revert]", e.getMessage()); } public void testValidate_jobWithoutVersion() { Job.Builder jobBuilder = buildJobBuilder("job_id"); - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> validateJobAndId("job_id", jobBuilder.build())); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> validateJobAndId("job_id", jobBuilder.build()) + ); assertEquals("Cannot open job [job_id] because jobs created prior to version 5.5 are not supported", e.getMessage()); assertEquals(RestStatus.BAD_REQUEST, e.status()); } @@ -166,9 +172,11 @@ public void testGetAssignment_GivenUnavailableIndicesWithLazyNode() { OpenJobAction.JobParams params = new OpenJobAction.JobParams("unavailable_index_with_lazy_node"); params.setJob(mock(Job.class)); - assertEquals("Not opening [unavailable_index_with_lazy_node], " + - "because not all primary shards are active for the following indices [.ml-state]", - executor.getAssignment(params, csBuilder.nodes().getAllNodes(), csBuilder.build()).getExplanation()); + assertEquals( + "Not opening [unavailable_index_with_lazy_node], " + + "because not all primary shards are active for the following indices [.ml-state]", + executor.getAssignment(params, csBuilder.nodes().getAllNodes(), csBuilder.build()).getExplanation() + ); } public void testGetAssignment_GivenLazyJobAndNoGlobalLazyNodes() { @@ -186,8 +194,11 @@ public void testGetAssignment_GivenLazyJobAndNoGlobalLazyNodes() { when(job.allowLazyOpen()).thenReturn(true); OpenJobAction.JobParams params = new OpenJobAction.JobParams("lazy_job"); params.setJob(job); - PersistentTasksCustomMetadata.Assignment assignment = executor.getAssignment(params, - csBuilder.nodes().getAllNodes(), csBuilder.build()); + PersistentTasksCustomMetadata.Assignment assignment = executor.getAssignment( + params, + csBuilder.nodes().getAllNodes(), + csBuilder.build() + ); assertNotNull(assignment); assertNull(assignment.getExecutorNode()); assertEquals(JobNodeSelector.AWAITING_LAZY_ASSIGNMENT.getExplanation(), assignment.getExplanation()); @@ -204,8 +215,11 @@ public void testGetAssignment_GivenResetInProgress() { Job job = mock(Job.class); OpenJobAction.JobParams params = new OpenJobAction.JobParams("job_during_reset"); params.setJob(job); - PersistentTasksCustomMetadata.Assignment assignment = executor.getAssignment(params, - csBuilder.nodes().getAllNodes(), csBuilder.build()); + PersistentTasksCustomMetadata.Assignment assignment = executor.getAssignment( + params, + csBuilder.nodes().getAllNodes(), + csBuilder.build() + ); assertNotNull(assignment); assertNull(assignment.getExecutorNode()); assertEquals(MlTasks.RESET_IN_PROGRESS.getExplanation(), assignment.getExplanation()); @@ -215,13 +229,24 @@ public static void addJobTask(String jobId, String nodeId, JobState jobState, Pe addJobTask(jobId, nodeId, jobState, builder, false); } - public static void addJobTask(String jobId, String nodeId, JobState jobState, PersistentTasksCustomMetadata.Builder builder, - boolean isStale) { - builder.addTask(MlTasks.jobTaskId(jobId), MlTasks.JOB_TASK_NAME, new OpenJobAction.JobParams(jobId), - new PersistentTasksCustomMetadata.Assignment(nodeId, "test assignment")); + public static void addJobTask( + String jobId, + String nodeId, + JobState jobState, + PersistentTasksCustomMetadata.Builder builder, + boolean isStale + ) { + builder.addTask( + MlTasks.jobTaskId(jobId), + MlTasks.JOB_TASK_NAME, + new OpenJobAction.JobParams(jobId), + new PersistentTasksCustomMetadata.Assignment(nodeId, "test assignment") + ); if (jobState != null) { - builder.updateTaskState(MlTasks.jobTaskId(jobId), - new JobTaskState(jobState, builder.getLastAllocationId() - (isStale ? 1 : 0), null)); + builder.updateTaskState( + MlTasks.jobTaskId(jobId), + new JobTaskState(jobState, builder.getLastAllocationId() - (isStale ? 1 : 0), null) + ); } } @@ -234,10 +259,11 @@ private void addIndices(Metadata.Builder metadata, RoutingTable.Builder routingT indices.add(AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + AnomalyDetectorsIndexFields.RESULTS_INDEX_DEFAULT); for (String indexName : indices) { IndexMetadata.Builder indexMetadata = IndexMetadata.builder(indexName); - indexMetadata.settings(Settings.builder() - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + indexMetadata.settings( + Settings.builder() + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) ); if (indexName.equals(AnomalyDetectorsIndexFields.STATE_INDEX_PREFIX)) { indexMetadata.putAlias(new AliasMetadata.Builder(AnomalyDetectorsIndex.jobStateIndexWriteAlias())); @@ -245,19 +271,24 @@ private void addIndices(Metadata.Builder metadata, RoutingTable.Builder routingT metadata.put(indexMetadata); Index index = new Index(indexName, "_uuid"); ShardId shardId = new ShardId(index, 0); - ShardRouting shardRouting = ShardRouting.newUnassigned(shardId, true, RecoverySource.EmptyStoreRecoverySource.INSTANCE, - new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "")); + ShardRouting shardRouting = ShardRouting.newUnassigned( + shardId, + true, + RecoverySource.EmptyStoreRecoverySource.INSTANCE, + new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "") + ); shardRouting = shardRouting.initialize("node_id", null, 0L); shardRouting = shardRouting.moveToStarted(); - routingTable.add(IndexRoutingTable.builder(index) - .addIndexShard(new IndexShardRoutingTable.Builder(shardId).addShard(shardRouting).build())); + routingTable.add( + IndexRoutingTable.builder(index).addIndexShard(new IndexShardRoutingTable.Builder(shardId).addShard(shardRouting).build()) + ); } } public static Job jobWithRules(String jobId) { - DetectionRule rule = new DetectionRule.Builder(Collections.singletonList( - new RuleCondition(RuleCondition.AppliesTo.TYPICAL, Operator.LT, 100.0) - )).build(); + DetectionRule rule = new DetectionRule.Builder( + Collections.singletonList(new RuleCondition(RuleCondition.AppliesTo.TYPICAL, Operator.LT, 100.0)) + ).build(); Detector.Builder detector = new Detector.Builder("count", null); detector.setRules(Collections.singletonList(rule)); @@ -271,7 +302,14 @@ public static Job jobWithRules(String jobId) { private OpenJobPersistentTasksExecutor createExecutor(Settings settings) { return new OpenJobPersistentTasksExecutor( - settings, clusterService, autodetectProcessManager, datafeedConfigProvider, mlMemoryTracker, client, - TestIndexNameExpressionResolver.newInstance(), licenseState); + settings, + clusterService, + autodetectProcessManager, + datafeedConfigProvider, + mlMemoryTracker, + client, + TestIndexNameExpressionResolver.newInstance(), + licenseState + ); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/modelsnapshots/GetModelSnapshotsTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/modelsnapshots/GetModelSnapshotsTests.java index 9138e1dc02e0b..f262f3174fb90 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/modelsnapshots/GetModelSnapshotsTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/modelsnapshots/GetModelSnapshotsTests.java @@ -13,14 +13,18 @@ public class GetModelSnapshotsTests extends ESTestCase { public void testModelSnapshots_GivenNegativeFrom() { - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> new GetModelSnapshotsAction.Request("foo", null).setPageParams(new PageParams(-5, 10))); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> new GetModelSnapshotsAction.Request("foo", null).setPageParams(new PageParams(-5, 10)) + ); assertEquals("Parameter [from] cannot be < 0", e.getMessage()); } public void testModelSnapshots_GivenNegativeSize() { - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> new GetModelSnapshotsAction.Request("foo", null).setPageParams(new PageParams(10, -5))); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> new GetModelSnapshotsAction.Request("foo", null).setPageParams(new PageParams(10, -5)) + ); assertEquals("Parameter [size] cannot be < 0", e.getMessage()); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/modelsnapshots/UpdateModelSnapshotActionTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/modelsnapshots/UpdateModelSnapshotActionTests.java index f3f2ba767319b..8c3a5a2c4a617 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/modelsnapshots/UpdateModelSnapshotActionTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/modelsnapshots/UpdateModelSnapshotActionTests.java @@ -9,16 +9,13 @@ import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.ml.action.UpdateModelSnapshotAction; - public class UpdateModelSnapshotActionTests extends ESTestCase { public void testUpdateDescription_GivenMissingArg() { - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> new UpdateModelSnapshotAction.Request(null, "foo")); + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> new UpdateModelSnapshotAction.Request(null, "foo")); assertEquals("[job_id] must not be null.", e.getMessage()); - e = expectThrows(IllegalArgumentException.class, - () -> new UpdateModelSnapshotAction.Request("foo", null)); + e = expectThrows(IllegalArgumentException.class, () -> new UpdateModelSnapshotAction.Request("foo", null)); assertEquals("[snapshot_id] must not be null.", e.getMessage()); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/AbstractNativeProcessTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/AbstractNativeProcessTests.java index bf0fe63569c1d..be013856f5770 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/AbstractNativeProcessTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/AbstractNativeProcessTests.java @@ -44,7 +44,7 @@ public class AbstractNativeProcessTests extends ESTestCase { private Consumer onProcessCrash; private ExecutorService executorService; // This must be counted down at the point where a real native process would terminate, thus - // causing an end-of-file on the stream tailing its logs. This will be: + // causing an end-of-file on the stream tailing its logs. This will be: // 1) After close() for jobs that stop gracefully // 2) After kill() for jobs that are forcefully terminated // 3) After a simulated crash when we test simulated crash @@ -58,11 +58,10 @@ public void initialize() throws IOException { mockNativeProcessLoggingStreamEnds = new CountDownLatch(1); // This answer blocks the thread on the executor service. // In order to unblock it, the test needs to call mockNativeProcessLoggingStreamEnds.countDown(). - doAnswer( - invocationOnMock -> { - mockNativeProcessLoggingStreamEnds.await(); - return null; - }).when(cppLogHandler).tailStream(); + doAnswer(invocationOnMock -> { + mockNativeProcessLoggingStreamEnds.await(); + return null; + }).when(cppLogHandler).tailStream(); when(cppLogHandler.getErrors()).thenReturn(""); inputStream = mock(OutputStream.class); outputStream = mock(InputStream.class); @@ -74,8 +73,14 @@ public void initialize() throws IOException { when(processPipes.getProcessOutStream()).thenReturn(Optional.of(outputStream)); when(processPipes.getRestoreStream()).thenReturn(Optional.of(restoreStream)); onProcessCrash = mock(Consumer.class); - executorService = EsExecutors.newFixed("test", 1, 1, EsExecutors.daemonThreadFactory("test"), new ThreadContext(Settings.EMPTY), - false); + executorService = EsExecutors.newFixed( + "test", + 1, + 1, + EsExecutors.daemonThreadFactory("test"), + new ThreadContext(Settings.EMPTY), + false + ); } @After @@ -104,9 +109,9 @@ public void testStart_DoNotDetectCrashWhenProcessIsBeingKilled() throws Exceptio process.start(executorService); process.kill(randomBoolean()); // This ends the logging stream immediately after the kill() instead of part - // way through the close sequence. It is critical that this is done, otherwise + // way through the close sequence. It is critical that this is done, otherwise // we would not be accurately simulating what happens with the order streams - // receive end-of-file after a kill() of a real process. The latch is counted + // receive end-of-file after a kill() of a real process. The latch is counted // down again during the close() call, but that is harmless. mockNativeProcessLoggingStreamEnds.countDown(); } @@ -142,7 +147,7 @@ public void testCrashReporting() throws Exception { public void testWriteRecord() throws Exception { try (AbstractNativeProcess process = new TestNativeProcess()) { process.start(executorService); - process.writeRecord(new String[]{"a", "b", "c"}); + process.writeRecord(new String[] { "a", "b", "c" }); process.flushStream(); verify(inputStream).write(any(), anyInt(), anyInt()); } @@ -152,7 +157,7 @@ public void testWriteRecord_FailWhenNoInputPipeProvided() throws Exception { when(processPipes.getProcessInStream()).thenReturn(Optional.empty()); try (AbstractNativeProcess process = new TestNativeProcess()) { process.start(executorService); - expectThrows(NullPointerException.class, () -> process.writeRecord(new String[]{"a", "b", "c"})); + expectThrows(NullPointerException.class, () -> process.writeRecord(new String[] { "a", "b", "c" })); } } @@ -203,12 +208,10 @@ public String getName() { } @Override - public void persistState() { - } + public void persistState() {} @Override - public void persistState(long snapshotTimestamp, String snapshotId, String snapshotDescription) { - } + public void persistState(long snapshotTimestamp, String snapshotId, String snapshotDescription) {} @Override protected void afterProcessInStreamClose() { diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/ControllerResponseTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/ControllerResponseTests.java index 4e013bec3e63a..5364bff3b82da 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/ControllerResponseTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/ControllerResponseTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.ml.process; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/IndexingStateProcessorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/IndexingStateProcessorTests.java index 1d318f3d4e779..27393b9ba7236 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/IndexingStateProcessorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/IndexingStateProcessorTests.java @@ -7,6 +7,7 @@ package org.elasticsearch.xpack.ml.process; import com.carrotsearch.randomizedtesting.annotations.Timeout; + import org.elasticsearch.action.DocWriteRequest; import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.bulk.BulkResponse; @@ -50,16 +51,16 @@ public class IndexingStateProcessorTests extends ESTestCase { private static final String STATE_SAMPLE = "" - + " \n" - + "{\"index\": {\"_index\": \"test\", \"_id\": \"1\"}}\n" - + "{ \"field\" : \"value1\" }\n" - + "\0" - + "{\"index\": {\"_index\": \"test\", \"_id\": \"2\"}}\n" - + "{ \"field\" : \"value2\" }\n" - + "\0" - + "{\"index\": {\"_index\": \"test\", \"_id\": \"3\"}}\n" - + "{ \"field\" : \"value3\" }\n" - + "\0"; + + " \n" + + "{\"index\": {\"_index\": \"test\", \"_id\": \"1\"}}\n" + + "{ \"field\" : \"value1\" }\n" + + "\0" + + "{\"index\": {\"_index\": \"test\", \"_id\": \"2\"}}\n" + + "{ \"field\" : \"value2\" }\n" + + "\0" + + "{\"index\": {\"_index\": \"test\", \"_id\": \"3\"}}\n" + + "{ \"field\" : \"value3\" }\n" + + "\0"; private static final String JOB_ID = "state-processor-test-job"; @@ -121,8 +122,9 @@ public void testStateRead_StateDocumentCreated() throws IOException { public void testStateRead_StateDocumentUpdated() throws IOException { testStateRead( - new SearchHits(new SearchHit[]{ SearchHit.createFromMap(Map.of("_index", ".ml-state-dummy")) }, null, 0.0f), - ".ml-state-dummy"); + new SearchHits(new SearchHit[] { SearchHit.createFromMap(Map.of("_index", ".ml-state-dummy")) }, null, 0.0f), + ".ml-state-dummy" + ); } public void testStateReadGivenConsecutiveZeroBytes() throws IOException { diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/MlMemoryTrackerTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/MlMemoryTrackerTests.java index 229925799aa0c..61b7780850116 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/MlMemoryTrackerTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/MlMemoryTrackerTests.java @@ -61,8 +61,10 @@ public class MlMemoryTrackerTests extends ESTestCase { @Before public void setup() { - ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, - Collections.singleton(PersistentTasksClusterService.CLUSTER_TASKS_ALLOCATION_RECHECK_INTERVAL_SETTING)); + ClusterSettings clusterSettings = new ClusterSettings( + Settings.EMPTY, + Collections.singleton(PersistentTasksClusterService.CLUSTER_TASKS_ALLOCATION_RECHECK_INTERVAL_SETTING) + ); ClusterService clusterService = mock(ClusterService.class); ClusterState clusterState = ClusterState.EMPTY_STATE; when(clusterService.getClusterSettings()).thenReturn(clusterSettings); @@ -109,8 +111,10 @@ public void testRefreshAll() { tasks.put(task.getId(), task); } - PersistentTasksCustomMetadata persistentTasks = - new PersistentTasksCustomMetadata(numAnomalyDetectorJobTasks + numDataFrameAnalyticsTasks, tasks); + PersistentTasksCustomMetadata persistentTasks = new PersistentTasksCustomMetadata( + numAnomalyDetectorJobTasks + numDataFrameAnalyticsTasks, + tasks + ); doAnswer(invocation -> { @SuppressWarnings("unchecked") @@ -123,8 +127,7 @@ public void testRefreshAll() { memoryTracker.refresh(persistentTasks, ActionListener.wrap(aVoid -> {}, ESTestCase::assertNull)); } else { AtomicReference exception = new AtomicReference<>(); - memoryTracker.refresh(persistentTasks, - ActionListener.wrap(e -> fail("Expected failure response"), exception::set)); + memoryTracker.refresh(persistentTasks, ActionListener.wrap(e -> fail("Expected failure response"), exception::set)); assertEquals("Request to refresh anomaly detector memory requirement on non-master node", exception.get().getMessage()); } @@ -157,8 +160,10 @@ public void testRefreshAllFailure() { tasks.put(task.getId(), task); } - PersistentTasksCustomMetadata persistentTasks = - new PersistentTasksCustomMetadata(numAnomalyDetectorJobTasks + numDataFrameAnalyticsTasks, tasks); + PersistentTasksCustomMetadata persistentTasks = new PersistentTasksCustomMetadata( + numAnomalyDetectorJobTasks + numDataFrameAnalyticsTasks, + tasks + ); doAnswer(invocation -> { @SuppressWarnings("unchecked") @@ -171,15 +176,17 @@ public void testRefreshAllFailure() { doAnswer(invocation -> { @SuppressWarnings("unchecked") - ActionListener> listener = - (ActionListener>) invocation.getArguments()[1]; + ActionListener> listener = (ActionListener>) invocation + .getArguments()[1]; listener.onFailure(new IllegalArgumentException("computer says no")); return null; }).when(configProvider).getConfigsForJobsWithTasksLeniently(any(), any()); AtomicBoolean gotErrorResponse = new AtomicBoolean(false); - memoryTracker.refresh(persistentTasks, - ActionListener.wrap(aVoid -> fail("Expected error response"), e -> gotErrorResponse.set(true))); + memoryTracker.refresh( + persistentTasks, + ActionListener.wrap(aVoid -> fail("Expected error response"), e -> gotErrorResponse.set(true)) + ); assertTrue(gotErrorResponse.get()); // Now run another refresh using a component that calls the onResponse method of the listener - this @@ -187,15 +194,14 @@ public void testRefreshAllFailure() { doAnswer(invocation -> { @SuppressWarnings("unchecked") - ActionListener> listener = - (ActionListener>) invocation.getArguments()[1]; + ActionListener> listener = (ActionListener>) invocation + .getArguments()[1]; listener.onResponse(Collections.emptyList()); return null; }).when(configProvider).getConfigsForJobsWithTasksLeniently(any(), any()); AtomicReference exception = new AtomicReference<>(); - memoryTracker.refresh(persistentTasks, - ActionListener.wrap(e -> fail("Expected failure response"), exception::set)); + memoryTracker.refresh(persistentTasks, ActionListener.wrap(e -> fail("Expected failure response"), exception::set)); assertEquals("Request to refresh anomaly detector memory requirement on non-master node", exception.get().getMessage()); } @@ -232,22 +238,31 @@ public void testRefreshOneAnomalyDetectorJob() { if (isMaster) { AtomicReference refreshedMemoryRequirement = new AtomicReference<>(); - memoryTracker.refreshAnomalyDetectorJobMemory(jobId, - ActionListener.wrap(refreshedMemoryRequirement::set, ESTestCase::assertNull)); + memoryTracker.refreshAnomalyDetectorJobMemory( + jobId, + ActionListener.wrap(refreshedMemoryRequirement::set, ESTestCase::assertNull) + ); if (haveEstablishedModelMemory) { - assertEquals(Long.valueOf(modelBytes + Job.PROCESS_MEMORY_OVERHEAD.getBytes()), - memoryTracker.getAnomalyDetectorJobMemoryRequirement(jobId)); + assertEquals( + Long.valueOf(modelBytes + Job.PROCESS_MEMORY_OVERHEAD.getBytes()), + memoryTracker.getAnomalyDetectorJobMemoryRequirement(jobId) + ); } else { - long expectedModelMemoryLimit = - simulateVeryOldJob ? AnalysisLimits.PRE_6_1_DEFAULT_MODEL_MEMORY_LIMIT_MB : recentJobModelMemoryLimitMb; - assertEquals(Long.valueOf(ByteSizeValue.ofMb(expectedModelMemoryLimit).getBytes() + Job.PROCESS_MEMORY_OVERHEAD.getBytes()), - memoryTracker.getAnomalyDetectorJobMemoryRequirement(jobId)); + long expectedModelMemoryLimit = simulateVeryOldJob + ? AnalysisLimits.PRE_6_1_DEFAULT_MODEL_MEMORY_LIMIT_MB + : recentJobModelMemoryLimitMb; + assertEquals( + Long.valueOf(ByteSizeValue.ofMb(expectedModelMemoryLimit).getBytes() + Job.PROCESS_MEMORY_OVERHEAD.getBytes()), + memoryTracker.getAnomalyDetectorJobMemoryRequirement(jobId) + ); } assertEquals(memoryTracker.getAnomalyDetectorJobMemoryRequirement(jobId), refreshedMemoryRequirement.get()); } else { AtomicReference exception = new AtomicReference<>(); - memoryTracker.refreshAnomalyDetectorJobMemory(jobId, - ActionListener.wrap(e -> fail("Expected failure response"), exception::set)); + memoryTracker.refreshAnomalyDetectorJobMemory( + jobId, + ActionListener.wrap(e -> fail("Expected failure response"), exception::set) + ); assertEquals("Request to refresh anomaly detector memory requirement on non-master node", exception.get().getMessage()); assertNull(memoryTracker.getAnomalyDetectorJobMemoryRequirement(jobId)); } @@ -270,15 +285,26 @@ public void testStop() { } private PersistentTasksCustomMetadata.PersistentTask makeTestAnomalyDetectorTask(String jobId) { - return new PersistentTasksCustomMetadata.PersistentTask<>(MlTasks.jobTaskId(jobId), MlTasks.JOB_TASK_NAME, - new OpenJobAction.JobParams(jobId), 0, PersistentTasksCustomMetadata.INITIAL_ASSIGNMENT); + return new PersistentTasksCustomMetadata.PersistentTask<>( + MlTasks.jobTaskId(jobId), + MlTasks.JOB_TASK_NAME, + new OpenJobAction.JobParams(jobId), + 0, + PersistentTasksCustomMetadata.INITIAL_ASSIGNMENT + ); } private PersistentTasksCustomMetadata.PersistentTask makeTestDataFrameAnalyticsTask( - String id, boolean allowLazyStart) { - return new PersistentTasksCustomMetadata.PersistentTask<>(MlTasks.dataFrameAnalyticsTaskId(id), - MlTasks.DATA_FRAME_ANALYTICS_TASK_NAME, new StartDataFrameAnalyticsAction.TaskParams(id, Version.CURRENT, allowLazyStart), - 0, PersistentTasksCustomMetadata.INITIAL_ASSIGNMENT); + String id, + boolean allowLazyStart + ) { + return new PersistentTasksCustomMetadata.PersistentTask<>( + MlTasks.dataFrameAnalyticsTaskId(id), + MlTasks.DATA_FRAME_ANALYTICS_TASK_NAME, + new StartDataFrameAnalyticsAction.TaskParams(id, Version.CURRENT, allowLazyStart), + 0, + PersistentTasksCustomMetadata.INITIAL_ASSIGNMENT + ); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/NativeControllerTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/NativeControllerTests.java index c3bf48c63a06e..f4ce7acc2de5f 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/NativeControllerTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/NativeControllerTests.java @@ -8,10 +8,10 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.env.Environment; import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.ml.utils.NamedPipeHelper; import java.io.ByteArrayInputStream; @@ -38,8 +38,8 @@ public class NativeControllerTests extends ESTestCase { private static final String NODE_NAME = "native-controller-tests-node"; private static final String TEST_MESSAGE = "{\"logger\":\"controller\",\"timestamp\":1478261151445,\"level\":\"INFO\",\"pid\":10211," - + "\"thread\":\"0x7fff7d2a8000\",\"message\":\"controller (64 bit): Version 6.0.0-alpha1-SNAPSHOT (Build a0d6ef8819418c) " - + "Copyright (c) 2017 Elasticsearch BV\",\"method\":\"main\",\"file\":\"Main.cc\",\"line\":123}\n"; + + "\"thread\":\"0x7fff7d2a8000\",\"message\":\"controller (64 bit): Version 6.0.0-alpha1-SNAPSHOT (Build a0d6ef8819418c) " + + "Copyright (c) 2017 Elasticsearch BV\",\"method\":\"main\",\"file\":\"Main.cc\",\"line\":123}\n"; private final Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build(); @@ -48,16 +48,16 @@ public void testStartProcessCommandSucceeds() throws Exception { final NamedPipeHelper namedPipeHelper = mock(NamedPipeHelper.class); final InputStream logStream = mock(InputStream.class); final CountDownLatch mockNativeProcessLoggingStreamEnds = new CountDownLatch(1); - doAnswer( - invocationOnMock -> { - mockNativeProcessLoggingStreamEnds.await(); - return -1; - }).when(logStream).read(any()); + doAnswer(invocationOnMock -> { + mockNativeProcessLoggingStreamEnds.await(); + return -1; + }).when(logStream).read(any()); when(namedPipeHelper.openNamedPipeInputStream(contains("log"), any(Duration.class))).thenReturn(logStream); ByteArrayOutputStream commandStream = new ByteArrayOutputStream(); when(namedPipeHelper.openNamedPipeOutputStream(contains("command"), any(Duration.class))).thenReturn(commandStream); - ByteArrayInputStream outputStream = - new ByteArrayInputStream("[{\"id\":1,\"success\":true,\"reason\":\"ok\"}]".getBytes(StandardCharsets.UTF_8)); + ByteArrayInputStream outputStream = new ByteArrayInputStream( + "[{\"id\":1,\"success\":true,\"reason\":\"ok\"}]".getBytes(StandardCharsets.UTF_8) + ); when(namedPipeHelper.openNamedPipeInputStream(contains("output"), any(Duration.class))).thenReturn(outputStream); List command = new ArrayList<>(); @@ -66,12 +66,18 @@ public void testStartProcessCommandSucceeds() throws Exception { command.add("--arg2=42"); command.add("--arg3=something with spaces"); - NativeController nativeController = new NativeController(NODE_NAME, TestEnvironment.newEnvironment(settings), namedPipeHelper, - mock(NamedXContentRegistry.class)); + NativeController nativeController = new NativeController( + NODE_NAME, + TestEnvironment.newEnvironment(settings), + namedPipeHelper, + mock(NamedXContentRegistry.class) + ); nativeController.startProcess(command); - assertEquals("1\tstart\tmy_process\t--arg1\t--arg2=42\t--arg3=something with spaces\n", - commandStream.toString(StandardCharsets.UTF_8.name())); + assertEquals( + "1\tstart\tmy_process\t--arg1\t--arg2=42\t--arg3=something with spaces\n", + commandStream.toString(StandardCharsets.UTF_8.name()) + ); mockNativeProcessLoggingStreamEnds.countDown(); } @@ -81,16 +87,16 @@ public void testStartProcessCommandFails() throws Exception { final NamedPipeHelper namedPipeHelper = mock(NamedPipeHelper.class); final InputStream logStream = mock(InputStream.class); final CountDownLatch mockNativeProcessLoggingStreamEnds = new CountDownLatch(1); - doAnswer( - invocationOnMock -> { - mockNativeProcessLoggingStreamEnds.await(); - return -1; - }).when(logStream).read(any()); + doAnswer(invocationOnMock -> { + mockNativeProcessLoggingStreamEnds.await(); + return -1; + }).when(logStream).read(any()); when(namedPipeHelper.openNamedPipeInputStream(contains("log"), any(Duration.class))).thenReturn(logStream); ByteArrayOutputStream commandStream = new ByteArrayOutputStream(); when(namedPipeHelper.openNamedPipeOutputStream(contains("command"), any(Duration.class))).thenReturn(commandStream); - ByteArrayInputStream outputStream = - new ByteArrayInputStream("[{\"id\":1,\"success\":false,\"reason\":\"some problem\"}]".getBytes(StandardCharsets.UTF_8)); + ByteArrayInputStream outputStream = new ByteArrayInputStream( + "[{\"id\":1,\"success\":false,\"reason\":\"some problem\"}]".getBytes(StandardCharsets.UTF_8) + ); when(namedPipeHelper.openNamedPipeInputStream(contains("output"), any(Duration.class))).thenReturn(outputStream); List command = new ArrayList<>(); @@ -99,12 +105,18 @@ public void testStartProcessCommandFails() throws Exception { command.add("--arg2=666"); command.add("--arg3=something different with spaces"); - NativeController nativeController = new NativeController(NODE_NAME, TestEnvironment.newEnvironment(settings), namedPipeHelper, - mock(NamedXContentRegistry.class)); + NativeController nativeController = new NativeController( + NODE_NAME, + TestEnvironment.newEnvironment(settings), + namedPipeHelper, + mock(NamedXContentRegistry.class) + ); IOException e = expectThrows(IOException.class, () -> nativeController.startProcess(command)); - assertEquals("1\tstart\tmy_process\t--arg1\t--arg2=666\t--arg3=something different with spaces\n", - commandStream.toString(StandardCharsets.UTF_8.name())); + assertEquals( + "1\tstart\tmy_process\t--arg1\t--arg2=666\t--arg3=something different with spaces\n", + commandStream.toString(StandardCharsets.UTF_8.name()) + ); assertEquals("ML controller failed to execute command [1]: [some problem]", e.getMessage()); mockNativeProcessLoggingStreamEnds.countDown(); @@ -120,8 +132,12 @@ public void testGetNativeCodeInfo() throws IOException, TimeoutException { ByteArrayInputStream outputStream = new ByteArrayInputStream("[]".getBytes(StandardCharsets.UTF_8)); when(namedPipeHelper.openNamedPipeInputStream(contains("output"), any(Duration.class))).thenReturn(outputStream); - NativeController nativeController = new NativeController(NODE_NAME, TestEnvironment.newEnvironment(settings), namedPipeHelper, - mock(NamedXContentRegistry.class)); + NativeController nativeController = new NativeController( + NODE_NAME, + TestEnvironment.newEnvironment(settings), + namedPipeHelper, + mock(NamedXContentRegistry.class) + ); Map nativeCodeInfo = nativeController.getNativeCodeInfo(); assertNotNull(nativeCodeInfo); @@ -132,7 +148,7 @@ public void testGetNativeCodeInfo() throws IOException, TimeoutException { public void testControllerDeath() throws Exception { - NamedPipeHelper namedPipeHelper = mock(NamedPipeHelper.class); + NamedPipeHelper namedPipeHelper = mock(NamedPipeHelper.class); ByteArrayInputStream logStream = new ByteArrayInputStream(TEST_MESSAGE.getBytes(StandardCharsets.UTF_8)); when(namedPipeHelper.openNamedPipeInputStream(contains("log"), any(Duration.class))).thenReturn(logStream); ByteArrayOutputStream commandStream = new ByteArrayOutputStream(); @@ -140,16 +156,24 @@ public void testControllerDeath() throws Exception { ByteArrayInputStream outputStream = new ByteArrayInputStream("[".getBytes(StandardCharsets.UTF_8)); when(namedPipeHelper.openNamedPipeInputStream(contains("output"), any(Duration.class))).thenReturn(outputStream); - NativeController nativeController = new NativeController(NODE_NAME, TestEnvironment.newEnvironment(settings), namedPipeHelper, - mock(NamedXContentRegistry.class)); + NativeController nativeController = new NativeController( + NODE_NAME, + TestEnvironment.newEnvironment(settings), + namedPipeHelper, + mock(NamedXContentRegistry.class) + ); // As soon as the log stream ends startProcess should think the native controller has died assertBusy(() -> { - ElasticsearchException e = expectThrows(ElasticsearchException.class, - () -> nativeController.startProcess(Collections.singletonList("my process"))); - - assertEquals("Cannot start process [my process]: native controller process has stopped on node " + - "[native-controller-tests-node]", e.getMessage()); + ElasticsearchException e = expectThrows( + ElasticsearchException.class, + () -> nativeController.startProcess(Collections.singletonList("my process")) + ); + + assertEquals( + "Cannot start process [my process]: native controller process has stopped on node " + "[native-controller-tests-node]", + e.getMessage() + ); }); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/NativeStorageProviderTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/NativeStorageProviderTests.java index e963b2a6dbeca..f2a4add8444bb 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/NativeStorageProviderTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/NativeStorageProviderTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.ml.process; -import org.elasticsearch.core.PathUtils; import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.core.PathUtils; import org.elasticsearch.env.Environment; import org.elasticsearch.test.ESTestCase; import org.junit.Assert; @@ -36,10 +36,10 @@ public void testTmpStorage() throws IOException { storage.put(tmpDir, ByteSizeValue.ofGb(6).getBytes()); NativeStorageProvider storageProvider = createNativeStorageProvider(storage); - Assert.assertNotNull( - storageProvider.tryGetLocalTmpStorage(randomAlphaOfLengthBetween(4, 10), ByteSizeValue.ofBytes(100))); - Assert.assertNull(storageProvider.tryGetLocalTmpStorage(randomAlphaOfLengthBetween(4, 10), - ByteSizeValue.ofBytes(1024 * 1024 * 1024 + 1))); + Assert.assertNotNull(storageProvider.tryGetLocalTmpStorage(randomAlphaOfLengthBetween(4, 10), ByteSizeValue.ofBytes(100))); + Assert.assertNull( + storageProvider.tryGetLocalTmpStorage(randomAlphaOfLengthBetween(4, 10), ByteSizeValue.ofBytes(1024 * 1024 * 1024 + 1)) + ); String id = randomAlphaOfLengthBetween(4, 10); Path path = storageProvider.tryGetLocalTmpStorage(id, ByteSizeValue.ofGb(1)); @@ -126,9 +126,8 @@ private NativeStorageProvider createNativeStorageProvider(Map paths) when(environment.dataFiles()).thenReturn(paths.keySet().toArray(new Path[paths.size()])); NativeStorageProvider storageProvider = spy(new NativeStorageProvider(environment, ByteSizeValue.ofGb(5))); - doAnswer(invocation -> { - return paths.getOrDefault(invocation.getArguments()[0], Long.valueOf(0)).longValue(); - } + doAnswer( + invocation -> { return paths.getOrDefault(invocation.getArguments()[0], Long.valueOf(0)).longValue(); } ).when(storageProvider).getUsableSpace(any(Path.class)); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/ProcessPipesTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/ProcessPipesTests.java index 502931b510ccc..69296f3508030 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/ProcessPipesTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/ProcessPipesTests.java @@ -45,25 +45,36 @@ public void testProcessPipes() throws Exception { Environment env = TestEnvironment.newEnvironment(settings); NamedPipeHelper namedPipeHelper = mock(NamedPipeHelper.class); - when(namedPipeHelper.openNamedPipeInputStream(contains("log"), any(Duration.class))) - .thenReturn(new ByteArrayInputStream(LOG_BYTES)); + when(namedPipeHelper.openNamedPipeInputStream(contains("log"), any(Duration.class))).thenReturn( + new ByteArrayInputStream(LOG_BYTES) + ); ByteArrayOutputStream commandStream = new ByteArrayOutputStream(); - when(namedPipeHelper.openNamedPipeOutputStream(contains("command"), any(Duration.class))) - .thenReturn(commandStream); + when(namedPipeHelper.openNamedPipeOutputStream(contains("command"), any(Duration.class))).thenReturn(commandStream); ByteArrayOutputStream processInStream = new ByteArrayOutputStream(); - when(namedPipeHelper.openNamedPipeOutputStream(contains("input"), any(Duration.class))) - .thenReturn(processInStream); - when(namedPipeHelper.openNamedPipeInputStream(contains("output"), any(Duration.class))) - .thenReturn(new ByteArrayInputStream(OUTPUT_BYTES)); + when(namedPipeHelper.openNamedPipeOutputStream(contains("input"), any(Duration.class))).thenReturn(processInStream); + when(namedPipeHelper.openNamedPipeInputStream(contains("output"), any(Duration.class))).thenReturn( + new ByteArrayInputStream(OUTPUT_BYTES) + ); ByteArrayOutputStream restoreStream = new ByteArrayOutputStream(); - when(namedPipeHelper.openNamedPipeOutputStream(contains("restore"), any(Duration.class))) - .thenReturn(restoreStream); - when(namedPipeHelper.openNamedPipeInputStream(contains("persist"), any(Duration.class))) - .thenReturn(new ByteArrayInputStream(PERSIST_BYTES)); + when(namedPipeHelper.openNamedPipeOutputStream(contains("restore"), any(Duration.class))).thenReturn(restoreStream); + when(namedPipeHelper.openNamedPipeInputStream(contains("persist"), any(Duration.class))).thenReturn( + new ByteArrayInputStream(PERSIST_BYTES) + ); int timeoutSeconds = randomIntBetween(5, 100); - ProcessPipes processPipes = new ProcessPipes(env, namedPipeHelper, Duration.ofSeconds(timeoutSeconds), AutodetectBuilder.AUTODETECT, - "my_job", null, false, true, true, true, true); + ProcessPipes processPipes = new ProcessPipes( + env, + namedPipeHelper, + Duration.ofSeconds(timeoutSeconds), + AutodetectBuilder.AUTODETECT, + "my_job", + null, + false, + true, + true, + true, + true + ); List command = new ArrayList<>(); processPipes.addArgs(command); @@ -111,35 +122,52 @@ public void testCloseUnusedPipes_notConnected() { Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build(); Environment env = TestEnvironment.newEnvironment(settings); - new ProcessPipes(env, namedPipeHelper, Duration.ofSeconds(2), AutodetectBuilder.AUTODETECT, "my_job", null, - true, true, true, true, true); + new ProcessPipes( + env, + namedPipeHelper, + Duration.ofSeconds(2), + AutodetectBuilder.AUTODETECT, + "my_job", + null, + true, + true, + true, + true, + true + ); } public void testCloseOpenedPipesOnError() throws IOException { NamedPipeHelper namedPipeHelper = mock(NamedPipeHelper.class); InputStream logStream = mock(InputStream.class); - when(namedPipeHelper.openNamedPipeInputStream(contains("log"), any(Duration.class))) - .thenReturn(logStream); + when(namedPipeHelper.openNamedPipeInputStream(contains("log"), any(Duration.class))).thenReturn(logStream); OutputStream commandStream = mock(OutputStream.class); - when(namedPipeHelper.openNamedPipeOutputStream(contains("command"), any(Duration.class))) - .thenReturn(commandStream); + when(namedPipeHelper.openNamedPipeOutputStream(contains("command"), any(Duration.class))).thenReturn(commandStream); OutputStream processInStream = mock(OutputStream.class); - when(namedPipeHelper.openNamedPipeOutputStream(contains("input"), any(Duration.class))) - .thenReturn(processInStream); + when(namedPipeHelper.openNamedPipeOutputStream(contains("input"), any(Duration.class))).thenReturn(processInStream); InputStream processOutStream = mock(InputStream.class); - when(namedPipeHelper.openNamedPipeInputStream(contains("output"), any(Duration.class))) - .thenReturn(processOutStream); + when(namedPipeHelper.openNamedPipeInputStream(contains("output"), any(Duration.class))).thenReturn(processOutStream); OutputStream restoreStream = mock(OutputStream.class); - when(namedPipeHelper.openNamedPipeOutputStream(contains("restore"), any(Duration.class))) - .thenReturn(restoreStream); + when(namedPipeHelper.openNamedPipeOutputStream(contains("restore"), any(Duration.class))).thenReturn(restoreStream); // opening this pipe will throw when(namedPipeHelper.openNamedPipeInputStream(contains("persist"), any(Duration.class))).thenThrow(new IOException()); Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build(); Environment env = TestEnvironment.newEnvironment(settings); - ProcessPipes processPipes = new ProcessPipes(env, namedPipeHelper, Duration.ofSeconds(2), AutodetectBuilder.AUTODETECT, "my_job", - null, true, true, true, true, true); + ProcessPipes processPipes = new ProcessPipes( + env, + namedPipeHelper, + Duration.ofSeconds(2), + AutodetectBuilder.AUTODETECT, + "my_job", + null, + true, + true, + true, + true, + true + ); processPipes.connectLogStream(); expectThrows(IOException.class, processPipes::connectOtherStreams); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/ProcessResultsParserTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/ProcessResultsParserTests.java index 9ede296e8910b..17edaa3a86ceb 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/ProcessResultsParserTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/ProcessResultsParserTests.java @@ -7,11 +7,11 @@ package org.elasticsearch.xpack.ml.process; import org.elasticsearch.ElasticsearchParseException; -import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParseException; -import org.elasticsearch.test.ESTestCase; import java.io.ByteArrayInputStream; import java.io.IOException; @@ -38,9 +38,10 @@ public void testParse_GivenUnknownObject() throws IOException { String json = "[{\"unknown\":{\"id\": 18}}]"; try (InputStream inputStream = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8))) { ProcessResultsParser parser = new ProcessResultsParser<>(TestResult.PARSER, NamedXContentRegistry.EMPTY); - XContentParseException e = expectThrows(XContentParseException.class, - () -> parser.parseResults(inputStream).forEachRemaining(a -> { - })); + XContentParseException e = expectThrows( + XContentParseException.class, + () -> parser.parseResults(inputStream).forEachRemaining(a -> {}) + ); assertEquals("[1:3] [test_result] unknown field [unknown]", e.getMessage()); } } @@ -49,16 +50,17 @@ public void testParse_GivenArrayContainsAnotherArray() throws IOException { String json = "[[]]"; try (InputStream inputStream = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8))) { ProcessResultsParser parser = new ProcessResultsParser<>(TestResult.PARSER, NamedXContentRegistry.EMPTY); - ElasticsearchParseException e = expectThrows(ElasticsearchParseException.class, - () -> parser.parseResults(inputStream).forEachRemaining(a -> { - })); + ElasticsearchParseException e = expectThrows( + ElasticsearchParseException.class, + () -> parser.parseResults(inputStream).forEachRemaining(a -> {}) + ); assertEquals("unexpected token [START_ARRAY]", e.getMessage()); } } public void testParseResults() throws IOException { String input = "[{\"field_1\": \"a\", \"field_2\": 1.0}, {\"field_1\": \"b\", \"field_2\": 2.0}," - + " {\"field_1\": \"c\", \"field_2\": 3.0}]"; + + " {\"field_1\": \"c\", \"field_2\": 3.0}]"; try (InputStream inputStream = new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8))) { ProcessResultsParser parser = new ProcessResultsParser<>(TestResult.PARSER, NamedXContentRegistry.EMPTY); @@ -78,8 +80,10 @@ private static class TestResult { private static final ParseField FIELD_1 = new ParseField("field_1"); private static final ParseField FIELD_2 = new ParseField("field_2"); - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("test_result", - a -> new TestResult((String) a[0], (Double) a[1])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "test_result", + a -> new TestResult((String) a[0], (Double) a[1]) + ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), FIELD_1); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/logging/CppLogMessageHandlerTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/logging/CppLogMessageHandlerTests.java index de9a833b2857d..a3bb33f8119de 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/logging/CppLogMessageHandlerTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/logging/CppLogMessageHandlerTests.java @@ -23,43 +23,43 @@ public class CppLogMessageHandlerTests extends ESTestCase { private static final String TEST_MESSAGE_NOISE = "{\"logger\":\"controller\",\"timestamp\":1478261151445,\"level\":\"INFO\"," - + "\"pid\":42,\"thread\":\"0x7fff7d2a8000\",\"message\":\"message 1\",\"class\":\"ml\"," - + "\"method\":\"core::SomeNoiseMaker\",\"file\":\"Noisemaker.cc\",\"line\":333}\n"; + + "\"pid\":42,\"thread\":\"0x7fff7d2a8000\",\"message\":\"message 1\",\"class\":\"ml\"," + + "\"method\":\"core::SomeNoiseMaker\",\"file\":\"Noisemaker.cc\",\"line\":333}\n"; private static final String TEST_MESSAGE_NOISE_DIFFERENT_MESSAGE = "{\"logger\":\"controller\",\"timestamp\":1478261151445," - + "\"level\":\"INFO\",\"pid\":42,\"thread\":\"0x7fff7d2a8000\",\"message\":\"message 2\",\"class\":\"ml\"," - + "\"method\":\"core::SomeNoiseMaker\",\"file\":\"Noisemaker.cc\",\"line\":333}\n"; + + "\"level\":\"INFO\",\"pid\":42,\"thread\":\"0x7fff7d2a8000\",\"message\":\"message 2\",\"class\":\"ml\"," + + "\"method\":\"core::SomeNoiseMaker\",\"file\":\"Noisemaker.cc\",\"line\":333}\n"; private static final String TEST_MESSAGE_NOISE_DIFFERENT_LEVEL = "{\"logger\":\"controller\",\"timestamp\":1478261151445," - + "\"level\":\"ERROR\",\"pid\":42,\"thread\":\"0x7fff7d2a8000\",\"message\":\"message 3\",\"class\":\"ml\"," - + "\"method\":\"core::SomeNoiseMaker\",\"file\":\"Noisemaker.cc\",\"line\":333}\n"; + + "\"level\":\"ERROR\",\"pid\":42,\"thread\":\"0x7fff7d2a8000\",\"message\":\"message 3\",\"class\":\"ml\"," + + "\"method\":\"core::SomeNoiseMaker\",\"file\":\"Noisemaker.cc\",\"line\":333}\n"; private static final String TEST_MESSAGE_OTHER_NOISE = "{\"logger\":\"controller\",\"timestamp\":1478261151446," - + "\"level\":\"INFO\",\"pid\":42,\"thread\":\"0x7fff7d2a8000\",\"message\":\"message 4\",\"class\":\"ml\"," - + "\"method\":\"core::SomeNoiseMaker\",\"file\":\"Noisemaker.h\",\"line\":333}\n"; + + "\"level\":\"INFO\",\"pid\":42,\"thread\":\"0x7fff7d2a8000\",\"message\":\"message 4\",\"class\":\"ml\"," + + "\"method\":\"core::SomeNoiseMaker\",\"file\":\"Noisemaker.h\",\"line\":333}\n"; private static final String TEST_MESSAGE_SOMETHING = "{\"logger\":\"controller\",\"timestamp\":1478261151447,\"level\":\"INFO\"" - + ",\"pid\":42,\"thread\":\"0x7fff7d2a8000\",\"message\":\"message 5\",\"class\":\"ml\"," - + "\"method\":\"core::Something\",\"file\":\"Something.cc\",\"line\":555}\n"; + + ",\"pid\":42,\"thread\":\"0x7fff7d2a8000\",\"message\":\"message 5\",\"class\":\"ml\"," + + "\"method\":\"core::Something\",\"file\":\"Something.cc\",\"line\":555}\n"; private static final String TEST_MESSAGE_NOISE_DEBUG = "{\"logger\":\"controller\",\"timestamp\":1478261151448,\"level\":\"DEBUG\"," - + "\"pid\":42,\"thread\":\"0x7fff7d2a8000\",\"message\":\"message 6\",\"class\":\"ml\"," - + "\"method\":\"core::SomeNoiseMake\",\"file\":\"Noisemaker.cc\",\"line\":333}\n"; + + "\"pid\":42,\"thread\":\"0x7fff7d2a8000\",\"message\":\"message 6\",\"class\":\"ml\"," + + "\"method\":\"core::SomeNoiseMake\",\"file\":\"Noisemaker.cc\",\"line\":333}\n"; private static final String TEST_MESSAGE_NON_JSON_FATAL_ERROR = "Segmentation fault core dumped"; public void testParse() throws IOException, TimeoutException { String testData = "{\"logger\":\"controller\",\"timestamp\":1478261151445,\"level\":\"INFO\",\"pid\":10211," - + "\"thread\":\"0x7fff7d2a8000\",\"message\":\"uname -a : Darwin Davids-MacBook-Pro.local 15.6.0 Darwin Kernel " - + "Version 15.6.0: Thu Sep 1 15:01:16 PDT 2016; root:xnu-3248.60.11~2/RELEASE_X86_64 x86_64\",\"class\":\"ml\"," - + "\"method\":\"core::CLogger::reconfigureFromProps\",\"file\":\"CLogger.cc\",\"line\":452}\n" - + "{\"logger\":\"controller\",\"timestamp\":1478261151445,\"level\":\"DEBUG\",\"pid\":10211,\"thread\":\"0x7fff7d2a8000\"," - + "\"message\":\"Logger is logging to named pipe " - + "/var/folders/k5/5sqcdlps5sg3cvlp783gcz740000h0/T/controller_log_784\",\"class\":\"ml\"," - + "\"method\":\"core::CLogger::reconfigureLogToNamedPipe\",\"file\":\"CLogger.cc\",\"line\":333}\n" - + "{\"logger\":\"controller\",\"timestamp\":1478261151445,\"level\":\"INFO\",\"pid\":10211,\"thread\":\"0x7fff7d2a8000\"," - + "\"message\":\"controller (64 bit): Version based on 6.0.0-alpha1 (Build b0d6ef8819418c) " - + "Copyright (c) 2017 Elasticsearch BV\",\"method\":\"main\",\"file\":\"Main.cc\",\"line\":123}\n" - + "{\"logger\":\"controller\",\"timestamp\":1478261169065,\"level\":\"ERROR\",\"pid\":10211,\"thread\":\"0x7fff7d2a8000\"," - + "\"message\":\"Did not understand verb 'a'\",\"class\":\"ml\"," - + "\"method\":\"controller::CCommandProcessor::handleCommand\",\"file\":\"CCommandProcessor.cc\",\"line\":100}\n" - + "{\"logger\":\"controller\",\"timestamp\":1478261169065,\"level\":\"DEBUG\",\"pid\":10211,\"thread\":\"0x7fff7d2a8000\"," - + "\"message\":\"Ml controller exiting\",\"method\":\"main\",\"file\":\"Main.cc\",\"line\":147}\n"; + + "\"thread\":\"0x7fff7d2a8000\",\"message\":\"uname -a : Darwin Davids-MacBook-Pro.local 15.6.0 Darwin Kernel " + + "Version 15.6.0: Thu Sep 1 15:01:16 PDT 2016; root:xnu-3248.60.11~2/RELEASE_X86_64 x86_64\",\"class\":\"ml\"," + + "\"method\":\"core::CLogger::reconfigureFromProps\",\"file\":\"CLogger.cc\",\"line\":452}\n" + + "{\"logger\":\"controller\",\"timestamp\":1478261151445,\"level\":\"DEBUG\",\"pid\":10211,\"thread\":\"0x7fff7d2a8000\"," + + "\"message\":\"Logger is logging to named pipe " + + "/var/folders/k5/5sqcdlps5sg3cvlp783gcz740000h0/T/controller_log_784\",\"class\":\"ml\"," + + "\"method\":\"core::CLogger::reconfigureLogToNamedPipe\",\"file\":\"CLogger.cc\",\"line\":333}\n" + + "{\"logger\":\"controller\",\"timestamp\":1478261151445,\"level\":\"INFO\",\"pid\":10211,\"thread\":\"0x7fff7d2a8000\"," + + "\"message\":\"controller (64 bit): Version based on 6.0.0-alpha1 (Build b0d6ef8819418c) " + + "Copyright (c) 2017 Elasticsearch BV\",\"method\":\"main\",\"file\":\"Main.cc\",\"line\":123}\n" + + "{\"logger\":\"controller\",\"timestamp\":1478261169065,\"level\":\"ERROR\",\"pid\":10211,\"thread\":\"0x7fff7d2a8000\"," + + "\"message\":\"Did not understand verb 'a'\",\"class\":\"ml\"," + + "\"method\":\"controller::CCommandProcessor::handleCommand\",\"file\":\"CCommandProcessor.cc\",\"line\":100}\n" + + "{\"logger\":\"controller\",\"timestamp\":1478261169065,\"level\":\"DEBUG\",\"pid\":10211,\"thread\":\"0x7fff7d2a8000\"," + + "\"message\":\"Ml controller exiting\",\"method\":\"main\",\"file\":\"Main.cc\",\"line\":147}\n"; // Try different buffer sizes to smoke out edge case problems in the buffer management for (int readBufSize : new int[] { 11, 42, 101, 1024, 9999 }) { @@ -71,8 +71,10 @@ public void testParse() throws IOException, TimeoutException { // Since this is all being done in one thread and we know the stream has // been completely consumed at this point the wait duration can be zero assertEquals(10211L, handler.getPid(Duration.ZERO)); - assertEquals("controller (64 bit): Version based on 6.0.0-alpha1 (Build b0d6ef8819418c) " - + "Copyright (c) 2017 Elasticsearch BV", handler.getCppCopyright(Duration.ZERO)); + assertEquals( + "controller (64 bit): Version based on 6.0.0-alpha1 (Build b0d6ef8819418c) " + "Copyright (c) 2017 Elasticsearch BV", + handler.getCppCopyright(Duration.ZERO) + ); assertEquals("Did not understand verb 'a'\n", handler.getErrors()); assertFalse(handler.seenFatalError()); } @@ -81,110 +83,276 @@ public void testParse() throws IOException, TimeoutException { public void testThrottlingSummary() throws IllegalAccessException, TimeoutException, IOException { - InputStream is = new ByteArrayInputStream(String.join("", - TEST_MESSAGE_NOISE, TEST_MESSAGE_NOISE, TEST_MESSAGE_NOISE, TEST_MESSAGE_NOISE, TEST_MESSAGE_NOISE, TEST_MESSAGE_NOISE, - TEST_MESSAGE_NOISE_DEBUG, TEST_MESSAGE_OTHER_NOISE, TEST_MESSAGE_SOMETHING) - .getBytes(StandardCharsets.UTF_8)); + InputStream is = new ByteArrayInputStream( + String.join( + "", + TEST_MESSAGE_NOISE, + TEST_MESSAGE_NOISE, + TEST_MESSAGE_NOISE, + TEST_MESSAGE_NOISE, + TEST_MESSAGE_NOISE, + TEST_MESSAGE_NOISE, + TEST_MESSAGE_NOISE_DEBUG, + TEST_MESSAGE_OTHER_NOISE, + TEST_MESSAGE_SOMETHING + ).getBytes(StandardCharsets.UTF_8) + ); MockLogAppender mockAppender = new MockLogAppender(); mockAppender.start(); mockAppender.addExpectation( - new MockLogAppender.SeenEventExpectation("test1", CppLogMessageHandler.class.getName(), Level.INFO, - "[test_throttling] * message 1")); + new MockLogAppender.SeenEventExpectation( + "test1", + CppLogMessageHandler.class.getName(), + Level.INFO, + "[test_throttling] * message 1" + ) + ); mockAppender.addExpectation( - new MockLogAppender.SeenEventExpectation("test2", CppLogMessageHandler.class.getName(), Level.INFO, - "[test_throttling] * message 1 | repeated [5]")); - mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test3", CppLogMessageHandler.class.getName(), Level.INFO, - "[test_throttling] * message 4")); - mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test4", CppLogMessageHandler.class.getName(), Level.INFO, - "[test_throttling] * message 5")); + new MockLogAppender.SeenEventExpectation( + "test2", + CppLogMessageHandler.class.getName(), + Level.INFO, + "[test_throttling] * message 1 | repeated [5]" + ) + ); + mockAppender.addExpectation( + new MockLogAppender.SeenEventExpectation( + "test3", + CppLogMessageHandler.class.getName(), + Level.INFO, + "[test_throttling] * message 4" + ) + ); + mockAppender.addExpectation( + new MockLogAppender.SeenEventExpectation( + "test4", + CppLogMessageHandler.class.getName(), + Level.INFO, + "[test_throttling] * message 5" + ) + ); executeLoggingTest(is, mockAppender, Level.INFO, "test_throttling"); } public void testThrottlingSummaryOneRepeat() throws IllegalAccessException, TimeoutException, IOException { - InputStream is = new ByteArrayInputStream(String - .join("", TEST_MESSAGE_NOISE, TEST_MESSAGE_NOISE, TEST_MESSAGE_NOISE_DEBUG, TEST_MESSAGE_OTHER_NOISE, - TEST_MESSAGE_SOMETHING) - .getBytes(StandardCharsets.UTF_8)); + InputStream is = new ByteArrayInputStream( + String.join( + "", + TEST_MESSAGE_NOISE, + TEST_MESSAGE_NOISE, + TEST_MESSAGE_NOISE_DEBUG, + TEST_MESSAGE_OTHER_NOISE, + TEST_MESSAGE_SOMETHING + ).getBytes(StandardCharsets.UTF_8) + ); MockLogAppender mockAppender = new MockLogAppender(); mockAppender.start(); - mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test1", CppLogMessageHandler.class.getName(), Level.INFO, - "[test_throttling] * message 1")); - mockAppender.addExpectation(new MockLogAppender.UnseenEventExpectation("test2", CppLogMessageHandler.class.getName(), Level.INFO, - "[test_throttling] * message 1 | repeated [1]")); - mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test1", CppLogMessageHandler.class.getName(), Level.INFO, - "[test_throttling] * message 4")); - mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test2", CppLogMessageHandler.class.getName(), Level.INFO, - "[test_throttling] * message 5")); + mockAppender.addExpectation( + new MockLogAppender.SeenEventExpectation( + "test1", + CppLogMessageHandler.class.getName(), + Level.INFO, + "[test_throttling] * message 1" + ) + ); + mockAppender.addExpectation( + new MockLogAppender.UnseenEventExpectation( + "test2", + CppLogMessageHandler.class.getName(), + Level.INFO, + "[test_throttling] * message 1 | repeated [1]" + ) + ); + mockAppender.addExpectation( + new MockLogAppender.SeenEventExpectation( + "test1", + CppLogMessageHandler.class.getName(), + Level.INFO, + "[test_throttling] * message 4" + ) + ); + mockAppender.addExpectation( + new MockLogAppender.SeenEventExpectation( + "test2", + CppLogMessageHandler.class.getName(), + Level.INFO, + "[test_throttling] * message 5" + ) + ); executeLoggingTest(is, mockAppender, Level.INFO, "test_throttling"); } public void testThrottlingSummaryLevelChanges() throws IllegalAccessException, TimeoutException, IOException { - InputStream is = new ByteArrayInputStream(String - .join("", TEST_MESSAGE_NOISE, TEST_MESSAGE_NOISE, TEST_MESSAGE_NOISE, TEST_MESSAGE_NOISE_DIFFERENT_LEVEL, - TEST_MESSAGE_NOISE, TEST_MESSAGE_NOISE, TEST_MESSAGE_NOISE, TEST_MESSAGE_NOISE, TEST_MESSAGE_NOISE_DEBUG, - TEST_MESSAGE_OTHER_NOISE, TEST_MESSAGE_SOMETHING) - .getBytes(StandardCharsets.UTF_8)); + InputStream is = new ByteArrayInputStream( + String.join( + "", + TEST_MESSAGE_NOISE, + TEST_MESSAGE_NOISE, + TEST_MESSAGE_NOISE, + TEST_MESSAGE_NOISE_DIFFERENT_LEVEL, + TEST_MESSAGE_NOISE, + TEST_MESSAGE_NOISE, + TEST_MESSAGE_NOISE, + TEST_MESSAGE_NOISE, + TEST_MESSAGE_NOISE_DEBUG, + TEST_MESSAGE_OTHER_NOISE, + TEST_MESSAGE_SOMETHING + ).getBytes(StandardCharsets.UTF_8) + ); MockLogAppender mockAppender = new MockLogAppender(); mockAppender.start(); - mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test1", CppLogMessageHandler.class.getName(), Level.INFO, - "[test_throttling] * message 1")); - mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test2", CppLogMessageHandler.class.getName(), Level.INFO, - "[test_throttling] * message 1 | repeated [2]")); - mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test3", CppLogMessageHandler.class.getName(), Level.ERROR, - "[test_throttling] * message 3")); - mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test4", CppLogMessageHandler.class.getName(), Level.INFO, - "[test_throttling] * message 1 | repeated [3]")); - mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test5", CppLogMessageHandler.class.getName(), Level.INFO, - "[test_throttling] * message 4")); - mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test6", CppLogMessageHandler.class.getName(), Level.INFO, - "[test_throttling] * message 5")); + mockAppender.addExpectation( + new MockLogAppender.SeenEventExpectation( + "test1", + CppLogMessageHandler.class.getName(), + Level.INFO, + "[test_throttling] * message 1" + ) + ); + mockAppender.addExpectation( + new MockLogAppender.SeenEventExpectation( + "test2", + CppLogMessageHandler.class.getName(), + Level.INFO, + "[test_throttling] * message 1 | repeated [2]" + ) + ); + mockAppender.addExpectation( + new MockLogAppender.SeenEventExpectation( + "test3", + CppLogMessageHandler.class.getName(), + Level.ERROR, + "[test_throttling] * message 3" + ) + ); + mockAppender.addExpectation( + new MockLogAppender.SeenEventExpectation( + "test4", + CppLogMessageHandler.class.getName(), + Level.INFO, + "[test_throttling] * message 1 | repeated [3]" + ) + ); + mockAppender.addExpectation( + new MockLogAppender.SeenEventExpectation( + "test5", + CppLogMessageHandler.class.getName(), + Level.INFO, + "[test_throttling] * message 4" + ) + ); + mockAppender.addExpectation( + new MockLogAppender.SeenEventExpectation( + "test6", + CppLogMessageHandler.class.getName(), + Level.INFO, + "[test_throttling] * message 5" + ) + ); executeLoggingTest(is, mockAppender, Level.INFO, "test_throttling"); } public void testThrottlingLastMessageRepeast() throws IllegalAccessException, TimeoutException, IOException { - InputStream is = new ByteArrayInputStream(String.join("", TEST_MESSAGE_NOISE, TEST_MESSAGE_NOISE, TEST_MESSAGE_NOISE, - TEST_MESSAGE_NOISE, TEST_MESSAGE_NOISE, TEST_MESSAGE_NOISE_DIFFERENT_MESSAGE).getBytes(StandardCharsets.UTF_8)); + InputStream is = new ByteArrayInputStream( + String.join( + "", + TEST_MESSAGE_NOISE, + TEST_MESSAGE_NOISE, + TEST_MESSAGE_NOISE, + TEST_MESSAGE_NOISE, + TEST_MESSAGE_NOISE, + TEST_MESSAGE_NOISE_DIFFERENT_MESSAGE + ).getBytes(StandardCharsets.UTF_8) + ); MockLogAppender mockAppender = new MockLogAppender(); mockAppender.start(); - mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test1", CppLogMessageHandler.class.getName(), Level.INFO, - "[test_throttling] * message 1")); - mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test2", CppLogMessageHandler.class.getName(), Level.INFO, - "[test_throttling] * message 2 | repeated [5]")); + mockAppender.addExpectation( + new MockLogAppender.SeenEventExpectation( + "test1", + CppLogMessageHandler.class.getName(), + Level.INFO, + "[test_throttling] * message 1" + ) + ); + mockAppender.addExpectation( + new MockLogAppender.SeenEventExpectation( + "test2", + CppLogMessageHandler.class.getName(), + Level.INFO, + "[test_throttling] * message 2 | repeated [5]" + ) + ); executeLoggingTest(is, mockAppender, Level.INFO, "test_throttling"); } public void testThrottlingDebug() throws IllegalAccessException, TimeoutException, IOException { - InputStream is = new ByteArrayInputStream(String.join("", TEST_MESSAGE_NOISE, TEST_MESSAGE_NOISE, TEST_MESSAGE_NOISE, - TEST_MESSAGE_NOISE, TEST_MESSAGE_NOISE, TEST_MESSAGE_NOISE, TEST_MESSAGE_NOISE_DEBUG) - .getBytes(StandardCharsets.UTF_8)); + InputStream is = new ByteArrayInputStream( + String.join( + "", + TEST_MESSAGE_NOISE, + TEST_MESSAGE_NOISE, + TEST_MESSAGE_NOISE, + TEST_MESSAGE_NOISE, + TEST_MESSAGE_NOISE, + TEST_MESSAGE_NOISE, + TEST_MESSAGE_NOISE_DEBUG + ).getBytes(StandardCharsets.UTF_8) + ); MockLogAppender mockAppender = new MockLogAppender(); mockAppender.start(); - mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test1", CppLogMessageHandler.class.getName(), Level.INFO, - "[test_throttling] * message 1")); - mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test2", CppLogMessageHandler.class.getName(), Level.DEBUG, - "[test_throttling] * message 6")); - mockAppender.addExpectation(new MockLogAppender.UnseenEventExpectation("test3", CppLogMessageHandler.class.getName(), Level.INFO, - "[test_throttling] * message 1 | repeated [5]")); + mockAppender.addExpectation( + new MockLogAppender.SeenEventExpectation( + "test1", + CppLogMessageHandler.class.getName(), + Level.INFO, + "[test_throttling] * message 1" + ) + ); + mockAppender.addExpectation( + new MockLogAppender.SeenEventExpectation( + "test2", + CppLogMessageHandler.class.getName(), + Level.DEBUG, + "[test_throttling] * message 6" + ) + ); + mockAppender.addExpectation( + new MockLogAppender.UnseenEventExpectation( + "test3", + CppLogMessageHandler.class.getName(), + Level.INFO, + "[test_throttling] * message 1 | repeated [5]" + ) + ); executeLoggingTest(is, mockAppender, Level.DEBUG, "test_throttling"); } public void testWaitForLogStreamClose() throws IOException { - InputStream is = new ByteArrayInputStream(String.join("", TEST_MESSAGE_NOISE, TEST_MESSAGE_NOISE, TEST_MESSAGE_NOISE, - TEST_MESSAGE_NOISE, TEST_MESSAGE_NOISE, TEST_MESSAGE_NOISE_DIFFERENT_MESSAGE).getBytes(StandardCharsets.UTF_8)); + InputStream is = new ByteArrayInputStream( + String.join( + "", + TEST_MESSAGE_NOISE, + TEST_MESSAGE_NOISE, + TEST_MESSAGE_NOISE, + TEST_MESSAGE_NOISE, + TEST_MESSAGE_NOISE, + TEST_MESSAGE_NOISE_DIFFERENT_MESSAGE + ).getBytes(StandardCharsets.UTF_8) + ); try (CppLogMessageHandler handler = new CppLogMessageHandler("test_throttling", is)) { handler.tailStream(); @@ -205,8 +373,7 @@ public void testParseFatalError() throws IOException, IllegalAccessException { } } - private static void executeLoggingTest(InputStream is, MockLogAppender mockAppender, Level level, String jobId) - throws IOException { + private static void executeLoggingTest(InputStream is, MockLogAppender mockAppender, Level level, String jobId) throws IOException { Logger cppMessageLogger = LogManager.getLogger(CppLogMessageHandler.class); Loggers.addAppender(cppMessageLogger, mockAppender); @@ -223,4 +390,3 @@ private static void executeLoggingTest(InputStream is, MockLogAppender mockAppen mockAppender.assertAllExpectationsMatched(); } } - diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/logging/CppLogMessageTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/logging/CppLogMessageTests.java index 944b270df57a5..05699fce238bf 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/logging/CppLogMessageTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/logging/CppLogMessageTests.java @@ -7,13 +7,13 @@ package org.elasticsearch.xpack.ml.process.logging; import org.elasticsearch.common.io.stream.Writeable.Reader; +import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContent; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.AbstractSerializingTestCase; import java.io.IOException; import java.time.Instant; @@ -39,8 +39,8 @@ public void testParseWithMissingTimestamp() throws IOException { Instant before = Instant.ofEpochMilli(Instant.now().toEpochMilli()); String input = "{\"logger\":\"controller\",\"level\":\"INFO\"," - + "\"pid\":42,\"thread\":\"0x7fff7d2a8000\",\"message\":\"message 1\",\"class\":\"ml\"," - + "\"method\":\"core::SomeNoiseMaker\",\"file\":\"Noisemaker.cc\",\"line\":333}\n"; + + "\"pid\":42,\"thread\":\"0x7fff7d2a8000\",\"message\":\"message 1\",\"class\":\"ml\"," + + "\"method\":\"core::SomeNoiseMaker\",\"file\":\"Noisemaker.cc\",\"line\":333}\n"; XContentParser parser = xContent.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, input); CppLogMessage msg = CppLogMessage.PARSER.apply(parser, null); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/writer/LengthEncodedWriterTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/writer/LengthEncodedWriterTests.java index 012311b44965b..b7c754aa90939 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/writer/LengthEncodedWriterTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/writer/LengthEncodedWriterTests.java @@ -16,7 +16,6 @@ import java.util.Arrays; import java.util.List; - public class LengthEncodedWriterTests extends ESTestCase { /** * Simple test push a list of records through the writer and @@ -25,9 +24,9 @@ public class LengthEncodedWriterTests extends ESTestCase { */ public void testLengthEncodedWriter() throws IOException { { - String[] header = {"one", "two", "three", "four", "five"}; - String[] record1 = {"r1", "r2", "", "rrr4", "r5"}; - String[] record2 = {"y1", "y2", "yy3", "yyy4", "y5"}; + String[] header = { "one", "two", "three", "four", "five" }; + String[] record1 = { "r1", "r2", "", "rrr4", "r5" }; + String[] record2 = { "y1", "y2", "yy3", "yyy4", "y5" }; ByteArrayOutputStream bos = new ByteArrayOutputStream(1024); @@ -89,9 +88,9 @@ public void testLengthEncodedWriter() throws IOException { // same again but using lists { - List header = Arrays.asList(new String[]{"one", "two", "three", "four", "five"}); - List record1 = Arrays.asList(new String[]{"r1", "r2", "rr3", "rrr4", "r5"}); - List record2 = Arrays.asList(new String[]{"y1", "y2", "yy3", "yyy4", "y5"}); + List header = Arrays.asList(new String[] { "one", "two", "three", "four", "five" }); + List record1 = Arrays.asList(new String[] { "r1", "r2", "rr3", "rrr4", "r5" }); + List record2 = Arrays.asList(new String[] { "y1", "y2", "yy3", "yyy4", "y5" }); ByteArrayOutputStream bos = new ByteArrayOutputStream(1024); @@ -105,7 +104,6 @@ public void testLengthEncodedWriter() throws IOException { writer.writeRecord(record2); } - ByteBuffer bb = ByteBuffer.wrap(bos.toByteArray()); // read header @@ -156,16 +154,14 @@ public void testLengthEncodedWriter() throws IOException { } } - /** * Test the writeField and writeNumFields methods of LengthEncodedWriter */ - public void testLengthEncodedWriterIndividualRecords() - throws IOException { + public void testLengthEncodedWriterIndividualRecords() throws IOException { { - String[] header = {"one", "two", "three", "four", "five"}; - String[] record1 = {"r1", "r2", "rr3", "rrr4", "r5"}; - String[] record2 = {"y1", "y2", "yy3", "yyy4", "y5"}; + String[] header = { "one", "two", "three", "four", "five" }; + String[] record1 = { "r1", "r2", "rr3", "rrr4", "r5" }; + String[] record2 = { "y1", "y2", "yy3", "yyy4", "y5" }; ByteArrayOutputStream bos = new ByteArrayOutputStream(1024); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/rest/datafeeds/RestStartDatafeedActionTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/rest/datafeeds/RestStartDatafeedActionTests.java index 89b9241673955..6f7852b717c56 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/rest/datafeeds/RestStartDatafeedActionTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/rest/datafeeds/RestStartDatafeedActionTests.java @@ -8,10 +8,10 @@ import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.rest.FakeRestRequest; +import org.elasticsearch.xcontent.NamedXContentRegistry; import java.util.HashMap; import java.util.Map; @@ -26,23 +26,25 @@ public void testPrepareRequest() throws Exception { Map params = new HashMap<>(); params.put("start", "not-a-date"); params.put("datafeed_id", "foo-datafeed"); - RestRequest restRequest1 = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY) - .withParams(params).build(); - ElasticsearchParseException e = expectThrows(ElasticsearchParseException.class, - () -> action.prepareRequest(restRequest1, mock(NodeClient.class))); - assertEquals("Query param [start] with value [not-a-date] cannot be parsed as a date or " + - "converted to a number (epoch).", - e.getMessage()); + RestRequest restRequest1 = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withParams(params).build(); + ElasticsearchParseException e = expectThrows( + ElasticsearchParseException.class, + () -> action.prepareRequest(restRequest1, mock(NodeClient.class)) + ); + assertEquals( + "Query param [start] with value [not-a-date] cannot be parsed as a date or " + "converted to a number (epoch).", + e.getMessage() + ); params = new HashMap<>(); params.put("start", "now"); params.put("end", "not-a-date"); params.put("datafeed_id", "foo-datafeed"); - RestRequest restRequest2 = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY) - .withParams(params).build(); - e = expectThrows(ElasticsearchParseException.class, - () -> action.prepareRequest(restRequest2, mock(NodeClient.class))); - assertEquals("Query param [end] with value [not-a-date] cannot be parsed as a date or " + - "converted to a number (epoch).", e.getMessage()); + RestRequest restRequest2 = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withParams(params).build(); + e = expectThrows(ElasticsearchParseException.class, () -> action.prepareRequest(restRequest2, mock(NodeClient.class))); + assertEquals( + "Query param [end] with value [not-a-date] cannot be parsed as a date or " + "converted to a number (epoch).", + e.getMessage() + ); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/support/BaseMlIntegTestCase.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/support/BaseMlIntegTestCase.java index 1336424cec037..0fbe42c107007 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/support/BaseMlIntegTestCase.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/support/BaseMlIntegTestCase.java @@ -29,12 +29,12 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.reindex.ReindexPlugin; import org.elasticsearch.indices.recovery.RecoveryState; import org.elasticsearch.ingest.common.IngestCommonPlugin; import org.elasticsearch.license.LicenseService; import org.elasticsearch.persistent.PersistentTasksClusterService; import org.elasticsearch.plugins.Plugin; +import org.elasticsearch.reindex.ReindexPlugin; import org.elasticsearch.script.IngestScript; import org.elasticsearch.script.MockDeterministicScript; import org.elasticsearch.script.MockScriptEngine; @@ -146,7 +146,8 @@ protected Collection> nodePlugins() { // ILM is required for .ml-state template index settings IndexLifecycle.class, // Deprecation warnings go to a data stream, if we ever cause a deprecation warning the data streams plugin is required - DataStreamsPlugin.class); + DataStreamsPlugin.class + ); } @Override @@ -158,8 +159,7 @@ protected Collection> getMockPlugins() { public void ensureTemplatesArePresent() throws Exception { assertBusy(() -> { ClusterState state = client().admin().cluster().prepareState().get().getState(); - assertTrue("Timed out waiting for the ML templates to be installed", - MachineLearning.criticalTemplatesInstalled(state)); + assertTrue("Timed out waiting for the ML templates to be installed", MachineLearning.criticalTemplatesInstalled(state)); }, 20, TimeUnit.SECONDS); } @@ -263,9 +263,7 @@ public void cleanup() throws Exception { deleteAllDataFrameAnalytics(client()); waitForPendingTasks(client()); assertBusy(() -> { - RecoveryResponse recoveryResponse = client().admin().indices().prepareRecoveries() - .setActiveOnly(true) - .get(); + RecoveryResponse recoveryResponse = client().admin().indices().prepareRecoveries().setActiveOnly(true).get(); for (List recoveryStates : recoveryResponse.shardRecoveryStates().values()) { assertThat(recoveryStates.size(), equalTo(0)); } @@ -283,9 +281,7 @@ protected static ThreadPool mockThreadPool() { doAnswer(invocationOnMock -> { ((Runnable) invocationOnMock.getArguments()[0]).run(); return null; - }).when(tp).schedule( - any(Runnable.class), any(TimeValue.class), any(String.class) - ); + }).when(tp).schedule(any(Runnable.class), any(TimeValue.class), any(String.class)); return tp; } @@ -299,9 +295,7 @@ public static void indexDocs(Logger logger, String index, long numDocs, long sta indexRequest.source("time", timestamp, "@timestamp", timestamp).opType(DocWriteRequest.OpType.CREATE); bulkRequestBuilder.add(indexRequest); } - BulkResponse bulkResponse = bulkRequestBuilder - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .get(); + BulkResponse bulkResponse = bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).get(); if (bulkResponse.hasFailures()) { int failures = 0; for (BulkItemResponse itemResponse : bulkResponse) { @@ -345,20 +339,20 @@ public static GetDatafeedsStatsAction.Response.DatafeedStats getDatafeedStats(St } public static void deleteAllDatafeeds(Logger logger, Client client) throws Exception { - final QueryPage datafeeds = - client.execute(GetDatafeedsAction.INSTANCE, new GetDatafeedsAction.Request(GetDatafeedsAction.ALL)).actionGet().getResponse(); + final QueryPage datafeeds = client.execute( + GetDatafeedsAction.INSTANCE, + new GetDatafeedsAction.Request(GetDatafeedsAction.ALL) + ).actionGet().getResponse(); try { logger.info("Stopping all datafeeds (using _all)"); - StopDatafeedAction.Response stopResponse = client - .execute(StopDatafeedAction.INSTANCE, new StopDatafeedAction.Request("_all")) - .get(); + StopDatafeedAction.Response stopResponse = client.execute(StopDatafeedAction.INSTANCE, new StopDatafeedAction.Request("_all")) + .get(); assertTrue(stopResponse.isStopped()); } catch (ExecutionException e1) { try { StopDatafeedAction.Request request = new StopDatafeedAction.Request("_all"); request.setForce(true); - StopDatafeedAction.Response stopResponse = client - .execute(StopDatafeedAction.INSTANCE, request).get(); + StopDatafeedAction.Response stopResponse = client.execute(StopDatafeedAction.INSTANCE, request).get(); assertTrue(stopResponse.isStopped()); } catch (ExecutionException e2) { logger.warn("Force-stopping datafeed with _all failed.", e2); @@ -376,15 +370,18 @@ public static void deleteAllDatafeeds(Logger logger, Client client) throws Excep throw new RuntimeException(e); } }); - AcknowledgedResponse deleteResponse = - client.execute(DeleteDatafeedAction.INSTANCE, new DeleteDatafeedAction.Request(datafeed.getId())).get(); + AcknowledgedResponse deleteResponse = client.execute( + DeleteDatafeedAction.INSTANCE, + new DeleteDatafeedAction.Request(datafeed.getId()) + ).get(); assertTrue(deleteResponse.isAcknowledged()); } } public static void deleteAllJobs(Logger logger, Client client) throws Exception { - final QueryPage jobs = - client.execute(GetJobsAction.INSTANCE, new GetJobsAction.Request(Metadata.ALL)).actionGet().getResponse(); + final QueryPage jobs = client.execute(GetJobsAction.INSTANCE, new GetJobsAction.Request(Metadata.ALL)) + .actionGet() + .getResponse(); try { CloseJobAction.Request closeRequest = new CloseJobAction.Request(Metadata.ALL); @@ -410,24 +407,28 @@ public static void deleteAllJobs(Logger logger, Client client) throws Exception for (final Job job : jobs.results()) { assertBusy(() -> { - GetJobsStatsAction.Response statsResponse = - client.execute(GetJobsStatsAction.INSTANCE, new GetJobsStatsAction.Request(job.getId())).actionGet(); + GetJobsStatsAction.Response statsResponse = client.execute( + GetJobsStatsAction.INSTANCE, + new GetJobsStatsAction.Request(job.getId()) + ).actionGet(); assertEquals(JobState.CLOSED, statsResponse.getResponse().results().get(0).getState()); }); - AcknowledgedResponse response = - client.execute(DeleteJobAction.INSTANCE, new DeleteJobAction.Request(job.getId())).get(); + AcknowledgedResponse response = client.execute(DeleteJobAction.INSTANCE, new DeleteJobAction.Request(job.getId())).get(); assertTrue(response.isAcknowledged()); } } public static void deleteAllDataFrameAnalytics(Client client) throws Exception { - final QueryPage analytics = - client.execute(GetDataFrameAnalyticsAction.INSTANCE, - new GetDataFrameAnalyticsAction.Request("_all")).get().getResources(); + final QueryPage analytics = client.execute( + GetDataFrameAnalyticsAction.INSTANCE, + new GetDataFrameAnalyticsAction.Request("_all") + ).get().getResources(); assertBusy(() -> { - GetDataFrameAnalyticsStatsAction.Response statsResponse = - client.execute(GetDataFrameAnalyticsStatsAction.INSTANCE, new GetDataFrameAnalyticsStatsAction.Request("_all")).get(); + GetDataFrameAnalyticsStatsAction.Response statsResponse = client.execute( + GetDataFrameAnalyticsStatsAction.INSTANCE, + new GetDataFrameAnalyticsStatsAction.Request("_all") + ).get(); assertTrue(statsResponse.getResponse().results().stream().allMatch(s -> s.getState().equals(DataFrameAnalyticsState.STOPPED))); }); for (final DataFrameAnalyticsConfig config : analytics.results()) { @@ -440,7 +441,8 @@ public static void waitForPendingTasks(Client client) throws Exception { assertBusy(() -> { ListTasksResponse response = client.execute(ListTasksAction.INSTANCE, request).get(); - List activeTasks = response.getTasks().stream() + List activeTasks = response.getTasks() + .stream() .filter(t -> t.getAction().startsWith(ListTasksAction.NAME) == false) .map(TaskInfo::toString) .collect(Collectors.toList()); @@ -448,20 +450,19 @@ public static void waitForPendingTasks(Client client) throws Exception { }); } - protected static void blockingCall(Consumer> function, - AtomicReference response, - AtomicReference error) throws InterruptedException { + protected static void blockingCall( + Consumer> function, + AtomicReference response, + AtomicReference error + ) throws InterruptedException { CountDownLatch latch = new CountDownLatch(1); - ActionListener listener = ActionListener.wrap( - r -> { - response.set(r); - latch.countDown(); - }, - e -> { - error.set(e); - latch.countDown(); - } - ); + ActionListener listener = ActionListener.wrap(r -> { + response.set(r); + latch.countDown(); + }, e -> { + error.set(e); + latch.countDown(); + }); function.accept(listener); latch.await(); @@ -469,8 +470,10 @@ protected static void blockingCall(Consumer> function, protected String awaitJobOpenedAndAssigned(String jobId, String queryNode) throws Exception { - PersistentTasksClusterService persistentTasksClusterService = - internalCluster().getInstance(PersistentTasksClusterService.class, internalCluster().getMasterName(queryNode)); + PersistentTasksClusterService persistentTasksClusterService = internalCluster().getInstance( + PersistentTasksClusterService.class, + internalCluster().getMasterName(queryNode) + ); // Speed up rechecks to a rate that is quicker than what settings would allow. // The check would work eventually without doing this, but the assertBusy() below // would need to wait 30 seconds, which would make the test run very slowly. @@ -481,8 +484,10 @@ protected String awaitJobOpenedAndAssigned(String jobId, String queryNode) throw AtomicReference jobNode = new AtomicReference<>(); assertBusy(() -> { - GetJobsStatsAction.Response statsResponse = - client(queryNode).execute(GetJobsStatsAction.INSTANCE, new GetJobsStatsAction.Request(jobId)).actionGet(); + GetJobsStatsAction.Response statsResponse = client(queryNode).execute( + GetJobsStatsAction.INSTANCE, + new GetJobsStatsAction.Request(jobId) + ).actionGet(); GetJobsStatsAction.Response.JobStats jobStats = statsResponse.getResponse().results().get(0); assertEquals(JobState.OPENED, jobStats.getState()); assertNotNull(jobStats.getNode()); @@ -496,9 +501,12 @@ protected String awaitJobOpenedAndAssigned(String jobId, String queryNode) throw */ protected void setMlIndicesDelayedNodeLeftTimeoutToZero() { OriginSettingClient originSettingClient = new OriginSettingClient(client(), ClientHelper.ML_ORIGIN); - originSettingClient.admin().indices().updateSettings(new UpdateSettingsRequest(".ml-*") - .origin(ClientHelper.ML_ORIGIN) - .settings(Settings.builder().put(UnassignedInfo.INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING.getKey(), 0).build())) + originSettingClient.admin() + .indices() + .updateSettings( + new UpdateSettingsRequest(".ml-*").origin(ClientHelper.ML_ORIGIN) + .settings(Settings.builder().put(UnassignedInfo.INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING.getKey(), 0).build()) + ) .actionGet(); } @@ -531,8 +539,7 @@ public T compile(String name, String script, ScriptContext context, Map new IngestScript(vars) { @Override - public void execute(Map ctx) { - } + public void execute(Map ctx) {} }; return context.factoryClazz.cast(factory); } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/task/AbstractJobPersistentTasksExecutorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/task/AbstractJobPersistentTasksExecutorTests.java index 0030c1ddf41b1..97ff177e0f304 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/task/AbstractJobPersistentTasksExecutorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/task/AbstractJobPersistentTasksExecutorTests.java @@ -50,39 +50,60 @@ public void testVerifyIndicesPrimaryShardsAreActive() { csBuilder.metadata(metadata); ClusterState cs = csBuilder.build(); - assertEquals(0, verifyIndicesPrimaryShardsAreActive(cs, resolver, true, ".ml-anomalies-shared", - AnomalyDetectorsIndex.jobStateIndexPattern(), - MlMetaIndex.indexName(), - MlConfigIndex.indexName()).size()); + assertEquals( + 0, + verifyIndicesPrimaryShardsAreActive( + cs, + resolver, + true, + ".ml-anomalies-shared", + AnomalyDetectorsIndex.jobStateIndexPattern(), + MlMetaIndex.indexName(), + MlConfigIndex.indexName() + ).size() + ); metadata = Metadata.builder(cs.metadata()); routingTable = new RoutingTable.Builder(cs.routingTable()); - String indexToRemove = randomFrom(resolver.concreteIndexNames(cs, IndicesOptions.lenientExpandOpen(), - ".ml-anomalies-shared", - AnomalyDetectorsIndex.jobStateIndexPattern(), - MlMetaIndex.indexName(), - MlConfigIndex.indexName())); + String indexToRemove = randomFrom( + resolver.concreteIndexNames( + cs, + IndicesOptions.lenientExpandOpen(), + ".ml-anomalies-shared", + AnomalyDetectorsIndex.jobStateIndexPattern(), + MlMetaIndex.indexName(), + MlConfigIndex.indexName() + ) + ); if (randomBoolean()) { routingTable.remove(indexToRemove); } else { Index index = new Index(indexToRemove, "_uuid"); ShardId shardId = new ShardId(index, 0); - ShardRouting shardRouting = ShardRouting.newUnassigned(shardId, true, RecoverySource.EmptyStoreRecoverySource.INSTANCE, - new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "")); + ShardRouting shardRouting = ShardRouting.newUnassigned( + shardId, + true, + RecoverySource.EmptyStoreRecoverySource.INSTANCE, + new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "") + ); shardRouting = shardRouting.initialize("node_id", null, 0L); - routingTable.add(IndexRoutingTable.builder(index) - .addIndexShard(new IndexShardRoutingTable.Builder(shardId).addShard(shardRouting).build())); + routingTable.add( + IndexRoutingTable.builder(index).addIndexShard(new IndexShardRoutingTable.Builder(shardId).addShard(shardRouting).build()) + ); } csBuilder = ClusterState.builder(cs); csBuilder.routingTable(routingTable.build()); csBuilder.metadata(metadata); - List result = verifyIndicesPrimaryShardsAreActive(csBuilder.build(), resolver, + List result = verifyIndicesPrimaryShardsAreActive( + csBuilder.build(), + resolver, true, ".ml-anomalies-shared", AnomalyDetectorsIndex.jobStateIndexPattern(), MlMetaIndex.indexName(), - MlConfigIndex.indexName()); + MlConfigIndex.indexName() + ); assertEquals(1, result.size()); assertEquals(indexToRemove, result.get(0)); } @@ -96,10 +117,11 @@ private void addIndices(Metadata.Builder metadata, RoutingTable.Builder routingT indices.add(AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + AnomalyDetectorsIndexFields.RESULTS_INDEX_DEFAULT); for (String indexName : indices) { IndexMetadata.Builder indexMetadata = IndexMetadata.builder(indexName); - indexMetadata.settings(Settings.builder() - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + indexMetadata.settings( + Settings.builder() + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) ); if (indexName.equals(AnomalyDetectorsIndexFields.STATE_INDEX_PREFIX)) { indexMetadata.putAlias(new AliasMetadata.Builder(AnomalyDetectorsIndex.jobStateIndexWriteAlias())); @@ -107,12 +129,17 @@ private void addIndices(Metadata.Builder metadata, RoutingTable.Builder routingT metadata.put(indexMetadata); Index index = new Index(indexName, "_uuid"); ShardId shardId = new ShardId(index, 0); - ShardRouting shardRouting = ShardRouting.newUnassigned(shardId, true, RecoverySource.EmptyStoreRecoverySource.INSTANCE, - new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "")); + ShardRouting shardRouting = ShardRouting.newUnassigned( + shardId, + true, + RecoverySource.EmptyStoreRecoverySource.INSTANCE, + new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "") + ); shardRouting = shardRouting.initialize("node_id", null, 0L); shardRouting = shardRouting.moveToStarted(); - routingTable.add(IndexRoutingTable.builder(index) - .addIndexShard(new IndexShardRoutingTable.Builder(shardId).addShard(shardRouting).build())); + routingTable.add( + IndexRoutingTable.builder(index).addIndexShard(new IndexShardRoutingTable.Builder(shardId).addShard(shardRouting).build()) + ); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/test/MockOriginSettingClient.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/test/MockOriginSettingClient.java index 848b383c030c4..2c9c4eae04cde 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/test/MockOriginSettingClient.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/test/MockOriginSettingClient.java @@ -7,7 +7,6 @@ package org.elasticsearch.xpack.ml.test; - import org.elasticsearch.client.Client; import org.elasticsearch.client.OriginSettingClient; import org.elasticsearch.common.settings.Settings; diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/DomainSplitFunctionTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/DomainSplitFunctionTests.java index 2041425beb33a..42570c1a8c497 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/DomainSplitFunctionTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/DomainSplitFunctionTests.java @@ -45,8 +45,11 @@ public void testDomainSplit() { assertDomainSplit("example", "local", "example.local"); assertDomainSplit("b.example", "local", "b.example.local"); assertDomainSplit("a.b.example", "local", "a.b.example.local"); - assertDomainSplit("r192494180984795-1-1041782-channel-live.ums", "ustream.tv", - "r192494180984795-1-1041782-channel-live.ums.ustream.tv"); + assertDomainSplit( + "r192494180984795-1-1041782-channel-live.ums", + "ustream.tv", + "r192494180984795-1-1041782-channel-live.ums.ustream.tv" + ); } private void assertDomainSplit(String expectedSubDomain, String expectedDomain, String hostName) { diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/MlStringsTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/MlStringsTests.java index 6ad1dd6af3f3f..fb60ac39bdef1 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/MlStringsTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/MlStringsTests.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.xpack.ml.utils; - import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.ml.utils.MlStrings; @@ -62,20 +61,25 @@ public void testFindMatching_GivenEmptyItems() { } public void testFindMatching_GivenAllPattern() { - assertThat(MlStrings.findMatching(new String[] {"_all"}, new HashSet<>(Arrays.asList("a", "b"))), contains("a", "b")); + assertThat(MlStrings.findMatching(new String[] { "_all" }, new HashSet<>(Arrays.asList("a", "b"))), contains("a", "b")); } public void testFindMatching_GivenWildcardPattern() { - assertThat(MlStrings.findMatching(new String[] {"*"}, new HashSet<>(Arrays.asList("a", "b"))), contains("a", "b")); + assertThat(MlStrings.findMatching(new String[] { "*" }, new HashSet<>(Arrays.asList("a", "b"))), contains("a", "b")); } public void testFindMatching_GivenMixedPatterns() { - assertThat(MlStrings.findMatching(new String[] {"concrete", "wild-*"}, new HashSet<>( - Arrays.asList("a", "concrete", "con*", "wild-1", "wild-2"))), contains("concrete", "wild-1", "wild-2")); + assertThat( + MlStrings.findMatching( + new String[] { "concrete", "wild-*" }, + new HashSet<>(Arrays.asList("a", "concrete", "con*", "wild-1", "wild-2")) + ), + contains("concrete", "wild-1", "wild-2") + ); } public void testFindMatching_GivenItemMatchedByTwoPatterns() { - Set matching = MlStrings.findMatching(new String[]{"a*", "ab*"}, new HashSet<>(Collections.singletonList("abc"))); + Set matching = MlStrings.findMatching(new String[] { "a*", "ab*" }, new HashSet<>(Collections.singletonList("abc"))); assertThat(matching, contains("abc")); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/NameResolverTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/NameResolverTests.java index f9990728727f3..6e39f90ec6cf3 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/NameResolverTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/NameResolverTests.java @@ -28,8 +28,7 @@ public class NameResolverTests extends ESTestCase { public void testNoMatchingNames() { - ResourceNotFoundException e = expectThrows(ResourceNotFoundException.class, - () -> newUnaliasedResolver().expand("foo", false)); + ResourceNotFoundException e = expectThrows(ResourceNotFoundException.class, () -> newUnaliasedResolver().expand("foo", false)); assertThat(e.getMessage(), equalTo("foo")); } @@ -38,14 +37,15 @@ public void testNoMatchingNames_GivenPatternAndAllowNoMatch() { } public void testNoMatchingNames_GivenPatternAndNotAllowNoMatch() { - ResourceNotFoundException e = expectThrows(ResourceNotFoundException.class, - () -> newUnaliasedResolver().expand("foo*", false)); + ResourceNotFoundException e = expectThrows(ResourceNotFoundException.class, () -> newUnaliasedResolver().expand("foo*", false)); assertThat(e.getMessage(), equalTo("foo*")); } public void testNoMatchingNames_GivenMatchingNameAndNonMatchingPatternAndNotAllowNoMatch() { - ResourceNotFoundException e = expectThrows(ResourceNotFoundException.class, - () -> newUnaliasedResolver("foo").expand("foo, bar*", false)); + ResourceNotFoundException e = expectThrows( + ResourceNotFoundException.class, + () -> newUnaliasedResolver("foo").expand("foo, bar*", false) + ); assertThat(e.getMessage(), equalTo("bar*")); } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/NamedPipeHelperTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/NamedPipeHelperTests.java index d7dd984b59b4c..d09f0cbb59c1b 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/NamedPipeHelperTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/NamedPipeHelperTests.java @@ -17,7 +17,6 @@ import java.nio.file.Path; import java.time.Duration; - /** * Only negative test cases are covered, as positive tests would need to create named pipes, * and this is not possible in Java with the Elasticsearch security manager configuration. @@ -28,37 +27,52 @@ public class NamedPipeHelperTests extends ESTestCase { public void testOpenForInputGivenPipeDoesNotExist() { Environment env = TestEnvironment.newEnvironment( - Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build()); - IOException ioe = ESTestCase.expectThrows(FileNotFoundException.class, - () -> NAMED_PIPE_HELPER.openNamedPipeInputStream( + Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build() + ); + IOException ioe = ESTestCase.expectThrows( + FileNotFoundException.class, + () -> NAMED_PIPE_HELPER.openNamedPipeInputStream( NAMED_PIPE_HELPER.getDefaultPipeDirectoryPrefix(env) + "this pipe does not exist", - Duration.ofSeconds(1))); - - assertTrue(ioe.getMessage(), - ioe.getMessage().contains("pipe does not exist") || - ioe.getMessage().contains("The system cannot find the file specified")); + Duration.ofSeconds(1) + ) + ); + + assertTrue( + ioe.getMessage(), + ioe.getMessage().contains("pipe does not exist") || ioe.getMessage().contains("The system cannot find the file specified") + ); } public void testOpenForOutputGivenPipeDoesNotExist() { Environment env = TestEnvironment.newEnvironment( - Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build()); - IOException ioe = ESTestCase.expectThrows(FileNotFoundException.class, - () -> NAMED_PIPE_HELPER.openNamedPipeOutputStream( + Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build() + ); + IOException ioe = ESTestCase.expectThrows( + FileNotFoundException.class, + () -> NAMED_PIPE_HELPER.openNamedPipeOutputStream( NAMED_PIPE_HELPER.getDefaultPipeDirectoryPrefix(env) + "this pipe does not exist", - Duration.ofSeconds(1))); - - assertTrue(ioe.getMessage(), ioe.getMessage().contains("this pipe does not exist") || - ioe.getMessage().contains("No such file or directory") || - ioe.getMessage().contains("The system cannot find the file specified")); + Duration.ofSeconds(1) + ) + ); + + assertTrue( + ioe.getMessage(), + ioe.getMessage().contains("this pipe does not exist") + || ioe.getMessage().contains("No such file or directory") + || ioe.getMessage().contains("The system cannot find the file specified") + ); } public void testOpenForInputGivenPipeIsRegularFile() throws IOException { Environment env = TestEnvironment.newEnvironment( - Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build()); + Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build() + ); Path tempFile = Files.createTempFile(env.tmpFile(), "not a named pipe", null); - IOException ioe = ESTestCase.expectThrows(IOException.class, () -> - NAMED_PIPE_HELPER.openNamedPipeInputStream(tempFile, Duration.ofSeconds(1))); + IOException ioe = ESTestCase.expectThrows( + IOException.class, + () -> NAMED_PIPE_HELPER.openNamedPipeInputStream(tempFile, Duration.ofSeconds(1)) + ); assertTrue(ioe.getMessage(), ioe.getMessage().contains("is not a named pipe")); @@ -67,14 +81,19 @@ public void testOpenForInputGivenPipeIsRegularFile() throws IOException { public void testOpenForOutputGivenPipeIsRegularFile() throws IOException { Environment env = TestEnvironment.newEnvironment( - Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build()); + Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build() + ); Path tempFile = Files.createTempFile(env.tmpFile(), "not a named pipe", null); - IOException ioe = ESTestCase.expectThrows(IOException.class, () -> - NAMED_PIPE_HELPER.openNamedPipeOutputStream(tempFile, Duration.ofSeconds(1))); + IOException ioe = ESTestCase.expectThrows( + IOException.class, + () -> NAMED_PIPE_HELPER.openNamedPipeOutputStream(tempFile, Duration.ofSeconds(1)) + ); - assertTrue(ioe.getMessage(), ioe.getMessage().contains("is not a named pipe") || - ioe.getMessage().contains("The system cannot find the file specified")); + assertTrue( + ioe.getMessage(), + ioe.getMessage().contains("is not a named pipe") || ioe.getMessage().contains("The system cannot find the file specified") + ); assertTrue(Files.deleteIfExists(tempFile)); } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/NativeMemoryCalculatorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/NativeMemoryCalculatorTests.java index 62eaadcec29bc..85d1556e432a9 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/NativeMemoryCalculatorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/NativeMemoryCalculatorTests.java @@ -36,9 +36,10 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.greaterThanOrEqualTo; -public class NativeMemoryCalculatorTests extends ESTestCase{ +public class NativeMemoryCalculatorTests extends ESTestCase { private static final int NUM_TEST_RUNS = 10; + public void testAllowedBytesForMLWhenAutoIsFalse() { for (int i = 0; i < NUM_TEST_RUNS; i++) { long nodeSize = randomLongBetween(ByteSizeValue.ofMb(500).getBytes(), ByteSizeValue.ofGb(64).getBytes()); @@ -47,7 +48,7 @@ public void testAllowedBytesForMLWhenAutoIsFalse() { Settings settings = newSettings(percent, false); ClusterSettings clusterSettings = newClusterSettings(percent, false); - long expected = (long)(nodeSize * (percent / 100.0)); + long expected = (long) (nodeSize * (percent / 100.0)); assertThat(NativeMemoryCalculator.allowedBytesForMl(node, settings).getAsLong(), equalTo(expected)); assertThat(NativeMemoryCalculator.allowedBytesForMl(node, clusterSettings).getAsLong(), equalTo(expected)); @@ -82,23 +83,23 @@ public void testConsistencyInAutoCalculation() { Settings settings = newSettings(30, true); ClusterSettings clusterSettings = newClusterSettings(30, true); - long bytesForML = randomBoolean() ? - NativeMemoryCalculator.allowedBytesForMl(node, settings).getAsLong() : - NativeMemoryCalculator.allowedBytesForMl(node, clusterSettings).getAsLong(); + long bytesForML = randomBoolean() + ? NativeMemoryCalculator.allowedBytesForMl(node, settings).getAsLong() + : NativeMemoryCalculator.allowedBytesForMl(node, clusterSettings).getAsLong(); - NativeMemoryCapacity nativeMemoryCapacity = new NativeMemoryCapacity( - bytesForML, - bytesForML, - jvmSize - ); + NativeMemoryCapacity nativeMemoryCapacity = new NativeMemoryCapacity(bytesForML, bytesForML, jvmSize); AutoscalingCapacity capacity = nativeMemoryCapacity.autoscalingCapacity(30, true); // We don't allow node sizes below 1GB, so we will always be at least that large // Also, allow 1 byte off for weird rounding issues - assertThat(capacity.node().memory().getBytes(), greaterThanOrEqualTo( - Math.max(nodeSize, ByteSizeValue.ofGb(1).getBytes()) - 1L)); - assertThat(capacity.total().memory().getBytes(), greaterThanOrEqualTo( - Math.max(nodeSize, ByteSizeValue.ofGb(1).getBytes()) - 1L)); + assertThat( + capacity.node().memory().getBytes(), + greaterThanOrEqualTo(Math.max(nodeSize, ByteSizeValue.ofGb(1).getBytes()) - 1L) + ); + assertThat( + capacity.total().memory().getBytes(), + greaterThanOrEqualTo(Math.max(nodeSize, ByteSizeValue.ofGb(1).getBytes()) - 1L) + ); } } } @@ -112,9 +113,7 @@ public void testAllowedBytesForMlWhenAutoIsTrue() { Settings settings = newSettings(percent, true); ClusterSettings clusterSettings = newClusterSettings(percent, true); - double truePercent = Math.min( - 90, - ((nodeSize - jvmSize - ByteSizeValue.ofMb(200).getBytes()) / (double)nodeSize) * 100.0D); + double truePercent = Math.min(90, ((nodeSize - jvmSize - ByteSizeValue.ofMb(200).getBytes()) / (double) nodeSize) * 100.0D); long expected = Math.round(nodeSize * (truePercent / 100.0)); assertThat(NativeMemoryCalculator.allowedBytesForMl(node, settings).getAsLong(), equalTo(expected)); @@ -153,43 +152,46 @@ public void testActualNodeSizeCalculationConsistency() { final TriConsumer consistentAutoAssertions = (nativeMemory, memoryPercentage, delta) -> { long autoNodeSize = NativeMemoryCalculator.calculateApproxNecessaryNodeSize(nativeMemory, null, memoryPercentage, true); // It should always be greater than the minimum supported node size - assertThat("node size [" + autoNodeSize +"] smaller than minimum required size [" + MINIMUM_AUTOMATIC_NODE_SIZE + "]", + assertThat( + "node size [" + autoNodeSize + "] smaller than minimum required size [" + MINIMUM_AUTOMATIC_NODE_SIZE + "]", autoNodeSize, - greaterThanOrEqualTo(MINIMUM_AUTOMATIC_NODE_SIZE)); + greaterThanOrEqualTo(MINIMUM_AUTOMATIC_NODE_SIZE) + ); // Our approximate real node size should always return a usable native memory size that is at least the original native memory // size. Rounding errors may cause it to be non-exact. long allowedBytesForMl = NativeMemoryCalculator.allowedBytesForMl(autoNodeSize, memoryPercentage, true); - assertThat("native memory [" - + allowedBytesForMl - + "] smaller than original native memory [" - + nativeMemory - + "]", + assertThat( + "native memory [" + allowedBytesForMl + "] smaller than original native memory [" + nativeMemory + "]", allowedBytesForMl, - greaterThanOrEqualTo(nativeMemory - delta)); + greaterThanOrEqualTo(nativeMemory - delta) + ); }; final BiConsumer consistentManualAssertions = (nativeMemory, memoryPercentage) -> { - assertThat(NativeMemoryCalculator.calculateApproxNecessaryNodeSize(nativeMemory, null, memoryPercentage, false), - equalTo((long)((100.0/memoryPercentage) * nativeMemory))); - assertThat(NativeMemoryCalculator.calculateApproxNecessaryNodeSize( - nativeMemory, - randomNonNegativeLong(), - memoryPercentage, - false), - equalTo((long)((100.0/memoryPercentage) * nativeMemory))); + assertThat( + NativeMemoryCalculator.calculateApproxNecessaryNodeSize(nativeMemory, null, memoryPercentage, false), + equalTo((long) ((100.0 / memoryPercentage) * nativeMemory)) + ); + assertThat( + NativeMemoryCalculator.calculateApproxNecessaryNodeSize(nativeMemory, randomNonNegativeLong(), memoryPercentage, false), + equalTo((long) ((100.0 / memoryPercentage) * nativeMemory)) + ); }; { // 0 memory - assertThat(NativeMemoryCalculator.calculateApproxNecessaryNodeSize( - 0L, - randomLongBetween(0L, ByteSizeValue.ofGb(100).getBytes()), - randomIntBetween(0, 100), - randomBoolean() + assertThat( + NativeMemoryCalculator.calculateApproxNecessaryNodeSize( + 0L, + randomLongBetween(0L, ByteSizeValue.ofGb(100).getBytes()), + randomIntBetween(0, 100), + randomBoolean() ), - equalTo(0L)); + equalTo(0L) + ); assertThat( NativeMemoryCalculator.calculateApproxNecessaryNodeSize(0L, null, randomIntBetween(0, 100), randomBoolean()), - equalTo(0L)); + equalTo(0L) + ); } for (int i = 0; i < NUM_TEST_RUNS; i++) { int memoryPercentage = randomIntBetween(5, 200); @@ -222,7 +224,8 @@ private static Settings newSettings(int maxMemoryPercent, boolean useAuto) { private static ClusterSettings newClusterSettings(int maxMemoryPercent, boolean useAuto) { return new ClusterSettings( newSettings(maxMemoryPercent, useAuto), - Sets.newHashSet(USE_AUTO_MACHINE_MEMORY_PERCENT, MAX_MACHINE_MEMORY_PERCENT)); + Sets.newHashSet(USE_AUTO_MACHINE_MEMORY_PERCENT, MAX_MACHINE_MEMORY_PERCENT) + ); } private static DiscoveryNode newNode(Long jvmSizeLong, Long nodeSizeLong) { @@ -235,13 +238,7 @@ private static DiscoveryNode newNode(Long jvmSizeLong, Long nodeSizeLong) { if (nodeSize != null) { attrs.put(MACHINE_MEMORY_NODE_ATTR, nodeSize); } - return new DiscoveryNode( - "node", - ESTestCase.buildNewFakeTransportAddress(), - attrs, - DiscoveryNodeRole.roles(), - Version.CURRENT - ); + return new DiscoveryNode("node", ESTestCase.buildNewFakeTransportAddress(), attrs, DiscoveryNodeRole.roles(), Version.CURRENT); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/VoidChainTaskExecutorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/VoidChainTaskExecutorTests.java index b074aa1b96206..efdec609db258 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/VoidChainTaskExecutorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/VoidChainTaskExecutorTests.java @@ -57,16 +57,16 @@ public void testExecute() throws InterruptedException { public void testExecute_GivenSingleFailureAndShortCircuit() throws InterruptedException { final List strings = new ArrayList<>(); - ActionListener> finalListener = createBlockingListener(() -> fail(), - e -> assertThat(e.getMessage(), equalTo("some error"))); + ActionListener> finalListener = createBlockingListener( + () -> fail(), + e -> assertThat(e.getMessage(), equalTo("some error")) + ); VoidChainTaskExecutor voidChainTaskExecutor = new VoidChainTaskExecutor(threadPool.generic(), true); voidChainTaskExecutor.add(listener -> { strings.add("before"); listener.onResponse(null); }); - voidChainTaskExecutor.add(listener -> { - throw new RuntimeException("some error"); - }); + voidChainTaskExecutor.add(listener -> { throw new RuntimeException("some error"); }); voidChainTaskExecutor.add(listener -> { strings.add("after"); listener.onResponse(null); @@ -81,19 +81,17 @@ public void testExecute_GivenSingleFailureAndShortCircuit() throws InterruptedEx public void testExecute_GivenMultipleFailuresAndShortCircuit() throws InterruptedException { final List strings = new ArrayList<>(); - ActionListener> finalListener = createBlockingListener(() -> fail(), - e -> assertThat(e.getMessage(), equalTo("some error 1"))); + ActionListener> finalListener = createBlockingListener( + () -> fail(), + e -> assertThat(e.getMessage(), equalTo("some error 1")) + ); VoidChainTaskExecutor voidChainTaskExecutor = new VoidChainTaskExecutor(threadPool.generic(), true); voidChainTaskExecutor.add(listener -> { strings.add("before"); listener.onResponse(null); }); - voidChainTaskExecutor.add(listener -> { - throw new RuntimeException("some error 1"); - }); - voidChainTaskExecutor.add(listener -> { - throw new RuntimeException("some error 2"); - }); + voidChainTaskExecutor.add(listener -> { throw new RuntimeException("some error 1"); }); + voidChainTaskExecutor.add(listener -> { throw new RuntimeException("some error 2"); }); voidChainTaskExecutor.execute(finalListener); @@ -110,9 +108,7 @@ public void testExecute_GivenFailureAndNoShortCircuit() throws InterruptedExcept strings.add("before"); listener.onResponse(null); }); - voidChainTaskExecutor.add(listener -> { - throw new RuntimeException("some error"); - }); + voidChainTaskExecutor.add(listener -> { throw new RuntimeException("some error"); }); voidChainTaskExecutor.add(listener -> { strings.add("after"); listener.onResponse(null); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/persistence/BatchedDocumentsIteratorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/persistence/BatchedDocumentsIteratorTests.java index 0cb0d138d0e14..678b6b85d9d21 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/persistence/BatchedDocumentsIteratorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/persistence/BatchedDocumentsIteratorTests.java @@ -47,7 +47,6 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; - public class BatchedDocumentsIteratorTests extends ESTestCase { private static final String INDEX_NAME = ".ml-anomalies-foo"; @@ -79,9 +78,7 @@ public void testQueryReturnsNoResults() { } public void testCallingNextWhenHasNextIsFalseThrows() { - new ScrollResponsesMocker(client) - .addBatch(createJsonDoc("a"), createJsonDoc("b"), createJsonDoc("c")) - .finishMock(); + new ScrollResponsesMocker(client).addBatch(createJsonDoc("a"), createJsonDoc("b"), createJsonDoc("c")).finishMock(); testIterator.next(); assertFalse(testIterator.hasNext()); @@ -89,9 +86,11 @@ public void testCallingNextWhenHasNextIsFalseThrows() { } public void testQueryReturnsSingleBatch() { - ResponsesMocker scrollResponsesMocker = new ScrollResponsesMocker(client) - .addBatch(createJsonDoc("a"), createJsonDoc("b"), createJsonDoc("c")) - .finishMock(); + ResponsesMocker scrollResponsesMocker = new ScrollResponsesMocker(client).addBatch( + createJsonDoc("a"), + createJsonDoc("b"), + createJsonDoc("c") + ).finishMock(); assertTrue(testIterator.hasNext()); Deque batch = testIterator.next(); @@ -105,11 +104,11 @@ public void testQueryReturnsSingleBatch() { } public void testQueryReturnsThreeBatches() { - ResponsesMocker responsesMocker = new ScrollResponsesMocker(client) - .addBatch(createJsonDoc("a"), createJsonDoc("b"), createJsonDoc("c")) - .addBatch(createJsonDoc("d"), createJsonDoc("e")) - .addBatch(createJsonDoc("f")) - .finishMock(); + ResponsesMocker responsesMocker = new ScrollResponsesMocker(client).addBatch( + createJsonDoc("a"), + createJsonDoc("b"), + createJsonDoc("c") + ).addBatch(createJsonDoc("d"), createJsonDoc("e")).addBatch(createJsonDoc("f")).finishMock(); assertTrue(testIterator.hasNext()); @@ -146,7 +145,6 @@ private void givenClearScrollRequest() { }).when(client).execute(eq(ClearScrollAction.INSTANCE), any(), any()); } - abstract static class ResponsesMocker { protected Client client; protected List batches = new ArrayList<>(); @@ -170,7 +168,6 @@ ResponsesMocker addBatch(String... hits) { abstract ResponsesMocker finishMock(); - protected SearchResponse createSearchResponseWithHits(String... hits) { SearchHits searchHits = createHits(hits); SearchResponse searchResponse = mock(SearchResponse.class); @@ -191,7 +188,7 @@ void assertSearchRequest(String indexName) { List searchRequests = searchRequestCaptor.getAllValues(); assertThat(searchRequests.size(), equalTo(1)); SearchRequest searchRequest = searchRequests.get(0); - assertThat(searchRequest.indices(), equalTo(new String[] {indexName})); + assertThat(searchRequest.indices(), equalTo(new String[] { indexName })); assertThat(searchRequest.scroll().keepAlive(), equalTo(TimeValue.timeValueMinutes(5))); assertThat(searchRequest.source().query(), equalTo(QueryBuilders.matchAllQuery())); assertThat(searchRequest.source().trackTotalHitsUpTo(), is(SearchContext.TRACK_TOTAL_HITS_ACCURATE)); @@ -215,8 +212,7 @@ static class ScrollResponsesMocker extends ResponsesMocker { @Override @SuppressWarnings("unchecked") - ResponsesMocker finishMock() - { + ResponsesMocker finishMock() { if (batches.isEmpty()) { givenInitialResponse(); return this; @@ -256,8 +252,7 @@ static class SearchResponsesMocker extends ResponsesMocker { @Override @SuppressWarnings("unchecked") - ResponsesMocker finishMock() - { + ResponsesMocker finishMock() { if (batches.isEmpty()) { doAnswer(invocationOnMock -> { ActionListener listener = (ActionListener) invocationOnMock.getArguments()[2]; @@ -282,7 +277,6 @@ ResponsesMocker finishMock() } } - private static class TestIterator extends BatchedDocumentsIterator { TestIterator(OriginSettingClient client, String jobId) { super(client, jobId); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/persistence/LimitAwareBulkIndexerTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/persistence/LimitAwareBulkIndexerTests.java index f8f603d139b2b..e966e88bbd60b 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/persistence/LimitAwareBulkIndexerTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/persistence/LimitAwareBulkIndexerTests.java @@ -67,8 +67,7 @@ public void testAddAndExecuteIfNeeded_GivenRequestsReachingBatchSize() { } public void testNoRequests() { - try (LimitAwareBulkIndexer bulkIndexer = createIndexer(10000)) { - } + try (LimitAwareBulkIndexer bulkIndexer = createIndexer(10000)) {} assertThat(executedBulkRequests, is(empty())); } @@ -83,4 +82,3 @@ private static IndexRequest mockIndexRequest(long ramBytes) { return indexRequest; } } - diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/persistence/ResultsPersisterServiceTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/persistence/ResultsPersisterServiceTests.java index a208022d6b79b..7991c6b60d0b0 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/persistence/ResultsPersisterServiceTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/persistence/ResultsPersisterServiceTests.java @@ -74,31 +74,49 @@ public class ResultsPersisterServiceTests extends ESTestCase { // Constants for searchWithRetry tests private static final SearchRequest SEARCH_REQUEST = new SearchRequest("my-index"); - private static final SearchResponse SEARCH_RESPONSE_SUCCESS = - new SearchResponse(null, null, 1, 1, 0, 0, ShardSearchFailure.EMPTY_ARRAY, null); - private static final SearchResponse SEARCH_RESPONSE_FAILURE = - new SearchResponse(null, null, 1, 0, 0, 0, ShardSearchFailure.EMPTY_ARRAY, null); + private static final SearchResponse SEARCH_RESPONSE_SUCCESS = new SearchResponse( + null, + null, + 1, + 1, + 0, + 0, + ShardSearchFailure.EMPTY_ARRAY, + null + ); + private static final SearchResponse SEARCH_RESPONSE_FAILURE = new SearchResponse( + null, + null, + 1, + 0, + 0, + 0, + ShardSearchFailure.EMPTY_ARRAY, + null + ); // Constants for bulkIndexWithRetry tests - private static final IndexRequest INDEX_REQUEST_SUCCESS = - new IndexRequest("my-index").id("success").source(Collections.singletonMap("data", "success")); - private static final IndexRequest INDEX_REQUEST_FAILURE = - new IndexRequest("my-index").id("fail").source(Collections.singletonMap("data", "fail")); - private static final BulkItemResponse BULK_ITEM_RESPONSE_SUCCESS = - BulkItemResponse.success( + private static final IndexRequest INDEX_REQUEST_SUCCESS = new IndexRequest("my-index").id("success") + .source(Collections.singletonMap("data", "success")); + private static final IndexRequest INDEX_REQUEST_FAILURE = new IndexRequest("my-index").id("fail") + .source(Collections.singletonMap("data", "fail")); + private static final BulkItemResponse BULK_ITEM_RESPONSE_SUCCESS = BulkItemResponse.success( + 1, + DocWriteRequest.OpType.INDEX, + new IndexResponse( + new ShardId(AnomalyDetectorsIndex.jobResultsIndexPrefix() + "shared", "uuid", 1), + INDEX_REQUEST_SUCCESS.id(), + 0, + 0, 1, - DocWriteRequest.OpType.INDEX, - new IndexResponse(new ShardId(AnomalyDetectorsIndex.jobResultsIndexPrefix() + "shared", "uuid", 1), - INDEX_REQUEST_SUCCESS.id(), - 0, - 0, - 1, - true)); - private static final BulkItemResponse BULK_ITEM_RESPONSE_FAILURE = - BulkItemResponse.failure( - 2, - DocWriteRequest.OpType.INDEX, - new BulkItemResponse.Failure("my-index", "fail", new Exception("boom"))); + true + ) + ); + private static final BulkItemResponse BULK_ITEM_RESPONSE_FAILURE = BulkItemResponse.failure( + 2, + DocWriteRequest.OpType.INDEX, + new BulkItemResponse.Failure("my-index", "fail", new Exception("boom")) + ); private Client client; private OriginSettingClient originSettingClient; @@ -112,8 +130,7 @@ public void setUpTests() { } public void testSearchWithRetries_ImmediateSuccess() { - doAnswer(withResponse(SEARCH_RESPONSE_SUCCESS)) - .when(client).execute(eq(SearchAction.INSTANCE), eq(SEARCH_REQUEST), any()); + doAnswer(withResponse(SEARCH_RESPONSE_SUCCESS)).when(client).execute(eq(SearchAction.INSTANCE), eq(SEARCH_REQUEST), any()); List messages = new ArrayList<>(); SearchResponse searchResponse = resultsPersisterService.searchWithRetry(SEARCH_REQUEST, JOB_ID, () -> true, messages::add); @@ -124,8 +141,8 @@ public void testSearchWithRetries_ImmediateSuccess() { } public void testSearchWithRetries_SuccessAfterRetry() { - doAnswerWithResponses(SEARCH_RESPONSE_FAILURE, SEARCH_RESPONSE_SUCCESS) - .when(client).execute(eq(SearchAction.INSTANCE), eq(SEARCH_REQUEST), any()); + doAnswerWithResponses(SEARCH_RESPONSE_FAILURE, SEARCH_RESPONSE_SUCCESS).when(client) + .execute(eq(SearchAction.INSTANCE), eq(SEARCH_REQUEST), any()); List messages = new ArrayList<>(); SearchResponse searchResponse = resultsPersisterService.searchWithRetry(SEARCH_REQUEST, JOB_ID, () -> true, messages::add); @@ -136,9 +153,9 @@ public void testSearchWithRetries_SuccessAfterRetry() { } public void testSearchWithRetries_SuccessAfterRetryDueToException() { - doAnswer(withFailure(new IndexPrimaryShardNotAllocatedException(new Index("my-index", "UUID")))) - .doAnswer(withResponse(SEARCH_RESPONSE_SUCCESS)) - .when(client).execute(eq(SearchAction.INSTANCE), eq(SEARCH_REQUEST), any()); + doAnswer(withFailure(new IndexPrimaryShardNotAllocatedException(new Index("my-index", "UUID")))).doAnswer( + withResponse(SEARCH_RESPONSE_SUCCESS) + ).when(client).execute(eq(SearchAction.INSTANCE), eq(SEARCH_REQUEST), any()); List messages = new ArrayList<>(); SearchResponse searchResponse = resultsPersisterService.searchWithRetry(SEARCH_REQUEST, JOB_ID, () -> true, messages::add); @@ -151,14 +168,13 @@ public void testSearchWithRetries_SuccessAfterRetryDueToException() { private void testSearchWithRetries_FailureAfterTooManyRetries(int maxFailureRetries) { resultsPersisterService.setMaxFailureRetries(maxFailureRetries); - doAnswer(withResponse(SEARCH_RESPONSE_FAILURE)) - .when(client).execute(eq(SearchAction.INSTANCE), eq(SEARCH_REQUEST), any()); + doAnswer(withResponse(SEARCH_RESPONSE_FAILURE)).when(client).execute(eq(SearchAction.INSTANCE), eq(SEARCH_REQUEST), any()); List messages = new ArrayList<>(); - ElasticsearchException e = - expectThrows( - ElasticsearchException.class, - () -> resultsPersisterService.searchWithRetry(SEARCH_REQUEST, JOB_ID, () -> true, messages::add)); + ElasticsearchException e = expectThrows( + ElasticsearchException.class, + () -> resultsPersisterService.searchWithRetry(SEARCH_REQUEST, JOB_ID, () -> true, messages::add) + ); assertThat(e.getMessage(), containsString("search failed with status")); assertThat(messages, hasSize(maxFailureRetries)); @@ -178,14 +194,13 @@ public void testSearchWithRetries_FailureAfterTooManyRetries_10() { } public void testSearchWithRetries_Failure_ShouldNotRetryFromTheBeginning() { - doAnswer(withResponse(SEARCH_RESPONSE_FAILURE)) - .when(client).execute(eq(SearchAction.INSTANCE), eq(SEARCH_REQUEST), any()); + doAnswer(withResponse(SEARCH_RESPONSE_FAILURE)).when(client).execute(eq(SearchAction.INSTANCE), eq(SEARCH_REQUEST), any()); List messages = new ArrayList<>(); - ElasticsearchException e = - expectThrows( - ElasticsearchException.class, - () -> resultsPersisterService.searchWithRetry(SEARCH_REQUEST, JOB_ID, () -> false, messages::add)); + ElasticsearchException e = expectThrows( + ElasticsearchException.class, + () -> resultsPersisterService.searchWithRetry(SEARCH_REQUEST, JOB_ID, () -> false, messages::add) + ); assertThat(e.getMessage(), containsString("search failed with status SERVICE_UNAVAILABLE")); assertThat(messages, empty()); @@ -196,17 +211,15 @@ public void testSearchWithRetries_Failure_ShouldNotRetryAfterRandomNumberOfRetri int maxFailureRetries = 10; resultsPersisterService.setMaxFailureRetries(maxFailureRetries); - doAnswer(withResponse(SEARCH_RESPONSE_FAILURE)) - .when(client).execute(eq(SearchAction.INSTANCE), eq(SEARCH_REQUEST), any()); + doAnswer(withResponse(SEARCH_RESPONSE_FAILURE)).when(client).execute(eq(SearchAction.INSTANCE), eq(SEARCH_REQUEST), any()); int maxRetries = randomIntBetween(1, maxFailureRetries); List messages = new ArrayList<>(); - ElasticsearchException e = - expectThrows( - ElasticsearchException.class, - () -> resultsPersisterService.searchWithRetry(SEARCH_REQUEST, JOB_ID, shouldRetryUntil(maxRetries), messages::add)); - assertThat( - e.getMessage(), containsString("search failed with status SERVICE_UNAVAILABLE")); + ElasticsearchException e = expectThrows( + ElasticsearchException.class, + () -> resultsPersisterService.searchWithRetry(SEARCH_REQUEST, JOB_ID, shouldRetryUntil(maxRetries), messages::add) + ); + assertThat(e.getMessage(), containsString("search failed with status SERVICE_UNAVAILABLE")); assertThat(messages, hasSize(maxRetries)); verify(client, times(maxRetries + 1)).execute(eq(SearchAction.INSTANCE), eq(SEARCH_REQUEST), any()); @@ -215,13 +228,13 @@ public void testSearchWithRetries_Failure_ShouldNotRetryAfterRandomNumberOfRetri public void testSearchWithRetries_FailureOnIrrecoverableError() { resultsPersisterService.setMaxFailureRetries(5); - doAnswer(withFailure(new ElasticsearchStatusException("bad search request", RestStatus.BAD_REQUEST))) - .when(client).execute(eq(SearchAction.INSTANCE), eq(SEARCH_REQUEST), any()); + doAnswer(withFailure(new ElasticsearchStatusException("bad search request", RestStatus.BAD_REQUEST))).when(client) + .execute(eq(SearchAction.INSTANCE), eq(SEARCH_REQUEST), any()); - ElasticsearchException e = - expectThrows( - ElasticsearchException.class, - () -> resultsPersisterService.searchWithRetry(SEARCH_REQUEST, JOB_ID, () -> true, (s) -> {})); + ElasticsearchException e = expectThrows( + ElasticsearchException.class, + () -> resultsPersisterService.searchWithRetry(SEARCH_REQUEST, JOB_ID, () -> true, (s) -> {}) + ); assertThat(e.getMessage(), containsString("bad search request")); verify(client, times(1)).execute(eq(SearchAction.INSTANCE), eq(SEARCH_REQUEST), any()); @@ -230,6 +243,7 @@ public void testSearchWithRetries_FailureOnIrrecoverableError() { private static Supplier shouldRetryUntil(int maxRetries) { return new Supplier<>() { int retries = 0; + @Override public Boolean get() { return ++retries <= maxRetries; @@ -239,9 +253,9 @@ public Boolean get() { public void testBulkRequestChangeOnFailures() { doAnswerWithResponses( - new BulkResponse(new BulkItemResponse[]{BULK_ITEM_RESPONSE_FAILURE, BULK_ITEM_RESPONSE_SUCCESS}, 0L), - new BulkResponse(new BulkItemResponse[0], 0L)) - .when(client).execute(eq(BulkAction.INSTANCE), any(), any()); + new BulkResponse(new BulkItemResponse[] { BULK_ITEM_RESPONSE_FAILURE, BULK_ITEM_RESPONSE_SUCCESS }, 0L), + new BulkResponse(new BulkItemResponse[0], 0L) + ).when(client).execute(eq(BulkAction.INSTANCE), any(), any()); BulkRequest bulkRequest = new BulkRequest(); bulkRequest.add(INDEX_REQUEST_FAILURE); @@ -251,7 +265,7 @@ public void testBulkRequestChangeOnFailures() { resultsPersisterService.bulkIndexWithRetry(bulkRequest, JOB_ID, () -> true, lastMessage::set); - ArgumentCaptor captor = ArgumentCaptor.forClass(BulkRequest.class); + ArgumentCaptor captor = ArgumentCaptor.forClass(BulkRequest.class); verify(client, times(2)).execute(eq(BulkAction.INSTANCE), captor.capture(), any()); List requests = captor.getAllValues(); @@ -267,18 +281,21 @@ public void testBulkRequestChangeOnIrrecoverableFailures() { BulkItemResponse irrecoverable = BulkItemResponse.failure( 2, DocWriteRequest.OpType.INDEX, - new BulkItemResponse.Failure("my-index", "fail", new ElasticsearchStatusException("boom", RestStatus.BAD_REQUEST))); + new BulkItemResponse.Failure("my-index", "fail", new ElasticsearchStatusException("boom", RestStatus.BAD_REQUEST)) + ); doAnswerWithResponses( - new BulkResponse(new BulkItemResponse[]{irrecoverable, BULK_ITEM_RESPONSE_SUCCESS}, 0L), - new BulkResponse(new BulkItemResponse[0], 0L)) - .when(client).execute(eq(BulkAction.INSTANCE), any(), any()); + new BulkResponse(new BulkItemResponse[] { irrecoverable, BULK_ITEM_RESPONSE_SUCCESS }, 0L), + new BulkResponse(new BulkItemResponse[0], 0L) + ).when(client).execute(eq(BulkAction.INSTANCE), any(), any()); BulkRequest bulkRequest = new BulkRequest(); bulkRequest.add(INDEX_REQUEST_FAILURE); bulkRequest.add(INDEX_REQUEST_SUCCESS); - ElasticsearchException ex = expectThrows(ElasticsearchException.class, - () -> resultsPersisterService.bulkIndexWithRetry(bulkRequest, JOB_ID, () -> true, (s)->{})); + ElasticsearchException ex = expectThrows( + ElasticsearchException.class, + () -> resultsPersisterService.bulkIndexWithRetry(bulkRequest, JOB_ID, () -> true, (s) -> {}) + ); verify(client).execute(eq(BulkAction.INSTANCE), any(), any()); assertThat(ex.getMessage(), containsString("experienced failure that cannot be automatically retried.")); @@ -286,9 +303,9 @@ public void testBulkRequestChangeOnIrrecoverableFailures() { public void testBulkRequestDoesNotRetryWhenSupplierIsFalse() { doAnswerWithResponses( - new BulkResponse(new BulkItemResponse[]{BULK_ITEM_RESPONSE_FAILURE, BULK_ITEM_RESPONSE_SUCCESS}, 0L), - new BulkResponse(new BulkItemResponse[0], 0L)) - .when(client).execute(eq(BulkAction.INSTANCE), any(), any()); + new BulkResponse(new BulkItemResponse[] { BULK_ITEM_RESPONSE_FAILURE, BULK_ITEM_RESPONSE_SUCCESS }, 0L), + new BulkResponse(new BulkItemResponse[0], 0L) + ).when(client).execute(eq(BulkAction.INSTANCE), any(), any()); BulkRequest bulkRequest = new BulkRequest(); bulkRequest.add(INDEX_REQUEST_FAILURE); @@ -296,8 +313,10 @@ public void testBulkRequestDoesNotRetryWhenSupplierIsFalse() { AtomicReference lastMessage = new AtomicReference<>(); - expectThrows(ElasticsearchException.class, - () -> resultsPersisterService.bulkIndexWithRetry(bulkRequest, JOB_ID, () -> false, lastMessage::set)); + expectThrows( + ElasticsearchException.class, + () -> resultsPersisterService.bulkIndexWithRetry(bulkRequest, JOB_ID, () -> false, lastMessage::set) + ); verify(client, times(1)).execute(eq(BulkAction.INSTANCE), any(), any()); assertThat(lastMessage.get(), is(nullValue())); @@ -307,16 +326,18 @@ public void testBulkRequestRetriesConfiguredAttemptNumber() { int maxFailureRetries = 10; resultsPersisterService.setMaxFailureRetries(maxFailureRetries); - doAnswer(withResponse(new BulkResponse(new BulkItemResponse[]{BULK_ITEM_RESPONSE_FAILURE}, 0L))) - .when(client).execute(eq(BulkAction.INSTANCE), any(), any()); + doAnswer(withResponse(new BulkResponse(new BulkItemResponse[] { BULK_ITEM_RESPONSE_FAILURE }, 0L))).when(client) + .execute(eq(BulkAction.INSTANCE), any(), any()); BulkRequest bulkRequest = new BulkRequest(); bulkRequest.add(INDEX_REQUEST_FAILURE); AtomicReference lastMessage = new AtomicReference<>(); - expectThrows(ElasticsearchException.class, - () -> resultsPersisterService.bulkIndexWithRetry(bulkRequest, JOB_ID, () -> true, lastMessage::set)); + expectThrows( + ElasticsearchException.class, + () -> resultsPersisterService.bulkIndexWithRetry(bulkRequest, JOB_ID, () -> true, lastMessage::set) + ); verify(client, times(maxFailureRetries + 1)).execute(eq(BulkAction.INSTANCE), any(), any()); assertThat(lastMessage.get(), containsString("failed to index after [10] attempts. Will attempt again")); @@ -324,9 +345,9 @@ public void testBulkRequestRetriesConfiguredAttemptNumber() { public void testBulkRequestRetriesMsgHandlerIsCalled() { doAnswerWithResponses( - new BulkResponse(new BulkItemResponse[]{BULK_ITEM_RESPONSE_FAILURE, BULK_ITEM_RESPONSE_SUCCESS}, 0L), - new BulkResponse(new BulkItemResponse[0], 0L)) - .when(client).execute(eq(BulkAction.INSTANCE), any(), any()); + new BulkResponse(new BulkItemResponse[] { BULK_ITEM_RESPONSE_FAILURE, BULK_ITEM_RESPONSE_SUCCESS }, 0L), + new BulkResponse(new BulkItemResponse[0], 0L) + ).when(client).execute(eq(BulkAction.INSTANCE), any(), any()); BulkRequest bulkRequest = new BulkRequest(); bulkRequest.add(INDEX_REQUEST_FAILURE); @@ -336,7 +357,7 @@ public void testBulkRequestRetriesMsgHandlerIsCalled() { resultsPersisterService.bulkIndexWithRetry(bulkRequest, JOB_ID, () -> true, lastMessage::set); - ArgumentCaptor captor = ArgumentCaptor.forClass(BulkRequest.class); + ArgumentCaptor captor = ArgumentCaptor.forClass(BulkRequest.class); verify(client, times(2)).execute(eq(BulkAction.INSTANCE), captor.capture(), any()); List requests = captor.getAllValues(); @@ -370,13 +391,19 @@ private static Answer withFailure(Exception failure) { public static ResultsPersisterService buildResultsPersisterService(OriginSettingClient client) { ThreadPool tp = mock(ThreadPool.class); - ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, - new HashSet<>(Arrays.asList(InferenceProcessor.MAX_INFERENCE_PROCESSORS, - MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING, - OperationRouting.USE_ADAPTIVE_REPLICA_SELECTION_SETTING, - ClusterService.USER_DEFINED_METADATA, - ResultsPersisterService.PERSIST_RESULTS_MAX_RETRIES, - ClusterApplierService.CLUSTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING))); + ClusterSettings clusterSettings = new ClusterSettings( + Settings.EMPTY, + new HashSet<>( + Arrays.asList( + InferenceProcessor.MAX_INFERENCE_PROCESSORS, + MasterService.MASTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING, + OperationRouting.USE_ADAPTIVE_REPLICA_SELECTION_SETTING, + ClusterService.USER_DEFINED_METADATA, + ResultsPersisterService.PERSIST_RESULTS_MAX_RETRIES, + ClusterApplierService.CLUSTER_SERVICE_SLOW_TASK_LOGGING_THRESHOLD_SETTING + ) + ) + ); ClusterService clusterService = new ClusterService(Settings.EMPTY, clusterSettings, tp); ExecutorService executor = mock(ExecutorService.class); doAnswer(invocationOnMock -> { @@ -387,9 +414,7 @@ public static ResultsPersisterService buildResultsPersisterService(OriginSetting doAnswer(invocationOnMock -> { ((Runnable) invocationOnMock.getArguments()[0]).run(); return null; - }).when(tp).schedule( - any(Runnable.class), any(TimeValue.class), any(String.class) - ); + }).when(tp).schedule(any(Runnable.class), any(TimeValue.class), any(String.class)); return new ResultsPersisterService(tp, client, clusterService, Settings.EMPTY); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/persistence/SearchAfterDocumentsIteratorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/persistence/SearchAfterDocumentsIteratorTests.java index 24ab7cc334383..9f55b87217c32 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/persistence/SearchAfterDocumentsIteratorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/persistence/SearchAfterDocumentsIteratorTests.java @@ -38,10 +38,8 @@ public void setUpMocks() { originSettingClient = MockOriginSettingClient.mockOriginSettingClient(client, ClientHelper.ML_ORIGIN); } - public void testHasNext() - { - new BatchedDocumentsIteratorTests.SearchResponsesMocker(client) - .addBatch(createJsonDoc("a"), createJsonDoc("b"), createJsonDoc("c")) + public void testHasNext() { + new BatchedDocumentsIteratorTests.SearchResponsesMocker(client).addBatch(createJsonDoc("a"), createJsonDoc("b"), createJsonDoc("c")) .addBatch(createJsonDoc("d"), createJsonDoc("e")) .finishMock(); @@ -59,11 +57,8 @@ public void testHasNext() ESTestCase.expectThrows(NoSuchElementException.class, testIterator::next); } - public void testFirstBatchIsEmpty() - { - new BatchedDocumentsIteratorTests.SearchResponsesMocker(client) - .addBatch() - .finishMock(); + public void testFirstBatchIsEmpty() { + new BatchedDocumentsIteratorTests.SearchResponsesMocker(client).addBatch().finishMock(); TestIterator testIterator = new TestIterator(originSettingClient, INDEX_NAME); assertTrue(testIterator.hasNext()); @@ -72,10 +67,8 @@ public void testFirstBatchIsEmpty() assertFalse(testIterator.hasNext()); } - public void testExtractSearchAfterValuesSet() - { - new BatchedDocumentsIteratorTests.SearchResponsesMocker(client) - .addBatch(createJsonDoc("a"), createJsonDoc("b"), createJsonDoc("c")) + public void testExtractSearchAfterValuesSet() { + new BatchedDocumentsIteratorTests.SearchResponsesMocker(client).addBatch(createJsonDoc("a"), createJsonDoc("b"), createJsonDoc("c")) .addBatch(createJsonDoc("d"), createJsonDoc("e")) .finishMock(); @@ -84,12 +77,12 @@ public void testExtractSearchAfterValuesSet() Deque next = testIterator.next(); assertThat(next, not(empty())); Object[] values = testIterator.searchAfterFields(); - assertArrayEquals(new Object[] {"c"}, values); + assertArrayEquals(new Object[] { "c" }, values); next = testIterator.next(); assertThat(next, not(empty())); values = testIterator.searchAfterFields(); - assertArrayEquals(new Object[] {"e"}, values); + assertArrayEquals(new Object[] { "e" }, values); } private static class TestIterator extends SearchAfterDocumentsIterator { @@ -117,12 +110,12 @@ protected String map(SearchHit hit) { @Override protected Object[] searchAfterFields() { - return new Object[] {searchAfterValue}; + return new Object[] { searchAfterValue }; } @Override protected void extractSearchAfterFields(SearchHit lastSearchHit) { - searchAfterValue = (String)lastSearchHit.getSourceAsMap().get("name"); + searchAfterValue = (String) lastSearchHit.getSourceAsMap().get("name"); } } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/time/DateTimeFormatterTimestampConverterTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/time/DateTimeFormatterTimestampConverterTests.java index e4f8c510bea3c..1bc027b8f786e 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/time/DateTimeFormatterTimestampConverterTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/utils/time/DateTimeFormatterTimestampConverterTests.java @@ -17,7 +17,6 @@ import java.time.ZonedDateTime; import java.time.format.DateTimeParseException; - public class DateTimeFormatterTimestampConverterTests extends ESTestCase { public void testOfPattern_GivenPatternIsOnlyYear() { @@ -26,19 +25,25 @@ public void testOfPattern_GivenPatternIsOnlyYear() { public void testOfPattern_GivenPatternIsOnlyDate() { - ESTestCase.expectThrows(IllegalArgumentException.class, - () -> DateTimeFormatterTimestampConverter.ofPattern("y-M-d", ZoneOffset.UTC)); + ESTestCase.expectThrows( + IllegalArgumentException.class, + () -> DateTimeFormatterTimestampConverter.ofPattern("y-M-d", ZoneOffset.UTC) + ); } public void testOfPattern_GivenPatternIsOnlyTime() { - ESTestCase.expectThrows(IllegalArgumentException.class, - () -> DateTimeFormatterTimestampConverter.ofPattern("HH:mm:ss", ZoneOffset.UTC)); + ESTestCase.expectThrows( + IllegalArgumentException.class, + () -> DateTimeFormatterTimestampConverter.ofPattern("HH:mm:ss", ZoneOffset.UTC) + ); } public void testOfPattern_GivenPatternIsUsingYearInsteadOfYearOfEra() { - ESTestCase.expectThrows(IllegalArgumentException.class, - () -> DateTimeFormatterTimestampConverter.ofPattern("uuuu-MM-dd HH:mm:ss", ZoneOffset.UTC)); + ESTestCase.expectThrows( + IllegalArgumentException.class, + () -> DateTimeFormatterTimestampConverter.ofPattern("uuuu-MM-dd HH:mm:ss", ZoneOffset.UTC) + ); } public void testToEpochSeconds_GivenValidTimestampDoesNotFollowPattern() { @@ -86,8 +91,7 @@ public void testToEpochSeconds_GivenPatternHasDateWithoutYearAndTimeWithoutTimeZ } public void testToEpochMillis_GivenPatternHasFullDateAndTimeWithTimeZone() { - assertEquals(1395703820542L, - toEpochMillis("2014-03-25 01:30:20.542 +02:00", "yyyy-MM-dd HH:mm:ss.SSS XXX")); + assertEquals(1395703820542L, toEpochMillis("2014-03-25 01:30:20.542 +02:00", "yyyy-MM-dd HH:mm:ss.SSS XXX")); } private static long toEpochSeconds(String timestamp, String pattern) { diff --git a/x-pack/plugin/monitoring/src/internalClusterTest/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporterIT.java b/x-pack/plugin/monitoring/src/internalClusterTest/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporterIT.java index 501b1257f51d8..912d88e9c942c 100644 --- a/x-pack/plugin/monitoring/src/internalClusterTest/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporterIT.java +++ b/x-pack/plugin/monitoring/src/internalClusterTest/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporterIT.java @@ -7,6 +7,7 @@ package org.elasticsearch.xpack.monitoring.exporter.http; import com.unboundid.util.Base64; + import org.elasticsearch.Version; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.DocWriteRequest; @@ -16,23 +17,17 @@ import org.elasticsearch.client.Requests; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.IndexTemplateMetadata; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.settings.MockSecureSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.time.DateFormatter; import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.xcontent.json.JsonXContent; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.core.Tuple; import org.elasticsearch.env.Environment; import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.license.TestUtils; @@ -43,6 +38,12 @@ import org.elasticsearch.test.http.MockRequest; import org.elasticsearch.test.http.MockResponse; import org.elasticsearch.test.http.MockWebServer; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc; import org.elasticsearch.xpack.core.ssl.SSLService; import org.elasticsearch.xpack.monitoring.LocalStateMonitoring; @@ -86,12 +87,12 @@ import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.notNullValue; -@ESIntegTestCase.ClusterScope(scope = Scope.TEST, - numDataNodes = 1, numClientNodes = 0, supportsDedicatedMasters = false) +@ESIntegTestCase.ClusterScope(scope = Scope.TEST, numDataNodes = 1, numClientNodes = 0, supportsDedicatedMasters = false) public class HttpExporterIT extends MonitoringIntegTestCase { - private final List clusterAlertBlacklist = - rarely() ? randomSubsetOf(Arrays.asList(ClusterAlertsUtil.WATCH_IDS)) : Collections.emptyList(); + private final List clusterAlertBlacklist = rarely() + ? randomSubsetOf(Arrays.asList(ClusterAlertsUtil.WATCH_IDS)) + : Collections.emptyList(); private final boolean remoteClusterAllowsWatcher = randomBoolean(); private final boolean currentLicenseAllowsWatcher = true; private final boolean watcherAlreadyExists = randomBoolean(); @@ -101,7 +102,8 @@ public class HttpExporterIT extends MonitoringIntegTestCase { private MockWebServer webServer; - private MockSecureSettings mockSecureSettings = new MockSecureSettings(); + private MockSecureSettings mockSecureSettings = new MockSecureSettings(); + @Override protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { @@ -152,17 +154,13 @@ public void testExport() throws Exception { final Settings settings = baseSettings().build(); enqueueGetClusterVersionResponse(Version.CURRENT); - enqueueSetupResponses(webServer, - true, - remoteClusterAllowsWatcher, currentLicenseAllowsWatcher, watcherAlreadyExists); + enqueueSetupResponses(webServer, true, remoteClusterAllowsWatcher, currentLicenseAllowsWatcher, watcherAlreadyExists); enqueueResponse(200, "{\"errors\": false, \"msg\": \"successful bulk request\"}"); final int nbDocs = randomIntBetween(1, 25); export(settings, newRandomMonitoringDocs(nbDocs)); - assertMonitorResources(webServer, - true, - remoteClusterAllowsWatcher, currentLicenseAllowsWatcher, watcherAlreadyExists); + assertMonitorResources(webServer, true, remoteClusterAllowsWatcher, currentLicenseAllowsWatcher, watcherAlreadyExists); assertBulk(webServer, nbDocs); } @@ -172,16 +170,13 @@ public void testSecureSetting() throws Exception { final String authHeaderValue = Base64.encode(userName + ":" + securePassword1); final String authHeaderValue2 = Base64.encode(userName + ":" + securePassword2); - Settings settings = secureSettings(securePassword1) - .build(); + Settings settings = secureSettings(securePassword1).build(); PluginsService pluginsService = internalCluster().getInstances(PluginsService.class).iterator().next(); LocalStateMonitoring localStateMonitoring = pluginsService.filterPlugins(LocalStateMonitoring.class).iterator().next(); localStateMonitoring.getMonitoring().reload(settings); enqueueGetClusterVersionResponse(Version.CURRENT); - enqueueSetupResponses(webServer, - true, - remoteClusterAllowsWatcher, currentLicenseAllowsWatcher, watcherAlreadyExists); + enqueueSetupResponses(webServer, true, remoteClusterAllowsWatcher, currentLicenseAllowsWatcher, watcherAlreadyExists); enqueueResponse(200, "{\"errors\": false, \"msg\": \"successful bulk request\"}"); final int nbDocs = randomIntBetween(1, 25); @@ -192,9 +187,7 @@ public void testSecureSetting() throws Exception { settings = secureSettings(securePassword2).build(); localStateMonitoring.getMonitoring().reload(settings); enqueueGetClusterVersionResponse(Version.CURRENT); - enqueueSetupResponses(webServer, - true, - remoteClusterAllowsWatcher, currentLicenseAllowsWatcher, watcherAlreadyExists); + enqueueSetupResponses(webServer, true, remoteClusterAllowsWatcher, currentLicenseAllowsWatcher, watcherAlreadyExists); enqueueResponse(200, "{\"errors\": false, \"msg\": \"successful bulk request\"}"); export(settings, newRandomMonitoringDocs(nbDocs)); @@ -211,25 +204,27 @@ public void testExportWithHeaders() throws Exception { headers.put("X-Found-Cluster", new String[] { headerValue }); headers.put("Array-Check", array); - final Settings settings = baseSettings() - .put("xpack.monitoring.exporters._http.headers.X-Cloud-Cluster", headerValue) - .put("xpack.monitoring.exporters._http.headers.X-Found-Cluster", headerValue) - .putList("xpack.monitoring.exporters._http.headers.Array-Check", array) - .build(); + final Settings settings = baseSettings().put("xpack.monitoring.exporters._http.headers.X-Cloud-Cluster", headerValue) + .put("xpack.monitoring.exporters._http.headers.X-Found-Cluster", headerValue) + .putList("xpack.monitoring.exporters._http.headers.Array-Check", array) + .build(); enqueueGetClusterVersionResponse(Version.CURRENT); - enqueueSetupResponses(webServer, - true, - remoteClusterAllowsWatcher, currentLicenseAllowsWatcher, watcherAlreadyExists); + enqueueSetupResponses(webServer, true, remoteClusterAllowsWatcher, currentLicenseAllowsWatcher, watcherAlreadyExists); enqueueResponse(200, "{\"errors\": false, \"msg\": \"successful bulk request\"}"); final int nbDocs = randomIntBetween(1, 25); export(settings, newRandomMonitoringDocs(nbDocs)); - assertMonitorResources(webServer, - true, - remoteClusterAllowsWatcher, currentLicenseAllowsWatcher, watcherAlreadyExists, - headers, null); + assertMonitorResources( + webServer, + true, + remoteClusterAllowsWatcher, + currentLicenseAllowsWatcher, + watcherAlreadyExists, + headers, + null + ); assertBulk(webServer, nbDocs, headers, null); } @@ -261,28 +256,33 @@ public void testExportWithBasePath() throws Exception { basePath = "/" + basePath; } - final Settings.Builder builder = baseSettings() - .put("xpack.monitoring.exporters._http.proxy.base_path", basePath + (randomBoolean() ? "/" : "")); + final Settings.Builder builder = baseSettings().put( + "xpack.monitoring.exporters._http.proxy.base_path", + basePath + (randomBoolean() ? "/" : "") + ); if (useHeaders) { builder.put("xpack.monitoring.exporters._http.headers.X-Cloud-Cluster", headerValue) - .put("xpack.monitoring.exporters._http.headers.X-Found-Cluster", headerValue) - .putList("xpack.monitoring.exporters._http.headers.Array-Check", array); + .put("xpack.monitoring.exporters._http.headers.X-Found-Cluster", headerValue) + .putList("xpack.monitoring.exporters._http.headers.Array-Check", array); } enqueueGetClusterVersionResponse(Version.CURRENT); - enqueueSetupResponses(webServer, - true, - remoteClusterAllowsWatcher, currentLicenseAllowsWatcher, watcherAlreadyExists); + enqueueSetupResponses(webServer, true, remoteClusterAllowsWatcher, currentLicenseAllowsWatcher, watcherAlreadyExists); enqueueResponse(200, "{\"errors\": false}"); final int nbDocs = randomIntBetween(1, 25); export(builder.build(), newRandomMonitoringDocs(nbDocs)); - assertMonitorResources(webServer, - true, - remoteClusterAllowsWatcher, currentLicenseAllowsWatcher, watcherAlreadyExists, - headers, basePath); + assertMonitorResources( + webServer, + true, + remoteClusterAllowsWatcher, + currentLicenseAllowsWatcher, + watcherAlreadyExists, + headers, + basePath + ); assertBulk(webServer, nbDocs, headers, basePath); } @@ -290,66 +290,60 @@ public void testHostChangeReChecksTemplate() throws Exception { final Settings settings = baseSettings().build(); enqueueGetClusterVersionResponse(Version.CURRENT); - enqueueSetupResponses(webServer, - true, - remoteClusterAllowsWatcher, currentLicenseAllowsWatcher, watcherAlreadyExists); + enqueueSetupResponses(webServer, true, remoteClusterAllowsWatcher, currentLicenseAllowsWatcher, watcherAlreadyExists); enqueueResponse(200, "{\"errors\": false}"); export(settings, Collections.singletonList(newRandomMonitoringDoc())); - assertMonitorResources(webServer, - true, - remoteClusterAllowsWatcher, currentLicenseAllowsWatcher, watcherAlreadyExists); + assertMonitorResources(webServer, true, remoteClusterAllowsWatcher, currentLicenseAllowsWatcher, watcherAlreadyExists); assertBulk(webServer); try (MockWebServer secondWebServer = createMockWebServer()) { final Settings newSettings = Settings.builder() - .put(settings) - .putList("xpack.monitoring.exporters._http.host", getFormattedAddress(secondWebServer)) - .build(); + .put(settings) + .putList("xpack.monitoring.exporters._http.host", getFormattedAddress(secondWebServer)) + .build(); enqueueGetClusterVersionResponse(secondWebServer, Version.CURRENT); - enqueueSetupResponses(secondWebServer, true, remoteClusterAllowsWatcher, currentLicenseAllowsWatcher, - watcherAlreadyExists); + enqueueSetupResponses(secondWebServer, true, remoteClusterAllowsWatcher, currentLicenseAllowsWatcher, watcherAlreadyExists); enqueueResponse(secondWebServer, 200, "{\"errors\": false}"); // second event export(newSettings, Collections.singletonList(newRandomMonitoringDoc())); - assertMonitorResources(secondWebServer, true, remoteClusterAllowsWatcher, currentLicenseAllowsWatcher, - watcherAlreadyExists); + assertMonitorResources(secondWebServer, true, remoteClusterAllowsWatcher, currentLicenseAllowsWatcher, watcherAlreadyExists); assertBulk(secondWebServer); } } public void testUnsupportedClusterVersion() throws Exception { final Settings settings = Settings.builder() - .put("xpack.monitoring.exporters._http.type", "http") - .put("xpack.monitoring.exporters._http.host", getFormattedAddress(webServer)) - .build(); + .put("xpack.monitoring.exporters._http.type", "http") + .put("xpack.monitoring.exporters._http.host", getFormattedAddress(webServer)) + .build(); // returning an unsupported cluster version enqueueGetClusterVersionResponse( - randomFrom(Version.fromString("0.18.0"), - Version.fromString("1.0.0"), - Version.fromString("1.4.0"), - Version.fromString("2.4.0"), - Version.fromString("5.0.0"), - Version.fromString("5.4.0"))); + randomFrom( + Version.fromString("0.18.0"), + Version.fromString("1.0.0"), + Version.fromString("1.4.0"), + Version.fromString("2.4.0"), + Version.fromString("5.0.0"), + Version.fromString("5.4.0") + ) + ); // ensure that the exporter is not able to be used try (HttpExporter exporter = createHttpExporter(settings)) { final CountDownLatch awaitResponseAndClose = new CountDownLatch(1); - final ActionListener listener = ActionListener.wrap( - bulk -> { - assertNull(bulk); + final ActionListener listener = ActionListener.wrap(bulk -> { + assertNull(bulk); - awaitResponseAndClose.countDown(); - }, - e -> fail(e.getMessage()) - ); + awaitResponseAndClose.countDown(); + }, e -> fail(e.getMessage())); exporter.openBulk(listener); @@ -370,22 +364,17 @@ public void testRemoteTemplatesNotPresent() throws Exception { // returning an unsupported cluster version enqueueGetClusterVersionResponse(Version.CURRENT); - enqueueSetupResponses(webServer, - false, - remoteClusterAllowsWatcher, currentLicenseAllowsWatcher, watcherAlreadyExists); + enqueueSetupResponses(webServer, false, remoteClusterAllowsWatcher, currentLicenseAllowsWatcher, watcherAlreadyExists); // ensure that the exporter is not able to be used try (HttpExporter exporter = createHttpExporter(settings)) { final CountDownLatch awaitResponseAndClose = new CountDownLatch(1); - final ActionListener listener = ActionListener.wrap( - bulk -> { - assertNull(bulk); + final ActionListener listener = ActionListener.wrap(bulk -> { + assertNull(bulk); - awaitResponseAndClose.countDown(); - }, - e -> fail(e.getMessage()) - ); + awaitResponseAndClose.countDown(); + }, e -> fail(e.getMessage())); exporter.openBulk(listener); @@ -404,17 +393,13 @@ public void testDynamicIndexFormatChange() throws Exception { final Settings settings = baseSettings().build(); enqueueGetClusterVersionResponse(Version.CURRENT); - enqueueSetupResponses(webServer, - true, - remoteClusterAllowsWatcher, currentLicenseAllowsWatcher, watcherAlreadyExists); + enqueueSetupResponses(webServer, true, remoteClusterAllowsWatcher, currentLicenseAllowsWatcher, watcherAlreadyExists); enqueueResponse(200, "{\"errors\": false, \"msg\": \"successful bulk request\"}"); MonitoringDoc doc = newRandomMonitoringDoc(); export(settings, Collections.singletonList(doc)); - assertMonitorResources(webServer, - true, - remoteClusterAllowsWatcher, currentLicenseAllowsWatcher, watcherAlreadyExists); + assertMonitorResources(webServer, true, remoteClusterAllowsWatcher, currentLicenseAllowsWatcher, watcherAlreadyExists); MockRequest recordedRequest = assertBulk(webServer); DateFormatter formatter = DateFormatter.forPattern("yyyy.MM.dd").withZone(ZoneOffset.UTC); @@ -429,13 +414,12 @@ public void testDynamicIndexFormatChange() throws Exception { String newTimeFormat = randomFrom("yy", "yyyy", "yyyy.MM", "yyyy-MM", "MM.yyyy", "MM"); final Settings newSettings = Settings.builder() - .put(settings) - .put("xpack.monitoring.exporters._http.index.name.time_format", newTimeFormat) - .build(); + .put(settings) + .put("xpack.monitoring.exporters._http.index.name.time_format", newTimeFormat) + .build(); enqueueGetClusterVersionResponse(Version.CURRENT); - enqueueSetupResponses(webServer, true, - true, true, true); + enqueueSetupResponses(webServer, true, true, true, true); enqueueResponse(200, "{\"errors\": false, \"msg\": \"successful bulk request\"}"); doc = newRandomMonitoringDoc(); @@ -443,11 +427,12 @@ public void testDynamicIndexFormatChange() throws Exception { DateFormatter newTimeFormatter = DateFormatter.forPattern(newTimeFormat).withZone(ZoneOffset.UTC); - String expectedMonitoringIndex = ".monitoring-es-" + TEMPLATE_VERSION + "-" - + newTimeFormatter.format(Instant.ofEpochMilli(doc.getTimestamp())); + String expectedMonitoringIndex = ".monitoring-es-" + + TEMPLATE_VERSION + + "-" + + newTimeFormatter.format(Instant.ofEpochMilli(doc.getTimestamp())); - assertMonitorResources(webServer, true, - true, true, true); + assertMonitorResources(webServer, true, true, true, true); recordedRequest = assertBulk(webServer); bytes = recordedRequest.getBody().getBytes(StandardCharsets.UTF_8); @@ -461,8 +446,11 @@ private void assertMonitorVersion(final MockWebServer webServer) throws Exceptio assertMonitorVersion(webServer, null, null); } - private void assertMonitorVersion(final MockWebServer webServer, @Nullable final Map customHeaders, - @Nullable final String basePath) throws Exception { + private void assertMonitorVersion( + final MockWebServer webServer, + @Nullable final Map customHeaders, + @Nullable final String basePath + ) throws Exception { final MockRequest request = webServer.takeRequest(); assertThat(request.getMethod(), equalTo("GET")); @@ -474,31 +462,51 @@ private void assertMonitorVersion(final MockWebServer webServer, @Nullable final assertHeaders(request, customHeaders); } - private void assertMonitorResources(final MockWebServer webServer, - final boolean templateAlreadyExists, - final boolean remoteClusterAllowsWatcher, final boolean currentLicenseAllowsWatcher, - final boolean watcherAlreadyExists) throws Exception { - assertMonitorResources(webServer, templateAlreadyExists, - remoteClusterAllowsWatcher, currentLicenseAllowsWatcher, watcherAlreadyExists, - null, null); - } - - private void assertMonitorResources(final MockWebServer webServer, - final boolean templateAlreadyExists, - final boolean remoteClusterAllowsWatcher, final boolean currentLicenseAllowsWatcher, - final boolean watcherAlreadyExists, - @Nullable final Map customHeaders, - @Nullable final String basePath) throws Exception { + private void assertMonitorResources( + final MockWebServer webServer, + final boolean templateAlreadyExists, + final boolean remoteClusterAllowsWatcher, + final boolean currentLicenseAllowsWatcher, + final boolean watcherAlreadyExists + ) throws Exception { + assertMonitorResources( + webServer, + templateAlreadyExists, + remoteClusterAllowsWatcher, + currentLicenseAllowsWatcher, + watcherAlreadyExists, + null, + null + ); + } + + private void assertMonitorResources( + final MockWebServer webServer, + final boolean templateAlreadyExists, + final boolean remoteClusterAllowsWatcher, + final boolean currentLicenseAllowsWatcher, + final boolean watcherAlreadyExists, + @Nullable final Map customHeaders, + @Nullable final String basePath + ) throws Exception { assertMonitorVersion(webServer, customHeaders, basePath); assertMonitorTemplates(webServer, templateAlreadyExists, customHeaders, basePath); - assertMonitorWatches(webServer, remoteClusterAllowsWatcher, currentLicenseAllowsWatcher, watcherAlreadyExists, - customHeaders, basePath); - } - - private void assertMonitorTemplates(final MockWebServer webServer, - final boolean alreadyExists, - @Nullable final Map customHeaders, - @Nullable final String basePath) throws Exception { + assertMonitorWatches( + webServer, + remoteClusterAllowsWatcher, + currentLicenseAllowsWatcher, + watcherAlreadyExists, + customHeaders, + basePath + ); + } + + private void assertMonitorTemplates( + final MockWebServer webServer, + final boolean alreadyExists, + @Nullable final Map customHeaders, + @Nullable final String basePath + ) throws Exception { final String resourcePrefix = "/_template/"; final String pathPrefix = basePathToAssertablePrefix(basePath); @@ -518,10 +526,14 @@ private void assertMonitorTemplates(final MockWebServer webServer, } } - private void assertMonitorVersionResource(final MockWebServer webServer, final boolean alreadyExists, - final String resourcePrefix, final List> resources, - @Nullable final Map customHeaders, - @Nullable final String basePath) throws Exception { + private void assertMonitorVersionResource( + final MockWebServer webServer, + final boolean alreadyExists, + final String resourcePrefix, + final List> resources, + @Nullable final Map customHeaders, + @Nullable final String basePath + ) throws Exception { final String pathPrefix = basePathToAssertablePrefix(basePath); for (Tuple resource : resources) { @@ -563,11 +575,14 @@ private void assertMonitorVersionQueryString(String query, final Map customHeaders, - @Nullable final String basePath) { + private void assertMonitorWatches( + final MockWebServer webServer, + final boolean remoteClusterAllowsWatcher, + final boolean currentLicenseAllowsWatcher, + final boolean alreadyExists, + @Nullable final Map customHeaders, + @Nullable final String basePath + ) { final String pathPrefix = basePathToAssertablePrefix(basePath); MockRequest request; @@ -601,7 +616,7 @@ private void assertMonitorWatches(final MockWebServer webServer, assertThat(request.getBody(), equalTo(watch.v2())); assertHeaders(request, customHeaders); } - // DELETE if we're not allowed to use it + // DELETE if we're not allowed to use it } else { assertThat(request.getMethod(), equalTo("DELETE")); assertThat(request.getUri().getPath(), equalTo(pathPrefix + "/_watcher/watch/" + uniqueWatchId)); @@ -620,9 +635,12 @@ private MockRequest assertBulk(final MockWebServer webServer, final int docs) th return assertBulk(webServer, docs, null, null); } - private MockRequest assertBulk(final MockWebServer webServer, final int docs, - @Nullable final Map customHeaders, @Nullable final String basePath) - throws Exception { + private MockRequest assertBulk( + final MockWebServer webServer, + final int docs, + @Nullable final Map customHeaders, + @Nullable final String basePath + ) throws Exception { final String pathPrefix = basePathToAssertablePrefix(basePath); final MockRequest request = webServer.takeRequest(); @@ -655,8 +673,7 @@ private void assertHeaders(final MockRequest request, final Map doc final CountDownLatch awaitResponseAndClose = new CountDownLatch(1); exporter.openBulk(ActionListener.wrap(exportBulk -> { - final HttpExportBulk bulk = (HttpExportBulk)exportBulk; + final HttpExportBulk bulk = (HttpExportBulk) exportBulk; assertThat("Bulk should never be null after the exporter is ready", bulk, notNullValue()); @@ -694,8 +711,13 @@ private MonitoringDoc newRandomMonitoringDoc() { long intervalMillis = randomNonNegativeLong(); MonitoringDoc.Node sourceNode = MonitoringTestUtils.randomMonitoringNode(random()); - return new IndexRecoveryMonitoringDoc(clusterUUID, timestamp, intervalMillis, sourceNode, - new RecoveryResponse(0, 0, 0, null, null)); + return new IndexRecoveryMonitoringDoc( + clusterUUID, + timestamp, + intervalMillis, + sourceNode, + new RecoveryResponse(0, 0, 0, null, null) + ); } private List newRandomMonitoringDocs(int nb) { @@ -710,7 +732,7 @@ private String basePathToAssertablePrefix(@Nullable String basePath) { if (basePath == null) { return ""; } - basePath = basePath.startsWith("/")? basePath : "/" + basePath; + basePath = basePath.startsWith("/") ? basePath : "/" + basePath; return basePath; } @@ -736,21 +758,28 @@ private void enqueueGetClusterVersionResponse(Version v) throws IOException { } private void enqueueGetClusterVersionResponse(MockWebServer mockWebServer, Version v) throws IOException { - mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody( - BytesReference.bytes(jsonBuilder().startObject().startObject("version") - .field("number", v.toString()).endObject().endObject()).utf8ToString())); - } - - private void enqueueSetupResponses(final MockWebServer webServer, - final boolean templatesAlreadyExists, - final boolean remoteClusterAllowsWatcher, final boolean currentLicenseAllowsWatcher, - final boolean watcherAlreadyExists) throws IOException { + mockWebServer.enqueue( + new MockResponse().setResponseCode(200) + .setBody( + BytesReference.bytes( + jsonBuilder().startObject().startObject("version").field("number", v.toString()).endObject().endObject() + ).utf8ToString() + ) + ); + } + + private void enqueueSetupResponses( + final MockWebServer webServer, + final boolean templatesAlreadyExists, + final boolean remoteClusterAllowsWatcher, + final boolean currentLicenseAllowsWatcher, + final boolean watcherAlreadyExists + ) throws IOException { enqueueTemplateResponses(webServer, templatesAlreadyExists); enqueueWatcherResponses(webServer, remoteClusterAllowsWatcher, currentLicenseAllowsWatcher, watcherAlreadyExists); } - private void enqueueTemplateResponses(final MockWebServer webServer, final boolean alreadyExists) - throws IOException { + private void enqueueTemplateResponses(final MockWebServer webServer, final boolean alreadyExists) throws IOException { if (alreadyExists) { enqueueTemplateResponsesExistsAlready(webServer); } else { @@ -758,18 +787,16 @@ private void enqueueTemplateResponses(final MockWebServer webServer, final boole } } - private void enqueueTemplateResponsesDoesNotExistYet(final MockWebServer webServer) - throws IOException { + private void enqueueTemplateResponsesDoesNotExistYet(final MockWebServer webServer) throws IOException { enqueueVersionedResourceResponsesDoesNotExistYet(Arrays.asList(MonitoringTemplateRegistry.TEMPLATE_NAMES), webServer); } - private void enqueueTemplateResponsesExistsAlready(final MockWebServer webServer) - throws IOException { + private void enqueueTemplateResponsesExistsAlready(final MockWebServer webServer) throws IOException { enqueueVersionedResourceResponsesExistsAlready(Arrays.asList(MonitoringTemplateRegistry.TEMPLATE_NAMES), webServer); } private void enqueueVersionedResourceResponsesDoesNotExistYet(final List names, final MockWebServer webServer) - throws IOException { + throws IOException { for (String resource : names) { if (randomBoolean()) { enqueueResponse(webServer, 404, "[" + resource + "] does not exist"); @@ -787,7 +814,7 @@ private void enqueueVersionedResourceResponsesDoesNotExistYet(final List } private void enqueueVersionedResourceResponsesExistsAlready(final List names, final MockWebServer webServer) - throws IOException { + throws IOException { for (String resource : names) { if (randomBoolean()) { final int newerVersion = randomFrom(Version.CURRENT.id, LAST_UPDATED_VERSION) + randomIntBetween(1, 1000000); @@ -801,9 +828,12 @@ private void enqueueVersionedResourceResponsesExistsAlready(final List n } } - private void enqueueWatcherResponses(final MockWebServer webServer, - final boolean remoteClusterAllowsWatcher, final boolean currentLicenseAllowsWatcher, - final boolean alreadyExists) throws IOException { + private void enqueueWatcherResponses( + final MockWebServer webServer, + final boolean remoteClusterAllowsWatcher, + final boolean currentLicenseAllowsWatcher, + final boolean alreadyExists + ) throws IOException { // if the remote cluster doesn't allow watcher, then we only check for it and we're done if (remoteClusterAllowsWatcher) { // X-Pack exists and Watcher can be used @@ -837,8 +867,7 @@ private void enqueueWatcherResponses(final MockWebServer webServer, } } - private void enqueueClusterAlertResponsesDoesNotExistYet(final MockWebServer webServer) - throws IOException { + private void enqueueClusterAlertResponsesDoesNotExistYet(final MockWebServer webServer) throws IOException { for (final String watchId : ClusterAlertsUtil.WATCH_IDS) { if (clusterAlertBlacklist.contains(watchId)) { enqueueDeleteClusterAlertResponse(webServer, watchId); @@ -903,7 +932,7 @@ private void enqueueResponse(MockWebServer mockWebServer, int responseCode, Stri private void assertBulkRequest(String requestBody, int numberOfActions) throws Exception { BulkRequest bulkRequest = Requests.bulkRequest() - .add(new BytesArray(requestBody.getBytes(StandardCharsets.UTF_8)), null, XContentType.JSON); + .add(new BytesArray(requestBody.getBytes(StandardCharsets.UTF_8)), null, XContentType.JSON); assertThat(bulkRequest.numberOfActions(), equalTo(numberOfActions)); for (DocWriteRequest actionRequest : bulkRequest.requests()) { assertThat(actionRequest, instanceOf(IndexRequest.class)); @@ -921,8 +950,10 @@ private MockWebServer createMockWebServer() throws IOException { } private String getExternalTemplateRepresentation(String internalRepresentation) throws IOException { - try (XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, internalRepresentation)) { + try ( + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, internalRepresentation) + ) { XContentBuilder builder = JsonXContent.contentBuilder(); IndexTemplateMetadata.Builder.removeType(IndexTemplateMetadata.Builder.fromXContent(parser, ""), builder); return BytesReference.bytes(builder).utf8ToString(); diff --git a/x-pack/plugin/monitoring/src/internalClusterTest/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporterSslIT.java b/x-pack/plugin/monitoring/src/internalClusterTest/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporterSslIT.java index e811e732b8442..6634f8293dddd 100644 --- a/x-pack/plugin/monitoring/src/internalClusterTest/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporterSslIT.java +++ b/x-pack/plugin/monitoring/src/internalClusterTest/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporterSslIT.java @@ -7,14 +7,15 @@ package org.elasticsearch.xpack.monitoring.exporter.http; import com.sun.net.httpserver.HttpsServer; + import org.elasticsearch.action.ActionFuture; import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest; import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsResponse; -import org.elasticsearch.jdk.JavaVersion; import org.elasticsearch.common.settings.MockSecureSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.ssl.SslVerificationMode; import org.elasticsearch.env.TestEnvironment; +import org.elasticsearch.jdk.JavaVersion; import org.elasticsearch.test.ESIntegTestCase; import org.elasticsearch.test.ESIntegTestCase.Scope; import org.elasticsearch.test.http.MockWebServer; @@ -27,7 +28,6 @@ import org.junit.AfterClass; import org.junit.Before; -import javax.net.ssl.SSLContext; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; @@ -36,12 +36,13 @@ import java.util.List; import java.util.Locale; +import javax.net.ssl.SSLContext; + import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.notNullValue; -@ESIntegTestCase.ClusterScope(scope = Scope.SUITE, - numDataNodes = 1, numClientNodes = 0, supportsDedicatedMasters = false) +@ESIntegTestCase.ClusterScope(scope = Scope.SUITE, numDataNodes = 1, numClientNodes = 0, supportsDedicatedMasters = false) public class HttpExporterSslIT extends MonitoringIntegTestCase { private final Settings globalSettings = Settings.builder().put("path.home", createTempDir()).build(); @@ -49,7 +50,6 @@ public class HttpExporterSslIT extends MonitoringIntegTestCase { private static MockWebServer webServer; private MockSecureSettings secureSettings; - @AfterClass public static void cleanUpStatics() { if (webServer != null) { @@ -196,7 +196,6 @@ private void clearPersistentSettings(String... names) { client().admin().cluster().updateSettings(updateSettings).actionGet(); } - /** * The {@link HttpsServer} in the JDK has issues with TLSv1.3 when running in a JDK prior to * 12.0.1 so we pin to TLSv1.2 when running on an earlier JDK @@ -205,9 +204,9 @@ private static List getProtocols() { if (JavaVersion.current().compareTo(JavaVersion.parse("12")) < 0) { return List.of("TLSv1.2"); } else { - JavaVersion full = - AccessController.doPrivileged( - (PrivilegedAction) () -> JavaVersion.parse(System.getProperty("java.version"))); + JavaVersion full = AccessController.doPrivileged( + (PrivilegedAction) () -> JavaVersion.parse(System.getProperty("java.version")) + ); if (full.compareTo(JavaVersion.parse("12.0.1")) < 0) { return List.of("TLSv1.2"); } diff --git a/x-pack/plugin/monitoring/src/internalClusterTest/java/org/elasticsearch/xpack/monitoring/integration/MonitoringIT.java b/x-pack/plugin/monitoring/src/internalClusterTest/java/org/elasticsearch/xpack/monitoring/integration/MonitoringIT.java index 3ee2089fa1645..6b0f44d626ef9 100644 --- a/x-pack/plugin/monitoring/src/internalClusterTest/java/org/elasticsearch/xpack/monitoring/integration/MonitoringIT.java +++ b/x-pack/plugin/monitoring/src/internalClusterTest/java/org/elasticsearch/xpack/monitoring/integration/MonitoringIT.java @@ -13,16 +13,13 @@ import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.analysis.common.CommonAnalysisPlugin; import org.elasticsearch.cluster.node.DiscoveryNode; -import org.elasticsearch.core.CheckedRunnable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.time.DateFormatter; -import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.ToXContentObject; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.core.CheckedRunnable; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.search.SearchHit; @@ -31,6 +28,9 @@ import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.test.ESSingleNodeTestCase; import org.elasticsearch.threadpool.ThreadPoolStats; +import org.elasticsearch.xcontent.ToXContentObject; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.xpack.core.action.XPackUsageRequestBuilder; import org.elasticsearch.xpack.core.action.XPackUsageResponse; @@ -60,10 +60,10 @@ import java.util.concurrent.atomic.AtomicReference; import java.util.stream.Collectors; -import static org.elasticsearch.xcontent.ToXContent.EMPTY_PARAMS; import static org.elasticsearch.common.xcontent.support.XContentMapValues.extractValue; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.elasticsearch.threadpool.ThreadPool.Names.WRITE; +import static org.elasticsearch.xcontent.ToXContent.EMPTY_PARAMS; import static org.elasticsearch.xpack.core.monitoring.exporter.MonitoringTemplateUtils.TEMPLATE_VERSION; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.emptyOrNullString; @@ -80,13 +80,13 @@ public class MonitoringIT extends ESSingleNodeTestCase { @Override protected Settings nodeSettings() { return Settings.builder() - .put(super.nodeSettings()) - .put(XPackSettings.MACHINE_LEARNING_ENABLED.getKey(), false) - .put("xpack.monitoring.collection.interval", MonitoringService.MIN_INTERVAL) - .put("xpack.monitoring.exporters._local.type", "local") - .put("xpack.monitoring.exporters._local.enabled", false) - .put("xpack.monitoring.exporters._local.cluster_alerts.management.enabled", false) - .build(); + .put(super.nodeSettings()) + .put(XPackSettings.MACHINE_LEARNING_ENABLED.getKey(), false) + .put("xpack.monitoring.collection.interval", MonitoringService.MIN_INTERVAL) + .put("xpack.monitoring.exporters._local.type", "local") + .put("xpack.monitoring.exporters._local.enabled", false) + .put("xpack.monitoring.exporters._local.cluster_alerts.management.enabled", false) + .build(); } @Override @@ -95,13 +95,13 @@ protected Collection> getPlugins() { } private String createBulkEntity() { - return "{\"index\":{\"_type\":\"monitoring_data_type\"}}\n" + - "{\"foo\":{\"bar\":0}}\n" + - "{\"index\":{\"_type\":\"monitoring_data_type\"}}\n" + - "{\"foo\":{\"bar\":1}}\n" + - "{\"index\":{\"_type\":\"monitoring_data_type\"}}\n" + - "{\"foo\":{\"bar\":2}}\n" + - "\n"; + return "{\"index\":{\"_type\":\"monitoring_data_type\"}}\n" + + "{\"foo\":{\"bar\":0}}\n" + + "{\"index\":{\"_type\":\"monitoring_data_type\"}}\n" + + "{\"foo\":{\"bar\":1}}\n" + + "{\"index\":{\"_type\":\"monitoring_data_type\"}}\n" + + "{\"foo\":{\"bar\":2}}\n" + + "\n"; } /** @@ -116,11 +116,13 @@ public void testMonitoringBulk() throws Exception { final MonitoredSystem system = randomSystem(); final TimeValue interval = TimeValue.timeValueSeconds(randomIntBetween(1, 20)); - final MonitoringBulkResponse bulkResponse = - new MonitoringBulkRequestBuilder(client()) - .add(system, new BytesArray(createBulkEntity().getBytes("UTF-8")), XContentType.JSON, - System.currentTimeMillis(), interval.millis()) - .get(); + final MonitoringBulkResponse bulkResponse = new MonitoringBulkRequestBuilder(client()).add( + system, + new BytesArray(createBulkEntity().getBytes("UTF-8")), + XContentType.JSON, + System.currentTimeMillis(), + interval.millis() + ).get(); assertThat(bulkResponse.status(), is(RestStatus.OK)); assertThat(bulkResponse.getError(), nullValue()); @@ -133,17 +135,15 @@ public void testMonitoringBulk() throws Exception { ensureGreen(monitoringIndex); assertThat(client().admin().indices().prepareRefresh(monitoringIndex).get().getStatus(), is(RestStatus.OK)); - final SearchResponse response = - client().prepareSearch(".monitoring-" + system.getSystem() + "-" + TEMPLATE_VERSION + "-*") - .get(); + final SearchResponse response = client().prepareSearch(".monitoring-" + system.getSystem() + "-" + TEMPLATE_VERSION + "-*") + .get(); // exactly 3 results are expected assertThat("No monitoring documents yet", response.getHits().getTotalHits().value, equalTo(3L)); - final List> sources = - Arrays.stream(response.getHits().getHits()) - .map(SearchHit::getSourceAsMap) - .collect(Collectors.toList()); + final List> sources = Arrays.stream(response.getHits().getHits()) + .map(SearchHit::getSourceAsMap) + .collect(Collectors.toList()); // find distinct _source.timestamp fields assertThat(sources.stream().map(source -> source.get("timestamp")).distinct().count(), is(1L)); @@ -155,18 +155,16 @@ public void testMonitoringBulk() throws Exception { final SearchHits hits = response.getHits(); assertThat(response.getHits().getTotalHits().value, equalTo(3L)); - assertThat("Monitoring documents must have the same timestamp", - Arrays.stream(hits.getHits()) - .map(hit -> extractValue("timestamp", hit.getSourceAsMap())) - .distinct() - .count(), - equalTo(1L)); - assertThat("Monitoring documents must have the same source_node timestamp", - Arrays.stream(hits.getHits()) - .map(hit -> extractValue("source_node.timestamp", hit.getSourceAsMap())) - .distinct() - .count(), - equalTo(1L)); + assertThat( + "Monitoring documents must have the same timestamp", + Arrays.stream(hits.getHits()).map(hit -> extractValue("timestamp", hit.getSourceAsMap())).distinct().count(), + equalTo(1L) + ); + assertThat( + "Monitoring documents must have the same source_node timestamp", + Arrays.stream(hits.getHits()).map(hit -> extractValue("source_node.timestamp", hit.getSourceAsMap())).distinct().count(), + equalTo(1L) + ); for (final SearchHit hit : hits.getHits()) { assertMonitoringDoc(toMap(hit), system, interval); @@ -184,32 +182,34 @@ public void testMonitoringService() throws Exception { final boolean createAPMIndex = randomBoolean(); final String indexName = createAPMIndex ? "apm-2017.11.06" : "books"; - assertThat(client().prepareIndex(indexName).setId("0") - .setRefreshPolicy("true") - .setSource("{\"field\":\"value\"}", XContentType.JSON) - .get() - .status(), - is(RestStatus.CREATED)); - - final Settings settings = Settings.builder() - .put("cluster.metadata.display_name", "my cluster") - .build(); + assertThat( + client().prepareIndex(indexName) + .setId("0") + .setRefreshPolicy("true") + .setSource("{\"field\":\"value\"}", XContentType.JSON) + .get() + .status(), + is(RestStatus.CREATED) + ); + + final Settings settings = Settings.builder().put("cluster.metadata.display_name", "my cluster").build(); assertAcked(client().admin().cluster().prepareUpdateSettings().setTransientSettings(settings)); whenExportersAreReady(() -> { final AtomicReference searchResponse = new AtomicReference<>(); assertBusy(() -> { - final SearchResponse response = - client().prepareSearch(".monitoring-es-*") - .setCollapse(new CollapseBuilder("type")) - .addSort("timestamp", SortOrder.DESC) - .get(); + final SearchResponse response = client().prepareSearch(".monitoring-es-*") + .setCollapse(new CollapseBuilder("type")) + .addSort("timestamp", SortOrder.DESC) + .get(); assertThat(response.status(), is(RestStatus.OK)); - assertThat("Expecting a minimum number of 6 docs, one per collector", - response.getHits().getHits().length, - greaterThanOrEqualTo(6)); + assertThat( + "Expecting a minimum number of 6 docs, one per collector", + response.getHits().getHits().length, + greaterThanOrEqualTo(6) + ); searchResponse.set(response); }); @@ -227,10 +227,8 @@ public void testMonitoringService() throws Exception { * all monitoring documents must have */ @SuppressWarnings("unchecked") - private void assertMonitoringDoc(final Map document, - final MonitoredSystem expectedSystem, - final TimeValue interval) { - assertEquals(document.toString(),3, document.size()); + private void assertMonitoringDoc(final Map document, final MonitoredSystem expectedSystem, final TimeValue interval) { + assertEquals(document.toString(), 3, document.size()); final String index = (String) document.get("_index"); assertThat(index, containsString(".monitoring-" + expectedSystem.getSystem() + "-" + TEMPLATE_VERSION + "-")); @@ -271,7 +269,7 @@ private void assertMonitoringDocSourceNode(final Map sourceNode) assertThat(sourceNode.get("uuid"), equalTo(node.getId())); assertThat(sourceNode.get("host"), equalTo(node.getHostName())); - assertThat(sourceNode.get("transport_address"),equalTo(node.getAddress().toString())); + assertThat(sourceNode.get("transport_address"), equalTo(node.getAddress().toString())); assertThat(sourceNode.get("ip"), equalTo(node.getAddress().getAddress())); assertThat(sourceNode.get("name"), equalTo(node.getName())); assertThat((String) sourceNode.get("timestamp"), is(not(emptyOrNullString()))); @@ -291,7 +289,7 @@ private void whenExportersAreReady(final CheckedRunnable runnable) th StringBuilder b = new StringBuilder(); b.append("\n==== jstack at monitoring enablement failure time ====\n"); for (ThreadInfo ti : ManagementFactory.getThreadMXBean().dumpAllThreads(true, true)) { - append(b, ti); + append(b, ti); } b.append("^^==============================================\n"); logger.info(b.toString()); @@ -311,12 +309,11 @@ private static void append(StringBuilder b, ThreadInfo ti) { final State threadState = ti.getThreadState(); b.append(" ").append(threadState); if (ti.getLockName() != null) { - b.append(" on ").append(ti.getLockName()); + b.append(" on ").append(ti.getLockName()); } if (ti.getLockOwnerName() != null) { - b.append(" owned by \"").append(ti.getLockOwnerName()) - .append("\" ID=").append(ti.getLockOwnerId()); + b.append(" owned by \"").append(ti.getLockOwnerName()).append("\" ID=").append(ti.getLockOwnerId()); } b.append(ti.isSuspended() ? " (suspended)" : ""); @@ -325,32 +322,29 @@ private static void append(StringBuilder b, ThreadInfo ti) { final StackTraceElement[] stack = ti.getStackTrace(); final LockInfo lockInfo = ti.getLockInfo(); - final MonitorInfo [] monitorInfos = ti.getLockedMonitors(); + final MonitorInfo[] monitorInfos = ti.getLockedMonitors(); for (int i = 0; i < stack.length; i++) { - b.append("\tat ").append(stack[i]).append("\n"); - if (i == 0 && lockInfo != null) { - b.append("\t- ") - .append(threadState) - .append(lockInfo) - .append("\n"); - } - - for (MonitorInfo mi : monitorInfos) { - if (mi.getLockedStackDepth() == i) { - b.append("\t- locked ").append(mi).append("\n"); + b.append("\tat ").append(stack[i]).append("\n"); + if (i == 0 && lockInfo != null) { + b.append("\t- ").append(threadState).append(lockInfo).append("\n"); + } + + for (MonitorInfo mi : monitorInfos) { + if (mi.getLockedStackDepth() == i) { + b.append("\t- locked ").append(mi).append("\n"); + } } - } } - LockInfo [] lockInfos = ti.getLockedSynchronizers(); + LockInfo[] lockInfos = ti.getLockedSynchronizers(); if (lockInfos.length > 0) { - b.append("\tLocked synchronizers:\n"); - for (LockInfo li : ti.getLockedSynchronizers()) { - b.append("\t- ").append(li).append("\n"); - } + b.append("\tLocked synchronizers:\n"); + for (LockInfo li : ti.getLockedSynchronizers()) { + b.append("\t- ").append(li).append("\n"); + } } b.append("\n"); - } + } /** * Enable the monitoring service and the Local exporter, waiting for some monitoring documents @@ -363,10 +357,10 @@ public void enableMonitoring() throws Exception { assertThat("Must be no enabled exporters before enabling monitoring", getMonitoringUsageExportersDefined(), is(false)); final Settings settings = Settings.builder() - .put("xpack.monitoring.collection.enabled", true) - .put("xpack.monitoring.exporters._local.type", "local") - .put("xpack.monitoring.exporters._local.enabled", true) - .build(); + .put("xpack.monitoring.collection.enabled", true) + .put("xpack.monitoring.exporters._local.type", "local") + .put("xpack.monitoring.exporters._local.enabled", true) + .build(); assertAcked(client().admin().cluster().prepareUpdateSettings().setTransientSettings(settings)); @@ -377,11 +371,11 @@ public void enableMonitoring() throws Exception { ensureGreen(".monitoring-es-*"); assertThat(client().admin().indices().prepareRefresh(".monitoring-es-*").get().getStatus(), is(RestStatus.OK)); - assertThat("No monitoring documents yet", - client().prepareSearch(".monitoring-es-" + TEMPLATE_VERSION + "-*") - .setSize(0) - .get().getHits().getTotalHits().value, - greaterThan(0L)); + assertThat( + "No monitoring documents yet", + client().prepareSearch(".monitoring-es-" + TEMPLATE_VERSION + "-*").setSize(0).get().getHits().getTotalHits().value, + greaterThan(0L) + ); }, 30L, TimeUnit.SECONDS); } @@ -390,11 +384,11 @@ public void enableMonitoring() throws Exception { */ public void disableMonitoring() throws Exception { final Settings settings = Settings.builder() - .putNull("xpack.monitoring.collection.enabled") - .putNull("xpack.monitoring.exporters._local.type") - .putNull("xpack.monitoring.exporters._local.enabled") - .putNull("cluster.metadata.display_name") - .build(); + .putNull("xpack.monitoring.collection.enabled") + .putNull("xpack.monitoring.exporters._local.type") + .putNull("xpack.monitoring.exporters._local.enabled") + .putNull("cluster.metadata.display_name") + .build(); assertAcked(client().admin().cluster().prepareUpdateSettings().setTransientSettings(settings)); @@ -407,7 +401,7 @@ public void disableMonitoring() throws Exception { for (final NodeStats nodeStats : response.getNodes()) { boolean foundBulkThreads = false; - for(final ThreadPoolStats.Stats threadPoolStats : nodeStats.getThreadPool()) { + for (final ThreadPoolStats.Stats threadPoolStats : nodeStats.getThreadPool()) { if (WRITE.equals(threadPoolStats.getName())) { foundBulkThreads = true; assertThat("Still some active _bulk threads!", threadPoolStats.getActive(), equalTo(0)); @@ -425,12 +419,11 @@ public void disableMonitoring() throws Exception { private boolean getMonitoringUsageExportersDefined() throws Exception { final XPackUsageResponse usageResponse = new XPackUsageRequestBuilder(client()).execute().get(); - final Optional monitoringUsage = - usageResponse.getUsages() - .stream() - .filter(usage -> usage instanceof MonitoringFeatureSetUsage) - .map(usage -> (MonitoringFeatureSetUsage)usage) - .findFirst(); + final Optional monitoringUsage = usageResponse.getUsages() + .stream() + .filter(usage -> usage instanceof MonitoringFeatureSetUsage) + .map(usage -> (MonitoringFeatureSetUsage) usage) + .findFirst(); assertThat("Monitoring feature set does not exist", monitoringUsage.isPresent(), is(true)); diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/Monitoring.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/Monitoring.java index fb9f01713457d..6abc2fc436ab5 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/Monitoring.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/Monitoring.java @@ -77,11 +77,19 @@ public class Monitoring extends Plugin implements ActionPlugin, ReloadablePlugin { - public static final Setting MIGRATION_DECOMMISSION_ALERTS = boolSetting("xpack.monitoring.migration.decommission_alerts", - false, Setting.Property.Dynamic, Setting.Property.NodeScope, Setting.Property.Deprecated); - - public static final LicensedFeature.Momentary MONITORING_CLUSTER_ALERTS_FEATURE = - LicensedFeature.momentary("monitoring", "cluster-alerts", License.OperationMode.STANDARD); + public static final Setting MIGRATION_DECOMMISSION_ALERTS = boolSetting( + "xpack.monitoring.migration.decommission_alerts", + false, + Setting.Property.Dynamic, + Setting.Property.NodeScope, + Setting.Property.Deprecated + ); + + public static final LicensedFeature.Momentary MONITORING_CLUSTER_ALERTS_FEATURE = LicensedFeature.momentary( + "monitoring", + "cluster-alerts", + License.OperationMode.STANDARD + ); protected final Settings settings; @@ -92,33 +100,57 @@ public Monitoring(Settings settings) { } // overridable by tests - protected SSLService getSslService() { return XPackPlugin.getSharedSslService(); } - protected XPackLicenseState getLicenseState() { return XPackPlugin.getSharedLicenseState(); } - protected LicenseService getLicenseService() { return XPackPlugin.getSharedLicenseService(); } + protected SSLService getSslService() { + return XPackPlugin.getSharedSslService(); + } + + protected XPackLicenseState getLicenseState() { + return XPackPlugin.getSharedLicenseState(); + } + + protected LicenseService getLicenseService() { + return XPackPlugin.getSharedLicenseService(); + } @Override - public Collection createComponents(Client client, ClusterService clusterService, ThreadPool threadPool, - ResourceWatcherService resourceWatcherService, ScriptService scriptService, - NamedXContentRegistry xContentRegistry, Environment environment, - NodeEnvironment nodeEnvironment, NamedWriteableRegistry namedWriteableRegistry, - IndexNameExpressionResolver expressionResolver, - Supplier repositoriesServiceSupplier) { + public Collection createComponents( + Client client, + ClusterService clusterService, + ThreadPool threadPool, + ResourceWatcherService resourceWatcherService, + ScriptService scriptService, + NamedXContentRegistry xContentRegistry, + Environment environment, + NodeEnvironment nodeEnvironment, + NamedWriteableRegistry namedWriteableRegistry, + IndexNameExpressionResolver expressionResolver, + Supplier repositoriesServiceSupplier + ) { final ClusterSettings clusterSettings = clusterService.getClusterSettings(); final CleanerService cleanerService = new CleanerService(settings, clusterSettings, threadPool, getLicenseState()); final SSLService dynamicSSLService = getSslService().createDynamicSSLService(); final MonitoringMigrationCoordinator migrationCoordinator = new MonitoringMigrationCoordinator(); Map exporterFactories = new HashMap<>(); - exporterFactories.put(HttpExporter.TYPE, config -> new HttpExporter(config, dynamicSSLService, threadPool.getThreadContext(), - migrationCoordinator)); + exporterFactories.put( + HttpExporter.TYPE, + config -> new HttpExporter(config, dynamicSSLService, threadPool.getThreadContext(), migrationCoordinator) + ); exporterFactories.put(LocalExporter.TYPE, config -> new LocalExporter(config, client, migrationCoordinator, cleanerService)); - exporters = new Exporters(settings, exporterFactories, clusterService, getLicenseState(), threadPool.getThreadContext(), - dynamicSSLService); + exporters = new Exporters( + settings, + exporterFactories, + clusterService, + getLicenseState(), + threadPool.getThreadContext(), + dynamicSSLService + ); Set collectors = new HashSet<>(); collectors.add(new IndexStatsCollector(clusterService, getLicenseState(), client)); collectors.add( - new ClusterStatsCollector(settings, clusterService, getLicenseState(), client, getLicenseService(), expressionResolver)); + new ClusterStatsCollector(settings, clusterService, getLicenseState(), client, getLicenseService(), expressionResolver) + ); collectors.add(new ShardsCollector(clusterService, getLicenseState())); collectors.add(new NodeStatsCollector(clusterService, getLicenseState(), client)); collectors.add(new IndexRecoveryCollector(clusterService, getLicenseState(), client)); @@ -130,8 +162,13 @@ public Collection createComponents(Client client, ClusterService cluster var usageServices = new MonitoringUsageServices(monitoringService, exporters); - MonitoringTemplateRegistry templateRegistry = new MonitoringTemplateRegistry(settings, clusterService, threadPool, client, - xContentRegistry); + MonitoringTemplateRegistry templateRegistry = new MonitoringTemplateRegistry( + settings, + clusterService, + threadPool, + client, + xContentRegistry + ); templateRegistry.initialize(); return Arrays.asList(monitoringService, exporters, migrationCoordinator, cleanerService, usageServices, templateRegistry); @@ -145,13 +182,20 @@ public Collection createComponents(Client client, ClusterService cluster new ActionHandler<>(MonitoringBulkAction.INSTANCE, TransportMonitoringBulkAction.class), new ActionHandler<>(MonitoringMigrateAlertsAction.INSTANCE, TransportMonitoringMigrateAlertsAction.class), usageAction, - infoAction); + infoAction + ); } @Override - public List getRestHandlers(Settings settings, RestController restController, ClusterSettings clusterSettings, - IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter, IndexNameExpressionResolver indexNameExpressionResolver, - Supplier nodesInCluster) { + public List getRestHandlers( + Settings settings, + RestController restController, + ClusterSettings clusterSettings, + IndexScopedSettings indexScopedSettings, + SettingsFilter settingsFilter, + IndexNameExpressionResolver indexNameExpressionResolver, + Supplier nodesInCluster + ) { return List.of(new RestMonitoringBulkAction(), new RestMonitoringMigrateAlertsAction()); } diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/MonitoringService.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/MonitoringService.java index 7c854fa5fc58c..28fd19cb70ff0 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/MonitoringService.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/MonitoringService.java @@ -17,8 +17,8 @@ import org.elasticsearch.common.component.Lifecycle; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.AbstractRunnable; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc; import org.elasticsearch.xpack.monitoring.collector.Collector; @@ -43,7 +43,6 @@ public class MonitoringService extends AbstractLifecycleComponent { private static final Logger logger = LogManager.getLogger(MonitoringService.class); - /** * Minimum value for sampling interval (1 second) */ @@ -57,24 +56,37 @@ public class MonitoringService extends AbstractLifecycleComponent { * Kibana, Logstash, Beats, and APM Server can all continue to report their stats through this cluster until they * are transitioned to being monitored by Metricbeat as well. */ - public static final Setting ELASTICSEARCH_COLLECTION_ENABLED = - Setting.boolSetting("xpack.monitoring.elasticsearch.collection.enabled", true, - Setting.Property.Dynamic, Setting.Property.NodeScope, Setting.Property.Deprecated); + public static final Setting ELASTICSEARCH_COLLECTION_ENABLED = Setting.boolSetting( + "xpack.monitoring.elasticsearch.collection.enabled", + true, + Setting.Property.Dynamic, + Setting.Property.NodeScope, + Setting.Property.Deprecated + ); /** * Dynamically controls enabling or disabling the collection of Monitoring data from Elasticsearch as well as other products * in the stack. */ - public static final Setting ENABLED = - Setting.boolSetting("xpack.monitoring.collection.enabled", false, - Setting.Property.Dynamic, Setting.Property.NodeScope, Setting.Property.Deprecated); + public static final Setting ENABLED = Setting.boolSetting( + "xpack.monitoring.collection.enabled", + false, + Setting.Property.Dynamic, + Setting.Property.NodeScope, + Setting.Property.Deprecated + ); /** * Sampling interval between two collections (default to 10s) */ - public static final Setting INTERVAL = - Setting.timeSetting("xpack.monitoring.collection.interval", TimeValue.timeValueSeconds(10), MIN_INTERVAL, - Setting.Property.Dynamic, Setting.Property.NodeScope, Setting.Property.Deprecated); + public static final Setting INTERVAL = Setting.timeSetting( + "xpack.monitoring.collection.interval", + TimeValue.timeValueSeconds(10), + MIN_INTERVAL, + Setting.Property.Dynamic, + Setting.Property.NodeScope, + Setting.Property.Deprecated + ); /** State of the monitoring service, either started or stopped **/ private final AtomicBoolean started = new AtomicBoolean(false); @@ -92,8 +104,13 @@ public class MonitoringService extends AbstractLifecycleComponent { private volatile TimeValue interval; private volatile ThreadPool.Cancellable scheduler; - MonitoringService(Settings settings, ClusterService clusterService, ThreadPool threadPool, - Set collectors, Exporters exporters) { + MonitoringService( + Settings settings, + ClusterService clusterService, + ThreadPool threadPool, + Set collectors, + Exporters exporters + ) { this.clusterService = Objects.requireNonNull(clusterService); this.threadPool = Objects.requireNonNull(threadPool); this.collectors = Objects.requireNonNull(collectors); @@ -254,8 +271,13 @@ protected void doRun() throws Exception { results.addAll(result); } } catch (Exception e) { - logger.warn((Supplier) () -> - new ParameterizedMessage("monitoring collector [{}] failed to collect data", collector.name()), e); + logger.warn( + (Supplier) () -> new ParameterizedMessage( + "monitoring collector [{}] failed to collect data", + collector.name() + ), + e + ); } } if (shouldScheduleExecution()) { diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/MonitoringTemplateRegistry.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/MonitoringTemplateRegistry.java index 032213e3eafc6..f5bf4ae49f495 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/MonitoringTemplateRegistry.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/MonitoringTemplateRegistry.java @@ -14,8 +14,8 @@ import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.ClientHelper; import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; import org.elasticsearch.xpack.core.template.IndexTemplateConfig; @@ -121,14 +121,12 @@ public class MonitoringTemplateRegistry extends IndexTemplateRegistry { ADDITIONAL_TEMPLATE_VARIABLES ); - public static final String[] TEMPLATE_NAMES = new String[]{ + public static final String[] TEMPLATE_NAMES = new String[] { ALERTS_INDEX_TEMPLATE_NAME, BEATS_INDEX_TEMPLATE_NAME, ES_INDEX_TEMPLATE_NAME, KIBANA_INDEX_TEMPLATE_NAME, - LOGSTASH_INDEX_TEMPLATE_NAME - }; - + LOGSTASH_INDEX_TEMPLATE_NAME }; private static final Map MONITORED_SYSTEM_CONFIG_LOOKUP = new HashMap<>(); static { @@ -143,8 +141,13 @@ public static IndexTemplateConfig getTemplateConfigForMonitoredSystem(MonitoredS .orElseThrow(() -> new IllegalArgumentException("Invalid system [" + system + "]")); } - public MonitoringTemplateRegistry(Settings nodeSettings, ClusterService clusterService, ThreadPool threadPool, Client client, - NamedXContentRegistry xContentRegistry) { + public MonitoringTemplateRegistry( + Settings nodeSettings, + ClusterService clusterService, + ThreadPool threadPool, + Client client, + NamedXContentRegistry xContentRegistry + ) { super(nodeSettings, clusterService, threadPool, client, xContentRegistry); this.clusterService = clusterService; this.monitoringTemplatesEnabled = MONITORING_TEMPLATES_ENABLED.get(nodeSettings); diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/MonitoringUsageTransportAction.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/MonitoringUsageTransportAction.java index a27e419be4d57..8553e63e380fe 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/MonitoringUsageTransportAction.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/MonitoringUsageTransportAction.java @@ -31,18 +31,33 @@ public class MonitoringUsageTransportAction extends XPackUsageFeatureTransportAc private final Exporters exporters; @Inject - public MonitoringUsageTransportAction(TransportService transportService, ClusterService clusterService, ThreadPool threadPool, - ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver, - MonitoringUsageServices monitoringServices) { - super(XPackUsageFeatureAction.MONITORING.name(), transportService, clusterService, threadPool, - actionFilters, indexNameExpressionResolver); + public MonitoringUsageTransportAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver, + MonitoringUsageServices monitoringServices + ) { + super( + XPackUsageFeatureAction.MONITORING.name(), + transportService, + clusterService, + threadPool, + actionFilters, + indexNameExpressionResolver + ); this.monitoringService = monitoringServices.monitoringService; this.exporters = monitoringServices.exporters; } @Override - protected void masterOperation(Task task, XPackUsageRequest request, ClusterState state, - ActionListener listener) { + protected void masterOperation( + Task task, + XPackUsageRequest request, + ClusterState state, + ActionListener listener + ) { final boolean collectionEnabled = monitoringService != null && monitoringService.isMonitoringActive(); var usage = new MonitoringFeatureSetUsage(collectionEnabled, exportersUsage(exporters)); listener.onResponse(new XPackUsageFeatureResponse(usage)); diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/action/TransportMonitoringBulkAction.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/action/TransportMonitoringBulkAction.java index e6a5ee8a86799..97c2dcae2f93a 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/action/TransportMonitoringBulkAction.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/action/TransportMonitoringBulkAction.java @@ -15,10 +15,10 @@ import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.inject.Inject; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.tasks.Task; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; import org.elasticsearch.xpack.core.monitoring.action.MonitoringBulkAction; import org.elasticsearch.xpack.core.monitoring.action.MonitoringBulkDoc; @@ -41,9 +41,14 @@ public class TransportMonitoringBulkAction extends HandledTransportAction listener, Exporters exportService, - String defaultClusterUUID, long defaultTimestamp, MonitoringDoc.Node defaultNode) { + AsyncAction( + ThreadPool threadPool, + MonitoringBulkRequest request, + ActionListener listener, + Exporters exportService, + String defaultClusterUUID, + long defaultTimestamp, + MonitoringDoc.Node defaultNode + ) { this.threadPool = threadPool; this.request = request; this.listener = listener; @@ -106,9 +120,9 @@ void start() { */ Collection createMonitoringDocs(Collection bulkDocs) { return bulkDocs.stream() - .filter(bulkDoc -> bulkDoc.getSystem() != MonitoredSystem.UNKNOWN) - .map(this::createMonitoringDoc) - .collect(Collectors.toList()); + .filter(bulkDoc -> bulkDoc.getSystem() != MonitoredSystem.UNKNOWN) + .map(this::createMonitoringDoc) + .collect(Collectors.toList()); } /** @@ -132,25 +146,31 @@ MonitoringDoc createMonitoringDoc(final MonitoringBulkDoc bulkDoc) { timestamp = defaultTimestamp; } - return new BytesReferenceMonitoringDoc(defaultClusterUUID, timestamp, intervalMillis, - defaultNode, system, type, id, xContentType, source); + return new BytesReferenceMonitoringDoc( + defaultClusterUUID, + timestamp, + intervalMillis, + defaultNode, + system, + type, + id, + xContentType, + source + ); } /** * Exports the documents */ - void executeExport(final Collection docs, final long startTimeNanos, - final ActionListener delegate) { + void executeExport( + final Collection docs, + final long startTimeNanos, + final ActionListener delegate + ) { threadPool.executor(ThreadPool.Names.GENERIC).execute(new ActionRunnable(delegate) { @Override protected void doRun() { - exportService.export( - docs, - ActionListener.wrap( - r -> listener.onResponse(response(startTimeNanos)), - this::onFailure - ) - ); + exportService.export(docs, ActionListener.wrap(r -> listener.onResponse(response(startTimeNanos)), this::onFailure)); } @Override diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/action/TransportMonitoringMigrateAlertsAction.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/action/TransportMonitoringMigrateAlertsAction.java index 42f12cce09e49..439531650cdc0 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/action/TransportMonitoringMigrateAlertsAction.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/action/TransportMonitoringMigrateAlertsAction.java @@ -45,7 +45,8 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; -public class TransportMonitoringMigrateAlertsAction extends TransportMasterNodeAction { private static final Logger logger = LogManager.getLogger(TransportMonitoringMigrateAlertsAction.class); @@ -55,20 +56,39 @@ public class TransportMonitoringMigrateAlertsAction extends TransportMasterNodeA private final Exporters exporters; @Inject - public TransportMonitoringMigrateAlertsAction(Client client, Exporters exporters, MonitoringMigrationCoordinator migrationCoordinator, - TransportService transportService, ClusterService clusterService, ThreadPool threadPool, - ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver) { - super(MonitoringMigrateAlertsAction.NAME, transportService, clusterService, threadPool, actionFilters, - MonitoringMigrateAlertsRequest::new, indexNameExpressionResolver, MonitoringMigrateAlertsResponse::new, - ThreadPool.Names.MANAGEMENT); + public TransportMonitoringMigrateAlertsAction( + Client client, + Exporters exporters, + MonitoringMigrationCoordinator migrationCoordinator, + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver + ) { + super( + MonitoringMigrateAlertsAction.NAME, + transportService, + clusterService, + threadPool, + actionFilters, + MonitoringMigrateAlertsRequest::new, + indexNameExpressionResolver, + MonitoringMigrateAlertsResponse::new, + ThreadPool.Names.MANAGEMENT + ); this.client = client; this.migrationCoordinator = migrationCoordinator; this.exporters = exporters; } @Override - protected void masterOperation(Task task, MonitoringMigrateAlertsRequest request, ClusterState state, - ActionListener listener) throws Exception { + protected void masterOperation( + Task task, + MonitoringMigrateAlertsRequest request, + ClusterState state, + ActionListener listener + ) throws Exception { // First, set the migration coordinator as currently running if (migrationCoordinator.tryBlockInstallationTasks() == false) { throw new EsRejectedExecutionException("Could not migrate cluster alerts. Migration already in progress."); @@ -77,7 +97,10 @@ protected void masterOperation(Task task, MonitoringMigrateAlertsRequest request // Wrap the listener to unblock resource installation before completing listener = ActionListener.runBefore(listener, migrationCoordinator::unblockInstallationTasks); Settings.Builder decommissionAlertSetting = Settings.builder().put(Monitoring.MIGRATION_DECOMMISSION_ALERTS.getKey(), true); - client.admin().cluster().prepareUpdateSettings().setPersistentSettings(decommissionAlertSetting) + client.admin() + .cluster() + .prepareUpdateSettings() + .setPersistentSettings(decommissionAlertSetting) .execute(completeOnManagementThread(listener)); } catch (Exception e) { // unblock resource installation if something fails here @@ -87,11 +110,12 @@ protected void masterOperation(Task task, MonitoringMigrateAlertsRequest request } private ActionListener completeOnManagementThread( - ActionListener delegate) { + ActionListener delegate + ) { // Send failures to the final listener directly, and on success, fork to management thread and execute best effort alert removal return ActionListener.wrap( - (response) -> threadPool.executor(ThreadPool.Names.MANAGEMENT).execute( - ActionRunnable.wrap(delegate, (listener) -> afterSettingUpdate(listener, response))), + (response) -> threadPool.executor(ThreadPool.Names.MANAGEMENT) + .execute(ActionRunnable.wrap(delegate, (listener) -> afterSettingUpdate(listener, response))), delegate::onFailure ); } @@ -101,8 +125,10 @@ private ActionListener completeOnManagementThread * to explicitly remove their installed alerts if possible. This makes sure that alerts are removed in a timely fashion instead of * waiting for metrics to be bulked into the monitoring cluster. */ - private void afterSettingUpdate(ActionListener listener, - ClusterUpdateSettingsResponse clusterUpdateSettingsResponse) { + private void afterSettingUpdate( + ActionListener listener, + ClusterUpdateSettingsResponse clusterUpdateSettingsResponse + ) { logger.info("THREAD NAME: {}" + Thread.currentThread().getName()); // Ensure positive result @@ -117,18 +143,28 @@ private void afterSettingUpdate(ActionListener List refreshTasks = new ArrayList<>(); AtomicInteger remaining = new AtomicInteger(enabledExporters.size() + disabledExporterConfigs.size()); List results = Collections.synchronizedList(new ArrayList<>(remaining.get())); - logger.debug("Exporters in need of refreshing [{}]; enabled [{}], disabled [{}]", remaining.get(), enabledExporters.size(), - disabledExporterConfigs.size()); + logger.debug( + "Exporters in need of refreshing [{}]; enabled [{}], disabled [{}]", + remaining.get(), + enabledExporters.size(), + disabledExporterConfigs.size() + ); for (Exporter enabledExporter : enabledExporters) { - refreshTasks.add(ActionRunnable.wrap( - resultCollector(enabledExporter.config(), listener, remaining, results), - (resultCollector) -> deleteAlertsFromOpenExporter(enabledExporter, resultCollector))); + refreshTasks.add( + ActionRunnable.wrap( + resultCollector(enabledExporter.config(), listener, remaining, results), + (resultCollector) -> deleteAlertsFromOpenExporter(enabledExporter, resultCollector) + ) + ); } for (Exporter.Config disabledExporter : disabledExporterConfigs) { - refreshTasks.add(ActionRunnable.wrap( - resultCollector(disabledExporter, listener, remaining, results), - (resultCollector) -> deleteAlertsFromDisabledExporter(disabledExporter, resultCollector))); + refreshTasks.add( + ActionRunnable.wrap( + resultCollector(disabledExporter, listener, remaining, results), + (resultCollector) -> deleteAlertsFromDisabledExporter(disabledExporter, resultCollector) + ) + ); } for (Runnable refreshTask : refreshTasks) { threadPool.executor(ThreadPool.Names.MANAGEMENT).execute(refreshTask); @@ -142,10 +178,12 @@ private void afterSettingUpdate(ActionListener * @param remaining The counter used to determine if any other operations are in flight * @param results A thread-safe collection to hold results */ - private ActionListener resultCollector(final Exporter.Config exporterConfig, - final ActionListener listener, - final AtomicInteger remaining, - final List results) { + private ActionListener resultCollector( + final Exporter.Config exporterConfig, + final ActionListener listener, + final AtomicInteger remaining, + final List results + ) { return new ActionListener<>() { @Override public void onResponse(ExporterResourceStatus exporterResourceStatus) { @@ -168,13 +206,16 @@ private void addStatus(ExporterResourceStatus exporterResourceStatus) { private void finalResult() { try { - List collectedResults = results.stream().map(status -> - new ExporterMigrationResult( - status.getExporterName(), - status.getExporterType(), - status.isComplete(), - compileReason(status)) - ).collect(Collectors.toList()); + List collectedResults = results.stream() + .map( + status -> new ExporterMigrationResult( + status.getExporterName(), + status.getExporterType(), + status.isComplete(), + compileReason(status) + ) + ) + .collect(Collectors.toList()); MonitoringMigrateAlertsResponse response = new MonitoringMigrateAlertsResponse(collectedResults); listener.onResponse(response); } catch (Exception e) { diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/cleaner/CleanerService.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/cleaner/CleanerService.java index c26ee8c0c4b48..494dab578a3ee 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/cleaner/CleanerService.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/cleaner/CleanerService.java @@ -39,8 +39,13 @@ public class CleanerService extends AbstractLifecycleComponent { private volatile TimeValue globalRetention; - CleanerService(Settings settings, ClusterSettings clusterSettings, XPackLicenseState licenseState, ThreadPool threadPool, - ExecutionScheduler executionScheduler) { + CleanerService( + Settings settings, + ClusterSettings clusterSettings, + XPackLicenseState licenseState, + ThreadPool threadPool, + ExecutionScheduler executionScheduler + ) { this.licenseState = licenseState; this.threadPool = threadPool; this.executionScheduler = executionScheduler; @@ -58,8 +63,7 @@ public CleanerService(Settings settings, ClusterSettings clusterSettings, Thread @Override protected void doStart() { logger.debug("starting cleaning service"); - threadPool.schedule(runnable, executionScheduler.nextExecutionDelay(ZonedDateTime.now(Clock.systemDefaultZone())), - executorName()); + threadPool.schedule(runnable, executionScheduler.nextExecutionDelay(ZonedDateTime.now(Clock.systemDefaultZone())), executorName()); logger.debug("cleaning service started"); } @@ -225,9 +229,7 @@ static class DefaultExecutionScheduler implements ExecutionScheduler { @Override public TimeValue nextExecutionDelay(ZonedDateTime now) { // Runs at 01:00 AM today or the next day if it's too late - ZonedDateTime next = now.toLocalDate() - .atStartOfDay(now.getZone()) - .plusHours(1); + ZonedDateTime next = now.toLocalDate().atStartOfDay(now.getZone()).plusHours(1); // if it's not after now, then it needs to be the next day! if (next.isAfter(now) == false) { next = next.plusDays(1); diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/Collector.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/Collector.java index 9f9749e3c453f..4f5213eacb0db 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/Collector.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/Collector.java @@ -14,9 +14,9 @@ import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Setting; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.xpack.core.XPackField; @@ -40,9 +40,14 @@ public abstract class Collector { /** * List of indices names whose stats will be exported (default to all indices) */ - public static final Setting> INDICES = - listSetting(collectionSetting("indices"), emptyList(), Function.identity(), Property.Dynamic, Property.NodeScope, - Setting.Property.Deprecated); + public static final Setting> INDICES = listSetting( + collectionSetting("indices"), + emptyList(), + Function.identity(), + Property.Dynamic, + Property.NodeScope, + Setting.Property.Deprecated + ); private final String name; private final Setting collectionTimeoutSetting; @@ -51,8 +56,12 @@ public abstract class Collector { protected final XPackLicenseState licenseState; protected final Logger logger; - public Collector(final String name, final ClusterService clusterService, - final Setting timeoutSetting, final XPackLicenseState licenseState) { + public Collector( + final String name, + final ClusterService clusterService, + final Setting timeoutSetting, + final XPackLicenseState licenseState + ) { this.name = name; this.clusterService = clusterService; this.collectionTimeoutSetting = timeoutSetting; @@ -93,9 +102,8 @@ public Collection collect(final long timestamp, final long interv return null; } - protected abstract Collection doCollect(MonitoringDoc.Node node, - long interval, - ClusterState clusterState) throws Exception; + protected abstract Collection doCollect(MonitoringDoc.Node node, long interval, ClusterState clusterState) + throws Exception; /** * Returns a timestamp to use in {@link MonitoringDoc} @@ -156,12 +164,14 @@ public static MonitoringDoc.Node convertNode(final long timestamp, final @Nullab if (node == null) { return null; } - return new MonitoringDoc.Node(node.getId(), - node.getHostName(), - node.getAddress().toString(), - node.getHostAddress(), - node.getName(), - timestamp); + return new MonitoringDoc.Node( + node.getId(), + node.getHostName(), + node.getAddress().toString(), + node.getHostAddress(), + node.getName(), + timestamp + ); } protected static String collectionSetting(final String settingName) { diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/TimeoutUtils.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/TimeoutUtils.java index 4608ba4ed2e49..508d720a33d14 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/TimeoutUtils.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/TimeoutUtils.java @@ -26,8 +26,7 @@ * the involved nodes times out. */ public final class TimeoutUtils { - private TimeoutUtils() { - } + private TimeoutUtils() {} /** * @throws ElasticsearchTimeoutException iff the {@code response} contains any node-level timeout. The exception message identifies the @@ -90,14 +89,15 @@ public static void ensureNoTimeouts(TimeValue collectionTimeout, BroadcastRespon private static boolean isTimeoutFailure(FailedNodeException failedNodeException) { final Throwable cause = failedNodeException.getCause(); return cause instanceof ElasticsearchTimeoutException - || cause instanceof TimeoutException - || cause instanceof ReceiveTimeoutTransportException; + || cause instanceof TimeoutException + || cause instanceof ReceiveTimeoutTransportException; } private static void ensureNoTimeouts(TimeValue collectionTimeout, HashSet timedOutNodeIds) { if (timedOutNodeIds != null) { - throw new ElasticsearchTimeoutException((timedOutNodeIds.size() == 1 ? "node " : "nodes ") + timedOutNodeIds + - " did not respond within [" + collectionTimeout + "]"); + throw new ElasticsearchTimeoutException( + (timedOutNodeIds.size() == 1 ? "node " : "nodes ") + timedOutNodeIds + " did not respond within [" + collectionTimeout + "]" + ); } } diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/ccr/AutoFollowStatsMonitoringDoc.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/ccr/AutoFollowStatsMonitoringDoc.java index d5e767d75b5e3..b2a99ab819b70 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/ccr/AutoFollowStatsMonitoringDoc.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/ccr/AutoFollowStatsMonitoringDoc.java @@ -26,16 +26,16 @@ public AutoFollowStats stats() { } public AutoFollowStatsMonitoringDoc( - final String cluster, - final long timestamp, - final long intervalMillis, - final Node node, - final AutoFollowStats stats) { + final String cluster, + final long timestamp, + final long intervalMillis, + final Node node, + final AutoFollowStats stats + ) { super(cluster, timestamp, intervalMillis, node, MonitoredSystem.ES, TYPE, null); this.stats = Objects.requireNonNull(stats, "stats"); } - @Override protected void innerToXContent(final XContentBuilder builder, final Params params) throws IOException { builder.startObject(TYPE); diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/ccr/FollowStatsMonitoringDoc.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/ccr/FollowStatsMonitoringDoc.java index 1554d5a082d78..ad7f52d1a7865 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/ccr/FollowStatsMonitoringDoc.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/ccr/FollowStatsMonitoringDoc.java @@ -26,16 +26,16 @@ public ShardFollowNodeTaskStatus status() { } public FollowStatsMonitoringDoc( - final String cluster, - final long timestamp, - final long intervalMillis, - final MonitoringDoc.Node node, - final ShardFollowNodeTaskStatus status) { + final String cluster, + final long timestamp, + final long intervalMillis, + final MonitoringDoc.Node node, + final ShardFollowNodeTaskStatus status + ) { super(cluster, timestamp, intervalMillis, node, MonitoredSystem.ES, TYPE, null); this.status = Objects.requireNonNull(status, "status"); } - @Override protected void innerToXContent(final XContentBuilder builder, final Params params) throws IOException { builder.startObject(TYPE); diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/ccr/StatsCollector.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/ccr/StatsCollector.java index bb60acb4d22c3..3798ed1d71ca0 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/ccr/StatsCollector.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/ccr/StatsCollector.java @@ -12,8 +12,8 @@ import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.xpack.core.ccr.CcrConstants; @@ -40,19 +40,21 @@ public final class StatsCollector extends Collector { private final Client client; public StatsCollector( - final Settings settings, - final ClusterService clusterService, - final XPackLicenseState licenseState, - final Client client) { + final Settings settings, + final ClusterService clusterService, + final XPackLicenseState licenseState, + final Client client + ) { this(settings, clusterService, licenseState, client, client.threadPool().getThreadContext()); } StatsCollector( - final Settings settings, - final ClusterService clusterService, - final XPackLicenseState licenseState, - final Client client, - final ThreadContext threadContext) { + final Settings settings, + final ClusterService clusterService, + final XPackLicenseState licenseState, + final Client client, + final ThreadContext threadContext + ) { super(TYPE, clusterService, CCR_STATS_TIMEOUT, licenseState); this.settings = settings; this.client = client; @@ -63,17 +65,14 @@ public StatsCollector( protected boolean shouldCollect(final boolean isElectedMaster) { // this can only run when monitoring is allowed and CCR is enabled and allowed, but also only on the elected master node return isElectedMaster - && super.shouldCollect(isElectedMaster) - && XPackSettings.CCR_ENABLED_SETTING.get(settings) - && CcrConstants.CCR_FEATURE.checkWithoutTracking(licenseState); + && super.shouldCollect(isElectedMaster) + && XPackSettings.CCR_ENABLED_SETTING.get(settings) + && CcrConstants.CCR_FEATURE.checkWithoutTracking(licenseState); } - @Override - protected Collection doCollect( - final MonitoringDoc.Node node, - final long interval, - final ClusterState clusterState) throws Exception { + protected Collection doCollect(final MonitoringDoc.Node node, final long interval, final ClusterState clusterState) + throws Exception { try (ThreadContext.StoredContext ignore = threadContext.stashWithOrigin(MONITORING_ORIGIN)) { final long timestamp = timestamp(); final String clusterUuid = clusterUuid(clusterState); @@ -81,12 +80,16 @@ protected Collection doCollect( final CcrStatsAction.Request request = new CcrStatsAction.Request(); final CcrStatsAction.Response response = client.execute(CcrStatsAction.INSTANCE, request).actionGet(getCollectionTimeout()); - final AutoFollowStatsMonitoringDoc autoFollowStatsDoc = - new AutoFollowStatsMonitoringDoc(clusterUuid, timestamp, interval, node, response.getAutoFollowStats()); + final AutoFollowStatsMonitoringDoc autoFollowStatsDoc = new AutoFollowStatsMonitoringDoc( + clusterUuid, + timestamp, + interval, + node, + response.getAutoFollowStats() + ); Set collectionIndices = new HashSet<>(Arrays.asList(getCollectionIndices())); - List docs = response - .getFollowStats() + List docs = response.getFollowStats() .getStatsResponses() .stream() .filter(statsResponse -> collectionIndices.isEmpty() || collectionIndices.contains(statsResponse.status().followerIndex())) diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/cluster/ClusterStatsCollector.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/cluster/ClusterStatsCollector.java index c26e0805f40af..1756dc51a1560 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/cluster/ClusterStatsCollector.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/cluster/ClusterStatsCollector.java @@ -16,9 +16,9 @@ import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexNotFoundException; @@ -62,12 +62,14 @@ public class ClusterStatsCollector extends Collector { private final LicenseService licenseService; private final Client client; - public ClusterStatsCollector(final Settings settings, - final ClusterService clusterService, - final XPackLicenseState licenseState, - final Client client, - final LicenseService licenseService, - final IndexNameExpressionResolver indexNameExpressionResolver) { + public ClusterStatsCollector( + final Settings settings, + final ClusterService clusterService, + final XPackLicenseState licenseState, + final Client client, + final LicenseService licenseService, + final IndexNameExpressionResolver indexNameExpressionResolver + ) { super(ClusterStatsMonitoringDoc.TYPE, clusterService, CLUSTER_STATS_TIMEOUT, licenseState); this.settings = settings; this.client = client; @@ -82,11 +84,8 @@ protected boolean shouldCollect(final boolean isElectedMaster) { } @Override - protected Collection doCollect(final MonitoringDoc.Node node, - final long interval, - final ClusterState clusterState) { - final Supplier> usageSupplier = - () -> new XPackUsageRequestBuilder(client).get().getUsages(); + protected Collection doCollect(final MonitoringDoc.Node node, final long interval, final ClusterState clusterState) { + final Supplier> usageSupplier = () -> new XPackUsageRequestBuilder(client).get().getUsages(); final ClusterStatsResponse clusterStats = client.admin().cluster().prepareClusterStats().setTimeout(getCollectionTimeout()).get(); ensureNoTimeouts(getCollectionTimeout(), clusterStats); @@ -98,23 +97,35 @@ protected Collection doCollect(final MonitoringDoc.Node node, final List xpackUsage = collect(usageSupplier); final boolean apmIndicesExist = doAPMIndicesExist(clusterState); // if they have any other type of license, then they are either okay or already know - final boolean clusterNeedsTLSEnabled = license != null && - license.operationMode() == License.OperationMode.TRIAL && - settings.hasValue(SECURITY_ENABLED.getKey()) && - SECURITY_ENABLED.get(settings) && - TRANSPORT_SSL_ENABLED.get(settings) == false; + final boolean clusterNeedsTLSEnabled = license != null + && license.operationMode() == License.OperationMode.TRIAL + && settings.hasValue(SECURITY_ENABLED.getKey()) + && SECURITY_ENABLED.get(settings) + && TRANSPORT_SSL_ENABLED.get(settings) == false; // Adds a cluster stats document return Collections.singleton( - new ClusterStatsMonitoringDoc(clusterUuid, timestamp(), interval, node, clusterName, version, clusterStats.getStatus(), - license, apmIndicesExist, xpackUsage, clusterStats, clusterState, - clusterNeedsTLSEnabled)); + new ClusterStatsMonitoringDoc( + clusterUuid, + timestamp(), + interval, + node, + clusterName, + version, + clusterStats.getStatus(), + license, + apmIndicesExist, + xpackUsage, + clusterStats, + clusterState, + clusterNeedsTLSEnabled + ) + ); } boolean doAPMIndicesExist(final ClusterState clusterState) { try { - final Index[] indices = - indexNameExpressionResolver.concreteIndices(clusterState, IndicesOptions.lenientExpandOpen(), "apm-*"); + final Index[] indices = indexNameExpressionResolver.concreteIndices(clusterState, IndicesOptions.lenientExpandOpen(), "apm-*"); return indices.length > 0; } catch (IndexNotFoundException | IllegalArgumentException e) { @@ -128,8 +139,13 @@ private T collect(final Supplier supplier) { return supplier.get(); } catch (ElasticsearchSecurityException e) { if (LicenseUtils.isLicenseExpiredException(e)) { - logger.trace((Supplier) () -> new ParameterizedMessage("collector [{}] - " + - "unable to collect data because of expired license", name()), e); + logger.trace( + (Supplier) () -> new ParameterizedMessage( + "collector [{}] - " + "unable to collect data because of expired license", + name() + ), + e + ); } else { throw e; } diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/cluster/ClusterStatsMonitoringDoc.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/cluster/ClusterStatsMonitoringDoc.java index 332c1f3ec28bf..d669f069efd8b 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/cluster/ClusterStatsMonitoringDoc.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/cluster/ClusterStatsMonitoringDoc.java @@ -12,11 +12,11 @@ import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.node.DiscoveryNodes; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.collect.MapBuilder; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.license.License; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.license.License; import org.elasticsearch.xpack.core.XPackFeatureSet; import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc; @@ -39,12 +39,12 @@ */ public class ClusterStatsMonitoringDoc extends MonitoringDoc { - private static final ToXContent.MapParams CLUSTER_STATS_PARAMS = - new ToXContent.MapParams( - Collections.singletonMap("metric", - ClusterState.Metric.VERSION + "," + - ClusterState.Metric.MASTER_NODE + "," + - ClusterState.Metric.NODES)); + private static final ToXContent.MapParams CLUSTER_STATS_PARAMS = new ToXContent.MapParams( + Collections.singletonMap( + "metric", + ClusterState.Metric.VERSION + "," + ClusterState.Metric.MASTER_NODE + "," + ClusterState.Metric.NODES + ) + ); public static final String TYPE = "cluster_stats"; protected static final String SETTING_DISPLAY_NAME = "cluster.metadata.display_name"; @@ -59,19 +59,21 @@ public class ClusterStatsMonitoringDoc extends MonitoringDoc { private final ClusterHealthStatus status; private final boolean clusterNeedsTLSEnabled; - ClusterStatsMonitoringDoc(final String cluster, - final long timestamp, - final long intervalMillis, - final MonitoringDoc.Node node, - final String clusterName, - final String version, - final ClusterHealthStatus status, - @Nullable final License license, - final boolean apmIndicesExist, - @Nullable final List usages, - @Nullable final ClusterStatsResponse clusterStats, - @Nullable final ClusterState clusterState, - final boolean clusterNeedsTLSEnabled) { + ClusterStatsMonitoringDoc( + final String cluster, + final long timestamp, + final long intervalMillis, + final MonitoringDoc.Node node, + final String clusterName, + final String version, + final ClusterHealthStatus status, + @Nullable final License license, + final boolean apmIndicesExist, + @Nullable final List usages, + @Nullable final ClusterStatsResponse clusterStats, + @Nullable final ClusterState clusterState, + final boolean clusterNeedsTLSEnabled + ) { super(cluster, timestamp, intervalMillis, node, MonitoredSystem.ES, TYPE, null); this.clusterName = Objects.requireNonNull(clusterName); @@ -137,9 +139,7 @@ protected void innerToXContent(XContentBuilder builder, Params params) throws IO if (license != null) { builder.startObject("license"); { - Map extraParams = new MapBuilder() - .put(License.REST_VIEW_MODE, "true") - .map(); + Map extraParams = new MapBuilder().put(License.REST_VIEW_MODE, "true").map(); params = new ToXContent.DelegatingMapParams(extraParams, params); license.toInnerXContent(builder, params); if (clusterNeedsTLSEnabled) { diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/enrich/EnrichCoordinatorDoc.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/enrich/EnrichCoordinatorDoc.java index ed8683d4bf71d..d99501e75a362 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/enrich/EnrichCoordinatorDoc.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/enrich/EnrichCoordinatorDoc.java @@ -20,11 +20,13 @@ public final class EnrichCoordinatorDoc extends MonitoringDoc { private final CoordinatorStats coordinatorStats; - public EnrichCoordinatorDoc(String cluster, - long timestamp, - long intervalMillis, - MonitoringDoc.Node node, - CoordinatorStats coordinatorStats) { + public EnrichCoordinatorDoc( + String cluster, + long timestamp, + long intervalMillis, + MonitoringDoc.Node node, + CoordinatorStats coordinatorStats + ) { super(cluster, timestamp, intervalMillis, node, MonitoredSystem.ES, TYPE, null); this.coordinatorStats = Objects.requireNonNull(coordinatorStats, "stats"); } diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/enrich/EnrichStatsCollector.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/enrich/EnrichStatsCollector.java index 2ffc7073044d8..27a66fc722657 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/enrich/EnrichStatsCollector.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/enrich/EnrichStatsCollector.java @@ -10,8 +10,8 @@ import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.settings.Setting; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.xpack.core.enrich.action.EnrichStatsAction; import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc; @@ -30,16 +30,11 @@ public final class EnrichStatsCollector extends Collector { private final Client client; private final ThreadContext threadContext; - public EnrichStatsCollector(ClusterService clusterService, - XPackLicenseState licenseState, - Client client) { + public EnrichStatsCollector(ClusterService clusterService, XPackLicenseState licenseState, Client client) { this(clusterService, licenseState, client, client.threadPool().getThreadContext()); } - EnrichStatsCollector(ClusterService clusterService, - XPackLicenseState licenseState, - Client client, - ThreadContext threadContext) { + EnrichStatsCollector(ClusterService clusterService, XPackLicenseState licenseState, Client client, ThreadContext threadContext) { super(EnrichCoordinatorDoc.TYPE, clusterService, STATS_TIMEOUT, licenseState); this.client = client; this.threadContext = threadContext; @@ -57,14 +52,16 @@ protected Collection doCollect(MonitoringDoc.Node node, long inte final String clusterUuid = clusterUuid(clusterState); final EnrichStatsAction.Request request = new EnrichStatsAction.Request(); - final EnrichStatsAction.Response response = - client.execute(EnrichStatsAction.INSTANCE, request).actionGet(getCollectionTimeout()); + final EnrichStatsAction.Response response = client.execute(EnrichStatsAction.INSTANCE, request) + .actionGet(getCollectionTimeout()); - final List docs = response.getCoordinatorStats().stream() + final List docs = response.getCoordinatorStats() + .stream() .map(stats -> new EnrichCoordinatorDoc(clusterUuid, timestamp, interval, node, stats)) .collect(Collectors.toList()); - response.getExecutingPolicies().stream() + response.getExecutingPolicies() + .stream() .map(stats -> new ExecutingPolicyDoc(clusterUuid, timestamp, interval, node, stats)) .forEach(docs::add); diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/enrich/ExecutingPolicyDoc.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/enrich/ExecutingPolicyDoc.java index e5dd723250623..76687af08bb0e 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/enrich/ExecutingPolicyDoc.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/enrich/ExecutingPolicyDoc.java @@ -20,11 +20,7 @@ public final class ExecutingPolicyDoc extends MonitoringDoc { private final ExecutingPolicy executingPolicy; - public ExecutingPolicyDoc(String cluster, - long timestamp, - long intervalMillis, - Node node, - ExecutingPolicy coordinatorStats) { + public ExecutingPolicyDoc(String cluster, long timestamp, long intervalMillis, Node node, ExecutingPolicy coordinatorStats) { super(cluster, timestamp, intervalMillis, node, MonitoredSystem.ES, TYPE, null); this.executingPolicy = Objects.requireNonNull(coordinatorStats, "stats"); } diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexRecoveryCollector.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexRecoveryCollector.java index c44a28cc2c2c1..c1e7b97e7ff67 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexRecoveryCollector.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexRecoveryCollector.java @@ -42,15 +42,17 @@ public class IndexRecoveryCollector extends Collector { /** * Flag to indicate if only active recoveries should be collected (default to false: all recoveries are collected) */ - public static final Setting INDEX_RECOVERY_ACTIVE_ONLY = - boolSetting(collectionSetting("index.recovery.active_only"), false, Setting.Property.Dynamic, Setting.Property.NodeScope, - Setting.Property.Deprecated); + public static final Setting INDEX_RECOVERY_ACTIVE_ONLY = boolSetting( + collectionSetting("index.recovery.active_only"), + false, + Setting.Property.Dynamic, + Setting.Property.NodeScope, + Setting.Property.Deprecated + ); private final Client client; - public IndexRecoveryCollector(final ClusterService clusterService, - final XPackLicenseState licenseState, - final Client client) { + public IndexRecoveryCollector(final ClusterService clusterService, final XPackLicenseState licenseState, final Client client) { super(IndexRecoveryMonitoringDoc.TYPE, clusterService, INDEX_RECOVERY_TIMEOUT, licenseState); this.client = Objects.requireNonNull(client); } @@ -65,16 +67,16 @@ protected boolean shouldCollect(final boolean isElectedMaster) { } @Override - protected Collection doCollect(final MonitoringDoc.Node node, - final long interval, - final ClusterState clusterState) { + protected Collection doCollect(final MonitoringDoc.Node node, final long interval, final ClusterState clusterState) { List results = new ArrayList<>(1); - RecoveryResponse recoveryResponse = client.admin().indices().prepareRecoveries() - .setIndices(getCollectionIndices()) - .setIndicesOptions(IndicesOptions.lenientExpandOpen()) - .setActiveOnly(getActiveRecoveriesOnly()) - .setTimeout(getCollectionTimeout()) - .get(); + RecoveryResponse recoveryResponse = client.admin() + .indices() + .prepareRecoveries() + .setIndices(getCollectionIndices()) + .setIndicesOptions(IndicesOptions.lenientExpandOpen()) + .setActiveOnly(getActiveRecoveriesOnly()) + .setTimeout(getCollectionTimeout()) + .get(); ensureNoTimeouts(getCollectionTimeout(), recoveryResponse); diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexRecoveryMonitoringDoc.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexRecoveryMonitoringDoc.java index 1417a0da90b90..66949cdfa1fd4 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexRecoveryMonitoringDoc.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexRecoveryMonitoringDoc.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.monitoring.collector.indices; import org.elasticsearch.action.admin.indices.recovery.RecoveryResponse; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.indices.recovery.RecoveryState; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc; @@ -26,11 +26,13 @@ public class IndexRecoveryMonitoringDoc extends MonitoringDoc { private final RecoveryResponse recoveryResponse; - public IndexRecoveryMonitoringDoc(final String cluster, - final long timestamp, - final long intervalMillis, - final MonitoringDoc.Node node, - final RecoveryResponse recoveryResponse) { + public IndexRecoveryMonitoringDoc( + final String cluster, + final long timestamp, + final long intervalMillis, + final MonitoringDoc.Node node, + final RecoveryResponse recoveryResponse + ) { super(cluster, timestamp, intervalMillis, node, MonitoredSystem.ES, TYPE, null); this.recoveryResponse = Objects.requireNonNull(recoveryResponse); diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexStatsCollector.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexStatsCollector.java index 88e32ca51baaa..52b80d4eba3b3 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexStatsCollector.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexStatsCollector.java @@ -42,9 +42,7 @@ public class IndexStatsCollector extends Collector { private final Client client; - public IndexStatsCollector(final ClusterService clusterService, - final XPackLicenseState licenseState, - final Client client) { + public IndexStatsCollector(final ClusterService clusterService, final XPackLicenseState licenseState, final Client client) { super("index-stats", clusterService, INDEX_STATS_TIMEOUT, licenseState); this.client = client; } @@ -55,27 +53,27 @@ protected boolean shouldCollect(final boolean isElectedMaster) { } @Override - protected Collection doCollect(final MonitoringDoc.Node node, - final long interval, - final ClusterState clusterState) { + protected Collection doCollect(final MonitoringDoc.Node node, final long interval, final ClusterState clusterState) { final List results = new ArrayList<>(); - final IndicesStatsResponse indicesStatsResponse = client.admin().indices().prepareStats() - .setIndices(getCollectionIndices()) - .setIndicesOptions(IndicesOptions.lenientExpandOpen()) - .clear() - .setDocs(true) - .setFieldData(true) - .setIndexing(true) - .setMerge(true) - .setSearch(true) - .setSegments(true) - .setStore(true) - .setRefresh(true) - .setQueryCache(true) - .setRequestCache(true) - .setBulk(true) - .setTimeout(getCollectionTimeout()) - .get(); + final IndicesStatsResponse indicesStatsResponse = client.admin() + .indices() + .prepareStats() + .setIndices(getCollectionIndices()) + .setIndicesOptions(IndicesOptions.lenientExpandOpen()) + .clear() + .setDocs(true) + .setFieldData(true) + .setIndexing(true) + .setMerge(true) + .setSearch(true) + .setSegments(true) + .setStore(true) + .setRefresh(true) + .setQueryCache(true) + .setRequestCache(true) + .setBulk(true) + .setTimeout(getCollectionTimeout()) + .get(); ensureNoTimeouts(getCollectionTimeout(), indicesStatsResponse); @@ -93,8 +91,17 @@ protected Collection doCollect(final MonitoringDoc.Node node, // The index appears both in the local cluster state and indices stats response indicesStats.add(indexStats); - results.add(new IndexStatsMonitoringDoc(clusterUuid, timestamp, interval, node, indexStats, - metadata.index(indexName), routingTable.index(indexName))); + results.add( + new IndexStatsMonitoringDoc( + clusterUuid, + timestamp, + interval, + node, + indexStats, + metadata.index(indexName), + routingTable.index(indexName) + ) + ); } } results.add(new IndicesStatsMonitoringDoc(clusterUuid, timestamp, interval, node, indicesStats)); diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexStatsMonitoringDoc.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexStatsMonitoringDoc.java index 36baa241d1806..aec384fac0d8c 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexStatsMonitoringDoc.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexStatsMonitoringDoc.java @@ -11,8 +11,8 @@ import org.elasticsearch.cluster.health.ClusterIndexHealth; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.routing.IndexRoutingTable; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.util.set.Sets; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc; @@ -34,13 +34,15 @@ public class IndexStatsMonitoringDoc extends FilteredMonitoringDoc { private final IndexMetadata metadata; private final IndexRoutingTable routingTable; - IndexStatsMonitoringDoc(final String cluster, - final long timestamp, - final long intervalMillis, - final MonitoringDoc.Node node, - @Nullable final IndexStats indexStats, - final IndexMetadata metadata, - final IndexRoutingTable routingTable) { + IndexStatsMonitoringDoc( + final String cluster, + final long timestamp, + final long intervalMillis, + final MonitoringDoc.Node node, + @Nullable final IndexStats indexStats, + final IndexMetadata metadata, + final IndexRoutingTable routingTable + ) { super(cluster, timestamp, intervalMillis, node, MonitoredSystem.ES, TYPE, null, XCONTENT_FILTERS); this.indexStats = indexStats; this.metadata = Objects.requireNonNull(metadata); @@ -117,92 +119,93 @@ protected void innerToXContent(XContentBuilder builder, Params params) throws IO builder.endObject(); } - public static final Set XCONTENT_FILTERS = - Sets.newHashSet("index_stats.index", - "index_stats.uuid", - "index_stats.created", - "index_stats.status", - "index_stats.shards.total", - "index_stats.shards.primaries", - "index_stats.shards.replicas", - "index_stats.shards.active_total", - "index_stats.shards.active_primaries", - "index_stats.shards.active_replicas", - "index_stats.shards.unassigned_total", - "index_stats.shards.unassigned_primaries", - "index_stats.shards.unassigned_replicas", - "index_stats.shards.initializing", - "index_stats.shards.relocating", - "index_stats.primaries.docs.count", - "index_stats.primaries.fielddata.memory_size_in_bytes", - "index_stats.primaries.fielddata.evictions", - "index_stats.primaries.indexing.index_total", - "index_stats.primaries.indexing.index_time_in_millis", - "index_stats.primaries.indexing.throttle_time_in_millis", - "index_stats.primaries.merges.total_size_in_bytes", - "index_stats.primaries.query_cache.memory_size_in_bytes", - "index_stats.primaries.query_cache.evictions", - "index_stats.primaries.query_cache.hit_count", - "index_stats.primaries.query_cache.miss_count", - "index_stats.primaries.request_cache.memory_size_in_bytes", - "index_stats.primaries.request_cache.evictions", - "index_stats.primaries.request_cache.hit_count", - "index_stats.primaries.request_cache.miss_count", - "index_stats.primaries.search.query_total", - "index_stats.primaries.search.query_time_in_millis", - "index_stats.primaries.segments.count", - "index_stats.primaries.segments.memory_in_bytes", - "index_stats.primaries.segments.terms_memory_in_bytes", - "index_stats.primaries.segments.stored_fields_memory_in_bytes", - "index_stats.primaries.segments.term_vectors_memory_in_bytes", - "index_stats.primaries.segments.norms_memory_in_bytes", - "index_stats.primaries.segments.points_memory_in_bytes", - "index_stats.primaries.segments.doc_values_memory_in_bytes", - "index_stats.primaries.segments.index_writer_memory_in_bytes", - "index_stats.primaries.segments.version_map_memory_in_bytes", - "index_stats.primaries.segments.fixed_bit_set_memory_in_bytes", - "index_stats.primaries.store.size_in_bytes", - "index_stats.primaries.refresh.total_time_in_millis", - "index_stats.primaries.refresh.external_total_time_in_millis", - "index_stats.primaries.bulk.total_operations", - "index_stats.primaries.bulk.total_time_in_millis", - "index_stats.primaries.bulk.total_size_in_bytes", - "index_stats.primaries.bulk.avg_time_in_millis", - "index_stats.primaries.bulk.avg_size_in_bytes", - "index_stats.total.docs.count", - "index_stats.total.fielddata.memory_size_in_bytes", - "index_stats.total.fielddata.evictions", - "index_stats.total.indexing.index_total", - "index_stats.total.indexing.index_time_in_millis", - "index_stats.total.indexing.throttle_time_in_millis", - "index_stats.total.merges.total_size_in_bytes", - "index_stats.total.query_cache.memory_size_in_bytes", - "index_stats.total.query_cache.evictions", - "index_stats.total.query_cache.hit_count", - "index_stats.total.query_cache.miss_count", - "index_stats.total.request_cache.memory_size_in_bytes", - "index_stats.total.request_cache.evictions", - "index_stats.total.request_cache.hit_count", - "index_stats.total.request_cache.miss_count", - "index_stats.total.search.query_total", - "index_stats.total.search.query_time_in_millis", - "index_stats.total.segments.count", - "index_stats.total.segments.memory_in_bytes", - "index_stats.total.segments.terms_memory_in_bytes", - "index_stats.total.segments.stored_fields_memory_in_bytes", - "index_stats.total.segments.term_vectors_memory_in_bytes", - "index_stats.total.segments.norms_memory_in_bytes", - "index_stats.total.segments.points_memory_in_bytes", - "index_stats.total.segments.doc_values_memory_in_bytes", - "index_stats.total.segments.index_writer_memory_in_bytes", - "index_stats.total.segments.version_map_memory_in_bytes", - "index_stats.total.segments.fixed_bit_set_memory_in_bytes", - "index_stats.total.store.size_in_bytes", - "index_stats.total.refresh.total_time_in_millis", - "index_stats.total.refresh.external_total_time_in_millis", - "index_stats.total.bulk.total_operations", - "index_stats.total.bulk.total_time_in_millis", - "index_stats.total.bulk.total_size_in_bytes", - "index_stats.total.bulk.avg_time_in_millis", - "index_stats.total.bulk.avg_size_in_bytes"); + public static final Set XCONTENT_FILTERS = Sets.newHashSet( + "index_stats.index", + "index_stats.uuid", + "index_stats.created", + "index_stats.status", + "index_stats.shards.total", + "index_stats.shards.primaries", + "index_stats.shards.replicas", + "index_stats.shards.active_total", + "index_stats.shards.active_primaries", + "index_stats.shards.active_replicas", + "index_stats.shards.unassigned_total", + "index_stats.shards.unassigned_primaries", + "index_stats.shards.unassigned_replicas", + "index_stats.shards.initializing", + "index_stats.shards.relocating", + "index_stats.primaries.docs.count", + "index_stats.primaries.fielddata.memory_size_in_bytes", + "index_stats.primaries.fielddata.evictions", + "index_stats.primaries.indexing.index_total", + "index_stats.primaries.indexing.index_time_in_millis", + "index_stats.primaries.indexing.throttle_time_in_millis", + "index_stats.primaries.merges.total_size_in_bytes", + "index_stats.primaries.query_cache.memory_size_in_bytes", + "index_stats.primaries.query_cache.evictions", + "index_stats.primaries.query_cache.hit_count", + "index_stats.primaries.query_cache.miss_count", + "index_stats.primaries.request_cache.memory_size_in_bytes", + "index_stats.primaries.request_cache.evictions", + "index_stats.primaries.request_cache.hit_count", + "index_stats.primaries.request_cache.miss_count", + "index_stats.primaries.search.query_total", + "index_stats.primaries.search.query_time_in_millis", + "index_stats.primaries.segments.count", + "index_stats.primaries.segments.memory_in_bytes", + "index_stats.primaries.segments.terms_memory_in_bytes", + "index_stats.primaries.segments.stored_fields_memory_in_bytes", + "index_stats.primaries.segments.term_vectors_memory_in_bytes", + "index_stats.primaries.segments.norms_memory_in_bytes", + "index_stats.primaries.segments.points_memory_in_bytes", + "index_stats.primaries.segments.doc_values_memory_in_bytes", + "index_stats.primaries.segments.index_writer_memory_in_bytes", + "index_stats.primaries.segments.version_map_memory_in_bytes", + "index_stats.primaries.segments.fixed_bit_set_memory_in_bytes", + "index_stats.primaries.store.size_in_bytes", + "index_stats.primaries.refresh.total_time_in_millis", + "index_stats.primaries.refresh.external_total_time_in_millis", + "index_stats.primaries.bulk.total_operations", + "index_stats.primaries.bulk.total_time_in_millis", + "index_stats.primaries.bulk.total_size_in_bytes", + "index_stats.primaries.bulk.avg_time_in_millis", + "index_stats.primaries.bulk.avg_size_in_bytes", + "index_stats.total.docs.count", + "index_stats.total.fielddata.memory_size_in_bytes", + "index_stats.total.fielddata.evictions", + "index_stats.total.indexing.index_total", + "index_stats.total.indexing.index_time_in_millis", + "index_stats.total.indexing.throttle_time_in_millis", + "index_stats.total.merges.total_size_in_bytes", + "index_stats.total.query_cache.memory_size_in_bytes", + "index_stats.total.query_cache.evictions", + "index_stats.total.query_cache.hit_count", + "index_stats.total.query_cache.miss_count", + "index_stats.total.request_cache.memory_size_in_bytes", + "index_stats.total.request_cache.evictions", + "index_stats.total.request_cache.hit_count", + "index_stats.total.request_cache.miss_count", + "index_stats.total.search.query_total", + "index_stats.total.search.query_time_in_millis", + "index_stats.total.segments.count", + "index_stats.total.segments.memory_in_bytes", + "index_stats.total.segments.terms_memory_in_bytes", + "index_stats.total.segments.stored_fields_memory_in_bytes", + "index_stats.total.segments.term_vectors_memory_in_bytes", + "index_stats.total.segments.norms_memory_in_bytes", + "index_stats.total.segments.points_memory_in_bytes", + "index_stats.total.segments.doc_values_memory_in_bytes", + "index_stats.total.segments.index_writer_memory_in_bytes", + "index_stats.total.segments.version_map_memory_in_bytes", + "index_stats.total.segments.fixed_bit_set_memory_in_bytes", + "index_stats.total.store.size_in_bytes", + "index_stats.total.refresh.total_time_in_millis", + "index_stats.total.refresh.external_total_time_in_millis", + "index_stats.total.bulk.total_operations", + "index_stats.total.bulk.total_time_in_millis", + "index_stats.total.bulk.total_size_in_bytes", + "index_stats.total.bulk.avg_time_in_millis", + "index_stats.total.bulk.avg_size_in_bytes" + ); } diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/indices/IndicesStatsMonitoringDoc.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/indices/IndicesStatsMonitoringDoc.java index a5cde39d4b5a4..eb729d10be00c 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/indices/IndicesStatsMonitoringDoc.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/indices/IndicesStatsMonitoringDoc.java @@ -29,11 +29,13 @@ public class IndicesStatsMonitoringDoc extends FilteredMonitoringDoc { private final List indicesStats; - IndicesStatsMonitoringDoc(final String cluster, - final long timestamp, - final long intervalMillis, - final MonitoringDoc.Node node, - final List indicesStats) { + IndicesStatsMonitoringDoc( + final String cluster, + final long timestamp, + final long intervalMillis, + final MonitoringDoc.Node node, + final List indicesStats + ) { super(cluster, timestamp, intervalMillis, node, MonitoredSystem.ES, TYPE, null, XCONTENT_FILTERS); this.indicesStats = Objects.requireNonNull(indicesStats); } @@ -92,31 +94,32 @@ protected void innerToXContent(XContentBuilder builder, Params params) throws IO builder.endObject(); } - public static final Set XCONTENT_FILTERS = - Sets.newHashSet("indices_stats._all.primaries.docs.count", - "indices_stats._all.primaries.indexing.index_time_in_millis", - "indices_stats._all.primaries.indexing.index_total", - "indices_stats._all.primaries.indexing.is_throttled", - "indices_stats._all.primaries.indexing.throttle_time_in_millis", - "indices_stats._all.primaries.search.query_time_in_millis", - "indices_stats._all.primaries.search.query_total", - "indices_stats._all.primaries.store.size_in_bytes", - "indices_stats._all.primaries.bulk.total_operations", - "indices_stats._all.primaries.bulk.total_time_in_millis", - "indices_stats._all.primaries.bulk.total_size_in_bytes", - "indices_stats._all.primaries.bulk.avg_time_in_millis", - "indices_stats._all.primaries.bulk.avg_size_in_bytes", - "indices_stats._all.total.docs.count", - "indices_stats._all.total.indexing.index_time_in_millis", - "indices_stats._all.total.indexing.index_total", - "indices_stats._all.total.indexing.is_throttled", - "indices_stats._all.total.indexing.throttle_time_in_millis", - "indices_stats._all.total.search.query_time_in_millis", - "indices_stats._all.total.search.query_total", - "indices_stats._all.total.store.size_in_bytes", - "indices_stats._all.total.bulk.total_operations", - "indices_stats._all.total.bulk.total_time_in_millis", - "indices_stats._all.total.bulk.total_size_in_bytes", - "indices_stats._all.total.bulk.avg_time_in_millis", - "indices_stats._all.total.bulk.avg_size_in_bytes"); + public static final Set XCONTENT_FILTERS = Sets.newHashSet( + "indices_stats._all.primaries.docs.count", + "indices_stats._all.primaries.indexing.index_time_in_millis", + "indices_stats._all.primaries.indexing.index_total", + "indices_stats._all.primaries.indexing.is_throttled", + "indices_stats._all.primaries.indexing.throttle_time_in_millis", + "indices_stats._all.primaries.search.query_time_in_millis", + "indices_stats._all.primaries.search.query_total", + "indices_stats._all.primaries.store.size_in_bytes", + "indices_stats._all.primaries.bulk.total_operations", + "indices_stats._all.primaries.bulk.total_time_in_millis", + "indices_stats._all.primaries.bulk.total_size_in_bytes", + "indices_stats._all.primaries.bulk.avg_time_in_millis", + "indices_stats._all.primaries.bulk.avg_size_in_bytes", + "indices_stats._all.total.docs.count", + "indices_stats._all.total.indexing.index_time_in_millis", + "indices_stats._all.total.indexing.index_total", + "indices_stats._all.total.indexing.is_throttled", + "indices_stats._all.total.indexing.throttle_time_in_millis", + "indices_stats._all.total.search.query_time_in_millis", + "indices_stats._all.total.search.query_total", + "indices_stats._all.total.store.size_in_bytes", + "indices_stats._all.total.bulk.total_operations", + "indices_stats._all.total.bulk.total_time_in_millis", + "indices_stats._all.total.bulk.total_size_in_bytes", + "indices_stats._all.total.bulk.avg_time_in_millis", + "indices_stats._all.total.bulk.avg_size_in_bytes" + ); } diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/ml/JobStatsCollector.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/ml/JobStatsCollector.java index 71267d1c2edf1..2dff908ac8db6 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/ml/JobStatsCollector.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/ml/JobStatsCollector.java @@ -46,13 +46,22 @@ public class JobStatsCollector extends Collector { private final ThreadContext threadContext; private final Client client; - public JobStatsCollector(final Settings settings, final ClusterService clusterService, - final XPackLicenseState licenseState, final Client client) { + public JobStatsCollector( + final Settings settings, + final ClusterService clusterService, + final XPackLicenseState licenseState, + final Client client + ) { this(settings, clusterService, licenseState, client, client.threadPool().getThreadContext()); } - JobStatsCollector(final Settings settings, final ClusterService clusterService, - final XPackLicenseState licenseState, final Client client, final ThreadContext threadContext) { + JobStatsCollector( + final Settings settings, + final ClusterService clusterService, + final XPackLicenseState licenseState, + final Client client, + final ThreadContext threadContext + ) { super(JobStatsMonitoringDoc.TYPE, clusterService, JOB_STATS_TIMEOUT, licenseState); this.settings = settings; this.client = client; @@ -63,15 +72,14 @@ public JobStatsCollector(final Settings settings, final ClusterService clusterSe protected boolean shouldCollect(final boolean isElectedMaster) { // This can only run when monitoring is allowed + ML is enabled/allowed, but also only on the elected master node return isElectedMaster - && super.shouldCollect(isElectedMaster) - && XPackSettings.MACHINE_LEARNING_ENABLED.get(settings) - && ML_API_FEATURE.checkWithoutTracking(licenseState); + && super.shouldCollect(isElectedMaster) + && XPackSettings.MACHINE_LEARNING_ENABLED.get(settings) + && ML_API_FEATURE.checkWithoutTracking(licenseState); } @Override - protected List doCollect(final MonitoringDoc.Node node, - final long interval, - final ClusterState clusterState) throws Exception { + protected List doCollect(final MonitoringDoc.Node node, final long interval, final ClusterState clusterState) + throws Exception { // fetch details about all jobs try (ThreadContext.StoredContext ignore = threadContext.stashWithOrigin(MONITORING_ORIGIN)) { final GetJobsStatsAction.Request request = new GetJobsStatsAction.Request(Metadata.ALL).setTimeout(getCollectionTimeout()); @@ -82,9 +90,11 @@ protected List doCollect(final MonitoringDoc.Node node, final long timestamp = timestamp(); final String clusterUuid = clusterUuid(clusterState); - return jobs.getResponse().results().stream() - .map(jobStats -> new JobStatsMonitoringDoc(clusterUuid, timestamp, interval, node, jobStats)) - .collect(Collectors.toList()); + return jobs.getResponse() + .results() + .stream() + .map(jobStats -> new JobStatsMonitoringDoc(clusterUuid, timestamp, interval, node, jobStats)) + .collect(Collectors.toList()); } } diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/ml/JobStatsMonitoringDoc.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/ml/JobStatsMonitoringDoc.java index 4784d56bb639a..e3d35fb299f23 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/ml/JobStatsMonitoringDoc.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/ml/JobStatsMonitoringDoc.java @@ -25,11 +25,13 @@ public class JobStatsMonitoringDoc extends MonitoringDoc { private final JobStats jobStats; - public JobStatsMonitoringDoc(final String cluster, - final long timestamp, - final long intervalMillis, - final MonitoringDoc.Node node, - final JobStats jobStats) { + public JobStatsMonitoringDoc( + final String cluster, + final long timestamp, + final long intervalMillis, + final MonitoringDoc.Node node, + final JobStats jobStats + ) { super(cluster, timestamp, intervalMillis, node, MonitoredSystem.ES, TYPE, null); this.jobStats = Objects.requireNonNull(jobStats); } diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/node/NodeStatsCollector.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/node/NodeStatsCollector.java index 60dfb82d547c9..8132a598f28d8 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/node/NodeStatsCollector.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/node/NodeStatsCollector.java @@ -39,22 +39,21 @@ public class NodeStatsCollector extends Collector { */ public static final Setting NODE_STATS_TIMEOUT = collectionTimeoutSetting("node.stats.timeout"); - private static final CommonStatsFlags FLAGS = - new CommonStatsFlags(CommonStatsFlags.Flag.Docs, - CommonStatsFlags.Flag.FieldData, - CommonStatsFlags.Flag.Store, - CommonStatsFlags.Flag.Indexing, - CommonStatsFlags.Flag.QueryCache, - CommonStatsFlags.Flag.RequestCache, - CommonStatsFlags.Flag.Search, - CommonStatsFlags.Flag.Segments, - CommonStatsFlags.Flag.Bulk); + private static final CommonStatsFlags FLAGS = new CommonStatsFlags( + CommonStatsFlags.Flag.Docs, + CommonStatsFlags.Flag.FieldData, + CommonStatsFlags.Flag.Store, + CommonStatsFlags.Flag.Indexing, + CommonStatsFlags.Flag.QueryCache, + CommonStatsFlags.Flag.RequestCache, + CommonStatsFlags.Flag.Search, + CommonStatsFlags.Flag.Segments, + CommonStatsFlags.Flag.Bulk + ); private final Client client; - public NodeStatsCollector(final ClusterService clusterService, - final XPackLicenseState licenseState, - final Client client) { + public NodeStatsCollector(final ClusterService clusterService, final XPackLicenseState licenseState, final Client client) { super(NodeStatsMonitoringDoc.TYPE, clusterService, NODE_STATS_TIMEOUT, licenseState); this.client = Objects.requireNonNull(client); } @@ -66,9 +65,7 @@ protected boolean shouldCollect(final boolean isElectedMaster) { } @Override - protected Collection doCollect(final MonitoringDoc.Node node, - final long interval, - final ClusterState clusterState) { + protected Collection doCollect(final MonitoringDoc.Node node, final long interval, final ClusterState clusterState) { NodesStatsRequest request = new NodesStatsRequest("_local"); request.indices(FLAGS); request.addMetrics( @@ -76,7 +73,8 @@ protected Collection doCollect(final MonitoringDoc.Node node, NodesStatsRequest.Metric.JVM.metricName(), NodesStatsRequest.Metric.PROCESS.metricName(), NodesStatsRequest.Metric.THREAD_POOL.metricName(), - NodesStatsRequest.Metric.FS.metricName()); + NodesStatsRequest.Metric.FS.metricName() + ); request.timeout(getCollectionTimeout()); final NodesStatsResponse response = client.admin().cluster().nodesStats(request).actionGet(); @@ -91,8 +89,18 @@ protected Collection doCollect(final MonitoringDoc.Node node, final String clusterUuid = clusterUuid(clusterState); final NodeStats nodeStats = response.getNodes().get(0); - return Collections.singletonList(new NodeStatsMonitoringDoc(clusterUuid, nodeStats.getTimestamp(), interval, node, - node.getUUID(), clusterState.getNodes().isLocalNodeElectedMaster(), nodeStats, BootstrapInfo.isMemoryLocked())); + return Collections.singletonList( + new NodeStatsMonitoringDoc( + clusterUuid, + nodeStats.getTimestamp(), + interval, + node, + node.getUUID(), + clusterState.getNodes().isLocalNodeElectedMaster(), + nodeStats, + BootstrapInfo.isMemoryLocked() + ) + ); } } diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/node/NodeStatsMonitoringDoc.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/node/NodeStatsMonitoringDoc.java index 3ffc1c64fa953..38039fcdc1638 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/node/NodeStatsMonitoringDoc.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/node/NodeStatsMonitoringDoc.java @@ -29,14 +29,16 @@ public class NodeStatsMonitoringDoc extends FilteredMonitoringDoc { private final NodeStats nodeStats; private final boolean mlockall; - NodeStatsMonitoringDoc(final String cluster, - final long timestamp, - final long interval, - final MonitoringDoc.Node node, - final String nodeId, - final boolean isMaster, - final NodeStats nodeStats, - final boolean mlockall) { + NodeStatsMonitoringDoc( + final String cluster, + final long timestamp, + final long interval, + final MonitoringDoc.Node node, + final String nodeId, + final boolean isMaster, + final NodeStats nodeStats, + final boolean mlockall + ) { super(cluster, timestamp, interval, node, MonitoredSystem.ES, TYPE, null, XCONTENT_FILTERS); this.nodeId = Objects.requireNonNull(nodeId); @@ -73,100 +75,101 @@ protected void innerToXContent(XContentBuilder builder, Params params) throws IO builder.endObject(); } - public static final Set XCONTENT_FILTERS = - Sets.newHashSet("node_stats.node_id", - "node_stats.node_master", - "node_stats.mlockall", - "node_stats.indices.docs.count", - "node_stats.indices.fielddata.memory_size_in_bytes", - "node_stats.indices.fielddata.evictions", - "node_stats.indices.store.size_in_bytes", - "node_stats.indices.indexing.throttle_time_in_millis", - "node_stats.indices.indexing.index_total", - "node_stats.indices.indexing.index_time_in_millis", - "node_stats.indices.query_cache.memory_size_in_bytes", - "node_stats.indices.query_cache.evictions", - "node_stats.indices.query_cache.hit_count", - "node_stats.indices.query_cache.miss_count", - "node_stats.indices.request_cache.memory_size_in_bytes", - "node_stats.indices.request_cache.evictions", - "node_stats.indices.request_cache.hit_count", - "node_stats.indices.request_cache.miss_count", - "node_stats.indices.search.query_total", - "node_stats.indices.search.query_time_in_millis", - "node_stats.indices.segments.count", - "node_stats.indices.segments.memory_in_bytes", - "node_stats.indices.segments.terms_memory_in_bytes", - "node_stats.indices.segments.stored_fields_memory_in_bytes", - "node_stats.indices.segments.term_vectors_memory_in_bytes", - "node_stats.indices.segments.norms_memory_in_bytes", - "node_stats.indices.segments.points_memory_in_bytes", - "node_stats.indices.segments.doc_values_memory_in_bytes", - "node_stats.indices.segments.index_writer_memory_in_bytes", - "node_stats.indices.segments.version_map_memory_in_bytes", - "node_stats.indices.segments.fixed_bit_set_memory_in_bytes", - "node_stats.indices.bulk.total_operations", - "node_stats.indices.bulk.total_time_in_millis", - "node_stats.indices.bulk.total_size_in_bytes", - "node_stats.indices.bulk.avg_time_in_millis", - "node_stats.indices.bulk.avg_size_in_bytes", - "node_stats.fs.io_stats.total.operations", - "node_stats.fs.io_stats.total.read_operations", - "node_stats.fs.io_stats.total.write_operations", - "node_stats.fs.io_stats.total.read_kilobytes", - "node_stats.fs.io_stats.total.write_kilobytes", - "node_stats.fs.total.total_in_bytes", - "node_stats.fs.total.free_in_bytes", - "node_stats.fs.total.available_in_bytes", - "node_stats.os.cgroup.cpuacct.control_group", - "node_stats.os.cgroup.cpuacct.usage_nanos", - "node_stats.os.cgroup.cpu.control_group", - "node_stats.os.cgroup.cpu.cfs_period_micros", - "node_stats.os.cgroup.cpu.cfs_quota_micros", - "node_stats.os.cgroup.cpu.stat.number_of_elapsed_periods", - "node_stats.os.cgroup.cpu.stat.number_of_times_throttled", - "node_stats.os.cgroup.cpu.stat.time_throttled_nanos", - "node_stats.os.cgroup.memory.control_group", - "node_stats.os.cgroup.memory.limit_in_bytes", - "node_stats.os.cgroup.memory.usage_in_bytes", - "node_stats.os.cpu.load_average.1m", - "node_stats.os.cpu.load_average.5m", - "node_stats.os.cpu.load_average.15m", - "node_stats.process.cpu.percent", - "node_stats.process.max_file_descriptors", - "node_stats.process.open_file_descriptors", - "node_stats.jvm.mem.heap_max_in_bytes", - "node_stats.jvm.mem.heap_used_in_bytes", - "node_stats.jvm.mem.heap_used_percent", - "node_stats.jvm.gc.collectors.young", - "node_stats.jvm.gc.collectors.young.collection_count", - "node_stats.jvm.gc.collectors.young.collection_time_in_millis", - "node_stats.jvm.gc.collectors.old", - "node_stats.jvm.gc.collectors.old.collection_count", - "node_stats.jvm.gc.collectors.old.collection_time_in_millis", - /* - * We whitelist both bulk and write in case the user is running in a mixed-version cluster or has the display name - * on the write thread pool set to "bulk". - */ - "node_stats.thread_pool.bulk.threads", - "node_stats.thread_pool.bulk.queue", - "node_stats.thread_pool.bulk.rejected", - "node_stats.thread_pool.write.threads", - "node_stats.thread_pool.write.queue", - "node_stats.thread_pool.write.rejected", - "node_stats.thread_pool.generic.threads", - "node_stats.thread_pool.generic.queue", - "node_stats.thread_pool.generic.rejected", - "node_stats.thread_pool.get.threads", - "node_stats.thread_pool.get.queue", - "node_stats.thread_pool.get.rejected", - "node_stats.thread_pool.management.threads", - "node_stats.thread_pool.management.queue", - "node_stats.thread_pool.management.rejected", - "node_stats.thread_pool.search.threads", - "node_stats.thread_pool.search.queue", - "node_stats.thread_pool.search.rejected", - "node_stats.thread_pool.watcher.threads", - "node_stats.thread_pool.watcher.queue", - "node_stats.thread_pool.watcher.rejected"); + public static final Set XCONTENT_FILTERS = Sets.newHashSet( + "node_stats.node_id", + "node_stats.node_master", + "node_stats.mlockall", + "node_stats.indices.docs.count", + "node_stats.indices.fielddata.memory_size_in_bytes", + "node_stats.indices.fielddata.evictions", + "node_stats.indices.store.size_in_bytes", + "node_stats.indices.indexing.throttle_time_in_millis", + "node_stats.indices.indexing.index_total", + "node_stats.indices.indexing.index_time_in_millis", + "node_stats.indices.query_cache.memory_size_in_bytes", + "node_stats.indices.query_cache.evictions", + "node_stats.indices.query_cache.hit_count", + "node_stats.indices.query_cache.miss_count", + "node_stats.indices.request_cache.memory_size_in_bytes", + "node_stats.indices.request_cache.evictions", + "node_stats.indices.request_cache.hit_count", + "node_stats.indices.request_cache.miss_count", + "node_stats.indices.search.query_total", + "node_stats.indices.search.query_time_in_millis", + "node_stats.indices.segments.count", + "node_stats.indices.segments.memory_in_bytes", + "node_stats.indices.segments.terms_memory_in_bytes", + "node_stats.indices.segments.stored_fields_memory_in_bytes", + "node_stats.indices.segments.term_vectors_memory_in_bytes", + "node_stats.indices.segments.norms_memory_in_bytes", + "node_stats.indices.segments.points_memory_in_bytes", + "node_stats.indices.segments.doc_values_memory_in_bytes", + "node_stats.indices.segments.index_writer_memory_in_bytes", + "node_stats.indices.segments.version_map_memory_in_bytes", + "node_stats.indices.segments.fixed_bit_set_memory_in_bytes", + "node_stats.indices.bulk.total_operations", + "node_stats.indices.bulk.total_time_in_millis", + "node_stats.indices.bulk.total_size_in_bytes", + "node_stats.indices.bulk.avg_time_in_millis", + "node_stats.indices.bulk.avg_size_in_bytes", + "node_stats.fs.io_stats.total.operations", + "node_stats.fs.io_stats.total.read_operations", + "node_stats.fs.io_stats.total.write_operations", + "node_stats.fs.io_stats.total.read_kilobytes", + "node_stats.fs.io_stats.total.write_kilobytes", + "node_stats.fs.total.total_in_bytes", + "node_stats.fs.total.free_in_bytes", + "node_stats.fs.total.available_in_bytes", + "node_stats.os.cgroup.cpuacct.control_group", + "node_stats.os.cgroup.cpuacct.usage_nanos", + "node_stats.os.cgroup.cpu.control_group", + "node_stats.os.cgroup.cpu.cfs_period_micros", + "node_stats.os.cgroup.cpu.cfs_quota_micros", + "node_stats.os.cgroup.cpu.stat.number_of_elapsed_periods", + "node_stats.os.cgroup.cpu.stat.number_of_times_throttled", + "node_stats.os.cgroup.cpu.stat.time_throttled_nanos", + "node_stats.os.cgroup.memory.control_group", + "node_stats.os.cgroup.memory.limit_in_bytes", + "node_stats.os.cgroup.memory.usage_in_bytes", + "node_stats.os.cpu.load_average.1m", + "node_stats.os.cpu.load_average.5m", + "node_stats.os.cpu.load_average.15m", + "node_stats.process.cpu.percent", + "node_stats.process.max_file_descriptors", + "node_stats.process.open_file_descriptors", + "node_stats.jvm.mem.heap_max_in_bytes", + "node_stats.jvm.mem.heap_used_in_bytes", + "node_stats.jvm.mem.heap_used_percent", + "node_stats.jvm.gc.collectors.young", + "node_stats.jvm.gc.collectors.young.collection_count", + "node_stats.jvm.gc.collectors.young.collection_time_in_millis", + "node_stats.jvm.gc.collectors.old", + "node_stats.jvm.gc.collectors.old.collection_count", + "node_stats.jvm.gc.collectors.old.collection_time_in_millis", + /* + * We whitelist both bulk and write in case the user is running in a mixed-version cluster or has the display name + * on the write thread pool set to "bulk". + */ + "node_stats.thread_pool.bulk.threads", + "node_stats.thread_pool.bulk.queue", + "node_stats.thread_pool.bulk.rejected", + "node_stats.thread_pool.write.threads", + "node_stats.thread_pool.write.queue", + "node_stats.thread_pool.write.rejected", + "node_stats.thread_pool.generic.threads", + "node_stats.thread_pool.generic.queue", + "node_stats.thread_pool.generic.rejected", + "node_stats.thread_pool.get.threads", + "node_stats.thread_pool.get.queue", + "node_stats.thread_pool.get.rejected", + "node_stats.thread_pool.management.threads", + "node_stats.thread_pool.management.queue", + "node_stats.thread_pool.management.rejected", + "node_stats.thread_pool.search.threads", + "node_stats.thread_pool.search.queue", + "node_stats.thread_pool.search.rejected", + "node_stats.thread_pool.watcher.threads", + "node_stats.thread_pool.watcher.queue", + "node_stats.thread_pool.watcher.rejected" + ); } diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/shards/ShardMonitoringDoc.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/shards/ShardMonitoringDoc.java index 7c8d18ffe9d1f..125b248b84a7f 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/shards/ShardMonitoringDoc.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/shards/ShardMonitoringDoc.java @@ -27,12 +27,14 @@ public class ShardMonitoringDoc extends FilteredMonitoringDoc { private final ShardRouting shardRouting; private final String clusterStateUUID; - ShardMonitoringDoc(final String cluster, - final long timestamp, - final long interval, - final MonitoringDoc.Node node, - final ShardRouting shardRouting, - final String clusterStateUUID) { + ShardMonitoringDoc( + final String cluster, + final long timestamp, + final long interval, + final MonitoringDoc.Node node, + final ShardRouting shardRouting, + final String clusterStateUUID + ) { super(cluster, timestamp, interval, node, MonitoredSystem.ES, TYPE, id(clusterStateUUID, shardRouting), XCONTENT_FILTERS); this.shardRouting = Objects.requireNonNull(shardRouting); @@ -84,12 +86,13 @@ public static String id(String stateUUID, ShardRouting shardRouting) { return builder.toString(); } - public static final Set XCONTENT_FILTERS = - Sets.newHashSet("state_uuid", - "shard.state", - "shard.primary", - "shard.node", - "shard.relocating_node", - "shard.shard", - "shard.index"); + public static final Set XCONTENT_FILTERS = Sets.newHashSet( + "state_uuid", + "shard.state", + "shard.primary", + "shard.node", + "shard.relocating_node", + "shard.shard", + "shard.index" + ); } diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/shards/ShardsCollector.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/shards/ShardsCollector.java index b2a27f26a9fe1..dde8fb7353a5d 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/shards/ShardsCollector.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/shards/ShardsCollector.java @@ -30,8 +30,7 @@ */ public class ShardsCollector extends Collector { - public ShardsCollector(final ClusterService clusterService, - final XPackLicenseState licenseState) { + public ShardsCollector(final ClusterService clusterService, final XPackLicenseState licenseState) { super(ShardMonitoringDoc.TYPE, clusterService, null, licenseState); } @@ -41,9 +40,8 @@ protected boolean shouldCollect(final boolean isElectedMaster) { } @Override - protected Collection doCollect(final MonitoringDoc.Node node, - final long interval, - final ClusterState clusterState) throws Exception { + protected Collection doCollect(final MonitoringDoc.Node node, final long interval, final ClusterState clusterState) + throws Exception { final List results = new ArrayList<>(1); if (clusterState != null) { RoutingTable routingTable = clusterState.routingTable(); diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/BytesReferenceMonitoringDoc.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/BytesReferenceMonitoringDoc.java index 16dc44e3c3552..c4a43bd9eb208 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/BytesReferenceMonitoringDoc.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/BytesReferenceMonitoringDoc.java @@ -6,8 +6,8 @@ */ package org.elasticsearch.xpack.monitoring.exporter; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; @@ -26,15 +26,17 @@ public class BytesReferenceMonitoringDoc extends MonitoringDoc { private final XContentType xContentType; private final BytesReference source; - public BytesReferenceMonitoringDoc(final String cluster, - final long timestamp, - final long intervalMillis, - @Nullable final Node node, - final MonitoredSystem system, - final String type, - @Nullable final String id, - final XContentType xContentType, - final BytesReference source) { + public BytesReferenceMonitoringDoc( + final String cluster, + final long timestamp, + final long intervalMillis, + @Nullable final Node node, + final MonitoredSystem system, + final String type, + @Nullable final String id, + final XContentType xContentType, + final BytesReference source + ) { super(cluster, timestamp, intervalMillis, node, system, type, id); this.xContentType = Objects.requireNonNull(xContentType); this.source = Objects.requireNonNull(source); diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/ClusterAlertsUtil.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/ClusterAlertsUtil.java index 682962b63c38f..3173af96cfd7d 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/ClusterAlertsUtil.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/ClusterAlertsUtil.java @@ -35,28 +35,24 @@ public class ClusterAlertsUtil { /** * Replace the ${monitoring.watch.cluster_uuid} field in the watches. */ - private static final Pattern CLUSTER_UUID_PROPERTY = - Pattern.compile(Pattern.quote("${monitoring.watch.cluster_uuid}")); + private static final Pattern CLUSTER_UUID_PROPERTY = Pattern.compile(Pattern.quote("${monitoring.watch.cluster_uuid}")); /** * Replace the ${monitoring.watch.id} field in the watches. */ - private static final Pattern WATCH_ID_PROPERTY = - Pattern.compile(Pattern.quote("${monitoring.watch.id}")); + private static final Pattern WATCH_ID_PROPERTY = Pattern.compile(Pattern.quote("${monitoring.watch.id}")); /** * Replace the ${monitoring.watch.unique_id} field in the watches. * * @see #createUniqueWatchId(ClusterService, String) */ - private static final Pattern UNIQUE_WATCH_ID_PROPERTY = - Pattern.compile(Pattern.quote("${monitoring.watch.unique_id}")); + private static final Pattern UNIQUE_WATCH_ID_PROPERTY = Pattern.compile(Pattern.quote("${monitoring.watch.unique_id}")); /** * Replace the ${monitoring.watch.unique_id} field in the watches. * * @see #createUniqueWatchId(ClusterService, String) */ - private static final Pattern VERSION_CREATED_PROPERTY = - Pattern.compile(Pattern.quote("${monitoring.version_created}")); + private static final Pattern VERSION_CREATED_PROPERTY = Pattern.compile(Pattern.quote("${monitoring.version_created}")); /** * The last time that all watches were updated. For now, all watches have been updated in the same version and should all be replaced @@ -73,8 +69,7 @@ public class ClusterAlertsUtil { "kibana_version_mismatch", "logstash_version_mismatch", "xpack_license_expiration", - "elasticsearch_nodes", - }; + "elasticsearch_nodes", }; /** * Create a unique identifier for the watch and cluster. @@ -142,8 +137,8 @@ private static BytesReference loadResource(final String resource) throws IOExcep * @throws SettingsException if an unknown cluster alert ID exists in the blacklist. */ public static List getClusterAlertsBlacklist(final Exporter.Config config) { - final List blacklist = - CLUSTER_ALERTS_BLACKLIST_SETTING.getConcreteSettingForNamespace(config.name()).get(config.settings()); + final List blacklist = CLUSTER_ALERTS_BLACKLIST_SETTING.getConcreteSettingForNamespace(config.name()) + .get(config.settings()); // validate the blacklist only contains recognized IDs if (blacklist.isEmpty() == false) { @@ -152,8 +147,12 @@ public static List getClusterAlertsBlacklist(final Exporter.Config confi if (unknownIds.isEmpty() == false) { throw new SettingsException( - "[" + CLUSTER_ALERTS_BLACKLIST_SETTING.getConcreteSettingForNamespace(config.name()).getKey() + - "] contains unrecognized Cluster Alert IDs [" + String.join(", ", unknownIds) + "]"); + "[" + + CLUSTER_ALERTS_BLACKLIST_SETTING.getConcreteSettingForNamespace(config.name()).getKey() + + "] contains unrecognized Cluster Alert IDs [" + + String.join(", ", unknownIds) + + "]" + ); } } diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/ExportBulk.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/ExportBulk.java index 6afc6bb6bea14..4fb086efc8776 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/ExportBulk.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/ExportBulk.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.xpack.monitoring.exporter; -import java.util.concurrent.atomic.AtomicBoolean; import org.apache.lucene.util.SetOnce; import org.elasticsearch.action.ActionListener; import org.elasticsearch.common.util.concurrent.ThreadContext; @@ -16,6 +15,7 @@ import java.util.Collection; import java.util.List; import java.util.Objects; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.BiConsumer; /** @@ -118,14 +118,19 @@ protected void doFlush(ActionListener listener) { iteratingListener.onResponse(null); })); }; - IteratingActionListener iteratingActionListener = - new IteratingActionListener<>(newExceptionHandlingListener(exceptionRef, listener), bulkBiConsumer, bulks, - threadContext); + IteratingActionListener iteratingActionListener = new IteratingActionListener<>( + newExceptionHandlingListener(exceptionRef, listener), + bulkBiConsumer, + bulks, + threadContext + ); iteratingActionListener.run(); } - private static ActionListener newExceptionHandlingListener(SetOnce exceptionRef, - ActionListener listener) { + private static ActionListener newExceptionHandlingListener( + SetOnce exceptionRef, + ActionListener listener + ) { return ActionListener.wrap(r -> { if (exceptionRef.get() == null) { listener.onResponse(null); diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/Exporter.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/Exporter.java index 535013fe71751..d1556ed3333c0 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/Exporter.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/Exporter.java @@ -9,12 +9,12 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.action.ActionListener; import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Setting.Property; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsException; import org.elasticsearch.common.time.DateFormatter; +import org.elasticsearch.core.Nullable; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.xpack.monitoring.exporter.http.HttpExporter; @@ -31,85 +31,101 @@ public abstract class Exporter implements AutoCloseable { public static Setting.AffixSettingDependency TYPE_DEPENDENCY = () -> Exporter.TYPE_SETTING; - private static final Setting.AffixSetting ENABLED_SETTING = - Setting.affixKeySetting("xpack.monitoring.exporters.","enabled", - key -> Setting.boolSetting(key, true, Property.Dynamic, Property.NodeScope, Property.Deprecated), TYPE_DEPENDENCY); + private static final Setting.AffixSetting ENABLED_SETTING = Setting.affixKeySetting( + "xpack.monitoring.exporters.", + "enabled", + key -> Setting.boolSetting(key, true, Property.Dynamic, Property.NodeScope, Property.Deprecated), + TYPE_DEPENDENCY + ); public static final Setting.AffixSetting TYPE_SETTING = Setting.affixKeySetting( "xpack.monitoring.exporters.", "type", - key -> Setting.simpleString( - key, - new Setting.Validator() { - - @Override - public void validate(final String value) { - + key -> Setting.simpleString(key, new Setting.Validator() { + + @Override + public void validate(final String value) { + + } + + @Override + public void validate(final String value, final Map, Object> settings) { + switch (value) { + case "": + break; + case "http": + // if the type is http, then hosts must be set + final String namespace = TYPE_SETTING.getNamespace(TYPE_SETTING.getConcreteSetting(key)); + final Setting> hostsSetting = HttpExporter.HOST_SETTING.getConcreteSettingForNamespace(namespace); + @SuppressWarnings("unchecked") + final List hosts = (List) settings.get(hostsSetting); + if (hosts.isEmpty()) { + throw new SettingsException("host list for [" + hostsSetting.getKey() + "] is empty"); + } + break; + case "local": + break; + default: + throw new SettingsException( + "type [" + value + "] for key [" + key + "] is invalid, only [http] and [local] are allowed" + ); } - @Override - public void validate(final String value, final Map, Object> settings) { - switch (value) { - case "": - break; - case "http": - // if the type is http, then hosts must be set - final String namespace = TYPE_SETTING.getNamespace(TYPE_SETTING.getConcreteSetting(key)); - final Setting> hostsSetting = HttpExporter.HOST_SETTING.getConcreteSettingForNamespace(namespace); - @SuppressWarnings("unchecked") final List hosts = (List) settings.get(hostsSetting); - if (hosts.isEmpty()) { - throw new SettingsException("host list for [" + hostsSetting.getKey() + "] is empty"); - } - break; - case "local": - break; - default: - throw new SettingsException( - "type [" + value + "] for key [" + key + "] is invalid, only [http] and [local] are allowed"); - } + } - } + @Override + public Iterator> settings() { + final String namespace = Exporter.TYPE_SETTING.getNamespace(Exporter.TYPE_SETTING.getConcreteSetting(key)); + final List> settings = List.of(HttpExporter.HOST_SETTING.getConcreteSettingForNamespace(namespace)); + return settings.iterator(); + } - @Override - public Iterator> settings() { - final String namespace = - Exporter.TYPE_SETTING.getNamespace(Exporter.TYPE_SETTING.getConcreteSetting(key)); - final List> settings = List.of(HttpExporter.HOST_SETTING.getConcreteSettingForNamespace(namespace)); - return settings.iterator(); - } - - }, - Property.Dynamic, - Property.NodeScope, - Property.Deprecated)); + }, Property.Dynamic, Property.NodeScope, Property.Deprecated) + ); /** * Every {@code Exporter} allows users to explicitly disable cluster alerts. */ - public static final Setting.AffixSetting CLUSTER_ALERTS_MANAGEMENT_SETTING = - Setting.affixKeySetting("xpack.monitoring.exporters.", "cluster_alerts.management.enabled", - key -> Setting.boolSetting(key, false, Property.Dynamic, Property.NodeScope, Property.Deprecated), TYPE_DEPENDENCY); + public static final Setting.AffixSetting CLUSTER_ALERTS_MANAGEMENT_SETTING = Setting.affixKeySetting( + "xpack.monitoring.exporters.", + "cluster_alerts.management.enabled", + key -> Setting.boolSetting(key, false, Property.Dynamic, Property.NodeScope, Property.Deprecated), + TYPE_DEPENDENCY + ); /** * Every {@code Exporter} allows users to explicitly disable specific cluster alerts. *

    * When cluster alerts management is enabled, this should delete anything blacklisted here in addition to not creating it. */ - public static final Setting.AffixSetting> CLUSTER_ALERTS_BLACKLIST_SETTING = Setting - .affixKeySetting("xpack.monitoring.exporters.", "cluster_alerts.management.blacklist", - key -> Setting.listSetting(key, Collections.emptyList(), Function.identity(), Property.Dynamic, Property.NodeScope, - Property.Deprecated), TYPE_DEPENDENCY); + public static final Setting.AffixSetting> CLUSTER_ALERTS_BLACKLIST_SETTING = Setting.affixKeySetting( + "xpack.monitoring.exporters.", + "cluster_alerts.management.blacklist", + key -> Setting.listSetting( + key, + Collections.emptyList(), + Function.identity(), + Property.Dynamic, + Property.NodeScope, + Property.Deprecated + ), + TYPE_DEPENDENCY + ); /** * Every {@code Exporter} allows users to use a different index time format. */ - static final Setting.AffixSetting INDEX_NAME_TIME_FORMAT_SETTING = - Setting.affixKeySetting("xpack.monitoring.exporters.","index.name.time_format", - key -> new Setting( - key, - Exporter.INDEX_FORMAT, - DateFormatter::forPattern, - Property.Dynamic, - Property.NodeScope, - Property.Deprecated), TYPE_DEPENDENCY); + static final Setting.AffixSetting INDEX_NAME_TIME_FORMAT_SETTING = Setting.affixKeySetting( + "xpack.monitoring.exporters.", + "index.name.time_format", + key -> new Setting( + key, + Exporter.INDEX_FORMAT, + DateFormatter::forPattern, + Property.Dynamic, + Property.NodeScope, + Property.Deprecated + ), + TYPE_DEPENDENCY + ); private static final String INDEX_FORMAT = "yyyy.MM.dd"; @@ -172,8 +188,13 @@ protected static DateFormatter dateTimeFormatter(final Config config) { } public static List> getSettings() { - return Arrays.asList(CLUSTER_ALERTS_MANAGEMENT_SETTING, TYPE_SETTING, ENABLED_SETTING, - INDEX_NAME_TIME_FORMAT_SETTING, CLUSTER_ALERTS_BLACKLIST_SETTING); + return Arrays.asList( + CLUSTER_ALERTS_MANAGEMENT_SETTING, + TYPE_SETTING, + ENABLED_SETTING, + INDEX_NAME_TIME_FORMAT_SETTING, + CLUSTER_ALERTS_BLACKLIST_SETTING + ); } public static class Config { @@ -185,8 +206,7 @@ public static class Config { private final ClusterService clusterService; private final XPackLicenseState licenseState; - public Config(String name, String type, Settings settings, - ClusterService clusterService, XPackLicenseState licenseState) { + public Config(String name, String type, Settings settings, ClusterService clusterService, XPackLicenseState licenseState) { this.name = name; this.type = type; this.settings = settings; diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/Exporters.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/Exporters.java index 6b9764cefedf8..a5baf255939b6 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/Exporters.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/Exporters.java @@ -23,10 +23,10 @@ import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.gateway.GatewayService; import org.elasticsearch.license.XPackLicenseState; +import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc; import org.elasticsearch.xpack.core.ssl.SSLService; import org.elasticsearch.xpack.monitoring.Monitoring; import org.elasticsearch.xpack.monitoring.exporter.http.HttpExporter; -import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc; import org.elasticsearch.xpack.monitoring.exporter.local.LocalExporter; import java.util.ArrayList; @@ -53,9 +53,14 @@ public class Exporters extends AbstractLifecycleComponent { private final XPackLicenseState licenseState; private final ThreadContext threadContext; - public Exporters(Settings settings, Map factories, - ClusterService clusterService, XPackLicenseState licenseState, - ThreadContext threadContext, SSLService sslService) { + public Exporters( + Settings settings, + Map factories, + ClusterService clusterService, + XPackLicenseState licenseState, + ThreadContext threadContext, + SSLService sslService + ) { this.settings = settings; this.factories = factories; this.exporters = new AtomicReference<>(emptyMap()); @@ -64,8 +69,9 @@ public Exporters(Settings settings, Map factories, this.clusterService = Objects.requireNonNull(clusterService); this.licenseState = Objects.requireNonNull(licenseState); - final List> dynamicSettings = - getSettings().stream().filter(Setting::isDynamic).collect(Collectors.toList()); + final List> dynamicSettings = getSettings().stream() + .filter(Setting::isDynamic) + .collect(Collectors.toList()); final List> updateSettings = new ArrayList>(dynamicSettings); updateSettings.add(Monitoring.MIGRATION_DECOMMISSION_ALERTS); clusterService.getClusterSettings().addSettingsUpdateConsumer(this::setExportersSetting, updateSettings); @@ -108,8 +114,7 @@ protected void doStop() { } @Override - protected void doClose() { - } + protected void doClose() {} public Exporter getExporter(String name) { return exporters.get().get(name); @@ -186,8 +191,9 @@ InitializedExporters initExporters(Settings settings) { // this is a singleton exporter, let's make sure we didn't already create one // (there can only be one instance of a singleton exporter) if (singletons.contains(type)) { - throw new SettingsException("multiple [" + type + "] exporters are configured. there can " + - "only be one [" + type + "] exporter configured"); + throw new SettingsException( + "multiple [" + type + "] exporters are configured. there can " + "only be one [" + type + "] exporter configured" + ); } singletons.add(type); } @@ -196,12 +202,17 @@ InitializedExporters initExporters(Settings settings) { // no exporters are configured, lets create a default local one. // - // NOTE: if there are exporters configured and they're all disabled, we don't - // fallback on the default + // NOTE: if there are exporters configured and they're all disabled, we don't + // fallback on the default // if (exporters.isEmpty() && hasDisabled == false) { - Exporter.Config config = - new Exporter.Config("default_" + LocalExporter.TYPE, LocalExporter.TYPE, settings, clusterService, licenseState); + Exporter.Config config = new Exporter.Config( + "default_" + LocalExporter.TYPE, + LocalExporter.TYPE, + settings, + clusterService, + licenseState + ); exporters.put(config.name(), factories.get(LocalExporter.TYPE).create(config)); } @@ -217,9 +228,9 @@ void wrapExportBulk(final ActionListener listener) { final ClusterState state = clusterService.state(); // wait until we have a usable cluster state - if (state.blocks().hasGlobalBlock(GatewayService.STATE_NOT_RECOVERED_BLOCK) || - ClusterState.UNKNOWN_UUID.equals(state.metadata().clusterUUID()) || - state.version() == ClusterState.UNKNOWN_VERSION) { + if (state.blocks().hasGlobalBlock(GatewayService.STATE_NOT_RECOVERED_BLOCK) + || ClusterState.UNKNOWN_UUID.equals(state.metadata().clusterUUID()) + || state.version() == ClusterState.UNKNOWN_VERSION) { logger.trace("skipping exporters because the cluster state is not loaded"); listener.onResponse(null); @@ -243,7 +254,8 @@ void wrapExportBulk(final ActionListener listener) { // get every exporter's ExportBulk and, when they've all responded, respond with a wrapped version for (final Exporter exporter : exporterMap.values()) { exporter.openBulk( - new AccumulatingExportBulkActionListener(exporter.name(), i++, accumulatedBulks, countDown, threadContext, listener)); + new AccumulatingExportBulkActionListener(exporter.name(), i++, accumulatedBulks, countDown, threadContext, listener) + ); } } @@ -320,10 +332,14 @@ static class AccumulatingExportBulkActionListener implements ActionListener delegate; private final ThreadContext threadContext; - AccumulatingExportBulkActionListener(final String name, - final int indexPosition, final AtomicArray accumulatedBulks, - final CountDown countDown, - final ThreadContext threadContext, final ActionListener delegate) { + AccumulatingExportBulkActionListener( + final String name, + final int indexPosition, + final AtomicArray accumulatedBulks, + final CountDown countDown, + final ThreadContext threadContext, + final ActionListener delegate + ) { this.name = name; this.indexPosition = indexPosition; this.accumulatedBulks = accumulatedBulks; diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/FilteredMonitoringDoc.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/FilteredMonitoringDoc.java index 45861de3a3a02..3f821746345a4 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/FilteredMonitoringDoc.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/FilteredMonitoringDoc.java @@ -6,10 +6,10 @@ */ package org.elasticsearch.xpack.monitoring.exporter; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.util.set.Sets; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.XContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -35,14 +35,16 @@ public abstract class FilteredMonitoringDoc extends MonitoringDoc { private final Set filters; - public FilteredMonitoringDoc(final String cluster, - final long timestamp, - final long intervalMillis, - @Nullable final Node node, - final MonitoredSystem system, - final String type, - @Nullable final String id, - final Set xContentFilters) { + public FilteredMonitoringDoc( + final String cluster, + final long timestamp, + final long intervalMillis, + @Nullable final Node node, + final MonitoredSystem system, + final String type, + @Nullable final String id, + final Set xContentFilters + ) { super(cluster, timestamp, intervalMillis, node, system, type, id); if (xContentFilters.isEmpty()) { throw new IllegalArgumentException("xContentFilters must not be empty"); @@ -62,8 +64,10 @@ public final XContentBuilder toXContent(XContentBuilder builder, Params params) try (XContentBuilder filteredBuilder = new XContentBuilder(builder.contentType(), out, filters)) { super.toXContent(filteredBuilder, params); } - try (InputStream stream = out.bytes().streamInput(); - XContentParser parser = xContent.createParser(EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) { + try ( + InputStream stream = out.bytes().streamInput(); + XContentParser parser = xContent.createParser(EMPTY, LoggingDeprecationHandler.INSTANCE, stream) + ) { return builder.copyCurrentStructure(parser); } } diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/ClusterAlertHttpResource.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/ClusterAlertHttpResource.java index e524eeb7e344c..03a35f5a4458f 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/ClusterAlertHttpResource.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/ClusterAlertHttpResource.java @@ -41,8 +41,10 @@ public class ClusterAlertHttpResource extends PublishableHttpResource { /** * Use this to retrieve the version of Cluster Alert in the Watch's JSON response from a request. */ - public static final Map CLUSTER_ALERT_VERSION_PARAMETERS = - Collections.singletonMap("filter_path", "metadata.xpack.version_created"); + public static final Map CLUSTER_ALERT_VERSION_PARAMETERS = Collections.singletonMap( + "filter_path", + "metadata.xpack.version_created" + ); /** * License State is used to determine if we should even be add or delete our watches. @@ -66,10 +68,12 @@ public class ClusterAlertHttpResource extends PublishableHttpResource { * @param watchId The name of the watch, which is lazily loaded. * @param watch The watch provider. {@code null} indicates that we should always delete this Watch. */ - public ClusterAlertHttpResource(final String resourceOwnerName, - final XPackLicenseState licenseState, - final Supplier watchId, - @Nullable final Supplier watch) { + public ClusterAlertHttpResource( + final String resourceOwnerName, + final XPackLicenseState licenseState, + final Supplier watchId, + @Nullable final Supplier watch + ) { // Watcher does not support master_timeout super(resourceOwnerName, null, CLUSTER_ALERT_VERSION_PARAMETERS); @@ -85,19 +89,38 @@ public ClusterAlertHttpResource(final String resourceOwnerName, protected void doCheck(final RestClient client, final ActionListener listener) { // if we should be adding, then we need to check for existence if (isWatchDefined() && Monitoring.MONITORING_CLUSTER_ALERTS_FEATURE.check(licenseState)) { - final CheckedFunction watchChecker = - (response) -> shouldReplaceClusterAlert(response, XContentType.JSON.xContent(), LAST_UPDATED_VERSION); - - checkForResource(client, listener, logger, - "/_watcher/watch", watchId.get(), "monitoring cluster alert", - resourceOwnerName, "monitoring cluster", - GET_EXISTS, GET_DOES_NOT_EXIST, - watchChecker, this::alwaysReplaceResource); + final CheckedFunction watchChecker = (response) -> shouldReplaceClusterAlert( + response, + XContentType.JSON.xContent(), + LAST_UPDATED_VERSION + ); + + checkForResource( + client, + listener, + logger, + "/_watcher/watch", + watchId.get(), + "monitoring cluster alert", + resourceOwnerName, + "monitoring cluster", + GET_EXISTS, + GET_DOES_NOT_EXIST, + watchChecker, + this::alwaysReplaceResource + ); } else { // if we should be deleting, then just try to delete it (same level of effort as checking) - deleteResource(client, listener, logger, "/_watcher/watch", watchId.get(), - "monitoring cluster alert", - resourceOwnerName, "monitoring cluster"); + deleteResource( + client, + listener, + logger, + "/_watcher/watch", + watchId.get(), + "monitoring cluster alert", + resourceOwnerName, + "monitoring cluster" + ); } } @@ -106,9 +129,18 @@ protected void doCheck(final RestClient client, final ActionListener li */ @Override protected void doPublish(final RestClient client, final ActionListener listener) { - putResource(client, listener, logger, - "/_watcher/watch", watchId.get(), Collections.emptyMap(), this::watchToHttpEntity, "monitoring cluster alert", - resourceOwnerName, "monitoring cluster"); + putResource( + client, + listener, + logger, + "/_watcher/watch", + watchId.get(), + Collections.emptyMap(), + this::watchToHttpEntity, + "monitoring cluster alert", + resourceOwnerName, + "monitoring cluster" + ); } /** diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExportBulk.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExportBulk.java index 383ad61dd21fd..0b223249dcaaf 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExportBulk.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExportBulk.java @@ -69,8 +69,13 @@ class HttpExportBulk extends ExportBulk { */ private long payloadLength = -1L; - HttpExportBulk(final String name, final RestClient client, final Map parameters, - final DateFormatter dateTimeFormatter, final ThreadContext threadContext) { + HttpExportBulk( + final String name, + final RestClient client, + final Map parameters, + final DateFormatter dateTimeFormatter, + final ThreadContext threadContext + ) { super(name, threadContext); this.client = client; @@ -111,8 +116,13 @@ public void doFlush(ActionListener listener) throws ExportException { try { // Don't use a thread-local decompressing stream since the HTTP client does not give strong guarantees about // thread-affinity when reading and closing the request entity - request.setEntity(new InputStreamEntity( - DeflateCompressor.inputStream(payload.streamInput(), false), payloadLength, ContentType.APPLICATION_JSON)); + request.setEntity( + new InputStreamEntity( + DeflateCompressor.inputStream(payload.streamInput(), false), + payloadLength, + ContentType.APPLICATION_JSON + ) + ); } catch (IOException e) { listener.onFailure(e); return; @@ -176,10 +186,7 @@ private void writeDocument(MonitoringDoc doc, OutputStream out) throws IOExcepti // Adds final bulk separator out.write(xContent.streamSeparator()); - logger.trace( - "http exporter [{}] - added index request [index={}, id={}, monitoring data type={}]", - name, index, id, doc.getType() - ); + logger.trace("http exporter [{}] - added index request [index={}, id={}, monitoring data type={}]", name, index, id, doc.getType()); } // Counting input stream used to record the uncompressed size of the bulk payload when writing it to a compressed stream @@ -195,10 +202,12 @@ public void write(final int b) throws IOException { out.write(b); count(1); } + @Override public void write(final byte[] b) throws IOException { write(b, 0, b.length); } + @Override public void write(final byte[] b, final int off, final int len) throws IOException { out.write(b, off, len); diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExportBulkResponseListener.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExportBulkResponseListener.java index c351d1c2ea440..b03fe685f1c1b 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExportBulkResponseListener.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExportBulkResponseListener.java @@ -53,8 +53,13 @@ class HttpExportBulkResponseListener implements ResponseListener { @Override public void onSuccess(final Response response) { // EMPTY is safe here because we never call namedObject - try (XContentParser parser = xContent - .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, response.getEntity().getContent())) { + try ( + XContentParser parser = xContent.createParser( + NamedXContentRegistry.EMPTY, + LoggingDeprecationHandler.INSTANCE, + response.getEntity().getContent() + ) + ) { // avoid parsing the entire payload if we don't need too XContentParser.Token token = parser.nextToken(); diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporter.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporter.java index 610fcc9917bac..4c5bd0efd7a8c 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporter.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporter.java @@ -62,6 +62,7 @@ import java.util.function.Function; import java.util.function.Supplier; import java.util.stream.Collectors; + import javax.net.ssl.SSLContext; import static java.util.Map.entry; @@ -102,222 +103,203 @@ public void validate(final String key, final Object value, final Object dependen /** * A string array representing the Elasticsearch node(s) to communicate with over HTTP(S). */ - public static final Setting.AffixSetting> HOST_SETTING = - Setting.affixKeySetting( - "xpack.monitoring.exporters.", - "host", - key -> Setting.listSetting( - key, - Collections.emptyList(), - Function.identity(), - new Setting.Validator<>() { - - @Override - public void validate(final List value) { - - } - - @Override - public void validate(final List hosts, final Map, Object> settings) { - final String namespace = - HttpExporter.HOST_SETTING.getNamespace(HttpExporter.HOST_SETTING.getConcreteSetting(key)); - final String type = (String) settings.get(Exporter.TYPE_SETTING.getConcreteSettingForNamespace(namespace)); - - if (hosts.isEmpty()) { - final String defaultType = - Exporter.TYPE_SETTING.getConcreteSettingForNamespace(namespace).get(Settings.EMPTY); - if (Objects.equals(type, defaultType)) { - // hosts can only be empty if the type is unset - return; - } else { - throw new SettingsException("host list for [" + key + "] is empty but type is [" + type + "]"); - } - } - - boolean httpHostFound = false; - boolean httpsHostFound = false; - - // every host must be configured - for (final String host : hosts) { - final HttpHost httpHost; - - try { - httpHost = HttpHostBuilder.builder(host).build(); - } catch (final IllegalArgumentException e) { - throw new SettingsException("[" + key + "] invalid host: [" + host + "]", e); - } - - if (TYPE.equals(httpHost.getSchemeName())) { - httpHostFound = true; - } else { - httpsHostFound = true; - } - - // fail if we find them configuring the scheme/protocol in different ways - if (httpHostFound && httpsHostFound) { - throw new SettingsException("[" + key + "] must use a consistent scheme: http or https"); - } - } - } - - @Override - public Iterator> settings() { - final String namespace = - HttpExporter.HOST_SETTING.getNamespace(HttpExporter.HOST_SETTING.getConcreteSetting(key)); - final List> settings = List.of(Exporter.TYPE_SETTING.getConcreteSettingForNamespace(namespace)); - return settings.iterator(); - } - - }, - Property.Dynamic, - Property.NodeScope, - Property.Deprecated), - HTTP_TYPE_DEPENDENCY); + public static final Setting.AffixSetting> HOST_SETTING = Setting.affixKeySetting( + "xpack.monitoring.exporters.", + "host", + key -> Setting.listSetting(key, Collections.emptyList(), Function.identity(), new Setting.Validator<>() { + + @Override + public void validate(final List value) { + + } + + @Override + public void validate(final List hosts, final Map, Object> settings) { + final String namespace = HttpExporter.HOST_SETTING.getNamespace(HttpExporter.HOST_SETTING.getConcreteSetting(key)); + final String type = (String) settings.get(Exporter.TYPE_SETTING.getConcreteSettingForNamespace(namespace)); + + if (hosts.isEmpty()) { + final String defaultType = Exporter.TYPE_SETTING.getConcreteSettingForNamespace(namespace).get(Settings.EMPTY); + if (Objects.equals(type, defaultType)) { + // hosts can only be empty if the type is unset + return; + } else { + throw new SettingsException("host list for [" + key + "] is empty but type is [" + type + "]"); + } + } + + boolean httpHostFound = false; + boolean httpsHostFound = false; + + // every host must be configured + for (final String host : hosts) { + final HttpHost httpHost; + + try { + httpHost = HttpHostBuilder.builder(host).build(); + } catch (final IllegalArgumentException e) { + throw new SettingsException("[" + key + "] invalid host: [" + host + "]", e); + } + + if (TYPE.equals(httpHost.getSchemeName())) { + httpHostFound = true; + } else { + httpsHostFound = true; + } + + // fail if we find them configuring the scheme/protocol in different ways + if (httpHostFound && httpsHostFound) { + throw new SettingsException("[" + key + "] must use a consistent scheme: http or https"); + } + } + } + + @Override + public Iterator> settings() { + final String namespace = HttpExporter.HOST_SETTING.getNamespace(HttpExporter.HOST_SETTING.getConcreteSetting(key)); + final List> settings = List.of(Exporter.TYPE_SETTING.getConcreteSettingForNamespace(namespace)); + return settings.iterator(); + } + + }, Property.Dynamic, Property.NodeScope, Property.Deprecated), + HTTP_TYPE_DEPENDENCY + ); /** * Master timeout associated with bulk requests. */ - public static final Setting.AffixSetting BULK_TIMEOUT_SETTING = - Setting.affixKeySetting("xpack.monitoring.exporters.","bulk.timeout", - (key) -> Setting.timeSetting(key, TimeValue.MINUS_ONE, Property.Dynamic, Property.NodeScope, Property.Deprecated), - HTTP_TYPE_DEPENDENCY); + public static final Setting.AffixSetting BULK_TIMEOUT_SETTING = Setting.affixKeySetting( + "xpack.monitoring.exporters.", + "bulk.timeout", + (key) -> Setting.timeSetting(key, TimeValue.MINUS_ONE, Property.Dynamic, Property.NodeScope, Property.Deprecated), + HTTP_TYPE_DEPENDENCY + ); /** * Timeout used for initiating a connection. */ - public static final Setting.AffixSetting CONNECTION_TIMEOUT_SETTING = - Setting.affixKeySetting( - "xpack.monitoring.exporters.", - "connection.timeout", - (key) -> Setting.timeSetting(key, TimeValue.timeValueSeconds(6), Property.Dynamic, Property.NodeScope, Property.Deprecated), - HTTP_TYPE_DEPENDENCY); + public static final Setting.AffixSetting CONNECTION_TIMEOUT_SETTING = Setting.affixKeySetting( + "xpack.monitoring.exporters.", + "connection.timeout", + (key) -> Setting.timeSetting(key, TimeValue.timeValueSeconds(6), Property.Dynamic, Property.NodeScope, Property.Deprecated), + HTTP_TYPE_DEPENDENCY + ); /** * Timeout used for reading from the connection. */ - public static final Setting.AffixSetting CONNECTION_READ_TIMEOUT_SETTING = - Setting.affixKeySetting( - "xpack.monitoring.exporters.", - "connection.read_timeout", - (key) -> Setting.timeSetting(key, TimeValue.timeValueSeconds(60), Property.Dynamic, Property.NodeScope, - Property.Deprecated), - HTTP_TYPE_DEPENDENCY); + public static final Setting.AffixSetting CONNECTION_READ_TIMEOUT_SETTING = Setting.affixKeySetting( + "xpack.monitoring.exporters.", + "connection.read_timeout", + (key) -> Setting.timeSetting(key, TimeValue.timeValueSeconds(60), Property.Dynamic, Property.NodeScope, Property.Deprecated), + HTTP_TYPE_DEPENDENCY + ); /** * Username for basic auth. */ - public static final Setting.AffixSetting AUTH_USERNAME_SETTING = - Setting.affixKeySetting("xpack.monitoring.exporters.","auth.username", - (key) -> Setting.simpleString( - key, - new Setting.Validator() { - @Override - public void validate(final String password) { - // no username validation that is independent of other settings - } - - @Override - public void validate(final String username, final Map, Object> settings) { - final String namespace = - HttpExporter.AUTH_USERNAME_SETTING.getNamespace( - HttpExporter.AUTH_USERNAME_SETTING.getConcreteSetting(key)); - - if (Strings.isNullOrEmpty(username) == false) { - final String type = - (String) settings.get(Exporter.TYPE_SETTING.getConcreteSettingForNamespace(namespace)); - if ("http".equals(type) == false) { - throw new SettingsException("username for [" + key + "] is set but type is [" + type + "]"); - } - } - } - - @Override - public Iterator> settings() { - final String namespace = - HttpExporter.AUTH_USERNAME_SETTING.getNamespace( - HttpExporter.AUTH_USERNAME_SETTING.getConcreteSetting(key)); - - final List> settings = List.of( - Exporter.TYPE_SETTING.getConcreteSettingForNamespace(namespace)); - return settings.iterator(); - } - - }, - Property.Dynamic, - Property.NodeScope, - Property.Filtered, - Property.Deprecated), - HTTP_TYPE_DEPENDENCY); + public static final Setting.AffixSetting AUTH_USERNAME_SETTING = Setting.affixKeySetting( + "xpack.monitoring.exporters.", + "auth.username", + (key) -> Setting.simpleString(key, new Setting.Validator() { + @Override + public void validate(final String password) { + // no username validation that is independent of other settings + } + + @Override + public void validate(final String username, final Map, Object> settings) { + final String namespace = HttpExporter.AUTH_USERNAME_SETTING.getNamespace( + HttpExporter.AUTH_USERNAME_SETTING.getConcreteSetting(key) + ); + + if (Strings.isNullOrEmpty(username) == false) { + final String type = (String) settings.get(Exporter.TYPE_SETTING.getConcreteSettingForNamespace(namespace)); + if ("http".equals(type) == false) { + throw new SettingsException("username for [" + key + "] is set but type is [" + type + "]"); + } + } + } + + @Override + public Iterator> settings() { + final String namespace = HttpExporter.AUTH_USERNAME_SETTING.getNamespace( + HttpExporter.AUTH_USERNAME_SETTING.getConcreteSetting(key) + ); + + final List> settings = List.of(Exporter.TYPE_SETTING.getConcreteSettingForNamespace(namespace)); + return settings.iterator(); + } + + }, Property.Dynamic, Property.NodeScope, Property.Filtered, Property.Deprecated), + HTTP_TYPE_DEPENDENCY + ); /** * Secure password for basic auth. */ - public static final Setting.AffixSetting AUTH_SECURE_PASSWORD_SETTING = - Setting.affixKeySetting( - "xpack.monitoring.exporters.", - "auth.secure_password", - key -> SecureSetting.secureString(key, null, Setting.Property.Deprecated), - HTTP_TYPE_DEPENDENCY); + public static final Setting.AffixSetting AUTH_SECURE_PASSWORD_SETTING = Setting.affixKeySetting( + "xpack.monitoring.exporters.", + "auth.secure_password", + key -> SecureSetting.secureString(key, null, Setting.Property.Deprecated), + HTTP_TYPE_DEPENDENCY + ); /** * The SSL settings. * * @see SSLService */ - public static final Setting.AffixSetting SSL_SETTING = - Setting.affixKeySetting( - "xpack.monitoring.exporters.", - "ssl", - (key) -> Setting.groupSetting(key + ".", Property.Dynamic, Property.NodeScope, Property.Filtered, Property.Deprecated), - HTTP_TYPE_DEPENDENCY); + public static final Setting.AffixSetting SSL_SETTING = Setting.affixKeySetting( + "xpack.monitoring.exporters.", + "ssl", + (key) -> Setting.groupSetting(key + ".", Property.Dynamic, Property.NodeScope, Property.Filtered, Property.Deprecated), + HTTP_TYPE_DEPENDENCY + ); /** * Proxy setting to allow users to send requests to a remote cluster that requires a proxy base path. */ - public static final Setting.AffixSetting PROXY_BASE_PATH_SETTING = - Setting.affixKeySetting("xpack.monitoring.exporters.","proxy.base_path", - (key) -> Setting.simpleString( - key, - value -> { - if (Strings.isNullOrEmpty(value) == false) { - try { - RestClientBuilder.cleanPathPrefix(value); - } catch (RuntimeException e) { - Setting concreteSetting = HttpExporter.PROXY_BASE_PATH_SETTING.getConcreteSetting(key); - throw new SettingsException("[" + concreteSetting.getKey() + "] is malformed [" + value + "]", e); - } - } - }, - Property.Dynamic, - Property.NodeScope, - Property.Deprecated), - HTTP_TYPE_DEPENDENCY); + public static final Setting.AffixSetting PROXY_BASE_PATH_SETTING = Setting.affixKeySetting( + "xpack.monitoring.exporters.", + "proxy.base_path", + (key) -> Setting.simpleString(key, value -> { + if (Strings.isNullOrEmpty(value) == false) { + try { + RestClientBuilder.cleanPathPrefix(value); + } catch (RuntimeException e) { + Setting concreteSetting = HttpExporter.PROXY_BASE_PATH_SETTING.getConcreteSetting(key); + throw new SettingsException("[" + concreteSetting.getKey() + "] is malformed [" + value + "]", e); + } + } + }, Property.Dynamic, Property.NodeScope, Property.Deprecated), + HTTP_TYPE_DEPENDENCY + ); /** * A boolean setting to enable or disable sniffing for extra connections. */ - public static final Setting.AffixSetting SNIFF_ENABLED_SETTING = - Setting.affixKeySetting("xpack.monitoring.exporters.","sniff.enabled", - (key) -> Setting.boolSetting(key, false, Property.Dynamic, Property.NodeScope, Property.Deprecated), - HTTP_TYPE_DEPENDENCY); + public static final Setting.AffixSetting SNIFF_ENABLED_SETTING = Setting.affixKeySetting( + "xpack.monitoring.exporters.", + "sniff.enabled", + (key) -> Setting.boolSetting(key, false, Property.Dynamic, Property.NodeScope, Property.Deprecated), + HTTP_TYPE_DEPENDENCY + ); /** * A parent setting to header key/value pairs, whose names are user defined. */ - public static final Setting.AffixSetting HEADERS_SETTING = - Setting.affixKeySetting("xpack.monitoring.exporters.","headers", - (key) -> Setting.groupSetting( - key + ".", - settings -> { - final Set names = settings.names(); - for (String name : names) { - final String fullSetting = key + "." + name; - if (HttpExporter.BLACKLISTED_HEADERS.contains(name)) { - throw new SettingsException("header cannot be overwritten via [" + fullSetting + "]"); - } - final List values = settings.getAsList(name); - if (values.isEmpty()) { - throw new SettingsException("headers must have values, missing for setting [" + fullSetting + "]"); - } - } - }, - Property.Dynamic, - Property.NodeScope, - Property.Deprecated), - HTTP_TYPE_DEPENDENCY); + public static final Setting.AffixSetting HEADERS_SETTING = Setting.affixKeySetting( + "xpack.monitoring.exporters.", + "headers", + (key) -> Setting.groupSetting(key + ".", settings -> { + final Set names = settings.names(); + for (String name : names) { + final String fullSetting = key + "." + name; + if (HttpExporter.BLACKLISTED_HEADERS.contains(name)) { + throw new SettingsException("header cannot be overwritten via [" + fullSetting + "]"); + } + final List values = settings.getAsList(name); + if (values.isEmpty()) { + throw new SettingsException("headers must have values, missing for setting [" + fullSetting + "]"); + } + } + }, Property.Dynamic, Property.NodeScope, Property.Deprecated), + HTTP_TYPE_DEPENDENCY + ); /** * Blacklist of headers that the user is not allowed to set. *

    @@ -327,10 +309,12 @@ public Iterator> settings() { /** * ES level timeout used when checking and writing templates (used to speed up tests) */ - public static final Setting.AffixSetting TEMPLATE_CHECK_TIMEOUT_SETTING = - Setting.affixKeySetting("xpack.monitoring.exporters.","index.template.master_timeout", - (key) -> Setting.timeSetting(key, TimeValue.MINUS_ONE, Property.Dynamic, Property.NodeScope, Property.Deprecated), - HTTP_TYPE_DEPENDENCY); + public static final Setting.AffixSetting TEMPLATE_CHECK_TIMEOUT_SETTING = Setting.affixKeySetting( + "xpack.monitoring.exporters.", + "index.template.master_timeout", + (key) -> Setting.timeSetting(key, TimeValue.MINUS_ONE, Property.Dynamic, Property.NodeScope, Property.Deprecated), + HTTP_TYPE_DEPENDENCY + ); /** * Minimum supported version of the remote monitoring cluster (same major). */ @@ -397,8 +381,12 @@ static class Resources { * @param migrationCoordinator The shared coordinator for determining monitoring migrations in progress * @throws SettingsException if any setting is malformed */ - public HttpExporter(final Config config, final SSLService sslService, final ThreadContext threadContext, - MonitoringMigrationCoordinator migrationCoordinator) { + public HttpExporter( + final Config config, + final SSLService sslService, + final ThreadContext threadContext, + MonitoringMigrationCoordinator migrationCoordinator + ) { this(config, sslService, threadContext, migrationCoordinator, new NodeFailureListener(), createResources(config)); } @@ -413,9 +401,14 @@ public HttpExporter(final Config config, final SSLService sslService, final Thre * @param resource Both the resource for all things required for bulk operations and those for just cluster alerts * @throws SettingsException if any setting is malformed */ - private HttpExporter(final Config config, final SSLService sslService, final ThreadContext threadContext, - final MonitoringMigrationCoordinator migrationCoordinator, final NodeFailureListener listener, - final Resources resource) { + private HttpExporter( + final Config config, + final SSLService sslService, + final ThreadContext threadContext, + final MonitoringMigrationCoordinator migrationCoordinator, + final NodeFailureListener listener, + final Resources resource + ) { this(config, sslService, threadContext, migrationCoordinator, listener, resource.allResources, resource.alertingResource); } @@ -431,11 +424,24 @@ private HttpExporter(final Config config, final SSLService sslService, final Thr * @param alertingResource The HTTP resource used to configure cluster alerts * @throws SettingsException if any setting is malformed */ - HttpExporter(final Config config, final SSLService sslService, final ThreadContext threadContext, - final MonitoringMigrationCoordinator migrationCoordinator, final NodeFailureListener listener, - final HttpResource resource, final HttpResource alertingResource) { - this(config, createRestClient(config, sslService, listener), threadContext, migrationCoordinator, listener, resource, - alertingResource); + HttpExporter( + final Config config, + final SSLService sslService, + final ThreadContext threadContext, + final MonitoringMigrationCoordinator migrationCoordinator, + final NodeFailureListener listener, + final HttpResource resource, + final HttpResource alertingResource + ) { + this( + config, + createRestClient(config, sslService, listener), + threadContext, + migrationCoordinator, + listener, + resource, + alertingResource + ); } /** @@ -450,11 +456,25 @@ private HttpExporter(final Config config, final SSLService sslService, final Thr * @param alertingResource The HTTP resource used to configure cluster alerts * @throws SettingsException if any setting is malformed */ - HttpExporter(final Config config, final RestClient client, final ThreadContext threadContext, - final MonitoringMigrationCoordinator migrationCoordinator, final NodeFailureListener listener, - final HttpResource resource, final HttpResource alertingResource) { - this(config, client, createSniffer(config, client, listener), threadContext, migrationCoordinator, listener, resource, - alertingResource); + HttpExporter( + final Config config, + final RestClient client, + final ThreadContext threadContext, + final MonitoringMigrationCoordinator migrationCoordinator, + final NodeFailureListener listener, + final HttpResource resource, + final HttpResource alertingResource + ) { + this( + config, + client, + createSniffer(config, client, listener), + threadContext, + migrationCoordinator, + listener, + resource, + alertingResource + ); } /** @@ -470,9 +490,16 @@ private HttpExporter(final Config config, final SSLService sslService, final Thr * @param alertingResource The HTTP resource used to configure cluster alerts * @throws SettingsException if any setting is malformed */ - HttpExporter(final Config config, final RestClient client, @Nullable final Sniffer sniffer, final ThreadContext threadContext, - final MonitoringMigrationCoordinator migrationCoordinator, final NodeFailureListener listener, - final HttpResource resource, final HttpResource alertingResource) { + HttpExporter( + final Config config, + final RestClient client, + @Nullable final Sniffer sniffer, + final ThreadContext threadContext, + final MonitoringMigrationCoordinator migrationCoordinator, + final NodeFailureListener listener, + final HttpResource resource, + final HttpResource alertingResource + ) { super(config); this.client = Objects.requireNonNull(client); @@ -487,7 +514,7 @@ private HttpExporter(final Config config, final SSLService sslService, final Thr // mark resources as dirty after any node failure or license change listener.setResource(resource); - //for a mixed cluster upgrade, ensure that if master changes and this is the master, allow the resources to re-publish + // for a mixed cluster upgrade, ensure that if master changes and this is the master, allow the resources to re-publish onLocalMasterListener = clusterChangedEvent -> { if (clusterChangedEvent.nodesDelta().masterNodeChanged() && clusterChangedEvent.localNodeMaster()) { resource.markDirty(); @@ -503,14 +530,12 @@ private HttpExporter(final Config config, final SSLService sslService, final Thr * (see {@link #configureSecurity(RestClientBuilder, Config, SSLService)} if this exporter has been configured with secure settings */ public static void registerSettingValidators(ClusterService clusterService, SSLService sslService) { - clusterService.getClusterSettings().addAffixUpdateConsumer(SSL_SETTING, - (ignoreKey, ignoreSettings) -> { + clusterService.getClusterSettings().addAffixUpdateConsumer(SSL_SETTING, (ignoreKey, ignoreSettings) -> { // no-op update. We only care about the validator - }, - (key, settings) -> { - validateSslSettings(key, settings); - configureSslStrategy(settings, null, sslService); - }); + }, (key, settings) -> { + validateSslSettings(key, settings); + configureSslStrategy(settings, null, sslService); + }); } /** @@ -527,8 +552,13 @@ private static void validateSslSettings(String exporter, Settings settings) { .map(Setting::getKey) .collect(Collectors.toList()); if (secureSettings.isEmpty() == false) { - throw new IllegalStateException("Cannot dynamically update SSL settings for the exporter [" + exporter - + "] as it depends on the secure setting(s) [" + Strings.collectionToCommaDelimitedString(secureSettings) + "]"); + throw new IllegalStateException( + "Cannot dynamically update SSL settings for the exporter [" + + exporter + + "] as it depends on the secure setting(s) [" + + Strings.collectionToCommaDelimitedString(secureSettings) + + "]" + ); } } @@ -582,10 +612,14 @@ static Sniffer createSniffer(final Config config, final RestClient client, final if (sniffingEnabled) { final List hosts = HOST_SETTING.getConcreteSettingForNamespace(config.name()).get(config.settings()); // createHosts(config) ensures that all schemes are the same for all hosts! - final ElasticsearchNodesSniffer.Scheme scheme = hosts.get(0).startsWith("https") ? - ElasticsearchNodesSniffer.Scheme.HTTPS : ElasticsearchNodesSniffer.Scheme.HTTP; - final ElasticsearchNodesSniffer hostsSniffer = - new ElasticsearchNodesSniffer(client, ElasticsearchNodesSniffer.DEFAULT_SNIFF_REQUEST_TIMEOUT, scheme); + final ElasticsearchNodesSniffer.Scheme scheme = hosts.get(0).startsWith("https") + ? ElasticsearchNodesSniffer.Scheme.HTTPS + : ElasticsearchNodesSniffer.Scheme.HTTP; + final ElasticsearchNodesSniffer hostsSniffer = new ElasticsearchNodesSniffer( + client, + ElasticsearchNodesSniffer.DEFAULT_SNIFF_REQUEST_TIMEOUT, + scheme + ); sniffer = Sniffer.builder(client).setNodesSniffer(hostsSniffer).build(); @@ -669,8 +703,8 @@ private static void configureHeaders(final RestClientBuilder builder, final Conf final List values = headerSettings.getAsList(name); // add each value as a separate header; they literally appear like: // - // Warning: abc - // Warning: xyz + // Warning: abc + // Warning: xyz for (final String value : values) { headers.add(new BasicHeader(name, value)); } @@ -696,8 +730,10 @@ private static void configureSecurity(final RestClientBuilder builder, final Con List hostList = HOST_SETTING.getConcreteSettingForNamespace(config.name()).get(config.settings()); // sending credentials in plaintext! if (credentialsProvider != null && hostList.stream().findFirst().orElse("").startsWith("https") == false) { - logger.warn("exporter [{}] is not using https, but using user authentication with plaintext " + - "username/password!", config.name()); + logger.warn( + "exporter [{}] is not using https, but using user authentication with plaintext " + "username/password!", + config.name() + ); } if (sslStrategy != null) { @@ -713,8 +749,11 @@ private static void configureSecurity(final RestClientBuilder builder, final Con * @param sslService The SSL Service used to create the SSL Context necessary for TLS / SSL communication * @return Appropriately configured instance of {@link SSLIOSessionStrategy} */ - private static SSLIOSessionStrategy configureSslStrategy(final Settings sslSettings, final Setting concreteSetting, - final SSLService sslService) { + private static SSLIOSessionStrategy configureSslStrategy( + final Settings sslSettings, + final Setting concreteSetting, + final SSLService sslService + ) { final SSLIOSessionStrategy sslStrategy; if (SSLConfigurationSettings.withoutPrefix(true).getSecureSettingsInUse(sslSettings).isEmpty()) { // This configuration does not use secure settings, so it is possible that is has been dynamically updated. @@ -737,16 +776,14 @@ private static SSLIOSessionStrategy configureSslStrategy(final Settings sslSetti * @param config The exporter's configuration */ private static void configureTimeouts(final RestClientBuilder builder, final Config config) { - final TimeValue connectTimeout = - CONNECTION_TIMEOUT_SETTING.getConcreteSettingForNamespace(config.name()).get(config.settings()); - final TimeValue socketTimeout = - CONNECTION_READ_TIMEOUT_SETTING.getConcreteSettingForNamespace(config.name()).get(config.settings()); + final TimeValue connectTimeout = CONNECTION_TIMEOUT_SETTING.getConcreteSettingForNamespace(config.name()).get(config.settings()); + final TimeValue socketTimeout = CONNECTION_READ_TIMEOUT_SETTING.getConcreteSettingForNamespace(config.name()) + .get(config.settings()); // if the values could ever be null, then we should only set it if they're not null builder.setRequestConfigCallback(new TimeoutRequestConfigCallback(connectTimeout, socketTimeout)); } - /** * Caches secure settings for use when dynamically configuring HTTP exporters * @param settings settings used for configuring HTTP exporter @@ -820,11 +857,13 @@ static Map createDefaultParams(final Config config) { * @param resourceOwnerName The resource owner name to display for any logging messages. * @param resources The resources to add too. */ - private static void configureTemplateResources(final Config config, - final String resourceOwnerName, - final List resources) { - final TimeValue templateTimeout = - TEMPLATE_CHECK_TIMEOUT_SETTING.getConcreteSettingForNamespace(config.name()).get(config.settings()); + private static void configureTemplateResources( + final Config config, + final String resourceOwnerName, + final List resources + ) { + final TimeValue templateTimeout = TEMPLATE_CHECK_TIMEOUT_SETTING.getConcreteSettingForNamespace(config.name()) + .get(config.settings()); // add templates not managed by resolvers for (final String templateName : MonitoringTemplateRegistry.TEMPLATE_NAMES) { @@ -856,37 +895,37 @@ private static HttpResource configureClusterAlertsResources(final Config config, } // wrap the watches in a conditional resource check to ensure the remote cluster has watcher available / enabled - return new WatcherExistsHttpResource(resourceOwnerName, clusterService, - new MultiHttpResource(resourceOwnerName, watchResources)); + return new WatcherExistsHttpResource( + resourceOwnerName, + clusterService, + new MultiHttpResource(resourceOwnerName, watchResources) + ); } return null; } @Override public void removeAlerts(Consumer listener) { - alertingResource.checkAndPublish(client, ActionListener.wrap( - (result) -> { - ExporterResourceStatus status; - if (result.isSuccess()) { - status = ExporterResourceStatus.ready(name(), TYPE); - } else { - switch (result.getResourceState()) { - case CLEAN: - status = ExporterResourceStatus.ready(name(), TYPE); - break; - case CHECKING: - case DIRTY: - // CHECKING should be unlikely, but in case of that, we mark it as not ready - status = ExporterResourceStatus.notReady(name(), TYPE, result.getReason()); - break; - default: - throw new ElasticsearchException("Illegal exporter resource status state [{}]", result.getResourceState()); - } + alertingResource.checkAndPublish(client, ActionListener.wrap((result) -> { + ExporterResourceStatus status; + if (result.isSuccess()) { + status = ExporterResourceStatus.ready(name(), TYPE); + } else { + switch (result.getResourceState()) { + case CLEAN: + status = ExporterResourceStatus.ready(name(), TYPE); + break; + case CHECKING: + case DIRTY: + // CHECKING should be unlikely, but in case of that, we mark it as not ready + status = ExporterResourceStatus.notReady(name(), TYPE, result.getReason()); + break; + default: + throw new ElasticsearchException("Illegal exporter resource status state [{}]", result.getResourceState()); } - listener.accept(status); - }, - (exception) -> listener.accept(ExporterResourceStatus.notReady(name(), TYPE, exception)) - )); + } + listener.accept(status); + }, (exception) -> listener.accept(ExporterResourceStatus.notReady(name(), TYPE, exception)))); } @Override @@ -935,9 +974,18 @@ public void doClose() { } public static List> getDynamicSettings() { - return Arrays.asList(HOST_SETTING, AUTH_USERNAME_SETTING, BULK_TIMEOUT_SETTING, - CONNECTION_READ_TIMEOUT_SETTING, CONNECTION_TIMEOUT_SETTING, PROXY_BASE_PATH_SETTING, - SNIFF_ENABLED_SETTING, TEMPLATE_CHECK_TIMEOUT_SETTING, SSL_SETTING, HEADERS_SETTING); + return Arrays.asList( + HOST_SETTING, + AUTH_USERNAME_SETTING, + BULK_TIMEOUT_SETTING, + CONNECTION_READ_TIMEOUT_SETTING, + CONNECTION_TIMEOUT_SETTING, + PROXY_BASE_PATH_SETTING, + SNIFF_ENABLED_SETTING, + TEMPLATE_CHECK_TIMEOUT_SETTING, + SSL_SETTING, + HEADERS_SETTING + ); } public static List> getSecureSettings() { diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpHostBuilder.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpHostBuilder.java index b7b9ee546e2dc..fb36dc557e1c7 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpHostBuilder.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpHostBuilder.java @@ -155,8 +155,12 @@ public static HttpHostBuilder builder(final String uri) { // fail for proxies if (parsedUri.getRawPath() != null && parsedUri.getRawPath().isEmpty() == false) { throw new IllegalArgumentException( - "HttpHosts do not use paths [" + parsedUri.getRawPath() + - "]. see setRequestConfigCallback for proxies. value: [" + uri + "]"); + "HttpHosts do not use paths [" + + parsedUri.getRawPath() + + "]. see setRequestConfigCallback for proxies. value: [" + + uri + + "]" + ); } } catch (URISyntaxException | IndexOutOfBoundsException | NullPointerException e) { throw new IllegalArgumentException("error parsing host: [" + uri + "]", e); diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpResource.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpResource.java index 05f384b23db54..69e396dc589e6 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpResource.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpResource.java @@ -103,9 +103,7 @@ public boolean equals(Object o) { return false; } ResourcePublishResult that = (ResourcePublishResult) o; - return success == that.success && - Objects.equals(reason, that.reason) && - resourceState == that.resourceState; + return success == that.success && Objects.equals(reason, that.reason) && resourceState == that.resourceState; } @Override @@ -115,11 +113,15 @@ public int hashCode() { @Override public String toString() { - return "ResourcePublishResult{" + - "success=" + success + - ", reason='" + reason + '\'' + - ", resourceState=" + resourceState + - '}'; + return "ResourcePublishResult{" + + "success=" + + success + + ", reason='" + + reason + + '\'' + + ", resourceState=" + + resourceState + + '}'; } } diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/MultiHttpResource.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/MultiHttpResource.java index 49c7e0a8ca34d..1d7d77c5ba492 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/MultiHttpResource.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/MultiHttpResource.java @@ -6,13 +6,13 @@ */ package org.elasticsearch.xpack.monitoring.exporter.http; -import java.util.Iterator; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.action.ActionListener; import org.elasticsearch.client.RestClient; import java.util.Collections; +import java.util.Iterator; import java.util.List; /** diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/PublishableHttpResource.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/PublishableHttpResource.java index eed5c90b18499..9c27d4b7b445d 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/PublishableHttpResource.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/PublishableHttpResource.java @@ -16,13 +16,13 @@ import org.elasticsearch.client.ResponseException; import org.elasticsearch.client.ResponseListener; import org.elasticsearch.client.RestClient; +import org.elasticsearch.common.util.set.Sets; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.core.CheckedFunction; import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.common.util.set.Sets; -import org.elasticsearch.xcontent.XContent; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.xcontent.XContent; import java.io.IOException; import java.util.Collections; @@ -56,8 +56,10 @@ public abstract class PublishableHttpResource extends HttpResource { /** * Use this to retrieve the version of template and pipeline resources in their JSON response from a request. */ - public static final Map RESOURCE_VERSION_PARAMETERS = - Collections.singletonMap("filter_path", FILTER_PATH_RESOURCE_VERSION); + public static final Map RESOURCE_VERSION_PARAMETERS = Collections.singletonMap( + "filter_path", + FILTER_PATH_RESOURCE_VERSION + ); /** * The default set of acceptable exists response codes for GET requests. @@ -80,8 +82,11 @@ public abstract class PublishableHttpResource extends HttpResource { * @param masterTimeout Master timeout to use with any request. * @param baseParameters The base parameters to specify for the request. */ - protected PublishableHttpResource(final String resourceOwnerName, @Nullable final TimeValue masterTimeout, - final Map baseParameters) { + protected PublishableHttpResource( + final String resourceOwnerName, + @Nullable final TimeValue masterTimeout, + final Map baseParameters + ) { this(resourceOwnerName, masterTimeout, baseParameters, true); } @@ -93,8 +98,12 @@ protected PublishableHttpResource(final String resourceOwnerName, @Nullable fina * @param baseParameters The base parameters to specify for the request. * @param dirty Whether the resource is dirty or not */ - protected PublishableHttpResource(final String resourceOwnerName, @Nullable final TimeValue masterTimeout, - final Map baseParameters, final boolean dirty) { + protected PublishableHttpResource( + final String resourceOwnerName, + @Nullable final TimeValue masterTimeout, + final Map baseParameters, + final boolean dirty + ) { super(resourceOwnerName, dirty); if (masterTimeout != null && TimeValue.MINUS_ONE.equals(masterTimeout) == false) { @@ -171,22 +180,39 @@ protected final void doCheckAndPublish(final RestClient client, final ActionList * @param xContent The XContent used to parse the response. * @param minimumVersion The minimum version allowed without being replaced (expected to be the last updated version). */ - protected void versionCheckForResource(final RestClient client, - final ActionListener listener, - final Logger logger, - final String resourceBasePath, - final String resourceName, - final String resourceType, - final String resourceOwnerName, - final String resourceOwnerType, - final XContent xContent, - final int minimumVersion) { - final CheckedFunction responseChecker = - (response) -> shouldReplaceResource(response, xContent, resourceName, minimumVersion); - - checkForResource(client, listener, logger, - resourceBasePath, resourceName, resourceType, resourceOwnerName, resourceOwnerType, - GET_EXISTS, GET_DOES_NOT_EXIST, responseChecker, this::alwaysReplaceResource); + protected void versionCheckForResource( + final RestClient client, + final ActionListener listener, + final Logger logger, + final String resourceBasePath, + final String resourceName, + final String resourceType, + final String resourceOwnerName, + final String resourceOwnerType, + final XContent xContent, + final int minimumVersion + ) { + final CheckedFunction responseChecker = (response) -> shouldReplaceResource( + response, + xContent, + resourceName, + minimumVersion + ); + + checkForResource( + client, + listener, + logger, + resourceBasePath, + resourceName, + resourceType, + resourceOwnerName, + resourceOwnerType, + GET_EXISTS, + GET_DOES_NOT_EXIST, + responseChecker, + this::alwaysReplaceResource + ); } /** @@ -207,18 +233,20 @@ protected void versionCheckForResource(final RestClient client, * @param responseChecker Returns {@code true} if the resource should be replaced. * @param doesNotExistResponseChecker Returns {@code true} if the resource should be replaced. */ - protected void checkForResource(final RestClient client, - final ActionListener listener, - final Logger logger, - final String resourceBasePath, - final String resourceName, - final String resourceType, - final String resourceOwnerName, - final String resourceOwnerType, - final Set exists, - final Set doesNotExist, - final CheckedFunction responseChecker, - final CheckedFunction doesNotExistResponseChecker) { + protected void checkForResource( + final RestClient client, + final ActionListener listener, + final Logger logger, + final String resourceBasePath, + final String resourceName, + final String resourceType, + final String resourceOwnerName, + final String resourceOwnerType, + final Set exists, + final Set doesNotExist, + final CheckedFunction responseChecker, + final CheckedFunction doesNotExistResponseChecker + ) { logger.trace("checking if {} [{}] exists on the [{}] {}", resourceType, resourceName, resourceOwnerName, resourceOwnerType); final Request request = new Request("GET", resourceBasePath + "/" + resourceName); @@ -243,8 +271,13 @@ public void onSuccess(final Response response) { listener.onResponse(false == responseChecker.apply(response)); } else if (doesNotExist.contains(statusCode)) { - logger.debug("{} [{}] does not exist on the [{}] {}", - resourceType, resourceName, resourceOwnerName, resourceOwnerType); + logger.debug( + "{} [{}] does not exist on the [{}] {}", + resourceType, + resourceName, + resourceOwnerName, + resourceOwnerType + ); // if we should replace it -- true -- then the resource "does not exist" as far as the caller is concerned listener.onResponse(false == doesNotExistResponseChecker.apply(response)); @@ -252,9 +285,15 @@ public void onSuccess(final Response response) { onFailure(new ResponseException(response)); } } catch (Exception e) { - logger.error((Supplier) () -> new ParameterizedMessage("failed to parse [{}/{}] on the [{}]", - resourceBasePath, resourceName, resourceOwnerName), - e); + logger.error( + (Supplier) () -> new ParameterizedMessage( + "failed to parse [{}/{}] on the [{}]", + resourceBasePath, + resourceName, + resourceOwnerName + ), + e + ); onFailure(e); } @@ -263,18 +302,31 @@ public void onSuccess(final Response response) { @Override public void onFailure(final Exception exception) { if (exception instanceof ResponseException) { - final Response response = ((ResponseException)exception).getResponse(); + final Response response = ((ResponseException) exception).getResponse(); final int statusCode = response.getStatusLine().getStatusCode(); - logger.error((Supplier) () -> - new ParameterizedMessage("failed to verify {} [{}] on the [{}] {} with status code [{}]", - resourceType, resourceName, resourceOwnerName, resourceOwnerType, statusCode), - exception); + logger.error( + (Supplier) () -> new ParameterizedMessage( + "failed to verify {} [{}] on the [{}] {} with status code [{}]", + resourceType, + resourceName, + resourceOwnerName, + resourceOwnerType, + statusCode + ), + exception + ); } else { - logger.error((Supplier) () -> - new ParameterizedMessage("failed to verify {} [{}] on the [{}] {}", - resourceType, resourceName, resourceOwnerName, resourceOwnerType), - exception); + logger.error( + (Supplier) () -> new ParameterizedMessage( + "failed to verify {} [{}] on the [{}] {}", + resourceType, + resourceName, + resourceOwnerName, + resourceOwnerType + ), + exception + ); } listener.onFailure(exception); @@ -307,19 +359,20 @@ public void onFailure(final Exception exception) { * @param resourceOwnerName The user-recognizeable resource owner. * @param resourceOwnerType The type of resource owner being dealt with (e.g., "monitoring cluster"). */ - protected void putResource(final RestClient client, - final ActionListener listener, - final Logger logger, - final String resourceBasePath, - final String resourceName, - final Map parameters, - final java.util.function.Supplier body, - final String resourceType, - final String resourceOwnerName, - final String resourceOwnerType) { + protected void putResource( + final RestClient client, + final ActionListener listener, + final Logger logger, + final String resourceBasePath, + final String resourceName, + final Map parameters, + final java.util.function.Supplier body, + final String resourceType, + final String resourceOwnerName, + final String resourceOwnerType + ) { logger.trace("uploading {} [{}] to the [{}] {}", resourceType, resourceName, resourceOwnerName, resourceOwnerType); - final Request request = new Request("PUT", resourceBasePath + "/" + resourceName); addDefaultParameters(request); addParameters(request, parameters); @@ -343,10 +396,16 @@ public void onSuccess(final Response response) { @Override public void onFailure(final Exception exception) { - logger.error((Supplier) () -> - new ParameterizedMessage("failed to upload {} [{}] on the [{}] {}", - resourceType, resourceName, resourceOwnerName, resourceOwnerType), - exception); + logger.error( + (Supplier) () -> new ParameterizedMessage( + "failed to upload {} [{}] on the [{}] {}", + resourceType, + resourceName, + resourceOwnerName, + resourceOwnerType + ), + exception + ); listener.onFailure(exception); } @@ -370,14 +429,16 @@ public void onFailure(final Exception exception) { * @param resourceOwnerName The user-recognizeable resource owner. * @param resourceOwnerType The type of resource owner being dealt with (e.g., "monitoring cluster"). */ - protected void deleteResource(final RestClient client, - final ActionListener listener, - final Logger logger, - final String resourceBasePath, - final String resourceName, - final String resourceType, - final String resourceOwnerName, - final String resourceOwnerType) { + protected void deleteResource( + final RestClient client, + final ActionListener listener, + final Logger logger, + final String resourceBasePath, + final String resourceName, + final String resourceType, + final String resourceOwnerName, + final String resourceOwnerType + ) { logger.trace("deleting {} [{}] from the [{}] {}", resourceType, resourceName, resourceOwnerName, resourceOwnerType); final Request request = new Request("DELETE", resourceBasePath + "/" + resourceName); @@ -406,10 +467,16 @@ public void onSuccess(Response response) { @Override public void onFailure(Exception exception) { - logger.error((Supplier) () -> - new ParameterizedMessage("failed to delete {} [{}] on the [{}] {}", - resourceType, resourceName, resourceOwnerName, resourceOwnerType), - exception); + logger.error( + (Supplier) () -> new ParameterizedMessage( + "failed to delete {} [{}] on the [{}] {}", + resourceType, + resourceName, + resourceOwnerName, + resourceOwnerType + ), + exception + ); listener.onFailure(exception); } @@ -437,9 +504,12 @@ public void onFailure(Exception exception) { * @throws IOException if any issue occurs while parsing the {@code xContent} {@code response}. * @throws RuntimeException if the response format is changed. */ - protected boolean shouldReplaceResource(final Response response, final XContent xContent, - final String resourceName, final int minimumVersion) - throws IOException { + protected boolean shouldReplaceResource( + final Response response, + final XContent xContent, + final String resourceName, + final int minimumVersion + ) throws IOException { // no named content used; so EMPTY is fine final Map resources = XContentHelper.convertToMap(xContent, response.getEntity().getContent(), false); diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/SecurityHttpClientConfigCallback.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/SecurityHttpClientConfigCallback.java index 17863470909c3..eacbc872f1aca 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/SecurityHttpClientConfigCallback.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/SecurityHttpClientConfigCallback.java @@ -13,9 +13,10 @@ import org.elasticsearch.client.RestClientBuilder; import org.elasticsearch.core.Nullable; +import java.util.Objects; + import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLContext; -import java.util.Objects; /** * {@code SecurityHttpClientConfigCallback} configures a {@link RestClient} for user authentication and SSL / TLS. @@ -39,8 +40,7 @@ class SecurityHttpClientConfigCallback implements RestClientBuilder.HttpClientCo * @param sslStrategy The SSL strategy, if SSL / TLS have been supplied * @throws NullPointerException if {@code sslStrategy} is {@code null} */ - SecurityHttpClientConfigCallback(final SSLIOSessionStrategy sslStrategy, - @Nullable final CredentialsProvider credentialsProvider) { + SecurityHttpClientConfigCallback(final SSLIOSessionStrategy sslStrategy, @Nullable final CredentialsProvider credentialsProvider) { this.sslStrategy = Objects.requireNonNull(sslStrategy); this.credentialsProvider = credentialsProvider; } diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/TemplateHttpResource.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/TemplateHttpResource.java index 32e7346c6f9a1..fe73da485475c 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/TemplateHttpResource.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/TemplateHttpResource.java @@ -64,10 +64,18 @@ public TemplateHttpResource(final String resourceOwnerName, @Nullable final Time */ @Override protected void doCheck(final RestClient client, final ActionListener listener) { - versionCheckForResource(client, listener, logger, - "/_template", templateName, "monitoring template", - resourceOwnerName, "monitoring cluster", - XContentType.JSON.xContent(), MonitoringTemplateUtils.LAST_UPDATED_VERSION); + versionCheckForResource( + client, + listener, + logger, + "/_template", + templateName, + "monitoring template", + resourceOwnerName, + "monitoring cluster", + XContentType.JSON.xContent(), + MonitoringTemplateUtils.LAST_UPDATED_VERSION + ); } /** @@ -76,7 +84,13 @@ protected void doCheck(final RestClient client, final ActionListener li */ @Override protected void doPublish(final RestClient client, final ActionListener listener) { - listener.onResponse(ResourcePublishResult.notReady("waiting for remote monitoring cluster to install appropriate template " + - "[" + templateName + "] (version mismatch or missing)")); + listener.onResponse( + ResourcePublishResult.notReady( + "waiting for remote monitoring cluster to install appropriate template " + + "[" + + templateName + + "] (version mismatch or missing)" + ) + ); } } diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/TimeoutRequestConfigCallback.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/TimeoutRequestConfigCallback.java index c0e63c41eeb1a..62e7203ac5ab0 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/TimeoutRequestConfigCallback.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/TimeoutRequestConfigCallback.java @@ -63,10 +63,10 @@ TimeValue getSocketTimeout() { @Override public Builder customizeRequestConfig(Builder requestConfigBuilder) { if (connectTimeout != null) { - requestConfigBuilder.setConnectTimeout((int)connectTimeout.millis()); + requestConfigBuilder.setConnectTimeout((int) connectTimeout.millis()); } if (socketTimeout != null) { - requestConfigBuilder.setSocketTimeout((int)socketTimeout.millis()); + requestConfigBuilder.setSocketTimeout((int) socketTimeout.millis()); } return requestConfigBuilder; diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/VersionHttpResource.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/VersionHttpResource.java index 253d826a01917..79b43c4bc53fb 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/VersionHttpResource.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/VersionHttpResource.java @@ -72,10 +72,14 @@ public void onSuccess(final Response response) { @Override public void onFailure(final Exception exception) { - logger.error((Supplier) () -> - new ParameterizedMessage("failed to verify minimum version [{}] on the [{}] monitoring cluster", - minimumVersion, resourceOwnerName), - exception); + logger.error( + (Supplier) () -> new ParameterizedMessage( + "failed to verify minimum version [{}] on the [{}] monitoring cluster", + minimumVersion, + resourceOwnerName + ), + exception + ); listener.onFailure(exception); } @@ -97,19 +101,16 @@ private ResourcePublishResult validateVersion(final Response response) throws IO // the response should be filtered to just '{"version":{"number":"xyz"}}', so this is cheap and guaranteed @SuppressWarnings("unchecked") final String versionNumber = (String) ((Map) map.get("version")).get("number"); - final Version version = Version.fromString( - versionNumber - .replace("-SNAPSHOT", "") - .replaceFirst("-(alpha\\d+|beta\\d+|rc\\d+)", "") - ); + final Version version = Version.fromString(versionNumber.replace("-SNAPSHOT", "").replaceFirst("-(alpha\\d+|beta\\d+|rc\\d+)", "")); if (version.onOrAfter(minimumVersion)) { logger.debug("version [{}] >= [{}] and supported for [{}]", version, minimumVersion, resourceOwnerName); return ResourcePublishResult.ready(); } else { logger.error("version [{}] < [{}] and NOT supported for [{}]", version, minimumVersion, resourceOwnerName); - return ResourcePublishResult.notReady("version [" + version + "] < [" + minimumVersion + "] and NOT supported for [" - + resourceOwnerName + "]"); + return ResourcePublishResult.notReady( + "version [" + version + "] < [" + minimumVersion + "] and NOT supported for [" + resourceOwnerName + "]" + ); } } diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/WatcherExistsHttpResource.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/WatcherExistsHttpResource.java index ee4237ac6cbd8..4f9b86fb82250 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/WatcherExistsHttpResource.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/http/WatcherExistsHttpResource.java @@ -12,12 +12,12 @@ import org.elasticsearch.client.Response; import org.elasticsearch.client.RestClient; import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.core.CheckedFunction; import org.elasticsearch.common.util.set.Sets; -import org.elasticsearch.xcontent.XContent; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.core.CheckedFunction; import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.xcontent.XContent; +import org.elasticsearch.xcontent.XContentType; import java.io.IOException; import java.util.Collections; @@ -35,13 +35,17 @@ public class WatcherExistsHttpResource extends PublishableHttpResource { /** * Use this to avoid getting any JSON response from a request. */ - public static final Map WATCHER_CHECK_PARAMETERS = - Collections.singletonMap("filter_path", "features.watcher.available,features.watcher.enabled"); + public static final Map WATCHER_CHECK_PARAMETERS = Collections.singletonMap( + "filter_path", + "features.watcher.available,features.watcher.enabled" + ); /** * Valid response codes that note explicitly that {@code _xpack} does not exist. */ - public static final Set XPACK_DOES_NOT_EXIST = - Sets.newHashSet(RestStatus.NOT_FOUND.getStatus(), RestStatus.BAD_REQUEST.getStatus()); + public static final Set XPACK_DOES_NOT_EXIST = Sets.newHashSet( + RestStatus.NOT_FOUND.getStatus(), + RestStatus.BAD_REQUEST.getStatus() + ); /** * The cluster service allows this check to be limited to only handling elected master nodes @@ -99,16 +103,27 @@ protected void doCheck(final RestClient client, final ActionListener li * @param listener Returns {@code true} to skip cluster alert creation. {@code false} to check/create them. */ private void checkXPackForWatcher(final RestClient client, final ActionListener listener) { - final CheckedFunction responseChecker = - (response) -> canUseWatcher(response, XContentType.JSON.xContent()); + final CheckedFunction responseChecker = (response) -> canUseWatcher( + response, + XContentType.JSON.xContent() + ); // use DNE to pretend that we're all set; it means that Watcher is unusable final CheckedFunction doesNotExistChecker = (response) -> false; - checkForResource(client, listener, logger, - "", "_xpack", "watcher check", - resourceOwnerName, "monitoring cluster", - GET_EXISTS, Sets.newHashSet(RestStatus.NOT_FOUND.getStatus(), RestStatus.BAD_REQUEST.getStatus()), - responseChecker, doesNotExistChecker); + checkForResource( + client, + listener, + logger, + "", + "_xpack", + "watcher check", + resourceOwnerName, + "monitoring cluster", + GET_EXISTS, + Sets.newHashSet(RestStatus.NOT_FOUND.getStatus(), RestStatus.BAD_REQUEST.getStatus()), + responseChecker, + doesNotExistChecker + ); } /** diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalBulk.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalBulk.java index 888e99efa9d04..0a796c57faebf 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalBulk.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalBulk.java @@ -42,7 +42,6 @@ public class LocalBulk extends ExportBulk { private BulkRequestBuilder requestBuilder; - LocalBulk(String name, Logger logger, Client client, DateFormatter dateTimeFormatter) { super(name, client.threadPool().getThreadContext()); this.logger = logger; @@ -73,8 +72,14 @@ protected void doAdd(Collection docs) throws ExportException { requestBuilder.add(request); if (logger.isTraceEnabled()) { - logger.trace("local exporter [{}] - added index request [index={}, id={}, pipeline={}, monitoring data type={}]", - name, request.index(), request.id(), request.getPipeline(), doc.getType()); + logger.trace( + "local exporter [{}] - added index request [index={}, id={}, pipeline={}, monitoring data type={}]", + name, + request.index(), + request.id(), + request.getPipeline(), + doc.getType() + ); } } catch (Exception e) { if (exception == null) { @@ -96,15 +101,19 @@ protected void doFlush(ActionListener listener) { } else { try { logger.trace("exporter [{}] - exporting {} documents", name, requestBuilder.numberOfActions()); - executeAsyncWithOrigin(client.threadPool().getThreadContext(), MONITORING_ORIGIN, requestBuilder.request(), - ActionListener.wrap(bulkResponse -> { - if (bulkResponse.hasFailures()) { - throwExportException(bulkResponse.getItems(), listener); - } else { - listener.onResponse(null); - } - }, e -> listener.onFailure(new ExportException("failed to flush export bulk [{}]", e, name))), - client::bulk); + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + MONITORING_ORIGIN, + requestBuilder.request(), + ActionListener.wrap(bulkResponse -> { + if (bulkResponse.hasFailures()) { + throwExportException(bulkResponse.getItems(), listener); + } else { + listener.onResponse(null); + } + }, e -> listener.onFailure(new ExportException("failed to flush export bulk [{}]", e, name))), + client::bulk + ); } finally { requestBuilder = null; } @@ -115,9 +124,9 @@ void throwExportException(BulkItemResponse[] bulkItemResponses, ActionListener new ExportException(item.getFailure().getCause())) - .forEach(exception::addExportException); + .filter(BulkItemResponse::isFailed) + .map(item -> new ExportException(item.getFailure().getCause())) + .forEach(exception::addExportException); if (exception.hasExportExceptions()) { for (ExportException e : exception) { diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalExporter.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalExporter.java index 47dd4501b0216..463ffe606e410 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalExporter.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalExporter.java @@ -111,8 +111,12 @@ public class LocalExporter extends Exporter implements ClusterStateListener, Cle private long stateInitializedTime; - public LocalExporter(Exporter.Config config, Client client, MonitoringMigrationCoordinator migrationCoordinator, - CleanerService cleanerService) { + public LocalExporter( + Exporter.Config config, + Client client, + MonitoringMigrationCoordinator migrationCoordinator, + CleanerService cleanerService + ) { super(config); this.client = client; this.clusterService = config.clusterService(); @@ -298,8 +302,7 @@ private boolean setupIfNotElectedMaster(final ClusterState clusterState) { // any required template is not yet installed in the given cluster state, we'll wait. for (final String template : MonitoringTemplateRegistry.TEMPLATE_NAMES) { if (hasTemplate(clusterState, template) == false) { - logger.debug("monitoring index template [{}] does not exist, so service cannot start (waiting on master)", - template); + logger.debug("monitoring index template [{}] does not exist, so service cannot start (waiting on master)", template); return false; } } @@ -343,14 +346,18 @@ private boolean setupIfElectedMaster(final ClusterState clusterState, final bool // Check that each required template exists. We can install the other resources needed while we wait for them to appear, so // continue with the async installation and return the readiness at the end of the setup. final List missingTemplates = Arrays.stream(MonitoringTemplateRegistry.TEMPLATE_NAMES) - .filter(name -> hasTemplate(clusterState, name) == false) - .collect(Collectors.toList()); + .filter(name -> hasTemplate(clusterState, name) == false) + .collect(Collectors.toList()); boolean templatesInstalled = false; if (missingTemplates.isEmpty() == false) { // Check to see if the template installation is disabled. If it isn't, then we should say so in the log. - logger.debug((Supplier) () -> new ParameterizedMessage("monitoring index templates [{}] do not exist, so service " + - "cannot start (waiting on registered templates)", missingTemplates)); + logger.debug( + (Supplier) () -> new ParameterizedMessage( + "monitoring index templates [{}] do not exist, so service " + "cannot start (waiting on registered templates)", + missingTemplates + ) + ); } else { templatesInstalled = true; } @@ -378,8 +385,12 @@ private boolean setupIfElectedMaster(final ClusterState clusterState, final bool return templatesInstalled; } - private void setupClusterAlertsTasks(ClusterState clusterState, boolean clusterStateChange, List asyncActions, - AtomicInteger pendingResponses) { + private void setupClusterAlertsTasks( + ClusterState clusterState, + boolean clusterStateChange, + List asyncActions, + AtomicInteger pendingResponses + ) { boolean shouldSetUpWatcher = state.get() == State.RUNNING && clusterStateChange == false; if (canUseWatcher()) { if (shouldSetUpWatcher) { @@ -393,22 +404,33 @@ private void setupClusterAlertsTasks(ClusterState clusterState, boolean clusterS logger.trace("installing monitoring watches"); getClusterAlertsInstallationAsyncActions(indexExists, asyncActions, pendingResponses); } else { - logger.trace("skipping installing monitoring watches, watches=[{}], indexExists=[{}], watcherSetup=[{}]", - watches, indexExists, watcherSetup.get()); + logger.trace( + "skipping installing monitoring watches, watches=[{}], indexExists=[{}], watcherSetup=[{}]", + watches, + indexExists, + watcherSetup.get() + ); } } else { logger.trace("watches shouldn't be setup, because state=[{}] and clusterStateChange=[{}]", state.get(), clusterStateChange); } } else { - logger.trace("watches will not be installed because xpack.watcher.enabled=[{}] and " + - "xpack.monitoring.exporters._local.cluster_alerts.management.enabled=[{}]", + logger.trace( + "watches will not be installed because xpack.watcher.enabled=[{}] and " + + "xpack.monitoring.exporters._local.cluster_alerts.management.enabled=[{}]", XPackSettings.WATCHER_ENABLED.get(config.settings()), - CLUSTER_ALERTS_MANAGEMENT_SETTING.getConcreteSettingForNamespace(config.name()).get(config.settings())); + CLUSTER_ALERTS_MANAGEMENT_SETTING.getConcreteSettingForNamespace(config.name()).get(config.settings()) + ); } } - private void removeClusterAlertsTasks(ClusterState clusterState, Consumer setupListener, - List asyncActions, AtomicInteger pendingResponses, List errors) { + private void removeClusterAlertsTasks( + ClusterState clusterState, + Consumer setupListener, + List asyncActions, + AtomicInteger pendingResponses, + List errors + ) { if (canUseWatcher()) { if (state.get() != State.TERMINATED) { final IndexRoutingTable watches = clusterState.routingTable().index(Watch.INDEX); @@ -429,8 +451,12 @@ private void removeClusterAlertsTasks(ClusterState clusterState, Consumer= minimumVersion; + return version instanceof Number && ((Number) version).intValue() >= minimumVersion; } /** @@ -467,27 +493,41 @@ private boolean hasValidVersion(final Object version, final long minimumVersion) * @param asyncActions Asynchronous actions are added to for each Watch. * @param pendingResponses Pending response countdown we use to track completion. */ - private void getClusterAlertsInstallationAsyncActions(final boolean indexExists, final List asyncActions, - final AtomicInteger pendingResponses) { + private void getClusterAlertsInstallationAsyncActions( + final boolean indexExists, + final List asyncActions, + final AtomicInteger pendingResponses + ) { final boolean canAddWatches = Monitoring.MONITORING_CLUSTER_ALERTS_FEATURE.check(licenseState); for (final String watchId : ClusterAlertsUtil.WATCH_IDS) { final String uniqueWatchId = ClusterAlertsUtil.createUniqueWatchId(clusterService, watchId); - final boolean addWatch = canAddWatches && clusterAlertBlacklist.contains(watchId) == false && - decommissionClusterAlerts == false; + final boolean addWatch = canAddWatches + && clusterAlertBlacklist.contains(watchId) == false + && decommissionClusterAlerts == false; // we aren't sure if no watches exist yet, so add them if (indexExists) { if (addWatch) { logger.trace("checking monitoring watch [{}]", uniqueWatchId); - asyncActions.add(() -> client.execute(GetWatchAction.INSTANCE, new GetWatchRequest(uniqueWatchId), - new GetAndPutWatchResponseActionListener(client, watchId, uniqueWatchId, pendingResponses))); + asyncActions.add( + () -> client.execute( + GetWatchAction.INSTANCE, + new GetWatchRequest(uniqueWatchId), + new GetAndPutWatchResponseActionListener(client, watchId, uniqueWatchId, pendingResponses) + ) + ); } else { logger.trace("pruning monitoring watch [{}]", uniqueWatchId); - asyncActions.add(() -> client.execute(DeleteWatchAction.INSTANCE, new DeleteWatchRequest(uniqueWatchId), - new ResponseActionListener<>("watch", uniqueWatchId, pendingResponses))); + asyncActions.add( + () -> client.execute( + DeleteWatchAction.INSTANCE, + new DeleteWatchRequest(uniqueWatchId), + new ResponseActionListener<>("watch", uniqueWatchId, pendingResponses) + ) + ); } } else if (addWatch) { logger.trace("adding monitoring watch [{}]", uniqueWatchId); @@ -505,15 +545,24 @@ private void getClusterAlertsInstallationAsyncActions(final boolean indexExists, * @param setupListener The listener to call with the status of the watch if there are watches to remove. * @param errors A list to collect errors during the watch removal process. */ - private void addClusterAlertsRemovalAsyncActions(final boolean indexExists, final List asyncActions, - final AtomicInteger pendingResponses, - Consumer setupListener, final List errors) { + private void addClusterAlertsRemovalAsyncActions( + final boolean indexExists, + final List asyncActions, + final AtomicInteger pendingResponses, + Consumer setupListener, + final List errors + ) { for (final String watchId : ClusterAlertsUtil.WATCH_IDS) { final String uniqueWatchId = ClusterAlertsUtil.createUniqueWatchId(clusterService, watchId); if (indexExists) { logger.trace("pruning monitoring watch [{}]", uniqueWatchId); - asyncActions.add(() -> client.execute(DeleteWatchAction.INSTANCE, new DeleteWatchRequest(uniqueWatchId), - new ErrorCapturingResponseListener<>("watch", uniqueWatchId, pendingResponses, setupListener, errors, this.name()))); + asyncActions.add( + () -> client.execute( + DeleteWatchAction.INSTANCE, + new DeleteWatchRequest(uniqueWatchId), + new ErrorCapturingResponseListener<>("watch", uniqueWatchId, pendingResponses, setupListener, errors, this.name()) + ) + ); } } } @@ -523,9 +572,13 @@ private void putWatch(final Client client, final String watchId, final String un logger.trace("adding monitoring watch [{}]", uniqueWatchId); - executeAsyncWithOrigin(client, MONITORING_ORIGIN, PutWatchAction.INSTANCE, - new PutWatchRequest(uniqueWatchId, new BytesArray(watch), XContentType.JSON), - new ResponseActionListener<>("watch", uniqueWatchId, pendingResponses, watcherSetup)); + executeAsyncWithOrigin( + client, + MONITORING_ORIGIN, + PutWatchAction.INSTANCE, + new PutWatchRequest(uniqueWatchId, new BytesArray(watch), XContentType.JSON), + new ResponseActionListener<>("watch", uniqueWatchId, pendingResponses, watcherSetup) + ); } /** @@ -534,8 +587,8 @@ private void putWatch(final Client client, final String watchId, final String un * @return {@code true} to use Cluster Alerts. */ private boolean canUseWatcher() { - return XPackSettings.WATCHER_ENABLED.get(config.settings()) && - CLUSTER_ALERTS_MANAGEMENT_SETTING.getConcreteSettingForNamespace(config.name()).get(config.settings()); + return XPackSettings.WATCHER_ENABLED.get(config.settings()) + && CLUSTER_ALERTS_MANAGEMENT_SETTING.getConcreteSettingForNamespace(config.name()).get(config.settings()); } @Override @@ -561,15 +614,15 @@ public void onCleanUpIndices(TimeValue retention) { // Get the names of the current monitoring indices final Set currents = MonitoredSystem.allSystems() - .map(s -> MonitoringTemplateUtils.indexName(dateTimeFormatter, s, currentTimeMillis)) - .collect(Collectors.toSet()); + .map(s -> MonitoringTemplateUtils.indexName(dateTimeFormatter, s, currentTimeMillis)) + .collect(Collectors.toSet()); // avoid deleting the current alerts index, but feel free to delete older ones currents.add(MonitoringTemplateRegistry.ALERTS_INDEX_TEMPLATE_NAME); Set indices = new HashSet<>(); for (ObjectObjectCursor index : clusterState.getMetadata().indices()) { - String indexName = index.key; + String indexName = index.key; if (Regex.simpleMatch(indexPatterns, indexName)) { // Never delete any "current" index (e.g., today's index or the most recent version no timestamp, like alerts) @@ -580,8 +633,12 @@ public void onCleanUpIndices(TimeValue retention) { long creationDate = index.value.getCreationDate(); if (creationDate <= expirationTimeMillis) { if (logger.isDebugEnabled()) { - logger.debug("detected expired index [name={}, created={}, expired={}]", - indexName, Instant.ofEpochMilli(creationDate).atZone(ZoneOffset.UTC), expiration); + logger.debug( + "detected expired index [name={}, created={}, expired={}]", + indexName, + Instant.ofEpochMilli(creationDate).atZone(ZoneOffset.UTC), + expiration + ); } indices.add(indexName); } @@ -601,24 +658,29 @@ public void onCleanUpIndices(TimeValue retention) { private void deleteIndices(Set indices) { logger.trace("deleting {} indices: [{}]", indices.size(), collectionToCommaDelimitedString(indices)); final DeleteIndexRequest request = new DeleteIndexRequest(indices.toArray(new String[indices.size()])); - executeAsyncWithOrigin(client.threadPool().getThreadContext(), MONITORING_ORIGIN, request, - new ActionListener() { - @Override - public void onResponse(AcknowledgedResponse response) { - if (response.isAcknowledged()) { - logger.debug("{} indices deleted", indices.size()); - } else { - // Probably means that the delete request has timed out, - // the indices will survive until the next clean up. - logger.warn("deletion of {} indices wasn't acknowledged", indices.size()); - } + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + MONITORING_ORIGIN, + request, + new ActionListener() { + @Override + public void onResponse(AcknowledgedResponse response) { + if (response.isAcknowledged()) { + logger.debug("{} indices deleted", indices.size()); + } else { + // Probably means that the delete request has timed out, + // the indices will survive until the next clean up. + logger.warn("deletion of {} indices wasn't acknowledged", indices.size()); } + } - @Override - public void onFailure(Exception e) { - logger.error("failed to delete indices", e); - } - }, client.admin().indices()::delete); + @Override + public void onFailure(Exception e) { + logger.error("failed to delete indices", e); + } + }, + client.admin().indices()::delete + ); } enum State { @@ -650,8 +712,13 @@ private ResponseActionListener(String type, String name, AtomicInteger countDown this(type, name, countDown, () -> {}, setup); } - private ResponseActionListener(String type, String name, AtomicInteger countDown, Runnable onComplete, - @Nullable AtomicBoolean setup) { + private ResponseActionListener( + String type, + String name, + AtomicInteger countDown, + Runnable onComplete, + @Nullable AtomicBoolean setup + ) { this.type = Objects.requireNonNull(type); this.name = Objects.requireNonNull(name); this.countDown = Objects.requireNonNull(countDown); @@ -662,7 +729,7 @@ private ResponseActionListener(String type, String name, AtomicInteger countDown @Override public void onResponse(Response response) { if (response instanceof AcknowledgedResponse) { - if (((AcknowledgedResponse)response).isAcknowledged()) { + if (((AcknowledgedResponse) response).isAcknowledged()) { logger.trace("successfully set monitoring {} [{}]", type, name); } else { logger.error("failed to set monitoring {} [{}]", type, name); @@ -683,8 +750,14 @@ public void onFailure(Exception e) { private class ErrorCapturingResponseListener extends ResponseActionListener { private final List errors; - ErrorCapturingResponseListener(String type, String name, AtomicInteger countDown, - Consumer setupListener, List errors, String configName) { + ErrorCapturingResponseListener( + String type, + String name, + AtomicInteger countDown, + Consumer setupListener, + List errors, + String configName + ) { super(type, name, countDown, () -> { // Called on completion of all removal tasks ExporterResourceStatus status = ExporterResourceStatus.determineReadiness(configName, TYPE, errors); @@ -695,7 +768,7 @@ private class ErrorCapturingResponseListener extends ResponseActionLis @Override public void onResponse(Response response) { - if (response instanceof AcknowledgedResponse && ((AcknowledgedResponse)response).isAcknowledged() == false) { + if (response instanceof AcknowledgedResponse && ((AcknowledgedResponse) response).isAcknowledged() == false) { errors.add(new ElasticsearchException("failed to set monitoring {} [{}]", type, name)); } super.onResponse(response); @@ -715,9 +788,12 @@ private class GetAndPutWatchResponseActionListener implements ActionListener {}, watcherSetup); } else { @@ -740,8 +819,7 @@ public void onFailure(Exception e) { responseReceived(countDown, false, () -> {}, watcherSetup); if ((e instanceof IndexNotFoundException) == false) { - logger.error((Supplier) () -> - new ParameterizedMessage("failed to get monitoring watch [{}]", uniqueWatchId), e); + logger.error((Supplier) () -> new ParameterizedMessage("failed to get monitoring watch [{}]", uniqueWatchId), e); } } diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/rest/action/RestMonitoringBulkAction.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/rest/action/RestMonitoringBulkAction.java index 52fc6792145b2..1cd8688100927 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/rest/action/RestMonitoringBulkAction.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/rest/action/RestMonitoringBulkAction.java @@ -9,7 +9,6 @@ import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.common.Strings; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.BytesRestResponse; @@ -17,6 +16,7 @@ import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; import org.elasticsearch.xpack.core.monitoring.action.MonitoringBulkRequestBuilder; import org.elasticsearch.xpack.core.monitoring.action.MonitoringBulkResponse; @@ -44,17 +44,19 @@ public class RestMonitoringBulkAction extends BaseRestHandler { ); private static final Map> SUPPORTED_API_VERSIONS = Map.of( - MonitoredSystem.KIBANA, ALL_VERSIONS, - MonitoredSystem.LOGSTASH, ALL_VERSIONS, - MonitoredSystem.BEATS, ALL_VERSIONS); + MonitoredSystem.KIBANA, + ALL_VERSIONS, + MonitoredSystem.LOGSTASH, + ALL_VERSIONS, + MonitoredSystem.BEATS, + ALL_VERSIONS + ); @Override public List routes() { return List.of( - Route.builder(POST, "/_monitoring/bulk") - .replaces(POST, "/_xpack/monitoring/_bulk", RestApiVersion.V_7).build(), - Route.builder(PUT, "/_monitoring/bulk") - .replaces(PUT, "/_xpack/monitoring/_bulk", RestApiVersion.V_7).build() + Route.builder(POST, "/_monitoring/bulk").replaces(POST, "/_xpack/monitoring/_bulk", RestApiVersion.V_7).build(), + Route.builder(PUT, "/_monitoring/bulk").replaces(PUT, "/_xpack/monitoring/_bulk", RestApiVersion.V_7).build() ); } @@ -87,8 +89,9 @@ public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client final MonitoredSystem system = MonitoredSystem.fromSystem(id); if (isSupportedSystemVersion(system, version) == false) { - throw new IllegalArgumentException(MONITORING_VERSION + " [" + version + "] is not supported by " - + MONITORING_ID + " [" + id + "]"); + throw new IllegalArgumentException( + MONITORING_VERSION + " [" + version + "] is not supported by " + MONITORING_ID + " [" + id + "]" + ); } final long timestamp = System.currentTimeMillis(); diff --git a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/rest/action/RestMonitoringMigrateAlertsAction.java b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/rest/action/RestMonitoringMigrateAlertsAction.java index 0e643e80f6388..42871a3fc28bf 100644 --- a/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/rest/action/RestMonitoringMigrateAlertsAction.java +++ b/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/rest/action/RestMonitoringMigrateAlertsAction.java @@ -7,12 +7,7 @@ package org.elasticsearch.xpack.monitoring.rest.action; -import java.io.IOException; -import java.util.List; - import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestChannel; @@ -20,19 +15,22 @@ import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.monitoring.action.MonitoringMigrateAlertsAction; import org.elasticsearch.xpack.core.monitoring.action.MonitoringMigrateAlertsRequest; import org.elasticsearch.xpack.core.monitoring.action.MonitoringMigrateAlertsResponse; +import java.io.IOException; +import java.util.List; + import static org.elasticsearch.rest.RestRequest.Method.POST; public class RestMonitoringMigrateAlertsAction extends BaseRestHandler { @Override public List routes() { - return List.of( - new Route(POST, "/_monitoring/migrate/alerts") - ); + return List.of(new Route(POST, "/_monitoring/migrate/alerts")); } @Override diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/BaseCollectorTestCase.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/BaseCollectorTestCase.java index dfcbe40e6e85a..e55a3c5879538 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/BaseCollectorTestCase.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/BaseCollectorTestCase.java @@ -18,9 +18,9 @@ import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.TransportAddress; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.common.util.set.Sets; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.license.MockLicenseState; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.test.ESTestCase; @@ -56,9 +56,7 @@ public void setUp() throws Exception { ThreadPool threadPool = mock(ThreadPool.class); when(client.threadPool()).thenReturn(threadPool); when(threadPool.getThreadContext()).thenReturn(new ThreadContext(Settings.EMPTY)); - settings = Settings.builder() - .put("path.home", createTempDir()) - .build(); + settings = Settings.builder().put("path.home", createTempDir()).build(); } protected void whenLocalNodeElectedMaster(final boolean electedMaster) { @@ -93,17 +91,13 @@ protected void withCollectionIndices(final String[] collectionIndices) throws Ex } protected void withCollectionSetting(final Function builder) throws Exception { - settings = Settings.builder() - .put(settings) - .put(builder.apply(Settings.builder()).build()) - .build(); - when(clusterService.getClusterSettings()) - .thenReturn(new ClusterSettings(settings, Sets.newHashSet(new Monitoring(settings) { - @Override - protected XPackLicenseState getLicenseState() { - return licenseState; - } - }.getSettings()))); + settings = Settings.builder().put(settings).put(builder.apply(Settings.builder()).build()).build(); + when(clusterService.getClusterSettings()).thenReturn(new ClusterSettings(settings, Sets.newHashSet(new Monitoring(settings) { + @Override + protected XPackLicenseState getLicenseState() { + return licenseState; + } + }.getSettings()))); } protected static DiscoveryNode localNode(final String uuid) { diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/LocalStateMonitoring.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/LocalStateMonitoring.java index 2dcb4bca519cd..45a6a1d27a29a 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/LocalStateMonitoring.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/LocalStateMonitoring.java @@ -44,11 +44,17 @@ public class LocalStateMonitoring extends LocalStateCompositeXPackPlugin { public static class MonitoringTransportXPackUsageAction extends TransportXPackUsageAction { @Inject - public MonitoringTransportXPackUsageAction(ThreadPool threadPool, TransportService transportService, - ClusterService clusterService, ActionFilters actionFilters, - IndexNameExpressionResolver indexNameExpressionResolver, NodeClient client) { + public MonitoringTransportXPackUsageAction( + ThreadPool threadPool, + TransportService transportService, + ClusterService clusterService, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver, + NodeClient client + ) { super(threadPool, transportService, clusterService, actionFilters, indexNameExpressionResolver, client); } + @Override protected List usageActions() { return Collections.singletonList(XPackUsageFeatureAction.MONITORING); @@ -121,16 +127,25 @@ public TransportCcrStatsStubAction(TransportService transportService, ActionFilt @Override protected void doExecute(Task task, CcrStatsAction.Request request, ActionListener listener) { - AutoFollowStats autoFollowStats = - new AutoFollowStats(0, 0, 0, Collections.emptyNavigableMap(), Collections.emptyNavigableMap()); - FollowStatsAction.StatsResponses statsResponses = - new FollowStatsAction.StatsResponses(Collections.emptyList(), Collections.emptyList(), Collections.emptyList()); + AutoFollowStats autoFollowStats = new AutoFollowStats( + 0, + 0, + 0, + Collections.emptyNavigableMap(), + Collections.emptyNavigableMap() + ); + FollowStatsAction.StatsResponses statsResponses = new FollowStatsAction.StatsResponses( + Collections.emptyList(), + Collections.emptyList(), + Collections.emptyList() + ); listener.onResponse(new CcrStatsAction.Response(autoFollowStats, statsResponses)); } } - public static class TransportEnrichStatsStubAction - extends HandledTransportAction { + public static class TransportEnrichStatsStubAction extends HandledTransportAction< + EnrichStatsAction.Request, + EnrichStatsAction.Response> { @Inject public TransportEnrichStatsStubAction(TransportService transportService, ActionFilters actionFilters) { diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/MonitoringHistoryDurationSettingsTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/MonitoringHistoryDurationSettingsTests.java index f9499966631da..904bbd87ed658 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/MonitoringHistoryDurationSettingsTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/MonitoringHistoryDurationSettingsTests.java @@ -20,25 +20,35 @@ public void testHistoryDurationDefaults7Days() { assertEquals(sevenDays, MonitoringField.HISTORY_DURATION.get(Settings.EMPTY)); // Note: this verifies the semantics because this is taken for granted that it never returns null! assertEquals(sevenDays, MonitoringField.HISTORY_DURATION.get(buildSettings(MonitoringField.HISTORY_DURATION.getKey(), null))); - assertWarnings("[xpack.monitoring.history.duration] setting was deprecated in Elasticsearch and will be removed in a future " + - "release! See the breaking changes documentation for the next major version."); + assertWarnings( + "[xpack.monitoring.history.duration] setting was deprecated in Elasticsearch and will be removed in a future " + + "release! See the breaking changes documentation for the next major version." + ); } public void testHistoryDurationMinimum24Hours() { // hit the minimum - assertEquals(MonitoringField.HISTORY_DURATION_MINIMUM, - MonitoringField.HISTORY_DURATION.get(buildSettings(MonitoringField.HISTORY_DURATION.getKey(), "24h"))); - assertWarnings("[xpack.monitoring.history.duration] setting was deprecated in Elasticsearch and will be removed in a future " + - "release! See the breaking changes documentation for the next major version."); + assertEquals( + MonitoringField.HISTORY_DURATION_MINIMUM, + MonitoringField.HISTORY_DURATION.get(buildSettings(MonitoringField.HISTORY_DURATION.getKey(), "24h")) + ); + assertWarnings( + "[xpack.monitoring.history.duration] setting was deprecated in Elasticsearch and will be removed in a future " + + "release! See the breaking changes documentation for the next major version." + ); } public void testHistoryDurationMinimum24HoursBlocksLower() { // 1 ms early! final String oneSecondEarly = (MonitoringField.HISTORY_DURATION_MINIMUM.millis() - 1) + "ms"; - expectThrows(IllegalArgumentException.class, - () -> MonitoringField.HISTORY_DURATION.get(buildSettings(MonitoringField.HISTORY_DURATION.getKey(), oneSecondEarly))); - assertWarnings("[xpack.monitoring.history.duration] setting was deprecated in Elasticsearch and will be removed in a future " + - "release! See the breaking changes documentation for the next major version."); + expectThrows( + IllegalArgumentException.class, + () -> MonitoringField.HISTORY_DURATION.get(buildSettings(MonitoringField.HISTORY_DURATION.getKey(), oneSecondEarly)) + ); + assertWarnings( + "[xpack.monitoring.history.duration] setting was deprecated in Elasticsearch and will be removed in a future " + + "release! See the breaking changes documentation for the next major version." + ); } private Settings buildSettings(String key, String value) { diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/MonitoringInfoTransportActionTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/MonitoringInfoTransportActionTests.java index a10430f4d9f58..759d2def7ca9d 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/MonitoringInfoTransportActionTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/MonitoringInfoTransportActionTests.java @@ -12,12 +12,12 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.StreamInput; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.VersionUtils; import org.elasticsearch.test.rest.yaml.ObjectPath; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.XPackFeatureSet; import org.elasticsearch.xpack.core.action.XPackUsageFeatureResponse; import org.elasticsearch.xpack.core.monitoring.MonitoringFeatureSetUsage; @@ -44,13 +44,17 @@ public class MonitoringInfoTransportActionTests extends ESTestCase { public void testAvailable() { MonitoringInfoTransportAction featureSet = new MonitoringInfoTransportAction( - mock(TransportService.class), mock(ActionFilters.class)); + mock(TransportService.class), + mock(ActionFilters.class) + ); assertThat(featureSet.available(), is(true)); } public void testMonitoringEnabledByDefault() { MonitoringInfoTransportAction featureSet = new MonitoringInfoTransportAction( - mock(TransportService.class), mock(ActionFilters.class)); + mock(TransportService.class), + mock(ActionFilters.class) + ); assertThat(featureSet.enabled(), is(true)); } @@ -89,8 +93,14 @@ public void testUsage() throws Exception { when(exporters.getEnabledExporters()).thenReturn(exporterList); when(monitoring.isMonitoringActive()).thenReturn(collectionEnabled); - var usageAction = new MonitoringUsageTransportAction(mock(TransportService.class), null, null, - mock(ActionFilters.class), null, new MonitoringUsageServices(monitoring, exporters)); + var usageAction = new MonitoringUsageTransportAction( + mock(TransportService.class), + null, + null, + mock(ActionFilters.class), + null, + new MonitoringUsageServices(monitoring, exporters) + ); PlainActionFuture future = new PlainActionFuture<>(); usageAction.masterOperation(null, null, null, future); MonitoringFeatureSetUsage monitoringUsage = (MonitoringFeatureSetUsage) future.get().getUsage(); @@ -101,7 +111,7 @@ public void testUsage() throws Exception { in.setVersion(serializedVersion); XPackFeatureSet.Usage serializedUsage = new MonitoringFeatureSetUsage(in); for (XPackFeatureSet.Usage usage : Arrays.asList(monitoringUsage, serializedUsage)) { - ObjectPath source; + ObjectPath source; try (XContentBuilder builder = jsonBuilder()) { usage.toXContent(builder, ToXContent.EMPTY_PARAMS); source = ObjectPath.createFromXContent(builder.contentType().xContent(), BytesReference.bytes(builder)); diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/MonitoringServiceTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/MonitoringServiceTests.java index e6c134512e0e7..f5b86fc8784cb 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/MonitoringServiceTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/MonitoringServiceTests.java @@ -48,9 +48,7 @@ public void setUp() throws Exception { super.setUp(); threadPool = new TestThreadPool(getTestName()); clusterService = mock(ClusterService.class); - Settings settings = Settings.builder() - .put("path.home", createTempDir()) - .build(); + Settings settings = Settings.builder().put("path.home", createTempDir()).build(); final Monitoring monitoring = new Monitoring(settings) { @Override @@ -97,10 +95,7 @@ public void testIsMonitoringActive() throws Exception { } public void testInterval() throws Exception { - final Settings settings = - Settings.builder() - .put("xpack.monitoring.collection.interval", MonitoringService.MIN_INTERVAL) - .build(); + final Settings settings = Settings.builder().put("xpack.monitoring.collection.interval", MonitoringService.MIN_INTERVAL).build(); CountingExporter exporter = new CountingExporter(); monitoringService = new MonitoringService(settings, clusterService, threadPool, emptySet(), exporter); @@ -118,18 +113,19 @@ public void testInterval() throws Exception { // take down threads monitoringService.setMonitoringActive(false); - assertWarnings("[xpack.monitoring.collection.interval] setting was deprecated in Elasticsearch and will be removed in " + - "a future release! See the breaking changes documentation for the next major version."); + assertWarnings( + "[xpack.monitoring.collection.interval] setting was deprecated in Elasticsearch and will be removed in " + + "a future release! See the breaking changes documentation for the next major version." + ); } public void testSkipExecution() throws Exception { final CountDownLatch latch = new CountDownLatch(1); final BlockingExporter exporter = new BlockingExporter(latch); - final Settings settings = - Settings.builder() - .put("xpack.monitoring.collection.enabled", true) - .put("xpack.monitoring.collection.interval", MonitoringService.MIN_INTERVAL) - .build(); + final Settings settings = Settings.builder() + .put("xpack.monitoring.collection.enabled", true) + .put("xpack.monitoring.collection.interval", MonitoringService.MIN_INTERVAL) + .build(); monitoringService = new MonitoringService(settings, clusterService, threadPool, emptySet(), exporter); @@ -144,10 +140,10 @@ public void testSkipExecution() throws Exception { assertThat(exporter.getExportsCount(), equalTo(1)); assertWarnings( - "[xpack.monitoring.collection.enabled] setting was deprecated in Elasticsearch and will be removed in " + - "a future release! See the breaking changes documentation for the next major version.", - "[xpack.monitoring.collection.interval] setting was deprecated in Elasticsearch and will be removed in " + - "a future release! See the breaking changes documentation for the next major version." + "[xpack.monitoring.collection.enabled] setting was deprecated in Elasticsearch and will be removed in " + + "a future release! See the breaking changes documentation for the next major version.", + "[xpack.monitoring.collection.interval] setting was deprecated in Elasticsearch and will be removed in " + + "a future release! See the breaking changes documentation for the next major version." ); } @@ -170,16 +166,13 @@ int getExportsCount() { } @Override - protected void doStart() { - } + protected void doStart() {} @Override - protected void doStop() { - } + protected void doStop() {} @Override - protected void doClose() { - } + protected void doClose() {} } class BlockingExporter extends CountingExporter { @@ -204,15 +197,12 @@ public void export(Collection docs, ActionListener listener } @Override - protected void doStart() { - } + protected void doStart() {} @Override - protected void doStop() { - } + protected void doStop() {} @Override - protected void doClose() { - } + protected void doClose() {} } } diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/MonitoringTestUtils.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/MonitoringTestUtils.java index 7f1ff3173676a..78eb664f31f22 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/MonitoringTestUtils.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/MonitoringTestUtils.java @@ -9,10 +9,11 @@ import com.carrotsearch.randomizedtesting.generators.RandomNumbers; import com.carrotsearch.randomizedtesting.generators.RandomPicks; import com.carrotsearch.randomizedtesting.generators.RandomStrings; + import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.transport.TransportAddress; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.test.RandomObjects; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; import org.elasticsearch.xpack.core.monitoring.action.MonitoringBulkDoc; import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc; @@ -25,10 +26,9 @@ public final class MonitoringTestUtils { // maximum number of milliseconds before a five digit year comes in, which could change formatting - public static final long MAX_MILLIS_BEFORE_10000 = 253402300799999L; + public static final long MAX_MILLIS_BEFORE_10000 = 253402300799999L; - private MonitoringTestUtils() { - } + private MonitoringTestUtils() {} /** * Generates a random {@link MonitoringDoc.Node} @@ -56,28 +56,31 @@ public static MonitoringBulkDoc randomMonitoringBulkDoc(Random random) throws IO /** * Generates a random {@link MonitoringDoc} with a given {@link XContentType} */ - public static MonitoringBulkDoc randomMonitoringBulkDoc(final Random random, - final XContentType xContentType) throws IOException { + public static MonitoringBulkDoc randomMonitoringBulkDoc(final Random random, final XContentType xContentType) throws IOException { return randomMonitoringBulkDoc(random, xContentType, RandomObjects.randomSource(random, xContentType)); } /** * Generates a random {@link MonitoringDoc} with a given {@link XContentType} and {@link BytesReference} source */ - public static MonitoringBulkDoc randomMonitoringBulkDoc(final Random random, - final XContentType xContentType, - final BytesReference source) throws IOException { + public static MonitoringBulkDoc randomMonitoringBulkDoc( + final Random random, + final XContentType xContentType, + final BytesReference source + ) throws IOException { return randomMonitoringBulkDoc(random, xContentType, source, RandomPicks.randomFrom(random, MonitoredSystem.values())); } /** * Generates a random {@link MonitoringDoc} with a given {@link XContentType}, {@link BytesReference} source and {@link MonitoredSystem} */ - public static MonitoringBulkDoc randomMonitoringBulkDoc(final Random random, - final XContentType xContentType, - final BytesReference source, - final MonitoredSystem system) throws IOException { - final String type = RandomPicks.randomFrom(random, new String[]{"type1", "type2", "type3"}); + public static MonitoringBulkDoc randomMonitoringBulkDoc( + final Random random, + final XContentType xContentType, + final BytesReference source, + final MonitoredSystem system + ) throws IOException { + final String type = RandomPicks.randomFrom(random, new String[] { "type1", "type2", "type3" }); return randomMonitoringBulkDoc(random, xContentType, source, system, type); } @@ -85,11 +88,13 @@ public static MonitoringBulkDoc randomMonitoringBulkDoc(final Random random, * Generates a random {@link MonitoringDoc} with a given {@link XContentType}, {@link BytesReference} source, * {@link MonitoredSystem} and type. */ - public static MonitoringBulkDoc randomMonitoringBulkDoc(final Random random, - final XContentType xContentType, - final BytesReference source, - final MonitoredSystem system, - final String type) throws IOException { + public static MonitoringBulkDoc randomMonitoringBulkDoc( + final Random random, + final XContentType xContentType, + final BytesReference source, + final MonitoredSystem system, + final String type + ) throws IOException { final String id = random.nextBoolean() ? RandomStrings.randomAsciiLettersOfLength(random, 5) : null; final long timestamp = RandomNumbers.randomLongBetween(random, 0L, MAX_MILLIS_BEFORE_10000); final long interval = RandomNumbers.randomLongBetween(random, 0L, Long.MAX_VALUE); diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/MultiNodesStatsTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/MultiNodesStatsTests.java index 598f53b455130..b61112f05b37b 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/MultiNodesStatsTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/MultiNodesStatsTests.java @@ -30,9 +30,9 @@ public class MultiNodesStatsTests extends MonitoringIntegTestCase { @Override protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { return Settings.builder() - .put(super.nodeSettings(nodeOrdinal, otherSettings)) - .put("xpack.monitoring.exporters.default_local.type", "local") - .build(); + .put(super.nodeSettings(nodeOrdinal, otherSettings)) + .put("xpack.monitoring.exporters.default_local.type", "local") + .build(); } @After @@ -59,7 +59,7 @@ public void testMultipleNodes() throws Exception { n = randomIntBetween(1, 2); // starting one by one to allow moving , for example, from a 2 node cluster to a 4 one while updating min_master_nodes - for (int i=0;i copy = - doc -> new MonitoringBulkDoc(doc.getSystem(), doc.getType(), doc.getId(), doc.getTimestamp(), doc.getIntervalMillis(), - doc.getSource(), doc.getXContentType()); + final EqualsHashCodeTestUtils.CopyFunction copy = doc -> new MonitoringBulkDoc( + doc.getSystem(), + doc.getType(), + doc.getId(), + doc.getTimestamp(), + doc.getIntervalMillis(), + doc.getSource(), + doc.getXContentType() + ); final List> mutations = new ArrayList<>(); mutations.add(doc -> { @@ -89,53 +95,102 @@ public void testEqualsAndHashcode() { do { system = randomFrom(MonitoredSystem.values()); } while (system == doc.getSystem()); - return new MonitoringBulkDoc(system, doc.getType(), doc.getId(), doc.getTimestamp(), doc.getIntervalMillis(), - doc.getSource(), doc.getXContentType()); + return new MonitoringBulkDoc( + system, + doc.getType(), + doc.getId(), + doc.getTimestamp(), + doc.getIntervalMillis(), + doc.getSource(), + doc.getXContentType() + ); }); mutations.add(doc -> { String type; do { type = randomAlphaOfLength(5); } while (type.equals(doc.getType())); - return new MonitoringBulkDoc(doc.getSystem(), type, doc.getId(), doc.getTimestamp(), doc.getIntervalMillis(), - doc.getSource(), doc.getXContentType()); + return new MonitoringBulkDoc( + doc.getSystem(), + type, + doc.getId(), + doc.getTimestamp(), + doc.getIntervalMillis(), + doc.getSource(), + doc.getXContentType() + ); }); mutations.add(doc -> { String id; do { id = randomAlphaOfLength(10); } while (id.equals(doc.getId())); - return new MonitoringBulkDoc(doc.getSystem(), doc.getType(), id, doc.getTimestamp(), doc.getIntervalMillis(), - doc.getSource(), doc.getXContentType()); + return new MonitoringBulkDoc( + doc.getSystem(), + doc.getType(), + id, + doc.getTimestamp(), + doc.getIntervalMillis(), + doc.getSource(), + doc.getXContentType() + ); }); mutations.add(doc -> { long timestamp; do { timestamp = randomNonNegativeLong(); } while (timestamp == doc.getTimestamp()); - return new MonitoringBulkDoc(doc.getSystem(), doc.getType(), doc.getId(), timestamp, doc.getIntervalMillis(), - doc.getSource(), doc.getXContentType()); + return new MonitoringBulkDoc( + doc.getSystem(), + doc.getType(), + doc.getId(), + timestamp, + doc.getIntervalMillis(), + doc.getSource(), + doc.getXContentType() + ); }); mutations.add(doc -> { long interval; do { interval = randomNonNegativeLong(); } while (interval == doc.getIntervalMillis()); - return new MonitoringBulkDoc(doc.getSystem(), doc.getType(), doc.getId(), doc.getTimestamp(), interval, - doc.getSource(), doc.getXContentType()); + return new MonitoringBulkDoc( + doc.getSystem(), + doc.getType(), + doc.getId(), + doc.getTimestamp(), + interval, + doc.getSource(), + doc.getXContentType() + ); }); mutations.add(doc -> { final BytesReference source = RandomObjects.randomSource(random(), doc.getXContentType()); - return new MonitoringBulkDoc(doc.getSystem(), doc.getType(), doc.getId(), doc.getTimestamp(), doc.getIntervalMillis(), - source, doc.getXContentType()); + return new MonitoringBulkDoc( + doc.getSystem(), + doc.getType(), + doc.getId(), + doc.getTimestamp(), + doc.getIntervalMillis(), + source, + doc.getXContentType() + ); }); mutations.add(doc -> { XContentType xContentType; do { xContentType = randomFrom(XContentType.values()); } while (xContentType == doc.getXContentType()); - return new MonitoringBulkDoc(doc.getSystem(), doc.getType(), doc.getId(), doc.getTimestamp(), doc.getIntervalMillis(), - doc.getSource(), xContentType); + return new MonitoringBulkDoc( + doc.getSystem(), + doc.getType(), + doc.getId(), + doc.getTimestamp(), + doc.getIntervalMillis(), + doc.getSource(), + xContentType + ); }); final MonitoringBulkDoc document = new MonitoringBulkDoc(system, type, id, timestamp, interval, source, xContentType); diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/action/MonitoringBulkRequestTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/action/MonitoringBulkRequestTests.java index 657faa9acb6e0..67a25ecdb1c2c 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/action/MonitoringBulkRequestTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/action/MonitoringBulkRequestTests.java @@ -12,11 +12,11 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.RandomObjects; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.test.RandomObjects; import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; import org.elasticsearch.xpack.core.monitoring.action.MonitoringBulkDoc; import org.elasticsearch.xpack.core.monitoring.action.MonitoringBulkRequest; @@ -180,8 +180,9 @@ public void testAddRequestContentWithEmptySource() throws IOException { } final MonitoringBulkRequest bulkRequest = new MonitoringBulkRequest(); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> - bulkRequest.add(randomFrom(MonitoredSystem.values()), content.bytes(), xContentType, 0L, 0L) + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> bulkRequest.add(randomFrom(MonitoredSystem.values()), content.bytes(), xContentType, 0L, 0L) ); assertThat(e.getMessage(), containsString("source is missing for monitoring document [][_doc][" + nbDocs + "]")); @@ -216,8 +217,9 @@ public void testAddRequestContentWithUnrecognizedIndexName() throws IOException } final MonitoringBulkRequest bulkRequest = new MonitoringBulkRequest(); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> - bulkRequest.add(randomFrom(MonitoredSystem.values()), content.bytes(), xContentType, 0L, 0L) + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> bulkRequest.add(randomFrom(MonitoredSystem.values()), content.bytes(), xContentType, 0L, 0L) ); assertThat(e.getMessage(), containsString("unrecognized index name [" + indexName + "]")); @@ -241,8 +243,8 @@ public void testSerialization() throws IOException { assertThat(in.available(), equalTo(0)); - final MonitoringBulkDoc[] originalBulkDocs = originalRequest.getDocs().toArray(new MonitoringBulkDoc[]{}); - final MonitoringBulkDoc[] deserializedBulkDocs = deserializedRequest.getDocs().toArray(new MonitoringBulkDoc[]{}); + final MonitoringBulkDoc[] originalBulkDocs = originalRequest.getDocs().toArray(new MonitoringBulkDoc[] {}); + final MonitoringBulkDoc[] deserializedBulkDocs = deserializedRequest.getDocs().toArray(new MonitoringBulkDoc[] {}); assertArrayEquals(originalBulkDocs, deserializedBulkDocs); } diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/action/MonitoringBulkResponseTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/action/MonitoringBulkResponseTests.java index b1295fd2df840..9af5581bc2f84 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/action/MonitoringBulkResponseTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/action/MonitoringBulkResponseTests.java @@ -58,9 +58,10 @@ public void testSerialization() throws IOException { response = new MonitoringBulkResponse(Math.abs(randomLong()), randomBoolean()); } else { Exception exception = randomFrom( - new ExportException(randomAlphaOfLength(5), new IllegalStateException(randomAlphaOfLength(5))), - new IllegalStateException(randomAlphaOfLength(5)), - new IllegalArgumentException(randomAlphaOfLength(5))); + new ExportException(randomAlphaOfLength(5), new IllegalStateException(randomAlphaOfLength(5))), + new IllegalStateException(randomAlphaOfLength(5)), + new IllegalArgumentException(randomAlphaOfLength(5)) + ); response = new MonitoringBulkResponse(Math.abs(randomLong()), new MonitoringBulkResponse.Error(exception)); } diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/action/TransportMonitoringBulkActionTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/action/TransportMonitoringBulkActionTests.java index 383933603c712..3852d84a27cb4 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/action/TransportMonitoringBulkActionTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/action/TransportMonitoringBulkActionTests.java @@ -23,9 +23,7 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.transport.TransportAddress; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.tasks.Task; import org.elasticsearch.tasks.TaskAwareRequest; @@ -34,6 +32,8 @@ import org.elasticsearch.test.RandomObjects; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; import org.elasticsearch.xpack.core.monitoring.action.MonitoringBulkAction; import org.elasticsearch.xpack.core.monitoring.action.MonitoringBulkDoc; @@ -105,7 +105,7 @@ public void setUpMocks() { // execute in the same thread doAnswer(invocation -> { - ((Runnable)invocation.getArguments()[0]).run(); + ((Runnable) invocation.getArguments()[0]).run(); return null; }).when(executor).execute(any(Runnable.class)); } @@ -114,9 +114,14 @@ public void testExecuteWithGlobalBlock() throws Exception { final ClusterBlocks.Builder clusterBlock = ClusterBlocks.builder().addGlobalBlock(NoMasterBlockService.NO_MASTER_BLOCK_ALL); when(clusterService.state()).thenReturn(ClusterState.builder(ClusterName.DEFAULT).blocks(clusterBlock).build()); - final TransportMonitoringBulkAction action = new TransportMonitoringBulkAction(threadPool, clusterService, - transportService, filters, exporters, - monitoringService); + final TransportMonitoringBulkAction action = new TransportMonitoringBulkAction( + threadPool, + clusterService, + transportService, + filters, + exporters, + monitoringService + ); final MonitoringBulkRequest request = randomRequest(); final ClusterBlockException e = expectThrows(ClusterBlockException.class, () -> ActionTestUtils.executeBlocking(action, request)); @@ -128,9 +133,14 @@ public void testExecuteIgnoresRequestWhenCollectionIsDisabled() throws Exception when(clusterService.state()).thenReturn(ClusterState.builder(ClusterName.DEFAULT).build()); when(monitoringService.isMonitoringActive()).thenReturn(false); - final TransportMonitoringBulkAction action = new TransportMonitoringBulkAction(threadPool, clusterService, - transportService, filters, exporters, - monitoringService); + final TransportMonitoringBulkAction action = new TransportMonitoringBulkAction( + threadPool, + clusterService, + transportService, + filters, + exporters, + monitoringService + ); final MonitoringBulkDoc doc = mock(MonitoringBulkDoc.class); when(doc.getSource()).thenReturn(new BytesArray("test")); @@ -150,13 +160,20 @@ public void testExecuteEmptyRequest() { // it validates the request before it tries to execute it when(monitoringService.isMonitoringActive()).thenReturn(randomBoolean()); - final TransportMonitoringBulkAction action = new TransportMonitoringBulkAction(threadPool, clusterService, - transportService, filters, exporters, - monitoringService); + final TransportMonitoringBulkAction action = new TransportMonitoringBulkAction( + threadPool, + clusterService, + transportService, + filters, + exporters, + monitoringService + ); final MonitoringBulkRequest request = new MonitoringBulkRequest(); - final ActionRequestValidationException e = expectThrows(ActionRequestValidationException.class, - () -> ActionTestUtils.executeBlocking(action, request)); + final ActionRequestValidationException e = expectThrows( + ActionRequestValidationException.class, + () -> ActionTestUtils.executeBlocking(action, request) + ); assertThat(e, hasToString(containsString("no monitoring documents added"))); } @@ -165,13 +182,13 @@ public void testExecuteEmptyRequest() { public void testExecuteRequest() { when(monitoringService.isMonitoringActive()).thenReturn(true); - final DiscoveryNode discoveryNode = new DiscoveryNode("_id", new TransportAddress(TransportAddress.META_ADDRESS, 9300), CURRENT); + final DiscoveryNode discoveryNode = new DiscoveryNode("_id", new TransportAddress(TransportAddress.META_ADDRESS, 9300), CURRENT); when(clusterService.localNode()).thenReturn(discoveryNode); final String clusterUUID = UUIDs.randomBase64UUID(); - when(clusterService.state()).thenReturn(ClusterState.builder(ClusterName.DEFAULT) - .metadata(Metadata.builder().clusterUUID(clusterUUID).build()) - .build()); + when(clusterService.state()).thenReturn( + ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder().clusterUUID(clusterUUID).build()).build() + ); final MonitoringBulkRequest request = new MonitoringBulkRequest(); @@ -215,9 +232,14 @@ public void testExecuteRequest() { return Void.TYPE; }).when(exporters).export(any(Collection.class), any(ActionListener.class)); - final TransportMonitoringBulkAction action = new TransportMonitoringBulkAction(threadPool, clusterService, - transportService, filters, exporters, - monitoringService); + final TransportMonitoringBulkAction action = new TransportMonitoringBulkAction( + threadPool, + clusterService, + transportService, + filters, + exporters, + monitoringService + ); ActionTestUtils.executeBlocking(action, request); verify(threadPool).executor(ThreadPool.Names.GENERIC); @@ -237,9 +259,15 @@ public void testAsyncActionCreateMonitoringDocsWithNoDocs() { } } - final Collection results = - new TransportMonitoringBulkAction.AsyncAction(threadPool, null, null, null, null, 0L, null) - .createMonitoringDocs(bulkDocs); + final Collection results = new TransportMonitoringBulkAction.AsyncAction( + threadPool, + null, + null, + null, + null, + 0L, + null + ).createMonitoringDocs(bulkDocs); assertThat(results, notNullValue()); assertThat(results.size(), equalTo(0)); @@ -270,9 +298,15 @@ public void testAsyncActionCreateMonitoringDocs() { when(mockBulkDoc.getXContentType()).thenReturn(xContentType); } - final Collection exportedDocs = - new TransportMonitoringBulkAction.AsyncAction(threadPool, null, null, null, "_cluster", 123L, node) - .createMonitoringDocs(docs); + final Collection exportedDocs = new TransportMonitoringBulkAction.AsyncAction( + threadPool, + null, + null, + null, + "_cluster", + 123L, + node + ).createMonitoringDocs(docs); assertThat(exportedDocs, notNullValue()); assertThat(exportedDocs.size(), equalTo(nbDocs)); @@ -288,11 +322,17 @@ public void testAsyncActionCreateMonitoringDocs() { } public void testAsyncActionCreateMonitoringDocWithNoTimestamp() { - final MonitoringBulkDoc monitoringBulkDoc = - new MonitoringBulkDoc(MonitoredSystem.LOGSTASH, "_type", "_id", 0L, 0L, BytesArray.EMPTY, XContentType.JSON); - final MonitoringDoc monitoringDoc = - new TransportMonitoringBulkAction.AsyncAction(threadPool, null, null, null, "", 456L, null) - .createMonitoringDoc(monitoringBulkDoc); + final MonitoringBulkDoc monitoringBulkDoc = new MonitoringBulkDoc( + MonitoredSystem.LOGSTASH, + "_type", + "_id", + 0L, + 0L, + BytesArray.EMPTY, + XContentType.JSON + ); + final MonitoringDoc monitoringDoc = new TransportMonitoringBulkAction.AsyncAction(threadPool, null, null, null, "", 456L, null) + .createMonitoringDoc(monitoringBulkDoc); assertThat(monitoringDoc.getTimestamp(), equalTo(456L)); } @@ -301,40 +341,53 @@ public void testAsyncActionCreateMonitoringDoc() throws Exception { final MonitoringDoc.Node node = new MonitoringDoc.Node("_uuid", "_host", "_addr", "_ip", "_name", 1504169190855L); final XContentType xContentType = randomFrom(XContentType.values()); - final BytesReference source = BytesReference.bytes(XContentBuilder.builder(xContentType.xContent()) - .startObject() - .startObject("_foo") - .field("_bar", "_baz") - .endObject() - .endObject()); - - final MonitoringBulkDoc monitoringBulkDoc = - new MonitoringBulkDoc(MonitoredSystem.LOGSTASH, "_type", "_id", 1502107402133L, 15_000L, source, xContentType); - - final MonitoringDoc monitoringDoc = - new TransportMonitoringBulkAction.AsyncAction(threadPool, null, null, null, "_cluster_uuid", 3L, node) - .createMonitoringDoc(monitoringBulkDoc); + final BytesReference source = BytesReference.bytes( + XContentBuilder.builder(xContentType.xContent()).startObject().startObject("_foo").field("_bar", "_baz").endObject().endObject() + ); + + final MonitoringBulkDoc monitoringBulkDoc = new MonitoringBulkDoc( + MonitoredSystem.LOGSTASH, + "_type", + "_id", + 1502107402133L, + 15_000L, + source, + xContentType + ); + + final MonitoringDoc monitoringDoc = new TransportMonitoringBulkAction.AsyncAction( + threadPool, + null, + null, + null, + "_cluster_uuid", + 3L, + node + ).createMonitoringDoc(monitoringBulkDoc); final BytesReference xContent = XContentHelper.toXContent(monitoringDoc, XContentType.JSON, randomBoolean()); - assertEquals("{" - + "\"cluster_uuid\":\"_cluster_uuid\"," - + "\"timestamp\":\"2017-08-07T12:03:22.133Z\"," - + "\"interval_ms\":15000," - + "\"type\":\"_type\"," - + "\"source_node\":{" - + "\"uuid\":\"_uuid\"," - + "\"host\":\"_host\"," - + "\"transport_address\":\"_addr\"," - + "\"ip\":\"_ip\"," - + "\"name\":\"_name\"," - + "\"timestamp\":\"2017-08-31T08:46:30.855Z\"" - + "}," - + "\"_type\":{" - + "\"_foo\":{" - + "\"_bar\":\"_baz\"" - + "}" - + "}" - + "}" , xContent.utf8ToString()); + assertEquals( + "{" + + "\"cluster_uuid\":\"_cluster_uuid\"," + + "\"timestamp\":\"2017-08-07T12:03:22.133Z\"," + + "\"interval_ms\":15000," + + "\"type\":\"_type\"," + + "\"source_node\":{" + + "\"uuid\":\"_uuid\"," + + "\"host\":\"_host\"," + + "\"transport_address\":\"_addr\"," + + "\"ip\":\"_ip\"," + + "\"name\":\"_name\"," + + "\"timestamp\":\"2017-08-31T08:46:30.855Z\"" + + "}," + + "\"_type\":{" + + "\"_foo\":{" + + "\"_bar\":\"_baz\"" + + "}" + + "}" + + "}", + xContent.utf8ToString() + ); } @SuppressWarnings("unchecked") @@ -354,8 +407,15 @@ public void testAsyncActionExecuteExport() { return Void.TYPE; }).when(exporters).export(any(Collection.class), any(ActionListener.class)); - final TransportMonitoringBulkAction.AsyncAction asyncAction = - new TransportMonitoringBulkAction.AsyncAction(threadPool, null, null, exporters, null, 0L, null); + final TransportMonitoringBulkAction.AsyncAction asyncAction = new TransportMonitoringBulkAction.AsyncAction( + threadPool, + null, + null, + exporters, + null, + 0L, + null + ); asyncAction.executeExport(docs, randomNonNegativeLong(), listener); @@ -371,12 +431,17 @@ public void testAsyncActionExportThrowsException() { docs.add(mock(MonitoringDoc.class)); } - doThrow(new IllegalStateException("something went wrong")) - .when(exporters) - .export(any(Collection.class), any(ActionListener.class)); - - final TransportMonitoringBulkAction.AsyncAction asyncAction = - new TransportMonitoringBulkAction.AsyncAction(threadPool, null, null, exporters, null, 0L, null); + doThrow(new IllegalStateException("something went wrong")).when(exporters).export(any(Collection.class), any(ActionListener.class)); + + final TransportMonitoringBulkAction.AsyncAction asyncAction = new TransportMonitoringBulkAction.AsyncAction( + threadPool, + null, + null, + exporters, + null, + 0L, + null + ); asyncAction.executeExport(docs, randomNonNegativeLong(), listener); diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/action/TransportMonitoringMigrateAlertsActionTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/action/TransportMonitoringMigrateAlertsActionTests.java index 51e579c993f6e..3bdc5cd0c6ac1 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/action/TransportMonitoringMigrateAlertsActionTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/action/TransportMonitoringMigrateAlertsActionTests.java @@ -7,28 +7,10 @@ package org.elasticsearch.xpack.monitoring.action; -import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; -import static org.elasticsearch.xpack.monitoring.exporter.http.ClusterAlertHttpResource.CLUSTER_ALERT_VERSION_PARAMETERS; -import static org.elasticsearch.xpack.monitoring.exporter.http.WatcherExistsHttpResource.WATCHER_CHECK_PARAMETERS; -import static org.hamcrest.Matchers.endsWith; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.notNullValue; -import static org.hamcrest.Matchers.nullValue; -import static org.hamcrest.Matchers.startsWith; - -import java.io.IOException; -import java.util.Arrays; -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import java.util.concurrent.TimeUnit; -import java.util.stream.Collectors; - import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse; import org.elasticsearch.cluster.metadata.IndexTemplateMetadata; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.Tuple; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.test.ESIntegTestCase; import org.elasticsearch.test.http.MockRequest; @@ -56,6 +38,24 @@ import org.junit.After; import org.junit.Before; +import java.io.IOException; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; +import static org.elasticsearch.xpack.monitoring.exporter.http.ClusterAlertHttpResource.CLUSTER_ALERT_VERSION_PARAMETERS; +import static org.elasticsearch.xpack.monitoring.exporter.http.WatcherExistsHttpResource.WATCHER_CHECK_PARAMETERS; +import static org.hamcrest.Matchers.endsWith; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.nullValue; +import static org.hamcrest.Matchers.startsWith; + @ESIntegTestCase.ClusterScope(numDataNodes = 3) public class TransportMonitoringMigrateAlertsActionTests extends MonitoringIntegTestCase { @@ -97,26 +97,36 @@ protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { private void stopMonitoring() { // Clean up any persistent settings we have added - assertAcked(client().admin().cluster().prepareUpdateSettings().setPersistentSettings(Settings.builder() - .putNull(MonitoringService.ENABLED.getKey()) - .putNull("xpack.monitoring.elasticsearch.collection.enabled") - .putNull("xpack.monitoring.exporters._local.type") - .putNull("xpack.monitoring.exporters._local.enabled") - .putNull("xpack.monitoring.exporters._local.cluster_alerts.management.enabled") - .putNull("xpack.monitoring.exporters.remoteCluster.type") - .putNull("xpack.monitoring.exporters.remoteCluster.enabled") - .putNull("xpack.monitoring.exporters.remoteCluster.host") - .putNull("xpack.monitoring.exporters.remoteCluster.cluster_alerts.management.enabled") - )); + assertAcked( + client().admin() + .cluster() + .prepareUpdateSettings() + .setPersistentSettings( + Settings.builder() + .putNull(MonitoringService.ENABLED.getKey()) + .putNull("xpack.monitoring.elasticsearch.collection.enabled") + .putNull("xpack.monitoring.exporters._local.type") + .putNull("xpack.monitoring.exporters._local.enabled") + .putNull("xpack.monitoring.exporters._local.cluster_alerts.management.enabled") + .putNull("xpack.monitoring.exporters.remoteCluster.type") + .putNull("xpack.monitoring.exporters.remoteCluster.enabled") + .putNull("xpack.monitoring.exporters.remoteCluster.host") + .putNull("xpack.monitoring.exporters.remoteCluster.cluster_alerts.management.enabled") + ) + ); // Make sure to clean up the migration setting if it is set - assertAcked(client().admin().cluster().prepareUpdateSettings().setPersistentSettings(Settings.builder() - .putNull(Monitoring.MIGRATION_DECOMMISSION_ALERTS.getKey()) - )); + assertAcked( + client().admin() + .cluster() + .prepareUpdateSettings() + .setPersistentSettings(Settings.builder().putNull(Monitoring.MIGRATION_DECOMMISSION_ALERTS.getKey())) + ); } @TestLogging( value = "org.elasticsearch.xpack.monitoring.exporter.local:trace", - reason = "to ensure we log local exporter on trace level") + reason = "to ensure we log local exporter on trace level" + ) public void testLocalAlertsRemoval() throws Exception { try { // start monitoring service @@ -133,8 +143,10 @@ public void testLocalAlertsRemoval() throws Exception { ensureInitialLocalResources(); // call migration api - MonitoringMigrateAlertsResponse response = client().execute(MonitoringMigrateAlertsAction.INSTANCE, - new MonitoringMigrateAlertsRequest()).actionGet(); + MonitoringMigrateAlertsResponse response = client().execute( + MonitoringMigrateAlertsAction.INSTANCE, + new MonitoringMigrateAlertsRequest() + ).actionGet(); // check response assertThat(response.getExporters().size(), is(1)); @@ -153,7 +165,8 @@ public void testLocalAlertsRemoval() throws Exception { @TestLogging( value = "org.elasticsearch.xpack.monitoring.exporter.local:trace", - reason = "to ensure we log local exporter on trace level") + reason = "to ensure we log local exporter on trace level" + ) public void testRepeatedLocalAlertsRemoval() throws Exception { try { // start monitoring service @@ -170,8 +183,10 @@ public void testRepeatedLocalAlertsRemoval() throws Exception { ensureInitialLocalResources(); // call migration api - MonitoringMigrateAlertsResponse response = client().execute(MonitoringMigrateAlertsAction.INSTANCE, - new MonitoringMigrateAlertsRequest()).actionGet(); + MonitoringMigrateAlertsResponse response = client().execute( + MonitoringMigrateAlertsAction.INSTANCE, + new MonitoringMigrateAlertsRequest() + ).actionGet(); // check response assertThat(response.getExporters().size(), is(1)); @@ -224,8 +239,10 @@ public void testDisabledLocalExporterAlertsRemoval() throws Exception { assertAcked(client().admin().cluster().prepareUpdateSettings().setPersistentSettings(disableSettings)); // call migration api - MonitoringMigrateAlertsResponse response = client().execute(MonitoringMigrateAlertsAction.INSTANCE, - new MonitoringMigrateAlertsRequest()).actionGet(); + MonitoringMigrateAlertsResponse response = client().execute( + MonitoringMigrateAlertsAction.INSTANCE, + new MonitoringMigrateAlertsRequest() + ).actionGet(); // check response assertThat(response.getExporters().size(), is(1)); @@ -267,8 +284,10 @@ public void testLocalExporterWithAlertingDisabled() throws Exception { assertAcked(client().admin().cluster().prepareUpdateSettings().setPersistentSettings(disableSettings)); // call migration api - MonitoringMigrateAlertsResponse response = client().execute(MonitoringMigrateAlertsAction.INSTANCE, - new MonitoringMigrateAlertsRequest()).actionGet(); + MonitoringMigrateAlertsResponse response = client().execute( + MonitoringMigrateAlertsAction.INSTANCE, + new MonitoringMigrateAlertsRequest() + ).actionGet(); // check response assertThat(response.getExporters().size(), is(1)); @@ -302,8 +321,10 @@ public void testRemoteAlertsRemoval() throws Exception { enqueueWatcherResponses(webServer, true); // call migration api - MonitoringMigrateAlertsResponse response = client().execute(MonitoringMigrateAlertsAction.INSTANCE, - new MonitoringMigrateAlertsRequest()).actionGet(); + MonitoringMigrateAlertsResponse response = client().execute( + MonitoringMigrateAlertsAction.INSTANCE, + new MonitoringMigrateAlertsRequest() + ).actionGet(); // check that all "remote watches" were deleted by the exporter assertThat(response.getExporters().size(), is(1)); @@ -340,8 +361,10 @@ public void testDisabledRemoteAlertsRemoval() throws Exception { enqueueWatcherResponses(webServer, true); // call migration api - MonitoringMigrateAlertsResponse response = client().execute(MonitoringMigrateAlertsAction.INSTANCE, - new MonitoringMigrateAlertsRequest()).actionGet(); + MonitoringMigrateAlertsResponse response = client().execute( + MonitoringMigrateAlertsAction.INSTANCE, + new MonitoringMigrateAlertsRequest() + ).actionGet(); // check that the disabled http exporter was enabled this one time in order to remove watches assertThat(response.getExporters().size(), is(1)); @@ -375,8 +398,10 @@ public void testRemoteAlertsRemovalWhenOriginalMonitoringClusterIsGone() throws assertAcked(client().admin().cluster().prepareUpdateSettings().setPersistentSettings(exporterSettings)); // call migration api - MonitoringMigrateAlertsResponse response = client().execute(MonitoringMigrateAlertsAction.INSTANCE, - new MonitoringMigrateAlertsRequest()).actionGet(); + MonitoringMigrateAlertsResponse response = client().execute( + MonitoringMigrateAlertsAction.INSTANCE, + new MonitoringMigrateAlertsRequest() + ).actionGet(); // check that migration failed due to monitoring cluster not responding assertThat(response.getExporters().size(), is(1)); @@ -412,8 +437,10 @@ public void testRemoteAlertsRemovalFailure() throws Exception { enqueueResponse(webServer, 500, "{\"error\":{}}"); // call migration api - MonitoringMigrateAlertsResponse response = client().execute(MonitoringMigrateAlertsAction.INSTANCE, - new MonitoringMigrateAlertsRequest()).actionGet(); + MonitoringMigrateAlertsResponse response = client().execute( + MonitoringMigrateAlertsAction.INSTANCE, + new MonitoringMigrateAlertsRequest() + ).actionGet(); // check that an error is reported while trying to remove a remote watch assertThat(response.getExporters().size(), is(1)); @@ -422,8 +449,10 @@ public void testRemoteAlertsRemovalFailure() throws Exception { assertThat(localExporterResult.getType(), is(HttpExporter.TYPE)); assertThat(localExporterResult.isMigrationComplete(), is(false)); assertThat(localExporterResult.getReason().getMessage(), startsWith("method [DELETE], host [")); - assertThat(localExporterResult.getReason().getMessage(), - endsWith("status line [HTTP/1.1 500 Internal Server Error]\n{\"error\":{}}")); + assertThat( + localExporterResult.getReason().getMessage(), + endsWith("status line [HTTP/1.1 500 Internal Server Error]\n{\"error\":{}}") + ); } finally { stopMonitoring(); @@ -450,8 +479,10 @@ public void testRemoteAlertsRemoteDisallowsWatcher() throws Exception { enqueueWatcherResponses(webServer, false); // call migration api - MonitoringMigrateAlertsResponse response = client().execute(MonitoringMigrateAlertsAction.INSTANCE, - new MonitoringMigrateAlertsRequest()).actionGet(); + MonitoringMigrateAlertsResponse response = client().execute( + MonitoringMigrateAlertsAction.INSTANCE, + new MonitoringMigrateAlertsRequest() + ).actionGet(); // Migration is marked as complete since watcher is disabled on remote cluster. assertThat(response.getExporters().size(), is(1)); @@ -521,8 +552,7 @@ private void assertWatchesExist(boolean exist) { } protected List monitoringTemplateNames() { - return Arrays.stream(MonitoringTemplateRegistry.TEMPLATE_NAMES) - .collect(Collectors.toList()); + return Arrays.stream(MonitoringTemplateRegistry.TEMPLATE_NAMES).collect(Collectors.toList()); } private void enqueueWatcherResponses(final MockWebServer webServer, final boolean remoteClusterAllowsWatcher) throws IOException { @@ -577,7 +607,7 @@ private String resourceClusterAlertQueryString() { } private void assertMonitorWatches(final MockWebServer webServer, final boolean remoteClusterAllowsWatcher) { - MockRequest request = webServer.takeRequest(); + MockRequest request = webServer.takeRequest(); // GET /_xpack assertThat(request.getMethod(), equalTo("GET")); diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/cleaner/AbstractIndicesCleanerTestCase.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/cleaner/AbstractIndicesCleanerTestCase.java index 4a90309dba0cd..a78e7baab75f4 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/cleaner/AbstractIndicesCleanerTestCase.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/cleaner/AbstractIndicesCleanerTestCase.java @@ -35,7 +35,7 @@ public abstract class AbstractIndicesCleanerTestCase extends MonitoringIntegTest public void setup() { internalCluster().startNode(); - //Set max retention time to avoid any accidental cleanups + // Set max retention time to avoid any accidental cleanups CleanerService cleanerService = internalCluster().getInstance(CleanerService.class, internalCluster().getMasterName()); cleanerService.setGlobalRetention(TimeValue.MAX_VALUE); } @@ -107,7 +107,7 @@ public void testIgnoreCurrentTimestampedIndex() throws Exception { assertIndicesCount(1); } - @AwaitsFix(bugUrl="https://github.com/elastic/elasticsearch/issues/78862") + @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/78862") public void testDeleteIndices() throws Exception { CleanerService.Listener listener = getListener(); @@ -147,8 +147,9 @@ public void testDeleteIndices() throws Exception { public void testRetentionAsGlobalSetting() throws Exception { final int max = 10; final int retention = randomIntBetween(1, max); - internalCluster().startNode(Settings.builder().put(MonitoringField.HISTORY_DURATION.getKey(), - String.format(Locale.ROOT, "%dd", retention))); + internalCluster().startNode( + Settings.builder().put(MonitoringField.HISTORY_DURATION.getKey(), String.format(Locale.ROOT, "%dd", retention)) + ); final ZonedDateTime now = now(); for (int i = 0; i < max; i++) { @@ -235,7 +236,7 @@ protected static TimeValue years(int years) { protected static TimeValue months(int months) { ZonedDateTime now = now(); - return TimeValue.timeValueMillis(now.toInstant().toEpochMilli() - now.minusMonths(months).toInstant().toEpochMilli()); + return TimeValue.timeValueMillis(now.toInstant().toEpochMilli() - now.minusMonths(months).toInstant().toEpochMilli()); } protected static TimeValue days(int days) { diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/cleaner/CleanerServiceTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/cleaner/CleanerServiceTests.java index 8563bd55e72ab..52727ec98ac62 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/cleaner/CleanerServiceTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/cleaner/CleanerServiceTests.java @@ -59,8 +59,10 @@ public void testConstructorWithInvalidRetention() { new CleanerService(settings, clusterSettings, threadPool, licenseState); } finally { - assertWarnings("[xpack.monitoring.history.duration] setting was deprecated in Elasticsearch and will be removed in " + - "a future release! See the breaking changes documentation for the next major version."); + assertWarnings( + "[xpack.monitoring.history.duration] setting was deprecated in Elasticsearch and will be removed in " + + "a future release! See the breaking changes documentation for the next major version." + ); } } @@ -70,8 +72,10 @@ public void testGetRetention() { assertEquals(expected, new CleanerService(settings, clusterSettings, threadPool, licenseState).getRetention()); - assertWarnings("[xpack.monitoring.history.duration] setting was deprecated in Elasticsearch and will be removed in " + - "a future release! See the breaking changes documentation for the next major version."); + assertWarnings( + "[xpack.monitoring.history.duration] setting was deprecated in Elasticsearch and will be removed in " + + "a future release! See the breaking changes documentation for the next major version." + ); } public void testSetGlobalRetention() { @@ -87,7 +91,7 @@ public void testSetGlobalRetention() { public void testNextExecutionDelay() { CleanerService.ExecutionScheduler scheduler = new CleanerService.DefaultExecutionScheduler(); - ZonedDateTime now = ZonedDateTime.of(2015, 1, 1, 0, 0,0,0, ZoneOffset.UTC); + ZonedDateTime now = ZonedDateTime.of(2015, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC); assertThat(scheduler.nextExecutionDelay(now).millis(), equalTo(TimeValue.timeValueHours(1).millis())); now = ZonedDateTime.of(2015, 1, 1, 1, 0, 0, 0, ZoneOffset.UTC); @@ -101,8 +105,7 @@ public void testNextExecutionDelay() { ZoneId defaultZone = Clock.systemDefaultZone().getZone(); now = ZonedDateTime.of(2015, 1, 1, 12, 34, 56, 0, defaultZone); - long nextScheduledMillis = ZonedDateTime.of(2015, 1, 2, 1, 0, 0,0, - defaultZone).toInstant().toEpochMilli(); + long nextScheduledMillis = ZonedDateTime.of(2015, 1, 2, 1, 0, 0, 0, defaultZone).toInstant().toEpochMilli(); assertThat(scheduler.nextExecutionDelay(now).millis(), equalTo(nextScheduledMillis - now.toInstant().toEpochMilli())); } @@ -113,8 +116,13 @@ public void testExecution() throws InterruptedException { logger.debug("--> creates a cleaner service that cleans every second"); XPackLicenseState licenseState = mock(XPackLicenseState.class); - CleanerService service = new CleanerService(Settings.EMPTY, clusterSettings, licenseState, threadPool, - new TestExecutionScheduler(1_000)); + CleanerService service = new CleanerService( + Settings.EMPTY, + clusterSettings, + licenseState, + threadPool, + new TestExecutionScheduler(1_000) + ); logger.debug("--> registers cleaning listener"); TestListener listener = new TestListener(latch); diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/cleaner/local/LocalIndicesCleanerTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/cleaner/local/LocalIndicesCleanerTests.java index 858fa4e3cf523..0e4999057bd04 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/cleaner/local/LocalIndicesCleanerTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/cleaner/local/LocalIndicesCleanerTests.java @@ -35,27 +35,30 @@ protected Collection> nodePlugins() { @Override protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { return Settings.builder() - .put(super.nodeSettings(nodeOrdinal, otherSettings)) - .put("xpack.monitoring.exporters._local.type", LocalExporter.TYPE) - .build(); + .put(super.nodeSettings(nodeOrdinal, otherSettings)) + .put("xpack.monitoring.exporters._local.type", LocalExporter.TYPE) + .build(); } @Override protected void createIndex(String name, ZonedDateTime creationDate) { long creationMillis = creationDate.toInstant().toEpochMilli(); - assertAcked(prepareCreate(name) - .setSettings(Settings.builder().put(IndexMetadata.SETTING_CREATION_DATE, creationMillis).build())); + assertAcked(prepareCreate(name).setSettings(Settings.builder().put(IndexMetadata.SETTING_CREATION_DATE, creationMillis).build())); } @Override protected void assertIndicesCount(int count) throws Exception { assertBusy(() -> { - //we set ignore_unavailable to true for this request as the monitoring index gets deleted concurrently with this assertion - //in some cases. When the plugin security is enabled, it expands wildcards to the existing index, which then gets deleted, - //so when es core gets the request with the explicit index name, it throws an index not found exception as that index - //doesn't exist anymore. If we ignore unavailable instead no error will be thrown. - GetSettingsResponse getSettingsResponse = client().admin().indices().prepareGetSettings().addIndices(".monitoring-*") - .setIndicesOptions(IndicesOptions.fromOptions(true, true, true, true, true)).get(); + // we set ignore_unavailable to true for this request as the monitoring index gets deleted concurrently with this assertion + // in some cases. When the plugin security is enabled, it expands wildcards to the existing index, which then gets deleted, + // so when es core gets the request with the explicit index name, it throws an index not found exception as that index + // doesn't exist anymore. If we ignore unavailable instead no error will be thrown. + GetSettingsResponse getSettingsResponse = client().admin() + .indices() + .prepareGetSettings() + .addIndices(".monitoring-*") + .setIndicesOptions(IndicesOptions.fromOptions(true, true, true, true, true)) + .get(); Iterator indices = getSettingsResponse.getIndexToSettings().keysIt(); List collectedIndices = new ArrayList<>(); while (indices.hasNext()) { diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/cluster/ClusterStatsCollectorTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/cluster/ClusterStatsCollectorTests.java index 8d669d3ddc955..20363a6a25468 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/cluster/ClusterStatsCollectorTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/cluster/ClusterStatsCollectorTests.java @@ -71,17 +71,27 @@ public void setUp() throws Exception { } public void testShouldCollectReturnsFalseIfNotMaster() { - final ClusterStatsCollector collector = - new ClusterStatsCollector(Settings.EMPTY, clusterService, licenseState, client, licenseService, - TestIndexNameExpressionResolver.newInstance()); + final ClusterStatsCollector collector = new ClusterStatsCollector( + Settings.EMPTY, + clusterService, + licenseState, + client, + licenseService, + TestIndexNameExpressionResolver.newInstance() + ); assertThat(collector.shouldCollect(false), is(false)); } public void testShouldCollectReturnsTrue() { - final ClusterStatsCollector collector = - new ClusterStatsCollector(Settings.EMPTY, clusterService, licenseState, client, licenseService, - TestIndexNameExpressionResolver.newInstance()); + final ClusterStatsCollector collector = new ClusterStatsCollector( + Settings.EMPTY, + clusterService, + licenseState, + client, + licenseService, + TestIndexNameExpressionResolver.newInstance() + ); assertThat(collector.shouldCollect(true), is(true)); } @@ -92,8 +102,14 @@ public void testDoAPMIndicesExistReturnsBasedOnIndices() { final IndexNameExpressionResolver resolver = mock(IndexNameExpressionResolver.class); when(resolver.concreteIndices(clusterState, IndicesOptions.lenientExpandOpen(), "apm-*")).thenReturn(indices); - final ClusterStatsCollector collector = - new ClusterStatsCollector(Settings.EMPTY, clusterService, licenseState, client, licenseService, resolver); + final ClusterStatsCollector collector = new ClusterStatsCollector( + Settings.EMPTY, + clusterService, + licenseState, + client, + licenseService, + resolver + ); assertThat(collector.doAPMIndicesExist(clusterState), is(apmIndicesExist)); } @@ -103,8 +119,14 @@ public void testDoAPMIndicesExistReturnsFalseForExpectedExceptions() { final IndexNameExpressionResolver resolver = mock(IndexNameExpressionResolver.class); when(resolver.concreteIndices(clusterState, IndicesOptions.lenientExpandOpen(), "apm-*")).thenThrow(exception); - final ClusterStatsCollector collector = - new ClusterStatsCollector(Settings.EMPTY, clusterService, licenseState, client, licenseService, resolver); + final ClusterStatsCollector collector = new ClusterStatsCollector( + Settings.EMPTY, + clusterService, + licenseState, + client, + licenseService, + resolver + ); assertThat(collector.doAPMIndicesExist(clusterState), is(false)); } @@ -114,16 +136,24 @@ public void testDoAPMIndicesExistRethrowsUnexpectedExceptions() { final IndexNameExpressionResolver resolver = mock(IndexNameExpressionResolver.class); when(resolver.concreteIndices(clusterState, IndicesOptions.lenientExpandOpen(), "apm-*")).thenThrow(exception); - final ClusterStatsCollector collector = - new ClusterStatsCollector(Settings.EMPTY, clusterService, licenseState, client, licenseService, resolver); + final ClusterStatsCollector collector = new ClusterStatsCollector( + Settings.EMPTY, + clusterService, + licenseState, + client, + licenseService, + resolver + ); expectThrows(RuntimeException.class, () -> collector.doAPMIndicesExist(clusterState)); } public void testDoCollect() throws Exception { final Settings.Builder settings = Settings.builder(); - final License.OperationMode mode = - randomValueOtherThan(License.OperationMode.MISSING, () -> randomFrom(License.OperationMode.values())); + final License.OperationMode mode = randomValueOtherThan( + License.OperationMode.MISSING, + () -> randomFrom(License.OperationMode.values()) + ); final boolean securityEnabled = randomBoolean(); final boolean transportTLSEnabled; @@ -169,15 +199,15 @@ public void testDoCollect() throws Exception { final MonitoringDoc.Node node = MonitoringTestUtils.randomMonitoringNode(random()); final License license = License.builder() - .uid(UUID.randomUUID().toString()) - .type(mode.name().toLowerCase(Locale.ROOT)) - .issuer("elasticsearch") - .issuedTo("elastic") - .issueDate(System.currentTimeMillis()) - .expiryDate(System.currentTimeMillis() + TimeValue.timeValueHours(24L).getMillis()) - .maxNodes(License.OperationMode.ENTERPRISE == mode ? -1 : randomIntBetween(1, 10)) - .maxResourceUnits(License.OperationMode.ENTERPRISE == mode ? randomIntBetween(10, 99) : -1) - .build(); + .uid(UUID.randomUUID().toString()) + .type(mode.name().toLowerCase(Locale.ROOT)) + .issuer("elasticsearch") + .issuedTo("elastic") + .issueDate(System.currentTimeMillis()) + .expiryDate(System.currentTimeMillis() + TimeValue.timeValueHours(24L).getMillis()) + .maxNodes(License.OperationMode.ENTERPRISE == mode ? -1 : randomIntBetween(1, 10)) + .maxResourceUnits(License.OperationMode.ENTERPRISE == mode ? randomIntBetween(10, 99) : -1) + .build(); when(licenseService.getLicense()).thenReturn(license); final ClusterStatsResponse mockClusterStatsResponse = mock(ClusterStatsResponse.class); @@ -208,20 +238,23 @@ public void testDoCollect() throws Exception { final IndexNameExpressionResolver indexNameExpressionResolver = mock(IndexNameExpressionResolver.class); final boolean apmIndicesExist = randomBoolean(); final Index[] indices = new Index[apmIndicesExist ? randomIntBetween(1, 5) : 0]; - when(indexNameExpressionResolver.concreteIndices(clusterState, IndicesOptions.lenientExpandOpen(), "apm-*")) - .thenReturn(indices); + when(indexNameExpressionResolver.concreteIndices(clusterState, IndicesOptions.lenientExpandOpen(), "apm-*")).thenReturn(indices); - final XPackUsageResponse xPackUsageResponse = new XPackUsageResponse( - singletonList(new MonitoringFeatureSetUsage(false, null))); + final XPackUsageResponse xPackUsageResponse = new XPackUsageResponse(singletonList(new MonitoringFeatureSetUsage(false, null))); @SuppressWarnings("unchecked") final ActionFuture xPackUsageFuture = (ActionFuture) mock(ActionFuture.class); when(client.execute(same(XPackUsageAction.INSTANCE), any(XPackUsageRequest.class))).thenReturn(xPackUsageFuture); when(xPackUsageFuture.actionGet()).thenReturn(xPackUsageResponse); - final ClusterStatsCollector collector = - new ClusterStatsCollector(settings.build(), clusterService, licenseState, - client, licenseService, indexNameExpressionResolver); + final ClusterStatsCollector collector = new ClusterStatsCollector( + settings.build(), + clusterService, + licenseState, + client, + licenseService, + indexNameExpressionResolver + ); Assert.assertEquals(timeout, collector.getCollectionTimeout()); @@ -248,9 +281,10 @@ public void testDoCollect() throws Exception { assertThat(document.getStatus(), equalTo(clusterStatus)); final boolean securitySettingDefined = settings.build().hasValue(XPackSettings.SECURITY_ENABLED.getKey()); - assertThat(document.getClusterNeedsTLSEnabled(), - equalTo(mode == License.OperationMode.TRIAL && securitySettingDefined && securityEnabled - && transportTLSEnabled == false)); + assertThat( + document.getClusterNeedsTLSEnabled(), + equalTo(mode == License.OperationMode.TRIAL && securitySettingDefined && securityEnabled && transportTLSEnabled == false) + ); assertThat(document.getClusterStats(), notNullValue()); assertThat(document.getClusterStats().getStatus(), equalTo(clusterStatus)); @@ -270,8 +304,10 @@ public void testDoCollect() throws Exception { verify(clusterAdminClient).prepareClusterStats(); verify(client).execute(same(XPackUsageAction.INSTANCE), any(XPackUsageRequest.class)); - assertWarnings("[xpack.monitoring.collection.cluster.stats.timeout] setting was deprecated in Elasticsearch and will be removed " + - "in a future release! See the breaking changes documentation for the next major version."); + assertWarnings( + "[xpack.monitoring.collection.cluster.stats.timeout] setting was deprecated in Elasticsearch and will be removed " + + "in a future release! See the breaking changes documentation for the next major version." + ); } public void testDoCollectNoLicense() throws Exception { @@ -287,8 +323,9 @@ public void testDoCollectNoLicense() throws Exception { final IndexNameExpressionResolver indexNameExpressionResolver; { indexNameExpressionResolver = mock(IndexNameExpressionResolver.class); - when(indexNameExpressionResolver.concreteIndices(clusterState, IndicesOptions.lenientExpandOpen(), "apm-*")) - .thenReturn(Index.EMPTY_ARRAY); + when(indexNameExpressionResolver.concreteIndices(clusterState, IndicesOptions.lenientExpandOpen(), "apm-*")).thenReturn( + Index.EMPTY_ARRAY + ); } final Client client = mock(Client.class); @@ -314,8 +351,7 @@ public void testDoCollectNoLicense() throws Exception { when(adminClient.cluster()).thenReturn(clusterAdminClient); when(client.admin()).thenReturn(adminClient); - final XPackUsageResponse xPackUsageResponse = new XPackUsageResponse( - singletonList(new MonitoringFeatureSetUsage(false, null))); + final XPackUsageResponse xPackUsageResponse = new XPackUsageResponse(singletonList(new MonitoringFeatureSetUsage(false, null))); @SuppressWarnings("unchecked") final ActionFuture xPackUsageFuture = (ActionFuture) mock(ActionFuture.class); when(client.execute(same(XPackUsageAction.INSTANCE), any(XPackUsageRequest.class))).thenReturn(xPackUsageFuture); @@ -326,16 +362,23 @@ public void testDoCollectNoLicense() throws Exception { final Settings.Builder settings = Settings.builder(); final MonitoringDoc.Node node = MonitoringTestUtils.randomMonitoringNode(random()); - final ClusterStatsCollector collector = - new ClusterStatsCollector(settings.build(), clusterService, licenseState, - client, licenseService, indexNameExpressionResolver); + final ClusterStatsCollector collector = new ClusterStatsCollector( + settings.build(), + clusterService, + licenseState, + client, + licenseService, + indexNameExpressionResolver + ); final Collection results = collector.doCollect(node, interval, clusterState); assertEquals(1, results.size()); final ClusterStatsMonitoringDoc doc = (ClusterStatsMonitoringDoc) results.iterator().next(); assertThat(doc.getLicense(), nullValue()); - assertWarnings("[xpack.monitoring.collection.cluster.stats.timeout] setting was deprecated in Elasticsearch and will be removed " + - "in a future release! See the breaking changes documentation for the next major version."); + assertWarnings( + "[xpack.monitoring.collection.cluster.stats.timeout] setting was deprecated in Elasticsearch and will be removed " + + "in a future release! See the breaking changes documentation for the next major version." + ); } public void testDoCollectThrowsTimeoutException() throws Exception { @@ -351,8 +394,9 @@ public void testDoCollectThrowsTimeoutException() throws Exception { final IndexNameExpressionResolver indexNameExpressionResolver; { indexNameExpressionResolver = mock(IndexNameExpressionResolver.class); - when(indexNameExpressionResolver.concreteIndices(clusterState, IndicesOptions.lenientExpandOpen(), "apm-*")) - .thenReturn(Index.EMPTY_ARRAY); + when(indexNameExpressionResolver.concreteIndices(clusterState, IndicesOptions.lenientExpandOpen(), "apm-*")).thenReturn( + Index.EMPTY_ARRAY + ); } final Client client = mock(Client.class); @@ -361,8 +405,9 @@ public void testDoCollectThrowsTimeoutException() throws Exception { final ClusterHealthStatus clusterStatus = randomFrom(ClusterHealthStatus.values()); when(mockClusterStatsResponse.getStatus()).thenReturn(clusterStatus); when(mockClusterStatsResponse.getNodesStats()).thenReturn(mock(ClusterStatsNodes.class)); - when(mockClusterStatsResponse.failures()).thenReturn(List.of(new FailedNodeException("node", "msg", - new ElasticsearchTimeoutException("timed out")))); + when(mockClusterStatsResponse.failures()).thenReturn( + List.of(new FailedNodeException("node", "msg", new ElasticsearchTimeoutException("timed out"))) + ); final ClusterStatsIndices mockClusterStatsIndices = mock(ClusterStatsIndices.class); @@ -385,13 +430,20 @@ public void testDoCollectThrowsTimeoutException() throws Exception { final Settings.Builder settings = Settings.builder(); final MonitoringDoc.Node node = MonitoringTestUtils.randomMonitoringNode(random()); - final ClusterStatsCollector collector = - new ClusterStatsCollector(settings.build(), clusterService, licenseState, - client, licenseService, indexNameExpressionResolver); + final ClusterStatsCollector collector = new ClusterStatsCollector( + settings.build(), + clusterService, + licenseState, + client, + licenseService, + indexNameExpressionResolver + ); expectThrows(ElasticsearchTimeoutException.class, () -> collector.doCollect(node, interval, clusterState)); - assertWarnings("[xpack.monitoring.collection.cluster.stats.timeout] setting was deprecated in Elasticsearch and will be removed " + - "in a future release! See the breaking changes documentation for the next major version."); + assertWarnings( + "[xpack.monitoring.collection.cluster.stats.timeout] setting was deprecated in Elasticsearch and will be removed " + + "in a future release! See the breaking changes documentation for the next major version." + ); } } diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/cluster/ClusterStatsMonitoringDocTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/cluster/ClusterStatsMonitoringDocTests.java index d166d37bbf043..6ee6e2dd24e9c 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/cluster/ClusterStatsMonitoringDocTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/cluster/ClusterStatsMonitoringDocTests.java @@ -36,9 +36,8 @@ import org.elasticsearch.common.transport.TransportAddress; import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.discovery.DiscoveryModule; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.license.License; @@ -52,6 +51,7 @@ import org.elasticsearch.plugins.PluginType; import org.elasticsearch.test.VersionUtils; import org.elasticsearch.transport.TransportInfo; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.XPackFeatureSet; import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; import org.elasticsearch.xpack.core.monitoring.MonitoringFeatureSetUsage; @@ -101,33 +101,50 @@ public void setUp() throws Exception { clusterState = mock(ClusterState.class); final License.OperationMode operationMode = randomFrom(License.OperationMode.values()); license = License.builder() - .uid(randomAlphaOfLength(5)) - .type(operationMode.name().toLowerCase(Locale.ROOT)) - .issuer(randomAlphaOfLength(5)) - .issuedTo(randomAlphaOfLength(5)) - .issueDate(timestamp) - .expiryDate(timestamp + randomIntBetween(1, 10) * 1_000L) - .maxNodes(License.OperationMode.ENTERPRISE == operationMode ? -1 : randomIntBetween(1, 5)) - .maxResourceUnits(License.OperationMode.ENTERPRISE == operationMode ? randomIntBetween(1, 42) : -1) - .build(); + .uid(randomAlphaOfLength(5)) + .type(operationMode.name().toLowerCase(Locale.ROOT)) + .issuer(randomAlphaOfLength(5)) + .issuedTo(randomAlphaOfLength(5)) + .issueDate(timestamp) + .expiryDate(timestamp + randomIntBetween(1, 10) * 1_000L) + .maxNodes(License.OperationMode.ENTERPRISE == operationMode ? -1 : randomIntBetween(1, 5)) + .maxResourceUnits(License.OperationMode.ENTERPRISE == operationMode ? randomIntBetween(1, 42) : -1) + .build(); final DiscoveryNode masterNode = masterNode(); - final DiscoveryNodes.Builder builder = - DiscoveryNodes.builder() - .masterNodeId(masterNode.getId()) - .localNodeId(masterNode.getId()) - .add(masterNode); + final DiscoveryNodes.Builder builder = DiscoveryNodes.builder() + .masterNodeId(masterNode.getId()) + .localNodeId(masterNode.getId()) + .add(masterNode); when(clusterState.nodes()).thenReturn(builder.build()); } @Override - protected ClusterStatsMonitoringDoc createMonitoringDoc(String cluster, long timestamp, long interval, MonitoringDoc.Node node, - MonitoredSystem system, String type, String id) { - return new ClusterStatsMonitoringDoc(cluster, timestamp, interval, node, - clusterName, version, clusterStatus, license, - apmIndicesExist, usages, clusterStats, clusterState, - needToEnableTLS); + protected ClusterStatsMonitoringDoc createMonitoringDoc( + String cluster, + long timestamp, + long interval, + MonitoringDoc.Node node, + MonitoredSystem system, + String type, + String id + ) { + return new ClusterStatsMonitoringDoc( + cluster, + timestamp, + interval, + node, + clusterName, + version, + clusterStatus, + license, + apmIndicesExist, + usages, + clusterStats, + clusterState, + needToEnableTLS + ); } @Override @@ -147,48 +164,88 @@ protected void assertMonitoringDoc(final ClusterStatsMonitoringDoc document) { } public void testConstructorClusterNameMustNotBeNull() { - expectThrows(NullPointerException.class, - () -> new ClusterStatsMonitoringDoc(cluster, timestamp, interval, node, - null, version, clusterStatus, license, apmIndicesExist, usages, clusterStats, clusterState, - needToEnableTLS)); + expectThrows( + NullPointerException.class, + () -> new ClusterStatsMonitoringDoc( + cluster, + timestamp, + interval, + node, + null, + version, + clusterStatus, + license, + apmIndicesExist, + usages, + clusterStats, + clusterState, + needToEnableTLS + ) + ); } public void testConstructorVersionMustNotBeNull() { - expectThrows(NullPointerException.class, - () -> new ClusterStatsMonitoringDoc(cluster, timestamp, interval, node, - clusterName, null, clusterStatus, license, apmIndicesExist, usages, clusterStats, clusterState, - needToEnableTLS)); + expectThrows( + NullPointerException.class, + () -> new ClusterStatsMonitoringDoc( + cluster, + timestamp, + interval, + node, + clusterName, + null, + clusterStatus, + license, + apmIndicesExist, + usages, + clusterStats, + clusterState, + needToEnableTLS + ) + ); } public void testConstructorClusterHealthStatusMustNotBeNull() { - expectThrows(NullPointerException.class, - () -> new ClusterStatsMonitoringDoc(cluster, timestamp, interval, node, - clusterName, version, null, license, apmIndicesExist, usages, clusterStats, clusterState, - needToEnableTLS)); + expectThrows( + NullPointerException.class, + () -> new ClusterStatsMonitoringDoc( + cluster, + timestamp, + interval, + node, + clusterName, + version, + null, + license, + apmIndicesExist, + usages, + clusterStats, + clusterState, + needToEnableTLS + ) + ); } public void testNodesHash() { final int nodeCount = randomIntBetween(0, 5); final Map emptyMap = emptyMap(); final DiscoveryNode masterNode = masterNode(); - final DiscoveryNodes.Builder builder = - DiscoveryNodes.builder() - .masterNodeId(masterNode.getId()) - .localNodeId(masterNode.getId()); + final DiscoveryNodes.Builder builder = DiscoveryNodes.builder().masterNodeId(masterNode.getId()).localNodeId(masterNode.getId()); for (int i = 0; i < nodeCount; ++i) { builder.add( - new DiscoveryNode(randomAlphaOfLength(5), - randomAlphaOfLength(2 + i), - randomAlphaOfLength(5), - randomAlphaOfLength(5), - randomAlphaOfLength(5), - new TransportAddress(TransportAddress.META_ADDRESS, 9301 + i), - randomBoolean() ? singletonMap("attr", randomAlphaOfLength(3)) : emptyMap, - singleton(randomValueOtherThan( - DiscoveryNodeRole.VOTING_ONLY_NODE_ROLE, () -> randomFrom(DiscoveryNodeRole.roles())) - ), - Version.CURRENT)); + new DiscoveryNode( + randomAlphaOfLength(5), + randomAlphaOfLength(2 + i), + randomAlphaOfLength(5), + randomAlphaOfLength(5), + randomAlphaOfLength(5), + new TransportAddress(TransportAddress.META_ADDRESS, 9301 + i), + randomBoolean() ? singletonMap("attr", randomAlphaOfLength(3)) : emptyMap, + singleton(randomValueOtherThan(DiscoveryNodeRole.VOTING_ONLY_NODE_ROLE, () -> randomFrom(DiscoveryNodeRole.roles()))), + Version.CURRENT + ) + ); } final DiscoveryNodes nodes = builder.build(); @@ -206,41 +263,39 @@ public void testToXContent() throws IOException { final String clusterUuid = "_cluster"; final ClusterName clusterName = new ClusterName("_cluster_name"); final TransportAddress transportAddress = new TransportAddress(TransportAddress.META_ADDRESS, 9300); - final DiscoveryNode discoveryNode = new DiscoveryNode("_node_name", - "_node_id", - "_ephemeral_id", - "_host_name", - "_host_address", - transportAddress, - singletonMap("attr", "value"), - singleton(DiscoveryNodeRole.MASTER_ROLE), - Version.CURRENT); + final DiscoveryNode discoveryNode = new DiscoveryNode( + "_node_name", + "_node_id", + "_ephemeral_id", + "_host_name", + "_host_address", + transportAddress, + singletonMap("attr", "value"), + singleton(DiscoveryNodeRole.MASTER_ROLE), + Version.CURRENT + ); final ClusterState clusterState = ClusterState.builder(clusterName) - .metadata(Metadata.builder() - .clusterUUID(clusterUuid) - .transientSettings(Settings.builder() - .put("cluster.metadata.display_name", "my_prod_cluster") - .build()) - .build()) - .stateUUID("_state_uuid") - .version(12L) - .nodes(DiscoveryNodes.builder() - .masterNodeId("_node") - .localNodeId("_node") - .add(discoveryNode) - .build()) - .build(); + .metadata( + Metadata.builder() + .clusterUUID(clusterUuid) + .transientSettings(Settings.builder().put("cluster.metadata.display_name", "my_prod_cluster").build()) + .build() + ) + .stateUUID("_state_uuid") + .version(12L) + .nodes(DiscoveryNodes.builder().masterNodeId("_node").localNodeId("_node").add(discoveryNode).build()) + .build(); final License license = License.builder() - .uid("442ca961-9c00-4bb2-b5c9-dfaacd547403") - .type("trial") - .issuer("elasticsearch") - .issuedTo("customer") - .issueDate(1451606400000L) - .expiryDate(1502107402133L) - .maxNodes(2) - .build(); + .uid("442ca961-9c00-4bb2-b5c9-dfaacd547403") + .type("trial") + .issuer("elasticsearch") + .issuedTo("customer") + .issueDate(1451606400000L) + .expiryDate(1502107402133L) + .maxNodes(2) + .build(); final List usages = singletonList(new MonitoringFeatureSetUsage(false, null)); @@ -252,18 +307,31 @@ public void testToXContent() throws IOException { final TransportInfo mockTransportInfo = mock(TransportInfo.class); when(mockNodeInfo.getInfo(TransportInfo.class)).thenReturn(mockTransportInfo); - final BoundTransportAddress bound = new BoundTransportAddress(new TransportAddress[]{transportAddress}, transportAddress); + final BoundTransportAddress bound = new BoundTransportAddress(new TransportAddress[] { transportAddress }, transportAddress); when(mockTransportInfo.address()).thenReturn(bound); - when(mockNodeInfo.getSettings()).thenReturn(Settings.builder() - .put(NetworkModule.TRANSPORT_TYPE_KEY, "_transport") - .put(NetworkModule.HTTP_TYPE_KEY, "_http") - .put(DiscoveryModule.DISCOVERY_TYPE_SETTING.getKey(), "_disco") - .build()); + when(mockNodeInfo.getSettings()).thenReturn( + Settings.builder() + .put(NetworkModule.TRANSPORT_TYPE_KEY, "_transport") + .put(NetworkModule.HTTP_TYPE_KEY, "_http") + .put(DiscoveryModule.DISCOVERY_TYPE_SETTING.getKey(), "_disco") + .build() + ); final PluginsAndModules mockPluginsAndModules = mock(PluginsAndModules.class); when(mockNodeInfo.getInfo(PluginsAndModules.class)).thenReturn(mockPluginsAndModules); - final PluginInfo pluginInfo = new PluginInfo("_plugin", "_plugin_desc", "_plugin_version", Version.CURRENT, - "1.8", "_plugin_class", Collections.emptyList(), false, PluginType.ISOLATED, "", false); + final PluginInfo pluginInfo = new PluginInfo( + "_plugin", + "_plugin_desc", + "_plugin_version", + Version.CURRENT, + "1.8", + "_plugin_class", + Collections.emptyList(), + false, + PluginType.ISOLATED, + "", + false + ); when(mockPluginsAndModules.getPluginInfos()).thenReturn(singletonList(pluginInfo)); final OsInfo mockOsInfo = mock(OsInfo.class); @@ -293,7 +361,7 @@ public void testToXContent() throws IOException { final FsInfo mockFsInfo = mock(FsInfo.class); when(mockNodeStats.getFs()).thenReturn(mockFsInfo); - when(mockFsInfo.getTotal()).thenReturn(new FsInfo.Path("_fs_path","_fs_mount", 100L, 49L, 51L)); + when(mockFsInfo.getTotal()).thenReturn(new FsInfo.Path("_fs_path", "_fs_mount", 100L, 49L, 51L)); final OsStats mockOsStats = mock(OsStats.class); when(mockNodeStats.getOs()).thenReturn(mockOsStats); @@ -319,8 +387,12 @@ public void testToXContent() throws IOException { final ShardId shardId = new ShardId("_index", "_index_id", 7); final UnassignedInfo unassignedInfo = new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "_message"); - final ShardRouting shardRouting = ShardRouting.newUnassigned(shardId, true, - RecoverySource.ExistingStoreRecoverySource.INSTANCE, unassignedInfo); + final ShardRouting shardRouting = ShardRouting.newUnassigned( + shardId, + true, + RecoverySource.ExistingStoreRecoverySource.INSTANCE, + unassignedInfo + ); final ShardStats mockShardStats = mock(ShardStats.class); when(mockShardStats.getShardRouting()).thenReturn(shardRouting); @@ -330,33 +402,37 @@ public void testToXContent() throws IOException { when(mockNodeResponse.clusterStatus()).thenReturn(ClusterHealthStatus.RED); when(mockNodeResponse.nodeInfo()).thenReturn(mockNodeInfo); when(mockNodeResponse.nodeStats()).thenReturn(mockNodeStats); - when(mockNodeResponse.shardsStats()).thenReturn(new ShardStats[]{mockShardStats}); + when(mockNodeResponse.shardsStats()).thenReturn(new ShardStats[] { mockShardStats }); final Metadata metadata = clusterState.metadata(); - final ClusterStatsResponse clusterStats = new ClusterStatsResponse(1451606400000L, - "_cluster", - clusterName, - singletonList(mockNodeResponse), - emptyList(), - MappingStats.of(metadata, () -> {}), - AnalysisStats.of(metadata, () -> {}), - VersionStats.of(metadata, singletonList(mockNodeResponse))); + final ClusterStatsResponse clusterStats = new ClusterStatsResponse( + 1451606400000L, + "_cluster", + clusterName, + singletonList(mockNodeResponse), + emptyList(), + MappingStats.of(metadata, () -> {}), + AnalysisStats.of(metadata, () -> {}), + VersionStats.of(metadata, singletonList(mockNodeResponse)) + ); final MonitoringDoc.Node node = new MonitoringDoc.Node("_uuid", "_host", "_addr", "_ip", "_name", 1504169190855L); - final ClusterStatsMonitoringDoc doc = new ClusterStatsMonitoringDoc("_cluster", - 1502107402133L, - 1506593717631L, - node, - clusterName.value(), - "_version", - ClusterHealthStatus.GREEN, - license, - apmIndicesExist, - usages, - clusterStats, - clusterState, - needToEnableTLS); + final ClusterStatsMonitoringDoc doc = new ClusterStatsMonitoringDoc( + "_cluster", + 1502107402133L, + 1506593717631L, + node, + clusterName.value(), + "_version", + ClusterHealthStatus.GREEN, + license, + apmIndicesExist, + usages, + clusterStats, + clusterState, + needToEnableTLS + ); final BytesReference xContent = XContentHelper.toXContent(doc, XContentType.JSON, false); final String expectedJson = String.format( @@ -646,15 +722,17 @@ public void testToXContent() throws IOException { } private DiscoveryNode masterNode() { - return new DiscoveryNode("_node_name", - "_node_id", - "_ephemeral_id", - "_host_name", - "_host_address", - new TransportAddress(TransportAddress.META_ADDRESS, 9300), - singletonMap("attr", "value"), - singleton(DiscoveryNodeRole.MASTER_ROLE), - Version.CURRENT); + return new DiscoveryNode( + "_node_name", + "_node_id", + "_ephemeral_id", + "_host_name", + "_host_address", + new TransportAddress(TransportAddress.META_ADDRESS, 9300), + singletonMap("attr", "value"), + singleton(DiscoveryNodeRole.MASTER_ROLE), + Version.CURRENT + ); } } diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexRecoveryCollectorTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexRecoveryCollectorTests.java index 3401720539e86..21192cc6c3992 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexRecoveryCollectorTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexRecoveryCollectorTests.java @@ -102,18 +102,17 @@ public void testDoCollect() throws Exception { ShardId shardId = new ShardId("_index_" + i, "_uuid_" + i, i); RecoverySource source = RecoverySource.PeerRecoverySource.INSTANCE; final UnassignedInfo unassignedInfo = new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "_index_info_" + i); - final ShardRouting shardRouting = ShardRouting - .newUnassigned(shardId, true, source, unassignedInfo) - .initialize(localNode.getId(), "_allocation_id", 10 * i); + final ShardRouting shardRouting = ShardRouting.newUnassigned(shardId, true, source, unassignedInfo) + .initialize(localNode.getId(), "_allocation_id", 10 * i); final RecoveryState recoveryState = new RecoveryState(shardRouting, localNode, localNode); recoveryStates.put("_index_" + i, singletonList(recoveryState)); } - final RecoveryResponse recoveryResponse = - new RecoveryResponse(randomInt(), randomInt(), randomInt(), recoveryStates, emptyList()); + final RecoveryResponse recoveryResponse = new RecoveryResponse(randomInt(), randomInt(), randomInt(), recoveryStates, emptyList()); - final RecoveryRequestBuilder recoveryRequestBuilder = - spy(new RecoveryRequestBuilder(mock(ElasticsearchClient.class), RecoveryAction.INSTANCE)); + final RecoveryRequestBuilder recoveryRequestBuilder = spy( + new RecoveryRequestBuilder(mock(ElasticsearchClient.class), RecoveryAction.INSTANCE) + ); doReturn(recoveryResponse).when(recoveryRequestBuilder).get(); final IndicesAdminClient indicesAdminClient = mock(IndicesAdminClient.class); @@ -170,12 +169,13 @@ public void testDoCollect() throws Exception { assertThat(recoveries.shardRecoveryStates().size(), equalTo(nbRecoveries)); } - assertWarnings("[xpack.monitoring.collection.index.recovery.timeout] setting was deprecated in Elasticsearch and will be " + - "removed in a future release! See the breaking changes documentation for the next major version.", - "[xpack.monitoring.collection.index.recovery.active_only] setting was deprecated in Elasticsearch and will be removed " + - "in a future release! See the breaking changes documentation for the next major version.", - "[xpack.monitoring.collection.indices] setting was deprecated in Elasticsearch and will be removed in a future release! " + - "See the breaking changes documentation for the next major version." + assertWarnings( + "[xpack.monitoring.collection.index.recovery.timeout] setting was deprecated in Elasticsearch and will be " + + "removed in a future release! See the breaking changes documentation for the next major version.", + "[xpack.monitoring.collection.index.recovery.active_only] setting was deprecated in Elasticsearch and will be removed " + + "in a future release! See the breaking changes documentation for the next major version.", + "[xpack.monitoring.collection.indices] setting was deprecated in Elasticsearch and will be removed in a future release! " + + "See the breaking changes documentation for the next major version." ); } @@ -196,12 +196,23 @@ public void testDoCollectThrowsTimeoutException() throws Exception { final MonitoringDoc.Node node = randomMonitoringNode(random()); - final RecoveryResponse recoveryResponse = - new RecoveryResponse(randomInt(), randomInt(), randomInt(), emptyMap(), List.of(new DefaultShardOperationFailedException( - "test", 0, new FailedNodeException(node.getUUID(), "msg", new ElasticsearchTimeoutException("test timeout"))))); + final RecoveryResponse recoveryResponse = new RecoveryResponse( + randomInt(), + randomInt(), + randomInt(), + emptyMap(), + List.of( + new DefaultShardOperationFailedException( + "test", + 0, + new FailedNodeException(node.getUUID(), "msg", new ElasticsearchTimeoutException("test timeout")) + ) + ) + ); - final RecoveryRequestBuilder recoveryRequestBuilder = - spy(new RecoveryRequestBuilder(mock(ElasticsearchClient.class), RecoveryAction.INSTANCE)); + final RecoveryRequestBuilder recoveryRequestBuilder = spy( + new RecoveryRequestBuilder(mock(ElasticsearchClient.class), RecoveryAction.INSTANCE) + ); doReturn(recoveryResponse).when(recoveryRequestBuilder).get(); final IndicesAdminClient indicesAdminClient = mock(IndicesAdminClient.class); @@ -220,8 +231,10 @@ public void testDoCollectThrowsTimeoutException() throws Exception { expectThrows(ElasticsearchTimeoutException.class, () -> collector.doCollect(node, interval, clusterState)); - assertWarnings("[xpack.monitoring.collection.index.recovery.timeout] setting was deprecated in Elasticsearch and will be " + - "removed in a future release! See the breaking changes documentation for the next major version."); + assertWarnings( + "[xpack.monitoring.collection.index.recovery.timeout] setting was deprecated in Elasticsearch and will be " + + "removed in a future release! See the breaking changes documentation for the next major version." + ); } } diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexRecoveryMonitoringDocTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexRecoveryMonitoringDocTests.java index 4c3362d4600b0..1c795c6604dbe 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexRecoveryMonitoringDocTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexRecoveryMonitoringDocTests.java @@ -18,10 +18,10 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.transport.TransportAddress; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.indices.recovery.RecoveryState; import org.elasticsearch.transport.NodeDisconnectedException; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc; import org.elasticsearch.xpack.monitoring.exporter.BaseMonitoringDocTestCase; @@ -53,8 +53,15 @@ public void setUp() throws Exception { } @Override - protected IndexRecoveryMonitoringDoc createMonitoringDoc(String cluster, long timestamp, long interval, MonitoringDoc.Node node, - MonitoredSystem system, String type, String id) { + protected IndexRecoveryMonitoringDoc createMonitoringDoc( + String cluster, + long timestamp, + long interval, + MonitoringDoc.Node node, + MonitoredSystem system, + String type, + String id + ) { return new IndexRecoveryMonitoringDoc(cluster, timestamp, interval, node, recoveryResponse); } @@ -68,37 +75,40 @@ protected void assertMonitoringDoc(final IndexRecoveryMonitoringDoc document) { } public void testConstructorRecoveryResponseMustNotBeNull() { - expectThrows(NullPointerException.class, - () -> new IndexRecoveryMonitoringDoc(cluster, timestamp, interval, node, null)); + expectThrows(NullPointerException.class, () -> new IndexRecoveryMonitoringDoc(cluster, timestamp, interval, node, null)); } @Override public void testToXContent() throws IOException { - final DiscoveryNode discoveryNodeZero = new DiscoveryNode("_node_0", - "_node_id_0", - "_ephemeral_id_0", - "_host_name_0", - "_host_address_0", - new TransportAddress(TransportAddress.META_ADDRESS, 9300), - singletonMap("attr", "value_0"), - singleton(DiscoveryNodeRole.MASTER_ROLE), - Version.CURRENT); - - final DiscoveryNode discoveryNodeOne = new DiscoveryNode("_node_1", - "_node_id_1", - "_ephemeral_id_1", - "_host_name_1", - "_host_address_1", - new TransportAddress(TransportAddress.META_ADDRESS, 9301), - singletonMap("attr", "value_1"), - singleton(DiscoveryNodeRole.DATA_ROLE), - Version.CURRENT.minimumIndexCompatibilityVersion()); + final DiscoveryNode discoveryNodeZero = new DiscoveryNode( + "_node_0", + "_node_id_0", + "_ephemeral_id_0", + "_host_name_0", + "_host_address_0", + new TransportAddress(TransportAddress.META_ADDRESS, 9300), + singletonMap("attr", "value_0"), + singleton(DiscoveryNodeRole.MASTER_ROLE), + Version.CURRENT + ); + + final DiscoveryNode discoveryNodeOne = new DiscoveryNode( + "_node_1", + "_node_id_1", + "_ephemeral_id_1", + "_host_name_1", + "_host_address_1", + new TransportAddress(TransportAddress.META_ADDRESS, 9301), + singletonMap("attr", "value_1"), + singleton(DiscoveryNodeRole.DATA_ROLE), + Version.CURRENT.minimumIndexCompatibilityVersion() + ); final ShardId shardId = new ShardId("_index_a", "_uuid_a", 0); final RecoverySource source = RecoverySource.PeerRecoverySource.INSTANCE; final UnassignedInfo unassignedInfo = new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "_index_info_a"); final ShardRouting shardRouting = ShardRouting.newUnassigned(shardId, true, source, unassignedInfo) - .initialize("_node_id", "_allocation_id", 123L); + .initialize("_node_id", "_allocation_id", 123L); final Map> shardRecoveryStates = new HashMap<>(); final RecoveryState recoveryState = new RecoveryState(shardRouting, discoveryNodeOne, discoveryNodeOne); @@ -113,8 +123,13 @@ public void testToXContent() throws IOException { final RecoveryResponse recoveryResponse = new RecoveryResponse(10, 7, 3, shardRecoveryStates, shardFailures); final MonitoringDoc.Node node = new MonitoringDoc.Node("_uuid", "_host", "_addr", "_ip", "_name", 1504169190855L); - final IndexRecoveryMonitoringDoc document = - new IndexRecoveryMonitoringDoc("_cluster", 1502266739402L, 1506593717631L, node, recoveryResponse); + final IndexRecoveryMonitoringDoc document = new IndexRecoveryMonitoringDoc( + "_cluster", + 1502266739402L, + 1506593717631L, + node, + recoveryResponse + ); final BytesReference xContent = XContentHelper.toXContent(document, XContentType.JSON, false); final String expected = XContentHelper.stripWhitespace( diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexStatsCollectorTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexStatsCollectorTests.java index e03447ee9da54..ef8ff535aa957 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexStatsCollectorTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexStatsCollectorTests.java @@ -117,8 +117,9 @@ public void testDoCollect() throws Exception { final String[] indexNames = indicesMetadata.keySet().toArray(new String[0]); when(metadata.getConcreteAllIndices()).thenReturn(indexNames); - final IndicesStatsRequestBuilder indicesStatsRequestBuilder = - spy(new IndicesStatsRequestBuilder(mock(ElasticsearchClient.class), IndicesStatsAction.INSTANCE)); + final IndicesStatsRequestBuilder indicesStatsRequestBuilder = spy( + new IndicesStatsRequestBuilder(mock(ElasticsearchClient.class), IndicesStatsAction.INSTANCE) + ); doReturn(indicesStatsResponse).when(indicesStatsRequestBuilder).get(); final IndicesAdminClient indicesAdminClient = mock(IndicesAdminClient.class); @@ -162,7 +163,7 @@ public void testDoCollect() throws Exception { } else { assertThat(document.getType(), equalTo(IndexStatsMonitoringDoc.TYPE)); - final IndexStatsMonitoringDoc indexStatsDocument = (IndexStatsMonitoringDoc)document; + final IndexStatsMonitoringDoc indexStatsDocument = (IndexStatsMonitoringDoc) document; final String index = indexStatsDocument.getIndexStats().getIndex(); assertThat(indexStatsDocument.getIndexStats(), is(indicesStats.get(index))); @@ -171,8 +172,10 @@ public void testDoCollect() throws Exception { } } - assertWarnings("[xpack.monitoring.collection.index.stats.timeout] setting was deprecated in Elasticsearch and will be removed " + - "in a future release! See the breaking changes documentation for the next major version."); + assertWarnings( + "[xpack.monitoring.collection.index.stats.timeout] setting was deprecated in Elasticsearch and will be removed " + + "in a future release! See the breaking changes documentation for the next major version." + ); } public void testDoCollectThrowsTimeoutException() throws Exception { @@ -184,13 +187,18 @@ public void testDoCollectThrowsTimeoutException() throws Exception { final IndicesStatsResponse indicesStatsResponse = mock(IndicesStatsResponse.class); final MonitoringDoc.Node node = randomMonitoringNode(random()); - when(indicesStatsResponse.getShardFailures()).thenReturn(new DefaultShardOperationFailedException[] { - new DefaultShardOperationFailedException("test", 0, - new FailedNodeException(node.getUUID(), "msg", new ElasticsearchTimeoutException("test timeout"))) - }); - - final IndicesStatsRequestBuilder indicesStatsRequestBuilder = - spy(new IndicesStatsRequestBuilder(mock(ElasticsearchClient.class), IndicesStatsAction.INSTANCE)); + when(indicesStatsResponse.getShardFailures()).thenReturn( + new DefaultShardOperationFailedException[] { + new DefaultShardOperationFailedException( + "test", + 0, + new FailedNodeException(node.getUUID(), "msg", new ElasticsearchTimeoutException("test timeout")) + ) } + ); + + final IndicesStatsRequestBuilder indicesStatsRequestBuilder = spy( + new IndicesStatsRequestBuilder(mock(ElasticsearchClient.class), IndicesStatsAction.INSTANCE) + ); doReturn(indicesStatsResponse).when(indicesStatsRequestBuilder).get(); final IndicesAdminClient indicesAdminClient = mock(IndicesAdminClient.class); @@ -207,8 +215,10 @@ public void testDoCollectThrowsTimeoutException() throws Exception { expectThrows(ElasticsearchTimeoutException.class, () -> collector.doCollect(node, interval, clusterState)); - assertWarnings("[xpack.monitoring.collection.index.stats.timeout] setting was deprecated in Elasticsearch and will be removed " + - "in a future release! See the breaking changes documentation for the next major version."); + assertWarnings( + "[xpack.monitoring.collection.index.stats.timeout] setting was deprecated in Elasticsearch and will be removed " + + "in a future release! See the breaking changes documentation for the next major version." + ); } } diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexStatsMonitoringDocTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexStatsMonitoringDocTests.java index 6ca74157fd62f..3bc3ef5c89189 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexStatsMonitoringDocTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexStatsMonitoringDocTests.java @@ -18,11 +18,7 @@ import org.elasticsearch.cluster.routing.UnassignedInfo; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.Index; import org.elasticsearch.index.cache.query.QueryCacheStats; import org.elasticsearch.index.cache.request.RequestCacheStats; @@ -34,6 +30,10 @@ import org.elasticsearch.index.shard.IndexingStats; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.index.store.StoreStats; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc; import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringTemplateUtils; @@ -83,8 +83,15 @@ public void setUp() throws Exception { } @Override - protected IndexStatsMonitoringDoc createMonitoringDoc(String cluster, long timestamp, long interval, MonitoringDoc.Node node, - MonitoredSystem system, String type, String id) { + protected IndexStatsMonitoringDoc createMonitoringDoc( + String cluster, + long timestamp, + long interval, + MonitoringDoc.Node node, + MonitoredSystem system, + String type, + String id + ) { return new IndexStatsMonitoringDoc(cluster, timestamp, interval, node, indexStats, metadata, routingTable); } @@ -111,15 +118,19 @@ public void testConstructorIndexStatsCanBeNull() { public void testConstructorMetadataMustNotBeNull() { final IndexStats indexStats = randomFrom(this.indexStats, null); - expectThrows(NullPointerException.class, - () -> new IndexStatsMonitoringDoc(cluster, timestamp, interval, node, indexStats, null, routingTable)); + expectThrows( + NullPointerException.class, + () -> new IndexStatsMonitoringDoc(cluster, timestamp, interval, node, indexStats, null, routingTable) + ); } public void testConstructorRoutingTableMustNotBeNull() { final IndexStats indexStats = randomFrom(this.indexStats, null); - expectThrows(NullPointerException.class, - () -> new IndexStatsMonitoringDoc(cluster, timestamp, interval, node, indexStats, metadata, null)); + expectThrows( + NullPointerException.class, + () -> new IndexStatsMonitoringDoc(cluster, timestamp, interval, node, indexStats, metadata, null) + ); } @Override @@ -128,8 +139,15 @@ public void testToXContent() throws IOException { when(indexStats.getTotal()).thenReturn(mockCommonStats()); when(indexStats.getPrimaries()).thenReturn(mockCommonStats()); - final IndexStatsMonitoringDoc document = - new IndexStatsMonitoringDoc("_cluster", 1502266739402L, 1506593717631L, node, indexStats, metadata, routingTable); + final IndexStatsMonitoringDoc document = new IndexStatsMonitoringDoc( + "_cluster", + 1502266739402L, + 1506593717631L, + node, + indexStats, + metadata, + routingTable + ); final BytesReference xContent; try (XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent())) { @@ -137,145 +155,150 @@ public void testToXContent() throws IOException { xContent = BytesReference.bytes(builder); } - final String expected = stripWhitespace(String.format(Locale.ROOT, "{" - + " \"cluster_uuid\": \"_cluster\"," - + " \"timestamp\": \"2017-08-09T08:18:59.402Z\"," - + " \"interval_ms\": 1506593717631," - + " \"type\": \"index_stats\"," - + " \"source_node\": {" - + " \"uuid\": \"_uuid\"," - + " \"host\": \"_host\"," - + " \"transport_address\": \"_addr\"," - + " \"ip\": \"_ip\"," - + " \"name\": \"_name\"," - + " \"timestamp\": \"2017-08-31T08:46:30.855Z\"" - + " }," - + " \"index_stats\": {" - + " %s," // indexStatsSummary() - + " \"total\": {" - + " \"docs\": {" - + " \"count\": 1" - + " }," - + " \"store\": {" - + " \"size_in_bytes\": 13" - + " }," - + " \"indexing\": {" - + " \"index_total\": 16," - + " \"index_time_in_millis\": 17," - + " \"throttle_time_in_millis\": 18" - + " }," - + " \"search\": {" - + " \"query_total\": 19," - + " \"query_time_in_millis\": 20" - + " }," - + " \"merges\": {" - + " \"total_size_in_bytes\": 4" - + " }," - + " \"refresh\": {" - + " \"total_time_in_millis\": 14," - + " \"external_total_time_in_millis\": 15" - + " }," - + " \"query_cache\": {" - + " \"memory_size_in_bytes\": 5," - + " \"hit_count\": 6," - + " \"miss_count\": 7," - + " \"evictions\": 9" - + " }," - + " \"fielddata\": {" - + " \"memory_size_in_bytes\": 2," - + " \"evictions\": 3" - + " }," - + " \"segments\": {" - + " \"count\": 21," - + " \"memory_in_bytes\": 0," - + " \"terms_memory_in_bytes\": 0," - + " \"stored_fields_memory_in_bytes\": 0," - + " \"term_vectors_memory_in_bytes\": 0," - + " \"norms_memory_in_bytes\": 0," - + " \"points_memory_in_bytes\": 0," - + " \"doc_values_memory_in_bytes\": 0," - + " \"index_writer_memory_in_bytes\": 22," - + " \"version_map_memory_in_bytes\": 23," - + " \"fixed_bit_set_memory_in_bytes\": 24" - + " }," - + " \"request_cache\": {" - + " \"memory_size_in_bytes\": 9," - + " \"evictions\": 10," - + " \"hit_count\": 11," - + " \"miss_count\": 12" - + " }," - + " \"bulk\": {" - + " \"total_operations\": 0," - + " \"total_time_in_millis\": 0," - + " \"total_size_in_bytes\": 0," - + " \"avg_time_in_millis\": 0," - + " \"avg_size_in_bytes\": 0" - + " }" - + " }," - + " \"primaries\": {" - + " \"docs\": {" - + " \"count\": 1" - + " }," - + " \"store\": {" - + " \"size_in_bytes\": 13" - + " }," - + " \"indexing\": {" - + " \"index_total\": 16," - + " \"index_time_in_millis\": 17," - + " \"throttle_time_in_millis\": 18" - + " }," - + " \"search\": {" - + " \"query_total\": 19," - + " \"query_time_in_millis\": 20" - + " }," - + " \"merges\": {" - + " \"total_size_in_bytes\": 4" - + " }," - + " \"refresh\": {" - + " \"total_time_in_millis\": 14," - + " \"external_total_time_in_millis\": 15" - + " }," - + " \"query_cache\": {" - + " \"memory_size_in_bytes\": 5," - + " \"hit_count\": 6," - + " \"miss_count\": 7," - + " \"evictions\": 9" - + " }," - + " \"fielddata\": {" - + " \"memory_size_in_bytes\": 2," - + " \"evictions\": 3" - + " }," - + " \"segments\": {" - + " \"count\": 21," - + " \"memory_in_bytes\": 0," - + " \"terms_memory_in_bytes\": 0," - + " \"stored_fields_memory_in_bytes\": 0," - + " \"term_vectors_memory_in_bytes\": 0," - + " \"norms_memory_in_bytes\": 0," - + " \"points_memory_in_bytes\": 0," - + " \"doc_values_memory_in_bytes\": 0," - + " \"index_writer_memory_in_bytes\": 22," - + " \"version_map_memory_in_bytes\": 23," - + " \"fixed_bit_set_memory_in_bytes\": 24" - + " }," - + " \"request_cache\": {" - + " \"memory_size_in_bytes\": 9," - + " \"evictions\": 10," - + " \"hit_count\": 11," - + " \"miss_count\": 12" - + " }," - + " \"bulk\": {" - + " \"total_operations\": 0," - + " \"total_time_in_millis\": 0," - + " \"total_size_in_bytes\": 0," - + " \"avg_time_in_millis\": 0," - + " \"avg_size_in_bytes\": 0" - + " }" - + " }" - + " }" - + "}", - // Since the summary is being merged with other data, remove the enclosing braces. - indexStatsSummary().replaceAll("(^\\{|}$)", ""))); + final String expected = stripWhitespace( + String.format( + Locale.ROOT, + "{" + + " \"cluster_uuid\": \"_cluster\"," + + " \"timestamp\": \"2017-08-09T08:18:59.402Z\"," + + " \"interval_ms\": 1506593717631," + + " \"type\": \"index_stats\"," + + " \"source_node\": {" + + " \"uuid\": \"_uuid\"," + + " \"host\": \"_host\"," + + " \"transport_address\": \"_addr\"," + + " \"ip\": \"_ip\"," + + " \"name\": \"_name\"," + + " \"timestamp\": \"2017-08-31T08:46:30.855Z\"" + + " }," + + " \"index_stats\": {" + + " %s," // indexStatsSummary() + + " \"total\": {" + + " \"docs\": {" + + " \"count\": 1" + + " }," + + " \"store\": {" + + " \"size_in_bytes\": 13" + + " }," + + " \"indexing\": {" + + " \"index_total\": 16," + + " \"index_time_in_millis\": 17," + + " \"throttle_time_in_millis\": 18" + + " }," + + " \"search\": {" + + " \"query_total\": 19," + + " \"query_time_in_millis\": 20" + + " }," + + " \"merges\": {" + + " \"total_size_in_bytes\": 4" + + " }," + + " \"refresh\": {" + + " \"total_time_in_millis\": 14," + + " \"external_total_time_in_millis\": 15" + + " }," + + " \"query_cache\": {" + + " \"memory_size_in_bytes\": 5," + + " \"hit_count\": 6," + + " \"miss_count\": 7," + + " \"evictions\": 9" + + " }," + + " \"fielddata\": {" + + " \"memory_size_in_bytes\": 2," + + " \"evictions\": 3" + + " }," + + " \"segments\": {" + + " \"count\": 21," + + " \"memory_in_bytes\": 0," + + " \"terms_memory_in_bytes\": 0," + + " \"stored_fields_memory_in_bytes\": 0," + + " \"term_vectors_memory_in_bytes\": 0," + + " \"norms_memory_in_bytes\": 0," + + " \"points_memory_in_bytes\": 0," + + " \"doc_values_memory_in_bytes\": 0," + + " \"index_writer_memory_in_bytes\": 22," + + " \"version_map_memory_in_bytes\": 23," + + " \"fixed_bit_set_memory_in_bytes\": 24" + + " }," + + " \"request_cache\": {" + + " \"memory_size_in_bytes\": 9," + + " \"evictions\": 10," + + " \"hit_count\": 11," + + " \"miss_count\": 12" + + " }," + + " \"bulk\": {" + + " \"total_operations\": 0," + + " \"total_time_in_millis\": 0," + + " \"total_size_in_bytes\": 0," + + " \"avg_time_in_millis\": 0," + + " \"avg_size_in_bytes\": 0" + + " }" + + " }," + + " \"primaries\": {" + + " \"docs\": {" + + " \"count\": 1" + + " }," + + " \"store\": {" + + " \"size_in_bytes\": 13" + + " }," + + " \"indexing\": {" + + " \"index_total\": 16," + + " \"index_time_in_millis\": 17," + + " \"throttle_time_in_millis\": 18" + + " }," + + " \"search\": {" + + " \"query_total\": 19," + + " \"query_time_in_millis\": 20" + + " }," + + " \"merges\": {" + + " \"total_size_in_bytes\": 4" + + " }," + + " \"refresh\": {" + + " \"total_time_in_millis\": 14," + + " \"external_total_time_in_millis\": 15" + + " }," + + " \"query_cache\": {" + + " \"memory_size_in_bytes\": 5," + + " \"hit_count\": 6," + + " \"miss_count\": 7," + + " \"evictions\": 9" + + " }," + + " \"fielddata\": {" + + " \"memory_size_in_bytes\": 2," + + " \"evictions\": 3" + + " }," + + " \"segments\": {" + + " \"count\": 21," + + " \"memory_in_bytes\": 0," + + " \"terms_memory_in_bytes\": 0," + + " \"stored_fields_memory_in_bytes\": 0," + + " \"term_vectors_memory_in_bytes\": 0," + + " \"norms_memory_in_bytes\": 0," + + " \"points_memory_in_bytes\": 0," + + " \"doc_values_memory_in_bytes\": 0," + + " \"index_writer_memory_in_bytes\": 22," + + " \"version_map_memory_in_bytes\": 23," + + " \"fixed_bit_set_memory_in_bytes\": 24" + + " }," + + " \"request_cache\": {" + + " \"memory_size_in_bytes\": 9," + + " \"evictions\": 10," + + " \"hit_count\": 11," + + " \"miss_count\": 12" + + " }," + + " \"bulk\": {" + + " \"total_operations\": 0," + + " \"total_time_in_millis\": 0," + + " \"total_size_in_bytes\": 0," + + " \"avg_time_in_millis\": 0," + + " \"avg_size_in_bytes\": 0" + + " }" + + " }" + + " }" + + "}", + // Since the summary is being merged with other data, remove the enclosing braces. + indexStatsSummary().replaceAll("(^\\{|}$)", "") + ) + ); assertThat(xContent.utf8ToString(), equalTo(expected)); } @@ -292,8 +315,15 @@ public void testToXContentWithNullStats() throws IOException { indexStats = null; } - final IndexStatsMonitoringDoc document = - new IndexStatsMonitoringDoc("_cluster", 1502266739402L, 1506593717631L, node, indexStats, metadata, routingTable); + final IndexStatsMonitoringDoc document = new IndexStatsMonitoringDoc( + "_cluster", + 1502266739402L, + 1506593717631L, + node, + indexStats, + metadata, + routingTable + ); final BytesReference xContent = XContentHelper.toXContent(document, XContentType.JSON, false); final String expected = stripWhitespace( @@ -383,8 +413,7 @@ private static CommonStats mockCommonStats() { return commonStats; } - private static IndexMetadata mockIndexMetadata(final Index index, - final int primaries, final int replicas) { + private static IndexMetadata mockIndexMetadata(final Index index, final int primaries, final int replicas) { final Settings.Builder settings = Settings.builder(); settings.put(IndexMetadata.SETTING_INDEX_UUID, index.getUUID()); @@ -396,10 +425,15 @@ private static IndexMetadata mockIndexMetadata(final Index index, return IndexMetadata.builder(index.getName()).settings(settings).build(); } - private static IndexRoutingTable mockIndexRoutingTable(final Index index, - final int primaries, final int replicas, - final int activePrimaries, final int activeReplicas, - final int initializing, final int relocating) { + private static IndexRoutingTable mockIndexRoutingTable( + final Index index, + final int primaries, + final int replicas, + final int activePrimaries, + final int activeReplicas, + final int initializing, + final int relocating + ) { final int total = primaries + (primaries * replicas); int unassignedTotal = total - (activePrimaries + activeReplicas); int unassignedPrimaries = primaries - activePrimaries; @@ -448,7 +482,7 @@ private static IndexRoutingTable mockIndexRoutingTable(final Index index, shard.addShard(TestShardRouting.newShardRouting(shardId, null, false, ShardRoutingState.UNASSIGNED)); } - // primary should be allocated, but replicas can still be unassigned + // primary should be allocated, but replicas can still be unassigned } else { --activePrimariesRemaining; diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndicesStatsMonitoringDocTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndicesStatsMonitoringDocTests.java index 4ebb26ed8ae69..02d18b61a4645 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndicesStatsMonitoringDocTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndicesStatsMonitoringDocTests.java @@ -14,7 +14,6 @@ import org.elasticsearch.cluster.routing.TestShardRouting; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.bulk.stats.BulkStats; import org.elasticsearch.index.search.stats.SearchStats; import org.elasticsearch.index.shard.DocsStats; @@ -22,6 +21,7 @@ import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.index.shard.ShardPath; import org.elasticsearch.index.store.StoreStats; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc; import org.elasticsearch.xpack.monitoring.exporter.BaseFilteredMonitoringDocTestCase; @@ -47,18 +47,30 @@ public class IndicesStatsMonitoringDocTests extends BaseFilteredMonitoringDocTes @Before public void setUp() throws Exception { super.setUp(); - indicesStats = Collections.singletonList(new IndexStats("index-0", "dcvO5uZATE-EhIKc3tk9Bg", new ShardStats[] { - // Primaries - new ShardStats(mockShardRouting(true), mockShardPath(), mockCommonStats(), null, null, null), - new ShardStats(mockShardRouting(true), mockShardPath(), mockCommonStats(), null, null, null), - // Replica - new ShardStats(mockShardRouting(false), mockShardPath(), mockCommonStats(), null, null, null) - })); + indicesStats = Collections.singletonList( + new IndexStats( + "index-0", + "dcvO5uZATE-EhIKc3tk9Bg", + new ShardStats[] { + // Primaries + new ShardStats(mockShardRouting(true), mockShardPath(), mockCommonStats(), null, null, null), + new ShardStats(mockShardRouting(true), mockShardPath(), mockCommonStats(), null, null, null), + // Replica + new ShardStats(mockShardRouting(false), mockShardPath(), mockCommonStats(), null, null, null) } + ) + ); } @Override - protected IndicesStatsMonitoringDoc createMonitoringDoc(String cluster, long timestamp, long interval, MonitoringDoc.Node node, - MonitoredSystem system, String type, String id) { + protected IndicesStatsMonitoringDoc createMonitoringDoc( + String cluster, + long timestamp, + long interval, + MonitoringDoc.Node node, + MonitoredSystem system, + String type, + String id + ) { return new IndicesStatsMonitoringDoc(cluster, timestamp, interval, node, indicesStats); } @@ -83,8 +95,13 @@ public void testConstructorIndexStatsMustNotBeNull() { @Override public void testToXContent() throws IOException { final MonitoringDoc.Node node = new MonitoringDoc.Node("_uuid", "_host", "_addr", "_ip", "_name", 1504169190855L); - final IndicesStatsMonitoringDoc document = - new IndicesStatsMonitoringDoc("_cluster", 1502266739402L, 1506593717631L, node, indicesStats); + final IndicesStatsMonitoringDoc document = new IndicesStatsMonitoringDoc( + "_cluster", + 1502266739402L, + 1506593717631L, + node, + indicesStats + ); final BytesReference xContent = XContentHelper.toXContent(document, XContentType.JSON, false); final String expected = XContentHelper.stripWhitespace( @@ -183,7 +200,7 @@ private ShardPath mockShardPath() { final Path getFileNameShardId = mock(Path.class); when(getFileNameShardId.toString()).thenReturn(Integer.toString(shardId)); - final String shardUuid = randomAlphaOfLength(5); + final String shardUuid = randomAlphaOfLength(5); final Path getFileNameShardUuid = mock(Path.class); when(getFileNameShardUuid.toString()).thenReturn(shardUuid); diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/ml/JobStatsCollectorTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/ml/JobStatsCollectorTests.java index d93a34139d61f..ff4cfa41c2f0d 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/ml/JobStatsCollectorTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/ml/JobStatsCollectorTests.java @@ -113,7 +113,7 @@ public void testDoCollect() throws Exception { final List jobStats = mockJobStats(); @SuppressWarnings("unchecked") - final ActionFuture future = (ActionFuture)mock(ActionFuture.class); + final ActionFuture future = (ActionFuture) mock(ActionFuture.class); final Response response = new Response(new QueryPage<>(jobStats, jobStats.size(), Job.RESULTS_FIELD)); when(client.execute(eq(GetJobsStatsAction.INSTANCE), eq(new Request(Metadata.ALL).setTimeout(timeout)))).thenReturn(future); @@ -128,7 +128,7 @@ public void testDoCollect() throws Exception { assertThat(monitoringDocs, hasSize(jobStats.size())); for (int i = 0; i < monitoringDocs.size(); ++i) { - final JobStatsMonitoringDoc jobStatsMonitoringDoc = (JobStatsMonitoringDoc)monitoringDocs.get(i); + final JobStatsMonitoringDoc jobStatsMonitoringDoc = (JobStatsMonitoringDoc) monitoringDocs.get(i); final JobStats jobStat = jobStats.get(i); assertThat(jobStatsMonitoringDoc.getCluster(), is(clusterUuid)); @@ -142,8 +142,10 @@ public void testDoCollect() throws Exception { assertThat(jobStatsMonitoringDoc.getJobStats(), is(jobStat)); } - assertWarnings("[xpack.monitoring.collection.ml.job.stats.timeout] setting was deprecated in Elasticsearch and will be removed " + - "in a future release! See the breaking changes documentation for the next major version."); + assertWarnings( + "[xpack.monitoring.collection.ml.job.stats.timeout] setting was deprecated in Elasticsearch and will be removed " + + "in a future release! See the breaking changes documentation for the next major version." + ); } public void testDoCollectThrowsTimeoutException() throws Exception { @@ -163,9 +165,12 @@ public void testDoCollectThrowsTimeoutException() throws Exception { final List jobStats = mockJobStats(); @SuppressWarnings("unchecked") - final ActionFuture future = (ActionFuture)mock(ActionFuture.class); - final Response response = new Response(List.of(), List.of(new FailedNodeException("node", "msg", - new ElasticsearchTimeoutException("test timeout"))), new QueryPage<>(jobStats, jobStats.size(), Job.RESULTS_FIELD)); + final ActionFuture future = (ActionFuture) mock(ActionFuture.class); + final Response response = new Response( + List.of(), + List.of(new FailedNodeException("node", "msg", new ElasticsearchTimeoutException("test timeout"))), + new QueryPage<>(jobStats, jobStats.size(), Job.RESULTS_FIELD) + ); when(client.execute(eq(GetJobsStatsAction.INSTANCE), eq(new Request(Metadata.ALL).setTimeout(timeout)))).thenReturn(future); when(future.actionGet()).thenReturn(response); @@ -174,8 +179,10 @@ public void testDoCollectThrowsTimeoutException() throws Exception { expectThrows(ElasticsearchTimeoutException.class, () -> collector.doCollect(node, interval, clusterState)); - assertWarnings("[xpack.monitoring.collection.ml.job.stats.timeout] setting was deprecated in Elasticsearch and will be removed " + - "in a future release! See the breaking changes documentation for the next major version."); + assertWarnings( + "[xpack.monitoring.collection.ml.job.stats.timeout] setting was deprecated in Elasticsearch and will be removed " + + "in a future release! See the breaking changes documentation for the next major version." + ); } private List mockJobStats() { diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/ml/JobStatsMonitoringDocTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/ml/JobStatsMonitoringDocTests.java index bcbc679ec6fe5..1c4b62794ef11 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/ml/JobStatsMonitoringDocTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/ml/JobStatsMonitoringDocTests.java @@ -11,8 +11,8 @@ import org.elasticsearch.cluster.node.DiscoveryNodeRole; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.transport.TransportAddress; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.ml.action.GetJobsStatsAction.Response.JobStats; import org.elasticsearch.xpack.core.ml.job.config.JobState; @@ -53,9 +53,15 @@ public void setUp() throws Exception { } @Override - protected JobStatsMonitoringDoc createMonitoringDoc(final String cluster, final long timestamp, long interval, - final MonitoringDoc.Node node, final MonitoredSystem system, - final String type, final String id) { + protected JobStatsMonitoringDoc createMonitoringDoc( + final String cluster, + final long timestamp, + long interval, + final MonitoringDoc.Node node, + final MonitoredSystem system, + final String type, + final String id + ) { return new JobStatsMonitoringDoc(cluster, timestamp, interval, node, jobStats); } @@ -69,8 +75,7 @@ protected void assertMonitoringDoc(final JobStatsMonitoringDoc document) { } public void testConstructorJobStatsMustNotBeNull() { - expectThrows(NullPointerException.class, - () -> new JobStatsMonitoringDoc(cluster, timestamp, interval, node, null)); + expectThrows(NullPointerException.class, () -> new JobStatsMonitoringDoc(cluster, timestamp, interval, node, null)); } @Override @@ -85,41 +90,75 @@ public void testToXContent() throws IOException { final Date date7 = new Date(ZonedDateTime.parse("2017-01-07T07:07:07.007+07:00").toInstant().toEpochMilli()); final Instant date8 = ZonedDateTime.parse("2017-01-07T08:08:08.007+07:00").toInstant(); - final DiscoveryNode discoveryNode = new DiscoveryNode("_node_name", - "_node_id", - "_ephemeral_id", - "_host_name", - "_host_address", - new TransportAddress(TransportAddress.META_ADDRESS, 9300), - singletonMap("attr", "value"), - singleton(DiscoveryNodeRole.MASTER_ROLE), - Version.CURRENT); - - final ModelSizeStats modelStats = new ModelSizeStats.Builder("_model") - .setModelBytes(100L) - .setTotalByFieldCount(101L) - .setTotalOverFieldCount(102L) - .setTotalPartitionFieldCount(103L) - .setBucketAllocationFailuresCount(104L) - .setMemoryStatus(ModelSizeStats.MemoryStatus.OK) - .setCategorizedDocCount(42) - .setTotalCategoryCount(8) - .setFrequentCategoryCount(4) - .setRareCategoryCount(2) - .setDeadCategoryCount(1) - .setFailedCategoryCount(3) - .setCategorizationStatus(CategorizationStatus.WARN) - .setTimestamp(date1) - .setLogTime(date2) - .build(); - - final DataCounts dataCounts = new DataCounts("_job_id", 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, - date3, date4, date5, date6, date7, date8); + final DiscoveryNode discoveryNode = new DiscoveryNode( + "_node_name", + "_node_id", + "_ephemeral_id", + "_host_name", + "_host_address", + new TransportAddress(TransportAddress.META_ADDRESS, 9300), + singletonMap("attr", "value"), + singleton(DiscoveryNodeRole.MASTER_ROLE), + Version.CURRENT + ); + + final ModelSizeStats modelStats = new ModelSizeStats.Builder("_model").setModelBytes(100L) + .setTotalByFieldCount(101L) + .setTotalOverFieldCount(102L) + .setTotalPartitionFieldCount(103L) + .setBucketAllocationFailuresCount(104L) + .setMemoryStatus(ModelSizeStats.MemoryStatus.OK) + .setCategorizedDocCount(42) + .setTotalCategoryCount(8) + .setFrequentCategoryCount(4) + .setRareCategoryCount(2) + .setDeadCategoryCount(1) + .setFailedCategoryCount(3) + .setCategorizationStatus(CategorizationStatus.WARN) + .setTimestamp(date1) + .setLogTime(date2) + .build(); + + final DataCounts dataCounts = new DataCounts( + "_job_id", + 0L, + 1L, + 2L, + 3L, + 4L, + 5L, + 6L, + 7L, + 8L, + 9L, + date3, + date4, + date5, + date6, + date7, + date8 + ); final ForecastStats forecastStats = new ForecastStats(); final TimingStats timingStats = new TimingStats( - "_job_id", 100, 10.0, 30.0, 20.0, 25.0, new ExponentialAverageCalculationContext(50.0, null, null)); + "_job_id", + 100, + 10.0, + 30.0, + 20.0, + 25.0, + new ExponentialAverageCalculationContext(50.0, null, null) + ); final JobStats jobStats = new JobStats( - "_job", dataCounts, modelStats, forecastStats, JobState.OPENED, discoveryNode, "_explanation", time, timingStats); + "_job", + dataCounts, + modelStats, + forecastStats, + JobState.OPENED, + discoveryNode, + "_explanation", + time, + timingStats + ); final MonitoringDoc.Node node = new MonitoringDoc.Node("_uuid", "_host", "_addr", "_ip", "_name", 1504169190855L); final JobStatsMonitoringDoc document = new JobStatsMonitoringDoc("_cluster", 1502266739402L, 1506593717631L, node, jobStats); diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/node/NodeStatsCollectorTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/node/NodeStatsCollectorTests.java index 3fc267e8e17d4..cb6a13697e25e 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/node/NodeStatsCollectorTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/node/NodeStatsCollectorTests.java @@ -62,12 +62,16 @@ public void testDoCollectWithFailures() throws Exception { final NodeStatsCollector collector = new NodeStatsCollector(clusterService, licenseState, client); assertEquals(timeout, collector.getCollectionTimeout()); - final FailedNodeException e = expectThrows(FailedNodeException.class, () -> - collector.doCollect(randomMonitoringNode(random()), randomNonNegativeLong(), clusterState)); + final FailedNodeException e = expectThrows( + FailedNodeException.class, + () -> collector.doCollect(randomMonitoringNode(random()), randomNonNegativeLong(), clusterState) + ); assertEquals(exception, e); - assertWarnings("[xpack.monitoring.collection.node.stats.timeout] setting was deprecated in Elasticsearch and will be removed " + - "in a future release! See the breaking changes documentation for the next major version."); + assertWarnings( + "[xpack.monitoring.collection.node.stats.timeout] setting was deprecated in Elasticsearch and will be removed " + + "in a future release! See the breaking changes documentation for the next major version." + ); } public void testDoCollect() throws Exception { @@ -122,8 +126,10 @@ public void testDoCollect() throws Exception { assertThat(document.getNodeStats(), is(nodeStats)); assertThat(document.isMlockall(), equalTo(BootstrapInfo.isMemoryLocked())); - assertWarnings("[xpack.monitoring.collection.node.stats.timeout] setting was deprecated in Elasticsearch and will be removed " + - "in a future release! See the breaking changes documentation for the next major version."); + assertWarnings( + "[xpack.monitoring.collection.node.stats.timeout] setting was deprecated in Elasticsearch and will be removed " + + "in a future release! See the breaking changes documentation for the next major version." + ); } public void testDoCollectThrowsTimeout() throws Exception { @@ -140,8 +146,9 @@ public void testDoCollectThrowsTimeout() throws Exception { final NodesStatsResponse nodesStatsResponse = mock(NodesStatsResponse.class); when(nodesStatsResponse.hasFailures()).thenReturn(true); - when(nodesStatsResponse.failures()).thenReturn(List.of(new FailedNodeException("node", "msg", - new ElasticsearchTimeoutException("test")))); + when(nodesStatsResponse.failures()).thenReturn( + List.of(new FailedNodeException("node", "msg", new ElasticsearchTimeoutException("test"))) + ); final Client client = mock(Client.class); thenReturnNodeStats(client, timeout, nodesStatsResponse); @@ -153,8 +160,10 @@ public void testDoCollectThrowsTimeout() throws Exception { expectThrows(ElasticsearchTimeoutException.class, () -> collector.doCollect(node, interval, clusterState)); - assertWarnings("[xpack.monitoring.collection.node.stats.timeout] setting was deprecated in Elasticsearch and will be removed " + - "in a future release! See the breaking changes documentation for the next major version."); + assertWarnings( + "[xpack.monitoring.collection.node.stats.timeout] setting was deprecated in Elasticsearch and will be removed " + + "in a future release! See the breaking changes documentation for the next major version." + ); } private void thenReturnNodeStats(final Client client, final TimeValue timeout, final NodesStatsResponse nodesStatsResponse) { diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/node/NodeStatsMonitoringDocTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/node/NodeStatsMonitoringDocTests.java index 387513cda0bd0..019dcc221b660 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/node/NodeStatsMonitoringDocTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/node/NodeStatsMonitoringDocTests.java @@ -13,10 +13,7 @@ import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.transport.TransportAddress; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.cache.query.QueryCacheStats; import org.elasticsearch.index.cache.request.RequestCacheStats; import org.elasticsearch.index.engine.SegmentsStats; @@ -31,6 +28,9 @@ import org.elasticsearch.monitor.os.OsStats; import org.elasticsearch.monitor.process.ProcessStats; import org.elasticsearch.threadpool.ThreadPoolStats; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc; import org.elasticsearch.xpack.monitoring.exporter.BaseFilteredMonitoringDocTestCase; @@ -67,8 +67,15 @@ public void setUp() throws Exception { } @Override - protected NodeStatsMonitoringDoc createMonitoringDoc(String cluster, long timestamp, long interval, MonitoringDoc.Node node, - MonitoredSystem system, String type, String id) { + protected NodeStatsMonitoringDoc createMonitoringDoc( + String cluster, + long timestamp, + long interval, + MonitoringDoc.Node node, + MonitoredSystem system, + String type, + String id + ) { return new NodeStatsMonitoringDoc(cluster, timestamp, interval, node, nodeId, isMaster, nodeStats, mlockall); } @@ -90,13 +97,17 @@ protected Set getExpectedXContentFilters() { } public void testConstructorNodeIdMustNotBeNull() { - expectThrows(NullPointerException.class, () -> - new NodeStatsMonitoringDoc(cluster, timestamp, interval, node, null, isMaster, nodeStats, mlockall)); + expectThrows( + NullPointerException.class, + () -> new NodeStatsMonitoringDoc(cluster, timestamp, interval, node, null, isMaster, nodeStats, mlockall) + ); } public void testConstructorNodeStatsMustNotBeNull() { - expectThrows(NullPointerException.class, () -> - new NodeStatsMonitoringDoc(cluster, timestamp, interval, node, nodeId, isMaster, null, mlockall)); + expectThrows( + NullPointerException.class, + () -> new NodeStatsMonitoringDoc(cluster, timestamp, interval, node, nodeId, isMaster, null, mlockall) + ); } @Override @@ -104,8 +115,16 @@ public void testToXContent() throws IOException { final MonitoringDoc.Node node = new MonitoringDoc.Node("_uuid", "_host", "_addr", "_ip", "_name", 1504169190855L); final NodeStats nodeStats = mockNodeStats(); - final NodeStatsMonitoringDoc doc = - new NodeStatsMonitoringDoc("_cluster", 1502107402133L, 1506593717631L, node, "_node_id", true, nodeStats, false); + final NodeStatsMonitoringDoc doc = new NodeStatsMonitoringDoc( + "_cluster", + 1502107402133L, + 1506593717631L, + node, + "_node_id", + true, + nodeStats, + false + ); final BytesReference xContent; try (XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent())) { @@ -328,18 +347,46 @@ private static NodeStats mockNodeStats() { final NodeIndicesStats indices = new NodeIndicesStats(indicesCommonStats, emptyMap()); // Filesystem - final FsInfo.DeviceStats ioStatsOne = new FsInfo.DeviceStats((int) no, (int) no, null, ++iota, ++iota, ++iota, ++iota,++iota, null); - final FsInfo.DeviceStats ioStatsTwo = new FsInfo.DeviceStats((int) no, (int) no, null, ++iota, ++iota, ++iota, ++iota, ++iota, - ioStatsOne); + final FsInfo.DeviceStats ioStatsOne = new FsInfo.DeviceStats( + (int) no, + (int) no, + null, + ++iota, + ++iota, + ++iota, + ++iota, + ++iota, + null + ); + final FsInfo.DeviceStats ioStatsTwo = new FsInfo.DeviceStats( + (int) no, + (int) no, + null, + ++iota, + ++iota, + ++iota, + ++iota, + ++iota, + ioStatsOne + ); - final FsInfo.IoStats ioStats = new FsInfo.IoStats(new FsInfo.DeviceStats[]{ioStatsTwo}); - final FsInfo fs = new FsInfo(no, ioStats, new FsInfo.Path[]{new FsInfo.Path(null, null, ++iota, ++iota, ++iota)}); + final FsInfo.IoStats ioStats = new FsInfo.IoStats(new FsInfo.DeviceStats[] { ioStatsTwo }); + final FsInfo fs = new FsInfo(no, ioStats, new FsInfo.Path[] { new FsInfo.Path(null, null, ++iota, ++iota, ++iota) }); // Os - final OsStats.Cpu osCpu = new OsStats.Cpu((short) no, new double[]{++iota, ++iota, ++iota}); + final OsStats.Cpu osCpu = new OsStats.Cpu((short) no, new double[] { ++iota, ++iota, ++iota }); final OsStats.Cgroup.CpuStat osCpuStat = new OsStats.Cgroup.CpuStat(++iota, ++iota, ++iota); - final OsStats.Cgroup osCgroup = new OsStats.Cgroup("_cpu_acct_ctrl_group", ++iota, "_cpu_ctrl_group", ++iota, ++iota, osCpuStat, - "_memory_ctrl_group", "2000000000", "1000000000"); + final OsStats.Cgroup osCgroup = new OsStats.Cgroup( + "_cpu_acct_ctrl_group", + ++iota, + "_cpu_ctrl_group", + ++iota, + ++iota, + osCpuStat, + "_memory_ctrl_group", + "2000000000", + "1000000000" + ); final OsStats.Mem osMem = new OsStats.Mem(0, 0, 0); final OsStats.Swap osSwap = new OsStats.Swap(0, 0); @@ -353,9 +400,11 @@ private static NodeStats mockNodeStats() { final JvmStats.Threads jvmThreads = new JvmStats.Threads((int) no, (int) no); final JvmStats.Classes jvmClasses = new JvmStats.Classes(no, no, no); final JvmStats.Mem jvmMem = new JvmStats.Mem(no, ++iota, ++iota, no, no, emptyList()); - final JvmStats.GarbageCollectors gcs = new JvmStats.GarbageCollectors(new JvmStats.GarbageCollector[]{ + final JvmStats.GarbageCollectors gcs = new JvmStats.GarbageCollectors( + new JvmStats.GarbageCollector[] { new JvmStats.GarbageCollector("young", ++iota, ++iota), - new JvmStats.GarbageCollector("old", ++iota, ++iota)}); + new JvmStats.GarbageCollector("old", ++iota, ++iota) } + ); final JvmStats jvm = new JvmStats(no, no, jvmMem, jvmThreads, gcs, emptyList(), jvmClasses); // Threadpools @@ -368,17 +417,36 @@ private static NodeStats mockNodeStats() { threadpools.add(new ThreadPoolStats.Stats("write", (int) ++iota, (int) ++iota, (int) no, ++iota, (int) no, no)); final ThreadPoolStats threadPool = new ThreadPoolStats(threadpools); - final DiscoveryNode discoveryNode = new DiscoveryNode("_node_name", - "_node_id", - "_ephemeral_id", - "_host_name", - "_host_address", - new TransportAddress(TransportAddress.META_ADDRESS, 1234), - emptyMap(), - emptySet(), - Version.CURRENT); - - return new NodeStats(discoveryNode, no, indices, os, process, jvm, threadPool, fs, null, null, null, null, null, null, null, null, - null); + final DiscoveryNode discoveryNode = new DiscoveryNode( + "_node_name", + "_node_id", + "_ephemeral_id", + "_host_name", + "_host_address", + new TransportAddress(TransportAddress.META_ADDRESS, 1234), + emptyMap(), + emptySet(), + Version.CURRENT + ); + + return new NodeStats( + discoveryNode, + no, + indices, + os, + process, + jvm, + threadPool, + fs, + null, + null, + null, + null, + null, + null, + null, + null, + null + ); } } diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/shards/ShardsCollectorTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/shards/ShardsCollectorTests.java index dc7e3870bebd4..5ac436608c1aa 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/shards/ShardsCollectorTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/shards/ShardsCollectorTests.java @@ -40,7 +40,7 @@ public class ShardsCollectorTests extends BaseCollectorTestCase { /** Used to match no indices when collecting shards information **/ - private static final String[] NONE = new String[]{"_none"}; + private static final String[] NONE = new String[] { "_none" }; public void testShouldCollectReturnsFalseIfNotMaster() { // this controls the blockage @@ -77,7 +77,7 @@ public void testDoCollect() throws Exception { final String stateUUID = UUID.randomUUID().toString(); when(clusterState.stateUUID()).thenReturn(stateUUID); - final String[] indices = randomFrom(NONE, Strings.EMPTY_ARRAY, new String[]{"_all"}, new String[]{"_index*"}); + final String[] indices = randomFrom(NONE, Strings.EMPTY_ARRAY, new String[] { "_all" }, new String[] { "_index*" }); withCollectionIndices(indices); final RoutingTable routingTable = mockRoutingTable(); @@ -122,8 +122,10 @@ public void testDoCollect() throws Exception { } - assertWarnings("[xpack.monitoring.collection.indices] setting was deprecated in Elasticsearch and will be removed in a" + - " future release! See the breaking changes documentation for the next major version."); + assertWarnings( + "[xpack.monitoring.collection.indices] setting was deprecated in Elasticsearch and will be removed in a" + + " future release! See the breaking changes documentation for the next major version." + ); } private static RoutingTable mockRoutingTable() { diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/shards/ShardsMonitoringDocTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/shards/ShardsMonitoringDocTests.java index c10d2a7e893c1..24f2c7dfef552 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/shards/ShardsMonitoringDocTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/shards/ShardsMonitoringDocTests.java @@ -39,16 +39,25 @@ public void setUp() throws Exception { stateUuid = randomAlphaOfLength(5); assignedToNode = randomBoolean(); node = assignedToNode ? MonitoringTestUtils.randomMonitoringNode(random()) : null; - shardRouting = newShardRouting(randomAlphaOfLength(5), - randomIntBetween(0, 5), - assignedToNode ? node.getUUID() : null, - randomBoolean(), - assignedToNode ? INITIALIZING : UNASSIGNED); + shardRouting = newShardRouting( + randomAlphaOfLength(5), + randomIntBetween(0, 5), + assignedToNode ? node.getUUID() : null, + randomBoolean(), + assignedToNode ? INITIALIZING : UNASSIGNED + ); } @Override - protected ShardMonitoringDoc createMonitoringDoc(String cluster, long timestamp, long interval, MonitoringDoc.Node node, - MonitoredSystem system, String type, String id) { + protected ShardMonitoringDoc createMonitoringDoc( + String cluster, + long timestamp, + long interval, + MonitoringDoc.Node node, + MonitoredSystem system, + String type, + String id + ) { return new ShardMonitoringDoc(cluster, timestamp, interval, node, shardRouting, stateUuid); } @@ -104,8 +113,14 @@ public void testIdWithReplicaShardUnassigned() { public void testToXContent() throws IOException { final ShardRouting shardRouting = newShardRouting("_index", 1, "_index_uuid", "_node_uuid", true, INITIALIZING); final MonitoringDoc.Node node = new MonitoringDoc.Node("_uuid", "_host", "_addr", "_ip", "_name", 1504169190855L); - final ShardMonitoringDoc doc = - new ShardMonitoringDoc("_cluster", 1502107402133L, 1506593717631L, node, shardRouting, "_state_uuid"); + final ShardMonitoringDoc doc = new ShardMonitoringDoc( + "_cluster", + 1502107402133L, + 1506593717631L, + node, + shardRouting, + "_state_uuid" + ); final BytesReference xContent = XContentHelper.toXContent(doc, XContentType.JSON, randomBoolean()); final String expected = "{" diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/BaseFilteredMonitoringDocTestCase.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/BaseFilteredMonitoringDocTestCase.java index 4055069b97765..316d2f825d7d6 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/BaseFilteredMonitoringDocTestCase.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/BaseFilteredMonitoringDocTestCase.java @@ -8,8 +8,8 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.util.set.Sets; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc; @@ -58,13 +58,17 @@ private void assertXContentFilters(final F document) { } public void testConstructorFiltersMustNotBeNull() { - expectThrows(NullPointerException.class, - () -> new TestFilteredMonitoringDoc(cluster, timestamp, interval, node, system, type, id, null)); + expectThrows( + NullPointerException.class, + () -> new TestFilteredMonitoringDoc(cluster, timestamp, interval, node, system, type, id, null) + ); } public void testConstructorFiltersMustNotBeEmpty() { - final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> new TestFilteredMonitoringDoc(cluster, timestamp, interval, node, system, type, id, emptySet())); + final IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> new TestFilteredMonitoringDoc(cluster, timestamp, interval, node, system, type, id, emptySet()) + ); assertThat(e.getMessage(), equalTo("xContentFilters must not be empty")); } @@ -75,10 +79,17 @@ public void testFilteredMonitoringDocToXContent() throws IOException { filters.add("_type.field_3"); filters.add("_type.field_5.sub_*"); - final MonitoringDoc.Node node = - new MonitoringDoc.Node("_uuid", "_host", "_addr", "_ip", "_name", 1504169190855L); - final TestFilteredMonitoringDoc document = new TestFilteredMonitoringDoc("_cluster", 1502266739402L, 1506593717631L, - node, MonitoredSystem.ES, "_type", "_id", filters); + final MonitoringDoc.Node node = new MonitoringDoc.Node("_uuid", "_host", "_addr", "_ip", "_name", 1504169190855L); + final TestFilteredMonitoringDoc document = new TestFilteredMonitoringDoc( + "_cluster", + 1502266739402L, + 1506593717631L, + node, + MonitoredSystem.ES, + "_type", + "_id", + filters + ); final BytesReference xContent = XContentHelper.toXContent(document, XContentType.JSON, false); final String expected = "{" @@ -111,14 +122,16 @@ public void testFilteredMonitoringDocToXContent() throws IOException { class TestFilteredMonitoringDoc extends FilteredMonitoringDoc { - TestFilteredMonitoringDoc(final String cluster, - final long timestamp, - final long intervalMillis, - final Node node, - final MonitoredSystem system, - final String type, - final String id, - final Set xContentFilters) { + TestFilteredMonitoringDoc( + final String cluster, + final long timestamp, + final long intervalMillis, + final Node node, + final MonitoredSystem system, + final String type, + final String id, + final Set xContentFilters + ) { super(cluster, timestamp, intervalMillis, node, system, type, id, xContentFilters); } diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/BaseMonitoringDocTestCase.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/BaseMonitoringDocTestCase.java index d56cc646bb4ee..05c496de2b5ab 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/BaseMonitoringDocTestCase.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/BaseMonitoringDocTestCase.java @@ -6,21 +6,21 @@ */ package org.elasticsearch.xpack.monitoring.exporter; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.UUIDs; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.time.DateUtils; import org.elasticsearch.common.transport.TransportAddress; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.EqualsHashCodeTestUtils; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.test.EqualsHashCodeTestUtils; import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc; import org.elasticsearch.xpack.monitoring.MonitoringTestUtils; @@ -71,13 +71,15 @@ public void setUp() throws Exception { * ie multiple calls with the same parameters within the same test must return * identical objects. */ - protected abstract T createMonitoringDoc(String cluster, - long timestamp, - long interval, - @Nullable MonitoringDoc.Node node, - MonitoredSystem system, - String type, - @Nullable String id); + protected abstract T createMonitoringDoc( + String cluster, + long timestamp, + long interval, + @Nullable MonitoringDoc.Node node, + MonitoredSystem system, + String type, + @Nullable String id + ); /** * Assert that two {@link MonitoringDoc} are equal. By default, it @@ -136,8 +138,10 @@ public final void testToXContentContainsCommonFields() throws IOException { final T document = createMonitoringDoc(cluster, timestamp, interval, node, system, type, id); final BytesReference bytes = XContentHelper.toXContent(document, xContentType, false); - try (XContentParser parser = xContentType.xContent() - .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, bytes.streamInput())) { + try ( + XContentParser parser = xContentType.xContent() + .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, bytes.streamInput()) + ) { final Map map = parser.map(); assertThat(map.get("cluster_uuid"), equalTo(cluster)); @@ -189,19 +193,28 @@ public void testMonitoringNodeToXContent() throws IOException { final MonitoringDoc.Node node = new MonitoringDoc.Node("_uuid", "_host", "_addr", "_ip", "_name", 1504169190855L); final BytesReference xContent = XContentHelper.toXContent(node, XContentType.JSON, randomBoolean()); - assertEquals("{" - + "\"uuid\":\"_uuid\"," - + "\"host\":\"_host\"," - + "\"transport_address\":\"_addr\"," - + "\"ip\":\"_ip\"," - + "\"name\":\"_name\"," - + "\"timestamp\":\"2017-08-31T08:46:30.855Z\"" - + "}" , xContent.utf8ToString()); + assertEquals( + "{" + + "\"uuid\":\"_uuid\"," + + "\"host\":\"_host\"," + + "\"transport_address\":\"_addr\"," + + "\"ip\":\"_ip\"," + + "\"name\":\"_name\"," + + "\"timestamp\":\"2017-08-31T08:46:30.855Z\"" + + "}", + xContent.utf8ToString() + ); } public void testMonitoringNodeEqualsAndHashcode() { - final EqualsHashCodeTestUtils.CopyFunction copy = node -> new MonitoringDoc.Node(node.getUUID(), node.getHost(), - node.getTransportAddress(), node.getIp(), node.getName(), node.getTimestamp()); + final EqualsHashCodeTestUtils.CopyFunction copy = node -> new MonitoringDoc.Node( + node.getUUID(), + node.getHost(), + node.getTransportAddress(), + node.getIp(), + node.getName(), + node.getTimestamp() + ); final List> mutations = new ArrayList<>(); mutations.add(n -> { diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/BytesReferenceMonitoringDocTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/BytesReferenceMonitoringDocTests.java index f1c364445cb29..81c5f57462b17 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/BytesReferenceMonitoringDocTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/BytesReferenceMonitoringDocTests.java @@ -8,11 +8,11 @@ import org.elasticsearch.common.UUIDs; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.test.EqualsHashCodeTestUtils; import org.elasticsearch.test.RandomObjects; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc; import org.elasticsearch.xpack.monitoring.MonitoringTestUtils; @@ -43,9 +43,15 @@ public void setUp() throws Exception { } @Override - protected BytesReferenceMonitoringDoc createMonitoringDoc(final String cluster, final long timestamp, final long intervalMillis, - final MonitoringDoc.Node node, - final MonitoredSystem system, final String type, final String id) { + protected BytesReferenceMonitoringDoc createMonitoringDoc( + final String cluster, + final long timestamp, + final long intervalMillis, + final MonitoringDoc.Node node, + final MonitoredSystem system, + final String type, + final String id + ) { return new BytesReferenceMonitoringDoc(cluster, timestamp, intervalMillis, node, system, type, id, xContentType, source); } @@ -60,23 +66,31 @@ protected void assertMonitoringDoc(final BytesReferenceMonitoringDoc document) { } public void testConstructorMonitoredSystemMustNotBeNull() { - expectThrows(NullPointerException.class, - () -> new BytesReferenceMonitoringDoc(cluster, timestamp, interval, node, null, type, id, xContentType, source)); + expectThrows( + NullPointerException.class, + () -> new BytesReferenceMonitoringDoc(cluster, timestamp, interval, node, null, type, id, xContentType, source) + ); } public void testConstructorTypeMustNotBeNull() { - expectThrows(NullPointerException.class, - () -> new BytesReferenceMonitoringDoc(cluster, timestamp, interval, node, system, null, id, xContentType, source)); + expectThrows( + NullPointerException.class, + () -> new BytesReferenceMonitoringDoc(cluster, timestamp, interval, node, system, null, id, xContentType, source) + ); } public void testConstructorXContentTypeMustNotBeNull() { - expectThrows(NullPointerException.class, - () -> new BytesReferenceMonitoringDoc(cluster, timestamp, interval, node, system, type, id, null, source)); + expectThrows( + NullPointerException.class, + () -> new BytesReferenceMonitoringDoc(cluster, timestamp, interval, node, system, type, id, null, source) + ); } public void testConstructorSourceMustNotBeNull() { - expectThrows(NullPointerException.class, - () -> new BytesReferenceMonitoringDoc(cluster, timestamp, interval, node, system, type, id, xContentType, null)); + expectThrows( + NullPointerException.class, + () -> new BytesReferenceMonitoringDoc(cluster, timestamp, interval, node, system, type, id, xContentType, null) + ); } @Override @@ -86,10 +100,18 @@ public void testToXContent() throws IOException { builder.field("field", "value"); builder.endObject(); - final MonitoringDoc.Node node = - new MonitoringDoc.Node("_uuid", "_host", "_addr", "_ip", "_name", 1504169190855L); - final BytesReferenceMonitoringDoc document = new BytesReferenceMonitoringDoc("_cluster", 1502266739402L, 1506593717631L, - node, KIBANA, "_type", "_id", xContentType, BytesReference.bytes(builder)); + final MonitoringDoc.Node node = new MonitoringDoc.Node("_uuid", "_host", "_addr", "_ip", "_name", 1504169190855L); + final BytesReferenceMonitoringDoc document = new BytesReferenceMonitoringDoc( + "_cluster", + 1502266739402L, + 1506593717631L, + node, + KIBANA, + "_type", + "_id", + xContentType, + BytesReference.bytes(builder) + ); final BytesReference xContent = XContentHelper.toXContent(document, XContentType.JSON, false); final String expected = "{" @@ -113,9 +135,15 @@ public void testToXContent() throws IOException { } public void testEqualsAndHashcode() { - final EqualsHashCodeTestUtils.CopyFunction copy = doc -> - createMonitoringDoc(doc.getCluster(), doc.getTimestamp(), doc.getIntervalMillis(), - doc.getNode(), doc.getSystem(), doc.getType(), doc.getId()); + final EqualsHashCodeTestUtils.CopyFunction copy = doc -> createMonitoringDoc( + doc.getCluster(), + doc.getTimestamp(), + doc.getIntervalMillis(), + doc.getNode(), + doc.getSystem(), + doc.getType(), + doc.getId() + ); final List> mutations = new ArrayList<>(); mutations.add(doc -> { @@ -123,56 +151,105 @@ public void testEqualsAndHashcode() { do { cluster = UUIDs.randomBase64UUID(); } while (cluster.equals(doc.getCluster())); - return createMonitoringDoc(cluster, doc.getTimestamp(), doc.getIntervalMillis(), - doc.getNode(), doc.getSystem(), doc.getType(), doc.getId()); + return createMonitoringDoc( + cluster, + doc.getTimestamp(), + doc.getIntervalMillis(), + doc.getNode(), + doc.getSystem(), + doc.getType(), + doc.getId() + ); }); mutations.add(doc -> { long timestamp; do { timestamp = randomNonNegativeLong(); } while (timestamp == doc.getTimestamp()); - return createMonitoringDoc(doc.getCluster(), timestamp, doc.getIntervalMillis(), - doc.getNode(), doc.getSystem(), doc.getType(), doc.getId()); + return createMonitoringDoc( + doc.getCluster(), + timestamp, + doc.getIntervalMillis(), + doc.getNode(), + doc.getSystem(), + doc.getType(), + doc.getId() + ); }); mutations.add(doc -> { long intervaMillis; do { intervaMillis = randomNonNegativeLong(); } while (intervaMillis == doc.getIntervalMillis()); - return createMonitoringDoc(doc.getCluster(), doc.getTimestamp(), intervaMillis, - doc.getNode(), doc.getSystem(), doc.getType(), doc.getId()); + return createMonitoringDoc( + doc.getCluster(), + doc.getTimestamp(), + intervaMillis, + doc.getNode(), + doc.getSystem(), + doc.getType(), + doc.getId() + ); }); mutations.add(doc -> { MonitoringDoc.Node node; do { node = MonitoringTestUtils.randomMonitoringNode(random()); } while (node.equals(doc.getNode())); - return createMonitoringDoc(doc.getCluster(), doc.getTimestamp(), doc.getIntervalMillis(), - node, doc.getSystem(), doc.getType(), doc.getId()); + return createMonitoringDoc( + doc.getCluster(), + doc.getTimestamp(), + doc.getIntervalMillis(), + node, + doc.getSystem(), + doc.getType(), + doc.getId() + ); }); mutations.add(doc -> { MonitoredSystem system; do { system = randomFrom(MonitoredSystem.values()); } while (system == doc.getSystem()); - return createMonitoringDoc(doc.getCluster(), doc.getTimestamp(), doc.getIntervalMillis(), - doc.getNode(), system, doc.getType(), doc.getId()); + return createMonitoringDoc( + doc.getCluster(), + doc.getTimestamp(), + doc.getIntervalMillis(), + doc.getNode(), + system, + doc.getType(), + doc.getId() + ); }); mutations.add(doc -> { String type; do { type = randomAlphaOfLength(5); } while (type.equals(doc.getType())); - return createMonitoringDoc(doc.getCluster(), doc.getTimestamp(), doc.getIntervalMillis(), - doc.getNode(), doc.getSystem(), type, doc.getId()); + return createMonitoringDoc( + doc.getCluster(), + doc.getTimestamp(), + doc.getIntervalMillis(), + doc.getNode(), + doc.getSystem(), + type, + doc.getId() + ); }); mutations.add(doc -> { String id; do { id = randomAlphaOfLength(10); } while (id.equals(doc.getId())); - return createMonitoringDoc(doc.getCluster(), doc.getTimestamp(), doc.getIntervalMillis(), - doc.getNode(), doc.getSystem(), doc.getType(), id); + return createMonitoringDoc( + doc.getCluster(), + doc.getTimestamp(), + doc.getIntervalMillis(), + doc.getNode(), + doc.getSystem(), + doc.getType(), + id + ); }); checkEqualsAndHashCode(createMonitoringDoc(cluster, timestamp, interval, node, system, type, id), copy, randomFrom(mutations)); diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/ClusterAlertsUtilTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/ClusterAlertsUtilTests.java index 07aa0087697d6..dbb1bf3261fc4 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/ClusterAlertsUtilTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/ClusterAlertsUtilTests.java @@ -6,24 +6,23 @@ */ package org.elasticsearch.xpack.monitoring.exporter; -import java.util.Arrays; -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import java.util.stream.Collectors; - import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsException; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.test.ESTestCase; - +import org.elasticsearch.xcontent.XContentType; import org.junit.Before; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasSize; @@ -101,16 +100,25 @@ public void testGetClusterAlertsBlacklistThrowsForUnknownWatchId() { final Set unknownIds = blacklist.stream().filter(id -> watchIds.contains(id) == false).collect(Collectors.toSet()); final String unknownIdsString = String.join(", ", unknownIds); - final SettingsException exception = - expectThrows(SettingsException.class, - () -> ClusterAlertsUtil.getClusterAlertsBlacklist(createConfigWithBlacklist("_random", blacklist))); - - assertThat(exception.getMessage(), - equalTo("[xpack.monitoring.exporters._random.cluster_alerts.management.blacklist] contains unrecognized Cluster " + - "Alert IDs [" + unknownIdsString + "]")); - - assertWarnings("[xpack.monitoring.exporters._random.cluster_alerts.management.blacklist] setting was deprecated in " + - "Elasticsearch and will be removed in a future release! See the breaking changes documentation for the next major version."); + final SettingsException exception = expectThrows( + SettingsException.class, + () -> ClusterAlertsUtil.getClusterAlertsBlacklist(createConfigWithBlacklist("_random", blacklist)) + ); + + assertThat( + exception.getMessage(), + equalTo( + "[xpack.monitoring.exporters._random.cluster_alerts.management.blacklist] contains unrecognized Cluster " + + "Alert IDs [" + + unknownIdsString + + "]" + ) + ); + + assertWarnings( + "[xpack.monitoring.exporters._random.cluster_alerts.management.blacklist] setting was deprecated in " + + "Elasticsearch and will be removed in a future release! See the breaking changes documentation for the next major version." + ); } public void testGetClusterAlertsBlacklist() { @@ -118,14 +126,16 @@ public void testGetClusterAlertsBlacklist() { assertThat(blacklist, equalTo(ClusterAlertsUtil.getClusterAlertsBlacklist(createConfigWithBlacklist("any", blacklist)))); - assertWarnings("[xpack.monitoring.exporters.any.cluster_alerts.management.blacklist] setting was deprecated in Elasticsearch " + - "and will be removed in a future release! See the breaking changes documentation for the next major version."); + assertWarnings( + "[xpack.monitoring.exporters.any.cluster_alerts.management.blacklist] setting was deprecated in Elasticsearch " + + "and will be removed in a future release! See the breaking changes documentation for the next major version." + ); } private Exporter.Config createConfigWithBlacklist(final String name, final List blacklist) { final Settings settings = Settings.builder() - .putList("xpack.monitoring.exporters." + name + ".cluster_alerts.management.blacklist", blacklist) - .build(); + .putList("xpack.monitoring.exporters." + name + ".cluster_alerts.management.blacklist", blacklist) + .build(); final ClusterService clusterService = mock(ClusterService.class); final XPackLicenseState licenseState = mock(XPackLicenseState.class); diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/ExportersTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/ExportersTests.java index 082830e0a5892..a7ca8f201650f 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/ExportersTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/ExportersTests.java @@ -19,11 +19,11 @@ import org.elasticsearch.common.time.DateFormatter; import org.elasticsearch.common.util.concurrent.AbstractRunnable; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.gateway.GatewayService; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc; import org.elasticsearch.xpack.core.ssl.SSLService; @@ -102,23 +102,28 @@ public void init() { sslService = mock(SSLService.class); // we always need to have the local exporter as it serves as the default one - factories.put(LocalExporter.TYPE, config -> new LocalExporter(config, client, new MonitoringMigrationCoordinator(), - mock(CleanerService.class))); + factories.put( + LocalExporter.TYPE, + config -> new LocalExporter(config, client, new MonitoringMigrationCoordinator(), mock(CleanerService.class)) + ); exporters = new Exporters(Settings.EMPTY, factories, clusterService, licenseState, threadContext, sslService); } public void testHostsMustBeSetIfTypeIsHttp() { final String prefix = "xpack.monitoring.exporters.example"; - final Settings settings = Settings.builder().put(prefix + ".type", "http").build(); + final Settings settings = Settings.builder().put(prefix + ".type", "http").build(); final IllegalArgumentException e = expectThrows( IllegalArgumentException.class, - () -> HttpExporter.TYPE_SETTING.getConcreteSetting(prefix + ".type").get(settings)); + () -> HttpExporter.TYPE_SETTING.getConcreteSetting(prefix + ".type").get(settings) + ); assertThat(e, hasToString(containsString("Failed to parse value [http] for setting [" + prefix + ".type]"))); assertThat(e.getCause(), instanceOf(SettingsException.class)); assertThat(e.getCause(), hasToString(containsString("host list for [" + prefix + ".host] is empty"))); - assertWarnings("[xpack.monitoring.exporters.example.type] setting was deprecated in Elasticsearch and will be removed in a " + - "future release! See the breaking changes documentation for the next major version."); + assertWarnings( + "[xpack.monitoring.exporters.example.type] setting was deprecated in Elasticsearch and will be removed in a " + + "future release! See the breaking changes documentation for the next major version." + ); } public void testIndexNameTimeFormatMustBeValid() { @@ -128,12 +133,15 @@ public void testIndexNameTimeFormatMustBeValid() { final Settings settings = Settings.builder().put(prefix + setting, value).build(); final IllegalArgumentException e = expectThrows( IllegalArgumentException.class, - () -> Exporter.INDEX_NAME_TIME_FORMAT_SETTING.getConcreteSetting(prefix + setting).get(settings)); + () -> Exporter.INDEX_NAME_TIME_FORMAT_SETTING.getConcreteSetting(prefix + setting).get(settings) + ); assertThat(e, hasToString(containsString("Invalid format: [" + value + "]: Unknown pattern letter: j"))); assertThat(e.getCause(), instanceOf(IllegalArgumentException.class)); assertThat(e.getCause(), hasToString(containsString("Unknown pattern letter: j"))); - assertWarnings("[xpack.monitoring.exporters.example.index.name.time_format] setting was deprecated in Elasticsearch and will " + - "be removed in a future release! See the breaking changes documentation for the next major version."); + assertWarnings( + "[xpack.monitoring.exporters.example.index.name.time_format] setting was deprecated in Elasticsearch and will " + + "be removed in a future release! See the breaking changes documentation for the next major version." + ); } public void testExporterIndexPattern() { @@ -163,9 +171,9 @@ public void testInitExportersDefault() throws Exception { public void testInitExportersSingle() throws Exception { factories.put("local", TestExporter::new); - Map internalExporters = exporters.initExporters(Settings.builder() - .put("xpack.monitoring.exporters._name.type", "local") - .build()).enabledExporters; + Map internalExporters = exporters.initExporters( + Settings.builder().put("xpack.monitoring.exporters._name.type", "local").build() + ).enabledExporters; assertThat(internalExporters, notNullValue()); assertThat(internalExporters.size(), is(1)); @@ -176,39 +184,48 @@ public void testInitExportersSingle() throws Exception { public void testInitExportersSingleDisabled() throws Exception { factories.put("local", TestExporter::new); - Map internalExporters = exporters.initExporters(Settings.builder() + Map internalExporters = exporters.initExporters( + Settings.builder() .put("xpack.monitoring.exporters._name.type", "local") .put("xpack.monitoring.exporters._name.enabled", false) - .build()).enabledExporters; + .build() + ).enabledExporters; assertThat(internalExporters, notNullValue()); // the only configured exporter is disabled... yet we intentionally don't fallback on the default assertThat(internalExporters.size(), is(0)); - assertWarnings("[xpack.monitoring.exporters._name.enabled] setting was deprecated in Elasticsearch and will be removed in a " + - "future release! See the breaking changes documentation for the next major version."); + assertWarnings( + "[xpack.monitoring.exporters._name.enabled] setting was deprecated in Elasticsearch and will be removed in a " + + "future release! See the breaking changes documentation for the next major version." + ); } public void testInitExportersSingleUnknownType() throws Exception { - SettingsException e = expectThrows(SettingsException.class, () -> exporters.initExporters(Settings.builder() - .put("xpack.monitoring.exporters._name.type", "unknown_type") - .build())); + SettingsException e = expectThrows( + SettingsException.class, + () -> exporters.initExporters(Settings.builder().put("xpack.monitoring.exporters._name.type", "unknown_type").build()) + ); assertThat(e.getMessage(), containsString("unknown exporter type [unknown_type]")); } public void testInitExportersSingleMissingExporterType() throws Exception { - SettingsException e = expectThrows(SettingsException.class, () -> exporters.initExporters( - Settings.builder().put("xpack.monitoring.exporters._name.foo", "bar").build())); + SettingsException e = expectThrows( + SettingsException.class, + () -> exporters.initExporters(Settings.builder().put("xpack.monitoring.exporters._name.foo", "bar").build()) + ); assertThat(e.getMessage(), containsString("missing exporter type for [_name]")); } public void testInitExportersMultipleSameType() throws Exception { factories.put("_type", TestExporter::new); - Map internalExporters = exporters.initExporters(Settings.builder() + Map internalExporters = exporters.initExporters( + Settings.builder() .put("xpack.monitoring.exporters._name0.type", "_type") .put("xpack.monitoring.exporters._name1.type", "_type") - .build()).enabledExporters; + .build() + ).enabledExporters; assertThat(internalExporters, notNullValue()); assertThat(internalExporters.size(), is(2)); @@ -222,11 +239,14 @@ public void testInitExportersMultipleSameType() throws Exception { public void testInitExportersMultipleSameTypeSingletons() throws Exception { factories.put("local", TestSingletonExporter::new); - SettingsException e = expectThrows(SettingsException.class, () -> - exporters.initExporters(Settings.builder() + SettingsException e = expectThrows( + SettingsException.class, + () -> exporters.initExporters( + Settings.builder() .put("xpack.monitoring.exporters._name0.type", "local") .put("xpack.monitoring.exporters._name1.type", "local") - .build()) + .build() + ) ); assertThat(e.getMessage(), containsString("multiple [local] exporters are configured. there can only be one")); } @@ -238,9 +258,9 @@ public void testSettingsUpdate() throws Exception { final AtomicReference settingsHolder = new AtomicReference<>(); Settings nodeSettings = Settings.builder() - .put("xpack.monitoring.exporters._name0.type", "local") - .put("xpack.monitoring.exporters._name1.type", "http") - .build(); + .put("xpack.monitoring.exporters._name0.type", "local") + .put("xpack.monitoring.exporters._name1.type", "http") + .build(); clusterSettings = new ClusterSettings(nodeSettings, new HashSet<>(Exporters.getSettings())); when(clusterService.getClusterSettings()).thenReturn(clusterSettings); @@ -260,9 +280,9 @@ InitializedExporters initExporters(Settings settings) { assertEquals(settings.get("xpack.monitoring.exporters._name1.type"), "http"); Settings update = Settings.builder() - .put("xpack.monitoring.exporters._name0.cluster_alerts.management.blacklist", true) - .put("xpack.monitoring.exporters._name1.cluster_alerts.management.blacklist", false) - .build(); + .put("xpack.monitoring.exporters._name0.cluster_alerts.management.blacklist", true) + .put("xpack.monitoring.exporters._name1.cluster_alerts.management.blacklist", false) + .build(); clusterSettings.applySettings(update); assertThat(settingsHolder.get(), notNullValue()); settings = settingsHolder.get(); @@ -274,14 +294,14 @@ InitializedExporters initExporters(Settings settings) { assertEquals(settings.get("xpack.monitoring.exporters._name1.cluster_alerts.management.blacklist"), "false"); assertWarnings( - "[xpack.monitoring.exporters._name1.type] setting was deprecated in Elasticsearch and will be removed in a future release! " + - "See the breaking changes documentation for the next major version.", - "[xpack.monitoring.exporters._name0.type] setting was deprecated in Elasticsearch and will be removed in a future release! " + - "See the breaking changes documentation for the next major version.", - "[xpack.monitoring.exporters._name0.cluster_alerts.management.blacklist] setting was deprecated in Elasticsearch and will " + - "be removed in a future release! See the breaking changes documentation for the next major version.", - "[xpack.monitoring.exporters._name1.cluster_alerts.management.blacklist] setting was deprecated in Elasticsearch and will " + - "be removed in a future release! See the breaking changes documentation for the next major version." + "[xpack.monitoring.exporters._name1.type] setting was deprecated in Elasticsearch and will be removed in a future release! " + + "See the breaking changes documentation for the next major version.", + "[xpack.monitoring.exporters._name0.type] setting was deprecated in Elasticsearch and will be removed in a future release! " + + "See the breaking changes documentation for the next major version.", + "[xpack.monitoring.exporters._name0.cluster_alerts.management.blacklist] setting was deprecated in Elasticsearch and will " + + "be removed in a future release! See the breaking changes documentation for the next major version.", + "[xpack.monitoring.exporters._name1.cluster_alerts.management.blacklist] setting was deprecated in Elasticsearch and will " + + "be removed in a future release! See the breaking changes documentation for the next major version." ); } @@ -304,10 +324,7 @@ public void testExporterBlocksOnClusterState() { final Exporters exporters = new Exporters(settings.build(), factories, clusterService, licenseState, threadContext, sslService); // synchronously checks the cluster state - exporters.wrapExportBulk(ActionListener.wrap( - bulk -> assertThat(bulk, is(nullValue())), - e -> fail(e.getMessage()) - )); + exporters.wrapExportBulk(ActionListener.wrap(bulk -> assertThat(bulk, is(nullValue())), e -> fail(e.getMessage()))); verify(state).blocks(); } @@ -316,10 +333,9 @@ public void testExporterBlocksOnClusterState() { * Verifies that, when no exporters are enabled, the {@code Exporters} will still return as expected. */ public void testNoExporters() throws Exception { - Settings.Builder settings = - Settings.builder() - .put("xpack.monitoring.exporters.explicitly_disabled.type", "local") - .put("xpack.monitoring.exporters.explicitly_disabled.enabled", false); + Settings.Builder settings = Settings.builder() + .put("xpack.monitoring.exporters.explicitly_disabled.type", "local") + .put("xpack.monitoring.exporters.explicitly_disabled.enabled", false); Exporters exporters = new Exporters(settings.build(), factories, clusterService, licenseState, threadContext, sslService); exporters.start(); @@ -330,8 +346,10 @@ public void testNoExporters() throws Exception { exporters.close(); - assertWarnings("[xpack.monitoring.exporters.explicitly_disabled.enabled] setting was deprecated in Elasticsearch and will be " + - "removed in a future release! See the breaking changes documentation for the next major version."); + assertWarnings( + "[xpack.monitoring.exporters.explicitly_disabled.enabled] setting was deprecated in Elasticsearch and will be " + + "removed in a future release! See the breaking changes documentation for the next major version." + ); } /** @@ -368,10 +386,19 @@ public void testConcurrentExports() throws Exception { */ public void testSettingsDependency() { List> settings = Exporters.getSettings().stream().filter(Setting::isDynamic).collect(Collectors.toList()); - settings.stream().filter(s -> s.getKey().equals("xpack.monitoring.exporters.*.type") == false) - .forEach(setting -> assertThat(setting.getKey() + " does not have a dependency on type", - setting.getDependencies().stream().map(Setting.AffixSettingDependency::getSetting).distinct().collect(Collectors.toList()), - contains(Exporter.TYPE_SETTING))); + settings.stream() + .filter(s -> s.getKey().equals("xpack.monitoring.exporters.*.type") == false) + .forEach( + setting -> assertThat( + setting.getKey() + " does not have a dependency on type", + setting.getDependencies() + .stream() + .map(Setting.AffixSettingDependency::getSetting) + .distinct() + .collect(Collectors.toList()), + contains(Exporter.TYPE_SETTING) + ) + ); } /** @@ -380,30 +407,28 @@ public void testSettingsDependency() { * then any associated settings are extraneous and thus invalid (and can cause validation issues on cluster state application). */ public void testRemoveType() { - //run the update for all dynamic settings and ensure that they correctly throw an exception + // run the update for all dynamic settings and ensure that they correctly throw an exception List> settings = Exporters.getSettings().stream().filter(Setting::isDynamic).collect(Collectors.toList()); - settings.stream().filter(s -> s.getKey().equals("xpack.monitoring.exporters.*.type") == false) - .forEach(setting -> { - String fullSettingName = setting.getKey().replace("*", "foobar"); - Settings nodeSettings = Settings.builder() - .put("xpack.monitoring.exporters.foobar.type", randomFrom("local, http")) //actual type should not matter - .put(fullSettingName, "") - .build(); - - clusterSettings = new ClusterSettings(nodeSettings, new HashSet<>(Exporters.getSettings())); - when(clusterService.getClusterSettings()).thenReturn(clusterSettings); - - Settings update = Settings.builder() - .put("xpack.monitoring.exporters.foobar.type", (String) null) - .build(); - - Settings.Builder target = Settings.builder().put(nodeSettings); - clusterSettings.updateDynamicSettings(update, target, Settings.builder(), "persistent"); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> clusterSettings.validate(target.build(), true)); - assertThat(e.getMessage(), - containsString("missing required setting [xpack.monitoring.exporters.foobar.type] for setting [" + fullSettingName)); - }); + settings.stream().filter(s -> s.getKey().equals("xpack.monitoring.exporters.*.type") == false).forEach(setting -> { + String fullSettingName = setting.getKey().replace("*", "foobar"); + Settings nodeSettings = Settings.builder() + .put("xpack.monitoring.exporters.foobar.type", randomFrom("local, http")) // actual type should not matter + .put(fullSettingName, "") + .build(); + + clusterSettings = new ClusterSettings(nodeSettings, new HashSet<>(Exporters.getSettings())); + when(clusterService.getClusterSettings()).thenReturn(clusterSettings); + + Settings update = Settings.builder().put("xpack.monitoring.exporters.foobar.type", (String) null).build(); + + Settings.Builder target = Settings.builder().put(nodeSettings); + clusterSettings.updateDynamicSettings(update, target, Settings.builder(), "persistent"); + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> clusterSettings.validate(target.build(), true)); + assertThat( + e.getMessage(), + containsString("missing required setting [xpack.monitoring.exporters.foobar.type] for setting [" + fullSettingName) + ); + }); } /** @@ -436,19 +461,26 @@ public void onFailure(Exception e) { protected void doRun() throws Exception { final List docs = new ArrayList<>(); for (int n = 0; n < threadDocs; n++) { - docs.add(new TestMonitoringDoc(randomAlphaOfLength(5), randomNonNegativeLong(), randomNonNegativeLong(), - null, MonitoredSystem.ES, randomAlphaOfLength(5), null, String.valueOf(n))); + docs.add( + new TestMonitoringDoc( + randomAlphaOfLength(5), + randomNonNegativeLong(), + randomNonNegativeLong(), + null, + MonitoredSystem.ES, + randomAlphaOfLength(5), + null, + String.valueOf(n) + ) + ); } - exporters.export(docs, ActionListener.wrap( - r -> { - counter.decrementAndGet(); - logger.debug("--> thread [{}] successfully exported {} documents", threadNum, threadDocs); - }, - e -> { - exceptions.add(e); - logger.debug("--> thread [{}] failed to export {} documents", threadNum, threadDocs); - }) - ); + exporters.export(docs, ActionListener.wrap(r -> { + counter.decrementAndGet(); + logger.debug("--> thread [{}] successfully exported {} documents", threadNum, threadDocs); + }, e -> { + exceptions.add(e); + logger.debug("--> thread [{}] failed to export {} documents", threadNum, threadDocs); + })); barrier.await(10, TimeUnit.SECONDS); } }, "export_thread_" + i); @@ -472,8 +504,7 @@ static class TestExporter extends Exporter { } @Override - public void removeAlerts(Consumer listener) { - } + public void removeAlerts(Consumer listener) {} @Override public void openBulk(final ActionListener listener) { @@ -481,8 +512,7 @@ public void openBulk(final ActionListener listener) { } @Override - public void doClose() { - } + public void doClose() {} } static class TestSingletonExporter extends TestExporter { @@ -508,8 +538,7 @@ static class CountingExporter extends Exporter { } @Override - public void removeAlerts(Consumer listener) { - } + public void removeAlerts(Consumer listener) {} @Override public void openBulk(final ActionListener listener) { @@ -520,8 +549,7 @@ public void openBulk(final ActionListener listener) { } @Override - public void doClose() { - } + public void doClose() {} public int getExportedCount() { int exported = 0; @@ -559,8 +587,16 @@ static class TestMonitoringDoc extends MonitoringDoc { private final String value; - TestMonitoringDoc(String cluster, long timestamp, long interval, - Node node, MonitoredSystem system, String type, String id, String value) { + TestMonitoringDoc( + String cluster, + long timestamp, + long interval, + Node node, + MonitoredSystem system, + String type, + String id, + String value + ) { super(cluster, timestamp, interval, node, system, type, id); this.value = value; } diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/MonitoringTemplateUtilsTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/MonitoringTemplateUtilsTests.java index 6c1de5c6352b6..56b669d356f89 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/MonitoringTemplateUtilsTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/MonitoringTemplateUtilsTests.java @@ -20,27 +20,39 @@ public class MonitoringTemplateUtilsTests extends ESTestCase { public void testIndexName() { - final long timestamp = ZonedDateTime.of(2017, 8, 3, 13, 47, 58, - 0, ZoneOffset.UTC).toInstant().toEpochMilli(); + final long timestamp = ZonedDateTime.of(2017, 8, 3, 13, 47, 58, 0, ZoneOffset.UTC).toInstant().toEpochMilli(); DateFormatter formatter = DateFormatter.forPattern("yyyy.MM.dd").withZone(ZoneOffset.UTC); - assertThat(indexName(formatter, MonitoredSystem.ES, timestamp), - equalTo(".monitoring-es-" + TEMPLATE_VERSION + "-2017.08.03")); - assertThat(indexName(formatter, MonitoredSystem.KIBANA, timestamp), - equalTo(".monitoring-kibana-" + TEMPLATE_VERSION + "-2017.08.03")); - assertThat(indexName(formatter, MonitoredSystem.LOGSTASH, timestamp), - equalTo(".monitoring-logstash-" + TEMPLATE_VERSION + "-2017.08.03")); - assertThat(indexName(formatter, MonitoredSystem.BEATS, timestamp), - equalTo(".monitoring-beats-" + TEMPLATE_VERSION + "-2017.08.03")); + assertThat(indexName(formatter, MonitoredSystem.ES, timestamp), equalTo(".monitoring-es-" + TEMPLATE_VERSION + "-2017.08.03")); + assertThat( + indexName(formatter, MonitoredSystem.KIBANA, timestamp), + equalTo(".monitoring-kibana-" + TEMPLATE_VERSION + "-2017.08.03") + ); + assertThat( + indexName(formatter, MonitoredSystem.LOGSTASH, timestamp), + equalTo(".monitoring-logstash-" + TEMPLATE_VERSION + "-2017.08.03") + ); + assertThat( + indexName(formatter, MonitoredSystem.BEATS, timestamp), + equalTo(".monitoring-beats-" + TEMPLATE_VERSION + "-2017.08.03") + ); formatter = DateFormatter.forPattern("yyyy-dd-MM-HH.mm.ss").withZone(ZoneOffset.UTC); - assertThat(indexName(formatter, MonitoredSystem.ES, timestamp), - equalTo(".monitoring-es-" + TEMPLATE_VERSION + "-2017-03-08-13.47.58")); - assertThat(indexName(formatter, MonitoredSystem.KIBANA, timestamp), - equalTo(".monitoring-kibana-" + TEMPLATE_VERSION + "-2017-03-08-13.47.58")); - assertThat(indexName(formatter, MonitoredSystem.LOGSTASH, timestamp), - equalTo(".monitoring-logstash-" + TEMPLATE_VERSION + "-2017-03-08-13.47.58")); - assertThat(indexName(formatter, MonitoredSystem.BEATS, timestamp), - equalTo(".monitoring-beats-" + TEMPLATE_VERSION + "-2017-03-08-13.47.58")); + assertThat( + indexName(formatter, MonitoredSystem.ES, timestamp), + equalTo(".monitoring-es-" + TEMPLATE_VERSION + "-2017-03-08-13.47.58") + ); + assertThat( + indexName(formatter, MonitoredSystem.KIBANA, timestamp), + equalTo(".monitoring-kibana-" + TEMPLATE_VERSION + "-2017-03-08-13.47.58") + ); + assertThat( + indexName(formatter, MonitoredSystem.LOGSTASH, timestamp), + equalTo(".monitoring-logstash-" + TEMPLATE_VERSION + "-2017-03-08-13.47.58") + ); + assertThat( + indexName(formatter, MonitoredSystem.BEATS, timestamp), + equalTo(".monitoring-beats-" + TEMPLATE_VERSION + "-2017-03-08-13.47.58") + ); } } diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/AbstractPublishableHttpResourceTestCase.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/AbstractPublishableHttpResourceTestCase.java index d5887cb2d53e3..4878289cae8d6 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/AbstractPublishableHttpResourceTestCase.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/AbstractPublishableHttpResourceTestCase.java @@ -17,9 +17,9 @@ import org.elasticsearch.client.ResponseException; import org.elasticsearch.client.ResponseListener; import org.elasticsearch.client.RestClient; +import org.elasticsearch.common.util.set.Sets; import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.common.util.set.Sets; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.monitoring.exporter.http.HttpResource.ResourcePublishResult; @@ -69,9 +69,11 @@ public abstract class AbstractPublishableHttpResourceTestCase extends ESTestCase * @param resourceBasePath The base endpoint (e.g., "/_template") * @param resourceName The resource name (e.g., the template or pipeline name). */ - protected void assertCheckDoesNotExist(final PublishableHttpResource resource, - final String resourceBasePath, - final String resourceName) { + protected void assertCheckDoesNotExist( + final PublishableHttpResource resource, + final String resourceBasePath, + final String resourceName + ) { doCheckWithStatusCode(resource, resourceBasePath, resourceName, notFoundCheckStatus(), false); } @@ -83,8 +85,11 @@ protected void assertCheckDoesNotExist(final PublishableHttpResource resource, * @param resourceBasePath The base endpoint (e.g., "/_template") * @param resourceName The resource name (e.g., the template or pipeline name). */ - protected void assertCheckWithException(final PublishableHttpResource resource, - final String resourceBasePath, final String resourceName) { + protected void assertCheckWithException( + final PublishableHttpResource resource, + final String resourceBasePath, + final String resourceName + ) { assertCheckWithException(resource, getParameters(resource.getDefaultParameters()), resourceBasePath, resourceName); } @@ -97,9 +102,12 @@ protected void assertCheckWithException(final PublishableHttpResource resource, * @param resourceBasePath The base endpoint (e.g., "/_template") * @param resourceName The resource name (e.g., the template or pipeline name). */ - protected void assertCheckWithException(final PublishableHttpResource resource, - final Map expectedParameters, - final String resourceBasePath, final String resourceName) { + protected void assertCheckWithException( + final PublishableHttpResource resource, + final Map expectedParameters, + final String resourceBasePath, + final String resourceName + ) { final String endpoint = concatenateEndpoint(resourceBasePath, resourceName); final ResponseException responseException = responseException("GET", endpoint, failedCheckStatus()); final Exception e = randomFrom(new IOException("expected"), new RuntimeException("expected"), responseException); @@ -122,8 +130,11 @@ protected void assertCheckWithException(final PublishableHttpResource resource, * @param resourceBasePath The base endpoint (e.g., "/_template") * @param resourceName The resource name (e.g., the template or pipeline name). */ - protected void assertCheckAsDeleteExists(final PublishableHttpResource resource, - final String resourceBasePath, final String resourceName) { + protected void assertCheckAsDeleteExists( + final PublishableHttpResource resource, + final String resourceBasePath, + final String resourceName + ) { final RestStatus status = randomFrom(successfulCheckStatus(), notFoundCheckStatus()); doCheckAsDeleteWithStatusCode(resource, resourceBasePath, resourceName, status, true); @@ -137,8 +148,11 @@ protected void assertCheckAsDeleteExists(final PublishableHttpResource resource, * @param resourceBasePath The base endpoint (e.g., "/_template") * @param resourceName The resource name (e.g., the template or pipeline name). */ - protected void assertCheckAsDeleteWithException(final PublishableHttpResource resource, - final String resourceBasePath, final String resourceName) { + protected void assertCheckAsDeleteWithException( + final PublishableHttpResource resource, + final String resourceBasePath, + final String resourceName + ) { final String endpoint = concatenateEndpoint(resourceBasePath, resourceName); final ResponseException responseException = responseException("DELETE", endpoint, failedCheckStatus()); final Exception e = randomFrom(new IOException("expected"), new RuntimeException("expected"), responseException); @@ -162,9 +176,13 @@ protected void assertCheckAsDeleteWithException(final PublishableHttpResource re * @param parameters Map of query string parameters, if any. * @param bodyType The request body provider's type. */ - protected void assertPublishSucceeds(final PublishableHttpResource resource, final String resourceBasePath, final String resourceName, - Map parameters, - final Class bodyType) { + protected void assertPublishSucceeds( + final PublishableHttpResource resource, + final String resourceBasePath, + final String resourceName, + Map parameters, + final Class bodyType + ) { doPublishWithStatusCode(resource, resourceBasePath, resourceName, parameters, bodyType, successfulPublishStatus(), true); } @@ -177,10 +195,13 @@ protected void assertPublishSucceeds(final PublishableHttpResource resource, fin * @param parameters Map of query string parameters, if any. * @param resourceName The resource name (e.g., the template or pipeline name). */ - protected void assertPublishWithException(final PublishableHttpResource resource, - final String resourceBasePath, final String resourceName, - Map parameters, - final Class bodyType) { + protected void assertPublishWithException( + final PublishableHttpResource resource, + final String resourceBasePath, + final String resourceName, + Map parameters, + final Class bodyType + ) { final String endpoint = concatenateEndpoint(resourceBasePath, resourceName); final Exception e = randomFrom(new IOException("expected"), new RuntimeException("expected")); @@ -190,7 +211,7 @@ protected void assertPublishWithException(final PublishableHttpResource resource verifyPublishListener(null); - Map allParameters = new HashMap<>(); + Map allParameters = new HashMap<>(); allParameters.putAll(resource.getDefaultParameters()); allParameters.putAll(parameters); @@ -229,35 +250,62 @@ protected void assertVersionParameters(final PublishableHttpResource resource) { assertThat(parameters.isEmpty(), is(true)); } - protected void doCheckWithStatusCode(final PublishableHttpResource resource, final String resourceBasePath, final String resourceName, - final RestStatus status, - final Boolean expected) { + protected void doCheckWithStatusCode( + final PublishableHttpResource resource, + final String resourceBasePath, + final String resourceName, + final RestStatus status, + final Boolean expected + ) { doCheckWithStatusCode(resource, resourceBasePath, resourceName, status, expected, null); } - protected void doCheckWithStatusCode(final PublishableHttpResource resource, final String resourceBasePath, final String resourceName, - final RestStatus status, final Boolean expected, final HttpEntity entity) { + protected void doCheckWithStatusCode( + final PublishableHttpResource resource, + final String resourceBasePath, + final String resourceName, + final RestStatus status, + final Boolean expected, + final HttpEntity entity + ) { doCheckWithStatusCode(resource, resourceBasePath, resourceName, status, GET_EXISTS, GET_DOES_NOT_EXIST, expected, entity); } - protected void doCheckWithStatusCode(final PublishableHttpResource resource, final String resourceBasePath, final String resourceName, - final RestStatus status, final Set exists, final Set doesNotExist, - final Boolean expected) { + protected void doCheckWithStatusCode( + final PublishableHttpResource resource, + final String resourceBasePath, + final String resourceName, + final RestStatus status, + final Set exists, + final Set doesNotExist, + final Boolean expected + ) { doCheckWithStatusCode(resource, resourceBasePath, resourceName, status, exists, doesNotExist, expected, null); } - protected void doCheckWithStatusCode(final PublishableHttpResource resource, final String resourceBasePath, final String resourceName, - final RestStatus status, final Set exists, final Set doesNotExist, - final Boolean expected, final HttpEntity entity) { + protected void doCheckWithStatusCode( + final PublishableHttpResource resource, + final String resourceBasePath, + final String resourceName, + final RestStatus status, + final Set exists, + final Set doesNotExist, + final Boolean expected, + final HttpEntity entity + ) { final String endpoint = concatenateEndpoint(resourceBasePath, resourceName); final Response response = response("GET", endpoint, status, entity); doCheckWithStatusCode(resource, getParameters(resource.getDefaultParameters(), exists, doesNotExist), endpoint, expected, response); } - protected void doCheckWithStatusCode(final PublishableHttpResource resource, final Map expectedParameters, - final String endpoint, final Boolean expected, - final Response response) { + protected void doCheckWithStatusCode( + final PublishableHttpResource resource, + final Map expectedParameters, + final String endpoint, + final Boolean expected, + final Response response + ) { final Request request = new Request("GET", endpoint); addParameters(request, expectedParameters); @@ -269,11 +317,15 @@ protected void doCheckWithStatusCode(final PublishableHttpResource resource, fin verifyCheckListener(expected); } - private void doPublishWithStatusCode(final PublishableHttpResource resource, final String resourceBasePath, final String resourceName, - Map parameters, - final Class bodyType, - final RestStatus status, - final boolean errorFree) { + private void doPublishWithStatusCode( + final PublishableHttpResource resource, + final String resourceBasePath, + final String resourceName, + Map parameters, + final Class bodyType, + final RestStatus status, + final boolean errorFree + ) { final String endpoint = concatenateEndpoint(resourceBasePath, resourceName); final Response response = response("GET", endpoint, status); @@ -286,7 +338,7 @@ private void doPublishWithStatusCode(final PublishableHttpResource resource, fin final ArgumentCaptor request = ArgumentCaptor.forClass(Request.class); verify(client).performRequestAsync(request.capture(), any(ResponseListener.class)); - Map allParameters = new HashMap<>(); + Map allParameters = new HashMap<>(); allParameters.putAll(resource.getDefaultParameters()); allParameters.putAll(parameters); @@ -296,19 +348,25 @@ private void doPublishWithStatusCode(final PublishableHttpResource resource, fin assertThat(request.getValue().getEntity(), instanceOf(bodyType)); } - protected void doCheckAsDeleteWithStatusCode(final PublishableHttpResource resource, - final String resourceBasePath, final String resourceName, - final RestStatus status, - final Boolean expected) { + protected void doCheckAsDeleteWithStatusCode( + final PublishableHttpResource resource, + final String resourceBasePath, + final String resourceName, + final RestStatus status, + final Boolean expected + ) { final String endpoint = concatenateEndpoint(resourceBasePath, resourceName); final Response response = response("DELETE", endpoint, status); doCheckAsDeleteWithStatusCode(resource, endpoint, expected, response); } - protected void doCheckAsDeleteWithStatusCode(final PublishableHttpResource resource, - final String endpoint, final Boolean expected, - final Response response) { + protected void doCheckAsDeleteWithStatusCode( + final PublishableHttpResource resource, + final String endpoint, + final Boolean expected, + final Response response + ) { final Request request = new Request("DELETE", endpoint); addParameters(request, deleteParameters(resource.getDefaultParameters())); whenPerformRequestAsyncWith(client, request, response); @@ -377,8 +435,11 @@ protected Map getParameters(final Map parameters return getParameters(parameters, GET_EXISTS, GET_DOES_NOT_EXIST); } - protected Map getParameters(final Map parameters, - final Set exists, final Set doesNotExist) { + protected Map getParameters( + final Map parameters, + final Set exists, + final Set doesNotExist + ) { final Set statusCodes = Sets.union(exists, doesNotExist); final Map parametersWithIgnore = new HashMap<>(parameters); @@ -431,8 +492,10 @@ protected HttpEntity entityForClusterAlert(final Boolean expected, final int min ); } else if (expected == Boolean.TRUE) { // the version is there and it's exactly what we specify - return new StringEntity("{\"metadata\":{\"xpack\":{\"version_created\":" + - minimumVersion + "}}}", ContentType.APPLICATION_JSON); + return new StringEntity( + "{\"metadata\":{\"xpack\":{\"version_created\":" + minimumVersion + "}}}", + ContentType.APPLICATION_JSON + ); } else { // expected == null, which is for malformed/failure // malformed return randomFrom( diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/AsyncHttpResourceHelper.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/AsyncHttpResourceHelper.java index 6b1632461a01b..201a2b7872975 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/AsyncHttpResourceHelper.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/AsyncHttpResourceHelper.java @@ -7,7 +7,6 @@ package org.elasticsearch.xpack.monitoring.exporter.http; -import java.util.List; import org.elasticsearch.action.ActionListener; import org.elasticsearch.client.Request; import org.elasticsearch.client.Response; @@ -16,6 +15,8 @@ import org.hamcrest.Matcher; import org.mockito.stubbing.Stubber; +import java.util.List; + import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.argThat; import static org.mockito.ArgumentMatchers.eq; @@ -41,14 +42,14 @@ static ActionListener wrapMockListener(ActionListener mock) { static void whenPerformRequestAsyncWith(final RestClient client, final Response response) { doAnswer(invocation -> { - ((ResponseListener)invocation.getArguments()[1]).onSuccess(response); + ((ResponseListener) invocation.getArguments()[1]).onSuccess(response); return null; }).when(client).performRequestAsync(any(Request.class), any(ResponseListener.class)); } static void whenPerformRequestAsyncWith(final RestClient client, final Matcher request, final Response response) { doAnswer(invocation -> { - ((ResponseListener)invocation.getArguments()[1]).onSuccess(response); + ((ResponseListener) invocation.getArguments()[1]).onSuccess(response); return null; }).when(client).performRequestAsync(argThat(request::matches), any(ResponseListener.class)); } @@ -61,20 +62,24 @@ static void whenPerformRequestAsyncWith(final RestClient client, final Matcher request, - final Response response, - final Exception exception) { + static void whenPerformRequestAsyncWith( + final RestClient client, + final Matcher request, + final Response response, + final Exception exception + ) { whenPerformRequestAsyncWith(client, request, response, null, exception); } - static void whenPerformRequestAsyncWith(final RestClient client, - final Matcher request, - final Response first, - final List responses, - final Exception exception) { + static void whenPerformRequestAsyncWith( + final RestClient client, + final Matcher request, + final Response first, + final List responses, + final Exception exception + ) { Stubber stub = doAnswer(invocation -> { - ((ResponseListener)invocation.getArguments()[1]).onSuccess(first); + ((ResponseListener) invocation.getArguments()[1]).onSuccess(first); return null; }); @@ -99,28 +104,28 @@ static void whenPerformRequestAsyncWith(final RestClient client, static void whenPerformRequestAsyncWith(final RestClient client, final Request request, final Response response) { doAnswer(invocation -> { - ((ResponseListener)invocation.getArguments()[1]).onSuccess(response); + ((ResponseListener) invocation.getArguments()[1]).onSuccess(response); return null; }).when(client).performRequestAsync(eq(request), any(ResponseListener.class)); } static void whenPerformRequestAsyncWith(final RestClient client, final Exception exception) { doAnswer(invocation -> { - ((ResponseListener)invocation.getArguments()[1]).onFailure(exception); + ((ResponseListener) invocation.getArguments()[1]).onFailure(exception); return null; }).when(client).performRequestAsync(any(Request.class), any(ResponseListener.class)); } static void whenPerformRequestAsyncWith(final RestClient client, final Matcher request, final Exception exception) { doAnswer(invocation -> { - ((ResponseListener)invocation.getArguments()[1]).onFailure(exception); + ((ResponseListener) invocation.getArguments()[1]).onFailure(exception); return null; }).when(client).performRequestAsync(argThat(request::matches), any(ResponseListener.class)); } static void whenPerformRequestAsyncWith(final RestClient client, final Request request, final Exception exception) { doAnswer(invocation -> { - ((ResponseListener)invocation.getArguments()[1]).onFailure(exception); + ((ResponseListener) invocation.getArguments()[1]).onFailure(exception); return null; }).when(client).performRequestAsync(eq(request), any(ResponseListener.class)); } diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExportBulkResponseListenerTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExportBulkResponseListenerTests.java index 69d5831a0db6c..559db2ce3c6ae 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExportBulkResponseListenerTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExportBulkResponseListenerTests.java @@ -10,6 +10,7 @@ import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.elasticsearch.client.Response; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContent; @@ -17,7 +18,6 @@ import org.elasticsearch.xcontent.XContentParser.Token; import org.elasticsearch.xcontent.XContentType; import org.mockito.Mockito; -import org.elasticsearch.test.ESTestCase; import java.io.IOException; import java.io.InputStream; @@ -53,13 +53,17 @@ public void testOnSuccessParsing() throws IOException { when(response.getEntity()).thenReturn(entity); when(entity.getContent()).thenReturn(stream); - when(xContent.createParser(Mockito.any(NamedXContentRegistry.class), - Mockito.any(DeprecationHandler.class), Mockito.eq(stream))).thenReturn(parser); + when(xContent.createParser(Mockito.any(NamedXContentRegistry.class), Mockito.any(DeprecationHandler.class), Mockito.eq(stream))) + .thenReturn(parser); // {, "took", 4, "errors", false - when(parser.nextToken()).thenReturn(Token.START_OBJECT, - Token.FIELD_NAME, Token.VALUE_NUMBER, - Token.FIELD_NAME, Token.VALUE_BOOLEAN); + when(parser.nextToken()).thenReturn( + Token.START_OBJECT, + Token.FIELD_NAME, + Token.VALUE_NUMBER, + Token.FIELD_NAME, + Token.VALUE_BOOLEAN + ); when(parser.currentName()).thenReturn("took", "errors"); when(parser.booleanValue()).thenReturn(false); @@ -75,15 +79,20 @@ public void testOnSuccessWithInnerErrors() { final AtomicInteger counter = new AtomicInteger(0); final Response response = mock(Response.class); final StringEntity entity = new StringEntity( - "{\"took\":4,\"errors\":true,\"items\":[" + - "{\"index\":{\"_index\":\".monitoring-data-2\",\"_type\":\"node\",\"_id\":\"123\"}}," + - "{\"index\":{\"_index\":\".monitoring-data-2\",\"_type\":\"node\",\"_id\":\"456\"," + - "\"error\":\"" + expectedErrors[0] + "\"}}," + - "{\"index\":{\"_index\":\".monitoring-data-2\",\"_type\":\"node\",\"_id\":\"789\"}}," + - "{\"index\":{\"_index\":\".monitoring-data-2\",\"_type\":\"node\",\"_id\":\"012\"," + - "\"error\":\"" + expectedErrors[1] + "\"}}" + - "]}", - ContentType.APPLICATION_JSON); + "{\"took\":4,\"errors\":true,\"items\":[" + + "{\"index\":{\"_index\":\".monitoring-data-2\",\"_type\":\"node\",\"_id\":\"123\"}}," + + "{\"index\":{\"_index\":\".monitoring-data-2\",\"_type\":\"node\",\"_id\":\"456\"," + + "\"error\":\"" + + expectedErrors[0] + + "\"}}," + + "{\"index\":{\"_index\":\".monitoring-data-2\",\"_type\":\"node\",\"_id\":\"789\"}}," + + "{\"index\":{\"_index\":\".monitoring-data-2\",\"_type\":\"node\",\"_id\":\"012\"," + + "\"error\":\"" + + expectedErrors[1] + + "\"}}" + + "]}", + ContentType.APPLICATION_JSON + ); when(response.getEntity()).thenReturn(entity); @@ -100,8 +109,8 @@ void onItemError(final String text) { public void testOnSuccessParsingWithInnerErrors() throws IOException { // {"took": 4, "errors": true, "items": [ { "index": { "_index": "ignored", "_type": "ignored", "_id": "ignored" }, - // { "index": { "_index": "ignored", "_type": "ignored", "_id": "ignored", "error": "blah" } - // ]... + // { "index": { "_index": "ignored", "_type": "ignored", "_id": "ignored", "error": "blah" } + // ]... final Response response = mock(Response.class); final XContent xContent = mock(XContent.class); final XContentParser parser = mock(XContentParser.class); @@ -110,8 +119,8 @@ public void testOnSuccessParsingWithInnerErrors() throws IOException { when(response.getEntity()).thenReturn(entity); when(entity.getContent()).thenReturn(stream); - when(xContent.createParser(Mockito.any(NamedXContentRegistry.class), - Mockito.any(DeprecationHandler.class), Mockito.eq(stream))).thenReturn(parser); + when(xContent.createParser(Mockito.any(NamedXContentRegistry.class), Mockito.any(DeprecationHandler.class), Mockito.eq(stream))) + .thenReturn(parser); // tag::disable-formatting // {, "took", 4, "errors", false @@ -136,9 +145,20 @@ public void testOnSuccessParsingWithInnerErrors() throws IOException { Token.END_OBJECT, // 29 Token.END_ARRAY); // 30 // end::disable-formatting - when(parser.currentName()).thenReturn("took", "errors", "items", - "index", "_index", "_type", "_id", - "index", "_index", "_type", "_id", "error"); + when(parser.currentName()).thenReturn( + "took", + "errors", + "items", + "index", + "_index", + "_type", + "_id", + "index", + "_index", + "_type", + "_id", + "error" + ); // there were errors; so go diving for the error when(parser.booleanValue()).thenReturn(true); when(parser.text()).thenReturn("this is the error"); diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporterResourceTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporterResourceTests.java index 95494bdeff638..b741aa9513b95 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporterResourceTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporterResourceTests.java @@ -80,9 +80,9 @@ public class HttpExporterResourceTests extends AbstractPublishableHttpResourceTe .put("xpack.monitoring.exporters._http.cluster_alerts.management.enabled", true) .build(); - private final MultiHttpResource resources = - HttpExporter.createResources( - new Exporter.Config("_http", "http", exporterSettings, clusterService, licenseState)).allResources; + private final MultiHttpResource resources = HttpExporter.createResources( + new Exporter.Config("_http", "http", exporterSettings, clusterService, licenseState) + ).allResources; @Before public void setupResources() { @@ -113,8 +113,14 @@ public void testInvalidVersionBlocks() { whenPerformRequestAsyncWith(client, new RequestMatcher(is("GET"), is("/")), versionResponse); assertTrue(resources.isDirty()); - awaitCheckAndPublish(resources, new ResourcePublishResult(false, - "version [3.0.0] < [7.0.0] and NOT supported for [xpack.monitoring.exporters._http]", HttpResource.State.DIRTY)); + awaitCheckAndPublish( + resources, + new ResourcePublishResult( + false, + "version [3.0.0] < [7.0.0] and NOT supported for [xpack.monitoring.exporters._http]", + HttpResource.State.DIRTY + ) + ); // ensure it didn't magically become clean assertTrue(resources.isDirty()); @@ -150,8 +156,7 @@ public void testTemplateCheckBlocksAfterSuccessfulVersion() { final List otherResponses = getTemplateResponses(1, successful, unsuccessful); // last check fails implies that N - 2 publishes succeeded! - whenPerformRequestAsyncWith(client, new RequestMatcher(is("GET"), startsWith("/_template/")), - first, otherResponses, exception); + whenPerformRequestAsyncWith(client, new RequestMatcher(is("GET"), startsWith("/_template/")), first, otherResponses, exception); // Since we return a "Not Ready" response on any templates that are not available (instead // of trying to publish them), we set the expected number of gets to be the first run of successful responses @@ -166,8 +171,14 @@ public void testTemplateCheckBlocksAfterSuccessfulVersion() { } else { // The first bad response will be either a 404 or a template with an old version String missingTemplateName = TEMPLATE_NAMES[expectedGets - 1]; - expectedResult = new ResourcePublishResult(false, "waiting for remote monitoring cluster to install " + - "appropriate template [" + missingTemplateName + "] (version mismatch or missing)", HttpResource.State.DIRTY); + expectedResult = new ResourcePublishResult( + false, + "waiting for remote monitoring cluster to install " + + "appropriate template [" + + missingTemplateName + + "] (version mismatch or missing)", + HttpResource.State.DIRTY + ); } } else { whenPerformRequestAsyncWith(client, new RequestMatcher(is("GET"), startsWith("/_template/")), exception); @@ -231,8 +242,13 @@ public void testWatchCheckBlocksAfterSuccessfulWatcherCheck() { final List otherResponses = getWatcherResponses(1, successful, unsuccessful); // last check fails implies that N - 2 publishes succeeded! - whenPerformRequestAsyncWith(client, new RequestMatcher(is("GET"), startsWith("/_watcher/watch/")), - first, otherResponses, exception); + whenPerformRequestAsyncWith( + client, + new RequestMatcher(is("GET"), startsWith("/_watcher/watch/")), + first, + otherResponses, + exception + ); whenSuccessfulPutWatches(otherResponses.size() + 1); // +1 for the "first" @@ -246,8 +262,13 @@ public void testWatchCheckBlocksAfterSuccessfulWatcherCheck() { // there is no form of an unsuccessful delete; only success or error final List responses = successfulDeleteResponses(successful); - whenPerformRequestAsyncWith(client, new RequestMatcher(is("DELETE"), startsWith("/_watcher/watch/")), - responses.get(0), responses.subList(1, responses.size()), exception); + whenPerformRequestAsyncWith( + client, + new RequestMatcher(is("DELETE"), startsWith("/_watcher/watch/")), + responses.get(0), + responses.subList(1, responses.size()), + exception + ); expectedGets += successful; } @@ -299,8 +320,13 @@ public void testWatchPublishBlocksAfterSuccessfulWatcherCheck() { whenGetWatches(successful, unsuccessful + 2); // previous publishes must have succeeded - whenPerformRequestAsyncWith(client, new RequestMatcher(is("PUT"), startsWith("/_watcher/watch/")), - firstSuccess, otherResponses, exception); + whenPerformRequestAsyncWith( + client, + new RequestMatcher(is("PUT"), startsWith("/_watcher/watch/")), + firstSuccess, + otherResponses, + exception + ); // GETs required for each PUT attempt (first is guaranteed "unsuccessful") expectedGets += successful + unsuccessful + 1; @@ -341,8 +367,11 @@ public void testDeployClusterAlerts() { // Instead it tries to DELETE the watches ignoring them not existing. whenGetWatches(existingWatches, EXPECTED_WATCHES - existingWatches); whenPerformRequestAsyncWith(client, new RequestMatcher(is("PUT"), startsWith("/_watcher/watch/")), exception); - whenPerformRequestAsyncWith(client, new RequestMatcher(is("DELETE"), startsWith("/_watcher/watch/")), - successfulDeleteResponses(EXPECTED_WATCHES)); + whenPerformRequestAsyncWith( + client, + new RequestMatcher(is("DELETE"), startsWith("/_watcher/watch/")), + successfulDeleteResponses(EXPECTED_WATCHES) + ); // Create resources that are configured to remove all watches Settings removalExporterSettings = Settings.builder() @@ -350,7 +379,8 @@ public void testDeployClusterAlerts() { .put("xpack.monitoring.migration.decommission_alerts", true) .build(); MultiHttpResource overrideResource = HttpExporter.createResources( - new Exporter.Config("_http", "http", removalExporterSettings, clusterService, licenseState)).allResources; + new Exporter.Config("_http", "http", removalExporterSettings, clusterService, licenseState) + ).allResources; assertTrue(overrideResource.isDirty()); awaitCheckAndPublish(overrideResource, true); @@ -366,10 +396,10 @@ public void testDeployClusterAlerts() { verifyNoMoreInteractions(client); assertWarnings( - "[xpack.monitoring.migration.decommission_alerts] setting was deprecated in Elasticsearch and will be " + - "removed in a future release! See the breaking changes documentation for the next major version.", - "[xpack.monitoring.exporters._http.cluster_alerts.management.enabled] setting was deprecated in Elasticsearch and " + - "will be removed in a future release! See the breaking changes documentation for the next major version." + "[xpack.monitoring.migration.decommission_alerts] setting was deprecated in Elasticsearch and will be " + + "removed in a future release! See the breaking changes documentation for the next major version.", + "[xpack.monitoring.exporters._http.cluster_alerts.management.enabled] setting was deprecated in Elasticsearch and " + + "will be removed in a future release! See the breaking changes documentation for the next major version." ); } @@ -418,10 +448,9 @@ public void testSuccessfulChecksIfNotElectedMasterNode() { final ClusterState state = mockClusterState(false); final ClusterService clusterService = mockClusterService(state); - final MultiHttpResource resources = - HttpExporter.createResources( - new Exporter.Config("_http", "http", exporterSettings, clusterService, licenseState)).allResources; - + final MultiHttpResource resources = HttpExporter.createResources( + new Exporter.Config("_http", "http", exporterSettings, clusterService, licenseState) + ).allResources; whenValidVersionResponse(); whenGetTemplates(EXPECTED_TEMPLATES); @@ -438,8 +467,10 @@ public void testSuccessfulChecksIfNotElectedMasterNode() { verifyGetTemplates(EXPECTED_TEMPLATES); verifyNoMoreInteractions(client); - assertWarnings("[xpack.monitoring.exporters._http.cluster_alerts.management.enabled] setting was deprecated in Elasticsearch " + - "and will be removed in a future release! See the breaking changes documentation for the next major version."); + assertWarnings( + "[xpack.monitoring.exporters._http.cluster_alerts.management.enabled] setting was deprecated in Elasticsearch " + + "and will be removed in a future release! See the breaking changes documentation for the next major version." + ); } private Exception failureGetException() { @@ -469,6 +500,7 @@ private Response successfulGetWatchResponse(final String watchId) { return response("GET", "/_watcher/watch/" + watchId, successfulCheckStatus(), goodEntity); } + private Response unsuccessfulGetWatchResponse(final String watchId) { if (randomBoolean()) { final HttpEntity badEntity = entityForClusterAlert(false, ClusterAlertsUtil.LAST_UPDATED_VERSION); @@ -509,8 +541,13 @@ private Response unsuccessfulGetResourceResponse(final String resourcePath, fina return unsuccessfulGetResponse(); } - private List getResourceResponses(final String resourcePath, final List resourceNames, - final int skip, final int successful, final int unsuccessful) { + private List getResourceResponses( + final String resourcePath, + final List resourceNames, + final int skip, + final int successful, + final int unsuccessful + ) { final List responses = new ArrayList<>(successful + unsuccessful); for (int i = 0; i < successful; ++i) { @@ -599,8 +636,10 @@ private void whenWatcherCanBeUsed(final boolean validLicense) { when(licenseState.isAllowed(Monitoring.MONITORING_CLUSTER_ALERTS_FEATURE)).thenReturn(validLicense); - final HttpEntity entity = - new StringEntity("{\"features\":{\"watcher\":{\"enabled\":true,\"available\":true}}}", ContentType.APPLICATION_JSON); + final HttpEntity entity = new StringEntity( + "{\"features\":{\"watcher\":{\"enabled\":true,\"available\":true}}}", + ContentType.APPLICATION_JSON + ); final Response successfulGet = response("GET", "_xpack", successfulCheckStatus(), entity); whenPerformRequestAsyncWith(client, new RequestMatcher(is("GET"), is("/_xpack")), successfulGet); @@ -648,13 +687,17 @@ private void verifyVersionCheck() { } private void verifyGetTemplates(final int called) { - verify(client, times(called)) - .performRequestAsync(argThat(new RequestMatcher(is("GET"), startsWith("/_template/"))::matches), any(ResponseListener.class)); + verify(client, times(called)).performRequestAsync( + argThat(new RequestMatcher(is("GET"), startsWith("/_template/"))::matches), + any(ResponseListener.class) + ); } private void verifyPutTemplates(final int called) { - verify(client, times(called)) - .performRequestAsync(argThat(new RequestMatcher(is("PUT"), startsWith("/_template/"))::matches), any(ResponseListener.class)); + verify(client, times(called)).performRequestAsync( + argThat(new RequestMatcher(is("PUT"), startsWith("/_template/"))::matches), + any(ResponseListener.class) + ); } private void verifyWatcherCheck() { @@ -662,21 +705,24 @@ private void verifyWatcherCheck() { } private void verifyDeleteWatches(final int called) { - verify(client, times(called)) - .performRequestAsync(argThat(new RequestMatcher(is("DELETE"), startsWith("/_watcher/watch/"))::matches), - any(ResponseListener.class)); + verify(client, times(called)).performRequestAsync( + argThat(new RequestMatcher(is("DELETE"), startsWith("/_watcher/watch/"))::matches), + any(ResponseListener.class) + ); } private void verifyGetWatches(final int called) { - verify(client, times(called)) - .performRequestAsync(argThat(new RequestMatcher(is("GET"), startsWith("/_watcher/watch/"))::matches), - any(ResponseListener.class)); + verify(client, times(called)).performRequestAsync( + argThat(new RequestMatcher(is("GET"), startsWith("/_watcher/watch/"))::matches), + any(ResponseListener.class) + ); } private void verifyPutWatches(final int called) { - verify(client, times(called)) - .performRequestAsync(argThat(new RequestMatcher(is("PUT"), startsWith("/_watcher/watch/"))::matches), - any(ResponseListener.class)); + verify(client, times(called)).performRequestAsync( + argThat(new RequestMatcher(is("PUT"), startsWith("/_watcher/watch/"))::matches), + any(ResponseListener.class) + ); } private ClusterService mockClusterService(final ClusterState state) { diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporterTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporterTests.java index 055fe9a41c18e..89f9e8805bfb1 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporterTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporterTests.java @@ -23,8 +23,8 @@ import org.elasticsearch.common.settings.MockSecureSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsException; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.ssl.SSLService; @@ -103,18 +103,23 @@ private void runTestEmptyHostList(final boolean useDefault) { List expectedWarnings = new ArrayList<>(); if (useDefault == false) { builder.putList(prefix + ".host", Collections.emptyList()); - expectedWarnings.add("[xpack.monitoring.exporters.example.host] setting was deprecated in Elasticsearch and will be removed " + - "in a future release! See the breaking changes documentation for the next major version."); + expectedWarnings.add( + "[xpack.monitoring.exporters.example.host] setting was deprecated in Elasticsearch and will be removed " + + "in a future release! See the breaking changes documentation for the next major version." + ); } final Settings settings = builder.build(); final IllegalArgumentException e = expectThrows( IllegalArgumentException.class, - () -> HttpExporter.HOST_SETTING.getConcreteSetting(prefix + ".host").get(settings)); + () -> HttpExporter.HOST_SETTING.getConcreteSetting(prefix + ".host").get(settings) + ); assertThat(e, hasToString(containsString("Failed to parse value [[]] for setting [" + prefix + ".host]"))); assertThat(e.getCause(), instanceOf(SettingsException.class)); assertThat(e.getCause(), hasToString(containsString("host list for [" + prefix + ".host] is empty"))); - expectedWarnings.add("[xpack.monitoring.exporters.example.type] setting was deprecated in Elasticsearch and will be removed " + - "in a future release! See the breaking changes documentation for the next major version."); + expectedWarnings.add( + "[xpack.monitoring.exporters.example.type] setting was deprecated in Elasticsearch and will be removed " + + "in a future release! See the breaking changes documentation for the next major version." + ); assertWarnings(expectedWarnings.toArray(new String[0])); } @@ -135,8 +140,10 @@ private void runTestEmptyHostListOkayIfTypeNotSet(final boolean useDefault) { builder.putList(prefix + ".host", Collections.emptyList()); final Settings settings = builder.build(); HttpExporter.HOST_SETTING.getConcreteSetting(prefix + ".host").get(settings); - assertWarnings("[xpack.monitoring.exporters.example.host] setting was deprecated in Elasticsearch and will be removed in a " + - "future release! See the breaking changes documentation for the next major version."); + assertWarnings( + "[xpack.monitoring.exporters.example.host] setting was deprecated in Elasticsearch and will be removed in a " + + "future release! See the breaking changes documentation for the next major version." + ); } public void testHostListIsRejectedIfTypeIsNotHttp() { @@ -148,10 +155,10 @@ public void testHostListIsRejectedIfTypeIsNotHttp() { final SettingsException e = expectThrows(SettingsException.class, () -> clusterSettings.validate(settings, true)); assertThat(e, hasToString(containsString("[" + prefix + ".host] is set but type is [local]"))); assertWarnings( - "[xpack.monitoring.exporters.example.type] setting was deprecated in Elasticsearch and will be removed in a future release! " + - "See the breaking changes documentation for the next major version.", - "[xpack.monitoring.exporters.example.host] setting was deprecated in Elasticsearch and will be removed in a future release! " + - "See the breaking changes documentation for the next major version." + "[xpack.monitoring.exporters.example.type] setting was deprecated in Elasticsearch and will be removed in a future release! " + + "See the breaking changes documentation for the next major version.", + "[xpack.monitoring.exporters.example.host] setting was deprecated in Elasticsearch and will be removed in a future release! " + + "See the breaking changes documentation for the next major version." ); } @@ -161,46 +168,44 @@ public void testSecurePasswordIsRejectedIfTypeIsNotHttp() { final String settingName = ".auth.secure_password"; final String settingValue = "securePassword"; - MockSecureSettings mockSecureSettings = new MockSecureSettings(); + MockSecureSettings mockSecureSettings = new MockSecureSettings(); mockSecureSettings.setString(prefix + settingName, settingValue); builder.setSecureSettings(mockSecureSettings); final Settings settings = builder.build(); - final ClusterSettings clusterSettings = - new ClusterSettings(settings, Set.of(HttpExporter.AUTH_SECURE_PASSWORD_SETTING, Exporter.TYPE_SETTING)); + final ClusterSettings clusterSettings = new ClusterSettings( + settings, + Set.of(HttpExporter.AUTH_SECURE_PASSWORD_SETTING, Exporter.TYPE_SETTING) + ); final SettingsException e = expectThrows(SettingsException.class, () -> clusterSettings.validate(settings, true)); assertThat(e, hasToString(containsString("[" + prefix + settingName + "] is set but type is [local]"))); assertWarnings( - "[xpack.monitoring.exporters.example.type] setting was deprecated in Elasticsearch and will be removed in a future release! " + - "See the breaking changes documentation for the next major version.", - "[xpack.monitoring.exporters.example.auth.secure_password] setting was deprecated in Elasticsearch and will be removed in a " + - "future release! See the breaking changes documentation for the next major version." + "[xpack.monitoring.exporters.example.type] setting was deprecated in Elasticsearch and will be removed in a future release! " + + "See the breaking changes documentation for the next major version.", + "[xpack.monitoring.exporters.example.auth.secure_password] setting was deprecated in Elasticsearch and will be removed in a " + + "future release! See the breaking changes documentation for the next major version." ); } public void testInvalidHost() { final String prefix = "xpack.monitoring.exporters.example"; final String host = "https://example.com:443/"; - final Settings settings = Settings.builder() - .put(prefix + ".type", "http") - .put(prefix + ".host", host) - .build(); + final Settings settings = Settings.builder().put(prefix + ".type", "http").put(prefix + ".host", host).build(); final IllegalArgumentException e = expectThrows( IllegalArgumentException.class, - () -> HttpExporter.HOST_SETTING.getConcreteSetting(prefix + ".host").get(settings)); - assertThat( - e, - hasToString(containsString("Failed to parse value [[\"" + host + "\"]] for setting [" + prefix + ".host]"))); + () -> HttpExporter.HOST_SETTING.getConcreteSetting(prefix + ".host").get(settings) + ); + assertThat(e, hasToString(containsString("Failed to parse value [[\"" + host + "\"]] for setting [" + prefix + ".host]"))); assertThat(e.getCause(), instanceOf(SettingsException.class)); assertThat(e.getCause(), hasToString(containsString("[" + prefix + ".host] invalid host: [" + host + "]"))); assertThat(e.getCause().getCause(), instanceOf(IllegalArgumentException.class)); assertThat(e.getCause().getCause(), hasToString(containsString("HttpHosts do not use paths [/]."))); assertWarnings( - "[xpack.monitoring.exporters.example.host] setting was deprecated in Elasticsearch and will be removed in a future release! " + - "See the breaking changes documentation for the next major version.", - "[xpack.monitoring.exporters.example.type] setting was deprecated in Elasticsearch and will be removed in a future release! " + - "See the breaking changes documentation for the next major version." + "[xpack.monitoring.exporters.example.host] setting was deprecated in Elasticsearch and will be removed in a future release! " + + "See the breaking changes documentation for the next major version.", + "[xpack.monitoring.exporters.example.type] setting was deprecated in Elasticsearch and will be removed in a future release! " + + "See the breaking changes documentation for the next major version." ); } @@ -214,18 +219,21 @@ public void testMixedSchemes() { .build(); final IllegalArgumentException e = expectThrows( IllegalArgumentException.class, - () -> HttpExporter.HOST_SETTING.getConcreteSetting(prefix + ".host").get(settings)); + () -> HttpExporter.HOST_SETTING.getConcreteSetting(prefix + ".host").get(settings) + ); assertThat( e, - hasToString(containsString( - "Failed to parse value [[\"" + httpHost + "\",\"" + httpsHost + "\"]] for setting [" + prefix + ".host]"))); + hasToString( + containsString("Failed to parse value [[\"" + httpHost + "\",\"" + httpsHost + "\"]] for setting [" + prefix + ".host]") + ) + ); assertThat(e.getCause(), instanceOf(SettingsException.class)); assertThat(e.getCause(), hasToString(containsString("[" + prefix + ".host] must use a consistent scheme: http or https"))); assertWarnings( - "[xpack.monitoring.exporters.example.host] setting was deprecated in Elasticsearch and will be removed in a future release! " + - "See the breaking changes documentation for the next major version.", - "[xpack.monitoring.exporters.example.type] setting was deprecated in Elasticsearch and will be removed in a future release! " + - "See the breaking changes documentation for the next major version." + "[xpack.monitoring.exporters.example.host] setting was deprecated in Elasticsearch and will be removed in a future release! " + + "See the breaking changes documentation for the next major version.", + "[xpack.monitoring.exporters.example.type] setting was deprecated in Elasticsearch and will be removed in a future release! " + + "See the breaking changes documentation for the next major version." ); } @@ -233,10 +241,10 @@ public void testExporterWithBlacklistedHeaders() { final String blacklistedHeader = randomFrom(HttpExporter.BLACKLISTED_HEADERS); final String expected = "header cannot be overwritten via [xpack.monitoring.exporters._http.headers." + blacklistedHeader + "]"; final Settings.Builder builder = Settings.builder() - .put("xpack.monitoring.exporters._http.type", HttpExporter.TYPE) - .put("xpack.monitoring.exporters._http.host", "http://localhost:9200") - .put("xpack.monitoring.exporters._http.headers.abc", "xyz") - .put("xpack.monitoring.exporters._http.headers." + blacklistedHeader, "value should not matter"); + .put("xpack.monitoring.exporters._http.type", HttpExporter.TYPE) + .put("xpack.monitoring.exporters._http.host", "http://localhost:9200") + .put("xpack.monitoring.exporters._http.headers.abc", "xyz") + .put("xpack.monitoring.exporters._http.headers." + blacklistedHeader, "value should not matter"); if (randomBoolean()) { builder.put("xpack.monitoring.exporters._http.headers.xyz", "abc"); @@ -245,16 +253,18 @@ public void testExporterWithBlacklistedHeaders() { final Config config = createConfig(builder.build()); final MonitoringMigrationCoordinator coordinator = new MonitoringMigrationCoordinator(); - final SettingsException exception = - expectThrows(SettingsException.class, () -> new HttpExporter(config, sslService, threadContext, coordinator)); + final SettingsException exception = expectThrows( + SettingsException.class, + () -> new HttpExporter(config, sslService, threadContext, coordinator) + ); assertThat(exception.getMessage(), equalTo(expected)); assertWarnings( - "[xpack.monitoring.exporters._http.host] setting was deprecated in Elasticsearch and will be removed in a " + - "future release! See the breaking changes documentation for the next major version.", - "[xpack.monitoring.exporters._http.type] setting was deprecated in Elasticsearch and will be removed in a future release! " + - "See the breaking changes documentation for the next major version." + "[xpack.monitoring.exporters._http.host] setting was deprecated in Elasticsearch and will be removed in a " + + "future release! See the breaking changes documentation for the next major version.", + "[xpack.monitoring.exporters._http.type] setting was deprecated in Elasticsearch and will be removed in a future release! " + + "See the breaking changes documentation for the next major version." ); } @@ -262,9 +272,9 @@ public void testExporterWithEmptyHeaders() { final String name = randomFrom("abc", "ABC", "X-Flag"); final String expected = "headers must have values, missing for setting [xpack.monitoring.exporters._http.headers." + name + "]"; final Settings.Builder builder = Settings.builder() - .put("xpack.monitoring.exporters._http.type", HttpExporter.TYPE) - .put("xpack.monitoring.exporters._http.host", "localhost:9200") - .put("xpack.monitoring.exporters._http.headers." + name, ""); + .put("xpack.monitoring.exporters._http.type", HttpExporter.TYPE) + .put("xpack.monitoring.exporters._http.host", "localhost:9200") + .put("xpack.monitoring.exporters._http.headers." + name, ""); if (randomBoolean()) { builder.put("xpack.monitoring.exporters._http.headers.xyz", "abc"); @@ -273,15 +283,17 @@ public void testExporterWithEmptyHeaders() { final Config config = createConfig(builder.build()); final MonitoringMigrationCoordinator coordinator = new MonitoringMigrationCoordinator(); - final SettingsException exception = - expectThrows(SettingsException.class, () -> new HttpExporter(config, sslService, threadContext, coordinator)); + final SettingsException exception = expectThrows( + SettingsException.class, + () -> new HttpExporter(config, sslService, threadContext, coordinator) + ); assertThat(exception.getMessage(), equalTo(expected)); assertWarnings( - "[xpack.monitoring.exporters._http.host] setting was deprecated in Elasticsearch and will be removed in a future release! " + - "See the breaking changes documentation for the next major version.", - "[xpack.monitoring.exporters._http.type] setting was deprecated in Elasticsearch and will be removed in a future release! " + - "See the breaking changes documentation for the next major version." + "[xpack.monitoring.exporters._http.host] setting was deprecated in Elasticsearch and will be removed in a future release! " + + "See the breaking changes documentation for the next major version.", + "[xpack.monitoring.exporters._http.type] setting was deprecated in Elasticsearch and will be removed in a future release! " + + "See the breaking changes documentation for the next major version." ); } @@ -299,25 +311,31 @@ public void testExporterWithUnknownBlacklistedClusterAlerts() { } final Settings.Builder builder = Settings.builder() - .put("xpack.monitoring.exporters._http.type", HttpExporter.TYPE) - .put("xpack.monitoring.exporters._http.host", "http://localhost:9200") - .put("xpack.monitoring.exporters._http.cluster_alerts.management.enabled", true) - .putList("xpack.monitoring.exporters._http.cluster_alerts.management.blacklist", blacklist); + .put("xpack.monitoring.exporters._http.type", HttpExporter.TYPE) + .put("xpack.monitoring.exporters._http.host", "http://localhost:9200") + .put("xpack.monitoring.exporters._http.cluster_alerts.management.enabled", true) + .putList("xpack.monitoring.exporters._http.cluster_alerts.management.blacklist", blacklist); final Config config = createConfig(builder.build()); final MonitoringMigrationCoordinator coordinator = new MonitoringMigrationCoordinator(); - final SettingsException exception = - expectThrows(SettingsException.class, () -> new HttpExporter(config, sslService, threadContext, coordinator)); + final SettingsException exception = expectThrows( + SettingsException.class, + () -> new HttpExporter(config, sslService, threadContext, coordinator) + ); - assertThat(exception.getMessage(), - equalTo("[xpack.monitoring.exporters._http.cluster_alerts.management.blacklist] contains unrecognized Cluster " + - "Alert IDs [does_not_exist]")); + assertThat( + exception.getMessage(), + equalTo( + "[xpack.monitoring.exporters._http.cluster_alerts.management.blacklist] contains unrecognized Cluster " + + "Alert IDs [does_not_exist]" + ) + ); assertWarnings( - "[xpack.monitoring.exporters._http.cluster_alerts.management.blacklist] setting was deprecated in Elasticsearch" + - " and will be removed in a future release! See the breaking changes documentation for the next major version.", - "[xpack.monitoring.exporters._http.cluster_alerts.management.enabled] setting was deprecated in Elasticsearch" + - " and will be removed in a future release! See the breaking changes documentation for the next major version." + "[xpack.monitoring.exporters._http.cluster_alerts.management.blacklist] setting was deprecated in Elasticsearch" + + " and will be removed in a future release! See the breaking changes documentation for the next major version.", + "[xpack.monitoring.exporters._http.cluster_alerts.management.enabled] setting was deprecated in Elasticsearch" + + " and will be removed in a future release! See the breaking changes documentation for the next major version." ); } @@ -326,18 +344,18 @@ public void testExporterWithHostOnly() throws Exception { when(sslService.sslIOSessionStrategy(any(Settings.class))).thenReturn(sslStrategy); final Settings.Builder builder = Settings.builder() - .put("xpack.monitoring.exporters._http.type", "http") - .put("xpack.monitoring.exporters._http.host", "http://localhost:9200"); + .put("xpack.monitoring.exporters._http.type", "http") + .put("xpack.monitoring.exporters._http.host", "http://localhost:9200"); final Config config = createConfig(builder.build()); final MonitoringMigrationCoordinator coordinator = new MonitoringMigrationCoordinator(); new HttpExporter(config, sslService, threadContext, coordinator).close(); assertWarnings( - "[xpack.monitoring.exporters._http.host] setting was deprecated in Elasticsearch and will be removed in a future release! " + - "See the breaking changes documentation for the next major version.", - "[xpack.monitoring.exporters._http.type] setting was deprecated in Elasticsearch and will be removed in a future release! " + - "See the breaking changes documentation for the next major version." + "[xpack.monitoring.exporters._http.host] setting was deprecated in Elasticsearch and will be removed in a future release! " + + "See the breaking changes documentation for the next major version.", + "[xpack.monitoring.exporters._http.type] setting was deprecated in Elasticsearch and will be removed in a future release! " + + "See the breaking changes documentation for the next major version." ); } @@ -354,16 +372,19 @@ public void testExporterWithInvalidProxyBasePath() throws Exception { final IllegalArgumentException e = expectThrows( IllegalArgumentException.class, - () -> HttpExporter.PROXY_BASE_PATH_SETTING.getConcreteSetting(prefix + settingName).get(settings)); + () -> HttpExporter.PROXY_BASE_PATH_SETTING.getConcreteSetting(prefix + settingName).get(settings) + ); assertThat( e, - hasToString( - containsString("Failed to parse value [" + settingValue + "] for setting [" + prefix + settingName + "]"))); + hasToString(containsString("Failed to parse value [" + settingValue + "] for setting [" + prefix + settingName + "]")) + ); assertThat(e.getCause(), instanceOf(SettingsException.class)); assertThat(e.getCause(), hasToString(containsString(expected))); - assertWarnings("[xpack.monitoring.exporters._http.proxy.base_path] setting was deprecated in Elasticsearch and will be removed " + - "in a future release! See the breaking changes documentation for the next major version."); + assertWarnings( + "[xpack.monitoring.exporters._http.proxy.base_path] setting was deprecated in Elasticsearch and will be removed " + + "in a future release! See the breaking changes documentation for the next major version." + ); } public void testCreateRestClient() throws IOException { @@ -373,22 +394,28 @@ public void testCreateRestClient() throws IOException { List expectedWarnings = new ArrayList<>(); final Settings.Builder builder = Settings.builder() - .put("xpack.monitoring.exporters._http.type", "http") - .put("xpack.monitoring.exporters._http.host", "http://localhost:9200"); - expectedWarnings.add("[xpack.monitoring.exporters._http.host] setting was deprecated in Elasticsearch and will be removed in " + - "a future release! See the breaking changes documentation for the next major version."); - expectedWarnings.add("[xpack.monitoring.exporters._http.type] setting was deprecated in Elasticsearch and will be removed in " + - "a future release! See the breaking changes documentation for the next major version."); + .put("xpack.monitoring.exporters._http.type", "http") + .put("xpack.monitoring.exporters._http.host", "http://localhost:9200"); + expectedWarnings.add( + "[xpack.monitoring.exporters._http.host] setting was deprecated in Elasticsearch and will be removed in " + + "a future release! See the breaking changes documentation for the next major version." + ); + expectedWarnings.add( + "[xpack.monitoring.exporters._http.type] setting was deprecated in Elasticsearch and will be removed in " + + "a future release! See the breaking changes documentation for the next major version." + ); // use basic auth final boolean useBasicAuth = randomBoolean(); if (useBasicAuth) { builder.put("xpack.monitoring.exporters._http.auth.username", "_user"); - MockSecureSettings mockSecureSettings = new MockSecureSettings(); + MockSecureSettings mockSecureSettings = new MockSecureSettings(); mockSecureSettings.setString("xpack.monitoring.exporters._http.auth.secure_password", "securePassword"); builder.setSecureSettings(mockSecureSettings); - expectedWarnings.add("[xpack.monitoring.exporters._http.auth.username] setting was deprecated in Elasticsearch and will be " + - "removed in a future release! See the breaking changes documentation for the next major version."); + expectedWarnings.add( + "[xpack.monitoring.exporters._http.auth.username] setting was deprecated in Elasticsearch and will be " + + "removed in a future release! See the breaking changes documentation for the next major version." + ); } // use headers @@ -417,8 +444,10 @@ public void testCreateCredentialsProviderWithoutSecurity() { assertNull(provider); - assertWarnings("[xpack.monitoring.exporters._http.type] setting was deprecated in Elasticsearch and will be removed in a " + - "future release! See the breaking changes documentation for the next major version."); + assertWarnings( + "[xpack.monitoring.exporters._http.type] setting was deprecated in Elasticsearch and will be removed in a " + + "future release! See the breaking changes documentation for the next major version." + ); } public void testCreateSnifferDisabledByDefault() { @@ -433,10 +462,10 @@ public void testCreateSnifferDisabledByDefault() { public void testCreateSniffer() throws IOException { final Settings.Builder builder = Settings.builder() - .put("xpack.monitoring.exporters._http.type", "http") - // it's a simple check: does it start with "https"? - .put("xpack.monitoring.exporters._http.host", randomFrom("neither", "http", "https")) - .put("xpack.monitoring.exporters._http.sniff.enabled", true); + .put("xpack.monitoring.exporters._http.type", "http") + // it's a simple check: does it start with "https"? + .put("xpack.monitoring.exporters._http.host", randomFrom("neither", "http", "https")) + .put("xpack.monitoring.exporters._http.sniff.enabled", true); final Config config = createConfig(builder.build()); final RestClient client = mock(RestClient.class); @@ -459,12 +488,12 @@ public void testCreateSniffer() throws IOException { verifyNoMoreInteractions(client, listener); assertWarnings( - "[xpack.monitoring.exporters._http.sniff.enabled] setting was deprecated in Elasticsearch and will be removed " + - "in a future release! See the breaking changes documentation for the next major version.", - "[xpack.monitoring.exporters._http.host] setting was deprecated in Elasticsearch and will be removed in a future release!" + - " See the breaking changes documentation for the next major version.", - "[xpack.monitoring.exporters._http.type] setting was deprecated in Elasticsearch and will be removed in a future release!" + - " See the breaking changes documentation for the next major version." + "[xpack.monitoring.exporters._http.sniff.enabled] setting was deprecated in Elasticsearch and will be removed " + + "in a future release! See the breaking changes documentation for the next major version.", + "[xpack.monitoring.exporters._http.host] setting was deprecated in Elasticsearch and will be removed in a future release!" + + " See the breaking changes documentation for the next major version.", + "[xpack.monitoring.exporters._http.type] setting was deprecated in Elasticsearch and will be removed in a future release!" + + " See the breaking changes documentation for the next major version." ); } @@ -472,22 +501,25 @@ public void testCreateResources() { final boolean clusterAlertManagement = randomBoolean(); final TimeValue templateTimeout = randomFrom(TimeValue.timeValueSeconds(30), null); - final Settings.Builder builder = Settings.builder() - .put("xpack.monitoring.exporters._http.type", "http"); + final Settings.Builder builder = Settings.builder().put("xpack.monitoring.exporters._http.type", "http"); List warningsExpected = new ArrayList<>(); if (clusterAlertManagement) { builder.put("xpack.monitoring.exporters._http.cluster_alerts.management.enabled", true); - warningsExpected.add("[xpack.monitoring.exporters._http.cluster_alerts.management.enabled] setting was deprecated in " + - "Elasticsearch and will be removed in a future release! See the breaking changes documentation for the next major " + - "version."); + warningsExpected.add( + "[xpack.monitoring.exporters._http.cluster_alerts.management.enabled] setting was deprecated in " + + "Elasticsearch and will be removed in a future release! See the breaking changes documentation for the next major " + + "version." + ); } if (templateTimeout != null) { builder.put("xpack.monitoring.exporters._http.index.template.master_timeout", templateTimeout.getStringRep()); - warningsExpected.add("[xpack.monitoring.exporters._http.index.template.master_timeout] setting was deprecated in " + - "Elasticsearch and will be removed in a future release! See the breaking changes documentation for the next major " + - "version."); + warningsExpected.add( + "[xpack.monitoring.exporters._http.index.template.master_timeout] setting was deprecated in " + + "Elasticsearch and will be removed in a future release! See the breaking changes documentation for the next major " + + "version." + ); } final Config config = createConfig(builder.build()); @@ -495,28 +527,30 @@ public void testCreateResources() { final MultiHttpResource multiResource = HttpExporter.createResources(config).allResources; final List resources = multiResource.getResources(); - final int version = (int)resources.stream().filter((resource) -> resource instanceof VersionHttpResource).count(); - final List templates = - resources.stream().filter((resource) -> resource instanceof TemplateHttpResource) - .map(TemplateHttpResource.class::cast) - .collect(Collectors.toList()); - final List watcherCheck = - resources.stream().filter((resource) -> resource instanceof WatcherExistsHttpResource) - .map(WatcherExistsHttpResource.class::cast) - .collect(Collectors.toList()); + final int version = (int) resources.stream().filter((resource) -> resource instanceof VersionHttpResource).count(); + final List templates = resources.stream() + .filter((resource) -> resource instanceof TemplateHttpResource) + .map(TemplateHttpResource.class::cast) + .collect(Collectors.toList()); + final List watcherCheck = resources.stream() + .filter((resource) -> resource instanceof WatcherExistsHttpResource) + .map(WatcherExistsHttpResource.class::cast) + .collect(Collectors.toList()); final List watches; if (watcherCheck.isEmpty()) { watches = Collections.emptyList(); } else { - watches = watcherCheck.get(0).getWatches().getResources() - .stream().filter((resource) -> resource instanceof ClusterAlertHttpResource) - .map(ClusterAlertHttpResource.class::cast) - .collect(Collectors.toList()); + watches = watcherCheck.get(0) + .getWatches() + .getResources() + .stream() + .filter((resource) -> resource instanceof ClusterAlertHttpResource) + .map(ClusterAlertHttpResource.class::cast) + .collect(Collectors.toList()); } // expected number of resources - assertThat(multiResource.getResources().size(), - equalTo(version + templates.size() + watcherCheck.size())); + assertThat(multiResource.getResources().size(), equalTo(version + templates.size() + watcherCheck.size())); assertThat(version, equalTo(1)); assertThat(templates, hasSize(MonitoringTemplateRegistry.TEMPLATE_NAMES.length)); assertThat(watcherCheck, hasSize(clusterAlertManagement ? 1 : 0)); @@ -526,8 +560,10 @@ public void testCreateResources() { assertMasterTimeoutSet(templates, templateTimeout); // logging owner names - final List uniqueOwners = - resources.stream().map(HttpResource::getResourceOwnerName).distinct().collect(Collectors.toList()); + final List uniqueOwners = resources.stream() + .map(HttpResource::getResourceOwnerName) + .distinct() + .collect(Collectors.toList()); assertThat(uniqueOwners, hasSize(1)); assertThat(uniqueOwners.get(0), equalTo("xpack.monitoring.exporters._http")); @@ -539,8 +575,7 @@ public void testCreateResources() { public void testCreateDefaultParams() { final TimeValue bulkTimeout = randomFrom(TimeValue.timeValueSeconds(30), null); - final Settings.Builder builder = Settings.builder() - .put("xpack.monitoring.exporters._http.type", "http"); + final Settings.Builder builder = Settings.builder().put("xpack.monitoring.exporters._http.type", "http"); if (bulkTimeout != null) { builder.put("xpack.monitoring.exporters._http.bulk.timeout", bulkTimeout.toString()); @@ -554,8 +589,10 @@ public void testCreateDefaultParams() { if (bulkTimeout != null) { assertThat(parameters.remove("timeout"), equalTo(bulkTimeout.toString())); - assertWarnings("[xpack.monitoring.exporters._http.bulk.timeout] setting was deprecated in Elasticsearch and will be removed " + - "in a future release! See the breaking changes documentation for the next major version."); + assertWarnings( + "[xpack.monitoring.exporters._http.bulk.timeout] setting was deprecated in Elasticsearch and will be removed " + + "in a future release! See the breaking changes documentation for the next major version." + ); } else { assertNull(parameters.remove("timeout")); } @@ -575,18 +612,25 @@ public void testHttpExporterMigrationInProgressBlock() throws Exception { final MonitoringMigrationCoordinator migrationCoordinator = new MonitoringMigrationCoordinator(); assertTrue(migrationCoordinator.tryBlockInstallationTasks()); - try (HttpExporter exporter = new HttpExporter(config, client, sniffer, threadContext, migrationCoordinator, listener, resource, - alertsResource)) { + try ( + HttpExporter exporter = new HttpExporter( + config, + client, + sniffer, + threadContext, + migrationCoordinator, + listener, + resource, + alertsResource + ) + ) { verify(listener).setResource(resource); final CountDownLatch awaitResponseAndClose = new CountDownLatch(1); - final ActionListener bulkListener = ActionListener.wrap( - bulk -> { - assertNull("should have been invoked with null value to denote migration in progress", bulk); - awaitResponseAndClose.countDown(); - }, - e -> fail("[onResponse] should have been invoked with null value to denote migration in progress") - ); + final ActionListener bulkListener = ActionListener.wrap(bulk -> { + assertNull("should have been invoked with null value to denote migration in progress", bulk); + awaitResponseAndClose.countDown(); + }, e -> fail("[onResponse] should have been invoked with null value to denote migration in progress")); exporter.openBulk(bulkListener); @@ -605,8 +649,18 @@ public void testHttpExporterDirtyResourcesBlock() throws Exception { final HttpResource alertsResource = new MockHttpResource(exporterName(), false, null, false); final MonitoringMigrationCoordinator migrationCoordinator = new MonitoringMigrationCoordinator(); - try (HttpExporter exporter = new HttpExporter(config, client, sniffer, threadContext, migrationCoordinator, listener, resource, - alertsResource)) { + try ( + HttpExporter exporter = new HttpExporter( + config, + client, + sniffer, + threadContext, + migrationCoordinator, + listener, + resource, + alertsResource + ) + ) { verify(listener).setResource(resource); final CountDownLatch awaitResponseAndClose = new CountDownLatch(1); @@ -632,19 +686,26 @@ public void testHttpExporterReturnsNullForOpenBulkIfNotReady() throws Exception final HttpResource alertsResource = new MockHttpResource(exporterName(), false, null, false); final MonitoringMigrationCoordinator migrationCoordinator = new MonitoringMigrationCoordinator(); - try (HttpExporter exporter = new HttpExporter(config, client, sniffer, threadContext, migrationCoordinator, listener, resource, - alertsResource)) { + try ( + HttpExporter exporter = new HttpExporter( + config, + client, + sniffer, + threadContext, + migrationCoordinator, + listener, + resource, + alertsResource + ) + ) { verify(listener).setResource(resource); final CountDownLatch awaitResponseAndClose = new CountDownLatch(1); - final ActionListener bulkListener = ActionListener.wrap( - bulk -> { - assertThat(bulk, nullValue()); + final ActionListener bulkListener = ActionListener.wrap(bulk -> { + assertThat(bulk, nullValue()); - awaitResponseAndClose.countDown(); - }, - e -> fail(e.getMessage()) - ); + awaitResponseAndClose.countDown(); + }, e -> fail(e.getMessage())); exporter.openBulk(bulkListener); @@ -663,19 +724,26 @@ public void testHttpExporter() throws Exception { final HttpResource alertsResource = new MockHttpResource(exporterName(), false, null, false); final MonitoringMigrationCoordinator migrationCoordinator = new MonitoringMigrationCoordinator(); - try (HttpExporter exporter = new HttpExporter(config, client, sniffer, threadContext, migrationCoordinator, listener, resource, - alertsResource)) { + try ( + HttpExporter exporter = new HttpExporter( + config, + client, + sniffer, + threadContext, + migrationCoordinator, + listener, + resource, + alertsResource + ) + ) { verify(listener).setResource(resource); final CountDownLatch awaitResponseAndClose = new CountDownLatch(1); - final ActionListener bulkListener = ActionListener.wrap( - bulk -> { - assertThat(bulk.getName(), equalTo(exporterName())); + final ActionListener bulkListener = ActionListener.wrap(bulk -> { + assertThat(bulk.getName(), equalTo(exporterName())); - awaitResponseAndClose.countDown(); - }, - e -> fail(e.getMessage()) - ); + awaitResponseAndClose.countDown(); + }, e -> fail(e.getMessage())); exporter.openBulk(bulkListener); diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpResourceTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpResourceTests.java index b42f783407c87..ce3b2546cee19 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpResourceTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpResourceTests.java @@ -6,13 +6,13 @@ */ package org.elasticsearch.xpack.monitoring.exporter.http; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; import org.elasticsearch.action.ActionListener; import org.elasticsearch.client.RestClient; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.monitoring.exporter.http.HttpResource.ResourcePublishResult; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; import java.util.function.Supplier; import static org.elasticsearch.xpack.monitoring.exporter.http.AsyncHttpResourceHelper.mockBooleanActionListener; @@ -85,8 +85,9 @@ public void testDirtiness() { public void testCheckAndPublish() { final ActionListener listener = mockPublishResultActionListener(); - final ResourcePublishResult expected = randomBoolean() ? ResourcePublishResult.ready() : ResourcePublishResult - .notReady("test unready"); + final ResourcePublishResult expected = randomBoolean() + ? ResourcePublishResult.ready() + : ResourcePublishResult.notReady("test unready"); // the default dirtiness should be irrelevant; it should always be run! final HttpResource resource = new HttpResource(owner) { @Override @@ -131,17 +132,14 @@ public void testCheckAndPublishIfDirtyFalseWhileChecking() throws InterruptedExc final boolean response = randomBoolean(); final ActionListener listener = mockBooleanActionListener(); // listener used while checking is blocked, and thus should be ignored - final ActionListener checkingListener = ActionListener.wrap( - success -> { - // busy checking, so this should be ignored - assertFalse(success); - secondCheck.countDown(); - }, - e -> { - fail(e.getMessage()); - secondCheck.countDown(); - } - ); + final ActionListener checkingListener = ActionListener.wrap(success -> { + // busy checking, so this should be ignored + assertFalse(success); + secondCheck.countDown(); + }, e -> { + fail(e.getMessage()); + secondCheck.countDown(); + }); // the default dirtiness should be irrelevant; it should always be run! final HttpResource resource = new HttpResource(owner) { diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/MockHttpResource.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/MockHttpResource.java index 6f3e4c9d2a295..36e68a24b36c4 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/MockHttpResource.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/MockHttpResource.java @@ -76,8 +76,13 @@ public MockHttpResource(final String resourceOwnerName, final boolean dirty, fin * @param masterTimeout Master timeout to use with any request. * @param parameters The base parameters to specify for the request. */ - public MockHttpResource(final String resourceOwnerName, @Nullable final TimeValue masterTimeout, final Map parameters, - final Boolean check, final Boolean publish) { + public MockHttpResource( + final String resourceOwnerName, + @Nullable final TimeValue masterTimeout, + final Map parameters, + final Boolean check, + final Boolean publish + ) { this(resourceOwnerName, masterTimeout, parameters, true, check, publish); } @@ -91,8 +96,14 @@ public MockHttpResource(final String resourceOwnerName, @Nullable final TimeValu * @param masterTimeout Master timeout to use with any request. * @param parameters The base parameters to specify for the request. */ - public MockHttpResource(final String resourceOwnerName, @Nullable final TimeValue masterTimeout, final Map parameters, - final boolean dirty, final Boolean check, final Boolean publish) { + public MockHttpResource( + final String resourceOwnerName, + @Nullable final TimeValue masterTimeout, + final Map parameters, + final boolean dirty, + final Boolean check, + final Boolean publish + ) { super(resourceOwnerName, masterTimeout, parameters, dirty); this.check = check; @@ -118,7 +129,6 @@ protected void doPublish(final RestClient client, final ActionListener body = () -> entity; - private final PublishableHttpResource resource = - new MockHttpResource(owner, masterTimeout, PublishableHttpResource.NO_BODY_PARAMETERS); + private final PublishableHttpResource resource = new MockHttpResource(owner, masterTimeout, PublishableHttpResource.NO_BODY_PARAMETERS); public void testCheckForResourceExists() throws IOException { assertCheckForResource(successfulCheckStatus(), true, "{} [{}] found on the [{}] {}"); @@ -89,12 +88,10 @@ public void testVersionCheckForResourceExists() { public void testVersionCheckForResourceDoesNotExist() { if (randomBoolean()) { // it literally does not exist - assertVersionCheckForResource(notFoundCheckStatus(), false, - randomInt(), "{} [{}] does not exist on the [{}] {}"); + assertVersionCheckForResource(notFoundCheckStatus(), false, randomInt(), "{} [{}] does not exist on the [{}] {}"); } else { // it DOES exist, but the version needs to be replaced - assertVersionCheckForResource(successfulCheckStatus(), false, - randomInt(), "{} [{}] found on the [{}] {}"); + assertVersionCheckForResource(successfulCheckStatus(), false, randomInt(), "{} [{}] found on the [{}] {}"); } } @@ -109,9 +106,18 @@ public void testVersionCheckForResourceUnexpectedResponse() { whenPerformRequestAsyncWith(client, request, response); - resource.versionCheckForResource(client, wrapMockListener(checkListener), logger, - resourceBasePath, resourceName, resourceType, owner, ownerType, - xContent, minimumVersion); + resource.versionCheckForResource( + client, + wrapMockListener(checkListener), + logger, + resourceBasePath, + resourceName, + resourceType, + owner, + ownerType, + xContent, + minimumVersion + ); verifyCheckListener(null); verify(logger).trace("checking if {} [{}] exists on the [{}] {}", resourceType, resourceName, owner, ownerType); @@ -133,9 +139,18 @@ public void testVersionCheckForResourceMalformedResponse() { whenPerformRequestAsyncWith(client, request, response); - resource.versionCheckForResource(client, wrapMockListener(checkListener), logger, - resourceBasePath, resourceName, resourceType, owner, ownerType, - xContent, minimumVersion); + resource.versionCheckForResource( + client, + wrapMockListener(checkListener), + logger, + resourceBasePath, + resourceName, + resourceType, + owner, + ownerType, + xContent, + minimumVersion + ); verifyCheckListener(null); verify(logger).trace("checking if {} [{}] exists on the [{}] {}", resourceType, resourceName, owner, ownerType); @@ -185,8 +200,18 @@ public void testPutResourceFalseWithException() { whenPerformRequestAsyncWith(client, request, e); final Map parameters = Collections.emptyMap(); - resource.putResource(client, wrapMockListener(publishListener), logger, resourceBasePath, resourceName, parameters, body, - resourceType, owner, ownerType); + resource.putResource( + client, + wrapMockListener(publishListener), + logger, + resourceBasePath, + resourceName, + parameters, + body, + resourceType, + owner, + ownerType + ); verifyPublishListener(null); @@ -218,8 +243,16 @@ public void testDeleteResourceErrors() { whenPerformRequestAsyncWith(client, request, e); - resource.deleteResource(client, wrapMockListener(checkListener), logger, resourceBasePath, resourceName, resourceType, owner, - ownerType); + resource.deleteResource( + client, + wrapMockListener(checkListener), + logger, + resourceBasePath, + resourceName, + resourceType, + owner, + ownerType + ); verifyCheckListener(null); @@ -235,8 +268,13 @@ public void testParameters() { } public void testDoCheckAndPublishIgnoresPublishWhenCheckErrors() { - final PublishableHttpResource resource = - new MockHttpResource(owner, masterTimeout, PublishableHttpResource.NO_BODY_PARAMETERS, null, true); + final PublishableHttpResource resource = new MockHttpResource( + owner, + masterTimeout, + PublishableHttpResource.NO_BODY_PARAMETERS, + null, + true + ); resource.doCheckAndPublish(client, wrapMockListener(publishListener)); @@ -248,8 +286,13 @@ public void testDoCheckAndPublish() { final boolean exists = randomBoolean(); final boolean publish = randomBoolean(); - final PublishableHttpResource resource = - new MockHttpResource(owner, masterTimeout, PublishableHttpResource.NO_BODY_PARAMETERS, exists, publish); + final PublishableHttpResource resource = new MockHttpResource( + owner, + masterTimeout, + PublishableHttpResource.NO_BODY_PARAMETERS, + exists, + publish + ); resource.doCheckAndPublish(client, wrapMockListener(publishListener)); @@ -295,8 +338,10 @@ public void testShouldReplaceResourceChecksVersion() throws IOException { final Response response = mock(Response.class); // { "resourceName": { "version": randomLong } } - final HttpEntity entity = - new StringEntity("{\"" + resourceName + "\":{\"version\":" + version + "}}", ContentType.APPLICATION_JSON); + final HttpEntity entity = new StringEntity( + "{\"" + resourceName + "\":{\"version\":" + version + "}}", + ContentType.APPLICATION_JSON + ); final XContent xContent = XContentType.JSON.xContent(); when(response.getEntity()).thenReturn(entity); @@ -305,8 +350,7 @@ public void testShouldReplaceResourceChecksVersion() throws IOException { } @SuppressLoggerChecks(reason = "mock logger used") - private void assertCheckForResource(final RestStatus status, final Boolean expected, final String debugLogMessage) - throws IOException { + private void assertCheckForResource(final RestStatus status, final Boolean expected, final String debugLogMessage) throws IOException { final String endpoint = concatenateEndpoint(resourceBasePath, resourceName); final Response response = response("GET", endpoint, status); final Request request = new Request("GET", endpoint); @@ -334,9 +378,12 @@ private void assertCheckForResource(final RestStatus status, final Boolean expec } @SuppressLoggerChecks(reason = "mock logger used") - private void assertVersionCheckForResource(final RestStatus status, final Boolean expected, - final int minimumVersion, - final String debugLogMessage) { + private void assertVersionCheckForResource( + final RestStatus status, + final Boolean expected, + final int minimumVersion, + final String debugLogMessage + ) { final String endpoint = concatenateEndpoint(resourceBasePath, resourceName); final boolean shouldReplace = status == RestStatus.OK && expected == Boolean.FALSE; final HttpEntity entity = status == RestStatus.OK ? entityForResource(expected, resourceName, minimumVersion) : null; @@ -347,9 +394,18 @@ private void assertVersionCheckForResource(final RestStatus status, final Boolea whenPerformRequestAsyncWith(client, request, response); - resource.versionCheckForResource(client, wrapMockListener(checkListener), logger, - resourceBasePath, resourceName, resourceType, owner, ownerType, - xContent, minimumVersion); + resource.versionCheckForResource( + client, + wrapMockListener(checkListener), + logger, + resourceBasePath, + resourceName, + resourceType, + owner, + ownerType, + xContent, + minimumVersion + ); verify(logger).trace("checking if {} [{}] exists on the [{}] {}", resourceType, resourceName, owner, ownerType); verify(client).performRequestAsync(eq(request), any(ResponseListener.class)); @@ -382,8 +438,18 @@ private void assertPutResource(final RestStatus status, final boolean errorFree) whenPerformRequestAsyncWith(client, request, response); final Map parameters = Collections.emptyMap(); - resource.putResource(client, wrapMockListener(publishListener), logger, resourceBasePath, resourceName, parameters, body, - resourceType, owner, ownerType); + resource.putResource( + client, + wrapMockListener(publishListener), + logger, + resourceBasePath, + resourceName, + parameters, + body, + resourceType, + owner, + ownerType + ); verifyPublishListener(errorFree ? ResourcePublishResult.ready() : null); verify(client).performRequestAsync(eq(request), any(ResponseListener.class)); @@ -398,18 +464,25 @@ private void assertPutResource(final RestStatus status, final boolean errorFree) verify(logger).error(any(org.apache.logging.log4j.util.Supplier.class), e.capture()); - assertThat(e.getValue().getMessage(), - is("[" + resourceBasePath + "/" + resourceName + "] responded with [" + status.getStatus() + "]")); + assertThat( + e.getValue().getMessage(), + is("[" + resourceBasePath + "/" + resourceName + "] responded with [" + status.getStatus() + "]") + ); } verifyNoMoreInteractions(client, response, logger, entity); } @SuppressWarnings("unchecked") - private void assertCheckForResource(final RestClient client, final Logger logger, - final String resourceBasePath, final String resourceName, final String resourceType, - final Boolean expected, final Response response) - throws IOException { + private void assertCheckForResource( + final RestClient client, + final Logger logger, + final String resourceBasePath, + final String resourceName, + final String resourceType, + final Boolean expected, + final Response response + ) throws IOException { final CheckedFunction responseChecker = mock(CheckedFunction.class); final CheckedFunction dneResponseChecker = mock(CheckedFunction.class); @@ -419,9 +492,20 @@ private void assertCheckForResource(final RestClient client, final Logger logger when(dneResponseChecker.apply(response)).thenReturn(false == expected); } - resource.checkForResource(client, wrapMockListener(checkListener), logger, resourceBasePath, resourceName, resourceType, owner, - ownerType, PublishableHttpResource.GET_EXISTS, PublishableHttpResource.GET_DOES_NOT_EXIST, - responseChecker, dneResponseChecker); + resource.checkForResource( + client, + wrapMockListener(checkListener), + logger, + resourceBasePath, + resourceName, + resourceType, + owner, + ownerType, + PublishableHttpResource.GET_EXISTS, + PublishableHttpResource.GET_DOES_NOT_EXIST, + responseChecker, + dneResponseChecker + ); if (expected == Boolean.TRUE) { verify(responseChecker).apply(response); @@ -445,8 +529,16 @@ private void assertDeleteResource(final RestStatus status, final boolean expecte whenPerformRequestAsyncWith(client, request, response); - resource.deleteResource(client, wrapMockListener(checkListener), logger, resourceBasePath, resourceName, resourceType, owner, - ownerType); + resource.deleteResource( + client, + wrapMockListener(checkListener), + logger, + resourceBasePath, + resourceName, + resourceType, + owner, + ownerType + ); verify(client).performRequestAsync(eq(request), any(ResponseListener.class)); verify(response).getStatusLine(); @@ -461,8 +553,10 @@ private void assertDeleteResource(final RestStatus status, final boolean expecte verify(logger).error(any(org.apache.logging.log4j.util.Supplier.class), e.capture()); - assertThat(e.getValue().getMessage(), - is("[" + resourceBasePath + "/" + resourceName + "] responded with [" + status.getStatus() + "]")); + assertThat( + e.getValue().getMessage(), + is("[" + resourceBasePath + "/" + resourceName + "] responded with [" + status.getStatus() + "]") + ); verifyCheckListener(null); } diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/SecurityHttpClientConfigCallbackTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/SecurityHttpClientConfigCallbackTests.java index 133c2517ac06c..9555799b29928 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/SecurityHttpClientConfigCallbackTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/SecurityHttpClientConfigCallbackTests.java @@ -43,8 +43,7 @@ public void testCustomizeHttpClient() { public void testCustomizeHttpClientWithOptionalParameters() { final CredentialsProvider optionalCredentialsProvider = randomFrom(credentialsProvider, null); - final SecurityHttpClientConfigCallback callback = - new SecurityHttpClientConfigCallback(sslStrategy, optionalCredentialsProvider); + final SecurityHttpClientConfigCallback callback = new SecurityHttpClientConfigCallback(sslStrategy, optionalCredentialsProvider); assertSame(builder, callback.customizeHttpClient(builder)); assertSame(optionalCredentialsProvider, callback.getCredentialsProvider()); diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/TemplateHttpResourceTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/TemplateHttpResourceTests.java index 92f9626325641..a3a42097fdd73 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/TemplateHttpResourceTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/TemplateHttpResourceTests.java @@ -27,11 +27,11 @@ public class TemplateHttpResourceTests extends AbstractPublishableHttpResourceTe private final String templateName = ".my_template"; - //the internal representation has the type, the external representation should not - private final String templateValueInternal = "{\"order\":0,\"index_patterns\":[\".xyz-*\"],\"settings\":{},\"mappings\":{\"_doc\"" + - ":{\"properties\":{\"one\":{\"properties\":{\"two\":{\"properties\":{\"name\":{\"type\":\"keyword\"}}}}}}}},\"aliases\":{}}"; - private final String templateValueExternal = "{\"order\":0,\"index_patterns\":[\".xyz-*\"],\"settings\":{},\"mappings\"" + - ":{\"properties\":{\"one\":{\"properties\":{\"two\":{\"properties\":{\"name\":{\"type\":\"keyword\"}}}}}}},\"aliases\":{}}"; + // the internal representation has the type, the external representation should not + private final String templateValueInternal = "{\"order\":0,\"index_patterns\":[\".xyz-*\"],\"settings\":{},\"mappings\":{\"_doc\"" + + ":{\"properties\":{\"one\":{\"properties\":{\"two\":{\"properties\":{\"name\":{\"type\":\"keyword\"}}}}}}}},\"aliases\":{}}"; + private final String templateValueExternal = "{\"order\":0,\"index_patterns\":[\".xyz-*\"],\"settings\":{},\"mappings\"" + + ":{\"properties\":{\"one\":{\"properties\":{\"two\":{\"properties\":{\"name\":{\"type\":\"keyword\"}}}}}}},\"aliases\":{}}"; private final Supplier template = () -> templateValueInternal; private final int minimumVersion = Math.min(MonitoringTemplateUtils.LAST_UPDATED_VERSION, Version.CURRENT.id); @@ -70,15 +70,16 @@ public void testDoCheckError() { public void testDoPublishFalseWithNonPublishedResource() { RestClient mockClient = mock(RestClient.class); SetOnce result = new SetOnce<>(); - resource.doPublish(mockClient, ActionListener.wrap(result::set, - e -> {throw new RuntimeException("Unexpected exception", e);})); + resource.doPublish(mockClient, ActionListener.wrap(result::set, e -> { throw new RuntimeException("Unexpected exception", e); })); verifyZeroInteractions(mockClient); // Should not have used the client at all. HttpResource.ResourcePublishResult resourcePublishResult = result.get(); assertThat(resourcePublishResult, notNullValue()); assertThat(resourcePublishResult.getResourceState(), notNullValue()); assertThat(resourcePublishResult.getResourceState(), is(HttpResource.State.DIRTY)); - assertThat(resourcePublishResult.getReason(), - is("waiting for remote monitoring cluster to install appropriate template [.my_template] (version mismatch or missing)")); + assertThat( + resourcePublishResult.getReason(), + is("waiting for remote monitoring cluster to install appropriate template [.my_template] (version mismatch or missing)") + ); } public void testParameters() { diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/TimeoutRequestConfigCallbackTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/TimeoutRequestConfigCallbackTests.java index 4f8a93c52cb5e..8640a648ff1a5 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/TimeoutRequestConfigCallbackTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/TimeoutRequestConfigCallbackTests.java @@ -9,7 +9,6 @@ import org.apache.http.client.config.RequestConfig; import org.elasticsearch.core.TimeValue; import org.elasticsearch.test.ESTestCase; - import org.junit.Before; import static org.mockito.ArgumentMatchers.anyInt; @@ -31,8 +30,8 @@ public class TimeoutRequestConfigCallbackTests extends ESTestCase { @Before public void configureTimeouts() { - when(connectTimeout.millis()).thenReturn((long)connectTimeoutMillis); - when(socketTimeout.millis()).thenReturn((long)socketTimeoutMillis); + when(connectTimeout.millis()).thenReturn((long) connectTimeoutMillis); + when(socketTimeout.millis()).thenReturn((long) socketTimeoutMillis); } public void testCustomizeRequestConfig() { diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/WatcherExistsHttpResourceTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/WatcherExistsHttpResourceTests.java index d86c584765063..b3b948dc8cdd6 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/WatcherExistsHttpResourceTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/WatcherExistsHttpResourceTests.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.xpack.monitoring.exporter.http; -import java.util.Collections; import org.apache.http.HttpEntity; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; @@ -17,6 +16,7 @@ import org.elasticsearch.rest.RestStatus; import org.elasticsearch.xpack.monitoring.exporter.http.HttpResource.ResourcePublishResult; +import java.util.Collections; import java.util.Map; import static org.elasticsearch.xpack.monitoring.exporter.http.AsyncHttpResourceHelper.wrapMockListener; @@ -53,16 +53,14 @@ public void testDoCheckExistsFor404() { whenElectedMaster(); // /_xpack returning a 404 means ES didn't handle the request properly and X-Pack doesn't exist - doCheckWithStatusCode(resource, "", "_xpack", notFoundCheckStatus(), - GET_EXISTS, XPACK_DOES_NOT_EXIST, true); + doCheckWithStatusCode(resource, "", "_xpack", notFoundCheckStatus(), GET_EXISTS, XPACK_DOES_NOT_EXIST, true); } public void testDoCheckExistsFor400() { whenElectedMaster(); // /_xpack returning a 400 means X-Pack does not exist - doCheckWithStatusCode(resource, "", "_xpack", RestStatus.BAD_REQUEST, - GET_EXISTS, XPACK_DOES_NOT_EXIST, true); + doCheckWithStatusCode(resource, "", "_xpack", RestStatus.BAD_REQUEST, GET_EXISTS, XPACK_DOES_NOT_EXIST, true); } public void testDoCheckExistsAsElectedMaster() { @@ -73,8 +71,7 @@ public void testDoCheckExistsAsElectedMaster() { "{\"features\":{\"watcher\":{\"available\":true,\"enabled\":false}}}", "{\"features\":{\"watcher\":{\"available\":false,\"enabled\":true}}}", "{\"features\":{\"watcher\":{\"available\":true}}}", - "{\"features\":{\"watcher\":{\"enabled\":true}}}" - }; + "{\"features\":{\"watcher\":{\"enabled\":true}}}" }; final String endpoint = "/_xpack"; // success only implies that it responded; it also needs to be available and enabled @@ -94,8 +91,7 @@ public void testDoCheckDoesNotExist() { final String[] hasWatcher = { "{\"features\":{\"watcher\":{\"available\":true,\"enabled\":true}}}", - "{\"features\":{\"watcher\":{\"enabled\":true,\"available\":true}}}" - }; + "{\"features\":{\"watcher\":{\"enabled\":true,\"available\":true}}}" }; final String endpoint = "/_xpack"; // success only implies that it responded; it also needs to be available and enabled diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalExporterIntegTestCase.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalExporterIntegTestCase.java index 56da3edd7490b..703393f2b47ce 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalExporterIntegTestCase.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalExporterIntegTestCase.java @@ -42,21 +42,18 @@ public static void cleanUpStatic() { protected Settings localExporterSettings() { return Settings.builder() - .put("xpack.monitoring.collection.enabled", false) - .put("xpack.monitoring.collection.interval", "1s") - .put("xpack.monitoring.exporters." + exporterName + ".type", LocalExporter.TYPE) - .put("xpack.monitoring.exporters." + exporterName + ".enabled", false) - .put("xpack.monitoring.exporters." + exporterName + ".cluster_alerts.management.enabled", false) - .put(XPackSettings.MACHINE_LEARNING_ENABLED.getKey(), false) - .build(); + .put("xpack.monitoring.collection.enabled", false) + .put("xpack.monitoring.collection.interval", "1s") + .put("xpack.monitoring.exporters." + exporterName + ".type", LocalExporter.TYPE) + .put("xpack.monitoring.exporters." + exporterName + ".enabled", false) + .put("xpack.monitoring.exporters." + exporterName + ".cluster_alerts.management.enabled", false) + .put(XPackSettings.MACHINE_LEARNING_ENABLED.getKey(), false) + .build(); } @Override protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { - return Settings.builder() - .put(super.nodeSettings(nodeOrdinal, otherSettings)) - .put(localExporterSettings()) - .build(); + return Settings.builder().put(super.nodeSettings(nodeOrdinal, otherSettings)).put(localExporterSettings()).build(); } /** @@ -83,12 +80,19 @@ protected LocalExporter createLocalExporter(String exporterName, Settings export return createLocalExporter(exporterName, exporterSettings, new MonitoringMigrationCoordinator()); } - protected LocalExporter createLocalExporter(String exporterName, Settings exporterSettings, - MonitoringMigrationCoordinator coordinator) { + protected LocalExporter createLocalExporter( + String exporterName, + Settings exporterSettings, + MonitoringMigrationCoordinator coordinator + ) { final XPackLicenseState licenseState = TestUtils.newTestLicenseState(); final Exporter.Config config = new Exporter.Config(exporterName, "local", exporterSettings, clusterService(), licenseState); - final CleanerService cleanerService = - new CleanerService(exporterSettings, clusterService().getClusterSettings(), THREADPOOL, licenseState); + final CleanerService cleanerService = new CleanerService( + exporterSettings, + clusterService().getClusterSettings(), + THREADPOOL, + licenseState + ); return new LocalExporter(config, client(), coordinator, cleanerService); } diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalExporterIntegTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalExporterIntegTests.java index c2c52e79a721e..4253a55cb2a98 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalExporterIntegTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalExporterIntegTests.java @@ -15,14 +15,14 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.time.DateFormatter; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.aggregations.metrics.Max; import org.elasticsearch.test.ESIntegTestCase; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; import org.elasticsearch.xpack.core.monitoring.action.MonitoringBulkDoc; import org.elasticsearch.xpack.core.monitoring.action.MonitoringBulkRequestBuilder; @@ -52,19 +52,25 @@ import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.lessThanOrEqualTo; -@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.SUITE, - numDataNodes = 1, numClientNodes = 0, supportsDedicatedMasters = false) +@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.SUITE, numDataNodes = 1, numClientNodes = 0, supportsDedicatedMasters = false) public class LocalExporterIntegTests extends LocalExporterIntegTestCase { private final String indexTimeFormat = randomFrom("yy", "yyyy", "yyyy.MM", "yyyy-MM", "MM.yyyy", "MM", null); private void stopMonitoring() { // Now disabling the monitoring service, so that no more collection are started - assertAcked(client().admin().cluster().prepareUpdateSettings().setPersistentSettings( - Settings.builder().putNull(MonitoringService.ENABLED.getKey()) - .putNull("xpack.monitoring.exporters._local.type") - .putNull("xpack.monitoring.exporters._local.enabled") - .putNull("xpack.monitoring.exporters._local.cluster_alerts.management.enabled") - .putNull("xpack.monitoring.exporters._local.index.name.time_format"))); + assertAcked( + client().admin() + .cluster() + .prepareUpdateSettings() + .setPersistentSettings( + Settings.builder() + .putNull(MonitoringService.ENABLED.getKey()) + .putNull("xpack.monitoring.exporters._local.type") + .putNull("xpack.monitoring.exporters._local.enabled") + .putNull("xpack.monitoring.exporters._local.cluster_alerts.management.enabled") + .putNull("xpack.monitoring.exporters._local.index.name.time_format") + ) + ); } public void testExport() throws Exception { @@ -73,18 +79,19 @@ public void testExport() throws Exception { // indexing some random documents IndexRequestBuilder[] indexRequestBuilders = new IndexRequestBuilder[5]; for (int i = 0; i < indexRequestBuilders.length; i++) { - indexRequestBuilders[i] = client().prepareIndex("test").setId(Integer.toString(i)) - .setSource("title", "This is a random document"); + indexRequestBuilders[i] = client().prepareIndex("test") + .setId(Integer.toString(i)) + .setSource("title", "This is a random document"); } indexRandom(true, indexRequestBuilders); } // start the monitoring service so that /_monitoring/bulk is not ignored final Settings.Builder exporterSettings = Settings.builder() - .put(MonitoringService.ENABLED.getKey(), true) - .put("xpack.monitoring.exporters._local.type", LocalExporter.TYPE) - .put("xpack.monitoring.exporters._local.enabled", true) - .put("xpack.monitoring.exporters._local.cluster_alerts.management.enabled", false); + .put(MonitoringService.ENABLED.getKey(), true) + .put("xpack.monitoring.exporters._local.type", LocalExporter.TYPE) + .put("xpack.monitoring.exporters._local.enabled", true) + .put("xpack.monitoring.exporters._local.cluster_alerts.management.enabled", false); if (indexTimeFormat != null) { exporterSettings.put("xpack.monitoring.exporters._local.index.name.time_format", indexTimeFormat); @@ -111,7 +118,7 @@ public void testExport() throws Exception { ensureYellowAndNoInitializingShards(".monitoring-*"); SearchResponse response = client().prepareSearch(".monitoring-*").get(); - assertThat((long)nbDocs, lessThanOrEqualTo(response.getHits().getTotalHits().value)); + assertThat((long) nbDocs, lessThanOrEqualTo(response.getHits().getTotalHits().value)); }); checkMonitoringTemplates(); @@ -123,40 +130,68 @@ public void testExport() throws Exception { assertThat(indexExists(".monitoring-*"), is(true)); ensureYellowAndNoInitializingShards(".monitoring-*"); - assertThat(client().prepareSearch(".monitoring-es-*") + assertThat( + client().prepareSearch(".monitoring-es-*") .setSize(0) .setQuery(QueryBuilders.termQuery("type", "cluster_stats")) - .get().getHits().getTotalHits().value, greaterThan(0L)); - - assertThat(client().prepareSearch(".monitoring-es-*") + .get() + .getHits() + .getTotalHits().value, + greaterThan(0L) + ); + + assertThat( + client().prepareSearch(".monitoring-es-*") .setSize(0) .setQuery(QueryBuilders.termQuery("type", "index_recovery")) - .get().getHits().getTotalHits().value, greaterThan(0L)); - - assertThat(client().prepareSearch(".monitoring-es-*") + .get() + .getHits() + .getTotalHits().value, + greaterThan(0L) + ); + + assertThat( + client().prepareSearch(".monitoring-es-*") .setSize(0) .setQuery(QueryBuilders.termQuery("type", "index_stats")) - .get().getHits().getTotalHits().value, greaterThan(0L)); - - assertThat(client().prepareSearch(".monitoring-es-*") + .get() + .getHits() + .getTotalHits().value, + greaterThan(0L) + ); + + assertThat( + client().prepareSearch(".monitoring-es-*") .setSize(0) .setQuery(QueryBuilders.termQuery("type", "indices_stats")) - .get().getHits().getTotalHits().value, greaterThan(0L)); - - assertThat(client().prepareSearch(".monitoring-es-*") + .get() + .getHits() + .getTotalHits().value, + greaterThan(0L) + ); + + assertThat( + client().prepareSearch(".monitoring-es-*") .setSize(0) .setQuery(QueryBuilders.termQuery("type", "shards")) - .get().getHits().getTotalHits().value, greaterThan(0L)); + .get() + .getHits() + .getTotalHits().value, + greaterThan(0L) + ); SearchResponse response = client().prepareSearch(".monitoring-es-*") - .setSize(0) - .setQuery(QueryBuilders.termQuery("type", "node_stats")) - .addAggregation(terms("agg_nodes_ids").field("node_stats.node_id")) - .get(); + .setSize(0) + .setQuery(QueryBuilders.termQuery("type", "node_stats")) + .addAggregation(terms("agg_nodes_ids").field("node_stats.node_id")) + .get(); Terms aggregation = response.getAggregations().get("agg_nodes_ids"); - assertEquals("Aggregation on node_id must return a bucket per node involved in test", - numNodes, aggregation.getBuckets().size()); + assertEquals( + "Aggregation on node_id must return a bucket per node involved in test", + numNodes, + aggregation.getBuckets().size() + ); for (String nodeName : internalCluster().getNodeNames()) { String nodeId = internalCluster().clusterService(nodeName).localNode().getId(); @@ -184,11 +219,12 @@ public void testExport() throws Exception { refresh(".monitoring-es-*"); SearchResponse response = client().prepareSearch(".monitoring-es-*") - .setSize(0) - .setQuery(QueryBuilders.termQuery("type", "node_stats")) - .addAggregation(terms("agg_nodes_ids").field("node_stats.node_id") - .subAggregation(max("agg_last_time_collected").field("timestamp"))) - .get(); + .setSize(0) + .setQuery(QueryBuilders.termQuery("type", "node_stats")) + .addAggregation( + terms("agg_nodes_ids").field("node_stats.node_id").subAggregation(max("agg_last_time_collected").field("timestamp")) + ) + .get(); Terms aggregation = response.getAggregations().get("agg_nodes_ids"); for (String nodeName : internalCluster().getNodeNames()) { @@ -229,8 +265,10 @@ private void checkMonitoringTemplates() { */ private void checkMonitoringDocs() { ClusterStateResponse response = client().admin().cluster().prepareState().get(); - String customTimeFormat = response.getState().getMetadata().persistentSettings() - .get("xpack.monitoring.exporters._local.index.name.time_format"); + String customTimeFormat = response.getState() + .getMetadata() + .persistentSettings() + .get("xpack.monitoring.exporters._local.index.name.time_format"); assertEquals(indexTimeFormat, customTimeFormat); if (customTimeFormat == null) { customTimeFormat = "yyyy.MM.dd"; diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalExporterResourceIntegTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalExporterResourceIntegTests.java index 8a365fae63a1c..f736b31461a6c 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalExporterResourceIntegTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalExporterResourceIntegTests.java @@ -13,14 +13,14 @@ import org.elasticsearch.cluster.metadata.IndexTemplateMetadata; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.ObjectPath; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.test.ESIntegTestCase; +import org.elasticsearch.xcontent.ObjectPath; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringTemplateUtils; import org.elasticsearch.xpack.core.watcher.transport.actions.put.PutWatchAction; @@ -35,16 +35,15 @@ import java.util.List; import java.util.Set; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.test.ESIntegTestCase.Scope.TEST; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.notNullValue; -@ESIntegTestCase.ClusterScope(scope = TEST, - numDataNodes = 1, numClientNodes = 0, supportsDedicatedMasters = false) +@ESIntegTestCase.ClusterScope(scope = TEST, numDataNodes = 1, numClientNodes = 0, supportsDedicatedMasters = false) public class LocalExporterResourceIntegTests extends LocalExporterIntegTestCase { public LocalExporterResourceIntegTests() { @@ -59,8 +58,12 @@ protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { .build(); } - private final MonitoredSystem system = randomFrom(MonitoredSystem.ES, MonitoredSystem.BEATS, MonitoredSystem.KIBANA, - MonitoredSystem.LOGSTASH); + private final MonitoredSystem system = randomFrom( + MonitoredSystem.ES, + MonitoredSystem.BEATS, + MonitoredSystem.KIBANA, + MonitoredSystem.LOGSTASH + ); public void testCreateWhenResourcesNeedToBeAddedOrUpdated() throws Exception { assumeFalse("https://github.com/elastic/elasticsearch/issues/68608", Constants.MAC_OS_X); @@ -87,7 +90,8 @@ public void testRemoveWhenResourcesShouldBeRemoved() throws Exception { assertResourcesExist(); waitNoPendingTasksOnAll(); - Settings exporterSettings = Settings.builder().put(localExporterSettings()) + Settings exporterSettings = Settings.builder() + .put(localExporterSettings()) .put("xpack.monitoring.migration.decommission_alerts", true) .put("xpack.monitoring.exporters.decommission_local.cluster_alerts.management.enabled", true) .build(); @@ -106,8 +110,10 @@ public void testResourcesBlockedDuringMigration() throws Exception { assertResourcesExist(); waitNoPendingTasksOnAll(); - Settings exporterSettings = Settings.builder().put(localExporterSettings()) - .put("xpack.monitoring.migration.decommission_alerts", true).build(); + Settings exporterSettings = Settings.builder() + .put(localExporterSettings()) + .put("xpack.monitoring.migration.decommission_alerts", true) + .build(); MonitoringMigrationCoordinator coordinator = new MonitoringMigrationCoordinator(); assertTrue(coordinator.tryBlockInstallationTasks()); @@ -125,7 +131,7 @@ protected Settings localExporterSettings() { // cluster alert creation and decommissioning return Settings.builder() .put(super.localExporterSettings()) - .put("xpack.monitoring.exporters." + exporterName + ".cluster_alerts.management.enabled", true) + .put("xpack.monitoring.exporters." + exporterName + ".cluster_alerts.management.enabled", true) .build(); } @@ -151,20 +157,19 @@ private static BytesReference generateTemplateSource(final String name, final In // this would totally break Monitoring UI, but the idea is that it's different from a real template and // the version controls that; it also won't break indexing (just searching) so this test can use it blindly - builder - .field("index_patterns", name) + builder.field("index_patterns", name) .startObject("settings") - .field("index.number_of_shards", 1) - .field("index.number_of_replicas", 0) + .field("index.number_of_shards", 1) + .field("index.number_of_replicas", 0) .endObject() .startObject("mappings") - // The internal representation still requires a default type of _doc - .startObject("_doc") - .startObject("_meta") - .field("test", true) - .endObject() - .field("enabled", false) - .endObject() + // The internal representation still requires a default type of _doc + .startObject("_doc") + .startObject("_meta") + .field("test", true) + .endObject() + .field("enabled", false) + .endObject() .endObject(); if (version != null) { @@ -196,8 +201,7 @@ private void putWatches(final Integer version) throws Exception { for (final String watchId : ClusterAlertsUtil.WATCH_IDS) { final String uniqueWatchId = ClusterAlertsUtil.createUniqueWatchId(clusterService(), watchId); final BytesReference watch = generateWatchSource(watchId, clusterService().state().metadata().clusterUUID(), version); - client().execute(PutWatchAction.INSTANCE, new PutWatchRequest(uniqueWatchId, watch, XContentType.JSON)) - .actionGet(); + client().execute(PutWatchAction.INSTANCE, new PutWatchRequest(uniqueWatchId, watch, XContentType.JSON)).actionGet(); } } @@ -206,30 +210,26 @@ private void putWatches(final Integer version) throws Exception { */ private static BytesReference generateWatchSource(final String id, final String clusterUUID, final Integer version) throws Exception { final XContentBuilder builder = jsonBuilder().startObject(); - builder - .startObject("metadata") - .startObject("xpack") - .field("cluster_uuid", clusterUUID); - if(version != null) { - builder.field("version_created", Integer.toString(version)); - } - builder - .field("watch", id) - .endObject() + builder.startObject("metadata").startObject("xpack").field("cluster_uuid", clusterUUID); + if (version != null) { + builder.field("version_created", Integer.toString(version)); + } + builder.field("watch", id) + .endObject() .endObject() .startObject("trigger") - .startObject("schedule") - .field("interval", "30m") - .endObject() + .startObject("schedule") + .field("interval", "30m") + .endObject() .endObject() .startObject("input") - .startObject("simple") - .field("ignore", "ignore") - .endObject() + .startObject("simple") + .field("ignore", "ignore") + .endObject() .endObject() .startObject("condition") - .startObject("never") - .endObject() + .startObject("never") + .endObject() .endObject() .startObject("actions") .endObject(); @@ -297,8 +297,9 @@ private void assertNoWatchesExist() { for (SearchHit hit : searchResponse.getHits().getHits()) { invalidWatches.add(ObjectPath.eval("metadata.xpack.watch", hit.getSourceAsMap())); } - fail("Found [" + searchResponse.getHits().getTotalHits().value + "] invalid watches when none were expected: " - + invalidWatches); + fail( + "Found [" + searchResponse.getHits().getTotalHits().value + "] invalid watches when none were expected: " + invalidWatches + ); } } diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/rest/action/RestMonitoringBulkActionTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/rest/action/RestMonitoringBulkActionTests.java index fe958b80c8bd3..de9a2ba6e1a9c 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/rest/action/RestMonitoringBulkActionTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/rest/action/RestMonitoringBulkActionTests.java @@ -8,14 +8,9 @@ import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.core.CheckedConsumer; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesArray; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.xcontent.json.JsonXContent; +import org.elasticsearch.core.CheckedConsumer; import org.elasticsearch.rest.RestChannel; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; @@ -23,6 +18,11 @@ import org.elasticsearch.rest.action.RestBuilderListener; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.rest.FakeRestRequest; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; import org.elasticsearch.xpack.core.monitoring.action.MonitoringBulkResponse; import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringTemplateUtils; @@ -90,8 +90,10 @@ public void testUnsupportedSystemVersion() { final RestRequest restRequest = createRestRequest(MonitoredSystem.UNKNOWN.getSystem(), systemApiVersion, "30s"); final IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> prepareRequest(restRequest)); - assertThat(exception.getMessage(), - containsString("system_api_version [" + systemApiVersion + "] is not supported by system_id [unknown]")); + assertThat( + exception.getMessage(), + containsString("system_api_version [" + systemApiVersion + "] is not supported by system_id [unknown]") + ); } public void testUnknownSystemVersion() { @@ -99,8 +101,10 @@ public void testUnknownSystemVersion() { final RestRequest restRequest = createRestRequest(system.getSystem(), "0", "30s"); final IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> prepareRequest(restRequest)); - assertThat(exception.getMessage(), - containsString("system_api_version [0] is not supported by system_id [" + system.getSystem() + "]")); + assertThat( + exception.getMessage(), + containsString("system_api_version [0] is not supported by system_id [" + system.getSystem() + "]") + ); } public void testNoErrors() throws Exception { @@ -108,8 +112,10 @@ public void testNoErrors() throws Exception { final RestResponse restResponse = getRestBuilderListener().buildResponse(response); assertThat(restResponse.status(), is(RestStatus.OK)); - assertThat(restResponse.content().utf8ToString(), - is("{\"took\":" + response.getTookInMillis() + ",\"ignored\":false,\"errors\":false}")); + assertThat( + restResponse.content().utf8ToString(), + is("{\"took\":" + response.getTookInMillis() + ",\"ignored\":false,\"errors\":false}") + ); } public void testNoErrorsButIgnored() throws Exception { @@ -117,8 +123,10 @@ public void testNoErrorsButIgnored() throws Exception { final RestResponse restResponse = getRestBuilderListener().buildResponse(response); assertThat(restResponse.status(), is(RestStatus.OK)); - assertThat(restResponse.content().utf8ToString(), - is("{\"took\":" + response.getTookInMillis() + ",\"ignored\":true,\"errors\":false}")); + assertThat( + restResponse.content().utf8ToString(), + is("{\"took\":" + response.getTookInMillis() + ",\"ignored\":true,\"errors\":false}") + ); } public void testWithErrors() throws Exception { @@ -135,8 +143,10 @@ public void testWithErrors() throws Exception { } assertThat(restResponse.status(), is(RestStatus.INTERNAL_SERVER_ERROR)); - assertThat(restResponse.content().utf8ToString(), - is("{\"took\":" + response.getTookInMillis() + ",\"ignored\":false,\"errors\":true,\"error\":" + errorJson + "}")); + assertThat( + restResponse.content().utf8ToString(), + is("{\"took\":" + response.getTookInMillis() + ",\"ignored\":false,\"errors\":true,\"error\":" + errorJson + "}") + ); } /** @@ -165,10 +175,12 @@ private static FakeRestRequest createRestRequest(final String systemId, final St return createRestRequest(randomIntBetween(1, 10), systemId, systemApiVersion, interval); } - private static FakeRestRequest createRestRequest(final int nbDocs, - final String systemId, - final String systemApiVersion, - final String interval) { + private static FakeRestRequest createRestRequest( + final int nbDocs, + final String systemId, + final String systemApiVersion, + final String interval + ) { final FakeRestRequest.Builder builder = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY); if (nbDocs > 0) { final StringBuilder requestBody = new StringBuilder(); diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/rest/action/RestMonitoringMigrateAlertsActionTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/rest/action/RestMonitoringMigrateAlertsActionTests.java index 7df7cc770b83f..a92cf9c775aba 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/rest/action/RestMonitoringMigrateAlertsActionTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/rest/action/RestMonitoringMigrateAlertsActionTests.java @@ -7,20 +7,20 @@ package org.elasticsearch.xpack.monitoring.rest.action; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; - -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.rest.RestChannel; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.monitoring.action.MonitoringMigrateAlertsResponse; import org.elasticsearch.xpack.core.monitoring.action.MonitoringMigrateAlertsResponse.ExporterMigrationResult; import org.elasticsearch.xpack.monitoring.exporter.http.HttpExporter; import org.elasticsearch.xpack.monitoring.exporter.local.LocalExporter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.startsWith; import static org.mockito.Mockito.mock; @@ -42,12 +42,14 @@ public void testRestActionCompletion() throws Exception { List migrationResults = new ArrayList<>(); for (int i = 0; i < randomInt(5); i++) { boolean success = randomBoolean(); - migrationResults.add(new ExporterMigrationResult( - randomAlphaOfLength(10), - randomFrom(LocalExporter.TYPE, HttpExporter.TYPE), - success, - success ? null : new IOException("mock failure") - )); + migrationResults.add( + new ExporterMigrationResult( + randomAlphaOfLength(10), + randomFrom(LocalExporter.TYPE, HttpExporter.TYPE), + success, + success ? null : new IOException("mock failure") + ) + ); } MonitoringMigrateAlertsResponse restResponse = new MonitoringMigrateAlertsResponse(migrationResults); diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/test/MockClusterAlertScriptEngine.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/test/MockClusterAlertScriptEngine.java index 267ef89e39b47..f5cd70c8d6834 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/test/MockClusterAlertScriptEngine.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/test/MockClusterAlertScriptEngine.java @@ -48,8 +48,9 @@ public T compile(String name, String script, ScriptContext context, Map new HashMap()))); + return context.factoryClazz.cast( + new MockWatcherTransformScript(MockDeterministicScript.asDeterministic(p -> new HashMap())) + ); } // We want to just add an allowance for watcher scripts, and to delegate everything else to the parent class. diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/test/MockIngestPlugin.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/test/MockIngestPlugin.java index 76768877a5127..0a39a679e0b13 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/test/MockIngestPlugin.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/test/MockIngestPlugin.java @@ -24,16 +24,16 @@ public class MockIngestPlugin extends Plugin implements IngestPlugin { @Override public Map getProcessors(final Processor.Parameters parameters) { final Map processorFields = MapBuilder.newMapBuilder() - .put("gsub", new String[] { "field", "pattern", "replacement" }) - .put("rename", new String[] { "field", "target_field" }) - .put("set", new String[] { "field", "value" }) - .put("script", new String[] { "source" }) - .map(); + .put("gsub", new String[] { "field", "pattern", "replacement" }) + .put("rename", new String[] { "field", "target_field" }) + .put("set", new String[] { "field", "value" }) + .put("script", new String[] { "source" }) + .map(); return processorFields.entrySet() - .stream() - .map(MockProcessorFactory::new) - .collect(Collectors.toMap(factory -> factory.type, factory -> factory)); + .stream() + .map(MockProcessorFactory::new) + .collect(Collectors.toMap(factory -> factory.type, factory -> factory)); } static class MockProcessorFactory implements Processor.Factory { @@ -51,9 +51,12 @@ static class MockProcessorFactory implements Processor.Factory { } @Override - public Processor create(Map processorFactories, - String tag, - String description, Map config) throws Exception { + public Processor create( + Map processorFactories, + String tag, + String description, + Map config + ) throws Exception { // read fields so the processor succeeds for (final String field : fields) { ConfigurationUtils.readObject(type, tag, config, field); diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/test/MonitoringIntegTestCase.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/test/MonitoringIntegTestCase.java index b50a269809f53..8a7147fe301e0 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/test/MonitoringIntegTestCase.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/test/MonitoringIntegTestCase.java @@ -10,10 +10,10 @@ import org.elasticsearch.cluster.metadata.IndexTemplateMetadata; import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.Strings; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.regex.Regex; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.CountDown; +import org.elasticsearch.core.Tuple; import org.elasticsearch.index.IndexNotFoundException; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.test.ESIntegTestCase; @@ -45,16 +45,16 @@ public abstract class MonitoringIntegTestCase extends ESIntegTestCase { @Override protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { Settings.Builder builder = Settings.builder() - .put(super.nodeSettings(nodeOrdinal, otherSettings)) - .put(MonitoringService.INTERVAL.getKey(), MonitoringService.MIN_INTERVAL) -// .put(XPackSettings.SECURITY_ENABLED.getKey(), false) -// .put(XPackSettings.WATCHER_ENABLED.getKey(), false) - // Disable native ML autodetect_process as the c++ controller won't be available -// .put(MachineLearningField.AUTODETECT_PROCESS.getKey(), false) -// .put(XPackSettings.MACHINE_LEARNING_ENABLED.getKey(), false) - // we do this by default in core, but for monitoring this isn't needed and only adds noise. - .put("indices.lifecycle.history_index_enabled", false) - .put("index.store.mock.check_index_on_close", false); + .put(super.nodeSettings(nodeOrdinal, otherSettings)) + .put(MonitoringService.INTERVAL.getKey(), MonitoringService.MIN_INTERVAL) + // .put(XPackSettings.SECURITY_ENABLED.getKey(), false) + // .put(XPackSettings.WATCHER_ENABLED.getKey(), false) + // Disable native ML autodetect_process as the c++ controller won't be available + // .put(MachineLearningField.AUTODETECT_PROCESS.getKey(), false) + // .put(XPackSettings.MACHINE_LEARNING_ENABLED.getKey(), false) + // we do this by default in core, but for monitoring this isn't needed and only adds noise. + .put("indices.lifecycle.history_index_enabled", false) + .put("index.store.mock.check_index_on_close", false); return builder.build(); } @@ -69,8 +69,12 @@ protected Collection> getMockPlugins() { @Override protected Collection> nodePlugins() { - return Arrays.asList(LocalStateMonitoring.class, MockClusterAlertScriptEngine.TestPlugin.class, - MockIngestPlugin.class, CommonAnalysisPlugin.class); + return Arrays.asList( + LocalStateMonitoring.class, + MockClusterAlertScriptEngine.TestPlugin.class, + MockIngestPlugin.class, + CommonAnalysisPlugin.class + ); } @Override @@ -126,15 +130,15 @@ protected List> monitoringWatches() { final ClusterService clusterService = clusterService(); return Arrays.stream(ClusterAlertsUtil.WATCH_IDS) - .map(id -> new Tuple<>(id, ClusterAlertsUtil.loadWatch(clusterService, id))) - .collect(Collectors.toList()); + .map(id -> new Tuple<>(id, ClusterAlertsUtil.loadWatch(clusterService, id))) + .collect(Collectors.toList()); } protected void assertTemplateInstalled(String name) { boolean found = false; for (IndexTemplateMetadata template : client().admin().indices().prepareGetTemplates().get().getIndexTemplates()) { if (Regex.simpleMatch(name, template.getName())) { - found = true; + found = true; } } assertTrue("failed to find a template matching [" + name + "]", found); @@ -157,12 +161,20 @@ private void assertIndicesExists(String... indices) { } protected void enableMonitoringCollection() { - assertAcked(client().admin().cluster().prepareUpdateSettings().setTransientSettings( - Settings.builder().put(MonitoringService.ENABLED.getKey(), true))); + assertAcked( + client().admin() + .cluster() + .prepareUpdateSettings() + .setTransientSettings(Settings.builder().put(MonitoringService.ENABLED.getKey(), true)) + ); } protected void disableMonitoringCollection() { - assertAcked(client().admin().cluster().prepareUpdateSettings().setTransientSettings( - Settings.builder().putNull(MonitoringService.ENABLED.getKey()))); + assertAcked( + client().admin() + .cluster() + .prepareUpdateSettings() + .setTransientSettings(Settings.builder().putNull(MonitoringService.ENABLED.getKey())) + ); } } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/analyzer/AnalyzerRules.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/analyzer/AnalyzerRules.java index fb83682bbb643..10f510436c955 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/analyzer/AnalyzerRules.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/analyzer/AnalyzerRules.java @@ -32,8 +32,9 @@ protected LogicalPlan rule(Filter filter) { Expression condition = replaceRawBoolFieldWithEquals(filter.condition()); // otherwise look for binary logic if (condition == filter.condition()) { - condition = condition.transformUp(BinaryLogic.class, b -> - b.replaceChildren(asList(replaceRawBoolFieldWithEquals(b.left()), replaceRawBoolFieldWithEquals(b.right()))) + condition = condition.transformUp( + BinaryLogic.class, + b -> b.replaceChildren(asList(replaceRawBoolFieldWithEquals(b.left()), replaceRawBoolFieldWithEquals(b.right()))) ); } @@ -56,7 +57,6 @@ protected boolean skipResolved() { } } - public abstract static class AnalyzerRule extends Rule { // transformUp (post-order) - that is first children and then the node diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/async/AsyncTaskManagementService.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/async/AsyncTaskManagementService.java index e4feaf8c947fe..ed510f884b5a8 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/async/AsyncTaskManagementService.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/async/AsyncTaskManagementService.java @@ -44,7 +44,9 @@ /** * Service for managing EQL requests */ -public class AsyncTaskManagementService> { private static final Logger logger = LogManager.getLogger(AsyncTaskManagementService.class); @@ -57,11 +59,21 @@ public class AsyncTaskManagementService taskClass; - public interface AsyncOperation { - T createTask(Request request, long id, String type, String action, TaskId parentTaskId, Map headers, - Map originHeaders, AsyncExecutionId asyncExecutionId); + T createTask( + Request request, + long id, + String type, + String action, + TaskId parentTaskId, + Map headers, + Map originHeaders, + AsyncExecutionId asyncExecutionId + ); void execute(Request request, T task, ActionListener listener); @@ -97,8 +109,16 @@ public TaskId getParentTask() { @Override public Task createTask(long id, String type, String action, TaskId parentTaskId, Map headers) { Map originHeaders = ClientHelper.filterSecurityHeaders(threadPool.getThreadContext().getHeaders()); - return operation.createTask(request, id, type, action, parentTaskId, headers, originHeaders, new AsyncExecutionId(doc, - new TaskId(node, id))); + return operation.createTask( + request, + id, + type, + action, + parentTaskId, + headers, + originHeaders, + new AsyncExecutionId(doc, new TaskId(node, id)) + ); } @Override @@ -107,30 +127,54 @@ public String getDescription() { } } - public AsyncTaskManagementService(String index, Client client, String origin, NamedWriteableRegistry registry, TaskManager taskManager, - String action, AsyncOperation operation, Class taskClass, - ClusterService clusterService, - ThreadPool threadPool, - BigArrays bigArrays) { + public AsyncTaskManagementService( + String index, + Client client, + String origin, + NamedWriteableRegistry registry, + TaskManager taskManager, + String action, + AsyncOperation operation, + Class taskClass, + ClusterService clusterService, + ThreadPool threadPool, + BigArrays bigArrays + ) { this.taskManager = taskManager; this.action = action; this.operation = operation; this.taskClass = taskClass; - this.asyncTaskIndexService = new AsyncTaskIndexService<>(index, clusterService, threadPool.getThreadContext(), client, - origin, i -> new StoredAsyncResponse<>(operation::readResponse, i), registry, bigArrays); + this.asyncTaskIndexService = new AsyncTaskIndexService<>( + index, + clusterService, + threadPool.getThreadContext(), + client, + origin, + i -> new StoredAsyncResponse<>(operation::readResponse, i), + registry, + bigArrays + ); this.clusterService = clusterService; this.threadPool = threadPool; } - public void asyncExecute(Request request, TimeValue waitForCompletionTimeout, TimeValue keepAlive, boolean keepOnCompletion, - ActionListener listener) { + public void asyncExecute( + Request request, + TimeValue waitForCompletionTimeout, + TimeValue keepAlive, + boolean keepOnCompletion, + ActionListener listener + ) { String nodeId = clusterService.localNode().getId(); @SuppressWarnings("unchecked") T searchTask = (T) taskManager.register("transport", action + "[a]", new AsyncRequestWrapper(request, nodeId)); boolean operationStarted = false; try { - operation.execute(request, searchTask, - wrapStoringListener(searchTask, waitForCompletionTimeout, keepAlive, keepOnCompletion, listener)); + operation.execute( + request, + searchTask, + wrapStoringListener(searchTask, waitForCompletionTimeout, keepAlive, keepOnCompletion, listener) + ); operationStarted = true; } finally { // If we didn't start operation for any reason, we need to clean up the task that we have created @@ -140,11 +184,13 @@ public void asyncExecute(Request request, TimeValue waitForCompletionTimeout, Ti } } - private ActionListener wrapStoringListener(T searchTask, - TimeValue waitForCompletionTimeout, - TimeValue keepAlive, - boolean keepOnCompletion, - ActionListener listener) { + private ActionListener wrapStoringListener( + T searchTask, + TimeValue waitForCompletionTimeout, + TimeValue keepAlive, + boolean keepOnCompletion, + ActionListener listener + ) { AtomicReference> exclusiveListener = new AtomicReference<>(listener); // This is will performed in case of timeout Scheduler.ScheduledCancellable timeoutHandler = threadPool.schedule(() -> { @@ -160,9 +206,11 @@ private ActionListener wrapStoringListener(T searchTask, // We finished before timeout timeoutHandler.cancel(); if (keepOnCompletion) { - storeResults(searchTask, + storeResults( + searchTask, new StoredAsyncResponse<>(response, threadPool.absoluteTimeInMillis() + keepAlive.getMillis()), - ActionListener.wrap(() -> acquiredListener.onResponse(response))); + ActionListener.wrap(() -> acquiredListener.onResponse(response)) + ); } else { taskManager.unregister(searchTask); searchTask.onResponse(response); @@ -178,9 +226,11 @@ private ActionListener wrapStoringListener(T searchTask, // We finished before timeout timeoutHandler.cancel(); if (keepOnCompletion) { - storeResults(searchTask, + storeResults( + searchTask, new StoredAsyncResponse<>(e, threadPool.absoluteTimeInMillis() + keepAlive.getMillis()), - ActionListener.wrap(() -> acquiredListener.onFailure(e))); + ActionListener.wrap(() -> acquiredListener.onFailure(e)) + ); } else { taskManager.unregister(searchTask); searchTask.onFailure(e); @@ -199,12 +249,16 @@ private void storeResults(T searchTask, StoredAsyncResponse storedResp private void storeResults(T searchTask, StoredAsyncResponse storedResponse, ActionListener finalListener) { try { - asyncTaskIndexService.createResponseForEQL(searchTask.getExecutionId().getDocId(), - searchTask.getOriginHeaders(), storedResponse, ActionListener.wrap( + asyncTaskIndexService.createResponseForEQL( + searchTask.getExecutionId().getDocId(), + searchTask.getOriginHeaders(), + storedResponse, + ActionListener.wrap( // We should only unregister after the result is saved resp -> { - logger.trace(() -> new ParameterizedMessage("stored eql search results for [{}]", - searchTask.getExecutionId().getEncoded())); + logger.trace( + () -> new ParameterizedMessage("stored eql search results for [{}]", searchTask.getExecutionId().getEncoded()) + ); taskManager.unregister(searchTask); if (storedResponse.getException() != null) { searchTask.onFailure(storedResponse.getException()); @@ -219,20 +273,29 @@ private void storeResults(T searchTask, StoredAsyncResponse storedResp taskManager.unregister(searchTask); searchTask.onFailure(exc); Throwable cause = ExceptionsHelper.unwrapCause(exc); - if (cause instanceof DocumentMissingException == false && - cause instanceof VersionConflictEngineException == false) { - logger.error(() -> new ParameterizedMessage("failed to store eql search results for [{}]", - searchTask.getExecutionId().getEncoded()), exc); + if (cause instanceof DocumentMissingException == false + && cause instanceof VersionConflictEngineException == false) { + logger.error( + () -> new ParameterizedMessage( + "failed to store eql search results for [{}]", + searchTask.getExecutionId().getEncoded() + ), + exc + ); } if (finalListener != null) { finalListener.onFailure(exc); } - })); + } + ) + ); } catch (Exception exc) { taskManager.unregister(searchTask); searchTask.onFailure(exc); - logger.error(() -> new ParameterizedMessage("failed to store eql search results for [{}]", - searchTask.getExecutionId().getEncoded()), exc); + logger.error( + () -> new ParameterizedMessage("failed to store eql search results for [{}]", searchTask.getExecutionId().getEncoded()), + exc + ); } } @@ -244,18 +307,27 @@ public static > listener, - TimeValue timeout) { + TimeValue timeout + ) { if (timeout.getMillis() <= 0) { getCurrentResult(task, listener); } else { - task.addCompletionListener(ListenerTimeouts.wrapWithTimeout(threadPool, timeout, ThreadPool.Names.SEARCH, ActionListener.wrap( - r -> listener.onResponse(new StoredAsyncResponse<>(r, task.getExpirationTimeMillis())), - e -> listener.onResponse(new StoredAsyncResponse<>(e, task.getExpirationTimeMillis())) - ), wrapper -> { - // Timeout was triggered - task.removeCompletionListener(wrapper); - getCurrentResult(task, listener); - })); + task.addCompletionListener( + ListenerTimeouts.wrapWithTimeout( + threadPool, + timeout, + ThreadPool.Names.SEARCH, + ActionListener.wrap( + r -> listener.onResponse(new StoredAsyncResponse<>(r, task.getExpirationTimeMillis())), + e -> listener.onResponse(new StoredAsyncResponse<>(e, task.getExpirationTimeMillis())) + ), + wrapper -> { + // Timeout was triggered + task.removeCompletionListener(wrapper); + getCurrentResult(task, listener); + } + ) + ); } } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/async/QlStatusResponse.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/async/QlStatusResponse.java index e729070feadda..30e43e71c82bd 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/async/QlStatusResponse.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/async/QlStatusResponse.java @@ -12,8 +12,8 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.xcontent.StatusToXContentObject; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.async.StoredAsyncResponse; import org.elasticsearch.xpack.core.search.action.SearchStatusResponse; @@ -41,12 +41,14 @@ public interface AsyncStatus { boolean isPartial(); } - public QlStatusResponse(String id, - boolean isRunning, - boolean isPartial, - Long startTimeMillis, - long expirationTimeMillis, - RestStatus completionStatus) { + public QlStatusResponse( + String id, + boolean isRunning, + boolean isPartial, + Long startTimeMillis, + long expirationTimeMillis, + RestStatus completionStatus + ) { this.id = id; this.isRunning = isRunning; this.isPartial = isPartial; @@ -62,8 +64,11 @@ public QlStatusResponse(String id, * @param id – encoded async search id * @return a status response */ - public static QlStatusResponse getStatusFromStoredSearch(StoredAsyncResponse storedResponse, - long expirationTimeMillis, String id) { + public static QlStatusResponse getStatusFromStoredSearch( + StoredAsyncResponse storedResponse, + long expirationTimeMillis, + String id + ) { S searchResponse = storedResponse.getResponse(); if (searchResponse != null) { assert searchResponse.isRunning() == false : "Stored Ql search response must have a completed status!"; diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/capabilities/Unresolvable.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/capabilities/Unresolvable.java index 10baa53ec0535..f214575add6db 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/capabilities/Unresolvable.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/capabilities/Unresolvable.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.xpack.ql.capabilities; - public interface Unresolvable extends Resolvable { String UNRESOLVED_PREFIX = "?"; diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/common/Failure.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/common/Failure.java index 6e0e720b5a26b..f5cedc4cc680f 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/common/Failure.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/common/Failure.java @@ -67,8 +67,13 @@ public static String failMessage(Collection failures) { return failures.stream().map(f -> { Location l = f.node().source().source(); return "line " + l.getLineNumber() + ":" + l.getColumnNumber() + ": " + f.message(); - }).collect(Collectors.joining(StringUtils.NEW_LINE, - format("Found {} problem{}\n", failures.size(), failures.size() > 1 ? "s" : StringUtils.EMPTY), - StringUtils.EMPTY)); + }) + .collect( + Collectors.joining( + StringUtils.NEW_LINE, + format("Found {} problem{}\n", failures.size(), failures.size() > 1 ? "s" : StringUtils.EMPTY), + StringUtils.EMPTY + ) + ); } } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/execution/search/QlSourceBuilder.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/execution/search/QlSourceBuilder.java index 8977155fb222d..946fa8797ccba 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/execution/search/QlSourceBuilder.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/execution/search/QlSourceBuilder.java @@ -30,8 +30,7 @@ public class QlSourceBuilder { boolean trackScores = false; - public QlSourceBuilder() { - } + public QlSourceBuilder() {} /** * Turns on returning the {@code _score} for documents. diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/execution/search/extractor/AbstractFieldHitExtractor.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/execution/search/extractor/AbstractFieldHitExtractor.java index fd6d3b442462f..1899e788566af 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/execution/search/extractor/AbstractFieldHitExtractor.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/execution/search/extractor/AbstractFieldHitExtractor.java @@ -153,7 +153,7 @@ protected Object unwrapFieldsMultiValue(Object values) { } if (values instanceof Map && hitName != null) { // extract the sub-field from a nested field (dep.dep_name -> dep_name) - return unwrapFieldsMultiValue(((Map) values).get(fieldName.substring(hitName.length() + 1))); + return unwrapFieldsMultiValue(((Map) values).get(fieldName.substring(hitName.length() + 1))); } if (values instanceof List) { List list = (List) values; @@ -214,13 +214,11 @@ public boolean equals(Object obj) { return false; } AbstractFieldHitExtractor other = (AbstractFieldHitExtractor) obj; - return fieldName.equals(other.fieldName) - && hitName.equals(other.hitName) - && arrayLeniency == other.arrayLeniency; + return fieldName.equals(other.fieldName) && hitName.equals(other.hitName) && arrayLeniency == other.arrayLeniency; } @Override public int hashCode() { return Objects.hash(fieldName, hitName, arrayLeniency); } -} \ No newline at end of file +} diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/execution/search/extractor/ComputingExtractor.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/execution/search/extractor/ComputingExtractor.java index 16c3f64b812d2..4ae74ab547f72 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/execution/search/extractor/ComputingExtractor.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/execution/search/extractor/ComputingExtractor.java @@ -91,8 +91,7 @@ public boolean equals(Object obj) { return false; } ComputingExtractor other = (ComputingExtractor) obj; - return Objects.equals(processor, other.processor) - && Objects.equals(hitName, other.hitName); + return Objects.equals(processor, other.processor) && Objects.equals(hitName, other.hitName); } @Override diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/execution/search/extractor/HitExtractor.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/execution/search/extractor/HitExtractor.java index afa7d2ab3daf2..9af97c09af824 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/execution/search/extractor/HitExtractor.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/execution/search/extractor/HitExtractor.java @@ -6,8 +6,8 @@ */ package org.elasticsearch.xpack.ql.execution.search.extractor; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.io.stream.NamedWriteable; +import org.elasticsearch.core.Nullable; import org.elasticsearch.search.SearchHit; /** diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/Alias.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/Alias.java index c8ee2811faf12..7f22cece89b00 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/Alias.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/Alias.java @@ -86,9 +86,9 @@ public DataType dataType() { @Override public Attribute toAttribute() { if (lazyAttribute == null) { - lazyAttribute = resolved() ? - new ReferenceAttribute(source(), name(), dataType(), qualifier, nullable(), id(), synthetic()) : - new UnresolvedAttribute(source(), name(), qualifier); + lazyAttribute = resolved() + ? new ReferenceAttribute(source(), name(), dataType(), qualifier, nullable(), id(), synthetic()) + : new UnresolvedAttribute(source(), name(), qualifier); } return lazyAttribute; } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/Attribute.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/Attribute.java index 0eab3d834a905..fe26fe54cb469 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/Attribute.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/Attribute.java @@ -77,8 +77,9 @@ public Attribute withLocation(Source source) { } public Attribute withQualifier(String qualifier) { - return Objects.equals(qualifier(), qualifier) ? this : clone(source(), name(), dataType(), qualifier, nullable(), id(), - synthetic()); + return Objects.equals(qualifier(), qualifier) + ? this + : clone(source(), name(), dataType(), qualifier, nullable(), id(), synthetic()); } public Attribute withName(String name) { @@ -86,8 +87,9 @@ public Attribute withName(String name) { } public Attribute withNullability(Nullability nullability) { - return Objects.equals(nullable(), nullability) ? this : clone(source(), name(), dataType(), qualifier(), nullability, id(), - synthetic()); + return Objects.equals(nullable(), nullability) + ? this + : clone(source(), name(), dataType(), qualifier(), nullability, id(), synthetic()); } public Attribute withId(NameId id) { @@ -98,8 +100,15 @@ public Attribute withDataType(DataType type) { return Objects.equals(dataType(), type) ? this : clone(source(), name(), type, qualifier(), nullable(), id(), synthetic()); } - protected abstract Attribute clone(Source source, String name, DataType type, String qualifier, Nullability nullability, - NameId id, boolean synthetic); + protected abstract Attribute clone( + Source source, + String name, + DataType type, + String qualifier, + Nullability nullability, + NameId id, + boolean synthetic + ); @Override public Attribute toAttribute() { @@ -130,8 +139,7 @@ public int hashCode() { public boolean equals(Object obj) { if (super.equals(obj)) { Attribute other = (Attribute) obj; - return Objects.equals(qualifier, other.qualifier) - && Objects.equals(nullability, other.nullability); + return Objects.equals(qualifier, other.qualifier) && Objects.equals(nullability, other.nullability); } return false; diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/AttributeMap.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/AttributeMap.java index 5b6e1654d9c59..b9e75351d9cac 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/AttributeMap.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/AttributeMap.java @@ -82,7 +82,6 @@ public U next() { protected abstract U unwrap(W next); - @Override public Stream stream() { return set.stream().map(this::unwrap); diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/AttributeSet.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/AttributeSet.java index cdaa48d949e4d..5c2a15a6d75ea 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/AttributeSet.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/AttributeSet.java @@ -36,8 +36,7 @@ public AttributeSet(Attribute attr) { public AttributeSet(Collection attr) { if (attr.isEmpty()) { delegate = EMPTY_DELEGATE; - } - else { + } else { delegate = new AttributeMap<>(); for (Attribute a : attr) { @@ -180,6 +179,7 @@ public boolean equals(Object o) { public int hashCode() { return delegate.hashCode(); } + @Override public String toString() { return delegate.keySet().toString(); diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/EmptyAttribute.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/EmptyAttribute.java index ef2680dbb80e4..a34ac136b1476 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/EmptyAttribute.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/EmptyAttribute.java @@ -24,8 +24,15 @@ public EmptyAttribute(Source source) { } @Override - protected Attribute clone(Source source, String name, DataType type, String qualifier, Nullability nullability, NameId id, - boolean synthetic) { + protected Attribute clone( + Source source, + String name, + DataType type, + String qualifier, + Nullability nullability, + NameId id, + boolean synthetic + ) { return this; } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/Expressions.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/Expressions.java index b3cdebb02e8e2..36ae521d7de85 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/Expressions.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/Expressions.java @@ -25,7 +25,6 @@ public final class Expressions { - private Expressions() {} public static NamedExpression wrapAsNamed(Expression exp) { diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/FieldAttribute.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/FieldAttribute.java index c4905dda22a16..d9fcc4b15c112 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/FieldAttribute.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/FieldAttribute.java @@ -39,13 +39,30 @@ public FieldAttribute(Source source, FieldAttribute parent, String name, EsField this(source, parent, name, field, null, Nullability.TRUE, null, false); } - public FieldAttribute(Source source, FieldAttribute parent, String name, EsField field, String qualifier, Nullability nullability, - NameId id, boolean synthetic) { + public FieldAttribute( + Source source, + FieldAttribute parent, + String name, + EsField field, + String qualifier, + Nullability nullability, + NameId id, + boolean synthetic + ) { this(source, parent, name, field.getDataType(), field, qualifier, nullability, id, synthetic); } - public FieldAttribute(Source source, FieldAttribute parent, String name, DataType type, EsField field, String qualifier, - Nullability nullability, NameId id, boolean synthetic) { + public FieldAttribute( + Source source, + FieldAttribute parent, + String name, + DataType type, + EsField field, + String qualifier, + Nullability nullability, + NameId id, + boolean synthetic + ) { super(source, name, type, qualifier, nullability, id, synthetic); this.path = parent != null ? parent.name() : StringUtils.EMPTY; this.parent = parent; @@ -105,8 +122,15 @@ private FieldAttribute innerField(EsField type) { } @Override - protected Attribute clone(Source source, String name, DataType type, String qualifier, Nullability nullability, NameId id, - boolean synthetic) { + protected Attribute clone( + Source source, + String name, + DataType type, + String qualifier, + Nullability nullability, + NameId id, + boolean synthetic + ) { FieldAttribute qualifiedParent = parent != null ? (FieldAttribute) parent.withQualifier(qualifier) : null; return new FieldAttribute(source, qualifiedParent, name, field, qualifier, nullability, id, synthetic); } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/NamedExpression.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/NamedExpression.java index 606e539dbfdc0..790a74e96b019 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/NamedExpression.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/NamedExpression.java @@ -21,7 +21,6 @@ public abstract class NamedExpression extends Expression { private final NameId id; private final boolean synthetic; - public NamedExpression(Source source, String name, List children, NameId id) { this(source, name, children, id, false); } @@ -63,14 +62,14 @@ public boolean equals(Object obj) { NamedExpression other = (NamedExpression) obj; return Objects.equals(synthetic, other.synthetic) - /* - * It is important that the line below be `name` - * and not `name()` because subclasses might override - * `name()` in ways that are not compatible with - * equality. Specifically the `Unresolved` subclasses. - */ - && Objects.equals(name, other.name) - && Objects.equals(children(), other.children()); + /* + * It is important that the line below be `name` + * and not `name()` because subclasses might override + * `name()` in ways that are not compatible with + * equality. Specifically the `Unresolved` subclasses. + */ + && Objects.equals(name, other.name) + && Objects.equals(children(), other.children()); } @Override diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/Order.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/Order.java index bc8b0c162cadb..567a84c91b45b 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/Order.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/Order.java @@ -19,11 +19,13 @@ public class Order extends Expression { public enum OrderDirection { - ASC, DESC + ASC, + DESC } public enum NullsPosition { - FIRST, LAST, + FIRST, + LAST, /** * Nulls position has not been specified by the user and an appropriate default will be used. * @@ -102,8 +104,6 @@ public boolean equals(Object obj) { } Order other = (Order) obj; - return Objects.equals(direction, other.direction) - && Objects.equals(nulls, other.nulls) - && Objects.equals(child, other.child); + return Objects.equals(direction, other.direction) && Objects.equals(nulls, other.nulls) && Objects.equals(child, other.child); } } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/ReferenceAttribute.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/ReferenceAttribute.java index 26e19951bb174..15616d62f5bf5 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/ReferenceAttribute.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/ReferenceAttribute.java @@ -19,14 +19,28 @@ public ReferenceAttribute(Source source, String name, DataType dataType) { this(source, name, dataType, null, Nullability.FALSE, null, false); } - public ReferenceAttribute(Source source, String name, DataType dataType, String qualifier, Nullability nullability, - NameId id, boolean synthetic) { + public ReferenceAttribute( + Source source, + String name, + DataType dataType, + String qualifier, + Nullability nullability, + NameId id, + boolean synthetic + ) { super(source, name, dataType, qualifier, nullability, id, synthetic); } @Override - protected Attribute clone(Source source, String name, DataType dataType, String qualifier, Nullability nullability, NameId id, - boolean synthetic) { + protected Attribute clone( + Source source, + String name, + DataType dataType, + String qualifier, + Nullability nullability, + NameId id, + boolean synthetic + ) { return new ReferenceAttribute(source, name, dataType, qualifier, nullability, id, synthetic); } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/TypeResolutions.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/TypeResolutions.java index d4e451f534e46..fde73b0f05dc7 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/TypeResolutions.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/TypeResolutions.java @@ -121,37 +121,54 @@ public static TypeResolution isIPAndExact(Expression e, String operationName, Pa public static TypeResolution isFoldable(Expression e, String operationName, ParamOrdinal paramOrd) { if (e.foldable() == false) { - return new TypeResolution(format(null, "{}argument of [{}] must be a constant, received [{}]", - paramOrd == null || paramOrd == DEFAULT ? "" : paramOrd.name().toLowerCase(Locale.ROOT) + " ", - operationName, - Expressions.name(e))); + return new TypeResolution( + format( + null, + "{}argument of [{}] must be a constant, received [{}]", + paramOrd == null || paramOrd == DEFAULT ? "" : paramOrd.name().toLowerCase(Locale.ROOT) + " ", + operationName, + Expressions.name(e) + ) + ); } return TypeResolution.TYPE_RESOLVED; } public static TypeResolution isNotFoldable(Expression e, String operationName, ParamOrdinal paramOrd) { if (e.foldable()) { - return new TypeResolution(format(null, "{}argument of [{}] must be a table column, found constant [{}]", - paramOrd == null || paramOrd == DEFAULT ? "" : paramOrd.name().toLowerCase(Locale.ROOT) + " ", - operationName, - Expressions.name(e))); + return new TypeResolution( + format( + null, + "{}argument of [{}] must be a table column, found constant [{}]", + paramOrd == null || paramOrd == DEFAULT ? "" : paramOrd.name().toLowerCase(Locale.ROOT) + " ", + operationName, + Expressions.name(e) + ) + ); } return TypeResolution.TYPE_RESOLVED; } - public static TypeResolution isType(Expression e, - Predicate predicate, - String operationName, - ParamOrdinal paramOrd, - String... acceptedTypes) { - return predicate.test(e.dataType()) || e.dataType() == NULL ? - TypeResolution.TYPE_RESOLVED : - new TypeResolution(format(null, "{}argument of [{}] must be [{}], found value [{}] type [{}]", - paramOrd == null || paramOrd == DEFAULT ? "" : paramOrd.name().toLowerCase(Locale.ROOT) + " ", - operationName, - acceptedTypesForErrorMsg(acceptedTypes), - name(e), - e.dataType().typeName())); + public static TypeResolution isType( + Expression e, + Predicate predicate, + String operationName, + ParamOrdinal paramOrd, + String... acceptedTypes + ) { + return predicate.test(e.dataType()) || e.dataType() == NULL + ? TypeResolution.TYPE_RESOLVED + : new TypeResolution( + format( + null, + "{}argument of [{}] must be [{}], found value [{}] type [{}]", + paramOrd == null || paramOrd == DEFAULT ? "" : paramOrd.name().toLowerCase(Locale.ROOT) + " ", + operationName, + acceptedTypesForErrorMsg(acceptedTypes), + name(e), + e.dataType().typeName() + ) + ); } private static String acceptedTypesForErrorMsg(String... acceptedTypes) { diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/TypedAttribute.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/TypedAttribute.java index ebb54e6194c0c..aab1533dc17c8 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/TypedAttribute.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/TypedAttribute.java @@ -15,8 +15,15 @@ public abstract class TypedAttribute extends Attribute { private final DataType dataType; - protected TypedAttribute(Source source, String name, DataType dataType, String qualifier, Nullability nullability, - NameId id, boolean synthetic) { + protected TypedAttribute( + Source source, + String name, + DataType dataType, + String qualifier, + Nullability nullability, + NameId id, + boolean synthetic + ) { super(source, name, qualifier, nullability, id, synthetic); this.dataType = dataType; } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/UnaryExpression.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/UnaryExpression.java index 8c96b9c15e5dc..81ddaf2784954 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/UnaryExpression.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/UnaryExpression.java @@ -27,6 +27,7 @@ protected UnaryExpression(Source source, Expression child) { public final UnaryExpression replaceChildren(List newChildren) { return replaceChild(newChildren.get(0)); } + protected abstract UnaryExpression replaceChild(Expression newChild); public Expression child() { diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/UnresolvedAttribute.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/UnresolvedAttribute.java index 7f305cbbede9e..7de89fa1f2d86 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/UnresolvedAttribute.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/UnresolvedAttribute.java @@ -35,8 +35,14 @@ public UnresolvedAttribute(Source source, String name, String qualifier, String this(source, name, qualifier, null, unresolvedMessage, null); } - public UnresolvedAttribute(Source source, String name, String qualifier, NameId id, String unresolvedMessage, - Object resolutionMetadata) { + public UnresolvedAttribute( + Source source, + String name, + String qualifier, + NameId id, + String unresolvedMessage, + Object resolutionMetadata + ) { super(source, name, qualifier, id); this.customMessage = unresolvedMessage != null; this.unresolvedMsg = unresolvedMessage == null ? errorMessage(qualifiedName(), null) : unresolvedMessage; @@ -45,8 +51,7 @@ public UnresolvedAttribute(Source source, String name, String qualifier, NameId @Override protected NodeInfo info() { - return NodeInfo.create(this, UnresolvedAttribute::new, - name(), qualifier(), id(), unresolvedMsg, resolutionMetadata); + return NodeInfo.create(this, UnresolvedAttribute::new, name(), qualifier(), id(), unresolvedMsg, resolutionMetadata); } public Object resolutionMetadata() { @@ -63,8 +68,15 @@ public boolean resolved() { } @Override - protected Attribute clone(Source source, String name, DataType dataType, String qualifier, Nullability nullability, - NameId id, boolean synthetic) { + protected Attribute clone( + Source source, + String name, + DataType dataType, + String qualifier, + Nullability nullability, + NameId id, + boolean synthetic + ) { return this; } @@ -100,8 +112,9 @@ public String unresolvedMessage() { public static String errorMessage(String name, List potentialMatches) { String msg = "Unknown column [" + name + "]"; if (CollectionUtils.isEmpty(potentialMatches) == false) { - msg += ", did you mean " + (potentialMatches.size() == 1 ? "[" + potentialMatches.get(0) - + "]": "any of " + potentialMatches.toString()) + "?"; + msg += ", did you mean " + + (potentialMatches.size() == 1 ? "[" + potentialMatches.get(0) + "]" : "any of " + potentialMatches.toString()) + + "?"; } return msg; } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/DefaultFunctionTypeRegistry.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/DefaultFunctionTypeRegistry.java index 6c96a40ef88da..056c7606882f2 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/DefaultFunctionTypeRegistry.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/DefaultFunctionTypeRegistry.java @@ -9,7 +9,6 @@ import org.elasticsearch.xpack.ql.expression.function.aggregate.AggregateFunction; import org.elasticsearch.xpack.ql.expression.function.scalar.ScalarFunction; - public class DefaultFunctionTypeRegistry implements FunctionTypeRegistry { public static final DefaultFunctionTypeRegistry INSTANCE = new DefaultFunctionTypeRegistry(); diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/FunctionRegistry.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/FunctionRegistry.java index 0b930572ec8e2..3e67d1acf5498 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/FunctionRegistry.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/FunctionRegistry.java @@ -34,14 +34,7 @@ public class FunctionRegistry { // Translation table for error messaging in the following function - private static final String[] NUM_NAMES = { - "zero", - "one", - "two", - "three", - "four", - "five", - }; + private static final String[] NUM_NAMES = { "zero", "one", "two", "three", "four", "five", }; // list of functions grouped by type of functions (aggregate, statistics, math etc) and ordered alphabetically inside each group // a single function will have one entry for itself with its name associated to its instance and, also, one entry for each alias @@ -76,18 +69,38 @@ protected void register(FunctionDefinition... functions) { for (String alias : f.aliases()) { Object old = batchMap.put(alias, f); if (old != null || defs.containsKey(alias)) { - throw new QlIllegalArgumentException("alias [" + alias + "] is used by " - + "[" + (old != null ? old : defs.get(alias).name()) + "] and [" + f.name() + "]"); + throw new QlIllegalArgumentException( + "alias [" + + alias + + "] is used by " + + "[" + + (old != null ? old : defs.get(alias).name()) + + "] and [" + + f.name() + + "]" + ); } aliases.put(alias, f.name()); } } // sort the temporary map by key name and add it to the global map of functions - defs.putAll(batchMap.entrySet().stream() + defs.putAll( + batchMap.entrySet() + .stream() .sorted(Map.Entry.comparingByKey()) - .collect(Collectors., String, - FunctionDefinition, LinkedHashMap> toMap(Map.Entry::getKey, Map.Entry::getValue, - (oldValue, newValue) -> oldValue, LinkedHashMap::new))); + .collect( + Collectors.< + Entry, + String, + FunctionDefinition, + LinkedHashMap>toMap( + Map.Entry::getKey, + Map.Entry::getValue, + (oldValue, newValue) -> oldValue, + LinkedHashMap::new + ) + ) + ); } public FunctionDefinition resolveFunction(String functionName) { @@ -119,7 +132,8 @@ public Collection listFunctions() { public Collection listFunctions(String pattern) { // It is worth double checking if we need this copy. These are immutable anyway. Pattern p = Strings.hasText(pattern) ? Pattern.compile(normalize(pattern)) : null; - return defs.entrySet().stream() + return defs.entrySet() + .stream() .filter(e -> p == null || p.matcher(e.getKey()).matches()) .map(e -> cloneDefinition(e.getKey(), e.getValue())) .collect(toList()); @@ -145,9 +159,12 @@ protected static FunctionDefinition def(Class function, Func List aliases = Arrays.asList(names).subList(1, names.length); FunctionDefinition.Builder realBuilder = (uf, cfg, extras) -> { if (CollectionUtils.isEmpty(extras) == false) { - throw new ParsingException(uf.source(), "Unused parameters {} detected when building [{}]", + throw new ParsingException( + uf.source(), + "Unused parameters {} detected when building [{}]", Arrays.toString(extras), - primaryName); + primaryName + ); } try { return builder.build(uf.source(), uf.children(), cfg); @@ -161,9 +178,11 @@ protected static FunctionDefinition def(Class function, Func /** * Build a {@linkplain FunctionDefinition} for a no-argument function. */ - protected static FunctionDefinition def(Class function, - java.util.function.Function ctorRef, - String... names) { + protected static FunctionDefinition def( + Class function, + java.util.function.Function ctorRef, + String... names + ) { FunctionBuilder builder = (source, children, cfg) -> { if (false == children.isEmpty()) { throw new QlIllegalArgumentException("expects no arguments"); @@ -177,9 +196,11 @@ protected static FunctionDefinition def(Class function, * Build a {@linkplain FunctionDefinition} for a unary function. */ @SuppressWarnings("overloads") // These are ambiguous if you aren't using ctor references but we always do - protected static FunctionDefinition def(Class function, - BiFunction ctorRef, - String... names) { + protected static FunctionDefinition def( + Class function, + BiFunction ctorRef, + String... names + ) { FunctionBuilder builder = (source, children, cfg) -> { if (children.size() != 1) { throw new QlIllegalArgumentException("expects exactly one argument"); @@ -194,9 +215,7 @@ protected static FunctionDefinition def(Class function, */ @SuppressWarnings("overloads") // These are ambiguous if you aren't using ctor references but we always do protected FunctionDefinition def(Class function, NaryBuilder ctorRef, String... names) { - FunctionBuilder builder = (source, children, cfg) -> { - return ctorRef.build(source, children); - }; + FunctionBuilder builder = (source, children, cfg) -> { return ctorRef.build(source, children); }; return def(function, builder, names); } @@ -264,9 +283,13 @@ protected static FunctionDefinition def(Class function, } else if (children.size() != 4) { throw new QlIllegalArgumentException("expects exactly four arguments"); } - return ctorRef.build(source, children.get(0), children.get(1), + return ctorRef.build( + source, + children.get(0), + children.get(1), children.size() > 2 ? children.get(2) : null, - children.size() > 3 ? children.get(3) : null); + children.size() > 3 ? children.get(3) : null + ); }; return def(function, builder, names); } @@ -279,25 +302,34 @@ protected interface QuaternaryBuilder { * Build a {@linkplain FunctionDefinition} for a quinary function. */ @SuppressWarnings("overloads") // These are ambiguous if you aren't using ctor references but we always do - protected static FunctionDefinition def(Class function, - QuinaryBuilder ctorRef, - int numOptionalParams, - String... names) { + protected static FunctionDefinition def( + Class function, + QuinaryBuilder ctorRef, + int numOptionalParams, + String... names + ) { FunctionBuilder builder = (source, children, cfg) -> { final int NUM_TOTAL_PARAMS = 5; boolean hasOptionalParams = OptionalArgument.class.isAssignableFrom(function); if (hasOptionalParams && (children.size() > NUM_TOTAL_PARAMS || children.size() < NUM_TOTAL_PARAMS - numOptionalParams)) { - throw new QlIllegalArgumentException("expects between " + NUM_NAMES[NUM_TOTAL_PARAMS - numOptionalParams] - + " and " + NUM_NAMES[NUM_TOTAL_PARAMS] + " arguments"); + throw new QlIllegalArgumentException( + "expects between " + + NUM_NAMES[NUM_TOTAL_PARAMS - numOptionalParams] + + " and " + + NUM_NAMES[NUM_TOTAL_PARAMS] + + " arguments" + ); } else if (hasOptionalParams == false && children.size() != NUM_TOTAL_PARAMS) { throw new QlIllegalArgumentException("expects exactly " + NUM_NAMES[NUM_TOTAL_PARAMS] + " arguments"); } - return ctorRef.build(source, + return ctorRef.build( + source, children.size() > 0 ? children.get(0) : null, children.size() > 1 ? children.get(1) : null, children.size() > 2 ? children.get(2) : null, children.size() > 3 ? children.get(3) : null, - children.size() > 4 ? children.get(4) : null); + children.size() > 4 ? children.get(4) : null + ); }; return def(function, builder, names); } @@ -349,9 +381,11 @@ protected interface ConfigurationAwareBuilder { * Build a {@linkplain FunctionDefinition} for a one-argument function that is configuration aware. */ @SuppressWarnings("overloads") - protected static FunctionDefinition def(Class function, - UnaryConfigurationAwareBuilder ctorRef, - String... names) { + protected static FunctionDefinition def( + Class function, + UnaryConfigurationAwareBuilder ctorRef, + String... names + ) { FunctionBuilder builder = (source, children, cfg) -> { if (children.size() > 1) { throw new QlIllegalArgumentException("expects exactly one argument"); @@ -370,9 +404,11 @@ protected interface UnaryConfigurationAwareBuilder { * Build a {@linkplain FunctionDefinition} for a binary function that is configuration aware. */ @SuppressWarnings("overloads") // These are ambiguous if you aren't using ctor references but we always do - protected static FunctionDefinition def(Class function, - BinaryConfigurationAwareBuilder ctorRef, - String... names) { + protected static FunctionDefinition def( + Class function, + BinaryConfigurationAwareBuilder ctorRef, + String... names + ) { FunctionBuilder builder = (source, children, cfg) -> { if (children.size() != 2) { throw new QlIllegalArgumentException("expects exactly two arguments"); diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/FunctionResolutionStrategy.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/FunctionResolutionStrategy.java index 8c39b6282ba1b..5d9ad026b98ec 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/FunctionResolutionStrategy.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/FunctionResolutionStrategy.java @@ -9,7 +9,6 @@ import org.elasticsearch.xpack.ql.session.Configuration; - /** * Strategy indicating the type of resolution to apply for resolving the actual function definition in a pluggable way. */ diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/UnresolvedFunction.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/UnresolvedFunction.java index 0a6515bd404f2..d6ae3ceb251e3 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/UnresolvedFunction.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/UnresolvedFunction.java @@ -45,8 +45,14 @@ public UnresolvedFunction(Source source, String name, FunctionResolutionStrategy * * @see #withMessage(String) */ - UnresolvedFunction(Source source, String name, FunctionResolutionStrategy resolutionStrategy, List children, - boolean analyzed, String unresolvedMessage) { + UnresolvedFunction( + Source source, + String name, + FunctionResolutionStrategy resolutionStrategy, + List children, + boolean analyzed, + String unresolvedMessage + ) { super(source, children); this.name = name; this.resolution = resolutionStrategy; @@ -56,8 +62,7 @@ public UnresolvedFunction(Source source, String name, FunctionResolutionStrategy @Override protected NodeInfo info() { - return NodeInfo.create(this, UnresolvedFunction::new, - name, resolution, children(), analyzed, unresolvedMsg); + return NodeInfo.create(this, UnresolvedFunction::new, name, resolution, children(), analyzed, unresolvedMsg); } @Override diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/aggregate/AggregateFunction.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/aggregate/AggregateFunction.java index 8f59e859b451c..80ccf4b964622 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/aggregate/AggregateFunction.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/aggregate/AggregateFunction.java @@ -76,8 +76,7 @@ public int hashCode() { public boolean equals(Object obj) { if (super.equals(obj)) { AggregateFunction other = (AggregateFunction) obj; - return Objects.equals(other.field(), field()) - && Objects.equals(other.parameters(), parameters()); + return Objects.equals(other.field(), field()) && Objects.equals(other.parameters(), parameters()); } return false; } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/aggregate/InnerAggregate.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/aggregate/InnerAggregate.java index 40d55df153024..20ce60fb120da 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/aggregate/InnerAggregate.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/aggregate/InnerAggregate.java @@ -86,9 +86,7 @@ public int hashCode() { public boolean equals(Object obj) { if (super.equals(obj)) { InnerAggregate other = (InnerAggregate) obj; - return Objects.equals(inner, other.inner) - && Objects.equals(outer, other.outer) - && Objects.equals(innerKey, other.innerKey); + return Objects.equals(inner, other.inner) && Objects.equals(outer, other.outer) && Objects.equals(innerKey, other.innerKey); } return false; } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/grouping/GroupingFunction.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/grouping/GroupingFunction.java index d78a77439be6f..019ab0253a83d 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/grouping/GroupingFunction.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/grouping/GroupingFunction.java @@ -64,8 +64,7 @@ public boolean equals(Object obj) { return false; } GroupingFunction other = (GroupingFunction) obj; - return Objects.equals(other.field(), field()) - && Objects.equals(other.parameters(), parameters()); + return Objects.equals(other.field(), field()) && Objects.equals(other.parameters(), parameters()); } @Override diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/scalar/ScalarFunction.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/scalar/ScalarFunction.java index 86d9d899fc68d..ecfe1718a9696 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/scalar/ScalarFunction.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/scalar/ScalarFunction.java @@ -6,10 +6,6 @@ */ package org.elasticsearch.xpack.ql.expression.function.scalar; -import java.time.OffsetTime; -import java.time.ZonedDateTime; -import java.util.List; - import org.elasticsearch.xpack.ql.QlIllegalArgumentException; import org.elasticsearch.xpack.ql.expression.Expression; import org.elasticsearch.xpack.ql.expression.FieldAttribute; @@ -23,6 +19,10 @@ import org.elasticsearch.xpack.ql.type.DataType; import org.elasticsearch.xpack.ql.util.DateUtils; +import java.time.OffsetTime; +import java.time.ZonedDateTime; +import java.util.List; + import static java.util.Collections.emptyList; import static org.elasticsearch.xpack.ql.expression.gen.script.ParamsBuilder.paramsBuilder; import static org.elasticsearch.xpack.ql.expression.gen.script.Scripts.PARAM; @@ -82,14 +82,20 @@ protected ScriptTemplate scriptWithFoldable(Expression foldable) { // wrap intervals with dedicated methods for serialization if (fold instanceof ZonedDateTime) { ZonedDateTime zdt = (ZonedDateTime) fold; - return new ScriptTemplate(processScript("{sql}.asDateTime({})"), paramsBuilder().variable(DateUtils.toString(zdt)).build(), - dataType()); + return new ScriptTemplate( + processScript("{sql}.asDateTime({})"), + paramsBuilder().variable(DateUtils.toString(zdt)).build(), + dataType() + ); } if (fold instanceof IntervalScripting) { IntervalScripting is = (IntervalScripting) fold; - return new ScriptTemplate(processScript(is.script()), paramsBuilder().variable(is.value()).variable(is.typeName()).build(), - dataType()); + return new ScriptTemplate( + processScript(is.script()), + paramsBuilder().variable(is.value()).variable(is.typeName()).build(), + dataType() + ); } if (fold instanceof OffsetTime) { @@ -101,16 +107,12 @@ protected ScriptTemplate scriptWithFoldable(Expression foldable) { return new ScriptTemplate(processScript("{sql}.stWktToSql({})"), paramsBuilder().variable(fold.toString()).build(), dataType()); } - return new ScriptTemplate(processScript("{}"), - paramsBuilder().variable(fold).build(), - dataType()); + return new ScriptTemplate(processScript("{}"), paramsBuilder().variable(fold).build(), dataType()); } protected ScriptTemplate scriptWithScalar(ScalarFunction scalar) { ScriptTemplate nested = scalar.asScript(); - return new ScriptTemplate(processScript(nested.template()), - paramsBuilder().script(nested.params()).build(), - dataType()); + return new ScriptTemplate(processScript(nested.template()), paramsBuilder().script(nested.params()).build(), dataType()); } protected ScriptTemplate scriptWithAggregate(AggregateFunction aggregate) { @@ -120,7 +122,7 @@ protected ScriptTemplate scriptWithAggregate(AggregateFunction aggregate) { DataType nullSafeCastDataType = null; DataType dataType = aggregate.dataType(); if (dataType.name().equals("DATE") || dataType == DATETIME || - // Aggregations on date_nanos are returned as string + // Aggregations on date_nanos are returned as string aggregate.field().dataType() == DATETIME) { template = "{sql}.asDateTime({})"; @@ -147,15 +149,11 @@ protected ScriptTemplate scriptWithAggregate(AggregateFunction aggregate) { // that currently results in a script being generated protected ScriptTemplate scriptWithGrouping(GroupingFunction grouping) { String template = PARAM; - return new ScriptTemplate(processScript(template), - paramsBuilder().grouping(grouping).build(), - dataType()); + return new ScriptTemplate(processScript(template), paramsBuilder().grouping(grouping).build(), dataType()); } protected ScriptTemplate scriptWithField(FieldAttribute field) { - return new ScriptTemplate(processScript(Scripts.DOC_VALUE), - paramsBuilder().variable(field.name()).build(), - dataType()); + return new ScriptTemplate(processScript(Scripts.DOC_VALUE), paramsBuilder().variable(field.name()).build(), dataType()); } protected String processScript(String script) { diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/scalar/string/BinaryComparisonCaseInsensitiveFunction.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/scalar/string/BinaryComparisonCaseInsensitiveFunction.java index c7bf5929b6119..d9c61762d20e9 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/scalar/string/BinaryComparisonCaseInsensitiveFunction.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/scalar/string/BinaryComparisonCaseInsensitiveFunction.java @@ -61,7 +61,6 @@ public DataType dataType() { return DataTypes.BOOLEAN; } - @Override public boolean foldable() { return left.foldable() && right.foldable(); @@ -76,17 +75,19 @@ public ScriptTemplate asScript() { } protected ScriptTemplate asScriptFrom(ScriptTemplate leftScript, ScriptTemplate rightScript) { - return new ScriptTemplate(format(Locale.ROOT, formatTemplate("%s.%s(%s,%s,%s)"), - Scripts.classPackageAsPrefix(getClass()), - scriptMethodName(), - leftScript.template(), - rightScript.template(), - "{}"), - paramsBuilder() - .script(leftScript.params()) - .script(rightScript.params()) - .variable(isCaseInsensitive()) - .build(), dataType()); + return new ScriptTemplate( + format( + Locale.ROOT, + formatTemplate("%s.%s(%s,%s,%s)"), + Scripts.classPackageAsPrefix(getClass()), + scriptMethodName(), + leftScript.template(), + rightScript.template(), + "{}" + ), + paramsBuilder().script(leftScript.params()).script(rightScript.params()).variable(isCaseInsensitive()).build(), + dataType() + ); } protected String scriptMethodName() { diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/scalar/string/CaseInsensitiveScalarFunction.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/scalar/string/CaseInsensitiveScalarFunction.java index a27c741270586..78e3ad5413642 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/scalar/string/CaseInsensitiveScalarFunction.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/scalar/string/CaseInsensitiveScalarFunction.java @@ -34,9 +34,11 @@ public boolean isCaseInsensitive() { @Override public ScriptTemplate scriptWithField(FieldAttribute field) { - return new ScriptTemplate(processScript(Scripts.DOC_VALUE), + return new ScriptTemplate( + processScript(Scripts.DOC_VALUE), paramsBuilder().variable(field.exactAttribute().name()).build(), - dataType()); + dataType() + ); } @Override diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/scalar/string/StartsWith.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/scalar/string/StartsWith.java index aeeb86d4a15af..72ae4a3f780d2 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/scalar/string/StartsWith.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/scalar/string/StartsWith.java @@ -90,18 +90,18 @@ protected ScriptTemplate asScriptFrom(ScriptTemplate fieldScript, ScriptTemplate ParamsBuilder params = paramsBuilder(); String template = formatTemplate("{ql}.startsWith(" + fieldScript.template() + ", " + patternScript.template() + ", {})"); - params.script(fieldScript.params()) - .script(patternScript.params()) - .variable(isCaseInsensitive()); + params.script(fieldScript.params()).script(patternScript.params()).variable(isCaseInsensitive()); return new ScriptTemplate(template, params.build(), dataType()); } @Override public ScriptTemplate scriptWithField(FieldAttribute field) { - return new ScriptTemplate(processScript(Scripts.DOC_VALUE), - paramsBuilder().variable(field.exactAttribute().name()).build(), - dataType()); + return new ScriptTemplate( + processScript(Scripts.DOC_VALUE), + paramsBuilder().variable(field.exactAttribute().name()).build(), + dataType() + ); } @Override diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/scalar/string/StartsWithFunctionPipe.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/scalar/string/StartsWithFunctionPipe.java index 9c0fb672ada33..278f59f8a4d2e 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/scalar/string/StartsWithFunctionPipe.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/scalar/string/StartsWithFunctionPipe.java @@ -103,7 +103,7 @@ public boolean equals(Object obj) { StartsWithFunctionPipe other = (StartsWithFunctionPipe) obj; return Objects.equals(input, other.input) - && Objects.equals(pattern, other.pattern) - && Objects.equals(isCaseSensitive, other.isCaseSensitive); + && Objects.equals(pattern, other.pattern) + && Objects.equals(isCaseSensitive, other.isCaseSensitive); } } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/scalar/string/StartsWithFunctionProcessor.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/scalar/string/StartsWithFunctionProcessor.java index 7fbba05cecec7..a94914334ab90 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/scalar/string/StartsWithFunctionProcessor.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/scalar/string/StartsWithFunctionProcessor.java @@ -101,7 +101,6 @@ public int hashCode() { return Objects.hash(source(), pattern(), isCaseInsensitive()); } - @Override public String getWriteableName() { return NAME; diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/scalar/whitelist/InternalQlScriptUtils.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/scalar/whitelist/InternalQlScriptUtils.java index f8d1a0e29c8e9..c975c876c9b12 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/scalar/whitelist/InternalQlScriptUtils.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/scalar/whitelist/InternalQlScriptUtils.java @@ -58,7 +58,6 @@ public static Number nullSafeCastNumeric(Number number, String typeName) { return number == null || Double.isNaN(number.doubleValue()) ? null : (Number) convert(number, fromTypeName(typeName)); } - // // Operators // diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/gen/pipeline/AggPathInput.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/gen/pipeline/AggPathInput.java index 77a4cfeaac83a..e2fe2bbb8f9a1 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/gen/pipeline/AggPathInput.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/gen/pipeline/AggPathInput.java @@ -69,7 +69,6 @@ public boolean equals(Object obj) { } AggPathInput other = (AggPathInput) obj; - return Objects.equals(context(), other.context()) - && Objects.equals(action, other.action); + return Objects.equals(context(), other.context()) && Objects.equals(action, other.action); } } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/gen/pipeline/BinaryPipe.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/gen/pipeline/BinaryPipe.java index 28bd3252dde8e..79d9371e19a90 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/gen/pipeline/BinaryPipe.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/gen/pipeline/BinaryPipe.java @@ -85,7 +85,6 @@ public boolean equals(Object obj) { } BinaryPipe other = (BinaryPipe) obj; - return Objects.equals(left(), other.left()) - && Objects.equals(right(), other.right()); + return Objects.equals(left(), other.left()) && Objects.equals(right(), other.right()); } } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/gen/pipeline/LeafInput.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/gen/pipeline/LeafInput.java index 697a72591ee1d..1cc878ad494ed 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/gen/pipeline/LeafInput.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/gen/pipeline/LeafInput.java @@ -53,7 +53,6 @@ public boolean equals(Object obj) { } LeafInput other = (LeafInput) obj; - return Objects.equals(context(), other.context()) - && Objects.equals(expression(), other.expression()); + return Objects.equals(context(), other.context()) && Objects.equals(expression(), other.expression()); } } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/gen/pipeline/UnaryPipe.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/gen/pipeline/UnaryPipe.java index 7fbb9688ad18d..5857e24f1eea6 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/gen/pipeline/UnaryPipe.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/gen/pipeline/UnaryPipe.java @@ -93,7 +93,7 @@ public boolean equals(Object obj) { UnaryPipe other = (UnaryPipe) obj; return Objects.equals(action, other.action) - && Objects.equals(child, other.child) - && Objects.equals(expression(), other.expression()); + && Objects.equals(child, other.child) + && Objects.equals(expression(), other.expression()); } } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/gen/processor/BinaryProcessor.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/gen/processor/BinaryProcessor.java index 2e664ba9090d3..1ab6c01c4fbb3 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/gen/processor/BinaryProcessor.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/gen/processor/BinaryProcessor.java @@ -55,7 +55,7 @@ public Object process(Object input) { * Checks the parameter (typically for its type) if the value is not null. */ protected void checkParameter(Object param) { - //no-op + // no-op } protected Processor left() { diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/gen/processor/FunctionalBinaryProcessor.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/gen/processor/FunctionalBinaryProcessor.java index e4763105d3241..08978355e7cdb 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/gen/processor/FunctionalBinaryProcessor.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/gen/processor/FunctionalBinaryProcessor.java @@ -57,7 +57,7 @@ public boolean equals(Object obj) { FunctionalBinaryProcessor other = (FunctionalBinaryProcessor) obj; return Objects.equals(function(), other.function()) - && Objects.equals(left(), other.left()) - && Objects.equals(right(), other.right()); + && Objects.equals(left(), other.left()) + && Objects.equals(right(), other.right()); } } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/gen/processor/FunctionalEnumBinaryProcessor.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/gen/processor/FunctionalEnumBinaryProcessor.java index 02a12191c0380..49d5246fc1334 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/gen/processor/FunctionalEnumBinaryProcessor.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/gen/processor/FunctionalEnumBinaryProcessor.java @@ -16,8 +16,11 @@ /** * Base class for definition binary processors based on functions (for applying) defined as enums (for serialization purposes). */ -public abstract class FunctionalEnumBinaryProcessor & BiFunction> - extends FunctionalBinaryProcessor { +public abstract class FunctionalEnumBinaryProcessor & BiFunction> extends FunctionalBinaryProcessor< + T, + U, + R, + F> { protected FunctionalEnumBinaryProcessor(Processor left, Processor right, F function) { super(left, right, function); diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/gen/script/Param.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/gen/script/Param.java index 0900c72cb8a9b..54b6f9b35c245 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/gen/script/Param.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/gen/script/Param.java @@ -28,7 +28,6 @@ public String toString() { return format(null, "{{}={}}", prefix(), value); } - @Override public int hashCode() { if (this.value == null) { @@ -39,12 +38,12 @@ public int hashCode() { @Override public boolean equals(Object obj) { - if ((obj instanceof Param) == false) { + if ((obj instanceof Param) == false) { return false; } if (this.value == null) { - return ((Param)obj).value == null; + return ((Param) obj).value == null; } - return this.value.equals(((Param)obj).value); + return this.value.equals(((Param) obj).value); } } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/gen/script/Params.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/gen/script/Params.java index 5d646005a3d49..8cf96f90de95d 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/gen/script/Params.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/gen/script/Params.java @@ -103,17 +103,13 @@ private static List> flatten(List> params) { for (Param p : params) { if (p instanceof Script) { flatten.addAll(flatten(((Script) p).value().params)); - } - else if (p instanceof Agg) { + } else if (p instanceof Agg) { flatten.add(p); - } - else if (p instanceof Grouping) { + } else if (p instanceof Grouping) { flatten.add(p); - } - else if (p instanceof Var) { + } else if (p instanceof Var) { flatten.add(p); - } - else { + } else { throw new QlIllegalArgumentException("Unsupported field {}", p); } } @@ -133,9 +129,9 @@ public int hashCode() { @Override public boolean equals(Object obj) { - if ((obj instanceof Params) == false) { + if ((obj instanceof Params) == false) { return false; } - return this.params.equals(((Params)obj).params); + return this.params.equals(((Params) obj).params); } } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/gen/script/ScriptTemplate.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/gen/script/ScriptTemplate.java index d3559526379cb..e017f9189ff89 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/gen/script/ScriptTemplate.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/gen/script/ScriptTemplate.java @@ -80,8 +80,8 @@ public boolean equals(Object obj) { ScriptTemplate other = (ScriptTemplate) obj; return Objects.equals(template, other.template) - && Objects.equals(params, other.params) - && Objects.equals(outputType, other.outputType); + && Objects.equals(params, other.params) + && Objects.equals(outputType, other.outputType); } @Override diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/gen/script/Scripts.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/gen/script/Scripts.java index a80386491140c..fb8664a733db5 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/gen/script/Scripts.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/gen/script/Scripts.java @@ -42,16 +42,17 @@ public final class Scripts { private static final int PKG_LENGTH = "org.elasticsearch.xpack.".length(); - private Scripts() { - } - - static final Map FORMATTING_PATTERNS = unmodifiableMap(Stream.of( - new SimpleEntry<>(DOC_VALUE, QL_SCRIPTS + ".docValue(doc,{})"), - new SimpleEntry<>(QL_SCRIPTS, INTERNAL_QL_SCRIPT_UTILS), - new SimpleEntry<>(EQL_SCRIPTS, INTERNAL_EQL_SCRIPT_UTILS), - new SimpleEntry<>(SQL_SCRIPTS, INTERNAL_SQL_SCRIPT_UTILS), - new SimpleEntry<>(PARAM, "params.%s")) - .collect(toMap(e -> Pattern.compile(e.getKey(), Pattern.LITERAL), Map.Entry::getValue, (a, b) -> a, LinkedHashMap::new))); + private Scripts() {} + + static final Map FORMATTING_PATTERNS = unmodifiableMap( + Stream.of( + new SimpleEntry<>(DOC_VALUE, QL_SCRIPTS + ".docValue(doc,{})"), + new SimpleEntry<>(QL_SCRIPTS, INTERNAL_QL_SCRIPT_UTILS), + new SimpleEntry<>(EQL_SCRIPTS, INTERNAL_EQL_SCRIPT_UTILS), + new SimpleEntry<>(SQL_SCRIPTS, INTERNAL_SQL_SCRIPT_UTILS), + new SimpleEntry<>(PARAM, "params.%s") + ).collect(toMap(e -> Pattern.compile(e.getKey(), Pattern.LITERAL), Map.Entry::getValue, (a, b) -> a, LinkedHashMap::new)) + ); static final Pattern qlDocValuePattern = Pattern.compile(DOC_VALUE_PARAMS_REGEX); /** @@ -71,18 +72,20 @@ public static String formatTemplate(String template) { } public static ScriptTemplate nullSafeFilter(ScriptTemplate script) { - return new ScriptTemplate(formatTemplate( - format(Locale.ROOT, "{ql}.nullSafeFilter(%s)", script.template())), - script.params(), - DataTypes.BOOLEAN); + return new ScriptTemplate( + formatTemplate(format(Locale.ROOT, "{ql}.nullSafeFilter(%s)", script.template())), + script.params(), + DataTypes.BOOLEAN + ); } public static ScriptTemplate nullSafeSort(ScriptTemplate script) { String methodName = script.outputType().isNumeric() ? "nullSafeSortNumeric" : "nullSafeSortString"; - return new ScriptTemplate(formatTemplate( - format(Locale.ROOT, "{ql}.%s(%s)", methodName, script.template())), - script.params(), - script.outputType()); + return new ScriptTemplate( + formatTemplate(format(Locale.ROOT, "{ql}.%s(%s)", methodName, script.template())), + script.params(), + script.outputType() + ); } public static ScriptTemplate and(ScriptTemplate left, ScriptTemplate right) { @@ -93,18 +96,25 @@ public static ScriptTemplate or(ScriptTemplate left, ScriptTemplate right) { return binaryMethod("{ql}", "or", left, right, DataTypes.BOOLEAN); } - public static ScriptTemplate binaryMethod(String prefix, String methodName, ScriptTemplate leftScript, ScriptTemplate rightScript, - DataType dataType) { - return new ScriptTemplate(format(Locale.ROOT, formatTemplate("%s.%s(%s,%s)"), - formatTemplate(prefix), - methodName, - leftScript.template(), - rightScript.template()), - paramsBuilder() - .script(leftScript.params()) - .script(rightScript.params()) - .build(), - dataType); + public static ScriptTemplate binaryMethod( + String prefix, + String methodName, + ScriptTemplate leftScript, + ScriptTemplate rightScript, + DataType dataType + ) { + return new ScriptTemplate( + format( + Locale.ROOT, + formatTemplate("%s.%s(%s,%s)"), + formatTemplate(prefix), + methodName, + leftScript.template(), + rightScript.template() + ), + paramsBuilder().script(leftScript.params()).script(rightScript.params()).build(), + dataType + ); } public static String classPackageAsPrefix(Class function) { @@ -119,7 +129,7 @@ public static String classPackageAsPrefix(Class function) { * Each variable is then used in a {@code java.util.function.Predicate} to iterate over the doc_values in a Painless script. * Multiple .docValue(doc,params.%s) calls for the same field will use multiple .docValue calls, meaning * a different value of the field will be used for each usage in the script. - * + * * For example, a query of the form fieldA - fieldB > 0 that gets translated into the following Painless script * {@code InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(InternalQlScriptUtils.sub( * InternalQlScriptUtils.docValue(doc,params.v0),InternalQlScriptUtils.docValue(doc,params.v1)),params.v2))} @@ -146,7 +156,7 @@ private static ScriptTemplate docValuesRewrite(ScriptTemplate script, boolean us // This method will use only one variable for one docValue call if ("InternalQlScriptUtils.docValue(doc,params.%s)".equals(token)) { Object fieldName = params.get("v" + index); - + if (useSameValueInScript) { // if the field is already in our list, don't add it one more time if (fieldVars.contains(fieldName) == false) { @@ -176,7 +186,7 @@ private static ScriptTemplate docValuesRewrite(ScriptTemplate script, boolean us } // iterate over the fields in reverse order and add a multiValueDocValues call for each - for(int i = fieldVars.size() - 1; i >= 0; i--) { + for (int i = fieldVars.size() - 1; i >= 0; i--) { newTemplate.insert(0, "InternalEqlScriptUtils.multiValueDocValues(doc,params.%s,X" + i + " -> "); newTemplate.append(")"); } @@ -203,7 +213,7 @@ static String[] splitWithMatches(String input, Pattern pattern) { ArrayList matchList = new ArrayList<>(); Matcher m = pattern.matcher(input); - while(m.find()) { + while (m.find()) { if (index != m.start()) { matchList.add(input.subSequence(index, m.start()).toString()); // add the segment before the match } @@ -215,7 +225,7 @@ static String[] splitWithMatches(String input, Pattern pattern) { // if no match was found, return this if (index == 0) { - return new String[] {input}; + return new String[] { input }; } // add remaining segment and avoid an empty element in matches list diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/predicate/BinaryPredicate.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/predicate/BinaryPredicate.java index cf3cf537918eb..0377266d8597a 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/predicate/BinaryPredicate.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/predicate/BinaryPredicate.java @@ -57,9 +57,7 @@ public boolean equals(Object obj) { BinaryPredicate other = (BinaryPredicate) obj; - return Objects.equals(symbol(), other.symbol()) - && Objects.equals(left(), other.left()) - && Objects.equals(right(), other.right()); + return Objects.equals(symbol(), other.symbol()) && Objects.equals(left(), other.left()) && Objects.equals(right(), other.right()); } public String symbol() { diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/predicate/Range.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/predicate/Range.java index ae401170afc89..a9512227d75db 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/predicate/Range.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/predicate/Range.java @@ -127,32 +127,44 @@ public ScriptTemplate asScript() { ScriptTemplate lowerScript = asScript(lower); ScriptTemplate upperScript = asScript(upper); - - String template = formatTemplate(format(Locale.ROOT, "{ql}.and({ql}.%s(%s, %s), {ql}.%s(%s, %s))", - includeLower() ? "gte" : "gt", - valueScript.template(), - lowerScript.template(), - includeUpper() ? "lte" : "lt", - valueScript.template(), - upperScript.template() - )); - - Params params = paramsBuilder() - .script(valueScript.params()) - .script(lowerScript.params()) - .script(valueScript.params()) - .script(upperScript.params()) - .build(); + String template = formatTemplate( + format( + Locale.ROOT, + "{ql}.and({ql}.%s(%s, %s), {ql}.%s(%s, %s))", + includeLower() ? "gte" : "gt", + valueScript.template(), + lowerScript.template(), + includeUpper() ? "lte" : "lt", + valueScript.template(), + upperScript.template() + ) + ); + + Params params = paramsBuilder().script(valueScript.params()) + .script(lowerScript.params()) + .script(valueScript.params()) + .script(upperScript.params()) + .build(); return new ScriptTemplate(template, params, DataTypes.BOOLEAN); } @Override protected Pipe makePipe() { - BinaryComparisonPipe lowerPipe = new BinaryComparisonPipe(source(), this, Expressions.pipe(value()), Expressions.pipe(lower()), - includeLower() ? BinaryComparisonOperation.GTE : BinaryComparisonOperation.GT); - BinaryComparisonPipe upperPipe = new BinaryComparisonPipe(source(), this, Expressions.pipe(value()), Expressions.pipe(upper()), - includeUpper() ? BinaryComparisonOperation.LTE : BinaryComparisonOperation.LT); + BinaryComparisonPipe lowerPipe = new BinaryComparisonPipe( + source(), + this, + Expressions.pipe(value()), + Expressions.pipe(lower()), + includeLower() ? BinaryComparisonOperation.GTE : BinaryComparisonOperation.GT + ); + BinaryComparisonPipe upperPipe = new BinaryComparisonPipe( + source(), + this, + Expressions.pipe(value()), + Expressions.pipe(upper()), + includeUpper() ? BinaryComparisonOperation.LTE : BinaryComparisonOperation.LT + ); BinaryLogicPipe and = new BinaryLogicPipe(source(), this, lowerPipe, upperPipe, BinaryLogicOperation.AND); return and; } @@ -174,10 +186,10 @@ public boolean equals(Object obj) { Range other = (Range) obj; return Objects.equals(includeLower, other.includeLower) - && Objects.equals(includeUpper, other.includeUpper) - && Objects.equals(value, other.value) - && Objects.equals(lower, other.lower) - && Objects.equals(upper, other.upper) - && Objects.equals(zoneId, other.zoneId); + && Objects.equals(includeUpper, other.includeUpper) + && Objects.equals(value, other.value) + && Objects.equals(lower, other.lower) + && Objects.equals(upper, other.upper) + && Objects.equals(zoneId, other.zoneId); } } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/predicate/fulltext/FullTextPredicate.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/predicate/fulltext/FullTextPredicate.java index 7ebde0ac43399..d64f60ac43083 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/predicate/fulltext/FullTextPredicate.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/predicate/fulltext/FullTextPredicate.java @@ -84,7 +84,6 @@ public boolean equals(Object obj) { } FullTextPredicate other = (FullTextPredicate) obj; - return Objects.equals(query, other.query) - && Objects.equals(options, other.options); + return Objects.equals(query, other.query) && Objects.equals(options, other.options); } } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/predicate/fulltext/FullTextUtils.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/predicate/fulltext/FullTextUtils.java index 3fb233ed7cc9e..6610dc2acd428 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/predicate/fulltext/FullTextUtils.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/predicate/fulltext/FullTextUtils.java @@ -62,16 +62,14 @@ static Map parseFields(String fieldString, Source source) { String[] split = splitInTwo(fieldName, "^"); if (split == null) { fields.put(fieldName, defaultBoost); - } - else { + } else { try { fields.put(split[0], Float.parseFloat(split[1])); } catch (NumberFormatException nfe) { throw new ParsingException(source, "Cannot parse boosting for {}", fieldName); } } - } - else { + } else { fields.put(fieldName, defaultBoost); } } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/predicate/operator/arithmetic/UnaryArithmeticProcessor.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/predicate/operator/arithmetic/UnaryArithmeticProcessor.java index e5f9d3ee72b99..6f4edf84f448e 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/predicate/operator/arithmetic/UnaryArithmeticProcessor.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/predicate/operator/arithmetic/UnaryArithmeticProcessor.java @@ -69,7 +69,6 @@ public Object process(Object input) { throw new QlIllegalArgumentException("A number is required; received {}", input); } - @Override public String toString() { return operation.symbol() + super.toString(); diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/predicate/operator/comparison/In.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/predicate/operator/comparison/In.java index aeddf5d518a36..517a031df543f 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/predicate/operator/comparison/In.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/predicate/operator/comparison/In.java @@ -85,8 +85,7 @@ public Nullability nullable() { @Override public boolean foldable() { - return Expressions.foldable(children()) || - (Expressions.foldable(list) && list().stream().allMatch(Expressions::isNull)); + return Expressions.foldable(children()) || (Expressions.foldable(list) && list().stream().allMatch(Expressions::isNull)); } @Override @@ -114,12 +113,10 @@ public ScriptTemplate asScript() { List values = new ArrayList<>(new LinkedHashSet<>(foldAndConvertListOfValues(list, value.dataType()))); return new ScriptTemplate( - formatTemplate(format("{ql}.","in({}, {})", leftScript.template())), - paramsBuilder() - .script(leftScript.params()) - .variable(values) - .build(), - dataType()); + formatTemplate(format("{ql}.", "in({}, {})", leftScript.template())), + paramsBuilder().script(leftScript.params()).variable(values).build(), + dataType() + ); } protected List foldAndConvertListOfValues(List list, DataType dataType) { @@ -148,9 +145,14 @@ protected TypeResolution resolveType() { for (Expression ex : list) { if (ex.foldable() == false) { - return new TypeResolution(format(null, "Comparisons against fields are not (currently) supported; offender [{}] in [{}]", - Expressions.name(ex), - sourceText())); + return new TypeResolution( + format( + null, + "Comparisons against fields are not (currently) supported; offender [{}] in [{}]", + Expressions.name(ex), + sourceText() + ) + ); } } @@ -158,12 +160,17 @@ protected TypeResolution resolveType() { for (int i = 0; i < list.size(); i++) { Expression listValue = list.get(i); if (areCompatible(dt, listValue.dataType()) == false) { - return new TypeResolution(format(null, "{} argument of [{}] must be [{}], found value [{}] type [{}]", - ordinal(i + 1), - sourceText(), - dt.typeName(), - Expressions.name(listValue), - listValue.dataType().typeName())); + return new TypeResolution( + format( + null, + "{} argument of [{}] must be [{}], found value [{}] type [{}]", + ordinal(i + 1), + sourceText(), + dt.typeName(), + Expressions.name(listValue), + listValue.dataType().typeName() + ) + ); } } @@ -185,7 +192,6 @@ public boolean equals(Object obj) { } In other = (In) obj; - return Objects.equals(value, other.value) - && Objects.equals(list, other.list); + return Objects.equals(value, other.value) && Objects.equals(list, other.list); } } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/predicate/regex/LikePattern.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/predicate/regex/LikePattern.java index 28fe8fa43602e..d9c1d74b5e084 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/predicate/regex/LikePattern.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/predicate/regex/LikePattern.java @@ -90,7 +90,6 @@ public boolean equals(Object obj) { } LikePattern other = (LikePattern) obj; - return Objects.equals(pattern, other.pattern) - && escape == other.escape; + return Objects.equals(pattern, other.pattern) && escape == other.escape; } } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/processor/Processors.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/processor/Processors.java index 4918be283cb59..7d83547ea8586 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/processor/Processors.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/processor/Processors.java @@ -53,7 +53,8 @@ public static List getNamedWriteables() { // arithmetic // binary arithmetics are pluggable entries.add( - new Entry(BinaryArithmeticOperation.class, DefaultBinaryArithmeticOperation.NAME, DefaultBinaryArithmeticOperation::read)); + new Entry(BinaryArithmeticOperation.class, DefaultBinaryArithmeticOperation.NAME, DefaultBinaryArithmeticOperation::read) + ); entries.add(new Entry(Processor.class, BinaryArithmeticProcessor.NAME, BinaryArithmeticProcessor::new)); entries.add(new Entry(Processor.class, UnaryArithmeticProcessor.NAME, UnaryArithmeticProcessor::new)); // comparators diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/index/IndexResolution.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/index/IndexResolution.java index 1652e9b97a85a..e4d4c7fc830b0 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/index/IndexResolution.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/index/IndexResolution.java @@ -15,10 +15,12 @@ public static IndexResolution valid(EsIndex index) { Objects.requireNonNull(index, "index must not be null if it was found"); return new IndexResolution(index, null); } + public static IndexResolution invalid(String invalid) { Objects.requireNonNull(invalid, "invalid must not be null to signal that the index is invalid"); return new IndexResolution(null, invalid); } + public static IndexResolution notFound(String name) { Objects.requireNonNull(name, "name must not be null"); return invalid("Unknown index [" + name + "]"); @@ -62,8 +64,7 @@ public boolean equals(Object obj) { return false; } IndexResolution other = (IndexResolution) obj; - return Objects.equals(index, other.index) - && Objects.equals(invalid, other.invalid); + return Objects.equals(index, other.index) && Objects.equals(invalid, other.invalid); } @Override diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/index/IndexResolver.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/index/IndexResolver.java index 17ab016d40d25..d8fdd02153134 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/index/IndexResolver.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/index/IndexResolver.java @@ -149,9 +149,7 @@ public boolean equals(Object obj) { } IndexResolver.IndexInfo other = (IndexResolver.IndexInfo) obj; - return Objects.equals(cluster, other.cluster) - && Objects.equals(name, other.name) - && Objects.equals(type, other.type); + return Objects.equals(cluster, other.cluster) && Objects.equals(name, other.name) && Objects.equals(type, other.type); } } @@ -159,16 +157,22 @@ public boolean equals(Object obj) { public static final String SQL_VIEW = "VIEW"; private static final IndicesOptions INDICES_ONLY_OPTIONS = new IndicesOptions( - EnumSet.of(Option.ALLOW_NO_INDICES, Option.IGNORE_UNAVAILABLE, Option.IGNORE_ALIASES, Option.IGNORE_THROTTLED), - EnumSet.of(WildcardStates.OPEN)); + EnumSet.of(Option.ALLOW_NO_INDICES, Option.IGNORE_UNAVAILABLE, Option.IGNORE_ALIASES, Option.IGNORE_THROTTLED), + EnumSet.of(WildcardStates.OPEN) + ); private static final IndicesOptions FROZEN_INDICES_OPTIONS = new IndicesOptions( - EnumSet.of(Option.ALLOW_NO_INDICES, Option.IGNORE_UNAVAILABLE, Option.IGNORE_ALIASES), EnumSet.of(WildcardStates.OPEN)); + EnumSet.of(Option.ALLOW_NO_INDICES, Option.IGNORE_UNAVAILABLE, Option.IGNORE_ALIASES), + EnumSet.of(WildcardStates.OPEN) + ); public static final IndicesOptions FIELD_CAPS_INDICES_OPTIONS = new IndicesOptions( - EnumSet.of(Option.ALLOW_NO_INDICES, Option.IGNORE_UNAVAILABLE, Option.IGNORE_THROTTLED), EnumSet.of(WildcardStates.OPEN)); + EnumSet.of(Option.ALLOW_NO_INDICES, Option.IGNORE_UNAVAILABLE, Option.IGNORE_THROTTLED), + EnumSet.of(WildcardStates.OPEN) + ); public static final IndicesOptions FIELD_CAPS_FROZEN_INDICES_OPTIONS = new IndicesOptions( - EnumSet.of(Option.ALLOW_NO_INDICES, Option.IGNORE_UNAVAILABLE), EnumSet.of(WildcardStates.OPEN)); - + EnumSet.of(Option.ALLOW_NO_INDICES, Option.IGNORE_UNAVAILABLE), + EnumSet.of(WildcardStates.OPEN) + ); private static final String UNMAPPED = "unmapped"; @@ -197,8 +201,13 @@ public Set remoteClusters() { * Resolves only the names, differentiating between indices and aliases. * This method is required since the other methods rely on mapping which is tied to an index (not an alias). */ - public void resolveNames(String clusterWildcard, String indexWildcard, String javaRegex, EnumSet types, - ActionListener> listener) { + public void resolveNames( + String clusterWildcard, + String indexWildcard, + String javaRegex, + EnumSet types, + ActionListener> listener + ) { // first get aliases (if specified) boolean retrieveAliases = CollectionUtils.isEmpty(types) || types.contains(IndexType.ALIAS); @@ -208,50 +217,59 @@ public void resolveNames(String clusterWildcard, String indexWildcard, String ja String[] indexWildcards = Strings.commaDelimitedListToStringArray(indexWildcard); Set indexInfos = new HashSet<>(); if (retrieveAliases) { - GetAliasesRequest aliasRequest = new GetAliasesRequest() - .local(true) - .aliases(indexWildcards) - .indicesOptions(IndicesOptions.lenientExpandOpen()); + GetAliasesRequest aliasRequest = new GetAliasesRequest().local(true) + .aliases(indexWildcards) + .indicesOptions(IndicesOptions.lenientExpandOpen()); client.admin().indices().getAliases(aliasRequest, wrap(aliases -> { - if (aliases != null) { - for (List aliasList : aliases.getAliases().values()) { - for (AliasMetadata amd : aliasList) { - String alias = amd.alias(); - if (alias != null) { - indexInfos.add(new IndexInfo(clusterName, alias, IndexType.ALIAS)); - } + if (aliases != null) { + for (List aliasList : aliases.getAliases().values()) { + for (AliasMetadata amd : aliasList) { + String alias = amd.alias(); + if (alias != null) { + indexInfos.add(new IndexInfo(clusterName, alias, IndexType.ALIAS)); } } } - resolveIndices(clusterWildcard, indexWildcards, javaRegex, retrieveIndices, retrieveFrozenIndices, indexInfos, - listener); - }, - ex -> { - // with security, two exception can be thrown: - // INFE - if no alias matches - // security exception is the user cannot access aliases - - // in both cases, that is allowed and we continue with the indices request - if (ex instanceof IndexNotFoundException || ex instanceof ElasticsearchSecurityException) { - resolveIndices(clusterWildcard, indexWildcards, javaRegex, retrieveIndices, retrieveFrozenIndices, indexInfos, - listener); - } else { - listener.onFailure(ex); - } - }) - ); + } + resolveIndices(clusterWildcard, indexWildcards, javaRegex, retrieveIndices, retrieveFrozenIndices, indexInfos, listener); + }, ex -> { + // with security, two exception can be thrown: + // INFE - if no alias matches + // security exception is the user cannot access aliases + + // in both cases, that is allowed and we continue with the indices request + if (ex instanceof IndexNotFoundException || ex instanceof ElasticsearchSecurityException) { + resolveIndices( + clusterWildcard, + indexWildcards, + javaRegex, + retrieveIndices, + retrieveFrozenIndices, + indexInfos, + listener + ); + } else { + listener.onFailure(ex); + } + })); } else { resolveIndices(clusterWildcard, indexWildcards, javaRegex, retrieveIndices, retrieveFrozenIndices, indexInfos, listener); } } - private void resolveIndices(String clusterWildcard, String[] indexWildcards, String javaRegex, boolean retrieveIndices, - boolean retrieveFrozenIndices, Set indexInfos, ActionListener> listener) { + private void resolveIndices( + String clusterWildcard, + String[] indexWildcards, + String javaRegex, + boolean retrieveIndices, + boolean retrieveFrozenIndices, + Set indexInfos, + ActionListener> listener + ) { if (retrieveIndices || retrieveFrozenIndices) { if (clusterWildcard == null || simpleMatch(clusterWildcard, clusterName)) { // resolve local indices - GetIndexRequest indexRequest = new GetIndexRequest() - .local(true) + GetIndexRequest indexRequest = new GetIndexRequest().local(true) .indices(indexWildcards) .features(Feature.SETTINGS) .includeDefaults(false) @@ -263,18 +281,17 @@ private void resolveIndices(String clusterWildcard, String[] indexWildcards, Str } client.admin().indices().getIndex(indexRequest, wrap(indices -> { - if (indices != null) { - for (String indexName : indices.getIndices()) { - boolean isFrozen = retrieveFrozenIndices - && indices.getSettings().get(indexName).getAsBoolean("index.frozen", false); - indexInfos.add(new IndexInfo(clusterName, indexName, - isFrozen ? IndexType.FROZEN_INDEX : IndexType.STANDARD_INDEX)); - } + if (indices != null) { + for (String indexName : indices.getIndices()) { + boolean isFrozen = retrieveFrozenIndices + && indices.getSettings().get(indexName).getAsBoolean("index.frozen", false); + indexInfos.add( + new IndexInfo(clusterName, indexName, isFrozen ? IndexType.FROZEN_INDEX : IndexType.STANDARD_INDEX) + ); } - resolveRemoteIndices(clusterWildcard, indexWildcards, javaRegex, retrieveFrozenIndices, indexInfos, listener); - }, - listener::onFailure) - ); + } + resolveRemoteIndices(clusterWildcard, indexWildcards, javaRegex, retrieveFrozenIndices, indexInfos, listener); + }, listener::onFailure)); } else { resolveRemoteIndices(clusterWildcard, indexWildcards, javaRegex, retrieveFrozenIndices, indexInfos, listener); } @@ -283,35 +300,42 @@ private void resolveIndices(String clusterWildcard, String[] indexWildcards, Str } } - private void resolveRemoteIndices(String clusterWildcard, String[] indexWildcards, String javaRegex, boolean retrieveFrozenIndices, - Set indexInfos, ActionListener> listener) { + private void resolveRemoteIndices( + String clusterWildcard, + String[] indexWildcards, + String javaRegex, + boolean retrieveFrozenIndices, + Set indexInfos, + ActionListener> listener + ) { if (hasText(clusterWildcard)) { IndicesOptions indicesOptions = retrieveFrozenIndices ? FIELD_CAPS_FROZEN_INDICES_OPTIONS : FIELD_CAPS_INDICES_OPTIONS; - FieldCapabilitiesRequest fieldRequest = createFieldCapsRequest(qualifyAndJoinIndices(clusterWildcard, indexWildcards), - indicesOptions, emptyMap()); - client.fieldCaps(fieldRequest, wrap(response -> { - String[] indices = response.getIndices(); - if (indices != null) { - for (String indexName : indices) { - // TODO: perform two requests w/ & w/o frozen option to retrieve (by diff) the throttling status? - Tuple splitRef = splitQualifiedIndex(indexName); - // Field caps on "remote:foo" should always return either empty or remote indices. But in case cluster's - // detail is missing, it's going to be a local index. TODO: why would this happen? - String cluster = splitRef.v1() == null ? clusterName : splitRef.v1(); - indexInfos.add(new IndexInfo(cluster, splitRef.v2(), IndexType.STANDARD_INDEX)); - } - } - filterResults(javaRegex, indexInfos, listener); - }, - ex -> { - // see comment in resolveNames() - if (ex instanceof NoSuchRemoteClusterException || ex instanceof ElasticsearchSecurityException) { - filterResults(javaRegex, indexInfos, listener); - } else { - listener.onFailure(ex); - } - }) + FieldCapabilitiesRequest fieldRequest = createFieldCapsRequest( + qualifyAndJoinIndices(clusterWildcard, indexWildcards), + indicesOptions, + emptyMap() ); + client.fieldCaps(fieldRequest, wrap(response -> { + String[] indices = response.getIndices(); + if (indices != null) { + for (String indexName : indices) { + // TODO: perform two requests w/ & w/o frozen option to retrieve (by diff) the throttling status? + Tuple splitRef = splitQualifiedIndex(indexName); + // Field caps on "remote:foo" should always return either empty or remote indices. But in case cluster's + // detail is missing, it's going to be a local index. TODO: why would this happen? + String cluster = splitRef.v1() == null ? clusterName : splitRef.v1(); + indexInfos.add(new IndexInfo(cluster, splitRef.v2(), IndexType.STANDARD_INDEX)); + } + } + filterResults(javaRegex, indexInfos, listener); + }, ex -> { + // see comment in resolveNames() + if (ex instanceof NoSuchRemoteClusterException || ex instanceof ElasticsearchSecurityException) { + filterResults(javaRegex, indexInfos, listener); + } else { + listener.onFailure(ex); + } + })); } else { filterResults(javaRegex, indexInfos, listener); } @@ -334,29 +358,40 @@ private void filterResults(String javaRegex, Set indexInfos, ActionLi /** * Resolves a pattern to one (potentially compound meaning that spawns multiple indices) mapping. */ - public void resolveAsMergedMapping(String indexWildcard, IndicesOptions indicesOptions, Map runtimeMappings, - ActionListener listener) { + public void resolveAsMergedMapping( + String indexWildcard, + IndicesOptions indicesOptions, + Map runtimeMappings, + ActionListener listener + ) { FieldCapabilitiesRequest fieldRequest = createFieldCapsRequest(indexWildcard, indicesOptions, runtimeMappings); - client.fieldCaps(fieldRequest, - ActionListener.wrap( - response -> listener.onResponse(mergedMappings(typeRegistry, indexWildcard, response)), - listener::onFailure)); + client.fieldCaps( + fieldRequest, + ActionListener.wrap(response -> listener.onResponse(mergedMappings(typeRegistry, indexWildcard, response)), listener::onFailure) + ); } /** * Resolves a pattern to one (potentially compound meaning that spawns multiple indices) mapping. */ - public void resolveAsMergedMapping(String indexWildcard, boolean includeFrozen, Map runtimeMappings, - ActionListener listener) { + public void resolveAsMergedMapping( + String indexWildcard, + boolean includeFrozen, + Map runtimeMappings, + ActionListener listener + ) { FieldCapabilitiesRequest fieldRequest = createFieldCapsRequest(indexWildcard, includeFrozen, runtimeMappings); - client.fieldCaps(fieldRequest, - ActionListener.wrap( - response -> listener.onResponse(mergedMappings(typeRegistry, indexWildcard, response)), - listener::onFailure)); + client.fieldCaps( + fieldRequest, + ActionListener.wrap(response -> listener.onResponse(mergedMappings(typeRegistry, indexWildcard, response)), listener::onFailure) + ); } - public static IndexResolution mergedMappings(DataTypeRegistry typeRegistry, String indexPattern, - FieldCapabilitiesResponse fieldCapsResponse) { + public static IndexResolution mergedMappings( + DataTypeRegistry typeRegistry, + String indexPattern, + FieldCapabilitiesResponse fieldCapsResponse + ) { if (fieldCapsResponse.getIndices().length == 0) { return IndexResolution.notFound(indexPattern); @@ -419,19 +454,23 @@ public static IndexResolution mergedMappings(DataTypeRegistry typeRegistry, Stri if (indices.size() > 1) { throw new QlIllegalArgumentException( - "Incorrect merging of mappings (likely due to a bug) - expect at most one but found [{}]", - indices.size()); + "Incorrect merging of mappings (likely due to a bug) - expect at most one but found [{}]", + indices.size() + ); } - final String indexName= fieldCapsResponse.getIndices()[0]; + final String indexName = fieldCapsResponse.getIndices()[0]; return IndexResolution.valid(indices.isEmpty() ? new EsIndex(indexName, emptyMap()) : indices.get(0)); } - private static EsField createField(DataTypeRegistry typeRegistry, String fieldName, - Map> globalCaps, - Map hierarchicalMapping, - Map flattedMapping, - Function field) { + private static EsField createField( + DataTypeRegistry typeRegistry, + String fieldName, + Map> globalCaps, + Map hierarchicalMapping, + Map flattedMapping, + Function field + ) { Map parentProps = hierarchicalMapping; @@ -489,8 +528,14 @@ private static EsField createField(DataTypeRegistry typeRegistry, String fieldNa return esField; } - private static EsField createField(DataTypeRegistry typeRegistry, String fieldName, String typeName, Map props, - boolean isAggregateable, boolean isAlias) { + private static EsField createField( + DataTypeRegistry typeRegistry, + String fieldName, + String typeName, + Map props, + boolean isAggregateable, + boolean isAlias + ) { DataType esType = typeRegistry.fromEs(typeName); if (esType == TEXT) { @@ -512,20 +557,25 @@ private static EsField createField(DataTypeRegistry typeRegistry, String fieldNa return new EsField(fieldName, esType, props, isAggregateable, isAlias); } - private static FieldCapabilitiesRequest createFieldCapsRequest(String index, IndicesOptions indicesOptions, - Map runtimeMappings) { - return new FieldCapabilitiesRequest() - .indices(Strings.commaDelimitedListToStringArray(index)) - .fields("*") - .includeUnmapped(true) - .runtimeFields(runtimeMappings) - //lenient because we throw our own errors looking at the response e.g. if something was not resolved - //also because this way security doesn't throw authorization exceptions but rather honors ignore_unavailable - .indicesOptions(indicesOptions); + private static FieldCapabilitiesRequest createFieldCapsRequest( + String index, + IndicesOptions indicesOptions, + Map runtimeMappings + ) { + return new FieldCapabilitiesRequest().indices(Strings.commaDelimitedListToStringArray(index)) + .fields("*") + .includeUnmapped(true) + .runtimeFields(runtimeMappings) + // lenient because we throw our own errors looking at the response e.g. if something was not resolved + // also because this way security doesn't throw authorization exceptions but rather honors ignore_unavailable + .indicesOptions(indicesOptions); } - private static FieldCapabilitiesRequest createFieldCapsRequest(String index, boolean includeFrozen, - Map runtimeMappings) { + private static FieldCapabilitiesRequest createFieldCapsRequest( + String index, + boolean includeFrozen, + Map runtimeMappings + ) { IndicesOptions indicesOptions = includeFrozen ? FIELD_CAPS_FROZEN_INDICES_OPTIONS : FIELD_CAPS_INDICES_OPTIONS; return createFieldCapsRequest(index, indicesOptions, runtimeMappings); } @@ -533,36 +583,47 @@ private static FieldCapabilitiesRequest createFieldCapsRequest(String index, boo /** * Resolves a pattern to multiple, separate indices. Doesn't perform validation. */ - public void resolveAsSeparateMappings(String indexWildcard, String javaRegex, boolean includeFrozen, - Map runtimeMappings, ActionListener> listener) { + public void resolveAsSeparateMappings( + String indexWildcard, + String javaRegex, + boolean includeFrozen, + Map runtimeMappings, + ActionListener> listener + ) { FieldCapabilitiesRequest fieldRequest = createFieldCapsRequest(indexWildcard, includeFrozen, runtimeMappings); client.fieldCaps(fieldRequest, wrap(response -> { - client.admin().indices().getAliases(createGetAliasesRequest(response, includeFrozen), wrap(aliases -> { - listener.onResponse(separateMappings(typeRegistry, javaRegex, response, aliases.getAliases()));}, - ex -> { - if (ex instanceof IndexNotFoundException || ex instanceof ElasticsearchSecurityException) { - listener.onResponse(separateMappings(typeRegistry, javaRegex, response, null)); - } else { - listener.onFailure(ex); - } - })); - }, - listener::onFailure)); + client.admin() + .indices() + .getAliases( + createGetAliasesRequest(response, includeFrozen), + wrap( + aliases -> { listener.onResponse(separateMappings(typeRegistry, javaRegex, response, aliases.getAliases())); }, + ex -> { + if (ex instanceof IndexNotFoundException || ex instanceof ElasticsearchSecurityException) { + listener.onResponse(separateMappings(typeRegistry, javaRegex, response, null)); + } else { + listener.onFailure(ex); + } + } + ) + ); + }, listener::onFailure)); } private GetAliasesRequest createGetAliasesRequest(FieldCapabilitiesResponse response, boolean includeFrozen) { - return new GetAliasesRequest() - .local(true) - .aliases("*") - .indices(response.getIndices()) - .indicesOptions(includeFrozen ? FIELD_CAPS_FROZEN_INDICES_OPTIONS : FIELD_CAPS_INDICES_OPTIONS); + return new GetAliasesRequest().local(true) + .aliases("*") + .indices(response.getIndices()) + .indicesOptions(includeFrozen ? FIELD_CAPS_FROZEN_INDICES_OPTIONS : FIELD_CAPS_INDICES_OPTIONS); } - public static List separateMappings(DataTypeRegistry typeRegistry, - String javaRegex, - FieldCapabilitiesResponse fieldCaps, - ImmutableOpenMap> aliases) { + public static List separateMappings( + DataTypeRegistry typeRegistry, + String javaRegex, + FieldCapabilitiesResponse fieldCaps, + ImmutableOpenMap> aliases + ) { return buildIndices(typeRegistry, javaRegex, fieldCaps, aliases, Function.identity(), (s, cap) -> null); } @@ -575,13 +636,17 @@ private static class Fields { * Assemble an index-based mapping from the field caps (which is field based) by looking at the indices associated with * each field. */ - private static List buildIndices(DataTypeRegistry typeRegistry, String javaRegex, - FieldCapabilitiesResponse fieldCapsResponse, ImmutableOpenMap> aliases, - Function indexNameProcessor, - BiFunction, InvalidMappedField> validityVerifier) { + private static List buildIndices( + DataTypeRegistry typeRegistry, + String javaRegex, + FieldCapabilitiesResponse fieldCapsResponse, + ImmutableOpenMap> aliases, + Function indexNameProcessor, + BiFunction, InvalidMappedField> validityVerifier + ) { if ((fieldCapsResponse.getIndices() == null || fieldCapsResponse.getIndices().length == 0) - && (aliases == null || aliases.isEmpty())) { + && (aliases == null || aliases.isEmpty())) { return emptyList(); } @@ -602,7 +667,8 @@ private static List buildIndices(DataTypeRegistry typeRegistry, String // sort fields in reverse order to build the field hierarchy Set>> sortedFields = new TreeSet<>( - Collections.reverseOrder(Comparator.comparing(Entry::getKey))); + Collections.reverseOrder(Comparator.comparing(Entry::getKey)) + ); final Map> fieldCaps = fieldCapsResponse.get(); sortedFields.addAll(fieldCaps.entrySet()); @@ -673,8 +739,7 @@ private static List buildIndices(DataTypeRegistry typeRegistry, String if (field == null || (invalidField != null && (field instanceof InvalidMappedField) == false)) { createField = true; } - } - else { + } else { if (field == null && invalidFieldsForAliases.get(index) == null) { createField = true; } @@ -698,10 +763,23 @@ private static List buildIndices(DataTypeRegistry typeRegistry, String } } - createField(typeRegistry, fieldName, fieldCaps, indexFields.hierarchicalMapping, indexFields.flattedMapping, - s -> invalidField != null ? invalidField : - createField(typeRegistry, s, typeCap.getType(), emptyMap(), typeCap.isAggregatable(), - isAliasFieldType.get())); + createField( + typeRegistry, + fieldName, + fieldCaps, + indexFields.hierarchicalMapping, + indexFields.flattedMapping, + s -> invalidField != null + ? invalidField + : createField( + typeRegistry, + s, + typeCap.getType(), + emptyMap(), + typeCap.isAggregatable(), + isAliasFieldType.get() + ) + ); } } } @@ -717,7 +795,6 @@ private static List buildIndices(DataTypeRegistry typeRegistry, String return foundIndices; } - /* * Checks if the field is valid (same type and same capabilities - searchable/aggregatable) across indices belonging to a list * of aliases. @@ -745,8 +822,11 @@ private static List buildIndices(DataTypeRegistry typeRegistry, String * } * } */ - private static Map getInvalidFieldsForAliases(String fieldName, Map types, - ImmutableOpenMap> aliases) { + private static Map getInvalidFieldsForAliases( + String fieldName, + Map types, + ImmutableOpenMap> aliases + ) { if (aliases == null || aliases.isEmpty()) { return emptyMap(); } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/optimizer/OptimizerRules.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/optimizer/OptimizerRules.java index e88b10eb8fcd7..d89802c004810 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/optimizer/OptimizerRules.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/optimizer/OptimizerRules.java @@ -84,7 +84,6 @@ import static org.elasticsearch.xpack.ql.tree.Source.EMPTY; import static org.elasticsearch.xpack.ql.util.CollectionUtils.combine; - public final class OptimizerRules { public static final class ConstantFolding extends OptimizerExpressionRule { @@ -360,44 +359,46 @@ private Expression propagate(And and) { if (otherEq.right().foldable() && DataTypes.isDateTime(otherEq.left().dataType()) == false) { for (BinaryComparison eq : equals) { if (otherEq.left().semanticEquals(eq.left())) { - Integer comp = BinaryComparison.compare(eq.right().fold(), otherEq.right().fold()); - if (comp != null) { - // var cannot be equal to two different values at the same time - if (comp != 0) { - return new Literal(and.source(), Boolean.FALSE, DataTypes.BOOLEAN); - } + Integer comp = BinaryComparison.compare(eq.right().fold(), otherEq.right().fold()); + if (comp != null) { + // var cannot be equal to two different values at the same time + if (comp != 0) { + return new Literal(and.source(), Boolean.FALSE, DataTypes.BOOLEAN); } } } + } equals.add(otherEq); } else { exps.add(otherEq); } - } else if (ex instanceof GreaterThan || ex instanceof GreaterThanOrEqual || - ex instanceof LessThan || ex instanceof LessThanOrEqual) { - BinaryComparison bc = (BinaryComparison) ex; - if (bc.right().foldable()) { - inequalities.add(bc); - } else { - exps.add(ex); - } - } else if (ex instanceof NotEquals) { - NotEquals otherNotEq = (NotEquals) ex; - if (otherNotEq.right().foldable()) { - notEquals.add(otherNotEq); + } else if (ex instanceof GreaterThan + || ex instanceof GreaterThanOrEqual + || ex instanceof LessThan + || ex instanceof LessThanOrEqual) { + BinaryComparison bc = (BinaryComparison) ex; + if (bc.right().foldable()) { + inequalities.add(bc); + } else { + exps.add(ex); + } + } else if (ex instanceof NotEquals) { + NotEquals otherNotEq = (NotEquals) ex; + if (otherNotEq.right().foldable()) { + notEquals.add(otherNotEq); + } else { + exps.add(ex); + } } else { exps.add(ex); } - } else { - exps.add(ex); - } } // check for (BinaryComparison eq : equals) { Object eqValue = eq.right().fold(); - for (Iterator iterator = ranges.iterator(); iterator.hasNext(); ) { + for (Iterator iterator = ranges.iterator(); iterator.hasNext();) { Range range = iterator.next(); if (range.value().semanticEquals(eq.left())) { @@ -405,22 +406,20 @@ private Expression propagate(And and) { if (range.lower().foldable()) { Integer compare = BinaryComparison.compare(range.lower().fold(), eqValue); if (compare != null && ( - // eq outside the lower boundary - compare > 0 || - // eq matches the boundary but should not be included - (compare == 0 && range.includeLower() == false)) - ) { + // eq outside the lower boundary + compare > 0 || + // eq matches the boundary but should not be included + (compare == 0 && range.includeLower() == false))) { return new Literal(and.source(), Boolean.FALSE, DataTypes.BOOLEAN); } } if (range.upper().foldable()) { Integer compare = BinaryComparison.compare(range.upper().fold(), eqValue); if (compare != null && ( - // eq outside the upper boundary - compare < 0 || - // eq matches the boundary but should not be included - (compare == 0 && range.includeUpper() == false)) - ) { + // eq outside the upper boundary + compare < 0 || + // eq matches the boundary but should not be included + (compare == 0 && range.includeUpper() == false))) { return new Literal(and.source(), Boolean.FALSE, DataTypes.BOOLEAN); } } @@ -432,7 +431,7 @@ private Expression propagate(And and) { } // evaluate all NotEquals against the Equal - for (Iterator iter = notEquals.iterator(); iter.hasNext(); ) { + for (Iterator iter = notEquals.iterator(); iter.hasNext();) { NotEquals neq = iter.next(); if (eq.left().semanticEquals(neq.left())) { Integer comp = BinaryComparison.compare(eqValue, neq.right().fold()); @@ -448,7 +447,7 @@ private Expression propagate(And and) { } // evaluate all inequalities against the Equal - for (Iterator iter = inequalities.iterator(); iter.hasNext(); ) { + for (Iterator iter = inequalities.iterator(); iter.hasNext();) { BinaryComparison bc = iter.next(); if (eq.left().semanticEquals(bc.left())) { Integer compare = BinaryComparison.compare(eqValue, bc.right().fold()); @@ -460,7 +459,7 @@ private Expression propagate(And and) { } } else if (bc instanceof GreaterThan || bc instanceof GreaterThanOrEqual) { // a = 2 AND a >/>= ? if ((compare == 0 && bc instanceof GreaterThan) || // a = 2 AND a > 2 - compare < 0) { // a = 2 AND a >/>= 3 + compare < 0) { // a = 2 AND a >/>= 3 return new Literal(and.source(), Boolean.FALSE, DataTypes.BOOLEAN); } } @@ -520,7 +519,7 @@ private Expression propagate(Or or) { boolean updated = false; // has the expression been modified? // evaluate the impact of each Equal over the different types of Expressions - for (Iterator iterEq = equals.iterator(); iterEq.hasNext(); ) { + for (Iterator iterEq = equals.iterator(); iterEq.hasNext();) { Equals eq = iterEq.next(); Object eqValue = eq.right().fold(); boolean removeEquals = false; @@ -546,7 +545,7 @@ private Expression propagate(Or or) { } // Equals OR Range - for (int i = 0; i < ranges.size(); i ++) { // might modify list, so use index loop + for (int i = 0; i < ranges.size(); i++) { // might modify list, so use index loop Range range = ranges.get(i); if (eq.left().semanticEquals(range.value())) { Integer lowerComp = range.lower().foldable() ? BinaryComparison.compare(eqValue, range.lower().fold()) : null; @@ -554,15 +553,35 @@ private Expression propagate(Or or) { if (lowerComp != null && lowerComp == 0) { if (range.includeLower() == false) { // a = 2 OR 2 < a < ? -> 2 <= a < ? - ranges.set(i, new Range(range.source(), range.value(), range.lower(), true, - range.upper(), range.includeUpper(), range.zoneId())); + ranges.set( + i, + new Range( + range.source(), + range.value(), + range.lower(), + true, + range.upper(), + range.includeUpper(), + range.zoneId() + ) + ); } // else : a = 2 OR 2 <= a < ? -> 2 <= a < ? removeEquals = true; // update range with lower equality instead or simply superfluous break; } else if (upperComp != null && upperComp == 0) { if (range.includeUpper() == false) { // a = 2 OR ? < a < 2 -> ? < a <= 2 - ranges.set(i, new Range(range.source(), range.value(), range.lower(), range.includeLower(), - range.upper(), true, range.zoneId())); + ranges.set( + i, + new Range( + range.source(), + range.value(), + range.lower(), + range.includeLower(), + range.upper(), + true, + range.zoneId() + ) + ); } // else : a = 2 OR ? < a <= 2 -> ? < a <= 2 removeEquals = true; // update range with upper equality instead break; @@ -581,7 +600,7 @@ private Expression propagate(Or or) { } // Equals OR Inequality - for (int i = 0; i < inequalities.size(); i ++) { + for (int i = 0; i < inequalities.size(); i++) { BinaryComparison bc = inequalities.get(i); if (eq.left().semanticEquals(bc.left())) { Integer comp = BinaryComparison.compare(eqValue, bc.right().fold()); @@ -592,7 +611,7 @@ private Expression propagate(Or or) { } else if (comp == 0 && bc instanceof GreaterThan) { // a = 2 OR a > 2 -> a >= 2 inequalities.set(i, new GreaterThanOrEqual(bc.source(), bc.left(), bc.right(), bc.zoneId())); } // else (0 < comp || bc instanceof GreaterThanOrEqual) : - // a = 3 OR a > 2 -> a > 2; a = 2 OR a => 2 -> a => 2 + // a = 3 OR a > 2 -> a > 2; a = 2 OR a => 2 -> a => 2 removeEquals = true; // update range with equality instead or simply superfluous break; @@ -705,9 +724,17 @@ private Expression combine(And and) { bcs.remove(j); bcs.remove(i); - ranges.add(new Range(and.source(), main.left(), - main.right(), main instanceof GreaterThanOrEqual, - other.right(), other instanceof LessThanOrEqual, main.zoneId())); + ranges.add( + new Range( + and.source(), + main.left(), + main.right(), + main instanceof GreaterThanOrEqual, + other.right(), + other instanceof LessThanOrEqual, + main.zoneId() + ) + ); changed = true; step = 0; @@ -716,22 +743,29 @@ private Expression combine(And and) { // />= else if ((other instanceof GreaterThan || other instanceof GreaterThanOrEqual) && (main instanceof LessThan || main instanceof LessThanOrEqual)) { - bcs.remove(j); - bcs.remove(i); + bcs.remove(j); + bcs.remove(i); - ranges.add(new Range(and.source(), main.left(), - other.right(), other instanceof GreaterThanOrEqual, - main.right(), main instanceof LessThanOrEqual, main.zoneId())); + ranges.add( + new Range( + and.source(), + main.left(), + other.right(), + other instanceof GreaterThanOrEqual, + main.right(), + main instanceof LessThanOrEqual, + main.zoneId() + ) + ); - changed = true; - step = 0; - break; - } + changed = true; + step = 0; + break; + } } } } - return changed ? Predicates.combineAnd(CollectionUtils.combine(exps, bcs, ranges)) : and; } @@ -800,14 +834,14 @@ private static boolean findExistingRange(Range main, List ranges, boolean // (2 < a < 3) AND (1 < a < 3) -> (2 < a < 3) lower = comp > 0 || // (2 < a < 3) AND (2 <= a < 3) -> (2 < a < 3) - (comp == 0 && main.includeLower() == false && other.includeLower()); + (comp == 0 && main.includeLower() == false && other.includeLower()); } // OR else { // (1 < a < 3) OR (2 < a < 3) -> (1 < a < 3) lower = comp < 0 || // (2 <= a < 3) OR (2 < a < 3) -> (2 <= a < 3) - (comp == 0 && main.includeLower() && other.includeLower() == false) || lowerEq; + (comp == 0 && main.includeLower() && other.includeLower() == false) || lowerEq; } } } @@ -826,14 +860,14 @@ private static boolean findExistingRange(Range main, List ranges, boolean // (1 < a < 2) AND (1 < a < 3) -> (1 < a < 2) upper = comp < 0 || // (1 < a < 2) AND (1 < a <= 2) -> (1 < a < 2) - (comp == 0 && main.includeUpper() == false && other.includeUpper()); + (comp == 0 && main.includeUpper() == false && other.includeUpper()); } // OR else { // (1 < a < 3) OR (1 < a < 2) -> (1 < a < 3) upper = comp > 0 || // (1 < a <= 3) OR (1 < a < 3) -> (2 < a < 3) - (comp == 0 && main.includeUpper() && other.includeUpper() == false) || upperEq; + (comp == 0 && main.includeUpper() && other.includeUpper() == false) || upperEq; } } } @@ -842,13 +876,18 @@ private static boolean findExistingRange(Range main, List ranges, boolean if (conjunctive) { // can tighten range if (lower || upper) { - ranges.set(i, - new Range(main.source(), main.value(), - lower ? main.lower() : other.lower(), - lower ? main.includeLower() : other.includeLower(), - upper ? main.upper() : other.upper(), - upper ? main.includeUpper() : other.includeUpper(), - main.zoneId())); + ranges.set( + i, + new Range( + main.source(), + main.value(), + lower ? main.lower() : other.lower(), + lower ? main.includeLower() : other.includeLower(), + upper ? main.upper() : other.upper(), + upper ? main.includeUpper() : other.includeUpper(), + main.zoneId() + ) + ); } // range was comparable @@ -858,13 +897,18 @@ private static boolean findExistingRange(Range main, List ranges, boolean else { // can loosen range if (lower && upper) { - ranges.set(i, - new Range(main.source(), main.value(), - main.lower(), - main.includeLower(), - main.upper(), - main.includeUpper(), - main.zoneId())); + ranges.set( + i, + new Range( + main.source(), + main.value(), + main.lower(), + main.includeLower(), + main.upper(), + main.includeUpper(), + main.zoneId() + ) + ); return true; } @@ -895,10 +939,18 @@ private boolean findConjunctiveComparisonInRange(BinaryComparison main, List 0 || lowerEq; if (lower) { - ranges.set(i, - new Range(other.source(), other.value(), - main.right(), lowerEq ? false : main instanceof GreaterThanOrEqual, - other.upper(), other.includeUpper(), other.zoneId())); + ranges.set( + i, + new Range( + other.source(), + other.value(), + main.right(), + lowerEq ? false : main instanceof GreaterThanOrEqual, + other.upper(), + other.includeUpper(), + other.zoneId() + ) + ); } // found a match @@ -915,9 +967,18 @@ private boolean findConjunctiveComparisonInRange(BinaryComparison main, List 3 AND a > 2 -> a > 3 - (compare > 0 || - // a > 2 AND a >= 2 -> a > 2 - (compare == 0 && main instanceof GreaterThan && other instanceof GreaterThanOrEqual))) - || - // OR - (conjunctive == false && - // a > 2 OR a > 3 -> a > 2 - (compare < 0 || - // a >= 2 OR a > 2 -> a >= 2 - (compare == 0 && main instanceof GreaterThanOrEqual && other instanceof GreaterThan)))) { + (compare > 0 || + // a > 2 AND a >= 2 -> a > 2 + (compare == 0 && main instanceof GreaterThan && other instanceof GreaterThanOrEqual))) || + // OR + (conjunctive == false && + // a > 2 OR a > 3 -> a > 2 + (compare < 0 || + // a >= 2 OR a > 2 -> a >= 2 + (compare == 0 && main instanceof GreaterThanOrEqual && other instanceof GreaterThan)))) { bcs.remove(i); bcs.add(i, main); } @@ -978,37 +1038,36 @@ private static boolean findExistingComparison(BinaryComparison main, List a < 2 - (compare < 0 || - // a < 2 AND a <= 2 -> a < 2 - (compare == 0 && main instanceof LessThan && other instanceof LessThanOrEqual))) - || - // OR - (conjunctive == false && - // a < 2 OR a < 3 -> a < 3 - (compare > 0 || - // a <= 2 OR a < 2 -> a <= 2 - (compare == 0 && main instanceof LessThanOrEqual && other instanceof LessThan)))) { - bcs.remove(i); - bcs.add(i, main); + else if ((other instanceof LessThan || other instanceof LessThanOrEqual) + && (main instanceof LessThan || main instanceof LessThanOrEqual)) { + + if (main.left().semanticEquals(other.left())) { + Integer compare = BinaryComparison.compare(value, other.right().fold()); + + if (compare != null) { + // AND + if ((conjunctive && + // a < 2 AND a < 3 -> a < 2 + (compare < 0 || + // a < 2 AND a <= 2 -> a < 2 + (compare == 0 && main instanceof LessThan && other instanceof LessThanOrEqual))) || + // OR + (conjunctive == false && + // a < 2 OR a < 3 -> a < 3 + (compare > 0 || + // a <= 2 OR a < 2 -> a <= 2 + (compare == 0 && main instanceof LessThanOrEqual && other instanceof LessThan)))) { + bcs.remove(i); + bcs.add(i, main); - } - // found a match - return true; } - - return false; + // found a match + return true; } + + return false; } + } } return false; @@ -1022,7 +1081,7 @@ private static boolean notEqualsIsRemovableFromConjunction(NotEquals notEquals, // a != 2 AND 3 < a < 5 -> 3 < a < 5; a != 2 AND 0 < a < 1 -> 0 < a < 1 (discard NotEquals) // a != 2 AND 2 <= a < 3 -> 2 < a < 3; a != 3 AND 2 < a <= 3 -> 2 < a < 3 (discard NotEquals, plus update Range) // a != 2 AND 1 < a < 3 -> nop (do nothing) - for (int i = 0; i < ranges.size(); i ++) { + for (int i = 0; i < ranges.size(); i++) { Range range = ranges.get(i); if (notEquals.left().semanticEquals(range.value())) { @@ -1030,19 +1089,39 @@ private static boolean notEqualsIsRemovableFromConjunction(NotEquals notEquals, if (comp != null) { if (comp <= 0) { if (comp == 0 && range.includeLower()) { // a != 2 AND 2 <= a < ? -> 2 < a < ? - ranges.set(i, new Range(range.source(), range.value(), range.lower(), false, range.upper(), - range.includeUpper(), range.zoneId())); + ranges.set( + i, + new Range( + range.source(), + range.value(), + range.lower(), + false, + range.upper(), + range.includeUpper(), + range.zoneId() + ) + ); } // else: !.includeLower() : a != 2 AND 2 < a < 3 -> 2 < a < 3; or: - // else: comp < 0 : a != 2 AND 3 < a < ? -> 3 < a < ? + // else: comp < 0 : a != 2 AND 3 < a < ? -> 3 < a < ? return true; } else { // comp > 0 : a != 4 AND 2 < a < ? : can only remove NotEquals if outside the range comp = range.upper().foldable() ? BinaryComparison.compare(neqVal, range.upper().fold()) : null; if (comp != null && comp >= 0) { if (comp == 0 && range.includeUpper()) { // a != 4 AND 2 < a <= 4 -> 2 < a < 4 - ranges.set(i, new Range(range.source(), range.value(), range.lower(), range.includeLower(), - range.upper(), false, range.zoneId())); + ranges.set( + i, + new Range( + range.source(), + range.value(), + range.lower(), + range.includeLower(), + range.upper(), + false, + range.zoneId() + ) + ); } // else: !.includeUpper() : a != 4 AND 2 < a < 4 -> 2 < a < 4 // else: comp > 0 : a != 4 AND 2 < a < 3 -> 2 < a < 3 @@ -1057,8 +1136,18 @@ private static boolean notEqualsIsRemovableFromConjunction(NotEquals notEquals, comp = range.upper().foldable() ? BinaryComparison.compare(neqVal, range.upper().fold()) : null; if (comp != null && comp >= 0) { if (comp == 0 && range.includeUpper()) { // a != 3 AND ?? < a <= 3 -> ?? < a < 3 - ranges.set(i, new Range(range.source(), range.value(), range.lower(), range.includeLower(), range.upper(), - false, range.zoneId())); + ranges.set( + i, + new Range( + range.source(), + range.value(), + range.lower(), + range.includeLower(), + range.upper(), + false, + range.zoneId() + ) + ); } // else: !.includeUpper() : a != 3 AND ?? < a < 3 -> ?? < a < 3 // else: comp > 0 : a != 3 and ?? < a < 2 -> ?? < a < 2 @@ -1078,7 +1167,7 @@ private static boolean notEqualsIsRemovableFromConjunction(NotEquals notEquals, // a != 2 AND a < 3 -> nop // a != 2 AND a <= 2 -> a < 2 // a != 2 AND a < 1 -> a < 1 - for (int i = 0; i < bcs.size(); i ++) { + for (int i = 0; i < bcs.size(); i++) { BinaryComparison bc = bcs.get(i); if (notEquals.left().semanticEquals(bc.left())) { @@ -1344,8 +1433,8 @@ final boolean isUnsafe(BiFunction typesCompatible) // Only operations on fixed point literals are supported, since optimizing float point operations can also change the // outcome of the filtering: - // x + 1e18 > 1e18::long will yield different results with a field value in [-2^6, 2^6], optimised vs original; - // x * (1 + 1e-15d) > 1 : same with a field value of (1 - 1e-15d) + // x + 1e18 > 1e18::long will yield different results with a field value in [-2^6, 2^6], optimised vs original; + // x * (1 + 1e-15d) > 1 : same with a field value of (1 - 1e-15d) // so consequently, int fields optimisation requiring FP arithmetic isn't possible either: (x - 1e-15) * (1 + 1e-15) > 1. if (opLiteral.dataType().isRational() || bcLiteral.dataType().isRational()) { return true; @@ -1374,10 +1463,10 @@ final Expression apply() { } // operation-specific operations: - // - fast-tracking of simplification unsafety + // - fast-tracking of simplification unsafety abstract boolean isOpUnsafe(); - // - post optimisation adjustments + // - post optimisation adjustments Expression postProcess(BinaryComparison binaryComparison) { return binaryComparison; } @@ -1714,11 +1803,11 @@ protected OptimizerRule(TransformDirection direction) { this.direction = direction; } - @Override public final LogicalPlan apply(LogicalPlan plan) { - return direction == TransformDirection.DOWN ? - plan.transformDown(typeToken(), this::rule) : plan.transformUp(typeToken(), this::rule); + return direction == TransformDirection.DOWN + ? plan.transformDown(typeToken(), this::rule) + : plan.transformUp(typeToken(), this::rule); } @Override @@ -1757,6 +1846,7 @@ public Class expressionToken() { } public enum TransformDirection { - UP, DOWN + UP, + DOWN } } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/Aggregate.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/Aggregate.java index ad0c608b08687..04ce5e2054410 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/Aggregate.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/Aggregate.java @@ -73,7 +73,7 @@ public boolean equals(Object obj) { Aggregate other = (Aggregate) obj; return Objects.equals(groupings, other.groupings) - && Objects.equals(aggregates, other.aggregates) - && Objects.equals(child(), other.child()); + && Objects.equals(aggregates, other.aggregates) + && Objects.equals(child(), other.child()); } } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/BinaryPlan.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/BinaryPlan.java index 24e6bbf8afdc7..9e94d54bdb754 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/BinaryPlan.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/BinaryPlan.java @@ -40,8 +40,7 @@ public boolean equals(Object obj) { BinaryPlan other = (BinaryPlan) obj; - return Objects.equals(left(), other.left()) - && Objects.equals(right(), other.right()); + return Objects.equals(left(), other.left()) && Objects.equals(right(), other.right()); } @Override diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/EsRelation.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/EsRelation.java index d3e9f1a2b160e..b25593a57e00d 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/EsRelation.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/EsRelation.java @@ -95,8 +95,7 @@ public boolean equals(Object obj) { } EsRelation other = (EsRelation) obj; - return Objects.equals(index, other.index) - && frozen == other.frozen; + return Objects.equals(index, other.index) && frozen == other.frozen; } @Override diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/Filter.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/Filter.java index ad1a0deb50125..d9b70a207f363 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/Filter.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/Filter.java @@ -61,8 +61,7 @@ public boolean equals(Object obj) { Filter other = (Filter) obj; - return Objects.equals(condition, other.condition) - && Objects.equals(child(), other.child()); + return Objects.equals(condition, other.condition) && Objects.equals(child(), other.child()); } public Filter with(Expression condition) { diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/Limit.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/Limit.java index 0a12cb78e4f07..8afd2d1a425a3 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/Limit.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/Limit.java @@ -56,7 +56,6 @@ public boolean equals(Object obj) { Limit other = (Limit) obj; - return Objects.equals(limit, other.limit) - && Objects.equals(child(), other.child()); + return Objects.equals(limit, other.limit) && Objects.equals(child(), other.child()); } } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/OrderBy.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/OrderBy.java index 62e7ee940ff2c..85c6d9eeda8ee 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/OrderBy.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/OrderBy.java @@ -58,7 +58,6 @@ public boolean equals(Object obj) { } OrderBy other = (OrderBy) obj; - return Objects.equals(order, other.order) - && Objects.equals(child(), other.child()); + return Objects.equals(order, other.order) && Objects.equals(child(), other.child()); } } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/Project.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/Project.java index 1f250905fda0f..dc63705b05685 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/Project.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/Project.java @@ -74,7 +74,6 @@ public boolean equals(Object obj) { Project other = (Project) obj; - return Objects.equals(projections, other.projections) - && Objects.equals(child(), other.child()); + return Objects.equals(projections, other.projections) && Objects.equals(child(), other.child()); } } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/planner/ExpressionTranslators.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/planner/ExpressionTranslators.java index da902e26d3f8c..1963c8a28e6f7 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/planner/ExpressionTranslators.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/planner/ExpressionTranslators.java @@ -67,7 +67,6 @@ public final class ExpressionTranslators { public static final String DATE_FORMAT = "strict_date_optional_time_nanos"; public static final String TIME_FORMAT = "strict_hour_minute_second_fraction"; - public static final List> QUERY_TRANSLATORS = List.of( new BinaryComparisons(), new Ranges(), @@ -199,9 +198,9 @@ protected Query asQuery(Not not, TranslatorHandler handler) { public static Query doTranslate(Not not, TranslatorHandler handler) { Expression e = not.field(); Query wrappedQuery = handler.asQuery(not.field()); - Query q = wrappedQuery instanceof ScriptQuery ? - new ScriptQuery(not.source(), not.asScript()) : - new NotQuery(not.source(), wrappedQuery); + Query q = wrappedQuery instanceof ScriptQuery + ? new ScriptQuery(not.source(), not.asScript()) + : new NotQuery(not.source(), wrappedQuery); return wrapIfNested(q, e); } @@ -248,10 +247,14 @@ protected Query asQuery(BinaryComparison bc, TranslatorHandler handler) { } public static void checkBinaryComparison(BinaryComparison bc) { - Check.isTrue(bc.right().foldable(), - "Line {}:{}: Comparisons against fields are not (currently) supported; offender [{}] in [{}]", - bc.right().sourceLocation().getLineNumber(), bc.right().sourceLocation().getColumnNumber(), - Expressions.name(bc.right()), bc.symbol()); + Check.isTrue( + bc.right().foldable(), + "Line {}:{}: Comparisons against fields are not (currently) supported; offender [{}] in [{}]", + bc.right().sourceLocation().getLineNumber(), + bc.right().sourceLocation().getColumnNumber(), + Expressions.name(bc.right()), + bc.symbol() + ); } public static Query doTranslate(BinaryComparison bc, TranslatorHandler handler) { @@ -319,8 +322,7 @@ static Query translate(BinaryComparison bc, TranslatorHandler handler) { return query; } - throw new QlIllegalArgumentException("Don't know how to translate binary comparison [{}] in [{}]", bc.right().nodeString(), - bc); + throw new QlIllegalArgumentException("Don't know how to translate binary comparison [{}] in [{}]", bc.right().nodeString(), bc); } } @@ -361,7 +363,15 @@ private static RangeQuery translate(Range r, TranslatorHandler handler) { format = formatter.pattern(); } return new RangeQuery( - r.source(), handler.nameOf(r.value()), lower, r.includeLower(), upper, r.includeUpper(), format, r.zoneId()); + r.source(), + handler.nameOf(r.value()), + lower, + r.includeLower(), + upper, + r.includeUpper(), + format, + r.zoneId() + ); } } @@ -405,8 +415,7 @@ private static Query translate(In in, TranslatorHandler handler) { queries.add(new TermsQuery(in.source(), fieldName, terms)); } - return queries.stream() - .reduce((q1, q2) -> or(in.source(), q1, q2)).get(); + return queries.stream().reduce((q1, q2) -> or(in.source(), q1, q2)).get(); } } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plugin/AbstractTransportQlAsyncGetResultsAction.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plugin/AbstractTransportQlAsyncGetResultsAction.java index a9c14257382d8..1f0a7eff02415 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plugin/AbstractTransportQlAsyncGetResultsAction.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plugin/AbstractTransportQlAsyncGetResultsAction.java @@ -24,9 +24,9 @@ import org.elasticsearch.xpack.core.async.AsyncResultsService; import org.elasticsearch.xpack.core.async.AsyncTaskIndexService; import org.elasticsearch.xpack.core.async.GetAsyncResultRequest; -import org.elasticsearch.xpack.ql.async.AsyncTaskManagementService; import org.elasticsearch.xpack.core.async.StoredAsyncResponse; import org.elasticsearch.xpack.core.async.StoredAsyncTask; +import org.elasticsearch.xpack.ql.async.AsyncTaskManagementService; import static org.elasticsearch.xpack.core.ClientHelper.ASYNC_SEARCH_ORIGIN; @@ -36,20 +36,29 @@ public abstract class AbstractTransportQlAsyncGetResultsAction> resultsService; private final TransportService transportService; - public AbstractTransportQlAsyncGetResultsAction(String actionName, - TransportService transportService, - ActionFilters actionFilters, - ClusterService clusterService, - NamedWriteableRegistry registry, - Client client, - ThreadPool threadPool, - BigArrays bigArrays, - Class asynkTaskClass) { + public AbstractTransportQlAsyncGetResultsAction( + String actionName, + TransportService transportService, + ActionFilters actionFilters, + ClusterService clusterService, + NamedWriteableRegistry registry, + Client client, + ThreadPool threadPool, + BigArrays bigArrays, + Class asynkTaskClass + ) { super(actionName, transportService, actionFilters, GetAsyncResultRequest::new); this.actionName = actionName; this.transportService = transportService; - this.resultsService = createResultsService(transportService, clusterService, registry, client, threadPool, bigArrays, - asynkTaskClass); + this.resultsService = createResultsService( + transportService, + clusterService, + registry, + client, + threadPool, + bigArrays, + asynkTaskClass + ); } AsyncResultsService> createResultsService( @@ -59,32 +68,47 @@ AsyncResultsService> createResultsServi Client client, ThreadPool threadPool, BigArrays bigArrays, - Class asyncTaskClass) { + Class asyncTaskClass + ) { Writeable.Reader> reader = in -> new StoredAsyncResponse<>(responseReader(), in); - AsyncTaskIndexService> store = new AsyncTaskIndexService<>(XPackPlugin.ASYNC_RESULTS_INDEX, - clusterService, threadPool.getThreadContext(), client, ASYNC_SEARCH_ORIGIN, reader, registry, bigArrays); - return new AsyncResultsService<>(store, false, asyncTaskClass, + AsyncTaskIndexService> store = new AsyncTaskIndexService<>( + XPackPlugin.ASYNC_RESULTS_INDEX, + clusterService, + threadPool.getThreadContext(), + client, + ASYNC_SEARCH_ORIGIN, + reader, + registry, + bigArrays + ); + return new AsyncResultsService<>( + store, + false, + asyncTaskClass, (task, listener, timeout) -> AsyncTaskManagementService.addCompletionListener(threadPool, task, listener, timeout), - transportService.getTaskManager(), clusterService); + transportService.getTaskManager(), + clusterService + ); } @Override protected void doExecute(Task task, GetAsyncResultRequest request, ActionListener listener) { DiscoveryNode node = resultsService.getNode(request.getId()); if (node == null || resultsService.isLocalNode(node)) { - resultsService.retrieveResult(request, ActionListener.wrap( - r -> { - if (r.getException() != null) { - listener.onFailure(r.getException()); - } else { - listener.onResponse(r.getResponse()); - } - }, - listener::onFailure - )); + resultsService.retrieveResult(request, ActionListener.wrap(r -> { + if (r.getException() != null) { + listener.onFailure(r.getException()); + } else { + listener.onResponse(r.getResponse()); + } + }, listener::onFailure)); } else { - transportService.sendRequest(node, actionName, request, - new ActionListenerResponseHandler<>(listener, responseReader(), ThreadPool.Names.SAME)); + transportService.sendRequest( + node, + actionName, + request, + new ActionListenerResponseHandler<>(listener, responseReader(), ThreadPool.Names.SAME) + ); } } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plugin/AbstractTransportQlAsyncGetStatusAction.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plugin/AbstractTransportQlAsyncGetStatusAction.java index daea69a9823dc..c7dbaf9e8738f 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plugin/AbstractTransportQlAsyncGetStatusAction.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plugin/AbstractTransportQlAsyncGetStatusAction.java @@ -32,8 +32,8 @@ import static org.elasticsearch.xpack.core.ClientHelper.ASYNC_SEARCH_ORIGIN; - -public abstract class AbstractTransportQlAsyncGetStatusAction> extends HandledTransportAction { private final String actionName; private final TransportService transportService; @@ -41,23 +41,33 @@ public abstract class AbstractTransportQlAsyncGetStatusAction asyncTaskClass; private final AsyncTaskIndexService> store; - public AbstractTransportQlAsyncGetStatusAction(String actionName, - TransportService transportService, - ActionFilters actionFilters, - ClusterService clusterService, - NamedWriteableRegistry registry, - Client client, - ThreadPool threadPool, - BigArrays bigArrays, - Class asyncTaskClass) { + public AbstractTransportQlAsyncGetStatusAction( + String actionName, + TransportService transportService, + ActionFilters actionFilters, + ClusterService clusterService, + NamedWriteableRegistry registry, + Client client, + ThreadPool threadPool, + BigArrays bigArrays, + Class asyncTaskClass + ) { super(actionName, transportService, actionFilters, GetAsyncStatusRequest::new); this.actionName = actionName; this.transportService = transportService; this.clusterService = clusterService; this.asyncTaskClass = asyncTaskClass; Writeable.Reader> reader = in -> new StoredAsyncResponse<>(responseReader(), in); - this.store = new AsyncTaskIndexService<>(XPackPlugin.ASYNC_RESULTS_INDEX, clusterService, - threadPool.getThreadContext(), client, ASYNC_SEARCH_ORIGIN, reader, registry, bigArrays); + this.store = new AsyncTaskIndexService<>( + XPackPlugin.ASYNC_RESULTS_INDEX, + clusterService, + threadPool.getThreadContext(), + client, + ASYNC_SEARCH_ORIGIN, + reader, + registry, + bigArrays + ); } @Override @@ -75,8 +85,12 @@ protected void doExecute(Task task, GetAsyncStatusRequest request, ActionListene listener ); } else { - transportService.sendRequest(node, actionName, request, - new ActionListenerResponseHandler<>(listener, QlStatusResponse::new, ThreadPool.Names.SAME)); + transportService.sendRequest( + node, + actionName, + request, + new ActionListenerResponseHandler<>(listener, QlStatusResponse::new, ThreadPool.Names.SAME) + ); } } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plugin/TransportActionUtils.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plugin/TransportActionUtils.java index f839ba54c55ef..6431c83ee1c2e 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plugin/TransportActionUtils.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plugin/TransportActionUtils.java @@ -19,15 +19,20 @@ public final class TransportActionUtils { /** * Execute a *QL request and re-try it in case the first request failed with a {@code VersionMismatchException} - * + * * @param clusterService The cluster service instance * @param onFailure On-failure handler in case the request doesn't fail with a {@code VersionMismatchException} * @param queryRunner *QL query execution code, typically a Plan Executor running the query * @param retryRequest Re-trial logic * @param log Log4j logger */ - public static void executeRequestWithRetryAttempt(ClusterService clusterService, Consumer onFailure, - Consumer> queryRunner, Consumer retryRequest, Logger log) { + public static void executeRequestWithRetryAttempt( + ClusterService clusterService, + Consumer onFailure, + Consumer> queryRunner, + Consumer retryRequest, + Logger log + ) { Holder retrySecondTime = new Holder(false); queryRunner.accept(e -> { @@ -49,8 +54,13 @@ public static void executeRequestWithRetryAttempt(ClusterService clusterService, } if (candidateNode != null) { if (log.isDebugEnabled()) { - log.debug("Candidate node to resend the request to: address [{}], id [{}], name [{}], version [{}]", - candidateNode.getAddress(), candidateNode.getId(), candidateNode.getName(), candidateNode.getVersion()); + log.debug( + "Candidate node to resend the request to: address [{}], id [{}], name [{}], version [{}]", + candidateNode.getAddress(), + candidateNode.getId(), + candidateNode.getName(), + candidateNode.getVersion() + ); } // re-send the request to the older node retryRequest.accept(candidateNode); diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/container/AttributeSort.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/container/AttributeSort.java index 27a28e5fedf2e..d6bb0181f0e0c 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/container/AttributeSort.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/container/AttributeSort.java @@ -40,7 +40,7 @@ public boolean equals(Object obj) { AttributeSort other = (AttributeSort) obj; return Objects.equals(direction(), other.direction()) - && Objects.equals(missing(), other.missing()) - && Objects.equals(attribute, other.attribute); + && Objects.equals(missing(), other.missing()) + && Objects.equals(attribute, other.attribute); } } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/container/ScriptSort.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/container/ScriptSort.java index f29eae447fa52..038a1e6372472 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/container/ScriptSort.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/container/ScriptSort.java @@ -41,7 +41,7 @@ public boolean equals(Object obj) { ScriptSort other = (ScriptSort) obj; return Objects.equals(direction(), other.direction()) - && Objects.equals(missing(), other.missing()) - && Objects.equals(script, other.script); + && Objects.equals(missing(), other.missing()) + && Objects.equals(script, other.script); } } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/container/Sort.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/container/Sort.java index 0899a40a2b42a..0ca3f1fc9e6d6 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/container/Sort.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/container/Sort.java @@ -14,7 +14,8 @@ public abstract class Sort { public enum Direction { - ASC, DESC; + ASC, + DESC; public static Direction from(OrderDirection dir) { return dir == null || dir == OrderDirection.ASC ? ASC : DESC; @@ -76,6 +77,7 @@ public String searchOrder(Direction direction) { } } } + /** * Preferred order of null values in aggregation queries. */ diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/query/BoolQuery.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/query/BoolQuery.java index 1a6a850946c58..18b4ba2d0339e 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/query/BoolQuery.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/query/BoolQuery.java @@ -96,9 +96,7 @@ public boolean equals(Object obj) { return false; } BoolQuery other = (BoolQuery) obj; - return isAnd == other.isAnd - && left.equals(other.left) - && right.equals(other.right); + return isAnd == other.isAnd && left.equals(other.left) && right.equals(other.right); } @Override diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/query/GeoDistanceQuery.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/query/GeoDistanceQuery.java index dd23a30f2a40a..4997e02b44e62 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/query/GeoDistanceQuery.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/query/GeoDistanceQuery.java @@ -65,14 +65,14 @@ public boolean equals(Object obj) { } GeoDistanceQuery other = (GeoDistanceQuery) obj; - return Objects.equals(field, other.field) && - Objects.equals(distance, other.distance) && - Objects.equals(lat, other.lat) && - Objects.equals(lon, other.lon); + return Objects.equals(field, other.field) + && Objects.equals(distance, other.distance) + && Objects.equals(lat, other.lat) + && Objects.equals(lon, other.lon); } @Override protected String innerToString() { - return field + ":" + "(" + distance + "," + "(" + lat + ", " + lon + "))"; + return field + ":" + "(" + distance + "," + "(" + lat + ", " + lon + "))"; } } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/query/MatchQuery.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/query/MatchQuery.java index 7edd18ed60fe4..c5455328fea4e 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/query/MatchQuery.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/query/MatchQuery.java @@ -6,8 +6,8 @@ */ package org.elasticsearch.xpack.ql.querydsl.query; -import org.elasticsearch.core.Booleans; import org.elasticsearch.common.unit.Fuzziness; +import org.elasticsearch.core.Booleans; import org.elasticsearch.index.query.MatchQueryBuilder; import org.elasticsearch.index.query.Operator; import org.elasticsearch.index.query.QueryBuilder; @@ -31,16 +31,17 @@ public class MatchQuery extends LeafQuery { // TODO: add zero terms query support, I'm not sure the best way to parse it yet... // appliers.put("zero_terms_query", (qb, s) -> qb.zeroTermsQuery(s)); BUILDER_APPLIERS = Map.ofEntries( - entry("analyzer", MatchQueryBuilder::analyzer), - entry("auto_generate_synonyms_phrase_query", (qb, s) -> qb.autoGenerateSynonymsPhraseQuery(Booleans.parseBoolean(s))), - entry("fuzziness", (qb, s) -> qb.fuzziness(Fuzziness.fromString(s))), - entry("fuzzy_transpositions", (qb, s) -> qb.fuzzyTranspositions(Booleans.parseBoolean(s))), - entry("fuzzy_rewrite", MatchQueryBuilder::fuzzyRewrite), - entry("lenient", (qb, s) -> qb.lenient(Booleans.parseBoolean(s))), - entry("max_expansions", (qb, s) -> qb.maxExpansions(Integer.valueOf(s))), - entry("minimum_should_match", MatchQueryBuilder::minimumShouldMatch), - entry("operator", (qb, s) -> qb.operator(Operator.fromString(s))), - entry("prefix_length", (qb, s) -> qb.prefixLength(Integer.valueOf(s)))); + entry("analyzer", MatchQueryBuilder::analyzer), + entry("auto_generate_synonyms_phrase_query", (qb, s) -> qb.autoGenerateSynonymsPhraseQuery(Booleans.parseBoolean(s))), + entry("fuzziness", (qb, s) -> qb.fuzziness(Fuzziness.fromString(s))), + entry("fuzzy_transpositions", (qb, s) -> qb.fuzzyTranspositions(Booleans.parseBoolean(s))), + entry("fuzzy_rewrite", MatchQueryBuilder::fuzzyRewrite), + entry("lenient", (qb, s) -> qb.lenient(Booleans.parseBoolean(s))), + entry("max_expansions", (qb, s) -> qb.maxExpansions(Integer.valueOf(s))), + entry("minimum_should_match", MatchQueryBuilder::minimumShouldMatch), + entry("operator", (qb, s) -> qb.operator(Operator.fromString(s))), + entry("prefix_length", (qb, s) -> qb.prefixLength(Integer.valueOf(s))) + ); } private final String name; @@ -48,7 +49,6 @@ public class MatchQuery extends LeafQuery { private final MatchQueryPredicate predicate; private final Map options; - public MatchQuery(Source source, String name, Object text) { this(source, name, text, null); } @@ -98,9 +98,7 @@ public boolean equals(Object obj) { } MatchQuery other = (MatchQuery) obj; - return Objects.equals(text, other.text) - && Objects.equals(name, other.name) - && Objects.equals(predicate, other.predicate); + return Objects.equals(text, other.text) && Objects.equals(name, other.name) && Objects.equals(predicate, other.predicate); } @Override diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/query/MultiMatchQuery.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/query/MultiMatchQuery.java index 76b13c4a88aea..2ae226bb2d88a 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/query/MultiMatchQuery.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/query/MultiMatchQuery.java @@ -6,8 +6,8 @@ */ package org.elasticsearch.xpack.ql.querydsl.query; -import org.elasticsearch.core.Booleans; import org.elasticsearch.common.unit.Fuzziness; +import org.elasticsearch.core.Booleans; import org.elasticsearch.index.query.MultiMatchQueryBuilder; import org.elasticsearch.index.query.Operator; import org.elasticsearch.index.query.QueryBuilder; @@ -28,21 +28,22 @@ public class MultiMatchQuery extends LeafQuery { static { // TODO: it'd be great if these could be constants instead of Strings, needs a core change to make the fields public first BUILDER_APPLIERS = Map.ofEntries( - entry("slop", (qb, s) -> qb.slop(Integer.valueOf(s))), - // TODO: add zero terms query support, I'm not sure the best way to parse it yet... - // appliers.put("zero_terms_query", (qb, s) -> qb.zeroTermsQuery(s)); - entry("analyzer", MultiMatchQueryBuilder::analyzer), - entry("auto_generate_synonyms_phrase_query", (qb, s) -> qb.autoGenerateSynonymsPhraseQuery(Booleans.parseBoolean(s))), - entry("fuzziness", (qb, s) -> qb.fuzziness(Fuzziness.fromString(s))), - entry("fuzzy_rewrite", MultiMatchQueryBuilder::fuzzyRewrite), - entry("fuzzy_transpositions", (qb, s) -> qb.fuzzyTranspositions(Booleans.parseBoolean(s))), - entry("lenient", (qb, s) -> qb.lenient(Booleans.parseBoolean(s))), - entry("max_expansions", (qb, s) -> qb.maxExpansions(Integer.valueOf(s))), - entry("minimum_should_match", MultiMatchQueryBuilder::minimumShouldMatch), - entry("operator", (qb, s) -> qb.operator(Operator.fromString(s))), - entry("prefix_length", (qb, s) -> qb.prefixLength(Integer.valueOf(s))), - entry("tie_breaker", (qb, s) -> qb.tieBreaker(Float.valueOf(s))), - entry("type", MultiMatchQueryBuilder::type)); + entry("slop", (qb, s) -> qb.slop(Integer.valueOf(s))), + // TODO: add zero terms query support, I'm not sure the best way to parse it yet... + // appliers.put("zero_terms_query", (qb, s) -> qb.zeroTermsQuery(s)); + entry("analyzer", MultiMatchQueryBuilder::analyzer), + entry("auto_generate_synonyms_phrase_query", (qb, s) -> qb.autoGenerateSynonymsPhraseQuery(Booleans.parseBoolean(s))), + entry("fuzziness", (qb, s) -> qb.fuzziness(Fuzziness.fromString(s))), + entry("fuzzy_rewrite", MultiMatchQueryBuilder::fuzzyRewrite), + entry("fuzzy_transpositions", (qb, s) -> qb.fuzzyTranspositions(Booleans.parseBoolean(s))), + entry("lenient", (qb, s) -> qb.lenient(Booleans.parseBoolean(s))), + entry("max_expansions", (qb, s) -> qb.maxExpansions(Integer.valueOf(s))), + entry("minimum_should_match", MultiMatchQueryBuilder::minimumShouldMatch), + entry("operator", (qb, s) -> qb.operator(Operator.fromString(s))), + entry("prefix_length", (qb, s) -> qb.prefixLength(Integer.valueOf(s))), + entry("tie_breaker", (qb, s) -> qb.tieBreaker(Float.valueOf(s))), + entry("type", MultiMatchQueryBuilder::type) + ); } private final String query; @@ -89,9 +90,7 @@ public boolean equals(Object obj) { } MultiMatchQuery other = (MultiMatchQuery) obj; - return Objects.equals(query, other.query) - && Objects.equals(fields, other.fields) - && Objects.equals(predicate, other.predicate); + return Objects.equals(query, other.query) && Objects.equals(fields, other.fields) && Objects.equals(predicate, other.predicate); } @Override diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/query/NestedQuery.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/query/NestedQuery.java index 2b17a30a90efe..cd32b7a312299 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/query/NestedQuery.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/query/NestedQuery.java @@ -93,7 +93,7 @@ public void enrichNestedSort(NestedSortBuilder sort) { return; } - //TODO: Add all filters in nested sorting when https://github.com/elastic/elasticsearch/issues/33079 is implemented + // TODO: Add all filters in nested sorting when https://github.com/elastic/elasticsearch/issues/33079 is implemented // Adding multiple filters to sort sections makes sense for nested queries where multiple conditions belong to the same // nested query. The current functionality creates one nested query for each condition involving a nested field. QueryBuilder childAsBuilder = child.asBuilder(); @@ -118,8 +118,7 @@ public QueryBuilder asBuilder() { for (Map.Entry> entry : fields.entrySet()) { if (entry.getValue().getKey()) { ihb.addFetchField(entry.getKey(), entry.getValue().getValue()); - } - else { + } else { ihb.addFetchField(entry.getKey()); } } @@ -155,9 +154,7 @@ public boolean equals(Object obj) { return false; } NestedQuery other = (NestedQuery) obj; - return path.equals(other.path) - && fields.equals(other.fields) - && child.equals(other.child); + return path.equals(other.path) && fields.equals(other.fields) && child.equals(other.child); } @Override diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/query/PrefixQuery.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/query/PrefixQuery.java index 72516182b95ac..dc15a95b2abd3 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/query/PrefixQuery.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/query/PrefixQuery.java @@ -54,9 +54,7 @@ public boolean equals(Object obj) { } PrefixQuery other = (PrefixQuery) obj; - return caseInsensitive == other.caseInsensitive - && Objects.equals(field, other.field) - && Objects.equals(query, other.query); + return caseInsensitive == other.caseInsensitive && Objects.equals(field, other.field) && Objects.equals(query, other.query); } @Override diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/query/QueryStringQuery.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/query/QueryStringQuery.java index 13cb8149e0478..39c179ee41691 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/query/QueryStringQuery.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/query/QueryStringQuery.java @@ -6,9 +6,9 @@ */ package org.elasticsearch.xpack.ql.querydsl.query; -import org.elasticsearch.core.Booleans; import org.elasticsearch.common.unit.Fuzziness; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.core.Booleans; import org.elasticsearch.index.query.MultiMatchQueryBuilder; import org.elasticsearch.index.query.Operator; import org.elasticsearch.index.query.QueryBuilder; @@ -28,29 +28,30 @@ public class QueryStringQuery extends LeafQuery { // TODO: it'd be great if these could be constants instead of Strings, needs a core change to make the fields public first private static final Map> BUILDER_APPLIERS = Map.ofEntries( - entry("allow_leading_wildcard", (qb, s) -> qb.allowLeadingWildcard(Booleans.parseBoolean(s))), - entry("analyze_wildcard", (qb, s) -> qb.analyzeWildcard(Booleans.parseBoolean(s))), - entry("analyzer", QueryStringQueryBuilder::analyzer), - entry("auto_generate_synonyms_phrase_query", (qb, s) -> qb.autoGenerateSynonymsPhraseQuery(Booleans.parseBoolean(s))), - entry("default_field", QueryStringQueryBuilder::defaultField), - entry("default_operator", (qb, s) -> qb.defaultOperator(Operator.fromString(s))), - entry("enable_position_increments", (qb, s) -> qb.enablePositionIncrements(Booleans.parseBoolean(s))), - entry("escape", (qb, s) -> qb.escape(Booleans.parseBoolean(s))), - entry("fuzziness", (qb, s) -> qb.fuzziness(Fuzziness.fromString(s))), - entry("fuzzy_max_expansions", (qb, s) -> qb.fuzzyMaxExpansions(Integer.valueOf(s))), - entry("fuzzy_prefix_length", (qb, s) -> qb.fuzzyPrefixLength(Integer.valueOf(s))), - entry("fuzzy_rewrite", QueryStringQueryBuilder::fuzzyRewrite), - entry("fuzzy_transpositions", (qb, s) -> qb.fuzzyTranspositions(Booleans.parseBoolean(s))), - entry("lenient", (qb, s) -> qb.lenient(Booleans.parseBoolean(s))), - entry("max_determinized_states", (qb, s) -> qb.maxDeterminizedStates(Integer.valueOf(s))), - entry("minimum_should_match", QueryStringQueryBuilder::minimumShouldMatch), - entry("phrase_slop", (qb, s) -> qb.phraseSlop(Integer.valueOf(s))), - entry("rewrite", QueryStringQueryBuilder::rewrite), - entry("quote_analyzer", QueryStringQueryBuilder::quoteAnalyzer), - entry("quote_field_suffix", QueryStringQueryBuilder::quoteFieldSuffix), - entry("tie_breaker", (qb, s) -> qb.tieBreaker(Float.valueOf(s))), - entry("time_zone", QueryStringQueryBuilder::timeZone), - entry("type", (qb, s) -> qb.type(MultiMatchQueryBuilder.Type.parse(s, LoggingDeprecationHandler.INSTANCE)))); + entry("allow_leading_wildcard", (qb, s) -> qb.allowLeadingWildcard(Booleans.parseBoolean(s))), + entry("analyze_wildcard", (qb, s) -> qb.analyzeWildcard(Booleans.parseBoolean(s))), + entry("analyzer", QueryStringQueryBuilder::analyzer), + entry("auto_generate_synonyms_phrase_query", (qb, s) -> qb.autoGenerateSynonymsPhraseQuery(Booleans.parseBoolean(s))), + entry("default_field", QueryStringQueryBuilder::defaultField), + entry("default_operator", (qb, s) -> qb.defaultOperator(Operator.fromString(s))), + entry("enable_position_increments", (qb, s) -> qb.enablePositionIncrements(Booleans.parseBoolean(s))), + entry("escape", (qb, s) -> qb.escape(Booleans.parseBoolean(s))), + entry("fuzziness", (qb, s) -> qb.fuzziness(Fuzziness.fromString(s))), + entry("fuzzy_max_expansions", (qb, s) -> qb.fuzzyMaxExpansions(Integer.valueOf(s))), + entry("fuzzy_prefix_length", (qb, s) -> qb.fuzzyPrefixLength(Integer.valueOf(s))), + entry("fuzzy_rewrite", QueryStringQueryBuilder::fuzzyRewrite), + entry("fuzzy_transpositions", (qb, s) -> qb.fuzzyTranspositions(Booleans.parseBoolean(s))), + entry("lenient", (qb, s) -> qb.lenient(Booleans.parseBoolean(s))), + entry("max_determinized_states", (qb, s) -> qb.maxDeterminizedStates(Integer.valueOf(s))), + entry("minimum_should_match", QueryStringQueryBuilder::minimumShouldMatch), + entry("phrase_slop", (qb, s) -> qb.phraseSlop(Integer.valueOf(s))), + entry("rewrite", QueryStringQueryBuilder::rewrite), + entry("quote_analyzer", QueryStringQueryBuilder::quoteAnalyzer), + entry("quote_field_suffix", QueryStringQueryBuilder::quoteFieldSuffix), + entry("tie_breaker", (qb, s) -> qb.tieBreaker(Float.valueOf(s))), + entry("time_zone", QueryStringQueryBuilder::timeZone), + entry("type", (qb, s) -> qb.type(MultiMatchQueryBuilder.Type.parse(s, LoggingDeprecationHandler.INSTANCE))) + ); private final String query; private final Map fields; @@ -108,9 +109,7 @@ public boolean equals(Object obj) { } QueryStringQuery other = (QueryStringQuery) obj; - return Objects.equals(query, other.query) - && Objects.equals(fields, other.fields) - && Objects.equals(predicate, other.predicate); + return Objects.equals(query, other.query) && Objects.equals(fields, other.fields) && Objects.equals(predicate, other.predicate); } @Override diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/query/RangeQuery.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/query/RangeQuery.java index 96ab36eecc3ff..5464b644c34aa 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/query/RangeQuery.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/query/RangeQuery.java @@ -28,8 +28,16 @@ public RangeQuery(Source source, String field, Object lower, boolean includeLowe this(source, field, lower, includeLower, upper, includeUpper, null, zoneId); } - public RangeQuery(Source source, String field, Object lower, boolean includeLower, Object upper, - boolean includeUpper, String format, ZoneId zoneId) { + public RangeQuery( + Source source, + String field, + Object lower, + boolean includeLower, + Object upper, + boolean includeUpper, + String format, + ZoneId zoneId + ) { super(source); this.field = field; this.lower = lower; @@ -97,19 +105,17 @@ public boolean equals(Object obj) { } RangeQuery other = (RangeQuery) obj; - return Objects.equals(field, other.field) && - Objects.equals(includeLower, other.includeLower) && - Objects.equals(includeUpper, other.includeUpper) && - Objects.equals(lower, other.lower) && - Objects.equals(upper, other.upper) && - Objects.equals(format, other.format) && - Objects.equals(zoneId, other.zoneId); + return Objects.equals(field, other.field) + && Objects.equals(includeLower, other.includeLower) + && Objects.equals(includeUpper, other.includeUpper) + && Objects.equals(lower, other.lower) + && Objects.equals(upper, other.upper) + && Objects.equals(format, other.format) + && Objects.equals(zoneId, other.zoneId); } @Override protected String innerToString() { - return field + ":" - + (includeLower ? "[" : "(") + lower + ", " - + upper + (includeUpper ? "]" : ")") + "@" + zoneId.getId(); + return field + ":" + (includeLower ? "[" : "(") + lower + ", " + upper + (includeUpper ? "]" : ")") + "@" + zoneId.getId(); } } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/query/TermQuery.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/query/TermQuery.java index d758b781b2551..5e1c9405d0525 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/query/TermQuery.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/query/TermQuery.java @@ -67,8 +67,8 @@ public boolean equals(Object obj) { TermQuery other = (TermQuery) obj; return Objects.equals(term, other.term) - && Objects.equals(value, other.value) - && Objects.equals(caseInsensitive, other.caseInsensitive); + && Objects.equals(value, other.value) + && Objects.equals(caseInsensitive, other.caseInsensitive); } @Override diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/query/TermsQuery.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/query/TermsQuery.java index 0138dd58b60d1..dd5e81b073589 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/query/TermsQuery.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/query/TermsQuery.java @@ -46,8 +46,7 @@ public boolean equals(Object obj) { } TermsQuery other = (TermsQuery) obj; - return Objects.equals(term, other.term) - && Objects.equals(values, other.values); + return Objects.equals(term, other.term) && Objects.equals(values, other.values); } @Override diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/query/WildcardQuery.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/query/WildcardQuery.java index a22fe244db5e6..52bc1df0501bf 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/query/WildcardQuery.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/querydsl/query/WildcardQuery.java @@ -66,8 +66,8 @@ public boolean equals(Object obj) { WildcardQuery other = (WildcardQuery) obj; return Objects.equals(field, other.field) - && Objects.equals(query, other.query) - && Objects.equals(caseInsensitive, other.caseInsensitive); + && Objects.equals(query, other.query) + && Objects.equals(caseInsensitive, other.caseInsensitive); } @Override diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/rule/RuleExecutor.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/rule/RuleExecutor.java index 594f352b0a06c..b0553facdf8b3 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/rule/RuleExecutor.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/rule/RuleExecutor.java @@ -165,8 +165,7 @@ protected ExecutionInfo executeWithInfo(TreeType plan) { if (log.isTraceEnabled()) { log.trace("Rule {} applied\n{}", rule, NodeUtils.diffString(tf.before, tf.after)); } - } - else { + } else { if (log.isTraceEnabled()) { log.trace("Rule {} applied w/o changes", rule); } @@ -184,14 +183,17 @@ protected ExecutionInfo executeWithInfo(TreeType plan) { before = tfs.get(0).before; after = tfs.get(tfs.size() - 1).after; } - log.trace("Batch {} applied took {}\n{}", - batch.name, TimeValue.timeValueMillis(batchDuration), NodeUtils.diffString(before, after)); + log.trace( + "Batch {} applied took {}\n{}", + batch.name, + TimeValue.timeValueMillis(batchDuration), + NodeUtils.diffString(before, after) + ); } } if (false == currentPlan.equals(plan) && log.isDebugEnabled()) { - log.debug("Tree transformation took {}\n{}", - TimeValue.timeValueMillis(totalDuration), NodeUtils.diffString(plan, currentPlan)); + log.debug("Tree transformation took {}\n{}", TimeValue.timeValueMillis(totalDuration), NodeUtils.diffString(plan, currentPlan)); } return new ExecutionInfo(plan, currentPlan, transformations); diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/tree/Location.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/tree/Location.java index d38e94c4850dc..05f1af132ea52 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/tree/Location.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/tree/Location.java @@ -43,7 +43,6 @@ public boolean equals(Object obj) { return false; } Location other = (Location) obj; - return line == other.line - && charPositionInLine == other.charPositionInLine; + return line == other.line && charPositionInLine == other.charPositionInLine; } } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/tree/Node.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/tree/Node.java index 04b8ae686e51a..18ddbd58fc764 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/tree/Node.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/tree/Node.java @@ -221,10 +221,11 @@ public T transformUp(Class typeToken, Function return (childrenChanged ? replaceChildrenSameSize(transformedChildren) : (T) this); } - public final T replaceChildrenSameSize(List newChildren) { + public final T replaceChildrenSameSize(List newChildren) { if (newChildren.size() != children.size()) { throw new QlIllegalArgumentException( - "Expected the same number of children [" + children.size() + "], but received [" + newChildren.size() + "]"); + "Expected the same number of children [" + children.size() + "], but received [" + newChildren.size() + "]" + ); } return replaceChildren(newChildren); } @@ -330,8 +331,7 @@ final StringBuilder treeString(StringBuilder sb, int depth, BitSet hasParentPerD if (column < depth - 1) { sb.append(" "); } - } - else { + } else { // if the child has no parent (elder on the previous level), it means its the last sibling sb.append((column == depth - 1) ? "\\" : " "); } @@ -387,7 +387,7 @@ public String propertiesToString(boolean skipIfChild) { String stringValue = toString(prop); - //: Objects.toString(prop); + // : Objects.toString(prop); if (maxWidth + stringValue.length() > TO_STRING_MAX_WIDTH) { int cutoff = Math.max(0, TO_STRING_MAX_WIDTH - maxWidth); sb.append(stringValue.substring(0, cutoff)); diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/tree/NodeInfo.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/tree/NodeInfo.java index 315c20304c15d..a9782554eadbd 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/tree/NodeInfo.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/tree/NodeInfo.java @@ -41,6 +41,7 @@ private NodeInfo(T node) { public final List properties() { return unmodifiableList(innerProperties()); } + protected abstract List innerProperties(); /** @@ -51,14 +52,14 @@ final T transform(Function rule, Class typeToken) List children = node.children(); Function realRule = p -> { - if (p != children && false == children.contains(p) - && (p == null || typeToken.isInstance(p))) { + if (p != children && false == children.contains(p) && (p == null || typeToken.isInstance(p))) { return rule.apply(typeToken.cast(p)); } return p; }; return innerTransform(realRule); } + protected abstract T innerTransform(Function rule); /** @@ -77,9 +78,7 @@ protected T innerTransform(Function rule) { }; } - public static , P1> NodeInfo create( - T n, BiFunction ctor, - P1 p1) { + public static , P1> NodeInfo create(T n, BiFunction ctor, P1 p1) { return new NodeInfo(n) { @Override protected List innerProperties() { @@ -98,9 +97,7 @@ protected T innerTransform(Function rule) { }; } - public static , P1, P2> NodeInfo create( - T n, NodeCtor2 ctor, - P1 p1, P2 p2) { + public static , P1, P2> NodeInfo create(T n, NodeCtor2 ctor, P1 p1, P2 p2) { return new NodeInfo(n) { @Override protected List innerProperties() { @@ -121,13 +118,12 @@ protected T innerTransform(Function rule) { } }; } + public interface NodeCtor2 { T apply(Source l, P1 p1, P2 p2); } - public static , P1, P2, P3> NodeInfo create( - T n, NodeCtor3 ctor, - P1 p1, P2 p2, P3 p3) { + public static , P1, P2, P3> NodeInfo create(T n, NodeCtor3 ctor, P1 p1, P2 p2, P3 p3) { return new NodeInfo(n) { @Override protected List innerProperties() { @@ -151,13 +147,19 @@ protected T innerTransform(Function rule) { } }; } + public interface NodeCtor3 { T apply(Source l, P1 p1, P2 p2, P3 p3); } public static , P1, P2, P3, P4> NodeInfo create( - T n, NodeCtor4 ctor, - P1 p1, P2 p2, P3 p3, P4 p4) { + T n, + NodeCtor4 ctor, + P1 p1, + P2 p2, + P3 p3, + P4 p4 + ) { return new NodeInfo(n) { @Override protected List innerProperties() { @@ -184,13 +186,20 @@ protected T innerTransform(Function rule) { } }; } + public interface NodeCtor4 { T apply(Source l, P1 p1, P2 p2, P3 p3, P4 p4); } public static , P1, P2, P3, P4, P5> NodeInfo create( - T n, NodeCtor5 ctor, - P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) { + T n, + NodeCtor5 ctor, + P1 p1, + P2 p2, + P3 p3, + P4 p4, + P5 p5 + ) { return new NodeInfo(n) { @Override protected List innerProperties() { @@ -220,13 +229,21 @@ protected T innerTransform(Function rule) { } }; } + public interface NodeCtor5 { T apply(Source l, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5); } public static , P1, P2, P3, P4, P5, P6> NodeInfo create( - T n, NodeCtor6 ctor, - P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6) { + T n, + NodeCtor6 ctor, + P1 p1, + P2 p2, + P3 p3, + P4 p4, + P5 p5, + P6 p6 + ) { return new NodeInfo(n) { @Override protected List innerProperties() { @@ -259,13 +276,22 @@ protected T innerTransform(Function rule) { } }; } + public interface NodeCtor6 { T apply(Source l, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6); } public static , P1, P2, P3, P4, P5, P6, P7> NodeInfo create( - T n, NodeCtor7 ctor, - P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7) { + T n, + NodeCtor7 ctor, + P1 p1, + P2 p2, + P3 p3, + P4 p4, + P5 p5, + P6 p6, + P7 p7 + ) { return new NodeInfo(n) { @Override protected List innerProperties() { @@ -301,13 +327,23 @@ protected T innerTransform(Function rule) { } }; } + public interface NodeCtor7 { T apply(Source l, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7); } public static , P1, P2, P3, P4, P5, P6, P7, P8> NodeInfo create( - T n, NodeCtor8 ctor, - P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8) { + T n, + NodeCtor8 ctor, + P1 p1, + P2 p2, + P3 p3, + P4 p4, + P5 p5, + P6 p6, + P7 p7, + P8 p8 + ) { return new NodeInfo(n) { @Override protected List innerProperties() { @@ -346,13 +382,24 @@ protected T innerTransform(Function rule) { } }; } + public interface NodeCtor8 { T apply(Source l, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8); } public static , P1, P2, P3, P4, P5, P6, P7, P8, P9> NodeInfo create( - T n, NodeCtor9 ctor, - P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8, P9 p9) { + T n, + NodeCtor9 ctor, + P1 p1, + P2 p2, + P3 p3, + P4 p4, + P5 p5, + P6 p6, + P7 p7, + P8 p8, + P9 p9 + ) { return new NodeInfo(n) { @Override protected List innerProperties() { @@ -394,13 +441,25 @@ protected T innerTransform(Function rule) { } }; } + public interface NodeCtor9 { T apply(Source l, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8, P9 p9); } public static , P1, P2, P3, P4, P5, P6, P7, P8, P9, P10> NodeInfo create( - T n, NodeCtor10 ctor, - P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8, P9 p9, P10 p10) { + T n, + NodeCtor10 ctor, + P1 p1, + P2 p2, + P3 p3, + P4 p4, + P5 p5, + P6 p6, + P7 p7, + P8 p8, + P9 p9, + P10 p10 + ) { return new NodeInfo(n) { @Override protected List innerProperties() { @@ -441,11 +500,11 @@ protected T innerTransform(Function rule) { P10 newP10 = (P10) rule.apply(p10); same &= Objects.equals(p10, newP10); - return same ? node : ctor.apply(node.source(), newP1, newP2, newP3, newP4, newP5, newP6, newP7, newP8, - newP9, newP10); + return same ? node : ctor.apply(node.source(), newP1, newP2, newP3, newP4, newP5, newP6, newP7, newP8, newP9, newP10); } }; } + public interface NodeCtor10 { T apply(Source l, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8, P9 p9, P10 p10); } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/tree/NodeUtils.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/tree/NodeUtils.java index 32583f39b7682..7ea9c17677f2d 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/tree/NodeUtils.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/tree/NodeUtils.java @@ -27,15 +27,14 @@ public static String diffString(String left, String right) { } // try to allocate the buffer - 5 represents the column comparison chars - StringBuilder sb = new StringBuilder(left.length() + right.length() + Math.max(left.length(), right.length()) * 3); + StringBuilder sb = new StringBuilder(left.length() + right.length() + Math.max(left.length(), right.length()) * 3); boolean leftAvailable = true, rightAvailable = true; for (int leftIndex = 0, rightIndex = 0; leftAvailable || rightAvailable; leftIndex++, rightIndex++) { String leftRow = "", rightRow = leftRow; if (leftIndex < leftSplit.length) { leftRow = leftSplit[leftIndex]; - } - else { + } else { leftAvailable = false; } sb.append(leftRow); @@ -45,8 +44,7 @@ public static String diffString(String left, String right) { // right side still available if (rightIndex < rightSplit.length) { rightRow = rightSplit[rightIndex]; - } - else { + } else { rightAvailable = false; } if (leftAvailable || rightAvailable) { @@ -58,7 +56,6 @@ public static String diffString(String left, String right) { return sb.toString(); } - private static final int TO_STRING_LIMIT = 52; public static String limitedToString(Collection c) { diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/DataType.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/DataType.java index 5a4576372d325..5b18234347fab 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/DataType.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/DataType.java @@ -99,11 +99,11 @@ public boolean equals(Object obj) { DataType other = (DataType) obj; return Objects.equals(typeName, other.typeName) - && Objects.equals(esType, other.esType) - && size == other.size - && isInteger == other.isInteger - && isRational == other.isRational - && docValues == other.docValues; + && Objects.equals(esType, other.esType) + && size == other.size + && isInteger == other.isInteger + && isRational == other.isRational + && docValues == other.docValues; } @Override diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/DataTypeConverter.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/DataTypeConverter.java index eaef4b1e8235d..d28202515f8e8 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/DataTypeConverter.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/DataTypeConverter.java @@ -6,10 +6,10 @@ */ package org.elasticsearch.xpack.ql.type; -import org.elasticsearch.core.Booleans; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.network.InetAddresses; +import org.elasticsearch.core.Booleans; import org.elasticsearch.xpack.ql.QlIllegalArgumentException; import java.io.IOException; @@ -383,8 +383,12 @@ public static Object convert(Object value, DataType dataType) { Converter converter = converterFor(detectedType, dataType); if (converter == null) { - throw new QlIllegalArgumentException("cannot convert from [{}], type [{}] to [{}]", value, detectedType.typeName(), - dataType.typeName()); + throw new QlIllegalArgumentException( + "cannot convert from [{}], type [{}] to [{}]", + value, + detectedType.typeName(), + dataType.typeName() + ); } return converter.convert(value); diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/DataTypes.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/DataTypes.java index 5bda14a13baaf..616f7f2ec5bde 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/DataTypes.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/DataTypes.java @@ -51,30 +51,27 @@ public final class DataTypes { //@formatter:on private static final Collection TYPES = Arrays.asList( - UNSUPPORTED, - NULL, - BOOLEAN, - BYTE, - SHORT, - INTEGER, - LONG, - DOUBLE, - FLOAT, - HALF_FLOAT, - SCALED_FLOAT, - KEYWORD, - TEXT, - DATETIME, - IP, - BINARY, - OBJECT, - NESTED) - .stream() - .sorted(Comparator.comparing(DataType::typeName)) - .collect(toUnmodifiableList()); - - private static final Map NAME_TO_TYPE = TYPES.stream() - .collect(toUnmodifiableMap(DataType::typeName, t -> t)); + UNSUPPORTED, + NULL, + BOOLEAN, + BYTE, + SHORT, + INTEGER, + LONG, + DOUBLE, + FLOAT, + HALF_FLOAT, + SCALED_FLOAT, + KEYWORD, + TEXT, + DATETIME, + IP, + BINARY, + OBJECT, + NESTED + ).stream().sorted(Comparator.comparing(DataType::typeName)).collect(toUnmodifiableList()); + + private static final Map NAME_TO_TYPE = TYPES.stream().collect(toUnmodifiableMap(DataType::typeName, t -> t)); private static Map ES_TO_TYPE; @@ -166,11 +163,10 @@ public static boolean areCompatible(DataType left, DataType right) { if (left == right) { return true; } else { - return - (left == NULL || right == NULL) - || (isString(left) && isString(right)) - || (left.isNumeric() && right.isNumeric()) - || (isDateTime(left) && isDateTime(right)); + return (left == NULL || right == NULL) + || (isString(left) && isString(right)) + || (left.isNumeric() && right.isNumeric()) + || (isDateTime(left) && isDateTime(right)); } } } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/DateUtils.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/DateUtils.java index d20231811cae4..9cc1f857a0f4a 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/DateUtils.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/DateUtils.java @@ -23,24 +23,24 @@ public final class DateUtils { public static final ZoneId UTC = ZoneId.of("Z"); - private static final DateTimeFormatter DATE_OPTIONAL_TIME_FORMATTER_WHITESPACE = new DateTimeFormatterBuilder() - .append(ISO_LOCAL_DATE) + private static final DateTimeFormatter DATE_OPTIONAL_TIME_FORMATTER_WHITESPACE = new DateTimeFormatterBuilder().append(ISO_LOCAL_DATE) .optionalStart() .appendLiteral(' ') .append(ISO_LOCAL_TIME) .optionalStart() .appendZoneOrOffsetId() .optionalEnd() - .toFormatter().withZone(UTC); - private static final DateTimeFormatter DATE_OPTIONAL_TIME_FORMATTER_T_LITERAL = new DateTimeFormatterBuilder() - .append(ISO_LOCAL_DATE) + .toFormatter() + .withZone(UTC); + private static final DateTimeFormatter DATE_OPTIONAL_TIME_FORMATTER_T_LITERAL = new DateTimeFormatterBuilder().append(ISO_LOCAL_DATE) .optionalStart() .appendLiteral('T') .append(ISO_LOCAL_TIME) .optionalStart() .appendZoneOrOffsetId() .optionalEnd() - .toFormatter().withZone(UTC); + .toFormatter() + .withZone(UTC); private DateUtils() {} @@ -61,8 +61,8 @@ public static ZonedDateTime asDateTime(String dateFormat) { } // Find the second `-` date separator and move 3 places past the dayOfYear to find the time separator // e.g. 2020-06-01T10:20:30.... - // ^ - // +3 = ^ + // ^ + // +3 = ^ separatorIdx = dateFormat.indexOf('-', separatorIdx + 1) + 3; // Avoid index out of bounds - it will lead to DateTimeParseException anyways diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/EsField.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/EsField.java index 49075d92ce899..163667749de2d 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/EsField.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/EsField.java @@ -106,9 +106,11 @@ public boolean equals(Object o) { return false; } EsField field = (EsField) o; - return aggregatable == field.aggregatable && isAlias == field.isAlias && esDataType == field.esDataType - && Objects.equals(name, field.name) - && Objects.equals(properties, field.properties); + return aggregatable == field.aggregatable + && isAlias == field.isAlias + && esDataType == field.esDataType + && Objects.equals(name, field.name) + && Objects.equals(properties, field.properties); } @Override diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/KeywordEsField.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/KeywordEsField.java index 513947c78634b..15eddbbaa2be6 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/KeywordEsField.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/KeywordEsField.java @@ -28,13 +28,26 @@ public KeywordEsField(String name, Map properties, boolean hasD this(name, properties, hasDocValues, precision, normalized, false); } - public KeywordEsField(String name, Map properties, boolean hasDocValues, int precision, - boolean normalized, boolean isAlias) { + public KeywordEsField( + String name, + Map properties, + boolean hasDocValues, + int precision, + boolean normalized, + boolean isAlias + ) { this(name, KEYWORD, properties, hasDocValues, precision, normalized, isAlias); } - protected KeywordEsField(String name, DataType esDataType, Map properties, boolean hasDocValues, int precision, - boolean normalized, boolean isAlias) { + protected KeywordEsField( + String name, + DataType esDataType, + Map properties, + boolean hasDocValues, + int precision, + boolean normalized, + boolean isAlias + ) { super(name, esDataType, properties, hasDocValues, isAlias); this.precision = precision; this.normalized = normalized; @@ -61,8 +74,7 @@ public boolean equals(Object o) { return false; } KeywordEsField that = (KeywordEsField) o; - return precision == that.precision && - normalized == that.normalized; + return precision == that.precision && normalized == that.normalized; } @Override diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/Schema.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/Schema.java index 8420bc703153b..5fa75996a9fbf 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/Schema.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/Schema.java @@ -22,6 +22,7 @@ public class Schema implements Iterable { public interface Entry { String name(); + DataType type(); } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/StringUtils.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/StringUtils.java index 92553bdd3e014..8542e3d5fc0b0 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/StringUtils.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/StringUtils.java @@ -30,29 +30,27 @@ final class StringUtils { public static final String EMPTY = ""; - public static final DateTimeFormatter ISO_DATE_WITH_NANOS = new DateTimeFormatterBuilder() - .parseCaseInsensitive() - .append(ISO_LOCAL_DATE) - .appendLiteral('T') - .appendValue(HOUR_OF_DAY, 2) - .appendLiteral(':') - .appendValue(MINUTE_OF_HOUR, 2) - .appendLiteral(':') - .appendValue(SECOND_OF_MINUTE, 2) - .appendFraction(NANO_OF_SECOND, 3, 9, true) - .appendOffsetId() - .toFormatter(Locale.ROOT); - - public static final DateTimeFormatter ISO_TIME_WITH_NANOS = new DateTimeFormatterBuilder() - .parseCaseInsensitive() - .appendValue(HOUR_OF_DAY, 2) - .appendLiteral(':') - .appendValue(MINUTE_OF_HOUR, 2) - .appendLiteral(':') - .appendValue(SECOND_OF_MINUTE, 2) - .appendFraction(NANO_OF_SECOND, 3, 9, true) - .appendOffsetId() - .toFormatter(Locale.ROOT); + public static final DateTimeFormatter ISO_DATE_WITH_NANOS = new DateTimeFormatterBuilder().parseCaseInsensitive() + .append(ISO_LOCAL_DATE) + .appendLiteral('T') + .appendValue(HOUR_OF_DAY, 2) + .appendLiteral(':') + .appendValue(MINUTE_OF_HOUR, 2) + .appendLiteral(':') + .appendValue(SECOND_OF_MINUTE, 2) + .appendFraction(NANO_OF_SECOND, 3, 9, true) + .appendOffsetId() + .toFormatter(Locale.ROOT); + + public static final DateTimeFormatter ISO_TIME_WITH_NANOS = new DateTimeFormatterBuilder().parseCaseInsensitive() + .appendValue(HOUR_OF_DAY, 2) + .appendLiteral(':') + .appendValue(MINUTE_OF_HOUR, 2) + .appendLiteral(':') + .appendValue(SECOND_OF_MINUTE, 2) + .appendFraction(NANO_OF_SECOND, 3, 9, true) + .appendOffsetId() + .toFormatter(Locale.ROOT); private static final int SECONDS_PER_MINUTE = 60; private static final int SECONDS_PER_HOUR = SECONDS_PER_MINUTE * 60; diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/TextEsField.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/TextEsField.java index 1d1681a9a28b6..95cd96e8531f8 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/TextEsField.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/TextEsField.java @@ -47,15 +47,19 @@ private Tuple findExact() { for (EsField property : getProperties().values()) { if (property.getDataType() == KEYWORD && property.getExactInfo().hasExact()) { if (field != null) { - return new Tuple<>(null, "Multiple exact keyword candidates available for [" + getName() + - "]; specify which one to use"); + return new Tuple<>( + null, + "Multiple exact keyword candidates available for [" + getName() + "]; specify which one to use" + ); } field = property; } } if (field == null) { - return new Tuple<>(null, "No keyword/multi-field defined exact matches for [" + getName() + - "]; define one or use MATCH/QUERY instead"); + return new Tuple<>( + null, + "No keyword/multi-field defined exact matches for [" + getName() + "]; define one or use MATCH/QUERY instead" + ); } return new Tuple<>(field, null); } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/Types.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/Types.java index e83bfba2afef9..515d70dff05c8 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/Types.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/Types.java @@ -6,8 +6,8 @@ */ package org.elasticsearch.xpack.ql.type; -import org.elasticsearch.core.Booleans; import org.elasticsearch.common.Strings; +import org.elasticsearch.core.Booleans; import java.util.Collections; import java.util.LinkedHashMap; diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/UnsupportedEsField.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/UnsupportedEsField.java index b24908bcfaf4c..c58bc1f6c1200 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/UnsupportedEsField.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/UnsupportedEsField.java @@ -53,8 +53,7 @@ public boolean equals(Object o) { return false; } UnsupportedEsField that = (UnsupportedEsField) o; - return Objects.equals(originalType, that.originalType) - && Objects.equals(inherited, that.inherited); + return Objects.equals(originalType, that.originalType) && Objects.equals(inherited, that.inherited); } @Override diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/util/CollectionUtils.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/util/CollectionUtils.java index e8a5ccdfb4c11..1f5cf65b91c7e 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/util/CollectionUtils.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/util/CollectionUtils.java @@ -53,8 +53,7 @@ public static List combine(Collection... collections) { for (T t : col) { list.add(t); } - } - else { + } else { list.addAll(col); } } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/util/DateUtils.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/util/DateUtils.java index f03f47033a5fb..eca50f367ff9b 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/util/DateUtils.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/util/DateUtils.java @@ -27,7 +27,6 @@ import static java.time.temporal.ChronoField.NANO_OF_SECOND; import static java.time.temporal.ChronoField.SECOND_OF_MINUTE; - //FIXME: Taken from sql-proto (StringUtils) //Ideally it should be shared but the dependencies across projects and and SQL-client make it tricky. // Maybe a gradle task would fix that... @@ -37,29 +36,27 @@ public class DateUtils { public static final String EMPTY = ""; - public static final DateTimeFormatter ISO_DATE_WITH_NANOS = new DateTimeFormatterBuilder() - .parseCaseInsensitive() - .append(ISO_LOCAL_DATE) - .appendLiteral('T') - .appendValue(HOUR_OF_DAY, 2) - .appendLiteral(':') - .appendValue(MINUTE_OF_HOUR, 2) - .appendLiteral(':') - .appendValue(SECOND_OF_MINUTE, 2) - .appendFraction(NANO_OF_SECOND, 3, 9, true) - .appendOffsetId() - .toFormatter(Locale.ROOT); - - public static final DateTimeFormatter ISO_TIME_WITH_NANOS = new DateTimeFormatterBuilder() - .parseCaseInsensitive() - .appendValue(HOUR_OF_DAY, 2) - .appendLiteral(':') - .appendValue(MINUTE_OF_HOUR, 2) - .appendLiteral(':') - .appendValue(SECOND_OF_MINUTE, 2) - .appendFraction(NANO_OF_SECOND, 3, 9, true) - .appendOffsetId() - .toFormatter(Locale.ROOT); + public static final DateTimeFormatter ISO_DATE_WITH_NANOS = new DateTimeFormatterBuilder().parseCaseInsensitive() + .append(ISO_LOCAL_DATE) + .appendLiteral('T') + .appendValue(HOUR_OF_DAY, 2) + .appendLiteral(':') + .appendValue(MINUTE_OF_HOUR, 2) + .appendLiteral(':') + .appendValue(SECOND_OF_MINUTE, 2) + .appendFraction(NANO_OF_SECOND, 3, 9, true) + .appendOffsetId() + .toFormatter(Locale.ROOT); + + public static final DateTimeFormatter ISO_TIME_WITH_NANOS = new DateTimeFormatterBuilder().parseCaseInsensitive() + .appendValue(HOUR_OF_DAY, 2) + .appendLiteral(':') + .appendValue(MINUTE_OF_HOUR, 2) + .appendLiteral(':') + .appendValue(SECOND_OF_MINUTE, 2) + .appendFraction(NANO_OF_SECOND, 3, 9, true) + .appendOffsetId() + .toFormatter(Locale.ROOT); public static final int SECONDS_PER_MINUTE = 60; public static final int SECONDS_PER_HOUR = SECONDS_PER_MINUTE * 60; diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/util/Graphviz.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/util/Graphviz.java index 97a142082e0fc..64cf05b37774d 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/util/Graphviz.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/util/Graphviz.java @@ -22,15 +22,18 @@ public abstract class Graphviz { private static final int CLUSTER_INDENT = 2; private static final int INDENT = 1; - public static String dot(String name, Node root) { StringBuilder sb = new StringBuilder(); // name - sb.append("digraph G { " + sb.append( + "digraph G { " + "rankdir=BT; \n" - + "label=\"" + name + "\"; \n" + + "label=\"" + + name + + "\"; \n" + "node[shape=plaintext, color=azure1];\n " - + "edge[color=black,arrowsize=0.5];\n"); + + "edge[color=black,arrowsize=0.5];\n" + ); handleNode(sb, root, new AtomicInteger(0), INDENT, true); sb.append("}"); return sb.toString(); @@ -41,12 +44,13 @@ public static String dot(Map> clusters, boolean drawSu StringBuilder sb = new StringBuilder(); // name - sb.append("digraph G { " + sb.append( + "digraph G { " + "rankdir=BT;\n " + "node[shape=plaintext, color=azure1];\n " + "edge[color=black];\n " - + "graph[compound=true];\n\n"); - + + "graph[compound=true];\n\n" + ); int clusterNodeStart = 1; int clusterId = 0; @@ -105,7 +109,7 @@ public static String dot(Map> clusters, boolean drawSu // connecting the clusters arranges them in a weird position // so don't - //sb.append(clusterEdges.toString()); + // sb.append(clusterEdges.toString()); // align the cluster by requiring the invisible nodes in each cluster to be of the same rank indent(sb, INDENT); @@ -128,9 +132,7 @@ private static void handleNode(StringBuilder output, Node n, AtomicInteger no indent(nodeInfo, currentIndent + NODE_LABEL_INDENT); nodeInfo.append("

    \n"); indent(nodeInfo, currentIndent + NODE_LABEL_INDENT); - nodeInfo.append("\n"); + nodeInfo.append("\n"); indent(nodeInfo, currentIndent + NODE_LABEL_INDENT); List props = n.nodeProperties(); @@ -142,25 +144,22 @@ private static void handleNode(StringBuilder output, Node n, AtomicInteger no if (v != null && n.children().contains(v) == false) { if (v instanceof Collection) { Collection c = (Collection) v; - StringBuilder colS = new StringBuilder(); - for (Object o : c) { - if (drawSubTrees && isAnotherTree(o)) { - subTrees.add((Node) o); - } - else { - colS.append(o); - colS.append("\n"); - } + StringBuilder colS = new StringBuilder(); + for (Object o : c) { + if (drawSubTrees && isAnotherTree(o)) { + subTrees.add((Node) o); + } else { + colS.append(o); + colS.append("\n"); } - if (colS.length() > 0) { - parsed.add(colS.toString()); - } - } - else { + } + if (colS.length() > 0) { + parsed.add(colS.toString()); + } + } else { if (drawSubTrees && isAnotherTree(v)) { subTrees.add((Node) v); - } - else { + } else { parsed.add(v.toString()); } } @@ -204,7 +203,7 @@ private static void handleNode(StringBuilder output, Node n, AtomicInteger no } indent(output, currentIndent + 1); - //output.append("{ rankdir=LR; rank=same; \n"); + // output.append("{ rankdir=LR; rank=same; \n"); int prevId = -1; // handle children for (Node c : n.children()) { @@ -232,7 +231,7 @@ private static void handleNode(StringBuilder output, Node n, AtomicInteger no prevId = childId; } indent(output, currentIndent); - //output.append("}\n"); + // output.append("}\n"); } private static void drawNodeTree(StringBuilder sb, Node node, String prefix, int counter) { @@ -270,8 +269,7 @@ private static void drawNodeTree(StringBuilder sb, Node node, String prefix, private static void drawNode(StringBuilder sb, Node node, String nodeName) { if (node.children().isEmpty()) { sb.append(nodeName + " [label=\"" + node.toString() + "\"];\n"); - } - else { + } else { sb.append(nodeName + " [label=\"" + node.nodeName() + "\"];\n"); } } @@ -289,12 +287,12 @@ private static boolean isAnotherTree(Object value) { private static String escapeHtml(Object value) { return String.valueOf(value) - .replace("&", "&") - .replace("\"", """) - .replace("'", "'") - .replace("<", "<") - .replace(">", ">") - .replace("\n", "
    "); + .replace("&", "&") + .replace("\"", """) + .replace("'", "'") + .replace("<", "<") + .replace(">", ">") + .replace("\n", "
    "); } private static String quoteGraphviz(String value) { @@ -306,10 +304,7 @@ private static String quoteGraphviz(String value) { } private static String escapeGraphviz(String value) { - return value - .replace("<", "\\<") - .replace(">", "\\>") - .replace("\"", "\\\""); + return value.replace("<", "\\<").replace(">", "\\>").replace("\"", "\\\""); } private static void indent(StringBuilder sb, int indent) { diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/util/Holder.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/util/Holder.java index a4516e68577df..92bf1a8c55eca 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/util/Holder.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/util/Holder.java @@ -15,8 +15,7 @@ public class Holder { private T value = null; - public Holder() { - } + public Holder() {} public Holder(T value) { this.value = value; diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/util/ReflectionUtils.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/util/ReflectionUtils.java index e2baedc4cf89c..93e76c8b412a9 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/util/ReflectionUtils.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/util/ReflectionUtils.java @@ -21,8 +21,11 @@ public static Class detectSuperTypeForRuleLike(Class c) { if (type instanceof ParameterizedType) { Type[] typeArguments = ((ParameterizedType) type).getActualTypeArguments(); if (typeArguments.length != 2 && typeArguments.length != 1) { - throw new QlIllegalArgumentException("Unexpected number of type arguments {} for {}", Arrays.toString(typeArguments), - c); + throw new QlIllegalArgumentException( + "Unexpected number of type arguments {} for {}", + Arrays.toString(typeArguments), + c + ); } Type tp = typeArguments[0]; @@ -49,8 +52,7 @@ public static String ruleLikeNaming(Class c) { if (parentPackage > 0) { int grandParentPackage = className.substring(0, parentPackage).lastIndexOf("."); return (grandParentPackage > 0 ? className.substring(grandParentPackage + 1) : className.substring(parentPackage)); - } - else { + } else { return className; } } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/util/RemoteClusterUtils.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/util/RemoteClusterUtils.java index e49dfdd99207c..825b351f18f19 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/util/RemoteClusterUtils.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/util/RemoteClusterUtils.java @@ -18,14 +18,15 @@ public class RemoteClusterUtils { public static Tuple splitQualifiedIndex(String indexName) { int separatorOffset = indexName.indexOf(REMOTE_CLUSTER_INDEX_SEPARATOR); - return separatorOffset > 0 ? Tuple.tuple(indexName.substring(0, separatorOffset), indexName.substring(separatorOffset + 1)) : - Tuple.tuple(null, indexName); + return separatorOffset > 0 + ? Tuple.tuple(indexName.substring(0, separatorOffset), indexName.substring(separatorOffset + 1)) + : Tuple.tuple(null, indexName); } public static String qualifyAndJoinIndices(String cluster, String[] indices) { StringJoiner sj = new StringJoiner(","); for (String index : indices) { - sj.add(cluster != null ? buildRemoteIndexName(cluster,index) : index); + sj.add(cluster != null ? buildRemoteIndexName(cluster, index) : index); } return sj.toString(); } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/util/StringUtils.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/util/StringUtils.java index 6a5cf27de456e..c1c05b2d2ec96 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/util/StringUtils.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/util/StringUtils.java @@ -10,10 +10,10 @@ import org.apache.lucene.util.CollectionUtil; import org.elasticsearch.common.Strings; import org.elasticsearch.core.Tuple; +import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.xpack.ql.QlIllegalArgumentException; import java.io.IOException; @@ -35,7 +35,7 @@ private StringUtils() {} private static final String[] INTEGER_ORDINALS = new String[] { "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th" }; - //CamelCase to camel_case + // CamelCase to camel_case public static String camelCaseToUnderscore(String string) { if (Strings.hasText(string) == false) { return EMPTY; @@ -52,12 +52,10 @@ public static String camelCaseToUnderscore(String string) { sb.append("_"); } previousCharWasUp = true; - } - else { + } else { previousCharWasUp = (ch == '_'); } - } - else { + } else { previousCharWasUp = true; } sb.append(ch); @@ -65,7 +63,7 @@ public static String camelCaseToUnderscore(String string) { return sb.toString().toUpperCase(Locale.ROOT); } - //CAMEL_CASE to camelCase + // CAMEL_CASE to camelCase public static String underscoreToLowerCamelCase(String string) { if (Strings.hasText(string) == false) { return EMPTY; @@ -78,13 +76,11 @@ public static String underscoreToLowerCamelCase(String string) { char ch = s.charAt(i); if (ch == '_') { previousCharWasUnderscore = true; - } - else { + } else { if (previousCharWasUnderscore) { sb.append(Character.toUpperCase(ch)); previousCharWasUnderscore = false; - } - else { + } else { sb.append(ch); } } @@ -106,11 +102,9 @@ public static String likeToJavaPattern(String pattern, char escape) { if (escaped == false && (curr == escape) && escape != 0) { escaped = true; if (i + 1 == pattern.length()) { - throw new QlIllegalArgumentException( - "Invalid sequence - escape character is not followed by special wildcard char"); + throw new QlIllegalArgumentException("Invalid sequence - escape character is not followed by special wildcard char"); } - } - else { + } else { switch (curr) { case '%': regex.append(escaped ? SQL_WILDCARD : ".*"); @@ -121,7 +115,8 @@ public static String likeToJavaPattern(String pattern, char escape) { default: if (escaped) { throw new QlIllegalArgumentException( - "Invalid sequence - escape character is not followed by special wildcard char"); + "Invalid sequence - escape character is not followed by special wildcard char" + ); } // escape special regex characters switch (curr) { @@ -184,14 +179,15 @@ public static String likeToLuceneWildcard(String pattern, char escape) { default: if (escaped) { throw new QlIllegalArgumentException( - "Invalid sequence - escape character is not followed by special wildcard char"); + "Invalid sequence - escape character is not followed by special wildcard char" + ); } // escape special regex characters switch (curr) { case '\\': case '*': case '?': - wildcard.append('\\'); + wildcard.append('\\'); } wildcard.append(curr); } @@ -230,7 +226,8 @@ public static String likeToIndexWildcard(String pattern, char escape) { default: if (escaped) { throw new QlIllegalArgumentException( - "Invalid sequence - escape character is not followed by special wildcard char"); + "Invalid sequence - escape character is not followed by special wildcard char" + ); } // the resolver doesn't support escaping... wildcard.append(curr); @@ -287,10 +284,8 @@ public static List findSimilar(String match, Iterable potentialM scoredMatches.add(new Tuple<>(distance, potentialMatch)); } } - CollectionUtil.timSort(scoredMatches, (a,b) -> b.v1().compareTo(a.v1())); - return scoredMatches.stream() - .map(a -> a.v2()) - .collect(toList()); + CollectionUtil.timSort(scoredMatches, (a, b) -> b.v1().compareTo(a.v1())); + return scoredMatches.stream().map(a -> a.v2()).collect(toList()); } public static double parseDouble(String string) throws QlIllegalArgumentException { diff --git a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/action/QlStatusResponseTests.java b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/action/QlStatusResponseTests.java index e482a4382c2d4..c107e5a256bde 100644 --- a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/action/QlStatusResponseTests.java +++ b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/action/QlStatusResponseTests.java @@ -8,11 +8,11 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.test.AbstractWireSerializingTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.rest.RestStatus; -import org.elasticsearch.test.AbstractWireSerializingTestCase; import org.elasticsearch.xpack.ql.async.QlStatusResponse; import java.io.IOException; @@ -58,25 +58,31 @@ protected QlStatusResponse mutateInstance(QlStatusResponse instance) { public void testToXContent() throws IOException { QlStatusResponse response = createTestInstance(); try (XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent())) { - String expectedJson = "{\n" + - " \"id\" : \"" + response.getId() + "\",\n" + - " \"is_running\" : " + response.isRunning() + ",\n" + - " \"is_partial\" : " + response.isPartial() + ",\n"; + String expectedJson = "{\n" + + " \"id\" : \"" + + response.getId() + + "\",\n" + + " \"is_running\" : " + + response.isRunning() + + ",\n" + + " \"is_partial\" : " + + response.isPartial() + + ",\n"; if (response.getStartTime() != null) { - expectedJson = expectedJson + - " \"start_time_in_millis\" : " + response.getStartTime() + ",\n"; + expectedJson = expectedJson + " \"start_time_in_millis\" : " + response.getStartTime() + ",\n"; } - expectedJson = expectedJson + - " \"expiration_time_in_millis\" : " + response.getExpirationTime(); + expectedJson = expectedJson + " \"expiration_time_in_millis\" : " + response.getExpirationTime(); if (response.getCompletionStatus() == null) { - expectedJson = expectedJson + "\n" + - "}"; + expectedJson = expectedJson + "\n" + "}"; } else { - expectedJson = expectedJson + ",\n" + - " \"completion_status\" : " + response.getCompletionStatus().getStatus() + "\n" + - "}"; + expectedJson = expectedJson + + ",\n" + + " \"completion_status\" : " + + response.getCompletionStatus().getStatus() + + "\n" + + "}"; } builder.prettyPrint(); response.toXContent(builder, ToXContent.EMPTY_PARAMS); diff --git a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/async/AsyncTaskManagementServiceTests.java b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/async/AsyncTaskManagementServiceTests.java index 068f7900f2860..8020d49e4766d 100644 --- a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/async/AsyncTaskManagementServiceTests.java +++ b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/async/AsyncTaskManagementServiceTests.java @@ -86,8 +86,17 @@ public void writeTo(StreamOutput out) throws IOException { public static class TestTask extends StoredAsyncTask { public volatile AtomicReference finalResponse = new AtomicReference<>(); - public TestTask(long id, String type, String action, String description, TaskId parentTaskId, Map headers, - Map originHeaders, AsyncExecutionId asyncExecutionId, TimeValue keepAlive) { + public TestTask( + long id, + String type, + String action, + String description, + TaskId parentTaskId, + Map headers, + Map originHeaders, + AsyncExecutionId asyncExecutionId, + TimeValue keepAlive + ) { super(id, type, action, description, parentTaskId, headers, originHeaders, asyncExecutionId, keepAlive); } @@ -100,10 +109,27 @@ public TestResponse getCurrentResult() { public static class TestOperation implements AsyncTaskManagementService.AsyncOperation { @Override - public TestTask createTask(TestRequest request, long id, String type, String action, TaskId parentTaskId, - Map headers, Map originHeaders, AsyncExecutionId asyncExecutionId) { - return new TestTask(id, type, action, request.getDescription(), parentTaskId, headers, originHeaders, asyncExecutionId, - TimeValue.timeValueDays(5)); + public TestTask createTask( + TestRequest request, + long id, + String type, + String action, + TaskId parentTaskId, + Map headers, + Map originHeaders, + AsyncExecutionId asyncExecutionId + ) { + return new TestTask( + id, + type, + action, + request.getDescription(), + parentTaskId, + headers, + originHeaders, + asyncExecutionId, + TimeValue.timeValueDays(5) + ); } @Override @@ -133,12 +159,24 @@ public void setup() { clusterService = getInstanceFromNode(ClusterService.class); transportService = getInstanceFromNode(TransportService.class); BigArrays bigArrays = getInstanceFromNode(BigArrays.class); - AsyncTaskIndexService> store = - new AsyncTaskIndexService<>(index, clusterService, transportService.getThreadPool().getThreadContext(), client(), "test", - in -> new StoredAsyncResponse<>(TestResponse::new, in), writableRegistry(), bigArrays); - results = new AsyncResultsService<>(store, true, TestTask.class, + AsyncTaskIndexService> store = new AsyncTaskIndexService<>( + index, + clusterService, + transportService.getThreadPool().getThreadContext(), + client(), + "test", + in -> new StoredAsyncResponse<>(TestResponse::new, in), + writableRegistry(), + bigArrays + ); + results = new AsyncResultsService<>( + store, + true, + TestTask.class, (task, listener, timeout) -> addCompletionListener(transportService.getThreadPool(), task, listener, timeout), - transportService.getTaskManager(), clusterService); + transportService.getTaskManager(), + clusterService + ); } /** @@ -150,11 +188,22 @@ public void shutdownExec() { } private AsyncTaskManagementService createManagementService( - AsyncTaskManagementService.AsyncOperation operation) { + AsyncTaskManagementService.AsyncOperation operation + ) { BigArrays bigArrays = getInstanceFromNode(BigArrays.class); - return new AsyncTaskManagementService<>(index, client(), "test_origin", writableRegistry(), - transportService.getTaskManager(), "test_action", operation, TestTask.class, clusterService, transportService.getThreadPool(), - bigArrays); + return new AsyncTaskManagementService<>( + index, + client(), + "test_origin", + writableRegistry(), + transportService.getTaskManager(), + "test_action", + operation, + TestTask.class, + clusterService, + transportService.getThreadPool(), + bigArrays + ); } public void testReturnBeforeTimeout() throws Exception { @@ -163,7 +212,11 @@ public void testReturnBeforeTimeout() throws Exception { boolean keepOnCompletion = randomBoolean(); CountDownLatch latch = new CountDownLatch(1); TestRequest request = new TestRequest(success ? randomAlphaOfLength(10) : "die"); - service.asyncExecute(request, TimeValue.timeValueMinutes(1), TimeValue.timeValueMinutes(10), keepOnCompletion, + service.asyncExecute( + request, + TimeValue.timeValueMinutes(1), + TimeValue.timeValueMinutes(10), + keepOnCompletion, ActionListener.wrap(r -> { assertThat(success, equalTo(true)); assertThat(r.string, equalTo("response for [" + request.string + "]")); @@ -173,7 +226,8 @@ public void testReturnBeforeTimeout() throws Exception { assertThat(success, equalTo(false)); assertThat(e.getMessage(), equalTo("test exception")); latch.countDown(); - })); + }) + ); assertThat(latch.await(10, TimeUnit.SECONDS), equalTo(true)); } @@ -199,13 +253,18 @@ public void execute(TestRequest request, TestTask task, ActionListener responseHolder = new AtomicReference<>(); - service.asyncExecute(request, TimeValue.timeValueMillis(1), TimeValue.timeValueMinutes(10), keepOnCompletion, + service.asyncExecute( + request, + TimeValue.timeValueMillis(1), + TimeValue.timeValueMinutes(10), + keepOnCompletion, ActionListener.wrap(r -> { assertThat(r.string, nullValue()); assertThat(r.id, notNullValue()); assertThat(responseHolder.getAndSet(r), nullValue()); latch.countDown(); - }, e -> fail("Shouldn't be here"))); + }, e -> fail("Shouldn't be here")) + ); assertThat(latch.await(20, TimeUnit.SECONDS), equalTo(true)); if (timeoutOnFirstAttempt) { @@ -222,8 +281,11 @@ public void execute(TestRequest request, TestTask task, ActionListener> responseRef = new AtomicReference<>(); - CountDownLatch getResponseCountDown = getResponse(responseHolder.get().id, TimeValue.timeValueSeconds(5), - ActionListener.wrap(responseRef::set, e -> fail("Shouldn't be here"))); + CountDownLatch getResponseCountDown = getResponse( + responseHolder.get().id, + TimeValue.timeValueSeconds(5), + ActionListener.wrap(responseRef::set, e -> fail("Shouldn't be here")) + ); executionLatch.countDown(); assertThat(getResponseCountDown.await(10, TimeUnit.SECONDS), equalTo(true)); @@ -272,22 +334,16 @@ private StoredAsyncResponse getResponse(String id, TimeValue timeo return response.get(); } - private CountDownLatch getResponse(String id, - TimeValue timeout, - ActionListener> listener) { + private CountDownLatch getResponse(String id, TimeValue timeout, ActionListener> listener) { CountDownLatch responseLatch = new CountDownLatch(1); - GetAsyncResultRequest getResultsRequest = new GetAsyncResultRequest(id) - .setWaitForCompletionTimeout(timeout); - results.retrieveResult(getResultsRequest, ActionListener.wrap( - r -> { - listener.onResponse(r); - responseLatch.countDown(); - }, - e -> { - listener.onFailure(e); - responseLatch.countDown(); - } - )); + GetAsyncResultRequest getResultsRequest = new GetAsyncResultRequest(id).setWaitForCompletionTimeout(timeout); + results.retrieveResult(getResultsRequest, ActionListener.wrap(r -> { + listener.onResponse(r); + responseLatch.countDown(); + }, e -> { + listener.onFailure(e); + responseLatch.countDown(); + })); return responseLatch; } diff --git a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/async/StoredAsyncResponseTests.java b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/async/StoredAsyncResponseTests.java index ef1cd3b94f531..845c6987c0147 100644 --- a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/async/StoredAsyncResponseTests.java +++ b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/async/StoredAsyncResponseTests.java @@ -11,9 +11,9 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.search.SearchModule; import org.elasticsearch.test.AbstractWireSerializingTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.async.StoredAsyncResponse; import java.io.IOException; diff --git a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/execution/search/extractor/ConstantExtractorTests.java b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/execution/search/extractor/ConstantExtractorTests.java index 3418ef5897848..b116916613fa1 100644 --- a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/execution/search/extractor/ConstantExtractorTests.java +++ b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/execution/search/extractor/ConstantExtractorTests.java @@ -20,10 +20,7 @@ public static ConstantExtractor randomConstantExtractor() { private static Object randomValidConstant() { @SuppressWarnings("unchecked") - Supplier valueSupplier = randomFrom( - () -> randomInt(), - () -> randomDouble(), - () -> randomAlphaOfLengthBetween(1, 140)); + Supplier valueSupplier = randomFrom(() -> randomInt(), () -> randomDouble(), () -> randomAlphaOfLengthBetween(1, 140)); return valueSupplier.get(); } diff --git a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/AttributeMapTests.java b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/AttributeMapTests.java index 9045b96324d97..00dd387a66c91 100644 --- a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/AttributeMapTests.java +++ b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/AttributeMapTests.java @@ -77,7 +77,7 @@ public void testResolve() { builder.put(threeAlias.toAttribute(), threeAlias.child()); builder.put(threeAliasAlias.toAttribute(), threeAliasAlias.child()); AttributeMap map = builder.build(); - + assertEquals(of("one"), map.resolve(one)); assertEquals("two", map.resolve(two)); assertEquals(of("three"), map.resolve(three)); @@ -115,11 +115,9 @@ public void testResolveMultiHopCycle() { builder.put(d, a); AttributeMap map = builder.build(); - // note: multi hop cycles should not happen, unless we have a + // note: multi hop cycles should not happen, unless we have a // bug in the code that populates the AttributeMaps - expectThrows(QlIllegalArgumentException.class, () -> { - assertEquals(a, map.resolve(a, c)); - }); + expectThrows(QlIllegalArgumentException.class, () -> { assertEquals(a, map.resolve(a, c)); }); } private Alias createIntParameterAlias(int index, int value) { @@ -241,7 +239,6 @@ public void testEntrySet() { assertThat(keys, hasSize(3)); - assertThat(values, hasSize(3)); assertThat(values, contains("one", "two", "three")); } diff --git a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/LiteralTests.java b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/LiteralTests.java index 7749cf71ecc67..c71f34307a6bf 100644 --- a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/LiteralTests.java +++ b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/LiteralTests.java @@ -40,6 +40,7 @@ static class ValueAndCompatibleTypes { this.validDataTypes = Arrays.asList(validDataTypes); } } + /** * Generators for values and data types. The first valid * data type is special it is used when picking a generator @@ -47,14 +48,15 @@ static class ValueAndCompatibleTypes { * after a generators is its "native" type. */ private static final List GENERATORS = Arrays.asList( - new ValueAndCompatibleTypes(() -> randomBoolean() ? randomBoolean() : randomFrom("true", "false"), BOOLEAN), - new ValueAndCompatibleTypes(ESTestCase::randomByte, BYTE, SHORT, INTEGER, LONG, FLOAT, DOUBLE, BOOLEAN), - new ValueAndCompatibleTypes(ESTestCase::randomShort, SHORT, INTEGER, LONG, FLOAT, DOUBLE, BOOLEAN), - new ValueAndCompatibleTypes(ESTestCase::randomInt, INTEGER, LONG, FLOAT, DOUBLE, BOOLEAN), - new ValueAndCompatibleTypes(ESTestCase::randomLong, LONG, FLOAT, DOUBLE, BOOLEAN), - new ValueAndCompatibleTypes(ESTestCase::randomFloat, FLOAT, LONG, DOUBLE, BOOLEAN), - new ValueAndCompatibleTypes(ESTestCase::randomDouble, DOUBLE, LONG, FLOAT, BOOLEAN), - new ValueAndCompatibleTypes(() -> randomAlphaOfLength(5), KEYWORD)); + new ValueAndCompatibleTypes(() -> randomBoolean() ? randomBoolean() : randomFrom("true", "false"), BOOLEAN), + new ValueAndCompatibleTypes(ESTestCase::randomByte, BYTE, SHORT, INTEGER, LONG, FLOAT, DOUBLE, BOOLEAN), + new ValueAndCompatibleTypes(ESTestCase::randomShort, SHORT, INTEGER, LONG, FLOAT, DOUBLE, BOOLEAN), + new ValueAndCompatibleTypes(ESTestCase::randomInt, INTEGER, LONG, FLOAT, DOUBLE, BOOLEAN), + new ValueAndCompatibleTypes(ESTestCase::randomLong, LONG, FLOAT, DOUBLE, BOOLEAN), + new ValueAndCompatibleTypes(ESTestCase::randomFloat, FLOAT, LONG, DOUBLE, BOOLEAN), + new ValueAndCompatibleTypes(ESTestCase::randomDouble, DOUBLE, LONG, FLOAT, BOOLEAN), + new ValueAndCompatibleTypes(() -> randomAlphaOfLength(5), KEYWORD) + ); public static Literal randomLiteral() { ValueAndCompatibleTypes gen = randomFrom(GENERATORS); @@ -92,15 +94,19 @@ public void testTransform() { // Replace value Object newValue = randomValueOfTypeOtherThan(literal.value(), literal.dataType()); - assertEquals(new Literal(literal.source(), newValue, literal.dataType()), - literal.transformPropertiesOnly(Object.class, p -> p == literal.value() ? newValue : p)); + assertEquals( + new Literal(literal.source(), newValue, literal.dataType()), + literal.transformPropertiesOnly(Object.class, p -> p == literal.value() ? newValue : p) + ); // Replace data type if there are more compatible data types List validDataTypes = validReplacementDataTypes(literal.value(), literal.dataType()); if (validDataTypes.size() > 1) { DataType newDataType = randomValueOtherThan(literal.dataType(), () -> randomFrom(validDataTypes)); - assertEquals(new Literal(literal.source(), literal.value(), newDataType), - literal.transformPropertiesOnly(DataType.class, p -> newDataType)); + assertEquals( + new Literal(literal.source(), literal.value(), newDataType), + literal.transformPropertiesOnly(DataType.class, p -> newDataType) + ); } } @@ -121,8 +127,7 @@ private Object randomValueOfTypeOtherThan(Object original, DataType type) { private List validReplacementDataTypes(Object value, DataType type) { List validDataTypes = new ArrayList<>(); - List options = Arrays.asList(BYTE, SHORT, INTEGER, LONG, - FLOAT, DOUBLE, BOOLEAN); + List options = Arrays.asList(BYTE, SHORT, INTEGER, LONG, FLOAT, DOUBLE, BOOLEAN); for (DataType candidate : options) { try { Converter c = DataTypeConverter.converterFor(type, candidate); diff --git a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/UnresolvedAttributeTests.java b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/UnresolvedAttributeTests.java index 634910af1e3de..84bb742ed760c 100644 --- a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/UnresolvedAttributeTests.java +++ b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/UnresolvedAttributeTests.java @@ -48,19 +48,35 @@ protected UnresolvedAttribute randomInstance() { @Override protected UnresolvedAttribute mutate(UnresolvedAttribute a) { - Supplier option = randomFrom(Arrays.asList( - () -> new UnresolvedAttribute(a.source(), + Supplier option = randomFrom( + Arrays.asList( + () -> new UnresolvedAttribute( + a.source(), randomValueOtherThan(a.name(), () -> randomAlphaOfLength(5)), - a.qualifier(), a.id(), a.unresolvedMessage(), a.resolutionMetadata()), - () -> new UnresolvedAttribute(a.source(), a.name(), + a.qualifier(), + a.id(), + a.unresolvedMessage(), + a.resolutionMetadata() + ), + () -> new UnresolvedAttribute( + a.source(), + a.name(), randomValueOtherThan(a.qualifier(), UnresolvedAttributeTests::randomQualifier), - a.id(), a.unresolvedMessage(), a.resolutionMetadata()), - () -> new UnresolvedAttribute(a.source(), a.name(), a.qualifier(), a.id(), + a.id(), + a.unresolvedMessage(), + a.resolutionMetadata() + ), + () -> new UnresolvedAttribute( + a.source(), + a.name(), + a.qualifier(), + a.id(), randomValueOtherThan(a.unresolvedMessage(), () -> randomUnresolvedMessage()), - a.resolutionMetadata()), - () -> new UnresolvedAttribute(a.source(), a.name(), - a.qualifier(), a.id(), a.unresolvedMessage(), new Object()) - )); + a.resolutionMetadata() + ), + () -> new UnresolvedAttribute(a.source(), a.name(), a.qualifier(), a.id(), a.unresolvedMessage(), new Object()) + ) + ); return option.get(); } @@ -74,29 +90,34 @@ public void testTransform() { UnresolvedAttribute a = randomUnresolvedAttribute(); String newName = randomValueOtherThan(a.name(), () -> randomAlphaOfLength(5)); - assertEquals(new UnresolvedAttribute(a.source(), newName, a.qualifier(), a.id(), - a.unresolvedMessage(), a.resolutionMetadata()), - a.transformPropertiesOnly(Object.class, v -> Objects.equals(v, a.name()) ? newName : v)); + assertEquals( + new UnresolvedAttribute(a.source(), newName, a.qualifier(), a.id(), a.unresolvedMessage(), a.resolutionMetadata()), + a.transformPropertiesOnly(Object.class, v -> Objects.equals(v, a.name()) ? newName : v) + ); String newQualifier = randomValueOtherThan(a.qualifier(), UnresolvedAttributeTests::randomQualifier); - assertEquals(new UnresolvedAttribute(a.source(), a.name(), newQualifier, a.id(), - a.unresolvedMessage(), a.resolutionMetadata()), - a.transformPropertiesOnly(Object.class, v -> Objects.equals(v, a.qualifier()) ? newQualifier : v)); + assertEquals( + new UnresolvedAttribute(a.source(), a.name(), newQualifier, a.id(), a.unresolvedMessage(), a.resolutionMetadata()), + a.transformPropertiesOnly(Object.class, v -> Objects.equals(v, a.qualifier()) ? newQualifier : v) + ); NameId newId = new NameId(); - assertEquals(new UnresolvedAttribute(a.source(), a.name(), a.qualifier(), newId, - a.unresolvedMessage(), a.resolutionMetadata()), - a.transformPropertiesOnly(Object.class, v -> Objects.equals(v, a.id()) ? newId : v)); + assertEquals( + new UnresolvedAttribute(a.source(), a.name(), a.qualifier(), newId, a.unresolvedMessage(), a.resolutionMetadata()), + a.transformPropertiesOnly(Object.class, v -> Objects.equals(v, a.id()) ? newId : v) + ); String newMessage = randomValueOtherThan(a.unresolvedMessage(), UnresolvedAttributeTests::randomUnresolvedMessage); - assertEquals(new UnresolvedAttribute(a.source(), a.name(), a.qualifier(), a.id(), - newMessage, a.resolutionMetadata()), - a.transformPropertiesOnly(Object.class, v -> Objects.equals(v, a.unresolvedMessage()) ? newMessage : v)); + assertEquals( + new UnresolvedAttribute(a.source(), a.name(), a.qualifier(), a.id(), newMessage, a.resolutionMetadata()), + a.transformPropertiesOnly(Object.class, v -> Objects.equals(v, a.unresolvedMessage()) ? newMessage : v) + ); Object newMeta = new Object(); - assertEquals(new UnresolvedAttribute(a.source(), a.name(), a.qualifier(), a.id(), - a.unresolvedMessage(), newMeta), - a.transformPropertiesOnly(Object.class, v -> Objects.equals(v, a.resolutionMetadata()) ? newMeta : v)); + assertEquals( + new UnresolvedAttribute(a.source(), a.name(), a.qualifier(), a.id(), a.unresolvedMessage(), newMeta), + a.transformPropertiesOnly(Object.class, v -> Objects.equals(v, a.resolutionMetadata()) ? newMeta : v) + ); } @Override diff --git a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/function/FunctionRegistryTests.java b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/function/FunctionRegistryTests.java index 67c2ccaf3efd3..169227dcf73e1 100644 --- a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/function/FunctionRegistryTests.java +++ b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/function/FunctionRegistryTests.java @@ -49,13 +49,14 @@ public void testUnaryFunction() { assertEquals(ur.source(), ur.buildResolved(randomConfiguration(), def).source()); // No children aren't supported - ParsingException e = expectThrows(ParsingException.class, () -> - uf(DEFAULT).buildResolved(randomConfiguration(), def)); + ParsingException e = expectThrows(ParsingException.class, () -> uf(DEFAULT).buildResolved(randomConfiguration(), def)); assertThat(e.getMessage(), endsWith("expects exactly one argument")); // Multiple children aren't supported - e = expectThrows(ParsingException.class, () -> - uf(DEFAULT, mock(Expression.class), mock(Expression.class)).buildResolved(randomConfiguration(), def)); + e = expectThrows( + ParsingException.class, + () -> uf(DEFAULT, mock(Expression.class), mock(Expression.class)).buildResolved(randomConfiguration(), def) + ); assertThat(e.getMessage(), endsWith("expects exactly one argument")); } @@ -77,40 +78,50 @@ public void testBinaryFunction() { assertEquals(ur.source(), ur.buildResolved(randomConfiguration(), def).source()); // No children aren't supported - ParsingException e = expectThrows(ParsingException.class, () -> - uf(DEFAULT).buildResolved(randomConfiguration(), def)); + ParsingException e = expectThrows(ParsingException.class, () -> uf(DEFAULT).buildResolved(randomConfiguration(), def)); assertThat(e.getMessage(), endsWith("expects exactly two arguments")); // One child isn't supported - e = expectThrows(ParsingException.class, () -> - uf(DEFAULT, mock(Expression.class)).buildResolved(randomConfiguration(), def)); + e = expectThrows(ParsingException.class, () -> uf(DEFAULT, mock(Expression.class)).buildResolved(randomConfiguration(), def)); assertThat(e.getMessage(), endsWith("expects exactly two arguments")); // Many children aren't supported - e = expectThrows(ParsingException.class, () -> - uf(DEFAULT, mock(Expression.class), mock(Expression.class), mock(Expression.class)) - .buildResolved(randomConfiguration(), def)); + e = expectThrows( + ParsingException.class, + () -> uf(DEFAULT, mock(Expression.class), mock(Expression.class), mock(Expression.class)).buildResolved( + randomConfiguration(), + def + ) + ); assertThat(e.getMessage(), endsWith("expects exactly two arguments")); } public void testAliasNameIsTheSameAsAFunctionName() { FunctionRegistry r = new FunctionRegistry(def(DummyFunction.class, DummyFunction::new, "DUMMY_FUNCTION", "ALIAS")); - QlIllegalArgumentException iae = expectThrows(QlIllegalArgumentException.class, () -> - r.register(def(DummyFunction2.class, DummyFunction2::new, "DUMMY_FUNCTION2", "DUMMY_FUNCTION"))); + QlIllegalArgumentException iae = expectThrows( + QlIllegalArgumentException.class, + () -> r.register(def(DummyFunction2.class, DummyFunction2::new, "DUMMY_FUNCTION2", "DUMMY_FUNCTION")) + ); assertEquals("alias [DUMMY_FUNCTION] is used by [DUMMY_FUNCTION] and [DUMMY_FUNCTION2]", iae.getMessage()); } public void testDuplicateAliasInTwoDifferentFunctionsFromTheSameBatch() { - QlIllegalArgumentException iae = expectThrows(QlIllegalArgumentException.class, () -> - new FunctionRegistry(def(DummyFunction.class, DummyFunction::new, "DUMMY_FUNCTION", "ALIAS"), - def(DummyFunction2.class, DummyFunction2::new, "DUMMY_FUNCTION2", "ALIAS"))); + QlIllegalArgumentException iae = expectThrows( + QlIllegalArgumentException.class, + () -> new FunctionRegistry( + def(DummyFunction.class, DummyFunction::new, "DUMMY_FUNCTION", "ALIAS"), + def(DummyFunction2.class, DummyFunction2::new, "DUMMY_FUNCTION2", "ALIAS") + ) + ); assertEquals("alias [ALIAS] is used by [DUMMY_FUNCTION(ALIAS)] and [DUMMY_FUNCTION2]", iae.getMessage()); } public void testDuplicateAliasInTwoDifferentFunctionsFromTwoDifferentBatches() { FunctionRegistry r = new FunctionRegistry(def(DummyFunction.class, DummyFunction::new, "DUMMY_FUNCTION", "ALIAS")); - QlIllegalArgumentException iae = expectThrows(QlIllegalArgumentException.class, () -> - r.register(def(DummyFunction2.class, DummyFunction2::new, "DUMMY_FUNCTION2", "ALIAS"))); + QlIllegalArgumentException iae = expectThrows( + QlIllegalArgumentException.class, + () -> r.register(def(DummyFunction2.class, DummyFunction2::new, "DUMMY_FUNCTION2", "ALIAS")) + ); assertEquals("alias [ALIAS] is used by [DUMMY_FUNCTION] and [DUMMY_FUNCTION2]", iae.getMessage()); } @@ -145,15 +156,14 @@ public void testFunctionResolving() { assertEquals(ur.source(), ur.buildResolved(randomConfiguration(), def).source()); // Not resolved - QlIllegalArgumentException e = expectThrows(QlIllegalArgumentException.class, - () -> r.resolveFunction(r.resolveAlias("DummyFunction"))); - assertThat(e.getMessage(), - is("Cannot find function DUMMYFUNCTION; this should have been caught during analysis")); - - e = expectThrows(QlIllegalArgumentException.class, - () -> r.resolveFunction(r.resolveAlias("dummyFunction"))); - assertThat(e.getMessage(), - is("Cannot find function DUMMYFUNCTION; this should have been caught during analysis")); + QlIllegalArgumentException e = expectThrows( + QlIllegalArgumentException.class, + () -> r.resolveFunction(r.resolveAlias("DummyFunction")) + ); + assertThat(e.getMessage(), is("Cannot find function DUMMYFUNCTION; this should have been caught during analysis")); + + e = expectThrows(QlIllegalArgumentException.class, () -> r.resolveFunction(r.resolveAlias("dummyFunction"))); + assertThat(e.getMessage(), is("Cannot find function DUMMYFUNCTION; this should have been caught during analysis")); } public static UnresolvedFunction uf(FunctionResolutionStrategy resolutionStrategy, Expression... children) { diff --git a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/function/UnresolvedFunctionTests.java b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/function/UnresolvedFunctionTests.java index 0a5405fd84460..1710a706d387c 100644 --- a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/function/UnresolvedFunctionTests.java +++ b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/function/UnresolvedFunctionTests.java @@ -42,11 +42,13 @@ protected List resolutionStrategies() { private static List randomFunctionArgs() { // At this point we only support functions with 0, 1, or 2 arguments. - Supplier> option = randomFrom(asList( - Collections::emptyList, - () -> singletonList(randomUnresolvedAttribute()), - () -> asList(randomUnresolvedAttribute(), randomUnresolvedAttribute()) - )); + Supplier> option = randomFrom( + asList( + Collections::emptyList, + () -> singletonList(randomUnresolvedAttribute()), + () -> asList(randomUnresolvedAttribute(), randomUnresolvedAttribute()) + ) + ); return option.get(); } @@ -67,27 +69,63 @@ protected UnresolvedFunction randomInstance() { @Override protected UnresolvedFunction mutate(UnresolvedFunction uf) { - Supplier option = randomFrom(asList( - () -> new UnresolvedFunction(uf.source(), randomValueOtherThan(uf.name(), () -> randomAlphaOfLength(5)), - uf.resolutionStrategy(), uf.children(), uf.analyzed(), uf.unresolvedMessage()), - () -> new UnresolvedFunction(uf.source(), uf.name(), - randomValueOtherThan(uf.resolutionStrategy(), () -> randomFrom(resolutionStrategies())), - uf.children(), uf.analyzed(), uf.unresolvedMessage()), - () -> new UnresolvedFunction(uf.source(), uf.name(), uf.resolutionStrategy(), - randomValueOtherThan(uf.children(), UnresolvedFunctionTests::randomFunctionArgs), - uf.analyzed(), uf.unresolvedMessage()), - () -> new UnresolvedFunction(uf.source(), uf.name(), uf.resolutionStrategy(), uf.children(), - uf.analyzed() == false, uf.unresolvedMessage()), - () -> new UnresolvedFunction(uf.source(), uf.name(), uf.resolutionStrategy(), uf.children(), - uf.analyzed(), randomValueOtherThan(uf.unresolvedMessage(), () -> randomAlphaOfLength(5))) - )); + Supplier option = randomFrom( + asList( + () -> new UnresolvedFunction( + uf.source(), + randomValueOtherThan(uf.name(), () -> randomAlphaOfLength(5)), + uf.resolutionStrategy(), + uf.children(), + uf.analyzed(), + uf.unresolvedMessage() + ), + () -> new UnresolvedFunction( + uf.source(), + uf.name(), + randomValueOtherThan(uf.resolutionStrategy(), () -> randomFrom(resolutionStrategies())), + uf.children(), + uf.analyzed(), + uf.unresolvedMessage() + ), + () -> new UnresolvedFunction( + uf.source(), + uf.name(), + uf.resolutionStrategy(), + randomValueOtherThan(uf.children(), UnresolvedFunctionTests::randomFunctionArgs), + uf.analyzed(), + uf.unresolvedMessage() + ), + () -> new UnresolvedFunction( + uf.source(), + uf.name(), + uf.resolutionStrategy(), + uf.children(), + uf.analyzed() == false, + uf.unresolvedMessage() + ), + () -> new UnresolvedFunction( + uf.source(), + uf.name(), + uf.resolutionStrategy(), + uf.children(), + uf.analyzed(), + randomValueOtherThan(uf.unresolvedMessage(), () -> randomAlphaOfLength(5)) + ) + ) + ); return option.get(); } @Override protected UnresolvedFunction copy(UnresolvedFunction uf) { - return new UnresolvedFunction(uf.source(), uf.name(), uf.resolutionStrategy(), uf.children(), - uf.analyzed(), uf.unresolvedMessage()); + return new UnresolvedFunction( + uf.source(), + uf.name(), + uf.resolutionStrategy(), + uf.children(), + uf.analyzed(), + uf.unresolvedMessage() + ); } @Override @@ -95,22 +133,32 @@ public void testTransform() { UnresolvedFunction uf = randomUnresolvedFunction(); String newName = randomValueOtherThan(uf.name(), () -> randomAlphaOfLength(5)); - assertEquals(new UnresolvedFunction(uf.source(), newName, uf.resolutionStrategy(), uf.children(), - uf.analyzed(), uf.unresolvedMessage()), - uf.transformPropertiesOnly(Object.class, p -> Objects.equals(p, uf.name()) ? newName : p)); - FunctionResolutionStrategy newResolution = randomValueOtherThan(uf.resolutionStrategy(), - () -> randomFrom(resolutionStrategies())); - assertEquals(new UnresolvedFunction(uf.source(), uf.name(), newResolution, uf.children(), - uf.analyzed(), uf.unresolvedMessage()), - uf.transformPropertiesOnly(Object.class, p -> Objects.equals(p, uf.resolutionStrategy()) ? newResolution : p)); + assertEquals( + new UnresolvedFunction(uf.source(), newName, uf.resolutionStrategy(), uf.children(), uf.analyzed(), uf.unresolvedMessage()), + uf.transformPropertiesOnly(Object.class, p -> Objects.equals(p, uf.name()) ? newName : p) + ); + FunctionResolutionStrategy newResolution = randomValueOtherThan(uf.resolutionStrategy(), () -> randomFrom(resolutionStrategies())); + assertEquals( + new UnresolvedFunction(uf.source(), uf.name(), newResolution, uf.children(), uf.analyzed(), uf.unresolvedMessage()), + uf.transformPropertiesOnly(Object.class, p -> Objects.equals(p, uf.resolutionStrategy()) ? newResolution : p) + ); String newUnresolvedMessage = randomValueOtherThan(uf.unresolvedMessage(), UnresolvedFunctionTests::randomUnresolvedMessage); - assertEquals(new UnresolvedFunction(uf.source(), uf.name(), uf.resolutionStrategy(), uf.children(), - uf.analyzed(), newUnresolvedMessage), - uf.transformPropertiesOnly(Object.class, p -> Objects.equals(p, uf.unresolvedMessage()) ? newUnresolvedMessage : p)); - - assertEquals(new UnresolvedFunction(uf.source(), uf.name(), uf.resolutionStrategy(), uf.children(), - uf.analyzed() == false, uf.unresolvedMessage()), - uf.transformPropertiesOnly(Object.class, p -> Objects.equals(p, uf.analyzed()) ? uf.analyzed() == false : p)); + assertEquals( + new UnresolvedFunction(uf.source(), uf.name(), uf.resolutionStrategy(), uf.children(), uf.analyzed(), newUnresolvedMessage), + uf.transformPropertiesOnly(Object.class, p -> Objects.equals(p, uf.unresolvedMessage()) ? newUnresolvedMessage : p) + ); + + assertEquals( + new UnresolvedFunction( + uf.source(), + uf.name(), + uf.resolutionStrategy(), + uf.children(), + uf.analyzed() == false, + uf.unresolvedMessage() + ), + uf.transformPropertiesOnly(Object.class, p -> Objects.equals(p, uf.analyzed()) ? uf.analyzed() == false : p) + ); } @@ -119,8 +167,9 @@ public void testReplaceChildren() { UnresolvedFunction uf = randomUnresolvedFunction(); List newChildren = randomValueOtherThan(uf.children(), UnresolvedFunctionTests::randomFunctionArgs); - assertEquals(new UnresolvedFunction(uf.source(), uf.name(), uf.resolutionStrategy(), newChildren, - uf.analyzed(), uf.unresolvedMessage()), - uf.replaceChildren(newChildren)); + assertEquals( + new UnresolvedFunction(uf.source(), uf.name(), uf.resolutionStrategy(), newChildren, uf.analyzed(), uf.unresolvedMessage()), + uf.replaceChildren(newChildren) + ); } } diff --git a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/function/scalar/string/StartsWithFunctionPipeTests.java b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/function/scalar/string/StartsWithFunctionPipeTests.java index 50c47a0e47629..593dd1ba29a0a 100644 --- a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/function/scalar/string/StartsWithFunctionPipeTests.java +++ b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/function/scalar/string/StartsWithFunctionPipeTests.java @@ -54,10 +54,7 @@ private Expression randomStartsWithFunctionExpression() { } public static StartsWithFunctionPipe randomStartsWithFunctionPipe() { - return (StartsWithFunctionPipe) new StartsWithTest(randomSource(), - randomStringLiteral(), - randomStringLiteral(), - randomBoolean()) + return (StartsWithFunctionPipe) new StartsWithTest(randomSource(), randomStringLiteral(), randomStringLiteral(), randomBoolean()) .makePipe(); } @@ -72,21 +69,16 @@ public void testTransform() { newExpression, b1.input(), b1.pattern(), - b1.isCaseSensitive()); + b1.isCaseSensitive() + ); assertEquals(newB, b1.transformPropertiesOnly(Expression.class, v -> Objects.equals(v, b1.expression()) ? newExpression : v)); StartsWithFunctionPipe b2 = randomInstance(); Source newLoc = randomValueOtherThan(b2.source(), SourceTests::randomSource); - newB = new StartsWithFunctionPipe( - newLoc, - b2.expression(), - b2.input(), - b2.pattern(), - b2.isCaseSensitive()); - - assertEquals(newB, - b2.transformPropertiesOnly(Source.class, v -> Objects.equals(v, b2.source()) ? newLoc : v)); + newB = new StartsWithFunctionPipe(newLoc, b2.expression(), b2.input(), b2.pattern(), b2.isCaseSensitive()); + + assertEquals(newB, b2.transformPropertiesOnly(Source.class, v -> Objects.equals(v, b2.source()) ? newLoc : v)); } @Override @@ -120,11 +112,15 @@ protected StartsWithFunctionPipe mutate(StartsWithFunctionPipe instance) { List> randoms = new ArrayList<>(); for (int i = 1; i < 4; i++) { for (BitSet comb : new Combinations(3, i)) { - randoms.add(f -> new StartsWithFunctionPipe(f.source(), - f.expression(), - comb.get(0) ? randomValueOtherThan(f.input(), () -> pipe(randomStringLiteral())) : f.input(), - comb.get(1) ? randomValueOtherThan(f.pattern(), () -> pipe(randomStringLiteral())) : f.pattern(), - comb.get(2) ? randomValueOtherThan(f.isCaseSensitive(), ESTestCase::randomBoolean) : f.isCaseSensitive())); + randoms.add( + f -> new StartsWithFunctionPipe( + f.source(), + f.expression(), + comb.get(0) ? randomValueOtherThan(f.input(), () -> pipe(randomStringLiteral())) : f.input(), + comb.get(1) ? randomValueOtherThan(f.pattern(), () -> pipe(randomStringLiteral())) : f.pattern(), + comb.get(2) ? randomValueOtherThan(f.isCaseSensitive(), ESTestCase::randomBoolean) : f.isCaseSensitive() + ) + ); } } @@ -133,10 +129,12 @@ protected StartsWithFunctionPipe mutate(StartsWithFunctionPipe instance) { @Override protected StartsWithFunctionPipe copy(StartsWithFunctionPipe instance) { - return new StartsWithFunctionPipe(instance.source(), - instance.expression(), - instance.input(), - instance.pattern(), - instance.isCaseSensitive()); + return new StartsWithFunctionPipe( + instance.source(), + instance.expression(), + instance.input(), + instance.pattern(), + instance.isCaseSensitive() + ); } } diff --git a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/function/scalar/string/StartsWithProcessorTests.java b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/function/scalar/string/StartsWithProcessorTests.java index 121f17c64d07b..61be448c6aaa1 100644 --- a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/function/scalar/string/StartsWithProcessorTests.java +++ b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/function/scalar/string/StartsWithProcessorTests.java @@ -60,31 +60,29 @@ private Boolean insensitiveStartsWith(String left, String right) { } private Boolean startsWith(boolean caseInsensitive, String left, String right) { - return (Boolean) new StartsWithFunctionPipeTests.StartsWithTest(EMPTY, l(left), l(right), caseInsensitive) - .makePipe().asProcessor().process(null); + return (Boolean) new StartsWithFunctionPipeTests.StartsWithTest(EMPTY, l(left), l(right), caseInsensitive).makePipe() + .asProcessor() + .process(null); } private Boolean untypedStartsWith(Object left, Object right) { - return (Boolean) new StartsWithFunctionPipeTests.StartsWithTest(EMPTY, l(left), l(right), randomBoolean()) - .makePipe().asProcessor().process(null); + return (Boolean) new StartsWithFunctionPipeTests.StartsWithTest(EMPTY, l(left), l(right), randomBoolean()).makePipe() + .asProcessor() + .process(null); } public void testStartsWithFunctionInputsValidation() { - QlIllegalArgumentException siae = expectThrows(QlIllegalArgumentException.class, - () -> untypedStartsWith(5, "foo")); + QlIllegalArgumentException siae = expectThrows(QlIllegalArgumentException.class, () -> untypedStartsWith(5, "foo")); assertEquals("A string/char is required; received [5]", siae.getMessage()); - siae = expectThrows(QlIllegalArgumentException.class, - () -> untypedStartsWith("bar", false)); + siae = expectThrows(QlIllegalArgumentException.class, () -> untypedStartsWith("bar", false)); assertEquals("A string/char is required; received [false]", siae.getMessage()); } public void testStartsWithFunctionWithRandomInvalidDataType() { Literal literal = randomValueOtherThanMany(v -> v.dataType() == KEYWORD, () -> LiteralTests.randomLiteral()); - QlIllegalArgumentException siae = expectThrows(QlIllegalArgumentException.class, - () -> untypedStartsWith(literal, "foo")); + QlIllegalArgumentException siae = expectThrows(QlIllegalArgumentException.class, () -> untypedStartsWith(literal, "foo")); assertThat(siae.getMessage(), Matchers.startsWith("A string/char is required; received")); - siae = expectThrows(QlIllegalArgumentException.class, - () -> untypedStartsWith("foo", literal)); + siae = expectThrows(QlIllegalArgumentException.class, () -> untypedStartsWith("foo", literal)); assertThat(siae.getMessage(), Matchers.startsWith("A string/char is required; received")); } } diff --git a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/gen/pipeline/BinaryPipesTests.java b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/gen/pipeline/BinaryPipesTests.java index b815f0fc7ac40..f0df88a9bd74e 100644 --- a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/gen/pipeline/BinaryPipesTests.java +++ b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/gen/pipeline/BinaryPipesTests.java @@ -159,7 +159,6 @@ public Pipe resolveAttributes(AttributeResolver resolver) { } @Override - public void collectFields(QlSourceBuilder sourceBuilder) { - } + public void collectFields(QlSourceBuilder sourceBuilder) {} } } diff --git a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/gen/processor/ChainingProcessorTests.java b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/gen/processor/ChainingProcessorTests.java index aa4a59d81ea5f..f321a323a8627 100644 --- a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/gen/processor/ChainingProcessorTests.java +++ b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/gen/processor/ChainingProcessorTests.java @@ -43,10 +43,9 @@ protected Reader instanceReader() { protected ChainingProcessor mutateInstance(ChainingProcessor instance) throws IOException { @SuppressWarnings("unchecked") Supplier supplier = randomFrom( - () -> new ChainingProcessor( - instance.first(), randomValueOtherThan(instance.second(), () -> randomProcessor())), - () -> new ChainingProcessor( - randomValueOtherThan(instance.first(), () -> randomProcessor()), instance.second())); + () -> new ChainingProcessor(instance.first(), randomValueOtherThan(instance.second(), () -> randomProcessor())), + () -> new ChainingProcessor(randomValueOtherThan(instance.first(), () -> randomProcessor()), instance.second()) + ); return supplier.get(); } diff --git a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/gen/processor/ConstantProcessorTests.java b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/gen/processor/ConstantProcessorTests.java index 71e98beb32202..78e0cc9b0ef9f 100644 --- a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/gen/processor/ConstantProcessorTests.java +++ b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/gen/processor/ConstantProcessorTests.java @@ -22,7 +22,7 @@ public static ConstantProcessor randomConstantProcessor() { if (randomBoolean()) { clock = Clock.tick(clock, Duration.ofNanos(1)); } - return new ConstantProcessor( ZonedDateTime.now(clock)); + return new ConstantProcessor(ZonedDateTime.now(clock)); } else { return new ConstantProcessor(randomAlphaOfLength(5)); } diff --git a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/gen/script/ScriptsTests.java b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/gen/script/ScriptsTests.java index 836dbf9d02dc8..a3e7acc48faf7 100644 --- a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/gen/script/ScriptsTests.java +++ b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/gen/script/ScriptsTests.java @@ -15,56 +15,56 @@ public class ScriptsTests extends ESTestCase { public void testSplitWithMatches() { String input = "A1B2C3"; Pattern pattern = Pattern.compile("[0-9]+"); - - assertArrayEquals(new String[] {"A", "1", "B", "2", "C", "3"}, Scripts.splitWithMatches(input, pattern)); + + assertArrayEquals(new String[] { "A", "1", "B", "2", "C", "3" }, Scripts.splitWithMatches(input, pattern)); } public void testSplitWithMatchesNoMatches() { String input = "AxBxCx"; Pattern pattern = Pattern.compile("[0-9]+"); - - assertArrayEquals(new String[] {input}, Scripts.splitWithMatches(input, pattern)); + + assertArrayEquals(new String[] { input }, Scripts.splitWithMatches(input, pattern)); } public void testSplitWithMatchesOneMatch() { String input = "ABC"; Pattern pattern = Pattern.compile("ABC"); - - assertArrayEquals(new String[] {input}, Scripts.splitWithMatches(input, pattern)); + + assertArrayEquals(new String[] { input }, Scripts.splitWithMatches(input, pattern)); } public void testSplitWithMatchesSameMatch() { String input = "xxxx"; Pattern pattern = Pattern.compile("x"); - - assertArrayEquals(new String[] {"x","x","x","x"}, Scripts.splitWithMatches(input, pattern)); + + assertArrayEquals(new String[] { "x", "x", "x", "x" }, Scripts.splitWithMatches(input, pattern)); } public void testSplitWithMatchesTwoPatterns() { String input = "xyxy"; Pattern pattern = Pattern.compile("x|y"); - - assertArrayEquals(new String[] {"x","y","x","y"}, Scripts.splitWithMatches(input, pattern)); + + assertArrayEquals(new String[] { "x", "y", "x", "y" }, Scripts.splitWithMatches(input, pattern)); } public void testSplitWithMatchesTwoPatterns2() { String input = "A1B2C3"; Pattern pattern = Pattern.compile("[0-9]{1}|[A-F]{1}"); - - assertArrayEquals(new String[] {"A", "1", "B", "2", "C", "3"}, Scripts.splitWithMatches(input, pattern)); + + assertArrayEquals(new String[] { "A", "1", "B", "2", "C", "3" }, Scripts.splitWithMatches(input, pattern)); } public void testSplitWithMatchesTwoPatterns3() { String input = "A111BBB2C3"; Pattern pattern = Pattern.compile("[0-9]+|[A-F]+"); - - assertArrayEquals(new String[] {"A", "111", "BBB", "2", "C", "3"}, Scripts.splitWithMatches(input, pattern)); + + assertArrayEquals(new String[] { "A", "111", "BBB", "2", "C", "3" }, Scripts.splitWithMatches(input, pattern)); } public void testSplitWithMatchesTwoPatterns4() { String input = "xA111BxBB2C3x"; Pattern pattern = Pattern.compile("[0-9]+|[A-F]+"); - - assertArrayEquals(new String[] {"x", "A", "111", "B", "x", "BB", "2", "C", "3", "x"}, Scripts.splitWithMatches(input, pattern)); + + assertArrayEquals(new String[] { "x", "A", "111", "B", "x", "BB", "2", "C", "3", "x" }, Scripts.splitWithMatches(input, pattern)); } } diff --git a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/predicate/fulltext/FullTextUtilsTests.java b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/predicate/fulltext/FullTextUtilsTests.java index b25743fb8e7d1..fd5aebd58793e 100644 --- a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/predicate/fulltext/FullTextUtilsTests.java +++ b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/predicate/fulltext/FullTextUtilsTests.java @@ -27,16 +27,14 @@ public void testColonDelimited() { } public void testColonDelimitedErrorString() { - ParsingException e = expectThrows(ParsingException.class, - () -> FullTextUtils.parseSettings("k1=v1;k2v2", source)); + ParsingException e = expectThrows(ParsingException.class, () -> FullTextUtils.parseSettings("k1=v1;k2v2", source)); assertThat(e.getMessage(), is("line 1:3: Cannot parse entry k2v2 in options k1=v1;k2v2")); assertThat(e.getLineNumber(), is(1)); assertThat(e.getColumnNumber(), is(3)); } public void testColonDelimitedErrorDuplicate() { - ParsingException e = expectThrows(ParsingException.class, - () -> FullTextUtils.parseSettings("k1=v1;k1=v2", source)); + ParsingException e = expectThrows(ParsingException.class, () -> FullTextUtils.parseSettings("k1=v1;k1=v2", source)); assertThat(e.getMessage(), is("line 1:3: Duplicate option k1=v2 detected in options k1=v1;k1=v2")); assertThat(e.getLineNumber(), is(1)); assertThat(e.getColumnNumber(), is(3)); diff --git a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/predicate/logical/BinaryLogicProcessorTests.java b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/predicate/logical/BinaryLogicProcessorTests.java index 7caa19deddef6..907fcb1815dde 100644 --- a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/predicate/logical/BinaryLogicProcessorTests.java +++ b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/predicate/logical/BinaryLogicProcessorTests.java @@ -21,9 +21,10 @@ public class BinaryLogicProcessorTests extends AbstractWireSerializingTestCase { diff --git a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/predicate/operator/arithmetic/BinaryArithmeticProcessorTests.java b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/predicate/operator/arithmetic/BinaryArithmeticProcessorTests.java index b84e95c363f10..2186926cbd50a 100644 --- a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/predicate/operator/arithmetic/BinaryArithmeticProcessorTests.java +++ b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/predicate/operator/arithmetic/BinaryArithmeticProcessorTests.java @@ -21,9 +21,10 @@ public class BinaryArithmeticProcessorTests extends AbstractWireSerializingTestCase { public static BinaryArithmeticProcessor randomProcessor() { return new BinaryArithmeticProcessor( - new ConstantProcessor(randomLong()), - new ConstantProcessor(randomLong()), - randomFrom(DefaultBinaryArithmeticOperation.values())); + new ConstantProcessor(randomLong()), + new ConstantProcessor(randomLong()), + randomFrom(DefaultBinaryArithmeticOperation.values()) + ); } @Override diff --git a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/predicate/operator/comparison/BinaryComparisonProcessorTests.java b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/predicate/operator/comparison/BinaryComparisonProcessorTests.java index d95ff3a816225..6221f8414a74c 100644 --- a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/predicate/operator/comparison/BinaryComparisonProcessorTests.java +++ b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/predicate/operator/comparison/BinaryComparisonProcessorTests.java @@ -27,9 +27,10 @@ public class BinaryComparisonProcessorTests extends AbstractWireSerializingTestCase { public static BinaryComparisonProcessor randomProcessor() { return new BinaryComparisonProcessor( - new ConstantProcessor(randomLong()), - new ConstantProcessor(randomLong()), - randomFrom(BinaryComparisonProcessor.BinaryComparisonOperation.values())); + new ConstantProcessor(randomLong()), + new ConstantProcessor(randomLong()), + randomFrom(BinaryComparisonProcessor.BinaryComparisonOperation.values()) + ); } @Override diff --git a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/predicate/operator/comparison/InProcessorTests.java b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/predicate/operator/comparison/InProcessorTests.java index c5c564d6aea91..f5014b81d3491 100644 --- a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/predicate/operator/comparison/InProcessorTests.java +++ b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/predicate/operator/comparison/InProcessorTests.java @@ -12,8 +12,6 @@ import org.elasticsearch.xpack.ql.TestUtils; import org.elasticsearch.xpack.ql.expression.Literal; import org.elasticsearch.xpack.ql.expression.gen.processor.ConstantProcessor; -import org.elasticsearch.xpack.ql.expression.predicate.operator.comparison.In; -import org.elasticsearch.xpack.ql.expression.predicate.operator.comparison.InProcessor; import org.elasticsearch.xpack.ql.expression.processor.Processors; import java.util.Arrays; diff --git a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/predicate/operator/comparison/InTests.java b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/predicate/operator/comparison/InTests.java index 9de85d3d99880..b57b3354aebe9 100644 --- a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/predicate/operator/comparison/InTests.java +++ b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/predicate/operator/comparison/InTests.java @@ -9,7 +9,6 @@ import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.ql.TestUtils; import org.elasticsearch.xpack.ql.expression.Literal; -import org.elasticsearch.xpack.ql.expression.predicate.operator.comparison.In; import java.util.Arrays; diff --git a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/optimizer/OptimizerRulesTests.java b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/optimizer/OptimizerRulesTests.java index ee3429eb5cbd7..386d04c80d21d 100644 --- a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/optimizer/OptimizerRulesTests.java +++ b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/optimizer/OptimizerRulesTests.java @@ -194,10 +194,8 @@ public void testConstantFoldingBinaryComparison() { } public void testConstantFoldingBinaryLogic() { - assertEquals(FALSE, - new ConstantFolding().rule(new And(EMPTY, greaterThanOf(TWO, THREE), TRUE)).canonical()); - assertEquals(TRUE, - new ConstantFolding().rule(new Or(EMPTY, greaterThanOrEqualOf(TWO, THREE), TRUE)).canonical()); + assertEquals(FALSE, new ConstantFolding().rule(new And(EMPTY, greaterThanOf(TWO, THREE), TRUE)).canonical()); + assertEquals(TRUE, new ConstantFolding().rule(new Or(EMPTY, greaterThanOrEqualOf(TWO, THREE), TRUE)).canonical()); } public void testConstantFoldingBinaryLogic_WithNullHandling() { @@ -225,11 +223,8 @@ public void testConstantNot() { } public void testConstantFoldingLikes() { - assertEquals(TRUE, - new ConstantFolding().rule(new Like(EMPTY, of("test_emp"), new LikePattern("test%", (char) 0))) - .canonical()); - assertEquals(TRUE, - new ConstantFolding().rule(new RLike(EMPTY, of("test_emp"), new RLikePattern("test.emp"))).canonical()); + assertEquals(TRUE, new ConstantFolding().rule(new Like(EMPTY, of("test_emp"), new LikePattern("test%", (char) 0))).canonical()); + assertEquals(TRUE, new ConstantFolding().rule(new RLike(EMPTY, of("test_emp"), new RLikePattern("test.emp"))).canonical()); } public void testArithmeticFolding() { @@ -259,7 +254,7 @@ public void testLiteralsOnTheRight() { a = new Alias(EMPTY, "a", L(10)); result = new LiteralsOnTheRight().rule(nullEqualsOf(FIVE, a)); assertTrue(result instanceof NullEquals); - NullEquals nullEquals= (NullEquals) result; + NullEquals nullEquals = (NullEquals) result; assertEquals(a, nullEquals.left()); assertEquals(FIVE, nullEquals.right()); } @@ -364,7 +359,7 @@ public void testBoolEqualsSimplificationOnFields() { // Range optimization // - // 6 < a <= 5 -> FALSE + // 6 < a <= 5 -> FALSE public void testFoldExcludingRangeToFalse() { FieldAttribute fa = getFieldAttribute(); @@ -395,7 +390,7 @@ public void testCombineBinaryComparisonsNotComparable() { assertEquals(exp, and); } - // a <= 6 AND a < 5 -> a < 5 + // a <= 6 AND a < 5 -> a < 5 public void testCombineBinaryComparisonsUpper() { FieldAttribute fa = getFieldAttribute(); LessThanOrEqual lte = lessThanOrEqualOf(fa, SIX); @@ -409,7 +404,7 @@ public void testCombineBinaryComparisonsUpper() { assertEquals(FIVE, r.right()); } - // 6 <= a AND 5 < a -> 6 <= a + // 6 <= a AND 5 < a -> 6 <= a public void testCombineBinaryComparisonsLower() { FieldAttribute fa = getFieldAttribute(); GreaterThanOrEqual gte = greaterThanOrEqualOf(fa, SIX); @@ -423,7 +418,7 @@ public void testCombineBinaryComparisonsLower() { assertEquals(SIX, r.right()); } - // 5 <= a AND 5 < a -> 5 < a + // 5 <= a AND 5 < a -> 5 < a public void testCombineBinaryComparisonsInclude() { FieldAttribute fa = getFieldAttribute(); GreaterThanOrEqual gte = greaterThanOrEqualOf(fa, FIVE); @@ -447,7 +442,7 @@ public void testCombineBinaryComparisonsAndRangeLower() { CombineBinaryComparisons rule = new CombineBinaryComparisons(); Expression exp = rule.rule(new And(EMPTY, gt, range)); assertEquals(Range.class, exp.getClass()); - Range r = (Range)exp; + Range r = (Range) exp; assertEquals(TWO, r.lower()); assertFalse(r.includeLower()); assertEquals(THREE, r.upper()); @@ -464,7 +459,7 @@ public void testCombineBinaryComparisonsAndRangeUpper() { CombineBinaryComparisons rule = new CombineBinaryComparisons(); Expression exp = rule.rule(new And(EMPTY, range, lt)); assertEquals(Range.class, exp.getClass()); - Range r = (Range)exp; + Range r = (Range) exp; assertEquals(ONE, r.lower()); assertFalse(r.includeLower()); assertEquals(THREE, r.upper()); @@ -481,7 +476,7 @@ public void testCombineBinaryComparisonsAndRangeUpperEqual() { CombineBinaryComparisons rule = new CombineBinaryComparisons(); Expression exp = rule.rule(new And(EMPTY, lte, range)); assertEquals(Range.class, exp.getClass()); - Range r = (Range)exp; + Range r = (Range) exp; assertEquals(ONE, r.lower()); assertFalse(r.includeLower()); assertEquals(TWO, r.upper()); @@ -529,7 +524,7 @@ public void testCombineMixedMultipleBinaryComparisons() { assertTrue(r.includeUpper()); } - // 1 <= a AND a < 5 -> 1 <= a < 5 + // 1 <= a AND a < 5 -> 1 <= a < 5 public void testCombineComparisonsIntoRange() { FieldAttribute fa = getFieldAttribute(); GreaterThanOrEqual gte = greaterThanOrEqualOf(fa, ONE); @@ -850,7 +845,6 @@ public void testCombineBinaryComparisonsDisjunctionNotComparable() { assertEquals(exp, or); } - // 2 < a OR 1 < a OR 3 < a -> 1 < a public void testCombineBinaryComparisonsDisjunctionLowerBound() { FieldAttribute fa = getFieldAttribute(); @@ -887,7 +881,7 @@ public void testCombineBinaryComparisonsDisjunctionIncludeLowerBounds() { assertEquals(ONE, gt.right()); } - // a < 1 OR a < 2 OR a < 3 -> a < 3 + // a < 1 OR a < 2 OR a < 3 -> a < 3 public void testCombineBinaryComparisonsDisjunctionUpperBound() { FieldAttribute fa = getFieldAttribute(); @@ -905,7 +899,7 @@ public void testCombineBinaryComparisonsDisjunctionUpperBound() { assertEquals(THREE, lt.right()); } - // a < 2 OR a <= 2 OR a < 1 -> a <= 2 + // a < 2 OR a <= 2 OR a < 1 -> a <= 2 public void testCombineBinaryComparisonsDisjunctionIncludeUpperBounds() { FieldAttribute fa = getFieldAttribute(); @@ -923,7 +917,7 @@ public void testCombineBinaryComparisonsDisjunctionIncludeUpperBounds() { assertEquals(TWO, lte.right()); } - // a < 2 OR 3 < a OR a < 1 OR 4 < a -> a < 2 OR 3 < a + // a < 2 OR 3 < a OR a < 1 OR 4 < a -> a < 2 OR 3 < a public void testCombineBinaryComparisonsDisjunctionOfLowerAndUpperBounds() { FieldAttribute fa = getFieldAttribute(); @@ -967,7 +961,6 @@ public void testCombineBinaryComparisonsDisjunctionOfIncludedRangeNotComparable( public void testCombineBinaryComparisonsDisjunctionOfIncludedRange() { FieldAttribute fa = getFieldAttribute(); - Range r1 = rangeOf(fa, TWO, false, THREE, false); Range r2 = rangeOf(fa, ONE, false, FOUR, false); @@ -1087,7 +1080,8 @@ public void testBinaryComparisonAndOutOfRangeNotEqualsDifferentFields() { // keyword != '2021' AND datetime <= '2020-12-04T17:48:22.954240Z' new And(EMPTY, notEqualsOf(keywordOne, L("2021")), lessThanOrEqualOf(datetimeOne, L("2020-12-04T17:48:22.954240Z"))), // double > 10.1 AND double2 != -10.1 - new And(EMPTY, greaterThanOf(doubleOne, L(10.1d)), notEqualsOf(doubleTwo, L(-10.1d)))); + new And(EMPTY, greaterThanOf(doubleOne, L(10.1d)), notEqualsOf(doubleTwo, L(-10.1d))) + ); for (And and : testCases) { CombineBinaryComparisons rule = new CombineBinaryComparisons(); @@ -1120,7 +1114,6 @@ public void testEliminateRangeByNullEqualsInInterval() { assertEquals(eq1, exp); } - // The following tests should work only to simplify filters and // not if the expressions are part of a projection // See: https://github.com/elastic/elasticsearch/issues/35859 @@ -1442,13 +1435,13 @@ public void testMatchAllLikeToExist() throws Exception { } public void testMatchAllRLikeToExist() throws Exception { - RLikePattern pattern = new RLikePattern(".*"); - FieldAttribute fa = getFieldAttribute(); - RLike l = new RLike(EMPTY, fa, pattern); - Expression e = new ReplaceRegexMatch().rule(l); - assertEquals(IsNotNull.class, e.getClass()); - IsNotNull inn = (IsNotNull) e; - assertEquals(fa, inn.field()); + RLikePattern pattern = new RLikePattern(".*"); + FieldAttribute fa = getFieldAttribute(); + RLike l = new RLike(EMPTY, fa, pattern); + Expression e = new ReplaceRegexMatch().rule(l); + assertEquals(IsNotNull.class, e.getClass()); + IsNotNull inn = (IsNotNull) e; + assertEquals(fa, inn.field()); } public void testExactMatchLike() throws Exception { diff --git a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/plan/QueryPlanTests.java b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/plan/QueryPlanTests.java index ebc1791594d93..cfd045481ca2f 100644 --- a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/plan/QueryPlanTests.java +++ b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/plan/QueryPlanTests.java @@ -56,8 +56,10 @@ public void testTransformWithExpressionTopLevelInCollection() throws Exception { FieldAttribute two = fieldAttribute("two", INTEGER); Project project = new Project(EMPTY, relation(), asList(one, two)); - LogicalPlan transformed = project.transformExpressionsOnly(NamedExpression.class, n -> n.name().equals("one") ? - new FieldAttribute(EMPTY, "changed", one.field()) : n); + LogicalPlan transformed = project.transformExpressionsOnly( + NamedExpression.class, + n -> n.name().equals("one") ? new FieldAttribute(EMPTY, "changed", one.field()) : n + ); assertEquals(Project.class, transformed.getClass()); Project p = (Project) transformed; diff --git a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/querydsl/query/BoolQueryTests.java b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/querydsl/query/BoolQueryTests.java index f61d2ba5f27e5..74249793b72ba 100644 --- a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/querydsl/query/BoolQueryTests.java +++ b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/querydsl/query/BoolQueryTests.java @@ -8,11 +8,6 @@ import org.elasticsearch.search.sort.NestedSortBuilder; import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.xpack.ql.querydsl.query.BoolQuery; -import org.elasticsearch.xpack.ql.querydsl.query.ExistsQuery; -import org.elasticsearch.xpack.ql.querydsl.query.MatchAll; -import org.elasticsearch.xpack.ql.querydsl.query.NestedQuery; -import org.elasticsearch.xpack.ql.querydsl.query.Query; import org.elasticsearch.xpack.ql.tree.Source; import org.elasticsearch.xpack.ql.tree.SourceTests; import org.elasticsearch.xpack.ql.util.StringUtils; @@ -27,8 +22,12 @@ public class BoolQueryTests extends ESTestCase { static BoolQuery randomBoolQuery(int depth) { - return new BoolQuery(SourceTests.randomSource(), randomBoolean(), - NestedQueryTests.randomQuery(depth), NestedQueryTests.randomQuery(depth)); + return new BoolQuery( + SourceTests.randomSource(), + randomBoolean(), + NestedQueryTests.randomQuery(depth), + NestedQueryTests.randomQuery(depth) + ); } public void testEqualsAndHashCode() { @@ -44,7 +43,8 @@ private static BoolQuery mutate(BoolQuery query) { q -> new BoolQuery(SourceTests.mutate(q.source()), q.isAnd(), q.left(), q.right()), q -> new BoolQuery(q.source(), false == q.isAnd(), q.left(), q.right()), q -> new BoolQuery(q.source(), q.isAnd(), randomValueOtherThan(q.left(), () -> NestedQueryTests.randomQuery(5)), q.right()), - q -> new BoolQuery(q.source(), q.isAnd(), q.left(), randomValueOtherThan(q.right(), () -> NestedQueryTests.randomQuery(5)))); + q -> new BoolQuery(q.source(), q.isAnd(), q.left(), randomValueOtherThan(q.right(), () -> NestedQueryTests.randomQuery(5))) + ); return randomFrom(options).apply(query); } @@ -85,14 +85,21 @@ public void testEnrichNestedSort() { } private Query boolQueryWithoutNestedChildren() { - return new BoolQuery(SourceTests.randomSource(), randomBoolean(), new MatchAll(SourceTests.randomSource()), - new MatchAll(SourceTests.randomSource())); + return new BoolQuery( + SourceTests.randomSource(), + randomBoolean(), + new MatchAll(SourceTests.randomSource()), + new MatchAll(SourceTests.randomSource()) + ); } private Query boolQueryWithNestedChildren(String path, String field) { - NestedQuery match = new NestedQuery(SourceTests.randomSource(), path, - singletonMap(field, new SimpleImmutableEntry<>(randomBoolean(), null)), - new MatchAll(SourceTests.randomSource())); + NestedQuery match = new NestedQuery( + SourceTests.randomSource(), + path, + singletonMap(field, new SimpleImmutableEntry<>(randomBoolean(), null)), + new MatchAll(SourceTests.randomSource()) + ); Query matchAll = new MatchAll(SourceTests.randomSource()); Query left; Query right; @@ -107,9 +114,14 @@ private Query boolQueryWithNestedChildren(String path, String field) { } public void testToString() { - assertEquals("BoolQuery@1:2[ExistsQuery@1:2[f1] AND ExistsQuery@1:8[f2]]", - new BoolQuery(new Source(1, 1, StringUtils.EMPTY), true, - new ExistsQuery(new Source(1, 1, StringUtils.EMPTY), "f1"), - new ExistsQuery(new Source(1, 7, StringUtils.EMPTY), "f2")).toString()); + assertEquals( + "BoolQuery@1:2[ExistsQuery@1:2[f1] AND ExistsQuery@1:8[f2]]", + new BoolQuery( + new Source(1, 1, StringUtils.EMPTY), + true, + new ExistsQuery(new Source(1, 1, StringUtils.EMPTY), "f1"), + new ExistsQuery(new Source(1, 7, StringUtils.EMPTY), "f2") + ).toString() + ); } } diff --git a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/querydsl/query/LeafQueryTests.java b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/querydsl/query/LeafQueryTests.java index 30e82b6055553..97d261d7c7e75 100644 --- a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/querydsl/query/LeafQueryTests.java +++ b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/querydsl/query/LeafQueryTests.java @@ -9,8 +9,6 @@ import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.search.sort.NestedSortBuilder; import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.xpack.ql.querydsl.query.LeafQuery; -import org.elasticsearch.xpack.ql.querydsl.query.Query; import org.elasticsearch.xpack.ql.tree.Source; import org.elasticsearch.xpack.ql.tree.SourceTests; import org.elasticsearch.xpack.ql.util.StringUtils; diff --git a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/querydsl/query/MatchQueryTests.java b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/querydsl/query/MatchQueryTests.java index 0925c357b57d3..c3e6f111ec243 100644 --- a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/querydsl/query/MatchQueryTests.java +++ b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/querydsl/query/MatchQueryTests.java @@ -11,7 +11,6 @@ import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.ql.expression.FieldAttribute; import org.elasticsearch.xpack.ql.expression.predicate.fulltext.MatchQueryPredicate; -import org.elasticsearch.xpack.ql.querydsl.query.MatchQuery; import org.elasticsearch.xpack.ql.tree.Source; import org.elasticsearch.xpack.ql.tree.SourceTests; import org.elasticsearch.xpack.ql.type.EsField; @@ -29,11 +28,8 @@ public class MatchQueryTests extends ESTestCase { static MatchQuery randomMatchQuery() { - return new MatchQuery( - SourceTests.randomSource(), - randomAlphaOfLength(5), - randomAlphaOfLength(5)); - // TODO add the predicate + return new MatchQuery(SourceTests.randomSource(), randomAlphaOfLength(5), randomAlphaOfLength(5)); + // TODO add the predicate } public void testEqualsAndHashCode() { @@ -48,8 +44,9 @@ private static MatchQuery mutate(MatchQuery query) { List> options = Arrays.asList( q -> new MatchQuery(SourceTests.mutate(q.source()), q.name(), q.text(), q.predicate()), q -> new MatchQuery(q.source(), randomValueOtherThan(q.name(), () -> randomAlphaOfLength(5)), q.text(), q.predicate()), - q -> new MatchQuery(q.source(), q.name(), randomValueOtherThan(q.text(), () -> randomAlphaOfLength(5)), q.predicate())); - // TODO mutate the predicate + q -> new MatchQuery(q.source(), q.name(), randomValueOtherThan(q.text(), () -> randomAlphaOfLength(5)), q.predicate()) + ); + // TODO mutate the predicate return randomFrom(options).apply(query); } diff --git a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/querydsl/query/MultiMatchQueryTests.java b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/querydsl/query/MultiMatchQueryTests.java index a5f97cddd11f1..c47a5313b047b 100644 --- a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/querydsl/query/MultiMatchQueryTests.java +++ b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/querydsl/query/MultiMatchQueryTests.java @@ -10,7 +10,6 @@ import org.elasticsearch.index.query.MultiMatchQueryBuilder; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.ql.expression.predicate.fulltext.MultiMatchQueryPredicate; -import org.elasticsearch.xpack.ql.querydsl.query.MultiMatchQuery; import org.elasticsearch.xpack.ql.tree.Source; import org.elasticsearch.xpack.ql.util.StringUtils; diff --git a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/querydsl/query/NestedQueryTests.java b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/querydsl/query/NestedQueryTests.java index 96ff266839fdc..596fabe12624b 100644 --- a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/querydsl/query/NestedQueryTests.java +++ b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/querydsl/query/NestedQueryTests.java @@ -8,9 +8,6 @@ import org.elasticsearch.search.sort.NestedSortBuilder; import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.xpack.ql.querydsl.query.MatchAll; -import org.elasticsearch.xpack.ql.querydsl.query.NestedQuery; -import org.elasticsearch.xpack.ql.querydsl.query.Query; import org.elasticsearch.xpack.ql.tree.Source; import org.elasticsearch.xpack.ql.tree.SourceTests; import org.elasticsearch.xpack.ql.util.StringUtils; @@ -65,7 +62,8 @@ private static NestedQuery mutate(NestedQuery query) { q -> new NestedQuery(SourceTests.mutate(q.source()), q.path(), q.fields(), q.child()), q -> new NestedQuery(q.source(), randomValueOtherThan(q.path(), () -> randomAlphaOfLength(5)), q.fields(), q.child()), q -> new NestedQuery(q.source(), q.path(), randomValueOtherThan(q.fields(), NestedQueryTests::randomFields), q.child()), - q -> new NestedQuery(q.source(), q.path(), q.fields(), randomValueOtherThan(q.child(), () -> randomQuery(5)))); + q -> new NestedQuery(q.source(), q.path(), q.fields(), randomValueOtherThan(q.child(), () -> randomQuery(5))) + ); return randomFrom(options).apply(query); } @@ -134,9 +132,12 @@ public void testEnrichNestedSort() { } public void testToString() { - NestedQuery q = new NestedQuery(new Source(1, 1, StringUtils.EMPTY), "a.b", - singletonMap("f", new SimpleImmutableEntry<>(true, null)), - new MatchAll(new Source(1, 1, StringUtils.EMPTY))); + NestedQuery q = new NestedQuery( + new Source(1, 1, StringUtils.EMPTY), + "a.b", + singletonMap("f", new SimpleImmutableEntry<>(true, null)), + new MatchAll(new Source(1, 1, StringUtils.EMPTY)) + ); assertEquals("NestedQuery@1:2[a.b.{f=true=null}[MatchAll@1:2[]]]", q.toString()); } } diff --git a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/querydsl/query/QueryStringQueryTests.java b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/querydsl/query/QueryStringQueryTests.java index d992a75b3f6a9..b9e31ae34a60f 100644 --- a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/querydsl/query/QueryStringQueryTests.java +++ b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/querydsl/query/QueryStringQueryTests.java @@ -11,7 +11,6 @@ import org.elasticsearch.index.query.QueryStringQueryBuilder; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.ql.expression.predicate.fulltext.StringQueryPredicate; -import org.elasticsearch.xpack.ql.querydsl.query.QueryStringQuery; import org.elasticsearch.xpack.ql.tree.Source; import org.elasticsearch.xpack.ql.util.StringUtils; @@ -43,7 +42,6 @@ private static QueryStringQueryBuilder getBuilder(String options) { return (QueryStringQueryBuilder) mmq.asBuilder(); } - public void testToString() { final Source source = new Source(1, 1, StringUtils.EMPTY); final StringQueryPredicate mmqp = new StringQueryPredicate(source, "eggplant", ""); diff --git a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/tree/AbstractNodeTestCase.java b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/tree/AbstractNodeTestCase.java index 0195d3deff32f..f5dd5c6814736 100644 --- a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/tree/AbstractNodeTestCase.java +++ b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/tree/AbstractNodeTestCase.java @@ -19,11 +19,13 @@ public abstract class AbstractNodeTestCase> exten * Make a new random instance. */ protected abstract T randomInstance(); + /** * Mutate an instance into some other similar instance that * shouldn't be {@link #equals} to the original. */ protected abstract T mutate(T instance); + /** * Copy and instance so it isn't {@code ==} but should still * be {@link #equals}. @@ -34,6 +36,7 @@ public abstract class AbstractNodeTestCase> exten * Test this subclass's implementation of {@link Node#transformNodeProps}. */ public abstract void testTransform(); + /** * Test this subclass's implementation of {@link Node#replaceChildren(List)}. */ diff --git a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/tree/NodeSubclassTests.java b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/tree/NodeSubclassTests.java index 819b5ce89cd0b..e7591248353c0 100644 --- a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/tree/NodeSubclassTests.java +++ b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/tree/NodeSubclassTests.java @@ -236,8 +236,14 @@ public void testReplaceChildren() throws Exception { } } - private void assertTransformedOrReplacedChildren(T node, B transformed, Constructor ctor, - Object[] nodeCtorArgs, int changedArgOffset, Object changedArgValue) throws Exception { + private void assertTransformedOrReplacedChildren( + T node, + B transformed, + Constructor ctor, + Object[] nodeCtorArgs, + int changedArgOffset, + Object changedArgValue + ) throws Exception { if (node instanceof Function) { /* * Functions have a weaker definition of transform then other @@ -283,7 +289,7 @@ private void assertTransformedOrReplacedChildren(T node, B transformed, Construc */ static Constructor longestCtor(Class clazz) { Constructor longest = null; - for (Constructor ctor: clazz.getConstructors()) { + for (Constructor ctor : clazz.getConstructors()) { if (longest == null || longest.getParameterCount() < ctor.getParameterCount()) { @SuppressWarnings("unchecked") // Safe because the ctor has to be a ctor for T Constructor castCtor = (Constructor) ctor; @@ -303,10 +309,7 @@ static Constructor longestCtor(Class clazz) { @ParametersFactory @SuppressWarnings("rawtypes") public static List nodeSubclasses() throws IOException { - return subclassesOf(Node.class).stream() - .filter(c -> testClassFor(c) == null) - .map(c -> new Object[] {c}) - .collect(toList()); + return subclassesOf(Node.class).stream().filter(c -> testClassFor(c) == null).map(c -> new Object[] { c }).collect(toList()); } /** @@ -320,19 +323,19 @@ private Object[] ctorArgs(Constructor> ctor) throws Exception for (int i = 0; i < argTypes.length; i++) { final int currentArgIndex = i; args[i] = randomValueOtherThanMany(candidate -> { - for (int a = 0; a < currentArgIndex; a++) { - if (Objects.equals(args[a], candidate)) { - return true; - } - } - return false; - }, () -> { - try { - return makeArg(ctor.getDeclaringClass(), argTypes[currentArgIndex]); - } catch (Exception e) { - throw new RuntimeException(e); + for (int a = 0; a < currentArgIndex; a++) { + if (Objects.equals(args[a], candidate)) { + return true; } - }); + } + return false; + }, () -> { + try { + return makeArg(ctor.getDeclaringClass(), argTypes[currentArgIndex]); + } catch (Exception e) { + throw new RuntimeException(e); + } + }); } return args; } @@ -415,12 +418,10 @@ public boolean equals(Object obj) { // InnerAggregate's AggregateFunction must be an EnclosedAgg. if (argClass == AggregateFunction.class) { return makeEnclosedAgg(); - } - else if (argClass == CompoundAggregate.class) { + } else if (argClass == CompoundAggregate.class) { return makeCompoundAgg(); } - } - else if (toBuildClass == FieldAttribute.class) { + } else if (toBuildClass == FieldAttribute.class) { // `parent` is nullable. if (argClass == FieldAttribute.class && randomBoolean()) { return null; @@ -618,7 +619,7 @@ public static Set> subclassesOf(Class clazz) throws IO } Set> results = new LinkedHashSet<>(); String[] paths = System.getProperty("java.class.path").split(System.getProperty("path.separator")); - for (String path: paths) { + for (String path : paths) { Path root = PathUtils.get(path); int rootLength = root.toString().length() + 1; @@ -669,11 +670,12 @@ private static JarInputStream jarStream(Path path) throws IOException { * Load classes from predefined packages (hack to limit the scope) and if they match the hierarchy, add them to the cache */ private static void maybeLoadClass(Class clazz, String className, String location, Set> results) - throws IOException { + throws IOException { // filter the class that are not interested // (and IDE folders like eclipse) - if (className.startsWith("org.elasticsearch.xpack.ql") == false && className.startsWith("org.elasticsearch.xpack.sql") == false + if (className.startsWith("org.elasticsearch.xpack.ql") == false + && className.startsWith("org.elasticsearch.xpack.sql") == false && className.startsWith("org.elasticsearch.xpack.eql") == false) { return; } @@ -685,9 +687,7 @@ private static void maybeLoadClass(Class clazz, String className, String throw new IOException("Couldn't load " + location, e); } - if (false == Modifier.isAbstract(c.getModifiers()) - && false == c.isAnonymousClass() - && clazz.isAssignableFrom(c)) { + if (false == Modifier.isAbstract(c.getModifiers()) && false == c.isAnonymousClass() && clazz.isAssignableFrom(c)) { Class s = c.asSubclass(clazz); results.add(s); } diff --git a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/tree/NodeTests.java b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/tree/NodeTests.java index 53946e1673e82..f124a8894819c 100644 --- a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/tree/NodeTests.java +++ b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/tree/NodeTests.java @@ -22,23 +22,27 @@ public void testToString() { { ChildrenAreAProperty empty = new ChildrenAreAProperty(randomSource(), emptyList(), "thing"); assertEquals("ChildrenAreAProperty[thing]", empty.toString()); - assertEquals("ChildrenAreAProperty[single]\n\\_ChildrenAreAProperty[thing]", - new ChildrenAreAProperty(randomSource(), singletonList(empty), "single").toString()); - assertEquals("ChildrenAreAProperty[many]\n" - + "|_ChildrenAreAProperty[thing]\n" - + "\\_ChildrenAreAProperty[thing]", - new ChildrenAreAProperty(randomSource(), Arrays.asList(empty, empty), "many").toString()); + assertEquals( + "ChildrenAreAProperty[single]\n\\_ChildrenAreAProperty[thing]", + new ChildrenAreAProperty(randomSource(), singletonList(empty), "single").toString() + ); + assertEquals( + "ChildrenAreAProperty[many]\n" + "|_ChildrenAreAProperty[thing]\n" + "\\_ChildrenAreAProperty[thing]", + new ChildrenAreAProperty(randomSource(), Arrays.asList(empty, empty), "many").toString() + ); } { NoChildren empty = new NoChildren(randomSource(), "thing"); - assertEquals("AChildIsAProperty[single]\n" - + "\\_NoChildren[thing]", - new AChildIsAProperty(randomSource(), empty, "single").toString()); + assertEquals( + "AChildIsAProperty[single]\n" + "\\_NoChildren[thing]", + new AChildIsAProperty(randomSource(), empty, "single").toString() + ); } } public abstract static class Dummy extends Node { private final String thing; + public Dummy(Source source, List children, String thing) { super(source, children); this.thing = thing; @@ -57,8 +61,7 @@ public boolean equals(Object obj) { return false; } Dummy other = (Dummy) obj; - return thing.equals(other.thing) - && children().equals(other.children()); + return thing.equals(other.thing) && children().equals(other.children()); } @Override diff --git a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/tree/SourceTests.java b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/tree/SourceTests.java index ecc75930b499f..a8eb331056ecd 100644 --- a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/tree/SourceTests.java +++ b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/tree/SourceTests.java @@ -24,17 +24,22 @@ public static Source mutate(Source source) { l -> new Source( randomValueOtherThan(l.source().getLineNumber(), () -> between(1, Integer.MAX_VALUE)), l.source().getColumnNumber() - 1, - l.text()), + l.text() + ), l -> new Source( l.source().getLineNumber(), randomValueOtherThan(l.source().getColumnNumber() - 1, () -> between(1, Integer.MAX_VALUE)), - l.text())); + l.text() + ) + ); return randomFrom(options).apply(source); } public void testEqualsAndHashCode() { - checkEqualsAndHashCode(randomSource(), - l -> new Source(l.source().getLineNumber(), l.source().getColumnNumber() - 1, l.text()), - SourceTests::mutate); + checkEqualsAndHashCode( + randomSource(), + l -> new Source(l.source().getLineNumber(), l.source().getColumnNumber() - 1, l.text()), + SourceTests::mutate + ); } } diff --git a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/type/DataTypeConversionTests.java b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/type/DataTypeConversionTests.java index 769f9199631e4..0df878003ffe0 100644 --- a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/type/DataTypeConversionTests.java +++ b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/type/DataTypeConversionTests.java @@ -144,8 +144,7 @@ public void testConversionToDateTime() { Converter back = converterFor(KEYWORD, DATETIME); assertEquals(dt, back.convert(forward.convert(dt))); Exception e = expectThrows(QlIllegalArgumentException.class, () -> conversion.convert("0xff")); - assertEquals("cannot cast [0xff] to [datetime]: Text '0xff' could not be parsed at index 0", - e.getMessage()); + assertEquals("cannot cast [0xff] to [datetime]: Text '0xff' could not be parsed at index 0", e.getMessage()); } } @@ -332,8 +331,7 @@ public void testConversionToShort() { assertEquals((short) -12345, conversion.convert(asDateTime(-12345L))); // Nanos are ignored, only millis are used assertEquals((short) 1123, conversion.convert(DateUtils.asDateTime("1970-01-01T00:00:01.123456789Z"))); - Exception e = expectThrows(QlIllegalArgumentException.class, - () -> conversion.convert(asDateTime(Integer.MAX_VALUE))); + Exception e = expectThrows(QlIllegalArgumentException.class, () -> conversion.convert(asDateTime(Integer.MAX_VALUE))); assertEquals("[" + Integer.MAX_VALUE + "] out of [short] range", e.getMessage()); } } @@ -356,8 +354,7 @@ public void testConversionToByte() { assertEquals((byte) -123, conversion.convert(asDateTime(-123L))); // Nanos are ignored, only millis are used assertEquals((byte) 123, conversion.convert(DateUtils.asDateTime("1970-01-01T00:00:00.123456789Z"))); - Exception e = expectThrows(QlIllegalArgumentException.class, - () -> conversion.convert(asDateTime(Integer.MAX_VALUE))); + Exception e = expectThrows(QlIllegalArgumentException.class, () -> conversion.convert(asDateTime(Integer.MAX_VALUE))); assertEquals("[" + Integer.MAX_VALUE + "] out of [byte] range", e.getMessage()); } } @@ -404,8 +401,7 @@ public void testEsDataTypes() { } public void testConversionToUnsupported() { - Exception e = expectThrows(QlIllegalArgumentException.class, - () -> DataTypeConverter.convert(Integer.valueOf(1), UNSUPPORTED)); + Exception e = expectThrows(QlIllegalArgumentException.class, () -> DataTypeConverter.convert(Integer.valueOf(1), UNSUPPORTED)); assertEquals("cannot convert from [1], type [integer] to [unsupported]", e.getMessage()); } diff --git a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/type/TypesTests.java b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/type/TypesTests.java index 86e7c569e2ab6..f565896416dce 100644 --- a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/type/TypesTests.java +++ b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/type/TypesTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.ql.type; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.json.JsonXContent; import java.io.IOException; import java.io.InputStream; @@ -119,7 +119,7 @@ public void testDocValueField() { assertThat(mapping.size(), is(1)); EsField field = mapping.get("session_id"); assertThat(field, instanceOf(KeywordEsField.class)); - //assertThat(field.getPrecision(), is(15)); + // assertThat(field.getPrecision(), is(15)); assertThat(field.isAggregatable(), is(false)); } diff --git a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/util/RemoteClusterUtilsTests.java b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/util/RemoteClusterUtilsTests.java index a6fda9490a438..fbedc38fc138c 100644 --- a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/util/RemoteClusterUtilsTests.java +++ b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/util/RemoteClusterUtilsTests.java @@ -14,7 +14,7 @@ import static org.elasticsearch.xpack.ql.util.RemoteClusterUtils.qualifyAndJoinIndices; import static org.elasticsearch.xpack.ql.util.RemoteClusterUtils.splitQualifiedIndex; -public class RemoteClusterUtilsTests extends ESTestCase { +public class RemoteClusterUtilsTests extends ESTestCase { public void testSplitQualifiedIndex() { String cluster = randomAlphaOfLength(20); String index = randomAlphaOfLength(30); @@ -22,7 +22,7 @@ public void testSplitQualifiedIndex() { } public void testQualifyAndJoinIndices() { - String[] indices = {"foo", "bar", "bar*", "*foo"}; + String[] indices = { "foo", "bar", "bar*", "*foo" }; assertEquals("cluster:foo,cluster:bar,cluster:bar*,cluster:*foo", qualifyAndJoinIndices("cluster", indices)); } diff --git a/x-pack/plugin/rollup/qa/rest/src/yamlRestTest/java/org/elasticsearch/xpack/rollup/v2/RollupRestIT.java b/x-pack/plugin/rollup/qa/rest/src/yamlRestTest/java/org/elasticsearch/xpack/rollup/v2/RollupRestIT.java index 9f61316a4f264..f482896c2e47a 100644 --- a/x-pack/plugin/rollup/qa/rest/src/yamlRestTest/java/org/elasticsearch/xpack/rollup/v2/RollupRestIT.java +++ b/x-pack/plugin/rollup/qa/rest/src/yamlRestTest/java/org/elasticsearch/xpack/rollup/v2/RollupRestIT.java @@ -8,6 +8,7 @@ package org.elasticsearch.xpack.rollup.v2; import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; + import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate; import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase; diff --git a/x-pack/plugin/search-business-rules/src/internalClusterTest/java/org/elasticsearch/xpack/searchbusinessrules/PinnedQueryBuilderIT.java b/x-pack/plugin/search-business-rules/src/internalClusterTest/java/org/elasticsearch/xpack/searchbusinessrules/PinnedQueryBuilderIT.java index 3c5a8b9dd272b..913a6df734f27 100644 --- a/x-pack/plugin/search-business-rules/src/internalClusterTest/java/org/elasticsearch/xpack/searchbusinessrules/PinnedQueryBuilderIT.java +++ b/x-pack/plugin/search-business-rules/src/internalClusterTest/java/org/elasticsearch/xpack/searchbusinessrules/PinnedQueryBuilderIT.java @@ -28,7 +28,6 @@ import java.util.Map; import static org.elasticsearch.action.search.SearchType.DFS_QUERY_THEN_FETCH; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertFirstHit; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertFourthHit; @@ -37,6 +36,7 @@ import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThirdHit; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.hasId; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.hasIndex; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.Matchers.both; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; @@ -44,7 +44,6 @@ import static org.hamcrest.Matchers.lessThanOrEqualTo; import static org.hamcrest.Matchers.notNullValue; - public class PinnedQueryBuilderIT extends ESIntegTestCase { @Override @@ -53,10 +52,20 @@ protected Collection> nodePlugins() { } public void testPinnedPromotions() throws Exception { - assertAcked(prepareCreate("test") - .setMapping(jsonBuilder().startObject().startObject("_doc").startObject("properties").startObject("field1") - .field("analyzer", "whitespace").field("type", "text").endObject().endObject().endObject().endObject()) - .setSettings(Settings.builder().put(indexSettings()).put("index.number_of_shards", randomIntBetween(2, 5)))); + assertAcked( + prepareCreate("test").setMapping( + jsonBuilder().startObject() + .startObject("_doc") + .startObject("properties") + .startObject("field1") + .field("analyzer", "whitespace") + .field("type", "text") + .endObject() + .endObject() + .endObject() + .endObject() + ).setSettings(Settings.builder().put(indexSettings()).put("index.number_of_shards", randomIntBetween(2, 5))) + ); int numRelevantDocs = randomIntBetween(1, 100); for (int i = 0; i < numRelevantDocs; i++) { @@ -104,7 +113,11 @@ public void testPinnedPromotions() throws Exception { private void assertPinnedPromotions(PinnedQueryBuilder pqb, LinkedHashSet pins, int iter, int numRelevantDocs) { int from = randomIntBetween(0, numRelevantDocs); int size = randomIntBetween(10, 100); - SearchResponse searchResponse = client().prepareSearch().setQuery(pqb).setTrackTotalHits(true).setSize(size).setFrom(from) + SearchResponse searchResponse = client().prepareSearch() + .setQuery(pqb) + .setTrackTotalHits(true) + .setSize(size) + .setFrom(from) .setSearchType(DFS_QUERY_THEN_FETCH) .get(); @@ -122,8 +135,11 @@ private void assertPinnedPromotions(PinnedQueryBuilder pqb, LinkedHashSet= from) { - assertThat("Hit " + globalHitNumber + " in iter " + iter + " wrong" + pins, hits[globalHitNumber - from].getId(), - equalTo(id)); + assertThat( + "Hit " + globalHitNumber + " in iter " + iter + " wrong" + pins, + hits[globalHitNumber - from].getId(), + equalTo(id) + ); } globalHitNumber++; } @@ -148,12 +164,24 @@ private void assertPinnedPromotions(PinnedQueryBuilder pqb, LinkedHashSet highlights = searchResponse.getHits().getHits()[0].getHighlightFields(); assertThat(highlights.size(), equalTo(1)); @@ -236,15 +294,35 @@ private void assertHighlight(PinnedQueryBuilder pqb) { } public void testMultiIndexDocs() throws Exception { - assertAcked(prepareCreate("test1") - .setMapping(jsonBuilder().startObject().startObject("_doc").startObject("properties").startObject("field1") - .field("analyzer", "whitespace").field("type", "text").endObject().endObject().endObject().endObject()) - .setSettings(Settings.builder().put(indexSettings()).put("index.number_of_shards", randomIntBetween(2, 5)))); + assertAcked( + prepareCreate("test1").setMapping( + jsonBuilder().startObject() + .startObject("_doc") + .startObject("properties") + .startObject("field1") + .field("analyzer", "whitespace") + .field("type", "text") + .endObject() + .endObject() + .endObject() + .endObject() + ).setSettings(Settings.builder().put(indexSettings()).put("index.number_of_shards", randomIntBetween(2, 5))) + ); - assertAcked(prepareCreate("test2") - .setMapping(jsonBuilder().startObject().startObject("_doc").startObject("properties").startObject("field1") - .field("analyzer", "whitespace").field("type", "text").endObject().endObject().endObject().endObject()) - .setSettings(Settings.builder().put(indexSettings()).put("index.number_of_shards", randomIntBetween(2, 5)))); + assertAcked( + prepareCreate("test2").setMapping( + jsonBuilder().startObject() + .startObject("_doc") + .startObject("properties") + .startObject("field1") + .field("analyzer", "whitespace") + .field("type", "text") + .endObject() + .endObject() + .endObject() + .endObject() + ).setSettings(Settings.builder().put(indexSettings()).put("index.number_of_shards", randomIntBetween(2, 5))) + ); client().prepareIndex("test1").setId("a").setSource("field1", "1a bar").get(); client().prepareIndex("test1").setId("b").setSource("field1", "1b bar").get(); @@ -262,8 +340,11 @@ public void testMultiIndexDocs() throws Exception { new Item("test1", "b") ); - SearchResponse searchResponse = client().prepareSearch().setQuery(pqb).setTrackTotalHits(true) - .setSearchType(DFS_QUERY_THEN_FETCH).get(); + SearchResponse searchResponse = client().prepareSearch() + .setQuery(pqb) + .setTrackTotalHits(true) + .setSearchType(DFS_QUERY_THEN_FETCH) + .get(); assertHitCount(searchResponse, 4); assertFirstHit(searchResponse, both(hasIndex("test2")).and(hasId("a"))); @@ -273,11 +354,22 @@ public void testMultiIndexDocs() throws Exception { } public void testMultiIndexWithAliases() throws Exception { - assertAcked(prepareCreate("test") - .setMapping(jsonBuilder().startObject().startObject("_doc").startObject("properties").startObject("field1") - .field("analyzer", "whitespace").field("type", "text").endObject().endObject().endObject().endObject()) - .setSettings(Settings.builder().put(indexSettings()).put("index.number_of_shards", randomIntBetween(2, 5))) - .addAlias(new Alias("test-alias"))); + assertAcked( + prepareCreate("test").setMapping( + jsonBuilder().startObject() + .startObject("_doc") + .startObject("properties") + .startObject("field1") + .field("analyzer", "whitespace") + .field("type", "text") + .endObject() + .endObject() + .endObject() + .endObject() + ) + .setSettings(Settings.builder().put(indexSettings()).put("index.number_of_shards", randomIntBetween(2, 5))) + .addAlias(new Alias("test-alias")) + ); client().prepareIndex("test").setId("a").setSource("field1", "document a").get(); client().prepareIndex("test").setId("b").setSource("field1", "document b").get(); @@ -292,8 +384,11 @@ public void testMultiIndexWithAliases() throws Exception { new Item("test", "a") ); - SearchResponse searchResponse = client().prepareSearch().setQuery(pqb).setTrackTotalHits(true) - .setSearchType(DFS_QUERY_THEN_FETCH).get(); + SearchResponse searchResponse = client().prepareSearch() + .setQuery(pqb) + .setTrackTotalHits(true) + .setSearchType(DFS_QUERY_THEN_FETCH) + .get(); assertHitCount(searchResponse, 3); assertFirstHit(searchResponse, both(hasIndex("test")).and(hasId("b"))); @@ -301,4 +396,3 @@ public void testMultiIndexWithAliases() throws Exception { assertThirdHit(searchResponse, both(hasIndex("test")).and(hasId("c"))); } } - diff --git a/x-pack/plugin/search-business-rules/src/main/java/org/elasticsearch/xpack/searchbusinessrules/CappedScoreQuery.java b/x-pack/plugin/search-business-rules/src/main/java/org/elasticsearch/xpack/searchbusinessrules/CappedScoreQuery.java index 6f02d1c868cde..614172a7edd73 100644 --- a/x-pack/plugin/search-business-rules/src/main/java/org/elasticsearch/xpack/searchbusinessrules/CappedScoreQuery.java +++ b/x-pack/plugin/search-business-rules/src/main/java/org/elasticsearch/xpack/searchbusinessrules/CappedScoreQuery.java @@ -6,9 +6,6 @@ */ package org.elasticsearch.xpack.searchbusinessrules; -import java.io.IOException; -import java.util.Objects; - import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.search.BooleanClause; @@ -29,6 +26,9 @@ import org.apache.lucene.search.Weight; import org.apache.lucene.util.Bits; +import java.io.IOException; +import java.util.Objects; + /** * A query that wraps another query and ensures scores do not exceed a maximum value */ @@ -150,7 +150,7 @@ public Scorer get(long leadCost) throws IOException { // short-circuit if scores will not need capping innerScorer.advanceShallow(0); if (innerScorer.getMaxScore(DocIdSetIterator.NO_MORE_DOCS) <= maxScore) { - return innerScorer; + return innerScorer; } } return new CappedScorer(innerWeight, innerScorer, maxScore); @@ -189,8 +189,7 @@ public String toString(String field) { @Override public boolean equals(Object other) { - return sameClassAs(other) && maxScore == ((CappedScoreQuery) other).maxScore && - query.equals(((CappedScoreQuery) other).query); + return sameClassAs(other) && maxScore == ((CappedScoreQuery) other).maxScore && query.equals(((CappedScoreQuery) other).query); } @Override diff --git a/x-pack/plugin/search-business-rules/src/main/java/org/elasticsearch/xpack/searchbusinessrules/CappedScoreWeight.java b/x-pack/plugin/search-business-rules/src/main/java/org/elasticsearch/xpack/searchbusinessrules/CappedScoreWeight.java index ce132db4ce9c1..6ad3b9ce4ef85 100644 --- a/x-pack/plugin/search-business-rules/src/main/java/org/elasticsearch/xpack/searchbusinessrules/CappedScoreWeight.java +++ b/x-pack/plugin/search-business-rules/src/main/java/org/elasticsearch/xpack/searchbusinessrules/CappedScoreWeight.java @@ -58,11 +58,14 @@ public Explanation explain(LeafReaderContext context, int doc) throws IOExceptio Explanation sub = innerWeight.explain(context, doc); if (sub.isMatch() && sub.getValue().floatValue() > maxScore) { - return Explanation.match(maxScore, "Capped score of " + innerWeight.getQuery() + ", max of", - sub, - Explanation.match(maxScore, "maximum score")); + return Explanation.match( + maxScore, + "Capped score of " + innerWeight.getQuery() + ", max of", + sub, + Explanation.match(maxScore, "maximum score") + ); } else { - return sub; + return sub; } } diff --git a/x-pack/plugin/search-business-rules/src/main/java/org/elasticsearch/xpack/searchbusinessrules/PinnedQueryBuilder.java b/x-pack/plugin/search-business-rules/src/main/java/org/elasticsearch/xpack/searchbusinessrules/PinnedQueryBuilder.java index f6e99afbd9996..714307318bf69 100644 --- a/x-pack/plugin/search-business-rules/src/main/java/org/elasticsearch/xpack/searchbusinessrules/PinnedQueryBuilder.java +++ b/x-pack/plugin/search-business-rules/src/main/java/org/elasticsearch/xpack/searchbusinessrules/PinnedQueryBuilder.java @@ -14,24 +14,24 @@ import org.apache.lucene.search.Query; import org.apache.lucene.util.NumericUtils; import org.elasticsearch.Version; -import org.elasticsearch.common.regex.Regex; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.ConstructingObjectParser; -import org.elasticsearch.xcontent.ToXContentObject; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.common.regex.Regex; import org.elasticsearch.index.mapper.IdFieldMapper; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.query.AbstractQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryRewriteContext; import org.elasticsearch.index.query.SearchExecutionContext; +import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.ToXContentObject; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.ArrayList; @@ -306,19 +306,14 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep builder.endObject(); } - - - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(NAME, - a -> - { - QueryBuilder organicQuery = (QueryBuilder) a[0]; - @SuppressWarnings("unchecked") - List ids = (List) a[1]; - @SuppressWarnings("unchecked") - List docs = (List) a[2]; - return new PinnedQueryBuilder(organicQuery, ids, docs); - } - ); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(NAME, a -> { + QueryBuilder organicQuery = (QueryBuilder) a[0]; + @SuppressWarnings("unchecked") + List ids = (List) a[1]; + @SuppressWarnings("unchecked") + List docs = (List) a[2]; + return new PinnedQueryBuilder(organicQuery, ids, docs); + }); static { PARSER.declareObject(constructorArg(), (p, c) -> parseInnerQueryBuilder(p), ORGANIC_QUERY_FIELD); PARSER.declareStringArray(optionalConstructorArg(), IDS_FIELD); diff --git a/x-pack/plugin/search-business-rules/src/test/java/org/elasticsearch/xpack/searchbusinessrules/PinnedQueryBuilderTests.java b/x-pack/plugin/search-business-rules/src/test/java/org/elasticsearch/xpack/searchbusinessrules/PinnedQueryBuilderTests.java index c184d6d49747c..8e0ea79fa4151 100644 --- a/x-pack/plugin/search-business-rules/src/test/java/org/elasticsearch/xpack/searchbusinessrules/PinnedQueryBuilderTests.java +++ b/x-pack/plugin/search-business-rules/src/test/java/org/elasticsearch/xpack/searchbusinessrules/PinnedQueryBuilderTests.java @@ -12,10 +12,6 @@ import org.apache.lucene.search.DisjunctionMaxQuery; import org.apache.lucene.search.Query; import org.elasticsearch.common.ParsingException; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.query.MatchAllQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.SearchExecutionContext; @@ -23,6 +19,10 @@ import org.elasticsearch.plugins.Plugin; import org.elasticsearch.test.AbstractQueryTestCase; import org.elasticsearch.test.TestGeoShapeFieldMapperPlugin; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.searchbusinessrules.PinnedQueryBuilder.Item; import java.io.IOException; @@ -51,48 +51,48 @@ private QueryBuilder createRandomQuery() { } private QueryBuilder createTestTermQueryBuilder() { - String fieldName = null; - Object value; - switch (randomIntBetween(0, 3)) { - case 0: - if (randomBoolean()) { - fieldName = BOOLEAN_FIELD_NAME; - } - value = randomBoolean(); - break; - case 1: - if (randomBoolean()) { - fieldName = randomFrom(TEXT_FIELD_NAME, TEXT_ALIAS_FIELD_NAME); - } - if (frequently()) { - value = randomAlphaOfLengthBetween(1, 10); - } else { - // generate unicode string in 10% of cases - JsonStringEncoder encoder = JsonStringEncoder.getInstance(); - value = new String(encoder.quoteAsString(randomUnicodeOfLength(10))); - } - break; - case 2: - if (randomBoolean()) { - fieldName = INT_FIELD_NAME; - } - value = randomInt(10000); - break; - case 3: - if (randomBoolean()) { - fieldName = DOUBLE_FIELD_NAME; - } - value = randomDouble(); - break; - default: - throw new UnsupportedOperationException(); - } + String fieldName = null; + Object value; + switch (randomIntBetween(0, 3)) { + case 0: + if (randomBoolean()) { + fieldName = BOOLEAN_FIELD_NAME; + } + value = randomBoolean(); + break; + case 1: + if (randomBoolean()) { + fieldName = randomFrom(TEXT_FIELD_NAME, TEXT_ALIAS_FIELD_NAME); + } + if (frequently()) { + value = randomAlphaOfLengthBetween(1, 10); + } else { + // generate unicode string in 10% of cases + JsonStringEncoder encoder = JsonStringEncoder.getInstance(); + value = new String(encoder.quoteAsString(randomUnicodeOfLength(10))); + } + break; + case 2: + if (randomBoolean()) { + fieldName = INT_FIELD_NAME; + } + value = randomInt(10000); + break; + case 3: + if (randomBoolean()) { + fieldName = DOUBLE_FIELD_NAME; + } + value = randomDouble(); + break; + default: + throw new UnsupportedOperationException(); + } - if (fieldName == null) { - fieldName = randomAlphaOfLengthBetween(1, 10); - } - return new TermQueryBuilder(fieldName, value); + if (fieldName == null) { + fieldName = randomAlphaOfLengthBetween(1, 10); } + return new TermQueryBuilder(fieldName, value); + } private Item[] generateRandomItems() { return randomArray(1, 100, Item[]::new, () -> new Item(randomAlphaOfLength(64), randomAlphaOfLength(256))); @@ -117,25 +117,19 @@ protected Collection> getPlugins() { } public void testIllegalArguments() { - expectThrows(IllegalArgumentException.class, () -> new PinnedQueryBuilder(new MatchAllQueryBuilder(), (String)null)); + expectThrows(IllegalArgumentException.class, () -> new PinnedQueryBuilder(new MatchAllQueryBuilder(), (String) null)); expectThrows(IllegalArgumentException.class, () -> new PinnedQueryBuilder(null, "1")); expectThrows(IllegalArgumentException.class, () -> new PinnedQueryBuilder(new MatchAllQueryBuilder(), "1", null, "2")); expectThrows( IllegalArgumentException.class, - () -> new PinnedQueryBuilder(new MatchAllQueryBuilder(), (PinnedQueryBuilder.Item)null) - ); - expectThrows( - IllegalArgumentException.class, - () -> new PinnedQueryBuilder(null, new Item("test", "1")) + () -> new PinnedQueryBuilder(new MatchAllQueryBuilder(), (PinnedQueryBuilder.Item) null) ); + expectThrows(IllegalArgumentException.class, () -> new PinnedQueryBuilder(null, new Item("test", "1"))); expectThrows( IllegalArgumentException.class, () -> new PinnedQueryBuilder(new MatchAllQueryBuilder(), new Item("test", "1"), null, new Item("test", "2")) ); - expectThrows( - IllegalArgumentException.class, - () -> new PinnedQueryBuilder(new MatchAllQueryBuilder(), new Item("test*", "1")) - ); + expectThrows(IllegalArgumentException.class, () -> new PinnedQueryBuilder(new MatchAllQueryBuilder(), new Item("test*", "1"))); String[] bigIdList = new String[PinnedQueryBuilder.MAX_NUM_PINNED_HITS + 1]; Item[] bigItemList = new Item[PinnedQueryBuilder.MAX_NUM_PINNED_HITS + 1]; for (int i = 0; i < bigIdList.length; i++) { @@ -156,21 +150,20 @@ public void testEmptyPinnedQuery() throws Exception { } public void testIdsFromJson() throws IOException { - String query = - "{" + - "\"pinned\" : {" + - " \"organic\" : {" + - " \"term\" : {" + - " \"tag\" : {" + - " \"value\" : \"tech\"," + - " \"boost\" : 1.0" + - " }" + - " }" + - " }, "+ - " \"ids\" : [ \"1\",\"2\" ]," + - " \"boost\":1.0 "+ - "}" + - "}"; + String query = "{" + + "\"pinned\" : {" + + " \"organic\" : {" + + " \"term\" : {" + + " \"tag\" : {" + + " \"value\" : \"tech\"," + + " \"boost\" : 1.0" + + " }" + + " }" + + " }, " + + " \"ids\" : [ \"1\",\"2\" ]," + + " \"boost\":1.0 " + + "}" + + "}"; PinnedQueryBuilder queryBuilder = (PinnedQueryBuilder) parseQuery(query); checkGeneratedJson(query, queryBuilder); @@ -180,21 +173,20 @@ public void testIdsFromJson() throws IOException { } public void testDocsFromJson() throws IOException { - String query = - "{" + - "\"pinned\" : {" + - " \"organic\" : {" + - " \"term\" : {" + - " \"tag\" : {" + - " \"value\" : \"tech\"," + - " \"boost\" : 1.0" + - " }" + - " }" + - " }, "+ - " \"docs\" : [{ \"_index\": \"test\", \"_id\": \"1\" }, { \"_index\": \"test\", \"_id\": \"2\" }]," + - " \"boost\":1.0 "+ - "}" + - "}"; + String query = "{" + + "\"pinned\" : {" + + " \"organic\" : {" + + " \"term\" : {" + + " \"tag\" : {" + + " \"value\" : \"tech\"," + + " \"boost\" : 1.0" + + " }" + + " }" + + " }, " + + " \"docs\" : [{ \"_index\": \"test\", \"_id\": \"1\" }, { \"_index\": \"test\", \"_id\": \"2\" }]," + + " \"boost\":1.0 " + + "}" + + "}"; PinnedQueryBuilder queryBuilder = (PinnedQueryBuilder) parseQuery(query); checkGeneratedJson(query, queryBuilder); @@ -233,8 +225,7 @@ public void testMustRewrite() throws IOException { SearchExecutionContext context = createSearchExecutionContext(); context.setAllowUnmappedFields(true); PinnedQueryBuilder queryBuilder = new PinnedQueryBuilder(new TermQueryBuilder("unmapped_field", "42"), "42"); - IllegalStateException e = expectThrows(IllegalStateException.class, - () -> queryBuilder.toQuery(context)); + IllegalStateException e = expectThrows(IllegalStateException.class, () -> queryBuilder.toQuery(context)); assertEquals("Rewrite first", e.getMessage()); } diff --git a/x-pack/plugin/searchable-snapshots/qa/rest/src/javaRestTest/java/org/elasticsearch/xpack/searchablesnapshots/rest/FsSearchableSnapshotsIT.java b/x-pack/plugin/searchable-snapshots/qa/rest/src/javaRestTest/java/org/elasticsearch/xpack/searchablesnapshots/rest/FsSearchableSnapshotsIT.java index fea3d1a44e597..758dec86aca10 100644 --- a/x-pack/plugin/searchable-snapshots/qa/rest/src/javaRestTest/java/org/elasticsearch/xpack/searchablesnapshots/rest/FsSearchableSnapshotsIT.java +++ b/x-pack/plugin/searchable-snapshots/qa/rest/src/javaRestTest/java/org/elasticsearch/xpack/searchablesnapshots/rest/FsSearchableSnapshotsIT.java @@ -27,8 +27,6 @@ protected Settings writeRepositorySettings() { @Override protected Settings restClientSettings() { String token = basicAuthHeaderValue("admin", new SecureString("admin-password".toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } } diff --git a/x-pack/plugin/searchable-snapshots/qa/rest/src/yamlRestTest/java/org/elasticsearch/xpack/searchablesnapshots/rest/SearchableSnapshotsClientYamlTestSuiteIT.java b/x-pack/plugin/searchable-snapshots/qa/rest/src/yamlRestTest/java/org/elasticsearch/xpack/searchablesnapshots/rest/SearchableSnapshotsClientYamlTestSuiteIT.java index 4d22db3105190..0d7f87d27c641 100644 --- a/x-pack/plugin/searchable-snapshots/qa/rest/src/yamlRestTest/java/org/elasticsearch/xpack/searchablesnapshots/rest/SearchableSnapshotsClientYamlTestSuiteIT.java +++ b/x-pack/plugin/searchable-snapshots/qa/rest/src/yamlRestTest/java/org/elasticsearch/xpack/searchablesnapshots/rest/SearchableSnapshotsClientYamlTestSuiteIT.java @@ -28,8 +28,6 @@ public static Iterable parameters() throws Exception { @Override protected Settings restClientSettings() { String token = basicAuthHeaderValue("admin", new SecureString("admin-password".toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } } diff --git a/x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/AutoConfigureNode.java b/x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/AutoConfigureNode.java index 30207011ee482..e3053eaa00378 100644 --- a/x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/AutoConfigureNode.java +++ b/x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/AutoConfigureNode.java @@ -8,7 +8,6 @@ package org.elasticsearch.xpack.security.cli; import joptsimple.OptionSet; - import joptsimple.OptionSpec; import org.apache.commons.io.FileUtils; @@ -84,6 +83,7 @@ import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.Stream; + import javax.security.auth.x500.X500Principal; import static org.elasticsearch.common.ssl.PemUtils.parsePKCS8PemString; @@ -1056,12 +1056,11 @@ private void removeAutoConfigurationFromKeystore(Environment env, Terminal termi if (Files.exists(KeyStoreWrapper.keystorePath(env.configFile()))) { try ( KeyStoreWrapper existingKeystore = KeyStoreWrapper.load(env.configFile()); - SecureString keystorePassword = existingKeystore.hasPassword() ? new SecureString( - terminal.readSecret( - "Enter password for the elasticsearch keystore: ", - KeyStoreWrapper.MAX_PASSPHRASE_LENGTH + SecureString keystorePassword = existingKeystore.hasPassword() + ? new SecureString( + terminal.readSecret("Enter password for the elasticsearch keystore: ", KeyStoreWrapper.MAX_PASSPHRASE_LENGTH) ) - ) : new SecureString(new char[0]); + : new SecureString(new char[0]); ) { existingKeystore.decrypt(keystorePassword.getChars()); List secureSettingsToRemove = List.of( diff --git a/x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/CertGenUtils.java b/x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/CertGenUtils.java index ef10541469197..a7af05beb5514 100644 --- a/x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/CertGenUtils.java +++ b/x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/CertGenUtils.java @@ -37,9 +37,6 @@ import org.elasticsearch.common.network.NetworkUtils; import org.elasticsearch.core.SuppressForbidden; -import javax.net.ssl.X509ExtendedKeyManager; -import javax.net.ssl.X509ExtendedTrustManager; -import javax.security.auth.x500.X500Principal; import java.io.IOException; import java.math.BigInteger; import java.net.InetAddress; @@ -60,6 +57,9 @@ import java.util.Objects; import java.util.Set; +import javax.net.ssl.X509ExtendedKeyManager; +import javax.net.ssl.X509ExtendedTrustManager; +import javax.security.auth.x500.X500Principal; /** * Utility methods that deal with {@link Certificate}, {@link KeyStore}, {@link X509ExtendedTrustManager}, {@link X509ExtendedKeyManager} @@ -72,8 +72,7 @@ public class CertGenUtils { private static final int SERIAL_BIT_LENGTH = 20 * 8; private static final BouncyCastleProvider BC_PROV = new BouncyCastleProvider(); - private CertGenUtils() { - } + private CertGenUtils() {} /** * Generates a CA certificate @@ -99,9 +98,14 @@ public static X509Certificate generateCACertificate(X500Principal x500Principal, * @param days no of days certificate will be valid from now * @return a signed {@link X509Certificate} */ - public static X509Certificate generateSignedCertificate(X500Principal principal, GeneralNames subjectAltNames, KeyPair keyPair, - X509Certificate caCert, PrivateKey caPrivKey, int days) - throws OperatorCreationException, CertificateException, CertIOException, NoSuchAlgorithmException { + public static X509Certificate generateSignedCertificate( + X500Principal principal, + GeneralNames subjectAltNames, + KeyPair keyPair, + X509Certificate caCert, + PrivateKey caPrivKey, + int days + ) throws OperatorCreationException, CertificateException, CertIOException, NoSuchAlgorithmException { return generateSignedCertificate(principal, subjectAltNames, keyPair, caCert, caPrivKey, false, days, null); } @@ -123,10 +127,15 @@ public static X509Certificate generateSignedCertificate(X500Principal principal, * empty, then use default algorithm {@link CertGenUtils#getDefaultSignatureAlgorithm(PrivateKey)} * @return a signed {@link X509Certificate} */ - public static X509Certificate generateSignedCertificate(X500Principal principal, GeneralNames subjectAltNames, KeyPair keyPair, - X509Certificate caCert, PrivateKey caPrivKey, - int days, String signatureAlgorithm) - throws OperatorCreationException, CertificateException, CertIOException, NoSuchAlgorithmException { + public static X509Certificate generateSignedCertificate( + X500Principal principal, + GeneralNames subjectAltNames, + KeyPair keyPair, + X509Certificate caCert, + PrivateKey caPrivKey, + int days, + String signatureAlgorithm + ) throws OperatorCreationException, CertificateException, CertIOException, NoSuchAlgorithmException { return generateSignedCertificate(principal, subjectAltNames, keyPair, caCert, caPrivKey, false, days, signatureAlgorithm); } @@ -148,24 +157,46 @@ public static X509Certificate generateSignedCertificate(X500Principal principal, * empty, then use default algorithm {@link CertGenUtils#getDefaultSignatureAlgorithm(PrivateKey)} * @return a signed {@link X509Certificate} */ - public static X509Certificate generateSignedCertificate(X500Principal principal, GeneralNames subjectAltNames, KeyPair keyPair, - X509Certificate caCert, PrivateKey caPrivKey, boolean isCa, - int days, String signatureAlgorithm) - throws NoSuchAlgorithmException, CertificateException, CertIOException, OperatorCreationException { + public static X509Certificate generateSignedCertificate( + X500Principal principal, + GeneralNames subjectAltNames, + KeyPair keyPair, + X509Certificate caCert, + PrivateKey caPrivKey, + boolean isCa, + int days, + String signatureAlgorithm + ) throws NoSuchAlgorithmException, CertificateException, CertIOException, OperatorCreationException { Objects.requireNonNull(keyPair, "Key-Pair must not be null"); final ZonedDateTime notBefore = ZonedDateTime.now(ZoneOffset.UTC); if (days < 1) { throw new IllegalArgumentException("the certificate must be valid for at least one day"); } final ZonedDateTime notAfter = notBefore.plusDays(days); - return generateSignedCertificate(principal, subjectAltNames, keyPair, caCert, caPrivKey, isCa, notBefore, notAfter, - signatureAlgorithm); + return generateSignedCertificate( + principal, + subjectAltNames, + keyPair, + caCert, + caPrivKey, + isCa, + notBefore, + notAfter, + signatureAlgorithm + ); } - public static X509Certificate generateSignedCertificate(X500Principal principal, GeneralNames subjectAltNames, KeyPair keyPair, - X509Certificate caCert, PrivateKey caPrivKey, boolean isCa, - ZonedDateTime notBefore, ZonedDateTime notAfter, String signatureAlgorithm) - throws NoSuchAlgorithmException, CertIOException, OperatorCreationException, CertificateException { + public static X509Certificate generateSignedCertificate( + X500Principal principal, + GeneralNames subjectAltNames, + KeyPair keyPair, + X509Certificate caCert, + PrivateKey caPrivKey, + boolean isCa, + ZonedDateTime notBefore, + ZonedDateTime notAfter, + String signatureAlgorithm + ) throws NoSuchAlgorithmException, CertIOException, OperatorCreationException, CertificateException { final BigInteger serial = CertGenUtils.getSerial(); JcaX509ExtensionUtils extUtils = new JcaX509ExtensionUtils(); @@ -183,10 +214,14 @@ public static X509Certificate generateSignedCertificate(X500Principal principal, authorityKeyIdentifier = extUtils.createAuthorityKeyIdentifier(keyPair.getPublic()); } - JcaX509v3CertificateBuilder builder = - new JcaX509v3CertificateBuilder(issuer, serial, - new Time(Date.from(notBefore.toInstant()), Locale.ROOT), new Time(Date.from(notAfter.toInstant()), Locale.ROOT), subject, - keyPair.getPublic()); + JcaX509v3CertificateBuilder builder = new JcaX509v3CertificateBuilder( + issuer, + serial, + new Time(Date.from(notBefore.toInstant()), Locale.ROOT), + new Time(Date.from(notAfter.toInstant()), Locale.ROOT), + subject, + keyPair.getPublic() + ); builder.addExtension(Extension.subjectKeyIdentifier, false, extUtils.createSubjectKeyIdentifier(keyPair.getPublic())); builder.addExtension(Extension.authorityKeyIdentifier, false, authorityKeyIdentifier); @@ -197,8 +232,8 @@ public static X509Certificate generateSignedCertificate(X500Principal principal, PrivateKey signingKey = caPrivKey != null ? caPrivKey : keyPair.getPrivate(); ContentSigner signer = new JcaContentSignerBuilder( - (Strings.isNullOrEmpty(signatureAlgorithm)) ? getDefaultSignatureAlgorithm(signingKey) : signatureAlgorithm) - .setProvider(CertGenUtils.BC_PROV).build(signingKey); + (Strings.isNullOrEmpty(signatureAlgorithm)) ? getDefaultSignatureAlgorithm(signingKey) : signatureAlgorithm + ).setProvider(CertGenUtils.BC_PROV).build(signingKey); X509CertificateHolder certificateHolder = builder.build(signer); return new JcaX509CertificateConverter().getCertificate(certificateHolder); } @@ -223,8 +258,11 @@ private static String getDefaultSignatureAlgorithm(PrivateKey key) { signatureAlgorithm = "SHA256withECDSA"; break; default: - throw new IllegalArgumentException("Unsupported algorithm : " + key.getAlgorithm() - + " for signature, allowed values for private key algorithm are [RSA, DSA, EC]"); + throw new IllegalArgumentException( + "Unsupported algorithm : " + + key.getAlgorithm() + + " for signature, allowed values for private key algorithm are [RSA, DSA, EC]" + ); } return signatureAlgorithm; } @@ -238,8 +276,8 @@ private static String getDefaultSignatureAlgorithm(PrivateKey key) { * {@code null} * @return a certificate signing request */ - static PKCS10CertificationRequest generateCSR(KeyPair keyPair, X500Principal principal, GeneralNames sanList) - throws IOException, OperatorCreationException { + static PKCS10CertificationRequest generateCSR(KeyPair keyPair, X500Principal principal, GeneralNames sanList) throws IOException, + OperatorCreationException { Objects.requireNonNull(keyPair, "Key-Pair must not be null"); Objects.requireNonNull(keyPair.getPublic(), "Public-Key must not be null"); Objects.requireNonNull(principal, "Principal must not be null"); @@ -312,7 +350,7 @@ private static void addSubjectAlternativeNames(boolean resolveName, InetAddress * recognised by other X.509/TLS implementations. */ public static GeneralName createCommonName(String cn) { - final ASN1Encodable[] sequence = {new ASN1ObjectIdentifier(CN_OID), new DERTaggedObject(true, 0, new DERUTF8String(cn))}; + final ASN1Encodable[] sequence = { new ASN1ObjectIdentifier(CN_OID), new DERTaggedObject(true, 0, new DERUTF8String(cn)) }; return new GeneralName(GeneralName.otherName, new DERSequence(sequence)); } diff --git a/x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/CertificateGenerateTool.java b/x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/CertificateGenerateTool.java index bddfb72764e9d..abc0dc54b3e97 100644 --- a/x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/CertificateGenerateTool.java +++ b/x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/CertificateGenerateTool.java @@ -9,6 +9,7 @@ import joptsimple.ArgumentAcceptingOptionSpec; import joptsimple.OptionSet; import joptsimple.OptionSpec; + import org.bouncycastle.asn1.DERIA5String; import org.bouncycastle.asn1.x509.GeneralName; import org.bouncycastle.asn1.x509.GeneralNames; @@ -22,23 +23,22 @@ import org.elasticsearch.cli.ExitCodes; import org.elasticsearch.cli.Terminal; import org.elasticsearch.cli.UserException; -import org.elasticsearch.common.ssl.PemUtils; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; -import org.elasticsearch.core.SuppressForbidden; -import org.elasticsearch.core.PathUtils; import org.elasticsearch.common.network.InetAddresses; +import org.elasticsearch.common.ssl.PemUtils; import org.elasticsearch.common.util.set.Sets; -import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.core.PathUtils; +import org.elasticsearch.core.SuppressForbidden; +import org.elasticsearch.env.Environment; +import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.env.Environment; import org.elasticsearch.xpack.core.ssl.CertParsingUtils; -import javax.security.auth.x500.X500Principal; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; @@ -68,6 +68,8 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; +import javax.security.auth.x500.X500Principal; + /** * CLI tool to make generation of certificates or certificate requests easier for users * @@ -83,8 +85,9 @@ public class CertificateGenerateTool extends EnvironmentAwareCommand { private static final int DEFAULT_DAYS = 3 * 365; private static final int FILE_EXTENSION_LENGTH = 4; static final int MAX_FILENAME_LENGTH = 255 - FILE_EXTENSION_LENGTH; - private static final Pattern ALLOWED_FILENAME_CHAR_PATTERN = - Pattern.compile("[a-zA-Z0-9!@#$%^&{}\\[\\]()_+\\-=,.~'` ]{1," + MAX_FILENAME_LENGTH + "}"); + private static final Pattern ALLOWED_FILENAME_CHAR_PATTERN = Pattern.compile( + "[a-zA-Z0-9!@#$%^&{}\\[\\]()_+\\-=,.~'` ]{1," + MAX_FILENAME_LENGTH + "}" + ); private static final int DEFAULT_KEY_SIZE = 2048; private static final BouncyCastleProvider BC_PROV = new BouncyCastleProvider(); @@ -98,12 +101,17 @@ private static class InputFileParser { // error messages from the class initializer for ParseField since it creates Logger instances; therefore, we bury the initialization // of the parser in this class so that we can defer initialization until after logging has been initialized static { - @SuppressWarnings("unchecked") final ConstructingObjectParser instanceParser = - new ConstructingObjectParser<>( - "instances", - a -> new CertificateInformation( - (String) a[0], (String) (a[1] == null ? a[0] : a[1]), - (List) a[2], (List) a[3], (List) a[4])); + @SuppressWarnings("unchecked") + final ConstructingObjectParser instanceParser = new ConstructingObjectParser<>( + "instances", + a -> new CertificateInformation( + (String) a[0], + (String) (a[1] == null ? a[0] : a[1]), + (List) a[2], + (List) a[3], + (List) a[4] + ) + ); instanceParser.declareString(ConstructingObjectParser.constructorArg(), new ParseField("name")); instanceParser.declareString(ConstructingObjectParser.optionalConstructorArg(), new ParseField("filename")); instanceParser.declareStringArray(ConstructingObjectParser.optionalConstructorArg(), new ParseField("ip")); @@ -127,8 +135,7 @@ private static class InputFileParser { CertificateGenerateTool() { super(DESCRIPTION); - outputPathSpec = parser.accepts("out", "path of the zip file that the output should be written to") - .withRequiredArg(); + outputPathSpec = parser.accepts("out", "path of the zip file that the output should be written to").withRequiredArg(); csrSpec = parser.accepts("csr", "only generate certificate signing requests"); caCertPathSpec = parser.accepts("cert", "path to an existing ca certificate").availableUnless(csrSpec).withRequiredArg(); caKeyPathSpec = parser.accepts("key", "path to an existing ca private key") @@ -180,8 +187,17 @@ protected void execute(Terminal terminal, OptionSet options, Environment env) th } else { p12Password = null; } - CAInfo caInfo = getCAInfo(terminal, dn, caCertPathSpec.value(options), caKeyPathSpec.value(options), keyPass, prompt, env, - keysize, days); + CAInfo caInfo = getCAInfo( + terminal, + dn, + caCertPathSpec.value(options), + caKeyPathSpec.value(options), + keyPass, + prompt, + env, + keysize, + days + ); Collection certificateInformations = getCertificateInformationList(terminal, inputFile); generateAndWriteSignedCertificates(outputFile, certificateInformations, caInfo, keysize, days, p12Password); } @@ -231,8 +247,7 @@ private static Path resolvePath(String pathStr) { * @param inputFile an optional file that will be used to load the instance information * @return a {@link Collection} of {@link CertificateInformation} that represents each instance */ - static Collection getCertificateInformationList(Terminal terminal, String inputFile) - throws Exception { + static Collection getCertificateInformationList(Terminal terminal, String inputFile) throws Exception { if (inputFile != null) { return parseAndValidateFile(terminal, resolvePath(inputFile).toAbsolutePath()); } @@ -242,8 +257,9 @@ static Collection getCertificateInformationList(Terminal String name = terminal.readText("Enter instance name: "); if (name.isEmpty() == false) { final boolean isNameValidFilename = Name.isValidFilename(name); - String filename = terminal.readText("Enter name for directories and files " + (isNameValidFilename ? "[" + name + "]" : "") - + ": "); + String filename = terminal.readText( + "Enter name for directories and files " + (isNameValidFilename ? "[" + name + "]" : "") + ": " + ); if (filename.isEmpty() && isNameValidFilename) { filename = name; } @@ -270,8 +286,9 @@ static Collection getCertificateInformationList(Terminal terminal.println("A name must be provided"); } - String exit = terminal.readText("Would you like to specify another instance? Press 'y' to continue entering instance " + - "information: "); + String exit = terminal.readText( + "Would you like to specify another instance? Press 'y' to continue entering instance " + "information: " + ); if ("y".equals(exit) == false) { done = true; } @@ -286,8 +303,10 @@ static Collection parseAndValidateFile(Terminal terminal final List errors = certInfo.validate(); if (errors.size() > 0) { hasError = true; - terminal.errorPrintln(Terminal.Verbosity.SILENT, "Configuration for instance " + certInfo.name.originalName - + " has invalid details"); + terminal.errorPrintln( + Terminal.Verbosity.SILENT, + "Configuration for instance " + certInfo.name.originalName + " has invalid details" + ); for (String message : errors) { terminal.errorPrintln(Terminal.Verbosity.SILENT, " * " + message); } @@ -325,8 +344,11 @@ static void generateAndWriteCsrs(Path outputFile, Collection { for (CertificateInformation certificateInformation : certInfo) { KeyPair keyPair = CertGenUtils.generateKeyPair(keysize); - GeneralNames sanList = getSubjectAlternativeNamesValue(certificateInformation.ipAddresses, certificateInformation.dnsNames, - certificateInformation.commonNames); + GeneralNames sanList = getSubjectAlternativeNamesValue( + certificateInformation.ipAddresses, + certificateInformation.dnsNames, + certificateInformation.commonNames + ); PKCS10CertificationRequest csr = CertGenUtils.generateCSR(keyPair, certificateInformation.name.x500Principal, sanList); final String dirName = certificateInformation.name.filename + "/"; @@ -364,8 +386,17 @@ static void generateAndWriteCsrs(Path outputFile, Collection certificateInformations, - CAInfo caInfo, int keysize, int days, char[] pkcs12Password) throws Exception { + static void generateAndWriteSignedCertificates( + Path outputFile, + Collection certificateInformations, + CAInfo caInfo, + int keysize, + int days, + char[] pkcs12Password + ) throws Exception { fullyWriteFile(outputFile, (outputStream, pemWriter) -> { // write out the CA info first if it was generated writeCAInfoIfGenerated(outputStream, pemWriter, caInfo); for (CertificateInformation certificateInformation : certificateInformations) { KeyPair keyPair = CertGenUtils.generateKeyPair(keysize); - Certificate certificate = CertGenUtils.generateSignedCertificate(certificateInformation.name.x500Principal, - getSubjectAlternativeNamesValue(certificateInformation.ipAddresses, certificateInformation.dnsNames, - certificateInformation.commonNames), - keyPair, caInfo.caCert, caInfo.privateKey, days); + Certificate certificate = CertGenUtils.generateSignedCertificate( + certificateInformation.name.x500Principal, + getSubjectAlternativeNamesValue( + certificateInformation.ipAddresses, + certificateInformation.dnsNames, + certificateInformation.commonNames + ), + keyPair, + caInfo.caCert, + caInfo.privateKey, + days + ); final String dirName = certificateInformation.name.filename + "/"; ZipEntry zipEntry = new ZipEntry(dirName); @@ -430,8 +475,12 @@ static void generateAndWriteSignedCertificates(Path outputFile, Collection instanceParser = - new ConstructingObjectParser<>( - "instances", - a -> new CertificateInformation( - (String) a[0], (String) (a[1] == null ? a[0] : a[1]), - (List) a[2], (List) a[3], (List) a[4])); + @SuppressWarnings("unchecked") + final ConstructingObjectParser instanceParser = new ConstructingObjectParser<>( + "instances", + a -> new CertificateInformation( + (String) a[0], + (String) (a[1] == null ? a[0] : a[1]), + (List) a[2], + (List) a[3], + (List) a[4] + ) + ); instanceParser.declareString(ConstructingObjectParser.constructorArg(), new ParseField("name")); instanceParser.declareString(ConstructingObjectParser.optionalConstructorArg(), new ParseField("filename")); instanceParser.declareStringArray(ConstructingObjectParser.optionalConstructorArg(), new ParseField("ip")); @@ -138,7 +146,6 @@ private static class CertificateToolParser { } } - public static void main(String[] args) throws Exception { exit(new CertificateTool().main(args, Terminal.DEFAULT)); } @@ -157,41 +164,40 @@ protected void execute(Terminal terminal, OptionSet options) throws Exception { super.execute(terminal, options); } catch (OptionException e) { if (e.options().size() == 1 && e.options().contains("keep-ca-key")) { - throw new UserException(ExitCodes.USAGE, - "Generating certificates without providing a CA is no longer supported.\n" + - "Please first generate a CA with the 'ca' sub-command and provide the ca file \n" + - "with either --ca or --ca-cert/--ca-key to generate certificates."); + throw new UserException( + ExitCodes.USAGE, + "Generating certificates without providing a CA is no longer supported.\n" + + "Please first generate a CA with the 'ca' sub-command and provide the ca file \n" + + "with either --ca or --ca-cert/--ca-key to generate certificates." + ); } else { throw e; } } } - static final String INTRO_TEXT = "This tool assists you in the generation of X.509 certificates and certificate\n" + - "signing requests for use with SSL/TLS in the Elastic stack."; - - static final String INSTANCE_EXPLANATION = - " * An instance is any piece of the Elastic Stack that requires an SSL certificate.\n" + - " Depending on your configuration, Elasticsearch, Logstash, Kibana, and Beats\n" + - " may all require a certificate and private key.\n" + - " * The minimum required value for each instance is a name. This can simply be the\n" + - " hostname, which will be used as the Common Name of the certificate. A full\n" + - " distinguished name may also be used.\n" + - " * A filename value may be required for each instance. This is necessary when the\n" + - " name would result in an invalid file or directory name. The name provided here\n" + - " is used as the directory name (within the zip) and the prefix for the key and\n" + - " certificate files. The filename is required if you are prompted and the name\n" + - " is not displayed in the prompt.\n" + - " * IP addresses and DNS names are optional. Multiple values can be specified as a\n" + - " comma separated string. If no IP addresses or DNS names are provided, you may\n" + - " disable hostname verification in your SSL configuration."; - - static final String CA_EXPLANATION = - " * All certificates generated by this tool will be signed by a certificate authority (CA)\n" + - " unless the --self-signed command line option is specified.\n" + - " The tool can automatically generate a new CA for you, or you can provide your own with\n" + - " the --ca or --ca-cert command line options."; - + static final String INTRO_TEXT = "This tool assists you in the generation of X.509 certificates and certificate\n" + + "signing requests for use with SSL/TLS in the Elastic stack."; + + static final String INSTANCE_EXPLANATION = " * An instance is any piece of the Elastic Stack that requires an SSL certificate.\n" + + " Depending on your configuration, Elasticsearch, Logstash, Kibana, and Beats\n" + + " may all require a certificate and private key.\n" + + " * The minimum required value for each instance is a name. This can simply be the\n" + + " hostname, which will be used as the Common Name of the certificate. A full\n" + + " distinguished name may also be used.\n" + + " * A filename value may be required for each instance. This is necessary when the\n" + + " name would result in an invalid file or directory name. The name provided here\n" + + " is used as the directory name (within the zip) and the prefix for the key and\n" + + " certificate files. The filename is required if you are prompted and the name\n" + + " is not displayed in the prompt.\n" + + " * IP addresses and DNS names are optional. Multiple values can be specified as a\n" + + " comma separated string. If no IP addresses or DNS names are provided, you may\n" + + " disable hostname verification in your SSL configuration."; + + static final String CA_EXPLANATION = " * All certificates generated by this tool will be signed by a certificate authority (CA)\n" + + " unless the --self-signed command line option is specified.\n" + + " The tool can automatically generate a new CA for you, or you can provide your own with\n" + + " the --ca or --ca-cert command line options."; abstract static class CertificateCommand extends EnvironmentAwareCommand { // Common option for multiple commands. @@ -226,7 +232,8 @@ abstract static class CertificateCommand extends EnvironmentAwareCommand { final void acceptCertificateGenerationOptions() { pemFormatSpec = parser.accepts("pem", "output certificates and keys in PEM format instead of PKCS#12"); daysSpec = parser.accepts("days", "number of days that the generated certificates are valid") - .withRequiredArg().ofType(Integer.class); + .withRequiredArg() + .ofType(Integer.class); } final void acceptsCertificateAuthority() { @@ -246,8 +253,10 @@ final void acceptsCertificateAuthority() { } void acceptsCertificateAuthorityName() { - OptionSpecBuilder builder = parser.accepts("ca-dn", - "distinguished name to use for the generated ca. defaults to " + AUTO_GEN_CA_DN); + OptionSpecBuilder builder = parser.accepts( + "ca-dn", + "distinguished name to use for the generated ca. defaults to " + AUTO_GEN_CA_DN + ); if (caPkcs12PathSpec != null) { builder = builder.availableUnless(caPkcs12PathSpec); } @@ -353,12 +362,18 @@ CAInfo getCAInfo(Terminal terminal, OptionSet options, Environment env) throws E private CAInfo loadPkcs12CA(Terminal terminal, OptionSet options, Environment env) throws Exception { Path path = resolvePath(options, caPkcs12PathSpec); char[] passwordOption = getChars(caPasswordSpec.value(options)); - Map keys = withPassword("CA (" + path + ")", passwordOption, terminal, false, - password -> CertParsingUtils.readPkcs12KeyPairs(path, password, a -> password)); + Map keys = withPassword( + "CA (" + path + ")", + passwordOption, + terminal, + false, + password -> CertParsingUtils.readPkcs12KeyPairs(path, password, a -> password) + ); if (keys.size() != 1) { - throw new IllegalArgumentException("expected a single key in file [" + path.toAbsolutePath() + "] but found [" + - keys.size() + "]"); + throw new IllegalArgumentException( + "expected a single key in file [" + path.toAbsolutePath() + "] but found [" + keys.size() + "]" + ); } final Map.Entry pair = keys.entrySet().iterator().next(); return new CAInfo((X509Certificate) pair.getKey(), (PrivateKey) pair.getValue()); @@ -403,8 +418,7 @@ CAInfo generateCA(Terminal terminal, OptionSet options) throws Exception { * * @return a {@link Collection} of {@link CertificateInformation} that represents each instance */ - Collection getCertificateInformationList(Terminal terminal, OptionSet options) - throws Exception { + Collection getCertificateInformationList(Terminal terminal, OptionSet options) throws Exception { final Path input = resolvePath(options, inputFileSpec); if (input != null) { return parseAndValidateFile(terminal, input.toAbsolutePath()); @@ -468,8 +482,9 @@ static Collection readMultipleCertificateInformation(Ter terminal.println("A name must be provided"); } - String exit = terminal.readText("Would you like to specify another instance? Press 'y' to continue entering instance " + - "information: "); + String exit = terminal.readText( + "Would you like to specify another instance? Press 'y' to continue entering instance " + "information: " + ); if ("y".equals(exit) == false) { done = true; } @@ -480,8 +495,9 @@ static Collection readMultipleCertificateInformation(Ter private static String requestFileName(Terminal terminal, String certName) { final boolean isNameValidFilename = Name.isValidFilename(certName); while (true) { - String filename = terminal.readText("Enter name for directories and files of " + certName + - (isNameValidFilename ? " [" + certName + "]" : "") + ": "); + String filename = terminal.readText( + "Enter name for directories and files of " + certName + (isNameValidFilename ? " [" + certName + "]" : "") + ": " + ); if (filename.isEmpty() && isNameValidFilename) { return certName; } @@ -502,8 +518,7 @@ private static String requestFileName(Terminal terminal, String certName) { * @param info the certificate authority information * @param includeKey if true, write the CA key in PEM format */ - static void writeCAInfo(ZipOutputStream outputStream, JcaPEMWriter pemWriter, CAInfo info, boolean includeKey) - throws Exception { + static void writeCAInfo(ZipOutputStream outputStream, JcaPEMWriter pemWriter, CAInfo info, boolean includeKey) throws Exception { final String caDirName = createCaDirectory(outputStream); outputStream.putNextEntry(new ZipEntry(caDirName + "ca.crt")); pemWriter.writeObject(info.certAndKey.cert); @@ -535,8 +550,15 @@ private static String createCaDirectory(ZipOutputStream outputStream) throws IOE return caDirName; } - static void writePkcs12(String fileName, OutputStream output, String alias, CertificateAndKey pair, X509Certificate caCert, - char[] password, Terminal terminal) throws Exception { + static void writePkcs12( + String fileName, + OutputStream output, + String alias, + CertificateAndKey pair, + X509Certificate caCert, + char[] password, + Terminal terminal + ) throws Exception { final KeyStore pkcs12 = KeyStore.getInstance("PKCS12"); pkcs12.load(null); withPassword(fileName, password, terminal, true, p12Password -> { @@ -610,8 +632,11 @@ void generateAndWriteCsrs(Path output, int keySize, Collection { for (CertificateInformation certificateInformation : certInfo) { KeyPair keyPair = CertGenUtils.generateKeyPair(keySize); - GeneralNames sanList = getSubjectAlternativeNamesValue(certificateInformation.ipAddresses, - certificateInformation.dnsNames, certificateInformation.commonNames); + GeneralNames sanList = getSubjectAlternativeNamesValue( + certificateInformation.ipAddresses, + certificateInformation.dnsNames, + certificateInformation.commonNames + ); PKCS10CertificationRequest csr = CertGenUtils.generateCSR(keyPair, certificateInformation.name.x500Principal, sanList); final String dirName = certificateInformation.name.filename + "/"; @@ -748,9 +773,14 @@ CAInfo getCAInfo(Terminal terminal, OptionSet options, Environment env) throws E * @param caInfo the CA information to sign the certificates with * @param terminal the terminal to use if prompting for passwords */ - void generateAndWriteSignedCertificates(Path output, boolean writeZipFile, OptionSet options, - Collection certs, CAInfo caInfo, Terminal terminal) - throws Exception { + void generateAndWriteSignedCertificates( + Path output, + boolean writeZipFile, + OptionSet options, + Collection certs, + CAInfo caInfo, + Terminal terminal + ) throws Exception { checkDirectory(output, terminal); @@ -794,9 +824,15 @@ void generateAndWriteSignedCertificates(Path output, boolean writeZipFile, Optio } else { final String fileName = entryBase + ".p12"; outputStream.putNextEntry(new ZipEntry(fileName)); - writePkcs12(fileName, outputStream, certificateInformation.name.originalName, pair, + writePkcs12( + fileName, + outputStream, + certificateInformation.name.originalName, + pair, caInfo == null ? null : caInfo.certAndKey.cert, - outputPassword, terminal); + outputPassword, + terminal + ); outputStream.closeEntry(); } } @@ -805,26 +841,57 @@ void generateAndWriteSignedCertificates(Path output, boolean writeZipFile, Optio assert certs.size() == 1; CertificateInformation certificateInformation = certs.iterator().next(); CertificateAndKey pair = generateCertificateAndKey(certificateInformation, caInfo, keySize, days); - fullyWriteFile(output, stream -> writePkcs12(output.getFileName().toString(), stream, - certificateInformation.name.originalName, pair, - caInfo == null ? null : caInfo.certAndKey.cert, outputPassword, terminal)); + fullyWriteFile( + output, + stream -> writePkcs12( + output.getFileName().toString(), + stream, + certificateInformation.name.originalName, + pair, + caInfo == null ? null : caInfo.certAndKey.cert, + outputPassword, + terminal + ) + ); } } - private CertificateAndKey generateCertificateAndKey(CertificateInformation certificateInformation, CAInfo caInfo, - int keySize, int days) throws Exception { + private CertificateAndKey generateCertificateAndKey( + CertificateInformation certificateInformation, + CAInfo caInfo, + int keySize, + int days + ) throws Exception { KeyPair keyPair = CertGenUtils.generateKeyPair(keySize); Certificate certificate; if (caInfo != null) { - certificate = CertGenUtils.generateSignedCertificate(certificateInformation.name.x500Principal, - getSubjectAlternativeNamesValue(certificateInformation.ipAddresses, certificateInformation.dnsNames, - certificateInformation.commonNames), - keyPair, caInfo.certAndKey.cert, caInfo.certAndKey.key, days); + certificate = CertGenUtils.generateSignedCertificate( + certificateInformation.name.x500Principal, + getSubjectAlternativeNamesValue( + certificateInformation.ipAddresses, + certificateInformation.dnsNames, + certificateInformation.commonNames + ), + keyPair, + caInfo.certAndKey.cert, + caInfo.certAndKey.key, + days + ); } else { - certificate = CertGenUtils.generateSignedCertificate(certificateInformation.name.x500Principal, - getSubjectAlternativeNamesValue(certificateInformation.ipAddresses, certificateInformation.dnsNames, - certificateInformation.commonNames), - keyPair, null, null, false, days, null); + certificate = CertGenUtils.generateSignedCertificate( + certificateInformation.name.x500Principal, + getSubjectAlternativeNamesValue( + certificateInformation.ipAddresses, + certificateInformation.dnsNames, + certificateInformation.commonNames + ), + keyPair, + null, + null, + false, + days, + null + ); } return new CertificateAndKey((X509Certificate) certificate, keyPair.getPrivate()); } @@ -871,8 +938,10 @@ private void writeCertificateAuthority(Path output, CAInfo caInfo, boolean write fullyWriteZipFile(output, (outputStream, pemWriter) -> writeCAInfo(outputStream, pemWriter, caInfo, true)); } else { final String fileName = output.getFileName().toString(); - fullyWriteFile(output, outputStream -> - writePkcs12(fileName, outputStream, "ca", caInfo.certAndKey, null, caInfo.password, terminal)); + fullyWriteFile( + output, + outputStream -> writePkcs12(fileName, outputStream, "ca", caInfo.certAndKey, null, caInfo.password, terminal) + ); } } } @@ -889,8 +958,10 @@ static Collection parseAndValidateFile(Terminal terminal final List errors = certInfo.validate(); if (errors.size() > 0) { hasError = true; - terminal.errorPrintln(Verbosity.SILENT, "Configuration for instance " + certInfo.name.originalName + - " has invalid details"); + terminal.errorPrintln( + Verbosity.SILENT, + "Configuration for instance " + certInfo.name.originalName + " has invalid details" + ); for (String message : errors) { terminal.errorPrintln(Verbosity.SILENT, " * " + message); } @@ -945,8 +1016,13 @@ static boolean checkAndConfirmPasswordLengthForOpenSSLCompatibility(char[] passw return true; } - private static T withPassword(String description, char[] password, Terminal terminal, boolean checkLength, - CheckedFunction body) throws E { + private static T withPassword( + String description, + char[] password, + Terminal terminal, + boolean checkLength, + CheckedFunction body + ) throws E { if (password == null) { while (true) { char[] promptedValue = terminal.readSecret("Enter password for " + description + " : "); @@ -975,8 +1051,10 @@ private static T withPassword(String description, char[ */ private static void fullyWriteZipFile(Path file, Writer writer) throws Exception { fullyWriteFile(file, outputStream -> { - try (ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream, StandardCharsets.UTF_8); - JcaPEMWriter pemWriter = new JcaPEMWriter(new OutputStreamWriter(zipOutputStream, StandardCharsets.UTF_8))) { + try ( + ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream, StandardCharsets.UTF_8); + JcaPEMWriter pemWriter = new JcaPEMWriter(new OutputStreamWriter(zipOutputStream, StandardCharsets.UTF_8)) + ) { writer.write(zipOutputStream, pemWriter); } }); @@ -1092,7 +1170,6 @@ private static char[] getChars(String password) { return password == null ? null : password.toCharArray(); } - static class CertificateInformation { final Name name; final List ipAddresses; @@ -1155,7 +1232,11 @@ static Name fromUserProvidedName(String name, String filename) { principal = new X500Principal("CN=" + name); } } catch (IllegalArgumentException e) { - String error = "[" + name + "] could not be converted to a valid DN\n" + e.getMessage() + "\n" + String error = "[" + + name + + "] could not be converted to a valid DN\n" + + e.getMessage() + + "\n" + ExceptionsHelper.stackTrace(e); return new Name(name, null, null, error); } @@ -1176,8 +1257,15 @@ static boolean isValidFilename(String name) { @Override public String toString() { return getClass().getSimpleName() - + "{original=[" + originalName + "] principal=[" + x500Principal - + "] file=[" + filename + "] err=[" + error + "]}"; + + "{original=[" + + originalName + + "] principal=[" + + x500Principal + + "] file=[" + + filename + + "] err=[" + + error + + "]}"; } } diff --git a/x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/HttpCertificateCommand.java b/x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/HttpCertificateCommand.java index 9ee73abe7d7ca..5cbdb67e53658 100644 --- a/x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/HttpCertificateCommand.java +++ b/x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/HttpCertificateCommand.java @@ -9,6 +9,7 @@ import joptsimple.OptionParser; import joptsimple.OptionSet; + import org.bouncycastle.asn1.DERIA5String; import org.bouncycastle.asn1.x509.GeneralNames; import org.bouncycastle.cert.CertIOException; @@ -26,16 +27,14 @@ import org.elasticsearch.cli.Terminal; import org.elasticsearch.cli.UserException; import org.elasticsearch.common.Strings; -import org.elasticsearch.common.ssl.PemUtils; -import org.elasticsearch.core.PathUtils; import org.elasticsearch.common.io.Streams; import org.elasticsearch.common.network.InetAddresses; import org.elasticsearch.common.ssl.PemUtils; import org.elasticsearch.common.util.set.Sets; +import org.elasticsearch.core.PathUtils; import org.elasticsearch.env.Environment; import org.elasticsearch.xpack.core.ssl.CertParsingUtils; -import javax.security.auth.x500.X500Principal; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -75,6 +74,8 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; +import javax.security.auth.x500.X500Principal; + import static org.elasticsearch.xpack.security.cli.CertGenUtils.generateSignedCertificate; /** @@ -244,16 +245,18 @@ protected Path resolvePath(String name) { return PathUtils.get(name).normalize().toAbsolutePath(); } - private void writeZip(Path file, char[] password, CertificateTool.CAInfo caInfo, List certificates, - Environment env) throws UserException { + private void writeZip(Path file, char[] password, CertificateTool.CAInfo caInfo, List certificates, Environment env) + throws UserException { if (Files.exists(file)) { throw new UserException(ExitCodes.IO_ERROR, "Output file '" + file + "' already exists"); } boolean success = false; try { - try (OutputStream fileStream = Files.newOutputStream(file, StandardOpenOption.CREATE_NEW); - ZipOutputStream zipStream = new ZipOutputStream(fileStream, StandardCharsets.UTF_8)) { + try ( + OutputStream fileStream = Files.newOutputStream(file, StandardOpenOption.CREATE_NEW); + ZipOutputStream zipStream = new ZipOutputStream(fileStream, StandardCharsets.UTF_8) + ) { createZipDirectory(zipStream, "elasticsearch"); if (certificates.size() == 1) { @@ -311,8 +314,14 @@ private void createZipDirectory(ZipOutputStream zip, String name) throws IOExcep zip.putNextEntry(entry); } - private void writeCertificateAndKeyDetails(ZipOutputStream zip, String dirName, CertOptions cert, CertificateTool.CAInfo ca, - char[] password, Environment env) { + private void writeCertificateAndKeyDetails( + ZipOutputStream zip, + String dirName, + CertOptions cert, + CertificateTool.CAInfo ca, + char[] password, + Environment env + ) { // TODO : Should we add support for configuring PKI in ES? try { final KeyPair keyPair = CertGenUtils.generateKeyPair(cert.keySize); @@ -327,12 +336,16 @@ private void writeCertificateAndKeyDetails(ZipOutputStream zip, String dirName, final String keyFile = "http-" + cert.name + ".key"; final String certName = "http-" + cert.name + ".crt"; final String ymlFile = "sample-elasticsearch.yml"; - final Map substitutions = buildSubstitutions(env, Map.ofEntries( - Map.entry("CSR", csrFile), - Map.entry("KEY", keyFile), - Map.entry("CERT", certName), - Map.entry("YML", ymlFile), - Map.entry("PASSWORD", hasPassword ? "*" : ""))); + final Map substitutions = buildSubstitutions( + env, + Map.ofEntries( + Map.entry("CSR", csrFile), + Map.entry("KEY", keyFile), + Map.entry("CERT", certName), + Map.entry("YML", ymlFile), + Map.entry("PASSWORD", hasPassword ? "*" : "") + ) + ); writeTextFile(zip, dirName + "/README.txt", ES_README_CSR, substitutions); writePemEntry(zip, dirName + "/" + csrFile, new JcaMiscPEMGenerator(csr)); writePemEntry(zip, dirName + "/" + keyFile, generator(keyPair.getPrivate(), password)); @@ -340,15 +353,24 @@ private void writeCertificateAndKeyDetails(ZipOutputStream zip, String dirName, } else { final ZonedDateTime notBefore = ZonedDateTime.now(ZoneOffset.UTC); final ZonedDateTime notAfter = notBefore.plus(cert.validity); - Certificate certificate = CertGenUtils.generateSignedCertificate(cert.subject, sanList, keyPair, ca.certAndKey.cert, - ca.certAndKey.key, false, notBefore, notAfter, null); + Certificate certificate = CertGenUtils.generateSignedCertificate( + cert.subject, + sanList, + keyPair, + ca.certAndKey.cert, + ca.certAndKey.key, + false, + notBefore, + notAfter, + null + ); final String p12Name = "http.p12"; final String ymlFile = "sample-elasticsearch.yml"; - final Map substitutions = buildSubstitutions(env, Map.ofEntries( - Map.entry("P12", p12Name), - Map.entry("YML", ymlFile), - Map.entry("PASSWORD", hasPassword ? "*" : ""))); + final Map substitutions = buildSubstitutions( + env, + Map.ofEntries(Map.entry("P12", p12Name), Map.entry("YML", ymlFile), Map.entry("PASSWORD", hasPassword ? "*" : "")) + ); writeTextFile(zip, dirName + "/README.txt", ES_README_P12, substitutions); writeKeyStore(zip, dirName + "/" + p12Name, certificate, keyPair.getPrivate(), password, ca.certAndKey.cert); writeTextFile(zip, dirName + "/" + ymlFile, ES_YML_P12, substitutions); @@ -363,12 +385,22 @@ private void writeCertificateAuthority(ZipOutputStream zip, String dirName, Cert assert ca.generated; try { - writeTextFile(zip, dirName + "/README.txt", CA_README_P12, - buildSubstitutions(env, Map.of( - "P12", "ca.p12", - "DN", ca.certAndKey.cert.getSubjectX500Principal().getName(), - "PASSWORD", ca.password == null || ca.password.length == 0 ? "" : "*" - ))); + writeTextFile( + zip, + dirName + "/README.txt", + CA_README_P12, + buildSubstitutions( + env, + Map.of( + "P12", + "ca.p12", + "DN", + ca.certAndKey.cert.getSubjectX500Principal().getName(), + "PASSWORD", + ca.password == null || ca.password.length == 0 ? "" : "*" + ) + ) + ); final KeyStore pkcs12 = KeyStore.getInstance("PKCS12"); pkcs12.load(null); pkcs12.setKeyEntry("ca", ca.certAndKey.key, ca.password, new Certificate[] { ca.certAndKey.cert }); @@ -385,11 +417,10 @@ private void writeKibanaInfo(ZipOutputStream zip, String dirName, CertificateToo final String caCert = ca == null ? "" : caCertName; final String ymlFile = "sample-kibana.yml"; - final Map substitutions = buildSubstitutions(env, Map.ofEntries( - Map.entry("CA_CERT_NAME", caCertName), - Map.entry("CA_CERT", caCert), - Map.entry("YML", ymlFile) - )); + final Map substitutions = buildSubstitutions( + env, + Map.ofEntries(Map.entry("CA_CERT_NAME", caCertName), Map.entry("CA_CERT", caCert), Map.entry("YML", ymlFile)) + ); // TODO : Should we add support for client certs from Kibana to ES? @@ -408,9 +439,11 @@ private void writeKibanaInfo(ZipOutputStream zip, String dirName, CertificateToo * Loads {@code resource} from the classpath, performs variable substitution on it, and then writes it to {@code writer}. */ private void writeTextFile(ZipOutputStream zip, String outputName, String resource, Map substitutions) { - try (InputStream stream = getClass().getResourceAsStream("certutil-http/" + resource); - ZipEntryStream entry = new ZipEntryStream(zip, outputName); - PrintWriter writer = new PrintWriter(entry, false, StandardCharsets.UTF_8)) { + try ( + InputStream stream = getClass().getResourceAsStream("certutil-http/" + resource); + ZipEntryStream entry = new ZipEntryStream(zip, outputName); + PrintWriter writer = new PrintWriter(entry, false, StandardCharsets.UTF_8) + ) { if (stream == null) { throw new IllegalStateException("Cannot find internal resource " + resource); } @@ -470,8 +503,14 @@ private Map buildSubstitutions(Environment env, Map dnsNames = new ArrayList<>(); while (true) { terminal.println(""); - terminal.println("Enter all the hostnames that you need, one per line." ); + terminal.println("Enter all the hostnames that you need, one per line."); terminal.println("When you are done, press once more to move on to the next step."); terminal.println(""); @@ -628,7 +674,9 @@ private CertOptions getCertificateConfiguration(Terminal terminal, boolean multi terminal.println(""); if (certName == null) { - certName = dnsNames.stream().filter(n -> n.indexOf('*') == -1).findFirst() + certName = dnsNames.stream() + .filter(n -> n.indexOf('*') == -1) + .findFirst() .or(() -> dnsNames.stream().map(s -> s.replace("*.", "")).findFirst()) .orElse("elasticsearch"); } @@ -721,7 +769,6 @@ private List readMultiLineInput(Terminal terminal, Function file2 = List.of( "# commented out line", "# commented out line", @@ -101,7 +102,8 @@ public void testRemovePreviousAutoconfigurationRetainsUserAdded() throws Excepti "xpack.security.transport.ssl.keystore.path: /path/to/the/file", "xpack.security.transport.ssl.truststore.path: /path/to/the/file", "", - AutoConfigureNode.AUTO_CONFIGURATION_END_MARKER); + AutoConfigureNode.AUTO_CONFIGURATION_END_MARKER + ); assertEquals(file1, removePreviousAutoconfiguration(file2)); } } diff --git a/x-pack/plugin/security/cli/src/test/java/org/elasticsearch/xpack/security/cli/CertGenUtilsTests.java b/x-pack/plugin/security/cli/src/test/java/org/elasticsearch/xpack/security/cli/CertGenUtilsTests.java index 2c4853bdc3b45..b1df067977dac 100644 --- a/x-pack/plugin/security/cli/src/test/java/org/elasticsearch/xpack/security/cli/CertGenUtilsTests.java +++ b/x-pack/plugin/security/cli/src/test/java/org/elasticsearch/xpack/security/cli/CertGenUtilsTests.java @@ -9,16 +9,12 @@ import org.bouncycastle.asn1.x509.GeneralName; import org.bouncycastle.asn1.x509.GeneralNames; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.common.network.InetAddresses; import org.elasticsearch.common.network.NetworkAddress; +import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.test.ESTestCase; import org.junit.BeforeClass; -import javax.net.ssl.TrustManager; -import javax.net.ssl.TrustManagerFactory; -import javax.net.ssl.X509ExtendedTrustManager; -import javax.security.auth.x500.X500Principal; import java.math.BigInteger; import java.net.InetAddress; import java.security.KeyPair; @@ -30,6 +26,11 @@ import java.util.Collections; import java.util.List; +import javax.net.ssl.TrustManager; +import javax.net.ssl.TrustManagerFactory; +import javax.net.ssl.X509ExtendedTrustManager; +import javax.security.auth.x500.X500Principal; + import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; @@ -44,7 +45,7 @@ public class CertGenUtilsTests extends ESTestCase { @BeforeClass - public static void muteInFips(){ + public static void muteInFips() { assumeFalse("Can't run in a FIPS JVM", inFipsJvm()); } @@ -116,22 +117,49 @@ public void testIssuerCertSubjectDN() throws Exception { // root CA final X500Principal rootCaPrincipal = new X500Principal("DC=example.com"); final KeyPair rootCaKeyPair = CertGenUtils.generateKeyPair(2048); - final X509Certificate rootCaCert = CertGenUtils.generateSignedCertificate(rootCaPrincipal, null, rootCaKeyPair, - null, rootCaKeyPair.getPrivate(), true, notBefore, notAfter, null); + final X509Certificate rootCaCert = CertGenUtils.generateSignedCertificate( + rootCaPrincipal, + null, + rootCaKeyPair, + null, + rootCaKeyPair.getPrivate(), + true, + notBefore, + notAfter, + null + ); // sub CA final X500Principal subCaPrincipal = new X500Principal("DC=Sub CA,DC=example.com"); final KeyPair subCaKeyPair = CertGenUtils.generateKeyPair(2048); - final X509Certificate subCaCert = CertGenUtils.generateSignedCertificate(subCaPrincipal, null, subCaKeyPair, - rootCaCert, rootCaKeyPair.getPrivate(), true, notBefore, notAfter, null); + final X509Certificate subCaCert = CertGenUtils.generateSignedCertificate( + subCaPrincipal, + null, + subCaKeyPair, + rootCaCert, + rootCaKeyPair.getPrivate(), + true, + notBefore, + notAfter, + null + ); // end entity final X500Principal endEntityPrincipal = new X500Principal("CN=TLS Client\\+Server,DC=Sub CA,DC=example.com"); final KeyPair endEntityKeyPair = CertGenUtils.generateKeyPair(2048); - final X509Certificate endEntityCert = CertGenUtils.generateSignedCertificate(endEntityPrincipal, null, endEntityKeyPair, - subCaCert, subCaKeyPair.getPrivate(), true, notBefore, notAfter, null); - - final X509Certificate[] certChain = new X509Certificate[]{endEntityCert, subCaCert, rootCaCert}; + final X509Certificate endEntityCert = CertGenUtils.generateSignedCertificate( + endEntityPrincipal, + null, + endEntityKeyPair, + subCaCert, + subCaKeyPair.getPrivate(), + true, + notBefore, + notAfter, + null + ); + + final X509Certificate[] certChain = new X509Certificate[] { endEntityCert, subCaCert, rootCaCert }; // verify generateSignedCertificate performed DN chaining correctly assertThat(endEntityCert.getIssuerX500Principal(), equalTo(subCaCert.getSubjectX500Principal())); @@ -158,8 +186,10 @@ public void testIssuerCertSubjectDN() throws Exception { * @throws Exception X509ExtendedTrustManager initialization failed, or cert chain validation failed. */ public static void validateEndEntityTlsChain( - final KeyStore trustStore, final X509Certificate[] certChain, - final boolean doTlsClientCheck, final boolean doTlsServerCheck + final KeyStore trustStore, + final X509Certificate[] certChain, + final boolean doTlsClientCheck, + final boolean doTlsServerCheck ) throws Exception { final TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("PKIX", "SunJSSE"); trustManagerFactory.init(trustStore); @@ -184,8 +214,8 @@ public static void validateEndEntityTlsChain( } // EKU=null|serverAuth, KU=digitalSignature, authType=DHE_DSS/DHE_RSA/ECDHE_ECDSA/ECDHE_RSA/RSA_EXPORT/UNKNOWN - // EKU=null|serverAuth, KU=keyEncipherment, authType=RSA - // EKU=null|serverAuth, KU=keyAgreement, authType=DH_DSS/DH_RSA/ECDH_ECDSA/ECDH_RSA + // EKU=null|serverAuth, KU=keyEncipherment, authType=RSA + // EKU=null|serverAuth, KU=keyAgreement, authType=DH_DSS/DH_RSA/ECDH_ECDSA/ECDH_RSA if (doTlsServerCheck) { x509ExtendedTrustManager.checkServerTrusted(certChain, "ECDHE_RSA"); } diff --git a/x-pack/plugin/security/cli/src/test/java/org/elasticsearch/xpack/security/cli/CertificateGenerateToolTests.java b/x-pack/plugin/security/cli/src/test/java/org/elasticsearch/xpack/security/cli/CertificateGenerateToolTests.java index 71a990bd8807f..0bc91a8ee9344 100644 --- a/x-pack/plugin/security/cli/src/test/java/org/elasticsearch/xpack/security/cli/CertificateGenerateToolTests.java +++ b/x-pack/plugin/security/cli/src/test/java/org/elasticsearch/xpack/security/cli/CertificateGenerateToolTests.java @@ -8,13 +8,12 @@ import com.google.common.jimfs.Configuration; import com.google.common.jimfs.Jimfs; -import org.bouncycastle.asn1.DLTaggedObject; -import org.elasticsearch.common.ssl.PemUtils; -import org.elasticsearch.core.internal.io.IOUtils; + import org.bouncycastle.asn1.ASN1ObjectIdentifier; import org.bouncycastle.asn1.ASN1Sequence; import org.bouncycastle.asn1.ASN1String; import org.bouncycastle.asn1.DEROctetString; +import org.bouncycastle.asn1.DLTaggedObject; import org.bouncycastle.asn1.pkcs.Attribute; import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers; import org.bouncycastle.asn1.x509.Extension; @@ -27,23 +26,23 @@ import org.bouncycastle.pkcs.PKCS10CertificationRequest; import org.elasticsearch.cli.MockTerminal; import org.elasticsearch.common.Strings; -import org.elasticsearch.core.SuppressForbidden; -import org.elasticsearch.core.PathUtils; import org.elasticsearch.common.network.NetworkAddress; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.ssl.PemUtils; +import org.elasticsearch.core.PathUtils; +import org.elasticsearch.core.SuppressForbidden; +import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.env.Environment; import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.SecuritySettingsSourceField; +import org.elasticsearch.xpack.core.ssl.CertParsingUtils; import org.elasticsearch.xpack.security.cli.CertificateGenerateTool.CAInfo; import org.elasticsearch.xpack.security.cli.CertificateGenerateTool.CertificateInformation; import org.elasticsearch.xpack.security.cli.CertificateGenerateTool.Name; -import org.elasticsearch.xpack.core.ssl.CertParsingUtils; import org.junit.After; import org.junit.BeforeClass; -import javax.security.auth.x500.X500Principal; - import java.io.IOException; import java.io.InputStream; import java.io.Reader; @@ -74,6 +73,8 @@ import java.util.function.Function; import java.util.stream.Collectors; +import javax.security.auth.x500.X500Principal; + import static org.elasticsearch.test.FileMatchers.pathExists; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; @@ -197,8 +198,8 @@ public void testParsingFile() throws Exception { Collection certInfos = CertificateGenerateTool.parseFile(instanceFile); assertEquals(4, certInfos.size()); - Map certInfosMap = - certInfos.stream().collect(Collectors.toMap((c) -> c.name.originalName, Function.identity())); + Map certInfosMap = certInfos.stream() + .collect(Collectors.toMap((c) -> c.name.originalName, Function.identity())); CertificateInformation certInfo = certInfosMap.get("node1"); assertEquals(Collections.singletonList("127.0.0.1"), certInfo.ipAddresses); assertEquals(Collections.singletonList("localhost"), certInfo.dnsNames); @@ -278,7 +279,7 @@ public void testGeneratingSignedCertificates() throws Exception { final boolean generatedCa = randomBoolean(); final char[] keyPassword = randomBoolean() ? SecuritySettingsSourceField.TEST_PASSWORD.toCharArray() : null; - final char[] pkcs12Password = randomBoolean() ? randomAlphaOfLengthBetween(1, 12).toCharArray() : null; + final char[] pkcs12Password = randomBoolean() ? randomAlphaOfLengthBetween(1, 12).toCharArray() : null; assertFalse(Files.exists(outputFile)); CAInfo caInfo = new CAInfo(caCert, keyPair.getPrivate(), generatedCa, keyPassword); CertificateGenerateTool.generateAndWriteSignedCertificates(outputFile, certInfos, caInfo, keysize, days, pkcs12Password); @@ -341,8 +342,10 @@ public void testGeneratingSignedCertificates() throws Exception { assertNull(certificate.getSubjectAlternativeNames()); } else { X509CertificateHolder x509CertHolder = new X509CertificateHolder(certificate.getEncoded()); - GeneralNames subjAltNames = - GeneralNames.fromExtensions(x509CertHolder.getExtensions(), Extension.subjectAlternativeName); + GeneralNames subjAltNames = GeneralNames.fromExtensions( + x509CertHolder.getExtensions(), + Extension.subjectAlternativeName + ); assertSubjAltNames(subjAltNames, certInfo); } if (pkcs12Password != null) { @@ -374,11 +377,19 @@ public void testGetCAInfo() throws Exception { } final int days = randomIntBetween(1, 1024); - CAInfo caInfo = CertificateGenerateTool.getCAInfo(terminal, "CN=foo", testNodeCertPath.toString(), testNodeKeyPath.toString(), - passwordPrompt ? null : "testnode".toCharArray(), passwordPrompt, env, randomFrom(1024, 2048), days); + CAInfo caInfo = CertificateGenerateTool.getCAInfo( + terminal, + "CN=foo", + testNodeCertPath.toString(), + testNodeKeyPath.toString(), + passwordPrompt ? null : "testnode".toCharArray(), + passwordPrompt, + env, + randomFrom(1024, 2048), + days + ); assertTrue(terminal.getOutput().isEmpty()); - assertEquals(caInfo.caCert.getSubjectX500Principal().getName(), - "CN=Elasticsearch Test Node,OU=elasticsearch,O=org"); + assertEquals(caInfo.caCert.getSubjectX500Principal().getName(), "CN=Elasticsearch Test Node,OU=elasticsearch,O=org"); assertThat(caInfo.privateKey.getAlgorithm(), containsString("RSA")); assertEquals(2048, ((RSAKey) caInfo.privateKey).getModulus().bitLength()); assertFalse(caInfo.generated); @@ -395,8 +406,17 @@ public void testGetCAInfo() throws Exception { password = "testnode".toCharArray(); } final int keysize = randomFrom(1024, 2048); - caInfo = CertificateGenerateTool.getCAInfo(terminal, "CN=foo bar", null, null, password, passwordProtected && passwordPrompt, env, - keysize, days); + caInfo = CertificateGenerateTool.getCAInfo( + terminal, + "CN=foo bar", + null, + null, + password, + passwordProtected && passwordPrompt, + env, + keysize, + days + ); assertTrue(terminal.getOutput().isEmpty()); assertThat(caInfo.caCert, instanceOf(X509Certificate.class)); assertEquals(caInfo.caCert.getSubjectX500Principal().getName(), "CN=foo bar"); @@ -460,8 +480,7 @@ public void testNameValues() throws Exception { } private PKCS10CertificationRequest readCertificateRequest(Path path) throws Exception { - try (Reader reader = Files.newBufferedReader(path); - PEMParser pemParser = new PEMParser(reader)) { + try (Reader reader = Files.newBufferedReader(path); PEMParser pemParser = new PEMParser(reader)) { Object object = pemParser.readObject(); assertThat(object, instanceOf(PKCS10CertificationRequest.class)); return (PKCS10CertificationRequest) object; @@ -523,22 +542,23 @@ private String getValidRandomInstanceName() { */ private Path writeInstancesTo(Path path) throws IOException { Iterable instances = Arrays.asList( - "instances:", - " - name: \"node1\"", - " ip:", - " - \"127.0.0.1\"", - " dns: \"localhost\"", - " - name: \"node2\"", - " filename: \"node2\"", - " ip: \"::1\"", - " cn:", - " - \"node2.elasticsearch\"", - " - name: \"node3\"", - " filename: \"node3\"", - " - name: \"CN=different value\"", - " filename: \"different file\"", - " dns:", - " - \"node4.mydomain.com\""); + "instances:", + " - name: \"node1\"", + " ip:", + " - \"127.0.0.1\"", + " dns: \"localhost\"", + " - name: \"node2\"", + " filename: \"node2\"", + " ip: \"::1\"", + " cn:", + " - \"node2.elasticsearch\"", + " - name: \"node3\"", + " filename: \"node3\"", + " - name: \"CN=different value\"", + " filename: \"different file\"", + " dns:", + " - \"node4.mydomain.com\"" + ); return Files.write(path, instances, StandardCharsets.UTF_8); } diff --git a/x-pack/plugin/security/cli/src/test/java/org/elasticsearch/xpack/security/cli/CertificateToolTests.java b/x-pack/plugin/security/cli/src/test/java/org/elasticsearch/xpack/security/cli/CertificateToolTests.java index 537572cb5844b..664d682595da4 100644 --- a/x-pack/plugin/security/cli/src/test/java/org/elasticsearch/xpack/security/cli/CertificateToolTests.java +++ b/x-pack/plugin/security/cli/src/test/java/org/elasticsearch/xpack/security/cli/CertificateToolTests.java @@ -6,11 +6,13 @@ */ package org.elasticsearch.xpack.security.cli; -import com.google.common.jimfs.Configuration; -import com.google.common.jimfs.Jimfs; import joptsimple.NonOptionArgumentSpec; import joptsimple.OptionSet; import joptsimple.OptionSpec; + +import com.google.common.jimfs.Configuration; +import com.google.common.jimfs.Jimfs; + import org.bouncycastle.asn1.ASN1ObjectIdentifier; import org.bouncycastle.asn1.ASN1Sequence; import org.bouncycastle.asn1.ASN1String; @@ -29,33 +31,28 @@ import org.elasticsearch.cli.Terminal; import org.elasticsearch.cli.UserException; import org.elasticsearch.common.Strings; -import org.elasticsearch.common.ssl.PemUtils; -import org.elasticsearch.core.SuppressForbidden; -import org.elasticsearch.core.PathUtils; import org.elasticsearch.common.network.NetworkAddress; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.ssl.KeyStoreUtil; +import org.elasticsearch.common.ssl.PemUtils; import org.elasticsearch.common.util.CollectionUtils; +import org.elasticsearch.core.PathUtils; +import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.env.Environment; import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.SecuritySettingsSourceField; +import org.elasticsearch.xpack.core.ssl.CertParsingUtils; import org.elasticsearch.xpack.security.cli.CertificateTool.CAInfo; import org.elasticsearch.xpack.security.cli.CertificateTool.CertificateAuthorityCommand; import org.elasticsearch.xpack.security.cli.CertificateTool.CertificateCommand; import org.elasticsearch.xpack.security.cli.CertificateTool.CertificateInformation; import org.elasticsearch.xpack.security.cli.CertificateTool.GenerateCertificateCommand; import org.elasticsearch.xpack.security.cli.CertificateTool.Name; -import org.elasticsearch.xpack.core.ssl.CertParsingUtils; import org.junit.After; import org.junit.BeforeClass; -import javax.net.ssl.KeyManagerFactory; -import javax.net.ssl.TrustManagerFactory; -import javax.net.ssl.X509ExtendedKeyManager; -import javax.net.ssl.X509ExtendedTrustManager; -import javax.security.auth.x500.X500Principal; import java.io.IOException; import java.io.InputStream; import java.io.Reader; @@ -88,6 +85,12 @@ import java.util.function.Function; import java.util.stream.Collectors; +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.TrustManagerFactory; +import javax.net.ssl.X509ExtendedKeyManager; +import javax.net.ssl.X509ExtendedTrustManager; +import javax.security.auth.x500.X500Principal; + import static org.elasticsearch.common.ssl.KeyStoreUtil.createKeyManager; import static org.elasticsearch.common.ssl.KeyStoreUtil.createTrustManager; import static org.elasticsearch.test.FileMatchers.pathExists; @@ -218,8 +221,8 @@ public void testParsingFile() throws Exception { Collection certInfos = CertificateTool.parseFile(instanceFile); assertEquals(4, certInfos.size()); - Map certInfosMap = - certInfos.stream().collect(Collectors.toMap((c) -> c.name.originalName, Function.identity())); + Map certInfosMap = certInfos.stream() + .collect(Collectors.toMap((c) -> c.name.originalName, Function.identity())); CertificateInformation certInfo = certInfosMap.get("node1"); assertEquals(Collections.singletonList("127.0.0.1"), certInfo.ipAddresses); assertEquals(Collections.singletonList("localhost"), certInfo.dnsNames); @@ -249,8 +252,10 @@ public void testParsingFileWithInvalidDetails() throws Exception { Path tempDir = initTempDir(); Path instanceFile = writeInvalidInstanceInformation(tempDir.resolve("instances-invalid.yml")); final MockTerminal terminal = new MockTerminal(); - final UserException exception = expectThrows(UserException.class, - () -> CertificateTool.parseAndValidateFile(terminal, instanceFile)); + final UserException exception = expectThrows( + UserException.class, + () -> CertificateTool.parseAndValidateFile(terminal, instanceFile) + ); assertThat(exception.getMessage(), containsString("invalid configuration")); assertThat(exception.getMessage(), containsString(instanceFile.toString())); assertThat(terminal.getErrorOutput(), containsString("THIS=not a,valid DN")); @@ -316,8 +321,9 @@ public void testGeneratingSignedPemCertificates() throws Exception { final String keyPassword = randomBoolean() ? SecuritySettingsSourceField.TEST_PASSWORD : null; assertFalse(Files.exists(outputFile)); - CAInfo caInfo = selfSigned ? null : - new CAInfo(caCert, keyPair.getPrivate(), false, keyPassword == null ? null : keyPassword.toCharArray()); + CAInfo caInfo = selfSigned + ? null + : new CAInfo(caCert, keyPair.getPrivate(), false, keyPassword == null ? null : keyPassword.toCharArray()); final GenerateCertificateCommand command = new GenerateCertificateCommand(); List args = CollectionUtils.arrayAsArrayList("-keysize", String.valueOf(keySize), "-days", String.valueOf(days), "-pem"); if (keyPassword != null) { @@ -364,8 +370,10 @@ public void testGeneratingSignedPemCertificates() throws Exception { assertNull(certificate.getSubjectAlternativeNames()); } else { X509CertificateHolder x509CertHolder = new X509CertificateHolder(certificate.getEncoded()); - GeneralNames subjAltNames = - GeneralNames.fromExtensions(x509CertHolder.getExtensions(), Extension.subjectAlternativeName); + GeneralNames subjAltNames = GeneralNames.fromExtensions( + x509CertHolder.getExtensions(), + Extension.subjectAlternativeName + ); assertSubjAltNames(subjAltNames, certInfo); } assertThat(p12, not(pathExists())); @@ -384,8 +392,7 @@ public void testErrorMessageOnInvalidKeepCaOption() { return List.of(); } }); - final UserException e = expectThrows(UserException.class, - () -> certificateTool.execute(new MockTerminal(), optionSet)); + final UserException e = expectThrows(UserException.class, () -> certificateTool.execute(new MockTerminal(), optionSet)); assertThat(e.getMessage(), containsString("Generating certificates without providing a CA is no longer supported")); } @@ -416,14 +423,19 @@ public void testHandleLongPasswords() throws Exception { terminal.reset(); final GenerateCertificateCommand genCommand = new PathAwareGenerateCertificateCommand(caFile, pemZipFile); - final OptionSet gen2Options = genCommand.getParser().parse( - "-ca", "", - "-ca-pass", longPassword, - (expectPrompt ? "-pass" : "-pass=" + longPassword), - "-out", "", - "-name", "cert", - "-pem" - ); + final OptionSet gen2Options = genCommand.getParser() + .parse( + "-ca", + "", + "-ca-pass", + longPassword, + (expectPrompt ? "-pass" : "-pass=" + longPassword), + "-out", + "", + "-name", + "cert", + "-pem" + ); if (expectPrompt) { terminal.addSecretInput(longPassword); @@ -473,11 +485,16 @@ public void testGetCAInfo() throws Exception { String caPassword = passwordPrompt ? null : "testnode"; List args = CollectionUtils.arrayAsArrayList( - "-keysize", String.valueOf(keySize), - "-days", String.valueOf(days), - "-pem", - "-ca-cert", testNodeCertPath.toString(), - "-ca-key", testNodeKeyPath.toString()); + "-keysize", + String.valueOf(keySize), + "-days", + String.valueOf(days), + "-pem", + "-ca-cert", + testNodeCertPath.toString(), + "-ca-key", + testNodeKeyPath.toString() + ); args.add("-ca-pass"); if (caPassword != null) { @@ -500,10 +517,14 @@ public void testGetCAInfo() throws Exception { // test generation args = CollectionUtils.arrayAsArrayList( - "-keysize", String.valueOf(keySize), - "-days", String.valueOf(days), - "-pem", - "-ca-dn", "CN=foo bar"); + "-keysize", + String.valueOf(keySize), + "-days", + String.valueOf(days), + "-pem", + "-ca-dn", + "CN=foo bar" + ); final boolean passwordProtected = randomBoolean(); if (passwordProtected) { @@ -621,48 +642,75 @@ public void testCreateCaAndMultipleInstances() throws Exception { final String caPassword = generateCA(caFile, terminal, env); final GenerateCertificateCommand gen1Command = new PathAwareGenerateCertificateCommand(caFile, node1File); - final OptionSet gen1Options = gen1Command.getParser().parse( - "-ca", "", - "-ca-pass", caPassword, - "-pass", node1Password, - "-out", "", - "-keysize", String.valueOf(node1KeySize), - "-days", String.valueOf(days), - "-dns", "node01.cluster1.es.internal.corp.net", - "-ip", node1Ip, - "-name", "node01"); + final OptionSet gen1Options = gen1Command.getParser() + .parse( + "-ca", + "", + "-ca-pass", + caPassword, + "-pass", + node1Password, + "-out", + "", + "-keysize", + String.valueOf(node1KeySize), + "-days", + String.valueOf(days), + "-dns", + "node01.cluster1.es.internal.corp.net", + "-ip", + node1Ip, + "-name", + "node01" + ); gen1Command.execute(terminal, gen1Options, env); assertThat(node1File, pathExists()); final GenerateCertificateCommand gen2Command = new PathAwareGenerateCertificateCommand(caFile, node2File); - final OptionSet gen2Options = gen2Command.getParser().parse( - "-ca", "", - "-ca-pass", caPassword, - "-pass", node2Password, - "-out", "", - "-keysize", String.valueOf(node2KeySize), - "-days", String.valueOf(days), - "-dns", "node02.cluster1.es.internal.corp.net", - "-ip", node2Ip, - "-name", "node02"); + final OptionSet gen2Options = gen2Command.getParser() + .parse( + "-ca", + "", + "-ca-pass", + caPassword, + "-pass", + node2Password, + "-out", + "", + "-keysize", + String.valueOf(node2KeySize), + "-days", + String.valueOf(days), + "-dns", + "node02.cluster1.es.internal.corp.net", + "-ip", + node2Ip, + "-name", + "node02" + ); gen2Command.execute(terminal, gen2Options, env); assertThat(node2File, pathExists()); // Node 3 uses an auto generated CA or a self-signed cert, and therefore should not be trusted by the other nodes. final List gen3Args = CollectionUtils.arrayAsArrayList( - "-pass", node3Password, - "-out", "", - "-keysize", String.valueOf(node3KeySize), - "-days", String.valueOf(days), - "-dns", "node03.cluster2.es.internal.corp.net", - "-ip", node3Ip + "-pass", + node3Password, + "-out", + "", + "-keysize", + String.valueOf(node3KeySize), + "-days", + String.valueOf(days), + "-dns", + "node03.cluster2.es.internal.corp.net", + "-ip", + node3Ip ); gen3Args.add("-self-signed"); final GenerateCertificateCommand gen3Command = new PathAwareGenerateCertificateCommand(null, node3File); - final OptionSet gen3Options = gen3Command.getParser().parse( - Strings.toStringArray(gen3Args)); + final OptionSet gen3Options = gen3Command.getParser().parse(Strings.toStringArray(gen3Args)); gen3Command.execute(terminal, gen3Options, env); assertThat(node3File, pathExists()); @@ -705,7 +753,6 @@ public void testCreateCaAndMultipleInstances() throws Exception { assertEquals(node3x509Certificate.getSubjectX500Principal(), node3x509Certificate.getIssuerX500Principal()); } - /** * A multi-stage test that: * - Creates a ZIP of a PKCS12 cert, with an auto-generated CA @@ -730,15 +777,23 @@ public void testTrustBetweenPEMandPKCS12() throws Exception { final String node1Password = randomAlphaOfLengthBetween(4, 16); final GenerateCertificateCommand gen1Command = new PathAwareGenerateCertificateCommand(caFile, node1Pkcs12); - final OptionSet gen1Options = gen1Command.getParser().parse( - "-ca", "", - "-ca-pass", caPassword, - "-out", "", - "-keysize", String.valueOf(keySize), - "-days", String.valueOf(days), - "-dns", "node01.cluster1.es.internal.corp.net", - "-name", "node01" - ); + final OptionSet gen1Options = gen1Command.getParser() + .parse( + "-ca", + "", + "-ca-pass", + caPassword, + "-out", + "", + "-keysize", + String.valueOf(keySize), + "-days", + String.valueOf(days), + "-dns", + "node01.cluster1.es.internal.corp.net", + "-name", + "node01" + ); terminal.addSecretInput(node1Password); gen1Command.execute(terminal, gen1Options, env); @@ -746,15 +801,22 @@ public void testTrustBetweenPEMandPKCS12() throws Exception { assertThat(node1Pkcs12, pathExists()); final GenerateCertificateCommand gen2Command = new PathAwareGenerateCertificateCommand(caFile, pemZip); - final OptionSet gen2Options = gen2Command.getParser().parse( - "-ca", "", - "-out", "", - "-keysize", String.valueOf(keySize), - "-days", String.valueOf(days), - "-dns", "node02.cluster1.es.internal.corp.net", - "-name", "node02", + final OptionSet gen2Options = gen2Command.getParser() + .parse( + "-ca", + "", + "-out", + "", + "-keysize", + String.valueOf(keySize), + "-days", + String.valueOf(days), + "-dns", + "node02.cluster1.es.internal.corp.net", + "-name", + "node02", "-pem" - ); + ); terminal.addSecretInput(caPassword); gen2Command.execute(terminal, gen2Options, env); @@ -793,9 +855,14 @@ public void testZipOutputFromCommandLineOptions() throws Exception { final AtomicBoolean isZip = new AtomicBoolean(false); final GenerateCertificateCommand genCommand = new PathAwareGenerateCertificateCommand(null, zip) { @Override - void generateAndWriteSignedCertificates(Path output, boolean writeZipFile, OptionSet options, - Collection certs, CAInfo caInfo, - Terminal terminal) throws Exception { + void generateAndWriteSignedCertificates( + Path output, + boolean writeZipFile, + OptionSet options, + Collection certs, + CAInfo caInfo, + Terminal terminal + ) throws Exception { isZip.set(writeZipFile); // do nothing, all we care about is the "zip" flag } @@ -803,17 +870,14 @@ void generateAndWriteSignedCertificates(Path output, boolean writeZipFile, Optio @Override Collection getCertificateInformationList(Terminal terminal, OptionSet options) throws Exception { // Regardless of the commandline options, just work with a single cert - return Collections.singleton(new CertificateInformation("node", "node", - Collections.emptyList(), Collections.emptyList(), Collections.emptyList())); + return Collections.singleton( + new CertificateInformation("node", "node", Collections.emptyList(), Collections.emptyList(), Collections.emptyList()) + ); } }; final String optionThatTriggersZip = randomFrom("-pem", "-multiple", "-in=input.yml"); - final OptionSet genOptions = genCommand.getParser().parse( - "--self-signed", - "-out", "", - optionThatTriggersZip - ); + final OptionSet genOptions = genCommand.getParser().parse("--self-signed", "-out", "", optionThatTriggersZip); genCommand.execute(terminal, genOptions, env); assertThat("For command line option " + optionThatTriggersZip, isZip.get(), equalTo(true)); @@ -831,8 +895,13 @@ private int getDurationInDays(X509Certificate cert) { private void assertSubjAltNames(Certificate certificate, String ip, String dns) throws Exception { final X509CertificateHolder holder = new X509CertificateHolder(certificate.getEncoded()); final GeneralNames names = GeneralNames.fromExtensions(holder.getExtensions(), Extension.subjectAlternativeName); - final CertificateInformation certInfo = new CertificateInformation("n", "n", Collections.singletonList(ip), - Collections.singletonList(dns), Collections.emptyList()); + final CertificateInformation certInfo = new CertificateInformation( + "n", + "n", + Collections.singletonList(ip), + Collections.singletonList(dns), + Collections.emptyList() + ); assertSubjAltNames(names, certInfo); } @@ -858,8 +927,7 @@ private void checkTrust(KeyStore keyStore, char[] keyPassword, KeyStore trustSto } private PKCS10CertificationRequest readCertificateRequest(Path path) throws Exception { - try (Reader reader = Files.newBufferedReader(path); - PEMParser pemParser = new PEMParser(reader)) { + try (Reader reader = Files.newBufferedReader(path); PEMParser pemParser = new PEMParser(reader)) { Object object = pemParser.readObject(); assertThat(object, instanceOf(PKCS10CertificationRequest.class)); return (PKCS10CertificationRequest) object; @@ -920,22 +988,23 @@ private String getValidRandomInstanceName() { */ private Path writeInstancesTo(Path path) throws IOException { Iterable instances = Arrays.asList( - "instances:", - " - name: \"node1\"", - " ip:", - " - \"127.0.0.1\"", - " dns: \"localhost\"", - " - name: \"node2\"", - " filename: \"node2\"", - " ip: \"::1\"", - " cn:", - " - \"node2.elasticsearch\"", - " - name: \"node3\"", - " filename: \"node3\"", - " - name: \"CN=different value\"", - " filename: \"different file\"", - " dns:", - " - \"node4.mydomain.com\""); + "instances:", + " - name: \"node1\"", + " ip:", + " - \"127.0.0.1\"", + " dns: \"localhost\"", + " - name: \"node2\"", + " filename: \"node2\"", + " ip: \"::1\"", + " cn:", + " - \"node2.elasticsearch\"", + " - name: \"node3\"", + " filename: \"node3\"", + " - name: \"CN=different value\"", + " filename: \"different file\"", + " dns:", + " - \"node4.mydomain.com\"" + ); return Files.write(path, instances, StandardCharsets.UTF_8); } @@ -944,10 +1013,7 @@ private Path writeInstancesTo(Path path) throws IOException { * Writes the description of instances to a given {@link Path} */ private Path writeInvalidInstanceInformation(Path path) throws IOException { - Iterable instances = Arrays.asList( - "instances:", - " - name: \"THIS=not a,valid DN\"", - " ip: \"127.0.0.1\""); + Iterable instances = Arrays.asList("instances:", " - name: \"THIS=not a,valid DN\"", " ip: \"127.0.0.1\""); return Files.write(path, instances, StandardCharsets.UTF_8); } @@ -962,13 +1028,19 @@ private String generateCA(Path caFile, MockTerminal terminal, Environment env) t final String caPassword = randomFrom("", randomAlphaOfLengthBetween(4, 80)); final CertificateAuthorityCommand caCommand = new PathAwareCertificateAuthorityCommand(caFile); - final OptionSet caOptions = caCommand.getParser().parse( - "-ca-dn", "CN=My ElasticSearch Cluster", - "-pass", caPassword, - "-out", caFile.toString(), - "-keysize", String.valueOf(caKeySize), - "-days", String.valueOf(days) - ); + final OptionSet caOptions = caCommand.getParser() + .parse( + "-ca-dn", + "CN=My ElasticSearch Cluster", + "-pass", + caPassword, + "-out", + caFile.toString(), + "-keysize", + String.valueOf(caKeySize), + "-days", + String.valueOf(days) + ); caCommand.execute(terminal, caOptions, env); // Check output for OpenSSL compatibility version diff --git a/x-pack/plugin/security/cli/src/test/java/org/elasticsearch/xpack/security/cli/HttpCertificateCommandTests.java b/x-pack/plugin/security/cli/src/test/java/org/elasticsearch/xpack/security/cli/HttpCertificateCommandTests.java index 02c096231ea6b..41f8730adad6f 100644 --- a/x-pack/plugin/security/cli/src/test/java/org/elasticsearch/xpack/security/cli/HttpCertificateCommandTests.java +++ b/x-pack/plugin/security/cli/src/test/java/org/elasticsearch/xpack/security/cli/HttpCertificateCommandTests.java @@ -7,9 +7,11 @@ package org.elasticsearch.xpack.security.cli; +import joptsimple.OptionSet; + import com.google.common.jimfs.Configuration; import com.google.common.jimfs.Jimfs; -import joptsimple.OptionSet; + import org.bouncycastle.asn1.DERIA5String; import org.bouncycastle.asn1.DEROctetString; import org.bouncycastle.asn1.DLSequence; @@ -25,11 +27,10 @@ import org.bouncycastle.util.io.pem.PemReader; import org.elasticsearch.cli.MockTerminal; import org.elasticsearch.common.CheckedBiFunction; +import org.elasticsearch.common.network.NetworkAddress; import org.elasticsearch.common.ssl.PemUtils; import org.elasticsearch.core.CheckedFunction; import org.elasticsearch.core.Tuple; -import org.elasticsearch.common.network.NetworkAddress; -import org.elasticsearch.common.ssl.PemUtils; import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.env.Environment; import org.elasticsearch.test.ESTestCase; @@ -39,7 +40,6 @@ import org.junit.Before; import org.junit.BeforeClass; -import javax.security.auth.x500.X500Principal; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; @@ -81,6 +81,8 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import javax.security.auth.x500.X500Principal; + import static org.elasticsearch.test.FileMatchers.isDirectory; import static org.elasticsearch.test.FileMatchers.isRegularFile; import static org.elasticsearch.test.FileMatchers.pathExists; @@ -209,9 +211,12 @@ public void testGenerateSingleCertificateSigningRequest() throws Exception { // No CA in CSR mode - verifyKibanaDirectory(zipRoot, false, List.of("Certificate Signing Request"), - Stream.of(password, csrName) - .filter(s -> "".equals(s) == false).collect(Collectors.toList())); + verifyKibanaDirectory( + zipRoot, + false, + List.of("Certificate Signing Request"), + Stream.of(password, csrName).filter(s -> "".equals(s) == false).collect(Collectors.toList()) + ); } public void testGenerateSingleCertificateWithExistingCA() throws Exception { @@ -331,9 +336,14 @@ public void testGenerateSingleCertificateWithExistingCA() throws Exception { // Should not be a CA directory when using an existing CA. assertThat(zipRoot.resolve("ca"), not(pathExists())); - verifyKibanaDirectory(zipRoot, true, List.of("2. elasticsearch-ca.pem"), + verifyKibanaDirectory( + zipRoot, + true, + List.of("2. elasticsearch-ca.pem"), Stream.of(password, caPassword, caKeyPath.getFileName().toString()) - .filter(s -> "".equals(s) == false).collect(Collectors.toList())); + .filter(s -> "".equals(s) == false) + .collect(Collectors.toList()) + ); } public void testGenerateMultipleCertificateWithNewCA() throws Exception { @@ -418,7 +428,6 @@ public void testGenerateMultipleCertificateWithNewCA() throws Exception { } terminal.addTextInput("n"); // no more certs - final String password = randomPassword(false); // randomly enter an incorrect password here which will fail the "enter twice" check and prompt to try again if (randomBoolean()) { @@ -496,9 +505,12 @@ public void testGenerateMultipleCertificateWithNewCA() throws Exception { } } - verifyKibanaDirectory(zipRoot, true, List.of("2. elasticsearch-ca.pem"), - Stream.of(password, caPassword, caPath.getFileName().toString()) - .filter(s -> "".equals(s) == false).collect(Collectors.toList())); + verifyKibanaDirectory( + zipRoot, + true, + List.of("2. elasticsearch-ca.pem"), + Stream.of(password, caPassword, caPath.getFileName().toString()).filter(s -> "".equals(s) == false).collect(Collectors.toList()) + ); } public void testParsingValidityPeriod() throws Exception { @@ -596,9 +608,11 @@ public void testGuessFileType() throws Exception { public void testTextFileSubstitutions() throws Exception { CheckedBiFunction, String, Exception> copy = (source, subs) -> { - try (InputStream in = new ByteArrayInputStream(source.getBytes(StandardCharsets.UTF_8)); - StringWriter out = new StringWriter(); - PrintWriter writer = new PrintWriter(out)) { + try ( + InputStream in = new ByteArrayInputStream(source.getBytes(StandardCharsets.UTF_8)); + StringWriter out = new StringWriter(); + PrintWriter writer = new PrintWriter(out) + ) { HttpCertificateCommand.copyWithSubstitutions(in, writer, subs); return out.toString().replace("\r\n", "\n"); } @@ -661,8 +675,12 @@ private String randomPassword(boolean longPassword) { ); } - private void verifyCertificationRequest(PKCS10CertificationRequest csr, String certificateName, List hostNames, - List ipAddresses) throws IOException { + private void verifyCertificationRequest( + PKCS10CertificationRequest csr, + String certificateName, + List hostNames, + List ipAddresses + ) throws IOException { // We rebuild the DN from the encoding because BC uses openSSL style toString, but we use LDAP style. assertThat(new X500Principal(csr.getSubject().getEncoded()).toString(), is("CN=" + certificateName.replaceAll("\\.", ", DC="))); final Attribute[] extensionAttributes = csr.getAttributes(PKCSObjectIdentifiers.pkcs_9_at_extensionRequest); @@ -687,8 +705,13 @@ private void verifyCertificationRequest(PKCS10CertificationRequest csr, String c } } - private void verifyCertificate(X509Certificate cert, String certificateName, int years, - List hostNames, List ipAddresses) throws CertificateParsingException { + private void verifyCertificate( + X509Certificate cert, + String certificateName, + int years, + List hostNames, + List ipAddresses + ) throws CertificateParsingException { assertThat(cert.getSubjectX500Principal().toString(), is("CN=" + certificateName.replaceAll("\\.", ", DC="))); final Collection> san = cert.getSubjectAlternativeNames(); final int expectedSanEntries = hostNames.size() + ipAddresses.size(); @@ -746,8 +769,12 @@ private void assertMatchingPair(PublicKey publicKey, PrivateKey privateKey) thro assertTrue("PublicKey and PrivateKey are not a matching pair", rsa.verify(signature)); } - private void verifyKibanaDirectory(Path zipRoot, boolean expectCAFile, Iterable readmeShouldContain, - Iterable shouldNotContain) throws IOException { + private void verifyKibanaDirectory( + Path zipRoot, + boolean expectCAFile, + Iterable readmeShouldContain, + Iterable shouldNotContain + ) throws IOException { assertThat(zipRoot.resolve("kibana"), isDirectory()); if (expectCAFile) { assertThat(zipRoot.resolve("kibana/elasticsearch-ca.pem"), isRegularFile()); @@ -785,8 +812,8 @@ private int getRSAKeySize(Key key) { return rsa.getModulus().bitLength(); } - private Tuple readCertificateAndKey(Path pkcs12, - char[] password) throws IOException, GeneralSecurityException { + private Tuple readCertificateAndKey(Path pkcs12, char[] password) throws IOException, + GeneralSecurityException { final Map entries = CertParsingUtils.readPkcs12KeyPairs(pkcs12, password, alias -> password); assertThat(entries.entrySet(), Matchers.hasSize(1)); @@ -800,8 +827,8 @@ private Tuple readCertificateAndKey(Path pkcs12, return new Tuple<>((X509Certificate) cert, (PrivateKey) key); } - private T readPemObject(Path path, String expectedType, - CheckedFunction factory) throws IOException { + private T readPemObject(Path path, String expectedType, CheckedFunction factory) + throws IOException { assertThat(path, isRegularFile()); final PemReader csrReader = new PemReader(Files.newBufferedReader(path)); final PemObject csrPem = csrReader.readPemObject(); diff --git a/x-pack/plugin/security/qa/basic-enable-security/src/javaRestTest/java/org/elasticsearch/xpack/security/EnableSecurityOnBasicLicenseIT.java b/x-pack/plugin/security/qa/basic-enable-security/src/javaRestTest/java/org/elasticsearch/xpack/security/EnableSecurityOnBasicLicenseIT.java index fc6cf885168c5..12ddd1478f78b 100644 --- a/x-pack/plugin/security/qa/basic-enable-security/src/javaRestTest/java/org/elasticsearch/xpack/security/EnableSecurityOnBasicLicenseIT.java +++ b/x-pack/plugin/security/qa/basic-enable-security/src/javaRestTest/java/org/elasticsearch/xpack/security/EnableSecurityOnBasicLicenseIT.java @@ -13,10 +13,10 @@ import org.elasticsearch.client.ResponseException; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClientBuilder; -import org.elasticsearch.core.Booleans; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.Booleans; import org.elasticsearch.test.rest.ESRestTestCase; import org.elasticsearch.test.rest.yaml.ObjectPath; import org.elasticsearch.xpack.security.authc.InternalRealms; @@ -47,17 +47,13 @@ public static void checkTestMode() { @Override protected Settings restAdminSettings() { String token = basicAuthHeaderValue("admin_user", new SecureString("admin-password".toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } @Override protected Settings restClientSettings() { String token = basicAuthHeaderValue("security_test_user", new SecureString("security-test-password".toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } @Override @@ -74,7 +70,6 @@ protected RestClient buildClient(Settings settings, HttpHost[] hosts) throws IOE return builder.build(); } - public void testSecuritySetup() throws Exception { logger.info("Security status: {}", securityEnabled); logger.info("Cluster:\n{}", getClusterInfo()); @@ -104,7 +99,7 @@ public void checkSecurityDisabledWarning() throws Exception { final Request request = new Request("GET", "/_cat/indices"); Response response = client().performRequest(request); List warningHeaders = response.getWarnings(); - assertThat (warningHeaders, Matchers.empty()); + assertThat(warningHeaders, Matchers.empty()); } private String getClusterInfo() throws IOException { diff --git a/x-pack/plugin/security/qa/security-basic/src/javaRestTest/java/org/elasticsearch/xpack/security/QueryApiKeyIT.java b/x-pack/plugin/security/qa/security-basic/src/javaRestTest/java/org/elasticsearch/xpack/security/QueryApiKeyIT.java index 57ece89a58fa2..5e127e0c68a71 100644 --- a/x-pack/plugin/security/qa/security-basic/src/javaRestTest/java/org/elasticsearch/xpack/security/QueryApiKeyIT.java +++ b/x-pack/plugin/security/qa/security-basic/src/javaRestTest/java/org/elasticsearch/xpack/security/QueryApiKeyIT.java @@ -11,9 +11,9 @@ import org.elasticsearch.client.Request; import org.elasticsearch.client.Response; import org.elasticsearch.client.ResponseException; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.core.Tuple; import org.elasticsearch.test.XContentTestUtils; +import org.elasticsearch.xcontent.XContentType; import java.io.IOException; import java.nio.charset.StandardCharsets; @@ -48,122 +48,124 @@ public void testQuery() throws IOException { createUser("someone"); // Admin with manage_api_key can search for all keys - assertQuery(API_KEY_ADMIN_AUTH_HEADER, - "{ \"query\": { \"wildcard\": {\"name\": \"*alert*\"} } }", - apiKeys -> { - assertThat(apiKeys.size(), equalTo(2)); - assertThat(apiKeys.get(0).get("name"), oneOf("my-org/alert-key-1", "my-alert-key-2")); - assertThat(apiKeys.get(1).get("name"), oneOf("my-org/alert-key-1", "my-alert-key-2")); - apiKeys.forEach(k -> assertThat(k, not(hasKey("_sort")))); - }); + assertQuery(API_KEY_ADMIN_AUTH_HEADER, "{ \"query\": { \"wildcard\": {\"name\": \"*alert*\"} } }", apiKeys -> { + assertThat(apiKeys.size(), equalTo(2)); + assertThat(apiKeys.get(0).get("name"), oneOf("my-org/alert-key-1", "my-alert-key-2")); + assertThat(apiKeys.get(1).get("name"), oneOf("my-org/alert-key-1", "my-alert-key-2")); + apiKeys.forEach(k -> assertThat(k, not(hasKey("_sort")))); + }); // An empty request body means search for all keys - assertQuery(API_KEY_ADMIN_AUTH_HEADER, + assertQuery( + API_KEY_ADMIN_AUTH_HEADER, randomBoolean() ? "" : "{\"query\":{\"match_all\":{}}}", - apiKeys -> assertThat(apiKeys.size(), equalTo(6))); + apiKeys -> assertThat(apiKeys.size(), equalTo(6)) + ); - assertQuery(API_KEY_ADMIN_AUTH_HEADER, - "{\"query\":{\"bool\":{\"must\":[" + - "{\"prefix\":{\"metadata.application\":\"fleet\"}},{\"term\":{\"metadata.environment.os\":\"Cat\"}}]}}}", + assertQuery( + API_KEY_ADMIN_AUTH_HEADER, + "{\"query\":{\"bool\":{\"must\":[" + + "{\"prefix\":{\"metadata.application\":\"fleet\"}},{\"term\":{\"metadata.environment.os\":\"Cat\"}}]}}}", apiKeys -> { assertThat(apiKeys, hasSize(2)); assertThat( apiKeys.stream().map(k -> k.get("name")).collect(Collectors.toList()), - containsInAnyOrder("my-org/ingest-key-1", "my-org/management-key-1")); + containsInAnyOrder("my-org/ingest-key-1", "my-org/management-key-1") + ); apiKeys.forEach(k -> assertThat(k, not(hasKey("_sort")))); } ); - assertQuery(API_KEY_ADMIN_AUTH_HEADER, - "{\"query\":{\"terms\":{\"metadata.tags\":[\"prod\",\"east\"]}}}", - apiKeys -> { - assertThat(apiKeys.size(), equalTo(5)); - apiKeys.forEach(k -> assertThat(k, not(hasKey("_sort")))); - }); + assertQuery(API_KEY_ADMIN_AUTH_HEADER, "{\"query\":{\"terms\":{\"metadata.tags\":[\"prod\",\"east\"]}}}", apiKeys -> { + assertThat(apiKeys.size(), equalTo(5)); + apiKeys.forEach(k -> assertThat(k, not(hasKey("_sort")))); + }); - assertQuery(API_KEY_ADMIN_AUTH_HEADER, - "{\"query\":{\"range\":{\"creation\":{\"lt\":\"now\"}}}}", - apiKeys -> { - assertThat(apiKeys.size(), equalTo(6)); - apiKeys.forEach(k -> assertThat(k, not(hasKey("_sort")))); - }); + assertQuery(API_KEY_ADMIN_AUTH_HEADER, "{\"query\":{\"range\":{\"creation\":{\"lt\":\"now\"}}}}", apiKeys -> { + assertThat(apiKeys.size(), equalTo(6)); + apiKeys.forEach(k -> assertThat(k, not(hasKey("_sort")))); + }); // Search for keys belong to an user - assertQuery(API_KEY_ADMIN_AUTH_HEADER, - "{ \"query\": { \"term\": {\"username\": \"api_key_user\"} } }", - apiKeys -> { - assertThat(apiKeys.size(), equalTo(2)); - assertThat(apiKeys.stream().map(m -> m.get("name")).collect(Collectors.toSet()), - equalTo(Set.of("my-ingest-key-1", "my-alert-key-2"))); - apiKeys.forEach(k -> assertThat(k, not(hasKey("_sort")))); - }); + assertQuery(API_KEY_ADMIN_AUTH_HEADER, "{ \"query\": { \"term\": {\"username\": \"api_key_user\"} } }", apiKeys -> { + assertThat(apiKeys.size(), equalTo(2)); + assertThat( + apiKeys.stream().map(m -> m.get("name")).collect(Collectors.toSet()), + equalTo(Set.of("my-ingest-key-1", "my-alert-key-2")) + ); + apiKeys.forEach(k -> assertThat(k, not(hasKey("_sort")))); + }); // Search for keys belong to users from a realm - assertQuery(API_KEY_ADMIN_AUTH_HEADER, - "{ \"query\": { \"term\": {\"realm_name\": \"default_file\"} } }", - apiKeys -> { - assertThat(apiKeys.size(), equalTo(6)); - apiKeys.forEach(k -> assertThat(k, not(hasKey("_sort")))); - // search using explicit IDs - try { - - var subset = randomSubsetOf(randomIntBetween(1,5), apiKeys); - assertQuery(API_KEY_ADMIN_AUTH_HEADER, - "{ \"query\": { \"ids\": { \"values\": [" - + subset.stream().map(m -> "\"" + m.get("id") + "\"").collect(Collectors.joining(",")) + "] } } }", - keys -> { - assertThat(keys, hasSize(subset.size())); - keys.forEach(k -> assertThat(k, not(hasKey("_sort")))); - }); - } catch (IOException e) { - throw new RuntimeException(e); - } - }); + assertQuery(API_KEY_ADMIN_AUTH_HEADER, "{ \"query\": { \"term\": {\"realm_name\": \"default_file\"} } }", apiKeys -> { + assertThat(apiKeys.size(), equalTo(6)); + apiKeys.forEach(k -> assertThat(k, not(hasKey("_sort")))); + // search using explicit IDs + try { + + var subset = randomSubsetOf(randomIntBetween(1, 5), apiKeys); + assertQuery( + API_KEY_ADMIN_AUTH_HEADER, + "{ \"query\": { \"ids\": { \"values\": [" + + subset.stream().map(m -> "\"" + m.get("id") + "\"").collect(Collectors.joining(",")) + + "] } } }", + keys -> { + assertThat(keys, hasSize(subset.size())); + keys.forEach(k -> assertThat(k, not(hasKey("_sort")))); + } + ); + } catch (IOException e) { + throw new RuntimeException(e); + } + }); // Search for fields outside of the allowlist fails - assertQueryError(API_KEY_ADMIN_AUTH_HEADER, 400, - "{ \"query\": { \"prefix\": {\"api_key_hash\": \"{PBKDF2}10000$\"} } }"); + assertQueryError(API_KEY_ADMIN_AUTH_HEADER, 400, "{ \"query\": { \"prefix\": {\"api_key_hash\": \"{PBKDF2}10000$\"} } }"); // Search for fields that are not allowed in Query DSL but used internally by the service itself final String fieldName = randomFrom("doc_type", "api_key_invalidated"); - assertQueryError(API_KEY_ADMIN_AUTH_HEADER, 400, - "{ \"query\": { \"term\": {\"" + fieldName + "\": \"" + randomAlphaOfLengthBetween(3, 8) + "\"} } }"); + assertQueryError( + API_KEY_ADMIN_AUTH_HEADER, + 400, + "{ \"query\": { \"term\": {\"" + fieldName + "\": \"" + randomAlphaOfLengthBetween(3, 8) + "\"} } }" + ); // Search for api keys won't return other entities - assertQuery(API_KEY_ADMIN_AUTH_HEADER, + assertQuery( + API_KEY_ADMIN_AUTH_HEADER, "{ \"query\": { \"term\": {\"name\": \"someone\"} } }", - apiKeys -> { - assertThat(apiKeys, empty()); - }); + apiKeys -> { assertThat(apiKeys, empty()); } + ); // User with manage_own_api_key will only see its own keys - assertQuery(API_KEY_USER_AUTH_HEADER, - randomBoolean() ? "" : "{\"query\":{\"match_all\":{}}}", - apiKeys -> { + assertQuery(API_KEY_USER_AUTH_HEADER, randomBoolean() ? "" : "{\"query\":{\"match_all\":{}}}", apiKeys -> { assertThat(apiKeys.size(), equalTo(2)); - assertThat(apiKeys.stream().map(m -> m.get("name")).collect(Collectors.toSet()), - containsInAnyOrder("my-ingest-key-1", "my-alert-key-2")); + assertThat( + apiKeys.stream().map(m -> m.get("name")).collect(Collectors.toSet()), + containsInAnyOrder("my-ingest-key-1", "my-alert-key-2") + ); apiKeys.forEach(k -> assertThat(k, not(hasKey("_sort")))); }); - assertQuery(API_KEY_USER_AUTH_HEADER, - "{ \"query\": { \"wildcard\": {\"name\": \"*alert*\"} } }", - apiKeys -> { - assertThat(apiKeys.size(), equalTo(1)); - assertThat(apiKeys.get(0).get("name"), equalTo("my-alert-key-2")); - apiKeys.forEach(k -> assertThat(k, not(hasKey("_sort")))); - }); + assertQuery(API_KEY_USER_AUTH_HEADER, "{ \"query\": { \"wildcard\": {\"name\": \"*alert*\"} } }", apiKeys -> { + assertThat(apiKeys.size(), equalTo(1)); + assertThat(apiKeys.get(0).get("name"), equalTo("my-alert-key-2")); + apiKeys.forEach(k -> assertThat(k, not(hasKey("_sort")))); + }); // User without manage_api_key or manage_own_api_key gets 403 trying to search API keys - assertQueryError(TEST_USER_AUTH_HEADER, 403, - "{ \"query\": { \"wildcard\": {\"name\": \"*alert*\"} } }"); + assertQueryError(TEST_USER_AUTH_HEADER, 403, "{ \"query\": { \"wildcard\": {\"name\": \"*alert*\"} } }"); // Invalidated API keys are returned by default, but can be filtered out final String authHeader = randomFrom(API_KEY_ADMIN_AUTH_HEADER, API_KEY_USER_AUTH_HEADER); final String invalidatedApiKeyId1 = createAndInvalidateApiKey("temporary-key-1", authHeader); - final String queryString = randomFrom("{ \"query\": { \"term\": {\"name\": \"temporary-key-1\"} } }", - "{\"query\":{\"bool\":{\"must\":[{\"term\":{\"name\":{\"value\":\"temporary-key-1\"}}}," + - "{\"term\":{\"invalidated\":{\"value\":\"" + randomBoolean() + "\"}}}]}}}"); + final String queryString = randomFrom( + "{ \"query\": { \"term\": {\"name\": \"temporary-key-1\"} } }", + "{\"query\":{\"bool\":{\"must\":[{\"term\":{\"name\":{\"value\":\"temporary-key-1\"}}}," + + "{\"term\":{\"invalidated\":{\"value\":\"" + + randomBoolean() + + "\"}}}]}}}" + ); assertQuery(authHeader, queryString, apiKeys -> { if (queryString.contains("\"invalidated\":{\"value\":\"false\"")) { @@ -180,13 +182,17 @@ public void testQuery() throws IOException { public void testQueryShouldRespectOwnerIdentityWithApiKeyAuth() throws IOException { final Tuple powerKey = createApiKey("power-key-1", null, null, API_KEY_ADMIN_AUTH_HEADER); - final String powerKeyAuthHeader = "ApiKey " + Base64.getEncoder() - .encodeToString((powerKey.v1() + ":" + powerKey.v2()).getBytes(StandardCharsets.UTF_8)); - - final Tuple limitKey = createApiKey("limit-key-1", - Map.of("a", Map.of("cluster", List.of("manage_own_api_key"))), null, API_KEY_ADMIN_AUTH_HEADER); - final String limitKeyAuthHeader = "ApiKey " + Base64.getEncoder() - .encodeToString((limitKey.v1() + ":" + limitKey.v2()).getBytes(StandardCharsets.UTF_8)); + final String powerKeyAuthHeader = "ApiKey " + + Base64.getEncoder().encodeToString((powerKey.v1() + ":" + powerKey.v2()).getBytes(StandardCharsets.UTF_8)); + + final Tuple limitKey = createApiKey( + "limit-key-1", + Map.of("a", Map.of("cluster", List.of("manage_own_api_key"))), + null, + API_KEY_ADMIN_AUTH_HEADER + ); + final String limitKeyAuthHeader = "ApiKey " + + Base64.getEncoder().encodeToString((limitKey.v1() + ":" + limitKey.v2()).getBytes(StandardCharsets.UTF_8)); createApiKey("power-key-1-derived-1", Map.of("a", Map.of()), null, powerKeyAuthHeader); createApiKey("limit-key-1-derived-1", Map.of("a", Map.of()), null, limitKeyAuthHeader); @@ -199,8 +205,8 @@ public void testQueryShouldRespectOwnerIdentityWithApiKeyAuth() throws IOExcepti assertThat(apiKeys.size(), equalTo(6)); assertThat( apiKeys.stream().map(m -> (String) m.get("name")).collect(Collectors.toUnmodifiableSet()), - equalTo(Set.of("power-key-1", "limit-key-1", "power-key-1-derived-1", "limit-key-1-derived-1", - "user-key-1", "user-key-2"))); + equalTo(Set.of("power-key-1", "limit-key-1", "power-key-1-derived-1", "limit-key-1-derived-1", "user-key-1", "user-key-2")) + ); apiKeys.forEach(k -> assertThat(k, not(hasKey("_sort")))); }); @@ -210,7 +216,8 @@ public void testQueryShouldRespectOwnerIdentityWithApiKeyAuth() throws IOExcepti assertThat(apiKeys.size(), equalTo(2)); assertThat( apiKeys.stream().map(m -> (String) m.get("name")).collect(Collectors.toUnmodifiableSet()), - equalTo(Set.of("power-key-1", "limit-key-1"))); + equalTo(Set.of("power-key-1", "limit-key-1")) + ); apiKeys.forEach(k -> assertThat(k, not(hasKey("_sort")))); }); @@ -219,8 +226,9 @@ public void testQueryShouldRespectOwnerIdentityWithApiKeyAuth() throws IOExcepti public void testPagination() throws IOException, InterruptedException { final String authHeader = randomFrom(API_KEY_ADMIN_AUTH_HEADER, API_KEY_USER_AUTH_HEADER); final int total = randomIntBetween(8, 12); - final List apiKeyNames = - IntStream.range(0, total).mapToObj(i -> String.format(Locale.ROOT, "k-%02d", i)).collect(Collectors.toUnmodifiableList()); + final List apiKeyNames = IntStream.range(0, total) + .mapToObj(i -> String.format(Locale.ROOT, "k-%02d", i)) + .collect(Collectors.toUnmodifiableList()); final List apiKeyIds = new ArrayList<>(total); for (int i = 0; i < total; i++) { apiKeyIds.add(createApiKey(apiKeyNames.get(i), null, authHeader).v1()); @@ -264,18 +272,22 @@ public void testPagination() throws IOException, InterruptedException { if ("name".equals(sortField)) { assertThat( apiKeyInfos.stream().map(m -> (String) m.get("name")).collect(Collectors.toUnmodifiableList()), - equalTo(apiKeyInfos.stream().map(m -> (String) extractSortValues(m).get(0)).collect(Collectors.toUnmodifiableList()))); + equalTo(apiKeyInfos.stream().map(m -> (String) extractSortValues(m).get(0)).collect(Collectors.toUnmodifiableList())) + ); } else { assertThat( apiKeyInfos.stream().map(m -> (long) m.get("creation")).collect(Collectors.toUnmodifiableList()), - equalTo(apiKeyInfos.stream().map(m -> (long) extractSortValues(m).get(0)).collect(Collectors.toUnmodifiableList()))); + equalTo(apiKeyInfos.stream().map(m -> (long) extractSortValues(m).get(0)).collect(Collectors.toUnmodifiableList())) + ); } assertThat( apiKeyInfos.stream().map(m -> (String) m.get("id")).collect(Collectors.toUnmodifiableList()), - equalTo(apiKeyIds.subList(from, total))); + equalTo(apiKeyIds.subList(from, total)) + ); assertThat( apiKeyInfos.stream().map(m -> (String) m.get("name")).collect(Collectors.toUnmodifiableList()), - equalTo(apiKeyNames.subList(from, total))); + equalTo(apiKeyNames.subList(from, total)) + ); // size can be zero, but total should still reflect the number of keys matched final Request request2 = new Request("GET", "/_security/_query/api_key"); @@ -298,7 +310,7 @@ public void testSort() throws IOException { assertQuery(authHeader, "{\"sort\":[{\"creation\":{\"order\":\"desc\"}}]}", apiKeys -> { assertThat(apiKeys.size(), equalTo(3)); - for (int i = 2, j = 0; i >=0; i--, j++) { + for (int i = 2, j = 0; i >= 0; i--, j++) { assertThat(apiKeys.get(i).get("id"), equalTo(apiKeyIds.get(j))); assertThat(apiKeys.get(i).get("creation"), equalTo(((List) apiKeys.get(i).get("_sort")).get(0))); } @@ -306,7 +318,7 @@ public void testSort() throws IOException { assertQuery(authHeader, "{\"sort\":[{\"name\":{\"order\":\"asc\"}}]}", apiKeys -> { assertThat(apiKeys.size(), equalTo(3)); - for (int i = 2, j = 0; i >=0; i--, j++) { + for (int i = 2, j = 0; i >= 0; i--, j++) { assertThat(apiKeys.get(i).get("id"), equalTo(apiKeyIds.get(j))); assertThat(apiKeys.get(i).get("name"), equalTo(((List) apiKeys.get(i).get("_sort")).get(0))); } @@ -368,18 +380,15 @@ private int collectApiKeys(List> apiKeyInfos, Request reques private void assertQueryError(String authHeader, int statusCode, String body) throws IOException { final Request request = new Request("GET", "/_security/_query/api_key"); request.setJsonEntity(body); - request.setOptions( - request.getOptions().toBuilder().addHeader(HttpHeaders.AUTHORIZATION, authHeader)); + request.setOptions(request.getOptions().toBuilder().addHeader(HttpHeaders.AUTHORIZATION, authHeader)); final ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest(request)); assertThat(responseException.getResponse().getStatusLine().getStatusCode(), equalTo(statusCode)); } - private void assertQuery(String authHeader, String body, - Consumer>> apiKeysVerifier) throws IOException { + private void assertQuery(String authHeader, String body, Consumer>> apiKeysVerifier) throws IOException { final Request request = new Request("GET", "/_security/_query/api_key"); request.setJsonEntity(body); - request.setOptions( - request.getOptions().toBuilder().addHeader(HttpHeaders.AUTHORIZATION, authHeader)); + request.setOptions(request.getOptions().toBuilder().addHeader(HttpHeaders.AUTHORIZATION, authHeader)); final Response response = client().performRequest(request); assertOK(response); final Map responseMap = responseAsMap(response); @@ -392,85 +401,99 @@ private void createApiKeys() throws IOException { createApiKey( "my-org/ingest-key-1", Map.of( - "application", "fleet-agent", - "tags", List.of("prod", "east"), - "environment", Map.of( - "os", "Cat", "level", 42, "system", false, "hostname", "my-org-host-1") + "application", + "fleet-agent", + "tags", + List.of("prod", "east"), + "environment", + Map.of("os", "Cat", "level", 42, "system", false, "hostname", "my-org-host-1") ), - API_KEY_ADMIN_AUTH_HEADER); + API_KEY_ADMIN_AUTH_HEADER + ); createApiKey( "my-org/ingest-key-2", Map.of( - "application", "fleet-server", - "tags", List.of("staging", "east"), - "environment", Map.of( - "os", "Dog", "level", 11, "system", true, "hostname", "my-org-host-2") + "application", + "fleet-server", + "tags", + List.of("staging", "east"), + "environment", + Map.of("os", "Dog", "level", 11, "system", true, "hostname", "my-org-host-2") ), - API_KEY_ADMIN_AUTH_HEADER); + API_KEY_ADMIN_AUTH_HEADER + ); createApiKey( "my-org/management-key-1", Map.of( - "application", "fleet-agent", - "tags", List.of("prod", "west"), - "environment", Map.of( - "os", "Cat", "level", 11, "system", false, "hostname", "my-org-host-3") + "application", + "fleet-agent", + "tags", + List.of("prod", "west"), + "environment", + Map.of("os", "Cat", "level", 11, "system", false, "hostname", "my-org-host-3") ), - API_KEY_ADMIN_AUTH_HEADER); + API_KEY_ADMIN_AUTH_HEADER + ); createApiKey( "my-org/alert-key-1", Map.of( - "application", "siem", - "tags", List.of("prod", "north", "upper"), - "environment", Map.of( - "os", "Dog", "level", 3, "system", true, "hostname", "my-org-host-4") + "application", + "siem", + "tags", + List.of("prod", "north", "upper"), + "environment", + Map.of("os", "Dog", "level", 3, "system", true, "hostname", "my-org-host-4") ), - API_KEY_ADMIN_AUTH_HEADER); + API_KEY_ADMIN_AUTH_HEADER + ); createApiKey( "my-ingest-key-1", - Map.of( - "application", "cli", - "tags", List.of("user", "test"), - "notes", Map.of( - "sun", "hot", "earth", "blue") - ), - API_KEY_USER_AUTH_HEADER); + Map.of("application", "cli", "tags", List.of("user", "test"), "notes", Map.of("sun", "hot", "earth", "blue")), + API_KEY_USER_AUTH_HEADER + ); createApiKey( "my-alert-key-2", - Map.of( - "application", "web", - "tags", List.of("app", "prod"), - "notes", Map.of( - "shared", false, "weather", "sunny") - ), - API_KEY_USER_AUTH_HEADER); + Map.of("application", "web", "tags", List.of("app", "prod"), "notes", Map.of("shared", false, "weather", "sunny")), + API_KEY_USER_AUTH_HEADER + ); } private Tuple createApiKey(String name, Map metadata, String authHeader) throws IOException { return createApiKey(name, null, metadata, authHeader); } - private Tuple createApiKey(String name, - Map roleDescriptors, - Map metadata, - String authHeader) throws IOException { + private Tuple createApiKey( + String name, + Map roleDescriptors, + Map metadata, + String authHeader + ) throws IOException { final Request request = new Request("POST", "/_security/api_key"); - final String roleDescriptorsString = - XContentTestUtils.convertToXContent(roleDescriptors == null ? Map.of() : roleDescriptors, XContentType.JSON).utf8ToString(); - final String metadataString = - XContentTestUtils.convertToXContent(metadata == null ? Map.of() : metadata, XContentType.JSON).utf8ToString(); + final String roleDescriptorsString = XContentTestUtils.convertToXContent( + roleDescriptors == null ? Map.of() : roleDescriptors, + XContentType.JSON + ).utf8ToString(); + final String metadataString = XContentTestUtils.convertToXContent(metadata == null ? Map.of() : metadata, XContentType.JSON) + .utf8ToString(); if (randomBoolean()) { - request.setJsonEntity("{\"name\":\"" + name - + "\", \"role_descriptors\":" + roleDescriptorsString - + ", \"metadata\":" + metadataString + "}"); + request.setJsonEntity( + "{\"name\":\"" + name + "\", \"role_descriptors\":" + roleDescriptorsString + ", \"metadata\":" + metadataString + "}" + ); } else { - request.setJsonEntity("{\"name\":\"" + name - + "\", \"expiration\": \"10d\", \"role_descriptors\":" + roleDescriptorsString - + ", \"metadata\":" + metadataString + "}"); + request.setJsonEntity( + "{\"name\":\"" + + name + + "\", \"expiration\": \"10d\", \"role_descriptors\":" + + roleDescriptorsString + + ", \"metadata\":" + + metadataString + + "}" + ); } request.setOptions(request.getOptions().toBuilder().addHeader(HttpHeaders.AUTHORIZATION, authHeader)); final Response response = client().performRequest(request); diff --git a/x-pack/plugin/security/qa/security-basic/src/javaRestTest/java/org/elasticsearch/xpack/security/SecurityInBasicRestTestCase.java b/x-pack/plugin/security/qa/security-basic/src/javaRestTest/java/org/elasticsearch/xpack/security/SecurityInBasicRestTestCase.java index 7aa691f1c61b1..577d7a434134f 100644 --- a/x-pack/plugin/security/qa/security-basic/src/javaRestTest/java/org/elasticsearch/xpack/security/SecurityInBasicRestTestCase.java +++ b/x-pack/plugin/security/qa/security-basic/src/javaRestTest/java/org/elasticsearch/xpack/security/SecurityInBasicRestTestCase.java @@ -22,26 +22,18 @@ public abstract class SecurityInBasicRestTestCase extends ESRestTestCase { @Override protected Settings restAdminSettings() { String token = basicAuthHeaderValue("admin_user", new SecureString("admin-password".toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } @Override protected Settings restClientSettings() { String token = basicAuthHeaderValue("security_test_user", new SecureString("security-test-password".toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } private RestHighLevelClient getHighLevelAdminClient() { if (highLevelAdminClient == null) { - highLevelAdminClient = new RestHighLevelClient( - adminClient(), - ignore -> { - }, - List.of()) { + highLevelAdminClient = new RestHighLevelClient(adminClient(), ignore -> {}, List.of()) { }; } return highLevelAdminClient; diff --git a/x-pack/plugin/security/qa/security-basic/src/javaRestTest/java/org/elasticsearch/xpack/security/SecurityWithBasicLicenseIT.java b/x-pack/plugin/security/qa/security-basic/src/javaRestTest/java/org/elasticsearch/xpack/security/SecurityWithBasicLicenseIT.java index 1a7edeec41f04..85a69c85002d3 100644 --- a/x-pack/plugin/security/qa/security-basic/src/javaRestTest/java/org/elasticsearch/xpack/security/SecurityWithBasicLicenseIT.java +++ b/x-pack/plugin/security/qa/security-basic/src/javaRestTest/java/org/elasticsearch/xpack/security/SecurityWithBasicLicenseIT.java @@ -135,10 +135,12 @@ private void checkAuthentication() throws IOException { private void checkHasPrivileges() throws IOException { final Request request = new Request("GET", "/_security/user/_has_privileges"); - request.setJsonEntity("{" + - "\"cluster\": [ \"manage\", \"monitor\" ]," + - "\"index\": [{ \"names\": [ \"index_allowed\", \"index_denied\" ], \"privileges\": [ \"read\", \"all\" ] }]" + - "}"); + request.setJsonEntity( + "{" + + "\"cluster\": [ \"manage\", \"monitor\" ]," + + "\"index\": [{ \"names\": [ \"index_allowed\", \"index_denied\" ], \"privileges\": [ \"read\", \"all\" ] }]" + + "}" + ); Response response = client().performRequest(request); final Map auth = entityAsMap(response); assertThat(ObjectPath.evaluate(auth, "username"), equalTo("security_test_user")); @@ -168,19 +170,18 @@ private void checkIndexWrite() throws IOException { private Request buildGetTokenRequest() { final Request getToken = new Request("POST", "/_security/oauth2/token"); - getToken.setJsonEntity("{\"grant_type\" : \"password\",\n" + - " \"username\" : \"security_test_user\",\n" + - " \"password\" : \"security-test-password\"\n" + - "}"); + getToken.setJsonEntity( + "{\"grant_type\" : \"password\",\n" + + " \"username\" : \"security_test_user\",\n" + + " \"password\" : \"security-test-password\"\n" + + "}" + ); return getToken; } private Request buildGetApiKeyRequest() { final Request getApiKey = new Request("POST", "/_security/api_key"); - getApiKey.setJsonEntity("{\"name\" : \"my-api-key\",\n" + - " \"expiration\" : \"2d\",\n" + - " \"role_descriptors\" : {} \n" + - "}"); + getApiKey.setJsonEntity("{\"name\" : \"my-api-key\",\n" + " \"expiration\" : \"2d\",\n" + " \"role_descriptors\" : {} \n" + "}"); return getApiKey; } @@ -218,8 +219,10 @@ private void assertAuthenticateWithToken(String accessToken, boolean shouldSucce } else { ResponseException e = expectThrows(ResponseException.class, () -> client().performRequest(request)); assertThat(e.getResponse().getStatusLine().getStatusCode(), equalTo(401)); - assertThat(e.getMessage(), containsString( - "unable to authenticate with provided credentials and anonymous access is not allowed for this request")); + assertThat( + e.getMessage(), + containsString("unable to authenticate with provided credentials and anonymous access is not allowed for this request") + ); } } @@ -259,24 +262,26 @@ private void assertAuthenticateWithServiceAccountToken(String bearerString) thro private void assertAddRoleWithDLS(boolean shouldSucceed) throws IOException { final Request addRole = new Request("POST", "/_security/role/dlsrole"); - addRole.setJsonEntity("{\n" + - " \"cluster\": [\"all\"],\n" + - " \"indices\": [\n" + - " {\n" + - " \"names\": [ \"index1\", \"index2\" ],\n" + - " \"privileges\": [\"all\"],\n" + - " \"query\": \"{\\\"match\\\": {\\\"title\\\": \\\"foo\\\"}}\" \n" + - " },\n" + - " {\n" + - " \"names\": [ \"index41\", \"index42\" ],\n" + - " \"privileges\": [\"read\"]\n" + - " }\n" + - " ],\n" + - " \"run_as\": [ \"other_user\" ],\n" + - " \"metadata\" : { // optional\n" + - " \"version\" : 1\n" + - " }\n" + - "}"); + addRole.setJsonEntity( + "{\n" + + " \"cluster\": [\"all\"],\n" + + " \"indices\": [\n" + + " {\n" + + " \"names\": [ \"index1\", \"index2\" ],\n" + + " \"privileges\": [\"all\"],\n" + + " \"query\": \"{\\\"match\\\": {\\\"title\\\": \\\"foo\\\"}}\" \n" + + " },\n" + + " {\n" + + " \"names\": [ \"index41\", \"index42\" ],\n" + + " \"privileges\": [\"read\"]\n" + + " }\n" + + " ],\n" + + " \"run_as\": [ \"other_user\" ],\n" + + " \"metadata\" : { // optional\n" + + " \"version\" : 1\n" + + " }\n" + + "}" + ); if (shouldSucceed) { Response addRoleResponse = adminClient().performRequest(addRole); assertThat(addRoleResponse.getStatusLine().getStatusCode(), equalTo(200)); @@ -289,26 +294,28 @@ private void assertAddRoleWithDLS(boolean shouldSucceed) throws IOException { private void assertAddRoleWithFLS(boolean shouldSucceed) throws IOException { final Request addRole = new Request("POST", "/_security/role/flsrole"); - addRole.setJsonEntity("{\n" + - " \"cluster\": [\"all\"],\n" + - " \"indices\": [\n" + - " {\n" + - " \"names\": [ \"index1\", \"index2\" ],\n" + - " \"privileges\": [\"all\"],\n" + - " \"field_security\" : { // optional\n" + - " \"grant\" : [ \"title\", \"body\" ]\n" + - " }\n" + - " },\n" + - " {\n" + - " \"names\": [ \"index41\", \"index42\" ],\n" + - " \"privileges\": [\"read\"]\n" + - " }\n" + - " ],\n" + - " \"run_as\": [ \"other_user\" ],\n" + - " \"metadata\" : { // optional\n" + - " \"version\" : 1\n" + - " }\n" + - "}"); + addRole.setJsonEntity( + "{\n" + + " \"cluster\": [\"all\"],\n" + + " \"indices\": [\n" + + " {\n" + + " \"names\": [ \"index1\", \"index2\" ],\n" + + " \"privileges\": [\"all\"],\n" + + " \"field_security\" : { // optional\n" + + " \"grant\" : [ \"title\", \"body\" ]\n" + + " }\n" + + " },\n" + + " {\n" + + " \"names\": [ \"index41\", \"index42\" ],\n" + + " \"privileges\": [\"read\"]\n" + + " }\n" + + " ],\n" + + " \"run_as\": [ \"other_user\" ],\n" + + " \"metadata\" : { // optional\n" + + " \"version\" : 1\n" + + " }\n" + + "}" + ); if (shouldSucceed) { Response addRoleResponse = adminClient().performRequest(addRole); assertThat(addRoleResponse.getStatusLine().getStatusCode(), equalTo(200)); @@ -321,8 +328,9 @@ private void assertAddRoleWithFLS(boolean shouldSucceed) throws IOException { private void createUserWithDlsOrFlsRole() throws IOException { final Request request = new Request("PUT", "/_security/user/dls_fls_user"); - request.setJsonEntity("{\"password\":\"superstrongpassword\"," + - "\"roles\":[\"" + (randomBoolean() ? "dlsrole" : "flsrole") + "\"]}"); + request.setJsonEntity( + "{\"password\":\"superstrongpassword\"," + "\"roles\":[\"" + (randomBoolean() ? "dlsrole" : "flsrole") + "\"]}" + ); assertOK(adminClient().performRequest(request)); } @@ -333,27 +341,35 @@ private Tuple assertCreateApiKeyWithDlsFls() throws IOException final boolean keyRoleHasDlsFls = randomBoolean(); if (keyRoleHasDlsFls) { if (randomBoolean()) { - request.setJsonEntity("{\"name\":\"my-key\",\"role_descriptors\":" + - "{\"a\":{\"indices\":[" + - "{\"names\":[\"index41\"],\"privileges\":[\"read\"]," + - "\"query\":{\"term\":{\"tag\":{\"value\":\"prod\"}}}}," + - "{\"names\":[\"index1\",\"index2\",\"index42\"],\"privileges\":[\"read\"]}" + - "]}}}"); + request.setJsonEntity( + "{\"name\":\"my-key\",\"role_descriptors\":" + + "{\"a\":{\"indices\":[" + + "{\"names\":[\"index41\"],\"privileges\":[\"read\"]," + + "\"query\":{\"term\":{\"tag\":{\"value\":\"prod\"}}}}," + + "{\"names\":[\"index1\",\"index2\",\"index42\"],\"privileges\":[\"read\"]}" + + "]}}}" + ); } else { request.setJsonEntity( - "{\"name\":\"my-key\",\"role_descriptors\":" + - "{\"a\":{\"indices\":[" + - "{\"names\":[\"index41\"],\"privileges\":[\"read\"]," + - "\"field_security\":{\"grant\":[\"tag\"]}}," + - "{\"names\":[\"index1\",\"index2\",\"index42\"],\"privileges\":[\"read\"]}" + - "]}}}"); + "{\"name\":\"my-key\",\"role_descriptors\":" + + "{\"a\":{\"indices\":[" + + "{\"names\":[\"index41\"],\"privileges\":[\"read\"]," + + "\"field_security\":{\"grant\":[\"tag\"]}}," + + "{\"names\":[\"index1\",\"index2\",\"index42\"],\"privileges\":[\"read\"]}" + + "]}}}" + ); } } else { - request.setJsonEntity("{\"name\":\"my-key\",\"role_descriptors\":" + - "{\"a\":{\"indices\":[{\"names\":[\"index1\",\"index2\",\"index41\",\"index42\"],\"privileges\":[\"read\"]}]}}}"); + request.setJsonEntity( + "{\"name\":\"my-key\",\"role_descriptors\":" + + "{\"a\":{\"indices\":[{\"names\":[\"index1\",\"index2\",\"index41\",\"index42\"],\"privileges\":[\"read\"]}]}}}" + ); } - request.setOptions(request.getOptions().toBuilder().addHeader("Authorization", - basicAuthHeaderValue("dls_fls_user", new SecureString("superstrongpassword".toCharArray())))); + request.setOptions( + request.getOptions() + .toBuilder() + .addHeader("Authorization", basicAuthHeaderValue("dls_fls_user", new SecureString("superstrongpassword".toCharArray()))) + ); final Response response = client().performRequest(request); assertOK(response); diff --git a/x-pack/plugin/security/qa/security-disabled/src/javaRestTest/java/org/elasticsearch/xpack/security/SetSecurityUserProcessorWithSecurityDisabledIT.java b/x-pack/plugin/security/qa/security-disabled/src/javaRestTest/java/org/elasticsearch/xpack/security/SetSecurityUserProcessorWithSecurityDisabledIT.java index 4bdc64b4deded..5e11a01a04e25 100644 --- a/x-pack/plugin/security/qa/security-disabled/src/javaRestTest/java/org/elasticsearch/xpack/security/SetSecurityUserProcessorWithSecurityDisabledIT.java +++ b/x-pack/plugin/security/qa/security-disabled/src/javaRestTest/java/org/elasticsearch/xpack/security/SetSecurityUserProcessorWithSecurityDisabledIT.java @@ -26,12 +26,16 @@ public void testDefineAndUseProcessor() throws Exception { final String index = "index-" + getTestName(); { final Request putPipeline = new Request("PUT", "/_ingest/pipeline/" + pipeline); - putPipeline.setJsonEntity("{" + - " \"description\": \"Test pipeline (" + getTestName() + ")\"," + - " \"processors\":[{" + - " \"set_security_user\":{ \"field\": \"user\" }" + - " }]" + - "}"); + putPipeline.setJsonEntity( + "{" + + " \"description\": \"Test pipeline (" + + getTestName() + + ")\"," + + " \"processors\":[{" + + " \"set_security_user\":{ \"field\": \"user\" }" + + " }]" + + "}" + ); final Response response = client().performRequest(putPipeline); assertOK(response); } @@ -41,8 +45,10 @@ public void testDefineAndUseProcessor() throws Exception { ingest.setJsonEntity("{\"field\":\"value\"}"); final ResponseException ex = expectThrows(ResponseException.class, () -> client().performRequest(ingest)); final Response response = ex.getResponse(); - assertThat(EntityUtils.toString(response.getEntity()), - containsString("Security (authentication) is not enabled on this cluster")); + assertThat( + EntityUtils.toString(response.getEntity()), + containsString("Security (authentication) is not enabled on this cluster") + ); } } diff --git a/x-pack/plugin/security/qa/security-trial/src/javaRestTest/java/org/elasticsearch/xpack/security/SecurityOnTrialLicenseRestTestCase.java b/x-pack/plugin/security/qa/security-trial/src/javaRestTest/java/org/elasticsearch/xpack/security/SecurityOnTrialLicenseRestTestCase.java index f0b293ffb5aa3..710edf9fe8a95 100644 --- a/x-pack/plugin/security/qa/security-trial/src/javaRestTest/java/org/elasticsearch/xpack/security/SecurityOnTrialLicenseRestTestCase.java +++ b/x-pack/plugin/security/qa/security-trial/src/javaRestTest/java/org/elasticsearch/xpack/security/SecurityOnTrialLicenseRestTestCase.java @@ -22,10 +22,10 @@ import org.elasticsearch.client.security.support.ApiKey; import org.elasticsearch.client.security.user.User; import org.elasticsearch.client.security.user.privileges.Role; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.Tuple; import org.elasticsearch.test.rest.ESRestTestCase; import org.hamcrest.Matchers; @@ -40,23 +40,22 @@ public abstract class SecurityOnTrialLicenseRestTestCase extends ESRestTestCase @Override protected Settings restAdminSettings() { String token = basicAuthHeaderValue("admin_user", new SecureString("admin-password".toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } @Override protected Settings restClientSettings() { String token = basicAuthHeaderValue("security_test_user", new SecureString("security-test-password".toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } protected void createUser(String username, SecureString password, List roles) throws IOException { final RestHighLevelClient client = getHighLevelAdminClient(); - client.security().putUser(PutUserRequest.withPassword(new User(username, roles), password.getChars(), true, - RefreshPolicy.WAIT_UNTIL), RequestOptions.DEFAULT); + client.security() + .putUser( + PutUserRequest.withPassword(new User(username, roles), password.getChars(), true, RefreshPolicy.WAIT_UNTIL), + RequestOptions.DEFAULT + ); } protected void createRole(String name, Collection clusterPrivileges) throws IOException { @@ -99,11 +98,7 @@ protected ApiKey getApiKey(String id) throws IOException { private RestHighLevelClient getHighLevelAdminClient() { if (highLevelAdminClient == null) { - highLevelAdminClient = new RestHighLevelClient( - adminClient(), - ignore -> { - }, - List.of()) { + highLevelAdminClient = new RestHighLevelClient(adminClient(), ignore -> {}, List.of()) { }; } return highLevelAdminClient; diff --git a/x-pack/plugin/security/qa/security-trial/src/javaRestTest/java/org/elasticsearch/xpack/security/apikey/ApiKeyRestIT.java b/x-pack/plugin/security/qa/security-trial/src/javaRestTest/java/org/elasticsearch/xpack/security/apikey/ApiKeyRestIT.java index 3e6c185654ff2..93fd8a437c424 100644 --- a/x-pack/plugin/security/qa/security-trial/src/javaRestTest/java/org/elasticsearch/xpack/security/apikey/ApiKeyRestIT.java +++ b/x-pack/plugin/security/qa/security-trial/src/javaRestTest/java/org/elasticsearch/xpack/security/apikey/ApiKeyRestIT.java @@ -12,10 +12,10 @@ import org.elasticsearch.client.Response; import org.elasticsearch.client.ResponseException; import org.elasticsearch.client.security.support.ApiKey; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.settings.SecureString; -import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.core.Tuple; import org.elasticsearch.test.XContentTestUtils; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken; import org.elasticsearch.xpack.security.SecurityOnTrialLicenseRestTestCase; import org.junit.After; @@ -65,8 +65,10 @@ public void cleanUp() throws IOException { public void testGrantApiKeyForOtherUserWithPassword() throws IOException { Request request = new Request("POST", "_security/api_key/grant"); - request.setOptions(RequestOptions.DEFAULT.toBuilder().addHeader("Authorization", - UsernamePasswordToken.basicAuthHeaderValue(SYSTEM_USER, SYSTEM_USER_PASSWORD))); + request.setOptions( + RequestOptions.DEFAULT.toBuilder() + .addHeader("Authorization", UsernamePasswordToken.basicAuthHeaderValue(SYSTEM_USER, SYSTEM_USER_PASSWORD)) + ); final Map requestBody = Map.ofEntries( Map.entry("grant_type", "password"), Map.entry("username", END_USER), @@ -91,8 +93,10 @@ public void testGrantApiKeyForOtherUserWithAccessToken() throws IOException { final String accessToken = token.v1(); final Request request = new Request("POST", "_security/api_key/grant"); - request.setOptions(RequestOptions.DEFAULT.toBuilder().addHeader("Authorization", - UsernamePasswordToken.basicAuthHeaderValue(SYSTEM_USER, SYSTEM_USER_PASSWORD))); + request.setOptions( + RequestOptions.DEFAULT.toBuilder() + .addHeader("Authorization", UsernamePasswordToken.basicAuthHeaderValue(SYSTEM_USER, SYSTEM_USER_PASSWORD)) + ); final Map requestBody = Map.ofEntries( Map.entry("grant_type", "access_token"), Map.entry("access_token", accessToken), @@ -121,8 +125,10 @@ public void testGrantApiKeyForOtherUserWithAccessToken() throws IOException { public void testGrantApiKeyWithoutApiKeyNameWillFail() throws IOException { Request request = new Request("POST", "_security/api_key/grant"); - request.setOptions(RequestOptions.DEFAULT.toBuilder().addHeader("Authorization", - UsernamePasswordToken.basicAuthHeaderValue(SYSTEM_USER, SYSTEM_USER_PASSWORD))); + request.setOptions( + RequestOptions.DEFAULT.toBuilder() + .addHeader("Authorization", UsernamePasswordToken.basicAuthHeaderValue(SYSTEM_USER, SYSTEM_USER_PASSWORD)) + ); final Map requestBody = Map.ofEntries( Map.entry("grant_type", "password"), Map.entry("username", END_USER), @@ -130,8 +136,7 @@ public void testGrantApiKeyWithoutApiKeyNameWillFail() throws IOException { ); request.setJsonEntity(XContentTestUtils.convertToXContent(requestBody, XContentType.JSON).utf8ToString()); - final ResponseException e = - expectThrows(ResponseException.class, () -> client().performRequest(request)); + final ResponseException e = expectThrows(ResponseException.class, () -> client().performRequest(request)); assertEquals(400, e.getResponse().getStatusLine().getStatusCode()); assertThat(e.getMessage(), containsString("api key name is required")); diff --git a/x-pack/plugin/security/qa/security-trial/src/javaRestTest/java/org/elasticsearch/xpack/security/dlsfls/DlsRequestCacheIT.java b/x-pack/plugin/security/qa/security-trial/src/javaRestTest/java/org/elasticsearch/xpack/security/dlsfls/DlsRequestCacheIT.java index bd6435fc7d0a6..6a34f9029910d 100644 --- a/x-pack/plugin/security/qa/security-trial/src/javaRestTest/java/org/elasticsearch/xpack/security/dlsfls/DlsRequestCacheIT.java +++ b/x-pack/plugin/security/qa/security-trial/src/javaRestTest/java/org/elasticsearch/xpack/security/dlsfls/DlsRequestCacheIT.java @@ -49,9 +49,7 @@ public void cleanUp() throws IOException { @Override protected Settings restAdminSettings() { String token = basicAuthHeaderValue("x_pack_rest_user", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } public void testRequestCacheDisabledForDlsTemplateRoleWithPainless() throws IOException { @@ -59,14 +57,18 @@ public void testRequestCacheDisabledForDlsTemplateRoleWithPainless() throws IOEx final RestClient client = client(); final Request putScriptRequest = new Request("PUT", "_scripts/range-now"); - putScriptRequest.setJsonEntity("{\"script\":{\"lang\":\"painless\"," + - "\"source\":\"'{\\\"range\\\":{\\\"date\\\": {\\\"lte\\\": \\\"' + new Date().getTime() + '\\\"}}}' \"}}"); + putScriptRequest.setJsonEntity( + "{\"script\":{\"lang\":\"painless\"," + + "\"source\":\"'{\\\"range\\\":{\\\"date\\\": {\\\"lte\\\": \\\"' + new Date().getTime() + '\\\"}}}' \"}}" + ); assertOK(adminClient.performRequest(putScriptRequest)); // Create the index with a date field and 1 primary shard with no replica final Request putIndexRequest = new Request("PUT", DLS_TEMPLATE_PAINLESS_INDEX); - putIndexRequest.setJsonEntity("{\"mappings\":{\"properties\":{\"date\":{\"type\":\"date\",\"format\":\"epoch_millis\"}}}," + - "\"settings\":{\"number_of_shards\":1,\"number_of_replicas\":0}}"); + putIndexRequest.setJsonEntity( + "{\"mappings\":{\"properties\":{\"date\":{\"type\":\"date\",\"format\":\"epoch_millis\"}}}," + + "\"settings\":{\"number_of_shards\":1,\"number_of_replicas\":0}}" + ); assertOK(adminClient.performRequest(putIndexRequest)); // A doc in the past 1 min @@ -85,8 +87,10 @@ public void testRequestCacheDisabledForDlsTemplateRoleWithPainless() throws IOEx // First search should only get 1 doc in the past final Request searchRequest = new Request("GET", DLS_TEMPLATE_PAINLESS_INDEX + "/_search"); searchRequest.addParameter("request_cache", "true"); - searchRequest.setOptions(RequestOptions.DEFAULT.toBuilder().addHeader("Authorization", - UsernamePasswordToken.basicAuthHeaderValue(DLS_USER, DLS_USER_PASSWORD))); + searchRequest.setOptions( + RequestOptions.DEFAULT.toBuilder() + .addHeader("Authorization", UsernamePasswordToken.basicAuthHeaderValue(DLS_USER, DLS_USER_PASSWORD)) + ); assertSearchResponse(client.performRequest(searchRequest), Set.of("1")); // Cache should not be used since DLS query uses stored script assertCacheState(0, 0); diff --git a/x-pack/plugin/security/qa/security-trial/src/javaRestTest/java/org/elasticsearch/xpack/test/rest/CatIndicesWithSecurityIT.java b/x-pack/plugin/security/qa/security-trial/src/javaRestTest/java/org/elasticsearch/xpack/test/rest/CatIndicesWithSecurityIT.java index c26804c9d1122..62bc97366f7b3 100644 --- a/x-pack/plugin/security/qa/security-trial/src/javaRestTest/java/org/elasticsearch/xpack/test/rest/CatIndicesWithSecurityIT.java +++ b/x-pack/plugin/security/qa/security-trial/src/javaRestTest/java/org/elasticsearch/xpack/test/rest/CatIndicesWithSecurityIT.java @@ -24,17 +24,13 @@ public class CatIndicesWithSecurityIT extends ESRestTestCase { @Override protected Settings restAdminSettings() { String token = basicAuthHeaderValue("x_pack_rest_user", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } @Override protected Settings restClientSettings() { String token = basicAuthHeaderValue("cat_test_user", new SecureString("cat-test-password".toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } public void testHiddenIndexWithVisibleAlias() throws IOException { @@ -42,7 +38,8 @@ public void testHiddenIndexWithVisibleAlias() throws IOException { { final Request createRequest = new Request("PUT", ".index_hidden"); createRequest.setJsonEntity( - "{\"settings\": {\"index.hidden\": true, \"number_of_replicas\": 0}, \"aliases\": {\"index_allowed\": {}}}"); + "{\"settings\": {\"index.hidden\": true, \"number_of_replicas\": 0}, \"aliases\": {\"index_allowed\": {}}}" + ); final Response createResponse = adminClient().performRequest(createRequest); assertOK(createResponse); ensureGreen("index_allowed"); @@ -63,8 +60,10 @@ public void testHiddenIndexWithHiddenAlias() throws IOException { // Create the index and alias { final Request createRequest = new Request("PUT", ".index_hidden"); - createRequest.setJsonEntity("{\"settings\": {\"index.hidden\": true, \"number_of_replicas\": 0}, " - + "\"aliases\": {\"index_allowed\": {\"is_hidden\": true}}}"); + createRequest.setJsonEntity( + "{\"settings\": {\"index.hidden\": true, \"number_of_replicas\": 0}, " + + "\"aliases\": {\"index_allowed\": {\"is_hidden\": true}}}" + ); final Response createResponse = adminClient().performRequest(createRequest); assertOK(createResponse); ensureGreen("index_allowed"); @@ -95,8 +94,9 @@ public void testVisibleIndexWithHiddenAlias() throws IOException { // Create the index and alias { final Request createRequest = new Request("PUT", "visible_index"); - createRequest.setJsonEntity("{\"settings\": {\"number_of_replicas\": 0}, " - + "\"aliases\": {\"index_allowed\": {\"is_hidden\": true}}}"); + createRequest.setJsonEntity( + "{\"settings\": {\"number_of_replicas\": 0}, " + "\"aliases\": {\"index_allowed\": {\"is_hidden\": true}}}" + ); final Response createResponse = adminClient().performRequest(createRequest); assertOK(createResponse); ensureGreen("index_allowed"); diff --git a/x-pack/plugin/security/qa/service-account/src/javaRestTest/java/org/elasticsearch/xpack/security/authc/service/ServiceAccountIT.java b/x-pack/plugin/security/qa/service-account/src/javaRestTest/java/org/elasticsearch/xpack/security/authc/service/ServiceAccountIT.java index fecfc25580670..825f5346553cc 100644 --- a/x-pack/plugin/security/qa/service-account/src/javaRestTest/java/org/elasticsearch/xpack/security/authc/service/ServiceAccountIT.java +++ b/x-pack/plugin/security/qa/service-account/src/javaRestTest/java/org/elasticsearch/xpack/security/authc/service/ServiceAccountIT.java @@ -14,15 +14,15 @@ import org.elasticsearch.client.ResponseException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesArray; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.core.PathUtils; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.core.PathUtils; import org.elasticsearch.test.rest.ESRestTestCase; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.security.authz.store.ReservedRolesStore; import org.elasticsearch.xpack.core.security.user.KibanaSystemUser; import org.junit.BeforeClass; @@ -150,37 +150,30 @@ protected String getProtocol() { @Override protected Settings restAdminSettings() { final String token = basicAuthHeaderValue("test_admin", new SecureString("x-pack-test-password".toCharArray())); - return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token) - .put(CERTIFICATE_AUTHORITIES, caPath) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).put(CERTIFICATE_AUTHORITIES, caPath).build(); } @Override protected Settings restClientSettings() { final String token = basicAuthHeaderValue("service_account_manager", new SecureString("x-pack-test-password".toCharArray())); - return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token) - .put(CERTIFICATE_AUTHORITIES, caPath) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).put(CERTIFICATE_AUTHORITIES, caPath).build(); } public void testGetServiceAccount() throws IOException { final Request getServiceAccountRequest1 = new Request("GET", "_security/service"); final Response getServiceAccountResponse1 = client().performRequest(getServiceAccountRequest1); assertOK(getServiceAccountResponse1); - assertServiceAccountRoleDescriptor(getServiceAccountResponse1, - "elastic/fleet-server", ELASTIC_FLEET_SERVER_ROLE_DESCRIPTOR); + assertServiceAccountRoleDescriptor(getServiceAccountResponse1, "elastic/fleet-server", ELASTIC_FLEET_SERVER_ROLE_DESCRIPTOR); final Request getServiceAccountRequest2 = new Request("GET", "_security/service/elastic"); final Response getServiceAccountResponse2 = client().performRequest(getServiceAccountRequest2); assertOK(getServiceAccountResponse2); - assertServiceAccountRoleDescriptor(getServiceAccountResponse2, - "elastic/fleet-server", ELASTIC_FLEET_SERVER_ROLE_DESCRIPTOR); + assertServiceAccountRoleDescriptor(getServiceAccountResponse2, "elastic/fleet-server", ELASTIC_FLEET_SERVER_ROLE_DESCRIPTOR); final Request getServiceAccountRequest3 = new Request("GET", "_security/service/elastic/fleet-server"); final Response getServiceAccountResponse3 = client().performRequest(getServiceAccountRequest3); assertOK(getServiceAccountResponse3); - assertServiceAccountRoleDescriptor(getServiceAccountResponse3, - "elastic/fleet-server", ELASTIC_FLEET_SERVER_ROLE_DESCRIPTOR); + assertServiceAccountRoleDescriptor(getServiceAccountResponse3, "elastic/fleet-server", ELASTIC_FLEET_SERVER_ROLE_DESCRIPTOR); final Request getServiceAccountRequestKibana = new Request("GET", "_security/service/elastic/kibana"); final Response getServiceAccountResponseKibana = client().performRequest(getServiceAccountRequestKibana); @@ -206,10 +199,16 @@ public void testAuthenticate() throws IOException { request.setOptions(RequestOptions.DEFAULT.toBuilder().addHeader("Authorization", "Bearer " + VALID_SERVICE_TOKEN)); final Response response = client().performRequest(request); assertOK(response); - assertThat(responseAsMap(response), - equalTo(XContentHelper.convertToMap( - new BytesArray(String.format(Locale.ROOT, AUTHENTICATE_RESPONSE, "token1", "file")), - false, XContentType.JSON).v2())); + assertThat( + responseAsMap(response), + equalTo( + XContentHelper.convertToMap( + new BytesArray(String.format(Locale.ROOT, AUTHENTICATE_RESPONSE, "token1", "file")), + false, + XContentType.JSON + ).v2() + ) + ); } public void testAuthenticateShouldNotFallThroughInCaseOfFailure() throws IOException { @@ -224,8 +223,10 @@ public void testAuthenticateShouldNotFallThroughInCaseOfFailure() throws IOExcep final ResponseException e = expectThrows(ResponseException.class, () -> client().performRequest(request)); assertThat(e.getResponse().getStatusLine().getStatusCode(), equalTo(401)); if (securityIndexExists) { - assertThat(e.getMessage(), containsString( - "failed to authenticate service account [elastic/fleet-server] with token name [token1]")); + assertThat( + e.getMessage(), + containsString("failed to authenticate service account [elastic/fleet-server] with token name [token1]") + ); } else { assertThat(e.getMessage(), containsString("no such index [.security]")); } @@ -256,9 +257,13 @@ public void testAuthenticateShouldWorkWithOAuthBearerToken() throws IOException public void testAuthenticateShouldDifferentiateBetweenNormalUserAndServiceAccount() throws IOException { final Request request = new Request("GET", "_security/_authenticate"); - request.setOptions(RequestOptions.DEFAULT.toBuilder().addHeader( - "Authorization", basicAuthHeaderValue("elastic/fleet-server", new SecureString("x-pack-test-password".toCharArray())) - )); + request.setOptions( + RequestOptions.DEFAULT.toBuilder() + .addHeader( + "Authorization", + basicAuthHeaderValue("elastic/fleet-server", new SecureString("x-pack-test-password".toCharArray())) + ) + ); final Response response = client().performRequest(request); assertOK(response); final Map responseMap = responseAsMap(response); @@ -284,10 +289,16 @@ public void testCreateApiServiceAccountTokenAndAuthenticateWithIt() throws IOExc request.setOptions(RequestOptions.DEFAULT.toBuilder().addHeader("Authorization", "Bearer " + tokenMap.get("value"))); final Response response = client().performRequest(request); assertOK(response); - assertThat(responseAsMap(response), - equalTo(XContentHelper.convertToMap( - new BytesArray(String.format(Locale.ROOT, AUTHENTICATE_RESPONSE, "api-token-1", "index")), - false, XContentType.JSON).v2())); + assertThat( + responseAsMap(response), + equalTo( + XContentHelper.convertToMap( + new BytesArray(String.format(Locale.ROOT, AUTHENTICATE_RESPONSE, "api-token-1", "index")), + false, + XContentType.JSON + ).v2() + ) + ); } public void testFileTokenAndApiTokenCanShareTheSameNameAndBothWorks() throws IOException { @@ -316,8 +327,7 @@ public void testNoDuplicateApiServiceAccountToken() throws IOException { final Response createTokenResponse = client().performRequest(createTokenRequest); assertOK(createTokenResponse); - final ResponseException e = - expectThrows(ResponseException.class, () -> client().performRequest(createTokenRequest)); + final ResponseException e = expectThrows(ResponseException.class, () -> client().performRequest(createTokenRequest)); assertThat(e.getResponse().getStatusLine().getStatusCode(), equalTo(409)); assertThat(e.getMessage(), containsString("document already exists")); } @@ -345,10 +355,7 @@ public void testGetServiceAccountCredentials() throws IOException { final Map getTokensResponseMap2 = responseAsMap(getTokensResponse2); assertThat(getTokensResponseMap2.get("service_account"), equalTo("elastic/fleet-server")); assertThat(getTokensResponseMap2.get("count"), equalTo(3)); - assertThat(getTokensResponseMap2.get("tokens"), equalTo(Map.of( - "api-token-1", Map.of(), - "api-token-2", Map.of() - ))); + assertThat(getTokensResponseMap2.get("tokens"), equalTo(Map.of("api-token-1", Map.of(), "api-token-2", Map.of()))); assertNodesCredentials(getTokensResponseMap2); final Request deleteTokenRequest1 = new Request("DELETE", "_security/service/elastic/fleet-server/credential/token/api-token-2"); @@ -361,9 +368,7 @@ public void testGetServiceAccountCredentials() throws IOException { final Map getTokensResponseMap3 = responseAsMap(getTokensResponse3); assertThat(getTokensResponseMap3.get("service_account"), equalTo("elastic/fleet-server")); assertThat(getTokensResponseMap3.get("count"), equalTo(2)); - assertThat(getTokensResponseMap3.get("tokens"), equalTo(Map.of( - "api-token-1", Map.of() - ))); + assertThat(getTokensResponseMap3.get("tokens"), equalTo(Map.of("api-token-1", Map.of()))); assertNodesCredentials(getTokensResponseMap3); final Request deleteTokenRequest2 = new Request("DELETE", "_security/service/elastic/fleet-server/credential/token/non-such-thing"); @@ -373,8 +378,12 @@ public void testGetServiceAccountCredentials() throws IOException { } public void testClearCache() throws IOException { - final Request clearCacheRequest = new Request("POST", "_security/service/elastic/fleet-server/credential/token/" - + randomFrom("", "*", "api-token-1", "api-token-1,api-token2") + "/_clear_cache"); + final Request clearCacheRequest = new Request( + "POST", + "_security/service/elastic/fleet-server/credential/token/" + + randomFrom("", "*", "api-token-1", "api-token-1,api-token2") + + "/_clear_cache" + ); final Response clearCacheResponse = adminClient().performRequest(clearCacheRequest); assertOK(clearCacheResponse); final Map clearCacheResponseMap = responseAsMap(clearCacheResponse); @@ -414,21 +423,17 @@ public void testManageOwnApiKey() throws IOException { assertApiKeys(apiKeyId1, "key-1", false, requestOptions); - final String base64ApiKeyKeyValue = Base64.getEncoder().encodeToString( - (apiKeyId1 + ":" + createApiKeyResponseMap1.get("api_key")).getBytes(StandardCharsets.UTF_8)); + final String base64ApiKeyKeyValue = Base64.getEncoder() + .encodeToString((apiKeyId1 + ":" + createApiKeyResponseMap1.get("api_key")).getBytes(StandardCharsets.UTF_8)); // API key can monitor cluster final Request mainRequest = new Request("GET", "/"); - mainRequest.setOptions(mainRequest.getOptions().toBuilder().addHeader( - "Authorization", "ApiKey " + base64ApiKeyKeyValue - )); + mainRequest.setOptions(mainRequest.getOptions().toBuilder().addHeader("Authorization", "ApiKey " + base64ApiKeyKeyValue)); assertOK(client().performRequest(mainRequest)); // API key cannot get user final Request getUserRequest = new Request("GET", "_security/user"); - getUserRequest.setOptions(getUserRequest.getOptions().toBuilder().addHeader( - "Authorization", "ApiKey " + base64ApiKeyKeyValue - )); + getUserRequest.setOptions(getUserRequest.getOptions().toBuilder().addHeader("Authorization", "ApiKey " + base64ApiKeyKeyValue)); final ResponseException e = expectThrows(ResponseException.class, () -> client().performRequest(getUserRequest)); assertThat(e.getResponse().getStatusLine().getStatusCode(), equalTo(403)); assertThat(e.getMessage(), containsString("is unauthorized for API key")); @@ -444,8 +449,8 @@ public void testManageOwnApiKey() throws IOException { assertApiKeys(apiKeyId1, "key-1", true, requestOptions); } - private void assertApiKeys(String apiKeyId, String name, boolean invalidated, - RequestOptions.Builder requestOptions) throws IOException { + private void assertApiKeys(String apiKeyId, String name, boolean invalidated, RequestOptions.Builder requestOptions) + throws IOException { final Request getApiKeysRequest = new Request("GET", "_security/api_key?owner=true"); getApiKeysRequest.setOptions(requestOptions); final Response getApiKeysResponse = client().performRequest(getApiKeysRequest); @@ -463,12 +468,16 @@ private void assertApiKeys(String apiKeyId, String name, boolean invalidated, assertThat(apiKey.get("invalidated"), is(invalidated)); } - private void assertServiceAccountRoleDescriptor(Response response, - String serviceAccountPrincipal, - String roleDescriptorString) throws IOException { + private void assertServiceAccountRoleDescriptor(Response response, String serviceAccountPrincipal, String roleDescriptorString) + throws IOException { final Map responseMap = responseAsMap(response); - assertThat(responseMap, hasEntry(serviceAccountPrincipal, Map.of("role_descriptor", - XContentHelper.convertToMap(new BytesArray(roleDescriptorString), false, XContentType.JSON).v2()))); + assertThat( + responseMap, + hasEntry( + serviceAccountPrincipal, + Map.of("role_descriptor", XContentHelper.convertToMap(new BytesArray(roleDescriptorString), false, XContentType.JSON).v2()) + ) + ); } @SuppressWarnings("unchecked") diff --git a/x-pack/plugin/security/qa/smoke-test-all-realms/src/javaRestTest/java/org/elasticsearch/xpack/security/authc/FileRealmAuthIT.java b/x-pack/plugin/security/qa/smoke-test-all-realms/src/javaRestTest/java/org/elasticsearch/xpack/security/authc/FileRealmAuthIT.java index b610f9db2064f..7e9fe63f3a38b 100644 --- a/x-pack/plugin/security/qa/smoke-test-all-realms/src/javaRestTest/java/org/elasticsearch/xpack/security/authc/FileRealmAuthIT.java +++ b/x-pack/plugin/security/qa/smoke-test-all-realms/src/javaRestTest/java/org/elasticsearch/xpack/security/authc/FileRealmAuthIT.java @@ -26,8 +26,7 @@ public class FileRealmAuthIT extends SecurityRealmSmokeTestCase { public void testAuthenticationUsingFileRealm() throws IOException { Map authenticate = super.authenticate( - RequestOptions.DEFAULT.toBuilder().addHeader("Authorization", - UsernamePasswordToken.basicAuthHeaderValue(USERNAME, PASSWORD)) + RequestOptions.DEFAULT.toBuilder().addHeader("Authorization", UsernamePasswordToken.basicAuthHeaderValue(USERNAME, PASSWORD)) ); assertUsername(authenticate, USERNAME); diff --git a/x-pack/plugin/security/qa/smoke-test-all-realms/src/javaRestTest/java/org/elasticsearch/xpack/security/authc/NativeRealmAuthIT.java b/x-pack/plugin/security/qa/smoke-test-all-realms/src/javaRestTest/java/org/elasticsearch/xpack/security/authc/NativeRealmAuthIT.java index e849332bd791b..3eed0e1abb4e8 100644 --- a/x-pack/plugin/security/qa/smoke-test-all-realms/src/javaRestTest/java/org/elasticsearch/xpack/security/authc/NativeRealmAuthIT.java +++ b/x-pack/plugin/security/qa/smoke-test-all-realms/src/javaRestTest/java/org/elasticsearch/xpack/security/authc/NativeRealmAuthIT.java @@ -41,8 +41,7 @@ public void cleanUp() throws IOException { public void testAuthenticationUsingNativeRealm() throws IOException { Map authenticate = super.authenticate( - RequestOptions.DEFAULT.toBuilder().addHeader("Authorization", - UsernamePasswordToken.basicAuthHeaderValue(USERNAME, PASSWORD)) + RequestOptions.DEFAULT.toBuilder().addHeader("Authorization", UsernamePasswordToken.basicAuthHeaderValue(USERNAME, PASSWORD)) ); assertUsername(authenticate, USERNAME); diff --git a/x-pack/plugin/security/qa/smoke-test-all-realms/src/javaRestTest/java/org/elasticsearch/xpack/security/authc/RealmInfoIT.java b/x-pack/plugin/security/qa/smoke-test-all-realms/src/javaRestTest/java/org/elasticsearch/xpack/security/authc/RealmInfoIT.java index 9a66236a72cc3..4e9974fe309fb 100644 --- a/x-pack/plugin/security/qa/smoke-test-all-realms/src/javaRestTest/java/org/elasticsearch/xpack/security/authc/RealmInfoIT.java +++ b/x-pack/plugin/security/qa/smoke-test-all-realms/src/javaRestTest/java/org/elasticsearch/xpack/security/authc/RealmInfoIT.java @@ -28,8 +28,7 @@ public void testThatAllRealmTypesAreEnabled() throws IOException { Map realms = ObjectPath.evaluate(usage, "security.realms"); realms.forEach((type, config) -> { assertThat(config, Matchers.instanceOf(Map.class)); - assertThat("Realm type [" + type + "] is not enabled", - ((Map) config).get("enabled"), Matchers.equalTo(true)); + assertThat("Realm type [" + type + "] is not enabled", ((Map) config).get("enabled"), Matchers.equalTo(true)); }); } diff --git a/x-pack/plugin/security/qa/smoke-test-all-realms/src/javaRestTest/java/org/elasticsearch/xpack/security/authc/SecurityRealmSmokeTestCase.java b/x-pack/plugin/security/qa/smoke-test-all-realms/src/javaRestTest/java/org/elasticsearch/xpack/security/authc/SecurityRealmSmokeTestCase.java index 93c477b79352f..c4a847e674d23 100644 --- a/x-pack/plugin/security/qa/smoke-test-all-realms/src/javaRestTest/java/org/elasticsearch/xpack/security/authc/SecurityRealmSmokeTestCase.java +++ b/x-pack/plugin/security/qa/smoke-test-all-realms/src/javaRestTest/java/org/elasticsearch/xpack/security/authc/SecurityRealmSmokeTestCase.java @@ -20,10 +20,10 @@ import org.elasticsearch.client.security.user.User; import org.elasticsearch.client.security.user.privileges.Role; import org.elasticsearch.common.Strings; -import org.elasticsearch.core.PathUtils; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.PathUtils; import org.elasticsearch.test.rest.ESRestTestCase; import org.junit.BeforeClass; @@ -58,19 +58,13 @@ public static void findHttpCertificateAuthority() throws Exception { @Override protected Settings restAdminSettings() { String token = basicAuthHeaderValue("admin_user", new SecureString("admin-password".toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .put(CERTIFICATE_AUTHORITIES, httpCAPath) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).put(CERTIFICATE_AUTHORITIES, httpCAPath).build(); } @Override protected Settings restClientSettings() { String token = basicAuthHeaderValue("security_test_user", new SecureString("security-test-password".toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .put(CERTIFICATE_AUTHORITIES, httpCAPath) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).put(CERTIFICATE_AUTHORITIES, httpCAPath).build(); } @Override @@ -100,7 +94,8 @@ protected void assertRealm(Map authenticateResponse, String real protected void assertRoles(Map authenticateResponse, String... roles) { assertThat(authenticateResponse, hasEntry(equalTo("roles"), instanceOf(List.class))); String[] roleJson = ((List) authenticateResponse.get("roles")).toArray(String[]::new); - assertThat("Server returned unexpected roles list [" + Strings.arrayToCommaDelimitedString(roleJson) + "]", + assertThat( + "Server returned unexpected roles list [" + Strings.arrayToCommaDelimitedString(roleJson) + "]", roleJson, arrayContainingInAnyOrder(roles) ); @@ -108,15 +103,17 @@ protected void assertRoles(Map authenticateResponse, String... r protected void createUser(String username, SecureString password, List roles) throws IOException { final RestHighLevelClient client = getHighLevelAdminClient(); - client.security().putUser( - PutUserRequest.withPassword(new User(username, roles), password.getChars(), true, RefreshPolicy.WAIT_UNTIL), - RequestOptions.DEFAULT); + client.security() + .putUser( + PutUserRequest.withPassword(new User(username, roles), password.getChars(), true, RefreshPolicy.WAIT_UNTIL), + RequestOptions.DEFAULT + ); } protected void changePassword(String username, SecureString password) throws IOException { final RestHighLevelClient client = getHighLevelAdminClient(); - client.security().changePassword(new ChangePasswordRequest(username, password.getChars(), RefreshPolicy.WAIT_UNTIL), - RequestOptions.DEFAULT); + client.security() + .changePassword(new ChangePasswordRequest(username, password.getChars(), RefreshPolicy.WAIT_UNTIL), RequestOptions.DEFAULT); } protected void createRole(String name, Collection clusterPrivileges) throws IOException { @@ -137,11 +134,7 @@ protected void deleteRole(String name) throws IOException { private RestHighLevelClient getHighLevelAdminClient() { if (highLevelAdminClient == null) { - highLevelAdminClient = new RestHighLevelClient( - adminClient(), - ignore -> { - }, - List.of()) { + highLevelAdminClient = new RestHighLevelClient(adminClient(), ignore -> {}, List.of()) { }; } return highLevelAdminClient; diff --git a/x-pack/plugin/security/qa/tls-basic/src/javaRestTest/java/org/elasticsearch/xpack/security/TlsWithBasicLicenseIT.java b/x-pack/plugin/security/qa/tls-basic/src/javaRestTest/java/org/elasticsearch/xpack/security/TlsWithBasicLicenseIT.java index c555f4fb81d21..16f7b33066401 100644 --- a/x-pack/plugin/security/qa/tls-basic/src/javaRestTest/java/org/elasticsearch/xpack/security/TlsWithBasicLicenseIT.java +++ b/x-pack/plugin/security/qa/tls-basic/src/javaRestTest/java/org/elasticsearch/xpack/security/TlsWithBasicLicenseIT.java @@ -9,10 +9,10 @@ import org.elasticsearch.client.Request; import org.elasticsearch.client.Response; import org.elasticsearch.client.ResponseException; -import org.elasticsearch.core.PathUtils; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.PathUtils; import org.elasticsearch.test.rest.ESRestTestCase; import org.elasticsearch.test.rest.yaml.ObjectPath; import org.junit.AfterClass; @@ -131,6 +131,4 @@ private void checkCertificateAPI() throws IOException { assertThat(paths, containsInAnyOrder("http.crt", "transport.crt", "ca.crt")); } - } - diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/AbstractPrivilegeTestCase.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/AbstractPrivilegeTestCase.java index 9a0ea4a23e633..bf534699c78a4 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/AbstractPrivilegeTestCase.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/AbstractPrivilegeTestCase.java @@ -34,9 +34,15 @@ protected Response assertAccessIsAllowed(String user, Request request) throws IO setUser(request, user); Response response = getRestClient().performRequest(request); StatusLine statusLine = response.getStatusLine(); - String message = String.format(Locale.ROOT, "%s %s: Expected no error got %s %s with body %s", - request.getMethod(), request.getEndpoint(), statusLine.getStatusCode(), - statusLine.getReasonPhrase(), EntityUtils.toString(response.getEntity())); + String message = String.format( + Locale.ROOT, + "%s %s: Expected no error got %s %s with body %s", + request.getMethod(), + request.getEndpoint(), + statusLine.getStatusCode(), + statusLine.getReasonPhrase(), + EntityUtils.toString(response.getEntity()) + ); assertThat(message, statusLine.getStatusCode(), is(not(greaterThanOrEqualTo(400)))); return response; } @@ -56,10 +62,16 @@ protected void assertAccessIsDenied(String user, Request request) throws IOExcep ResponseException responseException = expectThrows(ResponseException.class, () -> getRestClient().performRequest(request)); StatusLine statusLine = responseException.getResponse().getStatusLine(); String requestBody = request.getEntity() == null ? "" : "with body " + EntityUtils.toString(request.getEntity()); - String message = String.format(Locale.ROOT, "%s %s body %s: Expected 403, got %s %s with body %s", - request.getMethod(), request.getEndpoint(), requestBody, - statusLine.getStatusCode(), statusLine.getReasonPhrase(), - EntityUtils.toString(responseException.getResponse().getEntity())); + String message = String.format( + Locale.ROOT, + "%s %s body %s: Expected 403, got %s %s with body %s", + request.getMethod(), + request.getEndpoint(), + requestBody, + statusLine.getStatusCode(), + statusLine.getReasonPhrase(), + EntityUtils.toString(responseException.getResponse().getEntity()) + ); assertThat(message, statusLine.getStatusCode(), is(403)); } @@ -95,8 +107,10 @@ protected void assertBodyHasAccessIsDenied(String user, String method, String ur private void setUser(Request request, String user) { RequestOptions.Builder options = RequestOptions.DEFAULT.toBuilder(); - options.addHeader("Authorization", - UsernamePasswordToken.basicAuthHeaderValue(user, SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)); + options.addHeader( + "Authorization", + UsernamePasswordToken.basicAuthHeaderValue(user, SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING) + ); request.setOptions(options); } } diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/BulkUpdateTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/BulkUpdateTests.java index 0c315164b3631..6bc08fd6603ea 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/BulkUpdateTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/BulkUpdateTests.java @@ -14,10 +14,10 @@ import org.elasticsearch.client.RequestOptions; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.test.SecurityIntegTestCase; import org.elasticsearch.test.SecuritySettingsSource; import org.elasticsearch.test.SecuritySettingsSourceField; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken; @@ -35,14 +35,16 @@ protected boolean addMockHttpTransport() { @Override public Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { return Settings.builder() - .put(super.nodeSettings(nodeOrdinal, otherSettings)) - .put(XPackSettings.DLS_FLS_ENABLED.getKey(), randomBoolean()) - .build(); + .put(super.nodeSettings(nodeOrdinal, otherSettings)) + .put(XPackSettings.DLS_FLS_ENABLED.getKey(), randomBoolean()) + .build(); } public void testThatBulkUpdateDoesNotLoseFields() { - assertEquals(DocWriteResponse.Result.CREATED, - client().prepareIndex("index1").setSource("{\"test\": \"test\"}", XContentType.JSON).setId("1").get().getResult()); + assertEquals( + DocWriteResponse.Result.CREATED, + client().prepareIndex("index1").setSource("{\"test\": \"test\"}", XContentType.JSON).setId("1").get().getResult() + ); GetResponse getResponse = client().prepareGet("index1", "1").get(); assertEquals("test", getResponse.getSource().get("test")); @@ -51,8 +53,10 @@ public void testThatBulkUpdateDoesNotLoseFields() { } // update with a new field - assertEquals(DocWriteResponse.Result.UPDATED, client().prepareUpdate("index1", "1") - .setDoc("{\"not test\": \"not test\"}", XContentType.JSON).get().getResult()); + assertEquals( + DocWriteResponse.Result.UPDATED, + client().prepareUpdate("index1", "1").setDoc("{\"not test\": \"not test\"}", XContentType.JSON).get().getResult() + ); getResponse = client().prepareGet("index1", "1").get(); assertEquals("test", getResponse.getSource().get("test")); assertEquals("not test", getResponse.getSource().get("not test")); @@ -62,8 +66,9 @@ public void testThatBulkUpdateDoesNotLoseFields() { flushAndRefresh(); // do it in a bulk - BulkResponse response = client().prepareBulk().add(client().prepareUpdate("index1", "1") - .setDoc("{\"bulk updated\": \"bulk updated\"}", XContentType.JSON)).get(); + BulkResponse response = client().prepareBulk() + .add(client().prepareUpdate("index1", "1").setDoc("{\"bulk updated\": \"bulk updated\"}", XContentType.JSON)) + .get(); assertEquals(DocWriteResponse.Result.UPDATED, response.getItems()[0].getResponse().getResult()); getResponse = client().prepareGet("index1", "1").get(); assertEquals("test", getResponse.getSource().get("test")); @@ -74,8 +79,13 @@ public void testThatBulkUpdateDoesNotLoseFields() { public void testThatBulkUpdateDoesNotLoseFieldsHttp() throws IOException { final String path = "/index1/_doc/1"; final RequestOptions.Builder optionsBuilder = RequestOptions.DEFAULT.toBuilder(); - optionsBuilder.addHeader("Authorization", UsernamePasswordToken.basicAuthHeaderValue(SecuritySettingsSource.TEST_USER_NAME, - new SecureString(SecuritySettingsSourceField.TEST_PASSWORD.toCharArray()))); + optionsBuilder.addHeader( + "Authorization", + UsernamePasswordToken.basicAuthHeaderValue( + SecuritySettingsSource.TEST_USER_NAME, + new SecureString(SecuritySettingsSourceField.TEST_PASSWORD.toCharArray()) + ) + ); final RequestOptions options = optionsBuilder.build(); Request createRequest = new Request("PUT", path); @@ -91,7 +101,7 @@ public void testThatBulkUpdateDoesNotLoseFieldsHttp() throws IOException { flushAndRefresh(); } - //update with new field + // update with new field Request updateRequest = new Request("POST", "/index1/_update/1"); updateRequest.setOptions(options); updateRequest.setJsonEntity("{\"doc\": {\"not test\": \"not test\"}}"); @@ -108,8 +118,8 @@ public void testThatBulkUpdateDoesNotLoseFieldsHttp() throws IOException { Request bulkRequest = new Request("POST", "/_bulk"); bulkRequest.setOptions(options); bulkRequest.setJsonEntity( - "{\"update\": {\"_index\": \"index1\", \"_id\": \"1\"}}\n" + - "{\"doc\": {\"bulk updated\":\"bulk updated\"}}\n"); + "{\"update\": {\"_index\": \"index1\", \"_id\": \"1\"}}\n" + "{\"doc\": {\"bulk updated\":\"bulk updated\"}}\n" + ); getRestClient().performRequest(bulkRequest); String afterBulk = EntityUtils.toString(getRestClient().performRequest(getRequest).getEntity()); diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/ClearRealmsCacheTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/ClearRealmsCacheTests.java index 43d350422750a..88688bfbf3fd3 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/ClearRealmsCacheTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/ClearRealmsCacheTests.java @@ -100,8 +100,10 @@ public void assertEviction(User prevUser, User newUser) { @Override public void executeRequest() throws Exception { - executeHttpRequest("/_security/realm/" + (randomBoolean() ? "*" : "_all") + "/_clear_cache", - Collections.emptyMap()); + executeHttpRequest( + "/_security/realm/" + (randomBoolean() ? "*" : "_all") + "/_clear_cache", + Collections.emptyMap() + ); } }, @@ -165,8 +167,13 @@ static void executeHttpRequest(String path, Map params) throws E request.addParameter(param.getKey(), param.getValue()); } RequestOptions.Builder options = request.getOptions().toBuilder(); - options.addHeader("Authorization", UsernamePasswordToken.basicAuthHeaderValue(SecuritySettingsSource.TEST_USER_NAME, - new SecureString(SecuritySettingsSourceField.TEST_PASSWORD.toCharArray()))); + options.addHeader( + "Authorization", + UsernamePasswordToken.basicAuthHeaderValue( + SecuritySettingsSource.TEST_USER_NAME, + new SecureString(SecuritySettingsSourceField.TEST_PASSWORD.toCharArray()) + ) + ); request.setOptions(options); Response response = getRestClient().performRequest(request); assertNotNull(response.getEntity()); @@ -181,16 +188,15 @@ protected boolean addMockHttpTransport() { @Override protected String configRoles() { - return SecuritySettingsSource.CONFIG_ROLE_ALLOW_ALL + "\n" + - "r1:\n" + - " cluster: all\n"; + return SecuritySettingsSource.CONFIG_ROLE_ALLOW_ALL + "\n" + "r1:\n" + " cluster: all\n"; } @Override protected String configUsers() { StringBuilder builder = new StringBuilder(SecuritySettingsSource.CONFIG_STANDARD_USER); - final String usersPasswdHashed = - new String(getFastStoredHashAlgoForTests().hash(SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)); + final String usersPasswdHashed = new String( + getFastStoredHashAlgoForTests().hash(SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING) + ); for (String username : usernames) { builder.append(username).append(":").append(usersPasswdHashed).append("\n"); } @@ -199,8 +205,7 @@ protected String configUsers() { @Override protected String configUsersRoles() { - return SecuritySettingsSource.CONFIG_STANDARD_USER_ROLES + - "r1:" + Strings.arrayToCommaDelimitedString(usernames); + return SecuritySettingsSource.CONFIG_STANDARD_USER_ROLES + "r1:" + Strings.arrayToCommaDelimitedString(usernames); } public void testEvictAll() throws Exception { diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/ClearRolesCacheTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/ClearRolesCacheTests.java index 6c6493f088513..5f2b7a70cf6f3 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/ClearRolesCacheTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/ClearRolesCacheTests.java @@ -55,13 +55,20 @@ public void setupForTests() throws IOException { final RestHighLevelClient restClient = new TestRestHighLevelClient(); // create roles for (String role : roles) { - restClient.security().putRole(new PutRoleRequest( - Role.builder().name(role) - .clusterPrivileges("none") - .indicesPrivileges( - IndicesPrivileges.builder().indices("*").privileges("ALL").allowRestrictedIndices(randomBoolean()).build()) - .build(), RefreshPolicy.IMMEDIATE), - SECURITY_REQUEST_OPTIONS); + restClient.security() + .putRole( + new PutRoleRequest( + Role.builder() + .name(role) + .clusterPrivileges("none") + .indicesPrivileges( + IndicesPrivileges.builder().indices("*").privileges("ALL").allowRestrictedIndices(randomBoolean()).build() + ) + .build(), + RefreshPolicy.IMMEDIATE + ), + SECURITY_REQUEST_OPTIONS + ); logger.debug("--> created role [{}]", role); } @@ -88,12 +95,21 @@ public void testModifyingViaApiClearsCache() throws Exception { List toModify = randomSubsetOf(modifiedRolesCount, roles); logger.debug("--> modifying roles {} to have run_as", toModify); for (String role : toModify) { - PutRoleResponse response = restClient.security().putRole(new PutRoleRequest(Role.builder().name(role) - .clusterPrivileges("none") - .indicesPrivileges( - IndicesPrivileges.builder().indices("*").privileges("ALL").allowRestrictedIndices(randomBoolean()).build()) - .runAsPrivilege(role) - .build(), randomBoolean() ? RefreshPolicy.IMMEDIATE : RefreshPolicy.NONE), SECURITY_REQUEST_OPTIONS); + PutRoleResponse response = restClient.security() + .putRole( + new PutRoleRequest( + Role.builder() + .name(role) + .clusterPrivileges("none") + .indicesPrivileges( + IndicesPrivileges.builder().indices("*").privileges("ALL").allowRestrictedIndices(randomBoolean()).build() + ) + .runAsPrivilege(role) + .build(), + randomBoolean() ? RefreshPolicy.IMMEDIATE : RefreshPolicy.NONE + ), + SECURITY_REQUEST_OPTIONS + ); assertThat(response.isCreated(), is(false)); logger.debug("--> updated role [{}] with run_as", role); } @@ -126,8 +142,11 @@ private void assertRolesAreCorrect(RestHighLevelClient restClient, List assertThat("role [" + role + "] should be modified and have run as", runAs == null || runAs.size() == 0, is(false)); assertThat(runAs.contains(role), is(true)); } else { - assertThat("role [" + role + "] should be cached and not have run as set but does!", runAs == null || runAs.size() == 0, - is(true)); + assertThat( + "role [" + role + "] should be cached and not have run as set but does!", + runAs == null || runAs.size() == 0, + is(true) + ); } } } diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/ClusterPrivilegeIntegrationTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/ClusterPrivilegeIntegrationTests.java index e36e869707026..87c9804bc48cc 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/ClusterPrivilegeIntegrationTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/ClusterPrivilegeIntegrationTests.java @@ -24,29 +24,27 @@ public class ClusterPrivilegeIntegrationTests extends AbstractPrivilegeTestCase { - private static final String ROLES = - "role_a:\n" + - " cluster: [ all ]\n" + - "\n" + - "role_b:\n" + - " cluster: [ monitor ]\n" + - "\n" + - "role_c:\n" + - " indices:\n" + - " - names: 'someindex'\n" + - " privileges: [ all ]\n" + - "role_d:\n" + - " cluster: [ create_snapshot ]\n" + - "\n" + - "role_e:\n" + - " cluster: [ monitor_snapshot]\n"; - - private static final String USERS_ROLES = - "role_a:user_a\n" + - "role_b:user_b\n" + - "role_c:user_c\n" + - "role_d:user_d\n" + - "role_e:user_e\n"; + private static final String ROLES = "role_a:\n" + + " cluster: [ all ]\n" + + "\n" + + "role_b:\n" + + " cluster: [ monitor ]\n" + + "\n" + + "role_c:\n" + + " indices:\n" + + " - names: 'someindex'\n" + + " privileges: [ all ]\n" + + "role_d:\n" + + " cluster: [ create_snapshot ]\n" + + "\n" + + "role_e:\n" + + " cluster: [ monitor_snapshot]\n"; + + private static final String USERS_ROLES = "role_a:user_a\n" + + "role_b:user_b\n" + + "role_c:user_c\n" + + "role_d:user_d\n" + + "role_e:user_e\n"; private static Path repositoryLocation; @@ -67,9 +65,7 @@ protected boolean addMockHttpTransport() { @Override protected Settings nodeSettings() { - return Settings.builder().put(super.nodeSettings()) - .put("path.repo", repositoryLocation) - .build(); + return Settings.builder().put(super.nodeSettings()).put("path.repo", repositoryLocation).build(); } @Override @@ -81,12 +77,22 @@ protected String configRoles() { protected String configUsers() { final Hasher passwdHasher = getFastStoredHashAlgoForTests(); final String usersPasswdHashed = new String(passwdHasher.hash(SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)); - return super.configUsers() + - "user_a:" + usersPasswdHashed + "\n" + - "user_b:" + usersPasswdHashed + "\n" + - "user_c:" + usersPasswdHashed + "\n" + - "user_d:" + usersPasswdHashed + "\n" + - "user_e:" + usersPasswdHashed + "\n"; + return super.configUsers() + + "user_a:" + + usersPasswdHashed + + "\n" + + "user_b:" + + usersPasswdHashed + + "\n" + + "user_c:" + + usersPasswdHashed + + "\n" + + "user_d:" + + usersPasswdHashed + + "\n" + + "user_e:" + + usersPasswdHashed + + "\n"; } @Override @@ -160,8 +166,14 @@ public void testThatClusterPrivilegesWorkAsExpectedViaHttp() throws Exception { } public void testThatSnapshotAndRestore() throws Exception { - String repoJson = Strings.toString(jsonBuilder().startObject().field("type", "fs").startObject("settings").field("location", - repositoryLocation.toString()).endObject().endObject()); + String repoJson = Strings.toString( + jsonBuilder().startObject() + .field("type", "fs") + .startObject("settings") + .field("location", repositoryLocation.toString()) + .endObject() + .endObject() + ); assertAccessIsDenied("user_b", "PUT", "/_snapshot/my-repo", repoJson); assertAccessIsDenied("user_c", "PUT", "/_snapshot/my-repo", repoJson); assertAccessIsDenied("user_d", "PUT", "/_snapshot/my-repo", repoJson); @@ -233,8 +245,12 @@ private void waitForSnapshotToFinish(String repo, String snapshot) throws Except assertThat(response.getSnapshots().get(0).getState(), is(SnapshotsInProgress.State.SUCCESS)); // The status of the snapshot in the repository can become SUCCESS before it is fully finalized in the cluster state so wait for // it to disappear from the cluster state as well - SnapshotsInProgress snapshotsInProgress = - client().admin().cluster().state(new ClusterStateRequest()).get().getState().custom(SnapshotsInProgress.TYPE); + SnapshotsInProgress snapshotsInProgress = client().admin() + .cluster() + .state(new ClusterStateRequest()) + .get() + .getState() + .custom(SnapshotsInProgress.TYPE); assertTrue(snapshotsInProgress.isEmpty()); }); } diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/CreateDocsIndexPrivilegeTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/CreateDocsIndexPrivilegeTests.java index e06729dc1aadb..b56f0d6dffaf6 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/CreateDocsIndexPrivilegeTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/CreateDocsIndexPrivilegeTests.java @@ -18,19 +18,16 @@ public class CreateDocsIndexPrivilegeTests extends AbstractPrivilegeTestCase { private static final String INDEX_NAME = "index-1"; private static final String CREATE_DOC_USER = "create_doc_user"; private String jsonDoc = "{ \"name\" : \"elasticsearch\", \"body\": \"foo bar\" }"; - private static final String ROLES = - "all_indices_role:\n" + - " indices:\n" + - " - names: '*'\n" + - " privileges: [ all ]\n" + - "create_doc_role:\n" + - " indices:\n" + - " - names: '*'\n" + - " privileges: [ create_doc ]\n"; - - private static final String USERS_ROLES = - "all_indices_role:admin\n" + - "create_doc_role:" + CREATE_DOC_USER + "\n"; + private static final String ROLES = "all_indices_role:\n" + + " indices:\n" + + " - names: '*'\n" + + " privileges: [ all ]\n" + + "create_doc_role:\n" + + " indices:\n" + + " - names: '*'\n" + + " privileges: [ create_doc ]\n"; + + private static final String USERS_ROLES = "all_indices_role:admin\n" + "create_doc_role:" + CREATE_DOC_USER + "\n"; @Override protected boolean addMockHttpTransport() { @@ -47,9 +44,7 @@ protected String configUsers() { final Hasher passwdHasher = getFastStoredHashAlgoForTests(); final String usersPasswdHashed = new String(passwdHasher.hash(SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)); - return super.configUsers() + - "admin:" + usersPasswdHashed + "\n" + - CREATE_DOC_USER + ":" + usersPasswdHashed + "\n"; + return super.configUsers() + "admin:" + usersPasswdHashed + "\n" + CREATE_DOC_USER + ":" + usersPasswdHashed + "\n"; } @Override @@ -70,8 +65,12 @@ public void testCreateDocUserCanIndexNewDocumentsWithAutoGeneratedId() throws IO } public void testCreateDocUserCanIndexNewDocumentsWithExternalIdAndOpTypeIsCreate() throws IOException { - assertAccessIsAllowed(CREATE_DOC_USER, randomFrom("PUT", "POST"), "/" + INDEX_NAME + "/_doc/2?op_type=create", "{ \"foo\" : " + - "\"bar\" }"); + assertAccessIsAllowed( + CREATE_DOC_USER, + randomFrom("PUT", "POST"), + "/" + INDEX_NAME + "/_doc/2?op_type=create", + "{ \"foo\" : " + "\"bar\" }" + ); } public void testCreateDocUserIsDeniedToIndexNewDocumentsWithExternalIdAndOpTypeIsIndex() throws IOException { @@ -84,30 +83,54 @@ public void testCreateDocUserIsDeniedToIndexUpdatesToExistingDocument() throws I } public void testCreateDocUserCanIndexNewDocumentsWithAutoGeneratedIdUsingBulkApi() throws IOException { - assertAccessIsAllowed(CREATE_DOC_USER, randomFrom("PUT", "POST"), - "/" + INDEX_NAME + "/_bulk", "{ \"index\" : { } }\n{ \"foo\" : \"bar\" }\n"); + assertAccessIsAllowed( + CREATE_DOC_USER, + randomFrom("PUT", "POST"), + "/" + INDEX_NAME + "/_bulk", + "{ \"index\" : { } }\n{ \"foo\" : \"bar\" }\n" + ); } public void testCreateDocUserCanIndexNewDocumentsWithAutoGeneratedIdAndOpTypeCreateUsingBulkApi() throws IOException { - assertAccessIsAllowed(CREATE_DOC_USER, randomFrom("PUT", "POST"), - "/" + INDEX_NAME + "/_bulk", "{ \"create\" : { } }\n{ \"foo\" : \"bar\" }\n"); + assertAccessIsAllowed( + CREATE_DOC_USER, + randomFrom("PUT", "POST"), + "/" + INDEX_NAME + "/_bulk", + "{ \"create\" : { } }\n{ \"foo\" : \"bar\" }\n" + ); } public void testCreateDocUserCanIndexNewDocumentsWithExternalIdAndOpTypeIsCreateUsingBulkApi() throws IOException { - assertAccessIsAllowed(CREATE_DOC_USER, randomFrom("PUT", "POST"), - "/" + INDEX_NAME + "/_bulk", "{ \"create\" : { \"_id\" : \"4\" } }\n{ \"foo\" : \"bar\" }\n"); + assertAccessIsAllowed( + CREATE_DOC_USER, + randomFrom("PUT", "POST"), + "/" + INDEX_NAME + "/_bulk", + "{ \"create\" : { \"_id\" : \"4\" } }\n{ \"foo\" : \"bar\" }\n" + ); } public void testCreateDocUserIsDeniedToIndexNewDocumentsWithExternalIdAndOpTypeIsIndexUsingBulkApi() throws IOException { - assertBodyHasAccessIsDenied(CREATE_DOC_USER, randomFrom("PUT", "POST"), - "/" + INDEX_NAME + "/_bulk", "{ \"index\" : { \"_id\" : \"5\" } }\n{ \"foo\" : \"bar\" }\n"); + assertBodyHasAccessIsDenied( + CREATE_DOC_USER, + randomFrom("PUT", "POST"), + "/" + INDEX_NAME + "/_bulk", + "{ \"index\" : { \"_id\" : \"5\" } }\n{ \"foo\" : \"bar\" }\n" + ); } public void testCreateDocUserIsDeniedToIndexUpdatesToExistingDocumentUsingBulkApi() throws IOException { - assertBodyHasAccessIsDenied(CREATE_DOC_USER, randomFrom("PUT", "POST"), - "/" + INDEX_NAME + "/_bulk", "{ \"index\" : { \"_id\" : \"1\" } }\n{ \"doc\" : {\"foo\" : \"bazbaz\"} }\n"); - assertBodyHasAccessIsDenied(CREATE_DOC_USER, randomFrom("PUT", "POST"), - "/" + INDEX_NAME + "/_bulk", "{ \"update\" : { \"_id\" : \"1\" } }\n{ \"doc\" : {\"foo\" : \"bazbaz\"} }\n"); + assertBodyHasAccessIsDenied( + CREATE_DOC_USER, + randomFrom("PUT", "POST"), + "/" + INDEX_NAME + "/_bulk", + "{ \"index\" : { \"_id\" : \"1\" } }\n{ \"doc\" : {\"foo\" : \"bazbaz\"} }\n" + ); + assertBodyHasAccessIsDenied( + CREATE_DOC_USER, + randomFrom("PUT", "POST"), + "/" + INDEX_NAME + "/_bulk", + "{ \"update\" : { \"_id\" : \"1\" } }\n{ \"doc\" : {\"foo\" : \"bazbaz\"} }\n" + ); } } diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/DateMathExpressionIntegTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/DateMathExpressionIntegTests.java index 14bfc0be5f8a0..63671709627c5 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/DateMathExpressionIntegTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/DateMathExpressionIntegTests.java @@ -39,24 +39,22 @@ public class DateMathExpressionIntegTests extends SecurityIntegTestCase { protected String configUsers() { final String usersPasswdHashed = new String(getFastStoredHashAlgoForTests().hash(USERS_PASSWD)); - return super.configUsers() + - "user1:" + usersPasswdHashed + "\n"; + return super.configUsers() + "user1:" + usersPasswdHashed + "\n"; } @Override protected String configUsersRoles() { - return super.configUsersRoles() + - "role1:user1\n"; + return super.configUsersRoles() + "role1:user1\n"; } @Override protected String configRoles() { - return super.configRoles() + - "\nrole1:\n" + - " cluster: [ none ]\n" + - " indices:\n" + - " - names: 'datemath-*'\n" + - " privileges: [ ALL ]\n"; + return super.configRoles() + + "\nrole1:\n" + + " cluster: [ none ]\n" + + " indices:\n" + + " - names: 'datemath-*'\n" + + " privileges: [ ALL ]\n"; } public void testDateMathExpressionsCanBeAuthorized() throws Exception { @@ -69,8 +67,10 @@ public void testDateMathExpressionsCanBeAuthorized() throws Exception { CreateIndexResponse response = client.admin().indices().prepareCreate(expression).get(); assertThat(response.isAcknowledged(), is(true)); } - IndexResponse response = client.prepareIndex(expression).setSource("foo", "bar") - .setRefreshPolicy(refeshOnOperation ? IMMEDIATE : NONE).get(); + IndexResponse response = client.prepareIndex(expression) + .setSource("foo", "bar") + .setRefreshPolicy(refeshOnOperation ? IMMEDIATE : NONE) + .get(); assertEquals(DocWriteResponse.Result.CREATED, response.getResult()); assertThat(response.getIndex(), containsString(expectedIndexName)); @@ -78,20 +78,18 @@ public void testDateMathExpressionsCanBeAuthorized() throws Exception { if (refeshOnOperation == false) { client.admin().indices().prepareRefresh(expression).get(); } - SearchResponse searchResponse = client.prepareSearch(expression) - .setQuery(QueryBuilders.matchAllQuery()) - .get(); + SearchResponse searchResponse = client.prepareSearch(expression).setQuery(QueryBuilders.matchAllQuery()).get(); assertThat(searchResponse.getHits().getTotalHits().value, is(1L)); MultiSearchResponse multiSearchResponse = client.prepareMultiSearch() - .add(client.prepareSearch(expression).setQuery(QueryBuilders.matchAllQuery()).request()) - .get(); + .add(client.prepareSearch(expression).setQuery(QueryBuilders.matchAllQuery()).request()) + .get(); assertThat(multiSearchResponse.getResponses()[0].getResponse().getHits().getTotalHits().value, is(1L)); UpdateResponse updateResponse = client.prepareUpdate(expression, response.getId()) - .setDoc(Requests.INDEX_CONTENT_TYPE, "new", "field") - .setRefreshPolicy(refeshOnOperation ? IMMEDIATE : NONE) - .get(); + .setDoc(Requests.INDEX_CONTENT_TYPE, "new", "field") + .setRefreshPolicy(refeshOnOperation ? IMMEDIATE : NONE) + .get(); assertEquals(DocWriteResponse.Result.UPDATED, updateResponse.getResult()); if (refeshOnOperation == false) { @@ -104,14 +102,11 @@ public void testDateMathExpressionsCanBeAuthorized() throws Exception { assertThat(getResponse.getSourceAsMap().get("new").toString(), is("field")); // multi get doesn't support expressions - this is probably a bug - MultiGetResponse multiGetResponse = client.prepareMultiGet() - .add(expression, response.getId()) - .get(); + MultiGetResponse multiGetResponse = client.prepareMultiGet().add(expression, response.getId()).get(); assertFalse(multiGetResponse.getResponses()[0].isFailed()); assertTrue(multiGetResponse.getResponses()[0].getResponse().isExists()); assertEquals(expectedIndexName, multiGetResponse.getResponses()[0].getResponse().getIndex()); - AcknowledgedResponse deleteIndexResponse = client.admin().indices().prepareDelete(expression).get(); assertThat(deleteIndexResponse.isAcknowledged(), is(true)); } diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/DlsFlsRequestCacheTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/DlsFlsRequestCacheTests.java index 42c00c27292d7..89cdde42554b6 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/DlsFlsRequestCacheTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/DlsFlsRequestCacheTests.java @@ -16,13 +16,13 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.cache.request.RequestCacheStats; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.script.mustache.MustachePlugin; import org.elasticsearch.search.SearchHit; import org.elasticsearch.test.SecuritySingleNodeTestCase; import org.elasticsearch.test.hamcrest.ElasticsearchAssertions; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.xpack.core.security.action.CreateApiKeyAction; import org.elasticsearch.xpack.core.security.action.CreateApiKeyRequest; @@ -67,10 +67,7 @@ public class DlsFlsRequestCacheTests extends SecuritySingleNodeTestCase { @Override protected Settings nodeSettings() { - return Settings.builder() - .put(super.nodeSettings()) - .put(XPackSettings.API_KEY_SERVICE_ENABLED_SETTING.getKey(), true) - .build(); + return Settings.builder().put(super.nodeSettings()).put(XPackSettings.API_KEY_SERVICE_ENABLED_SETTING.getKey(), true).build(); } @Override @@ -83,15 +80,25 @@ protected Collection> getPlugins() { @Override protected String configUsers() { return super.configUsers() - + DLS_FLS_USER + ":" + TEST_PASSWORD_HASHED + "\n" - + DLS_TEMPLATE_ROLE_QUERY_USER_2 + ":" + TEST_PASSWORD_HASHED + "\n" - + DLS_TEMPLATE_ROLE_QUERY_USER_1 + ":" + TEST_PASSWORD_HASHED + "\n"; + + DLS_FLS_USER + + ":" + + TEST_PASSWORD_HASHED + + "\n" + + DLS_TEMPLATE_ROLE_QUERY_USER_2 + + ":" + + TEST_PASSWORD_HASHED + + "\n" + + DLS_TEMPLATE_ROLE_QUERY_USER_1 + + ":" + + TEST_PASSWORD_HASHED + + "\n"; } @Override protected String configRoles() { return super.configRoles() - + DLS_FLS_USER + ":\n" + + DLS_FLS_USER + + ":\n" + " cluster: [ \"manage_own_api_key\" ]\n" + " indices:\n" + " - names:\n" @@ -142,7 +149,8 @@ protected String configRoles() { + " - \"all-alias\"\n" + " privileges:\n" + " - \"read\"\n" - + DLS_TEMPLATE_ROLE_QUERY_ROLE + ":\n" + + DLS_TEMPLATE_ROLE_QUERY_ROLE + + ":\n" + " indices:\n" + " - names:\n" + " - \"dls-template-role-query-index\"\n" @@ -159,8 +167,16 @@ protected String configRoles() { @Override protected String configUsersRoles() { return super.configUsersRoles() - + DLS_FLS_USER + ":" + DLS_FLS_USER + "\n" - + DLS_TEMPLATE_ROLE_QUERY_ROLE + ":" + DLS_TEMPLATE_ROLE_QUERY_USER_1 + "," + DLS_TEMPLATE_ROLE_QUERY_USER_2 + "\n"; + + DLS_FLS_USER + + ":" + + DLS_FLS_USER + + "\n" + + DLS_TEMPLATE_ROLE_QUERY_ROLE + + ":" + + DLS_TEMPLATE_ROLE_QUERY_USER_1 + + "," + + DLS_TEMPLATE_ROLE_QUERY_USER_2 + + "\n"; } @Before @@ -206,34 +222,40 @@ public void testRequestCacheForFLS() { final Client limitedClient = limitedClient(); // Search first with power client, it should see all fields - assertSearchResponse(powerClient.prepareSearch(FLS_INDEX).setRequestCache(true).get(), - Set.of("201", "202"), Set.of("public", "private")); + assertSearchResponse( + powerClient.prepareSearch(FLS_INDEX).setRequestCache(true).get(), + Set.of("201", "202"), + Set.of("public", "private") + ); assertCacheState(FLS_INDEX, 0, 1); // Search with limited client and it should see only public field - assertSearchResponse(limitedClient.prepareSearch(FLS_INDEX).setRequestCache(true).get(), - Set.of("201", "202"), Set.of("public")); + assertSearchResponse(limitedClient.prepareSearch(FLS_INDEX).setRequestCache(true).get(), Set.of("201", "202"), Set.of("public")); assertCacheState(FLS_INDEX, 0, 2); // Search with limited client again and it should use the cache - assertSearchResponse(limitedClient.prepareSearch(FLS_INDEX).setRequestCache(true).get(), - Set.of("201", "202"), Set.of("public")); + assertSearchResponse(limitedClient.prepareSearch(FLS_INDEX).setRequestCache(true).get(), Set.of("201", "202"), Set.of("public")); assertCacheState(FLS_INDEX, 1, 2); // Search again with power client, it should use its own cache entry - assertSearchResponse(powerClient.prepareSearch(FLS_INDEX).setRequestCache(true).get(), - Set.of("201", "202"), Set.of("public", "private")); + assertSearchResponse( + powerClient.prepareSearch(FLS_INDEX).setRequestCache(true).get(), + Set.of("201", "202"), + Set.of("public", "private") + ); assertCacheState(FLS_INDEX, 2, 2); // The fls-alias has a different FLS definition compared to its underlying fls-index. - assertSearchResponse(limitedClient.prepareSearch(FLS_ALIAS).setRequestCache(true).get(), - Set.of("201", "202"), Set.of("private")); + assertSearchResponse(limitedClient.prepareSearch(FLS_ALIAS).setRequestCache(true).get(), Set.of("201", "202"), Set.of("private")); assertCacheState(FLS_INDEX, 2, 3); // Search with the limited client for both fls-alias and fls-index and all docs and fields are also returned. // But request cache is not shared with the power client because it still has a different indexAccessControl - assertSearchResponse(limitedClient.prepareSearch(FLS_ALIAS, FLS_INDEX).setRequestCache(true).get(), - Set.of("201", "202"), Set.of("public", "private")); + assertSearchResponse( + limitedClient.prepareSearch(FLS_ALIAS, FLS_INDEX).setRequestCache(true).get(), + Set.of("201", "202"), + Set.of("public", "private") + ); assertCacheState(FLS_INDEX, 2, 4); } @@ -242,8 +264,11 @@ public void testRequestCacheForBothDLSandFLS() throws ExecutionException, Interr final Client limitedClient = limitedClient(); // Search first with power client, it should see all fields - assertSearchResponse(powerClient.prepareSearch(INDEX).setRequestCache(true).get(), - Set.of("1", "2"), Set.of("number", "letter", "public", "private")); + assertSearchResponse( + powerClient.prepareSearch(INDEX).setRequestCache(true).get(), + Set.of("1", "2"), + Set.of("number", "letter", "public", "private") + ); assertCacheState(INDEX, 0, 1); // The limited client does not have access to the underlying index @@ -251,71 +276,106 @@ public void testRequestCacheForBothDLSandFLS() throws ExecutionException, Interr expectThrows(ElasticsearchSecurityException.class, () -> limitedClient.prepareSearch(INDEX).setRequestCache(true).get()); // Search for alias1 that points to index and has DLS/FLS - assertSearchResponse(limitedClient.prepareSearch(ALIAS1).setRequestCache(true).get(), - Set.of("1"), Set.of("number", "letter", "public")); + assertSearchResponse( + limitedClient.prepareSearch(ALIAS1).setRequestCache(true).get(), + Set.of("1"), + Set.of("number", "letter", "public") + ); assertCacheState(INDEX, 0, 2); // Search for alias2 that also points to index but has a different set of DLS/FLS - assertSearchResponse(limitedClient.prepareSearch(ALIAS2).setRequestCache(true).get(), - Set.of("2"), Set.of("number", "letter", "private")); + assertSearchResponse( + limitedClient.prepareSearch(ALIAS2).setRequestCache(true).get(), + Set.of("2"), + Set.of("number", "letter", "private") + ); assertCacheState(INDEX, 0, 3); // Search for all-alias that has full read access to the underlying index // This makes it share the cache entry of the power client - assertSearchResponse(limitedClient.prepareSearch(ALL_ALIAS).setRequestCache(true).get(), - Set.of("1", "2"), Set.of("number", "letter", "public", "private")); + assertSearchResponse( + limitedClient.prepareSearch(ALL_ALIAS).setRequestCache(true).get(), + Set.of("1", "2"), + Set.of("number", "letter", "public", "private") + ); assertCacheState(INDEX, 1, 3); // Similarly, search for alias1 and all-alias results in full read access to the index // and again reuse the cache entry of the power client - assertSearchResponse(limitedClient.prepareSearch(ALIAS1, ALL_ALIAS).setRequestCache(true).get(), - Set.of("1", "2"), Set.of("number", "letter", "public", "private")); + assertSearchResponse( + limitedClient.prepareSearch(ALIAS1, ALL_ALIAS).setRequestCache(true).get(), + Set.of("1", "2"), + Set.of("number", "letter", "public", "private") + ); assertCacheState(INDEX, 2, 3); // Though search for both alias1 and alias2 is effectively full read access to index, // it does not share the cache entry of the power client because role queries still exist. - assertSearchResponse(limitedClient.prepareSearch(ALIAS1, ALIAS2).setRequestCache(true).get(), - Set.of("1", "2"), Set.of("number", "letter", "public", "private")); + assertSearchResponse( + limitedClient.prepareSearch(ALIAS1, ALIAS2).setRequestCache(true).get(), + Set.of("1", "2"), + Set.of("number", "letter", "public", "private") + ); assertCacheState(INDEX, 2, 4); // Test with an API Key that has different DLS/FLS on all-alias final Client limitedClientApiKey = limitedClientApiKey(); // It should not reuse any entries from the cache - assertSearchResponse(limitedClientApiKey.prepareSearch(ALL_ALIAS).setRequestCache(true).get(), - Set.of("1"), Set.of("letter", "public", "private")); + assertSearchResponse( + limitedClientApiKey.prepareSearch(ALL_ALIAS).setRequestCache(true).get(), + Set.of("1"), + Set.of("letter", "public", "private") + ); assertCacheState(INDEX, 2, 5); } public void testRequestCacheWithTemplateRoleQuery() { - final Client client1 = client().filterWithHeader(Map.of( - "Authorization", basicAuthHeaderValue(DLS_TEMPLATE_ROLE_QUERY_USER_1, new SecureString(TEST_PASSWORD.toCharArray())))); - final Client client2 = client().filterWithHeader(Map.of( - "Authorization", basicAuthHeaderValue(DLS_TEMPLATE_ROLE_QUERY_USER_2, new SecureString(TEST_PASSWORD.toCharArray())))); + final Client client1 = client().filterWithHeader( + Map.of("Authorization", basicAuthHeaderValue(DLS_TEMPLATE_ROLE_QUERY_USER_1, new SecureString(TEST_PASSWORD.toCharArray()))) + ); + final Client client2 = client().filterWithHeader( + Map.of("Authorization", basicAuthHeaderValue(DLS_TEMPLATE_ROLE_QUERY_USER_2, new SecureString(TEST_PASSWORD.toCharArray()))) + ); // Search first with user1 and only one document will be return with the corresponding username - assertSearchResponse(client1.prepareSearch(DLS_TEMPLATE_ROLE_QUERY_INDEX).setRequestCache(true).get(), - Set.of("1"), Set.of("username")); + assertSearchResponse( + client1.prepareSearch(DLS_TEMPLATE_ROLE_QUERY_INDEX).setRequestCache(true).get(), + Set.of("1"), + Set.of("username") + ); assertCacheState(DLS_TEMPLATE_ROLE_QUERY_INDEX, 0, 1); // Search with user2 will not use user1's cache because template query is resolved differently for them - assertSearchResponse(client2.prepareSearch(DLS_TEMPLATE_ROLE_QUERY_INDEX).setRequestCache(true).get(), - Set.of("2"), Set.of("username")); + assertSearchResponse( + client2.prepareSearch(DLS_TEMPLATE_ROLE_QUERY_INDEX).setRequestCache(true).get(), + Set.of("2"), + Set.of("username") + ); assertCacheState(DLS_TEMPLATE_ROLE_QUERY_INDEX, 0, 2); // Search with user1 again will use user1's cache - assertSearchResponse(client1.prepareSearch(DLS_TEMPLATE_ROLE_QUERY_INDEX).setRequestCache(true).get(), - Set.of("1"), Set.of("username")); + assertSearchResponse( + client1.prepareSearch(DLS_TEMPLATE_ROLE_QUERY_INDEX).setRequestCache(true).get(), + Set.of("1"), + Set.of("username") + ); assertCacheState(DLS_TEMPLATE_ROLE_QUERY_INDEX, 1, 2); // Search with user2 again will use user2's cache - assertSearchResponse(client2.prepareSearch(DLS_TEMPLATE_ROLE_QUERY_INDEX).setRequestCache(true).get(), - Set.of("2"), Set.of("username")); + assertSearchResponse( + client2.prepareSearch(DLS_TEMPLATE_ROLE_QUERY_INDEX).setRequestCache(true).get(), + Set.of("2"), + Set.of("username") + ); assertCacheState(DLS_TEMPLATE_ROLE_QUERY_INDEX, 2, 2); // Since the DLS for the alias uses a stored script, this should cause the request cached to be disabled - assertSearchResponse(client1.prepareSearch(DLS_TEMPLATE_ROLE_QUERY_ALIAS).setRequestCache(true).get(), - Set.of("1"), Set.of("username")); + assertSearchResponse( + client1.prepareSearch(DLS_TEMPLATE_ROLE_QUERY_ALIAS).setRequestCache(true).get(), + Set.of("1"), + Set.of("username") + ); // No cache should be used assertCacheState(DLS_TEMPLATE_ROLE_QUERY_INDEX, 2, 2); } @@ -323,10 +383,20 @@ public void testRequestCacheWithTemplateRoleQuery() { private void prepareIndices() { final Client client = client(); - assertAcked(client.admin().cluster().preparePutStoredScript().setId("my-script") - .setContent(new BytesArray("{\"script\":{\"source\":" + - "\"{\\\"match\\\":{\\\"username\\\":\\\"{{_user.username}}\\\"}}\",\"lang\":\"mustache\"}}"), XContentType.JSON) - .get()); + assertAcked( + client.admin() + .cluster() + .preparePutStoredScript() + .setId("my-script") + .setContent( + new BytesArray( + "{\"script\":{\"source\":" + + "\"{\\\"match\\\":{\\\"username\\\":\\\"{{_user.username}}\\\"}}\",\"lang\":\"mustache\"}}" + ), + XContentType.JSON + ) + .get() + ); assertAcked(client.admin().indices().prepareCreate(DLS_INDEX).addAlias(new Alias("dls-alias")).get()); client.prepareIndex(DLS_INDEX).setId("101").setSource("number", 101, "letter", "A").get(); @@ -336,16 +406,21 @@ private void prepareIndices() { client.prepareIndex(FLS_INDEX).setId("201").setSource("public", "X", "private", "x").get(); client.prepareIndex(FLS_INDEX).setId("202").setSource("public", "Y", "private", "y").get(); - assertAcked(client.admin().indices().prepareCreate(INDEX) - .addAlias(new Alias(ALIAS1)) - .addAlias(new Alias(ALIAS2)) - .addAlias(new Alias(ALL_ALIAS)) - .get()); + assertAcked( + client.admin() + .indices() + .prepareCreate(INDEX) + .addAlias(new Alias(ALIAS1)) + .addAlias(new Alias(ALIAS2)) + .addAlias(new Alias(ALL_ALIAS)) + .get() + ); client.prepareIndex(INDEX).setId("1").setSource("number", 1, "letter", "a", "private", "sesame_1", "public", "door_1").get(); client.prepareIndex(INDEX).setId("2").setSource("number", 2, "letter", "b", "private", "sesame_2", "public", "door_2").get(); - assertAcked(client.admin().indices().prepareCreate(DLS_TEMPLATE_ROLE_QUERY_INDEX) - .addAlias(new Alias(DLS_TEMPLATE_ROLE_QUERY_ALIAS)).get()); + assertAcked( + client.admin().indices().prepareCreate(DLS_TEMPLATE_ROLE_QUERY_INDEX).addAlias(new Alias(DLS_TEMPLATE_ROLE_QUERY_ALIAS)).get() + ); client.prepareIndex(DLS_TEMPLATE_ROLE_QUERY_INDEX).setId("1").setSource("username", DLS_TEMPLATE_ROLE_QUERY_USER_1).get(); client.prepareIndex(DLS_TEMPLATE_ROLE_QUERY_INDEX).setId("2").setSource("username", DLS_TEMPLATE_ROLE_QUERY_USER_2).get(); @@ -356,34 +431,50 @@ private void prepareIndices() { assertCacheState(DLS_TEMPLATE_ROLE_QUERY_INDEX, 0, 0); // Force merge the index to ensure there can be no background merges during the subsequent searches that would invalidate the cache - final ForceMergeResponse forceMergeResponse = client.admin().indices() - .prepareForceMerge(DLS_INDEX, FLS_INDEX, INDEX, DLS_TEMPLATE_ROLE_QUERY_INDEX).setFlush(true).get(); + final ForceMergeResponse forceMergeResponse = client.admin() + .indices() + .prepareForceMerge(DLS_INDEX, FLS_INDEX, INDEX, DLS_TEMPLATE_ROLE_QUERY_INDEX) + .setFlush(true) + .get(); ElasticsearchAssertions.assertAllSuccessful(forceMergeResponse); - final RefreshResponse refreshResponse = client.admin().indices() - .prepareRefresh(DLS_INDEX, FLS_INDEX, INDEX, DLS_TEMPLATE_ROLE_QUERY_INDEX).get(); + final RefreshResponse refreshResponse = client.admin() + .indices() + .prepareRefresh(DLS_INDEX, FLS_INDEX, INDEX, DLS_TEMPLATE_ROLE_QUERY_INDEX) + .get(); assertThat(refreshResponse.getFailedShards(), equalTo(0)); ensureGreen(DLS_INDEX, FLS_INDEX, INDEX, DLS_TEMPLATE_ROLE_QUERY_INDEX); } private Client limitedClient() { - return client().filterWithHeader(Map.of( - "Authorization", basicAuthHeaderValue(DLS_FLS_USER, new SecureString(TEST_PASSWORD.toCharArray())))); + return client().filterWithHeader( + Map.of("Authorization", basicAuthHeaderValue(DLS_FLS_USER, new SecureString(TEST_PASSWORD.toCharArray()))) + ); } private Client limitedClientApiKey() throws ExecutionException, InterruptedException { - final CreateApiKeyRequest createApiKeyRequest = new CreateApiKeyRequest(randomAlphaOfLengthBetween(3, 8), - List.of(new RoleDescriptor(randomAlphaOfLengthBetween(3, 8), - null, - new RoleDescriptor.IndicesPrivileges[]{ - RoleDescriptor.IndicesPrivileges.builder().indices(ALL_ALIAS) - .privileges("read").query("{\"term\":{\"letter\":\"a\"}}").grantedFields("*").deniedFields("number").build() - }, - null)), - null); + final CreateApiKeyRequest createApiKeyRequest = new CreateApiKeyRequest( + randomAlphaOfLengthBetween(3, 8), + List.of( + new RoleDescriptor( + randomAlphaOfLengthBetween(3, 8), + null, + new RoleDescriptor.IndicesPrivileges[] { + RoleDescriptor.IndicesPrivileges.builder() + .indices(ALL_ALIAS) + .privileges("read") + .query("{\"term\":{\"letter\":\"a\"}}") + .grantedFields("*") + .deniedFields("number") + .build() }, + null + ) + ), + null + ); final CreateApiKeyResponse createApiKeyResponse = limitedClient().execute(CreateApiKeyAction.INSTANCE, createApiKeyRequest).get(); - final String base64ApiKey = Base64.getEncoder().encodeToString( - (createApiKeyResponse.getId() + ":" + createApiKeyResponse.getKey()).getBytes(StandardCharsets.UTF_8)); + final String base64ApiKey = Base64.getEncoder() + .encodeToString((createApiKeyResponse.getId() + ":" + createApiKeyResponse.getKey()).getBytes(StandardCharsets.UTF_8)); return client().filterWithHeader(Map.of("Authorization", "ApiKey " + base64ApiKey)); } @@ -404,14 +495,19 @@ private void assertSearchResponse(SearchResponse searchResponse, Set doc } private void assertCacheState(String index, long expectedHits, long expectedMisses) { - RequestCacheStats requestCacheStats = client().admin().indices().prepareStats(index) + RequestCacheStats requestCacheStats = client().admin() + .indices() + .prepareStats(index) .setRequestCache(true) - .get().getTotal().getRequestCache(); + .get() + .getTotal() + .getRequestCache(); // Check the hit count and miss count together so if they are not // correct we can see both values assertEquals( Arrays.asList(expectedHits, expectedMisses, 0L), - Arrays.asList(requestCacheStats.getHitCount(), requestCacheStats.getMissCount(), requestCacheStats.getEvictions())); + Arrays.asList(requestCacheStats.getHitCount(), requestCacheStats.getMissCount(), requestCacheStats.getEvictions()) + ); } private void clearCache() { diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/DocumentAndFieldLevelSecurityTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/DocumentAndFieldLevelSecurityTests.java index f18fd2bf1ae14..09444a8914f10 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/DocumentAndFieldLevelSecurityTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/DocumentAndFieldLevelSecurityTests.java @@ -50,81 +50,82 @@ public class DocumentAndFieldLevelSecurityTests extends SecurityIntegTestCase { protected String configUsers() { final String usersPasswdHashed = new String(getFastStoredHashAlgoForTests().hash(USERS_PASSWD)); - return super.configUsers() + - "user1:" + usersPasswdHashed + "\n" + - "user2:" + usersPasswdHashed + "\n" + - "user3:" + usersPasswdHashed + "\n" + - "user4:" + usersPasswdHashed + "\n" + - "user5:" + usersPasswdHashed + "\n"; + return super.configUsers() + + "user1:" + + usersPasswdHashed + + "\n" + + "user2:" + + usersPasswdHashed + + "\n" + + "user3:" + + usersPasswdHashed + + "\n" + + "user4:" + + usersPasswdHashed + + "\n" + + "user5:" + + usersPasswdHashed + + "\n"; } @Override protected String configUsersRoles() { - return super.configUsersRoles() + - "role1:user1\n" + - "role2:user1,user4\n" + - "role3:user2,user4\n" + - "role4:user3,user4,user5\n"; + return super.configUsersRoles() + "role1:user1\n" + "role2:user1,user4\n" + "role3:user2,user4\n" + "role4:user3,user4,user5\n"; } @Override protected String configRoles() { - return super.configRoles() + - "\nrole1:\n" + - " cluster: [ none ]\n" + - " indices:\n" + - " - names: '*'\n" + - " privileges: [ none ]\n" + - "role2:\n" + - " cluster:\n" + - " - all\n" + - " indices:\n" + - " - names: '*'\n" + - " privileges: [ ALL ]\n" + - " field_security:\n" + - " grant: [ field1, id ]\n" + - " query: '{\"term\" : {\"field1\" : \"value1\"}}'\n" + - "role3:\n" + - " cluster: [ all ]\n" + - " indices:\n" + - " - names: '*'\n" + - " privileges: [ ALL ]\n" + - " field_security:\n" + - " grant: [ field2, id ]\n" + - " query: '{\"term\" : {\"field2\" : \"value2\"}}'\n" + - "role4:\n" + - " cluster: [ all ]\n" + - " indices:\n" + - " - names: '*'\n" + - " privileges: [ ALL ]\n" + - " field_security:\n" + - " grant: [ field1, id ]\n" + - " query: '{\"term\" : {\"field2\" : \"value2\"}}'\n"; + return super.configRoles() + + "\nrole1:\n" + + " cluster: [ none ]\n" + + " indices:\n" + + " - names: '*'\n" + + " privileges: [ none ]\n" + + "role2:\n" + + " cluster:\n" + + " - all\n" + + " indices:\n" + + " - names: '*'\n" + + " privileges: [ ALL ]\n" + + " field_security:\n" + + " grant: [ field1, id ]\n" + + " query: '{\"term\" : {\"field1\" : \"value1\"}}'\n" + + "role3:\n" + + " cluster: [ all ]\n" + + " indices:\n" + + " - names: '*'\n" + + " privileges: [ ALL ]\n" + + " field_security:\n" + + " grant: [ field2, id ]\n" + + " query: '{\"term\" : {\"field2\" : \"value2\"}}'\n" + + "role4:\n" + + " cluster: [ all ]\n" + + " indices:\n" + + " - names: '*'\n" + + " privileges: [ ALL ]\n" + + " field_security:\n" + + " grant: [ field1, id ]\n" + + " query: '{\"term\" : {\"field2\" : \"value2\"}}'\n"; } @Override public Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { return Settings.builder() - .put(super.nodeSettings(nodeOrdinal, otherSettings)) - .put(XPackSettings.DLS_FLS_ENABLED.getKey(), true) - .build(); + .put(super.nodeSettings(nodeOrdinal, otherSettings)) + .put(XPackSettings.DLS_FLS_ENABLED.getKey(), true) + .build(); } public void testSimpleQuery() { - assertAcked(client().admin().indices().prepareCreate("test") - .setMapping("id", "type=keyword", "field1", "type=text", "field2", "type=text") + assertAcked( + client().admin().indices().prepareCreate("test").setMapping("id", "type=keyword", "field1", "type=text", "field2", "type=text") ); - client().prepareIndex("test").setId("1").setSource("id", "1", "field1", "value1") - .setRefreshPolicy(IMMEDIATE) - .get(); - client().prepareIndex("test").setId("2").setSource("id", "2", "field2", "value2") - .setRefreshPolicy(IMMEDIATE) - .get(); + client().prepareIndex("test").setId("1").setSource("id", "1", "field1", "value1").setRefreshPolicy(IMMEDIATE).get(); + client().prepareIndex("test").setId("2").setSource("id", "2", "field2", "value2").setRefreshPolicy(IMMEDIATE).get(); SearchResponse response = client().filterWithHeader( - Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareSearch("test") - .get(); + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).prepareSearch("test").get(); assertHitCount(response, 1); assertSearchHits(response, "1"); assertThat(response.getHits().getAt(0).getSourceAsMap().size(), equalTo(2)); @@ -132,8 +133,8 @@ public void testSimpleQuery() { assertThat(response.getHits().getAt(0).getSourceAsMap().get("id").toString(), equalTo("1")); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareSearch("test") - .get(); + .prepareSearch("test") + .get(); assertHitCount(response, 1); assertSearchHits(response, "2"); assertThat(response.getHits().getAt(0).getSourceAsMap().size(), equalTo(2)); @@ -141,9 +142,9 @@ public void testSimpleQuery() { assertThat(response.getHits().getAt(0).getSourceAsMap().get("id").toString(), equalTo("2")); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user4", USERS_PASSWD))) - .prepareSearch("test") - .addSort("id", SortOrder.ASC) - .get(); + .prepareSearch("test") + .addSort("id", SortOrder.ASC) + .get(); assertHitCount(response, 2); assertSearchHits(response, "1", "2"); assertThat(response.getHits().getAt(0).getSourceAsMap().get("field1").toString(), equalTo("value1")); @@ -152,93 +153,86 @@ public void testSimpleQuery() { public void testUpdatesAreRejected() { for (String indexName : List.of("", "test")) { - assertAcked(client().admin().indices().prepareCreate(indexName) + assertAcked( + client().admin() + .indices() + .prepareCreate(indexName) .setMapping("id", "type=keyword", "field1", "type=text", "field2", "type=text") - .setSettings(Settings.builder() - .put("index.number_of_replicas", 0) - .put("index.number_of_shards", 1)) + .setSettings(Settings.builder().put("index.number_of_replicas", 0).put("index.number_of_shards", 1)) ); - client().prepareIndex(indexName).setId("1").setSource("id", "1", "field1", "value1") - .setRefreshPolicy(IMMEDIATE) - .get(); + client().prepareIndex(indexName).setId("1").setSource("id", "1", "field1", "value1").setRefreshPolicy(IMMEDIATE).get(); ElasticsearchSecurityException exception = expectThrows(ElasticsearchSecurityException.class, () -> { - client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, - basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareUpdate(indexName, "1") - .setDoc(Requests.INDEX_CONTENT_TYPE, "field2", "value2") - .get(); - }); - assertThat(exception.getDetailedMessage(), containsString("Can't execute an update request if field or document level " + - "security")); - - BulkResponse bulkResponse = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", - USERS_PASSWD))) - .prepareBulk() - .add(client().prepareUpdate(indexName, "1") - .setDoc(Requests.INDEX_CONTENT_TYPE, "field2", "value2")) + client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) + .prepareUpdate(indexName, "1") + .setDoc(Requests.INDEX_CONTENT_TYPE, "field2", "value2") .get(); + }); + assertThat( + exception.getDetailedMessage(), + containsString("Can't execute an update request if field or document level " + "security") + ); + + BulkResponse bulkResponse = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).prepareBulk().add(client().prepareUpdate(indexName, "1").setDoc(Requests.INDEX_CONTENT_TYPE, "field2", "value2")).get(); assertThat(bulkResponse.getItems().length, is(1)); - assertThat(bulkResponse.getItems()[0].getFailureMessage(), containsString("Can't execute a bulk item request with update " + - "requests" + - " embedded if field or document level security is enabled")); + assertThat( + bulkResponse.getItems()[0].getFailureMessage(), + containsString( + "Can't execute a bulk item request with update " + + "requests" + + " embedded if field or document level security is enabled" + ) + ); } } public void testDLSIsAppliedBeforeFLS() { - assertAcked(client().admin().indices().prepareCreate("test") - .setMapping("field1", "type=text", "field2", "type=text") - ); - client().prepareIndex("test").setId("1").setSource("field1", "value1", "field2", "value1") - .setRefreshPolicy(IMMEDIATE) - .get(); - client().prepareIndex("test").setId("2").setSource("field1", "value2", "field2", "value2") - .setRefreshPolicy(IMMEDIATE) - .get(); + assertAcked(client().admin().indices().prepareCreate("test").setMapping("field1", "type=text", "field2", "type=text")); + client().prepareIndex("test").setId("1").setSource("field1", "value1", "field2", "value1").setRefreshPolicy(IMMEDIATE).get(); + client().prepareIndex("test").setId("2").setSource("field1", "value2", "field2", "value2").setRefreshPolicy(IMMEDIATE).get(); SearchResponse response = client().filterWithHeader( - Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user5", USERS_PASSWD))) - .prepareSearch("test").setQuery(QueryBuilders.termQuery("field1", "value2")) - .get(); + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user5", USERS_PASSWD)) + ).prepareSearch("test").setQuery(QueryBuilders.termQuery("field1", "value2")).get(); assertHitCount(response, 1); assertSearchHits(response, "2"); assertThat(response.getHits().getAt(0).getSourceAsMap().size(), equalTo(1)); assertThat(response.getHits().getAt(0).getSourceAsMap().get("field1").toString(), equalTo("value2")); - response = client().filterWithHeader( - Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user5", USERS_PASSWD))) - .prepareSearch("test").setQuery(QueryBuilders.termQuery("field1", "value1")) - .get(); + response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user5", USERS_PASSWD))) + .prepareSearch("test") + .setQuery(QueryBuilders.termQuery("field1", "value1")) + .get(); assertHitCount(response, 0); } public void testQueryCache() { - assertAcked(client().admin().indices().prepareCreate("test") - .setSettings(Settings.builder().put(IndexModule.INDEX_QUERY_CACHE_EVERYTHING_SETTING.getKey(), true)) - .setMapping("id", "type=keyword", "field1", "type=text", "field2", "type=text") + assertAcked( + client().admin() + .indices() + .prepareCreate("test") + .setSettings(Settings.builder().put(IndexModule.INDEX_QUERY_CACHE_EVERYTHING_SETTING.getKey(), true)) + .setMapping("id", "type=keyword", "field1", "type=text", "field2", "type=text") ); - client().prepareIndex("test").setId("1").setSource("id", "1", "field1", "value1") - .setRefreshPolicy(IMMEDIATE) - .get(); - client().prepareIndex("test").setId("2").setSource("id", "2", "field2", "value2") - .setRefreshPolicy(IMMEDIATE) - .get(); + client().prepareIndex("test").setId("1").setSource("id", "1", "field1", "value1").setRefreshPolicy(IMMEDIATE).get(); + client().prepareIndex("test").setId("2").setSource("id", "2", "field2", "value2").setRefreshPolicy(IMMEDIATE).get(); // Both users have the same role query, but user3 has access to field2 and not field1, which should result in zero hits: int max = scaledRandomIntBetween(4, 32); for (int i = 0; i < max; i++) { SearchResponse response = client().filterWithHeader( - Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareSearch("test") - .get(); + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).prepareSearch("test").get(); assertHitCount(response, 1); assertThat(response.getHits().getAt(0).getId(), equalTo("1")); assertThat(response.getHits().getAt(0).getSourceAsMap().size(), equalTo(2)); assertThat(response.getHits().getAt(0).getSourceAsMap().get("field1"), equalTo("value1")); assertThat(response.getHits().getAt(0).getSourceAsMap().get("id"), equalTo("1")); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareSearch("test") - .get(); + .prepareSearch("test") + .get(); assertHitCount(response, 1); assertThat(response.getHits().getAt(0).getId(), equalTo("2")); assertThat(response.getHits().getAt(0).getSourceAsMap().size(), equalTo(2)); @@ -249,19 +243,18 @@ public void testQueryCache() { // permissions (field1), // this results in document 2 being returned but no fields are visible: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) - .prepareSearch("test") - .get(); + .prepareSearch("test") + .get(); assertHitCount(response, 1); assertThat(response.getHits().getAt(0).getId(), equalTo("2")); assertThat(response.getHits().getAt(0).getSourceAsMap().size(), equalTo(1)); assertThat(response.getHits().getAt(0).getSourceAsMap().get("id"), equalTo("2")); // user4 has all roles - response = client().filterWithHeader( - Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user4", USERS_PASSWD))) - .prepareSearch("test") - .addSort("id", SortOrder.ASC) - .get(); + response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user4", USERS_PASSWD))) + .prepareSearch("test") + .addSort("id", SortOrder.ASC) + .get(); assertHitCount(response, 2); assertThat(response.getHits().getAt(0).getId(), equalTo("1")); assertThat(response.getHits().getAt(0).getSourceAsMap().size(), equalTo(2)); @@ -275,182 +268,153 @@ public void testQueryCache() { } public void testGetMappingsIsFiltered() { - assertAcked(client().admin().indices().prepareCreate("test") - .setMapping("field1", "type=text", "field2", "type=text") - ); - client().prepareIndex("test").setId("1").setSource("field1", "value1") - .setRefreshPolicy(IMMEDIATE) - .get(); - client().prepareIndex("test").setId("2").setSource("field2", "value2") - .setRefreshPolicy(IMMEDIATE) - .get(); + assertAcked(client().admin().indices().prepareCreate("test").setMapping("field1", "type=text", "field2", "type=text")); + client().prepareIndex("test").setId("1").setSource("field1", "value1").setRefreshPolicy(IMMEDIATE).get(); + client().prepareIndex("test").setId("2").setSource("field2", "value2").setRefreshPolicy(IMMEDIATE).get(); { GetMappingsResponse getMappingsResponse = client().filterWithHeader( - Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .admin().indices().prepareGetMappings("test").get(); + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).admin().indices().prepareGetMappings("test").get(); assertExpectedFields(getMappingsResponse.getMappings(), "field1"); } { GetMappingsResponse getMappingsResponse = client().filterWithHeader( - Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .admin().indices().prepareGetMappings("test").get(); + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD)) + ).admin().indices().prepareGetMappings("test").get(); assertExpectedFields(getMappingsResponse.getMappings(), "field2"); } { GetMappingsResponse getMappingsResponse = client().filterWithHeader( - Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) - .admin().indices().prepareGetMappings("test").get(); + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD)) + ).admin().indices().prepareGetMappings("test").get(); assertExpectedFields(getMappingsResponse.getMappings(), "field1"); } { GetMappingsResponse getMappingsResponse = client().filterWithHeader( - Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user4", USERS_PASSWD))) - .admin().indices().prepareGetMappings("test").get(); + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user4", USERS_PASSWD)) + ).admin().indices().prepareGetMappings("test").get(); assertExpectedFields(getMappingsResponse.getMappings(), "field1", "field2"); } } public void testGetIndexMappingsIsFiltered() { - assertAcked(client().admin().indices().prepareCreate("test") - .setMapping("field1", "type=text", "field2", "type=text") - ); - client().prepareIndex("test").setId("1").setSource("field1", "value1") - .setRefreshPolicy(IMMEDIATE) - .get(); - client().prepareIndex("test").setId("2").setSource("field2", "value2") - .setRefreshPolicy(IMMEDIATE) - .get(); + assertAcked(client().admin().indices().prepareCreate("test").setMapping("field1", "type=text", "field2", "type=text")); + client().prepareIndex("test").setId("1").setSource("field1", "value1").setRefreshPolicy(IMMEDIATE).get(); + client().prepareIndex("test").setId("2").setSource("field2", "value2").setRefreshPolicy(IMMEDIATE).get(); { GetIndexResponse getIndexResponse = client().filterWithHeader( - Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .admin().indices().prepareGetIndex().setIndices("test").get(); + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).admin().indices().prepareGetIndex().setIndices("test").get(); assertExpectedFields(getIndexResponse.getMappings(), "field1"); } { GetIndexResponse getIndexResponse = client().filterWithHeader( - Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .admin().indices().prepareGetIndex().setIndices("test").get(); + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD)) + ).admin().indices().prepareGetIndex().setIndices("test").get(); assertExpectedFields(getIndexResponse.getMappings(), "field2"); } { GetIndexResponse getIndexResponse = client().filterWithHeader( - Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) - .admin().indices().prepareGetIndex().setIndices("test").get(); + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD)) + ).admin().indices().prepareGetIndex().setIndices("test").get(); assertExpectedFields(getIndexResponse.getMappings(), "field1"); } { GetIndexResponse getIndexResponse = client().filterWithHeader( - Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user4", USERS_PASSWD))) - .admin().indices().prepareGetIndex().setIndices("test").get(); + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user4", USERS_PASSWD)) + ).admin().indices().prepareGetIndex().setIndices("test").get(); assertExpectedFields(getIndexResponse.getMappings(), "field1", "field2"); } } public void testGetFieldMappingsIsFiltered() { - assertAcked(client().admin().indices().prepareCreate("test") - .setMapping("field1", "type=text", "field2", "type=text") - ); - client().prepareIndex("test").setId("1").setSource("field1", "value1") - .setRefreshPolicy(IMMEDIATE) - .get(); - client().prepareIndex("test").setId("2").setSource("field2", "value2") - .setRefreshPolicy(IMMEDIATE) - .get(); + assertAcked(client().admin().indices().prepareCreate("test").setMapping("field1", "type=text", "field2", "type=text")); + client().prepareIndex("test").setId("1").setSource("field1", "value1").setRefreshPolicy(IMMEDIATE).get(); + client().prepareIndex("test").setId("2").setSource("field2", "value2").setRefreshPolicy(IMMEDIATE).get(); { GetFieldMappingsResponse getFieldMappingsResponse = client().filterWithHeader( - Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .admin().indices().prepareGetFieldMappings("test").setFields("*").get(); + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).admin().indices().prepareGetFieldMappings("test").setFields("*").get(); - Map> mappings = - getFieldMappingsResponse.mappings(); + Map> mappings = getFieldMappingsResponse.mappings(); assertEquals(1, mappings.size()); assertExpectedFields(mappings.get("test"), "field1"); } { GetFieldMappingsResponse getFieldMappingsResponse = client().filterWithHeader( - Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .admin().indices().prepareGetFieldMappings("test").setFields("*").get(); + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD)) + ).admin().indices().prepareGetFieldMappings("test").setFields("*").get(); - Map> mappings = - getFieldMappingsResponse.mappings(); + Map> mappings = getFieldMappingsResponse.mappings(); assertEquals(1, mappings.size()); assertExpectedFields(mappings.get("test"), "field2"); } { GetFieldMappingsResponse getFieldMappingsResponse = client().filterWithHeader( - Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) - .admin().indices().prepareGetFieldMappings("test").setFields("*").get(); + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD)) + ).admin().indices().prepareGetFieldMappings("test").setFields("*").get(); - Map> mappings = - getFieldMappingsResponse.mappings(); + Map> mappings = getFieldMappingsResponse.mappings(); assertEquals(1, mappings.size()); assertExpectedFields(mappings.get("test"), "field1"); } { GetFieldMappingsResponse getFieldMappingsResponse = client().filterWithHeader( - Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user4", USERS_PASSWD))) - .admin().indices().prepareGetFieldMappings("test").setFields("*").get(); + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user4", USERS_PASSWD)) + ).admin().indices().prepareGetFieldMappings("test").setFields("*").get(); - Map> mappings = - getFieldMappingsResponse.mappings(); + Map> mappings = getFieldMappingsResponse.mappings(); assertEquals(1, mappings.size()); assertExpectedFields(mappings.get("test"), "field1", "field2"); } } public void testFieldCapabilitiesIsFiltered() { - assertAcked(client().admin().indices().prepareCreate("test") - .setMapping("field1", "type=text", "field2", "type=text") - ); - client().prepareIndex("test").setId("1").setSource("field1", "value1") - .setRefreshPolicy(IMMEDIATE) - .get(); - client().prepareIndex("test").setId("2").setSource("field2", "value2") - .setRefreshPolicy(IMMEDIATE) - .get(); + assertAcked(client().admin().indices().prepareCreate("test").setMapping("field1", "type=text", "field2", "type=text")); + client().prepareIndex("test").setId("1").setSource("field1", "value1").setRefreshPolicy(IMMEDIATE).get(); + client().prepareIndex("test").setId("2").setSource("field2", "value2").setRefreshPolicy(IMMEDIATE).get(); { FieldCapabilitiesRequest fieldCapabilitiesRequest = new FieldCapabilitiesRequest().fields("*").indices("test"); FieldCapabilitiesResponse response = client().filterWithHeader( - Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .fieldCaps(fieldCapabilitiesRequest).actionGet(); + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).fieldCaps(fieldCapabilitiesRequest).actionGet(); assertExpectedFields(response, "field1"); } { FieldCapabilitiesRequest fieldCapabilitiesRequest = new FieldCapabilitiesRequest().fields("*").indices("test"); FieldCapabilitiesResponse response = client().filterWithHeader( - Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .fieldCaps(fieldCapabilitiesRequest).actionGet(); + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD)) + ).fieldCaps(fieldCapabilitiesRequest).actionGet(); assertExpectedFields(response, "field2"); } { FieldCapabilitiesRequest fieldCapabilitiesRequest = new FieldCapabilitiesRequest().fields("*").indices("test"); FieldCapabilitiesResponse response = client().filterWithHeader( - Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) - .fieldCaps(fieldCapabilitiesRequest).actionGet(); + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD)) + ).fieldCaps(fieldCapabilitiesRequest).actionGet(); assertExpectedFields(response, "field1"); } { FieldCapabilitiesRequest fieldCapabilitiesRequest = new FieldCapabilitiesRequest().fields("*").indices("test"); FieldCapabilitiesResponse response = client().filterWithHeader( - Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user4", USERS_PASSWD))) - .fieldCaps(fieldCapabilitiesRequest).actionGet(); + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user4", USERS_PASSWD)) + ).fieldCaps(fieldCapabilitiesRequest).actionGet(); assertExpectedFields(response, "field1", "field2"); } } @SuppressWarnings("unchecked") - private static void assertExpectedFields(ImmutableOpenMap mappings, - String... fields) { + private static void assertExpectedFields(ImmutableOpenMap mappings, String... fields) { Map sourceAsMap = mappings.get("test").getSourceAsMap(); assertEquals(1, sourceAsMap.size()); - Map properties = (Map)sourceAsMap.get("properties"); + Map properties = (Map) sourceAsMap.get("properties"); assertEquals(fields.length, properties.size()); for (String field : fields) { assertNotNull(properties.get(field)); @@ -471,8 +435,7 @@ private static void assertExpectedFields(FieldCapabilitiesResponse fieldCapabili assertEquals("Some unexpected fields were returned: " + responseMap.keySet(), 0, responseMap.size()); } - private static void assertExpectedFields(Map actual, - String... expectedFields) { + private static void assertExpectedFields(Map actual, String... expectedFields) { Map fields = new HashMap<>(actual); for (String field : actual.keySet()) { // best effort to remove metadata fields diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/DocumentLevelSecurityRandomTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/DocumentLevelSecurityRandomTests.java index 3d0971f11d1e9..dd919819f9306 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/DocumentLevelSecurityRandomTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/DocumentLevelSecurityRandomTests.java @@ -12,17 +12,17 @@ import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.query.QueryBuilders; +import org.elasticsearch.test.SecurityIntegTestCase; import org.elasticsearch.test.SecuritySettingsSourceField; import org.elasticsearch.xpack.core.XPackSettings; -import org.elasticsearch.test.SecurityIntegTestCase; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken.BASIC_AUTH_HEADER; import static org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken.basicAuthHeaderValue; -import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.hamcrest.Matchers.equalTo; public class DocumentLevelSecurityRandomTests extends SecurityIntegTestCase { @@ -86,15 +86,13 @@ protected String configRoles() { @Override public Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { return Settings.builder() - .put(super.nodeSettings(nodeOrdinal, otherSettings)) - .put(XPackSettings.DLS_FLS_ENABLED.getKey(), true) - .build(); + .put(super.nodeSettings(nodeOrdinal, otherSettings)) + .put(XPackSettings.DLS_FLS_ENABLED.getKey(), true) + .build(); } public void testDuelWithAliasFilters() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test") - .setMapping("field1", "type=text", "field2", "type=text") - ); + assertAcked(client().admin().indices().prepareCreate("test").setMapping("field1", "type=text", "field2", "type=text")); List requests = new ArrayList<>(numberOfRoles); IndicesAliasesRequestBuilder builder = client().admin().indices().prepareAliases(); @@ -107,10 +105,9 @@ public void testDuelWithAliasFilters() throws Exception { builder.get(); for (int roleI = 1; roleI <= numberOfRoles; roleI++) { - SearchResponse searchResponse1 = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user" + roleI, USERS_PASSWD))) - .prepareSearch("test") - .get(); + SearchResponse searchResponse1 = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user" + roleI, USERS_PASSWD)) + ).prepareSearch("test").get(); SearchResponse searchResponse2 = client().prepareSearch("alias" + roleI).get(); assertThat(searchResponse1.getHits().getTotalHits().value, equalTo(searchResponse2.getHits().getTotalHits().value)); for (int hitI = 0; hitI < searchResponse1.getHits().getHits().length; hitI++) { diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/DocumentLevelSecurityTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/DocumentLevelSecurityTests.java index e4a84e745cf1a..be6db7edada74 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/DocumentLevelSecurityTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/DocumentLevelSecurityTests.java @@ -109,8 +109,15 @@ public class DocumentLevelSecurityTests extends SecurityIntegTestCase { @Override protected Collection> nodePlugins() { - return Arrays.asList(LocalStateSecurity.class, CommonAnalysisPlugin.class, ParentJoinPlugin.class, - InternalSettingsPlugin.class, DenseVectorPlugin.class, SpatialPlugin.class, PercolatorPlugin.class); + return Arrays.asList( + LocalStateSecurity.class, + CommonAnalysisPlugin.class, + ParentJoinPlugin.class, + InternalSettingsPlugin.class, + DenseVectorPlugin.class, + SpatialPlugin.class, + PercolatorPlugin.class + ); } @Override @@ -122,120 +129,127 @@ protected boolean addMockGeoShapeFieldMapper() { @Override protected String configUsers() { final String usersPasswdHashed = new String(getFastStoredHashAlgoForTests().hash(USERS_PASSWD)); - return super.configUsers() + - "user1:" + usersPasswdHashed + "\n" + - "user2:" + usersPasswdHashed + "\n" + - "user3:" + usersPasswdHashed + "\n" + - "user4:" + usersPasswdHashed + "\n" + - "user5:" + usersPasswdHashed + "\n"; + return super.configUsers() + + "user1:" + + usersPasswdHashed + + "\n" + + "user2:" + + usersPasswdHashed + + "\n" + + "user3:" + + usersPasswdHashed + + "\n" + + "user4:" + + usersPasswdHashed + + "\n" + + "user5:" + + usersPasswdHashed + + "\n"; } @Override protected String configUsersRoles() { - return super.configUsersRoles() + - "role1:user1,user2,user3\n" + - "role2:user1,user3\n" + - "role3:user2,user3\n" + - "role4:user4\n" + - "role5:user5\n"; + return super.configUsersRoles() + + "role1:user1,user2,user3\n" + + "role2:user1,user3\n" + + "role3:user2,user3\n" + + "role4:user4\n" + + "role5:user5\n"; } @Override protected String configRoles() { - return super.configRoles() + - "\nrole1:\n" + - " cluster: [ none ]\n" + - " indices:\n" + - " - names: '*'\n" + - " privileges: [ none ]\n" + - "\nrole2:\n" + - " cluster:\n" + - " - all\n" + - " indices:\n" + - " - names: '*'\n" + - " privileges:\n" + - " - all\n" + - " query: \n" + - " term: \n" + - " field1: value1\n" + - "role3:\n" + - " cluster: [ all ]\n" + - " indices:\n" + - " - names: '*'\n" + - " privileges: [ ALL ]\n" + - " query: '{\"term\" : {\"field2\" : \"value2\"}}'\n" + // <-- query defined as json in a string - "role4:\n" + - " cluster: [ all ]\n" + - " indices:\n" + - " - names: '*'\n" + - " privileges: [ ALL ]\n" + - // query that can match nested documents - " query: '{\"bool\": { \"must_not\": { \"term\" : {\"field1\" : \"value2\"}}}}'\n" + - "role5:\n" + - " cluster: [ all ]\n" + - " indices:\n" + - " - names: [ 'test' ]\n" + - " privileges: [ read ]\n" + - " query: '{\"term\" : {\"field2\" : \"value2\"}}'\n" + - " - names: [ 'fls-index' ]\n" + - " privileges: [ read ]\n" + - " field_security:\n" + - " grant: [ 'field1', 'other_field', 'suggest_field2' ]\n"; + return super.configRoles() + + "\nrole1:\n" + + " cluster: [ none ]\n" + + " indices:\n" + + " - names: '*'\n" + + " privileges: [ none ]\n" + + "\nrole2:\n" + + " cluster:\n" + + " - all\n" + + " indices:\n" + + " - names: '*'\n" + + " privileges:\n" + + " - all\n" + + " query: \n" + + " term: \n" + + " field1: value1\n" + + "role3:\n" + + " cluster: [ all ]\n" + + " indices:\n" + + " - names: '*'\n" + + " privileges: [ ALL ]\n" + + " query: '{\"term\" : {\"field2\" : \"value2\"}}'\n" + + // <-- query defined as json in a string + "role4:\n" + + " cluster: [ all ]\n" + + " indices:\n" + + " - names: '*'\n" + + " privileges: [ ALL ]\n" + + + // query that can match nested documents + " query: '{\"bool\": { \"must_not\": { \"term\" : {\"field1\" : \"value2\"}}}}'\n" + + "role5:\n" + + " cluster: [ all ]\n" + + " indices:\n" + + " - names: [ 'test' ]\n" + + " privileges: [ read ]\n" + + " query: '{\"term\" : {\"field2\" : \"value2\"}}'\n" + + " - names: [ 'fls-index' ]\n" + + " privileges: [ read ]\n" + + " field_security:\n" + + " grant: [ 'field1', 'other_field', 'suggest_field2' ]\n"; } @Override public Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { return Settings.builder() - .put(super.nodeSettings(nodeOrdinal, otherSettings)) - .put(XPackSettings.DLS_FLS_ENABLED.getKey(), true) - .put(XPackSettings.AUDIT_ENABLED.getKey(), false) // Just to make logs less noisy - .build(); + .put(super.nodeSettings(nodeOrdinal, otherSettings)) + .put(XPackSettings.DLS_FLS_ENABLED.getKey(), true) + .put(XPackSettings.AUDIT_ENABLED.getKey(), false) // Just to make logs less noisy + .build(); } public void testSimpleQuery() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test") - .setMapping("field1", "type=text", "field2", "type=text", "field3", "type=text") + assertAcked( + client().admin().indices().prepareCreate("test").setMapping("field1", "type=text", "field2", "type=text", "field3", "type=text") ); - client().prepareIndex("test").setId("1").setSource("field1", "value1") - .setRefreshPolicy(IMMEDIATE) - .get(); - client().prepareIndex("test").setId("2").setSource("field2", "value2") - .setRefreshPolicy(IMMEDIATE) - .get(); - client().prepareIndex("test").setId("3").setSource("field3", "value3") - .setRefreshPolicy(IMMEDIATE) - .get(); + client().prepareIndex("test").setId("1").setSource("field1", "value1").setRefreshPolicy(IMMEDIATE).get(); + client().prepareIndex("test").setId("2").setSource("field2", "value2").setRefreshPolicy(IMMEDIATE).get(); + client().prepareIndex("test").setId("3").setSource("field3", "value3").setRefreshPolicy(IMMEDIATE).get(); - SearchResponse response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(randomBoolean() ? QueryBuilders.termQuery("field1", "value1") : QueryBuilders.matchAllQuery()) - .get(); + SearchResponse response = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ) + .prepareSearch("test") + .setQuery(randomBoolean() ? QueryBuilders.termQuery("field1", "value1") : QueryBuilders.matchAllQuery()) + .get(); assertHitCount(response, 1); assertSearchHits(response, "1"); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(randomBoolean() ? QueryBuilders.termQuery("field2", "value2") : QueryBuilders.matchAllQuery()) - .get(); + .prepareSearch("test") + .setQuery(randomBoolean() ? QueryBuilders.termQuery("field2", "value2") : QueryBuilders.matchAllQuery()) + .get(); assertHitCount(response, 1); assertSearchHits(response, "2"); QueryBuilder combined = QueryBuilders.boolQuery() - .should(QueryBuilders.termQuery("field2", "value2")) - .should(QueryBuilders.termQuery("field1", "value1")) - .minimumShouldMatch(1); + .should(QueryBuilders.termQuery("field2", "value2")) + .should(QueryBuilders.termQuery("field1", "value1")) + .minimumShouldMatch(1); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(randomBoolean() ? combined : QueryBuilders.matchAllQuery()) - .get(); + .prepareSearch("test") + .setQuery(randomBoolean() ? combined : QueryBuilders.matchAllQuery()) + .get(); assertHitCount(response, 2); assertSearchHits(response, "1", "2"); } public void testGetApi() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test") - .setMapping("field1", "type=text", "field2", "type=text", "field3", "type=text") + assertAcked( + client().admin().indices().prepareCreate("test").setMapping("field1", "type=text", "field2", "type=text", "field3", "type=text") ); client().prepareIndex("test").setId("1").setSource("field1", "value1").get(); @@ -244,62 +258,62 @@ public void testGetApi() throws Exception { // test documents users can see boolean realtime = randomBoolean(); - GetResponse response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareGet("test", "1") - .setRealtime(realtime) - .setRefresh(true) - .get(); + GetResponse response = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).prepareGet("test", "1").setRealtime(realtime).setRefresh(true).get(); assertThat(response.isExists(), is(true)); assertThat(response.getId(), equalTo("1")); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareGet("test", "2") - .setRealtime(realtime) - .setRefresh(true) - .get(); + .prepareGet("test", "2") + .setRealtime(realtime) + .setRefresh(true) + .get(); assertThat(response.isExists(), is(true)); assertThat(response.getId(), equalTo("2")); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) - .prepareGet("test","1") - .setRealtime(realtime) - .setRefresh(true) - .get(); + .prepareGet("test", "1") + .setRealtime(realtime) + .setRefresh(true) + .get(); assertThat(response.isExists(), is(true)); assertThat(response.getId(), equalTo("1")); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) - .prepareGet("test", "2") - .setRealtime(realtime) - .setRefresh(true) - .get(); + .prepareGet("test", "2") + .setRealtime(realtime) + .setRefresh(true) + .get(); assertThat(response.isExists(), is(true)); assertThat(response.getId(), equalTo("2")); // test documents user cannot see response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareGet("test", "1") - .setRealtime(realtime) - .setRefresh(true) - .get(); + .prepareGet("test", "1") + .setRealtime(realtime) + .setRefresh(true) + .get(); assertThat(response.isExists(), is(false)); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareGet("test", "2") - .setRealtime(realtime) - .setRefresh(true) - .get(); + .prepareGet("test", "2") + .setRealtime(realtime) + .setRefresh(true) + .get(); assertThat(response.isExists(), is(false)); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) - .prepareGet("test", "3") - .setRealtime(realtime) - .setRefresh(true) - .get(); + .prepareGet("test", "3") + .setRealtime(realtime) + .setRefresh(true) + .get(); assertThat(response.isExists(), is(false)); } public void testRealtimeGetApi() { - assertAcked(client().admin().indices().prepareCreate("test") + assertAcked( + client().admin() + .indices() + .prepareCreate("test") .setMapping("field1", "type=text", "field2", "type=text", "field3", "type=text") .setSettings(Settings.builder().put("refresh_interval", "-1").build()) ); @@ -310,90 +324,69 @@ public void testRealtimeGetApi() { client().prepareIndex("test").setId("2").setSource("field2", "value2").get(); // do a realtime get beforehand to flip an internal translog flag so that subsequent realtime gets are // served from the translog (this first one is NOT, it internally forces a refresh of the index) - client().prepareMultiGet() - .add("test", "1") - .add("test", "2") - .setRealtime(realtime) - .setRefresh(refresh) - .get(); + client().prepareMultiGet().add("test", "1").add("test", "2").setRealtime(realtime).setRefresh(refresh).get(); refresh("test"); // updates don't change the doc visibility for users // but updates populate the translog and the DLS filter must apply to the translog operations as well if (randomBoolean()) { - client().prepareIndex("test").setId("1").setSource("field1", "value1", "field3", "value3") - .setRefreshPolicy(WriteRequest.RefreshPolicy.NONE).get(); - client().prepareIndex("test").setId("2").setSource("field2", "value2", "field3", "value3") - .setRefreshPolicy(WriteRequest.RefreshPolicy.NONE).get(); + client().prepareIndex("test") + .setId("1") + .setSource("field1", "value1", "field3", "value3") + .setRefreshPolicy(WriteRequest.RefreshPolicy.NONE) + .get(); + client().prepareIndex("test") + .setId("2") + .setSource("field2", "value2", "field3", "value3") + .setRefreshPolicy(WriteRequest.RefreshPolicy.NONE) + .get(); } else { - client().prepareUpdate("test", "1").setDoc(Map.of("field3", "value3")) - .setRefreshPolicy(WriteRequest.RefreshPolicy.NONE).get(); - client().prepareUpdate("test", "2").setDoc(Map.of("field3", "value3")) - .setRefreshPolicy(WriteRequest.RefreshPolicy.NONE).get(); + client().prepareUpdate("test", "1").setDoc(Map.of("field3", "value3")).setRefreshPolicy(WriteRequest.RefreshPolicy.NONE).get(); + client().prepareUpdate("test", "2").setDoc(Map.of("field3", "value3")).setRefreshPolicy(WriteRequest.RefreshPolicy.NONE).get(); } GetResponse getResponse; MultiGetResponse mgetResponse; // test documents user1 cannot see if (randomBoolean()) { - getResponse = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareGet("test", "2") - .setRealtime(realtime) - .setRefresh(refresh) - .get(); + getResponse = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).prepareGet("test", "2").setRealtime(realtime).setRefresh(refresh).get(); assertThat(getResponse.isExists(), is(false)); } else { - mgetResponse = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareMultiGet() - .add("test", "2") - .setRealtime(realtime) - .setRefresh(refresh) - .get(); + mgetResponse = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).prepareMultiGet().add("test", "2").setRealtime(realtime).setRefresh(refresh).get(); assertThat(mgetResponse.getResponses()[0].getResponse().isExists(), is(false)); } // test documents user2 cannot see if (randomBoolean()) { - getResponse = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareGet("test", "1") - .setRealtime(realtime) - .setRefresh(refresh) - .get(); + getResponse = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD)) + ).prepareGet("test", "1").setRealtime(realtime).setRefresh(refresh).get(); assertThat(getResponse.isExists(), is(false)); } else { - mgetResponse = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareMultiGet() - .add("test", "1") - .setRealtime(realtime) - .setRefresh(refresh) - .get(); + mgetResponse = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD)) + ).prepareMultiGet().add("test", "1").setRealtime(realtime).setRefresh(refresh).get(); assertThat(mgetResponse.getResponses()[0].getResponse().isExists(), is(false)); } // test visible documents are still visible after updates if (randomBoolean()) { - getResponse = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareGet("test", "1") - .setRealtime(realtime) - .setRefresh(refresh) - .get(); + getResponse = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).prepareGet("test", "1").setRealtime(realtime).setRefresh(refresh).get(); assertThat(getResponse.isExists(), is(true)); } else { - getResponse = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareGet("test", "2") - .setRealtime(realtime) - .setRefresh(refresh) - .get(); + getResponse = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD)) + ).prepareGet("test", "2").setRealtime(realtime).setRefresh(refresh).get(); assertThat(getResponse.isExists(), is(true)); } } public void testMGetApi() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test") - .setMapping("field1", "type=text", "field2", "type=text", "field3", "type=text") + assertAcked( + client().admin().indices().prepareCreate("test").setMapping("field1", "type=text", "field2", "type=text", "field3", "type=text") ); client().prepareIndex("test").setId("1").setSource("field1", "value1").get(); @@ -401,34 +394,30 @@ public void testMGetApi() throws Exception { client().prepareIndex("test").setId("3").setSource("field3", "value3").get(); boolean realtime = randomBoolean(); - MultiGetResponse response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareMultiGet() - .add("test", "1") - .setRealtime(realtime) - .setRefresh(true) - .get(); + MultiGetResponse response = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).prepareMultiGet().add("test", "1").setRealtime(realtime).setRefresh(true).get(); assertThat(response.getResponses()[0].isFailed(), is(false)); assertThat(response.getResponses()[0].getResponse().isExists(), is(true)); assertThat(response.getResponses()[0].getResponse().getId(), equalTo("1")); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareMultiGet() - .add("test", "2") - .setRealtime(realtime) - .setRefresh(true) - .get(); + .prepareMultiGet() + .add("test", "2") + .setRealtime(realtime) + .setRefresh(true) + .get(); assertThat(response.getResponses()[0].isFailed(), is(false)); assertThat(response.getResponses()[0].getResponse().isExists(), is(true)); assertThat(response.getResponses()[0].getResponse().getId(), equalTo("2")); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) - .prepareMultiGet() - .add("test", "1") - .add("test", "2") - .setRealtime(realtime) - .setRefresh(true) - .get(); + .prepareMultiGet() + .add("test", "1") + .add("test", "2") + .setRealtime(realtime) + .setRefresh(true) + .get(); assertThat(response.getResponses()[0].isFailed(), is(false)); assertThat(response.getResponses()[0].getResponse().isExists(), is(true)); assertThat(response.getResponses()[0].getResponse().getId(), equalTo("1")); @@ -437,38 +426,44 @@ public void testMGetApi() throws Exception { assertThat(response.getResponses()[1].getResponse().getId(), equalTo("2")); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareMultiGet() - .add("test", "1") - .setRealtime(realtime) - .setRefresh(true) - .get(); + .prepareMultiGet() + .add("test", "1") + .setRealtime(realtime) + .setRefresh(true) + .get(); assertThat(response.getResponses()[0].isFailed(), is(false)); assertThat(response.getResponses()[0].getResponse().isExists(), is(false)); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareMultiGet() - .add("test", "2") - .setRealtime(realtime) - .setRefresh(true) - .get(); + .prepareMultiGet() + .add("test", "2") + .setRealtime(realtime) + .setRefresh(true) + .get(); assertThat(response.getResponses()[0].isFailed(), is(false)); assertThat(response.getResponses()[0].getResponse().isExists(), is(false)); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) - .prepareMultiGet() - .add("test", "3") - .setRealtime(realtime) - .setRefresh(true) - .get(); + .prepareMultiGet() + .add("test", "3") + .setRealtime(realtime) + .setRefresh(true) + .get(); assertThat(response.getResponses()[0].isFailed(), is(false)); assertThat(response.getResponses()[0].getResponse().isExists(), is(false)); } public void testMSearch() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test1") + assertAcked( + client().admin() + .indices() + .prepareCreate("test1") .setMapping("field1", "type=text", "field2", "type=text", "field3", "type=text", "id", "type=integer") ); - assertAcked(client().admin().indices().prepareCreate("test2") + assertAcked( + client().admin() + .indices() + .prepareCreate("test2") .setMapping("field1", "type=text", "field2", "type=text", "field3", "type=text", "id", "type=integer") ); @@ -480,12 +475,13 @@ public void testMSearch() throws Exception { client().prepareIndex("test2").setId("3").setSource("field3", "value3", "id", 3).get(); client().admin().indices().prepareRefresh("test1", "test2").get(); - MultiSearchResponse response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareMultiSearch() - .add(client().prepareSearch("test1").setQuery(QueryBuilders.matchAllQuery())) - .add(client().prepareSearch("test2").setQuery(QueryBuilders.matchAllQuery())) - .get(); + MultiSearchResponse response = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ) + .prepareMultiSearch() + .add(client().prepareSearch("test1").setQuery(QueryBuilders.matchAllQuery())) + .add(client().prepareSearch("test2").setQuery(QueryBuilders.matchAllQuery())) + .get(); assertFalse(response.getResponses()[0].isFailure()); assertThat(response.getResponses()[0].getResponse().getHits().getTotalHits().value, is(1L)); assertThat(response.getResponses()[0].getResponse().getHits().getAt(0).getSourceAsMap().size(), is(2)); @@ -498,12 +494,11 @@ public void testMSearch() throws Exception { assertThat(response.getResponses()[1].getResponse().getHits().getAt(0).getSourceAsMap().get("field1"), is("value1")); assertThat(response.getResponses()[1].getResponse().getHits().getAt(0).getSourceAsMap().get("id"), is(1)); - response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareMultiSearch() - .add(client().prepareSearch("test1").setQuery(QueryBuilders.matchAllQuery())) - .add(client().prepareSearch("test2").setQuery(QueryBuilders.matchAllQuery())) - .get(); + response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) + .prepareMultiSearch() + .add(client().prepareSearch("test1").setQuery(QueryBuilders.matchAllQuery())) + .add(client().prepareSearch("test2").setQuery(QueryBuilders.matchAllQuery())) + .get(); assertFalse(response.getResponses()[0].isFailure()); assertThat(response.getResponses()[0].getResponse().getHits().getTotalHits().value, is(1L)); assertThat(response.getResponses()[0].getResponse().getHits().getAt(0).getSourceAsMap().size(), is(2)); @@ -516,14 +511,19 @@ public void testMSearch() throws Exception { assertThat(response.getResponses()[1].getResponse().getHits().getAt(0).getSourceAsMap().get("field2"), is("value2")); assertThat(response.getResponses()[1].getResponse().getHits().getAt(0).getSourceAsMap().get("id"), is(2)); - response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) - .prepareMultiSearch() - .add(client().prepareSearch("test1").addSort(SortBuilders.fieldSort("id").sortMode(SortMode.MIN)) - .setQuery(QueryBuilders.matchAllQuery())) - .add(client().prepareSearch("test2").addSort(SortBuilders.fieldSort("id").sortMode(SortMode.MIN)) - .setQuery(QueryBuilders.matchAllQuery())) - .get(); + response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) + .prepareMultiSearch() + .add( + client().prepareSearch("test1") + .addSort(SortBuilders.fieldSort("id").sortMode(SortMode.MIN)) + .setQuery(QueryBuilders.matchAllQuery()) + ) + .add( + client().prepareSearch("test2") + .addSort(SortBuilders.fieldSort("id").sortMode(SortMode.MIN)) + .setQuery(QueryBuilders.matchAllQuery()) + ) + .get(); assertFalse(response.getResponses()[0].isFailure()); assertThat(response.getResponses()[0].getResponse().getHits().getTotalHits().value, is(2L)); assertThat(response.getResponses()[0].getResponse().getHits().getAt(0).getSourceAsMap().size(), is(2)); @@ -544,75 +544,89 @@ public void testMSearch() throws Exception { } public void testPercolateQueryWithIndexedDocWithDLS() { - assertAcked(client().admin().indices().prepareCreate("query_index") + assertAcked( + client().admin() + .indices() + .prepareCreate("query_index") .setMapping("message", "type=text", "query", "type=percolator", "field1", "type=text", "field2", "type=text") ); - assertAcked(client().admin().indices().prepareCreate("doc_index") - .setMapping("message", "type=text", "field1", "type=text") - ); - client().prepareIndex("query_index").setId("1") - .setSource("{\"field1\": \"value1\", \"field2\": \"value2\", \"query\": " + - "{\"match\": {\"message\": \"bonsai tree\"}}}", - XContentType.JSON) - .setRefreshPolicy(IMMEDIATE).get(); - client().prepareIndex("doc_index").setId("1") - .setSource("{\"field1\": \"value1\", \"message\": \"A new bonsai tree in the office\"}", - XContentType.JSON) - .setRefreshPolicy(IMMEDIATE).get(); + assertAcked(client().admin().indices().prepareCreate("doc_index").setMapping("message", "type=text", "field1", "type=text")); + client().prepareIndex("query_index") + .setId("1") + .setSource( + "{\"field1\": \"value1\", \"field2\": \"value2\", \"query\": " + "{\"match\": {\"message\": \"bonsai tree\"}}}", + XContentType.JSON + ) + .setRefreshPolicy(IMMEDIATE) + .get(); + client().prepareIndex("doc_index") + .setId("1") + .setSource("{\"field1\": \"value1\", \"message\": \"A new bonsai tree in the office\"}", XContentType.JSON) + .setRefreshPolicy(IMMEDIATE) + .get(); // user1 can preform the percolate search for doc#1 in the doc_index because user1 has access to the doc - SearchResponse result = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareSearch("query_index") - .setQuery(new PercolateQueryBuilder("query", "doc_index", "1", null, null, null)) - .get(); + SearchResponse result = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).prepareSearch("query_index").setQuery(new PercolateQueryBuilder("query", "doc_index", "1", null, null, null)).get(); assertSearchResponse(result); assertHitCount(result, 1); // user2 can access the query_index itself (without performing percolate search) - result = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareSearch("query_index") - .setQuery(QueryBuilders.matchAllQuery()) - .get(); + result = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) + .prepareSearch("query_index") + .setQuery(QueryBuilders.matchAllQuery()) + .get(); assertSearchResponse(result); assertHitCount(result, 1); // user2 cannot access doc#1 of the doc_index so the percolate search fails because doc#1 cannot be found - ResourceNotFoundException e = expectThrows(ResourceNotFoundException.class, () -> client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) + ResourceNotFoundException e = expectThrows( + ResourceNotFoundException.class, + () -> client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) .prepareSearch("query_index") .setQuery(new PercolateQueryBuilder("query", "doc_index", "1", null, null, null)) - .get()); + .get() + ); assertThat(e.getMessage(), is("indexed document [doc_index/1] couldn't be found")); } public void testGeoQueryWithIndexedShapeWithDLS() { - assertAcked(client().admin().indices().prepareCreate("search_index") + assertAcked( + client().admin() + .indices() + .prepareCreate("search_index") .setMapping("search_field", "type=shape", "field1", "type=text", "field2", "type=text") ); - assertAcked(client().admin().indices().prepareCreate("shape_index") + assertAcked( + client().admin() + .indices() + .prepareCreate("shape_index") .setMapping("shape_field", "type=shape", "field1", "type=text", "field2", "type=text") ); - client().prepareIndex("search_index").setId("1") - .setSource("{\"field1\": \"value1\", \"field2\": \"value2\", \"search_field\": " + - "{ \"type\": \"point\", \"coordinates\":[1, 1] }}", - XContentType.JSON) - .setRefreshPolicy(IMMEDIATE).get(); - client().prepareIndex("shape_index").setId("1") - .setSource("{\"field1\": \"value1\", \"shape_field\": " + - "{ \"type\": \"envelope\", \"coordinates\": [[0, 2], [2, 0]]}}", - XContentType.JSON) - .setRefreshPolicy(IMMEDIATE).get(); - ShapeQueryBuilder shapeQuery = new ShapeQueryBuilder("search_field", "1") - .relation(ShapeRelation.WITHIN) - .indexedShapeIndex("shape_index") - .indexedShapePath("shape_field"); + client().prepareIndex("search_index") + .setId("1") + .setSource( + "{\"field1\": \"value1\", \"field2\": \"value2\", \"search_field\": " + "{ \"type\": \"point\", \"coordinates\":[1, 1] }}", + XContentType.JSON + ) + .setRefreshPolicy(IMMEDIATE) + .get(); + client().prepareIndex("shape_index") + .setId("1") + .setSource( + "{\"field1\": \"value1\", \"shape_field\": " + "{ \"type\": \"envelope\", \"coordinates\": [[0, 2], [2, 0]]}}", + XContentType.JSON + ) + .setRefreshPolicy(IMMEDIATE) + .get(); + ShapeQueryBuilder shapeQuery = new ShapeQueryBuilder("search_field", "1").relation(ShapeRelation.WITHIN) + .indexedShapeIndex("shape_index") + .indexedShapePath("shape_field"); SearchResponse result; // user1 has access to doc#1 of the shape_index so everything works - SearchRequestBuilder requestBuilder = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareSearch("search_index"); + SearchRequestBuilder requestBuilder = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).prepareSearch("search_index"); if (randomBoolean()) { - requestBuilder.setQuery(QueryBuilders.matchAllQuery()) - .setPostFilter(shapeQuery); + requestBuilder.setQuery(QueryBuilders.matchAllQuery()).setPostFilter(shapeQuery); } else { requestBuilder.setQuery(shapeQuery); } @@ -620,216 +634,229 @@ public void testGeoQueryWithIndexedShapeWithDLS() { assertSearchResponse(result); assertHitCount(result, 1); // user2 does not have access to doc#1 of the shape_index - result = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareSearch("search_index") - .setQuery(QueryBuilders.matchAllQuery()) - .get(); + result = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) + .prepareSearch("search_index") + .setQuery(QueryBuilders.matchAllQuery()) + .get(); assertSearchResponse(result); assertHitCount(result, 1); IllegalArgumentException e; if (randomBoolean()) { - e = expectThrows(IllegalArgumentException.class, () -> client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) + e = expectThrows( + IllegalArgumentException.class, + () -> client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) .prepareSearch("search_index") .setQuery(shapeQuery) - .get()); + .get() + ); } else { - e = expectThrows(IllegalArgumentException.class, () -> client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) + e = expectThrows( + IllegalArgumentException.class, + () -> client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) .prepareSearch("search_index") .setQuery(QueryBuilders.matchAllQuery()) .setPostFilter(shapeQuery) - .get()); + .get() + ); } assertThat(e.getMessage(), is("Shape with ID [1] not found")); } public void testTermsLookupOnIndexWithDLS() { - assertAcked(client().admin().indices().prepareCreate("search_index") + assertAcked( + client().admin() + .indices() + .prepareCreate("search_index") .setMapping("search_field", "type=keyword", "field1", "type=text", "field2", "type=text") ); - assertAcked(client().admin().indices().prepareCreate("lookup_index") + assertAcked( + client().admin() + .indices() + .prepareCreate("lookup_index") .setMapping("lookup_field", "type=keyword", "field1", "type=text", "field2", "type=text") ); - client().prepareIndex("search_index").setId("1").setSource("field1", "value1", "search_field", - List.of("value1", "value2", "value3")) - .setRefreshPolicy(IMMEDIATE) - .get(); - client().prepareIndex("search_index").setId("2").setSource("field1", "value1", "field2", "value2", "search_field", - List.of("value1", "value2")) - .setRefreshPolicy(IMMEDIATE) - .get(); - client().prepareIndex("search_index").setId("3").setSource("field1", "value1", "field2", "value1", "search_field", "value1") - .setRefreshPolicy(IMMEDIATE) - .get(); - client().prepareIndex("search_index").setId("4").setSource("field2", "value2", "search_field", "value1") - .setRefreshPolicy(IMMEDIATE) - .get(); - client().prepareIndex("search_index").setId("5").setSource("field2", "value2", "search_field", List.of("value1", "value2")) - .setRefreshPolicy(IMMEDIATE) - .get(); - client().prepareIndex("lookup_index").setId("1").setSource("field1", "value1", "field2", "value1", "lookup_field", - List.of("value1", "value2")) - .setRefreshPolicy(IMMEDIATE) - .get(); - client().prepareIndex("lookup_index").setId("2").setSource("field1", "value2", "field2", "value2", "lookup_field", - List.of("value2")) - .setRefreshPolicy(IMMEDIATE) - .get(); + client().prepareIndex("search_index") + .setId("1") + .setSource("field1", "value1", "search_field", List.of("value1", "value2", "value3")) + .setRefreshPolicy(IMMEDIATE) + .get(); + client().prepareIndex("search_index") + .setId("2") + .setSource("field1", "value1", "field2", "value2", "search_field", List.of("value1", "value2")) + .setRefreshPolicy(IMMEDIATE) + .get(); + client().prepareIndex("search_index") + .setId("3") + .setSource("field1", "value1", "field2", "value1", "search_field", "value1") + .setRefreshPolicy(IMMEDIATE) + .get(); + client().prepareIndex("search_index") + .setId("4") + .setSource("field2", "value2", "search_field", "value1") + .setRefreshPolicy(IMMEDIATE) + .get(); + client().prepareIndex("search_index") + .setId("5") + .setSource("field2", "value2", "search_field", List.of("value1", "value2")) + .setRefreshPolicy(IMMEDIATE) + .get(); + client().prepareIndex("lookup_index") + .setId("1") + .setSource("field1", "value1", "field2", "value1", "lookup_field", List.of("value1", "value2")) + .setRefreshPolicy(IMMEDIATE) + .get(); + client().prepareIndex("lookup_index") + .setId("2") + .setSource("field1", "value2", "field2", "value2", "lookup_field", List.of("value2")) + .setRefreshPolicy(IMMEDIATE) + .get(); // Lookup doc#1 is: visible to user1 and user3, but hidden from user2 TermsQueryBuilder lookup = QueryBuilders.termsLookupQuery("search_field", new TermsLookup("lookup_index", "1", "lookup_field")); - SearchResponse response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareSearch("search_index") - .setQuery(lookup) - .get(); + SearchResponse response = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).prepareSearch("search_index").setQuery(lookup).get(); assertHitCount(response, 3); assertSearchHits(response, "1", "2", "3"); - response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareSearch("search_index") - .setQuery(lookup) - .get(); + response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) + .prepareSearch("search_index") + .setQuery(lookup) + .get(); assertHitCount(response, 0); - response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) - .prepareSearch("search_index") - .setQuery(lookup) - .get(); + response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) + .prepareSearch("search_index") + .setQuery(lookup) + .get(); assertHitCount(response, 5); assertSearchHits(response, "1", "2", "3", "4", "5"); // Lookup doc#2 is: hidden from user1, visible to user2 and user3 lookup = QueryBuilders.termsLookupQuery("search_field", new TermsLookup("lookup_index", "2", "lookup_field")); - response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareSearch("search_index") - .setQuery(lookup) - .get(); + response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) + .prepareSearch("search_index") + .setQuery(lookup) + .get(); assertHitCount(response, 0); - response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareSearch("search_index") - .setQuery(lookup) - .get(); + response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) + .prepareSearch("search_index") + .setQuery(lookup) + .get(); assertHitCount(response, 2); assertSearchHits(response, "2", "5"); - response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) - .prepareSearch("search_index") - .setQuery(lookup) - .get(); + response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) + .prepareSearch("search_index") + .setQuery(lookup) + .get(); assertHitCount(response, 3); assertSearchHits(response, "1", "2", "5"); } public void testTVApi() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test") - .setMapping("field1", "type=text,term_vector=with_positions_offsets_payloads", - "field2", "type=text,term_vector=with_positions_offsets_payloads", - "field3", "type=text,term_vector=with_positions_offsets_payloads") + assertAcked( + client().admin() + .indices() + .prepareCreate("test") + .setMapping( + "field1", + "type=text,term_vector=with_positions_offsets_payloads", + "field2", + "type=text,term_vector=with_positions_offsets_payloads", + "field3", + "type=text,term_vector=with_positions_offsets_payloads" + ) ); - client().prepareIndex("test").setId("1").setSource("field1", "value1") - .setRefreshPolicy(IMMEDIATE) - .get(); - client().prepareIndex("test").setId("2").setSource("field2", "value2") - .setRefreshPolicy(IMMEDIATE) - .get(); - client().prepareIndex("test").setId("3").setSource("field3", "value3") - .setRefreshPolicy(IMMEDIATE) - .get(); + client().prepareIndex("test").setId("1").setSource("field1", "value1").setRefreshPolicy(IMMEDIATE).get(); + client().prepareIndex("test").setId("2").setSource("field2", "value2").setRefreshPolicy(IMMEDIATE).get(); + client().prepareIndex("test").setId("3").setSource("field3", "value3").setRefreshPolicy(IMMEDIATE).get(); boolean realtime = randomBoolean(); - TermVectorsResponse response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareTermVectors("test", "1") - .setRealtime(realtime) - .get(); + TermVectorsResponse response = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).prepareTermVectors("test", "1").setRealtime(realtime).get(); assertThat(response.isExists(), is(true)); assertThat(response.getId(), is("1")); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareTermVectors("test", "2") - .setRealtime(realtime) - .get(); + .prepareTermVectors("test", "2") + .setRealtime(realtime) + .get(); assertThat(response.isExists(), is(true)); assertThat(response.getId(), is("2")); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) - .prepareTermVectors("test", "1") - .setRealtime(realtime) - .get(); + .prepareTermVectors("test", "1") + .setRealtime(realtime) + .get(); assertThat(response.isExists(), is(true)); assertThat(response.getId(), is("1")); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) - .prepareTermVectors("test", "2") - .setRealtime(realtime) - .get(); + .prepareTermVectors("test", "2") + .setRealtime(realtime) + .get(); assertThat(response.isExists(), is(true)); assertThat(response.getId(), is("2")); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareTermVectors("test", "1") - .setRealtime(realtime) - .get(); + .prepareTermVectors("test", "1") + .setRealtime(realtime) + .get(); assertThat(response.isExists(), is(false)); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareTermVectors("test", "2") - .setRealtime(realtime) - .get(); + .prepareTermVectors("test", "2") + .setRealtime(realtime) + .get(); assertThat(response.isExists(), is(false)); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) - .prepareTermVectors("test", "3") - .setRealtime(realtime) - .get(); + .prepareTermVectors("test", "3") + .setRealtime(realtime) + .get(); assertThat(response.isExists(), is(false)); } public void testMTVApi() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test") - .setMapping("field1", "type=text,term_vector=with_positions_offsets_payloads", - "field2", "type=text,term_vector=with_positions_offsets_payloads", - "field3", "type=text,term_vector=with_positions_offsets_payloads") + assertAcked( + client().admin() + .indices() + .prepareCreate("test") + .setMapping( + "field1", + "type=text,term_vector=with_positions_offsets_payloads", + "field2", + "type=text,term_vector=with_positions_offsets_payloads", + "field3", + "type=text,term_vector=with_positions_offsets_payloads" + ) ); - client().prepareIndex("test").setId("1").setSource("field1", "value1") - .setRefreshPolicy(IMMEDIATE) - .get(); - client().prepareIndex("test").setId("2").setSource("field2", "value2") - .setRefreshPolicy(IMMEDIATE) - .get(); - client().prepareIndex("test").setId("3").setSource("field3", "value3") - .setRefreshPolicy(IMMEDIATE) - .get(); + client().prepareIndex("test").setId("1").setSource("field1", "value1").setRefreshPolicy(IMMEDIATE).get(); + client().prepareIndex("test").setId("2").setSource("field2", "value2").setRefreshPolicy(IMMEDIATE).get(); + client().prepareIndex("test").setId("3").setSource("field3", "value3").setRefreshPolicy(IMMEDIATE).get(); boolean realtime = randomBoolean(); - MultiTermVectorsResponse response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareMultiTermVectors() - .add(new TermVectorsRequest("test", "1").realtime(realtime)) - .get(); + MultiTermVectorsResponse response = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).prepareMultiTermVectors().add(new TermVectorsRequest("test", "1").realtime(realtime)).get(); assertThat(response.getResponses().length, equalTo(1)); assertThat(response.getResponses()[0].getResponse().isExists(), is(true)); assertThat(response.getResponses()[0].getResponse().getId(), is("1")); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareMultiTermVectors() - .add(new TermVectorsRequest("test", "2").realtime(realtime)) - .get(); + .prepareMultiTermVectors() + .add(new TermVectorsRequest("test", "2").realtime(realtime)) + .get(); assertThat(response.getResponses().length, equalTo(1)); assertThat(response.getResponses()[0].getResponse().isExists(), is(true)); assertThat(response.getResponses()[0].getResponse().getId(), is("2")); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) - .prepareMultiTermVectors() - .add(new TermVectorsRequest("test", "1").realtime(realtime)).add(new TermVectorsRequest("test", "2").realtime(realtime)) - .get(); + .prepareMultiTermVectors() + .add(new TermVectorsRequest("test", "1").realtime(realtime)) + .add(new TermVectorsRequest("test", "2").realtime(realtime)) + .get(); assertThat(response.getResponses().length, equalTo(2)); assertThat(response.getResponses()[0].getResponse().isExists(), is(true)); assertThat(response.getResponses()[0].getResponse().getId(), is("1")); @@ -837,67 +864,58 @@ public void testMTVApi() throws Exception { assertThat(response.getResponses()[1].getResponse().getId(), is("2")); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareMultiTermVectors() - .add(new TermVectorsRequest("test", "1").realtime(realtime)) - .get(); + .prepareMultiTermVectors() + .add(new TermVectorsRequest("test", "1").realtime(realtime)) + .get(); assertThat(response.getResponses().length, equalTo(1)); assertThat(response.getResponses()[0].getResponse().isExists(), is(false)); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareMultiTermVectors() - .add(new TermVectorsRequest("test", "2").realtime(realtime)) - .get(); + .prepareMultiTermVectors() + .add(new TermVectorsRequest("test", "2").realtime(realtime)) + .get(); assertThat(response.getResponses().length, equalTo(1)); assertThat(response.getResponses()[0].getResponse().isExists(), is(false)); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) - .prepareMultiTermVectors() - .add(new TermVectorsRequest("test", "3").realtime(realtime)) - .get(); + .prepareMultiTermVectors() + .add(new TermVectorsRequest("test", "3").realtime(realtime)) + .get(); assertThat(response.getResponses().length, equalTo(1)); assertThat(response.getResponses()[0].getResponse().isExists(), is(false)); } public void testKnnSearch() throws Exception { - Settings indexSettings = Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .build(); - XContentBuilder builder = XContentFactory.jsonBuilder().startObject().startObject("properties") - .startObject("vector") - .field("type", "dense_vector") - .field("dims", 3) - .field("index", true) - .field("similarity", "l2_norm") - .endObject() - .endObject().endObject(); - assertAcked(client().admin().indices().prepareCreate("test") - .setSettings(indexSettings) - .setMapping(builder)); + Settings indexSettings = Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).build(); + XContentBuilder builder = XContentFactory.jsonBuilder() + .startObject() + .startObject("properties") + .startObject("vector") + .field("type", "dense_vector") + .field("dims", 3) + .field("index", true) + .field("similarity", "l2_norm") + .endObject() + .endObject() + .endObject(); + assertAcked(client().admin().indices().prepareCreate("test").setSettings(indexSettings).setMapping(builder)); for (int i = 0; i < 5; i++) { - client().prepareIndex("test") - .setSource("field1", "value1", "vector", new float[]{i, i, i}) - .get(); - client().prepareIndex("test") - .setSource("field2", "value2", "vector", new float[]{i, i, i}) - .get(); + client().prepareIndex("test").setSource("field1", "value1", "vector", new float[] { i, i, i }).get(); + client().prepareIndex("test").setSource("field2", "value2", "vector", new float[] { i, i, i }).get(); } client().admin().indices().prepareRefresh("test").get(); // Since there's no kNN search action at the transport layer, we just emulate // how the action works (it builds a kNN query under the hood) - float[] queryVector = new float[]{0.0f, 0.0f, 0.0f}; + float[] queryVector = new float[] { 0.0f, 0.0f, 0.0f }; KnnVectorQueryBuilder query = new KnnVectorQueryBuilder("vector", queryVector, 50); // user1 should only be able to see docs with field1: value1 - SearchResponse response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(query) - .addFetchField("field1") - .setSize(10) - .get(); + SearchResponse response = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).prepareSearch("test").setQuery(query).addFetchField("field1").setSize(10).get(); assertEquals(5, response.getHits().getTotalHits().value); assertEquals(5, response.getHits().getHits().length); for (SearchHit hit : response.getHits().getHits()) { @@ -905,8 +923,7 @@ public void testKnnSearch() throws Exception { } // user2 should only be able to see docs with field2: value2 - response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) + response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) .prepareSearch("test") .setQuery(query) .addFetchField("field2") @@ -919,8 +936,7 @@ public void testKnnSearch() throws Exception { } // user3 can see all indexed docs - response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) + response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) .prepareSearch("test") .setQuery(query) .setSize(10) @@ -930,22 +946,19 @@ public void testKnnSearch() throws Exception { } public void testGlobalAggregation() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test") - .setMapping("field1", "type=text", "field2", "type=text,fielddata=true", "field3", "type=text") + assertAcked( + client().admin() + .indices() + .prepareCreate("test") + .setMapping("field1", "type=text", "field2", "type=text,fielddata=true", "field3", "type=text") ); - client().prepareIndex("test").setId("1").setSource("field1", "value1") - .setRefreshPolicy(IMMEDIATE) - .get(); - client().prepareIndex("test").setId("2").setSource("field2", "value2") - .setRefreshPolicy(IMMEDIATE) - .get(); - client().prepareIndex("test").setId("3").setSource("field3", "value3") - .setRefreshPolicy(IMMEDIATE) - .get(); + client().prepareIndex("test").setId("1").setSource("field1", "value1").setRefreshPolicy(IMMEDIATE).get(); + client().prepareIndex("test").setId("2").setSource("field2", "value2").setRefreshPolicy(IMMEDIATE).get(); + client().prepareIndex("test").setId("3").setSource("field3", "value3").setRefreshPolicy(IMMEDIATE).get(); SearchResponse response = client().prepareSearch("test") - .addAggregation(AggregationBuilders.global("global").subAggregation(AggregationBuilders.terms("field2").field("field2"))) - .get(); + .addAggregation(AggregationBuilders.global("global").subAggregation(AggregationBuilders.terms("field2").field("field2"))) + .get(); assertHitCount(response, 3); assertSearchHits(response, "1", "2", "3"); @@ -956,9 +969,9 @@ public void testGlobalAggregation() throws Exception { assertThat(termsAgg.getBuckets().get(0).getDocCount(), equalTo(1L)); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareSearch("test") - .addAggregation(AggregationBuilders.global("global").subAggregation(AggregationBuilders.terms("field2").field("field2"))) - .get(); + .prepareSearch("test") + .addAggregation(AggregationBuilders.global("global").subAggregation(AggregationBuilders.terms("field2").field("field2"))) + .get(); assertHitCount(response, 1); assertSearchHits(response, "1"); @@ -968,9 +981,9 @@ public void testGlobalAggregation() throws Exception { assertThat(termsAgg.getBuckets().size(), equalTo(0)); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareSearch("test") - .addAggregation(AggregationBuilders.global("global").subAggregation(AggregationBuilders.terms("field2").field("field2"))) - .get(); + .prepareSearch("test") + .addAggregation(AggregationBuilders.global("global").subAggregation(AggregationBuilders.terms("field2").field("field2"))) + .get(); assertHitCount(response, 1); assertSearchHits(response, "2"); @@ -980,9 +993,9 @@ public void testGlobalAggregation() throws Exception { assertThat(termsAgg.getBuckets().size(), equalTo(1)); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) - .prepareSearch("test") - .addAggregation(AggregationBuilders.global("global").subAggregation(AggregationBuilders.terms("field2").field("field2"))) - .get(); + .prepareSearch("test") + .addAggregation(AggregationBuilders.global("global").subAggregation(AggregationBuilders.terms("field2").field("field2"))) + .get(); assertHitCount(response, 2); assertSearchHits(response, "1", "2"); @@ -994,29 +1007,28 @@ public void testGlobalAggregation() throws Exception { public void testParentChild() throws Exception { XContentBuilder mapping = jsonBuilder().startObject() - .startObject("properties") - .startObject("id") - .field("type", "keyword") - .endObject() - .startObject("join_field") - .field("type", "join") - .startObject("relations") - .field("parent", "child") - .endObject() - .endObject() - .startObject("field1") - .field("type", "text") - .endObject() - .startObject("field2") - .field("type", "text") - .endObject() - .startObject("field3") - .field("type", "text") - .endObject() - .endObject() - .endObject(); - assertAcked(prepareCreate("test") - .setMapping(mapping)); + .startObject("properties") + .startObject("id") + .field("type", "keyword") + .endObject() + .startObject("join_field") + .field("type", "join") + .startObject("relations") + .field("parent", "child") + .endObject() + .endObject() + .startObject("field1") + .field("type", "text") + .endObject() + .startObject("field2") + .field("type", "text") + .endObject() + .startObject("field3") + .field("type", "text") + .endObject() + .endObject() + .endObject(); + assertAcked(prepareCreate("test").setMapping(mapping)); ensureGreen(); // index simple data @@ -1043,15 +1055,15 @@ public void testParentChild() throws Exception { private void verifyParentChild() { SearchResponse searchResponse = client().prepareSearch("test") - .setQuery(hasChildQuery("child", matchAllQuery(), ScoreMode.None)) - .get(); + .setQuery(hasChildQuery("child", matchAllQuery(), ScoreMode.None)) + .get(); assertHitCount(searchResponse, 1L); assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("p1")); searchResponse = client().prepareSearch("test") - .setQuery(hasParentQuery("parent", matchAllQuery(), false)) - .addSort("id", SortOrder.ASC) - .get(); + .setQuery(hasParentQuery("parent", matchAllQuery(), false)) + .addSort("id", SortOrder.ASC) + .get(); assertHitCount(searchResponse, 3L); assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("c1")); assertThat(searchResponse.getHits().getAt(1).getId(), equalTo("c2")); @@ -1059,48 +1071,51 @@ private void verifyParentChild() { // Both user1 and user2 can't see field1 and field2, no parent/child query should yield results: searchResponse = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(hasChildQuery("child", matchAllQuery(), ScoreMode.None)) - .get(); + .prepareSearch("test") + .setQuery(hasChildQuery("child", matchAllQuery(), ScoreMode.None)) + .get(); assertHitCount(searchResponse, 0L); searchResponse = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(hasChildQuery("child", matchAllQuery(), ScoreMode.None)) - .get(); + .prepareSearch("test") + .setQuery(hasChildQuery("child", matchAllQuery(), ScoreMode.None)) + .get(); assertHitCount(searchResponse, 0L); searchResponse = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(hasParentQuery("parent", matchAllQuery(), false)) - .get(); + .prepareSearch("test") + .setQuery(hasParentQuery("parent", matchAllQuery(), false)) + .get(); assertHitCount(searchResponse, 0L); searchResponse = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(hasParentQuery("parent", matchAllQuery(), false)) - .get(); + .prepareSearch("test") + .setQuery(hasParentQuery("parent", matchAllQuery(), false)) + .get(); assertHitCount(searchResponse, 0L); // user 3 can see them but not c3 searchResponse = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(hasChildQuery("child", matchAllQuery(), ScoreMode.None)) - .get(); + .prepareSearch("test") + .setQuery(hasChildQuery("child", matchAllQuery(), ScoreMode.None)) + .get(); assertHitCount(searchResponse, 1L); assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("p1")); searchResponse = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(hasParentQuery("parent", matchAllQuery(), false)) - .get(); + .prepareSearch("test") + .setQuery(hasParentQuery("parent", matchAllQuery(), false)) + .get(); assertHitCount(searchResponse, 2L); assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("c1")); assertThat(searchResponse.getHits().getAt(1).getId(), equalTo("c2")); } public void testScroll() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test") + assertAcked( + client().admin() + .indices() + .prepareCreate("test") .setSettings(Settings.builder().put(IndicesRequestCache.INDEX_CACHE_REQUEST_ENABLED_SETTING.getKey(), true)) .setMapping("field1", "type=text", "field2", "type=text", "field3", "type=text") ); @@ -1119,13 +1134,12 @@ public void testScroll() throws Exception { SearchResponse response = null; try { - response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareSearch("test") - .setSize(1) - .setScroll(TimeValue.timeValueMinutes(1L)) - .setQuery(termQuery("field1", "value1")) - .get(); + response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) + .prepareSearch("test") + .setSize(1) + .setScroll(TimeValue.timeValueMinutes(1L)) + .setQuery(termQuery("field1", "value1")) + .get(); do { assertNoFailures(response); assertThat(response.getHits().getTotalHits().value, is((long) numVisible)); @@ -1136,11 +1150,9 @@ public void testScroll() throws Exception { break; } - response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareSearchScroll(response.getScrollId()) - .setScroll(TimeValue.timeValueMinutes(1L)) - .get(); + response = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).prepareSearchScroll(response.getScrollId()).setScroll(TimeValue.timeValueMinutes(1L)).get(); } while (response.getHits().getHits().length > 0); } finally { if (response != null) { @@ -1153,9 +1165,12 @@ public void testScroll() throws Exception { } public void testReaderId() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test") - .setSettings(Settings.builder().put(IndicesRequestCache.INDEX_CACHE_REQUEST_ENABLED_SETTING.getKey(), true)) - .setMapping("field1", "type=text", "field2", "type=text", "field3", "type=text") + assertAcked( + client().admin() + .indices() + .prepareCreate("test") + .setSettings(Settings.builder().put(IndicesRequestCache.INDEX_CACHE_REQUEST_ENABLED_SETTING.getKey(), true)) + .setMapping("field1", "type=text", "field2", "type=text", "field3", "type=text") ); final int numVisible = scaledRandomIntBetween(2, 10); final int numInvisible = scaledRandomIntBetween(2, 10); @@ -1174,8 +1189,9 @@ public void testReaderId() throws Exception { SearchResponse response = null; try { for (int from = 0; from < numVisible; from++) { - response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) + response = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ) .prepareSearch() .setSize(1) .setFrom(from) @@ -1193,62 +1209,55 @@ public void testReaderId() throws Exception { } public void testRequestCache() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test") + assertAcked( + client().admin() + .indices() + .prepareCreate("test") .setSettings(Settings.builder().put(IndicesRequestCache.INDEX_CACHE_REQUEST_ENABLED_SETTING.getKey(), true)) .setMapping("field1", "type=text", "field2", "type=text", "field3", "type=text") ); - client().prepareIndex("test").setId("1").setSource("field1", "value1") - .get(); - client().prepareIndex("test").setId("2").setSource("field2", "value2") - .get(); - client().prepareIndex("test").setId("3").setSource("field3", "value3") - .get(); + client().prepareIndex("test").setId("1").setSource("field1", "value1").get(); + client().prepareIndex("test").setId("2").setSource("field2", "value2").get(); + client().prepareIndex("test").setId("3").setSource("field3", "value3").get(); refresh(); int max = scaledRandomIntBetween(4, 32); for (int i = 0; i < max; i++) { Boolean requestCache = randomFrom(true, null); - SearchResponse response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareSearch("test") - .setSize(0) - .setQuery(termQuery("field1", "value1")) - .setRequestCache(requestCache) - .get(); + SearchResponse response = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).prepareSearch("test").setSize(0).setQuery(termQuery("field1", "value1")).setRequestCache(requestCache).get(); assertNoFailures(response); assertHitCount(response, 1); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareSearch("test") - .setSize(0) - .setQuery(termQuery("field1", "value1")) - .setRequestCache(requestCache) - .get(); + .prepareSearch("test") + .setSize(0) + .setQuery(termQuery("field1", "value1")) + .setRequestCache(requestCache) + .get(); assertNoFailures(response); assertHitCount(response, 0); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) - .prepareSearch("test") - .setSize(0) - .setQuery(termQuery("field1", "value1")) - .setRequestCache(requestCache) - .get(); + .prepareSearch("test") + .setSize(0) + .setQuery(termQuery("field1", "value1")) + .setRequestCache(requestCache) + .get(); assertNoFailures(response); assertHitCount(response, 1); } } public void testUpdateApiIsBlocked() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test") - .setMapping("field1", "type=text", "field2", "type=text") - ); - client().prepareIndex("test").setId("1").setSource("field1", "value1") - .setRefreshPolicy(IMMEDIATE) - .get(); + assertAcked(client().admin().indices().prepareCreate("test").setMapping("field1", "type=text", "field2", "type=text")); + client().prepareIndex("test").setId("1").setSource("field1", "value1").setRefreshPolicy(IMMEDIATE).get(); // With document level security enabled the update is not allowed: try { client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareUpdate("test", "1").setDoc(Requests.INDEX_CONTENT_TYPE, "field1", "value2") - .get(); + .prepareUpdate("test", "1") + .setDoc(Requests.INDEX_CONTENT_TYPE, "field1", "value2") + .get(); fail("failed, because update request shouldn't be allowed if document level security is enabled"); } catch (ElasticsearchSecurityException e) { assertThat(e.status(), equalTo(RestStatus.BAD_REQUEST)); @@ -1257,120 +1266,136 @@ public void testUpdateApiIsBlocked() throws Exception { assertThat(client().prepareGet("test", "1").get().getSource().get("field1").toString(), equalTo("value1")); // With no document level security enabled the update is allowed: - client().prepareUpdate("test", "1").setDoc(Requests.INDEX_CONTENT_TYPE, "field1", "value2") - .get(); + client().prepareUpdate("test", "1").setDoc(Requests.INDEX_CONTENT_TYPE, "field1", "value2").get(); assertThat(client().prepareGet("test", "1").get().getSource().get("field1").toString(), equalTo("value2")); // With document level security enabled the update in bulk is not allowed: - BulkResponse bulkResponse = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue - ("user1", USERS_PASSWD))) - .prepareBulk() - .add(new UpdateRequest("test", "1").doc(Requests.INDEX_CONTENT_TYPE, "field1", "value3")) - .get(); + BulkResponse bulkResponse = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).prepareBulk().add(new UpdateRequest("test", "1").doc(Requests.INDEX_CONTENT_TYPE, "field1", "value3")).get(); assertEquals(1, bulkResponse.getItems().length); BulkItemResponse bulkItem = bulkResponse.getItems()[0]; assertTrue(bulkItem.isFailed()); assertThat(bulkItem.getFailure().getCause(), instanceOf(ElasticsearchSecurityException.class)); ElasticsearchSecurityException securityException = (ElasticsearchSecurityException) bulkItem.getFailure().getCause(); assertThat(securityException.status(), equalTo(RestStatus.BAD_REQUEST)); - assertThat(securityException.getMessage(), - equalTo("Can't execute a bulk item request with update requests embedded if field or document level security is enabled")); + assertThat( + securityException.getMessage(), + equalTo("Can't execute a bulk item request with update requests embedded if field or document level security is enabled") + ); assertThat(client().prepareGet("test", "1").get().getSource().get("field1").toString(), equalTo("value2")); - client().prepareBulk() - .add(new UpdateRequest("test", "1").doc(Requests.INDEX_CONTENT_TYPE, "field1", "value3")) - .get(); + client().prepareBulk().add(new UpdateRequest("test", "1").doc(Requests.INDEX_CONTENT_TYPE, "field1", "value3")).get(); assertThat(client().prepareGet("test", "1").get().getSource().get("field1").toString(), equalTo("value3")); } public void testNestedInnerHits() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test") - .setMapping("field1", "type=text", "nested_field", "type=nested") - ); - client().prepareIndex("test").setId("1") - .setSource(jsonBuilder().startObject() - .field("field1", "value1") - .startArray("nested_field") - .startObject() - .field("field2", "value2") - .endObject() - .startObject() - .array("field2", "value2", "value3") - .endObject() - .endArray() - .endObject()) - .get(); - client().prepareIndex("test").setId("2") - .setSource(jsonBuilder().startObject() - .field("field1", "value2") - .startArray("nested_field") - .startObject() - .field("field2", "value2") - .endObject() - .endArray() - .endObject()) - .get(); + assertAcked(client().admin().indices().prepareCreate("test").setMapping("field1", "type=text", "nested_field", "type=nested")); + client().prepareIndex("test") + .setId("1") + .setSource( + jsonBuilder().startObject() + .field("field1", "value1") + .startArray("nested_field") + .startObject() + .field("field2", "value2") + .endObject() + .startObject() + .array("field2", "value2", "value3") + .endObject() + .endArray() + .endObject() + ) + .get(); + client().prepareIndex("test") + .setId("2") + .setSource( + jsonBuilder().startObject() + .field("field1", "value2") + .startArray("nested_field") + .startObject() + .field("field2", "value2") + .endObject() + .endArray() + .endObject() + ) + .get(); refresh("test"); - SearchResponse response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user4", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(QueryBuilders.nestedQuery("nested_field", QueryBuilders.termQuery("nested_field.field2", "value2"), - ScoreMode.None).innerHit(new InnerHitBuilder())) - .get(); + SearchResponse response = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user4", USERS_PASSWD)) + ) + .prepareSearch("test") + .setQuery( + QueryBuilders.nestedQuery("nested_field", QueryBuilders.termQuery("nested_field.field2", "value2"), ScoreMode.None) + .innerHit(new InnerHitBuilder()) + ) + .get(); assertHitCount(response, 1); assertSearchHits(response, "1"); assertThat(response.getHits().getAt(0).getInnerHits().get("nested_field").getAt(0).getId(), equalTo("1")); assertThat(response.getHits().getAt(0).getInnerHits().get("nested_field").getAt(0).getNestedIdentity().getOffset(), equalTo(0)); - assertThat(response.getHits().getAt(0).getInnerHits().get("nested_field").getAt(0).getSourceAsString(), - equalTo("{\"field2\":\"value2\"}")); + assertThat( + response.getHits().getAt(0).getInnerHits().get("nested_field").getAt(0).getSourceAsString(), + equalTo("{\"field2\":\"value2\"}") + ); assertThat(response.getHits().getAt(0).getInnerHits().get("nested_field").getAt(1).getNestedIdentity().getOffset(), equalTo(1)); - assertThat(response.getHits().getAt(0).getInnerHits().get("nested_field").getAt(1).getSourceAsString(), - equalTo("{\"field2\":[\"value2\",\"value3\"]}")); + assertThat( + response.getHits().getAt(0).getInnerHits().get("nested_field").getAt(1).getSourceAsString(), + equalTo("{\"field2\":[\"value2\",\"value3\"]}") + ); } public void testSuggesters() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test") - .setSettings(Settings.builder() - .put("index.number_of_shards", 1) - .put("index.number_of_replicas", 0) - ) + assertAcked( + client().admin() + .indices() + .prepareCreate("test") + .setSettings(Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0)) .setMapping("field1", "type=text", "suggest_field1", "type=text", "suggest_field2", "type=completion") ); - client().prepareIndex("test").setId("1") - .setSource(jsonBuilder().startObject() - .field("field1", "value1") - .field("suggest_field1", "value") - .startObject("suggest_field2") - .field("input", "value") - .endObject() - .endObject()).get(); + client().prepareIndex("test") + .setId("1") + .setSource( + jsonBuilder().startObject() + .field("field1", "value1") + .field("suggest_field1", "value") + .startObject("suggest_field2") + .field("input", "value") + .endObject() + .endObject() + ) + .get(); // A document that is always included by role query of both roles: - client().prepareIndex("test").setId("2") - .setSource(jsonBuilder().startObject() - .field("field1", "value1") - .field("field2", "value2") - .endObject()).get(); + client().prepareIndex("test") + .setId("2") + .setSource(jsonBuilder().startObject().field("field1", "value1").field("field2", "value2").endObject()) + .get(); refresh("test"); - assertAcked(client().admin().indices().prepareCreate("fls-index") - .setSettings(Settings.builder() - .put("index.number_of_shards", 1) - .put("index.number_of_replicas", 0) - ) - .setMapping("field1", "type=text", "suggest_field1", "type=text", "suggest_field2", "type=completion", - "yet_another", "type=text") + assertAcked( + client().admin() + .indices() + .prepareCreate("fls-index") + .setSettings(Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0)) + .setMapping( + "field1", + "type=text", + "suggest_field1", + "type=text", + "suggest_field2", + "type=completion", + "yet_another", + "type=text" + ) ); // Term suggester: - SearchResponse response = client() - .prepareSearch("test") - .suggest(new SuggestBuilder() - .setGlobalText("valeu") - .addSuggestion("_name1", new TermSuggestionBuilder("suggest_field1")) - ).get(); + SearchResponse response = client().prepareSearch("test") + .suggest(new SuggestBuilder().setGlobalText("valeu").addSuggestion("_name1", new TermSuggestionBuilder("suggest_field1"))) + .get(); assertNoFailures(response); TermSuggestion termSuggestion = response.getSuggest().getSuggestion("_name1"); @@ -1379,25 +1404,23 @@ public void testSuggesters() throws Exception { assertThat(termSuggestion.getEntries().get(0).getOptions().size(), equalTo(1)); assertThat(termSuggestion.getEntries().get(0).getOptions().get(0).getText().string(), equalTo("value")); - final String[] indices = - randomFrom(List.of(new String[] { "test" }, new String[] { "fls-index", "test" }, new String[] { "test", "fls-index" })); + final String[] indices = randomFrom( + List.of(new String[] { "test" }, new String[] { "fls-index", "test" }, new String[] { "test", "fls-index" }) + ); - Exception e = expectThrows(ElasticsearchSecurityException.class, () -> client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user5", USERS_PASSWD))) + Exception e = expectThrows( + ElasticsearchSecurityException.class, + () -> client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user5", USERS_PASSWD))) .prepareSearch(indices) - .suggest(new SuggestBuilder() - .setGlobalText("valeu") - .addSuggestion("_name1", new TermSuggestionBuilder("suggest_field1")) - ).get()); + .suggest(new SuggestBuilder().setGlobalText("valeu").addSuggestion("_name1", new TermSuggestionBuilder("suggest_field1"))) + .get() + ); assertThat(e.getMessage(), equalTo("Suggest isn't supported if document level security is enabled")); // Phrase suggester: - response = client() - .prepareSearch("test") - .suggest(new SuggestBuilder() - .setGlobalText("valeu") - .addSuggestion("_name1", new PhraseSuggestionBuilder("suggest_field1")) - ).get(); + response = client().prepareSearch("test") + .suggest(new SuggestBuilder().setGlobalText("valeu").addSuggestion("_name1", new PhraseSuggestionBuilder("suggest_field1"))) + .get(); assertNoFailures(response); PhraseSuggestion phraseSuggestion = response.getSuggest().getSuggestion("_name1"); @@ -1406,22 +1429,19 @@ public void testSuggesters() throws Exception { assertThat(phraseSuggestion.getEntries().get(0).getOptions().size(), equalTo(1)); assertThat(phraseSuggestion.getEntries().get(0).getOptions().get(0).getText().string(), equalTo("value")); - e = expectThrows(ElasticsearchSecurityException.class, () -> client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user5", USERS_PASSWD))) + e = expectThrows( + ElasticsearchSecurityException.class, + () -> client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user5", USERS_PASSWD))) .prepareSearch(indices) - .suggest(new SuggestBuilder() - .setGlobalText("valeu") - .addSuggestion("_name1", new PhraseSuggestionBuilder("suggest_field1")) - ).get()); + .suggest(new SuggestBuilder().setGlobalText("valeu").addSuggestion("_name1", new PhraseSuggestionBuilder("suggest_field1"))) + .get() + ); assertThat(e.getMessage(), equalTo("Suggest isn't supported if document level security is enabled")); // Completion suggester: - response = client() - .prepareSearch("test") - .suggest(new SuggestBuilder() - .setGlobalText("valu") - .addSuggestion("_name1", new CompletionSuggestionBuilder("suggest_field2")) - ).get(); + response = client().prepareSearch("test") + .suggest(new SuggestBuilder().setGlobalText("valu").addSuggestion("_name1", new CompletionSuggestionBuilder("suggest_field2"))) + .get(); assertNoFailures(response); CompletionSuggestion completionSuggestion = response.getSuggest().getSuggestion("_name1"); @@ -1430,51 +1450,50 @@ public void testSuggesters() throws Exception { assertThat(completionSuggestion.getEntries().get(0).getOptions().size(), equalTo(1)); assertThat(completionSuggestion.getEntries().get(0).getOptions().get(0).getText().string(), equalTo("value")); - e = expectThrows(ElasticsearchSecurityException.class, () -> client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user5", USERS_PASSWD))) + e = expectThrows( + ElasticsearchSecurityException.class, + () -> client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user5", USERS_PASSWD))) .prepareSearch(indices) - .suggest(new SuggestBuilder() - .setGlobalText("valeu") - .addSuggestion("_name1", new CompletionSuggestionBuilder("suggest_field2")) - ).get()); + .suggest( + new SuggestBuilder().setGlobalText("valeu").addSuggestion("_name1", new CompletionSuggestionBuilder("suggest_field2")) + ) + .get() + ); assertThat(e.getMessage(), equalTo("Suggest isn't supported if document level security is enabled")); } public void testProfile() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test") - .setSettings(Settings.builder() - .put("index.number_of_shards", 1) - .put("index.number_of_replicas", 0) - ) + assertAcked( + client().admin() + .indices() + .prepareCreate("test") + .setSettings(Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0)) .setMapping("field1", "type=text", "other_field", "type=text") ); - client().prepareIndex("test").setId("1") - .setSource(jsonBuilder().startObject() - .field("field1", "value1") - .field("other_field", "value") - .endObject()).get(); + client().prepareIndex("test") + .setId("1") + .setSource(jsonBuilder().startObject().field("field1", "value1").field("other_field", "value").endObject()) + .get(); // A document that is always included by role query of both roles: - client().prepareIndex("test").setId("2") - .setSource(jsonBuilder().startObject() - .field("field1", "value1") - .field("field2", "value2") - .endObject()).get(); + client().prepareIndex("test") + .setId("2") + .setSource(jsonBuilder().startObject().field("field1", "value1").field("field2", "value2").endObject()) + .get(); refresh("test"); - assertAcked(client().admin().indices().prepareCreate("fls-index") - .setSettings(Settings.builder() - .put("index.number_of_shards", 1) - .put("index.number_of_replicas", 0) - ) - .setMapping("field1", "type=text", "other_field", "type=text", "yet_another", "type=text") + assertAcked( + client().admin() + .indices() + .prepareCreate("fls-index") + .setSettings(Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0)) + .setMapping("field1", "type=text", "other_field", "type=text", "yet_another", "type=text") ); - SearchResponse response = client() - .prepareSearch("test") - .setProfile(true) - .setQuery(new FuzzyQueryBuilder("other_field", "valeu")) - .get(); + SearchResponse response = client().prepareSearch("test") + .setProfile(true) + .setQuery(new FuzzyQueryBuilder("other_field", "valeu")) + .get(); assertNoFailures(response); assertThat(response.getProfileResults().size(), equalTo(1)); @@ -1488,14 +1507,17 @@ public void testProfile() throws Exception { equalTo(List.of("(other_field:value)^0.8")) ); - final String[] indices = - randomFrom(List.of(new String[] { "test" }, new String[] { "fls-index", "test" }, new String[] { "test", "fls-index" })); - Exception e = expectThrows(ElasticsearchSecurityException.class, () -> client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user5", USERS_PASSWD))) + final String[] indices = randomFrom( + List.of(new String[] { "test" }, new String[] { "fls-index", "test" }, new String[] { "test", "fls-index" }) + ); + Exception e = expectThrows( + ElasticsearchSecurityException.class, + () -> client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user5", USERS_PASSWD))) .prepareSearch(indices) .setProfile(true) .setQuery(new FuzzyQueryBuilder("other_field", "valeu")) - .get()); + .get() + ); assertThat(e.getMessage(), equalTo("A search request cannot be profiled if document level security is enabled")); } diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/FieldLevelSecurityRandomTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/FieldLevelSecurityRandomTests.java index 4d9df570cf6a5..75fccb0eae9a9 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/FieldLevelSecurityRandomTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/FieldLevelSecurityRandomTests.java @@ -12,9 +12,9 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.sort.SortOrder; +import org.elasticsearch.test.SecurityIntegTestCase; import org.elasticsearch.test.SecuritySettingsSourceField; import org.elasticsearch.xpack.core.XPackSettings; -import org.elasticsearch.test.SecurityIntegTestCase; import java.util.ArrayList; import java.util.Collections; @@ -26,10 +26,10 @@ import static org.elasticsearch.action.support.WriteRequest.RefreshPolicy.IMMEDIATE; import static org.elasticsearch.index.query.QueryBuilders.matchQuery; -import static org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken.BASIC_AUTH_HEADER; -import static org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken.basicAuthHeaderValue; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; +import static org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken.BASIC_AUTH_HEADER; +import static org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken.basicAuthHeaderValue; import static org.hamcrest.Matchers.equalTo; public class FieldLevelSecurityRandomTests extends SecurityIntegTestCase { @@ -43,21 +43,29 @@ public class FieldLevelSecurityRandomTests extends SecurityIntegTestCase { protected String configUsers() { final String usersPasswdHashed = new String(getFastStoredHashAlgoForTests().hash(USERS_PASSWD)); - return super.configUsers() + - "user1:" + usersPasswdHashed + "\n" + - "user2:" + usersPasswdHashed + "\n" + - "user3:" + usersPasswdHashed + "\n" + - "user4:" + usersPasswdHashed + "\n"; + return super.configUsers() + + "user1:" + + usersPasswdHashed + + "\n" + + "user2:" + + usersPasswdHashed + + "\n" + + "user3:" + + usersPasswdHashed + + "\n" + + "user4:" + + usersPasswdHashed + + "\n"; } @Override protected String configUsersRoles() { - return super.configUsersRoles() + - "role1:user1,user2,user3,user4\n" + - "role2:user1\n" + - "role3:user2\n" + - "role4:user3\n" + - "role5:user4\n"; + return super.configUsersRoles() + + "role1:user1,user2,user3,user4\n" + + "role2:user1\n" + + "role3:user2\n" + + "role4:user3\n" + + "role5:user4\n"; } @Override @@ -82,50 +90,51 @@ protected String configRoles() { roleFields.append(" - ").append(field).append('\n'); } - return super.configRoles() + - "\nrole1:\n" + - " cluster: [ none ]\n" + - " indices:\n" + - " - names: '*'\n" + - " privileges: [ none ]\n" + - "\nrole2:\n" + - " cluster: [ all ]\n" + - " indices:\n" + - " - names: '*'\n" + - " privileges: [ ALL ]\n" + - " field_security:\n" + - " grant:\n" + roleFields.toString() + - "role3:\n" + - " cluster:\n" + - " - all\n" + - " indices:\n" + - " - names: test\n" + - " privileges:\n" + - " - all\n" + - " field_security:\n" + - " grant: [ id, field1 ]\n" + - "role4:\n" + - " cluster: [ all ]\n" + - " indices:\n" + - " - names: test\n" + - " privileges: [ ALL ]\n" + - " field_security:\n" + - " grant: [ id, field2 ]\n" + - "role5:\n" + - " cluster: [ all ]\n" + - " indices:\n" + - " - names: test\n" + - " privileges: [ ALL ]\n" + - " field_security:\n" + - " grant: [ id, field3 ]\n"; + return super.configRoles() + + "\nrole1:\n" + + " cluster: [ none ]\n" + + " indices:\n" + + " - names: '*'\n" + + " privileges: [ none ]\n" + + "\nrole2:\n" + + " cluster: [ all ]\n" + + " indices:\n" + + " - names: '*'\n" + + " privileges: [ ALL ]\n" + + " field_security:\n" + + " grant:\n" + + roleFields.toString() + + "role3:\n" + + " cluster:\n" + + " - all\n" + + " indices:\n" + + " - names: test\n" + + " privileges:\n" + + " - all\n" + + " field_security:\n" + + " grant: [ id, field1 ]\n" + + "role4:\n" + + " cluster: [ all ]\n" + + " indices:\n" + + " - names: test\n" + + " privileges: [ ALL ]\n" + + " field_security:\n" + + " grant: [ id, field2 ]\n" + + "role5:\n" + + " cluster: [ all ]\n" + + " indices:\n" + + " - names: test\n" + + " privileges: [ ALL ]\n" + + " field_security:\n" + + " grant: [ id, field3 ]\n"; } @Override public Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { return Settings.builder() - .put(super.nodeSettings(nodeOrdinal, otherSettings)) - .put(XPackSettings.DLS_FLS_ENABLED.getKey(), true) - .build(); + .put(super.nodeSettings(nodeOrdinal, otherSettings)) + .put(XPackSettings.DLS_FLS_ENABLED.getKey(), true) + .build(); } public void testRandom() { @@ -142,34 +151,31 @@ public void testRandom() { fieldMappers[j++] = "type=text"; doc.put(field, "value"); } - assertAcked(client().admin().indices().prepareCreate("test") - .setMapping(fieldMappers) - ); + assertAcked(client().admin().indices().prepareCreate("test").setMapping(fieldMappers)); client().prepareIndex("test").setId("1").setSource(doc).setRefreshPolicy(IMMEDIATE).get(); for (String allowedField : allowedFields) { logger.info("Checking allowed field [{}]", allowedField); - SearchResponse response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(matchQuery(allowedField, "value")) - .get(); + SearchResponse response = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).prepareSearch("test").setQuery(matchQuery(allowedField, "value")).get(); assertHitCount(response, 1); } for (String disallowedField : disAllowedFields) { logger.info("Checking disallowed field [{}]", disallowedField); - SearchResponse response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(matchQuery(disallowedField, "value")) - .get(); + SearchResponse response = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).prepareSearch("test").setQuery(matchQuery(disallowedField, "value")).get(); assertHitCount(response, 0); } } public void testDuel() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test") - .setMapping("id", "type=keyword", "field1", "type=text", "field2", "type=text", "field3", "type=text") + assertAcked( + client().admin() + .indices() + .prepareCreate("test") + .setMapping("id", "type=keyword", "field1", "type=text", "field2", "type=text", "field3", "type=text") ); int numDocs = scaledRandomIntBetween(32, 128); @@ -181,22 +187,22 @@ public void testDuel() throws Exception { } indexRandom(true, requests); - SearchResponse actual = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareSearch("test") - .addSort("id", SortOrder.ASC) - .setQuery(QueryBuilders.boolQuery() - .should(QueryBuilders.termQuery("field1", "value")) - .should(QueryBuilders.termQuery("field2", "value")) - .should(QueryBuilders.termQuery("field3", "value")) - ) - .get(); + SearchResponse actual = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD)) + ) + .prepareSearch("test") + .addSort("id", SortOrder.ASC) + .setQuery( + QueryBuilders.boolQuery() + .should(QueryBuilders.termQuery("field1", "value")) + .should(QueryBuilders.termQuery("field2", "value")) + .should(QueryBuilders.termQuery("field3", "value")) + ) + .get(); SearchResponse expected = client().prepareSearch("test") - .addSort("id", SortOrder.ASC) - .setQuery(QueryBuilders.boolQuery() - .should(QueryBuilders.termQuery("field1", "value")) - ) - .get(); + .addSort("id", SortOrder.ASC) + .setQuery(QueryBuilders.boolQuery().should(QueryBuilders.termQuery("field1", "value"))) + .get(); assertThat(actual.getHits().getTotalHits().value, equalTo(expected.getHits().getTotalHits().value)); assertThat(actual.getHits().getHits().length, equalTo(expected.getHits().getHits().length)); for (int i = 0; i < actual.getHits().getHits().length; i++) { @@ -204,20 +210,19 @@ public void testDuel() throws Exception { } actual = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) - .prepareSearch("test") - .addSort("id", SortOrder.ASC) - .setQuery(QueryBuilders.boolQuery() - .should(QueryBuilders.termQuery("field1", "value")) - .should(QueryBuilders.termQuery("field2", "value")) - .should(QueryBuilders.termQuery("field3", "value")) - ) - .get(); + .prepareSearch("test") + .addSort("id", SortOrder.ASC) + .setQuery( + QueryBuilders.boolQuery() + .should(QueryBuilders.termQuery("field1", "value")) + .should(QueryBuilders.termQuery("field2", "value")) + .should(QueryBuilders.termQuery("field3", "value")) + ) + .get(); expected = client().prepareSearch("test") - .addSort("id", SortOrder.ASC) - .setQuery(QueryBuilders.boolQuery() - .should(QueryBuilders.termQuery("field2", "value")) - ) - .get(); + .addSort("id", SortOrder.ASC) + .setQuery(QueryBuilders.boolQuery().should(QueryBuilders.termQuery("field2", "value"))) + .get(); assertThat(actual.getHits().getTotalHits().value, equalTo(expected.getHits().getTotalHits().value)); assertThat(actual.getHits().getHits().length, equalTo(expected.getHits().getHits().length)); for (int i = 0; i < actual.getHits().getHits().length; i++) { @@ -225,20 +230,19 @@ public void testDuel() throws Exception { } actual = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user4", USERS_PASSWD))) - .prepareSearch("test") - .addSort("id", SortOrder.ASC) - .setQuery(QueryBuilders.boolQuery() - .should(QueryBuilders.termQuery("field1", "value")) - .should(QueryBuilders.termQuery("field2", "value")) - .should(QueryBuilders.termQuery("field3", "value")) - ) - .get(); + .prepareSearch("test") + .addSort("id", SortOrder.ASC) + .setQuery( + QueryBuilders.boolQuery() + .should(QueryBuilders.termQuery("field1", "value")) + .should(QueryBuilders.termQuery("field2", "value")) + .should(QueryBuilders.termQuery("field3", "value")) + ) + .get(); expected = client().prepareSearch("test") - .addSort("id", SortOrder.ASC) - .setQuery(QueryBuilders.boolQuery() - .should(QueryBuilders.termQuery("field3", "value")) - ) - .get(); + .addSort("id", SortOrder.ASC) + .setQuery(QueryBuilders.boolQuery().should(QueryBuilders.termQuery("field3", "value"))) + .get(); assertThat(actual.getHits().getTotalHits().value, equalTo(expected.getHits().getTotalHits().value)); assertThat(actual.getHits().getHits().length, equalTo(expected.getHits().getHits().length)); for (int i = 0; i < actual.getHits().getHits().length; i++) { diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/FieldLevelSecurityTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/FieldLevelSecurityTests.java index 6758c26772759..f097196b087ae 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/FieldLevelSecurityTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/FieldLevelSecurityTests.java @@ -12,7 +12,12 @@ import org.elasticsearch.action.bulk.BulkResponse; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.get.MultiGetResponse; +import org.elasticsearch.action.search.ClosePointInTimeAction; +import org.elasticsearch.action.search.ClosePointInTimeRequest; import org.elasticsearch.action.search.MultiSearchResponse; +import org.elasticsearch.action.search.OpenPointInTimeAction; +import org.elasticsearch.action.search.OpenPointInTimeRequest; +import org.elasticsearch.action.search.OpenPointInTimeResponse; import org.elasticsearch.action.search.SearchRequestBuilder; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.support.WriteRequest; @@ -26,9 +31,6 @@ import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.IndexModule; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; @@ -48,12 +50,10 @@ import org.elasticsearch.test.InternalSettingsPlugin; import org.elasticsearch.test.SecurityIntegTestCase; import org.elasticsearch.test.SecuritySettingsSourceField; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.XPackSettings; -import org.elasticsearch.action.search.ClosePointInTimeAction; -import org.elasticsearch.action.search.ClosePointInTimeRequest; -import org.elasticsearch.action.search.OpenPointInTimeAction; -import org.elasticsearch.action.search.OpenPointInTimeRequest; -import org.elasticsearch.action.search.OpenPointInTimeResponse; import org.elasticsearch.xpack.security.LocalStateSecurity; import org.elasticsearch.xpack.spatial.SpatialPlugin; import org.elasticsearch.xpack.spatial.index.query.ShapeQueryBuilder; @@ -92,8 +92,15 @@ public class FieldLevelSecurityTests extends SecurityIntegTestCase { @Override protected Collection> nodePlugins() { - return Arrays.asList(LocalStateSecurity.class, CommonAnalysisPlugin.class, ParentJoinPlugin.class, - InternalSettingsPlugin.class, PercolatorPlugin.class, DenseVectorPlugin.class, SpatialPlugin.class); + return Arrays.asList( + LocalStateSecurity.class, + CommonAnalysisPlugin.class, + ParentJoinPlugin.class, + InternalSettingsPlugin.class, + PercolatorPlugin.class, + DenseVectorPlugin.class, + SpatialPlugin.class + ); } @Override @@ -105,283 +112,301 @@ protected boolean addMockGeoShapeFieldMapper() { @Override protected String configUsers() { final String usersPasswHashed = new String(getFastStoredHashAlgoForTests().hash(USERS_PASSWD)); - return super.configUsers() + - "user1:" + usersPasswHashed + "\n" + - "user2:" + usersPasswHashed + "\n" + - "user3:" + usersPasswHashed + "\n" + - "user4:" + usersPasswHashed + "\n" + - "user5:" + usersPasswHashed + "\n" + - "user6:" + usersPasswHashed + "\n" + - "user7:" + usersPasswHashed + "\n" + - "user8:" + usersPasswHashed + "\n" + - "user9:" + usersPasswHashed + "\n"; + return super.configUsers() + + "user1:" + + usersPasswHashed + + "\n" + + "user2:" + + usersPasswHashed + + "\n" + + "user3:" + + usersPasswHashed + + "\n" + + "user4:" + + usersPasswHashed + + "\n" + + "user5:" + + usersPasswHashed + + "\n" + + "user6:" + + usersPasswHashed + + "\n" + + "user7:" + + usersPasswHashed + + "\n" + + "user8:" + + usersPasswHashed + + "\n" + + "user9:" + + usersPasswHashed + + "\n"; } @Override protected String configUsersRoles() { - return super.configUsersRoles() + - "role1:user1\n" + - "role2:user1,user7,user8\n" + - "role3:user2,user7,user8\n" + - "role4:user3,user7\n" + - "role5:user4,user7\n" + - "role6:user5,user7\n" + - "role7:user6\n" + - "role8:user9"; + return super.configUsersRoles() + + "role1:user1\n" + + "role2:user1,user7,user8\n" + + "role3:user2,user7,user8\n" + + "role4:user3,user7\n" + + "role5:user4,user7\n" + + "role6:user5,user7\n" + + "role7:user6\n" + + "role8:user9"; } + @Override protected String configRoles() { - return super.configRoles() + - "\nrole1:\n" + - " cluster: [ none ]\n" + - " indices:\n" + - " - names: '*'\n" + - " privileges: [ none ]\n" + - "role2:\n" + - " cluster: [ all ]\n" + - " indices:\n" + - " - names: '*'\n" + - " privileges: [ ALL ]\n" + - " field_security:\n" + - " grant: [ field1, join_field*, vector ]\n" + - "role3:\n" + - " cluster: [ all ]\n" + - " indices:\n" + - " - names: '*'\n" + - " privileges: [ ALL ]\n" + - " field_security:\n" + - " grant: [ field2, query* ]\n" + - "role4:\n" + - " cluster: [ all ]\n" + - " indices:\n" + - " - names: '*'\n" + - " privileges: [ ALL ]\n" + - " field_security:\n" + - " grant: [ field1, field2]\n" + - "role5:\n" + - " cluster: [ all ]\n" + - " indices:\n" + - " - names: '*'\n" + - " privileges: [ ALL ]\n" + - " field_security:\n" + - " grant: [ ]\n" + - "role6:\n" + - " cluster: [ all ]\n" + - " indices:\n" + - " - names: '*'\n" + - " privileges: [ALL]\n" + - "role7:\n" + - " cluster: [ all ]\n" + - " indices:\n" + - " - names: '*'\n" + - " privileges: [ ALL ]\n" + - " field_security:\n" + - " grant: [ 'field*' ]\n" + - "role8:\n" + - " indices:\n" + - " - names: 'doc_index'\n" + - " privileges: [ ALL ]\n" + - " field_security:\n" + - " grant: [ 'field*' ]\n" + - " except: [ 'field2' ]\n" + - " - names: 'query_index'\n" + - " privileges: [ ALL ]\n" + - " field_security:\n" + - " grant: [ 'field*', 'query' ]\n"; + return super.configRoles() + + "\nrole1:\n" + + " cluster: [ none ]\n" + + " indices:\n" + + " - names: '*'\n" + + " privileges: [ none ]\n" + + "role2:\n" + + " cluster: [ all ]\n" + + " indices:\n" + + " - names: '*'\n" + + " privileges: [ ALL ]\n" + + " field_security:\n" + + " grant: [ field1, join_field*, vector ]\n" + + "role3:\n" + + " cluster: [ all ]\n" + + " indices:\n" + + " - names: '*'\n" + + " privileges: [ ALL ]\n" + + " field_security:\n" + + " grant: [ field2, query* ]\n" + + "role4:\n" + + " cluster: [ all ]\n" + + " indices:\n" + + " - names: '*'\n" + + " privileges: [ ALL ]\n" + + " field_security:\n" + + " grant: [ field1, field2]\n" + + "role5:\n" + + " cluster: [ all ]\n" + + " indices:\n" + + " - names: '*'\n" + + " privileges: [ ALL ]\n" + + " field_security:\n" + + " grant: [ ]\n" + + "role6:\n" + + " cluster: [ all ]\n" + + " indices:\n" + + " - names: '*'\n" + + " privileges: [ALL]\n" + + "role7:\n" + + " cluster: [ all ]\n" + + " indices:\n" + + " - names: '*'\n" + + " privileges: [ ALL ]\n" + + " field_security:\n" + + " grant: [ 'field*' ]\n" + + "role8:\n" + + " indices:\n" + + " - names: 'doc_index'\n" + + " privileges: [ ALL ]\n" + + " field_security:\n" + + " grant: [ 'field*' ]\n" + + " except: [ 'field2' ]\n" + + " - names: 'query_index'\n" + + " privileges: [ ALL ]\n" + + " field_security:\n" + + " grant: [ 'field*', 'query' ]\n"; } @Override public Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { return Settings.builder() - .put(super.nodeSettings(nodeOrdinal, otherSettings)) - .put(XPackSettings.DLS_FLS_ENABLED.getKey(), true) - .build(); + .put(super.nodeSettings(nodeOrdinal, otherSettings)) + .put(XPackSettings.DLS_FLS_ENABLED.getKey(), true) + .build(); } public void testQuery() { - assertAcked(client().admin().indices().prepareCreate("test").setMapping( - "field1", "type=text", - "field2", "type=text", - "field3", "type=text", - "alias", "type=alias,path=field1")); - client().prepareIndex("test").setId("1").setSource("field1", "value1", "field2", "value2", "field3", "value3") - .setRefreshPolicy(IMMEDIATE) - .get(); + assertAcked( + client().admin() + .indices() + .prepareCreate("test") + .setMapping("field1", "type=text", "field2", "type=text", "field3", "type=text", "alias", "type=alias,path=field1") + ); + client().prepareIndex("test") + .setId("1") + .setSource("field1", "value1", "field2", "value2", "field3", "value3") + .setRefreshPolicy(IMMEDIATE) + .get(); // user1 has access to field1, so the query should match with the document: - SearchResponse response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(matchQuery("field1", "value1")) - .get(); + SearchResponse response = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).prepareSearch("test").setQuery(matchQuery("field1", "value1")).get(); assertHitCount(response, 1); // user2 has no access to field1, so the query should not match with the document: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(matchQuery("field1", "value1")) - .get(); + .prepareSearch("test") + .setQuery(matchQuery("field1", "value1")) + .get(); assertHitCount(response, 0); // user3 has access to field1 and field2, so the query should match with the document: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(matchQuery("field1", "value1")) - .get(); + .prepareSearch("test") + .setQuery(matchQuery("field1", "value1")) + .get(); assertHitCount(response, 1); // user4 has access to no fields, so the query should not match with the document: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user4", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(matchQuery("field1", "value1")) - .get(); + .prepareSearch("test") + .setQuery(matchQuery("field1", "value1")) + .get(); assertHitCount(response, 0); // user5 has no field level security configured, so the query should match with the document: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user5", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(matchQuery("field1", "value1")) - .get(); + .prepareSearch("test") + .setQuery(matchQuery("field1", "value1")) + .get(); assertHitCount(response, 1); // user7 has roles with field level security configured and without field level security response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user7", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(matchQuery("field1", "value1")) - .get(); + .prepareSearch("test") + .setQuery(matchQuery("field1", "value1")) + .get(); assertHitCount(response, 1); // user8 has roles with field level security configured for field1 and field2 response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user8", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(matchQuery("field1", "value1")) - .get(); + .prepareSearch("test") + .setQuery(matchQuery("field1", "value1")) + .get(); assertHitCount(response, 1); // user1 has no access to field1, so the query should not match with the document: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(matchQuery("field2", "value2")) - .get(); + .prepareSearch("test") + .setQuery(matchQuery("field2", "value2")) + .get(); assertHitCount(response, 0); // user2 has access to field1, so the query should match with the document: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(matchQuery("field2", "value2")) - .get(); + .prepareSearch("test") + .setQuery(matchQuery("field2", "value2")) + .get(); assertHitCount(response, 1); // user3 has access to field1 and field2, so the query should match with the document: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(matchQuery("field2", "value2")) - .get(); + .prepareSearch("test") + .setQuery(matchQuery("field2", "value2")) + .get(); assertHitCount(response, 1); // user4 has access to no fields, so the query should not match with the document: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user4", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(matchQuery("field2", "value2")) - .get(); + .prepareSearch("test") + .setQuery(matchQuery("field2", "value2")) + .get(); assertHitCount(response, 0); // user5 has no field level security configured, so the query should match with the document: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user5", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(matchQuery("field2", "value2")) - .get(); + .prepareSearch("test") + .setQuery(matchQuery("field2", "value2")) + .get(); assertHitCount(response, 1); // user7 has role with field level security and without field level security response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user5", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(matchQuery("field2", "value2")) - .get(); + .prepareSearch("test") + .setQuery(matchQuery("field2", "value2")) + .get(); assertHitCount(response, 1); // user8 has roles with field level security configured for field1 and field2 response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user8", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(matchQuery("field2", "value2")) - .get(); + .prepareSearch("test") + .setQuery(matchQuery("field2", "value2")) + .get(); assertHitCount(response, 1); // user1 has access to field3, so the query should not match with the document: - response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(matchQuery("field3", "value3")) - .get(); + response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) + .prepareSearch("test") + .setQuery(matchQuery("field3", "value3")) + .get(); assertHitCount(response, 0); // user2 has no access to field3, so the query should not match with the document: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(matchQuery("field3", "value3")) - .get(); + .prepareSearch("test") + .setQuery(matchQuery("field3", "value3")) + .get(); assertHitCount(response, 0); // user3 has access to field1 and field2 but not field3, so the query should not match with the document: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(matchQuery("field3", "value3")) - .get(); + .prepareSearch("test") + .setQuery(matchQuery("field3", "value3")) + .get(); assertHitCount(response, 0); // user4 has access to no fields, so the query should not match with the document: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user4", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(matchQuery("field3", "value3")) - .get(); + .prepareSearch("test") + .setQuery(matchQuery("field3", "value3")) + .get(); assertHitCount(response, 0); // user5 has no field level security configured, so the query should match with the document: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user5", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(matchQuery("field3", "value3")) - .get(); + .prepareSearch("test") + .setQuery(matchQuery("field3", "value3")) + .get(); assertHitCount(response, 1); // user7 has roles with field level security and without field level security response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user5", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(matchQuery("field3", "value3")) - .get(); + .prepareSearch("test") + .setQuery(matchQuery("field3", "value3")) + .get(); assertHitCount(response, 1); // user8 has roles with field level security configured for field1 and field2 response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user8", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(matchQuery("field3", "value3")) - .get(); + .prepareSearch("test") + .setQuery(matchQuery("field3", "value3")) + .get(); assertHitCount(response, 0); // user1 has access to field1, so a query on its field alias should match with the document: - response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(matchQuery("alias", "value1")) - .get(); + response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) + .prepareSearch("test") + .setQuery(matchQuery("alias", "value1")) + .get(); assertHitCount(response, 1); // user2 has no access to field1, so a query on its field alias should not match with the document: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(matchQuery("alias", "value1")) - .get(); + .prepareSearch("test") + .setQuery(matchQuery("alias", "value1")) + .get(); assertHitCount(response, 0); } public void testKnnSearch() throws IOException { - XContentBuilder builder = XContentFactory.jsonBuilder().startObject().startObject("properties") - .startObject("vector") - .field("type", "dense_vector") - .field("dims", 3) - .field("index", true) - .field("similarity", "l2_norm") - .endObject() - .endObject().endObject(); + XContentBuilder builder = XContentFactory.jsonBuilder() + .startObject() + .startObject("properties") + .startObject("vector") + .field("type", "dense_vector") + .field("dims", 3) + .field("index", true) + .field("similarity", "l2_norm") + .endObject() + .endObject() + .endObject(); assertAcked(client().admin().indices().prepareCreate("test").setMapping(builder)); client().prepareIndex("test") - .setSource("field1", "value1", "vector", new float[]{0.0f, 0.0f, 0.0f}) + .setSource("field1", "value1", "vector", new float[] { 0.0f, 0.0f, 0.0f }) .setRefreshPolicy(IMMEDIATE) .get(); // Since there's no kNN search action at the transport layer, we just emulate // how the action works (it builds a kNN query under the hood) - float[] queryVector = new float[]{0.0f, 0.0f, 0.0f}; + float[] queryVector = new float[] { 0.0f, 0.0f, 0.0f }; KnnVectorQueryBuilder query = new KnnVectorQueryBuilder("vector", queryVector, 10); // user1 has access to vector field, so the query should match with the document: - SearchResponse response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(query) - .addFetchField("vector") - .get(); + SearchResponse response = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).prepareSearch("test").setQuery(query).addFetchField("vector").get(); assertHitCount(response, 1); assertNotNull(response.getHits().getAt(0).field("vector")); @@ -403,95 +428,90 @@ public void testKnnSearch() throws IOException { } public void testPercolateQueryWithIndexedDocWithFLS() { - assertAcked(client().admin().indices().prepareCreate("query_index") - .setMapping("query", "type=percolator", "field2", "type=text") - ); - assertAcked(client().admin().indices().prepareCreate("doc_index") - .setMapping("field2", "type=text", "field1", "type=text") - ); - client().prepareIndex("query_index").setId("1") - .setSource("{\"query\": {\"match\": {\"field2\": \"bonsai tree\"}}}", - XContentType.JSON) - .setRefreshPolicy(IMMEDIATE).get(); - client().prepareIndex("doc_index").setId("1") - .setSource("{\"field1\": \"value1\", \"field2\": \"A new bonsai tree in the office\"}", - XContentType.JSON) - .setRefreshPolicy(IMMEDIATE).get(); + assertAcked(client().admin().indices().prepareCreate("query_index").setMapping("query", "type=percolator", "field2", "type=text")); + assertAcked(client().admin().indices().prepareCreate("doc_index").setMapping("field2", "type=text", "field1", "type=text")); + client().prepareIndex("query_index") + .setId("1") + .setSource("{\"query\": {\"match\": {\"field2\": \"bonsai tree\"}}}", XContentType.JSON) + .setRefreshPolicy(IMMEDIATE) + .get(); + client().prepareIndex("doc_index") + .setId("1") + .setSource("{\"field1\": \"value1\", \"field2\": \"A new bonsai tree in the office\"}", XContentType.JSON) + .setRefreshPolicy(IMMEDIATE) + .get(); QueryBuilder percolateQuery = new PercolateQueryBuilder("query", "doc_index", "1", null, null, null); // user7 sees everything - SearchResponse result = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user7", USERS_PASSWD))) - .prepareSearch("query_index") - .setQuery(percolateQuery) - .get(); + SearchResponse result = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user7", USERS_PASSWD)) + ).prepareSearch("query_index").setQuery(percolateQuery).get(); assertSearchResponse(result); assertHitCount(result, 1); - result = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) - .prepareSearch("query_index") - .setQuery(QueryBuilders.matchAllQuery()) - .get(); + result = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) + .prepareSearch("query_index") + .setQuery(QueryBuilders.matchAllQuery()) + .get(); assertSearchResponse(result); assertHitCount(result, 1); // user 3 can see the fields of the percolated document, but not the "query" field of the indexed query - result = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) - .prepareSearch("query_index") - .setQuery(percolateQuery) - .get(); + result = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) + .prepareSearch("query_index") + .setQuery(percolateQuery) + .get(); assertSearchResponse(result); assertHitCount(result, 0); - result = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user9", USERS_PASSWD))) - .prepareSearch("query_index") - .setQuery(QueryBuilders.matchAllQuery()) - .get(); + result = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user9", USERS_PASSWD))) + .prepareSearch("query_index") + .setQuery(QueryBuilders.matchAllQuery()) + .get(); assertSearchResponse(result); assertHitCount(result, 1); // user 9 can see the fields of the index query, but not the field of the indexed document to be percolated - result = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user9", USERS_PASSWD))) - .prepareSearch("query_index") - .setQuery(percolateQuery) - .get(); + result = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user9", USERS_PASSWD))) + .prepareSearch("query_index") + .setQuery(percolateQuery) + .get(); assertSearchResponse(result); assertHitCount(result, 0); } public void testGeoQueryWithIndexedShapeWithFLS() { - assertAcked(client().admin().indices().prepareCreate("search_index") - .setMapping("field", "type=shape", "other", "type=shape") - ); - assertAcked(client().admin().indices().prepareCreate("shape_index") - .setMapping("field", "type=shape", "other", "type=shape") - ); - client().prepareIndex("search_index").setId("1") - .setSource("{\"field\": {\"type\": \"point\", \"coordinates\":[1, 1]}}", - XContentType.JSON) - .setRefreshPolicy(IMMEDIATE).get(); - client().prepareIndex("search_index").setId("2") - .setSource("{\"other\": {\"type\": \"point\", \"coordinates\":[1, 1]}}", - XContentType.JSON) - .setRefreshPolicy(IMMEDIATE).get(); - client().prepareIndex("shape_index").setId("1") - .setSource("{\"field\": {\"type\": \"envelope\", \"coordinates\": [[0, 2], [2, 0]]}, " + - "\"field2\": {\"type\": \"envelope\", \"coordinates\": [[0, 2], [2, 0]]}}", - XContentType.JSON) - .setRefreshPolicy(IMMEDIATE).get(); - client().prepareIndex("shape_index").setId("2") - .setSource("{\"other\": {\"type\": \"envelope\", \"coordinates\": [[0, 2], [2, 0]]}}", - XContentType.JSON) - .setRefreshPolicy(IMMEDIATE).get(); + assertAcked(client().admin().indices().prepareCreate("search_index").setMapping("field", "type=shape", "other", "type=shape")); + assertAcked(client().admin().indices().prepareCreate("shape_index").setMapping("field", "type=shape", "other", "type=shape")); + client().prepareIndex("search_index") + .setId("1") + .setSource("{\"field\": {\"type\": \"point\", \"coordinates\":[1, 1]}}", XContentType.JSON) + .setRefreshPolicy(IMMEDIATE) + .get(); + client().prepareIndex("search_index") + .setId("2") + .setSource("{\"other\": {\"type\": \"point\", \"coordinates\":[1, 1]}}", XContentType.JSON) + .setRefreshPolicy(IMMEDIATE) + .get(); + client().prepareIndex("shape_index") + .setId("1") + .setSource( + "{\"field\": {\"type\": \"envelope\", \"coordinates\": [[0, 2], [2, 0]]}, " + + "\"field2\": {\"type\": \"envelope\", \"coordinates\": [[0, 2], [2, 0]]}}", + XContentType.JSON + ) + .setRefreshPolicy(IMMEDIATE) + .get(); + client().prepareIndex("shape_index") + .setId("2") + .setSource("{\"other\": {\"type\": \"envelope\", \"coordinates\": [[0, 2], [2, 0]]}}", XContentType.JSON) + .setRefreshPolicy(IMMEDIATE) + .get(); SearchResponse result; // user sees both the querying shape and the queried point - SearchRequestBuilder requestBuilder = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user6", USERS_PASSWD))) - .prepareSearch("search_index"); - final ShapeQueryBuilder shapeQuery1 = new ShapeQueryBuilder("field", "1") - .relation(ShapeRelation.WITHIN).indexedShapeIndex("shape_index").indexedShapePath("field"); + SearchRequestBuilder requestBuilder = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user6", USERS_PASSWD)) + ).prepareSearch("search_index"); + final ShapeQueryBuilder shapeQuery1 = new ShapeQueryBuilder("field", "1").relation(ShapeRelation.WITHIN) + .indexedShapeIndex("shape_index") + .indexedShapePath("field"); if (randomBoolean()) { - requestBuilder.setQuery(QueryBuilders.matchAllQuery()) - .setPostFilter(shapeQuery1); + requestBuilder.setQuery(QueryBuilders.matchAllQuery()).setPostFilter(shapeQuery1); } else { requestBuilder.setQuery(shapeQuery1); } @@ -499,33 +519,37 @@ public void testGeoQueryWithIndexedShapeWithFLS() { assertSearchResponse(result); assertHitCount(result, 1); // user sees the queried point but not the querying shape - final ShapeQueryBuilder shapeQuery2 = new ShapeQueryBuilder("field", "2") - .relation(ShapeRelation.WITHIN).indexedShapeIndex("shape_index").indexedShapePath("other"); + final ShapeQueryBuilder shapeQuery2 = new ShapeQueryBuilder("field", "2").relation(ShapeRelation.WITHIN) + .indexedShapeIndex("shape_index") + .indexedShapePath("other"); IllegalStateException e; if (randomBoolean()) { - e = expectThrows(IllegalStateException.class, () -> client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user6", USERS_PASSWD))) + e = expectThrows( + IllegalStateException.class, + () -> client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user6", USERS_PASSWD))) .prepareSearch("search_index") .setQuery(QueryBuilders.matchAllQuery()) .setPostFilter(shapeQuery2) - .get()); + .get() + ); } else { - e = expectThrows(IllegalStateException.class, () -> client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user6", USERS_PASSWD))) + e = expectThrows( + IllegalStateException.class, + () -> client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user6", USERS_PASSWD))) .prepareSearch("search_index") .setQuery(shapeQuery2) - .get()); + .get() + ); } assertThat(e.getMessage(), is("Shape with name [2] found but missing other field")); // user sees the querying shape but not the queried point - requestBuilder = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user6", USERS_PASSWD))) - .prepareSearch("search_index"); - final ShapeQueryBuilder shapeQuery3 = new ShapeQueryBuilder("other", "1") - .relation(ShapeRelation.WITHIN).indexedShapeIndex("shape_index").indexedShapePath("field"); + requestBuilder = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user6", USERS_PASSWD))) + .prepareSearch("search_index"); + final ShapeQueryBuilder shapeQuery3 = new ShapeQueryBuilder("other", "1").relation(ShapeRelation.WITHIN) + .indexedShapeIndex("shape_index") + .indexedShapePath("field"); if (randomBoolean()) { - requestBuilder.setQuery(QueryBuilders.matchAllQuery()) - .setPostFilter(shapeQuery3); + requestBuilder.setQuery(QueryBuilders.matchAllQuery()).setPostFilter(shapeQuery3); } else { requestBuilder.setQuery(shapeQuery3); } @@ -535,98 +559,83 @@ public void testGeoQueryWithIndexedShapeWithFLS() { } public void testTermsLookupOnIndexWithFLS() { - assertAcked(client().admin().indices().prepareCreate("search_index") - .setMapping("field", "type=keyword", "other", "type=text") - ); - assertAcked(client().admin().indices().prepareCreate("lookup_index") - .setMapping("field", "type=keyword", "other", "type=text") - ); - client().prepareIndex("search_index").setId("1").setSource("field", - List.of("value1", "value2")) - .setRefreshPolicy(IMMEDIATE) - .get(); - client().prepareIndex("search_index").setId("2").setSource("field", - "value1", "other", List.of("value1", "value2")) - .setRefreshPolicy(IMMEDIATE) - .get(); - client().prepareIndex("search_index").setId("3").setSource("field", - "value3", "other", List.of("value1", "value2")) - .setRefreshPolicy(IMMEDIATE) - .get(); - client().prepareIndex("lookup_index").setId("1").setSource("field", List.of("value1", "value2")) - .setRefreshPolicy(IMMEDIATE) - .get(); - client().prepareIndex("lookup_index").setId("2").setSource("other", "value2", "field", "value2") - .setRefreshPolicy(IMMEDIATE) - .get(); + assertAcked(client().admin().indices().prepareCreate("search_index").setMapping("field", "type=keyword", "other", "type=text")); + assertAcked(client().admin().indices().prepareCreate("lookup_index").setMapping("field", "type=keyword", "other", "type=text")); + client().prepareIndex("search_index").setId("1").setSource("field", List.of("value1", "value2")).setRefreshPolicy(IMMEDIATE).get(); + client().prepareIndex("search_index") + .setId("2") + .setSource("field", "value1", "other", List.of("value1", "value2")) + .setRefreshPolicy(IMMEDIATE) + .get(); + client().prepareIndex("search_index") + .setId("3") + .setSource("field", "value3", "other", List.of("value1", "value2")) + .setRefreshPolicy(IMMEDIATE) + .get(); + client().prepareIndex("lookup_index").setId("1").setSource("field", List.of("value1", "value2")).setRefreshPolicy(IMMEDIATE).get(); + client().prepareIndex("lookup_index").setId("2").setSource("other", "value2", "field", "value2").setRefreshPolicy(IMMEDIATE).get(); // user sees the terms doc field - SearchResponse response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user6", USERS_PASSWD))) - .prepareSearch("search_index") - .setQuery(QueryBuilders.termsLookupQuery("field", new TermsLookup("lookup_index", "1", "field"))) - .get(); + SearchResponse response = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user6", USERS_PASSWD)) + ) + .prepareSearch("search_index") + .setQuery(QueryBuilders.termsLookupQuery("field", new TermsLookup("lookup_index", "1", "field"))) + .get(); assertHitCount(response, 2); assertSearchHits(response, "1", "2"); - response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user6", USERS_PASSWD))) - .prepareSearch("search_index") - .setQuery(QueryBuilders.termsLookupQuery("field", new TermsLookup("lookup_index", "2", "field"))) - .get(); + response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user6", USERS_PASSWD))) + .prepareSearch("search_index") + .setQuery(QueryBuilders.termsLookupQuery("field", new TermsLookup("lookup_index", "2", "field"))) + .get(); assertHitCount(response, 1); assertSearchHits(response, "1"); // user does not see the terms doc field - response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user6", USERS_PASSWD))) - .prepareSearch("search_index") - .setQuery(QueryBuilders.termsLookupQuery("field", new TermsLookup("lookup_index", "2", "other"))) - .get(); + response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user6", USERS_PASSWD))) + .prepareSearch("search_index") + .setQuery(QueryBuilders.termsLookupQuery("field", new TermsLookup("lookup_index", "2", "other"))) + .get(); assertHitCount(response, 0); // user does not see the queried field - response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user6", USERS_PASSWD))) - .prepareSearch("search_index") - .setQuery(QueryBuilders.termsLookupQuery("other", new TermsLookup("lookup_index", "1", "field"))) - .get(); + response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user6", USERS_PASSWD))) + .prepareSearch("search_index") + .setQuery(QueryBuilders.termsLookupQuery("other", new TermsLookup("lookup_index", "1", "field"))) + .get(); assertHitCount(response, 0); } public void testGetApi() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test") - .setMapping("field1", "type=text", "field2", "type=text", "field3", "type=text") + assertAcked( + client().admin().indices().prepareCreate("test").setMapping("field1", "type=text", "field2", "type=text", "field3", "type=text") ); - client().prepareIndex("test").setId("1").setSource("field1", "value1", "field2", "value2", "field3", "value3") - .get(); + client().prepareIndex("test").setId("1").setSource("field1", "value1", "field2", "value2", "field3", "value3").get(); boolean realtime = randomBoolean(); // user1 is granted access to field1 only: - GetResponse response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareGet("test", "1") - .setRealtime(realtime) - .setRefresh(true) - .get(); + GetResponse response = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).prepareGet("test", "1").setRealtime(realtime).setRefresh(true).get(); assertThat(response.isExists(), is(true)); assertThat(response.getSource().size(), equalTo(1)); assertThat(response.getSource().get("field1").toString(), equalTo("value1")); // user2 is granted access to field2 only: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareGet("test", "1") - .setRealtime(realtime) - .setRefresh(true) - .get(); + .prepareGet("test", "1") + .setRealtime(realtime) + .setRefresh(true) + .get(); assertThat(response.isExists(), is(true)); assertThat(response.getSource().size(), equalTo(1)); assertThat(response.getSource().get("field2").toString(), equalTo("value2")); // user3 is granted access to field1 and field2: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) - .prepareGet("test", "1") - .setRealtime(realtime) - .setRefresh(true) - .get(); + .prepareGet("test", "1") + .setRealtime(realtime) + .setRefresh(true) + .get(); assertThat(response.isExists(), is(true)); assertThat(response.getSource().size(), equalTo(2)); assertThat(response.getSource().get("field1").toString(), equalTo("value1")); @@ -634,19 +643,19 @@ public void testGetApi() throws Exception { // user4 is granted access to no fields, so the get response does say the doc exist, but no fields are returned: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user4", USERS_PASSWD))) - .prepareGet("test", "1") - .setRealtime(realtime) - .setRefresh(true) - .get(); + .prepareGet("test", "1") + .setRealtime(realtime) + .setRefresh(true) + .get(); assertThat(response.isExists(), is(true)); assertThat(response.getSource().size(), equalTo(0)); // user5 has no field level security configured, so all fields are returned: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user5", USERS_PASSWD))) - .prepareGet("test", "1") - .setRealtime(realtime) - .setRefresh(true) - .get(); + .prepareGet("test", "1") + .setRealtime(realtime) + .setRefresh(true) + .get(); assertThat(response.isExists(), is(true)); assertThat(response.getSource().size(), equalTo(3)); assertThat(response.getSource().get("field1").toString(), equalTo("value1")); @@ -655,10 +664,10 @@ public void testGetApi() throws Exception { // user6 has access to field* response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user6", USERS_PASSWD))) - .prepareGet("test", "1") - .setRealtime(realtime) - .setRefresh(true) - .get(); + .prepareGet("test", "1") + .setRealtime(realtime) + .setRefresh(true) + .get(); assertThat(response.isExists(), is(true)); assertThat(response.getSource().size(), equalTo(3)); assertThat(response.getSource().get("field1").toString(), equalTo("value1")); @@ -667,10 +676,10 @@ public void testGetApi() throws Exception { // user7 has roles with field level security and without field level security response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user7", USERS_PASSWD))) - .prepareGet("test", "1") - .setRealtime(realtime) - .setRefresh(true) - .get(); + .prepareGet("test", "1") + .setRealtime(realtime) + .setRefresh(true) + .get(); assertThat(response.isExists(), is(true)); assertThat(response.getSource().size(), equalTo(3)); assertThat(response.getSource().get("field1").toString(), equalTo("value1")); @@ -679,10 +688,10 @@ public void testGetApi() throws Exception { // user8 has roles with field level security with access to field1 and field2 response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user8", USERS_PASSWD))) - .prepareGet("test", "1") - .setRealtime(realtime) - .setRefresh(true) - .get(); + .prepareGet("test", "1") + .setRealtime(realtime) + .setRefresh(true) + .get(); assertThat(response.isExists(), is(true)); assertThat(response.getSource().size(), equalTo(2)); assertThat(response.getSource().get("field1").toString(), equalTo("value1")); @@ -690,7 +699,10 @@ public void testGetApi() throws Exception { } public void testRealtimeGetApi() { - assertAcked(client().admin().indices().prepareCreate("test") + assertAcked( + client().admin() + .indices() + .prepareCreate("test") .setMapping("field1", "type=text", "field2", "type=text", "field3", "type=text") .setSettings(Settings.builder().put("refresh_interval", "-1").build()) ); @@ -700,65 +712,50 @@ public void testRealtimeGetApi() { client().prepareIndex("test").setId("1").setSource("field1", "value1", "field2", "value2", "field3", "value3").get(); // do a realtime get beforehand to flip an internal translog flag so that subsequent realtime gets are // served from the translog (this first one is NOT, it internally forces a refresh of the index) - client().prepareGet("test", "1") - .setRealtime(realtime) - .setRefresh(refresh) - .get(); + client().prepareGet("test", "1").setRealtime(realtime).setRefresh(refresh).get(); refresh("test"); // updates don't change the doc visibility for users // but updates populate the translog and the FLS filter must apply to the translog operations too if (randomBoolean()) { - client().prepareIndex("test").setId("1").setSource("field1", "value1", "field2", "value2", "field3", "value3") - .setRefreshPolicy(WriteRequest.RefreshPolicy.NONE).get(); + client().prepareIndex("test") + .setId("1") + .setSource("field1", "value1", "field2", "value2", "field3", "value3") + .setRefreshPolicy(WriteRequest.RefreshPolicy.NONE) + .get(); } else { - client().prepareUpdate("test", "1").setDoc(Map.of("field3", "value3")) - .setRefreshPolicy(WriteRequest.RefreshPolicy.NONE).get(); + client().prepareUpdate("test", "1").setDoc(Map.of("field3", "value3")).setRefreshPolicy(WriteRequest.RefreshPolicy.NONE).get(); } GetResponse getResponse; MultiGetResponse mgetResponse; // user1 is granted access to field1 only: if (randomBoolean()) { - getResponse = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareGet("test", "1") - .setRealtime(realtime) - .setRefresh(refresh) - .get(); + getResponse = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).prepareGet("test", "1").setRealtime(realtime).setRefresh(refresh).get(); assertThat(getResponse.isExists(), is(true)); assertThat(getResponse.getSource().size(), equalTo(1)); assertThat(getResponse.getSource().get("field1").toString(), equalTo("value1")); } else { - mgetResponse = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareMultiGet() - .add("test", "1") - .setRealtime(realtime) - .setRefresh(refresh) - .get(); + mgetResponse = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).prepareMultiGet().add("test", "1").setRealtime(realtime).setRefresh(refresh).get(); assertThat(mgetResponse.getResponses()[0].getResponse().isExists(), is(true)); assertThat(mgetResponse.getResponses()[0].getResponse().getSource().size(), equalTo(1)); assertThat(mgetResponse.getResponses()[0].getResponse().getSource().get("field1").toString(), equalTo("value1")); } // user2 is granted access to field2 only: if (randomBoolean()) { - getResponse = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareGet("test", "1") - .setRealtime(realtime) - .setRefresh(refresh) - .get(); + getResponse = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD)) + ).prepareGet("test", "1").setRealtime(realtime).setRefresh(refresh).get(); assertThat(getResponse.isExists(), is(true)); assertThat(getResponse.getSource().size(), equalTo(1)); assertThat(getResponse.getSource().get("field2").toString(), equalTo("value2")); } else { - mgetResponse = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareMultiGet() - .add("test", "1") - .setRealtime(realtime) - .setRefresh(refresh) - .get(); + mgetResponse = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD)) + ).prepareMultiGet().add("test", "1").setRealtime(realtime).setRefresh(refresh).get(); assertThat(mgetResponse.getResponses()[0].getResponse().isExists(), is(true)); assertThat(mgetResponse.getResponses()[0].getResponse().getSource().size(), equalTo(1)); assertThat(mgetResponse.getResponses()[0].getResponse().getSource().get("field2").toString(), equalTo("value2")); @@ -766,20 +763,16 @@ public void testRealtimeGetApi() { } public void testMGetApi() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test") - .setMapping("field1", "type=text", "field2", "type=text", "field3", "type=text") + assertAcked( + client().admin().indices().prepareCreate("test").setMapping("field1", "type=text", "field2", "type=text", "field3", "type=text") ); client().prepareIndex("test").setId("1").setSource("field1", "value1", "field2", "value2", "field3", "value3").get(); boolean realtime = randomBoolean(); // user1 is granted access to field1 only: - MultiGetResponse response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareMultiGet() - .add("test", "1") - .setRealtime(realtime) - .setRefresh(true) - .get(); + MultiGetResponse response = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).prepareMultiGet().add("test", "1").setRealtime(realtime).setRefresh(true).get(); assertThat(response.getResponses()[0].isFailed(), is(false)); assertThat(response.getResponses()[0].getResponse().isExists(), is(true)); assertThat(response.getResponses()[0].getResponse().getSource().size(), equalTo(1)); @@ -787,11 +780,11 @@ public void testMGetApi() throws Exception { // user2 is granted access to field2 only: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareMultiGet() - .add("test", "1") - .setRealtime(realtime) - .setRefresh(true) - .get(); + .prepareMultiGet() + .add("test", "1") + .setRealtime(realtime) + .setRefresh(true) + .get(); assertThat(response.getResponses()[0].isFailed(), is(false)); assertThat(response.getResponses()[0].getResponse().isExists(), is(true)); assertThat(response.getResponses()[0].getResponse().getSource().size(), equalTo(1)); @@ -799,11 +792,11 @@ public void testMGetApi() throws Exception { // user3 is granted access to field1 and field2: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) - .prepareMultiGet() - .add("test", "1") - .setRealtime(realtime) - .setRefresh(true) - .get(); + .prepareMultiGet() + .add("test", "1") + .setRealtime(realtime) + .setRefresh(true) + .get(); assertThat(response.getResponses()[0].isFailed(), is(false)); assertThat(response.getResponses()[0].getResponse().isExists(), is(true)); assertThat(response.getResponses()[0].getResponse().getSource().size(), equalTo(2)); @@ -812,22 +805,22 @@ public void testMGetApi() throws Exception { // user4 is granted access to no fields, so the get response does say the doc exist, but no fields are returned: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user4", USERS_PASSWD))) - .prepareMultiGet() - .add("test", "1") - .setRealtime(realtime) - .setRefresh(true) - .get(); + .prepareMultiGet() + .add("test", "1") + .setRealtime(realtime) + .setRefresh(true) + .get(); assertThat(response.getResponses()[0].isFailed(), is(false)); assertThat(response.getResponses()[0].getResponse().isExists(), is(true)); assertThat(response.getResponses()[0].getResponse().getSource().size(), equalTo(0)); // user5 has no field level security configured, so all fields are returned: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user5", USERS_PASSWD))) - .prepareMultiGet() - .add("test", "1") - .setRealtime(realtime) - .setRefresh(true) - .get(); + .prepareMultiGet() + .add("test", "1") + .setRealtime(realtime) + .setRefresh(true) + .get(); assertThat(response.getResponses()[0].isFailed(), is(false)); assertThat(response.getResponses()[0].getResponse().isExists(), is(true)); assertThat(response.getResponses()[0].getResponse().getSource().size(), equalTo(3)); @@ -837,11 +830,11 @@ public void testMGetApi() throws Exception { // user6 has access to field* response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user6", USERS_PASSWD))) - .prepareMultiGet() - .add("test", "1") - .setRealtime(realtime) - .setRefresh(true) - .get(); + .prepareMultiGet() + .add("test", "1") + .setRealtime(realtime) + .setRefresh(true) + .get(); assertThat(response.getResponses()[0].isFailed(), is(false)); assertThat(response.getResponses()[0].getResponse().isExists(), is(true)); assertThat(response.getResponses()[0].getResponse().getSource().size(), equalTo(3)); @@ -851,11 +844,11 @@ public void testMGetApi() throws Exception { // user7 has roles with field level security and without field level security response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user7", USERS_PASSWD))) - .prepareMultiGet() - .add("test", "1") - .setRealtime(realtime) - .setRefresh(true) - .get(); + .prepareMultiGet() + .add("test", "1") + .setRealtime(realtime) + .setRefresh(true) + .get(); assertThat(response.getResponses()[0].isFailed(), is(false)); assertThat(response.getResponses()[0].getResponse().isExists(), is(true)); assertThat(response.getResponses()[0].getResponse().getSource().size(), equalTo(3)); @@ -865,11 +858,11 @@ public void testMGetApi() throws Exception { // user8 has roles with field level security with access to field1 and field2 response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user8", USERS_PASSWD))) - .prepareMultiGet() - .add("test", "1") - .setRealtime(realtime) - .setRefresh(true) - .get(); + .prepareMultiGet() + .add("test", "1") + .setRealtime(realtime) + .setRefresh(true) + .get(); assertThat(response.getResponses()[0].isFailed(), is(false)); assertThat(response.getResponses()[0].getResponse().isExists(), is(true)); assertThat(response.getResponses()[0].getResponse().getSource().size(), equalTo(2)); @@ -878,26 +871,31 @@ public void testMGetApi() throws Exception { } public void testMSearchApi() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test1") + assertAcked( + client().admin() + .indices() + .prepareCreate("test1") .setMapping("field1", "type=text", "field2", "type=text", "field3", "type=text") ); - assertAcked(client().admin().indices().prepareCreate("test2") + assertAcked( + client().admin() + .indices() + .prepareCreate("test2") .setMapping("field1", "type=text", "field2", "type=text", "field3", "type=text") ); - client().prepareIndex("test1").setId("1") - .setSource("field1", "value1", "field2", "value2", "field3", "value3").get(); - client().prepareIndex("test2").setId("1") - .setSource("field1", "value1", "field2", "value2", "field3", "value3").get(); + client().prepareIndex("test1").setId("1").setSource("field1", "value1", "field2", "value2", "field3", "value3").get(); + client().prepareIndex("test2").setId("1").setSource("field1", "value1", "field2", "value2", "field3", "value3").get(); client().admin().indices().prepareRefresh("test1", "test2").get(); // user1 is granted access to field1 only - MultiSearchResponse response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareMultiSearch() - .add(client().prepareSearch("test1").setQuery(QueryBuilders.matchAllQuery())) - .add(client().prepareSearch("test2").setQuery(QueryBuilders.matchAllQuery())) - .get(); + MultiSearchResponse response = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ) + .prepareMultiSearch() + .add(client().prepareSearch("test1").setQuery(QueryBuilders.matchAllQuery())) + .add(client().prepareSearch("test2").setQuery(QueryBuilders.matchAllQuery())) + .get(); assertFalse(response.getResponses()[0].isFailure()); assertThat(response.getResponses()[0].getResponse().getHits().getTotalHits().value, is(1L)); assertThat(response.getResponses()[0].getResponse().getHits().getAt(0).getSourceAsMap().size(), is(1)); @@ -907,12 +905,11 @@ public void testMSearchApi() throws Exception { assertThat(response.getResponses()[1].getResponse().getHits().getAt(0).getSourceAsMap().get("field1"), is("value1")); // user2 is granted access to field2 only - response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareMultiSearch() - .add(client().prepareSearch("test1").setQuery(QueryBuilders.matchAllQuery())) - .add(client().prepareSearch("test2").setQuery(QueryBuilders.matchAllQuery())) - .get(); + response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) + .prepareMultiSearch() + .add(client().prepareSearch("test1").setQuery(QueryBuilders.matchAllQuery())) + .add(client().prepareSearch("test2").setQuery(QueryBuilders.matchAllQuery())) + .get(); assertFalse(response.getResponses()[0].isFailure()); assertThat(response.getResponses()[0].getResponse().getHits().getTotalHits().value, is(1L)); assertThat(response.getResponses()[0].getResponse().getHits().getAt(0).getSourceAsMap().size(), is(1)); @@ -922,12 +919,11 @@ public void testMSearchApi() throws Exception { assertThat(response.getResponses()[1].getResponse().getHits().getAt(0).getSourceAsMap().get("field2"), is("value2")); // user3 is granted access to field1 and field2 - response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) - .prepareMultiSearch() - .add(client().prepareSearch("test1").setQuery(QueryBuilders.matchAllQuery())) - .add(client().prepareSearch("test2").setQuery(QueryBuilders.matchAllQuery())) - .get(); + response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) + .prepareMultiSearch() + .add(client().prepareSearch("test1").setQuery(QueryBuilders.matchAllQuery())) + .add(client().prepareSearch("test2").setQuery(QueryBuilders.matchAllQuery())) + .get(); assertFalse(response.getResponses()[0].isFailure()); assertThat(response.getResponses()[0].getResponse().getHits().getTotalHits().value, is(1L)); assertThat(response.getResponses()[0].getResponse().getHits().getAt(0).getSourceAsMap().size(), is(2)); @@ -939,12 +935,11 @@ public void testMSearchApi() throws Exception { assertThat(response.getResponses()[1].getResponse().getHits().getAt(0).getSourceAsMap().get("field2"), is("value2")); // user4 is granted access to no fields, so the search response does say the doc exist, but no fields are returned - response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user4", USERS_PASSWD))) - .prepareMultiSearch() - .add(client().prepareSearch("test1").setQuery(QueryBuilders.matchAllQuery())) - .add(client().prepareSearch("test2").setQuery(QueryBuilders.matchAllQuery())) - .get(); + response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user4", USERS_PASSWD))) + .prepareMultiSearch() + .add(client().prepareSearch("test1").setQuery(QueryBuilders.matchAllQuery())) + .add(client().prepareSearch("test2").setQuery(QueryBuilders.matchAllQuery())) + .get(); assertFalse(response.getResponses()[0].isFailure()); assertThat(response.getResponses()[0].getResponse().getHits().getTotalHits().value, is(1L)); assertThat(response.getResponses()[0].getResponse().getHits().getAt(0).getSourceAsMap().size(), is(0)); @@ -952,12 +947,11 @@ public void testMSearchApi() throws Exception { assertThat(response.getResponses()[1].getResponse().getHits().getAt(0).getSourceAsMap().size(), is(0)); // user5 has no field level security configured, so all fields are returned - response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user5", USERS_PASSWD))) - .prepareMultiSearch() - .add(client().prepareSearch("test1").setQuery(QueryBuilders.matchAllQuery())) - .add(client().prepareSearch("test2").setQuery(QueryBuilders.matchAllQuery())) - .get(); + response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user5", USERS_PASSWD))) + .prepareMultiSearch() + .add(client().prepareSearch("test1").setQuery(QueryBuilders.matchAllQuery())) + .add(client().prepareSearch("test2").setQuery(QueryBuilders.matchAllQuery())) + .get(); assertFalse(response.getResponses()[0].isFailure()); assertThat(response.getResponses()[0].getResponse().getHits().getTotalHits().value, is(1L)); assertThat(response.getResponses()[0].getResponse().getHits().getAt(0).getSourceAsMap().size(), is(3)); @@ -971,12 +965,11 @@ public void testMSearchApi() throws Exception { assertThat(response.getResponses()[1].getResponse().getHits().getAt(0).getSourceAsMap().get("field3"), is("value3")); // user6 has access to field* - response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user6", USERS_PASSWD))) - .prepareMultiSearch() - .add(client().prepareSearch("test1").setQuery(QueryBuilders.matchAllQuery())) - .add(client().prepareSearch("test2").setQuery(QueryBuilders.matchAllQuery())) - .get(); + response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user6", USERS_PASSWD))) + .prepareMultiSearch() + .add(client().prepareSearch("test1").setQuery(QueryBuilders.matchAllQuery())) + .add(client().prepareSearch("test2").setQuery(QueryBuilders.matchAllQuery())) + .get(); assertFalse(response.getResponses()[0].isFailure()); assertThat(response.getResponses()[0].getResponse().getHits().getTotalHits().value, is(1L)); assertThat(response.getResponses()[0].getResponse().getHits().getAt(0).getSourceAsMap().size(), is(3)); @@ -990,12 +983,11 @@ public void testMSearchApi() throws Exception { assertThat(response.getResponses()[1].getResponse().getHits().getAt(0).getSourceAsMap().get("field3"), is("value3")); // user7 has roles with field level security and without field level security - response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user7", USERS_PASSWD))) - .prepareMultiSearch() - .add(client().prepareSearch("test1").setQuery(QueryBuilders.matchAllQuery())) - .add(client().prepareSearch("test2").setQuery(QueryBuilders.matchAllQuery())) - .get(); + response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user7", USERS_PASSWD))) + .prepareMultiSearch() + .add(client().prepareSearch("test1").setQuery(QueryBuilders.matchAllQuery())) + .add(client().prepareSearch("test2").setQuery(QueryBuilders.matchAllQuery())) + .get(); assertFalse(response.getResponses()[0].isFailure()); assertThat(response.getResponses()[0].getResponse().getHits().getTotalHits().value, is(1L)); assertThat(response.getResponses()[0].getResponse().getHits().getAt(0).getSourceAsMap().size(), is(3)); @@ -1009,12 +1001,11 @@ public void testMSearchApi() throws Exception { assertThat(response.getResponses()[1].getResponse().getHits().getAt(0).getSourceAsMap().get("field3"), is("value3")); // user8 has roles with field level security with access to field1 and field2 - response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user8", USERS_PASSWD))) - .prepareMultiSearch() - .add(client().prepareSearch("test1").setQuery(QueryBuilders.matchAllQuery())) - .add(client().prepareSearch("test2").setQuery(QueryBuilders.matchAllQuery())) - .get(); + response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user8", USERS_PASSWD))) + .prepareMultiSearch() + .add(client().prepareSearch("test1").setQuery(QueryBuilders.matchAllQuery())) + .add(client().prepareSearch("test2").setQuery(QueryBuilders.matchAllQuery())) + .get(); assertFalse(response.getResponses()[0].isFailure()); assertThat(response.getResponses()[0].getResponse().getHits().getTotalHits().value, is(1L)); assertThat(response.getResponses()[0].getResponse().getHits().getAt(0).getSourceAsMap().size(), is(2)); @@ -1027,30 +1018,32 @@ public void testMSearchApi() throws Exception { } public void testScroll() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test") - .setSettings(Settings.builder() - .put(IndexModule.INDEX_QUERY_CACHE_EVERYTHING_SETTING.getKey(), true)) + assertAcked( + client().admin() + .indices() + .prepareCreate("test") + .setSettings(Settings.builder().put(IndexModule.INDEX_QUERY_CACHE_EVERYTHING_SETTING.getKey(), true)) .setMapping("field1", "type=text", "field2", "type=text", "field3", "type=text") ); final int numDocs = scaledRandomIntBetween(2, 10); for (int i = 0; i < numDocs; i++) { - client().prepareIndex("test").setId(String.valueOf(i)) - .setSource("field1", "value1", "field2", "value2", "field3", "value3") - .get(); + client().prepareIndex("test") + .setId(String.valueOf(i)) + .setSource("field1", "value1", "field2", "value2", "field3", "value3") + .get(); } refresh("test"); SearchResponse response = null; try { - response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareSearch("test") - .setScroll(TimeValue.timeValueMinutes(1L)) - .setSize(1) - .setQuery(constantScoreQuery(termQuery("field1", "value1"))) - .setFetchSource(true) - .get(); + response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) + .prepareSearch("test") + .setScroll(TimeValue.timeValueMinutes(1L)) + .setSize(1) + .setQuery(constantScoreQuery(termQuery("field1", "value1"))) + .setFetchSource(true) + .get(); do { assertThat(response.getHits().getTotalHits().value, is((long) numDocs)); @@ -1062,11 +1055,9 @@ public void testScroll() throws Exception { break; } - response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareSearchScroll(response.getScrollId()) - .setScroll(TimeValue.timeValueMinutes(1L)) - .get(); + response = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).prepareSearchScroll(response.getScrollId()).setScroll(TimeValue.timeValueMinutes(1L)).get(); } while (response.getHits().getHits().length > 0); } finally { @@ -1081,21 +1072,25 @@ public void testScroll() throws Exception { static String openPointInTime(String userName, TimeValue keepAlive, String... indices) { OpenPointInTimeRequest request = new OpenPointInTimeRequest(indices).keepAlive(keepAlive); - final OpenPointInTimeResponse response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue(userName, USERS_PASSWD))) - .execute(OpenPointInTimeAction.INSTANCE, request).actionGet(); + final OpenPointInTimeResponse response = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue(userName, USERS_PASSWD)) + ).execute(OpenPointInTimeAction.INSTANCE, request).actionGet(); return response.getPointInTimeId(); } public void testPointInTimeId() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test") - .setSettings(Settings.builder().put(IndexModule.INDEX_QUERY_CACHE_EVERYTHING_SETTING.getKey(), true)) - .setMapping("field1", "type=text", "field2", "type=text", "field3", "type=text") + assertAcked( + client().admin() + .indices() + .prepareCreate("test") + .setSettings(Settings.builder().put(IndexModule.INDEX_QUERY_CACHE_EVERYTHING_SETTING.getKey(), true)) + .setMapping("field1", "type=text", "field2", "type=text", "field3", "type=text") ); final int numDocs = scaledRandomIntBetween(2, 10); for (int i = 0; i < numDocs; i++) { - client().prepareIndex("test").setId(String.valueOf(i)) + client().prepareIndex("test") + .setId(String.valueOf(i)) .setSource("field1", "value1", "field2", "value2", "field3", "value3") .get(); } @@ -1105,8 +1100,9 @@ public void testPointInTimeId() throws Exception { SearchResponse response = null; try { for (int from = 0; from < numDocs; from++) { - response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) + response = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ) .prepareSearch() .setPointInTime(new PointInTimeBuilder(pitId)) .setSize(1) @@ -1125,35 +1121,36 @@ public void testPointInTimeId() throws Exception { } public void testQueryCache() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test") - .setSettings(Settings.builder().put(IndexModule.INDEX_QUERY_CACHE_EVERYTHING_SETTING.getKey(), true)) - .setMapping("field1", "type=text", "field2", "type=text", "field3", "type=text") + assertAcked( + client().admin() + .indices() + .prepareCreate("test") + .setSettings(Settings.builder().put(IndexModule.INDEX_QUERY_CACHE_EVERYTHING_SETTING.getKey(), true)) + .setMapping("field1", "type=text", "field2", "type=text", "field3", "type=text") ); - client().prepareIndex("test").setId("1").setSource("field1", "value1", "field2", "value2", "field3", "value3") - .setRefreshPolicy(IMMEDIATE) - .get(); + client().prepareIndex("test") + .setId("1") + .setSource("field1", "value1", "field2", "value2", "field3", "value3") + .setRefreshPolicy(IMMEDIATE) + .get(); int max = scaledRandomIntBetween(4, 32); for (int i = 0; i < max; i++) { - SearchResponse response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(constantScoreQuery(termQuery("field1", "value1"))) - .get(); + SearchResponse response = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).prepareSearch("test").setQuery(constantScoreQuery(termQuery("field1", "value1"))).get(); assertHitCount(response, 1); assertThat(response.getHits().getAt(0).getSourceAsMap().size(), is(1)); assertThat(response.getHits().getAt(0).getSourceAsMap().get("field1"), is("value1")); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(constantScoreQuery(termQuery("field1", "value1"))) - .get(); + .prepareSearch("test") + .setQuery(constantScoreQuery(termQuery("field1", "value1"))) + .get(); assertHitCount(response, 0); String multipleFieldsUser = randomFrom("user5", "user6", "user7"); - response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue(multipleFieldsUser, USERS_PASSWD))) - .prepareSearch("test") - .setQuery(constantScoreQuery(termQuery("field1", "value1"))) - .get(); + response = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue(multipleFieldsUser, USERS_PASSWD)) + ).prepareSearch("test").setQuery(constantScoreQuery(termQuery("field1", "value1"))).get(); assertHitCount(response, 1); assertThat(response.getHits().getAt(0).getSourceAsMap().size(), is(3)); assertThat(response.getHits().getAt(0).getSourceAsMap().get("field1"), is("value1")); @@ -1163,16 +1160,17 @@ public void testQueryCache() throws Exception { } public void testScrollWithQueryCache() { - assertAcked(client().admin().indices().prepareCreate("test") + assertAcked( + client().admin() + .indices() + .prepareCreate("test") .setSettings(Settings.builder().put(IndexModule.INDEX_QUERY_CACHE_EVERYTHING_SETTING.getKey(), true)) .setMapping("field1", "type=text", "field2", "type=text") ); final int numDocs = scaledRandomIntBetween(2, 4); for (int i = 0; i < numDocs; i++) { - client().prepareIndex("test").setId(String.valueOf(i)) - .setSource("field1", "value1", "field2", "value2") - .get(); + client().prepareIndex("test").setId(String.valueOf(i)).setSource("field1", "value1", "field2", "value2").get(); } refresh("test"); @@ -1187,24 +1185,22 @@ public void testScrollWithQueryCache() { for (int i = 0; i < numScrollSearch; i++) { if (randomBoolean()) { if (user2SearchResponse == null) { - user2SearchResponse = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue( - "user2", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(cacheableQueryBuilder) - .setScroll(TimeValue.timeValueMinutes(10L)) - .setSize(1) - .setFetchSource(true) - .get(); + user2SearchResponse = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD)) + ) + .prepareSearch("test") + .setQuery(cacheableQueryBuilder) + .setScroll(TimeValue.timeValueMinutes(10L)) + .setSize(1) + .setFetchSource(true) + .get(); assertThat(user2SearchResponse.getHits().getTotalHits().value, is((long) 0)); assertThat(user2SearchResponse.getHits().getHits().length, is(0)); } else { // make sure scroll is empty - user2SearchResponse = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", - USERS_PASSWD))) - .prepareSearchScroll(user2SearchResponse.getScrollId()) - .setScroll(TimeValue.timeValueMinutes(10L)) - .get(); + user2SearchResponse = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD)) + ).prepareSearchScroll(user2SearchResponse.getScrollId()).setScroll(TimeValue.timeValueMinutes(10L)).get(); assertThat(user2SearchResponse.getHits().getTotalHits().value, is((long) 0)); assertThat(user2SearchResponse.getHits().getHits().length, is(0)); if (randomBoolean()) { @@ -1215,25 +1211,24 @@ public void testScrollWithQueryCache() { } } else { if (user1SearchResponse == null) { - user1SearchResponse = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue( - "user1", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(cacheableQueryBuilder) - .setScroll(TimeValue.timeValueMinutes(10L)) - .setSize(1) - .setFetchSource(true) - .get(); + user1SearchResponse = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ) + .prepareSearch("test") + .setQuery(cacheableQueryBuilder) + .setScroll(TimeValue.timeValueMinutes(10L)) + .setSize(1) + .setFetchSource(true) + .get(); assertThat(user1SearchResponse.getHits().getTotalHits().value, is((long) numDocs)); assertThat(user1SearchResponse.getHits().getHits().length, is(1)); assertThat(user1SearchResponse.getHits().getAt(0).getSourceAsMap().size(), is(1)); assertThat(user1SearchResponse.getHits().getAt(0).getSourceAsMap().get("field1"), is("value1")); scrolledDocsUser1++; } else { - user1SearchResponse = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareSearchScroll(user1SearchResponse.getScrollId()) - .setScroll(TimeValue.timeValueMinutes(10L)) - .get(); + user1SearchResponse = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).prepareSearchScroll(user1SearchResponse.getScrollId()).setScroll(TimeValue.timeValueMinutes(10L)).get(); assertThat(user1SearchResponse.getHits().getTotalHits().value, is((long) numDocs)); if (scrolledDocsUser1 < numDocs) { assertThat(user1SearchResponse.getHits().getHits().length, is(1)); @@ -1271,105 +1266,106 @@ public void testScrollWithQueryCache() { } public void testRequestCache() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test") - .setSettings(Settings.builder().put(IndicesRequestCache.INDEX_CACHE_REQUEST_ENABLED_SETTING.getKey(), true)) - .setMapping("field1", "type=text", "field2", "type=text", "field3", "type=text") + assertAcked( + client().admin() + .indices() + .prepareCreate("test") + .setSettings(Settings.builder().put(IndicesRequestCache.INDEX_CACHE_REQUEST_ENABLED_SETTING.getKey(), true)) + .setMapping("field1", "type=text", "field2", "type=text", "field3", "type=text") ); - client().prepareIndex("test").setId("1").setSource("field1", "value1", "field2", "value2") - .setRefreshPolicy(IMMEDIATE) - .get(); + client().prepareIndex("test").setId("1").setSource("field1", "value1", "field2", "value2").setRefreshPolicy(IMMEDIATE).get(); int max = scaledRandomIntBetween(4, 32); for (int i = 0; i < max; i++) { Boolean requestCache = randomFrom(true, null); - SearchResponse response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareSearch("test") - .setSize(0) - .setQuery(termQuery("field1", "value1")) - .setRequestCache(requestCache) - .get(); + SearchResponse response = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).prepareSearch("test").setSize(0).setQuery(termQuery("field1", "value1")).setRequestCache(requestCache).get(); assertNoFailures(response); assertHitCount(response, 1); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareSearch("test") - .setSize(0) - .setQuery(termQuery("field1", "value1")) - .setRequestCache(requestCache) - .get(); + .prepareSearch("test") + .setSize(0) + .setQuery(termQuery("field1", "value1")) + .setRequestCache(requestCache) + .get(); assertNoFailures(response); assertHitCount(response, 0); String multipleFieldsUser = randomFrom("user5", "user6", "user7"); - response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue(multipleFieldsUser, USERS_PASSWD))) - .prepareSearch("test") - .setSize(0) - .setQuery(termQuery("field1", "value1")) - .setRequestCache(requestCache) - .get(); + response = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue(multipleFieldsUser, USERS_PASSWD)) + ).prepareSearch("test").setSize(0).setQuery(termQuery("field1", "value1")).setRequestCache(requestCache).get(); assertNoFailures(response); assertHitCount(response, 1); } } public void testFields() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test").setMapping( - "field1", "type=text,store=true", - "field2", "type=text,store=true", - "field3", "type=text,store=true", - "alias", "type=alias,path=field1")); - client().prepareIndex("test").setId("1").setSource("field1", "value1", "field2", "value2", "field3", "value3") - .setRefreshPolicy(IMMEDIATE) - .get(); + assertAcked( + client().admin() + .indices() + .prepareCreate("test") + .setMapping( + "field1", + "type=text,store=true", + "field2", + "type=text,store=true", + "field3", + "type=text,store=true", + "alias", + "type=alias,path=field1" + ) + ); + client().prepareIndex("test") + .setId("1") + .setSource("field1", "value1", "field2", "value2", "field3", "value3") + .setRefreshPolicy(IMMEDIATE) + .get(); // user1 is granted access to field1 only: - SearchResponse response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareSearch("test") - .addStoredField("field1") - .addStoredField("field2") - .addStoredField("field3") - .get(); + SearchResponse response = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).prepareSearch("test").addStoredField("field1").addStoredField("field2").addStoredField("field3").get(); assertThat(response.getHits().getAt(0).getFields().size(), equalTo(1)); assertThat(response.getHits().getAt(0).getFields().get("field1").getValue(), equalTo("value1")); // user2 is granted access to field2 only: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareSearch("test") - .addStoredField("field1") - .addStoredField("field2") - .addStoredField("field3") - .get(); + .prepareSearch("test") + .addStoredField("field1") + .addStoredField("field2") + .addStoredField("field3") + .get(); assertThat(response.getHits().getAt(0).getFields().size(), equalTo(1)); assertThat(response.getHits().getAt(0).getFields().get("field2").getValue(), equalTo("value2")); // user3 is granted access to field1 and field2: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) - .prepareSearch("test") - .addStoredField("field1") - .addStoredField("field2") - .addStoredField("field3") - .get(); + .prepareSearch("test") + .addStoredField("field1") + .addStoredField("field2") + .addStoredField("field3") + .get(); assertThat(response.getHits().getAt(0).getFields().size(), equalTo(2)); assertThat(response.getHits().getAt(0).getFields().get("field1").getValue(), equalTo("value1")); assertThat(response.getHits().getAt(0).getFields().get("field2").getValue(), equalTo("value2")); // user4 is granted access to no fields: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user4", USERS_PASSWD))) - .prepareSearch("test") - .addStoredField("field1") - .addStoredField("field2") - .addStoredField("field3") - .get(); + .prepareSearch("test") + .addStoredField("field1") + .addStoredField("field2") + .addStoredField("field3") + .get(); assertThat(response.getHits().getAt(0).getFields().size(), equalTo(0)); // user5 has no field level security configured: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user5", USERS_PASSWD))) - .prepareSearch("test") - .addStoredField("field1") - .addStoredField("field2") - .addStoredField("field3") - .get(); + .prepareSearch("test") + .addStoredField("field1") + .addStoredField("field2") + .addStoredField("field3") + .get(); assertThat(response.getHits().getAt(0).getFields().size(), equalTo(3)); assertThat(response.getHits().getAt(0).getFields().get("field1").getValue(), equalTo("value1")); assertThat(response.getHits().getAt(0).getFields().get("field2").getValue(), equalTo("value2")); @@ -1377,11 +1373,11 @@ public void testFields() throws Exception { // user6 has field level security configured with access to field*: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user6", USERS_PASSWD))) - .prepareSearch("test") - .addStoredField("field1") - .addStoredField("field2") - .addStoredField("field3") - .get(); + .prepareSearch("test") + .addStoredField("field1") + .addStoredField("field2") + .addStoredField("field3") + .get(); assertThat(response.getHits().getAt(0).getFields().size(), equalTo(3)); assertThat(response.getHits().getAt(0).getFields().get("field1").getValue(), equalTo("value1")); assertThat(response.getHits().getAt(0).getFields().get("field2").getValue(), equalTo("value2")); @@ -1389,11 +1385,11 @@ public void testFields() throws Exception { // user7 has access to all fields due to a mix of roles without field level security and with: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user7", USERS_PASSWD))) - .prepareSearch("test") - .addStoredField("field1") - .addStoredField("field2") - .addStoredField("field3") - .get(); + .prepareSearch("test") + .addStoredField("field1") + .addStoredField("field2") + .addStoredField("field3") + .get(); assertThat(response.getHits().getAt(0).getFields().size(), equalTo(3)); assertThat(response.getHits().getAt(0).getFields().get("field1").getValue(), equalTo("value1")); assertThat(response.getHits().getAt(0).getFields().get("field2").getValue(), equalTo("value2")); @@ -1401,73 +1397,73 @@ public void testFields() throws Exception { // user8 has field level security configured with access to field1 and field2: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user8", USERS_PASSWD))) - .prepareSearch("test") - .addStoredField("field1") - .addStoredField("field2") - .addStoredField("field3") - .get(); + .prepareSearch("test") + .addStoredField("field1") + .addStoredField("field2") + .addStoredField("field3") + .get(); assertThat(response.getHits().getAt(0).getFields().size(), equalTo(2)); assertThat(response.getHits().getAt(0).getFields().get("field1").getValue(), equalTo("value1")); assertThat(response.getHits().getAt(0).getFields().get("field2").getValue(), equalTo("value2")); // user1 is granted access to field1 only, and so should be able to load it by alias: - response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareSearch("test") - .addStoredField("alias") - .get(); + response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) + .prepareSearch("test") + .addStoredField("alias") + .get(); assertThat(response.getHits().getAt(0).getFields().size(), equalTo(1)); assertThat(response.getHits().getAt(0).getFields().get("alias").getValue(), equalTo("value1")); // user2 is not granted access to field1, and so should not be able to load it by alias: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareSearch("test") - .addStoredField("alias") - .get(); + .prepareSearch("test") + .addStoredField("alias") + .get(); assertThat(response.getHits().getAt(0).getFields().size(), equalTo(0)); } public void testSource() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test") - .setMapping("field1", "type=text", "field2", "type=text", "field3", "type=text") + assertAcked( + client().admin().indices().prepareCreate("test").setMapping("field1", "type=text", "field2", "type=text", "field3", "type=text") ); - client().prepareIndex("test").setId("1").setSource("field1", "value1", "field2", "value2", "field3", "value3") - .setRefreshPolicy(IMMEDIATE) - .get(); + client().prepareIndex("test") + .setId("1") + .setSource("field1", "value1", "field2", "value2", "field3", "value3") + .setRefreshPolicy(IMMEDIATE) + .get(); // user1 is granted access to field1 only: - SearchResponse response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareSearch("test") - .get(); + SearchResponse response = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).prepareSearch("test").get(); assertThat(response.getHits().getAt(0).getSourceAsMap().size(), equalTo(1)); assertThat(response.getHits().getAt(0).getSourceAsMap().get("field1").toString(), equalTo("value1")); // user2 is granted access to field2 only: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareSearch("test") - .get(); + .prepareSearch("test") + .get(); assertThat(response.getHits().getAt(0).getSourceAsMap().size(), equalTo(1)); assertThat(response.getHits().getAt(0).getSourceAsMap().get("field2").toString(), equalTo("value2")); // user3 is granted access to field1 and field2: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) - .prepareSearch("test") - .get(); + .prepareSearch("test") + .get(); assertThat(response.getHits().getAt(0).getSourceAsMap().size(), equalTo(2)); assertThat(response.getHits().getAt(0).getSourceAsMap().get("field1").toString(), equalTo("value1")); assertThat(response.getHits().getAt(0).getSourceAsMap().get("field2").toString(), equalTo("value2")); // user4 is granted access to no fields: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user4", USERS_PASSWD))) - .prepareSearch("test") - .get(); + .prepareSearch("test") + .get(); assertThat(response.getHits().getAt(0).getSourceAsMap().size(), equalTo(0)); // user5 has no field level security configured: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user5", USERS_PASSWD))) - .prepareSearch("test") - .get(); + .prepareSearch("test") + .get(); assertThat(response.getHits().getAt(0).getSourceAsMap().size(), equalTo(3)); assertThat(response.getHits().getAt(0).getSourceAsMap().get("field1").toString(), equalTo("value1")); assertThat(response.getHits().getAt(0).getSourceAsMap().get("field2").toString(), equalTo("value2")); @@ -1475,8 +1471,8 @@ public void testSource() throws Exception { // user6 has field level security configured with access to field*: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user6", USERS_PASSWD))) - .prepareSearch("test") - .get(); + .prepareSearch("test") + .get(); assertThat(response.getHits().getAt(0).getSourceAsMap().size(), equalTo(3)); assertThat(response.getHits().getAt(0).getSourceAsMap().get("field1").toString(), equalTo("value1")); assertThat(response.getHits().getAt(0).getSourceAsMap().get("field2").toString(), equalTo("value2")); @@ -1484,8 +1480,8 @@ public void testSource() throws Exception { // user7 has access to all fields response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user7", USERS_PASSWD))) - .prepareSearch("test") - .get(); + .prepareSearch("test") + .get(); assertThat(response.getHits().getAt(0).getSourceAsMap().size(), equalTo(3)); assertThat(response.getHits().getAt(0).getSourceAsMap().get("field1").toString(), equalTo("value1")); assertThat(response.getHits().getAt(0).getSourceAsMap().get("field2").toString(), equalTo("value2")); @@ -1493,222 +1489,223 @@ public void testSource() throws Exception { // user8 has field level security configured with access to field1 and field2: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user8", USERS_PASSWD))) - .prepareSearch("test") - .get(); + .prepareSearch("test") + .get(); assertThat(response.getHits().getAt(0).getSourceAsMap().size(), equalTo(2)); assertThat(response.getHits().getAt(0).getSourceAsMap().get("field1").toString(), equalTo("value1")); assertThat(response.getHits().getAt(0).getSourceAsMap().get("field2").toString(), equalTo("value2")); } public void testSort() { - assertAcked(client().admin().indices().prepareCreate("test").setMapping( - "field1", "type=long", - "field2", "type=long", - "alias", "type=alias,path=field1")); - client().prepareIndex("test").setId("1").setSource("field1", 1d, "field2", 2d) - .setRefreshPolicy(IMMEDIATE) - .get(); + assertAcked( + client().admin() + .indices() + .prepareCreate("test") + .setMapping("field1", "type=long", "field2", "type=long", "alias", "type=alias,path=field1") + ); + client().prepareIndex("test").setId("1").setSource("field1", 1d, "field2", 2d).setRefreshPolicy(IMMEDIATE).get(); // user1 is granted to use field1, so it is included in the sort_values - SearchResponse response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareSearch("test") - .addSort("field1", SortOrder.ASC) - .get(); + SearchResponse response = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).prepareSearch("test").addSort("field1", SortOrder.ASC).get(); assertThat(response.getHits().getAt(0).getSortValues()[0], equalTo(1L)); // user2 is not granted to use field1, so the default missing sort value is included response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareSearch("test") - .addSort("field1", SortOrder.ASC) - .get(); + .prepareSearch("test") + .addSort("field1", SortOrder.ASC) + .get(); assertThat(response.getHits().getAt(0).getSortValues()[0], equalTo(Long.MAX_VALUE)); // user1 is not granted to use field2, so the default missing sort value is included response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareSearch("test") - .addSort("field2", SortOrder.ASC) - .get(); + .prepareSearch("test") + .addSort("field2", SortOrder.ASC) + .get(); assertThat(response.getHits().getAt(0).getSortValues()[0], equalTo(Long.MAX_VALUE)); // user2 is granted to use field2, so it is included in the sort_values response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareSearch("test") - .addSort("field2", SortOrder.ASC) - .get(); + .prepareSearch("test") + .addSort("field2", SortOrder.ASC) + .get(); assertThat(response.getHits().getAt(0).getSortValues()[0], equalTo(2L)); // user1 is granted to use field1, so it is included in the sort_values when using its alias: - response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareSearch("test") - .addSort("alias", SortOrder.ASC) - .get(); + response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) + .prepareSearch("test") + .addSort("alias", SortOrder.ASC) + .get(); assertThat(response.getHits().getAt(0).getSortValues()[0], equalTo(1L)); // user2 is not granted to use field1, so the default missing sort value is included when using its alias: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareSearch("test") - .addSort("alias", SortOrder.ASC) - .get(); + .prepareSearch("test") + .addSort("alias", SortOrder.ASC) + .get(); assertThat(response.getHits().getAt(0).getSortValues()[0], equalTo(Long.MAX_VALUE)); } - public void testHighlighting() { - assertAcked(client().admin().indices().prepareCreate("test").setMapping( - "field1", "type=text", - "field2", "type=text", - "field3", "type=text", - "alias", "type=alias,path=field1")); - client().prepareIndex("test").setId("1").setSource("field1", "value1", "field2", "value2", "field3", "value3") - .setRefreshPolicy(IMMEDIATE) - .get(); - - // user1 has access to field1, so the highlight should be visible: - SearchResponse response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(matchQuery("field1", "value1")) - .highlighter(new HighlightBuilder().field("field1")) - .get(); - assertHitCount(response, 1); - SearchHit hit = response.getHits().iterator().next(); - assertEquals(hit.getHighlightFields().size(), 1); - - // user2 has no access to field1, so the highlight should not be visible: - response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(matchQuery("field2", "value2")) - .highlighter(new HighlightBuilder().field("field1")) - .get(); - assertHitCount(response, 1); - hit = response.getHits().iterator().next(); - assertEquals(hit.getHighlightFields().size(), 0); - - // user1 has access to field1, so the highlight on its alias should be visible: - response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(matchQuery("field1", "value1")) - .highlighter(new HighlightBuilder().field("alias")) - .get(); - assertHitCount(response, 1); - hit = response.getHits().iterator().next(); - assertEquals(hit.getHighlightFields().size(), 1); - - // user2 has no access to field1, so the highlight on its alias should not be visible: - response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(matchQuery("field2", "value2")) - .highlighter(new HighlightBuilder().field("alias")) - .get(); - assertHitCount(response, 1); - hit = response.getHits().iterator().next(); - assertEquals(hit.getHighlightFields().size(), 0); - } + public void testHighlighting() { + assertAcked( + client().admin() + .indices() + .prepareCreate("test") + .setMapping("field1", "type=text", "field2", "type=text", "field3", "type=text", "alias", "type=alias,path=field1") + ); + client().prepareIndex("test") + .setId("1") + .setSource("field1", "value1", "field2", "value2", "field3", "value3") + .setRefreshPolicy(IMMEDIATE) + .get(); + + // user1 has access to field1, so the highlight should be visible: + SearchResponse response = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).prepareSearch("test").setQuery(matchQuery("field1", "value1")).highlighter(new HighlightBuilder().field("field1")).get(); + assertHitCount(response, 1); + SearchHit hit = response.getHits().iterator().next(); + assertEquals(hit.getHighlightFields().size(), 1); + + // user2 has no access to field1, so the highlight should not be visible: + response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) + .prepareSearch("test") + .setQuery(matchQuery("field2", "value2")) + .highlighter(new HighlightBuilder().field("field1")) + .get(); + assertHitCount(response, 1); + hit = response.getHits().iterator().next(); + assertEquals(hit.getHighlightFields().size(), 0); + + // user1 has access to field1, so the highlight on its alias should be visible: + response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) + .prepareSearch("test") + .setQuery(matchQuery("field1", "value1")) + .highlighter(new HighlightBuilder().field("alias")) + .get(); + assertHitCount(response, 1); + hit = response.getHits().iterator().next(); + assertEquals(hit.getHighlightFields().size(), 1); + + // user2 has no access to field1, so the highlight on its alias should not be visible: + response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) + .prepareSearch("test") + .setQuery(matchQuery("field2", "value2")) + .highlighter(new HighlightBuilder().field("alias")) + .get(); + assertHitCount(response, 1); + hit = response.getHits().iterator().next(); + assertEquals(hit.getHighlightFields().size(), 0); + } public void testAggs() { - assertAcked(client().admin().indices().prepareCreate("test").setMapping( - "field1", "type=text,fielddata=true", - "field2", "type=text,fielddata=true", - "alias", "type=alias,path=field1")); - client().prepareIndex("test").setId("1").setSource("field1", "value1", "field2", "value2") - .setRefreshPolicy(IMMEDIATE) - .get(); + assertAcked( + client().admin() + .indices() + .prepareCreate("test") + .setMapping("field1", "type=text,fielddata=true", "field2", "type=text,fielddata=true", "alias", "type=alias,path=field1") + ); + client().prepareIndex("test").setId("1").setSource("field1", "value1", "field2", "value2").setRefreshPolicy(IMMEDIATE).get(); // user1 is authorized to use field1, so buckets are include for a term agg on field1 - SearchResponse response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareSearch("test") - .addAggregation(AggregationBuilders.terms("_name").field("field1")) - .get(); + SearchResponse response = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).prepareSearch("test").addAggregation(AggregationBuilders.terms("_name").field("field1")).get(); assertThat(((Terms) response.getAggregations().get("_name")).getBucketByKey("value1").getDocCount(), equalTo(1L)); // user2 is not authorized to use field1, so no buckets are include for a term agg on field1 response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareSearch("test") - .addAggregation(AggregationBuilders.terms("_name").field("field1")) - .get(); + .prepareSearch("test") + .addAggregation(AggregationBuilders.terms("_name").field("field1")) + .get(); assertThat(((Terms) response.getAggregations().get("_name")).getBucketByKey("value1"), nullValue()); // user1 is not authorized to use field2, so no buckets are include for a term agg on field2 response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareSearch("test") - .addAggregation(AggregationBuilders.terms("_name").field("field2")) - .get(); + .prepareSearch("test") + .addAggregation(AggregationBuilders.terms("_name").field("field2")) + .get(); assertThat(((Terms) response.getAggregations().get("_name")).getBucketByKey("value2"), nullValue()); // user2 is authorized to use field2, so buckets are include for a term agg on field2 response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareSearch("test") - .addAggregation(AggregationBuilders.terms("_name").field("field2")) - .get(); + .prepareSearch("test") + .addAggregation(AggregationBuilders.terms("_name").field("field2")) + .get(); assertThat(((Terms) response.getAggregations().get("_name")).getBucketByKey("value2").getDocCount(), equalTo(1L)); // user1 is authorized to use field1, so buckets are include for a term agg on its alias: - response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareSearch("test") - .addAggregation(AggregationBuilders.terms("_name").field("alias")) - .get(); + response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) + .prepareSearch("test") + .addAggregation(AggregationBuilders.terms("_name").field("alias")) + .get(); assertThat(((Terms) response.getAggregations().get("_name")).getBucketByKey("value1").getDocCount(), equalTo(1L)); // user2 is not authorized to use field1, so no buckets are include for a term agg on its alias: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareSearch("test") - .addAggregation(AggregationBuilders.terms("_name").field("alias")) - .get(); + .prepareSearch("test") + .addAggregation(AggregationBuilders.terms("_name").field("alias")) + .get(); assertThat(((Terms) response.getAggregations().get("_name")).getBucketByKey("value1"), nullValue()); } public void testTVApi() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test") - .setMapping("field1", "type=text,term_vector=with_positions_offsets_payloads", - "field2", "type=text,term_vector=with_positions_offsets_payloads", - "field3", "type=text,term_vector=with_positions_offsets_payloads") + assertAcked( + client().admin() + .indices() + .prepareCreate("test") + .setMapping( + "field1", + "type=text,term_vector=with_positions_offsets_payloads", + "field2", + "type=text,term_vector=with_positions_offsets_payloads", + "field3", + "type=text,term_vector=with_positions_offsets_payloads" + ) ); - client().prepareIndex("test").setId("1").setSource("field1", "value1", "field2", "value2", "field3", "value3") - .setRefreshPolicy(IMMEDIATE) - .get(); + client().prepareIndex("test") + .setId("1") + .setSource("field1", "value1", "field2", "value2", "field3", "value3") + .setRefreshPolicy(IMMEDIATE) + .get(); boolean realtime = randomBoolean(); - TermVectorsResponse response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareTermVectors("test", "1") - .setRealtime(realtime) - .get(); + TermVectorsResponse response = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).prepareTermVectors("test", "1").setRealtime(realtime).get(); assertThat(response.isExists(), is(true)); assertThat(response.getFields().size(), equalTo(1)); assertThat(response.getFields().terms("field1").size(), equalTo(1L)); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareTermVectors("test", "1") - .setRealtime(realtime) - .get(); + .prepareTermVectors("test", "1") + .setRealtime(realtime) + .get(); assertThat(response.isExists(), is(true)); assertThat(response.getFields().size(), equalTo(1)); assertThat(response.getFields().terms("field2").size(), equalTo(1L)); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) - .prepareTermVectors("test", "1") - .setRealtime(realtime) - .get(); + .prepareTermVectors("test", "1") + .setRealtime(realtime) + .get(); assertThat(response.isExists(), is(true)); assertThat(response.getFields().size(), equalTo(2)); assertThat(response.getFields().terms("field1").size(), equalTo(1L)); assertThat(response.getFields().terms("field2").size(), equalTo(1L)); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user4", USERS_PASSWD))) - .prepareTermVectors("test", "1") - .setRealtime(realtime) - .get(); + .prepareTermVectors("test", "1") + .setRealtime(realtime) + .get(); assertThat(response.isExists(), is(true)); assertThat(response.getFields().size(), equalTo(0)); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user5", USERS_PASSWD))) - .prepareTermVectors("test", "1") - .setRealtime(realtime) - .get(); + .prepareTermVectors("test", "1") + .setRealtime(realtime) + .get(); assertThat(response.isExists(), is(true)); assertThat(response.getFields().size(), equalTo(3)); assertThat(response.getFields().terms("field1").size(), equalTo(1L)); @@ -1716,9 +1713,9 @@ public void testTVApi() throws Exception { assertThat(response.getFields().terms("field3").size(), equalTo(1L)); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user6", USERS_PASSWD))) - .prepareTermVectors("test", "1") - .setRealtime(realtime) - .get(); + .prepareTermVectors("test", "1") + .setRealtime(realtime) + .get(); assertThat(response.isExists(), is(true)); assertThat(response.getFields().size(), equalTo(3)); assertThat(response.getFields().terms("field1").size(), equalTo(1L)); @@ -1726,9 +1723,9 @@ public void testTVApi() throws Exception { assertThat(response.getFields().terms("field3").size(), equalTo(1L)); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user7", USERS_PASSWD))) - .prepareTermVectors("test", "1") - .setRealtime(realtime) - .get(); + .prepareTermVectors("test", "1") + .setRealtime(realtime) + .get(); assertThat(response.isExists(), is(true)); assertThat(response.getFields().size(), equalTo(3)); assertThat(response.getFields().terms("field1").size(), equalTo(1L)); @@ -1736,9 +1733,9 @@ public void testTVApi() throws Exception { assertThat(response.getFields().terms("field3").size(), equalTo(1L)); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user8", USERS_PASSWD))) - .prepareTermVectors("test", "1") - .setRealtime(realtime) - .get(); + .prepareTermVectors("test", "1") + .setRealtime(realtime) + .get(); assertThat(response.isExists(), is(true)); assertThat(response.getFields().size(), equalTo(2)); assertThat(response.getFields().terms("field1").size(), equalTo(1L)); @@ -1746,39 +1743,47 @@ public void testTVApi() throws Exception { } public void testMTVApi() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test") - .setMapping("field1", "type=text,term_vector=with_positions_offsets_payloads", - "field2", "type=text,term_vector=with_positions_offsets_payloads", - "field3", "type=text,term_vector=with_positions_offsets_payloads") + assertAcked( + client().admin() + .indices() + .prepareCreate("test") + .setMapping( + "field1", + "type=text,term_vector=with_positions_offsets_payloads", + "field2", + "type=text,term_vector=with_positions_offsets_payloads", + "field3", + "type=text,term_vector=with_positions_offsets_payloads" + ) ); - client().prepareIndex("test").setId("1").setSource("field1", "value1", "field2", "value2", "field3", "value3") - .setRefreshPolicy(IMMEDIATE) - .get(); + client().prepareIndex("test") + .setId("1") + .setSource("field1", "value1", "field2", "value2", "field3", "value3") + .setRefreshPolicy(IMMEDIATE) + .get(); boolean realtime = randomBoolean(); - MultiTermVectorsResponse response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareMultiTermVectors() - .add(new TermVectorsRequest("test", "1").realtime(realtime)) - .get(); + MultiTermVectorsResponse response = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).prepareMultiTermVectors().add(new TermVectorsRequest("test", "1").realtime(realtime)).get(); assertThat(response.getResponses().length, equalTo(1)); assertThat(response.getResponses()[0].getResponse().isExists(), is(true)); assertThat(response.getResponses()[0].getResponse().getFields().size(), equalTo(1)); assertThat(response.getResponses()[0].getResponse().getFields().terms("field1").size(), equalTo(1L)); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareMultiTermVectors() - .add(new TermVectorsRequest("test", "1").realtime(realtime)) - .get(); + .prepareMultiTermVectors() + .add(new TermVectorsRequest("test", "1").realtime(realtime)) + .get(); assertThat(response.getResponses().length, equalTo(1)); assertThat(response.getResponses()[0].getResponse().isExists(), is(true)); assertThat(response.getResponses()[0].getResponse().getFields().size(), equalTo(1)); assertThat(response.getResponses()[0].getResponse().getFields().terms("field2").size(), equalTo(1L)); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) - .prepareMultiTermVectors() - .add(new TermVectorsRequest("test", "1").realtime(realtime)) - .get(); + .prepareMultiTermVectors() + .add(new TermVectorsRequest("test", "1").realtime(realtime)) + .get(); assertThat(response.getResponses().length, equalTo(1)); assertThat(response.getResponses()[0].getResponse().isExists(), is(true)); assertThat(response.getResponses()[0].getResponse().getFields().size(), equalTo(2)); @@ -1786,17 +1791,17 @@ public void testMTVApi() throws Exception { assertThat(response.getResponses()[0].getResponse().getFields().terms("field2").size(), equalTo(1L)); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user4", USERS_PASSWD))) - .prepareMultiTermVectors() - .add(new TermVectorsRequest("test", "1").realtime(realtime)) - .get(); + .prepareMultiTermVectors() + .add(new TermVectorsRequest("test", "1").realtime(realtime)) + .get(); assertThat(response.getResponses().length, equalTo(1)); assertThat(response.getResponses()[0].getResponse().isExists(), is(true)); assertThat(response.getResponses()[0].getResponse().getFields().size(), equalTo(0)); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user5", USERS_PASSWD))) - .prepareMultiTermVectors() - .add(new TermVectorsRequest("test", "1").realtime(realtime)) - .get(); + .prepareMultiTermVectors() + .add(new TermVectorsRequest("test", "1").realtime(realtime)) + .get(); assertThat(response.getResponses().length, equalTo(1)); assertThat(response.getResponses()[0].getResponse().isExists(), is(true)); assertThat(response.getResponses()[0].getResponse().getFields().size(), equalTo(3)); @@ -1805,9 +1810,9 @@ public void testMTVApi() throws Exception { assertThat(response.getResponses()[0].getResponse().getFields().terms("field3").size(), equalTo(1L)); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user6", USERS_PASSWD))) - .prepareMultiTermVectors() - .add(new TermVectorsRequest("test", "1").realtime(realtime)) - .get(); + .prepareMultiTermVectors() + .add(new TermVectorsRequest("test", "1").realtime(realtime)) + .get(); assertThat(response.getResponses().length, equalTo(1)); assertThat(response.getResponses()[0].getResponse().isExists(), is(true)); assertThat(response.getResponses()[0].getResponse().getFields().size(), equalTo(3)); @@ -1816,9 +1821,9 @@ public void testMTVApi() throws Exception { assertThat(response.getResponses()[0].getResponse().getFields().terms("field3").size(), equalTo(1L)); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user7", USERS_PASSWD))) - .prepareMultiTermVectors() - .add(new TermVectorsRequest("test", "1").realtime(realtime)) - .get(); + .prepareMultiTermVectors() + .add(new TermVectorsRequest("test", "1").realtime(realtime)) + .get(); assertThat(response.getResponses().length, equalTo(1)); assertThat(response.getResponses()[0].getResponse().isExists(), is(true)); assertThat(response.getResponses()[0].getResponse().getFields().size(), equalTo(3)); @@ -1827,9 +1832,9 @@ public void testMTVApi() throws Exception { assertThat(response.getResponses()[0].getResponse().getFields().terms("field3").size(), equalTo(1L)); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user8", USERS_PASSWD))) - .prepareMultiTermVectors() - .add(new TermVectorsRequest("test", "1").realtime(realtime)) - .get(); + .prepareMultiTermVectors() + .add(new TermVectorsRequest("test", "1").realtime(realtime)) + .get(); assertThat(response.getResponses().length, equalTo(1)); assertThat(response.getResponses()[0].getResponse().isExists(), is(true)); assertThat(response.getResponses()[0].getResponse().getFields().size(), equalTo(2)); @@ -1838,25 +1843,25 @@ public void testMTVApi() throws Exception { } public void testParentChild() throws Exception { - XContentBuilder mapping = XContentFactory.jsonBuilder().startObject() + XContentBuilder mapping = XContentFactory.jsonBuilder() + .startObject() .startObject("properties") - .startObject("field1") - .field("type", "keyword") - .endObject() - .startObject("alias") - .field("type", "alias") - .field("path", "field1") - .endObject() - .startObject("join_field") - .field("type", "join") - .startObject("relations") - .field("parent", "child") - .endObject() - .endObject() + .startObject("field1") + .field("type", "keyword") + .endObject() + .startObject("alias") + .field("type", "alias") + .field("path", "field1") + .endObject() + .startObject("join_field") + .field("type", "join") + .startObject("relations") + .field("parent", "child") + .endObject() + .endObject() .endObject() .endObject(); - assertAcked(prepareCreate("test") - .setMapping(mapping)); + assertAcked(prepareCreate("test").setMapping(mapping)); ensureGreen(); // index simple data @@ -1877,25 +1882,21 @@ public void testParentChild() throws Exception { } private void verifyParentChild() { - SearchResponse searchResponse = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(hasChildQuery("child", termQuery("field1", "yellow"), ScoreMode.None)) - .get(); + SearchResponse searchResponse = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).prepareSearch("test").setQuery(hasChildQuery("child", termQuery("field1", "yellow"), ScoreMode.None)).get(); assertHitCount(searchResponse, 1L); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("p1")); - searchResponse = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(hasChildQuery("child", termQuery("field1", "yellow"), ScoreMode.None)) - .get(); + searchResponse = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) + .prepareSearch("test") + .setQuery(hasChildQuery("child", termQuery("field1", "yellow"), ScoreMode.None)) + .get(); assertHitCount(searchResponse, 0L); // Perform the same checks, but using an alias for field1. - searchResponse = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) + searchResponse = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) .prepareSearch("test") .setQuery(hasChildQuery("child", termQuery("alias", "yellow"), ScoreMode.None)) .get(); @@ -1903,8 +1904,7 @@ private void verifyParentChild() { assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("p1")); - searchResponse = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) + searchResponse = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) .prepareSearch("test") .setQuery(hasChildQuery("child", termQuery("alias", "yellow"), ScoreMode.None)) .get(); @@ -1912,19 +1912,15 @@ private void verifyParentChild() { } public void testUpdateApiIsBlocked() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test") - .setMapping("field1", "type=text", "field2", "type=text") - ); - client().prepareIndex("test").setId("1") - .setSource("field1", "value1", "field2", "value1") - .setRefreshPolicy(IMMEDIATE) - .get(); + assertAcked(client().admin().indices().prepareCreate("test").setMapping("field1", "type=text", "field2", "type=text")); + client().prepareIndex("test").setId("1").setSource("field1", "value1", "field2", "value1").setRefreshPolicy(IMMEDIATE).get(); // With field level security enabled the update is not allowed: try { client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareUpdate("test", "1").setDoc(Requests.INDEX_CONTENT_TYPE, "field2", "value2") - .get(); + .prepareUpdate("test", "1") + .setDoc(Requests.INDEX_CONTENT_TYPE, "field2", "value2") + .get(); fail("failed, because update request shouldn't be allowed if field level security is enabled"); } catch (ElasticsearchSecurityException e) { assertThat(e.status(), equalTo(RestStatus.BAD_REQUEST)); @@ -1933,55 +1929,47 @@ public void testUpdateApiIsBlocked() throws Exception { assertThat(client().prepareGet("test", "1").get().getSource().get("field2").toString(), equalTo("value1")); // With no field level security enabled the update is allowed: - client().prepareUpdate("test", "1").setDoc(Requests.INDEX_CONTENT_TYPE, "field2", "value2") - .get(); + client().prepareUpdate("test", "1").setDoc(Requests.INDEX_CONTENT_TYPE, "field2", "value2").get(); assertThat(client().prepareGet("test", "1").get().getSource().get("field2").toString(), equalTo("value2")); // With field level security enabled the update in bulk is not allowed: - BulkResponse bulkResponse = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue - ("user1", USERS_PASSWD))) - .prepareBulk() - .add(new UpdateRequest("test", "1").doc(Requests.INDEX_CONTENT_TYPE, "field2", "value3")) - .get(); + BulkResponse bulkResponse = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).prepareBulk().add(new UpdateRequest("test", "1").doc(Requests.INDEX_CONTENT_TYPE, "field2", "value3")).get(); assertEquals(1, bulkResponse.getItems().length); BulkItemResponse bulkItem = bulkResponse.getItems()[0]; assertTrue(bulkItem.isFailed()); assertThat(bulkItem.getFailure().getCause(), instanceOf(ElasticsearchSecurityException.class)); ElasticsearchSecurityException securityException = (ElasticsearchSecurityException) bulkItem.getFailure().getCause(); assertThat(securityException.status(), equalTo(RestStatus.BAD_REQUEST)); - assertThat(securityException.getMessage(), - equalTo("Can't execute a bulk item request with update requests embedded if field or document level security is enabled")); + assertThat( + securityException.getMessage(), + equalTo("Can't execute a bulk item request with update requests embedded if field or document level security is enabled") + ); assertThat(client().prepareGet("test", "1").get().getSource().get("field2").toString(), equalTo("value2")); - client().prepareBulk() - .add(new UpdateRequest("test", "1").doc(Requests.INDEX_CONTENT_TYPE, "field2", "value3")) - .get(); + client().prepareBulk().add(new UpdateRequest("test", "1").doc(Requests.INDEX_CONTENT_TYPE, "field2", "value3")).get(); assertThat(client().prepareGet("test", "1").get().getSource().get("field2").toString(), equalTo("value3")); } public void testQuery_withRoleWithFieldWildcards() { - assertAcked(client().admin().indices().prepareCreate("test") - .setMapping("field1", "type=text", "field2", "type=text")); - client().prepareIndex("test").setId("1").setSource("field1", "value1", "field2", "value2") - .setRefreshPolicy(IMMEDIATE) - .get(); + assertAcked(client().admin().indices().prepareCreate("test").setMapping("field1", "type=text", "field2", "type=text")); + client().prepareIndex("test").setId("1").setSource("field1", "value1", "field2", "value2").setRefreshPolicy(IMMEDIATE).get(); // user6 has access to all fields, so the query should match with the document: - SearchResponse response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user6", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(matchQuery("field1", "value1")) - .get(); + SearchResponse response = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user6", USERS_PASSWD)) + ).prepareSearch("test").setQuery(matchQuery("field1", "value1")).get(); assertHitCount(response, 1); assertThat(response.getHits().getAt(0).getSourceAsMap().size(), equalTo(2)); assertThat(response.getHits().getAt(0).getSourceAsMap().get("field1").toString(), equalTo("value1")); assertThat(response.getHits().getAt(0).getSourceAsMap().get("field2").toString(), equalTo("value2")); response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user6", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(matchQuery("field2", "value2")) - .get(); + .prepareSearch("test") + .setQuery(matchQuery("field2", "value2")) + .get(); assertHitCount(response, 1); assertThat(response.getHits().getAt(0).getSourceAsMap().size(), equalTo(2)); assertThat(response.getHits().getAt(0).getSourceAsMap().get("field1").toString(), equalTo("value1")); @@ -1989,79 +1977,78 @@ public void testQuery_withRoleWithFieldWildcards() { } public void testExistQuery() { - assertAcked(client().admin().indices().prepareCreate("test").setMapping( - "field1", "type=text", - "field2", "type=text", - "field3", "type=text", - "alias", "type=alias,path=field1")); - - client().prepareIndex("test").setId("1").setSource("field1", "value1", "field2", "value2", "field3", "value3") - .setRefreshPolicy(IMMEDIATE) - .get(); + assertAcked( + client().admin() + .indices() + .prepareCreate("test") + .setMapping("field1", "type=text", "field2", "type=text", "field3", "type=text", "alias", "type=alias,path=field1") + ); + + client().prepareIndex("test") + .setId("1") + .setSource("field1", "value1", "field2", "value2", "field3", "value3") + .setRefreshPolicy(IMMEDIATE) + .get(); // user1 has access to field1, so the query should match with the document: - SearchResponse response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(existsQuery("field1")) - .get(); + SearchResponse response = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).prepareSearch("test").setQuery(existsQuery("field1")).get(); assertHitCount(response, 1); // user1 has no access to field2, so the query should not match with the document: - response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(existsQuery("field2")) - .get(); + response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) + .prepareSearch("test") + .setQuery(existsQuery("field2")) + .get(); assertHitCount(response, 0); // user2 has no access to field1, so the query should not match with the document: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(existsQuery("field1")) - .get(); + .prepareSearch("test") + .setQuery(existsQuery("field1")) + .get(); assertHitCount(response, 0); // user2 has access to field2, so the query should match with the document: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(existsQuery("field2")) - .get(); + .prepareSearch("test") + .setQuery(existsQuery("field2")) + .get(); assertHitCount(response, 1); // user3 has access to field1 and field2, so the query should match with the document: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(existsQuery("field1")) - .get(); + .prepareSearch("test") + .setQuery(existsQuery("field1")) + .get(); assertHitCount(response, 1); // user3 has access to field1 and field2, so the query should match with the document: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user3", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(existsQuery("field2")) - .get(); + .prepareSearch("test") + .setQuery(existsQuery("field2")) + .get(); assertHitCount(response, 1); // user4 has access to no fields, so the query should not match with the document: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user4", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(existsQuery("field1")) - .get(); + .prepareSearch("test") + .setQuery(existsQuery("field1")) + .get(); assertHitCount(response, 0); // user4 has access to no fields, so the query should not match with the document: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user4", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(existsQuery("field2")) - .get(); + .prepareSearch("test") + .setQuery(existsQuery("field2")) + .get(); assertHitCount(response, 0); // user1 has access to field1, so a query on its alias should match with the document: - response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(existsQuery("alias")) - .get(); + response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) + .prepareSearch("test") + .setQuery(existsQuery("alias")) + .get(); assertHitCount(response, 1); // user2 has no access to field1, so the query should not match with the document: response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user2", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(existsQuery("alias")) - .get(); + .prepareSearch("test") + .setQuery(existsQuery("alias")) + .get(); assertHitCount(response, 0); } diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/IndexPrivilegeIntegTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/IndexPrivilegeIntegTests.java index 4c976e7c101fa..539a602367796 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/IndexPrivilegeIntegTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/IndexPrivilegeIntegTests.java @@ -27,90 +27,88 @@ public class IndexPrivilegeIntegTests extends AbstractPrivilegeTestCase { private String jsonDoc = "{ \"name\" : \"elasticsearch\", \"body\": \"foo bar\" }"; - private static final String ROLES = - "all_cluster_role:\n" + - " cluster: [ all ]\n" + - "all_indices_role:\n" + - " indices:\n" + - " - names: '*'\n" + - " privileges: [ all ]\n" + - "all_a_role:\n" + - " indices:\n" + - " - names: 'a'\n" + - " privileges: [ all ]\n" + - "read_a_role:\n" + - " indices:\n" + - " - names: 'a'\n" + - " privileges: [ read ]\n" + - "read_b_role:\n" + - " indices:\n" + - " - names: 'b'\n" + - " privileges: [ read ]\n" + - "write_a_role:\n" + - " indices:\n" + - " - names: 'a'\n" + - " privileges: [ write ]\n" + - "read_ab_role:\n" + - " indices:\n" + - " - names: [ 'a', 'b' ]\n" + - " privileges: [ read ]\n" + - "all_regex_ab_role:\n" + - " indices:\n" + - " - names: '/a|b/'\n" + - " privileges: [ all ]\n" + - "manage_starts_with_a_role:\n" + - " indices:\n" + - " - names: 'a*'\n" + - " privileges: [ manage ]\n" + - "read_write_all_role:\n" + - " indices:\n" + - " - names: '*'\n" + - " privileges: [ read, write ]\n" + - "create_c_role:\n" + - " indices:\n" + - " - names: 'c'\n" + - " privileges: [ create_index ]\n" + - "monitor_b_role:\n" + - " indices:\n" + - " - names: 'b'\n" + - " privileges: [ monitor ]\n" + - "maintenance_a_view_meta_b_role:\n" + - " indices:\n" + - " - names: 'a'\n" + - " privileges: [ maintenance ]\n" + - " - names: '*b'\n" + - " privileges: [ view_index_metadata ]\n" + - "read_write_a_role:\n" + - " indices:\n" + - " - names: 'a'\n" + - " privileges: [ read, write ]\n" + - "delete_b_role:\n" + - " indices:\n" + - " - names: 'b'\n" + - " privileges: [ delete ]\n" + - "index_a_role:\n" + - " indices:\n" + - " - names: 'a'\n" + - " privileges: [ index ]\n" + - "\n"; - - private static final String USERS_ROLES = - "all_indices_role:admin,u8\n" + - "all_cluster_role:admin\n" + - "all_a_role:u1,u2,u6\n" + - "read_a_role:u1,u5,u14\n" + - "read_b_role:u3,u5,u6,u8,u13\n" + - "write_a_role:u9\n" + - "read_ab_role:u2,u4,u9\n" + - "all_regex_ab_role:u3\n" + - "manage_starts_with_a_role:u4\n" + - "read_write_all_role:u12\n" + - "create_c_role:u11\n" + - "monitor_b_role:u14\n" + - "maintenance_a_view_meta_b_role:u15\n" + - "read_write_a_role:u12\n" + - "delete_b_role:u11\n" + - "index_a_role:u13\n"; + private static final String ROLES = "all_cluster_role:\n" + + " cluster: [ all ]\n" + + "all_indices_role:\n" + + " indices:\n" + + " - names: '*'\n" + + " privileges: [ all ]\n" + + "all_a_role:\n" + + " indices:\n" + + " - names: 'a'\n" + + " privileges: [ all ]\n" + + "read_a_role:\n" + + " indices:\n" + + " - names: 'a'\n" + + " privileges: [ read ]\n" + + "read_b_role:\n" + + " indices:\n" + + " - names: 'b'\n" + + " privileges: [ read ]\n" + + "write_a_role:\n" + + " indices:\n" + + " - names: 'a'\n" + + " privileges: [ write ]\n" + + "read_ab_role:\n" + + " indices:\n" + + " - names: [ 'a', 'b' ]\n" + + " privileges: [ read ]\n" + + "all_regex_ab_role:\n" + + " indices:\n" + + " - names: '/a|b/'\n" + + " privileges: [ all ]\n" + + "manage_starts_with_a_role:\n" + + " indices:\n" + + " - names: 'a*'\n" + + " privileges: [ manage ]\n" + + "read_write_all_role:\n" + + " indices:\n" + + " - names: '*'\n" + + " privileges: [ read, write ]\n" + + "create_c_role:\n" + + " indices:\n" + + " - names: 'c'\n" + + " privileges: [ create_index ]\n" + + "monitor_b_role:\n" + + " indices:\n" + + " - names: 'b'\n" + + " privileges: [ monitor ]\n" + + "maintenance_a_view_meta_b_role:\n" + + " indices:\n" + + " - names: 'a'\n" + + " privileges: [ maintenance ]\n" + + " - names: '*b'\n" + + " privileges: [ view_index_metadata ]\n" + + "read_write_a_role:\n" + + " indices:\n" + + " - names: 'a'\n" + + " privileges: [ read, write ]\n" + + "delete_b_role:\n" + + " indices:\n" + + " - names: 'b'\n" + + " privileges: [ delete ]\n" + + "index_a_role:\n" + + " indices:\n" + + " - names: 'a'\n" + + " privileges: [ index ]\n" + + "\n"; + + private static final String USERS_ROLES = "all_indices_role:admin,u8\n" + + "all_cluster_role:admin\n" + + "all_a_role:u1,u2,u6\n" + + "read_a_role:u1,u5,u14\n" + + "read_b_role:u3,u5,u6,u8,u13\n" + + "write_a_role:u9\n" + + "read_ab_role:u2,u4,u9\n" + + "all_regex_ab_role:u3\n" + + "manage_starts_with_a_role:u4\n" + + "read_write_all_role:u12\n" + + "create_c_role:u11\n" + + "monitor_b_role:u14\n" + + "maintenance_a_view_meta_b_role:u15\n" + + "read_write_a_role:u12\n" + + "delete_b_role:u11\n" + + "index_a_role:u13\n"; @Override protected boolean addMockHttpTransport() { @@ -127,22 +125,52 @@ protected String configUsers() { final Hasher passwdHasher = getFastStoredHashAlgoForTests(); final String usersPasswdHashed = new String(passwdHasher.hash(SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)); - return super.configUsers() + - "admin:" + usersPasswdHashed + "\n" + - "u1:" + usersPasswdHashed + "\n" + - "u2:" + usersPasswdHashed + "\n" + - "u3:" + usersPasswdHashed + "\n" + - "u4:" + usersPasswdHashed + "\n" + - "u5:" + usersPasswdHashed + "\n" + - "u6:" + usersPasswdHashed + "\n" + - "u7:" + usersPasswdHashed + "\n" + - "u8:" + usersPasswdHashed + "\n" + - "u9:" + usersPasswdHashed + "\n" + - "u11:" + usersPasswdHashed + "\n" + - "u12:" + usersPasswdHashed + "\n" + - "u13:" + usersPasswdHashed + "\n" + - "u14:" + usersPasswdHashed + "\n" + - "u15:" + usersPasswdHashed + "\n"; + return super.configUsers() + + "admin:" + + usersPasswdHashed + + "\n" + + "u1:" + + usersPasswdHashed + + "\n" + + "u2:" + + usersPasswdHashed + + "\n" + + "u3:" + + usersPasswdHashed + + "\n" + + "u4:" + + usersPasswdHashed + + "\n" + + "u5:" + + usersPasswdHashed + + "\n" + + "u6:" + + usersPasswdHashed + + "\n" + + "u7:" + + usersPasswdHashed + + "\n" + + "u8:" + + usersPasswdHashed + + "\n" + + "u9:" + + usersPasswdHashed + + "\n" + + "u11:" + + usersPasswdHashed + + "\n" + + "u12:" + + usersPasswdHashed + + "\n" + + "u13:" + + usersPasswdHashed + + "\n" + + "u14:" + + usersPasswdHashed + + "\n" + + "u15:" + + usersPasswdHashed + + "\n"; } @Override @@ -153,7 +181,7 @@ protected String configUsersRoles() { @Before public void insertBaseDocumentsAsAdmin() throws Exception { // indices: a,b,c,abc - for (String index : new String[]{"a", "b", "c", "abc"}) { + for (String index : new String[] { "a", "b", "c", "abc" }) { Request request = new Request("PUT", "/" + index + "/_doc/1"); request.setJsonEntity(jsonDoc); request.addParameter("refresh", "true"); @@ -170,13 +198,20 @@ public void testUserU1() throws Exception { assertUserIsAllowed("u1", "all", "a"); assertUserIsDenied("u1", "all", "b"); assertUserIsDenied("u1", "all", "c"); - assertAccessIsAllowed("u1", - "GET", "/" + randomIndex() + "/_msearch", "{}\n{ \"query\" : { \"match_all\" : {} } }\n"); + assertAccessIsAllowed("u1", "GET", "/" + randomIndex() + "/_msearch", "{}\n{ \"query\" : { \"match_all\" : {} } }\n"); assertAccessIsAllowed("u1", "POST", "/" + randomIndex() + "/_mget", "{ \"ids\" : [ \"1\", \"2\" ] } "); - assertAccessIsAllowed("u1", "PUT", - "/" + randomIndex() + "/_bulk", "{ \"index\" : { \"_id\" : \"123\" } }\n{ \"foo\" : \"bar\" }\n"); - assertAccessIsAllowed("u1", - "GET", "/" + randomIndex() + "/_mtermvectors", "{ \"docs\" : [ { \"_id\": \"1\" }, { \"_id\": \"2\" } ] }"); + assertAccessIsAllowed( + "u1", + "PUT", + "/" + randomIndex() + "/_bulk", + "{ \"index\" : { \"_id\" : \"123\" } }\n{ \"foo\" : \"bar\" }\n" + ); + assertAccessIsAllowed( + "u1", + "GET", + "/" + randomIndex() + "/_mtermvectors", + "{ \"docs\" : [ { \"_id\": \"1\" }, { \"_id\": \"2\" } ] }" + ); assertAccessIsDenied("u1", randomFrom("GET", "POST"), "/" + "b" + "/_field_caps?fields=*"); assertAccessIsDenied("u1", randomFrom("GET", "POST"), "/" + "c" + "/_field_caps?fields=*"); } @@ -189,13 +224,20 @@ public void testUserU2() throws Exception { assertUserIsDenied("u2", "monitor", "b"); assertUserIsDenied("u2", "create_index", "b"); assertUserIsDenied("u2", "all", "c"); - assertAccessIsAllowed("u2", - "GET", "/" + randomIndex() + "/_msearch", "{}\n{ \"query\" : { \"match_all\" : {} } }\n"); + assertAccessIsAllowed("u2", "GET", "/" + randomIndex() + "/_msearch", "{}\n{ \"query\" : { \"match_all\" : {} } }\n"); assertAccessIsAllowed("u2", "POST", "/" + randomIndex() + "/_mget", "{ \"ids\" : [ \"1\", \"2\" ] } "); - assertAccessIsAllowed("u2", "PUT", - "/" + randomIndex() + "/_bulk", "{ \"index\" : { \"_id\" : \"123\" } }\n{ \"foo\" : \"bar\" }\n"); - assertAccessIsAllowed("u2", - "GET", "/" + randomIndex() + "/_mtermvectors", "{ \"docs\" : [ { \"_id\": \"1\" }, { \"_id\": \"2\" } ] }"); + assertAccessIsAllowed( + "u2", + "PUT", + "/" + randomIndex() + "/_bulk", + "{ \"index\" : { \"_id\" : \"123\" } }\n{ \"foo\" : \"bar\" }\n" + ); + assertAccessIsAllowed( + "u2", + "GET", + "/" + randomIndex() + "/_mtermvectors", + "{ \"docs\" : [ { \"_id\": \"1\" }, { \"_id\": \"2\" } ] }" + ); assertAccessIsDenied("u2", randomFrom("GET", "POST"), "/" + "c" + "/_field_caps?fields=*"); } @@ -204,13 +246,20 @@ public void testUserU3() throws Exception { assertUserIsAllowed("u3", "all", "a"); assertUserIsAllowed("u3", "all", "b"); assertUserIsDenied("u3", "all", "c"); - assertAccessIsAllowed("u3", - "GET", "/" + randomIndex() + "/_msearch", "{}\n{ \"query\" : { \"match_all\" : {} } }\n"); + assertAccessIsAllowed("u3", "GET", "/" + randomIndex() + "/_msearch", "{}\n{ \"query\" : { \"match_all\" : {} } }\n"); assertAccessIsAllowed("u3", "POST", "/" + randomIndex() + "/_mget", "{ \"ids\" : [ \"1\", \"2\" ] } "); - assertAccessIsAllowed("u3", "PUT", - "/" + randomIndex() + "/_bulk", "{ \"index\" : { \"_id\" : \"123\" } }\n{ \"foo\" : \"bar\" }\n"); - assertAccessIsAllowed("u3", - "GET", "/" + randomIndex() + "/_mtermvectors", "{ \"docs\" : [ { \"_id\": \"1\" }, { \"_id\": \"2\" } ] }"); + assertAccessIsAllowed( + "u3", + "PUT", + "/" + randomIndex() + "/_bulk", + "{ \"index\" : { \"_id\" : \"123\" } }\n{ \"foo\" : \"bar\" }\n" + ); + assertAccessIsAllowed( + "u3", + "GET", + "/" + randomIndex() + "/_mtermvectors", + "{ \"docs\" : [ { \"_id\": \"1\" }, { \"_id\": \"2\" } ] }" + ); } public void testUserU4() throws Exception { @@ -228,13 +277,15 @@ public void testUserU4() throws Exception { assertUserIsAllowed("u4", "create_index", "an_index"); assertUserIsAllowed("u4", "manage", "an_index"); - assertAccessIsAllowed("u4", - "GET", "/" + randomIndex() + "/_msearch", "{}\n{ \"query\" : { \"match_all\" : {} } }\n"); + assertAccessIsAllowed("u4", "GET", "/" + randomIndex() + "/_msearch", "{}\n{ \"query\" : { \"match_all\" : {} } }\n"); assertAccessIsAllowed("u4", "POST", "/" + randomIndex() + "/_mget", "{ \"ids\" : [ \"1\", \"2\" ] } "); - assertAccessIsDenied("u4", "PUT", - "/" + randomIndex() + "/_bulk", "{ \"index\" : { \"_id\" : \"123\" } }\n{ \"foo\" : \"bar\" }\n"); - assertAccessIsAllowed("u4", - "GET", "/" + randomIndex() + "/_mtermvectors", "{ \"docs\" : [ { \"_id\": \"1\" }, { \"_id\": \"2\" } ] }"); + assertAccessIsDenied("u4", "PUT", "/" + randomIndex() + "/_bulk", "{ \"index\" : { \"_id\" : \"123\" } }\n{ \"foo\" : \"bar\" }\n"); + assertAccessIsAllowed( + "u4", + "GET", + "/" + randomIndex() + "/_mtermvectors", + "{ \"docs\" : [ { \"_id\": \"1\" }, { \"_id\": \"2\" } ] }" + ); assertAccessIsDenied("u2", randomFrom("GET", "POST"), "/" + "c" + "/_field_caps?fields=*"); } @@ -248,13 +299,15 @@ public void testUserU5() throws Exception { assertUserIsDenied("u5", "manage", "b"); assertUserIsDenied("u5", "write", "b"); - assertAccessIsAllowed("u5", - "GET", "/" + randomIndex() + "/_msearch", "{}\n{ \"query\" : { \"match_all\" : {} } }\n"); + assertAccessIsAllowed("u5", "GET", "/" + randomIndex() + "/_msearch", "{}\n{ \"query\" : { \"match_all\" : {} } }\n"); assertAccessIsAllowed("u5", "POST", "/" + randomIndex() + "/_mget", "{ \"ids\" : [ \"1\", \"2\" ] } "); - assertAccessIsDenied("u5", "PUT", - "/" + randomIndex() + "/_bulk", "{ \"index\" : { \"_id\" : \"123\" } }\n{ \"foo\" : \"bar\" }\n"); - assertAccessIsAllowed("u5", - "GET", "/" + randomIndex() + "/_mtermvectors", "{ \"docs\" : [ { \"_id\": \"1\" }, { \"_id\": \"2\" } ] }"); + assertAccessIsDenied("u5", "PUT", "/" + randomIndex() + "/_bulk", "{ \"index\" : { \"_id\" : \"123\" } }\n{ \"foo\" : \"bar\" }\n"); + assertAccessIsAllowed( + "u5", + "GET", + "/" + randomIndex() + "/_mtermvectors", + "{ \"docs\" : [ { \"_id\": \"1\" }, { \"_id\": \"2\" } ] }" + ); } public void testUserU6() throws Exception { @@ -264,13 +317,20 @@ public void testUserU6() throws Exception { assertUserIsDenied("u6", "manage", "b"); assertUserIsDenied("u6", "write", "b"); assertUserIsDenied("u6", "all", "c"); - assertAccessIsAllowed("u6", - "GET", "/" + randomIndex() + "/_msearch", "{}\n{ \"query\" : { \"match_all\" : {} } }\n"); + assertAccessIsAllowed("u6", "GET", "/" + randomIndex() + "/_msearch", "{}\n{ \"query\" : { \"match_all\" : {} } }\n"); assertAccessIsAllowed("u6", "POST", "/" + randomIndex() + "/_mget", "{ \"ids\" : [ \"1\", \"2\" ] } "); - assertAccessIsAllowed("u6", "PUT", - "/" + randomIndex() + "/_bulk", "{ \"index\" : { \"_id\" : \"123\" } }\n{ \"foo\" : \"bar\" }\n"); - assertAccessIsAllowed("u6", - "GET", "/" + randomIndex() + "/_mtermvectors", "{ \"docs\" : [ { \"_id\": \"1\" }, { \"_id\": \"2\" } ] }"); + assertAccessIsAllowed( + "u6", + "PUT", + "/" + randomIndex() + "/_bulk", + "{ \"index\" : { \"_id\" : \"123\" } }\n{ \"foo\" : \"bar\" }\n" + ); + assertAccessIsAllowed( + "u6", + "GET", + "/" + randomIndex() + "/_mtermvectors", + "{ \"docs\" : [ { \"_id\": \"1\" }, { \"_id\": \"2\" } ] }" + ); } public void testUserU7() throws Exception { @@ -278,13 +338,15 @@ public void testUserU7() throws Exception { assertUserIsDenied("u7", "all", "a"); assertUserIsDenied("u7", "all", "b"); assertUserIsDenied("u7", "all", "c"); - assertAccessIsDenied("u7", - "GET", "/" + randomIndex() + "/_msearch", "{}\n{ \"query\" : { \"match_all\" : {} } }\n"); + assertAccessIsDenied("u7", "GET", "/" + randomIndex() + "/_msearch", "{}\n{ \"query\" : { \"match_all\" : {} } }\n"); assertAccessIsDenied("u7", "POST", "/" + randomIndex() + "/_mget", "{ \"ids\" : [ \"1\", \"2\" ] } "); - assertAccessIsDenied("u7", "PUT", - "/" + randomIndex() + "/_bulk", "{ \"index\" : { \"_id\" : \"123\" } }\n{ \"foo\" : \"bar\" }\n"); - assertAccessIsDenied("u7", - "GET", "/" + randomIndex() + "/_mtermvectors", "{ \"docs\" : [ { \"_id\": \"1\" }, { \"_id\": \"2\" } ] }"); + assertAccessIsDenied("u7", "PUT", "/" + randomIndex() + "/_bulk", "{ \"index\" : { \"_id\" : \"123\" } }\n{ \"foo\" : \"bar\" }\n"); + assertAccessIsDenied( + "u7", + "GET", + "/" + randomIndex() + "/_mtermvectors", + "{ \"docs\" : [ { \"_id\": \"1\" }, { \"_id\": \"2\" } ] }" + ); assertAccessIsDenied("u7", randomFrom("GET", "POST"), "/" + randomIndex() + "/_field_caps?fields=*"); } @@ -293,13 +355,20 @@ public void testUserU8() throws Exception { assertUserIsAllowed("u8", "all", "a"); assertUserIsAllowed("u8", "all", "b"); assertUserIsAllowed("u8", "all", "c"); - assertAccessIsAllowed("u8", - "GET", "/" + randomIndex() + "/_msearch", "{}\n{ \"query\" : { \"match_all\" : {} } }\n"); + assertAccessIsAllowed("u8", "GET", "/" + randomIndex() + "/_msearch", "{}\n{ \"query\" : { \"match_all\" : {} } }\n"); assertAccessIsAllowed("u8", "POST", "/" + randomIndex() + "/_mget", "{ \"ids\" : [ \"1\", \"2\" ] } "); - assertAccessIsAllowed("u8", "PUT", - "/" + randomIndex() + "/_bulk", "{ \"index\" : { \"_id\" : \"123\" } }\n{ \"foo\" : \"bar\" }\n"); - assertAccessIsAllowed("u8", - "GET", "/" + randomIndex() + "/_mtermvectors", "{ \"docs\" : [ { \"_id\": \"1\" }, { \"_id\": \"2\" } ] }"); + assertAccessIsAllowed( + "u8", + "PUT", + "/" + randomIndex() + "/_bulk", + "{ \"index\" : { \"_id\" : \"123\" } }\n{ \"foo\" : \"bar\" }\n" + ); + assertAccessIsAllowed( + "u8", + "GET", + "/" + randomIndex() + "/_mtermvectors", + "{ \"docs\" : [ { \"_id\": \"1\" }, { \"_id\": \"2\" } ] }" + ); } public void testUserU9() throws Exception { @@ -310,13 +379,20 @@ public void testUserU9() throws Exception { assertUserIsDenied("u9", "manage", "b"); assertUserIsDenied("u9", "write", "b"); assertUserIsDenied("u9", "all", "c"); - assertAccessIsAllowed("u9", - "GET", "/" + randomIndex() + "/_msearch", "{}\n{ \"query\" : { \"match_all\" : {} } }\n"); + assertAccessIsAllowed("u9", "GET", "/" + randomIndex() + "/_msearch", "{}\n{ \"query\" : { \"match_all\" : {} } }\n"); assertAccessIsAllowed("u9", "POST", "/" + randomIndex() + "/_mget", "{ \"ids\" : [ \"1\", \"2\" ] } "); - assertAccessIsAllowed("u9", "PUT", - "/" + randomIndex() + "/_bulk", "{ \"index\" : { \"_id\" : \"123\" } }\n{ \"foo\" : \"bar\" }\n"); - assertAccessIsAllowed("u9", - "GET", "/" + randomIndex() + "/_mtermvectors", "{ \"docs\" : [ { \"_id\": \"1\" }, { \"_id\": \"2\" } ] }"); + assertAccessIsAllowed( + "u9", + "PUT", + "/" + randomIndex() + "/_bulk", + "{ \"index\" : { \"_id\" : \"123\" } }\n{ \"foo\" : \"bar\" }\n" + ); + assertAccessIsAllowed( + "u9", + "GET", + "/" + randomIndex() + "/_mtermvectors", + "{ \"docs\" : [ { \"_id\": \"1\" }, { \"_id\": \"2\" } ] }" + ); assertAccessIsDenied("u9", randomFrom("GET", "POST"), "/" + "c" + "/_field_caps?fields=*"); } @@ -336,13 +412,20 @@ public void testUserU11() throws Exception { assertUserIsDenied("u11", "monitor", "c"); assertUserIsDenied("u11", "maintenance", "c"); - assertAccessIsDenied("u11", - "GET", "/" + randomIndex() + "/_msearch", "{}\n{ \"query\" : { \"match_all\" : {} } }\n"); + assertAccessIsDenied("u11", "GET", "/" + randomIndex() + "/_msearch", "{}\n{ \"query\" : { \"match_all\" : {} } }\n"); assertAccessIsDenied("u11", "POST", "/" + randomIndex() + "/_mget", "{ \"ids\" : [ \"1\", \"2\" ] } "); - assertBodyHasAccessIsDenied("u11", "PUT", - "/" + randomIndex() + "/_bulk", "{ \"index\" : { \"_id\" : \"123\" } }\n{ \"foo\" : \"bar\" }\n"); - assertAccessIsDenied("u11", - "GET", "/" + randomIndex() + "/_mtermvectors", "{ \"docs\" : [ { \"_id\": \"1\" }, { \"_id\": \"2\" } ] }"); + assertBodyHasAccessIsDenied( + "u11", + "PUT", + "/" + randomIndex() + "/_bulk", + "{ \"index\" : { \"_id\" : \"123\" } }\n{ \"foo\" : \"bar\" }\n" + ); + assertAccessIsDenied( + "u11", + "GET", + "/" + randomIndex() + "/_mtermvectors", + "{ \"docs\" : [ { \"_id\": \"1\" }, { \"_id\": \"2\" } ] }" + ); assertAccessIsDenied("u11", randomFrom("GET", "POST"), "/" + "b" + "/_field_caps?fields=*"); assertAccessIsDenied("u11", randomFrom("GET", "POST"), "/" + "c" + "/_field_caps?fields=*"); } @@ -355,13 +438,20 @@ public void testUserU12() throws Exception { assertUserIsAllowed("u12", "data_access", "b"); assertUserIsDenied("u12", "manage", "c"); assertUserIsAllowed("u12", "data_access", "c"); - assertAccessIsAllowed("u12", - "GET", "/" + randomIndex() + "/_msearch", "{}\n{ \"query\" : { \"match_all\" : {} } }\n"); + assertAccessIsAllowed("u12", "GET", "/" + randomIndex() + "/_msearch", "{}\n{ \"query\" : { \"match_all\" : {} } }\n"); assertAccessIsAllowed("u12", "POST", "/" + randomIndex() + "/_mget", "{ \"ids\" : [ \"1\", \"2\" ] } "); - assertAccessIsAllowed("u12", "PUT", - "/" + randomIndex() + "/_bulk", "{ \"index\" : { \"_id\" : \"123\" } }\n{ \"foo\" : \"bar\" }\n"); - assertAccessIsAllowed("u12", - "GET", "/" + randomIndex() + "/_mtermvectors", "{ \"docs\" : [ { \"_id\": \"1\" }, { \"_id\": \"2\" } ] }"); + assertAccessIsAllowed( + "u12", + "PUT", + "/" + randomIndex() + "/_bulk", + "{ \"index\" : { \"_id\" : \"123\" } }\n{ \"foo\" : \"bar\" }\n" + ); + assertAccessIsAllowed( + "u12", + "GET", + "/" + randomIndex() + "/_mtermvectors", + "{ \"docs\" : [ { \"_id\": \"1\" }, { \"_id\": \"2\" } ] }" + ); } public void testUserU13() throws Exception { @@ -377,13 +467,16 @@ public void testUserU13() throws Exception { assertUserIsDenied("u13", "all", "c"); - assertAccessIsAllowed("u13", - "GET", "/" + randomIndex() + "/_msearch", "{}\n{ \"query\" : { \"match_all\" : {} } }\n"); + assertAccessIsAllowed("u13", "GET", "/" + randomIndex() + "/_msearch", "{}\n{ \"query\" : { \"match_all\" : {} } }\n"); assertAccessIsAllowed("u13", "POST", "/" + randomIndex() + "/_mget", "{ \"ids\" : [ \"1\", \"2\" ] } "); assertAccessIsAllowed("u13", "PUT", "/a/_bulk", "{ \"index\" : { \"_id\" : \"123\" } }\n{ \"foo\" : \"bar\" }\n"); assertBodyHasAccessIsDenied("u13", "PUT", "/b/_bulk", "{ \"index\" : { \"_id\" : \"123\" } }\n{ \"foo\" : \"bar\" }\n"); - assertAccessIsAllowed("u13", - "GET", "/" + randomIndex() + "/_mtermvectors", "{ \"docs\" : [ { \"_id\": \"1\" }, { \"_id\": \"2\" } ] }"); + assertAccessIsAllowed( + "u13", + "GET", + "/" + randomIndex() + "/_mtermvectors", + "{ \"docs\" : [ { \"_id\": \"1\" }, { \"_id\": \"2\" } ] }" + ); assertAccessIsDenied("u13", randomFrom("GET", "POST"), "/" + "a" + "/_field_caps?fields=*"); } @@ -400,13 +493,20 @@ public void testUserU14() throws Exception { assertUserIsDenied("u14", "all", "c"); - assertAccessIsAllowed("u14", - "GET", "/" + randomIndex() + "/_msearch", "{}\n{ \"query\" : { \"match_all\" : {} } }\n"); + assertAccessIsAllowed("u14", "GET", "/" + randomIndex() + "/_msearch", "{}\n{ \"query\" : { \"match_all\" : {} } }\n"); assertAccessIsAllowed("u14", "POST", "/" + randomIndex() + "/_mget", "{ \"ids\" : [ \"1\", \"2\" ] } "); - assertAccessIsDenied("u14", "PUT", - "/" + randomIndex() + "/_bulk", "{ \"index\" : { \"_id\" : \"123\" } }\n{ \"foo\" : \"bar\" }\n"); - assertAccessIsAllowed("u14", - "GET", "/" + randomIndex() + "/_mtermvectors", "{ \"docs\" : [ { \"_id\": \"1\" }, { \"_id\": \"2\" } ] }"); + assertAccessIsDenied( + "u14", + "PUT", + "/" + randomIndex() + "/_bulk", + "{ \"index\" : { \"_id\" : \"123\" } }\n{ \"foo\" : \"bar\" }\n" + ); + assertAccessIsAllowed( + "u14", + "GET", + "/" + randomIndex() + "/_mtermvectors", + "{ \"docs\" : [ { \"_id\": \"1\" }, { \"_id\": \"2\" } ] }" + ); assertAccessIsDenied("u14", randomFrom("GET", "POST"), "/" + "b" + "/_field_caps?fields=*"); } @@ -438,8 +538,10 @@ public void testThatUnknownUserIsRejectedProperly() throws Exception { try { Request request = new Request("GET", "/"); RequestOptions.Builder options = request.getOptions().toBuilder(); - options.addHeader("Authorization", - UsernamePasswordToken.basicAuthHeaderValue("idonotexist", new SecureString("passwd".toCharArray()))); + options.addHeader( + "Authorization", + UsernamePasswordToken.basicAuthHeaderValue("idonotexist", new SecureString("passwd".toCharArray())) + ); request.setOptions(options); getRestClient().performRequest(request); fail("request should have failed"); @@ -585,8 +687,12 @@ private void assertUserExecutes(String user, String action, String index, boolea if (userIsAllowed) { assertAccessIsAllowed(user, "PUT", "/" + index + "/_doc/321", "{ \"foo\" : \"bar\" }"); // test auto mapping update is allowed but deprecated - Response response = assertAccessIsAllowed(user, "PUT", "/" + index + "/_doc/4321", "{ \"" + - UUIDs.randomBase64UUID() + "\" : \"foo\" }"); + Response response = assertAccessIsAllowed( + user, + "PUT", + "/" + index + "/_doc/4321", + "{ \"" + UUIDs.randomBase64UUID() + "\" : \"foo\" }" + ); String warningHeader = response.getHeader("Warning"); assertThat( warningHeader, @@ -598,13 +704,24 @@ private void assertUserExecutes(String user, String action, String index, boolea ) ); assertAccessIsAllowed(user, "POST", "/" + index + "/_update/321", "{ \"doc\" : { \"foo\" : \"baz\" } }"); - response = assertAccessIsAllowed(user, "POST", "/" + index + "/_update/321", - "{ \"doc\" : { \"" + UUIDs.randomBase64UUID() + "\" : \"baz\" } }"); + response = assertAccessIsAllowed( + user, + "POST", + "/" + index + "/_update/321", + "{ \"doc\" : { \"" + UUIDs.randomBase64UUID() + "\" : \"baz\" } }" + ); warningHeader = response.getHeader("Warning"); - assertThat(warningHeader, containsString("the index privilege [index] allowed the update mapping action " + - "[indices:admin/mapping/auto_put] on index [" + index + "], this privilege will not permit mapping updates in" + - " the next major release - users who require access to update mappings must be" + - " granted explicit privileges")); + assertThat( + warningHeader, + containsString( + "the index privilege [index] allowed the update mapping action " + + "[indices:admin/mapping/auto_put] on index [" + + index + + "], this privilege will not permit mapping updates in" + + " the next major release - users who require access to update mappings must be" + + " granted explicit privileges" + ) + ); assertThat( warningHeader, containsString( @@ -638,17 +755,39 @@ private void assertUserExecutes(String user, String action, String index, boolea assertAccessIsAllowed(user, "PUT", "/" + index + "/_doc/321", "{ \"foo\" : \"bar\" }"); // test auto mapping update is allowed but deprecated - Response response = assertAccessIsAllowed(user, "PUT", "/" + index + "/_doc/4321", "{ \"" + - UUIDs.randomBase64UUID() + "\" : \"foo\" }"); + Response response = assertAccessIsAllowed( + user, + "PUT", + "/" + index + "/_doc/4321", + "{ \"" + UUIDs.randomBase64UUID() + "\" : \"foo\" }" + ); String warningHeader = response.getHeader("Warning"); - assertThat(warningHeader, containsString("the index privilege [write] allowed the update mapping action [" + - "indices:admin/mapping/auto_put] on index [" + index + "]")); + assertThat( + warningHeader, + containsString( + "the index privilege [write] allowed the update mapping action [" + + "indices:admin/mapping/auto_put] on index [" + + index + + "]" + ) + ); assertAccessIsAllowed(user, "POST", "/" + index + "/_update/321", "{ \"doc\" : { \"foo\" : \"baz\" } }"); - response = assertAccessIsAllowed(user, "POST", "/" + index + "/_update/321", - "{ \"doc\" : { \"" + UUIDs.randomBase64UUID() + "\" : \"baz\" } }"); + response = assertAccessIsAllowed( + user, + "POST", + "/" + index + "/_update/321", + "{ \"doc\" : { \"" + UUIDs.randomBase64UUID() + "\" : \"baz\" } }" + ); warningHeader = response.getHeader("Warning"); - assertThat(warningHeader, containsString("the index privilege [write] allowed the update mapping action [" + - "indices:admin/mapping/auto_put] on index [" + index + "]")); + assertThat( + warningHeader, + containsString( + "the index privilege [write] allowed the update mapping action [" + + "indices:admin/mapping/auto_put] on index [" + + index + + "]" + ) + ); } else { assertUserIsDenied(user, "index", index); assertUserIsDenied(user, "delete", index); diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/IndicesPermissionsWithAliasesWildcardsAndRegexsTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/IndicesPermissionsWithAliasesWildcardsAndRegexsTests.java index 91210395c38af..a624879e20635 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/IndicesPermissionsWithAliasesWildcardsAndRegexsTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/IndicesPermissionsWithAliasesWildcardsAndRegexsTests.java @@ -20,9 +20,9 @@ import org.elasticsearch.index.mapper.DateFieldMapper; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.plugins.Plugin; +import org.elasticsearch.test.SecurityIntegTestCase; import org.elasticsearch.test.SecuritySettingsSourceField; import org.elasticsearch.xpack.core.XPackSettings; -import org.elasticsearch.test.SecurityIntegTestCase; import org.elasticsearch.xpack.core.action.CreateDataStreamAction; import org.elasticsearch.xpack.core.action.DeleteDataStreamAction; import org.elasticsearch.xpack.datastreams.DataStreamsPlugin; @@ -36,9 +36,9 @@ import static org.elasticsearch.action.support.WriteRequest.RefreshPolicy.IMMEDIATE; import static org.elasticsearch.cluster.metadata.MetadataIndexTemplateService.DEFAULT_TIMESTAMP_FIELD; +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken.BASIC_AUTH_HEADER; import static org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken.basicAuthHeaderValue; -import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.hamcrest.Matchers.equalTo; public class IndicesPermissionsWithAliasesWildcardsAndRegexsTests extends SecurityIntegTestCase { @@ -48,42 +48,40 @@ public class IndicesPermissionsWithAliasesWildcardsAndRegexsTests extends Securi @Override protected String configUsers() { final String usersPasswdHashed = new String(getFastStoredHashAlgoForTests().hash(USERS_PASSWD)); - return super.configUsers() + - "user1:" + usersPasswdHashed + "\n"; + return super.configUsers() + "user1:" + usersPasswdHashed + "\n"; } @Override protected String configUsersRoles() { - return super.configUsersRoles() + - "role1:user1\n"; + return super.configUsersRoles() + "role1:user1\n"; } @Override protected String configRoles() { - return super.configRoles() + - "\nrole1:\n" + - " cluster: [ all ]\n" + - " indices:\n" + - " - names: 't*'\n" + - " privileges: [ALL]\n" + - " field_security:\n" + - " grant: [ field1 ]\n" + - " - names: 'my_alias'\n" + - " privileges: [ALL]\n" + - " field_security:\n" + - " grant: [ field2 ]\n" + - " - names: '/an_.*/'\n" + - " privileges: [ALL]\n" + - " field_security:\n" + - " grant: [ field3 ]\n"; + return super.configRoles() + + "\nrole1:\n" + + " cluster: [ all ]\n" + + " indices:\n" + + " - names: 't*'\n" + + " privileges: [ALL]\n" + + " field_security:\n" + + " grant: [ field1 ]\n" + + " - names: 'my_alias'\n" + + " privileges: [ALL]\n" + + " field_security:\n" + + " grant: [ field2 ]\n" + + " - names: '/an_.*/'\n" + + " privileges: [ALL]\n" + + " field_security:\n" + + " grant: [ field3 ]\n"; } @Override public Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { return Settings.builder() - .put(super.nodeSettings(nodeOrdinal, otherSettings)) - .put(XPackSettings.DLS_FLS_ENABLED.getKey(), true) - .build(); + .put(super.nodeSettings(nodeOrdinal, otherSettings)) + .put(XPackSettings.DLS_FLS_ENABLED.getKey(), true) + .build(); } @Override @@ -94,59 +92,63 @@ protected Collection> nodePlugins() { } public void testGetResolveWildcardsRegexs() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test") - .setMapping("field1", "type=text", "field2", "type=text") - .addAlias(new Alias("my_alias")) - .addAlias(new Alias("an_alias")) + assertAcked( + client().admin() + .indices() + .prepareCreate("test") + .setMapping("field1", "type=text", "field2", "type=text") + .addAlias(new Alias("my_alias")) + .addAlias(new Alias("an_alias")) ); - client().prepareIndex("test").setId("1").setSource("field1", "value1", "field2", "value2", "field3", "value3") - .setRefreshPolicy(IMMEDIATE) - .get(); + client().prepareIndex("test") + .setId("1") + .setSource("field1", "value1", "field2", "value2", "field3", "value3") + .setRefreshPolicy(IMMEDIATE) + .get(); - GetResponse getResponse = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareGet("test", "1") - .get(); + GetResponse getResponse = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).prepareGet("test", "1").get(); assertThat(getResponse.getSource().size(), equalTo(1)); assertThat((String) getResponse.getSource().get("field1"), equalTo("value1")); - getResponse = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareGet("my_alias", "1") - .get(); + getResponse = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) + .prepareGet("my_alias", "1") + .get(); assertThat(getResponse.getSource().size(), equalTo(1)); assertThat((String) getResponse.getSource().get("field2"), equalTo("value2")); - getResponse = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareGet("an_alias", "1") - .get(); + getResponse = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) + .prepareGet("an_alias", "1") + .get(); assertThat(getResponse.getSource().size(), equalTo(1)); assertThat((String) getResponse.getSource().get("field3"), equalTo("value3")); } public void testSearchResolveWildcardsRegexs() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test") - .setMapping("field1", "type=text", "field2", "type=text") - .addAlias(new Alias("my_alias")) - .addAlias(new Alias("an_alias")) + assertAcked( + client().admin() + .indices() + .prepareCreate("test") + .setMapping("field1", "type=text", "field2", "type=text") + .addAlias(new Alias("my_alias")) + .addAlias(new Alias("an_alias")) ); - client().prepareIndex("test").setId("1").setSource("field1", "value1", "field2", "value2", "field3", "value3") + client().prepareIndex("test") + .setId("1") + .setSource("field1", "value1", "field2", "value2", "field3", "value3") .setRefreshPolicy(IMMEDIATE) .get(); - SearchResponse response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(QueryBuilders.termQuery("_id", "1")) - .get(); + SearchResponse response = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).prepareSearch("test").setQuery(QueryBuilders.termQuery("_id", "1")).get(); assertThat(response.getHits().getHits().length, equalTo(1)); Map source = response.getHits().getHits()[0].getSourceAsMap(); assertThat(source.size(), equalTo(1)); assertThat((String) source.get("field1"), equalTo("value1")); - response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) + response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) .prepareSearch("my_alias") .setQuery(QueryBuilders.termQuery("_id", "1")) .get(); @@ -155,9 +157,7 @@ public void testSearchResolveWildcardsRegexs() throws Exception { assertThat(source.size(), equalTo(1)); assertThat((String) source.get("field2"), equalTo("value2")); - - response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) + response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) .prepareSearch("an_alias") .setQuery(QueryBuilders.termQuery("_id", "1")) .get(); @@ -166,8 +166,7 @@ public void testSearchResolveWildcardsRegexs() throws Exception { assertThat(source.size(), equalTo(1)); assertThat((String) source.get("field3"), equalTo("value3")); - response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) + response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) .prepareSearch("*_alias") .setQuery(QueryBuilders.termQuery("_id", "1")) .get(); @@ -177,8 +176,7 @@ public void testSearchResolveWildcardsRegexs() throws Exception { assertThat((String) source.get("field2"), equalTo("value2")); assertThat((String) source.get("field3"), equalTo("value3")); - response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) + response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) .prepareSearch("*_alias", "t*") .setQuery(QueryBuilders.termQuery("_id", "1")) .get(); @@ -196,9 +194,10 @@ public void testSearchResolveDataStreams() throws Exception { client().execute(CreateDataStreamAction.INSTANCE, createDataStreamRequest).get(); IndicesAliasesRequest aliasesRequest = new IndicesAliasesRequest(); - aliasesRequest.addAliasAction(new IndicesAliasesRequest.AliasActions(IndicesAliasesRequest.AliasActions.Type.ADD) - .aliases("my_alias", "an_alias") - .index("test")); + aliasesRequest.addAliasAction( + new IndicesAliasesRequest.AliasActions(IndicesAliasesRequest.AliasActions.Type.ADD).aliases("my_alias", "an_alias") + .index("test") + ); assertAcked(client().admin().indices().aliases(aliasesRequest).actionGet()); try { @@ -210,18 +209,15 @@ public void testSearchResolveDataStreams() throws Exception { .setRefreshPolicy(IMMEDIATE) .get(); - SearchResponse response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) - .prepareSearch("test") - .setQuery(QueryBuilders.termQuery("_id", "1")) - .get(); + SearchResponse response = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD)) + ).prepareSearch("test").setQuery(QueryBuilders.termQuery("_id", "1")).get(); assertThat(response.getHits().getHits().length, equalTo(1)); Map source = response.getHits().getHits()[0].getSourceAsMap(); assertThat(source.size(), equalTo(1)); assertThat((String) source.get("field1"), equalTo("value1")); - response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) + response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) .prepareSearch("my_alias") .setQuery(QueryBuilders.termQuery("_id", "1")) .get(); @@ -230,8 +226,7 @@ public void testSearchResolveDataStreams() throws Exception { assertThat(source.size(), equalTo(1)); assertThat((String) source.get("field2"), equalTo("value2")); - response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) + response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) .prepareSearch("an_alias") .setQuery(QueryBuilders.termQuery("_id", "1")) .get(); @@ -240,8 +235,7 @@ public void testSearchResolveDataStreams() throws Exception { assertThat(source.size(), equalTo(1)); assertThat((String) source.get("field3"), equalTo("value3")); - response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) + response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) .prepareSearch("*_alias") .setQuery(QueryBuilders.termQuery("_id", "1")) .get(); @@ -251,8 +245,7 @@ public void testSearchResolveDataStreams() throws Exception { assertThat((String) source.get("field2"), equalTo("value2")); assertThat((String) source.get("field3"), equalTo("value3")); - response = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) + response = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user1", USERS_PASSWD))) .prepareSearch("*_alias", "t*") .setQuery(QueryBuilders.termQuery("_id", "1")) .get(); @@ -265,11 +258,11 @@ public void testSearchResolveDataStreams() throws Exception { } finally { AcknowledgedResponse response = client().execute( DeleteDataStreamAction.INSTANCE, - new DeleteDataStreamAction.Request(new String[]{"*"}) + new DeleteDataStreamAction.Request(new String[] { "*" }) ).actionGet(); assertAcked(response); - DeleteDataStreamAction.Request deleteDSRequest = new DeleteDataStreamAction.Request(new String[]{"*"}); + DeleteDataStreamAction.Request deleteDSRequest = new DeleteDataStreamAction.Request(new String[] { "*" }); client().execute(DeleteDataStreamAction.INSTANCE, deleteDSRequest).actionGet(); DeleteComposableIndexTemplateAction.Request deleteTemplateRequest = new DeleteComposableIndexTemplateAction.Request("*"); client().execute(DeleteComposableIndexTemplateAction.INSTANCE, deleteTemplateRequest).actionGet(); diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/KibanaSystemRoleIntegTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/KibanaSystemRoleIntegTests.java index 021103502ac30..e221bad6b2c1c 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/KibanaSystemRoleIntegTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/KibanaSystemRoleIntegTests.java @@ -28,42 +28,32 @@ public class KibanaSystemRoleIntegTests extends SecurityIntegTestCase { @Override public String configUsers() { final String usersPasswdHashed = new String(getFastStoredHashAlgoForTests().hash(USERS_PASSWD)); - return super.configUsers() + - "my_kibana_system:" + usersPasswdHashed; + return super.configUsers() + "my_kibana_system:" + usersPasswdHashed; } @Override public String configUsersRoles() { - return super.configUsersRoles() + - "kibana_system:my_kibana_system"; + return super.configUsersRoles() + "kibana_system:my_kibana_system"; } - public void testCreateIndexDeleteInKibanaIndex() throws Exception { - final String index = randomBoolean()? ".kibana" : ".kibana-" + randomAlphaOfLengthBetween(1, 10).toLowerCase(Locale.ENGLISH); + final String index = randomBoolean() ? ".kibana" : ".kibana-" + randomAlphaOfLengthBetween(1, 10).toLowerCase(Locale.ENGLISH); if (randomBoolean()) { - CreateIndexResponse createIndexResponse = client().filterWithHeader(singletonMap("Authorization", - UsernamePasswordToken.basicAuthHeaderValue("my_kibana_system", USERS_PASSWD))) - .admin().indices().prepareCreate(index).get(); + CreateIndexResponse createIndexResponse = client().filterWithHeader( + singletonMap("Authorization", UsernamePasswordToken.basicAuthHeaderValue("my_kibana_system", USERS_PASSWD)) + ).admin().indices().prepareCreate(index).get(); assertThat(createIndexResponse.isAcknowledged(), is(true)); } - IndexResponse response = client() - .filterWithHeader(singletonMap("Authorization", - UsernamePasswordToken.basicAuthHeaderValue("my_kibana_system", USERS_PASSWD))) - .prepareIndex() - .setIndex(index) - .setSource("foo", "bar") - .setRefreshPolicy(IMMEDIATE) - .get(); + IndexResponse response = client().filterWithHeader( + singletonMap("Authorization", UsernamePasswordToken.basicAuthHeaderValue("my_kibana_system", USERS_PASSWD)) + ).prepareIndex().setIndex(index).setSource("foo", "bar").setRefreshPolicy(IMMEDIATE).get(); assertEquals(DocWriteResponse.Result.CREATED, response.getResult()); - DeleteResponse deleteResponse = client() - .filterWithHeader(singletonMap("Authorization", - UsernamePasswordToken.basicAuthHeaderValue("my_kibana_system", USERS_PASSWD))) - .prepareDelete(index, response.getId()) - .get(); + DeleteResponse deleteResponse = client().filterWithHeader( + singletonMap("Authorization", UsernamePasswordToken.basicAuthHeaderValue("my_kibana_system", USERS_PASSWD)) + ).prepareDelete(index, response.getId()).get(); assertEquals(DocWriteResponse.Result.DELETED, deleteResponse.getResult()); } } diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/KibanaUserRoleIntegTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/KibanaUserRoleIntegTests.java index 679aa3e357c81..ad10488bf5f60 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/KibanaUserRoleIntegTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/KibanaUserRoleIntegTests.java @@ -38,27 +38,25 @@ public class KibanaUserRoleIntegTests extends NativeRealmIntegTestCase { @Override public String configRoles() { - return super.configRoles() + "\n" + - "my_kibana_user:\n" + - " indices:\n" + - " - names: 'logstash-*'\n" + - " privileges:\n" + - " - view_index_metadata\n" + - " - read\n"; + return super.configRoles() + + "\n" + + "my_kibana_user:\n" + + " indices:\n" + + " - names: 'logstash-*'\n" + + " privileges:\n" + + " - view_index_metadata\n" + + " - read\n"; } @Override public String configUsers() { final String usersPasswdHashed = new String(getFastStoredHashAlgoForTests().hash(USERS_PASSWD)); - return super.configUsers() + - "kibana_user:" + usersPasswdHashed; + return super.configUsers() + "kibana_user:" + usersPasswdHashed; } @Override public String configUsersRoles() { - return super.configUsersRoles() + - "my_kibana_user:kibana_user\n" + - "kibana_user:kibana_user"; + return super.configUsersRoles() + "my_kibana_user:kibana_user\n" + "kibana_user:kibana_user"; } public void testFieldMappings() throws Exception { @@ -66,16 +64,19 @@ public void testFieldMappings() throws Exception { final String field = "foo"; indexRandom(true, client().prepareIndex().setIndex(index).setSource(field, "bar")); - GetFieldMappingsResponse response = client().admin().indices().prepareGetFieldMappings().addIndices("logstash-*").setFields("*") - .includeDefaults(true).get(); + GetFieldMappingsResponse response = client().admin() + .indices() + .prepareGetFieldMappings() + .addIndices("logstash-*") + .setFields("*") + .includeDefaults(true) + .get(); FieldMappingMetadata fieldMappingMetadata = response.fieldMappings(index, field); assertThat(fieldMappingMetadata, notNullValue()); - response = client() - .filterWithHeader(singletonMap("Authorization", UsernamePasswordToken.basicAuthHeaderValue("kibana_user", USERS_PASSWD))) - .admin().indices().prepareGetFieldMappings().addIndices("logstash-*") - .setFields("*") - .includeDefaults(true).get(); + response = client().filterWithHeader( + singletonMap("Authorization", UsernamePasswordToken.basicAuthHeaderValue("kibana_user", USERS_PASSWD)) + ).admin().indices().prepareGetFieldMappings().addIndices("logstash-*").setFields("*").includeDefaults(true).get(); FieldMappingMetadata fieldMappingMetadata1 = response.fieldMappings(index, field); assertThat(fieldMappingMetadata1, notNullValue()); assertThat(fieldMappingMetadata1.fullName(), equalTo(fieldMappingMetadata.fullName())); @@ -87,15 +88,16 @@ public void testValidateQuery() throws Exception { final String field = "foo"; indexRandom(true, client().prepareIndex().setIndex(index).setSource(field, "bar")); - ValidateQueryResponse response = client().admin().indices() - .prepareValidateQuery(index).setQuery(QueryBuilders.termQuery(field, "bar")).get(); + ValidateQueryResponse response = client().admin() + .indices() + .prepareValidateQuery(index) + .setQuery(QueryBuilders.termQuery(field, "bar")) + .get(); assertThat(response.isValid(), is(true)); - response = client() - .filterWithHeader(singletonMap("Authorization", UsernamePasswordToken.basicAuthHeaderValue("kibana_user", USERS_PASSWD))) - .admin().indices() - .prepareValidateQuery(index) - .setQuery(QueryBuilders.termQuery(field, "bar")).get(); + response = client().filterWithHeader( + singletonMap("Authorization", UsernamePasswordToken.basicAuthHeaderValue("kibana_user", USERS_PASSWD)) + ).admin().indices().prepareValidateQuery(index).setQuery(QueryBuilders.termQuery(field, "bar")).get(); assertThat(response.isValid(), is(true)); } @@ -108,21 +110,19 @@ public void testSearchAndMSearch() throws Exception { SearchResponse response = client().prepareSearch(index).setQuery(QueryBuilders.matchAllQuery()).get(); final long hits = response.getHits().getTotalHits().value; assertThat(hits, greaterThan(0L)); - response = client() - .filterWithHeader(singletonMap("Authorization", UsernamePasswordToken.basicAuthHeaderValue("kibana_user", USERS_PASSWD))) - .prepareSearch(index) - .setQuery(QueryBuilders.matchAllQuery()).get(); + response = client().filterWithHeader( + singletonMap("Authorization", UsernamePasswordToken.basicAuthHeaderValue("kibana_user", USERS_PASSWD)) + ).prepareSearch(index).setQuery(QueryBuilders.matchAllQuery()).get(); assertEquals(response.getHits().getTotalHits().value, hits); - MultiSearchResponse multiSearchResponse = client().prepareMultiSearch() - .add(client().prepareSearch(index).setQuery(QueryBuilders.matchAllQuery())).get(); + .add(client().prepareSearch(index).setQuery(QueryBuilders.matchAllQuery())) + .get(); final long multiHits = multiSearchResponse.getResponses()[0].getResponse().getHits().getTotalHits().value; assertThat(hits, greaterThan(0L)); - multiSearchResponse = client() - .filterWithHeader(singletonMap("Authorization", UsernamePasswordToken.basicAuthHeaderValue("kibana_user", USERS_PASSWD))) - .prepareMultiSearch() - .add(client().prepareSearch(index).setQuery(QueryBuilders.matchAllQuery())).get(); + multiSearchResponse = client().filterWithHeader( + singletonMap("Authorization", UsernamePasswordToken.basicAuthHeaderValue("kibana_user", USERS_PASSWD)) + ).prepareMultiSearch().add(client().prepareSearch(index).setQuery(QueryBuilders.matchAllQuery())).get(); assertEquals(multiSearchResponse.getResponses()[0].getResponse().getHits().getTotalHits().value, multiHits); } @@ -135,10 +135,9 @@ public void testGetIndex() throws Exception { GetIndexResponse response = client().admin().indices().prepareGetIndex().setIndices(index).get(); assertThat(response.getIndices(), arrayContaining(index)); - response = client() - .filterWithHeader(singletonMap("Authorization", UsernamePasswordToken.basicAuthHeaderValue("kibana_user", USERS_PASSWD))) - .admin().indices().prepareGetIndex() - .setIndices(index).get(); + response = client().filterWithHeader( + singletonMap("Authorization", UsernamePasswordToken.basicAuthHeaderValue("kibana_user", USERS_PASSWD)) + ).admin().indices().prepareGetIndex().setIndices(index).get(); assertThat(response.getIndices(), arrayContaining(index)); } @@ -148,12 +147,9 @@ public void testGetMappings() throws Exception { final String field = "foo"; indexRandom(true, client().prepareIndex().setIndex(index).setSource(field, "bar")); - GetMappingsResponse response = client() - .filterWithHeader(singletonMap("Authorization", UsernamePasswordToken.basicAuthHeaderValue("kibana_user", USERS_PASSWD))) - .admin() - .indices() - .prepareGetMappings("logstash-*") - .get(); + GetMappingsResponse response = client().filterWithHeader( + singletonMap("Authorization", UsernamePasswordToken.basicAuthHeaderValue("kibana_user", USERS_PASSWD)) + ).admin().indices().prepareGetMappings("logstash-*").get(); ImmutableOpenMap mappingsMap = response.getMappings(); assertNotNull(mappingsMap); assertNotNull(mappingsMap.get(index)); diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/MultipleIndicesPermissionsTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/MultipleIndicesPermissionsTests.java index 9fffa56ed61a5..c93f250e3f6d2 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/MultipleIndicesPermissionsTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/MultipleIndicesPermissionsTests.java @@ -38,12 +38,12 @@ import java.util.List; import static org.elasticsearch.action.support.WriteRequest.RefreshPolicy.IMMEDIATE; -import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery; import static org.elasticsearch.test.SecuritySettingsSource.SECURITY_REQUEST_OPTIONS; +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken.BASIC_AUTH_HEADER; import static org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken.basicAuthHeaderValue; import static org.hamcrest.Matchers.containsInAnyOrder; @@ -58,9 +58,16 @@ public class MultipleIndicesPermissionsTests extends SecurityIntegTestCase { @Before public void waitForSecurityIndexWritable() throws Exception { // adds a dummy user to the native realm to force .security index creation - new TestRestHighLevelClient().security().putUser( - PutUserRequest.withPassword(new User("dummy_user", List.of("missing_role")), "password".toCharArray(), true, - RefreshPolicy.IMMEDIATE), SECURITY_REQUEST_OPTIONS); + new TestRestHighLevelClient().security() + .putUser( + PutUserRequest.withPassword( + new User("dummy_user", List.of("missing_role")), + "password".toCharArray(), + true, + RefreshPolicy.IMMEDIATE + ), + SECURITY_REQUEST_OPTIONS + ); assertSecurityIndexActive(); } @@ -76,66 +83,66 @@ protected boolean addMockHttpTransport() { @Override protected String configRoles() { - return SecuritySettingsSource.TEST_ROLE + ":\n" + - " cluster: [ all ]\n" + - " indices:\n" + - " - names: '*'\n" + - " privileges: [manage]\n" + - " - names: '/.*/'\n" + - " privileges: [write]\n" + - " - names: 'test'\n" + - " privileges: [read]\n" + - " - names: 'test1'\n" + - " privileges: [read]\n" + - "\n" + - "role_a:\n" + - " indices:\n" + - " - names: 'a'\n" + - " privileges: [all]\n" + - " - names: 'alias1'\n" + - " privileges: [read]\n" + - "\n" + - "role_monitor_all_unrestricted_indices:\n" + - " cluster: [monitor]\n" + - " indices:\n" + - " - names: '*'\n" + - " privileges: [monitor]\n" + - "\n" + - "role_b:\n" + - " indices:\n" + - " - names: 'b'\n" + - " privileges: [all]\n"; + return SecuritySettingsSource.TEST_ROLE + + ":\n" + + " cluster: [ all ]\n" + + " indices:\n" + + " - names: '*'\n" + + " privileges: [manage]\n" + + " - names: '/.*/'\n" + + " privileges: [write]\n" + + " - names: 'test'\n" + + " privileges: [read]\n" + + " - names: 'test1'\n" + + " privileges: [read]\n" + + "\n" + + "role_a:\n" + + " indices:\n" + + " - names: 'a'\n" + + " privileges: [all]\n" + + " - names: 'alias1'\n" + + " privileges: [read]\n" + + "\n" + + "role_monitor_all_unrestricted_indices:\n" + + " cluster: [monitor]\n" + + " indices:\n" + + " - names: '*'\n" + + " privileges: [monitor]\n" + + "\n" + + "role_b:\n" + + " indices:\n" + + " - names: 'b'\n" + + " privileges: [all]\n"; } @Override protected String configUsers() { final String usersPasswdHashed = new String(getFastStoredHashAlgoForTests().hash(USERS_PASSWD)); - return SecuritySettingsSource.CONFIG_STANDARD_USER + - "user_a:" + usersPasswdHashed + "\n" + - "user_ab:" + usersPasswdHashed + "\n" + - "user_monitor:" + usersPasswdHashed + "\n"; + return SecuritySettingsSource.CONFIG_STANDARD_USER + + "user_a:" + + usersPasswdHashed + + "\n" + + "user_ab:" + + usersPasswdHashed + + "\n" + + "user_monitor:" + + usersPasswdHashed + + "\n"; } @Override protected String configUsersRoles() { - return SecuritySettingsSource.CONFIG_STANDARD_USER_ROLES + - "role_a:user_a,user_ab\n" + - "role_b:user_ab\n" + - "role_monitor_all_unrestricted_indices:user_monitor\n"; + return SecuritySettingsSource.CONFIG_STANDARD_USER_ROLES + + "role_a:user_a,user_ab\n" + + "role_b:user_ab\n" + + "role_monitor_all_unrestricted_indices:user_monitor\n"; } public void testSingleRole() throws Exception { - IndexResponse indexResponse = index("test", jsonBuilder() - .startObject() - .field("name", "value") - .endObject()); + IndexResponse indexResponse = index("test", jsonBuilder().startObject().field("name", "value").endObject()); assertEquals(DocWriteResponse.Result.CREATED, indexResponse.getResult()); - - indexResponse = index("test1", jsonBuilder() - .startObject() - .field("name", "value1") - .endObject()); + indexResponse = index("test1", jsonBuilder().startObject().field("name", "value1").endObject()); assertEquals(DocWriteResponse.Result.CREATED, indexResponse.getResult()); refresh(); @@ -166,9 +173,9 @@ public void testSingleRole() throws Exception { } MultiSearchResponse msearchResponse = client.prepareMultiSearch() - .add(client.prepareSearch("test")) - .add(client.prepareSearch("test1")) - .get(); + .add(client.prepareSearch("test")) + .add(client.prepareSearch("test1")) + .get(); MultiSearchResponse.Item[] items = msearchResponse.getResponses(); assertThat(items.length, is(2)); assertThat(items[0].isFailure(), is(false)); @@ -183,28 +190,20 @@ public void testSingleRole() throws Exception { public void testMonitorRestrictedWildcards() throws Exception { - IndexResponse indexResponse = index("foo", jsonBuilder() - .startObject() - .field("name", "value") - .endObject()); + IndexResponse indexResponse = index("foo", jsonBuilder().startObject().field("name", "value").endObject()); assertEquals(DocWriteResponse.Result.CREATED, indexResponse.getResult()); - indexResponse = index("foobar", jsonBuilder() - .startObject() - .field("name", "value") - .endObject()); + indexResponse = index("foobar", jsonBuilder().startObject().field("name", "value").endObject()); assertEquals(DocWriteResponse.Result.CREATED, indexResponse.getResult()); - indexResponse = index("foobarfoo", jsonBuilder() - .startObject() - .field("name", "value") - .endObject()); + indexResponse = index("foobarfoo", jsonBuilder().startObject().field("name", "value").endObject()); assertEquals(DocWriteResponse.Result.CREATED, indexResponse.getResult()); refresh(); - final Client client = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user_monitor", USERS_PASSWD))); + final Client client = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user_monitor", USERS_PASSWD)) + ); final GetSettingsResponse getSettingsResponse = client.admin().indices().prepareGetSettings(randomFrom("*", "_all", "foo*")).get(); assertThat(getSettingsResponse.getIndexToSettings().size(), is(3)); @@ -212,8 +211,11 @@ public void testMonitorRestrictedWildcards() throws Exception { assertThat(getSettingsResponse.getIndexToSettings().containsKey("foobar"), is(true)); assertThat(getSettingsResponse.getIndexToSettings().containsKey("foobarfoo"), is(true)); - final IndicesShardStoresResponse indicesShardsStoresResponse = client.admin().indices() - .prepareShardStores(randomFrom("*", "_all", "foo*")).setShardStatuses("all").get(); + final IndicesShardStoresResponse indicesShardsStoresResponse = client.admin() + .indices() + .prepareShardStores(randomFrom("*", "_all", "foo*")) + .setShardStatuses("all") + .get(); assertThat(indicesShardsStoresResponse.getStoreStatuses().size(), is(3)); assertThat(indicesShardsStoresResponse.getStoreStatuses().containsKey("foo"), is(true)); assertThat(indicesShardsStoresResponse.getStoreStatuses().containsKey("foobar"), is(true)); @@ -242,99 +244,93 @@ public void testMonitorRestrictedWildcards() throws Exception { } public void testMultipleRoles() throws Exception { - IndexResponse indexResponse = index("a", jsonBuilder() - .startObject() - .field("name", "value_a") - .endObject()); + IndexResponse indexResponse = index("a", jsonBuilder().startObject().field("name", "value_a").endObject()); assertEquals(DocWriteResponse.Result.CREATED, indexResponse.getResult()); - indexResponse = index("b", jsonBuilder() - .startObject() - .field("name", "value_b") - .endObject()); + indexResponse = index("b", jsonBuilder().startObject().field("name", "value_b").endObject()); assertEquals(DocWriteResponse.Result.CREATED, indexResponse.getResult()); refresh(); Client client = client(); - SearchResponse response = client - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user_a", USERS_PASSWD))) - .prepareSearch("a") - .get(); + SearchResponse response = client.filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user_a", USERS_PASSWD)) + ).prepareSearch("a").get(); assertNoFailures(response); assertHitCount(response, 1); - String[] indices = randomDouble() < 0.3 ? - new String[] { "_all"} : randomBoolean() ? - new String[] { "*" } : - new String[] {}; - response = client - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user_a", USERS_PASSWD))) - .prepareSearch(indices) - .get(); + String[] indices = randomDouble() < 0.3 ? new String[] { "_all" } : randomBoolean() ? new String[] { "*" } : new String[] {}; + response = client.filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user_a", USERS_PASSWD))) + .prepareSearch(indices) + .get(); assertNoFailures(response); assertHitCount(response, 1); try { indices = randomBoolean() ? new String[] { "a", "b" } : new String[] { "b", "a" }; - client - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user_a", USERS_PASSWD))) - .prepareSearch(indices) - .get(); - fail("expected an authorization excpetion when trying to search on multiple indices where there are no search permissions on " + - "one/some of them"); + client.filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user_a", USERS_PASSWD))) + .prepareSearch(indices) + .get(); + fail( + "expected an authorization excpetion when trying to search on multiple indices where there are no search permissions on " + + "one/some of them" + ); } catch (ElasticsearchSecurityException e) { // expected assertThat(e.status(), is(RestStatus.FORBIDDEN)); } response = client.filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user_ab", USERS_PASSWD))) - .prepareSearch("b") - .get(); + .prepareSearch("b") + .get(); assertNoFailures(response); assertHitCount(response, 1); indices = randomBoolean() ? new String[] { "a", "b" } : new String[] { "b", "a" }; response = client.filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user_ab", USERS_PASSWD))) - .prepareSearch(indices) - .get(); + .prepareSearch(indices) + .get(); assertNoFailures(response); assertHitCount(response, 2); - indices = randomDouble() < 0.3 ? - new String[] { "_all"} : randomBoolean() ? - new String[] { "*" } : - new String[] {}; + indices = randomDouble() < 0.3 ? new String[] { "_all" } : randomBoolean() ? new String[] { "*" } : new String[] {}; response = client.filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user_ab", USERS_PASSWD))) - .prepareSearch(indices) - .get(); + .prepareSearch(indices) + .get(); assertNoFailures(response); assertHitCount(response, 2); } public void testMultiNamesWorkCorrectly() { - assertAcked(client().admin().indices().prepareCreate("index1") - .setMapping("field1", "type=text") - .addAlias(new Alias("alias1").filter(QueryBuilders.termQuery("field1", "public"))) + assertAcked( + client().admin() + .indices() + .prepareCreate("index1") + .setMapping("field1", "type=text") + .addAlias(new Alias("alias1").filter(QueryBuilders.termQuery("field1", "public"))) ); client().prepareIndex("index1").setId("1").setSource("field1", "private").setRefreshPolicy(IMMEDIATE).get(); - final Client userAClient = client() - .filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user_a", USERS_PASSWD))); + final Client userAClient = client().filterWithHeader( + Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("user_a", USERS_PASSWD)) + ); - final SearchResponse searchResponse = userAClient - .prepareSearch("alias1").setSize(0).get(); + final SearchResponse searchResponse = userAClient.prepareSearch("alias1").setSize(0).get(); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(0L)); - final ElasticsearchSecurityException e1 = - expectThrows(ElasticsearchSecurityException.class, () -> userAClient.prepareSearch("index1").get()); + final ElasticsearchSecurityException e1 = expectThrows( + ElasticsearchSecurityException.class, + () -> userAClient.prepareSearch("index1").get() + ); assertThat(e1.getMessage(), containsString("is unauthorized for user [user_a]")); // The request should fail because index1 is directly requested and is not authorized for user_a - final ElasticsearchSecurityException e2 = - expectThrows(ElasticsearchSecurityException.class, () -> userAClient.prepareSearch("index1", "alias1").get()); + final ElasticsearchSecurityException e2 = expectThrows( + ElasticsearchSecurityException.class, + () -> userAClient.prepareSearch("index1", "alias1").get() + ); assertThat(e2.getMessage(), containsString("is unauthorized for user [user_a]")); } } diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/PermissionPrecedenceTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/PermissionPrecedenceTests.java index 8a7cc0ba6dfac..5c3ce15000a2b 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/PermissionPrecedenceTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/PermissionPrecedenceTests.java @@ -37,32 +37,29 @@ public class PermissionPrecedenceTests extends SecurityIntegTestCase { @Override protected String configRoles() { - return "admin:\n" + - " cluster: [ all ] \n" + - " indices:\n" + - " - names: '*'\n" + - " privileges: [ all ]" + - "\n" + - "user:\n" + - " indices:\n" + - " - names: 'test_*'\n" + - " privileges: [ all ]"; + return "admin:\n" + + " cluster: [ all ] \n" + + " indices:\n" + + " - names: '*'\n" + + " privileges: [ all ]" + + "\n" + + "user:\n" + + " indices:\n" + + " - names: 'test_*'\n" + + " privileges: [ all ]"; } @Override protected String configUsers() { - final String usersPasswdHashed = - new String(getFastStoredHashAlgoForTests().hash(SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)); - return "admin:" + usersPasswdHashed + "\n" + - "client:" + usersPasswdHashed + "\n" + - "user:" + usersPasswdHashed + "\n"; + final String usersPasswdHashed = new String( + getFastStoredHashAlgoForTests().hash(SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING) + ); + return "admin:" + usersPasswdHashed + "\n" + "client:" + usersPasswdHashed + "\n" + "user:" + usersPasswdHashed + "\n"; } @Override protected String configUsersRoles() { - return "admin:admin\n" + - "transport_client:client\n" + - "user:user\n"; + return "admin:admin\n" + "transport_client:client\n" + "user:user\n"; } @Override @@ -80,29 +77,42 @@ public void testDifferentCombinationsOfIndices() throws Exception { // first lets try with "admin"... all should work - AcknowledgedResponse putResponse = client - .filterWithHeader(Collections.singletonMap(UsernamePasswordToken.BASIC_AUTH_HEADER, - basicAuthHeaderValue(nodeClientUsername(), nodeClientPassword()))) - .admin().indices().preparePutTemplate("template1") - .setPatterns(Collections.singletonList("test_*")) - .get(); + AcknowledgedResponse putResponse = client.filterWithHeader( + Collections.singletonMap( + UsernamePasswordToken.BASIC_AUTH_HEADER, + basicAuthHeaderValue(nodeClientUsername(), nodeClientPassword()) + ) + ).admin().indices().preparePutTemplate("template1").setPatterns(Collections.singletonList("test_*")).get(); assertAcked(putResponse); - GetIndexTemplatesResponse getResponse = client.admin().indices().prepareGetTemplates("template1") - .get(); + GetIndexTemplatesResponse getResponse = client.admin().indices().prepareGetTemplates("template1").get(); List templates = getResponse.getIndexTemplates(); assertThat(templates, hasSize(1)); // now lets try with "user" - Map auth = Collections.singletonMap(UsernamePasswordToken.BASIC_AUTH_HEADER, basicAuthHeaderValue("user", - nodeClientPassword())); - assertThrowsAuthorizationException(client.filterWithHeader(auth).admin().indices().preparePutTemplate("template1") - .setPatterns(Collections.singletonList("test_*"))::get, PutIndexTemplateAction.NAME, "user"); - - Map headers = Collections.singletonMap(UsernamePasswordToken.BASIC_AUTH_HEADER, basicAuthHeaderValue("user", - SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)); - assertThrowsAuthorizationException(client.filterWithHeader(headers).admin().indices().prepareGetTemplates("template1")::get, - GetIndexTemplatesAction.NAME, "user"); + Map auth = Collections.singletonMap( + UsernamePasswordToken.BASIC_AUTH_HEADER, + basicAuthHeaderValue("user", nodeClientPassword()) + ); + assertThrowsAuthorizationException( + client.filterWithHeader(auth) + .admin() + .indices() + .preparePutTemplate("template1") + .setPatterns(Collections.singletonList("test_*"))::get, + PutIndexTemplateAction.NAME, + "user" + ); + + Map headers = Collections.singletonMap( + UsernamePasswordToken.BASIC_AUTH_HEADER, + basicAuthHeaderValue("user", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING) + ); + assertThrowsAuthorizationException( + client.filterWithHeader(headers).admin().indices().prepareGetTemplates("template1")::get, + GetIndexTemplatesAction.NAME, + "user" + ); } } diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/SecurityCachePermissionTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/SecurityCachePermissionTests.java index 9d2051c4dc766..49bb1034ee0bc 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/SecurityCachePermissionTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/SecurityCachePermissionTests.java @@ -26,23 +26,17 @@ public class SecurityCachePermissionTests extends SecurityIntegTestCase { @Override public String configUsers() { - return super.configUsers() - + READ_ONE_IDX_USER + ":" + SecuritySettingsSource.TEST_PASSWORD_HASHED + "\n"; + return super.configUsers() + READ_ONE_IDX_USER + ":" + SecuritySettingsSource.TEST_PASSWORD_HASHED + "\n"; } @Override public String configRoles() { - return super.configRoles() - + "\nread_one_idx:\n" - + " indices:\n" - + " 'data':\n" - + " - read\n"; + return super.configRoles() + "\nread_one_idx:\n" + " indices:\n" + " 'data':\n" + " - read\n"; } @Override public String configUsersRoles() { - return super.configUsersRoles() - + "read_one_idx:" + READ_ONE_IDX_USER + "\n"; + return super.configUsersRoles() + "read_one_idx:" + READ_ONE_IDX_USER + "\n"; } @Before @@ -53,20 +47,27 @@ public void loadData() { } public void testThatTermsFilterQueryDoesntLeakData() { - SearchResponse response = client().prepareSearch("data").setQuery(QueryBuilders.constantScoreQuery( - QueryBuilders.termsLookupQuery("token", new TermsLookup("tokens", "1", "tokens")))) - .execute().actionGet(); + SearchResponse response = client().prepareSearch("data") + .setQuery(QueryBuilders.constantScoreQuery(QueryBuilders.termsLookupQuery("token", new TermsLookup("tokens", "1", "tokens")))) + .execute() + .actionGet(); assertThat(response.isTimedOut(), is(false)); assertThat(response.getHits().getHits().length, is(1)); // Repeat with unauthorized user!!!! try { - response = client().filterWithHeader(singletonMap("Authorization", basicAuthHeaderValue(READ_ONE_IDX_USER, - SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING))) - .prepareSearch("data") - .setQuery(QueryBuilders.constantScoreQuery( - QueryBuilders.termsLookupQuery("token", new TermsLookup("tokens", "1", "tokens")))) - .execute().actionGet(); + response = client().filterWithHeader( + singletonMap( + "Authorization", + basicAuthHeaderValue(READ_ONE_IDX_USER, SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING) + ) + ) + .prepareSearch("data") + .setQuery( + QueryBuilders.constantScoreQuery(QueryBuilders.termsLookupQuery("token", new TermsLookup("tokens", "1", "tokens"))) + ) + .execute() + .actionGet(); fail("search phase exception should have been thrown! response was:\n" + response.toString()); } catch (ElasticsearchSecurityException e) { assertThat(e.toString(), containsString("ElasticsearchSecurityException: action")); diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/SecurityClearScrollTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/SecurityClearScrollTests.java index 20abc1c80ff3e..7fcc6288b4565 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/SecurityClearScrollTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/SecurityClearScrollTests.java @@ -13,9 +13,9 @@ import org.elasticsearch.action.search.MultiSearchRequestBuilder; import org.elasticsearch.action.search.MultiSearchResponse; import org.elasticsearch.action.search.SearchPhaseExecutionException; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.test.SecurityIntegTestCase; import org.elasticsearch.test.SecuritySettingsSourceField; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.security.SecurityField; import org.junit.After; import org.junit.Before; @@ -38,38 +38,36 @@ public class SecurityClearScrollTests extends SecurityIntegTestCase { @Override protected String configUsers() { - final String usersPasswdHashed = - new String(getFastStoredHashAlgoForTests().hash(SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)); - return super.configUsers() + - "allowed_user:" + usersPasswdHashed + "\n" + - "denied_user:" + usersPasswdHashed + "\n"; + final String usersPasswdHashed = new String( + getFastStoredHashAlgoForTests().hash(SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING) + ); + return super.configUsers() + "allowed_user:" + usersPasswdHashed + "\n" + "denied_user:" + usersPasswdHashed + "\n"; } @Override protected String configUsersRoles() { - return super.configUsersRoles() + - "allowed_role:allowed_user\n" + - "denied_role:denied_user\n"; + return super.configUsersRoles() + "allowed_role:allowed_user\n" + "denied_role:denied_user\n"; } @Override protected String configRoles() { - return super.configRoles() + - "\nallowed_role:\n" + - " cluster:\n" + - " - cluster:admin/indices/scroll/clear_all \n" + - "denied_role:\n" + - " indices:\n" + - " - names: '*'\n" + - " privileges: [ALL]\n"; + return super.configRoles() + + "\nallowed_role:\n" + + " cluster:\n" + + " - cluster:admin/indices/scroll/clear_all \n" + + "denied_role:\n" + + " indices:\n" + + " - names: '*'\n" + + " privileges: [ALL]\n"; } @Before public void indexRandomDocuments() { BulkRequestBuilder bulkRequestBuilder = client().prepareBulk().setRefreshPolicy(IMMEDIATE); for (int i = 0; i < randomIntBetween(10, 50); i++) { - bulkRequestBuilder.add(client().prepareIndex("index") - .setId(String.valueOf(i)).setSource("{ \"foo\" : \"bar\" }", XContentType.JSON)); + bulkRequestBuilder.add( + client().prepareIndex("index").setId(String.valueOf(i)).setSource("{ \"foo\" : \"bar\" }", XContentType.JSON) + ); } BulkResponse bulkItemResponses = bulkRequestBuilder.get(); assertThat(bulkItemResponses.hasFailures(), is(false)); @@ -85,34 +83,33 @@ public void indexRandomDocuments() { @After public void clearScrolls() { - //clear all scroll ids from the default admin user, just in case any of test fails + // clear all scroll ids from the default admin user, just in case any of test fails client().prepareClearScroll().addScrollId("_all").get(); } public void testThatClearingAllScrollIdsWorks() throws Exception { - String user = "allowed_user:"+SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING; + String user = "allowed_user:" + SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING; String basicAuth = basicAuthHeaderValue("allowed_user", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING); Map headers = new HashMap<>(); headers.put(SecurityField.USER_SETTING.getKey(), user); headers.put(BASIC_AUTH_HEADER, basicAuth); - ClearScrollResponse clearScrollResponse = client().filterWithHeader(headers) - .prepareClearScroll() - .addScrollId("_all").get(); + ClearScrollResponse clearScrollResponse = client().filterWithHeader(headers).prepareClearScroll().addScrollId("_all").get(); assertThat(clearScrollResponse.isSucceeded(), is(true)); assertThatScrollIdsDoNotExist(scrollIds); } public void testThatClearingAllScrollIdsRequirePermissions() throws Exception { - String user = "denied_user:"+SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING; + String user = "denied_user:" + SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING; String basicAuth = basicAuthHeaderValue("denied_user", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING); Map headers = new HashMap<>(); headers.put(SecurityField.USER_SETTING.getKey(), user); headers.put(BASIC_AUTH_HEADER, basicAuth); - assertRequestBuilderThrows(client().filterWithHeader(headers) - .prepareClearScroll() - .addScrollId("_all"), ElasticsearchSecurityException.class, - "action [cluster:admin/indices/scroll/clear_all] is unauthorized for user [denied_user]"); + assertRequestBuilderThrows( + client().filterWithHeader(headers).prepareClearScroll().addScrollId("_all"), + ElasticsearchSecurityException.class, + "action [cluster:admin/indices/scroll/clear_all] is unauthorized for user [denied_user]" + ); // deletion of scroll ids should work ClearScrollResponse clearByIdScrollResponse = client().prepareClearScroll().setScrollIds(scrollIds).get(); @@ -124,8 +121,10 @@ public void testThatClearingAllScrollIdsRequirePermissions() throws Exception { private void assertThatScrollIdsDoNotExist(List scrollIds) { for (String scrollId : scrollIds) { - SearchPhaseExecutionException expectedException = - expectThrows(SearchPhaseExecutionException.class, () -> client().prepareSearchScroll(scrollId).get()); + SearchPhaseExecutionException expectedException = expectThrows( + SearchPhaseExecutionException.class, + () -> client().prepareSearchScroll(scrollId).get() + ); assertThat(expectedException.toString(), containsString("SearchContextMissingException")); } } diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/SecurityFeatureStateIntegTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/SecurityFeatureStateIntegTests.java index 67dc91bce3c48..ab0611f617fee 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/SecurityFeatureStateIntegTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/SecurityFeatureStateIntegTests.java @@ -52,9 +52,7 @@ protected boolean addMockHttpTransport() { @Override protected Settings nodeSettings() { - return Settings.builder().put(super.nodeSettings()) - .put("path.repo", repositoryLocation) - .build(); + return Settings.builder().put(super.nodeSettings()).put("path.repo", repositoryLocation).build(); } /** @@ -69,7 +67,9 @@ protected Settings nodeSettings() { public void testSecurityFeatureStateSnapshotAndRestore() throws Exception { // set up a snapshot repository final String repositoryName = "test-repo"; - client().admin().cluster().preparePutRepository(repositoryName) + client().admin() + .cluster() + .preparePutRepository(repositoryName) .setType("fs") .setSettings(Settings.builder().put("location", repositoryLocation)) .get(); @@ -78,23 +78,24 @@ public void testSecurityFeatureStateSnapshotAndRestore() throws Exception { final String roleName = "extra_role"; final Request createRoleRequest = new Request("PUT", "/_security/role/" + roleName); createRoleRequest.addParameter("refresh", "wait_for"); - createRoleRequest.setJsonEntity("{" + - " \"indices\": [" + - " {" + - " \"names\": [ \"test_index\" ]," + - " \"privileges\" : [ \"create\", \"create_index\", \"create_doc\" ]" + - " }" + - " ]" + - "}"); + createRoleRequest.setJsonEntity( + "{" + + " \"indices\": [" + + " {" + + " \"names\": [ \"test_index\" ]," + + " \"privileges\" : [ \"create\", \"create_index\", \"create_doc\" ]" + + " }" + + " ]" + + "}" + ); performSuperuserRequest(createRoleRequest); // create a test user final Request createUserRequest = new Request("PUT", "/_security/user/" + LOCAL_TEST_USER_NAME); createUserRequest.addParameter("refresh", "wait_for"); - createUserRequest.setJsonEntity("{" + - " \"password\": \"" + LOCAL_TEST_USER_PASSWORD + "\"," + - " \"roles\": [ \"" + roleName + "\" ]" + - "}"); + createUserRequest.setJsonEntity( + "{" + " \"password\": \"" + LOCAL_TEST_USER_PASSWORD + "\"," + " \"roles\": [ \"" + roleName + "\" ]" + "}" + ); performSuperuserRequest(createUserRequest); // test user posts a document @@ -104,7 +105,9 @@ public void testSecurityFeatureStateSnapshotAndRestore() throws Exception { // snapshot state final String snapshotName = "security-state"; - client().admin().cluster().prepareCreateSnapshot(repositoryName, snapshotName) + client().admin() + .cluster() + .prepareCreateSnapshot(repositoryName, snapshotName) .setIndices("test_index") .setFeatureStates("LocalStateSecurity") .get(); @@ -122,13 +125,17 @@ public void testSecurityFeatureStateSnapshotAndRestore() throws Exception { ResponseException exception = expectThrows(ResponseException.class, () -> performTestUserRequest(postDocumentRequest2)); assertThat(exception.getResponse().getStatusLine().getStatusCode(), equalTo(403)); - assertThat(exception.getMessage(), - containsString("action [" + IndexAction.NAME + "] is unauthorized for user [" + LOCAL_TEST_USER_NAME + "]")); + assertThat( + exception.getMessage(), + containsString("action [" + IndexAction.NAME + "] is unauthorized for user [" + LOCAL_TEST_USER_NAME + "]") + ); client().admin().indices().prepareClose("test_index").get(); // restore state - client().admin().cluster().prepareRestoreSnapshot(repositoryName, snapshotName) + client().admin() + .cluster() + .prepareRestoreSnapshot(repositoryName, snapshotName) .setFeatureStates("LocalStateSecurity") .setIndices("test_index") .setWaitForCompletion(true) @@ -142,13 +149,17 @@ public void testSecurityFeatureStateSnapshotAndRestore() throws Exception { private Response performSuperuserRequest(Request request) throws Exception { String token = UsernamePasswordToken.basicAuthHeaderValue( - TEST_SUPERUSER, new SecureString(SecuritySettingsSourceField.TEST_PASSWORD.toCharArray())); + TEST_SUPERUSER, + new SecureString(SecuritySettingsSourceField.TEST_PASSWORD.toCharArray()) + ); return performAuthenticatedRequest(request, token); } private Response performTestUserRequest(Request request) throws Exception { String token = UsernamePasswordToken.basicAuthHeaderValue( - LOCAL_TEST_USER_NAME, new SecureString(LOCAL_TEST_USER_PASSWORD.toCharArray())); + LOCAL_TEST_USER_NAME, + new SecureString(LOCAL_TEST_USER_PASSWORD.toCharArray()) + ); return performAuthenticatedRequest(request, token); } @@ -165,8 +176,12 @@ private void waitForSnapshotToFinish(String repo, String snapshot) throws Except assertThat(response.getSnapshots().get(0).getState(), is(SnapshotsInProgress.State.SUCCESS)); // The status of the snapshot in the repository can become SUCCESS before it is fully finalized in the cluster state so wait for // it to disappear from the cluster state as well - SnapshotsInProgress snapshotsInProgress = - client().admin().cluster().state(new ClusterStateRequest()).get().getState().custom(SnapshotsInProgress.TYPE); + SnapshotsInProgress snapshotsInProgress = client().admin() + .cluster() + .state(new ClusterStateRequest()) + .get() + .getState() + .custom(SnapshotsInProgress.TYPE); assertTrue(snapshotsInProgress.isEmpty()); }); } diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/ShrinkIndexWithSecurityTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/ShrinkIndexWithSecurityTests.java index 9bc77792fb8fb..1bc3ed80d5201 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/ShrinkIndexWithSecurityTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/ShrinkIndexWithSecurityTests.java @@ -38,28 +38,37 @@ public void testShrinkIndex() throws Exception { client().prepareIndex("bigindex").setSource("foo", "bar").get(); } - ImmutableOpenMap dataNodes = client().admin().cluster().prepareState().get().getState().nodes() - .getDataNodes(); + ImmutableOpenMap dataNodes = client().admin() + .cluster() + .prepareState() + .get() + .getState() + .nodes() + .getDataNodes(); DiscoveryNode[] discoveryNodes = dataNodes.values().toArray(DiscoveryNode[]::new); final String mergeNode = discoveryNodes[0].getName(); ensureGreen(); // relocate all shards to one node such that we can merge it. - client().admin().indices().prepareUpdateSettings("bigindex") - .setSettings(Settings.builder() - .put("index.routing.allocation.require._name", mergeNode) - .put("index.blocks.write", true)).get(); + client().admin() + .indices() + .prepareUpdateSettings("bigindex") + .setSettings(Settings.builder().put("index.routing.allocation.require._name", mergeNode).put("index.blocks.write", true)) + .get(); // wait for green and then shrink ensureGreen(); - assertAcked(client().admin().indices().prepareResizeIndex("bigindex", "shrunk_bigindex") - .setSettings(Settings.builder() - .put("index.number_of_replicas", 0) - .put("index.number_of_shards", 1) - .build())); + assertAcked( + client().admin() + .indices() + .prepareResizeIndex("bigindex", "shrunk_bigindex") + .setSettings(Settings.builder().put("index.number_of_replicas", 0).put("index.number_of_shards", 1).build()) + ); // verify all docs ensureGreen(); - assertHitCount(client().prepareSearch("shrunk_bigindex").setSize(100).setQuery(new TermsQueryBuilder("foo", "bar")).get(), - randomNumberOfDocs); + assertHitCount( + client().prepareSearch("shrunk_bigindex").setSize(100).setQuery(new TermsQueryBuilder("foo", "bar")).get(), + randomNumberOfDocs + ); } } diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/license/LicensingTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/license/LicensingTests.java index 322ce4aa9baf6..1ab39d461e9ae 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/license/LicensingTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/license/LicensingTests.java @@ -34,8 +34,8 @@ import org.elasticsearch.test.SecurityIntegTestCase; import org.elasticsearch.test.SecuritySettingsSource; import org.elasticsearch.test.SecuritySettingsSourceField; -import org.elasticsearch.transport.netty4.Netty4Plugin; import org.elasticsearch.transport.TransportInfo; +import org.elasticsearch.transport.netty4.Netty4Plugin; import org.elasticsearch.xpack.core.XPackField; import org.elasticsearch.xpack.core.security.authc.support.Hasher; import org.elasticsearch.xpack.security.LocalStateSecurity; @@ -54,10 +54,10 @@ import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.discovery.SettingsBasedSeedHostsProvider.DISCOVERY_SEED_HOSTS_SETTING; import static org.elasticsearch.license.LicenseService.LICENSE_EXPIRATION_WARNING_PERIOD; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken.basicAuthHeaderValue; import static org.hamcrest.Matchers.arrayWithSize; import static org.hamcrest.Matchers.containsStringIgnoringCase; @@ -71,36 +71,35 @@ public class LicensingTests extends SecurityIntegTestCase { private static final SecureString HASH_PASSWD = new SecureString(Hasher.BCRYPT4.hash(new SecureString("passwd".toCharArray()))); - private static final String ROLES = - SecuritySettingsSource.TEST_ROLE + ":\n" + - " cluster: [ all ]\n" + - " indices:\n" + - " - names: '*'\n" + - " privileges: [manage]\n" + - " - names: '/.*/'\n" + - " privileges: [write]\n" + - " - names: 'test'\n" + - " privileges: [read]\n" + - " - names: 'test1'\n" + - " privileges: [read]\n" + - "\n" + - "role_a:\n" + - " indices:\n" + - " - names: 'a'\n" + - " privileges: [all]\n" + - " - names: 'test-dls'\n" + - " privileges: [read]\n" + - " query: '{\"term\":{\"field\":\"value\"} }'\n" + - "\n" + - "role_b:\n" + - " indices:\n" + - " - names: 'b'\n" + - " privileges: [all]\n"; - - private static final String USERS_ROLES = - SecuritySettingsSource.CONFIG_STANDARD_USER_ROLES + - "role_a:user_a,user_b\n" + - "role_b:user_b\n"; + private static final String ROLES = SecuritySettingsSource.TEST_ROLE + + ":\n" + + " cluster: [ all ]\n" + + " indices:\n" + + " - names: '*'\n" + + " privileges: [manage]\n" + + " - names: '/.*/'\n" + + " privileges: [write]\n" + + " - names: 'test'\n" + + " privileges: [read]\n" + + " - names: 'test1'\n" + + " privileges: [read]\n" + + "\n" + + "role_a:\n" + + " indices:\n" + + " - names: 'a'\n" + + " privileges: [all]\n" + + " - names: 'test-dls'\n" + + " privileges: [read]\n" + + " query: '{\"term\":{\"field\":\"value\"} }'\n" + + "\n" + + "role_b:\n" + + " indices:\n" + + " - names: 'b'\n" + + " privileges: [all]\n"; + + private static final String USERS_ROLES = SecuritySettingsSource.CONFIG_STANDARD_USER_ROLES + + "role_a:user_a,user_b\n" + + "role_b:user_b\n"; @Override protected String configRoles() { @@ -109,9 +108,7 @@ protected String configRoles() { @Override protected String configUsers() { - return SecuritySettingsSource.CONFIG_STANDARD_USER + - "user_a:" + HASH_PASSWD + "\n" + - "user_b:" + HASH_PASSWD + "\n"; + return SecuritySettingsSource.CONFIG_STANDARD_USER + "user_a:" + HASH_PASSWD + "\n" + "user_b:" + HASH_PASSWD + "\n"; } @Override @@ -145,17 +142,10 @@ public void cleanupSecurityIndex() { } public void testEnableDisableBehaviour() throws Exception { - IndexResponse indexResponse = index("test", jsonBuilder() - .startObject() - .field("name", "value") - .endObject()); + IndexResponse indexResponse = index("test", jsonBuilder().startObject().field("name", "value").endObject()); assertEquals(DocWriteResponse.Result.CREATED, indexResponse.getResult()); - - indexResponse = index("test1", jsonBuilder() - .startObject() - .field("name", "value1") - .endObject()); + indexResponse = index("test1", jsonBuilder().startObject().field("name", "value1").endObject()); assertEquals(DocWriteResponse.Result.CREATED, indexResponse.getResult()); refresh(); @@ -187,12 +177,23 @@ public void testEnableDisableBehaviour() throws Exception { } public void testNodeJoinWithoutSecurityExplicitlyEnabled() throws Exception { - License.OperationMode mode = randomFrom(License.OperationMode.GOLD, License.OperationMode.PLATINUM, - License.OperationMode.ENTERPRISE, License.OperationMode.STANDARD); + License.OperationMode mode = randomFrom( + License.OperationMode.GOLD, + License.OperationMode.PLATINUM, + License.OperationMode.ENTERPRISE, + License.OperationMode.STANDARD + ); enableLicensing(mode); - final List seedHosts = internalCluster().masterClient().admin().cluster().nodesInfo(new NodesInfoRequest()).get() - .getNodes().stream().map(n -> n.getInfo(TransportInfo.class).getAddress().publishAddress().toString()).distinct() + final List seedHosts = internalCluster().masterClient() + .admin() + .cluster() + .nodesInfo(new NodesInfoRequest()) + .get() + .getNodes() + .stream() + .map(n -> n.getInfo(TransportInfo.class).getAddress().publishAddress().toString()) + .distinct() .collect(Collectors.toList()); Path home = createTempDir(); @@ -217,11 +218,20 @@ public void testNodeJoinWithoutSecurityExplicitlyEnabled() throws Exception { public void testNoWarningHeaderWhenAuthenticationFailed() throws Exception { Request request = new Request("GET", "/_security/user"); RequestOptions.Builder options = request.getOptions().toBuilder(); - options.addHeader("Authorization", basicAuthHeaderValue(SecuritySettingsSource.TEST_USER_NAME, - new SecureString(SecuritySettingsSourceField.TEST_INVALID_PASSWORD.toCharArray()))); + options.addHeader( + "Authorization", + basicAuthHeaderValue( + SecuritySettingsSource.TEST_USER_NAME, + new SecureString(SecuritySettingsSourceField.TEST_INVALID_PASSWORD.toCharArray()) + ) + ); request.setOptions(options); - License.OperationMode mode = randomFrom(License.OperationMode.GOLD, License.OperationMode.PLATINUM, - License.OperationMode.ENTERPRISE, License.OperationMode.STANDARD); + License.OperationMode mode = randomFrom( + License.OperationMode.GOLD, + License.OperationMode.PLATINUM, + License.OperationMode.ENTERPRISE, + License.OperationMode.STANDARD + ); long now = System.currentTimeMillis(); long newExpirationDate = now + LICENSE_EXPIRATION_WARNING_PERIOD.getMillis() - 1; setLicensingExpirationDate(mode, "warning: license will expire soon"); diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/test/NativeRealmIntegTestCase.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/test/NativeRealmIntegTestCase.java index 13f8b28a03ae6..6a1f42cac5d31 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/test/NativeRealmIntegTestCase.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/test/NativeRealmIntegTestCase.java @@ -101,8 +101,14 @@ public void setupReservedPasswords(RestClient restClient) throws IOException { RequestOptions.Builder optionsBuilder = RequestOptions.DEFAULT.toBuilder(); optionsBuilder.addHeader("Authorization", UsernamePasswordToken.basicAuthHeaderValue(ElasticUser.NAME, reservedPassword)); RequestOptions options = optionsBuilder.build(); - final List usernames = Arrays.asList(KibanaUser.NAME, KibanaSystemUser.NAME, LogstashSystemUser.NAME, BeatsSystemUser.NAME, - APMSystemUser.NAME, RemoteMonitoringUser.NAME); + final List usernames = Arrays.asList( + KibanaUser.NAME, + KibanaSystemUser.NAME, + LogstashSystemUser.NAME, + BeatsSystemUser.NAME, + APMSystemUser.NAME, + RemoteMonitoringUser.NAME + ); for (String username : usernames) { Request request = new Request("PUT", "/_security/user/" + username + "/_password"); request.setJsonEntity("{\"password\": \"" + new String(reservedPassword.getChars()) + "\"}"); diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/test/SecuritySingleNodeTestCase.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/test/SecuritySingleNodeTestCase.java index ee42a07b41c1e..b80d3921ed3bc 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/test/SecuritySingleNodeTestCase.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/test/SecuritySingleNodeTestCase.java @@ -8,6 +8,7 @@ import io.netty.util.ThreadDeathWatcher; import io.netty.util.concurrent.GlobalEventExecutor; + import org.apache.http.HttpHost; import org.elasticsearch.action.admin.cluster.node.info.NodeInfo; import org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse; @@ -62,8 +63,7 @@ public abstract class SecuritySingleNodeTestCase extends ESSingleNodeTestCase { @BeforeClass public static void initDefaultSettings() { if (SECURITY_DEFAULT_SETTINGS == null) { - SECURITY_DEFAULT_SETTINGS = - new SecuritySettingsSource(randomBoolean(), createTempDir(), ESIntegTestCase.Scope.SUITE); + SECURITY_DEFAULT_SETTINGS = new SecuritySettingsSource(randomBoolean(), createTempDir(), ESIntegTestCase.Scope.SUITE); } } @@ -95,13 +95,16 @@ private static void tearDownRestClient() { } @Rule - //Rules are the only way to have something run before the before (final) method inherited from ESSingleNodeTestCase + // Rules are the only way to have something run before the before (final) method inherited from ESSingleNodeTestCase public ExternalResource externalResource = new ExternalResource() { @Override protected void before() { if (customSecuritySettingsSource == null) { - customSecuritySettingsSource = - new CustomSecuritySettingsSource(transportSSLEnabled(), createTempDir(), ESIntegTestCase.Scope.SUITE); + customSecuritySettingsSource = new CustomSecuritySettingsSource( + transportSSLEnabled(), + createTempDir(), + ESIntegTestCase.Scope.SUITE + ); } } }; @@ -136,7 +139,7 @@ protected void after() { }; @Before - //before methods from the superclass are run before this, which means that the current cluster is ready to go + // before methods from the superclass are run before this, which means that the current cluster is ready to go public void assertXPackIsInstalled() { doAssertXPackIsInstalled(); } @@ -146,11 +149,16 @@ private void doAssertXPackIsInstalled() { for (NodeInfo nodeInfo : nodeInfos.getNodes()) { // TODO: disable this assertion for now, due to random runs with mock plugins. perhaps run without mock plugins? // assertThat(nodeInfo.getInfo(PluginsAndModules.class).getInfos(), hasSize(2)); - Collection pluginNames = nodeInfo.getInfo(PluginsAndModules.class).getPluginInfos().stream() + Collection pluginNames = nodeInfo.getInfo(PluginsAndModules.class) + .getPluginInfos() + .stream() .map(PluginInfo::getClassname) .collect(Collectors.toList()); - assertThat("plugin [" + LocalStateSecurity.class.getName() + "] not found in [" + pluginNames + "]", pluginNames, - hasItem(LocalStateSecurity.class.getName())); + assertThat( + "plugin [" + LocalStateSecurity.class.getName() + "] not found in [" + pluginNames + "]", + pluginNames, + hasItem(LocalStateSecurity.class.getName()) + ); } } @@ -164,8 +172,10 @@ protected Settings nodeSettings() { builder.put("path.home", customSecuritySettingsSource.nodePath(0)); Settings.Builder customBuilder = Settings.builder().put(customSettings); if (customBuilder.getSecureSettings() != null) { - SecuritySettingsSource.addSecureSettings(builder, secureSettings -> - secureSettings.merge((MockSecureSettings) customBuilder.getSecureSettings())); + SecuritySettingsSource.addSecureSettings( + builder, + secureSettings -> secureSettings.merge((MockSecureSettings) customBuilder.getSecureSettings()) + ); } if (builder.getSecureSettings() == null) { builder.setSecureSettings(new MockSecureSettings()); @@ -280,8 +290,10 @@ protected SecureString nodeClientPassword() { @Override public Client wrapClient(final Client client) { - Map headers = Collections.singletonMap("Authorization", - basicAuthHeaderValue(nodeClientUsername(), nodeClientPassword())); + Map headers = Collections.singletonMap( + "Authorization", + basicAuthHeaderValue(nodeClientUsername(), nodeClientPassword()) + ); // we need to wrap node clients because we do not specify a user for nodes and all requests will use the system // user. This is ok for internal n2n stuff but the test framework does other things like wiping indices, repositories, etc // that the system user cannot do. so we wrap the node client with a user that can do these things since the client() calls @@ -308,7 +320,8 @@ protected RestClient createRestClient(RestClientBuilder.HttpClientConfigCallback } protected static Hasher getFastStoredHashAlgoForTests() { - return inFipsJvm() ? Hasher.resolve(randomFrom("pbkdf2", "pbkdf2_1000", "pbkdf2_stretch_1000", "pbkdf2_stretch")) + return inFipsJvm() + ? Hasher.resolve(randomFrom("pbkdf2", "pbkdf2_1000", "pbkdf2_stretch_1000", "pbkdf2_stretch")) : Hasher.resolve(randomFrom("pbkdf2", "pbkdf2_1000", "pbkdf2_stretch_1000", "pbkdf2_stretch", "bcrypt", "bcrypt9")); } @@ -319,8 +332,11 @@ private static synchronized RestClient getRestClient(Client client) { return restClient; } - private static RestClient createRestClient(Client client, RestClientBuilder.HttpClientConfigCallback httpClientConfigCallback, - String protocol) { + private static RestClient createRestClient( + Client client, + RestClientBuilder.HttpClientConfigCallback httpClientConfigCallback, + String protocol + ) { NodesInfoResponse nodesInfoResponse = client.admin().cluster().prepareNodesInfo().get(); assertFalse(nodesInfoResponse.hasFailures()); assertEquals(nodesInfoResponse.getNodes().size(), 1); diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/transport/SecurityServerTransportServiceTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/transport/SecurityServerTransportServiceTests.java index e97c35055a149..b359824505e5f 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/transport/SecurityServerTransportServiceTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/transport/SecurityServerTransportServiceTests.java @@ -18,11 +18,18 @@ public void testSecurityServerTransportServiceWrapsAllHandlers() { RequestHandlerRegistry handler = transportService.transport.getRequestHandlers() .getHandler(TransportService.HANDSHAKE_ACTION_NAME); assertEquals( - "handler not wrapped by " + SecurityServerTransportInterceptor.ProfileSecuredRequestHandler.class + - "; do all the handler registration methods have overrides?", - handler.toString(), - "ProfileSecuredRequestHandler{action='" + handler.getAction() + "', executorName='" + handler.getExecutor() - + "', forceExecution=" + handler.isForceExecution() + "}"); + "handler not wrapped by " + + SecurityServerTransportInterceptor.ProfileSecuredRequestHandler.class + + "; do all the handler registration methods have overrides?", + handler.toString(), + "ProfileSecuredRequestHandler{action='" + + handler.getAction() + + "', executorName='" + + handler.getExecutor() + + "', forceExecution=" + + handler.isForceExecution() + + "}" + ); } } } diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/ScrollHelperIntegTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/ScrollHelperIntegTests.java index 50959223fcb38..dc7d9cd7e1af0 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/ScrollHelperIntegTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/ScrollHelperIntegTests.java @@ -15,8 +15,8 @@ import org.elasticsearch.action.support.PlainActionFuture; import org.elasticsearch.client.Client; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; @@ -39,7 +39,6 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; - public class ScrollHelperIntegTests extends ESSingleNodeTestCase { public void testFetchAllEntities() throws ExecutionException, InterruptedException { @@ -50,15 +49,14 @@ public void testFetchAllEntities() throws ExecutionException, InterruptedExcepti } client.admin().indices().prepareRefresh("foo").get(); SearchRequest request = client.prepareSearch() - .setScroll(TimeValue.timeValueHours(10L)) - .setQuery(QueryBuilders.matchAllQuery()) - .setSize(randomIntBetween(1, 10)) - .setFetchSource(true) - .request(); + .setScroll(TimeValue.timeValueHours(10L)) + .setQuery(QueryBuilders.matchAllQuery()) + .setSize(randomIntBetween(1, 10)) + .setFetchSource(true) + .request(); request.indicesOptions().ignoreUnavailable(); PlainActionFuture> future = new PlainActionFuture<>(); - ScrollHelper.fetchAllByEntity(client(), request, future, - (hit) -> Integer.parseInt(hit.getSourceAsMap().get("number").toString())); + ScrollHelper.fetchAllByEntity(client(), request, future, (hit) -> Integer.parseInt(hit.getSourceAsMap().get("number").toString())); Collection integers = future.actionGet(); ArrayList list = new ArrayList<>(integers); CollectionUtil.timSort(list); @@ -84,16 +82,26 @@ public void testFetchAllByEntityWithBrokenScroll() { request.scroll(TimeValue.timeValueHours(10L)); String scrollId = randomAlphaOfLength(5); - SearchHit[] hits = new SearchHit[] {new SearchHit(1), new SearchHit(2)}; - InternalSearchResponse internalResponse = new InternalSearchResponse(new SearchHits(hits, - new TotalHits(3, TotalHits.Relation.EQUAL_TO), 1), + SearchHit[] hits = new SearchHit[] { new SearchHit(1), new SearchHit(2) }; + InternalSearchResponse internalResponse = new InternalSearchResponse( + new SearchHits(hits, new TotalHits(3, TotalHits.Relation.EQUAL_TO), 1), null, null, - null, false, + null, + false, false, - 1); - SearchResponse response = new SearchResponse(internalResponse, scrollId, 1, 1, 0, 0, ShardSearchFailure.EMPTY_ARRAY, - SearchResponse.Clusters.EMPTY); + 1 + ); + SearchResponse response = new SearchResponse( + internalResponse, + scrollId, + 1, + 1, + 0, + 0, + ShardSearchFailure.EMPTY_ARRAY, + SearchResponse.Clusters.EMPTY + ); Answer returnResponse = invocation -> { @SuppressWarnings("unchecked") @@ -121,7 +129,9 @@ public void onFailure(Exception e) { }, Function.identity()); assertNotNull("onFailure wasn't called", failure.get()); - assertEquals("scrolling returned more hits [4] than expected [3] so bailing out to prevent unbounded memory consumption.", - failure.get().getMessage()); + assertEquals( + "scrolling returned more hits [4] than expected [3] so bailing out to prevent unbounded memory consumption.", + failure.get().getMessage() + ); } } diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/SecurityPluginTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/SecurityPluginTests.java index 1916d500a8280..6df0d62b87d3c 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/SecurityPluginTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/SecurityPluginTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.security; import org.elasticsearch.client.Request; -import org.elasticsearch.client.Response; import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.Response; import org.elasticsearch.client.ResponseException; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.test.SecurityIntegTestCase; @@ -34,7 +34,7 @@ public void testThatPluginIsLoaded() throws IOException { logger.info("executing unauthorized request to /_xpack info"); getRestClient().performRequest(new Request("GET", "/_xpack")); fail("request should have failed"); - } catch(ResponseException e) { + } catch (ResponseException e) { assertThat(e.getResponse().getStatusLine().getStatusCode(), is(UNAUTHORIZED.getStatus())); } @@ -42,8 +42,13 @@ public void testThatPluginIsLoaded() throws IOException { Request request = new Request("GET", "/_xpack"); RequestOptions.Builder options = request.getOptions().toBuilder(); - options.addHeader("Authorization", basicAuthHeaderValue(SecuritySettingsSource.TEST_USER_NAME, - new SecureString(SecuritySettingsSourceField.TEST_PASSWORD.toCharArray()))); + options.addHeader( + "Authorization", + basicAuthHeaderValue( + SecuritySettingsSource.TEST_USER_NAME, + new SecureString(SecuritySettingsSourceField.TEST_PASSWORD.toCharArray()) + ) + ); request.setOptions(options); Response response = getRestClient().performRequest(request); assertThat(response.getStatusLine().getStatusCode(), is(OK.getStatus())); diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/TemplateUpgraderTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/TemplateUpgraderTests.java index 5192fb9ef026e..450d88ae1eac6 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/TemplateUpgraderTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/TemplateUpgraderTests.java @@ -43,21 +43,31 @@ public void testTemplatesWorkAsExpected() throws Exception { Client client = internalCluster().getInstance(Client.class, internalCluster().getMasterName()); UnaryOperator> indexTemplateMetadataUpgraders = map -> { map.remove("removed-template"); - map.put("added-template", IndexTemplateMetadata.builder("added-template") + map.put( + "added-template", + IndexTemplateMetadata.builder("added-template") .order(1) - .patterns(Collections.singletonList(randomAlphaOfLength(10))).build()); + .patterns(Collections.singletonList(randomAlphaOfLength(10))) + .build() + ); return map; }; - AcknowledgedResponse putIndexTemplateResponse = client().admin().indices().preparePutTemplate("removed-template") - .setOrder(1) - .setPatterns(Collections.singletonList(randomAlphaOfLength(10))) - .get(); + AcknowledgedResponse putIndexTemplateResponse = client().admin() + .indices() + .preparePutTemplate("removed-template") + .setOrder(1) + .setPatterns(Collections.singletonList(randomAlphaOfLength(10))) + .get(); assertAcked(putIndexTemplateResponse); assertTemplates("removed-template", "added-template"); - TemplateUpgradeService templateUpgradeService = new TemplateUpgradeService(client, clusterService, threadPool, - Collections.singleton(indexTemplateMetadataUpgraders)); + TemplateUpgradeService templateUpgradeService = new TemplateUpgradeService( + client, + clusterService, + threadPool, + Collections.singleton(indexTemplateMetadataUpgraders) + ); // ensure the cluster listener gets triggered ClusterChangedEvent event = new ClusterChangedEvent("testing", clusterService.state(), clusterService.state()); diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/action/filter/DestructiveOperationsTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/action/filter/DestructiveOperationsTests.java index 56f7ee6ea2f7e..69bf58a7bcd03 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/action/filter/DestructiveOperationsTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/action/filter/DestructiveOperationsTests.java @@ -17,7 +17,7 @@ public class DestructiveOperationsTests extends SecurityIntegTestCase { @After public void afterTest() { - Settings settings = Settings.builder().put(DestructiveOperations.REQUIRES_NAME_SETTING.getKey(), (String)null).build(); + Settings settings = Settings.builder().put(DestructiveOperations.REQUIRES_NAME_SETTING.getKey(), (String) null).build(); assertAcked(client().admin().cluster().prepareUpdateSettings().setPersistentSettings(settings)); } @@ -26,24 +26,30 @@ public void testDeleteIndexDestructiveOperationsRequireName() { Settings settings = Settings.builder().put(DestructiveOperations.REQUIRES_NAME_SETTING.getKey(), true).build(); assertAcked(client().admin().cluster().prepareUpdateSettings().setPersistentSettings(settings)); { - IllegalArgumentException illegalArgumentException = expectThrows(IllegalArgumentException.class, - () -> client().admin().indices().prepareDelete("*").get()); + IllegalArgumentException illegalArgumentException = expectThrows( + IllegalArgumentException.class, + () -> client().admin().indices().prepareDelete("*").get() + ); assertEquals("Wildcard expressions or all indices are not allowed", illegalArgumentException.getMessage()); String[] indices = client().admin().indices().prepareGetIndex().setIndices("index1").get().getIndices(); assertEquals(1, indices.length); assertEquals("index1", indices[0]); } { - IllegalArgumentException illegalArgumentException = expectThrows(IllegalArgumentException.class, - () -> client().admin().indices().prepareDelete("*", "-index1").get()); + IllegalArgumentException illegalArgumentException = expectThrows( + IllegalArgumentException.class, + () -> client().admin().indices().prepareDelete("*", "-index1").get() + ); assertEquals("Wildcard expressions or all indices are not allowed", illegalArgumentException.getMessage()); String[] indices = client().admin().indices().prepareGetIndex().setIndices("index1").get().getIndices(); assertEquals(1, indices.length); assertEquals("index1", indices[0]); } { - IllegalArgumentException illegalArgumentException = expectThrows(IllegalArgumentException.class, - () -> client().admin().indices().prepareDelete("_all").get()); + IllegalArgumentException illegalArgumentException = expectThrows( + IllegalArgumentException.class, + () -> client().admin().indices().prepareDelete("_all").get() + ); assertEquals("Wildcard expressions or all indices are not allowed", illegalArgumentException.getMessage()); String[] indices = client().admin().indices().prepareGetIndex().setIndices("index1").get().getIndices(); assertEquals(1, indices.length); @@ -62,7 +68,7 @@ public void testDestructiveOperationsDefaultBehaviour() { } createIndex("index1", "index2"); - switch(randomIntBetween(0, 2)) { + switch (randomIntBetween(0, 2)) { case 0: assertAcked(client().admin().indices().prepareClose("*")); assertAcked(client().admin().indices().prepareOpen("*")); @@ -87,33 +93,45 @@ public void testOpenCloseIndexDestructiveOperationsRequireName() { Settings settings = Settings.builder().put(DestructiveOperations.REQUIRES_NAME_SETTING.getKey(), true).build(); assertAcked(client().admin().cluster().prepareUpdateSettings().setPersistentSettings(settings)); { - IllegalArgumentException illegalArgumentException = expectThrows(IllegalArgumentException.class, - () -> client().admin().indices().prepareClose("*").get()); + IllegalArgumentException illegalArgumentException = expectThrows( + IllegalArgumentException.class, + () -> client().admin().indices().prepareClose("*").get() + ); assertEquals("Wildcard expressions or all indices are not allowed", illegalArgumentException.getMessage()); } { - IllegalArgumentException illegalArgumentException = expectThrows(IllegalArgumentException.class, - () -> client().admin().indices().prepareClose("*", "-index1").get()); + IllegalArgumentException illegalArgumentException = expectThrows( + IllegalArgumentException.class, + () -> client().admin().indices().prepareClose("*", "-index1").get() + ); assertEquals("Wildcard expressions or all indices are not allowed", illegalArgumentException.getMessage()); } { - IllegalArgumentException illegalArgumentException = expectThrows(IllegalArgumentException.class, - () -> client().admin().indices().prepareClose("_all").get()); + IllegalArgumentException illegalArgumentException = expectThrows( + IllegalArgumentException.class, + () -> client().admin().indices().prepareClose("_all").get() + ); assertEquals("Wildcard expressions or all indices are not allowed", illegalArgumentException.getMessage()); } { - IllegalArgumentException illegalArgumentException = expectThrows(IllegalArgumentException.class, - () -> client().admin().indices().prepareOpen("*").get()); + IllegalArgumentException illegalArgumentException = expectThrows( + IllegalArgumentException.class, + () -> client().admin().indices().prepareOpen("*").get() + ); assertEquals("Wildcard expressions or all indices are not allowed", illegalArgumentException.getMessage()); } { - IllegalArgumentException illegalArgumentException = expectThrows(IllegalArgumentException.class, - () -> client().admin().indices().prepareOpen("*", "-index1").get()); + IllegalArgumentException illegalArgumentException = expectThrows( + IllegalArgumentException.class, + () -> client().admin().indices().prepareOpen("*", "-index1").get() + ); assertEquals("Wildcard expressions or all indices are not allowed", illegalArgumentException.getMessage()); } { - IllegalArgumentException illegalArgumentException = expectThrows(IllegalArgumentException.class, - () -> client().admin().indices().prepareOpen("_all").get()); + IllegalArgumentException illegalArgumentException = expectThrows( + IllegalArgumentException.class, + () -> client().admin().indices().prepareOpen("_all").get() + ); assertEquals("Wildcard expressions or all indices are not allowed", illegalArgumentException.getMessage()); } diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/audit/logfile/AuditTrailSettingsUpdateTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/audit/logfile/AuditTrailSettingsUpdateTests.java index 2c49416b4d373..fbb0784f46dca 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/audit/logfile/AuditTrailSettingsUpdateTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/audit/logfile/AuditTrailSettingsUpdateTests.java @@ -12,21 +12,22 @@ import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.test.ESIntegTestCase.ClusterScope; import org.elasticsearch.test.SecurityIntegTestCase; import org.elasticsearch.xpack.security.audit.AuditLevel; import org.elasticsearch.xpack.security.audit.AuditTrailService; -import org.elasticsearch.test.ESIntegTestCase.ClusterScope; import org.junit.BeforeClass; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; + import static org.elasticsearch.test.ESIntegTestCase.Scope.TEST; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.Matchers.startsWith; import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.startsWith; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -82,25 +83,28 @@ public void testDynamicFilterSettings() throws Exception { // update settings on internal cluster updateSettings(updateFilterSettings); final String actual = ((LoggingAuditTrail) internalCluster().getInstances(AuditTrailService.class) - .iterator() - .next() - .getAuditTrails() - .iterator() - .next()).eventFilterPolicyRegistry.toString(); + .iterator() + .next() + .getAuditTrails() + .iterator() + .next()).eventFilterPolicyRegistry.toString(); assertEquals(expected, actual); } public void testInvalidFilterSettings() throws Exception { final String invalidLuceneRegex = "/invalid"; final Settings.Builder settingsBuilder = Settings.builder(); - final String[] allSettingsKeys = new String[] { "xpack.security.audit.logfile.events.ignore_filters.invalid.users", - "xpack.security.audit.logfile.events.ignore_filters.invalid.realms", - "xpack.security.audit.logfile.events.ignore_filters.invalid.roles", - "xpack.security.audit.logfile.events.ignore_filters.invalid.indices", - "xpack.security.audit.logfile.events.ignore_filters.invalid.actions"}; + final String[] allSettingsKeys = new String[] { + "xpack.security.audit.logfile.events.ignore_filters.invalid.users", + "xpack.security.audit.logfile.events.ignore_filters.invalid.realms", + "xpack.security.audit.logfile.events.ignore_filters.invalid.roles", + "xpack.security.audit.logfile.events.ignore_filters.invalid.indices", + "xpack.security.audit.logfile.events.ignore_filters.invalid.actions" }; settingsBuilder.put(randomFrom(allSettingsKeys), invalidLuceneRegex); - final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> client().admin().cluster().prepareUpdateSettings().setPersistentSettings(settingsBuilder.build()).get()); + final IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> client().admin().cluster().prepareUpdateSettings().setPersistentSettings(settingsBuilder.build()).get() + ); assertThat(e.getMessage(), containsString("invalid pattern [/invalid]")); } @@ -113,11 +117,11 @@ public void testDynamicHostSettings() { settingsBuilder.put(LoggingAuditTrail.EMIT_NODE_ID_SETTING.getKey(), true); updateSettings(settingsBuilder.build()); final LoggingAuditTrail loggingAuditTrail = (LoggingAuditTrail) internalCluster().getInstances(AuditTrailService.class) - .iterator() - .next() - .getAuditTrails() - .iterator() - .next(); + .iterator() + .next() + .getAuditTrails() + .iterator() + .next(); assertThat(loggingAuditTrail.entryCommonFields.commonFields.get(LoggingAuditTrail.NODE_NAME_FIELD_NAME), startsWith("node_")); assertThat(loggingAuditTrail.entryCommonFields.commonFields.containsKey(LoggingAuditTrail.NODE_ID_FIELD_NAME), is(true)); assertThat(loggingAuditTrail.entryCommonFields.commonFields.get(LoggingAuditTrail.HOST_ADDRESS_FIELD_NAME), is("127.0.0.1")); @@ -154,11 +158,11 @@ public void testDynamicRequestBodySettings() { settingsBuilder.put(LoggingAuditTrail.INCLUDE_REQUEST_BODY.getKey(), enableRequestBody); updateSettings(settingsBuilder.build()); final LoggingAuditTrail loggingAuditTrail = (LoggingAuditTrail) internalCluster().getInstances(AuditTrailService.class) - .iterator() - .next() - .getAuditTrails() - .iterator() - .next(); + .iterator() + .next() + .getAuditTrails() + .iterator() + .next(); assertEquals(enableRequestBody, loggingAuditTrail.includeRequestBody); settingsBuilder.put(LoggingAuditTrail.INCLUDE_REQUEST_BODY.getKey(), enableRequestBody == false); updateSettings(settingsBuilder.build()); @@ -166,9 +170,20 @@ public void testDynamicRequestBodySettings() { } public void testDynamicEventsSettings() { - final List allEventTypes = Arrays.asList("anonymous_access_denied", "authentication_failed", "realm_authentication_failed", - "access_granted", "access_denied", "tampered_request", "connection_granted", "connection_denied", "system_access_granted", - "authentication_success", "run_as_granted", "run_as_denied"); + final List allEventTypes = Arrays.asList( + "anonymous_access_denied", + "authentication_failed", + "realm_authentication_failed", + "access_granted", + "access_denied", + "tampered_request", + "connection_granted", + "connection_denied", + "system_access_granted", + "authentication_success", + "run_as_granted", + "run_as_denied" + ); final List includedEvents = randomSubsetOf(allEventTypes); final List excludedEvents = randomSubsetOf(allEventTypes); final Settings.Builder settingsBuilder = Settings.builder(); @@ -176,11 +191,11 @@ public void testDynamicEventsSettings() { settingsBuilder.putList(LoggingAuditTrail.EXCLUDE_EVENT_SETTINGS.getKey(), excludedEvents); updateSettings(settingsBuilder.build()); final LoggingAuditTrail loggingAuditTrail = (LoggingAuditTrail) internalCluster().getInstances(AuditTrailService.class) - .iterator() - .next() - .getAuditTrails() - .iterator() - .next(); + .iterator() + .next() + .getAuditTrails() + .iterator() + .next(); assertEquals(AuditLevel.parse(includedEvents, excludedEvents), loggingAuditTrail.events); } @@ -222,8 +237,7 @@ private static Settings randomFilterPolicySettings(String policyName) { if (randomBoolean()) { // filter by actions final List filteredActions = randomNonEmptyListOfFilteredNames(); - settingsBuilder.putList("xpack.security.audit.logfile.events.ignore_filters." + policyName + ".actions", - filteredActions); + settingsBuilder.putList("xpack.security.audit.logfile.events.ignore_filters." + policyName + ".actions", filteredActions); } } while (settingsBuilder.build().isEmpty()); diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/ApiKeyIntegTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/ApiKeyIntegTests.java index 7715f3645a0a5..9bc349edcd73b 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/ApiKeyIntegTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/ApiKeyIntegTests.java @@ -28,12 +28,12 @@ import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.security.AuthenticateResponse; import org.elasticsearch.common.Strings; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.EsExecutors; import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException; import org.elasticsearch.common.util.set.Sets; +import org.elasticsearch.core.TimeValue; +import org.elasticsearch.core.Tuple; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.test.SecurityIntegTestCase; import org.elasticsearch.test.SecuritySettingsSource; @@ -133,33 +133,39 @@ public void wipeSecurityIndex() throws Exception { @Override public String configRoles() { - return super.configRoles() + "\n" + - "no_api_key_role:\n" + - " cluster: [\"manage_token\"]\n" + - "manage_api_key_role:\n" + - " cluster: [\"manage_api_key\"]\n" + - "manage_own_api_key_role:\n" + - " cluster: [\"manage_own_api_key\"]\n" + - "run_as_role:\n" + - " run_as: [\"user_with_manage_own_api_key_role\"]\n"; + return super.configRoles() + + "\n" + + "no_api_key_role:\n" + + " cluster: [\"manage_token\"]\n" + + "manage_api_key_role:\n" + + " cluster: [\"manage_api_key\"]\n" + + "manage_own_api_key_role:\n" + + " cluster: [\"manage_own_api_key\"]\n" + + "run_as_role:\n" + + " run_as: [\"user_with_manage_own_api_key_role\"]\n"; } @Override public String configUsers() { - final String usersPasswdHashed = new String( - getFastStoredHashAlgoForTests().hash(TEST_PASSWORD_SECURE_STRING)); - return super.configUsers() + - "user_with_no_api_key_role:" + usersPasswdHashed + "\n" + - "user_with_manage_api_key_role:" + usersPasswdHashed + "\n" + - "user_with_manage_own_api_key_role:" + usersPasswdHashed + "\n"; + final String usersPasswdHashed = new String(getFastStoredHashAlgoForTests().hash(TEST_PASSWORD_SECURE_STRING)); + return super.configUsers() + + "user_with_no_api_key_role:" + + usersPasswdHashed + + "\n" + + "user_with_manage_api_key_role:" + + usersPasswdHashed + + "\n" + + "user_with_manage_own_api_key_role:" + + usersPasswdHashed + + "\n"; } @Override public String configUsersRoles() { - return super.configUsersRoles() + - "no_api_key_role:user_with_no_api_key_role\n" + - "manage_api_key_role:user_with_manage_api_key_role\n" + - "manage_own_api_key_role:user_with_manage_own_api_key_role\n"; + return super.configUsersRoles() + + "no_api_key_role:user_with_no_api_key_role\n" + + "manage_api_key_role:user_with_manage_api_key_role\n" + + "manage_own_api_key_role:user_with_manage_own_api_key_role\n"; } private void awaitApiKeysRemoverCompletion() throws Exception { @@ -173,9 +179,9 @@ public void testCreateApiKey() throws Exception { final Instant start = Instant.ofEpochMilli(Instant.now().toEpochMilli()); final RoleDescriptor descriptor = new RoleDescriptor("role", new String[] { "monitor" }, null, null); Client client = client().filterWithHeader( - Collections.singletonMap("Authorization", basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING))); - final CreateApiKeyResponse response = new CreateApiKeyRequestBuilder(client) - .setName("test key") + Collections.singletonMap("Authorization", basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING)) + ); + final CreateApiKeyResponse response = new CreateApiKeyRequestBuilder(client).setName("test key") .setExpiration(TimeValue.timeValueHours(TimeUnit.DAYS.toHours(7L))) .setRoleDescriptors(Collections.singletonList(descriptor)) .setMetadata(ApiKeyTests.randomMetadata()) @@ -198,22 +204,25 @@ public void testCreateApiKey() throws Exception { assertNull(simple.getExpiration()); // use the first ApiKey for authorized action - final String base64ApiKeyKeyValue = Base64.getEncoder().encodeToString( - (response.getId() + ":" + response.getKey().toString()).getBytes(StandardCharsets.UTF_8)); + final String base64ApiKeyKeyValue = Base64.getEncoder() + .encodeToString((response.getId() + ":" + response.getKey().toString()).getBytes(StandardCharsets.UTF_8)); // Assert that we can authenticate with the API KEY final RestHighLevelClient restClient = new TestRestHighLevelClient(); - AuthenticateResponse authResponse = restClient.security().authenticate(RequestOptions.DEFAULT.toBuilder().addHeader("Authorization", - "ApiKey " + base64ApiKeyKeyValue).build()); + AuthenticateResponse authResponse = restClient.security() + .authenticate(RequestOptions.DEFAULT.toBuilder().addHeader("Authorization", "ApiKey " + base64ApiKeyKeyValue).build()); assertThat(authResponse.getUser().getUsername(), equalTo(TEST_SUPERUSER)); assertThat(authResponse.getAuthenticationType(), equalTo("api_key")); // use the first ApiKey for an unauthorized action - ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, () -> - client().filterWithHeader(Collections.singletonMap("Authorization", "ApiKey " + base64ApiKeyKeyValue)) + ElasticsearchSecurityException e = expectThrows( + ElasticsearchSecurityException.class, + () -> client().filterWithHeader(Collections.singletonMap("Authorization", "ApiKey " + base64ApiKeyKeyValue)) .admin() .cluster() - .prepareUpdateSettings().setPersistentSettings(Settings.builder().put(IPFilter.IP_FILTER_ENABLED_SETTING.getKey(), true)) - .get()); + .prepareUpdateSettings() + .setPersistentSettings(Settings.builder().put(IPFilter.IP_FILTER_ENABLED_SETTING.getKey(), true)) + .get() + ); assertThat(e.getMessage(), containsString("unauthorized")); assertThat(e.status(), is(RestStatus.FORBIDDEN)); } @@ -225,10 +234,13 @@ public void testMultipleApiKeysCanHaveSameName() { for (int i = 0; i < noOfApiKeys; i++) { final RoleDescriptor descriptor = new RoleDescriptor("role", new String[] { "monitor" }, null, null); Client client = client().filterWithHeader( - Collections.singletonMap("Authorization", basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING))); - final CreateApiKeyResponse response = new CreateApiKeyRequestBuilder(client).setName(keyName).setExpiration(null) + Collections.singletonMap("Authorization", basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING)) + ); + final CreateApiKeyResponse response = new CreateApiKeyRequestBuilder(client).setName(keyName) + .setExpiration(null) .setRoleDescriptors(Collections.singletonList(descriptor)) - .setMetadata(ApiKeyTests.randomMetadata()).get(); + .setMetadata(ApiKeyTests.randomMetadata()) + .get(); assertNotNull(response.getId()); assertNotNull(response.getKey()); responses.add(response); @@ -241,9 +253,12 @@ public void testMultipleApiKeysCanHaveSameName() { public void testCreateApiKeyWithoutNameWillFail() { Client client = client().filterWithHeader( - Collections.singletonMap("Authorization", basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING))); - final ActionRequestValidationException e = - expectThrows(ActionRequestValidationException.class, () -> new CreateApiKeyRequestBuilder(client).get()); + Collections.singletonMap("Authorization", basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING)) + ); + final ActionRequestValidationException e = expectThrows( + ActionRequestValidationException.class, + () -> new CreateApiKeyRequestBuilder(client).get() + ); assertThat(e.getMessage(), containsString("api key name is required")); } @@ -251,7 +266,8 @@ public void testInvalidateApiKeysForRealm() throws InterruptedException, Executi int noOfApiKeys = randomIntBetween(3, 5); List responses = createApiKeys(noOfApiKeys, null).v1(); Client client = client().filterWithHeader( - Collections.singletonMap("Authorization", basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING))); + Collections.singletonMap("Authorization", basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING)) + ); PlainActionFuture listener = new PlainActionFuture<>(); client.execute(InvalidateApiKeyAction.INSTANCE, InvalidateApiKeyRequest.usingRealmName("file"), listener); InvalidateApiKeyResponse invalidateResponse = listener.get(); @@ -262,10 +278,10 @@ public void testInvalidateApiKeysForUser() throws Exception { int noOfApiKeys = randomIntBetween(3, 5); List responses = createApiKeys(noOfApiKeys, null).v1(); Client client = client().filterWithHeader( - Collections.singletonMap("Authorization", basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING))); + Collections.singletonMap("Authorization", basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING)) + ); PlainActionFuture listener = new PlainActionFuture<>(); - client.execute(InvalidateApiKeyAction.INSTANCE, - InvalidateApiKeyRequest.usingUserName(TEST_SUPERUSER), listener); + client.execute(InvalidateApiKeyAction.INSTANCE, InvalidateApiKeyRequest.usingUserName(TEST_SUPERUSER), listener); InvalidateApiKeyResponse invalidateResponse = listener.get(); verifyInvalidateResponse(noOfApiKeys, responses, invalidateResponse); } @@ -273,10 +289,10 @@ public void testInvalidateApiKeysForUser() throws Exception { public void testInvalidateApiKeysForRealmAndUser() throws InterruptedException, ExecutionException { List responses = createApiKeys(1, null).v1(); Client client = client().filterWithHeader( - Collections.singletonMap("Authorization", basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING))); + Collections.singletonMap("Authorization", basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING)) + ); PlainActionFuture listener = new PlainActionFuture<>(); - client.execute(InvalidateApiKeyAction.INSTANCE, - InvalidateApiKeyRequest.usingRealmAndUserName("file", TEST_SUPERUSER), listener); + client.execute(InvalidateApiKeyAction.INSTANCE, InvalidateApiKeyRequest.usingRealmAndUserName("file", TEST_SUPERUSER), listener); InvalidateApiKeyResponse invalidateResponse = listener.get(); verifyInvalidateResponse(1, responses, invalidateResponse); } @@ -284,7 +300,8 @@ public void testInvalidateApiKeysForRealmAndUser() throws InterruptedException, public void testInvalidateApiKeysForApiKeyId() throws InterruptedException, ExecutionException { List responses = createApiKeys(1, null).v1(); Client client = client().filterWithHeader( - Collections.singletonMap("Authorization", basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING))); + Collections.singletonMap("Authorization", basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING)) + ); PlainActionFuture listener = new PlainActionFuture<>(); client.execute(InvalidateApiKeyAction.INSTANCE, InvalidateApiKeyRequest.usingApiKeyId(responses.get(0).getId(), false), listener); InvalidateApiKeyResponse invalidateResponse = listener.get(); @@ -294,10 +311,14 @@ public void testInvalidateApiKeysForApiKeyId() throws InterruptedException, Exec public void testInvalidateApiKeysForApiKeyName() throws InterruptedException, ExecutionException { List responses = createApiKeys(1, null).v1(); Client client = client().filterWithHeader( - Collections.singletonMap("Authorization", basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING))); + Collections.singletonMap("Authorization", basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING)) + ); PlainActionFuture listener = new PlainActionFuture<>(); - client.execute(InvalidateApiKeyAction.INSTANCE, InvalidateApiKeyRequest.usingApiKeyName(responses.get(0).getName(), false), - listener); + client.execute( + InvalidateApiKeyAction.INSTANCE, + InvalidateApiKeyRequest.usingApiKeyName(responses.get(0).getName(), false), + listener + ); InvalidateApiKeyResponse invalidateResponse = listener.get(); verifyInvalidateResponse(1, responses, invalidateResponse); } @@ -312,10 +333,14 @@ public void testInvalidateApiKeyWillClearApiKeyCache() throws IOException, Execu Tuple apiKey2 = createApiKeyAndAuthenticateWithIt(); // Find out which nodes handled the above authentication requests - final ApiKeyService serviceForDoc1 = - services.stream().filter(s -> s.getDocCache().get(apiKey1.v1()) != null).findFirst().orElseThrow(); - final ApiKeyService serviceForDoc2 = - services.stream().filter(s -> s.getDocCache().get(apiKey2.v1()) != null).findFirst().orElseThrow(); + final ApiKeyService serviceForDoc1 = services.stream() + .filter(s -> s.getDocCache().get(apiKey1.v1()) != null) + .findFirst() + .orElseThrow(); + final ApiKeyService serviceForDoc2 = services.stream() + .filter(s -> s.getDocCache().get(apiKey2.v1()) != null) + .findFirst() + .orElseThrow(); assertNotNull(serviceForDoc1.getFromCache(apiKey1.v1())); assertNotNull(serviceForDoc2.getFromCache(apiKey2.v1())); final boolean sameServiceNode = serviceForDoc1 == serviceForDoc2; @@ -328,7 +353,8 @@ public void testInvalidateApiKeyWillClearApiKeyCache() throws IOException, Execu // Invalidate the first key Client client = client().filterWithHeader( - Collections.singletonMap("Authorization", basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING))); + Collections.singletonMap("Authorization", basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING)) + ); PlainActionFuture listener = new PlainActionFuture<>(); client.execute(InvalidateApiKeyAction.INSTANCE, InvalidateApiKeyRequest.usingApiKeyId(apiKey1.v1(), false), listener); InvalidateApiKeyResponse invalidateResponse = listener.get(); @@ -344,34 +370,44 @@ public void testInvalidateApiKeyWillClearApiKeyCache() throws IOException, Execu } // Authentication with the first key should fail - final String base64ApiKeyKeyValue = Base64.getEncoder().encodeToString( - (apiKey1.v1() + ":" + apiKey1.v2()).getBytes(StandardCharsets.UTF_8)); - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, + final String base64ApiKeyKeyValue = Base64.getEncoder() + .encodeToString((apiKey1.v1() + ":" + apiKey1.v2()).getBytes(StandardCharsets.UTF_8)); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, () -> new TestRestHighLevelClient().security() - .authenticate(RequestOptions.DEFAULT.toBuilder().addHeader("Authorization", - "ApiKey " + base64ApiKeyKeyValue).build())); + .authenticate(RequestOptions.DEFAULT.toBuilder().addHeader("Authorization", "ApiKey " + base64ApiKeyKeyValue).build()) + ); assertThat(e.getMessage(), containsString("security_exception")); assertThat(e.status(), is(RestStatus.UNAUTHORIZED)); } - private void verifyInvalidateResponse(int noOfApiKeys, List responses, - InvalidateApiKeyResponse invalidateResponse) { + private void verifyInvalidateResponse( + int noOfApiKeys, + List responses, + InvalidateApiKeyResponse invalidateResponse + ) { assertThat(invalidateResponse.getInvalidatedApiKeys().size(), equalTo(noOfApiKeys)); - assertThat(invalidateResponse.getInvalidatedApiKeys(), - containsInAnyOrder(responses.stream().map(r -> r.getId()).collect(Collectors.toList()).toArray(Strings.EMPTY_ARRAY))); + assertThat( + invalidateResponse.getInvalidatedApiKeys(), + containsInAnyOrder(responses.stream().map(r -> r.getId()).collect(Collectors.toList()).toArray(Strings.EMPTY_ARRAY)) + ); assertThat(invalidateResponse.getPreviouslyInvalidatedApiKeys().size(), equalTo(0)); assertThat(invalidateResponse.getErrors().size(), equalTo(0)); } public void testInvalidatedApiKeysDeletedByRemover() throws Exception { Client client = waitForExpiredApiKeysRemoverTriggerReadyAndGetClient().filterWithHeader( - Collections.singletonMap("Authorization", basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING))); + Collections.singletonMap("Authorization", basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING)) + ); List createdApiKeys = createApiKeys(2, null).v1(); PlainActionFuture listener = new PlainActionFuture<>(); - client.execute(InvalidateApiKeyAction.INSTANCE, InvalidateApiKeyRequest.usingApiKeyId(createdApiKeys.get(0).getId(), false), - listener); + client.execute( + InvalidateApiKeyAction.INSTANCE, + InvalidateApiKeyRequest.usingApiKeyId(createdApiKeys.get(0).getId(), false), + listener + ); InvalidateApiKeyResponse invalidateResponse = listener.get(); assertThat(invalidateResponse.getInvalidatedApiKeys().size(), equalTo(1)); assertThat(invalidateResponse.getPreviouslyInvalidatedApiKeys().size(), equalTo(0)); @@ -395,16 +431,22 @@ public void testInvalidatedApiKeysDeletedByRemover() throws Exception { assertThat(apiKey.isInvalidated(), is(false)); } } - assertThat(getApiKeyResponseListener.get().getApiKeyInfos().length, - is((apiKeyInvalidatedButNotYetDeletedByExpiredApiKeysRemover) ? 2 : 1)); + assertThat( + getApiKeyResponseListener.get().getApiKeyInfos().length, + is((apiKeyInvalidatedButNotYetDeletedByExpiredApiKeysRemover) ? 2 : 1) + ); client = waitForExpiredApiKeysRemoverTriggerReadyAndGetClient().filterWithHeader( - Collections.singletonMap("Authorization", basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING))); + Collections.singletonMap("Authorization", basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING)) + ); // invalidate API key to trigger remover listener = new PlainActionFuture<>(); - client.execute(InvalidateApiKeyAction.INSTANCE, InvalidateApiKeyRequest.usingApiKeyId(createdApiKeys.get(1).getId(), false), - listener); + client.execute( + InvalidateApiKeyAction.INSTANCE, + InvalidateApiKeyRequest.usingApiKeyId(createdApiKeys.get(1).getId(), false), + listener + ); assertThat(listener.get().getInvalidatedApiKeys().size(), is(1)); awaitApiKeysRemoverCompletion(); @@ -424,8 +466,10 @@ public void testInvalidatedApiKeysDeletedByRemover() throws Exception { apiKeyInvalidatedButNotYetDeletedByExpiredApiKeysRemover = true; } } - assertThat(getApiKeyResponseListener.get().getApiKeyInfos().length, - is((apiKeyInvalidatedButNotYetDeletedByExpiredApiKeysRemover) ? 1 : 0)); + assertThat( + getApiKeyResponseListener.get().getApiKeyInfos().length, + is((apiKeyInvalidatedButNotYetDeletedByExpiredApiKeysRemover) ? 1 : 0) + ); } private Client waitForExpiredApiKeysRemoverTriggerReadyAndGetClient() throws Exception { @@ -442,15 +486,14 @@ private Client waitForExpiredApiKeysRemoverTriggerReadyAndGetClient() throws Exc } final ThreadPool threadPool = internalCluster().getInstance(ThreadPool.class, nodeWithMostRecentRun); final long lastRunTime = apiKeyLastTrigger; - assertBusy(() -> { - assertThat(threadPool.relativeTimeInMillis() - lastRunTime, greaterThan(DELETE_INTERVAL_MILLIS)); - }); + assertBusy(() -> { assertThat(threadPool.relativeTimeInMillis() - lastRunTime, greaterThan(DELETE_INTERVAL_MILLIS)); }); return internalCluster().client(nodeWithMostRecentRun); } public void testExpiredApiKeysBehaviorWhenKeysExpired1WeekBeforeAnd1DayBefore() throws Exception { Client client = waitForExpiredApiKeysRemoverTriggerReadyAndGetClient().filterWithHeader( - Collections.singletonMap("Authorization", basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING))); + Collections.singletonMap("Authorization", basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING)) + ); int noOfKeys = 4; List createdApiKeys = createApiKeys(noOfKeys, null).v1(); @@ -464,8 +507,7 @@ public void testExpiredApiKeysBehaviorWhenKeysExpired1WeekBeforeAnd1DayBefore() // hack doc to modify the expiration time to a day before Instant dayBefore = created.minus(1L, ChronoUnit.DAYS); assertTrue(Instant.now().isAfter(dayBefore)); - UpdateResponse expirationDateUpdatedResponse = client - .prepareUpdate(SECURITY_MAIN_ALIAS, createdApiKeys.get(0).getId()) + UpdateResponse expirationDateUpdatedResponse = client.prepareUpdate(SECURITY_MAIN_ALIAS, createdApiKeys.get(0).getId()) .setDoc("expiration_time", dayBefore.toEpochMilli()) .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) .get(); @@ -483,8 +525,11 @@ public void testExpiredApiKeysBehaviorWhenKeysExpired1WeekBeforeAnd1DayBefore() // Invalidate to trigger the remover PlainActionFuture listener = new PlainActionFuture<>(); - client.execute(InvalidateApiKeyAction.INSTANCE, InvalidateApiKeyRequest.usingApiKeyId(createdApiKeys.get(2).getId(), false), - listener); + client.execute( + InvalidateApiKeyAction.INSTANCE, + InvalidateApiKeyRequest.usingApiKeyId(createdApiKeys.get(2).getId(), false), + listener + ); assertThat(listener.get().getInvalidatedApiKeys().size(), is(1)); awaitApiKeysRemoverCompletion(); @@ -495,8 +540,11 @@ public void testExpiredApiKeysBehaviorWhenKeysExpired1WeekBeforeAnd1DayBefore() getApiKeyResponseListener = new PlainActionFuture<>(); client.execute(GetApiKeyAction.INSTANCE, GetApiKeyRequest.usingRealmName("file"), getApiKeyResponseListener); - Set expectedKeyIds = Sets.newHashSet(createdApiKeys.get(0).getId(), createdApiKeys.get(2).getId(), - createdApiKeys.get(3).getId()); + Set expectedKeyIds = Sets.newHashSet( + createdApiKeys.get(0).getId(), + createdApiKeys.get(2).getId(), + createdApiKeys.get(3).getId() + ); boolean apiKeyInvalidatedButNotYetDeletedByExpiredApiKeysRemover = false; for (ApiKey apiKey : getApiKeyResponseListener.get().getApiKeyInfos()) { assertThat(apiKey.getId(), is(in(expectedKeyIds))); @@ -517,8 +565,10 @@ public void testExpiredApiKeysBehaviorWhenKeysExpired1WeekBeforeAnd1DayBefore() fail("unexpected API key " + apiKey); } } - assertThat(getApiKeyResponseListener.get().getApiKeyInfos().length, - is((apiKeyInvalidatedButNotYetDeletedByExpiredApiKeysRemover) ? 3 : 2)); + assertThat( + getApiKeyResponseListener.get().getApiKeyInfos().length, + is((apiKeyInvalidatedButNotYetDeletedByExpiredApiKeysRemover) ? 3 : 2) + ); } private void refreshSecurityIndex() throws Exception { @@ -533,7 +583,8 @@ public void testActiveApiKeysWithNoExpirationNeverGetDeletedByRemover() throws E List responses = tuple.v1(); Client client = client().filterWithHeader( - Collections.singletonMap("Authorization", basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING))); + Collections.singletonMap("Authorization", basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING)) + ); PlainActionFuture listener = new PlainActionFuture<>(); // trigger expired keys remover client.execute(InvalidateApiKeyAction.INSTANCE, InvalidateApiKeyRequest.usingApiKeyId(responses.get(1).getId(), false), listener); @@ -545,8 +596,14 @@ public void testActiveApiKeysWithNoExpirationNeverGetDeletedByRemover() throws E PlainActionFuture getApiKeyResponseListener = new PlainActionFuture<>(); client.execute(GetApiKeyAction.INSTANCE, GetApiKeyRequest.usingRealmName("file"), getApiKeyResponseListener); GetApiKeyResponse response = getApiKeyResponseListener.get(); - verifyGetResponse(2, responses, tuple.v2(), response, Collections.singleton(responses.get(0).getId()), - Collections.singletonList(responses.get(1).getId())); + verifyGetResponse( + 2, + responses, + tuple.v2(), + response, + Collections.singleton(responses.get(0).getId()), + Collections.singletonList(responses.get(1).getId()) + ); } public void testGetApiKeysForRealm() throws InterruptedException, ExecutionException { @@ -554,17 +611,23 @@ public void testGetApiKeysForRealm() throws InterruptedException, ExecutionExcep final Tuple, List>> tuple = createApiKeys(noOfApiKeys, null); List responses = tuple.v1(); Client client = client().filterWithHeader( - Collections.singletonMap("Authorization", basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING))); + Collections.singletonMap("Authorization", basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING)) + ); boolean invalidate = randomBoolean(); List invalidatedApiKeyIds = null; Set expectedValidKeyIds = null; if (invalidate) { PlainActionFuture listener = new PlainActionFuture<>(); - client.execute(InvalidateApiKeyAction.INSTANCE, InvalidateApiKeyRequest.usingApiKeyId(responses.get(0).getId(), false), - listener); + client.execute( + InvalidateApiKeyAction.INSTANCE, + InvalidateApiKeyRequest.usingApiKeyId(responses.get(0).getId(), false), + listener + ); InvalidateApiKeyResponse invalidateResponse = listener.get(); invalidatedApiKeyIds = invalidateResponse.getInvalidatedApiKeys(); - expectedValidKeyIds = responses.stream().filter(o -> o.getId().equals(responses.get(0).getId()) == false).map(o -> o.getId()) + expectedValidKeyIds = responses.stream() + .filter(o -> o.getId().equals(responses.get(0).getId()) == false) + .map(o -> o.getId()) .collect(Collectors.toSet()); } else { invalidatedApiKeyIds = Collections.emptyList(); @@ -574,9 +637,7 @@ public void testGetApiKeysForRealm() throws InterruptedException, ExecutionExcep PlainActionFuture listener = new PlainActionFuture<>(); client.execute(GetApiKeyAction.INSTANCE, GetApiKeyRequest.usingRealmName("file"), listener); GetApiKeyResponse response = listener.get(); - verifyGetResponse(noOfApiKeys, responses, tuple.v2(), response, - expectedValidKeyIds, - invalidatedApiKeyIds); + verifyGetResponse(noOfApiKeys, responses, tuple.v2(), response, expectedValidKeyIds, invalidatedApiKeyIds); } public void testGetApiKeysForUser() throws Exception { @@ -584,22 +645,29 @@ public void testGetApiKeysForUser() throws Exception { final Tuple, List>> tuple = createApiKeys(noOfApiKeys, null); List responses = tuple.v1(); Client client = client().filterWithHeader( - Collections.singletonMap("Authorization", basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING))); + Collections.singletonMap("Authorization", basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING)) + ); PlainActionFuture listener = new PlainActionFuture<>(); client.execute(GetApiKeyAction.INSTANCE, GetApiKeyRequest.usingUserName(TEST_SUPERUSER), listener); GetApiKeyResponse response = listener.get(); - verifyGetResponse(noOfApiKeys, responses, tuple.v2(), - response, responses.stream().map(o -> o.getId()).collect(Collectors.toSet()), null); + verifyGetResponse( + noOfApiKeys, + responses, + tuple.v2(), + response, + responses.stream().map(o -> o.getId()).collect(Collectors.toSet()), + null + ); } public void testGetApiKeysForRealmAndUser() throws InterruptedException, ExecutionException { final Tuple, List>> tuple = createApiKeys(1, null); List responses = tuple.v1(); Client client = client().filterWithHeader( - Collections.singletonMap("Authorization", basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING))); + Collections.singletonMap("Authorization", basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING)) + ); PlainActionFuture listener = new PlainActionFuture<>(); - client.execute(GetApiKeyAction.INSTANCE, GetApiKeyRequest.usingRealmAndUserName("file", TEST_SUPERUSER), - listener); + client.execute(GetApiKeyAction.INSTANCE, GetApiKeyRequest.usingRealmAndUserName("file", TEST_SUPERUSER), listener); GetApiKeyResponse response = listener.get(); verifyGetResponse(1, responses, tuple.v2(), response, Collections.singleton(responses.get(0).getId()), null); } @@ -608,7 +676,8 @@ public void testGetApiKeysForApiKeyId() throws InterruptedException, ExecutionEx final Tuple, List>> tuple = createApiKeys(1, null); List responses = tuple.v1(); Client client = client().filterWithHeader( - Collections.singletonMap("Authorization", basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING))); + Collections.singletonMap("Authorization", basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING)) + ); PlainActionFuture listener = new PlainActionFuture<>(); client.execute(GetApiKeyAction.INSTANCE, GetApiKeyRequest.usingApiKeyId(responses.get(0).getId(), false), listener); GetApiKeyResponse response = listener.get(); @@ -618,13 +687,19 @@ public void testGetApiKeysForApiKeyId() throws InterruptedException, ExecutionEx public void testGetApiKeysForApiKeyName() throws InterruptedException, ExecutionException { final Map headers = Collections.singletonMap( "Authorization", - basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING)); + basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING) + ); final int noOfApiKeys = randomIntBetween(1, 3); final Tuple, List>> tuple1 = createApiKeys(noOfApiKeys, null); final List createApiKeyResponses1 = tuple1.v1(); - final Tuple, List>> tuple2 = - createApiKeys(headers, noOfApiKeys, "another-test-key-", null, "monitor"); + final Tuple, List>> tuple2 = createApiKeys( + headers, + noOfApiKeys, + "another-test-key-", + null, + "monitor" + ); final List createApiKeyResponses2 = tuple2.v1(); Client client = client().filterWithHeader(headers); @@ -637,15 +712,27 @@ public void testGetApiKeysForApiKeyName() throws InterruptedException, Execution PlainActionFuture listener2 = new PlainActionFuture<>(); client.execute(GetApiKeyAction.INSTANCE, GetApiKeyRequest.usingApiKeyName("test-key*", false), listener2); - verifyGetResponse(noOfApiKeys, createApiKeyResponses1, tuple1.v2(), listener2.get(), - createApiKeyResponses1.stream().map(CreateApiKeyResponse::getId).collect(Collectors.toSet()), null); + verifyGetResponse( + noOfApiKeys, + createApiKeyResponses1, + tuple1.v2(), + listener2.get(), + createApiKeyResponses1.stream().map(CreateApiKeyResponse::getId).collect(Collectors.toSet()), + null + ); PlainActionFuture listener3 = new PlainActionFuture<>(); client.execute(GetApiKeyAction.INSTANCE, GetApiKeyRequest.usingApiKeyName("*", false), listener3); responses = Stream.concat(createApiKeyResponses1.stream(), createApiKeyResponses2.stream()).collect(Collectors.toList()); metadatas = Stream.concat(tuple1.v2().stream(), tuple2.v2().stream()).collect(Collectors.toList()); - verifyGetResponse(2 * noOfApiKeys, responses, metadatas, listener3.get(), - responses.stream().map(CreateApiKeyResponse::getId).collect(Collectors.toSet()), null); + verifyGetResponse( + 2 * noOfApiKeys, + responses, + metadatas, + listener3.get(), + responses.stream().map(CreateApiKeyResponse::getId).collect(Collectors.toSet()), + null + ); PlainActionFuture listener4 = new PlainActionFuture<>(); client.execute(GetApiKeyAction.INSTANCE, GetApiKeyRequest.usingApiKeyName("does-not-exist*", false), listener4); @@ -653,8 +740,14 @@ public void testGetApiKeysForApiKeyName() throws InterruptedException, Execution PlainActionFuture listener5 = new PlainActionFuture<>(); client.execute(GetApiKeyAction.INSTANCE, GetApiKeyRequest.usingApiKeyName("another-test-key*", false), listener5); - verifyGetResponse(noOfApiKeys, createApiKeyResponses2, tuple2.v2(), listener5.get(), - createApiKeyResponses2.stream().map(CreateApiKeyResponse::getId).collect(Collectors.toSet()), null); + verifyGetResponse( + noOfApiKeys, + createApiKeyResponses2, + tuple2.v2(), + listener5.get(), + createApiKeyResponses2.stream().map(CreateApiKeyResponse::getId).collect(Collectors.toSet()), + null + ); } public void testGetApiKeysOwnedByCurrentAuthenticatedUser() throws InterruptedException, ExecutionException { @@ -662,17 +755,29 @@ public void testGetApiKeysOwnedByCurrentAuthenticatedUser() throws InterruptedEx int noOfApiKeysForUserWithManageApiKeyRole = randomIntBetween(3, 5); List defaultUserCreatedKeys = createApiKeys(noOfSuperuserApiKeys, null).v1(); String userWithManageApiKeyRole = randomFrom("user_with_manage_api_key_role", "user_with_manage_own_api_key_role"); - final Tuple, List>> tuple = - createApiKeys(userWithManageApiKeyRole, noOfApiKeysForUserWithManageApiKeyRole, null, "monitor"); + final Tuple, List>> tuple = createApiKeys( + userWithManageApiKeyRole, + noOfApiKeysForUserWithManageApiKeyRole, + null, + "monitor" + ); List userWithManageApiKeyRoleApiKeys = tuple.v1(); final Client client = client().filterWithHeader( - Collections.singletonMap("Authorization", basicAuthHeaderValue(userWithManageApiKeyRole, TEST_PASSWORD_SECURE_STRING))); + Collections.singletonMap("Authorization", basicAuthHeaderValue(userWithManageApiKeyRole, TEST_PASSWORD_SECURE_STRING)) + ); PlainActionFuture listener = new PlainActionFuture<>(); client.execute(GetApiKeyAction.INSTANCE, GetApiKeyRequest.forOwnedApiKeys(), listener); GetApiKeyResponse response = listener.get(); - verifyGetResponse(userWithManageApiKeyRole, noOfApiKeysForUserWithManageApiKeyRole, userWithManageApiKeyRoleApiKeys, tuple.v2(), - response, userWithManageApiKeyRoleApiKeys.stream().map(o -> o.getId()).collect(Collectors.toSet()), null); + verifyGetResponse( + userWithManageApiKeyRole, + noOfApiKeysForUserWithManageApiKeyRole, + userWithManageApiKeyRoleApiKeys, + tuple.v2(), + response, + userWithManageApiKeyRoleApiKeys.stream().map(o -> o.getId()).collect(Collectors.toSet()), + null + ); } public void testGetApiKeysOwnedByRunAsUserWhenOwnerIsTrue() throws ExecutionException, InterruptedException { @@ -680,18 +785,26 @@ public void testGetApiKeysOwnedByRunAsUserWhenOwnerIsTrue() throws ExecutionExce int noOfSuperuserApiKeys = randomIntBetween(3, 5); int noOfApiKeysForUserWithManageApiKeyRole = randomIntBetween(3, 5); createApiKeys(noOfSuperuserApiKeys, null); - final Tuple, List>> tuple = createApiKeys("user_with_manage_own_api_key_role", + final Tuple, List>> tuple = createApiKeys( + "user_with_manage_own_api_key_role", "user_with_run_as_role", noOfApiKeysForUserWithManageApiKeyRole, null, - "monitor"); + "monitor" + ); List userWithManageOwnApiKeyRoleApiKeys = tuple.v1(); PlainActionFuture listener = new PlainActionFuture<>(); getClientForRunAsUser().execute(GetApiKeyAction.INSTANCE, GetApiKeyRequest.forOwnedApiKeys(), listener); GetApiKeyResponse response = listener.get(); - verifyGetResponse("user_with_manage_own_api_key_role", noOfApiKeysForUserWithManageApiKeyRole, - userWithManageOwnApiKeyRoleApiKeys, tuple.v2(), - response, userWithManageOwnApiKeyRoleApiKeys.stream().map(o -> o.getId()).collect(Collectors.toSet()), null); + verifyGetResponse( + "user_with_manage_own_api_key_role", + noOfApiKeysForUserWithManageApiKeyRole, + userWithManageOwnApiKeyRoleApiKeys, + tuple.v2(), + response, + userWithManageOwnApiKeyRoleApiKeys.stream().map(o -> o.getId()).collect(Collectors.toSet()), + null + ); } public void testGetApiKeysOwnedByRunAsUserWhenRunAsUserInfoIsGiven() throws ExecutionException, InterruptedException { @@ -699,19 +812,30 @@ public void testGetApiKeysOwnedByRunAsUserWhenRunAsUserInfoIsGiven() throws Exec int noOfSuperuserApiKeys = randomIntBetween(3, 5); int noOfApiKeysForUserWithManageApiKeyRole = randomIntBetween(3, 5); createApiKeys(noOfSuperuserApiKeys, null); - final Tuple, List>> tuple = createApiKeys("user_with_manage_own_api_key_role", + final Tuple, List>> tuple = createApiKeys( + "user_with_manage_own_api_key_role", "user_with_run_as_role", noOfApiKeysForUserWithManageApiKeyRole, null, - "monitor"); + "monitor" + ); List userWithManageOwnApiKeyRoleApiKeys = tuple.v1(); PlainActionFuture listener = new PlainActionFuture<>(); - getClientForRunAsUser().execute(GetApiKeyAction.INSTANCE, - GetApiKeyRequest.usingRealmAndUserName("file", "user_with_manage_own_api_key_role"), listener); + getClientForRunAsUser().execute( + GetApiKeyAction.INSTANCE, + GetApiKeyRequest.usingRealmAndUserName("file", "user_with_manage_own_api_key_role"), + listener + ); GetApiKeyResponse response = listener.get(); - verifyGetResponse("user_with_manage_own_api_key_role", noOfApiKeysForUserWithManageApiKeyRole, - userWithManageOwnApiKeyRoleApiKeys, tuple.v2(), - response, userWithManageOwnApiKeyRoleApiKeys.stream().map(o -> o.getId()).collect(Collectors.toSet()), null); + verifyGetResponse( + "user_with_manage_own_api_key_role", + noOfApiKeysForUserWithManageApiKeyRole, + userWithManageOwnApiKeyRoleApiKeys, + tuple.v2(), + response, + userWithManageOwnApiKeyRoleApiKeys.stream().map(o -> o.getId()).collect(Collectors.toSet()), + null + ); } public void testGetApiKeysOwnedByRunAsUserWillNotWorkWhenAuthUserInfoIsGiven() throws ExecutionException, InterruptedException { @@ -719,19 +843,30 @@ public void testGetApiKeysOwnedByRunAsUserWillNotWorkWhenAuthUserInfoIsGiven() t int noOfSuperuserApiKeys = randomIntBetween(3, 5); int noOfApiKeysForUserWithManageApiKeyRole = randomIntBetween(3, 5); createApiKeys(noOfSuperuserApiKeys, null); - final List userWithManageOwnApiKeyRoleApiKeys = createApiKeys("user_with_manage_own_api_key_role", - "user_with_run_as_role", noOfApiKeysForUserWithManageApiKeyRole, null, "monitor").v1(); + final List userWithManageOwnApiKeyRoleApiKeys = createApiKeys( + "user_with_manage_own_api_key_role", + "user_with_run_as_role", + noOfApiKeysForUserWithManageApiKeyRole, + null, + "monitor" + ).v1(); PlainActionFuture listener = new PlainActionFuture<>(); @SuppressWarnings("unchecked") final Tuple invalidRealmAndUserPair = randomFrom( new Tuple<>("file", "user_with_run_as_role"), new Tuple<>("index", "user_with_manage_own_api_key_role"), - new Tuple<>("index", "user_with_run_as_role")); - getClientForRunAsUser().execute(GetApiKeyAction.INSTANCE, - GetApiKeyRequest.usingRealmAndUserName(invalidRealmAndUserPair.v1(), invalidRealmAndUserPair.v2()), listener); + new Tuple<>("index", "user_with_run_as_role") + ); + getClientForRunAsUser().execute( + GetApiKeyAction.INSTANCE, + GetApiKeyRequest.usingRealmAndUserName(invalidRealmAndUserPair.v1(), invalidRealmAndUserPair.v2()), + listener + ); final ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, listener::actionGet); - assertThat(e.getMessage(), containsString( - "unauthorized for user [user_with_run_as_role] run as [user_with_manage_own_api_key_role]")); + assertThat( + e.getMessage(), + containsString("unauthorized for user [user_with_run_as_role] run as [user_with_manage_own_api_key_role]") + ); } public void testGetAllApiKeys() throws InterruptedException, ExecutionException { @@ -740,27 +875,42 @@ public void testGetAllApiKeys() throws InterruptedException, ExecutionException int noOfApiKeysForUserWithManageOwnApiKeyRole = randomIntBetween(3, 7); final Tuple, List>> defaultUserTuple = createApiKeys(noOfSuperuserApiKeys, null); List defaultUserCreatedKeys = defaultUserTuple.v1(); - final Tuple, List>> userWithManageTuple = - createApiKeys("user_with_manage_api_key_role", noOfApiKeysForUserWithManageApiKeyRole, null, "monitor"); + final Tuple, List>> userWithManageTuple = createApiKeys( + "user_with_manage_api_key_role", + noOfApiKeysForUserWithManageApiKeyRole, + null, + "monitor" + ); List userWithManageApiKeyRoleApiKeys = userWithManageTuple.v1(); - final Tuple, List>> userWithManageOwnTuple = - createApiKeys("user_with_manage_own_api_key_role", noOfApiKeysForUserWithManageOwnApiKeyRole, null, "monitor"); + final Tuple, List>> userWithManageOwnTuple = createApiKeys( + "user_with_manage_own_api_key_role", + noOfApiKeysForUserWithManageOwnApiKeyRole, + null, + "monitor" + ); List userWithManageOwnApiKeyRoleApiKeys = userWithManageOwnTuple.v1(); final Client client = client().filterWithHeader( - Collections.singletonMap("Authorization", basicAuthHeaderValue("user_with_manage_api_key_role", TEST_PASSWORD_SECURE_STRING))); + Collections.singletonMap("Authorization", basicAuthHeaderValue("user_with_manage_api_key_role", TEST_PASSWORD_SECURE_STRING)) + ); PlainActionFuture listener = new PlainActionFuture<>(); client.execute(GetApiKeyAction.INSTANCE, new GetApiKeyRequest(), listener); GetApiKeyResponse response = listener.get(); int totalApiKeys = noOfSuperuserApiKeys + noOfApiKeysForUserWithManageApiKeyRole + noOfApiKeysForUserWithManageOwnApiKeyRole; List allApiKeys = new ArrayList<>(); - Stream.of(defaultUserCreatedKeys, userWithManageApiKeyRoleApiKeys, userWithManageOwnApiKeyRoleApiKeys).forEach( - allApiKeys::addAll); + Stream.of(defaultUserCreatedKeys, userWithManageApiKeyRoleApiKeys, userWithManageOwnApiKeyRoleApiKeys).forEach(allApiKeys::addAll); final List> metadatas = Stream.of(defaultUserTuple.v2(), userWithManageTuple.v2(), userWithManageOwnTuple.v2()) - .flatMap(List::stream).collect(Collectors.toList()); - verifyGetResponse(new String[] {TEST_SUPERUSER, "user_with_manage_api_key_role", - "user_with_manage_own_api_key_role" }, totalApiKeys, allApiKeys, metadatas, response, - allApiKeys.stream().map(o -> o.getId()).collect(Collectors.toSet()), null); + .flatMap(List::stream) + .collect(Collectors.toList()); + verifyGetResponse( + new String[] { TEST_SUPERUSER, "user_with_manage_api_key_role", "user_with_manage_own_api_key_role" }, + totalApiKeys, + allApiKeys, + metadatas, + response, + allApiKeys.stream().map(o -> o.getId()).collect(Collectors.toSet()), + null + ); } public void testGetAllApiKeysFailsForUserWithNoRoleOrRetrieveOwnApiKeyRole() throws InterruptedException, ExecutionException { @@ -768,14 +918,23 @@ public void testGetAllApiKeysFailsForUserWithNoRoleOrRetrieveOwnApiKeyRole() thr int noOfApiKeysForUserWithManageApiKeyRole = randomIntBetween(3, 5); int noOfApiKeysForUserWithManageOwnApiKeyRole = randomIntBetween(3, 7); List defaultUserCreatedKeys = createApiKeys(noOfSuperuserApiKeys, null).v1(); - List userWithManageApiKeyRoleApiKeys = createApiKeys("user_with_manage_api_key_role", - noOfApiKeysForUserWithManageApiKeyRole, null, "monitor").v1(); - List userWithManageOwnApiKeyRoleApiKeys = createApiKeys("user_with_manage_own_api_key_role", - noOfApiKeysForUserWithManageOwnApiKeyRole, null, "monitor").v1(); + List userWithManageApiKeyRoleApiKeys = createApiKeys( + "user_with_manage_api_key_role", + noOfApiKeysForUserWithManageApiKeyRole, + null, + "monitor" + ).v1(); + List userWithManageOwnApiKeyRoleApiKeys = createApiKeys( + "user_with_manage_own_api_key_role", + noOfApiKeysForUserWithManageOwnApiKeyRole, + null, + "monitor" + ).v1(); final String withUser = randomFrom("user_with_manage_own_api_key_role", "user_with_no_api_key_role"); final Client client = client().filterWithHeader( - Collections.singletonMap("Authorization", basicAuthHeaderValue(withUser, TEST_PASSWORD_SECURE_STRING))); + Collections.singletonMap("Authorization", basicAuthHeaderValue(withUser, TEST_PASSWORD_SECURE_STRING)) + ); PlainActionFuture listener = new PlainActionFuture<>(); client.execute(GetApiKeyAction.INSTANCE, new GetApiKeyRequest(), listener); ElasticsearchSecurityException ese = expectThrows(ElasticsearchSecurityException.class, () -> listener.actionGet()); @@ -787,10 +946,15 @@ public void testInvalidateApiKeysOwnedByCurrentAuthenticatedUser() throws Interr int noOfApiKeysForUserWithManageApiKeyRole = randomIntBetween(3, 5); List defaultUserCreatedKeys = createApiKeys(noOfSuperuserApiKeys, null).v1(); String userWithManageApiKeyRole = randomFrom("user_with_manage_api_key_role", "user_with_manage_own_api_key_role"); - List userWithManageApiKeyRoleApiKeys = createApiKeys(userWithManageApiKeyRole, - noOfApiKeysForUserWithManageApiKeyRole, null, "monitor").v1(); + List userWithManageApiKeyRoleApiKeys = createApiKeys( + userWithManageApiKeyRole, + noOfApiKeysForUserWithManageApiKeyRole, + null, + "monitor" + ).v1(); final Client client = client().filterWithHeader( - Collections.singletonMap("Authorization", basicAuthHeaderValue(userWithManageApiKeyRole, TEST_PASSWORD_SECURE_STRING))); + Collections.singletonMap("Authorization", basicAuthHeaderValue(userWithManageApiKeyRole, TEST_PASSWORD_SECURE_STRING)) + ); PlainActionFuture listener = new PlainActionFuture<>(); client.execute(InvalidateApiKeyAction.INSTANCE, InvalidateApiKeyRequest.forOwnedApiKeys(), listener); @@ -804,8 +968,13 @@ public void testInvalidateApiKeysOwnedByRunAsUserWhenOwnerIsTrue() throws Interr int noOfSuperuserApiKeys = randomIntBetween(3, 5); int noOfApiKeysForUserWithManageApiKeyRole = randomIntBetween(3, 5); createApiKeys(noOfSuperuserApiKeys, null); - List userWithManageApiKeyRoleApiKeys = createApiKeys("user_with_manage_own_api_key_role", - "user_with_run_as_role", noOfApiKeysForUserWithManageApiKeyRole, null, "monitor").v1(); + List userWithManageApiKeyRoleApiKeys = createApiKeys( + "user_with_manage_own_api_key_role", + "user_with_run_as_role", + noOfApiKeysForUserWithManageApiKeyRole, + null, + "monitor" + ).v1(); PlainActionFuture listener = new PlainActionFuture<>(); getClientForRunAsUser().execute(InvalidateApiKeyAction.INSTANCE, InvalidateApiKeyRequest.forOwnedApiKeys(), listener); InvalidateApiKeyResponse invalidateResponse = listener.get(); @@ -817,11 +986,19 @@ public void testInvalidateApiKeysOwnedByRunAsUserWhenRunAsUserInfoIsGiven() thro int noOfSuperuserApiKeys = randomIntBetween(3, 5); int noOfApiKeysForUserWithManageApiKeyRole = randomIntBetween(3, 5); createApiKeys(noOfSuperuserApiKeys, null); - List userWithManageApiKeyRoleApiKeys = createApiKeys("user_with_manage_own_api_key_role", - "user_with_run_as_role", noOfApiKeysForUserWithManageApiKeyRole, null, "monitor").v1(); + List userWithManageApiKeyRoleApiKeys = createApiKeys( + "user_with_manage_own_api_key_role", + "user_with_run_as_role", + noOfApiKeysForUserWithManageApiKeyRole, + null, + "monitor" + ).v1(); PlainActionFuture listener = new PlainActionFuture<>(); - getClientForRunAsUser().execute(InvalidateApiKeyAction.INSTANCE, - InvalidateApiKeyRequest.usingRealmAndUserName("file", "user_with_manage_own_api_key_role"), listener); + getClientForRunAsUser().execute( + InvalidateApiKeyAction.INSTANCE, + InvalidateApiKeyRequest.usingRealmAndUserName("file", "user_with_manage_own_api_key_role"), + listener + ); InvalidateApiKeyResponse invalidateResponse = listener.get(); verifyInvalidateResponse(noOfApiKeysForUserWithManageApiKeyRole, userWithManageApiKeyRoleApiKeys, invalidateResponse); } @@ -831,28 +1008,38 @@ public void testInvalidateApiKeysOwnedByRunAsUserWillNotWorkWhenAuthUserInfoIsGi int noOfSuperuserApiKeys = randomIntBetween(3, 5); int noOfApiKeysForUserWithManageApiKeyRole = randomIntBetween(3, 5); createApiKeys(noOfSuperuserApiKeys, null); - List userWithManageApiKeyRoleApiKeys = createApiKeys("user_with_manage_own_api_key_role", - "user_with_run_as_role", noOfApiKeysForUserWithManageApiKeyRole, null, "monitor").v1(); + List userWithManageApiKeyRoleApiKeys = createApiKeys( + "user_with_manage_own_api_key_role", + "user_with_run_as_role", + noOfApiKeysForUserWithManageApiKeyRole, + null, + "monitor" + ).v1(); PlainActionFuture listener = new PlainActionFuture<>(); @SuppressWarnings("unchecked") final Tuple invalidRealmAndUserPair = randomFrom( new Tuple<>("file", "user_with_run_as_role"), new Tuple<>("index", "user_with_manage_own_api_key_role"), - new Tuple<>("index", "user_with_run_as_role")); - getClientForRunAsUser().execute(InvalidateApiKeyAction.INSTANCE, - InvalidateApiKeyRequest.usingRealmAndUserName(invalidRealmAndUserPair.v1(), invalidRealmAndUserPair.v2()), listener); + new Tuple<>("index", "user_with_run_as_role") + ); + getClientForRunAsUser().execute( + InvalidateApiKeyAction.INSTANCE, + InvalidateApiKeyRequest.usingRealmAndUserName(invalidRealmAndUserPair.v1(), invalidRealmAndUserPair.v2()), + listener + ); final ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, listener::actionGet); - assertThat(e.getMessage(), containsString( - "unauthorized for user [user_with_run_as_role] run as [user_with_manage_own_api_key_role]")); + assertThat( + e.getMessage(), + containsString("unauthorized for user [user_with_run_as_role] run as [user_with_manage_own_api_key_role]") + ); } public void testApiKeyAuthorizationApiKeyMustBeAbleToRetrieveItsOwnInformationButNotAnyOtherKeysCreatedBySameOwner() throws InterruptedException, ExecutionException { - final Tuple, List>> tuple = - createApiKeys(TEST_SUPERUSER, 2, null, (String[]) null); + final Tuple, List>> tuple = createApiKeys(TEST_SUPERUSER, 2, null, (String[]) null); List responses = tuple.v1(); - final String base64ApiKeyKeyValue = Base64.getEncoder().encodeToString( - (responses.get(0).getId() + ":" + responses.get(0).getKey().toString()).getBytes(StandardCharsets.UTF_8)); + final String base64ApiKeyKeyValue = Base64.getEncoder() + .encodeToString((responses.get(0).getId() + ":" + responses.get(0).getKey().toString()).getBytes(StandardCharsets.UTF_8)); Client client = client().filterWithHeader(Map.of("Authorization", "ApiKey " + base64ApiKeyKeyValue)); PlainActionFuture listener = new PlainActionFuture<>(); client.execute(GetApiKeyAction.INSTANCE, GetApiKeyRequest.usingApiKeyId(responses.get(0).getId(), randomBoolean()), listener); @@ -861,11 +1048,13 @@ public void testApiKeyAuthorizationApiKeyMustBeAbleToRetrieveItsOwnInformationBu final PlainActionFuture failureListener = new PlainActionFuture<>(); // for any other API key id, it must deny access - client.execute(GetApiKeyAction.INSTANCE, GetApiKeyRequest.usingApiKeyId(responses.get(1).getId(), randomBoolean()), - failureListener); + client.execute( + GetApiKeyAction.INSTANCE, + GetApiKeyRequest.usingApiKeyId(responses.get(1).getId(), randomBoolean()), + failureListener + ); ElasticsearchSecurityException ese = expectThrows(ElasticsearchSecurityException.class, () -> failureListener.actionGet()); - assertErrorMessage(ese, "cluster:admin/xpack/security/api_key/get", TEST_SUPERUSER, - responses.get(0).getId()); + assertErrorMessage(ese, "cluster:admin/xpack/security/api_key/get", TEST_SUPERUSER, responses.get(0).getId()); final PlainActionFuture failureListener1 = new PlainActionFuture<>(); client.execute(GetApiKeyAction.INSTANCE, GetApiKeyRequest.forOwnedApiKeys(), failureListener1); @@ -873,20 +1062,22 @@ public void testApiKeyAuthorizationApiKeyMustBeAbleToRetrieveItsOwnInformationBu assertErrorMessage(ese, "cluster:admin/xpack/security/api_key/get", TEST_SUPERUSER, responses.get(0).getId()); } - public void testApiKeyWithManageOwnPrivilegeIsAbleToInvalidateItselfButNotAnyOtherKeysCreatedBySameOwner() - throws InterruptedException, ExecutionException { + public void testApiKeyWithManageOwnPrivilegeIsAbleToInvalidateItselfButNotAnyOtherKeysCreatedBySameOwner() throws InterruptedException, + ExecutionException { List responses = createApiKeys(TEST_SUPERUSER, 2, null, "manage_own_api_key").v1(); - final String base64ApiKeyKeyValue = Base64.getEncoder().encodeToString( - (responses.get(0).getId() + ":" + responses.get(0).getKey().toString()).getBytes(StandardCharsets.UTF_8)); + final String base64ApiKeyKeyValue = Base64.getEncoder() + .encodeToString((responses.get(0).getId() + ":" + responses.get(0).getKey().toString()).getBytes(StandardCharsets.UTF_8)); Client client = client().filterWithHeader(Map.of("Authorization", "ApiKey " + base64ApiKeyKeyValue)); final PlainActionFuture failureListener = new PlainActionFuture<>(); // for any other API key id, it must deny access - client.execute(InvalidateApiKeyAction.INSTANCE, InvalidateApiKeyRequest.usingApiKeyId(responses.get(1).getId(), randomBoolean()), - failureListener); + client.execute( + InvalidateApiKeyAction.INSTANCE, + InvalidateApiKeyRequest.usingApiKeyId(responses.get(1).getId(), randomBoolean()), + failureListener + ); ElasticsearchSecurityException ese = expectThrows(ElasticsearchSecurityException.class, () -> failureListener.actionGet()); - assertErrorMessage(ese, "cluster:admin/xpack/security/api_key/invalidate", TEST_SUPERUSER, - responses.get(0).getId()); + assertErrorMessage(ese, "cluster:admin/xpack/security/api_key/invalidate", TEST_SUPERUSER, responses.get(0).getId()); final PlainActionFuture failureListener1 = new PlainActionFuture<>(); client.execute(InvalidateApiKeyAction.INSTANCE, InvalidateApiKeyRequest.forOwnedApiKeys(), failureListener1); @@ -894,8 +1085,11 @@ public void testApiKeyWithManageOwnPrivilegeIsAbleToInvalidateItselfButNotAnyOth assertErrorMessage(ese, "cluster:admin/xpack/security/api_key/invalidate", TEST_SUPERUSER, responses.get(0).getId()); PlainActionFuture listener = new PlainActionFuture<>(); - client.execute(InvalidateApiKeyAction.INSTANCE, InvalidateApiKeyRequest.usingApiKeyId(responses.get(0).getId(), randomBoolean()), - listener); + client.execute( + InvalidateApiKeyAction.INSTANCE, + InvalidateApiKeyRequest.usingApiKeyId(responses.get(0).getId(), randomBoolean()), + listener + ); InvalidateApiKeyResponse invalidateResponse = listener.get(); assertThat(invalidateResponse.getInvalidatedApiKeys().size(), equalTo(1)); @@ -905,13 +1099,11 @@ public void testApiKeyWithManageOwnPrivilegeIsAbleToInvalidateItselfButNotAnyOth } public void testDerivedKeys() throws ExecutionException, InterruptedException { - Client client = client().filterWithHeader(Collections.singletonMap("Authorization", - basicAuthHeaderValue(TEST_SUPERUSER, - TEST_PASSWORD_SECURE_STRING))); - final CreateApiKeyResponse response = new CreateApiKeyRequestBuilder(client) - .setName("key-1") - .setRoleDescriptors(Collections.singletonList( - new RoleDescriptor("role", new String[] { "manage_api_key" }, null, null))) + Client client = client().filterWithHeader( + Collections.singletonMap("Authorization", basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING)) + ); + final CreateApiKeyResponse response = new CreateApiKeyRequestBuilder(client).setName("key-1") + .setRoleDescriptors(Collections.singletonList(new RoleDescriptor("role", new String[] { "manage_api_key" }, null, null))) .setMetadata(ApiKeyTests.randomMetadata()) .get(); @@ -920,45 +1112,54 @@ public void testDerivedKeys() throws ExecutionException, InterruptedException { assertNotNull(response.getKey()); // use the first ApiKey for authorized action - final String base64ApiKeyKeyValue = Base64.getEncoder().encodeToString( - (response.getId() + ":" + response.getKey().toString()).getBytes(StandardCharsets.UTF_8)); + final String base64ApiKeyKeyValue = Base64.getEncoder() + .encodeToString((response.getId() + ":" + response.getKey().toString()).getBytes(StandardCharsets.UTF_8)); final Client clientKey1 = client().filterWithHeader(Collections.singletonMap("Authorization", "ApiKey " + base64ApiKeyKeyValue)); final String expectedMessage = "creating derived api keys requires an explicit role descriptor that is empty"; - final IllegalArgumentException e1 = expectThrows(IllegalArgumentException.class, - () -> new CreateApiKeyRequestBuilder(clientKey1).setName("key-2").setMetadata(ApiKeyTests.randomMetadata()).get()); + final IllegalArgumentException e1 = expectThrows( + IllegalArgumentException.class, + () -> new CreateApiKeyRequestBuilder(clientKey1).setName("key-2").setMetadata(ApiKeyTests.randomMetadata()).get() + ); assertThat(e1.getMessage(), containsString(expectedMessage)); - final IllegalArgumentException e2 = expectThrows(IllegalArgumentException.class, - () -> new CreateApiKeyRequestBuilder(clientKey1).setName("key-3") - .setRoleDescriptors(Collections.emptyList()).get()); + final IllegalArgumentException e2 = expectThrows( + IllegalArgumentException.class, + () -> new CreateApiKeyRequestBuilder(clientKey1).setName("key-3").setRoleDescriptors(Collections.emptyList()).get() + ); assertThat(e2.getMessage(), containsString(expectedMessage)); - final IllegalArgumentException e3 = expectThrows(IllegalArgumentException.class, + final IllegalArgumentException e3 = expectThrows( + IllegalArgumentException.class, () -> new CreateApiKeyRequestBuilder(clientKey1).setName("key-4") .setMetadata(ApiKeyTests.randomMetadata()) - .setRoleDescriptors(Collections.singletonList( - new RoleDescriptor("role", new String[] { "manage_own_api_key" }, null, null) - )).get()); + .setRoleDescriptors( + Collections.singletonList(new RoleDescriptor("role", new String[] { "manage_own_api_key" }, null, null)) + ) + .get() + ); assertThat(e3.getMessage(), containsString(expectedMessage)); - final List roleDescriptors = randomList(2, 10, - () -> new RoleDescriptor("role", null, null, null)); - roleDescriptors.set(randomInt(roleDescriptors.size() - 1), - new RoleDescriptor("role", new String[] { "manage_own_api_key" }, null, null)); + final List roleDescriptors = randomList(2, 10, () -> new RoleDescriptor("role", null, null, null)); + roleDescriptors.set( + randomInt(roleDescriptors.size() - 1), + new RoleDescriptor("role", new String[] { "manage_own_api_key" }, null, null) + ); - final IllegalArgumentException e4 = expectThrows(IllegalArgumentException.class, + final IllegalArgumentException e4 = expectThrows( + IllegalArgumentException.class, () -> new CreateApiKeyRequestBuilder(clientKey1).setName("key-5") .setMetadata(ApiKeyTests.randomMetadata()) - .setRoleDescriptors(roleDescriptors).get()); + .setRoleDescriptors(roleDescriptors) + .get() + ); assertThat(e4.getMessage(), containsString(expectedMessage)); final CreateApiKeyResponse key100Response = new CreateApiKeyRequestBuilder(clientKey1).setName("key-100") .setMetadata(ApiKeyTests.randomMetadata()) - .setRoleDescriptors(Collections.singletonList( - new RoleDescriptor("role", null, null, null) - )).get(); + .setRoleDescriptors(Collections.singletonList(new RoleDescriptor("role", null, null, null))) + .get(); assertEquals("key-100", key100Response.getName()); assertNotNull(key100Response.getId()); assertNotNull(key100Response.getKey()); @@ -978,11 +1179,11 @@ public void testCreationAndAuthenticationReturns429WhenThreadPoolIsSaturated() t final ThreadPool threadPool = internalCluster().getInstance(ThreadPool.class, nodeName); final ApiKeyService apiKeyService = internalCluster().getInstance(ApiKeyService.class, nodeName); - final RoleDescriptor descriptor = new RoleDescriptor("auth_only", new String[] { }, null, null); + final RoleDescriptor descriptor = new RoleDescriptor("auth_only", new String[] {}, null, null); final Client client = client().filterWithHeader( - Collections.singletonMap("Authorization", basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING))); - final CreateApiKeyResponse createApiKeyResponse = new CreateApiKeyRequestBuilder(client) - .setName("auth only key") + Collections.singletonMap("Authorization", basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING)) + ); + final CreateApiKeyResponse createApiKeyResponse = new CreateApiKeyRequestBuilder(client).setName("auth only key") .setRoleDescriptors(Collections.singletonList(descriptor)) .setMetadata(ApiKeyTests.randomMetadata()) .get(); @@ -992,7 +1193,12 @@ public void testCreationAndAuthenticationReturns429WhenThreadPoolIsSaturated() t // Clear the auth cache to force recompute the expensive hash which requires the crypto thread pool apiKeyService.getApiKeyAuthCache().invalidateAll(); - final List nodeInfos = client().admin().cluster().prepareNodesInfo().get().getNodes().stream() + final List nodeInfos = client().admin() + .cluster() + .prepareNodesInfo() + .get() + .getNodes() + .stream() .filter(nodeInfo -> nodeInfo.getNode().getName().equals(nodeName)) .collect(Collectors.toList()); assertEquals(1, nodeInfos.size()); @@ -1018,27 +1224,31 @@ public void testCreationAndAuthenticationReturns429WhenThreadPoolIsSaturated() t int i = 0; try { for (i = 0; i < CRYPTO_THREAD_POOL_QUEUE_SIZE; i++) { - lastTaskFuture = executorService.submit(() -> { }); + lastTaskFuture = executorService.submit(() -> {}); } } catch (EsRejectedExecutionException e) { logger.info("Attempted to push {} tasks but only pushed {}", CRYPTO_THREAD_POOL_QUEUE_SIZE, i + 1); } try (RestClient restClient = createRestClient(nodeInfos, null, "http")) { - final String base64ApiKeyKeyValue = Base64.getEncoder().encodeToString( - (createApiKeyResponse.getId() + ":" + createApiKeyResponse.getKey().toString()).getBytes(StandardCharsets.UTF_8)); + final String base64ApiKeyKeyValue = Base64.getEncoder() + .encodeToString( + (createApiKeyResponse.getId() + ":" + createApiKeyResponse.getKey().toString()).getBytes(StandardCharsets.UTF_8) + ); final Request authRequest = new Request("GET", "_security/_authenticate"); - authRequest.setOptions(RequestOptions.DEFAULT.toBuilder().addHeader( - "Authorization", "ApiKey " + base64ApiKeyKeyValue).build()); + authRequest.setOptions(RequestOptions.DEFAULT.toBuilder().addHeader("Authorization", "ApiKey " + base64ApiKeyKeyValue).build()); final ResponseException e1 = expectThrows(ResponseException.class, () -> restClient.performRequest(authRequest)); assertThat(e1.getMessage(), containsString("429 Too Many Requests")); assertThat(e1.getResponse().getStatusLine().getStatusCode(), is(429)); final Request createApiKeyRequest = new Request("POST", "_security/api_key"); createApiKeyRequest.setJsonEntity("{\"name\":\"key\"}"); - createApiKeyRequest.setOptions(createApiKeyRequest.getOptions().toBuilder() - .addHeader("Authorization", basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING))); + createApiKeyRequest.setOptions( + createApiKeyRequest.getOptions() + .toBuilder() + .addHeader("Authorization", basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING)) + ); final ResponseException e2 = expectThrows(ResponseException.class, () -> restClient.performRequest(createApiKeyRequest)); assertThat(e2.getMessage(), containsString("429 Too Many Requests")); assertThat(e2.getResponse().getStatusLine().getStatusCode(), is(429)); @@ -1060,10 +1270,8 @@ public void testCacheInvalidationViaApiCalls() throws Exception { String docId2 = createApiKeyAndAuthenticateWithIt().v1(); // Find out which nodes handled the above authentication requests - final ApiKeyService serviceForDoc1 = - services.stream().filter(s -> s.getDocCache().get(docId1) != null).findFirst().orElseThrow(); - final ApiKeyService serviceForDoc2 = - services.stream().filter(s -> s.getDocCache().get(docId2) != null).findFirst().orElseThrow(); + final ApiKeyService serviceForDoc1 = services.stream().filter(s -> s.getDocCache().get(docId1) != null).findFirst().orElseThrow(); + final ApiKeyService serviceForDoc2 = services.stream().filter(s -> s.getDocCache().get(docId2) != null).findFirst().orElseThrow(); assertNotNull(serviceForDoc1.getFromCache(docId1)); assertNotNull(serviceForDoc2.getFromCache(docId2)); final boolean sameServiceNode = serviceForDoc1 == serviceForDoc2; @@ -1081,8 +1289,10 @@ public void testCacheInvalidationViaApiCalls() throws Exception { ClearSecurityCacheRequest clearSecurityCacheRequest = new ClearSecurityCacheRequest(); clearSecurityCacheRequest.cacheName("api_key"); clearSecurityCacheRequest.keys(docId1); - ClearSecurityCacheResponse clearSecurityCacheResponse = - client().execute(ClearSecurityCacheAction.INSTANCE, clearSecurityCacheRequest).get(); + ClearSecurityCacheResponse clearSecurityCacheResponse = client().execute( + ClearSecurityCacheAction.INSTANCE, + clearSecurityCacheRequest + ).get(); assertFalse(clearSecurityCacheResponse.hasFailures()); assertBusy(() -> { @@ -1102,8 +1312,7 @@ public void testCacheInvalidationViaApiCalls() throws Exception { // Invalidate all cache entries by setting keys to an empty array clearSecurityCacheRequest.keys(new String[0]); - clearSecurityCacheResponse = - client().execute(ClearSecurityCacheAction.INSTANCE, clearSecurityCacheRequest).get(); + clearSecurityCacheResponse = client().execute(ClearSecurityCacheAction.INSTANCE, clearSecurityCacheRequest).get(); assertFalse(clearSecurityCacheResponse.hasFailures()); assertBusy(() -> { assertEquals(0, serviceForDoc1.getDocCache().count()); @@ -1126,15 +1335,16 @@ public void testSecurityIndexStateChangeWillInvalidateApiKeyCaches() throws Exce String docId = createApiKeyAndAuthenticateWithIt().v1(); // The API key is cached by one of the node that the above request hits, find out which one - final ApiKeyService apiKeyService = - services.stream().filter(s -> s.getDocCache().count() > 0).findFirst().orElseThrow(); + final ApiKeyService apiKeyService = services.stream().filter(s -> s.getDocCache().count() > 0).findFirst().orElseThrow(); assertNotNull(apiKeyService.getFromCache(docId)); assertEquals(1, apiKeyService.getDocCache().count()); assertEquals(2, apiKeyService.getRoleDescriptorsBytesCache().count()); // Close security index to trigger invalidation - final CloseIndexResponse closeIndexResponse = client().admin().indices().close( - new CloseIndexRequest(INTERNAL_SECURITY_MAIN_INDEX_7)).get(); + final CloseIndexResponse closeIndexResponse = client().admin() + .indices() + .close(new CloseIndexRequest(INTERNAL_SECURITY_MAIN_INDEX_7)) + .get(); assertTrue(closeIndexResponse.isAcknowledged()); assertBusy(() -> { expectThrows(NullPointerException.class, () -> apiKeyService.getFromCache(docId)); @@ -1145,71 +1355,108 @@ public void testSecurityIndexStateChangeWillInvalidateApiKeyCaches() throws Exce private Tuple createApiKeyAndAuthenticateWithIt() throws IOException { Client client = client().filterWithHeader( - Collections.singletonMap("Authorization", basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING))); + Collections.singletonMap("Authorization", basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING)) + ); - final CreateApiKeyResponse createApiKeyResponse = new CreateApiKeyRequestBuilder(client) - .setName("test key") + final CreateApiKeyResponse createApiKeyResponse = new CreateApiKeyRequestBuilder(client).setName("test key") .setMetadata(ApiKeyTests.randomMetadata()) .get(); final String docId = createApiKeyResponse.getId(); - final String base64ApiKeyKeyValue = Base64.getEncoder().encodeToString( - (docId + ":" + createApiKeyResponse.getKey().toString()).getBytes(StandardCharsets.UTF_8)); + final String base64ApiKeyKeyValue = Base64.getEncoder() + .encodeToString((docId + ":" + createApiKeyResponse.getKey().toString()).getBytes(StandardCharsets.UTF_8)); AuthenticateResponse authResponse = new TestRestHighLevelClient().security() - .authenticate(RequestOptions.DEFAULT.toBuilder().addHeader("Authorization", - "ApiKey " + base64ApiKeyKeyValue).build()); + .authenticate(RequestOptions.DEFAULT.toBuilder().addHeader("Authorization", "ApiKey " + base64ApiKeyKeyValue).build()); assertEquals("api_key", authResponse.getAuthenticationType()); return Tuple.tuple(docId, createApiKeyResponse.getKey().toString()); } private void assertApiKeyNotCreated(Client client, String keyName) throws ExecutionException, InterruptedException { new RefreshRequestBuilder(client, RefreshAction.INSTANCE).setIndices(SECURITY_MAIN_ALIAS).execute().get(); - assertEquals(0, client.execute(GetApiKeyAction.INSTANCE, - GetApiKeyRequest.usingApiKeyName(keyName, false)).get().getApiKeyInfos().length); + assertEquals( + 0, + client.execute(GetApiKeyAction.INSTANCE, GetApiKeyRequest.usingApiKeyName(keyName, false)).get().getApiKeyInfos().length + ); } - private void verifyGetResponse(int expectedNumberOfApiKeys, List responses, - List> metadatas, - GetApiKeyResponse response, Set validApiKeyIds, List invalidatedApiKeyIds) { - verifyGetResponse(TEST_SUPERUSER, expectedNumberOfApiKeys, responses, metadatas, response, validApiKeyIds, - invalidatedApiKeyIds); + private void verifyGetResponse( + int expectedNumberOfApiKeys, + List responses, + List> metadatas, + GetApiKeyResponse response, + Set validApiKeyIds, + List invalidatedApiKeyIds + ) { + verifyGetResponse(TEST_SUPERUSER, expectedNumberOfApiKeys, responses, metadatas, response, validApiKeyIds, invalidatedApiKeyIds); } - private void verifyGetResponse(String user, int expectedNumberOfApiKeys, List responses, - List> metadatas, - GetApiKeyResponse response, Set validApiKeyIds, List invalidatedApiKeyIds) { + private void verifyGetResponse( + String user, + int expectedNumberOfApiKeys, + List responses, + List> metadatas, + GetApiKeyResponse response, + Set validApiKeyIds, + List invalidatedApiKeyIds + ) { verifyGetResponse( - new String[]{user}, expectedNumberOfApiKeys, responses, metadatas, response, validApiKeyIds, invalidatedApiKeyIds); + new String[] { user }, + expectedNumberOfApiKeys, + responses, + metadatas, + response, + validApiKeyIds, + invalidatedApiKeyIds + ); } - private void verifyGetResponse(String[] user, int expectedNumberOfApiKeys, List responses, - List> metadatas, - GetApiKeyResponse response, Set validApiKeyIds, List invalidatedApiKeyIds) { + private void verifyGetResponse( + String[] user, + int expectedNumberOfApiKeys, + List responses, + List> metadatas, + GetApiKeyResponse response, + Set validApiKeyIds, + List invalidatedApiKeyIds + ) { assertThat(response.getApiKeyInfos().length, equalTo(expectedNumberOfApiKeys)); - List expectedIds = responses.stream().filter(o -> validApiKeyIds.contains(o.getId())).map(o -> o.getId()) + List expectedIds = responses.stream() + .filter(o -> validApiKeyIds.contains(o.getId())) + .map(o -> o.getId()) .collect(Collectors.toList()); - List actualIds = Arrays.stream(response.getApiKeyInfos()).filter(o -> o.isInvalidated() == false).map(o -> o.getId()) + List actualIds = Arrays.stream(response.getApiKeyInfos()) + .filter(o -> o.isInvalidated() == false) + .map(o -> o.getId()) .collect(Collectors.toList()); assertThat(actualIds, containsInAnyOrder(expectedIds.toArray(Strings.EMPTY_ARRAY))); - List expectedNames = responses.stream().filter(o -> validApiKeyIds.contains(o.getId())).map(o -> o.getName()) + List expectedNames = responses.stream() + .filter(o -> validApiKeyIds.contains(o.getId())) + .map(o -> o.getName()) .collect(Collectors.toList()); - List actualNames = Arrays.stream(response.getApiKeyInfos()).filter(o -> o.isInvalidated() == false).map(o -> o.getName()) + List actualNames = Arrays.stream(response.getApiKeyInfos()) + .filter(o -> o.isInvalidated() == false) + .map(o -> o.getName()) .collect(Collectors.toList()); assertThat(actualNames, containsInAnyOrder(expectedNames.toArray(Strings.EMPTY_ARRAY))); - Set expectedUsernames = (validApiKeyIds.isEmpty()) ? Collections.emptySet() - : Set.of(user); - Set actualUsernames = Arrays.stream(response.getApiKeyInfos()).filter(o -> o.isInvalidated() == false) - .map(o -> o.getUsername()).collect(Collectors.toSet()); + Set expectedUsernames = (validApiKeyIds.isEmpty()) ? Collections.emptySet() : Set.of(user); + Set actualUsernames = Arrays.stream(response.getApiKeyInfos()) + .filter(o -> o.isInvalidated() == false) + .map(o -> o.getUsername()) + .collect(Collectors.toSet()); assertThat(actualUsernames, containsInAnyOrder(expectedUsernames.toArray(Strings.EMPTY_ARRAY))); if (invalidatedApiKeyIds != null) { - List actualInvalidatedApiKeyIds = Arrays.stream(response.getApiKeyInfos()).filter(o -> o.isInvalidated()) - .map(o -> o.getId()).collect(Collectors.toList()); + List actualInvalidatedApiKeyIds = Arrays.stream(response.getApiKeyInfos()) + .filter(o -> o.isInvalidated()) + .map(o -> o.getId()) + .collect(Collectors.toList()); assertThat(invalidatedApiKeyIds, containsInAnyOrder(actualInvalidatedApiKeyIds.toArray(Strings.EMPTY_ARRAY))); } if (metadatas != null) { - final HashMap> idToMetadata = IntStream.range(0, responses.size()).collect( - (Supplier>>) HashMap::new, - (m, i) -> m.put(responses.get(i).getId(), metadatas.get(i)), - HashMap::putAll); + final HashMap> idToMetadata = IntStream.range(0, responses.size()) + .collect( + (Supplier>>) HashMap::new, + (m, i) -> m.put(responses.get(i).getId(), metadatas.get(i)), + HashMap::putAll + ); for (ApiKey apiKey : response.getApiKeyInfos()) { final Map metadata = idToMetadata.get(apiKey.getId()); assertThat(apiKey.getMetadata(), equalTo(metadata == null ? Map.of() : metadata)); @@ -1222,27 +1469,50 @@ private Tuple, List>> createApiKe } private Tuple, List>> createApiKeys( - String user, int noOfApiKeys, TimeValue expiration, String... clusterPrivileges) { - final Map headers = Collections.singletonMap("Authorization", - basicAuthHeaderValue(user, TEST_PASSWORD_SECURE_STRING)); + String user, + int noOfApiKeys, + TimeValue expiration, + String... clusterPrivileges + ) { + final Map headers = Collections.singletonMap( + "Authorization", + basicAuthHeaderValue(user, TEST_PASSWORD_SECURE_STRING) + ); return createApiKeys(headers, noOfApiKeys, expiration, clusterPrivileges); } private Tuple, List>> createApiKeys( - String owningUser, String authenticatingUser, int noOfApiKeys, TimeValue expiration, String... clusterPrivileges) { + String owningUser, + String authenticatingUser, + int noOfApiKeys, + TimeValue expiration, + String... clusterPrivileges + ) { final Map headers = Map.of( - "Authorization", basicAuthHeaderValue(authenticatingUser, TEST_PASSWORD_SECURE_STRING), - "es-security-runas-user", owningUser); + "Authorization", + basicAuthHeaderValue(authenticatingUser, TEST_PASSWORD_SECURE_STRING), + "es-security-runas-user", + owningUser + ); return createApiKeys(headers, noOfApiKeys, expiration, clusterPrivileges); } private Tuple, List>> createApiKeys( - Map headers, int noOfApiKeys, TimeValue expiration, String... clusterPrivileges) { + Map headers, + int noOfApiKeys, + TimeValue expiration, + String... clusterPrivileges + ) { return createApiKeys(headers, noOfApiKeys, "test-key-", expiration, clusterPrivileges); } private Tuple, List>> createApiKeys( - Map headers, int noOfApiKeys, String namePrefix, TimeValue expiration, String... clusterPrivileges) { + Map headers, + int noOfApiKeys, + String namePrefix, + TimeValue expiration, + String... clusterPrivileges + ) { List> metadatas = new ArrayList<>(noOfApiKeys); List responses = new ArrayList<>(); for (int i = 0; i < noOfApiKeys; i++) { @@ -1250,10 +1520,9 @@ private Tuple, List>> createApiKe Client client = client().filterWithHeader(headers); final Map metadata = ApiKeyTests.randomMetadata(); metadatas.add(metadata); - final CreateApiKeyResponse response = new CreateApiKeyRequestBuilder(client) - .setName(namePrefix + randomAlphaOfLengthBetween(5, 9) + i).setExpiration(expiration) - .setRoleDescriptors(Collections.singletonList(descriptor)) - .setMetadata(metadata).get(); + final CreateApiKeyResponse response = new CreateApiKeyRequestBuilder(client).setName( + namePrefix + randomAlphaOfLengthBetween(5, 9) + i + ).setExpiration(expiration).setRoleDescriptors(Collections.singletonList(descriptor)).setMetadata(metadata).get(); assertNotNull(response.getId()); assertNotNull(response.getKey()); responses.add(response); @@ -1274,28 +1543,37 @@ private void createUserWithRunAsRole() throws ExecutionException, InterruptedExc putUserRequest.passwordHash(SecuritySettingsSource.TEST_PASSWORD_HASHED.toCharArray()); PlainActionFuture listener = new PlainActionFuture<>(); final Client client = client().filterWithHeader( - Map.of("Authorization", basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING))); + Map.of("Authorization", basicAuthHeaderValue(TEST_SUPERUSER, TEST_PASSWORD_SECURE_STRING)) + ); client.execute(PutUserAction.INSTANCE, putUserRequest, listener); final PutUserResponse putUserResponse = listener.get(); assertTrue(putUserResponse.created()); } private Client getClientForRunAsUser() { - return client().filterWithHeader(Map.of( - "Authorization", basicAuthHeaderValue("user_with_run_as_role", TEST_PASSWORD_SECURE_STRING), - "es-security-runas-user", "user_with_manage_own_api_key_role")); + return client().filterWithHeader( + Map.of( + "Authorization", + basicAuthHeaderValue("user_with_run_as_role", TEST_PASSWORD_SECURE_STRING), + "es-security-runas-user", + "user_with_manage_own_api_key_role" + ) + ); } private void assertErrorMessage(final ElasticsearchSecurityException ese, String action, String userName, String apiKeyId) { - assertThat(ese, throwableWithMessage( - containsString("action [" + action + "] is unauthorized for API key id [" + apiKeyId + "] of user [" + userName + "]"))); + assertThat( + ese, + throwableWithMessage( + containsString("action [" + action + "] is unauthorized for API key id [" + apiKeyId + "] of user [" + userName + "]") + ) + ); assertThat(ese, throwableWithMessage(containsString(", this action is granted by the cluster privileges ["))); assertThat(ese, throwableWithMessage(containsString("manage_api_key,manage_security,all]"))); } private void assertErrorMessage(final ElasticsearchSecurityException ese, String action, String userName) { - assertThat(ese, throwableWithMessage( - containsString("action [" + action + "] is unauthorized for user [" + userName + "]"))); + assertThat(ese, throwableWithMessage(containsString("action [" + action + "] is unauthorized for user [" + userName + "]"))); assertThat(ese, throwableWithMessage(containsString(", this action is granted by the cluster privileges ["))); assertThat(ese, throwableWithMessage(containsString("manage_api_key,manage_security,all]"))); } diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/RunAsIntegTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/RunAsIntegTests.java index 6deda3308c7f7..80caac28faad8 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/RunAsIntegTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/RunAsIntegTests.java @@ -22,9 +22,7 @@ public class RunAsIntegTests extends SecurityIntegTestCase { private static final String RUN_AS_USER = "run_as_user"; private static final String CLIENT_USER = "transport_user"; - private static final String ROLES = - "run_as_role:\n" + - " run_as: [ '" + SecuritySettingsSource.TEST_USER_NAME + "', 'idontexist' ]\n"; + private static final String ROLES = "run_as_role:\n" + " run_as: [ '" + SecuritySettingsSource.TEST_USER_NAME + "', 'idontexist' ]\n"; // indicates whether the RUN_AS_USER that is being authenticated is also a superuser private static boolean runAsHasSuperUserRole; @@ -47,18 +45,21 @@ public String configRoles() { @Override public String configUsers() { return super.configUsers() - + RUN_AS_USER + ":" + SecuritySettingsSource.TEST_PASSWORD_HASHED + "\n" - + CLIENT_USER + ":" + SecuritySettingsSource.TEST_PASSWORD_HASHED + "\n"; + + RUN_AS_USER + + ":" + + SecuritySettingsSource.TEST_PASSWORD_HASHED + + "\n" + + CLIENT_USER + + ":" + + SecuritySettingsSource.TEST_PASSWORD_HASHED + + "\n"; } @Override public String configUsersRoles() { - String roles = super.configUsersRoles() - + "run_as_role:" + RUN_AS_USER + "\n" - + "transport_client:" + CLIENT_USER; + String roles = super.configUsersRoles() + "run_as_role:" + RUN_AS_USER + "\n" + "transport_client:" + CLIENT_USER; if (runAsHasSuperUserRole) { - roles = roles + "\n" - + "superuser:" + RUN_AS_USER; + roles = roles + "\n" + "superuser:" + RUN_AS_USER; } return roles; } @@ -73,19 +74,18 @@ public void testUserImpersonationUsingHttp() throws Exception { try { Request request = new Request("GET", "/_nodes"); RequestOptions.Builder options = request.getOptions().toBuilder(); - options.addHeader("Authorization", - UsernamePasswordToken.basicAuthHeaderValue(CLIENT_USER, TEST_PASSWORD_SECURE_STRING)); + options.addHeader("Authorization", UsernamePasswordToken.basicAuthHeaderValue(CLIENT_USER, TEST_PASSWORD_SECURE_STRING)); options.addHeader(AuthenticationServiceField.RUN_AS_USER_HEADER, SecuritySettingsSource.TEST_USER_NAME); request.setOptions(options); getRestClient().performRequest(request); fail("request should have failed"); - } catch(ResponseException e) { + } catch (ResponseException e) { assertThat(e.getResponse().getStatusLine().getStatusCode(), is(403)); } if (runAsHasSuperUserRole == false) { try { - //the run as user shouldn't have access to the nodes api + // the run as user shouldn't have access to the nodes api Request request = new Request("GET", "/_nodes"); RequestOptions.Builder options = request.getOptions().toBuilder(); options.addHeader("Authorization", UsernamePasswordToken.basicAuthHeaderValue(RUN_AS_USER, TEST_PASSWORD_SECURE_STRING)); @@ -105,7 +105,7 @@ public void testEmptyHeaderUsingHttp() throws Exception { try { getRestClient().performRequest(requestForUserRunAsUser("")); fail("request should have failed"); - } catch(ResponseException e) { + } catch (ResponseException e) { assertThat(e.getResponse().getStatusLine().getStatusCode(), is(401)); } } diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/SecurityRealmSettingsTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/SecurityRealmSettingsTests.java index fbbf9c0a6f969..0f160a2a42da3 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/SecurityRealmSettingsTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/SecurityRealmSettingsTests.java @@ -55,8 +55,7 @@ protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { OpenIdConnectTestCase.writeJwkSetToFile(jwkSet); final Settings existingSettings = super.nodeSettings(nodeOrdinal, otherSettings); - MockSecureSettings mockSecureSettings = - (MockSecureSettings) Settings.builder().put(existingSettings).getSecureSettings(); + MockSecureSettings mockSecureSettings = (MockSecureSettings) Settings.builder().put(existingSettings).getSecureSettings(); mockSecureSettings.setString("xpack.security.authc.realms.oidc.oidc1.rp.client_secret", randomAlphaOfLength(12)); settings = Settings.builder() .put(existingSettings.filter(s -> s.startsWith("xpack.security.authc.realms.") == false), false) @@ -98,8 +97,11 @@ protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { .stream() .map(RealmConfig.RealmIdentifier::getType) .collect(Collectors.toSet()); - assertThat("One or more realm type are not configured " + configuredRealmTypes, - configuredRealmTypes, Matchers.containsInAnyOrder(InternalRealms.getConfigurableRealmsTypes().toArray(Strings.EMPTY_ARRAY))); + assertThat( + "One or more realm type are not configured " + configuredRealmTypes, + configuredRealmTypes, + Matchers.containsInAnyOrder(InternalRealms.getConfigurableRealmsTypes().toArray(Strings.EMPTY_ARRAY)) + ); return settings; } diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/TokenAuthIntegTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/TokenAuthIntegTests.java index 3a507cd175023..951f2addbfd5c 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/TokenAuthIntegTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/TokenAuthIntegTests.java @@ -66,12 +66,12 @@ public class TokenAuthIntegTests extends SecurityIntegTestCase { @Override public Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { return Settings.builder() - .put(super.nodeSettings(nodeOrdinal, otherSettings)) - // crank up the deletion interval and set timeout for delete requests - .put(TokenService.DELETE_INTERVAL.getKey(), TimeValue.timeValueMillis(200L)) - .put(TokenService.DELETE_TIMEOUT.getKey(), TimeValue.timeValueSeconds(5L)) - .put(XPackSettings.TOKEN_SERVICE_ENABLED_SETTING.getKey(), true) - .build(); + .put(super.nodeSettings(nodeOrdinal, otherSettings)) + // crank up the deletion interval and set timeout for delete requests + .put(TokenService.DELETE_INTERVAL.getKey(), TimeValue.timeValueMillis(200L)) + .put(TokenService.DELETE_TIMEOUT.getKey(), TimeValue.timeValueSeconds(5L)) + .put(XPackSettings.TOKEN_SERVICE_ENABLED_SETTING.getKey(), true) + .build(); } @Override @@ -87,8 +87,14 @@ protected boolean addMockHttpTransport() { public void testTokenServiceBootstrapOnNodeJoin() throws Exception { final RestHighLevelClient restClient = new TestRestHighLevelClient(); - CreateTokenResponse response = restClient.security().createToken(CreateTokenRequest.passwordGrant( - SecuritySettingsSource.TEST_USER_NAME, SecuritySettingsSourceField.TEST_PASSWORD.toCharArray()), SECURITY_REQUEST_OPTIONS); + CreateTokenResponse response = restClient.security() + .createToken( + CreateTokenRequest.passwordGrant( + SecuritySettingsSource.TEST_USER_NAME, + SecuritySettingsSourceField.TEST_PASSWORD.toCharArray() + ), + SECURITY_REQUEST_OPTIONS + ); assertNotNull(response.getAuthentication()); for (TokenService tokenService : internalCluster().getInstances(TokenService.class)) { PlainActionFuture userTokenFuture = new PlainActionFuture<>(); @@ -109,11 +115,16 @@ public void testTokenServiceBootstrapOnNodeJoin() throws Exception { assertNotNull(userTokenFuture.actionGet()); } - public void testTokenServiceCanRotateKeys() throws Exception { final RestHighLevelClient restClient = new TestRestHighLevelClient(); - CreateTokenResponse response = restClient.security().createToken(CreateTokenRequest.passwordGrant( - SecuritySettingsSource.TEST_USER_NAME, SecuritySettingsSourceField.TEST_PASSWORD.toCharArray()), SECURITY_REQUEST_OPTIONS); + CreateTokenResponse response = restClient.security() + .createToken( + CreateTokenRequest.passwordGrant( + SecuritySettingsSource.TEST_USER_NAME, + SecuritySettingsSourceField.TEST_PASSWORD.toCharArray() + ), + SECURITY_REQUEST_OPTIONS + ); String masterName = internalCluster().getMasterName(); TokenService masterTokenService = internalCluster().getInstance(TokenService.class, masterName); String activeKeyHash = masterTokenService.getActiveKeyHash(); @@ -142,24 +153,31 @@ public void testTokenServiceCanRotateKeys() throws Exception { public void testExpiredTokensDeletedAfterExpiration() throws Exception { final RestHighLevelClient restClient = new TestRestHighLevelClient(); - CreateTokenResponse response = restClient.security().createToken(CreateTokenRequest.passwordGrant( - SecuritySettingsSource.TEST_USER_NAME, SecuritySettingsSourceField.TEST_PASSWORD.toCharArray()), SECURITY_REQUEST_OPTIONS); + CreateTokenResponse response = restClient.security() + .createToken( + CreateTokenRequest.passwordGrant( + SecuritySettingsSource.TEST_USER_NAME, + SecuritySettingsSourceField.TEST_PASSWORD.toCharArray() + ), + SECURITY_REQUEST_OPTIONS + ); final String accessToken = response.getAccessToken(); final String refreshToken = response.getRefreshToken(); Instant created = Instant.now(); - InvalidateTokenResponse invalidateResponse = restClient.security().invalidateToken( - InvalidateTokenRequest.accessToken(accessToken), SECURITY_REQUEST_OPTIONS); + InvalidateTokenResponse invalidateResponse = restClient.security() + .invalidateToken(InvalidateTokenRequest.accessToken(accessToken), SECURITY_REQUEST_OPTIONS); assertThat(invalidateResponse.getInvalidatedTokens(), equalTo(1)); assertThat(invalidateResponse.getPreviouslyInvalidatedTokens(), equalTo(0)); assertThat(invalidateResponse.getErrors(), empty()); AtomicReference docId = new AtomicReference<>(); assertBusy(() -> { - SearchResponse searchResponse = restClient.search(new SearchRequest(RestrictedIndicesNames.SECURITY_TOKENS_ALIAS) - .source(SearchSourceBuilder.searchSource() - .size(1) - .terminateAfter(1) - .query(QueryBuilders.termQuery("doc_type", "token"))), SECURITY_REQUEST_OPTIONS); + SearchResponse searchResponse = restClient.search( + new SearchRequest(RestrictedIndicesNames.SECURITY_TOKENS_ALIAS).source( + SearchSourceBuilder.searchSource().size(1).terminateAfter(1).query(QueryBuilders.termQuery("doc_type", "token")) + ), + SECURITY_REQUEST_OPTIONS + ); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); docId.set(searchResponse.getHits().getAt(0).getId()); }); @@ -167,31 +185,35 @@ public void testExpiredTokensDeletedAfterExpiration() throws Exception { // hack doc to modify the creation time to the day before Instant yesterday = created.minus(36L, ChronoUnit.HOURS); assertTrue(Instant.now().isAfter(yesterday)); - restClient.update(new UpdateRequest(RestrictedIndicesNames.SECURITY_TOKENS_ALIAS, docId.get()) - .doc("creation_time", yesterday.toEpochMilli()) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), SECURITY_REQUEST_OPTIONS); + restClient.update( + new UpdateRequest(RestrictedIndicesNames.SECURITY_TOKENS_ALIAS, docId.get()).doc("creation_time", yesterday.toEpochMilli()) + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), + SECURITY_REQUEST_OPTIONS + ); AtomicBoolean deleteTriggered = new AtomicBoolean(false); assertBusy(() -> { if (deleteTriggered.compareAndSet(false, true)) { // invalidate a invalid token... doesn't matter that it is bad... we just want this action to trigger the deletion InvalidateTokenResponse invalidateResponseTwo = restClient.security() - .invalidateToken(InvalidateTokenRequest.accessToken("fooobar"), - SECURITY_REQUEST_OPTIONS); + .invalidateToken(InvalidateTokenRequest.accessToken("fooobar"), SECURITY_REQUEST_OPTIONS); assertThat(invalidateResponseTwo.getInvalidatedTokens(), equalTo(0)); assertThat(invalidateResponseTwo.getPreviouslyInvalidatedTokens(), equalTo(0)); assertThat(invalidateResponseTwo.getErrors(), empty()); } restClient.indices().refresh(new RefreshRequest(RestrictedIndicesNames.SECURITY_TOKENS_ALIAS), SECURITY_REQUEST_OPTIONS); - SearchResponse searchResponse = restClient.search(new SearchRequest(RestrictedIndicesNames.SECURITY_TOKENS_ALIAS) - .source(SearchSourceBuilder.searchSource() - .query(QueryBuilders.termQuery("doc_type", "token")).terminateAfter(1)), SECURITY_REQUEST_OPTIONS); + SearchResponse searchResponse = restClient.search( + new SearchRequest(RestrictedIndicesNames.SECURITY_TOKENS_ALIAS).source( + SearchSourceBuilder.searchSource().query(QueryBuilders.termQuery("doc_type", "token")).terminateAfter(1) + ), + SECURITY_REQUEST_OPTIONS + ); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(0L)); }, 30, TimeUnit.SECONDS); // Now the documents are deleted, try to invalidate the access token and refresh token again - InvalidateTokenResponse invalidateAccessTokenResponse = restClient.security().invalidateToken( - InvalidateTokenRequest.accessToken(accessToken), SECURITY_REQUEST_OPTIONS); + InvalidateTokenResponse invalidateAccessTokenResponse = restClient.security() + .invalidateToken(InvalidateTokenRequest.accessToken(accessToken), SECURITY_REQUEST_OPTIONS); assertThat(invalidateAccessTokenResponse.getInvalidatedTokens(), equalTo(0)); assertThat(invalidateAccessTokenResponse.getPreviouslyInvalidatedTokens(), equalTo(0)); assertThat(invalidateAccessTokenResponse.getErrors(), empty()); @@ -204,8 +226,8 @@ public void testExpiredTokensDeletedAfterExpiration() throws Exception { // anything (concurrency controls must prevent that), nor may return any errors, // but it might _temporarily_ find an "already deleted" token. final InvalidateTokenRequest invalidateRefreshTokenRequest = InvalidateTokenRequest.refreshToken(refreshToken); - InvalidateTokenResponse invalidateRefreshTokenResponse = restClient.security().invalidateToken( - invalidateRefreshTokenRequest, SECURITY_REQUEST_OPTIONS); + InvalidateTokenResponse invalidateRefreshTokenResponse = restClient.security() + .invalidateToken(invalidateRefreshTokenRequest, SECURITY_REQUEST_OPTIONS); assertThat(invalidateRefreshTokenResponse.getInvalidatedTokens(), equalTo(0)); assertThat(invalidateRefreshTokenResponse.getPreviouslyInvalidatedTokens(), equalTo(0)); @@ -220,8 +242,14 @@ public void testExpiredTokensDeletedAfterExpiration() throws Exception { public void testAccessTokenAndRefreshTokenCanBeInvalidatedIndependently() throws IOException { final RestHighLevelClient restClient = new TestRestHighLevelClient(); - CreateTokenResponse response = restClient.security().createToken(CreateTokenRequest.passwordGrant( - SecuritySettingsSource.TEST_USER_NAME, SecuritySettingsSourceField.TEST_PASSWORD.toCharArray()), SECURITY_REQUEST_OPTIONS); + CreateTokenResponse response = restClient.security() + .createToken( + CreateTokenRequest.passwordGrant( + SecuritySettingsSource.TEST_USER_NAME, + SecuritySettingsSourceField.TEST_PASSWORD.toCharArray() + ), + SECURITY_REQUEST_OPTIONS + ); final InvalidateTokenRequest invalidateRequest1, invalidateRequest2; if (randomBoolean()) { invalidateRequest1 = InvalidateTokenRequest.accessToken(response.getAccessToken()); @@ -231,14 +259,12 @@ public void testAccessTokenAndRefreshTokenCanBeInvalidatedIndependently() throws invalidateRequest2 = InvalidateTokenRequest.accessToken(response.getAccessToken()); } - final InvalidateTokenResponse response1 = - restClient.security().invalidateToken(invalidateRequest1, SECURITY_REQUEST_OPTIONS); + final InvalidateTokenResponse response1 = restClient.security().invalidateToken(invalidateRequest1, SECURITY_REQUEST_OPTIONS); assertThat(response1.getInvalidatedTokens(), equalTo(1)); assertThat(response1.getPreviouslyInvalidatedTokens(), equalTo(0)); assertThat(response1.getErrors(), empty()); - final InvalidateTokenResponse response2 = - restClient.security().invalidateToken(invalidateRequest2, SECURITY_REQUEST_OPTIONS); + final InvalidateTokenResponse response2 = restClient.security().invalidateToken(invalidateRequest2, SECURITY_REQUEST_OPTIONS); assertThat(response2.getInvalidatedTokens(), equalTo(1)); assertThat(response2.getPreviouslyInvalidatedTokens(), equalTo(0)); assertThat(response2.getErrors(), empty()); @@ -248,14 +274,28 @@ public void testInvalidateAllTokensForUser() throws Exception { final RestHighLevelClient restClient = new TestRestHighLevelClient(); final int numOfRequests = randomIntBetween(5, 10); for (int i = 0; i < numOfRequests; i++) { - restClient.security().createToken(CreateTokenRequest.passwordGrant(SecuritySettingsSource.TEST_USER_NAME, - SecuritySettingsSourceField.TEST_PASSWORD.toCharArray()), SECURITY_REQUEST_OPTIONS); + restClient.security() + .createToken( + CreateTokenRequest.passwordGrant( + SecuritySettingsSource.TEST_USER_NAME, + SecuritySettingsSourceField.TEST_PASSWORD.toCharArray() + ), + SECURITY_REQUEST_OPTIONS + ); } - InvalidateTokenResponse invalidateResponse = restClient.security().invalidateToken( - InvalidateTokenRequest.userTokens(SecuritySettingsSource.TEST_USER_NAME), - RequestOptions.DEFAULT.toBuilder().addHeader("Authorization", - UsernamePasswordToken.basicAuthHeaderValue(SecuritySettingsSource.TEST_SUPERUSER, - SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)).build()); + InvalidateTokenResponse invalidateResponse = restClient.security() + .invalidateToken( + InvalidateTokenRequest.userTokens(SecuritySettingsSource.TEST_USER_NAME), + RequestOptions.DEFAULT.toBuilder() + .addHeader( + "Authorization", + UsernamePasswordToken.basicAuthHeaderValue( + SecuritySettingsSource.TEST_SUPERUSER, + SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING + ) + ) + .build() + ); assertThat(invalidateResponse.getInvalidatedTokens(), equalTo(2 * (numOfRequests))); assertThat(invalidateResponse.getPreviouslyInvalidatedTokens(), equalTo(0)); assertThat(invalidateResponse.getErrors(), empty()); @@ -265,14 +305,28 @@ public void testInvalidateAllTokensForRealm() throws Exception { final RestHighLevelClient restClient = new TestRestHighLevelClient(); final int numOfRequests = randomIntBetween(5, 10); for (int i = 0; i < numOfRequests; i++) { - restClient.security().createToken(CreateTokenRequest.passwordGrant(SecuritySettingsSource.TEST_USER_NAME, - SecuritySettingsSourceField.TEST_PASSWORD.toCharArray()), SECURITY_REQUEST_OPTIONS); + restClient.security() + .createToken( + CreateTokenRequest.passwordGrant( + SecuritySettingsSource.TEST_USER_NAME, + SecuritySettingsSourceField.TEST_PASSWORD.toCharArray() + ), + SECURITY_REQUEST_OPTIONS + ); } - InvalidateTokenResponse invalidateResponse = restClient.security().invalidateToken( - InvalidateTokenRequest.realmTokens("file"), - RequestOptions.DEFAULT.toBuilder().addHeader("Authorization", - UsernamePasswordToken.basicAuthHeaderValue(SecuritySettingsSource.TEST_SUPERUSER, - SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)).build()); + InvalidateTokenResponse invalidateResponse = restClient.security() + .invalidateToken( + InvalidateTokenRequest.realmTokens("file"), + RequestOptions.DEFAULT.toBuilder() + .addHeader( + "Authorization", + UsernamePasswordToken.basicAuthHeaderValue( + SecuritySettingsSource.TEST_SUPERUSER, + SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING + ) + ) + .build() + ); assertThat(invalidateResponse.getInvalidatedTokens(), equalTo(2 * (numOfRequests))); assertThat(invalidateResponse.getPreviouslyInvalidatedTokens(), equalTo(0)); assertThat(invalidateResponse.getErrors(), empty()); @@ -282,14 +336,28 @@ public void testInvalidateAllTokensForRealmThatHasNone() throws IOException { final RestHighLevelClient restClient = new TestRestHighLevelClient(); final int numOfRequests = randomIntBetween(2, 4); for (int i = 0; i < numOfRequests; i++) { - restClient.security().createToken(CreateTokenRequest.passwordGrant(SecuritySettingsSource.TEST_USER_NAME, - SecuritySettingsSourceField.TEST_PASSWORD.toCharArray()), SECURITY_REQUEST_OPTIONS); + restClient.security() + .createToken( + CreateTokenRequest.passwordGrant( + SecuritySettingsSource.TEST_USER_NAME, + SecuritySettingsSourceField.TEST_PASSWORD.toCharArray() + ), + SECURITY_REQUEST_OPTIONS + ); } - InvalidateTokenResponse invalidateResponse = restClient.security().invalidateToken( - InvalidateTokenRequest.realmTokens("saml"), - RequestOptions.DEFAULT.toBuilder().addHeader("Authorization", - UsernamePasswordToken.basicAuthHeaderValue(SecuritySettingsSource.TEST_SUPERUSER, - SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)).build()); + InvalidateTokenResponse invalidateResponse = restClient.security() + .invalidateToken( + InvalidateTokenRequest.realmTokens("saml"), + RequestOptions.DEFAULT.toBuilder() + .addHeader( + "Authorization", + UsernamePasswordToken.basicAuthHeaderValue( + SecuritySettingsSource.TEST_SUPERUSER, + SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING + ) + ) + .build() + ); assertThat(invalidateResponse.getInvalidatedTokens(), equalTo(0)); assertThat(invalidateResponse.getPreviouslyInvalidatedTokens(), equalTo(0)); assertThat(invalidateResponse.getErrors(), empty()); @@ -297,8 +365,14 @@ public void testInvalidateAllTokensForRealmThatHasNone() throws IOException { public void testInvalidateMultipleTimes() throws IOException { final RestHighLevelClient restClient = new TestRestHighLevelClient(); - CreateTokenResponse response = restClient.security().createToken(CreateTokenRequest.passwordGrant( - SecuritySettingsSource.TEST_USER_NAME, SecuritySettingsSourceField.TEST_PASSWORD.toCharArray()), SECURITY_REQUEST_OPTIONS); + CreateTokenResponse response = restClient.security() + .createToken( + CreateTokenRequest.passwordGrant( + SecuritySettingsSource.TEST_USER_NAME, + SecuritySettingsSourceField.TEST_PASSWORD.toCharArray() + ), + SECURITY_REQUEST_OPTIONS + ); InvalidateTokenResponse invalidateResponse = restClient.security() .invalidateToken(InvalidateTokenRequest.accessToken(response.getAccessToken()), SECURITY_REQUEST_OPTIONS); @@ -315,16 +389,26 @@ public void testInvalidateMultipleTimes() throws IOException { public void testInvalidateNotValidAccessTokens() throws Exception { final RestHighLevelClient restClient = new TestRestHighLevelClient(); // Perform a request to invalidate a token, before the tokens index is created - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, () -> restClient.security() - .invalidateToken(InvalidateTokenRequest.accessToken(generateAccessToken(Version.CURRENT)), - SECURITY_REQUEST_OPTIONS)); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> restClient.security() + .invalidateToken(InvalidateTokenRequest.accessToken(generateAccessToken(Version.CURRENT)), SECURITY_REQUEST_OPTIONS) + ); assertThat(e.status(), equalTo(RestStatus.BAD_REQUEST)); // Create a token to trigger index creation - restClient.security().createToken(CreateTokenRequest.passwordGrant( - SecuritySettingsSource.TEST_USER_NAME, SecuritySettingsSourceField.TEST_PASSWORD.toCharArray()), SECURITY_REQUEST_OPTIONS); + restClient.security() + .createToken( + CreateTokenRequest.passwordGrant( + SecuritySettingsSource.TEST_USER_NAME, + SecuritySettingsSourceField.TEST_PASSWORD.toCharArray() + ), + SECURITY_REQUEST_OPTIONS + ); InvalidateTokenResponse invalidateResponse = restClient.security() - .invalidateToken(InvalidateTokenRequest.accessToken("!this_is_not_a_base64_string_and_we_should_fail_decoding_it"), - SECURITY_REQUEST_OPTIONS); + .invalidateToken( + InvalidateTokenRequest.accessToken("!this_is_not_a_base64_string_and_we_should_fail_decoding_it"), + SECURITY_REQUEST_OPTIONS + ); assertThat(invalidateResponse.getInvalidatedTokens(), equalTo(0)); assertThat(invalidateResponse.getPreviouslyInvalidatedTokens(), equalTo(0)); assertThat(invalidateResponse.getErrors(), empty()); @@ -336,8 +420,10 @@ public void testInvalidateNotValidAccessTokens() throws Exception { assertThat(invalidateResponse.getErrors(), empty()); invalidateResponse = restClient.security() - .invalidateToken(InvalidateTokenRequest.accessToken(generateInvalidShortAccessToken(Version.CURRENT)), - SECURITY_REQUEST_OPTIONS); + .invalidateToken( + InvalidateTokenRequest.accessToken(generateInvalidShortAccessToken(Version.CURRENT)), + SECURITY_REQUEST_OPTIONS + ); assertThat(invalidateResponse.getInvalidatedTokens(), equalTo(0)); assertThat(invalidateResponse.getPreviouslyInvalidatedTokens(), equalTo(0)); assertThat(invalidateResponse.getErrors(), empty()); @@ -345,8 +431,7 @@ public void testInvalidateNotValidAccessTokens() throws Exception { // Generate a token that could be a valid token string for the version we are on, and should decode fine, but is not found in our // tokens index invalidateResponse = restClient.security() - .invalidateToken(InvalidateTokenRequest.accessToken(generateAccessToken(Version.CURRENT)), - SECURITY_REQUEST_OPTIONS); + .invalidateToken(InvalidateTokenRequest.accessToken(generateAccessToken(Version.CURRENT)), SECURITY_REQUEST_OPTIONS); assertThat(invalidateResponse.getInvalidatedTokens(), equalTo(0)); assertThat(invalidateResponse.getPreviouslyInvalidatedTokens(), equalTo(0)); assertThat(invalidateResponse.getErrors(), empty()); @@ -355,17 +440,31 @@ public void testInvalidateNotValidAccessTokens() throws Exception { public void testInvalidateNotValidRefreshTokens() throws Exception { final RestHighLevelClient restClient = new TestRestHighLevelClient(); // Perform a request to invalidate a refresh token, before the tokens index is created - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, () -> restClient.security() - .invalidateToken(InvalidateTokenRequest.refreshToken( - TokenService.prependVersionAndEncodeRefreshToken(Version.CURRENT, UUIDs.randomBase64UUID())), - SECURITY_REQUEST_OPTIONS)); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> restClient.security() + .invalidateToken( + InvalidateTokenRequest.refreshToken( + TokenService.prependVersionAndEncodeRefreshToken(Version.CURRENT, UUIDs.randomBase64UUID()) + ), + SECURITY_REQUEST_OPTIONS + ) + ); assertThat(e.status(), equalTo(RestStatus.BAD_REQUEST)); // Create a token to trigger index creation - restClient.security().createToken(CreateTokenRequest.passwordGrant( - SecuritySettingsSource.TEST_USER_NAME, SecuritySettingsSourceField.TEST_PASSWORD.toCharArray()), SECURITY_REQUEST_OPTIONS); + restClient.security() + .createToken( + CreateTokenRequest.passwordGrant( + SecuritySettingsSource.TEST_USER_NAME, + SecuritySettingsSourceField.TEST_PASSWORD.toCharArray() + ), + SECURITY_REQUEST_OPTIONS + ); InvalidateTokenResponse invalidateResponse = restClient.security() - .invalidateToken(InvalidateTokenRequest.refreshToken("!this_is_not_a_base64_string_and_we_should_fail_decoding_it"), - SECURITY_REQUEST_OPTIONS); + .invalidateToken( + InvalidateTokenRequest.refreshToken("!this_is_not_a_base64_string_and_we_should_fail_decoding_it"), + SECURITY_REQUEST_OPTIONS + ); assertThat(invalidateResponse.getInvalidatedTokens(), equalTo(0)); assertThat(invalidateResponse.getPreviouslyInvalidatedTokens(), equalTo(0)); assertThat(invalidateResponse.getErrors(), empty()); @@ -377,9 +476,12 @@ public void testInvalidateNotValidRefreshTokens() throws Exception { assertThat(invalidateResponse.getErrors(), empty()); invalidateResponse = restClient.security() - .invalidateToken(InvalidateTokenRequest.refreshToken( - TokenService.prependVersionAndEncodeRefreshToken(Version.CURRENT, randomAlphaOfLength(32))), - SECURITY_REQUEST_OPTIONS); + .invalidateToken( + InvalidateTokenRequest.refreshToken( + TokenService.prependVersionAndEncodeRefreshToken(Version.CURRENT, randomAlphaOfLength(32)) + ), + SECURITY_REQUEST_OPTIONS + ); assertThat(invalidateResponse.getInvalidatedTokens(), equalTo(0)); assertThat(invalidateResponse.getPreviouslyInvalidatedTokens(), equalTo(0)); assertThat(invalidateResponse.getErrors(), empty()); @@ -387,9 +489,12 @@ public void testInvalidateNotValidRefreshTokens() throws Exception { // Generate a token that could be a valid token string for the version we are on, and should decode fine, but is not found in our // tokens index invalidateResponse = restClient.security() - .invalidateToken(InvalidateTokenRequest.refreshToken( - TokenService.prependVersionAndEncodeRefreshToken(Version.CURRENT, UUIDs.randomBase64UUID())), - SECURITY_REQUEST_OPTIONS); + .invalidateToken( + InvalidateTokenRequest.refreshToken( + TokenService.prependVersionAndEncodeRefreshToken(Version.CURRENT, UUIDs.randomBase64UUID()) + ), + SECURITY_REQUEST_OPTIONS + ); assertThat(invalidateResponse.getInvalidatedTokens(), equalTo(0)); assertThat(invalidateResponse.getPreviouslyInvalidatedTokens(), equalTo(0)); assertThat(invalidateResponse.getErrors(), empty()); @@ -397,8 +502,14 @@ public void testInvalidateNotValidRefreshTokens() throws Exception { public void testRefreshingToken() throws IOException { final RestHighLevelClient restClient = new TestRestHighLevelClient(); - CreateTokenResponse response = restClient.security().createToken(CreateTokenRequest.passwordGrant( - SecuritySettingsSource.TEST_USER_NAME, SecuritySettingsSourceField.TEST_PASSWORD.toCharArray()), SECURITY_REQUEST_OPTIONS); + CreateTokenResponse response = restClient.security() + .createToken( + CreateTokenRequest.passwordGrant( + SecuritySettingsSource.TEST_USER_NAME, + SecuritySettingsSourceField.TEST_PASSWORD.toCharArray() + ), + SECURITY_REQUEST_OPTIONS + ); assertNotNull(response.getRefreshToken()); // Assert that we can authenticate with the access token assertAuthenticateWithToken(response.getAccessToken(), SecuritySettingsSource.TEST_USER_NAME); @@ -415,8 +526,14 @@ public void testRefreshingToken() throws IOException { public void testRefreshingInvalidatedToken() throws IOException { final RestHighLevelClient restClient = new TestRestHighLevelClient(); - CreateTokenResponse createTokenResponse = restClient.security().createToken(CreateTokenRequest.passwordGrant( - SecuritySettingsSource.TEST_USER_NAME, SecuritySettingsSourceField.TEST_PASSWORD.toCharArray()), SECURITY_REQUEST_OPTIONS); + CreateTokenResponse createTokenResponse = restClient.security() + .createToken( + CreateTokenRequest.passwordGrant( + SecuritySettingsSource.TEST_USER_NAME, + SecuritySettingsSourceField.TEST_PASSWORD.toCharArray() + ), + SECURITY_REQUEST_OPTIONS + ); assertNotNull(createTokenResponse.getRefreshToken()); InvalidateTokenResponse invalidateResponse = restClient.security() .invalidateToken(InvalidateTokenRequest.refreshToken(createTokenResponse.getRefreshToken()), SECURITY_REQUEST_OPTIONS); @@ -424,9 +541,11 @@ public void testRefreshingInvalidatedToken() throws IOException { assertThat(invalidateResponse.getPreviouslyInvalidatedTokens(), equalTo(0)); assertThat(invalidateResponse.getErrors(), empty()); - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> restClient.security().createToken( - CreateTokenRequest.refreshTokenGrant(createTokenResponse.getRefreshToken()), SECURITY_REQUEST_OPTIONS)); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> restClient.security() + .createToken(CreateTokenRequest.refreshTokenGrant(createTokenResponse.getRefreshToken()), SECURITY_REQUEST_OPTIONS) + ); assertThat(e.getCause().getMessage(), containsString("invalid_grant")); assertEquals(RestStatus.BAD_REQUEST, e.status()); assertThat(e.getCause().getMessage(), containsString("token has been invalidated")); @@ -434,8 +553,14 @@ public void testRefreshingInvalidatedToken() throws IOException { public void testRefreshingMultipleTimesFails() throws Exception { final RestHighLevelClient restClient = new TestRestHighLevelClient(); - CreateTokenResponse createTokenResponse = restClient.security().createToken(CreateTokenRequest.passwordGrant( - SecuritySettingsSource.TEST_USER_NAME, SecuritySettingsSourceField.TEST_PASSWORD.toCharArray()), SECURITY_REQUEST_OPTIONS); + CreateTokenResponse createTokenResponse = restClient.security() + .createToken( + CreateTokenRequest.passwordGrant( + SecuritySettingsSource.TEST_USER_NAME, + SecuritySettingsSourceField.TEST_PASSWORD.toCharArray() + ), + SECURITY_REQUEST_OPTIONS + ); assertNotNull(createTokenResponse.getRefreshToken()); CreateTokenResponse refreshResponse = restClient.security() @@ -444,13 +569,19 @@ public void testRefreshingMultipleTimesFails() throws Exception { // We now have two documents, the original(now refreshed) token doc and the new one with the new access doc AtomicReference docId = new AtomicReference<>(); assertBusy(() -> { - SearchResponse searchResponse = restClient.search(new SearchRequest(RestrictedIndicesNames.SECURITY_TOKENS_ALIAS) - .source(SearchSourceBuilder.searchSource() - .query(QueryBuilders.boolQuery() - .must(QueryBuilders.termQuery("doc_type", TokenService.TOKEN_DOC_TYPE)) - .must(QueryBuilders.termQuery("refresh_token.refreshed", "true"))) - .size(1) - .terminateAfter(1)), SECURITY_REQUEST_OPTIONS); + SearchResponse searchResponse = restClient.search( + new SearchRequest(RestrictedIndicesNames.SECURITY_TOKENS_ALIAS).source( + SearchSourceBuilder.searchSource() + .query( + QueryBuilders.boolQuery() + .must(QueryBuilders.termQuery("doc_type", TokenService.TOKEN_DOC_TYPE)) + .must(QueryBuilders.termQuery("refresh_token.refreshed", "true")) + ) + .size(1) + .terminateAfter(1) + ), + SECURITY_REQUEST_OPTIONS + ); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); docId.set(searchResponse.getHits().getAt(0).getId()); }); @@ -459,17 +590,22 @@ public void testRefreshingMultipleTimesFails() throws Exception { Instant refreshed = Instant.now(); Instant aWhileAgo = refreshed.minus(50L, ChronoUnit.SECONDS); assertTrue(Instant.now().isAfter(aWhileAgo)); - UpdateResponse updateResponse = restClient.update(new UpdateRequest(RestrictedIndicesNames.SECURITY_TOKENS_ALIAS, docId.get()) - .doc("refresh_token", Collections.singletonMap("refresh_time", aWhileAgo.toEpochMilli())) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .fetchSource("refresh_token", Strings.EMPTY_STRING), SECURITY_REQUEST_OPTIONS); + UpdateResponse updateResponse = restClient.update( + new UpdateRequest(RestrictedIndicesNames.SECURITY_TOKENS_ALIAS, docId.get()).doc( + "refresh_token", + Collections.singletonMap("refresh_time", aWhileAgo.toEpochMilli()) + ).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).fetchSource("refresh_token", Strings.EMPTY_STRING), + SECURITY_REQUEST_OPTIONS + ); assertNotNull(updateResponse); @SuppressWarnings("unchecked") Map refreshTokenMap = (Map) updateResponse.getGetResult().sourceAsMap().get("refresh_token"); - assertTrue( - Instant.ofEpochMilli((long) refreshTokenMap.get("refresh_time")).isBefore(Instant.now().minus(30L, ChronoUnit.SECONDS))); - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, () -> restClient.security() - .createToken(CreateTokenRequest.refreshTokenGrant(createTokenResponse.getRefreshToken()), SECURITY_REQUEST_OPTIONS)); + assertTrue(Instant.ofEpochMilli((long) refreshTokenMap.get("refresh_time")).isBefore(Instant.now().minus(30L, ChronoUnit.SECONDS))); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> restClient.security() + .createToken(CreateTokenRequest.refreshTokenGrant(createTokenResponse.getRefreshToken()), SECURITY_REQUEST_OPTIONS) + ); assertThat(e.getCause().getMessage(), containsString("invalid_grant")); assertEquals(RestStatus.BAD_REQUEST, e.status()); assertThat(e.getCause().getMessage(), containsString("token has already been refreshed more than 30 seconds in the past")); @@ -481,8 +617,14 @@ public void testRefreshingMultipleTimesWithinWindowSucceeds() throws Exception { final List tokens = Collections.synchronizedList(new ArrayList<>()); final List authStatuses = Collections.synchronizedList(new ArrayList<>()); final RestHighLevelClient restClient = new TestRestHighLevelClient(); - CreateTokenResponse createTokenResponse = restClient.security().createToken(CreateTokenRequest.passwordGrant( - SecuritySettingsSource.TEST_USER_NAME, SecuritySettingsSourceField.TEST_PASSWORD.toCharArray()), SECURITY_REQUEST_OPTIONS); + CreateTokenResponse createTokenResponse = restClient.security() + .createToken( + CreateTokenRequest.passwordGrant( + SecuritySettingsSource.TEST_USER_NAME, + SecuritySettingsSourceField.TEST_PASSWORD.toCharArray() + ), + SECURITY_REQUEST_OPTIONS + ); assertNotNull(createTokenResponse.getRefreshToken()); final int numberOfProcessors = Runtime.getRuntime().availableProcessors(); final int numberOfThreads = scaledRandomIntBetween((numberOfProcessors + 1) / 2, numberOfProcessors * 3); @@ -508,8 +650,11 @@ public void testRefreshingMultipleTimesWithinWindowSucceeds() throws Exception { .createToken(CreateTokenRequest.refreshTokenGrant(createTokenResponse.getRefreshToken()), SECURITY_REQUEST_OPTIONS); final Instant t2 = clock.instant(); if (t1.plusSeconds(30L).isBefore(t2)) { - logger.warn("Tokens [{}], [{}] were received more than 30 seconds after the request, not checking them", - result.getAccessToken(), result.getRefreshToken()); + logger.warn( + "Tokens [{}], [{}] were received more than 30 seconds after the request, not checking them", + result.getAccessToken(), + result.getRefreshToken() + ); } else { authStatuses.add(getAuthenticationResponseCode(result.getAccessToken())); tokens.add(result.getAccessToken() + result.getRefreshToken()); @@ -547,27 +692,56 @@ public void testRefreshingMultipleTimesWithinWindowSucceeds() throws Exception { public void testRefreshAsDifferentUser() throws IOException { final RestHighLevelClient restClient = new TestRestHighLevelClient(); - CreateTokenResponse createTokenResponse = restClient.security().createToken(CreateTokenRequest.passwordGrant( - SecuritySettingsSource.TEST_USER_NAME, SecuritySettingsSourceField.TEST_PASSWORD.toCharArray()), SECURITY_REQUEST_OPTIONS); + CreateTokenResponse createTokenResponse = restClient.security() + .createToken( + CreateTokenRequest.passwordGrant( + SecuritySettingsSource.TEST_USER_NAME, + SecuritySettingsSourceField.TEST_PASSWORD.toCharArray() + ), + SECURITY_REQUEST_OPTIONS + ); assertNotNull(createTokenResponse.getRefreshToken()); - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> restClient.security().createToken(CreateTokenRequest.refreshTokenGrant(createTokenResponse.getRefreshToken()), - RequestOptions.DEFAULT.toBuilder().addHeader("Authorization", - UsernamePasswordToken.basicAuthHeaderValue(SecuritySettingsSource.TEST_SUPERUSER, - SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)).build())); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> restClient.security() + .createToken( + CreateTokenRequest.refreshTokenGrant(createTokenResponse.getRefreshToken()), + RequestOptions.DEFAULT.toBuilder() + .addHeader( + "Authorization", + UsernamePasswordToken.basicAuthHeaderValue( + SecuritySettingsSource.TEST_SUPERUSER, + SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING + ) + ) + .build() + ) + ); assertThat(e.getCause().getMessage(), containsString("invalid_grant")); assertEquals(RestStatus.BAD_REQUEST, e.status()); assertThat(e.getCause().getMessage(), containsString("tokens must be refreshed by the creating client")); } public void testCreateThenRefreshAsDifferentUser() throws IOException { - final RequestOptions superuserOptions = RequestOptions.DEFAULT.toBuilder().addHeader("Authorization", - UsernamePasswordToken.basicAuthHeaderValue(SecuritySettingsSource.TEST_SUPERUSER, - SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)).build(); + final RequestOptions superuserOptions = RequestOptions.DEFAULT.toBuilder() + .addHeader( + "Authorization", + UsernamePasswordToken.basicAuthHeaderValue( + SecuritySettingsSource.TEST_SUPERUSER, + SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING + ) + ) + .build(); final RestHighLevelClient restClient = new TestRestHighLevelClient(); - CreateTokenResponse createTokenResponse = restClient.security().createToken(CreateTokenRequest.passwordGrant( - SecuritySettingsSource.TEST_USER_NAME, SecuritySettingsSourceField.TEST_PASSWORD.toCharArray()), superuserOptions); + CreateTokenResponse createTokenResponse = restClient.security() + .createToken( + CreateTokenRequest.passwordGrant( + SecuritySettingsSource.TEST_USER_NAME, + SecuritySettingsSourceField.TEST_PASSWORD.toCharArray() + ), + superuserOptions + ); assertNotNull(createTokenResponse.getRefreshToken()); CreateTokenResponse refreshResponse = restClient.security() @@ -585,12 +759,18 @@ public void testCreateThenRefreshAsDifferentUser() throws IOException { } public void testClientCredentialsGrant() throws Exception { - final RequestOptions superuserOptions = RequestOptions.DEFAULT.toBuilder().addHeader("Authorization", - UsernamePasswordToken.basicAuthHeaderValue(SecuritySettingsSource.TEST_SUPERUSER, - SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)).build(); + final RequestOptions superuserOptions = RequestOptions.DEFAULT.toBuilder() + .addHeader( + "Authorization", + UsernamePasswordToken.basicAuthHeaderValue( + SecuritySettingsSource.TEST_SUPERUSER, + SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING + ) + ) + .build(); final RestHighLevelClient restClient = new TestRestHighLevelClient(); - CreateTokenResponse createTokenResponse = - restClient.security().createToken(CreateTokenRequest.clientCredentialsGrant(), superuserOptions); + CreateTokenResponse createTokenResponse = restClient.security() + .createToken(CreateTokenRequest.clientCredentialsGrant(), superuserOptions); assertNull(createTokenResponse.getRefreshToken()); assertAuthenticateWithToken(createTokenResponse.getAccessToken(), SecuritySettingsSource.TEST_SUPERUSER); @@ -602,13 +782,19 @@ public void testClientCredentialsGrant() throws Exception { assertThat(invalidateTokenResponse.getPreviouslyInvalidatedTokens(), equalTo(0)); assertThat(invalidateTokenResponse.getErrors(), empty()); - assertUnauthorizedToken(createTokenResponse.getAccessToken()); + assertUnauthorizedToken(createTokenResponse.getAccessToken()); } public void testAuthenticateWithWrongToken() throws Exception { final RestHighLevelClient restClient = new TestRestHighLevelClient(); - CreateTokenResponse response = restClient.security().createToken(CreateTokenRequest.passwordGrant( - SecuritySettingsSource.TEST_USER_NAME, SecuritySettingsSourceField.TEST_PASSWORD.toCharArray()), SECURITY_REQUEST_OPTIONS); + CreateTokenResponse response = restClient.security() + .createToken( + CreateTokenRequest.passwordGrant( + SecuritySettingsSource.TEST_USER_NAME, + SecuritySettingsSourceField.TEST_PASSWORD.toCharArray() + ), + SECURITY_REQUEST_OPTIONS + ); assertNotNull(response.getRefreshToken()); // Assert that we can authenticate with the access token assertAuthenticateWithToken(response.getAccessToken(), SecuritySettingsSource.TEST_USER_NAME); @@ -653,25 +839,27 @@ private String generateInvalidShortAccessToken(Version version) throws Exception private void assertAuthenticateWithToken(String accessToken, String expectedUser) throws IOException { final RestHighLevelClient restClient = new TestRestHighLevelClient(); - AuthenticateResponse authResponse = restClient.security().authenticate(RequestOptions.DEFAULT.toBuilder().addHeader("Authorization", - "Bearer " + accessToken).build()); + AuthenticateResponse authResponse = restClient.security() + .authenticate(RequestOptions.DEFAULT.toBuilder().addHeader("Authorization", "Bearer " + accessToken).build()); assertThat(authResponse.getUser().getUsername(), equalTo(expectedUser)); assertThat(authResponse.getAuthenticationType(), equalTo("token")); } private void assertUnauthorizedToken(String accessToken) { final RestHighLevelClient restClient = new TestRestHighLevelClient(); - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> restClient.security().authenticate(RequestOptions.DEFAULT.toBuilder().addHeader("Authorization", - "Bearer " + accessToken).build())); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> restClient.security() + .authenticate(RequestOptions.DEFAULT.toBuilder().addHeader("Authorization", "Bearer " + accessToken).build()) + ); assertThat(e.status(), equalTo(RestStatus.UNAUTHORIZED)); } private RestStatus getAuthenticationResponseCode(String accessToken) throws IOException { final RestHighLevelClient restClient = new TestRestHighLevelClient(); try { - restClient.security().authenticate(RequestOptions.DEFAULT.toBuilder().addHeader("Authorization", - "Bearer " + accessToken).build()); + restClient.security() + .authenticate(RequestOptions.DEFAULT.toBuilder().addHeader("Authorization", "Bearer " + accessToken).build()); return RestStatus.OK; } catch (ElasticsearchStatusException esse) { return esse.status(); diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/apikey/ApiKeySingleNodeTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/apikey/ApiKeySingleNodeTests.java index c6f3cfcc51977..9f831d057cf05 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/apikey/ApiKeySingleNodeTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/apikey/ApiKeySingleNodeTests.java @@ -18,11 +18,11 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.test.SecuritySingleNodeTestCase; import org.elasticsearch.test.XContentTestUtils; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.xpack.core.security.action.CreateApiKeyAction; import org.elasticsearch.xpack.core.security.action.CreateApiKeyRequest; @@ -64,20 +64,21 @@ protected Settings nodeSettings() { } public void testQueryWithExpiredKeys() throws InterruptedException { - final String id1 = client().execute(CreateApiKeyAction.INSTANCE, - new CreateApiKeyRequest("expired-shortly", null, TimeValue.timeValueMillis(1), null)) - .actionGet() - .getId(); - final String id2 = client().execute(CreateApiKeyAction.INSTANCE, - new CreateApiKeyRequest("long-lived", null, TimeValue.timeValueDays(1), null)) - .actionGet() - .getId(); + final String id1 = client().execute( + CreateApiKeyAction.INSTANCE, + new CreateApiKeyRequest("expired-shortly", null, TimeValue.timeValueMillis(1), null) + ).actionGet().getId(); + final String id2 = client().execute( + CreateApiKeyAction.INSTANCE, + new CreateApiKeyRequest("long-lived", null, TimeValue.timeValueDays(1), null) + ).actionGet().getId(); Thread.sleep(10); // just to be 100% sure that the 1st key is expired when we search for it final QueryApiKeyRequest queryApiKeyRequest = new QueryApiKeyRequest( QueryBuilders.boolQuery() .filter(QueryBuilders.idsQuery().addIds(id1, id2)) - .filter(QueryBuilders.rangeQuery("expiration").from(Instant.now().toEpochMilli()))); + .filter(QueryBuilders.rangeQuery("expiration").from(Instant.now().toEpochMilli())) + ); final QueryApiKeyResponse queryApiKeyResponse = client().execute(QueryApiKeyAction.INSTANCE, queryApiKeyRequest).actionGet(); assertThat(queryApiKeyResponse.getItems().length, equalTo(1)); assertThat(queryApiKeyResponse.getItems()[0].getApiKey().getId(), equalTo(id2)); @@ -99,23 +100,40 @@ public void testCreatingApiKeyWithNoAccess() { grantApiKeyRequest.getGrant().setUsername(username); grantApiKeyRequest.getGrant().setPassword(password); grantApiKeyRequest.getApiKeyRequest().setName(randomAlphaOfLength(8)); - grantApiKeyRequest.getApiKeyRequest().setRoleDescriptors(List.of( - new RoleDescriptor("x", new String[] { "all" }, - new RoleDescriptor.IndicesPrivileges[]{ - RoleDescriptor.IndicesPrivileges.builder().indices("*").privileges("all").allowRestrictedIndices(true).build() - }, - null, null, null, null, null))); + grantApiKeyRequest.getApiKeyRequest() + .setRoleDescriptors( + List.of( + new RoleDescriptor( + "x", + new String[] { "all" }, + new RoleDescriptor.IndicesPrivileges[] { + RoleDescriptor.IndicesPrivileges.builder() + .indices("*") + .privileges("all") + .allowRestrictedIndices(true) + .build() }, + null, + null, + null, + null, + null + ) + ) + ); final CreateApiKeyResponse createApiKeyResponse = client().execute(GrantApiKeyAction.INSTANCE, grantApiKeyRequest).actionGet(); - final String base64ApiKeyKeyValue = Base64.getEncoder().encodeToString( - (createApiKeyResponse.getId() + ":" + createApiKeyResponse.getKey().toString()).getBytes(StandardCharsets.UTF_8)); + final String base64ApiKeyKeyValue = Base64.getEncoder() + .encodeToString( + (createApiKeyResponse.getId() + ":" + createApiKeyResponse.getKey().toString()).getBytes(StandardCharsets.UTF_8) + ); // No cluster access final ElasticsearchSecurityException e1 = expectThrows( ElasticsearchSecurityException.class, () -> client().filterWithHeader(Map.of("Authorization", "ApiKey " + base64ApiKeyKeyValue)) .execute(MainAction.INSTANCE, new MainRequest()) - .actionGet()); + .actionGet() + ); assertThat(e1.status().getStatus(), equalTo(403)); assertThat(e1.getMessage(), containsString("is unauthorized for API key")); @@ -123,45 +141,52 @@ public void testCreatingApiKeyWithNoAccess() { final ElasticsearchSecurityException e2 = expectThrows( ElasticsearchSecurityException.class, () -> client().filterWithHeader(Map.of("Authorization", "ApiKey " + base64ApiKeyKeyValue)) - .execute(CreateIndexAction.INSTANCE, new CreateIndexRequest( - randomFrom(randomAlphaOfLengthBetween(3, 8), SECURITY_MAIN_ALIAS))) - .actionGet()); + .execute( + CreateIndexAction.INSTANCE, + new CreateIndexRequest(randomFrom(randomAlphaOfLengthBetween(3, 8), SECURITY_MAIN_ALIAS)) + ) + .actionGet() + ); assertThat(e2.status().getStatus(), equalTo(403)); assertThat(e2.getMessage(), containsString("is unauthorized for API key")); } public void testServiceAccountApiKey() throws IOException { - final CreateServiceAccountTokenRequest createServiceAccountTokenRequest = - new CreateServiceAccountTokenRequest("elastic", "fleet-server", randomAlphaOfLength(8)); - final CreateServiceAccountTokenResponse createServiceAccountTokenResponse = - client().execute(CreateServiceAccountTokenAction.INSTANCE, createServiceAccountTokenRequest).actionGet(); - - final CreateApiKeyResponse createApiKeyResponse = - client().filterWithHeader(Map.of("Authorization", "Bearer " + createServiceAccountTokenResponse.getValue())) - .execute(CreateApiKeyAction.INSTANCE, new CreateApiKeyRequest(randomAlphaOfLength(8), null, null)) - .actionGet(); + final CreateServiceAccountTokenRequest createServiceAccountTokenRequest = new CreateServiceAccountTokenRequest( + "elastic", + "fleet-server", + randomAlphaOfLength(8) + ); + final CreateServiceAccountTokenResponse createServiceAccountTokenResponse = client().execute( + CreateServiceAccountTokenAction.INSTANCE, + createServiceAccountTokenRequest + ).actionGet(); + + final CreateApiKeyResponse createApiKeyResponse = client().filterWithHeader( + Map.of("Authorization", "Bearer " + createServiceAccountTokenResponse.getValue()) + ).execute(CreateApiKeyAction.INSTANCE, new CreateApiKeyRequest(randomAlphaOfLength(8), null, null)).actionGet(); final Map apiKeyDocument = getApiKeyDocument(createApiKeyResponse.getId()); @SuppressWarnings("unchecked") - final Map fleetServerRoleDescriptor = - (Map) apiKeyDocument.get("limited_by_role_descriptors"); + final Map fleetServerRoleDescriptor = (Map) apiKeyDocument.get("limited_by_role_descriptors"); assertThat(fleetServerRoleDescriptor.size(), equalTo(1)); assertThat(fleetServerRoleDescriptor, hasKey("elastic/fleet-server")); @SuppressWarnings("unchecked") final Map descriptor = (Map) fleetServerRoleDescriptor.get("elastic/fleet-server"); - final RoleDescriptor roleDescriptor = RoleDescriptor.parse("elastic/fleet-server", + final RoleDescriptor roleDescriptor = RoleDescriptor.parse( + "elastic/fleet-server", XContentTestUtils.convertToXContent(descriptor, XContentType.JSON), false, - XContentType.JSON); + XContentType.JSON + ); assertThat(roleDescriptor, equalTo(ServiceAccountService.getServiceAccounts().get("elastic/fleet-server").roleDescriptor())); } private Map getApiKeyDocument(String apiKeyId) { - final GetResponse getResponse = - client().execute(GetAction.INSTANCE, new GetRequest(".security-7", apiKeyId)).actionGet(); + final GetResponse getResponse = client().execute(GetAction.INSTANCE, new GetRequest(".security-7", apiKeyId)).actionGet(); return getResponse.getSource(); } } diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/esnative/NativeRealmIntegTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/esnative/NativeRealmIntegTests.java index da96074a62720..ebcec34062d4c 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/esnative/NativeRealmIntegTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/esnative/NativeRealmIntegTests.java @@ -109,7 +109,8 @@ public static void init() { @Override public Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { - Settings.Builder builder = Settings.builder().put(super.nodeSettings(nodeOrdinal, otherSettings)) + Settings.Builder builder = Settings.builder() + .put(super.nodeSettings(nodeOrdinal, otherSettings)) .put("xpack.security.authc.password_hashing.algorithm", hasher.name()); if (anonymousEnabled) { builder.put(AnonymousUser.ROLES_SETTING.getKey(), "native_anonymous"); @@ -123,10 +124,9 @@ public void setupAnonymousRoleIfNecessary() throws Exception { if (anonymousEnabled) { if (roleExists) { logger.info("anonymous is enabled. creating [native_anonymous] role"); - PutRoleResponse response = new PutRoleRequestBuilder(client()) - .name("native_anonymous") + PutRoleResponse response = new PutRoleRequestBuilder(client()).name("native_anonymous") .cluster("ALL") - .addIndices(new String[]{"*"}, new String[]{"ALL"}, null, null, null, randomBoolean()) + .addIndices(new String[] { "*" }, new String[] { "ALL" }, null, null, null, randomBoolean()) .get(); assertTrue(response.isCreated()); } else { @@ -163,7 +163,7 @@ public void testAddAndGetUser() throws Exception { assertTrue("user should exist", resp.hasUsers()); User joe = resp.users()[0]; assertEquals("joe", joe.principal()); - assertArrayEquals(joe.roles(), new String[]{"role1", "user"}); + assertArrayEquals(joe.roles(), new String[] { "role1", "user" }); logger.info("--> adding two more users"); preparePutUser("joe2", "s3krit-password2", hasher, "role2", "user").get(); @@ -188,7 +188,7 @@ public void testAddAndGetUser() throws Exception { names.add(u.principal()); } CollectionUtil.timSort(names); - assertArrayEquals(new String[]{"joe", "joe3"}, names.toArray(Strings.EMPTY_ARRAY)); + assertArrayEquals(new String[] { "joe", "joe3" }, names.toArray(Strings.EMPTY_ARRAY)); logger.info("--> deleting user"); DeleteUserResponse delResp = new DeleteUserRequestBuilder(client()).username("joe").get(); @@ -203,13 +203,18 @@ public void testAddAndGetRole() throws Exception { final int existing = existingRoles.size(); final Map metadata = Collections.singletonMap("key", randomAlphaOfLengthBetween(1, 10)); logger.error("--> creating role"); - preparePutRole("test_role") - .cluster("all", "none") - .runAs("root", "nobody") - .addIndices(new String[]{"index"}, new String[]{"read"}, new String[]{"body", "title"}, null, - new BytesArray("{\"match_all\": {}}"), randomBoolean()) - .metadata(metadata) - .get(); + preparePutRole("test_role").cluster("all", "none") + .runAs("root", "nobody") + .addIndices( + new String[] { "index" }, + new String[] { "read" }, + new String[] { "body", "title" }, + null, + new BytesArray("{\"match_all\": {}}"), + randomBoolean() + ) + .metadata(metadata) + .get(); logger.error("--> waiting for .security index"); ensureGreen(SECURITY_MAIN_ALIAS); logger.info("--> retrieving role"); @@ -220,18 +225,28 @@ public void testAddAndGetRole() throws Exception { assertThat(testRole.getMetadata().size(), is(1)); assertThat(testRole.getMetadata().get("key"), is(metadata.get("key"))); - preparePutRole("test_role2") - .cluster("all", "none") - .runAs("root", "nobody") - .addIndices(new String[]{"index"}, new String[]{"read"}, new String[]{"body", "title"}, null, - new BytesArray("{\"match_all\": {}}"), randomBoolean()) - .get(); - preparePutRole("test_role3") - .cluster("all", "none") - .runAs("root", "nobody") - .addIndices(new String[]{"index"}, new String[]{"read"}, new String[]{"body", "title"}, null, - new BytesArray("{\"match_all\": {}}"), randomBoolean()) - .get(); + preparePutRole("test_role2").cluster("all", "none") + .runAs("root", "nobody") + .addIndices( + new String[] { "index" }, + new String[] { "read" }, + new String[] { "body", "title" }, + null, + new BytesArray("{\"match_all\": {}}"), + randomBoolean() + ) + .get(); + preparePutRole("test_role3").cluster("all", "none") + .runAs("root", "nobody") + .addIndices( + new String[] { "index" }, + new String[] { "read" }, + new String[] { "body", "title" }, + null, + new BytesArray("{\"match_all\": {}}"), + randomBoolean() + ) + .get(); logger.info("--> retrieving all roles"); GetRolesResponse allRolesResp = new GetRolesRequestBuilder(client()).get(); @@ -253,11 +268,16 @@ public void testAddAndGetRole() throws Exception { public void testAddUserAndRoleThenAuth() throws Exception { logger.error("--> creating role"); - preparePutRole("test_role") - .cluster("all") - .addIndices(new String[] { "*" }, new String[] { "read" }, new String[]{"body", "title"}, null, - new BytesArray("{\"match_all\": {}}"), randomBoolean()) - .get(); + preparePutRole("test_role").cluster("all") + .addIndices( + new String[] { "*" }, + new String[] { "read" }, + new String[] { "body", "title" }, + null, + new BytesArray("{\"match_all\": {}}"), + randomBoolean() + ) + .get(); logger.error("--> creating user"); preparePutUser("joe", "s3krit-password", hasher, "test_role").get(); logger.error("--> waiting for .security index"); @@ -313,8 +333,7 @@ public void testUpdatingUserAndAuthentication() throws Exception { public void testCreateDeleteAuthenticate() { logger.error("--> creating user"); - preparePutUser("joe", "s3krit-password", hasher, - SecuritySettingsSource.TEST_ROLE).get(); + preparePutUser("joe", "s3krit-password", hasher, SecuritySettingsSource.TEST_ROLE).get(); logger.error("--> waiting for .security index"); ensureGreen(SECURITY_MAIN_ALIAS); logger.info("--> retrieving user"); @@ -345,11 +364,16 @@ public void testCreateDeleteAuthenticate() { public void testCreateAndUpdateRole() { final boolean authenticate = randomBoolean(); logger.error("--> creating role"); - preparePutRole("test_role") - .cluster("all") - .addIndices(new String[]{"*"}, new String[]{"read"}, new String[]{"body", "title"}, null, - new BytesArray("{\"match_all\": {}}"), randomBoolean()) - .get(); + preparePutRole("test_role").cluster("all") + .addIndices( + new String[] { "*" }, + new String[] { "read" }, + new String[] { "body", "title" }, + null, + new BytesArray("{\"match_all\": {}}"), + randomBoolean() + ) + .get(); logger.error("--> creating user"); preparePutUser("joe", "s3krit-password", hasher, "test_role").get(); logger.error("--> waiting for .security index"); @@ -357,20 +381,35 @@ public void testCreateAndUpdateRole() { if (authenticate) { final String token = basicAuthHeaderValue("joe", new SecureString("s3krit-password")); - ClusterHealthResponse response = client().filterWithHeader(Collections.singletonMap("Authorization", token)).admin().cluster() - .prepareHealth().get(); + ClusterHealthResponse response = client().filterWithHeader(Collections.singletonMap("Authorization", token)) + .admin() + .cluster() + .prepareHealth() + .get(); assertFalse(response.isTimedOut()); - preparePutRole("test_role") - .cluster("none") - .addIndices(new String[]{"*"}, new String[]{"read"}, new String[]{"body", "title"}, null, - new BytesArray("{\"match_all\": {}}"), randomBoolean()) - .get(); + preparePutRole("test_role").cluster("none") + .addIndices( + new String[] { "*" }, + new String[] { "read" }, + new String[] { "body", "title" }, + null, + new BytesArray("{\"match_all\": {}}"), + randomBoolean() + ) + .get(); if (anonymousEnabled && roleExists) { - assertNoTimeout(client() - .filterWithHeader(Collections.singletonMap("Authorization", token)).admin().cluster().prepareHealth().get()); + assertNoTimeout( + client().filterWithHeader(Collections.singletonMap("Authorization", token)).admin().cluster().prepareHealth().get() + ); } else { - ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, () -> client() - .filterWithHeader(Collections.singletonMap("Authorization", token)).admin().cluster().prepareHealth().get()); + ElasticsearchSecurityException e = expectThrows( + ElasticsearchSecurityException.class, + () -> client().filterWithHeader(Collections.singletonMap("Authorization", token)) + .admin() + .cluster() + .prepareHealth() + .get() + ); assertThat(e.status(), is(RestStatus.FORBIDDEN)); } } else { @@ -378,59 +417,80 @@ public void testCreateAndUpdateRole() { final Authentication authentication = mock(Authentication.class); GetRolesResponse getRolesResponse = new GetRolesRequestBuilder(client()).names("test_role").get(); assertTrue("test_role does not exist!", getRolesResponse.hasRoles()); - assertTrue("any cluster permission should be authorized", - Role.builder(getRolesResponse.roles()[0], null, Automatons.EMPTY).build() - .cluster() - .check("cluster:admin/foo", request, authentication)); - - preparePutRole("test_role") - .cluster("none") - .addIndices(new String[]{"*"}, new String[]{"read"}, new String[]{"body", "title"}, null, - new BytesArray("{\"match_all\": {}}"), randomBoolean()) - .get(); + assertTrue( + "any cluster permission should be authorized", + Role.builder(getRolesResponse.roles()[0], null, Automatons.EMPTY) + .build() + .cluster() + .check("cluster:admin/foo", request, authentication) + ); + + preparePutRole("test_role").cluster("none") + .addIndices( + new String[] { "*" }, + new String[] { "read" }, + new String[] { "body", "title" }, + null, + new BytesArray("{\"match_all\": {}}"), + randomBoolean() + ) + .get(); getRolesResponse = new GetRolesRequestBuilder(client()).names("test_role").get(); assertTrue("test_role does not exist!", getRolesResponse.hasRoles()); - assertFalse("no cluster permission should be authorized", - Role.builder(getRolesResponse.roles()[0], null, Automatons.EMPTY).build() - .cluster() - .check("cluster:admin/bar", request, authentication)); + assertFalse( + "no cluster permission should be authorized", + Role.builder(getRolesResponse.roles()[0], null, Automatons.EMPTY) + .build() + .cluster() + .check("cluster:admin/bar", request, authentication) + ); } } public void testSnapshotDeleteRestore() { logger.error("--> creating role"); - preparePutRole("test_role") - .cluster("all") - .addIndices(new String[]{"*"}, new String[]{"create_index"}, null, null, null, true) + preparePutRole("test_role").cluster("all") + .addIndices(new String[] { "*" }, new String[] { "create_index" }, null, null, null, true) .get(); logger.error("--> creating user"); preparePutUser("joe", "s3krit-password", hasher, "test_role", "snapshot_user").get(); logger.error("--> waiting for .security index"); ensureGreen(SECURITY_MAIN_ALIAS); logger.info("--> creating repository"); - assertAcked(client().admin().cluster() + assertAcked( + client().admin() + .cluster() .preparePutRepository("test-repo") - .setType("fs").setSettings(Settings.builder() - .put("location", randomRepoPath()) - .put("compress", randomBoolean()) - .put("chunk_size", randomIntBetween(100, 1000), ByteSizeUnit.BYTES))); + .setType("fs") + .setSettings( + Settings.builder() + .put("location", randomRepoPath()) + .put("compress", randomBoolean()) + .put("chunk_size", randomIntBetween(100, 1000), ByteSizeUnit.BYTES) + ) + ); final String token = basicAuthHeaderValue("joe", new SecureString("s3krit-password")); // joe can snapshot all indices, including '.security' - SnapshotInfo snapshotInfo = client().filterWithHeader(Collections.singletonMap("Authorization", token)).admin().cluster() - .prepareCreateSnapshot("test-repo", "test-snap-1") - .setWaitForCompletion(true) - .setIncludeGlobalState(false) - .setIndices(SECURITY_MAIN_ALIAS) - .get().getSnapshotInfo(); + SnapshotInfo snapshotInfo = client().filterWithHeader(Collections.singletonMap("Authorization", token)) + .admin() + .cluster() + .prepareCreateSnapshot("test-repo", "test-snap-1") + .setWaitForCompletion(true) + .setIncludeGlobalState(false) + .setIndices(SECURITY_MAIN_ALIAS) + .get() + .getSnapshotInfo(); assertThat(snapshotInfo.state(), is(SnapshotState.SUCCESS)); assertThat(snapshotInfo.indices(), contains(INTERNAL_SECURITY_MAIN_INDEX_7)); deleteSecurityIndex(); // the realm cache should clear itself but we don't wish to race it new ClearRolesCacheRequestBuilder(client()).get(); // authn fails - final ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, () -> client() - .filterWithHeader(Collections.singletonMap("Authorization", token)).admin().indices().prepareCreate("idx").get()); + final ElasticsearchSecurityException e = expectThrows( + ElasticsearchSecurityException.class, + () -> client().filterWithHeader(Collections.singletonMap("Authorization", token)).admin().indices().prepareCreate("idx").get() + ); assertThat(e.status(), is(RestStatus.UNAUTHORIZED)); // users and roles are missing GetUsersResponse getUsersResponse = new GetUsersRequestBuilder(client()).usernames("joe").get(); @@ -438,8 +498,12 @@ public void testSnapshotDeleteRestore() { GetRolesResponse getRolesResponse = new GetRolesRequestBuilder(client()).names("test_role").get(); assertThat(getRolesResponse.roles().length, is(0)); // restore - RestoreSnapshotResponse response = client().admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap-1") - .setWaitForCompletion(true).setIncludeAliases(true).get(); + RestoreSnapshotResponse response = client().admin() + .cluster() + .prepareRestoreSnapshot("test-repo", "test-snap-1") + .setWaitForCompletion(true) + .setIncludeAliases(true) + .get(); assertThat(response.status(), equalTo(RestStatus.OK)); assertThat(response.getRestoreInfo().indices(), contains(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7)); // the realm cache should clear itself but we don't wish to race it @@ -455,48 +519,60 @@ public void testSnapshotDeleteRestore() { assertThat(Arrays.asList(getRolesResponse.roles()[0].getIndicesPrivileges()[0].getPrivileges()), contains("create_index")); assertThat(Arrays.asList(getRolesResponse.roles()[0].getIndicesPrivileges()[0].getIndices()), contains("*")); // joe can create indices - CreateIndexResponse createIndexResponse = client().filterWithHeader(Collections.singletonMap("Authorization", token)).admin() - .indices().prepareCreate("idx").get(); - assertThat(createIndexResponse.isAcknowledged(), is (true)); + CreateIndexResponse createIndexResponse = client().filterWithHeader(Collections.singletonMap("Authorization", token)) + .admin() + .indices() + .prepareCreate("idx") + .get(); + assertThat(createIndexResponse.isAcknowledged(), is(true)); assertAcked(client().admin().cluster().prepareDeleteRepository("test-repo")); } public void testAuthenticateWithDeletedRole() { logger.error("--> creating role"); - preparePutRole("test_role") - .cluster("all") - .addIndices(new String[]{"*"}, new String[]{"read"}, new String[]{"body", "title"}, null, - new BytesArray("{\"match_all\": {}}"), randomBoolean()) - .get(); + preparePutRole("test_role").cluster("all") + .addIndices( + new String[] { "*" }, + new String[] { "read" }, + new String[] { "body", "title" }, + null, + new BytesArray("{\"match_all\": {}}"), + randomBoolean() + ) + .get(); preparePutUser("joe", "s3krit-password", hasher, "test_role").get(); logger.error("--> waiting for .security index"); ensureGreen(SECURITY_MAIN_ALIAS); final String token = basicAuthHeaderValue("joe", new SecureString("s3krit-password")); - ClusterHealthResponse response = client().filterWithHeader(Collections.singletonMap("Authorization", token)).admin().cluster() - .prepareHealth().get(); + ClusterHealthResponse response = client().filterWithHeader(Collections.singletonMap("Authorization", token)) + .admin() + .cluster() + .prepareHealth() + .get(); assertFalse(response.isTimedOut()); new DeleteRoleRequestBuilder(client()).name("test_role").get(); if (anonymousEnabled && roleExists) { assertNoTimeout( - client().filterWithHeader(Collections.singletonMap("Authorization", token)).admin().cluster().prepareHealth().get()); + client().filterWithHeader(Collections.singletonMap("Authorization", token)).admin().cluster().prepareHealth().get() + ); } else { - ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, () -> - client().filterWithHeader(Collections.singletonMap("Authorization", token)).admin().cluster().prepareHealth().get()); + ElasticsearchSecurityException e = expectThrows( + ElasticsearchSecurityException.class, + () -> client().filterWithHeader(Collections.singletonMap("Authorization", token)).admin().cluster().prepareHealth().get() + ); assertThat(e.status(), is(RestStatus.FORBIDDEN)); } } public void testPutUserWithoutPassword() { // create some roles - preparePutRole("admin_role") - .cluster("all") - .addIndices(new String[]{"*"}, new String[]{"all"}, null, null, null, randomBoolean()) - .get(); - preparePutRole("read_role") - .cluster("none") - .addIndices(new String[]{"*"}, new String[]{"read"}, null, null, null, randomBoolean()) - .get(); + preparePutRole("admin_role").cluster("all") + .addIndices(new String[] { "*" }, new String[] { "all" }, null, null, null, randomBoolean()) + .get(); + preparePutRole("read_role").cluster("none") + .addIndices(new String[] { "*" }, new String[] { "read" }, null, null, null, randomBoolean()) + .get(); assertThat(new GetUsersRequestBuilder(client()).usernames("joes").get().hasUsers(), is(false)); // check that putting a user without a password fails if the user doesn't exist @@ -513,8 +589,11 @@ public void testPutUserWithoutPassword() { preparePutUser("joe", SecuritySettingsSourceField.TEST_PASSWORD, hasher, "admin_role").get(); assertThat(new GetUsersRequestBuilder(client()).usernames("joe").get().hasUsers(), is(true)); final String token = basicAuthHeaderValue("joe", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING); - ClusterHealthResponse response = client().filterWithHeader(Collections.singletonMap("Authorization", token)).admin().cluster() - .prepareHealth().get(); + ClusterHealthResponse response = client().filterWithHeader(Collections.singletonMap("Authorization", token)) + .admin() + .cluster() + .prepareHealth() + .get(); assertFalse(response.isTimedOut()); // modify joe without sending the password @@ -529,18 +608,20 @@ public void testPutUserWithoutPassword() { // test that role change took effect if anonymous is disabled as anonymous grants monitoring permissions... if (anonymousEnabled && roleExists) { assertNoTimeout( - client().filterWithHeader(Collections.singletonMap("Authorization", token)).admin().cluster().prepareHealth().get()); + client().filterWithHeader(Collections.singletonMap("Authorization", token)).admin().cluster().prepareHealth().get() + ); } else { - ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, () -> - client().filterWithHeader(Collections.singletonMap("Authorization", token)).admin().cluster().prepareHealth().get()); + ElasticsearchSecurityException e = expectThrows( + ElasticsearchSecurityException.class, + () -> client().filterWithHeader(Collections.singletonMap("Authorization", token)).admin().cluster().prepareHealth().get() + ); assertThat(e.status(), is(RestStatus.FORBIDDEN)); assertThat(e.getMessage(), containsString("authorized")); } // update the user with password and admin role again String secondPassword = SecuritySettingsSourceField.TEST_PASSWORD + "2"; - preparePutUser("joe", secondPassword, hasher, "admin_role"). - fullName("Joe Smith").get(); + preparePutUser("joe", secondPassword, hasher, "admin_role").fullName("Joe Smith").get(); getUsersResponse = new GetUsersRequestBuilder(client()).usernames("joe").get(); assertThat(getUsersResponse.hasUsers(), is(true)); assertThat(getUsersResponse.users().length, is(1)); @@ -557,11 +638,9 @@ public void testPutUserWithoutPassword() { } // test with new password and role - response = client() - .filterWithHeader( - Collections.singletonMap("Authorization", - basicAuthHeaderValue("joe", new SecureString(secondPassword.toCharArray())))) - .admin().cluster().prepareHealth().get(); + response = client().filterWithHeader( + Collections.singletonMap("Authorization", basicAuthHeaderValue("joe", new SecureString(secondPassword.toCharArray()))) + ).admin().cluster().prepareHealth().get(); assertFalse(response.isTimedOut()); } @@ -575,7 +654,8 @@ public void testCannotCreateUserWithShortPassword() throws Exception { } public void testCannotCreateUserWithInvalidCharactersInName() throws Exception { - IllegalArgumentException v = expectThrows(IllegalArgumentException.class, + IllegalArgumentException v = expectThrows( + IllegalArgumentException.class, () -> preparePutUser("fóóbár", "my-am@zing-password", hasher, "admin_role").get() ); assertThat(v.getMessage(), containsString("names must be")); @@ -585,12 +665,10 @@ public void testUsersAndRolesDoNotInterfereWithIndicesStats() throws Exception { client().prepareIndex("foo").setSource("ignore", "me").get(); if (randomBoolean()) { - preparePutUser("joe", "s3krit-password", hasher, - SecuritySettingsSource.TEST_ROLE).get(); + preparePutUser("joe", "s3krit-password", hasher, SecuritySettingsSource.TEST_ROLE).get(); } else { - preparePutRole("read_role") - .cluster("none") - .addIndices(new String[]{"*"}, new String[]{"read"}, null, null, null, randomBoolean()) + preparePutRole("read_role").cluster("none") + .addIndices(new String[] { "*" }, new String[] { "read" }, null, null, null, randomBoolean()) .get(); } @@ -598,45 +676,54 @@ public void testUsersAndRolesDoNotInterfereWithIndicesStats() throws Exception { assertThat(response.getFailedShards(), is(0)); assertThat(response.getIndices().size(), is(2)); assertThat(response.getIndices().get(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7), notNullValue()); - assertThat(response.getIndices().get(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7).getIndex(), - is(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7)); + assertThat( + response.getIndices().get(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7).getIndex(), + is(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7) + ); } public void testOperationsOnReservedUsers() throws Exception { final String username = randomFrom(ElasticUser.NAME, KibanaUser.NAME); - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, - () -> preparePutUser(username, randomBoolean() ? SecuritySettingsSourceField.TEST_PASSWORD : null, hasher, "admin").get()); + IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> preparePutUser(username, randomBoolean() ? SecuritySettingsSourceField.TEST_PASSWORD : null, hasher, "admin").get() + ); assertThat(exception.getMessage(), containsString("user [" + username + "] is reserved")); - exception = expectThrows(IllegalArgumentException.class, - () -> new DeleteUserRequestBuilder(client()).username(username).get()); + exception = expectThrows(IllegalArgumentException.class, () -> new DeleteUserRequestBuilder(client()).username(username).get()); assertThat(exception.getMessage(), containsString("user [" + username + "] is reserved")); - exception = expectThrows(IllegalArgumentException.class, - () -> new DeleteUserRequestBuilder(client()).username(AnonymousUser.DEFAULT_ANONYMOUS_USERNAME).get()); + exception = expectThrows( + IllegalArgumentException.class, + () -> new DeleteUserRequestBuilder(client()).username(AnonymousUser.DEFAULT_ANONYMOUS_USERNAME).get() + ); assertThat(exception.getMessage(), containsString("user [" + AnonymousUser.DEFAULT_ANONYMOUS_USERNAME + "] is anonymous")); - exception = expectThrows(IllegalArgumentException.class, + exception = expectThrows( + IllegalArgumentException.class, () -> new ChangePasswordRequestBuilder(client()).username(AnonymousUser.DEFAULT_ANONYMOUS_USERNAME) - .password("foobar-password".toCharArray(), hasher).get()); + .password("foobar-password".toCharArray(), hasher) + .get() + ); assertThat(exception.getMessage(), containsString("user [" + AnonymousUser.DEFAULT_ANONYMOUS_USERNAME + "] is anonymous")); - exception = expectThrows(IllegalArgumentException.class, - () -> preparePutUser(AnonymousUser.DEFAULT_ANONYMOUS_USERNAME, "foobar-password", hasher).get()); + exception = expectThrows( + IllegalArgumentException.class, + () -> preparePutUser(AnonymousUser.DEFAULT_ANONYMOUS_USERNAME, "foobar-password", hasher).get() + ); assertThat(exception.getMessage(), containsString("user [" + AnonymousUser.DEFAULT_ANONYMOUS_USERNAME + "] is anonymous")); final String internalUser = randomFrom(SystemUser.NAME, XPackUser.NAME, XPackSecurityUser.NAME, AsyncSearchUser.NAME); - exception = expectThrows(IllegalArgumentException.class, - () -> preparePutUser(internalUser, "foobar-password", hasher).get()); + exception = expectThrows(IllegalArgumentException.class, () -> preparePutUser(internalUser, "foobar-password", hasher).get()); assertThat(exception.getMessage(), containsString("user [" + internalUser + "] is internal")); - exception = expectThrows(IllegalArgumentException.class, - () -> new ChangePasswordRequestBuilder(client()).username(internalUser) - .password("foobar-password".toCharArray(), hasher).get()); + exception = expectThrows( + IllegalArgumentException.class, + () -> new ChangePasswordRequestBuilder(client()).username(internalUser).password("foobar-password".toCharArray(), hasher).get() + ); assertThat(exception.getMessage(), containsString("user [" + internalUser + "] is internal")); - exception = expectThrows(IllegalArgumentException.class, - () -> new DeleteUserRequestBuilder(client()).username(internalUser).get()); + exception = expectThrows(IllegalArgumentException.class, () -> new DeleteUserRequestBuilder(client()).username(internalUser).get()); assertThat(exception.getMessage(), containsString("user [" + internalUser + "] is internal")); // get should work @@ -645,11 +732,9 @@ public void testOperationsOnReservedUsers() throws Exception { assertThat(response.users()[0].principal(), is(username)); // authenticate should work - AuthenticateResponse authenticateResponse = client() - .filterWithHeader(Collections.singletonMap("Authorization", - basicAuthHeaderValue(username, getReservedPassword()))) - .execute(AuthenticateAction.INSTANCE, new AuthenticateRequest(username)) - .get(); + AuthenticateResponse authenticateResponse = client().filterWithHeader( + Collections.singletonMap("Authorization", basicAuthHeaderValue(username, getReservedPassword())) + ).execute(AuthenticateAction.INSTANCE, new AuthenticateRequest(username)).get(); assertThat(authenticateResponse.authentication().getUser().principal(), is(username)); assertThat(authenticateResponse.authentication().getAuthenticatedBy().getName(), equalTo("reserved")); assertThat(authenticateResponse.authentication().getAuthenticatedBy().getType(), equalTo("reserved")); @@ -658,12 +743,13 @@ public void testOperationsOnReservedUsers() throws Exception { public void testOperationsOnReservedRoles() throws Exception { final String name = randomFrom(ReservedRolesStore.names()); - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, - () -> preparePutRole(name).cluster("monitor").get()); + IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> preparePutRole(name).cluster("monitor").get() + ); assertThat(exception.getMessage(), containsString("role [" + name + "] is reserved")); - exception = expectThrows(IllegalArgumentException.class, - () -> new DeleteRoleRequestBuilder(client()).name(name).get()); + exception = expectThrows(IllegalArgumentException.class, () -> new DeleteRoleRequestBuilder(client()).name(name).get()); assertThat(exception.getMessage(), containsString("role [" + name + "] is reserved")); // get role is allowed @@ -673,36 +759,36 @@ public void testOperationsOnReservedRoles() throws Exception { } public void testCreateAndChangePassword() throws Exception { - preparePutUser("joe", "s3krit-password", hasher, - SecuritySettingsSource.TEST_ROLE).get(); + preparePutUser("joe", "s3krit-password", hasher, SecuritySettingsSource.TEST_ROLE).get(); final String token = basicAuthHeaderValue("joe", new SecureString("s3krit-password")); ClusterHealthResponse response = client().filterWithHeader(Collections.singletonMap("Authorization", token)) - .admin().cluster().prepareHealth().get(); + .admin() + .cluster() + .prepareHealth() + .get(); assertThat(response.isTimedOut(), is(false)); - ActionResponse.Empty passwordResponse = - new ChangePasswordRequestBuilder(client().filterWithHeader(Collections.singletonMap("Authorization", token))) - .username("joe").password(SecuritySettingsSourceField.TEST_PASSWORD.toCharArray(), hasher).get(); + ActionResponse.Empty passwordResponse = new ChangePasswordRequestBuilder( + client().filterWithHeader(Collections.singletonMap("Authorization", token)) + ).username("joe").password(SecuritySettingsSourceField.TEST_PASSWORD.toCharArray(), hasher).get(); assertThat(passwordResponse, notNullValue()); - - ElasticsearchSecurityException expected = expectThrows(ElasticsearchSecurityException.class, - () -> client().filterWithHeader(Collections.singletonMap("Authorization", token)).admin().cluster().prepareHealth().get()); + ElasticsearchSecurityException expected = expectThrows( + ElasticsearchSecurityException.class, + () -> client().filterWithHeader(Collections.singletonMap("Authorization", token)).admin().cluster().prepareHealth().get() + ); assertThat(expected.status(), is(RestStatus.UNAUTHORIZED)); - response = client() - .filterWithHeader( - Collections.singletonMap("Authorization", - basicAuthHeaderValue("joe", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING))) - .admin().cluster().prepareHealth().get(); + response = client().filterWithHeader( + Collections.singletonMap("Authorization", basicAuthHeaderValue("joe", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)) + ).admin().cluster().prepareHealth().get(); assertThat(response.isTimedOut(), is(false)); } public void testRolesUsageStats() throws Exception { NativeRolesStore rolesStore = internalCluster().getInstance(NativeRolesStore.class); - long roles = anonymousEnabled && roleExists ? 1L: 0L; - logger.info("--> running testRolesUsageStats with anonymousEnabled=[{}], roleExists=[{}]", - anonymousEnabled, roleExists); + long roles = anonymousEnabled && roleExists ? 1L : 0L; + logger.info("--> running testRolesUsageStats with anonymousEnabled=[{}], roleExists=[{}]", anonymousEnabled, roleExists); PlainActionFuture> future = new PlainActionFuture<>(); rolesStore.usageStats(future); Map usage = future.get(); @@ -713,14 +799,14 @@ public void testRolesUsageStats() throws Exception { final boolean fls = randomBoolean(); final boolean dls = randomBoolean(); PutRoleResponse putRoleResponse = new PutRoleRequestBuilder(client()).name("admin_role") - .cluster("all") - .addIndices(new String[]{"*"}, new String[]{"all"}, null, null, null, randomBoolean()) - .get(); + .cluster("all") + .addIndices(new String[] { "*" }, new String[] { "all" }, null, null, null, randomBoolean()) + .get(); assertThat(putRoleResponse.isCreated(), is(true)); roles++; if (fls) { PutRoleResponse roleResponse; - String[] fields = new String[]{"foo"}; + String[] fields = new String[] { "foo" }; final String[] grantedFields; final String[] deniedFields; if (randomBoolean()) { @@ -731,18 +817,25 @@ public void testRolesUsageStats() throws Exception { deniedFields = fields; } roleResponse = new PutRoleRequestBuilder(client()).name("admin_role_fls") - .cluster("all") - .addIndices(new String[]{"*"}, new String[]{"all"}, grantedFields, deniedFields, null, randomBoolean()) - .get(); + .cluster("all") + .addIndices(new String[] { "*" }, new String[] { "all" }, grantedFields, deniedFields, null, randomBoolean()) + .get(); assertThat(roleResponse.isCreated(), is(true)); roles++; } if (dls) { PutRoleResponse roleResponse = new PutRoleRequestBuilder(client()).name("admin_role_dls") - .cluster("all") - .addIndices(new String[]{"*"}, new String[]{"all"}, null, null, new BytesArray("{\"match_all\": {}}"), randomBoolean()) - .get(); + .cluster("all") + .addIndices( + new String[] { "*" }, + new String[] { "all" }, + null, + null, + new BytesArray("{\"match_all\": {}}"), + randomBoolean() + ) + .get(); assertThat(roleResponse.isCreated(), is(true)); roles++; } @@ -765,7 +858,8 @@ public void testRealmUsageStats() { } XPackUsageResponse response = new XPackUsageRequestBuilder(client()).get(); - Optional securityUsage = response.getUsages().stream() + Optional securityUsage = response.getUsages() + .stream() .filter(usage -> usage instanceof SecurityFeatureSetUsage) .findFirst(); assertTrue(securityUsage.isPresent()); @@ -780,17 +874,21 @@ public void testRealmUsageStats() { } public void testSetEnabled() throws Exception { - preparePutUser("joe", "s3krit-password", hasher, - SecuritySettingsSource.TEST_ROLE).get(); + preparePutUser("joe", "s3krit-password", hasher, SecuritySettingsSource.TEST_ROLE).get(); final String token = basicAuthHeaderValue("joe", new SecureString("s3krit-password")); ClusterHealthResponse response = client().filterWithHeader(Collections.singletonMap("Authorization", token)) - .admin().cluster().prepareHealth().get(); + .admin() + .cluster() + .prepareHealth() + .get(); assertThat(response.isTimedOut(), is(false)); new SetEnabledRequestBuilder(client()).username("joe").enabled(false).get(); - ElasticsearchSecurityException expected = expectThrows(ElasticsearchSecurityException.class, - () -> client().filterWithHeader(Collections.singletonMap("Authorization", token)).admin().cluster().prepareHealth().get()); + ElasticsearchSecurityException expected = expectThrows( + ElasticsearchSecurityException.class, + () -> client().filterWithHeader(Collections.singletonMap("Authorization", token)).admin().cluster().prepareHealth().get() + ); assertThat(expected.status(), is(RestStatus.UNAUTHORIZED)); new SetEnabledRequestBuilder(client()).username("joe").enabled(true).get(); @@ -798,8 +896,10 @@ public void testSetEnabled() throws Exception { response = client().filterWithHeader(Collections.singletonMap("Authorization", token)).admin().cluster().prepareHealth().get(); assertThat(response.isTimedOut(), is(false)); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> new SetEnabledRequestBuilder(client()).username("not_a_real_user").enabled(false).get()); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> new SetEnabledRequestBuilder(client()).username("not_a_real_user").enabled(false).get() + ); assertThat(e.getMessage(), containsString("only existing users can be disabled")); } @@ -809,25 +909,25 @@ public void testNegativeLookupsThenCreateRole() throws Exception { final int negativeLookups = scaledRandomIntBetween(1, 10); for (int i = 0; i < negativeLookups; i++) { if (anonymousEnabled && roleExists) { - ClusterHealthResponse response = client() - .filterWithHeader(Collections.singletonMap("Authorization", - basicAuthHeaderValue("joe", new SecureString("s3krit-password")))) - .admin().cluster().prepareHealth().get(); + ClusterHealthResponse response = client().filterWithHeader( + Collections.singletonMap("Authorization", basicAuthHeaderValue("joe", new SecureString("s3krit-password"))) + ).admin().cluster().prepareHealth().get(); assertNoTimeout(response); } else { - ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, () -> client() - .filterWithHeader(Collections.singletonMap("Authorization", - basicAuthHeaderValue("joe", new SecureString("s3krit-password")))) - .admin().cluster().prepareHealth().get()); + ElasticsearchSecurityException e = expectThrows( + ElasticsearchSecurityException.class, + () -> client().filterWithHeader( + Collections.singletonMap("Authorization", basicAuthHeaderValue("joe", new SecureString("s3krit-password"))) + ).admin().cluster().prepareHealth().get() + ); assertThat(e.status(), is(RestStatus.FORBIDDEN)); } } preparePutRole("unknown_role").cluster("all").get(); - ClusterHealthResponse response = client() - .filterWithHeader(Collections.singletonMap("Authorization", - basicAuthHeaderValue("joe", new SecureString("s3krit-password")))) - .admin().cluster().prepareHealth().get(); + ClusterHealthResponse response = client().filterWithHeader( + Collections.singletonMap("Authorization", basicAuthHeaderValue("joe", new SecureString("s3krit-password"))) + ).admin().cluster().prepareHealth().get(); assertNoTimeout(response); } @@ -843,9 +943,7 @@ public void testConcurrentRunAs() throws Exception { preparePutUser("joe", "s3krit-password", hasher, SecuritySettingsSource.TEST_ROLE).get(); preparePutUser("executor", "s3krit-password", hasher, "superuser").get(); final String token = basicAuthHeaderValue("executor", new SecureString("s3krit-password")); - final Client client = client().filterWithHeader(Map.of( - "Authorization", token, - "es-security-runas-user", "joe")); + final Client client = client().filterWithHeader(Map.of("Authorization", token, "es-security-runas-user", "joe")); final CountDownLatch latch = new CountDownLatch(1); final int numberOfProcessors = Runtime.getRuntime().availableProcessors(); final int numberOfThreads = scaledRandomIntBetween(numberOfProcessors, numberOfProcessors * 3); @@ -875,8 +973,7 @@ public void testConcurrentRunAs() throws Exception { } private PutUserRequestBuilder preparePutUser(String username, String password, Hasher hasher, String... roles) { - return new PutUserRequestBuilder(client()) - .username(username) + return new PutUserRequestBuilder(client()).username(username) .password(password == null ? null : new SecureString(password.toCharArray()), hasher) .roles(roles); } diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/esnative/ReservedRealmElasticAutoconfigIntegTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/esnative/ReservedRealmElasticAutoconfigIntegTests.java index 9d4e3d9f2a288..611678d6f43c9 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/esnative/ReservedRealmElasticAutoconfigIntegTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/esnative/ReservedRealmElasticAutoconfigIntegTests.java @@ -47,8 +47,10 @@ public Settings nodeSettings() { Settings.Builder settingsBuilder = Settings.builder() .put(super.nodeSettings()) .put("xpack.security.authc.password_hashing.algorithm", hasher.name()); - ((MockSecureSettings) settingsBuilder.getSecureSettings()).setString("autoconfiguration.password_hash", - new String(hasher.hash(new SecureString("auto_password_that_is_longer_than_14_chars_because_of_FIPS".toCharArray())))); + ((MockSecureSettings) settingsBuilder.getSecureSettings()).setString( + "autoconfiguration.password_hash", + new String(hasher.hash(new SecureString("auto_password_that_is_longer_than_14_chars_because_of_FIPS".toCharArray()))) + ); return settingsBuilder.build(); } @@ -66,8 +68,7 @@ public void testAutoconfigFailedPasswordPromotion() { try { // prevents the .security index from being created automatically (after elastic user authentication) ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); - updateSettingsRequest.transientSettings(Settings.builder().put(Metadata.SETTING_READ_ONLY_ALLOW_DELETE_SETTING.getKey(), - true)); + updateSettingsRequest.transientSettings(Settings.builder().put(Metadata.SETTING_READ_ONLY_ALLOW_DELETE_SETTING.getKey(), true)); assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); // delete the security index, if it exist @@ -83,28 +84,45 @@ public void testAutoconfigFailedPasswordPromotion() { } // elastic user gets 503 for the good password - Request restRequest = randomFrom(new Request("GET", "/_security/_authenticate"), new Request("GET", "_cluster/health"), - new Request("GET", "_nodes")); + Request restRequest = randomFrom( + new Request("GET", "/_security/_authenticate"), + new Request("GET", "_cluster/health"), + new Request("GET", "_nodes") + ); RequestOptions.Builder options = RequestOptions.DEFAULT.toBuilder(); - options.addHeader(UsernamePasswordToken.BASIC_AUTH_HEADER, UsernamePasswordToken.basicAuthHeaderValue("elastic", - new SecureString("auto_password_that_is_longer_than_14_chars_because_of_FIPS".toCharArray()))); + options.addHeader( + UsernamePasswordToken.BASIC_AUTH_HEADER, + UsernamePasswordToken.basicAuthHeaderValue( + "elastic", + new SecureString("auto_password_that_is_longer_than_14_chars_because_of_FIPS".toCharArray()) + ) + ); restRequest.setOptions(options); ResponseException exception = expectThrows(ResponseException.class, () -> getRestClient().performRequest(restRequest)); assertThat(exception.getResponse().getStatusLine().getStatusCode(), is(RestStatus.SERVICE_UNAVAILABLE.getStatus())); // but gets a 401 for the wrong password - Request restRequest2 = randomFrom(new Request("GET", "/_security/_authenticate"), new Request("GET", "_cluster/health"), - new Request("GET", "_nodes")); + Request restRequest2 = randomFrom( + new Request("GET", "/_security/_authenticate"), + new Request("GET", "_cluster/health"), + new Request("GET", "_nodes") + ); options = RequestOptions.DEFAULT.toBuilder(); - options.addHeader(UsernamePasswordToken.BASIC_AUTH_HEADER, UsernamePasswordToken.basicAuthHeaderValue("elastic", - new SecureString("wrong password_that_is_longer_than_14_chars_because_of_FIPS".toCharArray()))); + options.addHeader( + UsernamePasswordToken.BASIC_AUTH_HEADER, + UsernamePasswordToken.basicAuthHeaderValue( + "elastic", + new SecureString("wrong password_that_is_longer_than_14_chars_because_of_FIPS".toCharArray()) + ) + ); restRequest2.setOptions(options); exception = expectThrows(ResponseException.class, () -> getRestClient().performRequest(restRequest2)); assertThat(exception.getResponse().getStatusLine().getStatusCode(), is(RestStatus.UNAUTHORIZED.getStatus())); } finally { ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); - updateSettingsRequest.transientSettings(Settings.builder().put(Metadata.SETTING_READ_ONLY_ALLOW_DELETE_SETTING.getKey(), - (String) null)); + updateSettingsRequest.transientSettings( + Settings.builder().put(Metadata.SETTING_READ_ONLY_ALLOW_DELETE_SETTING.getKey(), (String) null) + ); assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); } } @@ -122,49 +140,74 @@ public void testAutoconfigSucceedsAfterPromotionFailure() throws Exception { // but then make the cluster read-only ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); - updateSettingsRequest.transientSettings(Settings.builder().put(Metadata.SETTING_READ_ONLY_ALLOW_DELETE_SETTING.getKey(), - true)); + updateSettingsRequest.transientSettings(Settings.builder().put(Metadata.SETTING_READ_ONLY_ALLOW_DELETE_SETTING.getKey(), true)); assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); // elastic user now gets 503 for the good password - Request restRequest = randomFrom(new Request("GET", "/_security/_authenticate"), new Request("GET", "_cluster/health"), - new Request("GET", "_nodes")); + Request restRequest = randomFrom( + new Request("GET", "/_security/_authenticate"), + new Request("GET", "_cluster/health"), + new Request("GET", "_nodes") + ); RequestOptions.Builder options = RequestOptions.DEFAULT.toBuilder(); - options.addHeader(UsernamePasswordToken.BASIC_AUTH_HEADER, UsernamePasswordToken.basicAuthHeaderValue("elastic", - new SecureString("auto_password_that_is_longer_than_14_chars_because_of_FIPS".toCharArray()))); + options.addHeader( + UsernamePasswordToken.BASIC_AUTH_HEADER, + UsernamePasswordToken.basicAuthHeaderValue( + "elastic", + new SecureString("auto_password_that_is_longer_than_14_chars_because_of_FIPS".toCharArray()) + ) + ); restRequest.setOptions(options); ResponseException exception = expectThrows(ResponseException.class, () -> getRestClient().performRequest(restRequest)); assertThat(exception.getResponse().getStatusLine().getStatusCode(), is(RestStatus.SERVICE_UNAVAILABLE.getStatus())); // clear cluster-wide write block updateSettingsRequest = new ClusterUpdateSettingsRequest(); - updateSettingsRequest.transientSettings(Settings.builder().put(Metadata.SETTING_READ_ONLY_ALLOW_DELETE_SETTING.getKey(), - (String) null)); + updateSettingsRequest.transientSettings( + Settings.builder().put(Metadata.SETTING_READ_ONLY_ALLOW_DELETE_SETTING.getKey(), (String) null) + ); assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); if (randomBoolean()) { - Request restRequest2 = randomFrom(new Request("GET", "/_security/_authenticate"), new Request("GET", "_cluster/health"), - new Request("GET", "_nodes")); + Request restRequest2 = randomFrom( + new Request("GET", "/_security/_authenticate"), + new Request("GET", "_cluster/health"), + new Request("GET", "_nodes") + ); options = RequestOptions.DEFAULT.toBuilder(); - options.addHeader(UsernamePasswordToken.BASIC_AUTH_HEADER, UsernamePasswordToken.basicAuthHeaderValue("elastic", - new SecureString("wrong password_that_is_longer_than_14_chars_because_of_FIPS".toCharArray()))); + options.addHeader( + UsernamePasswordToken.BASIC_AUTH_HEADER, + UsernamePasswordToken.basicAuthHeaderValue( + "elastic", + new SecureString("wrong password_that_is_longer_than_14_chars_because_of_FIPS".toCharArray()) + ) + ); restRequest2.setOptions(options); exception = expectThrows(ResponseException.class, () -> getRestClient().performRequest(restRequest2)); assertThat(exception.getResponse().getStatusLine().getStatusCode(), is(RestStatus.UNAUTHORIZED.getStatus())); } // now the auto config password can be promoted, and authn succeeds - Request restRequest3 = randomFrom(new Request("GET", "/_security/_authenticate"), new Request("GET", "_cluster/health"), - new Request("GET", "_nodes")); + Request restRequest3 = randomFrom( + new Request("GET", "/_security/_authenticate"), + new Request("GET", "_cluster/health"), + new Request("GET", "_nodes") + ); options = RequestOptions.DEFAULT.toBuilder(); - options.addHeader(UsernamePasswordToken.BASIC_AUTH_HEADER, UsernamePasswordToken.basicAuthHeaderValue("elastic", - new SecureString("auto_password_that_is_longer_than_14_chars_because_of_FIPS".toCharArray()))); + options.addHeader( + UsernamePasswordToken.BASIC_AUTH_HEADER, + UsernamePasswordToken.basicAuthHeaderValue( + "elastic", + new SecureString("auto_password_that_is_longer_than_14_chars_because_of_FIPS".toCharArray()) + ) + ); restRequest3.setOptions(options); assertThat(getRestClient().performRequest(restRequest3).getStatusLine().getStatusCode(), is(RestStatus.OK.getStatus())); } finally { ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); - updateSettingsRequest.transientSettings(Settings.builder().put(Metadata.SETTING_READ_ONLY_ALLOW_DELETE_SETTING.getKey(), - (String) null)); + updateSettingsRequest.transientSettings( + Settings.builder().put(Metadata.SETTING_READ_ONLY_ALLOW_DELETE_SETTING.getKey(), (String) null) + ); assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); } } diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/esnative/ReservedRealmIntegTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/esnative/ReservedRealmIntegTests.java index d56d145b9e9eb..7b503538b24f4 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/esnative/ReservedRealmIntegTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/esnative/ReservedRealmIntegTests.java @@ -64,15 +64,19 @@ protected boolean addMockHttpTransport() { } public void testAuthenticate() { - final List usernames = Arrays.asList(ElasticUser.NAME, KibanaUser.NAME, KibanaSystemUser.NAME, - LogstashSystemUser.NAME, BeatsSystemUser.NAME, APMSystemUser.NAME, RemoteMonitoringUser.NAME); + final List usernames = Arrays.asList( + ElasticUser.NAME, + KibanaUser.NAME, + KibanaSystemUser.NAME, + LogstashSystemUser.NAME, + BeatsSystemUser.NAME, + APMSystemUser.NAME, + RemoteMonitoringUser.NAME + ); for (String username : usernames) { - ClusterHealthResponse response = client() - .filterWithHeader(singletonMap("Authorization", basicAuthHeaderValue(username, getReservedPassword()))) - .admin() - .cluster() - .prepareHealth() - .get(); + ClusterHealthResponse response = client().filterWithHeader( + singletonMap("Authorization", basicAuthHeaderValue(username, getReservedPassword())) + ).admin().cluster().prepareHealth().get(); assertThat(response.getClusterName(), is(cluster().getClusterName())); } @@ -84,93 +88,99 @@ public void testAuthenticate() { */ public void testAuthenticateAfterEnablingUser() throws IOException { final RestHighLevelClient restClient = new TestRestHighLevelClient(); - final List usernames = Arrays.asList(ElasticUser.NAME, KibanaUser.NAME, KibanaSystemUser.NAME, - LogstashSystemUser.NAME, BeatsSystemUser.NAME, APMSystemUser.NAME, RemoteMonitoringUser.NAME); + final List usernames = Arrays.asList( + ElasticUser.NAME, + KibanaUser.NAME, + KibanaSystemUser.NAME, + LogstashSystemUser.NAME, + BeatsSystemUser.NAME, + APMSystemUser.NAME, + RemoteMonitoringUser.NAME + ); for (String username : usernames) { restClient.security().enableUser(new EnableUserRequest(username, RefreshPolicy.getDefault()), SECURITY_REQUEST_OPTIONS); - ClusterHealthResponse response = client() - .filterWithHeader(singletonMap("Authorization", basicAuthHeaderValue(username, getReservedPassword()))) - .admin() - .cluster() - .prepareHealth() - .get(); + ClusterHealthResponse response = client().filterWithHeader( + singletonMap("Authorization", basicAuthHeaderValue(username, getReservedPassword())) + ).admin().cluster().prepareHealth().get(); assertThat(response.getClusterName(), is(cluster().getClusterName())); } } public void testChangingPassword() throws IOException { - String username = randomFrom(ElasticUser.NAME, KibanaUser.NAME, KibanaSystemUser.NAME, - LogstashSystemUser.NAME, BeatsSystemUser.NAME, APMSystemUser.NAME, RemoteMonitoringUser.NAME); + String username = randomFrom( + ElasticUser.NAME, + KibanaUser.NAME, + KibanaSystemUser.NAME, + LogstashSystemUser.NAME, + BeatsSystemUser.NAME, + APMSystemUser.NAME, + RemoteMonitoringUser.NAME + ); final char[] newPassword = "supersecretvalue".toCharArray(); if (randomBoolean()) { - ClusterHealthResponse response = client() - .filterWithHeader(singletonMap("Authorization", basicAuthHeaderValue(username, getReservedPassword()))) - .admin() - .cluster() - .prepareHealth() - .get(); + ClusterHealthResponse response = client().filterWithHeader( + singletonMap("Authorization", basicAuthHeaderValue(username, getReservedPassword())) + ).admin().cluster().prepareHealth().get(); assertThat(response.getClusterName(), is(cluster().getClusterName())); } final RestHighLevelClient restClient = new TestRestHighLevelClient(); final boolean changed = restClient.security() - .changePassword(new ChangePasswordRequest(username, Arrays.copyOf(newPassword, newPassword.length), RefreshPolicy.IMMEDIATE), - SECURITY_REQUEST_OPTIONS); + .changePassword( + new ChangePasswordRequest(username, Arrays.copyOf(newPassword, newPassword.length), RefreshPolicy.IMMEDIATE), + SECURITY_REQUEST_OPTIONS + ); assertTrue(changed); - ElasticsearchSecurityException elasticsearchSecurityException = expectThrows(ElasticsearchSecurityException.class, () -> client() - .filterWithHeader(singletonMap("Authorization", basicAuthHeaderValue(username, getReservedPassword()))) - .admin() - .cluster() - .prepareHealth() - .get()); - assertThat(elasticsearchSecurityException.getMessage(), containsString("authenticate")); - - ClusterHealthResponse healthResponse = client() - .filterWithHeader(singletonMap("Authorization", basicAuthHeaderValue(username, new SecureString(newPassword)))) + ElasticsearchSecurityException elasticsearchSecurityException = expectThrows( + ElasticsearchSecurityException.class, + () -> client().filterWithHeader(singletonMap("Authorization", basicAuthHeaderValue(username, getReservedPassword()))) .admin() .cluster() .prepareHealth() - .get(); + .get() + ); + assertThat(elasticsearchSecurityException.getMessage(), containsString("authenticate")); + + ClusterHealthResponse healthResponse = client().filterWithHeader( + singletonMap("Authorization", basicAuthHeaderValue(username, new SecureString(newPassword))) + ).admin().cluster().prepareHealth().get(); assertThat(healthResponse.getClusterName(), is(cluster().getClusterName())); } public void testDisablingUser() throws Exception { final RestHighLevelClient restClient = new TestRestHighLevelClient(); // validate the user works - ClusterHealthResponse response = client() - .filterWithHeader(singletonMap("Authorization", basicAuthHeaderValue(ElasticUser.NAME, getReservedPassword()))) - .admin() - .cluster() - .prepareHealth() - .get(); + ClusterHealthResponse response = client().filterWithHeader( + singletonMap("Authorization", basicAuthHeaderValue(ElasticUser.NAME, getReservedPassword())) + ).admin().cluster().prepareHealth().get(); assertThat(response.getClusterName(), is(cluster().getClusterName())); // disable user - final boolean disabled = - restClient.security().disableUser(new DisableUserRequest(ElasticUser.NAME, RefreshPolicy.getDefault()), - SECURITY_REQUEST_OPTIONS); + final boolean disabled = restClient.security() + .disableUser(new DisableUserRequest(ElasticUser.NAME, RefreshPolicy.getDefault()), SECURITY_REQUEST_OPTIONS); assertTrue(disabled); - ElasticsearchSecurityException elasticsearchSecurityException = expectThrows(ElasticsearchSecurityException.class, () -> client() - .filterWithHeader(singletonMap("Authorization", basicAuthHeaderValue(ElasticUser.NAME, getReservedPassword()))) + ElasticsearchSecurityException elasticsearchSecurityException = expectThrows( + ElasticsearchSecurityException.class, + () -> client().filterWithHeader(singletonMap("Authorization", basicAuthHeaderValue(ElasticUser.NAME, getReservedPassword()))) .admin() .cluster() .prepareHealth() - .get()); + .get() + ); assertThat(elasticsearchSecurityException.getMessage(), containsString("authenticate")); - //enable - final boolean enabled = - restClient.security().enableUser(new EnableUserRequest(ElasticUser.NAME, RefreshPolicy.getDefault()), SECURITY_REQUEST_OPTIONS); + // enable + final boolean enabled = restClient.security() + .enableUser(new EnableUserRequest(ElasticUser.NAME, RefreshPolicy.getDefault()), SECURITY_REQUEST_OPTIONS); assertTrue(enabled); - response = client() - .filterWithHeader(singletonMap("Authorization", basicAuthHeaderValue(ElasticUser.NAME, getReservedPassword()))) - .admin() - .cluster() - .prepareHealth() - .get(); + response = client().filterWithHeader(singletonMap("Authorization", basicAuthHeaderValue(ElasticUser.NAME, getReservedPassword()))) + .admin() + .cluster() + .prepareHealth() + .get(); assertThat(response.getClusterName(), is(cluster().getClusterName())); } } diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/pki/PkiAuthDelegationIntegTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/pki/PkiAuthDelegationIntegTests.java index 01e68d662ed0e..4d7102fd59b96 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/pki/PkiAuthDelegationIntegTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/pki/PkiAuthDelegationIntegTests.java @@ -12,39 +12,39 @@ import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.ValidationException; import org.elasticsearch.client.security.AuthenticateResponse; -import org.elasticsearch.client.security.PutRoleMappingRequest; -import org.elasticsearch.client.security.RefreshPolicy; import org.elasticsearch.client.security.AuthenticateResponse.RealmInfo; -import org.elasticsearch.client.security.DeleteRoleMappingRequest; -import org.elasticsearch.client.security.support.expressiondsl.fields.FieldRoleMapperExpression; import org.elasticsearch.client.security.DelegatePkiAuthenticationRequest; import org.elasticsearch.client.security.DelegatePkiAuthenticationResponse; +import org.elasticsearch.client.security.DeleteRoleMappingRequest; import org.elasticsearch.client.security.InvalidateTokenRequest; import org.elasticsearch.client.security.InvalidateTokenResponse; +import org.elasticsearch.client.security.PutRoleMappingRequest; +import org.elasticsearch.client.security.RefreshPolicy; +import org.elasticsearch.client.security.support.expressiondsl.fields.FieldRoleMapperExpression; import org.elasticsearch.client.security.user.User; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.test.SecurityIntegTestCase; +import org.elasticsearch.test.SecuritySettingsSource; import org.elasticsearch.test.SecuritySettingsSourceField; import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.xpack.core.security.action.realm.ClearRealmCacheRequestBuilder; import org.elasticsearch.xpack.core.security.authc.support.Hasher; import org.junit.Before; -import org.elasticsearch.test.SecuritySettingsSource; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; import java.security.cert.CertificateFactory; import java.security.cert.X509Certificate; -import java.util.Collections; import java.util.Arrays; +import java.util.Collections; import static org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken.basicAuthHeaderValue; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.emptyCollectionOf; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; -import static org.hamcrest.Matchers.emptyCollectionOf; -import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.startsWith; @SuppressWarnings("removal") @@ -53,65 +53,83 @@ public class PkiAuthDelegationIntegTests extends SecurityIntegTestCase { @Override public Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { return Settings.builder() - .put(super.nodeSettings(nodeOrdinal, otherSettings)) - .put(XPackSettings.TOKEN_SERVICE_ENABLED_SETTING.getKey(), true) - // pki1 does not allow delegation - .put("xpack.security.authc.realms.pki.pki1.order", "2") - .putList("xpack.security.authc.realms.pki.pki1.certificate_authorities", - getDataPath("/org/elasticsearch/xpack/security/action/pki_delegation/testRootCA.crt").toString()) - .put("xpack.security.authc.realms.pki.pki1.files.role_mapping", getDataPath("role_mapping.yml")) - // pki2 allows delegation but has a non-matching username pattern - .put("xpack.security.authc.realms.pki.pki2.order", "3") - .putList("xpack.security.authc.realms.pki.pki2.certificate_authorities", - getDataPath("/org/elasticsearch/xpack/security/action/pki_delegation/testRootCA.crt").toString()) - .put("xpack.security.authc.realms.pki.pki2.username_pattern", "CN=MISMATCH(.*?)(?:,|$)") - .put("xpack.security.authc.realms.pki.pki2.delegation.enabled", true) - .put("xpack.security.authc.realms.pki.pki2.files.role_mapping", getDataPath("role_mapping.yml")) - // pki3 allows delegation and the username pattern (default) matches - .put("xpack.security.authc.realms.pki.pki3.order", "4") - .putList("xpack.security.authc.realms.pki.pki3.certificate_authorities", - getDataPath("/org/elasticsearch/xpack/security/action/pki_delegation/testRootCA.crt").toString()) - .put("xpack.security.authc.realms.pki.pki3.delegation.enabled", true) - .put("xpack.security.authc.realms.pki.pki3.files.role_mapping", getDataPath("role_mapping.yml")) - .build(); + .put(super.nodeSettings(nodeOrdinal, otherSettings)) + .put(XPackSettings.TOKEN_SERVICE_ENABLED_SETTING.getKey(), true) + // pki1 does not allow delegation + .put("xpack.security.authc.realms.pki.pki1.order", "2") + .putList( + "xpack.security.authc.realms.pki.pki1.certificate_authorities", + getDataPath("/org/elasticsearch/xpack/security/action/pki_delegation/testRootCA.crt").toString() + ) + .put("xpack.security.authc.realms.pki.pki1.files.role_mapping", getDataPath("role_mapping.yml")) + // pki2 allows delegation but has a non-matching username pattern + .put("xpack.security.authc.realms.pki.pki2.order", "3") + .putList( + "xpack.security.authc.realms.pki.pki2.certificate_authorities", + getDataPath("/org/elasticsearch/xpack/security/action/pki_delegation/testRootCA.crt").toString() + ) + .put("xpack.security.authc.realms.pki.pki2.username_pattern", "CN=MISMATCH(.*?)(?:,|$)") + .put("xpack.security.authc.realms.pki.pki2.delegation.enabled", true) + .put("xpack.security.authc.realms.pki.pki2.files.role_mapping", getDataPath("role_mapping.yml")) + // pki3 allows delegation and the username pattern (default) matches + .put("xpack.security.authc.realms.pki.pki3.order", "4") + .putList( + "xpack.security.authc.realms.pki.pki3.certificate_authorities", + getDataPath("/org/elasticsearch/xpack/security/action/pki_delegation/testRootCA.crt").toString() + ) + .put("xpack.security.authc.realms.pki.pki3.delegation.enabled", true) + .put("xpack.security.authc.realms.pki.pki3.files.role_mapping", getDataPath("role_mapping.yml")) + .build(); } @Override protected String configUsers() { final Hasher passwdHasher = getFastStoredHashAlgoForTests(); final String usersPasswdHashed = new String(passwdHasher.hash(SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)); - return super.configUsers() + - "user_manage:" + usersPasswdHashed + "\n" + - "user_manage_security:" + usersPasswdHashed + "\n" + - "user_delegate_pki:" + usersPasswdHashed + "\n" + - "user_all:" + usersPasswdHashed + "\n" + - "my_kibana_system:" + usersPasswdHashed + "\n"; + return super.configUsers() + + "user_manage:" + + usersPasswdHashed + + "\n" + + "user_manage_security:" + + usersPasswdHashed + + "\n" + + "user_delegate_pki:" + + usersPasswdHashed + + "\n" + + "user_all:" + + usersPasswdHashed + + "\n" + + "my_kibana_system:" + + usersPasswdHashed + + "\n"; } @Override protected String configRoles() { - return super.configRoles() + "\n" + - "role_manage:\n" + - " cluster: [ manage ]\n" + - "\n" + - "role_manage_security:\n" + - " cluster: [ manage_security ]\n" + - "\n" + - "role_delegate_pki:\n" + - " cluster: [ delegate_pki ]\n" + - "\n" + - "role_all:\n" + - " cluster: [ all ]\n"; + return super.configRoles() + + "\n" + + "role_manage:\n" + + " cluster: [ manage ]\n" + + "\n" + + "role_manage_security:\n" + + " cluster: [ manage_security ]\n" + + "\n" + + "role_delegate_pki:\n" + + " cluster: [ delegate_pki ]\n" + + "\n" + + "role_all:\n" + + " cluster: [ all ]\n"; } @Override protected String configUsersRoles() { - return super.configUsersRoles() + "\n" + - "role_manage:user_manage\n" + - "role_manage_security:user_manage_security\n" + - "role_delegate_pki:user_delegate_pki\n" + - "role_all:user_all\n" + - "kibana_system:my_kibana_system\n"; + return super.configUsersRoles() + + "\n" + + "role_manage:user_manage\n" + + "role_manage_security:user_manage_security\n" + + "role_delegate_pki:user_delegate_pki\n" + + "role_all:user_all\n" + + "kibana_system:my_kibana_system\n"; } @Override @@ -145,10 +163,12 @@ public void testDelegateThenAuthenticate() throws Exception { for (String delegateeUsername : Arrays.asList("user_all", "user_delegate_pki", "my_kibana_system")) { // delegate RequestOptions.Builder optionsBuilder = RequestOptions.DEFAULT.toBuilder(); - optionsBuilder.addHeader("Authorization", - basicAuthHeaderValue(delegateeUsername, SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)); - DelegatePkiAuthenticationResponse delegatePkiResponse = restClient.security().delegatePkiAuthentication(delegatePkiRequest, - optionsBuilder.build()); + optionsBuilder.addHeader( + "Authorization", + basicAuthHeaderValue(delegateeUsername, SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING) + ); + DelegatePkiAuthenticationResponse delegatePkiResponse = restClient.security() + .delegatePkiAuthentication(delegatePkiRequest, optionsBuilder.build()); String token = delegatePkiResponse.getAccessToken(); assertThat(token, is(notNullValue())); assertNotNull(delegatePkiResponse.getAuthentication()); @@ -186,10 +206,12 @@ public void testTokenInvalidate() throws Exception { String delegateeUsername = randomFrom("user_all", "user_delegate_pki", "my_kibana_system"); // delegate RequestOptions.Builder optionsBuilder = RequestOptions.DEFAULT.toBuilder(); - optionsBuilder.addHeader("Authorization", - basicAuthHeaderValue(delegateeUsername, SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)); - DelegatePkiAuthenticationResponse delegatePkiResponse = restClient.security().delegatePkiAuthentication(delegatePkiRequest, - optionsBuilder.build()); + optionsBuilder.addHeader( + "Authorization", + basicAuthHeaderValue(delegateeUsername, SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING) + ); + DelegatePkiAuthenticationResponse delegatePkiResponse = restClient.security() + .delegatePkiAuthentication(delegatePkiRequest, optionsBuilder.build()); String token = delegatePkiResponse.getAccessToken(); assertThat(token, is(notNullValue())); assertNotNull(delegatePkiResponse.getAuthentication()); @@ -216,14 +238,19 @@ public void testTokenInvalidate() throws Exception { // invalidate InvalidateTokenRequest invalidateRequest = InvalidateTokenRequest.accessToken(token); optionsBuilder = RequestOptions.DEFAULT.toBuilder(); - optionsBuilder.addHeader("Authorization", - basicAuthHeaderValue(delegateeUsername, SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)); + optionsBuilder.addHeader( + "Authorization", + basicAuthHeaderValue(delegateeUsername, SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING) + ); InvalidateTokenResponse invalidateResponse = restClient.security().invalidateToken(invalidateRequest, optionsBuilder.build()); assertThat(invalidateResponse.getInvalidatedTokens(), is(1)); assertThat(invalidateResponse.getErrorsCount(), is(0)); // failed authenticate - ElasticsearchStatusException e1 = expectThrows(ElasticsearchStatusException.class, () -> restClient.security() - .authenticate(RequestOptions.DEFAULT.toBuilder().addHeader("Authorization", "Bearer " + token).build())); + ElasticsearchStatusException e1 = expectThrows( + ElasticsearchStatusException.class, + () -> restClient.security() + .authenticate(RequestOptions.DEFAULT.toBuilder().addHeader("Authorization", "Bearer " + token).build()) + ); assertThat(e1.getMessage(), is("Elasticsearch exception [type=security_exception, reason=token expired]")); } } @@ -242,13 +269,21 @@ public void testDelegateUnauthorized() throws Exception { try (RestHighLevelClient restClient = new TestRestHighLevelClient()) { for (String delegateeUsername : Arrays.asList("user_manage", "user_manage_security")) { RequestOptions.Builder optionsBuilder = RequestOptions.DEFAULT.toBuilder(); - optionsBuilder.addHeader("Authorization", - basicAuthHeaderValue(delegateeUsername, SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)); - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, () -> { - restClient.security().delegatePkiAuthentication(delegatePkiRequest, optionsBuilder.build()); - }); - assertThat(e.getMessage(), startsWith("Elasticsearch exception [type=security_exception, reason=action" - + " [cluster:admin/xpack/security/delegate_pki] is unauthorized for user")); + optionsBuilder.addHeader( + "Authorization", + basicAuthHeaderValue(delegateeUsername, SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING) + ); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> { restClient.security().delegatePkiAuthentication(delegatePkiRequest, optionsBuilder.build()); } + ); + assertThat( + e.getMessage(), + startsWith( + "Elasticsearch exception [type=security_exception, reason=action" + + " [cluster:admin/xpack/security/delegate_pki] is unauthorized for user" + ) + ); } } } @@ -265,25 +300,44 @@ public void testDelegatePkiWithRoleMapping() throws Exception { delegatePkiRequest = new DelegatePkiAuthenticationRequest(Arrays.asList(clientCertificate, intermediateCA, rootCA)); } final RequestOptions testUserOptions = RequestOptions.DEFAULT.toBuilder() - .addHeader("Authorization", basicAuthHeaderValue(SecuritySettingsSource.TEST_USER_NAME, - new SecureString(SecuritySettingsSourceField.TEST_PASSWORD.toCharArray()))) - .build(); + .addHeader( + "Authorization", + basicAuthHeaderValue( + SecuritySettingsSource.TEST_USER_NAME, + new SecureString(SecuritySettingsSourceField.TEST_PASSWORD.toCharArray()) + ) + ) + .build(); try (RestHighLevelClient restClient = new TestRestHighLevelClient()) { // put role mappings for delegated PKI - PutRoleMappingRequest request = new PutRoleMappingRequest("role_by_delegated_user", true, - Collections.singletonList("role_by_delegated_user"), Collections.emptyList(), - new FieldRoleMapperExpression("metadata.pki_delegated_by_user", "test_user"), null, RefreshPolicy.IMMEDIATE); + PutRoleMappingRequest request = new PutRoleMappingRequest( + "role_by_delegated_user", + true, + Collections.singletonList("role_by_delegated_user"), + Collections.emptyList(), + new FieldRoleMapperExpression("metadata.pki_delegated_by_user", "test_user"), + null, + RefreshPolicy.IMMEDIATE + ); restClient.security().putRoleMapping(request, testUserOptions); - request = new PutRoleMappingRequest("role_by_delegated_realm", true, Collections.singletonList("role_by_delegated_realm"), - Collections.emptyList(), new FieldRoleMapperExpression("metadata.pki_delegated_by_realm", "file"), null, - RefreshPolicy.IMMEDIATE); + request = new PutRoleMappingRequest( + "role_by_delegated_realm", + true, + Collections.singletonList("role_by_delegated_realm"), + Collections.emptyList(), + new FieldRoleMapperExpression("metadata.pki_delegated_by_realm", "file"), + null, + RefreshPolicy.IMMEDIATE + ); restClient.security().putRoleMapping(request, testUserOptions); // delegate - DelegatePkiAuthenticationResponse delegatePkiResponse = restClient.security().delegatePkiAuthentication(delegatePkiRequest, - testUserOptions); + DelegatePkiAuthenticationResponse delegatePkiResponse = restClient.security() + .delegatePkiAuthentication(delegatePkiRequest, testUserOptions); // authenticate - AuthenticateResponse resp = restClient.security().authenticate(RequestOptions.DEFAULT.toBuilder() - .addHeader("Authorization", "Bearer " + delegatePkiResponse.getAccessToken()).build()); + AuthenticateResponse resp = restClient.security() + .authenticate( + RequestOptions.DEFAULT.toBuilder().addHeader("Authorization", "Bearer " + delegatePkiResponse.getAccessToken()).build() + ); User user = resp.getUser(); assertThat(user, is(notNullValue())); assertThat(user.getUsername(), is("Elasticsearch Test Client")); @@ -301,10 +355,10 @@ public void testDelegatePkiWithRoleMapping() throws Exception { assertThat(authnRealm.getType(), is("pki")); assertThat(resp.getAuthenticationType(), is("token")); // delete role mappings for delegated PKI - restClient.security().deleteRoleMapping(new DeleteRoleMappingRequest("role_by_delegated_user", RefreshPolicy.IMMEDIATE), - testUserOptions); - restClient.security().deleteRoleMapping(new DeleteRoleMappingRequest("role_by_delegated_realm", RefreshPolicy.IMMEDIATE), - testUserOptions); + restClient.security() + .deleteRoleMapping(new DeleteRoleMappingRequest("role_by_delegated_user", RefreshPolicy.IMMEDIATE), testUserOptions); + restClient.security() + .deleteRoleMapping(new DeleteRoleMappingRequest("role_by_delegated_realm", RefreshPolicy.IMMEDIATE), testUserOptions); } } @@ -313,25 +367,42 @@ public void testIncorrectCertChain() throws Exception { X509Certificate intermediateCA = readCertForPkiDelegation("testIntermediateCA.crt"); X509Certificate bogusCertificate = readCertForPkiDelegation("bogus.crt"); RequestOptions.Builder optionsBuilder = RequestOptions.DEFAULT.toBuilder(); - optionsBuilder.addHeader("Authorization", basicAuthHeaderValue(SecuritySettingsSource.TEST_USER_NAME, - new SecureString(SecuritySettingsSourceField.TEST_PASSWORD.toCharArray()))); + optionsBuilder.addHeader( + "Authorization", + basicAuthHeaderValue( + SecuritySettingsSource.TEST_USER_NAME, + new SecureString(SecuritySettingsSourceField.TEST_PASSWORD.toCharArray()) + ) + ); try (RestHighLevelClient restClient = new TestRestHighLevelClient()) { // incomplete cert chain DelegatePkiAuthenticationRequest delegatePkiRequest1 = new DelegatePkiAuthenticationRequest(Arrays.asList(clientCertificate)); - ElasticsearchStatusException e1 = expectThrows(ElasticsearchStatusException.class, - () -> restClient.security().delegatePkiAuthentication(delegatePkiRequest1, optionsBuilder.build())); - assertThat(e1.getMessage(), is("Elasticsearch exception [type=security_exception, reason=unable to authenticate user" - + " [O=org, OU=Elasticsearch, CN=Elasticsearch Test Client] for action [cluster:admin/xpack/security/delegate_pki]]")); + ElasticsearchStatusException e1 = expectThrows( + ElasticsearchStatusException.class, + () -> restClient.security().delegatePkiAuthentication(delegatePkiRequest1, optionsBuilder.build()) + ); + assertThat( + e1.getMessage(), + is( + "Elasticsearch exception [type=security_exception, reason=unable to authenticate user" + + " [O=org, OU=Elasticsearch, CN=Elasticsearch Test Client] for action [cluster:admin/xpack/security/delegate_pki]]" + ) + ); // swapped order DelegatePkiAuthenticationRequest delegatePkiRequest2 = new DelegatePkiAuthenticationRequest( - Arrays.asList(intermediateCA, clientCertificate)); - ValidationException e2 = expectThrows(ValidationException.class, - () -> restClient.security().delegatePkiAuthentication(delegatePkiRequest2, optionsBuilder.build())); + Arrays.asList(intermediateCA, clientCertificate) + ); + ValidationException e2 = expectThrows( + ValidationException.class, + () -> restClient.security().delegatePkiAuthentication(delegatePkiRequest2, optionsBuilder.build()) + ); assertThat(e2.getMessage(), is("Validation Failed: 1: certificates chain must be an ordered chain;")); // bogus certificate DelegatePkiAuthenticationRequest delegatePkiRequest3 = new DelegatePkiAuthenticationRequest(Arrays.asList(bogusCertificate)); - ElasticsearchStatusException e3 = expectThrows(ElasticsearchStatusException.class, - () -> restClient.security().delegatePkiAuthentication(delegatePkiRequest3, optionsBuilder.build())); + ElasticsearchStatusException e3 = expectThrows( + ElasticsearchStatusException.class, + () -> restClient.security().delegatePkiAuthentication(delegatePkiRequest3, optionsBuilder.build()) + ); assertThat(e3.getMessage(), startsWith("Elasticsearch exception [type=security_exception, reason=unable to authenticate user")); } } diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/pki/PkiAuthenticationTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/pki/PkiAuthenticationTests.java index 65e001368dc00..b7e1cca8b6e77 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/pki/PkiAuthenticationTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/pki/PkiAuthenticationTests.java @@ -20,9 +20,6 @@ import org.elasticsearch.xpack.core.common.socket.SocketAccess; import org.elasticsearch.xpack.core.ssl.CertParsingUtils; -import javax.net.ssl.KeyManager; -import javax.net.ssl.SSLContext; -import javax.net.ssl.TrustManager; import java.net.InetSocketAddress; import java.nio.file.Path; import java.security.SecureRandom; @@ -31,6 +28,10 @@ import java.util.Locale; import java.util.stream.Collectors; +import javax.net.ssl.KeyManager; +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManager; + import static org.elasticsearch.test.SecuritySettingsSource.addSSLSettingsForNodePEMFiles; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.is; @@ -51,23 +52,26 @@ protected Settings nodeSettings() { ? SslClientAuthenticationMode.REQUIRED : SslClientAuthenticationMode.OPTIONAL; - Settings.Builder builder = Settings.builder() - .put(super.nodeSettings()); + Settings.Builder builder = Settings.builder().put(super.nodeSettings()); addSSLSettingsForNodePEMFiles(builder, "xpack.security.http.", true); builder.put("xpack.security.http.ssl.enabled", true) .put("xpack.security.http.ssl.client_authentication", clientAuth) .put("xpack.security.authc.realms.file.file.order", "0") .put("xpack.security.authc.realms.pki.pki1.order", "2") - .putList("xpack.security.authc.realms.pki.pki1.certificate_authorities", - getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testclient.crt").toString()) + .putList( + "xpack.security.authc.realms.pki.pki1.certificate_authorities", + getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testclient.crt").toString() + ) .put("xpack.security.authc.realms.pki.pki1.files.role_mapping", getDataPath("role_mapping.yml")) .put("xpack.security.authc.realms.pki.pki1.files.role_mapping", getDataPath("role_mapping.yml")) // pki1 never authenticates because of the principal pattern .put("xpack.security.authc.realms.pki.pki1.username_pattern", "CN=(MISMATCH.*?)(?:,|$)") .put("xpack.security.authc.realms.pki.pki2.order", "3") - .putList("xpack.security.authc.realms.pki.pki2.certificate_authorities", + .putList( + "xpack.security.authc.realms.pki.pki2.certificate_authorities", getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt").toString(), - getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode_ec.crt").toString()) + getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode_ec.crt").toString() + ) .put("xpack.security.authc.realms.pki.pki2.files.role_mapping", getDataPath("role_mapping.yml")); return builder.build(); } @@ -84,12 +88,16 @@ protected boolean enableWarningsCheck() { } public void testRestAuthenticationViaPki() throws Exception { - SSLContext context = getRestSSLContext("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.pem", + SSLContext context = getRestSSLContext( + "/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.pem", "testnode", "/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt", - Arrays.asList("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testclient.crt", + Arrays.asList( + "/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testclient.crt", "/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt", - "/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode_ec.crt")); + "/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode_ec.crt" + ) + ); try (CloseableHttpClient client = HttpClients.custom().setSSLContext(context).build()) { HttpPut put = new HttpPut(getNodeUrl() + "foo"); try (CloseableHttpResponse response = SocketAccess.doPrivileged(() -> client.execute(put))) { @@ -100,11 +108,16 @@ public void testRestAuthenticationViaPki() throws Exception { } public void testRestAuthenticationFailure() throws Exception { - SSLContext context = getRestSSLContext("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testclient.pem", - "testclient", "/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testclient.crt", - Arrays.asList("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testclient.crt", + SSLContext context = getRestSSLContext( + "/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testclient.pem", + "testclient", + "/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testclient.crt", + Arrays.asList( + "/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testclient.crt", "/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt", - "/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode_ec.crt")); + "/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode_ec.crt" + ) + ); try (CloseableHttpClient client = HttpClients.custom().setSSLContext(context).build()) { HttpPut put = new HttpPut(getNodeUrl() + "foo"); try (CloseableHttpResponse response = SocketAccess.doPrivileged(() -> client.execute(put))) { @@ -125,8 +138,9 @@ private SSLContext getRestSSLContext(String keyPath, String password, String cer } private String getNodeUrl() { - TransportAddress transportAddress = randomFrom(node().injector().getInstance(HttpServerTransport.class) - .boundAddress().boundAddresses()); + TransportAddress transportAddress = randomFrom( + node().injector().getInstance(HttpServerTransport.class).boundAddress().boundAddresses() + ); final InetSocketAddress inetSocketAddress = transportAddress.address(); return String.format(Locale.ROOT, "https://%s/", NetworkAddress.format(inetSocketAddress)); } diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/pki/PkiOptionalClientAuthTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/pki/PkiOptionalClientAuthTests.java index 87a4b1922c9e6..07f35e499d43a 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/pki/PkiOptionalClientAuthTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/pki/PkiOptionalClientAuthTests.java @@ -21,15 +21,15 @@ import org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken; import org.junit.BeforeClass; -import javax.net.ssl.SSLContext; -import javax.net.ssl.TrustManagerFactory; - import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; import java.security.KeyStore; import java.security.SecureRandom; +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManagerFactory; + import static org.hamcrest.Matchers.is; public class PkiOptionalClientAuthTests extends SecuritySingleNodeTestCase { @@ -47,27 +47,34 @@ protected boolean addMockHttpTransport() { } protected Settings nodeSettings() { - String randomClientPortRange = randomClientPort + "-" + (randomClientPort+100); + String randomClientPortRange = randomClientPort + "-" + (randomClientPort + 100); Settings.Builder builder = Settings.builder() .put(super.nodeSettings()) .put("xpack.security.http.ssl.enabled", true) .put("xpack.security.http.ssl.client_authentication", SslClientAuthenticationMode.OPTIONAL) - .put("xpack.security.http.ssl.key", - getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.pem")) - .put("xpack.security.http.ssl.certificate", - getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt")) + .put("xpack.security.http.ssl.key", getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.pem")) + .put( + "xpack.security.http.ssl.certificate", + getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt") + ) .put("xpack.security.authc.realms.file.file.order", "0") .put("xpack.security.authc.realms.pki.pki1.order", "2") - .put("xpack.security.authc.realms.pki.pki1.truststore.path", - getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/truststore-testnode-only.jks")) + .put( + "xpack.security.authc.realms.pki.pki1.truststore.path", + getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/truststore-testnode-only.jks") + ) .put("xpack.security.authc.realms.pki.pki1.files.role_mapping", getDataPath("role_mapping.yml")) .put("transport.profiles.want_client_auth.port", randomClientPortRange) .put("transport.profiles.want_client_auth.bind_host", "localhost") - .put("transport.profiles.want_client_auth.xpack.security.ssl.key", - getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.pem")) - .put("transport.profiles.want_client_auth.xpack.security.ssl.certificate", - getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt")) + .put( + "transport.profiles.want_client_auth.xpack.security.ssl.key", + getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.pem") + ) + .put( + "transport.profiles.want_client_auth.xpack.security.ssl.certificate", + getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt") + ) .put("transport.profiles.want_client_auth.xpack.security.ssl.client_authentication", SslClientAuthenticationMode.OPTIONAL); SecuritySettingsSource.addSecureSettings(builder, secureSettings -> { @@ -92,8 +99,13 @@ public void testRestClientWithoutClientCertificate() throws Exception { Request request = new Request("GET", "_nodes"); RequestOptions.Builder options = request.getOptions().toBuilder(); - options.addHeader("Authorization", UsernamePasswordToken.basicAuthHeaderValue(SecuritySettingsSource.TEST_USER_NAME, - new SecureString(SecuritySettingsSourceField.TEST_PASSWORD.toCharArray()))); + options.addHeader( + "Authorization", + UsernamePasswordToken.basicAuthHeaderValue( + SecuritySettingsSource.TEST_USER_NAME, + new SecureString(SecuritySettingsSourceField.TEST_PASSWORD.toCharArray()) + ) + ); request.setOptions(options); Response response = restClient.performRequest(request); assertThat(response.getStatusLine().getStatusCode(), is(200)); diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/service/ServiceAccountSingleNodeTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/service/ServiceAccountSingleNodeTests.java index d0e1394bda1bf..7af1a7372f8ec 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/service/ServiceAccountSingleNodeTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/service/ServiceAccountSingleNodeTests.java @@ -48,22 +48,17 @@ public class ServiceAccountSingleNodeTests extends SecuritySingleNodeTestCase { @Override protected String configUsers() { - return super.configUsers() - + SERVICE_ACCOUNT_MANAGER_NAME + ":" + TEST_PASSWORD_HASHED + "\n"; + return super.configUsers() + SERVICE_ACCOUNT_MANAGER_NAME + ":" + TEST_PASSWORD_HASHED + "\n"; } @Override protected String configRoles() { - return super.configRoles() - + SERVICE_ACCOUNT_MANAGER_NAME + ":\n" - + " cluster:\n" - + " - 'manage_service_account'\n"; + return super.configRoles() + SERVICE_ACCOUNT_MANAGER_NAME + ":\n" + " cluster:\n" + " - 'manage_service_account'\n"; } @Override protected String configUsersRoles() { - return super.configUsersRoles() - + SERVICE_ACCOUNT_MANAGER_NAME + ":" + SERVICE_ACCOUNT_MANAGER_NAME + "\n"; + return super.configUsersRoles() + SERVICE_ACCOUNT_MANAGER_NAME + ":" + SERVICE_ACCOUNT_MANAGER_NAME + "\n"; } @Override @@ -93,12 +88,12 @@ protected String configServiceTokens() { public void testAuthenticateWithServiceFileToken() { final AuthenticateRequest authenticateRequest = new AuthenticateRequest("elastic/fleet-server"); - final AuthenticateResponse authenticateResponse = - createServiceAccountClient().execute(AuthenticateAction.INSTANCE, authenticateRequest).actionGet(); + final AuthenticateResponse authenticateResponse = createServiceAccountClient().execute( + AuthenticateAction.INSTANCE, + authenticateRequest + ).actionGet(); final String nodeName = node().settings().get(Node.NODE_NAME_SETTING.getKey()); - assertThat(authenticateResponse.authentication(), equalTo( - getExpectedAuthentication("token1", "file") - )); + assertThat(authenticateResponse.authentication(), equalTo(getExpectedAuthentication("token1", "file"))); } public void testApiServiceAccountToken() { @@ -108,16 +103,23 @@ public void testApiServiceAccountToken() { assertThat(cache.count(), equalTo(0)); final AuthenticateRequest authenticateRequest = new AuthenticateRequest("elastic/fleet-server"); - final AuthenticateResponse authenticateResponse = createServiceAccountClient(secretValue1.toString()) - .execute(AuthenticateAction.INSTANCE, authenticateRequest).actionGet(); + final AuthenticateResponse authenticateResponse = createServiceAccountClient(secretValue1.toString()).execute( + AuthenticateAction.INSTANCE, + authenticateRequest + ).actionGet(); assertThat(authenticateResponse.authentication(), equalTo(getExpectedAuthentication("api-token-1", "index"))); // cache is populated after authenticate assertThat(cache.count(), equalTo(1)); - final DeleteServiceAccountTokenRequest deleteServiceAccountTokenRequest = - new DeleteServiceAccountTokenRequest("elastic", "fleet-server", "api-token-1"); - final DeleteServiceAccountTokenResponse deleteServiceAccountTokenResponse = createServiceAccountManagerClient() - .execute(DeleteServiceAccountTokenAction.INSTANCE, deleteServiceAccountTokenRequest).actionGet(); + final DeleteServiceAccountTokenRequest deleteServiceAccountTokenRequest = new DeleteServiceAccountTokenRequest( + "elastic", + "fleet-server", + "api-token-1" + ); + final DeleteServiceAccountTokenResponse deleteServiceAccountTokenResponse = createServiceAccountManagerClient().execute( + DeleteServiceAccountTokenAction.INSTANCE, + deleteServiceAccountTokenRequest + ).actionGet(); assertThat(deleteServiceAccountTokenResponse.found(), is(true)); // cache is cleared after token deletion assertThat(cache.count(), equalTo(0)); @@ -149,8 +151,8 @@ public void testClearCache() { authenticateWithApiToken("api-token-2", secret2); assertThat(cache.count(), equalTo(2)); - final ClearSecurityCacheRequest clearSecurityCacheRequest2 - = new ClearSecurityCacheRequest().cacheName("service").keys("elastic/fleet-server/api-token-" + randomFrom("1", "2")); + final ClearSecurityCacheRequest clearSecurityCacheRequest2 = new ClearSecurityCacheRequest().cacheName("service") + .keys("elastic/fleet-server/api-token-" + randomFrom("1", "2")); final PlainActionFuture future2 = new PlainActionFuture<>(); client().execute(ClearSecurityCacheAction.INSTANCE, clearSecurityCacheRequest2, future2); assertThat(future2.actionGet().failures().isEmpty(), is(true)); @@ -158,8 +160,9 @@ public void testClearCache() { } private Client createServiceAccountManagerClient() { - return client().filterWithHeader(Map.of("Authorization", - basicAuthHeaderValue(SERVICE_ACCOUNT_MANAGER_NAME, new SecureString(TEST_PASSWORD.toCharArray())))); + return client().filterWithHeader( + Map.of("Authorization", basicAuthHeaderValue(SERVICE_ACCOUNT_MANAGER_NAME, new SecureString(TEST_PASSWORD.toCharArray()))) + ); } private Client createServiceAccountClient() { @@ -173,29 +176,42 @@ private Client createServiceAccountClient(String bearerString) { private Authentication getExpectedAuthentication(String tokenName, String tokenSource) { final String nodeName = node().settings().get(Node.NODE_NAME_SETTING.getKey()); return new Authentication( - new User("elastic/fleet-server", Strings.EMPTY_ARRAY, "Service account - elastic/fleet-server", null, - Map.of("_elastic_service_account", true), true), + new User( + "elastic/fleet-server", + Strings.EMPTY_ARRAY, + "Service account - elastic/fleet-server", + null, + Map.of("_elastic_service_account", true), + true + ), new Authentication.RealmRef("_service_account", "_service_account", nodeName), - null, Version.CURRENT, Authentication.AuthenticationType.TOKEN, + null, + Version.CURRENT, + Authentication.AuthenticationType.TOKEN, Map.of("_token_name", tokenName, "_token_source", tokenSource) ); } private SecureString createApiServiceToken(String tokenName) { - final CreateServiceAccountTokenRequest createServiceAccountTokenRequest = - new CreateServiceAccountTokenRequest("elastic", "fleet-server", tokenName); - final CreateServiceAccountTokenResponse createServiceAccountTokenResponse = - createServiceAccountManagerClient().execute( - CreateServiceAccountTokenAction.INSTANCE, createServiceAccountTokenRequest).actionGet(); + final CreateServiceAccountTokenRequest createServiceAccountTokenRequest = new CreateServiceAccountTokenRequest( + "elastic", + "fleet-server", + tokenName + ); + final CreateServiceAccountTokenResponse createServiceAccountTokenResponse = createServiceAccountManagerClient().execute( + CreateServiceAccountTokenAction.INSTANCE, + createServiceAccountTokenRequest + ).actionGet(); assertThat(createServiceAccountTokenResponse.getName(), equalTo(tokenName)); return createServiceAccountTokenResponse.getValue(); } private void authenticateWithApiToken(String tokenName, SecureString secret) { final AuthenticateRequest authenticateRequest = new AuthenticateRequest("elastic/fleet-server"); - final AuthenticateResponse authenticateResponse = - createServiceAccountClient(secret.toString()) - .execute(AuthenticateAction.INSTANCE, authenticateRequest).actionGet(); + final AuthenticateResponse authenticateResponse = createServiceAccountClient(secret.toString()).execute( + AuthenticateAction.INSTANCE, + authenticateRequest + ).actionGet(); assertThat(authenticateResponse.authentication(), equalTo(getExpectedAuthentication(tokenName, "index"))); } } diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/AnalyzeTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/AnalyzeTests.java index 359fb934ac05b..c05855563197b 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/AnalyzeTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/AnalyzeTests.java @@ -21,31 +21,28 @@ public class AnalyzeTests extends SecurityIntegTestCase { @Override protected String configUsers() { - final String usersPasswdHashed = - new String(getFastStoredHashAlgoForTests().hash(SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)); - return super.configUsers() + - "analyze_indices:" + usersPasswdHashed + "\n" + - "analyze_cluster:" + usersPasswdHashed + "\n"; + final String usersPasswdHashed = new String( + getFastStoredHashAlgoForTests().hash(SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING) + ); + return super.configUsers() + "analyze_indices:" + usersPasswdHashed + "\n" + "analyze_cluster:" + usersPasswdHashed + "\n"; } @Override protected String configUsersRoles() { - return super.configUsersRoles() + - "analyze_indices:analyze_indices\n" + - "analyze_cluster:analyze_cluster\n"; + return super.configUsersRoles() + "analyze_indices:analyze_indices\n" + "analyze_cluster:analyze_cluster\n"; } @Override protected String configRoles() { - return super.configRoles()+ "\n" + - //role that has analyze indices privileges only - "analyze_indices:\n" + - " indices:\n" + - " - names: 'test_*'\n" + - " privileges: [ 'indices:admin/analyze' ]\n" + - "analyze_cluster:\n" + - " cluster:\n" + - " - cluster:admin/analyze\n"; + return super.configRoles() + "\n" + + // role that has analyze indices privileges only + "analyze_indices:\n" + + " indices:\n" + + " - names: 'test_*'\n" + + " privileges: [ 'indices:admin/analyze' ]\n" + + "analyze_cluster:\n" + + " cluster:\n" + + " - cluster:admin/analyze\n"; } public void testAnalyzeWithIndices() { @@ -55,35 +52,61 @@ public void testAnalyzeWithIndices() { createIndex("test_1"); ensureGreen(); - //ok: user has permissions for analyze on test_* + // ok: user has permissions for analyze on test_* SecureString passwd = SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING; client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("analyze_indices", passwd))) - .admin().indices().prepareAnalyze("this is my text").setIndex("test_1").setAnalyzer("standard").get(); + .admin() + .indices() + .prepareAnalyze("this is my text") + .setIndex("test_1") + .setAnalyzer("standard") + .get(); - //fails: user doesn't have permissions for analyze on index non_authorized - assertThrowsAuthorizationException(client().filterWithHeader( - Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("analyze_indices", passwd))) - .admin().indices().prepareAnalyze("this is my text").setIndex("non_authorized").setAnalyzer("standard")::get, - AnalyzeAction.NAME, "analyze_indices"); + // fails: user doesn't have permissions for analyze on index non_authorized + assertThrowsAuthorizationException( + client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("analyze_indices", passwd))) + .admin() + .indices() + .prepareAnalyze("this is my text") + .setIndex("non_authorized") + .setAnalyzer("standard")::get, + AnalyzeAction.NAME, + "analyze_indices" + ); - //fails: user doesn't have permissions for cluster level analyze - assertThrowsAuthorizationException(client().filterWithHeader( - Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("analyze_indices", passwd))) - .admin().indices().prepareAnalyze("this is my text").setAnalyzer("standard")::get, - "cluster:admin/analyze", "analyze_indices"); + // fails: user doesn't have permissions for cluster level analyze + assertThrowsAuthorizationException( + client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("analyze_indices", passwd))) + .admin() + .indices() + .prepareAnalyze("this is my text") + .setAnalyzer("standard")::get, + "cluster:admin/analyze", + "analyze_indices" + ); } public void testAnalyzeWithoutIndices() { - //this test tries to execute different analyze api variants from a user that has analyze privileges only at cluster level + // this test tries to execute different analyze api variants from a user that has analyze privileges only at cluster level SecureString passwd = SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING; - //fails: user doesn't have permissions for analyze on index test_1 - assertThrowsAuthorizationException(client().filterWithHeader( - Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("analyze_cluster", passwd))) - .admin().indices().prepareAnalyze("this is my text").setIndex("test_1").setAnalyzer("standard")::get, - AnalyzeAction.NAME, "analyze_cluster"); + // fails: user doesn't have permissions for analyze on index test_1 + assertThrowsAuthorizationException( + client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("analyze_cluster", passwd))) + .admin() + .indices() + .prepareAnalyze("this is my text") + .setIndex("test_1") + .setAnalyzer("standard")::get, + AnalyzeAction.NAME, + "analyze_cluster" + ); client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("analyze_cluster", passwd))) - .admin().indices().prepareAnalyze("this is my text").setAnalyzer("standard").get(); + .admin() + .indices() + .prepareAnalyze("this is my text") + .setAnalyzer("standard") + .get(); } } diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/IndexAliasesTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/IndexAliasesTests.java index d26f0930cb703..639e5c446d20e 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/IndexAliasesTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/IndexAliasesTests.java @@ -39,239 +39,348 @@ public class IndexAliasesTests extends SecurityIntegTestCase { @Override protected String configUsers() { - final String usersPasswdHashed = - new String(getFastStoredHashAlgoForTests().hash(SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)); - return super.configUsers() + - "create_only:" + usersPasswdHashed + "\n" + - "all_on_test:" + usersPasswdHashed + "\n" + - "create_test_aliases_test:" + usersPasswdHashed + "\n" + - "create_test_aliases_alias:" + usersPasswdHashed + "\n" + - "create_test_aliases_test_alias:" + usersPasswdHashed + "\n" + - "aliases_only:" + usersPasswdHashed + "\n"; + final String usersPasswdHashed = new String( + getFastStoredHashAlgoForTests().hash(SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING) + ); + return super.configUsers() + + "create_only:" + + usersPasswdHashed + + "\n" + + "all_on_test:" + + usersPasswdHashed + + "\n" + + "create_test_aliases_test:" + + usersPasswdHashed + + "\n" + + "create_test_aliases_alias:" + + usersPasswdHashed + + "\n" + + "create_test_aliases_test_alias:" + + usersPasswdHashed + + "\n" + + "aliases_only:" + + usersPasswdHashed + + "\n"; } @Override protected String configUsersRoles() { - return super.configUsersRoles() + - "create_only:create_only\n" + - "all_on_test:all_on_test\n" + - "create_test_aliases_test:create_test_aliases_test\n" + - "create_test_aliases_alias:create_test_aliases_alias\n" + - "create_test_aliases_test_alias:create_test_aliases_test_alias\n" + - "aliases_only:aliases_only\n"; + return super.configUsersRoles() + + "create_only:create_only\n" + + "all_on_test:all_on_test\n" + + "create_test_aliases_test:create_test_aliases_test\n" + + "create_test_aliases_alias:create_test_aliases_alias\n" + + "create_test_aliases_test_alias:create_test_aliases_test_alias\n" + + "aliases_only:aliases_only\n"; } @Override protected String configRoles() { return super.configRoles() + "\n" + - //role that has create index only privileges - "create_only:\n" + - " indices:\n" + - " - names: '*'\n" + - " privileges: [ create_index ]\n" + - "all_on_test:\n" + - " indices:\n" + - " - names: 'test_*'\n" + - " privileges: [ all ]\n" + - //role that has create index and manage_aliases on test_*, not enough to manage_aliases aliases outside of test_* namespace - "create_test_aliases_test:\n" + - " indices:\n" + - " - names: 'test_*'\n" + - " privileges: [ create_index, 'indices:admin/aliases*' ]\n" + - //role that has create index on test_* and manage_aliases on alias_*, can't create aliases pointing to test_* though - "create_test_aliases_alias:\n" + - " indices:\n" + - " - names: 'test_*'\n" + - " privileges: [ create_index ]\n" + - " - names: 'alias_*'\n" + - " privileges: [ 'indices:admin/aliases*' ]\n" + - //role that has create index on test_* and manage_aliases on both alias_* and test_* - "create_test_aliases_test_alias:\n" + - " indices:\n" + - " - names: 'test_*'\n" + - " privileges: [ create_index ]\n" + - " - names: [ 'alias_*', 'test_*' ]\n" + - " privileges: [ 'indices:admin/aliases*' ]\n" + - //role that has manage_aliases only on both test_* and alias_* - "aliases_only:\n" + - " indices:\n" + - " - names: [ 'alias_*', 'test_*']\n" + - " privileges: [ 'indices:admin/aliases*' ]\n"; + // role that has create index only privileges + "create_only:\n" + + " indices:\n" + + " - names: '*'\n" + + " privileges: [ create_index ]\n" + + "all_on_test:\n" + + " indices:\n" + + " - names: 'test_*'\n" + + " privileges: [ all ]\n" + + + // role that has create index and manage_aliases on test_*, not enough to manage_aliases aliases outside of test_* namespace + "create_test_aliases_test:\n" + + " indices:\n" + + " - names: 'test_*'\n" + + " privileges: [ create_index, 'indices:admin/aliases*' ]\n" + + + // role that has create index on test_* and manage_aliases on alias_*, can't create aliases pointing to test_* though + "create_test_aliases_alias:\n" + + " indices:\n" + + " - names: 'test_*'\n" + + " privileges: [ create_index ]\n" + + " - names: 'alias_*'\n" + + " privileges: [ 'indices:admin/aliases*' ]\n" + + + // role that has create index on test_* and manage_aliases on both alias_* and test_* + "create_test_aliases_test_alias:\n" + + " indices:\n" + + " - names: 'test_*'\n" + + " privileges: [ create_index ]\n" + + " - names: [ 'alias_*', 'test_*' ]\n" + + " privileges: [ 'indices:admin/aliases*' ]\n" + + + // role that has manage_aliases only on both test_* and alias_* + "aliases_only:\n" + + " indices:\n" + + " - names: [ 'alias_*', 'test_*']\n" + + " privileges: [ 'indices:admin/aliases*' ]\n"; } @Before public void createBogusIndex() { - //randomly create an index with two aliases from user admin, to make sure it doesn't affect any of the test results - assertAcked(client().admin().indices().prepareCreate("bogus_index_1").addAlias(new Alias("bogus_alias_1")) - .addAlias(new Alias("bogus_alias_2"))); + // randomly create an index with two aliases from user admin, to make sure it doesn't affect any of the test results + assertAcked( + client().admin() + .indices() + .prepareCreate("bogus_index_1") + .addAlias(new Alias("bogus_alias_1")) + .addAlias(new Alias("bogus_alias_2")) + ); } public void testCreateIndexThenAliasesCreateOnlyPermission() { - //user has create permission only: allows to create indices, manage_aliases is required to add/remove aliases - Map headers = Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("create_only", - SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)); + // user has create permission only: allows to create indices, manage_aliases is required to add/remove aliases + Map headers = Collections.singletonMap( + BASIC_AUTH_HEADER, + basicAuthHeaderValue("create_only", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING) + ); final Client client = client().filterWithHeader(headers); assertAcked(client.admin().indices().prepareCreate("test_1").get()); assertThrowsAuthorizationException( - client.admin().indices().prepareAliases().addAlias("test_1", "test_alias")::get, - IndicesAliasesAction.NAME, "create_only"); + client.admin().indices().prepareAliases().addAlias("test_1", "test_alias")::get, + IndicesAliasesAction.NAME, + "create_only" + ); - assertThrowsAuthorizationException(client.admin().indices().prepareAliases() - .addAlias("test_*", "test_alias")::get, IndicesAliasesAction.NAME, "create_only"); + assertThrowsAuthorizationException( + client.admin().indices().prepareAliases().addAlias("test_*", "test_alias")::get, + IndicesAliasesAction.NAME, + "create_only" + ); } public void testCreateIndexAndAliasesCreateOnlyPermission() { - //user has create permission only: allows to create indices, manage_aliases is required to add aliases although they are part of + // user has create permission only: allows to create indices, manage_aliases is required to add aliases although they are part of // the same create index request - Map headers = Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("create_only", - SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)); + Map headers = Collections.singletonMap( + BASIC_AUTH_HEADER, + basicAuthHeaderValue("create_only", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING) + ); assertThrowsAuthorizationException( - client(headers).admin().indices().prepareCreate("test_1").addAlias(new Alias("test_2"))::get, - IndicesAliasesAction.NAME, "create_only"); + client(headers).admin().indices().prepareCreate("test_1").addAlias(new Alias("test_2"))::get, + IndicesAliasesAction.NAME, + "create_only" + ); } public void testDeleteAliasesCreateOnlyPermission() { - //user has create permission only: allows to create indices, manage_aliases is required to add/remove aliases - Map headers = Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("create_only", - SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)); + // user has create permission only: allows to create indices, manage_aliases is required to add/remove aliases + Map headers = Collections.singletonMap( + BASIC_AUTH_HEADER, + basicAuthHeaderValue("create_only", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING) + ); final Client client = client().filterWithHeader(headers); assertThrowsAuthorizationException( - client.admin().indices().prepareAliases().removeAlias("test_1", "alias_1")::get, - IndicesAliasesAction.NAME, "create_only"); + client.admin().indices().prepareAliases().removeAlias("test_1", "alias_1")::get, + IndicesAliasesAction.NAME, + "create_only" + ); - assertThrowsAuthorizationException(client.admin().indices().prepareAliases() - .removeAlias("test_1", "alias_*")::get, IndicesAliasesAction.NAME, "create_only"); + assertThrowsAuthorizationException( + client.admin().indices().prepareAliases().removeAlias("test_1", "alias_*")::get, + IndicesAliasesAction.NAME, + "create_only" + ); - assertThrowsAuthorizationException(client.admin().indices().prepareAliases() - .removeAlias("test_1", "_all")::get, IndicesAliasesAction.NAME, "create_only"); + assertThrowsAuthorizationException( + client.admin().indices().prepareAliases().removeAlias("test_1", "_all")::get, + IndicesAliasesAction.NAME, + "create_only" + ); } public void testGetAliasesCreateOnlyPermissionStrict() { - //user has create permission only: allows to create indices, manage_aliases is required to retrieve aliases though - Map headers = Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("create_only", - SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)); + // user has create permission only: allows to create indices, manage_aliases is required to retrieve aliases though + Map headers = Collections.singletonMap( + BASIC_AUTH_HEADER, + basicAuthHeaderValue("create_only", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING) + ); final Client client = client().filterWithHeader(headers); - assertThrowsAuthorizationException(client.admin().indices().prepareGetAliases("test_1") - .setIndices("test_1").setIndicesOptions(IndicesOptions.strictExpand())::get, GetAliasesAction.NAME, "create_only"); - - assertThrowsAuthorizationException(client - .admin().indices().prepareGetAliases("_all") - .setIndices("test_1").setIndicesOptions(IndicesOptions.strictExpand())::get, GetAliasesAction.NAME, "create_only"); + assertThrowsAuthorizationException( + client.admin().indices().prepareGetAliases("test_1").setIndices("test_1").setIndicesOptions(IndicesOptions.strictExpand())::get, + GetAliasesAction.NAME, + "create_only" + ); - assertThrowsAuthorizationException(client.admin().indices() - .prepareGetAliases().setIndices("test_1").setIndicesOptions(IndicesOptions.strictExpand())::get, - GetAliasesAction.NAME, "create_only"); + assertThrowsAuthorizationException( + client.admin().indices().prepareGetAliases("_all").setIndices("test_1").setIndicesOptions(IndicesOptions.strictExpand())::get, + GetAliasesAction.NAME, + "create_only" + ); - assertThrowsAuthorizationException(client.admin().indices().prepareGetAliases("test_alias") - .setIndices("test_*").setIndicesOptions(IndicesOptions.strictExpand())::get, GetAliasesAction.NAME, "create_only"); + assertThrowsAuthorizationException( + client.admin().indices().prepareGetAliases().setIndices("test_1").setIndicesOptions(IndicesOptions.strictExpand())::get, + GetAliasesAction.NAME, + "create_only" + ); - //this throws exception no matter what the indices options are because the aliases part cannot be resolved to any alias - //and there is no way to "allow_no_aliases" like we can do with indices. - assertThrowsAuthorizationException(client.admin().indices().prepareGetAliases()::get, - GetAliasesAction.NAME, "create_only"); + assertThrowsAuthorizationException( + client.admin() + .indices() + .prepareGetAliases("test_alias") + .setIndices("test_*") + .setIndicesOptions(IndicesOptions.strictExpand())::get, + GetAliasesAction.NAME, + "create_only" + ); + + // this throws exception no matter what the indices options are because the aliases part cannot be resolved to any alias + // and there is no way to "allow_no_aliases" like we can do with indices. + assertThrowsAuthorizationException(client.admin().indices().prepareGetAliases()::get, GetAliasesAction.NAME, "create_only"); } public void testGetAliasesCreateOnlyPermissionIgnoreUnavailable() { - //user has create permission only: allows to create indices, manage_aliases is required to retrieve aliases though - Map headers = Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("create_only", - SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)); + // user has create permission only: allows to create indices, manage_aliases is required to retrieve aliases though + Map headers = Collections.singletonMap( + BASIC_AUTH_HEADER, + basicAuthHeaderValue("create_only", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING) + ); final Client client = client().filterWithHeader(headers); - assertThrowsAuthorizationException(client.admin().indices().prepareGetAliases("test_1") - .setIndices("test_1").setIndicesOptions(IndicesOptions.lenientExpandOpen())::get, GetAliasesAction.NAME, "create_only"); + assertThrowsAuthorizationException( + client.admin() + .indices() + .prepareGetAliases("test_1") + .setIndices("test_1") + .setIndicesOptions(IndicesOptions.lenientExpandOpen())::get, + GetAliasesAction.NAME, + "create_only" + ); - assertThrowsAuthorizationException(client.admin().indices().prepareGetAliases("_all") - .setIndices("test_1").setIndicesOptions(IndicesOptions.lenientExpandOpen())::get, GetAliasesAction.NAME, "create_only"); + assertThrowsAuthorizationException( + client.admin() + .indices() + .prepareGetAliases("_all") + .setIndices("test_1") + .setIndicesOptions(IndicesOptions.lenientExpandOpen())::get, + GetAliasesAction.NAME, + "create_only" + ); assertThrowsAuthorizationException(client.admin().indices().prepareGetAliases("alias*")::get, GetAliasesAction.NAME, "create_only"); - assertThrowsAuthorizationException(client.admin().indices().prepareGetAliases().setIndices("test_1") - .setIndicesOptions(IndicesOptions.lenientExpandOpen())::get, GetAliasesAction.NAME, "create_only"); + assertThrowsAuthorizationException( + client.admin().indices().prepareGetAliases().setIndices("test_1").setIndicesOptions(IndicesOptions.lenientExpandOpen())::get, + GetAliasesAction.NAME, + "create_only" + ); assertThrowsAuthorizationException( - client.admin().indices().prepareGetAliases("test_alias") - .setIndices("test_*").setIndicesOptions(IndicesOptions.lenientExpandOpen())::get, GetAliasesAction.NAME, "create_only"); + client.admin() + .indices() + .prepareGetAliases("test_alias") + .setIndices("test_*") + .setIndicesOptions(IndicesOptions.lenientExpandOpen())::get, + GetAliasesAction.NAME, + "create_only" + ); assertThrowsAuthorizationException( - client.admin().indices().prepareGetAliases() - .setIndices("test_*").setIndicesOptions(IndicesOptions.lenientExpandOpen())::get, GetAliasesAction.NAME, "create_only"); + client.admin().indices().prepareGetAliases().setIndices("test_*").setIndicesOptions(IndicesOptions.lenientExpandOpen())::get, + GetAliasesAction.NAME, + "create_only" + ); - //this throws exception no matter what the indices options are because the aliases part cannot be resolved to any alias - //and there is no way to "allow_no_aliases" like we can do with indices. - assertThrowsAuthorizationException(client.admin().indices() - .prepareGetAliases().setIndicesOptions(IndicesOptions.lenientExpandOpen())::get, GetAliasesAction.NAME, "create_only"); + // this throws exception no matter what the indices options are because the aliases part cannot be resolved to any alias + // and there is no way to "allow_no_aliases" like we can do with indices. + assertThrowsAuthorizationException( + client.admin().indices().prepareGetAliases().setIndicesOptions(IndicesOptions.lenientExpandOpen())::get, + GetAliasesAction.NAME, + "create_only" + ); } public void testCreateIndexThenAliasesCreateAndAliasesPermission() { - //user has create and manage_aliases permission on test_*. manage_aliases is required to add/remove aliases on both aliases and + // user has create and manage_aliases permission on test_*. manage_aliases is required to add/remove aliases on both aliases and // indices - Map headers = Collections.singletonMap(BASIC_AUTH_HEADER, - basicAuthHeaderValue("create_test_aliases_test", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)); + Map headers = Collections.singletonMap( + BASIC_AUTH_HEADER, + basicAuthHeaderValue("create_test_aliases_test", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING) + ); final Client client = client().filterWithHeader(headers); assertAcked(client.admin().indices().prepareCreate("test_1").get()); - //ok: user has manage_aliases on test_* + // ok: user has manage_aliases on test_* assertAcked(client.admin().indices().prepareAliases().addAlias("test_1", "test_alias").get()); - //ok: user has manage_aliases on test_* + // ok: user has manage_aliases on test_* assertAcked(client.admin().indices().prepareAliases().addAlias("test_*", "test_alias_2").get()); - //fails: user doesn't have manage_aliases on alias_1 - assertThrowsAuthorizationException(client.admin().indices().prepareAliases() - .addAlias("test_1", "alias_1").addAlias("test_1", "test_alias")::get, - IndicesAliasesAction.NAME, "create_test_aliases_test"); + // fails: user doesn't have manage_aliases on alias_1 + assertThrowsAuthorizationException( + client.admin().indices().prepareAliases().addAlias("test_1", "alias_1").addAlias("test_1", "test_alias")::get, + IndicesAliasesAction.NAME, + "create_test_aliases_test" + ); } public void testCreateIndexAndAliasesCreateAndAliasesPermission() { - //user has create and manage_aliases permission on test_*. manage_aliases is required to add/remove aliases on both aliases and + // user has create and manage_aliases permission on test_*. manage_aliases is required to add/remove aliases on both aliases and // indices - //ok: user has manage_aliases on test_* - Map headers = Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("create_test_aliases_test", - SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)); + // ok: user has manage_aliases on test_* + Map headers = Collections.singletonMap( + BASIC_AUTH_HEADER, + basicAuthHeaderValue("create_test_aliases_test", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING) + ); final Client client = client(headers); assertAcked(client.admin().indices().prepareCreate("test_1").addAlias(new Alias("test_alias")).get()); - //fails: user doesn't have manage_aliases on alias_1 - assertThrowsAuthorizationException(client.admin().indices().prepareCreate("test_2") - .addAlias(new Alias("test_alias")).addAlias(new Alias("alias_2"))::get, - IndicesAliasesAction.NAME, "create_test_aliases_test"); + // fails: user doesn't have manage_aliases on alias_1 + assertThrowsAuthorizationException( + client.admin().indices().prepareCreate("test_2").addAlias(new Alias("test_alias")).addAlias(new Alias("alias_2"))::get, + IndicesAliasesAction.NAME, + "create_test_aliases_test" + ); } public void testDeleteAliasesCreateAndAliasesPermission() { - //user has create and manage_aliases permission on test_*. manage_aliases is required to add/remove aliases on both aliases and + // user has create and manage_aliases permission on test_*. manage_aliases is required to add/remove aliases on both aliases and // indices - //ok: user has manage_aliases on test_* - Map headers = Collections.singletonMap(BASIC_AUTH_HEADER, basicAuthHeaderValue("create_test_aliases_test", - SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)); + // ok: user has manage_aliases on test_* + Map headers = Collections.singletonMap( + BASIC_AUTH_HEADER, + basicAuthHeaderValue("create_test_aliases_test", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING) + ); final Client client = client(headers); - assertAcked(client.admin().indices().prepareCreate("test_1").addAlias(new Alias("test_alias_1")) + assertAcked( + client.admin() + .indices() + .prepareCreate("test_1") + .addAlias(new Alias("test_alias_1")) .addAlias(new Alias("test_alias_2")) - .addAlias(new Alias("test_alias_3")).addAlias(new Alias("test_alias_4")).get()); - //ok: user has manage_aliases on test_* + .addAlias(new Alias("test_alias_3")) + .addAlias(new Alias("test_alias_4")) + .get() + ); + // ok: user has manage_aliases on test_* assertAcked(client.admin().indices().prepareAliases().removeAlias("test_1", "test_alias_1").get()); - //ok: user has manage_aliases on test_* + // ok: user has manage_aliases on test_* assertAcked(client.admin().indices().prepareAliases().removeAlias("test_*", "test_alias_2").get()); - //ok: user has manage_aliases on test_* + // ok: user has manage_aliases on test_* assertAcked(client.admin().indices().prepareAliases().removeAlias("test_1", "test_alias_*").get()); { - //fails: all aliases have been deleted, no existing aliases match test_alias_* - AliasesNotFoundException exception = expectThrows(AliasesNotFoundException.class, - client.admin().indices().prepareAliases().removeAlias("test_1", "test_alias_*")::get); + // fails: all aliases have been deleted, no existing aliases match test_alias_* + AliasesNotFoundException exception = expectThrows( + AliasesNotFoundException.class, + client.admin().indices().prepareAliases().removeAlias("test_1", "test_alias_*")::get + ); assertThat(exception.getMessage(), equalTo("aliases [test_alias_*] missing")); } { - //fails: all aliases have been deleted, no existing aliases match _all - AliasesNotFoundException exception = expectThrows(AliasesNotFoundException.class, - client.admin().indices().prepareAliases().removeAlias("test_1", "_all")::get); + // fails: all aliases have been deleted, no existing aliases match _all + AliasesNotFoundException exception = expectThrows( + AliasesNotFoundException.class, + client.admin().indices().prepareAliases().removeAlias("test_1", "_all")::get + ); assertThat(exception.getMessage(), equalTo("aliases [_all] missing")); } @@ -281,175 +390,222 @@ public void testDeleteAliasesCreateAndAliasesPermission() { } assertAcked(client().admin().indices().prepareAliases().addAlias("test_1", "alias_2").get()); - //fails: user doesn't have manage_aliases on alias_1 - assertThrowsAuthorizationException(client.admin().indices().prepareAliases() - .removeAlias("test_1", "alias_1")::get, IndicesAliasesAction.NAME, "create_test_aliases_test"); - - //fails: user doesn't have manage_aliases on alias_1 - assertThrowsAuthorizationException(client.admin().indices().prepareAliases() - .removeAlias("test_1", new String[]{"_all", "alias_1"})::get, IndicesAliasesAction.NAME, "create_test_aliases_test"); + // fails: user doesn't have manage_aliases on alias_1 + assertThrowsAuthorizationException( + client.admin().indices().prepareAliases().removeAlias("test_1", "alias_1")::get, + IndicesAliasesAction.NAME, + "create_test_aliases_test" + ); - AliasesNotFoundException exception = expectThrows(AliasesNotFoundException.class, - client.admin().indices().prepareAliases().removeAlias("test_1", "*")::get); + // fails: user doesn't have manage_aliases on alias_1 + assertThrowsAuthorizationException( + client.admin().indices().prepareAliases().removeAlias("test_1", new String[] { "_all", "alias_1" })::get, + IndicesAliasesAction.NAME, + "create_test_aliases_test" + ); + + AliasesNotFoundException exception = expectThrows( + AliasesNotFoundException.class, + client.admin().indices().prepareAliases().removeAlias("test_1", "*")::get + ); assertThat(exception.getMessage(), equalTo("aliases [*] missing")); } public void testGetAliasesCreateAndAliasesPermission() { - //user has create and manage_aliases permission on test_*. manage_aliases is required to retrieve aliases on both aliases and + // user has create and manage_aliases permission on test_*. manage_aliases is required to retrieve aliases on both aliases and // indices - Map headers = Collections.singletonMap(BASIC_AUTH_HEADER, - basicAuthHeaderValue("create_test_aliases_test", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)); + Map headers = Collections.singletonMap( + BASIC_AUTH_HEADER, + basicAuthHeaderValue("create_test_aliases_test", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING) + ); final Client client = client(headers); assertAcked(client.admin().indices().prepareCreate("test_1").addAlias(new Alias("test_alias")).get()); - //ok: user has manage_aliases on test_* - assertAliases(client.admin().indices().prepareGetAliases().setAliases("test_alias").setIndices("test_1"), - "test_1", "test_alias"); + // ok: user has manage_aliases on test_* + assertAliases(client.admin().indices().prepareGetAliases().setAliases("test_alias").setIndices("test_1"), "test_1", "test_alias"); - //ok: user has manage_aliases on test_*, test_* gets resolved to test_1 - assertAliases(client.admin().indices().prepareGetAliases().setAliases("test_alias").setIndices("test_*"), - "test_1", "test_alias"); + // ok: user has manage_aliases on test_*, test_* gets resolved to test_1 + assertAliases(client.admin().indices().prepareGetAliases().setAliases("test_alias").setIndices("test_*"), "test_1", "test_alias"); - //ok: user has manage_aliases on test_*, empty indices gets resolved to _all indices (thus test_1) - assertAliases(client.admin().indices().prepareGetAliases().setAliases("test_alias"), - "test_1", "test_alias"); + // ok: user has manage_aliases on test_*, empty indices gets resolved to _all indices (thus test_1) + assertAliases(client.admin().indices().prepareGetAliases().setAliases("test_alias"), "test_1", "test_alias"); - //ok: user has manage_aliases on test_*, _all aliases gets resolved to test_alias and empty indices gets resolved to _all + // ok: user has manage_aliases on test_*, _all aliases gets resolved to test_alias and empty indices gets resolved to _all // indices (thus test_1) - assertAliases(client.admin().indices().prepareGetAliases().setAliases("_all").setIndices("test_1"), - "test_1", "test_alias"); + assertAliases(client.admin().indices().prepareGetAliases().setAliases("_all").setIndices("test_1"), "test_1", "test_alias"); - //ok: user has manage_aliases on test_*, empty aliases gets resolved to test_alias and empty indices gets resolved to _all + // ok: user has manage_aliases on test_*, empty aliases gets resolved to test_alias and empty indices gets resolved to _all // indices (thus test_1) - assertAliases(client.admin().indices().prepareGetAliases().setIndices("test_1"), - "test_1", "test_alias"); + assertAliases(client.admin().indices().prepareGetAliases().setIndices("test_1"), "test_1", "test_alias"); - //ok: user has manage_aliases on test_*, test_* aliases gets resolved to test_alias and empty indices gets resolved to _all + // ok: user has manage_aliases on test_*, test_* aliases gets resolved to test_alias and empty indices gets resolved to _all // indices (thus test_1) - assertAliases(client.admin().indices().prepareGetAliases().setAliases("test_*").setIndices("test_1"), - "test_1", "test_alias"); + assertAliases(client.admin().indices().prepareGetAliases().setAliases("test_*").setIndices("test_1"), "test_1", "test_alias"); - //ok: user has manage_aliases on test_*, _all aliases gets resolved to test_alias and _all indices becomes test_1 - assertAliases(client.admin().indices().prepareGetAliases().setAliases("_all").setIndices("_all"), - "test_1", "test_alias"); + // ok: user has manage_aliases on test_*, _all aliases gets resolved to test_alias and _all indices becomes test_1 + assertAliases(client.admin().indices().prepareGetAliases().setAliases("_all").setIndices("_all"), "test_1", "test_alias"); - //ok: user has manage_aliases on test_*, empty aliases gets resolved to test_alias and empty indices becomes test_1 - assertAliases(client.admin().indices().prepareGetAliases(), - "test_1", "test_alias"); + // ok: user has manage_aliases on test_*, empty aliases gets resolved to test_alias and empty indices becomes test_1 + assertAliases(client.admin().indices().prepareGetAliases(), "test_1", "test_alias"); - //fails: user has manage_aliases on test_*, although _all aliases and empty indices can be resolved, the explicit non + // fails: user has manage_aliases on test_*, although _all aliases and empty indices can be resolved, the explicit non // authorized alias (alias_1) causes the request to fail - assertThrowsAuthorizationException(client.admin().indices().prepareGetAliases().setAliases("_all", "alias_1")::get, - GetAliasesAction.NAME, "create_test_aliases_test"); + assertThrowsAuthorizationException( + client.admin().indices().prepareGetAliases().setAliases("_all", "alias_1")::get, + GetAliasesAction.NAME, + "create_test_aliases_test" + ); - //fails: user doesn't have manage_aliases on alias_1 - assertThrowsAuthorizationException(client.admin().indices().prepareGetAliases().setAliases("alias_1")::get, - GetAliasesAction.NAME, "create_test_aliases_test"); + // fails: user doesn't have manage_aliases on alias_1 + assertThrowsAuthorizationException( + client.admin().indices().prepareGetAliases().setAliases("alias_1")::get, + GetAliasesAction.NAME, + "create_test_aliases_test" + ); } public void testCreateIndexThenAliasesCreateAndAliasesPermission2() { - Map headers = Collections.singletonMap(BASIC_AUTH_HEADER, - basicAuthHeaderValue("create_test_aliases_alias", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)); + Map headers = Collections.singletonMap( + BASIC_AUTH_HEADER, + basicAuthHeaderValue("create_test_aliases_alias", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING) + ); final Client client = client(headers); - //user has create permission on test_* and manage_aliases permission on alias_*. manage_aliases is required to add/remove aliases + // user has create permission on test_* and manage_aliases permission on alias_*. manage_aliases is required to add/remove aliases // on both aliases and indices assertAcked(client.admin().indices().prepareCreate("test_1")); - //fails: user doesn't have manage_aliases aliases on test_1 - assertThrowsAuthorizationException(client.admin().indices().prepareAliases().addAlias("test_1", "test_alias")::get, - IndicesAliasesAction.NAME, "create_test_aliases_alias"); - - //fails: user doesn't have manage_aliases aliases on test_1 - assertThrowsAuthorizationException(client.admin().indices().prepareAliases().addAlias("test_1", "alias_1")::get, - IndicesAliasesAction.NAME, "create_test_aliases_alias"); + // fails: user doesn't have manage_aliases aliases on test_1 + assertThrowsAuthorizationException( + client.admin().indices().prepareAliases().addAlias("test_1", "test_alias")::get, + IndicesAliasesAction.NAME, + "create_test_aliases_alias" + ); - //fails: user doesn't have manage_aliases aliases on test_*, no matching indices to replace wildcards - IndexNotFoundException indexNotFoundException = expectThrows(IndexNotFoundException.class, - client.admin().indices().prepareAliases().addAlias("test_*", "alias_1")::get); + // fails: user doesn't have manage_aliases aliases on test_1 + assertThrowsAuthorizationException( + client.admin().indices().prepareAliases().addAlias("test_1", "alias_1")::get, + IndicesAliasesAction.NAME, + "create_test_aliases_alias" + ); + + // fails: user doesn't have manage_aliases aliases on test_*, no matching indices to replace wildcards + IndexNotFoundException indexNotFoundException = expectThrows( + IndexNotFoundException.class, + client.admin().indices().prepareAliases().addAlias("test_*", "alias_1")::get + ); assertThat(indexNotFoundException.toString(), containsString("[test_*]")); } public void testCreateIndexAndAliasesCreateAndAliasesPermission2() { - Map headers = Collections.singletonMap(BASIC_AUTH_HEADER, - basicAuthHeaderValue("create_test_aliases_alias", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)); + Map headers = Collections.singletonMap( + BASIC_AUTH_HEADER, + basicAuthHeaderValue("create_test_aliases_alias", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING) + ); final Client client = client(headers); - //user has create permission on test_* and manage_aliases permission on alias_*. manage_aliases is required to add/remove aliases + // user has create permission on test_* and manage_aliases permission on alias_*. manage_aliases is required to add/remove aliases // on both aliases and indices - //fails: user doesn't have manage_aliases on test_1, create index is rejected as a whole - assertThrowsAuthorizationException(client.admin().indices().prepareCreate("test_1").addAlias(new Alias("test_alias"))::get, - IndicesAliasesAction.NAME, "create_test_aliases_alias"); + // fails: user doesn't have manage_aliases on test_1, create index is rejected as a whole + assertThrowsAuthorizationException( + client.admin().indices().prepareCreate("test_1").addAlias(new Alias("test_alias"))::get, + IndicesAliasesAction.NAME, + "create_test_aliases_alias" + ); } public void testDeleteAliasesCreateAndAliasesPermission2() { - Map headers = Collections.singletonMap(BASIC_AUTH_HEADER, - basicAuthHeaderValue("create_test_aliases_alias", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)); + Map headers = Collections.singletonMap( + BASIC_AUTH_HEADER, + basicAuthHeaderValue("create_test_aliases_alias", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING) + ); final Client client = client(headers); - //user has create permission on test_* and manage_aliases permission on alias_*. manage_aliases is required to add/remove aliases + // user has create permission on test_* and manage_aliases permission on alias_*. manage_aliases is required to add/remove aliases // on both aliases and indices - //fails: user doesn't have manage_aliases on test_1 - assertThrowsAuthorizationException(client.admin().indices().prepareAliases().removeAlias("test_1", "test_alias")::get, - IndicesAliasesAction.NAME, "create_test_aliases_alias"); + // fails: user doesn't have manage_aliases on test_1 + assertThrowsAuthorizationException( + client.admin().indices().prepareAliases().removeAlias("test_1", "test_alias")::get, + IndicesAliasesAction.NAME, + "create_test_aliases_alias" + ); - //fails: user doesn't have manage_aliases on test_*, wildcards can't get replaced + // fails: user doesn't have manage_aliases on test_*, wildcards can't get replaced expectThrows(IndexNotFoundException.class, client.admin().indices().prepareAliases().removeAlias("test_*", "alias_1")::get); - } + } public void testGetAliasesCreateAndAliasesPermission2() { - Map headers = Collections.singletonMap(BASIC_AUTH_HEADER, - basicAuthHeaderValue("create_test_aliases_alias", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)); + Map headers = Collections.singletonMap( + BASIC_AUTH_HEADER, + basicAuthHeaderValue("create_test_aliases_alias", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING) + ); final Client client = client(headers); - //user has create permission on test_* and manage_aliases permission on alias_*. manage_aliases is required to retrieve aliases + // user has create permission on test_* and manage_aliases permission on alias_*. manage_aliases is required to retrieve aliases // on both aliases and indices assertAcked(client.admin().indices().prepareCreate("test_1")); - //fails: user doesn't have manage_aliases aliases on test_1, nor test_alias - assertThrowsAuthorizationException(client.admin().indices().prepareGetAliases().setAliases("test_alias").setIndices("test_1")::get, - GetAliasesAction.NAME, "create_test_aliases_alias"); - - //user doesn't have manage_aliases aliases on test_*, no matching indices to replace wildcards - GetAliasesResponse getAliasesResponse = client.admin().indices().prepareGetAliases() - .setIndices("test_*").setAliases("test_alias").get(); + // fails: user doesn't have manage_aliases aliases on test_1, nor test_alias + assertThrowsAuthorizationException( + client.admin().indices().prepareGetAliases().setAliases("test_alias").setIndices("test_1")::get, + GetAliasesAction.NAME, + "create_test_aliases_alias" + ); + + // user doesn't have manage_aliases aliases on test_*, no matching indices to replace wildcards + GetAliasesResponse getAliasesResponse = client.admin() + .indices() + .prepareGetAliases() + .setIndices("test_*") + .setAliases("test_alias") + .get(); assertEquals(0, getAliasesResponse.getAliases().size()); - //no existing indices to replace empty indices (thus _all) + // no existing indices to replace empty indices (thus _all) getAliasesResponse = client.admin().indices().prepareGetAliases().setAliases("test_alias").get(); assertEquals(0, getAliasesResponse.getAliases().size()); { - //fails: no existing aliases to replace wildcards + // fails: no existing aliases to replace wildcards assertThrowsAuthorizationException( client.admin().indices().prepareGetAliases().setIndices("test_1").setAliases("test_*")::get, - GetAliasesAction.NAME, "create_test_aliases_alias"); + GetAliasesAction.NAME, + "create_test_aliases_alias" + ); } { - //fails: no existing aliases to replace _all - assertThrowsAuthorizationException(client.admin().indices().prepareGetAliases().setIndices("test_1").setAliases("_all")::get, - GetAliasesAction.NAME, "create_test_aliases_alias"); + // fails: no existing aliases to replace _all + assertThrowsAuthorizationException( + client.admin().indices().prepareGetAliases().setIndices("test_1").setAliases("_all")::get, + GetAliasesAction.NAME, + "create_test_aliases_alias" + ); } { - //fails: no existing aliases to replace empty aliases - assertThrowsAuthorizationException(client.admin().indices().prepareGetAliases().setIndices("test_1")::get, - GetAliasesAction.NAME, "create_test_aliases_alias"); + // fails: no existing aliases to replace empty aliases + assertThrowsAuthorizationException( + client.admin().indices().prepareGetAliases().setIndices("test_1")::get, + GetAliasesAction.NAME, + "create_test_aliases_alias" + ); } { - //fails: no existing aliases to replace empty aliases + // fails: no existing aliases to replace empty aliases GetAliasesResponse response = client.admin().indices().prepareGetAliases().get(); assertThat(response.getAliases().size(), equalTo(0)); } } public void testCreateIndexThenAliasesCreateAndAliasesPermission3() { - Map headers = Collections.singletonMap(BASIC_AUTH_HEADER, - basicAuthHeaderValue("create_test_aliases_test_alias", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)); + Map headers = Collections.singletonMap( + BASIC_AUTH_HEADER, + basicAuthHeaderValue("create_test_aliases_test_alias", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING) + ); final Client client = client(headers); - //user has create permission on test_* and manage_aliases permission on test_*,alias_*. All good. + // user has create permission on test_* and manage_aliases permission on test_*,alias_*. All good. assertAcked(client.admin().indices().prepareCreate("test_1")); assertAcked(client.admin().indices().prepareAliases().addAlias("test_1", "test_alias")); @@ -460,126 +616,185 @@ public void testCreateIndexThenAliasesCreateAndAliasesPermission3() { } public void testCreateIndexAndAliasesCreateAndAliasesPermission3() { - Map headers = Collections.singletonMap(BASIC_AUTH_HEADER, - basicAuthHeaderValue("create_test_aliases_test_alias", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)); + Map headers = Collections.singletonMap( + BASIC_AUTH_HEADER, + basicAuthHeaderValue("create_test_aliases_test_alias", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING) + ); final Client client = client(headers); - //user has create permission on test_* and manage_aliases permission on test_*,alias_*. All good. + // user has create permission on test_* and manage_aliases permission on test_*,alias_*. All good. assertAcked(client.admin().indices().prepareCreate("test_1").addAlias(new Alias("test_alias"))); assertAcked(client.admin().indices().prepareCreate("test_2").addAlias(new Alias("test_alias_2")).addAlias(new Alias("alias_2"))); } public void testDeleteAliasesCreateAndAliasesPermission3() { - Map headers = Collections.singletonMap(BASIC_AUTH_HEADER, - basicAuthHeaderValue("create_test_aliases_test_alias", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)); + Map headers = Collections.singletonMap( + BASIC_AUTH_HEADER, + basicAuthHeaderValue("create_test_aliases_test_alias", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING) + ); final Client client = client(headers); - //user has create permission on test_* and manage_aliases permission on test_*,alias_*. All good. - assertAcked(client.admin().indices().prepareCreate("test_1").addAlias(new Alias("test_alias")).addAlias(new Alias("alias_1")) - .addAlias(new Alias("alias_2")).addAlias(new Alias("alias_3"))); - - //fails: user doesn't have manage_aliases privilege on non_authorized - assertThrowsAuthorizationException(client.admin().indices().prepareAliases().removeAlias("test_1", "non_authorized") - .removeAlias("test_1", "test_alias")::get, IndicesAliasesAction.NAME, "create_test_aliases_test_alias"); + // user has create permission on test_* and manage_aliases permission on test_*,alias_*. All good. + assertAcked( + client.admin() + .indices() + .prepareCreate("test_1") + .addAlias(new Alias("test_alias")) + .addAlias(new Alias("alias_1")) + .addAlias(new Alias("alias_2")) + .addAlias(new Alias("alias_3")) + ); + + // fails: user doesn't have manage_aliases privilege on non_authorized + assertThrowsAuthorizationException( + client.admin().indices().prepareAliases().removeAlias("test_1", "non_authorized").removeAlias("test_1", "test_alias")::get, + IndicesAliasesAction.NAME, + "create_test_aliases_test_alias" + ); assertAcked(client.admin().indices().prepareAliases().removeAlias("test_1", "alias_1")); assertAcked(client.admin().indices().prepareAliases().removeAlias("test_*", "_all")); - //fails: all aliases have been deleted, _all can't be resolved to any existing authorized aliases - AliasesNotFoundException exception = expectThrows(AliasesNotFoundException.class, - client.admin().indices().prepareAliases().removeAlias("test_1", "_all")::get); + // fails: all aliases have been deleted, _all can't be resolved to any existing authorized aliases + AliasesNotFoundException exception = expectThrows( + AliasesNotFoundException.class, + client.admin().indices().prepareAliases().removeAlias("test_1", "_all")::get + ); assertThat(exception.getMessage(), equalTo("aliases [_all] missing")); } public void testGetAliasesCreateAndAliasesPermission3() { - Map headers = Collections.singletonMap(BASIC_AUTH_HEADER, - basicAuthHeaderValue("create_test_aliases_test_alias", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)); + Map headers = Collections.singletonMap( + BASIC_AUTH_HEADER, + basicAuthHeaderValue("create_test_aliases_test_alias", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING) + ); final Client client = client(headers); - //user has create permission on test_* and manage_aliases permission on test_*,alias_*. All good. + // user has create permission on test_* and manage_aliases permission on test_*,alias_*. All good. assertAcked(client.admin().indices().prepareCreate("test_1").addAlias(new Alias("test_alias")).addAlias(new Alias("alias_1"))); - assertAliases(client.admin().indices().prepareGetAliases().setAliases("test_alias").setIndices("test_1"), - "test_1", "test_alias"); + assertAliases(client.admin().indices().prepareGetAliases().setAliases("test_alias").setIndices("test_1"), "test_1", "test_alias"); - assertAliases(client.admin().indices().prepareGetAliases().setAliases("alias_1").setIndices("test_1"), - "test_1", "alias_1"); + assertAliases(client.admin().indices().prepareGetAliases().setAliases("alias_1").setIndices("test_1"), "test_1", "alias_1"); - assertAliases(client.admin().indices().prepareGetAliases().setAliases("alias_1").setIndices("test_*"), - "test_1", "alias_1"); + assertAliases(client.admin().indices().prepareGetAliases().setAliases("alias_1").setIndices("test_*"), "test_1", "alias_1"); - assertAliases(client.admin().indices().prepareGetAliases().setAliases("test_*").setIndices("test_1"), - "test_1", "test_alias"); + assertAliases(client.admin().indices().prepareGetAliases().setAliases("test_*").setIndices("test_1"), "test_1", "test_alias"); - assertAliases(client.admin().indices().prepareGetAliases().setAliases("_all").setIndices("test_1"), - "test_1", "alias_1", "test_alias"); + assertAliases( + client.admin().indices().prepareGetAliases().setAliases("_all").setIndices("test_1"), + "test_1", + "alias_1", + "test_alias" + ); - assertAliases(client.admin().indices().prepareGetAliases().setAliases("_all"), - "test_1", "alias_1", "test_alias"); + assertAliases(client.admin().indices().prepareGetAliases().setAliases("_all"), "test_1", "alias_1", "test_alias"); - assertAliases(client.admin().indices().prepareGetAliases().setIndices("test_1"), - "test_1", "alias_1", "test_alias"); + assertAliases(client.admin().indices().prepareGetAliases().setIndices("test_1"), "test_1", "alias_1", "test_alias"); assertAliases(client.admin().indices().prepareGetAliases(), "test_1", "alias_1", "test_alias"); - assertAliases(client.admin().indices().prepareGetAliases().setAliases("alias_*").setIndices("test_*"), - "test_1", "alias_1"); + assertAliases(client.admin().indices().prepareGetAliases().setAliases("alias_*").setIndices("test_*"), "test_1", "alias_1"); } public void testCreateIndexAliasesOnlyPermission() { - assertThrowsAuthorizationException(client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, - basicAuthHeaderValue("aliases_only", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING))) - .admin().indices().prepareCreate("test_1")::get, CreateIndexAction.NAME, "aliases_only"); + assertThrowsAuthorizationException( + client().filterWithHeader( + Collections.singletonMap( + BASIC_AUTH_HEADER, + basicAuthHeaderValue("aliases_only", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING) + ) + ).admin().indices().prepareCreate("test_1")::get, + CreateIndexAction.NAME, + "aliases_only" + ); } public void testGetAliasesAliasesOnlyPermissionStrict() { - Map headers = Collections.singletonMap(BASIC_AUTH_HEADER, - basicAuthHeaderValue("aliases_only", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)); + Map headers = Collections.singletonMap( + BASIC_AUTH_HEADER, + basicAuthHeaderValue("aliases_only", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING) + ); final Client client = client(headers); - //user has manage_aliases only permissions on both alias_* and test_* - - //security plugin lets it through, but es core intercepts it due to strict indices options and throws index not found - IndexNotFoundException indexNotFoundException = expectThrows(IndexNotFoundException.class, client.admin().indices() - .prepareGetAliases("alias_1").addIndices("test_1").setIndicesOptions(IndicesOptions.strictExpandOpen())::get); + // user has manage_aliases only permissions on both alias_* and test_* + + // security plugin lets it through, but es core intercepts it due to strict indices options and throws index not found + IndexNotFoundException indexNotFoundException = expectThrows( + IndexNotFoundException.class, + client.admin() + .indices() + .prepareGetAliases("alias_1") + .addIndices("test_1") + .setIndicesOptions(IndicesOptions.strictExpandOpen())::get + ); assertEquals("no such index [test_1]", indexNotFoundException.getMessage()); - //fails: no manage_aliases privilege on non_authorized alias - assertThrowsAuthorizationException(client.admin().indices().prepareGetAliases("non_authorized").addIndices("test_1") - .setIndicesOptions(IndicesOptions.strictExpandOpen())::get, GetAliasesAction.NAME, "aliases_only"); - - //fails: no manage_aliases privilege on non_authorized index - assertThrowsAuthorizationException(client.admin().indices().prepareGetAliases("alias_1").addIndices("non_authorized") - .setIndicesOptions(IndicesOptions.strictExpandOpen())::get, GetAliasesAction.NAME, "aliases_only"); + // fails: no manage_aliases privilege on non_authorized alias + assertThrowsAuthorizationException( + client.admin() + .indices() + .prepareGetAliases("non_authorized") + .addIndices("test_1") + .setIndicesOptions(IndicesOptions.strictExpandOpen())::get, + GetAliasesAction.NAME, + "aliases_only" + ); + + // fails: no manage_aliases privilege on non_authorized index + assertThrowsAuthorizationException( + client.admin() + .indices() + .prepareGetAliases("alias_1") + .addIndices("non_authorized") + .setIndicesOptions(IndicesOptions.strictExpandOpen())::get, + GetAliasesAction.NAME, + "aliases_only" + ); } public void testGetAliasesAliasesOnlyPermissionIgnoreUnavailable() { - Map headers = Collections.singletonMap(BASIC_AUTH_HEADER, - basicAuthHeaderValue("aliases_only", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)); + Map headers = Collections.singletonMap( + BASIC_AUTH_HEADER, + basicAuthHeaderValue("aliases_only", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING) + ); final Client client = client(headers); - //user has manage_aliases only permissions on both alias_* and test_* - - //ok: manage_aliases on both test_* and alias_* - GetAliasesResponse getAliasesResponse = client.admin().indices().prepareGetAliases("alias_1") - .addIndices("test_1").setIndicesOptions(IndicesOptions.lenientExpandOpen()).get(); + // user has manage_aliases only permissions on both alias_* and test_* + + // ok: manage_aliases on both test_* and alias_* + GetAliasesResponse getAliasesResponse = client.admin() + .indices() + .prepareGetAliases("alias_1") + .addIndices("test_1") + .setIndicesOptions(IndicesOptions.lenientExpandOpen()) + .get(); assertEquals(0, getAliasesResponse.getAliases().size()); - //no manage_aliases privilege on non_authorized alias - getAliasesResponse = client.admin().indices().prepareGetAliases("non_authorized").addIndices("test_1") - .setIndicesOptions(IndicesOptions.lenientExpandOpen()).get(); + // no manage_aliases privilege on non_authorized alias + getAliasesResponse = client.admin() + .indices() + .prepareGetAliases("non_authorized") + .addIndices("test_1") + .setIndicesOptions(IndicesOptions.lenientExpandOpen()) + .get(); assertEquals(0, getAliasesResponse.getAliases().size()); - //no manage_aliases privilege on non_authorized index - getAliasesResponse = client.admin().indices().prepareGetAliases("alias_1").addIndices("non_authorized") - .setIndicesOptions(IndicesOptions.lenientExpandOpen()).get(); + // no manage_aliases privilege on non_authorized index + getAliasesResponse = client.admin() + .indices() + .prepareGetAliases("alias_1") + .addIndices("non_authorized") + .setIndicesOptions(IndicesOptions.lenientExpandOpen()) + .get(); assertEquals(0, getAliasesResponse.getAliases().size()); } public void testRemoveIndex() { final Map headers = Collections.singletonMap( BASIC_AUTH_HEADER, - basicAuthHeaderValue("all_on_test", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)); + basicAuthHeaderValue("all_on_test", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING) + ); final Client client = client(headers); assertAcked(client.admin().indices().prepareCreate("test_delete_1").get()); @@ -601,19 +816,31 @@ public void testAliasesForHiddenIndices() { final String hiddenAlias = "alias_hidden"; final Map createHeaders = Collections.singletonMap( - BASIC_AUTH_HEADER, basicAuthHeaderValue("all_on_test", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)); + BASIC_AUTH_HEADER, + basicAuthHeaderValue("all_on_test", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING) + ); final Client createClient = client(createHeaders); final Map aliasHeaders = Collections.singletonMap( - BASIC_AUTH_HEADER, basicAuthHeaderValue("aliases_only", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)); + BASIC_AUTH_HEADER, + basicAuthHeaderValue("aliases_only", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING) + ); final Client aliasesClient = client(aliasHeaders); - assertAcked(createClient.admin().indices().prepareCreate(hiddenIndex) - .setSettings(Settings.builder().put("index.hidden", true).build()) - .get()); - - assertAcked(aliasesClient.admin().indices().prepareAliases() - .addAliasAction(IndicesAliasesRequest.AliasActions.add().index(hiddenIndex).alias(visibleAlias))); + assertAcked( + createClient.admin() + .indices() + .prepareCreate(hiddenIndex) + .setSettings(Settings.builder().put("index.hidden", true).build()) + .get() + ); + + assertAcked( + aliasesClient.admin() + .indices() + .prepareAliases() + .addAliasAction(IndicesAliasesRequest.AliasActions.add().index(hiddenIndex).alias(visibleAlias)) + ); // The index should be returned here when queried by name or by wildcard because the alias is visible final GetAliasesRequestBuilder req = aliasesClient.admin().indices().prepareGetAliases(visibleAlias); @@ -628,9 +855,13 @@ public void testAliasesForHiddenIndices() { assertThat(response.getAliases().get(hiddenIndex).get(0).isHidden(), nullValue()); // Now try with a hidden alias - assertAcked(aliasesClient.admin().indices().prepareAliases() - .addAliasAction(IndicesAliasesRequest.AliasActions.remove().index(hiddenIndex).alias(visibleAlias)) - .addAliasAction(IndicesAliasesRequest.AliasActions.add().index(hiddenIndex).alias(hiddenAlias).isHidden(true))); + assertAcked( + aliasesClient.admin() + .indices() + .prepareAliases() + .addAliasAction(IndicesAliasesRequest.AliasActions.remove().index(hiddenIndex).alias(visibleAlias)) + .addAliasAction(IndicesAliasesRequest.AliasActions.add().index(hiddenIndex).alias(hiddenAlias).isHidden(true)) + ); // Querying by name directly should get the right result response = aliasesClient.admin().indices().prepareGetAliases(hiddenAlias).get(); @@ -645,8 +876,7 @@ public void testAliasesForHiddenIndices() { assertThat(response.getAliases().get(hiddenIndex).get(0).isHidden(), Matchers.equalTo(true)); // But we should get no results if we specify indices options that don't include hidden - response = aliasesClient.admin().indices().prepareGetAliases("alias*") - .setIndicesOptions(IndicesOptions.strictExpandOpen()).get(); + response = aliasesClient.admin().indices().prepareGetAliases("alias*").setIndicesOptions(IndicesOptions.strictExpandOpen()).get(); assertThat(response.getAliases().get(hiddenIndex), nullValue()); } diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/ReadActionsTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/ReadActionsTests.java index ab751b476079a..f54543be98eff 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/ReadActionsTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/ReadActionsTests.java @@ -39,17 +39,18 @@ public class ReadActionsTests extends SecurityIntegTestCase { @Override protected String configRoles() { - return SecuritySettingsSource.TEST_ROLE + ":\n" + - " cluster: [ ALL ]\n" + - " indices:\n" + - " - names: '*'\n" + - " privileges: [ manage, write ]\n" + - " - names: ['/test.*/', '/-alias.*/']\n" + - " privileges: [ read ]\n"; + return SecuritySettingsSource.TEST_ROLE + + ":\n" + + " cluster: [ ALL ]\n" + + " indices:\n" + + " - names: '*'\n" + + " privileges: [ manage, write ]\n" + + " - names: ['/test.*/', '/-alias.*/']\n" + + " privileges: [ read ]\n"; } public void testSearchForAll() { - //index1 is not authorized and referred to through wildcard + // index1 is not authorized and referred to through wildcard createIndicesWithRandomAliases("test1", "test2", "test3", "index1"); SearchResponse searchResponse = trySearch(); @@ -57,7 +58,7 @@ public void testSearchForAll() { } public void testSearchForWildcard() { - //index1 is not authorized and referred to through wildcard + // index1 is not authorized and referred to through wildcard createIndicesWithRandomAliases("test1", "test2", "test3", "index1"); SearchResponse searchResponse = trySearch("*"); @@ -65,13 +66,13 @@ public void testSearchForWildcard() { } public void testSearchNonAuthorizedWildcard() { - //wildcard doesn't match any authorized index + // wildcard doesn't match any authorized index createIndicesWithRandomAliases("test1", "test2", "index1", "index2"); assertNoSearchHits(trySearch("index*")); } public void testSearchNonAuthorizedWildcardDisallowNoIndices() { - //wildcard doesn't match any authorized index + // wildcard doesn't match any authorized index createIndicesWithRandomAliases("test1", "test2", "index1", "index2"); IndexNotFoundException e = expectThrows( IndexNotFoundException.class, @@ -154,7 +155,7 @@ public void testIndexNotFoundIgnoreUnavailable() { assertReturnedIndices(trySearch(indicesOptions, "missing_*", "test*"), "test1", "test2"); - //an unauthorized index is the same as a missing one + // an unauthorized index is the same as a missing one assertNoSearchHits(trySearch(indicesOptions, "missing")); assertNoSearchHits(trySearch(indicesOptions, "index1")); @@ -171,7 +172,7 @@ public void testIndexNotFoundIgnoreUnavailable() { } public void testExplicitExclusion() { - //index1 is not authorized and referred to through wildcard, test2 is excluded + // index1 is not authorized and referred to through wildcard, test2 is excluded createIndicesWithRandomAliases("test1", "test2", "test3", "index1"); SearchResponse searchResponse = trySearch("*", "-test2"); @@ -179,7 +180,7 @@ public void testExplicitExclusion() { } public void testWildcardExclusion() { - //index1 is not authorized and referred to through wildcard, test2 is excluded + // index1 is not authorized and referred to through wildcard, test2 is excluded createIndicesWithRandomAliases("test1", "test2", "test21", "test3", "index1"); SearchResponse searchResponse = trySearch("*", "-test2*"); @@ -187,7 +188,7 @@ public void testWildcardExclusion() { } public void testInclusionAndWildcardsExclusion() { - //index1 is not authorized and referred to through wildcard, test111 and test112 are excluded + // index1 is not authorized and referred to through wildcard, test111 and test112 are excluded createIndicesWithRandomAliases("test1", "test10", "test111", "test112", "test2", "index1"); SearchResponse searchResponse = trySearch("test1*", "index*", "-test11*"); @@ -195,7 +196,7 @@ public void testInclusionAndWildcardsExclusion() { } public void testExplicitAndWildcardsInclusionAndWildcardExclusion() { - //index1 is not authorized and referred to through wildcard, test111 and test112 are excluded + // index1 is not authorized and referred to through wildcard, test111 and test112 are excluded createIndicesWithRandomAliases("test1", "test10", "test111", "test112", "test2", "index1"); SearchResponse searchResponse = trySearch("test2", "test11*", "index*", "-test2*"); @@ -203,7 +204,7 @@ public void testExplicitAndWildcardsInclusionAndWildcardExclusion() { } public void testExplicitAndWildcardInclusionAndExplicitExclusions() { - //index1 is not authorized and referred to through wildcard, test111 and test112 are excluded + // index1 is not authorized and referred to through wildcard, test111 and test112 are excluded createIndicesWithRandomAliases("test1", "test10", "test111", "test112", "test2", "index1"); SearchResponse searchResponse = trySearch("test10", "test11*", "index*", "-test111", "-test112"); @@ -215,12 +216,13 @@ public void testMissingDateMath() { } public void testMultiSearchUnauthorizedIndex() { - //index1 is not authorized, only that specific item fails + // index1 is not authorized, only that specific item fails createIndicesWithRandomAliases("test1", "test2", "test3", "index1"); { MultiSearchResponse multiSearchResponse = client().prepareMultiSearch() - .add(Requests.searchRequest()) - .add(Requests.searchRequest("index1")).get(); + .add(Requests.searchRequest()) + .add(Requests.searchRequest("index1")) + .get(); assertEquals(2, multiSearchResponse.getResponses().length); assertFalse(multiSearchResponse.getResponses()[0].isFailure()); SearchResponse searchResponse = multiSearchResponse.getResponses()[0].getResponse(); @@ -233,9 +235,9 @@ public void testMultiSearchUnauthorizedIndex() { } { MultiSearchResponse multiSearchResponse = client().prepareMultiSearch() - .add(Requests.searchRequest()) - .add(Requests.searchRequest("index1") - .indicesOptions(IndicesOptions.fromOptions(true, true, true, randomBoolean()))).get(); + .add(Requests.searchRequest()) + .add(Requests.searchRequest("index1").indicesOptions(IndicesOptions.fromOptions(true, true, true, randomBoolean()))) + .get(); assertEquals(2, multiSearchResponse.getResponses().length); assertFalse(multiSearchResponse.getResponses()[0].isFailure()); SearchResponse searchResponse = multiSearchResponse.getResponses()[0].getResponse(); @@ -250,8 +252,9 @@ public void testMultiSearchMissingUnauthorizedIndex() { createIndicesWithRandomAliases("test1", "test2", "test3", "index1"); { MultiSearchResponse multiSearchResponse = client().prepareMultiSearch() - .add(Requests.searchRequest()) - .add(Requests.searchRequest("missing")).get(); + .add(Requests.searchRequest()) + .add(Requests.searchRequest("missing")) + .get(); assertEquals(2, multiSearchResponse.getResponses().length); assertFalse(multiSearchResponse.getResponses()[0].isFailure()); SearchResponse searchResponse = multiSearchResponse.getResponses()[0].getResponse(); @@ -264,9 +267,9 @@ public void testMultiSearchMissingUnauthorizedIndex() { } { MultiSearchResponse multiSearchResponse = client().prepareMultiSearch() - .add(Requests.searchRequest()) - .add(Requests.searchRequest("missing") - .indicesOptions(IndicesOptions.fromOptions(true, true, true, randomBoolean()))).get(); + .add(Requests.searchRequest()) + .add(Requests.searchRequest("missing").indicesOptions(IndicesOptions.fromOptions(true, true, true, randomBoolean()))) + .get(); assertEquals(2, multiSearchResponse.getResponses().length); assertFalse(multiSearchResponse.getResponses()[0].isFailure()); SearchResponse searchResponse = multiSearchResponse.getResponses()[0].getResponse(); @@ -278,25 +281,28 @@ public void testMultiSearchMissingUnauthorizedIndex() { } public void testMultiSearchMissingAuthorizedIndex() { - //test4 is missing but authorized, only that specific item fails + // test4 is missing but authorized, only that specific item fails createIndicesWithRandomAliases("test1", "test2", "test3", "index1"); { - //default indices options for search request don't ignore unavailable indices, only individual items fail. + // default indices options for search request don't ignore unavailable indices, only individual items fail. MultiSearchResponse multiSearchResponse = client().prepareMultiSearch() - .add(Requests.searchRequest()) - .add(Requests.searchRequest("test4")).get(); + .add(Requests.searchRequest()) + .add(Requests.searchRequest("test4")) + .get(); assertFalse(multiSearchResponse.getResponses()[0].isFailure()); assertReturnedIndices(multiSearchResponse.getResponses()[0].getResponse(), "test1", "test2", "test3"); assertTrue(multiSearchResponse.getResponses()[1].isFailure()); - assertThat(multiSearchResponse.getResponses()[1].getFailure().toString(), - equalTo("[test4] org.elasticsearch.index.IndexNotFoundException: no such index [test4]")); + assertThat( + multiSearchResponse.getResponses()[1].getFailure().toString(), + equalTo("[test4] org.elasticsearch.index.IndexNotFoundException: no such index [test4]") + ); } { - //we set ignore_unavailable and allow_no_indices to true, no errors returned, second item doesn't have hits. + // we set ignore_unavailable and allow_no_indices to true, no errors returned, second item doesn't have hits. MultiSearchResponse multiSearchResponse = client().prepareMultiSearch() - .add(Requests.searchRequest()) - .add(Requests.searchRequest("test4") - .indicesOptions(IndicesOptions.fromOptions(true, true, true, randomBoolean()))).get(); + .add(Requests.searchRequest()) + .add(Requests.searchRequest("test4").indicesOptions(IndicesOptions.fromOptions(true, true, true, randomBoolean()))) + .get(); assertReturnedIndices(multiSearchResponse.getResponses()[0].getResponse(), "test1", "test2", "test3"); assertNoSearchHits(multiSearchResponse.getResponses()[1].getResponse()); } @@ -305,8 +311,10 @@ public void testMultiSearchMissingAuthorizedIndex() { public void testMultiSearchWildcard() { createIndicesWithRandomAliases("test1", "test2", "test3", "index1"); { - MultiSearchResponse multiSearchResponse = client().prepareMultiSearch().add(Requests.searchRequest()) - .add(Requests.searchRequest("index*")).get(); + MultiSearchResponse multiSearchResponse = client().prepareMultiSearch() + .add(Requests.searchRequest()) + .add(Requests.searchRequest("index*")) + .get(); assertEquals(2, multiSearchResponse.getResponses().length); assertFalse(multiSearchResponse.getResponses()[0].isFailure()); SearchResponse searchResponse = multiSearchResponse.getResponses()[0].getResponse(); @@ -315,9 +323,13 @@ public void testMultiSearchWildcard() { assertNoSearchHits(multiSearchResponse.getResponses()[1].getResponse()); } { - MultiSearchResponse multiSearchResponse = client().prepareMultiSearch().add(Requests.searchRequest()) - .add(Requests.searchRequest("index*") - .indicesOptions(IndicesOptions.fromOptions(randomBoolean(), false, true, randomBoolean()))).get(); + MultiSearchResponse multiSearchResponse = client().prepareMultiSearch() + .add(Requests.searchRequest()) + .add( + Requests.searchRequest("index*") + .indicesOptions(IndicesOptions.fromOptions(randomBoolean(), false, true, randomBoolean())) + ) + .get(); assertEquals(2, multiSearchResponse.getResponses().length); assertFalse(multiSearchResponse.getResponses()[0].isFailure()); SearchResponse searchResponse = multiSearchResponse.getResponses()[0].getResponse(); @@ -344,25 +356,28 @@ public void testGet() { public void testMultiGet() { createIndicesWithRandomAliases("test1", "test2", "test3", "index1"); MultiGetResponse multiGetResponse = client().prepareMultiGet() - .add("test1", "id") - .add("index1", "id") - .add("test3", "id") - .add("missing", "id") - .add("test5", "id").get(); + .add("test1", "id") + .add("index1", "id") + .add("test3", "id") + .add("missing", "id") + .add("test5", "id") + .get(); assertEquals(5, multiGetResponse.getResponses().length); assertFalse(multiGetResponse.getResponses()[0].isFailed()); assertEquals("test1", multiGetResponse.getResponses()[0].getResponse().getIndex()); assertTrue(multiGetResponse.getResponses()[1].isFailed()); assertEquals("index1", multiGetResponse.getResponses()[1].getFailure().getIndex()); - assertAuthorizationExceptionDefaultUsers(multiGetResponse.getResponses()[1].getFailure().getFailure(), - MultiGetAction.NAME + "[shard]"); + assertAuthorizationExceptionDefaultUsers( + multiGetResponse.getResponses()[1].getFailure().getFailure(), + MultiGetAction.NAME + "[shard]" + ); assertFalse(multiGetResponse.getResponses()[2].isFailed()); assertEquals("test3", multiGetResponse.getResponses()[2].getResponse().getIndex()); assertTrue(multiGetResponse.getResponses()[3].isFailed()); assertEquals("missing", multiGetResponse.getResponses()[3].getFailure().getIndex()); - //different behaviour compared to get api: we leak information about a non existing index that the current user is not - //authorized for. Should rather be an authorization exception but we only authorize at the shard level in mget. If we - //authorized globally, we would fail the whole mget request which is not desirable. + // different behaviour compared to get api: we leak information about a non existing index that the current user is not + // authorized for. Should rather be an authorization exception but we only authorize at the shard level in mget. If we + // authorized globally, we would fail the whole mget request which is not desirable. assertThat(multiGetResponse.getResponses()[3].getFailure().getFailure(), instanceOf(IndexNotFoundException.class)); assertTrue(multiGetResponse.getResponses()[4].isFailed()); assertThat(multiGetResponse.getResponses()[4].getFailure().getFailure(), instanceOf(IndexNotFoundException.class)); @@ -382,35 +397,38 @@ public void testTermVectors() { public void testMultiTermVectors() { createIndicesWithRandomAliases("test1", "test2", "test3", "index1"); MultiTermVectorsResponse response = client().prepareMultiTermVectors() - .add("test1", "id") - .add("index1", "id") - .add("test3", "id") - .add("missing", "id") - .add("test5", "id").get(); + .add("test1", "id") + .add("index1", "id") + .add("test3", "id") + .add("missing", "id") + .add("test5", "id") + .get(); assertEquals(5, response.getResponses().length); assertFalse(response.getResponses()[0].isFailed()); assertEquals("test1", response.getResponses()[0].getResponse().getIndex()); assertTrue(response.getResponses()[1].isFailed()); assertEquals("index1", response.getResponses()[1].getFailure().getIndex()); - assertAuthorizationExceptionDefaultUsers(response.getResponses()[1].getFailure().getCause(), - MultiTermVectorsAction.NAME + "[shard]"); + assertAuthorizationExceptionDefaultUsers( + response.getResponses()[1].getFailure().getCause(), + MultiTermVectorsAction.NAME + "[shard]" + ); assertFalse(response.getResponses()[2].isFailed()); assertEquals("test3", response.getResponses()[2].getResponse().getIndex()); assertTrue(response.getResponses()[3].isFailed()); assertEquals("missing", response.getResponses()[3].getFailure().getIndex()); - //different behaviour compared to term_vector api: we leak information about a non existing index that the current user is not - //authorized for. Should rather be an authorization exception but we only authorize at the shard level in mget. If we - //authorized globally, we would fail the whole mget request which is not desirable. + // different behaviour compared to term_vector api: we leak information about a non existing index that the current user is not + // authorized for. Should rather be an authorization exception but we only authorize at the shard level in mget. If we + // authorized globally, we would fail the whole mget request which is not desirable. assertThat(response.getResponses()[3].getFailure().getCause(), instanceOf(IndexNotFoundException.class)); assertTrue(response.getResponses()[4].isFailed()); assertThat(response.getResponses()[4].getFailure().getCause(), instanceOf(IndexNotFoundException.class)); } - private SearchResponse trySearch(String ... indices) { + private SearchResponse trySearch(String... indices) { return client().prepareSearch(indices).get(TimeValue.timeValueSeconds(20)); } - private SearchResponse trySearch(IndicesOptions options, String ... indices) { + private SearchResponse trySearch(IndicesOptions options, String... indices) { return client().prepareSearch(indices).setIndicesOptions(options).get(TimeValue.timeValueSeconds(20)); } diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/SecurityScrollTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/SecurityScrollTests.java index 6df90c0062944..0ead996103256 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/SecurityScrollTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/SecurityScrollTests.java @@ -32,8 +32,8 @@ public class SecurityScrollTests extends SecurityIntegTestCase { public void testScrollIsPerUser() throws Exception { assertSecurityIndexActive(); new PutRoleRequestBuilder(client()).name("scrollable") - .addIndices(new String[] { randomAlphaOfLengthBetween(4, 12) }, new String[] { "read" }, null, null, null, randomBoolean()) - .get(); + .addIndices(new String[] { randomAlphaOfLengthBetween(4, 12) }, new String[] { "read" }, null, null, null, randomBoolean()) + .get(); new PutUserRequestBuilder(client()).username("other") .password(SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING, getFastStoredHashAlgoForTests()) .roles("scrollable") @@ -47,10 +47,10 @@ public void testScrollIsPerUser() throws Exception { indexRandom(true, docs); SearchResponse response = client().prepareSearch("foo") - .setScroll(TimeValue.timeValueSeconds(5L)) - .setQuery(matchAllQuery()) - .setSize(1) - .get(); + .setScroll(TimeValue.timeValueSeconds(5L)) + .setQuery(matchAllQuery()) + .setSize(1) + .get(); assertEquals(numDocs, response.getHits().getTotalHits().value); assertEquals(1, response.getHits().getHits().length); @@ -61,12 +61,15 @@ public void testScrollIsPerUser() throws Exception { } final String scrollId = response.getScrollId(); - SearchPhaseExecutionException e = expectThrows(SearchPhaseExecutionException.class, () -> - client() - .filterWithHeader(Collections.singletonMap("Authorization", - UsernamePasswordToken.basicAuthHeaderValue("other", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING))) - .prepareSearchScroll(scrollId) - .get()); + SearchPhaseExecutionException e = expectThrows( + SearchPhaseExecutionException.class, + () -> client().filterWithHeader( + Collections.singletonMap( + "Authorization", + UsernamePasswordToken.basicAuthHeaderValue("other", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING) + ) + ).prepareSearchScroll(scrollId).get() + ); for (ShardSearchFailure failure : e.shardFailures()) { assertThat(ExceptionsHelper.unwrapCause(failure.getCause()), instanceOf(SearchContextMissingException.class)); } @@ -79,17 +82,17 @@ public void testSearchAndClearScroll() throws Exception { } indexRandom(true, docs); SearchResponse response = client().prepareSearch() - .setQuery(matchAllQuery()) - .setScroll(TimeValue.timeValueSeconds(5L)) - .setSize(randomIntBetween(1, 10)).get(); + .setQuery(matchAllQuery()) + .setScroll(TimeValue.timeValueSeconds(5L)) + .setSize(randomIntBetween(1, 10)) + .get(); int hits = 0; try { do { assertHitCount(response, docs.length); hits += response.getHits().getHits().length; - response = client().prepareSearchScroll(response.getScrollId()) - .setScroll(TimeValue.timeValueSeconds(5L)).get(); + response = client().prepareSearchScroll(response.getScrollId()).setScroll(TimeValue.timeValueSeconds(5L)).get(); } while (response.getHits().getHits().length != 0); assertThat(hits, equalTo(docs.length)); diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/SecuritySearchOperationListenerTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/SecuritySearchOperationListenerTests.java index f39e6a618568d..d56226f108d0a 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/SecuritySearchOperationListenerTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/SecuritySearchOperationListenerTests.java @@ -65,9 +65,16 @@ public void setupShard() { public void testOnNewContextSetsAuthentication() throws Exception { final ShardSearchRequest shardSearchRequest = mock(ShardSearchRequest.class); when(shardSearchRequest.scroll()).thenReturn(new Scroll(TimeValue.timeValueMinutes(between(1, 10)))); - try (LegacyReaderContext readerContext = - new LegacyReaderContext(new ShardSearchContextId(UUIDs.randomBase64UUID(), 0L), - indexService, shard, shard.acquireSearcherSupplier(), shardSearchRequest, Long.MAX_VALUE)) { + try ( + LegacyReaderContext readerContext = new LegacyReaderContext( + new ShardSearchContextId(UUIDs.randomBase64UUID(), 0L), + indexService, + shard, + shard.acquireSearcherSupplier(), + shardSearchRequest, + Long.MAX_VALUE + ) + ) { ThreadContext threadContext = new ThreadContext(Settings.EMPTY); final SecurityContext securityContext = new SecurityContext(Settings.EMPTY, threadContext); AuditTrailService auditTrailService = mock(AuditTrailService.class); @@ -76,8 +83,7 @@ public void testOnNewContextSetsAuthentication() throws Exception { IndicesAccessControl indicesAccessControl = mock(IndicesAccessControl.class); threadContext.putTransient(AuthorizationServiceField.INDICES_PERMISSIONS_KEY, indicesAccessControl); - SecuritySearchOperationListener listener = - new SecuritySearchOperationListener(securityContext, auditTrailService); + SecuritySearchOperationListener listener = new SecuritySearchOperationListener(securityContext, auditTrailService); listener.onNewScrollContext(readerContext); Authentication contextAuth = readerContext.getFromContext(AuthenticationField.AUTHENTICATION_KEY); @@ -91,11 +97,20 @@ public void testOnNewContextSetsAuthentication() throws Exception { public void testValidateSearchContext() throws Exception { final ShardSearchRequest shardSearchRequest = mock(ShardSearchRequest.class); when(shardSearchRequest.scroll()).thenReturn(new Scroll(TimeValue.timeValueMinutes(between(1, 10)))); - try (LegacyReaderContext readerContext = - new LegacyReaderContext(new ShardSearchContextId(UUIDs.randomBase64UUID(), 0L), indexService, shard, - shard.acquireSearcherSupplier(), shardSearchRequest, Long.MAX_VALUE)) { - readerContext.putInContext(AuthenticationField.AUTHENTICATION_KEY, - new Authentication(new User("test", "role"), new RealmRef("realm", "file", "node"), null)); + try ( + LegacyReaderContext readerContext = new LegacyReaderContext( + new ShardSearchContextId(UUIDs.randomBase64UUID(), 0L), + indexService, + shard, + shard.acquireSearcherSupplier(), + shardSearchRequest, + Long.MAX_VALUE + ) + ) { + readerContext.putInContext( + AuthenticationField.AUTHENTICATION_KEY, + new Authentication(new User("test", "role"), new RealmRef("realm", "file", "node"), null) + ); final IndicesAccessControl indicesAccessControl = mock(IndicesAccessControl.class); readerContext.putInContext(AuthorizationServiceField.INDICES_PERMISSIONS_KEY, indicesAccessControl); MockLicenseState licenseState = mock(MockLicenseState.class); @@ -103,11 +118,9 @@ public void testValidateSearchContext() throws Exception { ThreadContext threadContext = new ThreadContext(Settings.EMPTY); final SecurityContext securityContext = new SecurityContext(Settings.EMPTY, threadContext); AuditTrail auditTrail = mock(AuditTrail.class); - AuditTrailService auditTrailService = - new AuditTrailService(Collections.singletonList(auditTrail), licenseState); + AuditTrailService auditTrailService = new AuditTrailService(Collections.singletonList(auditTrail), licenseState); - SecuritySearchOperationListener listener = - new SecuritySearchOperationListener(securityContext, auditTrailService); + SecuritySearchOperationListener listener = new SecuritySearchOperationListener(securityContext, auditTrailService); try (StoredContext ignore = threadContext.newStoredContext(false)) { Authentication authentication = new Authentication(new User("test", "role"), new RealmRef("realm", "file", "node"), null); authentication.writeToContext(threadContext); @@ -119,8 +132,11 @@ public void testValidateSearchContext() throws Exception { try (StoredContext ignore = threadContext.newStoredContext(false)) { final String nodeName = randomAlphaOfLengthBetween(1, 8); final String realmName = randomAlphaOfLengthBetween(1, 16); - Authentication authentication = - new Authentication(new User("test", "role"), new RealmRef(realmName, "file", nodeName), null); + Authentication authentication = new Authentication( + new User("test", "role"), + new RealmRef(realmName, "file", nodeName), + null + ); authentication.writeToContext(threadContext); listener.validateReaderContext(readerContext, Empty.INSTANCE); assertThat(threadContext.getTransient(AuthorizationServiceField.INDICES_PERMISSIONS_KEY), is(indicesAccessControl)); @@ -131,19 +147,27 @@ public void testValidateSearchContext() throws Exception { final String nodeName = randomBoolean() ? "node" : randomAlphaOfLengthBetween(1, 8); final String realmName = randomBoolean() ? "realm" : randomAlphaOfLengthBetween(1, 16); final String type = randomAlphaOfLengthBetween(5, 16); - Authentication authentication = - new Authentication(new User("test", "role"), new RealmRef(realmName, type, nodeName), null); + Authentication authentication = new Authentication(new User("test", "role"), new RealmRef(realmName, type, nodeName), null); authentication.writeToContext(threadContext); threadContext.putTransient(ORIGINATING_ACTION_KEY, "action"); - threadContext.putTransient(AUTHORIZATION_INFO_KEY, - (AuthorizationInfo) () -> Collections.singletonMap(PRINCIPAL_ROLES_FIELD_NAME, authentication.getUser().roles())); + threadContext.putTransient( + AUTHORIZATION_INFO_KEY, + (AuthorizationInfo) () -> Collections.singletonMap(PRINCIPAL_ROLES_FIELD_NAME, authentication.getUser().roles()) + ); final InternalScrollSearchRequest request = new InternalScrollSearchRequest(); - SearchContextMissingException expected = expectThrows(SearchContextMissingException.class, - () -> listener.validateReaderContext(readerContext, request)); + SearchContextMissingException expected = expectThrows( + SearchContextMissingException.class, + () -> listener.validateReaderContext(readerContext, request) + ); assertEquals(readerContext.id(), expected.contextId()); assertThat(threadContext.getTransient(AuthorizationServiceField.INDICES_PERMISSIONS_KEY), nullValue()); - verify(auditTrail).accessDenied(eq(null), eq(authentication), eq("action"), eq(request), - authzInfoRoles(authentication.getUser().roles())); + verify(auditTrail).accessDenied( + eq(null), + eq(authentication), + eq("action"), + eq(request), + authzInfoRoles(authentication.getUser().roles()) + ); } // another user running as the original user @@ -152,8 +176,11 @@ public void testValidateSearchContext() throws Exception { final String realmName = randomBoolean() ? "realm" : randomAlphaOfLengthBetween(1, 16); final String type = randomAlphaOfLengthBetween(5, 16); User user = new User(new User("test", "role"), new User("authenticated", "runas")); - Authentication authentication = new Authentication(user, new RealmRef(realmName, type, nodeName), - new RealmRef(randomAlphaOfLengthBetween(1, 16), "file", nodeName)); + Authentication authentication = new Authentication( + user, + new RealmRef(realmName, type, nodeName), + new RealmRef(randomAlphaOfLengthBetween(1, 16), "file", nodeName) + ); authentication.writeToContext(threadContext); threadContext.putTransient(ORIGINATING_ACTION_KEY, "action"); final InternalScrollSearchRequest request = new InternalScrollSearchRequest(); @@ -167,27 +194,40 @@ public void testValidateSearchContext() throws Exception { final String nodeName = randomBoolean() ? "node" : randomAlphaOfLengthBetween(1, 8); final String realmName = randomBoolean() ? "realm" : randomAlphaOfLengthBetween(1, 16); final String type = randomAlphaOfLengthBetween(5, 16); - Authentication authentication = - new Authentication(new User("authenticated", "runas"), new RealmRef(realmName, type, nodeName), null); + Authentication authentication = new Authentication( + new User("authenticated", "runas"), + new RealmRef(realmName, type, nodeName), + null + ); authentication.writeToContext(threadContext); threadContext.putTransient(ORIGINATING_ACTION_KEY, "action"); - threadContext.putTransient(AUTHORIZATION_INFO_KEY, - (AuthorizationInfo) () -> Collections.singletonMap(PRINCIPAL_ROLES_FIELD_NAME, authentication.getUser().roles())); + threadContext.putTransient( + AUTHORIZATION_INFO_KEY, + (AuthorizationInfo) () -> Collections.singletonMap(PRINCIPAL_ROLES_FIELD_NAME, authentication.getUser().roles()) + ); final InternalScrollSearchRequest request = new InternalScrollSearchRequest(); - SearchContextMissingException expected = expectThrows(SearchContextMissingException.class, - () -> listener.validateReaderContext(readerContext, request)); + SearchContextMissingException expected = expectThrows( + SearchContextMissingException.class, + () -> listener.validateReaderContext(readerContext, request) + ); assertEquals(readerContext.id(), expected.contextId()); assertThat(threadContext.getTransient(AuthorizationServiceField.INDICES_PERMISSIONS_KEY), nullValue()); - verify(auditTrail).accessDenied(eq(null), eq(authentication), eq("action"), eq(request), - authzInfoRoles(authentication.getUser().roles())); + verify(auditTrail).accessDenied( + eq(null), + eq(authentication), + eq("action"), + eq(request), + authzInfoRoles(authentication.getUser().roles()) + ); } } } public void testEnsuredAuthenticatedUserIsSame() { Authentication original = new Authentication(new User("test", "role"), new RealmRef("realm", "file", "node"), null); - Authentication current = - randomBoolean() ? original : new Authentication(new User("test", "role"), new RealmRef("realm", "file", "node"), null); + Authentication current = randomBoolean() + ? original + : new Authentication(new User("test", "role"), new RealmRef("realm", "file", "node"), null); ShardSearchContextId contextId = new ShardSearchContextId(UUIDs.randomBase64UUID(), randomLong()); final String action = randomAlphaOfLength(4); TransportRequest request = Empty.INSTANCE; @@ -197,63 +237,156 @@ public void testEnsuredAuthenticatedUserIsSame() { AuditTrailService auditTrailService = new AuditTrailService(Collections.singletonList(auditTrail), licenseState); final String auditId = randomAlphaOfLengthBetween(8, 20); - ensureAuthenticatedUserIsSame(original, current, auditTrailService, contextId, action, request, auditId, - () -> Collections.singletonMap(PRINCIPAL_ROLES_FIELD_NAME, original.getUser().roles())); + ensureAuthenticatedUserIsSame( + original, + current, + auditTrailService, + contextId, + action, + request, + auditId, + () -> Collections.singletonMap(PRINCIPAL_ROLES_FIELD_NAME, original.getUser().roles()) + ); verifyZeroInteractions(auditTrail); // original user being run as User user = new User(new User("test", "role"), new User("authenticated", "runas")); - current = new Authentication(user, new RealmRef("realm", "file", "node"), - new RealmRef(randomAlphaOfLengthBetween(1, 16), "file", "node")); - ensureAuthenticatedUserIsSame(original, current, auditTrailService, contextId, action, request, auditId, - () -> Collections.singletonMap(PRINCIPAL_ROLES_FIELD_NAME, original.getUser().roles())); + current = new Authentication( + user, + new RealmRef("realm", "file", "node"), + new RealmRef(randomAlphaOfLengthBetween(1, 16), "file", "node") + ); + ensureAuthenticatedUserIsSame( + original, + current, + auditTrailService, + contextId, + action, + request, + auditId, + () -> Collections.singletonMap(PRINCIPAL_ROLES_FIELD_NAME, original.getUser().roles()) + ); verifyZeroInteractions(auditTrail); // both user are run as - current = new Authentication(user, new RealmRef("realm", "file", "node"), - new RealmRef(randomAlphaOfLengthBetween(1, 16), "file", "node")); + current = new Authentication( + user, + new RealmRef("realm", "file", "node"), + new RealmRef(randomAlphaOfLengthBetween(1, 16), "file", "node") + ); Authentication runAs = current; - ensureAuthenticatedUserIsSame(runAs, current, auditTrailService, contextId, action, request, auditId, - () -> Collections.singletonMap(PRINCIPAL_ROLES_FIELD_NAME, original.getUser().roles())); + ensureAuthenticatedUserIsSame( + runAs, + current, + auditTrailService, + contextId, + action, + request, + auditId, + () -> Collections.singletonMap(PRINCIPAL_ROLES_FIELD_NAME, original.getUser().roles()) + ); verifyZeroInteractions(auditTrail); // different authenticated by type - Authentication differentRealmType = - new Authentication(new User("test", "role"), new RealmRef("realm", randomAlphaOfLength(5), "node"), null); - SearchContextMissingException e = expectThrows(SearchContextMissingException.class, - () -> ensureAuthenticatedUserIsSame(original, differentRealmType, auditTrailService, contextId, action, request, auditId, - () -> Collections.singletonMap(PRINCIPAL_ROLES_FIELD_NAME, original.getUser().roles()))); + Authentication differentRealmType = new Authentication( + new User("test", "role"), + new RealmRef("realm", randomAlphaOfLength(5), "node"), + null + ); + SearchContextMissingException e = expectThrows( + SearchContextMissingException.class, + () -> ensureAuthenticatedUserIsSame( + original, + differentRealmType, + auditTrailService, + contextId, + action, + request, + auditId, + () -> Collections.singletonMap(PRINCIPAL_ROLES_FIELD_NAME, original.getUser().roles()) + ) + ); assertEquals(contextId, e.contextId()); - verify(auditTrail).accessDenied(eq(auditId), eq(differentRealmType), eq(action), eq(request), - authzInfoRoles(original.getUser().roles())); + verify(auditTrail).accessDenied( + eq(auditId), + eq(differentRealmType), + eq(action), + eq(request), + authzInfoRoles(original.getUser().roles()) + ); // wrong user - Authentication differentUser = - new Authentication(new User("test2", "role"), new RealmRef("realm", "realm", "node"), null); - e = expectThrows(SearchContextMissingException.class, - () -> ensureAuthenticatedUserIsSame(original, differentUser, auditTrailService, contextId, action, request, auditId, - () -> Collections.singletonMap(PRINCIPAL_ROLES_FIELD_NAME, original.getUser().roles()))); + Authentication differentUser = new Authentication(new User("test2", "role"), new RealmRef("realm", "realm", "node"), null); + e = expectThrows( + SearchContextMissingException.class, + () -> ensureAuthenticatedUserIsSame( + original, + differentUser, + auditTrailService, + contextId, + action, + request, + auditId, + () -> Collections.singletonMap(PRINCIPAL_ROLES_FIELD_NAME, original.getUser().roles()) + ) + ); assertEquals(contextId, e.contextId()); - verify(auditTrail).accessDenied(eq(auditId), eq(differentUser), eq(action), eq(request), - authzInfoRoles(original.getUser().roles())); + verify(auditTrail).accessDenied( + eq(auditId), + eq(differentUser), + eq(action), + eq(request), + authzInfoRoles(original.getUser().roles()) + ); // run as different user - Authentication diffRunAs = new Authentication(new User(new User("test2", "role"), new User("authenticated", "runas")), - new RealmRef("realm", "file", "node1"), new RealmRef("realm", "file", "node1")); - e = expectThrows(SearchContextMissingException.class, - () -> ensureAuthenticatedUserIsSame(original, diffRunAs, auditTrailService, contextId, action, request, auditId, - () -> Collections.singletonMap(PRINCIPAL_ROLES_FIELD_NAME, original.getUser().roles()))); + Authentication diffRunAs = new Authentication( + new User(new User("test2", "role"), new User("authenticated", "runas")), + new RealmRef("realm", "file", "node1"), + new RealmRef("realm", "file", "node1") + ); + e = expectThrows( + SearchContextMissingException.class, + () -> ensureAuthenticatedUserIsSame( + original, + diffRunAs, + auditTrailService, + contextId, + action, + request, + auditId, + () -> Collections.singletonMap(PRINCIPAL_ROLES_FIELD_NAME, original.getUser().roles()) + ) + ); assertEquals(contextId, e.contextId()); verify(auditTrail).accessDenied(eq(auditId), eq(diffRunAs), eq(action), eq(request), authzInfoRoles(original.getUser().roles())); // run as different looked up by type - Authentication runAsDiffType = new Authentication(user, new RealmRef("realm", "file", "node"), - new RealmRef(randomAlphaOfLengthBetween(1, 16), randomAlphaOfLengthBetween(5, 12), "node")); - e = expectThrows(SearchContextMissingException.class, - () -> ensureAuthenticatedUserIsSame(runAs, runAsDiffType, auditTrailService, contextId, action, request, auditId, - () -> Collections.singletonMap(PRINCIPAL_ROLES_FIELD_NAME, original.getUser().roles()))); + Authentication runAsDiffType = new Authentication( + user, + new RealmRef("realm", "file", "node"), + new RealmRef(randomAlphaOfLengthBetween(1, 16), randomAlphaOfLengthBetween(5, 12), "node") + ); + e = expectThrows( + SearchContextMissingException.class, + () -> ensureAuthenticatedUserIsSame( + runAs, + runAsDiffType, + auditTrailService, + contextId, + action, + request, + auditId, + () -> Collections.singletonMap(PRINCIPAL_ROLES_FIELD_NAME, original.getUser().roles()) + ) + ); assertEquals(contextId, e.contextId()); - verify(auditTrail).accessDenied(eq(auditId), eq(runAsDiffType), eq(action), eq(request), - authzInfoRoles(original.getUser().roles())); + verify(auditTrail).accessDenied( + eq(auditId), + eq(runAsDiffType), + eq(action), + eq(request), + authzInfoRoles(original.getUser().roles()) + ); } } diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/SnapshotUserRoleIntegTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/SnapshotUserRoleIntegTests.java index fde6ed834d2ae..c41ec68cbb7db 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/SnapshotUserRoleIntegTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/SnapshotUserRoleIntegTests.java @@ -52,9 +52,13 @@ public class SnapshotUserRoleIntegTests extends NativeRealmIntegTestCase { @Before public void setupClusterBeforeSnapshot() throws IOException { logger.info("--> creating repository"); - assertAcked(client().admin().cluster().preparePutRepository("repo") + assertAcked( + client().admin() + .cluster() + .preparePutRepository("repo") .setType("fs") - .setSettings(Settings.builder().put("location", randomRepoPath()))); + .setSettings(Settings.builder().put("location", randomRepoPath())) + ); logger.info("--> creating ordinary index"); final int shards = between(1, 10); @@ -64,86 +68,130 @@ public void setupClusterBeforeSnapshot() throws IOException { logger.info("--> creating snapshot_user user"); final String user = "snapshot_user"; - final char[] password = new char[] {'p', 'a', 's', 's', 'w', 'o', 'r', 'd'}; + final char[] password = new char[] { 'p', 'a', 's', 's', 'w', 'o', 'r', 'd' }; final String snapshotUserToken = basicAuthHeaderValue(user, new SecureString(password)); client = client().filterWithHeader(Collections.singletonMap("Authorization", snapshotUserToken)); - PutUserResponse response = new TestRestHighLevelClient().security().putUser( - PutUserRequest.withPassword(new User(user, List.of("snapshot_user")), password, true, RefreshPolicy.IMMEDIATE), - SECURITY_REQUEST_OPTIONS); + PutUserResponse response = new TestRestHighLevelClient().security() + .putUser( + PutUserRequest.withPassword(new User(user, List.of("snapshot_user")), password, true, RefreshPolicy.IMMEDIATE), + SECURITY_REQUEST_OPTIONS + ); assertTrue(response.isCreated()); ensureGreen(INTERNAL_SECURITY_MAIN_INDEX_7); } public void testSnapshotUserRoleCanSnapshotAndSeeAllIndices() { // view repositories - final GetRepositoriesResponse getRepositoriesResponse = client.admin().cluster().prepareGetRepositories(randomFrom("*", "_all")) - .get(); + final GetRepositoriesResponse getRepositoriesResponse = client.admin() + .cluster() + .prepareGetRepositories(randomFrom("*", "_all")) + .get(); assertThat(getRepositoriesResponse.repositories().size(), is(1)); assertThat(getRepositoriesResponse.repositories().get(0).name(), is("repo")); // view all indices, including restricted ones final GetIndexResponse getIndexResponse = client.admin().indices().prepareGetIndex().setIndices(randomFrom("_all", "*")).get(); assertThat(Arrays.asList(getIndexResponse.indices()), containsInAnyOrder(INTERNAL_SECURITY_MAIN_INDEX_7, ordinaryIndex)); // create snapshot that includes restricted indices - final CreateSnapshotResponse snapshotResponse = client.admin().cluster().prepareCreateSnapshot("repo", "snap") - .setIndices(randomFrom("_all", "*")).setWaitForCompletion(true).get(); + final CreateSnapshotResponse snapshotResponse = client.admin() + .cluster() + .prepareCreateSnapshot("repo", "snap") + .setIndices(randomFrom("_all", "*")) + .setWaitForCompletion(true) + .get(); assertThat(snapshotResponse.getSnapshotInfo().state(), is(SnapshotState.SUCCESS)); assertThat(snapshotResponse.getSnapshotInfo().indices(), containsInAnyOrder(INTERNAL_SECURITY_MAIN_INDEX_7, ordinaryIndex)); // view snapshots for repo final GetSnapshotsResponse getSnapshotResponse = client.admin().cluster().prepareGetSnapshots("repo").get(); assertThat(getSnapshotResponse.getSnapshots().size(), is(1)); assertThat(getSnapshotResponse.getSnapshots().get(0).snapshotId().getName(), is("snap")); - assertThat(getSnapshotResponse.getSnapshots().get(0).indices(), containsInAnyOrder(INTERNAL_SECURITY_MAIN_INDEX_7, - ordinaryIndex)); + assertThat(getSnapshotResponse.getSnapshots().get(0).indices(), containsInAnyOrder(INTERNAL_SECURITY_MAIN_INDEX_7, ordinaryIndex)); } public void testSnapshotUserRoleIsReserved() { final RestHighLevelClient restClient = new TestRestHighLevelClient(); - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> restClient.security().putRole( - new PutRoleRequest(Role.builder().name("snapshot_user").build(), RefreshPolicy.IMMEDIATE), SECURITY_REQUEST_OPTIONS)); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> restClient.security() + .putRole( + new PutRoleRequest(Role.builder().name("snapshot_user").build(), RefreshPolicy.IMMEDIATE), + SECURITY_REQUEST_OPTIONS + ) + ); assertThat(e.getMessage(), containsString("role [snapshot_user] is reserved and cannot be modified")); - e = expectThrows(ElasticsearchStatusException.class, - () -> restClient.security().deleteRole( - new DeleteRoleRequest("snapshot_user", RefreshPolicy.IMMEDIATE), SECURITY_REQUEST_OPTIONS)); + e = expectThrows( + ElasticsearchStatusException.class, + () -> restClient.security() + .deleteRole(new DeleteRoleRequest("snapshot_user", RefreshPolicy.IMMEDIATE), SECURITY_REQUEST_OPTIONS) + ); assertThat(e.getMessage(), containsString("role [snapshot_user] is reserved and cannot be deleted")); } public void testSnapshotUserRoleUnathorizedForDestructiveActions() { // try search all - assertThrowsAuthorizationException(() -> client.prepareSearch(randomFrom("_all", "*")).get(), "indices:data/read/search", - "snapshot_user"); + assertThrowsAuthorizationException( + () -> client.prepareSearch(randomFrom("_all", "*")).get(), + "indices:data/read/search", + "snapshot_user" + ); // try create index - assertThrowsAuthorizationException(() -> client.admin().indices().prepareCreate(ordinaryIndex + "2").get(), "indices:admin/create", - "snapshot_user"); + assertThrowsAuthorizationException( + () -> client.admin().indices().prepareCreate(ordinaryIndex + "2").get(), + "indices:admin/create", + "snapshot_user" + ); // try create another repo assertThrowsAuthorizationException( - () -> client.admin().cluster().preparePutRepository("some_other_repo").setType("fs") - .setSettings(Settings.builder().put("location", randomRepoPath())).get(), - "cluster:admin/repository/put", "snapshot_user"); + () -> client.admin() + .cluster() + .preparePutRepository("some_other_repo") + .setType("fs") + .setSettings(Settings.builder().put("location", randomRepoPath())) + .get(), + "cluster:admin/repository/put", + "snapshot_user" + ); // try delete repo - assertThrowsAuthorizationException(() -> client.admin().cluster().prepareDeleteRepository("repo").get(), - "cluster:admin/repository/delete", "snapshot_user"); + assertThrowsAuthorizationException( + () -> client.admin().cluster().prepareDeleteRepository("repo").get(), + "cluster:admin/repository/delete", + "snapshot_user" + ); // try fumble with snapshots assertThrowsAuthorizationException( - () -> client.admin().cluster().prepareRestoreSnapshot("repo", randomAlphaOfLength(4).toLowerCase(Locale.ROOT)).get(), - "cluster:admin/snapshot/restore", "snapshot_user"); + () -> client.admin().cluster().prepareRestoreSnapshot("repo", randomAlphaOfLength(4).toLowerCase(Locale.ROOT)).get(), + "cluster:admin/snapshot/restore", + "snapshot_user" + ); assertThrowsAuthorizationException( - () -> client.admin().cluster().prepareDeleteSnapshot("repo", randomAlphaOfLength(4).toLowerCase(Locale.ROOT)).get(), - "cluster:admin/snapshot/delete", "snapshot_user"); + () -> client.admin().cluster().prepareDeleteSnapshot("repo", randomAlphaOfLength(4).toLowerCase(Locale.ROOT)).get(), + "cluster:admin/snapshot/delete", + "snapshot_user" + ); // try destructive/revealing actions on all indices for (final String indexToTest : Arrays.asList(INTERNAL_SECURITY_MAIN_INDEX_7, SECURITY_MAIN_ALIAS, ordinaryIndex)) { assertThrowsAuthorizationException(() -> client.prepareSearch(indexToTest).get(), "indices:data/read/search", "snapshot_user"); - assertThrowsAuthorizationException(() -> client.prepareGet(indexToTest, "1").get(), "indices:data/read/get", - "snapshot_user"); - assertThrowsAuthorizationException(() -> client.prepareIndex(indexToTest).setSource("term", "val").get(), - "indices:data/write/index", "snapshot_user"); - assertThrowsAuthorizationException(() -> client.prepareUpdate(indexToTest, "1").setDoc("term", "val").get(), - "indices:data/write/update", "snapshot_user"); - assertThrowsAuthorizationException(() -> client.prepareDelete(indexToTest, "1").get(), "indices:data/write/delete", - "snapshot_user"); + assertThrowsAuthorizationException(() -> client.prepareGet(indexToTest, "1").get(), "indices:data/read/get", "snapshot_user"); + assertThrowsAuthorizationException( + () -> client.prepareIndex(indexToTest).setSource("term", "val").get(), + "indices:data/write/index", + "snapshot_user" + ); + assertThrowsAuthorizationException( + () -> client.prepareUpdate(indexToTest, "1").setDoc("term", "val").get(), + "indices:data/write/update", + "snapshot_user" + ); + assertThrowsAuthorizationException( + () -> client.prepareDelete(indexToTest, "1").get(), + "indices:data/write/delete", + "snapshot_user" + ); - assertThrowsAuthorizationException(() -> client.admin().indices().prepareDelete(indexToTest).get(), "indices:admin/delete", - "snapshot_user"); + assertThrowsAuthorizationException( + () -> client.admin().indices().prepareDelete(indexToTest).get(), + "indices:admin/delete", + "snapshot_user" + ); } } diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/WriteActionsTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/WriteActionsTests.java index 20d10962f9810..5ac35a9a8c935 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/WriteActionsTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/WriteActionsTests.java @@ -33,31 +33,36 @@ public class WriteActionsTests extends SecurityIntegTestCase { @Override protected String configRoles() { - return SecuritySettingsSource.TEST_ROLE + ":\n" + - " cluster: [ ALL ]\n" + - " indices:\n" + - " - names: 'missing'\n" + - " privileges: [ 'indices:admin/create', 'indices:admin/auto_create', " + - "'indices:admin/delete' ]\n" + - " - names: ['/index.*/']\n" + - " privileges: [ manage ]\n" + - " - names: ['/test.*/']\n" + - " privileges: [ manage, write ]\n" + - " - names: '/test.*/'\n" + - " privileges: [ read ]\n"; + return SecuritySettingsSource.TEST_ROLE + + ":\n" + + " cluster: [ ALL ]\n" + + " indices:\n" + + " - names: 'missing'\n" + + " privileges: [ 'indices:admin/create', 'indices:admin/auto_create', " + + "'indices:admin/delete' ]\n" + + " - names: ['/index.*/']\n" + + " privileges: [ manage ]\n" + + " - names: ['/test.*/']\n" + + " privileges: [ manage, write ]\n" + + " - names: '/test.*/'\n" + + " privileges: [ read ]\n"; } public void testIndex() { createIndex("test1", "index1"); client().prepareIndex("test1").setId("id").setSource("field", "value").get(); - assertThrowsAuthorizationExceptionDefaultUsers(client().prepareIndex("index1").setId("id").setSource("field", "value")::get, - BulkAction.NAME + "[s]"); + assertThrowsAuthorizationExceptionDefaultUsers( + client().prepareIndex("index1").setId("id").setSource("field", "value")::get, + BulkAction.NAME + "[s]" + ); client().prepareIndex("test4").setId("id").setSource("field", "value").get(); - //the missing index gets automatically created (user has permissions for that), but indexing fails due to missing authorization - assertThrowsAuthorizationExceptionDefaultUsers(client().prepareIndex("missing").setId("id").setSource("field", "value")::get, - BulkAction.NAME + "[s]"); + // the missing index gets automatically created (user has permissions for that), but indexing fails due to missing authorization + assertThrowsAuthorizationExceptionDefaultUsers( + client().prepareIndex("missing").setId("id").setSource("field", "value")::get, + BulkAction.NAME + "[s]" + ); ensureGreen(); } @@ -75,36 +80,45 @@ public void testDelete() { public void testUpdate() { createIndex("test1", "index1"); client().prepareIndex("test1").setId("id").setSource("field", "value").get(); - assertEquals(RestStatus.OK, client().prepareUpdate("test1", "id") - .setDoc(Requests.INDEX_CONTENT_TYPE, "field2", "value2").get().status()); + assertEquals( + RestStatus.OK, + client().prepareUpdate("test1", "id").setDoc(Requests.INDEX_CONTENT_TYPE, "field2", "value2").get().status() + ); - assertThrowsAuthorizationExceptionDefaultUsers(client().prepareUpdate("index1", "id") - .setDoc(Requests.INDEX_CONTENT_TYPE, "field2", "value2")::get, UpdateAction.NAME); + assertThrowsAuthorizationExceptionDefaultUsers( + client().prepareUpdate("index1", "id").setDoc(Requests.INDEX_CONTENT_TYPE, "field2", "value2")::get, + UpdateAction.NAME + ); - expectThrows(DocumentMissingException.class, () -> client().prepareUpdate("test4", "id") - .setDoc(Requests.INDEX_CONTENT_TYPE, "field2", "value2").get()); + expectThrows( + DocumentMissingException.class, + () -> client().prepareUpdate("test4", "id").setDoc(Requests.INDEX_CONTENT_TYPE, "field2", "value2").get() + ); - assertThrowsAuthorizationExceptionDefaultUsers(client().prepareUpdate("missing", "id") - .setDoc(Requests.INDEX_CONTENT_TYPE, "field2", "value2")::get, UpdateAction.NAME); + assertThrowsAuthorizationExceptionDefaultUsers( + client().prepareUpdate("missing", "id").setDoc(Requests.INDEX_CONTENT_TYPE, "field2", "value2")::get, + UpdateAction.NAME + ); ensureGreen(); } public void testBulk() { createIndex("test1", "test2", "test3", "index1"); BulkResponse bulkResponse = client().prepareBulk() - .add(new IndexRequest("test1").id("id").source(Requests.INDEX_CONTENT_TYPE, "field", "value")) - .add(new IndexRequest("index1").id("id").source(Requests.INDEX_CONTENT_TYPE, "field", "value")) - .add(new IndexRequest("test4").id("id").source(Requests.INDEX_CONTENT_TYPE, "field", "value")) - .add(new IndexRequest("missing").id("id").source(Requests.INDEX_CONTENT_TYPE, "field", "value")) - .add(new DeleteRequest("test1", "id")) - .add(new DeleteRequest("index1", "id")) - .add(new DeleteRequest("test4", "id")) - .add(new DeleteRequest("missing", "id")) - .add(new IndexRequest("test1").id("id").source(Requests.INDEX_CONTENT_TYPE, "field", "value")) - .add(new UpdateRequest("test1", "id").doc(Requests.INDEX_CONTENT_TYPE, "field", "value")) - .add(new UpdateRequest("index1", "id").doc(Requests.INDEX_CONTENT_TYPE, "field", "value")) - .add(new UpdateRequest("test4", "id").doc(Requests.INDEX_CONTENT_TYPE, "field", "value")) - .add(new UpdateRequest("missing", "id").doc(Requests.INDEX_CONTENT_TYPE, "field", "value")).get(); + .add(new IndexRequest("test1").id("id").source(Requests.INDEX_CONTENT_TYPE, "field", "value")) + .add(new IndexRequest("index1").id("id").source(Requests.INDEX_CONTENT_TYPE, "field", "value")) + .add(new IndexRequest("test4").id("id").source(Requests.INDEX_CONTENT_TYPE, "field", "value")) + .add(new IndexRequest("missing").id("id").source(Requests.INDEX_CONTENT_TYPE, "field", "value")) + .add(new DeleteRequest("test1", "id")) + .add(new DeleteRequest("index1", "id")) + .add(new DeleteRequest("test4", "id")) + .add(new DeleteRequest("missing", "id")) + .add(new IndexRequest("test1").id("id").source(Requests.INDEX_CONTENT_TYPE, "field", "value")) + .add(new UpdateRequest("test1", "id").doc(Requests.INDEX_CONTENT_TYPE, "field", "value")) + .add(new UpdateRequest("index1", "id").doc(Requests.INDEX_CONTENT_TYPE, "field", "value")) + .add(new UpdateRequest("test4", "id").doc(Requests.INDEX_CONTENT_TYPE, "field", "value")) + .add(new UpdateRequest("missing", "id").doc(Requests.INDEX_CONTENT_TYPE, "field", "value")) + .get(); assertTrue(bulkResponse.hasFailures()); assertThat(bulkResponse.getItems().length, equalTo(13)); assertThat(bulkResponse.getItems()[0].getFailure(), nullValue()); @@ -116,8 +130,10 @@ public void testBulk() { assertThat(bulkResponse.getItems()[1].getOpType(), equalTo(DocWriteRequest.OpType.INDEX)); assertThat(bulkResponse.getItems()[1].getFailure().getIndex(), equalTo("index1")); assertAuthorizationExceptionDefaultUsers(bulkResponse.getItems()[1].getFailure().getCause(), BulkAction.NAME + "[s]"); - assertThat(bulkResponse.getItems()[1].getFailure().getCause().getMessage(), - containsString("[indices:data/write/bulk[s]] is unauthorized")); + assertThat( + bulkResponse.getItems()[1].getFailure().getCause().getMessage(), + containsString("[indices:data/write/bulk[s]] is unauthorized") + ); assertThat(bulkResponse.getItems()[2].getFailure(), nullValue()); assertThat(bulkResponse.getItems()[2].isFailed(), equalTo(false)); assertThat(bulkResponse.getItems()[2].getOpType(), equalTo(DocWriteRequest.OpType.INDEX)); @@ -125,12 +141,14 @@ public void testBulk() { assertThat(bulkResponse.getItems()[3].getFailure(), notNullValue()); assertThat(bulkResponse.getItems()[3].isFailed(), equalTo(true)); assertThat(bulkResponse.getItems()[3].getOpType(), equalTo(DocWriteRequest.OpType.INDEX)); - //the missing index gets automatically created (user has permissions for that), but indexing fails due to missing authorization + // the missing index gets automatically created (user has permissions for that), but indexing fails due to missing authorization assertThat(bulkResponse.getItems()[3].getFailure().getIndex(), equalTo("missing")); assertThat(bulkResponse.getItems()[3].getFailure().getCause(), instanceOf(ElasticsearchSecurityException.class)); assertAuthorizationExceptionDefaultUsers(bulkResponse.getItems()[3].getFailure().getCause(), BulkAction.NAME + "[s]"); - assertThat(bulkResponse.getItems()[3].getFailure().getCause().getMessage(), - containsString("[indices:data/write/bulk[s]] is unauthorized")); + assertThat( + bulkResponse.getItems()[3].getFailure().getCause().getMessage(), + containsString("[indices:data/write/bulk[s]] is unauthorized") + ); assertThat(bulkResponse.getItems()[4].getFailure(), nullValue()); assertThat(bulkResponse.getItems()[4].isFailed(), equalTo(false)); assertThat(bulkResponse.getItems()[4].getOpType(), equalTo(DocWriteRequest.OpType.DELETE)); @@ -140,8 +158,10 @@ public void testBulk() { assertThat(bulkResponse.getItems()[5].getOpType(), equalTo(DocWriteRequest.OpType.DELETE)); assertThat(bulkResponse.getItems()[5].getFailure().getIndex(), equalTo("index1")); assertAuthorizationExceptionDefaultUsers(bulkResponse.getItems()[5].getFailure().getCause(), BulkAction.NAME + "[s]"); - assertThat(bulkResponse.getItems()[5].getFailure().getCause().getMessage(), - containsString("[indices:data/write/bulk[s]] is unauthorized")); + assertThat( + bulkResponse.getItems()[5].getFailure().getCause().getMessage(), + containsString("[indices:data/write/bulk[s]] is unauthorized") + ); assertThat(bulkResponse.getItems()[6].getFailure(), nullValue()); assertThat(bulkResponse.getItems()[6].isFailed(), equalTo(false)); assertThat(bulkResponse.getItems()[6].getOpType(), equalTo(DocWriteRequest.OpType.DELETE)); @@ -151,8 +171,10 @@ public void testBulk() { assertThat(bulkResponse.getItems()[7].getOpType(), equalTo(DocWriteRequest.OpType.DELETE)); assertThat(bulkResponse.getItems()[7].getFailure().getIndex(), equalTo("missing")); assertAuthorizationExceptionDefaultUsers(bulkResponse.getItems()[7].getFailure().getCause(), BulkAction.NAME + "[s]"); - assertThat(bulkResponse.getItems()[7].getFailure().getCause().getMessage(), - containsString("[indices:data/write/bulk[s]] is unauthorized")); + assertThat( + bulkResponse.getItems()[7].getFailure().getCause().getMessage(), + containsString("[indices:data/write/bulk[s]] is unauthorized") + ); assertThat(bulkResponse.getItems()[8].getFailure(), nullValue()); assertThat(bulkResponse.getItems()[8].isFailed(), equalTo(false)); assertThat(bulkResponse.getItems()[8].getOpType(), equalTo(DocWriteRequest.OpType.INDEX)); @@ -166,8 +188,10 @@ public void testBulk() { assertThat(bulkResponse.getItems()[10].getOpType(), equalTo(DocWriteRequest.OpType.UPDATE)); assertThat(bulkResponse.getItems()[10].getFailure().getIndex(), equalTo("index1")); assertAuthorizationExceptionDefaultUsers(bulkResponse.getItems()[10].getFailure().getCause(), BulkAction.NAME + "[s]"); - assertThat(bulkResponse.getItems()[10].getFailure().getCause().getMessage(), - containsString("[indices:data/write/bulk[s]] is unauthorized")); + assertThat( + bulkResponse.getItems()[10].getFailure().getCause().getMessage(), + containsString("[indices:data/write/bulk[s]] is unauthorized") + ); assertThat(bulkResponse.getItems()[11].getFailure(), notNullValue()); assertThat(bulkResponse.getItems()[11].isFailed(), equalTo(true)); assertThat(bulkResponse.getItems()[11].getOpType(), equalTo(DocWriteRequest.OpType.UPDATE)); @@ -179,8 +203,10 @@ public void testBulk() { assertThat(bulkResponse.getItems()[12].getFailure().getIndex(), equalTo("missing")); assertThat(bulkResponse.getItems()[12].getFailure().getCause(), instanceOf(ElasticsearchSecurityException.class)); assertAuthorizationExceptionDefaultUsers(bulkResponse.getItems()[12].getFailure().getCause(), BulkAction.NAME + "[s]"); - assertThat(bulkResponse.getItems()[12].getFailure().getCause().getMessage(), - containsString("[indices:data/write/bulk[s]] is unauthorized")); + assertThat( + bulkResponse.getItems()[12].getFailure().getCause().getMessage(), + containsString("[indices:data/write/bulk[s]] is unauthorized") + ); ensureGreen(); } } diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/store/NativePrivilegeStoreCacheTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/store/NativePrivilegeStoreCacheTests.java index 7b0264527de04..328f4d28b7d0f 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/store/NativePrivilegeStoreCacheTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/store/NativePrivilegeStoreCacheTests.java @@ -62,8 +62,7 @@ public class NativePrivilegeStoreCacheTests extends SecuritySingleNodeTestCase { @Override protected String configUsers() { - return super.configUsers() - + APP_USER_NAME + ":" + TEST_PASSWORD_HASHED + "\n"; + return super.configUsers() + APP_USER_NAME + ":" + TEST_PASSWORD_HASHED + "\n"; } @Override @@ -85,9 +84,7 @@ protected String configRoles() { @Override protected String configUsersRoles() { - return super.configUsersRoles() - + "app_role:" + APP_USER_NAME + "\n" - + TEST_ROLE + ":" + APP_USER_NAME + "\n"; + return super.configUsersRoles() + "app_role:" + APP_USER_NAME + "\n" + TEST_ROLE + ":" + APP_USER_NAME + "\n"; } @Override @@ -107,12 +104,12 @@ public void configureApplicationPrivileges() { new ApplicationPrivilegeDescriptor("app-1", "admin", Set.of("a:a:b:c", "a:x:y:z"), emptyMap()), new ApplicationPrivilegeDescriptor("app-2", "read", Set.of("r:e:f:g", "r:t:u:v"), emptyMap()), new ApplicationPrivilegeDescriptor("app-2", "write", Set.of("w:e:f:g", "w:t:u:v"), emptyMap()), - new ApplicationPrivilegeDescriptor("app-2", "admin", Set.of("a:e:f:g", "a:t:u:v"), emptyMap())); + new ApplicationPrivilegeDescriptor("app-2", "admin", Set.of("a:e:f:g", "a:t:u:v"), emptyMap()) + ); final PutPrivilegesRequest putPrivilegesRequest = new PutPrivilegesRequest(); putPrivilegesRequest.setPrivileges(applicationPrivilegeDescriptors); - final ActionFuture future = - client().execute(PutPrivilegesAction.INSTANCE, putPrivilegesRequest); + final ActionFuture future = client().execute(PutPrivilegesAction.INSTANCE, putPrivilegesRequest); final PutPrivilegesResponse putPrivilegesResponse = future.actionGet(); assertEquals(2, putPrivilegesResponse.created().size()); @@ -122,8 +119,11 @@ public void configureApplicationPrivileges() { public void testGetPrivilegesUsesCache() { final Client client = client(); - ApplicationPrivilegeDescriptor[] privileges = new GetPrivilegesRequestBuilder(client) - .application("app-2").privileges("write").execute().actionGet().privileges(); + ApplicationPrivilegeDescriptor[] privileges = new GetPrivilegesRequestBuilder(client).application("app-2") + .privileges("write") + .execute() + .actionGet() + .privileges(); assertEquals(1, privileges.length); assertEquals("app-2", privileges[0].getApplication()); @@ -131,40 +131,43 @@ public void testGetPrivilegesUsesCache() { // A hacky way to test cache is populated and used by deleting the backing documents. // The test will fail if the cache is not in place - assertFalse(client.prepareBulk() - .add(new DeleteRequest(SECURITY_MAIN_ALIAS, DOC_TYPE_VALUE + "_app-2:read")) - .add(new DeleteRequest(SECURITY_MAIN_ALIAS, DOC_TYPE_VALUE + "_app-2:write")) - .add(new DeleteRequest(SECURITY_MAIN_ALIAS, DOC_TYPE_VALUE + "_app-2:admin")) - .setRefreshPolicy(IMMEDIATE).execute().actionGet().hasFailures()); + assertFalse( + client.prepareBulk() + .add(new DeleteRequest(SECURITY_MAIN_ALIAS, DOC_TYPE_VALUE + "_app-2:read")) + .add(new DeleteRequest(SECURITY_MAIN_ALIAS, DOC_TYPE_VALUE + "_app-2:write")) + .add(new DeleteRequest(SECURITY_MAIN_ALIAS, DOC_TYPE_VALUE + "_app-2:admin")) + .setRefreshPolicy(IMMEDIATE) + .execute() + .actionGet() + .hasFailures() + ); // We can still get the privileges because it is cached - privileges = new GetPrivilegesRequestBuilder(client) - .application("app-2").privileges("read").execute().actionGet().privileges(); + privileges = new GetPrivilegesRequestBuilder(client).application("app-2").privileges("read").execute().actionGet().privileges(); assertEquals(1, privileges.length); // We can get all app-2 privileges because cache is keyed by application - privileges = new GetPrivilegesRequestBuilder(client) - .application("app-2").execute().actionGet().privileges(); + privileges = new GetPrivilegesRequestBuilder(client).application("app-2").execute().actionGet().privileges(); assertEquals(3, privileges.length); // Now properly invalidate the cache - final ClearPrivilegesCacheResponse clearPrivilegesCacheResponse = - client.execute(ClearPrivilegesCacheAction.INSTANCE, new ClearPrivilegesCacheRequest()).actionGet(); + final ClearPrivilegesCacheResponse clearPrivilegesCacheResponse = client.execute( + ClearPrivilegesCacheAction.INSTANCE, + new ClearPrivilegesCacheRequest() + ).actionGet(); assertFalse(clearPrivilegesCacheResponse.hasFailures()); // app-2 is no longer found - privileges = new GetPrivilegesRequestBuilder(client) - .application("app-2").privileges("read").execute().actionGet().privileges(); + privileges = new GetPrivilegesRequestBuilder(client).application("app-2").privileges("read").execute().actionGet().privileges(); assertEquals(0, privileges.length); } public void testPopulationOfCacheWhenLoadingPrivilegesForAllApplications() { final Client client = client(); - ApplicationPrivilegeDescriptor[] privileges = new GetPrivilegesRequestBuilder(client) - .execute().actionGet().privileges(); + ApplicationPrivilegeDescriptor[] privileges = new GetPrivilegesRequestBuilder(client).execute().actionGet().privileges(); assertEquals(6, privileges.length); @@ -172,8 +175,10 @@ public void testPopulationOfCacheWhenLoadingPrivilegesForAllApplications() { deleteApplicationPrivilege("app-2", "read"); // A direct read should also get nothing - assertEquals(0, new GetPrivilegesRequestBuilder(client) - .application("app-2").privileges("read").execute().actionGet().privileges().length); + assertEquals( + 0, + new GetPrivilegesRequestBuilder(client).application("app-2").privileges("read").execute().actionGet().privileges().length + ); // The wildcard expression expansion should be invalidated assertEquals(5, new GetPrivilegesRequestBuilder(client).execute().actionGet().privileges().length); @@ -188,18 +193,27 @@ public void testPopulationOfCacheWhenLoadingPrivilegesForAllApplications() { // The descriptors cache is keyed by application name hence removal of a app-2 privilege only affects // app-2, but not app-1. The cache hit/miss is tested by removing the backing documents - assertFalse(client.prepareBulk() - .add(new DeleteRequest(SECURITY_MAIN_ALIAS, DOC_TYPE_VALUE + "_app-1:write")) - .add(new DeleteRequest(SECURITY_MAIN_ALIAS, DOC_TYPE_VALUE + "_app-2:write")) - .setRefreshPolicy(IMMEDIATE).execute().actionGet().hasFailures()); + assertFalse( + client.prepareBulk() + .add(new DeleteRequest(SECURITY_MAIN_ALIAS, DOC_TYPE_VALUE + "_app-1:write")) + .add(new DeleteRequest(SECURITY_MAIN_ALIAS, DOC_TYPE_VALUE + "_app-2:write")) + .setRefreshPolicy(IMMEDIATE) + .execute() + .actionGet() + .hasFailures() + ); // app-2 write privilege will not be found since cache is invalidated and backing document is gone - assertEquals(0, new GetPrivilegesRequestBuilder(client) - .application("app-2").privileges("write").execute().actionGet().privileges().length); + assertEquals( + 0, + new GetPrivilegesRequestBuilder(client).application("app-2").privileges("write").execute().actionGet().privileges().length + ); // app-1 write privilege is still found since it is cached even when the backing document is gone - assertEquals(1, new GetPrivilegesRequestBuilder(client) - .application("app-1").privileges("write").execute().actionGet().privileges().length); + assertEquals( + 1, + new GetPrivilegesRequestBuilder(client).application("app-1").privileges("write").execute().actionGet().privileges().length + ); } public void testSuffixWildcard() { @@ -209,34 +223,77 @@ public void testSuffixWildcard() { assertEquals(6, new GetPrivilegesRequestBuilder(client).application("app-*").execute().actionGet().privileges().length); // Delete a backing document - assertEquals(RestStatus.OK, client.prepareDelete(SECURITY_MAIN_ALIAS, DOC_TYPE_VALUE + "_app-1:read") - .setRefreshPolicy(IMMEDIATE).execute().actionGet().status()); + assertEquals( + RestStatus.OK, + client.prepareDelete(SECURITY_MAIN_ALIAS, DOC_TYPE_VALUE + "_app-1:read") + .setRefreshPolicy(IMMEDIATE) + .execute() + .actionGet() + .status() + ); // A direct get privilege with no wildcard should still hit the cache without needing it to be in the names cache - assertEquals(1, new GetPrivilegesRequestBuilder(client).application("app-1") - .privileges("read").execute().actionGet().privileges().length); + assertEquals( + 1, + new GetPrivilegesRequestBuilder(client).application("app-1").privileges("read").execute().actionGet().privileges().length + ); } public void testHasPrivileges() { - assertTrue(checkPrivilege("app-1", "read").getApplicationPrivileges() - .get("app-1").stream().findFirst().orElseThrow().getPrivileges().get("read")); + assertTrue( + checkPrivilege("app-1", "read").getApplicationPrivileges() + .get("app-1") + .stream() + .findFirst() + .orElseThrow() + .getPrivileges() + .get("read") + ); - assertFalse(checkPrivilege("app-1", "check").getApplicationPrivileges() - .get("app-1").stream().findFirst().orElseThrow().getPrivileges().get("check")); + assertFalse( + checkPrivilege("app-1", "check").getApplicationPrivileges() + .get("app-1") + .stream() + .findFirst() + .orElseThrow() + .getPrivileges() + .get("check") + ); // Add the app-1 check privilege and it should be picked up addApplicationPrivilege("app-1", "check", "c:a:b:c"); - assertTrue(checkPrivilege("app-1", "check").getApplicationPrivileges() - .get("app-1").stream().findFirst().orElseThrow().getPrivileges().get("check")); + assertTrue( + checkPrivilege("app-1", "check").getApplicationPrivileges() + .get("app-1") + .stream() + .findFirst() + .orElseThrow() + .getPrivileges() + .get("check") + ); // Delete the app-1 read privilege and it should be picked up as well deleteApplicationPrivilege("app-1", "read"); - assertFalse(checkPrivilege("app-1", "read").getApplicationPrivileges() - .get("app-1").stream().findFirst().orElseThrow().getPrivileges().get("read")); + assertFalse( + checkPrivilege("app-1", "read").getApplicationPrivileges() + .get("app-1") + .stream() + .findFirst() + .orElseThrow() + .getPrivileges() + .get("read") + ); // TODO: This is a bug - assertTrue(checkPrivilege("app-2", "check").getApplicationPrivileges() - .get("app-2").stream().findFirst().orElseThrow().getPrivileges().get("check")); + assertTrue( + checkPrivilege("app-2", "check").getApplicationPrivileges() + .get("app-2") + .stream() + .findFirst() + .orElseThrow() + .getPrivileges() + .get("check") + ); } public void testRolesCacheIsClearedWhenPrivilegesIsChanged() { @@ -245,22 +302,25 @@ public void testRolesCacheIsClearedWhenPrivilegesIsChanged() { // Add a new user and role so they do not interfere existing tests final String testRole = "test_role_cache_role"; final String testRoleCacheUser = "test_role_cache_user"; - final PutRoleResponse putRoleResponse = new PutRoleRequestBuilder(client).name(testRole). - cluster("all") + final PutRoleResponse putRoleResponse = new PutRoleRequestBuilder(client).name(testRole) + .cluster("all") .addIndices(new String[] { "*" }, new String[] { "read" }, null, null, null, false) .get(); assertTrue(putRoleResponse.isCreated()); final Hasher hasher = getFastStoredHashAlgoForTests(); - final PutUserResponse putUserResponse = new PutUserRequestBuilder(client) - .username(testRoleCacheUser) + final PutUserResponse putUserResponse = new PutUserRequestBuilder(client).username(testRoleCacheUser) .roles(testRole) .password(new SecureString("longerpassword".toCharArray()), hasher) .get(); assertTrue(putUserResponse.created()); // The created user can access cluster health because its role grants access - final Client testRoleCacheUserClient = client.filterWithHeader(singletonMap("Authorization", - "Basic " + Base64.getEncoder().encodeToString((testRoleCacheUser + ":longerpassword").getBytes(StandardCharsets.UTF_8)))); + final Client testRoleCacheUserClient = client.filterWithHeader( + singletonMap( + "Authorization", + "Basic " + Base64.getEncoder().encodeToString((testRoleCacheUser + ":longerpassword").getBytes(StandardCharsets.UTF_8)) + ) + ); new ClusterHealthRequestBuilder(testRoleCacheUserClient, ClusterHealthAction.INSTANCE).get(); // Directly deleted the role document @@ -277,21 +337,30 @@ public void testRolesCacheIsClearedWhenPrivilegesIsChanged() { addApplicationPrivilege("app-3", "read", "r:q:r:s"); } // Since role cache is cleared, the cluster health action is no longer authorized - expectThrows(ElasticsearchSecurityException.class, - () -> new ClusterHealthRequestBuilder(testRoleCacheUserClient, ClusterHealthAction.INSTANCE).get()); + expectThrows( + ElasticsearchSecurityException.class, + () -> new ClusterHealthRequestBuilder(testRoleCacheUserClient, ClusterHealthAction.INSTANCE).get() + ); } private HasPrivilegesResponse checkPrivilege(String applicationName, String privilegeName) { - final Client client = client().filterWithHeader(singletonMap("Authorization", - "Basic " + Base64.getEncoder().encodeToString(("app_user:" + TEST_PASSWORD).getBytes(StandardCharsets.UTF_8)))); + final Client client = client().filterWithHeader( + singletonMap( + "Authorization", + "Basic " + Base64.getEncoder().encodeToString(("app_user:" + TEST_PASSWORD).getBytes(StandardCharsets.UTF_8)) + ) + ); // Has privileges always loads all privileges for an application final HasPrivilegesRequest hasPrivilegesRequest = new HasPrivilegesRequest(); hasPrivilegesRequest.username(APP_USER_NAME); hasPrivilegesRequest.applicationPrivileges( RoleDescriptor.ApplicationResourcePrivileges.builder() - .application(applicationName).privileges(privilegeName).resources("foo").build() + .application(applicationName) + .privileges(privilegeName) + .resources("foo") + .build() ); hasPrivilegesRequest.clusterPrivileges("monitor"); hasPrivilegesRequest.indexPrivileges(RoleDescriptor.IndicesPrivileges.builder().indices("*").privileges("read").build()); @@ -300,14 +369,21 @@ private HasPrivilegesResponse checkPrivilege(String applicationName, String priv private void addApplicationPrivilege(String applicationName, String privilegeName, String... actions) { final List applicationPrivilegeDescriptors = Collections.singletonList( - new ApplicationPrivilegeDescriptor(applicationName, privilegeName, Set.of(actions), emptyMap())); + new ApplicationPrivilegeDescriptor(applicationName, privilegeName, Set.of(actions), emptyMap()) + ); final PutPrivilegesRequest putPrivilegesRequest = new PutPrivilegesRequest(); putPrivilegesRequest.setPrivileges(applicationPrivilegeDescriptors); assertEquals(1, client().execute(PutPrivilegesAction.INSTANCE, putPrivilegesRequest).actionGet().created().keySet().size()); } private void deleteApplicationPrivilege(String applicationName, String privilegeName) { - assertEquals(singleton(privilegeName), new DeletePrivilegesRequestBuilder(client()) - .application(applicationName).privileges(new String[] { privilegeName }).execute().actionGet().found()); + assertEquals( + singleton(privilegeName), + new DeletePrivilegesRequestBuilder(client()).application(applicationName) + .privileges(new String[] { privilegeName }) + .execute() + .actionGet() + .found() + ); } } diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/operator/OperatorPrivilegesPartiallyEnabledIntegTestCase.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/operator/OperatorPrivilegesPartiallyEnabledIntegTestCase.java index 105b46b48164b..d3b96b7fe6817 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/operator/OperatorPrivilegesPartiallyEnabledIntegTestCase.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/operator/OperatorPrivilegesPartiallyEnabledIntegTestCase.java @@ -32,21 +32,17 @@ public class OperatorPrivilegesPartiallyEnabledIntegTestCase extends SecurityInt @Override protected String configUsers() { - return super.configUsers() - + OPERATOR_USER_NAME + ":" + TEST_PASSWORD_HASHED + "\n"; + return super.configUsers() + OPERATOR_USER_NAME + ":" + TEST_PASSWORD_HASHED + "\n"; } @Override protected String configUsersRoles() { - return super.configUsersRoles() - + "superuser:" + OPERATOR_USER_NAME + "\n"; + return super.configUsersRoles() + "superuser:" + OPERATOR_USER_NAME + "\n"; } @Override protected String configOperatorUsers() { - return super.configOperatorUsers() - + "operator:\n" - + " - usernames: ['" + OPERATOR_USER_NAME + "']\n"; + return super.configOperatorUsers() + "operator:\n" + " - usernames: ['" + OPERATOR_USER_NAME + "']\n"; } @Override diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/operator/OperatorPrivilegesSingleNodeTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/operator/OperatorPrivilegesSingleNodeTests.java index 657bca473590e..f745d033ff306 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/operator/OperatorPrivilegesSingleNodeTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/operator/OperatorPrivilegesSingleNodeTests.java @@ -33,8 +33,7 @@ public class OperatorPrivilegesSingleNodeTests extends SecuritySingleNodeTestCas @Override protected String configUsers() { - return super.configUsers() - + OPERATOR_USER_NAME + ":" + TEST_PASSWORD_HASHED + "\n"; + return super.configUsers() + OPERATOR_USER_NAME + ":" + TEST_PASSWORD_HASHED + "\n"; } @Override @@ -49,15 +48,12 @@ protected String configRoles() { @Override protected String configUsersRoles() { - return super.configUsersRoles() - + "limited_operator:" + OPERATOR_USER_NAME + "\n"; + return super.configUsersRoles() + "limited_operator:" + OPERATOR_USER_NAME + "\n"; } @Override protected String configOperatorUsers() { - return super.configOperatorUsers() - + "operator:\n" - + " - usernames: ['" + OPERATOR_USER_NAME + "']\n"; + return super.configOperatorUsers() + "operator:\n" + " - usernames: ['" + OPERATOR_USER_NAME + "']\n"; } @Override @@ -72,7 +68,8 @@ public void testNormalSuperuserWillFailToCallOperatorOnlyAction() { final ClearVotingConfigExclusionsRequest clearVotingConfigExclusionsRequest = new ClearVotingConfigExclusionsRequest(); final ElasticsearchSecurityException e = expectThrows( ElasticsearchSecurityException.class, - () -> client().execute(ClearVotingConfigExclusionsAction.INSTANCE, clearVotingConfigExclusionsRequest).actionGet()); + () -> client().execute(ClearVotingConfigExclusionsAction.INSTANCE, clearVotingConfigExclusionsRequest).actionGet() + ); assertThat(e.getCause().getMessage(), containsString("Operator privileges are required for action")); } @@ -84,8 +81,10 @@ public void testNormalSuperuserWillFailToSetOperatorOnlySettings() { } else { clusterUpdateSettingsRequest.persistentSettings(settings); } - final ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, - () -> client().execute(ClusterUpdateSettingsAction.INSTANCE, clusterUpdateSettingsRequest).actionGet()); + final ElasticsearchSecurityException e = expectThrows( + ElasticsearchSecurityException.class, + () -> client().execute(ClusterUpdateSettingsAction.INSTANCE, clusterUpdateSettingsRequest).actionGet() + ); assertThat(e.getCause().getMessage(), containsString("Operator privileges are required for setting")); } @@ -122,13 +121,16 @@ public void testOperatorUserWillSucceedToSetOperatorOnlySettings() { public void testOperatorUserIsStillSubjectToRoleLimits() { final Client client = createOperatorClient(); final GetUsersRequest getUsersRequest = new GetUsersRequest(); - final ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, - () -> client.execute(GetUsersAction.INSTANCE, getUsersRequest).actionGet()); + final ElasticsearchSecurityException e = expectThrows( + ElasticsearchSecurityException.class, + () -> client.execute(GetUsersAction.INSTANCE, getUsersRequest).actionGet() + ); assertThat(e.getMessage(), containsString("is unauthorized for user")); } private Client createOperatorClient() { - return client().filterWithHeader(Map.of("Authorization", - basicAuthHeaderValue(OPERATOR_USER_NAME, new SecureString(TEST_PASSWORD.toCharArray())))); + return client().filterWithHeader( + Map.of("Authorization", basicAuthHeaderValue(OPERATOR_USER_NAME, new SecureString(TEST_PASSWORD.toCharArray()))) + ); } } diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/rest/action/RestAuthenticateActionTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/rest/action/RestAuthenticateActionTests.java index 333992074c2cf..e4ce8c76edbe9 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/rest/action/RestAuthenticateActionTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/rest/action/RestAuthenticateActionTests.java @@ -47,8 +47,8 @@ protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { if (anonymousEnabled) { builder.put(AnonymousUser.USERNAME_SETTING.getKey(), "anon") - .putList(AnonymousUser.ROLES_SETTING.getKey(), SecuritySettingsSource.TEST_ROLE, "foo") - .put(AuthorizationService.ANONYMOUS_AUTHORIZATION_EXCEPTION_SETTING.getKey(), false); + .putList(AnonymousUser.ROLES_SETTING.getKey(), SecuritySettingsSource.TEST_ROLE, "foo") + .put(AuthorizationService.ANONYMOUS_AUTHORIZATION_EXCEPTION_SETTING.getKey(), false); } return builder.build(); } @@ -56,8 +56,13 @@ protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { public void testAuthenticateApi() throws Exception { Request request = new Request("GET", "/_security/_authenticate"); RequestOptions.Builder options = request.getOptions().toBuilder(); - options.addHeader("Authorization", basicAuthHeaderValue(SecuritySettingsSource.TEST_USER_NAME, - new SecureString(SecuritySettingsSourceField.TEST_PASSWORD.toCharArray()))); + options.addHeader( + "Authorization", + basicAuthHeaderValue( + SecuritySettingsSource.TEST_USER_NAME, + new SecureString(SecuritySettingsSourceField.TEST_PASSWORD.toCharArray()) + ) + ); request.setOptions(options); Response a = getRestClient().performRequest(request); ObjectPath objectPath = ObjectPath.createFromResponse(a); @@ -91,7 +96,7 @@ public void testAuthenticateApiWithoutAuthentication() throws Exception { } else { fail("request should have failed"); } - } catch(ResponseException e) { + } catch (ResponseException e) { if (anonymousEnabled) { fail("request should have succeeded"); } else { diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/support/SecurityIndexManagerIntegTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/support/SecurityIndexManagerIntegTests.java index ddbb533c0b21f..09da9a5f032e1 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/support/SecurityIndexManagerIntegTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/support/SecurityIndexManagerIntegTests.java @@ -49,10 +49,11 @@ protected void doRun() throws Exception { final List requests = new ArrayList<>(numRequests); final SecureString password = new SecureString("test-user-password".toCharArray()); for (int i = 0; i < numRequests; i++) { - requests.add(new PutUserRequestBuilder(client()) - .username("user" + userNumber.getAndIncrement()) - .password(password, getFastStoredHashAlgoForTests()) - .roles(randomAlphaOfLengthBetween(1, 16))); + requests.add( + new PutUserRequestBuilder(client()).username("user" + userNumber.getAndIncrement()) + .password(password, getFastStoredHashAlgoForTests()) + .roles(randomAlphaOfLengthBetween(1, 16)) + ); } barrier.await(10L, TimeUnit.SECONDS); diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/transport/filter/IpFilteringIntegrationTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/transport/filter/IpFilteringIntegrationTests.java index c19b48ad306cd..16e0b322efcac 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/transport/filter/IpFilteringIntegrationTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/transport/filter/IpFilteringIntegrationTests.java @@ -6,9 +6,9 @@ */ package org.elasticsearch.xpack.security.transport.filter; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.TransportAddress; +import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.http.HttpServerTransport; import org.elasticsearch.test.ESIntegTestCase.ClusterScope; import org.elasticsearch.test.ESIntegTestCase.Scope; @@ -42,27 +42,29 @@ protected boolean addMockHttpTransport() { @Override protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { - String randomClientPortRange = randomClientPort + "-" + (randomClientPort+100); - return Settings.builder().put(super.nodeSettings(nodeOrdinal, otherSettings)) - .put("transport.profiles.client.port", randomClientPortRange) - // make sure this is "localhost", no matter if ipv4 or ipv6, but be consistent - .put("transport.profiles.client.bind_host", "localhost") - .put("transport.profiles.client.xpack.security.filter.deny", "_all") - .put(IPFilter.TRANSPORT_FILTER_DENY_SETTING.getKey(), "_all") - .build(); + String randomClientPortRange = randomClientPort + "-" + (randomClientPort + 100); + return Settings.builder() + .put(super.nodeSettings(nodeOrdinal, otherSettings)) + .put("transport.profiles.client.port", randomClientPortRange) + // make sure this is "localhost", no matter if ipv4 or ipv6, but be consistent + .put("transport.profiles.client.bind_host", "localhost") + .put("transport.profiles.client.xpack.security.filter.deny", "_all") + .put(IPFilter.TRANSPORT_FILTER_DENY_SETTING.getKey(), "_all") + .build(); } public void testThatIpFilteringIsIntegratedIntoNettyPipelineViaHttp() throws Exception { - TransportAddress transportAddress = - randomFrom(internalCluster().getDataNodeInstance(HttpServerTransport.class).boundAddress().boundAddresses()); - try (Socket socket = new Socket()){ + TransportAddress transportAddress = randomFrom( + internalCluster().getDataNodeInstance(HttpServerTransport.class).boundAddress().boundAddresses() + ); + try (Socket socket = new Socket()) { trySocketConnection(socket, transportAddress.address()); assertThat(socket.isClosed(), is(true)); } } public void testThatIpFilteringIsAppliedForProfile() throws Exception { - try (Socket socket = new Socket()){ + try (Socket socket = new Socket()) { trySocketConnection(socket, getProfileAddress("client")); assertThat(socket.isClosed(), is(true)); } @@ -81,8 +83,9 @@ private void trySocketConnection(Socket socket, InetSocketAddress address) throw } private static InetSocketAddress getProfileAddress(String profile) { - TransportAddress transportAddress = - randomFrom(internalCluster().getInstance(Transport.class).profileBoundAddresses().get(profile).boundAddresses()); + TransportAddress transportAddress = randomFrom( + internalCluster().getInstance(Transport.class).profileBoundAddresses().get(profile).boundAddresses() + ); return transportAddress.address(); } } diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/transport/filter/IpFilteringUpdateTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/transport/filter/IpFilteringUpdateTests.java index 9677e14ac11f8..bc206f4557f65 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/transport/filter/IpFilteringUpdateTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/transport/filter/IpFilteringUpdateTests.java @@ -43,12 +43,12 @@ protected boolean addMockHttpTransport() { @Override protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { - String randomClientPortRange = randomClientPort + "-" + (randomClientPort+100); + String randomClientPortRange = randomClientPort + "-" + (randomClientPort + 100); return Settings.builder() - .put(super.nodeSettings(nodeOrdinal, otherSettings)) - .put("xpack.security.transport.filter.deny", "127.0.0.200") - .put("transport.profiles.client.port", randomClientPortRange) - .build(); + .put(super.nodeSettings(nodeOrdinal, otherSettings)) + .put("xpack.security.transport.filter.deny", "127.0.0.200") + .put("transport.profiles.client.port", randomClientPortRange) + .build(); } public void testThatIpFilterConfigurationCanBeChangedDynamically() throws Exception { @@ -61,24 +61,24 @@ public void testThatIpFilterConfigurationCanBeChangedDynamically() throws Except assertConnectionAccepted("client", "127.0.0.8"); Settings settings = Settings.builder() - .put("xpack.security.transport.filter.allow", "127.0.0.1") - .put("xpack.security.transport.filter.deny", "127.0.0.8") - .build(); + .put("xpack.security.transport.filter.allow", "127.0.0.1") + .put("xpack.security.transport.filter.deny", "127.0.0.8") + .build(); updateSettings(settings); assertConnectionRejected("default", "127.0.0.8"); settings = Settings.builder() - .putList("xpack.security.http.filter.allow", "127.0.0.1") - .putList("xpack.security.http.filter.deny", "127.0.0.8") - .build(); + .putList("xpack.security.http.filter.allow", "127.0.0.1") + .putList("xpack.security.http.filter.deny", "127.0.0.8") + .build(); updateSettings(settings); assertConnectionRejected("default", "127.0.0.8"); assertConnectionRejected(".http", "127.0.0.8"); settings = Settings.builder() - .put("transport.profiles.client.xpack.security.filter.allow", "127.0.0.1") - .put("transport.profiles.client.xpack.security.filter.deny", "127.0.0.8") - .build(); + .put("transport.profiles.client.xpack.security.filter.allow", "127.0.0.1") + .put("transport.profiles.client.xpack.security.filter.deny", "127.0.0.8") + .build(); updateSettings(settings); assertConnectionRejected("default", "127.0.0.8"); assertConnectionRejected(".http", "127.0.0.8"); @@ -95,9 +95,9 @@ public void testThatIpFilterConfigurationCanBeChangedDynamically() throws Except // now disable ip filtering dynamically and make sure nothing is rejected settings = Settings.builder() - .put(IPFilter.IP_FILTER_ENABLED_SETTING.getKey(), false) - .put(IPFilter.IP_FILTER_ENABLED_HTTP_SETTING.getKey(), true) - .build(); + .put(IPFilter.IP_FILTER_ENABLED_SETTING.getKey(), false) + .put(IPFilter.IP_FILTER_ENABLED_HTTP_SETTING.getKey(), true) + .build(); updateSettings(settings); assertConnectionAccepted("default", "127.0.0.8"); assertConnectionAccepted("client", "127.0.0.8"); @@ -114,9 +114,7 @@ public void testThatIpFilterConfigurationCanBeChangedDynamically() throws Except // now also disable for HTTP if (httpEnabled) { assertConnectionRejected(".http", "127.0.0.8"); - settings = Settings.builder() - .put(IPFilter.IP_FILTER_ENABLED_HTTP_SETTING.getKey(), false) - .build(); + settings = Settings.builder().put(IPFilter.IP_FILTER_ENABLED_HTTP_SETTING.getKey(), false).build(); assertAcked(client().admin().cluster().prepareUpdateSettings().setPersistentSettings(settings)); assertConnectionAccepted(".http", "127.0.0.8"); } @@ -138,55 +136,43 @@ public void testThatInvalidDynamicIpFilterConfigurationIsRejected() { final String invalidValue = "http://"; for (final String settingPrefix : new String[] { - "xpack.security.transport.filter", - "xpack.security.http.filter", - "transport.profiles.default.xpack.security.filter", - "transport.profiles.anotherprofile.xpack.security.filter" - }) { - for (final String settingSuffix : new String[]{"allow", "deny"}) { + "xpack.security.transport.filter", + "xpack.security.http.filter", + "transport.profiles.default.xpack.security.filter", + "transport.profiles.anotherprofile.xpack.security.filter" }) { + for (final String settingSuffix : new String[] { "allow", "deny" }) { final String settingName = settingPrefix + "." + settingSuffix; final Settings settings = Settings.builder().put(settingName, invalidValue).build(); assertThat( - settingName, - expectThrows( - IllegalArgumentException.class, - settingName, - () -> updateSettings(settings)).getMessage(), - allOf(containsString("invalid IP filter"), containsString(invalidValue))); + settingName, + expectThrows(IllegalArgumentException.class, settingName, () -> updateSettings(settings)).getMessage(), + allOf(containsString("invalid IP filter"), containsString(invalidValue)) + ); } } } // issue #762, occurred because in the above test we use HTTP and transport public void testThatDisablingIpFilterWorksAsExpected() throws Exception { - Settings settings = Settings.builder() - .put("xpack.security.transport.filter.deny", "127.0.0.8") - .build(); + Settings settings = Settings.builder().put("xpack.security.transport.filter.deny", "127.0.0.8").build(); updateSettings(settings); assertConnectionRejected("default", "127.0.0.8"); - settings = Settings.builder() - .put(IPFilter.IP_FILTER_ENABLED_SETTING.getKey(), false) - .build(); + settings = Settings.builder().put(IPFilter.IP_FILTER_ENABLED_SETTING.getKey(), false).build(); updateSettings(settings); assertConnectionAccepted("default", "127.0.0.8"); } public void testThatDisablingIpFilterForProfilesWorksAsExpected() throws Exception { - Settings settings = Settings.builder() - .put("transport.profiles.client.xpack.security.filter.deny", "127.0.0.8") - .build(); + Settings settings = Settings.builder().put("transport.profiles.client.xpack.security.filter.deny", "127.0.0.8").build(); updateSettings(settings); assertConnectionRejected("client", "127.0.0.8"); - settings = Settings.builder() - .put(IPFilter.IP_FILTER_ENABLED_SETTING.getKey(), false) - .build(); + settings = Settings.builder().put(IPFilter.IP_FILTER_ENABLED_SETTING.getKey(), false).build(); updateSettings(settings); assertConnectionAccepted("client", "127.0.0.8"); } - private void updateSettings(Settings settings) { assertAcked(client().admin().cluster().prepareUpdateSettings().setPersistentSettings(settings)); } diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/transport/ssl/EllipticCurveSSLTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/transport/ssl/EllipticCurveSSLTests.java index 14b9bfaf6d353..1707d22124870 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/transport/ssl/EllipticCurveSSLTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/transport/ssl/EllipticCurveSSLTests.java @@ -23,6 +23,7 @@ import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicReference; + import javax.net.ssl.HandshakeCompletedEvent; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLEngine; @@ -38,7 +39,7 @@ import static org.hamcrest.Matchers.is; public class EllipticCurveSSLTests extends SecurityIntegTestCase { - private static String CURVE; + private static String CURVE; @Override protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { @@ -67,19 +68,18 @@ public void testConnection() throws Exception { final X509ExtendedKeyManager x509ExtendedKeyManager = CertParsingUtils.getKeyManagerFromPEM(certPath, keyPath, new char[0]); final X509ExtendedTrustManager trustManager = CertParsingUtils.getTrustManagerFromPEM(List.of(certPath)); SSLContext sslContext = SSLContext.getInstance("TLS"); - sslContext.init(new X509ExtendedKeyManager[]{x509ExtendedKeyManager}, - new TrustManager[]{trustManager}, - new SecureRandom()); + sslContext.init(new X509ExtendedKeyManager[] { x509ExtendedKeyManager }, new TrustManager[] { trustManager }, new SecureRandom()); SSLSocketFactory socketFactory = sslContext.getSocketFactory(); NodesInfoResponse response = client().admin().cluster().prepareNodesInfo().setTransport(true).get(); TransportAddress address = randomFrom(response.getNodes()).getInfo(TransportInfo.class).getAddress().publishAddress(); final CountDownLatch latch = new CountDownLatch(1); try (SSLSocket sslSocket = AccessController.doPrivileged(new PrivilegedExceptionAction() { - @Override - public SSLSocket run() throws Exception { - return (SSLSocket) socketFactory.createSocket(address.address().getAddress(), address.address().getPort()); - }})) { + @Override + public SSLSocket run() throws Exception { + return (SSLSocket) socketFactory.createSocket(address.address().getAddress(), address.address().getPort()); + } + })) { final AtomicReference reference = new AtomicReference<>(); sslSocket.addHandshakeCompletedListener((event) -> { reference.set(event); @@ -94,8 +94,10 @@ public SSLSocket run() throws Exception { Certificate[] peerChain = session.getPeerCertificates(); assertEquals(1, peerChain.length); assertEquals(CertParsingUtils.readX509Certificate(certPath), peerChain[0]); - assertThat(session.getCipherSuite(), - anyOf(containsString("ECDSA"), is("TLS_AES_256_GCM_SHA384"), is("TLS_AES_128_GCM_SHA256"))); + assertThat( + session.getCipherSuite(), + anyOf(containsString("ECDSA"), is("TLS_AES_256_GCM_SHA384"), is("TLS_AES_128_GCM_SHA256")) + ); } } @@ -105,9 +107,12 @@ public static void assumeECDSACiphersSupported() throws Exception { SSLContext sslContext = SSLContext.getInstance("TLSv1.2"); sslContext.init(null, null, null); SSLEngine sslEngine = sslContext.createSSLEngine(); - assumeTrue("ECDSA ciphers must be supported for this test to run. Enabled ciphers: " + - Arrays.toString(sslEngine.getEnabledCipherSuites()) + ", supported ciphers: " + - Arrays.toString(sslEngine.getSupportedCipherSuites()), - Arrays.stream(sslEngine.getEnabledCipherSuites()).anyMatch(s -> s.contains("ECDSA"))); + assumeTrue( + "ECDSA ciphers must be supported for this test to run. Enabled ciphers: " + + Arrays.toString(sslEngine.getEnabledCipherSuites()) + + ", supported ciphers: " + + Arrays.toString(sslEngine.getSupportedCipherSuites()), + Arrays.stream(sslEngine.getEnabledCipherSuites()).anyMatch(s -> s.contains("ECDSA")) + ); } } diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/transport/ssl/SslIntegrationTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/transport/ssl/SslIntegrationTests.java index b4b7d175c1f8e..3901f7029be4e 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/transport/ssl/SslIntegrationTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/transport/ssl/SslIntegrationTests.java @@ -27,9 +27,6 @@ import org.elasticsearch.xpack.core.common.socket.SocketAccess; import org.elasticsearch.xpack.core.ssl.SSLService; -import javax.net.ssl.SSLContext; -import javax.net.ssl.SSLHandshakeException; -import javax.net.ssl.TrustManagerFactory; import java.io.InputStreamReader; import java.net.InetSocketAddress; import java.nio.charset.StandardCharsets; @@ -38,6 +35,10 @@ import java.util.Arrays; import java.util.Locale; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLHandshakeException; +import javax.net.ssl.TrustManagerFactory; + import static org.elasticsearch.test.SecuritySettingsSource.addSSLSettingsForNodePEMFiles; import static org.elasticsearch.test.SecuritySettingsSource.addSSLSettingsForPEMFiles; import static org.hamcrest.CoreMatchers.is; @@ -65,22 +66,33 @@ protected boolean transportSSLEnabled() { public void testThatConnectionToHTTPWorks() throws Exception { Settings.Builder builder = Settings.builder().put("xpack.security.http.ssl.enabled", true); addSSLSettingsForPEMFiles( - builder, "/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testclient.pem", + builder, + "/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testclient.pem", "testclient", "/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testclient.crt", "xpack.security.http.", - Arrays.asList("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt")); + Arrays.asList("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt") + ); SSLService service = new SSLService(TestEnvironment.newEnvironment(buildEnvSettings(builder.build()))); CredentialsProvider provider = new BasicCredentialsProvider(); - provider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(nodeClientUsername(), - new String(nodeClientPassword().getChars()))); + provider.setCredentials( + AuthScope.ANY, + new UsernamePasswordCredentials(nodeClientUsername(), new String(nodeClientPassword().getChars())) + ); SslConfiguration sslConfiguration = service.getSSLConfiguration("xpack.security.http.ssl"); - try (CloseableHttpClient client = HttpClients.custom() - .setSSLSocketFactory(new SSLConnectionSocketFactory(service.sslSocketFactory(sslConfiguration), - SSLConnectionSocketFactory.getDefaultHostnameVerifier())) - .setDefaultCredentialsProvider(provider).build(); - CloseableHttpResponse response = SocketAccess.doPrivileged(() -> client.execute(new HttpGet(getNodeUrl())))) { + try ( + CloseableHttpClient client = HttpClients.custom() + .setSSLSocketFactory( + new SSLConnectionSocketFactory( + service.sslSocketFactory(sslConfiguration), + SSLConnectionSocketFactory.getDefaultHostnameVerifier() + ) + ) + .setDefaultCredentialsProvider(provider) + .build(); + CloseableHttpResponse response = SocketAccess.doPrivileged(() -> client.execute(new HttpGet(getNodeUrl()))) + ) { assertThat(response.getStatusLine().getStatusCode(), is(200)); String data = Streams.copyToString(new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8)); assertThat(data, containsString("You Know, for Search")); @@ -94,16 +106,21 @@ public void testThatHttpUsingSSLv3IsRejected() throws Exception { factory.init((KeyStore) null); sslContext.init(null, factory.getTrustManagers(), new SecureRandom()); - SSLConnectionSocketFactory sf = new SSLConnectionSocketFactory(sslContext, new String[]{ "SSLv3" }, null, - NoopHostnameVerifier.INSTANCE); + SSLConnectionSocketFactory sf = new SSLConnectionSocketFactory( + sslContext, + new String[] { "SSLv3" }, + null, + NoopHostnameVerifier.INSTANCE + ); try (CloseableHttpClient client = HttpClients.custom().setSSLSocketFactory(sf).build()) { expectThrows(SSLHandshakeException.class, () -> SocketAccess.doPrivileged(() -> client.execute(new HttpGet(getNodeUrl())))); } } private String getNodeUrl() { - TransportAddress transportAddress = - randomFrom(internalCluster().getInstance(HttpServerTransport.class).boundAddress().boundAddresses()); + TransportAddress transportAddress = randomFrom( + internalCluster().getInstance(HttpServerTransport.class).boundAddress().boundAddresses() + ); final InetSocketAddress inetSocketAddress = transportAddress.address(); return String.format(Locale.ROOT, "https://%s/", NetworkAddress.format(inetSocketAddress)); } diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/user/AnonymousUserIntegTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/user/AnonymousUserIntegTests.java index 35c3fb72837a9..92dd9a5acbcd9 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/user/AnonymousUserIntegTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/user/AnonymousUserIntegTests.java @@ -31,26 +31,22 @@ protected boolean addMockHttpTransport() { @Override public Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { return Settings.builder() - .put(super.nodeSettings(nodeOrdinal, otherSettings)) - .put(AnonymousUser.ROLES_SETTING.getKey(), "anonymous") - .put(AuthorizationService.ANONYMOUS_AUTHORIZATION_EXCEPTION_SETTING.getKey(), authorizationExceptionsEnabled) - .build(); + .put(super.nodeSettings(nodeOrdinal, otherSettings)) + .put(AnonymousUser.ROLES_SETTING.getKey(), "anonymous") + .put(AuthorizationService.ANONYMOUS_AUTHORIZATION_EXCEPTION_SETTING.getKey(), authorizationExceptionsEnabled) + .build(); } @Override public String configRoles() { - return super.configRoles() + "\n" + - "anonymous:\n" + - " indices:\n" + - " - names: '*'\n" + - " privileges: [ READ ]\n"; + return super.configRoles() + "\n" + "anonymous:\n" + " indices:\n" + " - names: '*'\n" + " privileges: [ READ ]\n"; } public void testAnonymousViaHttp() throws Exception { try { getRestClient().performRequest(new Request("GET", "/_nodes")); fail("request should have failed"); - } catch(ResponseException e) { + } catch (ResponseException e) { int statusCode = e.getResponse().getStatusLine().getStatusCode(); Response response = e.getResponse(); if (authorizationExceptionsEnabled) { diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/ssl/SSLReloadIntegTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/ssl/SSLReloadIntegTests.java index cc7c008d4bf13..cb4dde4e1cf0b 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/ssl/SSLReloadIntegTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/ssl/SSLReloadIntegTests.java @@ -16,9 +16,6 @@ import org.elasticsearch.transport.Transport; import org.elasticsearch.xpack.core.ssl.SSLService; -import javax.net.ssl.SSLException; -import javax.net.ssl.SSLSocket; -import javax.net.ssl.SSLSocketFactory; import java.io.IOException; import java.net.SocketException; import java.nio.file.AtomicMoveNotSupportedException; @@ -28,6 +25,10 @@ import java.util.Arrays; import java.util.concurrent.CountDownLatch; +import javax.net.ssl.SSLException; +import javax.net.ssl.SSLSocket; +import javax.net.ssl.SSLSocketFactory; + import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.is; @@ -71,14 +72,15 @@ public Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { } Settings settings = super.nodeSettings(nodeOrdinal, otherSettings); - Settings.Builder builder = Settings.builder() - .put(settings.filter((s) -> s.startsWith("xpack.security.transport.ssl.") == false)); + Settings.Builder builder = Settings.builder().put(settings.filter((s) -> s.startsWith("xpack.security.transport.ssl.") == false)); builder.put("path.home", createTempDir()) .put("xpack.security.transport.ssl.key", nodeKeyPath) .put("xpack.security.transport.ssl.key_passphrase", "testnode") .put("xpack.security.transport.ssl.certificate", nodeCertPath) - .putList("xpack.security.transport.ssl.certificate_authorities", - Arrays.asList(nodeCertPath.toString(), clientCertPath.toString(), updateableCertPath.toString())) + .putList( + "xpack.security.transport.ssl.certificate_authorities", + Arrays.asList(nodeCertPath.toString(), clientCertPath.toString(), updateableCertPath.toString()) + ) .put("resource.reload.interval.high", "1s"); builder.put("xpack.security.transport.ssl.enabled", true); @@ -103,16 +105,17 @@ public void testThatSSLConfigurationReloadsOnModification() throws Exception { .put("xpack.security.transport.ssl.enabled", true) .put("xpack.security.transport.ssl.key", keyPath) .put("xpack.security.transport.ssl.certificate", certPath) - .putList("xpack.security.transport.ssl.certificate_authorities", - Arrays.asList(nodeCertPath.toString(), clientCertPath.toString(), updateableCertPath.toString())) + .putList( + "xpack.security.transport.ssl.certificate_authorities", + Arrays.asList(nodeCertPath.toString(), clientCertPath.toString(), updateableCertPath.toString()) + ) .setSecureSettings(secureSettings) .build(); String node = randomFrom(internalCluster().getNodeNames()); SSLService sslService = new SSLService(TestEnvironment.newEnvironment(settings)); SslConfiguration sslConfiguration = sslService.getSSLConfiguration("xpack.security.transport.ssl"); SSLSocketFactory sslSocketFactory = sslService.sslSocketFactory(sslConfiguration); - TransportAddress address = internalCluster() - .getInstance(Transport.class, node).boundAddress().publishAddress(); + TransportAddress address = internalCluster().getInstance(Transport.class, node).boundAddress().publishAddress(); // Fails as our nodes do not trust testnode_updated.crt try (SSLSocket socket = (SSLSocket) sslSocketFactory.createSocket(address.getAddress(), address.getPort())) { assertThat(socket.isConnected(), is(true)); diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/ssl/SSLTrustRestrictionsTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/ssl/SSLTrustRestrictionsTests.java index ac403fa5d81dc..b4c0796d2d584 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/ssl/SSLTrustRestrictionsTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/ssl/SSLTrustRestrictionsTests.java @@ -36,6 +36,7 @@ import java.security.cert.X509Certificate; import java.util.Collections; import java.util.concurrent.TimeUnit; + import javax.net.ssl.SSLException; import javax.net.ssl.SSLSocket; import javax.net.ssl.SSLSocketFactory; @@ -74,37 +75,47 @@ protected int maxNumberOfNodes() { public static void setupCertificates() throws Exception { assumeFalse("Can't run in a FIPS JVM, custom TrustManager implementations cannot be used.", inFipsJvm()); configPath = createTempDir(); - Path caCertPath = PathUtils.get(SSLTrustRestrictionsTests.class.getResource - ("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/nodes/ca.crt").toURI()); + Path caCertPath = PathUtils.get( + SSLTrustRestrictionsTests.class.getResource("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/nodes/ca.crt").toURI() + ); X509Certificate caCert = CertParsingUtils.readX509Certificates(Collections.singletonList(caCertPath))[0]; - Path caKeyPath = PathUtils.get(SSLTrustRestrictionsTests.class.getResource - ("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/nodes/ca.key").toURI()); + Path caKeyPath = PathUtils.get( + SSLTrustRestrictionsTests.class.getResource("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/nodes/ca.key").toURI() + ); PrivateKey caKey = PemUtils.readPrivateKey(caKeyPath, ""::toCharArray); ca = new CertificateInfo(caKey, caKeyPath, caCert, caCertPath); - Path trustedCertPath = PathUtils.get(SSLTrustRestrictionsTests.class.getResource - ("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/nodes/trusted.crt").toURI()); + Path trustedCertPath = PathUtils.get( + SSLTrustRestrictionsTests.class.getResource("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/nodes/trusted.crt") + .toURI() + ); X509Certificate trustedX509Certificate = CertParsingUtils.readX509Certificates(Collections.singletonList(trustedCertPath))[0]; - Path trustedKeyPath = PathUtils.get(SSLTrustRestrictionsTests.class.getResource - ("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/nodes/trusted.key").toURI()); + Path trustedKeyPath = PathUtils.get( + SSLTrustRestrictionsTests.class.getResource("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/nodes/trusted.key") + .toURI() + ); PrivateKey trustedKey = PemUtils.readPrivateKey(trustedKeyPath, ""::toCharArray); trustedCert = new CertificateInfo(trustedKey, trustedKeyPath, trustedX509Certificate, trustedCertPath); - Path untrustedCertPath = PathUtils.get(SSLTrustRestrictionsTests.class.getResource - ("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/nodes/untrusted.crt").toURI()); + Path untrustedCertPath = PathUtils.get( + SSLTrustRestrictionsTests.class.getResource("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/nodes/untrusted.crt") + .toURI() + ); X509Certificate untrustedX509Certificate = CertParsingUtils.readX509Certificates(Collections.singletonList(untrustedCertPath))[0]; - Path untrustedKeyPath = PathUtils.get(SSLTrustRestrictionsTests.class.getResource - ("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/nodes/untrusted.key").toURI()); + Path untrustedKeyPath = PathUtils.get( + SSLTrustRestrictionsTests.class.getResource("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/nodes/untrusted.key") + .toURI() + ); PrivateKey untrustedKey = PemUtils.readPrivateKey(untrustedKeyPath, ""::toCharArray); untrustedCert = new CertificateInfo(untrustedKey, untrustedKeyPath, untrustedX509Certificate, untrustedCertPath); nodeSSL = Settings.builder() - .put("xpack.security.transport.ssl.enabled", true) - .put("xpack.security.transport.ssl.verification_mode", "certificate") - .putList("xpack.security.transport.ssl.certificate_authorities", ca.getCertPath().toString()) - .put("xpack.security.transport.ssl.key", trustedCert.getKeyPath()) - .put("xpack.security.transport.ssl.certificate", trustedCert.getCertPath()) - .build(); + .put("xpack.security.transport.ssl.enabled", true) + .put("xpack.security.transport.ssl.verification_mode", "certificate") + .putList("xpack.security.transport.ssl.certificate_authorities", ca.getCertPath().toString()) + .put("xpack.security.transport.ssl.key", trustedCert.getKeyPath()) + .put("xpack.security.transport.ssl.certificate", trustedCert.getCertPath()) + .build(); } @AfterClass @@ -121,8 +132,8 @@ public Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { Settings parentSettings = super.nodeSettings(nodeOrdinal, otherSettings); Settings.Builder builder = Settings.builder() - .put(parentSettings.filter((s) -> s.startsWith("xpack.security.transport.ssl.") == false)) - .put(nodeSSL); + .put(parentSettings.filter((s) -> s.startsWith("xpack.security.transport.ssl.") == false)) + .put(nodeSSL); restrictionsPath = configPath.resolve("trust_restrictions.yml"); restrictionsTmpPath = configPath.resolve("trust_restrictions.tmp"); @@ -157,8 +168,14 @@ public void testCertificateWithTrustedNameIsAccepted() throws Exception { try { tryConnect(trustedCert, false); } catch (SSLException | SocketException ex) { - logger.warn(new ParameterizedMessage("unexpected handshake failure with certificate [{}] [{}]", - trustedCert.certificate.getSubjectDN(), trustedCert.certificate.getSubjectAlternativeNames()), ex); + logger.warn( + new ParameterizedMessage( + "unexpected handshake failure with certificate [{}] [{}]", + trustedCert.certificate.getSubjectDN(), + trustedCert.certificate.getSubjectAlternativeNames() + ), + ex + ); fail("handshake should have been successful, but failed with " + ex); } } @@ -213,13 +230,13 @@ private void runResourceWatcher() { private void tryConnect(CertificateInfo certificate, boolean shouldFail) throws Exception { Settings settings = Settings.builder() - .put("path.home", createTempDir()) - .put("xpack.security.transport.ssl.enabled", true) - .put("xpack.security.transport.ssl.key", certificate.getKeyPath()) - .put("xpack.security.transport.ssl.certificate", certificate.getCertPath()) - .putList("xpack.security.transport.ssl.certificate_authorities", ca.getCertPath().toString()) - .put("xpack.security.transport.ssl.verification_mode", "certificate") - .build(); + .put("path.home", createTempDir()) + .put("xpack.security.transport.ssl.enabled", true) + .put("xpack.security.transport.ssl.key", certificate.getKeyPath()) + .put("xpack.security.transport.ssl.certificate", certificate.getCertPath()) + .putList("xpack.security.transport.ssl.certificate_authorities", ca.getCertPath().toString()) + .put("xpack.security.transport.ssl.verification_mode", "certificate") + .build(); String node = randomFrom(internalCluster().getNodeNames()); SSLService sslService = new SSLService(TestEnvironment.newEnvironment(settings)); diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/ssl/SslClientAuthenticationTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/ssl/SslClientAuthenticationTests.java index 6d1868bc7ce60..88e413b05aaff 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/ssl/SslClientAuthenticationTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/ssl/SslClientAuthenticationTests.java @@ -36,6 +36,7 @@ import java.security.cert.CertPathBuilderException; import java.util.HashSet; import java.util.List; + import javax.net.ssl.KeyManager; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; @@ -57,15 +58,13 @@ protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { Settings baseSettings = super.nodeSettings(nodeOrdinal, otherSettings); Settings.Builder builder = Settings.builder().put(baseSettings); - baseSettings.getByPrefix("xpack.security.transport.ssl.") - .keySet() - .forEach(k -> { - String httpKey = "xpack.security.http.ssl." + k; - String value = baseSettings.get("xpack.security.transport.ssl." + k); - if (value != null) { - builder.put(httpKey, baseSettings.get("xpack.security.transport.ssl." + k)); - } - }); + baseSettings.getByPrefix("xpack.security.transport.ssl.").keySet().forEach(k -> { + String httpKey = "xpack.security.http.ssl." + k; + String value = baseSettings.get("xpack.security.transport.ssl." + k); + if (value != null) { + builder.put(httpKey, baseSettings.get("xpack.security.transport.ssl." + k)); + } + }); MockSecureSettings secureSettings = (MockSecureSettings) builder.getSecureSettings(); for (String key : new HashSet<>(secureSettings.getSettingNames())) { @@ -85,15 +84,15 @@ protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { } return builder - // invert the require auth settings - .put("xpack.security.transport.ssl.client_authentication", SslClientAuthenticationMode.NONE) - // Due to the TLSv1.3 bug with session resumption when client authentication is not - // used, we need to set the protocols since we disabled client auth for transport - // to avoid failures on pre 11.0.3 JDKs. See #getProtocols - .putList("xpack.security.transport.ssl.supported_protocols", getProtocols()) - .put("xpack.security.http.ssl.enabled", true) - .put("xpack.security.http.ssl.client_authentication", SslClientAuthenticationMode.REQUIRED) - .build(); + // invert the require auth settings + .put("xpack.security.transport.ssl.client_authentication", SslClientAuthenticationMode.NONE) + // Due to the TLSv1.3 bug with session resumption when client authentication is not + // used, we need to set the protocols since we disabled client auth for transport + // to avoid failures on pre 11.0.3 JDKs. See #getProtocols + .putList("xpack.security.transport.ssl.supported_protocols", getProtocols()) + .put("xpack.security.http.ssl.enabled", true) + .put("xpack.security.http.ssl.client_authentication", SslClientAuthenticationMode.REQUIRED) + .build(); } @Override @@ -169,9 +168,9 @@ private byte[] toByteArray(InputStream is) throws IOException { * However if client authentication is turned off and TLSv1.3 is used on the affected JVMs then we will hit this issue. */ private static List getProtocols() { - JavaVersion full = - AccessController.doPrivileged( - (PrivilegedAction) () -> JavaVersion.parse(System.getProperty("java.version"))); + JavaVersion full = AccessController.doPrivileged( + (PrivilegedAction) () -> JavaVersion.parse(System.getProperty("java.version")) + ); if (full.compareTo(JavaVersion.parse("11.0.3")) < 0) { return List.of("TLSv1.2"); } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/core/security/transport/ProfileConfigurations.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/core/security/transport/ProfileConfigurations.java index ebecbdb8055ce..90065ddc90ea1 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/core/security/transport/ProfileConfigurations.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/core/security/transport/ProfileConfigurations.java @@ -31,8 +31,10 @@ public static Map get(Settings settings, SSLService ss if (settings.getByPrefix("transport.profiles.default.xpack.security.ssl.").isEmpty()) { continue; } else { - throw new IllegalArgumentException("SSL settings should not be configured for the default profile. " + - "Use the [xpack.security.transport.ssl] settings instead."); + throw new IllegalArgumentException( + "SSL settings should not be configured for the default profile. " + + "Use the [xpack.security.transport.ssl] settings instead." + ); } } SslConfiguration configuration = sslService.getSSLConfiguration("transport.profiles." + profileName + "." + setting("ssl")); diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/core/security/transport/SSLExceptionHelper.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/core/security/transport/SSLExceptionHelper.java index 3ec54ae5e9c7e..9bc63be18e041 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/core/security/transport/SSLExceptionHelper.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/core/security/transport/SSLExceptionHelper.java @@ -6,9 +6,9 @@ */ package org.elasticsearch.xpack.core.security.transport; - import io.netty.handler.codec.DecoderException; import io.netty.handler.ssl.NotSslRecordException; + import org.elasticsearch.common.regex.Regex; import javax.net.ssl.SSLException; @@ -16,34 +16,29 @@ public class SSLExceptionHelper { - private SSLExceptionHelper() { - } + private SSLExceptionHelper() {} public static boolean isNotSslRecordException(Throwable e) { - return e instanceof DecoderException && - e.getCause() instanceof NotSslRecordException; + return e instanceof DecoderException && e.getCause() instanceof NotSslRecordException; } public static boolean isCloseDuringHandshakeException(Throwable e) { - return isCloseDuringHandshakeSSLException(e) - || isCloseDuringHandshakeSSLException(e.getCause()); + return isCloseDuringHandshakeSSLException(e) || isCloseDuringHandshakeSSLException(e.getCause()); } private static boolean isCloseDuringHandshakeSSLException(Throwable e) { - return e instanceof SSLException - && e.getCause() == null - && "Received close_notify during handshake".equals(e.getMessage()); + return e instanceof SSLException && e.getCause() == null && "Received close_notify during handshake".equals(e.getMessage()); } public static boolean isReceivedCertificateUnknownException(Throwable e) { return e instanceof DecoderException - && e.getCause() instanceof SSLException - && "Received fatal alert: certificate_unknown".equals(e.getCause().getMessage()); + && e.getCause() instanceof SSLException + && "Received fatal alert: certificate_unknown".equals(e.getCause().getMessage()); } public static boolean isInsufficientBufferRemainingException(Throwable e) { return e instanceof DecoderException - && e.getCause() instanceof SSLHandshakeException - && Regex.simpleMatch("Insufficient buffer remaining for AEAD cipher fragment*", e.getCause().getMessage()); + && e.getCause() instanceof SSLHandshakeException + && Regex.simpleMatch("Insufficient buffer remaining for AEAD cipher fragment*", e.getCause().getMessage()); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/core/security/transport/SecurityTransportExceptionHandler.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/core/security/transport/SecurityTransportExceptionHandler.java index 8f91a1f976ffe..051801479e3f7 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/core/security/transport/SecurityTransportExceptionHandler.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/core/security/transport/SecurityTransportExceptionHandler.java @@ -10,7 +10,6 @@ import org.elasticsearch.common.component.Lifecycle; import org.elasticsearch.common.network.CloseableChannel; import org.elasticsearch.transport.TcpChannel; -import org.elasticsearch.xpack.core.security.transport.SSLExceptionHelper; import java.util.function.BiConsumer; diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/core/security/transport/netty4/SecurityNetty4Transport.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/core/security/transport/netty4/SecurityNetty4Transport.java index 158919ca74c32..02533a97e6134 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/core/security/transport/netty4/SecurityNetty4Transport.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/core/security/transport/netty4/SecurityNetty4Transport.java @@ -12,6 +12,7 @@ import io.netty.channel.ChannelOutboundHandlerAdapter; import io.netty.channel.ChannelPromise; import io.netty.handler.ssl.SslHandler; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.Version; @@ -24,23 +25,24 @@ import org.elasticsearch.indices.breaker.CircuitBreakerService; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.ConnectTransportException; -import org.elasticsearch.transport.netty4.SharedGroupFactory; import org.elasticsearch.transport.TcpChannel; import org.elasticsearch.transport.netty4.Netty4Transport; +import org.elasticsearch.transport.netty4.SharedGroupFactory; import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.xpack.core.security.transport.ProfileConfigurations; import org.elasticsearch.xpack.core.security.transport.SecurityTransportExceptionHandler; import org.elasticsearch.xpack.core.ssl.SSLService; -import javax.net.ssl.SNIHostName; -import javax.net.ssl.SNIServerName; -import javax.net.ssl.SSLEngine; -import javax.net.ssl.SSLParameters; import java.net.InetSocketAddress; import java.net.SocketAddress; import java.util.Collections; import java.util.Map; +import javax.net.ssl.SNIHostName; +import javax.net.ssl.SNIServerName; +import javax.net.ssl.SSLEngine; +import javax.net.ssl.SSLParameters; + import static org.elasticsearch.xpack.core.security.SecurityField.setting; /** @@ -56,17 +58,26 @@ public class SecurityNetty4Transport extends Netty4Transport { private final boolean sslEnabled; public SecurityNetty4Transport( - final Settings settings, - final Version version, - final ThreadPool threadPool, - final NetworkService networkService, - final PageCacheRecycler pageCacheRecycler, - final NamedWriteableRegistry namedWriteableRegistry, - final CircuitBreakerService circuitBreakerService, - final SSLService sslService, - final SharedGroupFactory sharedGroupFactory) { - super(settings, version, threadPool, networkService, pageCacheRecycler, namedWriteableRegistry, circuitBreakerService, - sharedGroupFactory); + final Settings settings, + final Version version, + final ThreadPool threadPool, + final NetworkService networkService, + final PageCacheRecycler pageCacheRecycler, + final NamedWriteableRegistry namedWriteableRegistry, + final CircuitBreakerService circuitBreakerService, + final SSLService sslService, + final SharedGroupFactory sharedGroupFactory + ) { + super( + settings, + version, + threadPool, + networkService, + pageCacheRecycler, + namedWriteableRegistry, + circuitBreakerService, + sharedGroupFactory + ); this.exceptionHandler = new SecurityTransportExceptionHandler(logger, lifecycle, (c, e) -> super.onException(c, e)); this.sslService = sslService; this.sslEnabled = XPackSettings.TRANSPORT_SSL_ENABLED.get(settings); @@ -163,8 +174,8 @@ private class SecurityClientChannelInitializer extends ClientChannelInitializer protected void initChannel(Channel ch) throws Exception { super.initChannel(ch); if (sslEnabled) { - ch.pipeline().addFirst(new ClientSslHandlerInitializer(sslConfiguration, sslService, hostnameVerificationEnabled, - serverName)); + ch.pipeline() + .addFirst(new ClientSslHandlerInitializer(sslConfiguration, sslService, hostnameVerificationEnabled, serverName)); } } } @@ -176,8 +187,12 @@ private static class ClientSslHandlerInitializer extends ChannelOutboundHandlerA private final SSLService sslService; private final SNIServerName serverName; - private ClientSslHandlerInitializer(SslConfiguration sslConfiguration, SSLService sslService, boolean hostnameVerificationEnabled, - SNIServerName serverName) { + private ClientSslHandlerInitializer( + SslConfiguration sslConfiguration, + SSLService sslService, + boolean hostnameVerificationEnabled, + SNIServerName serverName + ) { this.sslConfiguration = sslConfiguration; this.hostnameVerificationEnabled = hostnameVerificationEnabled; this.sslService = sslService; @@ -185,14 +200,13 @@ private ClientSslHandlerInitializer(SslConfiguration sslConfiguration, SSLServic } @Override - public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, - SocketAddress localAddress, ChannelPromise promise) throws Exception { + public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) + throws Exception { final SSLEngine sslEngine; if (hostnameVerificationEnabled) { InetSocketAddress inetSocketAddress = (InetSocketAddress) remoteAddress; // we create the socket based on the name given. don't reverse DNS - sslEngine = sslService.createSSLEngine(sslConfiguration, inetSocketAddress.getHostString(), - inetSocketAddress.getPort()); + sslEngine = sslService.createSSLEngine(sslConfiguration, inetSocketAddress.getHostString(), inetSocketAddress.getPort()); } else { sslEngine = sslService.createSSLEngine(sslConfiguration, null, -1); } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/InitialNodeSecurityAutoConfiguration.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/InitialNodeSecurityAutoConfiguration.java index 33a4d31215800..6765b739ce670 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/InitialNodeSecurityAutoConfiguration.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/InitialNodeSecurityAutoConfiguration.java @@ -68,9 +68,11 @@ public static void maybeGenerateEnrollmentTokensAndElasticCredentialsOnNodeStart // Check if it has been closed, try to write something so that we trigger PrintStream#ensureOpen out.println(); if (out.checkError()) { - LOGGER.info("Auto-configuration will not generate a password for the elastic built-in superuser, as we cannot " + - " determine if there is a terminal attached to the elasticsearch process. You can use the" + - " `bin/elasticsearch-reset-password` tool to set the password for the elastic user."); + LOGGER.info( + "Auto-configuration will not generate a password for the elastic built-in superuser, as we cannot " + + " determine if there is a terminal attached to the elasticsearch process. You can use the" + + " `bin/elasticsearch-reset-password` tool to set the password for the elastic user." + ); return; } // if enrollment is enabled, we assume (and document this assumption) that the node is auto-configured in a specific way @@ -86,15 +88,19 @@ public static void maybeGenerateEnrollmentTokensAndElasticCredentialsOnNodeStart String fingerprint; try { fingerprint = enrollmentTokenGenerator.getHttpsCaFingerprint(); - LOGGER.info("HTTPS has been configured with automatically generated certificates, " + - "and the CA's hex-encoded SHA-256 fingerprint is [" + fingerprint + "]"); + LOGGER.info( + "HTTPS has been configured with automatically generated certificates, " + + "and the CA's hex-encoded SHA-256 fingerprint is [" + + fingerprint + + "]" + ); } catch (Exception e) { fingerprint = null; LOGGER.error("Failed to compute the HTTPS CA fingerprint, probably the certs are not auto-generated", e); } final String httpsCaFingerprint = fingerprint; - GroupedActionListener> groupedActionListener = - new GroupedActionListener<>(ActionListener.wrap(results -> { + GroupedActionListener> groupedActionListener = new GroupedActionListener<>( + ActionListener.wrap(results -> { final Map allResultsMap = new HashMap<>(); for (Map result : results) { allResultsMap.putAll(result); @@ -103,20 +109,19 @@ public static void maybeGenerateEnrollmentTokensAndElasticCredentialsOnNodeStart final String kibanaEnrollmentToken = allResultsMap.get("kibana_enrollment_token"); final String nodeEnrollmentToken = allResultsMap.get("node_enrollment_token"); outputInformationToConsole(elasticPassword, kibanaEnrollmentToken, nodeEnrollmentToken, httpsCaFingerprint, out); - }, e -> { - LOGGER.error("Unexpected exception during security auto-configuration", e); - }), 3); + }, e -> { LOGGER.error("Unexpected exception during security auto-configuration", e); }), + 3 + ); // we only generate the elastic user password if the node has been auto-configured in a specific way, such that the first // time a node starts it will form a cluster by itself and can hold the .security index (which we assume it is when // {@code ENROLLMENT_ENABLED} is true), that the node process's output is a terminal and that the password is not // specified already via the two secure settings - if (false == BOOTSTRAP_ELASTIC_PASSWORD.exists(environment.settings()) && - false == AUTOCONFIG_ELASTIC_PASSWORD_HASH.exists(environment.settings())) { + if (false == BOOTSTRAP_ELASTIC_PASSWORD.exists(environment.settings()) + && false == AUTOCONFIG_ELASTIC_PASSWORD_HASH.exists(environment.settings())) { final char[] elasticPassword = generatePassword(20); nativeUsersStore.createElasticUser(elasticPassword, ActionListener.wrap(aVoid -> { LOGGER.debug("elastic credentials generated successfully"); - groupedActionListener.onResponse(Map.of( - "generated_elastic_user_password", new String(elasticPassword))); + groupedActionListener.onResponse(Map.of("generated_elastic_user_password", new String(elasticPassword))); }, e -> { LOGGER.error("Failed to generate credentials for the elastic built-in superuser", e); // null password in case of error @@ -124,9 +129,12 @@ public static void maybeGenerateEnrollmentTokensAndElasticCredentialsOnNodeStart })); } else { if (false == BOOTSTRAP_ELASTIC_PASSWORD.exists(environment.settings())) { - LOGGER.info("Auto-configuration will not generate a password for the elastic built-in superuser, " + - "you should use the password specified in the node's secure setting [" + BOOTSTRAP_ELASTIC_PASSWORD.getKey() + - "] in order to authenticate as elastic"); + LOGGER.info( + "Auto-configuration will not generate a password for the elastic built-in superuser, " + + "you should use the password specified in the node's secure setting [" + + BOOTSTRAP_ELASTIC_PASSWORD.getKey() + + "] in order to authenticate as elastic" + ); } // empty password in case password generation is skyped groupedActionListener.onResponse(Map.of("generated_elastic_user_password", "")); @@ -155,8 +163,13 @@ public static void maybeGenerateEnrollmentTokensAndElasticCredentialsOnNodeStart }); } - private static void outputInformationToConsole(String elasticPassword, String kibanaEnrollmentToken, - String nodeEnrollmentToken, String caCertFingerprint, PrintStream out) { + private static void outputInformationToConsole( + String elasticPassword, + String kibanaEnrollmentToken, + String nodeEnrollmentToken, + String caCertFingerprint, + PrintStream out + ) { StringBuilder builder = new StringBuilder(); builder.append(System.lineSeparator()); builder.append(System.lineSeparator()); diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/PkiRealmBootstrapCheck.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/PkiRealmBootstrapCheck.java index 41048eece6c6b..443b15caa8a06 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/PkiRealmBootstrapCheck.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/PkiRealmBootstrapCheck.java @@ -39,10 +39,11 @@ class PkiRealmBootstrapCheck implements BootstrapCheck { public BootstrapCheckResult check(BootstrapContext context) { final Settings settings = context.settings(); final Map realms = RealmSettings.getRealmSettings(settings); - final boolean pkiRealmEnabledWithoutDelegation = realms.entrySet().stream() - .filter(e -> PkiRealmSettings.TYPE.equals(e.getKey().getType())) - .map(Map.Entry::getValue) - .anyMatch(s -> s.getAsBoolean("enabled", true) && (false == s.getAsBoolean("delegation.enabled", false))); + final boolean pkiRealmEnabledWithoutDelegation = realms.entrySet() + .stream() + .filter(e -> PkiRealmSettings.TYPE.equals(e.getKey().getType())) + .map(Map.Entry::getValue) + .anyMatch(s -> s.getAsBoolean("enabled", true) && (false == s.getAsBoolean("delegation.enabled", false))); if (pkiRealmEnabledWithoutDelegation) { for (String contextName : getSslContextNames(settings)) { final SslConfiguration configuration = sslService.getSSLConfiguration(contextName); @@ -51,7 +52,8 @@ public BootstrapCheckResult check(BootstrapContext context) { } } return BootstrapCheckResult.failure( - "a PKI realm is enabled but cannot be used as neither HTTP or Transport have SSL and client authentication enabled"); + "a PKI realm is enabled but cannot be used as neither HTTP or Transport have SSL and client authentication enabled" + ); } else { return BootstrapCheckResult.success(); } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/Security.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/Security.java index b8346464972cd..f125fd186592e 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/Security.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/Security.java @@ -44,8 +44,6 @@ import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.common.util.set.Sets; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.env.Environment; import org.elasticsearch.env.NodeEnvironment; import org.elasticsearch.env.NodeMetadata; @@ -84,6 +82,8 @@ import org.elasticsearch.transport.netty4.SharedGroupFactory; import org.elasticsearch.transport.nio.NioGroupFactory; import org.elasticsearch.watcher.ResourceWatcherService; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.XPackField; import org.elasticsearch.xpack.core.XPackPlugin; import org.elasticsearch.xpack.core.XPackSettings; @@ -348,50 +348,100 @@ import static org.elasticsearch.xpack.security.support.SecurityIndexManager.INTERNAL_TOKENS_INDEX_FORMAT; import static org.elasticsearch.xpack.security.support.SecurityIndexManager.SECURITY_VERSION_STRING; -public class Security extends Plugin implements SystemIndexPlugin, IngestPlugin, NetworkPlugin, ClusterPlugin, - DiscoveryPlugin, MapperPlugin, ExtensiblePlugin, SearchPlugin { +public class Security extends Plugin + implements + SystemIndexPlugin, + IngestPlugin, + NetworkPlugin, + ClusterPlugin, + DiscoveryPlugin, + MapperPlugin, + ExtensiblePlugin, + SearchPlugin { public static final String SECURITY_CRYPTO_THREAD_POOL_NAME = XPackField.SECURITY + "-crypto"; // TODO: ip filtering does not actually track license usage yet - public static final LicensedFeature.Momentary IP_FILTERING_FEATURE = - LicensedFeature.momentaryLenient(null, "security-ip-filtering", License.OperationMode.GOLD); - public static final LicensedFeature.Momentary AUDITING_FEATURE = - LicensedFeature.momentary(null, "security-auditing", License.OperationMode.GOLD); - public static final LicensedFeature.Momentary TOKEN_SERVICE_FEATURE = - LicensedFeature.momentary(null, "security-token-service", License.OperationMode.STANDARD); + public static final LicensedFeature.Momentary IP_FILTERING_FEATURE = LicensedFeature.momentaryLenient( + null, + "security-ip-filtering", + License.OperationMode.GOLD + ); + public static final LicensedFeature.Momentary AUDITING_FEATURE = LicensedFeature.momentary( + null, + "security-auditing", + License.OperationMode.GOLD + ); + public static final LicensedFeature.Momentary TOKEN_SERVICE_FEATURE = LicensedFeature.momentary( + null, + "security-token-service", + License.OperationMode.STANDARD + ); private static final String REALMS_FEATURE_FAMILY = "security-realms"; // Builtin realms (file/native) realms are Basic licensed, so don't need to be checked or tracked // Some realms (LDAP, AD, PKI) are Gold+ - public static final LicensedFeature.Persistent LDAP_REALM_FEATURE = - LicensedFeature.persistent(REALMS_FEATURE_FAMILY, "ldap", License.OperationMode.GOLD); - public static final LicensedFeature.Persistent AD_REALM_FEATURE = - LicensedFeature.persistent(REALMS_FEATURE_FAMILY, "active-directory", License.OperationMode.GOLD); - public static final LicensedFeature.Persistent PKI_REALM_FEATURE = - LicensedFeature.persistent(REALMS_FEATURE_FAMILY, "pki", License.OperationMode.GOLD); + public static final LicensedFeature.Persistent LDAP_REALM_FEATURE = LicensedFeature.persistent( + REALMS_FEATURE_FAMILY, + "ldap", + License.OperationMode.GOLD + ); + public static final LicensedFeature.Persistent AD_REALM_FEATURE = LicensedFeature.persistent( + REALMS_FEATURE_FAMILY, + "active-directory", + License.OperationMode.GOLD + ); + public static final LicensedFeature.Persistent PKI_REALM_FEATURE = LicensedFeature.persistent( + REALMS_FEATURE_FAMILY, + "pki", + License.OperationMode.GOLD + ); // SSO realms are Platinum+ - public static final LicensedFeature.Persistent SAML_REALM_FEATURE = - LicensedFeature.persistent(REALMS_FEATURE_FAMILY, "saml", License.OperationMode.PLATINUM); - public static final LicensedFeature.Persistent OIDC_REALM_FEATURE = - LicensedFeature.persistent(REALMS_FEATURE_FAMILY, "oidc", License.OperationMode.PLATINUM); - public static final LicensedFeature.Persistent KERBEROS_REALM_FEATURE = - LicensedFeature.persistent(REALMS_FEATURE_FAMILY, "kerberos", License.OperationMode.PLATINUM); + public static final LicensedFeature.Persistent SAML_REALM_FEATURE = LicensedFeature.persistent( + REALMS_FEATURE_FAMILY, + "saml", + License.OperationMode.PLATINUM + ); + public static final LicensedFeature.Persistent OIDC_REALM_FEATURE = LicensedFeature.persistent( + REALMS_FEATURE_FAMILY, + "oidc", + License.OperationMode.PLATINUM + ); + public static final LicensedFeature.Persistent KERBEROS_REALM_FEATURE = LicensedFeature.persistent( + REALMS_FEATURE_FAMILY, + "kerberos", + License.OperationMode.PLATINUM + ); // Custom realms are Platinum+ - public static final LicensedFeature.Persistent CUSTOM_REALMS_FEATURE = - LicensedFeature.persistent(REALMS_FEATURE_FAMILY, "custom", License.OperationMode.PLATINUM); - - public static final LicensedFeature.Momentary DELEGATED_AUTHORIZATION_FEATURE = - LicensedFeature.momentary(null, "security-delegated-authorization", License.OperationMode.PLATINUM); - public static final LicensedFeature.Momentary AUTHORIZATION_ENGINE_FEATURE = - LicensedFeature.momentary(null, "security-authorization-engine", License.OperationMode.PLATINUM); + public static final LicensedFeature.Persistent CUSTOM_REALMS_FEATURE = LicensedFeature.persistent( + REALMS_FEATURE_FAMILY, + "custom", + License.OperationMode.PLATINUM + ); + + public static final LicensedFeature.Momentary DELEGATED_AUTHORIZATION_FEATURE = LicensedFeature.momentary( + null, + "security-delegated-authorization", + License.OperationMode.PLATINUM + ); + public static final LicensedFeature.Momentary AUTHORIZATION_ENGINE_FEATURE = LicensedFeature.momentary( + null, + "security-authorization-engine", + License.OperationMode.PLATINUM + ); // Custom role providers are Platinum+ - public static final LicensedFeature.Persistent CUSTOM_ROLE_PROVIDERS_FEATURE = - LicensedFeature.persistent(null, "security-roles-provider", License.OperationMode.PLATINUM); - - public static final LicensedFeature.Momentary OPERATOR_PRIVILEGES_FEATURE = - LicensedFeature.momentary(null, "operator-privileges", License.OperationMode.ENTERPRISE); + public static final LicensedFeature.Persistent CUSTOM_ROLE_PROVIDERS_FEATURE = LicensedFeature.persistent( + null, + "security-roles-provider", + License.OperationMode.PLATINUM + ); + + public static final LicensedFeature.Momentary OPERATOR_PRIVILEGES_FEATURE = LicensedFeature.momentary( + null, + "operator-privileges", + License.OperationMode.ENTERPRISE + ); private static final Logger logger = LogManager.getLogger(Security.class); @@ -450,29 +500,58 @@ private static void runStartupChecks(Settings settings) { protected Clock getClock() { return Clock.systemUTC(); } - protected SSLService getSslService() { return XPackPlugin.getSharedSslService(); } - protected XPackLicenseState getLicenseState() { return XPackPlugin.getSharedLicenseState(); } + + protected SSLService getSslService() { + return XPackPlugin.getSharedSslService(); + } + + protected XPackLicenseState getLicenseState() { + return XPackPlugin.getSharedLicenseState(); + } @Override - public Collection createComponents(Client client, ClusterService clusterService, ThreadPool threadPool, - ResourceWatcherService resourceWatcherService, ScriptService scriptService, - NamedXContentRegistry xContentRegistry, Environment environment, - NodeEnvironment nodeEnvironment, NamedWriteableRegistry namedWriteableRegistry, - IndexNameExpressionResolver expressionResolver, - Supplier repositoriesServiceSupplier) { + public Collection createComponents( + Client client, + ClusterService clusterService, + ThreadPool threadPool, + ResourceWatcherService resourceWatcherService, + ScriptService scriptService, + NamedXContentRegistry xContentRegistry, + Environment environment, + NodeEnvironment nodeEnvironment, + NamedWriteableRegistry namedWriteableRegistry, + IndexNameExpressionResolver expressionResolver, + Supplier repositoriesServiceSupplier + ) { try { - return createComponents(client, threadPool, clusterService, resourceWatcherService, scriptService, xContentRegistry, - environment, nodeEnvironment.nodeMetadata(), expressionResolver); + return createComponents( + client, + threadPool, + clusterService, + resourceWatcherService, + scriptService, + xContentRegistry, + environment, + nodeEnvironment.nodeMetadata(), + expressionResolver + ); } catch (final Exception e) { throw new IllegalStateException("security initialization failed", e); } } // pkg private for testing - tests want to pass in their set of extensions hence we are not using the extension service directly - Collection createComponents(Client client, ThreadPool threadPool, ClusterService clusterService, - ResourceWatcherService resourceWatcherService, ScriptService scriptService, - NamedXContentRegistry xContentRegistry, Environment environment, NodeMetadata nodeMetadata, - IndexNameExpressionResolver expressionResolver) throws Exception { + Collection createComponents( + Client client, + ThreadPool threadPool, + ClusterService clusterService, + ResourceWatcherService resourceWatcherService, + ScriptService scriptService, + NamedXContentRegistry xContentRegistry, + Environment environment, + NodeMetadata nodeMetadata, + IndexNameExpressionResolver expressionResolver + ) throws Exception { logger.info("Security is {}", enabled ? "enabled" : "disabled"); if (enabled == false) { return Collections.singletonList(new SecurityUsageServices(null, null, null, null)); @@ -482,11 +561,14 @@ Collection createComponents(Client client, ThreadPool threadPool, Cluste // We need to construct the checks here while the secure settings are still available. // If we wait until #getBoostrapChecks the secure settings will have been cleared/closed. final List checks = new ArrayList<>(); - checks.addAll(Arrays.asList( - new TokenSSLBootstrapCheck(), - new PkiRealmBootstrapCheck(getSslService()), - new SecurityImplicitBehaviorBootstrapCheck(nodeMetadata), - new TransportTLSBootstrapCheck())); + checks.addAll( + Arrays.asList( + new TokenSSLBootstrapCheck(), + new PkiRealmBootstrapCheck(getSslService()), + new SecurityImplicitBehaviorBootstrapCheck(nodeMetadata), + new TransportTLSBootstrapCheck() + ) + ); checks.addAll(InternalRealms.getBootstrapChecks(settings, environment)); this.bootstrapChecks.set(Collections.unmodifiableList(checks)); @@ -497,8 +579,8 @@ Collection createComponents(Client client, ThreadPool threadPool, Cluste // audit trail service construction final List auditTrails = XPackSettings.AUDIT_ENABLED.get(settings) - ? Collections.singletonList(new LoggingAuditTrail(settings, clusterService, threadPool)) - : Collections.emptyList(); + ? Collections.singletonList(new LoggingAuditTrail(settings, clusterService, threadPool)) + : Collections.emptyList(); final AuditTrailService auditTrailService = new AuditTrailService(auditTrails, getLicenseState()); components.add(auditTrailService); this.auditTrailService.set(auditTrailService); @@ -520,15 +602,32 @@ Collection createComponents(Client client, ThreadPool threadPool, Cluste // realms construction final NativeUsersStore nativeUsersStore = new NativeUsersStore(settings, client, securityIndex.get()); - final NativeRoleMappingStore nativeRoleMappingStore = new NativeRoleMappingStore(settings, client, securityIndex.get(), - scriptService); + final NativeRoleMappingStore nativeRoleMappingStore = new NativeRoleMappingStore( + settings, + client, + securityIndex.get(), + scriptService + ); final AnonymousUser anonymousUser = new AnonymousUser(settings); components.add(anonymousUser); final ReservedRealm reservedRealm = new ReservedRealm(environment, settings, nativeUsersStore, anonymousUser, threadPool); - final SecurityExtension.SecurityComponents extensionComponents = new ExtensionComponents(environment, client, clusterService, - resourceWatcherService, nativeRoleMappingStore); - Map realmFactories = new HashMap<>(InternalRealms.getFactories(threadPool, resourceWatcherService, - getSslService(), nativeUsersStore, nativeRoleMappingStore, securityIndex.get())); + final SecurityExtension.SecurityComponents extensionComponents = new ExtensionComponents( + environment, + client, + clusterService, + resourceWatcherService, + nativeRoleMappingStore + ); + Map realmFactories = new HashMap<>( + InternalRealms.getFactories( + threadPool, + resourceWatcherService, + getSslService(), + nativeUsersStore, + nativeRoleMappingStore, + securityIndex.get() + ) + ); for (SecurityExtension extension : securityExtensions) { Map newRealms = extension.getRealms(extensionComponents); for (Map.Entry entry : newRealms.entrySet()) { @@ -537,8 +636,14 @@ Collection createComponents(Client client, ThreadPool threadPool, Cluste } } } - final Realms realms = - new Realms(settings, environment, realmFactories, getLicenseState(), threadPool.getThreadContext(), reservedRealm); + final Realms realms = new Realms( + settings, + environment, + realmFactories, + getLicenseState(), + threadPool.getThreadContext(), + reservedRealm + ); components.add(nativeUsersStore); components.add(nativeRoleMappingStore); components.add(realms); @@ -551,14 +656,23 @@ Collection createComponents(Client client, ThreadPool threadPool, Cluste components.add(cacheInvalidatorRegistry); securityIndex.get().addStateListener(cacheInvalidatorRegistry::onSecurityIndexStateChange); - final NativePrivilegeStore privilegeStore = - new NativePrivilegeStore(settings, client, securityIndex.get(), cacheInvalidatorRegistry); + final NativePrivilegeStore privilegeStore = new NativePrivilegeStore( + settings, + client, + securityIndex.get(), + cacheInvalidatorRegistry + ); components.add(privilegeStore); dlsBitsetCache.set(new DocumentSubsetBitsetCache(settings, threadPool)); final FieldPermissionsCache fieldPermissionsCache = new FieldPermissionsCache(settings); - final FileRolesStore fileRolesStore = new FileRolesStore(settings, environment, resourceWatcherService, getLicenseState(), - xContentRegistry); + final FileRolesStore fileRolesStore = new FileRolesStore( + settings, + environment, + resourceWatcherService, + getLicenseState(), + xContentRegistry + ); final NativeRolesStore nativeRolesStore = new NativeRolesStore(settings, client, getLicenseState(), securityIndex.get()); final ReservedRolesStore reservedRolesStore = new ReservedRolesStore(); @@ -572,16 +686,35 @@ Collection createComponents(Client client, ThreadPool threadPool, Cluste } } - final ApiKeyService apiKeyService = new ApiKeyService(settings, Clock.systemUTC(), client, securityIndex.get(), - clusterService, cacheInvalidatorRegistry, threadPool); + final ApiKeyService apiKeyService = new ApiKeyService( + settings, + Clock.systemUTC(), + client, + securityIndex.get(), + clusterService, + cacheInvalidatorRegistry, + threadPool + ); components.add(apiKeyService); final IndexServiceAccountTokenStore indexServiceAccountTokenStore = new IndexServiceAccountTokenStore( - settings, threadPool, getClock(), client, securityIndex.get(), clusterService, cacheInvalidatorRegistry); + settings, + threadPool, + getClock(), + client, + securityIndex.get(), + clusterService, + cacheInvalidatorRegistry + ); components.add(indexServiceAccountTokenStore); - final FileServiceAccountTokenStore fileServiceAccountTokenStore = - new FileServiceAccountTokenStore(environment, resourceWatcherService, threadPool, clusterService, cacheInvalidatorRegistry); + final FileServiceAccountTokenStore fileServiceAccountTokenStore = new FileServiceAccountTokenStore( + environment, + resourceWatcherService, + threadPool, + clusterService, + cacheInvalidatorRegistry + ); components.add(fileServiceAccountTokenStore); final ServiceAccountService serviceAccountService = new ServiceAccountService( @@ -598,10 +731,19 @@ Collection createComponents(Client client, ThreadPool threadPool, Cluste customRoleProviders, getLicenseState() ); - final CompositeRolesStore allRolesStore = new CompositeRolesStore(settings, roleProviders, - privilegeStore, threadPool.getThreadContext(), getLicenseState(), fieldPermissionsCache, apiKeyService, - serviceAccountService, dlsBitsetCache.get(), expressionResolver, - new DeprecationRoleDescriptorConsumer(clusterService, threadPool)); + final CompositeRolesStore allRolesStore = new CompositeRolesStore( + settings, + roleProviders, + privilegeStore, + threadPool.getThreadContext(), + getLicenseState(), + fieldPermissionsCache, + apiKeyService, + serviceAccountService, + dlsBitsetCache.get(), + expressionResolver, + new DeprecationRoleDescriptorConsumer(clusterService, threadPool) + ); securityIndex.get().addStateListener(allRolesStore::onSecurityIndexStateChange); // We use the value of the {@code ENROLLMENT_ENABLED} setting to determine if the node is starting up with auto-generated @@ -614,7 +756,8 @@ Collection createComponents(Client client, ThreadPool threadPool, Cluste securityIndex.get(), getSslService(), client, - environment); + environment + ); // to keep things simple, just invalidate all cached entries on license change. this happens so rarely that the impact should be // minimal @@ -625,34 +768,62 @@ Collection createComponents(Client client, ThreadPool threadPool, Cluste final boolean operatorPrivilegesEnabled = OPERATOR_PRIVILEGES_ENABLED.get(settings); if (operatorPrivilegesEnabled) { logger.info("operator privileges are enabled"); - operatorPrivilegesService = new OperatorPrivileges.DefaultOperatorPrivilegesService(getLicenseState(), + operatorPrivilegesService = new OperatorPrivileges.DefaultOperatorPrivilegesService( + getLicenseState(), new FileOperatorUsersStore(environment, resourceWatcherService), - new OperatorOnlyRegistry(clusterService.getClusterSettings())); + new OperatorOnlyRegistry(clusterService.getClusterSettings()) + ); } else { operatorPrivilegesService = OperatorPrivileges.NOOP_OPERATOR_PRIVILEGES_SERVICE; } - authcService.set(new AuthenticationService(settings, realms, auditTrailService, failureHandler, threadPool, - anonymousUser, tokenService, apiKeyService, serviceAccountService, operatorPrivilegesService)); + authcService.set( + new AuthenticationService( + settings, + realms, + auditTrailService, + failureHandler, + threadPool, + anonymousUser, + tokenService, + apiKeyService, + serviceAccountService, + operatorPrivilegesService + ) + ); components.add(authcService.get()); securityIndex.get().addStateListener(authcService.get()::onSecurityIndexStateChange); Set requestInterceptors = Sets.newHashSet( new ResizeRequestInterceptor(threadPool, getLicenseState(), auditTrailService), - new IndicesAliasesRequestInterceptor(threadPool.getThreadContext(), getLicenseState(), auditTrailService)); + new IndicesAliasesRequestInterceptor(threadPool.getThreadContext(), getLicenseState(), auditTrailService) + ); if (XPackSettings.DLS_FLS_ENABLED.get(settings)) { - requestInterceptors.addAll(Arrays.asList( - new SearchRequestInterceptor(threadPool, getLicenseState(), clusterService), - new ShardSearchRequestInterceptor(threadPool, getLicenseState(), clusterService), - new UpdateRequestInterceptor(threadPool, getLicenseState()), - new BulkShardRequestInterceptor(threadPool, getLicenseState()), - new DlsFlsLicenseRequestInterceptor(threadPool.getThreadContext(), getLicenseState()) - )); + requestInterceptors.addAll( + Arrays.asList( + new SearchRequestInterceptor(threadPool, getLicenseState(), clusterService), + new ShardSearchRequestInterceptor(threadPool, getLicenseState(), clusterService), + new UpdateRequestInterceptor(threadPool, getLicenseState()), + new BulkShardRequestInterceptor(threadPool, getLicenseState()), + new DlsFlsLicenseRequestInterceptor(threadPool.getThreadContext(), getLicenseState()) + ) + ); } requestInterceptors = Collections.unmodifiableSet(requestInterceptors); - final AuthorizationService authzService = new AuthorizationService(settings, allRolesStore, clusterService, - auditTrailService, failureHandler, threadPool, anonymousUser, getAuthorizationEngine(), requestInterceptors, - getLicenseState(), expressionResolver, operatorPrivilegesService); + final AuthorizationService authzService = new AuthorizationService( + settings, + allRolesStore, + clusterService, + auditTrailService, + failureHandler, + threadPool, + anonymousUser, + getAuthorizationEngine(), + requestInterceptors, + getLicenseState(), + expressionResolver, + operatorPrivilegesService + ); components.add(nativeRolesStore); // used by roles actions components.add(reservedRolesStore); // used by roles actions @@ -666,11 +837,30 @@ auditTrailService, failureHandler, threadPool, anonymousUser, getAuthorizationEn ipFilter.set(new IPFilter(settings, auditTrailService, clusterService.getClusterSettings(), getLicenseState())); components.add(ipFilter.get()); DestructiveOperations destructiveOperations = new DestructiveOperations(settings, clusterService.getClusterSettings()); - securityInterceptor.set(new SecurityServerTransportInterceptor(settings, threadPool, authcService.get(), - authzService, getSslService(), securityContext.get(), destructiveOperations, clusterService)); + securityInterceptor.set( + new SecurityServerTransportInterceptor( + settings, + threadPool, + authcService.get(), + authzService, + getSslService(), + securityContext.get(), + destructiveOperations, + clusterService + ) + ); - securityActionFilter.set(new SecurityActionFilter(authcService.get(), authzService, auditTrailService, getLicenseState(), - threadPool, securityContext.get(), destructiveOperations)); + securityActionFilter.set( + new SecurityActionFilter( + authcService.get(), + authzService, + auditTrailService, + getLicenseState(), + threadPool, + securityContext.get(), + destructiveOperations + ) + ); components.add(new SecurityUsageServices(realms, allRolesStore, nativeRoleMappingStore, ipFilter.get())); @@ -683,8 +873,10 @@ private AuthorizationEngine getAuthorizationEngine() { return findValueFromExtensions("authorization engine", extension -> extension.getAuthorizationEngine(settings)); } - private AuthenticationFailureHandler createAuthenticationFailureHandler(final Realms realms, - final SecurityExtension.SecurityComponents components) { + private AuthenticationFailureHandler createAuthenticationFailureHandler( + final Realms realms, + final SecurityExtension.SecurityComponents components + ) { AuthenticationFailureHandler failureHandler = findValueFromExtensions( "authentication failure handler", extension -> extension.getAuthenticationFailureHandler(components) @@ -697,17 +889,17 @@ private AuthenticationFailureHandler createAuthenticationFailureHandler(final Re Map> realmFailureHeaders = realm.getAuthenticationFailureHeaders(); realmFailureHeaders.entrySet().stream().forEach((e) -> { String key = e.getKey(); - e.getValue().stream() - .filter(v -> defaultFailureResponseHeaders.computeIfAbsent(key, x -> new ArrayList<>()).contains(v) - == false) - .forEach(v -> defaultFailureResponseHeaders.get(key).add(v)); + e.getValue() + .stream() + .filter(v -> defaultFailureResponseHeaders.computeIfAbsent(key, x -> new ArrayList<>()).contains(v) == false) + .forEach(v -> defaultFailureResponseHeaders.get(key).add(v)); }); }); if (TokenService.isTokenServiceEnabled(settings)) { String bearerScheme = "Bearer realm=\"" + XPackField.SECURITY + "\""; if (defaultFailureResponseHeaders.computeIfAbsent("WWW-Authenticate", x -> new ArrayList<>()) - .contains(bearerScheme) == false) { + .contains(bearerScheme) == false) { defaultFailureResponseHeaders.get("WWW-Authenticate").add(bearerScheme); } } @@ -722,9 +914,7 @@ private AuthenticationFailureHandler createAuthenticationFailureHandler(final Re }; DefaultAuthenticationFailureHandler finalDefaultFailureHandler = new DefaultAuthenticationFailureHandler(headersSupplier.get()); failureHandler = finalDefaultFailureHandler; - getLicenseState().addListener(() -> { - finalDefaultFailureHandler.setHeaders(headersSupplier.get()); - }); + getLicenseState().addListener(() -> { finalDefaultFailureHandler.setHeaders(headersSupplier.get()); }); } return failureHandler; } @@ -785,12 +975,13 @@ static Settings additionalSettings(final Settings settings, final boolean enable SecurityHttpSettings.overrideSettings(builder, settings); } else { final String message = String.format( - Locale.ROOT, - "http type setting [%s] must be [%s] or [%s] but is [%s]", - NetworkModule.HTTP_TYPE_KEY, - SecurityField.NAME4, - SecurityField.NIO, - httpType); + Locale.ROOT, + "http type setting [%s] must be [%s] or [%s] but is [%s]", + NetworkModule.HTTP_TYPE_KEY, + SecurityField.NAME4, + SecurityField.NIO, + httpType + ); throw new IllegalArgumentException(message); } } else { @@ -810,9 +1001,9 @@ public List> getSettings() { return getSettings(securityExtensions); } - /** - * Get the {@link Setting setting configuration} for all security components, including those defined in extensions. - */ + /** + * Get the {@link Setting setting configuration} for all security components, including those defined in extensions. + */ public static List> getSettings(List securityExtensions) { List> settingsList = new ArrayList<>(); @@ -855,8 +1046,15 @@ public static List> getSettings(List securityExten settingsList.add(CachingServiceAccountTokenStore.CACHE_MAX_TOKENS_SETTING); // hide settings - settingsList.add(Setting.listSetting(SecurityField.setting("hide_settings"), Collections.emptyList(), Function.identity(), - Property.NodeScope, Property.Filtered)); + settingsList.add( + Setting.listSetting( + SecurityField.setting("hide_settings"), + Collections.emptyList(), + Function.identity(), + Property.NodeScope, + Property.Filtered + ) + ); return settingsList; } @@ -885,7 +1083,7 @@ public List getSettingsFilter() { @Override public List getBootstrapChecks() { - return bootstrapChecks.get(); + return bootstrapChecks.get(); } @Override @@ -894,43 +1092,44 @@ public void onIndexModule(IndexModule module) { assert getLicenseState() != null; if (XPackSettings.DLS_FLS_ENABLED.get(settings)) { assert dlsBitsetCache.get() != null; - module.setReaderWrapper(indexService -> - new SecurityIndexReaderWrapper( - shardId -> indexService.newSearchExecutionContext(shardId.id(), - 0, - // we pass a null index reader, which is legal and will disable rewrite optimizations - // based on index statistics, which is probably safer... - null, - () -> { - throw new IllegalArgumentException("permission filters are not allowed to use the current timestamp"); - - }, - null, - // Don't use runtime mappings in the security query - emptyMap()), - dlsBitsetCache.get(), - securityContext.get(), - getLicenseState(), - indexService.getScriptService())); + module.setReaderWrapper( + indexService -> new SecurityIndexReaderWrapper( + shardId -> indexService.newSearchExecutionContext( + shardId.id(), + 0, + // we pass a null index reader, which is legal and will disable rewrite optimizations + // based on index statistics, which is probably safer... + null, + () -> { + throw new IllegalArgumentException("permission filters are not allowed to use the current timestamp"); + + }, + null, + // Don't use runtime mappings in the security query + emptyMap() + ), + dlsBitsetCache.get(), + securityContext.get(), + getLicenseState(), + indexService.getScriptService() + ) + ); /* * We need to forcefully overwrite the query cache implementation to use security's opt-out query cache implementation. This * implementation disables the query cache if field level security is used for a particular request. We have to forcefully * overwrite the query cache implementation to prevent data leakage to unauthorized users. */ - module.forceQueryCacheProvider( - (indexSettings, cache) -> { - final OptOutQueryCache queryCache = - new OptOutQueryCache(indexSettings, cache, threadContext.get()); + module.forceQueryCacheProvider((indexSettings, cache) -> { + final OptOutQueryCache queryCache = new OptOutQueryCache(indexSettings, cache, threadContext.get()); - return queryCache; - }); + return queryCache; + }); } // in order to prevent scroll ids from being maliciously crafted and/or guessed, a listener is added that // attaches information to the scroll context so that we can validate the user that created the scroll against // the user that is executing a scroll operation - module.addSearchOperationListener( - new SecuritySearchOperationListener(securityContext.get(), auditTrailService.get())); + module.addSearchOperationListener(new SecuritySearchOperationListener(securityContext.get(), auditTrailService.get())); } } @@ -942,58 +1141,57 @@ public void onIndexModule(IndexModule module) { return Arrays.asList(usageAction, infoAction); } return Arrays.asList( - new ActionHandler<>(ClearRealmCacheAction.INSTANCE, TransportClearRealmCacheAction.class), - new ActionHandler<>(ClearRolesCacheAction.INSTANCE, TransportClearRolesCacheAction.class), - new ActionHandler<>(ClearPrivilegesCacheAction.INSTANCE, TransportClearPrivilegesCacheAction.class), - new ActionHandler<>(ClearSecurityCacheAction.INSTANCE, TransportClearSecurityCacheAction.class), - new ActionHandler<>(GetUsersAction.INSTANCE, TransportGetUsersAction.class), - new ActionHandler<>(PutUserAction.INSTANCE, TransportPutUserAction.class), - new ActionHandler<>(DeleteUserAction.INSTANCE, TransportDeleteUserAction.class), - new ActionHandler<>(GetRolesAction.INSTANCE, TransportGetRolesAction.class), - new ActionHandler<>(PutRoleAction.INSTANCE, TransportPutRoleAction.class), - new ActionHandler<>(DeleteRoleAction.INSTANCE, TransportDeleteRoleAction.class), - new ActionHandler<>(ChangePasswordAction.INSTANCE, TransportChangePasswordAction.class), - new ActionHandler<>(AuthenticateAction.INSTANCE, TransportAuthenticateAction.class), - new ActionHandler<>(SetEnabledAction.INSTANCE, TransportSetEnabledAction.class), - new ActionHandler<>(HasPrivilegesAction.INSTANCE, TransportHasPrivilegesAction.class), - new ActionHandler<>(GetUserPrivilegesAction.INSTANCE, TransportGetUserPrivilegesAction.class), - new ActionHandler<>(GetRoleMappingsAction.INSTANCE, TransportGetRoleMappingsAction.class), - new ActionHandler<>(PutRoleMappingAction.INSTANCE, TransportPutRoleMappingAction.class), - new ActionHandler<>(DeleteRoleMappingAction.INSTANCE, TransportDeleteRoleMappingAction.class), - new ActionHandler<>(CreateTokenAction.INSTANCE, TransportCreateTokenAction.class), - new ActionHandler<>(InvalidateTokenAction.INSTANCE, TransportInvalidateTokenAction.class), - new ActionHandler<>(GetCertificateInfoAction.INSTANCE, TransportGetCertificateInfoAction.class), - new ActionHandler<>(RefreshTokenAction.INSTANCE, TransportRefreshTokenAction.class), - new ActionHandler<>(SamlPrepareAuthenticationAction.INSTANCE, TransportSamlPrepareAuthenticationAction.class), - new ActionHandler<>(SamlAuthenticateAction.INSTANCE, TransportSamlAuthenticateAction.class), - new ActionHandler<>(SamlLogoutAction.INSTANCE, TransportSamlLogoutAction.class), - new ActionHandler<>(SamlInvalidateSessionAction.INSTANCE, TransportSamlInvalidateSessionAction.class), - new ActionHandler<>(SamlCompleteLogoutAction.INSTANCE, TransportSamlCompleteLogoutAction.class), - new ActionHandler<>(SamlSpMetadataAction.INSTANCE, TransportSamlSpMetadataAction.class), - new ActionHandler<>(OpenIdConnectPrepareAuthenticationAction.INSTANCE, - TransportOpenIdConnectPrepareAuthenticationAction.class), - new ActionHandler<>(OpenIdConnectAuthenticateAction.INSTANCE, TransportOpenIdConnectAuthenticateAction.class), - new ActionHandler<>(OpenIdConnectLogoutAction.INSTANCE, TransportOpenIdConnectLogoutAction.class), - new ActionHandler<>(GetBuiltinPrivilegesAction.INSTANCE, TransportGetBuiltinPrivilegesAction.class), - new ActionHandler<>(GetPrivilegesAction.INSTANCE, TransportGetPrivilegesAction.class), - new ActionHandler<>(PutPrivilegesAction.INSTANCE, TransportPutPrivilegesAction.class), - new ActionHandler<>(DeletePrivilegesAction.INSTANCE, TransportDeletePrivilegesAction.class), - new ActionHandler<>(CreateApiKeyAction.INSTANCE, TransportCreateApiKeyAction.class), - new ActionHandler<>(GrantApiKeyAction.INSTANCE, TransportGrantApiKeyAction.class), - new ActionHandler<>(InvalidateApiKeyAction.INSTANCE, TransportInvalidateApiKeyAction.class), - new ActionHandler<>(GetApiKeyAction.INSTANCE, TransportGetApiKeyAction.class), - new ActionHandler<>(QueryApiKeyAction.INSTANCE, TransportQueryApiKeyAction.class), - new ActionHandler<>(DelegatePkiAuthenticationAction.INSTANCE, TransportDelegatePkiAuthenticationAction.class), - new ActionHandler<>(CreateServiceAccountTokenAction.INSTANCE, TransportCreateServiceAccountTokenAction.class), - new ActionHandler<>(DeleteServiceAccountTokenAction.INSTANCE, TransportDeleteServiceAccountTokenAction.class), - new ActionHandler<>(GetServiceAccountCredentialsAction.INSTANCE, TransportGetServiceAccountCredentialsAction.class), - new ActionHandler<>(GetServiceAccountNodesCredentialsAction.INSTANCE, - TransportGetServiceAccountNodesCredentialsAction.class), - new ActionHandler<>(GetServiceAccountAction.INSTANCE, TransportGetServiceAccountAction.class), - new ActionHandler<>(KibanaEnrollmentAction.INSTANCE, TransportKibanaEnrollmentAction.class), - new ActionHandler<>(NodeEnrollmentAction.INSTANCE, TransportNodeEnrollmentAction.class), - usageAction, - infoAction); + new ActionHandler<>(ClearRealmCacheAction.INSTANCE, TransportClearRealmCacheAction.class), + new ActionHandler<>(ClearRolesCacheAction.INSTANCE, TransportClearRolesCacheAction.class), + new ActionHandler<>(ClearPrivilegesCacheAction.INSTANCE, TransportClearPrivilegesCacheAction.class), + new ActionHandler<>(ClearSecurityCacheAction.INSTANCE, TransportClearSecurityCacheAction.class), + new ActionHandler<>(GetUsersAction.INSTANCE, TransportGetUsersAction.class), + new ActionHandler<>(PutUserAction.INSTANCE, TransportPutUserAction.class), + new ActionHandler<>(DeleteUserAction.INSTANCE, TransportDeleteUserAction.class), + new ActionHandler<>(GetRolesAction.INSTANCE, TransportGetRolesAction.class), + new ActionHandler<>(PutRoleAction.INSTANCE, TransportPutRoleAction.class), + new ActionHandler<>(DeleteRoleAction.INSTANCE, TransportDeleteRoleAction.class), + new ActionHandler<>(ChangePasswordAction.INSTANCE, TransportChangePasswordAction.class), + new ActionHandler<>(AuthenticateAction.INSTANCE, TransportAuthenticateAction.class), + new ActionHandler<>(SetEnabledAction.INSTANCE, TransportSetEnabledAction.class), + new ActionHandler<>(HasPrivilegesAction.INSTANCE, TransportHasPrivilegesAction.class), + new ActionHandler<>(GetUserPrivilegesAction.INSTANCE, TransportGetUserPrivilegesAction.class), + new ActionHandler<>(GetRoleMappingsAction.INSTANCE, TransportGetRoleMappingsAction.class), + new ActionHandler<>(PutRoleMappingAction.INSTANCE, TransportPutRoleMappingAction.class), + new ActionHandler<>(DeleteRoleMappingAction.INSTANCE, TransportDeleteRoleMappingAction.class), + new ActionHandler<>(CreateTokenAction.INSTANCE, TransportCreateTokenAction.class), + new ActionHandler<>(InvalidateTokenAction.INSTANCE, TransportInvalidateTokenAction.class), + new ActionHandler<>(GetCertificateInfoAction.INSTANCE, TransportGetCertificateInfoAction.class), + new ActionHandler<>(RefreshTokenAction.INSTANCE, TransportRefreshTokenAction.class), + new ActionHandler<>(SamlPrepareAuthenticationAction.INSTANCE, TransportSamlPrepareAuthenticationAction.class), + new ActionHandler<>(SamlAuthenticateAction.INSTANCE, TransportSamlAuthenticateAction.class), + new ActionHandler<>(SamlLogoutAction.INSTANCE, TransportSamlLogoutAction.class), + new ActionHandler<>(SamlInvalidateSessionAction.INSTANCE, TransportSamlInvalidateSessionAction.class), + new ActionHandler<>(SamlCompleteLogoutAction.INSTANCE, TransportSamlCompleteLogoutAction.class), + new ActionHandler<>(SamlSpMetadataAction.INSTANCE, TransportSamlSpMetadataAction.class), + new ActionHandler<>(OpenIdConnectPrepareAuthenticationAction.INSTANCE, TransportOpenIdConnectPrepareAuthenticationAction.class), + new ActionHandler<>(OpenIdConnectAuthenticateAction.INSTANCE, TransportOpenIdConnectAuthenticateAction.class), + new ActionHandler<>(OpenIdConnectLogoutAction.INSTANCE, TransportOpenIdConnectLogoutAction.class), + new ActionHandler<>(GetBuiltinPrivilegesAction.INSTANCE, TransportGetBuiltinPrivilegesAction.class), + new ActionHandler<>(GetPrivilegesAction.INSTANCE, TransportGetPrivilegesAction.class), + new ActionHandler<>(PutPrivilegesAction.INSTANCE, TransportPutPrivilegesAction.class), + new ActionHandler<>(DeletePrivilegesAction.INSTANCE, TransportDeletePrivilegesAction.class), + new ActionHandler<>(CreateApiKeyAction.INSTANCE, TransportCreateApiKeyAction.class), + new ActionHandler<>(GrantApiKeyAction.INSTANCE, TransportGrantApiKeyAction.class), + new ActionHandler<>(InvalidateApiKeyAction.INSTANCE, TransportInvalidateApiKeyAction.class), + new ActionHandler<>(GetApiKeyAction.INSTANCE, TransportGetApiKeyAction.class), + new ActionHandler<>(QueryApiKeyAction.INSTANCE, TransportQueryApiKeyAction.class), + new ActionHandler<>(DelegatePkiAuthenticationAction.INSTANCE, TransportDelegatePkiAuthenticationAction.class), + new ActionHandler<>(CreateServiceAccountTokenAction.INSTANCE, TransportCreateServiceAccountTokenAction.class), + new ActionHandler<>(DeleteServiceAccountTokenAction.INSTANCE, TransportDeleteServiceAccountTokenAction.class), + new ActionHandler<>(GetServiceAccountCredentialsAction.INSTANCE, TransportGetServiceAccountCredentialsAction.class), + new ActionHandler<>(GetServiceAccountNodesCredentialsAction.INSTANCE, TransportGetServiceAccountNodesCredentialsAction.class), + new ActionHandler<>(GetServiceAccountAction.INSTANCE, TransportGetServiceAccountAction.class), + new ActionHandler<>(KibanaEnrollmentAction.INSTANCE, TransportKibanaEnrollmentAction.class), + new ActionHandler<>(NodeEnrollmentAction.INSTANCE, TransportNodeEnrollmentAction.class), + usageAction, + infoAction + ); } @Override @@ -1005,68 +1203,75 @@ public List getActionFilters() { } @Override - public List getRestHandlers(Settings settings, RestController restController, ClusterSettings clusterSettings, - IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter, - IndexNameExpressionResolver indexNameExpressionResolver, - Supplier nodesInCluster) { + public List getRestHandlers( + Settings settings, + RestController restController, + ClusterSettings clusterSettings, + IndexScopedSettings indexScopedSettings, + SettingsFilter settingsFilter, + IndexNameExpressionResolver indexNameExpressionResolver, + Supplier nodesInCluster + ) { if (enabled == false) { return emptyList(); } return Arrays.asList( - new RestAuthenticateAction(settings, securityContext.get(), getLicenseState()), - new RestClearRealmCacheAction(settings, getLicenseState()), - new RestClearRolesCacheAction(settings, getLicenseState()), - new RestClearPrivilegesCacheAction(settings, getLicenseState()), - new RestClearApiKeyCacheAction(settings, getLicenseState()), - new RestClearServiceAccountTokenStoreCacheAction(settings, getLicenseState()), - new RestGetUsersAction(settings, getLicenseState()), - new RestPutUserAction(settings, getLicenseState()), - new RestDeleteUserAction(settings, getLicenseState()), - new RestGetRolesAction(settings, getLicenseState()), - new RestPutRoleAction(settings, getLicenseState()), - new RestDeleteRoleAction(settings, getLicenseState()), - new RestChangePasswordAction(settings, securityContext.get(), getLicenseState()), - new RestSetEnabledAction(settings, getLicenseState()), - new RestHasPrivilegesAction(settings, securityContext.get(), getLicenseState()), - new RestGetUserPrivilegesAction(settings, securityContext.get(), getLicenseState()), - new RestGetRoleMappingsAction(settings, getLicenseState()), - new RestPutRoleMappingAction(settings, getLicenseState()), - new RestDeleteRoleMappingAction(settings, getLicenseState()), - new RestGetTokenAction(settings, getLicenseState()), - new RestInvalidateTokenAction(settings, getLicenseState()), - new RestGetCertificateInfoAction(), - new RestSamlPrepareAuthenticationAction(settings, getLicenseState()), - new RestSamlAuthenticateAction(settings, getLicenseState()), - new RestSamlLogoutAction(settings, getLicenseState()), - new RestSamlInvalidateSessionAction(settings, getLicenseState()), - new RestSamlCompleteLogoutAction(settings, getLicenseState()), - new RestSamlSpMetadataAction(settings, getLicenseState()), - new RestOpenIdConnectPrepareAuthenticationAction(settings, getLicenseState()), - new RestOpenIdConnectAuthenticateAction(settings, getLicenseState()), - new RestOpenIdConnectLogoutAction(settings, getLicenseState()), - new RestGetBuiltinPrivilegesAction(settings, getLicenseState()), - new RestGetPrivilegesAction(settings, getLicenseState()), - new RestPutPrivilegesAction(settings, getLicenseState()), - new RestDeletePrivilegesAction(settings, getLicenseState()), - new RestCreateApiKeyAction(settings, getLicenseState()), - new RestGrantApiKeyAction(settings, getLicenseState()), - new RestInvalidateApiKeyAction(settings, getLicenseState()), - new RestGetApiKeyAction(settings, getLicenseState()), - new RestQueryApiKeyAction(settings, getLicenseState()), - new RestDelegatePkiAuthenticationAction(settings, getLicenseState()), - new RestCreateServiceAccountTokenAction(settings, getLicenseState()), - new RestDeleteServiceAccountTokenAction(settings, getLicenseState()), - new RestGetServiceAccountCredentialsAction(settings, getLicenseState()), - new RestGetServiceAccountAction(settings, getLicenseState()), - new RestKibanaEnrollAction(settings, getLicenseState()), - new RestNodeEnrollmentAction(settings, getLicenseState()) + new RestAuthenticateAction(settings, securityContext.get(), getLicenseState()), + new RestClearRealmCacheAction(settings, getLicenseState()), + new RestClearRolesCacheAction(settings, getLicenseState()), + new RestClearPrivilegesCacheAction(settings, getLicenseState()), + new RestClearApiKeyCacheAction(settings, getLicenseState()), + new RestClearServiceAccountTokenStoreCacheAction(settings, getLicenseState()), + new RestGetUsersAction(settings, getLicenseState()), + new RestPutUserAction(settings, getLicenseState()), + new RestDeleteUserAction(settings, getLicenseState()), + new RestGetRolesAction(settings, getLicenseState()), + new RestPutRoleAction(settings, getLicenseState()), + new RestDeleteRoleAction(settings, getLicenseState()), + new RestChangePasswordAction(settings, securityContext.get(), getLicenseState()), + new RestSetEnabledAction(settings, getLicenseState()), + new RestHasPrivilegesAction(settings, securityContext.get(), getLicenseState()), + new RestGetUserPrivilegesAction(settings, securityContext.get(), getLicenseState()), + new RestGetRoleMappingsAction(settings, getLicenseState()), + new RestPutRoleMappingAction(settings, getLicenseState()), + new RestDeleteRoleMappingAction(settings, getLicenseState()), + new RestGetTokenAction(settings, getLicenseState()), + new RestInvalidateTokenAction(settings, getLicenseState()), + new RestGetCertificateInfoAction(), + new RestSamlPrepareAuthenticationAction(settings, getLicenseState()), + new RestSamlAuthenticateAction(settings, getLicenseState()), + new RestSamlLogoutAction(settings, getLicenseState()), + new RestSamlInvalidateSessionAction(settings, getLicenseState()), + new RestSamlCompleteLogoutAction(settings, getLicenseState()), + new RestSamlSpMetadataAction(settings, getLicenseState()), + new RestOpenIdConnectPrepareAuthenticationAction(settings, getLicenseState()), + new RestOpenIdConnectAuthenticateAction(settings, getLicenseState()), + new RestOpenIdConnectLogoutAction(settings, getLicenseState()), + new RestGetBuiltinPrivilegesAction(settings, getLicenseState()), + new RestGetPrivilegesAction(settings, getLicenseState()), + new RestPutPrivilegesAction(settings, getLicenseState()), + new RestDeletePrivilegesAction(settings, getLicenseState()), + new RestCreateApiKeyAction(settings, getLicenseState()), + new RestGrantApiKeyAction(settings, getLicenseState()), + new RestInvalidateApiKeyAction(settings, getLicenseState()), + new RestGetApiKeyAction(settings, getLicenseState()), + new RestQueryApiKeyAction(settings, getLicenseState()), + new RestDelegatePkiAuthenticationAction(settings, getLicenseState()), + new RestCreateServiceAccountTokenAction(settings, getLicenseState()), + new RestDeleteServiceAccountTokenAction(settings, getLicenseState()), + new RestGetServiceAccountCredentialsAction(settings, getLicenseState()), + new RestGetServiceAccountAction(settings, getLicenseState()), + new RestKibanaEnrollAction(settings, getLicenseState()), + new RestNodeEnrollmentAction(settings, getLicenseState()) ); } @Override public Map getProcessors(Processor.Parameters parameters) { - return Collections.singletonMap(SetSecurityUserProcessor.TYPE, - new SetSecurityUserProcessor.Factory(securityContext::get, settings)); + return Collections.singletonMap( + SetSecurityUserProcessor.TYPE, + new SetSecurityUserProcessor.Factory(securityContext::get, settings) + ); } /** @@ -1084,22 +1289,24 @@ public Map getProcessors(Processor.Parameters paramet * unknown setting [xpack.security.authc.realms.file1.order]. This validation method provides an error that is easier to * understand and take action on. */ - static void validateRealmSettings(Settings settings) { - final Set badRealmSettings = settings.keySet().stream() - .filter(k -> k.startsWith(RealmSettings.PREFIX)) - .filter(key -> { - final String suffix = key.substring(RealmSettings.PREFIX.length()); - // suffix-part, only contains a single '.' - return suffix.indexOf('.') == suffix.lastIndexOf('.'); - }) - .collect(Collectors.toSet()); + static void validateRealmSettings(Settings settings) { + final Set badRealmSettings = settings.keySet().stream().filter(k -> k.startsWith(RealmSettings.PREFIX)).filter(key -> { + final String suffix = key.substring(RealmSettings.PREFIX.length()); + // suffix-part, only contains a single '.' + return suffix.indexOf('.') == suffix.lastIndexOf('.'); + }).collect(Collectors.toSet()); if (badRealmSettings.isEmpty() == false) { String sampleRealmSetting = RealmSettings.realmSettingPrefix(new RealmConfig.RealmIdentifier("file", "my_file")) + "order"; - throw new IllegalArgumentException("Incorrect realm settings found. " + - "Realm settings have been changed to include the type as part of the setting key.\n" + - "For example '" + sampleRealmSetting + "'\n" + - "Found invalid config: " + Strings.collectionToDelimitedString(badRealmSettings, ", ") + "\n" + - "Please see the breaking changes documentation." + throw new IllegalArgumentException( + "Incorrect realm settings found. " + + "Realm settings have been changed to include the type as part of the setting key.\n" + + "For example '" + + sampleRealmSetting + + "'\n" + + "Found invalid config: " + + Strings.collectionToDelimitedString(badRealmSettings, ", ") + + "\n" + + "Please see the breaking changes documentation." ); } } @@ -1109,20 +1316,32 @@ static void validateForFips(Settings settings) { Settings keystoreTypeSettings = settings.filter(k -> k.endsWith("keystore.type")) .filter(k -> settings.get(k).equalsIgnoreCase("jks")); if (keystoreTypeSettings.isEmpty() == false) { - validationErrors.add("JKS Keystores cannot be used in a FIPS 140 compliant JVM. Please " + - "revisit [" + keystoreTypeSettings.toDelimitedString(',') + "] settings"); + validationErrors.add( + "JKS Keystores cannot be used in a FIPS 140 compliant JVM. Please " + + "revisit [" + + keystoreTypeSettings.toDelimitedString(',') + + "] settings" + ); } Settings keystorePathSettings = settings.filter(k -> k.endsWith("keystore.path")) .filter(k -> settings.hasValue(k.replace(".path", ".type")) == false) .filter(k -> KeyStoreUtil.inferKeyStoreType(settings.get(k)).equals("jks")); if (keystorePathSettings.isEmpty() == false) { - validationErrors.add("JKS Keystores cannot be used in a FIPS 140 compliant JVM. Please " + - "revisit [" + keystorePathSettings.toDelimitedString(',') + "] settings"); + validationErrors.add( + "JKS Keystores cannot be used in a FIPS 140 compliant JVM. Please " + + "revisit [" + + keystorePathSettings.toDelimitedString(',') + + "] settings" + ); } final String selectedAlgorithm = XPackSettings.PASSWORD_HASHING_ALGORITHM.get(settings); if (selectedAlgorithm.toLowerCase(Locale.ROOT).startsWith("pbkdf2") == false) { - validationErrors.add("Only PBKDF2 is allowed for password hashing in a FIPS 140 JVM. Please set the " + - "appropriate value for [ " + XPackSettings.PASSWORD_HASHING_ALGORITHM.getKey() + " ] setting."); + validationErrors.add( + "Only PBKDF2 is allowed for password hashing in a FIPS 140 JVM. Please set the " + + "appropriate value for [ " + + XPackSettings.PASSWORD_HASHING_ALGORITHM.getKey() + + " ] setting." + ); } if (validationErrors.isEmpty() == false) { @@ -1141,11 +1360,14 @@ public List getTransportInterceptors(NamedWriteableRegistr if (enabled == false) { // don't register anything if we are not enabled return Collections.emptyList(); } - return Collections.singletonList(new TransportInterceptor() { + return Collections.singletonList(new TransportInterceptor() { @Override - public TransportRequestHandler interceptHandler(String action, String executor, - boolean forceExecution, - TransportRequestHandler actualHandler) { + public TransportRequestHandler interceptHandler( + String action, + String executor, + boolean forceExecution, + TransportRequestHandler actualHandler + ) { assert securityInterceptor.get() != null; return securityInterceptor.get().interceptHandler(action, executor, forceExecution, actualHandler); } @@ -1159,19 +1381,25 @@ public AsyncSender interceptSender(AsyncSender sender) { } @Override - public Map> getTransports(Settings settings, ThreadPool threadPool, PageCacheRecycler pageCacheRecycler, - CircuitBreakerService circuitBreakerService, - NamedWriteableRegistry namedWriteableRegistry, NetworkService networkService) { + public Map> getTransports( + Settings settings, + ThreadPool threadPool, + PageCacheRecycler pageCacheRecycler, + CircuitBreakerService circuitBreakerService, + NamedWriteableRegistry namedWriteableRegistry, + NetworkService networkService + ) { if (enabled == false) { // don't register anything if we are not enabled return Collections.emptyMap(); } IPFilter ipFilter = this.ipFilter.get(); return Map.of( - // security based on Netty 4 - SecurityField.NAME4, - () -> { - transportReference.set(new SecurityNetty4ServerTransport( + // security based on Netty 4 + SecurityField.NAME4, + () -> { + transportReference.set( + new SecurityNetty4ServerTransport( settings, Version.CURRENT, threadPool, @@ -1181,13 +1409,17 @@ public Map> getTransports(Settings settings, ThreadP circuitBreakerService, ipFilter, getSslService(), - getNettySharedGroupFactory(settings))); - return transportReference.get(); - }, - // security based on NIO - SecurityField.NIO, - () -> { - transportReference.set(new SecurityNioTransport(settings, + getNettySharedGroupFactory(settings) + ) + ); + return transportReference.get(); + }, + // security based on NIO + SecurityField.NIO, + () -> { + transportReference.set( + new SecurityNioTransport( + settings, Version.CURRENT, threadPool, networkService, @@ -1196,30 +1428,62 @@ public Map> getTransports(Settings settings, ThreadP circuitBreakerService, ipFilter, getSslService(), - getNioGroupFactory(settings))); - return transportReference.get(); - }); + getNioGroupFactory(settings) + ) + ); + return transportReference.get(); + } + ); } @Override - public Map> getHttpTransports(Settings settings, ThreadPool threadPool, BigArrays bigArrays, - PageCacheRecycler pageCacheRecycler, - CircuitBreakerService circuitBreakerService, - NamedXContentRegistry xContentRegistry, - NetworkService networkService, - HttpServerTransport.Dispatcher dispatcher, - ClusterSettings clusterSettings) { + public Map> getHttpTransports( + Settings settings, + ThreadPool threadPool, + BigArrays bigArrays, + PageCacheRecycler pageCacheRecycler, + CircuitBreakerService circuitBreakerService, + NamedXContentRegistry xContentRegistry, + NetworkService networkService, + HttpServerTransport.Dispatcher dispatcher, + ClusterSettings clusterSettings + ) { if (enabled == false) { // don't register anything if we are not enabled return Collections.emptyMap(); } Map> httpTransports = new HashMap<>(); - httpTransports.put(SecurityField.NAME4, () -> new SecurityNetty4HttpServerTransport(settings, networkService, bigArrays, - ipFilter.get(), getSslService(), threadPool, xContentRegistry, dispatcher, clusterSettings, - getNettySharedGroupFactory(settings))); - httpTransports.put(SecurityField.NIO, () -> new SecurityNioHttpServerTransport(settings, networkService, bigArrays, - pageCacheRecycler, threadPool, xContentRegistry, dispatcher, ipFilter.get(), getSslService(), getNioGroupFactory(settings), - clusterSettings)); + httpTransports.put( + SecurityField.NAME4, + () -> new SecurityNetty4HttpServerTransport( + settings, + networkService, + bigArrays, + ipFilter.get(), + getSslService(), + threadPool, + xContentRegistry, + dispatcher, + clusterSettings, + getNettySharedGroupFactory(settings) + ) + ); + httpTransports.put( + SecurityField.NIO, + () -> new SecurityNioHttpServerTransport( + settings, + networkService, + bigArrays, + pageCacheRecycler, + threadPool, + xContentRegistry, + dispatcher, + ipFilter.get(), + getSslService(), + getNioGroupFactory(settings), + clusterSettings + ) + ); return httpTransports; } @@ -1233,8 +1497,14 @@ public UnaryOperator getRestHandlerWrapper(ThreadContext threadCont } else { extractClientCertificate = false; } - return handler -> new SecurityRestFilter(settings, threadContext, authcService.get(), secondayAuthc.get(), - handler, extractClientCertificate); + return handler -> new SecurityRestFilter( + settings, + threadContext, + authcService.get(), + secondayAuthc.get(), + handler, + extractClientCertificate + ); } @Override @@ -1242,11 +1512,15 @@ public List> getExecutorBuilders(final Settings settings) { if (enabled) { final int allocatedProcessors = EsExecutors.allocatedProcessors(settings); return List.of( - new FixedExecutorBuilder(settings, TokenService.THREAD_POOL_NAME, 1, 1000, - "xpack.security.authc.token.thread_pool", false), - new FixedExecutorBuilder(settings, SECURITY_CRYPTO_THREAD_POOL_NAME, - (allocatedProcessors + 1) / 2, 1000, - "xpack.security.crypto.thread_pool", false) + new FixedExecutorBuilder(settings, TokenService.THREAD_POOL_NAME, 1, 1000, "xpack.security.authc.token.thread_pool", false), + new FixedExecutorBuilder( + settings, + SECURITY_CRYPTO_THREAD_POOL_NAME, + (allocatedProcessors + 1) / 2, + 1000, + "xpack.security.crypto.thread_pool", + false + ) ); } return Collections.emptyList(); @@ -1266,8 +1540,8 @@ public Function> getFieldFilter() { if (enabled) { return index -> { XPackLicenseState licenseState = getLicenseState(); - IndicesAccessControl indicesAccessControl = threadContext.get().getTransient( - AuthorizationServiceField.INDICES_PERMISSIONS_KEY); + IndicesAccessControl indicesAccessControl = threadContext.get() + .getTransient(AuthorizationServiceField.INDICES_PERMISSIONS_KEY); if (indicesAccessControl == null) { return MapperPlugin.NOOP_FIELD_PREDICATE; } @@ -1311,10 +1585,12 @@ static final class ValidateLicenseForFIPS implements BiConsumer getSystemIndexDescriptors(Settings settings) { @@ -1780,7 +2057,9 @@ private static XContentBuilder getIndexMappings() { } catch (IOException e) { logger.fatal("Failed to build " + RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7 + " index mappings", e); throw new UncheckedIOException( - "Failed to build " + RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7 + " index mappings", e); + "Failed to build " + RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7 + " index mappings", + e + ); } } @@ -1795,7 +2074,6 @@ private static Settings getTokenIndexSettings() { .build(); } - private static XContentBuilder getTokenIndexMappings() { try { final XContentBuilder builder = jsonBuilder(); @@ -1941,7 +2219,9 @@ private static XContentBuilder getTokenIndexMappings() { return builder; } catch (IOException e) { throw new UncheckedIOException( - "Failed to build " + RestrictedIndicesNames.INTERNAL_SECURITY_TOKENS_INDEX_7 + " index mappings", e); + "Failed to build " + RestrictedIndicesNames.INTERNAL_SECURITY_TOKENS_INDEX_7 + " index mappings", + e + ); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/SecurityUsageTransportAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/SecurityUsageTransportAction.java index 94d6a3ec0b332..9c30fd9941c10 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/SecurityUsageTransportAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/SecurityUsageTransportAction.java @@ -11,10 +11,10 @@ import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.CountDown; +import org.elasticsearch.core.Nullable; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.protocol.xpack.XPackUsageRequest; import org.elasticsearch.tasks.Task; @@ -56,11 +56,24 @@ public class SecurityUsageTransportAction extends XPackUsageFeatureTransportActi private final IPFilter ipFilter; @Inject - public SecurityUsageTransportAction(TransportService transportService, ClusterService clusterService, ThreadPool threadPool, - ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver, - Settings settings, XPackLicenseState licenseState, SecurityUsageServices securityServices) { - super(XPackUsageFeatureAction.SECURITY.name(), transportService, clusterService, threadPool, - actionFilters, indexNameExpressionResolver); + public SecurityUsageTransportAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver, + Settings settings, + XPackLicenseState licenseState, + SecurityUsageServices securityServices + ) { + super( + XPackUsageFeatureAction.SECURITY.name(), + transportService, + clusterService, + threadPool, + actionFilters, + indexNameExpressionResolver + ); this.settings = settings; this.licenseState = licenseState; this.realms = securityServices.realms; @@ -70,8 +83,12 @@ public SecurityUsageTransportAction(TransportService transportService, ClusterSe } @Override - protected void masterOperation(Task task, XPackUsageRequest request, ClusterState state, - ActionListener listener) { + protected void masterOperation( + Task task, + XPackUsageRequest request, + ClusterState state, + ActionListener listener + ) { Map sslUsage = sslUsage(settings); Map tokenServiceUsage = tokenServiceUsage(settings); Map apiKeyServiceUsage = apiKeyServiceUsage(settings); @@ -80,8 +97,10 @@ protected void masterOperation(Task task, XPackUsageRequest request, ClusterStat Map anonymousUsage = singletonMap("enabled", AnonymousUser.isAnonymousEnabled(settings)); Map fips140Usage = fips140Usage(settings); Map operatorPrivilegesUsage = Map.of( - "available", Security.OPERATOR_PRIVILEGES_FEATURE.checkWithoutTracking(licenseState), - "enabled", OperatorPrivileges.OPERATOR_PRIVILEGES_ENABLED.get(settings) + "available", + Security.OPERATOR_PRIVILEGES_FEATURE.checkWithoutTracking(licenseState), + "enabled", + OperatorPrivileges.OPERATOR_PRIVILEGES_ENABLED.get(settings) ); final AtomicReference> rolesUsageRef = new AtomicReference<>(); @@ -92,31 +111,39 @@ protected void masterOperation(Task task, XPackUsageRequest request, ClusterStat final CountDown countDown = new CountDown(3); final Runnable doCountDown = () -> { if (countDown.countDown()) { - var usage = new SecurityFeatureSetUsage(enabled, - realmsUsageRef.get(), rolesUsageRef.get(), roleMappingUsageRef.get(), sslUsage, auditUsage, - ipFilterUsage, anonymousUsage, tokenServiceUsage, apiKeyServiceUsage, fips140Usage, operatorPrivilegesUsage); + var usage = new SecurityFeatureSetUsage( + enabled, + realmsUsageRef.get(), + rolesUsageRef.get(), + roleMappingUsageRef.get(), + sslUsage, + auditUsage, + ipFilterUsage, + anonymousUsage, + tokenServiceUsage, + apiKeyServiceUsage, + fips140Usage, + operatorPrivilegesUsage + ); listener.onResponse(new XPackUsageFeatureResponse(usage)); } }; - final ActionListener> rolesStoreUsageListener = - ActionListener.wrap(rolesStoreUsage -> { - rolesUsageRef.set(rolesStoreUsage); - doCountDown.run(); - }, listener::onFailure); - - final ActionListener> roleMappingStoreUsageListener = - ActionListener.wrap(nativeRoleMappingStoreUsage -> { - Map usage = singletonMap("native", nativeRoleMappingStoreUsage); - roleMappingUsageRef.set(usage); - doCountDown.run(); - }, listener::onFailure); - - final ActionListener> realmsUsageListener = - ActionListener.wrap(realmsUsage -> { - realmsUsageRef.set(realmsUsage); - doCountDown.run(); - }, listener::onFailure); + final ActionListener> rolesStoreUsageListener = ActionListener.wrap(rolesStoreUsage -> { + rolesUsageRef.set(rolesStoreUsage); + doCountDown.run(); + }, listener::onFailure); + + final ActionListener> roleMappingStoreUsageListener = ActionListener.wrap(nativeRoleMappingStoreUsage -> { + Map usage = singletonMap("native", nativeRoleMappingStoreUsage); + roleMappingUsageRef.set(usage); + doCountDown.run(); + }, listener::onFailure); + + final ActionListener> realmsUsageListener = ActionListener.wrap(realmsUsage -> { + realmsUsageRef.set(realmsUsage); + doCountDown.run(); + }, listener::onFailure); if (rolesStore == null || enabled == false) { rolesStoreUsageListener.onResponse(Collections.emptyMap()); @@ -138,7 +165,7 @@ protected void masterOperation(Task task, XPackUsageRequest request, ClusterStat static Map sslUsage(Settings settings) { // If security has been explicitly disabled in the settings, then SSL is also explicitly disabled, and we don't want to report - // these http/transport settings as they would be misleading (they could report `true` even though they were ignored) + // these http/transport settings as they would be misleading (they could report `true` even though they were ignored) if (XPackSettings.SECURITY_ENABLED.get(settings)) { Map map = new HashMap<>(2); map.put("http", singletonMap("enabled", HTTP_SSL_ENABLED.get(settings))); diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/TokenSSLBootstrapCheck.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/TokenSSLBootstrapCheck.java index ef92dd3fad94a..1c2fbb3df425b 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/TokenSSLBootstrapCheck.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/TokenSSLBootstrapCheck.java @@ -23,11 +23,12 @@ public BootstrapCheckResult check(BootstrapContext context) { final Boolean tokenServiceEnabled = XPackSettings.TOKEN_SERVICE_ENABLED_SETTING.get(context.settings()); if (httpsEnabled == false && tokenServiceEnabled) { final String message = String.format( - Locale.ROOT, - "HTTPS is required in order to use the token service; " - + "please enable HTTPS using the [%s] setting or disable the token service using the [%s] setting", - XPackSettings.HTTP_SSL_ENABLED.getKey(), - XPackSettings.TOKEN_SERVICE_ENABLED_SETTING.getKey()); + Locale.ROOT, + "HTTPS is required in order to use the token service; " + + "please enable HTTPS using the [%s] setting or disable the token service using the [%s] setting", + XPackSettings.HTTP_SSL_ENABLED.getKey(), + XPackSettings.TOKEN_SERVICE_ENABLED_SETTING.getKey() + ); return BootstrapCheckResult.failure(message); } else { return BootstrapCheckResult.success(); diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/SecurityActionMapper.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/SecurityActionMapper.java index dd7ae4d4fa4e0..1a067add8077a 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/SecurityActionMapper.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/SecurityActionMapper.java @@ -29,7 +29,7 @@ public String action(String action, TransportRequest request) { switch (action) { case ClearScrollAction.NAME: assert request instanceof ClearScrollRequest; - boolean isClearAllScrollRequest = ((ClearScrollRequest) request).scrollIds().contains("_all"); + boolean isClearAllScrollRequest = ((ClearScrollRequest) request).scrollIds().contains("_all"); if (isClearAllScrollRequest) { return CLUSTER_PERMISSION_SCROLL_CLEAR_ALL_NAME; } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/TransportClearSecurityCacheAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/TransportClearSecurityCacheAction.java index c62bbd9589d7f..44480e5fee8dd 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/TransportClearSecurityCacheAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/TransportClearSecurityCacheAction.java @@ -29,8 +29,11 @@ * Clears a security cache by name (with optional keys). * @see CacheInvalidatorRegistry */ -public class TransportClearSecurityCacheAction extends TransportNodesAction { +public class TransportClearSecurityCacheAction extends TransportNodesAction< + ClearSecurityCacheRequest, + ClearSecurityCacheResponse, + ClearSecurityCacheRequest.Node, + ClearSecurityCacheResponse.Node> { private final CacheInvalidatorRegistry cacheInvalidatorRegistry; @@ -40,7 +43,8 @@ public TransportClearSecurityCacheAction( ClusterService clusterService, TransportService transportService, ActionFilters actionFilters, - CacheInvalidatorRegistry cacheInvalidatorRegistry) { + CacheInvalidatorRegistry cacheInvalidatorRegistry + ) { super( ClearSecurityCacheAction.NAME, threadPool, @@ -50,13 +54,17 @@ public TransportClearSecurityCacheAction( ClearSecurityCacheRequest::new, ClearSecurityCacheRequest.Node::new, ThreadPool.Names.MANAGEMENT, - ClearSecurityCacheResponse.Node.class); + ClearSecurityCacheResponse.Node.class + ); this.cacheInvalidatorRegistry = cacheInvalidatorRegistry; } @Override protected ClearSecurityCacheResponse newResponse( - ClearSecurityCacheRequest request, List nodes, List failures) { + ClearSecurityCacheRequest request, + List nodes, + List failures + ) { return new ClearSecurityCacheResponse(clusterService.getClusterName(), nodes, failures); } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/TransportCreateApiKeyAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/TransportCreateApiKeyAction.java index 4475153d32207..0a17d4e349dd7 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/TransportCreateApiKeyAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/TransportCreateApiKeyAction.java @@ -11,9 +11,9 @@ import org.elasticsearch.action.support.ActionFilters; import org.elasticsearch.action.support.HandledTransportAction; import org.elasticsearch.common.inject.Inject; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.tasks.Task; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.security.SecurityContext; import org.elasticsearch.xpack.core.security.action.CreateApiKeyAction; import org.elasticsearch.xpack.core.security.action.CreateApiKeyRequest; @@ -33,8 +33,14 @@ public final class TransportCreateApiKeyAction extends HandledTransportActiontrusted * to have performed the TLS authentication, and this API translates that authentication into an Elasticsearch access token. */ -public final class TransportDelegatePkiAuthenticationAction - extends HandledTransportAction { +public final class TransportDelegatePkiAuthenticationAction extends HandledTransportAction< + DelegatePkiAuthenticationRequest, + DelegatePkiAuthenticationResponse> { private static final Logger logger = LogManager.getLogger(TransportDelegatePkiAuthenticationAction.class); @@ -56,9 +57,14 @@ public final class TransportDelegatePkiAuthenticationAction private final SecurityContext securityContext; @Inject - public TransportDelegatePkiAuthenticationAction(ThreadPool threadPool, TransportService transportService, ActionFilters actionFilters, - AuthenticationService authenticationService, TokenService tokenService, - SecurityContext securityContext) { + public TransportDelegatePkiAuthenticationAction( + ThreadPool threadPool, + TransportService transportService, + ActionFilters actionFilters, + AuthenticationService authenticationService, + TokenService tokenService, + SecurityContext securityContext + ) { super(DelegatePkiAuthenticationAction.NAME, transportService, actionFilters, DelegatePkiAuthenticationRequest::new); this.threadPool = threadPool; this.authenticationService = authenticationService; @@ -67,32 +73,52 @@ public TransportDelegatePkiAuthenticationAction(ThreadPool threadPool, Transport } @Override - protected void doExecute(Task task, DelegatePkiAuthenticationRequest request, - ActionListener listener) { + protected void doExecute( + Task task, + DelegatePkiAuthenticationRequest request, + ActionListener listener + ) { final ThreadContext threadContext = threadPool.getThreadContext(); Authentication delegateeAuthentication = securityContext.getAuthentication(); if (delegateeAuthentication == null) { listener.onFailure(new IllegalStateException("Delegatee authentication cannot be null")); return; } - final X509AuthenticationToken x509DelegatedToken = X509AuthenticationToken - .delegated(request.getCertificateChain().toArray(new X509Certificate[0]), delegateeAuthentication); + final X509AuthenticationToken x509DelegatedToken = X509AuthenticationToken.delegated( + request.getCertificateChain().toArray(new X509Certificate[0]), + delegateeAuthentication + ); logger.trace("Attempting to authenticate delegated x509Token [{}]", x509DelegatedToken); try (ThreadContext.StoredContext ignore = threadContext.stashContext()) { - authenticationService.authenticate(DelegatePkiAuthenticationAction.NAME, request, x509DelegatedToken, - ActionListener.wrap(authentication -> { - assert authentication != null : "authentication should never be null at this point"; - tokenService.createOAuth2Tokens(authentication, delegateeAuthentication, Map.of(), false, - ActionListener.wrap(tokenResult -> { - final TimeValue expiresIn = tokenService.getExpirationDelay(); - listener.onResponse(new DelegatePkiAuthenticationResponse(tokenResult.getAccessToken(), expiresIn, - authentication)); - }, listener::onFailure)); - }, e -> { - logger.debug((Supplier) () -> new ParameterizedMessage("Delegated x509Token [{}] could not be authenticated", - x509DelegatedToken), e); - listener.onFailure(e); - })); + authenticationService.authenticate( + DelegatePkiAuthenticationAction.NAME, + request, + x509DelegatedToken, + ActionListener.wrap(authentication -> { + assert authentication != null : "authentication should never be null at this point"; + tokenService.createOAuth2Tokens( + authentication, + delegateeAuthentication, + Map.of(), + false, + ActionListener.wrap(tokenResult -> { + final TimeValue expiresIn = tokenService.getExpirationDelay(); + listener.onResponse( + new DelegatePkiAuthenticationResponse(tokenResult.getAccessToken(), expiresIn, authentication) + ); + }, listener::onFailure) + ); + }, e -> { + logger.debug( + (Supplier) () -> new ParameterizedMessage( + "Delegated x509Token [{}] could not be authenticated", + x509DelegatedToken + ), + e + ); + listener.onFailure(e); + }) + ); } } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/TransportGetApiKeyAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/TransportGetApiKeyAction.java index 4c8e4c4f6eca1..e47fcac23afc5 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/TransportGetApiKeyAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/TransportGetApiKeyAction.java @@ -21,16 +21,19 @@ import org.elasticsearch.xpack.core.security.authc.Authentication; import org.elasticsearch.xpack.security.authc.ApiKeyService; -public final class TransportGetApiKeyAction extends HandledTransportAction { +public final class TransportGetApiKeyAction extends HandledTransportAction { private final ApiKeyService apiKeyService; private final SecurityContext securityContext; @Inject - public TransportGetApiKeyAction(TransportService transportService, ActionFilters actionFilters, ApiKeyService apiKeyService, - SecurityContext context) { - super(GetApiKeyAction.NAME, transportService, actionFilters, - (Writeable.Reader) GetApiKeyRequest::new); + public TransportGetApiKeyAction( + TransportService transportService, + ActionFilters actionFilters, + ApiKeyService apiKeyService, + SecurityContext context + ) { + super(GetApiKeyAction.NAME, transportService, actionFilters, (Writeable.Reader) GetApiKeyRequest::new); this.apiKeyService = apiKeyService; this.securityContext = context; } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/TransportGrantApiKeyAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/TransportGrantApiKeyAction.java index c7c14f06b0174..e44ed3ca119e2 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/TransportGrantApiKeyAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/TransportGrantApiKeyAction.java @@ -13,11 +13,11 @@ import org.elasticsearch.action.support.HandledTransportAction; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.tasks.Task; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportRequest; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.security.action.CreateApiKeyResponse; import org.elasticsearch.xpack.core.security.action.GrantApiKeyAction; import org.elasticsearch.xpack.core.security.action.GrantApiKeyRequest; @@ -40,17 +40,35 @@ public final class TransportGrantApiKeyAction extends HandledTransportAction listener) { try (ThreadContext.StoredContext ignore = threadContext.stashContext()) { - resolveAuthentication(request.getGrant(), request, ActionListener.wrap( - authentication -> generator.generateApiKey(authentication, request.getApiKeyRequest(), listener), - listener::onFailure - )); + resolveAuthentication( + request.getGrant(), + request, + ActionListener.wrap( + authentication -> generator.generateApiKey(authentication, request.getApiKeyRequest(), listener), + listener::onFailure + ) + ); } catch (Exception e) { listener.onFailure(e); } } - private void resolveAuthentication(GrantApiKeyRequest.Grant grant, TransportRequest transportRequest, - ActionListener listener) { + private void resolveAuthentication( + GrantApiKeyRequest.Grant grant, + TransportRequest transportRequest, + ActionListener listener + ) { switch (grant.getType()) { case GrantApiKeyRequest.PASSWORD_GRANT_TYPE: final UsernamePasswordToken token = new UsernamePasswordToken(grant.getUsername(), grant.getPassword()); @@ -86,5 +111,4 @@ private void resolveAuthentication(GrantApiKeyRequest.Grant grant, TransportRequ } } - } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/TransportInvalidateApiKeyAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/TransportInvalidateApiKeyAction.java index aa6356368bee1..462cac914c827 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/TransportInvalidateApiKeyAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/TransportInvalidateApiKeyAction.java @@ -27,10 +27,18 @@ public final class TransportInvalidateApiKeyAction extends HandledTransportActio private final SecurityContext securityContext; @Inject - public TransportInvalidateApiKeyAction(TransportService transportService, ActionFilters actionFilters, ApiKeyService apiKeyService, - SecurityContext context) { - super(InvalidateApiKeyAction.NAME, transportService, actionFilters, - (Writeable.Reader) InvalidateApiKeyRequest::new); + public TransportInvalidateApiKeyAction( + TransportService transportService, + ActionFilters actionFilters, + ApiKeyService apiKeyService, + SecurityContext context + ) { + super( + InvalidateApiKeyAction.NAME, + transportService, + actionFilters, + (Writeable.Reader) InvalidateApiKeyRequest::new + ); this.apiKeyService = apiKeyService; this.securityContext = context; } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/apikey/TransportQueryApiKeyAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/apikey/TransportQueryApiKeyAction.java index a240504b9d602..51c50c2618f73 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/apikey/TransportQueryApiKeyAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/apikey/TransportQueryApiKeyAction.java @@ -35,8 +35,12 @@ public final class TransportQueryApiKeyAction extends HandledTransportAction fieldSortBuilders, if (translatedFieldName.equals(fieldSortBuilder.getFieldName())) { searchSourceBuilder.sort(fieldSortBuilder); } else { - final FieldSortBuilder translatedFieldSortBuilder = - new FieldSortBuilder(translatedFieldName) - .order(fieldSortBuilder.order()) - .missing(fieldSortBuilder.missing()) - .unmappedType(fieldSortBuilder.unmappedType()) - .setFormat(fieldSortBuilder.getFormat()); + final FieldSortBuilder translatedFieldSortBuilder = new FieldSortBuilder(translatedFieldName).order( + fieldSortBuilder.order() + ) + .missing(fieldSortBuilder.missing()) + .unmappedType(fieldSortBuilder.unmappedType()) + .setFormat(fieldSortBuilder.getFormat()); if (fieldSortBuilder.sortMode() != null) { translatedFieldSortBuilder.sortMode(fieldSortBuilder.sortMode()); diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/enrollment/TransportKibanaEnrollmentAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/enrollment/TransportKibanaEnrollmentAction.java index aee05ee32bf05..074d25622e6c9 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/enrollment/TransportKibanaEnrollmentAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/enrollment/TransportKibanaEnrollmentAction.java @@ -28,8 +28,6 @@ import org.elasticsearch.xpack.core.security.action.service.CreateServiceAccountTokenRequest; import org.elasticsearch.xpack.core.ssl.SSLService; -import static org.elasticsearch.xpack.core.ClientHelper.SECURITY_ORIGIN; - import java.security.cert.CertificateEncodingException; import java.security.cert.X509Certificate; import java.time.ZoneOffset; @@ -38,6 +36,8 @@ import java.util.List; import java.util.stream.Collectors; +import static org.elasticsearch.xpack.core.ClientHelper.SECURITY_ORIGIN; + public class TransportKibanaEnrollmentAction extends HandledTransportAction { private static final Logger logger = LogManager.getLogger(TransportKibanaEnrollmentAction.class); @@ -62,8 +62,11 @@ protected void doExecute(Task task, KibanaEnrollmentRequest request, ActionListe final SslKeyConfig keyConfig = sslService.getHttpTransportSSLConfiguration().getKeyConfig(); if (keyConfig instanceof StoreKeyConfig == false) { - listener.onFailure(new ElasticsearchException( - "Unable to enroll kibana instance. Elasticsearch node HTTP layer SSL configuration is not configured with a keystore")); + listener.onFailure( + new ElasticsearchException( + "Unable to enroll kibana instance. Elasticsearch node HTTP layer SSL configuration is not configured with a keystore" + ) + ); return; } List caCertificates; @@ -74,37 +77,51 @@ protected void doExecute(Task task, KibanaEnrollmentRequest request, ActionListe .filter(x509Certificate -> x509Certificate.getBasicConstraints() != -1) .collect(Collectors.toList()); } catch (Exception e) { - listener.onFailure(new ElasticsearchException("Unable to enroll kibana instance. Cannot retrieve CA certificate " + - "for the HTTP layer of the Elasticsearch node.", e)); + listener.onFailure( + new ElasticsearchException( + "Unable to enroll kibana instance. Cannot retrieve CA certificate " + "for the HTTP layer of the Elasticsearch node.", + e + ) + ); return; } if (caCertificates.size() != 1) { - listener.onFailure(new ElasticsearchException( - "Unable to enroll kibana instance. Elasticsearch node HTTP layer SSL configuration Keystore " + - "[xpack.security.http.ssl.keystore] doesn't contain a single PrivateKey entry where the associated " + - "certificate is a CA certificate")); + listener.onFailure( + new ElasticsearchException( + "Unable to enroll kibana instance. Elasticsearch node HTTP layer SSL configuration Keystore " + + "[xpack.security.http.ssl.keystore] doesn't contain a single PrivateKey entry where the associated " + + "certificate is a CA certificate" + ) + ); } else { String httpCa; try { httpCa = Base64.getEncoder().encodeToString(caCertificates.get(0).getEncoded()); } catch (CertificateEncodingException cee) { - listener.onFailure(new ElasticsearchException( - "Unable to enroll kibana instance. Elasticsearch node HTTP layer SSL configuration uses a malformed CA certificate", - cee)); + listener.onFailure( + new ElasticsearchException( + "Unable to enroll kibana instance. Elasticsearch node HTTP layer SSL configuration uses a malformed CA certificate", + cee + ) + ); return; } - final CreateServiceAccountTokenRequest createServiceAccountTokenRequest = - new CreateServiceAccountTokenRequest("elastic", "kibana", getTokenName()); + final CreateServiceAccountTokenRequest createServiceAccountTokenRequest = new CreateServiceAccountTokenRequest( + "elastic", + "kibana", + getTokenName() + ); client.execute(CreateServiceAccountTokenAction.INSTANCE, createServiceAccountTokenRequest, ActionListener.wrap(response -> { - logger.debug("Successfully created token [{}] for the [elastic/kibana] service account during kibana enrollment", - response.getName()); + logger.debug( + "Successfully created token [{}] for the [elastic/kibana] service account during kibana enrollment", + response.getName() + ); listener.onResponse(new KibanaEnrollmentResponse(response.getName(), response.getValue(), httpCa)); - }, e -> listener.onFailure( - new ElasticsearchException("Failed to create token for the [elastic/kibana] service account", e)))); + }, e -> listener.onFailure(new ElasticsearchException("Failed to create token for the [elastic/kibana] service account", e)))); } } - protected static String getTokenName(){ + protected static String getTokenName() { final ZonedDateTime enrollTime = ZonedDateTime.now(ZoneOffset.UTC); final String prefix = "enroll-process-token-"; return prefix + enrollTime.toInstant().toEpochMilli(); diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/enrollment/TransportNodeEnrollmentAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/enrollment/TransportNodeEnrollmentAction.java index ec4fb27486279..e2b21573279c5 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/enrollment/TransportNodeEnrollmentAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/enrollment/TransportNodeEnrollmentAction.java @@ -15,11 +15,11 @@ import org.elasticsearch.action.support.ActionFilters; import org.elasticsearch.action.support.HandledTransportAction; import org.elasticsearch.client.Client; -import org.elasticsearch.common.ssl.StoredCertificate; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.ssl.SslKeyConfig; import org.elasticsearch.common.ssl.StoreKeyConfig; +import org.elasticsearch.common.ssl.StoredCertificate; +import org.elasticsearch.core.Tuple; import org.elasticsearch.tasks.Task; import org.elasticsearch.transport.TransportInfo; import org.elasticsearch.transport.TransportService; @@ -44,8 +44,12 @@ public class TransportNodeEnrollmentAction extends HandledTransportAction> transportKeysAndCertificates = transportKeyConfig.getKeys(); final List> httpCaKeysAndCertificates = httpKeyConfig.getKeys() .stream() - .filter(t -> t.v2().getBasicConstraints() != -1) + .filter(t -> t.v2().getBasicConstraints() != -1) .collect(Collectors.toUnmodifiableList()); if (transportKeysAndCertificates.isEmpty()) { - listener.onFailure(new IllegalStateException( - "Unable to enroll node. Elasticsearch node transport layer SSL configuration doesn't contain any keys")); + listener.onFailure( + new IllegalStateException( + "Unable to enroll node. Elasticsearch node transport layer SSL configuration doesn't contain any keys" + ) + ); return; } else if (transportKeysAndCertificates.size() > 1) { - listener.onFailure(new IllegalStateException( - "Unable to enroll node. Elasticsearch node transport layer SSL configuration contains multiple keys")); + listener.onFailure( + new IllegalStateException( + "Unable to enroll node. Elasticsearch node transport layer SSL configuration contains multiple keys" + ) + ); return; } final List transportCaCertificates; @@ -90,53 +106,59 @@ protected void doExecute(Task task, NodeEnrollmentRequest request, ActionListene .filter(x509Certificate -> x509Certificate.getBasicConstraints() != -1) .collect(Collectors.toList()); } catch (Exception e) { - listener.onFailure(new ElasticsearchException("Unable to enroll node. Cannot retrieve CA certificate " + - "for the transport layer of the Elasticsearch node.", e)); + listener.onFailure( + new ElasticsearchException( + "Unable to enroll node. Cannot retrieve CA certificate " + "for the transport layer of the Elasticsearch node.", + e + ) + ); return; } if (transportCaCertificates.size() != 1) { - listener.onFailure(new ElasticsearchException( - "Unable to enroll Elasticsearch node. Elasticsearch node transport layer SSL configuration Keystore " + - "[xpack.security.transport.ssl.keystore] doesn't contain a single CA certificate")); + listener.onFailure( + new ElasticsearchException( + "Unable to enroll Elasticsearch node. Elasticsearch node transport layer SSL configuration Keystore " + + "[xpack.security.transport.ssl.keystore] doesn't contain a single CA certificate" + ) + ); } if (httpCaKeysAndCertificates.isEmpty()) { - listener.onFailure(new IllegalStateException( - "Unable to enroll node. Elasticsearch node HTTP layer SSL configuration Keystore doesn't contain any " + - "PrivateKey entries where the associated certificate is a CA certificate")); + listener.onFailure( + new IllegalStateException( + "Unable to enroll node. Elasticsearch node HTTP layer SSL configuration Keystore doesn't contain any " + + "PrivateKey entries where the associated certificate is a CA certificate" + ) + ); return; } else if (httpCaKeysAndCertificates.size() > 1) { - listener.onFailure(new IllegalStateException( - "Unable to enroll node. Elasticsearch node HTTP layer SSL configuration Keystore contain multiple " + - "PrivateKey entries where the associated certificate is a CA certificate")); + listener.onFailure( + new IllegalStateException( + "Unable to enroll node. Elasticsearch node HTTP layer SSL configuration Keystore contain multiple " + + "PrivateKey entries where the associated certificate is a CA certificate" + ) + ); return; } final List nodeList = new ArrayList<>(); final NodesInfoRequest nodesInfoRequest = new NodesInfoRequest().addMetric(NodesInfoRequest.Metric.TRANSPORT.metricName()); - executeAsyncWithOrigin(client, SECURITY_ORIGIN, NodesInfoAction.INSTANCE, nodesInfoRequest, ActionListener.wrap( - response -> { - for (NodeInfo nodeInfo : response.getNodes()) { - nodeList.add(nodeInfo.getInfo(TransportInfo.class).getAddress().publishAddress().toString()); - } - try { - final String httpCaKey = Base64.getEncoder().encodeToString(httpCaKeysAndCertificates.get(0).v1().getEncoded()); - final String httpCaCert = Base64.getEncoder().encodeToString(httpCaKeysAndCertificates.get(0).v2().getEncoded()); - final String transportCaCert = Base64.getEncoder().encodeToString(transportCaCertificates.get(0).getEncoded()); - final String transportKey = - Base64.getEncoder().encodeToString(transportKeysAndCertificates.get(0).v1().getEncoded()); - final String transportCert = - Base64.getEncoder().encodeToString(transportKeysAndCertificates.get(0).v2().getEncoded()); - listener.onResponse(new NodeEnrollmentResponse(httpCaKey, - httpCaCert, - transportCaCert, - transportKey, - transportCert, - nodeList)); - } catch (CertificateEncodingException e) { - listener.onFailure(new ElasticsearchException("Unable to enroll node", e)); - } - }, listener::onFailure - )); + executeAsyncWithOrigin(client, SECURITY_ORIGIN, NodesInfoAction.INSTANCE, nodesInfoRequest, ActionListener.wrap(response -> { + for (NodeInfo nodeInfo : response.getNodes()) { + nodeList.add(nodeInfo.getInfo(TransportInfo.class).getAddress().publishAddress().toString()); + } + try { + final String httpCaKey = Base64.getEncoder().encodeToString(httpCaKeysAndCertificates.get(0).v1().getEncoded()); + final String httpCaCert = Base64.getEncoder().encodeToString(httpCaKeysAndCertificates.get(0).v2().getEncoded()); + final String transportCaCert = Base64.getEncoder().encodeToString(transportCaCertificates.get(0).getEncoded()); + final String transportKey = Base64.getEncoder().encodeToString(transportKeysAndCertificates.get(0).v1().getEncoded()); + final String transportCert = Base64.getEncoder().encodeToString(transportKeysAndCertificates.get(0).v2().getEncoded()); + listener.onResponse( + new NodeEnrollmentResponse(httpCaKey, httpCaCert, transportCaCert, transportKey, transportCert, nodeList) + ); + } catch (CertificateEncodingException e) { + listener.onFailure(new ElasticsearchException("Unable to enroll node", e)); + } + }, listener::onFailure)); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/filter/SecurityActionFilter.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/filter/SecurityActionFilter.java index b83fbd44079d9..9032e0c008cef 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/filter/SecurityActionFilter.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/filter/SecurityActionFilter.java @@ -53,9 +53,15 @@ public class SecurityActionFilter implements ActionFilter { private final SecurityContext securityContext; private final DestructiveOperations destructiveOperations; - public SecurityActionFilter(AuthenticationService authcService, AuthorizationService authzService, - AuditTrailService auditTrailService, XPackLicenseState licenseState, ThreadPool threadPool, - SecurityContext securityContext, DestructiveOperations destructiveOperations) { + public SecurityActionFilter( + AuthenticationService authcService, + AuthorizationService authzService, + AuditTrailService auditTrailService, + XPackLicenseState licenseState, + ThreadPool threadPool, + SecurityContext securityContext, + DestructiveOperations destructiveOperations + ) { this.authcService = authcService; this.authzService = authzService; this.auditTrailService = auditTrailService; @@ -66,9 +72,13 @@ public SecurityActionFilter(AuthenticationService authcService, AuthorizationSer } @Override - public void apply(Task task, String action, Request request, - ActionListener listener, - ActionFilterChain chain) { + public void apply( + Task task, + String action, + Request request, + ActionListener listener, + ActionFilterChain chain + ) { /* A functional requirement - when the license of security is disabled (invalid/expires), security will continue to operate normally, except the following read operations will be blocked: @@ -78,24 +88,33 @@ public void app - cluster:monitor/nodes/stats* */ if (licenseState.isActive() == false && LICENSE_EXPIRATION_ACTION_MATCHER.test(action)) { - logger.error("blocking [{}] operation due to expired license. Cluster health, cluster stats and indices stats \n" + - "operations are blocked on license expiration. All data operations (read and write) continue to work. \n" + - "If you have a new license, please update it. Otherwise, please reach out to your support contact.", action); + logger.error( + "blocking [{}] operation due to expired license. Cluster health, cluster stats and indices stats \n" + + "operations are blocked on license expiration. All data operations (read and write) continue to work. \n" + + "If you have a new license, please update it. Otherwise, please reach out to your support contact.", + action + ); throw LicenseUtils.newComplianceException(XPackField.SECURITY); } - final ActionListener contextPreservingListener = - ContextPreservingActionListener.wrapPreservingContext(listener, threadContext); + final ActionListener contextPreservingListener = ContextPreservingActionListener.wrapPreservingContext( + listener, + threadContext + ); final boolean useSystemUser = AuthorizationUtils.shouldReplaceUserWithSystem(threadContext, action); try { if (useSystemUser) { - securityContext.executeAsUser(SystemUser.INSTANCE, (original) -> { - applyInternal(task, chain, action, request, contextPreservingListener); - }, Version.CURRENT); + securityContext.executeAsUser( + SystemUser.INSTANCE, + (original) -> { applyInternal(task, chain, action, request, contextPreservingListener); }, + Version.CURRENT + ); } else if (AuthorizationUtils.shouldSetUserBasedOnActionOrigin(threadContext)) { - AuthorizationUtils.switchUserBasedOnActionOriginAndExecute(threadContext, securityContext, (original) -> { - applyInternal(task, chain, action, request, contextPreservingListener); - }); + AuthorizationUtils.switchUserBasedOnActionOriginAndExecute( + threadContext, + securityContext, + (original) -> { applyInternal(task, chain, action, request, contextPreservingListener); } + ); } else { try (ThreadContext.StoredContext ignore = threadContext.newStoredContext(true)) { applyInternal(task, chain, action, request, contextPreservingListener); @@ -111,8 +130,13 @@ public int order() { return Integer.MIN_VALUE; } - private void applyInternal(Task task, - ActionFilterChain chain, String action, Request request, ActionListener listener) { + private void applyInternal( + Task task, + ActionFilterChain chain, + String action, + Request request, + ActionListener listener + ) { if (CloseIndexAction.NAME.equals(action) || OpenIndexAction.NAME.equals(action) || DeleteIndexAction.NAME.equals(action)) { IndicesRequest indicesRequest = (IndicesRequest) request; try { @@ -134,20 +158,22 @@ it to the action without an associated user (not via REST or transport - this is here if a request is not associated with any other user. */ final String securityAction = actionMapper.action(action, request); - authcService.authenticate(securityAction, request, SystemUser.INSTANCE, - ActionListener.wrap((authc) -> { - if (authc != null) { - final String requestId = AuditUtil.extractRequestId(threadContext); - assert Strings.hasText(requestId); - authzService.authorize(authc, securityAction, request, listener.delegateFailure( - (ll, aVoid) -> chain.proceed(task, action, request, ll.delegateFailure((l, response) -> { - auditTrailService.get().coordinatingActionResponse(requestId, authc, action, request, - response); - l.onResponse(response); - })))); - } else { - listener.onFailure(new IllegalStateException("no authentication present but auth is allowed")); - } - }, listener::onFailure)); + authcService.authenticate(securityAction, request, SystemUser.INSTANCE, ActionListener.wrap((authc) -> { + if (authc != null) { + final String requestId = AuditUtil.extractRequestId(threadContext); + assert Strings.hasText(requestId); + authzService.authorize( + authc, + securityAction, + request, + listener.delegateFailure((ll, aVoid) -> chain.proceed(task, action, request, ll.delegateFailure((l, response) -> { + auditTrailService.get().coordinatingActionResponse(requestId, authc, action, request, response); + l.onResponse(response); + }))) + ); + } else { + listener.onFailure(new IllegalStateException("no authentication present but auth is allowed")); + } + }, listener::onFailure)); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/oidc/TransportOpenIdConnectAuthenticateAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/oidc/TransportOpenIdConnectAuthenticateAction.java index 910c1d5053aa4..39fe95f0a5277 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/oidc/TransportOpenIdConnectAuthenticateAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/oidc/TransportOpenIdConnectAuthenticateAction.java @@ -8,6 +8,7 @@ import com.nimbusds.oauth2.sdk.id.State; import com.nimbusds.openid.connect.sdk.Nonce; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.message.ParameterizedMessage; @@ -16,15 +17,15 @@ import org.elasticsearch.action.support.HandledTransportAction; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.tasks.Task; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; import org.elasticsearch.xpack.core.security.SecurityContext; +import org.elasticsearch.xpack.core.security.action.oidc.OpenIdConnectAuthenticateAction; import org.elasticsearch.xpack.core.security.action.oidc.OpenIdConnectAuthenticateRequest; import org.elasticsearch.xpack.core.security.action.oidc.OpenIdConnectAuthenticateResponse; -import org.elasticsearch.xpack.core.security.action.oidc.OpenIdConnectAuthenticateAction; import org.elasticsearch.xpack.core.security.authc.Authentication; import org.elasticsearch.xpack.core.security.authc.AuthenticationResult; import org.elasticsearch.xpack.core.security.user.User; @@ -35,8 +36,9 @@ import java.util.Map; -public class TransportOpenIdConnectAuthenticateAction - extends HandledTransportAction { +public class TransportOpenIdConnectAuthenticateAction extends HandledTransportAction< + OpenIdConnectAuthenticateRequest, + OpenIdConnectAuthenticateResponse> { private final ThreadPool threadPool; private final AuthenticationService authenticationService; @@ -45,11 +47,20 @@ public class TransportOpenIdConnectAuthenticateAction private static final Logger logger = LogManager.getLogger(TransportOpenIdConnectAuthenticateAction.class); @Inject - public TransportOpenIdConnectAuthenticateAction(ThreadPool threadPool, TransportService transportService, - ActionFilters actionFilters, AuthenticationService authenticationService, - TokenService tokenService, SecurityContext securityContext) { - super(OpenIdConnectAuthenticateAction.NAME, transportService, actionFilters, - (Writeable.Reader) OpenIdConnectAuthenticateRequest::new); + public TransportOpenIdConnectAuthenticateAction( + ThreadPool threadPool, + TransportService transportService, + ActionFilters actionFilters, + AuthenticationService authenticationService, + TokenService tokenService, + SecurityContext securityContext + ) { + super( + OpenIdConnectAuthenticateAction.NAME, + transportService, + actionFilters, + (Writeable.Reader) OpenIdConnectAuthenticateRequest::new + ); this.threadPool = threadPool; this.authenticationService = authenticationService; this.tokenService = tokenService; @@ -57,34 +68,50 @@ public TransportOpenIdConnectAuthenticateAction(ThreadPool threadPool, Transport } @Override - protected void doExecute(Task task, OpenIdConnectAuthenticateRequest request, - ActionListener listener) { - final OpenIdConnectToken token = new OpenIdConnectToken(request.getRedirectUri(), new State(request.getState()), - new Nonce(request.getNonce()), request.getRealm()); + protected void doExecute( + Task task, + OpenIdConnectAuthenticateRequest request, + ActionListener listener + ) { + final OpenIdConnectToken token = new OpenIdConnectToken( + request.getRedirectUri(), + new State(request.getState()), + new Nonce(request.getNonce()), + request.getRealm() + ); final ThreadContext threadContext = threadPool.getThreadContext(); Authentication originatingAuthentication = securityContext.getAuthentication(); try (ThreadContext.StoredContext ignore = threadContext.stashContext()) { - authenticationService.authenticate(OpenIdConnectAuthenticateAction.NAME, request, token, ActionListener.wrap( - authentication -> { - AuthenticationResult result = threadContext.getTransient(AuthenticationResult.THREAD_CONTEXT_KEY); - if (result == null) { - listener.onFailure(new IllegalStateException("Cannot find User AuthenticationResult on thread context")); - return; - } - @SuppressWarnings("unchecked") final Map tokenMetadata = (Map) result.getMetadata() - .get(OpenIdConnectRealm.CONTEXT_TOKEN_DATA); - tokenService.createOAuth2Tokens(authentication, originatingAuthentication, tokenMetadata, true, - ActionListener.wrap(tokenResult -> { - final TimeValue expiresIn = tokenService.getExpirationDelay(); - listener.onResponse(new OpenIdConnectAuthenticateResponse(authentication, tokenResult.getAccessToken(), - tokenResult.getRefreshToken(), expiresIn)); - }, listener::onFailure)); - }, e -> { - logger.debug(() -> new ParameterizedMessage("OpenIDConnectToken [{}] could not be authenticated", token), e); - listener.onFailure(e); + authenticationService.authenticate(OpenIdConnectAuthenticateAction.NAME, request, token, ActionListener.wrap(authentication -> { + AuthenticationResult result = threadContext.getTransient(AuthenticationResult.THREAD_CONTEXT_KEY); + if (result == null) { + listener.onFailure(new IllegalStateException("Cannot find User AuthenticationResult on thread context")); + return; } - )); + @SuppressWarnings("unchecked") + final Map tokenMetadata = (Map) result.getMetadata() + .get(OpenIdConnectRealm.CONTEXT_TOKEN_DATA); + tokenService.createOAuth2Tokens( + authentication, + originatingAuthentication, + tokenMetadata, + true, + ActionListener.wrap(tokenResult -> { + final TimeValue expiresIn = tokenService.getExpirationDelay(); + listener.onResponse( + new OpenIdConnectAuthenticateResponse( + authentication, + tokenResult.getAccessToken(), + tokenResult.getRefreshToken(), + expiresIn + ) + ); + }, listener::onFailure) + ); + }, e -> { + logger.debug(() -> new ParameterizedMessage("OpenIDConnectToken [{}] could not be authenticated", token), e); + listener.onFailure(e); + })); } } } - diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/oidc/TransportOpenIdConnectLogoutAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/oidc/TransportOpenIdConnectLogoutAction.java index 61dd5a9525bee..bc10a6b1e4efd 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/oidc/TransportOpenIdConnectLogoutAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/oidc/TransportOpenIdConnectLogoutAction.java @@ -8,6 +8,7 @@ import com.nimbusds.jwt.JWT; import com.nimbusds.jwt.JWTParser; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.ElasticsearchSecurityException; @@ -43,10 +44,18 @@ public class TransportOpenIdConnectLogoutAction extends HandledTransportAction) OpenIdConnectLogoutRequest::new); + public TransportOpenIdConnectLogoutAction( + TransportService transportService, + ActionFilters actionFilters, + Realms realms, + TokenService tokenService + ) { + super( + OpenIdConnectLogoutAction.NAME, + transportService, + actionFilters, + (Writeable.Reader) OpenIdConnectLogoutRequest::new + ); this.realms = realms; this.tokenService = tokenService; } @@ -55,24 +64,23 @@ public TransportOpenIdConnectLogoutAction(TransportService transportService, Act protected void doExecute(Task task, OpenIdConnectLogoutRequest request, ActionListener listener) { invalidateRefreshToken(request.getRefreshToken(), ActionListener.wrap(ignore -> { final String token = request.getToken(); - tokenService.getAuthenticationAndMetadata(token, ActionListener.wrap( - tuple -> { - final Authentication authentication = tuple.v1(); - final Map tokenMetadata = tuple.v2(); - validateAuthenticationAndMetadata(authentication, tokenMetadata); - tokenService.invalidateAccessToken(token, ActionListener.wrap( - result -> { - if (logger.isTraceEnabled()) { - logger.trace("OpenID Connect Logout for user [{}] and token [{}...{}]", - authentication.getUser().principal(), - token.substring(0, 8), - token.substring(token.length() - 8)); - } - OpenIdConnectLogoutResponse response = buildResponse(authentication, tokenMetadata); - listener.onResponse(response); - }, listener::onFailure) - ); + tokenService.getAuthenticationAndMetadata(token, ActionListener.wrap(tuple -> { + final Authentication authentication = tuple.v1(); + final Map tokenMetadata = tuple.v2(); + validateAuthenticationAndMetadata(authentication, tokenMetadata); + tokenService.invalidateAccessToken(token, ActionListener.wrap(result -> { + if (logger.isTraceEnabled()) { + logger.trace( + "OpenID Connect Logout for user [{}] and token [{}...{}]", + authentication.getUser().principal(), + token.substring(0, 8), + token.substring(token.length() - 8) + ); + } + OpenIdConnectLogoutResponse response = buildResponse(authentication, tokenMetadata); + listener.onResponse(response); }, listener::onFailure)); + }, listener::onFailure)); }, listener::onFailure)); } @@ -102,8 +110,7 @@ private void validateAuthenticationAndMetadata(Authentication authentication, Ma final Authentication.RealmRef ref = authentication.getAuthenticatedBy(); if (ref == null || Strings.isNullOrEmpty(ref.getName())) { - throw new ElasticsearchSecurityException("Authentication {} has no authenticating realm", - authentication); + throw new ElasticsearchSecurityException("Authentication {} has no authenticating realm", authentication); } final Realm realm = this.realms.realm(authentication.getAuthenticatedBy().getName()); if (realm == null) { @@ -120,12 +127,16 @@ private Object getFromMetadata(Map metadata, String key) { } Object value = metadata.get(key); if (null != value && value instanceof String == false) { - throw new ElasticsearchSecurityException("In authentication token, OpenID Connect metadata [{}] is [{}] rather than " + - "String", key, value.getClass()); + throw new ElasticsearchSecurityException( + "In authentication token, OpenID Connect metadata [{}] is [{}] rather than " + "String", + key, + value.getClass() + ); } return value; } + private void invalidateRefreshToken(String refreshToken, ActionListener listener) { if (refreshToken == null) { listener.onResponse(null); diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/oidc/TransportOpenIdConnectPrepareAuthenticationAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/oidc/TransportOpenIdConnectPrepareAuthenticationAction.java index a3524b3d48a0c..dd9800879c2d9 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/oidc/TransportOpenIdConnectPrepareAuthenticationAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/oidc/TransportOpenIdConnectPrepareAuthenticationAction.java @@ -26,22 +26,33 @@ import java.util.List; import java.util.stream.Collectors; -public class TransportOpenIdConnectPrepareAuthenticationAction extends HandledTransportAction { private final Realms realms; @Inject - public TransportOpenIdConnectPrepareAuthenticationAction(TransportService transportService, - ActionFilters actionFilters, Realms realms) { - super(OpenIdConnectPrepareAuthenticationAction.NAME, transportService, actionFilters, - (Writeable.Reader) OpenIdConnectPrepareAuthenticationRequest::new); + public TransportOpenIdConnectPrepareAuthenticationAction( + TransportService transportService, + ActionFilters actionFilters, + Realms realms + ) { + super( + OpenIdConnectPrepareAuthenticationAction.NAME, + transportService, + actionFilters, + (Writeable.Reader) OpenIdConnectPrepareAuthenticationRequest::new + ); this.realms = realms; } @Override - protected void doExecute(Task task, OpenIdConnectPrepareAuthenticationRequest request, - ActionListener listener) { + protected void doExecute( + Task task, + OpenIdConnectPrepareAuthenticationRequest request, + ActionListener listener + ) { Realm realm = null; if (Strings.hasText(request.getIssuer())) { List matchingRealms = this.realms.stream() @@ -50,10 +61,12 @@ protected void doExecute(Task task, OpenIdConnectPrepareAuthenticationRequest re .collect(Collectors.toList()); if (matchingRealms.isEmpty()) { listener.onFailure( - new ElasticsearchSecurityException("Cannot find OpenID Connect realm with issuer [{}]", request.getIssuer())); + new ElasticsearchSecurityException("Cannot find OpenID Connect realm with issuer [{}]", request.getIssuer()) + ); } else if (matchingRealms.size() > 1) { listener.onFailure( - new ElasticsearchSecurityException("Found multiple OpenID Connect realm with issuer [{}]", request.getIssuer())); + new ElasticsearchSecurityException("Found multiple OpenID Connect realm with issuer [{}]", request.getIssuer()) + ); } else { realm = matchingRealms.get(0); } @@ -62,19 +75,33 @@ protected void doExecute(Task task, OpenIdConnectPrepareAuthenticationRequest re } if (realm instanceof OpenIdConnectRealm) { - prepareAuthenticationResponse((OpenIdConnectRealm) realm, request.getState(), request.getNonce(), request.getLoginHint(), - listener); + prepareAuthenticationResponse( + (OpenIdConnectRealm) realm, + request.getState(), + request.getNonce(), + request.getLoginHint(), + listener + ); } else { listener.onFailure( - new ElasticsearchSecurityException("Cannot find OpenID Connect realm with name [{}]", request.getRealmName())); + new ElasticsearchSecurityException("Cannot find OpenID Connect realm with name [{}]", request.getRealmName()) + ); } } - private void prepareAuthenticationResponse(OpenIdConnectRealm realm, String state, String nonce, String loginHint, - ActionListener listener) { + private void prepareAuthenticationResponse( + OpenIdConnectRealm realm, + String state, + String nonce, + String loginHint, + ActionListener listener + ) { try { - final OpenIdConnectPrepareAuthenticationResponse authenticationResponse = - realm.buildAuthenticationRequestUri(state, nonce, loginHint); + final OpenIdConnectPrepareAuthenticationResponse authenticationResponse = realm.buildAuthenticationRequestUri( + state, + nonce, + loginHint + ); listener.onResponse(authenticationResponse); } catch (ElasticsearchException e) { listener.onFailure(e); diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/privilege/TransportClearPrivilegesCacheAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/privilege/TransportClearPrivilegesCacheAction.java index 352e0242f4414..c8a653981eee5 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/privilege/TransportClearPrivilegesCacheAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/privilege/TransportClearPrivilegesCacheAction.java @@ -26,8 +26,11 @@ import java.io.IOException; import java.util.List; -public class TransportClearPrivilegesCacheAction extends TransportNodesAction { +public class TransportClearPrivilegesCacheAction extends TransportNodesAction< + ClearPrivilegesCacheRequest, + ClearPrivilegesCacheResponse, + ClearPrivilegesCacheRequest.Node, + ClearPrivilegesCacheResponse.Node> { private final CompositeRolesStore rolesStore; private final CacheInvalidatorRegistry cacheInvalidatorRegistry; @@ -39,7 +42,8 @@ public TransportClearPrivilegesCacheAction( TransportService transportService, ActionFilters actionFilters, CompositeRolesStore rolesStore, - CacheInvalidatorRegistry cacheInvalidatorRegistry) { + CacheInvalidatorRegistry cacheInvalidatorRegistry + ) { super( ClearPrivilegesCacheAction.NAME, threadPool, @@ -49,14 +53,18 @@ public TransportClearPrivilegesCacheAction( ClearPrivilegesCacheRequest::new, ClearPrivilegesCacheRequest.Node::new, ThreadPool.Names.MANAGEMENT, - ClearPrivilegesCacheResponse.Node.class); + ClearPrivilegesCacheResponse.Node.class + ); this.rolesStore = rolesStore; this.cacheInvalidatorRegistry = cacheInvalidatorRegistry; } @Override protected ClearPrivilegesCacheResponse newResponse( - ClearPrivilegesCacheRequest request, List nodes, List failures) { + ClearPrivilegesCacheRequest request, + List nodes, + List failures + ) { return new ClearPrivilegesCacheResponse(clusterService.getClusterName(), nodes, failures); } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/privilege/TransportDeletePrivilegesAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/privilege/TransportDeletePrivilegesAction.java index 655880f4d6b1d..0e9bf37beadff 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/privilege/TransportDeletePrivilegesAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/privilege/TransportDeletePrivilegesAction.java @@ -29,8 +29,11 @@ public class TransportDeletePrivilegesAction extends HandledTransportAction names = Sets.newHashSet(request.privileges()); - this.privilegeStore.deletePrivileges(request.application(), names, request.getRefreshPolicy(), ActionListener.wrap( + this.privilegeStore.deletePrivileges( + request.application(), + names, + request.getRefreshPolicy(), + ActionListener.wrap( privileges -> listener.onResponse( - new DeletePrivilegesResponse(privileges.getOrDefault(request.application(), Collections.emptyList())) - ), listener::onFailure - )); + new DeletePrivilegesResponse(privileges.getOrDefault(request.application(), Collections.emptyList())) + ), + listener::onFailure + ) + ); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/privilege/TransportGetPrivilegesAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/privilege/TransportGetPrivilegesAction.java index 5f3e29a6d8c32..2475246353c83 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/privilege/TransportGetPrivilegesAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/privilege/TransportGetPrivilegesAction.java @@ -33,8 +33,11 @@ public class TransportGetPrivilegesAction extends HandledTransportAction applications = isNullOrEmpty(request.application()) ? null : Collections.singleton(request.application()); - this.privilegeStore.getPrivileges(applications, names, ActionListener.wrap( - privileges -> listener.onResponse(new GetPrivilegesResponse(privileges)), - listener::onFailure - )); + this.privilegeStore.getPrivileges( + applications, + names, + ActionListener.wrap(privileges -> listener.onResponse(new GetPrivilegesResponse(privileges)), listener::onFailure) + ); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/privilege/TransportPutPrivilegesAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/privilege/TransportPutPrivilegesAction.java index 61673ab9ac0f4..331d7596c7e33 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/privilege/TransportPutPrivilegesAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/privilege/TransportPutPrivilegesAction.java @@ -27,8 +27,11 @@ public class TransportPutPrivilegesAction extends HandledTransportAction listener.onResponse(new PutPrivilegesResponse(created)), - listener::onFailure - )); + this.privilegeStore.putPrivileges( + request.getPrivileges(), + request.getRefreshPolicy(), + ActionListener.wrap(created -> listener.onResponse(new PutPrivilegesResponse(created)), listener::onFailure) + ); } } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/realm/TransportClearRealmCacheAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/realm/TransportClearRealmCacheAction.java index 93923273851ef..14964c83ab52b 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/realm/TransportClearRealmCacheAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/realm/TransportClearRealmCacheAction.java @@ -21,32 +21,52 @@ import org.elasticsearch.xpack.core.security.action.realm.ClearRealmCacheRequest; import org.elasticsearch.xpack.core.security.action.realm.ClearRealmCacheResponse; import org.elasticsearch.xpack.core.security.authc.Realm; +import org.elasticsearch.xpack.core.security.authc.support.CachingRealm; import org.elasticsearch.xpack.security.authc.AuthenticationService; import org.elasticsearch.xpack.security.authc.Realms; -import org.elasticsearch.xpack.core.security.authc.support.CachingRealm; import java.io.IOException; import java.util.List; -public class TransportClearRealmCacheAction extends TransportNodesAction { +public class TransportClearRealmCacheAction extends TransportNodesAction< + ClearRealmCacheRequest, + ClearRealmCacheResponse, + ClearRealmCacheRequest.Node, + ClearRealmCacheResponse.Node> { private final Realms realms; private final AuthenticationService authenticationService; @Inject - public TransportClearRealmCacheAction(ThreadPool threadPool, ClusterService clusterService, TransportService transportService, - ActionFilters actionFilters, Realms realms, AuthenticationService authenticationService) { - super(ClearRealmCacheAction.NAME, threadPool, clusterService, transportService, actionFilters, - ClearRealmCacheRequest::new, ClearRealmCacheRequest.Node::new, ThreadPool.Names.MANAGEMENT, - ClearRealmCacheResponse.Node.class); + public TransportClearRealmCacheAction( + ThreadPool threadPool, + ClusterService clusterService, + TransportService transportService, + ActionFilters actionFilters, + Realms realms, + AuthenticationService authenticationService + ) { + super( + ClearRealmCacheAction.NAME, + threadPool, + clusterService, + transportService, + actionFilters, + ClearRealmCacheRequest::new, + ClearRealmCacheRequest.Node::new, + ThreadPool.Names.MANAGEMENT, + ClearRealmCacheResponse.Node.class + ); this.realms = realms; this.authenticationService = authenticationService; } @Override - protected ClearRealmCacheResponse newResponse(ClearRealmCacheRequest request, - List responses, List failures) { + protected ClearRealmCacheResponse newResponse( + ClearRealmCacheRequest request, + List responses, + List failures + ) { return new ClearRealmCacheResponse(clusterService.getClusterName(), responses, failures); } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/role/TransportClearRolesCacheAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/role/TransportClearRolesCacheAction.java index 4b7a5d6131310..004beb867b05c 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/role/TransportClearRolesCacheAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/role/TransportClearRolesCacheAction.java @@ -24,22 +24,42 @@ import java.io.IOException; import java.util.List; -public class TransportClearRolesCacheAction extends TransportNodesAction { +public class TransportClearRolesCacheAction extends TransportNodesAction< + ClearRolesCacheRequest, + ClearRolesCacheResponse, + ClearRolesCacheRequest.Node, + ClearRolesCacheResponse.Node> { private final CompositeRolesStore rolesStore; @Inject - public TransportClearRolesCacheAction(ThreadPool threadPool, ClusterService clusterService, - TransportService transportService, ActionFilters actionFilters, CompositeRolesStore rolesStore) { - super(ClearRolesCacheAction.NAME, threadPool, clusterService, transportService, actionFilters, ClearRolesCacheRequest::new, - ClearRolesCacheRequest.Node::new, ThreadPool.Names.MANAGEMENT, ClearRolesCacheResponse.Node.class); + public TransportClearRolesCacheAction( + ThreadPool threadPool, + ClusterService clusterService, + TransportService transportService, + ActionFilters actionFilters, + CompositeRolesStore rolesStore + ) { + super( + ClearRolesCacheAction.NAME, + threadPool, + clusterService, + transportService, + actionFilters, + ClearRolesCacheRequest::new, + ClearRolesCacheRequest.Node::new, + ThreadPool.Names.MANAGEMENT, + ClearRolesCacheResponse.Node.class + ); this.rolesStore = rolesStore; } @Override - protected ClearRolesCacheResponse newResponse(ClearRolesCacheRequest request, - List responses, List failures) { + protected ClearRolesCacheResponse newResponse( + ClearRolesCacheRequest request, + List responses, + List failures + ) { return new ClearRolesCacheResponse(clusterService.getClusterName(), responses, failures); } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/role/TransportGetRolesAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/role/TransportGetRolesAction.java index 4158253b0013f..f046ba2b16278 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/role/TransportGetRolesAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/role/TransportGetRolesAction.java @@ -30,8 +30,12 @@ public class TransportGetRolesAction extends HandledTransportAction { +public class TransportDeleteRoleMappingAction extends HandledTransportAction { private final NativeRoleMappingStore roleMappingStore; @Inject - public TransportDeleteRoleMappingAction(ActionFilters actionFilters, TransportService transportService, - NativeRoleMappingStore roleMappingStore) { + public TransportDeleteRoleMappingAction( + ActionFilters actionFilters, + TransportService transportService, + NativeRoleMappingStore roleMappingStore + ) { super(DeleteRoleMappingAction.NAME, transportService, actionFilters, DeleteRoleMappingRequest::new); this.roleMappingStore = roleMappingStore; } @Override protected void doExecute(Task task, DeleteRoleMappingRequest request, ActionListener listener) { - roleMappingStore.deleteRoleMapping(request, - listener.delegateFailure((l, found) -> l.onResponse(new DeleteRoleMappingResponse(found)))); + roleMappingStore.deleteRoleMapping( + request, + listener.delegateFailure((l, found) -> l.onResponse(new DeleteRoleMappingResponse(found))) + ); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/rolemapping/TransportGetRoleMappingsAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/rolemapping/TransportGetRoleMappingsAction.java index ff585a645ad75..a07f3267e8ace 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/rolemapping/TransportGetRoleMappingsAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/rolemapping/TransportGetRoleMappingsAction.java @@ -22,14 +22,16 @@ import java.util.HashSet; import java.util.Set; -public class TransportGetRoleMappingsAction - extends HandledTransportAction { +public class TransportGetRoleMappingsAction extends HandledTransportAction { private final NativeRoleMappingStore roleMappingStore; @Inject - public TransportGetRoleMappingsAction(ActionFilters actionFilters, TransportService transportService, - NativeRoleMappingStore nativeRoleMappingStore) { + public TransportGetRoleMappingsAction( + ActionFilters actionFilters, + TransportService transportService, + NativeRoleMappingStore nativeRoleMappingStore + ) { super(GetRoleMappingsAction.NAME, transportService, actionFilters, GetRoleMappingsRequest::new); this.roleMappingStore = nativeRoleMappingStore; } @@ -42,14 +44,9 @@ protected void doExecute(Task task, final GetRoleMappingsRequest request, final } else { names = new HashSet<>(Arrays.asList(request.getNames())); } - this.roleMappingStore.getRoleMappings(names, ActionListener.wrap( - mappings -> { - ExpressionRoleMapping[] array = mappings.toArray( - new ExpressionRoleMapping[mappings.size()] - ); - listener.onResponse(new GetRoleMappingsResponse(array)); - }, - listener::onFailure - )); + this.roleMappingStore.getRoleMappings(names, ActionListener.wrap(mappings -> { + ExpressionRoleMapping[] array = mappings.toArray(new ExpressionRoleMapping[mappings.size()]); + listener.onResponse(new GetRoleMappingsResponse(array)); + }, listener::onFailure)); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/rolemapping/TransportPutRoleMappingAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/rolemapping/TransportPutRoleMappingAction.java index 94ab55e9c5448..f5df0330bfe8a 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/rolemapping/TransportPutRoleMappingAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/rolemapping/TransportPutRoleMappingAction.java @@ -17,23 +17,25 @@ import org.elasticsearch.xpack.core.security.action.rolemapping.PutRoleMappingResponse; import org.elasticsearch.xpack.security.authc.support.mapper.NativeRoleMappingStore; -public class TransportPutRoleMappingAction - extends HandledTransportAction { +public class TransportPutRoleMappingAction extends HandledTransportAction { private final NativeRoleMappingStore roleMappingStore; @Inject - public TransportPutRoleMappingAction(ActionFilters actionFilters, TransportService transportService, - NativeRoleMappingStore roleMappingStore) { + public TransportPutRoleMappingAction( + ActionFilters actionFilters, + TransportService transportService, + NativeRoleMappingStore roleMappingStore + ) { super(PutRoleMappingAction.NAME, transportService, actionFilters, PutRoleMappingRequest::new); this.roleMappingStore = roleMappingStore; } @Override protected void doExecute(Task task, final PutRoleMappingRequest request, final ActionListener listener) { - roleMappingStore.putRoleMapping(request, ActionListener.wrap( - created -> listener.onResponse(new PutRoleMappingResponse(created)), - listener::onFailure - )); + roleMappingStore.putRoleMapping( + request, + ActionListener.wrap(created -> listener.onResponse(new PutRoleMappingResponse(created)), listener::onFailure) + ); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/saml/TransportSamlAuthenticateAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/saml/TransportSamlAuthenticateAction.java index 27ed889681796..f016e37df8105 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/saml/TransportSamlAuthenticateAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/saml/TransportSamlAuthenticateAction.java @@ -11,8 +11,8 @@ import org.elasticsearch.action.support.ActionFilters; import org.elasticsearch.action.support.HandledTransportAction; import org.elasticsearch.common.inject.Inject; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.tasks.Task; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; @@ -41,9 +41,14 @@ public final class TransportSamlAuthenticateAction extends HandledTransportActio private final SecurityContext securityContext; @Inject - public TransportSamlAuthenticateAction(ThreadPool threadPool, TransportService transportService, - ActionFilters actionFilters, AuthenticationService authenticationService, - TokenService tokenService, SecurityContext securityContext) { + public TransportSamlAuthenticateAction( + ThreadPool threadPool, + TransportService transportService, + ActionFilters actionFilters, + AuthenticationService authenticationService, + TokenService tokenService, + SecurityContext securityContext + ) { super(SamlAuthenticateAction.NAME, transportService, actionFilters, SamlAuthenticateRequest::new); this.threadPool = threadPool; this.authenticationService = authenticationService; @@ -67,13 +72,23 @@ protected void doExecute(Task task, SamlAuthenticateRequest request, ActionListe assert authentication != null : "authentication should never be null at this point"; @SuppressWarnings("unchecked") final Map tokenMeta = (Map) result.getMetadata().get(SamlRealm.CONTEXT_TOKEN_DATA); - tokenService.createOAuth2Tokens(authentication, originatingAuthentication, - tokenMeta, true, ActionListener.wrap(tokenResult -> { - final TimeValue expiresIn = tokenService.getExpirationDelay(); - listener.onResponse( - new SamlAuthenticateResponse(authentication, tokenResult.getAccessToken(), tokenResult.getRefreshToken(), - expiresIn)); - }, listener::onFailure)); + tokenService.createOAuth2Tokens( + authentication, + originatingAuthentication, + tokenMeta, + true, + ActionListener.wrap(tokenResult -> { + final TimeValue expiresIn = tokenService.getExpirationDelay(); + listener.onResponse( + new SamlAuthenticateResponse( + authentication, + tokenResult.getAccessToken(), + tokenResult.getRefreshToken(), + expiresIn + ) + ); + }, listener::onFailure) + ); }, e -> { logger.debug(() -> new ParameterizedMessage("SamlToken [{}] could not be authenticated", saml), e); listener.onFailure(e); diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/saml/TransportSamlCompleteLogoutAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/saml/TransportSamlCompleteLogoutAction.java index 1db1a77e45d59..da1d1bd3b5cfa 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/saml/TransportSamlCompleteLogoutAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/saml/TransportSamlCompleteLogoutAction.java @@ -49,8 +49,11 @@ protected void doExecute(Task task, SamlCompleteLogoutRequest request, ActionLis } } - private void processLogoutResponse(SamlRealm samlRealm, SamlCompleteLogoutRequest request, - ActionListener listener) { + private void processLogoutResponse( + SamlRealm samlRealm, + SamlCompleteLogoutRequest request, + ActionListener listener + ) { final SamlLogoutResponseHandler logoutResponseHandler = samlRealm.getLogoutResponseHandler(); try { diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/saml/TransportSamlInvalidateSessionAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/saml/TransportSamlInvalidateSessionAction.java index c4402bb85b453..11d4e223f3703 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/saml/TransportSamlInvalidateSessionAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/saml/TransportSamlInvalidateSessionAction.java @@ -12,8 +12,8 @@ import org.elasticsearch.action.support.GroupedActionListener; import org.elasticsearch.action.support.HandledTransportAction; import org.elasticsearch.common.Strings; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.inject.Inject; +import org.elasticsearch.core.Tuple; import org.elasticsearch.tasks.Task; import org.elasticsearch.transport.TransportService; import org.elasticsearch.xpack.core.security.action.saml.SamlInvalidateSessionAction; @@ -39,15 +39,20 @@ /** * Transport action responsible for taking a SAML {@code LogoutRequest} and invalidating any associated Security Tokens */ -public final class TransportSamlInvalidateSessionAction - extends HandledTransportAction { +public final class TransportSamlInvalidateSessionAction extends HandledTransportAction< + SamlInvalidateSessionRequest, + SamlInvalidateSessionResponse> { private final TokenService tokenService; private final Realms realms; @Inject - public TransportSamlInvalidateSessionAction(TransportService transportService, ActionFilters actionFilters, TokenService tokenService, - Realms realms) { + public TransportSamlInvalidateSessionAction( + TransportService transportService, + ActionFilters actionFilters, + TokenService tokenService, + Realms realms + ) { super(SamlInvalidateSessionAction.NAME, transportService, actionFilters, SamlInvalidateSessionRequest::new); this.tokenService = tokenService; this.realms = realms; @@ -65,13 +70,23 @@ protected void doExecute(Task task, SamlInvalidateSessionRequest request, Action } } - private void invalidateSession(SamlRealm realm, SamlInvalidateSessionRequest request, - ActionListener listener) { + private void invalidateSession( + SamlRealm realm, + SamlInvalidateSessionRequest request, + ActionListener listener + ) { try { final SamlLogoutRequestHandler.Result result = realm.getLogoutHandler().parseFromQueryString(request.getQueryString()); - findAndInvalidateTokens(realm, result, ActionListener.wrap(count -> listener.onResponse( - new SamlInvalidateSessionResponse(realm.name(), count, buildLogoutResponseUrl(realm, result)) - ), listener::onFailure)); + findAndInvalidateTokens( + realm, + result, + ActionListener.wrap( + count -> listener.onResponse( + new SamlInvalidateSessionResponse(realm.name(), count, buildLogoutResponseUrl(realm, result)) + ), + listener::onFailure + ) + ); } catch (ElasticsearchSecurityException e) { logger.info("Failed to invalidate SAML session", e); listener.onFailure(e); @@ -93,29 +108,33 @@ private void findAndInvalidateTokens(SamlRealm realm, SamlLogoutRequestHandler.R } tokenService.findActiveTokensForRealm(realm.name(), containsMetadata(tokenMetadata), ActionListener.wrap(tokens -> { - logger.debug("Found [{}] token pairs to invalidate for SAML metadata [{}]", tokens.size(), tokenMetadata); - if (tokens.isEmpty()) { - listener.onResponse(0); - } else { - GroupedActionListener groupedListener = new GroupedActionListener<>( - ActionListener.wrap(collection -> listener.onResponse(collection.size()), listener::onFailure), tokens.size()); - tokens.forEach(tuple -> invalidateTokenPair(tuple, groupedListener)); - } - }, listener::onFailure - )); + logger.debug("Found [{}] token pairs to invalidate for SAML metadata [{}]", tokens.size(), tokenMetadata); + if (tokens.isEmpty()) { + listener.onResponse(0); + } else { + GroupedActionListener groupedListener = new GroupedActionListener<>( + ActionListener.wrap(collection -> listener.onResponse(collection.size()), listener::onFailure), + tokens.size() + ); + tokens.forEach(tuple -> invalidateTokenPair(tuple, groupedListener)); + } + }, listener::onFailure)); } private void invalidateTokenPair(Tuple tokenPair, ActionListener listener) { // Invalidate the refresh token first, so the client doesn't trigger a refresh once the access token is invalidated - tokenService.invalidateRefreshToken(tokenPair.v2(), ActionListener.wrap(ignore -> tokenService.invalidateAccessToken( - tokenPair.v1(), - ActionListener.wrap(listener::onResponse, e -> { + tokenService.invalidateRefreshToken( + tokenPair.v2(), + ActionListener.wrap( + ignore -> tokenService.invalidateAccessToken(tokenPair.v1(), ActionListener.wrap(listener::onResponse, e -> { logger.info("Failed to invalidate SAML access_token [{}] - {}", tokenPair.v1().getId(), e.toString()); listener.onFailure(e); - })), listener::onFailure)); + })), + listener::onFailure + ) + ); } - private Predicate> containsMetadata(Map requiredMetadata) { return source -> { @SuppressWarnings("unchecked") diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/saml/TransportSamlLogoutAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/saml/TransportSamlLogoutAction.java index 5e36c98e2b93e..7487da2e6aae7 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/saml/TransportSamlLogoutAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/saml/TransportSamlLogoutAction.java @@ -34,15 +34,18 @@ /** * Transport action responsible for generating a SAML {@code <LogoutRequest>} as a redirect binding URL. */ -public final class TransportSamlLogoutAction - extends HandledTransportAction { +public final class TransportSamlLogoutAction extends HandledTransportAction { private final Realms realms; private final TokenService tokenService; @Inject - public TransportSamlLogoutAction(TransportService transportService, ActionFilters actionFilters, Realms realms, - TokenService tokenService) { + public TransportSamlLogoutAction( + TransportService transportService, + ActionFilters actionFilters, + Realms realms, + TokenService tokenService + ) { super(SamlLogoutAction.NAME, transportService, actionFilters, SamlLogoutRequest::new); this.realms = realms; this.tokenService = tokenService; @@ -53,26 +56,22 @@ protected void doExecute(Task task, SamlLogoutRequest request, ActionListener { try { final String token = request.getToken(); - tokenService.getAuthenticationAndMetadata(token, ActionListener.wrap( - tuple -> { - Authentication authentication = tuple.v1(); - final Map tokenMetadata = tuple.v2(); - SamlLogoutResponse response = buildResponse(authentication, tokenMetadata); - tokenService.invalidateAccessToken(token, ActionListener.wrap( - created -> { - if (logger.isTraceEnabled()) { - logger.trace("SAML Logout User [{}], Token [{}...{}]", - authentication.getUser().principal(), - token.substring(0, 8), - token.substring(token.length() - 8) - ); - } - listener.onResponse(response); - }, - listener::onFailure - )); - }, listener::onFailure - )); + tokenService.getAuthenticationAndMetadata(token, ActionListener.wrap(tuple -> { + Authentication authentication = tuple.v1(); + final Map tokenMetadata = tuple.v2(); + SamlLogoutResponse response = buildResponse(authentication, tokenMetadata); + tokenService.invalidateAccessToken(token, ActionListener.wrap(created -> { + if (logger.isTraceEnabled()) { + logger.trace( + "SAML Logout User [{}], Token [{}...{}]", + authentication.getUser().principal(), + token.substring(0, 8), + token.substring(token.length() - 8) + ); + } + listener.onResponse(response); + }, listener::onFailure)); + }, listener::onFailure)); } catch (ElasticsearchException e) { logger.debug("Internal exception during SAML logout", e); listener.onFailure(e); @@ -104,11 +103,11 @@ private SamlLogoutResponse buildResponse(Authentication authentication, Map { +public final class TransportSamlPrepareAuthenticationAction extends HandledTransportAction< + SamlPrepareAuthenticationRequest, + SamlPrepareAuthenticationResponse> { private final Realms realms; @Inject public TransportSamlPrepareAuthenticationAction(TransportService transportService, ActionFilters actionFilters, Realms realms) { - super(SamlPrepareAuthenticationAction.NAME, transportService, actionFilters, SamlPrepareAuthenticationRequest::new - ); + super(SamlPrepareAuthenticationAction.NAME, transportService, actionFilters, SamlPrepareAuthenticationRequest::new); this.realms = realms; } @Override - protected void doExecute(Task task, SamlPrepareAuthenticationRequest request, - ActionListener listener) { + protected void doExecute( + Task task, + SamlPrepareAuthenticationRequest request, + ActionListener listener + ) { List realms = findSamlRealms(this.realms, request.getRealmName(), request.getAssertionConsumerServiceURL()); if (realms.isEmpty()) { listener.onFailure(SamlUtils.samlException("Cannot find any matching realm for [{}]", request)); @@ -58,11 +61,7 @@ private void prepareAuthentication(SamlRealm realm, String relayState, ActionLis final AuthnRequest authnRequest = realm.buildAuthenticationRequest(); try { String redirectUrl = new SamlRedirect(authnRequest, realm.getSigningConfiguration()).getRedirectUrl(relayState); - listener.onResponse(new SamlPrepareAuthenticationResponse( - realm.name(), - authnRequest.getID(), - redirectUrl - )); + listener.onResponse(new SamlPrepareAuthenticationResponse(realm.name(), authnRequest.getID(), redirectUrl)); } catch (ElasticsearchException e) { listener.onFailure(e); } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/saml/TransportSamlSpMetadataAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/saml/TransportSamlSpMetadataAction.java index 1bbdad0f8bad1..2b59e1b1019c9 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/saml/TransportSamlSpMetadataAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/saml/TransportSamlSpMetadataAction.java @@ -27,33 +27,31 @@ import org.opensaml.saml.saml2.metadata.impl.EntityDescriptorMarshaller; import org.w3c.dom.Element; -import javax.xml.transform.Transformer; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; import java.io.StringWriter; import java.util.List; import java.util.Locale; +import javax.xml.transform.Transformer; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; + import static org.elasticsearch.xpack.security.authc.saml.SamlRealm.findSamlRealms; /** * Transport action responsible for generating a SAML SP Metadata. */ -public class TransportSamlSpMetadataAction - extends HandledTransportAction { +public class TransportSamlSpMetadataAction extends HandledTransportAction { private final Realms realms; @Inject public TransportSamlSpMetadataAction(TransportService transportService, ActionFilters actionFilters, Realms realms) { - super(SamlSpMetadataAction.NAME, transportService, actionFilters, SamlSpMetadataRequest::new - ); + super(SamlSpMetadataAction.NAME, transportService, actionFilters, SamlSpMetadataRequest::new); this.realms = realms; } @Override - protected void doExecute(Task task, SamlSpMetadataRequest request, - ActionListener listener) { + protected void doExecute(Task task, SamlSpMetadataRequest request, ActionListener listener) { List realms = findSamlRealms(this.realms, request.getRealmName(), null); if (realms.isEmpty()) { listener.onFailure(SamlUtils.samlException("Cannot find any matching realm for [{}]", request.getRealmName())); @@ -81,8 +79,7 @@ private void prepareMetadata(SamlRealm realm, ActionListener { +public class TransportCreateServiceAccountTokenAction extends HandledTransportAction< + CreateServiceAccountTokenRequest, + CreateServiceAccountTokenResponse> { private final ServiceAccountService serviceAccountService; private final SecurityContext securityContext; @Inject - public TransportCreateServiceAccountTokenAction(TransportService transportService, ActionFilters actionFilters, - ServiceAccountService serviceAccountService, - SecurityContext securityContext) { + public TransportCreateServiceAccountTokenAction( + TransportService transportService, + ActionFilters actionFilters, + ServiceAccountService serviceAccountService, + SecurityContext securityContext + ) { super(CreateServiceAccountTokenAction.NAME, transportService, actionFilters, CreateServiceAccountTokenRequest::new); this.serviceAccountService = serviceAccountService; this.securityContext = securityContext; } @Override - protected void doExecute(Task task, CreateServiceAccountTokenRequest request, - ActionListener listener) { + protected void doExecute( + Task task, + CreateServiceAccountTokenRequest request, + ActionListener listener + ) { final Authentication authentication = securityContext.getAuthentication(); if (authentication == null) { listener.onFailure(new IllegalStateException("authentication is required")); diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/service/TransportDeleteServiceAccountTokenAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/service/TransportDeleteServiceAccountTokenAction.java index 33f15325cce5f..4a317c566fe04 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/service/TransportDeleteServiceAccountTokenAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/service/TransportDeleteServiceAccountTokenAction.java @@ -18,22 +18,31 @@ import org.elasticsearch.xpack.core.security.action.service.DeleteServiceAccountTokenResponse; import org.elasticsearch.xpack.security.authc.service.ServiceAccountService; -public class TransportDeleteServiceAccountTokenAction - extends HandledTransportAction { +public class TransportDeleteServiceAccountTokenAction extends HandledTransportAction< + DeleteServiceAccountTokenRequest, + DeleteServiceAccountTokenResponse> { private final ServiceAccountService serviceAccountService; @Inject - public TransportDeleteServiceAccountTokenAction(TransportService transportService, ActionFilters actionFilters, - ServiceAccountService serviceAccountService) { + public TransportDeleteServiceAccountTokenAction( + TransportService transportService, + ActionFilters actionFilters, + ServiceAccountService serviceAccountService + ) { super(DeleteServiceAccountTokenAction.NAME, transportService, actionFilters, DeleteServiceAccountTokenRequest::new); this.serviceAccountService = serviceAccountService; } @Override - protected void doExecute(Task task, DeleteServiceAccountTokenRequest request, - ActionListener listener) { - serviceAccountService.deleteIndexToken(request, ActionListener.wrap(found -> - listener.onResponse(new DeleteServiceAccountTokenResponse(found)), listener::onFailure)); + protected void doExecute( + Task task, + DeleteServiceAccountTokenRequest request, + ActionListener listener + ) { + serviceAccountService.deleteIndexToken( + request, + ActionListener.wrap(found -> listener.onResponse(new DeleteServiceAccountTokenResponse(found)), listener::onFailure) + ); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/service/TransportGetServiceAccountCredentialsAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/service/TransportGetServiceAccountCredentialsAction.java index 7182a41f0bd48..5d4f485699a4e 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/service/TransportGetServiceAccountCredentialsAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/service/TransportGetServiceAccountCredentialsAction.java @@ -18,21 +18,28 @@ import org.elasticsearch.xpack.core.security.action.service.GetServiceAccountCredentialsResponse; import org.elasticsearch.xpack.security.authc.service.ServiceAccountService; -public class TransportGetServiceAccountCredentialsAction - extends HandledTransportAction { +public class TransportGetServiceAccountCredentialsAction extends HandledTransportAction< + GetServiceAccountCredentialsRequest, + GetServiceAccountCredentialsResponse> { private final ServiceAccountService serviceAccountService; @Inject - public TransportGetServiceAccountCredentialsAction(TransportService transportService, ActionFilters actionFilters, - ServiceAccountService serviceAccountService) { + public TransportGetServiceAccountCredentialsAction( + TransportService transportService, + ActionFilters actionFilters, + ServiceAccountService serviceAccountService + ) { super(GetServiceAccountCredentialsAction.NAME, transportService, actionFilters, GetServiceAccountCredentialsRequest::new); this.serviceAccountService = serviceAccountService; } @Override - protected void doExecute(Task task, GetServiceAccountCredentialsRequest request, - ActionListener listener) { + protected void doExecute( + Task task, + GetServiceAccountCredentialsRequest request, + ActionListener listener + ) { serviceAccountService.findTokensFor(request, listener); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/service/TransportGetServiceAccountNodesCredentialsAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/service/TransportGetServiceAccountNodesCredentialsAction.java index 09b7b0f1f39be..5e12da07ac652 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/service/TransportGetServiceAccountNodesCredentialsAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/service/TransportGetServiceAccountNodesCredentialsAction.java @@ -17,9 +17,9 @@ import org.elasticsearch.tasks.Task; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; -import org.elasticsearch.xpack.core.security.action.service.GetServiceAccountNodesCredentialsAction; import org.elasticsearch.xpack.core.security.action.service.GetServiceAccountCredentialsNodesRequest; import org.elasticsearch.xpack.core.security.action.service.GetServiceAccountCredentialsNodesResponse; +import org.elasticsearch.xpack.core.security.action.service.GetServiceAccountNodesCredentialsAction; import org.elasticsearch.xpack.core.security.action.service.TokenInfo; import org.elasticsearch.xpack.security.authc.service.FileServiceAccountTokenStore; import org.elasticsearch.xpack.security.authc.service.ServiceAccount.ServiceAccountId; @@ -31,20 +31,33 @@ * This action handler is to retrieve service account credentials that are local to the node. * Currently this means file-backed service tokens. */ -public class TransportGetServiceAccountNodesCredentialsAction - extends TransportNodesAction { +public class TransportGetServiceAccountNodesCredentialsAction extends TransportNodesAction< + GetServiceAccountCredentialsNodesRequest, + GetServiceAccountCredentialsNodesResponse, + GetServiceAccountCredentialsNodesRequest.Node, + GetServiceAccountCredentialsNodesResponse.Node> { private final FileServiceAccountTokenStore fileServiceAccountTokenStore; @Inject - public TransportGetServiceAccountNodesCredentialsAction(ThreadPool threadPool, ClusterService clusterService, - TransportService transportService, ActionFilters actionFilters, - FileServiceAccountTokenStore fileServiceAccountTokenStore) { + public TransportGetServiceAccountNodesCredentialsAction( + ThreadPool threadPool, + ClusterService clusterService, + TransportService transportService, + ActionFilters actionFilters, + FileServiceAccountTokenStore fileServiceAccountTokenStore + ) { super( - GetServiceAccountNodesCredentialsAction.NAME, threadPool, clusterService, transportService, actionFilters, - GetServiceAccountCredentialsNodesRequest::new, GetServiceAccountCredentialsNodesRequest.Node::new, - ThreadPool.Names.SAME, GetServiceAccountCredentialsNodesResponse.Node.class); + GetServiceAccountNodesCredentialsAction.NAME, + threadPool, + clusterService, + transportService, + actionFilters, + GetServiceAccountCredentialsNodesRequest::new, + GetServiceAccountCredentialsNodesRequest.Node::new, + ThreadPool.Names.SAME, + GetServiceAccountCredentialsNodesResponse.Node.class + ); this.fileServiceAccountTokenStore = fileServiceAccountTokenStore; } @@ -52,7 +65,8 @@ public TransportGetServiceAccountNodesCredentialsAction(ThreadPool threadPool, C protected GetServiceAccountCredentialsNodesResponse newResponse( GetServiceAccountCredentialsNodesRequest request, List nodes, - List failures) { + List failures + ) { return new GetServiceAccountCredentialsNodesResponse(clusterService.getClusterName(), nodes, failures); } @@ -68,12 +82,14 @@ protected GetServiceAccountCredentialsNodesResponse.Node newNodeResponse(StreamI @Override protected GetServiceAccountCredentialsNodesResponse.Node nodeOperation( - GetServiceAccountCredentialsNodesRequest.Node request, Task task + GetServiceAccountCredentialsNodesRequest.Node request, + Task task ) { final ServiceAccountId accountId = new ServiceAccountId(request.getNamespace(), request.getServiceName()); final List tokenInfos = fileServiceAccountTokenStore.findTokensFor(accountId); return new GetServiceAccountCredentialsNodesResponse.Node( clusterService.localNode(), - tokenInfos.stream().map(TokenInfo::getName).toArray(String[]::new)); + tokenInfos.stream().map(TokenInfo::getName).toArray(String[]::new) + ); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/token/TransportCreateTokenAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/token/TransportCreateTokenAction.java index 3a1288be49222..b4a7c9970dead 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/token/TransportCreateTokenAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/token/TransportCreateTokenAction.java @@ -10,10 +10,10 @@ import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.support.ActionFilters; import org.elasticsearch.action.support.HandledTransportAction; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.Tuple; import org.elasticsearch.tasks.Task; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; @@ -49,9 +49,14 @@ public final class TransportCreateTokenAction extends HandledTransportAction { - clearCredentialsFromRequest(grantType, request); + authenticationService.authenticate(CreateTokenAction.NAME, request, authToken, ActionListener.wrap(authentication -> { + clearCredentialsFromRequest(grantType, request); - if (authentication != null) { - createToken(grantType, request, authentication, originatingAuthentication, true, listener); - } else { - listener.onFailure(new UnsupportedOperationException("cannot create token if authentication is not allowed")); - } - }, e -> { - clearCredentialsFromRequest(grantType, request); - listener.onFailure(e); - })); + if (authentication != null) { + createToken(grantType, request, authentication, originatingAuthentication, true, listener); + } else { + listener.onFailure(new UnsupportedOperationException("cannot create token if authentication is not allowed")); + } + }, e -> { + clearCredentialsFromRequest(grantType, request); + listener.onFailure(e); + })); } } @@ -125,8 +131,10 @@ private Tuple> extractAuthenticationTok try { decodedKerberosTicket = Base64.getDecoder().decode(base64EncodedToken); } catch (IllegalArgumentException iae) { - return new Tuple<>(null, - Optional.of(new UnsupportedOperationException("could not decode base64 kerberos ticket " + base64EncodedToken, iae))); + return new Tuple<>( + null, + Optional.of(new UnsupportedOperationException("could not decode base64 kerberos ticket " + base64EncodedToken, iae)) + ); } authToken = new KerberosAuthenticationToken(decodedKerberosTicket); } @@ -141,17 +149,33 @@ private void clearCredentialsFromRequest(GrantType grantType, CreateTokenRequest } } - private void createToken(GrantType grantType, CreateTokenRequest request, Authentication authentication, Authentication originatingAuth, - boolean includeRefreshToken, ActionListener listener) { - tokenService.createOAuth2Tokens(authentication, originatingAuth, Collections.emptyMap(), includeRefreshToken, - ActionListener.wrap(tokenResult -> { - final String scope = getResponseScopeValue(request.getScope()); - final String base64AuthenticateResponse = (grantType == GrantType.KERBEROS) ? extractOutToken() : null; - final CreateTokenResponse response = new CreateTokenResponse(tokenResult.getAccessToken(), - tokenService.getExpirationDelay(), scope, tokenResult.getRefreshToken(), base64AuthenticateResponse, - authentication); - listener.onResponse(response); - }, listener::onFailure)); + private void createToken( + GrantType grantType, + CreateTokenRequest request, + Authentication authentication, + Authentication originatingAuth, + boolean includeRefreshToken, + ActionListener listener + ) { + tokenService.createOAuth2Tokens( + authentication, + originatingAuth, + Collections.emptyMap(), + includeRefreshToken, + ActionListener.wrap(tokenResult -> { + final String scope = getResponseScopeValue(request.getScope()); + final String base64AuthenticateResponse = (grantType == GrantType.KERBEROS) ? extractOutToken() : null; + final CreateTokenResponse response = new CreateTokenResponse( + tokenResult.getAccessToken(), + tokenService.getExpirationDelay(), + scope, + tokenResult.getRefreshToken(), + base64AuthenticateResponse, + authentication + ); + listener.onResponse(response); + }, listener::onFailure) + ); } private String extractOutToken() { @@ -160,8 +184,9 @@ private String extractOutToken() { final String wwwAuthenticateHeaderValue = values.get(0); // it may contain base64 encoded token that needs to be sent to client if mutual auth was requested if (wwwAuthenticateHeaderValue.startsWith(KerberosAuthenticationToken.NEGOTIATE_AUTH_HEADER_PREFIX)) { - final String base64EncodedToken = wwwAuthenticateHeaderValue - .substring(KerberosAuthenticationToken.NEGOTIATE_AUTH_HEADER_PREFIX.length()).trim(); + final String base64EncodedToken = wwwAuthenticateHeaderValue.substring( + KerberosAuthenticationToken.NEGOTIATE_AUTH_HEADER_PREFIX.length() + ).trim(); return base64EncodedToken; } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/token/TransportInvalidateTokenAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/token/TransportInvalidateTokenAction.java index ec565950b15ab..de0779e24871a 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/token/TransportInvalidateTokenAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/token/TransportInvalidateTokenAction.java @@ -34,9 +34,10 @@ public TransportInvalidateTokenAction(TransportService transportService, ActionF @Override protected void doExecute(Task task, InvalidateTokenRequest request, ActionListener listener) { - final ActionListener invalidateListener = - ActionListener.wrap(tokensInvalidationResult -> - listener.onResponse(new InvalidateTokenResponse(tokensInvalidationResult)), listener::onFailure); + final ActionListener invalidateListener = ActionListener.wrap( + tokensInvalidationResult -> listener.onResponse(new InvalidateTokenResponse(tokensInvalidationResult)), + listener::onFailure + ); if (Strings.hasText(request.getUserName()) || Strings.hasText(request.getRealmName())) { tokenService.invalidateActiveTokensForRealmAndUser(request.getRealmName(), request.getUserName(), invalidateListener); } else if (request.getTokenType() == InvalidateTokenRequest.Type.ACCESS_TOKEN) { diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/token/TransportRefreshTokenAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/token/TransportRefreshTokenAction.java index 12be11233dffe..dd91762d155d0 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/token/TransportRefreshTokenAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/token/TransportRefreshTokenAction.java @@ -33,9 +33,14 @@ public TransportRefreshTokenAction(TransportService transportService, ActionFilt protected void doExecute(Task task, CreateTokenRequest request, ActionListener listener) { tokenService.refreshToken(request.getRefreshToken(), ActionListener.wrap(tokenResult -> { final String scope = getResponseScopeValue(request.getScope()); - final CreateTokenResponse response = - new CreateTokenResponse(tokenResult.getAccessToken(), tokenService.getExpirationDelay(), scope, - tokenResult.getRefreshToken(), null, tokenResult.getAuthentication()); + final CreateTokenResponse response = new CreateTokenResponse( + tokenResult.getAccessToken(), + tokenService.getExpirationDelay(), + scope, + tokenResult.getRefreshToken(), + null, + tokenResult.getAuthentication() + ); listener.onResponse(response); }, listener::onFailure)); } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/user/TransportAuthenticateAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/user/TransportAuthenticateAction.java index 5056ad7dfc5e6..4c9cbcf0e5f65 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/user/TransportAuthenticateAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/user/TransportAuthenticateAction.java @@ -29,8 +29,12 @@ public class TransportAuthenticateAction extends HandledTransportAction l.onResponse(ActionResponse.Empty.INSTANCE))); diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/user/TransportDeleteUserAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/user/TransportDeleteUserAction.java index 3bfda0f091cf4..2118376152079 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/user/TransportDeleteUserAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/user/TransportDeleteUserAction.java @@ -27,8 +27,12 @@ public class TransportDeleteUserAction extends HandledTransportAction>> sendingListener = ActionListener.wrap((userLists) -> { - users.addAll(userLists.stream().flatMap(Collection::stream).filter(Objects::nonNull).collect(Collectors.toList())); - listener.onResponse(new GetUsersResponse(users)); - }, listener::onFailure); - final GroupedActionListener> groupListener = - new GroupedActionListener<>(sendingListener, 2); + users.addAll(userLists.stream().flatMap(Collection::stream).filter(Objects::nonNull).collect(Collectors.toList())); + listener.onResponse(new GetUsersResponse(users)); + }, listener::onFailure); + final GroupedActionListener> groupListener = new GroupedActionListener<>(sendingListener, 2); // We have two sources for the users object, the reservedRealm and the usersStore, we query both at the same time with a // GroupedActionListener if (realmLookup.isEmpty()) { diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/user/TransportHasPrivilegesAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/user/TransportHasPrivilegesAction.java index 71144e2e880e8..7271a02fa3329 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/user/TransportHasPrivilegesAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/user/TransportHasPrivilegesAction.java @@ -11,10 +11,10 @@ import org.elasticsearch.action.support.HandledTransportAction; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.inject.Inject; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.tasks.Task; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.security.SecurityContext; import org.elasticsearch.xpack.core.security.action.user.HasPrivilegesAction; import org.elasticsearch.xpack.core.security.action.user.HasPrivilegesRequest; @@ -44,10 +44,15 @@ public class TransportHasPrivilegesAction extends HandledTransportAction - authorizationService.checkPrivileges(authentication, request, applicationPrivilegeDescriptors, listener), - listener::onFailure)); + resolveApplicationPrivileges( + request, + ActionListener.wrap( + applicationPrivilegeDescriptors -> authorizationService.checkPrivileges( + authentication, + request, + applicationPrivilegeDescriptors, + listener + ), + listener::onFailure + ) + ); } - private void resolveApplicationPrivileges(HasPrivilegesRequest request, - ActionListener> listener) { + private void resolveApplicationPrivileges( + HasPrivilegesRequest request, + ActionListener> listener + ) { final Set applications = getApplicationNames(request); privilegeStore.getPrivileges(applications, null, listener); } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/user/TransportPutUserAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/user/TransportPutUserAction.java index b84ceeaad93d7..a54c0b036271e 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/user/TransportPutUserAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/user/TransportPutUserAction.java @@ -33,8 +33,12 @@ public class TransportPutUserAction extends HandledTransportAction l.onResponse(ActionResponse.Empty.INSTANCE))); + usersStore.setEnabled( + username, + request.enabled(), + request.getRefreshPolicy(), + listener.delegateFailure((l, v) -> l.onResponse(ActionResponse.Empty.INSTANCE)) + ); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/audit/AuditTrail.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/audit/AuditTrail.java index a71ade0c8faca..259af77408aca 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/audit/AuditTrail.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/audit/AuditTrail.java @@ -43,11 +43,21 @@ public interface AuditTrail { void authenticationFailed(String requestId, String realm, AuthenticationToken token, RestRequest request); - void accessGranted(String requestId, Authentication authentication, String action, TransportRequest transportRequest, - AuthorizationInfo authorizationInfo); - - void accessDenied(String requestId, Authentication authentication, String action, TransportRequest transportRequest, - AuthorizationInfo authorizationInfo); + void accessGranted( + String requestId, + Authentication authentication, + String action, + TransportRequest transportRequest, + AuthorizationInfo authorizationInfo + ); + + void accessDenied( + String requestId, + Authentication authentication, + String action, + TransportRequest transportRequest, + AuthorizationInfo authorizationInfo + ); void tamperedRequest(String requestId, RestRequest request); @@ -65,14 +75,23 @@ void accessDenied(String requestId, Authentication authentication, String action void connectionDenied(InetAddress inetAddress, String profile, SecurityIpFilterRule rule); - void runAsGranted(String requestId, Authentication authentication, String action, TransportRequest transportRequest, - AuthorizationInfo authorizationInfo); + void runAsGranted( + String requestId, + Authentication authentication, + String action, + TransportRequest transportRequest, + AuthorizationInfo authorizationInfo + ); - void runAsDenied(String requestId, Authentication authentication, String action, TransportRequest transportRequest, - AuthorizationInfo authorizationInfo); + void runAsDenied( + String requestId, + Authentication authentication, + String action, + TransportRequest transportRequest, + AuthorizationInfo authorizationInfo + ); - void runAsDenied(String requestId, Authentication authentication, RestRequest request, - AuthorizationInfo authorizationInfo); + void runAsDenied(String requestId, Authentication authentication, RestRequest request, AuthorizationInfo authorizationInfo); /** * This is a "workaround" method to log index "access_granted" and "access_denied" events for actions not tied to a @@ -80,12 +99,25 @@ void runAsDenied(String requestId, Authentication authentication, RestRequest re * message. It is currently only used to audit the resolved index (alias) name for each {@code BulkItemRequest} comprised by a * {@code BulkShardRequest}. We should strive to not use this and TODO refactor it out! */ - void explicitIndexAccessEvent(String requestId, AuditLevel eventType, Authentication authentication, String action, String indices, - String requestName, TransportAddress remoteAddress, AuthorizationInfo authorizationInfo); + void explicitIndexAccessEvent( + String requestId, + AuditLevel eventType, + Authentication authentication, + String action, + String indices, + String requestName, + TransportAddress remoteAddress, + AuthorizationInfo authorizationInfo + ); // this is the only audit method that is called *after* the action executed, when the response is available // it is however *only called for coordinating actions*, which are the actions that a client invokes as opposed to // the actions that a node invokes in order to service a client request - void coordinatingActionResponse(String requestId, Authentication authentication, String action, TransportRequest transportRequest, - TransportResponse transportResponse); + void coordinatingActionResponse( + String requestId, + Authentication authentication, + String action, + TransportRequest transportRequest, + TransportResponse transportResponse + ); } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/audit/AuditTrailService.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/audit/AuditTrailService.java index e888a2db910f1..f89bb9a4d5b74 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/audit/AuditTrailService.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/audit/AuditTrailService.java @@ -65,8 +65,11 @@ private void maybeLogAuditingDisabled() { Instant nextLogInstant = nextLogInstantAtomic.get(); if (nextLogInstant.isBefore(nowInstant)) { if (nextLogInstantAtomic.compareAndSet(nextLogInstant, nowInstant.plus(minLogPeriod))) { - logger.warn("Auditing logging is DISABLED because the currently active license [" + - licenseState.getOperationMode() + "] does not permit it"); + logger.warn( + "Auditing logging is DISABLED because the currently active license [" + + licenseState.getOperationMode() + + "] does not permit it" + ); } } } @@ -82,8 +85,12 @@ public String name() { public void authenticationSuccess(String requestId, Authentication authentication, RestRequest request) {} @Override - public void authenticationSuccess(String requestId, Authentication authentication, String action, - TransportRequest transportRequest) {} + public void authenticationSuccess( + String requestId, + Authentication authentication, + String action, + TransportRequest transportRequest + ) {} @Override public void anonymousAccessDenied(String requestId, String action, TransportRequest transportRequest) {} @@ -104,19 +111,34 @@ public void authenticationFailed(String requestId, AuthenticationToken token, St public void authenticationFailed(String requestId, AuthenticationToken token, RestRequest request) {} @Override - public void authenticationFailed(String requestId, String realm, AuthenticationToken token, - String action, TransportRequest transportRequest) {} + public void authenticationFailed( + String requestId, + String realm, + AuthenticationToken token, + String action, + TransportRequest transportRequest + ) {} @Override public void authenticationFailed(String requestId, String realm, AuthenticationToken token, RestRequest request) {} @Override - public void accessGranted(String requestId, Authentication authentication, String action, TransportRequest transportRequest, - AuthorizationInfo authorizationInfo) {} + public void accessGranted( + String requestId, + Authentication authentication, + String action, + TransportRequest transportRequest, + AuthorizationInfo authorizationInfo + ) {} @Override - public void accessDenied(String requestId, Authentication authentication, String action, TransportRequest transportRequest, - AuthorizationInfo authorizationInfo) {} + public void accessDenied( + String requestId, + Authentication authentication, + String action, + TransportRequest transportRequest, + AuthorizationInfo authorizationInfo + ) {} @Override public void tamperedRequest(String requestId, RestRequest request) {} @@ -134,26 +156,51 @@ public void connectionGranted(InetAddress inetAddress, String profile, SecurityI public void connectionDenied(InetAddress inetAddress, String profile, SecurityIpFilterRule rule) {} @Override - public void runAsGranted(String requestId, Authentication authentication, String action, TransportRequest transportRequest, - AuthorizationInfo authorizationInfo) {} - - @Override - public void runAsDenied(String requestId, Authentication authentication, String action, TransportRequest transportRequest, - AuthorizationInfo authorizationInfo) {} - - @Override - public void runAsDenied(String requestId, Authentication authentication, RestRequest request, - AuthorizationInfo authorizationInfo) {} - - @Override - public void explicitIndexAccessEvent(String requestId, AuditLevel eventType, Authentication authentication, - String action, String indices, String requestName, TransportAddress remoteAddress, - AuthorizationInfo authorizationInfo) {} - - @Override - public void coordinatingActionResponse(String requestId, Authentication authentication, String action, - TransportRequest transportRequest, - TransportResponse transportResponse) { } + public void runAsGranted( + String requestId, + Authentication authentication, + String action, + TransportRequest transportRequest, + AuthorizationInfo authorizationInfo + ) {} + + @Override + public void runAsDenied( + String requestId, + Authentication authentication, + String action, + TransportRequest transportRequest, + AuthorizationInfo authorizationInfo + ) {} + + @Override + public void runAsDenied( + String requestId, + Authentication authentication, + RestRequest request, + AuthorizationInfo authorizationInfo + ) {} + + @Override + public void explicitIndexAccessEvent( + String requestId, + AuditLevel eventType, + Authentication authentication, + String action, + String indices, + String requestName, + TransportAddress remoteAddress, + AuthorizationInfo authorizationInfo + ) {} + + @Override + public void coordinatingActionResponse( + String requestId, + Authentication authentication, + String action, + TransportRequest transportRequest, + TransportResponse transportResponse + ) {} } private static class CompositeAuditTrail implements AuditTrail { @@ -181,8 +228,12 @@ public void authenticationSuccess(String requestId, Authentication authenticatio } @Override - public void authenticationSuccess(String requestId, Authentication authentication, String action, - TransportRequest transportRequest) { + public void authenticationSuccess( + String requestId, + Authentication authentication, + String action, + TransportRequest transportRequest + ) { for (AuditTrail auditTrail : auditTrails) { auditTrail.authenticationSuccess(requestId, authentication, action, transportRequest); } @@ -224,8 +275,13 @@ public void authenticationFailed(String requestId, AuthenticationToken token, St } @Override - public void authenticationFailed(String requestId, String realm, AuthenticationToken token, String action, - TransportRequest transportRequest) { + public void authenticationFailed( + String requestId, + String realm, + AuthenticationToken token, + String action, + TransportRequest transportRequest + ) { for (AuditTrail auditTrail : auditTrails) { auditTrail.authenticationFailed(requestId, realm, token, action, transportRequest); } @@ -246,25 +302,39 @@ public void authenticationFailed(String requestId, String realm, AuthenticationT } @Override - public void accessGranted(String requestId, Authentication authentication, String action, TransportRequest msg, - AuthorizationInfo authorizationInfo) { + public void accessGranted( + String requestId, + Authentication authentication, + String action, + TransportRequest msg, + AuthorizationInfo authorizationInfo + ) { for (AuditTrail auditTrail : auditTrails) { auditTrail.accessGranted(requestId, authentication, action, msg, authorizationInfo); } } @Override - public void accessDenied(String requestId, Authentication authentication, String action, TransportRequest transportRequest, - AuthorizationInfo authorizationInfo) { + public void accessDenied( + String requestId, + Authentication authentication, + String action, + TransportRequest transportRequest, + AuthorizationInfo authorizationInfo + ) { for (AuditTrail auditTrail : auditTrails) { auditTrail.accessDenied(requestId, authentication, action, transportRequest, authorizationInfo); } } @Override - public void coordinatingActionResponse(String requestId, Authentication authentication, String action, - TransportRequest transportRequest, - TransportResponse transportResponse) { + public void coordinatingActionResponse( + String requestId, + Authentication authentication, + String action, + TransportRequest transportRequest, + TransportResponse transportResponse + ) { for (AuditTrail auditTrail : auditTrails) { auditTrail.coordinatingActionResponse(requestId, authentication, action, transportRequest, transportResponse); } @@ -306,36 +376,60 @@ public void connectionDenied(InetAddress inetAddress, String profile, SecurityIp } @Override - public void runAsGranted(String requestId, Authentication authentication, String action, TransportRequest transportRequest, - AuthorizationInfo authorizationInfo) { + public void runAsGranted( + String requestId, + Authentication authentication, + String action, + TransportRequest transportRequest, + AuthorizationInfo authorizationInfo + ) { for (AuditTrail auditTrail : auditTrails) { auditTrail.runAsGranted(requestId, authentication, action, transportRequest, authorizationInfo); } } @Override - public void runAsDenied(String requestId, Authentication authentication, String action, TransportRequest transportRequest, - AuthorizationInfo authorizationInfo) { + public void runAsDenied( + String requestId, + Authentication authentication, + String action, + TransportRequest transportRequest, + AuthorizationInfo authorizationInfo + ) { for (AuditTrail auditTrail : auditTrails) { auditTrail.runAsDenied(requestId, authentication, action, transportRequest, authorizationInfo); } } @Override - public void runAsDenied(String requestId, Authentication authentication, RestRequest request, - AuthorizationInfo authorizationInfo) { + public void runAsDenied(String requestId, Authentication authentication, RestRequest request, AuthorizationInfo authorizationInfo) { for (AuditTrail auditTrail : auditTrails) { auditTrail.runAsDenied(requestId, authentication, request, authorizationInfo); } } @Override - public void explicitIndexAccessEvent(String requestId, AuditLevel eventType, Authentication authentication, String action, - String indices, String requestName, TransportAddress remoteAddress, - AuthorizationInfo authorizationInfo) { + public void explicitIndexAccessEvent( + String requestId, + AuditLevel eventType, + Authentication authentication, + String action, + String indices, + String requestName, + TransportAddress remoteAddress, + AuthorizationInfo authorizationInfo + ) { for (AuditTrail auditTrail : auditTrails) { - auditTrail.explicitIndexAccessEvent(requestId, eventType, authentication, action, indices, requestName, remoteAddress, - authorizationInfo); + auditTrail.explicitIndexAccessEvent( + requestId, + eventType, + authentication, + action, + indices, + requestName, + remoteAddress, + authorizationInfo + ); } } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/audit/AuditUtil.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/audit/AuditUtil.java index b14ff756e599f..7957b34368353 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/audit/AuditUtil.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/audit/AuditUtil.java @@ -62,8 +62,9 @@ private static String generateRequestId(ThreadContext threadContext, boolean che if (checkExisting) { final String existing = extractRequestId(threadContext); if (existing != null) { - throw new IllegalStateException("Cannot generate a new audit request id - existing id [" - + existing + "] already registered"); + throw new IllegalStateException( + "Cannot generate a new audit request id - existing id [" + existing + "] already registered" + ); } } final String requestId = UUIDs.randomBase64UUID(Randomness.get()); diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/audit/index/IndexNameResolver.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/audit/index/IndexNameResolver.java index f9d5498ee5066..7243362541128 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/audit/index/IndexNameResolver.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/audit/index/IndexNameResolver.java @@ -13,10 +13,10 @@ public class IndexNameResolver { public enum Rollover { - HOURLY ("-yyyy.MM.dd.HH"), - DAILY ("-yyyy.MM.dd"), - WEEKLY ("-yyyy.w"), - MONTHLY ("-yyyy.MM"); + HOURLY("-yyyy.MM.dd.HH"), + DAILY("-yyyy.MM.dd"), + WEEKLY("-yyyy.w"), + MONTHLY("-yyyy.MM"); private final DateTimeFormatter formatter; diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/audit/logfile/LoggingAuditTrail.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/audit/logfile/LoggingAuditTrail.java index 74f7b08eb7424..8609a49bc866c 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/audit/logfile/LoggingAuditTrail.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/audit/logfile/LoggingAuditTrail.java @@ -7,6 +7,7 @@ package org.elasticsearch.xpack.security.audit.logfile; import com.fasterxml.jackson.core.io.JsonStringEncoder; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Marker; @@ -21,7 +22,6 @@ import org.elasticsearch.cluster.ClusterStateListener; import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.logging.Loggers; import org.elasticsearch.common.network.NetworkAddress; @@ -29,18 +29,19 @@ import org.elasticsearch.common.settings.Setting.Property; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.TransportAddress; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.Maps; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.json.JsonXContent; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.node.Node; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.tasks.Task; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportRequest; import org.elasticsearch.transport.TransportResponse; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.security.action.CreateApiKeyAction; import org.elasticsearch.xpack.core.security.action.CreateApiKeyRequest; import org.elasticsearch.xpack.core.security.action.GrantApiKeyAction; @@ -184,54 +185,145 @@ public class LoggingAuditTrail implements AuditTrail, ClusterStateListener { public static final String INVALIDATE_API_KEYS_FIELD_NAME = "invalidate"; public static final String NAME = "logfile"; - public static final Setting EMIT_HOST_ADDRESS_SETTING = Setting.boolSetting(setting("audit.logfile.emit_node_host_address"), - false, Property.NodeScope, Property.Dynamic); - public static final Setting EMIT_HOST_NAME_SETTING = Setting.boolSetting(setting("audit.logfile.emit_node_host_name"), - false, Property.NodeScope, Property.Dynamic); - public static final Setting EMIT_NODE_NAME_SETTING = Setting.boolSetting(setting("audit.logfile.emit_node_name"), - false, Property.NodeScope, Property.Dynamic); - public static final Setting EMIT_NODE_ID_SETTING = Setting.boolSetting(setting("audit.logfile.emit_node_id"), true, - Property.NodeScope, Property.Dynamic); - private static final List DEFAULT_EVENT_INCLUDES = Arrays.asList(ACCESS_DENIED.toString(), ACCESS_GRANTED.toString(), - ANONYMOUS_ACCESS_DENIED.toString(), AUTHENTICATION_FAILED.toString(), CONNECTION_DENIED.toString(), TAMPERED_REQUEST.toString(), - RUN_AS_DENIED.toString(), RUN_AS_GRANTED.toString(), SECURITY_CONFIG_CHANGE.toString()); - public static final Setting> INCLUDE_EVENT_SETTINGS = Setting.listSetting(setting("audit.logfile.events.include"), - DEFAULT_EVENT_INCLUDES, Function.identity(), value -> AuditLevel.parse(value, List.of()), - Property.NodeScope, Property.Dynamic); - public static final Setting> EXCLUDE_EVENT_SETTINGS = Setting.listSetting(setting("audit.logfile.events.exclude"), - Collections.emptyList(), Function.identity(), value -> AuditLevel.parse(List.of(), value), - Property.NodeScope, Property.Dynamic); - public static final Setting INCLUDE_REQUEST_BODY = Setting.boolSetting(setting("audit.logfile.events.emit_request_body"), - false, Property.NodeScope, Property.Dynamic); + public static final Setting EMIT_HOST_ADDRESS_SETTING = Setting.boolSetting( + setting("audit.logfile.emit_node_host_address"), + false, + Property.NodeScope, + Property.Dynamic + ); + public static final Setting EMIT_HOST_NAME_SETTING = Setting.boolSetting( + setting("audit.logfile.emit_node_host_name"), + false, + Property.NodeScope, + Property.Dynamic + ); + public static final Setting EMIT_NODE_NAME_SETTING = Setting.boolSetting( + setting("audit.logfile.emit_node_name"), + false, + Property.NodeScope, + Property.Dynamic + ); + public static final Setting EMIT_NODE_ID_SETTING = Setting.boolSetting( + setting("audit.logfile.emit_node_id"), + true, + Property.NodeScope, + Property.Dynamic + ); + private static final List DEFAULT_EVENT_INCLUDES = Arrays.asList( + ACCESS_DENIED.toString(), + ACCESS_GRANTED.toString(), + ANONYMOUS_ACCESS_DENIED.toString(), + AUTHENTICATION_FAILED.toString(), + CONNECTION_DENIED.toString(), + TAMPERED_REQUEST.toString(), + RUN_AS_DENIED.toString(), + RUN_AS_GRANTED.toString(), + SECURITY_CONFIG_CHANGE.toString() + ); + public static final Setting> INCLUDE_EVENT_SETTINGS = Setting.listSetting( + setting("audit.logfile.events.include"), + DEFAULT_EVENT_INCLUDES, + Function.identity(), + value -> AuditLevel.parse(value, List.of()), + Property.NodeScope, + Property.Dynamic + ); + public static final Setting> EXCLUDE_EVENT_SETTINGS = Setting.listSetting( + setting("audit.logfile.events.exclude"), + Collections.emptyList(), + Function.identity(), + value -> AuditLevel.parse(List.of(), value), + Property.NodeScope, + Property.Dynamic + ); + public static final Setting INCLUDE_REQUEST_BODY = Setting.boolSetting( + setting("audit.logfile.events.emit_request_body"), + false, + Property.NodeScope, + Property.Dynamic + ); // actions (and their requests) that are audited as "security change" events - public static final Set SECURITY_CHANGE_ACTIONS = Set.of(PutUserAction.NAME, PutRoleAction.NAME, PutRoleMappingAction.NAME, - SetEnabledAction.NAME, ChangePasswordAction.NAME, CreateApiKeyAction.NAME, GrantApiKeyAction.NAME, PutPrivilegesAction.NAME, - DeleteUserAction.NAME, DeleteRoleAction.NAME, DeleteRoleMappingAction.NAME, InvalidateApiKeyAction.NAME, - DeletePrivilegesAction.NAME, CreateServiceAccountTokenAction.NAME, DeleteServiceAccountTokenAction.NAME); + public static final Set SECURITY_CHANGE_ACTIONS = Set.of( + PutUserAction.NAME, + PutRoleAction.NAME, + PutRoleMappingAction.NAME, + SetEnabledAction.NAME, + ChangePasswordAction.NAME, + CreateApiKeyAction.NAME, + GrantApiKeyAction.NAME, + PutPrivilegesAction.NAME, + DeleteUserAction.NAME, + DeleteRoleAction.NAME, + DeleteRoleMappingAction.NAME, + InvalidateApiKeyAction.NAME, + DeletePrivilegesAction.NAME, + CreateServiceAccountTokenAction.NAME, + DeleteServiceAccountTokenAction.NAME + ); private static final String FILTER_POLICY_PREFIX = setting("audit.logfile.events.ignore_filters."); // because of the default wildcard value (*) for the field filter, a policy with // an unspecified filter field will match events that have any value for that // particular field, as well as events with that particular field missing - protected static final Setting.AffixSetting> FILTER_POLICY_IGNORE_PRINCIPALS = - Setting.affixKeySetting(FILTER_POLICY_PREFIX, "users", - (key) -> Setting.listSetting(key, Collections.singletonList("*"), Function.identity(), - value -> EventFilterPolicy.parsePredicate(value), Property.NodeScope, Property.Dynamic)); - protected static final Setting.AffixSetting> FILTER_POLICY_IGNORE_REALMS = - Setting.affixKeySetting(FILTER_POLICY_PREFIX, "realms", - (key) -> Setting.listSetting(key, Collections.singletonList("*"), Function.identity(), - value -> EventFilterPolicy.parsePredicate(value), Property.NodeScope, Property.Dynamic)); - protected static final Setting.AffixSetting> FILTER_POLICY_IGNORE_ROLES = - Setting.affixKeySetting(FILTER_POLICY_PREFIX, "roles", - (key) -> Setting.listSetting(key, Collections.singletonList("*"), Function.identity(), - value -> EventFilterPolicy.parsePredicate(value), Property.NodeScope, Property.Dynamic)); - protected static final Setting.AffixSetting> FILTER_POLICY_IGNORE_INDICES = - Setting.affixKeySetting(FILTER_POLICY_PREFIX, "indices", - (key) -> Setting.listSetting(key, Collections.singletonList("*"), Function.identity(), - value -> EventFilterPolicy.parsePredicate(value), Property.NodeScope, Property.Dynamic)); - protected static final Setting.AffixSetting> FILTER_POLICY_IGNORE_ACTIONS = - Setting.affixKeySetting(FILTER_POLICY_PREFIX, "actions", - (key) -> Setting.listSetting(key, Collections.singletonList("*"), Function.identity(), - value -> EventFilterPolicy.parsePredicate(value), Property.NodeScope, Property.Dynamic)); + protected static final Setting.AffixSetting> FILTER_POLICY_IGNORE_PRINCIPALS = Setting.affixKeySetting( + FILTER_POLICY_PREFIX, + "users", + (key) -> Setting.listSetting( + key, + Collections.singletonList("*"), + Function.identity(), + value -> EventFilterPolicy.parsePredicate(value), + Property.NodeScope, + Property.Dynamic + ) + ); + protected static final Setting.AffixSetting> FILTER_POLICY_IGNORE_REALMS = Setting.affixKeySetting( + FILTER_POLICY_PREFIX, + "realms", + (key) -> Setting.listSetting( + key, + Collections.singletonList("*"), + Function.identity(), + value -> EventFilterPolicy.parsePredicate(value), + Property.NodeScope, + Property.Dynamic + ) + ); + protected static final Setting.AffixSetting> FILTER_POLICY_IGNORE_ROLES = Setting.affixKeySetting( + FILTER_POLICY_PREFIX, + "roles", + (key) -> Setting.listSetting( + key, + Collections.singletonList("*"), + Function.identity(), + value -> EventFilterPolicy.parsePredicate(value), + Property.NodeScope, + Property.Dynamic + ) + ); + protected static final Setting.AffixSetting> FILTER_POLICY_IGNORE_INDICES = Setting.affixKeySetting( + FILTER_POLICY_PREFIX, + "indices", + (key) -> Setting.listSetting( + key, + Collections.singletonList("*"), + Function.identity(), + value -> EventFilterPolicy.parsePredicate(value), + Property.NodeScope, + Property.Dynamic + ) + ); + protected static final Setting.AffixSetting> FILTER_POLICY_IGNORE_ACTIONS = Setting.affixKeySetting( + FILTER_POLICY_PREFIX, + "actions", + (key) -> Setting.listSetting( + key, + Collections.singletonList("*"), + Function.identity(), + value -> EventFilterPolicy.parsePredicate(value), + Property.NodeScope, + Property.Dynamic + ) + ); private static final Marker AUDIT_MARKER = MarkerManager.getMarker("org.elasticsearch.xpack.security.audit"); @@ -268,12 +360,21 @@ public LoggingAuditTrail(Settings settings, ClusterService clusterService, Threa // `entryCommonFields` and `includeRequestBody` writes happen-before! `events` is // always read before `entryCommonFields` and `includeRequestBody`. this.events = parse(INCLUDE_EVENT_SETTINGS.get(newSettings), EXCLUDE_EVENT_SETTINGS.get(newSettings)); - }, Arrays.asList(EMIT_HOST_ADDRESS_SETTING, EMIT_HOST_NAME_SETTING, EMIT_NODE_NAME_SETTING, EMIT_NODE_ID_SETTING, - INCLUDE_EVENT_SETTINGS, EXCLUDE_EVENT_SETTINGS, INCLUDE_REQUEST_BODY)); + }, + Arrays.asList( + EMIT_HOST_ADDRESS_SETTING, + EMIT_HOST_NAME_SETTING, + EMIT_NODE_NAME_SETTING, + EMIT_NODE_ID_SETTING, + INCLUDE_EVENT_SETTINGS, + EXCLUDE_EVENT_SETTINGS, + INCLUDE_REQUEST_BODY + ) + ); clusterService.getClusterSettings().addAffixUpdateConsumer(FILTER_POLICY_IGNORE_PRINCIPALS, (policyName, filtersList) -> { final Optional policy = eventFilterPolicyRegistry.get(policyName); final EventFilterPolicy newPolicy = policy.orElse(new EventFilterPolicy(policyName, settings)) - .changePrincipalsFilter(filtersList); + .changePrincipalsFilter(filtersList); this.eventFilterPolicyRegistry.set(policyName, newPolicy); }, (policyName, filtersList) -> EventFilterPolicy.parsePredicate(filtersList)); clusterService.getClusterSettings().addAffixUpdateConsumer(FILTER_POLICY_IGNORE_REALMS, (policyName, filtersList) -> { @@ -293,8 +394,7 @@ public LoggingAuditTrail(Settings settings, ClusterService clusterService, Threa }, (policyName, filtersList) -> EventFilterPolicy.parsePredicate(filtersList)); clusterService.getClusterSettings().addAffixUpdateConsumer(FILTER_POLICY_IGNORE_ACTIONS, (policyName, filtersList) -> { final Optional policy = eventFilterPolicyRegistry.get(policyName); - final EventFilterPolicy newPolicy = policy.orElse(new EventFilterPolicy(policyName, settings)). - changeActionsFilter(filtersList); + final EventFilterPolicy newPolicy = policy.orElse(new EventFilterPolicy(policyName, settings)).changeActionsFilter(filtersList); this.eventFilterPolicyRegistry.set(policyName, newPolicy); }, (policyName, filtersList) -> EventFilterPolicy.parsePredicate(filtersList)); // this log filter ensures that audit events are not filtered out because of the log level @@ -302,35 +402,43 @@ public LoggingAuditTrail(Settings settings, ClusterService clusterService, Threa MarkerFilter auditMarkerFilter = MarkerFilter.createFilter(AUDIT_MARKER.getName(), Result.ACCEPT, Result.NEUTRAL); ctx.addFilter(auditMarkerFilter); ctx.updateLoggers(); - clusterService.getClusterSettings().addSettingsUpdateConsumer(ignored -> { - LogManager.getLogger(Security.class).warn("Changing log level for [" + LoggingAuditTrail.class.getName() + "] has no effect"); - }, List.of(Loggers.LOG_LEVEL_SETTING.getConcreteSettingForNamespace(LoggingAuditTrail.class.getName()))); + clusterService.getClusterSettings() + .addSettingsUpdateConsumer( + ignored -> { + LogManager.getLogger(Security.class) + .warn("Changing log level for [" + LoggingAuditTrail.class.getName() + "] has no effect"); + }, + List.of(Loggers.LOG_LEVEL_SETTING.getConcreteSettingForNamespace(LoggingAuditTrail.class.getName())) + ); } @Override public void authenticationSuccess(String requestId, Authentication authentication, RestRequest request) { - if (events.contains(AUTHENTICATION_SUCCESS) && eventFilterPolicyRegistry.ignorePredicate() - .test(new AuditEventMetaInfo( + if (events.contains(AUTHENTICATION_SUCCESS) + && eventFilterPolicyRegistry.ignorePredicate() + .test( + new AuditEventMetaInfo( Optional.of(authentication.getUser()), // can be null for API keys created before version 7.7 Optional.ofNullable(ApiKeyService.getCreatorRealmName(authentication)), Optional.empty(), Optional.empty(), - Optional.empty())) == false) { + Optional.empty() + ) + ) == false) { // this is redundant information maintained for bwc purposes final String authnRealm = authentication.getAuthenticatedBy().getName(); - new LogEntryBuilder() - .with(EVENT_TYPE_FIELD_NAME, REST_ORIGIN_FIELD_VALUE) - .with(EVENT_ACTION_FIELD_NAME, "authentication_success") - .with(REALM_FIELD_NAME, authnRealm) - .withRestUriAndMethod(request) - .withRequestId(requestId) - .withAuthentication(authentication) - .withRestOrigin(request) - .withRequestBody(request) - .withOpaqueId(threadContext) - .withXForwardedFor(threadContext) - .build(); + new LogEntryBuilder().with(EVENT_TYPE_FIELD_NAME, REST_ORIGIN_FIELD_VALUE) + .with(EVENT_ACTION_FIELD_NAME, "authentication_success") + .with(REALM_FIELD_NAME, authnRealm) + .withRestUriAndMethod(request) + .withRequestId(requestId) + .withAuthentication(authentication) + .withRestOrigin(request) + .withRequestBody(request) + .withOpaqueId(threadContext) + .withXForwardedFor(threadContext) + .build(); } } @@ -339,25 +447,27 @@ public void authenticationSuccess(String requestId, Authentication authenticatio if (events.contains(AUTHENTICATION_SUCCESS)) { final Optional indices = indices(transportRequest); if (eventFilterPolicyRegistry.ignorePredicate() - .test(new AuditEventMetaInfo( - Optional.of(authentication.getUser()), - // can be null for API keys created before version 7.7 - Optional.ofNullable(ApiKeyService.getCreatorRealmName(authentication)), - Optional.empty(), - indices, - Optional.of(action))) == false) { - new LogEntryBuilder() - .with(EVENT_TYPE_FIELD_NAME, TRANSPORT_ORIGIN_FIELD_VALUE) - .with(EVENT_ACTION_FIELD_NAME, "authentication_success") - .with(ACTION_FIELD_NAME, action) - .with(REQUEST_NAME_FIELD_NAME, transportRequest.getClass().getSimpleName()) - .withRequestId(requestId) - .withAuthentication(authentication) - .withRestOrTransportOrigin(transportRequest, threadContext) - .with(INDICES_FIELD_NAME, indices.orElse(null)) - .withOpaqueId(threadContext) - .withXForwardedFor(threadContext) - .build(); + .test( + new AuditEventMetaInfo( + Optional.of(authentication.getUser()), + // can be null for API keys created before version 7.7 + Optional.ofNullable(ApiKeyService.getCreatorRealmName(authentication)), + Optional.empty(), + indices, + Optional.of(action) + ) + ) == false) { + new LogEntryBuilder().with(EVENT_TYPE_FIELD_NAME, TRANSPORT_ORIGIN_FIELD_VALUE) + .with(EVENT_ACTION_FIELD_NAME, "authentication_success") + .with(ACTION_FIELD_NAME, action) + .with(REQUEST_NAME_FIELD_NAME, transportRequest.getClass().getSimpleName()) + .withRequestId(requestId) + .withAuthentication(authentication) + .withRestOrTransportOrigin(transportRequest, threadContext) + .with(INDICES_FIELD_NAME, indices.orElse(null)) + .withOpaqueId(threadContext) + .withXForwardedFor(threadContext) + .build(); } } } @@ -367,18 +477,17 @@ public void anonymousAccessDenied(String requestId, String action, TransportRequ if (events.contains(ANONYMOUS_ACCESS_DENIED)) { final Optional indices = indices(transportRequest); if (eventFilterPolicyRegistry.ignorePredicate() - .test(new AuditEventMetaInfo(Optional.empty(), Optional.empty(), indices, Optional.of(action))) == false) { - new LogEntryBuilder() - .with(EVENT_TYPE_FIELD_NAME, TRANSPORT_ORIGIN_FIELD_VALUE) - .with(EVENT_ACTION_FIELD_NAME, "anonymous_access_denied") - .with(ACTION_FIELD_NAME, action) - .with(REQUEST_NAME_FIELD_NAME, transportRequest.getClass().getSimpleName()) - .withRequestId(requestId) - .withRestOrTransportOrigin(transportRequest, threadContext) - .with(INDICES_FIELD_NAME, indices.orElse(null)) - .withOpaqueId(threadContext) - .withXForwardedFor(threadContext) - .build(); + .test(new AuditEventMetaInfo(Optional.empty(), Optional.empty(), indices, Optional.of(action))) == false) { + new LogEntryBuilder().with(EVENT_TYPE_FIELD_NAME, TRANSPORT_ORIGIN_FIELD_VALUE) + .with(EVENT_ACTION_FIELD_NAME, "anonymous_access_denied") + .with(ACTION_FIELD_NAME, action) + .with(REQUEST_NAME_FIELD_NAME, transportRequest.getClass().getSimpleName()) + .withRequestId(requestId) + .withRestOrTransportOrigin(transportRequest, threadContext) + .with(INDICES_FIELD_NAME, indices.orElse(null)) + .withOpaqueId(threadContext) + .withXForwardedFor(threadContext) + .build(); } } } @@ -386,17 +495,16 @@ public void anonymousAccessDenied(String requestId, String action, TransportRequ @Override public void anonymousAccessDenied(String requestId, RestRequest request) { if (events.contains(ANONYMOUS_ACCESS_DENIED) - && eventFilterPolicyRegistry.ignorePredicate().test(AuditEventMetaInfo.EMPTY) == false) { - new LogEntryBuilder() - .with(EVENT_TYPE_FIELD_NAME, REST_ORIGIN_FIELD_VALUE) - .with(EVENT_ACTION_FIELD_NAME, "anonymous_access_denied") - .withRestUriAndMethod(request) - .withRestOrigin(request) - .withRequestBody(request) - .withRequestId(requestId) - .withOpaqueId(threadContext) - .withXForwardedFor(threadContext) - .build(); + && eventFilterPolicyRegistry.ignorePredicate().test(AuditEventMetaInfo.EMPTY) == false) { + new LogEntryBuilder().with(EVENT_TYPE_FIELD_NAME, REST_ORIGIN_FIELD_VALUE) + .with(EVENT_ACTION_FIELD_NAME, "anonymous_access_denied") + .withRestUriAndMethod(request) + .withRestOrigin(request) + .withRequestBody(request) + .withRequestId(requestId) + .withOpaqueId(threadContext) + .withXForwardedFor(threadContext) + .build(); } } @@ -405,9 +513,8 @@ public void authenticationFailed(String requestId, AuthenticationToken token, St if (events.contains(AUTHENTICATION_FAILED)) { final Optional indices = indices(transportRequest); if (eventFilterPolicyRegistry.ignorePredicate() - .test(new AuditEventMetaInfo(Optional.of(token), Optional.empty(), indices, Optional.of(action))) == false) { - final LogEntryBuilder logEntryBuilder = new LogEntryBuilder() - .with(EVENT_TYPE_FIELD_NAME, TRANSPORT_ORIGIN_FIELD_VALUE) + .test(new AuditEventMetaInfo(Optional.of(token), Optional.empty(), indices, Optional.of(action))) == false) { + final LogEntryBuilder logEntryBuilder = new LogEntryBuilder().with(EVENT_TYPE_FIELD_NAME, TRANSPORT_ORIGIN_FIELD_VALUE) .with(EVENT_ACTION_FIELD_NAME, "authentication_failed") .with(ACTION_FIELD_NAME, action) .with(PRINCIPAL_FIELD_NAME, token.principal()) @@ -428,16 +535,15 @@ public void authenticationFailed(String requestId, AuthenticationToken token, St @Override public void authenticationFailed(String requestId, RestRequest request) { if (events.contains(AUTHENTICATION_FAILED) && eventFilterPolicyRegistry.ignorePredicate().test(AuditEventMetaInfo.EMPTY) == false) { - new LogEntryBuilder() - .with(EVENT_TYPE_FIELD_NAME, REST_ORIGIN_FIELD_VALUE) - .with(EVENT_ACTION_FIELD_NAME, "authentication_failed") - .withRestUriAndMethod(request) - .withRestOrigin(request) - .withRequestBody(request) - .withRequestId(requestId) - .withOpaqueId(threadContext) - .withXForwardedFor(threadContext) - .build(); + new LogEntryBuilder().with(EVENT_TYPE_FIELD_NAME, REST_ORIGIN_FIELD_VALUE) + .with(EVENT_ACTION_FIELD_NAME, "authentication_failed") + .withRestUriAndMethod(request) + .withRestOrigin(request) + .withRequestBody(request) + .withRequestId(requestId) + .withOpaqueId(threadContext) + .withXForwardedFor(threadContext) + .build(); } } @@ -446,28 +552,27 @@ public void authenticationFailed(String requestId, String action, TransportReque if (events.contains(AUTHENTICATION_FAILED)) { final Optional indices = indices(transportRequest); if (eventFilterPolicyRegistry.ignorePredicate() - .test(new AuditEventMetaInfo(Optional.empty(), Optional.empty(), indices, Optional.of(action))) == false) { - new LogEntryBuilder() - .with(EVENT_TYPE_FIELD_NAME, TRANSPORT_ORIGIN_FIELD_VALUE) - .with(EVENT_ACTION_FIELD_NAME, "authentication_failed") - .with(ACTION_FIELD_NAME, action) - .with(REQUEST_NAME_FIELD_NAME, transportRequest.getClass().getSimpleName()) - .withRequestId(requestId) - .withRestOrTransportOrigin(transportRequest, threadContext) - .with(INDICES_FIELD_NAME, indices.orElse(null)) - .withOpaqueId(threadContext) - .withXForwardedFor(threadContext) - .build(); + .test(new AuditEventMetaInfo(Optional.empty(), Optional.empty(), indices, Optional.of(action))) == false) { + new LogEntryBuilder().with(EVENT_TYPE_FIELD_NAME, TRANSPORT_ORIGIN_FIELD_VALUE) + .with(EVENT_ACTION_FIELD_NAME, "authentication_failed") + .with(ACTION_FIELD_NAME, action) + .with(REQUEST_NAME_FIELD_NAME, transportRequest.getClass().getSimpleName()) + .withRequestId(requestId) + .withRestOrTransportOrigin(transportRequest, threadContext) + .with(INDICES_FIELD_NAME, indices.orElse(null)) + .withOpaqueId(threadContext) + .withXForwardedFor(threadContext) + .build(); } } } @Override public void authenticationFailed(String requestId, AuthenticationToken token, RestRequest request) { - if (events.contains(AUTHENTICATION_FAILED) && eventFilterPolicyRegistry.ignorePredicate() + if (events.contains(AUTHENTICATION_FAILED) + && eventFilterPolicyRegistry.ignorePredicate() .test(new AuditEventMetaInfo(Optional.of(token), Optional.empty(), Optional.empty(), Optional.empty())) == false) { - final LogEntryBuilder logEntryBuilder = new LogEntryBuilder() - .with(EVENT_TYPE_FIELD_NAME, REST_ORIGIN_FIELD_VALUE) + final LogEntryBuilder logEntryBuilder = new LogEntryBuilder().with(EVENT_TYPE_FIELD_NAME, REST_ORIGIN_FIELD_VALUE) .with(EVENT_ACTION_FIELD_NAME, "authentication_failed") .with(PRINCIPAL_FIELD_NAME, token.principal()) .withRestUriAndMethod(request) @@ -484,73 +589,87 @@ public void authenticationFailed(String requestId, AuthenticationToken token, Re } @Override - public void authenticationFailed(String requestId, String realm, AuthenticationToken token, String action, - TransportRequest transportRequest) { + public void authenticationFailed( + String requestId, + String realm, + AuthenticationToken token, + String action, + TransportRequest transportRequest + ) { if (events.contains(REALM_AUTHENTICATION_FAILED)) { final Optional indices = indices(transportRequest); if (eventFilterPolicyRegistry.ignorePredicate() - .test(new AuditEventMetaInfo(Optional.of(token), Optional.of(realm), indices, Optional.of(action))) == false) { - new LogEntryBuilder() - .with(EVENT_TYPE_FIELD_NAME, TRANSPORT_ORIGIN_FIELD_VALUE) - .with(EVENT_ACTION_FIELD_NAME, "realm_authentication_failed") - .with(REALM_FIELD_NAME, realm) - .with(PRINCIPAL_FIELD_NAME, token.principal()) - .with(ACTION_FIELD_NAME, action) - .with(REQUEST_NAME_FIELD_NAME, transportRequest.getClass().getSimpleName()) - .withRequestId(requestId) - .withRestOrTransportOrigin(transportRequest, threadContext) - .with(INDICES_FIELD_NAME, indices.orElse(null)) - .withOpaqueId(threadContext) - .withXForwardedFor(threadContext) - .build(); - } - } - } - - @Override - public void authenticationFailed(String requestId, String realm, AuthenticationToken token, RestRequest request) { - if (events.contains(REALM_AUTHENTICATION_FAILED) && eventFilterPolicyRegistry.ignorePredicate() - .test(new AuditEventMetaInfo(Optional.of(token), Optional.of(realm), Optional.empty(), Optional.empty())) == false) { - new LogEntryBuilder() - .with(EVENT_TYPE_FIELD_NAME, REST_ORIGIN_FIELD_VALUE) + .test(new AuditEventMetaInfo(Optional.of(token), Optional.of(realm), indices, Optional.of(action))) == false) { + new LogEntryBuilder().with(EVENT_TYPE_FIELD_NAME, TRANSPORT_ORIGIN_FIELD_VALUE) .with(EVENT_ACTION_FIELD_NAME, "realm_authentication_failed") .with(REALM_FIELD_NAME, realm) .with(PRINCIPAL_FIELD_NAME, token.principal()) - .withRestUriAndMethod(request) - .withRestOrigin(request) - .withRequestBody(request) + .with(ACTION_FIELD_NAME, action) + .with(REQUEST_NAME_FIELD_NAME, transportRequest.getClass().getSimpleName()) .withRequestId(requestId) + .withRestOrTransportOrigin(transportRequest, threadContext) + .with(INDICES_FIELD_NAME, indices.orElse(null)) .withOpaqueId(threadContext) .withXForwardedFor(threadContext) .build(); + } } } @Override - public void accessGranted(String requestId, Authentication authentication, String action, TransportRequest msg, - AuthorizationInfo authorizationInfo) { + public void authenticationFailed(String requestId, String realm, AuthenticationToken token, RestRequest request) { + if (events.contains(REALM_AUTHENTICATION_FAILED) + && eventFilterPolicyRegistry.ignorePredicate() + .test(new AuditEventMetaInfo(Optional.of(token), Optional.of(realm), Optional.empty(), Optional.empty())) == false) { + new LogEntryBuilder().with(EVENT_TYPE_FIELD_NAME, REST_ORIGIN_FIELD_VALUE) + .with(EVENT_ACTION_FIELD_NAME, "realm_authentication_failed") + .with(REALM_FIELD_NAME, realm) + .with(PRINCIPAL_FIELD_NAME, token.principal()) + .withRestUriAndMethod(request) + .withRestOrigin(request) + .withRequestBody(request) + .withRequestId(requestId) + .withOpaqueId(threadContext) + .withXForwardedFor(threadContext) + .build(); + } + } + + @Override + public void accessGranted( + String requestId, + Authentication authentication, + String action, + TransportRequest msg, + AuthorizationInfo authorizationInfo + ) { final User user = authentication.getUser(); final boolean isSystem = User.isInternal(user); if ((isSystem && events.contains(SYSTEM_ACCESS_GRANTED)) || ((isSystem == false) && events.contains(ACCESS_GRANTED))) { final Optional indices = indices(msg); - if (eventFilterPolicyRegistry.ignorePredicate().test(new AuditEventMetaInfo(Optional.of(user), - // can be null for API keys created before version 7.7 - Optional.ofNullable(ApiKeyService.getCreatorRealmName(authentication)), - Optional.of(authorizationInfo), indices, - Optional.of(action))) == false) { - new LogEntryBuilder() - .with(EVENT_TYPE_FIELD_NAME, TRANSPORT_ORIGIN_FIELD_VALUE) - .with(EVENT_ACTION_FIELD_NAME, "access_granted") - .with(ACTION_FIELD_NAME, action) - .with(REQUEST_NAME_FIELD_NAME, msg.getClass().getSimpleName()) - .withRequestId(requestId) - .withAuthentication(authentication) - .withRestOrTransportOrigin(msg, threadContext) - .with(INDICES_FIELD_NAME, indices.orElse(null)) - .withOpaqueId(threadContext) - .withXForwardedFor(threadContext) - .with(authorizationInfo.asMap()) - .build(); + if (eventFilterPolicyRegistry.ignorePredicate() + .test( + new AuditEventMetaInfo( + Optional.of(user), + // can be null for API keys created before version 7.7 + Optional.ofNullable(ApiKeyService.getCreatorRealmName(authentication)), + Optional.of(authorizationInfo), + indices, + Optional.of(action) + ) + ) == false) { + new LogEntryBuilder().with(EVENT_TYPE_FIELD_NAME, TRANSPORT_ORIGIN_FIELD_VALUE) + .with(EVENT_ACTION_FIELD_NAME, "access_granted") + .with(ACTION_FIELD_NAME, action) + .with(REQUEST_NAME_FIELD_NAME, msg.getClass().getSimpleName()) + .withRequestId(requestId) + .withAuthentication(authentication) + .withRestOrTransportOrigin(msg, threadContext) + .with(INDICES_FIELD_NAME, indices.orElse(null)) + .withOpaqueId(threadContext) + .withXForwardedFor(threadContext) + .with(authorizationInfo.asMap()) + .build(); } } // "Security config change" records are not filtered out by ignore policies (i.e. they are always printed). @@ -605,8 +724,13 @@ public void accessGranted(String requestId, Authentication authentication, Strin assert DeleteServiceAccountTokenAction.NAME.equals(action); securityChangeLogEntryBuilder(requestId).withRequestBody((DeleteServiceAccountTokenRequest) msg).build(); } else { - throw new IllegalStateException("Unknown message class type [" + msg.getClass().getSimpleName() + - "] for the \"security change\" action [" + action + "]"); + throw new IllegalStateException( + "Unknown message class type [" + + msg.getClass().getSimpleName() + + "] for the \"security change\" action [" + + action + + "]" + ); } } catch (IOException e) { throw new ElasticsearchSecurityException("Unexpected error while serializing event data", e); @@ -615,8 +739,16 @@ public void accessGranted(String requestId, Authentication authentication, Strin } @Override - public void explicitIndexAccessEvent(String requestId, AuditLevel eventType, Authentication authentication, String action, String index, - String requestName, TransportAddress remoteAddress, AuthorizationInfo authorizationInfo) { + public void explicitIndexAccessEvent( + String requestId, + AuditLevel eventType, + Authentication authentication, + String action, + String index, + String requestName, + TransportAddress remoteAddress, + AuthorizationInfo authorizationInfo + ) { assert eventType == ACCESS_DENIED || eventType == AuditLevel.ACCESS_GRANTED || eventType == SYSTEM_ACCESS_GRANTED; final String[] indices = index == null ? null : new String[] { index }; final User user = authentication.getUser(); @@ -625,30 +757,32 @@ public void explicitIndexAccessEvent(String requestId, AuditLevel eventType, Aut } if (events.contains(eventType)) { if (eventFilterPolicyRegistry.ignorePredicate() - .test(new AuditEventMetaInfo(Optional.of(user), - // can be null for API keys created before version 7.7 - Optional.ofNullable(ApiKeyService.getCreatorRealmName(authentication)), - Optional.of(authorizationInfo), Optional.ofNullable(indices), - Optional.of(action))) == false) { - final LogEntryBuilder logEntryBuilder = new LogEntryBuilder() - .with(EVENT_TYPE_FIELD_NAME, TRANSPORT_ORIGIN_FIELD_VALUE) - .with(EVENT_ACTION_FIELD_NAME, eventType == ACCESS_DENIED ? "access_denied" : "access_granted") - .with(ACTION_FIELD_NAME, action) - .with(REQUEST_NAME_FIELD_NAME, requestName) - .withRequestId(requestId) - .withAuthentication(authentication) - .with(INDICES_FIELD_NAME, indices) - .withOpaqueId(threadContext) - .withXForwardedFor(threadContext) - .with(authorizationInfo.asMap()); + .test( + new AuditEventMetaInfo( + Optional.of(user), + // can be null for API keys created before version 7.7 + Optional.ofNullable(ApiKeyService.getCreatorRealmName(authentication)), + Optional.of(authorizationInfo), + Optional.ofNullable(indices), + Optional.of(action) + ) + ) == false) { + final LogEntryBuilder logEntryBuilder = new LogEntryBuilder().with(EVENT_TYPE_FIELD_NAME, TRANSPORT_ORIGIN_FIELD_VALUE) + .with(EVENT_ACTION_FIELD_NAME, eventType == ACCESS_DENIED ? "access_denied" : "access_granted") + .with(ACTION_FIELD_NAME, action) + .with(REQUEST_NAME_FIELD_NAME, requestName) + .withRequestId(requestId) + .withAuthentication(authentication) + .with(INDICES_FIELD_NAME, indices) + .withOpaqueId(threadContext) + .withXForwardedFor(threadContext) + .with(authorizationInfo.asMap()); final InetSocketAddress restAddress = RemoteHostHeader.restRemoteAddress(threadContext); if (restAddress != null) { - logEntryBuilder - .with(ORIGIN_TYPE_FIELD_NAME, REST_ORIGIN_FIELD_VALUE) + logEntryBuilder.with(ORIGIN_TYPE_FIELD_NAME, REST_ORIGIN_FIELD_VALUE) .with(ORIGIN_ADDRESS_FIELD_NAME, NetworkAddress.format(restAddress)); } else if (remoteAddress != null) { - logEntryBuilder - .with(ORIGIN_TYPE_FIELD_NAME, TRANSPORT_ORIGIN_FIELD_VALUE) + logEntryBuilder.with(ORIGIN_TYPE_FIELD_NAME, TRANSPORT_ORIGIN_FIELD_VALUE) .with(ORIGIN_ADDRESS_FIELD_NAME, NetworkAddress.format(remoteAddress.address())); } logEntryBuilder.build(); @@ -657,28 +791,38 @@ public void explicitIndexAccessEvent(String requestId, AuditLevel eventType, Aut } @Override - public void accessDenied(String requestId, Authentication authentication, String action, TransportRequest transportRequest, - AuthorizationInfo authorizationInfo) { + public void accessDenied( + String requestId, + Authentication authentication, + String action, + TransportRequest transportRequest, + AuthorizationInfo authorizationInfo + ) { if (events.contains(ACCESS_DENIED)) { final Optional indices = indices(transportRequest); - if (eventFilterPolicyRegistry.ignorePredicate().test(new AuditEventMetaInfo(Optional.of(authentication.getUser()), - // can be null for API keys created before version 7.7 - Optional.ofNullable(ApiKeyService.getCreatorRealmName(authentication)), - Optional.of(authorizationInfo), indices, - Optional.of(action))) == false) { - new LogEntryBuilder() - .with(EVENT_TYPE_FIELD_NAME, TRANSPORT_ORIGIN_FIELD_VALUE) - .with(EVENT_ACTION_FIELD_NAME, "access_denied") - .with(ACTION_FIELD_NAME, action) - .with(REQUEST_NAME_FIELD_NAME, transportRequest.getClass().getSimpleName()) - .withRequestId(requestId) - .withAuthentication(authentication) - .withRestOrTransportOrigin(transportRequest, threadContext) - .with(INDICES_FIELD_NAME, indices.orElse(null)) - .with(authorizationInfo.asMap()) - .withOpaqueId(threadContext) - .withXForwardedFor(threadContext) - .build(); + if (eventFilterPolicyRegistry.ignorePredicate() + .test( + new AuditEventMetaInfo( + Optional.of(authentication.getUser()), + // can be null for API keys created before version 7.7 + Optional.ofNullable(ApiKeyService.getCreatorRealmName(authentication)), + Optional.of(authorizationInfo), + indices, + Optional.of(action) + ) + ) == false) { + new LogEntryBuilder().with(EVENT_TYPE_FIELD_NAME, TRANSPORT_ORIGIN_FIELD_VALUE) + .with(EVENT_ACTION_FIELD_NAME, "access_denied") + .with(ACTION_FIELD_NAME, action) + .with(REQUEST_NAME_FIELD_NAME, transportRequest.getClass().getSimpleName()) + .withRequestId(requestId) + .withAuthentication(authentication) + .withRestOrTransportOrigin(transportRequest, threadContext) + .with(INDICES_FIELD_NAME, indices.orElse(null)) + .with(authorizationInfo.asMap()) + .withOpaqueId(threadContext) + .withXForwardedFor(threadContext) + .build(); } } } @@ -686,16 +830,15 @@ public void accessDenied(String requestId, Authentication authentication, String @Override public void tamperedRequest(String requestId, RestRequest request) { if (events.contains(TAMPERED_REQUEST) && eventFilterPolicyRegistry.ignorePredicate().test(AuditEventMetaInfo.EMPTY) == false) { - new LogEntryBuilder() - .with(EVENT_TYPE_FIELD_NAME, REST_ORIGIN_FIELD_VALUE) - .with(EVENT_ACTION_FIELD_NAME, "tampered_request") - .withRestUriAndMethod(request) - .withRestOrigin(request) - .withRequestBody(request) - .withRequestId(requestId) - .withOpaqueId(threadContext) - .withXForwardedFor(threadContext) - .build(); + new LogEntryBuilder().with(EVENT_TYPE_FIELD_NAME, REST_ORIGIN_FIELD_VALUE) + .with(EVENT_ACTION_FIELD_NAME, "tampered_request") + .withRestUriAndMethod(request) + .withRestOrigin(request) + .withRequestBody(request) + .withRequestId(requestId) + .withOpaqueId(threadContext) + .withXForwardedFor(threadContext) + .build(); } } @@ -704,18 +847,17 @@ public void tamperedRequest(String requestId, String action, TransportRequest tr if (events.contains(TAMPERED_REQUEST)) { final Optional indices = indices(transportRequest); if (eventFilterPolicyRegistry.ignorePredicate() - .test(new AuditEventMetaInfo(Optional.empty(), Optional.empty(), indices, Optional.of(action))) == false) { - new LogEntryBuilder() - .with(EVENT_TYPE_FIELD_NAME, TRANSPORT_ORIGIN_FIELD_VALUE) - .with(EVENT_ACTION_FIELD_NAME, "tampered_request") - .with(ACTION_FIELD_NAME, action) - .with(REQUEST_NAME_FIELD_NAME, transportRequest.getClass().getSimpleName()) - .withRequestId(requestId) - .withRestOrTransportOrigin(transportRequest, threadContext) - .with(INDICES_FIELD_NAME, indices.orElse(null)) - .withOpaqueId(threadContext) - .withXForwardedFor(threadContext) - .build(); + .test(new AuditEventMetaInfo(Optional.empty(), Optional.empty(), indices, Optional.of(action))) == false) { + new LogEntryBuilder().with(EVENT_TYPE_FIELD_NAME, TRANSPORT_ORIGIN_FIELD_VALUE) + .with(EVENT_ACTION_FIELD_NAME, "tampered_request") + .with(ACTION_FIELD_NAME, action) + .with(REQUEST_NAME_FIELD_NAME, transportRequest.getClass().getSimpleName()) + .withRequestId(requestId) + .withRestOrTransportOrigin(transportRequest, threadContext) + .with(INDICES_FIELD_NAME, indices.orElse(null)) + .withOpaqueId(threadContext) + .withXForwardedFor(threadContext) + .build(); } } } @@ -724,24 +866,28 @@ public void tamperedRequest(String requestId, String action, TransportRequest tr public void tamperedRequest(String requestId, Authentication authentication, String action, TransportRequest transportRequest) { if (events.contains(TAMPERED_REQUEST)) { final Optional indices = indices(transportRequest); - if (eventFilterPolicyRegistry.ignorePredicate().test(new AuditEventMetaInfo( - Optional.of(authentication.getUser()), - // can be null for API keys created before version 7.7 - Optional.ofNullable(ApiKeyService.getCreatorRealmName(authentication)), - Optional.empty(), - indices, Optional.of(action))) == false) { - new LogEntryBuilder() - .with(EVENT_TYPE_FIELD_NAME, TRANSPORT_ORIGIN_FIELD_VALUE) - .with(EVENT_ACTION_FIELD_NAME, "tampered_request") - .with(ACTION_FIELD_NAME, action) - .with(REQUEST_NAME_FIELD_NAME, transportRequest.getClass().getSimpleName()) - .withRequestId(requestId) - .withRestOrTransportOrigin(transportRequest, threadContext) - .withAuthentication(authentication) - .with(INDICES_FIELD_NAME, indices.orElse(null)) - .withOpaqueId(threadContext) - .withXForwardedFor(threadContext) - .build(); + if (eventFilterPolicyRegistry.ignorePredicate() + .test( + new AuditEventMetaInfo( + Optional.of(authentication.getUser()), + // can be null for API keys created before version 7.7 + Optional.ofNullable(ApiKeyService.getCreatorRealmName(authentication)), + Optional.empty(), + indices, + Optional.of(action) + ) + ) == false) { + new LogEntryBuilder().with(EVENT_TYPE_FIELD_NAME, TRANSPORT_ORIGIN_FIELD_VALUE) + .with(EVENT_ACTION_FIELD_NAME, "tampered_request") + .with(ACTION_FIELD_NAME, action) + .with(REQUEST_NAME_FIELD_NAME, transportRequest.getClass().getSimpleName()) + .withRequestId(requestId) + .withRestOrTransportOrigin(transportRequest, threadContext) + .withAuthentication(authentication) + .with(INDICES_FIELD_NAME, indices.orElse(null)) + .withOpaqueId(threadContext) + .withXForwardedFor(threadContext) + .build(); } } } @@ -749,122 +895,154 @@ public void tamperedRequest(String requestId, Authentication authentication, Str @Override public void connectionGranted(InetAddress inetAddress, String profile, SecurityIpFilterRule rule) { if (events.contains(CONNECTION_GRANTED) && eventFilterPolicyRegistry.ignorePredicate().test(AuditEventMetaInfo.EMPTY) == false) { - new LogEntryBuilder() - .with(EVENT_TYPE_FIELD_NAME, IP_FILTER_ORIGIN_FIELD_VALUE) - .with(EVENT_ACTION_FIELD_NAME, "connection_granted") - .with(ORIGIN_TYPE_FIELD_NAME, - IPFilter.HTTP_PROFILE_NAME.equals(profile) ? REST_ORIGIN_FIELD_VALUE : TRANSPORT_ORIGIN_FIELD_VALUE) - .with(ORIGIN_ADDRESS_FIELD_NAME, NetworkAddress.format(inetAddress)) - .with(TRANSPORT_PROFILE_FIELD_NAME, profile) - .with(RULE_FIELD_NAME, rule.toString()) - .withOpaqueId(threadContext) - .withXForwardedFor(threadContext) - .build(); + new LogEntryBuilder().with(EVENT_TYPE_FIELD_NAME, IP_FILTER_ORIGIN_FIELD_VALUE) + .with(EVENT_ACTION_FIELD_NAME, "connection_granted") + .with( + ORIGIN_TYPE_FIELD_NAME, + IPFilter.HTTP_PROFILE_NAME.equals(profile) ? REST_ORIGIN_FIELD_VALUE : TRANSPORT_ORIGIN_FIELD_VALUE + ) + .with(ORIGIN_ADDRESS_FIELD_NAME, NetworkAddress.format(inetAddress)) + .with(TRANSPORT_PROFILE_FIELD_NAME, profile) + .with(RULE_FIELD_NAME, rule.toString()) + .withOpaqueId(threadContext) + .withXForwardedFor(threadContext) + .build(); } } @Override public void connectionDenied(InetAddress inetAddress, String profile, SecurityIpFilterRule rule) { if (events.contains(CONNECTION_DENIED) && eventFilterPolicyRegistry.ignorePredicate().test(AuditEventMetaInfo.EMPTY) == false) { - new LogEntryBuilder() - .with(EVENT_TYPE_FIELD_NAME, IP_FILTER_ORIGIN_FIELD_VALUE) - .with(EVENT_ACTION_FIELD_NAME, "connection_denied") - .with(ORIGIN_TYPE_FIELD_NAME, - IPFilter.HTTP_PROFILE_NAME.equals(profile) ? REST_ORIGIN_FIELD_VALUE : TRANSPORT_ORIGIN_FIELD_VALUE) - .with(ORIGIN_ADDRESS_FIELD_NAME, NetworkAddress.format(inetAddress)) - .with(TRANSPORT_PROFILE_FIELD_NAME, profile) - .with(RULE_FIELD_NAME, rule.toString()) - .withOpaqueId(threadContext) - .withXForwardedFor(threadContext) - .build(); + new LogEntryBuilder().with(EVENT_TYPE_FIELD_NAME, IP_FILTER_ORIGIN_FIELD_VALUE) + .with(EVENT_ACTION_FIELD_NAME, "connection_denied") + .with( + ORIGIN_TYPE_FIELD_NAME, + IPFilter.HTTP_PROFILE_NAME.equals(profile) ? REST_ORIGIN_FIELD_VALUE : TRANSPORT_ORIGIN_FIELD_VALUE + ) + .with(ORIGIN_ADDRESS_FIELD_NAME, NetworkAddress.format(inetAddress)) + .with(TRANSPORT_PROFILE_FIELD_NAME, profile) + .with(RULE_FIELD_NAME, rule.toString()) + .withOpaqueId(threadContext) + .withXForwardedFor(threadContext) + .build(); } } @Override - public void runAsGranted(String requestId, Authentication authentication, String action, TransportRequest transportRequest, - AuthorizationInfo authorizationInfo) { + public void runAsGranted( + String requestId, + Authentication authentication, + String action, + TransportRequest transportRequest, + AuthorizationInfo authorizationInfo + ) { if (events.contains(RUN_AS_GRANTED)) { final Optional indices = indices(transportRequest); - if (eventFilterPolicyRegistry.ignorePredicate().test(new AuditEventMetaInfo(Optional.of(authentication.getUser()), - // can be null for API keys created before version 7.7 - Optional.ofNullable(ApiKeyService.getCreatorRealmName(authentication)), - Optional.of(authorizationInfo), indices, Optional.of(action))) == false) { - new LogEntryBuilder() - .with(EVENT_TYPE_FIELD_NAME, TRANSPORT_ORIGIN_FIELD_VALUE) - .with(EVENT_ACTION_FIELD_NAME, "run_as_granted") - .with(ACTION_FIELD_NAME, action) - .with(REQUEST_NAME_FIELD_NAME, transportRequest.getClass().getSimpleName()) - .withRequestId(requestId) - .withRunAsSubject(authentication) - .withRestOrTransportOrigin(transportRequest, threadContext) - .with(INDICES_FIELD_NAME, indices.orElse(null)) - .with(authorizationInfo.asMap()) - .withOpaqueId(threadContext) - .withXForwardedFor(threadContext) - .build(); + if (eventFilterPolicyRegistry.ignorePredicate() + .test( + new AuditEventMetaInfo( + Optional.of(authentication.getUser()), + // can be null for API keys created before version 7.7 + Optional.ofNullable(ApiKeyService.getCreatorRealmName(authentication)), + Optional.of(authorizationInfo), + indices, + Optional.of(action) + ) + ) == false) { + new LogEntryBuilder().with(EVENT_TYPE_FIELD_NAME, TRANSPORT_ORIGIN_FIELD_VALUE) + .with(EVENT_ACTION_FIELD_NAME, "run_as_granted") + .with(ACTION_FIELD_NAME, action) + .with(REQUEST_NAME_FIELD_NAME, transportRequest.getClass().getSimpleName()) + .withRequestId(requestId) + .withRunAsSubject(authentication) + .withRestOrTransportOrigin(transportRequest, threadContext) + .with(INDICES_FIELD_NAME, indices.orElse(null)) + .with(authorizationInfo.asMap()) + .withOpaqueId(threadContext) + .withXForwardedFor(threadContext) + .build(); } } } @Override - public void runAsDenied(String requestId, Authentication authentication, String action, TransportRequest transportRequest, - AuthorizationInfo authorizationInfo) { + public void runAsDenied( + String requestId, + Authentication authentication, + String action, + TransportRequest transportRequest, + AuthorizationInfo authorizationInfo + ) { if (events.contains(RUN_AS_DENIED)) { final Optional indices = indices(transportRequest); - if (eventFilterPolicyRegistry.ignorePredicate().test(new AuditEventMetaInfo(Optional.of(authentication.getUser()), - // can be null for API keys created before version 7.7 - Optional.ofNullable(ApiKeyService.getCreatorRealmName(authentication)), - Optional.of(authorizationInfo), indices, Optional.of(action))) == false) { - new LogEntryBuilder() - .with(EVENT_TYPE_FIELD_NAME, TRANSPORT_ORIGIN_FIELD_VALUE) - .with(EVENT_ACTION_FIELD_NAME, "run_as_denied") - .with(ACTION_FIELD_NAME, action) - .with(REQUEST_NAME_FIELD_NAME, transportRequest.getClass().getSimpleName()) - .withRequestId(requestId) - .withRunAsSubject(authentication) - .withRestOrTransportOrigin(transportRequest, threadContext) - .with(INDICES_FIELD_NAME, indices.orElse(null)) - .with(authorizationInfo.asMap()) - .withOpaqueId(threadContext) - .withXForwardedFor(threadContext) - .build(); + if (eventFilterPolicyRegistry.ignorePredicate() + .test( + new AuditEventMetaInfo( + Optional.of(authentication.getUser()), + // can be null for API keys created before version 7.7 + Optional.ofNullable(ApiKeyService.getCreatorRealmName(authentication)), + Optional.of(authorizationInfo), + indices, + Optional.of(action) + ) + ) == false) { + new LogEntryBuilder().with(EVENT_TYPE_FIELD_NAME, TRANSPORT_ORIGIN_FIELD_VALUE) + .with(EVENT_ACTION_FIELD_NAME, "run_as_denied") + .with(ACTION_FIELD_NAME, action) + .with(REQUEST_NAME_FIELD_NAME, transportRequest.getClass().getSimpleName()) + .withRequestId(requestId) + .withRunAsSubject(authentication) + .withRestOrTransportOrigin(transportRequest, threadContext) + .with(INDICES_FIELD_NAME, indices.orElse(null)) + .with(authorizationInfo.asMap()) + .withOpaqueId(threadContext) + .withXForwardedFor(threadContext) + .build(); } } } @Override public void runAsDenied(String requestId, Authentication authentication, RestRequest request, AuthorizationInfo authorizationInfo) { - if (events.contains(RUN_AS_DENIED) && eventFilterPolicyRegistry.ignorePredicate().test( - new AuditEventMetaInfo(Optional.of(authentication.getUser()), + if (events.contains(RUN_AS_DENIED) + && eventFilterPolicyRegistry.ignorePredicate() + .test( + new AuditEventMetaInfo( + Optional.of(authentication.getUser()), // can be null for API keys created before version 7.7 Optional.ofNullable(ApiKeyService.getCreatorRealmName(authentication)), - Optional.of(authorizationInfo), Optional.empty(), Optional.empty())) == false) { - new LogEntryBuilder() - .with(EVENT_TYPE_FIELD_NAME, REST_ORIGIN_FIELD_VALUE) - .with(EVENT_ACTION_FIELD_NAME, "run_as_denied") - .with(authorizationInfo.asMap()) - .withRestUriAndMethod(request) - .withRunAsSubject(authentication) - .withRestOrigin(request) - .withRequestBody(request) - .withRequestId(requestId) - .withOpaqueId(threadContext) - .withXForwardedFor(threadContext) - .build(); + Optional.of(authorizationInfo), + Optional.empty(), + Optional.empty() + ) + ) == false) { + new LogEntryBuilder().with(EVENT_TYPE_FIELD_NAME, REST_ORIGIN_FIELD_VALUE) + .with(EVENT_ACTION_FIELD_NAME, "run_as_denied") + .with(authorizationInfo.asMap()) + .withRestUriAndMethod(request) + .withRunAsSubject(authentication) + .withRestOrigin(request) + .withRequestBody(request) + .withRequestId(requestId) + .withOpaqueId(threadContext) + .withXForwardedFor(threadContext) + .build(); } } @Override - public void coordinatingActionResponse(String requestId, Authentication authentication, String action, - TransportRequest transportRequest, - TransportResponse transportResponse) { + public void coordinatingActionResponse( + String requestId, + Authentication authentication, + String action, + TransportRequest transportRequest, + TransportResponse transportResponse + ) { // not implemented yet } private LogEntryBuilder securityChangeLogEntryBuilder(String requestId) { - return new LogEntryBuilder(false) - .with(EVENT_TYPE_FIELD_NAME, SECURITY_CHANGE_ORIGIN_FIELD_VALUE) - .withRequestId(requestId); + return new LogEntryBuilder(false).with(EVENT_TYPE_FIELD_NAME, SECURITY_CHANGE_ORIGIN_FIELD_VALUE).withRequestId(requestId); } private class LogEntryBuilder { @@ -887,27 +1065,27 @@ LogEntryBuilder withRequestBody(PutUserRequest putUserRequest) throws IOExceptio logEntry.with(EVENT_ACTION_FIELD_NAME, "put_user"); XContentBuilder builder = JsonXContent.contentBuilder().humanReadable(true); builder.startObject() - .startObject("user") - .field("name", putUserRequest.username()) - .field("enabled", putUserRequest.enabled()) - .array("roles", putUserRequest.roles()); - if (putUserRequest.fullName() != null) { - builder.field("full_name", putUserRequest.fullName()); - } - if (putUserRequest.email() != null) { - builder.field("email", putUserRequest.email()); - } - // password and password hashes are not exposed in the audit log - builder.field("has_password", putUserRequest.passwordHash() != null); - if (putUserRequest.metadata() != null && false == putUserRequest.metadata().isEmpty()) { - // JSON building for the metadata might fail when encountering unknown class types. - // This is NOT a problem because such metadata (eg containing GeoPoint) will most probably - // cause troubles in downstream code (eg storing the metadata), so this simply introduces a new failure mode. - // Also the malevolent metadata can only be produced by the transport client. - builder.field("metadata", putUserRequest.metadata()); - } - builder.endObject() // user - .endObject(); + .startObject("user") + .field("name", putUserRequest.username()) + .field("enabled", putUserRequest.enabled()) + .array("roles", putUserRequest.roles()); + if (putUserRequest.fullName() != null) { + builder.field("full_name", putUserRequest.fullName()); + } + if (putUserRequest.email() != null) { + builder.field("email", putUserRequest.email()); + } + // password and password hashes are not exposed in the audit log + builder.field("has_password", putUserRequest.passwordHash() != null); + if (putUserRequest.metadata() != null && false == putUserRequest.metadata().isEmpty()) { + // JSON building for the metadata might fail when encountering unknown class types. + // This is NOT a problem because such metadata (eg containing GeoPoint) will most probably + // cause troubles in downstream code (eg storing the metadata), so this simply introduces a new failure mode. + // Also the malevolent metadata can only be produced by the transport client. + builder.field("metadata", putUserRequest.metadata()); + } + builder.endObject() // user + .endObject(); logEntry.with(PUT_CONFIG_FIELD_NAME, Strings.toString(builder)); return this; } @@ -916,12 +1094,12 @@ LogEntryBuilder withRequestBody(ChangePasswordRequest changePasswordRequest) thr logEntry.with(EVENT_ACTION_FIELD_NAME, "change_password"); XContentBuilder builder = JsonXContent.contentBuilder().humanReadable(true); builder.startObject() - .startObject("password") - .startObject("user") - .field("name", changePasswordRequest.username()) - .endObject() // user - .endObject() // password - .endObject(); + .startObject("password") + .startObject("user") + .field("name", changePasswordRequest.username()) + .endObject() // user + .endObject() // password + .endObject(); logEntry.with(CHANGE_CONFIG_FIELD_NAME, Strings.toString(builder)); return this; } @@ -930,14 +1108,14 @@ LogEntryBuilder withRequestBody(PutRoleRequest putRoleRequest) throws IOExceptio logEntry.with(EVENT_ACTION_FIELD_NAME, "put_role"); XContentBuilder builder = JsonXContent.contentBuilder().humanReadable(true); builder.startObject() - .startObject("role") - .field("name", putRoleRequest.name()) - // the "role_descriptor" nested structure, where the "name" is left out, is closer to the event structure - // for creating API Keys - .field("role_descriptor"); + .startObject("role") + .field("name", putRoleRequest.name()) + // the "role_descriptor" nested structure, where the "name" is left out, is closer to the event structure + // for creating API Keys + .field("role_descriptor"); withRoleDescriptor(builder, putRoleRequest.roleDescriptor()); builder.endObject() // role - .endObject(); + .endObject(); logEntry.with(PUT_CONFIG_FIELD_NAME, Strings.toString(builder)); return this; } @@ -945,24 +1123,21 @@ LogEntryBuilder withRequestBody(PutRoleRequest putRoleRequest) throws IOExceptio LogEntryBuilder withRequestBody(PutRoleMappingRequest putRoleMappingRequest) throws IOException { logEntry.with(EVENT_ACTION_FIELD_NAME, "put_role_mapping"); XContentBuilder builder = JsonXContent.contentBuilder().humanReadable(true); - builder.startObject() - .startObject("role_mapping") - .field("name", putRoleMappingRequest.getName()); - if (putRoleMappingRequest.getRoles() != null && false == putRoleMappingRequest.getRoles().isEmpty()) { - builder.field("roles", putRoleMappingRequest.getRoles()); - } - if (putRoleMappingRequest.getRoleTemplates() != null && false == putRoleMappingRequest.getRoleTemplates().isEmpty()) { - // the toXContent method of the {@code TemplateRoleName} does a good job - builder.field("role_templates", putRoleMappingRequest.getRoleTemplates()); - } - // the toXContent methods of the {@code RoleMapperExpression} instances do a good job - builder.field("rules", putRoleMappingRequest.getRules()) - .field("enabled", putRoleMappingRequest.isEnabled()); - if (putRoleMappingRequest.getMetadata() != null && false == putRoleMappingRequest.getMetadata().isEmpty()) { - builder.field("metadata", putRoleMappingRequest.getMetadata()); - } - builder.endObject() // role_mapping - .endObject(); + builder.startObject().startObject("role_mapping").field("name", putRoleMappingRequest.getName()); + if (putRoleMappingRequest.getRoles() != null && false == putRoleMappingRequest.getRoles().isEmpty()) { + builder.field("roles", putRoleMappingRequest.getRoles()); + } + if (putRoleMappingRequest.getRoleTemplates() != null && false == putRoleMappingRequest.getRoleTemplates().isEmpty()) { + // the toXContent method of the {@code TemplateRoleName} does a good job + builder.field("role_templates", putRoleMappingRequest.getRoleTemplates()); + } + // the toXContent methods of the {@code RoleMapperExpression} instances do a good job + builder.field("rules", putRoleMappingRequest.getRules()).field("enabled", putRoleMappingRequest.isEnabled()); + if (putRoleMappingRequest.getMetadata() != null && false == putRoleMappingRequest.getMetadata().isEmpty()) { + builder.field("metadata", putRoleMappingRequest.getMetadata()); + } + builder.endObject() // role_mapping + .endObject(); logEntry.with(PUT_CONFIG_FIELD_NAME, Strings.toString(builder)); return this; } @@ -972,21 +1147,21 @@ LogEntryBuilder withRequestBody(SetEnabledRequest setEnabledRequest) throws IOEx // setEnabledRequest#enabled cannot be `null`, but nevertheless we should not assume it at this layer if (setEnabledRequest.enabled() != null && setEnabledRequest.enabled()) { builder.startObject() - .startObject("enable") - .startObject("user") - .field("name", setEnabledRequest.username()) - .endObject() // user - .endObject() // enable - .endObject(); + .startObject("enable") + .startObject("user") + .field("name", setEnabledRequest.username()) + .endObject() // user + .endObject() // enable + .endObject(); logEntry.with(EVENT_ACTION_FIELD_NAME, "change_enable_user"); } else { builder.startObject() - .startObject("disable") - .startObject("user") - .field("name", setEnabledRequest.username()) - .endObject() // user - .endObject() // disable - .endObject(); + .startObject("disable") + .startObject("user") + .field("name", setEnabledRequest.username()) + .endObject() // user + .endObject() // disable + .endObject(); logEntry.with(EVENT_ACTION_FIELD_NAME, "change_disable_user"); } logEntry.with(CHANGE_CONFIG_FIELD_NAME, Strings.toString(builder)); @@ -997,9 +1172,9 @@ LogEntryBuilder withRequestBody(PutPrivilegesRequest putPrivilegesRequest) throw logEntry.with(EVENT_ACTION_FIELD_NAME, "put_privileges"); XContentBuilder builder = JsonXContent.contentBuilder().humanReadable(true); builder.startObject() - // toXContent of {@code ApplicationPrivilegeDescriptor} does a good job - .field("privileges", putPrivilegesRequest.getPrivileges()) - .endObject(); + // toXContent of {@code ApplicationPrivilegeDescriptor} does a good job + .field("privileges", putPrivilegesRequest.getPrivileges()) + .endObject(); logEntry.with(PUT_CONFIG_FIELD_NAME, Strings.toString(builder)); return this; } @@ -1020,18 +1195,17 @@ LogEntryBuilder withRequestBody(GrantApiKeyRequest grantApiKeyRequest) throws IO builder.startObject(); withRequestBody(builder, grantApiKeyRequest.getApiKeyRequest()); GrantApiKeyRequest.Grant grant = grantApiKeyRequest.getGrant(); - builder.startObject("grant") - .field("type", grant.getType()); - if (grant.getUsername() != null) { - builder.startObject("user") - .field("name", grant.getUsername()) - .field("has_password", grant.getPassword() != null) - .endObject(); // user - } - if (grant.getAccessToken() != null) { - builder.field("has_access_token", grant.getAccessToken() != null); - } - builder.endObject(); // grant + builder.startObject("grant").field("type", grant.getType()); + if (grant.getUsername() != null) { + builder.startObject("user") + .field("name", grant.getUsername()) + .field("has_password", grant.getPassword() != null) + .endObject(); // user + } + if (grant.getAccessToken() != null) { + builder.field("has_access_token", grant.getAccessToken() != null); + } + builder.endObject(); // grant builder.endObject(); logEntry.with(CREATE_CONFIG_FIELD_NAME, Strings.toString(builder)); return this; @@ -1040,26 +1214,28 @@ LogEntryBuilder withRequestBody(GrantApiKeyRequest grantApiKeyRequest) throws IO private void withRequestBody(XContentBuilder builder, CreateApiKeyRequest createApiKeyRequest) throws IOException { TimeValue expiration = createApiKeyRequest.getExpiration(); builder.startObject("apikey") - .field("name", createApiKeyRequest.getName()) - .field("expiration", expiration != null ? expiration.toString() : null) - .startArray("role_descriptors"); + .field("name", createApiKeyRequest.getName()) + .field("expiration", expiration != null ? expiration.toString() : null) + .startArray("role_descriptors"); for (RoleDescriptor roleDescriptor : createApiKeyRequest.getRoleDescriptors()) { withRoleDescriptor(builder, roleDescriptor); } builder.endArray() // role_descriptors - .endObject(); // apikey + .endObject(); // apikey } private void withRoleDescriptor(XContentBuilder builder, RoleDescriptor roleDescriptor) throws IOException { - builder.startObject() - .array(RoleDescriptor.Fields.CLUSTER.getPreferredName(), roleDescriptor.getClusterPrivileges()); + builder.startObject().array(RoleDescriptor.Fields.CLUSTER.getPreferredName(), roleDescriptor.getClusterPrivileges()); if (roleDescriptor.getConditionalClusterPrivileges() != null && roleDescriptor.getConditionalClusterPrivileges().length > 0) { // This fails if this list contains multiple instances of the {@code ManageApplicationPrivileges} // Again, only the transport client can produce this, and this only introduces a different failure mode and // not a new one (i.e. without auditing it would fail differently, but it would still fail) builder.field(RoleDescriptor.Fields.GLOBAL.getPreferredName()); - ConfigurableClusterPrivileges.toXContent(builder, ToXContent.EMPTY_PARAMS, - Arrays.asList(roleDescriptor.getConditionalClusterPrivileges())); + ConfigurableClusterPrivileges.toXContent( + builder, + ToXContent.EMPTY_PARAMS, + Arrays.asList(roleDescriptor.getConditionalClusterPrivileges()) + ); } builder.startArray(RoleDescriptor.Fields.INDICES.getPreferredName()); for (RoleDescriptor.IndicesPrivileges indicesPrivileges : roleDescriptor.getIndicesPrivileges()) { @@ -1107,10 +1283,10 @@ LogEntryBuilder withRequestBody(DeleteUserRequest deleteUserRequest) throws IOEx logEntry.with(EVENT_ACTION_FIELD_NAME, "delete_user"); XContentBuilder builder = JsonXContent.contentBuilder().humanReadable(true); builder.startObject() - .startObject("user") - .field("name", deleteUserRequest.username()) - .endObject() // user - .endObject(); + .startObject("user") + .field("name", deleteUserRequest.username()) + .endObject() // user + .endObject(); logEntry.with(DELETE_CONFIG_FIELD_NAME, Strings.toString(builder)); return this; } @@ -1119,10 +1295,10 @@ LogEntryBuilder withRequestBody(DeleteRoleRequest deleteRoleRequest) throws IOEx logEntry.with(EVENT_ACTION_FIELD_NAME, "delete_role"); XContentBuilder builder = JsonXContent.contentBuilder().humanReadable(true); builder.startObject() - .startObject("role") - .field("name", deleteRoleRequest.name()) - .endObject() // role - .endObject(); + .startObject("role") + .field("name", deleteRoleRequest.name()) + .endObject() // role + .endObject(); logEntry.with(DELETE_CONFIG_FIELD_NAME, Strings.toString(builder)); return this; } @@ -1131,10 +1307,10 @@ LogEntryBuilder withRequestBody(DeleteRoleMappingRequest deleteRoleMappingReques logEntry.with(EVENT_ACTION_FIELD_NAME, "delete_role_mapping"); XContentBuilder builder = JsonXContent.contentBuilder().humanReadable(true); builder.startObject() - .startObject("role_mapping") - .field("name", deleteRoleMappingRequest.getName()) - .endObject() // role_mapping - .endObject(); + .startObject("role_mapping") + .field("name", deleteRoleMappingRequest.getName()) + .endObject() // role_mapping + .endObject(); logEntry.with(DELETE_CONFIG_FIELD_NAME, Strings.toString(builder)); return this; } @@ -1142,23 +1318,22 @@ LogEntryBuilder withRequestBody(DeleteRoleMappingRequest deleteRoleMappingReques LogEntryBuilder withRequestBody(InvalidateApiKeyRequest invalidateApiKeyRequest) throws IOException { logEntry.with(EVENT_ACTION_FIELD_NAME, "invalidate_apikeys"); XContentBuilder builder = JsonXContent.contentBuilder().humanReadable(true); - builder.startObject() - .startObject("apikeys"); - if (invalidateApiKeyRequest.getIds() != null && invalidateApiKeyRequest.getIds().length > 0) { - builder.array("ids", invalidateApiKeyRequest.getIds()); - } - if (Strings.hasLength(invalidateApiKeyRequest.getName())) { - builder.field("name", invalidateApiKeyRequest.getName()); - } - builder.field("owned_by_authenticated_user", invalidateApiKeyRequest.ownedByAuthenticatedUser()); - if (Strings.hasLength(invalidateApiKeyRequest.getUserName()) || Strings.hasLength(invalidateApiKeyRequest.getRealmName())) { - builder.startObject("user") - .field("name", invalidateApiKeyRequest.getUserName()) - .field("realm", invalidateApiKeyRequest.getRealmName()) - .endObject(); // user - } + builder.startObject().startObject("apikeys"); + if (invalidateApiKeyRequest.getIds() != null && invalidateApiKeyRequest.getIds().length > 0) { + builder.array("ids", invalidateApiKeyRequest.getIds()); + } + if (Strings.hasLength(invalidateApiKeyRequest.getName())) { + builder.field("name", invalidateApiKeyRequest.getName()); + } + builder.field("owned_by_authenticated_user", invalidateApiKeyRequest.ownedByAuthenticatedUser()); + if (Strings.hasLength(invalidateApiKeyRequest.getUserName()) || Strings.hasLength(invalidateApiKeyRequest.getRealmName())) { + builder.startObject("user") + .field("name", invalidateApiKeyRequest.getUserName()) + .field("realm", invalidateApiKeyRequest.getRealmName()) + .endObject(); // user + } builder.endObject() // apikeys - .endObject(); + .endObject(); logEntry.with(INVALIDATE_API_KEYS_FIELD_NAME, Strings.toString(builder)); return this; } @@ -1167,11 +1342,11 @@ LogEntryBuilder withRequestBody(DeletePrivilegesRequest deletePrivilegesRequest) logEntry.with(EVENT_ACTION_FIELD_NAME, "delete_privileges"); XContentBuilder builder = JsonXContent.contentBuilder().humanReadable(true); builder.startObject() - .startObject("privileges") - .field("application", deletePrivilegesRequest.application()) - .array("privileges", deletePrivilegesRequest.privileges()) - .endObject() // privileges - .endObject(); + .startObject("privileges") + .field("application", deletePrivilegesRequest.application()) + .array("privileges", deletePrivilegesRequest.privileges()) + .endObject() // privileges + .endObject(); logEntry.with(DELETE_CONFIG_FIELD_NAME, Strings.toString(builder)); return this; } @@ -1224,8 +1399,8 @@ LogEntryBuilder withRestUriAndMethod(RestRequest request) { LogEntryBuilder withRunAsSubject(Authentication authentication) { logEntry.with(PRINCIPAL_FIELD_NAME, authentication.getUser().authenticatedUser().principal()) - .with(PRINCIPAL_REALM_FIELD_NAME, authentication.getAuthenticatedBy().getName()) - .with(PRINCIPAL_RUN_AS_FIELD_NAME, authentication.getUser().principal()); + .with(PRINCIPAL_REALM_FIELD_NAME, authentication.getAuthenticatedBy().getName()) + .with(PRINCIPAL_RUN_AS_FIELD_NAME, authentication.getUser().principal()); if (authentication.getLookedUpBy() != null) { logEntry.with(PRINCIPAL_RUN_AS_REALM_FIELD_NAME, authentication.getLookedUpBy().getName()); } @@ -1237,7 +1412,7 @@ LogEntryBuilder withRestOrigin(RestRequest request) { final InetSocketAddress socketAddress = request.getHttpChannel().getRemoteAddress(); if (socketAddress != null) { logEntry.with(ORIGIN_TYPE_FIELD_NAME, REST_ORIGIN_FIELD_VALUE) - .with(ORIGIN_ADDRESS_FIELD_NAME, NetworkAddress.format(socketAddress)); + .with(ORIGIN_ADDRESS_FIELD_NAME, NetworkAddress.format(socketAddress)); } // fall through to local_node default return this; @@ -1248,12 +1423,12 @@ LogEntryBuilder withRestOrTransportOrigin(TransportRequest transportRequest, Thr final InetSocketAddress restAddress = RemoteHostHeader.restRemoteAddress(threadContext); if (restAddress != null) { logEntry.with(ORIGIN_TYPE_FIELD_NAME, REST_ORIGIN_FIELD_VALUE) - .with(ORIGIN_ADDRESS_FIELD_NAME, NetworkAddress.format(restAddress)); + .with(ORIGIN_ADDRESS_FIELD_NAME, NetworkAddress.format(restAddress)); } else { final TransportAddress address = transportRequest.remoteAddress(); if (address != null) { logEntry.with(ORIGIN_TYPE_FIELD_NAME, TRANSPORT_ORIGIN_FIELD_VALUE) - .with(ORIGIN_ADDRESS_FIELD_NAME, NetworkAddress.format(address.address())); + .with(ORIGIN_ADDRESS_FIELD_NAME, NetworkAddress.format(address.address())); } } // fall through to local_node default @@ -1310,16 +1485,18 @@ LogEntryBuilder withAuthentication(Authentication authentication) { } else { if (authentication.getUser().isRunAs()) { logEntry.with(PRINCIPAL_REALM_FIELD_NAME, authentication.getLookedUpBy().getName()) - .with(PRINCIPAL_RUN_BY_FIELD_NAME, authentication.getUser().authenticatedUser().principal()) - .with(PRINCIPAL_RUN_BY_REALM_FIELD_NAME, authentication.getAuthenticatedBy().getName()); + .with(PRINCIPAL_RUN_BY_FIELD_NAME, authentication.getUser().authenticatedUser().principal()) + .with(PRINCIPAL_RUN_BY_REALM_FIELD_NAME, authentication.getAuthenticatedBy().getName()); } else { logEntry.with(PRINCIPAL_REALM_FIELD_NAME, authentication.getAuthenticatedBy().getName()); } } if (authentication.isServiceAccount()) { logEntry.with(SERVICE_TOKEN_NAME_FIELD_NAME, (String) authentication.getMetadata().get(TOKEN_NAME_FIELD)) - .with(SERVICE_TOKEN_TYPE_FIELD_NAME, - ServiceAccountSettings.REALM_TYPE + "_" + authentication.getMetadata().get(TOKEN_SOURCE_FIELD)); + .with( + SERVICE_TOKEN_TYPE_FIELD_NAME, + ServiceAccountSettings.REALM_TYPE + "_" + authentication.getMetadata().get(TOKEN_SOURCE_FIELD) + ); } return this; } @@ -1374,7 +1551,6 @@ String toQuotedJsonArray(Object[] values) { } } - private static Optional indices(TransportRequest transportRequest) { if (transportRequest instanceof IndicesRequest) { final String[] indices = ((IndicesRequest) transportRequest).indices(); @@ -1416,11 +1592,14 @@ private static final class EventFilterPolicy { private final Predicate ignoreActionsPredicate; EventFilterPolicy(String name, Settings settings) { - this(name, parsePredicate(FILTER_POLICY_IGNORE_PRINCIPALS.getConcreteSettingForNamespace(name).get(settings)), - parsePredicate(FILTER_POLICY_IGNORE_REALMS.getConcreteSettingForNamespace(name).get(settings)), - parsePredicate(FILTER_POLICY_IGNORE_ROLES.getConcreteSettingForNamespace(name).get(settings)), - parsePredicate(FILTER_POLICY_IGNORE_INDICES.getConcreteSettingForNamespace(name).get(settings)), - parsePredicate(FILTER_POLICY_IGNORE_ACTIONS.getConcreteSettingForNamespace(name).get(settings))); + this( + name, + parsePredicate(FILTER_POLICY_IGNORE_PRINCIPALS.getConcreteSettingForNamespace(name).get(settings)), + parsePredicate(FILTER_POLICY_IGNORE_REALMS.getConcreteSettingForNamespace(name).get(settings)), + parsePredicate(FILTER_POLICY_IGNORE_ROLES.getConcreteSettingForNamespace(name).get(settings)), + parsePredicate(FILTER_POLICY_IGNORE_INDICES.getConcreteSettingForNamespace(name).get(settings)), + parsePredicate(FILTER_POLICY_IGNORE_ACTIONS.getConcreteSettingForNamespace(name).get(settings)) + ); } /** @@ -1428,9 +1607,14 @@ private static final class EventFilterPolicy { * An event with an undefined field has the field value the empty string ("") or * a singleton list of the empty string ([""]). */ - EventFilterPolicy(String name, Predicate ignorePrincipalsPredicate, Predicate ignoreRealmsPredicate, - Predicate ignoreRolesPredicate, Predicate ignoreIndicesPredicate, - Predicate ignoreActionsPredicate) { + EventFilterPolicy( + String name, + Predicate ignorePrincipalsPredicate, + Predicate ignoreRealmsPredicate, + Predicate ignoreRolesPredicate, + Predicate ignoreIndicesPredicate, + Predicate ignoreActionsPredicate + ) { this.name = name; // "null" values are "unexpected" and should not match any ignore policy this.ignorePrincipalsPredicate = ignorePrincipalsPredicate; @@ -1441,28 +1625,58 @@ private static final class EventFilterPolicy { } private EventFilterPolicy changePrincipalsFilter(List filtersList) { - return new EventFilterPolicy(name, parsePredicate(filtersList), ignoreRealmsPredicate, ignoreRolesPredicate, - ignoreIndicesPredicate, ignoreActionsPredicate); + return new EventFilterPolicy( + name, + parsePredicate(filtersList), + ignoreRealmsPredicate, + ignoreRolesPredicate, + ignoreIndicesPredicate, + ignoreActionsPredicate + ); } private EventFilterPolicy changeRealmsFilter(List filtersList) { - return new EventFilterPolicy(name, ignorePrincipalsPredicate, parsePredicate(filtersList), ignoreRolesPredicate, - ignoreIndicesPredicate, ignoreActionsPredicate); + return new EventFilterPolicy( + name, + ignorePrincipalsPredicate, + parsePredicate(filtersList), + ignoreRolesPredicate, + ignoreIndicesPredicate, + ignoreActionsPredicate + ); } private EventFilterPolicy changeRolesFilter(List filtersList) { - return new EventFilterPolicy(name, ignorePrincipalsPredicate, ignoreRealmsPredicate, parsePredicate(filtersList), - ignoreIndicesPredicate, ignoreActionsPredicate); + return new EventFilterPolicy( + name, + ignorePrincipalsPredicate, + ignoreRealmsPredicate, + parsePredicate(filtersList), + ignoreIndicesPredicate, + ignoreActionsPredicate + ); } private EventFilterPolicy changeIndicesFilter(List filtersList) { - return new EventFilterPolicy(name, ignorePrincipalsPredicate, ignoreRealmsPredicate, ignoreRolesPredicate, - parsePredicate(filtersList), ignoreActionsPredicate); + return new EventFilterPolicy( + name, + ignorePrincipalsPredicate, + ignoreRealmsPredicate, + ignoreRolesPredicate, + parsePredicate(filtersList), + ignoreActionsPredicate + ); } private EventFilterPolicy changeActionsFilter(List filtersList) { - return new EventFilterPolicy(name, ignorePrincipalsPredicate, ignoreRealmsPredicate, ignoreRolesPredicate, - ignoreIndicesPredicate, parsePredicate(filtersList)); + return new EventFilterPolicy( + name, + ignorePrincipalsPredicate, + ignoreRealmsPredicate, + ignoreRolesPredicate, + ignoreIndicesPredicate, + parsePredicate(filtersList) + ); } static Predicate parsePredicate(List l) { @@ -1489,9 +1703,12 @@ private static List emptyStringBuildsEmptyAutomaton(List l) { */ Predicate ignorePredicate() { return eventInfo -> { - return eventInfo.principal != null && ignorePrincipalsPredicate.test(eventInfo.principal) - && eventInfo.realm != null && ignoreRealmsPredicate.test(eventInfo.realm) - && eventInfo.action != null && ignoreActionsPredicate.test(eventInfo.action) + return eventInfo.principal != null + && ignorePrincipalsPredicate.test(eventInfo.principal) + && eventInfo.realm != null + && ignoreRealmsPredicate.test(eventInfo.realm) + && eventInfo.action != null + && ignoreActionsPredicate.test(eventInfo.action) && eventInfo.roles.get().allMatch(role -> role != null && ignoreRolesPredicate.test(role)) && eventInfo.indices.get().allMatch(index -> index != null && ignoreIndicesPredicate.test(index)); }; @@ -1499,9 +1716,16 @@ Predicate ignorePredicate() { @Override public String toString() { - return "[users]:" + ignorePrincipalsPredicate.toString() + "&[realms]:" + ignoreRealmsPredicate.toString() + "&[roles]:" - + ignoreRolesPredicate.toString() + "&[indices]:" + ignoreIndicesPredicate.toString() + "&[actions]:" - + ignoreActionsPredicate.toString(); + return "[users]:" + + ignorePrincipalsPredicate.toString() + + "&[realms]:" + + ignoreRealmsPredicate.toString() + + "&[roles]:" + + ignoreRolesPredicate.toString() + + "&[indices]:" + + ignoreIndicesPredicate.toString() + + "&[actions]:" + + ignoreActionsPredicate.toString(); } } @@ -1566,8 +1790,12 @@ static final class AuditEventMetaInfo { final Supplier> indices; // empty is used for events can be filtered out only by the lack of a field - static final AuditEventMetaInfo EMPTY = new AuditEventMetaInfo(Optional.empty(), Optional.empty(), Optional.empty(), - Optional.empty()); + static final AuditEventMetaInfo EMPTY = new AuditEventMetaInfo( + Optional.empty(), + Optional.empty(), + Optional.empty(), + Optional.empty() + ); /** * If a field is missing for an event, its value for filtering purposes is the @@ -1577,8 +1805,13 @@ static final class AuditEventMetaInfo { * user field (such as `anonymous_access_denied`) as well as events from the * "elastic" username. */ - AuditEventMetaInfo(Optional user, Optional realm, Optional authorizationInfo, - Optional indices, Optional action) { + AuditEventMetaInfo( + Optional user, + Optional realm, + Optional authorizationInfo, + Optional indices, + Optional action + ) { this.principal = user.map(u -> u.principal()).orElse(""); this.realm = realm.orElse(""); this.action = action.orElse(""); @@ -1589,16 +1822,22 @@ static final class AuditEventMetaInfo { // to be regenerated as they cannot be operated upon twice this.roles = () -> authorizationInfo.filter(info -> { final Object value = info.asMap().get("user.roles"); - return value instanceof String[] && - ((String[]) value).length != 0 && - Arrays.stream((String[]) value).anyMatch(Objects::nonNull); + return value instanceof String[] + && ((String[]) value).length != 0 + && Arrays.stream((String[]) value).anyMatch(Objects::nonNull); }).map(info -> Arrays.stream((String[]) info.asMap().get("user.roles"))).orElse(Stream.of("")); - this.indices = () -> indices.filter(i -> i.length > 0).filter(a -> Arrays.stream(a).anyMatch(Objects::nonNull)) - .map(Arrays::stream).orElse(Stream.of("")); - } - - AuditEventMetaInfo(Optional authenticationToken, Optional realm, Optional indices, - Optional action) { + this.indices = () -> indices.filter(i -> i.length > 0) + .filter(a -> Arrays.stream(a).anyMatch(Objects::nonNull)) + .map(Arrays::stream) + .orElse(Stream.of("")); + } + + AuditEventMetaInfo( + Optional authenticationToken, + Optional realm, + Optional indices, + Optional action + ) { this.principal = authenticationToken.map(u -> u.principal()).orElse(""); this.realm = realm.orElse(""); this.action = action.orElse(""); diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ApiKeyAuthenticator.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ApiKeyAuthenticator.java index 44a95aa3e7d53..506bde7c0d59a 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ApiKeyAuthenticator.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ApiKeyAuthenticator.java @@ -52,24 +52,26 @@ public void authenticate(Context context, ActionListener listener.onFailure(context.getRequest().exceptionProcessingRequest(e, null)))); } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ApiKeyService.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ApiKeyService.java index 99ef1dc258649..2018b667fbd8f 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ApiKeyService.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ApiKeyService.java @@ -54,18 +54,9 @@ import org.elasticsearch.common.util.concurrent.FutureUtils; import org.elasticsearch.common.util.concurrent.ListenableFuture; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.DeprecationHandler; -import org.elasticsearch.xcontent.InstantiatingObjectParser; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.common.xcontent.ObjectParserHelper; -import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentLocation; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.core.CharArrays; import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; @@ -74,6 +65,15 @@ import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.DeprecationHandler; +import org.elasticsearch.xcontent.InstantiatingObjectParser; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentLocation; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.xpack.core.security.ScrollHelper; import org.elasticsearch.xpack.core.security.action.ApiKey; @@ -126,12 +126,13 @@ import java.util.function.Function; import java.util.function.Supplier; import java.util.stream.Collectors; + import javax.crypto.SecretKeyFactory; import static org.elasticsearch.action.bulk.TransportSingleItemBulkWriteAction.toSingleItemBulkRequest; +import static org.elasticsearch.search.SearchService.DEFAULT_KEEPALIVE_SETTING; import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg; import static org.elasticsearch.xcontent.ConstructingObjectParser.optionalConstructorArg; -import static org.elasticsearch.search.SearchService.DEFAULT_KEEPALIVE_SETTING; import static org.elasticsearch.xpack.core.ClientHelper.SECURITY_ORIGIN; import static org.elasticsearch.xpack.core.ClientHelper.executeAsyncWithOrigin; import static org.elasticsearch.xpack.core.security.authc.Authentication.AuthenticationType; @@ -154,43 +155,71 @@ public class ApiKeyService { public static final String API_KEY_CREATOR_REALM_TYPE = "_security_api_key_creator_realm_type"; public static final Setting PASSWORD_HASHING_ALGORITHM = new Setting<>( - "xpack.security.authc.api_key.hashing.algorithm", "pbkdf2", Function.identity(), v -> { - if (Hasher.getAvailableAlgoStoredHash().contains(v.toLowerCase(Locale.ROOT)) == false) { - throw new IllegalArgumentException("Invalid algorithm: " + v + ". Valid values for password hashing are " + - Hasher.getAvailableAlgoStoredHash().toString()); - } else if (v.regionMatches(true, 0, "pbkdf2", 0, "pbkdf2".length())) { - try { - SecretKeyFactory.getInstance("PBKDF2withHMACSHA512"); - } catch (NoSuchAlgorithmException e) { + "xpack.security.authc.api_key.hashing.algorithm", + "pbkdf2", + Function.identity(), + v -> { + if (Hasher.getAvailableAlgoStoredHash().contains(v.toLowerCase(Locale.ROOT)) == false) { throw new IllegalArgumentException( - "Support for PBKDF2WithHMACSHA512 must be available in order to use any of the " + - "PBKDF2 algorithms for the [xpack.security.authc.api_key.hashing.algorithm] setting.", e); + "Invalid algorithm: " + v + ". Valid values for password hashing are " + Hasher.getAvailableAlgoStoredHash().toString() + ); + } else if (v.regionMatches(true, 0, "pbkdf2", 0, "pbkdf2".length())) { + try { + SecretKeyFactory.getInstance("PBKDF2withHMACSHA512"); + } catch (NoSuchAlgorithmException e) { + throw new IllegalArgumentException( + "Support for PBKDF2WithHMACSHA512 must be available in order to use any of the " + + "PBKDF2 algorithms for the [xpack.security.authc.api_key.hashing.algorithm] setting.", + e + ); + } } - } - }, Setting.Property.NodeScope); - public static final Setting DELETE_TIMEOUT = Setting.timeSetting("xpack.security.authc.api_key.delete.timeout", - TimeValue.MINUS_ONE, Property.NodeScope); - public static final Setting DELETE_INTERVAL = Setting.timeSetting("xpack.security.authc.api_key.delete.interval", - TimeValue.timeValueHours(24L), Property.NodeScope); - public static final Setting CACHE_HASH_ALGO_SETTING = Setting.simpleString("xpack.security.authc.api_key.cache.hash_algo", - "ssha256", Setting.Property.NodeScope); - public static final Setting CACHE_TTL_SETTING = Setting.timeSetting("xpack.security.authc.api_key.cache.ttl", - TimeValue.timeValueHours(24L), Property.NodeScope); - public static final Setting CACHE_MAX_KEYS_SETTING = Setting.intSetting("xpack.security.authc.api_key.cache.max_keys", - 25000, Property.NodeScope); - public static final Setting DOC_CACHE_TTL_SETTING = Setting.timeSetting("xpack.security.authc.api_key.doc_cache.ttl", - TimeValue.timeValueMinutes(5), TimeValue.timeValueMinutes(0), TimeValue.timeValueMinutes(15), Property.NodeScope); + }, + Setting.Property.NodeScope + ); + public static final Setting DELETE_TIMEOUT = Setting.timeSetting( + "xpack.security.authc.api_key.delete.timeout", + TimeValue.MINUS_ONE, + Property.NodeScope + ); + public static final Setting DELETE_INTERVAL = Setting.timeSetting( + "xpack.security.authc.api_key.delete.interval", + TimeValue.timeValueHours(24L), + Property.NodeScope + ); + public static final Setting CACHE_HASH_ALGO_SETTING = Setting.simpleString( + "xpack.security.authc.api_key.cache.hash_algo", + "ssha256", + Setting.Property.NodeScope + ); + public static final Setting CACHE_TTL_SETTING = Setting.timeSetting( + "xpack.security.authc.api_key.cache.ttl", + TimeValue.timeValueHours(24L), + Property.NodeScope + ); + public static final Setting CACHE_MAX_KEYS_SETTING = Setting.intSetting( + "xpack.security.authc.api_key.cache.max_keys", + 25000, + Property.NodeScope + ); + public static final Setting DOC_CACHE_TTL_SETTING = Setting.timeSetting( + "xpack.security.authc.api_key.doc_cache.ttl", + TimeValue.timeValueMinutes(5), + TimeValue.timeValueMinutes(0), + TimeValue.timeValueMinutes(15), + Property.NodeScope + ); // This following fixed role descriptor is for fleet-server BWC on and before 7.14. // It is fixed and must NOT be updated when the fleet-server service account updates. private static final BytesArray FLEET_SERVER_ROLE_DESCRIPTOR_BYTES_V_7_14 = new BytesArray( - "{\"elastic/fleet-server\":{\"cluster\":[\"monitor\",\"manage_own_api_key\"]," + - "\"indices\":[{\"names\":[\"logs-*\",\"metrics-*\",\"traces-*\",\"synthetics-*\"," + - "\".logs-endpoint.diagnostic.collection-*\"]," + - "\"privileges\":[\"write\",\"create_index\",\"auto_configure\"],\"allow_restricted_indices\":false}," + - "{\"names\":[\".fleet-*\"],\"privileges\":[\"read\",\"write\",\"monitor\",\"create_index\",\"auto_configure\"]," + - "\"allow_restricted_indices\":false}],\"applications\":[],\"run_as\":[],\"metadata\":{}," + - "\"transient_metadata\":{\"enabled\":true}}}" + "{\"elastic/fleet-server\":{\"cluster\":[\"monitor\",\"manage_own_api_key\"]," + + "\"indices\":[{\"names\":[\"logs-*\",\"metrics-*\",\"traces-*\",\"synthetics-*\"," + + "\".logs-endpoint.diagnostic.collection-*\"]," + + "\"privileges\":[\"write\",\"create_index\",\"auto_configure\"],\"allow_restricted_indices\":false}," + + "{\"names\":[\".fleet-*\"],\"privileges\":[\"read\",\"write\",\"monitor\",\"create_index\",\"auto_configure\"]," + + "\"allow_restricted_indices\":false}],\"applications\":[],\"run_as\":[],\"metadata\":{}," + + "\"transient_metadata\":{\"enabled\":true}}}" ); private final Clock clock; @@ -215,8 +244,15 @@ public class ApiKeyService { private final AtomicLong lastEvictionCheckedAt = new AtomicLong(0); private final LongAdder evictionCounter = new LongAdder(); - public ApiKeyService(Settings settings, Clock clock, Client client, SecurityIndexManager securityIndex, - ClusterService clusterService, CacheInvalidatorRegistry cacheInvalidatorRegistry, ThreadPool threadPool) { + public ApiKeyService( + Settings settings, + Clock clock, + Client client, + SecurityIndexManager securityIndex, + ClusterService clusterService, + CacheInvalidatorRegistry cacheInvalidatorRegistry, + ThreadPool threadPool + ) { this.clock = clock; this.client = client; this.securityIndex = securityIndex; @@ -268,8 +304,12 @@ public void invalidateAll() { * @param userRoles the user's actual roles that we always enforce * @param listener the listener that will be used to notify of completion */ - public void createApiKey(Authentication authentication, CreateApiKeyRequest request, Set userRoles, - ActionListener listener) { + public void createApiKey( + Authentication authentication, + CreateApiKeyRequest request, + Set userRoles, + ActionListener listener + ) { ensureEnabled(); if (authentication == null) { listener.onFailure(new IllegalArgumentException("authentication must be provided")); @@ -278,39 +318,55 @@ public void createApiKey(Authentication authentication, CreateApiKeyRequest requ } } - private void createApiKeyAndIndexIt(Authentication authentication, CreateApiKeyRequest request, Set roleDescriptorSet, - ActionListener listener) { + private void createApiKeyAndIndexIt( + Authentication authentication, + CreateApiKeyRequest request, + Set roleDescriptorSet, + ActionListener listener + ) { final Instant created = clock.instant(); final Instant expiration = getApiKeyExpiration(created, request); final SecureString apiKey = UUIDs.randomBase64UUIDSecureString(); final Version version = clusterService.state().nodes().getMinNodeVersion(); - computeHashForApiKey(apiKey, listener.delegateFailure((l, apiKeyHashChars) -> { - try (XContentBuilder builder = newDocument(apiKeyHashChars, request.getName(), authentication, - roleDescriptorSet, created, expiration, - request.getRoleDescriptors(), version, request.getMetadata())) { - - final IndexRequest indexRequest = - client.prepareIndex(SECURITY_MAIN_ALIAS) - .setSource(builder) - .setId(request.getId()) - .setRefreshPolicy(request.getRefreshPolicy()) - .request(); + try ( + XContentBuilder builder = newDocument( + apiKeyHashChars, + request.getName(), + authentication, + roleDescriptorSet, + created, + expiration, + request.getRoleDescriptors(), + version, + request.getMetadata() + ) + ) { + + final IndexRequest indexRequest = client.prepareIndex(SECURITY_MAIN_ALIAS) + .setSource(builder) + .setId(request.getId()) + .setRefreshPolicy(request.getRefreshPolicy()) + .request(); final BulkRequest bulkRequest = toSingleItemBulkRequest(indexRequest); - securityIndex.prepareIndexIfNeededThenExecute(listener::onFailure, () -> - executeAsyncWithOrigin(client, SECURITY_ORIGIN, BulkAction.INSTANCE, bulkRequest, - TransportSingleItemBulkWriteAction.wrapBulkResponse(ActionListener.wrap( - indexResponse -> { - assert request.getId().equals(indexResponse.getId()); - final ListenableFuture listenableFuture = new ListenableFuture<>(); - listenableFuture.onResponse(new CachedApiKeyHashResult(true, apiKey)); - apiKeyAuthCache.put(request.getId(), listenableFuture); - listener.onResponse( - new CreateApiKeyResponse(request.getName(), request.getId(), apiKey, expiration)); - }, - listener::onFailure)))); + securityIndex.prepareIndexIfNeededThenExecute( + listener::onFailure, + () -> executeAsyncWithOrigin( + client, + SECURITY_ORIGIN, + BulkAction.INSTANCE, + bulkRequest, + TransportSingleItemBulkWriteAction.wrapBulkResponse(ActionListener.wrap(indexResponse -> { + assert request.getId().equals(indexResponse.getId()); + final ListenableFuture listenableFuture = new ListenableFuture<>(); + listenableFuture.onResponse(new CachedApiKeyHashResult(true, apiKey)); + apiKeyAuthCache.put(request.getId(), listenableFuture); + listener.onResponse(new CreateApiKeyResponse(request.getName(), request.getId(), apiKey, expiration)); + }, listener::onFailure)) + ) + ); } catch (IOException e) { listener.onFailure(e); } finally { @@ -322,9 +378,17 @@ private void createApiKeyAndIndexIt(Authentication authentication, CreateApiKeyR /** * package-private for testing */ - XContentBuilder newDocument(char[] apiKeyHashChars, String name, Authentication authentication, Set userRoles, - Instant created, Instant expiration, List keyRoles, - Version version, @Nullable Map metadata) throws IOException { + XContentBuilder newDocument( + char[] apiKeyHashChars, + String name, + Authentication authentication, + Set userRoles, + Instant created, + Instant expiration, + List keyRoles, + Version version, + @Nullable Map metadata + ) throws IOException { XContentBuilder builder = XContentFactory.jsonBuilder(); builder.startObject() .field("doc_type", "api_key") @@ -332,7 +396,6 @@ XContentBuilder newDocument(char[] apiKeyHashChars, String name, Authentication .field("expiration_time", expiration == null ? null : expiration.toEpochMilli()) .field("api_key_invalidated", false); - byte[] utf8Bytes = null; try { utf8Bytes = CharArrays.toUtf8Bytes(apiKeyHashChars); @@ -347,8 +410,7 @@ XContentBuilder newDocument(char[] apiKeyHashChars, String name, Authentication builder.startObject("role_descriptors"); if (keyRoles != null && keyRoles.isEmpty() == false) { for (RoleDescriptor descriptor : keyRoles) { - builder.field(descriptor.getName(), - (contentBuilder, params) -> descriptor.toXContent(contentBuilder, params, true)); + builder.field(descriptor.getName(), (contentBuilder, params) -> descriptor.toXContent(contentBuilder, params, true)); } } builder.endObject(); @@ -356,8 +418,7 @@ XContentBuilder newDocument(char[] apiKeyHashChars, String name, Authentication // Save limited_by_role_descriptors builder.startObject("limited_by_role_descriptors"); for (RoleDescriptor descriptor : userRoles) { - builder.field(descriptor.getName(), - (contentBuilder, params) -> descriptor.toXContent(contentBuilder, params, true)); + builder.field(descriptor.getName(), (contentBuilder, params) -> descriptor.toXContent(contentBuilder, params, true)); } builder.endObject(); @@ -382,16 +443,13 @@ void tryAuthenticate(ThreadContext ctx, ApiKeyCredentials credentials, ActionLis listener.onResponse(AuthenticationResult.notHandled()); } assert credentials != null : "api key credentials must not be null"; - loadApiKeyAndValidateCredentials(ctx, credentials, ActionListener.wrap( - response -> { - credentials.close(); - listener.onResponse(response); - }, - e -> { - credentials.close(); - listener.onFailure(e); - } - )); + loadApiKeyAndValidateCredentials(ctx, credentials, ActionListener.wrap(response -> { + credentials.close(); + listener.onResponse(response); + }, e -> { + credentials.close(); + listener.onFailure(e); + })); } public Authentication createApiKeyAuthentication(AuthenticationResult authResult, String nodeName) { @@ -400,22 +458,36 @@ public Authentication createApiKeyAuthentication(AuthenticationResult auth } final User user = authResult.getValue(); final RealmRef authenticatedBy = new RealmRef(ApiKeyService.API_KEY_REALM_NAME, ApiKeyService.API_KEY_REALM_TYPE, nodeName); - return new Authentication(user, authenticatedBy, null, Version.CURRENT, Authentication.AuthenticationType.API_KEY, - authResult.getMetadata()); + return new Authentication( + user, + authenticatedBy, + null, + Version.CURRENT, + Authentication.AuthenticationType.API_KEY, + authResult.getMetadata() + ); } - void loadApiKeyAndValidateCredentials(ThreadContext ctx, ApiKeyCredentials credentials, - ActionListener> listener) { + void loadApiKeyAndValidateCredentials( + ThreadContext ctx, + ApiKeyCredentials credentials, + ActionListener> listener + ) { final String docId = credentials.getId(); - Consumer validator = apiKeyDoc -> - validateApiKeyCredentials(docId, apiKeyDoc, credentials, clock, listener.delegateResponse((l, e) -> { + Consumer validator = apiKeyDoc -> validateApiKeyCredentials( + docId, + apiKeyDoc, + credentials, + clock, + listener.delegateResponse((l, e) -> { if (ExceptionsHelper.unwrapCause(e) instanceof EsRejectedExecutionException) { l.onResponse(AuthenticationResult.terminate("server is too busy to respond", e)); } else { l.onFailure(e); } - })); + }) + ); final long invalidationCount; if (apiKeyDocCache != null) { @@ -430,39 +502,39 @@ void loadApiKeyAndValidateCredentials(ThreadContext ctx, ApiKeyCredentials crede invalidationCount = -1; } - final GetRequest getRequest = client - .prepareGet(SECURITY_MAIN_ALIAS, docId) - .setFetchSource(true) - .request(); + final GetRequest getRequest = client.prepareGet(SECURITY_MAIN_ALIAS, docId).setFetchSource(true).request(); executeAsyncWithOrigin(ctx, SECURITY_ORIGIN, getRequest, ActionListener.wrap(response -> { - if (response.isExists()) { - final ApiKeyDoc apiKeyDoc; - try (XContentParser parser = XContentHelper.createParser( - NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, - response.getSourceAsBytesRef(), XContentType.JSON)) { - apiKeyDoc = ApiKeyDoc.fromXContent(parser); - } - if (invalidationCount != -1) { - apiKeyDocCache.putIfNoInvalidationSince(docId, apiKeyDoc, invalidationCount); - } - validator.accept(apiKeyDoc); - } else { - if (apiKeyAuthCache != null) { - apiKeyAuthCache.invalidate(docId); - } - listener.onResponse( - AuthenticationResult.unsuccessful("unable to find apikey with id " + credentials.getId(), null)); + if (response.isExists()) { + final ApiKeyDoc apiKeyDoc; + try ( + XContentParser parser = XContentHelper.createParser( + NamedXContentRegistry.EMPTY, + LoggingDeprecationHandler.INSTANCE, + response.getSourceAsBytesRef(), + XContentType.JSON + ) + ) { + apiKeyDoc = ApiKeyDoc.fromXContent(parser); } - }, - e -> { - if (ExceptionsHelper.unwrapCause(e) instanceof EsRejectedExecutionException) { - listener.onResponse(AuthenticationResult.terminate("server is too busy to respond", e)); - } else { - listener.onResponse(AuthenticationResult.unsuccessful( - "apikey authentication for id " + credentials.getId() + " encountered a failure",e)); + if (invalidationCount != -1) { + apiKeyDocCache.putIfNoInvalidationSince(docId, apiKeyDoc, invalidationCount); + } + validator.accept(apiKeyDoc); + } else { + if (apiKeyAuthCache != null) { + apiKeyAuthCache.invalidate(docId); } - }), - client::get); + listener.onResponse(AuthenticationResult.unsuccessful("unable to find apikey with id " + credentials.getId(), null)); + } + }, e -> { + if (ExceptionsHelper.unwrapCause(e) instanceof EsRejectedExecutionException) { + listener.onResponse(AuthenticationResult.terminate("server is too busy to respond", e)); + } else { + listener.onResponse( + AuthenticationResult.unsuccessful("apikey authentication for id " + credentials.getId() + " encountered a failure", e) + ); + } + }), client::get); } /** @@ -476,8 +548,8 @@ public void getRoleForApiKey(Authentication authentication, ActionListener metadata = authentication.getMetadata(); final String apiKeyId = (String) metadata.get(API_KEY_ID_KEY); @@ -502,12 +574,13 @@ public Tuple getApiKeyIdAndRoleBytes(Authentication auth if (authentication.getAuthenticationType() != AuthenticationType.API_KEY) { throw new IllegalStateException("authentication type must be api key but is " + authentication.getAuthenticationType()); } - assert authentication.getVersion() - .onOrAfter(VERSION_API_KEY_ROLES_AS_BYTES) : "This method only applies to authentication objects created on or after v7.9.0"; + assert authentication.getVersion().onOrAfter(VERSION_API_KEY_ROLES_AS_BYTES) + : "This method only applies to authentication objects created on or after v7.9.0"; final Map metadata = authentication.getMetadata(); - final BytesReference bytesReference = - (BytesReference) metadata.get(limitedBy ? API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY : API_KEY_ROLE_DESCRIPTORS_KEY); + final BytesReference bytesReference = (BytesReference) metadata.get( + limitedBy ? API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY : API_KEY_ROLE_DESCRIPTORS_KEY + ); if (limitedBy && bytesReference.length() == 2 && "{}".equals(bytesReference.utf8ToString())) { if (ServiceAccountSettings.REALM_NAME.equals(metadata.get(API_KEY_CREATOR_REALM_NAME)) && "elastic/fleet-server".equals(authentication.getUser().principal())) { @@ -546,22 +619,26 @@ private List parseRoleDescriptors(final String apiKeyId, final M if (roleDescriptors == null) { return null; } - return roleDescriptors.entrySet().stream() - .map(entry -> { - final String name = entry.getKey(); - @SuppressWarnings("unchecked") - final Map rdMap = (Map) entry.getValue(); - try (XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent())) { - builder.map(rdMap); - try (XContentParser parser = XContentType.JSON.xContent().createParser(NamedXContentRegistry.EMPTY, - new ApiKeyLoggingDeprecationHandler(deprecationLogger, apiKeyId), - BytesReference.bytes(builder).streamInput())) { - return RoleDescriptor.parse(name, parser, false); - } - } catch (IOException e) { - throw new UncheckedIOException(e); + return roleDescriptors.entrySet().stream().map(entry -> { + final String name = entry.getKey(); + @SuppressWarnings("unchecked") + final Map rdMap = (Map) entry.getValue(); + try (XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent())) { + builder.map(rdMap); + try ( + XContentParser parser = XContentType.JSON.xContent() + .createParser( + NamedXContentRegistry.EMPTY, + new ApiKeyLoggingDeprecationHandler(deprecationLogger, apiKeyId), + BytesReference.bytes(builder).streamInput() + ) + ) { + return RoleDescriptor.parse(name, parser, false); } - }).collect(Collectors.toList()); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + }).collect(Collectors.toList()); } public List parseRoleDescriptors(final String apiKeyId, BytesReference bytesReference) { @@ -575,7 +652,9 @@ public List parseRoleDescriptors(final String apiKeyId, BytesRef NamedXContentRegistry.EMPTY, new ApiKeyLoggingDeprecationHandler(deprecationLogger, apiKeyId), bytesReference, - XContentType.JSON)) { + XContentType.JSON + ) + ) { parser.nextToken(); // skip outer start object while (parser.nextToken() != XContentParser.Token.END_OBJECT) { parser.nextToken(); // role name @@ -595,11 +674,17 @@ public List parseRoleDescriptors(final String apiKeyId, BytesRef * @param credentials the credentials provided by the user * @param listener the listener to notify after verification */ - void validateApiKeyCredentials(String docId, ApiKeyDoc apiKeyDoc, ApiKeyCredentials credentials, Clock clock, - ActionListener> listener) { + void validateApiKeyCredentials( + String docId, + ApiKeyDoc apiKeyDoc, + ApiKeyCredentials credentials, + Clock clock, + ActionListener> listener + ) { if ("api_key".equals(apiKeyDoc.docType) == false) { listener.onResponse( - AuthenticationResult.unsuccessful("document [" + docId + "] is [" + apiKeyDoc.docType + "] not an api key", null)); + AuthenticationResult.unsuccessful("document [" + docId + "] is [" + apiKeyDoc.docType + "] not an api key", null) + ); } else if (apiKeyDoc.invalidated == null) { listener.onResponse(AuthenticationResult.unsuccessful("api key document is missing invalidated field", null)); } else if (apiKeyDoc.invalidated) { @@ -616,11 +701,10 @@ void validateApiKeyCredentials(String docId, ApiKeyDoc apiKeyDoc, ApiKeyCredenti final AtomicBoolean valueAlreadyInCache = new AtomicBoolean(true); final ListenableFuture listenableCacheEntry; try { - listenableCacheEntry = apiKeyAuthCache.computeIfAbsent(credentials.getId(), - k -> { - valueAlreadyInCache.set(false); - return new ListenableFuture<>(); - }); + listenableCacheEntry = apiKeyAuthCache.computeIfAbsent(credentials.getId(), k -> { + valueAlreadyInCache.set(false); + return new ListenableFuture<>(); + }); } catch (ExecutionException e) { listener.onFailure(e); return; @@ -628,46 +712,40 @@ void validateApiKeyCredentials(String docId, ApiKeyDoc apiKeyDoc, ApiKeyCredenti if (valueAlreadyInCache.get()) { listenableCacheEntry.addListener(ActionListener.wrap(result -> { - if (result.success) { - if (result.verify(credentials.getKey())) { - // move on - validateApiKeyExpiration(apiKeyDoc, credentials, clock, listener); - } else { - listener.onResponse(AuthenticationResult.unsuccessful("invalid credentials", null)); - } - } else if (result.verify(credentials.getKey())) { // same key, pass the same result - listener.onResponse(AuthenticationResult.unsuccessful("invalid credentials", null)); - } else { - apiKeyAuthCache.invalidate(credentials.getId(), listenableCacheEntry); - validateApiKeyCredentials(docId, apiKeyDoc, credentials, clock, listener); - } - }, listener::onFailure), - threadPool.generic(), threadPool.getThreadContext()); - } else { - verifyKeyAgainstHash(apiKeyDoc.hash, credentials, ActionListener.wrap( - verified -> { - listenableCacheEntry.onResponse(new CachedApiKeyHashResult(verified, credentials.getKey())); - if (verified) { + if (result.success) { + if (result.verify(credentials.getKey())) { // move on validateApiKeyExpiration(apiKeyDoc, credentials, clock, listener); } else { listener.onResponse(AuthenticationResult.unsuccessful("invalid credentials", null)); } - }, listener::onFailure - )); - } - } else { - verifyKeyAgainstHash(apiKeyDoc.hash, credentials, ActionListener.wrap( - verified -> { + } else if (result.verify(credentials.getKey())) { // same key, pass the same result + listener.onResponse(AuthenticationResult.unsuccessful("invalid credentials", null)); + } else { + apiKeyAuthCache.invalidate(credentials.getId(), listenableCacheEntry); + validateApiKeyCredentials(docId, apiKeyDoc, credentials, clock, listener); + } + }, listener::onFailure), threadPool.generic(), threadPool.getThreadContext()); + } else { + verifyKeyAgainstHash(apiKeyDoc.hash, credentials, ActionListener.wrap(verified -> { + listenableCacheEntry.onResponse(new CachedApiKeyHashResult(verified, credentials.getKey())); if (verified) { // move on validateApiKeyExpiration(apiKeyDoc, credentials, clock, listener); } else { listener.onResponse(AuthenticationResult.unsuccessful("invalid credentials", null)); } - }, - listener::onFailure - )); + }, listener::onFailure)); + } + } else { + verifyKeyAgainstHash(apiKeyDoc.hash, credentials, ActionListener.wrap(verified -> { + if (verified) { + // move on + validateApiKeyExpiration(apiKeyDoc, credentials, clock, listener); + } else { + listener.onResponse(AuthenticationResult.unsuccessful("invalid credentials", null)); + } + }, listener::onFailure)); } } } @@ -693,8 +771,12 @@ Cache getRoleDescriptorsBytesCache() { } // package-private for testing - void validateApiKeyExpiration(ApiKeyDoc apiKeyDoc, ApiKeyCredentials credentials, Clock clock, - ActionListener> listener) { + void validateApiKeyExpiration( + ApiKeyDoc apiKeyDoc, + ApiKeyCredentials credentials, + Clock clock, + ActionListener> listener + ) { if (apiKeyDoc.expirationTime == -1 || Instant.ofEpochMilli(apiKeyDoc.expirationTime).isAfter(clock.instant())) { final String principal = Objects.requireNonNull((String) apiKeyDoc.creator.get("principal")); final String fullName = (String) apiKeyDoc.creator.get("full_name"); @@ -743,8 +825,10 @@ ApiKeyCredentials getCredentialsFromHeader(ThreadContext threadContext) { if (colonIndex < 1) { throw new IllegalArgumentException("invalid ApiKey value"); } - return new ApiKeyCredentials(new String(Arrays.copyOfRange(apiKeyCredChars, 0, colonIndex)), - new SecureString(Arrays.copyOfRange(apiKeyCredChars, colonIndex + 1, apiKeyCredChars.length))); + return new ApiKeyCredentials( + new String(Arrays.copyOfRange(apiKeyCredChars, 0, colonIndex)), + new SecureString(Arrays.copyOfRange(apiKeyCredChars, colonIndex + 1, apiKeyCredChars.length)) + ); } finally { if (apiKeyCredChars != null) { Arrays.fill(apiKeyCredChars, (char) 0); @@ -852,23 +936,42 @@ private ApiKeyLoggingDeprecationHandler(DeprecationLogger logger, String apiKeyI @Override public void logRenamedField(String parserName, Supplier location, String oldName, String currentName) { String prefix = parserName == null ? "" : "[" + parserName + "][" + location.get() + "] "; - deprecationLogger.critical(DeprecationCategory.API, "api_key_field", - "{}Deprecated field [{}] used in api key [{}], expected [{}] instead", prefix, oldName, apiKeyId, currentName); + deprecationLogger.critical( + DeprecationCategory.API, + "api_key_field", + "{}Deprecated field [{}] used in api key [{}], expected [{}] instead", + prefix, + oldName, + apiKeyId, + currentName + ); } @Override public void logReplacedField(String parserName, Supplier location, String oldName, String replacedName) { String prefix = parserName == null ? "" : "[" + parserName + "][" + location.get() + "] "; - deprecationLogger.critical(DeprecationCategory.API, "api_key_field", - "{}Deprecated field [{}] used in api key [{}], replaced by [{}]", prefix, oldName, apiKeyId, replacedName); + deprecationLogger.critical( + DeprecationCategory.API, + "api_key_field", + "{}Deprecated field [{}] used in api key [{}], replaced by [{}]", + prefix, + oldName, + apiKeyId, + replacedName + ); } @Override public void logRemovedField(String parserName, Supplier location, String removedName) { String prefix = parserName == null ? "" : "[" + parserName + "][" + location.get() + "] "; - deprecationLogger.critical(DeprecationCategory.API, "api_key_field", + deprecationLogger.critical( + DeprecationCategory.API, + "api_key_field", "{}Deprecated field [{}] used in api key [{}], which is unused and will be removed entirely", - prefix, removedName, apiKeyId); + prefix, + removedName, + apiKeyId + ); } } @@ -880,27 +983,48 @@ public void logRemovedField(String parserName, Supplier locati * @param apiKeyIds API key id * @param invalidateListener listener for {@link InvalidateApiKeyResponse} */ - public void invalidateApiKeys(String realmName, String username, String apiKeyName, String[] apiKeyIds, - ActionListener invalidateListener) { + public void invalidateApiKeys( + String realmName, + String username, + String apiKeyName, + String[] apiKeyIds, + ActionListener invalidateListener + ) { ensureEnabled(); - if (Strings.hasText(realmName) == false && Strings.hasText(username) == false && Strings.hasText(apiKeyName) == false + if (Strings.hasText(realmName) == false + && Strings.hasText(username) == false + && Strings.hasText(apiKeyName) == false && (apiKeyIds == null || apiKeyIds.length == 0)) { logger.trace("none of the parameters [api key id, api key name, username, realm name] were specified for invalidation"); - invalidateListener - .onFailure(new IllegalArgumentException("One of [api key id, api key name, username, realm name] must be specified")); + invalidateListener.onFailure( + new IllegalArgumentException("One of [api key id, api key name, username, realm name] must be specified") + ); } else { - findApiKeysForUserRealmApiKeyIdAndNameCombination(realmName, username, apiKeyName, apiKeyIds, true, false, + findApiKeysForUserRealmApiKeyIdAndNameCombination( + realmName, + username, + apiKeyName, + apiKeyIds, + true, + false, ActionListener.wrap(apiKeys -> { if (apiKeys.isEmpty()) { logger.debug( "No active api keys to invalidate for realm [{}], username [{}], api key name [{}] and api key id [{}]", - realmName, username, apiKeyName, Arrays.toString(apiKeyIds)); + realmName, + username, + apiKeyName, + Arrays.toString(apiKeyIds) + ); invalidateListener.onResponse(InvalidateApiKeyResponse.emptyResponse()); } else { - invalidateAllApiKeys(apiKeys.stream().map(apiKey -> apiKey.getId()).collect(Collectors.toSet()), - invalidateListener); + invalidateAllApiKeys( + apiKeys.stream().map(apiKey -> apiKey.getId()).collect(Collectors.toSet()), + invalidateListener + ); } - }, invalidateListener::onFailure)); + }, invalidateListener::onFailure) + ); } } @@ -908,8 +1032,12 @@ private void invalidateAllApiKeys(Collection apiKeyIds, ActionListener> listener) { + private void findApiKeys( + final BoolQueryBuilder boolQuery, + boolean filterOutInvalidatedKeys, + boolean filterOutExpiredKeys, + ActionListener> listener + ) { if (filterOutInvalidatedKeys) { boolQuery.filter(QueryBuilders.termQuery("api_key_invalidated", false)); } @@ -922,21 +1050,33 @@ private void findApiKeys(final BoolQueryBuilder boolQuery, boolean filterOutInva final Supplier supplier = client.threadPool().getThreadContext().newRestorableContext(false); try (ThreadContext.StoredContext ignore = client.threadPool().getThreadContext().stashWithOrigin(SECURITY_ORIGIN)) { final SearchRequest request = client.prepareSearch(SECURITY_MAIN_ALIAS) - .setScroll(DEFAULT_KEEPALIVE_SETTING.get(settings)) - .setQuery(boolQuery) - .setVersion(false) - .setSize(1000) - .setFetchSource(true) - .request(); - securityIndex.checkIndexVersionThenExecute(listener::onFailure, - () -> ScrollHelper.fetchAllByEntity(client, request, new ContextPreservingActionListener<>(supplier, listener), - ApiKeyService::convertSearchHitToApiKeyInfo)); + .setScroll(DEFAULT_KEEPALIVE_SETTING.get(settings)) + .setQuery(boolQuery) + .setVersion(false) + .setSize(1000) + .setFetchSource(true) + .request(); + securityIndex.checkIndexVersionThenExecute( + listener::onFailure, + () -> ScrollHelper.fetchAllByEntity( + client, + request, + new ContextPreservingActionListener<>(supplier, listener), + ApiKeyService::convertSearchHitToApiKeyInfo + ) + ); } } - private void findApiKeysForUserRealmApiKeyIdAndNameCombination(String realmName, String userName, String apiKeyName, String[] apiKeyIds, - boolean filterOutInvalidatedKeys, boolean filterOutExpiredKeys, - ActionListener> listener) { + private void findApiKeysForUserRealmApiKeyIdAndNameCombination( + String realmName, + String userName, + String apiKeyName, + String[] apiKeyIds, + boolean filterOutInvalidatedKeys, + boolean filterOutExpiredKeys, + ActionListener> listener + ) { final SecurityIndexManager frozenSecurityIndex = securityIndex.freeze(); if (frozenSecurityIndex.indexExists() == false) { listener.onResponse(Collections.emptyList()); @@ -973,23 +1113,29 @@ private void findApiKeysForUserRealmApiKeyIdAndNameCombination(String realmName, * @param previousResult if this not the initial attempt for invalidation, it contains the result of invalidating * api keys up to the point of the retry. This result is added to the result of the current attempt */ - private void indexInvalidation(Collection apiKeyIds, ActionListener listener, - @Nullable InvalidateApiKeyResponse previousResult) { + private void indexInvalidation( + Collection apiKeyIds, + ActionListener listener, + @Nullable InvalidateApiKeyResponse previousResult + ) { maybeStartApiKeyRemover(); if (apiKeyIds.isEmpty()) { listener.onFailure(new ElasticsearchSecurityException("No api key ids provided for invalidation")); } else { BulkRequestBuilder bulkRequestBuilder = client.prepareBulk(); for (String apiKeyId : apiKeyIds) { - UpdateRequest request = client - .prepareUpdate(SECURITY_MAIN_ALIAS, apiKeyId) + UpdateRequest request = client.prepareUpdate(SECURITY_MAIN_ALIAS, apiKeyId) .setDoc(Collections.singletonMap("api_key_invalidated", true)) .request(); bulkRequestBuilder.add(request); } bulkRequestBuilder.setRefreshPolicy(RefreshPolicy.WAIT_UNTIL); - securityIndex.prepareIndexIfNeededThenExecute(ex -> listener.onFailure(traceLog("prepare security index", ex)), - () -> executeAsyncWithOrigin(client.threadPool().getThreadContext(), SECURITY_ORIGIN, bulkRequestBuilder.request(), + securityIndex.prepareIndexIfNeededThenExecute( + ex -> listener.onFailure(traceLog("prepare security index", ex)), + () -> executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + SECURITY_ORIGIN, + bulkRequestBuilder.request(), ActionListener.wrap(bulkResponse -> { ArrayList failedRequestResponses = new ArrayList<>(); ArrayList previouslyInvalidated = new ArrayList<>(); @@ -1015,34 +1161,38 @@ private void indexInvalidation(Collection apiKeyIds, ActionListener { Throwable cause = ExceptionsHelper.unwrapCause(e); traceLog("invalidate api keys", cause); listener.onFailure(e); - }), client::bulk)); + }), + client::bulk + ) + ); } } private void clearCache(InvalidateApiKeyResponse result, ActionListener listener) { - final ClearSecurityCacheRequest clearApiKeyCacheRequest = - new ClearSecurityCacheRequest().cacheName("api_key").keys(result.getInvalidatedApiKeys().toArray(String[]::new)); - executeAsyncWithOrigin(client, SECURITY_ORIGIN, ClearSecurityCacheAction.INSTANCE, clearApiKeyCacheRequest, - new ActionListener<>() { - @Override - public void onResponse(ClearSecurityCacheResponse nodes) { - listener.onResponse(result); - } + final ClearSecurityCacheRequest clearApiKeyCacheRequest = new ClearSecurityCacheRequest().cacheName("api_key") + .keys(result.getInvalidatedApiKeys().toArray(String[]::new)); + executeAsyncWithOrigin(client, SECURITY_ORIGIN, ClearSecurityCacheAction.INSTANCE, clearApiKeyCacheRequest, new ActionListener<>() { + @Override + public void onResponse(ClearSecurityCacheResponse nodes) { + listener.onResponse(result); + } - @Override - public void onFailure(Exception e) { - logger.error("unable to clear API key cache", e); - listener.onFailure(new ElasticsearchException( - "clearing the API key cache failed; please clear the caches manually", e)); - } - }); + @Override + public void onFailure(Exception e) { + logger.error("unable to clear API key cache", e); + listener.onFailure(new ElasticsearchException("clearing the API key cache failed; please clear the caches manually", e)); + } + }); } /** @@ -1054,11 +1204,9 @@ private E traceLog(String action, String identifier, E exc final ElasticsearchException esEx = (ElasticsearchException) exception; final Object detail = esEx.getHeader("error_description"); if (detail != null) { - logger.trace(() -> new ParameterizedMessage("Failure in [{}] for id [{}] - [{}]", action, identifier, detail), - esEx); + logger.trace(() -> new ParameterizedMessage("Failure in [{}] for id [{}] - [{}]", action, identifier, detail), esEx); } else { - logger.trace(() -> new ParameterizedMessage("Failure in [{}] for id [{}]", action, identifier), - esEx); + logger.trace(() -> new ParameterizedMessage("Failure in [{}] for id [{}]", action, identifier), esEx); } } else { logger.trace(() -> new ParameterizedMessage("Failure in [{}] for id [{}]", action, identifier), exception); @@ -1114,20 +1262,37 @@ private void maybeStartApiKeyRemover() { * @param apiKeyId API key id * @param listener listener for {@link GetApiKeyResponse} */ - public void getApiKeys(String realmName, String username, String apiKeyName, String apiKeyId, - ActionListener listener) { + public void getApiKeys( + String realmName, + String username, + String apiKeyName, + String apiKeyId, + ActionListener listener + ) { ensureEnabled(); final String[] apiKeyIds = Strings.hasText(apiKeyId) == false ? null : new String[] { apiKeyId }; - findApiKeysForUserRealmApiKeyIdAndNameCombination(realmName, username, apiKeyName, apiKeyIds, false, false, + findApiKeysForUserRealmApiKeyIdAndNameCombination( + realmName, + username, + apiKeyName, + apiKeyIds, + false, + false, ActionListener.wrap(apiKeyInfos -> { if (apiKeyInfos.isEmpty()) { - logger.debug("No active api keys found for realm [{}], user [{}], api key name [{}] and api key id [{}]", - realmName, username, apiKeyName, apiKeyId); + logger.debug( + "No active api keys found for realm [{}], user [{}], api key name [{}] and api key id [{}]", + realmName, + username, + apiKeyName, + apiKeyId + ); listener.onResponse(GetApiKeyResponse.emptyResponse()); } else { listener.onResponse(new GetApiKeyResponse(apiKeyInfos)); } - }, listener::onFailure)); + }, listener::onFailure) + ); } public void queryApiKeys(SearchRequest searchRequest, ActionListener listener) { @@ -1140,8 +1305,10 @@ public void queryApiKeys(SearchRequest searchRequest, ActionListener executeAsyncWithOrigin(client, + securityIndex.checkIndexVersionThenExecute( + listener::onFailure, + () -> executeAsyncWithOrigin( + client, SECURITY_ORIGIN, SearchAction.INSTANCE, searchRequest, @@ -1156,7 +1323,9 @@ public void queryApiKeys(SearchRequest searchRequest, ActionListener> getAut return notification -> { if (RemovalReason.EVICTED == notification.getRemovalReason() && getApiKeyAuthCache().count() >= maximumWeight) { evictionCounter.increment(); - logger.trace("API key with ID [{}] was evicted from the authentication cache, " - + "possibly due to cache size limit", notification.getKey()); + logger.trace( + "API key with ID [{}] was evicted from the authentication cache, " + "possibly due to cache size limit", + notification.getKey() + ); final long last = lastEvictionCheckedAt.get(); final long now = System.nanoTime(); if (now - last >= EVICTION_MONITOR_INTERVAL_NANOS && lastEvictionCheckedAt.compareAndSet(last, now)) { final long sum = evictionCounter.sum(); evictionCounter.add(-sum); // reset by decrease if (sum >= EVICTION_WARNING_THRESHOLD) { - logger.warn("Possible thrashing for API key authentication cache, " + logger.warn( + "Possible thrashing for API key authentication cache, " + "[{}] eviction due to cache size within last [{}] seconds", - sum, EVICTION_MONITOR_INTERVAL_SECONDS); + sum, + EVICTION_MONITOR_INTERVAL_SECONDS + ); } } } @@ -1259,13 +1433,19 @@ public static String getCreatorRealmType(final Authentication authentication) { */ public static Map getApiKeyMetadata(Authentication authentication) { if (AuthenticationType.API_KEY != authentication.getAuthenticationType()) { - throw new IllegalArgumentException("authentication type must be [api_key], got [" - + authentication.getAuthenticationType().name().toLowerCase(Locale.ROOT) + "]"); + throw new IllegalArgumentException( + "authentication type must be [api_key], got [" + + authentication.getAuthenticationType().name().toLowerCase(Locale.ROOT) + + "]" + ); } final Object apiKeyMetadata = authentication.getMetadata().get(ApiKeyService.API_KEY_METADATA_KEY); if (apiKeyMetadata != null) { - final Tuple> tuple = - XContentHelper.convertToMap((BytesReference) apiKeyMetadata, false, XContentType.JSON); + final Tuple> tuple = XContentHelper.convertToMap( + (BytesReference) apiKeyMetadata, + false, + XContentType.JSON + ); return tuple.v2(); } else { return Map.of(); @@ -1291,8 +1471,11 @@ public static final class ApiKeyDoc { private static final BytesReference NULL_BYTES = new BytesArray("null"); static final InstantiatingObjectParser PARSER; static { - InstantiatingObjectParser.Builder builder = - InstantiatingObjectParser.builder("api_key_doc", true, ApiKeyDoc.class); + InstantiatingObjectParser.Builder builder = InstantiatingObjectParser.builder( + "api_key_doc", + true, + ApiKeyDoc.class + ); builder.declareString(constructorArg(), new ParseField("doc_type")); builder.declareLong(constructorArg(), new ParseField("creation_time")); builder.declareLongOrNull(constructorArg(), -1, new ParseField("expiration_time")); @@ -1333,7 +1516,8 @@ public ApiKeyDoc( BytesReference roleDescriptorsBytes, BytesReference limitedByRoleDescriptorsBytes, Map creator, - @Nullable BytesReference metadataFlattened) { + @Nullable BytesReference metadataFlattened + ) { this.docType = docType; this.creationTime = creationTime; @@ -1352,8 +1536,9 @@ public CachedApiKeyDoc toCachedApiKeyDoc() { final MessageDigest digest = MessageDigests.sha256(); final String roleDescriptorsHash = MessageDigests.toHexString(MessageDigests.digest(roleDescriptorsBytes, digest)); digest.reset(); - final String limitedByRoleDescriptorsHash = - MessageDigests.toHexString(MessageDigests.digest(limitedByRoleDescriptorsBytes, digest)); + final String limitedByRoleDescriptorsHash = MessageDigests.toHexString( + MessageDigests.digest(limitedByRoleDescriptorsBytes, digest) + ); return new CachedApiKeyDoc( creationTime, expirationTime, @@ -1364,7 +1549,8 @@ public CachedApiKeyDoc toCachedApiKeyDoc() { creator, roleDescriptorsHash, limitedByRoleDescriptorsHash, - metadataFlattened); + metadataFlattened + ); } static ApiKeyDoc fromXContent(XContentParser parser) { @@ -1391,13 +1577,17 @@ public static final class CachedApiKeyDoc { final BytesReference metadataFlattened; public CachedApiKeyDoc( - long creationTime, long expirationTime, + long creationTime, + long expirationTime, Boolean invalidated, String hash, - String name, int version, Map creator, + String name, + int version, + Map creator, String roleDescriptorsHash, String limitedByRoleDescriptorsHash, - @Nullable BytesReference metadataFlattened) { + @Nullable BytesReference metadataFlattened + ) { this.creationTime = creationTime; this.expirationTime = expirationTime; this.invalidated = invalidated; @@ -1422,7 +1612,8 @@ public ApiKeyDoc toApiKeyDoc(BytesReference roleDescriptorsBytes, BytesReference roleDescriptorsBytes, limitedByRoleDescriptorsBytes, creator, - metadataFlattened); + metadataFlattened + ); } } @@ -1467,10 +1658,11 @@ public void putIfNoInvalidationSince(String docId, ApiKeyDoc apiKeyDoc, long inv lockingAtomicCounter.compareAndRun(invalidationCount, () -> { docCache.put(docId, cachedApiKeyDoc); try { + roleDescriptorsBytesCache.computeIfAbsent(cachedApiKeyDoc.roleDescriptorsHash, k -> apiKeyDoc.roleDescriptorsBytes); roleDescriptorsBytesCache.computeIfAbsent( - cachedApiKeyDoc.roleDescriptorsHash, k -> apiKeyDoc.roleDescriptorsBytes); - roleDescriptorsBytesCache.computeIfAbsent( - cachedApiKeyDoc.limitedByRoleDescriptorsHash, k -> apiKeyDoc.limitedByRoleDescriptorsBytes); + cachedApiKeyDoc.limitedByRoleDescriptorsHash, + k -> apiKeyDoc.limitedByRoleDescriptorsBytes + ); } catch (ExecutionException e) { throw new RuntimeException(e); } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/AuthenticationService.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/AuthenticationService.java index cae0a969d627f..6c9b4c00852d1 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/AuthenticationService.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/AuthenticationService.java @@ -52,12 +52,21 @@ */ public class AuthenticationService { - static final Setting SUCCESS_AUTH_CACHE_ENABLED = - Setting.boolSetting("xpack.security.authc.success_cache.enabled", true, Property.NodeScope); - private static final Setting SUCCESS_AUTH_CACHE_MAX_SIZE = - Setting.intSetting("xpack.security.authc.success_cache.size", 10000, Property.NodeScope); - private static final Setting SUCCESS_AUTH_CACHE_EXPIRE_AFTER_ACCESS = - Setting.timeSetting("xpack.security.authc.success_cache.expire_after_access", TimeValue.timeValueHours(1L), Property.NodeScope); + static final Setting SUCCESS_AUTH_CACHE_ENABLED = Setting.boolSetting( + "xpack.security.authc.success_cache.enabled", + true, + Property.NodeScope + ); + private static final Setting SUCCESS_AUTH_CACHE_MAX_SIZE = Setting.intSetting( + "xpack.security.authc.success_cache.size", + 10000, + Property.NodeScope + ); + private static final Setting SUCCESS_AUTH_CACHE_EXPIRE_AFTER_ACCESS = Setting.timeSetting( + "xpack.security.authc.success_cache.expire_after_access", + TimeValue.timeValueHours(1L), + Property.NodeScope + ); private static final Logger logger = LogManager.getLogger(AuthenticationService.class); private final Realms realms; @@ -68,11 +77,18 @@ public class AuthenticationService { private final AtomicLong numInvalidation = new AtomicLong(); private final AuthenticatorChain authenticatorChain; - public AuthenticationService(Settings settings, Realms realms, AuditTrailService auditTrailService, - AuthenticationFailureHandler failureHandler, ThreadPool threadPool, - AnonymousUser anonymousUser, TokenService tokenService, ApiKeyService apiKeyService, - ServiceAccountService serviceAccountService, - OperatorPrivilegesService operatorPrivilegesService) { + public AuthenticationService( + Settings settings, + Realms realms, + AuditTrailService auditTrailService, + AuthenticationFailureHandler failureHandler, + ThreadPool threadPool, + AnonymousUser anonymousUser, + TokenService tokenService, + ApiKeyService apiKeyService, + ServiceAccountService serviceAccountService, + OperatorPrivilegesService operatorPrivilegesService + ) { this.realms = realms; this.auditTrailService = auditTrailService; this.failureHandler = failureHandler; @@ -128,7 +144,8 @@ public void authenticate(RestRequest request, boolean allowAnonymous, ActionList new AuditableRestRequest(auditTrailService.get(), failureHandler, threadContext, request), null, allowAnonymous, - realms); + realms + ); authenticatorChain.authenticateAsync(context, authenticationListener); } @@ -140,7 +157,7 @@ public void authenticate(RestRequest request, boolean allowAnonymous, ActionList * @param action The action of the message * @param transportRequest The request to be authenticated * @param fallbackUser The default user that will be assumed if no other user is attached to the message. May not - * be {@code null}. + * be {@code null}. */ public void authenticate(String action, TransportRequest transportRequest, User fallbackUser, ActionListener listener) { Objects.requireNonNull(fallbackUser, "fallback user may not be null"); @@ -149,7 +166,8 @@ public void authenticate(String action, TransportRequest transportRequest, User new AuditableTransportRequest(auditTrailService.get(), failureHandler, threadContext, action, transportRequest), fallbackUser, false, - realms); + realms + ); authenticatorChain.authenticateAsync(context, listener); } @@ -165,14 +183,19 @@ public void authenticate(String action, TransportRequest transportRequest, User * @param allowAnonymous Whether to permit anonymous access for this request (this only relevant if the service is * configured for anonymous access). */ - public void authenticate(String action, TransportRequest transportRequest, boolean allowAnonymous, - ActionListener listener) { + public void authenticate( + String action, + TransportRequest transportRequest, + boolean allowAnonymous, + ActionListener listener + ) { final Authenticator.Context context = new Authenticator.Context( threadContext, new AuditableTransportRequest(auditTrailService.get(), failureHandler, threadContext, action, transportRequest), null, allowAnonymous, - realms); + realms + ); authenticatorChain.authenticateAsync(context, listener); } @@ -183,14 +206,19 @@ public void authenticate(String action, TransportRequest transportRequest, boole * @param transportRequest The message that resulted in this authenticate call * @param token The token (credentials) to be authenticated */ - public void authenticate(String action, TransportRequest transportRequest, - AuthenticationToken token, ActionListener listener) { + public void authenticate( + String action, + TransportRequest transportRequest, + AuthenticationToken token, + ActionListener listener + ) { final Authenticator.Context context = new Authenticator.Context( - threadContext, - new AuditableTransportRequest(auditTrailService.get(), failureHandler, threadContext, action, transportRequest), - null, - true, - realms); + threadContext, + new AuditableTransportRequest(auditTrailService.get(), failureHandler, threadContext, action, transportRequest), + null, + true, + realms + ); context.addAuthenticationToken(token); authenticatorChain.authenticateAsync(context, listener); } @@ -258,12 +286,17 @@ static class AuditableTransportRequest extends AuditableRequest { private final TransportRequest transportRequest; private final String requestId; - AuditableTransportRequest(AuditTrail auditTrail, AuthenticationFailureHandler failureHandler, ThreadContext threadContext, - String action, TransportRequest transportRequest) { + AuditableTransportRequest( + AuditTrail auditTrail, + AuthenticationFailureHandler failureHandler, + ThreadContext threadContext, + String action, + TransportRequest transportRequest + ) { super(auditTrail, failureHandler, threadContext); this.action = action; this.transportRequest = transportRequest; - // There might be an existing audit-id (e.g. generated by the rest request) but there might not be (e.g. an internal action) + // There might be an existing audit-id (e.g. generated by the rest request) but there might not be (e.g. an internal action) this.requestId = AuditUtil.getOrGenerateRequestId(threadContext); } @@ -323,8 +356,12 @@ static class AuditableRestRequest extends AuditableRequest { private final RestRequest request; private final String requestId; - AuditableRestRequest(AuditTrail auditTrail, AuthenticationFailureHandler failureHandler, ThreadContext threadContext, - RestRequest request) { + AuditableRestRequest( + AuditTrail auditTrail, + AuthenticationFailureHandler failureHandler, + ThreadContext threadContext, + RestRequest request + ) { super(auditTrail, failureHandler, threadContext); this.request = request; // There should never be an existing audit-id when processing a rest request. diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/AuthenticatorChain.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/AuthenticatorChain.java index 3085f2cfa4cfc..b44fde0d28654 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/AuthenticatorChain.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/AuthenticatorChain.java @@ -65,12 +65,7 @@ class AuthenticatorChain { this.isAnonymousUserEnabled = AnonymousUser.isAnonymousEnabled(settings); this.authenticationSerializer = authenticationSerializer; this.realmsAuthenticator = realmsAuthenticator; - this.allAuthenticators = List.of( - serviceAccountAuthenticator, - oAuth2TokenAuthenticator, - apiKeyAuthenticator, - realmsAuthenticator - ); + this.allAuthenticators = List.of(serviceAccountAuthenticator, oAuth2TokenAuthenticator, apiKeyAuthenticator, realmsAuthenticator); } void authenticateAsync(Authenticator.Context context, ActionListener originalListener) { @@ -107,19 +102,13 @@ void authenticateAsync(Authenticator.Context context, ActionListener listener) { + private void authenticateAsyncWithExistingAuthenticationToken(Authenticator.Context context, ActionListener listener) { assert context.getMostRecentAuthenticationToken() != null : "existing authentication token must not be null"; context.setHandleNullToken(false); // already has a token, should not try null token doAuthenticate(context, false, listener); } - private void doAuthenticate( - Authenticator.Context context, - boolean shouldExtractCredentials, - ActionListener listener - ) { + private void doAuthenticate(Authenticator.Context context, boolean shouldExtractCredentials, ActionListener listener) { // The iterating listener walks through the list of Authenticators and attempts to authenticate using // each Authenticator (and optionally asks it to extract the authenticationToken). // Depending on the authentication result from each Authenticator, the iteration may stop earlier @@ -142,7 +131,8 @@ private void doAuthenticate( allAuthenticators, context.getThreadContext(), Function.identity(), - result -> result.getStatus() == AuthenticationResult.Status.CONTINUE); + result -> result.getStatus() == AuthenticationResult.Status.CONTINUE + ); iterListener.run(); } @@ -221,10 +211,13 @@ private void maybeLookupRunAsUser( final User user = authentication.getUser(); if (runAsUsername.isEmpty()) { logger.debug("user [{}] attempted to runAs with an empty username", user.principal()); - listener.onFailure(context.getRequest() - .runAsDenied(new Authentication(new User(runAsUsername, null, user), - authentication.getAuthenticatedBy(), - null), context.getMostRecentAuthenticationToken())); + listener.onFailure( + context.getRequest() + .runAsDenied( + new Authentication(new User(runAsUsername, null, user), authentication.getAuthenticatedBy(), null), + context.getMostRecentAuthenticationToken() + ) + ); return; } @@ -252,9 +245,13 @@ private Authentication lookForExistingAuthentication(Authenticator.Context conte try { authentication = authenticationSerializer.readFromContext(context.getThreadContext()); } catch (Exception e) { - logger.error(() -> new ParameterizedMessage( - "caught exception while trying to read authentication from request [{}]", - context.getRequest()), e); + logger.error( + () -> new ParameterizedMessage( + "caught exception while trying to read authentication from request [{}]", + context.getRequest() + ), + e + ); throw context.getRequest().tamperedRequest(); } if (authentication != null && context.getRequest() instanceof AuthenticationService.AuditableRestRequest) { @@ -286,26 +283,32 @@ void handleNullToken(Authenticator.Context context, ActionListener eseWithPreviousCredentials.addHeader(k, ese.getHeader(k))); addMetadata(context, eseWithPreviousCredentials); listener.onFailure(eseWithPreviousCredentials); @@ -356,11 +364,12 @@ void writeAuthToContext(Authenticator.Context context, Authentication authentica authenticationSerializer.writeToContext(authentication, context.getThreadContext()); context.getRequest().authenticationSuccess(authentication); } catch (Exception e) { - logger.debug(new ParameterizedMessage("Failed to store authentication [{}] for request [{}]", - authentication, - context.getRequest()), e); - final ElasticsearchSecurityException ese = context.getRequest().exceptionProcessingRequest( - e, context.getMostRecentAuthenticationToken()); + logger.debug( + new ParameterizedMessage("Failed to store authentication [{}] for request [{}]", authentication, context.getRequest()), + e + ); + final ElasticsearchSecurityException ese = context.getRequest() + .exceptionProcessingRequest(e, context.getMostRecentAuthenticationToken()); addMetadata(context, ese); listener.onFailure(ese); return; @@ -392,9 +401,9 @@ private boolean shouldFallbackToAnonymous(Authenticator.Context context) { return false; } String header = context.getThreadContext().getHeader("Authorization"); - if (Strings.hasText(header) && - ((header.regionMatches(true, 0, "Bearer ", 0, "Bearer ".length()) && header.length() > "Bearer ".length()) || - (header.regionMatches(true, 0, "ApiKey ", 0, "ApiKey ".length()) && header.length() > "ApiKey ".length()))) { + if (Strings.hasText(header) + && ((header.regionMatches(true, 0, "Bearer ", 0, "Bearer ".length()) && header.length() > "Bearer ".length()) + || (header.regionMatches(true, 0, "ApiKey ", 0, "ApiKey ".length()) && header.length() > "ApiKey ".length()))) { return false; } return true; diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ExpiredApiKeysRemover.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ExpiredApiKeysRemover.java index 3f1612ea3e6d5..c27d6b3f1de88 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ExpiredApiKeysRemover.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ExpiredApiKeysRemover.java @@ -14,8 +14,8 @@ import org.elasticsearch.action.bulk.BulkItemResponse; import org.elasticsearch.client.Client; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.AbstractRunnable; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.reindex.BulkByScrollResponse; import org.elasticsearch.index.reindex.DeleteByQueryAction; @@ -58,19 +58,18 @@ public void doRun() { expiredDbq.getSearchRequest().source().timeout(timeout); } final Instant now = Instant.now(); - expiredDbq - .setQuery(QueryBuilders.boolQuery() + expiredDbq.setQuery( + QueryBuilders.boolQuery() .filter(QueryBuilders.termsQuery("doc_type", "api_key")) .should(QueryBuilders.termsQuery("api_key_invalidated", true)) .should(QueryBuilders.rangeQuery("expiration_time").lte(now.minus(EXPIRED_API_KEYS_RETENTION_PERIOD).toEpochMilli())) .minimumShouldMatch(1) - ); + ); - executeAsyncWithOrigin(client, SECURITY_ORIGIN, DeleteByQueryAction.INSTANCE, expiredDbq, - ActionListener.wrap(r -> { - debugDbqResponse(r); - markComplete(); - }, this::onFailure)); + executeAsyncWithOrigin(client, SECURITY_ORIGIN, DeleteByQueryAction.INSTANCE, expiredDbq, ActionListener.wrap(r -> { + debugDbqResponse(r); + markComplete(); + }, this::onFailure)); } void submit(ThreadPool threadPool) { @@ -81,15 +80,28 @@ void submit(ThreadPool threadPool) { private void debugDbqResponse(BulkByScrollResponse response) { if (logger.isDebugEnabled()) { - logger.debug("delete by query of api keys finished with [{}] deletions, [{}] bulk failures, [{}] search failures", - response.getDeleted(), response.getBulkFailures().size(), response.getSearchFailures().size()); + logger.debug( + "delete by query of api keys finished with [{}] deletions, [{}] bulk failures, [{}] search failures", + response.getDeleted(), + response.getBulkFailures().size(), + response.getSearchFailures().size() + ); for (BulkItemResponse.Failure failure : response.getBulkFailures()) { - logger.debug(new ParameterizedMessage("deletion failed for index [{}], id [{}]", - failure.getIndex(), failure.getId()), failure.getCause()); + logger.debug( + new ParameterizedMessage("deletion failed for index [{}], id [{}]", failure.getIndex(), failure.getId()), + failure.getCause() + ); } for (ScrollableHitSource.SearchFailure failure : response.getSearchFailures()) { - logger.debug(new ParameterizedMessage("search failed for index [{}], shard [{}] on node [{}]", - failure.getIndex(), failure.getShardId(), failure.getNodeId()), failure.getReason()); + logger.debug( + new ParameterizedMessage( + "search failed for index [{}], shard [{}] on node [{}]", + failure.getIndex(), + failure.getShardId(), + failure.getNodeId() + ), + failure.getReason() + ); } } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ExpiredTokenRemover.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ExpiredTokenRemover.java index 144cefd5bf005..87c949ba295fc 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ExpiredTokenRemover.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ExpiredTokenRemover.java @@ -6,16 +6,16 @@ */ package org.elasticsearch.xpack.security.authc; -import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.message.ParameterizedMessage; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.bulk.BulkItemResponse; import org.elasticsearch.client.Client; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.AbstractRunnable; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.reindex.BulkByScrollResponse; import org.elasticsearch.index.reindex.DeleteByQueryAction; @@ -51,8 +51,12 @@ final class ExpiredTokenRemover extends AbstractRunnable { private final TimeValue timeout; private boolean checkMainIndexForExpiredTokens; - ExpiredTokenRemover(Settings settings, Client client, SecurityIndexManager securityMainIndex, - SecurityIndexManager securityTokensIndex) { + ExpiredTokenRemover( + Settings settings, + Client client, + SecurityIndexManager securityMainIndex, + SecurityIndexManager securityTokensIndex + ) { this.client = client; this.securityMainIndex = securityMainIndex; this.securityTokensIndex = securityTokensIndex; @@ -80,24 +84,27 @@ public void doRun() { expiredDbq.getSearchRequest().source().timeout(timeout); } final Instant now = Instant.now(); - expiredDbq - .setQuery(QueryBuilders.boolQuery() + expiredDbq.setQuery( + QueryBuilders.boolQuery() .filter(QueryBuilders.termsQuery("doc_type", TokenService.TOKEN_DOC_TYPE)) - .filter(QueryBuilders.rangeQuery("creation_time") - .lte(now.minus(MAXIMUM_TOKEN_LIFETIME_HOURS, ChronoUnit.HOURS).toEpochMilli()))); + .filter( + QueryBuilders.rangeQuery("creation_time").lte(now.minus(MAXIMUM_TOKEN_LIFETIME_HOURS, ChronoUnit.HOURS).toEpochMilli()) + ) + ); logger.trace(() -> new ParameterizedMessage("Removing old tokens: [{}]", Strings.toString(expiredDbq))); - executeAsyncWithOrigin(client, SECURITY_ORIGIN, DeleteByQueryAction.INSTANCE, expiredDbq, - ActionListener.wrap(bulkResponse -> { - debugDbqResponse(bulkResponse); - // tokens can still linger on the main index for their maximum lifetime after the tokens index has been created, because - // only after the tokens index has been created all nodes will store tokens there and not on the main security index - if (checkMainIndexForExpiredTokens && securityTokensIndex.indexExists() - && securityTokensIndex.getCreationTime().isBefore(now.minus(MAXIMUM_TOKEN_LIFETIME_HOURS, ChronoUnit.HOURS)) - && bulkResponse.getBulkFailures().isEmpty() && bulkResponse.getSearchFailures().isEmpty()) { - checkMainIndexForExpiredTokens = false; - } - markComplete(); - }, this::onFailure)); + executeAsyncWithOrigin(client, SECURITY_ORIGIN, DeleteByQueryAction.INSTANCE, expiredDbq, ActionListener.wrap(bulkResponse -> { + debugDbqResponse(bulkResponse); + // tokens can still linger on the main index for their maximum lifetime after the tokens index has been created, because + // only after the tokens index has been created all nodes will store tokens there and not on the main security index + if (checkMainIndexForExpiredTokens + && securityTokensIndex.indexExists() + && securityTokensIndex.getCreationTime().isBefore(now.minus(MAXIMUM_TOKEN_LIFETIME_HOURS, ChronoUnit.HOURS)) + && bulkResponse.getBulkFailures().isEmpty() + && bulkResponse.getSearchFailures().isEmpty()) { + checkMainIndexForExpiredTokens = false; + } + markComplete(); + }, this::onFailure)); } void submit(ThreadPool threadPool) { @@ -108,15 +115,28 @@ void submit(ThreadPool threadPool) { private void debugDbqResponse(BulkByScrollResponse response) { if (logger.isDebugEnabled()) { - logger.debug("delete by query of tokens finished with [{}] deletions, [{}] bulk failures, [{}] search failures", - response.getDeleted(), response.getBulkFailures().size(), response.getSearchFailures().size()); + logger.debug( + "delete by query of tokens finished with [{}] deletions, [{}] bulk failures, [{}] search failures", + response.getDeleted(), + response.getBulkFailures().size(), + response.getSearchFailures().size() + ); for (BulkItemResponse.Failure failure : response.getBulkFailures()) { - logger.debug(new ParameterizedMessage("deletion failed for index [{}], id [{}]", - failure.getIndex(), failure.getId()), failure.getCause()); + logger.debug( + new ParameterizedMessage("deletion failed for index [{}], id [{}]", failure.getIndex(), failure.getId()), + failure.getCause() + ); } for (ScrollableHitSource.SearchFailure failure : response.getSearchFailures()) { - logger.debug(new ParameterizedMessage("search failed for index [{}], shard [{}] on node [{}]", - failure.getIndex(), failure.getShardId(), failure.getNodeId()), failure.getReason()); + logger.debug( + new ParameterizedMessage( + "search failed for index [{}], shard [{}] on node [{}]", + failure.getIndex(), + failure.getShardId(), + failure.getNodeId() + ), + failure.getReason() + ); } } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/InternalRealms.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/InternalRealms.java index f897355618f96..407de075d5029 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/InternalRealms.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/InternalRealms.java @@ -121,49 +121,54 @@ static LicensedFeature.Persistent getLicensedFeature(String type) { * * @return A map from realm-type to Factory */ - public static Map getFactories(ThreadPool threadPool, ResourceWatcherService resourceWatcherService, - SSLService sslService, NativeUsersStore nativeUsersStore, - NativeRoleMappingStore nativeRoleMappingStore, - SecurityIndexManager securityIndex) { + public static Map getFactories( + ThreadPool threadPool, + ResourceWatcherService resourceWatcherService, + SSLService sslService, + NativeUsersStore nativeUsersStore, + NativeRoleMappingStore nativeRoleMappingStore, + SecurityIndexManager securityIndex + ) { return Map.of( - // file realm - FileRealmSettings.TYPE, - config -> new FileRealm(config, resourceWatcherService, threadPool), - // native realm - NativeRealmSettings.TYPE, - config -> { - final NativeRealm nativeRealm = new NativeRealm(config, nativeUsersStore, threadPool); - securityIndex.addStateListener(nativeRealm::onSecurityIndexStateChange); - return nativeRealm; - }, - // active directory realm - LdapRealmSettings.AD_TYPE, - config -> new LdapRealm(config, sslService, resourceWatcherService, nativeRoleMappingStore, threadPool), - // LDAP realm - LdapRealmSettings.LDAP_TYPE, - config -> new LdapRealm(config, sslService, resourceWatcherService, nativeRoleMappingStore, threadPool), - // PKI realm - PkiRealmSettings.TYPE, - config -> new PkiRealm(config, resourceWatcherService, nativeRoleMappingStore), - // SAML realm - SamlRealmSettings.TYPE, - config -> SamlRealm.create(config, sslService, resourceWatcherService, nativeRoleMappingStore), - // Kerberos realm - KerberosRealmSettings.TYPE, - config -> new KerberosRealm(config, nativeRoleMappingStore, threadPool), - // OpenID Connect realm - OpenIdConnectRealmSettings.TYPE, - config -> new OpenIdConnectRealm(config, sslService, nativeRoleMappingStore, resourceWatcherService)); + // file realm + FileRealmSettings.TYPE, + config -> new FileRealm(config, resourceWatcherService, threadPool), + // native realm + NativeRealmSettings.TYPE, + config -> { + final NativeRealm nativeRealm = new NativeRealm(config, nativeUsersStore, threadPool); + securityIndex.addStateListener(nativeRealm::onSecurityIndexStateChange); + return nativeRealm; + }, + // active directory realm + LdapRealmSettings.AD_TYPE, + config -> new LdapRealm(config, sslService, resourceWatcherService, nativeRoleMappingStore, threadPool), + // LDAP realm + LdapRealmSettings.LDAP_TYPE, + config -> new LdapRealm(config, sslService, resourceWatcherService, nativeRoleMappingStore, threadPool), + // PKI realm + PkiRealmSettings.TYPE, + config -> new PkiRealm(config, resourceWatcherService, nativeRoleMappingStore), + // SAML realm + SamlRealmSettings.TYPE, + config -> SamlRealm.create(config, sslService, resourceWatcherService, nativeRoleMappingStore), + // Kerberos realm + KerberosRealmSettings.TYPE, + config -> new KerberosRealm(config, nativeRoleMappingStore, threadPool), + // OpenID Connect realm + OpenIdConnectRealmSettings.TYPE, + config -> new OpenIdConnectRealm(config, sslService, nativeRoleMappingStore, resourceWatcherService) + ); } - private InternalRealms() { - } + private InternalRealms() {} public static List getBootstrapChecks(final Settings globalSettings, final Environment env) { final Set realmTypes = Sets.newHashSet(LdapRealmSettings.AD_TYPE, LdapRealmSettings.LDAP_TYPE, PkiRealmSettings.TYPE); final List checks = RealmSettings.getRealmSettings(globalSettings) - .keySet().stream() + .keySet() + .stream() .filter(id -> realmTypes.contains(id.getType())) .map(id -> new RealmConfig(id, globalSettings, env, null)) .map(RoleMappingFileBootstrapCheck::create) diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/Realms.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/Realms.java index 4bbe4163a1c22..29c3816e82098 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/Realms.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/Realms.java @@ -65,8 +65,14 @@ public class Realms implements Iterable { // the realms in current use. This list will change dynamically as the license changes private volatile List activeRealms; - public Realms(Settings settings, Environment env, Map factories, XPackLicenseState licenseState, - ThreadContext threadContext, ReservedRealm reservedRealm) throws Exception { + public Realms( + Settings settings, + Environment env, + Map factories, + XPackLicenseState licenseState, + ThreadContext threadContext, + ReservedRealm reservedRealm + ) throws Exception { this.settings = settings; this.env = env; this.factories = factories; @@ -98,9 +104,9 @@ protected void recomputeActiveRealms() { // Stop license-tracking for any previously-active realms that are no longer allowed if (activeRealms != null) { - activeRealms.stream().filter(r -> licensedRealms.contains(r) == false).forEach(realm -> { - handleDisabledRealmDueToLicenseChange(realm, licenseStateSnapshot); - }); + activeRealms.stream() + .filter(r -> licensedRealms.contains(r) == false) + .forEach(realm -> { handleDisabledRealmDueToLicenseChange(realm, licenseStateSnapshot); }); } activeRealms = licensedRealms; @@ -109,11 +115,12 @@ protected void recomputeActiveRealms() { // Can be overridden in testing protected void handleDisabledRealmDueToLicenseChange(Realm realm, XPackLicenseState licenseStateSnapshot) { final LicensedFeature.Persistent feature = getLicensedFeatureForRealm(realm.type()); - assert feature != null : "Realm [" - + realm - + "] with no licensed feature became inactive due to change to license mode [" - + licenseStateSnapshot.getOperationMode() - + "]"; + assert feature != null + : "Realm [" + + realm + + "] with no licensed feature became inactive due to change to license mode [" + + licenseStateSnapshot.getOperationMode() + + "]"; feature.stopTracking(licenseStateSnapshot, realm.name()); logger.warn( "The [{}.{}] realm has been automatically disabled due to a change in license [{}]", @@ -152,9 +159,7 @@ public List getActiveRealms() { // Protected for testing protected List calculateLicensedRealms(XPackLicenseState licenseStateSnapshot) { - return allConfiguredRealms.stream() - .filter(r -> checkLicense(r, licenseStateSnapshot)) - .collect(Collectors.toUnmodifiableList()); + return allConfiguredRealms.stream().filter(r -> checkLicense(r, licenseStateSnapshot)).collect(Collectors.toUnmodifiableList()); } private static boolean checkLicense(Realm realm, XPackLicenseState licenseState) { @@ -201,7 +206,7 @@ protected List initRealms(List realmConfigs) throws Exceptio Map> nameToRealmIdentifier = new HashMap<>(); Map> orderToRealmName = new HashMap<>(); List reservedPrefixedRealmIdentifiers = new ArrayList<>(); - for (RealmConfig config: realmConfigs) { + for (RealmConfig config : realmConfigs) { Realm.Factory factory = factories.get(config.identifier().getType()); assert factory != null : "unknown realm type [" + config.identifier().getType() + "]"; if (config.identifier().getName().startsWith(RealmSettings.RESERVED_REALM_NAME_PREFIX)) { @@ -214,10 +219,9 @@ protected List initRealms(List realmConfigs) throws Exceptio continue; } Realm realm = factory.create(config); - nameToRealmIdentifier.computeIfAbsent(realm.name(), k -> - new HashSet<>()).add(RealmSettings.realmSettingPrefix(realm.type()) + realm.name()); - orderToRealmName.computeIfAbsent(realm.order(), k -> new HashSet<>()) - .add(realm.name()); + nameToRealmIdentifier.computeIfAbsent(realm.name(), k -> new HashSet<>()) + .add(RealmSettings.realmSettingPrefix(realm.type()) + realm.name()); + orderToRealmName.computeIfAbsent(realm.order(), k -> new HashSet<>()).add(realm.name()); realms.add(realm); } @@ -227,7 +231,8 @@ protected List initRealms(List realmConfigs) throws Exceptio maybeAddBasicRealms(realms, findDisabledBasicRealmTypes(realmConfigs)); // always add built in first! realms.add(0, reservedRealm); - String duplicateRealms = nameToRealmIdentifier.entrySet().stream() + String duplicateRealms = nameToRealmIdentifier.entrySet() + .stream() .filter(entry -> entry.getValue().size() > 1) .map(entry -> entry.getKey() + ": " + entry.getValue()) .collect(Collectors.joining("; ")); @@ -278,26 +283,25 @@ public void usageStats(ActionListener> listener) { } else { for (Realm realm : realmList) { realm.usageStats(ActionListener.wrap(stats -> { - if (failed.get() == false) { - synchronized (realmMap) { - realmMap.compute(realm.type(), (key, value) -> { - if (value == null) { - Object realmTypeUsage = convertToMapOfLists(stats); - return realmTypeUsage; - } - assert value instanceof Map; - combineMaps((Map) value, stats); - return value; - }); - } - doCountDown.run(); - } - }, - e -> { - if (failed.compareAndSet(false, true)) { - listener.onFailure(e); + if (failed.get() == false) { + synchronized (realmMap) { + realmMap.compute(realm.type(), (key, value) -> { + if (value == null) { + Object realmTypeUsage = convertToMapOfLists(stats); + return realmTypeUsage; + } + assert value instanceof Map; + combineMaps((Map) value, stats); + return value; + }); } - })); + doCountDown.run(); + } + }, e -> { + if (failed.compareAndSet(false, true)) { + listener.onFailure(e); + } + })); } } } @@ -307,17 +311,21 @@ private void maybeAddBasicRealms(List realms, Set disabledBasicRe // Add native realm first so that file realm will be in the beginning if (false == disabledBasicRealmTypes.contains(NativeRealmSettings.TYPE) && false == realmTypes.contains(NativeRealmSettings.TYPE)) { var nativeRealmId = new RealmConfig.RealmIdentifier(NativeRealmSettings.TYPE, NativeRealmSettings.DEFAULT_NAME); - realms.add(0, factories.get(NativeRealmSettings.TYPE).create(new RealmConfig( - nativeRealmId, - ensureOrderSetting(settings, nativeRealmId, Integer.MIN_VALUE), - env, threadContext))); + realms.add( + 0, + factories.get(NativeRealmSettings.TYPE) + .create( + new RealmConfig(nativeRealmId, ensureOrderSetting(settings, nativeRealmId, Integer.MIN_VALUE), env, threadContext) + ) + ); } if (false == disabledBasicRealmTypes.contains(FileRealmSettings.TYPE) && false == realmTypes.contains(FileRealmSettings.TYPE)) { var fileRealmId = new RealmConfig.RealmIdentifier(FileRealmSettings.TYPE, FileRealmSettings.DEFAULT_NAME); - realms.add(0, factories.get(FileRealmSettings.TYPE).create(new RealmConfig( - fileRealmId, - ensureOrderSetting(settings, fileRealmId, Integer.MIN_VALUE), - env, threadContext))); + realms.add( + 0, + factories.get(FileRealmSettings.TYPE) + .create(new RealmConfig(fileRealmId, ensureOrderSetting(settings, fileRealmId, Integer.MIN_VALUE), env, threadContext)) + ); } } @@ -327,7 +335,8 @@ private Settings ensureOrderSetting(Settings settings, RealmConfig.RealmIdentifi } private void checkUniqueOrders(Map> orderToRealmName) { - String duplicateOrders = orderToRealmName.entrySet().stream() + String duplicateOrders = orderToRealmName.entrySet() + .stream() .filter(entry -> entry.getValue().size() > 1) .map(entry -> entry.getKey() + ": " + entry.getValue()) .collect(Collectors.joining("; ")); @@ -351,17 +360,29 @@ private List buildRealmConfigs() { // this is an internal realm factory, let's make sure we didn't already registered one // (there can only be one instance of an internal realm) if (internalTypes.contains(identifier.getType())) { - throw new IllegalArgumentException("multiple [" + identifier.getType() + "] realms are configured. [" - + identifier.getType() + "] is an internal realm and therefore there can only be one such realm configured"); + throw new IllegalArgumentException( + "multiple [" + + identifier.getType() + + "] realms are configured. [" + + identifier.getType() + + "] is an internal realm and therefore there can only be one such realm configured" + ); } internalTypes.add(identifier.getType()); } if (KerberosRealmSettings.TYPE.equals(identifier.getType())) { kerberosRealmNames.add(identifier.getName()); if (kerberosRealmNames.size() > 1) { - throw new IllegalArgumentException("multiple realms " + kerberosRealmNames.toString() + " configured of type [" - + identifier.getType() + "], [" + identifier.getType() + "] can only have one such realm " + - "configured"); + throw new IllegalArgumentException( + "multiple realms " + + kerberosRealmNames.toString() + + " configured of type [" + + identifier.getType() + + "], [" + + identifier.getType() + + "] can only have one such realm " + + "configured" + ); } } realmConfigs.add(config); @@ -379,18 +400,23 @@ private Set findDisabledBasicRealmTypes(List realmConfigs) private void logDeprecationForReservedPrefixedRealmNames(List realmIdentifiers) { if (false == realmIdentifiers.isEmpty()) { - deprecationLogger.critical(DeprecationCategory.SECURITY, "realm_name_with_reserved_prefix", - "Found realm " + (realmIdentifiers.size() == 1 ? "name" : "names") + " with reserved prefix [{}]: [{}]. " + - "In a future major release, node will fail to start if any realm names start with reserved prefix.", + deprecationLogger.critical( + DeprecationCategory.SECURITY, + "realm_name_with_reserved_prefix", + "Found realm " + + (realmIdentifiers.size() == 1 ? "name" : "names") + + " with reserved prefix [{}]: [{}]. " + + "In a future major release, node will fail to start if any realm names start with reserved prefix.", RealmSettings.RESERVED_REALM_NAME_PREFIX, realmIdentifiers.stream() .map(rid -> RealmSettings.PREFIX + rid.getType() + "." + rid.getName()) .sorted() - .collect(Collectors.joining("; "))); + .collect(Collectors.joining("; ")) + ); } } - @SuppressWarnings({"unchecked", "rawtypes"}) + @SuppressWarnings({ "unchecked", "rawtypes" }) private static void combineMaps(Map mapA, Map mapB) { for (Entry entry : mapB.entrySet()) { mapA.compute(entry.getKey(), (key, value) -> { diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/RealmsAuthenticator.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/RealmsAuthenticator.java index c9e7041137729..a8240ec04ab74 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/RealmsAuthenticator.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/RealmsAuthenticator.java @@ -46,8 +46,7 @@ class RealmsAuthenticator implements Authenticator { private final Cache lastSuccessfulAuthCache; private boolean authenticationTokenExtracted = false; - RealmsAuthenticator( - String nodeName, AtomicLong numInvalidation, Cache lastSuccessfulAuthCache) { + RealmsAuthenticator(String nodeName, AtomicLong numInvalidation, Cache lastSuccessfulAuthCache) { this.nodeName = nodeName; this.numInvalidation = numInvalidation; this.lastSuccessfulAuthCache = lastSuccessfulAuthCache; @@ -76,8 +75,9 @@ public boolean canBeFollowedByNullTokenHandler() { @Override public void authenticate(Context context, ActionListener> listener) { if (context.getMostRecentAuthenticationToken() == null) { - listener.onFailure(new ElasticsearchSecurityException( - "authentication token must present for realms authentication", RestStatus.UNAUTHORIZED)); + listener.onFailure( + new ElasticsearchSecurityException("authentication token must present for realms authentication", RestStatus.UNAUTHORIZED) + ); return; } assert context.getMostRecentAuthenticationToken() != null : "null token should be handled by fallback authenticator"; @@ -95,10 +95,12 @@ AuthenticationToken extractToken(Context context) { for (Realm realm : context.getDefaultOrderedRealmList()) { final AuthenticationToken token = realm.token(context.getThreadContext()); if (token != null) { - logger.trace("Found authentication credentials [{}] for principal [{}] in request [{}]", + logger.trace( + "Found authentication credentials [{}] for principal [{}] in request [{}]", token.getClass().getName(), token.principal(), - context.getRequest()); + context.getRequest() + ); return token; } } @@ -108,10 +110,11 @@ AuthenticationToken extractToken(Context context) { } if (context.getUnlicensedRealms().isEmpty() == false) { logger.warn( - "No authentication credential could be extracted using realms [{}]." + - " Realms [{}] were skipped because they are not permitted on the current license", + "No authentication credential could be extracted using realms [{}]." + + " Realms [{}] were skipped because they are not permitted on the current license", Strings.collectionToCommaDelimitedString(context.getDefaultOrderedRealmList()), - Strings.collectionToCommaDelimitedString(context.getUnlicensedRealms())); + Strings.collectionToCommaDelimitedString(context.getUnlicensedRealms()) + ); } return null; } @@ -126,8 +129,7 @@ AuthenticationToken extractToken(Context context) { private void consumeToken(Context context, ActionListener> listener) { final AuthenticationToken authenticationToken = context.getMostRecentAuthenticationToken(); final List realmsList = getRealmList(context, authenticationToken.principal()); - logger.trace("Checking token of type [{}] against [{}] realm(s)", - authenticationToken.getClass().getName(), realmsList.size()); + logger.trace("Checking token of type [{}] against [{}] realm(s)", authenticationToken.getClass().getName(), realmsList.size()); final long startInvalidation = numInvalidation.get(); final Map> messages = new LinkedHashMap<>(); @@ -137,17 +139,21 @@ private void consumeToken(Context context, ActionListener> realmAuthenticatingConsumer = (realm, userListener) -> { if (realm.supports(authenticationToken)) { - logger.trace("Trying to authenticate [{}] using realm [{}] with token [{}] ", + logger.trace( + "Trying to authenticate [{}] using realm [{}] with token [{}] ", authenticationToken.principal(), realm, - authenticationToken.getClass().getName()); + authenticationToken.getClass().getName() + ); realm.authenticate(authenticationToken, ActionListener.wrap(result -> { assert result != null : "Realm " + realm + " produced a null authentication result"; - logger.debug("Authentication of [{}] using realm [{}] with token [{}] was [{}]", + logger.debug( + "Authentication of [{}] using realm [{}] with token [{}] was [{}]", authenticationToken.principal(), realm, authenticationToken.getClass().getSimpleName(), - result); + result + ); if (result.getStatus() == AuthenticationResult.Status.SUCCESS) { // user was authenticated, populate the authenticated by information authenticatedByRef.set(new Authentication.RealmRef(realm.name(), realm.type(), nodeName)); @@ -161,15 +167,22 @@ private void consumeToken(Context context, ActionListener { - logger.warn(new ParameterizedMessage("An error occurred while attempting to authenticate [{}] against realm [{}]", - authenticationToken.principal(), - realm.name()), ex); + logger.warn( + new ParameterizedMessage( + "An error occurred while attempting to authenticate [{}] against realm [{}]", + authenticationToken.principal(), + realm.name() + ), + ex + ); userListener.onFailure(ex); })); } else { @@ -191,35 +209,37 @@ private void consumeToken(Context context, ActionListener authenticatingListener = new IteratingActionListener<>( - ContextPreservingActionListener.wrapPreservingContext(ActionListener.wrap( - user -> { - if (user == null) { - consumeNullUser(context, messages, listener); - } else { - final AuthenticationResult result = authenticationResultRef.get(); - assert result != null : "authentication result must not be null when user is not null"; - context.getThreadContext().putTransient(AuthenticationResult.THREAD_CONTEXT_KEY, result); - listener.onResponse(AuthenticationResult.success( - new Authentication(user, authenticatedByRef.get(), null))); - } - }, - e -> { - if (e != null) { - listener.onFailure(context.getRequest().exceptionProcessingRequest(e, authenticationToken)); - } else { - listener.onFailure(context.getRequest().authenticationFailed(authenticationToken)); - } - }), context.getThreadContext()), - realmAuthenticatingConsumer, realmsList, context.getThreadContext() + ContextPreservingActionListener.wrapPreservingContext(ActionListener.wrap(user -> { + if (user == null) { + consumeNullUser(context, messages, listener); + } else { + final AuthenticationResult result = authenticationResultRef.get(); + assert result != null : "authentication result must not be null when user is not null"; + context.getThreadContext().putTransient(AuthenticationResult.THREAD_CONTEXT_KEY, result); + listener.onResponse(AuthenticationResult.success(new Authentication(user, authenticatedByRef.get(), null))); + } + }, e -> { + if (e != null) { + listener.onFailure(context.getRequest().exceptionProcessingRequest(e, authenticationToken)); + } else { + listener.onFailure(context.getRequest().authenticationFailed(authenticationToken)); + } + }), context.getThreadContext()), + realmAuthenticatingConsumer, + realmsList, + context.getThreadContext() ); try { authenticatingListener.run(); } catch (Exception e) { logger.debug( - new ParameterizedMessage("Authentication of [{}] with token [{}] failed", + new ParameterizedMessage( + "Authentication of [{}] with token [{}] failed", authenticationToken.principal(), - authenticationToken.getClass().getName()), - e); + authenticationToken.getClass().getName() + ), + e + ); listener.onFailure(context.getRequest().exceptionProcessingRequest(e, authenticationToken)); } } @@ -238,10 +258,12 @@ private void consumeNullUser( logger.warn("Authentication to realm {} failed - {}{}", realm.name(), message, cause); }); if (context.getUnlicensedRealms().isEmpty() == false) { - logger.warn("Authentication failed using realms [{}]." + logger.warn( + "Authentication failed using realms [{}]." + " Realms [{}] were skipped because they are not permitted on the current license", Strings.collectionToCommaDelimitedString(context.getDefaultOrderedRealmList()), - Strings.collectionToCommaDelimitedString(context.getUnlicensedRealms())); + Strings.collectionToCommaDelimitedString(context.getUnlicensedRealms()) + ); } logger.trace("Failed to authenticate request [{}]", context.getRequest()); listener.onFailure(context.getRequest().authenticationFailed(context.getMostRecentAuthenticationToken())); @@ -265,8 +287,11 @@ public void lookupRunAsUser( final long startInvalidationNum = numInvalidation.get(); lookup.lookup(runAsUsername, ActionListener.wrap(tuple -> { if (tuple == null) { - logger.debug("Cannot find run-as user [{}] for authenticated user [{}]", - runAsUsername, authentication.getUser().principal()); + logger.debug( + "Cannot find run-as user [{}] for authenticated user [{}]", + runAsUsername, + authentication.getUser().principal() + ); listener.onResponse(null); } else { User foundUser = Objects.requireNonNull(tuple.v1()); @@ -278,16 +303,25 @@ public void lookupRunAsUser( } logger.trace("Using run-as user [{}] with authenticated user [{}]", foundUser, authentication.getUser().principal()); listener.onResponse( - new Tuple<>(tuple.v1(), new Authentication.RealmRef(tuple.v2().name(), tuple.v2().type(), nodeName))); + new Tuple<>(tuple.v1(), new Authentication.RealmRef(tuple.v2().name(), tuple.v2().type(), nodeName)) + ); } }, e -> listener.onFailure(context.getRequest().exceptionProcessingRequest(e, context.getMostRecentAuthenticationToken())))); } else if (runAsUsername == null) { listener.onResponse(null); } else { logger.debug("user [{}] attempted to runAs with an empty username", authentication.getUser().principal()); - listener.onFailure(context.getRequest().runAsDenied( - new Authentication(new User(runAsUsername, null, authentication.getUser()), authentication.getAuthenticatedBy(), null), - context.getMostRecentAuthenticationToken())); + listener.onFailure( + context.getRequest() + .runAsDenied( + new Authentication( + new User(runAsUsername, null, authentication.getUser()), + authentication.getAuthenticatedBy(), + null + ), + context.getMostRecentAuthenticationToken() + ) + ); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/TokenService.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/TokenService.java index c65d71de58275..cab1c8fe508a4 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/TokenService.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/TokenService.java @@ -64,9 +64,6 @@ import org.elasticsearch.common.util.concurrent.AbstractRunnable; import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.common.util.iterable.Iterables; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; import org.elasticsearch.core.Tuple; @@ -82,6 +79,9 @@ import org.elasticsearch.rest.RestStatus; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.XPackField; import org.elasticsearch.xpack.core.XPackPlugin; import org.elasticsearch.xpack.core.XPackSettings; @@ -136,6 +136,7 @@ import java.util.function.Predicate; import java.util.function.Supplier; import java.util.stream.Collectors; + import javax.crypto.Cipher; import javax.crypto.CipherInputStream; import javax.crypto.CipherOutputStream; @@ -174,19 +175,32 @@ public final class TokenService { static final int IV_BYTES = 12; private static final int VERSION_BYTES = 4; private static final String ENCRYPTION_CIPHER = "AES/GCM/NoPadding"; - private static final String EXPIRED_TOKEN_WWW_AUTH_VALUE = "Bearer realm=\"" + XPackField.SECURITY + - "\", error=\"invalid_token\", error_description=\"The access token expired\""; - private static final String MALFORMED_TOKEN_WWW_AUTH_VALUE = "Bearer realm=\"" + XPackField.SECURITY + - "\", error=\"invalid_token\", error_description=\"The access token is malformed\""; + private static final String EXPIRED_TOKEN_WWW_AUTH_VALUE = "Bearer realm=\"" + + XPackField.SECURITY + + "\", error=\"invalid_token\", error_description=\"The access token expired\""; + private static final String MALFORMED_TOKEN_WWW_AUTH_VALUE = "Bearer realm=\"" + + XPackField.SECURITY + + "\", error=\"invalid_token\", error_description=\"The access token is malformed\""; private static final BackoffPolicy DEFAULT_BACKOFF = BackoffPolicy.exponentialBackoff(); public static final String THREAD_POOL_NAME = XPackField.SECURITY + "-token-key"; - public static final Setting TOKEN_EXPIRATION = Setting.timeSetting("xpack.security.authc.token.timeout", - TimeValue.timeValueMinutes(20L), TimeValue.timeValueSeconds(1L), TimeValue.timeValueHours(1L), Property.NodeScope); - public static final Setting DELETE_INTERVAL = Setting.timeSetting("xpack.security.authc.token.delete.interval", - TimeValue.timeValueMinutes(30L), Property.NodeScope); - public static final Setting DELETE_TIMEOUT = Setting.timeSetting("xpack.security.authc.token.delete.timeout", - TimeValue.MINUS_ONE, Property.NodeScope); + public static final Setting TOKEN_EXPIRATION = Setting.timeSetting( + "xpack.security.authc.token.timeout", + TimeValue.timeValueMinutes(20L), + TimeValue.timeValueSeconds(1L), + TimeValue.timeValueHours(1L), + Property.NodeScope + ); + public static final Setting DELETE_INTERVAL = Setting.timeSetting( + "xpack.security.authc.token.delete.interval", + TimeValue.timeValueMinutes(30L), + Property.NodeScope + ); + public static final Setting DELETE_TIMEOUT = Setting.timeSetting( + "xpack.security.authc.token.delete.timeout", + TimeValue.MINUS_ONE, + Property.NodeScope + ); static final String TOKEN_DOC_TYPE = "token"; private static final int HASHED_TOKEN_LENGTH = 43; @@ -224,9 +238,16 @@ public final class TokenService { /** * Creates a new token service */ - public TokenService(Settings settings, Clock clock, Client client, XPackLicenseState licenseState, SecurityContext securityContext, - SecurityIndexManager securityMainIndex, SecurityIndexManager securityTokensIndex, - ClusterService clusterService) throws GeneralSecurityException { + public TokenService( + Settings settings, + Clock clock, + Client client, + XPackLicenseState licenseState, + SecurityContext securityContext, + SecurityIndexManager securityMainIndex, + SecurityIndexManager securityTokensIndex, + ClusterService clusterService + ) throws GeneralSecurityException { byte[] saltArr = new byte[SALT_BYTES]; secureRandom.nextBytes(saltArr); final SecureString tokenPassphrase = generateTokenKey(); @@ -243,8 +264,10 @@ public TokenService(Settings settings, Clock clock, Client client, XPackLicenseS this.enabled = isTokenServiceEnabled(settings); this.expiredTokenRemover = new ExpiredTokenRemover(settings, client, this.securityMainIndex, securityTokensIndex); ensureEncryptionCiphersSupported(); - KeyAndCache keyAndCache = new KeyAndCache(new KeyAndTimestamp(tokenPassphrase, createdTimeStamps.incrementAndGet()), - new BytesKey(saltArr)); + KeyAndCache keyAndCache = new KeyAndCache( + new KeyAndTimestamp(tokenPassphrase, createdTimeStamps.incrementAndGet()), + new BytesKey(saltArr) + ); keyCache = new TokenKeys(Collections.singletonMap(keyAndCache.getKeyHash(), keyAndCache), keyAndCache.getKeyHash()); this.clusterService = clusterService; initialize(clusterService); @@ -256,8 +279,13 @@ public TokenService(Settings settings, Clock clock, Client client, XPackLicenseS * auto-generated values. The created tokens are stored in the security index for versions up to * {@link #VERSION_TOKENS_INDEX_INTRODUCED} and to a specific security tokens index for later versions. */ - public void createOAuth2Tokens(Authentication authentication, Authentication originatingClientAuth, Map metadata, - boolean includeRefreshToken, ActionListener listener) { + public void createOAuth2Tokens( + Authentication authentication, + Authentication originatingClientAuth, + Map metadata, + boolean includeRefreshToken, + ActionListener listener + ) { // the created token is compatible with the oldest node version in the cluster final Version tokenVersion = getTokenVersionCompatibility(); // tokens moved to a separate index in newer versions @@ -273,10 +301,15 @@ public void createOAuth2Tokens(Authentication authentication, Authentication ori * metadata. The created tokens are stored in the security index for versions up to {@link #VERSION_TOKENS_INDEX_INTRODUCED} and to a * specific security tokens index for later versions. */ - //public for testing - public void createOAuth2Tokens(String accessToken, String refreshToken, Authentication authentication, - Authentication originatingClientAuth, - Map metadata, ActionListener listener) { + // public for testing + public void createOAuth2Tokens( + String accessToken, + String refreshToken, + Authentication authentication, + Authentication originatingClientAuth, + Map metadata, + ActionListener listener + ) { // the created token is compatible with the oldest node version in the cluster final Version tokenVersion = getTokenVersionCompatibility(); // tokens moved to a separate index in newer versions @@ -313,20 +346,35 @@ public void createOAuth2Tokens(String accessToken, String refreshToken, Authenti * serialized access token, serialized refresh token and authentication for which the token is created * as these will be returned to the client */ - private void createOAuth2Tokens(String accessToken, String refreshToken, Version tokenVersion, SecurityIndexManager tokensIndex, - Authentication authentication, Authentication originatingClientAuth, Map metadata, - ActionListener listener) { - assert accessToken.length() == TOKEN_LENGTH : "We assume token ids have a fixed length for nodes of a certain version." - + " When changing the token length, be careful that the inferences about its length still hold."; + private void createOAuth2Tokens( + String accessToken, + String refreshToken, + Version tokenVersion, + SecurityIndexManager tokensIndex, + Authentication authentication, + Authentication originatingClientAuth, + Map metadata, + ActionListener listener + ) { + assert accessToken.length() == TOKEN_LENGTH + : "We assume token ids have a fixed length for nodes of a certain version." + + " When changing the token length, be careful that the inferences about its length still hold."; ensureEnabled(); if (authentication == null) { listener.onFailure(traceLog("create token", new IllegalArgumentException("authentication must be provided"))); } else if (originatingClientAuth == null) { - listener.onFailure(traceLog("create token", - new IllegalArgumentException("originating client authentication must be provided"))); + listener.onFailure( + traceLog("create token", new IllegalArgumentException("originating client authentication must be provided")) + ); } else { - final Authentication tokenAuth = new Authentication(authentication.getUser(), authentication.getAuthenticatedBy(), - authentication.getLookedUpBy(), tokenVersion, AuthenticationType.TOKEN, authentication.getMetadata()); + final Authentication tokenAuth = new Authentication( + authentication.getUser(), + authentication.getAuthenticatedBy(), + authentication.getLookedUpBy(), + tokenVersion, + AuthenticationType.TOKEN, + authentication.getMetadata() + ); final String storedAccessToken; final String storedRefreshToken; if (tokenVersion.onOrAfter(VERSION_HASHED_TOKENS)) { @@ -340,34 +388,41 @@ private void createOAuth2Tokens(String accessToken, String refreshToken, Version final BytesReference tokenDocument = createTokenDocument(userToken, storedRefreshToken, originatingClientAuth); final String documentId = getTokenDocumentId(storedAccessToken); - final IndexRequest indexTokenRequest = client.prepareIndex(tokensIndex.aliasName()).setId(documentId) - .setOpType(OpType.CREATE) - .setSource(tokenDocument, XContentType.JSON) - .setRefreshPolicy(RefreshPolicy.WAIT_UNTIL) - .request(); + final IndexRequest indexTokenRequest = client.prepareIndex(tokensIndex.aliasName()) + .setId(documentId) + .setOpType(OpType.CREATE) + .setSource(tokenDocument, XContentType.JSON) + .setRefreshPolicy(RefreshPolicy.WAIT_UNTIL) + .request(); tokensIndex.prepareIndexIfNeededThenExecute( - ex -> listener.onFailure(traceLog("prepare tokens index [" + tokensIndex.aliasName() + "]", documentId, ex)), - () -> executeAsyncWithOrigin(client, SECURITY_ORIGIN, IndexAction.INSTANCE, indexTokenRequest, - ActionListener.wrap(indexResponse -> { - if (indexResponse.getResult() == Result.CREATED) { - final String versionedAccessToken = prependVersionAndEncodeAccessToken(tokenVersion, accessToken); - if (tokenVersion.onOrAfter(VERSION_TOKENS_INDEX_INTRODUCED)) { - final String versionedRefreshToken = refreshToken != null - ? prependVersionAndEncodeRefreshToken(tokenVersion, refreshToken) - : null; - listener.onResponse(new CreateTokenResult(versionedAccessToken, versionedRefreshToken, - authentication)); - } else { - // prior versions of the refresh token are not version-prepended, as nodes on those - // versions don't expect it. - // Such nodes might exist in a mixed cluster during a rolling upgrade. - listener.onResponse(new CreateTokenResult(versionedAccessToken, refreshToken,authentication)); - } - } else { - listener.onFailure(traceLog("create token", - new ElasticsearchException("failed to create token document [{}]", indexResponse))); - } - }, listener::onFailure))); + ex -> listener.onFailure(traceLog("prepare tokens index [" + tokensIndex.aliasName() + "]", documentId, ex)), + () -> executeAsyncWithOrigin( + client, + SECURITY_ORIGIN, + IndexAction.INSTANCE, + indexTokenRequest, + ActionListener.wrap(indexResponse -> { + if (indexResponse.getResult() == Result.CREATED) { + final String versionedAccessToken = prependVersionAndEncodeAccessToken(tokenVersion, accessToken); + if (tokenVersion.onOrAfter(VERSION_TOKENS_INDEX_INTRODUCED)) { + final String versionedRefreshToken = refreshToken != null + ? prependVersionAndEncodeRefreshToken(tokenVersion, refreshToken) + : null; + listener.onResponse(new CreateTokenResult(versionedAccessToken, versionedRefreshToken, authentication)); + } else { + // prior versions of the refresh token are not version-prepended, as nodes on those + // versions don't expect it. + // Such nodes might exist in a mixed cluster during a rolling upgrade. + listener.onResponse(new CreateTokenResult(versionedAccessToken, refreshToken, authentication)); + } + } else { + listener.onFailure( + traceLog("create token", new ElasticsearchException("failed to create token document [{}]", indexResponse)) + ); + } + }, listener::onFailure) + ) + ); } } @@ -416,16 +471,13 @@ public void authenticateToken(SecureString tokenString, ActionListener>> listener) { - decodeToken(token, ActionListener.wrap( - userToken -> { - if (userToken == null) { - listener.onFailure(new ElasticsearchSecurityException("supplied token is not valid")); - } else { - listener.onResponse(new Tuple<>(userToken.getAuthentication(), userToken.getMetadata())); - } - }, - listener::onFailure - )); + decodeToken(token, ActionListener.wrap(userToken -> { + if (userToken == null) { + listener.onFailure(new ElasticsearchSecurityException("supplied token is not valid")); + } else { + listener.onResponse(new Tuple<>(userToken.getAuthentication(), userToken.getMetadata())); + } + }, listener::onFailure)); } /** @@ -439,47 +491,50 @@ private void getUserTokenFromId(String userTokenId, Version tokenVersion, Action logger.warn("failed to get access token [{}] because index [{}] is not available", userTokenId, tokensIndex.aliasName()); listener.onFailure(frozenTokensIndex.getUnavailableReason()); } else { - final GetRequest getRequest = client.prepareGet(tokensIndex.aliasName(), - getTokenDocumentId(userTokenId)).request(); + final GetRequest getRequest = client.prepareGet(tokensIndex.aliasName(), getTokenDocumentId(userTokenId)).request(); final Consumer onFailure = ex -> listener.onFailure(traceLog("get token from id", userTokenId, ex)); tokensIndex.checkIndexVersionThenExecute( ex -> listener.onFailure(traceLog("prepare tokens index [" + tokensIndex.aliasName() + "]", userTokenId, ex)), - () -> executeAsyncWithOrigin(client.threadPool().getThreadContext(), SECURITY_ORIGIN, getRequest, + () -> executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + SECURITY_ORIGIN, + getRequest, ActionListener.wrap(response -> { - if (response.isExists()) { - @SuppressWarnings("unchecked") - Map accessTokenSource = - (Map) response.getSource().get("access_token"); - if (accessTokenSource == null) { - onFailure.accept(new IllegalStateException( - "token document is missing the access_token field")); - } else if (accessTokenSource.containsKey("user_token") == false) { - onFailure.accept(new IllegalStateException( - "token document is missing the user_token field")); - } else { - @SuppressWarnings("unchecked") - Map userTokenSource = - (Map) accessTokenSource.get("user_token"); - listener.onResponse(UserToken.fromSourceMap(userTokenSource)); - } - } else { - // The chances of a random token string decoding to something that we can read is minimal, so - // we assume that this was a token we have created but is now expired/revoked and deleted - logger.trace("The access token [{}] is expired and already deleted", userTokenId); - listener.onResponse(null); - } - }, e -> { - // if the index or the shard is not there / available we assume that - // the token is not valid - if (isShardNotAvailableException(e)) { - logger.warn("failed to get access token [{}] because index [{}] is not available", userTokenId, - tokensIndex.aliasName()); + if (response.isExists()) { + @SuppressWarnings("unchecked") + Map accessTokenSource = (Map) response.getSource().get("access_token"); + if (accessTokenSource == null) { + onFailure.accept(new IllegalStateException("token document is missing the access_token field")); + } else if (accessTokenSource.containsKey("user_token") == false) { + onFailure.accept(new IllegalStateException("token document is missing the user_token field")); } else { - logger.error(new ParameterizedMessage("failed to get access token [{}]", userTokenId), e); + @SuppressWarnings("unchecked") + Map userTokenSource = (Map) accessTokenSource.get("user_token"); + listener.onResponse(UserToken.fromSourceMap(userTokenSource)); } - listener.onFailure(e); - }), client::get) - ); + } else { + // The chances of a random token string decoding to something that we can read is minimal, so + // we assume that this was a token we have created but is now expired/revoked and deleted + logger.trace("The access token [{}] is expired and already deleted", userTokenId); + listener.onResponse(null); + } + }, e -> { + // if the index or the shard is not there / available we assume that + // the token is not valid + if (isShardNotAvailableException(e)) { + logger.warn( + "failed to get access token [{}] because index [{}] is not available", + userTokenId, + tokensIndex.aliasName() + ); + } else { + logger.error(new ParameterizedMessage("failed to get access token [{}]", userTokenId), e); + } + listener.onFailure(e); + }), + client::get + ) + ); } } @@ -568,7 +623,7 @@ void decodeToken(String token, ActionListener listener) { } catch (Exception e) { // could happen with a token that is not ours if (logger.isDebugEnabled()) { - logger.debug("built in token service unable to decode token", e); + logger.debug("built in token service unable to decode token", e); } else { logger.warn("built in token service unable to decode token"); } @@ -637,31 +692,31 @@ public void invalidateRefreshToken(String refreshToken, ActionListener backoff = DEFAULT_BACKOFF.iterator(); - findTokenFromRefreshToken(refreshToken, - backoff, ActionListener.wrap(searchHits -> { - if (searchHits.getHits().length < 1) { - logger.debug("could not find token document for refresh token"); - listener.onResponse(TokensInvalidationResult.emptyResult(RestStatus.NOT_FOUND)); - } else if (searchHits.getHits().length > 1) { - listener.onFailure(new IllegalStateException("multiple tokens share the same refresh token")); - } else { - final Tuple parsedTokens = - parseTokenAndRefreshStatus(searchHits.getAt(0).getSourceAsMap()); - final UserToken userToken = parsedTokens.v1(); - final RefreshTokenStatus refresh = parsedTokens.v2(); - if (refresh.isInvalidated()) { - listener.onResponse(new TokensInvalidationResult(List.of(), List.of(userToken.getId()), null, RestStatus.OK)); - } else { - indexInvalidation(Collections.singletonList(userToken), backoff, "refresh_token", null, listener); - } - } - }, e -> { - if (e instanceof IndexNotFoundException || e instanceof IndexClosedException) { - listener.onFailure(new ElasticsearchSecurityException("failed to invalidate token", RestStatus.BAD_REQUEST)); + findTokenFromRefreshToken(refreshToken, backoff, ActionListener.wrap(searchHits -> { + if (searchHits.getHits().length < 1) { + logger.debug("could not find token document for refresh token"); + listener.onResponse(TokensInvalidationResult.emptyResult(RestStatus.NOT_FOUND)); + } else if (searchHits.getHits().length > 1) { + listener.onFailure(new IllegalStateException("multiple tokens share the same refresh token")); + } else { + final Tuple parsedTokens = parseTokenAndRefreshStatus( + searchHits.getAt(0).getSourceAsMap() + ); + final UserToken userToken = parsedTokens.v1(); + final RefreshTokenStatus refresh = parsedTokens.v2(); + if (refresh.isInvalidated()) { + listener.onResponse(new TokensInvalidationResult(List.of(), List.of(userToken.getId()), null, RestStatus.OK)); } else { - listener.onFailure(unableToPerformAction(e)); + indexInvalidation(Collections.singletonList(userToken), backoff, "refresh_token", null, listener); } - })); + } + }, e -> { + if (e instanceof IndexNotFoundException || e instanceof IndexClosedException) { + listener.onFailure(new ElasticsearchSecurityException("failed to invalidate token", RestStatus.BAD_REQUEST)); + } else { + listener.onFailure(unableToPerformAction(e)); + } + })); } } @@ -673,8 +728,11 @@ public void invalidateRefreshToken(String refreshToken, ActionListener listener) { + public void invalidateActiveTokensForRealmAndUser( + @Nullable String realmName, + @Nullable String username, + ActionListener listener + ) { ensureEnabled(); if (Strings.isNullOrEmpty(realmName) && Strings.isNullOrEmpty(username)) { logger.trace("No realm name or username provided"); @@ -718,16 +776,25 @@ private void invalidateAllTokens(Collection userTokens, ActionListene // Invalidate the refresh tokens first so that they cannot be used to get new // access tokens while we invalidate the access tokens we currently know about final Iterator backoff = DEFAULT_BACKOFF.iterator(); - indexInvalidation(userTokens, backoff, "refresh_token", null, ActionListener.wrap(result -> - indexInvalidation(userTokens, backoff, "access_token", result, listener), - listener::onFailure)); + indexInvalidation( + userTokens, + backoff, + "refresh_token", + null, + ActionListener.wrap(result -> indexInvalidation(userTokens, backoff, "access_token", result, listener), listener::onFailure) + ); } /** * Invalidates access and/or refresh tokens associated to a user token (coexisting in the same token document) */ - private void indexInvalidation(Collection userTokens, Iterator backoff, String srcPrefix, - @Nullable TokensInvalidationResult previousResult, ActionListener listener) { + private void indexInvalidation( + Collection userTokens, + Iterator backoff, + String srcPrefix, + @Nullable TokensInvalidationResult previousResult, + ActionListener listener + ) { final Set idsOfRecentTokens = new HashSet<>(); final Set idsOfOlderTokens = new HashSet<>(); for (UserToken userToken : userTokens) { @@ -765,26 +832,33 @@ private void indexInvalidation(Collection userTokens, Iterator tokenIds, SecurityIndexManager tokensIndexManager, Iterator backoff, - String srcPrefix, @Nullable TokensInvalidationResult previousResult, - ActionListener listener) { + private void indexInvalidation( + Collection tokenIds, + SecurityIndexManager tokensIndexManager, + Iterator backoff, + String srcPrefix, + @Nullable TokensInvalidationResult previousResult, + ActionListener listener + ) { if (tokenIds.isEmpty()) { logger.warn("No [{}] tokens provided for invalidation", srcPrefix); listener.onFailure(invalidGrantException("No tokens provided for invalidation")); } else { BulkRequestBuilder bulkRequestBuilder = client.prepareBulk(); for (String tokenId : tokenIds) { - UpdateRequest request = client - .prepareUpdate(tokensIndexManager.aliasName(), getTokenDocumentId(tokenId)) - .setDoc(srcPrefix, Collections.singletonMap("invalidated", true)) - .setFetchSource(srcPrefix, null) - .request(); + UpdateRequest request = client.prepareUpdate(tokensIndexManager.aliasName(), getTokenDocumentId(tokenId)) + .setDoc(srcPrefix, Collections.singletonMap("invalidated", true)) + .setFetchSource(srcPrefix, null) + .request(); bulkRequestBuilder.add(request); } bulkRequestBuilder.setRefreshPolicy(RefreshPolicy.WAIT_UNTIL); tokensIndexManager.prepareIndexIfNeededThenExecute( ex -> listener.onFailure(traceLog("prepare index [" + tokensIndexManager.aliasName() + "]", ex)), - () -> executeAsyncWithOrigin(client.threadPool().getThreadContext(), SECURITY_ORIGIN, bulkRequestBuilder.request(), + () -> executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + SECURITY_ORIGIN, + bulkRequestBuilder.request(), ActionListener.wrap(bulkResponse -> { ArrayList retryTokenDocIds = new ArrayList<>(); ArrayList failedRequestResponses = new ArrayList<>(); @@ -808,8 +882,13 @@ private void indexInvalidation(Collection tokenIds, SecurityIndexManager } else { UpdateResponse updateResponse = bulkItemResponse.getResponse(); if (updateResponse.getResult() == DocWriteResponse.Result.UPDATED) { - logger.debug(() -> new ParameterizedMessage("Invalidated [{}] for doc [{}]", - srcPrefix, updateResponse.getGetResult().getId())); + logger.debug( + () -> new ParameterizedMessage( + "Invalidated [{}] for doc [{}]", + srcPrefix, + updateResponse.getGetResult().getId() + ) + ); invalidated.add(updateResponse.getGetResult().getId()); } else if (updateResponse.getResult() == DocWriteResponse.Result.NOOP) { previouslyInvalidated.add(updateResponse.getGetResult().getId()); @@ -817,24 +896,53 @@ private void indexInvalidation(Collection tokenIds, SecurityIndexManager } } if (retryTokenDocIds.isEmpty() == false && backoff.hasNext()) { - logger.debug("failed to invalidate [{}] tokens out of [{}], retrying to invalidate these too", - retryTokenDocIds.size(), tokenIds.size()); - final TokensInvalidationResult incompleteResult = new TokensInvalidationResult(invalidated, - previouslyInvalidated, failedRequestResponses, RestStatus.OK); - client.threadPool().schedule(() -> indexInvalidation(retryTokenDocIds, tokensIndexManager, backoff, - srcPrefix, incompleteResult, listener), backoff.next(), GENERIC); + logger.debug( + "failed to invalidate [{}] tokens out of [{}], retrying to invalidate these too", + retryTokenDocIds.size(), + tokenIds.size() + ); + final TokensInvalidationResult incompleteResult = new TokensInvalidationResult( + invalidated, + previouslyInvalidated, + failedRequestResponses, + RestStatus.OK + ); + client.threadPool() + .schedule( + () -> indexInvalidation( + retryTokenDocIds, + tokensIndexManager, + backoff, + srcPrefix, + incompleteResult, + listener + ), + backoff.next(), + GENERIC + ); } else { if (retryTokenDocIds.isEmpty() == false) { - logger.warn("failed to invalidate [{}] tokens out of [{}] after all retries", retryTokenDocIds.size(), - tokenIds.size()); + logger.warn( + "failed to invalidate [{}] tokens out of [{}] after all retries", + retryTokenDocIds.size(), + tokenIds.size() + ); for (String retryTokenDocId : retryTokenDocIds) { failedRequestResponses.add( - new ElasticsearchException("Error invalidating [{}] with doc id [{}] after retries exhausted", - srcPrefix, retryTokenDocId)); + new ElasticsearchException( + "Error invalidating [{}] with doc id [{}] after retries exhausted", + srcPrefix, + retryTokenDocId + ) + ); } } - final TokensInvalidationResult result = new TokensInvalidationResult(invalidated, previouslyInvalidated, - failedRequestResponses, RestStatus.OK); + final TokensInvalidationResult result = new TokensInvalidationResult( + invalidated, + previouslyInvalidated, + failedRequestResponses, + RestStatus.OK + ); listener.onResponse(result); } }, e -> { @@ -842,12 +950,19 @@ private void indexInvalidation(Collection tokenIds, SecurityIndexManager traceLog("invalidate tokens", cause); if (isShardNotAvailableException(cause) && backoff.hasNext()) { logger.debug("failed to invalidate tokens, retrying "); - client.threadPool().schedule(() -> indexInvalidation(tokenIds, tokensIndexManager, backoff, srcPrefix, - previousResult, listener), backoff.next(), GENERIC); + client.threadPool() + .schedule( + () -> indexInvalidation(tokenIds, tokensIndexManager, backoff, srcPrefix, previousResult, listener), + backoff.next(), + GENERIC + ); } else { listener.onFailure(e); } - }), client::bulk)); + }), + client::bulk + ) + ); } } @@ -864,22 +979,28 @@ public void refreshToken(String refreshToken, ActionListener final Instant refreshRequested = clock.instant(); final Iterator backoff = DEFAULT_BACKOFF.iterator(); final Consumer onFailure = ex -> listener.onFailure(traceLog("find token by refresh token", refreshToken, ex)); - findTokenFromRefreshToken(refreshToken, - backoff, - ActionListener.wrap(searchHits -> { - if (searchHits.getHits().length < 1) { - logger.warn("could not find token document for refresh token"); - onFailure.accept(invalidGrantException("could not refresh the requested token")); - } else if (searchHits.getHits().length > 1) { - onFailure.accept(new IllegalStateException("multiple tokens share the same refresh token")); - } else { - final SearchHit tokenDocHit = searchHits.getAt(0); - final Authentication clientAuth = securityContext.getAuthentication(); - innerRefresh(refreshToken, tokenDocHit.getId(), tokenDocHit.getSourceAsMap(), tokenDocHit.getSeqNo(), - tokenDocHit.getPrimaryTerm(), - clientAuth, backoff, refreshRequested, listener); - } - }, e -> listener.onFailure(invalidGrantException("could not refresh the requested token")))); + findTokenFromRefreshToken(refreshToken, backoff, ActionListener.wrap(searchHits -> { + if (searchHits.getHits().length < 1) { + logger.warn("could not find token document for refresh token"); + onFailure.accept(invalidGrantException("could not refresh the requested token")); + } else if (searchHits.getHits().length > 1) { + onFailure.accept(new IllegalStateException("multiple tokens share the same refresh token")); + } else { + final SearchHit tokenDocHit = searchHits.getAt(0); + final Authentication clientAuth = securityContext.getAuthentication(); + innerRefresh( + refreshToken, + tokenDocHit.getId(), + tokenDocHit.getSourceAsMap(), + tokenDocHit.getSeqNo(), + tokenDocHit.getPrimaryTerm(), + clientAuth, + backoff, + refreshRequested, + listener + ); + } + }, e -> listener.onFailure(invalidGrantException("could not refresh the requested token")))); } /** @@ -889,8 +1010,11 @@ public void refreshToken(String refreshToken, ActionListener private void findTokenFromRefreshToken(String refreshToken, Iterator backoff, ActionListener listener) { if (refreshToken.length() == TOKEN_LENGTH) { // first check if token has the old format before the new version-prepended one - logger.debug("Assuming an unversioned refresh token [{}], generated for node versions" - + " prior to the introduction of the version-header format.", refreshToken); + logger.debug( + "Assuming an unversioned refresh token [{}], generated for node versions" + + " prior to the introduction of the version-header format.", + refreshToken + ); findTokenFromRefreshToken(refreshToken, securityMainIndex, backoff, listener); } else { if (refreshToken.length() == HASHED_TOKEN_LENGTH) { @@ -911,8 +1035,7 @@ private void findTokenFromRefreshToken(String refreshToken, Iterator return; } if (refreshTokenVersion.before(VERSION_TOKENS_INDEX_INTRODUCED) || unencodedRefreshToken.length() != TOKEN_LENGTH) { - logger.debug("Decoded refresh token [{}] with version [{}] is invalid.", unencodedRefreshToken, - refreshTokenVersion); + logger.debug("Decoded refresh token [{}] with version [{}] is invalid.", unencodedRefreshToken, refreshTokenVersion); listener.onResponse(SearchHits.empty()); } else { // TODO Remove this conditional after backporting to 7.x @@ -932,15 +1055,23 @@ private void findTokenFromRefreshToken(String refreshToken, Iterator * with the resulting {@link SearchResponse}. In case of recoverable errors the {@code SearchRequest} is retried using an exponential * backoff policy. This method requires the tokens index where the token document, pointed to by the refresh token, resides. */ - private void findTokenFromRefreshToken(String refreshToken, SecurityIndexManager tokensIndexManager, Iterator backoff, - ActionListener listener) { + private void findTokenFromRefreshToken( + String refreshToken, + SecurityIndexManager tokensIndexManager, + Iterator backoff, + ActionListener listener + ) { final Consumer onFailure = ex -> listener.onFailure(traceLog("find token by refresh token", refreshToken, ex)); final Consumer maybeRetryOnFailure = ex -> { if (backoff.hasNext()) { final TimeValue backofTimeValue = backoff.next(); logger.debug("retrying after [{}] back off", backofTimeValue); - client.threadPool().schedule(() -> findTokenFromRefreshToken(refreshToken, tokensIndexManager, backoff, listener), - backofTimeValue, GENERIC); + client.threadPool() + .schedule( + () -> findTokenFromRefreshToken(refreshToken, tokensIndexManager, backoff, listener), + backofTimeValue, + GENERIC + ); } else { logger.warn("failed to find token from refresh token after all retries"); onFailure.accept(ex); @@ -955,13 +1086,19 @@ private void findTokenFromRefreshToken(String refreshToken, SecurityIndexManager maybeRetryOnFailure.accept(frozenTokensIndex.getUnavailableReason()); } else { final SearchRequest request = client.prepareSearch(tokensIndexManager.aliasName()) - .setQuery(QueryBuilders.boolQuery() - .filter(QueryBuilders.termQuery("doc_type", TOKEN_DOC_TYPE)) - .filter(QueryBuilders.termQuery("refresh_token.token", refreshToken))) - .seqNoAndPrimaryTerm(true) - .request(); - tokensIndexManager.checkIndexVersionThenExecute(listener::onFailure, () -> - executeAsyncWithOrigin(client.threadPool().getThreadContext(), SECURITY_ORIGIN, request, + .setQuery( + QueryBuilders.boolQuery() + .filter(QueryBuilders.termQuery("doc_type", TOKEN_DOC_TYPE)) + .filter(QueryBuilders.termQuery("refresh_token.token", refreshToken)) + ) + .seqNoAndPrimaryTerm(true) + .request(); + tokensIndexManager.checkIndexVersionThenExecute( + listener::onFailure, + () -> executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + SECURITY_ORIGIN, + request, ActionListener.wrap(searchResponse -> { if (searchResponse.isTimedOut()) { logger.debug("find token from refresh token response timed out, retrying"); @@ -977,7 +1114,9 @@ private void findTokenFromRefreshToken(String refreshToken, SecurityIndexManager onFailure.accept(e); } }), - client::search)); + client::search + ) + ); } } @@ -990,9 +1129,17 @@ private void findTokenFromRefreshToken(String refreshToken, SecurityIndexManager * supersedes this one. The new document that contains the new access token and refresh token is created and finally the new access * token and refresh token are returned to the listener. */ - private void innerRefresh(String refreshToken, String tokenDocId, Map source, long seqNo, long primaryTerm, - Authentication clientAuth, Iterator backoff, Instant refreshRequested, - ActionListener listener) { + private void innerRefresh( + String refreshToken, + String tokenDocId, + Map source, + long seqNo, + long primaryTerm, + Authentication clientAuth, + Iterator backoff, + Instant refreshRequested, + ActionListener listener + ) { logger.debug("Attempting to refresh token stored in token document [{}]", tokenDocId); final Consumer onFailure = ex -> listener.onFailure(traceLog("refresh token", tokenDocId, ex)); final Tuple> checkRefreshResult; @@ -1009,8 +1156,10 @@ private void innerRefresh(String refreshToken, String tokenDocId, Map parsedTokens = parseTokensFromDocument(source, null); Authentication authentication = parsedTokens.v1().getAuthentication(); decryptAndReturnSupersedingTokens(refreshToken, refreshTokenStatus, refreshedTokenIndex, authentication, listener); @@ -1025,8 +1174,13 @@ private void innerRefresh(String refreshToken, String tokenDocId, Map listener.onFailure(traceLog("prepare index [" + refreshedTokenIndex.aliasName() + "]", ex)), - () -> executeAsyncWithOrigin(client.threadPool().getThreadContext(), SECURITY_ORIGIN, updateRequest.request(), + ex -> listener.onFailure(traceLog("prepare index [" + refreshedTokenIndex.aliasName() + "]", ex)), + () -> executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + SECURITY_ORIGIN, + updateRequest.request(), ActionListener.wrap(updateResponse -> { if (updateResponse.getResult() == DocWriteResponse.Result.UPDATED) { - logger.debug(() -> new ParameterizedMessage("updated the original token document to {}", - updateResponse.getGetResult().sourceAsMap())); + logger.debug( + () -> new ParameterizedMessage( + "updated the original token document to {}", + updateResponse.getGetResult().sourceAsMap() + ) + ); final Tuple parsedTokens = parseTokensFromDocument(source, null); final UserToken toRefreshUserToken = parsedTokens.v1(); - createOAuth2Tokens(newAccessTokenString, newRefreshTokenString, newTokenVersion, - getTokensIndexForVersion(newTokenVersion), toRefreshUserToken.getAuthentication(), clientAuth, - toRefreshUserToken.getMetadata(), listener); + createOAuth2Tokens( + newAccessTokenString, + newRefreshTokenString, + newTokenVersion, + getTokensIndexForVersion(newTokenVersion), + toRefreshUserToken.getAuthentication(), + clientAuth, + toRefreshUserToken.getMetadata(), + listener + ); } else if (backoff.hasNext()) { - logger.info("failed to update the original token document [{}], the update result was [{}]. Retrying", - tokenDocId, updateResponse.getResult()); - client.threadPool().schedule(() -> innerRefresh(refreshToken, tokenDocId, source, seqNo, primaryTerm, - clientAuth, backoff, refreshRequested, listener), backoff.next(), GENERIC); + logger.info( + "failed to update the original token document [{}], the update result was [{}]. Retrying", + tokenDocId, + updateResponse.getResult() + ); + client.threadPool() + .schedule( + () -> innerRefresh( + refreshToken, + tokenDocId, + source, + seqNo, + primaryTerm, + clientAuth, + backoff, + refreshRequested, + listener + ), + backoff.next(), + GENERIC + ); } else { - logger.info("failed to update the original token document [{}] after all retries, the update result was [{}]. ", - tokenDocId, updateResponse.getResult()); + logger.info( + "failed to update the original token document [{}] after all retries, the update result was [{}]. ", + tokenDocId, + updateResponse.getResult() + ); listener.onFailure(invalidGrantException("could not refresh the requested token")); } }, e -> { @@ -1075,8 +1262,17 @@ private void innerRefresh(String refreshToken, String tokenDocId, Map getTokenDocAsync(tokenDocId, refreshedTokenIndex, true, this), - backoff.next(), GENERIC); + client.threadPool() + .schedule( + () -> getTokenDocAsync(tokenDocId, refreshedTokenIndex, true, this), + backoff.next(), + GENERIC + ); } else { logger.warn("could not get token document [{}] for refresh after all retries", tokenDocId); onFailure.accept(invalidGrantException("could not refresh the requested token")); @@ -1103,8 +1302,22 @@ public void onFailure(Exception e) { } else if (isShardNotAvailableException(e)) { if (backoff.hasNext()) { logger.debug("failed to update the original token document [{}], retrying", tokenDocId); - client.threadPool().schedule(() -> innerRefresh(refreshToken, tokenDocId, source, seqNo, primaryTerm, - clientAuth, backoff, refreshRequested, listener), backoff.next(), GENERIC); + client.threadPool() + .schedule( + () -> innerRefresh( + refreshToken, + tokenDocId, + source, + seqNo, + primaryTerm, + clientAuth, + backoff, + refreshRequested, + listener + ), + backoff.next(), + GENERIC + ); } else { logger.warn("failed to update the original token document [{}], after all retries", tokenDocId); onFailure.accept(invalidGrantException("could not refresh the requested token")); @@ -1112,7 +1325,10 @@ public void onFailure(Exception e) { } else { onFailure.accept(e); } - }), client::update)); + }), + client::update + ) + ); } } @@ -1130,8 +1346,13 @@ public void onFailure(Exception e) { * serialized access token, serialized refresh token and authentication for which the token is created * as these will be returned to the client */ - void decryptAndReturnSupersedingTokens(String refreshToken, RefreshTokenStatus refreshTokenStatus, SecurityIndexManager tokensIndex, - Authentication authentication, ActionListener listener) { + void decryptAndReturnSupersedingTokens( + String refreshToken, + RefreshTokenStatus refreshTokenStatus, + SecurityIndexManager tokensIndex, + Authentication authentication, + ActionListener listener + ) { final byte[] iv = Base64.getDecoder().decode(refreshTokenStatus.getIv()); final byte[] salt = Base64.getDecoder().decode(refreshTokenStatus.getSalt()); @@ -1147,17 +1368,16 @@ void decryptAndReturnSupersedingTokens(String refreshToken, RefreshTokenStatus r // We expect this to protect against race conditions that manifest within few ms final Iterator backoff = BackoffPolicy.exponentialBackoff(TimeValue.timeValueMillis(10), 8).iterator(); final String tokenDocId = getTokenDocumentId(hashTokenString(decryptedTokens[0])); - final Consumer onFailure = ex -> - listener.onFailure(traceLog("decrypt and get superseding token", tokenDocId, ex)); + final Consumer onFailure = ex -> listener.onFailure( + traceLog("decrypt and get superseding token", tokenDocId, ex) + ); final Consumer> maybeRetryGet = actionListener -> { if (backoff.hasNext()) { logger.info("could not get token document [{}] that should have been created, retrying", tokenDocId); - client.threadPool().schedule( - () -> getTokenDocAsync(tokenDocId, tokensIndex, false, actionListener), - backoff.next(), GENERIC); + client.threadPool() + .schedule(() -> getTokenDocAsync(tokenDocId, tokensIndex, false, actionListener), backoff.next(), GENERIC); } else { - logger.warn("could not get token document [{}] that should have been created after all retries", - tokenDocId); + logger.warn("could not get token document [{}] that should have been created after all retries", tokenDocId); onFailure.accept(invalidGrantException("could not refresh the requested token")); } }; @@ -1166,13 +1386,20 @@ void decryptAndReturnSupersedingTokens(String refreshToken, RefreshTokenStatus r public void onResponse(GetResponse response) { if (response.isExists()) { try { - logger.debug("Found superseding document: index=[{}] id=[{}] primTerm=[{}] seqNo=[{}]", - response.getIndex(), response.getId(), response.getPrimaryTerm(), response.getSeqNo()); + logger.debug( + "Found superseding document: index=[{}] id=[{}] primTerm=[{}] seqNo=[{}]", + response.getIndex(), + response.getId(), + response.getPrimaryTerm(), + response.getSeqNo() + ); listener.onResponse( - new CreateTokenResult(prependVersionAndEncodeAccessToken(refreshTokenStatus.getVersion(), - decryptedTokens[0]), + new CreateTokenResult( + prependVersionAndEncodeAccessToken(refreshTokenStatus.getVersion(), decryptedTokens[0]), prependVersionAndEncodeRefreshToken(refreshTokenStatus.getVersion(), decryptedTokens[1]), - authentication)); + authentication + ) + ); } catch (GeneralSecurityException | IOException e) { logger.warn("Could not format stored superseding token values", e); onFailure.accept(invalidGrantException("could not refresh the requested token")); @@ -1203,21 +1430,29 @@ public void onFailure(Exception e) { * The tokens are concatenated to a string separated with `|` before encryption so that we only perform one encryption operation * and that we only need to store one field */ - String encryptSupersedingTokens(String supersedingAccessToken, String supersedingRefreshToken, - String refreshToken, byte[] iv, byte[] salt) throws GeneralSecurityException { + String encryptSupersedingTokens( + String supersedingAccessToken, + String supersedingRefreshToken, + String refreshToken, + byte[] iv, + byte[] salt + ) throws GeneralSecurityException { Cipher cipher = getEncryptionCipher(iv, refreshToken, salt); final String supersedingTokens = supersedingAccessToken + "|" + supersedingRefreshToken; return Base64.getEncoder().encodeToString(cipher.doFinal(supersedingTokens.getBytes(StandardCharsets.UTF_8))); } - private void getTokenDocAsync(String tokenDocId, SecurityIndexManager tokensIndex, - boolean fetchSource, ActionListener listener) { - final GetRequest getRequest = client.prepareGet(tokensIndex.aliasName(), tokenDocId) - .setFetchSource(fetchSource) - .request(); + private void getTokenDocAsync( + String tokenDocId, + SecurityIndexManager tokensIndex, + boolean fetchSource, + ActionListener listener + ) { + final GetRequest getRequest = client.prepareGet(tokensIndex.aliasName(), tokenDocId).setFetchSource(fetchSource).request(); tokensIndex.checkIndexVersionThenExecute( ex -> listener.onFailure(traceLog("prepare tokens index [" + tokensIndex.aliasName() + "]", tokenDocId, ex)), - () -> executeAsyncWithOrigin(client.threadPool().getThreadContext(), SECURITY_ORIGIN, getRequest, listener, client::get)); + () -> executeAsyncWithOrigin(client.threadPool().getThreadContext(), SECURITY_ORIGIN, getRequest, listener, client::get) + ); } Version getTokenVersionCompatibility() { @@ -1253,14 +1488,18 @@ private static Optional checkTokenDocumentExpire * when and by who a token can be refreshed. */ private static Tuple> checkTokenDocumentForRefresh( - Instant refreshRequested, Authentication clientAuth, Map source) throws IllegalStateException, DateTimeException { + Instant refreshRequested, + Authentication clientAuth, + Map source + ) throws IllegalStateException, DateTimeException { final RefreshTokenStatus refreshTokenStatus = parseTokenAndRefreshStatus(source).v2(); final ElasticsearchSecurityException validationException = checkTokenDocumentExpired(refreshRequested, source).orElseGet(() -> { if (refreshTokenStatus.isInvalidated()) { return invalidGrantException("token has been invalidated"); } else { - return checkClientCanRefresh(refreshTokenStatus, clientAuth) - .orElse(checkMultipleRefreshes(refreshRequested, refreshTokenStatus).orElse(null)); + return checkClientCanRefresh(refreshTokenStatus, clientAuth).orElse( + checkMultipleRefreshes(refreshRequested, refreshTokenStatus).orElse(null) + ); } }); return new Tuple<>(refreshTokenStatus, Optional.ofNullable(validationException)); @@ -1277,16 +1516,24 @@ private static Tuple parseTokenAndRefreshStatus(M * Refresh tokens are bound to be used only by the client that originally created them. This check validates this condition, given the * {@code Authentication} of the client that attempted the refresh operation. */ - private static Optional checkClientCanRefresh(RefreshTokenStatus refreshToken, - Authentication clientAuthentication) { + private static Optional checkClientCanRefresh( + RefreshTokenStatus refreshToken, + Authentication clientAuthentication + ) { if (clientAuthentication.getUser().principal().equals(refreshToken.getAssociatedUser()) == false) { - logger.warn("Token was originally created by [{}] but [{}] attempted to refresh it", refreshToken.getAssociatedUser(), - clientAuthentication.getUser().principal()); + logger.warn( + "Token was originally created by [{}] but [{}] attempted to refresh it", + refreshToken.getAssociatedUser(), + clientAuthentication.getUser().principal() + ); return Optional.of(invalidGrantException("tokens must be refreshed by the creating client")); } else if (clientAuthentication.getAuthenticatedBy().getName().equals(refreshToken.getAssociatedRealm()) == false) { - logger.warn("[{}] created the refresh token while authenticated by [{}] but is now authenticated by [{}]", - refreshToken.getAssociatedUser(), refreshToken.getAssociatedRealm(), - clientAuthentication.getAuthenticatedBy().getName()); + logger.warn( + "[{}] created the refresh token while authenticated by [{}] but is now authenticated by [{}]", + refreshToken.getAssociatedUser(), + refreshToken.getAssociatedRealm(), + clientAuthentication.getAuthenticatedBy().getName() + ); return Optional.of(invalidGrantException("tokens must be refreshed by the creating client")); } else { return Optional.empty(); @@ -1323,16 +1570,19 @@ private static Map getUserTokenSourceMap(Map sou * @return An {@code Optional} containing the exception in case this refresh token cannot be reused, or an empty Optional if * refreshing is allowed. */ - private static Optional checkMultipleRefreshes(Instant refreshRequested, - RefreshTokenStatus refreshTokenStatus) { + private static Optional checkMultipleRefreshes( + Instant refreshRequested, + RefreshTokenStatus refreshTokenStatus + ) { if (refreshTokenStatus.isRefreshed()) { if (refreshTokenStatus.getVersion().onOrAfter(VERSION_MULTIPLE_CONCURRENT_REFRESHES)) { if (refreshRequested.isAfter(refreshTokenStatus.getRefreshInstant().plus(30L, ChronoUnit.SECONDS))) { return Optional.of(invalidGrantException("token has already been refreshed more than 30 seconds in the past")); } if (refreshRequested.isBefore(refreshTokenStatus.getRefreshInstant().minus(30L, ChronoUnit.SECONDS))) { - return Optional - .of(invalidGrantException("token has been refreshed more than 30 seconds in the future, clock skew too great")); + return Optional.of( + invalidGrantException("token has been refreshed more than 30 seconds in the future, clock skew too great") + ); } } else { return Optional.of(invalidGrantException("token has already been refreshed")); @@ -1349,8 +1599,11 @@ private static Optional checkMultipleRefreshes(I * @param filter an optional Predicate to test the source of the found documents against * @param listener The listener to notify upon completion */ - public void findActiveTokensForRealm(String realmName, @Nullable Predicate> filter, - ActionListener>> listener) { + public void findActiveTokensForRealm( + String realmName, + @Nullable Predicate> filter, + ActionListener>> listener + ) { ensureEnabled(); if (Strings.isNullOrEmpty(realmName)) { listener.onFailure(new IllegalArgumentException("realm name is required")); @@ -1362,30 +1615,43 @@ public void findActiveTokensForRealm(String realmName, @Nullable Predicate supplier = client.threadPool().getThreadContext().newRestorableContext(false); try (ThreadContext.StoredContext ignore = client.threadPool().getThreadContext().stashWithOrigin(SECURITY_ORIGIN)) { final SearchRequest request = client.prepareSearch(indicesWithTokens.toArray(new String[0])) - .setScroll(DEFAULT_KEEPALIVE_SETTING.get(settings)) - .setQuery(boolQuery) - .setVersion(false) - .setSize(1000) - .setFetchSource(true) - .request(); - ScrollHelper.fetchAllByEntity(client, request, new ContextPreservingActionListener<>(supplier, listener), - (SearchHit hit) -> filterAndParseHit(hit, filter)); + .setScroll(DEFAULT_KEEPALIVE_SETTING.get(settings)) + .setQuery(boolQuery) + .setVersion(false) + .setSize(1000) + .setFetchSource(true) + .request(); + ScrollHelper.fetchAllByEntity( + client, + request, + new ContextPreservingActionListener<>(supplier, listener), + (SearchHit hit) -> filterAndParseHit(hit, filter) + ); } } }, listener::onFailure)); @@ -1410,29 +1676,42 @@ public void findActiveTokensForUser(String username, ActionListener supplier = client.threadPool().getThreadContext().newRestorableContext(false); try (ThreadContext.StoredContext ignore = client.threadPool().getThreadContext().stashWithOrigin(SECURITY_ORIGIN)) { final SearchRequest request = client.prepareSearch(indicesWithTokens.toArray(new String[0])) - .setScroll(DEFAULT_KEEPALIVE_SETTING.get(settings)) - .setQuery(boolQuery) - .setVersion(false) - .setSize(1000) - .setFetchSource(true) - .request(); - ScrollHelper.fetchAllByEntity(client, request, new ContextPreservingActionListener<>(supplier, listener), - (SearchHit hit) -> filterAndParseHit(hit, isOfUser(username))); + .setScroll(DEFAULT_KEEPALIVE_SETTING.get(settings)) + .setQuery(boolQuery) + .setVersion(false) + .setSize(1000) + .setFetchSource(true) + .request(); + ScrollHelper.fetchAllByEntity( + client, + request, + new ContextPreservingActionListener<>(supplier, listener), + (SearchHit hit) -> filterAndParseHit(hit, isOfUser(username)) + ); } } }, listener::onFailure)); @@ -1458,9 +1737,14 @@ private void sourceIndicesWithTokensAndRun(ActionListener> listener return; } if (false == frozenTokensIndex.isIndexUpToDate()) { - listener.onFailure(new IllegalStateException( - "Index [" + frozenTokensIndex.aliasName() + "] is not on the current version. Features relying on the index" - + " will not be available until the upgrade API is run on the index")); + listener.onFailure( + new IllegalStateException( + "Index [" + + frozenTokensIndex.aliasName() + + "] is not on the current version. Features relying on the index" + + " will not be available until the upgrade API is run on the index" + ) + ); return; } indicesWithTokens.add(frozenTokensIndex.aliasName()); @@ -1468,16 +1752,22 @@ private void sourceIndicesWithTokensAndRun(ActionListener> listener final SecurityIndexManager frozenMainIndex = securityMainIndex.freeze(); if (frozenMainIndex.indexExists()) { // main security index _might_ contain tokens if the tokens index has been created recently - if (false == frozenTokensIndex.indexExists() || frozenTokensIndex.getCreationTime() + if (false == frozenTokensIndex.indexExists() + || frozenTokensIndex.getCreationTime() .isAfter(clock.instant().minus(ExpiredTokenRemover.MAXIMUM_TOKEN_LIFETIME_HOURS, ChronoUnit.HOURS))) { if (false == frozenMainIndex.isAvailable()) { listener.onFailure(frozenMainIndex.getUnavailableReason()); return; } if (false == frozenMainIndex.isIndexUpToDate()) { - listener.onFailure(new IllegalStateException( - "Index [" + frozenMainIndex.aliasName() + "] is not on the current version. Features relying on the index" - + " will not be available until the upgrade API is run on the index")); + listener.onFailure( + new IllegalStateException( + "Index [" + + frozenMainIndex.aliasName() + + "] is not on the current version. Features relying on the index" + + " will not be available until the upgrade API is run on the index" + ) + ); return; } indicesWithTokens.add(frozenMainIndex.aliasName()); @@ -1486,16 +1776,23 @@ private void sourceIndicesWithTokensAndRun(ActionListener> listener listener.onResponse(indicesWithTokens); } - private BytesReference createTokenDocument(UserToken userToken, @Nullable String refreshToken, - @Nullable Authentication originatingClientAuth) { - final Instant creationTime = getCreationTime(userToken.getExpirationTime()); + private BytesReference createTokenDocument( + UserToken userToken, + @Nullable String refreshToken, + @Nullable Authentication originatingClientAuth + ) { + final Instant creationTime = getCreationTime(userToken.getExpirationTime()); return createTokenDocument(userToken, refreshToken, originatingClientAuth, creationTime); } - static BytesReference createTokenDocument(UserToken userToken, String refreshToken, Authentication originatingClientAuth, - Instant creationTime) { - assert refreshToken == null || originatingClientAuth != null : "non-null refresh token " + refreshToken - + " requires non-null client authn " + originatingClientAuth; + static BytesReference createTokenDocument( + UserToken userToken, + String refreshToken, + Authentication originatingClientAuth, + Instant creationTime + ) { + assert refreshToken == null || originatingClientAuth != null + : "non-null refresh token " + refreshToken + " requires non-null client authn " + originatingClientAuth; try (XContentBuilder builder = XContentFactory.jsonBuilder()) { builder.startObject(); builder.field("doc_type", TOKEN_DOC_TYPE); @@ -1506,17 +1803,17 @@ static BytesReference createTokenDocument(UserToken userToken, String refreshTok .field("invalidated", false) .field("refreshed", false) .startObject("client") - .field("type", "unassociated_client") - .field("user", originatingClientAuth.getUser().principal()) - .field("realm", originatingClientAuth.getAuthenticatedBy().getName()) + .field("type", "unassociated_client") + .field("user", originatingClientAuth.getUser().principal()) + .field("realm", originatingClientAuth.getAuthenticatedBy().getName()) .endObject() .endObject(); } builder.startObject("access_token") - .field("invalidated", false) - .field("user_token", userToken) - .field("realm", userToken.getAuthentication().getAuthenticatedBy().getName()) - .endObject(); + .field("invalidated", false) + .field("user_token", userToken) + .field("realm", userToken.getAuthentication().getAuthenticatedBy().getName()) + .endObject(); builder.endObject(); return BytesReference.bytes(builder); } catch (IOException e) { @@ -1540,7 +1837,7 @@ private static Predicate> isOfUser(String username) { } private Tuple filterAndParseHit(SearchHit hit, @Nullable Predicate> filter) - throws IllegalStateException, DateTimeException { + throws IllegalStateException, DateTimeException { final Map source = hit.getSourceAsMap(); if (source == null) { throw new IllegalStateException("token document did not have source but source should have been fetched"); @@ -1557,12 +1854,13 @@ private Tuple filterAndParseHit(SearchHit hit, @Nullable Pred * satisfy it */ private Tuple parseTokensFromDocument(Map source, @Nullable Predicate> filter) - throws IllegalStateException, DateTimeException { + throws IllegalStateException, DateTimeException { @SuppressWarnings("unchecked") final String hashedRefreshToken = (String) ((Map) source.get("refresh_token")).get("token"); @SuppressWarnings("unchecked") - final Map userTokenSource = (Map) - ((Map) source.get("access_token")).get("user_token"); + final Map userTokenSource = (Map) ((Map) source.get("access_token")).get( + "user_token" + ); if (null != filter && filter.test(userTokenSource) == false) { return null; } @@ -1628,11 +1926,13 @@ private void checkIfTokenIsValid(UserToken userToken, ActionListener logger.warn("failed to validate access token because the index [" + tokensIndex.aliasName() + "] doesn't exist"); listener.onResponse(null); } else { - final GetRequest getRequest = client - .prepareGet(tokensIndex.aliasName(), getTokenDocumentId(userToken)).request(); + final GetRequest getRequest = client.prepareGet(tokensIndex.aliasName(), getTokenDocumentId(userToken)).request(); Consumer onFailure = ex -> listener.onFailure(traceLog("check token state", userToken.getId(), ex)); tokensIndex.checkIndexVersionThenExecute(listener::onFailure, () -> { - executeAsyncWithOrigin(client.threadPool().getThreadContext(), SECURITY_ORIGIN, getRequest, + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + SECURITY_ORIGIN, + getRequest, ActionListener.wrap(response -> { if (response.isExists()) { Map source = response.getSource(); @@ -1660,9 +1960,15 @@ private void checkIfTokenIsValid(UserToken userToken, ActionListener response.getIndex(), response.getId(), userToken.getId(), - userToken.getAuthentication().getUser().principal()); - onFailure.accept(traceLog("validate token", userToken.getId(), - new IllegalStateException("token document is missing and must be present"))); + userToken.getAuthentication().getUser().principal() + ); + onFailure.accept( + traceLog( + "validate token", + userToken.getId(), + new IllegalStateException("token document is missing and must be present") + ) + ); } }, e -> { // if the index or the shard is not there / available we assume that @@ -1674,7 +1980,9 @@ private void checkIfTokenIsValid(UserToken userToken, ActionListener logger.error(new ParameterizedMessage("failed to get token [{}]", userToken.getId()), e); listener.onFailure(e); } - }), client::get); + }), + client::get + ); }); } } @@ -1708,9 +2016,11 @@ String prependVersionAndEncodeAccessToken(Version version, String accessToken) t } } else { // we know that the minimum length is larger than the default of the ByteArrayOutputStream so set the size to this explicitly - try (ByteArrayOutputStream os = new ByteArrayOutputStream(LEGACY_MINIMUM_BASE64_BYTES); - OutputStream base64 = Base64.getEncoder().wrap(os); - StreamOutput out = new OutputStreamStreamOutput(base64)) { + try ( + ByteArrayOutputStream os = new ByteArrayOutputStream(LEGACY_MINIMUM_BASE64_BYTES); + OutputStream base64 = Base64.getEncoder().wrap(os); + StreamOutput out = new OutputStreamStreamOutput(base64) + ) { out.setVersion(version); KeyAndCache keyAndCache = keyCache.activeKeyCache; Version.writeVersion(version, out); @@ -1718,9 +2028,13 @@ String prependVersionAndEncodeAccessToken(Version version, String accessToken) t out.writeByteArray(keyAndCache.getKeyHash().bytes); final byte[] initializationVector = getRandomBytes(IV_BYTES); out.writeByteArray(initializationVector); - try (CipherOutputStream encryptedOutput = - new CipherOutputStream(out, getEncryptionCipher(initializationVector, keyAndCache, version)); - StreamOutput encryptedStreamOutput = new OutputStreamStreamOutput(encryptedOutput)) { + try ( + CipherOutputStream encryptedOutput = new CipherOutputStream( + out, + getEncryptionCipher(initializationVector, keyAndCache, version) + ); + StreamOutput encryptedStreamOutput = new OutputStreamStreamOutput(encryptedOutput) + ) { encryptedStreamOutput.setVersion(version); encryptedStreamOutput.writeString(accessToken); // StreamOutput needs to be closed explicitly because it wraps CipherOutputStream @@ -1797,15 +2111,16 @@ private void getKeyAsync(BytesKey decodedSalt, KeyAndCache keyAndCache, ActionLi * request(s) that require a key computation will be delayed and there will be * some additional latency. */ - client.threadPool().executor(THREAD_POOL_NAME) - .submit(new KeyComputingRunnable(decodedSalt, keyAndCache, listener)); + client.threadPool().executor(THREAD_POOL_NAME).submit(new KeyComputingRunnable(decodedSalt, keyAndCache, listener)); } } private static String decryptTokenId(byte[] encryptedTokenId, Cipher cipher, Version version) throws IOException { - try (ByteArrayInputStream bais = new ByteArrayInputStream(encryptedTokenId); - CipherInputStream cis = new CipherInputStream(bais, cipher); - StreamInput decryptedInput = new InputStreamStreamInput(cis)) { + try ( + ByteArrayInputStream bais = new ByteArrayInputStream(encryptedTokenId); + CipherInputStream cis = new CipherInputStream(bais, cipher); + StreamInput decryptedInput = new InputStreamStreamInput(cis) + ) { decryptedInput.setVersion(version); return decryptedInput.readString(); } @@ -1840,8 +2155,8 @@ byte[] getRandomBytes(int length) { * Generates a secret key based off of the provided password and salt. * This method can be computationally expensive. */ - static SecretKey computeSecretKey(char[] rawPassword, byte[] salt, int iterations) - throws NoSuchAlgorithmException, InvalidKeySpecException { + static SecretKey computeSecretKey(char[] rawPassword, byte[] salt, int iterations) throws NoSuchAlgorithmException, + InvalidKeySpecException { SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(KDF_ALGORITHM); PBEKeySpec keySpec = new PBEKeySpec(rawPassword, salt, iterations, 128); SecretKey tmp = secretKeyFactory.generateSecret(keySpec); @@ -1854,8 +2169,7 @@ static SecretKey computeSecretKey(char[] rawPassword, byte[] salt, int iteration * is defined in */ private static ElasticsearchSecurityException expiredTokenException() { - ElasticsearchSecurityException e = - new ElasticsearchSecurityException("token expired", RestStatus.UNAUTHORIZED); + ElasticsearchSecurityException e = new ElasticsearchSecurityException("token expired", RestStatus.UNAUTHORIZED); e.addHeader("WWW-Authenticate", EXPIRED_TOKEN_WWW_AUTH_VALUE); return e; } @@ -1864,8 +2178,7 @@ private static ElasticsearchSecurityException expiredTokenException() { * Creates an {@link ElasticsearchSecurityException} that indicates the request contained an invalid grant */ private static ElasticsearchSecurityException invalidGrantException(String detail) { - ElasticsearchSecurityException e = - new ElasticsearchSecurityException("invalid_grant", RestStatus.BAD_REQUEST); + ElasticsearchSecurityException e = new ElasticsearchSecurityException("invalid_grant", RestStatus.BAD_REQUEST); e.addHeader("error_description", detail); return e; } @@ -1883,11 +2196,9 @@ private E traceLog(String action, String identifier, E exc final ElasticsearchException esEx = (ElasticsearchException) exception; final Object detail = esEx.getHeader("error_description"); if (detail != null) { - logger.trace(() -> new ParameterizedMessage("Failure in [{}] for id [{}] - [{}]", action, identifier, detail), - esEx); + logger.trace(() -> new ParameterizedMessage("Failure in [{}] for id [{}] - [{}]", action, identifier, detail), esEx); } else { - logger.trace(() -> new ParameterizedMessage("Failure in [{}] for id [{}]", action, identifier), - esEx); + logger.trace(() -> new ParameterizedMessage("Failure in [{}] for id [{}]", action, identifier), esEx); } } else { logger.trace(() -> new ParameterizedMessage("Failure in [{}] for id [{}]", action, identifier), exception); @@ -1967,8 +2278,9 @@ protected void doRun() { final SecretKey computedKey = keyAndCache.getOrComputeKey(decodedSalt); listener.onResponse(computedKey); } catch (ExecutionException e) { - if (e.getCause() != null && - (e.getCause() instanceof GeneralSecurityException || e.getCause() instanceof IOException + if (e.getCause() != null + && (e.getCause() instanceof GeneralSecurityException + || e.getCause() instanceof IOException || e.getCause() instanceof IllegalArgumentException)) { // this could happen if another realm supports the Bearer token so we should // see if another realm can use this token! @@ -2004,8 +2316,10 @@ synchronized TokenMetadata generateSpareKey() { if (keyCache.cache.containsKey(keyAndCache.getKeyHash())) { continue; // collision -- generate a new key } - return newTokenMetadata(keyCache.currentTokenKeyHash, Iterables.concat(keyCache.cache.values(), - Collections.singletonList(keyAndCache))); + return newTokenMetadata( + keyCache.currentTokenKeyHash, + Iterables.concat(keyCache.cache.values(), Collections.singletonList(keyAndCache)) + ); } } return newTokenMetadata(keyCache.currentTokenKeyHash, keyCache.cache.values()); @@ -2034,11 +2348,9 @@ synchronized TokenMetadata pruneKeys(int numKeysToKeep) { Map map = new HashMap<>(keyCache.cache.size() + 1); KeyAndCache currentKey = keyCache.get(keyCache.currentTokenKeyHash); ArrayList entries = new ArrayList<>(keyCache.cache.values()); - Collections.sort(entries, - (left, right) -> Long.compare(right.keyAndTimestamp.getTimestamp(), left.keyAndTimestamp.getTimestamp())); + Collections.sort(entries, (left, right) -> Long.compare(right.keyAndTimestamp.getTimestamp(), left.keyAndTimestamp.getTimestamp())); for (KeyAndCache value : entries) { - if (map.size() < numKeysToKeep || value.keyAndTimestamp.getTimestamp() >= currentKey - .keyAndTimestamp.getTimestamp()) { + if (map.size() < numKeysToKeep || value.keyAndTimestamp.getTimestamp() >= currentKey.keyAndTimestamp.getTimestamp()) { logger.debug("keeping key {} ", value.getKeyHash()); map.put(value.getKeyHash(), value); } else { @@ -2116,17 +2428,20 @@ synchronized String getActiveKeyHash() { void rotateKeysOnMaster(ActionListener listener) { logger.info("rotate keys on master"); TokenMetadata tokenMetadata = generateSpareKey(); - clusterService.submitStateUpdateTask("publish next key to prepare key rotation", - new TokenMetadataPublishAction( - tokenMetadata, ActionListener.wrap((res) -> { - if (res.isAcknowledged()) { - TokenMetadata metadata = rotateToSpareKey(); - clusterService.submitStateUpdateTask("publish next key to prepare key rotation", - new TokenMetadataPublishAction(metadata, listener)); - } else { - listener.onFailure(new IllegalStateException("not acked")); - } - }, listener::onFailure))); + clusterService.submitStateUpdateTask( + "publish next key to prepare key rotation", + new TokenMetadataPublishAction(tokenMetadata, ActionListener.wrap((res) -> { + if (res.isAcknowledged()) { + TokenMetadata metadata = rotateToSpareKey(); + clusterService.submitStateUpdateTask( + "publish next key to prepare key rotation", + new TokenMetadataPublishAction(metadata, listener) + ); + } else { + listener.onFailure(new IllegalStateException("not acked")); + } + }, listener::onFailure)) + ); } private static final class TokenMetadataPublishAction extends AckedClusterStateUpdateTask { @@ -2170,8 +2485,10 @@ private void initialize(ClusterService clusterService) { if (XPackPlugin.isReadyForXPackCustomMetadata(state)) { installTokenMetadata(state); } else { - logger.debug("cannot add token metadata to cluster as the following nodes might not understand the metadata: {}", - () -> XPackPlugin.nodesNotReadyForXPackCustomMetadata(state)); + logger.debug( + "cannot add token metadata to cluster as the following nodes might not understand the metadata: {}", + () -> XPackPlugin.nodesNotReadyForXPackCustomMetadata(state) + ); } } @@ -2325,7 +2642,8 @@ static final class RefreshTokenStatus { private final String associatedUser; private final String associatedRealm; private final boolean refreshed; - @Nullable private final Instant refreshInstant; + @Nullable + private final Instant refreshInstant; @Nullable private final String supersedingTokens; @Nullable @@ -2335,8 +2653,16 @@ static final class RefreshTokenStatus { private Version version; // pkg-private for testing - RefreshTokenStatus(boolean invalidated, String associatedUser, String associatedRealm, boolean refreshed, Instant refreshInstant, - String supersedingTokens, String iv, String salt) { + RefreshTokenStatus( + boolean invalidated, + String associatedUser, + String associatedRealm, + boolean refreshed, + Instant refreshInstant, + String supersedingTokens, + String iv, + String salt + ) { this.invalidated = invalidated; this.associatedUser = associatedUser; this.associatedRealm = associatedRealm; @@ -2363,7 +2689,8 @@ boolean isRefreshed() { return refreshed; } - @Nullable Instant getRefreshInstant() { + @Nullable + Instant getRefreshInstant() { return refreshInstant; } @@ -2417,8 +2744,16 @@ static RefreshTokenStatus fromSourceMap(Map refreshTokenSource) final String supersedingTokens = (String) refreshTokenSource.get("superseding.encrypted_tokens"); final String iv = (String) refreshTokenSource.get("superseding.encryption_iv"); final String salt = (String) refreshTokenSource.get("superseding.encryption_salt"); - return new RefreshTokenStatus(invalidated, associatedUser, associatedRealm, refreshed, refreshInstant, supersedingTokens, - iv, salt); + return new RefreshTokenStatus( + invalidated, + associatedUser, + associatedRealm, + refreshed, + refreshInstant, + supersedingTokens, + iv, + salt + ); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/esnative/NativeRealm.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/esnative/NativeRealm.java index fff2b86ec6dc6..e75b02c01c8ec 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/esnative/NativeRealm.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/esnative/NativeRealm.java @@ -53,12 +53,10 @@ public void onSecurityIndexStateChange(SecurityIndexManager.State previousState, @Override public void usageStats(ActionListener> listener) { - super.usageStats(ActionListener.wrap(stats -> - userStore.getUserCount(ActionListener.wrap(size -> { - stats.put("size", size); - listener.onResponse(stats); - }, listener::onFailure)) - , listener::onFailure)); + super.usageStats(ActionListener.wrap(stats -> userStore.getUserCount(ActionListener.wrap(size -> { + stats.put("size", size); + listener.onResponse(stats); + }, listener::onFailure)), listener::onFailure)); } // method is used for testing to verify cache expiration since expireAll is final diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/esnative/NativeUsersStore.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/esnative/NativeUsersStore.java index 95c79465af203..1127b7f2bb04d 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/esnative/NativeUsersStore.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/esnative/NativeUsersStore.java @@ -26,18 +26,18 @@ import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.client.Client; import org.elasticsearch.client.Requests; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.ValidationException; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.core.Nullable; import org.elasticsearch.index.IndexNotFoundException; import org.elasticsearch.index.engine.DocumentMissingException; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.security.ScrollHelper; import org.elasticsearch.xpack.core.security.action.realm.ClearRealmCacheAction; import org.elasticsearch.xpack.core.security.action.realm.ClearRealmCacheRequest; @@ -95,9 +95,10 @@ public NativeUsersStore(Settings settings, Client client, SecurityIndexManager s * Blocking version of {@code getUser} that blocks until the User is returned */ public void getUser(String username, ActionListener listener) { - getUserAndPassword(username, ActionListener.wrap((uap) -> { - listener.onResponse(uap == null ? null : uap.user()); - }, listener::onFailure)); + getUserAndPassword( + username, + ActionListener.wrap((uap) -> { listener.onResponse(uap == null ? null : uap.user()); }, listener::onFailure) + ); } /** @@ -125,9 +126,13 @@ public void getUsers(String[] userNames, final ActionListener> listener.onFailure(frozenSecurityIndex.getUnavailableReason()); } else if (userNames.length == 1) { // optimization for single user lookup final String username = userNames[0]; - getUserAndPassword(username, ActionListener.wrap( + getUserAndPassword( + username, + ActionListener.wrap( (uap) -> listener.onResponse(uap == null ? Collections.emptyList() : Collections.singletonList(uap.user())), - handleException)); + handleException + ) + ); } else { securityIndex.checkIndexVersionThenExecute(listener::onFailure, () -> { final QueryBuilder query; @@ -140,11 +145,11 @@ public void getUsers(String[] userNames, final ActionListener> final Supplier supplier = client.threadPool().getThreadContext().newRestorableContext(false); try (ThreadContext.StoredContext ignore = client.threadPool().getThreadContext().stashWithOrigin(SECURITY_ORIGIN)) { SearchRequest request = client.prepareSearch(SECURITY_MAIN_ALIAS) - .setScroll(DEFAULT_KEEPALIVE_SETTING.get(settings)) - .setQuery(query) - .setSize(1000) - .setFetchSource(true) - .request(); + .setScroll(DEFAULT_KEEPALIVE_SETTING.get(settings)) + .setQuery(query) + .setSize(1000) + .setFetchSource(true) + .request(); request.indicesOptions().ignoreUnavailable(); ScrollHelper.fetchAllByEntity(client, request, new ContextPreservingActionListener<>(supplier, listener), (hit) -> { UserAndPassword u = transformUser(hit.getId(), hit.getSourceAsMap()); @@ -162,15 +167,20 @@ void getUserCount(final ActionListener listener) { } else if (frozenSecurityIndex.isAvailable() == false) { listener.onFailure(frozenSecurityIndex.getUnavailableReason()); } else { - securityIndex.checkIndexVersionThenExecute(listener::onFailure, () -> - executeAsyncWithOrigin(client.threadPool().getThreadContext(), SECURITY_ORIGIN, + securityIndex.checkIndexVersionThenExecute( + listener::onFailure, + () -> executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + SECURITY_ORIGIN, client.prepareSearch(SECURITY_MAIN_ALIAS) .setQuery(QueryBuilders.termQuery(Fields.TYPE.getPreferredName(), USER_DOC_TYPE)) .setSize(0) .setTrackTotalHits(true) .request(), - listener.delegateFailure( - (l, response) -> l.onResponse(response.getHits().getTotalHits().value)), client::search)); + listener.delegateFailure((l, response) -> l.onResponse(response.getHits().getTotalHits().value)), + client::search + ) + ); } } @@ -187,38 +197,44 @@ private void getUserAndPassword(final String user, final ActionListener - executeAsyncWithOrigin(client.threadPool().getThreadContext(), SECURITY_ORIGIN, - client.prepareGet(SECURITY_MAIN_ALIAS, getIdForUser(USER_DOC_TYPE, user)).request(), - new ActionListener() { - @Override - public void onResponse(GetResponse response) { - logger.trace( - "user [{}] is doc [{}] in index [{}] with primTerm [{}] and seqNo [{}]", - user, - response.getId(), - response.getIndex(), - response.getPrimaryTerm(), - response.getSeqNo() - ); - listener.onResponse(transformUser(response.getId(), response.getSource())); - } + securityIndex.checkIndexVersionThenExecute( + listener::onFailure, + () -> executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + SECURITY_ORIGIN, + client.prepareGet(SECURITY_MAIN_ALIAS, getIdForUser(USER_DOC_TYPE, user)).request(), + new ActionListener() { + @Override + public void onResponse(GetResponse response) { + logger.trace( + "user [{}] is doc [{}] in index [{}] with primTerm [{}] and seqNo [{}]", + user, + response.getId(), + response.getIndex(), + response.getPrimaryTerm(), + response.getSeqNo() + ); + listener.onResponse(transformUser(response.getId(), response.getSource())); + } - @Override - public void onFailure(Exception t) { - if (t instanceof IndexNotFoundException) { - logger.trace(new ParameterizedMessage( - "could not retrieve user [{}] because security index does not exist", - user), - t); - } else { - logger.error(new ParameterizedMessage("failed to retrieve user [{}]", user), t); - } - // We don't invoke the onFailure listener here, instead - // we call the response with a null user - listener.onResponse(null); - } - }, client::get)); + @Override + public void onFailure(Exception t) { + if (t instanceof IndexNotFoundException) { + logger.trace( + new ParameterizedMessage("could not retrieve user [{}] because security index does not exist", user), + t + ); + } else { + logger.error(new ParameterizedMessage("failed to retrieve user [{}]", user), t); + } + // We don't invoke the onFailure listener here, instead + // we call the response with a null user + listener.onResponse(null); + } + }, + client::get + ) + ); } } @@ -237,42 +253,51 @@ public void changePassword(final ChangePasswordRequest request, final ActionList } securityIndex.prepareIndexIfNeededThenExecute(listener::onFailure, () -> { - executeAsyncWithOrigin(client.threadPool().getThreadContext(), SECURITY_ORIGIN, - client.prepareUpdate(SECURITY_MAIN_ALIAS, getIdForUser(docType, username)) - .setDoc(Requests.INDEX_CONTENT_TYPE, Fields.PASSWORD.getPreferredName(), - String.valueOf(request.passwordHash())) - .setRefreshPolicy(request.getRefreshPolicy()).request(), - new ActionListener() { - @Override - public void onResponse(UpdateResponse updateResponse) { - assert updateResponse.getResult() == DocWriteResponse.Result.UPDATED - || updateResponse.getResult() == DocWriteResponse.Result.NOOP; - clearRealmCache(request.username(), listener, null); - } + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + SECURITY_ORIGIN, + client.prepareUpdate(SECURITY_MAIN_ALIAS, getIdForUser(docType, username)) + .setDoc(Requests.INDEX_CONTENT_TYPE, Fields.PASSWORD.getPreferredName(), String.valueOf(request.passwordHash())) + .setRefreshPolicy(request.getRefreshPolicy()) + .request(), + new ActionListener() { + @Override + public void onResponse(UpdateResponse updateResponse) { + assert updateResponse.getResult() == DocWriteResponse.Result.UPDATED + || updateResponse.getResult() == DocWriteResponse.Result.NOOP; + clearRealmCache(request.username(), listener, null); + } - @Override - public void onFailure(Exception e) { - if (isIndexNotFoundOrDocumentMissing(e)) { - if (docType.equals(RESERVED_USER_TYPE)) { - updateReservedUser( - username, - request.passwordHash(), - DocWriteRequest.OpType.INDEX, - request.getRefreshPolicy(), - listener - ); - } else { - logger.debug((org.apache.logging.log4j.util.Supplier) () -> - new ParameterizedMessage("failed to change password for user [{}]", request.username()), e); - ValidationException validationException = new ValidationException(); - validationException.addValidationError("user must exist in order to change password"); - listener.onFailure(validationException); - } + @Override + public void onFailure(Exception e) { + if (isIndexNotFoundOrDocumentMissing(e)) { + if (docType.equals(RESERVED_USER_TYPE)) { + updateReservedUser( + username, + request.passwordHash(), + DocWriteRequest.OpType.INDEX, + request.getRefreshPolicy(), + listener + ); } else { - listener.onFailure(e); + logger.debug( + (org.apache.logging.log4j.util.Supplier) () -> new ParameterizedMessage( + "failed to change password for user [{}]", + request.username() + ), + e + ); + ValidationException validationException = new ValidationException(); + validationException.addValidationError("user must exist in order to change password"); + listener.onFailure(validationException); } + } else { + listener.onFailure(e); } - }, client::update); + } + }, + client::update + ); }); } @@ -339,64 +364,100 @@ private void updateUserWithoutPassword(final PutUserRequest putUserRequest, fina assert putUserRequest.passwordHash() == null; // We must have an existing document securityIndex.prepareIndexIfNeededThenExecute(listener::onFailure, () -> { - executeAsyncWithOrigin(client.threadPool().getThreadContext(), SECURITY_ORIGIN, - client.prepareUpdate(SECURITY_MAIN_ALIAS, getIdForUser(USER_DOC_TYPE, putUserRequest.username())) - .setDoc(Requests.INDEX_CONTENT_TYPE, - Fields.USERNAME.getPreferredName(), putUserRequest.username(), - Fields.ROLES.getPreferredName(), putUserRequest.roles(), - Fields.FULL_NAME.getPreferredName(), putUserRequest.fullName(), - Fields.EMAIL.getPreferredName(), putUserRequest.email(), - Fields.METADATA.getPreferredName(), putUserRequest.metadata(), - Fields.ENABLED.getPreferredName(), putUserRequest.enabled(), - Fields.TYPE.getPreferredName(), USER_DOC_TYPE) - .setRefreshPolicy(putUserRequest.getRefreshPolicy()) - .request(), - new ActionListener() { - @Override - public void onResponse(UpdateResponse updateResponse) { - assert updateResponse.getResult() == DocWriteResponse.Result.UPDATED - || updateResponse.getResult() == DocWriteResponse.Result.NOOP - : "Expected 'UPDATED' or 'NOOP' result [" + updateResponse + "] for request [" + putUserRequest + "]"; - clearRealmCache(putUserRequest.username(), listener, false); - } + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + SECURITY_ORIGIN, + client.prepareUpdate(SECURITY_MAIN_ALIAS, getIdForUser(USER_DOC_TYPE, putUserRequest.username())) + .setDoc( + Requests.INDEX_CONTENT_TYPE, + Fields.USERNAME.getPreferredName(), + putUserRequest.username(), + Fields.ROLES.getPreferredName(), + putUserRequest.roles(), + Fields.FULL_NAME.getPreferredName(), + putUserRequest.fullName(), + Fields.EMAIL.getPreferredName(), + putUserRequest.email(), + Fields.METADATA.getPreferredName(), + putUserRequest.metadata(), + Fields.ENABLED.getPreferredName(), + putUserRequest.enabled(), + Fields.TYPE.getPreferredName(), + USER_DOC_TYPE + ) + .setRefreshPolicy(putUserRequest.getRefreshPolicy()) + .request(), + new ActionListener() { + @Override + public void onResponse(UpdateResponse updateResponse) { + assert updateResponse.getResult() == DocWriteResponse.Result.UPDATED + || updateResponse.getResult() == DocWriteResponse.Result.NOOP + : "Expected 'UPDATED' or 'NOOP' result [" + updateResponse + "] for request [" + putUserRequest + "]"; + clearRealmCache(putUserRequest.username(), listener, false); + } - @Override - public void onFailure(Exception e) { - Exception failure = e; - if (isIndexNotFoundOrDocumentMissing(e)) { - // if the index doesn't exist we can never update a user - // if the document doesn't exist, then this update is not valid - logger.debug((org.apache.logging.log4j.util.Supplier) - () -> new ParameterizedMessage("failed to update user document with username [{}]", - putUserRequest.username()), e); - ValidationException validationException = new ValidationException(); - validationException - .addValidationError("password must be specified unless you are updating an existing user"); - failure = validationException; - } - listener.onFailure(failure); + @Override + public void onFailure(Exception e) { + Exception failure = e; + if (isIndexNotFoundOrDocumentMissing(e)) { + // if the index doesn't exist we can never update a user + // if the document doesn't exist, then this update is not valid + logger.debug( + (org.apache.logging.log4j.util.Supplier) () -> new ParameterizedMessage( + "failed to update user document with username [{}]", + putUserRequest.username() + ), + e + ); + ValidationException validationException = new ValidationException(); + validationException.addValidationError("password must be specified unless you are updating an existing user"); + failure = validationException; } - }, client::update); + listener.onFailure(failure); + } + }, + client::update + ); }); } private void indexUser(final PutUserRequest putUserRequest, final ActionListener listener) { assert putUserRequest.passwordHash() != null; securityIndex.prepareIndexIfNeededThenExecute(listener::onFailure, () -> { - executeAsyncWithOrigin(client.threadPool().getThreadContext(), SECURITY_ORIGIN, - client.prepareIndex(SECURITY_MAIN_ALIAS).setId(getIdForUser(USER_DOC_TYPE, putUserRequest.username())) - .setSource(Fields.USERNAME.getPreferredName(), putUserRequest.username(), - Fields.PASSWORD.getPreferredName(), String.valueOf(putUserRequest.passwordHash()), - Fields.ROLES.getPreferredName(), putUserRequest.roles(), - Fields.FULL_NAME.getPreferredName(), putUserRequest.fullName(), - Fields.EMAIL.getPreferredName(), putUserRequest.email(), - Fields.METADATA.getPreferredName(), putUserRequest.metadata(), - Fields.ENABLED.getPreferredName(), putUserRequest.enabled(), - Fields.TYPE.getPreferredName(), USER_DOC_TYPE) - .setRefreshPolicy(putUserRequest.getRefreshPolicy()) - .request(), - listener.delegateFailure((l, updateResponse) -> clearRealmCache(putUserRequest.username(), l, - updateResponse.getResult() == DocWriteResponse.Result.CREATED)), client::index); + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + SECURITY_ORIGIN, + client.prepareIndex(SECURITY_MAIN_ALIAS) + .setId(getIdForUser(USER_DOC_TYPE, putUserRequest.username())) + .setSource( + Fields.USERNAME.getPreferredName(), + putUserRequest.username(), + Fields.PASSWORD.getPreferredName(), + String.valueOf(putUserRequest.passwordHash()), + Fields.ROLES.getPreferredName(), + putUserRequest.roles(), + Fields.FULL_NAME.getPreferredName(), + putUserRequest.fullName(), + Fields.EMAIL.getPreferredName(), + putUserRequest.email(), + Fields.METADATA.getPreferredName(), + putUserRequest.metadata(), + Fields.ENABLED.getPreferredName(), + putUserRequest.enabled(), + Fields.TYPE.getPreferredName(), + USER_DOC_TYPE + ) + .setRefreshPolicy(putUserRequest.getRefreshPolicy()) + .request(), + listener.delegateFailure( + (l, updateResponse) -> clearRealmCache( + putUserRequest.username(), + l, + updateResponse.getResult() == DocWriteResponse.Result.CREATED + ) + ), + client::index + ); }); } @@ -404,8 +465,12 @@ private void indexUser(final PutUserRequest putUserRequest, final ActionListener * Asynchronous method that will update the enabled flag of a user. If the user is reserved and the document does not exist, a document * will be created. If the user is not reserved, the user must exist otherwise the operation will fail. */ - public void setEnabled(final String username, final boolean enabled, final RefreshPolicy refreshPolicy, - final ActionListener listener) { + public void setEnabled( + final String username, + final boolean enabled, + final RefreshPolicy refreshPolicy, + final ActionListener listener + ) { if (ClientReservedRealm.isReserved(username, settings)) { setReservedUserEnabled(username, enabled, refreshPolicy, true, listener); } else { @@ -413,59 +478,85 @@ public void setEnabled(final String username, final boolean enabled, final Refre } } - private void setRegularUserEnabled(final String username, final boolean enabled, final RefreshPolicy refreshPolicy, - final ActionListener listener) { + private void setRegularUserEnabled( + final String username, + final boolean enabled, + final RefreshPolicy refreshPolicy, + final ActionListener listener + ) { securityIndex.prepareIndexIfNeededThenExecute(listener::onFailure, () -> { - executeAsyncWithOrigin(client.threadPool().getThreadContext(), SECURITY_ORIGIN, - client.prepareUpdate(SECURITY_MAIN_ALIAS, getIdForUser(USER_DOC_TYPE, username)) - .setDoc(Requests.INDEX_CONTENT_TYPE, Fields.ENABLED.getPreferredName(), enabled) - .setRefreshPolicy(refreshPolicy) - .request(), - new ActionListener() { - @Override - public void onResponse(UpdateResponse updateResponse) { - clearRealmCache(username, listener, null); - } + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + SECURITY_ORIGIN, + client.prepareUpdate(SECURITY_MAIN_ALIAS, getIdForUser(USER_DOC_TYPE, username)) + .setDoc(Requests.INDEX_CONTENT_TYPE, Fields.ENABLED.getPreferredName(), enabled) + .setRefreshPolicy(refreshPolicy) + .request(), + new ActionListener() { + @Override + public void onResponse(UpdateResponse updateResponse) { + clearRealmCache(username, listener, null); + } - @Override - public void onFailure(Exception e) { - Exception failure = e; - if (isIndexNotFoundOrDocumentMissing(e)) { - // if the index doesn't exist we can never update a user - // if the document doesn't exist, then this update is not valid - logger.debug((org.apache.logging.log4j.util.Supplier) - () -> new ParameterizedMessage("failed to {} user [{}]", - enabled ? "enable" : "disable", username), e); - ValidationException validationException = new ValidationException(); - validationException.addValidationError("only existing users can be " + - (enabled ? "enabled" : "disabled")); - failure = validationException; - } - listener.onFailure(failure); + @Override + public void onFailure(Exception e) { + Exception failure = e; + if (isIndexNotFoundOrDocumentMissing(e)) { + // if the index doesn't exist we can never update a user + // if the document doesn't exist, then this update is not valid + logger.debug( + (org.apache.logging.log4j.util.Supplier) () -> new ParameterizedMessage( + "failed to {} user [{}]", + enabled ? "enable" : "disable", + username + ), + e + ); + ValidationException validationException = new ValidationException(); + validationException.addValidationError("only existing users can be " + (enabled ? "enabled" : "disabled")); + failure = validationException; } - }, client::update); + listener.onFailure(failure); + } + }, + client::update + ); }); } - private void setReservedUserEnabled(final String username, final boolean enabled, final RefreshPolicy refreshPolicy, - boolean clearCache, final ActionListener listener) { + private void setReservedUserEnabled( + final String username, + final boolean enabled, + final RefreshPolicy refreshPolicy, + boolean clearCache, + final ActionListener listener + ) { securityIndex.prepareIndexIfNeededThenExecute(listener::onFailure, () -> { - executeAsyncWithOrigin(client.threadPool().getThreadContext(), SECURITY_ORIGIN, - client.prepareUpdate(SECURITY_MAIN_ALIAS, getIdForUser(RESERVED_USER_TYPE, username)) - .setDoc(Requests.INDEX_CONTENT_TYPE, Fields.ENABLED.getPreferredName(), enabled) - .setUpsert(XContentType.JSON, - Fields.PASSWORD.getPreferredName(), "", - Fields.ENABLED.getPreferredName(), enabled, - Fields.TYPE.getPreferredName(), RESERVED_USER_TYPE) - .setRefreshPolicy(refreshPolicy) - .request(), - listener.delegateFailure((l, updateResponse) -> { - if (clearCache) { - clearRealmCache(username, l, null); - } else { - l.onResponse(null); - } - }), client::update); + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + SECURITY_ORIGIN, + client.prepareUpdate(SECURITY_MAIN_ALIAS, getIdForUser(RESERVED_USER_TYPE, username)) + .setDoc(Requests.INDEX_CONTENT_TYPE, Fields.ENABLED.getPreferredName(), enabled) + .setUpsert( + XContentType.JSON, + Fields.PASSWORD.getPreferredName(), + "", + Fields.ENABLED.getPreferredName(), + enabled, + Fields.TYPE.getPreferredName(), + RESERVED_USER_TYPE + ) + .setRefreshPolicy(refreshPolicy) + .request(), + listener.delegateFailure((l, updateResponse) -> { + if (clearCache) { + clearRealmCache(username, l, null); + } else { + l.onResponse(null); + } + }), + client::update + ); }); } @@ -477,13 +568,22 @@ public void deleteUser(final DeleteUserRequest deleteUserRequest, final ActionLi listener.onFailure(frozenSecurityIndex.getUnavailableReason()); } else { securityIndex.checkIndexVersionThenExecute(listener::onFailure, () -> { - DeleteRequest request = client - .prepareDelete(SECURITY_MAIN_ALIAS, getIdForUser(USER_DOC_TYPE, deleteUserRequest.username())) - .request(); + DeleteRequest request = client.prepareDelete(SECURITY_MAIN_ALIAS, getIdForUser(USER_DOC_TYPE, deleteUserRequest.username())) + .request(); request.setRefreshPolicy(deleteUserRequest.getRefreshPolicy()); - executeAsyncWithOrigin(client.threadPool().getThreadContext(), SECURITY_ORIGIN, request, - listener.delegateFailure((l, deleteResponse) -> clearRealmCache(deleteUserRequest.username(), l, - deleteResponse.getResult() == DocWriteResponse.Result.DELETED)), client::delete); + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + SECURITY_ORIGIN, + request, + listener.delegateFailure( + (l, deleteResponse) -> clearRealmCache( + deleteUserRequest.username(), + l, + deleteResponse.getResult() == DocWriteResponse.Result.DELETED + ) + ), + client::delete + ); }); } } @@ -508,8 +608,7 @@ void verifyPassword(String username, final SecureString password, ActionListener listener.onResponse(AuthenticationResult.notHandled()); } else { if (userAndPassword.verifyPassword(password)) { - logger.trace( - "successfully authenticated user [{}] (security index [{}])", userAndPassword, securityIndex.aliasName()); + logger.trace("successfully authenticated user [{}] (security index [{}])", userAndPassword, securityIndex.aliasName()); listener.onResponse(AuthenticationResult.success(userAndPassword.user())); } else { logger.trace("password mismatch for user [{}] (security index [{}])", userAndPassword, securityIndex.aliasName()); @@ -526,42 +625,52 @@ void getReservedUserInfo(String username, ActionListener liste } else if (frozenSecurityIndex.isAvailable() == false) { listener.onFailure(frozenSecurityIndex.getUnavailableReason()); } else { - securityIndex.checkIndexVersionThenExecute(listener::onFailure, () -> - executeAsyncWithOrigin(client.threadPool().getThreadContext(), SECURITY_ORIGIN, - client.prepareGet(SECURITY_MAIN_ALIAS, getIdForUser(RESERVED_USER_TYPE, username)) - .request(), - new ActionListener() { - @Override - public void onResponse(GetResponse getResponse) { - if (getResponse.isExists()) { - Map sourceMap = getResponse.getSourceAsMap(); - String password = (String) sourceMap.get(Fields.PASSWORD.getPreferredName()); - Boolean enabled = (Boolean) sourceMap.get(Fields.ENABLED.getPreferredName()); - if (password == null) { - listener.onFailure(new IllegalStateException("password hash must not be null!")); - } else if (enabled == null) { - listener.onFailure(new IllegalStateException("enabled must not be null!")); - } else if (password.isEmpty()) { - listener.onResponse(enabled ? ReservedUserInfo.defaultEnabledUserInfo() - : ReservedUserInfo.defaultDisabledUserInfo()); - } else { - listener.onResponse(new ReservedUserInfo(password.toCharArray(), enabled)); - } - } else { - listener.onResponse(null); - } + securityIndex.checkIndexVersionThenExecute( + listener::onFailure, + () -> executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + SECURITY_ORIGIN, + client.prepareGet(SECURITY_MAIN_ALIAS, getIdForUser(RESERVED_USER_TYPE, username)).request(), + new ActionListener() { + @Override + public void onResponse(GetResponse getResponse) { + if (getResponse.isExists()) { + Map sourceMap = getResponse.getSourceAsMap(); + String password = (String) sourceMap.get(Fields.PASSWORD.getPreferredName()); + Boolean enabled = (Boolean) sourceMap.get(Fields.ENABLED.getPreferredName()); + if (password == null) { + listener.onFailure(new IllegalStateException("password hash must not be null!")); + } else if (enabled == null) { + listener.onFailure(new IllegalStateException("enabled must not be null!")); + } else if (password.isEmpty()) { + listener.onResponse( + enabled ? ReservedUserInfo.defaultEnabledUserInfo() : ReservedUserInfo.defaultDisabledUserInfo() + ); + } else { + listener.onResponse(new ReservedUserInfo(password.toCharArray(), enabled)); } + } else { + listener.onResponse(null); + } + } - @Override - public void onFailure(Exception e) { - if (TransportActions.isShardNotAvailableException(e)) { - logger.trace((org.apache.logging.log4j.util.Supplier) () -> new ParameterizedMessage( - "could not retrieve built in user [{}] info since security index unavailable", username), - e); - } - listener.onFailure(e); - } - }, client::get)); + @Override + public void onFailure(Exception e) { + if (TransportActions.isShardNotAvailableException(e)) { + logger.trace( + (org.apache.logging.log4j.util.Supplier) () -> new ParameterizedMessage( + "could not retrieve built in user [{}] info since security index unavailable", + username + ), + e + ); + } + listener.onFailure(e); + } + }, + client::get + ) + ); } } @@ -572,25 +681,29 @@ void getAllReservedUserInfo(ActionListener> listen } else if (frozenSecurityIndex.isAvailable() == false) { listener.onFailure(frozenSecurityIndex.getUnavailableReason()); } else { - securityIndex.checkIndexVersionThenExecute(listener::onFailure, () -> - executeAsyncWithOrigin(client.threadPool().getThreadContext(), SECURITY_ORIGIN, + securityIndex.checkIndexVersionThenExecute( + listener::onFailure, + () -> executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + SECURITY_ORIGIN, client.prepareSearch(SECURITY_MAIN_ALIAS) .setTrackTotalHits(true) .setQuery(QueryBuilders.termQuery(Fields.TYPE.getPreferredName(), RESERVED_USER_TYPE)) - .setFetchSource(true).request(), + .setFetchSource(true) + .request(), new ActionListener() { @Override public void onResponse(SearchResponse searchResponse) { Map userInfos = new HashMap<>(); - assert searchResponse.getHits().getTotalHits().value <= 10 : - "there are more than 10 reserved users we need to change this to retrieve them all!"; + assert searchResponse.getHits().getTotalHits().value <= 10 + : "there are more than 10 reserved users we need to change this to retrieve them all!"; for (SearchHit searchHit : searchResponse.getHits().getHits()) { Map sourceMap = searchHit.getSourceAsMap(); String password = (String) sourceMap.get(Fields.PASSWORD.getPreferredName()); Boolean enabled = (Boolean) sourceMap.get(Fields.ENABLED.getPreferredName()); final String id = searchHit.getId(); - assert id != null && id.startsWith(RESERVED_USER_TYPE) : - "id [" + id + "] does not start with reserved-user prefix"; + assert id != null && id.startsWith(RESERVED_USER_TYPE) + : "id [" + id + "] does not start with reserved-user prefix"; final String username = id.substring(RESERVED_USER_TYPE.length() + 1); if (password == null) { listener.onFailure(new IllegalStateException("password hash must not be null!")); @@ -615,27 +728,31 @@ public void onFailure(Exception e) { listener.onFailure(e); } } - }, client::search)); + }, + client::search + ) + ); } } private void clearRealmCache(String username, ActionListener listener, Response response) { ClearRealmCacheRequest request = new ClearRealmCacheRequest().usernames(username); - executeAsyncWithOrigin(client, SECURITY_ORIGIN, ClearRealmCacheAction.INSTANCE, request, - new ActionListener<>() { - @Override - public void onResponse(ClearRealmCacheResponse nodes) { - listener.onResponse(response); - } + executeAsyncWithOrigin(client, SECURITY_ORIGIN, ClearRealmCacheAction.INSTANCE, request, new ActionListener<>() { + @Override + public void onResponse(ClearRealmCacheResponse nodes) { + listener.onResponse(response); + } - @Override - public void onFailure(Exception e) { - logger.error(new ParameterizedMessage("unable to clear realm cache for user [{}]", username), e); - ElasticsearchException exception = new ElasticsearchException("clearing the cache for [" + username - + "] failed. please clear the realm cache manually", e); - listener.onFailure(exception); - } - }); + @Override + public void onFailure(Exception e) { + logger.error(new ParameterizedMessage("unable to clear realm cache for user [{}]", username), e); + ElasticsearchException exception = new ElasticsearchException( + "clearing the cache for [" + username + "] failed. please clear the realm cache manually", + e + ); + listener.onFailure(exception); + } + }); } @Nullable diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/esnative/ReservedRealm.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/esnative/ReservedRealm.java index 131579ff74b4a..e216f05da09dd 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/esnative/ReservedRealm.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/esnative/ReservedRealm.java @@ -59,13 +59,17 @@ public class ReservedRealm extends CachingUsernamePasswordRealm { private final ReservedUserInfo bootstrapUserInfo; private final ReservedUserInfo autoconfigUserInfo; - public static final Setting BOOTSTRAP_ELASTIC_PASSWORD = SecureSetting.secureString("bootstrap.password", - KeyStoreWrapper.SEED_SETTING); + public static final Setting BOOTSTRAP_ELASTIC_PASSWORD = SecureSetting.secureString( + "bootstrap.password", + KeyStoreWrapper.SEED_SETTING + ); // we do not document this setting on the website because it mustn't be set by the users // it is only set by various installation scripts - public static final Setting AUTOCONFIG_ELASTIC_PASSWORD_HASH = - SecureSetting.secureString("autoconfiguration.password_hash", null); + public static final Setting AUTOCONFIG_ELASTIC_PASSWORD_HASH = SecureSetting.secureString( + "autoconfiguration.password_hash", + null + ); private final NativeUsersStore nativeUsersStore; private final AnonymousUser anonymousUser; @@ -75,13 +79,25 @@ public class ReservedRealm extends CachingUsernamePasswordRealm { private final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(logger.getName()); - public ReservedRealm(Environment env, Settings settings, NativeUsersStore nativeUsersStore, AnonymousUser anonymousUser, - ThreadPool threadPool) { - super(new RealmConfig(new RealmConfig.RealmIdentifier(TYPE, NAME), - Settings.builder() - .put(settings) - .put(RealmSettings.realmSettingPrefix(new RealmConfig.RealmIdentifier(TYPE, NAME)) + "order", Integer.MIN_VALUE) - .build(), env, threadPool.getThreadContext()), threadPool); + public ReservedRealm( + Environment env, + Settings settings, + NativeUsersStore nativeUsersStore, + AnonymousUser anonymousUser, + ThreadPool threadPool + ) { + super( + new RealmConfig( + new RealmConfig.RealmIdentifier(TYPE, NAME), + Settings.builder() + .put(settings) + .put(RealmSettings.realmSettingPrefix(new RealmConfig.RealmIdentifier(TYPE, NAME)) + "order", Integer.MIN_VALUE) + .build(), + env, + threadPool.getThreadContext() + ), + threadPool + ); this.nativeUsersStore = nativeUsersStore; this.realmEnabled = XPackSettings.RESERVED_REALM_ENABLED_SETTING.get(settings); this.anonymousUser = anonymousUser; @@ -90,21 +106,22 @@ public ReservedRealm(Environment env, Settings settings, NativeUsersStore native // validate the password hash setting value, even if it is not going to be used if (AUTOCONFIG_ELASTIC_PASSWORD_HASH.exists(settings)) { autoconfigPasswordHash = AUTOCONFIG_ELASTIC_PASSWORD_HASH.get(settings).getChars(); - if (autoconfigPasswordHash.length == 0 || Set.of(Hasher.SHA1, Hasher.MD5, Hasher.SSHA256, Hasher.NOOP) - .contains(Hasher.resolveFromHash(autoconfigPasswordHash))) { + if (autoconfigPasswordHash.length == 0 + || Set.of(Hasher.SHA1, Hasher.MD5, Hasher.SSHA256, Hasher.NOOP).contains(Hasher.resolveFromHash(autoconfigPasswordHash))) { throw new IllegalArgumentException("Invalid password hash for elastic user auto configuration"); } } - elasticUserAutoconfigured = - AUTOCONFIG_ELASTIC_PASSWORD_HASH.exists(settings) && false == BOOTSTRAP_ELASTIC_PASSWORD.exists(settings); + elasticUserAutoconfigured = AUTOCONFIG_ELASTIC_PASSWORD_HASH.exists(settings) + && false == BOOTSTRAP_ELASTIC_PASSWORD.exists(settings); if (elasticUserAutoconfigured) { autoconfigUserInfo = new ReservedUserInfo(autoconfigPasswordHash, true); bootstrapUserInfo = null; } else { autoconfigUserInfo = null; final Hasher reservedRealmHasher = Hasher.resolve(XPackSettings.PASSWORD_HASHING_ALGORITHM.get(settings)); - final char[] hash = BOOTSTRAP_ELASTIC_PASSWORD.get(settings).length() == 0 ? new char[0] : - reservedRealmHasher.hash(BOOTSTRAP_ELASTIC_PASSWORD.get(settings)); + final char[] hash = BOOTSTRAP_ELASTIC_PASSWORD.get(settings).length() == 0 + ? new char[0] + : reservedRealmHasher.hash(BOOTSTRAP_ELASTIC_PASSWORD.get(settings)); bootstrapUserInfo = new ReservedUserInfo(hash, true); } } @@ -132,22 +149,31 @@ protected void doAuthenticate(UsernamePasswordToken token, ActionListener { - hashCleanupListener.onResponse(AuthenticationResult.success(user)); - }, e -> { - // exceptionally, we must propagate a 500 or a 503 error if the auto config password hash - // can't be promoted as the elastic user password, otherwise, such errors will - // implicitly translate to 401s, which is wrong because the presented password was successfully - // verified by the auto-config hash; the client must retry the request. - listener.onFailure(Exceptions.authenticationProcessError("failed to promote the auto-configured " + - "elastic password hash", e)); - })); + nativeUsersStore.createElasticUser( + userInfo.passwordHash, + ActionListener.wrap( + aVoid -> { hashCleanupListener.onResponse(AuthenticationResult.success(user)); }, + e -> { + // exceptionally, we must propagate a 500 or a 503 error if the auto config password hash + // can't be promoted as the elastic user password, otherwise, such errors will + // implicitly translate to 401s, which is wrong because the presented password was successfully + // verified by the auto-config hash; the client must retry the request. + listener.onFailure( + Exceptions.authenticationProcessError( + "failed to promote the auto-configured " + "elastic password hash", + e + ) + ); + } + ) + ); } else { hashCleanupListener.onResponse(AuthenticationResult.success(user)); } } else { - hashCleanupListener.onResponse(AuthenticationResult.terminate("failed to authenticate user [" + - token.principal() + "]")); + hashCleanupListener.onResponse( + AuthenticationResult.terminate("failed to authenticate user [" + token.principal() + "]") + ); } } } else { @@ -254,19 +280,25 @@ private void getUserInfo(final String username, Consumer consu consumer.accept(userInfo); } }, (e) -> { - logger.error((Supplier) () -> - new ParameterizedMessage("failed to retrieve password hash for reserved user [{}]", username), e); + logger.error( + (Supplier) () -> new ParameterizedMessage("failed to retrieve password hash for reserved user [{}]", username), + e + ); consumer.accept(null); })); } - private void logDeprecatedUser(final User user){ + private void logDeprecatedUser(final User user) { Map metadata = user.metadata(); if (Boolean.TRUE.equals(metadata.get(MetadataUtils.DEPRECATED_METADATA_KEY))) { - deprecationLogger.critical(DeprecationCategory.SECURITY, "deprecated_user-" + user.principal(), "The user [" + - user.principal() + - "] is deprecated and will be removed in a future version of Elasticsearch. " + - metadata.get(MetadataUtils.DEPRECATED_REASON_METADATA_KEY)); + deprecationLogger.critical( + DeprecationCategory.SECURITY, + "deprecated_user-" + user.principal(), + "The user [" + + user.principal() + + "] is deprecated and will be removed in a future version of Elasticsearch. " + + metadata.get(MetadataUtils.DEPRECATED_REASON_METADATA_KEY) + ); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/esnative/tool/ResetPasswordTool.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/esnative/tool/ResetPasswordTool.java index 8d00e0a8a3060..632bcd7627334 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/esnative/tool/ResetPasswordTool.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/esnative/tool/ResetPasswordTool.java @@ -8,7 +8,6 @@ package org.elasticsearch.xpack.security.authc.esnative.tool; import joptsimple.OptionSet; - import joptsimple.OptionSpec; import joptsimple.OptionSpecBuilder; @@ -18,14 +17,14 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.KeyStoreWrapper; import org.elasticsearch.common.settings.SecureString; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.core.CheckedFunction; import org.elasticsearch.env.Environment; -import org.elasticsearch.xpack.core.security.support.Validation; -import org.elasticsearch.xpack.security.tool.BaseRunAsSuperuserCommand; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.security.CommandLineHttpClient; import org.elasticsearch.xpack.core.security.HttpResponse; +import org.elasticsearch.xpack.core.security.support.Validation; +import org.elasticsearch.xpack.security.tool.BaseRunAsSuperuserCommand; import java.net.HttpURLConnection; import java.net.URL; diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/esnative/tool/SetupPasswordTool.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/esnative/tool/SetupPasswordTool.java index 252a723571375..5346e2b436902 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/esnative/tool/SetupPasswordTool.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/esnative/tool/SetupPasswordTool.java @@ -9,6 +9,7 @@ import joptsimple.OptionParser; import joptsimple.OptionSet; import joptsimple.OptionSpec; + import org.elasticsearch.ExceptionsHelper; import org.elasticsearch.cli.ExitCodes; import org.elasticsearch.cli.KeyStoreAwareCommand; @@ -16,17 +17,20 @@ import org.elasticsearch.cli.Terminal; import org.elasticsearch.cli.Terminal.Verbosity; import org.elasticsearch.cli.UserException; -import org.elasticsearch.core.Booleans; import org.elasticsearch.common.CheckedBiConsumer; -import org.elasticsearch.core.CheckedFunction; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.KeyStoreWrapper; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.Booleans; +import org.elasticsearch.core.CheckedFunction; +import org.elasticsearch.env.Environment; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.env.Environment; import org.elasticsearch.xpack.core.XPackSettings; +import org.elasticsearch.xpack.core.security.CommandLineHttpClient; +import org.elasticsearch.xpack.core.security.HttpResponse; +import org.elasticsearch.xpack.core.security.HttpResponse.HttpResponseBuilder; import org.elasticsearch.xpack.core.security.support.Validation; import org.elasticsearch.xpack.core.security.user.APMSystemUser; import org.elasticsearch.xpack.core.security.user.BeatsSystemUser; @@ -36,11 +40,7 @@ import org.elasticsearch.xpack.core.security.user.LogstashSystemUser; import org.elasticsearch.xpack.core.security.user.RemoteMonitoringUser; import org.elasticsearch.xpack.security.authc.esnative.ReservedRealm; -import org.elasticsearch.xpack.core.security.HttpResponse; -import org.elasticsearch.xpack.core.security.HttpResponse.HttpResponseBuilder; -import org.elasticsearch.xpack.core.security.CommandLineHttpClient; -import javax.net.ssl.SSLException; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; @@ -56,6 +56,8 @@ import java.util.Objects; import java.util.function.Function; +import javax.net.ssl.SSLException; + import static java.util.Arrays.asList; /** @@ -73,8 +75,15 @@ public class SetupPasswordTool extends LoggingAwareMultiCommand { private static final char[] CHARS = ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789").toCharArray(); - public static final List USERS = asList(ElasticUser.NAME, APMSystemUser.NAME, KibanaUser.NAME, KibanaSystemUser.NAME, - LogstashSystemUser.NAME, BeatsSystemUser.NAME, RemoteMonitoringUser.NAME); + public static final List USERS = asList( + ElasticUser.NAME, + APMSystemUser.NAME, + KibanaUser.NAME, + KibanaSystemUser.NAME, + LogstashSystemUser.NAME, + BeatsSystemUser.NAME, + RemoteMonitoringUser.NAME + ); public static final Map USERS_WITH_SHARED_PASSWORDS = Map.of(KibanaSystemUser.NAME, KibanaUser.NAME); @@ -87,15 +96,19 @@ public class SetupPasswordTool extends LoggingAwareMultiCommand { this(environment -> new CommandLineHttpClient(environment), environment -> { KeyStoreWrapper keyStoreWrapper = KeyStoreWrapper.load(environment.configFile()); if (keyStoreWrapper == null) { - throw new UserException(ExitCodes.CONFIG, - "Elasticsearch keystore file is missing [" + KeyStoreWrapper.keystorePath(environment.configFile()) + "]"); + throw new UserException( + ExitCodes.CONFIG, + "Elasticsearch keystore file is missing [" + KeyStoreWrapper.keystorePath(environment.configFile()) + "]" + ); } return keyStoreWrapper; }); } - SetupPasswordTool(Function clientFunction, - CheckedFunction keyStoreFunction) { + SetupPasswordTool( + Function clientFunction, + CheckedFunction keyStoreFunction + ) { super("Sets the passwords for reserved users"); subcommands.put("auto", newAutoSetup()); subcommands.put("interactive", newInteractiveSetup()); @@ -139,8 +152,10 @@ protected void execute(Terminal terminal, OptionSet options, Environment env) th if (shouldPrompt) { terminal.println("******************************************************************************"); - terminal.println("Note: The 'elasticsearch-setup-passwords' tool has been deprecated. This " + - " command will be removed in a future release."); + terminal.println( + "Note: The 'elasticsearch-setup-passwords' tool has been deprecated. This " + + " command will be removed in a future release." + ); terminal.println("******************************************************************************"); terminal.println(""); terminal.println("Initiating the setup of passwords for reserved users " + String.join(",", USERS) + "."); @@ -153,8 +168,11 @@ protected void execute(Terminal terminal, OptionSet options, Environment env) th } SecureRandom secureRandom = new SecureRandom(); - changePasswords((user) -> generatePassword(secureRandom, user), - (user, password) -> changedPasswordCallback(terminal, user, password), terminal); + changePasswords( + (user) -> generatePassword(secureRandom, user), + (user, password) -> changedPasswordCallback(terminal, user, password), + terminal + ); } private SecureString generatePassword(SecureRandom secureRandom, String user) { @@ -190,8 +208,10 @@ protected void execute(Terminal terminal, OptionSet options, Environment env) th if (shouldPrompt) { terminal.println("******************************************************************************"); - terminal.println("Note: The 'elasticsearch-setup-passwords' tool has been deprecated. This " + - " command will be removed in a future release."); + terminal.println( + "Note: The 'elasticsearch-setup-passwords' tool has been deprecated. This " + + " command will be removed in a future release." + ); terminal.println("******************************************************************************"); terminal.println(""); terminal.println("Initiating the setup of passwords for reserved users " + String.join(",", USERS) + "."); @@ -203,8 +223,11 @@ protected void execute(Terminal terminal, OptionSet options, Environment env) th } } - changePasswords(user -> promptForPassword(terminal, user), - (user, password) -> changedPasswordCallback(terminal, user, password), terminal); + changePasswords( + user -> promptForPassword(terminal, user), + (user, password) -> changedPasswordCallback(terminal, user, password), + terminal + ); } private SecureString promptForPassword(Terminal terminal, String user) throws UserException { @@ -290,8 +313,10 @@ void setupOptions(Terminal terminal, OptionSet options, Environment env) throws private void setParser() { urlOption = parser.acceptsAll(asList("u", "url"), "The url for the change password request.").withRequiredArg(); - noPromptOption = parser.acceptsAll(asList("b", "batch"), - "If enabled, run the change password process without prompting the user.").withOptionalArg(); + noPromptOption = parser.acceptsAll( + asList("b", "batch"), + "If enabled, run the change password process without prompting the user." + ).withOptionalArg(); } private void setShouldPrompt(OptionSet options) { @@ -315,8 +340,14 @@ void checkElasticKeystorePasswordValid(Terminal terminal, Environment env) throw terminal.println(Verbosity.VERBOSE, ""); terminal.println(Verbosity.VERBOSE, "Testing if bootstrap password is valid for " + route.toString()); try { - final HttpResponse httpResponse = client.execute("GET", route, elasticUser, elasticUserPassword, () -> null, - is -> responseBuilder(is, terminal)); + final HttpResponse httpResponse = client.execute( + "GET", + route, + elasticUser, + elasticUserPassword, + () -> null, + is -> responseBuilder(is, terminal) + ); final int httpCode = httpResponse.getHttpStatus(); // keystore password is not valid @@ -329,8 +360,7 @@ void checkElasticKeystorePasswordValid(Terminal terminal, Environment env) throw terminal.errorPrintln(" This tool used the keystore at " + KeyStoreWrapper.keystorePath(env.configFile())); terminal.errorPrintln(""); terminal.errorPrintln( - "You can use the `elasticsearch-reset-password` CLI tool to reset the password of the '" + elasticUser - + "' user" + "You can use the `elasticsearch-reset-password` CLI tool to reset the password of the '" + elasticUser + "' user" ); terminal.errorPrintln(""); throw new UserException(ExitCodes.CONFIG, "Failed to verify bootstrap password"); @@ -340,15 +370,17 @@ void checkElasticKeystorePasswordValid(Terminal terminal, Environment env) throw XPackSecurityFeatureConfig xPackSecurityFeatureConfig = getXPackSecurityConfig(terminal); if (xPackSecurityFeatureConfig.isAvailable == false) { terminal.errorPrintln("It doesn't look like the X-Pack security feature is available on this Elasticsearch node."); - terminal.errorPrintln("Please check if you have installed a license that allows access to " + - "X-Pack Security feature."); + terminal.errorPrintln( + "Please check if you have installed a license that allows access to " + "X-Pack Security feature." + ); terminal.errorPrintln(""); throw new UserException(ExitCodes.CONFIG, "X-Pack Security is not available."); } if (xPackSecurityFeatureConfig.isEnabled == false) { terminal.errorPrintln("It doesn't look like the X-Pack security feature is enabled on this Elasticsearch node."); - terminal.errorPrintln("Please check if you have enabled X-Pack security in your elasticsearch.yml " + - "configuration file."); + terminal.errorPrintln( + "Please check if you have enabled X-Pack security in your elasticsearch.yml " + "configuration file." + ); terminal.errorPrintln(""); throw new UserException(ExitCodes.CONFIG, "X-Pack Security is disabled by configuration."); } @@ -367,16 +399,22 @@ void checkElasticKeystorePasswordValid(Terminal terminal, Environment env) throw terminal.errorPrintln(Verbosity.VERBOSE, ""); terminal.errorPrintln(Verbosity.VERBOSE, ExceptionsHelper.stackTrace(e)); terminal.errorPrintln(""); - throw new UserException(ExitCodes.CONFIG, - "Failed to establish SSL connection to elasticsearch at " + route.toString() + ". ", e); + throw new UserException( + ExitCodes.CONFIG, + "Failed to establish SSL connection to elasticsearch at " + route.toString() + ". ", + e + ); } catch (IOException e) { terminal.errorPrintln(""); terminal.errorPrintln("Connection failure to: " + route.toString() + " failed: " + e.getMessage()); terminal.errorPrintln(Verbosity.VERBOSE, ""); terminal.errorPrintln(Verbosity.VERBOSE, ExceptionsHelper.stackTrace(e)); terminal.errorPrintln(""); - throw new UserException(ExitCodes.CONFIG, - "Failed to connect to elasticsearch at " + route.toString() + ". Is the URL correct and elasticsearch running?", e); + throw new UserException( + ExitCodes.CONFIG, + "Failed to connect to elasticsearch at " + route.toString() + ". Is the URL correct and elasticsearch running?", + e + ); } } @@ -384,12 +422,19 @@ void checkElasticKeystorePasswordValid(Terminal terminal, Environment env) throw private XPackSecurityFeatureConfig getXPackSecurityConfig(Terminal terminal) throws Exception { // Get x-pack security info. URL route = createURL(url, "/_xpack", "?categories=features&human=false&pretty"); - final HttpResponse httpResponse = - client.execute("GET", route, elasticUser, elasticUserPassword, () -> null, is -> responseBuilder(is, terminal)); + final HttpResponse httpResponse = client.execute( + "GET", + route, + elasticUser, + elasticUserPassword, + () -> null, + is -> responseBuilder(is, terminal) + ); if (httpResponse.getHttpStatus() != HttpURLConnection.HTTP_OK) { terminal.errorPrintln(""); - terminal.errorPrintln("Unexpected response code [" + httpResponse.getHttpStatus() + "] from calling GET " + - route.toString()); + terminal.errorPrintln( + "Unexpected response code [" + httpResponse.getHttpStatus() + "] from calling GET " + route.toString() + ); if (httpResponse.getHttpStatus() == HttpURLConnection.HTTP_BAD_REQUEST) { terminal.errorPrintln("It doesn't look like the X-Pack is available on this Elasticsearch node."); terminal.errorPrintln("Please check that you have followed all installation instructions and that this tool"); @@ -410,9 +455,10 @@ private XPackSecurityFeatureConfig getXPackSecurityConfig(Terminal terminal) thr if (features != null) { Map featureInfo = (Map) features.get("security"); if (featureInfo != null) { - xPackSecurityFeatureConfig = - new XPackSecurityFeatureConfig(Boolean.parseBoolean(featureInfo.get("available").toString()), - Boolean.parseBoolean(featureInfo.get("enabled").toString())); + xPackSecurityFeatureConfig = new XPackSecurityFeatureConfig( + Boolean.parseBoolean(featureInfo.get("available").toString()), + Boolean.parseBoolean(featureInfo.get("enabled").toString()) + ); return xPackSecurityFeatureConfig; } } @@ -430,13 +476,20 @@ void checkClusterHealth(Terminal terminal) throws Exception { URL route = createURL(url, "/_cluster/health", "?pretty"); terminal.println(Verbosity.VERBOSE, ""); terminal.println(Verbosity.VERBOSE, "Checking cluster health: " + route.toString()); - final HttpResponse httpResponse = client.execute("GET", route, elasticUser, elasticUserPassword, () -> null, - is -> responseBuilder(is, terminal)); + final HttpResponse httpResponse = client.execute( + "GET", + route, + elasticUser, + elasticUserPassword, + () -> null, + is -> responseBuilder(is, terminal) + ); if (httpResponse.getHttpStatus() != HttpURLConnection.HTTP_OK) { terminal.errorPrintln(""); terminal.errorPrintln("Failed to determine the health of the cluster running at " + url); - terminal.errorPrintln("Unexpected response code [" + httpResponse.getHttpStatus() + "] from calling GET " + - route.toString()); + terminal.errorPrintln( + "Unexpected response code [" + httpResponse.getHttpStatus() + "] from calling GET " + route.toString() + ); final String cause = CommandLineHttpClient.getErrorCause(httpResponse); if (cause != null) { terminal.errorPrintln("Cause: " + cause); @@ -458,7 +511,8 @@ void checkClusterHealth(Terminal terminal) throws Exception { } terminal.errorPrintln(""); terminal.errorPrintln( - "It is recommended that you resolve the issues with your cluster before running elasticsearch-setup-passwords."); + "It is recommended that you resolve the issues with your cluster before running elasticsearch-setup-passwords." + ); terminal.errorPrintln("It is very likely that the password changes will fail when run against an unhealthy cluster."); terminal.errorPrintln(""); if (shouldPrompt) { @@ -495,7 +549,8 @@ private void changeUserPassword(String user, SecureString password, Terminal ter if (httpResponse.getHttpStatus() != HttpURLConnection.HTTP_OK) { terminal.errorPrintln(""); terminal.errorPrintln( - "Unexpected response code [" + httpResponse.getHttpStatus() + "] from calling PUT " + route.toString()); + "Unexpected response code [" + httpResponse.getHttpStatus() + "] from calling PUT " + route.toString() + ); String cause = CommandLineHttpClient.getErrorCause(httpResponse); if (cause != null) { terminal.errorPrintln("Cause: " + cause); @@ -526,8 +581,11 @@ private void changeUserPassword(String user, SecureString password, Terminal ter * @param passwordFn Function to generate or prompt for each user's password. * @param successCallback Callback for each successful operation */ - void changePasswords(CheckedFunction passwordFn, - CheckedBiConsumer successCallback, Terminal terminal) throws Exception { + void changePasswords( + CheckedFunction passwordFn, + CheckedBiConsumer successCallback, + Terminal terminal + ) throws Exception { Map passwordsMap = new LinkedHashMap<>(USERS.size()); try { for (String user : USERS) { diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/file/FileUserPasswdStore.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/file/FileUserPasswdStore.java index e917de95579e2..4c13fe4e66ac3 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/file/FileUserPasswdStore.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/file/FileUserPasswdStore.java @@ -11,10 +11,10 @@ import org.apache.logging.log4j.message.ParameterizedMessage; import org.apache.logging.log4j.util.Supplier; import org.elasticsearch.ElasticsearchException; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.Maps; +import org.elasticsearch.core.Nullable; import org.elasticsearch.env.Environment; import org.elasticsearch.watcher.FileWatcher; import org.elasticsearch.watcher.ResourceWatcherService; @@ -107,8 +107,12 @@ static Map parseFileLenient(Path path, Logger logger, Settings s return map == null ? emptyMap() : map; } catch (Exception e) { logger.error( - (Supplier) () -> new ParameterizedMessage( - "failed to parse users file [{}]. skipping/removing all users...", path.toAbsolutePath()), e); + (Supplier) () -> new ParameterizedMessage( + "failed to parse users file [{}]. skipping/removing all users...", + path.toAbsolutePath() + ), + e + ); return emptyMap(); } } @@ -156,8 +160,12 @@ public static Map parseFile(Path path, @Nullable Logger logger, String username = line.substring(0, i); Validation.Error validationError = Users.validateUsername(username, allowReserved, settings); if (validationError != null) { - logger.error("invalid username [{}] in users file [{}], skipping... ({})", username, path.toAbsolutePath(), - validationError); + logger.error( + "invalid username [{}] in users file [{}], skipping... ({})", + username, + path.toAbsolutePath(), + validationError + ); continue; } String hash = line.substring(i + 1); @@ -169,10 +177,7 @@ public static Map parseFile(Path path, @Nullable Logger logger, } public static void writeFile(Map users, Path path) { - SecurityFiles.writeFileAtomically( - path, - users, - e -> String.format(Locale.ROOT, "%s:%s", e.getKey(), new String(e.getValue()))); + SecurityFiles.writeFileAtomically(path, users, e -> String.format(Locale.ROOT, "%s:%s", e.getKey(), new String(e.getValue()))); } void notifyRefresh() { diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/file/FileUserRolesStore.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/file/FileUserRolesStore.java index f7458c2770581..5ce584e1a4b0b 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/file/FileUserRolesStore.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/file/FileUserRolesStore.java @@ -11,9 +11,9 @@ import org.apache.logging.log4j.message.ParameterizedMessage; import org.apache.logging.log4j.util.Supplier; import org.elasticsearch.ElasticsearchException; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.util.Maps; +import org.elasticsearch.core.Nullable; import org.elasticsearch.env.Environment; import org.elasticsearch.watcher.FileChangesListener; import org.elasticsearch.watcher.FileWatcher; @@ -94,9 +94,12 @@ static Map parseFileLenient(Path path, Logger logger) { return map == null ? emptyMap() : map; } catch (Exception e) { logger.error( - (Supplier) () -> new ParameterizedMessage("failed to parse users_roles file [{}]. skipping/removing all entries...", - path.toAbsolutePath()), - e); + (Supplier) () -> new ParameterizedMessage( + "failed to parse users_roles file [{}]. skipping/removing all entries...", + path.toAbsolutePath() + ), + e + ); return emptyMap(); } } @@ -129,7 +132,7 @@ public static Map parseFile(Path path, @Nullable Logger logger int lineNr = 0; for (String line : lines) { lineNr++; - if (line.startsWith("#")) { //comment + if (line.startsWith("#")) { // comment continue; } int i = line.indexOf(":"); @@ -140,20 +143,32 @@ public static Map parseFile(Path path, @Nullable Logger logger String role = line.substring(0, i).trim(); Validation.Error validationError = Validation.Roles.validateRoleName(role, true); if (validationError != null) { - logger.error("invalid role entry in users_roles file [{}], line [{}] - {}. skipping...", path.toAbsolutePath(), lineNr, - validationError); + logger.error( + "invalid role entry in users_roles file [{}], line [{}] - {}. skipping...", + path.toAbsolutePath(), + lineNr, + validationError + ); continue; } String usersStr = line.substring(i + 1).trim(); if (Strings.isEmpty(usersStr)) { - logger.error("invalid entry for role [{}] in users_roles file [{}], line [{}]. no users found. skipping...", role, - path.toAbsolutePath(), lineNr); + logger.error( + "invalid entry for role [{}] in users_roles file [{}], line [{}]. no users found. skipping...", + role, + path.toAbsolutePath(), + lineNr + ); continue; } String[] roleUsers = USERS_DELIM.split(usersStr); if (roleUsers.length == 0) { - logger.error("invalid entry for role [{}] in users_roles file [{}], line [{}]. no users found. skipping...", role, - path.toAbsolutePath(), lineNr); + logger.error( + "invalid entry for role [{}] in users_roles file [{}], line [{}]. no users found. skipping...", + role, + path.toAbsolutePath(), + lineNr + ); continue; } @@ -185,9 +200,10 @@ public static void writeFile(Map userToRoles, Path path) { } SecurityFiles.writeFileAtomically( - path, - roleToUsers, - e -> String.format(Locale.ROOT, "%s:%s", e.getKey(), collectionToCommaDelimitedString(e.getValue()))); + path, + roleToUsers, + e -> String.format(Locale.ROOT, "%s:%s", e.getKey(), collectionToCommaDelimitedString(e.getValue())) + ); } void notifyRefresh() { diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/file/tool/UsersTool.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/file/tool/UsersTool.java index 425e8f75573cd..b77e3b334332d 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/file/tool/UsersTool.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/file/tool/UsersTool.java @@ -8,6 +8,7 @@ import joptsimple.OptionSet; import joptsimple.OptionSpec; + import org.elasticsearch.ElasticsearchException; import org.elasticsearch.cli.EnvironmentAwareCommand; import org.elasticsearch.cli.ExitCodes; @@ -85,12 +86,10 @@ static class AddUserCommand extends EnvironmentAwareCommand { AddUserCommand() { super("Adds a file user"); - this.passwordOption = parser.acceptsAll(Arrays.asList("p", "password"), - "The user password") - .withRequiredArg(); - this.rolesOption = parser.acceptsAll(Arrays.asList("r", "roles"), - "Comma-separated list of the roles of the user") - .withRequiredArg().defaultsTo(""); + this.passwordOption = parser.acceptsAll(Arrays.asList("p", "password"), "The user password").withRequiredArg(); + this.rolesOption = parser.acceptsAll(Arrays.asList("r", "roles"), "Comma-separated list of the roles of the user") + .withRequiredArg() + .defaultsTo(""); this.arguments = parser.nonOptions("username"); } @@ -199,9 +198,7 @@ static class PasswordCommand extends EnvironmentAwareCommand { PasswordCommand() { super("Changes the password of an existing file based user"); - this.passwordOption = parser.acceptsAll(Arrays.asList("p", "password"), - "The user password") - .withRequiredArg(); + this.passwordOption = parser.acceptsAll(Arrays.asList("p", "password"), "The user password").withRequiredArg(); this.arguments = parser.nonOptions("username"); } @@ -245,12 +242,12 @@ static class RolesCommand extends EnvironmentAwareCommand { RolesCommand() { super("Edit roles of an existing user"); - this.addOption = parser.acceptsAll(Arrays.asList("a", "add"), - "Adds supplied roles to the specified user") - .withRequiredArg().defaultsTo(""); - this.removeOption = parser.acceptsAll(Arrays.asList("r", "remove"), - "Remove supplied roles from the specified user") - .withRequiredArg().defaultsTo(""); + this.addOption = parser.acceptsAll(Arrays.asList("a", "add"), "Adds supplied roles to the specified user") + .withRequiredArg() + .defaultsTo(""); + this.removeOption = parser.acceptsAll(Arrays.asList("r", "remove"), "Remove supplied roles from the specified user") + .withRequiredArg() + .defaultsTo(""); this.arguments = parser.nonOptions("username"); } @@ -299,7 +296,7 @@ protected void execute(Terminal terminal, OptionSet options, Environment env) th if (roles.isEmpty()) { userRolesToWrite.remove(username); } else { - userRolesToWrite.put(username, new LinkedHashSet<>(roles).toArray(new String[]{})); + userRolesToWrite.put(username, new LinkedHashSet<>(roles).toArray(new String[] {})); } FileUserRolesStore.writeFile(userRolesToWrite, rolesFile); @@ -362,14 +359,24 @@ static void listUsersAndRoles(Terminal terminal, Environment env, String usernam String[] roles = userRoles.get(username); Set unknownRoles = Sets.difference(Sets.newHashSet(roles), knownRoles); String[] markedRoles = markUnknownRoles(roles, unknownRoles); - terminal.println(String.format(Locale.ROOT, "%-15s: %s", username, Arrays.stream(markedRoles).map(s -> s == null ? - "-" : s).collect(Collectors.joining(",")))); + terminal.println( + String.format( + Locale.ROOT, + "%-15s: %s", + username, + Arrays.stream(markedRoles).map(s -> s == null ? "-" : s).collect(Collectors.joining(",")) + ) + ); if (unknownRoles.isEmpty() == false) { // at least one role is marked... so printing the legend Path rolesFile = FileRolesStore.resolveFile(env).toAbsolutePath(); terminal.println(""); - terminal.println(" [*] Role is not in the [" + rolesFile.toAbsolutePath() + "] file. If the role has been created " - + "using the API, please disregard this message."); + terminal.println( + " [*] Role is not in the [" + + rolesFile.toAbsolutePath() + + "] file. If the role has been created " + + "using the API, please disregard this message." + ); } } else { terminal.println(String.format(Locale.ROOT, "%-15s: -", username)); @@ -402,8 +409,12 @@ static void listUsersAndRoles(Terminal terminal, Environment env, String usernam // at least one role is marked... so printing the legend Path rolesFile = FileRolesStore.resolveFile(env).toAbsolutePath(); terminal.println(""); - terminal.println(" [*] Role is not in the [" + rolesFile.toAbsolutePath() + "] file. If the role has been created " - + "using the API, please disregard this message."); + terminal.println( + " [*] Role is not in the [" + + rolesFile.toAbsolutePath() + + "] file. If the role has been created " + + "using the API, please disregard this message." + ); } } } @@ -442,8 +453,13 @@ static String parseUsername(List args, Settings settings) throws UserExc private static char[] getPasswordHash(Terminal terminal, Environment env, String cliPasswordValue) throws UserException { final Hasher hasher = Hasher.resolve(XPackSettings.PASSWORD_HASHING_ALGORITHM.get(env.settings())); if (XPackSettings.FIPS_MODE_ENABLED.get(env.settings()) && hasher.name().toLowerCase(Locale.ROOT).startsWith("pbkdf2") == false) { - throw new UserException(ExitCodes.CONFIG, "Only PBKDF2 is allowed for password hashing in a FIPS 140 JVM. Please set the " + - "appropriate value for [ " + XPackSettings.PASSWORD_HASHING_ALGORITHM.getKey() + " ] setting."); + throw new UserException( + ExitCodes.CONFIG, + "Only PBKDF2 is allowed for password hashing in a FIPS 140 JVM. Please set the " + + "appropriate value for [ " + + XPackSettings.PASSWORD_HASHING_ALGORITHM.getKey() + + " ] setting." + ); } final char[] passwordHash; try (SecureString password = parsePassword(terminal, cliPasswordValue)) { @@ -484,10 +500,16 @@ private static void verifyRoles(Terminal terminal, Environment env, String[] rol Set knownRoles = Sets.union(FileRolesStore.parseFileForRoleNames(rolesFile, null), ReservedRolesStore.names()); Set unknownRoles = Sets.difference(Sets.newHashSet(roles), knownRoles); if (unknownRoles.isEmpty() == false) { - terminal.errorPrintln(String.format(Locale.ROOT, "Warning: The following roles [%s] are not in the [%s] file. " + - "Make sure the names are correct. If the names are correct and the roles were created using the API please " + - "disregard this message. Nonetheless the user will still be associated with all specified roles", - Strings.collectionToCommaDelimitedString(unknownRoles), rolesFile.toAbsolutePath())); + terminal.errorPrintln( + String.format( + Locale.ROOT, + "Warning: The following roles [%s] are not in the [%s] file. " + + "Make sure the names are correct. If the names are correct and the roles were created using the API please " + + "disregard this message. Nonetheless the user will still be associated with all specified roles", + Strings.collectionToCommaDelimitedString(unknownRoles), + rolesFile.toAbsolutePath() + ) + ); terminal.errorPrintln("Known roles: " + knownRoles.toString()); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosAuthenticationToken.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosAuthenticationToken.java index 72f872dfa6020..d4d099cc633f1 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosAuthenticationToken.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosAuthenticationToken.java @@ -58,8 +58,13 @@ public static KerberosAuthenticationToken extractToken(final String authorizatio if (Strings.isNullOrEmpty(authorizationHeader)) { return null; } - if (authorizationHeader.regionMatches(IGNORE_CASE_AUTH_HEADER_MATCH, 0, NEGOTIATE_AUTH_HEADER_PREFIX, 0, - NEGOTIATE_AUTH_HEADER_PREFIX.length()) == false) { + if (authorizationHeader.regionMatches( + IGNORE_CASE_AUTH_HEADER_MATCH, + 0, + NEGOTIATE_AUTH_HEADER_PREFIX, + 0, + NEGOTIATE_AUTH_HEADER_PREFIX.length() + ) == false) { return null; } @@ -100,12 +105,9 @@ public int hashCode() { @Override public boolean equals(final Object other) { - if (this == other) - return true; - if (other == null) - return false; - if (getClass() != other.getClass()) - return false; + if (this == other) return true; + if (other == null) return false; + if (getClass() != other.getClass()) return false; final KerberosAuthenticationToken otherKerbToken = (KerberosAuthenticationToken) other; return Arrays.equals(otherKerbToken.decodedToken, this.decodedToken); } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosRealm.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosRealm.java index 961b4dfee4e1a..b51db1a138cde 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosRealm.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosRealm.java @@ -12,8 +12,8 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.cache.Cache; import org.elasticsearch.common.cache.CacheBuilder; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.xpack.core.security.authc.AuthenticationResult; @@ -21,10 +21,10 @@ import org.elasticsearch.xpack.core.security.authc.Realm; import org.elasticsearch.xpack.core.security.authc.RealmConfig; import org.elasticsearch.xpack.core.security.authc.kerberos.KerberosRealmSettings; -import org.elasticsearch.xpack.core.security.user.User; import org.elasticsearch.xpack.core.security.authc.support.CachingRealm; -import org.elasticsearch.xpack.security.authc.support.DelegatedAuthorizationSupport; import org.elasticsearch.xpack.core.security.authc.support.UserRoleMapper; +import org.elasticsearch.xpack.core.security.user.User; +import org.elasticsearch.xpack.security.authc.support.DelegatedAuthorizationSupport; import org.elasticsearch.xpack.security.authc.support.mapper.NativeRoleMappingStore; import org.ietf.jgss.GSSException; @@ -77,19 +77,24 @@ public KerberosRealm(final RealmConfig config, final NativeRoleMappingStore nati } // pkg scoped for testing - KerberosRealm(final RealmConfig config, final NativeRoleMappingStore nativeRoleMappingStore, - final KerberosTicketValidator kerberosTicketValidator, final ThreadPool threadPool, - final Cache userPrincipalNameToUserCache) { + KerberosRealm( + final RealmConfig config, + final NativeRoleMappingStore nativeRoleMappingStore, + final KerberosTicketValidator kerberosTicketValidator, + final ThreadPool threadPool, + final Cache userPrincipalNameToUserCache + ) { super(config); this.userRoleMapper = nativeRoleMappingStore; this.userRoleMapper.refreshRealmOnChange(this); final TimeValue ttl = config.getSetting(KerberosRealmSettings.CACHE_TTL_SETTING); if (ttl.getNanos() > 0) { this.userPrincipalNameToUserCache = (userPrincipalNameToUserCache == null) - ? CacheBuilder.builder() - .setExpireAfterWrite(config.getSetting(KerberosRealmSettings.CACHE_TTL_SETTING)) - .setMaximumWeight(config.getSetting(KerberosRealmSettings.CACHE_MAX_USERS_SETTING)).build() - : userPrincipalNameToUserCache; + ? CacheBuilder.builder() + .setExpireAfterWrite(config.getSetting(KerberosRealmSettings.CACHE_TTL_SETTING)) + .setMaximumWeight(config.getSetting(KerberosRealmSettings.CACHE_MAX_USERS_SETTING)) + .build() + : userPrincipalNameToUserCache; } else { this.userPrincipalNameToUserCache = null; } @@ -154,26 +159,30 @@ public void authenticate(final AuthenticationToken token, final ActionListener { - if (userPrincipalNameOutToken.v1() != null) { - resolveUser(userPrincipalNameOutToken.v1(), userPrincipalNameOutToken.v2(), listener); - } else { - /** - * This is when security context could not be established may be due to ongoing - * negotiation and requires token to be sent back to peer for continuing - * further. We are terminating the authentication process as this is spengo - * negotiation and no other realm can handle this. We can have only one Kerberos - * realm in the system so terminating with RestStatus Unauthorized (401) and - * with 'WWW-Authenticate' header populated with value with token in the form - * 'Negotiate oYH1MIHyoAMK...' - */ - String errorMessage = "failed to authenticate user, gss context negotiation not complete"; - ElasticsearchSecurityException ese = unauthorized(errorMessage, null); - ese = unauthorizedWithOutputToken(ese, userPrincipalNameOutToken.v2()); - listener.onResponse(AuthenticationResult.terminate(errorMessage, ese)); - } - }, e -> handleException(e, listener))); + kerberosTicketValidator.validateTicket( + (byte[]) kerbAuthnToken.credentials(), + keytabPath, + enableKerberosDebug, + ActionListener.wrap(userPrincipalNameOutToken -> { + if (userPrincipalNameOutToken.v1() != null) { + resolveUser(userPrincipalNameOutToken.v1(), userPrincipalNameOutToken.v2(), listener); + } else { + /** + * This is when security context could not be established may be due to ongoing + * negotiation and requires token to be sent back to peer for continuing + * further. We are terminating the authentication process as this is spengo + * negotiation and no other realm can handle this. We can have only one Kerberos + * realm in the system so terminating with RestStatus Unauthorized (401) and + * with 'WWW-Authenticate' header populated with value with token in the form + * 'Negotiate oYH1MIHyoAMK...' + */ + String errorMessage = "failed to authenticate user, gss context negotiation not complete"; + ElasticsearchSecurityException ese = unauthorized(errorMessage, null); + ese = unauthorizedWithOutputToken(ese, userPrincipalNameOutToken.v2()); + listener.onResponse(AuthenticationResult.terminate(errorMessage, ese)); + } + }, e -> handleException(e, listener)) + ); } private String[] splitUserPrincipalName(final String userPrincipalName) { @@ -183,12 +192,20 @@ private String[] splitUserPrincipalName(final String userPrincipalName) { private void handleException(Exception e, final ActionListener> listener) { if (e instanceof LoginException) { logger.debug("failed to authenticate user, service login failure", e); - listener.onResponse(AuthenticationResult.terminate("failed to authenticate user, service login failure", - unauthorized(e.getLocalizedMessage(), e))); + listener.onResponse( + AuthenticationResult.terminate( + "failed to authenticate user, service login failure", + unauthorized(e.getLocalizedMessage(), e) + ) + ); } else if (e instanceof GSSException) { logger.debug("failed to authenticate user, gss context negotiation failure", e); - listener.onResponse(AuthenticationResult.terminate("failed to authenticate user, gss context negotiation failure", - unauthorized(e.getLocalizedMessage(), e))); + listener.onResponse( + AuthenticationResult.terminate( + "failed to authenticate user, gss context negotiation failure", + unauthorized(e.getLocalizedMessage(), e) + ) + ); } else { logger.debug("failed to authenticate user", e); listener.onFailure(e); @@ -196,7 +213,9 @@ private void handleException(Exception e, final ActionListener> listener + final String userPrincipalName, + final String outToken, + final ActionListener> listener ) { // if outToken is present then it needs to be communicated with peer, add it to // response header in thread context. @@ -228,7 +247,9 @@ private void resolveUser( } private void buildUser( - final String username, final Map metadata, final ActionListener> listener + final String username, + final Map metadata, + final ActionListener> listener ) { final UserRoleMapper.UserData userData = new UserRoleMapper.UserData(username, null, Set.of(), metadata, this.config); userRoleMapper.resolveRoles(userData, ActionListener.wrap(roles -> { diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosTicketValidator.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosTicketValidator.java index 625d32dfffffa..c83e8f999339b 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosTicketValidator.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosTicketValidator.java @@ -18,12 +18,6 @@ import org.ietf.jgss.GSSManager; import org.ietf.jgss.Oid; -import javax.security.auth.Subject; -import javax.security.auth.login.AppConfigurationEntry; -import javax.security.auth.login.Configuration; -import javax.security.auth.login.LoginContext; -import javax.security.auth.login.LoginException; - import java.nio.file.Path; import java.security.AccessController; import java.security.PrivilegedActionException; @@ -32,6 +26,12 @@ import java.util.Collections; import java.util.Map; +import javax.security.auth.Subject; +import javax.security.auth.login.AppConfigurationEntry; +import javax.security.auth.login.Configuration; +import javax.security.auth.login.LoginContext; +import javax.security.auth.login.LoginException; + /** * Utility class that validates kerberos ticket for peer authentication. *

    @@ -80,8 +80,12 @@ private static Oid getOid(final String id) { * service. * @param krbDebug if {@code true} enables jaas krb5 login module debug logs. */ - public void validateTicket(final byte[] decodedToken, final Path keytabPath, final boolean krbDebug, - final ActionListener> actionListener) { + public void validateTicket( + final byte[] decodedToken, + final Path keytabPath, + final boolean krbDebug, + final ActionListener> actionListener + ) { final GSSManager gssManager = GSSManager.getInstance(); GSSContext gssContext = null; LoginContext loginContext = null; @@ -90,8 +94,12 @@ public void validateTicket(final byte[] decodedToken, final Path keytabPath, fin GSSCredential serviceCreds = createCredentials(gssManager, loginContext.getSubject()); gssContext = gssManager.createContext(serviceCreds); final String base64OutToken = encodeToString(acceptSecContext(decodedToken, gssContext, loginContext.getSubject())); - LOGGER.trace("validateTicket isGSSContextEstablished = {}, username = {}, outToken = {}", gssContext.isEstablished(), - gssContext.getSrcName().toString(), base64OutToken); + LOGGER.trace( + "validateTicket isGSSContextEstablished = {}, username = {}, outToken = {}", + gssContext.isEstablished(), + gssContext.getSrcName().toString(), + base64OutToken + ); actionListener.onResponse(new Tuple<>(gssContext.isEstablished() ? gssContext.getSrcName().toString() : null, base64OutToken)); } catch (GSSException e) { actionListener.onFailure(e); @@ -137,10 +145,12 @@ private String encodeToString(final byte[] outToken) { * @see GSSContext#acceptSecContext(byte[], int, int) */ private static byte[] acceptSecContext(final byte[] base64decodedTicket, final GSSContext gssContext, Subject subject) - throws PrivilegedActionException { + throws PrivilegedActionException { // process token with gss context - return doAsWrapper(subject, - (PrivilegedExceptionAction) () -> gssContext.acceptSecContext(base64decodedTicket, 0, base64decodedTicket.length)); + return doAsWrapper( + subject, + (PrivilegedExceptionAction) () -> gssContext.acceptSecContext(base64decodedTicket, 0, base64decodedTicket.length) + ); } /** @@ -152,8 +162,15 @@ private static byte[] acceptSecContext(final byte[] base64decodedTicket, final G * @throws PrivilegedActionException when privileged action threw exception */ private static GSSCredential createCredentials(final GSSManager gssManager, final Subject subject) throws PrivilegedActionException { - return doAsWrapper(subject, (PrivilegedExceptionAction) () -> gssManager.createCredential(null, - GSSCredential.DEFAULT_LIFETIME, SUPPORTED_OIDS, GSSCredential.ACCEPT_ONLY)); + return doAsWrapper( + subject, + (PrivilegedExceptionAction) () -> gssManager.createCredential( + null, + GSSCredential.DEFAULT_LIFETIME, + SUPPORTED_OIDS, + GSSCredential.ACCEPT_ONLY + ) + ); } /** @@ -253,18 +270,28 @@ static class KeytabJaasConf extends Configuration { @Override public AppConfigurationEntry[] getAppConfigurationEntry(final String name) { - return new AppConfigurationEntry[]{new AppConfigurationEntry( + return new AppConfigurationEntry[] { + new AppConfigurationEntry( SUN_KRB5_LOGIN_MODULE, AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, Map.of( - "keyTab", keytabFilePath, - // as acceptor, we can have multiple SPNs, we do not want to use any particular principal so it uses "*" - "principal", "*", - "useKeyTab", Boolean.TRUE.toString(), - "storeKey", Boolean.TRUE.toString(), - "doNotPrompt", Boolean.TRUE.toString(), - "isInitiator", Boolean.FALSE.toString(), - "debug", Boolean.toString(krbDebug)))}; + "keyTab", + keytabFilePath, + // as acceptor, we can have multiple SPNs, we do not want to use any particular principal so it uses "*" + "principal", + "*", + "useKeyTab", + Boolean.TRUE.toString(), + "storeKey", + Boolean.TRUE.toString(), + "doNotPrompt", + Boolean.TRUE.toString(), + "isInitiator", + Boolean.FALSE.toString(), + "debug", + Boolean.toString(krbDebug) + ) + ) }; } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/ActiveDirectoryGroupsResolver.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/ActiveDirectoryGroupsResolver.java index 4b8a6a9069856..a038583acfee2 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/ActiveDirectoryGroupsResolver.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/ActiveDirectoryGroupsResolver.java @@ -12,6 +12,7 @@ import com.unboundid.ldap.sdk.SearchRequest; import com.unboundid.ldap.sdk.SearchResultEntry; import com.unboundid.ldap.sdk.SearchScope; + import org.apache.logging.log4j.Logger; import org.elasticsearch.action.ActionListener; import org.elasticsearch.core.TimeValue; @@ -27,8 +28,8 @@ import java.util.stream.Collectors; import static org.elasticsearch.xpack.core.security.authc.ldap.support.SessionFactorySettings.IGNORE_REFERRAL_ERRORS_SETTING; -import static org.elasticsearch.xpack.security.authc.ldap.ActiveDirectorySIDUtil.convertToString; import static org.elasticsearch.xpack.security.authc.ldap.ActiveDirectorySIDUtil.TOKEN_GROUPS; +import static org.elasticsearch.xpack.security.authc.ldap.ActiveDirectorySIDUtil.convertToString; import static org.elasticsearch.xpack.security.authc.ldap.ActiveDirectorySessionFactory.buildDnFromDomain; import static org.elasticsearch.xpack.security.authc.ldap.support.LdapUtils.OBJECT_CLASS_PRESENCE_FILTER; import static org.elasticsearch.xpack.security.authc.ldap.support.LdapUtils.search; @@ -41,33 +42,43 @@ class ActiveDirectoryGroupsResolver implements GroupsResolver { private final boolean ignoreReferralErrors; ActiveDirectoryGroupsResolver(RealmConfig config) { - this.baseDn = config.getSetting(SearchGroupsResolverSettings.BASE_DN, - () -> buildDnFromDomain(config.getSetting(ActiveDirectorySessionFactorySettings.AD_DOMAIN_NAME_SETTING))); + this.baseDn = config.getSetting( + SearchGroupsResolverSettings.BASE_DN, + () -> buildDnFromDomain(config.getSetting(ActiveDirectorySessionFactorySettings.AD_DOMAIN_NAME_SETTING)) + ); this.scope = config.getSetting(SearchGroupsResolverSettings.SCOPE); this.ignoreReferralErrors = config.getSetting(IGNORE_REFERRAL_ERRORS_SETTING); } @Override - public void resolve(LDAPInterface connection, String userDn, TimeValue timeout, Logger logger, Collection attributes, - ActionListener> listener) { - buildGroupQuery(connection, userDn, timeout, - ignoreReferralErrors, ActionListener.wrap((filter) -> { - if (filter == null) { - listener.onResponse(List.of()); - } else { - logger.debug("group SID to DN [{}] search filter: [{}]", userDn, filter); - search(connection, baseDn, scope.scope(), filter, - Math.toIntExact(timeout.seconds()), ignoreReferralErrors, - ActionListener.wrap((results) -> { - List groups = results.stream() - .map(SearchResultEntry::getDN) - .collect(Collectors.toUnmodifiableList()); - listener.onResponse(groups); - }, - listener::onFailure), - SearchRequest.NO_ATTRIBUTES); - } - }, listener::onFailure)); + public void resolve( + LDAPInterface connection, + String userDn, + TimeValue timeout, + Logger logger, + Collection attributes, + ActionListener> listener + ) { + buildGroupQuery(connection, userDn, timeout, ignoreReferralErrors, ActionListener.wrap((filter) -> { + if (filter == null) { + listener.onResponse(List.of()); + } else { + logger.debug("group SID to DN [{}] search filter: [{}]", userDn, filter); + search( + connection, + baseDn, + scope.scope(), + filter, + Math.toIntExact(timeout.seconds()), + ignoreReferralErrors, + ActionListener.wrap((results) -> { + List groups = results.stream().map(SearchResultEntry::getDN).collect(Collectors.toUnmodifiableList()); + listener.onResponse(groups); + }, listener::onFailure), + SearchRequest.NO_ATTRIBUTES + ); + } + }, listener::onFailure)); } @Override @@ -76,22 +87,33 @@ public String[] attributes() { return null; } - static void buildGroupQuery(LDAPInterface connection, String userDn, TimeValue timeout, - boolean ignoreReferralErrors, ActionListener listener) { - searchForEntry(connection, userDn, SearchScope.BASE, OBJECT_CLASS_PRESENCE_FILTER, - Math.toIntExact(timeout.seconds()), ignoreReferralErrors, - ActionListener.wrap((entry) -> { - if (entry == null || entry.hasAttribute(TOKEN_GROUPS) == false) { - listener.onResponse(null); - } else { - final byte[][] tokenGroupSIDBytes = entry.getAttributeValueByteArrays(TOKEN_GROUPS); - List orFilters = Arrays.stream(tokenGroupSIDBytes) - .map((sidBytes) -> Filter.createEqualityFilter("objectSid", convertToString(sidBytes))) - .collect(Collectors.toList()); - listener.onResponse(Filter.createORFilter(orFilters)); - } - }, listener::onFailure), - TOKEN_GROUPS); + static void buildGroupQuery( + LDAPInterface connection, + String userDn, + TimeValue timeout, + boolean ignoreReferralErrors, + ActionListener listener + ) { + searchForEntry( + connection, + userDn, + SearchScope.BASE, + OBJECT_CLASS_PRESENCE_FILTER, + Math.toIntExact(timeout.seconds()), + ignoreReferralErrors, + ActionListener.wrap((entry) -> { + if (entry == null || entry.hasAttribute(TOKEN_GROUPS) == false) { + listener.onResponse(null); + } else { + final byte[][] tokenGroupSIDBytes = entry.getAttributeValueByteArrays(TOKEN_GROUPS); + List orFilters = Arrays.stream(tokenGroupSIDBytes) + .map((sidBytes) -> Filter.createEqualityFilter("objectSid", convertToString(sidBytes))) + .collect(Collectors.toList()); + listener.onResponse(Filter.createORFilter(orFilters)); + } + }, listener::onFailure), + TOKEN_GROUPS + ); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/ActiveDirectorySIDUtil.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/ActiveDirectorySIDUtil.java index 2eb618e94cb27..e3e406af775f0 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/ActiveDirectorySIDUtil.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/ActiveDirectorySIDUtil.java @@ -28,8 +28,8 @@ public class ActiveDirectorySIDUtil { public static final String TOKEN_GROUPS = "tokenGroups"; - public static String convertToString(byte[] bytes) - { + + public static String convertToString(byte[] bytes) { /* * The binary data structure, from http://msdn.microsoft.com/en-us/library/cc230371(PROT.10).aspx: * byte[0] - Revision (1 byte): An 8-bit unsigned integer that specifies the revision level of @@ -46,50 +46,46 @@ public static String convertToString(byte[] bytes) * by SubAuthorityCount. little-endian! */ - if ( ( bytes == null ) || ( bytes.length < 8 ) ) - { + if ((bytes == null) || (bytes.length < 8)) { throw new IllegalArgumentException("Invalid SID"); } - char[] hex = Hex.encodeHex( bytes ); + char[] hex = Hex.encodeHex(bytes); StringBuffer sb = new StringBuffer(); // start with 'S' - sb.append( 'S' ); + sb.append('S'); // revision - int revision = Integer.parseInt( new String( hex, 0, 2 ), 16 ); - sb.append( '-' ); - sb.append( revision ); + int revision = Integer.parseInt(new String(hex, 0, 2), 16); + sb.append('-'); + sb.append(revision); // get count - int count = Integer.parseInt( new String( hex, 2, 2 ), 16 ); + int count = Integer.parseInt(new String(hex, 2, 2), 16); // check length - if ( bytes.length != ( 8 + count * 4 ) ) - { + if (bytes.length != (8 + count * 4)) { throw new IllegalArgumentException("Invalid SID"); } // get authority, big-endian - long authority = Long.parseLong( new String( hex, 4, 12 ), 16 ); - sb.append( '-' ); - sb.append( authority ); + long authority = Long.parseLong(new String(hex, 4, 12), 16); + sb.append('-'); + sb.append(authority); // sub-authorities, little-endian - for ( int i = 0; i < count; i++ ) - { + for (int i = 0; i < count; i++) { StringBuffer rid = new StringBuffer(); - for ( int k = 3; k >= 0; k-- ) - { - rid.append( hex[16 + ( i * 8 ) + ( k * 2 )] ); - rid.append( hex[16 + ( i * 8 ) + ( k * 2 ) + 1] ); + for (int k = 3; k >= 0; k--) { + rid.append(hex[16 + (i * 8) + (k * 2)]); + rid.append(hex[16 + (i * 8) + (k * 2) + 1]); } - long subAuthority = Long.parseLong( rid.toString(), 16 ); - sb.append( '-' ); - sb.append( subAuthority ); + long subAuthority = Long.parseLong(rid.toString(), 16); + sb.append('-'); + sb.append(subAuthority); } return sb.toString(); diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/ActiveDirectorySessionFactory.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/ActiveDirectorySessionFactory.java index db4be15391d0f..f6e0b5f62d8ab 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/ActiveDirectorySessionFactory.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/ActiveDirectorySessionFactory.java @@ -67,19 +67,26 @@ class ActiveDirectorySessionFactory extends PoolingSessionFactory { final UpnADAuthenticator upnADAuthenticator; ActiveDirectorySessionFactory(RealmConfig config, SSLService sslService, ThreadPool threadPool) throws LDAPException { - super(config, sslService, new ActiveDirectoryGroupsResolver(config), - ActiveDirectorySessionFactorySettings.POOL_ENABLED, - config.hasSetting(PoolingSessionFactorySettings.BIND_DN) ? getBindDN(config) : null, - () -> { - if (config.hasSetting(PoolingSessionFactorySettings.BIND_DN)) { - final String healthCheckDn = config.getSetting(PoolingSessionFactorySettings.BIND_DN); - if (healthCheckDn.isEmpty() && healthCheckDn.indexOf('=') > 0) { - return healthCheckDn; - } + super( + config, + sslService, + new ActiveDirectoryGroupsResolver(config), + ActiveDirectorySessionFactorySettings.POOL_ENABLED, + config.hasSetting(PoolingSessionFactorySettings.BIND_DN) ? getBindDN(config) : null, + () -> { + if (config.hasSetting(PoolingSessionFactorySettings.BIND_DN)) { + final String healthCheckDn = config.getSetting(PoolingSessionFactorySettings.BIND_DN); + if (healthCheckDn.isEmpty() && healthCheckDn.indexOf('=') > 0) { + return healthCheckDn; } - return config.getSetting(ActiveDirectorySessionFactorySettings.AD_USER_SEARCH_BASEDN_SETTING, - () -> config.getSetting(ActiveDirectorySessionFactorySettings.AD_DOMAIN_NAME_SETTING)); - }, threadPool); + } + return config.getSetting( + ActiveDirectorySessionFactorySettings.AD_USER_SEARCH_BASEDN_SETTING, + () -> config.getSetting(ActiveDirectorySessionFactorySettings.AD_DOMAIN_NAME_SETTING) + ); + }, + threadPool + ); String domainName = config.getSetting(ActiveDirectorySessionFactorySettings.AD_DOMAIN_NAME_SETTING); String domainDN = buildDnFromDomain(domainName); final int ldapPort = config.getSetting(ActiveDirectorySessionFactorySettings.AD_LDAP_PORT_SETTING); @@ -87,19 +94,52 @@ class ActiveDirectorySessionFactory extends PoolingSessionFactory { final int gcLdapPort = config.getSetting(ActiveDirectorySessionFactorySettings.AD_GC_LDAP_PORT_SETTING); final int gcLdapsPort = config.getSetting(ActiveDirectorySessionFactorySettings.AD_GC_LDAPS_PORT_SETTING); - defaultADAuthenticator = new DefaultADAuthenticator(config, timeout, ignoreReferralErrors, logger, groupResolver, - metadataResolver, domainDN, threadPool); - downLevelADAuthenticator = new DownLevelADAuthenticator(config, timeout, ignoreReferralErrors, logger, groupResolver, - metadataResolver, domainDN, sslService, threadPool, ldapPort, ldapsPort, gcLdapPort, gcLdapsPort); - upnADAuthenticator = new UpnADAuthenticator(config, timeout, ignoreReferralErrors, logger, groupResolver, - metadataResolver, domainDN, threadPool); + defaultADAuthenticator = new DefaultADAuthenticator( + config, + timeout, + ignoreReferralErrors, + logger, + groupResolver, + metadataResolver, + domainDN, + threadPool + ); + downLevelADAuthenticator = new DownLevelADAuthenticator( + config, + timeout, + ignoreReferralErrors, + logger, + groupResolver, + metadataResolver, + domainDN, + sslService, + threadPool, + ldapPort, + ldapsPort, + gcLdapPort, + gcLdapsPort + ); + upnADAuthenticator = new UpnADAuthenticator( + config, + timeout, + ignoreReferralErrors, + logger, + groupResolver, + metadataResolver, + domainDN, + threadPool + ); } @Override protected List getDefaultLdapUrls(RealmConfig config) { - return Collections.singletonList("ldap://" + config.getSetting(ActiveDirectorySessionFactorySettings.AD_DOMAIN_NAME_SETTING) + - ":" + config.getSetting(ActiveDirectorySessionFactorySettings.AD_LDAP_PORT_SETTING)); + return Collections.singletonList( + "ldap://" + + config.getSetting(ActiveDirectorySessionFactorySettings.AD_DOMAIN_NAME_SETTING) + + ":" + + config.getSetting(ActiveDirectorySessionFactorySettings.AD_LDAP_PORT_SETTING) + ); } @Override @@ -157,19 +197,34 @@ public void onFailure(Exception e) { @Override protected void doRun() throws Exception { - getADAuthenticator(user).searchForDN(connection, user, null, Math.toIntExact(timeout.getSeconds()), - ActionListener.wrap(entry -> { - if (entry == null) { - IOUtils.close(connection); - listener.onResponse(null); - } else { - listener.onResponse(new LdapSession(logger, config, connection, entry.getDN(), groupResolver, - metadataResolver, timeout, null)); - } - }, e -> { - IOUtils.closeWhileHandlingException(connection); - listener.onFailure(e); - })); + getADAuthenticator(user).searchForDN( + connection, + user, + null, + Math.toIntExact(timeout.getSeconds()), + ActionListener.wrap(entry -> { + if (entry == null) { + IOUtils.close(connection); + listener.onResponse(null); + } else { + listener.onResponse( + new LdapSession( + logger, + config, + connection, + entry.getDN(), + groupResolver, + metadataResolver, + timeout, + null + ) + ); + } + }, e -> { + IOUtils.closeWhileHandlingException(connection); + listener.onFailure(e); + }) + ); } }); @@ -223,9 +278,18 @@ abstract static class ADAuthenticator { final SecureString bindPassword; final ThreadPool threadPool; - ADAuthenticator(RealmConfig realm, TimeValue timeout, boolean ignoreReferralErrors, Logger logger, GroupsResolver groupsResolver, - LdapMetadataResolver metadataResolver, String domainDN, Setting.AffixSetting userSearchFilterSetting, - String defaultUserSearchFilter, ThreadPool threadPool) { + ADAuthenticator( + RealmConfig realm, + TimeValue timeout, + boolean ignoreReferralErrors, + Logger logger, + GroupsResolver groupsResolver, + LdapMetadataResolver metadataResolver, + String domainDN, + Setting.AffixSetting userSearchFilterSetting, + String defaultUserSearchFilter, + ThreadPool threadPool + ) { this.realm = realm; this.timeout = timeout; this.ignoreReferralErrors = ignoreReferralErrors; @@ -233,19 +297,26 @@ abstract static class ADAuthenticator { this.groupsResolver = groupsResolver; this.metadataResolver = metadataResolver; this.bindDN = getBindDN(realm); - this.bindPassword = realm.getSetting(PoolingSessionFactorySettings.SECURE_BIND_PASSWORD, - () -> realm.getSetting(PoolingSessionFactorySettings.LEGACY_BIND_PASSWORD)); + this.bindPassword = realm.getSetting( + PoolingSessionFactorySettings.SECURE_BIND_PASSWORD, + () -> realm.getSetting(PoolingSessionFactorySettings.LEGACY_BIND_PASSWORD) + ); this.threadPool = threadPool; userSearchDN = realm.getSetting(ActiveDirectorySessionFactorySettings.AD_USER_SEARCH_BASEDN_SETTING, () -> domainDN); - userSearchScope = LdapSearchScope.resolve(realm.getSetting(ActiveDirectorySessionFactorySettings.AD_USER_SEARCH_SCOPE_SETTING), - LdapSearchScope.SUB_TREE); + userSearchScope = LdapSearchScope.resolve( + realm.getSetting(ActiveDirectorySessionFactorySettings.AD_USER_SEARCH_SCOPE_SETTING), + LdapSearchScope.SUB_TREE + ); userSearchFilter = realm.getSetting(userSearchFilterSetting, () -> defaultUserSearchFilter); } final void authenticate(LDAPConnection connection, String username, SecureString password, ActionListener listener) { final byte[] passwordBytes = CharArrays.toUtf8Bytes(password.getChars()); - final SimpleBindRequest userBind = new SimpleBindRequest(bindUsername(username), passwordBytes, - new AuthorizationIdentityRequestControl()); + final SimpleBindRequest userBind = new SimpleBindRequest( + bindUsername(username), + passwordBytes, + new AuthorizationIdentityRequestControl() + ); LdapUtils.maybeForkThenBind(connection, userBind, false, threadPool, new ActionRunnable(listener) { @Override protected void doRun() throws Exception { @@ -255,15 +326,26 @@ protected void doRun() throws Exception { searchForDN(connection, username, password, Math.toIntExact(timeout.seconds()), ActionListener.wrap((entry) -> { if (entry == null) { // we did not find the user, cannot authenticate in this realm - listener.onFailure(new ElasticsearchSecurityException( - "search for user [" + username + "] by principal name yielded no results")); + listener.onFailure( + new ElasticsearchSecurityException( + "search for user [" + username + "] by principal name yielded no results" + ) + ); } else { - listener.onResponse(new LdapSession(logger, realm, connection, entry.getDN(), groupsResolver, - metadataResolver, timeout, null)); + listener.onResponse( + new LdapSession( + logger, + realm, + connection, + entry.getDN(), + groupsResolver, + metadataResolver, + timeout, + null + ) + ); } - }, e -> { - listener.onFailure(e); - })); + }, e -> { listener.onFailure(e); })); } }; if (bindDN.isEmpty()) { @@ -276,8 +358,13 @@ protected void doRun() throws Exception { }); } - final void authenticate(LDAPConnectionPool pool, String username, SecureString password, ThreadPool threadPool, - ActionListener listener) { + final void authenticate( + LDAPConnectionPool pool, + String username, + SecureString password, + ThreadPool threadPool, + ActionListener listener + ) { final byte[] passwordBytes = CharArrays.toUtf8Bytes(password.getChars()); final SimpleBindRequest bind = new SimpleBindRequest(bindUsername(username), passwordBytes); LdapUtils.maybeForkThenBindAndRevert(pool, bind, threadPool, new ActionRunnable(listener) { @@ -286,15 +373,17 @@ protected void doRun() throws Exception { searchForDN(pool, username, password, Math.toIntExact(timeout.seconds()), ActionListener.wrap((entry) -> { if (entry == null) { // we did not find the user, cannot authenticate in this realm - listener.onFailure(new ElasticsearchSecurityException( - "search for user [" + username + "] by principal name yielded no results")); + listener.onFailure( + new ElasticsearchSecurityException( + "search for user [" + username + "] by principal name yielded no results" + ) + ); } else { listener.onResponse( - new LdapSession(logger, realm, pool, entry.getDN(), groupsResolver, metadataResolver, timeout, null)); + new LdapSession(logger, realm, pool, entry.getDN(), groupsResolver, metadataResolver, timeout, null) + ); } - }, e -> { - listener.onFailure(e); - })); + }, e -> { listener.onFailure(e); })); } }); } @@ -308,8 +397,13 @@ final String getUserSearchFilter() { return userSearchFilter; } - abstract void searchForDN(LDAPInterface connection, String username, SecureString password, int timeLimitSeconds, - ActionListener listener); + abstract void searchForDN( + LDAPInterface connection, + String username, + SecureString password, + int timeLimitSeconds, + ActionListener listener + ); } /** @@ -321,12 +415,28 @@ static class DefaultADAuthenticator extends ADAuthenticator { final String domainName; - DefaultADAuthenticator(RealmConfig realm, TimeValue timeout, boolean ignoreReferralErrors, Logger logger, - GroupsResolver groupsResolver, LdapMetadataResolver metadataResolver, String domainDN, - ThreadPool threadPool) { - super(realm, timeout, ignoreReferralErrors, logger, groupsResolver, metadataResolver, domainDN, - ActiveDirectorySessionFactorySettings.AD_USER_SEARCH_FILTER_SETTING, - "(&(objectClass=user)(|(sAMAccountName={0})(userPrincipalName={0}@" + domainName(realm) + ")))", threadPool); + DefaultADAuthenticator( + RealmConfig realm, + TimeValue timeout, + boolean ignoreReferralErrors, + Logger logger, + GroupsResolver groupsResolver, + LdapMetadataResolver metadataResolver, + String domainDN, + ThreadPool threadPool + ) { + super( + realm, + timeout, + ignoreReferralErrors, + logger, + groupsResolver, + metadataResolver, + domainDN, + ActiveDirectorySessionFactorySettings.AD_USER_SEARCH_FILTER_SETTING, + "(&(objectClass=user)(|(sAMAccountName={0})(userPrincipalName={0}@" + domainName(realm) + ")))", + threadPool + ); domainName = domainName(realm); } @@ -335,13 +445,24 @@ private static String domainName(RealmConfig realm) { } @Override - void searchForDN(LDAPInterface connection, String username, SecureString password, - int timeLimitSeconds, ActionListener listener) { + void searchForDN( + LDAPInterface connection, + String username, + SecureString password, + int timeLimitSeconds, + ActionListener listener + ) { try { - searchForEntry(connection, userSearchDN, userSearchScope.scope(), - createFilter(userSearchFilter, username), timeLimitSeconds, - ignoreReferralErrors, listener, - attributesToSearchFor(groupsResolver.attributes())); + searchForEntry( + connection, + userSearchDN, + userSearchScope.scope(), + createFilter(userSearchFilter, username), + timeLimitSeconds, + ignoreReferralErrors, + listener, + attributesToSearchFor(groupsResolver.attributes()) + ); } catch (LDAPException e) { listener.onFailure(e); } @@ -369,12 +490,33 @@ static class DownLevelADAuthenticator extends ADAuthenticator { private final int gcLdapPort; private final int gcLdapsPort; - DownLevelADAuthenticator(RealmConfig config, TimeValue timeout, boolean ignoreReferralErrors, Logger logger, - GroupsResolver groupsResolver, LdapMetadataResolver metadataResolver, String domainDN, - SSLService sslService, ThreadPool threadPool, - int ldapPort, int ldapsPort, int gcLdapPort, int gcLdapsPort) { - super(config, timeout, ignoreReferralErrors, logger, groupsResolver, metadataResolver, domainDN, - ActiveDirectorySessionFactorySettings.AD_DOWN_LEVEL_USER_SEARCH_FILTER_SETTING, DOWN_LEVEL_FILTER, threadPool); + DownLevelADAuthenticator( + RealmConfig config, + TimeValue timeout, + boolean ignoreReferralErrors, + Logger logger, + GroupsResolver groupsResolver, + LdapMetadataResolver metadataResolver, + String domainDN, + SSLService sslService, + ThreadPool threadPool, + int ldapPort, + int ldapsPort, + int gcLdapPort, + int gcLdapsPort + ) { + super( + config, + timeout, + ignoreReferralErrors, + logger, + groupsResolver, + metadataResolver, + domainDN, + ActiveDirectorySessionFactorySettings.AD_DOWN_LEVEL_USER_SEARCH_FILTER_SETTING, + DOWN_LEVEL_FILTER, + threadPool + ); this.domainDN = domainDN; this.sslService = sslService; this.config = config; @@ -385,8 +527,13 @@ static class DownLevelADAuthenticator extends ADAuthenticator { } @Override - void searchForDN(LDAPInterface connection, String username, SecureString password, int timeLimitSeconds, - ActionListener listener) { + void searchForDN( + LDAPInterface connection, + String username, + SecureString password, + int timeLimitSeconds, + ActionListener listener + ) { String[] parts = username.split("\\\\"); assert parts.length == 2; final String netBiosDomainName = parts[0]; @@ -396,14 +543,28 @@ void searchForDN(LDAPInterface connection, String username, SecureString passwor if (domainDN == null) { listener.onResponse(null); } else { - searchForEntry(connection, domainDN, LdapSearchScope.SUB_TREE.scope(), createFilter(userSearchFilter, accountName), - timeLimitSeconds, ignoreReferralErrors, listener, attributesToSearchFor(groupsResolver.attributes())); + searchForEntry( + connection, + domainDN, + LdapSearchScope.SUB_TREE.scope(), + createFilter(userSearchFilter, accountName), + timeLimitSeconds, + ignoreReferralErrors, + listener, + attributesToSearchFor(groupsResolver.attributes()) + ); } }, listener::onFailure)); } - void netBiosDomainNameToDn(LDAPInterface ldapInterface, String netBiosDomainName, String username, SecureString password, - int timeLimitSeconds, ActionListener listener) { + void netBiosDomainNameToDn( + LDAPInterface ldapInterface, + String netBiosDomainName, + String username, + SecureString password, + int timeLimitSeconds, + ActionListener listener + ) { LDAPConnection ldapConnection = null; try { final Filter filter = createFilter(NETBIOS_NAME_FILTER_TEMPLATE, netBiosDomainName); @@ -411,11 +572,19 @@ void netBiosDomainNameToDn(LDAPInterface ldapInterface, String netBiosDomainName if (cachedName != null) { listener.onResponse(cachedName); } else if (usingGlobalCatalog(ldapInterface) == false) { - search(ldapInterface, "CN=Configuration," + domainDN, LdapSearchScope.SUB_TREE.scope(), filter, timeLimitSeconds, - ignoreReferralErrors, - ActionListener.wrap((results) -> handleSearchResults(results, netBiosDomainName, domainNameCache, listener), - listener::onFailure), - "ncname"); + search( + ldapInterface, + "CN=Configuration," + domainDN, + LdapSearchScope.SUB_TREE.scope(), + filter, + timeLimitSeconds, + ignoreReferralErrors, + ActionListener.wrap( + (results) -> handleSearchResults(results, netBiosDomainName, domainNameCache, listener), + listener::onFailure + ), + "ncname" + ); } else { // the global catalog does not replicate the necessary information to map a // netbios dns name to a DN so we need to instead connect to the normal ports. @@ -428,14 +597,18 @@ void netBiosDomainNameToDn(LDAPInterface ldapInterface, String netBiosDomainName } final LDAPConnection finalLdapConnection = ldapConnection; final LDAPConnection searchConnection = LdapUtils.privilegedConnect( - () -> new LDAPConnection(finalLdapConnection.getSocketFactory(), connectionOptions(config, sslService, logger), - finalLdapConnection.getConnectedAddress(), - finalLdapConnection.getSSLSession() != null ? ldapsPort : ldapPort)); + () -> new LDAPConnection( + finalLdapConnection.getSocketFactory(), + connectionOptions(config, sslService, logger), + finalLdapConnection.getConnectedAddress(), + finalLdapConnection.getSSLSession() != null ? ldapsPort : ldapPort + ) + ); final byte[] passwordBytes = CharArrays.toUtf8Bytes(password.getChars()); final boolean bindAsAuthenticatingUser = this.bindDN.isEmpty(); final SimpleBindRequest bind = bindAsAuthenticatingUser - ? new SimpleBindRequest(username, passwordBytes) - : new SimpleBindRequest(bindDN, CharArrays.toUtf8Bytes(bindPassword.getChars())); + ? new SimpleBindRequest(username, passwordBytes) + : new SimpleBindRequest(bindDN, CharArrays.toUtf8Bytes(bindPassword.getChars())); ActionRunnable body = new ActionRunnable<>(listener) { @Override protected void doRun() throws Exception { @@ -474,12 +647,13 @@ public void onFailure(Exception e) { } } - static void handleSearchResults(List results, String netBiosDomainName, - Cache domainNameCache, - ActionListener listener) { - Optional entry = results.stream() - .filter((r) -> r.hasAttribute("ncname")) - .findFirst(); + static void handleSearchResults( + List results, + String netBiosDomainName, + Cache domainNameCache, + ActionListener listener + ) { + Optional entry = results.stream().filter((r) -> r.hasAttribute("ncname")).findFirst(); if (entry.isPresent()) { final String value = entry.get().getAttributeValue("ncname"); try { @@ -523,29 +697,62 @@ static class UpnADAuthenticator extends ADAuthenticator { static final String UPN_USER_FILTER = "(&(objectClass=user)(userPrincipalName={1}))"; private final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(logger.getName()); - UpnADAuthenticator(RealmConfig config, TimeValue timeout, boolean ignoreReferralErrors, Logger logger, - GroupsResolver groupsResolver, LdapMetadataResolver metadataResolver, String domainDN, ThreadPool threadPool) { - super(config, timeout, ignoreReferralErrors, logger, groupsResolver, metadataResolver, domainDN, - ActiveDirectorySessionFactorySettings.AD_UPN_USER_SEARCH_FILTER_SETTING, UPN_USER_FILTER, threadPool); + UpnADAuthenticator( + RealmConfig config, + TimeValue timeout, + boolean ignoreReferralErrors, + Logger logger, + GroupsResolver groupsResolver, + LdapMetadataResolver metadataResolver, + String domainDN, + ThreadPool threadPool + ) { + super( + config, + timeout, + ignoreReferralErrors, + logger, + groupsResolver, + metadataResolver, + domainDN, + ActiveDirectorySessionFactorySettings.AD_UPN_USER_SEARCH_FILTER_SETTING, + UPN_USER_FILTER, + threadPool + ); if (userSearchFilter.contains("{0}")) { - deprecationLogger.critical(DeprecationCategory.SECURITY, "ldap_settings", + deprecationLogger.critical( + DeprecationCategory.SECURITY, + "ldap_settings", "The use of the account name variable {0} in the setting [" - + RealmSettings.getFullSettingKey(config, ActiveDirectorySessionFactorySettings.AD_UPN_USER_SEARCH_FILTER_SETTING) - + "] has been deprecated and will be removed in a future version!"); + + RealmSettings.getFullSettingKey(config, ActiveDirectorySessionFactorySettings.AD_UPN_USER_SEARCH_FILTER_SETTING) + + "] has been deprecated and will be removed in a future version!" + ); } } @Override - void searchForDN(LDAPInterface connection, String username, SecureString password, int timeLimitSeconds, - ActionListener listener) { + void searchForDN( + LDAPInterface connection, + String username, + SecureString password, + int timeLimitSeconds, + ActionListener listener + ) { String[] parts = username.split("@"); assert parts.length == 2 : "there should have only been two values for " + username + " after splitting on '@'"; final String accountName = parts[0]; try { Filter filter = createFilter(userSearchFilter, accountName, username); - searchForEntry(connection, userSearchDN, LdapSearchScope.SUB_TREE.scope(), filter, - timeLimitSeconds, ignoreReferralErrors, listener, - attributesToSearchFor(groupsResolver.attributes())); + searchForEntry( + connection, + userSearchDN, + LdapSearchScope.SUB_TREE.scope(), + filter, + timeLimitSeconds, + ignoreReferralErrors, + listener, + attributesToSearchFor(groupsResolver.attributes()) + ); } catch (LDAPException e) { listener.onFailure(e); } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/LdapRealm.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/LdapRealm.java index ec9a81a954fb5..bdd8ad35cf9b3 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/LdapRealm.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/LdapRealm.java @@ -7,15 +7,16 @@ package org.elasticsearch.xpack.security.authc.ldap; import com.unboundid.ldap.sdk.LDAPException; + import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.message.ParameterizedMessage; import org.elasticsearch.ElasticsearchTimeoutException; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.support.ContextPreservingActionListener; import org.elasticsearch.common.collect.MapBuilder; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.AbstractRunnable; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.threadpool.ThreadPool; @@ -28,6 +29,8 @@ import org.elasticsearch.xpack.core.security.authc.ldap.LdapRealmSettings; import org.elasticsearch.xpack.core.security.authc.ldap.LdapSessionFactorySettings; import org.elasticsearch.xpack.core.security.authc.ldap.LdapUserSearchSessionFactorySettings; +import org.elasticsearch.xpack.core.security.authc.support.UserRoleMapper; +import org.elasticsearch.xpack.core.security.authc.support.UserRoleMapper.UserData; import org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken; import org.elasticsearch.xpack.core.security.user.User; import org.elasticsearch.xpack.core.ssl.SSLService; @@ -36,8 +39,6 @@ import org.elasticsearch.xpack.security.authc.ldap.support.SessionFactory; import org.elasticsearch.xpack.security.authc.support.CachingUsernamePasswordRealm; import org.elasticsearch.xpack.security.authc.support.DelegatedAuthorizationSupport; -import org.elasticsearch.xpack.core.security.authc.support.UserRoleMapper; -import org.elasticsearch.xpack.core.security.authc.support.UserRoleMapper.UserData; import org.elasticsearch.xpack.security.authc.support.mapper.CompositeRoleMapper; import org.elasticsearch.xpack.security.authc.support.mapper.NativeRoleMappingStore; @@ -48,7 +49,6 @@ import java.util.function.Function; import java.util.function.Supplier; - /** * Authenticates username/password tokens against ldap, locates groups and maps them to roles. */ @@ -61,18 +61,23 @@ public final class LdapRealm extends CachingUsernamePasswordRealm { private DelegatedAuthorizationSupport delegatedRealms; - public LdapRealm(RealmConfig config, SSLService sslService, - ResourceWatcherService watcherService, - NativeRoleMappingStore nativeRoleMappingStore, ThreadPool threadPool) - throws LDAPException { - this(config, sessionFactory(config, sslService, threadPool), - new CompositeRoleMapper(config, watcherService, nativeRoleMappingStore), - threadPool); + public LdapRealm( + RealmConfig config, + SSLService sslService, + ResourceWatcherService watcherService, + NativeRoleMappingStore nativeRoleMappingStore, + ThreadPool threadPool + ) throws LDAPException { + this( + config, + sessionFactory(config, sslService, threadPool), + new CompositeRoleMapper(config, watcherService, nativeRoleMappingStore), + threadPool + ); } // pkg private for testing - LdapRealm(RealmConfig config, SessionFactory sessionFactory, - UserRoleMapper roleMapper, ThreadPool threadPool) { + LdapRealm(RealmConfig config, SessionFactory sessionFactory, UserRoleMapper roleMapper, ThreadPool threadPool) { super(config, threadPool); this.sessionFactory = sessionFactory; this.roleMapper = roleMapper; @@ -81,36 +86,45 @@ public LdapRealm(RealmConfig config, SSLService sslService, roleMapper.refreshRealmOnChange(this); } - static SessionFactory sessionFactory(RealmConfig config, SSLService sslService, ThreadPool threadPool) - throws LDAPException { + static SessionFactory sessionFactory(RealmConfig config, SSLService sslService, ThreadPool threadPool) throws LDAPException { final SessionFactory sessionFactory; if (LdapRealmSettings.AD_TYPE.equals(config.type())) { sessionFactory = new ActiveDirectorySessionFactory(config, sslService, threadPool); } else { - assert LdapRealmSettings.LDAP_TYPE.equals(config.type()) : "type [" + config.type() + "] is unknown. expected one of [" - + LdapRealmSettings.AD_TYPE + ", " + LdapRealmSettings.LDAP_TYPE + "]"; + assert LdapRealmSettings.LDAP_TYPE.equals(config.type()) + : "type [" + + config.type() + + "] is unknown. expected one of [" + + LdapRealmSettings.AD_TYPE + + ", " + + LdapRealmSettings.LDAP_TYPE + + "]"; final boolean hasSearchSettings = LdapUserSearchSessionFactory.hasUserSearchSettings(config); final boolean hasTemplates = config.hasSetting(LdapSessionFactorySettings.USER_DN_TEMPLATES_SETTING); if (hasSearchSettings == false) { if (hasTemplates == false) { - throw new IllegalArgumentException("settings were not found for either user search [" + - RealmSettings.getFullSettingKey(config, LdapUserSearchSessionFactorySettings.SEARCH_BASE_DN) + - "] or user template [" + - RealmSettings.getFullSettingKey(config, LdapSessionFactorySettings.USER_DN_TEMPLATES_SETTING) + - "] modes of operation. " + - "Please provide the settings for the mode you wish to use. For more details refer to the ldap " + - "authentication section of the X-Pack guide."); + throw new IllegalArgumentException( + "settings were not found for either user search [" + + RealmSettings.getFullSettingKey(config, LdapUserSearchSessionFactorySettings.SEARCH_BASE_DN) + + "] or user template [" + + RealmSettings.getFullSettingKey(config, LdapSessionFactorySettings.USER_DN_TEMPLATES_SETTING) + + "] modes of operation. " + + "Please provide the settings for the mode you wish to use. For more details refer to the ldap " + + "authentication section of the X-Pack guide." + ); } sessionFactory = new LdapSessionFactory(config, sslService, threadPool); } else if (hasTemplates) { - throw new IllegalArgumentException("settings were found for both user search [" + - RealmSettings.getFullSettingKey(config, LdapUserSearchSessionFactorySettings.SEARCH_BASE_DN) + - "] and user template [" + - RealmSettings.getFullSettingKey(config, LdapSessionFactorySettings.USER_DN_TEMPLATES_SETTING) + - "] modes of operation. " + - "Please remove the settings for the mode you do not wish to use. For more details refer to the ldap " + - "authentication section of the X-Pack guide."); + throw new IllegalArgumentException( + "settings were found for both user search [" + + RealmSettings.getFullSettingKey(config, LdapUserSearchSessionFactorySettings.SEARCH_BASE_DN) + + "] and user template [" + + RealmSettings.getFullSettingKey(config, LdapSessionFactorySettings.USER_DN_TEMPLATES_SETTING) + + "] modes of operation. " + + "Please remove the settings for the mode you do not wish to use. For more details refer to the ldap " + + "authentication section of the X-Pack guide." + ); } else { sessionFactory = new LdapUserSearchSessionFactory(config, sslService, threadPool); } @@ -127,10 +141,15 @@ protected void doAuthenticate(UsernamePasswordToken token, ActionListener> cancellableLdapRunnable = new CancellableLdapRunnable<>(listener, - ex -> AuthenticationResult.unsuccessful("Authentication against realm [" + this.toString() + "] failed", ex), - () -> sessionFactory.session(token.principal(), token.credentials(), - contextPreservingListener(new LdapSessionActionListener("authenticate", token.principal(), listener))), logger + final CancellableLdapRunnable> cancellableLdapRunnable = new CancellableLdapRunnable<>( + listener, + ex -> AuthenticationResult.unsuccessful("Authentication against realm [" + this.toString() + "] failed", ex), + () -> sessionFactory.session( + token.principal(), + token.credentials(), + contextPreservingListener(new LdapSessionActionListener("authenticate", token.principal(), listener)) + ), + logger ); threadPool.generic().execute(cancellableLdapRunnable); threadPool.schedule(cancellableLdapRunnable::maybeTimeout, executionTimeout, Names.SAME); @@ -142,11 +161,18 @@ protected void doLookupUser(String username, ActionListener userActionList // we submit to the threadpool because authentication using LDAP will execute blocking I/O for a bind request and we don't want // network threads stuck waiting for a socket to connect. After the bind, then all interaction with LDAP should be async final ActionListener> sessionListener = ActionListener.wrap( - result -> userActionListener.onResponse(result.getValue()), - userActionListener::onFailure); - final CancellableLdapRunnable cancellableLdapRunnable = new CancellableLdapRunnable<>(userActionListener, e -> null, - () -> sessionFactory.unauthenticatedSession(username, - contextPreservingListener(new LdapSessionActionListener("lookup", username, sessionListener))), logger); + result -> userActionListener.onResponse(result.getValue()), + userActionListener::onFailure + ); + final CancellableLdapRunnable cancellableLdapRunnable = new CancellableLdapRunnable<>( + userActionListener, + e -> null, + () -> sessionFactory.unauthenticatedSession( + username, + contextPreservingListener(new LdapSessionActionListener("lookup", username, sessionListener)) + ), + logger + ); threadPool.generic().execute(cancellableLdapRunnable); threadPool.schedule(cancellableLdapRunnable::maybeTimeout, executionTimeout, Names.SAME); } else { @@ -162,8 +188,7 @@ protected void doLookupUser(String username, ActionListener userActionList */ private ContextPreservingActionListener contextPreservingListener(LdapSessionActionListener sessionListener) { final Supplier toRestore = config.threadContext().newRestorableContext(false); - return new ContextPreservingActionListener<>(toRestore, - sessionListener); + return new ContextPreservingActionListener<>(toRestore, sessionListener); } @Override @@ -185,8 +210,13 @@ public void usageStats(ActionListener> listener) { }, listener::onFailure)); } - private static void buildUser(LdapSession session, String username, ActionListener> listener, - UserRoleMapper roleMapper, DelegatedAuthorizationSupport delegatedAuthz) { + private static void buildUser( + LdapSession session, + String username, + ActionListener> listener, + UserRoleMapper roleMapper, + DelegatedAuthorizationSupport delegatedAuthz + ) { assert delegatedAuthz != null : "DelegatedAuthorizationSupport is null"; if (session == null) { listener.onResponse(AuthenticationResult.notHandled()); @@ -206,8 +236,12 @@ protected void handleCachedAuthentication(User user, ActionListener> listener) { + private static void lookupUserFromSession( + String username, + LdapSession session, + UserRoleMapper roleMapper, + ActionListener> listener + ) { boolean loadingGroups = false; try { final Consumer onFailure = e -> { @@ -221,15 +255,11 @@ private static void lookupUserFromSession(String username, LdapSession session, .putAll(ldapData.metadata) .map(); final UserData user = new UserData(username, session.userDn(), ldapData.groups, metadata, session.realm()); - roleMapper.resolveRoles(user, ActionListener.wrap( - roles -> { - IOUtils.close(session); - String[] rolesArray = roles.toArray(new String[roles.size()]); - listener.onResponse(AuthenticationResult.success( - new User(username, rolesArray, null, null, metadata, true)) - ); - }, onFailure - )); + roleMapper.resolveRoles(user, ActionListener.wrap(roles -> { + IOUtils.close(session); + String[] rolesArray = roles.toArray(new String[roles.size()]); + listener.onResponse(AuthenticationResult.success(new User(username, rolesArray, null, null, metadata, true))); + }, onFailure)); }, onFailure)); loadingGroups = true; } finally { @@ -239,7 +269,6 @@ private static void lookupUserFromSession(String username, LdapSession session, } } - /** * A special {@link ActionListener} that encapsulates the handling of a LdapSession, which is used to return a user. This class handles * cases where the session is null or where an exception may be caught after a session has been established, which requires the @@ -328,10 +357,8 @@ public void onRejection(Exception e) { */ void maybeTimeout() { if (state.compareAndSet(LdapRunnableState.AWAITING_EXECUTION, LdapRunnableState.TIMED_OUT)) { - logger.warn("skipping execution of ldap runnable as it has been waiting for " + - "execution too long"); - listener.onFailure(new ElasticsearchTimeoutException("timed out waiting for " + - "execution of ldap runnable")); + logger.warn("skipping execution of ldap runnable as it has been waiting for " + "execution too long"); + listener.onFailure(new ElasticsearchTimeoutException("timed out waiting for " + "execution of ldap runnable")); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/LdapSessionFactory.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/LdapSessionFactory.java index 82997f18d07e3..682f91c0bb6c0 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/LdapSessionFactory.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/LdapSessionFactory.java @@ -47,8 +47,11 @@ public LdapSessionFactory(RealmConfig config, SSLService sslService, ThreadPool super(config, sslService, threadPool); userDnTemplates = config.getSetting(LdapSessionFactorySettings.USER_DN_TEMPLATES_SETTING).toArray(Strings.EMPTY_ARRAY); if (userDnTemplates.length == 0) { - throw new IllegalArgumentException("missing required LDAP setting [" - + RealmSettings.getFullSettingKey(config, LdapSessionFactorySettings.USER_DN_TEMPLATES_SETTING) + "]"); + throw new IllegalArgumentException( + "missing required LDAP setting [" + + RealmSettings.getFullSettingKey(config, LdapSessionFactorySettings.USER_DN_TEMPLATES_SETTING) + + "]" + ); } logger.info("Realm [{}] is in user-dn-template mode: [{}]", config.name(), userDnTemplates); groupResolver = groupResolver(config); @@ -72,8 +75,17 @@ public void session(String username, SecureString password, ActionListener null), - () -> config.getSetting(BIND_DN, () -> config.getSetting(LdapUserSearchSessionFactorySettings.SEARCH_BASE_DN)), - threadPool); - userSearchBaseDn = config.getSetting(LdapUserSearchSessionFactorySettings.SEARCH_BASE_DN, - () -> { - throw new IllegalArgumentException("[" + RealmSettings.getFullSettingKey(config, - LdapUserSearchSessionFactorySettings.SEARCH_BASE_DN) + "] must be specified"); - } + super( + config, + sslService, + groupResolver(config), + LdapUserSearchSessionFactorySettings.POOL_ENABLED, + config.getSetting(BIND_DN, () -> null), + () -> config.getSetting(BIND_DN, () -> config.getSetting(LdapUserSearchSessionFactorySettings.SEARCH_BASE_DN)), + threadPool ); + userSearchBaseDn = config.getSetting(LdapUserSearchSessionFactorySettings.SEARCH_BASE_DN, () -> { + throw new IllegalArgumentException( + "[" + RealmSettings.getFullSettingKey(config, LdapUserSearchSessionFactorySettings.SEARCH_BASE_DN) + "] must be specified" + ); + }); scope = config.getSetting(LdapUserSearchSessionFactorySettings.SEARCH_SCOPE); searchFilter = getSearchFilter(config); - logger.info("Realm [{}] is in user-search mode - base_dn=[{}], search filter=[{}]", - config.name(), userSearchBaseDn, searchFilter); + logger.info("Realm [{}] is in user-search mode - base_dn=[{}], search filter=[{}]", config.name(), userSearchBaseDn, searchFilter); } static boolean hasUserSearchSettings(RealmConfig config) { return Stream.of( - LdapUserSearchSessionFactorySettings.SEARCH_BASE_DN, - LdapUserSearchSessionFactorySettings.SEARCH_ATTRIBUTE, - LdapUserSearchSessionFactorySettings.SEARCH_SCOPE, - LdapUserSearchSessionFactorySettings.SEARCH_FILTER, - LdapUserSearchSessionFactorySettings.POOL_ENABLED + LdapUserSearchSessionFactorySettings.SEARCH_BASE_DN, + LdapUserSearchSessionFactorySettings.SEARCH_ATTRIBUTE, + LdapUserSearchSessionFactorySettings.SEARCH_SCOPE, + LdapUserSearchSessionFactorySettings.SEARCH_FILTER, + LdapUserSearchSessionFactorySettings.POOL_ENABLED ).anyMatch(config::hasSetting); } @@ -85,8 +88,24 @@ void getSessionWithPool(LDAPConnectionPool connectionPool, String user, SecureSt final String dn = entry.getDN(); final byte[] passwordBytes = CharArrays.toUtf8Bytes(password.getChars()); final SimpleBindRequest bind = new SimpleBindRequest(dn, passwordBytes); - LdapUtils.maybeForkThenBindAndRevert(connectionPool, bind, threadPool, ActionRunnable.supply(listener, () -> - new LdapSession(logger, config, connectionPool, dn, groupResolver, metadataResolver, timeout, entry.getAttributes()))); + LdapUtils.maybeForkThenBindAndRevert( + connectionPool, + bind, + threadPool, + ActionRunnable.supply( + listener, + () -> new LdapSession( + logger, + config, + connectionPool, + dn, + groupResolver, + metadataResolver, + timeout, + entry.getAttributes() + ) + ) + ); } }, listener::onFailure)); } @@ -125,8 +144,18 @@ protected void doRun() throws Exception { @Override protected void doRun() throws Exception { - listener.onResponse(new LdapSession(logger, config, connection, dn, groupResolver, - metadataResolver, timeout, entry.getAttributes())); + listener.onResponse( + new LdapSession( + logger, + config, + connection, + dn, + groupResolver, + metadataResolver, + timeout, + entry.getAttributes() + ) + ); } @Override @@ -173,8 +202,16 @@ void getUnauthenticatedSessionWithPool(LDAPConnectionPool connectionPool, String listener.onResponse(null); } else { final String dn = entry.getDN(); - LdapSession session = new LdapSession(logger, config, connectionPool, dn, groupResolver, metadataResolver, timeout, - entry.getAttributes()); + LdapSession session = new LdapSession( + logger, + config, + connectionPool, + dn, + groupResolver, + metadataResolver, + timeout, + entry.getAttributes() + ); listener.onResponse(session); } }, listener::onFailure)); @@ -192,8 +229,18 @@ protected void doRun() throws Exception { IOUtils.close(connection); listener.onResponse(null); } else { - listener.onResponse(new LdapSession(logger, config, connection, entry.getDN(), groupResolver, metadataResolver, - timeout, entry.getAttributes())); + listener.onResponse( + new LdapSession( + logger, + config, + connection, + entry.getDN(), + groupResolver, + metadataResolver, + timeout, + entry.getAttributes() + ) + ); } }, e -> { IOUtils.closeWhileHandlingException(connection); @@ -221,9 +268,16 @@ private void findUser(String user, LDAPInterface ldapInterface, ActionListener poolingEnabled, @Nullable String bindDn, Supplier healthCheckDNSupplier, - ThreadPool threadPool) throws LDAPException { + PoolingSessionFactory( + RealmConfig config, + SSLService sslService, + LdapSession.GroupsResolver groupResolver, + Setting.AffixSetting poolingEnabled, + @Nullable String bindDn, + Supplier healthCheckDNSupplier, + ThreadPool threadPool + ) throws LDAPException { super(config, sslService, threadPool); this.groupResolver = groupResolver; final byte[] bindPassword; if (config.hasSetting(LEGACY_BIND_PASSWORD)) { if (config.hasSetting(SECURE_BIND_PASSWORD)) { - throw new IllegalArgumentException("You cannot specify both [" - + RealmSettings.getFullSettingKey(config, LEGACY_BIND_PASSWORD) + "] and [" - + RealmSettings.getFullSettingKey(config, SECURE_BIND_PASSWORD) + "]"); + throw new IllegalArgumentException( + "You cannot specify both [" + + RealmSettings.getFullSettingKey(config, LEGACY_BIND_PASSWORD) + + "] and [" + + RealmSettings.getFullSettingKey(config, SECURE_BIND_PASSWORD) + + "]" + ); } else { bindPassword = CharArrays.toUtf8Bytes(config.getSetting(LEGACY_BIND_PASSWORD).getChars()); } @@ -112,8 +123,12 @@ public final void unauthenticatedSession(String user, ActionListener listener); + abstract void getSessionWithPool( + LDAPConnectionPool connectionPool, + String user, + SecureString password, + ActionListener listener + ); /** * Attempts to get a {@link LdapSession} using the provided credentials and opens a new connection to the ldap server @@ -135,9 +150,14 @@ abstract void getSessionWithPool(LDAPConnectionPool connectionPool, String user, /** * Creates the connection pool that will be used by the session factory and initializes the health check support */ - static LDAPConnectionPool createConnectionPool(RealmConfig config, ServerSet serverSet, TimeValue timeout, Logger logger, - BindRequest bindRequest, - Supplier healthCheckDnSupplier) throws LDAPException { + static LDAPConnectionPool createConnectionPool( + RealmConfig config, + ServerSet serverSet, + TimeValue timeout, + Logger logger, + BindRequest bindRequest, + Supplier healthCheckDnSupplier + ) throws LDAPException { final int initialSize = config.getSetting(PoolingSessionFactorySettings.POOL_INITIAL_SIZE); final int size = config.getSetting(PoolingSessionFactorySettings.POOL_SIZE); LDAPConnectionPool pool = null; @@ -154,15 +174,26 @@ static LDAPConnectionPool createConnectionPool(RealmConfig config, ServerSet ser // create as the LDAP server may require authentication to get an entry and a bind request has not been executed // yet so we could end up never getting a connection. We do not check on checkout as we always set retry operations // and the pool will handle a bad connection without the added latency on every operation - LDAPConnectionPoolHealthCheck healthCheck = new GetEntryLDAPConnectionPoolHealthCheck(entryDn, timeout.millis(), - false, false, false, true, false); + LDAPConnectionPoolHealthCheck healthCheck = new GetEntryLDAPConnectionPoolHealthCheck( + entryDn, + timeout.millis(), + false, + false, + false, + true, + false + ); pool.setHealthCheck(healthCheck); pool.setHealthCheckIntervalMillis(healthCheckInterval); } else { - logger.warn(new ParameterizedMessage("[{}] and [{}} have not been specified or are not valid distinguished names," + - "so connection health checking is disabled", RealmSettings.getFullSettingKey(config, - PoolingSessionFactorySettings.BIND_DN), - RealmSettings.getFullSettingKey(config, PoolingSessionFactorySettings.HEALTH_CHECK_DN))); + logger.warn( + new ParameterizedMessage( + "[{}] and [{}} have not been specified or are not valid distinguished names," + + "so connection health checking is disabled", + RealmSettings.getFullSettingKey(config, PoolingSessionFactorySettings.BIND_DN), + RealmSettings.getFullSettingKey(config, PoolingSessionFactorySettings.HEALTH_CHECK_DN) + ) + ); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/SearchGroupsResolver.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/SearchGroupsResolver.java index f16caa516b1d2..3312afd4acb2f 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/SearchGroupsResolver.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/SearchGroupsResolver.java @@ -12,6 +12,7 @@ import com.unboundid.ldap.sdk.LDAPInterface; import com.unboundid.ldap.sdk.SearchRequest; import com.unboundid.ldap.sdk.SearchScope; + import org.apache.logging.log4j.Logger; import org.elasticsearch.action.ActionListener; import org.elasticsearch.common.Strings; @@ -45,9 +46,10 @@ class SearchGroupsResolver implements GroupsResolver { private final boolean ignoreReferralErrors; SearchGroupsResolver(RealmConfig config) { - baseDn = config.getSetting(SearchGroupsResolverSettings.BASE_DN, () -> { - throw new IllegalArgumentException("base_dn must be specified"); - }); + baseDn = config.getSetting( + SearchGroupsResolverSettings.BASE_DN, + () -> { throw new IllegalArgumentException("base_dn must be specified"); } + ); filter = config.getSetting(SearchGroupsResolverSettings.FILTER); userAttribute = config.getSetting(SearchGroupsResolverSettings.USER_ATTRIBUTE); scope = config.getSetting(SearchGroupsResolverSettings.SCOPE); @@ -55,24 +57,35 @@ class SearchGroupsResolver implements GroupsResolver { } @Override - public void resolve(LDAPInterface connection, String userDn, TimeValue timeout, Logger logger, - Collection attributes, ActionListener> listener) { + public void resolve( + LDAPInterface connection, + String userDn, + TimeValue timeout, + Logger logger, + Collection attributes, + ActionListener> listener + ) { getUserId(userDn, attributes, connection, timeout, ActionListener.wrap((userId) -> { if (userId == null) { listener.onResponse(List.of()); } else { try { Filter userFilter = createFilter(filter, userId); - search(connection, baseDn, scope.scope(), userFilter, - Math.toIntExact(timeout.seconds()), ignoreReferralErrors, - ActionListener.wrap( - (results) -> listener.onResponse(results - .stream() - .map((r) -> r.getDN()) - .collect(Collectors.toUnmodifiableList()) - ), - listener::onFailure), - SearchRequest.NO_ATTRIBUTES); + search( + connection, + baseDn, + scope.scope(), + userFilter, + Math.toIntExact(timeout.seconds()), + ignoreReferralErrors, + ActionListener.wrap( + (results) -> listener.onResponse( + results.stream().map((r) -> r.getDN()).collect(Collectors.toUnmodifiableList()) + ), + listener::onFailure + ), + SearchRequest.NO_ATTRIBUTES + ); } catch (LDAPException e) { listener.onFailure(e); } @@ -88,35 +101,44 @@ public String[] attributes() { return null; } - private void getUserId(String dn, Collection attributes, LDAPInterface connection, - TimeValue timeout, ActionListener listener) { + private void getUserId( + String dn, + Collection attributes, + LDAPInterface connection, + TimeValue timeout, + ActionListener listener + ) { if (isNullOrEmpty(userAttribute) || userAttribute.equals("dn")) { listener.onResponse(dn); } else if (attributes != null) { final String value = attributes.stream() - .filter((attribute) -> attribute.getName().equals(userAttribute)) - .map(Attribute::getValue) - .findFirst() - .orElse(null); + .filter((attribute) -> attribute.getName().equals(userAttribute)) + .map(Attribute::getValue) + .findFirst() + .orElse(null); listener.onResponse(value); } else { readUserAttribute(connection, dn, timeout, listener); } } - void readUserAttribute(LDAPInterface connection, String userDn, TimeValue timeout, - ActionListener listener) { - searchForEntry(connection, userDn, SearchScope.BASE, OBJECT_CLASS_PRESENCE_FILTER, - Math.toIntExact(timeout.seconds()), ignoreReferralErrors, - ActionListener.wrap((entry) -> { - if (entry == null || entry.hasAttribute(userAttribute) == false) { - listener.onResponse(null); - } else { - listener.onResponse(entry.getAttributeValue(userAttribute)); - } - }, listener::onFailure), - userAttribute); + void readUserAttribute(LDAPInterface connection, String userDn, TimeValue timeout, ActionListener listener) { + searchForEntry( + connection, + userDn, + SearchScope.BASE, + OBJECT_CLASS_PRESENCE_FILTER, + Math.toIntExact(timeout.seconds()), + ignoreReferralErrors, + ActionListener.wrap((entry) -> { + if (entry == null || entry.hasAttribute(userAttribute) == false) { + listener.onResponse(null); + } else { + listener.onResponse(entry.getAttributeValue(userAttribute)); + } + }, listener::onFailure), + userAttribute + ); } - } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/UserAttributeGroupsResolver.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/UserAttributeGroupsResolver.java index 8e17bf4ed0d07..8bf42a8eadc87 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/UserAttributeGroupsResolver.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/UserAttributeGroupsResolver.java @@ -9,6 +9,7 @@ import com.unboundid.ldap.sdk.Attribute; import com.unboundid.ldap.sdk.LDAPInterface; import com.unboundid.ldap.sdk.SearchScope; + import org.apache.logging.log4j.Logger; import org.elasticsearch.action.ActionListener; import org.elasticsearch.core.TimeValue; @@ -44,23 +45,37 @@ private UserAttributeGroupsResolver(String attribute, boolean ignoreReferralErro } @Override - public void resolve(LDAPInterface connection, String userDn, TimeValue timeout, Logger logger, Collection attributes, - ActionListener> listener) { + public void resolve( + LDAPInterface connection, + String userDn, + TimeValue timeout, + Logger logger, + Collection attributes, + ActionListener> listener + ) { if (attributes != null) { final List groups = attributes.stream() - .filter((attr) -> attr.getName().equals(attribute)) - .flatMap(attr -> Arrays.stream(attr.getValues())) - .collect(Collectors.toUnmodifiableList()); + .filter((attr) -> attr.getName().equals(attribute)) + .flatMap(attr -> Arrays.stream(attr.getValues())) + .collect(Collectors.toUnmodifiableList()); listener.onResponse(groups); } else { - searchForEntry(connection, userDn, SearchScope.BASE, OBJECT_CLASS_PRESENCE_FILTER, Math.toIntExact(timeout.seconds()), - ignoreReferralErrors, ActionListener.wrap((entry) -> { - if (entry == null || entry.hasAttribute(attribute) == false) { - listener.onResponse(List.of()); - } else { - listener.onResponse(List.of(entry.getAttributeValues(attribute))); - } - }, listener::onFailure), attribute); + searchForEntry( + connection, + userDn, + SearchScope.BASE, + OBJECT_CLASS_PRESENCE_FILTER, + Math.toIntExact(timeout.seconds()), + ignoreReferralErrors, + ActionListener.wrap((entry) -> { + if (entry == null || entry.hasAttribute(attribute) == false) { + listener.onResponse(List.of()); + } else { + listener.onResponse(List.of(entry.getAttributeValues(attribute))); + } + }, listener::onFailure), + attribute + ); } } @@ -69,5 +84,4 @@ public String[] attributes() { return new String[] { attribute }; } - } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/support/LdapLoadBalancing.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/support/LdapLoadBalancing.java index 7e62a65ef495e..d56773d288a73 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/support/LdapLoadBalancing.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/support/LdapLoadBalancing.java @@ -11,17 +11,19 @@ import com.unboundid.ldap.sdk.RoundRobinDNSServerSet; import com.unboundid.ldap.sdk.RoundRobinServerSet; import com.unboundid.ldap.sdk.ServerSet; -import org.elasticsearch.core.Nullable; + import org.elasticsearch.common.Strings; import org.elasticsearch.common.network.InetAddresses; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xpack.core.security.authc.RealmConfig; import org.elasticsearch.xpack.core.security.authc.RealmSettings; import org.elasticsearch.xpack.core.security.authc.ldap.support.LdapLoadBalancingSettings; -import javax.net.SocketFactory; import java.util.Locale; +import javax.net.SocketFactory; + /** * Enumeration representing the various supported {@link ServerSet} types that can be used with out built in realms. */ @@ -29,8 +31,13 @@ public enum LdapLoadBalancing { FAILOVER() { @Override - ServerSet buildServerSet(String[] addresses, int[] ports, RealmConfig realmConfig, @Nullable SocketFactory socketFactory, - @Nullable LDAPConnectionOptions options) { + ServerSet buildServerSet( + String[] addresses, + int[] ports, + RealmConfig realmConfig, + @Nullable SocketFactory socketFactory, + @Nullable LDAPConnectionOptions options + ) { FailoverServerSet serverSet = new FailoverServerSet(addresses, ports, socketFactory, options); serverSet.setReOrderOnFailover(true); return serverSet; @@ -39,16 +46,26 @@ ServerSet buildServerSet(String[] addresses, int[] ports, RealmConfig realmConfi ROUND_ROBIN() { @Override - ServerSet buildServerSet(String[] addresses, int[] ports, RealmConfig realmConfig, @Nullable SocketFactory socketFactory, - @Nullable LDAPConnectionOptions options) { + ServerSet buildServerSet( + String[] addresses, + int[] ports, + RealmConfig realmConfig, + @Nullable SocketFactory socketFactory, + @Nullable LDAPConnectionOptions options + ) { return new RoundRobinServerSet(addresses, ports, socketFactory, options); } }, DNS_ROUND_ROBIN() { @Override - ServerSet buildServerSet(String[] addresses, int[] ports, RealmConfig realmConfig, @Nullable SocketFactory socketFactory, - @Nullable LDAPConnectionOptions options) { + ServerSet buildServerSet( + String[] addresses, + int[] ports, + RealmConfig realmConfig, + @Nullable SocketFactory socketFactory, + @Nullable LDAPConnectionOptions options + ) { if (addresses.length != 1) { throw new IllegalArgumentException(toString() + " can only be used with a single url"); } @@ -56,15 +73,27 @@ ServerSet buildServerSet(String[] addresses, int[] ports, RealmConfig realmConfi throw new IllegalArgumentException(toString() + " can only be used with a DNS name"); } TimeValue dnsTtl = realmConfig.getSetting(LdapLoadBalancingSettings.CACHE_TTL_SETTING); - return new RoundRobinDNSServerSet(addresses[0], ports[0], - RoundRobinDNSServerSet.AddressSelectionMode.ROUND_ROBIN, dnsTtl.millis(), null, socketFactory, options); + return new RoundRobinDNSServerSet( + addresses[0], + ports[0], + RoundRobinDNSServerSet.AddressSelectionMode.ROUND_ROBIN, + dnsTtl.millis(), + null, + socketFactory, + options + ); } }, DNS_FAILOVER() { @Override - ServerSet buildServerSet(String[] addresses, int[] ports, RealmConfig realmConfig, @Nullable SocketFactory socketFactory, - @Nullable LDAPConnectionOptions options) { + ServerSet buildServerSet( + String[] addresses, + int[] ports, + RealmConfig realmConfig, + @Nullable SocketFactory socketFactory, + @Nullable LDAPConnectionOptions options + ) { if (addresses.length != 1) { throw new IllegalArgumentException(toString() + " can only be used with a single url"); } @@ -72,15 +101,27 @@ ServerSet buildServerSet(String[] addresses, int[] ports, RealmConfig realmConfi throw new IllegalArgumentException(toString() + " can only be used with a DNS name"); } TimeValue dnsTtl = realmConfig.getSetting(LdapLoadBalancingSettings.CACHE_TTL_SETTING); - return new RoundRobinDNSServerSet(addresses[0], ports[0], - RoundRobinDNSServerSet.AddressSelectionMode.FAILOVER, dnsTtl.millis(), null, socketFactory, options); + return new RoundRobinDNSServerSet( + addresses[0], + ports[0], + RoundRobinDNSServerSet.AddressSelectionMode.FAILOVER, + dnsTtl.millis(), + null, + socketFactory, + options + ); } }; public static final LdapLoadBalancing LOAD_BALANCE_TYPE_DEFAULT = LdapLoadBalancing.FAILOVER; - abstract ServerSet buildServerSet(String[] addresses, int[] ports, RealmConfig realmConfig, @Nullable SocketFactory socketFactory, - @Nullable LDAPConnectionOptions options); + abstract ServerSet buildServerSet( + String[] addresses, + int[] ports, + RealmConfig realmConfig, + @Nullable SocketFactory socketFactory, + @Nullable LDAPConnectionOptions options + ); @Override public String toString() { @@ -95,14 +136,24 @@ public static LdapLoadBalancing resolve(RealmConfig realmConfig) { try { return valueOf(type.toUpperCase(Locale.ROOT)); } catch (IllegalArgumentException ilae) { - throw new IllegalArgumentException("unknown load balance type [" + type + "] in setting [" + - RealmSettings.getFullSettingKey(realmConfig, LdapLoadBalancingSettings.LOAD_BALANCE_TYPE_SETTING) + - "]", ilae); + throw new IllegalArgumentException( + "unknown load balance type [" + + type + + "] in setting [" + + RealmSettings.getFullSettingKey(realmConfig, LdapLoadBalancingSettings.LOAD_BALANCE_TYPE_SETTING) + + "]", + ilae + ); } } - public static ServerSet serverSet(String[] addresses, int[] ports, RealmConfig realmConfig, @Nullable SocketFactory socketFactory, - @Nullable LDAPConnectionOptions options) { + public static ServerSet serverSet( + String[] addresses, + int[] ports, + RealmConfig realmConfig, + @Nullable SocketFactory socketFactory, + @Nullable LDAPConnectionOptions options + ) { LdapLoadBalancing loadBalancing = resolve(realmConfig); return loadBalancing.buildServerSet(addresses, ports, realmConfig, socketFactory, options); } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/support/LdapMetadataResolver.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/support/LdapMetadataResolver.java index 86739a3132055..c82066a15c593 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/support/LdapMetadataResolver.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/support/LdapMetadataResolver.java @@ -10,6 +10,7 @@ import com.unboundid.ldap.sdk.LDAPInterface; import com.unboundid.ldap.sdk.SearchResultEntry; import com.unboundid.ldap.sdk.SearchScope; + import org.apache.logging.log4j.Logger; import org.elasticsearch.action.ActionListener; import org.elasticsearch.core.TimeValue; @@ -24,8 +25,8 @@ import java.util.function.Function; import java.util.stream.Collectors; -import static org.elasticsearch.xpack.security.authc.ldap.ActiveDirectorySIDUtil.convertToString; import static org.elasticsearch.xpack.security.authc.ldap.ActiveDirectorySIDUtil.TOKEN_GROUPS; +import static org.elasticsearch.xpack.security.authc.ldap.ActiveDirectorySIDUtil.convertToString; import static org.elasticsearch.xpack.security.authc.ldap.support.LdapUtils.OBJECT_CLASS_PRESENCE_FILTER; import static org.elasticsearch.xpack.security.authc.ldap.support.LdapUtils.searchForEntry; @@ -47,47 +48,57 @@ public String[] attributeNames() { return attributeNames; } - public void resolve(LDAPInterface connection, String userDn, TimeValue timeout, Logger logger, - Collection attributes, - ActionListener> listener) { + public void resolve( + LDAPInterface connection, + String userDn, + TimeValue timeout, + Logger logger, + Collection attributes, + ActionListener> listener + ) { if (this.attributeNames.length == 0) { listener.onResponse(Map.of()); } else if (attributes != null) { listener.onResponse(toMap(name -> findAttribute(attributes, name))); } else { - searchForEntry(connection, userDn, SearchScope.BASE, OBJECT_CLASS_PRESENCE_FILTER, - Math.toIntExact(timeout.seconds()), ignoreReferralErrors, - ActionListener.wrap((SearchResultEntry entry) -> { - if (entry == null) { - listener.onResponse(Map.of()); - } else { - listener.onResponse(toMap(entry::getAttribute)); - } - }, listener::onFailure), this.attributeNames); + searchForEntry( + connection, + userDn, + SearchScope.BASE, + OBJECT_CLASS_PRESENCE_FILTER, + Math.toIntExact(timeout.seconds()), + ignoreReferralErrors, + ActionListener.wrap((SearchResultEntry entry) -> { + if (entry == null) { + listener.onResponse(Map.of()); + } else { + listener.onResponse(toMap(entry::getAttribute)); + } + }, listener::onFailure), + this.attributeNames + ); } } private Attribute findAttribute(Collection attributes, String name) { - return attributes.stream() - .filter(attr -> attr.getName().equals(name)) - .findFirst().orElse(null); + return attributes.stream().filter(attr -> attr.getName().equals(name)).findFirst().orElse(null); } private Map toMap(Function attributes) { - return Arrays.stream(this.attributeNames).map(attributes).filter(Objects::nonNull) - .collect(Collectors.toUnmodifiableMap( - attr -> attr.getName(), - attr -> { - final String[] values = attr.getValues(); - if(attr.getName().equals(TOKEN_GROUPS)) { - return values.length == 1 ? convertToString(attr.getValueByteArrays()[0]) : - Arrays.stream(attr.getValueByteArrays()) - .map((sidBytes) -> convertToString(sidBytes)) - .collect(Collectors.toList()); - } - return values.length == 1 ? values[0] : List.of(values); - }) - ); + return Arrays.stream(this.attributeNames) + .map(attributes) + .filter(Objects::nonNull) + .collect(Collectors.toUnmodifiableMap(attr -> attr.getName(), attr -> { + final String[] values = attr.getValues(); + if (attr.getName().equals(TOKEN_GROUPS)) { + return values.length == 1 + ? convertToString(attr.getValueByteArrays()[0]) + : Arrays.stream(attr.getValueByteArrays()) + .map((sidBytes) -> convertToString(sidBytes)) + .collect(Collectors.toList()); + } + return values.length == 1 ? values[0] : List.of(values); + })); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/support/LdapSession.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/support/LdapSession.java index ee530e1ae0500..4cc10b73421af 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/support/LdapSession.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/support/LdapSession.java @@ -9,6 +9,7 @@ import com.unboundid.ldap.sdk.Attribute; import com.unboundid.ldap.sdk.LDAPConnection; import com.unboundid.ldap.sdk.LDAPInterface; + import org.apache.logging.log4j.Logger; import org.elasticsearch.action.ActionListener; import org.elasticsearch.core.Releasable; @@ -41,8 +42,16 @@ public class LdapSession implements Releasable { * outside of and be reused across all connections. We can't keep a static logger in this class * since we want the logger to be contextual (i.e. aware of the settings and its environment). */ - public LdapSession(Logger logger, RealmConfig realm, LDAPInterface connection, String userDn, GroupsResolver groupsResolver, - LdapMetadataResolver metadataResolver, TimeValue timeout, Collection attributes) { + public LdapSession( + Logger logger, + RealmConfig realm, + LDAPInterface connection, + String userDn, + GroupsResolver groupsResolver, + LdapMetadataResolver metadataResolver, + TimeValue timeout, + Collection attributes + ) { this.logger = logger; this.realm = realm; this.connection = connection; @@ -99,17 +108,13 @@ public void metadata(ActionListener> listener) { public void resolve(ActionListener listener) { logger.debug("Resolving LDAP groups + meta-data for user [{}]", userDn); - groups(ActionListener.wrap( - groups -> { - logger.debug("Resolved {} LDAP groups [{}] for user [{}]", groups.size(), groups, userDn); - metadata(ActionListener.wrap( - meta -> { - logger.debug("Resolved {} meta-data fields [{}] for user [{}]", meta.size(), meta, userDn); - listener.onResponse(new LdapUserData(groups, meta)); - }, - listener::onFailure)); - }, - listener::onFailure)); + groups(ActionListener.wrap(groups -> { + logger.debug("Resolved {} LDAP groups [{}] for user [{}]", groups.size(), groups, userDn); + metadata(ActionListener.wrap(meta -> { + logger.debug("Resolved {} meta-data fields [{}] for user [{}]", meta.size(), meta, userDn); + listener.onResponse(new LdapUserData(groups, meta)); + }, listener::onFailure)); + }, listener::onFailure)); } public static class LdapUserData { @@ -137,8 +142,14 @@ public interface GroupsResolver { * {@code null} indicates that the attributes have not been attempted to be retrieved * @param listener the listener to call on a result or on failure */ - void resolve(LDAPInterface ldapConnection, String userDn, TimeValue timeout, Logger logger, Collection attributes, - ActionListener> listener); + void resolve( + LDAPInterface ldapConnection, + String userDn, + TimeValue timeout, + Logger logger, + Collection attributes, + ActionListener> listener + ); /** * Returns the attributes that this resolvers uses. If no attributes are required, return {@code null}. diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/support/LdapUtils.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/support/LdapUtils.java index 05522bdafad87..e893257914e8c 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/support/LdapUtils.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/support/LdapUtils.java @@ -25,8 +25,8 @@ import com.unboundid.ldap.sdk.SearchScope; import org.apache.logging.log4j.Level; -import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.message.ParameterizedMessage; import org.apache.logging.log4j.util.Supplier; import org.apache.lucene.util.SetOnce; @@ -34,14 +34,13 @@ import org.elasticsearch.action.ActionListener; import org.elasticsearch.common.CheckedSupplier; import org.elasticsearch.common.Strings; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.common.util.concurrent.AbstractRunnable; import org.elasticsearch.common.util.concurrent.CountDown; +import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.xpack.core.security.support.Exceptions; -import javax.naming.ldap.Rdn; import java.security.AccessController; import java.security.PrivilegedActionException; import java.security.PrivilegedExceptionAction; @@ -53,6 +52,8 @@ import java.util.Locale; import java.util.stream.Collectors; +import javax.naming.ldap.Rdn; + public final class LdapUtils { public static final Filter OBJECT_CLASS_PRESENCE_FILTER = Filter.createPresenceFilter("objectClass"); @@ -295,9 +296,10 @@ public static void searchForEntry( ldapConnection = privilegedConnect(ldap::getConnection); final LDAPConnection finalConnection = ldapConnection; searchForEntry(finalConnection, baseDN, scope, filter, timeLimitSeconds, ignoreReferralErrors, ActionListener.wrap(entry -> { - assert isLdapConnectionThread(Thread.currentThread()) : "Expected current thread [" - + Thread.currentThread() - + "] to be an LDAPConnectionReader Thread. Probably the new library has changed the thread's name."; + assert isLdapConnectionThread(Thread.currentThread()) + : "Expected current thread [" + + Thread.currentThread() + + "] to be an LDAPConnectionReader Thread. Probably the new library has changed the thread's name."; IOUtils.close(() -> ldap.releaseConnection(finalConnection)); listener.onResponse(entry); }, e -> { @@ -357,9 +359,10 @@ public static void search( ldap, ignoreReferralErrors, ActionListener.wrap(searchResult -> { - assert isLdapConnectionThread(Thread.currentThread()) : "Expected current thread [" - + Thread.currentThread() - + "] to be an LDAPConnectionReader Thread. Probably the new library has changed the thread's name."; + assert isLdapConnectionThread(Thread.currentThread()) + : "Expected current thread [" + + Thread.currentThread() + + "] to be an LDAPConnectionReader Thread. Probably the new library has changed the thread's name."; listener.onResponse(Collections.unmodifiableList(searchResult.getSearchEntries())); }, listener::onFailure), 1 @@ -611,40 +614,37 @@ public void searchResultReceived(AsyncRequestID requestID, SearchResult searchRe final CountDown countDown = new CountDown(referralUrls.length); final List referralUrlsList = new ArrayList<>(Arrays.asList(referralUrls)); - ActionListener referralListener = ActionListener.wrap( - innerResult -> { - // synchronize here since we are possibly sending out a lot of requests - // and the result lists are not thread safe and this also provides us - // with a consistent view - synchronized (this) { - if (innerResult.getSearchEntries() != null) { - entryList.addAll(innerResult.getSearchEntries()); - } - if (innerResult.getSearchReferences() != null) { - referenceList.addAll(innerResult.getSearchReferences()); - } + ActionListener referralListener = ActionListener.wrap(innerResult -> { + // synchronize here since we are possibly sending out a lot of requests + // and the result lists are not thread safe and this also provides us + // with a consistent view + synchronized (this) { + if (innerResult.getSearchEntries() != null) { + entryList.addAll(innerResult.getSearchEntries()); } - - // count down and once all referrals have been traversed then we can - // create the results - if (countDown.countDown()) { - SearchResult resultWithValues = new SearchResult( - searchResult.getMessageID(), - searchResult.getResultCode(), - searchResult.getDiagnosticMessage(), - searchResult.getMatchedDN(), - referralUrlsList.toArray(Strings.EMPTY_ARRAY), - entryList, - referenceList, - entryList.size(), - referenceList.size(), - searchResult.getResponseControls() - ); - listener.onResponse(resultWithValues); + if (innerResult.getSearchReferences() != null) { + referenceList.addAll(innerResult.getSearchReferences()); } - }, - listener::onFailure - ); + } + + // count down and once all referrals have been traversed then we can + // create the results + if (countDown.countDown()) { + SearchResult resultWithValues = new SearchResult( + searchResult.getMessageID(), + searchResult.getResultCode(), + searchResult.getDiagnosticMessage(), + searchResult.getMatchedDN(), + referralUrlsList.toArray(Strings.EMPTY_ARRAY), + entryList, + referenceList, + entryList.size(), + referenceList.size(), + searchResult.getResponseControls() + ); + listener.onResponse(resultWithValues); + } + }, listener::onFailure); for (String referralUrl : referralUrls) { try { diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/support/SessionFactory.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/support/SessionFactory.java index 5c3fdb9aa56be..bd80bda8d82c7 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/support/SessionFactory.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/support/SessionFactory.java @@ -11,6 +11,7 @@ import com.unboundid.ldap.sdk.LDAPURL; import com.unboundid.ldap.sdk.ServerSet; import com.unboundid.util.ssl.HostNameSSLSocketVerifier; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.action.ActionListener; @@ -27,13 +28,14 @@ import org.elasticsearch.xpack.core.ssl.SSLConfigurationSettings; import org.elasticsearch.xpack.core.ssl.SSLService; -import javax.net.SocketFactory; import java.io.Closeable; import java.io.IOException; import java.util.Arrays; import java.util.List; import java.util.regex.Pattern; +import javax.net.SocketFactory; + /** * This factory holds settings needed for authenticating to LDAP and creating LdapConnections. * Each created LdapConnection needs to be closed or else connections will pill up consuming @@ -49,10 +51,8 @@ */ public abstract class SessionFactory implements Closeable { - private static final Pattern STARTS_WITH_LDAPS = Pattern.compile("^ldaps:.*", - Pattern.CASE_INSENSITIVE); - private static final Pattern STARTS_WITH_LDAP = Pattern.compile("^ldap:.*", - Pattern.CASE_INSENSITIVE); + private static final Pattern STARTS_WITH_LDAPS = Pattern.compile("^ldaps:.*", Pattern.CASE_INSENSITIVE); + private static final Pattern STARTS_WITH_LDAP = Pattern.compile("^ldap:.*", Pattern.CASE_INSENSITIVE); protected final Logger logger; protected final RealmConfig config; @@ -69,12 +69,15 @@ public abstract class SessionFactory implements Closeable { protected SessionFactory(RealmConfig config, SSLService sslService, ThreadPool threadPool) { this.config = config; this.logger = LogManager.getLogger(getClass()); - TimeValue searchTimeout = config.getSetting(SessionFactorySettings.TIMEOUT_LDAP_SETTING, - () -> SessionFactorySettings.TIMEOUT_DEFAULT); + TimeValue searchTimeout = config.getSetting( + SessionFactorySettings.TIMEOUT_LDAP_SETTING, + () -> SessionFactorySettings.TIMEOUT_DEFAULT + ); if (searchTimeout.millis() < 1000L) { - logger.warn("ldap_search timeout [{}] is less than the minimum supported search " + - "timeout of 1s. using 1s", - searchTimeout.millis()); + logger.warn( + "ldap_search timeout [{}] is less than the minimum supported search " + "timeout of 1s. using 1s", + searchTimeout.millis() + ); searchTimeout = TimeValue.timeValueSeconds(1L); } this.timeout = searchTimeout; @@ -101,8 +104,7 @@ public void close() throws IOException { * @param password The password of the user * @param listener the listener to call on a failure or result */ - public abstract void session(String user, SecureString password, - ActionListener listener); + public abstract void session(String user, SecureString password, ActionListener listener); /** * Returns a flag to indicate if this session factory supports unauthenticated sessions. @@ -125,17 +127,20 @@ public void unauthenticatedSession(String username, ActionListener throw new UnsupportedOperationException("unauthenticated sessions are not supported"); } - protected static LDAPConnectionOptions connectionOptions(RealmConfig config, - SSLService sslService, Logger logger) { + protected static LDAPConnectionOptions connectionOptions(RealmConfig config, SSLService sslService, Logger logger) { LDAPConnectionOptions options = new LDAPConnectionOptions(); options.setConnectTimeoutMillis(Math.toIntExact(config.getSetting(SessionFactorySettings.TIMEOUT_TCP_CONNECTION_SETTING).millis())); options.setFollowReferrals(config.getSetting(SessionFactorySettings.FOLLOW_REFERRALS_SETTING)); final long responseTimeoutMillis; if (config.hasSetting(SessionFactorySettings.TIMEOUT_RESPONSE_SETTING)) { if (config.hasSetting(SessionFactorySettings.TIMEOUT_TCP_READ_SETTING)) { - throw new IllegalArgumentException("[" + RealmSettings.getFullSettingKey(config, - SessionFactorySettings.TIMEOUT_TCP_READ_SETTING) + "] and [" + RealmSettings.getFullSettingKey(config, - SessionFactorySettings.TIMEOUT_RESPONSE_SETTING) + "] may not be used at the same time"); + throw new IllegalArgumentException( + "[" + + RealmSettings.getFullSettingKey(config, SessionFactorySettings.TIMEOUT_TCP_READ_SETTING) + + "] and [" + + RealmSettings.getFullSettingKey(config, SessionFactorySettings.TIMEOUT_RESPONSE_SETTING) + + "] may not be used at the same time" + ); } responseTimeoutMillis = config.getSetting(SessionFactorySettings.TIMEOUT_RESPONSE_SETTING).millis(); } else { @@ -152,10 +157,13 @@ protected static LDAPConnectionOptions connectionOptions(RealmConfig config, final boolean hostnameVerificationExists = config.hasSetting(SessionFactorySettings.HOSTNAME_VERIFICATION_SETTING); if (verificationModeExists && hostnameVerificationExists) { - throw new IllegalArgumentException("[" + - RealmSettings.getFullSettingKey(config, SessionFactorySettings.HOSTNAME_VERIFICATION_SETTING) + "] and [" + - RealmSettings.getFullSettingKey(config, SSLConfigurationSettings.VERIFICATION_MODE_SETTING_REALM) + - "] may not be used at the same time"); + throw new IllegalArgumentException( + "[" + + RealmSettings.getFullSettingKey(config, SessionFactorySettings.HOSTNAME_VERIFICATION_SETTING) + + "] and [" + + RealmSettings.getFullSettingKey(config, SSLConfigurationSettings.VERIFICATION_MODE_SETTING_REALM) + + "] may not be used at the same time" + ); } else if (verificationModeExists) { final String sslKey = RealmSettings.realmSslPrefix(config.identifier()); final SslConfiguration sslConfiguration = sslService.getSSLConfiguration(sslKey); @@ -168,9 +176,14 @@ protected static LDAPConnectionOptions connectionOptions(RealmConfig config, } else if (hostnameVerificationExists) { final String fullSettingKey = RealmSettings.getFullSettingKey(config, SessionFactorySettings.HOSTNAME_VERIFICATION_SETTING); final String deprecationKey = "deprecated_setting_" + fullSettingKey.replace('.', '_'); - DeprecationLogger.getLogger(logger.getName()).critical(DeprecationCategory.SETTINGS, deprecationKey, - "the setting [{}] has been deprecated and will be removed in a future version. use [{}] instead", - fullSettingKey, RealmSettings.getFullSettingKey(config, SSLConfigurationSettings.VERIFICATION_MODE_SETTING_REALM)); + DeprecationLogger.getLogger(logger.getName()) + .critical( + DeprecationCategory.SETTINGS, + deprecationKey, + "the setting [{}] has been deprecated and will be removed in a future version. use [{}] instead", + fullSettingKey, + RealmSettings.getFullSettingKey(config, SSLConfigurationSettings.VERIFICATION_MODE_SETTING_REALM) + ); if (config.getSetting(SessionFactorySettings.HOSTNAME_VERIFICATION_SETTING)) { options.setSSLSocketVerifier(new HostNameSSLSocketVerifier(true)); } @@ -184,8 +197,9 @@ private LDAPServers ldapServers(RealmConfig config) { // Parse LDAP urls List ldapUrls = config.getSetting(SessionFactorySettings.URLS_SETTING, () -> getDefaultLdapUrls(config)); if (ldapUrls == null || ldapUrls.isEmpty()) { - throw new IllegalArgumentException("missing required LDAP setting [" - + RealmSettings.getFullSettingKey(config, SessionFactorySettings.URLS_SETTING) + "]"); + throw new IllegalArgumentException( + "missing required LDAP setting [" + RealmSettings.getFullSettingKey(config, SessionFactorySettings.URLS_SETTING) + "]" + ); } return new LDAPServers(ldapUrls.toArray(new String[ldapUrls.size()])); } @@ -194,8 +208,7 @@ protected List getDefaultLdapUrls(RealmConfig config) { return null; } - private ServerSet serverSet(RealmConfig realmConfig, SSLService clientSSLService, - LDAPServers ldapServers) { + private ServerSet serverSet(RealmConfig realmConfig, SSLService clientSSLService, LDAPServers ldapServers) { SocketFactory socketFactory = null; if (ldapServers.ssl()) { final String sslKey = RealmSettings.realmSslPrefix(config.identifier()); @@ -207,8 +220,13 @@ private ServerSet serverSet(RealmConfig realmConfig, SSLService clientSSLService logger.debug("using encryption for LDAP connections without hostname verification"); } } - return LdapLoadBalancing.serverSet(ldapServers.addresses(), ldapServers.ports(), realmConfig, - socketFactory, connectionOptions(realmConfig, sslService, logger)); + return LdapLoadBalancing.serverSet( + ldapServers.addresses(), + ldapServers.ports(), + realmConfig, + socketFactory, + connectionOptions(realmConfig, sslService, logger) + ); } // package private to use for testing @@ -236,8 +254,7 @@ public LDAPServers(String[] urls) { addresses[i] = url.getHost(); ports[i] = url.getPort(); } catch (LDAPException e) { - throw new IllegalArgumentException("unable to parse configured LDAP url [" + - urls[i] + "]", e); + throw new IllegalArgumentException("unable to parse configured LDAP url [" + urls[i] + "]", e); } } } @@ -262,16 +279,16 @@ private boolean secureUrls(String[] ldapUrls) { return true; } - final boolean allSecure = Arrays.stream(ldapUrls) - .allMatch(s -> STARTS_WITH_LDAPS.matcher(s).find()); - final boolean allClear = Arrays.stream(ldapUrls) - .allMatch(s -> STARTS_WITH_LDAP.matcher(s).find()); + final boolean allSecure = Arrays.stream(ldapUrls).allMatch(s -> STARTS_WITH_LDAPS.matcher(s).find()); + final boolean allClear = Arrays.stream(ldapUrls).allMatch(s -> STARTS_WITH_LDAP.matcher(s).find()); if (allSecure == false && allClear == false) { - //No mixing is allowed because we use the same socketfactory + // No mixing is allowed because we use the same socketfactory throw new IllegalArgumentException( - "configured LDAP protocols are not all equal (ldaps://.. and ldap://..): [" - + Strings.arrayToCommaDelimitedString(ldapUrls) + "]"); + "configured LDAP protocols are not all equal (ldaps://.. and ldap://..): [" + + Strings.arrayToCommaDelimitedString(ldapUrls) + + "]" + ); } return allSecure; diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectAuthenticator.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectAuthenticator.java index 50e03d9c5c9bc..3ea5e3ee15f66 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectAuthenticator.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectAuthenticator.java @@ -78,11 +78,10 @@ import org.elasticsearch.SpecialPermission; import org.elasticsearch.action.ActionListener; import org.elasticsearch.common.Strings; +import org.elasticsearch.common.ssl.SslConfiguration; import org.elasticsearch.common.util.concurrent.ListenableFuture; import org.elasticsearch.core.CheckedRunnable; import org.elasticsearch.core.Nullable; -import org.elasticsearch.common.Strings; -import org.elasticsearch.common.ssl.SslConfiguration; import org.elasticsearch.core.Tuple; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.watcher.FileChangesListener; @@ -111,6 +110,7 @@ import java.util.List; import java.util.Map; import java.util.concurrent.atomic.AtomicReference; + import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLContext; @@ -141,8 +141,13 @@ public class OpenIdConnectAuthenticator { private static final Logger LOGGER = LogManager.getLogger(OpenIdConnectAuthenticator.class); - public OpenIdConnectAuthenticator(RealmConfig realmConfig, OpenIdConnectProviderConfiguration opConfig, - RelyingPartyConfiguration rpConfig, SSLService sslService, ResourceWatcherService watcherService) { + public OpenIdConnectAuthenticator( + RealmConfig realmConfig, + OpenIdConnectProviderConfiguration opConfig, + RelyingPartyConfiguration rpConfig, + SSLService sslService, + ResourceWatcherService watcherService + ) { this.realmConfig = realmConfig; this.opConfig = opConfig; this.rpConfig = rpConfig; @@ -153,8 +158,14 @@ public OpenIdConnectAuthenticator(RealmConfig realmConfig, OpenIdConnectProvider } // For testing - OpenIdConnectAuthenticator(RealmConfig realmConfig, OpenIdConnectProviderConfiguration opConfig, RelyingPartyConfiguration rpConfig, - SSLService sslService, IDTokenValidator idTokenValidator, ResourceWatcherService watcherService) { + OpenIdConnectAuthenticator( + RealmConfig realmConfig, + OpenIdConnectProviderConfiguration opConfig, + RelyingPartyConfiguration rpConfig, + SSLService sslService, + IDTokenValidator idTokenValidator, + ResourceWatcherService watcherService + ) { this.realmConfig = realmConfig; this.opConfig = opConfig; this.rpConfig = rpConfig; @@ -179,13 +190,22 @@ public void authenticate(OpenIdConnectToken token, final ActionListener claimsListener) { + private void getUserClaims( + @Nullable AccessToken accessToken, + JWT idToken, + Nonce expectedNonce, + boolean shouldRetry, + ActionListener claimsListener + ) { try { JWTClaimsSet verifiedIdTokenClaims = idTokenValidator.get().validate(idToken, expectedNonce).toJWTClaimsSet(); if (LOGGER.isTraceEnabled()) { @@ -257,12 +282,12 @@ private void getUserClaims(@Nullable AccessToken accessToken, JWT idToken, Nonce && JWSAlgorithm.Family.HMAC_SHA.contains(rpConfig.getSignatureAlgorithm()) == false && opConfig.getJwkSetPath().startsWith("https://")) { ((ReloadableJWKSource) ((JWSVerificationKeySelector) idTokenValidator.get().getJWSKeySelector()).getJWKSource()) - .triggerReload(ActionListener.wrap(v -> { - getUserClaims(accessToken, idToken, expectedNonce, false, claimsListener); - }, ex -> { - LOGGER.trace("Attempted and failed to refresh JWK cache upon token validation failure", e); - claimsListener.onFailure(ex); - })); + .triggerReload( + ActionListener.wrap(v -> { getUserClaims(accessToken, idToken, expectedNonce, false, claimsListener); }, ex -> { + LOGGER.trace("Attempted and failed to refresh JWK cache upon token validation failure", e); + claimsListener.onFailure(ex); + }) + ); } else { claimsListener.onFailure(new ElasticsearchSecurityException("Failed to parse or validate the ID Token", e)); } @@ -286,14 +311,16 @@ private void getUserClaims(@Nullable AccessToken accessToken, JWT idToken, Nonce */ private void validateAccessToken(AccessToken accessToken, JWT idToken) { try { - if (rpConfig.getResponseType().equals(ResponseType.parse("id_token token")) || - rpConfig.getResponseType().equals(ResponseType.parse("code"))) { + if (rpConfig.getResponseType().equals(ResponseType.parse("id_token token")) + || rpConfig.getResponseType().equals(ResponseType.parse("code"))) { assert (accessToken != null) : "Access Token cannot be null for Response Type " + rpConfig.getResponseType().toString(); final boolean isValidationOptional = rpConfig.getResponseType().equals(ResponseType.parse("code")); // only "Bearer" is defined in the specification but check just in case if (accessToken.getType().toString().equals("Bearer") == false) { - throw new ElasticsearchSecurityException("Invalid access token type [{}], while [Bearer] was expected", - accessToken.getType()); + throw new ElasticsearchSecurityException( + "Invalid access token type [{}], while [Bearer] was expected", + accessToken.getType() + ); } String atHashValue = idToken.getJWTClaimsSet().getStringClaim("at_hash"); if (Strings.hasText(atHashValue) == false) { @@ -337,8 +364,11 @@ private JWKSet readJwkSetFromFile(String jwkSetPath) throws IOException, ParseEx */ private void validateResponseType(AuthenticationSuccessResponse response) { if (rpConfig.getResponseType().equals(response.impliedResponseType()) == false) { - throw new ElasticsearchSecurityException("Unexpected response type [{}], while [{}] is configured", - response.impliedResponseType(), rpConfig.getResponseType()); + throw new ElasticsearchSecurityException( + "Unexpected response type [{}], while [{}] is configured", + response.impliedResponseType(), + rpConfig.getResponseType() + ); } } @@ -353,8 +383,9 @@ private void validateState(State expectedState, State state) { if (null == state) { throw new ElasticsearchSecurityException("Failed to validate the response, the response did not contain a state parameter"); } else if (null == expectedState) { - throw new ElasticsearchSecurityException("Failed to validate the response, the user's session did not contain a state " + - "parameter"); + throw new ElasticsearchSecurityException( + "Failed to validate the response, the user's session did not contain a state " + "parameter" + ); } else if (state.equals(expectedState) == false) { throw new ElasticsearchSecurityException("Invalid state parameter [{}], while [{}] was expected", state, expectedState); } @@ -363,8 +394,11 @@ private void validateState(State expectedState, State state) { /** * Attempts to make a request to the UserInfo Endpoint of the OpenID Connect provider */ - private void getAndCombineUserInfoClaims(AccessToken accessToken, JWTClaimsSet verifiedIdTokenClaims, - ActionListener claimsListener) { + private void getAndCombineUserInfoClaims( + AccessToken accessToken, + JWTClaimsSet verifiedIdTokenClaims, + ActionListener claimsListener + ) { try { final HttpGet httpGet = new HttpGet(opConfig.getUserinfoEndpoint()); httpGet.setHeader("Authorization", "Bearer " + accessToken.getValue()); @@ -377,14 +411,16 @@ public void completed(HttpResponse result) { @Override public void failed(Exception ex) { - claimsListener.onFailure(new ElasticsearchSecurityException("Failed to get claims from the Userinfo Endpoint.", - ex)); + claimsListener.onFailure( + new ElasticsearchSecurityException("Failed to get claims from the Userinfo Endpoint.", ex) + ); } @Override public void cancelled() { claimsListener.onFailure( - new ElasticsearchSecurityException("Failed to get claims from the Userinfo Endpoint. Request was cancelled")); + new ElasticsearchSecurityException("Failed to get claims from the Userinfo Endpoint. Request was cancelled") + ); } }); return null; @@ -398,8 +434,11 @@ public void cancelled() { * Handle the UserInfo Response from the OpenID Connect Provider. If successful, merge the returned claims with the claims * of the Id Token and call the provided listener. */ - private void handleUserinfoResponse(HttpResponse httpResponse, JWTClaimsSet verifiedIdTokenClaims, - ActionListener claimsListener) { + private void handleUserinfoResponse( + HttpResponse httpResponse, + JWTClaimsSet verifiedIdTokenClaims, + ActionListener claimsListener + ) { try { final HttpEntity entity = httpResponse.getEntity(); final Header encodingHeader = entity.getContentEncoding(); @@ -407,8 +446,11 @@ private void handleUserinfoResponse(HttpResponse httpResponse, JWTClaimsSet veri final Header contentHeader = entity.getContentType(); final String contentAsString = EntityUtils.toString(entity, encoding); if (LOGGER.isTraceEnabled()) { - LOGGER.trace("Received UserInfo Response from OP with status [{}] and content [{}] ", - httpResponse.getStatusLine().getStatusCode(), contentAsString); + LOGGER.trace( + "Received UserInfo Response from OP with status [{}] and content [{}] ", + httpResponse.getStatusLine().getStatusCode(), + contentAsString + ); } if (httpResponse.getStatusLine().getStatusCode() == 200) { if (ContentType.parse(contentHeader.getValue()).getMimeType().equals("application/json")) { @@ -421,30 +463,45 @@ private void handleUserinfoResponse(HttpResponse httpResponse, JWTClaimsSet veri mergeObjects(combinedClaims, userInfoClaims.toJSONObject()); claimsListener.onResponse(JWTClaimsSet.parse(combinedClaims)); } else if (ContentType.parse(contentHeader.getValue()).getMimeType().equals("application/jwt")) { - //TODO Handle validating possibly signed responses - claimsListener.onFailure(new IllegalStateException("Unable to parse Userinfo Response. Signed/encrypted JWTs are" + - "not currently supported")); + // TODO Handle validating possibly signed responses + claimsListener.onFailure( + new IllegalStateException( + "Unable to parse Userinfo Response. Signed/encrypted JWTs are" + "not currently supported" + ) + ); } else { - claimsListener.onFailure(new IllegalStateException("Unable to parse Userinfo Response. Content type was expected to " + - "be [application/json] or [appliation/jwt] but was [" + contentHeader.getValue() + "]")); + claimsListener.onFailure( + new IllegalStateException( + "Unable to parse Userinfo Response. Content type was expected to " + + "be [application/json] or [appliation/jwt] but was [" + + contentHeader.getValue() + + "]" + ) + ); } } else { final Header wwwAuthenticateHeader = httpResponse.getFirstHeader("WWW-Authenticate"); if (Strings.hasText(wwwAuthenticateHeader.getValue())) { BearerTokenError error = BearerTokenError.parse(wwwAuthenticateHeader.getValue()); claimsListener.onFailure( - new ElasticsearchSecurityException("Failed to get user information from the UserInfo endpoint. Code=[{}], " + - "Description=[{}]", error.getCode(), error.getDescription())); + new ElasticsearchSecurityException( + "Failed to get user information from the UserInfo endpoint. Code=[{}], " + "Description=[{}]", + error.getCode(), + error.getDescription() + ) + ); } else { claimsListener.onFailure( - new ElasticsearchSecurityException("Failed to get user information from the UserInfo endpoint. Code=[{}], " + - "Description=[{}]", httpResponse.getStatusLine().getStatusCode(), - httpResponse.getStatusLine().getReasonPhrase())); + new ElasticsearchSecurityException( + "Failed to get user information from the UserInfo endpoint. Code=[{}], " + "Description=[{}]", + httpResponse.getStatusLine().getStatusCode(), + httpResponse.getStatusLine().getReasonPhrase() + ) + ); } } } catch (Exception e) { - claimsListener.onFailure(new ElasticsearchSecurityException("Failed to get user information from the UserInfo endpoint.", - e)); + claimsListener.onFailure(new ElasticsearchSecurityException("Failed to get user information from the UserInfo endpoint.", e)); } } @@ -455,9 +512,13 @@ private void validateUserInfoResponse(JWTClaimsSet userInfoClaims, String expect if (userInfoClaims.getSubject().isEmpty()) { claimsListener.onFailure(new ElasticsearchSecurityException("Userinfo Response did not contain a sub Claim")); } else if (userInfoClaims.getSubject().equals(expectedSub) == false) { - claimsListener.onFailure(new ElasticsearchSecurityException("Userinfo Response is not valid as it is for " + - "subject [{}] while the ID Token was for subject [{}]", userInfoClaims.getSubject(), - expectedSub)); + claimsListener.onFailure( + new ElasticsearchSecurityException( + "Userinfo Response is not valid as it is for " + "subject [{}] while the ID Token was for subject [{}]", + userInfoClaims.getSubject(), + expectedSub + ) + ); } } @@ -476,24 +537,36 @@ private void exchangeCodeForToken(AuthorizationCode code, ActionListener> entry : clientSecretJWT.toParameters().entrySet()) { // Both client_assertion and client_assertion_type are singleton lists params.add(new BasicNameValuePair(entry.getKey(), entry.getValue().get(0))); } } else { - tokensListener.onFailure(new ElasticsearchSecurityException("Failed to exchange code for Id Token using Token Endpoint." + - "Expected client authentication method to be one of " + OpenIdConnectRealmSettings.CLIENT_AUTH_METHODS - + " but was [" + rpConfig.getClientAuthenticationMethod() + "]")); + tokensListener.onFailure( + new ElasticsearchSecurityException( + "Failed to exchange code for Id Token using Token Endpoint." + + "Expected client authentication method to be one of " + + OpenIdConnectRealmSettings.CLIENT_AUTH_METHODS + + " but was [" + + rpConfig.getClientAuthenticationMethod() + + "]" + ) + ); } httpPost.setEntity(new UrlEncodedFormEntity(params)); SpecialPermission.check(); @@ -508,7 +581,8 @@ public void completed(HttpResponse result) { @Override public void failed(Exception ex) { tokensListener.onFailure( - new ElasticsearchSecurityException("Failed to exchange code for Id Token using the Token Endpoint.", ex)); + new ElasticsearchSecurityException("Failed to exchange code for Id Token using the Token Endpoint.", ex) + ); } @Override @@ -521,7 +595,8 @@ public void cancelled() { }); } catch (AuthenticationException | UnsupportedEncodingException | JOSEException e) { tokensListener.onFailure( - new ElasticsearchSecurityException("Failed to exchange code for Id Token using the Token Endpoint.", e)); + new ElasticsearchSecurityException("Failed to exchange code for Id Token using the Token Endpoint.", e) + ); } } @@ -535,8 +610,14 @@ private void handleTokenResponse(HttpResponse httpResponse, ActionListener(accessToken, idToken)); } } catch (Exception e) { tokensListener.onFailure( - new ElasticsearchSecurityException("Failed to exchange code for Id Token using the Token Endpoint. " + - "Unable to parse Token Response", e)); + new ElasticsearchSecurityException( + "Failed to exchange code for Id Token using the Token Endpoint. " + "Unable to parse Token Response", + e + ) + ); } } @@ -589,35 +682,40 @@ private static String truncateToken(String input) { private CloseableHttpAsyncClient createHttpClient() { try { SpecialPermission.check(); - return AccessController.doPrivileged( - (PrivilegedExceptionAction) () -> { - ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(); - final String sslKey = RealmSettings.realmSslPrefix(realmConfig.identifier()); - final SslConfiguration sslConfiguration = sslService.getSSLConfiguration(sslKey); - final SSLContext clientContext = sslService.sslContext(sslConfiguration); - final HostnameVerifier verifier = SSLService.getHostnameVerifier(sslConfiguration); - Registry registry = RegistryBuilder.create() - .register("http", NoopIOSessionStrategy.INSTANCE) - .register("https", new SSLIOSessionStrategy(clientContext, verifier)) - .build(); - PoolingNHttpClientConnectionManager connectionManager = new PoolingNHttpClientConnectionManager(ioReactor, registry); - connectionManager.setDefaultMaxPerRoute(realmConfig.getSetting(HTTP_MAX_ENDPOINT_CONNECTIONS)); - connectionManager.setMaxTotal(realmConfig.getSetting(HTTP_MAX_CONNECTIONS)); - final RequestConfig requestConfig = RequestConfig.custom() - .setConnectTimeout(Math.toIntExact(realmConfig.getSetting(HTTP_CONNECT_TIMEOUT).getMillis())) - .setConnectionRequestTimeout(Math.toIntExact(realmConfig.getSetting(HTTP_CONNECTION_READ_TIMEOUT).getSeconds())) - .setSocketTimeout(Math.toIntExact(realmConfig.getSetting(HTTP_SOCKET_TIMEOUT).getMillis())).build(); - HttpAsyncClientBuilder httpAsyncClientBuilder = HttpAsyncClients.custom() - .setConnectionManager(connectionManager) - .setDefaultRequestConfig(requestConfig); - if (realmConfig.hasSetting(HTTP_PROXY_HOST)) { - httpAsyncClientBuilder.setProxy(new HttpHost(realmConfig.getSetting(HTTP_PROXY_HOST), - realmConfig.getSetting(HTTP_PROXY_PORT), realmConfig.getSetting(HTTP_PROXY_SCHEME))); - } - CloseableHttpAsyncClient httpAsyncClient = httpAsyncClientBuilder.build(); - httpAsyncClient.start(); - return httpAsyncClient; - }); + return AccessController.doPrivileged((PrivilegedExceptionAction) () -> { + ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(); + final String sslKey = RealmSettings.realmSslPrefix(realmConfig.identifier()); + final SslConfiguration sslConfiguration = sslService.getSSLConfiguration(sslKey); + final SSLContext clientContext = sslService.sslContext(sslConfiguration); + final HostnameVerifier verifier = SSLService.getHostnameVerifier(sslConfiguration); + Registry registry = RegistryBuilder.create() + .register("http", NoopIOSessionStrategy.INSTANCE) + .register("https", new SSLIOSessionStrategy(clientContext, verifier)) + .build(); + PoolingNHttpClientConnectionManager connectionManager = new PoolingNHttpClientConnectionManager(ioReactor, registry); + connectionManager.setDefaultMaxPerRoute(realmConfig.getSetting(HTTP_MAX_ENDPOINT_CONNECTIONS)); + connectionManager.setMaxTotal(realmConfig.getSetting(HTTP_MAX_CONNECTIONS)); + final RequestConfig requestConfig = RequestConfig.custom() + .setConnectTimeout(Math.toIntExact(realmConfig.getSetting(HTTP_CONNECT_TIMEOUT).getMillis())) + .setConnectionRequestTimeout(Math.toIntExact(realmConfig.getSetting(HTTP_CONNECTION_READ_TIMEOUT).getSeconds())) + .setSocketTimeout(Math.toIntExact(realmConfig.getSetting(HTTP_SOCKET_TIMEOUT).getMillis())) + .build(); + HttpAsyncClientBuilder httpAsyncClientBuilder = HttpAsyncClients.custom() + .setConnectionManager(connectionManager) + .setDefaultRequestConfig(requestConfig); + if (realmConfig.hasSetting(HTTP_PROXY_HOST)) { + httpAsyncClientBuilder.setProxy( + new HttpHost( + realmConfig.getSetting(HTTP_PROXY_HOST), + realmConfig.getSetting(HTTP_PROXY_PORT), + realmConfig.getSetting(HTTP_PROXY_SCHEME) + ) + ); + } + CloseableHttpAsyncClient httpAsyncClient = httpAsyncClientBuilder.build(); + httpAsyncClient.start(); + return httpAsyncClient; + }); } catch (PrivilegedActionException e) { throw new IllegalStateException("Unable to create a HttpAsyncClient instance", e); } @@ -633,15 +731,16 @@ IDTokenValidator createIdTokenValidator(boolean addFileWatcherIfRequired) { final IDTokenValidator idTokenValidator; if (JWSAlgorithm.Family.HMAC_SHA.contains(requestedAlgorithm)) { final Secret clientSecret = new Secret(rpConfig.getClientSecret().toString()); - idTokenValidator = - new IDTokenValidator(opConfig.getIssuer(), rpConfig.getClientId(), requestedAlgorithm, clientSecret); + idTokenValidator = new IDTokenValidator(opConfig.getIssuer(), rpConfig.getClientId(), requestedAlgorithm, clientSecret); } else { String jwkSetPath = opConfig.getJwkSetPath(); if (jwkSetPath.startsWith("http://")) { throw new IllegalArgumentException("The [http] protocol is not supported as it is insecure. Use [https] instead"); } else if (jwkSetPath.startsWith("https://")) { - final JWSVerificationKeySelector keySelector = new JWSVerificationKeySelector<>(requestedAlgorithm, - new ReloadableJWKSource<>(new URL(jwkSetPath))); + final JWSVerificationKeySelector keySelector = new JWSVerificationKeySelector<>( + requestedAlgorithm, + new ReloadableJWKSource<>(new URL(jwkSetPath)) + ); idTokenValidator = new IDTokenValidator(opConfig.getIssuer(), rpConfig.getClientId(), keySelector, null); } else { if (addFileWatcherIfRequired) { @@ -699,8 +798,16 @@ static Map mergeObjects(Map idToken, Map mergeObjects(Map idToken, Map mergeObjects(Map jsonObject1, Object jsonObject2) { + private static Map mergeObjects(Map jsonObject1, Object jsonObject2) { if (jsonObject2 == null) { return jsonObject1; } if (jsonObject2 instanceof Map) { return mergeObjects(jsonObject1, (Map) jsonObject2); } - throw new IllegalStateException("Error while merging ID token and userinfo claims. " + - "Cannot merge a Map with a [" + jsonObject2.getClass().getName() + "]"); + throw new IllegalStateException( + "Error while merging ID token and userinfo claims. " + "Cannot merge a Map with a [" + jsonObject2.getClass().getName() + "]" + ); } private static JSONArray mergeArrays(JSONArray jsonArray1, Object jsonArray2) { @@ -795,8 +903,12 @@ class ReloadableJWKSource implements JWKSource { private ReloadableJWKSource(URL jwkSetPath) { this.jwkSetPath = jwkSetPath; - triggerReload(ActionListener.wrap(success -> LOGGER.trace("Successfully loaded and cached remote JWKSet on startup"), - failure -> LOGGER.trace("Failed to load and cache remote JWKSet on startup", failure))); + triggerReload( + ActionListener.wrap( + success -> LOGGER.trace("Successfully loaded and cached remote JWKSet on startup"), + failure -> LOGGER.trace("Failed to load and cache remote JWKSet on startup", failure) + ) + ); } @Override @@ -825,8 +937,9 @@ void reloadAsync(final ListenableFuture future) { @Override public void completed(HttpResponse result) { try { - cachedJwkSet = JWKSet.parse(IOUtils.readInputStreamToString(result.getEntity().getContent(), - StandardCharsets.UTF_8)); + cachedJwkSet = JWKSet.parse( + IOUtils.readInputStreamToString(result.getEntity().getContent(), StandardCharsets.UTF_8) + ); reloadFutureRef.set(null); LOGGER.trace("Successfully refreshed and cached remote JWKSet"); future.onResponse(null); @@ -844,7 +957,8 @@ public void failed(Exception ex) { @Override public void cancelled() { future.onFailure( - new ElasticsearchSecurityException("Failed to retrieve remote JWK set. Request was cancelled.")); + new ElasticsearchSecurityException("Failed to retrieve remote JWK set. Request was cancelled.") + ); reloadFutureRef.set(null); } }); diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectProviderConfiguration.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectProviderConfiguration.java index aa75206268681..6296a089c18ac 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectProviderConfiguration.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectProviderConfiguration.java @@ -7,6 +7,7 @@ package org.elasticsearch.xpack.security.authc.oidc; import com.nimbusds.oauth2.sdk.id.Issuer; + import org.elasticsearch.core.Nullable; import java.net.URI; @@ -23,9 +24,14 @@ public class OpenIdConnectProviderConfiguration { private final Issuer issuer; private final String jwkSetPath; - public OpenIdConnectProviderConfiguration(Issuer issuer, String jwkSetPath, URI authorizationEndpoint, - @Nullable URI tokenEndpoint, @Nullable URI userinfoEndpoint, - @Nullable URI endsessionEndpoint) { + public OpenIdConnectProviderConfiguration( + Issuer issuer, + String jwkSetPath, + URI authorizationEndpoint, + @Nullable URI tokenEndpoint, + @Nullable URI userinfoEndpoint, + @Nullable URI endsessionEndpoint + ) { this.authorizationEndpoint = Objects.requireNonNull(authorizationEndpoint, "Authorization Endpoint must be provided"); this.tokenEndpoint = tokenEndpoint; this.userinfoEndpoint = userinfoEndpoint; diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectRealm.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectRealm.java index 7b08e48c47702..b2a0be954054e 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectRealm.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectRealm.java @@ -9,7 +9,6 @@ import com.nimbusds.jose.JWSAlgorithm; import com.nimbusds.jwt.JWT; import com.nimbusds.jwt.JWTClaimsSet; - import com.nimbusds.oauth2.sdk.ParseException; import com.nimbusds.oauth2.sdk.ResponseType; import com.nimbusds.oauth2.sdk.Scope; @@ -20,17 +19,17 @@ import com.nimbusds.openid.connect.sdk.AuthenticationRequest; import com.nimbusds.openid.connect.sdk.LogoutRequest; import com.nimbusds.openid.connect.sdk.Nonce; + import org.apache.logging.log4j.Logger; import org.elasticsearch.ElasticsearchSecurityException; - import org.elasticsearch.action.ActionListener; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; -import org.elasticsearch.core.Releasable; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.SettingsException; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.core.Releasable; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.watcher.ResourceWatcherService; import org.elasticsearch.xpack.core.XPackSettings; @@ -42,11 +41,11 @@ import org.elasticsearch.xpack.core.security.authc.RealmConfig; import org.elasticsearch.xpack.core.security.authc.RealmSettings; import org.elasticsearch.xpack.core.security.authc.oidc.OpenIdConnectRealmSettings; +import org.elasticsearch.xpack.core.security.authc.support.UserRoleMapper; import org.elasticsearch.xpack.core.security.user.User; import org.elasticsearch.xpack.core.ssl.SSLService; import org.elasticsearch.xpack.security.authc.TokenService; import org.elasticsearch.xpack.security.authc.support.DelegatedAuthorizationSupport; -import org.elasticsearch.xpack.core.security.authc.support.UserRoleMapper; import java.net.URI; import java.net.URISyntaxException; @@ -60,7 +59,6 @@ import java.util.regex.Pattern; import java.util.stream.Collectors; - import static org.elasticsearch.xpack.core.security.authc.oidc.OpenIdConnectRealmSettings.DN_CLAIM; import static org.elasticsearch.xpack.core.security.authc.oidc.OpenIdConnectRealmSettings.GROUPS_CLAIM; import static org.elasticsearch.xpack.core.security.authc.oidc.OpenIdConnectRealmSettings.MAIL_CLAIM; @@ -79,8 +77,8 @@ import static org.elasticsearch.xpack.core.security.authc.oidc.OpenIdConnectRealmSettings.RP_CLIENT_SECRET; import static org.elasticsearch.xpack.core.security.authc.oidc.OpenIdConnectRealmSettings.RP_POST_LOGOUT_REDIRECT_URI; import static org.elasticsearch.xpack.core.security.authc.oidc.OpenIdConnectRealmSettings.RP_REDIRECT_URI; -import static org.elasticsearch.xpack.core.security.authc.oidc.OpenIdConnectRealmSettings.RP_RESPONSE_TYPE; import static org.elasticsearch.xpack.core.security.authc.oidc.OpenIdConnectRealmSettings.RP_REQUESTED_SCOPES; +import static org.elasticsearch.xpack.core.security.authc.oidc.OpenIdConnectRealmSettings.RP_RESPONSE_TYPE; import static org.elasticsearch.xpack.core.security.authc.oidc.OpenIdConnectRealmSettings.RP_SIGNATURE_ALGORITHM; public class OpenIdConnectRealm extends Realm implements Releasable { @@ -99,8 +97,7 @@ public class OpenIdConnectRealm extends Realm implements Releasable { private DelegatedAuthorizationSupport delegatedRealms; - public OpenIdConnectRealm(RealmConfig config, SSLService sslService, UserRoleMapper roleMapper, - ResourceWatcherService watcherService) { + public OpenIdConnectRealm(RealmConfig config, SSLService sslService, UserRoleMapper roleMapper, ResourceWatcherService watcherService) { super(config); this.roleMapper = roleMapper; this.rpConfiguration = buildRelyingPartyConfiguration(config); @@ -112,11 +109,19 @@ public OpenIdConnectRealm(RealmConfig config, SSLService sslService, UserRoleMap this.mailAttribute = ClaimParser.forSetting(logger, MAIL_CLAIM, config, false); this.populateUserMetadata = config.getSetting(POPULATE_USER_METADATA); if (TokenService.isTokenServiceEnabled(config.settings()) == false) { - throw new IllegalStateException("OpenID Connect Realm requires that the token service be enabled (" - + XPackSettings.TOKEN_SERVICE_ENABLED_SETTING.getKey() + ")"); + throw new IllegalStateException( + "OpenID Connect Realm requires that the token service be enabled (" + + XPackSettings.TOKEN_SERVICE_ENABLED_SETTING.getKey() + + ")" + ); } - this.openIdConnectAuthenticator = - new OpenIdConnectAuthenticator(config, opConfiguration, rpConfiguration, sslService, watcherService); + this.openIdConnectAuthenticator = new OpenIdConnectAuthenticator( + config, + opConfiguration, + rpConfiguration, + sslService, + watcherService + ); } // For testing @@ -164,18 +169,17 @@ public AuthenticationToken token(ThreadContext context) { public void authenticate(AuthenticationToken token, ActionListener> listener) { if (token instanceof OpenIdConnectToken && isTokenForRealm((OpenIdConnectToken) token)) { OpenIdConnectToken oidcToken = (OpenIdConnectToken) token; - openIdConnectAuthenticator.authenticate(oidcToken, ActionListener.wrap( - jwtClaimsSet -> { - buildUserFromClaims(jwtClaimsSet, listener); - }, - e -> { + openIdConnectAuthenticator.authenticate( + oidcToken, + ActionListener.wrap(jwtClaimsSet -> { buildUserFromClaims(jwtClaimsSet, listener); }, e -> { logger.debug("Failed to consume the OpenIdConnectToken ", e); if (e instanceof ElasticsearchSecurityException) { listener.onResponse(AuthenticationResult.unsuccessful("Failed to authenticate user with OpenID Connect", e)); } else { listener.onFailure(e); } - })); + }) + ); } else { listener.onResponse(AuthenticationResult.notHandled()); } @@ -186,12 +190,12 @@ public void lookupUser(String username, ActionListener listener) { listener.onResponse(null); } - private void buildUserFromClaims(JWTClaimsSet claims, ActionListener> authResultListener) { final String principal = principalAttribute.getClaimValue(claims); if (Strings.isNullOrEmpty(principal)) { - authResultListener.onResponse(AuthenticationResult.unsuccessful( - principalAttribute + "not found in " + claims.toJSONObject(), null)); + authResultListener.onResponse( + AuthenticationResult.unsuccessful(principalAttribute + "not found in " + claims.toJSONObject(), null) + ); return; } @@ -214,7 +218,9 @@ private void buildUserFromClaims(JWTClaimsSet claims, ActionListener userMetadata; if (populateUserMetadata) { - userMetadata = claims.getClaims().entrySet().stream() + userMetadata = claims.getClaims() + .entrySet() + .stream() .filter(entry -> isAllowedTypeForClaim(entry.getValue())) .collect(Collectors.toUnmodifiableMap(entry -> "oidc(" + entry.getKey() + ")", Map.Entry::getValue)); } else { @@ -252,8 +258,9 @@ private RelyingPartyConfiguration buildRelyingPartyConfiguration(RealmConfig con final ClientID clientId = new ClientID(require(config, RP_CLIENT_ID)); final SecureString clientSecret = config.getSetting(RP_CLIENT_SECRET); if (clientSecret.length() == 0) { - throw new SettingsException("The configuration setting [" + RealmSettings.getFullSettingKey(config, RP_CLIENT_SECRET) - + "] is required"); + throw new SettingsException( + "The configuration setting [" + RealmSettings.getFullSettingKey(config, RP_CLIENT_SECRET) + "] is required" + ); } final ResponseType responseType; try { @@ -268,11 +275,21 @@ private RelyingPartyConfiguration buildRelyingPartyConfiguration(RealmConfig con requestedScope.add("openid"); } final JWSAlgorithm signatureAlgorithm = JWSAlgorithm.parse(require(config, RP_SIGNATURE_ALGORITHM)); - final ClientAuthenticationMethod clientAuthenticationMethod = - ClientAuthenticationMethod.parse(require(config, RP_CLIENT_AUTH_METHOD)); + final ClientAuthenticationMethod clientAuthenticationMethod = ClientAuthenticationMethod.parse( + require(config, RP_CLIENT_AUTH_METHOD) + ); final JWSAlgorithm clientAuthJwtAlgorithm = JWSAlgorithm.parse(require(config, RP_CLIENT_AUTH_JWT_SIGNATURE_ALGORITHM)); - return new RelyingPartyConfiguration(clientId, clientSecret, redirectUri, responseType, requestedScope, - signatureAlgorithm, clientAuthenticationMethod, clientAuthJwtAlgorithm, postLogoutRedirectUri); + return new RelyingPartyConfiguration( + clientId, + clientSecret, + redirectUri, + responseType, + requestedScope, + signatureAlgorithm, + clientAuthenticationMethod, + clientAuthJwtAlgorithm, + postLogoutRedirectUri + ); } private OpenIdConnectProviderConfiguration buildOpenIdConnectProviderConfiguration(RealmConfig config) { @@ -290,8 +307,13 @@ private OpenIdConnectProviderConfiguration buildOpenIdConnectProviderConfigurati String responseType = require(config, RP_RESPONSE_TYPE); String tokenEndpointString = config.getSetting(OP_TOKEN_ENDPOINT); if (responseType.equals("code") && tokenEndpointString.isEmpty()) { - throw new SettingsException("The configuration setting [" + OP_TOKEN_ENDPOINT.getConcreteSettingForNamespace(name()).getKey() - + "] is required when [" + RP_RESPONSE_TYPE.getConcreteSettingForNamespace(name()).getKey() + "] is set to \"code\""); + throw new SettingsException( + "The configuration setting [" + + OP_TOKEN_ENDPOINT.getConcreteSettingForNamespace(name()).getKey() + + "] is required when [" + + RP_RESPONSE_TYPE.getConcreteSettingForNamespace(name()).getKey() + + "] is set to \"code\"" + ); } URI tokenEndpoint; try { @@ -302,30 +324,37 @@ private OpenIdConnectProviderConfiguration buildOpenIdConnectProviderConfigurati } URI userinfoEndpoint; try { - userinfoEndpoint = (config.getSetting(OP_USERINFO_ENDPOINT).isEmpty()) ? null : - new URI(config.getSetting(OP_USERINFO_ENDPOINT)); + userinfoEndpoint = (config.getSetting(OP_USERINFO_ENDPOINT).isEmpty()) + ? null + : new URI(config.getSetting(OP_USERINFO_ENDPOINT)); } catch (URISyntaxException e) { // This should never happen as it's already validated in the settings throw new SettingsException("Invalid URI: " + OP_USERINFO_ENDPOINT.getKey(), e); } URI endsessionEndpoint; try { - endsessionEndpoint = (config.getSetting(OP_ENDSESSION_ENDPOINT).isEmpty()) ? null : - new URI(config.getSetting(OP_ENDSESSION_ENDPOINT)); + endsessionEndpoint = (config.getSetting(OP_ENDSESSION_ENDPOINT).isEmpty()) + ? null + : new URI(config.getSetting(OP_ENDSESSION_ENDPOINT)); } catch (URISyntaxException e) { // This should never happen as it's already validated in the settings throw new SettingsException("Invalid URI: " + OP_ENDSESSION_ENDPOINT.getKey(), e); } - return new OpenIdConnectProviderConfiguration(issuer, jwkSetUrl, authorizationEndpoint, tokenEndpoint, - userinfoEndpoint, endsessionEndpoint); + return new OpenIdConnectProviderConfiguration( + issuer, + jwkSetUrl, + authorizationEndpoint, + tokenEndpoint, + userinfoEndpoint, + endsessionEndpoint + ); } private static String require(RealmConfig config, Setting.AffixSetting setting) { final String value = config.getSetting(setting); if (value.isEmpty()) { - throw new SettingsException("The configuration setting [" + RealmSettings.getFullSettingKey(config, setting) - + "] is required"); + throw new SettingsException("The configuration setting [" + RealmSettings.getFullSettingKey(config, setting) + "] is required"); } return value; } @@ -348,23 +377,28 @@ private static String require(RealmConfig config, Setting.AffixSetting s * * @return an {@link OpenIdConnectPrepareAuthenticationResponse} */ - public OpenIdConnectPrepareAuthenticationResponse buildAuthenticationRequestUri(@Nullable String existingState, - @Nullable String existingNonce, - @Nullable String loginHint) { + public OpenIdConnectPrepareAuthenticationResponse buildAuthenticationRequestUri( + @Nullable String existingState, + @Nullable String existingNonce, + @Nullable String loginHint + ) { final State state = existingState != null ? new State(existingState) : new State(); final Nonce nonce = existingNonce != null ? new Nonce(existingNonce) : new Nonce(); - final AuthenticationRequest.Builder builder = new AuthenticationRequest.Builder(rpConfiguration.getResponseType(), + final AuthenticationRequest.Builder builder = new AuthenticationRequest.Builder( + rpConfiguration.getResponseType(), rpConfiguration.getRequestedScope(), rpConfiguration.getClientId(), - rpConfiguration.getRedirectUri()) - .endpointURI(opConfiguration.getAuthorizationEndpoint()) - .state(state) - .nonce(nonce); + rpConfiguration.getRedirectUri() + ).endpointURI(opConfiguration.getAuthorizationEndpoint()).state(state).nonce(nonce); if (Strings.hasText(loginHint)) { builder.loginHint(loginHint); } - return new OpenIdConnectPrepareAuthenticationResponse(builder.build().toURI().toString(), - state.getValue(), nonce.getValue(), this.name()); + return new OpenIdConnectPrepareAuthenticationResponse( + builder.build().toURI().toString(), + state.getValue(), + nonce.getValue(), + this.name() + ); } public boolean isIssuerValid(String issuer) { @@ -374,8 +408,12 @@ public boolean isIssuerValid(String issuer) { public OpenIdConnectLogoutResponse buildLogoutResponse(JWT idTokenHint) { if (opConfiguration.getEndsessionEndpoint() != null) { final State state = new State(); - final LogoutRequest logoutRequest = new LogoutRequest(opConfiguration.getEndsessionEndpoint(), idTokenHint, - rpConfiguration.getPostLogoutRedirectUri(), state); + final LogoutRequest logoutRequest = new LogoutRequest( + opConfiguration.getEndsessionEndpoint(), + idTokenHint, + rpConfiguration.getPostLogoutRedirectUri(), + state + ); return new OpenIdConnectLogoutResponse(logoutRequest.toURI().toString()); } else { return new OpenIdConnectLogoutResponse((String) null); @@ -391,9 +429,11 @@ public void close() { * We only map claims that are of Type String, Boolean, or Number, or arrays that contain only these types */ private static boolean isAllowedTypeForClaim(Object o) { - return (o instanceof String || o instanceof Boolean || o instanceof Number - || (o instanceof Collection && ((Collection) o).stream() - .allMatch(c -> c instanceof String || c instanceof Boolean || c instanceof Number))); + return (o instanceof String + || o instanceof Boolean + || o instanceof Number + || (o instanceof Collection + && ((Collection) o).stream().allMatch(c -> c instanceof String || c instanceof Boolean || c instanceof Number))); } static final class ClaimParser { @@ -431,28 +471,40 @@ private static Collection parseClaimValues(JWTClaimsSet claimsSet, Strin values = List.of(); } else if (claimValueObject instanceof String) { values = List.of((String) claimValueObject); - } else if (claimValueObject instanceof Collection && - ((Collection) claimValueObject).stream().allMatch(c -> c instanceof String)) { - values = (Collection) claimValueObject; - } else { - throw new SettingsException("Setting [ " + settingKey + " expects a claim with String or a String Array value"); - } + } else if (claimValueObject instanceof Collection + && ((Collection) claimValueObject).stream().allMatch(c -> c instanceof String)) { + values = (Collection) claimValueObject; + } else { + throw new SettingsException("Setting [ " + settingKey + " expects a claim with String or a String Array value"); + } return values; } - static ClaimParser forSetting(Logger logger, OpenIdConnectRealmSettings.ClaimSetting setting, RealmConfig realmConfig, - boolean required) { + static ClaimParser forSetting( + Logger logger, + OpenIdConnectRealmSettings.ClaimSetting setting, + RealmConfig realmConfig, + boolean required + ) { if (realmConfig.hasSetting(setting.getClaim())) { String claimName = realmConfig.getSetting(setting.getClaim()); if (realmConfig.hasSetting(setting.getPattern())) { Pattern regex = Pattern.compile(realmConfig.getSetting(setting.getPattern())); return new ClaimParser( - "OpenID Connect Claim [" + claimName + "] with pattern [" + regex.pattern() + "] for [" - + setting.name(realmConfig) + "]", + "OpenID Connect Claim [" + + claimName + + "] with pattern [" + + regex.pattern() + + "] for [" + + setting.name(realmConfig) + + "]", claims -> { - Collection values = - parseClaimValues(claims, claimName, RealmSettings.getFullSettingKey(realmConfig, setting.getClaim())); + Collection values = parseClaimValues( + claims, + claimName, + RealmSettings.getFullSettingKey(realmConfig, setting.getClaim()) + ); return values.stream().map(s -> { if (s == null) { logger.debug("OpenID Connect Claim [{}] is null", claimName); @@ -460,34 +512,49 @@ static ClaimParser forSetting(Logger logger, OpenIdConnectRealmSettings.ClaimSet } final Matcher matcher = regex.matcher(s); if (matcher.find() == false) { - logger.debug("OpenID Connect Claim [{}] is [{}], which does not match [{}]", - claimName, s, regex.pattern()); + logger.debug( + "OpenID Connect Claim [{}] is [{}], which does not match [{}]", + claimName, + s, + regex.pattern() + ); return null; } final String value = matcher.group(1); if (Strings.isNullOrEmpty(value)) { - logger.debug("OpenID Connect Claim [{}] is [{}], which does match [{}] but group(1) is empty", - claimName, s, regex.pattern()); + logger.debug( + "OpenID Connect Claim [{}] is [{}], which does match [{}] but group(1) is empty", + claimName, + s, + regex.pattern() + ); return null; } return value; }).filter(Objects::nonNull).collect(Collectors.toUnmodifiableList()); - }); + } + ); } else { return new ClaimParser( "OpenID Connect Claim [" + claimName + "] for [" + setting.name(realmConfig) + "]", claims -> parseClaimValues(claims, claimName, RealmSettings.getFullSettingKey(realmConfig, setting.getClaim())) .stream() .filter(Objects::nonNull) - .collect(Collectors.toUnmodifiableList())); + .collect(Collectors.toUnmodifiableList()) + ); } } else if (required) { - throw new SettingsException("Setting [" + RealmSettings.getFullSettingKey(realmConfig, setting.getClaim()) - + "] is required"); + throw new SettingsException( + "Setting [" + RealmSettings.getFullSettingKey(realmConfig, setting.getClaim()) + "] is required" + ); } else if (realmConfig.hasSetting(setting.getPattern())) { - throw new SettingsException("Setting [" + RealmSettings.getFullSettingKey(realmConfig, setting.getPattern()) - + "] cannot be set unless [" + RealmSettings.getFullSettingKey(realmConfig, setting.getClaim()) - + "] is also set"); + throw new SettingsException( + "Setting [" + + RealmSettings.getFullSettingKey(realmConfig, setting.getPattern()) + + "] cannot be set unless [" + + RealmSettings.getFullSettingKey(realmConfig, setting.getClaim()) + + "] is also set" + ); } else { return new ClaimParser("No OpenID Connect Claim for [" + setting.name(realmConfig) + "]", attributes -> List.of()); } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectToken.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectToken.java index 7a907bad798ad..309def1cf31f4 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectToken.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectToken.java @@ -8,6 +8,7 @@ import com.nimbusds.oauth2.sdk.id.State; import com.nimbusds.openid.connect.sdk.Nonce; + import org.elasticsearch.core.Nullable; import org.elasticsearch.xpack.core.security.authc.AuthenticationToken; @@ -67,10 +68,21 @@ public String getRedirectUrl() { return redirectUrl; } - public String getAuthenticatingRealm() { return authenticatingRealm; } + public String getAuthenticatingRealm() { + return authenticatingRealm; + } public String toString() { - return getClass().getSimpleName() + "{ redirectUrl=" + redirectUrl + ", state=" + state + ", nonce=" + nonce + ", " + - "authenticatingRealm="+ authenticatingRealm +"}"; + return getClass().getSimpleName() + + "{ redirectUrl=" + + redirectUrl + + ", state=" + + state + + ", nonce=" + + nonce + + ", " + + "authenticatingRealm=" + + authenticatingRealm + + "}"; } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/oidc/RelyingPartyConfiguration.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/oidc/RelyingPartyConfiguration.java index bc9e967861495..0c26b8f61b43e 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/oidc/RelyingPartyConfiguration.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/oidc/RelyingPartyConfiguration.java @@ -11,8 +11,9 @@ import com.nimbusds.oauth2.sdk.Scope; import com.nimbusds.oauth2.sdk.auth.ClientAuthenticationMethod; import com.nimbusds.oauth2.sdk.id.ClientID; -import org.elasticsearch.core.Nullable; + import org.elasticsearch.common.settings.SecureString; +import org.elasticsearch.core.Nullable; import java.net.URI; import java.util.Objects; @@ -31,19 +32,28 @@ public class RelyingPartyConfiguration { private final ClientAuthenticationMethod clientAuthenticationMethod; private final JWSAlgorithm clientAuthenticationJwtAlgorithm; - public RelyingPartyConfiguration(ClientID clientId, SecureString clientSecret, URI redirectUri, ResponseType responseType, - Scope requestedScope, JWSAlgorithm algorithm, ClientAuthenticationMethod clientAuthenticationMethod, - JWSAlgorithm clientAuthenticationJwtAlgorithm, @Nullable URI postLogoutRedirectUri) { + public RelyingPartyConfiguration( + ClientID clientId, + SecureString clientSecret, + URI redirectUri, + ResponseType responseType, + Scope requestedScope, + JWSAlgorithm algorithm, + ClientAuthenticationMethod clientAuthenticationMethod, + JWSAlgorithm clientAuthenticationJwtAlgorithm, + @Nullable URI postLogoutRedirectUri + ) { this.clientId = Objects.requireNonNull(clientId, "clientId must be provided"); this.clientSecret = Objects.requireNonNull(clientSecret, "clientSecret must be provided"); this.redirectUri = Objects.requireNonNull(redirectUri, "redirectUri must be provided"); this.responseType = Objects.requireNonNull(responseType, "responseType must be provided"); this.requestedScope = Objects.requireNonNull(requestedScope, "responseType must be provided"); this.signatureAlgorithm = Objects.requireNonNull(algorithm, "algorithm must be provided"); - this.clientAuthenticationMethod = Objects.requireNonNull(clientAuthenticationMethod, - "clientAuthenticationMethod must be provided"); - this.clientAuthenticationJwtAlgorithm = Objects.requireNonNull(clientAuthenticationJwtAlgorithm, - "clientAuthenticationJwtAlgorithm must be provided"); + this.clientAuthenticationMethod = Objects.requireNonNull(clientAuthenticationMethod, "clientAuthenticationMethod must be provided"); + this.clientAuthenticationJwtAlgorithm = Objects.requireNonNull( + clientAuthenticationJwtAlgorithm, + "clientAuthenticationJwtAlgorithm must be provided" + ); this.postLogoutRedirectUri = postLogoutRedirectUri; } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/pki/PkiRealm.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/pki/PkiRealm.java index 55572484365df..8052659a91551 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/pki/PkiRealm.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/pki/PkiRealm.java @@ -37,8 +37,6 @@ import org.elasticsearch.xpack.security.authc.support.mapper.CompositeRoleMapper; import org.elasticsearch.xpack.security.authc.support.mapper.NativeRoleMappingStore; -import javax.net.ssl.X509ExtendedTrustManager; -import javax.net.ssl.X509TrustManager; import java.security.MessageDigest; import java.security.cert.CertificateEncodingException; import java.security.cert.CertificateException; @@ -53,6 +51,9 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; +import javax.net.ssl.X509ExtendedTrustManager; +import javax.net.ssl.X509TrustManager; + public class PkiRealm extends Realm implements CachingRealm { public static final String PKI_CERT_HEADER_NAME = "__SECURITY_CLIENT_CERTIFICATE"; @@ -93,9 +94,9 @@ public PkiRealm(RealmConfig config, ResourceWatcherService watcherService, Nativ this.roleMapper = roleMapper; this.roleMapper.refreshRealmOnChange(this); this.cache = CacheBuilder.builder() - .setExpireAfterWrite(config.getSetting(PkiRealmSettings.CACHE_TTL_SETTING)) - .setMaximumWeight(config.getSetting(PkiRealmSettings.CACHE_MAX_USERS_SETTING)) - .build(); + .setExpireAfterWrite(config.getSetting(PkiRealmSettings.CACHE_TTL_SETTING)) + .setMaximumWeight(config.getSetting(PkiRealmSettings.CACHE_MAX_USERS_SETTING)) + .build(); this.delegatedRealms = null; validateAuthenticationDelegationConfiguration(config); } @@ -149,8 +150,13 @@ public void authenticate(AuthenticationToken authToken, ActionListener) () -> new ParameterizedMessage("Using cached authentication for DN [{}], as principal [{}]", - token.dn(), user.principal())); + logger.debug( + (Supplier) () -> new ParameterizedMessage( + "Using cached authentication for DN [{}], as principal [{}]", + token.dn(), + user.principal() + ) + ); if (delegatedRealms.hasDelegation()) { delegatedRealms.resolve(user.principal(), listener); } else { @@ -166,9 +172,13 @@ public void authenticate(AuthenticationToken authToken, ActionListener) () -> new ParameterizedMessage( - "the extracted principal after cert chain validation, from DN [{}], using pattern [{}] is null", token.dn(), - principalPattern.toString())); + logger.debug( + (Supplier) () -> new ParameterizedMessage( + "the extracted principal after cert chain validation, from DN [{}], using pattern [{}] is null", + token.dn(), + principalPattern.toString() + ) + ); listener.onResponse(AuthenticationResult.unsuccessful("Could not parse principal from Subject DN " + token.dn(), null)); } else { final ActionListener> cachingListener = ActionListener.wrap(result -> { @@ -180,9 +190,14 @@ public void authenticate(AuthenticationToken authToken, ActionListener) () -> new ParameterizedMessage( + logger.debug( + (Supplier) () -> new ParameterizedMessage( "the extracted principal before [{}] and after [{}] cert chain validation, for DN [{}], are different", - token.principal(), principal, token.dn())); + token.principal(), + principal, + token.dn() + ) + ); } if (delegatedRealms.hasDelegation()) { delegatedRealms.resolve(principal, cachingListener); @@ -199,9 +214,14 @@ public void authenticate(AuthenticationToken authToken, ActionListener> listener) { final Map metadata; if (token.isDelegated()) { - metadata = Map.of("pki_dn", token.dn(), - "pki_delegated_by_user", token.getDelegateeAuthentication().getUser().principal(), - "pki_delegated_by_realm", token.getDelegateeAuthentication().getAuthenticatedBy().getName()); + metadata = Map.of( + "pki_dn", + token.dn(), + "pki_delegated_by_user", + token.getDelegateeAuthentication().getUser().principal(), + "pki_delegated_by_realm", + token.getDelegateeAuthentication().getAuthenticatedBy().getName() + ); } else { metadata = Map.of("pki_dn", token.dn()); } @@ -221,14 +241,24 @@ static String getPrincipalFromSubjectDN(Pattern principalPattern, X509Authentica String dn = token.credentials()[0].getSubjectX500Principal().toString(); Matcher matcher = principalPattern.matcher(dn); if (false == matcher.find()) { - logger.debug((Supplier) () -> new ParameterizedMessage("could not extract principal from DN [{}] using pattern [{}]", dn, - principalPattern.toString())); + logger.debug( + (Supplier) () -> new ParameterizedMessage( + "could not extract principal from DN [{}] using pattern [{}]", + dn, + principalPattern.toString() + ) + ); return null; } String principal = matcher.group(1); if (Strings.isNullOrEmpty(principal)) { - logger.debug((Supplier) () -> new ParameterizedMessage("the extracted principal from DN [{}] using pattern [{}] is empty", - dn, principalPattern.toString())); + logger.debug( + (Supplier) () -> new ParameterizedMessage( + "the extracted principal from DN [{}] using pattern [{}] is empty", + dn, + principalPattern.toString() + ) + ); return null; } return principal; @@ -307,13 +337,18 @@ private void validateAuthenticationDelegationConfiguration(RealmConfig config) { if (delegationEnabled) { List exceptionMessages = new ArrayList<>(2); if (this.trustManager == null) { - exceptionMessages.add("a trust configuration (" - + config.getConcreteSetting(PkiRealmSettings.CAPATH_SETTING).getKey() + " or " - + config.getConcreteSetting(PkiRealmSettings.TRUST_STORE_PATH).getKey() + ")"); + exceptionMessages.add( + "a trust configuration (" + + config.getConcreteSetting(PkiRealmSettings.CAPATH_SETTING).getKey() + + " or " + + config.getConcreteSetting(PkiRealmSettings.TRUST_STORE_PATH).getKey() + + ")" + ); } if (false == TokenService.isTokenServiceEnabled(config.settings())) { - exceptionMessages.add("that the token service be also enabled (" - + XPackSettings.TOKEN_SERVICE_ENABLED_SETTING.getKey() + ")"); + exceptionMessages.add( + "that the token service be also enabled (" + XPackSettings.TOKEN_SERVICE_ENABLED_SETTING.getKey() + ")" + ); } if (false == exceptionMessages.isEmpty()) { String message = "PKI realms with delegation enabled require " + exceptionMessages.get(0); diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/IdpConfiguration.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/IdpConfiguration.java index 963d3cf08726c..e693b463afb35 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/IdpConfiguration.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/IdpConfiguration.java @@ -6,11 +6,11 @@ */ package org.elasticsearch.xpack.security.authc.saml; +import org.opensaml.security.credential.Credential; + import java.util.List; import java.util.function.Supplier; -import org.opensaml.security.credential.Credential; - /** * A simple container class that holds all configuration related to a SAML Identity Provider (IdP). */ diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlAttributes.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlAttributes.java index 8e8471a7f5ddb..fabf7da46ebb5 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlAttributes.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlAttributes.java @@ -6,15 +6,15 @@ */ package org.elasticsearch.xpack.security.authc.saml; -import java.util.List; -import java.util.Objects; -import java.util.stream.Collectors; - -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; +import org.elasticsearch.core.Nullable; import org.opensaml.saml.saml2.core.Attribute; import org.opensaml.saml.saml2.core.NameIDType; +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; + /** * An lightweight collection of SAML attributes */ @@ -50,9 +50,9 @@ List getAttributeValues(String attributeId) { return List.of(name.value); } return attributes.stream() - .filter(attr -> attributeId.equals(attr.name) || attributeId.equals(attr.friendlyName)) - .flatMap(attr -> attr.values.stream()) - .collect(Collectors.toUnmodifiableList()); + .filter(attr -> attributeId.equals(attr.name) || attributeId.equals(attr.friendlyName)) + .flatMap(attr -> attr.values.stream()) + .collect(Collectors.toUnmodifiableList()); } List attributes() { @@ -78,11 +78,15 @@ static class SamlAttribute { final List values; SamlAttribute(Attribute attribute) { - this(attribute.getName(), attribute.getFriendlyName(), - attribute.getAttributeValues().stream() - .map(x -> x.getDOM().getTextContent()) - .filter(Objects::nonNull) - .collect(Collectors.toUnmodifiableList())); + this( + attribute.getName(), + attribute.getFriendlyName(), + attribute.getAttributeValues() + .stream() + .map(x -> x.getDOM().getTextContent()) + .filter(Objects::nonNull) + .collect(Collectors.toUnmodifiableList()) + ); } SamlAttribute(String name, @Nullable String friendlyName, List values) { diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlAuthenticator.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlAuthenticator.java index bb477c23e72ee..8a25c814f25b1 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlAuthenticator.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlAuthenticator.java @@ -9,8 +9,8 @@ import org.apache.logging.log4j.message.ParameterizedMessage; import org.elasticsearch.ElasticsearchSecurityException; import org.elasticsearch.common.Strings; -import org.elasticsearch.core.Tuple; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.core.Tuple; import org.opensaml.core.xml.XMLObject; import org.opensaml.saml.saml2.core.Assertion; import org.opensaml.saml.saml2.core.Attribute; @@ -47,10 +47,7 @@ class SamlAuthenticator extends SamlResponseHandler { private static final String RESPONSE_TAG_NAME = "Response"; - SamlAuthenticator(Clock clock, - IdpConfiguration idp, - SpConfiguration sp, - TimeValue maxSkew) { + SamlAuthenticator(Clock clock, IdpConfiguration idp, SpConfiguration sp, TimeValue maxSkew) { super(clock, idp, sp, maxSkew); } @@ -65,13 +62,20 @@ SamlAttributes authenticate(SamlToken token) { try { return authenticateResponse(root, token.getAllowedSamlRequestIds()); } catch (ElasticsearchSecurityException e) { - logger.trace("Rejecting SAML response [{}...] because {}", Strings.cleanTruncate(SamlUtils.toString(root), 512), - e.getMessage()); + logger.trace( + "Rejecting SAML response [{}...] because {}", + Strings.cleanTruncate(SamlUtils.toString(root), 512), + e.getMessage() + ); throw e; } } else { - throw samlException("SAML content [{}] should have a root element of Namespace=[{}] Tag=[{}]", - root, SAML_NAMESPACE, RESPONSE_TAG_NAME); + throw samlException( + "SAML content [{}] should have a root element of Namespace=[{}] Tag=[{}]", + root, + SAML_NAMESPACE, + RESPONSE_TAG_NAME + ); } } @@ -100,9 +104,10 @@ private SamlAttributes authenticateResponse(Element element, Collection final Assertion assertion = details.v1(); final SamlNameId nameId = SamlNameId.forSubject(assertion.getSubject()); final String session = getSessionIndex(assertion); - final List attributes = details.v2().stream() - .map(SamlAttributes.SamlAttribute::new) - .collect(Collectors.toList()); + final List attributes = details.v2() + .stream() + .map(SamlAttributes.SamlAttribute::new) + .collect(Collectors.toList()); if (logger.isTraceEnabled()) { StringBuilder sb = new StringBuilder(); sb.append("The SAML Assertion contained the following attributes: \n"); @@ -112,9 +117,12 @@ private SamlAttributes authenticateResponse(Element element, Collection logger.trace(sb.toString()); } if (attributes.isEmpty() && nameId == null) { - logger.debug("The Attribute Statements of SAML Response with ID [{}] contained no attributes and the SAML Assertion Subject " + - "did not contain a SAML NameID. Please verify that the Identity Provider configuration with regards to attribute " + - "release is correct. ", response.getID()); + logger.debug( + "The Attribute Statements of SAML Response with ID [{}] contained no attributes and the SAML Assertion Subject " + + "did not contain a SAML NameID. Please verify that the Identity Provider configuration with regards to attribute " + + "release is correct. ", + response.getID() + ); throw samlException("Could not process any SAML attributes in {}", response.getElementQName()); } @@ -129,14 +137,18 @@ private void checkResponseDestination(Response response) { final String asc = getSpConfiguration().getAscUrl(); if (asc.equals(response.getDestination()) == false) { if (response.isSigned() || Strings.hasText(response.getDestination())) { - throw samlException("SAML response " + response.getID() + " is for destination " + response.getDestination() - + " but this realm uses " + asc); + throw samlException( + "SAML response " + response.getID() + " is for destination " + response.getDestination() + " but this realm uses " + asc + ); } } } - private Tuple> extractDetails(Response response, Collection allowedSamlRequestIds, - boolean requireSignedAssertions) { + private Tuple> extractDetails( + Response response, + Collection allowedSamlRequestIds, + boolean requireSignedAssertions + ) { final int assertionCount = response.getAssertions().size() + response.getEncryptedAssertions().size(); if (assertionCount > 1) { throw samlException("Expecting only 1 assertion, but response contains multiple (" + assertionCount + ")"); @@ -167,8 +179,14 @@ private Assertion decrypt(EncryptedAssertion encrypted) { try { return decrypter.decrypt(encrypted); } catch (DecryptionException e) { - logger.debug(() -> new ParameterizedMessage("Failed to decrypt SAML assertion [{}] with [{}]", - text(encrypted, 512), describe(getSpConfiguration().getEncryptionCredentials())), e); + logger.debug( + () -> new ParameterizedMessage( + "Failed to decrypt SAML assertion [{}] with [{}]", + text(encrypted, 512), + describe(getSpConfiguration().getEncryptionCredentials()) + ), + e + ); throw samlException("Failed to decrypt SAML assertion " + text(encrypted, 32), e); } } @@ -192,8 +210,11 @@ private List processAssertion(Assertion assertion, boolean requireSig List attributes = new ArrayList<>(); for (AttributeStatement statement : assertion.getAttributeStatements()) { - logger.trace("SAML AttributeStatement has [{}] attributes and [{}] encrypted attributes", - statement.getAttributes().size(), statement.getEncryptedAttributes().size()); + logger.trace( + "SAML AttributeStatement has [{}] attributes and [{}] encrypted attributes", + statement.getAttributes().size(), + statement.getEncryptedAttributes().size() + ); attributes.addAll(statement.getAttributes()); for (EncryptedAttribute enc : statement.getEncryptedAttributes()) { final Attribute attribute = decrypt(enc); @@ -208,17 +229,21 @@ private List processAssertion(Assertion assertion, boolean requireSig private void checkAuthnStatement(List authnStatements) { if (authnStatements.size() != 1) { - throw samlException("SAML Assertion subject contains [{}] Authn Statements while exactly one was expected.", - authnStatements.size()); + throw samlException( + "SAML Assertion subject contains [{}] Authn Statements while exactly one was expected.", + authnStatements.size() + ); } final AuthnStatement authnStatement = authnStatements.get(0); // "past now" that is now - the maximum skew we will tolerate. Essentially "if our clock is 2min fast, what time is it now?" final Instant now = now(); final Instant pastNow = now.minusMillis(maxSkewInMillis()); - if (authnStatement.getSessionNotOnOrAfter() != null && - pastNow.isBefore(authnStatement.getSessionNotOnOrAfter()) == false) { - throw samlException("Rejecting SAML assertion's Authentication Statement because [{}] is on/after [{}]", pastNow, - authnStatement.getSessionNotOnOrAfter()); + if (authnStatement.getSessionNotOnOrAfter() != null && pastNow.isBefore(authnStatement.getSessionNotOnOrAfter()) == false) { + throw samlException( + "Rejecting SAML assertion's Authentication Statement because [{}] is on/after [{}]", + pastNow, + authnStatement.getSessionNotOnOrAfter() + ); } List reqAuthnCtxClassRef = this.getSpConfiguration().getReqAuthnCtxClassRef(); if (reqAuthnCtxClassRef.isEmpty() == false) { @@ -227,8 +252,12 @@ private void checkAuthnStatement(List authnStatements) { authnCtxClassRefValue = authnStatement.getAuthnContext().getAuthnContextClassRef().getURI(); } if (Strings.isNullOrEmpty(authnCtxClassRefValue) || reqAuthnCtxClassRef.contains(authnCtxClassRefValue) == false) { - throw samlException("Rejecting SAML assertion as the AuthnContextClassRef [{}] is not one of the ({}) that were " + - "requested in the corresponding AuthnRequest", authnCtxClassRefValue, reqAuthnCtxClassRef); + throw samlException( + "Rejecting SAML assertion as the AuthnContextClassRef [{}] is not one of the ({}) that were " + + "requested in the corresponding AuthnRequest", + authnCtxClassRefValue, + reqAuthnCtxClassRef + ); } } } @@ -249,9 +278,10 @@ private Attribute decrypt(EncryptedAttribute encrypted) { private void checkConditions(Conditions conditions) { if (conditions != null) { if (logger.isTraceEnabled()) { - logger.trace("SAML Assertion was intended for the following Service providers: {}", - conditions.getAudienceRestrictions().stream().map(r -> text(r, 32)) - .collect(Collectors.joining(" | "))); + logger.trace( + "SAML Assertion was intended for the following Service providers: {}", + conditions.getAudienceRestrictions().stream().map(r -> text(r, 32)).collect(Collectors.joining(" | ")) + ); logger.trace("SAML Assertion is only valid between: " + conditions.getNotBefore() + " and " + conditions.getNotOnOrAfter()); } checkAudienceRestrictions(conditions.getAudienceRestrictions()); @@ -264,12 +294,17 @@ private void checkSubject(Subject assertionSubject, XMLObject parent, Collection if (assertionSubject == null) { throw samlException("SAML Assertion ({}) has no Subject", text(parent, 16)); } - final List confirmationData = assertionSubject.getSubjectConfirmations().stream() - .filter(data -> data.getMethod().equals(METHOD_BEARER)) - .map(SubjectConfirmation::getSubjectConfirmationData).filter(Objects::nonNull).collect(Collectors.toList()); + final List confirmationData = assertionSubject.getSubjectConfirmations() + .stream() + .filter(data -> data.getMethod().equals(METHOD_BEARER)) + .map(SubjectConfirmation::getSubjectConfirmationData) + .filter(Objects::nonNull) + .collect(Collectors.toList()); if (confirmationData.size() != 1) { - throw samlException("SAML Assertion subject contains [{}] bearer SubjectConfirmation, while exactly one was expected.", - confirmationData.size()); + throw samlException( + "SAML Assertion subject contains [{}] bearer SubjectConfirmation, while exactly one was expected.", + confirmationData.size() + ); } if (logger.isTraceEnabled()) { logger.trace("SAML Assertion Subject Confirmation intended recipient is: " + confirmationData.get(0).getRecipient()); @@ -281,28 +316,36 @@ private void checkSubject(Subject assertionSubject, XMLObject parent, Collection checkSubjectInResponseTo(confirmationData.get(0), allowedSamlRequestIds); } - private void checkSubjectInResponseTo( - SubjectConfirmationData subjectConfirmationData, Collection allowedSamlRequestIds) { + private void checkSubjectInResponseTo(SubjectConfirmationData subjectConfirmationData, Collection allowedSamlRequestIds) { // Allow for IdP initiated SSO where InResponseTo MUST be missing if (Strings.hasText(subjectConfirmationData.getInResponseTo()) - && allowedSamlRequestIds.contains(subjectConfirmationData.getInResponseTo()) == false) { - throw samlException("SAML Assertion SubjectConfirmationData is in-response-to [{}] but expected one of [{}]", - subjectConfirmationData.getInResponseTo(), allowedSamlRequestIds); + && allowedSamlRequestIds.contains(subjectConfirmationData.getInResponseTo()) == false) { + throw samlException( + "SAML Assertion SubjectConfirmationData is in-response-to [{}] but expected one of [{}]", + subjectConfirmationData.getInResponseTo(), + allowedSamlRequestIds + ); } } private void checkRecipient(SubjectConfirmationData subjectConfirmationData) { final SpConfiguration sp = getSpConfiguration(); if (sp.getAscUrl().equals(subjectConfirmationData.getRecipient()) == false) { - throw samlException("SAML Assertion SubjectConfirmationData Recipient [{}] does not match expected value [{}]", - subjectConfirmationData.getRecipient(), sp.getAscUrl()); + throw samlException( + "SAML Assertion SubjectConfirmationData Recipient [{}] does not match expected value [{}]", + subjectConfirmationData.getRecipient(), + sp.getAscUrl() + ); } } private void checkAudienceRestrictions(List restrictions) { if (restrictions.stream().allMatch(this::checkAudienceRestriction) == false) { - throw samlException("Conditions [{}] do not match required audience [{}]", - restrictions.stream().map(r -> text(r, 56, 8)).collect(Collectors.joining(" | ")), getSpConfiguration().getEntityId()); + throw samlException( + "Conditions [{}] do not match required audience [{}]", + restrictions.stream().map(r -> text(r, 56, 8)).collect(Collectors.joining(" | ")), + getSpConfiguration().getEntityId() + ); } } @@ -318,9 +361,15 @@ private boolean checkAudienceRestriction(AudienceRestriction restriction) { } // If the difference is less than half the length of the string, show it in detail if (diffChar >= spEntityId.length() / 2) { - logger.info("Audience restriction [{}] does not match required audience [{}] " + - "(difference starts at character [#{}] [{}] vs [{}])", - uri, spEntityId, diffChar, uri.substring(diffChar), spEntityId.substring(diffChar)); + logger.info( + "Audience restriction [{}] does not match required audience [{}] " + + "(difference starts at character [#{}] [{}] vs [{}])", + uri, + spEntityId, + diffChar, + uri.substring(diffChar), + spEntityId.substring(diffChar) + ); } else { logger.info("Audience restriction [{}] does not match required audience [{}]", uri, spEntityId); @@ -333,8 +382,8 @@ private boolean checkAudienceRestriction(AudienceRestriction restriction) { private void checkLifetimeRestrictions(Conditions conditions) { // In order to compensate for clock skew we construct 2 alternate realities - // - a "future now" that is now + the maximum skew we will tolerate. Essentially "if our clock is 2min slow, what time is it now?" - // - a "past now" that is now - the maximum skew we will tolerate. Essentially "if our clock is 2min fast, what time is it now?" + // - a "future now" that is now + the maximum skew we will tolerate. Essentially "if our clock is 2min slow, what time is it now?" + // - a "past now" that is now - the maximum skew we will tolerate. Essentially "if our clock is 2min fast, what time is it now?" final Instant now = now(); final Instant futureNow = now.plusMillis(maxSkewInMillis()); final Instant pastNow = now.minusMillis(maxSkewInMillis()); diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlAuthnRequestBuilder.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlAuthnRequestBuilder.java index 57d56ab0a63c9..8ddf8c9d89448 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlAuthnRequestBuilder.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlAuthnRequestBuilder.java @@ -66,11 +66,15 @@ AuthnRequest build() { } private RequestedAuthnContext buildRequestedAuthnContext() { - RequestedAuthnContext requestedAuthnContext = SamlUtils.buildObject(RequestedAuthnContext.class, RequestedAuthnContext - .DEFAULT_ELEMENT_NAME); + RequestedAuthnContext requestedAuthnContext = SamlUtils.buildObject( + RequestedAuthnContext.class, + RequestedAuthnContext.DEFAULT_ELEMENT_NAME + ); for (String authnCtxClass : super.serviceProvider.getReqAuthnCtxClassRef()) { - AuthnContextClassRef authnContextClassRef = SamlUtils.buildObject(AuthnContextClassRef.class, AuthnContextClassRef - .DEFAULT_ELEMENT_NAME); + AuthnContextClassRef authnContextClassRef = SamlUtils.buildObject( + AuthnContextClassRef.class, + AuthnContextClassRef.DEFAULT_ELEMENT_NAME + ); authnContextClassRef.setURI(authnCtxClass); requestedAuthnContext.getAuthnContextClassRefs().add(authnContextClassRef); } @@ -90,8 +94,12 @@ private NameIDPolicy buildNameIDPolicy() { private String getIdpLocation() { final String location = getIdentityProviderEndpoint(idpBinding, IDPSSODescriptor::getSingleSignOnServices); if (location == null) { - throw new ElasticsearchException("Cannot find [{}]/[{}] in descriptor [{}]", - IDPSSODescriptor.DEFAULT_ELEMENT_NAME, idpBinding, identityProvider.getID()); + throw new ElasticsearchException( + "Cannot find [{}]/[{}] in descriptor [{}]", + IDPSSODescriptor.DEFAULT_ELEMENT_NAME, + idpBinding, + identityProvider.getID() + ); } return location; } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlLogoutRequestHandler.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlLogoutRequestHandler.java index 2f7aad050475a..8acf793a4377d 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlLogoutRequestHandler.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlLogoutRequestHandler.java @@ -6,9 +6,6 @@ */ package org.elasticsearch.xpack.security.authc.saml; -import java.time.Clock; -import java.util.Objects; - import org.apache.logging.log4j.message.ParameterizedMessage; import org.elasticsearch.ElasticsearchSecurityException; import org.elasticsearch.core.TimeValue; @@ -20,6 +17,9 @@ import org.opensaml.xmlsec.signature.Signature; import org.w3c.dom.Element; +import java.time.Clock; +import java.util.Objects; + import static org.elasticsearch.xpack.security.authc.saml.SamlUtils.samlException; /** @@ -57,8 +57,12 @@ public Result parseFromQueryString(String queryString) { throw e; } } else { - throw samlException("SAML content [{}] should have a root element of Namespace=[{}] Tag=[{}]", - root, SAML_NAMESPACE, REQUEST_TAG_NAME); + throw samlException( + "SAML content [{}] should have a root element of Namespace=[{}] Tag=[{}]", + root, + SAML_NAMESPACE, + REQUEST_TAG_NAME + ); } } @@ -100,30 +104,37 @@ private SAMLObject decrypt(EncryptedID encrypted) { try { return decrypter.decrypt(encrypted); } catch (DecryptionException e) { - logger.debug(() -> new ParameterizedMessage("Failed to decrypt SAML EncryptedID [{}] with [{}]", - text(encrypted, 512), describe(getSpConfiguration().getEncryptionCredentials())), e); + logger.debug( + () -> new ParameterizedMessage( + "Failed to decrypt SAML EncryptedID [{}] with [{}]", + text(encrypted, 512), + describe(getSpConfiguration().getEncryptionCredentials()) + ), + e + ); throw samlException("Failed to decrypt SAML EncryptedID " + text(encrypted, 32), e); } } private String getSessionIndex(LogoutRequest logoutRequest) { - return logoutRequest.getSessionIndexes() - .stream() - .map(as -> as.getValue()) - .filter(Objects::nonNull) - .findFirst() - .orElse(null); + return logoutRequest.getSessionIndexes().stream().map(as -> as.getValue()).filter(Objects::nonNull).findFirst().orElse(null); } private void checkDestination(LogoutRequest request) { final String url = getSpConfiguration().getLogoutUrl(); if (url == null) { - throw samlException("SAML request " + request.getID() + " is for destination " + request.getDestination() - + " but this realm is not configured for logout"); + throw samlException( + "SAML request " + + request.getID() + + " is for destination " + + request.getDestination() + + " but this realm is not configured for logout" + ); } if (url.equals(request.getDestination()) == false) { - throw samlException("SAML request " + request.getID() + " is for destination " + request.getDestination() - + " but this realm uses " + url); + throw samlException( + "SAML request " + request.getID() + " is for destination " + request.getDestination() + " but this realm uses " + url + ); } } @@ -158,12 +169,19 @@ public String getRelayState() { @Override public String toString() { - return "SamlLogoutRequestHandler.Result{" + - "requestId='" + requestId + '\'' + - ", nameId=" + nameId + - ", session='" + session + '\'' + - ", relayState='" + relayState + '\'' + - '}'; + return "SamlLogoutRequestHandler.Result{" + + "requestId='" + + requestId + + '\'' + + ", nameId=" + + nameId + + ", session='" + + session + + '\'' + + ", relayState='" + + relayState + + '\'' + + '}'; } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlLogoutRequestMessageBuilder.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlLogoutRequestMessageBuilder.java index 0118482c6b13b..31b231424f6bf 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlLogoutRequestMessageBuilder.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlLogoutRequestMessageBuilder.java @@ -6,8 +6,6 @@ */ package org.elasticsearch.xpack.security.authc.saml; -import java.time.Clock; - import org.elasticsearch.common.Strings; import org.opensaml.saml.common.xml.SAMLConstants; import org.opensaml.saml.saml2.core.Issuer; @@ -17,6 +15,8 @@ import org.opensaml.saml.saml2.metadata.EntityDescriptor; import org.opensaml.saml.saml2.metadata.SSODescriptor; +import java.time.Clock; + /** * Constructs {@code <LogoutRequest<} objects for use in a SAML Single-Sign-Out flow. */ @@ -24,8 +24,13 @@ class SamlLogoutRequestMessageBuilder extends SamlMessageBuilder { private final NameID nameId; private final String session; - SamlLogoutRequestMessageBuilder(Clock clock, SpConfiguration serviceProvider, EntityDescriptor identityProvider, - NameID nameId, String session) { + SamlLogoutRequestMessageBuilder( + Clock clock, + SpConfiguration serviceProvider, + EntityDescriptor identityProvider, + NameID nameId, + String session + ) { super(identityProvider, serviceProvider, clock); this.nameId = nameId; this.session = session; diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlLogoutResponseBuilder.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlLogoutResponseBuilder.java index 492f50a56aa00..05fe9d2643e84 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlLogoutResponseBuilder.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlLogoutResponseBuilder.java @@ -6,8 +6,6 @@ */ package org.elasticsearch.xpack.security.authc.saml; -import java.time.Clock; - import org.elasticsearch.common.Strings; import org.opensaml.saml.common.xml.SAMLConstants; import org.opensaml.saml.saml2.core.LogoutResponse; @@ -16,6 +14,8 @@ import org.opensaml.saml.saml2.metadata.EntityDescriptor; import org.opensaml.saml.saml2.metadata.SSODescriptor; +import java.time.Clock; + import static org.elasticsearch.xpack.security.authc.saml.SamlUtils.samlException; /** @@ -25,8 +25,13 @@ class SamlLogoutResponseBuilder extends SamlMessageBuilder { private final String inResponseTo; private final String statusValue; - SamlLogoutResponseBuilder(Clock clock, SpConfiguration serviceProvider, EntityDescriptor identityProvider, - String inResponseTo, String statusValue) { + SamlLogoutResponseBuilder( + Clock clock, + SpConfiguration serviceProvider, + EntityDescriptor identityProvider, + String inResponseTo, + String statusValue + ) { super(identityProvider, serviceProvider, clock); this.inResponseTo = inResponseTo; this.statusValue = statusValue; @@ -35,8 +40,10 @@ class SamlLogoutResponseBuilder extends SamlMessageBuilder { LogoutResponse build() { final String destination = getLogoutUrl(); if (Strings.isNullOrEmpty(destination)) { - throw samlException("Cannot send LogoutResponse because the IDP {} does not provide a logout service", - identityProvider.getEntityID()); + throw samlException( + "Cannot send LogoutResponse because the IDP {} does not provide a logout service", + identityProvider.getEntityID() + ); } final LogoutResponse res = SamlUtils.buildObject(LogoutResponse.class, LogoutResponse.DEFAULT_ELEMENT_NAME); @@ -47,7 +54,7 @@ LogoutResponse build() { res.setInResponseTo(inResponseTo); final Status status = SamlUtils.buildObject(Status.class, Status.DEFAULT_ELEMENT_NAME); - final StatusCode statusCode= SamlUtils.buildObject(StatusCode.class, StatusCode.DEFAULT_ELEMENT_NAME); + final StatusCode statusCode = SamlUtils.buildObject(StatusCode.class, StatusCode.DEFAULT_ELEMENT_NAME); statusCode.setValue(this.statusValue); status.setStatusCode(statusCode); res.setStatus(status); diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlLogoutResponseHandler.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlLogoutResponseHandler.java index 07da47e870f32..3a16687f84b37 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlLogoutResponseHandler.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlLogoutResponseHandler.java @@ -20,8 +20,7 @@ public class SamlLogoutResponseHandler extends SamlResponseHandler { private static final String LOGOUT_RESPONSE_TAG_NAME = "LogoutResponse"; - public SamlLogoutResponseHandler( - Clock clock, IdpConfiguration idp, SpConfiguration sp, TimeValue maxSkew) { + public SamlLogoutResponseHandler(Clock clock, IdpConfiguration idp, SpConfiguration sp, TimeValue maxSkew) { super(clock, idp, sp, maxSkew); } @@ -30,7 +29,7 @@ public void handle(boolean httpRedirect, String payload, Collection allo if (httpRedirect) { logger.debug("Process SAML LogoutResponse with HTTP-Redirect binding"); final ParsedQueryString parsed = parseQueryStringAndValidateSignature(payload, "SAMLResponse"); - if (parsed.hasSignature == false){ + if (parsed.hasSignature == false) { throw samlException("Query string is not signed, but is required for HTTP-Redirect binding"); } root = parseSamlMessage(inflate(decodeBase64(parsed.samlMessage))); @@ -53,8 +52,12 @@ public void handle(boolean httpRedirect, String payload, Collection allo checkIssuer(logoutResponse.getIssuer(), logoutResponse); checkResponseDestination(logoutResponse, getSpConfiguration().getLogoutUrl()); } else { - throw samlException("SAML content [{}] should have a root element of Namespace=[{}] Tag=[{}]", - root, SAML_NAMESPACE, LOGOUT_RESPONSE_TAG_NAME); + throw samlException( + "SAML content [{}] should have a root element of Namespace=[{}] Tag=[{}]", + root, + SAML_NAMESPACE, + LOGOUT_RESPONSE_TAG_NAME + ); } } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlMessageBuilder.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlMessageBuilder.java index 03b8f9ecb1e0d..ddefce807dc9a 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlMessageBuilder.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlMessageBuilder.java @@ -37,20 +37,27 @@ public SamlMessageBuilder(EntityDescriptor identityProvider, SpConfiguration ser this.clock = clock; } - protected String getIdentityProviderEndpoint(String binding, - Function> selector) { - final List locations = identityProvider.getRoleDescriptors(IDPSSODescriptor.DEFAULT_ELEMENT_NAME).stream() - .map(rd -> (IDPSSODescriptor) rd) - .flatMap(idp -> selector.apply(idp).stream()) - .filter(endp -> binding.equals(endp.getBinding())) - .map(sso -> sso.getLocation()) - .collect(Collectors.toList()); + protected String getIdentityProviderEndpoint( + String binding, + Function> selector + ) { + final List locations = identityProvider.getRoleDescriptors(IDPSSODescriptor.DEFAULT_ELEMENT_NAME) + .stream() + .map(rd -> (IDPSSODescriptor) rd) + .flatMap(idp -> selector.apply(idp).stream()) + .filter(endp -> binding.equals(endp.getBinding())) + .map(sso -> sso.getLocation()) + .collect(Collectors.toList()); if (locations.isEmpty()) { return null; } if (locations.size() > 1) { - throw new ElasticsearchException("Found multiple locations for binding [{}] in descriptor [{}] - [{}]", - binding, identityProvider.getID(), locations); + throw new ElasticsearchException( + "Found multiple locations for binding [{}] in descriptor [{}] - [{}]", + binding, + identityProvider.getID(), + locations + ); } return locations.get(0); } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlMetadataCommand.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlMetadataCommand.java index 7f5389c204d3c..cd74a39e7bc42 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlMetadataCommand.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlMetadataCommand.java @@ -6,26 +6,6 @@ */ package org.elasticsearch.xpack.security.authc.saml; -import java.io.IOException; -import java.io.InputStream; -import java.io.Writer; -import java.nio.file.Files; -import java.nio.file.Path; -import java.security.Key; -import java.security.PrivateKey; -import java.security.cert.Certificate; -import java.security.cert.X509Certificate; -import java.util.Arrays; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.Set; -import java.util.SortedSet; -import java.util.TreeSet; -import java.util.concurrent.atomic.AtomicReference; -import java.util.stream.Collectors; - import joptsimple.OptionParser; import joptsimple.OptionSet; import joptsimple.OptionSpec; @@ -66,6 +46,26 @@ import org.w3c.dom.Element; import org.xml.sax.SAXException; +import java.io.IOException; +import java.io.InputStream; +import java.io.Writer; +import java.nio.file.Files; +import java.nio.file.Path; +import java.security.Key; +import java.security.PrivateKey; +import java.security.cert.Certificate; +import java.security.cert.X509Certificate; +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Set; +import java.util.SortedSet; +import java.util.TreeSet; +import java.util.concurrent.atomic.AtomicReference; +import java.util.stream.Collectors; + /** * CLI tool to generate SAML Metadata for a Service Provider (realm) */ @@ -111,22 +111,25 @@ public SamlMetadataCommand(CheckedFunction attributes = getAttributeNames(options, realm); for (String attr : attributes.keySet()) { @@ -185,23 +191,31 @@ EntityDescriptor buildEntityDescriptor(Terminal terminal, OptionSet options, Env if (batch) { friendlyName = settingName; } else { - friendlyName = terminal.readText("What is the friendly name for " + - attributeSource - + " attribute \"" + attr + "\" [default: " + - (settingName == null ? "none" : settingName) + - "] "); + friendlyName = terminal.readText( + "What is the friendly name for " + + attributeSource + + " attribute \"" + + attr + + "\" [default: " + + (settingName == null ? "none" : settingName) + + "] " + ); if (Strings.isNullOrEmpty(friendlyName)) { friendlyName = settingName; } } } else { if (batch) { - throw new UserException(ExitCodes.CONFIG, "Option " + batchSpec.toString() + " is specified, but attribute " - + attr + " appears to be a FriendlyName value"); + throw new UserException( + ExitCodes.CONFIG, + "Option " + batchSpec.toString() + " is specified, but attribute " + attr + " appears to be a FriendlyName value" + ); } friendlyName = attr; - name = requireText(terminal, - "What is the standard (urn) name for " + attributeSource + " attribute \"" + attr + "\" (required): "); + name = requireText( + terminal, + "What is the standard (urn) name for " + attributeSource + " attribute \"" + attr + "\" (required): " + ); } terminal.println(Terminal.Verbosity.VERBOSE, "Requesting attribute '" + name + "' (FriendlyName: '" + friendlyName + "')"); builder.withAttribute(friendlyName, name); @@ -225,8 +239,12 @@ EntityDescriptor buildEntityDescriptor(Terminal terminal, OptionSet options, Env if (ContactInfo.TYPES.containsKey(type)) { break; } else { - terminal.errorPrintln("Type '" + type + "' is not valid. Valid values are " - + Strings.collectionToCommaDelimitedString(ContactInfo.TYPES.keySet())); + terminal.errorPrintln( + "Type '" + + type + + "' is not valid. Valid values are " + + Strings.collectionToCommaDelimitedString(ContactInfo.TYPES.keySet()) + ); } } builder.withContact(type, givenName, surName, email); @@ -238,13 +256,13 @@ EntityDescriptor buildEntityDescriptor(Terminal terminal, OptionSet options, Env // package-protected for testing Element possiblySignDescriptor(Terminal terminal, OptionSet options, EntityDescriptor descriptor, Environment env) - throws UserException { + throws UserException { try { final EntityDescriptorMarshaller marshaller = new EntityDescriptorMarshaller(); if (options.has(signingPkcs12PathSpec) || (options.has(signingCertPathSpec) && options.has(signingKeyPathSpec))) { Signature signature = (Signature) XMLObjectProviderRegistrySupport.getBuilderFactory() - .getBuilder(Signature.DEFAULT_ELEMENT_NAME) - .buildObject(Signature.DEFAULT_ELEMENT_NAME); + .getBuilder(Signature.DEFAULT_ELEMENT_NAME) + .buildObject(Signature.DEFAULT_ELEMENT_NAME); signature.setSigningCredential(buildSigningCredential(terminal, options, env)); signature.setSignatureAlgorithm(SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA256); signature.setCanonicalizationAlgorithm(SignatureConstants.ALGO_ID_C14N_EXCL_OMIT_COMMENTS); @@ -279,19 +297,23 @@ private Path writeOutput(Terminal terminal, OptionSet options, Element element) return outputFile; } - private Credential buildSigningCredential(Terminal terminal, OptionSet options, Environment env) throws - Exception { + private Credential buildSigningCredential(Terminal terminal, OptionSet options, Environment env) throws Exception { X509Certificate signingCertificate; PrivateKey signingKey; char[] password = getChars(keyPasswordSpec.value(options)); if (options.has(signingPkcs12PathSpec)) { Path p12Path = resolvePath(signingPkcs12PathSpec.value(options)); - Map keys = withPassword("certificate bundle (" + p12Path + ")", password, - terminal, keyPassword -> CertParsingUtils.readPkcs12KeyPairs(p12Path, keyPassword, a -> keyPassword)); + Map keys = withPassword( + "certificate bundle (" + p12Path + ")", + password, + terminal, + keyPassword -> CertParsingUtils.readPkcs12KeyPairs(p12Path, keyPassword, a -> keyPassword) + ); if (keys.size() != 1) { - throw new IllegalArgumentException("expected a single key in file [" + p12Path.toAbsolutePath() + "] but found [" + - keys.size() + "]"); + throw new IllegalArgumentException( + "expected a single key in file [" + p12Path.toAbsolutePath() + "] but found [" + keys.size() + "]" + ); } final Map.Entry pair = keys.entrySet().iterator().next(); signingCertificate = (X509Certificate) pair.getKey(); @@ -305,8 +327,12 @@ private Credential buildSigningCredential(Terminal terminal, OptionSet options, return new BasicX509Credential(signingCertificate, signingKey); } - private static T withPassword(String description, char[] password, Terminal terminal, - CheckedFunction body) throws E { + private static T withPassword( + String description, + char[] password, + Terminal terminal, + CheckedFunction body + ) throws E { if (password == null) { char[] promptedValue = terminal.readSecret("Enter password for " + description + " : "); try { @@ -340,13 +366,16 @@ private static PrivateKey readSigningKey(Path path, char[] password, Terminal te } } } + private void validateXml(Terminal terminal, Path xml) throws Exception { try (InputStream xmlInput = Files.newInputStream(xml)) { SamlUtils.validate(xmlInput, METADATA_SCHEMA); terminal.println(Terminal.Verbosity.VERBOSE, "The generated metadata file conforms to the SAML metadata schema"); } catch (SAXException e) { - terminal.errorPrintln(Terminal.Verbosity.SILENT, "Error - The generated metadata file does not conform to the " + - "SAML metadata schema"); + terminal.errorPrintln( + Terminal.Verbosity.SILENT, + "Error - The generated metadata file does not conform to the " + "SAML metadata schema" + ); terminal.errorPrintln("While validating " + xml.toString() + " the follow errors were found:"); printExceptions(terminal, e); throw new UserException(ExitCodes.CODE_ERROR, "Generated metadata is not valid"); @@ -440,19 +469,24 @@ private RealmConfig findRealm(Terminal terminal, OptionSet options, Environment throw new UserException(ExitCodes.CONFIG, "Realm '" + name + "' is not a SAML realm (is '" + identifier.getType() + "')"); } } else { - final List> saml = realms.entrySet().stream() - .filter(entry -> isSamlRealm(entry.getKey())) - .collect(Collectors.toList()); + final List> saml = realms.entrySet() + .stream() + .filter(entry -> isSamlRealm(entry.getKey())) + .collect(Collectors.toList()); if (saml.isEmpty()) { throw new UserException(ExitCodes.CONFIG, "There is no SAML realm configured in " + env.configFile()); } if (saml.size() > 1) { terminal.errorPrintln("Using configuration in " + env.configFile()); - terminal.errorPrintln("Found multiple SAML realms: " - + saml.stream().map(Map.Entry::getKey).map(Object::toString).collect(Collectors.joining(", "))); + terminal.errorPrintln( + "Found multiple SAML realms: " + + saml.stream().map(Map.Entry::getKey).map(Object::toString).collect(Collectors.joining(", ")) + ); terminal.errorPrintln("Use the -" + optionName(realmSpec) + " option to specify an explicit realm"); - throw new UserException(ExitCodes.CONFIG, - "Found multiple SAML realms, please specify one with '-" + optionName(realmSpec) + "'"); + throw new UserException( + ExitCodes.CONFIG, + "Found multiple SAML realms, please specify one with '-" + optionName(realmSpec) + "'" + ); } final Map.Entry entry = saml.get(0); terminal.println("Building metadata for SAML realm " + entry.getKey()); @@ -464,7 +498,7 @@ private String optionName(OptionSpec spec) { return spec.options().get(0); } - private RealmConfig buildRealm(RealmConfig.RealmIdentifier identifier, Environment env, Settings globalSettings ) { + private RealmConfig buildRealm(RealmConfig.RealmIdentifier identifier, Environment env, Settings globalSettings) { return new RealmConfig(identifier, globalSettings, env, new ThreadContext(globalSettings)); } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlNameId.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlNameId.java index 926f5322c4f00..a0df1f88ae225 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlNameId.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlNameId.java @@ -46,8 +46,13 @@ static SamlNameId fromXml(NameID name) { if (name == null) { return null; } - return new SamlNameId(name.getFormat(), name.getValue(), name.getNameQualifier(), - name.getSPNameQualifier(), name.getSPProvidedID()); + return new SamlNameId( + name.getFormat(), + name.getValue(), + name.getNameQualifier(), + name.getSPNameQualifier(), + name.getSPProvidedID() + ); } static SamlNameId forSubject(Subject subject) { diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlObjectHandler.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlObjectHandler.java index fe8120c10328a..47eb71ccd719b 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlObjectHandler.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlObjectHandler.java @@ -69,6 +69,7 @@ import java.util.stream.Collectors; import java.util.zip.Inflater; import java.util.zip.InflaterInputStream; + import javax.xml.parsers.DocumentBuilder; import static org.elasticsearch.xpack.security.authc.saml.SamlUtils.samlException; @@ -78,10 +79,11 @@ public class SamlObjectHandler { protected static final String SAML_NAMESPACE = "urn:oasis:names:tc:SAML:2.0:protocol"; - private static final String[] XSD_FILES = new String[] { "/org/elasticsearch/xpack/security/authc/saml/saml-schema-protocol-2.0.xsd", - "/org/elasticsearch/xpack/security/authc/saml/saml-schema-assertion-2.0.xsd", - "/org/elasticsearch/xpack/security/authc/saml/xenc-schema.xsd", - "/org/elasticsearch/xpack/security/authc/saml/xmldsig-core-schema.xsd" }; + private static final String[] XSD_FILES = new String[] { + "/org/elasticsearch/xpack/security/authc/saml/saml-schema-protocol-2.0.xsd", + "/org/elasticsearch/xpack/security/authc/saml/saml-schema-assertion-2.0.xsd", + "/org/elasticsearch/xpack/security/authc/saml/xenc-schema.xsd", + "/org/elasticsearch/xpack/security/authc/saml/xmldsig-core-schema.xsd" }; private static final ThreadLocal THREAD_LOCAL_DOCUMENT_BUILDER = ThreadLocal.withInitial(() -> { try { @@ -116,17 +118,29 @@ public SamlObjectHandler(Clock clock, IdpConfiguration idp, SpConfiguration sp, } private KeyInfoCredentialResolver createResolverForEncryptionKeys() { - final CollectionKeyInfoCredentialResolver collectionKeyInfoCredentialResolver = - new CollectionKeyInfoCredentialResolver(Collections.unmodifiableCollection(sp.getEncryptionCredentials())); - final LocalKeyInfoCredentialResolver localKeyInfoCredentialResolver = - new LocalKeyInfoCredentialResolver(Arrays.asList(new InlineX509DataProvider(), new KeyInfoReferenceProvider(), - new RSAKeyValueProvider(), new DEREncodedKeyValueProvider()), collectionKeyInfoCredentialResolver); + final CollectionKeyInfoCredentialResolver collectionKeyInfoCredentialResolver = new CollectionKeyInfoCredentialResolver( + Collections.unmodifiableCollection(sp.getEncryptionCredentials()) + ); + final LocalKeyInfoCredentialResolver localKeyInfoCredentialResolver = new LocalKeyInfoCredentialResolver( + Arrays.asList( + new InlineX509DataProvider(), + new KeyInfoReferenceProvider(), + new RSAKeyValueProvider(), + new DEREncodedKeyValueProvider() + ), + collectionKeyInfoCredentialResolver + ); return new ChainingKeyInfoCredentialResolver(Arrays.asList(localKeyInfoCredentialResolver, collectionKeyInfoCredentialResolver)); } private EncryptedKeyResolver createResolverForEncryptedKeyElements() { - return new ChainingEncryptedKeyResolver(Arrays.asList(new InlineEncryptedKeyResolver(), - new SimpleRetrievalMethodEncryptedKeyResolver(), new SimpleKeyInfoReferenceEncryptedKeyResolver())); + return new ChainingEncryptedKeyResolver( + Arrays.asList( + new InlineEncryptedKeyResolver(), + new SimpleRetrievalMethodEncryptedKeyResolver(), + new SimpleKeyInfoReferenceEncryptedKeyResolver() + ) + ); } protected SpConfiguration getSpConfiguration() { @@ -134,8 +148,7 @@ protected SpConfiguration getSpConfiguration() { } protected String describe(X509Certificate certificate) { - return "X509Certificate{Subject=" + certificate.getSubjectDN() + "; SerialNo=" + - certificate.getSerialNumber().toString(16) + "}"; + return "X509Certificate{Subject=" + certificate.getSubjectDN() + "; SerialNo=" + certificate.getSerialNumber().toString(16) + "}"; } protected String describe(Collection credentials) { @@ -156,8 +169,14 @@ void validateSignature(Signature signature) { return AccessController.doPrivileged((PrivilegedExceptionAction) () -> { try (RestorableContextClassLoader ignore = new RestorableContextClassLoader(SignatureValidator.class)) { SignatureValidator.validate(signature, credential); - logger.debug(() -> new ParameterizedMessage("SAML Signature [{}] matches credentials [{}] [{}]", - signatureText, credential.getEntityId(), credential.getPublicKey())); + logger.debug( + () -> new ParameterizedMessage( + "SAML Signature [{}] matches credentials [{}] [{}]", + signatureText, + credential.getEntityId(), + credential.getPublicKey() + ) + ); return true; } catch (PrivilegedActionException e) { logger.warn("SecurityException while attempting to validate SAML signature", e); @@ -179,8 +198,15 @@ protected void checkIdpSignature(CheckedFunction try { return check.apply(credential); } catch (SignatureException | SecurityException e) { - logger.debug(() -> new ParameterizedMessage("SAML Signature [{}] does not match credentials [{}] [{}] -- {}", - signatureText, credential.getEntityId(), credential.getPublicKey(), e)); + logger.debug( + () -> new ParameterizedMessage( + "SAML Signature [{}] does not match credentials [{}] [{}] -- {}", + signatureText, + credential.getEntityId(), + credential.getPublicKey(), + e + ) + ); logger.trace("SAML Signature failure caused by", e); return false; } catch (Exception e) { @@ -198,45 +224,51 @@ protected void checkIdpSignature(CheckedFunction * Constructs a SAML specific exception with a consistent message regarding SAML Signature validation failures */ private ElasticsearchSecurityException samlSignatureException(List credentials, String signature, Exception cause) { - logger.warn("The XML Signature of this SAML message cannot be validated. Please verify that the saml realm uses the correct SAML" + - "metadata file/URL for this Identity Provider"); + logger.warn( + "The XML Signature of this SAML message cannot be validated. Please verify that the saml realm uses the correct SAML" + + "metadata file/URL for this Identity Provider" + ); final String msg = "SAML Signature [{}] could not be validated against [{}]"; return samlException(msg, cause, signature, describeCredentials(credentials)); } private ElasticsearchSecurityException samlSignatureException(List credentials, String signature) { - logger.warn("The XML Signature of this SAML message cannot be validated. Please verify that the saml realm uses the correct SAML" + - "metadata file/URL for this Identity Provider"); + logger.warn( + "The XML Signature of this SAML message cannot be validated. Please verify that the saml realm uses the correct SAML" + + "metadata file/URL for this Identity Provider" + ); final String msg = "SAML Signature [{}] could not be validated against [{}]"; return samlException(msg, signature, describeCredentials(credentials)); } private String describeCredentials(List credentials) { - return credentials.stream() - .map(c -> { - if (c == null) { - return ""; - } - byte[] encoded; - if (c instanceof X509Credential) { - X509Credential x = (X509Credential) c; - try { - encoded = x.getEntityCertificate().getEncoded(); - } catch (CertificateEncodingException e) { - encoded = c.getPublicKey().getEncoded(); - } - } else { - encoded = c.getPublicKey().getEncoded(); - } - return Base64.getEncoder().encodeToString(encoded).substring(0, 64) + "..."; - }) - .collect(Collectors.joining(",")); + return credentials.stream().map(c -> { + if (c == null) { + return ""; + } + byte[] encoded; + if (c instanceof X509Credential) { + X509Credential x = (X509Credential) c; + try { + encoded = x.getEntityCertificate().getEncoded(); + } catch (CertificateEncodingException e) { + encoded = c.getPublicKey().getEncoded(); + } + } else { + encoded = c.getPublicKey().getEncoded(); + } + return Base64.getEncoder().encodeToString(encoded).substring(0, 64) + "..."; + }).collect(Collectors.joining(",")); } protected void checkIssuer(Issuer issuer, XMLObject parent) { if (issuer == null) { - throw samlException("Element {} ({}) has no issuer, but expected [{}]", - parent.getElementQName(), text(parent, 16), idp.getEntityId()); + throw samlException( + "Element {} ({}) has no issuer, but expected [{}]", + parent.getElementQName(), + text(parent, 16), + idp.getEntityId() + ); } if (idp.getEntityId().equals(issuer.getValue()) == false) { throw samlException("SAML Issuer [{}] does not match expected value [{}]", issuer.getValue(), idp.getEntityId()); @@ -256,8 +288,11 @@ T buildXmlObject(Element element, Class type) { try { Unmarshaller unmarshaller = unmarshallerFactory.getUnmarshaller(element); if (unmarshaller == null) { - throw samlException("XML element [{}] cannot be unmarshalled to SAML type [{}] (no unmarshaller)", - element.getTagName(), type); + throw samlException( + "XML element [{}] cannot be unmarshalled to SAML type [{}] (no unmarshaller)", + element.getTagName(), + type + ); } final XMLObject object = unmarshaller.unmarshall(element); if (type.isInstance(object)) { @@ -348,12 +383,24 @@ private void validateSignature(String inputString, String signatureAlgorithm, St final String signatureText = Strings.cleanTruncate(signature, 32); checkIdpSignature(credential -> { if (XMLSigningUtil.verifyWithURI(credential, signatureAlgorithm, sigBytes, inputBytes)) { - logger.debug(() -> new ParameterizedMessage("SAML Signature [{}] matches credentials [{}] [{}]", - signatureText, credential.getEntityId(), credential.getPublicKey())); + logger.debug( + () -> new ParameterizedMessage( + "SAML Signature [{}] matches credentials [{}] [{}]", + signatureText, + credential.getEntityId(), + credential.getPublicKey() + ) + ); return true; } else { - logger.debug(() -> new ParameterizedMessage("SAML Signature [{}] failed against credentials [{}] [{}]", - signatureText, credential.getEntityId(), credential.getPublicKey())); + logger.debug( + () -> new ParameterizedMessage( + "SAML Signature [{}] failed against credentials [{}] [{}]", + signatureText, + credential.getEntityId(), + credential.getPublicKey() + ) + ); return false; } }, signatureText); @@ -370,9 +417,11 @@ protected byte[] decodeBase64(String content) { protected byte[] inflate(byte[] bytes) { Inflater inflater = new Inflater(true); - try (ByteArrayInputStream in = new ByteArrayInputStream(bytes); - InflaterInputStream inflate = new InflaterInputStream(in, inflater); - ByteArrayOutputStream out = new ByteArrayOutputStream(bytes.length * 3 / 2)) { + try ( + ByteArrayInputStream in = new ByteArrayInputStream(bytes); + InflaterInputStream inflate = new InflaterInputStream(in, inflater); + ByteArrayOutputStream out = new ByteArrayOutputStream(bytes.length * 3 / 2) + ) { Streams.copy(inflate, out); return out.toByteArray(); } catch (IOException e) { diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlRealm.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlRealm.java index 2efb925a4dbe5..06ee389a32161 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlRealm.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlRealm.java @@ -10,6 +10,7 @@ import net.shibboleth.utilities.java.support.resolver.CriteriaSet; import net.shibboleth.utilities.java.support.resolver.ResolverException; import net.shibboleth.utilities.java.support.xml.BasicParserPool; + import org.apache.http.client.HttpClient; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.impl.client.HttpClientBuilder; @@ -20,20 +21,20 @@ import org.elasticsearch.ExceptionsHelper; import org.elasticsearch.SpecialPermission; import org.elasticsearch.action.ActionListener; -import org.elasticsearch.core.CheckedRunnable; import org.elasticsearch.common.Strings; -import org.elasticsearch.core.SuppressForbidden; -import org.elasticsearch.core.Tuple; -import org.elasticsearch.core.Releasable; -import org.elasticsearch.core.Releasables; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.SettingsException; import org.elasticsearch.common.ssl.SslConfiguration; import org.elasticsearch.common.ssl.SslKeyConfig; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.CollectionUtils; import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.common.util.set.Sets; +import org.elasticsearch.core.CheckedRunnable; +import org.elasticsearch.core.Releasable; +import org.elasticsearch.core.Releasables; +import org.elasticsearch.core.SuppressForbidden; +import org.elasticsearch.core.TimeValue; +import org.elasticsearch.core.Tuple; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.watcher.FileChangesListener; import org.elasticsearch.watcher.FileWatcher; @@ -76,8 +77,6 @@ import org.opensaml.xmlsec.keyinfo.impl.BasicProviderKeyInfoCredentialResolver; import org.opensaml.xmlsec.keyinfo.impl.provider.InlineX509DataProvider; -import javax.net.ssl.HostnameVerifier; -import javax.net.ssl.X509KeyManager; import java.io.IOException; import java.nio.file.Path; import java.security.AccessController; @@ -102,6 +101,9 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import javax.net.ssl.HostnameVerifier; +import javax.net.ssl.X509KeyManager; + import static org.elasticsearch.xpack.core.security.authc.saml.SamlRealmSettings.CLOCK_SKEW; import static org.elasticsearch.xpack.core.security.authc.saml.SamlRealmSettings.DN_ATTRIBUTE; import static org.elasticsearch.xpack.core.security.authc.saml.SamlRealmSettings.ENCRYPTION_KEY_ALIAS; @@ -176,17 +178,26 @@ public final class SamlRealm extends Realm implements Releasable { * This is not a constructor as it needs to initialise a number of components before delegating to * {@link #SamlRealm} */ - public static SamlRealm create(RealmConfig config, SSLService sslService, ResourceWatcherService watcherService, - UserRoleMapper roleMapper) throws Exception { + public static SamlRealm create( + RealmConfig config, + SSLService sslService, + ResourceWatcherService watcherService, + UserRoleMapper roleMapper + ) throws Exception { SamlUtils.initialize(logger); if (TokenService.isTokenServiceEnabled(config.settings()) == false) { - throw new IllegalStateException("SAML requires that the token service be enabled (" - + XPackSettings.TOKEN_SERVICE_ENABLED_SETTING.getKey() + ")"); + throw new IllegalStateException( + "SAML requires that the token service be enabled (" + XPackSettings.TOKEN_SERVICE_ENABLED_SETTING.getKey() + ")" + ); } - final Tuple> tuple - = initializeResolver(logger, config, sslService, watcherService); + final Tuple> tuple = initializeResolver( + logger, + config, + sslService, + watcherService + ); final AbstractReloadingMetadataResolver metadataResolver = tuple.v1(); final Supplier idpDescriptor = tuple.v2(); @@ -196,13 +207,23 @@ public static SamlRealm create(RealmConfig config, SSLService sslService, Resour final IdpConfiguration idpConfiguration = getIdpConfiguration(config, metadataResolver, idpDescriptor); final TimeValue maxSkew = config.getSetting(CLOCK_SKEW); final SamlAuthenticator authenticator = new SamlAuthenticator(clock, idpConfiguration, serviceProvider, maxSkew); - final SamlLogoutRequestHandler logoutHandler = - new SamlLogoutRequestHandler(clock, idpConfiguration, serviceProvider, maxSkew); - final SamlLogoutResponseHandler logoutResponseHandler = - new SamlLogoutResponseHandler(clock, idpConfiguration, serviceProvider, maxSkew); - - final SamlRealm realm = new SamlRealm(config, roleMapper, authenticator, logoutHandler, - logoutResponseHandler, idpDescriptor, serviceProvider); + final SamlLogoutRequestHandler logoutHandler = new SamlLogoutRequestHandler(clock, idpConfiguration, serviceProvider, maxSkew); + final SamlLogoutResponseHandler logoutResponseHandler = new SamlLogoutResponseHandler( + clock, + idpConfiguration, + serviceProvider, + maxSkew + ); + + final SamlRealm realm = new SamlRealm( + config, + roleMapper, + authenticator, + logoutHandler, + logoutResponseHandler, + idpDescriptor, + serviceProvider + ); // the metadata resolver needs to be destroyed since it runs a timer task in the background and destroying stops it! realm.releasables.add(() -> metadataResolver.destroy()); @@ -222,7 +243,8 @@ public SpConfiguration getServiceProvider() { SamlLogoutRequestHandler logoutHandler, SamlLogoutResponseHandler logoutResponseHandler, Supplier idpDescriptor, - SpConfiguration spConfiguration) throws Exception { + SpConfiguration spConfiguration + ) throws Exception { super(config); this.roleMapper = roleMapper; @@ -233,8 +255,11 @@ public SpConfiguration getServiceProvider() { this.idpDescriptor = idpDescriptor; this.serviceProvider = spConfiguration; - this.nameIdPolicy = new SamlAuthnRequestBuilder.NameIDPolicySettings(config.getSetting(NAMEID_FORMAT), - config.getSetting(NAMEID_ALLOW_CREATE), config.getSetting(NAMEID_SP_QUALIFIER)); + this.nameIdPolicy = new SamlAuthnRequestBuilder.NameIDPolicySettings( + config.getSetting(NAMEID_FORMAT), + config.getSetting(NAMEID_ALLOW_CREATE), + config.getSetting(NAMEID_SP_QUALIFIER) + ); this.forceAuthn = config.getSetting(FORCE_AUTHN, () -> null); this.useSingleLogout = config.getSetting(IDP_SINGLE_LOGOUT); this.populateUserMetadata = config.getSetting(POPULATE_USER_METADATA); @@ -259,14 +284,18 @@ public void initialize(Iterable realms, XPackLicenseState licenseState) { static String require(RealmConfig config, Setting.AffixSetting setting) { final String value = config.getSetting(setting); if (value.isEmpty()) { - throw new IllegalArgumentException("The configuration setting [" + RealmSettings.getFullSettingKey(config, setting) - + "] is required"); + throw new IllegalArgumentException( + "The configuration setting [" + RealmSettings.getFullSettingKey(config, setting) + "] is required" + ); } return value; } - private static IdpConfiguration getIdpConfiguration(RealmConfig config, MetadataResolver metadataResolver, - Supplier idpDescriptor) { + private static IdpConfiguration getIdpConfiguration( + RealmConfig config, + MetadataResolver metadataResolver, + Supplier idpDescriptor + ) { final MetadataCredentialResolver resolver = new MetadataCredentialResolver(); final PredicateRoleDescriptorResolver roleDescriptorResolver = new PredicateRoleDescriptorResolver(metadataResolver); @@ -285,10 +314,13 @@ private static IdpConfiguration getIdpConfiguration(RealmConfig config, Metadata final String entityID = idpDescriptor.get().getEntityID(); return new IdpConfiguration(entityID, () -> { try { - final Iterable credentials = resolver.resolve(new CriteriaSet( + final Iterable credentials = resolver.resolve( + new CriteriaSet( new EntityIdCriterion(entityID), new EntityRoleCriterion(IDPSSODescriptor.DEFAULT_ELEMENT_NAME), - new UsageCriterion(UsageType.SIGNING))); + new UsageCriterion(UsageType.SIGNING) + ) + ); return CollectionUtils.iterableAsArrayList(credentials); } catch (ResolverException e) { throw new IllegalStateException("Cannot resolve SAML IDP credentials resolver for realm " + config.name(), e); @@ -301,25 +333,40 @@ static SpConfiguration getSpConfiguration(RealmConfig config) throws IOException final String assertionConsumerServiceURL = require(config, SP_ACS); final String logoutUrl = config.getSetting(SP_LOGOUT); final List reqAuthnCtxClassRef = config.getSetting(REQUESTED_AUTHN_CONTEXT_CLASS_REF); - return new SpConfiguration(serviceProviderId, assertionConsumerServiceURL, - logoutUrl, buildSigningConfiguration(config), buildEncryptionCredential(config), reqAuthnCtxClassRef); + return new SpConfiguration( + serviceProviderId, + assertionConsumerServiceURL, + logoutUrl, + buildSigningConfiguration(config), + buildEncryptionCredential(config), + reqAuthnCtxClassRef + ); } - // Package-private for testing static List buildEncryptionCredential(RealmConfig config) throws IOException, GeneralSecurityException { - return buildCredential(config, - RealmSettings.realmSettingPrefix(config.identifier()) + ENCRYPTION_SETTING_KEY, - ENCRYPTION_KEY_ALIAS, true); + return buildCredential( + config, + RealmSettings.realmSettingPrefix(config.identifier()) + ENCRYPTION_SETTING_KEY, + ENCRYPTION_KEY_ALIAS, + true + ); } static SigningConfiguration buildSigningConfiguration(RealmConfig config) throws IOException, GeneralSecurityException { - final List credentials = buildCredential(config, - RealmSettings.realmSettingPrefix(config.identifier()) + SIGNING_SETTING_KEY, SIGNING_KEY_ALIAS, false); + final List credentials = buildCredential( + config, + RealmSettings.realmSettingPrefix(config.identifier()) + SIGNING_SETTING_KEY, + SIGNING_KEY_ALIAS, + false + ); if (credentials == null || credentials.isEmpty()) { if (config.hasSetting(SIGNING_MESSAGE_TYPES)) { - throw new IllegalArgumentException("The setting [" + RealmSettings.getFullSettingKey(config, SIGNING_MESSAGE_TYPES) - + "] cannot be specified if there are no signing credentials"); + throw new IllegalArgumentException( + "The setting [" + + RealmSettings.getFullSettingKey(config, SIGNING_MESSAGE_TYPES) + + "] cannot be specified if there are no signing credentials" + ); } else { return new SigningConfiguration(Collections.emptySet(), null); } @@ -329,8 +376,12 @@ static SigningConfiguration buildSigningConfiguration(RealmConfig config) throws } } - private static List buildCredential(RealmConfig config, String prefix, Setting.AffixSetting aliasSetting, - boolean allowMultiple) { + private static List buildCredential( + RealmConfig config, + String prefix, + Setting.AffixSetting aliasSetting, + boolean allowMultiple + ) { final SslKeyConfig keyConfig = CertParsingUtils.createKeyConfig(config.settings(), prefix, config.env(), false); if (keyConfig.hasKeyMaterial() == false) { return null; @@ -350,14 +401,15 @@ private static List buildCredential(RealmConfig config, String p } if (aliases.isEmpty()) { - throw new IllegalArgumentException( - "The configured key store for " + prefix - + " does not contain any RSA key pairs"); + throw new IllegalArgumentException("The configured key store for " + prefix + " does not contain any RSA key pairs"); } else if (allowMultiple == false && aliases.size() > 1) { throw new IllegalArgumentException( - "The configured key store for " + prefix - + " has multiple keys but no alias has been specified (from setting " - + RealmSettings.getFullSettingKey(config, aliasSetting) + ")"); + "The configured key store for " + + prefix + + " has multiple keys but no alias has been specified (from setting " + + RealmSettings.getFullSettingKey(config, aliasSetting) + + ")" + ); } } else { aliases.add(configuredAlias); @@ -367,18 +419,29 @@ private static List buildCredential(RealmConfig config, String p for (String alias : aliases) { if (keyManager.getPrivateKey(alias) == null) { throw new IllegalArgumentException( - "The configured key store for " + prefix - + " does not have a key associated with alias [" + alias + "] " - + ((Strings.isNullOrEmpty(configuredAlias) == false) - ? "(from setting " + RealmSettings.getFullSettingKey(config, aliasSetting) + ")" - : "")); + "The configured key store for " + + prefix + + " does not have a key associated with alias [" + + alias + + "] " + + ((Strings.isNullOrEmpty(configuredAlias) == false) + ? "(from setting " + RealmSettings.getFullSettingKey(config, aliasSetting) + ")" + : "") + ); } final String keyType = keyManager.getPrivateKey(alias).getAlgorithm(); if (keyType.equals("RSA") == false) { - throw new IllegalArgumentException("The key associated with alias [" + alias + "] " + "(from setting " - + RealmSettings.getFullSettingKey(config, aliasSetting) + ") uses unsupported key algorithm type [" + keyType - + "], only RSA is supported"); + throw new IllegalArgumentException( + "The key associated with alias [" + + alias + + "] " + + "(from setting " + + RealmSettings.getFullSettingKey(config, aliasSetting) + + ") uses unsupported key algorithm type [" + + keyType + + "], only RSA is supported" + ); } credentials.add(new X509KeyManagerX509CredentialAdapter(keyManager, alias)); } @@ -443,8 +506,12 @@ public void authenticate(AuthenticationToken authenticationToken, ActionListener private void buildUser(SamlAttributes attributes, ActionListener> baseListener) { final String principal = resolveSingleValueAttribute(attributes, principalAttribute, PRINCIPAL_ATTRIBUTE.name(config)); if (Strings.isNullOrEmpty(principal)) { - final String msg = - principalAttribute + " not found in saml attributes" + attributes.attributes() + " or NameID [" + attributes.name() + "]"; + final String msg = principalAttribute + + " not found in saml attributes" + + attributes.attributes() + + " or NameID [" + + attributes.name() + + "]"; baseListener.onResponse(AuthenticationResult.unsuccessful(msg, null)); return; } @@ -534,10 +601,12 @@ public void lookupUser(String username, ActionListener listener) { listener.onResponse(null); } - static Tuple> initializeResolver(Logger logger, RealmConfig config, - SSLService sslService, - ResourceWatcherService watcherService) - throws ResolverException, ComponentInitializationException, PrivilegedActionException, IOException { + static Tuple> initializeResolver( + Logger logger, + RealmConfig config, + SSLService sslService, + ResourceWatcherService watcherService + ) throws ResolverException, ComponentInitializationException, PrivilegedActionException, IOException { final String metadataUrl = require(config, IDP_METADATA_PATH); if (metadataUrl.startsWith("http://")) { throw new IllegalArgumentException("The [http] protocol is not supported as it is insecure. Use [https] instead"); @@ -548,10 +617,11 @@ static Tuple> init } } - private static Tuple> parseHttpMetadata(String metadataUrl, - RealmConfig config, - SSLService sslService) - throws ResolverException, ComponentInitializationException, PrivilegedActionException { + private static Tuple> parseHttpMetadata( + String metadataUrl, + RealmConfig config, + SSLService sslService + ) throws ResolverException, ComponentInitializationException, PrivilegedActionException { final String entityId = require(config, IDP_ENTITY_ID); HttpClientBuilder builder = HttpClientBuilder.create(); @@ -573,8 +643,9 @@ private static Tuple) - () -> resolveEntityDescriptor(resolver, entityId, metadataUrl)); + return AccessController.doPrivileged( + (PrivilegedExceptionAction) () -> resolveEntityDescriptor(resolver, entityId, metadataUrl) + ); } catch (PrivilegedActionException e) { throw ExceptionsHelper.convertToRuntime((Exception) ExceptionsHelper.unwrapCause(e)); } @@ -591,7 +662,8 @@ private static final class PrivilegedHTTPMetadataResolver extends HTTPMetadataRe protected byte[] fetchMetadata() throws ResolverException { try { return AccessController.doPrivileged( - (PrivilegedExceptionAction) () -> PrivilegedHTTPMetadataResolver.super.fetchMetadata()); + (PrivilegedExceptionAction) () -> PrivilegedHTTPMetadataResolver.super.fetchMetadata() + ); } catch (final PrivilegedActionException e) { throw (ResolverException) e.getCause(); } @@ -601,16 +673,21 @@ protected byte[] fetchMetadata() throws ResolverException { @SuppressForbidden(reason = "uses toFile") private static Tuple> parseFileSystemMetadata( - Logger logger, String metadataPath, RealmConfig config, ResourceWatcherService watcherService) - throws ResolverException, ComponentInitializationException, IOException, PrivilegedActionException { + Logger logger, + String metadataPath, + RealmConfig config, + ResourceWatcherService watcherService + ) throws ResolverException, ComponentInitializationException, IOException, PrivilegedActionException { final String entityId = require(config, IDP_ENTITY_ID); final Path path = config.env().configFile().resolve(metadataPath); final FilesystemMetadataResolver resolver = new FilesystemMetadataResolver(path.toFile()); if (config.hasSetting(IDP_METADATA_HTTP_REFRESH)) { - logger.info("Ignoring setting [{}] because the IdP metadata is being loaded from a file", - RealmSettings.getFullSettingKey(config, IDP_METADATA_HTTP_REFRESH)); + logger.info( + "Ignoring setting [{}] because the IdP metadata is being loaded from a file", + RealmSettings.getFullSettingKey(config, IDP_METADATA_HTTP_REFRESH) + ); } // We don't want to rely on the internal OpenSAML refresh timer, but we can't turn it off, so just set it to run once a day. @@ -626,8 +703,11 @@ private static Tuple(resolver, () -> resolveEntityDescriptor(resolver, entityId, path.toString())); } - private static EntityDescriptor resolveEntityDescriptor(AbstractReloadingMetadataResolver resolver, String entityId, - String sourceLocation) { + private static EntityDescriptor resolveEntityDescriptor( + AbstractReloadingMetadataResolver resolver, + String entityId, + String sourceLocation + ) { try { final EntityDescriptor descriptor = resolver.resolveSingle(new CriteriaSet(new EntityIdCriterion(entityId))); if (descriptor == null) { @@ -639,14 +719,13 @@ private static EntityDescriptor resolveEntityDescriptor(AbstractReloadingMetadat } } - @Override public void close() { Releasables.close(releasables); } private static void initialiseResolver(AbstractReloadingMetadataResolver resolver, RealmConfig config) - throws ComponentInitializationException, PrivilegedActionException { + throws ComponentInitializationException, PrivilegedActionException { resolver.setRequireValidMetadata(true); BasicParserPool pool = new BasicParserPool(); pool.initialize(); @@ -669,14 +748,12 @@ public String assertionConsumerServiceURL() { public AuthnRequest buildAuthenticationRequest() { final AuthnRequest authnRequest = new SamlAuthnRequestBuilder( - serviceProvider, - SAMLConstants.SAML2_POST_BINDING_URI, - idpDescriptor.get(), - SAMLConstants.SAML2_REDIRECT_BINDING_URI, - Clock.systemUTC()) - .nameIDPolicy(nameIdPolicy) - .forceAuthn(forceAuthn) - .build(); + serviceProvider, + SAMLConstants.SAML2_POST_BINDING_URI, + idpDescriptor.get(), + SAMLConstants.SAML2_REDIRECT_BINDING_URI, + Clock.systemUTC() + ).nameIDPolicy(nameIdPolicy).forceAuthn(forceAuthn).build(); if (logger.isTraceEnabled()) { logger.trace("Constructed SAML Authentication Request: {}", SamlUtils.getXmlContent(authnRequest, true)); } @@ -692,7 +769,12 @@ public AuthnRequest buildAuthenticationRequest() { public LogoutRequest buildLogoutRequest(NameID nameId, String session) { if (useSingleLogout) { final LogoutRequest logoutRequest = new SamlLogoutRequestMessageBuilder( - Clock.systemUTC(), serviceProvider, idpDescriptor.get(), nameId, session).build(); + Clock.systemUTC(), + serviceProvider, + idpDescriptor.get(), + nameId, + session + ).build(); if (logoutRequest != null && logger.isTraceEnabled()) { logger.trace("Constructed SAML Logout Request: {}", SamlUtils.getXmlContent(logoutRequest, true)); } @@ -708,7 +790,12 @@ public LogoutRequest buildLogoutRequest(NameID nameId, String session) { */ public LogoutResponse buildLogoutResponse(String inResponseTo) { final LogoutResponse logoutResponse = new SamlLogoutResponseBuilder( - Clock.systemUTC(), serviceProvider, idpDescriptor.get(), inResponseTo, StatusCode.SUCCESS).build(); + Clock.systemUTC(), + serviceProvider, + idpDescriptor.get(), + inResponseTo, + StatusCode.SUCCESS + ).build(); if (logoutResponse != null && logger.isTraceEnabled()) { logger.trace("Constructed SAML Logout Response: {}", SamlUtils.getXmlContent(logoutResponse, true)); } @@ -783,45 +870,63 @@ public String toString() { return name; } - static AttributeParser forSetting(Logger logger, SamlRealmSettings.AttributeSetting setting, RealmConfig realmConfig, - boolean required) { + static AttributeParser forSetting( + Logger logger, + SamlRealmSettings.AttributeSetting setting, + RealmConfig realmConfig, + boolean required + ) { if (realmConfig.hasSetting(setting.getAttribute())) { String attributeName = realmConfig.getSetting(setting.getAttribute()); if (realmConfig.hasSetting(setting.getPattern())) { Pattern regex = Pattern.compile(realmConfig.getSetting(setting.getPattern())); return new AttributeParser( - "SAML Attribute [" + attributeName + "] with pattern [" + regex.pattern() + "] for [" - + setting.name(realmConfig) + "]", - attributes -> attributes.getAttributeValues(attributeName).stream().map(s -> { - final Matcher matcher = regex.matcher(s); - if (matcher.find() == false) { - logger.debug("Attribute [{}] is [{}], which does not match [{}]", attributeName, s, regex.pattern()); - return null; - } - final String value = matcher.group(1); - if (Strings.isNullOrEmpty(value)) { - logger.debug("Attribute [{}] is [{}], which does match [{}] but group(1) is empty", - attributeName, s, regex.pattern()); - return null; - } - return value; - }).filter(Objects::nonNull).collect(Collectors.toUnmodifiableList()) + "SAML Attribute [" + + attributeName + + "] with pattern [" + + regex.pattern() + + "] for [" + + setting.name(realmConfig) + + "]", + attributes -> attributes.getAttributeValues(attributeName).stream().map(s -> { + final Matcher matcher = regex.matcher(s); + if (matcher.find() == false) { + logger.debug("Attribute [{}] is [{}], which does not match [{}]", attributeName, s, regex.pattern()); + return null; + } + final String value = matcher.group(1); + if (Strings.isNullOrEmpty(value)) { + logger.debug( + "Attribute [{}] is [{}], which does match [{}] but group(1) is empty", + attributeName, + s, + regex.pattern() + ); + return null; + } + return value; + }).filter(Objects::nonNull).collect(Collectors.toUnmodifiableList()) ); } else { return new AttributeParser( - "SAML Attribute [" + attributeName + "] for [" + setting.name(realmConfig) + "]", - attributes -> attributes.getAttributeValues(attributeName)); + "SAML Attribute [" + attributeName + "] for [" + setting.name(realmConfig) + "]", + attributes -> attributes.getAttributeValues(attributeName) + ); } } else if (required) { - throw new SettingsException("Setting [" + RealmSettings.getFullSettingKey(realmConfig, setting.getAttribute()) - + "] is required"); + throw new SettingsException( + "Setting [" + RealmSettings.getFullSettingKey(realmConfig, setting.getAttribute()) + "] is required" + ); } else if (realmConfig.hasSetting(setting.getPattern())) { - throw new SettingsException("Setting [" + RealmSettings.getFullSettingKey(realmConfig, setting.getPattern()) - + "] cannot be set unless [" + RealmSettings.getFullSettingKey(realmConfig, setting.getAttribute()) - + "] is also set"); + throw new SettingsException( + "Setting [" + + RealmSettings.getFullSettingKey(realmConfig, setting.getPattern()) + + "] cannot be set unless [" + + RealmSettings.getFullSettingKey(realmConfig, setting.getAttribute()) + + "] is also set" + ); } else { - return new AttributeParser("No SAML attribute for [" + setting.name(realmConfig) + "]", - attributes -> List.of()); + return new AttributeParser("No SAML attribute for [" + setting.name(realmConfig) + "]", attributes -> List.of()); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlRedirect.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlRedirect.java index 022664dfdb06a..ee97ea437396d 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlRedirect.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlRedirect.java @@ -83,11 +83,12 @@ private String urlEncode(String param) throws UnsupportedEncodingException { return URLEncoder.encode(param, StandardCharsets.US_ASCII.name()); } - protected String deflateAndBase64Encode(SAMLObject message) - throws Exception { + protected String deflateAndBase64Encode(SAMLObject message) throws Exception { Deflater deflater = new Deflater(Deflater.DEFLATED, true); - try (ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); - DeflaterOutputStream deflaterStream = new DeflaterOutputStream(bytesOut, deflater)) { + try ( + ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); + DeflaterOutputStream deflaterStream = new DeflaterOutputStream(bytesOut, deflater) + ) { String messageStr = SamlUtils.toString(XMLObjectSupport.marshall(message)); deflaterStream.write(messageStr.getBytes(StandardCharsets.UTF_8)); deflaterStream.finish(); diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlResponseHandler.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlResponseHandler.java index 61835ee3f627d..ed3e88fa22ae6 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlResponseHandler.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlResponseHandler.java @@ -27,10 +27,16 @@ public SamlResponseHandler(Clock clock, IdpConfiguration idp, SpConfiguration sp protected void checkInResponseTo(StatusResponseType response, Collection allowedSamlRequestIds) { if (Strings.hasText(response.getInResponseTo()) && allowedSamlRequestIds.contains(response.getInResponseTo()) == false) { - logger.debug("The SAML Response with ID [{}] is unsolicited. A user might have used a stale URL or the Identity Provider " + - "incorrectly populates the InResponseTo attribute", response.getID()); - throw samlException("SAML content is in-response-to [{}] but expected one of {} ", - response.getInResponseTo(), allowedSamlRequestIds); + logger.debug( + "The SAML Response with ID [{}] is unsolicited. A user might have used a stale URL or the Identity Provider " + + "incorrectly populates the InResponseTo attribute", + response.getID() + ); + throw samlException( + "SAML content is in-response-to [{}] but expected one of {} ", + response.getInResponseTo(), + allowedSamlRequestIds + ); } } @@ -63,8 +69,14 @@ protected String getStatusCodeMessage(Status status) { protected void checkResponseDestination(StatusResponseType response, String spConfiguredUrl) { if (spConfiguredUrl.equals(response.getDestination()) == false) { if (response.isSigned() || Strings.hasText(response.getDestination())) { - throw samlException("SAML response " + response.getID() + " is for destination " + response.getDestination() - + " but this realm uses " + spConfiguredUrl); + throw samlException( + "SAML response " + + response.getID() + + " is for destination " + + response.getDestination() + + " but this realm uses " + + spConfiguredUrl + ); } } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlSpMetadataBuilder.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlSpMetadataBuilder.java index 54b951d8a7c72..fe403310b87eb 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlSpMetadataBuilder.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlSpMetadataBuilder.java @@ -176,8 +176,11 @@ public SamlSpMetadataBuilder encryptionCertificates(Collection * The certificate credential that should be used to send encrypted data to the service provider. */ public SamlSpMetadataBuilder encryptionCredentials(Collection credentials) { - return encryptionCertificates(credentials == null ? Collections.emptyList() - : credentials.stream().map(credential -> credential.getEntityCertificate()).collect(Collectors.toList())); + return encryptionCertificates( + credentials == null + ? Collections.emptyList() + : credentials.stream().map(credential -> credential.getEntityCertificate()).collect(Collectors.toList()) + ); } /** @@ -241,7 +244,7 @@ public EntityDescriptor build() throws Exception { if (organization != null) { descriptor.setOrganization(buildOrganization()); } - if(contacts.size() > 0) { + if (contacts.size() > 0) { contacts.forEach(c -> descriptor.getContactPersons().add(buildContact(c))); } @@ -274,9 +277,9 @@ private AttributeConsumingService buildAttributeConsumerService() { service.setIndex(1); service.setIsDefault(true); service.getNames().add(buildServiceName()); - attributeNames.forEach((name, friendlyName) -> { - service.getRequestedAttributes().add(buildRequestedAttribute(friendlyName, name)); - }); + attributeNames.forEach( + (name, friendlyName) -> { service.getRequestedAttributes().add(buildRequestedAttribute(friendlyName, name)); } + ); return service; } @@ -318,7 +321,7 @@ private List buildKeyDescriptors() throws CertificateEn if (signingCertificate != null) { keys.add(buildKeyDescriptor(signingCertificate, UsageType.SIGNING)); } - for( X509Certificate encryptionCertificate : encryptionCertificates) { + for (X509Certificate encryptionCertificate : encryptionCertificates) { keys.add(buildKeyDescriptor(encryptionCertificate, UsageType.ENCRYPTION)); } return keys; @@ -368,7 +371,6 @@ private ContactPerson buildContact(ContactInfo contact) { return person; } - public static class OrganizationInfo { public final String organizationName; public final String displayName; @@ -391,14 +393,15 @@ public OrganizationInfo(String organizationName, String displayName, String url) } public static class ContactInfo { - static final Map TYPES = - MapBuilder.newMapBuilder(new LinkedHashMap<>()) - .put(ContactPersonTypeEnumeration.ADMINISTRATIVE.toString(), ContactPersonTypeEnumeration.ADMINISTRATIVE) - .put(ContactPersonTypeEnumeration.BILLING.toString(), ContactPersonTypeEnumeration.BILLING) - .put(ContactPersonTypeEnumeration.SUPPORT.toString(), ContactPersonTypeEnumeration.SUPPORT) - .put(ContactPersonTypeEnumeration.TECHNICAL.toString(), ContactPersonTypeEnumeration.TECHNICAL) - .put(ContactPersonTypeEnumeration.OTHER.toString(), ContactPersonTypeEnumeration.OTHER) - .map(); + static final Map TYPES = MapBuilder.newMapBuilder( + new LinkedHashMap<>() + ) + .put(ContactPersonTypeEnumeration.ADMINISTRATIVE.toString(), ContactPersonTypeEnumeration.ADMINISTRATIVE) + .put(ContactPersonTypeEnumeration.BILLING.toString(), ContactPersonTypeEnumeration.BILLING) + .put(ContactPersonTypeEnumeration.SUPPORT.toString(), ContactPersonTypeEnumeration.SUPPORT) + .put(ContactPersonTypeEnumeration.TECHNICAL.toString(), ContactPersonTypeEnumeration.TECHNICAL) + .put(ContactPersonTypeEnumeration.OTHER.toString(), ContactPersonTypeEnumeration.OTHER) + .map(); public final ContactPersonTypeEnumeration type; public final String givenName; @@ -415,8 +418,9 @@ public ContactInfo(ContactPersonTypeEnumeration type, String givenName, String s private static ContactPersonTypeEnumeration getType(String name) { final ContactPersonTypeEnumeration type = TYPES.get(name.toLowerCase(Locale.ROOT)); if (type == null) { - throw new IllegalArgumentException("Invalid contact type " + name + " allowed values are " - + Strings.collectionToCommaDelimitedString(TYPES.keySet())); + throw new IllegalArgumentException( + "Invalid contact type " + name + " allowed values are " + Strings.collectionToCommaDelimitedString(TYPES.keySet()) + ); } return type; } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlToken.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlToken.java index c36b05bbe0255..bf9d1cc57ee68 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlToken.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlToken.java @@ -6,13 +6,13 @@ */ package org.elasticsearch.xpack.security.authc.saml; -import java.util.List; - import org.apache.commons.codec.binary.Hex; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xpack.core.security.authc.AuthenticationToken; +import java.util.List; + /** * A very lightweight {@link AuthenticationToken} to hold SAML content. * Due to the nature of SAML, it is impossible to know the {@link #principal() principal} for the token @@ -63,7 +63,6 @@ public String getAuthenticatingRealm() { return authenticatingRealm; } - @Override public String toString() { return getClass().getSimpleName() + "{" + Strings.cleanTruncate(Hex.encodeHexString(content), 128) + "...}"; diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlUtils.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlUtils.java index 21c6bbd5731a5..bc02642ca905d 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlUtils.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlUtils.java @@ -6,44 +6,13 @@ */ package org.elasticsearch.xpack.security.authc.saml; -import javax.xml.XMLConstants; -import javax.xml.namespace.QName; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.OutputKeys; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerConfigurationException; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; -import javax.xml.transform.stream.StreamSource; -import javax.xml.validation.Schema; -import javax.xml.validation.SchemaFactory; -import javax.xml.validation.Validator; -import java.io.IOException; -import java.io.InputStream; -import java.io.StringWriter; -import java.io.Writer; -import java.net.URISyntaxException; -import java.security.AccessController; -import java.security.PrivilegedActionException; -import java.security.PrivilegedExceptionAction; -import java.security.SecureRandom; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Objects; -import java.util.concurrent.atomic.AtomicBoolean; - -import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; -import org.elasticsearch.core.internal.io.IOUtils; +import org.apache.logging.log4j.Logger; import org.elasticsearch.ElasticsearchSecurityException; import org.elasticsearch.SpecialPermission; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.common.hash.MessageDigests; +import org.elasticsearch.core.SuppressForbidden; +import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.xpack.core.security.support.RestorableContextClassLoader; import org.opensaml.core.config.InitializationService; import org.opensaml.core.xml.XMLObject; @@ -64,6 +33,38 @@ import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; +import java.io.IOException; +import java.io.InputStream; +import java.io.StringWriter; +import java.io.Writer; +import java.net.URISyntaxException; +import java.security.AccessController; +import java.security.PrivilegedActionException; +import java.security.PrivilegedExceptionAction; +import java.security.SecureRandom; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Objects; +import java.util.concurrent.atomic.AtomicBoolean; + +import javax.xml.XMLConstants; +import javax.xml.namespace.QName; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerConfigurationException; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; +import javax.xml.transform.stream.StreamSource; +import javax.xml.validation.Schema; +import javax.xml.validation.SchemaFactory; +import javax.xml.validation.Validator; + public class SamlUtils { private static final String SAML_EXCEPTION_KEY = "es.security.saml"; @@ -132,8 +133,9 @@ public static T buildObject(Class type, QName elementNa if (type.isInstance(obj)) { return type.cast(obj); } else { - throw new IllegalArgumentException("Object for element " + elementName.getLocalPart() + " is of type " + obj.getClass() - + " not " + type); + throw new IllegalArgumentException( + "Object for element " + elementName.getLocalPart() + " is of type " + obj.getClass() + " not " + type + ); } } @@ -223,8 +225,7 @@ public static Transformer getHardenedXMLTransformer() throws TransformerConfigur static void validate(InputStream xml, String xsdName) throws Exception { SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); - try (InputStream xsdStream = loadSchema(xsdName); - ResourceResolver resolver = new ResourceResolver()) { + try (InputStream xsdStream = loadSchema(xsdName); ResourceResolver resolver = new ResourceResolver()) { schemaFactory.setResourceResolver(resolver); Schema schema = schemaFactory.newSchema(new StreamSource(xsdStream)); Validator validator = schema.newValidator(); @@ -304,8 +305,7 @@ public static DocumentBuilder getHardenedBuilder(String[] schemaFiles) throws Pa dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); dbf.setAttribute("http://apache.org/xml/features/validation/schema", true); dbf.setAttribute("http://apache.org/xml/features/validation/schema-full-checking", true); - dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", - XMLConstants.W3C_XML_SCHEMA_NS_URI); + dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", XMLConstants.W3C_XML_SCHEMA_NS_URI); // We ship our own xsd files for schema validation since we do not trust anyone else. dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", resolveSchemaFilePaths(schemaFiles)); DocumentBuilder documentBuilder = dbf.newDocumentBuilder(); @@ -315,15 +315,14 @@ public static DocumentBuilder getHardenedBuilder(String[] schemaFiles) throws Pa private static String[] resolveSchemaFilePaths(String[] relativePaths) { - return Arrays.stream(relativePaths). - map(file -> { - try { - return SamlUtils.class.getResource(file).toURI().toString(); - } catch (URISyntaxException e) { - LOGGER.warn("Error resolving schema file path", e); - return null; - } - }).filter(Objects::nonNull).toArray(String[]::new); + return Arrays.stream(relativePaths).map(file -> { + try { + return SamlUtils.class.getResource(file).toURI().toString(); + } catch (URISyntaxException e) { + LOGGER.warn("Error resolving schema file path", e); + return null; + } + }).filter(Objects::nonNull).toArray(String[]::new); } private static class ErrorListener implements javax.xml.transform.ErrorListener { diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SigningConfiguration.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SigningConfiguration.java index 61d91e15fd91e..fa104c7d1551b 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SigningConfiguration.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SigningConfiguration.java @@ -6,14 +6,14 @@ */ package org.elasticsearch.xpack.security.authc.saml; -import java.nio.charset.StandardCharsets; -import java.util.Set; - import org.opensaml.saml.common.SAMLObject; import org.opensaml.security.SecurityException; import org.opensaml.security.x509.X509Credential; import org.opensaml.xmlsec.crypto.XMLSigningUtil; +import java.nio.charset.StandardCharsets; +import java.util.Set; + /** * Encapsulates the rules and credentials for how and when Elasticsearch should sign outgoing SAML messages. */ diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SpConfiguration.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SpConfiguration.java index 43c0838b3bc52..16592223c1c7d 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SpConfiguration.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SpConfiguration.java @@ -24,9 +24,14 @@ public class SpConfiguration { private final List reqAuthnCtxClassRef; private final List encryptionCredentials; - public SpConfiguration(final String entityId, final String ascUrl, final String logoutUrl, - final SigningConfiguration signingConfiguration, @Nullable final List encryptionCredential, - final List authnCtxClassRef) { + public SpConfiguration( + final String entityId, + final String ascUrl, + final String logoutUrl, + final SigningConfiguration signingConfiguration, + @Nullable final List encryptionCredential, + final List authnCtxClassRef + ) { this.entityId = entityId; this.ascUrl = ascUrl; this.logoutUrl = logoutUrl; diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/service/CachingServiceAccountTokenStore.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/service/CachingServiceAccountTokenStore.java index 2a9bd40d48925..5a938a13bab90 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/service/CachingServiceAccountTokenStore.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/service/CachingServiceAccountTokenStore.java @@ -15,8 +15,8 @@ import org.elasticsearch.common.cache.CacheBuilder; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.ListenableFuture; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.xpack.core.security.action.service.TokenInfo.TokenSource; import org.elasticsearch.xpack.core.security.authc.support.Hasher; @@ -36,13 +36,22 @@ public abstract class CachingServiceAccountTokenStore implements ServiceAccountT private static final Logger logger = LogManager.getLogger(CachingServiceAccountTokenStore.class); - public static final Setting CACHE_HASH_ALGO_SETTING = Setting.simpleString("xpack.security.authc.service_token.cache.hash_algo", - "ssha256", Setting.Property.NodeScope); - - public static final Setting CACHE_TTL_SETTING = Setting.timeSetting("xpack.security.authc.service_token.cache.ttl", - TimeValue.timeValueMinutes(20), Setting.Property.NodeScope); + public static final Setting CACHE_HASH_ALGO_SETTING = Setting.simpleString( + "xpack.security.authc.service_token.cache.hash_algo", + "ssha256", + Setting.Property.NodeScope + ); + + public static final Setting CACHE_TTL_SETTING = Setting.timeSetting( + "xpack.security.authc.service_token.cache.ttl", + TimeValue.timeValueMinutes(20), + Setting.Property.NodeScope + ); public static final Setting CACHE_MAX_TOKENS_SETTING = Setting.intSetting( - "xpack.security.authc.service_token.cache.max_tokens", 100_000, Setting.Property.NodeScope); + "xpack.security.authc.service_token.cache.max_tokens", + 100_000, + Setting.Property.NodeScope + ); private final Settings settings; private final ThreadPool threadPool; diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/service/CompositeServiceAccountTokenStore.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/service/CompositeServiceAccountTokenStore.java index 5b27239e74454..386009ce44f7d 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/service/CompositeServiceAccountTokenStore.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/service/CompositeServiceAccountTokenStore.java @@ -39,7 +39,8 @@ public void authenticate(ServiceAccountToken token, ActionListener false == storeAuthenticationResult.isSuccess()); + storeAuthenticationResult -> false == storeAuthenticationResult.isSuccess() + ); try { authenticatingListener.run(); } catch (Exception e) { diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/service/ElasticServiceAccounts.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/service/ElasticServiceAccounts.java index 3d7c76d780499..ec8cb1e301fd6 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/service/ElasticServiceAccounts.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/service/ElasticServiceAccounts.java @@ -22,36 +22,40 @@ final class ElasticServiceAccounts { static final String NAMESPACE = "elastic"; - private static final ServiceAccount FLEET_ACCOUNT = new ElasticServiceAccount("fleet-server", + private static final ServiceAccount FLEET_ACCOUNT = new ElasticServiceAccount( + "fleet-server", new RoleDescriptor( NAMESPACE + "/fleet-server", - new String[]{"monitor", "manage_own_api_key"}, - new RoleDescriptor.IndicesPrivileges[]{ - RoleDescriptor.IndicesPrivileges - .builder() + new String[] { "monitor", "manage_own_api_key" }, + new RoleDescriptor.IndicesPrivileges[] { + RoleDescriptor.IndicesPrivileges.builder() .indices("logs-*", "metrics-*", "traces-*", "synthetics-*", ".logs-endpoint.diagnostic.collection-*") .privileges("write", "create_index", "auto_configure") .build(), - RoleDescriptor.IndicesPrivileges - .builder() + RoleDescriptor.IndicesPrivileges.builder() .indices(".fleet-*") .privileges("read", "write", "monitor", "create_index", "auto_configure") .allowRestrictedIndices(true) - .build() - }, - new RoleDescriptor.ApplicationResourcePrivileges[]{ + .build() }, + new RoleDescriptor.ApplicationResourcePrivileges[] { RoleDescriptor.ApplicationResourcePrivileges.builder() - .application("kibana-*").resources("*").privileges("reserved_fleet-setup").build() - }, + .application("kibana-*") + .resources("*") + .privileges("reserved_fleet-setup") + .build() }, null, null, null, null - )); - private static final ServiceAccount KIBANA_SYSTEM_ACCOUNT = - new ElasticServiceAccount("kibana", ReservedRolesStore.kibanaSystemRoleDescriptor(NAMESPACE + "/kibana")); + ) + ); + private static final ServiceAccount KIBANA_SYSTEM_ACCOUNT = new ElasticServiceAccount( + "kibana", + ReservedRolesStore.kibanaSystemRoleDescriptor(NAMESPACE + "/kibana") + ); - static final Map ACCOUNTS = List.of(FLEET_ACCOUNT, KIBANA_SYSTEM_ACCOUNT).stream() + static final Map ACCOUNTS = List.of(FLEET_ACCOUNT, KIBANA_SYSTEM_ACCOUNT) + .stream() .collect(Collectors.toMap(a -> a.id().asPrincipal(), Function.identity())); private ElasticServiceAccounts() {} @@ -65,12 +69,22 @@ static class ElasticServiceAccount implements ServiceAccount { this.id = new ServiceAccountId(NAMESPACE, serviceName); this.roleDescriptor = Objects.requireNonNull(roleDescriptor, "Role descriptor cannot be null"); if (roleDescriptor.getName().equals(id.asPrincipal()) == false) { - throw new IllegalArgumentException("the provided role descriptor [" + roleDescriptor.getName() - + "] must have the same name as the service account [" + id.asPrincipal() + "]"); + throw new IllegalArgumentException( + "the provided role descriptor [" + + roleDescriptor.getName() + + "] must have the same name as the service account [" + + id.asPrincipal() + + "]" + ); } - this.user = new User(id.asPrincipal(), Strings.EMPTY_ARRAY, "Service account - " + id, null, + this.user = new User( + id.asPrincipal(), + Strings.EMPTY_ARRAY, + "Service account - " + id, + null, Map.of("_elastic_service_account", true), - true); + true + ); } @Override diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/service/FileServiceAccountTokenStore.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/service/FileServiceAccountTokenStore.java index bbb0d3dc7fa52..8857cccd9e69f 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/service/FileServiceAccountTokenStore.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/service/FileServiceAccountTokenStore.java @@ -12,9 +12,9 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.action.ActionListener; import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.util.Maps; +import org.elasticsearch.core.Nullable; import org.elasticsearch.env.Environment; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.watcher.FileWatcher; @@ -50,8 +50,13 @@ public class FileServiceAccountTokenStore extends CachingServiceAccountTokenStor private final CopyOnWriteArrayList refreshListeners; private volatile Map tokenHashes; - public FileServiceAccountTokenStore(Environment env, ResourceWatcherService resourceWatcherService, ThreadPool threadPool, - ClusterService clusterService, CacheInvalidatorRegistry cacheInvalidatorRegistry) { + public FileServiceAccountTokenStore( + Environment env, + ResourceWatcherService resourceWatcherService, + ThreadPool threadPool, + ClusterService clusterService, + CacheInvalidatorRegistry cacheInvalidatorRegistry + ) { super(env.settings(), threadPool); this.clusterService = clusterService; file = resolveFile(env); @@ -75,9 +80,11 @@ public FileServiceAccountTokenStore(Environment env, ResourceWatcherService reso public void doAuthenticate(ServiceAccountToken token, ActionListener listener) { // This is done on the current thread instead of using a dedicated thread pool like API key does // because it is not expected to have a large number of service tokens. - listener.onResponse(Optional.ofNullable(tokenHashes.get(token.getQualifiedName())) - .map(hash -> new StoreAuthenticationResult(Hasher.verifyHash(token.getSecret(), hash), getTokenSource())) - .orElse(new StoreAuthenticationResult(false, getTokenSource()))); + listener.onResponse( + Optional.ofNullable(tokenHashes.get(token.getQualifiedName())) + .map(hash -> new StoreAuthenticationResult(Hasher.verifyHash(token.getSecret(), hash), getTokenSource())) + .orElse(new StoreAuthenticationResult(false, getTokenSource())) + ); } @Override @@ -90,9 +97,12 @@ public List findTokensFor(ServiceAccountId accountId) { return tokenHashes.keySet() .stream() .filter(k -> k.startsWith(principal + "/")) - .map(k -> TokenInfo.fileToken( - Strings.substring(k, principal.length() + 1, k.length()), - List.of(clusterService.localNode().getName()))) + .map( + k -> TokenInfo.fileToken( + Strings.substring(k, principal.length() + 1, k.length()), + List.of(clusterService.localNode().getName()) + ) + ) .collect(Collectors.toUnmodifiableList()); } @@ -131,8 +141,7 @@ static Map parseFileLenient(Path path, @Nullable Logger logger) try { return parseFile(path, logger); } catch (Exception e) { - logger.error("failed to parse service tokens file [{}]. skipping/removing all tokens...", - path.toAbsolutePath()); + logger.error("failed to parse service tokens file [{}]. skipping/removing all tokens...", path.toAbsolutePath()); return Map.of(); } } @@ -172,7 +181,10 @@ static Map parseFile(Path path, @Nullable Logger logger) throws static void writeFile(Path path, Map tokenHashes) { SecurityFiles.writeFileAtomically( - path, tokenHashes, e -> String.format(Locale.ROOT, "%s:%s", e.getKey(), new String(e.getValue()))); + path, + tokenHashes, + e -> String.format(Locale.ROOT, "%s:%s", e.getKey(), new String(e.getValue())) + ); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/service/FileTokensTool.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/service/FileTokensTool.java index 37e1b289e5b96..b8b4a0f634e90 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/service/FileTokensTool.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/service/FileTokensTool.java @@ -9,6 +9,7 @@ import joptsimple.OptionSet; import joptsimple.OptionSpec; + import org.elasticsearch.cli.EnvironmentAwareCommand; import org.elasticsearch.cli.ExitCodes; import org.elasticsearch.cli.LoggingAwareMultiCommand; @@ -130,14 +131,22 @@ protected void execute(Terminal terminal, OptionSet options, Environment env) th throw new UserException( ExitCodes.USAGE, "Expected at most one argument, service-account-principal, found extra: [" - + Strings.collectionToCommaDelimitedString(args) + "]"); + + Strings.collectionToCommaDelimitedString(args) + + "]" + ); } Predicate filter = k -> true; if (args.size() == 1) { final String principal = args.get(0); if (false == ServiceAccountService.isServiceAccountPrincipal(principal)) { - throw new UserException(ExitCodes.NO_USER, "Unknown service account principal: [" + principal + "]. Must be one of [" - + Strings.collectionToDelimitedString(ServiceAccountService.getServiceAccountPrincipals(), ",") + "]"); + throw new UserException( + ExitCodes.NO_USER, + "Unknown service account principal: [" + + principal + + "]. Must be one of [" + + Strings.collectionToDelimitedString(ServiceAccountService.getServiceAccountPrincipals(), ",") + + "]" + ); } filter = filter.and(k -> k.startsWith(principal + "/")); } @@ -160,13 +169,21 @@ static ServiceAccountTokenId parsePrincipalAndTokenName(List arguments, throw new UserException( ExitCodes.USAGE, "Expected two arguments, service-account-principal and token-name, found extra: [" - + Strings.collectionToCommaDelimitedString(arguments) + "]"); + + Strings.collectionToCommaDelimitedString(arguments) + + "]" + ); } final String principal = arguments.get(0); final String tokenName = arguments.get(1); if (false == ServiceAccountService.isServiceAccountPrincipal(principal)) { - throw new UserException(ExitCodes.NO_USER, "Unknown service account principal: [" + principal + "]. Must be one of [" - + Strings.collectionToDelimitedString(ServiceAccountService.getServiceAccountPrincipals(), ",") + "]"); + throw new UserException( + ExitCodes.NO_USER, + "Unknown service account principal: [" + + principal + + "]. Must be one of [" + + Strings.collectionToDelimitedString(ServiceAccountService.getServiceAccountPrincipals(), ",") + + "]" + ); } if (false == Validation.isValidServiceAccountTokenName(tokenName)) { throw new UserException(ExitCodes.CODE_ERROR, Validation.formatInvalidServiceTokenNameErrorMessage(tokenName)); diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/service/IndexServiceAccountTokenStore.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/service/IndexServiceAccountTokenStore.java index ee00c1dee7ee2..e52cf8612b4b5 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/service/IndexServiceAccountTokenStore.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/service/IndexServiceAccountTokenStore.java @@ -29,15 +29,15 @@ import org.elasticsearch.action.support.ContextPreservingActionListener; import org.elasticsearch.client.Client; import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.core.CharArrays; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.core.CharArrays; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.xpack.core.security.ScrollHelper; import org.elasticsearch.xpack.core.security.action.ClearSecurityCacheAction; @@ -78,9 +78,15 @@ public class IndexServiceAccountTokenStore extends CachingServiceAccountTokenSto private final ClusterService clusterService; private final Hasher hasher; - public IndexServiceAccountTokenStore(Settings settings, ThreadPool threadPool, Clock clock, Client client, - SecurityIndexManager securityIndex, ClusterService clusterService, - CacheInvalidatorRegistry cacheInvalidatorRegistry) { + public IndexServiceAccountTokenStore( + Settings settings, + ThreadPool threadPool, + Clock clock, + Client client, + SecurityIndexManager securityIndex, + ClusterService clusterService, + CacheInvalidatorRegistry cacheInvalidatorRegistry + ) { super(settings, threadPool); this.clock = clock; this.client = client; @@ -92,21 +98,30 @@ public IndexServiceAccountTokenStore(Settings settings, ThreadPool threadPool, C @Override void doAuthenticate(ServiceAccountToken token, ActionListener listener) { - final GetRequest getRequest = client - .prepareGet(SECURITY_MAIN_ALIAS, docIdForToken(token.getQualifiedName())) + final GetRequest getRequest = client.prepareGet(SECURITY_MAIN_ALIAS, docIdForToken(token.getQualifiedName())) .setFetchSource(true) .request(); - securityIndex.checkIndexVersionThenExecute(listener::onFailure, () -> - executeAsyncWithOrigin(client, SECURITY_ORIGIN, GetAction.INSTANCE, getRequest, ActionListener.wrap(response -> { - if (response.isExists()) { - final String tokenHash = (String) response.getSource().get("password"); - assert tokenHash != null : "service account token hash cannot be null"; - listener.onResponse(new StoreAuthenticationResult( - Hasher.verifyHash(token.getSecret(), tokenHash.toCharArray()), getTokenSource())); - } else { - logger.trace("service account token [{}] not found in index", token.getQualifiedName()); - listener.onResponse(new StoreAuthenticationResult(false, getTokenSource())); - }}, listener::onFailure))); + securityIndex.checkIndexVersionThenExecute( + listener::onFailure, + () -> executeAsyncWithOrigin( + client, + SECURITY_ORIGIN, + GetAction.INSTANCE, + getRequest, + ActionListener.wrap(response -> { + if (response.isExists()) { + final String tokenHash = (String) response.getSource().get("password"); + assert tokenHash != null : "service account token hash cannot be null"; + listener.onResponse( + new StoreAuthenticationResult(Hasher.verifyHash(token.getSecret(), tokenHash.toCharArray()), getTokenSource()) + ); + } else { + logger.trace("service account token [{}] not found in index", token.getQualifiedName()); + listener.onResponse(new StoreAuthenticationResult(false, getTokenSource())); + } + }, listener::onFailure) + ) + ); } @Override @@ -114,8 +129,11 @@ public TokenSource getTokenSource() { return TokenSource.INDEX; } - void createToken(Authentication authentication, CreateServiceAccountTokenRequest request, - ActionListener listener) { + void createToken( + Authentication authentication, + CreateServiceAccountTokenRequest request, + ActionListener listener + ) { final ServiceAccountId accountId = new ServiceAccountId(request.getNamespace(), request.getServiceName()); if (false == ServiceAccountService.isServiceAccountPrincipal(accountId.asPrincipal())) { listener.onFailure(new IllegalArgumentException("service account [" + accountId + "] does not exist")); @@ -123,23 +141,26 @@ void createToken(Authentication authentication, CreateServiceAccountTokenRequest } final ServiceAccountToken token = ServiceAccountToken.newToken(accountId, request.getTokenName()); try (XContentBuilder builder = newDocument(authentication, token)) { - final IndexRequest indexRequest = - client.prepareIndex(SECURITY_MAIN_ALIAS) - .setId(docIdForToken(token.getQualifiedName())) - .setSource(builder) - .setOpType(OpType.CREATE) - .setRefreshPolicy(request.getRefreshPolicy()) - .request(); + final IndexRequest indexRequest = client.prepareIndex(SECURITY_MAIN_ALIAS) + .setId(docIdForToken(token.getQualifiedName())) + .setSource(builder) + .setOpType(OpType.CREATE) + .setRefreshPolicy(request.getRefreshPolicy()) + .request(); final BulkRequest bulkRequest = toSingleItemBulkRequest(indexRequest); securityIndex.prepareIndexIfNeededThenExecute(listener::onFailure, () -> { - executeAsyncWithOrigin(client, SECURITY_ORIGIN, BulkAction.INSTANCE, bulkRequest, + executeAsyncWithOrigin( + client, + SECURITY_ORIGIN, + BulkAction.INSTANCE, + bulkRequest, TransportSingleItemBulkWriteAction.wrapBulkResponse(ActionListener.wrap(response -> { assert DocWriteResponse.Result.CREATED == response.getResult() : "an successful response of an OpType.CREATE request must have result of CREATED"; - listener.onResponse(CreateServiceAccountTokenResponse.created( - token.getTokenName(), token.asBearerString())); - }, listener::onFailure))); + listener.onResponse(CreateServiceAccountTokenResponse.created(token.getTokenName(), token.asBearerString())); + }, listener::onFailure)) + ); }); } catch (IOException e) { listener.onFailure(e); @@ -154,8 +175,9 @@ void findTokensFor(ServiceAccountId accountId, ActionListener { - final Supplier contextSupplier = - client.threadPool().getThreadContext().newRestorableContext(false); + final Supplier contextSupplier = client.threadPool() + .getThreadContext() + .newRestorableContext(false); try (ThreadContext.StoredContext ignore = client.threadPool().getThreadContext().stashWithOrigin(SECURITY_ORIGIN)) { // TODO: wildcard support? final BoolQueryBuilder query = QueryBuilders.boolQuery() @@ -170,9 +192,12 @@ void findTokensFor(ServiceAccountId accountId, ActionListener(contextSupplier, listener), - hit -> extractTokenInfo(hit.getId(), accountId)); + hit -> extractTokenInfo(hit.getId(), accountId) + ); } }); } @@ -195,21 +220,36 @@ void deleteToken(DeleteServiceAccountTokenRequest request, ActionListener { final DeleteRequest deleteRequest = client.prepareDelete(SECURITY_MAIN_ALIAS, docIdForToken(qualifiedTokenName)).request(); deleteRequest.setRefreshPolicy(request.getRefreshPolicy()); - executeAsyncWithOrigin(client, SECURITY_ORIGIN, DeleteAction.INSTANCE, deleteRequest, + executeAsyncWithOrigin( + client, + SECURITY_ORIGIN, + DeleteAction.INSTANCE, + deleteRequest, ActionListener.wrap(deleteResponse -> { - final ClearSecurityCacheRequest clearSecurityCacheRequest = - new ClearSecurityCacheRequest().cacheName("index_service_account_token").keys(qualifiedTokenName); - executeAsyncWithOrigin(client, SECURITY_ORIGIN, ClearSecurityCacheAction.INSTANCE, clearSecurityCacheRequest, - ActionListener.wrap(clearSecurityCacheResponse -> { - listener.onResponse(deleteResponse.getResult() == DocWriteResponse.Result.DELETED); - }, e -> { - final ParameterizedMessage message = new ParameterizedMessage( - "clearing the cache for service token [{}] failed. please clear the cache manually", - qualifiedTokenName); - logger.error(message, e); - listener.onFailure(new ElasticsearchException(message.getFormattedMessage(), e)); - })); - }, listener::onFailure)); + final ClearSecurityCacheRequest clearSecurityCacheRequest = new ClearSecurityCacheRequest().cacheName( + "index_service_account_token" + ).keys(qualifiedTokenName); + executeAsyncWithOrigin( + client, + SECURITY_ORIGIN, + ClearSecurityCacheAction.INSTANCE, + clearSecurityCacheRequest, + ActionListener.wrap( + clearSecurityCacheResponse -> { + listener.onResponse(deleteResponse.getResult() == DocWriteResponse.Result.DELETED); + }, + e -> { + final ParameterizedMessage message = new ParameterizedMessage( + "clearing the cache for service token [{}] failed. please clear the cache manually", + qualifiedTokenName + ); + logger.error(message, e); + listener.onFailure(new ElasticsearchException(message.getFormattedMessage(), e)); + } + ) + ); + }, listener::onFailure) + ); }); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/service/ServiceAccount.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/service/ServiceAccount.java index ba97ac2b8196c..3ac5fcf0d0b80 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/service/ServiceAccount.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/service/ServiceAccount.java @@ -33,7 +33,8 @@ public static ServiceAccountId fromPrincipal(String principal) { final int split = principal.indexOf('/'); if (split == -1) { throw new IllegalArgumentException( - "a service account ID must be in the form {namespace}/{service-name}, but was [" + principal + "]"); + "a service account ID must be in the form {namespace}/{service-name}, but was [" + principal + "]" + ); } return new ServiceAccountId(principal.substring(0, split), principal.substring(split + 1)); } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/service/ServiceAccountService.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/service/ServiceAccountService.java index 07d4a9161ad44..f7dd99f0aaa53 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/service/ServiceAccountService.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/service/ServiceAccountService.java @@ -50,13 +50,17 @@ public class ServiceAccountService { private final IndexServiceAccountTokenStore indexServiceAccountTokenStore; private final CompositeServiceAccountTokenStore compositeServiceAccountTokenStore; - public ServiceAccountService(Client client, - FileServiceAccountTokenStore fileServiceAccountTokenStore, - IndexServiceAccountTokenStore indexServiceAccountTokenStore) { + public ServiceAccountService( + Client client, + FileServiceAccountTokenStore fileServiceAccountTokenStore, + IndexServiceAccountTokenStore indexServiceAccountTokenStore + ) { this.client = client; this.indexServiceAccountTokenStore = indexServiceAccountTokenStore; this.compositeServiceAccountTokenStore = new CompositeServiceAccountTokenStore( - List.of(fileServiceAccountTokenStore, indexServiceAccountTokenStore), client.threadPool().getThreadContext()); + List.of(fileServiceAccountTokenStore, indexServiceAccountTokenStore), + client.threadPool().getThreadContext() + ); } public static boolean isServiceAccountPrincipal(String principal) { @@ -142,17 +146,19 @@ public void authenticateToken(ServiceAccountToken serviceAccountToken, String no }, listener::onFailure)); } - public void createIndexToken(Authentication authentication, CreateServiceAccountTokenRequest request, - ActionListener listener) { + public void createIndexToken( + Authentication authentication, + CreateServiceAccountTokenRequest request, + ActionListener listener + ) { indexServiceAccountTokenStore.createToken(authentication, request, listener); } public void deleteIndexToken(DeleteServiceAccountTokenRequest request, ActionListener listener) { - indexServiceAccountTokenStore.deleteToken(request, listener); + indexServiceAccountTokenStore.deleteToken(request, listener); } - public void findTokensFor(GetServiceAccountCredentialsRequest request, - ActionListener listener) { + public void findTokensFor(GetServiceAccountCredentialsRequest request, ActionListener listener) { final ServiceAccountId accountId = new ServiceAccountId(request.getNamespace(), request.getServiceName()); findIndexTokens(accountId, listener); } @@ -170,36 +176,60 @@ public void getRoleDescriptor(Authentication authentication, ActionListener listener) { - indexServiceAccountTokenStore.findTokensFor(accountId, ActionListener.wrap(indexTokenInfos -> { - findFileTokens(indexTokenInfos, accountId, listener); - }, listener::onFailure)); + indexServiceAccountTokenStore.findTokensFor( + accountId, + ActionListener.wrap(indexTokenInfos -> { findFileTokens(indexTokenInfos, accountId, listener); }, listener::onFailure) + ); } - private void findFileTokens( Collection indexTokenInfos, - ServiceAccountId accountId, - ActionListener listener) { - executeAsyncWithOrigin(client, SECURITY_ORIGIN, + private void findFileTokens( + Collection indexTokenInfos, + ServiceAccountId accountId, + ActionListener listener + ) { + executeAsyncWithOrigin( + client, + SECURITY_ORIGIN, GetServiceAccountNodesCredentialsAction.INSTANCE, new GetServiceAccountCredentialsNodesRequest(accountId.namespace(), accountId.serviceName()), - ActionListener.wrap(fileTokensResponse -> listener.onResponse( - new GetServiceAccountCredentialsResponse(accountId.asPrincipal(), indexTokenInfos, fileTokensResponse)), - listener::onFailure)); + ActionListener.wrap( + fileTokensResponse -> listener.onResponse( + new GetServiceAccountCredentialsResponse(accountId.asPrincipal(), indexTokenInfos, fileTokensResponse) + ), + listener::onFailure + ) + ); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/service/ServiceAccountToken.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/service/ServiceAccountToken.java index fb7eb1418831e..94310aec3afab 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/service/ServiceAccountToken.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/service/ServiceAccountToken.java @@ -10,11 +10,11 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.message.ParameterizedMessage; -import org.elasticsearch.core.CharArrays; import org.elasticsearch.common.Strings; import org.elasticsearch.common.UUIDs; import org.elasticsearch.common.hash.MessageDigests; import org.elasticsearch.common.settings.SecureString; +import org.elasticsearch.core.CharArrays; import org.elasticsearch.xpack.core.security.authc.AuthenticationToken; import org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken; import org.elasticsearch.xpack.core.security.support.Validation; @@ -94,9 +94,13 @@ public static ServiceAccountToken fromBearerString(SecureString bearerString) th try (InputStream in = Base64.getDecoder().wrap(new ByteArrayInputStream(bytes))) { final byte[] prefixBytes = in.readNBytes(4); if (prefixBytes.length != 4 || false == Arrays.equals(prefixBytes, PREFIX)) { - logger.trace(() -> new ParameterizedMessage( - "service account token expects the 4 leading bytes to be {}, got {}.", - Arrays.toString(PREFIX), Arrays.toString(prefixBytes))); + logger.trace( + () -> new ParameterizedMessage( + "service account token expects the 4 leading bytes to be {}, got {}.", + Arrays.toString(PREFIX), + Arrays.toString(prefixBytes) + ) + ); return null; } final char[] content = CharArrays.utf8BytesToChars(in.readAllBytes()); @@ -108,12 +112,17 @@ public static ServiceAccountToken fromBearerString(SecureString bearerString) th final String qualifiedName = new String(Arrays.copyOfRange(content, 0, i)); final String[] split = Strings.delimitedListToStringArray(qualifiedName, "/"); if (split == null || split.length != 3) { - logger.trace("The qualified name of a service token should take format of " + - "'namespace/service_name/token_name', got [{}]", qualifiedName); + logger.trace( + "The qualified name of a service token should take format of " + "'namespace/service_name/token_name', got [{}]", + qualifiedName + ); return null; } - return new ServiceAccountToken(new ServiceAccountId(split[0], split[1]), split[2], - new SecureString(Arrays.copyOfRange(content, i + 1, content.length))); + return new ServiceAccountToken( + new ServiceAccountId(split[0], split[1]), + split[2], + new SecureString(Arrays.copyOfRange(content, i + 1, content.length)) + ); } } @@ -129,10 +138,8 @@ public String toString() { @Override public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; ServiceAccountToken that = (ServiceAccountToken) o; return tokenId.equals(that.tokenId) && secret.equals(that.secret); } @@ -192,10 +199,8 @@ public String toString() { @Override public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; ServiceAccountTokenId that = (ServiceAccountTokenId) o; return accountId.equals(that.accountId) && tokenName.equals(that.tokenName); } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/support/ApiKeyGenerator.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/support/ApiKeyGenerator.java index e51c775acc423..bdadfba1ac6dd 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/support/ApiKeyGenerator.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/support/ApiKeyGenerator.java @@ -60,9 +60,13 @@ public void generateApiKey(Authentication authentication, CreateApiKeyRequest re if (ServiceAccountSettings.REALM_NAME.equals(authentication.getSourceRealm().getName())) { final ServiceAccount serviceAccount = ServiceAccountService.getServiceAccounts().get(authentication.getUser().principal()); if (serviceAccount == null) { - roleDescriptorsListener.onFailure(new ElasticsearchSecurityException( - "the authentication is created by a service account that does not exist: [" - + authentication.getUser().principal() + "]")); + roleDescriptorsListener.onFailure( + new ElasticsearchSecurityException( + "the authentication is created by a service account that does not exist: [" + + authentication.getUser().principal() + + "]" + ) + ); } else { roleDescriptorsListener.onResponse(Set.of(serviceAccount.roleDescriptor())); } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/support/CachingUsernamePasswordRealm.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/support/CachingUsernamePasswordRealm.java index 4f2e61b813687..287f6035b74d5 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/support/CachingUsernamePasswordRealm.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/support/CachingUsernamePasswordRealm.java @@ -7,13 +7,13 @@ package org.elasticsearch.xpack.security.authc.support; import org.elasticsearch.action.ActionListener; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.cache.Cache; import org.elasticsearch.common.cache.CacheBuilder; import org.elasticsearch.common.settings.SecureString; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.ListenableFuture; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.xpack.core.security.authc.AuthenticationResult; import org.elasticsearch.xpack.core.security.authc.AuthenticationToken; @@ -44,9 +44,9 @@ protected CachingUsernamePasswordRealm(RealmConfig config, ThreadPool threadPool final TimeValue ttl = this.config.getSetting(CachingUsernamePasswordRealmSettings.CACHE_TTL_SETTING); if (ttl.getNanos() > 0) { cache = CacheBuilder.>builder() - .setExpireAfterWrite(ttl) - .setMaximumWeight(this.config.getSetting(CachingUsernamePasswordRealmSettings.CACHE_MAX_USERS_SETTING)) - .build(); + .setExpireAfterWrite(ttl) + .setMaximumWeight(this.config.getSetting(CachingUsernamePasswordRealmSettings.CACHE_MAX_USERS_SETTING)) + .build(); } else { cache = null; } @@ -138,19 +138,28 @@ private void authenticateWithCache(UsernamePasswordToken token, ActionListener { if (authResult.isAuthenticated()) { - logger.debug("realm [{}] authenticated user [{}], with roles [{}] (cached)", - name(), token.principal(), authResult.getValue().roles()); + logger.debug( + "realm [{}] authenticated user [{}], with roles [{}] (cached)", + name(), + token.principal(), + authResult.getValue().roles() + ); } else { - logger.debug("realm [{}] authenticated user [{}] from cache, but then failed [{}]", - name(), token.principal(), authResult.getMessage()); + logger.debug( + "realm [{}] authenticated user [{}] from cache, but then failed [{}]", + name(), + token.principal(), + authResult.getMessage() + ); } listener.onResponse(authResult); }, listener::onFailure)); } else { logger.trace( - "realm [{}], provided credentials for user [{}] do not match (known good) cached credentials," + - " invalidating cache and retrying", - name(), token.principal() + "realm [{}], provided credentials for user [{}] do not match (known good) cached credentials," + + " invalidating cache and retrying", + name(), + token.principal() ); // its credential hash does not match the // hash of the credential for this forestalled request. @@ -172,8 +181,8 @@ private void authenticateWithCache(UsernamePasswordToken token, ActionListener { if (authResult.isAuthenticated() == false) { @@ -193,7 +205,10 @@ private void authenticateWithCache(UsernamePasswordToken token, ActionListener allRealms, RealmConfig config, XPackLicenseState licenseState) { - this(allRealms, config.getSetting(AUTHZ_REALMS), config.settings(), config.threadContext(), - licenseState); + this(allRealms, config.getSetting(AUTHZ_REALMS), config.settings(), config.threadContext(), licenseState); } /** @@ -57,8 +56,13 @@ public DelegatedAuthorizationSupport(Iterable allRealms, RealmC * {@code allRealms}. * @throws IllegalArgumentException if one of the specified realms does not exist */ - protected DelegatedAuthorizationSupport(Iterable allRealms, List lookupRealms, Settings settings, - ThreadContext threadContext, XPackLicenseState licenseState) { + protected DelegatedAuthorizationSupport( + Iterable allRealms, + List lookupRealms, + Settings settings, + ThreadContext threadContext, + XPackLicenseState licenseState + ) { final List resolvedLookupRealms = resolveRealms(allRealms, lookupRealms); checkForRealmChains(resolvedLookupRealms, settings); this.lookup = new RealmUserLookup(resolvedLookupRealms, threadContext); @@ -84,15 +88,21 @@ public boolean hasDelegation() { public void resolve(String username, ActionListener> resultListener) { boolean authzOk = Security.DELEGATED_AUTHORIZATION_FEATURE.check(licenseState); if (authzOk == false) { - resultListener.onResponse(AuthenticationResult.unsuccessful( - DelegatedAuthorizationSettings.AUTHZ_REALMS_SUFFIX + " are not permitted", - LicenseUtils.newComplianceException(DelegatedAuthorizationSettings.AUTHZ_REALMS_SUFFIX) - )); + resultListener.onResponse( + AuthenticationResult.unsuccessful( + DelegatedAuthorizationSettings.AUTHZ_REALMS_SUFFIX + " are not permitted", + LicenseUtils.newComplianceException(DelegatedAuthorizationSettings.AUTHZ_REALMS_SUFFIX) + ) + ); return; } if (hasDelegation() == false) { - resultListener.onResponse(AuthenticationResult.unsuccessful( - "No [" + DelegatedAuthorizationSettings.AUTHZ_REALMS_SUFFIX + "] have been configured", null)); + resultListener.onResponse( + AuthenticationResult.unsuccessful( + "No [" + DelegatedAuthorizationSettings.AUTHZ_REALMS_SUFFIX + "] have been configured", + null + ) + ); return; } ActionListener> userListener = ActionListener.wrap(tuple -> { @@ -100,9 +110,16 @@ public void resolve(String username, ActionListener> logger.trace("Found user " + tuple.v1() + " in realm " + tuple.v2()); resultListener.onResponse(AuthenticationResult.success(tuple.v1())); } else { - resultListener.onResponse(AuthenticationResult.unsuccessful("the principal [" + username - + "] was authenticated, but no user could be found in realms [" + collectionToDelimitedString(lookup.getRealms(), ",") - + "]", null)); + resultListener.onResponse( + AuthenticationResult.unsuccessful( + "the principal [" + + username + + "] was authenticated, but no user could be found in realms [" + + collectionToDelimitedString(lookup.getRealms(), ",") + + "]", + null + ) + ); } }, resultListener::onFailure); lookup.lookup(username, userListener); @@ -129,9 +146,13 @@ private void checkForRealmChains(Iterable delegatedRealms, Settings globa for (Realm realm : delegatedRealms) { Setting> realmAuthzSetting = AUTHZ_REALMS.apply(realm.type()).getConcreteSettingForNamespace(realm.name()); if (realmAuthzSetting.exists(globalSettings)) { - throw new IllegalArgumentException("cannot use realm [" + realm - + "] as an authorization realm - it is already delegating authorization to [" + realmAuthzSetting.get(globalSettings) - + "]"); + throw new IllegalArgumentException( + "cannot use realm [" + + realm + + "] as an authorization realm - it is already delegating authorization to [" + + realmAuthzSetting.get(globalSettings) + + "]" + ); } } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/support/DnRoleMapper.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/support/DnRoleMapper.java index 452301c439779..5221b2c148547 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/support/DnRoleMapper.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/support/DnRoleMapper.java @@ -8,6 +8,7 @@ import com.unboundid.ldap.sdk.DN; import com.unboundid.ldap.sdk.LDAPException; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.message.ParameterizedMessage; @@ -96,8 +97,12 @@ public static Map> parseFileLenient(Path path, Logger logge return parseFile(path, logger, realmType, realmName, false); } catch (Exception e) { logger.error( - (Supplier) () -> new ParameterizedMessage( - "failed to parse role mappings file [{}]. skipping/removing all mappings...", path.toAbsolutePath()), e); + (Supplier) () -> new ParameterizedMessage( + "failed to parse role mappings file [{}]. skipping/removing all mappings...", + path.toAbsolutePath() + ), + e + ); return emptyMap(); } } @@ -108,8 +113,10 @@ public static Map> parseFile(Path path, Logger logger, Stri if (Files.exists(path) == false) { final ParameterizedMessage message = new ParameterizedMessage( - "Role mapping file [{}] for realm [{}] does not exist.", - path.toAbsolutePath(), realmName); + "Role mapping file [{}] for realm [{}] does not exist.", + path.toAbsolutePath(), + realmName + ); if (strict) { throw new ElasticsearchException(message.getFormattedMessage()); } else { @@ -118,7 +125,7 @@ public static Map> parseFile(Path path, Logger logger, Stri } } - try { + try { Settings settings = Settings.builder().loadFromPath(path).build(); Map> dnToRoles = new HashMap<>(); @@ -135,12 +142,13 @@ public static Map> parseFile(Path path, Logger logger, Stri dnRoles.add(role); } catch (LDAPException e) { ParameterizedMessage message = new ParameterizedMessage( - "invalid DN [{}] found in [{}] role mappings [{}] for realm [{}/{}].", - providedDn, - realmType, - path.toAbsolutePath(), - realmType, - realmName); + "invalid DN [{}] found in [{}] role mappings [{}] for realm [{}/{}].", + providedDn, + realmType, + path.toAbsolutePath(), + realmType, + realmName + ); if (strict) { throw new ElasticsearchException(message.getFormattedMessage(), e); } else { @@ -151,15 +159,22 @@ public static Map> parseFile(Path path, Logger logger, Stri } - logger.debug("[{}] role mappings found in file [{}] for realm [{}/{}]", dnToRoles.size(), path.toAbsolutePath(), realmType, - realmName); - Map> normalizedMap = dnToRoles.entrySet().stream().collect(Collectors.toMap( - entry -> entry.getKey().toNormalizedString(), - entry -> List.copyOf(entry.getValue()))); + logger.debug( + "[{}] role mappings found in file [{}] for realm [{}/{}]", + dnToRoles.size(), + path.toAbsolutePath(), + realmType, + realmName + ); + Map> normalizedMap = dnToRoles.entrySet() + .stream() + .collect(Collectors.toMap(entry -> entry.getKey().toNormalizedString(), entry -> List.copyOf(entry.getValue()))); return unmodifiableMap(normalizedMap); } catch (IOException | SettingsException e) { - throw new ElasticsearchException("could not read realm [" + realmType + "/" + realmName + "] role mappings file [" + - path.toAbsolutePath() + "]", e); + throw new ElasticsearchException( + "could not read realm [" + realmType + "/" + realmName + "] role mappings file [" + path.toAbsolutePath() + "]", + e + ); } } @@ -191,8 +206,15 @@ public Set resolveRoles(String userDnString, Collection groupDns } } if (logger.isDebugEnabled()) { - logger.debug("the roles [{}], are mapped from these [{}] groups [{}] using file [{}] for realm [{}/{}]", roles, config.type(), - groupDns, file.getFileName(), config.type(), config.name()); + logger.debug( + "the roles [{}], are mapped from these [{}] groups [{}] using file [{}] for realm [{}/{}]", + roles, + config.type(), + groupDns, + file.getFileName(), + config.type(), + config.name() + ); } String normalizedUserDn = dn(userDnString).toNormalizedString(); @@ -201,9 +223,14 @@ public Set resolveRoles(String userDnString, Collection groupDns roles.addAll(rolesMappedToUserDn); } if (logger.isDebugEnabled()) { - logger.debug("the roles [{}], are mapped from the user [{}] using file [{}] for realm [{}/{}]", - (rolesMappedToUserDn == null) ? Collections.emptySet() : rolesMappedToUserDn, normalizedUserDn, file.getFileName(), - config.type(), config.name()); + logger.debug( + "the roles [{}], are mapped from the user [{}] using file [{}] for realm [{}/{}]", + (rolesMappedToUserDn == null) ? Collections.emptySet() : rolesMappedToUserDn, + normalizedUserDn, + file.getFileName(), + config.type(), + config.name() + ); } return roles; } @@ -230,8 +257,12 @@ public void onFileChanged(Path file) { dnRoles = parseFileLenient(file, logger, config.type(), config.name()); if (previousDnRoles.equals(dnRoles) == false) { - logger.info("role mappings file [{}] changed for realm [{}/{}]. updating mappings...", file.toAbsolutePath(), - config.type(), config.name()); + logger.info( + "role mappings file [{}] changed for realm [{}/{}]. updating mappings...", + file.toAbsolutePath(), + config.type(), + config.name() + ); notifyRefresh(); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/support/RealmUserLookup.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/support/RealmUserLookup.java index 45a86d975ed62..57100ca9b9bfe 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/support/RealmUserLookup.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/support/RealmUserLookup.java @@ -8,8 +8,8 @@ package org.elasticsearch.xpack.security.authc.support; import org.elasticsearch.action.ActionListener; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.Tuple; import org.elasticsearch.xpack.core.common.IteratingActionListener; import org.elasticsearch.xpack.core.security.authc.Realm; import org.elasticsearch.xpack.core.security.user.User; @@ -43,18 +43,18 @@ public boolean hasRealms() { * {@link ActionListener#onResponse(Object)} is called with a {@code null} {@link Tuple}. */ public void lookup(String principal, ActionListener> listener) { - final IteratingActionListener, ? extends Realm> userLookupListener = - new IteratingActionListener<>(listener, - (realm, lookupUserListener) -> realm.lookupUser(principal, - ActionListener.wrap(foundUser -> { - if (foundUser != null) { - lookupUserListener.onResponse(new Tuple<>(foundUser, realm)); - } else { - lookupUserListener.onResponse(null); - } - }, - lookupUserListener::onFailure)), - realms, threadContext); + final IteratingActionListener, ? extends Realm> userLookupListener = new IteratingActionListener<>( + listener, + (realm, lookupUserListener) -> realm.lookupUser(principal, ActionListener.wrap(foundUser -> { + if (foundUser != null) { + lookupUserListener.onResponse(new Tuple<>(foundUser, realm)); + } else { + lookupUserListener.onResponse(null); + } + }, lookupUserListener::onFailure)), + realms, + threadContext + ); try { userLookupListener.run(); } catch (Exception e) { diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/support/RoleMappingFileBootstrapCheck.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/support/RoleMappingFileBootstrapCheck.java index f5234331d315a..b76124d5c4631 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/support/RoleMappingFileBootstrapCheck.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/support/RoleMappingFileBootstrapCheck.java @@ -6,14 +6,14 @@ */ package org.elasticsearch.xpack.security.authc.support; -import java.nio.file.Path; - import org.apache.logging.log4j.LogManager; import org.elasticsearch.bootstrap.BootstrapCheck; import org.elasticsearch.bootstrap.BootstrapContext; import org.elasticsearch.xpack.core.security.authc.RealmConfig; import org.elasticsearch.xpack.core.security.authc.support.DnRoleMapperSettings; +import java.nio.file.Path; + /** * A BootstrapCheck that {@link DnRoleMapper} files exist and are valid (valid YAML and valid DNs) */ diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/support/SecondaryAuthenticator.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/support/SecondaryAuthenticator.java index b8a66a59a43bf..4dc29496cff9d 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/support/SecondaryAuthenticator.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/support/SecondaryAuthenticator.java @@ -70,20 +70,21 @@ public void authenticate(String action, TransportRequest request, ActionListener * If the secondary authentication credentials are found in the thread context, but fail to be authenticated, then * the failure is returned through {@link ActionListener#onFailure(Exception)}. */ - public void authenticateAndAttachToContext(RestRequest request, ActionListener listener) { - final ThreadContext threadContext = securityContext.getThreadContext(); - // We never want the secondary authentication to fallback to anonymous. - // Use cases for secondary authentication are far more likely to want to fall back to the primary authentication if no secondary - // auth is provided, so in that case we do no want to set anything in the context - authenticate(authListener -> authenticationService.authenticate(request, false, authListener), - ActionListener.wrap(secondaryAuthentication -> { - if (secondaryAuthentication != null) { - secondaryAuthentication.writeToContext(threadContext); - } - listener.onResponse(secondaryAuthentication); - }, - listener::onFailure)); - } + public void authenticateAndAttachToContext(RestRequest request, ActionListener listener) { + final ThreadContext threadContext = securityContext.getThreadContext(); + // We never want the secondary authentication to fallback to anonymous. + // Use cases for secondary authentication are far more likely to want to fall back to the primary authentication if no secondary + // auth is provided, so in that case we do no want to set anything in the context + authenticate( + authListener -> authenticationService.authenticate(request, false, authListener), + ActionListener.wrap(secondaryAuthentication -> { + if (secondaryAuthentication != null) { + secondaryAuthentication.writeToContext(threadContext); + } + listener.onResponse(secondaryAuthentication); + }, listener::onFailure) + ); + } private void authenticate(Consumer> authenticate, ActionListener listener) { final ThreadContext threadContext = securityContext.getThreadContext(); @@ -95,25 +96,26 @@ private void authenticate(Consumer> authenticate, } final Supplier originalContext = threadContext.newRestorableContext(false); - final ActionListener authenticationListener = new ContextPreservingActionListener<>(originalContext, - ActionListener.wrap( - authentication -> { - if (authentication == null) { - logger.debug("secondary authentication failed - authentication service returned a null authentication object"); - listener.onFailure(new ElasticsearchSecurityException("Failed to authenticate secondary user")); - } else { - logger.debug("secondary authentication succeeded [{}]", authentication); - listener.onResponse(new SecondaryAuthentication(securityContext, authentication)); - } - }, - e -> { - logger.debug("secondary authentication failed - authentication service responded with failure", e); - listener.onFailure(new ElasticsearchSecurityException("Failed to authenticate secondary user", e)); + final ActionListener authenticationListener = new ContextPreservingActionListener<>( + originalContext, + ActionListener.wrap(authentication -> { + if (authentication == null) { + logger.debug("secondary authentication failed - authentication service returned a null authentication object"); + listener.onFailure(new ElasticsearchSecurityException("Failed to authenticate secondary user")); + } else { + logger.debug("secondary authentication succeeded [{}]", authentication); + listener.onResponse(new SecondaryAuthentication(securityContext, authentication)); } - )); + }, e -> { + logger.debug("secondary authentication failed - authentication service responded with failure", e); + listener.onFailure(new ElasticsearchSecurityException("Failed to authenticate secondary user", e)); + }) + ); try (ThreadContext.StoredContext ignore = threadContext.stashContext()) { - logger.trace("found secondary authentication credentials, placing them in the internal [{}] header for authentication", - UsernamePasswordToken.BASIC_AUTH_HEADER); + logger.trace( + "found secondary authentication credentials, placing them in the internal [{}] header for authentication", + UsernamePasswordToken.BASIC_AUTH_HEADER + ); threadContext.putHeader(UsernamePasswordToken.BASIC_AUTH_HEADER, header); authenticate.accept(authenticationListener); } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/support/mapper/CompositeRoleMapper.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/support/mapper/CompositeRoleMapper.java index 4b7113affecad..d8f0e574e94c2 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/support/mapper/CompositeRoleMapper.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/support/mapper/CompositeRoleMapper.java @@ -6,19 +6,19 @@ */ package org.elasticsearch.xpack.security.authc.support.mapper; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Set; -import java.util.stream.Collectors; - import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.support.GroupedActionListener; import org.elasticsearch.watcher.ResourceWatcherService; import org.elasticsearch.xpack.core.security.authc.RealmConfig; import org.elasticsearch.xpack.core.security.authc.support.CachingRealm; -import org.elasticsearch.xpack.security.authc.support.DnRoleMapper; import org.elasticsearch.xpack.core.security.authc.support.UserRoleMapper; +import org.elasticsearch.xpack.security.authc.support.DnRoleMapper; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; /** * A {@link UserRoleMapper} that composes one or more delegate role-mappers. @@ -29,9 +29,11 @@ public class CompositeRoleMapper implements UserRoleMapper { private List delegates; - public CompositeRoleMapper(RealmConfig realmConfig, - ResourceWatcherService watcherService, - NativeRoleMappingStore nativeRoleMappingStore) { + public CompositeRoleMapper( + RealmConfig realmConfig, + ResourceWatcherService watcherService, + NativeRoleMappingStore nativeRoleMappingStore + ) { this(new DnRoleMapper(realmConfig, watcherService), nativeRoleMappingStore); } @@ -41,9 +43,13 @@ private CompositeRoleMapper(UserRoleMapper... delegates) { @Override public void resolveRoles(UserData user, ActionListener> listener) { - GroupedActionListener> groupListener = new GroupedActionListener<>(ActionListener.wrap( - composite -> listener.onResponse(composite.stream().flatMap(Set::stream).collect(Collectors.toSet())), listener::onFailure - ), delegates.size()); + GroupedActionListener> groupListener = new GroupedActionListener<>( + ActionListener.wrap( + composite -> listener.onResponse(composite.stream().flatMap(Set::stream).collect(Collectors.toSet())), + listener::onFailure + ), + delegates.size() + ); this.delegates.forEach(mapper -> mapper.resolveRoles(user, groupListener)); } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/support/mapper/NativeRoleMappingStore.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/support/mapper/NativeRoleMappingStore.java index 75f548ea0dd91..6e1803ab51522 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/support/mapper/NativeRoleMappingStore.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/support/mapper/NativeRoleMappingStore.java @@ -21,25 +21,25 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.index.query.QueryBuilder; +import org.elasticsearch.index.query.QueryBuilders; +import org.elasticsearch.script.ScriptService; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.index.query.QueryBuilder; -import org.elasticsearch.index.query.QueryBuilders; -import org.elasticsearch.script.ScriptService; import org.elasticsearch.xpack.core.security.ScrollHelper; import org.elasticsearch.xpack.core.security.action.realm.ClearRealmCacheAction; import org.elasticsearch.xpack.core.security.action.realm.ClearRealmCacheRequest; import org.elasticsearch.xpack.core.security.action.rolemapping.DeleteRoleMappingRequest; import org.elasticsearch.xpack.core.security.action.rolemapping.PutRoleMappingRequest; +import org.elasticsearch.xpack.core.security.authc.support.CachingRealm; +import org.elasticsearch.xpack.core.security.authc.support.UserRoleMapper; import org.elasticsearch.xpack.core.security.authc.support.mapper.ExpressionRoleMapping; import org.elasticsearch.xpack.core.security.authc.support.mapper.TemplateRoleName; import org.elasticsearch.xpack.core.security.authc.support.mapper.expressiondsl.ExpressionModel; import org.elasticsearch.xpack.core.security.index.RestrictedIndicesNames; -import org.elasticsearch.xpack.core.security.authc.support.CachingRealm; -import org.elasticsearch.xpack.core.security.authc.support.UserRoleMapper; import org.elasticsearch.xpack.security.support.SecurityIndexManager; import java.io.IOException; @@ -58,8 +58,8 @@ import static org.elasticsearch.action.DocWriteResponse.Result.CREATED; import static org.elasticsearch.action.DocWriteResponse.Result.DELETED; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.search.SearchService.DEFAULT_KEEPALIVE_SETTING; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.xpack.core.ClientHelper.SECURITY_ORIGIN; import static org.elasticsearch.xpack.core.ClientHelper.executeAsyncWithOrigin; import static org.elasticsearch.xpack.core.security.index.RestrictedIndicesNames.SECURITY_MAIN_ALIAS; @@ -125,37 +125,56 @@ private String getIdForName(String name) { */ protected void loadMappings(ActionListener> listener) { if (securityIndex.isIndexUpToDate() == false) { - listener.onFailure(new IllegalStateException( - "Security index is not on the current version - the native realm will not be operational until " + - "the upgrade API is run on the security index")); + listener.onFailure( + new IllegalStateException( + "Security index is not on the current version - the native realm will not be operational until " + + "the upgrade API is run on the security index" + ) + ); return; } final QueryBuilder query = QueryBuilders.termQuery(DOC_TYPE_FIELD, DOC_TYPE_ROLE_MAPPING); final Supplier supplier = client.threadPool().getThreadContext().newRestorableContext(false); try (ThreadContext.StoredContext ignore = client.threadPool().getThreadContext().stashWithOrigin(SECURITY_ORIGIN)) { SearchRequest request = client.prepareSearch(SECURITY_MAIN_ALIAS) - .setScroll(DEFAULT_KEEPALIVE_SETTING.get(settings)) - .setQuery(query) - .setSize(1000) - .setFetchSource(true) - .request(); + .setScroll(DEFAULT_KEEPALIVE_SETTING.get(settings)) + .setQuery(query) + .setSize(1000) + .setFetchSource(true) + .request(); request.indicesOptions().ignoreUnavailable(); - ScrollHelper.fetchAllByEntity(client, request, - new ContextPreservingActionListener<>(supplier, ActionListener.wrap((Collection mappings) -> - listener.onResponse(mappings.stream().filter(Objects::nonNull).collect(Collectors.toList())), - ex -> { - logger.error(new ParameterizedMessage("failed to load role mappings from index [{}] skipping all mappings.", - SECURITY_MAIN_ALIAS), ex); - listener.onResponse(Collections.emptyList()); - })), - doc -> buildMapping(getNameFromId(doc.getId()), doc.getSourceRef())); + ScrollHelper.fetchAllByEntity( + client, + request, + new ContextPreservingActionListener<>( + supplier, + ActionListener.wrap( + (Collection mappings) -> listener.onResponse( + mappings.stream().filter(Objects::nonNull).collect(Collectors.toList()) + ), + ex -> { + logger.error( + new ParameterizedMessage( + "failed to load role mappings from index [{}] skipping all mappings.", + SECURITY_MAIN_ALIAS + ), + ex + ); + listener.onResponse(Collections.emptyList()); + } + ) + ), + doc -> buildMapping(getNameFromId(doc.getId()), doc.getSourceRef()) + ); } } protected ExpressionRoleMapping buildMapping(String id, BytesReference source) { - try (InputStream stream = source.streamInput(); - XContentParser parser = XContentType.JSON.xContent() - .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) { + try ( + InputStream stream = source.streamInput(); + XContentParser parser = XContentType.JSON.xContent() + .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream) + ) { return ExpressionRoleMapping.parse(id, parser); } catch (Exception e) { logger.warn(new ParameterizedMessage("Role mapping [{}] cannot be parsed and will be skipped", id), e); @@ -181,12 +200,19 @@ public void deleteRoleMapping(DeleteRoleMappingRequest request, ActionListener void modifyMapping(String name, CheckedBiConsumer, Exception> inner, - Request request, ActionListener listener) { + private void modifyMapping( + String name, + CheckedBiConsumer, Exception> inner, + Request request, + ActionListener listener + ) { if (securityIndex.isIndexUpToDate() == false) { - listener.onFailure(new IllegalStateException( - "Security index is not on the current version - the native realm will not be operational until " + - "the upgrade API is run on the security index")); + listener.onFailure( + new IllegalStateException( + "Security index is not on the current version - the native realm will not be operational until " + + "the upgrade API is run on the security index" + ) + ); } else { try { inner.accept(request, ActionListener.wrap(r -> refreshRealms(listener, r), listener::onFailure)); @@ -207,24 +233,29 @@ private void innerPutMapping(PutRoleMappingRequest request, ActionListener() { - @Override - public void onResponse(IndexResponse indexResponse) { - boolean created = indexResponse.getResult() == CREATED; - listener.onResponse(created); - } - - @Override - public void onFailure(Exception e) { - logger.error(new ParameterizedMessage("failed to put role-mapping [{}]", mapping.getName()), e); - listener.onFailure(e); - } - }, client::index); + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + SECURITY_ORIGIN, + client.prepareIndex(SECURITY_MAIN_ALIAS) + .setId(getIdForName(mapping.getName())) + .setSource(xContentBuilder) + .setRefreshPolicy(request.getRefreshPolicy()) + .request(), + new ActionListener() { + @Override + public void onResponse(IndexResponse indexResponse) { + boolean created = indexResponse.getResult() == CREATED; + listener.onResponse(created); + } + + @Override + public void onFailure(Exception e) { + logger.error(new ParameterizedMessage("failed to put role-mapping [{}]", mapping.getName()), e); + listener.onFailure(e); + } + }, + client::index + ); }); } @@ -236,8 +267,10 @@ private void innerDeleteMapping(DeleteRoleMappingRequest request, ActionListener listener.onFailure(frozenSecurityIndex.getUnavailableReason()); } else { securityIndex.checkIndexVersionThenExecute(listener::onFailure, () -> { - executeAsyncWithOrigin(client.threadPool().getThreadContext(), SECURITY_ORIGIN, - client.prepareDelete(SECURITY_MAIN_ALIAS, getIdForName(request.getName())) + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + SECURITY_ORIGIN, + client.prepareDelete(SECURITY_MAIN_ALIAS, getIdForName(request.getName())) .setRefreshPolicy(request.getRefreshPolicy()) .request(), new ActionListener() { @@ -254,7 +287,9 @@ public void onFailure(Exception e) { listener.onFailure(e); } - }, client::delete); + }, + client::delete + ); }); } } @@ -270,8 +305,8 @@ public void getRoleMappings(Set names, ActionListener { final List filtered = mappings.stream() - .filter(m -> names.contains(m.getName())) - .collect(Collectors.toList()); + .filter(m -> names.contains(m.getName())) + .collect(Collectors.toList()); l.onResponse(filtered); })); } @@ -283,10 +318,11 @@ private void getMappings(ActionListener> listener) { } else { logger.info("The security index is not yet available - no role mappings can be loaded"); if (logger.isDebugEnabled()) { - logger.debug("Security Index [{}] [exists: {}] [available: {}]", - SECURITY_MAIN_ALIAS, - securityIndex.indexExists(), - securityIndex.isAvailable() + logger.debug( + "Security Index [{}] [exists: {}] [available: {}]", + SECURITY_MAIN_ALIAS, + securityIndex.indexExists(), + securityIndex.isAvailable() ); } listener.onResponse(Collections.emptyList()); @@ -333,38 +369,42 @@ private void refreshRealms(ActionListener listener, Result resu } final String[] realmNames = this.realmsToRefresh.toArray(Strings.EMPTY_ARRAY); - executeAsyncWithOrigin(client, SECURITY_ORIGIN, ClearRealmCacheAction.INSTANCE, new ClearRealmCacheRequest().realms(realmNames), - ActionListener.wrap( - response -> { - logger.debug((org.apache.logging.log4j.util.Supplier) () -> new ParameterizedMessage( - "Cleared cached in realms [{}] due to role mapping change", Arrays.toString(realmNames))); - listener.onResponse(result); - }, - ex -> { - logger.warn(new ParameterizedMessage("Failed to clear cache for realms [{}]", Arrays.toString(realmNames)), ex); - listener.onFailure(ex); - })); + executeAsyncWithOrigin( + client, + SECURITY_ORIGIN, + ClearRealmCacheAction.INSTANCE, + new ClearRealmCacheRequest().realms(realmNames), + ActionListener.wrap(response -> { + logger.debug( + (org.apache.logging.log4j.util.Supplier) () -> new ParameterizedMessage( + "Cleared cached in realms [{}] due to role mapping change", + Arrays.toString(realmNames) + ) + ); + listener.onResponse(result); + }, ex -> { + logger.warn(new ParameterizedMessage("Failed to clear cache for realms [{}]", Arrays.toString(realmNames)), ex); + listener.onFailure(ex); + }) + ); } @Override public void resolveRoles(UserData user, ActionListener> listener) { - getRoleMappings(null, ActionListener.wrap( - mappings -> { - final ExpressionModel model = user.asModel(); - final Set roles = mappings.stream() - .filter(ExpressionRoleMapping::isEnabled) - .filter(m -> m.getExpression().match(model)) - .flatMap(m -> { - final Set roleNames = m.getRoleNames(scriptService, model); - logger.trace("Applying role-mapping [{}] to user-model [{}] produced role-names [{}]", - m.getName(), model, roleNames); - return roleNames.stream(); - }) - .collect(Collectors.toSet()); - logger.debug("Mapping user [{}] to roles [{}]", user, roles); - listener.onResponse(roles); - }, listener::onFailure - )); + getRoleMappings(null, ActionListener.wrap(mappings -> { + final ExpressionModel model = user.asModel(); + final Set roles = mappings.stream() + .filter(ExpressionRoleMapping::isEnabled) + .filter(m -> m.getExpression().match(model)) + .flatMap(m -> { + final Set roleNames = m.getRoleNames(scriptService, model); + logger.trace("Applying role-mapping [{}] to user-model [{}] produced role-names [{}]", m.getName(), model, roleNames); + return roleNames.stream(); + }) + .collect(Collectors.toSet()); + logger.debug("Mapping user [{}] to roles [{}]", user, roles); + listener.onResponse(roles); + }, listener::onFailure)); } /** diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/AuthorizationService.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/AuthorizationService.java index 76248eac0fc03..72b95efd1ea4e 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/AuthorizationService.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/AuthorizationService.java @@ -106,10 +106,15 @@ import static org.elasticsearch.xpack.security.audit.logfile.LoggingAuditTrail.PRINCIPAL_ROLES_FIELD_NAME; public class AuthorizationService { - public static final Setting ANONYMOUS_AUTHORIZATION_EXCEPTION_SETTING = - Setting.boolSetting(setting("authc.anonymous.authz_exception"), true, Property.NodeScope); - private static final AuthorizationInfo SYSTEM_AUTHZ_INFO = - () -> Collections.singletonMap(PRINCIPAL_ROLES_FIELD_NAME, new String[] { SystemUser.ROLE_NAME }); + public static final Setting ANONYMOUS_AUTHORIZATION_EXCEPTION_SETTING = Setting.boolSetting( + setting("authc.anonymous.authz_exception"), + true, + Property.NodeScope + ); + private static final AuthorizationInfo SYSTEM_AUTHZ_INFO = () -> Collections.singletonMap( + PRINCIPAL_ROLES_FIELD_NAME, + new String[] { SystemUser.ROLE_NAME } + ); private static final String IMPLIED_INDEX_ACTION = IndexAction.NAME + ":op_type/index"; private static final String IMPLIED_CREATE_ACTION = IndexAction.NAME + ":op_type/create"; @@ -130,11 +135,20 @@ public class AuthorizationService { private final boolean isAnonymousEnabled; private final boolean anonymousAuthzExceptionEnabled; - public AuthorizationService(Settings settings, CompositeRolesStore rolesStore, ClusterService clusterService, - AuditTrailService auditTrailService, AuthenticationFailureHandler authcFailureHandler, - ThreadPool threadPool, AnonymousUser anonymousUser, @Nullable AuthorizationEngine authorizationEngine, - Set requestInterceptors, XPackLicenseState licenseState, - IndexNameExpressionResolver resolver, OperatorPrivilegesService operatorPrivilegesService) { + public AuthorizationService( + Settings settings, + CompositeRolesStore rolesStore, + ClusterService clusterService, + AuditTrailService auditTrailService, + AuthenticationFailureHandler authcFailureHandler, + ThreadPool threadPool, + AnonymousUser anonymousUser, + @Nullable AuthorizationEngine authorizationEngine, + Set requestInterceptors, + XPackLicenseState licenseState, + IndexNameExpressionResolver resolver, + OperatorPrivilegesService operatorPrivilegesService + ) { this.clusterService = clusterService; this.auditTrailService = auditTrailService; this.indicesAndAliasesResolver = new IndicesAndAliasesResolver(settings, clusterService, resolver); @@ -151,15 +165,26 @@ public AuthorizationService(Settings settings, CompositeRolesStore rolesStore, C this.operatorPrivilegesService = operatorPrivilegesService; } - public void checkPrivileges(Authentication authentication, HasPrivilegesRequest request, - Collection applicationPrivilegeDescriptors, - ActionListener listener) { - getAuthorizationEngine(authentication).checkPrivileges(authentication, getAuthorizationInfoFromContext(), request, - applicationPrivilegeDescriptors, wrapPreservingContext(listener, threadContext)); + public void checkPrivileges( + Authentication authentication, + HasPrivilegesRequest request, + Collection applicationPrivilegeDescriptors, + ActionListener listener + ) { + getAuthorizationEngine(authentication).checkPrivileges( + authentication, + getAuthorizationInfoFromContext(), + request, + applicationPrivilegeDescriptors, + wrapPreservingContext(listener, threadContext) + ); } - public void retrieveUserPrivileges(Authentication authentication, GetUserPrivilegesRequest request, - ActionListener listener) { + public void retrieveUserPrivileges( + Authentication authentication, + GetUserPrivilegesRequest request, + ActionListener listener + ) { getAuthorizationEngine(authentication).getUserPrivileges(authentication, getAuthorizationInfoFromContext(), request, listener); } @@ -177,8 +202,12 @@ private AuthorizationInfo getAuthorizationInfoFromContext() { * @param originalRequest The request * @param listener The listener that gets called. A call to {@link ActionListener#onResponse(Object)} indicates success */ - public void authorize(final Authentication authentication, final String action, final TransportRequest originalRequest, - final ActionListener listener) { + public void authorize( + final Authentication authentication, + final String action, + final TransportRequest originalRequest, + final ActionListener listener + ) { final AuthorizationContext enclosingContext = extractAuthorizationContext(threadContext, action); @@ -221,11 +250,10 @@ public void authorize(final Authentication authentication, final String action, } else { final RequestInfo requestInfo = new RequestInfo(authentication, unwrappedRequest, action, enclosingContext); final AuthorizationEngine engine = getAuthorizationEngine(authentication); - final ActionListener authzInfoListener = wrapPreservingContext(ActionListener.wrap( - authorizationInfo -> { - threadContext.putTransient(AUTHORIZATION_INFO_KEY, authorizationInfo); - maybeAuthorizeRunAs(requestInfo, auditId, authorizationInfo, listener); - }, listener::onFailure), threadContext); + final ActionListener authzInfoListener = wrapPreservingContext(ActionListener.wrap(authorizationInfo -> { + threadContext.putTransient(AUTHORIZATION_INFO_KEY, authorizationInfo); + maybeAuthorizeRunAs(requestInfo, auditId, authorizationInfo, listener); + }, listener::onFailure), threadContext); engine.resolveAuthorizationInfo(requestInfo, authzInfoListener); } } @@ -285,17 +313,24 @@ private void checkOperatorPrivileges(Authentication authentication, String actio throws ElasticsearchSecurityException { // Check operator privileges // TODO: audit? - final ElasticsearchSecurityException operatorException = - operatorPrivilegesService.check(authentication, action, originalRequest, threadContext); + final ElasticsearchSecurityException operatorException = operatorPrivilegesService.check( + authentication, + action, + originalRequest, + threadContext + ); if (operatorException != null) { - throw denialException(authentication, action, originalRequest, - "because it requires operator privileges", operatorException); + throw denialException(authentication, action, originalRequest, "because it requires operator privileges", operatorException); } operatorPrivilegesService.maybeInterceptRequest(threadContext, originalRequest); } - private void maybeAuthorizeRunAs(final RequestInfo requestInfo, final String requestId, final AuthorizationInfo authzInfo, - final ActionListener listener) { + private void maybeAuthorizeRunAs( + final RequestInfo requestInfo, + final String requestId, + final AuthorizationInfo authzInfo, + final ActionListener listener + ) { final Authentication authentication = requestInfo.getAuthentication(); final TransportRequest request = requestInfo.getRequest(); final String action = requestInfo.getAction(); @@ -305,20 +340,29 @@ private void maybeAuthorizeRunAs(final RequestInfo requestInfo, final String req ActionListener runAsListener = wrapPreservingContext(ActionListener.wrap(result -> { if (result.isGranted()) { if (result.isAuditable()) { - auditTrail.runAsGranted(requestId, authentication, action, request, - authzInfo.getAuthenticatedUserAuthorizationInfo()); + auditTrail.runAsGranted( + requestId, + authentication, + action, + request, + authzInfo.getAuthenticatedUserAuthorizationInfo() + ); } authorizeAction(requestInfo, requestId, authzInfo, listener); } else { if (result.isAuditable()) { - auditTrail.runAsDenied(requestId, authentication, action, request, - authzInfo.getAuthenticatedUserAuthorizationInfo()); + auditTrail.runAsDenied( + requestId, + authentication, + action, + request, + authzInfo.getAuthenticatedUserAuthorizationInfo() + ); } listener.onFailure(denialException(authentication, action, request, null)); } }, e -> { - auditTrail.runAsDenied(requestId, authentication, action, request, - authzInfo.getAuthenticatedUserAuthorizationInfo()); + auditTrail.runAsDenied(requestId, authentication, action, request, authzInfo.getAuthenticatedUserAuthorizationInfo()); listener.onFailure(denialException(authentication, action, request, null)); }), threadContext); authorizeRunAs(requestInfo, authzInfo, runAsListener); @@ -327,8 +371,12 @@ private void maybeAuthorizeRunAs(final RequestInfo requestInfo, final String req } } - private void authorizeAction(final RequestInfo requestInfo, final String requestId, final AuthorizationInfo authzInfo, - final ActionListener listener) { + private void authorizeAction( + final RequestInfo requestInfo, + final String requestId, + final AuthorizationInfo authzInfo, + final ActionListener listener + ) { final Authentication authentication = requestInfo.getAuthentication(); final TransportRequest request = requestInfo.getRequest(); final String action = requestInfo.getAction(); @@ -336,11 +384,13 @@ private void authorizeAction(final RequestInfo requestInfo, final String request final AuditTrail auditTrail = auditTrailService.get(); if (ClusterPrivilegeResolver.isClusterAction(action)) { - final ActionListener clusterAuthzListener = - wrapPreservingContext(new AuthorizationResultListener<>(result -> { - threadContext.putTransient(INDICES_PERMISSIONS_KEY, IndicesAccessControl.allowAll()); - listener.onResponse(null); - }, listener::onFailure, requestInfo, requestId, authzInfo), threadContext); + final ActionListener clusterAuthzListener = wrapPreservingContext( + new AuthorizationResultListener<>(result -> { + threadContext.putTransient(INDICES_PERMISSIONS_KEY, IndicesAccessControl.allowAll()); + listener.onResponse(null); + }, listener::onFailure, requestInfo, requestId, authzInfo), + threadContext + ); authzEngine.authorizeClusterAction(requestInfo, authzInfo, ActionListener.wrap(result -> { if (false == result.isGranted() && QueryApiKeyAction.NAME.equals(action)) { assert request instanceof QueryApiKeyRequest : "request does not match action"; @@ -374,10 +424,9 @@ private void authorizeAction(final RequestInfo requestInfo, final String request } else { authorizedIndicesSupplier.getAsync( ActionListener.wrap( - authorizedIndices -> - resolvedIndicesListener.onResponse( - indicesAndAliasesResolver.resolve(action, request, metadata, authorizedIndices) - ), + authorizedIndices -> resolvedIndicesListener.onResponse( + indicesAndAliasesResolver.resolve(action, request, metadata, authorizedIndices) + ), e -> { auditTrail.accessDenied(requestId, authentication, action, request, authzInfo); if (e instanceof IndexNotFoundException) { @@ -390,11 +439,31 @@ private void authorizeAction(final RequestInfo requestInfo, final String request ); } }); - authzEngine.authorizeIndexAction(requestInfo, authzInfo, resolvedIndicesAsyncSupplier, - metadata.getIndicesLookup(), wrapPreservingContext(new AuthorizationResultListener<>(result -> - handleIndexActionAuthorizationResult(result, requestInfo, requestId, authzInfo, authzEngine, - resolvedIndicesAsyncSupplier, metadata, listener), - listener::onFailure, requestInfo, requestId, authzInfo), threadContext)); + authzEngine.authorizeIndexAction( + requestInfo, + authzInfo, + resolvedIndicesAsyncSupplier, + metadata.getIndicesLookup(), + wrapPreservingContext( + new AuthorizationResultListener<>( + result -> handleIndexActionAuthorizationResult( + result, + requestInfo, + requestId, + authzInfo, + authzEngine, + resolvedIndicesAsyncSupplier, + metadata, + listener + ), + listener::onFailure, + requestInfo, + requestId, + authzInfo + ), + threadContext + ) + ); } else { logger.warn("denying access as action [{}] is not an index or cluster action", action); auditTrail.accessDenied(requestId, authentication, action, request, authzInfo); @@ -402,24 +471,30 @@ private void authorizeAction(final RequestInfo requestInfo, final String request } } - private void handleIndexActionAuthorizationResult(final IndexAuthorizationResult result, final RequestInfo requestInfo, - final String requestId, final AuthorizationInfo authzInfo, - final AuthorizationEngine authzEngine, - final AsyncSupplier resolvedIndicesAsyncSupplier, - final Metadata metadata, - final ActionListener listener) { + private void handleIndexActionAuthorizationResult( + final IndexAuthorizationResult result, + final RequestInfo requestInfo, + final String requestId, + final AuthorizationInfo authzInfo, + final AuthorizationEngine authzEngine, + final AsyncSupplier resolvedIndicesAsyncSupplier, + final Metadata metadata, + final ActionListener listener + ) { final Authentication authentication = requestInfo.getAuthentication(); final TransportRequest request = requestInfo.getRequest(); final String action = requestInfo.getAction(); if (result.getIndicesAccessControl() != null) { threadContext.putTransient(INDICES_PERMISSIONS_KEY, result.getIndicesAccessControl()); } - //if we are creating an index we need to authorize potential aliases created at the same time + // if we are creating an index we need to authorize potential aliases created at the same time if (IndexPrivilege.CREATE_INDEX_MATCHER.test(action)) { - assert (request instanceof CreateIndexRequest) || (request instanceof MigrateToDataStreamAction.Request) || - (request instanceof CreateDataStreamAction.Request); - if (request instanceof CreateDataStreamAction.Request || (request instanceof MigrateToDataStreamAction.Request) || - ((CreateIndexRequest) request).aliases().isEmpty()) { + assert (request instanceof CreateIndexRequest) + || (request instanceof MigrateToDataStreamAction.Request) + || (request instanceof CreateDataStreamAction.Request); + if (request instanceof CreateDataStreamAction.Request + || (request instanceof MigrateToDataStreamAction.Request) + || ((CreateIndexRequest) request).aliases().isEmpty()) { runRequestInterceptors(requestInfo, authzInfo, authorizationEngine, listener); } else { Set aliases = ((CreateIndexRequest) request).aliases(); @@ -429,21 +504,28 @@ private void handleIndexActionAuthorizationResult(final IndexAuthorizationResult result.getIndicesAccessControl() ); final RequestInfo aliasesRequestInfo = new RequestInfo(authentication, request, IndicesAliasesAction.NAME, parentContext); - authzEngine.authorizeIndexAction(aliasesRequestInfo, authzInfo, - ril -> { - resolvedIndicesAsyncSupplier.getAsync(ActionListener.wrap(resolvedIndices -> { - List aliasesAndIndices = new ArrayList<>(resolvedIndices.getLocal()); - for (Alias alias : aliases) { - aliasesAndIndices.add(alias.name()); - } - ResolvedIndices withAliases = new ResolvedIndices(aliasesAndIndices, Collections.emptyList()); - ril.onResponse(withAliases); - }, ril::onFailure)); - }, + authzEngine.authorizeIndexAction(aliasesRequestInfo, authzInfo, ril -> { + resolvedIndicesAsyncSupplier.getAsync(ActionListener.wrap(resolvedIndices -> { + List aliasesAndIndices = new ArrayList<>(resolvedIndices.getLocal()); + for (Alias alias : aliases) { + aliasesAndIndices.add(alias.name()); + } + ResolvedIndices withAliases = new ResolvedIndices(aliasesAndIndices, Collections.emptyList()); + ril.onResponse(withAliases); + }, ril::onFailure)); + }, metadata.getIndicesLookup(), - wrapPreservingContext(new AuthorizationResultListener<>( - authorizationResult -> runRequestInterceptors(requestInfo, authzInfo, authorizationEngine, listener), - listener::onFailure, aliasesRequestInfo, requestId, authzInfo), threadContext)); + wrapPreservingContext( + new AuthorizationResultListener<>( + authorizationResult -> runRequestInterceptors(requestInfo, authzInfo, authorizationEngine, listener), + listener::onFailure, + aliasesRequestInfo, + requestId, + authzInfo + ), + threadContext + ) + ); } } else if (action.equals(TransportShardBulkAction.ACTION_NAME)) { // if this is performing multiple actions on the index, then check each of those actions. @@ -454,25 +536,38 @@ private void handleIndexActionAuthorizationResult(final IndexAuthorizationResult authzInfo, result.getIndicesAccessControl() ); - authorizeBulkItems(requestInfo, authzContext, authzEngine, resolvedIndicesAsyncSupplier, metadata, + authorizeBulkItems( + requestInfo, + authzContext, + authzEngine, + resolvedIndicesAsyncSupplier, + metadata, requestId, wrapPreservingContext( - ActionListener.wrap(ignore -> runRequestInterceptors(requestInfo, authzInfo, authorizationEngine, listener), - listener::onFailure), - threadContext)); + ActionListener.wrap( + ignore -> runRequestInterceptors(requestInfo, authzInfo, authorizationEngine, listener), + listener::onFailure + ), + threadContext + ) + ); } else { runRequestInterceptors(requestInfo, authzInfo, authorizationEngine, listener); } } - private void runRequestInterceptors(RequestInfo requestInfo, AuthorizationInfo authorizationInfo, - AuthorizationEngine authorizationEngine, ActionListener listener) { + private void runRequestInterceptors( + RequestInfo requestInfo, + AuthorizationInfo authorizationInfo, + AuthorizationEngine authorizationEngine, + ActionListener listener + ) { if (requestInterceptors.isEmpty()) { listener.onResponse(null); } else { final Iterator requestInterceptorIterator = requestInterceptors.iterator(); - requestInterceptorIterator.next().intercept(requestInfo, authorizationEngine, authorizationInfo, - new ActionListener.Delegating<>(listener) { + requestInterceptorIterator.next() + .intercept(requestInfo, authorizationEngine, authorizationInfo, new ActionListener.Delegating<>(listener) { @Override public void onResponse(Void unused) { if (requestInterceptorIterator.hasNext()) { @@ -481,12 +576,10 @@ public void onResponse(Void unused) { listener.onResponse(null); } } - } - ); + }); } } - // pkg-private for testing AuthorizationEngine getRunAsAuthorizationEngine(final Authentication authentication) { return getAuthorizationEngineForUser(authentication.getUser().authenticatedUser()); @@ -498,8 +591,7 @@ AuthorizationEngine getAuthorizationEngine(final Authentication authentication) } private AuthorizationEngine getAuthorizationEngineForUser(final User user) { - if (rbacEngine != authorizationEngine - && Security.AUTHORIZATION_ENGINE_FEATURE.check(licenseState)) { + if (rbacEngine != authorizationEngine && Security.AUTHORIZATION_ENGINE_FEATURE.check(licenseState)) { if (ClientReservedRealm.isReserved(user.principal(), settings) || isInternal(user)) { return rbacEngine; } else { @@ -510,8 +602,13 @@ private AuthorizationEngine getAuthorizationEngineForUser(final User user) { } } - private void authorizeSystemUser(final Authentication authentication, final String action, final String requestId, - final TransportRequest request, final ActionListener listener) { + private void authorizeSystemUser( + final Authentication authentication, + final String action, + final String requestId, + final TransportRequest request, + final ActionListener listener + ) { final AuditTrail auditTrail = auditTrailService.get(); if (SystemUser.isAuthorized(action)) { threadContext.putTransient(INDICES_PERMISSIONS_KEY, IndicesAccessControl.allowAll()); @@ -524,8 +621,12 @@ private void authorizeSystemUser(final Authentication authentication, final Stri } } - private TransportRequest maybeUnwrapRequest(Authentication authentication, TransportRequest originalRequest, String action, - String requestId) { + private TransportRequest maybeUnwrapRequest( + Authentication authentication, + TransportRequest originalRequest, + String action, + String requestId + ) { final TransportRequest request; if (originalRequest instanceof ConcreteShardRequest) { request = ((ConcreteShardRequest) originalRequest).getRequest(); @@ -536,14 +637,16 @@ private TransportRequest maybeUnwrapRequest(Authentication authentication, Trans final boolean isProxyAction = TransportActionProxy.isProxyAction(action); final AuditTrail auditTrail = auditTrailService.get(); if (isProxyAction && isOriginalRequestProxyRequest == false) { - IllegalStateException cause = new IllegalStateException("originalRequest is not a proxy request: [" + originalRequest + - "] but action: [" + action + "] is a proxy action"); + IllegalStateException cause = new IllegalStateException( + "originalRequest is not a proxy request: [" + originalRequest + "] but action: [" + action + "] is a proxy action" + ); auditTrail.accessDenied(requestId, authentication, action, request, EmptyAuthorizationInfo.INSTANCE); throw denialException(authentication, action, request, cause); } if (TransportActionProxy.isProxyRequest(originalRequest) && TransportActionProxy.isProxyAction(action) == false) { - IllegalStateException cause = new IllegalStateException("originalRequest is a proxy request for: [" + request + - "] but action: [" + action + "] isn't"); + IllegalStateException cause = new IllegalStateException( + "originalRequest is a proxy request for: [" + request + "] but action: [" + action + "] isn't" + ); auditTrail.accessDenied(requestId, authentication, action, request, EmptyAuthorizationInfo.INSTANCE); throw denialException(authentication, action, request, cause); } @@ -551,8 +654,11 @@ private TransportRequest maybeUnwrapRequest(Authentication authentication, Trans return request; } - private void authorizeRunAs(final RequestInfo requestInfo, final AuthorizationInfo authzInfo, - final ActionListener listener) { + private void authorizeRunAs( + final RequestInfo requestInfo, + final AuthorizationInfo authzInfo, + final ActionListener listener + ) { final Authentication authentication = requestInfo.getAuthentication(); if (authentication.getLookedUpBy() == null) { // this user did not really exist @@ -578,9 +684,15 @@ private void authorizeRunAs(final RequestInfo requestInfo, final AuthorizationIn * is very small, but the results must be cached, to avoid adding a high * overhead to each bulk request. */ - private void authorizeBulkItems(RequestInfo requestInfo, AuthorizationContext bulkAuthzContext, - AuthorizationEngine authzEngine, AsyncSupplier resolvedIndicesAsyncSupplier, - Metadata metadata, String requestId, ActionListener listener) { + private void authorizeBulkItems( + RequestInfo requestInfo, + AuthorizationContext bulkAuthzContext, + AuthorizationEngine authzEngine, + AsyncSupplier resolvedIndicesAsyncSupplier, + Metadata metadata, + String requestId, + ActionListener listener + ) { final Authentication authentication = requestInfo.getAuthentication(); final AuthorizationInfo authzInfo = bulkAuthzContext.getAuthorizationInfo(); final BulkShardRequest request = (BulkShardRequest) requestInfo.getRequest(); @@ -595,20 +707,27 @@ private void authorizeBulkItems(RequestInfo requestInfo, AuthorizationContext bu for (BulkItemRequest item : request.items()) { final String itemAction = getAction(item); String resolvedIndex = resolvedIndexNames.computeIfAbsent(item.index(), key -> { - final ResolvedIndices resolvedIndices = - indicesAndAliasesResolver.resolveIndicesAndAliasesWithoutWildcards(itemAction, item.request()); + final ResolvedIndices resolvedIndices = indicesAndAliasesResolver.resolveIndicesAndAliasesWithoutWildcards( + itemAction, + item.request() + ); if (resolvedIndices.getRemote().size() != 0) { - throw illegalArgument("Bulk item should not write to remote indices, but request writes to " - + String.join(",", resolvedIndices.getRemote())); + throw illegalArgument( + "Bulk item should not write to remote indices, but request writes to " + + String.join(",", resolvedIndices.getRemote()) + ); } if (resolvedIndices.getLocal().size() != 1) { - throw illegalArgument("Bulk item should write to exactly 1 index, but request writes to " - + String.join(",", resolvedIndices.getLocal())); + throw illegalArgument( + "Bulk item should write to exactly 1 index, but request writes to " + + String.join(",", resolvedIndices.getLocal()) + ); } final String resolved = resolvedIndices.getLocal().get(0); if (localIndices.contains(resolved) == false) { - throw illegalArgument("Found bulk item that writes to index " + resolved + " but the request writes to " + - localIndices); + throw illegalArgument( + "Found bulk item that writes to index " + resolved + " but the request writes to " + localIndices + ); } return resolved; }); @@ -620,13 +739,15 @@ private void authorizeBulkItems(RequestInfo requestInfo, AuthorizationContext bu }); } - final ActionListener>> bulkAuthzListener = - ActionListener.wrap(collection -> { + final ActionListener>> bulkAuthzListener = ActionListener.wrap( + collection -> { final Map actionToIndicesAccessControl = new HashMap<>(); final AtomicBoolean audit = new AtomicBoolean(false); collection.forEach(tuple -> { - final IndicesAccessControl existing = - actionToIndicesAccessControl.putIfAbsent(tuple.v1(), tuple.v2().getIndicesAccessControl()); + final IndicesAccessControl existing = actionToIndicesAccessControl.putIfAbsent( + tuple.v1(), + tuple.v2().getIndicesAccessControl() + ); if (existing != null) { throw new IllegalStateException("a value already exists for action " + tuple.v1()); } @@ -639,31 +760,69 @@ private void authorizeBulkItems(RequestInfo requestInfo, AuthorizationContext bu final String resolvedIndex = resolvedIndexNames.get(item.index()); final String itemAction = getAction(item); final IndicesAccessControl indicesAccessControl = actionToIndicesAccessControl.get(itemAction); - final IndicesAccessControl.IndexAccessControl indexAccessControl - = indicesAccessControl.getIndexPermissions(resolvedIndex); + final IndicesAccessControl.IndexAccessControl indexAccessControl = indicesAccessControl.getIndexPermissions( + resolvedIndex + ); if (indexAccessControl == null || indexAccessControl.isGranted() == false) { - auditTrail.explicitIndexAccessEvent(requestId, AuditLevel.ACCESS_DENIED, authentication, itemAction, - resolvedIndex, item.getClass().getSimpleName(), request.remoteAddress(), authzInfo); - item.abort(resolvedIndex, denialException(authentication, itemAction, request, - AuthorizationEngine.IndexAuthorizationResult.getFailureDescription(List.of(resolvedIndex)), null)); + auditTrail.explicitIndexAccessEvent( + requestId, + AuditLevel.ACCESS_DENIED, + authentication, + itemAction, + resolvedIndex, + item.getClass().getSimpleName(), + request.remoteAddress(), + authzInfo + ); + item.abort( + resolvedIndex, + denialException( + authentication, + itemAction, + request, + AuthorizationEngine.IndexAuthorizationResult.getFailureDescription(List.of(resolvedIndex)), + null + ) + ); } else if (audit.get()) { - auditTrail.explicitIndexAccessEvent(requestId, AuditLevel.ACCESS_GRANTED, authentication, itemAction, - resolvedIndex, item.getClass().getSimpleName(), request.remoteAddress(), authzInfo); + auditTrail.explicitIndexAccessEvent( + requestId, + AuditLevel.ACCESS_GRANTED, + authentication, + itemAction, + resolvedIndex, + item.getClass().getSimpleName(), + request.remoteAddress(), + authzInfo + ); } } listener.onResponse(null); - }, listener::onFailure); + }, + listener::onFailure + ); final ActionListener> groupedActionListener = wrapPreservingContext( - new GroupedActionListener<>(bulkAuthzListener, actionToIndicesMap.size()), threadContext); + new GroupedActionListener<>(bulkAuthzListener, actionToIndicesMap.size()), + threadContext + ); actionToIndicesMap.forEach((bulkItemAction, indices) -> { - final RequestInfo bulkItemInfo = - new RequestInfo(requestInfo.getAuthentication(), requestInfo.getRequest(), bulkItemAction, bulkAuthzContext); - authzEngine.authorizeIndexAction(bulkItemInfo, authzInfo, + final RequestInfo bulkItemInfo = new RequestInfo( + requestInfo.getAuthentication(), + requestInfo.getRequest(), + bulkItemAction, + bulkAuthzContext + ); + authzEngine.authorizeIndexAction( + bulkItemInfo, + authzInfo, ril -> ril.onResponse(new ResolvedIndices(new ArrayList<>(indices), Collections.emptyList())), - metadata.getIndicesLookup(), ActionListener.wrap(indexAuthorizationResult -> - groupedActionListener.onResponse(new Tuple<>(bulkItemAction, indexAuthorizationResult)), - groupedActionListener::onFailure)); + metadata.getIndicesLookup(), + ActionListener.wrap( + indexAuthorizationResult -> groupedActionListener.onResponse(new Tuple<>(bulkItemAction, indexAuthorizationResult)), + groupedActionListener::onFailure + ) + ); }); }, listener::onFailure)); } @@ -699,13 +858,22 @@ private void putTransientIfNonExisting(String key, Object value) { } } - private ElasticsearchSecurityException denialException(Authentication authentication, String action, TransportRequest request, - Exception cause) { + private ElasticsearchSecurityException denialException( + Authentication authentication, + String action, + TransportRequest request, + Exception cause + ) { return denialException(authentication, action, request, null, cause); } - private ElasticsearchSecurityException denialException(Authentication authentication, String action, TransportRequest request, - @Nullable String context, Exception cause) { + private ElasticsearchSecurityException denialException( + Authentication authentication, + String action, + TransportRequest request, + @Nullable String context, + Exception cause + ) { final User authUser = authentication.getUser().authenticatedUser(); // Special case for anonymous user if (isAnonymousEnabled && anonymousUser.equals(authUser)) { @@ -738,14 +906,18 @@ private ElasticsearchSecurityException denialException(Authentication authentica if (ClusterPrivilegeResolver.isClusterAction(action)) { final Collection privileges = ClusterPrivilegeResolver.findPrivilegesThatGrant(action, request, authentication); if (privileges != null && privileges.size() > 0) { - message = message + ", this action is granted by the cluster privileges [" - + collectionToCommaDelimitedString(privileges) + "]"; + message = message + + ", this action is granted by the cluster privileges [" + + collectionToCommaDelimitedString(privileges) + + "]"; } } else if (isIndexAction(action)) { final Collection privileges = IndexPrivilege.findPrivilegesThatGrant(action); if (privileges != null && privileges.size() > 0) { - message = message + ", this action is granted by the index privileges [" - + collectionToCommaDelimitedString(privileges) + "]"; + message = message + + ", this action is granted by the index privileges [" + + collectionToCommaDelimitedString(privileges) + + "]"; } } @@ -761,8 +933,13 @@ private class AuthorizationResultListener impleme private final String requestId; private final AuthorizationInfo authzInfo; - private AuthorizationResultListener(Consumer responseConsumer, Consumer failureConsumer, RequestInfo requestInfo, - String requestId, AuthorizationInfo authzInfo) { + private AuthorizationResultListener( + Consumer responseConsumer, + Consumer failureConsumer, + RequestInfo requestInfo, + String requestId, + AuthorizationInfo authzInfo + ) { this.responseConsumer = responseConsumer; this.failureConsumer = failureConsumer; this.requestInfo = requestInfo; @@ -774,8 +951,14 @@ private AuthorizationResultListener(Consumer responseConsumer, Consumer consumer) { + public static void switchUserBasedOnActionOriginAndExecute( + ThreadContext threadContext, + SecurityContext securityContext, + Consumer consumer + ) { final String actionOrigin = threadContext.getTransient(ClientHelper.ACTION_ORIGIN_TRANSIENT_NAME); if (actionOrigin == null) { assert false : "cannot switch user if there is no action origin"; diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/DlsFlsRequestCacheDifferentiator.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/DlsFlsRequestCacheDifferentiator.java index 296762b7b5288..ce682da0e79df 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/DlsFlsRequestCacheDifferentiator.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/DlsFlsRequestCacheDifferentiator.java @@ -32,9 +32,11 @@ public class DlsFlsRequestCacheDifferentiator implements CheckedBiConsumer securityContextHolder; private final SetOnce scriptServiceReference; - public DlsFlsRequestCacheDifferentiator(XPackLicenseState licenseState, - SetOnce securityContextReference, - SetOnce scriptServiceReference) { + public DlsFlsRequestCacheDifferentiator( + XPackLicenseState licenseState, + SetOnce securityContextReference, + SetOnce scriptServiceReference + ) { this.licenseState = licenseState; this.securityContextHolder = securityContextReference; this.scriptServiceReference = scriptServiceReference; @@ -43,19 +45,25 @@ public DlsFlsRequestCacheDifferentiator(XPackLicenseState licenseState, @Override public void accept(ShardSearchRequest request, StreamOutput out) throws IOException { final SecurityContext securityContext = securityContextHolder.get(); - final IndicesAccessControl indicesAccessControl = - securityContext.getThreadContext().getTransient(AuthorizationServiceField.INDICES_PERMISSIONS_KEY); + final IndicesAccessControl indicesAccessControl = securityContext.getThreadContext() + .getTransient(AuthorizationServiceField.INDICES_PERMISSIONS_KEY); final String indexName = request.shardId().getIndexName(); IndicesAccessControl.IndexAccessControl indexAccessControl = indicesAccessControl.getIndexPermissions(indexName); if (indexAccessControl != null) { final boolean flsEnabled = indexAccessControl.getFieldPermissions().hasFieldLevelSecurity(); final boolean dlsEnabled = indexAccessControl.getDocumentPermissions().hasDocumentLevelPermissions(); if ((flsEnabled || dlsEnabled) && DOCUMENT_LEVEL_SECURITY_FEATURE.checkWithoutTracking(licenseState)) { - logger.debug("index [{}] with field level access controls [{}] " + - "document level access controls [{}]. Differentiating request cache key", - indexName, flsEnabled, dlsEnabled); + logger.debug( + "index [{}] with field level access controls [{}] " + + "document level access controls [{}]. Differentiating request cache key", + indexName, + flsEnabled, + dlsEnabled + ); indexAccessControl.buildCacheKey( - out, SecurityQueryTemplateEvaluator.wrap(securityContext.getUser(), scriptServiceReference.get())); + out, + SecurityQueryTemplateEvaluator.wrap(securityContext.getUser(), scriptServiceReference.get()) + ); } } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/IndicesAndAliasesResolver.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/IndicesAndAliasesResolver.java index fe9bec298c8bd..10afb8d7a2ea6 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/IndicesAndAliasesResolver.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/IndicesAndAliasesResolver.java @@ -48,7 +48,7 @@ class IndicesAndAliasesResolver { - //`*,-*` what we replace indices and aliases with if we need Elasticsearch to return empty responses without throwing exception + // `*,-*` what we replace indices and aliases with if we need Elasticsearch to return empty responses without throwing exception static final String[] NO_INDICES_OR_ALIASES_ARRAY = new String[] { "*", "-*" }; static final List NO_INDICES_OR_ALIASES_LIST = Arrays.asList(NO_INDICES_OR_ALIASES_ARRAY); @@ -171,10 +171,10 @@ ResolvedIndices resolveIndicesAndAliasesWithoutWildcards(String action, IndicesR ); } - //NOTE: shard level requests do support wildcards (as they hold the original indices options) but don't support + // NOTE: shard level requests do support wildcards (as they hold the original indices options) but don't support // replacing their indices. - //That is fine though because they never contain wildcards, as they get replaced as part of the authorization of their - //corresponding parent request on the coordinating node. Hence wildcards don't need to get replaced nor exploded for + // That is fine though because they never contain wildcards, as they get replaced as part of the authorization of their + // corresponding parent request on the coordinating node. Hence wildcards don't need to get replaced nor exploded for // shard level requests. final List localIndices = new ArrayList<>(indices.length); for (String name : indices) { @@ -183,8 +183,12 @@ ResolvedIndices resolveIndicesAndAliasesWithoutWildcards(String action, IndicesR return new ResolvedIndices(localIndices, List.of()); } - ResolvedIndices resolveIndicesAndAliases(String action, IndicesRequest indicesRequest, Metadata metadata, - Set authorizedIndices) { + ResolvedIndices resolveIndicesAndAliases( + String action, + IndicesRequest indicesRequest, + Metadata metadata, + Set authorizedIndices + ) { final ResolvedIndices.Builder resolvedIndicesBuilder = new ResolvedIndices.Builder(); boolean indicesReplacedWithNoIndices = false; if (indicesRequest instanceof PutMappingRequest && ((PutMappingRequest) indicesRequest).getConcreteIndex() != null) { @@ -194,7 +198,7 @@ ResolvedIndices resolveIndicesAndAliases(String action, IndicesRequest indicesRe * the list of indices in there, if we do so it will result in an invalid request and the update will fail. */ assert indicesRequest.indices() == null || indicesRequest.indices().length == 0 - : "indices are: " + Arrays.toString(indicesRequest.indices()); // Arrays.toString() can handle null values - all good + : "indices are: " + Arrays.toString(indicesRequest.indices()); // Arrays.toString() can handle null values - all good resolvedIndicesBuilder.addLocal(getPutMappingIndexOrAlias((PutMappingRequest) indicesRequest, authorizedIndices, metadata)); } else if (indicesRequest instanceof IndicesRequest.Replaceable) { final IndicesRequest.Replaceable replaceable = (IndicesRequest.Replaceable) indicesRequest; @@ -205,8 +209,14 @@ ResolvedIndices resolveIndicesAndAliases(String action, IndicesRequest indicesRe if (IndexNameExpressionResolver.isAllIndices(indicesList(indicesRequest.indices()))) { if (replaceWildcards) { for (String authorizedIndex : authorizedIndices) { - if (IndexAbstractionResolver.isIndexVisible("*", authorizedIndex, indicesOptions, metadata, nameExpressionResolver, - indicesRequest.includeDataStreams())) { + if (IndexAbstractionResolver.isIndexVisible( + "*", + authorizedIndex, + indicesOptions, + metadata, + nameExpressionResolver, + indicesRequest.includeDataStreams() + )) { resolvedIndicesBuilder.addLocal(authorizedIndex); } } @@ -220,17 +230,23 @@ ResolvedIndices resolveIndicesAndAliases(String action, IndicesRequest indicesRe } else { split = new ResolvedIndices(Arrays.asList(indicesRequest.indices()), Collections.emptyList()); } - List replaced = indexAbstractionResolver.resolveIndexAbstractions(split.getLocal(), indicesOptions, metadata, - authorizedIndices, replaceWildcards, indicesRequest.includeDataStreams()); + List replaced = indexAbstractionResolver.resolveIndexAbstractions( + split.getLocal(), + indicesOptions, + metadata, + authorizedIndices, + replaceWildcards, + indicesRequest.includeDataStreams() + ); resolvedIndicesBuilder.addLocal(replaced); resolvedIndicesBuilder.addRemote(split.getRemote()); } if (resolvedIndicesBuilder.isEmpty()) { if (indicesOptions.allowNoIndices()) { - //this is how we tell es core to return an empty response, we can let the request through being sure - //that the '-*' wildcard expression will be resolved to no indices. We can't let empty indices through - //as that would be resolved to _all by es core. + // this is how we tell es core to return an empty response, we can let the request through being sure + // that the '-*' wildcard expression will be resolved to no indices. We can't let empty indices through + // as that would be resolved to _all by es core. replaceable.indices(NO_INDICES_OR_ALIASES_ARRAY); indicesReplacedWithNoIndices = true; resolvedIndicesBuilder.addLocal(NO_INDEX_PLACEHOLDER); @@ -250,22 +266,30 @@ ResolvedIndices resolveIndicesAndAliases(String action, IndicesRequest indicesRe } if (indicesRequest instanceof AliasesRequest) { - //special treatment for AliasesRequest since we need to replace wildcards among the specified aliases too. - //AliasesRequest extends IndicesRequest.Replaceable, hence its indices have already been properly replaced. + // special treatment for AliasesRequest since we need to replace wildcards among the specified aliases too. + // AliasesRequest extends IndicesRequest.Replaceable, hence its indices have already been properly replaced. AliasesRequest aliasesRequest = (AliasesRequest) indicesRequest; if (aliasesRequest.expandAliasesWildcards()) { - List aliases = replaceWildcardsWithAuthorizedAliases(aliasesRequest.aliases(), - loadAuthorizedAliases(authorizedIndices, metadata)); + List aliases = replaceWildcardsWithAuthorizedAliases( + aliasesRequest.aliases(), + loadAuthorizedAliases(authorizedIndices, metadata) + ); aliasesRequest.replaceAliases(aliases.toArray(new String[aliases.size()])); } if (indicesReplacedWithNoIndices) { if (indicesRequest instanceof GetAliasesRequest == false) { - throw new IllegalStateException(GetAliasesRequest.class.getSimpleName() + " is the only known " + - "request implementing " + AliasesRequest.class.getSimpleName() + " that may allow no indices. Found [" + - indicesRequest.getClass().getName() + "] which ended up with an empty set of indices."); + throw new IllegalStateException( + GetAliasesRequest.class.getSimpleName() + + " is the only known " + + "request implementing " + + AliasesRequest.class.getSimpleName() + + " that may allow no indices. Found [" + + indicesRequest.getClass().getName() + + "] which ended up with an empty set of indices." + ); } - //if we replaced the indices with '-*' we shouldn't be adding the aliases to the list otherwise the request will - //not get authorized. Leave only '-*' and ignore the rest, result will anyway be empty. + // if we replaced the indices with '-*' we shouldn't be adding the aliases to the list otherwise the request will + // not get authorized. Leave only '-*' and ignore the rest, result will anyway be empty. } else { resolvedIndicesBuilder.addLocal(aliasesRequest.aliases()); } @@ -299,8 +323,13 @@ static String getPutMappingIndexOrAlias(PutMappingRequest request, Set a if (indexAbstraction == null) { resolvedAliasOrIndex = concreteIndexName; } else if (indexAbstraction.getType() != IndexAbstraction.Type.CONCRETE_INDEX) { - throw new IllegalStateException("concrete index [" + concreteIndexName + "] is a [" + - indexAbstraction.getType().getDisplayName() + "], but a concrete index is expected"); + throw new IllegalStateException( + "concrete index [" + + concreteIndexName + + "] is a [" + + indexAbstraction.getType().getDisplayName() + + "], but a concrete index is expected" + ); } else if (authorizedIndicesList.contains(concreteIndexName)) { // user is authorized to put mappings for this index resolvedAliasOrIndex = concreteIndexName; @@ -409,9 +438,10 @@ protected void updateRemoteCluster(String clusterAlias, Settings settings) { ResolvedIndices splitLocalAndRemoteIndexNames(String... indices) { final Map> map = super.groupClusterIndices(clusters, indices); final List local = map.remove(LOCAL_CLUSTER_GROUP_KEY); - final List remote = map.entrySet().stream() - .flatMap(e -> e.getValue().stream().map(v -> e.getKey() + REMOTE_CLUSTER_INDEX_SEPARATOR + v)) - .collect(Collectors.toList()); + final List remote = map.entrySet() + .stream() + .flatMap(e -> e.getValue().stream().map(v -> e.getKey() + REMOTE_CLUSTER_INDEX_SEPARATOR + v)) + .collect(Collectors.toList()); return new ResolvedIndices(local == null ? Collections.emptyList() : local, remote); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/RBACEngine.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/RBACEngine.java index ed0cebe5c4526..55cec41983ae1 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/RBACEngine.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/RBACEngine.java @@ -100,7 +100,12 @@ public class RBACEngine implements AuthorizationEngine { private static final Predicate SAME_USER_PRIVILEGE = StringMatcher.of( - ChangePasswordAction.NAME, AuthenticateAction.NAME, HasPrivilegesAction.NAME, GetUserPrivilegesAction.NAME, GetApiKeyAction.NAME); + ChangePasswordAction.NAME, + AuthenticateAction.NAME, + HasPrivilegesAction.NAME, + GetUserPrivilegesAction.NAME, + GetApiKeyAction.NAME + ); private static final String INDEX_SUB_REQUEST_PRIMARY = IndexAction.NAME + "[p]"; private static final String INDEX_SUB_REQUEST_REPLICA = IndexAction.NAME + "[r]"; private static final String DELETE_SUB_REQUEST_PRIMARY = DeleteAction.NAME + "[p]"; @@ -121,9 +126,14 @@ public void resolveAuthorizationInfo(RequestInfo requestInfo, ActionListener { if (authentication.getUser().isRunAs()) { - getRoles(authentication.getUser().authenticatedUser(), authentication, ActionListener.wrap( - authenticatedUserRole -> listener.onResponse(new RBACAuthorizationInfo(role, authenticatedUserRole)), - listener::onFailure)); + getRoles( + authentication.getUser().authenticatedUser(), + authentication, + ActionListener.wrap( + authenticatedUserRole -> listener.onResponse(new RBACAuthorizationInfo(role, authenticatedUserRole)), + listener::onFailure + ) + ); } else { listener.onResponse(new RBACAuthorizationInfo(role, role)); } @@ -138,17 +148,20 @@ private void getRoles(User user, Authentication authentication, ActionListener listener) { if (authorizationInfo instanceof RBACAuthorizationInfo) { final Role role = ((RBACAuthorizationInfo) authorizationInfo).getAuthenticatedUserAuthorizationInfo().getRole(); - listener.onResponse( - new AuthorizationResult(role.checkRunAs(requestInfo.getAuthentication().getUser().principal()))); + listener.onResponse(new AuthorizationResult(role.checkRunAs(requestInfo.getAuthentication().getUser().principal()))); } else { - listener.onFailure(new IllegalArgumentException("unsupported authorization info:" + - authorizationInfo.getClass().getSimpleName())); + listener.onFailure( + new IllegalArgumentException("unsupported authorization info:" + authorizationInfo.getClass().getSimpleName()) + ); } } @Override - public void authorizeClusterAction(RequestInfo requestInfo, AuthorizationInfo authorizationInfo, - ActionListener listener) { + public void authorizeClusterAction( + RequestInfo requestInfo, + AuthorizationInfo authorizationInfo, + ActionListener listener + ) { if (authorizationInfo instanceof RBACAuthorizationInfo) { final Role role = ((RBACAuthorizationInfo) authorizationInfo).getRole(); if (role.checkClusterAction(requestInfo.getAction(), requestInfo.getRequest(), requestInfo.getAuthentication())) { @@ -159,8 +172,9 @@ public void authorizeClusterAction(RequestInfo requestInfo, AuthorizationInfo au listener.onResponse(AuthorizationResult.deny()); } } else { - listener.onFailure(new IllegalArgumentException("unsupported authorization info:" + - authorizationInfo.getClass().getSimpleName())); + listener.onFailure( + new IllegalArgumentException("unsupported authorization info:" + authorizationInfo.getClass().getSimpleName()) + ); } } @@ -181,9 +195,10 @@ boolean checkSameUserPermissions(String action, TransportRequest request, Authen return checkChangePasswordAction(authentication); } - assert AuthenticateAction.NAME.equals(action) || HasPrivilegesAction.NAME.equals(action) - || GetUserPrivilegesAction.NAME.equals(action) || sameUsername == false - : "Action '" + action + "' should not be possible when sameUsername=" + sameUsername; + assert AuthenticateAction.NAME.equals(action) + || HasPrivilegesAction.NAME.equals(action) + || GetUserPrivilegesAction.NAME.equals(action) + || sameUsername == false : "Action '" + action + "' should not be possible when sameUsername=" + sameUsername; return sameUsername; } else if (request instanceof GetApiKeyRequest) { GetApiKeyRequest getApiKeyRequest = (GetApiKeyRequest) request; @@ -227,9 +242,14 @@ private static boolean shouldAuthorizeIndexActionNameOnly(String action, Transpo return false; } if (request instanceof CompositeIndicesRequest == false) { - throw new IllegalStateException("Composite and bulk actions must implement " + - CompositeIndicesRequest.class.getSimpleName() + ", " + request.getClass().getSimpleName() + " doesn't. Action " + - action); + throw new IllegalStateException( + "Composite and bulk actions must implement " + + CompositeIndicesRequest.class.getSimpleName() + + ", " + + request.getClass().getSimpleName() + + " doesn't. Action " + + action + ); } return true; default: @@ -238,10 +258,13 @@ private static boolean shouldAuthorizeIndexActionNameOnly(String action, Transpo } @Override - public void authorizeIndexAction(RequestInfo requestInfo, AuthorizationInfo authorizationInfo, - AsyncSupplier indicesAsyncSupplier, - Map aliasOrIndexLookup, - ActionListener listener) { + public void authorizeIndexAction( + RequestInfo requestInfo, + AuthorizationInfo authorizationInfo, + AsyncSupplier indicesAsyncSupplier, + Map aliasOrIndexLookup, + ActionListener listener + ) { final String action = requestInfo.getAction(); final TransportRequest request = requestInfo.getRequest(); if (TransportActionProxy.isProxyAction(action) || shouldAuthorizeIndexActionNameOnly(action, request)) { @@ -269,16 +292,13 @@ public void authorizeIndexAction(RequestInfo requestInfo, AuthorizationInfo auth // index and if they cannot, we can fail the request early before we allow the execution of the action and in // turn the shard actions if (SearchScrollAction.NAME.equals(action)) { - ActionRunnable.supply( - ActionListener.wrap(parsedScrollId -> { - if (parsedScrollId.hasLocalIndices()) { - listener.onResponse(authorizeIndexActionName(action, authorizationInfo, null)); - } else { - listener.onResponse(new IndexAuthorizationResult(true, null)); - } - }, listener::onFailure), - ((SearchScrollRequest) request)::parseScrollId - ).run(); + ActionRunnable.supply(ActionListener.wrap(parsedScrollId -> { + if (parsedScrollId.hasLocalIndices()) { + listener.onResponse(authorizeIndexActionName(action, authorizationInfo, null)); + } else { + listener.onResponse(new IndexAuthorizationResult(true, null)); + } + }, listener::onFailure), ((SearchScrollRequest) request)::parseScrollId).run(); } else { // RBACEngine simply authorizes scroll related actions without filling in any DLS/FLS permissions. // Scroll related actions have special security logic, where the security context of the initial search @@ -302,12 +322,17 @@ public void authorizeIndexAction(RequestInfo requestInfo, AuthorizationInfo auth listener.onResponse(new IndexAuthorizationResult(true, IndicesAccessControl.ALLOW_NO_INDICES)); } } else if (action.equals(ClosePointInTimeAction.NAME)) { - listener.onResponse(new IndexAuthorizationResult(true, IndicesAccessControl.ALLOW_NO_INDICES)); + listener.onResponse(new IndexAuthorizationResult(true, IndicesAccessControl.ALLOW_NO_INDICES)); } else { - assert false : "only scroll and async-search related requests are known indices api that don't " + - "support retrieving the indices they relate to"; - listener.onFailure(new IllegalStateException("only scroll and async-search related requests are known indices " + - "api that don't support retrieving the indices they relate to")); + assert false + : "only scroll and async-search related requests are known indices api that don't " + + "support retrieving the indices they relate to"; + listener.onFailure( + new IllegalStateException( + "only scroll and async-search related requests are known indices " + + "api that don't support retrieving the indices they relate to" + ) + ); } } else if (isChildActionAuthorizedByParent(requestInfo, authorizationInfo)) { listener.onResponse( @@ -318,31 +343,46 @@ public void authorizeIndexAction(RequestInfo requestInfo, AuthorizationInfo auth indicesAsyncSupplier.getAsync(ActionListener.wrap(resolvedIndices -> { assert resolvedIndices.isEmpty() == false : "every indices request needs to have its indices set thus the resolved indices must not be empty"; - //all wildcard expressions have been resolved and only the security plugin could have set '-*' here. - //'-*' matches no indices so we allow the request to go through, which will yield an empty response + // all wildcard expressions have been resolved and only the security plugin could have set '-*' here. + // '-*' matches no indices so we allow the request to go through, which will yield an empty response if (resolvedIndices.isNoIndicesPlaceholder()) { // check action name listener.onResponse(authorizeIndexActionName(action, authorizationInfo, IndicesAccessControl.ALLOW_NO_INDICES)); } else { - listener.onResponse(buildIndicesAccessControl( - action, authorizationInfo, Sets.newHashSet(resolvedIndices.getLocal()), aliasOrIndexLookup)); + listener.onResponse( + buildIndicesAccessControl( + action, + authorizationInfo, + Sets.newHashSet(resolvedIndices.getLocal()), + aliasOrIndexLookup + ) + ); } }, listener::onFailure)); } else { try { - final IndexAuthorizationResult indexAuthorizationResult = - authorizeIndexActionName(action, authorizationInfo, IndicesAccessControl.ALLOW_NO_INDICES); + final IndexAuthorizationResult indexAuthorizationResult = authorizeIndexActionName( + action, + authorizationInfo, + IndicesAccessControl.ALLOW_NO_INDICES + ); if (indexAuthorizationResult.isGranted()) { indicesAsyncSupplier.getAsync(ActionListener.wrap(resolvedIndices -> { assert resolvedIndices.isEmpty() == false - : "every indices request needs to have its indices set thus the resolved indices must not be empty"; - //all wildcard expressions have been resolved and only the security plugin could have set '-*' here. - //'-*' matches no indices so we allow the request to go through, which will yield an empty response + : "every indices request needs to have its indices set thus the resolved indices must not be empty"; + // all wildcard expressions have been resolved and only the security plugin could have set '-*' here. + // '-*' matches no indices so we allow the request to go through, which will yield an empty response if (resolvedIndices.isNoIndicesPlaceholder()) { listener.onResponse(new IndexAuthorizationResult(true, IndicesAccessControl.ALLOW_NO_INDICES)); } else { - listener.onResponse(buildIndicesAccessControl( - action, authorizationInfo, Sets.newHashSet(resolvedIndices.getLocal()), aliasOrIndexLookup)); + listener.onResponse( + buildIndicesAccessControl( + action, + authorizationInfo, + Sets.newHashSet(resolvedIndices.getLocal()), + aliasOrIndexLookup + ) + ); } }, listener::onFailure)); } else { @@ -403,11 +443,8 @@ private boolean isChildActionAuthorizedByParent(RequestInfo requestInfo, Authori } for (String idx : indices) { - assert Regex.isSimpleMatchPattern(idx) == false : "Wildcards should already be expanded but action [" - + requestInfo.getAction() - + "] has index [" - + idx - + "]"; + assert Regex.isSimpleMatchPattern(idx) == false + : "Wildcards should already be expanded but action [" + requestInfo.getAction() + "] has index [" + idx + "]"; IndicesAccessControl.IndexAccessControl iac = indicesAccessControl.getIndexPermissions(idx); // The parent context has already successfully authorized access to this index (by name) if (iac == null || iac.isGranted() == false) { @@ -417,30 +454,40 @@ private boolean isChildActionAuthorizedByParent(RequestInfo requestInfo, Authori return true; } - private static IndexAuthorizationResult authorizeIndexActionName(String action, - AuthorizationInfo authorizationInfo, - IndicesAccessControl grantedValue) { + private static IndexAuthorizationResult authorizeIndexActionName( + String action, + AuthorizationInfo authorizationInfo, + IndicesAccessControl grantedValue + ) { final Role role = ensureRBAC(authorizationInfo).getRole(); return new IndexAuthorizationResult(true, role.checkIndicesAction(action) ? grantedValue : IndicesAccessControl.DENIED); } @Override - public void loadAuthorizedIndices(RequestInfo requestInfo, AuthorizationInfo authorizationInfo, - Map indicesLookup, ActionListener> listener) { + public void loadAuthorizedIndices( + RequestInfo requestInfo, + AuthorizationInfo authorizationInfo, + Map indicesLookup, + ActionListener> listener + ) { if (authorizationInfo instanceof RBACAuthorizationInfo) { final Role role = ((RBACAuthorizationInfo) authorizationInfo).getRole(); listener.onResponse(resolveAuthorizedIndicesFromRole(role, requestInfo, indicesLookup)); } else { listener.onFailure( - new IllegalArgumentException("unsupported authorization info:" + authorizationInfo.getClass().getSimpleName())); + new IllegalArgumentException("unsupported authorization info:" + authorizationInfo.getClass().getSimpleName()) + ); } } @Override - public void validateIndexPermissionsAreSubset(RequestInfo requestInfo, AuthorizationInfo authorizationInfo, - Map> indexNameToNewNames, - ActionListener listener) { + public void validateIndexPermissionsAreSubset( + RequestInfo requestInfo, + AuthorizationInfo authorizationInfo, + Map> indexNameToNewNames, + ActionListener listener + ) { if (authorizationInfo instanceof RBACAuthorizationInfo) { final Role role = ((RBACAuthorizationInfo) authorizationInfo).getRole(); Map permissionMap = new HashMap<>(); @@ -457,27 +504,35 @@ public void validateIndexPermissionsAreSubset(RequestInfo requestInfo, Authoriza listener.onResponse(AuthorizationResult.granted()); } else { listener.onFailure( - new IllegalArgumentException("unsupported authorization info:" + authorizationInfo.getClass().getSimpleName())); + new IllegalArgumentException("unsupported authorization info:" + authorizationInfo.getClass().getSimpleName()) + ); } } @Override - public void checkPrivileges(Authentication authentication, AuthorizationInfo authorizationInfo, - HasPrivilegesRequest request, - Collection applicationPrivileges, - ActionListener listener) { + public void checkPrivileges( + Authentication authentication, + AuthorizationInfo authorizationInfo, + HasPrivilegesRequest request, + Collection applicationPrivileges, + ActionListener listener + ) { if (authorizationInfo instanceof RBACAuthorizationInfo == false) { listener.onFailure( - new IllegalArgumentException("unsupported authorization info:" + authorizationInfo.getClass().getSimpleName())); + new IllegalArgumentException("unsupported authorization info:" + authorizationInfo.getClass().getSimpleName()) + ); return; } final Role userRole = ((RBACAuthorizationInfo) authorizationInfo).getRole(); - logger.trace(() -> new ParameterizedMessage("Check whether role [{}] has privileges cluster=[{}] index=[{}] application=[{}]", - Strings.arrayToCommaDelimitedString(userRole.names()), - Strings.arrayToCommaDelimitedString(request.clusterPrivileges()), - Strings.arrayToCommaDelimitedString(request.indexPrivileges()), - Strings.arrayToCommaDelimitedString(request.applicationPrivileges()) - )); + logger.trace( + () -> new ParameterizedMessage( + "Check whether role [{}] has privileges cluster=[{}] index=[{}] application=[{}]", + Strings.arrayToCommaDelimitedString(userRole.names()), + Strings.arrayToCommaDelimitedString(request.clusterPrivileges()), + Strings.arrayToCommaDelimitedString(request.indexPrivileges()), + Strings.arrayToCommaDelimitedString(request.applicationPrivileges()) + ) + ); Map cluster = new HashMap<>(); for (String checkAction : request.clusterPrivileges()) { @@ -486,8 +541,11 @@ public void checkPrivileges(Authentication authentication, AuthorizationInfo aut boolean allMatch = cluster.values().stream().allMatch(Boolean::booleanValue); ResourcePrivilegesMap.Builder combineIndicesResourcePrivileges = ResourcePrivilegesMap.builder(); for (RoleDescriptor.IndicesPrivileges check : request.indexPrivileges()) { - ResourcePrivilegesMap resourcePrivileges = userRole.checkIndicesPrivileges(Sets.newHashSet(check.getIndices()), - check.allowRestrictedIndices(), Sets.newHashSet(check.getPrivileges())); + ResourcePrivilegesMap resourcePrivileges = userRole.checkIndicesPrivileges( + Sets.newHashSet(check.getIndices()), + check.allowRestrictedIndices(), + Sets.newHashSet(check.getPrivileges()) + ); allMatch = allMatch && resourcePrivileges.allAllowed(); combineIndicesResourcePrivileges.addResourcePrivilegesMap(resourcePrivileges); } @@ -500,8 +558,12 @@ public void checkPrivileges(Authentication authentication, AuthorizationInfo aut ResourcePrivilegesMap.Builder builder = ResourcePrivilegesMap.builder(); for (RoleDescriptor.ApplicationResourcePrivileges p : request.applicationPrivileges()) { if (applicationName.equals(p.getApplication())) { - ResourcePrivilegesMap appPrivsByResourceMap = userRole.checkApplicationResourcePrivileges(applicationName, - Sets.newHashSet(p.getResources()), Sets.newHashSet(p.getPrivileges()), applicationPrivileges); + ResourcePrivilegesMap appPrivsByResourceMap = userRole.checkApplicationResourcePrivileges( + applicationName, + Sets.newHashSet(p.getResources()), + Sets.newHashSet(p.getPrivileges()), + applicationPrivileges + ); builder.addResourcePrivilegesMap(appPrivsByResourceMap); } } @@ -510,17 +572,28 @@ public void checkPrivileges(Authentication authentication, AuthorizationInfo aut privilegesByApplication.put(applicationName, resourcePrivsForApplication.getResourceToResourcePrivileges().values()); } - listener.onResponse(new HasPrivilegesResponse(request.username(), allMatch, cluster, - allIndices.getResourceToResourcePrivileges().values(), privilegesByApplication)); + listener.onResponse( + new HasPrivilegesResponse( + request.username(), + allMatch, + cluster, + allIndices.getResourceToResourcePrivileges().values(), + privilegesByApplication + ) + ); } - @Override - public void getUserPrivileges(Authentication authentication, AuthorizationInfo authorizationInfo, GetUserPrivilegesRequest request, - ActionListener listener) { + public void getUserPrivileges( + Authentication authentication, + AuthorizationInfo authorizationInfo, + GetUserPrivilegesRequest request, + ActionListener listener + ) { if (authorizationInfo instanceof RBACAuthorizationInfo == false) { listener.onFailure( - new IllegalArgumentException("unsupported authorization info:" + authorizationInfo.getClass().getSimpleName())); + new IllegalArgumentException("unsupported authorization info:" + authorizationInfo.getClass().getSimpleName()) + ); } else { final Role role = ((RBACAuthorizationInfo) authorizationInfo).getRole(); listener.onResponse(buildUserPrivilegesResponseObject(role)); @@ -541,8 +614,10 @@ GetUserPrivilegesResponse buildUserPrivilegesResponseObject(Role userRole) { conditionalCluster.add((ConfigurableClusterPrivilege) privilege); } else { throw new IllegalArgumentException( - "found unsupported cluster privilege : " + privilege + - ((privilege != null) ? " of type " + privilege.getClass().getSimpleName() : "")); + "found unsupported cluster privilege : " + + privilege + + ((privilege != null) ? " of type " + privilege.getClass().getSimpleName() : "") + ); } } @@ -558,13 +633,15 @@ GetUserPrivilegesResponse buildUserPrivilegesResponseObject(Role userRole) { } else { fieldSecurity = Collections.emptySet(); } - indices.add(new GetUserPrivilegesResponse.Indices( - Arrays.asList(group.indices()), - group.privilege().name(), - fieldSecurity, - queries, - group.allowRestrictedIndices() - )); + indices.add( + new GetUserPrivilegesResponse.Indices( + Arrays.asList(group.indices()), + group.privilege().name(), + fieldSecurity, + queries, + group.allowRestrictedIndices() + ) + ); } final Set application = new LinkedHashSet<>(); @@ -574,11 +651,13 @@ GetUserPrivilegesResponse buildUserPrivilegesResponseObject(Role userRole) { if (resources.isEmpty()) { logger.trace("No resources defined in application privilege {}", privilege); } else { - application.add(RoleDescriptor.ApplicationResourcePrivileges.builder() - .application(applicationName) - .privileges(privilege.name()) - .resources(resources) - .build()); + application.add( + RoleDescriptor.ApplicationResourcePrivileges.builder() + .application(applicationName) + .privileges(privilege.name()) + .resources(resources) + .build() + ); } } } @@ -625,10 +704,12 @@ static Set resolveAuthorizedIndicesFromRole(Role role, RequestInfo reque return Collections.unmodifiableSet(indicesAndAliases); } - private IndexAuthorizationResult buildIndicesAccessControl(String action, - AuthorizationInfo authorizationInfo, - Set indices, - Map aliasAndIndexLookup) { + private IndexAuthorizationResult buildIndicesAccessControl( + String action, + AuthorizationInfo authorizationInfo, + Set indices, + Map aliasAndIndexLookup + ) { final Role role = ensureRBAC(authorizationInfo).getRole(); final IndicesAccessControl accessControl = role.authorize(action, indices, aliasAndIndexLookup, fieldPermissionsCache); return new IndexAuthorizationResult(true, accessControl); @@ -678,8 +759,9 @@ static class RBACAuthorizationInfo implements AuthorizationInfo { RBACAuthorizationInfo(Role role, Role authenticatedUserRole) { this.role = Objects.requireNonNull(role); this.info = Collections.singletonMap(PRINCIPAL_ROLES_FIELD_NAME, role.names()); - this.authenticatedUserAuthorizationInfo = - authenticatedUserRole == null ? this : new RBACAuthorizationInfo(authenticatedUserRole, null); + this.authenticatedUserAuthorizationInfo = authenticatedUserRole == null + ? this + : new RBACAuthorizationInfo(authenticatedUserRole, null); } Role getRole() { @@ -724,21 +806,21 @@ public int hashCode() { } private static boolean isScrollRelatedAction(String action) { - return action.equals(SearchScrollAction.NAME) || - action.equals(SearchTransportService.FETCH_ID_SCROLL_ACTION_NAME) || - action.equals(SearchTransportService.QUERY_FETCH_SCROLL_ACTION_NAME) || - action.equals(SearchTransportService.QUERY_SCROLL_ACTION_NAME) || - action.equals(SearchTransportService.FREE_CONTEXT_SCROLL_ACTION_NAME) || - action.equals(ClearScrollAction.NAME) || - action.equals("indices:data/read/sql/close_cursor") || - action.equals(SearchTransportService.CLEAR_SCROLL_CONTEXTS_ACTION_NAME); + return action.equals(SearchScrollAction.NAME) + || action.equals(SearchTransportService.FETCH_ID_SCROLL_ACTION_NAME) + || action.equals(SearchTransportService.QUERY_FETCH_SCROLL_ACTION_NAME) + || action.equals(SearchTransportService.QUERY_SCROLL_ACTION_NAME) + || action.equals(SearchTransportService.FREE_CONTEXT_SCROLL_ACTION_NAME) + || action.equals(ClearScrollAction.NAME) + || action.equals("indices:data/read/sql/close_cursor") + || action.equals(SearchTransportService.CLEAR_SCROLL_CONTEXTS_ACTION_NAME); } private static boolean isAsyncRelatedAction(String action) { - return action.equals(SubmitAsyncSearchAction.NAME) || - action.equals(GetAsyncSearchAction.NAME) || - action.equals(DeleteAsyncResultAction.NAME) || - action.equals(EqlAsyncActionNames.EQL_ASYNC_GET_RESULT_ACTION_NAME) || - action.equals(SqlAsyncActionNames.SQL_ASYNC_GET_RESULT_ACTION_NAME); + return action.equals(SubmitAsyncSearchAction.NAME) + || action.equals(GetAsyncSearchAction.NAME) + || action.equals(DeleteAsyncResultAction.NAME) + || action.equals(EqlAsyncActionNames.EQL_ASYNC_GET_RESULT_ACTION_NAME) + || action.equals(SqlAsyncActionNames.SQL_ASYNC_GET_RESULT_ACTION_NAME); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/SecuritySearchOperationListener.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/SecuritySearchOperationListener.java index f1da24fa70395..a12861def74d6 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/SecuritySearchOperationListener.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/SecuritySearchOperationListener.java @@ -54,8 +54,8 @@ public void onNewScrollContext(ReaderContext readerContext) { readerContext.putInContext(AuthenticationField.AUTHENTICATION_KEY, securityContext.getAuthentication()); // store the DLS and FLS permissions of the initial search request that created the scroll // this is then used to assert the DLS/FLS permission for the scroll search action - IndicesAccessControl indicesAccessControl = - securityContext.getThreadContext().getTransient(AuthorizationServiceField.INDICES_PERMISSIONS_KEY); + IndicesAccessControl indicesAccessControl = securityContext.getThreadContext() + .getTransient(AuthorizationServiceField.INDICES_PERMISSIONS_KEY); assert indicesAccessControl != null : "thread context does not contain index access control"; readerContext.putInContext(AuthorizationServiceField.INDICES_PERMISSIONS_KEY, indicesAccessControl); } @@ -71,16 +71,25 @@ public void validateReaderContext(ReaderContext readerContext, TransportRequest final Authentication current = securityContext.getAuthentication(); final ThreadContext threadContext = securityContext.getThreadContext(); final String action = threadContext.getTransient(ORIGINATING_ACTION_KEY); - ensureAuthenticatedUserIsSame(originalAuth, current, auditTrailService, readerContext.id(), action, request, - AuditUtil.extractRequestId(threadContext), threadContext.getTransient(AUTHORIZATION_INFO_KEY)); + ensureAuthenticatedUserIsSame( + originalAuth, + current, + auditTrailService, + readerContext.id(), + action, + request, + AuditUtil.extractRequestId(threadContext), + threadContext.getTransient(AUTHORIZATION_INFO_KEY) + ); // piggyback on context validation to assert the DLS/FLS permissions on the thread context of the scroll search handler if (null == securityContext.getThreadContext().getTransient(AuthorizationServiceField.INDICES_PERMISSIONS_KEY)) { // fill in the DLS and FLS permissions for the scroll search action from the scroll context - IndicesAccessControl scrollIndicesAccessControl = - readerContext.getFromContext(AuthorizationServiceField.INDICES_PERMISSIONS_KEY); + IndicesAccessControl scrollIndicesAccessControl = readerContext.getFromContext( + AuthorizationServiceField.INDICES_PERMISSIONS_KEY + ); assert scrollIndicesAccessControl != null : "scroll does not contain index access control"; - securityContext.getThreadContext().putTransient(AuthorizationServiceField.INDICES_PERMISSIONS_KEY, - scrollIndicesAccessControl); + securityContext.getThreadContext() + .putTransient(AuthorizationServiceField.INDICES_PERMISSIONS_KEY, scrollIndicesAccessControl); } } } @@ -97,12 +106,18 @@ public void onPreQueryPhase(SearchContext searchContext) { void ensureIndicesAccessControlForScrollThreadContext(SearchContext searchContext) { if (searchContext.readerContext().scrollContext() != null) { - IndicesAccessControl threadIndicesAccessControl = - securityContext.getThreadContext().getTransient(AuthorizationServiceField.INDICES_PERMISSIONS_KEY); + IndicesAccessControl threadIndicesAccessControl = securityContext.getThreadContext() + .getTransient(AuthorizationServiceField.INDICES_PERMISSIONS_KEY); if (null == threadIndicesAccessControl) { - throw new ElasticsearchSecurityException("Unexpected null indices access control for search context [" + - searchContext.id() + "] for request [" + searchContext.request().getDescription() + "] with source [" + - searchContext.source() + "]"); + throw new ElasticsearchSecurityException( + "Unexpected null indices access control for search context [" + + searchContext.id() + + "] for request [" + + searchContext.request().getDescription() + + "] with source [" + + searchContext.source() + + "]" + ); } } } @@ -113,9 +128,16 @@ void ensureIndicesAccessControlForScrollThreadContext(SearchContext searchContex * be the same. Some things that could differ include the roles, the name of the authenticating * (or lookup) realm. To work around this we compare the username and the originating realm type. */ - static void ensureAuthenticatedUserIsSame(Authentication original, Authentication current, AuditTrailService auditTrailService, - ShardSearchContextId id, String action, TransportRequest request, String requestId, - AuthorizationInfo authorizationInfo) { + static void ensureAuthenticatedUserIsSame( + Authentication original, + Authentication current, + AuditTrailService auditTrailService, + ShardSearchContextId id, + String action, + TransportRequest request, + String requestId, + AuthorizationInfo authorizationInfo + ) { final boolean sameUser = original.canAccessResourcesOf(current); if (sameUser == false) { auditTrailService.get().accessDenied(requestId, current, action, request, authorizationInfo); diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/accesscontrol/FieldExtractor.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/accesscontrol/FieldExtractor.java index 309ca6da30da0..7108718eaf938 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/accesscontrol/FieldExtractor.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/accesscontrol/FieldExtractor.java @@ -60,9 +60,9 @@ static void extractFields(Query query, Set fields) throws UnsupportedOpe } else if (query instanceof SpanTermQuery) { // we just do SpanTerm, other spans are trickier, they could contain // the evil FieldMaskingSpanQuery: so SpanQuery.getField cannot be trusted. - fields.add(((SpanTermQuery)query).getField()); + fields.add(((SpanTermQuery) query).getField()); } else if (query instanceof TermQuery) { - fields.add(((TermQuery)query).getTerm().field()); + fields.add(((TermQuery) query).getTerm().field()); } else if (query instanceof SynonymQuery) { SynonymQuery q = (SynonymQuery) query; // all terms must have the same field @@ -76,13 +76,13 @@ static void extractFields(Query query, Set fields) throws UnsupportedOpe // all terms must have the same field fields.add(q.getTermArrays()[0][0].field()); } else if (query instanceof PointRangeQuery) { - fields.add(((PointRangeQuery)query).getField()); + fields.add(((PointRangeQuery) query).getField()); } else if (query instanceof PointInSetQuery) { - fields.add(((PointInSetQuery)query).getField()); + fields.add(((PointInSetQuery) query).getField()); } else if (query instanceof DocValuesFieldExistsQuery) { - fields.add(((DocValuesFieldExistsQuery)query).getField()); + fields.add(((DocValuesFieldExistsQuery) query).getField()); } else if (query instanceof DocValuesNumbersQuery) { - fields.add(((DocValuesNumbersQuery)query).getField()); + fields.add(((DocValuesNumbersQuery) query).getField()); } else if (query instanceof IndexOrDocValuesQuery) { // Both queries are supposed to be equivalent, so if any of them can be extracted, we are good try { diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/accesscontrol/OptOutQueryCache.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/accesscontrol/OptOutQueryCache.java index 27ff050bd97ba..721b05db50990 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/accesscontrol/OptOutQueryCache.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/accesscontrol/OptOutQueryCache.java @@ -31,10 +31,7 @@ public final class OptOutQueryCache extends AbstractIndexComponent implements Qu private final ThreadContext context; private final String indexName; - public OptOutQueryCache( - final IndexSettings indexSettings, - final IndicesQueryCache indicesQueryCache, - final ThreadContext context) { + public OptOutQueryCache(final IndexSettings indexSettings, final IndicesQueryCache indicesQueryCache, final ThreadContext context) { super(indexSettings); this.indicesQueryCache = indicesQueryCache; this.context = Objects.requireNonNull(context, "threadContext must not be null"); @@ -46,7 +43,6 @@ public void close() throws ElasticsearchException { clear("close"); } - @Override public void clear(final String reason) { logger.debug("full cache clear, reason [{}]", reason); @@ -55,8 +51,7 @@ public void clear(final String reason) { @Override public Weight doCache(Weight weight, QueryCachingPolicy policy) { - IndicesAccessControl indicesAccessControl = context.getTransient( - AuthorizationServiceField.INDICES_PERMISSIONS_KEY); + IndicesAccessControl indicesAccessControl = context.getTransient(AuthorizationServiceField.INDICES_PERMISSIONS_KEY); if (indicesAccessControl == null) { logger.debug("opting out of the query cache. current request doesn't hold indices permissions"); return weight; diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/interceptor/BulkShardRequestInterceptor.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/interceptor/BulkShardRequestInterceptor.java index 4055e36a5a3dc..bb380fb48c507 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/interceptor/BulkShardRequestInterceptor.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/interceptor/BulkShardRequestInterceptor.java @@ -41,8 +41,12 @@ public BulkShardRequestInterceptor(ThreadPool threadPool, XPackLicenseState lice } @Override - public void intercept(RequestInfo requestInfo, AuthorizationEngine authzEngine, AuthorizationInfo authorizationInfo, - ActionListener listener) { + public void intercept( + RequestInfo requestInfo, + AuthorizationEngine authzEngine, + AuthorizationInfo authorizationInfo, + ActionListener listener + ) { if (requestInfo.getRequest() instanceof BulkShardRequest) { IndicesAccessControl indicesAccessControl = threadContext.getTransient(AuthorizationServiceField.INDICES_PERMISSIONS_KEY); BulkShardRequest bulkShardRequest = (BulkShardRequest) requestInfo.getRequest(); @@ -61,14 +65,21 @@ public void intercept(RequestInfo requestInfo, AuthorizationEngine authzEngine, if ((fls || dls) && isDlsLicensed) { found = true; logger.trace("aborting bulk item update request for index [{}]", bulkShardRequest.index()); - bulkItemRequest.abort(bulkItemRequest.index(), new ElasticsearchSecurityException("Can't execute a bulk " + - "item request with update requests embedded if field or document level security is enabled", - RestStatus.BAD_REQUEST)); + bulkItemRequest.abort( + bulkItemRequest.index(), + new ElasticsearchSecurityException( + "Can't execute a bulk " + + "item request with update requests embedded if field or document level security is enabled", + RestStatus.BAD_REQUEST + ) + ); } } if (found == false) { - logger.trace("intercepted bulk request for index [{}] without any update requests, continuing execution", - bulkShardRequest.index()); + logger.trace( + "intercepted bulk request for index [{}] without any update requests, continuing execution", + bulkShardRequest.index() + ); } } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/interceptor/DlsFlsLicenseRequestInterceptor.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/interceptor/DlsFlsLicenseRequestInterceptor.java index 4db0eaf9ef2ef..6852e1094d540 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/interceptor/DlsFlsLicenseRequestInterceptor.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/interceptor/DlsFlsLicenseRequestInterceptor.java @@ -43,8 +43,8 @@ public void intercept( AuthorizationEngine.RequestInfo requestInfo, AuthorizationEngine authorizationEngine, AuthorizationInfo authorizationInfo, - ActionListener listener) - { + ActionListener listener + ) { if (requestInfo.getRequest() instanceof IndicesRequest && false == TransportActionProxy.isProxyAction(requestInfo.getAction())) { final Role role = RBACEngine.maybeGetRBACEngineRole(threadContext.getTransient(AUTHORIZATION_INFO_KEY)); // Checking whether role has FLS or DLS first before checking indicesAccessControl for efficiency because indicesAccessControl @@ -68,10 +68,13 @@ public void intercept( } } if (incompatibleLicense) { - final ElasticsearchSecurityException licenseException = - LicenseUtils.newComplianceException("field and document level security"); + final ElasticsearchSecurityException licenseException = LicenseUtils.newComplianceException( + "field and document level security" + ); licenseException.addMetadata( - "es.indices_with_dls_or_fls", indicesAccessControl.getIndicesWithFieldOrDocumentLevelSecurity()); + "es.indices_with_dls_or_fls", + indicesAccessControl.getIndicesWithFieldOrDocumentLevelSecurity() + ); listener.onFailure(licenseException); return; } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/interceptor/FieldAndDocumentLevelSecurityRequestInterceptor.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/interceptor/FieldAndDocumentLevelSecurityRequestInterceptor.java index 02c11aa13cb95..de00bee28cff0 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/interceptor/FieldAndDocumentLevelSecurityRequestInterceptor.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/interceptor/FieldAndDocumentLevelSecurityRequestInterceptor.java @@ -41,15 +41,20 @@ abstract class FieldAndDocumentLevelSecurityRequestInterceptor implements Reques } @Override - public void intercept(RequestInfo requestInfo, AuthorizationEngine authorizationEngine, AuthorizationInfo authorizationInfo, - ActionListener listener) { + public void intercept( + RequestInfo requestInfo, + AuthorizationEngine authorizationEngine, + AuthorizationInfo authorizationInfo, + ActionListener listener + ) { if (requestInfo.getRequest() instanceof IndicesRequest && false == TransportActionProxy.isProxyAction(requestInfo.getAction())) { IndicesRequest indicesRequest = (IndicesRequest) requestInfo.getRequest(); // TODO: should we check is DLS/FLS feature allowed here if (supports(indicesRequest)) { final boolean isDlsLicensed = DOCUMENT_LEVEL_SECURITY_FEATURE.checkWithoutTracking(licenseState); - final IndicesAccessControl indicesAccessControl - = threadContext.getTransient(AuthorizationServiceField.INDICES_PERMISSIONS_KEY); + final IndicesAccessControl indicesAccessControl = threadContext.getTransient( + AuthorizationServiceField.INDICES_PERMISSIONS_KEY + ); final Map accessControlByIndex = new HashMap<>(); for (String index : requestIndices(indicesRequest)) { IndicesAccessControl.IndexAccessControl indexAccessControl = indicesAccessControl.getIndexPermissions(index); @@ -57,9 +62,13 @@ public void intercept(RequestInfo requestInfo, AuthorizationEngine authorization final boolean flsEnabled = indexAccessControl.getFieldPermissions().hasFieldLevelSecurity(); final boolean dlsEnabled = indexAccessControl.getDocumentPermissions().hasDocumentLevelPermissions(); if ((flsEnabled || dlsEnabled) && isDlsLicensed) { - logger.trace("intercepted request for index [{}] with field level access controls [{}] " + - "document level access controls [{}]. disabling conflicting features", - index, flsEnabled, dlsEnabled); + logger.trace( + "intercepted request for index [{}] with field level access controls [{}] " + + "document level access controls [{}]. disabling conflicting features", + index, + flsEnabled, + dlsEnabled + ); accessControlByIndex.put(index, indexAccessControl); } } else { @@ -75,9 +84,11 @@ public void intercept(RequestInfo requestInfo, AuthorizationEngine authorization listener.onResponse(null); } - abstract void disableFeatures(IndicesRequest indicesRequest, - Map indicesAccessControlByIndex, - ActionListener listener); + abstract void disableFeatures( + IndicesRequest indicesRequest, + Map indicesAccessControlByIndex, + ActionListener listener + ); String[] requestIndices(IndicesRequest indicesRequest) { return indicesRequest.indices(); diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/interceptor/IndicesAliasesRequestInterceptor.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/interceptor/IndicesAliasesRequestInterceptor.java index e44ab7ec80591..de75258fd3926 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/interceptor/IndicesAliasesRequestInterceptor.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/interceptor/IndicesAliasesRequestInterceptor.java @@ -9,8 +9,8 @@ import org.elasticsearch.ElasticsearchSecurityException; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.Tuple; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.xpack.core.security.authz.AuthorizationEngine; @@ -38,34 +38,43 @@ public final class IndicesAliasesRequestInterceptor implements RequestIntercepto private final XPackLicenseState licenseState; private final AuditTrailService auditTrailService; - public IndicesAliasesRequestInterceptor(ThreadContext threadContext, XPackLicenseState licenseState, - AuditTrailService auditTrailService) { + public IndicesAliasesRequestInterceptor( + ThreadContext threadContext, + XPackLicenseState licenseState, + AuditTrailService auditTrailService + ) { this.threadContext = threadContext; this.licenseState = licenseState; this.auditTrailService = auditTrailService; } @Override - public void intercept(RequestInfo requestInfo, AuthorizationEngine authorizationEngine, AuthorizationInfo authorizationInfo, - ActionListener listener) { + public void intercept( + RequestInfo requestInfo, + AuthorizationEngine authorizationEngine, + AuthorizationInfo authorizationInfo, + ActionListener listener + ) { if (requestInfo.getRequest() instanceof IndicesAliasesRequest) { final IndicesAliasesRequest request = (IndicesAliasesRequest) requestInfo.getRequest(); final AuditTrail auditTrail = auditTrailService.get(); final boolean isDlsLicensed = DOCUMENT_LEVEL_SECURITY_FEATURE.checkWithoutTracking(licenseState); - IndicesAccessControl indicesAccessControl = - threadContext.getTransient(AuthorizationServiceField.INDICES_PERMISSIONS_KEY); + IndicesAccessControl indicesAccessControl = threadContext.getTransient(AuthorizationServiceField.INDICES_PERMISSIONS_KEY); for (IndicesAliasesRequest.AliasActions aliasAction : request.getAliasActions()) { if (aliasAction.actionType() == IndicesAliasesRequest.AliasActions.Type.ADD) { for (String index : aliasAction.indices()) { - IndicesAccessControl.IndexAccessControl indexAccessControl = - indicesAccessControl.getIndexPermissions(index); + IndicesAccessControl.IndexAccessControl indexAccessControl = indicesAccessControl.getIndexPermissions(index); if (indexAccessControl != null) { final boolean fls = indexAccessControl.getFieldPermissions().hasFieldLevelSecurity(); final boolean dls = indexAccessControl.getDocumentPermissions().hasDocumentLevelPermissions(); if ((fls || dls) && isDlsLicensed) { - listener.onFailure(new ElasticsearchSecurityException("Alias requests are not allowed for " + - "users who have field or document level security enabled on one of the indices", - RestStatus.BAD_REQUEST)); + listener.onFailure( + new ElasticsearchSecurityException( + "Alias requests are not allowed for " + + "users who have field or document level security enabled on one of the indices", + RestStatus.BAD_REQUEST + ) + ); return; } } @@ -73,29 +82,43 @@ public void intercept(RequestInfo requestInfo, AuthorizationEngine authorization } } - Map> indexToAliasesMap = request.getAliasActions().stream() - .filter(aliasAction -> aliasAction.actionType() == IndicesAliasesRequest.AliasActions.Type.ADD) - .flatMap(aliasActions -> - Arrays.stream(aliasActions.indices()) - .map(indexName -> new Tuple<>(indexName, Arrays.asList(aliasActions.aliases())))) - .collect(Collectors.toMap(Tuple::v1, Tuple::v2, (existing, toMerge) -> { - List list = new ArrayList<>(existing.size() + toMerge.size()); - list.addAll(existing); - list.addAll(toMerge); - return list; - })); - authorizationEngine.validateIndexPermissionsAreSubset(requestInfo, authorizationInfo, indexToAliasesMap, - wrapPreservingContext(ActionListener.wrap(authzResult -> { - if (authzResult.isGranted()) { - // do not audit success again - listener.onResponse(null); - } else { - auditTrail.accessDenied(AuditUtil.extractRequestId(threadContext), requestInfo.getAuthentication(), - requestInfo.getAction(), request, authorizationInfo); - listener.onFailure(Exceptions.authorizationError("Adding an alias is not allowed when the alias " + - "has more permissions than any of the indices")); - } - }, listener::onFailure), threadContext)); + Map> indexToAliasesMap = request.getAliasActions() + .stream() + .filter(aliasAction -> aliasAction.actionType() == IndicesAliasesRequest.AliasActions.Type.ADD) + .flatMap( + aliasActions -> Arrays.stream(aliasActions.indices()) + .map(indexName -> new Tuple<>(indexName, Arrays.asList(aliasActions.aliases()))) + ) + .collect(Collectors.toMap(Tuple::v1, Tuple::v2, (existing, toMerge) -> { + List list = new ArrayList<>(existing.size() + toMerge.size()); + list.addAll(existing); + list.addAll(toMerge); + return list; + })); + authorizationEngine.validateIndexPermissionsAreSubset( + requestInfo, + authorizationInfo, + indexToAliasesMap, + wrapPreservingContext(ActionListener.wrap(authzResult -> { + if (authzResult.isGranted()) { + // do not audit success again + listener.onResponse(null); + } else { + auditTrail.accessDenied( + AuditUtil.extractRequestId(threadContext), + requestInfo.getAuthentication(), + requestInfo.getAction(), + request, + authorizationInfo + ); + listener.onFailure( + Exceptions.authorizationError( + "Adding an alias is not allowed when the alias " + "has more permissions than any of the indices" + ) + ); + } + }, listener::onFailure), threadContext) + ); } else { listener.onResponse(null); } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/interceptor/RequestInterceptor.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/interceptor/RequestInterceptor.java index a9ce040877207..ba36cd2b78bb0 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/interceptor/RequestInterceptor.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/interceptor/RequestInterceptor.java @@ -20,6 +20,10 @@ public interface RequestInterceptor { * This interceptor will introspect the request and potentially modify it. If the interceptor does not apply * to the request then the request will not be modified. */ - void intercept(RequestInfo requestInfo, AuthorizationEngine authorizationEngine, AuthorizationInfo authorizationInfo, - ActionListener listener); + void intercept( + RequestInfo requestInfo, + AuthorizationEngine authorizationEngine, + AuthorizationInfo authorizationInfo, + ActionListener listener + ); } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/interceptor/ResizeRequestInterceptor.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/interceptor/ResizeRequestInterceptor.java index 92cb21c944f53..e52e52a723637 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/interceptor/ResizeRequestInterceptor.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/interceptor/ResizeRequestInterceptor.java @@ -41,39 +41,57 @@ public ResizeRequestInterceptor(ThreadPool threadPool, XPackLicenseState license } @Override - public void intercept(RequestInfo requestInfo, AuthorizationEngine authorizationEngine, AuthorizationInfo authorizationInfo, - ActionListener listener) { + public void intercept( + RequestInfo requestInfo, + AuthorizationEngine authorizationEngine, + AuthorizationInfo authorizationInfo, + ActionListener listener + ) { if (requestInfo.getRequest() instanceof ResizeRequest) { final ResizeRequest request = (ResizeRequest) requestInfo.getRequest(); final AuditTrail auditTrail = auditTrailService.get(); - IndicesAccessControl indicesAccessControl = - threadContext.getTransient(AuthorizationServiceField.INDICES_PERMISSIONS_KEY); - IndicesAccessControl.IndexAccessControl indexAccessControl = - indicesAccessControl.getIndexPermissions(request.getSourceIndex()); + IndicesAccessControl indicesAccessControl = threadContext.getTransient(AuthorizationServiceField.INDICES_PERMISSIONS_KEY); + IndicesAccessControl.IndexAccessControl indexAccessControl = indicesAccessControl.getIndexPermissions(request.getSourceIndex()); if (indexAccessControl != null) { final boolean fls = indexAccessControl.getFieldPermissions().hasFieldLevelSecurity(); final boolean dls = indexAccessControl.getDocumentPermissions().hasDocumentLevelPermissions(); if ((fls || dls) && DOCUMENT_LEVEL_SECURITY_FEATURE.checkWithoutTracking(licenseState)) { - listener.onFailure(new ElasticsearchSecurityException("Resize requests are not allowed for users when " + - "field or document level security is enabled on the source index", RestStatus.BAD_REQUEST)); + listener.onFailure( + new ElasticsearchSecurityException( + "Resize requests are not allowed for users when " + + "field or document level security is enabled on the source index", + RestStatus.BAD_REQUEST + ) + ); return; } } - authorizationEngine.validateIndexPermissionsAreSubset(requestInfo, authorizationInfo, + authorizationEngine.validateIndexPermissionsAreSubset( + requestInfo, + authorizationInfo, Collections.singletonMap(request.getSourceIndex(), Collections.singletonList(request.getTargetIndexRequest().index())), wrapPreservingContext(ActionListener.wrap(authzResult -> { if (authzResult.isGranted()) { listener.onResponse(null); } else { if (authzResult.isAuditable()) { - auditTrail.accessDenied(extractRequestId(threadContext), requestInfo.getAuthentication(), - requestInfo.getAction(), request, authorizationInfo); + auditTrail.accessDenied( + extractRequestId(threadContext), + requestInfo.getAuthentication(), + requestInfo.getAction(), + request, + authorizationInfo + ); } - listener.onFailure(Exceptions.authorizationError("Resizing an index is not allowed when the target index " + - "has more permissions than the source index")); + listener.onFailure( + Exceptions.authorizationError( + "Resizing an index is not allowed when the target index " + "has more permissions than the source index" + ) + ); } - }, listener::onFailure), threadContext)); + }, listener::onFailure), threadContext) + ); } else { listener.onResponse(null); } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/interceptor/SearchRequestInterceptor.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/interceptor/SearchRequestInterceptor.java index b84eace0c1d9d..b7de342ef5caf 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/interceptor/SearchRequestInterceptor.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/interceptor/SearchRequestInterceptor.java @@ -34,9 +34,11 @@ public SearchRequestInterceptor(ThreadPool threadPool, XPackLicenseState license } @Override - void disableFeatures(IndicesRequest indicesRequest, - Map indexAccessControlByIndex, - ActionListener listener) { + void disableFeatures( + IndicesRequest indicesRequest, + Map indexAccessControlByIndex, + ActionListener listener + ) { final SearchRequest request = (SearchRequest) indicesRequest; // The 7.11.2 version check is needed because request caching has a bug related to DLS/FLS // versions before 7.11.2. It is fixed by #69505. See also ESA-2021-08. @@ -49,11 +51,19 @@ void disableFeatures(IndicesRequest indicesRequest, if (indexAccessControlByIndex.values().stream().anyMatch(iac -> iac.getDocumentPermissions().hasDocumentLevelPermissions())) { if (source != null && source.suggest() != null) { - listener.onFailure(new ElasticsearchSecurityException("Suggest isn't supported if document level security is enabled", - RestStatus.BAD_REQUEST)); + listener.onFailure( + new ElasticsearchSecurityException( + "Suggest isn't supported if document level security is enabled", + RestStatus.BAD_REQUEST + ) + ); } else if (source != null && source.profile()) { - listener.onFailure(new ElasticsearchSecurityException("A search request cannot be profiled if document level security " + - "is enabled", RestStatus.BAD_REQUEST)); + listener.onFailure( + new ElasticsearchSecurityException( + "A search request cannot be profiled if document level security " + "is enabled", + RestStatus.BAD_REQUEST + ) + ); } else { listener.onResponse(null); } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/interceptor/ShardSearchRequestInterceptor.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/interceptor/ShardSearchRequestInterceptor.java index b6dc3a21181ac..3eabc01eba5b3 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/interceptor/ShardSearchRequestInterceptor.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/interceptor/ShardSearchRequestInterceptor.java @@ -35,9 +35,11 @@ public ShardSearchRequestInterceptor(ThreadPool threadPool, XPackLicenseState li } @Override - void disableFeatures(IndicesRequest indicesRequest, - Map indexAccessControlByIndex, - ActionListener listener) { + void disableFeatures( + IndicesRequest indicesRequest, + Map indexAccessControlByIndex, + ActionListener listener + ) { final ShardSearchRequest request = (ShardSearchRequest) indicesRequest; // The 7.11.2 version check is needed because request caching has a bug related to DLS/FLS // versions before 7.11.2. It is fixed by #69505. See also ESA-2021-08. @@ -62,8 +64,10 @@ public boolean supports(IndicesRequest request) { return request instanceof ShardSearchRequest; } - boolean dlsUsesStoredScripts(ShardSearchRequest request, - Map indexAccessControlByIndex) { + boolean dlsUsesStoredScripts( + ShardSearchRequest request, + Map indexAccessControlByIndex + ) { final String indexName = request.shardId().getIndexName(); final IndicesAccessControl.IndexAccessControl indexAccessControl = indexAccessControlByIndex.get(indexName); assert indexAccessControl != null : "index access control cannot be null"; diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/interceptor/UpdateRequestInterceptor.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/interceptor/UpdateRequestInterceptor.java index f3b1a76328e35..135b8be209f4d 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/interceptor/UpdateRequestInterceptor.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/interceptor/UpdateRequestInterceptor.java @@ -31,11 +31,17 @@ public UpdateRequestInterceptor(ThreadPool threadPool, XPackLicenseState license } @Override - void disableFeatures(IndicesRequest indicesRequest, - Map indicesAccessControlByIndex, - ActionListener listener) { - listener.onFailure(new ElasticsearchSecurityException("Can't execute an update request if field or document level security " + - "is enabled", RestStatus.BAD_REQUEST)); + void disableFeatures( + IndicesRequest indicesRequest, + Map indicesAccessControlByIndex, + ActionListener listener + ) { + listener.onFailure( + new ElasticsearchSecurityException( + "Can't execute an update request if field or document level security " + "is enabled", + RestStatus.BAD_REQUEST + ) + ); } @Override @@ -43,7 +49,7 @@ String[] requestIndices(IndicesRequest indicesRequest) { if (indicesRequest instanceof UpdateRequest) { UpdateRequest updateRequest = (UpdateRequest) indicesRequest; if (updateRequest.getShardId() != null) { - return new String[]{updateRequest.getShardId().getIndexName()}; + return new String[] { updateRequest.getShardId().getIndexName() }; } } return new String[0]; diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/store/CompositeRolesStore.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/store/CompositeRolesStore.java index 5477fe77e0d9d..a8b4dd4149d25 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/store/CompositeRolesStore.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/store/CompositeRolesStore.java @@ -14,12 +14,10 @@ import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.support.ContextPreservingActionListener; import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.cache.Cache; import org.elasticsearch.common.cache.CacheBuilder; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.hash.MessageDigests; import org.elasticsearch.common.logging.DeprecationCategory; import org.elasticsearch.common.logging.DeprecationLogger; @@ -29,6 +27,8 @@ import org.elasticsearch.common.util.concurrent.ReleasableLock; import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.common.util.set.Sets; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.core.Tuple; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.xpack.core.common.IteratingActionListener; import org.elasticsearch.xpack.core.security.authc.Authentication; @@ -89,10 +89,16 @@ public class CompositeRolesStore { private static final String ROLES_STORE_SOURCE = "roles_stores"; - private static final Setting CACHE_SIZE_SETTING = - Setting.intSetting("xpack.security.authz.store.roles.cache.max_size", 10000, Property.NodeScope); - private static final Setting NEGATIVE_LOOKUP_CACHE_SIZE_SETTING = - Setting.intSetting("xpack.security.authz.store.roles.negative_lookup_cache.max_size", 10000, Property.NodeScope); + private static final Setting CACHE_SIZE_SETTING = Setting.intSetting( + "xpack.security.authz.store.roles.cache.max_size", + 10000, + Property.NodeScope + ); + private static final Setting NEGATIVE_LOOKUP_CACHE_SIZE_SETTING = Setting.intSetting( + "xpack.security.authz.store.roles.negative_lookup_cache.max_size", + 10000, + Property.NodeScope + ); private static final Logger logger = LogManager.getLogger(CompositeRolesStore.class); private final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(CompositeRolesStore.class); @@ -117,11 +123,19 @@ public class CompositeRolesStore { private final Role asyncSearchUserRole; private final Automaton restrictedIndicesAutomaton; - public CompositeRolesStore(Settings settings, RoleProviders roleProviders, NativePrivilegeStore privilegeStore, - ThreadContext threadContext, XPackLicenseState licenseState, FieldPermissionsCache fieldPermissionsCache, - ApiKeyService apiKeyService, ServiceAccountService serviceAccountService, - DocumentSubsetBitsetCache dlsBitsetCache, IndexNameExpressionResolver resolver, - Consumer> effectiveRoleDescriptorsConsumer) { + public CompositeRolesStore( + Settings settings, + RoleProviders roleProviders, + NativePrivilegeStore privilegeStore, + ThreadContext threadContext, + XPackLicenseState licenseState, + FieldPermissionsCache fieldPermissionsCache, + ApiKeyService apiKeyService, + ServiceAccountService serviceAccountService, + DocumentSubsetBitsetCache dlsBitsetCache, + IndexNameExpressionResolver resolver, + Consumer> effectiveRoleDescriptorsConsumer + ) { this.roleProviders = roleProviders; roleProviders.addChangeListener(new RoleProviders.ChangeListener() { @Override @@ -159,11 +173,8 @@ public void providersChanged() { this.anonymousUser = new AnonymousUser(settings); this.isAnonymousEnabled = AnonymousUser.isAnonymousEnabled(settings); this.restrictedIndicesAutomaton = resolver.getSystemNameAutomaton(); - this.superuserRole = Role.builder( - ReservedRolesStore.SUPERUSER_ROLE_DESCRIPTOR, - fieldPermissionsCache, - restrictedIndicesAutomaton - ).build(); + this.superuserRole = Role.builder(ReservedRolesStore.SUPERUSER_ROLE_DESCRIPTOR, fieldPermissionsCache, restrictedIndicesAutomaton) + .build(); xpackUserRole = Role.builder(XPackUser.ROLE_DESCRIPTOR, fieldPermissionsCache, restrictedIndicesAutomaton).build(); asyncSearchUserRole = Role.builder(AsyncSearchUser.ROLE_DESCRIPTOR, fieldPermissionsCache, restrictedIndicesAutomaton).build(); } @@ -175,33 +186,48 @@ public void roles(Set roleNames, ActionListener roleActionListener roleActionListener.onResponse(existing); } else { final long invalidationCounter = numInvalidation.get(); - roleDescriptors(roleNames, ActionListener.wrap( - rolesRetrievalResult -> { - logDeprecatedRoles(rolesRetrievalResult.roleDescriptors); - final boolean missingRoles = rolesRetrievalResult.getMissingRoles().isEmpty() == false; - if (missingRoles) { - logger.debug(() -> new ParameterizedMessage("Could not find roles with names {}", - rolesRetrievalResult.getMissingRoles())); - } - final Set effectiveDescriptors; - Set roleDescriptors = rolesRetrievalResult.getRoleDescriptors(); - if (roleDescriptors.stream().anyMatch(RoleDescriptor::isUsingDocumentOrFieldLevelSecurity) && - DOCUMENT_LEVEL_SECURITY_FEATURE.checkWithoutTracking(licenseState) == false) { - effectiveDescriptors = roleDescriptors.stream() - .filter(not(RoleDescriptor::isUsingDocumentOrFieldLevelSecurity)) - .collect(Collectors.toSet()); - } else { - effectiveDescriptors = roleDescriptors; - } - logger.trace(() -> new ParameterizedMessage("Exposing effective role descriptors [{}] for role names [{}]", - effectiveDescriptors, roleNames)); - effectiveRoleDescriptorsConsumer.accept(Collections.unmodifiableCollection(effectiveDescriptors)); - logger.trace(() -> new ParameterizedMessage("Building role from descriptors [{}] for role names [{}]", - effectiveDescriptors, roleNames)); - buildThenMaybeCacheRole(roleKey, effectiveDescriptors, rolesRetrievalResult.getMissingRoles(), - rolesRetrievalResult.isSuccess(), invalidationCounter, roleActionListener); - }, - roleActionListener::onFailure)); + roleDescriptors(roleNames, ActionListener.wrap(rolesRetrievalResult -> { + logDeprecatedRoles(rolesRetrievalResult.roleDescriptors); + final boolean missingRoles = rolesRetrievalResult.getMissingRoles().isEmpty() == false; + if (missingRoles) { + logger.debug( + () -> new ParameterizedMessage("Could not find roles with names {}", rolesRetrievalResult.getMissingRoles()) + ); + } + final Set effectiveDescriptors; + Set roleDescriptors = rolesRetrievalResult.getRoleDescriptors(); + if (roleDescriptors.stream().anyMatch(RoleDescriptor::isUsingDocumentOrFieldLevelSecurity) + && DOCUMENT_LEVEL_SECURITY_FEATURE.checkWithoutTracking(licenseState) == false) { + effectiveDescriptors = roleDescriptors.stream() + .filter(not(RoleDescriptor::isUsingDocumentOrFieldLevelSecurity)) + .collect(Collectors.toSet()); + } else { + effectiveDescriptors = roleDescriptors; + } + logger.trace( + () -> new ParameterizedMessage( + "Exposing effective role descriptors [{}] for role names [{}]", + effectiveDescriptors, + roleNames + ) + ); + effectiveRoleDescriptorsConsumer.accept(Collections.unmodifiableCollection(effectiveDescriptors)); + logger.trace( + () -> new ParameterizedMessage( + "Building role from descriptors [{}] for role names [{}]", + effectiveDescriptors, + roleNames + ) + ); + buildThenMaybeCacheRole( + roleKey, + effectiveDescriptors, + rolesRetrievalResult.getMissingRoles(), + rolesRetrievalResult.isSuccess(), + invalidationCounter, + roleActionListener + ); + }, roleActionListener::onFailure)); } } @@ -210,9 +236,14 @@ void logDeprecatedRoles(Set roleDescriptors) { .filter(rd -> Boolean.TRUE.equals(rd.getMetadata().get(MetadataUtils.DEPRECATED_METADATA_KEY))) .forEach(rd -> { String reason = Objects.toString( - rd.getMetadata().get(MetadataUtils.DEPRECATED_REASON_METADATA_KEY), "Please check the documentation"); - deprecationLogger.critical(DeprecationCategory.SECURITY, "deprecated_role-" + rd.getName(), "The role [" + rd.getName() + - "] is deprecated and will be removed in a future version of Elasticsearch. " + reason); + rd.getMetadata().get(MetadataUtils.DEPRECATED_REASON_METADATA_KEY), + "Please check the documentation" + ); + deprecationLogger.critical( + DeprecationCategory.SECURITY, + "deprecated_role-" + rd.getName(), + "The role [" + rd.getName() + "] is deprecated and will be removed in a future version of Elasticsearch. " + reason + ); }); } @@ -232,8 +263,9 @@ public void getRoles(User user, Authentication authentication, ActionListener roleActionListener) { if (authentication.getVersion().onOrAfter(VERSION_API_KEY_ROLES_AS_BYTES)) { - buildAndCacheRoleForApiKey(authentication, false, ActionListener.wrap( - role -> { - if (role == Role.EMPTY) { - buildAndCacheRoleForApiKey(authentication, true, roleActionListener); - } else { - buildAndCacheRoleForApiKey(authentication, true, ActionListener.wrap( - limitedByRole -> roleActionListener.onResponse( - LimitedRole.createLimitedRole(role, limitedByRole)), + buildAndCacheRoleForApiKey(authentication, false, ActionListener.wrap(role -> { + if (role == Role.EMPTY) { + buildAndCacheRoleForApiKey(authentication, true, roleActionListener); + } else { + buildAndCacheRoleForApiKey( + authentication, + true, + ActionListener.wrap( + limitedByRole -> roleActionListener.onResponse(LimitedRole.createLimitedRole(role, limitedByRole)), roleActionListener::onFailure - )); - } - }, - roleActionListener::onFailure - )); + ) + ); + } + }, roleActionListener::onFailure)); } else { apiKeyService.getRoleForApiKey(authentication, ActionListener.wrap(apiKeyRoleDescriptors -> { final List descriptors = apiKeyRoleDescriptors.getRoleDescriptors(); if (descriptors == null) { roleActionListener.onFailure(new IllegalStateException("missing role descriptors")); } else if (apiKeyRoleDescriptors.getLimitedByRoleDescriptors() == null) { - buildAndCacheRoleFromDescriptors(descriptors, - apiKeyRoleDescriptors.getApiKeyId() + "_role_desc", roleActionListener); + buildAndCacheRoleFromDescriptors(descriptors, apiKeyRoleDescriptors.getApiKeyId() + "_role_desc", roleActionListener); } else { - buildAndCacheRoleFromDescriptors(descriptors, apiKeyRoleDescriptors.getApiKeyId() + "_role_desc", + buildAndCacheRoleFromDescriptors( + descriptors, + apiKeyRoleDescriptors.getApiKeyId() + "_role_desc", ActionListener.wrap( - role -> buildAndCacheRoleFromDescriptors(apiKeyRoleDescriptors.getLimitedByRoleDescriptors(), - apiKeyRoleDescriptors.getApiKeyId() + "_limited_role_desc", ActionListener.wrap( + role -> buildAndCacheRoleFromDescriptors( + apiKeyRoleDescriptors.getLimitedByRoleDescriptors(), + apiKeyRoleDescriptors.getApiKeyId() + "_limited_role_desc", + ActionListener.wrap( limitedBy -> roleActionListener.onResponse(LimitedRole.createLimitedRole(role, limitedBy)), - roleActionListener::onFailure)), roleActionListener::onFailure)); + roleActionListener::onFailure + ) + ), + roleActionListener::onFailure + ) + ); } }, roleActionListener::onFailure)); } } - public void buildAndCacheRoleFromDescriptors(Collection roleDescriptors, String source, - ActionListener listener) { + public void buildAndCacheRoleFromDescriptors(Collection roleDescriptors, String source, ActionListener listener) { if (ROLES_STORE_SOURCE.equals(source)) { throw new IllegalArgumentException("source [" + ROLES_STORE_SOURCE + "] is reserved for internal use"); } @@ -336,10 +375,20 @@ public void buildAndCacheRoleFromDescriptors(Collection roleDesc } } - private void buildThenMaybeCacheRole(RoleKey roleKey, Collection roleDescriptors, Set missing, - boolean tryCache, long invalidationCounter, ActionListener listener) { + private void buildThenMaybeCacheRole( + RoleKey roleKey, + Collection roleDescriptors, + Set missing, + boolean tryCache, + long invalidationCounter, + ActionListener listener + ) { logger.trace("Building role from descriptors [{}] for names [{}] from source [{}]", roleDescriptors, roleKey.names, roleKey.source); - buildRoleFromDescriptors(roleDescriptors, fieldPermissionsCache, privilegeStore, restrictedIndicesAutomaton, + buildRoleFromDescriptors( + roleDescriptors, + fieldPermissionsCache, + privilegeStore, + restrictedIndicesAutomaton, ActionListener.wrap(role -> { if (role != null && tryCache) { try (ReleasableLock ignored = roleCacheHelper.acquireUpdateLock()) { @@ -366,15 +415,15 @@ private void buildThenMaybeCacheRole(RoleKey roleKey, Collection private void buildAndCacheRoleForApiKey(Authentication authentication, boolean limitedBy, ActionListener roleActionListener) { final Tuple apiKeyIdAndBytes = apiKeyService.getApiKeyIdAndRoleBytes(authentication, limitedBy); - final String roleDescriptorsHash = - MessageDigests.toHexString(MessageDigests.digest(apiKeyIdAndBytes.v2(), MessageDigests.sha256())); + final String roleDescriptorsHash = MessageDigests.toHexString( + MessageDigests.digest(apiKeyIdAndBytes.v2(), MessageDigests.sha256()) + ); final RoleKey roleKey = new RoleKey(Set.of("apikey:" + roleDescriptorsHash), limitedBy ? "apikey_limited_role" : "apikey_role"); final Role existing = roleCache.get(roleKey); if (existing == null) { final long invalidationCounter = numInvalidation.get(); final List roleDescriptors = apiKeyService.parseRoleDescriptors(apiKeyIdAndBytes.v1(), apiKeyIdAndBytes.v2()); - buildThenMaybeCacheRole(roleKey, roleDescriptors, Collections.emptySet(), - true, invalidationCounter, roleActionListener); + buildThenMaybeCacheRole(roleKey, roleDescriptors, Collections.emptySet(), true, invalidationCounter, roleActionListener); } else { roleActionListener.onResponse(existing); } @@ -406,19 +455,22 @@ private void roleDescriptors(Set roleNames, ActionListener roleNames, ActionListener listener) { final RolesRetrievalResult rolesResult = new RolesRetrievalResult(); final List, ActionListener>> asyncRoleProviders = roleProviders.getProviders(); - final ActionListener descriptorsListener = - ContextPreservingActionListener.wrapPreservingContext(ActionListener.wrap(ignore -> { - rolesResult.setMissingRoles(roleNames); - listener.onResponse(rolesResult); - }, listener::onFailure), threadContext); + final ActionListener descriptorsListener = ContextPreservingActionListener.wrapPreservingContext( + ActionListener.wrap(ignore -> { + rolesResult.setMissingRoles(roleNames); + listener.onResponse(rolesResult); + }, listener::onFailure), + threadContext + ); final Predicate iterationPredicate = result -> roleNames.isEmpty() == false; new IteratingActionListener<>(descriptorsListener, (rolesProvider, providerListener) -> { // try to resolve descriptors with role provider rolesProvider.accept(roleNames, ActionListener.wrap(result -> { if (result.isSuccess()) { - logger.debug(() -> new ParameterizedMessage("Roles [{}] were resolved by [{}]", - names(result.getDescriptors()), rolesProvider)); + logger.debug( + () -> new ParameterizedMessage("Roles [{}] were resolved by [{}]", names(result.getDescriptors()), rolesProvider) + ); final Set resolvedDescriptors = result.getDescriptors(); rolesResult.addDescriptors(resolvedDescriptors); // remove resolved descriptors from the set of roles still needed to be resolved @@ -438,9 +490,13 @@ private String names(Collection descriptors) { return descriptors.stream().map(RoleDescriptor::getName).collect(Collectors.joining(",")); } - public static void buildRoleFromDescriptors(Collection roleDescriptors, FieldPermissionsCache fieldPermissionsCache, - NativePrivilegeStore privilegeStore, Automaton restrictedIndicesAutomaton, - ActionListener listener) { + public static void buildRoleFromDescriptors( + Collection roleDescriptors, + FieldPermissionsCache fieldPermissionsCache, + NativePrivilegeStore privilegeStore, + Automaton restrictedIndicesAutomaton, + ActionListener listener + ) { if (roleDescriptors.isEmpty()) { listener.onResponse(Role.EMPTY); return; @@ -484,10 +540,10 @@ public static void buildRoleFromDescriptors(Collection roleDescr final Privilege runAsPrivilege = runAs.isEmpty() ? Privilege.NONE : new Privilege(runAs, runAs.toArray(Strings.EMPTY_ARRAY)); final Role.Builder builder = Role.builder(restrictedIndicesAutomaton, roleNames.toArray(Strings.EMPTY_ARRAY)) - .cluster(clusterPrivileges, configurableClusterPrivileges) - .runAs(runAsPrivilege); - indicesPrivilegesMap.forEach((key, privilege) -> - builder.add( + .cluster(clusterPrivileges, configurableClusterPrivileges) + .runAs(runAsPrivilege); + indicesPrivilegesMap.forEach( + (key, privilege) -> builder.add( fieldPermissionsCache.getFieldPermissions(privilege.fieldPermissionsDefinition), privilege.query, IndexPrivilege.get(privilege.privileges), @@ -495,8 +551,8 @@ public static void buildRoleFromDescriptors(Collection roleDescr privilege.indices.toArray(Strings.EMPTY_ARRAY) ) ); - restrictedIndicesPrivilegesMap.forEach((key, privilege) -> - builder.add( + restrictedIndicesPrivilegesMap.forEach( + (key, privilege) -> builder.add( fieldPermissionsCache.getFieldPermissions(privilege.fieldPermissionsDefinition), privilege.query, IndexPrivilege.get(privilege.privileges), @@ -508,15 +564,16 @@ public static void buildRoleFromDescriptors(Collection roleDescr if (applicationPrivilegesMap.isEmpty()) { listener.onResponse(builder.build()); } else { - final Set applicationNames = applicationPrivilegesMap.keySet().stream() - .map(Tuple::v1) - .collect(Collectors.toSet()); - final Set applicationPrivilegeNames = applicationPrivilegesMap.values().stream() - .flatMap(Collection::stream) - .collect(Collectors.toSet()); + final Set applicationNames = applicationPrivilegesMap.keySet().stream().map(Tuple::v1).collect(Collectors.toSet()); + final Set applicationPrivilegeNames = applicationPrivilegesMap.values() + .stream() + .flatMap(Collection::stream) + .collect(Collectors.toSet()); privilegeStore.getPrivileges(applicationNames, applicationPrivilegeNames, ActionListener.wrap(appPrivileges -> { - applicationPrivilegesMap.forEach((key, names) -> ApplicationPrivilege.get(key.v1(), names, appPrivileges) - .forEach(priv -> builder.addApplicationPrivilege(priv, key.v2()))); + applicationPrivilegesMap.forEach( + (key, names) -> ApplicationPrivilege.get(key.v1(), names, appPrivileges) + .forEach(priv -> builder.addApplicationPrivilege(priv, key.v2())) + ); listener.onResponse(builder.build()); }, listener::onFailure)); } @@ -576,8 +633,13 @@ private static class MergeableIndicesPrivilege { private FieldPermissionsDefinition fieldPermissionsDefinition; private Set query = null; - MergeableIndicesPrivilege(String[] indices, String[] privileges, @Nullable String[] grantedFields, @Nullable String[] deniedFields, - @Nullable BytesReference query) { + MergeableIndicesPrivilege( + String[] indices, + String[] privileges, + @Nullable String[] grantedFields, + @Nullable String[] deniedFields, + @Nullable BytesReference query + ) { this.indices = newHashSet(Objects.requireNonNull(indices)); this.privileges = newHashSet(Objects.requireNonNull(privileges)); this.fieldPermissionsDefinition = new FieldPermissionsDefinition(grantedFields, deniedFields); @@ -601,24 +663,39 @@ void merge(MergeableIndicesPrivilege other) { } } - private static void collatePrivilegesByIndices(IndicesPrivileges[] indicesPrivileges, boolean allowsRestrictedIndices, - Map, MergeableIndicesPrivilege> indicesPrivilegesMap) { + private static void collatePrivilegesByIndices( + IndicesPrivileges[] indicesPrivileges, + boolean allowsRestrictedIndices, + Map, MergeableIndicesPrivilege> indicesPrivilegesMap + ) { for (final IndicesPrivileges indicesPrivilege : indicesPrivileges) { // if a index privilege is an explicit denial, then we treat it as non-existent since we skipped these in the past when // merging final boolean isExplicitDenial = indicesPrivileges.length == 1 - && "none".equalsIgnoreCase(indicesPrivilege.getPrivileges()[0]); + && "none".equalsIgnoreCase(indicesPrivilege.getPrivileges()[0]); if (isExplicitDenial || (indicesPrivilege.allowRestrictedIndices() != allowsRestrictedIndices)) { continue; } final Set key = newHashSet(indicesPrivilege.getIndices()); indicesPrivilegesMap.compute(key, (k, value) -> { if (value == null) { - return new MergeableIndicesPrivilege(indicesPrivilege.getIndices(), indicesPrivilege.getPrivileges(), - indicesPrivilege.getGrantedFields(), indicesPrivilege.getDeniedFields(), indicesPrivilege.getQuery()); + return new MergeableIndicesPrivilege( + indicesPrivilege.getIndices(), + indicesPrivilege.getPrivileges(), + indicesPrivilege.getGrantedFields(), + indicesPrivilege.getDeniedFields(), + indicesPrivilege.getQuery() + ); } else { - value.merge(new MergeableIndicesPrivilege(indicesPrivilege.getIndices(), indicesPrivilege.getPrivileges(), - indicesPrivilege.getGrantedFields(), indicesPrivilege.getDeniedFields(), indicesPrivilege.getQuery())); + value.merge( + new MergeableIndicesPrivilege( + indicesPrivilege.getIndices(), + indicesPrivilege.getPrivileges(), + indicesPrivilege.getGrantedFields(), + indicesPrivilege.getDeniedFields(), + indicesPrivilege.getQuery() + ) + ); return value; } }); @@ -672,8 +749,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; RoleKey roleKey = (RoleKey) o; - return names.equals(roleKey.names) && - source.equals(roleKey.source); + return names.equals(roleKey.names) && source.equals(roleKey.source); } @Override diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/store/DeprecationRoleDescriptorConsumer.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/store/DeprecationRoleDescriptorConsumer.java index 762548694a901..e786c941e8281 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/store/DeprecationRoleDescriptorConsumer.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/store/DeprecationRoleDescriptorConsumer.java @@ -61,9 +61,9 @@ public final class DeprecationRoleDescriptorConsumer implements Consumer> { private static final String ROLE_PERMISSION_DEPRECATION_STANZA = "Role [%s] contains index privileges covering the [%s] alias but" - + " which do not cover some of the indices that it points to [%s]. Granting privileges over an alias and hence granting" - + " privileges over all the indices that the alias points to is deprecated and will be removed in a future version of" - + " Elasticsearch. Instead define permissions exclusively on index names or index name patterns."; + + " which do not cover some of the indices that it points to [%s]. Granting privileges over an alias and hence granting" + + " privileges over all the indices that the alias points to is deprecated and will be removed in a future version of" + + " Elasticsearch. Instead define permissions exclusively on index names or index name patterns."; private static final Logger logger = LogManager.getLogger(DeprecationRoleDescriptorConsumer.class); @@ -169,12 +169,10 @@ private void logDeprecatedPermission(RoleDescriptor roleDescriptor) { final String aliasOrIndexName = aliasOrIndex.getKey(); if (matcher.test(aliasOrIndexName)) { if (aliasOrIndex.getValue().getType() == IndexAbstraction.Type.ALIAS) { - final Set privilegesByAlias = privilegesByAliasMap.computeIfAbsent(aliasOrIndexName, - k -> new HashSet<>()); + final Set privilegesByAlias = privilegesByAliasMap.computeIfAbsent(aliasOrIndexName, k -> new HashSet<>()); privilegesByAlias.addAll(Arrays.asList(indexPrivilege.getPrivileges())); } else { - final Set privilegesByIndex = privilegesByIndexMap.computeIfAbsent(aliasOrIndexName, - k -> new HashSet<>()); + final Set privilegesByIndex = privilegesByIndexMap.computeIfAbsent(aliasOrIndexName, k -> new HashSet<>()); privilegesByIndex.addAll(Arrays.asList(indexPrivilege.getPrivileges())); } } @@ -193,8 +191,10 @@ private void logDeprecatedPermission(RoleDescriptor roleDescriptor) { // null iff the index does not have *any* privilege if (indexPrivileges != null) { // compute automaton once per index no matter how many times it is pointed to - final Automaton indexPrivilegeAutomaton = indexAutomatonMap.computeIfAbsent(index.getName(), - i -> IndexPrivilege.get(indexPrivileges).getAutomaton()); + final Automaton indexPrivilegeAutomaton = indexAutomatonMap.computeIfAbsent( + index.getName(), + i -> IndexPrivilege.get(indexPrivileges).getAutomaton() + ); if (false == Operations.subsetOf(indexPrivilegeAutomaton, aliasPrivilegeAutomaton)) { inferiorIndexNames.add(index.getName()); } @@ -204,8 +204,13 @@ private void logDeprecatedPermission(RoleDescriptor roleDescriptor) { } // log inferior indices for this role, for this alias if (false == inferiorIndexNames.isEmpty()) { - final String logMessage = String.format(Locale.ROOT, ROLE_PERMISSION_DEPRECATION_STANZA, roleDescriptor.getName(), - aliasName, String.join(", ", inferiorIndexNames)); + final String logMessage = String.format( + Locale.ROOT, + ROLE_PERMISSION_DEPRECATION_STANZA, + roleDescriptor.getName(), + aliasName, + String.join(", ", inferiorIndexNames) + ); deprecationLogger.critical(DeprecationCategory.SECURITY, "index_permissions_on_alias", logMessage); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/store/FileRolesStore.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/store/FileRolesStore.java index 11dafdd98dcbb..ac4e25d83e1f1 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/store/FileRolesStore.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/store/FileRolesStore.java @@ -13,18 +13,18 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.action.ActionListener; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.set.Sets; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.yaml.YamlXContent; +import org.elasticsearch.core.Nullable; import org.elasticsearch.env.Environment; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.watcher.FileChangesListener; import org.elasticsearch.watcher.FileWatcher; import org.elasticsearch.watcher.ResourceWatcherService; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.yaml.YamlXContent; import org.elasticsearch.xpack.core.XPackPlugin; import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.xpack.core.security.authz.RoleDescriptor; @@ -70,14 +70,24 @@ public class FileRolesStore implements BiConsumer, ActionListener permissions; - public FileRolesStore(Settings settings, Environment env, ResourceWatcherService watcherService, XPackLicenseState licenseState, - NamedXContentRegistry xContentRegistry) - throws IOException { + public FileRolesStore( + Settings settings, + Environment env, + ResourceWatcherService watcherService, + XPackLicenseState licenseState, + NamedXContentRegistry xContentRegistry + ) throws IOException { this(settings, env, watcherService, null, licenseState, xContentRegistry); } - FileRolesStore(Settings settings, Environment env, ResourceWatcherService watcherService, Consumer> listener, - XPackLicenseState licenseState, NamedXContentRegistry xContentRegistry) throws IOException { + FileRolesStore( + Settings settings, + Environment env, + ResourceWatcherService watcherService, + Consumer> listener, + XPackLicenseState licenseState, + NamedXContentRegistry xContentRegistry + ) throws IOException { this.settings = settings; this.file = resolveFile(env); if (listener != null) { @@ -91,7 +101,6 @@ public FileRolesStore(Settings settings, Environment env, ResourceWatcherService permissions = parseFile(file, logger, settings, licenseState, xContentRegistry); } - @Override public void accept(Set names, ActionListener listener) { listener.onResponse(RoleRetrievalResult.success(roleDescriptors(names))); @@ -161,13 +170,24 @@ public static Set parseFileForRoleNames(Path path, Logger logger) { return parseRoleDescriptors(path, logger, false, Settings.EMPTY, NamedXContentRegistry.EMPTY).keySet(); } - public static Map parseFile(Path path, Logger logger, Settings settings, XPackLicenseState licenseState, - NamedXContentRegistry xContentRegistry) { + public static Map parseFile( + Path path, + Logger logger, + Settings settings, + XPackLicenseState licenseState, + NamedXContentRegistry xContentRegistry + ) { return parseFile(path, logger, true, settings, licenseState, xContentRegistry); } - public static Map parseFile(Path path, Logger logger, boolean resolvePermission, Settings settings, - XPackLicenseState licenseState, NamedXContentRegistry xContentRegistry) { + public static Map parseFile( + Path path, + Logger logger, + boolean resolvePermission, + Settings settings, + XPackLicenseState licenseState, + NamedXContentRegistry xContentRegistry + ) { if (logger == null) { logger = NoOpLogger.INSTANCE; } @@ -183,11 +203,16 @@ public static Map parseFile(Path path, Logger logger, bo RoleDescriptor descriptor = parseRoleDescriptor(segment, path, logger, resolvePermission, settings, xContentRegistry); if (descriptor != null) { if (ReservedRolesStore.isReserved(descriptor.getName())) { - logger.warn("role [{}] is reserved. the relevant role definition in the mapping file will be ignored", - descriptor.getName()); + logger.warn( + "role [{}] is reserved. the relevant role definition in the mapping file will be ignored", + descriptor.getName() + ); } else if (descriptor.isUsingDocumentOrFieldLevelSecurity() && isDlsLicensed == false) { - logger.warn("role [{}] uses document and/or field level security, which is not enabled by the current license" + - ". this role will be ignored", descriptor.getName()); + logger.warn( + "role [{}] uses document and/or field level security, which is not enabled by the current license" + + ". this role will be ignored", + descriptor.getName() + ); // we still put the role in the map to avoid unnecessary negative lookups roles.put(descriptor.getName(), descriptor); } else { @@ -197,10 +222,12 @@ public static Map parseFile(Path path, Logger logger, bo } } catch (IOException ioe) { logger.error( - (Supplier) () -> new ParameterizedMessage( - "failed to read roles file [{}]. skipping all roles...", - path.toAbsolutePath()), - ioe); + (Supplier) () -> new ParameterizedMessage( + "failed to read roles file [{}]. skipping all roles...", + path.toAbsolutePath() + ), + ioe + ); return emptyMap(); } } else { @@ -212,8 +239,13 @@ public static Map parseFile(Path path, Logger logger, bo return unmodifiableMap(roles); } - public static Map parseRoleDescriptors(Path path, Logger logger, boolean resolvePermission, Settings settings, - NamedXContentRegistry xContentRegistry) { + public static Map parseRoleDescriptors( + Path path, + Logger logger, + boolean resolvePermission, + Settings settings, + NamedXContentRegistry xContentRegistry + ) { if (logger == null) { logger = NoOpLogger.INSTANCE; } @@ -231,10 +263,12 @@ public static Map parseRoleDescriptors(Path path, Logger } } catch (IOException ioe) { logger.error( - (Supplier) () -> new ParameterizedMessage( - "failed to read roles file [{}]. skipping all roles...", - path.toAbsolutePath()), - ioe); + (Supplier) () -> new ParameterizedMessage( + "failed to read roles file [{}]. skipping all roles...", + path.toAbsolutePath() + ), + ioe + ); return emptyMap(); } } @@ -242,12 +276,17 @@ public static Map parseRoleDescriptors(Path path, Logger } @Nullable - static RoleDescriptor parseRoleDescriptor(String segment, Path path, Logger logger, boolean resolvePermissions, Settings settings, - NamedXContentRegistry xContentRegistry) { + static RoleDescriptor parseRoleDescriptor( + String segment, + Path path, + Logger logger, + boolean resolvePermissions, + Settings settings, + NamedXContentRegistry xContentRegistry + ) { String roleName = null; try { - XContentParser parser = YamlXContent.yamlXContent - .createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, segment); + XContentParser parser = YamlXContent.yamlXContent.createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, segment); XContentParser.Token token = parser.nextToken(); if (token == XContentParser.Token.START_OBJECT) { token = parser.nextToken(); @@ -255,8 +294,12 @@ static RoleDescriptor parseRoleDescriptor(String segment, Path path, Logger logg roleName = parser.currentName(); Validation.Error validationError = Validation.Roles.validateRoleName(roleName); if (validationError != null) { - logger.error("invalid role definition [{}] in roles file [{}]. invalid role name - {}. skipping role... ", - roleName, path.toAbsolutePath(), validationError); + logger.error( + "invalid role definition [{}] in roles file [{}]. invalid role name - {}. skipping role... ", + roleName, + path.toAbsolutePath(), + validationError + ); return null; } @@ -289,40 +332,55 @@ static RoleDescriptor parseRoleDescriptor(String segment, Path path, Logger logg if (roleName != null) { final String finalRoleName = roleName; logger.error( - (Supplier) () -> new ParameterizedMessage( - "invalid role definition [{}] in roles file [{}]. skipping role...", - finalRoleName, - path), - e); + (Supplier) () -> new ParameterizedMessage( + "invalid role definition [{}] in roles file [{}]. skipping role...", + finalRoleName, + path + ), + e + ); } else { logger.error( - (Supplier) () -> new ParameterizedMessage( - "invalid role definition in roles file [{}]. skipping role...", - path), - e); + (Supplier) () -> new ParameterizedMessage("invalid role definition in roles file [{}]. skipping role...", path), + e + ); } } return null; } @Nullable - private static RoleDescriptor checkDescriptor(RoleDescriptor descriptor, Path path, Logger logger, Settings settings, - NamedXContentRegistry xContentRegistry) { + private static RoleDescriptor checkDescriptor( + RoleDescriptor descriptor, + Path path, + Logger logger, + Settings settings, + NamedXContentRegistry xContentRegistry + ) { String roleName = descriptor.getName(); // first check if FLS/DLS is enabled on the role... if (descriptor.isUsingDocumentOrFieldLevelSecurity()) { if (XPackSettings.DLS_FLS_ENABLED.get(settings) == false) { - logger.error("invalid role definition [{}] in roles file [{}]. document and field level security is not " + - "enabled. set [{}] to [true] in the configuration file. skipping role...", roleName, path - .toAbsolutePath(), XPackSettings.DLS_FLS_ENABLED.getKey()); + logger.error( + "invalid role definition [{}] in roles file [{}]. document and field level security is not " + + "enabled. set [{}] to [true] in the configuration file. skipping role...", + roleName, + path.toAbsolutePath(), + XPackSettings.DLS_FLS_ENABLED.getKey() + ); return null; } else { try { DLSRoleQueryValidator.validateQueryField(descriptor.getIndicesPrivileges(), xContentRegistry); } catch (ElasticsearchException | IllegalArgumentException e) { - logger.error((Supplier) () -> new ParameterizedMessage( - "invalid role definition [{}] in roles file [{}]. failed to validate query field. skipping role...", roleName, - path.toAbsolutePath()), e); + logger.error( + (Supplier) () -> new ParameterizedMessage( + "invalid role definition [{}] in roles file [{}]. failed to validate query field. skipping role...", + roleName, + path.toAbsolutePath() + ), + e + ); return null; } } @@ -373,20 +431,23 @@ public synchronized void onFileChanged(Path file) { permissions = parseFile(file, logger, settings, licenseState, xContentRegistry); } catch (Exception e) { logger.error( - (Supplier) () -> new ParameterizedMessage( - "could not reload roles file [{}]. Current roles remain unmodified", file.toAbsolutePath()), e); + (Supplier) () -> new ParameterizedMessage( + "could not reload roles file [{}]. Current roles remain unmodified", + file.toAbsolutePath() + ), + e + ); return; } final Set changedOrMissingRoles = Sets.difference(previousPermissions.entrySet(), permissions.entrySet()) - .stream() - .map(Map.Entry::getKey) - .collect(Collectors.toSet()); + .stream() + .map(Map.Entry::getKey) + .collect(Collectors.toSet()); final Set addedRoles = Sets.difference(permissions.keySet(), previousPermissions.keySet()); final Set changedRoles = Collections.unmodifiableSet(Sets.union(changedOrMissingRoles, addedRoles)); if (changedRoles.isEmpty() == false) { - logger.info("updated roles (roles file [{}] {})", file.toAbsolutePath(), - Files.exists(file) ? "changed" : "removed"); + logger.info("updated roles (roles file [{}] {})", file.toAbsolutePath(), Files.exists(file) ? "changed" : "removed"); listeners.forEach(c -> c.accept(changedRoles)); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/store/NativePrivilegeStore.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/store/NativePrivilegeStore.java index 6e93709f9b395..2a86cafdf4b46 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/store/NativePrivilegeStore.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/store/NativePrivilegeStore.java @@ -23,24 +23,24 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.cache.Cache; import org.elasticsearch.common.cache.CacheBuilder; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.CollectionUtils; import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParseException; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.core.TimeValue; +import org.elasticsearch.core.Tuple; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.TermQueryBuilder; import org.elasticsearch.index.query.TermsQueryBuilder; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParseException; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.ClientHelper; import org.elasticsearch.xpack.core.security.ScrollHelper; import org.elasticsearch.xpack.core.security.action.privilege.ClearPrivilegesCacheAction; @@ -63,8 +63,8 @@ import java.util.stream.Collector; import java.util.stream.Collectors; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.search.SearchService.DEFAULT_KEEPALIVE_SETTING; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.xpack.core.ClientHelper.SECURITY_ORIGIN; import static org.elasticsearch.xpack.core.ClientHelper.executeAsyncWithOrigin; import static org.elasticsearch.xpack.core.security.authz.privilege.ApplicationPrivilegeDescriptor.DOC_TYPE_VALUE; @@ -77,20 +77,26 @@ */ public class NativePrivilegeStore { + public static final Setting CACHE_MAX_APPLICATIONS_SETTING = Setting.intSetting( + "xpack.security.authz.store.privileges.cache.max_size", + 10_000, + Setting.Property.NodeScope + ); - public static final Setting CACHE_MAX_APPLICATIONS_SETTING = - Setting.intSetting("xpack.security.authz.store.privileges.cache.max_size", - 10_000, Setting.Property.NodeScope); - - public static final Setting CACHE_TTL_SETTING = Setting.timeSetting("xpack.security.authz.store.privileges.cache.ttl", - TimeValue.timeValueHours(24L), Setting.Property.NodeScope); + public static final Setting CACHE_TTL_SETTING = Setting.timeSetting( + "xpack.security.authz.store.privileges.cache.ttl", + TimeValue.timeValueHours(24L), + Setting.Property.NodeScope + ); private static final Collector, ?, Map>> TUPLES_TO_MAP = Collectors.toMap( Tuple::v1, - t -> CollectionUtils.newSingletonArrayList(t.v2()), (a, b) -> { + t -> CollectionUtils.newSingletonArrayList(t.v2()), + (a, b) -> { a.addAll(b); return a; - }); + } + ); private static final Logger logger = LogManager.getLogger(NativePrivilegeStore.class); private final Settings settings; @@ -98,48 +104,62 @@ public class NativePrivilegeStore { private final SecurityIndexManager securityIndexManager; private final DescriptorsAndApplicationNamesCache descriptorsAndApplicationNamesCache; - public NativePrivilegeStore( - Settings settings, Client client, SecurityIndexManager securityIndexManager, CacheInvalidatorRegistry cacheInvalidatorRegistry) { + Settings settings, + Client client, + SecurityIndexManager securityIndexManager, + CacheInvalidatorRegistry cacheInvalidatorRegistry + ) { this.settings = settings; this.client = client; this.securityIndexManager = securityIndexManager; final TimeValue ttl = CACHE_TTL_SETTING.get(settings); if (ttl.getNanos() > 0) { descriptorsAndApplicationNamesCache = new DescriptorsAndApplicationNamesCache( - ttl, CACHE_MAX_APPLICATIONS_SETTING.get(settings)); + ttl, + CACHE_MAX_APPLICATIONS_SETTING.get(settings) + ); cacheInvalidatorRegistry.registerCacheInvalidator("application_privileges", descriptorsAndApplicationNamesCache); } else { descriptorsAndApplicationNamesCache = null; } } - public void getPrivileges(Collection applications, Collection names, - ActionListener> listener) { + public void getPrivileges( + Collection applications, + Collection names, + ActionListener> listener + ) { - final Set applicationNamesCacheKey = (isEmpty(applications) || applications.contains("*")) ? - Set.of("*") : Set.copyOf(applications); + final Set applicationNamesCacheKey = (isEmpty(applications) || applications.contains("*")) + ? Set.of("*") + : Set.copyOf(applications); // Always fetch for the concrete application names even when the passed-in application names has no wildcard. // This serves as a negative lookup, i.e. when a passed-in non-wildcard application does not exist. - Set concreteApplicationNames = descriptorsAndApplicationNamesCache == null ? null + Set concreteApplicationNames = descriptorsAndApplicationNamesCache == null + ? null : descriptorsAndApplicationNamesCache.getConcreteApplicationNames(applicationNamesCacheKey); if (concreteApplicationNames != null && concreteApplicationNames.isEmpty()) { - logger.debug("returning empty application privileges for [{}] as application names result in empty list", - applicationNamesCacheKey); + logger.debug( + "returning empty application privileges for [{}] as application names result in empty list", + applicationNamesCacheKey + ); listener.onResponse(Collections.emptySet()); } else { final Set cachedDescriptors = cachedDescriptorsForApplicationNames( - concreteApplicationNames != null ? concreteApplicationNames : applicationNamesCacheKey); + concreteApplicationNames != null ? concreteApplicationNames : applicationNamesCacheKey + ); if (cachedDescriptors != null) { logger.debug("All application privileges for [{}] found in cache", applicationNamesCacheKey); listener.onResponse(filterDescriptorsForPrivilegeNames(cachedDescriptors, names)); } else { // Always fetch all privileges of an application for caching purpose logger.debug("Fetching application privilege documents for: {}", applicationNamesCacheKey); - final long invalidationCount = - descriptorsAndApplicationNamesCache == null ? -1 : descriptorsAndApplicationNamesCache.getInvalidationCount(); + final long invalidationCount = descriptorsAndApplicationNamesCache == null + ? -1 + : descriptorsAndApplicationNamesCache.getInvalidationCount(); innerGetPrivileges(applicationNamesCacheKey, ActionListener.wrap(fetchedDescriptors -> { final Map> mapOfFetchedDescriptors = fetchedDescriptors.stream() .collect(Collectors.groupingBy(ApplicationPrivilegeDescriptor::getApplication, Collectors.toUnmodifiableSet())); @@ -163,10 +183,11 @@ private void innerGetPrivileges(Collection applications, ActionListener< } else { securityIndexManager.checkIndexVersionThenExecute(listener::onFailure, () -> { - final TermQueryBuilder typeQuery = QueryBuilders - .termQuery(ApplicationPrivilegeDescriptor.Fields.TYPE.getPreferredName(), DOC_TYPE_VALUE); - final QueryBuilder query = QueryBuilders.boolQuery().filter(typeQuery) - .filter(getApplicationNameQuery(applications)); + final TermQueryBuilder typeQuery = QueryBuilders.termQuery( + ApplicationPrivilegeDescriptor.Fields.TYPE.getPreferredName(), + DOC_TYPE_VALUE + ); + final QueryBuilder query = QueryBuilders.boolQuery().filter(typeQuery).filter(getApplicationNameQuery(applications)); final Supplier supplier = client.threadPool().getThreadContext().newRestorableContext(false); try (ThreadContext.StoredContext ignore = client.threadPool().getThreadContext().stashWithOrigin(SECURITY_ORIGIN)) { @@ -176,12 +197,20 @@ private void innerGetPrivileges(Collection applications, ActionListener< .setSize(1000) .setFetchSource(true) .request(); - logger.trace(() -> - new ParameterizedMessage("Searching for [{}] privileges with query [{}]", - applications, Strings.toString(query))); + logger.trace( + () -> new ParameterizedMessage( + "Searching for [{}] privileges with query [{}]", + applications, + Strings.toString(query) + ) + ); request.indicesOptions().ignoreUnavailable(); - ScrollHelper.fetchAllByEntity(client, request, new ContextPreservingActionListener<>(supplier, listener), - hit -> buildPrivilege(hit.getId(), hit.getSourceRef())); + ScrollHelper.fetchAllByEntity( + client, + request, + new ContextPreservingActionListener<>(supplier, listener), + hit -> buildPrivilege(hit.getId(), hit.getSourceRef()) + ); } }); } @@ -228,9 +257,11 @@ private ApplicationPrivilegeDescriptor buildPrivilege(String docId, BytesReferen try { // EMPTY is safe here because we never use namedObject - try (StreamInput input = source.streamInput(); - XContentParser parser = XContentType.JSON.xContent().createParser(NamedXContentRegistry.EMPTY, - LoggingDeprecationHandler.INSTANCE, input)) { + try ( + StreamInput input = source.streamInput(); + XContentParser parser = XContentType.JSON.xContent() + .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, input) + ) { final ApplicationPrivilegeDescriptor privilege = ApplicationPrivilegeDescriptor.parse(parser, null, null, true); assert privilege.getApplication().equals(name.v1()) : "Incorrect application name for privilege. Expected [" + name.v1() + "] but was " + privilege.getApplication(); @@ -256,12 +287,13 @@ private Set cachedDescriptorsForApplicationNames return null; } final Set cachedDescriptors = new HashSet<>(); - for (String applicationName: applicationNames) { + for (String applicationName : applicationNames) { if (applicationName.endsWith("*")) { return null; } else { - final Set descriptors = - descriptorsAndApplicationNamesCache.getApplicationDescriptors(applicationName); + final Set descriptors = descriptorsAndApplicationNamesCache.getApplicationDescriptors( + applicationName + ); if (descriptors == null) { return null; } else { @@ -276,7 +308,9 @@ private Set cachedDescriptorsForApplicationNames * Filter to get all privilege descriptors that have any of the given privilege names. */ private Collection filterDescriptorsForPrivilegeNames( - Collection descriptors, Collection privilegeNames) { + Collection descriptors, + Collection privilegeNames + ) { // empty set of names equals to retrieve everything if (isEmpty(privilegeNames)) { return descriptors; @@ -285,15 +319,19 @@ private Collection filterDescriptorsForPrivilege } // protected for tests - protected void cacheFetchedDescriptors(Set applicationNamesCacheKey, - Map> mapOfFetchedDescriptors, - long invalidationCount) { - descriptorsAndApplicationNamesCache.putIfNoInvalidationSince(applicationNamesCacheKey, mapOfFetchedDescriptors, - invalidationCount); + protected void cacheFetchedDescriptors( + Set applicationNamesCacheKey, + Map> mapOfFetchedDescriptors, + long invalidationCount + ) { + descriptorsAndApplicationNamesCache.putIfNoInvalidationSince(applicationNamesCacheKey, mapOfFetchedDescriptors, invalidationCount); } - public void putPrivileges(Collection privileges, WriteRequest.RefreshPolicy refreshPolicy, - ActionListener>> listener) { + public void putPrivileges( + Collection privileges, + WriteRequest.RefreshPolicy refreshPolicy, + ActionListener>> listener + ) { securityIndexManager.prepareIndexIfNeededThenExecute(listener::onFailure, () -> { ActionListener groupListener = new GroupedActionListener<>( ActionListener.wrap((Collection responses) -> { @@ -302,34 +340,51 @@ public void putPrivileges(Collection privileges, .map(r -> r.getId()) .map(NativePrivilegeStore::nameFromDocId) .collect(TUPLES_TO_MAP); - clearCaches(listener, + clearCaches( + listener, privileges.stream().map(ApplicationPrivilegeDescriptor::getApplication).collect(Collectors.toUnmodifiableSet()), - createdNames); - }, listener::onFailure), privileges.size()); + createdNames + ); + }, listener::onFailure), + privileges.size() + ); for (ApplicationPrivilegeDescriptor privilege : privileges) { innerPutPrivilege(privilege, refreshPolicy, groupListener); } }); } - private void innerPutPrivilege(ApplicationPrivilegeDescriptor privilege, WriteRequest.RefreshPolicy refreshPolicy, - ActionListener listener) { + private void innerPutPrivilege( + ApplicationPrivilegeDescriptor privilege, + WriteRequest.RefreshPolicy refreshPolicy, + ActionListener listener + ) { try { final String name = privilege.getName(); final XContentBuilder xContentBuilder = privilege.toXContent(jsonBuilder(), true); - ClientHelper.executeAsyncWithOrigin(client.threadPool().getThreadContext(), SECURITY_ORIGIN, - client.prepareIndex(SECURITY_MAIN_ALIAS).setId(toDocId(privilege.getApplication(), name)) + ClientHelper.executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + SECURITY_ORIGIN, + client.prepareIndex(SECURITY_MAIN_ALIAS) + .setId(toDocId(privilege.getApplication(), name)) .setSource(xContentBuilder) .setRefreshPolicy(refreshPolicy) - .request(), listener, client::index); + .request(), + listener, + client::index + ); } catch (Exception e) { logger.warn("Failed to put privilege {} - {}", Strings.toString(privilege), e.toString()); listener.onFailure(e); } } - public void deletePrivileges(String application, Collection names, WriteRequest.RefreshPolicy refreshPolicy, - ActionListener>> listener) { + public void deletePrivileges( + String application, + Collection names, + WriteRequest.RefreshPolicy refreshPolicy, + ActionListener>> listener + ) { final SecurityIndexManager frozenSecurityIndex = securityIndexManager.freeze(); if (frozenSecurityIndex.indexExists() == false) { listener.onResponse(Collections.emptyMap()); @@ -337,20 +392,22 @@ public void deletePrivileges(String application, Collection names, Write listener.onFailure(frozenSecurityIndex.getUnavailableReason()); } else { securityIndexManager.checkIndexVersionThenExecute(listener::onFailure, () -> { - ActionListener groupListener = new GroupedActionListener<>( - ActionListener.wrap(responses -> { - final Map> deletedNames = responses.stream() - .filter(r -> r.getResult() == DocWriteResponse.Result.DELETED) - .map(r -> r.getId()) - .map(NativePrivilegeStore::nameFromDocId) - .collect(TUPLES_TO_MAP); - clearCaches(listener, Collections.singleton(application), deletedNames); - }, listener::onFailure), names.size()); + ActionListener groupListener = new GroupedActionListener<>(ActionListener.wrap(responses -> { + final Map> deletedNames = responses.stream() + .filter(r -> r.getResult() == DocWriteResponse.Result.DELETED) + .map(r -> r.getId()) + .map(NativePrivilegeStore::nameFromDocId) + .collect(TUPLES_TO_MAP); + clearCaches(listener, Collections.singleton(application), deletedNames); + }, listener::onFailure), names.size()); for (String name : names) { - ClientHelper.executeAsyncWithOrigin(client.threadPool().getThreadContext(), SECURITY_ORIGIN, - client.prepareDelete(SECURITY_MAIN_ALIAS, toDocId(application, name)) - .setRefreshPolicy(refreshPolicy) - .request(), groupListener, client::delete); + ClientHelper.executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + SECURITY_ORIGIN, + client.prepareDelete(SECURITY_MAIN_ALIAS, toDocId(application, name)).setRefreshPolicy(refreshPolicy).request(), + groupListener, + client::delete + ); } }); } @@ -358,23 +415,26 @@ public void deletePrivileges(String application, Collection names, Write private void clearCaches(ActionListener listener, Set applicationNames, T value) { // This currently clears _all_ roles, but could be improved to clear only those roles that reference the affected application - final ClearPrivilegesCacheRequest request = new ClearPrivilegesCacheRequest() - .applicationNames(applicationNames.toArray(String[]::new)).clearRolesCache(true); - executeAsyncWithOrigin(client, SECURITY_ORIGIN, ClearPrivilegesCacheAction.INSTANCE, request, - new ActionListener<>() { - @Override - public void onResponse(ClearPrivilegesCacheResponse nodes) { - listener.onResponse(value); - } + final ClearPrivilegesCacheRequest request = new ClearPrivilegesCacheRequest().applicationNames( + applicationNames.toArray(String[]::new) + ).clearRolesCache(true); + executeAsyncWithOrigin(client, SECURITY_ORIGIN, ClearPrivilegesCacheAction.INSTANCE, request, new ActionListener<>() { + @Override + public void onResponse(ClearPrivilegesCacheResponse nodes) { + listener.onResponse(value); + } - @Override - public void onFailure(Exception e) { - logger.error("unable to clear application privileges and role cache", e); - listener.onFailure( - new ElasticsearchException("clearing the application privileges and role cache failed. " + - "please clear the caches manually", e)); - } - }); + @Override + public void onFailure(Exception e) { + logger.error("unable to clear application privileges and role cache", e); + listener.onFailure( + new ElasticsearchException( + "clearing the application privileges and role cache failed. " + "please clear the caches manually", + e + ) + ); + } + }); } private static Tuple nameFromDocId(String docId) { @@ -440,9 +500,11 @@ public Set getConcreteApplicationNames(Set applicationNames) { return applicationNamesCache.get(applicationNames); } - public void putIfNoInvalidationSince(Set applicationNamesCacheKey, - Map> mapOfFetchedDescriptors, - long invalidationCount) { + public void putIfNoInvalidationSince( + Set applicationNamesCacheKey, + Map> mapOfFetchedDescriptors, + long invalidationCount + ) { lockingAtomicCounter.compareAndRun(invalidationCount, () -> { final Set fetchedApplicationNames = Collections.unmodifiableSet(mapOfFetchedDescriptors.keySet()); // Do not cache the names if expansion has no effect diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/store/NativeRolesStore.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/store/NativeRolesStore.java index de4ee1dd401df..565fa6a79832c 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/store/NativeRolesStore.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/store/NativeRolesStore.java @@ -25,17 +25,17 @@ import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.support.ContextPreservingActionListener; import org.elasticsearch.client.Client; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.core.Nullable; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.license.LicenseUtils; import org.elasticsearch.license.XPackLicenseState; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.security.ScrollHelper; import org.elasticsearch.xpack.core.security.action.role.ClearRolesCacheAction; import org.elasticsearch.xpack.core.security.action.role.ClearRolesCacheRequest; @@ -60,9 +60,9 @@ import java.util.function.BiConsumer; import java.util.function.Supplier; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.index.query.QueryBuilders.existsQuery; import static org.elasticsearch.search.SearchService.DEFAULT_KEEPALIVE_SETTING; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.xpack.core.ClientHelper.SECURITY_ORIGIN; import static org.elasticsearch.xpack.core.ClientHelper.executeAsyncWithOrigin; import static org.elasticsearch.xpack.core.security.SecurityField.DOCUMENT_LEVEL_SECURITY_FEATURE; @@ -121,10 +121,18 @@ public void getRoleDescriptors(Set names, final ActionListener(supplier, - ActionListener.wrap(roles -> listener.onResponse(RoleRetrievalResult.success(new HashSet<>(roles))), - e -> listener.onResponse(RoleRetrievalResult.failure(e)))), - (hit) -> transformRole(hit.getId(), hit.getSourceRef(), logger, licenseState)); + ScrollHelper.fetchAllByEntity( + client, + request, + new ContextPreservingActionListener<>( + supplier, + ActionListener.wrap( + roles -> listener.onResponse(RoleRetrievalResult.success(new HashSet<>(roles))), + e -> listener.onResponse(RoleRetrievalResult.failure(e)) + ) + ), + (hit) -> transformRole(hit.getId(), hit.getSourceRef(), logger, licenseState) + ); } }); } else if (names.size() == 1) { @@ -133,29 +141,33 @@ public void getRoleDescriptors(Set names, final ActionListener { final String[] roleIds = names.stream().map(NativeRolesStore::getIdForRole).toArray(String[]::new); MultiGetRequest multiGetRequest = client.prepareMultiGet().addIds(SECURITY_MAIN_ALIAS, roleIds).request(); - executeAsyncWithOrigin(client.threadPool().getThreadContext(), SECURITY_ORIGIN, multiGetRequest, + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + SECURITY_ORIGIN, + multiGetRequest, ActionListener.wrap(mGetResponse -> { - final MultiGetItemResponse[] responses = mGetResponse.getResponses(); - Set descriptors = new HashSet<>(); - for (int i = 0; i < responses.length; i++) { - MultiGetItemResponse item = responses[i]; - if (item.isFailed()) { - final Exception failure = item.getFailure().getFailure(); - for (int j = i + 1; j < responses.length; j++) { - item = responses[j]; - if (item.isFailed()) { - failure.addSuppressed(failure); - } + final MultiGetItemResponse[] responses = mGetResponse.getResponses(); + Set descriptors = new HashSet<>(); + for (int i = 0; i < responses.length; i++) { + MultiGetItemResponse item = responses[i]; + if (item.isFailed()) { + final Exception failure = item.getFailure().getFailure(); + for (int j = i + 1; j < responses.length; j++) { + item = responses[j]; + if (item.isFailed()) { + failure.addSuppressed(failure); } - listener.onResponse(RoleRetrievalResult.failure(failure)); - return; - } else if (item.getResponse().isExists()) { - descriptors.add(transformRole(item.getResponse())); } + listener.onResponse(RoleRetrievalResult.failure(failure)); + return; + } else if (item.getResponse().isExists()) { + descriptors.add(transformRole(item.getResponse())); } - listener.onResponse(RoleRetrievalResult.success(descriptors)); - }, - e -> listener.onResponse(RoleRetrievalResult.failure(e))), client::multiGet); + } + listener.onResponse(RoleRetrievalResult.success(descriptors)); + }, e -> listener.onResponse(RoleRetrievalResult.failure(e))), + client::multiGet + ); }); } } @@ -168,15 +180,20 @@ public void deleteRole(final DeleteRoleRequest deleteRoleRequest, final ActionLi listener.onFailure(frozenSecurityIndex.getUnavailableReason()); } else { securityIndex.checkIndexVersionThenExecute(listener::onFailure, () -> { - DeleteRequest request = client - .prepareDelete(SECURITY_MAIN_ALIAS, getIdForRole(deleteRoleRequest.name())).request(); + DeleteRequest request = client.prepareDelete(SECURITY_MAIN_ALIAS, getIdForRole(deleteRoleRequest.name())).request(); request.setRefreshPolicy(deleteRoleRequest.getRefreshPolicy()); - executeAsyncWithOrigin(client.threadPool().getThreadContext(), SECURITY_ORIGIN, request, + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + SECURITY_ORIGIN, + request, new ActionListener() { @Override public void onResponse(DeleteResponse deleteResponse) { - clearRoleCache(deleteRoleRequest.name(), listener, - deleteResponse.getResult() == DocWriteResponse.Result.DELETED); + clearRoleCache( + deleteRoleRequest.name(), + listener, + deleteResponse.getResult() == DocWriteResponse.Result.DELETED + ); } @Override @@ -184,7 +201,9 @@ public void onFailure(Exception e) { logger.error("failed to delete role from the index", e); listener.onFailure(e); } - }, client::delete); + }, + client::delete + ); }); } } @@ -207,26 +226,31 @@ void innerPutRole(final PutRoleRequest request, final RoleDescriptor role, final listener.onFailure(e); return; } - final IndexRequest indexRequest = client.prepareIndex(SECURITY_MAIN_ALIAS).setId(getIdForRole(role.getName())) - .setSource(xContentBuilder) - .setRefreshPolicy(request.getRefreshPolicy()) - .request(); - executeAsyncWithOrigin(client.threadPool().getThreadContext(), SECURITY_ORIGIN, - indexRequest, - new ActionListener() { - @Override - public void onResponse(IndexResponse indexResponse) { - final boolean created = indexResponse.getResult() == DocWriteResponse.Result.CREATED; - logger.trace("Created role: [{}]", indexRequest); - clearRoleCache(role.getName(), listener, created); - } + final IndexRequest indexRequest = client.prepareIndex(SECURITY_MAIN_ALIAS) + .setId(getIdForRole(role.getName())) + .setSource(xContentBuilder) + .setRefreshPolicy(request.getRefreshPolicy()) + .request(); + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + SECURITY_ORIGIN, + indexRequest, + new ActionListener() { + @Override + public void onResponse(IndexResponse indexResponse) { + final boolean created = indexResponse.getResult() == DocWriteResponse.Result.CREATED; + logger.trace("Created role: [{}]", indexRequest); + clearRoleCache(role.getName(), listener, created); + } - @Override - public void onFailure(Exception e) { - logger.error(new ParameterizedMessage("failed to put role [{}]", request.name()), e); - listener.onFailure(e); - } - }, client::index); + @Override + public void onFailure(Exception e) { + logger.error(new ParameterizedMessage("failed to put role [{}]", request.name()), e); + listener.onFailure(e); + } + }, + client::index + ); }); } @@ -238,31 +262,46 @@ public void usageStats(ActionListener> listener) { usageStats.put("dls", false); listener.onResponse(usageStats); } else { - securityIndex.checkIndexVersionThenExecute(listener::onFailure, () -> - executeAsyncWithOrigin(client.threadPool().getThreadContext(), SECURITY_ORIGIN, + securityIndex.checkIndexVersionThenExecute( + listener::onFailure, + () -> executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + SECURITY_ORIGIN, client.prepareMultiSearch() - .add(client.prepareSearch(SECURITY_MAIN_ALIAS) - .setQuery(QueryBuilders.termQuery(RoleDescriptor.Fields.TYPE.getPreferredName(), ROLE_TYPE)) - .setTrackTotalHits(true) - .setSize(0)) - .add(client.prepareSearch(SECURITY_MAIN_ALIAS) - .setQuery(QueryBuilders.boolQuery() - .must(QueryBuilders.termQuery(RoleDescriptor.Fields.TYPE.getPreferredName(), ROLE_TYPE)) - .must(QueryBuilders.boolQuery() - .should(existsQuery("indices.field_security.grant")) - .should(existsQuery("indices.field_security.except")) - // for backwardscompat with 2.x - .should(existsQuery("indices.fields")))) - .setTrackTotalHits(true) - .setSize(0) - .setTerminateAfter(1)) - .add(client.prepareSearch(SECURITY_MAIN_ALIAS) - .setQuery(QueryBuilders.boolQuery() - .must(QueryBuilders.termQuery(RoleDescriptor.Fields.TYPE.getPreferredName(), ROLE_TYPE)) - .filter(existsQuery("indices.query"))) - .setTrackTotalHits(true) - .setSize(0) - .setTerminateAfter(1)) + .add( + client.prepareSearch(SECURITY_MAIN_ALIAS) + .setQuery(QueryBuilders.termQuery(RoleDescriptor.Fields.TYPE.getPreferredName(), ROLE_TYPE)) + .setTrackTotalHits(true) + .setSize(0) + ) + .add( + client.prepareSearch(SECURITY_MAIN_ALIAS) + .setQuery( + QueryBuilders.boolQuery() + .must(QueryBuilders.termQuery(RoleDescriptor.Fields.TYPE.getPreferredName(), ROLE_TYPE)) + .must( + QueryBuilders.boolQuery() + .should(existsQuery("indices.field_security.grant")) + .should(existsQuery("indices.field_security.except")) + // for backwardscompat with 2.x + .should(existsQuery("indices.fields")) + ) + ) + .setTrackTotalHits(true) + .setSize(0) + .setTerminateAfter(1) + ) + .add( + client.prepareSearch(SECURITY_MAIN_ALIAS) + .setQuery( + QueryBuilders.boolQuery() + .must(QueryBuilders.termQuery(RoleDescriptor.Fields.TYPE.getPreferredName(), ROLE_TYPE)) + .filter(existsQuery("indices.query")) + ) + .setTrackTotalHits(true) + .setSize(0) + .setTerminateAfter(1) + ) .request(), new ActionListener.Delegating>(listener) { @Override @@ -286,7 +325,10 @@ public void onResponse(MultiSearchResponse items) { } delegate.onResponse(usageStats); } - }, client::multiSearch)); + }, + client::multiSearch + ) + ); } } @@ -303,48 +345,57 @@ private void getRoleDescriptor(final String roleId, ActionListener resultListener.onResponse(RoleRetrievalResult.failure(e)), - () -> executeGetRoleRequest(roleId, new ActionListener() { - @Override - public void onResponse(GetResponse response) { - final RoleDescriptor descriptor = transformRole(response); - resultListener.onResponse(RoleRetrievalResult - .success(descriptor == null ? Collections.emptySet() : Collections.singleton(descriptor))); - } + securityIndex.checkIndexVersionThenExecute( + e -> resultListener.onResponse(RoleRetrievalResult.failure(e)), + () -> executeGetRoleRequest(roleId, new ActionListener() { + @Override + public void onResponse(GetResponse response) { + final RoleDescriptor descriptor = transformRole(response); + resultListener.onResponse( + RoleRetrievalResult.success(descriptor == null ? Collections.emptySet() : Collections.singleton(descriptor)) + ); + } - @Override - public void onFailure(Exception e) { - resultListener.onResponse(RoleRetrievalResult.failure(e)); - } - })); + @Override + public void onFailure(Exception e) { + resultListener.onResponse(RoleRetrievalResult.failure(e)); + } + }) + ); } } private void executeGetRoleRequest(String role, ActionListener listener) { - securityIndex.checkIndexVersionThenExecute(listener::onFailure, () -> - executeAsyncWithOrigin(client.threadPool().getThreadContext(), SECURITY_ORIGIN, - client.prepareGet(SECURITY_MAIN_ALIAS, getIdForRole(role)).request(), - listener, - client::get)); + securityIndex.checkIndexVersionThenExecute( + listener::onFailure, + () -> executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + SECURITY_ORIGIN, + client.prepareGet(SECURITY_MAIN_ALIAS, getIdForRole(role)).request(), + listener, + client::get + ) + ); } private void clearRoleCache(final String role, ActionListener listener, Response response) { ClearRolesCacheRequest request = new ClearRolesCacheRequest().names(role); - executeAsyncWithOrigin(client, SECURITY_ORIGIN, ClearRolesCacheAction.INSTANCE, request, - new ActionListener<>() { - @Override - public void onResponse(ClearRolesCacheResponse nodes) { - listener.onResponse(response); - } + executeAsyncWithOrigin(client, SECURITY_ORIGIN, ClearRolesCacheAction.INSTANCE, request, new ActionListener<>() { + @Override + public void onResponse(ClearRolesCacheResponse nodes) { + listener.onResponse(response); + } - @Override - public void onFailure(Exception e) { - logger.error(new ParameterizedMessage("unable to clear cache for role [{}]", role), e); - ElasticsearchException exception = new ElasticsearchException("clearing the cache for [" + role - + "] failed. please clear the role cache manually", e); - listener.onFailure(exception); - } - }); + @Override + public void onFailure(Exception e) { + logger.error(new ParameterizedMessage("unable to clear cache for role [{}]", role), e); + ElasticsearchException exception = new ElasticsearchException( + "clearing the cache for [" + role + "] failed. please clear the role cache manually", + e + ); + listener.onFailure(exception); + } + }); } @Nullable @@ -364,10 +415,10 @@ static RoleDescriptor transformRole(String id, BytesReference sourceBytes, Logge // we pass true as last parameter because we do not want to reject permissions if the field permissions // are given in 2.x syntax RoleDescriptor roleDescriptor = RoleDescriptor.parse(name, sourceBytes, true, XContentType.JSON); - final boolean dlsEnabled = - Arrays.stream(roleDescriptor.getIndicesPrivileges()).anyMatch(IndicesPrivileges::isUsingDocumentLevelSecurity); - final boolean flsEnabled = - Arrays.stream(roleDescriptor.getIndicesPrivileges()).anyMatch(IndicesPrivileges::isUsingFieldLevelSecurity); + final boolean dlsEnabled = Arrays.stream(roleDescriptor.getIndicesPrivileges()) + .anyMatch(IndicesPrivileges::isUsingDocumentLevelSecurity); + final boolean flsEnabled = Arrays.stream(roleDescriptor.getIndicesPrivileges()) + .anyMatch(IndicesPrivileges::isUsingFieldLevelSecurity); if ((dlsEnabled || flsEnabled) && DOCUMENT_LEVEL_SECURITY_FEATURE.checkWithoutTracking(licenseState) == false) { List unlicensedFeatures = new ArrayList<>(2); if (flsEnabled) { @@ -379,8 +430,14 @@ static RoleDescriptor transformRole(String id, BytesReference sourceBytes, Logge Map transientMap = new HashMap<>(2); transientMap.put("unlicensed_features", unlicensedFeatures); transientMap.put("enabled", false); - return new RoleDescriptor(roleDescriptor.getName(), roleDescriptor.getClusterPrivileges(), - roleDescriptor.getIndicesPrivileges(), roleDescriptor.getRunAs(), roleDescriptor.getMetadata(), transientMap); + return new RoleDescriptor( + roleDescriptor.getName(), + roleDescriptor.getClusterPrivileges(), + roleDescriptor.getIndicesPrivileges(), + roleDescriptor.getRunAs(), + roleDescriptor.getMetadata(), + transientMap + ); } else { return roleDescriptor; } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/store/RoleProviders.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/store/RoleProviders.java index 8e0252cb5c4fc..3a9ff20991e2d 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/store/RoleProviders.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/store/RoleProviders.java @@ -118,6 +118,7 @@ public void usageStats(ActionListener> listener) { interface ChangeListener { void rolesChanged(Set roles); + void providersChanged(); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/crypto/tool/SystemKeyTool.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/crypto/tool/SystemKeyTool.java index 10059112e297b..098157b2d26c1 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/crypto/tool/SystemKeyTool.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/crypto/tool/SystemKeyTool.java @@ -8,19 +8,18 @@ import joptsimple.OptionSet; import joptsimple.OptionSpec; + import org.elasticsearch.ElasticsearchException; import org.elasticsearch.cli.EnvironmentAwareCommand; import org.elasticsearch.cli.ExitCodes; import org.elasticsearch.cli.Terminal; import org.elasticsearch.cli.UserException; -import org.elasticsearch.core.SuppressForbidden; -import org.elasticsearch.core.PathUtils; import org.elasticsearch.common.util.set.Sets; +import org.elasticsearch.core.PathUtils; +import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.env.Environment; import org.elasticsearch.xpack.core.XPackPlugin; -import javax.crypto.KeyGenerator; -import javax.crypto.SecretKey; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardOpenOption; @@ -31,6 +30,9 @@ import java.util.Locale; import java.util.Set; +import javax.crypto.KeyGenerator; +import javax.crypto.SecretKey; + public class SystemKeyTool extends EnvironmentAwareCommand { static final String KEY_ALGO = "HmacSHA512"; @@ -43,8 +45,10 @@ public class SystemKeyTool extends EnvironmentAwareCommand { arguments = parser.nonOptions("key path"); } - public static final Set PERMISSION_OWNER_READ_WRITE = Sets.newHashSet(PosixFilePermission.OWNER_READ, - PosixFilePermission.OWNER_WRITE); + public static final Set PERMISSION_OWNER_READ_WRITE = Sets.newHashSet( + PosixFilePermission.OWNER_READ, + PosixFilePermission.OWNER_WRITE + ); public static void main(String[] args) throws Exception { final SystemKeyTool tool = new SystemKeyTool(); @@ -82,8 +86,10 @@ protected void execute(Terminal terminal, OptionSet options, Environment env) th PosixFileAttributeView view = Files.getFileAttributeView(keyPath, PosixFileAttributeView.class); if (view != null) { view.setPermissions(PERMISSION_OWNER_READ_WRITE); - terminal.println("Ensure the generated key can be read by the user that Elasticsearch runs as, " - + "permissions are set to owner read/write only"); + terminal.println( + "Ensure the generated key can be read by the user that Elasticsearch runs as, " + + "permissions are set to owner read/write only" + ); } } @@ -101,7 +107,6 @@ static SecretKey generateSecretKey(int keyLength) { } } - @SuppressForbidden(reason = "Parsing command line path") private static Path parsePath(String path) { return PathUtils.get(path); diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/enrollment/BaseEnrollmentTokenGenerator.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/enrollment/BaseEnrollmentTokenGenerator.java index e53b7a3d6a5ca..ba5350e50ea37 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/enrollment/BaseEnrollmentTokenGenerator.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/enrollment/BaseEnrollmentTokenGenerator.java @@ -27,25 +27,30 @@ public class BaseEnrollmentTokenGenerator { public static final long ENROLL_API_KEY_EXPIRATION_MINUTES = 30L; - public BaseEnrollmentTokenGenerator() { - } + public BaseEnrollmentTokenGenerator() {} static String getHttpsCaFingerprint(SSLService sslService) throws Exception { final SslKeyConfig keyConfig = sslService.getHttpTransportSSLConfiguration().getKeyConfig(); if (keyConfig instanceof StoreKeyConfig == false) { - throw new IllegalStateException("Unable to create an enrollment token. Elasticsearch node HTTP layer SSL configuration is " + - "not configured with a keystore"); + throw new IllegalStateException( + "Unable to create an enrollment token. Elasticsearch node HTTP layer SSL configuration is " + + "not configured with a keystore" + ); } - final List> httpCaKeysAndCertificates = - ((StoreKeyConfig) keyConfig).getKeys().stream() - .filter(t -> t.v2().getBasicConstraints() != -1) - .collect(Collectors.toList()); + final List> httpCaKeysAndCertificates = ((StoreKeyConfig) keyConfig).getKeys() + .stream() + .filter(t -> t.v2().getBasicConstraints() != -1) + .collect(Collectors.toList()); if (httpCaKeysAndCertificates.isEmpty()) { - throw new IllegalStateException("Unable to create an enrollment token. Elasticsearch node HTTP layer SSL configuration " + - "Keystore doesn't contain any PrivateKey entries where the associated certificate is a CA certificate"); + throw new IllegalStateException( + "Unable to create an enrollment token. Elasticsearch node HTTP layer SSL configuration " + + "Keystore doesn't contain any PrivateKey entries where the associated certificate is a CA certificate" + ); } else if (httpCaKeysAndCertificates.size() > 1) { - throw new IllegalStateException("Unable to create an enrollment token. Elasticsearch node HTTP layer SSL configuration " + - "Keystore contains multiple PrivateKey entries where the associated certificate is a CA certificate"); + throw new IllegalStateException( + "Unable to create an enrollment token. Elasticsearch node HTTP layer SSL configuration " + + "Keystore contains multiple PrivateKey entries where the associated certificate is a CA certificate" + ); } return SslUtil.calculateFingerprint(httpCaKeysAndCertificates.get(0).v2(), "SHA-256"); } @@ -53,7 +58,7 @@ static String getHttpsCaFingerprint(SSLService sslService) throws Exception { static Tuple, List> splitAddresses(List addresses) throws Exception { final List nonLocalAddresses = new ArrayList<>(); final List localAddresses = new ArrayList<>(); - for (String boundAddress : addresses){ + for (String boundAddress : addresses) { InetAddress inetAddress = getInetAddressFromString(boundAddress); if (inetAddress.isLoopbackAddress()) { localAddresses.add(boundAddress); diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/enrollment/ExternalEnrollmentTokenGenerator.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/enrollment/ExternalEnrollmentTokenGenerator.java index 2513c2ef2aa7b..b3751db53bfe4 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/enrollment/ExternalEnrollmentTokenGenerator.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/enrollment/ExternalEnrollmentTokenGenerator.java @@ -14,17 +14,17 @@ import org.elasticsearch.common.UUIDs; import org.elasticsearch.common.io.Streams; import org.elasticsearch.common.settings.SecureString; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.core.Tuple; import org.elasticsearch.env.Environment; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.XPackSettings; +import org.elasticsearch.xpack.core.security.CommandLineHttpClient; import org.elasticsearch.xpack.core.security.EnrollmentToken; +import org.elasticsearch.xpack.core.security.HttpResponse; import org.elasticsearch.xpack.core.security.action.enrollment.KibanaEnrollmentAction; import org.elasticsearch.xpack.core.security.action.enrollment.NodeEnrollmentAction; import org.elasticsearch.xpack.core.ssl.SSLService; -import org.elasticsearch.xpack.core.security.CommandLineHttpClient; -import org.elasticsearch.xpack.core.security.HttpResponse; import java.io.IOException; import java.io.InputStream; @@ -130,15 +130,21 @@ protected String getApiKeyCredentials(String user, SecureString password, String }; final URL createApiKeyUrl = createAPIKeyUrl(); - final HttpResponse httpResponseApiKey = client.execute("POST", createApiKeyUrl, user, password, - createApiKeyRequestBodySupplier, is -> responseBuilder(is)); + final HttpResponse httpResponseApiKey = client.execute( + "POST", + createApiKeyUrl, + user, + password, + createApiKeyRequestBodySupplier, + is -> responseBuilder(is) + ); final int httpCode = httpResponseApiKey.getHttpStatus(); if (httpCode != HttpURLConnection.HTTP_OK) { - logger.error("Error " + httpCode + "when calling GET " + createApiKeyUrl + ". ResponseBody: " + - httpResponseApiKey.getResponseBody()); - throw new IllegalStateException("Unexpected response code [" + httpCode + "] from calling POST " - + createApiKeyUrl); + logger.error( + "Error " + httpCode + "when calling GET " + createApiKeyUrl + ". ResponseBody: " + httpResponseApiKey.getResponseBody() + ); + throw new IllegalStateException("Unexpected response code [" + httpCode + "] from calling POST " + createApiKeyUrl); } final String apiKey = Objects.toString(httpResponseApiKey.getResponseBody().get("api_key"), ""); @@ -155,15 +161,18 @@ protected Tuple, String> getNodeInfo(String user, SecureString pass final int httpCode = httpResponseHttp.getHttpStatus(); if (httpCode != HttpURLConnection.HTTP_OK) { - logger.error("Error " + httpCode + "when calling GET " + httpInfoUrl + ". ResponseBody: " + - httpResponseHttp.getResponseBody()); + logger.error("Error " + httpCode + "when calling GET " + httpInfoUrl + ". ResponseBody: " + httpResponseHttp.getResponseBody()); throw new IllegalStateException("Unexpected response code [" + httpCode + "] from calling GET " + httpInfoUrl); } final List addresses = getBoundAddresses(httpResponseHttp.getResponseBody()); if (addresses == null || addresses.isEmpty()) { - logger.error("No bound addresses found in response from calling GET " + httpInfoUrl + ". ResponseBody: " + - httpResponseHttp.getResponseBody()); + logger.error( + "No bound addresses found in response from calling GET " + + httpInfoUrl + + ". ResponseBody: " + + httpResponseHttp.getResponseBody() + ); throw new IllegalStateException("No bound addresses found in response from calling GET " + httpInfoUrl); } final List filteredAddresses = getFilteredAddresses(addresses); diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/enrollment/InternalEnrollmentTokenGenerator.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/enrollment/InternalEnrollmentTokenGenerator.java index 62e5464a12f4f..b43900bbf8b96 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/enrollment/InternalEnrollmentTokenGenerator.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/enrollment/InternalEnrollmentTokenGenerator.java @@ -62,17 +62,18 @@ public InternalEnrollmentTokenGenerator(Environment environment, SSLService sslS */ public void maybeCreateNodeEnrollmentToken(Consumer consumer) { // the enrollment token can only be used against the node that generated it - final NodesInfoRequest nodesInfoRequest = - new NodesInfoRequest().nodesIds("_local").addMetrics(NodesInfoRequest.Metric.HTTP.metricName(), - NodesInfoRequest.Metric.TRANSPORT.metricName()); + final NodesInfoRequest nodesInfoRequest = new NodesInfoRequest().nodesIds("_local") + .addMetrics(NodesInfoRequest.Metric.HTTP.metricName(), NodesInfoRequest.Metric.TRANSPORT.metricName()); client.execute(NodesInfoAction.INSTANCE, nodesInfoRequest, ActionListener.wrap(response -> { assert response.getNodes().size() == 1; NodeInfo nodeInfo = response.getNodes().get(0); TransportInfo transportInfo = nodeInfo.getInfo(TransportInfo.class); boolean noNonLocalTransportAddresses = splitAddresses(getAllTransportAddresses(transportInfo)).v2().isEmpty(); if (noNonLocalTransportAddresses) { - LOGGER.info("Will not generate node enrollment token because node is only bound on localhost for transport " + - "and cannot connect to nodes from other hosts"); + LOGGER.info( + "Will not generate node enrollment token because node is only bound on localhost for transport " + + "and cannot connect to nodes from other hosts" + ); // empty enrollment token when skip consumer.accept(""); return; @@ -80,8 +81,10 @@ public void maybeCreateNodeEnrollmentToken(Consumer consumer) { HttpInfo httpInfo = nodeInfo.getInfo(HttpInfo.class); boolean noNonLocalHttpAddresses = splitAddresses(getAllHttpAddresses(httpInfo)).v2().isEmpty(); if (noNonLocalHttpAddresses) { - LOGGER.info("Will not generate node enrollment token because node is only bound on localhost for HTTPS " + - "and cannot enroll nodes from other hosts"); + LOGGER.info( + "Will not generate node enrollment token because node is only bound on localhost for HTTPS " + + "and cannot enroll nodes from other hosts" + ); // empty enrollment token when skip consumer.accept(""); return; @@ -130,8 +133,14 @@ public void createKibanaEnrollmentToken(Consumer consumer) { private void assembleToken(EnrollmentTokenType enrollTokenType, HttpInfo httpInfo, Consumer consumer) { if (false == ENROLLMENT_ENABLED.get(environment.settings())) { - LOGGER.error("Cannot create enrollment token [" + enrollTokenType + "] because enrollment is disabled " + - "with setting [" + ENROLLMENT_ENABLED.getKey() + "] set to [false]"); + LOGGER.error( + "Cannot create enrollment token [" + + enrollTokenType + + "] because enrollment is disabled " + + "with setting [" + + ENROLLMENT_ENABLED.getKey() + + "] set to [false]" + ); consumer.accept(null); return; } @@ -139,8 +148,10 @@ private void assembleToken(EnrollmentTokenType enrollTokenType, HttpInfo httpInf try { fingerprint = getHttpsCaFingerprint(); } catch (Exception e) { - LOGGER.error("Failed to create enrollment token when computing HTTPS CA fingerprint, possibly the certs are not auto-generated", - e); + LOGGER.error( + "Failed to create enrollment token when computing HTTPS CA fingerprint, possibly the certs are not auto-generated", + e + ); consumer.accept(null); return; } @@ -160,12 +171,7 @@ private void assembleToken(EnrollmentTokenType enrollTokenType, HttpInfo httpInf ); client.execute(CreateApiKeyAction.INSTANCE, apiKeyRequest, ActionListener.wrap(createApiKeyResponse -> { final String apiKey = createApiKeyResponse.getId() + ":" + createApiKeyResponse.getKey().toString(); - final EnrollmentToken enrollmentToken = new EnrollmentToken( - apiKey, - fingerprint, - Version.CURRENT.toString(), - tokenAddresses - ); + final EnrollmentToken enrollmentToken = new EnrollmentToken(apiKey, fingerprint, Version.CURRENT.toString(), tokenAddresses); consumer.accept(enrollmentToken); }, e -> { LOGGER.error("Failed to create enrollment token when generating API key", e); @@ -186,16 +192,13 @@ private static List getAllHttpAddresses(HttpInfo httpInfo) { private static List getAllTransportAddresses(TransportInfo transportInfo) { final List transportAddressesList = new ArrayList<>(); collectAllAddresses(transportInfo.getAddress(), transportAddressesList); - transportInfo.getProfileAddresses().values().forEach(profileAddress -> - collectAllAddresses(profileAddress, transportAddressesList)); + transportInfo.getProfileAddresses().values().forEach(profileAddress -> collectAllAddresses(profileAddress, transportAddressesList)); return transportAddressesList; } private static void collectAllAddresses(BoundTransportAddress address, List collectedAddressesList) { collectedAddressesList.add(address.publishAddress().toString()); - Arrays.stream(address.boundAddresses()) - .map(TransportAddress::toString) - .forEach(collectedAddressesList::add); + Arrays.stream(address.boundAddresses()).map(TransportAddress::toString).forEach(collectedAddressesList::add); } private enum EnrollmentTokenType { diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/enrollment/tool/CreateEnrollmentTokenTool.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/enrollment/tool/CreateEnrollmentTokenTool.java index 2314ca4009924..84c6ccf4964ea 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/enrollment/tool/CreateEnrollmentTokenTool.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/enrollment/tool/CreateEnrollmentTokenTool.java @@ -18,9 +18,9 @@ import org.elasticsearch.core.CheckedFunction; import org.elasticsearch.env.Environment; import org.elasticsearch.xpack.core.XPackSettings; +import org.elasticsearch.xpack.core.security.CommandLineHttpClient; import org.elasticsearch.xpack.security.enrollment.ExternalEnrollmentTokenGenerator; import org.elasticsearch.xpack.security.tool.BaseRunAsSuperuserCommand; -import org.elasticsearch.xpack.core.security.CommandLineHttpClient; import java.util.List; import java.util.function.Function; diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/ingest/SetSecurityUserProcessor.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/ingest/SetSecurityUserProcessor.java index 4cb5d154df777..c44f35ab57921 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/ingest/SetSecurityUserProcessor.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/ingest/SetSecurityUserProcessor.java @@ -46,14 +46,25 @@ public final class SetSecurityUserProcessor extends AbstractProcessor { private final String field; private final Set properties; - public SetSecurityUserProcessor(String tag, String description, SecurityContext securityContext, Settings settings, - String field, Set properties) { + public SetSecurityUserProcessor( + String tag, + String description, + SecurityContext securityContext, + Settings settings, + String field, + Set properties + ) { super(tag, description); this.securityContext = securityContext; this.settings = Objects.requireNonNull(settings, "settings object cannot be null"); if (XPackSettings.SECURITY_ENABLED.get(settings) == false) { - logger.warn("Creating processor [{}] (tag [{}]) on field [{}] but authentication is not currently enabled on this cluster " + - " - this processor is likely to fail at runtime if it is used", TYPE, tag, field); + logger.warn( + "Creating processor [{}] (tag [{}]) on field [{}] but authentication is not currently enabled on this cluster " + + " - this processor is likely to fail at runtime if it is used", + TYPE, + tag, + field + ); } else if (this.securityContext == null) { throw new IllegalArgumentException("Authentication is allowed on this cluster state, but there is no security context"); } @@ -74,15 +85,24 @@ public IngestDocument execute(IngestDocument ingestDocument) throws Exception { if (user == null) { logger.debug( - "Failed to find active user. SecurityContext=[{}] Authentication=[{}] User=[{}]", securityContext, authentication, user); + "Failed to find active user. SecurityContext=[{}] Authentication=[{}] User=[{}]", + securityContext, + authentication, + user + ); if (XPackSettings.SECURITY_ENABLED.get(settings)) { // This shouldn't happen. If authentication is allowed (and active), then there _should_ always be an authenticated user. // If we ever see this error message, then one of our assumptions are wrong. - throw new IllegalStateException("There is no authenticated user - the [" + TYPE - + "] processor requires an authenticated user"); + throw new IllegalStateException( + "There is no authenticated user - the [" + TYPE + "] processor requires an authenticated user" + ); } else { - throw new IllegalStateException("Security (authentication) is not enabled on this cluster, so there is no active user - " + - "the [" + TYPE + "] processor cannot be used without security"); + throw new IllegalStateException( + "Security (authentication) is not enabled on this cluster, so there is no active user - " + + "the [" + + TYPE + + "] processor cannot be used without security" + ); } } @@ -123,8 +143,9 @@ public IngestDocument execute(IngestDocument ingestDocument) throws Exception { final String apiKey = "api_key"; final Object existingApiKeyField = userObject.get(apiKey); @SuppressWarnings("unchecked") - final Map apiKeyField = - existingApiKeyField instanceof Map ? (Map) existingApiKeyField : new HashMap<>(); + final Map apiKeyField = existingApiKeyField instanceof Map + ? (Map) existingApiKeyField + : new HashMap<>(); Object apiKeyName = authentication.getMetadata().get(ApiKeyService.API_KEY_NAME_KEY); if (apiKeyName != null) { apiKeyField.put("name", apiKeyName); @@ -133,7 +154,7 @@ public IngestDocument execute(IngestDocument ingestDocument) throws Exception { if (apiKeyId != null) { apiKeyField.put("id", apiKeyId); } - final Map apiKeyMetadata = ApiKeyService.getApiKeyMetadata(authentication); + final Map apiKeyMetadata = ApiKeyService.getApiKeyMetadata(authentication); if (false == apiKeyMetadata.isEmpty()) { apiKeyField.put("metadata", apiKeyMetadata); } @@ -146,8 +167,9 @@ public IngestDocument execute(IngestDocument ingestDocument) throws Exception { final String realmKey = "realm"; final Object existingRealmField = userObject.get(realmKey); @SuppressWarnings("unchecked") - final Map realmField = - existingRealmField instanceof Map ? (Map) existingRealmField : new HashMap<>(); + final Map realmField = existingRealmField instanceof Map + ? (Map) existingRealmField + : new HashMap<>(); final Object realmName = ApiKeyService.getCreatorRealmName(authentication); if (realmName != null) { @@ -198,8 +220,12 @@ public Factory(Supplier securityContext, Settings settings) { } @Override - public SetSecurityUserProcessor create(Map processorFactories, String tag, - String description, Map config) throws Exception { + public SetSecurityUserProcessor create( + Map processorFactories, + String tag, + String description, + Map config + ) throws Exception { String field = readStringProperty(TYPE, tag, config, "field"); List propertyNames = readOptionalList(TYPE, tag, config, "properties"); Set properties; diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/operator/FileOperatorUsersStore.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/operator/FileOperatorUsersStore.java index f8cf69bf2b500..03b4949e4f0c7 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/operator/FileOperatorUsersStore.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/operator/FileOperatorUsersStore.java @@ -12,19 +12,19 @@ import org.apache.logging.log4j.message.ParameterizedMessage; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchParseException; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.ValidationException; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.env.Environment; +import org.elasticsearch.watcher.FileChangesListener; +import org.elasticsearch.watcher.FileWatcher; +import org.elasticsearch.watcher.ResourceWatcherService; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.env.Environment; -import org.elasticsearch.watcher.FileChangesListener; -import org.elasticsearch.watcher.FileWatcher; -import org.elasticsearch.watcher.ResourceWatcherService; import org.elasticsearch.xpack.core.XPackPlugin; import org.elasticsearch.xpack.core.security.authc.Authentication; import org.elasticsearch.xpack.core.security.authc.esnative.NativeRealmSettings; @@ -52,7 +52,7 @@ public class FileOperatorUsersStore { private volatile OperatorUsersDescriptor operatorUsersDescriptor; public FileOperatorUsersStore(Environment env, ResourceWatcherService watcherService) { - this.file = XPackPlugin.resolveConfigFile(env, "operator_users.yml"); + this.file = XPackPlugin.resolveConfigFile(env, "operator_users.yml"); this.operatorUsersDescriptor = parseFile(this.file, logger); FileWatcher watcher = new FileWatcher(file.getParent(), true); watcher.addListener(new FileOperatorUsersStore.FileListener()); @@ -75,8 +75,7 @@ public boolean isOperatorUser(Authentication authentication) { && group.authenticationType == authentication.getAuthenticationType() && realm.getType().equals(group.realmType) && (group.realmName == null || group.realmName.equals(realm.getName())); - logger.trace("Matching user [{}] against operator rule [{}] is [{}]", - authentication.getUser(), group, match); + logger.trace("Matching user [{}] against operator rule [{}] is [{}]", authentication.getUser(), group, match); return match; }); } @@ -100,10 +99,8 @@ List getGroups() { @Override public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; OperatorUsersDescriptor that = (OperatorUsersDescriptor) o; return groups.equals(that.groups); } @@ -123,7 +120,10 @@ public String toString() { static final class Group { private static final Set SINGLETON_REALM_TYPES = Set.of( - FileRealmSettings.TYPE, NativeRealmSettings.TYPE, ReservedRealm.TYPE); + FileRealmSettings.TYPE, + NativeRealmSettings.TYPE, + ReservedRealm.TYPE + ); private final Set usernames; private final String realmName; @@ -138,13 +138,13 @@ static final class Group { this(usernames, realmName, null, null); } - Group(Set usernames, @Nullable String realmName, @Nullable String realmType, - @Nullable String authenticationType) { + Group(Set usernames, @Nullable String realmName, @Nullable String realmType, @Nullable String authenticationType) { this.usernames = usernames; this.realmName = realmName; this.realmType = realmType == null ? FileRealmSettings.TYPE : realmType; - this.authenticationType = authenticationType == null ? Authentication.AuthenticationType.REALM : - Authentication.AuthenticationType.valueOf(authenticationType.toUpperCase(Locale.ROOT)); + this.authenticationType = authenticationType == null + ? Authentication.AuthenticationType.REALM + : Authentication.AuthenticationType.valueOf(authenticationType.toUpperCase(Locale.ROOT)); validate(); } @@ -160,7 +160,9 @@ private void validate() { if (false == SINGLETON_REALM_TYPES.contains(realmType)) { validationException.addValidationError( "[realm_name] must be specified for realm types other than [" - + Strings.collectionToCommaDelimitedString(SINGLETON_REALM_TYPES) + "]"); + + Strings.collectionToCommaDelimitedString(SINGLETON_REALM_TYPES) + + "]" + ); } } if (false == validationException.validationErrors().isEmpty()) { @@ -187,15 +189,13 @@ public String toString() { @Override public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; Group group = (Group) o; return usernames.equals(group.usernames) - && Objects.equals(realmName, group.realmName) - && realmType.equals(group.realmType) - && authenticationType == group.authenticationType; + && Objects.equals(realmName, group.realmName) + && realmType.equals(group.realmType) + && authenticationType == group.authenticationType; } @Override @@ -206,17 +206,22 @@ public int hashCode() { public static OperatorUsersDescriptor parseFile(Path file, Logger logger) { if (false == Files.exists(file)) { - logger.warn("Operator privileges [{}] is enabled, but operator user file does not exist. " + - "No user will be able to perform operator-only actions.", OPERATOR_PRIVILEGES_ENABLED.getKey()); + logger.warn( + "Operator privileges [{}] is enabled, but operator user file does not exist. " + + "No user will be able to perform operator-only actions.", + OPERATOR_PRIVILEGES_ENABLED.getKey() + ); return EMPTY_OPERATOR_USERS_DESCRIPTOR; } else { logger.debug("Reading operator users file [{}]", file.toAbsolutePath()); try (InputStream in = Files.newInputStream(file, StandardOpenOption.READ)) { final OperatorUsersDescriptor operatorUsersDescriptor = parseConfig(in); - logger.info("parsed [{}] group(s) with a total of [{}] operator user(s) from file [{}]", + logger.info( + "parsed [{}] group(s) with a total of [{}] operator user(s) from file [{}]", operatorUsersDescriptor.groups.size(), operatorUsersDescriptor.groups.stream().mapToLong(g -> g.usernames.size()).sum(), - file.toAbsolutePath()); + file.toAbsolutePath() + ); logger.debug("operator user descriptor: [{}]", operatorUsersDescriptor); return operatorUsersDescriptor; } catch (IOException | RuntimeException e) { @@ -235,18 +240,15 @@ static OperatorUsersDescriptor parseConfig(InputStream in) throws IOException { @SuppressWarnings("unchecked") private static final ConstructingObjectParser GROUP_PARSER = new ConstructingObjectParser<>( - "operator_privileges.operator.group", false, - (Object[] arr) -> new Group( - Set.copyOf((List)arr[0]), - (String) arr[1], - (String) arr[2], - (String) arr[3] - ) + "operator_privileges.operator.group", + false, + (Object[] arr) -> new Group(Set.copyOf((List) arr[0]), (String) arr[1], (String) arr[2], (String) arr[3]) ); @SuppressWarnings("unchecked") private static final ConstructingObjectParser OPERATOR_USER_PARSER = new ConstructingObjectParser<>( - "operator_privileges.operator", false, + "operator_privileges.operator", + false, (Object[] arr) -> new OperatorUsersDescriptor((List) arr[0]) ); diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/operator/OperatorOnlyRegistry.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/operator/OperatorOnlyRegistry.java index ff79733f2ff55..5d8e0dc829e7b 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/operator/OperatorOnlyRegistry.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/operator/OperatorOnlyRegistry.java @@ -25,7 +25,8 @@ public class OperatorOnlyRegistry { - public static final Set SIMPLE_ACTIONS = Set.of(AddVotingConfigExclusionsAction.NAME, + public static final Set SIMPLE_ACTIONS = Set.of( + AddVotingConfigExclusionsAction.NAME, ClearVotingConfigExclusionsAction.NAME, PutLicenseAction.NAME, DeleteLicenseAction.NAME, @@ -40,7 +41,7 @@ public class OperatorOnlyRegistry { "cluster:admin/shutdown/create", "cluster:admin/shutdown/get", "cluster:admin/shutdown/delete" - ); + ); private final ClusterSettings clusterSettings; @@ -66,14 +67,17 @@ public OperatorPrivilegesViolation check(String action, TransportRequest request private OperatorPrivilegesViolation checkClusterUpdateSettings(ClusterUpdateSettingsRequest request) { List operatorOnlySettingKeys = Stream.concat( - request.transientSettings().keySet().stream(), request.persistentSettings().keySet().stream() + request.transientSettings().keySet().stream(), + request.persistentSettings().keySet().stream() ).filter(k -> { final Setting setting = clusterSettings.get(k); return setting != null && setting.isOperatorOnly(); }).collect(Collectors.toList()); if (false == operatorOnlySettingKeys.isEmpty()) { return () -> (operatorOnlySettingKeys.size() == 1 ? "setting" : "settings") - + " [" + Strings.collectionToDelimitedString(operatorOnlySettingKeys, ",") + "]"; + + " [" + + Strings.collectionToDelimitedString(operatorOnlySettingKeys, ",") + + "]"; } else { return null; } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/operator/OperatorPrivileges.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/operator/OperatorPrivileges.java index c8a5606720beb..5fac15eb72cb9 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/operator/OperatorPrivileges.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/operator/OperatorPrivileges.java @@ -24,8 +24,11 @@ public class OperatorPrivileges { private static final Logger logger = LogManager.getLogger(OperatorPrivileges.class); - public static final Setting OPERATOR_PRIVILEGES_ENABLED = - Setting.boolSetting("xpack.security.operator_privileges.enabled", false, Setting.Property.NodeScope); + public static final Setting OPERATOR_PRIVILEGES_ENABLED = Setting.boolSetting( + "xpack.security.operator_privileges.enabled", + false, + Setting.Property.NodeScope + ); public interface OperatorPrivilegesService { /** @@ -39,7 +42,11 @@ public interface OperatorPrivilegesService { * @return An exception if user is an non-operator and the request is operator-only. Otherwise returns null. */ ElasticsearchSecurityException check( - Authentication authentication, String action, TransportRequest request, ThreadContext threadContext); + Authentication authentication, + String action, + TransportRequest request, + ThreadContext threadContext + ); /** * When operator privileges are enabled, certain requests needs to be configured in a specific way @@ -59,7 +66,8 @@ public static final class DefaultOperatorPrivilegesService implements OperatorPr public DefaultOperatorPrivilegesService( XPackLicenseState licenseState, FileOperatorUsersStore fileOperatorUsersStore, - OperatorOnlyRegistry operatorOnlyRegistry) { + OperatorOnlyRegistry operatorOnlyRegistry + ) { this.fileOperatorUsersStore = fileOperatorUsersStore; this.operatorOnlyRegistry = operatorOnlyRegistry; this.licenseState = licenseState; @@ -87,7 +95,10 @@ public void maybeMarkOperatorUser(Authentication authentication, ThreadContext t } public ElasticsearchSecurityException check( - Authentication authentication, String action, TransportRequest request, ThreadContext threadContext + Authentication authentication, + String action, + TransportRequest request, + ThreadContext threadContext ) { if (false == shouldProcess()) { return null; @@ -98,7 +109,8 @@ public ElasticsearchSecurityException check( return null; } if (false == AuthenticationField.PRIVILEGE_CATEGORY_VALUE_OPERATOR.equals( - threadContext.getHeader(AuthenticationField.PRIVILEGE_CATEGORY_KEY))) { + threadContext.getHeader(AuthenticationField.PRIVILEGE_CATEGORY_KEY) + )) { // Only check whether request is operator-only when user is NOT an operator logger.trace("Checking operator-only violation for user [{}] and action [{}]", user, action); final OperatorOnlyRegistry.OperatorPrivilegesViolation violation = operatorOnlyRegistry.check(action, request); @@ -123,12 +135,14 @@ private boolean shouldProcess() { public static final OperatorPrivilegesService NOOP_OPERATOR_PRIVILEGES_SERVICE = new OperatorPrivilegesService() { @Override - public void maybeMarkOperatorUser(Authentication authentication, ThreadContext threadContext) { - } + public void maybeMarkOperatorUser(Authentication authentication, ThreadContext threadContext) {} @Override public ElasticsearchSecurityException check( - Authentication authentication, String action, TransportRequest request, ThreadContext threadContext + Authentication authentication, + String action, + TransportRequest request, + ThreadContext threadContext ) { return null; } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/SecurityRestFilter.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/SecurityRestFilter.java index 253c72369be91..22e53ecfd2511 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/SecurityRestFilter.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/SecurityRestFilter.java @@ -16,8 +16,6 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.Maps; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.MediaType; -import org.elasticsearch.xcontent.MediaTypeRegistry; import org.elasticsearch.http.HttpChannel; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestChannel; @@ -26,6 +24,8 @@ import org.elasticsearch.rest.RestRequest.Method; import org.elasticsearch.rest.RestRequestFilter; import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.xcontent.MediaType; +import org.elasticsearch.xcontent.MediaTypeRegistry; import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.xpack.security.authc.AuthenticationService; import org.elasticsearch.xpack.security.authc.support.SecondaryAuthenticator; @@ -52,13 +52,25 @@ public enum ActionType { RequestHandling("Request handling"); private final String name; - ActionType(String name) { this.name = name; } + + ActionType(String name) { + this.name = name; + } + @Override - public String toString() { return name; } + public String toString() { + return name; + } } - public SecurityRestFilter(Settings settings, ThreadContext threadContext, AuthenticationService authenticationService, - SecondaryAuthenticator secondaryAuthenticator, RestHandler restHandler, boolean extractClientCertificate) { + public SecurityRestFilter( + Settings settings, + ThreadContext threadContext, + AuthenticationService authenticationService, + SecondaryAuthenticator secondaryAuthenticator, + RestHandler restHandler, + boolean extractClientCertificate + ) { this.settings = settings; this.threadContext = threadContext; this.authenticationService = authenticationService; @@ -87,27 +99,24 @@ public void handleRequest(RestRequest request, RestChannel channel, NodeClient c } final String requestUri = request.uri(); - authenticationService.authenticate(maybeWrapRestRequest(request), ActionListener.wrap( - authentication -> { - if (authentication == null) { - logger.trace("No authentication available for REST request [{}]", requestUri); - } else { - logger.trace("Authenticated REST request [{}] as {}", requestUri, authentication); + authenticationService.authenticate(maybeWrapRestRequest(request), ActionListener.wrap(authentication -> { + if (authentication == null) { + logger.trace("No authentication available for REST request [{}]", requestUri); + } else { + logger.trace("Authenticated REST request [{}] as {}", requestUri, authentication); + } + secondaryAuthenticator.authenticateAndAttachToContext(request, ActionListener.wrap(secondaryAuthentication -> { + if (secondaryAuthentication != null) { + logger.trace("Found secondary authentication {} in REST request [{}]", secondaryAuthentication, requestUri); } - secondaryAuthenticator.authenticateAndAttachToContext(request, ActionListener.wrap( - secondaryAuthentication -> { - if (secondaryAuthentication != null) { - logger.trace("Found secondary authentication {} in REST request [{}]", secondaryAuthentication, requestUri); - } - RemoteHostHeader.process(request, threadContext); - try { - restHandler.handleRequest(request, channel, client); - } catch (Exception e) { - handleException(ActionType.RequestHandling, request, channel, e); - } - }, - e -> handleException(ActionType.SecondaryAuthentication, request, channel, e))); - }, e -> handleException(ActionType.Authentication, request, channel, e))); + RemoteHostHeader.process(request, threadContext); + try { + restHandler.handleRequest(request, channel, client); + } catch (Exception e) { + handleException(ActionType.RequestHandling, request, channel, e); + } + }, e -> handleException(ActionType.SecondaryAuthentication, request, channel, e))); + }, e -> handleException(ActionType.Authentication, request, channel, e))); } else { restHandler.handleRequest(request, channel, client); } @@ -120,7 +129,9 @@ protected void handleException(ActionType actionType, RestRequest request, RestC channel.sendResponse(new BytesRestResponse(channel, restStatus, e) { @Override - protected boolean skipStackTrace() { return restStatus == RestStatus.UNAUTHORIZED; } + protected boolean skipStackTrace() { + return restStatus == RestStatus.UNAUTHORIZED; + } @Override public Map> filterHeaders(Map> headers) { @@ -139,8 +150,10 @@ public Map> filterHeaders(Map> headers }); } catch (Exception inner) { inner.addSuppressed(e); - logger.error((Supplier) () -> - new ParameterizedMessage("failed to send failure response for uri [{}]", request.uri()), inner); + logger.error( + (Supplier) () -> new ParameterizedMessage("failed to send failure response for uri [{}]", request.uri()), + inner + ); } } @@ -166,7 +179,7 @@ public List routes() { private RestRequest maybeWrapRestRequest(RestRequest restRequest) throws IOException { if (restHandler instanceof RestRequestFilter) { - return ((RestRequestFilter)restHandler).getFilteredRequest(restRequest); + return ((RestRequestFilter) restHandler).getFilteredRequest(restRequest); } return restRequest; } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/RestAuthenticateAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/RestAuthenticateAction.java index b4295dee7e042..d746ba2ddde96 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/RestAuthenticateAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/RestAuthenticateAction.java @@ -7,16 +7,16 @@ package org.elasticsearch.xpack.security.rest.action; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.security.SecurityContext; import org.elasticsearch.xpack.core.security.action.user.AuthenticateAction; import org.elasticsearch.xpack.core.security.action.user.AuthenticateRequest; @@ -40,8 +40,7 @@ public RestAuthenticateAction(Settings settings, SecurityContext securityContext @Override public List routes() { return List.of( - Route.builder(GET, "/_security/_authenticate") - .replaces(GET, "/_xpack/security/_authenticate", RestApiVersion.V_7).build() + Route.builder(GET, "/_security/_authenticate").replaces(GET, "/_xpack/security/_authenticate", RestApiVersion.V_7).build() ); } @@ -58,14 +57,17 @@ public RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClient c } final String username = user.principal(); - return channel -> client.execute(AuthenticateAction.INSTANCE, new AuthenticateRequest(username), - new RestBuilderListener(channel) { - @Override - public RestResponse buildResponse(AuthenticateResponse authenticateResponse, XContentBuilder builder) throws Exception { - authenticateResponse.authentication().toXContent(builder, ToXContent.EMPTY_PARAMS); - return new BytesRestResponse(RestStatus.OK, builder); + return channel -> client.execute( + AuthenticateAction.INSTANCE, + new AuthenticateRequest(username), + new RestBuilderListener(channel) { + @Override + public RestResponse buildResponse(AuthenticateResponse authenticateResponse, XContentBuilder builder) throws Exception { + authenticateResponse.authentication().toXContent(builder, ToXContent.EMPTY_PARAMS); + return new BytesRestResponse(RestStatus.OK, builder); + } } - }); + ); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/RestDelegatePkiAuthenticationAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/RestDelegatePkiAuthenticationAction.java index 2fc6e00bac939..e01cc3673ff97 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/RestDelegatePkiAuthenticationAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/RestDelegatePkiAuthenticationAction.java @@ -11,8 +11,6 @@ import org.apache.logging.log4j.Logger; import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.license.LicenseUtils; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.BytesRestResponse; @@ -20,6 +18,8 @@ import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.security.action.DelegatePkiAuthenticationAction; import org.elasticsearch.xpack.core.security.action.DelegatePkiAuthenticationRequest; import org.elasticsearch.xpack.core.security.action.DelegatePkiAuthenticationResponse; @@ -67,15 +67,18 @@ protected Exception checkFeatureAvailable(RestRequest request) { protected RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClient client) throws IOException { try (XContentParser parser = request.contentParser()) { final DelegatePkiAuthenticationRequest delegatePkiRequest = DelegatePkiAuthenticationRequest.fromXContent(parser); - return channel -> client.execute(DelegatePkiAuthenticationAction.INSTANCE, delegatePkiRequest, - new RestBuilderListener(channel) { - @Override - public RestResponse buildResponse(DelegatePkiAuthenticationResponse delegatePkiResponse, XContentBuilder builder) - throws Exception { - delegatePkiResponse.toXContent(builder, channel.request()); - return new BytesRestResponse(RestStatus.OK, builder); - } - }); + return channel -> client.execute( + DelegatePkiAuthenticationAction.INSTANCE, + delegatePkiRequest, + new RestBuilderListener(channel) { + @Override + public RestResponse buildResponse(DelegatePkiAuthenticationResponse delegatePkiResponse, XContentBuilder builder) + throws Exception { + delegatePkiResponse.toXContent(builder, channel.request()); + return new BytesRestResponse(RestStatus.OK, builder); + } + } + ); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/SecurityBaseRestHandler.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/SecurityBaseRestHandler.java index 89f9c9455eb20..a5c2ce3c7c896 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/SecurityBaseRestHandler.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/SecurityBaseRestHandler.java @@ -70,7 +70,6 @@ protected Exception checkFeatureAvailable(RestRequest request) { } } - /** * Implementers should implement this method as they normally would for * {@link BaseRestHandler#prepareRequest(RestRequest, NodeClient)} and ensure that all request diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/apikey/RestCreateApiKeyAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/apikey/RestCreateApiKeyAction.java index 91c46ad2117a5..1ba6447b7fd13 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/apikey/RestCreateApiKeyAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/apikey/RestCreateApiKeyAction.java @@ -39,9 +39,7 @@ public RestCreateApiKeyAction(Settings settings, XPackLicenseState licenseState) @Override public List routes() { - return List.of( - new Route(POST, "/_security/api_key"), - new Route(PUT, "/_security/api_key")); + return List.of(new Route(POST, "/_security/api_key"), new Route(PUT, "/_security/api_key")); } @Override @@ -52,10 +50,13 @@ public String getName() { @Override protected RestChannelConsumer innerPrepareRequest(final RestRequest request, final NodeClient client) throws IOException { String refresh = request.param("refresh"); - CreateApiKeyRequestBuilder builder = new CreateApiKeyRequestBuilder(client) - .source(request.requiredContent(), request.getXContentType()) - .setRefreshPolicy((refresh != null) ? - WriteRequest.RefreshPolicy.parse(request.param("refresh")) : CreateApiKeyRequest.DEFAULT_REFRESH_POLICY); + CreateApiKeyRequestBuilder builder = new CreateApiKeyRequestBuilder(client).source( + request.requiredContent(), + request.getXContentType() + ) + .setRefreshPolicy( + (refresh != null) ? WriteRequest.RefreshPolicy.parse(request.param("refresh")) : CreateApiKeyRequest.DEFAULT_REFRESH_POLICY + ); return channel -> builder.execute(new RestToXContentListener<>(channel)); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/apikey/RestGetApiKeyAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/apikey/RestGetApiKeyAction.java index d17755a1c8966..ddc65e1dd4581 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/apikey/RestGetApiKeyAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/apikey/RestGetApiKeyAction.java @@ -10,13 +10,13 @@ import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.security.action.GetApiKeyAction; import org.elasticsearch.xpack.core.security.action.GetApiKeyRequest; import org.elasticsearch.xpack.core.security.action.GetApiKeyResponse; @@ -49,20 +49,19 @@ protected RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClien final String realmName = request.param("realm_name"); final boolean myApiKeysOnly = request.paramAsBoolean("owner", false); final GetApiKeyRequest getApiKeyRequest = new GetApiKeyRequest(realmName, userName, apiKeyId, apiKeyName, myApiKeysOnly); - return channel -> client.execute(GetApiKeyAction.INSTANCE, getApiKeyRequest, - new RestBuilderListener(channel) { - @Override - public RestResponse buildResponse(GetApiKeyResponse getApiKeyResponse, XContentBuilder builder) throws Exception { - getApiKeyResponse.toXContent(builder, channel.request()); + return channel -> client.execute(GetApiKeyAction.INSTANCE, getApiKeyRequest, new RestBuilderListener(channel) { + @Override + public RestResponse buildResponse(GetApiKeyResponse getApiKeyResponse, XContentBuilder builder) throws Exception { + getApiKeyResponse.toXContent(builder, channel.request()); - // return HTTP status 404 if no API key found for API key id - if (Strings.hasText(apiKeyId) && getApiKeyResponse.getApiKeyInfos().length == 0) { - return new BytesRestResponse(RestStatus.NOT_FOUND, builder); - } - return new BytesRestResponse(RestStatus.OK, builder); - } + // return HTTP status 404 if no API key found for API key id + if (Strings.hasText(apiKeyId) && getApiKeyResponse.getApiKeyInfos().length == 0) { + return new BytesRestResponse(RestStatus.NOT_FOUND, builder); + } + return new BytesRestResponse(RestStatus.OK, builder); + } - }); + }); } @Override diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/apikey/RestGrantApiKeyAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/apikey/RestGrantApiKeyAction.java index 580d44f4f7d65..450845fa5265e 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/apikey/RestGrantApiKeyAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/apikey/RestGrantApiKeyAction.java @@ -11,16 +11,16 @@ import org.elasticsearch.ExceptionsHelper; import org.elasticsearch.action.support.WriteRequest; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.ObjectParser; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestRequestFilter; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestToXContentListener; +import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.security.action.CreateApiKeyRequestBuilder; import org.elasticsearch.xpack.core.security.action.CreateApiKeyResponse; import org.elasticsearch.xpack.core.security.action.GrantApiKeyAction; @@ -45,17 +45,29 @@ public final class RestGrantApiKeyAction extends SecurityBaseRestHandler impleme static { PARSER.declareString((req, str) -> req.getGrant().setType(str), new ParseField("grant_type")); PARSER.declareString((req, str) -> req.getGrant().setUsername(str), new ParseField("username")); - PARSER.declareField((req, secStr) -> req.getGrant().setPassword(secStr), RestGrantApiKeyAction::getSecureString, - new ParseField("password"), ObjectParser.ValueType.STRING); - PARSER.declareField((req, secStr) -> req.getGrant().setAccessToken(secStr), RestGrantApiKeyAction::getSecureString, - new ParseField("access_token"), ObjectParser.ValueType.STRING); - PARSER.declareObject((req, api) -> req.setApiKeyRequest(api), (parser, ignore) -> CreateApiKeyRequestBuilder.parse(parser), - new ParseField("api_key")); + PARSER.declareField( + (req, secStr) -> req.getGrant().setPassword(secStr), + RestGrantApiKeyAction::getSecureString, + new ParseField("password"), + ObjectParser.ValueType.STRING + ); + PARSER.declareField( + (req, secStr) -> req.getGrant().setAccessToken(secStr), + RestGrantApiKeyAction::getSecureString, + new ParseField("access_token"), + ObjectParser.ValueType.STRING + ); + PARSER.declareObject( + (req, api) -> req.setApiKeyRequest(api), + (parser, ignore) -> CreateApiKeyRequestBuilder.parse(parser), + new ParseField("api_key") + ); } private static SecureString getSecureString(XContentParser parser) throws IOException { return new SecureString( - Arrays.copyOfRange(parser.textCharacters(), parser.textOffset(), parser.textOffset() + parser.textLength())); + Arrays.copyOfRange(parser.textCharacters(), parser.textOffset(), parser.textOffset() + parser.textLength()) + ); } public RestGrantApiKeyAction(Settings settings, XPackLicenseState licenseState) { @@ -64,9 +76,7 @@ public RestGrantApiKeyAction(Settings settings, XPackLicenseState licenseState) @Override public List routes() { - return List.of( - new Route(POST, "/_security/api_key/grant"), - new Route(PUT, "/_security/api_key/grant")); + return List.of(new Route(POST, "/_security/api_key/grant"), new Route(PUT, "/_security/api_key/grant")); } @Override @@ -82,16 +92,20 @@ protected RestChannelConsumer innerPrepareRequest(final RestRequest request, fin if (refresh != null) { grantRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.parse(refresh)); } - return channel -> client.execute(GrantApiKeyAction.INSTANCE, grantRequest, + return channel -> client.execute( + GrantApiKeyAction.INSTANCE, + grantRequest, new RestToXContentListener(channel).delegateResponse((listener, ex) -> { RestStatus status = ExceptionsHelper.status(ex); if (status == RestStatus.UNAUTHORIZED) { listener.onFailure( - new ElasticsearchSecurityException("Failed to authenticate api key grant", RestStatus.FORBIDDEN, ex)); + new ElasticsearchSecurityException("Failed to authenticate api key grant", RestStatus.FORBIDDEN, ex) + ); } else { listener.onFailure(ex); } - })); + }) + ); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/apikey/RestInvalidateApiKeyAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/apikey/RestInvalidateApiKeyAction.java index 9bdfa9540ccc7..6ba070aaafca4 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/apikey/RestInvalidateApiKeyAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/apikey/RestInvalidateApiKeyAction.java @@ -11,16 +11,16 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.RestApiVersion; -import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.xcontent.ConstructingObjectParser; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.security.action.InvalidateApiKeyAction; import org.elasticsearch.xpack.core.security.action.InvalidateApiKeyRequest; import org.elasticsearch.xpack.core.security.action.InvalidateApiKeyResponse; @@ -36,12 +36,18 @@ */ public final class RestInvalidateApiKeyAction extends SecurityBaseRestHandler { @SuppressWarnings("unchecked") - static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("invalidate_api_key", - a -> { - return new InvalidateApiKeyRequest((String) a[0], (String) a[1], (String) a[2], - (a[3] == null) ? false : (Boolean) a[3], - (a[4] == null) ? null : ((List) a[4]).toArray(new String[0])); - }); + static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "invalidate_api_key", + a -> { + return new InvalidateApiKeyRequest( + (String) a[0], + (String) a[1], + (String) a[2], + (a[3] == null) ? false : (Boolean) a[3], + (a[4] == null) ? null : ((List) a[4]).toArray(new String[0]) + ); + } + ); static { initObjectParser(PARSER, false); @@ -60,15 +66,17 @@ public List routes() { protected RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClient client) throws IOException { try (XContentParser parser = request.contentParser()) { final InvalidateApiKeyRequest invalidateApiKeyRequest = getObjectParser(request).parse(parser, null); - return channel -> client.execute(InvalidateApiKeyAction.INSTANCE, invalidateApiKeyRequest, + return channel -> client.execute( + InvalidateApiKeyAction.INSTANCE, + invalidateApiKeyRequest, new RestBuilderListener(channel) { @Override - public RestResponse buildResponse(InvalidateApiKeyResponse invalidateResp, - XContentBuilder builder) throws Exception { + public RestResponse buildResponse(InvalidateApiKeyResponse invalidateResp, XContentBuilder builder) throws Exception { invalidateResp.toXContent(builder, channel.request()); return new BytesRestResponse(RestStatus.OK, builder); } - }); + } + ); } } @@ -80,24 +88,31 @@ public String getName() { private ConstructingObjectParser getObjectParser(RestRequest request) { if (request.getRestApiVersion() == RestApiVersion.V_7) { final ConstructingObjectParser objectParser = new ConstructingObjectParser<>( - "invalidate_api_key_v7", a -> { - final String id = (String) a[5]; - @SuppressWarnings("unchecked") - final List ids = (List) a[4]; - if (id != null && ids != null) { - throw new IllegalArgumentException("Must use either [id] or [ids], not both at the same time"); - } - final String[] idsArray; - if (Strings.hasText(id)) { - idsArray = new String[] { id }; - } else if (ids != null) { - idsArray = ids.toArray(String[]::new); - } else { - idsArray = null; + "invalidate_api_key_v7", + a -> { + final String id = (String) a[5]; + @SuppressWarnings("unchecked") + final List ids = (List) a[4]; + if (id != null && ids != null) { + throw new IllegalArgumentException("Must use either [id] or [ids], not both at the same time"); + } + final String[] idsArray; + if (Strings.hasText(id)) { + idsArray = new String[] { id }; + } else if (ids != null) { + idsArray = ids.toArray(String[]::new); + } else { + idsArray = null; + } + return new InvalidateApiKeyRequest( + (String) a[0], + (String) a[1], + (String) a[2], + (a[3] == null) ? false : (Boolean) a[3], + idsArray + ); } - return new InvalidateApiKeyRequest((String) a[0], (String) a[1], (String) a[2], - (a[3] == null) ? false : (Boolean) a[3], idsArray); - }); + ); initObjectParser(objectParser, true); return objectParser; } else { diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/apikey/RestQueryApiKeyAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/apikey/RestQueryApiKeyAction.java index c91f33eba6db7..e7716cbc92b2c 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/apikey/RestQueryApiKeyAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/apikey/RestQueryApiKeyAction.java @@ -9,10 +9,6 @@ import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.ConstructingObjectParser; -import org.elasticsearch.xcontent.ObjectParser; -import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParserUtils; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.license.XPackLicenseState; @@ -20,6 +16,10 @@ import org.elasticsearch.rest.action.RestToXContentListener; import org.elasticsearch.search.searchafter.SearchAfterBuilder; import org.elasticsearch.search.sort.FieldSortBuilder; +import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.security.action.apikey.QueryApiKeyAction; import org.elasticsearch.xpack.core.security.action.apikey.QueryApiKeyRequest; import org.elasticsearch.xpack.security.rest.action.SecurityBaseRestHandler; @@ -27,10 +27,10 @@ import java.io.IOException; import java.util.List; -import static org.elasticsearch.xcontent.ConstructingObjectParser.optionalConstructorArg; import static org.elasticsearch.index.query.AbstractQueryBuilder.parseInnerQueryBuilder; import static org.elasticsearch.rest.RestRequest.Method.GET; import static org.elasticsearch.rest.RestRequest.Method.POST; +import static org.elasticsearch.xcontent.ConstructingObjectParser.optionalConstructorArg; /** * Rest action to search for API keys @@ -40,8 +40,14 @@ public final class RestQueryApiKeyAction extends SecurityBaseRestHandler { @SuppressWarnings("unchecked") private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( "query_api_key_request", - a -> new QueryApiKeyRequest((QueryBuilder) a[0], (Integer) a[1], (Integer) a[2], - (List) a[3], (SearchAfterBuilder) a[4])); + a -> new QueryApiKeyRequest( + (QueryBuilder) a[0], + (Integer) a[1], + (Integer) a[2], + (List) a[3], + (SearchAfterBuilder) a[4] + ) + ); static { PARSER.declareObject(optionalConstructorArg(), (p, c) -> parseInnerQueryBuilder(p), new ParseField("query")); @@ -59,8 +65,12 @@ public final class RestQueryApiKeyAction extends SecurityBaseRestHandler { throw new IllegalArgumentException("mal-formatted sort object"); } }, new ParseField("sort")); - PARSER.declareField(optionalConstructorArg(), (p, c) -> SearchAfterBuilder.fromXContent(p), - new ParseField("search_after"), ObjectParser.ValueType.VALUE_ARRAY); + PARSER.declareField( + optionalConstructorArg(), + (p, c) -> SearchAfterBuilder.fromXContent(p), + new ParseField("search_after"), + ObjectParser.ValueType.VALUE_ARRAY + ); } /** @@ -74,9 +84,7 @@ public RestQueryApiKeyAction(Settings settings, XPackLicenseState licenseState) @Override public List routes() { - return List.of( - new Route(GET, "/_security/_query/api_key"), - new Route(POST, "/_security/_query/api_key")); + return List.of(new Route(GET, "/_security/_query/api_key"), new Route(POST, "/_security/_query/api_key")); } @Override @@ -86,8 +94,9 @@ public String getName() { @Override protected RestChannelConsumer innerPrepareRequest(final RestRequest request, final NodeClient client) throws IOException { - final QueryApiKeyRequest queryApiKeyRequest = - request.hasContentOrSourceParam() ? PARSER.parse(request.contentOrSourceParamParser(), null) : new QueryApiKeyRequest(); + final QueryApiKeyRequest queryApiKeyRequest = request.hasContentOrSourceParam() + ? PARSER.parse(request.contentOrSourceParamParser(), null) + : new QueryApiKeyRequest(); return channel -> client.execute(QueryApiKeyAction.INSTANCE, queryApiKeyRequest, new RestToXContentListener<>(channel)); } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/enrollment/EnrollmentBaseRestHandler.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/enrollment/EnrollmentBaseRestHandler.java index 40dfc5a64ead0..9b8cf92d18164 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/enrollment/EnrollmentBaseRestHandler.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/enrollment/EnrollmentBaseRestHandler.java @@ -30,9 +30,12 @@ protected Exception checkFeatureAvailable(RestRequest request) { if (failedFeature != null) { return failedFeature; } else if (XPackSettings.ENROLLMENT_ENABLED.get(settings) == false) { - return new ElasticsearchSecurityException("Enrollment mode is not enabled. Set [" + XPackSettings.ENROLLMENT_ENABLED.getKey() + - "] to true, in order to use this API.", - RestStatus.FORBIDDEN); + return new ElasticsearchSecurityException( + "Enrollment mode is not enabled. Set [" + + XPackSettings.ENROLLMENT_ENABLED.getKey() + + "] to true, in order to use this API.", + RestStatus.FORBIDDEN + ); } else { return null; } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/enrollment/RestKibanaEnrollAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/enrollment/RestKibanaEnrollAction.java index c993410b040ba..57acc53bee835 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/enrollment/RestKibanaEnrollAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/enrollment/RestKibanaEnrollAction.java @@ -9,13 +9,13 @@ import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.security.action.enrollment.KibanaEnrollmentAction; import org.elasticsearch.xpack.core.security.action.enrollment.KibanaEnrollmentRequest; import org.elasticsearch.xpack.core.security.action.enrollment.KibanaEnrollmentResponse; @@ -33,23 +33,29 @@ public RestKibanaEnrollAction(Settings settings, XPackLicenseState licenseState) super(settings, licenseState); } - @Override public String getName() { + @Override + public String getName() { return "kibana_enroll_action"; } - @Override public List routes() { + @Override + public List routes() { return List.of(new Route(RestRequest.Method.GET, "/_security/enroll/kibana")); } - @Override protected RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClient client) throws IOException { - return restChannel -> client.execute(KibanaEnrollmentAction.INSTANCE, + @Override + protected RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClient client) throws IOException { + return restChannel -> client.execute( + KibanaEnrollmentAction.INSTANCE, new KibanaEnrollmentRequest(), new RestBuilderListener(restChannel) { - @Override public RestResponse buildResponse( - KibanaEnrollmentResponse kibanaEnrollmentResponse, XContentBuilder builder) throws Exception { + @Override + public RestResponse buildResponse(KibanaEnrollmentResponse kibanaEnrollmentResponse, XContentBuilder builder) + throws Exception { kibanaEnrollmentResponse.toXContent(builder, channel.request()); return new BytesRestResponse(RestStatus.OK, builder); } - }); + } + ); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/enrollment/RestNodeEnrollmentAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/enrollment/RestNodeEnrollmentAction.java index 507dddc029679..ccf36a62b99ba 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/enrollment/RestNodeEnrollmentAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/enrollment/RestNodeEnrollmentAction.java @@ -9,13 +9,13 @@ import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.security.action.enrollment.NodeEnrollmentAction; import org.elasticsearch.xpack.core.security.action.enrollment.NodeEnrollmentRequest; import org.elasticsearch.xpack.core.security.action.enrollment.NodeEnrollmentResponse; @@ -33,25 +33,28 @@ public RestNodeEnrollmentAction(Settings settings, XPackLicenseState licenseStat super(settings, licenseState); } - @Override public String getName() { + @Override + public String getName() { return "node_enroll_action"; } - @Override public List routes() { - return List.of( - new Route(RestRequest.Method.GET, "_security/enroll/node") - ); + @Override + public List routes() { + return List.of(new Route(RestRequest.Method.GET, "_security/enroll/node")); } - @Override protected RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClient client) throws IOException { - return restChannel -> client.execute(NodeEnrollmentAction.INSTANCE, + @Override + protected RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClient client) throws IOException { + return restChannel -> client.execute( + NodeEnrollmentAction.INSTANCE, new NodeEnrollmentRequest(), new RestBuilderListener(restChannel) { - @Override public RestResponse buildResponse( - NodeEnrollmentResponse nodeEnrollmentResponse, XContentBuilder builder) throws Exception { + @Override + public RestResponse buildResponse(NodeEnrollmentResponse nodeEnrollmentResponse, XContentBuilder builder) throws Exception { nodeEnrollmentResponse.toXContent(builder, channel.request()); return new BytesRestResponse(RestStatus.OK, builder); } - }); + } + ); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/oauth2/RestGetTokenAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/oauth2/RestGetTokenAction.java index 18ad3d75d6331..ab933628c36cc 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/oauth2/RestGetTokenAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/oauth2/RestGetTokenAction.java @@ -12,20 +12,20 @@ import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.ActionType; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.ConstructingObjectParser; -import org.elasticsearch.xcontent.ObjectParser.ValueType; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestChannel; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestRequestFilter; import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.security.action.token.CreateTokenAction; import org.elasticsearch.xpack.core.security.action.token.CreateTokenRequest; import org.elasticsearch.xpack.core.security.action.token.CreateTokenResponse; @@ -49,18 +49,29 @@ */ public final class RestGetTokenAction extends TokenBaseRestHandler implements RestRequestFilter { - static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("token_request", - a -> new CreateTokenRequest((String) a[0], (String) a[1], (SecureString) a[2], (SecureString) a[3], (String) a[4], - (String) a[5])); + static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "token_request", + a -> new CreateTokenRequest((String) a[0], (String) a[1], (SecureString) a[2], (SecureString) a[3], (String) a[4], (String) a[5]) + ); static { PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), new ParseField("grant_type")); PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), new ParseField("username")); - PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(), parser -> new SecureString( - Arrays.copyOfRange(parser.textCharacters(), parser.textOffset(), parser.textOffset() + parser.textLength())), - new ParseField("password"), ValueType.STRING); - PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(), parser -> new SecureString( - Arrays.copyOfRange(parser.textCharacters(), parser.textOffset(), parser.textOffset() + parser.textLength())), - new ParseField("kerberos_ticket"), ValueType.STRING); + PARSER.declareField( + ConstructingObjectParser.optionalConstructorArg(), + parser -> new SecureString( + Arrays.copyOfRange(parser.textCharacters(), parser.textOffset(), parser.textOffset() + parser.textLength()) + ), + new ParseField("password"), + ValueType.STRING + ); + PARSER.declareField( + ConstructingObjectParser.optionalConstructorArg(), + parser -> new SecureString( + Arrays.copyOfRange(parser.textCharacters(), parser.textOffset(), parser.textOffset() + parser.textLength()) + ), + new ParseField("kerberos_ticket"), + ValueType.STRING + ); PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), new ParseField("scope")); PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), new ParseField("refresh_token")); } @@ -72,8 +83,7 @@ public RestGetTokenAction(Settings settings, XPackLicenseState xPackLicenseState @Override public List routes() { return List.of( - Route.builder(POST, "/_security/oauth2/token") - .replaces(POST, "/_xpack/security/oauth2/token", RestApiVersion.V_7).build() + Route.builder(POST, "/_security/oauth2/token").replaces(POST, "/_xpack/security/oauth2/token", RestApiVersion.V_7).build() ); } @@ -83,15 +93,19 @@ public String getName() { } @Override - protected RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClient client)throws IOException { + protected RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClient client) throws IOException { try (XContentParser parser = request.contentParser()) { final CreateTokenRequest tokenRequest = PARSER.parse(parser, null); - final ActionType action = - "refresh_token".equals(tokenRequest.getGrantType()) ? RefreshTokenAction.INSTANCE : CreateTokenAction.INSTANCE; - return channel -> client.execute(action, tokenRequest, - // this doesn't use the RestBuilderListener since we need to override the - // handling of failures in some cases. - new CreateTokenResponseActionListener(channel, request, logger)); + final ActionType action = "refresh_token".equals(tokenRequest.getGrantType()) + ? RefreshTokenAction.INSTANCE + : CreateTokenAction.INSTANCE; + return channel -> client.execute( + action, + tokenRequest, + // this doesn't use the RestBuilderListener since we need to override the + // handling of failures in some cases. + new CreateTokenResponseActionListener(channel, request, logger) + ); } } @@ -101,8 +115,7 @@ static class CreateTokenResponseActionListener implements ActionListener PARSER = - new ConstructingObjectParser<>("invalidate_token", a -> { - final String token = (String) a[0]; - final String refreshToken = (String) a[1]; - final String tokenString; - final String tokenType; - if (Strings.hasLength(token) && Strings.hasLength(refreshToken)) { - throw new IllegalArgumentException("only one of [token, refresh_token] may be sent per request"); - } else if (Strings.hasLength(token)) { - tokenString = token; - tokenType = InvalidateTokenRequest.Type.ACCESS_TOKEN.getValue(); - } else if (Strings.hasLength(refreshToken)) { - tokenString = refreshToken; - tokenType = InvalidateTokenRequest.Type.REFRESH_TOKEN.getValue(); - } else { - tokenString = null; - tokenType = null; - } - return new InvalidateTokenRequest(tokenString, tokenType, (String) a[2], (String) a[3]); - }); + static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("invalidate_token", a -> { + final String token = (String) a[0]; + final String refreshToken = (String) a[1]; + final String tokenString; + final String tokenType; + if (Strings.hasLength(token) && Strings.hasLength(refreshToken)) { + throw new IllegalArgumentException("only one of [token, refresh_token] may be sent per request"); + } else if (Strings.hasLength(token)) { + tokenString = token; + tokenType = InvalidateTokenRequest.Type.ACCESS_TOKEN.getValue(); + } else if (Strings.hasLength(refreshToken)) { + tokenString = refreshToken; + tokenType = InvalidateTokenRequest.Type.REFRESH_TOKEN.getValue(); + } else { + tokenString = null; + tokenType = null; + } + return new InvalidateTokenRequest(tokenString, tokenType, (String) a[2], (String) a[3]); + }); static { PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), new ParseField("token")); @@ -70,8 +69,7 @@ public RestInvalidateTokenAction(Settings settings, XPackLicenseState xPackLicen @Override public List routes() { return List.of( - Route.builder(DELETE, "/_security/oauth2/token") - .replaces(DELETE, "/_xpack/security/oauth2/token", RestApiVersion.V_7).build() + Route.builder(DELETE, "/_security/oauth2/token").replaces(DELETE, "/_xpack/security/oauth2/token", RestApiVersion.V_7).build() ); } @@ -84,15 +82,17 @@ public String getName() { protected RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClient client) throws IOException { try (XContentParser parser = request.contentParser()) { final InvalidateTokenRequest invalidateTokenRequest = PARSER.parse(parser, null); - return channel -> client.execute(InvalidateTokenAction.INSTANCE, invalidateTokenRequest, + return channel -> client.execute( + InvalidateTokenAction.INSTANCE, + invalidateTokenRequest, new RestBuilderListener(channel) { @Override - public RestResponse buildResponse(InvalidateTokenResponse invalidateResp, - XContentBuilder builder) throws Exception { + public RestResponse buildResponse(InvalidateTokenResponse invalidateResp, XContentBuilder builder) throws Exception { invalidateResp.toXContent(builder, channel.request()); return new BytesRestResponse(invalidateResp.getResult().getRestStatus(), builder); } - }); + } + ); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/oidc/RestOpenIdConnectAuthenticateAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/oidc/RestOpenIdConnectAuthenticateAction.java index 6d320445dcc0c..25e384311beba 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/oidc/RestOpenIdConnectAuthenticateAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/oidc/RestOpenIdConnectAuthenticateAction.java @@ -9,21 +9,21 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.ObjectParser; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; +import org.elasticsearch.rest.RestRequestFilter; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.security.action.oidc.OpenIdConnectAuthenticateAction; import org.elasticsearch.xpack.core.security.action.oidc.OpenIdConnectAuthenticateRequest; import org.elasticsearch.xpack.core.security.action.oidc.OpenIdConnectAuthenticateResponse; -import org.elasticsearch.rest.RestRequestFilter; import java.io.IOException; import java.util.List; @@ -37,8 +37,10 @@ public class RestOpenIdConnectAuthenticateAction extends OpenIdConnectBaseRestHandler implements RestRequestFilter { private static final Logger logger = LogManager.getLogger(); - static final ObjectParser PARSER = new ObjectParser<>("oidc_authn", - OpenIdConnectAuthenticateRequest::new); + static final ObjectParser PARSER = new ObjectParser<>( + "oidc_authn", + OpenIdConnectAuthenticateRequest::new + ); static { PARSER.declareString(OpenIdConnectAuthenticateRequest::setRedirectUri, new ParseField("redirect_uri")); @@ -61,7 +63,9 @@ protected RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClien try (XContentParser parser = request.contentParser()) { final OpenIdConnectAuthenticateRequest authenticateRequest = PARSER.parse(parser, null); logger.trace("OIDC Authenticate: " + authenticateRequest); - return channel -> client.execute(OpenIdConnectAuthenticateAction.INSTANCE, authenticateRequest, + return channel -> client.execute( + OpenIdConnectAuthenticateAction.INSTANCE, + authenticateRequest, new RestBuilderListener(channel) { @Override public RestResponse buildResponse(OpenIdConnectAuthenticateResponse response, XContentBuilder builder) @@ -71,13 +75,14 @@ public RestResponse buildResponse(OpenIdConnectAuthenticateResponse response, XC builder.field("access_token", response.getAccessTokenString()); builder.field("refresh_token", response.getRefreshTokenString()); builder.field("expires_in", response.getExpiresIn().seconds()); - if(response.getAuthentication() != null) { + if (response.getAuthentication() != null) { builder.field("authentication", response.getAuthentication()); } builder.endObject(); return new BytesRestResponse(RestStatus.OK, builder); } - }); + } + ); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/oidc/RestOpenIdConnectLogoutAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/oidc/RestOpenIdConnectLogoutAction.java index 18d6d9ba023ef..18bc93a53cad0 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/oidc/RestOpenIdConnectLogoutAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/oidc/RestOpenIdConnectLogoutAction.java @@ -7,17 +7,17 @@ package org.elasticsearch.xpack.security.rest.action.oidc; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.ObjectParser; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.security.action.oidc.OpenIdConnectLogoutAction; import org.elasticsearch.xpack.core.security.action.oidc.OpenIdConnectLogoutRequest; import org.elasticsearch.xpack.core.security.action.oidc.OpenIdConnectLogoutResponse; @@ -33,8 +33,7 @@ */ public class RestOpenIdConnectLogoutAction extends OpenIdConnectBaseRestHandler { - static final ObjectParser PARSER = new ObjectParser<>("oidc_logout", - OpenIdConnectLogoutRequest::new); + static final ObjectParser PARSER = new ObjectParser<>("oidc_logout", OpenIdConnectLogoutRequest::new); static { PARSER.declareString(OpenIdConnectLogoutRequest::setToken, new ParseField("token")); @@ -54,7 +53,9 @@ public List routes() { protected RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClient client) throws IOException { try (XContentParser parser = request.contentParser()) { final OpenIdConnectLogoutRequest logoutRequest = PARSER.parse(parser, null); - return channel -> client.execute(OpenIdConnectLogoutAction.INSTANCE, logoutRequest, + return channel -> client.execute( + OpenIdConnectLogoutAction.INSTANCE, + logoutRequest, new RestBuilderListener(channel) { @Override public RestResponse buildResponse(OpenIdConnectLogoutResponse response, XContentBuilder builder) throws Exception { @@ -63,7 +64,8 @@ public RestResponse buildResponse(OpenIdConnectLogoutResponse response, XContent builder.endObject(); return new BytesRestResponse(RestStatus.OK, builder); } - }); + } + ); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/oidc/RestOpenIdConnectPrepareAuthenticationAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/oidc/RestOpenIdConnectPrepareAuthenticationAction.java index f8fc28d761146..1f169eeb19d63 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/oidc/RestOpenIdConnectPrepareAuthenticationAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/oidc/RestOpenIdConnectPrepareAuthenticationAction.java @@ -9,17 +9,17 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.ObjectParser; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.security.action.oidc.OpenIdConnectPrepareAuthenticationAction; import org.elasticsearch.xpack.core.security.action.oidc.OpenIdConnectPrepareAuthenticationRequest; import org.elasticsearch.xpack.core.security.action.oidc.OpenIdConnectPrepareAuthenticationResponse; @@ -35,8 +35,10 @@ public class RestOpenIdConnectPrepareAuthenticationAction extends OpenIdConnectBaseRestHandler { private static final Logger logger = LogManager.getLogger(); - static final ObjectParser PARSER = new ObjectParser<>("oidc_prepare_authentication", - OpenIdConnectPrepareAuthenticationRequest::new); + static final ObjectParser PARSER = new ObjectParser<>( + "oidc_prepare_authentication", + OpenIdConnectPrepareAuthenticationRequest::new + ); static { PARSER.declareString(OpenIdConnectPrepareAuthenticationRequest::setRealmName, new ParseField("realm")); @@ -60,7 +62,9 @@ protected RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClien try (XContentParser parser = request.contentParser()) { final OpenIdConnectPrepareAuthenticationRequest prepareAuthenticationRequest = PARSER.parse(parser, null); logger.trace("OIDC Prepare Authentication: " + prepareAuthenticationRequest); - return channel -> client.execute(OpenIdConnectPrepareAuthenticationAction.INSTANCE, prepareAuthenticationRequest, + return channel -> client.execute( + OpenIdConnectPrepareAuthenticationAction.INSTANCE, + prepareAuthenticationRequest, new RestBuilderListener(channel) { @Override public RestResponse buildResponse(OpenIdConnectPrepareAuthenticationResponse response, XContentBuilder builder) @@ -68,7 +72,8 @@ public RestResponse buildResponse(OpenIdConnectPrepareAuthenticationResponse res logger.trace("OIDC Prepare Authentication Response: " + response); return new BytesRestResponse(RestStatus.OK, response.toXContent(builder, request)); } - }); + } + ); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/privilege/RestClearPrivilegesCacheAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/privilege/RestClearPrivilegesCacheAction.java index 4a1d6301ed218..b28270a4a3cbf 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/privilege/RestClearPrivilegesCacheAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/privilege/RestClearPrivilegesCacheAction.java @@ -40,8 +40,7 @@ public List routes() { @Override protected RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClient client) throws IOException { String[] applicationNames = request.paramAsStringArrayOrEmptyIfAll("application"); - final ClearSecurityCacheRequest req = - new ClearSecurityCacheRequest().cacheName("application_privileges").keys(applicationNames); + final ClearSecurityCacheRequest req = new ClearSecurityCacheRequest().cacheName("application_privileges").keys(applicationNames); return channel -> client.execute(ClearSecurityCacheAction.INSTANCE, req, new NodesResponseRestListener<>(channel)); } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/privilege/RestDeletePrivilegesAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/privilege/RestDeletePrivilegesAction.java index 17da1ffa1af75..dbd488fbf14d8 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/privilege/RestDeletePrivilegesAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/privilege/RestDeletePrivilegesAction.java @@ -7,15 +7,15 @@ package org.elasticsearch.xpack.security.rest.action.privilege; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.security.action.privilege.DeletePrivilegesRequestBuilder; import org.elasticsearch.xpack.core.security.action.privilege.DeletePrivilegesResponse; import org.elasticsearch.xpack.security.rest.action.SecurityBaseRestHandler; @@ -41,7 +41,8 @@ public RestDeletePrivilegesAction(Settings settings, XPackLicenseState licenseSt public List routes() { return List.of( Route.builder(DELETE, "/_security/privilege/{application}/{privilege}") - .replaces(DELETE, "/_xpack/security/privilege/{application}/{privilege}", RestApiVersion.V_7).build() + .replaces(DELETE, "/_xpack/security/privilege/{application}/{privilege}", RestApiVersion.V_7) + .build() ); } @@ -55,8 +56,7 @@ public RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClient c final String application = request.param("application"); final String[] privileges = request.paramAsStringArray("privilege", null); final String refresh = request.param("refresh"); - return channel -> new DeletePrivilegesRequestBuilder(client) - .application(application) + return channel -> new DeletePrivilegesRequestBuilder(client).application(application) .privileges(privileges) .setRefreshPolicy(refresh) .execute(new RestBuilderListener<>(channel) { diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/privilege/RestGetBuiltinPrivilegesAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/privilege/RestGetBuiltinPrivilegesAction.java index 0694d1b51be88..9d03698ea1ccd 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/privilege/RestGetBuiltinPrivilegesAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/privilege/RestGetBuiltinPrivilegesAction.java @@ -8,13 +8,13 @@ import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.security.action.privilege.GetBuiltinPrivilegesAction; import org.elasticsearch.xpack.core.security.action.privilege.GetBuiltinPrivilegesRequest; import org.elasticsearch.xpack.core.security.action.privilege.GetBuiltinPrivilegesResponse; @@ -46,7 +46,9 @@ public String getName() { @Override public RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClient client) throws IOException { - return channel -> client.execute(GetBuiltinPrivilegesAction.INSTANCE, new GetBuiltinPrivilegesRequest(), + return channel -> client.execute( + GetBuiltinPrivilegesAction.INSTANCE, + new GetBuiltinPrivilegesRequest(), new RestBuilderListener<>(channel) { @Override public RestResponse buildResponse(GetBuiltinPrivilegesResponse response, XContentBuilder builder) throws Exception { @@ -56,7 +58,8 @@ public RestResponse buildResponse(GetBuiltinPrivilegesResponse response, XConten builder.endObject(); return new BytesRestResponse(RestStatus.OK, builder); } - }); + } + ); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/privilege/RestGetPrivilegesAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/privilege/RestGetPrivilegesAction.java index 00a8ff9ba808d..f321e852a6e68 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/privilege/RestGetPrivilegesAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/privilege/RestGetPrivilegesAction.java @@ -8,16 +8,16 @@ import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.common.Strings; -import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.set.Sets; -import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.security.action.privilege.GetPrivilegesRequestBuilder; import org.elasticsearch.xpack.core.security.action.privilege.GetPrivilegesResponse; import org.elasticsearch.xpack.core.security.authz.privilege.ApplicationPrivilegeDescriptor; @@ -45,12 +45,13 @@ public RestGetPrivilegesAction(Settings settings, XPackLicenseState licenseState @Override public List routes() { return List.of( - Route.builder(GET, "/_security/privilege/") - .replaces(GET, "/_xpack/security/privilege/", RestApiVersion.V_7).build(), + Route.builder(GET, "/_security/privilege/").replaces(GET, "/_xpack/security/privilege/", RestApiVersion.V_7).build(), Route.builder(GET, "/_security/privilege/{application}") - .replaces(GET, "/_xpack/security/privilege/{application}", RestApiVersion.V_7).build(), + .replaces(GET, "/_xpack/security/privilege/{application}", RestApiVersion.V_7) + .build(), Route.builder(GET, "/_security/privilege/{application}/{privilege}") - .replaces(GET, "/_xpack/security/privilege/{application}/{privilege}", RestApiVersion.V_7).build() + .replaces(GET, "/_xpack/security/privilege/{application}/{privilege}", RestApiVersion.V_7) + .build() ); } @@ -99,10 +100,7 @@ public RestResponse buildResponse(GetPrivilegesResponse response, XContentBuilde } static Map> groupByApplicationName(ApplicationPrivilegeDescriptor[] privileges) { - return Arrays.stream(privileges).collect(Collectors.toMap( - ApplicationPrivilegeDescriptor::getApplication, - Collections::singleton, - Sets::union - )); + return Arrays.stream(privileges) + .collect(Collectors.toMap(ApplicationPrivilegeDescriptor::getApplication, Collections::singleton, Sets::union)); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/privilege/RestPutPrivilegesAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/privilege/RestPutPrivilegesAction.java index 7a360fee09ebf..baf5eca5f7f0a 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/privilege/RestPutPrivilegesAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/privilege/RestPutPrivilegesAction.java @@ -7,15 +7,15 @@ package org.elasticsearch.xpack.security.rest.action.privilege; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.security.action.privilege.PutPrivilegesRequestBuilder; import org.elasticsearch.xpack.core.security.action.privilege.PutPrivilegesResponse; import org.elasticsearch.xpack.core.security.authz.privilege.ApplicationPrivilege; @@ -43,10 +43,8 @@ public RestPutPrivilegesAction(Settings settings, XPackLicenseState licenseState @Override public List routes() { return List.of( - Route.builder(PUT, "/_security/privilege/") - .replaces(PUT, "/_xpack/security/privilege/", RestApiVersion.V_7).build(), - Route.builder(POST, "/_security/privilege/") - .replaces(POST, "/_xpack/security/privilege/", RestApiVersion.V_7).build() + Route.builder(PUT, "/_security/privilege/").replaces(PUT, "/_xpack/security/privilege/", RestApiVersion.V_7).build(), + Route.builder(POST, "/_security/privilege/").replaces(POST, "/_xpack/security/privilege/", RestApiVersion.V_7).build() ); } @@ -57,9 +55,10 @@ public String getName() { @Override public RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClient client) throws IOException { - PutPrivilegesRequestBuilder requestBuilder = new PutPrivilegesRequestBuilder(client) - .source(request.requiredContent(), request.getXContentType()) - .setRefreshPolicy(request.param("refresh")); + PutPrivilegesRequestBuilder requestBuilder = new PutPrivilegesRequestBuilder(client).source( + request.requiredContent(), + request.getXContentType() + ).setRefreshPolicy(request.param("refresh")); return execute(requestBuilder); } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/realm/RestClearRealmCacheAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/realm/RestClearRealmCacheAction.java index e2bcc912e1651..28587563ed2bf 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/realm/RestClearRealmCacheAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/realm/RestClearRealmCacheAction.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.security.rest.action.realm; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestActions.NodesResponseRestListener; @@ -30,7 +30,8 @@ public RestClearRealmCacheAction(Settings settings, XPackLicenseState licenseSta public List routes() { return List.of( Route.builder(POST, "/_security/realm/{realms}/_clear_cache") - .replaces(POST, "/_xpack/security/realm/{realms}/_clear_cache", RestApiVersion.V_7).build() + .replaces(POST, "/_xpack/security/realm/{realms}/_clear_cache", RestApiVersion.V_7) + .build() ); } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/role/RestClearRolesCacheAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/role/RestClearRolesCacheAction.java index cd70c94d6d1dc..9b545fbe03a85 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/role/RestClearRolesCacheAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/role/RestClearRolesCacheAction.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.security.rest.action.role; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestActions.NodesResponseRestListener; @@ -30,7 +30,8 @@ public RestClearRolesCacheAction(Settings settings, XPackLicenseState licenseSta public List routes() { return List.of( Route.builder(POST, "/_security/role/{name}/_clear_cache") - .replaces(POST, "/_xpack/security/role/{name}/_clear_cache", RestApiVersion.V_7).build() + .replaces(POST, "/_xpack/security/role/{name}/_clear_cache", RestApiVersion.V_7) + .build() ); } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/role/RestDeleteRoleAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/role/RestDeleteRoleAction.java index 9dd8cf6fe7d54..96cff574e8e7a 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/role/RestDeleteRoleAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/role/RestDeleteRoleAction.java @@ -7,15 +7,15 @@ package org.elasticsearch.xpack.security.rest.action.role; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.security.action.role.DeleteRoleRequestBuilder; import org.elasticsearch.xpack.core.security.action.role.DeleteRoleResponse; import org.elasticsearch.xpack.security.rest.action.SecurityBaseRestHandler; @@ -37,8 +37,7 @@ public RestDeleteRoleAction(Settings settings, XPackLicenseState licenseState) { @Override public List routes() { return List.of( - Route.builder(DELETE, "/_security/role/{name}") - .replaces(DELETE, "/_xpack/security/role/{name}", RestApiVersion.V_7).build() + Route.builder(DELETE, "/_security/role/{name}").replaces(DELETE, "/_xpack/security/role/{name}", RestApiVersion.V_7).build() ); } @@ -52,15 +51,15 @@ public RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClient c final String name = request.param("name"); final String refresh = request.param("refresh"); - return channel -> new DeleteRoleRequestBuilder(client) - .name(name) + return channel -> new DeleteRoleRequestBuilder(client).name(name) .setRefreshPolicy(refresh) .execute(new RestBuilderListener<>(channel) { @Override public RestResponse buildResponse(DeleteRoleResponse response, XContentBuilder builder) throws Exception { return new BytesRestResponse( response.found() ? RestStatus.OK : RestStatus.NOT_FOUND, - builder.startObject().field("found", response.found()).endObject()); + builder.startObject().field("found", response.found()).endObject() + ); } }); } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/role/RestGetRolesAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/role/RestGetRolesAction.java index 9e24909c2d6c5..f95d584e46500 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/role/RestGetRolesAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/role/RestGetRolesAction.java @@ -8,15 +8,15 @@ import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.common.Strings; -import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.security.action.role.GetRolesRequestBuilder; import org.elasticsearch.xpack.core.security.action.role.GetRolesResponse; import org.elasticsearch.xpack.core.security.authz.RoleDescriptor; @@ -39,10 +39,8 @@ public RestGetRolesAction(Settings settings, XPackLicenseState licenseState) { @Override public List routes() { return List.of( - Route.builder(GET, "/_security/role/") - .replaces(GET, "/_xpack/security/role/", RestApiVersion.V_7).build(), - Route.builder(GET, "/_security/role/{name}") - .replaces(GET, "/_xpack/security/role/{name}", RestApiVersion.V_7).build() + Route.builder(GET, "/_security/role/").replaces(GET, "/_xpack/security/role/", RestApiVersion.V_7).build(), + Route.builder(GET, "/_security/role/{name}").replaces(GET, "/_xpack/security/role/{name}", RestApiVersion.V_7).build() ); } @@ -54,9 +52,7 @@ public String getName() { @Override public RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClient client) throws IOException { final String[] roles = request.paramAsStringArray("name", Strings.EMPTY_ARRAY); - return channel -> new GetRolesRequestBuilder(client) - .names(roles) - .execute(new RestBuilderListener<>(channel) { + return channel -> new GetRolesRequestBuilder(client).names(roles).execute(new RestBuilderListener<>(channel) { @Override public RestResponse buildResponse(GetRolesResponse response, XContentBuilder builder) throws Exception { builder.startObject(); diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/role/RestPutRoleAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/role/RestPutRoleAction.java index 1002c33c99866..fdf8869c98baf 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/role/RestPutRoleAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/role/RestPutRoleAction.java @@ -7,15 +7,15 @@ package org.elasticsearch.xpack.security.rest.action.role; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.security.action.role.PutRoleRequestBuilder; import org.elasticsearch.xpack.core.security.action.role.PutRoleResponse; import org.elasticsearch.xpack.security.rest.action.SecurityBaseRestHandler; @@ -38,10 +38,8 @@ public RestPutRoleAction(Settings settings, XPackLicenseState licenseState) { @Override public List routes() { return List.of( - Route.builder(POST, "/_security/role/{name}") - .replaces(POST, "/_xpack/security/role/{name}", RestApiVersion.V_7).build(), - Route.builder(PUT, "/_security/role/{name}") - .replaces(PUT, "/_xpack/security/role/{name}", RestApiVersion.V_7).build() + Route.builder(POST, "/_security/role/{name}").replaces(POST, "/_xpack/security/role/{name}", RestApiVersion.V_7).build(), + Route.builder(PUT, "/_security/role/{name}").replaces(PUT, "/_xpack/security/role/{name}", RestApiVersion.V_7).build() ); } @@ -52,9 +50,11 @@ public String getName() { @Override public RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClient client) throws IOException { - PutRoleRequestBuilder requestBuilder = new PutRoleRequestBuilder(client) - .source(request.param("name"), request.requiredContent(), request.getXContentType()) - .setRefreshPolicy(request.param("refresh")); + PutRoleRequestBuilder requestBuilder = new PutRoleRequestBuilder(client).source( + request.param("name"), + request.requiredContent(), + request.getXContentType() + ).setRefreshPolicy(request.param("refresh")); return channel -> requestBuilder.execute(new RestBuilderListener<>(channel) { @Override public RestResponse buildResponse(PutRoleResponse putRoleResponse, XContentBuilder builder) throws Exception { diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/rolemapping/RestDeleteRoleMappingAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/rolemapping/RestDeleteRoleMappingAction.java index 2271b43fefa8c..e6381044aa868 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/rolemapping/RestDeleteRoleMappingAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/rolemapping/RestDeleteRoleMappingAction.java @@ -7,15 +7,15 @@ package org.elasticsearch.xpack.security.rest.action.rolemapping; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.security.action.rolemapping.DeleteRoleMappingRequestBuilder; import org.elasticsearch.xpack.core.security.action.rolemapping.DeleteRoleMappingResponse; import org.elasticsearch.xpack.security.rest.action.SecurityBaseRestHandler; @@ -38,7 +38,8 @@ public RestDeleteRoleMappingAction(Settings settings, XPackLicenseState licenseS public List routes() { return List.of( Route.builder(DELETE, "/_security/role_mapping/{name}") - .replaces(DELETE, "/_xpack/security/role_mapping/{name}", RestApiVersion.V_7).build() + .replaces(DELETE, "/_xpack/security/role_mapping/{name}", RestApiVersion.V_7) + .build() ); } @@ -52,14 +53,15 @@ public RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClient c final String name = request.param("name"); final String refresh = request.param("refresh"); - return channel -> new DeleteRoleMappingRequestBuilder(client) - .name(name) + return channel -> new DeleteRoleMappingRequestBuilder(client).name(name) .setRefreshPolicy(refresh) .execute(new RestBuilderListener<>(channel) { @Override public RestResponse buildResponse(DeleteRoleMappingResponse response, XContentBuilder builder) throws Exception { - return new BytesRestResponse(response.isFound() ? RestStatus.OK : RestStatus.NOT_FOUND, - builder.startObject().field("found", response.isFound()).endObject()); + return new BytesRestResponse( + response.isFound() ? RestStatus.OK : RestStatus.NOT_FOUND, + builder.startObject().field("found", response.isFound()).endObject() + ); } }); } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/rolemapping/RestGetRoleMappingsAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/rolemapping/RestGetRoleMappingsAction.java index 954623c93cca0..111b688bf2ca2 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/rolemapping/RestGetRoleMappingsAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/rolemapping/RestGetRoleMappingsAction.java @@ -7,15 +7,15 @@ package org.elasticsearch.xpack.security.rest.action.rolemapping; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.security.action.rolemapping.GetRoleMappingsRequestBuilder; import org.elasticsearch.xpack.core.security.action.rolemapping.GetRoleMappingsResponse; import org.elasticsearch.xpack.core.security.authc.support.mapper.ExpressionRoleMapping; @@ -38,10 +38,10 @@ public RestGetRoleMappingsAction(Settings settings, XPackLicenseState licenseSta @Override public List routes() { return List.of( - Route.builder(GET, "/_security/role_mapping/") - .replaces(GET, "/_xpack/security/role_mapping/", RestApiVersion.V_7).build(), + Route.builder(GET, "/_security/role_mapping/").replaces(GET, "/_xpack/security/role_mapping/", RestApiVersion.V_7).build(), Route.builder(GET, "/_security/role_mapping/{name}") - .replaces(GET, "/_xpack/security/role_mapping/{name}", RestApiVersion.V_7).build() + .replaces(GET, "/_xpack/security/role_mapping/{name}", RestApiVersion.V_7) + .build() ); } @@ -53,24 +53,22 @@ public String getName() { @Override public RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClient client) throws IOException { final String[] names = request.paramAsStringArrayOrEmptyIfAll("name"); - return channel -> new GetRoleMappingsRequestBuilder(client) - .names(names) - .execute(new RestBuilderListener<>(channel) { - @Override - public RestResponse buildResponse(GetRoleMappingsResponse response, XContentBuilder builder) throws Exception { - builder.startObject(); - for (ExpressionRoleMapping mapping : response.mappings()) { - builder.field(mapping.getName(), mapping); - } - builder.endObject(); + return channel -> new GetRoleMappingsRequestBuilder(client).names(names).execute(new RestBuilderListener<>(channel) { + @Override + public RestResponse buildResponse(GetRoleMappingsResponse response, XContentBuilder builder) throws Exception { + builder.startObject(); + for (ExpressionRoleMapping mapping : response.mappings()) { + builder.field(mapping.getName(), mapping); + } + builder.endObject(); - // if the request specified mapping names, but nothing was found then return a 404 result - if (names.length != 0 && response.mappings().length == 0) { - return new BytesRestResponse(RestStatus.NOT_FOUND, builder); - } else { - return new BytesRestResponse(RestStatus.OK, builder); - } + // if the request specified mapping names, but nothing was found then return a 404 result + if (names.length != 0 && response.mappings().length == 0) { + return new BytesRestResponse(RestStatus.NOT_FOUND, builder); + } else { + return new BytesRestResponse(RestStatus.OK, builder); } - }); + } + }); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/rolemapping/RestPutRoleMappingAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/rolemapping/RestPutRoleMappingAction.java index 9cef3c46cda02..a881fa8d0fc92 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/rolemapping/RestPutRoleMappingAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/rolemapping/RestPutRoleMappingAction.java @@ -7,15 +7,15 @@ package org.elasticsearch.xpack.security.rest.action.rolemapping; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.security.action.rolemapping.PutRoleMappingRequestBuilder; import org.elasticsearch.xpack.core.security.action.rolemapping.PutRoleMappingResponse; import org.elasticsearch.xpack.security.rest.action.SecurityBaseRestHandler; @@ -41,9 +41,11 @@ public RestPutRoleMappingAction(Settings settings, XPackLicenseState licenseStat public List routes() { return List.of( Route.builder(POST, "/_security/role_mapping/{name}") - .replaces(POST, "/_xpack/security/role_mapping/{name}", RestApiVersion.V_7).build(), + .replaces(POST, "/_xpack/security/role_mapping/{name}", RestApiVersion.V_7) + .build(), Route.builder(PUT, "/_security/role_mapping/{name}") - .replaces(PUT, "/_xpack/security/role_mapping/{name}", RestApiVersion.V_7).build() + .replaces(PUT, "/_xpack/security/role_mapping/{name}", RestApiVersion.V_7) + .build() ); } @@ -55,15 +57,16 @@ public String getName() { @Override public RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClient client) throws IOException { final String name = request.param("name"); - PutRoleMappingRequestBuilder requestBuilder = new PutRoleMappingRequestBuilder(client) - .source(name, request.requiredContent(), request.getXContentType()) - .setRefreshPolicy(request.param("refresh")); - return channel -> requestBuilder.execute( - new RestBuilderListener<>(channel) { - @Override - public RestResponse buildResponse(PutRoleMappingResponse response, XContentBuilder builder) throws Exception { - return new BytesRestResponse(RestStatus.OK, builder.startObject().field("role_mapping", response).endObject()); - } - }); + PutRoleMappingRequestBuilder requestBuilder = new PutRoleMappingRequestBuilder(client).source( + name, + request.requiredContent(), + request.getXContentType() + ).setRefreshPolicy(request.param("refresh")); + return channel -> requestBuilder.execute(new RestBuilderListener<>(channel) { + @Override + public RestResponse buildResponse(PutRoleMappingResponse response, XContentBuilder builder) throws Exception { + return new BytesRestResponse(RestStatus.OK, builder.startObject().field("role_mapping", response).endObject()); + } + }); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/saml/RestSamlAuthenticateAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/saml/RestSamlAuthenticateAction.java index ab0113522409c..710ba17c4bc46 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/saml/RestSamlAuthenticateAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/saml/RestSamlAuthenticateAction.java @@ -9,13 +9,9 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; -import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.ObjectParser; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; @@ -23,6 +19,10 @@ import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.security.action.saml.SamlAuthenticateRequestBuilder; import org.elasticsearch.xpack.core.security.action.saml.SamlAuthenticateResponse; @@ -52,7 +52,9 @@ void setIds(List ids) { this.ids = ids; } - void setRealm(String realm) { this.realm = realm;} + void setRealm(String realm) { + this.realm = realm; + } } static final ObjectParser PARSER = new ObjectParser<>("saml_authenticate", Input::new); @@ -71,7 +73,8 @@ public RestSamlAuthenticateAction(Settings settings, XPackLicenseState licenseSt public List routes() { return List.of( Route.builder(POST, "/_security/saml/authenticate") - .replaces(POST, "/_xpack/security/saml/authenticate", RestApiVersion.V_7).build() + .replaces(POST, "/_xpack/security/saml/authenticate", RestApiVersion.V_7) + .build() ); } @@ -87,8 +90,9 @@ public RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClient c logger.trace("SAML Authenticate: [{}...] [{}]", Strings.cleanTruncate(input.content, 128), input.ids); return channel -> { final byte[] bytes = decodeBase64(input.content); - final SamlAuthenticateRequestBuilder requestBuilder = - new SamlAuthenticateRequestBuilder(client).saml(bytes).validRequestIds(input.ids).authenticatingRealm(input.realm); + final SamlAuthenticateRequestBuilder requestBuilder = new SamlAuthenticateRequestBuilder(client).saml(bytes) + .validRequestIds(input.ids) + .authenticatingRealm(input.realm); requestBuilder.execute(new RestBuilderListener<>(channel) { @Override public RestResponse buildResponse(SamlAuthenticateResponse response, XContentBuilder builder) throws Exception { @@ -98,7 +102,7 @@ public RestResponse buildResponse(SamlAuthenticateResponse response, XContentBui builder.field("access_token", response.getTokenString()); builder.field("refresh_token", response.getRefreshToken()); builder.field("expires_in", response.getExpiresIn().seconds()); - if(response.getAuthentication() != null) { + if (response.getAuthentication() != null) { builder.field("authentication", response.getAuthentication()); } builder.endObject(); diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/saml/RestSamlCompleteLogoutAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/saml/RestSamlCompleteLogoutAction.java index 007ef4dbb4c29..b82f25c0c49b0 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/saml/RestSamlCompleteLogoutAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/saml/RestSamlCompleteLogoutAction.java @@ -11,18 +11,18 @@ import org.apache.logging.log4j.Logger; import org.elasticsearch.action.ActionResponse; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.ObjectParser; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.security.action.saml.SamlCompleteLogoutAction; import org.elasticsearch.xpack.core.security.action.saml.SamlCompleteLogoutRequest; @@ -39,12 +39,14 @@ * SAMLResponse form parameter, i.e. caller of this API must do the work to extract the SAMLResponse value * from body of the HTTP-Post request. The value must also be URL decoded if necessary. */ -public class RestSamlCompleteLogoutAction extends SamlBaseRestHandler{ +public class RestSamlCompleteLogoutAction extends SamlBaseRestHandler { private static final Logger logger = LogManager.getLogger(RestSamlCompleteLogoutAction.class); - static final ObjectParser - PARSER = new ObjectParser<>("saml_complete_logout", SamlCompleteLogoutRequest::new); + static final ObjectParser PARSER = new ObjectParser<>( + "saml_complete_logout", + SamlCompleteLogoutRequest::new + ); static { PARSER.declareStringOrNull(SamlCompleteLogoutRequest::setQueryString, new ParseField("query_string", "queryString")); @@ -71,18 +73,23 @@ public List routes() { protected RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClient client) throws IOException { try (XContentParser parser = request.contentParser()) { final SamlCompleteLogoutRequest samlCompleteLogoutRequest = PARSER.parse(parser, null); - logger.trace("SAML LogoutResponse: [{}...] [{}...] [{}]", + logger.trace( + "SAML LogoutResponse: [{}...] [{}...] [{}]", Strings.cleanTruncate(samlCompleteLogoutRequest.getQueryString(), 128), Strings.cleanTruncate(samlCompleteLogoutRequest.getContent(), 128), - samlCompleteLogoutRequest.getValidRequestIds()); - return channel -> client.execute(SamlCompleteLogoutAction.INSTANCE, samlCompleteLogoutRequest, + samlCompleteLogoutRequest.getValidRequestIds() + ); + return channel -> client.execute( + SamlCompleteLogoutAction.INSTANCE, + samlCompleteLogoutRequest, new RestBuilderListener<>(channel) { @Override public RestResponse buildResponse(ActionResponse.Empty response, XContentBuilder builder) throws Exception { builder.startObject().endObject(); return new BytesRestResponse(RestStatus.OK, builder); } - }); + } + ); } } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/saml/RestSamlInvalidateSessionAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/saml/RestSamlInvalidateSessionAction.java index 1fc57ab3d593f..847ab2f21a69a 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/saml/RestSamlInvalidateSessionAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/saml/RestSamlInvalidateSessionAction.java @@ -7,18 +7,18 @@ package org.elasticsearch.xpack.security.rest.action.saml; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.ObjectParser; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.security.action.saml.SamlInvalidateSessionAction; import org.elasticsearch.xpack.core.security.action.saml.SamlInvalidateSessionRequest; import org.elasticsearch.xpack.core.security.action.saml.SamlInvalidateSessionResponse; @@ -34,8 +34,10 @@ */ public class RestSamlInvalidateSessionAction extends SamlBaseRestHandler { - static final ObjectParser PARSER = - new ObjectParser<>("saml_invalidate_session", SamlInvalidateSessionRequest::new); + static final ObjectParser PARSER = new ObjectParser<>( + "saml_invalidate_session", + SamlInvalidateSessionRequest::new + ); static { PARSER.declareString(SamlInvalidateSessionRequest::setQueryString, new ParseField("query_string", "queryString")); @@ -50,8 +52,7 @@ public RestSamlInvalidateSessionAction(Settings settings, XPackLicenseState lice @Override public List routes() { return List.of( - Route.builder(POST, "/_security/saml/invalidate") - .replaces(POST, "/_xpack/security/saml/invalidate", RestApiVersion.V_7).build() + Route.builder(POST, "/_security/saml/invalidate").replaces(POST, "/_xpack/security/saml/invalidate", RestApiVersion.V_7).build() ); } @@ -64,7 +65,9 @@ public String getName() { public RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClient client) throws IOException { try (XContentParser parser = request.contentParser()) { final SamlInvalidateSessionRequest invalidateRequest = PARSER.parse(parser, this); - return channel -> client.execute(SamlInvalidateSessionAction.INSTANCE, invalidateRequest, + return channel -> client.execute( + SamlInvalidateSessionAction.INSTANCE, + invalidateRequest, new RestBuilderListener(channel) { @Override public RestResponse buildResponse(SamlInvalidateSessionResponse resp, XContentBuilder builder) throws Exception { @@ -75,7 +78,8 @@ public RestResponse buildResponse(SamlInvalidateSessionResponse resp, XContentBu builder.endObject(); return new BytesRestResponse(RestStatus.OK, builder); } - }); + } + ); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/saml/RestSamlLogoutAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/saml/RestSamlLogoutAction.java index d6701a8cde8c4..805b5384d2335 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/saml/RestSamlLogoutAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/saml/RestSamlLogoutAction.java @@ -7,18 +7,18 @@ package org.elasticsearch.xpack.security.rest.action.saml; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.ObjectParser; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.security.action.saml.SamlLogoutAction; import org.elasticsearch.xpack.core.security.action.saml.SamlLogoutRequest; import org.elasticsearch.xpack.core.security.action.saml.SamlLogoutResponse; @@ -50,8 +50,7 @@ public RestSamlLogoutAction(Settings settings, XPackLicenseState licenseState) { @Override public List routes() { return List.of( - Route.builder(POST, "/_security/saml/logout") - .replaces(POST, "/_xpack/security/saml/logout", RestApiVersion.V_7).build() + Route.builder(POST, "/_security/saml/logout").replaces(POST, "/_xpack/security/saml/logout", RestApiVersion.V_7).build() ); } @@ -64,7 +63,9 @@ public String getName() { public RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClient client) throws IOException { try (XContentParser parser = request.contentParser()) { final SamlLogoutRequest logoutRequest = PARSER.parse(parser, null); - return channel -> client.execute(SamlLogoutAction.INSTANCE, logoutRequest, + return channel -> client.execute( + SamlLogoutAction.INSTANCE, + logoutRequest, new RestBuilderListener(channel) { @Override public RestResponse buildResponse(SamlLogoutResponse response, XContentBuilder builder) throws Exception { @@ -74,7 +75,8 @@ public RestResponse buildResponse(SamlLogoutResponse response, XContentBuilder b builder.endObject(); return new BytesRestResponse(RestStatus.OK, builder); } - }); + } + ); } } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/saml/RestSamlPrepareAuthenticationAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/saml/RestSamlPrepareAuthenticationAction.java index 7655fc101e981..05310ecadb154 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/saml/RestSamlPrepareAuthenticationAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/saml/RestSamlPrepareAuthenticationAction.java @@ -7,18 +7,18 @@ package org.elasticsearch.xpack.security.rest.action.saml; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.ObjectParser; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.security.action.saml.SamlPrepareAuthenticationAction; import org.elasticsearch.xpack.core.security.action.saml.SamlPrepareAuthenticationRequest; import org.elasticsearch.xpack.core.security.action.saml.SamlPrepareAuthenticationResponse; @@ -36,8 +36,10 @@ */ public class RestSamlPrepareAuthenticationAction extends SamlBaseRestHandler { - static final ObjectParser PARSER = new ObjectParser<>("saml_prepare_authn", - SamlPrepareAuthenticationRequest::new); + static final ObjectParser PARSER = new ObjectParser<>( + "saml_prepare_authn", + SamlPrepareAuthenticationRequest::new + ); static { PARSER.declareString(SamlPrepareAuthenticationRequest::setAssertionConsumerServiceURL, new ParseField("acs")); @@ -52,8 +54,7 @@ public RestSamlPrepareAuthenticationAction(Settings settings, XPackLicenseState @Override public List routes() { return List.of( - Route.builder(POST, "/_security/saml/prepare") - .replaces(POST, "/_xpack/security/saml/prepare", RestApiVersion.V_7).build() + Route.builder(POST, "/_security/saml/prepare").replaces(POST, "/_xpack/security/saml/prepare", RestApiVersion.V_7).build() ); } @@ -66,19 +67,22 @@ public String getName() { public RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClient client) throws IOException { try (XContentParser parser = request.contentParser()) { final SamlPrepareAuthenticationRequest authenticationRequest = PARSER.parse(parser, null); - return channel -> client.execute(SamlPrepareAuthenticationAction.INSTANCE, authenticationRequest, - new RestBuilderListener(channel) { - @Override - public RestResponse buildResponse(SamlPrepareAuthenticationResponse response, XContentBuilder builder) - throws Exception { - builder.startObject(); - builder.field("realm", response.getRealmName()); - builder.field("id", response.getRequestId()); - builder.field("redirect", response.getRedirectUrl()); - builder.endObject(); - return new BytesRestResponse(RestStatus.OK, builder); - } - }); + return channel -> client.execute( + SamlPrepareAuthenticationAction.INSTANCE, + authenticationRequest, + new RestBuilderListener(channel) { + @Override + public RestResponse buildResponse(SamlPrepareAuthenticationResponse response, XContentBuilder builder) + throws Exception { + builder.startObject(); + builder.field("realm", response.getRealmName()); + builder.field("id", response.getRequestId()); + builder.field("redirect", response.getRedirectUrl()); + builder.endObject(); + return new BytesRestResponse(RestStatus.OK, builder); + } + } + ); } } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/saml/RestSamlSpMetadataAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/saml/RestSamlSpMetadataAction.java index 1a29806aa535a..5e699f3283c4c 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/saml/RestSamlSpMetadataAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/saml/RestSamlSpMetadataAction.java @@ -9,13 +9,13 @@ import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.security.action.saml.SamlSpMetadataAction; import org.elasticsearch.xpack.core.security.action.saml.SamlSpMetadataRequest; import org.elasticsearch.xpack.core.security.action.saml.SamlSpMetadataResponse; @@ -44,7 +44,9 @@ public String getName() { @Override public RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClient client) throws IOException { final SamlSpMetadataRequest SamlSpMetadataRequest = new SamlSpMetadataRequest(request.param("realm")); - return channel -> client.execute(SamlSpMetadataAction.INSTANCE, SamlSpMetadataRequest, + return channel -> client.execute( + SamlSpMetadataAction.INSTANCE, + SamlSpMetadataRequest, new RestBuilderListener(channel) { @Override public RestResponse buildResponse(SamlSpMetadataResponse response, XContentBuilder builder) throws Exception { @@ -53,6 +55,7 @@ public RestResponse buildResponse(SamlSpMetadataResponse response, XContentBuild builder.endObject(); return new BytesRestResponse(RestStatus.OK, builder); } - }); + } + ); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/service/RestClearServiceAccountTokenStoreCacheAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/service/RestClearServiceAccountTokenStoreCacheAction.java index 8f1b0d58e2b51..7856a73cde6ef 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/service/RestClearServiceAccountTokenStoreCacheAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/service/RestClearServiceAccountTokenStoreCacheAction.java @@ -52,7 +52,7 @@ protected RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClien req.keys(namespace + "/" + service + "/"); } else { final Set qualifiedTokenNames = new HashSet<>(tokenNames.length); - for (String name: tokenNames) { + for (String name : tokenNames) { if (false == Validation.isValidServiceAccountTokenName(name)) { throw new IllegalArgumentException(Validation.formatInvalidServiceTokenNameErrorMessage(name)); } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/service/RestCreateServiceAccountTokenAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/service/RestCreateServiceAccountTokenAction.java index f74e20c98adcc..572b19794a803 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/service/RestCreateServiceAccountTokenAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/service/RestCreateServiceAccountTokenAction.java @@ -36,7 +36,8 @@ public List routes() { return List.of( new Route(POST, "/_security/service/{namespace}/{service}/credential/token/{name}"), new Route(PUT, "/_security/service/{namespace}/{service}/credential/token/{name}"), - new Route(POST, "/_security/service/{namespace}/{service}/credential/token")); + new Route(POST, "/_security/service/{namespace}/{service}/credential/token") + ); } @Override @@ -51,14 +52,19 @@ protected RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClien tokenName = "token_" + UUIDs.base64UUID(); } final CreateServiceAccountTokenRequest createServiceAccountTokenRequest = new CreateServiceAccountTokenRequest( - request.param("namespace"), request.param("service"), tokenName); + request.param("namespace"), + request.param("service"), + tokenName + ); final String refreshPolicy = request.param("refresh"); if (refreshPolicy != null) { createServiceAccountTokenRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.parse(refreshPolicy)); } - return channel -> client.execute(CreateServiceAccountTokenAction.INSTANCE, + return channel -> client.execute( + CreateServiceAccountTokenAction.INSTANCE, createServiceAccountTokenRequest, - new RestToXContentListener<>(channel)); + new RestToXContentListener<>(channel) + ); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/service/RestDeleteServiceAccountTokenAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/service/RestDeleteServiceAccountTokenAction.java index 6f767bd2ce406..921e5fc874688 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/service/RestDeleteServiceAccountTokenAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/service/RestDeleteServiceAccountTokenAction.java @@ -32,8 +32,7 @@ public RestDeleteServiceAccountTokenAction(Settings settings, XPackLicenseState @Override public List routes() { - return List.of( - new Route(DELETE, "/_security/service/{namespace}/{service}/credential/token/{name}")); + return List.of(new Route(DELETE, "/_security/service/{namespace}/{service}/credential/token/{name}")); } @Override @@ -44,17 +43,23 @@ public String getName() { @Override protected RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClient client) throws IOException { final DeleteServiceAccountTokenRequest deleteServiceAccountTokenRequest = new DeleteServiceAccountTokenRequest( - request.param("namespace"), request.param("service"), request.param("name")); + request.param("namespace"), + request.param("service"), + request.param("name") + ); final String refreshPolicy = request.param("refresh"); if (refreshPolicy != null) { deleteServiceAccountTokenRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.parse(refreshPolicy)); } - return channel -> client.execute(DeleteServiceAccountTokenAction.INSTANCE, deleteServiceAccountTokenRequest, + return channel -> client.execute( + DeleteServiceAccountTokenAction.INSTANCE, + deleteServiceAccountTokenRequest, new RestToXContentListener<>(channel) { @Override protected RestStatus getStatus(DeleteServiceAccountTokenResponse response) { return response.found() ? RestStatus.OK : RestStatus.NOT_FOUND; } - }); + } + ); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/service/RestGetServiceAccountAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/service/RestGetServiceAccountAction.java index ff6b249d8822c..b28a56e450549 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/service/RestGetServiceAccountAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/service/RestGetServiceAccountAction.java @@ -46,7 +46,6 @@ protected RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClien final String namespace = request.param("namespace"); final String serviceName = request.param("service"); final GetServiceAccountRequest getServiceAccountRequest = new GetServiceAccountRequest(namespace, serviceName); - return channel -> client.execute(GetServiceAccountAction.INSTANCE, getServiceAccountRequest, - new RestToXContentListener<>(channel)); + return channel -> client.execute(GetServiceAccountAction.INSTANCE, getServiceAccountRequest, new RestToXContentListener<>(channel)); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/service/RestGetServiceAccountCredentialsAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/service/RestGetServiceAccountCredentialsAction.java index 1bcf6602bcbd5..9db1b7bda7ec1 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/service/RestGetServiceAccountCredentialsAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/service/RestGetServiceAccountCredentialsAction.java @@ -29,9 +29,7 @@ public RestGetServiceAccountCredentialsAction(Settings settings, XPackLicenseSta @Override public List routes() { - return List.of( - new Route(GET, "/_security/service/{namespace}/{service}/credential") - ); + return List.of(new Route(GET, "/_security/service/{namespace}/{service}/credential")); } @Override @@ -41,10 +39,14 @@ public String getName() { @Override protected RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClient client) throws IOException { - final GetServiceAccountCredentialsRequest getServiceAccountCredentialsRequest = - new GetServiceAccountCredentialsRequest(request.param("namespace"), request.param("service")); + final GetServiceAccountCredentialsRequest getServiceAccountCredentialsRequest = new GetServiceAccountCredentialsRequest( + request.param("namespace"), + request.param("service") + ); return channel -> client.execute( - GetServiceAccountCredentialsAction.INSTANCE, getServiceAccountCredentialsRequest, - new RestToXContentListener<>(channel)); + GetServiceAccountCredentialsAction.INSTANCE, + getServiceAccountCredentialsRequest, + new RestToXContentListener<>(channel) + ); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/user/RestChangePasswordAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/user/RestChangePasswordAction.java index 46e8b7252a895..35e9b4d7c6436 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/user/RestChangePasswordAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/user/RestChangePasswordAction.java @@ -9,9 +9,8 @@ import org.elasticsearch.action.ActionResponse; import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; @@ -19,6 +18,7 @@ import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.xpack.core.security.SecurityContext; import org.elasticsearch.xpack.core.security.action.user.ChangePasswordRequestBuilder; @@ -48,13 +48,13 @@ public RestChangePasswordAction(Settings settings, SecurityContext securityConte public List routes() { return List.of( Route.builder(PUT, "/_security/user/{username}/_password") - .replaces(PUT, "/_xpack/security/user/{username}/_password", RestApiVersion.V_7).build(), + .replaces(PUT, "/_xpack/security/user/{username}/_password", RestApiVersion.V_7) + .build(), Route.builder(POST, "/_security/user/{username}/_password") - .replaces(POST, "/_xpack/security/user/{username}/_password", RestApiVersion.V_7).build(), - Route.builder(PUT, "/_security/user/_password") - .replaces(PUT, "/_xpack/security/user/_password", RestApiVersion.V_7).build(), - Route.builder(POST, "/_security/user/_password") - .replaces(POST, "/_xpack/security/user/_password", RestApiVersion.V_7).build() + .replaces(POST, "/_xpack/security/user/{username}/_password", RestApiVersion.V_7) + .build(), + Route.builder(PUT, "/_security/user/_password").replaces(PUT, "/_xpack/security/user/_password", RestApiVersion.V_7).build(), + Route.builder(POST, "/_security/user/_password").replaces(POST, "/_xpack/security/user/_password", RestApiVersion.V_7).build() ); } @@ -75,8 +75,7 @@ public RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClient c final String refresh = request.param("refresh"); final BytesReference content = request.requiredContent(); - return channel -> new ChangePasswordRequestBuilder(client) - .username(username) + return channel -> new ChangePasswordRequestBuilder(client).username(username) .source(content, request.getXContentType(), passwordHasher) .setRefreshPolicy(refresh) .execute(new RestBuilderListener<>(channel) { diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/user/RestDeleteUserAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/user/RestDeleteUserAction.java index e1cff5c99f8ce..f1b849a10cb09 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/user/RestDeleteUserAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/user/RestDeleteUserAction.java @@ -7,15 +7,15 @@ package org.elasticsearch.xpack.security.rest.action.user; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.security.action.user.DeleteUserRequestBuilder; import org.elasticsearch.xpack.core.security.action.user.DeleteUserResponse; import org.elasticsearch.xpack.security.rest.action.SecurityBaseRestHandler; @@ -38,7 +38,8 @@ public RestDeleteUserAction(Settings settings, XPackLicenseState licenseState) { public List routes() { return List.of( Route.builder(DELETE, "/_security/user/{username}") - .replaces(DELETE, "/_xpack/security/user/{username}", RestApiVersion.V_7).build() + .replaces(DELETE, "/_xpack/security/user/{username}", RestApiVersion.V_7) + .build() ); } @@ -51,16 +52,15 @@ public String getName() { public RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClient client) throws IOException { final String username = request.param("username"); final String refresh = request.param("refresh"); - return channel -> new DeleteUserRequestBuilder(client) - .username(username) + return channel -> new DeleteUserRequestBuilder(client).username(username) .setRefreshPolicy(refresh) .execute(new RestBuilderListener<>(channel) { @Override public RestResponse buildResponse(DeleteUserResponse response, XContentBuilder builder) throws Exception { - return new BytesRestResponse(response.found() ? RestStatus.OK : RestStatus.NOT_FOUND, - builder.startObject() - .field("found", response.found()) - .endObject()); + return new BytesRestResponse( + response.found() ? RestStatus.OK : RestStatus.NOT_FOUND, + builder.startObject().field("found", response.found()).endObject() + ); } }); } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/user/RestGetUserPrivilegesAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/user/RestGetUserPrivilegesAction.java index 1b36d43f7c17c..d77b5d547b17f 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/user/RestGetUserPrivilegesAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/user/RestGetUserPrivilegesAction.java @@ -8,10 +8,8 @@ import org.elasticsearch.ElasticsearchSecurityException; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestChannel; @@ -19,6 +17,8 @@ import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.security.SecurityContext; import org.elasticsearch.xpack.core.security.action.user.GetUserPrivilegesRequestBuilder; import org.elasticsearch.xpack.core.security.action.user.GetUserPrivilegesResponse; @@ -49,8 +49,7 @@ public RestGetUserPrivilegesAction(Settings settings, SecurityContext securityCo @Override public List routes() { return List.of( - Route.builder(GET, "/_security/user/_privileges") - .replaces(GET, "/_xpack/security/user/_privileges", RestApiVersion.V_7).build() + Route.builder(GET, "/_security/user/_privileges").replaces(GET, "/_xpack/security/user/_privileges", RestApiVersion.V_7).build() ); } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/user/RestGetUsersAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/user/RestGetUsersAction.java index 287c109fbc1b3..83737e99585fe 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/user/RestGetUsersAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/user/RestGetUsersAction.java @@ -8,15 +8,15 @@ import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.common.Strings; -import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.security.action.user.GetUsersRequestBuilder; import org.elasticsearch.xpack.core.security.action.user.GetUsersResponse; import org.elasticsearch.xpack.core.security.user.User; @@ -39,10 +39,8 @@ public RestGetUsersAction(Settings settings, XPackLicenseState licenseState) { @Override public List routes() { return List.of( - Route.builder(GET, "/_security/user/") - .replaces(GET, "/_xpack/security/user/", RestApiVersion.V_7).build(), - Route.builder(GET, "/_security/user/{username}") - .replaces(GET, "/_xpack/security/user/{username}", RestApiVersion.V_7).build() + Route.builder(GET, "/_security/user/").replaces(GET, "/_xpack/security/user/", RestApiVersion.V_7).build(), + Route.builder(GET, "/_security/user/{username}").replaces(GET, "/_xpack/security/user/{username}", RestApiVersion.V_7).build() ); } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/user/RestHasPrivilegesAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/user/RestHasPrivilegesAction.java index 8737dfa11ef36..78b8ffa6c3f92 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/user/RestHasPrivilegesAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/user/RestHasPrivilegesAction.java @@ -9,18 +9,18 @@ import org.elasticsearch.ElasticsearchSecurityException; import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.core.Tuple; -import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.core.RestApiVersion; +import org.elasticsearch.core.Tuple; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.security.SecurityContext; import org.elasticsearch.xpack.core.security.action.user.HasPrivilegesRequestBuilder; import org.elasticsearch.xpack.core.security.action.user.HasPrivilegesResponse; @@ -51,13 +51,17 @@ public RestHasPrivilegesAction(Settings settings, SecurityContext securityContex public List routes() { return List.of( Route.builder(GET, "/_security/user/{username}/_has_privileges") - .replaces(GET, "/_xpack/security/user/{username}/_has_privileges", RestApiVersion.V_7).build(), + .replaces(GET, "/_xpack/security/user/{username}/_has_privileges", RestApiVersion.V_7) + .build(), Route.builder(POST, "/_security/user/{username}/_has_privileges") - .replaces(POST, "/_xpack/security/user/{username}/_has_privileges", RestApiVersion.V_7).build(), + .replaces(POST, "/_xpack/security/user/{username}/_has_privileges", RestApiVersion.V_7) + .build(), Route.builder(GET, "/_security/user/_has_privileges") - .replaces(GET, "/_xpack/security/user/_has_privileges", RestApiVersion.V_7).build(), + .replaces(GET, "/_xpack/security/user/_has_privileges", RestApiVersion.V_7) + .build(), Route.builder(POST, "/_security/user/_has_privileges") - .replaces(POST, "/_xpack/security/user/_has_privileges", RestApiVersion.V_7).build() + .replaces(POST, "/_xpack/security/user/_has_privileges", RestApiVersion.V_7) + .build() ); } @@ -75,9 +79,7 @@ public RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClient c final Tuple content = request.contentOrSourceParam(); final String username = getUsername(request); if (username == null) { - return restChannel -> { - throw new ElasticsearchSecurityException("there is no authenticated user"); - }; + return restChannel -> { throw new ElasticsearchSecurityException("there is no authenticated user"); }; } HasPrivilegesRequestBuilder requestBuilder = new HasPrivilegesRequestBuilder(client).source(username, content.v2(), content.v1()); return channel -> requestBuilder.execute(new RestBuilderListener<>(channel) { diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/user/RestPutUserAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/user/RestPutUserAction.java index e0ab83efedbdf..2dd19243863f3 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/user/RestPutUserAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/user/RestPutUserAction.java @@ -7,10 +7,9 @@ package org.elasticsearch.xpack.security.rest.action.user; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.set.Sets; -import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; @@ -18,6 +17,7 @@ import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.xpack.core.security.action.user.PutUserRequestBuilder; import org.elasticsearch.xpack.core.security.action.user.PutUserResponse; @@ -48,9 +48,9 @@ public RestPutUserAction(Settings settings, XPackLicenseState licenseState) { public List routes() { return List.of( Route.builder(POST, "/_security/user/{username}") - .replaces(POST, "/_xpack/security/user/{username}", RestApiVersion.V_7).build(), - Route.builder(PUT, "/_security/user/{username}") - .replaces(PUT, "/_xpack/security/user/{username}", RestApiVersion.V_7).build() + .replaces(POST, "/_xpack/security/user/{username}", RestApiVersion.V_7) + .build(), + Route.builder(PUT, "/_security/user/{username}").replaces(PUT, "/_xpack/security/user/{username}", RestApiVersion.V_7).build() ); } @@ -61,9 +61,12 @@ public String getName() { @Override public RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClient client) throws IOException { - PutUserRequestBuilder requestBuilder = new PutUserRequestBuilder(client) - .source(request.param("username"), request.requiredContent(), request.getXContentType(), passwordHasher) - .setRefreshPolicy(request.param("refresh")); + PutUserRequestBuilder requestBuilder = new PutUserRequestBuilder(client).source( + request.param("username"), + request.requiredContent(), + request.getXContentType(), + passwordHasher + ).setRefreshPolicy(request.param("refresh")); return channel -> requestBuilder.execute(new RestBuilderListener<>(channel) { @Override diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/user/RestSetEnabledAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/user/RestSetEnabledAction.java index f4ab446183a25..bdbbb46a4f5cc 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/user/RestSetEnabledAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/user/RestSetEnabledAction.java @@ -8,15 +8,15 @@ import org.elasticsearch.action.ActionResponse; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.security.action.user.SetEnabledRequestBuilder; import org.elasticsearch.xpack.security.rest.action.SecurityBaseRestHandler; @@ -40,13 +40,17 @@ public RestSetEnabledAction(Settings settings, XPackLicenseState licenseState) { public List routes() { return List.of( Route.builder(POST, "/_security/user/{username}/_enable") - .replaces(POST, "/_xpack/security/user/{username}/_enable", RestApiVersion.V_7).build(), + .replaces(POST, "/_xpack/security/user/{username}/_enable", RestApiVersion.V_7) + .build(), Route.builder(PUT, "/_security/user/{username}/_enable") - .replaces(PUT, "/_xpack/security/user/{username}/_enable", RestApiVersion.V_7).build(), + .replaces(PUT, "/_xpack/security/user/{username}/_enable", RestApiVersion.V_7) + .build(), Route.builder(POST, "/_security/user/{username}/_disable") - .replaces(POST, "/_xpack/security/user/{username}/_disable", RestApiVersion.V_7).build(), + .replaces(POST, "/_xpack/security/user/{username}/_disable", RestApiVersion.V_7) + .build(), Route.builder(PUT, "/_security/user/{username}/_disable") - .replaces(PUT, "/_xpack/security/user/{username}/_disable", RestApiVersion.V_7).build() + .replaces(PUT, "/_xpack/security/user/{username}/_disable", RestApiVersion.V_7) + .build() ); } @@ -61,8 +65,7 @@ public RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClient c final boolean enabled = request.path().endsWith("_enable"); assert enabled || request.path().endsWith("_disable"); final String username = request.param("username"); - return channel -> new SetEnabledRequestBuilder(client) - .username(username) + return channel -> new SetEnabledRequestBuilder(client).username(username) .enabled(enabled) .execute(new RestBuilderListener<>(channel) { @Override diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/ApiKeyBoolQueryBuilder.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/ApiKeyBoolQueryBuilder.java index e89a9c53f7f82..6c6d8b4b31a16 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/ApiKeyBoolQueryBuilder.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/ApiKeyBoolQueryBuilder.java @@ -30,8 +30,14 @@ public class ApiKeyBoolQueryBuilder extends BoolQueryBuilder { // Field names allowed at the index level - private static final Set ALLOWED_EXACT_INDEX_FIELD_NAMES = - Set.of("_id", "doc_type", "name", "api_key_invalidated", "creation_time", "expiration_time"); + private static final Set ALLOWED_EXACT_INDEX_FIELD_NAMES = Set.of( + "_id", + "doc_type", + "name", + "api_key_invalidated", + "creation_time", + "expiration_time" + ); private ApiKeyBoolQueryBuilder() {} @@ -60,8 +66,7 @@ public static ApiKeyBoolQueryBuilder build(QueryBuilder queryBuilder, @Nullable finalQuery.filter(QueryBuilders.termQuery("doc_type", "api_key")); if (authentication != null) { - finalQuery - .filter(QueryBuilders.termQuery("creator.principal", authentication.getUser().principal())) + finalQuery.filter(QueryBuilders.termQuery("creator.principal", authentication.getUser().principal())) .filter(QueryBuilders.termQuery("creator.realm", ApiKeyService.getCreatorRealmName(authentication))); } return finalQuery; @@ -70,8 +75,9 @@ public static ApiKeyBoolQueryBuilder build(QueryBuilder queryBuilder, @Nullable private static QueryBuilder doProcess(QueryBuilder qb) { if (qb instanceof BoolQueryBuilder) { final BoolQueryBuilder query = (BoolQueryBuilder) qb; - final BoolQueryBuilder newQuery = - QueryBuilders.boolQuery().minimumShouldMatch(query.minimumShouldMatch()).adjustPureNegative(query.adjustPureNegative()); + final BoolQueryBuilder newQuery = QueryBuilders.boolQuery() + .minimumShouldMatch(query.minimumShouldMatch()) + .adjustPureNegative(query.adjustPureNegative()); query.must().stream().map(ApiKeyBoolQueryBuilder::doProcess).forEach(newQuery::must); query.should().stream().map(ApiKeyBoolQueryBuilder::doProcess).forEach(newQuery::should); query.mustNot().stream().map(ApiKeyBoolQueryBuilder::doProcess).forEach(newQuery::mustNot); @@ -127,7 +133,6 @@ private static QueryBuilder doProcess(QueryBuilder qb) { } } - @Override protected Query doToQuery(SearchExecutionContext context) throws IOException { context.setAllowedFields(ApiKeyBoolQueryBuilder::isIndexFieldNameAllowed); diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/ApiKeyFieldNameTranslators.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/ApiKeyFieldNameTranslators.java index 70d064a63f00e..5392399dde201 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/ApiKeyFieldNameTranslators.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/ApiKeyFieldNameTranslators.java @@ -24,7 +24,8 @@ public class ApiKeyFieldNameTranslators { new ExactFieldNameTranslator(s -> "creation_time", "creation"), new ExactFieldNameTranslator(s -> "expiration_time", "expiration"), new ExactFieldNameTranslator(s -> "api_key_invalidated", "invalidated"), - new PrefixFieldNameTranslator(s -> "metadata_flattened" + s.substring(8), "metadata.")); + new PrefixFieldNameTranslator(s -> "metadata_flattened" + s.substring(8), "metadata.") + ); } /** diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/CacheInvalidatorRegistry.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/CacheInvalidatorRegistry.java index 30a360bcd5d33..78bfe9c1d021d 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/CacheInvalidatorRegistry.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/CacheInvalidatorRegistry.java @@ -27,8 +27,7 @@ public class CacheInvalidatorRegistry { private final Map cacheInvalidators = new ConcurrentHashMap<>(); private final Map> cacheAliases = new ConcurrentHashMap<>(); - public CacheInvalidatorRegistry() { - } + public CacheInvalidatorRegistry() {} public void registerCacheInvalidator(String name, CacheInvalidator cacheInvalidator) { if (cacheInvalidators.containsKey(name)) { @@ -55,8 +54,11 @@ public void validate() { } final Set names = cacheAliases.get(alias); if (false == cacheInvalidators.keySet().containsAll(names)) { - throw new IllegalStateException("cache names not found: [" - + Strings.collectionToCommaDelimitedString(Sets.difference(names, cacheInvalidators.keySet())) + "]"); + throw new IllegalStateException( + "cache names not found: [" + + Strings.collectionToCommaDelimitedString(Sets.difference(names, cacheInvalidators.keySet())) + + "]" + ); } } } @@ -66,8 +68,10 @@ public void onSecurityIndexStateChange(SecurityIndexManager.State previousState, || isIndexDeleted(previousState, currentState) || Objects.equals(previousState.indexUUID, currentState.indexUUID) == false || previousState.isIndexUpToDate != currentState.isIndexUpToDate) { - cacheInvalidators.values().stream() - .filter(CacheInvalidator::shouldClearOnSecurityIndexStateChange).forEach(CacheInvalidator::invalidateAll); + cacheInvalidators.values() + .stream() + .filter(CacheInvalidator::shouldClearOnSecurityIndexStateChange) + .forEach(CacheInvalidator::invalidateAll); } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/ExtensionComponents.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/ExtensionComponents.java index 6fc797923ff2c..2ccd20abf079c 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/ExtensionComponents.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/ExtensionComponents.java @@ -26,8 +26,13 @@ public final class ExtensionComponents implements SecurityExtension.SecurityComp private final ResourceWatcherService resourceWatcherService; private final UserRoleMapper roleMapper; - public ExtensionComponents(Environment environment, Client client, ClusterService clusterService, - ResourceWatcherService resourceWatcherService, UserRoleMapper roleMapper) { + public ExtensionComponents( + Environment environment, + Client client, + ClusterService clusterService, + ResourceWatcherService resourceWatcherService, + UserRoleMapper roleMapper + ) { this.environment = environment; this.client = client; this.clusterService = clusterService; diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/FileAttributesChecker.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/FileAttributesChecker.java index 7cc81a932c91d..f7045f5bcb6ca 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/FileAttributesChecker.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/FileAttributesChecker.java @@ -6,6 +6,8 @@ */ package org.elasticsearch.xpack.security.support; +import org.elasticsearch.cli.Terminal; + import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; @@ -13,8 +15,6 @@ import java.nio.file.attribute.PosixFileAttributes; import java.nio.file.attribute.PosixFilePermissions; -import org.elasticsearch.cli.Terminal; - /** * A utility for cli tools to capture file attributes * before writing files, and to warn if the permissions/group/owner changes. @@ -52,21 +52,50 @@ public void check(Terminal terminal) throws IOException { PosixFileAttributes newAttributes = view.readAttributes(); PosixFileAttributes oldAttributes = attributes[i]; if (oldAttributes.permissions().equals(newAttributes.permissions()) == false) { - terminal.errorPrintln(Terminal.Verbosity.SILENT, "WARNING: The file permissions of [" + paths[i] + "] have changed " - + "from [" + PosixFilePermissions.toString(oldAttributes.permissions()) + "] " - + "to [" + PosixFilePermissions.toString(newAttributes.permissions()) + "]"); - terminal.errorPrintln(Terminal.Verbosity.SILENT, - "Please ensure that the user account running Elasticsearch has read access to this file!"); + terminal.errorPrintln( + Terminal.Verbosity.SILENT, + "WARNING: The file permissions of [" + + paths[i] + + "] have changed " + + "from [" + + PosixFilePermissions.toString(oldAttributes.permissions()) + + "] " + + "to [" + + PosixFilePermissions.toString(newAttributes.permissions()) + + "]" + ); + terminal.errorPrintln( + Terminal.Verbosity.SILENT, + "Please ensure that the user account running Elasticsearch has read access to this file!" + ); } if (oldAttributes.owner().getName().equals(newAttributes.owner().getName()) == false) { - terminal.errorPrintln(Terminal.Verbosity.SILENT, "WARNING: Owner of file [" + paths[i] + "] " - + "used to be [" + oldAttributes.owner().getName() + "], " - + "but now is [" + newAttributes.owner().getName() + "]"); + terminal.errorPrintln( + Terminal.Verbosity.SILENT, + "WARNING: Owner of file [" + + paths[i] + + "] " + + "used to be [" + + oldAttributes.owner().getName() + + "], " + + "but now is [" + + newAttributes.owner().getName() + + "]" + ); } if (oldAttributes.group().getName().equals(newAttributes.group().getName()) == false) { - terminal.errorPrintln(Terminal.Verbosity.SILENT, "WARNING: Group of file [" + paths[i] + "] " - + "used to be [" + oldAttributes.group().getName() + "], " - + "but now is [" + newAttributes.group().getName() + "]"); + terminal.errorPrintln( + Terminal.Verbosity.SILENT, + "WARNING: Group of file [" + + paths[i] + + "] " + + "used to be [" + + oldAttributes.group().getName() + + "], " + + "but now is [" + + newAttributes.group().getName() + + "]" + ); } } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/SecurityFiles.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/SecurityFiles.java index 1ca1ce2dac808..81fe8cfe8733a 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/SecurityFiles.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/SecurityFiles.java @@ -30,8 +30,7 @@ public class SecurityFiles { - private SecurityFiles() { - } + private SecurityFiles() {} /** * Atomically writes to the specified file a line per entry in the specified map using the specified transform to convert each entry to @@ -58,8 +57,7 @@ public static void writeFileAtomically(final Path path, final Map m } // get original permissions if (Files.exists(path)) { - boolean supportsPosixAttributes = - Environment.getFileStore(path).supportsFileAttributeView(PosixFileAttributeView.class); + boolean supportsPosixAttributes = Environment.getFileStore(path).supportsFileAttributeView(PosixFileAttributeView.class); if (supportsPosixAttributes) { setPosixAttributesOnTempFile(path, tempFile); } @@ -88,12 +86,10 @@ static void setPosixAttributesOnTempFile(Path path, Path tempFile) throws IOExce // will be notified by the FileAttributeChecker that the ownership has changed and needs to be corrected try { tempFileView.setOwner(attributes.owner()); - } catch (Exception e) { - } + } catch (Exception e) {} try { tempFileView.setGroup(attributes.group()); - } catch (Exception e) { - } + } catch (Exception e) {} } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/SecurityIndexManager.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/SecurityIndexManager.java index ff71d3924e791..c2f3567b74756 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/SecurityIndexManager.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/SecurityIndexManager.java @@ -139,8 +139,10 @@ public ElasticsearchException getUnavailableReason() { if (state.indexState == IndexMetadata.State.CLOSE) { return new IndexClosedException(new Index(state.concreteIndexName, ClusterState.UNKNOWN_UUID)); } else if (state.indexExists()) { - return new UnavailableShardsException(null, - "at least one primary shard for the index [" + state.concreteIndexName + "] is unavailable"); + return new UnavailableShardsException( + null, + "at least one primary shard for the index [" + state.concreteIndexName + "] is unavailable" + ); } else { return new IndexNotFoundException(state.concreteIndexName); } @@ -174,8 +176,8 @@ public void clusterChanged(ClusterChangedEvent event) { final State previousState = state; final IndexMetadata indexMetadata = resolveConcreteIndex(systemIndexDescriptor.getAliasName(), event.state().metadata()); final Instant creationTime = indexMetadata != null ? Instant.ofEpochMilli(indexMetadata.getCreationDate()) : null; - final boolean isIndexUpToDate = indexMetadata == null || - INDEX_FORMAT_SETTING.get(indexMetadata.getSettings()) == systemIndexDescriptor.getIndexFormat(); + final boolean isIndexUpToDate = indexMetadata == null + || INDEX_FORMAT_SETTING.get(indexMetadata.getSettings()) == systemIndexDescriptor.getIndexFormat(); final boolean indexAvailable = checkIndexAvailable(event.state()); final boolean mappingIsUpToDate = indexMetadata == null || checkIndexMappingUpToDate(event.state()); final Version mappingVersion = oldestIndexMappingVersion(event.state()); @@ -198,8 +200,18 @@ public void clusterChanged(ClusterChangedEvent event) { indexHealth = new ClusterIndexHealth(indexMetadata, routingTable).getStatus(); } final String indexUUID = indexMetadata != null ? indexMetadata.getIndexUUID() : null; - final State newState = new State(creationTime, isIndexUpToDate, indexAvailable, mappingIsUpToDate, mappingVersion, - concreteIndexName, indexHealth, indexState, event.state().nodes().getSmallestNonClientNodeVersion(), indexUUID); + final State newState = new State( + creationTime, + isIndexUpToDate, + indexAvailable, + mappingIsUpToDate, + mappingVersion, + concreteIndexName, + indexHealth, + indexState, + event.state().nodes().getSmallestNonClientNodeVersion(), + indexUUID + ); this.state = newState; if (newState.equals(previousState) == false) { @@ -263,8 +275,12 @@ private boolean checkIndexMappingVersionMatches(ClusterState clusterState, Predi return checkIndexMappingVersionMatches(this.systemIndexDescriptor.getAliasName(), clusterState, logger, predicate); } - public static boolean checkIndexMappingVersionMatches(String indexName, ClusterState clusterState, Logger logger, - Predicate predicate) { + public static boolean checkIndexMappingVersionMatches( + String indexName, + ClusterState clusterState, + Logger logger, + Predicate predicate + ) { return loadIndexMappingVersions(indexName, clusterState, logger).stream().allMatch(predicate); } @@ -304,18 +320,15 @@ private static IndexMetadata resolveConcreteIndex(final String indexOrAliasName, private static Version readMappingVersion(String indexName, MappingMetadata mappingMetadata, Logger logger) { try { @SuppressWarnings("unchecked") - Map meta = - (Map) mappingMetadata.sourceAsMap().get("_meta"); + Map meta = (Map) mappingMetadata.sourceAsMap().get("_meta"); if (meta == null) { logger.info("Missing _meta field in mapping [{}] of index [{}]", mappingMetadata.type(), indexName); throw new IllegalStateException("Cannot read security-version string in index " + indexName); } return Version.fromString((String) meta.get(SECURITY_VERSION_STRING)); } catch (ElasticsearchParseException e) { - logger.error(new ParameterizedMessage( - "Cannot parse the mapping for index [{}]", indexName), e); - throw new ElasticsearchException( - "Cannot parse the mapping for index [{}]", e, indexName); + logger.error(new ParameterizedMessage("Cannot parse the mapping for index [{}]", indexName), e); + throw new ElasticsearchException("Cannot parse the mapping for index [{}]", e, indexName); } } @@ -328,9 +341,14 @@ private static Version readMappingVersion(String indexName, MappingMetadata mapp public void checkIndexVersionThenExecute(final Consumer consumer, final Runnable andThen) { final State state = this.state; // use a local copy so all checks execute against the same state! if (state.indexExists() && state.isIndexUpToDate == false) { - consumer.accept(new IllegalStateException( - "Index [" + state.concreteIndexName + "] is not on the current version. Security features relying on the index" - + " will not be available until the upgrade API is run on the index")); + consumer.accept( + new IllegalStateException( + "Index [" + + state.concreteIndexName + + "] is not on the current version. Security features relying on the index" + + " will not be available until the upgrade API is run on the index" + ) + ); } else { andThen.run(); } @@ -348,14 +366,20 @@ public void prepareIndexIfNeededThenExecute(final Consumer consumer, if (state == State.UNRECOVERED_STATE) { throw new ElasticsearchStatusException( "Cluster state has not been recovered yet, cannot write to the [" + state.concreteIndexName + "] index", - RestStatus.SERVICE_UNAVAILABLE); + RestStatus.SERVICE_UNAVAILABLE + ); } else if (state.indexExists() && state.isIndexUpToDate == false) { - throw new IllegalStateException("Index [" + state.concreteIndexName + "] is not on the current version." - + "Security features relying on the index will not be available until the upgrade API is run on the index"); + throw new IllegalStateException( + "Index [" + + state.concreteIndexName + + "] is not on the current version." + + "Security features relying on the index will not be available until the upgrade API is run on the index" + ); } else if (state.indexExists() == false) { assert state.concreteIndexName != null; - final SystemIndexDescriptor descriptorForVersion = - systemIndexDescriptor.getDescriptorCompatibleWith(state.minimumNodeVersion); + final SystemIndexDescriptor descriptorForVersion = systemIndexDescriptor.getDescriptorCompatibleWith( + state.minimumNodeVersion + ); if (descriptorForVersion == null) { final String error = systemIndexDescriptor.getMinimumNodeVersionMessage("create index"); @@ -368,14 +392,16 @@ public void prepareIndexIfNeededThenExecute(final Consumer consumer, ); // Although `TransportCreateIndexAction` is capable of automatically applying the right mappings, settings and aliases // for system indices, we nonetheless specify them here so that the values from `descriptorForVersion` are used. - CreateIndexRequest request = new CreateIndexRequest(state.concreteIndexName) - .origin(descriptorForVersion.getOrigin()) + CreateIndexRequest request = new CreateIndexRequest(state.concreteIndexName).origin(descriptorForVersion.getOrigin()) .mapping(descriptorForVersion.getMappings()) .settings(descriptorForVersion.getSettings()) .alias(new Alias(descriptorForVersion.getAliasName())) .waitForActiveShards(ActiveShardCount.ALL); - executeAsyncWithOrigin(client.threadPool().getThreadContext(), descriptorForVersion.getOrigin(), request, + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + descriptorForVersion.getOrigin(), + request, new ActionListener() { @Override public void onResponse(CreateIndexResponse createIndexResponse) { @@ -397,12 +423,14 @@ public void onFailure(Exception e) { consumer.accept(e); } } - }, client.admin().indices()::create + }, + client.admin().indices()::create ); } } else if (state.mappingUpToDate == false) { - final SystemIndexDescriptor descriptorForVersion = - systemIndexDescriptor.getDescriptorCompatibleWith(state.minimumNodeVersion); + final SystemIndexDescriptor descriptorForVersion = systemIndexDescriptor.getDescriptorCompatibleWith( + state.minimumNodeVersion + ); if (descriptorForVersion == null) { final String error = systemIndexDescriptor.getMinimumNodeVersionMessage("updating mapping"); consumer.accept(new IllegalStateException(error)); @@ -416,14 +444,19 @@ public void onFailure(Exception e) { descriptorForVersion.getMappings(), XContentType.JSON ).origin(descriptorForVersion.getOrigin()); - executeAsyncWithOrigin(client.threadPool().getThreadContext(), descriptorForVersion.getOrigin(), request, + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + descriptorForVersion.getOrigin(), + request, ActionListener.wrap(putMappingResponse -> { if (putMappingResponse.isAcknowledged()) { andThen.run(); } else { consumer.accept(new IllegalStateException("put mapping request was not acknowledged")); } - }, consumer), client.admin().indices()::putMapping); + }, consumer), + client.admin().indices()::putMapping + ); } } else { andThen.run(); @@ -438,7 +471,8 @@ public void onFailure(Exception e) { */ public static boolean isMoveFromRedToNonRed(State previousState, State currentState) { return (previousState.indexHealth == null || previousState.indexHealth == ClusterHealthStatus.RED) - && currentState.indexHealth != null && currentState.indexHealth != ClusterHealthStatus.RED; + && currentState.indexHealth != null + && currentState.indexHealth != ClusterHealthStatus.RED; } /** @@ -464,9 +498,18 @@ public static class State { public final Version minimumNodeVersion; public final String indexUUID; - public State(Instant creationTime, boolean isIndexUpToDate, boolean indexAvailable, - boolean mappingUpToDate, Version mappingVersion, String concreteIndexName, ClusterHealthStatus indexHealth, - IndexMetadata.State indexState, Version minimumNodeVersion, String indexUUID) { + public State( + Instant creationTime, + boolean isIndexUpToDate, + boolean indexAvailable, + boolean mappingUpToDate, + Version mappingVersion, + String concreteIndexName, + ClusterHealthStatus indexHealth, + IndexMetadata.State indexState, + Version minimumNodeVersion, + String indexUUID + ) { this.creationTime = creationTime; this.isIndexUpToDate = isIndexUpToDate; this.indexAvailable = indexAvailable; @@ -484,15 +527,15 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; State state = (State) o; - return Objects.equals(creationTime, state.creationTime) && - isIndexUpToDate == state.isIndexUpToDate && - indexAvailable == state.indexAvailable && - mappingUpToDate == state.mappingUpToDate && - Objects.equals(mappingVersion, state.mappingVersion) && - Objects.equals(concreteIndexName, state.concreteIndexName) && - indexHealth == state.indexHealth && - indexState == state.indexState && - Objects.equals(minimumNodeVersion, state.minimumNodeVersion); + return Objects.equals(creationTime, state.creationTime) + && isIndexUpToDate == state.isIndexUpToDate + && indexAvailable == state.indexAvailable + && mappingUpToDate == state.mappingUpToDate + && Objects.equals(mappingVersion, state.mappingVersion) + && Objects.equals(concreteIndexName, state.concreteIndexName) + && indexHealth == state.indexHealth + && indexState == state.indexState + && Objects.equals(minimumNodeVersion, state.minimumNodeVersion); } public boolean indexExists() { @@ -501,8 +544,16 @@ public boolean indexExists() { @Override public int hashCode() { - return Objects.hash(creationTime, isIndexUpToDate, indexAvailable, mappingUpToDate, mappingVersion, concreteIndexName, - indexHealth, minimumNodeVersion); + return Objects.hash( + creationTime, + isIndexUpToDate, + indexAvailable, + mappingUpToDate, + mappingVersion, + concreteIndexName, + indexHealth, + minimumNodeVersion + ); } } } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/tool/BaseRunAsSuperuserCommand.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/tool/BaseRunAsSuperuserCommand.java index 270d80d0b68f3..22f01d57ee91d 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/tool/BaseRunAsSuperuserCommand.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/tool/BaseRunAsSuperuserCommand.java @@ -68,8 +68,10 @@ public BaseRunAsSuperuserCommand( super(description); this.clientFunction = clientFunction; this.keyStoreFunction = keyStoreFunction; - force = parser.acceptsAll(List.of("f", "force"), - "Use this option to force execution of the command against a cluster that is currently unhealthy."); + force = parser.acceptsAll( + List.of("f", "force"), + "Use this option to force execution of the command against a cluster that is currently unhealthy." + ); } @Override @@ -94,7 +96,7 @@ protected final void execute(Terminal terminal, OptionSet options, Environment e } final String username = generateUsername("autogenerated_", null, 8); - try (SecureString password = new SecureString(generatePassword(PASSWORD_LENGTH))){ + try (SecureString password = new SecureString(generatePassword(PASSWORD_LENGTH))) { final Hasher hasher = Hasher.resolve(XPackSettings.PASSWORD_HASHING_ALGORITHM.get(settings)); final Path passwordFile = FileUserPasswdStore.resolveFile(newEnv); final Path rolesFile = FileUserRolesStore.resolveFile(newEnv); @@ -165,7 +167,7 @@ private void cleanup(Terminal terminal, Environment env, String username) throws FileUserRolesStore.writeFile(userRoles, rolesFile); } } - if ( errorMessages.isEmpty() == false ) { + if (errorMessages.isEmpty() == false) { throw new UserException(ExitCodes.CONFIG, String.join(" , ", errorMessages)); } attributesChecker.check(terminal); @@ -173,7 +175,8 @@ private void cleanup(Terminal terminal, Environment env, String username) throws private void ensureFileRealmEnabled(Settings settings) throws Exception { final Map realms = RealmSettings.getRealmSettings(settings); - Map fileRealmSettings = realms.entrySet().stream() + Map fileRealmSettings = realms.entrySet() + .stream() .filter(e -> e.getKey().getType().equals(FileRealmSettings.TYPE)) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); if (fileRealmSettings.size() == 1) { @@ -190,8 +193,14 @@ private void ensureFileRealmEnabled(Settings settings) throws Exception { * retries as the file realm might not have reloaded the users file yet in order to authenticate our * newly created file realm user. */ - private void checkClusterHealthWithRetries(Environment env, Terminal terminal, String username, SecureString password, int retries, - boolean force) throws Exception { + private void checkClusterHealthWithRetries( + Environment env, + Terminal terminal, + String username, + SecureString password, + int retries, + boolean force + ) throws Exception { CommandLineHttpClient client = clientFunction.apply(env); final URL clusterHealthUrl = CommandLineHttpClient.createURL(new URL(client.getDefaultURL()), "_cluster/health", "?pretty"); final HttpResponse response; @@ -205,10 +214,12 @@ private void checkClusterHealthWithRetries(Environment env, Terminal terminal, S // We try to write the roles file first and then the users one, but theoretically we could have loaded the users // before we have actually loaded the roles so we also retry on 403 ( temp user is found but has no roles ) if ((responseStatus == HttpURLConnection.HTTP_UNAUTHORIZED || responseStatus == HttpURLConnection.HTTP_FORBIDDEN) - && retries > 0 ) { + && retries > 0) { terminal.println( Terminal.Verbosity.VERBOSE, - "Unexpected http status [" + responseStatus + "] while attempting to determine cluster health. Will retry at most " + "Unexpected http status [" + + responseStatus + + "] while attempting to determine cluster health. Will retry at most " + retries + " more times." ); @@ -231,17 +242,25 @@ private void checkClusterHealthWithRetries(Environment env, Terminal terminal, S } else if ("red".equalsIgnoreCase(clusterStatus) && force == false) { terminal.errorPrintln("Failed to determine the health of the cluster. Cluster health is currently RED."); terminal.errorPrintln("This means that some cluster data is unavailable and your cluster is not fully functional."); - terminal.errorPrintln("The cluster logs (https://www.elastic.co/guide/en/elasticsearch/reference/" - + Version.CURRENT.major + "." + Version.CURRENT.minor + "/logging.html)" - + " might contain information/indications for the underlying cause"); terminal.errorPrintln( - "It is recommended that you resolve the issues with your cluster before continuing"); + "The cluster logs (https://www.elastic.co/guide/en/elasticsearch/reference/" + + Version.CURRENT.major + + "." + + Version.CURRENT.minor + + "/logging.html)" + + " might contain information/indications for the underlying cause" + ); + terminal.errorPrintln("It is recommended that you resolve the issues with your cluster before continuing"); terminal.errorPrintln("It is very likely that the command will fail when run against an unhealthy cluster."); terminal.errorPrintln(""); - terminal.errorPrintln("If you still want to attempt to execute this command against an unhealthy cluster," + - " you can pass the `-f` parameter."); - throw new UserException(ExitCodes.UNAVAILABLE, - "Failed to determine the health of the cluster. Cluster health is currently RED."); + terminal.errorPrintln( + "If you still want to attempt to execute this command against an unhealthy cluster," + + " you can pass the `-f` parameter." + ); + throw new UserException( + ExitCodes.UNAVAILABLE, + "Failed to determine the health of the cluster. Cluster health is currently RED." + ); } // else it is yellow or green so we can continue } @@ -260,5 +279,5 @@ protected abstract void executeCommand(Terminal terminal, OptionSet options, Env * implement {@link BaseRunAsSuperuserCommand} can do preflight checks such as parsing and validating options without * the need to go through the process of attempting to create and remove the temporary user unnecessarily. */ - protected abstract void validate(Terminal terminal, OptionSet options, Environment env) throws Exception ; + protected abstract void validate(Terminal terminal, OptionSet options, Environment env) throws Exception; } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/tool/CommandUtils.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/tool/CommandUtils.java index 6f424fa07f820..667da9b2ff742 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/tool/CommandUtils.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/tool/CommandUtils.java @@ -8,6 +8,7 @@ package org.elasticsearch.xpack.security.tool; import org.elasticsearch.core.Nullable; + import java.security.SecureRandom; public class CommandUtils { diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/SSLEngineUtils.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/SSLEngineUtils.java index 09f48146a2747..c63f3f5ace1e0 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/SSLEngineUtils.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/SSLEngineUtils.java @@ -9,6 +9,7 @@ import io.netty.channel.Channel; import io.netty.channel.ChannelException; import io.netty.handler.ssl.SslHandler; + import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.message.ParameterizedMessage; import org.apache.logging.log4j.util.Supplier; @@ -23,11 +24,12 @@ import org.elasticsearch.xpack.security.authc.pki.PkiRealm; import org.elasticsearch.xpack.security.transport.nio.SSLChannelContext; -import javax.net.ssl.SSLEngine; -import javax.net.ssl.SSLPeerUnverifiedException; import java.security.cert.Certificate; import java.security.cert.X509Certificate; +import javax.net.ssl.SSLEngine; +import javax.net.ssl.SSLPeerUnverifiedException; + public class SSLEngineUtils { private SSLEngineUtils() {} @@ -91,8 +93,9 @@ private static void extract(Logger logger, ThreadContext threadContext, SSLEngin assert sslEngine.getWantClientAuth(); if (logger.isTraceEnabled()) { logger.trace( - (Supplier) () -> new ParameterizedMessage( - "SSL Peer did not present a certificate on channel [{}]", channel), e); + (Supplier) () -> new ParameterizedMessage("SSL Peer did not present a certificate on channel [{}]", channel), + e + ); } else if (logger.isDebugEnabled()) { logger.debug("SSL Peer did not present a certificate on channel [{}]", channel); } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/SecurityServerTransportInterceptor.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/SecurityServerTransportInterceptor.java index b3ee239a841c0..a3093c35bb8ed 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/SecurityServerTransportInterceptor.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/SecurityServerTransportInterceptor.java @@ -60,14 +60,16 @@ public class SecurityServerTransportInterceptor implements TransportInterceptor private volatile boolean isStateNotRecovered = true; - public SecurityServerTransportInterceptor(Settings settings, - ThreadPool threadPool, - AuthenticationService authcService, - AuthorizationService authzService, - SSLService sslService, - SecurityContext securityContext, - DestructiveOperations destructiveOperations, - ClusterService clusterService) { + public SecurityServerTransportInterceptor( + Settings settings, + ThreadPool threadPool, + AuthenticationService authcService, + AuthorizationService authzService, + SSLService sslService, + SecurityContext securityContext, + DestructiveOperations destructiveOperations, + ClusterService clusterService + ) { this.settings = settings; this.threadPool = threadPool; this.authcService = authcService; @@ -82,8 +84,13 @@ public SecurityServerTransportInterceptor(Settings settings, public AsyncSender interceptSender(AsyncSender sender) { return new AsyncSender() { @Override - public void sendRequest(Transport.Connection connection, String action, TransportRequest request, - TransportRequestOptions options, TransportResponseHandler handler) { + public void sendRequest( + Transport.Connection connection, + String action, + TransportRequest request, + TransportRequestOptions options, + TransportResponseHandler handler + ) { // the transport in core normally does this check, BUT since we are serializing to a string header we need to do it // ourselves otherwise we wind up using a version newer than what we can actually send final Version minVersion = Version.min(connection.getVersion(), Version.CURRENT); @@ -91,30 +98,60 @@ public void sendRequest(Transport.Connection conne // Sometimes a system action gets executed like a internal create index request or update mappings request // which means that the user is copied over to system actions so we need to change the user if (AuthorizationUtils.shouldReplaceUserWithSystem(threadPool.getThreadContext(), action)) { - securityContext.executeAsUser(SystemUser.INSTANCE, (original) -> sendWithUser(connection, action, request, options, - new ContextRestoreResponseHandler<>(threadPool.getThreadContext().wrapRestorable(original) - , handler), sender), minVersion); + securityContext.executeAsUser( + SystemUser.INSTANCE, + (original) -> sendWithUser( + connection, + action, + request, + options, + new ContextRestoreResponseHandler<>(threadPool.getThreadContext().wrapRestorable(original), handler), + sender + ), + minVersion + ); } else if (AuthorizationUtils.shouldSetUserBasedOnActionOrigin(threadPool.getThreadContext())) { - AuthorizationUtils.switchUserBasedOnActionOriginAndExecute(threadPool.getThreadContext(), securityContext, - (original) -> sendWithUser(connection, action, request, options, - new ContextRestoreResponseHandler<>(threadPool.getThreadContext().wrapRestorable(original) - , handler), sender)); - } else if (securityContext.getAuthentication() != null && - securityContext.getAuthentication().getVersion().equals(minVersion) == false) { - // re-write the authentication since we want the authentication version to match the version of the connection - securityContext.executeAfterRewritingAuthentication(original -> sendWithUser(connection, action, request, options, - new ContextRestoreResponseHandler<>(threadPool.getThreadContext().wrapRestorable(original), handler), sender), - minVersion); - } else { - sendWithUser(connection, action, request, options, handler, sender); - } + AuthorizationUtils.switchUserBasedOnActionOriginAndExecute( + threadPool.getThreadContext(), + securityContext, + (original) -> sendWithUser( + connection, + action, + request, + options, + new ContextRestoreResponseHandler<>(threadPool.getThreadContext().wrapRestorable(original), handler), + sender + ) + ); + } else if (securityContext.getAuthentication() != null + && securityContext.getAuthentication().getVersion().equals(minVersion) == false) { + // re-write the authentication since we want the authentication version to match the version of the connection + securityContext.executeAfterRewritingAuthentication( + original -> sendWithUser( + connection, + action, + request, + options, + new ContextRestoreResponseHandler<>(threadPool.getThreadContext().wrapRestorable(original), handler), + sender + ), + minVersion + ); + } else { + sendWithUser(connection, action, request, options, handler, sender); + } } }; } - private void sendWithUser(Transport.Connection connection, String action, TransportRequest request, - TransportRequestOptions options, TransportResponseHandler handler, - AsyncSender sender) { + private void sendWithUser( + Transport.Connection connection, + String action, + TransportRequest request, + TransportRequestOptions options, + TransportResponseHandler handler, + AsyncSender sender + ) { if (securityContext.getAuthentication() == null) { // we use an assertion here to ensure we catch this in our testing infrastructure, but leave the ISE for cases we do not catch // in tests and may be hit by a user @@ -135,11 +172,22 @@ void assertNoAuthentication(String action) { } @Override - public TransportRequestHandler interceptHandler(String action, String executor, - boolean forceExecution, - TransportRequestHandler actualHandler) { - return new ProfileSecuredRequestHandler<>(logger, action, forceExecution, executor, actualHandler, profileFilters, - settings, threadPool); + public TransportRequestHandler interceptHandler( + String action, + String executor, + boolean forceExecution, + TransportRequestHandler actualHandler + ) { + return new ProfileSecuredRequestHandler<>( + logger, + action, + forceExecution, + executor, + actualHandler, + profileFilters, + settings, + threadPool + ); } private Map initializeProfileFilters(DestructiveOperations destructiveOperations) { @@ -152,8 +200,17 @@ private Map initializeProfileFilters(DestructiveO for (Map.Entry entry : profileConfigurations.entrySet()) { final SslConfiguration profileConfiguration = entry.getValue(); final boolean extractClientCert = transportSSLEnabled && sslService.isSSLClientAuthEnabled(profileConfiguration); - profileFilters.put(entry.getKey(), new ServerTransportFilter(authcService, authzService, threadPool.getThreadContext(), - extractClientCert, destructiveOperations, securityContext)); + profileFilters.put( + entry.getKey(), + new ServerTransportFilter( + authcService, + authzService, + threadPool.getThreadContext(), + extractClientCert, + destructiveOperations, + securityContext + ) + ); } return Collections.unmodifiableMap(profileFilters); @@ -170,9 +227,16 @@ public static class ProfileSecuredRequestHandler imp private final boolean forceExecution; private final Logger logger; - ProfileSecuredRequestHandler(Logger logger, String action, boolean forceExecution, String executorName, - TransportRequestHandler handler, Map profileFilters, - Settings settings, ThreadPool threadPool) { + ProfileSecuredRequestHandler( + Logger logger, + String action, + boolean forceExecution, + String executorName, + TransportRequestHandler handler, + Map profileFilters, + Settings settings, + ThreadPool threadPool + ) { this.logger = logger; this.action = action; this.executorName = executorName; @@ -216,11 +280,16 @@ public void onAfter() { @Override public String toString() { - return "ProfileSecuredRequestHandler{" + - "action='" + action + '\'' + - ", executorName='" + executorName + '\'' + - ", forceExecution=" + forceExecution + - '}'; + return "ProfileSecuredRequestHandler{" + + "action='" + + action + + '\'' + + ", executorName='" + + executorName + + '\'' + + ", forceExecution=" + + forceExecution + + '}'; } @Override diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/ServerTransportFilter.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/ServerTransportFilter.java index 8e5b47ef48486..82655e30e2c6d 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/ServerTransportFilter.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/ServerTransportFilter.java @@ -47,9 +47,14 @@ final class ServerTransportFilter { private final DestructiveOperations destructiveOperations; private final SecurityContext securityContext; - ServerTransportFilter(AuthenticationService authcService, AuthorizationService authzService, - ThreadContext threadContext, boolean extractClientCert, DestructiveOperations destructiveOperations, - SecurityContext securityContext) { + ServerTransportFilter( + AuthenticationService authcService, + AuthorizationService authzService, + ThreadContext threadContext, + boolean extractClientCert, + DestructiveOperations destructiveOperations, + SecurityContext securityContext + ) { this.authcService = authcService; this.authzService = authzService; this.threadContext = threadContext; @@ -63,12 +68,12 @@ final class ServerTransportFilter { * thrown by this method will stop the request from being handled and the error will * be sent back to the sender. */ - void inbound(String action, TransportRequest request, TransportChannel transportChannel,ActionListener listener) { + void inbound(String action, TransportRequest request, TransportChannel transportChannel, ActionListener listener) { if (CloseIndexAction.NAME.equals(action) || OpenIndexAction.NAME.equals(action) || DeleteIndexAction.NAME.equals(action)) { IndicesRequest indicesRequest = (IndicesRequest) request; try { destructiveOperations.failDestructive(indicesRequest.indices()); - } catch(IllegalArgumentException e) { + } catch (IllegalArgumentException e) { listener.onFailure(e); return; } @@ -99,8 +104,7 @@ requests from all the nodes are attached with a user (either a serialize final Version version = transportChannel.getVersion(); authcService.authenticate(securityAction, request, true, ActionListener.wrap((authentication) -> { if (authentication != null) { - if (securityAction.equals(TransportService.HANDSHAKE_ACTION_NAME) && - SystemUser.is(authentication.getUser()) == false) { + if (securityAction.equals(TransportService.HANDSHAKE_ACTION_NAME) && SystemUser.is(authentication.getUser()) == false) { securityContext.executeAsUser(SystemUser.INSTANCE, (ctx) -> { final Authentication replaced = securityContext.getAuthentication(); authzService.authorize(replaced, securityAction, request, listener); diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/filter/IPFilter.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/filter/IPFilter.java index b60e4bbc8417e..870a8a9412e04 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/filter/IPFilter.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/filter/IPFilter.java @@ -6,8 +6,8 @@ */ package org.elasticsearch.xpack.security.transport.filter; - import io.netty.handler.ipfilter.IpFilterRuleType; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.lucene.util.SetOnce; @@ -46,89 +46,108 @@ public class IPFilter { */ public static final String HTTP_PROFILE_NAME = ".http"; - public static final Setting ALLOW_BOUND_ADDRESSES_SETTING = - Setting.boolSetting(setting("filter.always_allow_bound_address"), true, Property.NodeScope); - - public static final Setting IP_FILTER_ENABLED_HTTP_SETTING = Setting.boolSetting(setting("http.filter.enabled"), - true, Property.OperatorDynamic, Property.NodeScope); - - public static final Setting IP_FILTER_ENABLED_SETTING = Setting.boolSetting(setting("transport.filter.enabled"), - true, Property.OperatorDynamic, Property.NodeScope); + public static final Setting ALLOW_BOUND_ADDRESSES_SETTING = Setting.boolSetting( + setting("filter.always_allow_bound_address"), + true, + Property.NodeScope + ); + + public static final Setting IP_FILTER_ENABLED_HTTP_SETTING = Setting.boolSetting( + setting("http.filter.enabled"), + true, + Property.OperatorDynamic, + Property.NodeScope + ); + + public static final Setting IP_FILTER_ENABLED_SETTING = Setting.boolSetting( + setting("transport.filter.enabled"), + true, + Property.OperatorDynamic, + Property.NodeScope + ); private static final IPFilterValidator ALLOW_VALIDATOR = new IPFilterValidator(true); private static final IPFilterValidator DENY_VALIDATOR = new IPFilterValidator(false); public static final Setting> TRANSPORT_FILTER_ALLOW_SETTING = Setting.listSetting( - setting("transport.filter.allow"), - Collections.emptyList(), - Function.identity(), - ALLOW_VALIDATOR, - Property.OperatorDynamic, - Property.NodeScope); + setting("transport.filter.allow"), + Collections.emptyList(), + Function.identity(), + ALLOW_VALIDATOR, + Property.OperatorDynamic, + Property.NodeScope + ); public static final Setting> TRANSPORT_FILTER_DENY_SETTING = Setting.listSetting( - setting("transport.filter.deny"), + setting("transport.filter.deny"), + Collections.emptyList(), + Function.identity(), + DENY_VALIDATOR, + Property.OperatorDynamic, + Property.NodeScope + ); + + public static final Setting.AffixSetting> PROFILE_FILTER_DENY_SETTING = Setting.affixKeySetting( + "transport.profiles.", + "xpack.security.filter.deny", + key -> Setting.listSetting( + key, Collections.emptyList(), Function.identity(), DENY_VALIDATOR, Property.OperatorDynamic, - Property.NodeScope); - - public static final Setting.AffixSetting> PROFILE_FILTER_DENY_SETTING = Setting.affixKeySetting( - "transport.profiles.", - "xpack.security.filter.deny", - key -> Setting.listSetting( - key, - Collections.emptyList(), - Function.identity(), - DENY_VALIDATOR, - Property.OperatorDynamic, - Property.NodeScope)); + Property.NodeScope + ) + ); public static final Setting.AffixSetting> PROFILE_FILTER_ALLOW_SETTING = Setting.affixKeySetting( - "transport.profiles.", - "xpack.security.filter.allow", - key -> Setting.listSetting( - key, - Collections.emptyList(), - Function.identity(), - ALLOW_VALIDATOR, - Property.OperatorDynamic, - Property.NodeScope)); - - private static final Setting> HTTP_FILTER_ALLOW_FALLBACK = Setting.listSetting( - "transport.profiles.default.xpack.security.filter.allow", - TRANSPORT_FILTER_ALLOW_SETTING, - Function.identity(), - TRANSPORT_FILTER_ALLOW_SETTING::get, - ALLOW_VALIDATOR, - Property.NodeScope); - public static final Setting> HTTP_FILTER_ALLOW_SETTING = Setting.listSetting( - setting("http.filter.allow"), - HTTP_FILTER_ALLOW_FALLBACK, + "transport.profiles.", + "xpack.security.filter.allow", + key -> Setting.listSetting( + key, + Collections.emptyList(), Function.identity(), - HTTP_FILTER_ALLOW_FALLBACK::get, ALLOW_VALIDATOR, Property.OperatorDynamic, - Property.NodeScope); + Property.NodeScope + ) + ); + + private static final Setting> HTTP_FILTER_ALLOW_FALLBACK = Setting.listSetting( + "transport.profiles.default.xpack.security.filter.allow", + TRANSPORT_FILTER_ALLOW_SETTING, + Function.identity(), + TRANSPORT_FILTER_ALLOW_SETTING::get, + ALLOW_VALIDATOR, + Property.NodeScope + ); + public static final Setting> HTTP_FILTER_ALLOW_SETTING = Setting.listSetting( + setting("http.filter.allow"), + HTTP_FILTER_ALLOW_FALLBACK, + Function.identity(), + HTTP_FILTER_ALLOW_FALLBACK::get, + ALLOW_VALIDATOR, + Property.OperatorDynamic, + Property.NodeScope + ); private static final Setting> HTTP_FILTER_DENY_FALLBACK = Setting.listSetting( - "transport.profiles.default.xpack.security.filter.deny", - TRANSPORT_FILTER_DENY_SETTING, - Function.identity(), - TRANSPORT_FILTER_DENY_SETTING::get, - DENY_VALIDATOR, - Property.NodeScope); + "transport.profiles.default.xpack.security.filter.deny", + TRANSPORT_FILTER_DENY_SETTING, + Function.identity(), + TRANSPORT_FILTER_DENY_SETTING::get, + DENY_VALIDATOR, + Property.NodeScope + ); public static final Setting> HTTP_FILTER_DENY_SETTING = Setting.listSetting( - setting("http.filter.deny"), - HTTP_FILTER_DENY_FALLBACK, - Function.identity(), - HTTP_FILTER_DENY_FALLBACK::get, - DENY_VALIDATOR, - Property.OperatorDynamic, - Property.NodeScope); + setting("http.filter.deny"), + HTTP_FILTER_DENY_FALLBACK, + Function.identity(), + HTTP_FILTER_DENY_FALLBACK::get, + DENY_VALIDATOR, + Property.OperatorDynamic, + Property.NodeScope + ); - public static final Map DISABLED_USAGE_STATS = Map.of( - "http", false, - "transport", false); + public static final Map DISABLED_USAGE_STATS = Map.of("http", false, "transport", false); public static final SecurityIpFilterRule DEFAULT_PROFILE_ACCEPT_ALL = new SecurityIpFilterRule(true, "default:accept_all") { @@ -163,8 +182,12 @@ public IpFilterRuleType ruleType() { private final Map> profileAllowRules = Collections.synchronizedMap(new HashMap<>()); private final Map> profileDenyRules = Collections.synchronizedMap(new HashMap<>()); - public IPFilter(final Settings settings, AuditTrailService auditTrailService, ClusterSettings clusterSettings, - XPackLicenseState licenseState) { + public IPFilter( + final Settings settings, + AuditTrailService auditTrailService, + ClusterSettings clusterSettings, + XPackLicenseState licenseState + ) { this.auditTrailService = auditTrailService; this.licenseState = licenseState; this.alwaysAllowBoundAddresses = ALLOW_BOUND_ADDRESSES_SETTING.get(settings); @@ -175,8 +198,11 @@ public IPFilter(final Settings settings, AuditTrailService auditTrailService, Cl isHttpFilterEnabled = IP_FILTER_ENABLED_HTTP_SETTING.get(settings); isIpFilterEnabled = IP_FILTER_ENABLED_SETTING.get(settings); - this.profiles = settings.getGroups("transport.profiles.",true).keySet().stream().filter(k -> TransportSettings - .DEFAULT_PROFILE.equals(k) == false).collect(Collectors.toSet()); // exclude default profile -- it's handled differently + this.profiles = settings.getGroups("transport.profiles.", true) + .keySet() + .stream() + .filter(k -> TransportSettings.DEFAULT_PROFILE.equals(k) == false) + .collect(Collectors.toSet()); // exclude default profile -- it's handled differently for (String profile : profiles) { Setting> allowSetting = PROFILE_FILTER_ALLOW_SETTING.getConcreteSettingForNamespace(profile); profileAllowRules.put(profile, allowSetting.get(settings)); @@ -189,16 +215,16 @@ public IPFilter(final Settings settings, AuditTrailService auditTrailService, Cl clusterSettings.addSettingsUpdateConsumer(TRANSPORT_FILTER_DENY_SETTING, this::setTransportDenyFilter); clusterSettings.addSettingsUpdateConsumer(HTTP_FILTER_ALLOW_SETTING, this::setHttpAllowFilter); clusterSettings.addSettingsUpdateConsumer(HTTP_FILTER_DENY_SETTING, this::setHttpDenyFilter); - clusterSettings.addAffixUpdateConsumer(PROFILE_FILTER_ALLOW_SETTING, this::setProfileAllowRules, (a,b) -> {}); - clusterSettings.addAffixUpdateConsumer(PROFILE_FILTER_DENY_SETTING, this::setProfileDenyRules, (a,b) -> {}); + clusterSettings.addAffixUpdateConsumer(PROFILE_FILTER_ALLOW_SETTING, this::setProfileAllowRules, (a, b) -> {}); + clusterSettings.addAffixUpdateConsumer(PROFILE_FILTER_DENY_SETTING, this::setProfileDenyRules, (a, b) -> {}); updateRules(); } public Map usageStats() { Map map = new HashMap<>(2); final boolean httpFilterEnabled = isHttpFilterEnabled && (httpAllowFilter.isEmpty() == false || httpDenyFilter.isEmpty() == false); - final boolean transportFilterEnabled = isIpFilterEnabled && - (transportAllowFilter.isEmpty() == false || transportDenyFilter.isEmpty() == false); + final boolean transportFilterEnabled = isIpFilterEnabled + && (transportAllowFilter.isEmpty() == false || transportDenyFilter.isEmpty() == false); map.put("http", httpFilterEnabled); map.put("transport", transportFilterEnabled); return map; @@ -326,8 +352,10 @@ private SecurityIpFilterRule[] createRules(List allow, List deny return rules.toArray(new SecurityIpFilterRule[rules.size()]); } - public void setBoundTransportAddress(BoundTransportAddress boundTransportAddress, - Map profileBoundAddress) { + public void setBoundTransportAddress( + BoundTransportAddress boundTransportAddress, + Map profileBoundAddress + ) { this.boundTransportAddress.set(boundTransportAddress); this.profileBoundAddress.set(profileBoundAddress); updateRules(); diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/filter/PatternRule.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/filter/PatternRule.java index b60ac85bd0557..771d8e8db1a17 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/filter/PatternRule.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/filter/PatternRule.java @@ -8,8 +8,9 @@ import io.netty.handler.ipfilter.IpFilterRule; import io.netty.handler.ipfilter.IpFilterRuleType; -import org.elasticsearch.core.SuppressForbidden; + import org.elasticsearch.common.network.NetworkAddress; +import org.elasticsearch.core.SuppressForbidden; import java.net.InetAddress; import java.net.InetSocketAddress; @@ -120,7 +121,6 @@ private boolean isLocalhost(InetAddress address) { } } - @Override public boolean matches(InetSocketAddress remoteAddress) { InetAddress inetAddress = remoteAddress.getAddress(); diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/filter/SecurityIpFilterRule.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/filter/SecurityIpFilterRule.java index d3234678928cb..3fa140a4a3dab 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/filter/SecurityIpFilterRule.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/filter/SecurityIpFilterRule.java @@ -9,11 +9,12 @@ import io.netty.handler.ipfilter.IpFilterRule; import io.netty.handler.ipfilter.IpFilterRuleType; import io.netty.handler.ipfilter.IpSubnetFilterRule; + import org.elasticsearch.ElasticsearchException; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.network.InetAddresses; import org.elasticsearch.common.network.NetworkAddress; import org.elasticsearch.common.transport.TransportAddress; +import org.elasticsearch.core.Tuple; import org.elasticsearch.xcontent.ToXContentFragment; import org.elasticsearch.xcontent.XContentBuilder; @@ -87,7 +88,7 @@ static Tuple parseSubnetMask(String address) throws Unknow if (p < 0) { throw new UnknownHostException("Invalid CIDR notation used: " + address); } - if (p == address.length() -1) { + if (p == address.length() - 1) { throw new IllegalArgumentException("address must not end with a '/"); } String addrString = address.substring(0, p); @@ -108,7 +109,6 @@ static Tuple parseSubnetMask(String address) throws Unknow return new Tuple<>(addr, mask); } - /** * Get the Subnet's Netmask in Decimal format.
    * i.e.: getNetMask("255.255.255.0") returns the integer CIDR mask diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/netty4/IpFilterRemoteAddressFilter.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/netty4/IpFilterRemoteAddressFilter.java index 51a7dfc5a72c5..9a3c9c847d131 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/netty4/IpFilterRemoteAddressFilter.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/netty4/IpFilterRemoteAddressFilter.java @@ -9,6 +9,7 @@ import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.ipfilter.AbstractRemoteAddressFilter; + import org.elasticsearch.xpack.security.transport.filter.IPFilter; import java.net.InetSocketAddress; diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/netty4/SecurityNetty4HttpServerTransport.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/netty4/SecurityNetty4HttpServerTransport.java index 36006a7cfae3b..5875f43a5d741 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/netty4/SecurityNetty4HttpServerTransport.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/netty4/SecurityNetty4HttpServerTransport.java @@ -9,6 +9,7 @@ import io.netty.channel.Channel; import io.netty.channel.ChannelHandler; import io.netty.handler.ssl.SslHandler; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.common.network.NetworkService; @@ -16,11 +17,11 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.ssl.SslConfiguration; import org.elasticsearch.common.util.BigArrays; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.http.HttpChannel; import org.elasticsearch.http.netty4.Netty4HttpServerTransport; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.netty4.SharedGroupFactory; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.ssl.SSLService; import org.elasticsearch.xpack.security.transport.SecurityHttpExceptionHandler; import org.elasticsearch.xpack.security.transport.filter.IPFilter; @@ -37,10 +38,18 @@ public class SecurityNetty4HttpServerTransport extends Netty4HttpServerTransport private final SSLService sslService; private final SslConfiguration sslConfiguration; - public SecurityNetty4HttpServerTransport(Settings settings, NetworkService networkService, BigArrays bigArrays, IPFilter ipFilter, - SSLService sslService, ThreadPool threadPool, NamedXContentRegistry xContentRegistry, - Dispatcher dispatcher, ClusterSettings clusterSettings, - SharedGroupFactory sharedGroupFactory) { + public SecurityNetty4HttpServerTransport( + Settings settings, + NetworkService networkService, + BigArrays bigArrays, + IPFilter ipFilter, + SSLService sslService, + ThreadPool threadPool, + NamedXContentRegistry xContentRegistry, + Dispatcher dispatcher, + ClusterSettings clusterSettings, + SharedGroupFactory sharedGroupFactory + ) { super(settings, networkService, bigArrays, threadPool, xContentRegistry, dispatcher, clusterSettings, sharedGroupFactory); this.securityExceptionHandler = new SecurityHttpExceptionHandler(logger, lifecycle, (c, e) -> super.onException(c, e)); this.ipFilter = ipFilter; @@ -49,8 +58,10 @@ public SecurityNetty4HttpServerTransport(Settings settings, NetworkService netwo if (ssl) { this.sslConfiguration = sslService.getHttpTransportSSLConfiguration(); if (sslService.isConfigurationValidForServerUsage(sslConfiguration) == false) { - throw new IllegalArgumentException("a key must be provided to run as a server. the key should be configured using the " + - "[xpack.security.http.ssl.key] or [xpack.security.http.ssl.keystore.path] setting"); + throw new IllegalArgumentException( + "a key must be provided to run as a server. the key should be configured using the " + + "[xpack.security.http.ssl.key] or [xpack.security.http.ssl.keystore.path] setting" + ); } } else { this.sslConfiguration = null; diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/netty4/SecurityNetty4ServerTransport.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/netty4/SecurityNetty4ServerTransport.java index 892f8463b79d7..c93dd93444e17 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/netty4/SecurityNetty4ServerTransport.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/netty4/SecurityNetty4ServerTransport.java @@ -8,13 +8,14 @@ import io.netty.channel.Channel; import io.netty.channel.ChannelHandler; + import org.elasticsearch.Version; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.network.NetworkService; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.ssl.SslConfiguration; import org.elasticsearch.common.util.PageCacheRecycler; +import org.elasticsearch.core.Nullable; import org.elasticsearch.indices.breaker.CircuitBreakerService; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.netty4.SharedGroupFactory; @@ -24,21 +25,32 @@ public class SecurityNetty4ServerTransport extends SecurityNetty4Transport { - @Nullable private final IPFilter authenticator; + @Nullable + private final IPFilter authenticator; public SecurityNetty4ServerTransport( - final Settings settings, - final Version version, - final ThreadPool threadPool, - final NetworkService networkService, - final PageCacheRecycler pageCacheRecycler, - final NamedWriteableRegistry namedWriteableRegistry, - final CircuitBreakerService circuitBreakerService, - @Nullable final IPFilter authenticator, - final SSLService sslService, - final SharedGroupFactory sharedGroupFactory) { - super(settings, version, threadPool, networkService, pageCacheRecycler, namedWriteableRegistry, circuitBreakerService, sslService, - sharedGroupFactory); + final Settings settings, + final Version version, + final ThreadPool threadPool, + final NetworkService networkService, + final PageCacheRecycler pageCacheRecycler, + final NamedWriteableRegistry namedWriteableRegistry, + final CircuitBreakerService circuitBreakerService, + @Nullable final IPFilter authenticator, + final SSLService sslService, + final SharedGroupFactory sharedGroupFactory + ) { + super( + settings, + version, + threadPool, + networkService, + pageCacheRecycler, + namedWriteableRegistry, + circuitBreakerService, + sslService, + sharedGroupFactory + ); this.authenticator = authenticator; } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/nio/SSLChannelContext.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/nio/SSLChannelContext.java index 9a37bbe4adc1a..196b8966dc1cc 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/nio/SSLChannelContext.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/nio/SSLChannelContext.java @@ -8,17 +8,15 @@ import org.elasticsearch.core.TimeValue; import org.elasticsearch.core.internal.io.IOUtils; +import org.elasticsearch.nio.Config; import org.elasticsearch.nio.FlushOperation; import org.elasticsearch.nio.InboundChannelBuffer; import org.elasticsearch.nio.NioChannelHandler; import org.elasticsearch.nio.NioSelector; import org.elasticsearch.nio.NioSocketChannel; import org.elasticsearch.nio.SocketChannelContext; -import org.elasticsearch.nio.Config; import org.elasticsearch.nio.WriteOperation; -import javax.net.ssl.SSLEngine; -import javax.net.ssl.SSLException; import java.io.IOException; import java.nio.channels.ClosedChannelException; import java.util.LinkedList; @@ -26,6 +24,9 @@ import java.util.function.BiConsumer; import java.util.function.Consumer; +import javax.net.ssl.SSLEngine; +import javax.net.ssl.SSLException; + /** * Provides a TLS/SSL read/write layer over a channel. This context will use a {@link SSLDriver} to handshake * with the peer channel. Once the handshake is complete, any data from the peer channel will be decrypted @@ -35,24 +36,44 @@ public final class SSLChannelContext extends SocketChannelContext { private static final long CLOSE_TIMEOUT_NANOS = new TimeValue(10, TimeUnit.SECONDS).nanos(); - private static final Runnable DEFAULT_TIMEOUT_CANCELLER = () -> { - }; + private static final Runnable DEFAULT_TIMEOUT_CANCELLER = () -> {}; private final SSLDriver sslDriver; private final InboundChannelBuffer networkReadBuffer; private final LinkedList encryptedFlushes = new LinkedList<>(); private Runnable closeTimeoutCanceller = DEFAULT_TIMEOUT_CANCELLER; - SSLChannelContext(NioSocketChannel channel, NioSelector selector, Config.Socket socketConfig, - Consumer exceptionHandler, SSLDriver sslDriver, NioChannelHandler readWriteHandler, - InboundChannelBuffer applicationBuffer) { - this(channel, selector, socketConfig, exceptionHandler, sslDriver, readWriteHandler, InboundChannelBuffer.allocatingInstance(), - applicationBuffer); + SSLChannelContext( + NioSocketChannel channel, + NioSelector selector, + Config.Socket socketConfig, + Consumer exceptionHandler, + SSLDriver sslDriver, + NioChannelHandler readWriteHandler, + InboundChannelBuffer applicationBuffer + ) { + this( + channel, + selector, + socketConfig, + exceptionHandler, + sslDriver, + readWriteHandler, + InboundChannelBuffer.allocatingInstance(), + applicationBuffer + ); } - SSLChannelContext(NioSocketChannel channel, NioSelector selector, Config.Socket socketConfig, - Consumer exceptionHandler, SSLDriver sslDriver, NioChannelHandler readWriteHandler, - InboundChannelBuffer networkReadBuffer, InboundChannelBuffer channelBuffer) { + SSLChannelContext( + NioSocketChannel channel, + NioSelector selector, + Config.Socket socketConfig, + Consumer exceptionHandler, + SSLDriver sslDriver, + NioChannelHandler readWriteHandler, + InboundChannelBuffer networkReadBuffer, + InboundChannelBuffer channelBuffer + ) { super(channel, selector, socketConfig, exceptionHandler, readWriteHandler, channelBuffer); this.sslDriver = sslDriver; this.networkReadBuffer = networkReadBuffer; @@ -224,8 +245,7 @@ private boolean pendingChannelFlush() { private static class CloseNotifyOperation implements WriteOperation { - private static final BiConsumer LISTENER = (v, t) -> { - }; + private static final BiConsumer LISTENER = (v, t) -> {}; private static final Object WRITE_OBJECT = new Object(); private final SocketChannelContext channelContext; diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/nio/SSLDriver.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/nio/SSLDriver.java index 78558ae902c40..0cc346766e257 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/nio/SSLDriver.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/nio/SSLDriver.java @@ -12,13 +12,14 @@ import org.elasticsearch.nio.utils.ByteBufferUtils; import org.elasticsearch.nio.utils.ExceptionsHelper; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.function.IntFunction; + import javax.net.ssl.SSLEngine; import javax.net.ssl.SSLEngineResult; import javax.net.ssl.SSLException; import javax.net.ssl.SSLSession; -import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.function.IntFunction; /** * SSLDriver is a class that wraps the {@link SSLEngine} and attempts to simplify the API. The basic usage is @@ -50,7 +51,7 @@ */ public class SSLDriver implements AutoCloseable { - private static final ByteBuffer[] EMPTY_BUFFERS = {ByteBuffer.allocate(0)}; + private static final ByteBuffer[] EMPTY_BUFFERS = { ByteBuffer.allocate(0) }; private static final FlushOperation EMPTY_FLUSH_OPERATION = new FlushOperation(EMPTY_BUFFERS, (r, t) -> {}); private final SSLEngine engine; diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/nio/SecurityNioHttpServerTransport.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/nio/SecurityNioHttpServerTransport.java index ffc5d0a4804be..43b954937c268 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/nio/SecurityNioHttpServerTransport.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/nio/SecurityNioHttpServerTransport.java @@ -14,7 +14,6 @@ import org.elasticsearch.common.ssl.SslConfiguration; import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.common.util.PageCacheRecycler; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.http.nio.HttpReadWriteHandler; import org.elasticsearch.http.nio.NioHttpChannel; import org.elasticsearch.http.nio.NioHttpServerChannel; @@ -30,17 +29,19 @@ import org.elasticsearch.nio.SocketChannelContext; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.nio.NioGroupFactory; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.ssl.SSLService; import org.elasticsearch.xpack.security.transport.SecurityHttpExceptionHandler; import org.elasticsearch.xpack.security.transport.filter.IPFilter; -import javax.net.ssl.SSLEngine; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; import java.util.function.Consumer; +import javax.net.ssl.SSLEngine; + import static org.elasticsearch.xpack.core.XPackSettings.HTTP_SSL_ENABLED; public class SecurityNioHttpServerTransport extends NioHttpServerTransport { @@ -52,13 +53,30 @@ public class SecurityNioHttpServerTransport extends NioHttpServerTransport { private final SslConfiguration sslConfiguration; private final boolean sslEnabled; - public SecurityNioHttpServerTransport(Settings settings, NetworkService networkService, BigArrays bigArrays, - PageCacheRecycler pageCacheRecycler, ThreadPool threadPool, - NamedXContentRegistry xContentRegistry, Dispatcher dispatcher, IPFilter ipFilter, - SSLService sslService, NioGroupFactory nioGroupFactory, - ClusterSettings clusterSettings) { - super(settings, networkService, bigArrays, pageCacheRecycler, threadPool, xContentRegistry, dispatcher, nioGroupFactory, - clusterSettings); + public SecurityNioHttpServerTransport( + Settings settings, + NetworkService networkService, + BigArrays bigArrays, + PageCacheRecycler pageCacheRecycler, + ThreadPool threadPool, + NamedXContentRegistry xContentRegistry, + Dispatcher dispatcher, + IPFilter ipFilter, + SSLService sslService, + NioGroupFactory nioGroupFactory, + ClusterSettings clusterSettings + ) { + super( + settings, + networkService, + bigArrays, + pageCacheRecycler, + threadPool, + xContentRegistry, + dispatcher, + nioGroupFactory, + clusterSettings + ); this.securityExceptionHandler = new SecurityHttpExceptionHandler(logger, lifecycle, (c, e) -> super.onException(c, e)); this.ipFilter = ipFilter; this.sslEnabled = HTTP_SSL_ENABLED.get(settings); @@ -66,8 +84,10 @@ public SecurityNioHttpServerTransport(Settings settings, NetworkService networkS if (sslEnabled) { this.sslConfiguration = sslService.getHttpTransportSSLConfiguration(); if (sslService.isConfigurationValidForServerUsage(sslConfiguration) == false) { - throw new IllegalArgumentException("a key must be provided to run as a server. the key should be configured using the " + - "[xpack.security.http.ssl.key] or [xpack.security.http.ssl.keystore.path] setting"); + throw new IllegalArgumentException( + "a key must be provided to run as a server. the key should be configured using the " + + "[xpack.security.http.ssl.key] or [xpack.security.http.ssl.keystore.path] setting" + ); } } else { this.sslConfiguration = null; @@ -87,15 +107,28 @@ protected SecurityHttpChannelFactory channelFactory() { class SecurityHttpChannelFactory extends ChannelFactory { private SecurityHttpChannelFactory() { - super(tcpNoDelay, tcpKeepAlive, tcpKeepIdle, tcpKeepInterval, tcpKeepCount, reuseAddress, tcpSendBufferSize, - tcpReceiveBufferSize); + super( + tcpNoDelay, + tcpKeepAlive, + tcpKeepIdle, + tcpKeepInterval, + tcpKeepCount, + reuseAddress, + tcpSendBufferSize, + tcpReceiveBufferSize + ); } @Override public NioHttpChannel createChannel(NioSelector selector, SocketChannel channel, Config.Socket socketConfig) throws IOException { NioHttpChannel httpChannel = new NioHttpChannel(channel); - HttpReadWriteHandler httpHandler = new HttpReadWriteHandler(httpChannel,SecurityNioHttpServerTransport.this, - handlingSettings, selector.getTaskScheduler(), threadPool::relativeTimeInNanos); + HttpReadWriteHandler httpHandler = new HttpReadWriteHandler( + httpChannel, + SecurityNioHttpServerTransport.this, + handlingSettings, + selector.getTaskScheduler(), + threadPool::relativeTimeInNanos + ); final NioChannelHandler handler; if (ipFilter != null) { handler = new NioIPFilter(httpHandler, socketConfig.getRemoteAddress(), ipFilter, IPFilter.HTTP_PROFILE_NAME); @@ -119,8 +152,16 @@ public NioHttpChannel createChannel(NioSelector selector, SocketChannel channel, } SSLDriver sslDriver = new SSLDriver(sslEngine, pageAllocator, false); InboundChannelBuffer applicationBuffer = new InboundChannelBuffer(pageAllocator); - context = new SSLChannelContext(httpChannel, selector, socketConfig, exceptionHandler, sslDriver, handler, networkBuffer, - applicationBuffer); + context = new SSLChannelContext( + httpChannel, + selector, + socketConfig, + exceptionHandler, + sslDriver, + handler, + networkBuffer, + applicationBuffer + ); } else { context = new BytesChannelContext(httpChannel, selector, socketConfig, exceptionHandler, handler, networkBuffer); } @@ -130,13 +171,22 @@ public NioHttpChannel createChannel(NioSelector selector, SocketChannel channel, } @Override - public NioHttpServerChannel createServerChannel(NioSelector selector, ServerSocketChannel channel, - Config.ServerSocket socketConfig) { + public NioHttpServerChannel createServerChannel( + NioSelector selector, + ServerSocketChannel channel, + Config.ServerSocket socketConfig + ) { NioHttpServerChannel httpServerChannel = new NioHttpServerChannel(channel); Consumer exceptionHandler = (e) -> onServerException(httpServerChannel, e); Consumer acceptor = SecurityNioHttpServerTransport.this::acceptChannel; - ServerChannelContext context = new ServerChannelContext(httpServerChannel, this, selector, socketConfig, acceptor, - exceptionHandler); + ServerChannelContext context = new ServerChannelContext( + httpServerChannel, + this, + selector, + socketConfig, + acceptor, + exceptionHandler + ); httpServerChannel.setContext(context); return httpServerChannel; diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/nio/SecurityNioTransport.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/nio/SecurityNioTransport.java index eb11c8d4e2964..7f6b44866e2c0 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/nio/SecurityNioTransport.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/nio/SecurityNioTransport.java @@ -10,12 +10,12 @@ import org.apache.logging.log4j.Logger; import org.elasticsearch.Version; import org.elasticsearch.cluster.node.DiscoveryNode; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.network.NetworkService; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.ssl.SslConfiguration; import org.elasticsearch.common.util.PageCacheRecycler; +import org.elasticsearch.core.Nullable; import org.elasticsearch.indices.breaker.CircuitBreakerService; import org.elasticsearch.nio.BytesChannelContext; import org.elasticsearch.nio.Config; @@ -40,9 +40,6 @@ import org.elasticsearch.xpack.core.ssl.SSLService; import org.elasticsearch.xpack.security.transport.filter.IPFilter; -import javax.net.ssl.SNIHostName; -import javax.net.ssl.SSLEngine; -import javax.net.ssl.SSLParameters; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.channels.ServerSocketChannel; @@ -52,6 +49,10 @@ import java.util.function.Consumer; import java.util.function.Function; +import javax.net.ssl.SNIHostName; +import javax.net.ssl.SSLEngine; +import javax.net.ssl.SSLParameters; + import static org.elasticsearch.xpack.core.security.SecurityField.setting; /** @@ -72,12 +73,28 @@ public class SecurityNioTransport extends NioTransport { private final Map profileConfiguration; private final boolean sslEnabled; - public SecurityNioTransport(Settings settings, Version version, ThreadPool threadPool, NetworkService networkService, - PageCacheRecycler pageCacheRecycler, NamedWriteableRegistry namedWriteableRegistry, - CircuitBreakerService circuitBreakerService, @Nullable final IPFilter ipFilter, - SSLService sslService, NioGroupFactory groupFactory) { - super(settings, version, threadPool, networkService, pageCacheRecycler, namedWriteableRegistry, circuitBreakerService, - groupFactory); + public SecurityNioTransport( + Settings settings, + Version version, + ThreadPool threadPool, + NetworkService networkService, + PageCacheRecycler pageCacheRecycler, + NamedWriteableRegistry namedWriteableRegistry, + CircuitBreakerService circuitBreakerService, + @Nullable final IPFilter ipFilter, + SSLService sslService, + NioGroupFactory groupFactory + ) { + super( + settings, + version, + threadPool, + networkService, + pageCacheRecycler, + namedWriteableRegistry, + circuitBreakerService, + groupFactory + ); this.exceptionHandler = new SecurityTransportExceptionHandler(logger, lifecycle, (c, e) -> super.onException(c, e)); this.ipFilter = ipFilter; this.sslService = sslService; @@ -160,8 +177,16 @@ public NioTcpChannel createChannel(NioSelector selector, SocketChannel channel, if (sslEnabled) { SSLDriver sslDriver = new SSLDriver(createSSLEngine(socketConfig), pageAllocator, isClient); InboundChannelBuffer applicationBuffer = new InboundChannelBuffer(pageAllocator); - context = new SSLChannelContext(nioChannel, selector, socketConfig, exceptionHandler, sslDriver, handler, networkBuffer, - applicationBuffer); + context = new SSLChannelContext( + nioChannel, + selector, + socketConfig, + exceptionHandler, + sslDriver, + handler, + networkBuffer, + applicationBuffer + ); } else { context = new BytesChannelContext(nioChannel, selector, socketConfig, exceptionHandler, handler, networkBuffer); } @@ -171,8 +196,11 @@ public NioTcpChannel createChannel(NioSelector selector, SocketChannel channel, } @Override - public NioTcpServerChannel createServerChannel(NioSelector selector, ServerSocketChannel channel, - Config.ServerSocket socketConfig) { + public NioTcpServerChannel createServerChannel( + NioSelector selector, + ServerSocketChannel channel, + Config.ServerSocket socketConfig + ) { NioTcpServerChannel nioChannel = new NioTcpServerChannel(channel); Consumer exceptionHandler = (e) -> onServerException(nioChannel, e); Consumer acceptor = SecurityNioTransport.this::acceptChannel; @@ -207,8 +235,11 @@ private SecurityClientTcpChannelFactory(ProfileSettings profileSettings, SNIHost } @Override - public NioTcpServerChannel createServerChannel(NioSelector selector, ServerSocketChannel channel, - Config.ServerSocket socketConfig) { + public NioTcpServerChannel createServerChannel( + NioSelector selector, + ServerSocketChannel channel, + Config.ServerSocket socketConfig + ) { throw new AssertionError("Cannot create TcpServerChannel with client factory"); } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/test/SecurityIntegTestCase.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/test/SecurityIntegTestCase.java index a42bf04e1785e..2efb88e5a185c 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/test/SecurityIntegTestCase.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/test/SecurityIntegTestCase.java @@ -8,6 +8,7 @@ import io.netty.util.ThreadDeathWatcher; import io.netty.util.concurrent.GlobalEventExecutor; + import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse; import org.elasticsearch.action.admin.cluster.node.info.NodeInfo; import org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse; @@ -28,12 +29,12 @@ import org.elasticsearch.common.settings.MockSecureSettings; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.gateway.GatewayService; import org.elasticsearch.index.Index; import org.elasticsearch.license.LicenseService; import org.elasticsearch.plugins.Plugin; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.security.authc.support.Hasher; import org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken; import org.elasticsearch.xpack.security.LocalStateSecurity; @@ -86,15 +87,14 @@ public static void generateBootstrapPassword() { BOOTSTRAP_PASSWORD = TEST_PASSWORD_SECURE_STRING.clone(); } - //UnicastZen requires the number of nodes in a cluster to generate the unicast configuration. - //The number of nodes is randomized though, but we can predict what the maximum number of nodes will be - //and configure them all in unicast.hosts + // UnicastZen requires the number of nodes in a cluster to generate the unicast configuration. + // The number of nodes is randomized though, but we can predict what the maximum number of nodes will be + // and configure them all in unicast.hosts protected static int defaultMaxNumberOfNodes() { ClusterScope clusterScope = SecurityIntegTestCase.class.getAnnotation(ClusterScope.class); if (clusterScope == null) { - return InternalTestCluster.DEFAULT_HIGH_NUM_MASTER_NODES + - InternalTestCluster.DEFAULT_MAX_NUM_DATA_NODES + - InternalTestCluster.DEFAULT_MAX_NUM_CLIENT_NODES; + return InternalTestCluster.DEFAULT_HIGH_NUM_MASTER_NODES + InternalTestCluster.DEFAULT_MAX_NUM_DATA_NODES + + InternalTestCluster.DEFAULT_MAX_NUM_CLIENT_NODES; } else { int clientNodes = clusterScope.numClientNodes(); if (clientNodes < 0) { @@ -142,8 +142,7 @@ private static Scope getCurrentClusterScope(Class clazz) { @BeforeClass public static void initDefaultSettings() { if (SECURITY_DEFAULT_SETTINGS == null) { - SECURITY_DEFAULT_SETTINGS = - new SecuritySettingsSource(randomBoolean(), createTempDir(), Scope.SUITE); + SECURITY_DEFAULT_SETTINGS = new SecuritySettingsSource(randomBoolean(), createTempDir(), Scope.SUITE); } } @@ -159,7 +158,7 @@ public static void destroyDefaultSettings() { } @Rule - //Rules are the only way to have something run before the before (final) method inherited from ESIntegTestCase + // Rules are the only way to have something run before the before (final) method inherited from ESIntegTestCase public ExternalResource externalResource = new ExternalResource() { @Override protected void before() throws Throwable { @@ -167,13 +166,19 @@ protected void before() throws Throwable { switch (currentClusterScope) { case SUITE: if (customSecuritySettingsSource == null) { - customSecuritySettingsSource = - new CustomSecuritySettingsSource(transportSSLEnabled(), createTempDir(), currentClusterScope); + customSecuritySettingsSource = new CustomSecuritySettingsSource( + transportSSLEnabled(), + createTempDir(), + currentClusterScope + ); } break; case TEST: - customSecuritySettingsSource = - new CustomSecuritySettingsSource(transportSSLEnabled(), createTempDir(), currentClusterScope); + customSecuritySettingsSource = new CustomSecuritySettingsSource( + transportSSLEnabled(), + createTempDir(), + currentClusterScope + ); break; } } @@ -209,7 +214,7 @@ protected void after() { }; @Before - //before methods from the superclass are run before this, which means that the current cluster is ready to go + // before methods from the superclass are run before this, which means that the current cluster is ready to go public void assertXPackIsInstalled() { doAssertXPackIsInstalled(); } @@ -218,11 +223,17 @@ protected void doAssertXPackIsInstalled() { NodesInfoResponse nodeInfos = client().admin().cluster().prepareNodesInfo().clear().setPlugins(true).get(); for (NodeInfo nodeInfo : nodeInfos.getNodes()) { // TODO: disable this assertion for now, due to random runs with mock plugins. perhaps run without mock plugins? -// assertThat(nodeInfo.getPlugins().getInfos(), hasSize(2)); - Collection pluginNames = - nodeInfo.getInfo(PluginsAndModules.class).getPluginInfos().stream().map(p -> p.getClassname()).collect(Collectors.toList()); - assertThat("plugin [" + LocalStateSecurity.class.getName() + "] not found in [" + pluginNames + "]", pluginNames, - hasItem(LocalStateSecurity.class.getName())); + // assertThat(nodeInfo.getPlugins().getInfos(), hasSize(2)); + Collection pluginNames = nodeInfo.getInfo(PluginsAndModules.class) + .getPluginInfos() + .stream() + .map(p -> p.getClassname()) + .collect(Collectors.toList()); + assertThat( + "plugin [" + LocalStateSecurity.class.getName() + "] not found in [" + pluginNames + "]", + pluginNames, + hasItem(LocalStateSecurity.class.getName()) + ); } } @@ -230,14 +241,16 @@ protected void doAssertXPackIsInstalled() { protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { Settings.Builder builder = Settings.builder().put(super.nodeSettings(nodeOrdinal, otherSettings)); // Disable native ML autodetect_process as the c++ controller won't be available -// builder.put(MachineLearningField.AUTODETECT_PROCESS.getKey(), false); + // builder.put(MachineLearningField.AUTODETECT_PROCESS.getKey(), false); Settings customSettings = customSecuritySettingsSource.nodeSettings(nodeOrdinal, otherSettings); builder.put(customSettings, false); // handle secure settings separately builder.put(LicenseService.SELF_GENERATED_LICENSE_TYPE.getKey(), "trial"); Settings.Builder customBuilder = Settings.builder().put(customSettings); if (customBuilder.getSecureSettings() != null) { - SecuritySettingsSource.addSecureSettings(builder, secureSettings -> - secureSettings.merge((MockSecureSettings) customBuilder.getSecureSettings())); + SecuritySettingsSource.addSecureSettings( + builder, + secureSettings -> secureSettings.merge((MockSecureSettings) customBuilder.getSecureSettings()) + ); } if (builder.getSecureSettings() == null) { builder.setSecureSettings(new MockSecureSettings()); @@ -373,7 +386,7 @@ protected void createIndicesWithRandomAliases(String... indices) { IndicesAliasesRequestBuilder builder = client().admin().indices().prepareAliases(); for (String index : indices) { if (frequently()) { - //one alias per index with prefix "alias-" + // one alias per index with prefix "alias-" builder.addAlias(index, "alias-" + index); aliasAdded = true; } @@ -381,7 +394,7 @@ protected void createIndicesWithRandomAliases(String... indices) { // If we get to this point and we haven't added an alias to the request we need to add one // or the request will fail so use noAliasAdded to force adding the alias in this case if (aliasAdded == false || randomBoolean()) { - //one alias pointing to all indices + // one alias pointing to all indices for (String index : indices) { builder.addAlias(index, "alias"); } @@ -397,8 +410,10 @@ protected void createIndicesWithRandomAliases(String... indices) { @Override protected Function getClientWrapper() { - Map headers = Collections.singletonMap("Authorization", - basicAuthHeaderValue(nodeClientUsername(), nodeClientPassword())); + Map headers = Collections.singletonMap( + "Authorization", + basicAuthHeaderValue(nodeClientUsername(), nodeClientPassword()) + ); // we need to wrap node clients because we do not specify a user for nodes and all requests will use the system // user. This is ok for internal n2n stuff but the test framework does other things like wiping indices, repositories, etc // that the system user cannot do. so we wrap the node client with a user that can do these things since the client() calls @@ -428,9 +443,15 @@ public void assertSecurityIndexActive(TestCluster testCluster) throws Exception } protected void deleteSecurityIndex() { - final Client client = client().filterWithHeader(Collections.singletonMap("Authorization", - UsernamePasswordToken.basicAuthHeaderValue(SecuritySettingsSource.TEST_SUPERUSER, - SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING))); + final Client client = client().filterWithHeader( + Collections.singletonMap( + "Authorization", + UsernamePasswordToken.basicAuthHeaderValue( + SecuritySettingsSource.TEST_SUPERUSER, + SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING + ) + ) + ); GetIndexRequest getIndexRequest = new GetIndexRequest(); getIndexRequest.indices(SECURITY_MAIN_ALIAS); getIndexRequest.indicesOptions(IndicesOptions.lenientExpandOpen()); @@ -455,7 +476,8 @@ protected boolean isTransportSSLEnabled() { } public static Hasher getFastStoredHashAlgoForTests() { - return inFipsJvm() ? Hasher.resolve(randomFrom("pbkdf2", "pbkdf2_1000", "pbkdf2_stretch_1000", "pbkdf2_stretch")) + return inFipsJvm() + ? Hasher.resolve(randomFrom("pbkdf2", "pbkdf2_1000", "pbkdf2_stretch_1000", "pbkdf2_stretch")) : Hasher.resolve(randomFrom("pbkdf2", "pbkdf2_1000", "pbkdf2_stretch_1000", "pbkdf2_stretch", "bcrypt", "bcrypt9")); } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/test/SecuritySettingsSource.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/test/SecuritySettingsSource.java index 42ce468008a41..aba9309274eb0 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/test/SecuritySettingsSource.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/test/SecuritySettingsSource.java @@ -16,8 +16,8 @@ import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.env.Environment; -import org.elasticsearch.reindex.ReindexPlugin; import org.elasticsearch.plugins.Plugin; +import org.elasticsearch.reindex.ReindexPlugin; import org.elasticsearch.test.ESIntegTestCase.Scope; import org.elasticsearch.transport.netty4.Netty4Plugin; import org.elasticsearch.xpack.core.XPackSettings; @@ -58,35 +58,53 @@ public class SecuritySettingsSource extends NodeConfigurationSource { public static final String TEST_USER_NAME = "test_user"; public static final Hasher HASHER = getFastStoredHashAlgoForTests(); - public static final String TEST_PASSWORD_HASHED = - new String(HASHER.hash(new SecureString(TEST_PASSWORD.toCharArray()))); + public static final String TEST_PASSWORD_HASHED = new String(HASHER.hash(new SecureString(TEST_PASSWORD.toCharArray()))); public static final String TEST_ROLE = "user"; public static final String TEST_SUPERUSER = "test_superuser"; public static final RequestOptions SECURITY_REQUEST_OPTIONS = RequestOptions.DEFAULT.toBuilder() - .addHeader("Authorization", - "Basic " + Base64.getEncoder().encodeToString((TEST_USER_NAME + ":" + TEST_PASSWORD).getBytes(StandardCharsets.UTF_8))) + .addHeader( + "Authorization", + "Basic " + Base64.getEncoder().encodeToString((TEST_USER_NAME + ":" + TEST_PASSWORD).getBytes(StandardCharsets.UTF_8)) + ) .build(); public static final String DEFAULT_TRANSPORT_CLIENT_ROLE = "transport_client"; public static final String DEFAULT_TRANSPORT_CLIENT_USER_NAME = "test_trans_client_user"; - public static final String CONFIG_STANDARD_USER = - TEST_USER_NAME + ":" + TEST_PASSWORD_HASHED + "\n" + - DEFAULT_TRANSPORT_CLIENT_USER_NAME + ":" + TEST_PASSWORD_HASHED + "\n" + - TEST_SUPERUSER + ":" + TEST_PASSWORD_HASHED + "\n"; - - public static final String CONFIG_STANDARD_USER_ROLES = - TEST_ROLE + ":" + TEST_USER_NAME + "," + DEFAULT_TRANSPORT_CLIENT_USER_NAME + "\n" + - DEFAULT_TRANSPORT_CLIENT_ROLE + ":" + DEFAULT_TRANSPORT_CLIENT_USER_NAME + "\n" + - "superuser:" + TEST_SUPERUSER + "\n"; - - public static final String CONFIG_ROLE_ALLOW_ALL = - TEST_ROLE + ":\n" + - " cluster: [ ALL ]\n" + - " indices:\n" + - " - names: '*'\n" + - " allow_restricted_indices: true\n" + - " privileges: [ ALL ]\n"; + public static final String CONFIG_STANDARD_USER = TEST_USER_NAME + + ":" + + TEST_PASSWORD_HASHED + + "\n" + + DEFAULT_TRANSPORT_CLIENT_USER_NAME + + ":" + + TEST_PASSWORD_HASHED + + "\n" + + TEST_SUPERUSER + + ":" + + TEST_PASSWORD_HASHED + + "\n"; + + public static final String CONFIG_STANDARD_USER_ROLES = TEST_ROLE + + ":" + + TEST_USER_NAME + + "," + + DEFAULT_TRANSPORT_CLIENT_USER_NAME + + "\n" + + DEFAULT_TRANSPORT_CLIENT_ROLE + + ":" + + DEFAULT_TRANSPORT_CLIENT_USER_NAME + + "\n" + + "superuser:" + + TEST_SUPERUSER + + "\n"; + + public static final String CONFIG_ROLE_ALLOW_ALL = TEST_ROLE + + ":\n" + + " cluster: [ ALL ]\n" + + " indices:\n" + + " - names: '*'\n" + + " allow_restricted_indices: true\n" + + " privileges: [ ALL ]\n"; private final Path parentFolder; private final String subfolderPrefix; @@ -134,20 +152,20 @@ public Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { writeFile(xpackConf, "service_tokens", configServiceTokens()); Settings.Builder builder = Settings.builder() - .put(Environment.PATH_HOME_SETTING.getKey(), home) - .put(XPackSettings.SECURITY_ENABLED.getKey(), true) - .put(NetworkModule.TRANSPORT_TYPE_KEY, randomBoolean() ? SecurityField.NAME4 : SecurityField.NIO) - .put(NetworkModule.HTTP_TYPE_KEY, randomBoolean() ? SecurityField.NAME4 : SecurityField.NIO) - //TODO: for now isolate security tests from watcher (randomize this later) - .put(XPackSettings.WATCHER_ENABLED.getKey(), false) - .put(XPackSettings.AUDIT_ENABLED.getKey(), randomBoolean()) - .put(LoggingAuditTrail.EMIT_HOST_ADDRESS_SETTING.getKey(), randomBoolean()) - .put(LoggingAuditTrail.EMIT_HOST_NAME_SETTING.getKey(), randomBoolean()) - .put(LoggingAuditTrail.EMIT_NODE_NAME_SETTING.getKey(), randomBoolean()) - .put(LoggingAuditTrail.EMIT_NODE_ID_SETTING.getKey(), randomBoolean()) - .put("xpack.security.authc.realms." + FileRealmSettings.TYPE + ".file.order", 0) - .put("xpack.security.authc.realms." + NativeRealmSettings.TYPE + ".index.order", "1") - .put("xpack.license.self_generated.type", "trial"); + .put(Environment.PATH_HOME_SETTING.getKey(), home) + .put(XPackSettings.SECURITY_ENABLED.getKey(), true) + .put(NetworkModule.TRANSPORT_TYPE_KEY, randomBoolean() ? SecurityField.NAME4 : SecurityField.NIO) + .put(NetworkModule.HTTP_TYPE_KEY, randomBoolean() ? SecurityField.NAME4 : SecurityField.NIO) + // TODO: for now isolate security tests from watcher (randomize this later) + .put(XPackSettings.WATCHER_ENABLED.getKey(), false) + .put(XPackSettings.AUDIT_ENABLED.getKey(), randomBoolean()) + .put(LoggingAuditTrail.EMIT_HOST_ADDRESS_SETTING.getKey(), randomBoolean()) + .put(LoggingAuditTrail.EMIT_HOST_NAME_SETTING.getKey(), randomBoolean()) + .put(LoggingAuditTrail.EMIT_NODE_NAME_SETTING.getKey(), randomBoolean()) + .put(LoggingAuditTrail.EMIT_NODE_ID_SETTING.getKey(), randomBoolean()) + .put("xpack.security.authc.realms." + FileRealmSettings.TYPE + ".file.order", 0) + .put("xpack.security.authc.realms." + NativeRealmSettings.TYPE + ".index.order", "1") + .put("xpack.license.self_generated.type", "trial"); addNodeSSLSettings(builder); return builder.build(); } @@ -163,11 +181,15 @@ protected void addDefaultSecurityTransportType(Settings.Builder builder, Setting } } - @Override public Collection> nodePlugins() { - return Arrays.asList(LocalStateSecurity.class, Netty4Plugin.class, ReindexPlugin.class, CommonAnalysisPlugin.class, - InternalSettingsPlugin.class); + return Arrays.asList( + LocalStateSecurity.class, + Netty4Plugin.class, + ReindexPlugin.class, + CommonAnalysisPlugin.class, + InternalSettingsPlugin.class + ); } protected String configUsers() { @@ -200,16 +222,22 @@ protected SecureString nodeClientPassword() { } public static void addSSLSettingsForNodePEMFiles(Settings.Builder builder, String prefix, boolean hostnameVerificationEnabled) { - addSSLSettingsForPEMFiles(builder, prefix, - "/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.pem", "testnode", + addSSLSettingsForPEMFiles( + builder, + prefix, + "/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.pem", + "testnode", "/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt", - Arrays.asList("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode-client-profile.crt", + Arrays.asList( + "/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode-client-profile.crt", "/org/elasticsearch/xpack/security/transport/ssl/certs/simple/active-directory-ca.crt", "/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testclient.crt", "/org/elasticsearch/xpack/security/transport/ssl/certs/simple/openldap.crt", "/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt", - "/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode_ec.crt"), - hostnameVerificationEnabled); + "/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode_ec.crt" + ), + hostnameVerificationEnabled + ); } private void addNodeSSLSettings(Settings.Builder builder) { @@ -218,9 +246,13 @@ private void addNodeSSLSettings(Settings.Builder builder) { if (usePEM) { addSSLSettingsForNodePEMFiles(builder, "xpack.security.transport.", hostnameVerificationEnabled); } else { - addSSLSettingsForStore(builder, "xpack.security.transport.", - "/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.jks", "testnode", - hostnameVerificationEnabled); + addSSLSettingsForStore( + builder, + "xpack.security.transport.", + "/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.jks", + "testnode", + hostnameVerificationEnabled + ); } } else if (randomBoolean()) { builder.put(XPackSettings.TRANSPORT_SSL_ENABLED.getKey(), false); @@ -230,16 +262,27 @@ private void addNodeSSLSettings(Settings.Builder builder) { public void addClientSSLSettings(Settings.Builder builder, String prefix) { builder.put("xpack.security.transport.ssl.enabled", sslEnabled); if (usePEM) { - addSSLSettingsForPEMFiles(builder, prefix, - "/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testclient.pem", "testclient", + addSSLSettingsForPEMFiles( + builder, + prefix, + "/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testclient.pem", + "testclient", "/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testclient.crt", - Arrays.asList("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt", + Arrays.asList( + "/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt", "/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode_ec.crt", - "/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testclient.crt"), - hostnameVerificationEnabled); + "/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testclient.crt" + ), + hostnameVerificationEnabled + ); } else { - addSSLSettingsForStore(builder, prefix, "/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testclient.jks", - "testclient", hostnameVerificationEnabled); + addSSLSettingsForStore( + builder, + prefix, + "/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testclient.jks", + "testclient", + hostnameVerificationEnabled + ); } } @@ -253,19 +296,25 @@ public static void addSSLSettingsForStore(Settings.Builder builder, String resou addSSLSettingsForStore(builder, prefix, resourcePathToStore, password, true); } - private static void addSSLSettingsForStore(Settings.Builder builder, String prefix, String resourcePathToStore, String password, - boolean hostnameVerificationEnabled) { + private static void addSSLSettingsForStore( + Settings.Builder builder, + String prefix, + String resourcePathToStore, + String password, + boolean hostnameVerificationEnabled + ) { Path store = resolveResourcePath(resourcePathToStore); builder.put(prefix + "ssl.verification_mode", hostnameVerificationEnabled ? "full" : "certificate"); builder.put(prefix + "ssl.keystore.path", store); final String finalPrefix = prefix; - addSecureSettings(builder, secureSettings -> - secureSettings.setString(finalPrefix + "ssl.keystore.secure_password", password)); + addSecureSettings(builder, secureSettings -> secureSettings.setString(finalPrefix + "ssl.keystore.secure_password", password)); if (randomBoolean()) { builder.put(prefix + "ssl.truststore.path", store); - addSecureSettings(builder, secureSettings -> - secureSettings.setString(finalPrefix + "ssl.truststore.secure_password", password)); + addSecureSettings( + builder, + secureSettings -> secureSettings.setString(finalPrefix + "ssl.truststore.secure_password", password) + ); } } @@ -280,8 +329,13 @@ private static void addSSLSettingsForStore(Settings.Builder builder, String pref * SSL connections. * @param trustedCertificates A list of PEM formatted certificates that will be trusted. */ - public static void addSSLSettingsForPEMFiles(Settings.Builder builder, String keyPath, String password, - String certificatePath, List trustedCertificates) { + public static void addSSLSettingsForPEMFiles( + Settings.Builder builder, + String keyPath, + String password, + String certificatePath, + List trustedCertificates + ) { addSSLSettingsForPEMFiles(builder, "", keyPath, password, certificatePath, trustedCertificates, true); } @@ -297,27 +351,39 @@ public static void addSSLSettingsForPEMFiles(Settings.Builder builder, String ke * @param prefix The settings prefix to use before ssl setting names * @param trustedCertificates A list of PEM formatted certificates that will be trusted. */ - public static void addSSLSettingsForPEMFiles(Settings.Builder builder, String keyPath, String password, - String certificatePath, String prefix, List trustedCertificates) { + public static void addSSLSettingsForPEMFiles( + Settings.Builder builder, + String keyPath, + String password, + String certificatePath, + String prefix, + List trustedCertificates + ) { addSSLSettingsForPEMFiles(builder, prefix, keyPath, password, certificatePath, trustedCertificates, true); } - private static void addSSLSettingsForPEMFiles(Settings.Builder builder, String prefix, String keyPath, String password, - String certificatePath, List trustedCertificates, - boolean hostnameVerificationEnabled) { + private static void addSSLSettingsForPEMFiles( + Settings.Builder builder, + String prefix, + String keyPath, + String password, + String certificatePath, + List trustedCertificates, + boolean hostnameVerificationEnabled + ) { if (prefix.equals("")) { prefix = "xpack.security.transport."; } builder.put(prefix + "ssl.verification_mode", hostnameVerificationEnabled ? "full" : "certificate"); - builder.put(prefix + "ssl.key", resolveResourcePath(keyPath)) - .put(prefix + "ssl.certificate", resolveResourcePath(certificatePath)); + builder.put(prefix + "ssl.key", resolveResourcePath(keyPath)).put(prefix + "ssl.certificate", resolveResourcePath(certificatePath)); final String finalPrefix = prefix; - addSecureSettings(builder, secureSettings -> - secureSettings.setString(finalPrefix + "ssl.secure_key_passphrase", password)); + addSecureSettings(builder, secureSettings -> secureSettings.setString(finalPrefix + "ssl.secure_key_passphrase", password)); if (trustedCertificates.isEmpty() == false) { - builder.put(prefix + "ssl.certificate_authorities", - Strings.arrayToCommaDelimitedString(resolvePathsToString(trustedCertificates))); + builder.put( + prefix + "ssl.certificate_authorities", + Strings.arrayToCommaDelimitedString(resolvePathsToString(trustedCertificates)) + ); } } @@ -330,8 +396,9 @@ public static void addSecureSettings(Settings.Builder builder, Consumer messageMatcher) { + public static void assertThrowsAuthorizationException( + LuceneTestCase.ThrowingRunnable throwingRunnable, + Matcher messageMatcher + ) { ElasticsearchSecurityException securityException = expectThrows(ElasticsearchSecurityException.class, throwingRunnable); assertAuthorizationException(securityException, messageMatcher); } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/test/SettingsFilterTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/test/SettingsFilterTests.java index 91fef098424d3..dd4b13f0d2971 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/test/SettingsFilterTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/test/SettingsFilterTests.java @@ -25,6 +25,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; + import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.TrustManagerFactory; @@ -56,24 +57,31 @@ public void testFiltering() throws Exception { // active directory filtering configureUnfilteredSetting("xpack.security.authc.realms.active_directory.ad1.enabled", "false"); configureUnfilteredSetting("xpack.security.authc.realms.active_directory.ad1.url", "ldap://host.domain"); - configureFilteredSetting("xpack.security.authc.realms.active_directory.ad1.hostname_verification", - Boolean.toString(randomBoolean())); + configureFilteredSetting( + "xpack.security.authc.realms.active_directory.ad1.hostname_verification", + Boolean.toString(randomBoolean()) + ); // pki filtering configureUnfilteredSetting("xpack.security.authc.realms.pki.pki1.order", "0"); if (inFipsJvm() == false) { - configureFilteredSetting("xpack.security.authc.realms.pki.pki1.truststore.path", - getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/truststore-testnode-only.jks").toString()); - configureFilteredSetting("xpack.security.transport.ssl.keystore.path", - getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.jks").toString()); + configureFilteredSetting( + "xpack.security.authc.realms.pki.pki1.truststore.path", + getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/truststore-testnode-only.jks").toString() + ); + configureFilteredSetting( + "xpack.security.transport.ssl.keystore.path", + getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.jks").toString() + ); } configureSecureSetting("xpack.security.authc.realms.pki.pki1.truststore.secure_password", "truststore-testnode-only"); configureFilteredSetting("xpack.security.authc.realms.pki.pki1.truststore.algorithm", "SunX509"); - configureUnfilteredSetting("xpack.security.transport.ssl.enabled", "true"); - configureFilteredSetting("xpack.security.transport.ssl.cipher_suites", - Strings.arrayToCommaDelimitedString(XPackSettings.DEFAULT_CIPHERS.toArray())); + configureFilteredSetting( + "xpack.security.transport.ssl.cipher_suites", + Strings.arrayToCommaDelimitedString(XPackSettings.DEFAULT_CIPHERS.toArray()) + ); configureFilteredSetting("xpack.security.transport.ssl.supported_protocols", randomFrom("TLSv1", "TLSv1.1", "TLSv1.2")); configureSecureSetting("xpack.security.transport.ssl.keystore.secure_password", "testnode"); configureFilteredSetting("xpack.security.transport.ssl.keystore.algorithm", KeyManagerFactory.getDefaultAlgorithm()); @@ -84,20 +92,30 @@ public void testFiltering() throws Exception { // client profile configureUnfilteredSetting("transport.profiles.client.port", "9500-9600"); if (inFipsJvm() == false) { - configureFilteredSetting("transport.profiles.client.xpack.security.ssl.keystore.path", - getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.jks").toString()); + configureFilteredSetting( + "transport.profiles.client.xpack.security.ssl.keystore.path", + getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.jks").toString() + ); } - configureFilteredSetting("transport.profiles.client.xpack.security.ssl.cipher_suites", - Strings.arrayToCommaDelimitedString(XPackSettings.DEFAULT_CIPHERS.toArray())); - configureFilteredSetting("transport.profiles.client.xpack.security.ssl.supported_protocols", - randomFrom("TLSv1", "TLSv1.1", "TLSv1.2")); + configureFilteredSetting( + "transport.profiles.client.xpack.security.ssl.cipher_suites", + Strings.arrayToCommaDelimitedString(XPackSettings.DEFAULT_CIPHERS.toArray()) + ); + configureFilteredSetting( + "transport.profiles.client.xpack.security.ssl.supported_protocols", + randomFrom("TLSv1", "TLSv1.1", "TLSv1.2") + ); configureSecureSetting("transport.profiles.client.xpack.security.ssl.keystore.secure_password", "testnode"); - configureFilteredSetting("transport.profiles.client.xpack.security.ssl.keystore.algorithm", - KeyManagerFactory.getDefaultAlgorithm()); + configureFilteredSetting( + "transport.profiles.client.xpack.security.ssl.keystore.algorithm", + KeyManagerFactory.getDefaultAlgorithm() + ); configureSecureSetting("transport.profiles.client.xpack.security.ssl.keystore.secure_key_password", "testnode"); configureSecureSetting("transport.profiles.client.xpack.security.ssl.truststore.secure_password", randomAlphaOfLength(5)); - configureFilteredSetting("transport.profiles.client.xpack.security.ssl.truststore.algorithm", - TrustManagerFactory.getDefaultAlgorithm()); + configureFilteredSetting( + "transport.profiles.client.xpack.security.ssl.truststore.algorithm", + TrustManagerFactory.getDefaultAlgorithm() + ); // custom settings, potentially added by a plugin configureFilteredSetting("foo.bar", "_secret"); @@ -107,10 +125,10 @@ public void testFiltering() throws Exception { configureFilteredSetting("xpack.security.hide_settings", "foo.*,bar.baz"); Settings settings = Settings.builder() - .put("path.home", createTempDir()) - .put(configuredSettingsBuilder.build()) - .setSecureSettings(mockSecureSettings) - .build(); + .put("path.home", createTempDir()) + .put(configuredSettingsBuilder.build()) + .setSecureSettings(mockSecureSettings) + .build(); LocalStateSecurity securityPlugin = new LocalStateSecurity(settings, null); @@ -134,9 +152,11 @@ public void testFiltering() throws Exception { } if (useLegacyLdapBindPassword) { - assertSettingDeprecationsAndWarnings(new Setting[]{PoolingSessionFactorySettings.LEGACY_BIND_PASSWORD - .apply(LdapRealmSettings.LDAP_TYPE) - .getConcreteSettingForNamespace("ldap1")}); + assertSettingDeprecationsAndWarnings( + new Setting[] { + PoolingSessionFactorySettings.LEGACY_BIND_PASSWORD.apply(LdapRealmSettings.LDAP_TYPE) + .getConcreteSettingForNamespace("ldap1") } + ); } } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/core/security/action/privilege/PutPrivilegesRequestBuilderTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/core/security/action/privilege/PutPrivilegesRequestBuilderTests.java index efbde30d0dbc2..c8e763d0911ce 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/core/security/action/privilege/PutPrivilegesRequestBuilderTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/core/security/action/privilege/PutPrivilegesRequestBuilderTests.java @@ -8,8 +8,8 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.util.set.Sets; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.security.authz.privilege.ApplicationPrivilegeDescriptor; import java.util.Collections; @@ -25,41 +25,55 @@ public class PutPrivilegesRequestBuilderTests extends ESTestCase { public void testBuildRequestWithMultipleElements() throws Exception { final PutPrivilegesRequestBuilder builder = new PutPrivilegesRequestBuilder(null); - builder.source(new BytesArray("{ " - + "\"foo\":{" - + " \"read\":{ \"application\":\"foo\", \"name\":\"read\", \"actions\":[ \"data:/read/*\", \"admin:/read/*\" ] }," - + " \"write\":{ \"application\":\"foo\", \"name\":\"write\", \"actions\":[ \"data:/write/*\", \"admin:*\" ] }," - + " \"all\":{ \"application\":\"foo\", \"name\":\"all\", \"actions\":[ \"*\" ] }" - + " }, " - + "\"bar\":{" - + " \"read\":{ \"application\":\"bar\", \"name\":\"read\", \"actions\":[ \"read/*\" ] }," - + " \"write\":{ \"application\":\"bar\", \"name\":\"write\", \"actions\":[ \"write/*\" ] }," - + " \"all\":{ \"application\":\"bar\", \"name\":\"all\", \"actions\":[ \"*\" ] }" - + " } " - + "}"), XContentType.JSON); + builder.source( + new BytesArray( + "{ " + + "\"foo\":{" + + " \"read\":{ \"application\":\"foo\", \"name\":\"read\", \"actions\":[ \"data:/read/*\", \"admin:/read/*\" ] }," + + " \"write\":{ \"application\":\"foo\", \"name\":\"write\", \"actions\":[ \"data:/write/*\", \"admin:*\" ] }," + + " \"all\":{ \"application\":\"foo\", \"name\":\"all\", \"actions\":[ \"*\" ] }" + + " }, " + + "\"bar\":{" + + " \"read\":{ \"application\":\"bar\", \"name\":\"read\", \"actions\":[ \"read/*\" ] }," + + " \"write\":{ \"application\":\"bar\", \"name\":\"write\", \"actions\":[ \"write/*\" ] }," + + " \"all\":{ \"application\":\"bar\", \"name\":\"all\", \"actions\":[ \"*\" ] }" + + " } " + + "}" + ), + XContentType.JSON + ); final List privileges = builder.request().getPrivileges(); assertThat(privileges, iterableWithSize(6)); - assertThat(privileges, contains( + assertThat( + privileges, + contains( descriptor("foo", "read", "data:/read/*", "admin:/read/*"), descriptor("foo", "write", "data:/write/*", "admin:*"), descriptor("foo", "all", "*"), descriptor("bar", "read", "read/*"), descriptor("bar", "write", "write/*"), descriptor("bar", "all", "*") - )); + ) + ); } - private ApplicationPrivilegeDescriptor descriptor(String app, String name, String ... actions) { + private ApplicationPrivilegeDescriptor descriptor(String app, String name, String... actions) { return new ApplicationPrivilegeDescriptor(app, name, Sets.newHashSet(actions), Collections.emptyMap()); } public void testPrivilegeNameValidationOfMultipleElement() throws Exception { final PutPrivilegesRequestBuilder builder = new PutPrivilegesRequestBuilder(null); - final IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> - builder.source(new BytesArray("{ \"foo\":{" + final IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> builder.source( + new BytesArray( + "{ \"foo\":{" + "\"write\":{ \"application\":\"foo\", \"name\":\"read\", \"actions\":[\"data:/read/*\",\"admin:/read/*\"] }," + "\"all\":{ \"application\":\"foo\", \"name\":\"all\", \"actions\":[ \"/*\" ] }" - + "} }"), XContentType.JSON) + + "} }" + ), + XContentType.JSON + ) ); assertThat(exception.getMessage(), containsString("write")); assertThat(exception.getMessage(), containsString("read")); @@ -67,12 +81,18 @@ public void testPrivilegeNameValidationOfMultipleElement() throws Exception { public void testApplicationNameValidationOfMultipleElement() throws Exception { final PutPrivilegesRequestBuilder builder = new PutPrivilegesRequestBuilder(null); - final IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> - builder.source(new BytesArray("{ \"bar\":{" + final IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> builder.source( + new BytesArray( + "{ \"bar\":{" + "\"read\":{ \"application\":\"foo\", \"name\":\"read\", \"actions\":[ \"data:/read/*\", \"admin:/read/*\" ] }," + "\"write\":{ \"application\":\"foo\", \"name\":\"write\", \"actions\":[ \"data:/write/*\", \"admin:/*\" ] }," + "\"all\":{ \"application\":\"foo\", \"name\":\"all\", \"actions\":[ \"/*\" ] }" - + "} }"), XContentType.JSON) + + "} }" + ), + XContentType.JSON + ) ); assertThat(exception.getMessage(), containsString("bar")); assertThat(exception.getMessage(), containsString("foo")); @@ -80,11 +100,16 @@ public void testApplicationNameValidationOfMultipleElement() throws Exception { public void testInferApplicationNameAndPrivilegeName() throws Exception { final PutPrivilegesRequestBuilder builder = new PutPrivilegesRequestBuilder(null); - builder.source(new BytesArray("{ \"foo\":{" - + "\"read\":{ \"actions\":[ \"data:/read/*\", \"admin:/read/*\" ] }," - + "\"write\":{ \"actions\":[ \"data:/write/*\", \"admin:/*\" ] }," - + "\"all\":{ \"actions\":[ \"*\" ] }" - + "} }"), XContentType.JSON); + builder.source( + new BytesArray( + "{ \"foo\":{" + + "\"read\":{ \"actions\":[ \"data:/read/*\", \"admin:/read/*\" ] }," + + "\"write\":{ \"actions\":[ \"data:/write/*\", \"admin:/*\" ] }," + + "\"all\":{ \"actions\":[ \"*\" ] }" + + "} }" + ), + XContentType.JSON + ); assertThat(builder.request().getPrivileges(), iterableWithSize(3)); for (ApplicationPrivilegeDescriptor p : builder.request().getPrivileges()) { assertThat(p.getApplication(), equalTo("foo")); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/core/security/transport/ProfileConfigurationsTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/core/security/transport/ProfileConfigurationsTests.java index c059027a3700c..da4ced4c22a7e 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/core/security/transport/ProfileConfigurationsTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/core/security/transport/ProfileConfigurationsTests.java @@ -24,8 +24,7 @@ public class ProfileConfigurationsTests extends ESTestCase { public void testGetSecureTransportProfileConfigurations() { assumeFalse("Can't run in a FIPS JVM, uses JKS/PKCS12 keystores", inFipsJvm()); - final Settings settings = getBaseSettings() - .put("path.home", createTempDir()) + final Settings settings = getBaseSettings().put("path.home", createTempDir()) .put("xpack.security.transport.ssl.verification_mode", SslVerificationMode.CERTIFICATE.name()) .put("xpack.security.transport.ssl.verification_mode", SslVerificationMode.CERTIFICATE.name()) .put("transport.profiles.full.xpack.security.ssl.verification_mode", SslVerificationMode.FULL.name()) @@ -44,8 +43,7 @@ public void testGetSecureTransportProfileConfigurations() { public void testGetInsecureTransportProfileConfigurations() { assumeFalse("Can't run in a FIPS JVM with verification mode None", inFipsJvm()); - final Settings settings = getBaseSettings() - .put("path.home", createTempDir()) + final Settings settings = getBaseSettings().put("path.home", createTempDir()) .put("xpack.security.transport.ssl.verification_mode", SslVerificationMode.CERTIFICATE.name()) .put("transport.profiles.none.xpack.security.ssl.verification_mode", SslVerificationMode.NONE.name()) .build(); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/LocalStateSecurity.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/LocalStateSecurity.java index 71b296bbed9b6..9946f5d2a2c49 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/LocalStateSecurity.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/LocalStateSecurity.java @@ -37,11 +37,17 @@ public class LocalStateSecurity extends LocalStateCompositeXPackPlugin { public static class SecurityTransportXPackUsageAction extends TransportXPackUsageAction { @Inject - public SecurityTransportXPackUsageAction(ThreadPool threadPool, TransportService transportService, - ClusterService clusterService, ActionFilters actionFilters, - IndexNameExpressionResolver indexNameExpressionResolver, NodeClient client) { + public SecurityTransportXPackUsageAction( + ThreadPool threadPool, + TransportService transportService, + ClusterService clusterService, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver, + NodeClient client + ) { super(threadPool, transportService, clusterService, actionFilters, indexNameExpressionResolver, client); } + @Override protected List usageActions() { return Collections.singletonList(XPackUsageFeatureAction.SECURITY); @@ -50,8 +56,12 @@ protected List usageActions() { public static class SecurityTransportXPackInfoAction extends TransportXPackInfoAction { @Inject - public SecurityTransportXPackInfoAction(TransportService transportService, ActionFilters actionFilters, - LicenseService licenseService, NodeClient client) { + public SecurityTransportXPackInfoAction( + TransportService transportService, + ActionFilters actionFilters, + LicenseService licenseService, + NodeClient client + ) { super(transportService, actionFilters, licenseService, client); } @@ -82,10 +92,14 @@ protected XPackLicenseState getLicenseState() { }); plugins.add(new Security(settings, configPath) { @Override - protected SSLService getSslService() { return thisVar.getSslService(); } + protected SSLService getSslService() { + return thisVar.getSslService(); + } @Override - protected XPackLicenseState getLicenseState() { return thisVar.getLicenseState(); } + protected XPackLicenseState getLicenseState() { + return thisVar.getLicenseState(); + } }); } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/PkiRealmBootstrapCheckTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/PkiRealmBootstrapCheckTests.java index 937055af941c2..4b97c61e5927b 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/PkiRealmBootstrapCheckTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/PkiRealmBootstrapCheckTests.java @@ -30,55 +30,58 @@ public void testBootstrapCheckWithPkiRealm() throws Exception { MockSecureSettings secureSettings = new MockSecureSettings(); Settings settings = Settings.builder() - .put("xpack.security.authc.realms.pki.test_pki.order", 0) - .put("path.home", createTempDir()) - .setSecureSettings(secureSettings) - .build(); + .put("xpack.security.authc.realms.pki.test_pki.order", 0) + .put("path.home", createTempDir()) + .setSecureSettings(secureSettings) + .build(); assertTrue(runCheck(settings).isFailure()); // enable transport tls secureSettings.setString("xpack.security.transport.ssl.secure_key_passphrase", "testnode"); - settings = Settings.builder().put(settings) - .put("xpack.security.transport.ssl.enabled", true) - .put("xpack.security.transport.ssl.certificate", certPath) - .put("xpack.security.transport.ssl.key", keyPath) - .build(); + settings = Settings.builder() + .put(settings) + .put("xpack.security.transport.ssl.enabled", true) + .put("xpack.security.transport.ssl.certificate", certPath) + .put("xpack.security.transport.ssl.key", keyPath) + .build(); assertFalse(runCheck(settings).isFailure()); // enable ssl for http secureSettings.setString("xpack.security.http.ssl.secure_key_passphrase", "testnode"); - settings = Settings.builder().put(settings) - .put("xpack.security.transport.ssl.enabled", false) - .put("xpack.security.http.ssl.enabled", true) - .put("xpack.security.http.ssl.certificate", certPath) - .put("xpack.security.http.ssl.key", keyPath) - .build(); + settings = Settings.builder() + .put(settings) + .put("xpack.security.transport.ssl.enabled", false) + .put("xpack.security.http.ssl.enabled", true) + .put("xpack.security.http.ssl.certificate", certPath) + .put("xpack.security.http.ssl.key", keyPath) + .build(); assertTrue(runCheck(settings).isFailure()); // enable client auth for http - settings = Settings.builder().put(settings) - .put("xpack.security.http.ssl.client_authentication", randomFrom("required", "optional")) - .build(); + settings = Settings.builder() + .put(settings) + .put("xpack.security.http.ssl.client_authentication", randomFrom("required", "optional")) + .build(); assertFalse(runCheck(settings).isFailure()); // disable http ssl - settings = Settings.builder().put(settings) - .put("xpack.security.http.ssl.enabled", false) - .build(); + settings = Settings.builder().put(settings).put("xpack.security.http.ssl.enabled", false).build(); assertTrue(runCheck(settings).isFailure()); // set transport auth - settings = Settings.builder().put(settings) - .put("xpack.security.transport.client_authentication", randomFrom("required", "optional")) - .build(); + settings = Settings.builder() + .put(settings) + .put("xpack.security.transport.client_authentication", randomFrom("required", "optional")) + .build(); assertTrue(runCheck(settings).isFailure()); // test with transport profile - settings = Settings.builder().put(settings) - .put("xpack.security.transport.ssl.enabled", true) - .put("xpack.security.transport.client_authentication", "none") - .put("transport.profiles.foo.xpack.security.ssl.client_authentication", randomFrom("required", "optional")) - .build(); + settings = Settings.builder() + .put(settings) + .put("xpack.security.transport.ssl.enabled", true) + .put("xpack.security.transport.client_authentication", "none") + .put("transport.profiles.foo.xpack.security.ssl.client_authentication", randomFrom("required", "optional")) + .build(); assertFalse(runCheck(settings).isFailure()); } @@ -89,11 +92,11 @@ private BootstrapCheck.BootstrapCheckResult runCheck(Settings settings) throws E public void testBootstrapCheckWithDisabledRealm() throws Exception { Settings settings = Settings.builder() - .put("xpack.security.authc.realms.pki.test_pki.enabled", false) - .put("xpack.security.transport.ssl.enabled", false) - .put("xpack.security.transport.ssl.client_authentication", "none") - .put("path.home", createTempDir()) - .build(); + .put("xpack.security.authc.realms.pki.test_pki.enabled", false) + .put("xpack.security.transport.ssl.enabled", false) + .put("xpack.security.transport.ssl.client_authentication", "none") + .put("path.home", createTempDir()) + .build(); Environment env = TestEnvironment.newEnvironment(settings); assertFalse(runCheck(settings).isFailure()); } @@ -105,15 +108,15 @@ public void testBootstrapCheckWithDelegationEnabled() throws Exception { // enable transport tls secureSettings.setString("xpack.security.transport.ssl.secure_key_passphrase", "testnode"); Settings settings = Settings.builder() - .put("xpack.security.authc.realms.pki.test_pki.enabled", true) - .put("xpack.security.authc.realms.pki.test_pki.delegation.enabled", true) - .put("xpack.security.transport.ssl.enabled", randomBoolean()) - .put("xpack.security.transport.ssl.client_authentication", "none") - .put("xpack.security.transport.ssl.certificate", certPath.toString()) - .put("xpack.security.transport.ssl.key", keyPath.toString()) - .put("path.home", createTempDir()) - .setSecureSettings(secureSettings) - .build(); + .put("xpack.security.authc.realms.pki.test_pki.enabled", true) + .put("xpack.security.authc.realms.pki.test_pki.delegation.enabled", true) + .put("xpack.security.transport.ssl.enabled", randomBoolean()) + .put("xpack.security.transport.ssl.client_authentication", "none") + .put("xpack.security.transport.ssl.certificate", certPath.toString()) + .put("xpack.security.transport.ssl.key", keyPath.toString()) + .put("path.home", createTempDir()) + .setSecureSettings(secureSettings) + .build(); assertFalse(runCheck(settings).isFailure()); } @@ -125,10 +128,11 @@ public void testBootstrapCheckWithClosedSecuredSetting() throws Exception { .put("xpack.security.authc.realms.pki.test_pki.order", 0) .put("xpack.security.http.ssl.enabled", true) .put("xpack.security.http.ssl.client_authentication", expectFail ? "none" : "optional") - .put("xpack.security.http.ssl.key", - getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.pem")) - .put("xpack.security.http.ssl.certificate", - getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt")) + .put("xpack.security.http.ssl.key", getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.pem")) + .put( + "xpack.security.http.ssl.certificate", + getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt") + ) .put("path.home", createTempDir()) .setSecureSettings(secureSettings) .build(); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/SecurityContextTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/SecurityContextTests.java index d681e86239b65..f64ad4572e78d 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/SecurityContextTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/SecurityContextTests.java @@ -44,9 +44,7 @@ public class SecurityContextTests extends ESTestCase { @Before public void buildSecurityContext() throws IOException { - settings = Settings.builder() - .put("path.home", createTempDir()) - .build(); + settings = Settings.builder().put("path.home", createTempDir()).build(); threadContext = new ThreadContext(settings); securityContext = new SecurityContext(settings, threadContext); } @@ -81,8 +79,10 @@ public void testSetUser() { assertEquals(user, securityContext.getUser()); assertEquals(AuthenticationType.INTERNAL, securityContext.getAuthentication().getAuthenticationType()); - IllegalStateException e = expectThrows(IllegalStateException.class, - () -> securityContext.setUser(randomFrom(user, SystemUser.INSTANCE), Version.CURRENT)); + IllegalStateException e = expectThrows( + IllegalStateException.class, + () -> securityContext.setUser(randomFrom(user, SystemUser.INSTANCE), Version.CURRENT) + ); assertEquals("authentication ([_xpack_security_authentication]) is already present in the context", e.getMessage()); } @@ -121,8 +121,10 @@ public void testExecuteAfterRewritingAuthentication() throws IOException { final Authentication original = new Authentication(user, authBy, authBy); original.writeToContext(threadContext); final Map requestHeaders = Map.of( - AuthenticationField.PRIVILEGE_CATEGORY_KEY, randomAlphaOfLengthBetween(3, 10), - randomAlphaOfLengthBetween(3, 8), randomAlphaOfLengthBetween(3, 8) + AuthenticationField.PRIVILEGE_CATEGORY_KEY, + randomAlphaOfLengthBetween(3, 10), + randomAlphaOfLengthBetween(3, 8), + randomAlphaOfLengthBetween(3, 8) ); threadContext.putHeader(requestHeaders); @@ -151,20 +153,25 @@ public void testExecuteAfterRewritingAuthenticationWillConditionallyRewriteNewAp User user = new User("test", null, new User("authUser")); RealmRef authBy = new RealmRef("_es_api_key", "_es_api_key", "node1"); final Map metadata = Map.of( - API_KEY_ROLE_DESCRIPTORS_KEY, new BytesArray("{\"a role\": {\"cluster\": [\"all\"]}}"), - API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY, new BytesArray("{\"limitedBy role\": {\"cluster\": [\"all\"]}}") + API_KEY_ROLE_DESCRIPTORS_KEY, + new BytesArray("{\"a role\": {\"cluster\": [\"all\"]}}"), + API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY, + new BytesArray("{\"limitedBy role\": {\"cluster\": [\"all\"]}}") ); - final Authentication original = new Authentication(user, authBy, authBy, Version.V_8_0_0, - AuthenticationType.API_KEY, metadata); + final Authentication original = new Authentication(user, authBy, authBy, Version.V_8_0_0, AuthenticationType.API_KEY, metadata); original.writeToContext(threadContext); // If target is old node, rewrite new style API key metadata to old format securityContext.executeAfterRewritingAuthentication(originalCtx -> { Authentication authentication = securityContext.getAuthentication(); - assertEquals(Map.of("a role", Map.of("cluster", List.of("all"))), - authentication.getMetadata().get(API_KEY_ROLE_DESCRIPTORS_KEY)); - assertEquals(Map.of("limitedBy role", Map.of("cluster", List.of("all"))), - authentication.getMetadata().get(API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY)); + assertEquals( + Map.of("a role", Map.of("cluster", List.of("all"))), + authentication.getMetadata().get(API_KEY_ROLE_DESCRIPTORS_KEY) + ); + assertEquals( + Map.of("limitedBy role", Map.of("cluster", List.of("all"))), + authentication.getMetadata().get(API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY) + ); }, Version.V_7_8_0); // If target is new node, no need to rewrite the new style API key metadata @@ -178,8 +185,10 @@ public void testExecuteAfterRewritingAuthenticationWillConditionallyRewriteOldAp User user = new User("test", null, new User("authUser")); RealmRef authBy = new RealmRef("_es_api_key", "_es_api_key", "node1"); final Map metadata = Map.of( - API_KEY_ROLE_DESCRIPTORS_KEY, Map.of("a role", Map.of("cluster", List.of("all"))), - API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY, Map.of("limitedBy role", Map.of("cluster", List.of("all"))) + API_KEY_ROLE_DESCRIPTORS_KEY, + Map.of("a role", Map.of("cluster", List.of("all"))), + API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY, + Map.of("limitedBy role", Map.of("cluster", List.of("all"))) ); final Authentication original = new Authentication(user, authBy, authBy, Version.V_7_8_0, AuthenticationType.API_KEY, metadata); original.writeToContext(threadContext); @@ -193,10 +202,14 @@ public void testExecuteAfterRewritingAuthenticationWillConditionallyRewriteOldAp // If target is new old, ensure old map style API key metadata is rewritten to bytesreference securityContext.executeAfterRewritingAuthentication(originalCtx -> { Authentication authentication = securityContext.getAuthentication(); - assertEquals("{\"a role\":{\"cluster\":[\"all\"]}}", - ((BytesReference)authentication.getMetadata().get(API_KEY_ROLE_DESCRIPTORS_KEY)).utf8ToString()); - assertEquals("{\"limitedBy role\":{\"cluster\":[\"all\"]}}", - ((BytesReference)authentication.getMetadata().get(API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY)).utf8ToString()); + assertEquals( + "{\"a role\":{\"cluster\":[\"all\"]}}", + ((BytesReference) authentication.getMetadata().get(API_KEY_ROLE_DESCRIPTORS_KEY)).utf8ToString() + ); + assertEquals( + "{\"limitedBy role\":{\"cluster\":[\"all\"]}}", + ((BytesReference) authentication.getMetadata().get(API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY)).utf8ToString() + ); }, VersionUtils.randomVersionBetween(random(), VERSION_API_KEY_ROLES_AS_BYTES, Version.CURRENT)); } } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/SecurityInfoTransportActionTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/SecurityInfoTransportActionTests.java index 65f6841bf4d04..0f25c27bbe00d 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/SecurityInfoTransportActionTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/SecurityInfoTransportActionTests.java @@ -71,18 +71,23 @@ public void init() throws Exception { public void testAvailable() { SecurityInfoTransportAction featureSet = new SecurityInfoTransportAction( - mock(TransportService.class), mock(ActionFilters.class), settings); + mock(TransportService.class), + mock(ActionFilters.class), + settings + ); assertThat(featureSet.available(), is(true)); } public void testEnabled() { SecurityInfoTransportAction featureSet = new SecurityInfoTransportAction( - mock(TransportService.class), mock(ActionFilters.class), settings); + mock(TransportService.class), + mock(ActionFilters.class), + settings + ); assertThat(featureSet.enabled(), is(true)); - Settings disabled = Settings.builder().put(XPackSettings.SECURITY_ENABLED.getKey(),false).build(); - featureSet = new SecurityInfoTransportAction( - mock(TransportService.class), mock(ActionFilters.class), disabled); + Settings disabled = Settings.builder().put(XPackSettings.SECURITY_ENABLED.getKey(), false).build(); + featureSet = new SecurityInfoTransportAction(mock(TransportService.class), mock(ActionFilters.class), disabled); assertThat(featureSet.enabled(), is(false)); } @@ -124,12 +129,12 @@ public void testUsage() throws Exception { settings.put(XPackSettings.AUDIT_ENABLED.getKey(), auditingEnabled); final boolean httpIpFilterEnabled = randomBoolean(); final boolean transportIPFilterEnabled = randomBoolean(); - when(ipFilter.usageStats()) - .thenReturn(MapBuilder.newMapBuilder() - .put("http", Collections.singletonMap("enabled", httpIpFilterEnabled)) - .put("transport", Collections.singletonMap("enabled", transportIPFilterEnabled)) - .map()); - + when(ipFilter.usageStats()).thenReturn( + MapBuilder.newMapBuilder() + .put("http", Collections.singletonMap("enabled", httpIpFilterEnabled)) + .put("transport", Collections.singletonMap("enabled", transportIPFilterEnabled)) + .map() + ); final boolean rolesStoreEnabled = randomBoolean(); configureRoleStoreUsage(rolesStoreEnabled); @@ -296,8 +301,15 @@ private void configureRoleMappingStoreUsage(boolean roleMappingStoreEnabled) { } private SecurityUsageTransportAction newUsageAction(Settings settings) { - return new SecurityUsageTransportAction(mock(TransportService.class),null, - null, mock(ActionFilters.class),null, - settings, licenseState, securityServices); + return new SecurityUsageTransportAction( + mock(TransportService.class), + null, + null, + mock(ActionFilters.class), + null, + settings, + licenseState, + securityServices + ); } } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/SecurityTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/SecurityTests.java index 81e08976ee683..d1b9da81b6373 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/SecurityTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/SecurityTests.java @@ -28,8 +28,8 @@ import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsModule; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.env.Environment; import org.elasticsearch.env.NodeMetadata; import org.elasticsearch.env.TestEnvironment; @@ -99,11 +99,11 @@ import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.empty; -import static org.hamcrest.Matchers.hasSize; -import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.instanceOf; +import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.notNullValue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -156,8 +156,17 @@ protected SSLService getSslService() { Client client = mock(Client.class); when(client.threadPool()).thenReturn(threadPool); when(client.settings()).thenReturn(settings); - return security.createComponents(client, threadPool, clusterService, mock(ResourceWatcherService.class), mock(ScriptService.class), - xContentRegistry(), env, nodeMetadata, TestIndexNameExpressionResolver.newInstance(threadContext)); + return security.createComponents( + client, + threadPool, + clusterService, + mock(ResourceWatcherService.class), + mock(ScriptService.class), + xContentRegistry(), + env, + nodeMetadata, + TestIndexNameExpressionResolver.newInstance(threadContext) + ); } private Collection createComponents(Settings testSettings, SecurityExtension... extensions) throws Exception { @@ -167,7 +176,8 @@ private Collection createComponents(Settings testSettings, SecurityExten Settings settings = Settings.builder() .put("xpack.security.enabled", true) .put(testSettings) - .put("path.home", createTempDir()).build(); + .put("path.home", createTempDir()) + .build(); return createComponentsUtil(settings, extensions); } @@ -193,8 +203,10 @@ public void testCustomRealmExtension() throws Exception { } public void testCustomRealmExtensionConflict() throws Exception { - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> createComponents(Settings.EMPTY, new DummyExtension(FileRealmSettings.TYPE))); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> createComponents(Settings.EMPTY, new DummyExtension(FileRealmSettings.TYPE)) + ); assertEquals("Realm type [" + FileRealmSettings.TYPE + "] is already registered", e.getMessage()); } @@ -220,10 +232,13 @@ public void testHttpSettingDefaults() throws Exception { } public void testTransportSettingNetty4Both() { - Settings both4 = Security.additionalSettings(Settings.builder() - .put(NetworkModule.TRANSPORT_TYPE_KEY, SecurityField.NAME4) - .put(NetworkModule.HTTP_TYPE_KEY, SecurityField.NAME4) - .build(), true); + Settings both4 = Security.additionalSettings( + Settings.builder() + .put(NetworkModule.TRANSPORT_TYPE_KEY, SecurityField.NAME4) + .put(NetworkModule.HTTP_TYPE_KEY, SecurityField.NAME4) + .build(), + true + ); assertFalse(NetworkModule.TRANSPORT_TYPE_SETTING.exists(both4)); assertFalse(NetworkModule.HTTP_TYPE_SETTING.exists(both4)); } @@ -231,14 +246,18 @@ public void testTransportSettingNetty4Both() { public void testTransportSettingValidation() { final String badType = randomFrom("netty4", "other", "security1"); Settings settingsTransport = Settings.builder().put(NetworkModule.TRANSPORT_TYPE_KEY, badType).build(); - IllegalArgumentException badTransport = expectThrows(IllegalArgumentException.class, - () -> Security.additionalSettings(settingsTransport, true)); + IllegalArgumentException badTransport = expectThrows( + IllegalArgumentException.class, + () -> Security.additionalSettings(settingsTransport, true) + ); assertThat(badTransport.getMessage(), containsString(SecurityField.NAME4)); assertThat(badTransport.getMessage(), containsString(NetworkModule.TRANSPORT_TYPE_KEY)); Settings settingsHttp = Settings.builder().put(NetworkModule.HTTP_TYPE_KEY, badType).build(); - IllegalArgumentException badHttp = expectThrows(IllegalArgumentException.class, - () -> Security.additionalSettings(settingsHttp, true)); + IllegalArgumentException badHttp = expectThrows( + IllegalArgumentException.class, + () -> Security.additionalSettings(settingsHttp, true) + ); assertThat(badHttp.getMessage(), containsString(SecurityField.NAME4)); assertThat(badHttp.getMessage(), containsString(NetworkModule.HTTP_TYPE_KEY)); } @@ -249,7 +268,7 @@ public void testNoRealmsWhenSecurityDisabled() throws Exception { .put("path.home", createTempDir()) .build(); Collection components = createComponents(settings); - for (Object component: components) { + for (Object component : components) { assertThat(component, not(instanceOf(Realms.class))); assertThat(component, not(instanceOf(NativeUsersStore.class))); assertThat(component, not(instanceOf(ReservedRealm.class))); @@ -264,17 +283,34 @@ public void testSettingFilter() throws Exception { public void testOnIndexModuleIsNoOpWithSecurityDisabled() throws Exception { Settings settings = Settings.builder() - .put(XPackSettings.SECURITY_ENABLED.getKey(), false) - .put("path.home", createTempDir()) - .build(); + .put(XPackSettings.SECURITY_ENABLED.getKey(), false) + .put("path.home", createTempDir()) + .build(); createComponents(settings); IndexSettings indexSettings = IndexSettingsModule.newIndexSettings("foo", Settings.EMPTY); ThreadPool threadPool = mock(ThreadPool.class); when(threadPool.getThreadContext()).thenReturn(threadContext); - AnalysisRegistry emptyAnalysisRegistry = new AnalysisRegistry(TestEnvironment.newEnvironment(settings), emptyMap(), emptyMap(), - emptyMap(), emptyMap(), emptyMap(), emptyMap(), emptyMap(), emptyMap(), emptyMap()); - IndexModule indexModule = new IndexModule(indexSettings, emptyAnalysisRegistry, new InternalEngineFactory(), Collections.emptyMap(), - () -> true, TestIndexNameExpressionResolver.newInstance(threadPool.getThreadContext()), Collections.emptyMap()); + AnalysisRegistry emptyAnalysisRegistry = new AnalysisRegistry( + TestEnvironment.newEnvironment(settings), + emptyMap(), + emptyMap(), + emptyMap(), + emptyMap(), + emptyMap(), + emptyMap(), + emptyMap(), + emptyMap(), + emptyMap() + ); + IndexModule indexModule = new IndexModule( + indexSettings, + emptyAnalysisRegistry, + new InternalEngineFactory(), + Collections.emptyMap(), + () -> true, + TestIndexNameExpressionResolver.newInstance(threadPool.getThreadContext()), + Collections.emptyMap() + ); security.onIndexModule(indexModule); // indexReaderWrapper is a SetOnce so if Security#onIndexModule had already set an ReaderWrapper we would get an exception here indexModule.setReaderWrapper(null); @@ -282,22 +318,28 @@ public void testOnIndexModuleIsNoOpWithSecurityDisabled() throws Exception { public void testFilteredSettings() throws Exception { createComponents(Settings.EMPTY); - final List> realmSettings = security.getSettings().stream() + final List> realmSettings = security.getSettings() + .stream() .filter(s -> s.getKey().startsWith("xpack.security.authc.realms")) .collect(Collectors.toList()); Arrays.asList( - "bind_dn", "bind_password", + "bind_dn", + "bind_password", "hostname_verification", - "truststore.password", "truststore.path", "truststore.algorithm", - "keystore.key_password").forEach(suffix -> { + "truststore.password", + "truststore.path", + "truststore.algorithm", + "keystore.key_password" + ).forEach(suffix -> { final List> matching = realmSettings.stream() .filter(s -> s.getKey().endsWith("." + suffix)) .collect(Collectors.toList()); assertThat("For suffix " + suffix, matching, Matchers.not(empty())); - matching.forEach(setting -> assertThat("For setting " + setting, - setting.getProperties(), Matchers.hasItem(Setting.Property.Filtered))); + matching.forEach( + setting -> assertThat("For setting " + setting, setting.getProperties(), Matchers.hasItem(Setting.Property.Filtered)) + ); }); } @@ -309,13 +351,16 @@ public void testJoinValidatorOnDisabledSecurity() throws Exception { } public void testJoinValidatorForFIPSOnAllowedLicense() throws Exception { - DiscoveryNode node = new DiscoveryNode("foo", buildNewFakeTransportAddress(), - VersionUtils.randomVersionBetween(random(), null, Version.CURRENT)); + DiscoveryNode node = new DiscoveryNode( + "foo", + buildNewFakeTransportAddress(), + VersionUtils.randomVersionBetween(random(), null, Version.CURRENT) + ); Metadata.Builder builder = Metadata.builder(); - License license = - TestUtils.generateSignedLicense( - randomFrom(License.OperationMode.ENTERPRISE, License.OperationMode.PLATINUM, License.OperationMode.TRIAL).toString(), - TimeValue.timeValueHours(24)); + License license = TestUtils.generateSignedLicense( + randomFrom(License.OperationMode.ENTERPRISE, License.OperationMode.PLATINUM, License.OperationMode.TRIAL).toString(), + TimeValue.timeValueHours(24) + ); TestUtils.putLicense(builder, license); ClusterState state = ClusterState.builder(ClusterName.DEFAULT).metadata(builder.build()).build(); new Security.ValidateLicenseForFIPS(false).accept(node, state); @@ -325,19 +370,27 @@ public void testJoinValidatorForFIPSOnAllowedLicense() throws Exception { } public void testJoinValidatorForFIPSOnForbiddenLicense() throws Exception { - DiscoveryNode node = new DiscoveryNode("foo", buildNewFakeTransportAddress(), - VersionUtils.randomVersionBetween(random(), null, Version.CURRENT)); + DiscoveryNode node = new DiscoveryNode( + "foo", + buildNewFakeTransportAddress(), + VersionUtils.randomVersionBetween(random(), null, Version.CURRENT) + ); Metadata.Builder builder = Metadata.builder(); - final String forbiddenLicenseType = - randomFrom(List.of(License.OperationMode.values()).stream() - .filter(l -> XPackLicenseState.isFipsAllowedForOperationMode(l) == false).collect(Collectors.toList())).toString(); + final String forbiddenLicenseType = randomFrom( + List.of(License.OperationMode.values()) + .stream() + .filter(l -> XPackLicenseState.isFipsAllowedForOperationMode(l) == false) + .collect(Collectors.toList()) + ).toString(); License license = TestUtils.generateSignedLicense(forbiddenLicenseType, TimeValue.timeValueHours(24)); TestUtils.putLicense(builder, license); ClusterState state = ClusterState.builder(ClusterName.DEFAULT).metadata(builder.build()).build(); new Security.ValidateLicenseForFIPS(false).accept(node, state); // no exception thrown - IllegalStateException e = expectThrows(IllegalStateException.class, - () -> new Security.ValidateLicenseForFIPS(true).accept(node, state)); + IllegalStateException e = expectThrows( + IllegalStateException.class, + () -> new Security.ValidateLicenseForFIPS(true).accept(node, state) + ); assertThat(e.getMessage(), containsString("FIPS mode cannot be used")); } @@ -350,13 +403,15 @@ public void testIndexJoinValidator_FullyCurrentCluster() throws Exception { int indexFormat = randomBoolean() ? INTERNAL_MAIN_INDEX_FORMAT : INTERNAL_MAIN_INDEX_FORMAT - 1; IndexMetadata indexMetadata = IndexMetadata.builder(SECURITY_MAIN_ALIAS) .settings(settings(VersionUtils.randomIndexCompatibleVersion(random())).put(INDEX_FORMAT_SETTING.getKey(), indexFormat)) - .numberOfShards(1).numberOfReplicas(0) + .numberOfShards(1) + .numberOfReplicas(0) .build(); DiscoveryNode existingOtherNode = new DiscoveryNode("bar", buildNewFakeTransportAddress(), Version.CURRENT); DiscoveryNodes discoveryNodes = DiscoveryNodes.builder().add(existingOtherNode).build(); ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) .nodes(discoveryNodes) - .metadata(Metadata.builder().put(indexMetadata, true).build()).build(); + .metadata(Metadata.builder().put(indexMetadata, true).build()) + .build(); joinValidator.accept(node, clusterState); } @@ -368,13 +423,15 @@ public void testIndexUpgradeValidatorWithUpToDateIndex() throws Exception { DiscoveryNode node = new DiscoveryNode("foo", buildNewFakeTransportAddress(), Version.CURRENT); IndexMetadata indexMetadata = IndexMetadata.builder(SECURITY_MAIN_ALIAS) .settings(settings(version).put(INDEX_FORMAT_SETTING.getKey(), INTERNAL_MAIN_INDEX_FORMAT)) - .numberOfShards(1).numberOfReplicas(0) + .numberOfShards(1) + .numberOfReplicas(0) .build(); DiscoveryNode existingOtherNode = new DiscoveryNode("bar", buildNewFakeTransportAddress(), version); DiscoveryNodes discoveryNodes = DiscoveryNodes.builder().add(existingOtherNode).build(); ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) .nodes(discoveryNodes) - .metadata(Metadata.builder().put(indexMetadata, true).build()).build(); + .metadata(Metadata.builder().put(indexMetadata, true).build()) + .build(); joinValidator.accept(node, clusterState); } @@ -383,11 +440,13 @@ public void testIndexUpgradeValidatorWithMissingIndex() throws Exception { BiConsumer joinValidator = security.getJoinValidator(); assertNotNull(joinValidator); DiscoveryNode node = new DiscoveryNode("foo", buildNewFakeTransportAddress(), Version.CURRENT); - DiscoveryNode existingOtherNode = new DiscoveryNode("bar", buildNewFakeTransportAddress(), - VersionUtils.randomCompatibleVersion(random(), Version.CURRENT)); + DiscoveryNode existingOtherNode = new DiscoveryNode( + "bar", + buildNewFakeTransportAddress(), + VersionUtils.randomCompatibleVersion(random(), Version.CURRENT) + ); DiscoveryNodes discoveryNodes = DiscoveryNodes.builder().add(existingOtherNode).build(); - ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) - .nodes(discoveryNodes).build(); + ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).nodes(discoveryNodes).build(); joinValidator.accept(node, clusterState); } @@ -398,15 +457,25 @@ public void testGetFieldFilterSecurityEnabled() throws Exception { Map permissionsMap = new HashMap<>(); FieldPermissions permissions = new FieldPermissions( - new FieldPermissionsDefinition(new String[] { "field_granted" }, Strings.EMPTY_ARRAY)); - IndicesAccessControl.IndexAccessControl indexGrantedAccessControl = new IndicesAccessControl.IndexAccessControl(true, permissions, - DocumentPermissions.allowAll()); + new FieldPermissionsDefinition(new String[] { "field_granted" }, Strings.EMPTY_ARRAY) + ); + IndicesAccessControl.IndexAccessControl indexGrantedAccessControl = new IndicesAccessControl.IndexAccessControl( + true, + permissions, + DocumentPermissions.allowAll() + ); permissionsMap.put("index_granted", indexGrantedAccessControl); - IndicesAccessControl.IndexAccessControl indexAccessControl = new IndicesAccessControl.IndexAccessControl(false, - FieldPermissions.DEFAULT, DocumentPermissions.allowAll()); + IndicesAccessControl.IndexAccessControl indexAccessControl = new IndicesAccessControl.IndexAccessControl( + false, + FieldPermissions.DEFAULT, + DocumentPermissions.allowAll() + ); permissionsMap.put("index_not_granted", indexAccessControl); - IndicesAccessControl.IndexAccessControl nullFieldPermissions = - new IndicesAccessControl.IndexAccessControl(true, null, DocumentPermissions.allowAll()); + IndicesAccessControl.IndexAccessControl nullFieldPermissions = new IndicesAccessControl.IndexAccessControl( + true, + null, + DocumentPermissions.allowAll() + ); permissionsMap.put("index_null", nullFieldPermissions); IndicesAccessControl index = new IndicesAccessControl(true, permissionsMap); threadContext.putTransient(AuthorizationServiceField.INDICES_PERMISSIONS_KEY, index); @@ -431,7 +500,10 @@ public void testGetFieldFilterSecurityEnabledLicenseNoFLS() throws Exception { Function> fieldFilter = security.getFieldFilter(); assertNotSame(MapperPlugin.NOOP_FIELD_FILTER, fieldFilter); licenseState.update( - randomFrom(License.OperationMode.BASIC, License.OperationMode.STANDARD, License.OperationMode.GOLD), true, null); + randomFrom(License.OperationMode.BASIC, License.OperationMode.STANDARD, License.OperationMode.GOLD), + true, + null + ); assertNotSame(MapperPlugin.NOOP_FIELD_FILTER, fieldFilter); assertSame(MapperPlugin.NOOP_FIELD_PREDICATE, fieldFilter.apply(randomAlphaOfLengthBetween(3, 6))); } @@ -461,13 +533,18 @@ public void testValidateForFipsKeystoreWithImplicitJksType() { final Settings settings = Settings.builder() .put(XPackSettings.FIPS_MODE_ENABLED.getKey(), true) .put("xpack.security.transport.ssl.keystore.path", "path/to/keystore") - .put(XPackSettings.PASSWORD_HASHING_ALGORITHM.getKey(), - randomFrom(Hasher.getAvailableAlgoStoredHash().stream() - .filter(alg -> alg.startsWith("pbkdf2") == false).collect(Collectors.toList()))) + .put( + XPackSettings.PASSWORD_HASHING_ALGORITHM.getKey(), + randomFrom( + Hasher.getAvailableAlgoStoredHash() + .stream() + .filter(alg -> alg.startsWith("pbkdf2") == false) + .collect(Collectors.toList()) + ) + ) .build(); - final IllegalArgumentException iae = - expectThrows(IllegalArgumentException.class, () -> Security.validateForFips(settings)); - assertThat(iae.getMessage(), containsString("JKS Keystores cannot be used in a FIPS 140 compliant JVM")); + final IllegalArgumentException iae = expectThrows(IllegalArgumentException.class, () -> Security.validateForFips(settings)); + assertThat(iae.getMessage(), containsString("JKS Keystores cannot be used in a FIPS 140 compliant JVM")); } public void testValidateForFipsKeystoreWithExplicitJksType() { @@ -475,24 +552,31 @@ public void testValidateForFipsKeystoreWithExplicitJksType() { .put(XPackSettings.FIPS_MODE_ENABLED.getKey(), true) .put("xpack.security.transport.ssl.keystore.path", "path/to/keystore") .put("xpack.security.transport.ssl.keystore.type", "JKS") - .put(XPackSettings.PASSWORD_HASHING_ALGORITHM.getKey(), - randomFrom(Hasher.getAvailableAlgoStoredHash().stream() - .filter(alg -> alg.startsWith("pbkdf2")).collect(Collectors.toList()))) + .put( + XPackSettings.PASSWORD_HASHING_ALGORITHM.getKey(), + randomFrom( + Hasher.getAvailableAlgoStoredHash().stream().filter(alg -> alg.startsWith("pbkdf2")).collect(Collectors.toList()) + ) + ) .build(); - final IllegalArgumentException iae = - expectThrows(IllegalArgumentException.class, () -> Security.validateForFips(settings)); + final IllegalArgumentException iae = expectThrows(IllegalArgumentException.class, () -> Security.validateForFips(settings)); assertThat(iae.getMessage(), containsString("JKS Keystores cannot be used in a FIPS 140 compliant JVM")); } public void testValidateForFipsInvalidPasswordHashingAlgorithm() { final Settings settings = Settings.builder() .put(XPackSettings.FIPS_MODE_ENABLED.getKey(), true) - .put(XPackSettings.PASSWORD_HASHING_ALGORITHM.getKey(), - randomFrom(Hasher.getAvailableAlgoStoredHash().stream() - .filter(alg -> alg.startsWith("pbkdf2") == false).collect(Collectors.toList()))) + .put( + XPackSettings.PASSWORD_HASHING_ALGORITHM.getKey(), + randomFrom( + Hasher.getAvailableAlgoStoredHash() + .stream() + .filter(alg -> alg.startsWith("pbkdf2") == false) + .collect(Collectors.toList()) + ) + ) .build(); - final IllegalArgumentException iae = - expectThrows(IllegalArgumentException.class, () -> Security.validateForFips(settings)); + final IllegalArgumentException iae = expectThrows(IllegalArgumentException.class, () -> Security.validateForFips(settings)); assertThat(iae.getMessage(), containsString("Only PBKDF2 is allowed for password hashing in a FIPS 140 JVM.")); } @@ -501,12 +585,17 @@ public void testValidateForFipsMultipleValidationErrors() { .put(XPackSettings.FIPS_MODE_ENABLED.getKey(), true) .put("xpack.security.transport.ssl.keystore.path", "path/to/keystore") .put("xpack.security.transport.ssl.keystore.type", "JKS") - .put(XPackSettings.PASSWORD_HASHING_ALGORITHM.getKey(), - randomFrom(Hasher.getAvailableAlgoStoredHash().stream() - .filter(alg -> alg.startsWith("pbkdf2") == false).collect(Collectors.toList()))) + .put( + XPackSettings.PASSWORD_HASHING_ALGORITHM.getKey(), + randomFrom( + Hasher.getAvailableAlgoStoredHash() + .stream() + .filter(alg -> alg.startsWith("pbkdf2") == false) + .collect(Collectors.toList()) + ) + ) .build(); - final IllegalArgumentException iae = - expectThrows(IllegalArgumentException.class, () -> Security.validateForFips(settings)); + final IllegalArgumentException iae = expectThrows(IllegalArgumentException.class, () -> Security.validateForFips(settings)); assertThat(iae.getMessage(), containsString("JKS Keystores cannot be used in a FIPS 140 compliant JVM")); assertThat(iae.getMessage(), containsString("Only PBKDF2 is allowed for password hashing in a FIPS 140 JVM.")); } @@ -516,18 +605,19 @@ public void testValidateForFipsNoErrors() { .put(XPackSettings.FIPS_MODE_ENABLED.getKey(), true) .put("xpack.security.transport.ssl.keystore.path", "path/to/keystore") .put("xpack.security.transport.ssl.keystore.type", "BCFKS") - .put(XPackSettings.PASSWORD_HASHING_ALGORITHM.getKey(), - randomFrom(Hasher.getAvailableAlgoStoredHash().stream() - .filter(alg -> alg.startsWith("pbkdf2")).collect(Collectors.toList()))) + .put( + XPackSettings.PASSWORD_HASHING_ALGORITHM.getKey(), + randomFrom( + Hasher.getAvailableAlgoStoredHash().stream().filter(alg -> alg.startsWith("pbkdf2")).collect(Collectors.toList()) + ) + ) .build(); Security.validateForFips(settings); // no exception thrown } public void testValidateForFipsNoErrorsForDefaultSettings() { - final Settings settings = Settings.builder() - .put(XPackSettings.FIPS_MODE_ENABLED.getKey(), true) - .build(); + final Settings settings = Settings.builder().put(XPackSettings.FIPS_MODE_ENABLED.getKey(), true).build(); Security.validateForFips(settings); // no exception thrown } @@ -546,18 +636,13 @@ public void testLicenseUpdateFailureHandlerUpdate() throws Exception { assertNotNull(service); RestRequest request = new FakeRestRequest(); final AtomicBoolean completed = new AtomicBoolean(false); - service.authenticate(request, ActionListener.wrap(result -> { - assertTrue(completed.compareAndSet(false, true)); - }, e -> { + service.authenticate(request, ActionListener.wrap(result -> { assertTrue(completed.compareAndSet(false, true)); }, e -> { // On trial license, kerberos is allowed and the WWW-Authenticate response header should reflect that verifyHasAuthenticationHeaderValue(e, "Basic realm=\"" + XPackField.SECURITY + "\" charset=\"UTF-8\"", "Negotiate", "ApiKey"); })); threadContext.stashContext(); - licenseState.update( - randomFrom(License.OperationMode.GOLD, License.OperationMode.BASIC), true, null); - service.authenticate(request, ActionListener.wrap(result -> { - assertTrue(completed.compareAndSet(false, true)); - }, e -> { + licenseState.update(randomFrom(License.OperationMode.GOLD, License.OperationMode.BASIC), true, null); + service.authenticate(request, ActionListener.wrap(result -> { assertTrue(completed.compareAndSet(false, true)); }, e -> { // On basic or gold license, kerberos is not allowed and the WWW-Authenticate response header should also reflect that verifyHasAuthenticationHeaderValue(e, "Basic realm=\"" + XPackField.SECURITY + "\" charset=\"UTF-8\"", "ApiKey"); })); @@ -567,10 +652,7 @@ public void testLicenseUpdateFailureHandlerUpdate() throws Exception { } public void testSecurityPluginInstallsRestHandlerWrapperEvenIfSecurityIsDisabled() throws IllegalAccessException { - Settings settings = Settings.builder() - .put("xpack.security.enabled", false) - .put("path.home", createTempDir()) - .build(); + Settings settings = Settings.builder().put("xpack.security.enabled", false).put("path.home", createTempDir()).build(); SettingsModule settingsModule = new SettingsModule(Settings.EMPTY); ThreadPool threadPool = new TestThreadPool(getTestName()); @@ -592,10 +674,7 @@ public void testSecurityRestHandlerWrapperCanBeInstalled() throws IllegalAccessE Loggers.addAppender(amLogger, appender); appender.start(); - Settings settings = Settings.builder() - .put("xpack.security.enabled", false) - .put("path.home", createTempDir()) - .build(); + Settings settings = Settings.builder().put("xpack.security.enabled", false).put("path.home", createTempDir()).build(); SettingsModule settingsModule = new SettingsModule(Settings.EMPTY); ThreadPool threadPool = new TestThreadPool(getTestName()); @@ -605,15 +684,28 @@ public void testSecurityRestHandlerWrapperCanBeInstalled() throws IllegalAccessE // Verify Security rest wrapper is about to be installed // We will throw later if another wrapper is already installed - appender.addExpectation(new MockLogAppender.SeenEventExpectation( - "Security rest wrapper", ActionModule.class.getName(), Level.DEBUG, - "Using REST wrapper from plugin org.elasticsearch.xpack.security.Security" - )); - - ActionModule actionModule = new ActionModule(settingsModule.getSettings(), + appender.addExpectation( + new MockLogAppender.SeenEventExpectation( + "Security rest wrapper", + ActionModule.class.getName(), + Level.DEBUG, + "Using REST wrapper from plugin org.elasticsearch.xpack.security.Security" + ) + ); + + ActionModule actionModule = new ActionModule( + settingsModule.getSettings(), TestIndexNameExpressionResolver.newInstance(threadPool.getThreadContext()), - settingsModule.getIndexScopedSettings(), settingsModule.getClusterSettings(), settingsModule.getSettingsFilter(), - threadPool, Arrays.asList(security), null, null, usageService, null); + settingsModule.getIndexScopedSettings(), + settingsModule.getClusterSettings(), + settingsModule.getSettingsFilter(), + threadPool, + Arrays.asList(security), + null, + null, + usageService, + null + ); actionModule.initRestHandlers(null); appender.assertAllExpectationsMatched(); @@ -624,7 +716,7 @@ public void testSecurityRestHandlerWrapperCanBeInstalled() throws IllegalAccessE } } - public void testSecurityStatusMessageInLog() throws Exception{ + public void testSecurityStatusMessageInLog() throws Exception { final Logger mockLogger = LogManager.getLogger(Security.class); boolean securityEnabled = true; Loggers.setLevel(mockLogger, Level.INFO); @@ -632,8 +724,7 @@ public void testSecurityStatusMessageInLog() throws Exception{ Loggers.addAppender(mockLogger, appender); appender.start(); - Settings.Builder settings = Settings.builder() - .put("path.home", createTempDir()); + Settings.Builder settings = Settings.builder().put("path.home", createTempDir()); if (randomBoolean()) { // randomize explicit vs implicit configuration securityEnabled = randomBoolean(); @@ -641,10 +732,14 @@ public void testSecurityStatusMessageInLog() throws Exception{ } try { - appender.addExpectation(new MockLogAppender.SeenEventExpectation( - "message", Security.class.getName(), Level.INFO, - "Security is " + (securityEnabled ? "enabled" : "disabled") - )); + appender.addExpectation( + new MockLogAppender.SeenEventExpectation( + "message", + Security.class.getName(), + Level.INFO, + "Security is " + (securityEnabled ? "enabled" : "disabled") + ) + ); createComponents(settings.build()); appender.assertAllExpectationsMatched(); } finally { diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/TokenSSLBootsrapCheckTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/TokenSSLBootsrapCheckTests.java index ecaf92f9d25a9..76b7d0f9bb4ee 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/TokenSSLBootsrapCheckTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/TokenSSLBootsrapCheckTests.java @@ -25,8 +25,9 @@ public void testTokenSSLBootstrapCheck() { assertTrue(new TokenSSLBootstrapCheck().check(createTestContext(settings, null)).isFailure()); settings = Settings.builder() - .put(XPackSettings.HTTP_SSL_ENABLED.getKey(), true) - .put(XPackSettings.TOKEN_SERVICE_ENABLED_SETTING.getKey(), true).build(); + .put(XPackSettings.HTTP_SSL_ENABLED.getKey(), true) + .put(XPackSettings.TOKEN_SERVICE_ENABLED_SETTING.getKey(), true) + .build(); assertTrue(new TokenSSLBootstrapCheck().check(createTestContext(settings, null)).isSuccess()); } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/SecurityActionMapperTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/SecurityActionMapperTests.java index 29e5455a7f95c..9f8ee7249b426 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/SecurityActionMapperTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/SecurityActionMapperTests.java @@ -39,9 +39,12 @@ public void testThatAllOrdinaryActionsRemainTheSame() { actionNameBuilder.append(randomAlphaOfLengthBetween(2, 12)); } String randomAction = actionNameBuilder.toString(); - assumeFalse("Random action is one of the known mapped values: " + randomAction, randomAction.equals(ClearScrollAction.NAME) || - randomAction.equals(AnalyzeAction.NAME) || - randomAction.equals(AnalyzeAction.NAME + "[s]")); + assumeFalse( + "Random action is one of the known mapped values: " + randomAction, + randomAction.equals(ClearScrollAction.NAME) + || randomAction.equals(AnalyzeAction.NAME) + || randomAction.equals(AnalyzeAction.NAME + "[s]") + ); assertThat(securityActionMapper.action(randomAction, null), equalTo(randomAction)); } @@ -64,11 +67,13 @@ public void testClearScrollAll() { clearScrollRequest.addScrollId(randomAlphaOfLength(randomIntBetween(1, 30))); } clearScrollRequest.addScrollId("_all"); - //make sure that wherever the _all is among the scroll ids the action name gets translated + // make sure that wherever the _all is among the scroll ids the action name gets translated Collections.shuffle(clearScrollRequest.getScrollIds(), random()); - assertThat(securityActionMapper.action(ClearScrollAction.NAME, clearScrollRequest), - equalTo(SecurityActionMapper.CLUSTER_PERMISSION_SCROLL_CLEAR_ALL_NAME)); + assertThat( + securityActionMapper.action(ClearScrollAction.NAME, clearScrollRequest), + equalTo(SecurityActionMapper.CLUSTER_PERMISSION_SCROLL_CLEAR_ALL_NAME) + ); } public void testIndicesAnalyze() { @@ -86,7 +91,9 @@ public void testIndicesAnalyze() { public void testClusterAnalyze() { SecurityActionMapper securityActionMapper = new SecurityActionMapper(); AnalyzeAction.Request analyzeRequest = new AnalyzeAction.Request(null).text("text"); - assertThat(securityActionMapper.action(AnalyzeAction.NAME, analyzeRequest), - equalTo(SecurityActionMapper.CLUSTER_PERMISSION_ANALYZE)); + assertThat( + securityActionMapper.action(AnalyzeAction.NAME, analyzeRequest), + equalTo(SecurityActionMapper.CLUSTER_PERMISSION_ANALYZE) + ); } } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/TransportGrantApiKeyActionTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/TransportGrantApiKeyActionTests.java index 234cb94ce9a7e..cc95e977e9c97 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/TransportGrantApiKeyActionTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/TransportGrantApiKeyActionTests.java @@ -64,8 +64,14 @@ public void setupMocks() throws Exception { tokenServiceMock = SecurityMocks.tokenService(true, threadPool); final ThreadContext threadContext = threadPool.getThreadContext(); - action = new TransportGrantApiKeyAction(mock(TransportService.class), mock(ActionFilters.class), threadContext, - apiKeyGenerator, authenticationService, tokenServiceMock.tokenService); + action = new TransportGrantApiKeyAction( + mock(TransportService.class), + mock(ActionFilters.class), + threadContext, + apiKeyGenerator, + authenticationService, + tokenServiceMock.tokenService + ); } @After @@ -200,13 +206,16 @@ public void testGrantApiKeyWithInvalidatedAccessToken() throws Exception { } private Authentication buildAuthentication(String username) { - return new Authentication(new User(username), - new Authentication.RealmRef("realm_name", "realm_type", "node_name"), null); + return new Authentication(new User(username), new Authentication.RealmRef("realm_name", "realm_type", "node_name"), null); } private CreateApiKeyResponse mockResponse(GrantApiKeyRequest request) { - return new CreateApiKeyResponse(request.getApiKeyRequest().getName(), - randomAlphaOfLength(12), new SecureString(randomAlphaOfLength(18).toCharArray()), null); + return new CreateApiKeyResponse( + request.getApiKeyRequest().getName(), + randomAlphaOfLength(12), + new SecureString(randomAlphaOfLength(18).toCharArray()), + null + ); } private GrantApiKeyRequest mockRequest() { diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/apikey/TransportQueryApiKeyActionTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/apikey/TransportQueryApiKeyActionTests.java index 8be82a390aebf..8a68bc38f1b62 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/apikey/TransportQueryApiKeyActionTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/apikey/TransportQueryApiKeyActionTests.java @@ -32,10 +32,12 @@ public void testTranslateFieldSortBuilders() { "creation", "expiration", "invalidated", - "metadata." + randomAlphaOfLengthBetween(3, 8)); + "metadata." + randomAlphaOfLengthBetween(3, 8) + ); - final List originals = - fieldNames.stream().map(this::randomFieldSortBuilderWithName).collect(Collectors.toUnmodifiableList()); + final List originals = fieldNames.stream() + .map(this::randomFieldSortBuilderWithName) + .collect(Collectors.toUnmodifiableList()); final SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.searchSource(); TransportQueryApiKeyAction.translateFieldSortBuilders(originals, searchSourceBuilder); @@ -76,7 +78,8 @@ public void testNestedSortingIsNotAllowed() { fieldSortBuilder.setNestedSort(new NestedSortBuilder("name")); final IllegalArgumentException e = expectThrows( IllegalArgumentException.class, - () -> TransportQueryApiKeyAction.translateFieldSortBuilders(List.of(fieldSortBuilder), SearchSourceBuilder.searchSource())); + () -> TransportQueryApiKeyAction.translateFieldSortBuilders(List.of(fieldSortBuilder), SearchSourceBuilder.searchSource()) + ); assertThat(e.getMessage(), equalTo("nested sorting is not supported for API Key query")); } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/enrollment/TransportKibanaEnrollmentActionTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/enrollment/TransportKibanaEnrollmentActionTests.java index 3fdfe7c553b61..ce26c4a7992d9 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/enrollment/TransportKibanaEnrollmentActionTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/enrollment/TransportKibanaEnrollmentActionTests.java @@ -15,8 +15,8 @@ import org.elasticsearch.common.settings.MockSecureSettings; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.common.ssl.SslConfiguration; +import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.env.Environment; import org.elasticsearch.tasks.Task; import org.elasticsearch.test.ESTestCase; @@ -57,11 +57,13 @@ public class TransportKibanaEnrollmentActionTests extends ESTestCase { private static final SecureString TOKEN_VALUE = new SecureString("token-value".toCharArray()); @BeforeClass - public static void muteInFips(){ + public static void muteInFips() { assumeFalse("Enrollment is not supported in FIPS 140-2 as we are using PKCS#12 keystores", inFipsJvm()); } - @Before @SuppressWarnings("unchecked") public void setup() throws Exception { + @Before + @SuppressWarnings("unchecked") + public void setup() throws Exception { createServiceAccountTokenRequests = new ArrayList<>(); final Environment env = mock(Environment.class); final Path tempDir = createTempDir(); @@ -70,10 +72,7 @@ public static void muteInFips(){ when(env.configFile()).thenReturn(tempDir); final MockSecureSettings secureSettings = new MockSecureSettings(); secureSettings.setString("keystore.secure_password", "password"); - final Settings settings = Settings.builder() - .put("keystore.path", httpCaPath) - .setSecureSettings(secureSettings) - .build(); + final Settings settings = Settings.builder().put("keystore.path", httpCaPath).setSecureSettings(secureSettings).build(); when(env.settings()).thenReturn(settings); final SSLService sslService = mock(SSLService.class); final SslConfiguration sslConfiguration = SslSettingsLoader.load(settings, null, env); @@ -84,21 +83,23 @@ public static void muteInFips(){ client = mock(Client.class); when(client.threadPool()).thenReturn(threadPool); doAnswer(invocation -> { - CreateServiceAccountTokenRequest createServiceAccountTokenRequest = - (CreateServiceAccountTokenRequest) invocation.getArguments()[1]; + CreateServiceAccountTokenRequest createServiceAccountTokenRequest = (CreateServiceAccountTokenRequest) invocation + .getArguments()[1]; createServiceAccountTokenRequests.add(createServiceAccountTokenRequest); ActionListener listener = (ActionListener) invocation.getArguments()[2]; listener.onResponse(CreateServiceAccountTokenResponse.created(TOKEN_NAME, TOKEN_VALUE)); return null; }).when(client).execute(eq(CreateServiceAccountTokenAction.INSTANCE), any(), any()); - final TransportService transportService = new TransportService(Settings.EMPTY, + final TransportService transportService = new TransportService( + Settings.EMPTY, mock(Transport.class), threadPool, TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, - Collections.emptySet()); + Collections.emptySet() + ); action = new TransportKibanaEnrollmentAction(transportService, client, sslService, mock(ActionFilters.class)); } @@ -111,11 +112,11 @@ public void testKibanaEnrollment() { assertThat( response.getHttpCa(), startsWith( - "MIIDSjCCAjKgAwIBAgIVALCgZXvbceUrjJaQMheDCX0kXnRJMA0GCSqGSIb3DQEBCwUAMDQxMjAw" + - "BgNVBAMTKUVsYXN0aWMgQ2VydGlmaWNhdGUgVG9vbCBBdXRvZ2VuZXJhdGVkIENBMB4XDTIx" + - "MDQyODEyNTY0MVoXDTI0MDQyNzEyNTY0MVowNDEyMDAGA1UEAxMpRWxhc3RpYyBDZXJ0aWZp" + - "Y2F0ZSBUb29sIEF1dG9nZW5lcmF0ZWQgQ0EwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK" + - "AoIBAQCCJbOU4JvxDD/F" + "MIIDSjCCAjKgAwIBAgIVALCgZXvbceUrjJaQMheDCX0kXnRJMA0GCSqGSIb3DQEBCwUAMDQxMjAw" + + "BgNVBAMTKUVsYXN0aWMgQ2VydGlmaWNhdGUgVG9vbCBBdXRvZ2VuZXJhdGVkIENBMB4XDTIx" + + "MDQyODEyNTY0MVoXDTI0MDQyNzEyNTY0MVowNDEyMDAGA1UEAxMpRWxhc3RpYyBDZXJ0aWZp" + + "Y2F0ZSBUb29sIEF1dG9nZW5lcmF0ZWQgQ0EwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK" + + "AoIBAQCCJbOU4JvxDD/F" ) ); assertThat(response.getTokenValue(), equalTo(TOKEN_VALUE)); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/enrollment/TransportNodeEnrollmentActionTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/enrollment/TransportNodeEnrollmentActionTests.java index 4e160f1feabb1..547e91f3e639a 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/enrollment/TransportNodeEnrollmentActionTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/enrollment/TransportNodeEnrollmentActionTests.java @@ -78,10 +78,7 @@ public void testDoExecute() throws Exception { final SSLService sslService = mock(SSLService.class); final MockSecureSettings secureSettings = new MockSecureSettings(); secureSettings.setString("keystore.secure_password", "password"); - final Settings httpSettings = Settings.builder() - .put("keystore.path", httpCaPath) - .setSecureSettings(secureSettings) - .build(); + final Settings httpSettings = Settings.builder().put("keystore.path", httpCaPath).setSecureSettings(secureSettings).build(); final SslConfiguration httpSslConfiguration = SslSettingsLoader.load(httpSettings, null, env); when(sslService.getHttpTransportSSLConfiguration()).thenReturn(httpSslConfiguration); final Settings transportSettings = Settings.builder() @@ -100,20 +97,24 @@ public void testDoExecute() throws Exception { final List nodesInfoRequests = new ArrayList<>(); for (int i = 0; i < numberOfNodes; i++) { DiscoveryNode n = node(i); - nodeInfos.add(new NodeInfo(Version.CURRENT, - null, - n, - null, - null, - null, - null, - null, - new TransportInfo(new BoundTransportAddress(new TransportAddress[] { n.getAddress() }, n.getAddress()), null, false), - null, - null, - null, - null, - null)); + nodeInfos.add( + new NodeInfo( + Version.CURRENT, + null, + n, + null, + null, + null, + null, + null, + new TransportInfo(new BoundTransportAddress(new TransportAddress[] { n.getAddress() }, n.getAddress()), null, false), + null, + null, + null, + null, + null + ) + ); } doAnswer(invocation -> { NodesInfoRequest nodesInfoRequest = (NodesInfoRequest) invocation.getArguments()[1]; @@ -123,16 +124,22 @@ public void testDoExecute() throws Exception { return null; }).when(client).execute(same(NodesInfoAction.INSTANCE), any(), any()); - final TransportService transportService = new TransportService(Settings.EMPTY, + final TransportService transportService = new TransportService( + Settings.EMPTY, mock(Transport.class), threadPool, TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, - Collections.emptySet()); + Collections.emptySet() + ); - final TransportNodeEnrollmentAction action = - new TransportNodeEnrollmentAction(transportService, sslService, client, mock(ActionFilters.class)); + final TransportNodeEnrollmentAction action = new TransportNodeEnrollmentAction( + transportService, + sslService, + client, + mock(ActionFilters.class) + ); final NodeEnrollmentRequest request = new NodeEnrollmentRequest(); final PlainActionFuture future = new PlainActionFuture<>(); action.doExecute(mock(Task.class), request, future); @@ -142,14 +149,17 @@ public void testDoExecute() throws Exception { assertThat(response.getNodesAddresses(), hasSize(numberOfNodes)); assertThat(nodesInfoRequests, hasSize(1)); - assertWarnings("[keystore.password] setting was deprecated in Elasticsearch and will be removed in a future release! " + - "See the breaking changes documentation for the next major version."); + assertWarnings( + "[keystore.password] setting was deprecated in Elasticsearch and will be removed in a future release! " + + "See the breaking changes documentation for the next major version." + ); } - private void assertSameCertificate(String cert, Path original, char[] originalPassword, boolean isCa) throws Exception{ + private void assertSameCertificate(String cert, Path original, char[] originalPassword, boolean isCa) throws Exception { Map originalKeysAndCerts = CertParsingUtils.readPkcs12KeyPairs(original, originalPassword, p -> originalPassword); Certificate deserializedCert = CertParsingUtils.readCertificates( - new ByteArrayInputStream(Base64.getDecoder().decode(cert.getBytes(StandardCharsets.UTF_8)))).get(0); + new ByteArrayInputStream(Base64.getDecoder().decode(cert.getBytes(StandardCharsets.UTF_8))) + ).get(0); assertThat(originalKeysAndCerts, hasKey(deserializedCert)); assertThat(deserializedCert, instanceOf(X509Certificate.class)); if (isCa) { diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/filter/SecurityActionFilterTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/filter/SecurityActionFilterTests.java index 35dadfd0b1f71..32af91cad3238 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/filter/SecurityActionFilterTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/filter/SecurityActionFilterTests.java @@ -62,7 +62,7 @@ import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; -@SuppressWarnings({"unchecked", "rawtypes"}) +@SuppressWarnings({ "unchecked", "rawtypes" }) public class SecurityActionFilterTests extends ESTestCase { private AuthenticationService authcService; private AuthorizationService authzService; @@ -87,20 +87,28 @@ public void init() throws Exception { threadContext = new ThreadContext(Settings.EMPTY); when(threadPool.getThreadContext()).thenReturn(threadContext); failDestructiveOperations = randomBoolean(); - Settings settings = Settings.builder() - .put(DestructiveOperations.REQUIRES_NAME_SETTING.getKey(), failDestructiveOperations).build(); - DestructiveOperations destructiveOperations = new DestructiveOperations(settings, - new ClusterSettings(settings, Collections.singleton(DestructiveOperations.REQUIRES_NAME_SETTING))); + Settings settings = Settings.builder().put(DestructiveOperations.REQUIRES_NAME_SETTING.getKey(), failDestructiveOperations).build(); + DestructiveOperations destructiveOperations = new DestructiveOperations( + settings, + new ClusterSettings(settings, Collections.singleton(DestructiveOperations.REQUIRES_NAME_SETTING)) + ); ClusterState state = mock(ClusterState.class); DiscoveryNodes nodes = DiscoveryNodes.builder() - .add(new DiscoveryNode("id1", buildNewFakeTransportAddress(), Version.CURRENT)) - .add(new DiscoveryNode("id2", buildNewFakeTransportAddress(), Version.CURRENT.minimumCompatibilityVersion())) - .build(); + .add(new DiscoveryNode("id1", buildNewFakeTransportAddress(), Version.CURRENT)) + .add(new DiscoveryNode("id2", buildNewFakeTransportAddress(), Version.CURRENT.minimumCompatibilityVersion())) + .build(); when(state.nodes()).thenReturn(nodes); SecurityContext securityContext = new SecurityContext(settings, threadContext); - filter = new SecurityActionFilter(authcService, authzService, auditTrailService, licenseState, threadPool, - securityContext, destructiveOperations); + filter = new SecurityActionFilter( + authcService, + authzService, + auditTrailService, + licenseState, + threadPool, + securityContext, + destructiveOperations + ); } public void testApply() throws Exception { @@ -201,8 +209,9 @@ public void testApplyAsSystemUser() throws Exception { public void testApplyDestructiveOperations() throws Exception { ActionRequest request = new MockIndicesRequest( - IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean()), - randomFrom("*", "_all", "test*")); + IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean()), + randomFrom("*", "_all", "test*") + ); String action = randomFrom(CloseIndexAction.NAME, OpenIndexAction.NAME, DeleteIndexAction.NAME); ActionListener listener = mock(ActionListener.class); Task task = mock(Task.class); @@ -225,8 +234,7 @@ public void testApplyDestructiveOperations() throws Exception { ActionListener callback = (ActionListener) i.getArguments()[3]; callback.onResponse(null); return Void.TYPE; - }).when(authzService) - .authorize(any(Authentication.class), any(String.class), any(TransportRequest.class), anyActionListener()); + }).when(authzService).authorize(any(Authentication.class), any(String.class), any(TransportRequest.class), anyActionListener()); filter.apply(task, action, request, listener, chain); if (failDestructiveOperations) { verify(listener).onFailure(isA(IllegalArgumentException.class)); @@ -234,8 +242,13 @@ public void testApplyDestructiveOperations() throws Exception { } else { verify(authzService).authorize(eq(authentication), eq(action), eq(request), anyActionListener()); verify(chain).proceed(eq(task), eq(action), eq(request), anyActionListener()); - verify(auditTrail).coordinatingActionResponse(eq(requestIdFromAuthn.get()), eq(authentication), eq(action), eq(request), - eq(actionResponse)); + verify(auditTrail).coordinatingActionResponse( + eq(requestIdFromAuthn.get()), + eq(authentication), + eq(action), + eq(request), + eq(actionResponse) + ); } } @@ -263,8 +276,7 @@ public void testActionProcessException() throws Exception { ActionListener callback = (ActionListener) i.getArguments()[3]; callback.onFailure(exception); return Void.TYPE; - }).when(authzService) - .authorize(eq(authentication), eq("_action"), eq(request), anyActionListener()); + }).when(authzService).authorize(eq(authentication), eq("_action"), eq(request), anyActionListener()); } filter.apply(task, "_action", request, listener, chain); verify(listener).onFailure(exception); @@ -298,8 +310,7 @@ private void mockAuthorize(IndicesAccessControl indicesAccessControl) { threadContext.putTransient(INDICES_PERMISSIONS_KEY, indicesAccessControl); callback.onResponse(null); return Void.TYPE; - }).when(authzService) - .authorize(any(Authentication.class), any(String.class), any(TransportRequest.class), anyActionListener()); + }).when(authzService).authorize(any(Authentication.class), any(String.class), any(TransportRequest.class), anyActionListener()); } private void mockChain(Task task, String action, ActionRequest request, ActionResponse actionResponse) { diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/oidc/OpenIdConnectAuthenticateRequestTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/oidc/OpenIdConnectAuthenticateRequestTests.java index 45b543d0dacbf..f84ddd22ef239 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/oidc/OpenIdConnectAuthenticateRequestTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/oidc/OpenIdConnectAuthenticateRequestTests.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.xpack.security.action.oidc; - import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.test.ESTestCase; diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/oidc/OpenIdConnectPrepareAuthenticationRequestTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/oidc/OpenIdConnectPrepareAuthenticationRequestTests.java index 951fe20e40b34..966f8083b3a43 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/oidc/OpenIdConnectPrepareAuthenticationRequestTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/oidc/OpenIdConnectPrepareAuthenticationRequestTests.java @@ -25,8 +25,9 @@ public void testSerialization() throws IOException { final BytesStreamOutput out = new BytesStreamOutput(); request.writeTo(out); - final OpenIdConnectPrepareAuthenticationRequest deserialized = - new OpenIdConnectPrepareAuthenticationRequest(out.bytes().streamInput()); + final OpenIdConnectPrepareAuthenticationRequest deserialized = new OpenIdConnectPrepareAuthenticationRequest( + out.bytes().streamInput() + ); assertThat(deserialized.getRealmName(), equalTo("oidc-realm1")); final OpenIdConnectPrepareAuthenticationRequest request2 = new OpenIdConnectPrepareAuthenticationRequest(); @@ -34,8 +35,9 @@ public void testSerialization() throws IOException { final BytesStreamOutput out2 = new BytesStreamOutput(); request2.writeTo(out2); - final OpenIdConnectPrepareAuthenticationRequest deserialized2 = - new OpenIdConnectPrepareAuthenticationRequest(out2.bytes().streamInput()); + final OpenIdConnectPrepareAuthenticationRequest deserialized2 = new OpenIdConnectPrepareAuthenticationRequest( + out2.bytes().streamInput() + ); assertThat(deserialized2.getIssuer(), equalTo("https://op.company.org/")); } @@ -49,8 +51,9 @@ public void testSerializationWithStateAndNonce() throws IOException { final BytesStreamOutput out = new BytesStreamOutput(); request.writeTo(out); - final OpenIdConnectPrepareAuthenticationRequest deserialized = - new OpenIdConnectPrepareAuthenticationRequest(out.bytes().streamInput()); + final OpenIdConnectPrepareAuthenticationRequest deserialized = new OpenIdConnectPrepareAuthenticationRequest( + out.bytes().streamInput() + ); assertThat(deserialized.getRealmName(), equalTo("oidc-realm1")); assertThat(deserialized.getState(), equalTo(state)); assertThat(deserialized.getNonce(), equalTo(nonce)); @@ -69,7 +72,9 @@ public void testValidation() { final ActionRequestValidationException validation2 = request2.validate(); assertNotNull(validation2); assertThat(validation2.validationErrors(), hasSize(1)); - assertThat(validation2.validationErrors().get(0), - containsString("only one of [realm, issuer] can be provided in the same request")); + assertThat( + validation2.validationErrors().get(0), + containsString("only one of [realm, issuer] can be provided in the same request") + ); } } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/oidc/TransportOpenIdConnectLogoutActionTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/oidc/TransportOpenIdConnectLogoutActionTests.java index c2e981bcbdabb..75f6d54f203d5 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/oidc/TransportOpenIdConnectLogoutActionTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/oidc/TransportOpenIdConnectLogoutActionTests.java @@ -75,10 +75,10 @@ import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.startsWith; -import static org.mockito.ArgumentMatchers.nullable; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.nullable; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -95,8 +95,7 @@ public class TransportOpenIdConnectLogoutActionTests extends OpenIdConnectTestCa @Before public void setup() throws Exception { final RealmConfig.RealmIdentifier realmIdentifier = new RealmConfig.RealmIdentifier("oidc", REALM_NAME); - final Settings settings = getBasicRealmSettings() - .put(XPackSettings.TOKEN_SERVICE_ENABLED_SETTING.getKey(), true) + final Settings settings = getBasicRealmSettings().put(XPackSettings.TOKEN_SERVICE_ENABLED_SETTING.getKey(), true) .put("path.home", createTempDir()) .put(RealmSettings.getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0) .build(); @@ -115,8 +114,7 @@ public void setup() throws Exception { when(client.settings()).thenReturn(settings); doAnswer(invocationOnMock -> { GetRequestBuilder builder = new GetRequestBuilder(client, GetAction.INSTANCE); - builder.setIndex((String) invocationOnMock.getArguments()[0]) - .setId((String) invocationOnMock.getArguments()[1]); + builder.setIndex((String) invocationOnMock.getArguments()[0]).setId((String) invocationOnMock.getArguments()[1]); return builder; }).when(client).prepareGet(nullable(String.class), nullable(String.class)); doAnswer(invocationOnMock -> { @@ -126,8 +124,7 @@ public void setup() throws Exception { }).when(client).prepareIndex(nullable(String.class)); doAnswer(invocationOnMock -> { UpdateRequestBuilder builder = new UpdateRequestBuilder(client, UpdateAction.INSTANCE); - builder.setIndex((String) invocationOnMock.getArguments()[0]) - .setId((String) invocationOnMock.getArguments()[1]); + builder.setIndex((String) invocationOnMock.getArguments()[0]).setId((String) invocationOnMock.getArguments()[1]); return builder; }).when(client).prepareUpdate(nullable(String.class), anyString()); doAnswer(invocationOnMock -> { @@ -139,8 +136,7 @@ public void setup() throws Exception { @SuppressWarnings("unchecked") ActionListener listener = (ActionListener) invocationOnMock.getArguments()[1]; indexRequests.add(indexRequest); - final IndexResponse response = new IndexResponse( - indexRequest.shardId(), indexRequest.id(), 1, 1, 1, true); + final IndexResponse response = new IndexResponse(indexRequest.shardId(), indexRequest.id(), 1, 1, 1, true); listener.onResponse(response); return Void.TYPE; }).when(client).index(any(IndexRequest.class), anyActionListener()); @@ -149,8 +145,7 @@ public void setup() throws Exception { @SuppressWarnings("unchecked") ActionListener listener = (ActionListener) invocationOnMock.getArguments()[2]; indexRequests.add(indexRequest); - final IndexResponse response = new IndexResponse( - new ShardId("test", "test", 0), indexRequest.id(), 1, 1, 1, true); + final IndexResponse response = new IndexResponse(new ShardId("test", "test", 0), indexRequest.id(), 1, 1, 1, true); listener.onResponse(response); return Void.TYPE; }).when(client).execute(eq(IndexAction.INSTANCE), any(IndexRequest.class), anyActionListener()); @@ -181,32 +176,57 @@ public void setup() throws Exception { final MockLicenseState licenseState = mock(MockLicenseState.class); when(licenseState.isAllowed(Security.TOKEN_SERVICE_FEATURE)).thenReturn(true); - tokenService = new TokenService(settings, Clock.systemUTC(), client, licenseState, new SecurityContext(settings, threadContext), - securityIndex, securityIndex, clusterService); + tokenService = new TokenService( + settings, + Clock.systemUTC(), + client, + licenseState, + new SecurityContext(settings, threadContext), + securityIndex, + securityIndex, + clusterService + ); - final TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet()); + final TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); final Realms realms = mock(Realms.class); action = new TransportOpenIdConnectLogoutAction(transportService, mock(ActionFilters.class), realms, tokenService); final Environment env = TestEnvironment.newEnvironment(settings); final RealmConfig realmConfig = new RealmConfig(realmIdentifier, settings, env, threadContext); - oidcRealm = new OpenIdConnectRealm(realmConfig, new SSLService(TestEnvironment.newEnvironment(sslSettings)), - mock(UserRoleMapper.class), mock(ResourceWatcherService.class)); + oidcRealm = new OpenIdConnectRealm( + realmConfig, + new SSLService(TestEnvironment.newEnvironment(sslSettings)), + mock(UserRoleMapper.class), + mock(ResourceWatcherService.class) + ); when(realms.realm(realmConfig.name())).thenReturn(oidcRealm); } public void testLogoutInvalidatesTokens() throws Exception { final String subject = randomAlphaOfLength(8); final JWT signedIdToken = generateIdToken(subject, randomAlphaOfLength(8), randomAlphaOfLength(8)); - final User user = new User("oidc-user", new String[]{"superuser"}, null, null, Map.of(), true); + final User user = new User("oidc-user", new String[] { "superuser" }, null, null, Map.of(), true); final Authentication.RealmRef realmRef = new Authentication.RealmRef(oidcRealm.name(), OpenIdConnectRealmSettings.TYPE, "node01"); final Map tokenMetadata = new HashMap<>(); tokenMetadata.put("id_token_hint", signedIdToken.serialize()); tokenMetadata.put("oidc_realm", REALM_NAME); - final Authentication authentication = new Authentication(user, realmRef, null, null, Authentication.AuthenticationType.REALM, - tokenMetadata); + final Authentication authentication = new Authentication( + user, + realmRef, + null, + null, + Authentication.AuthenticationType.REALM, + tokenMetadata + ); final PlainActionFuture future = new PlainActionFuture<>(); final String userTokenId = UUIDs.randomBase64UUID(); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/role/PutRoleBuilderTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/role/PutRoleBuilderTests.java index 67e7215f2c2d3..2988c880aa0ec 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/role/PutRoleBuilderTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/role/PutRoleBuilderTests.java @@ -9,9 +9,9 @@ import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.client.Client; import org.elasticsearch.common.bytes.BytesArray; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.client.NoOpClient; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.security.action.role.PutRoleRequestBuilder; import java.nio.charset.Charset; @@ -27,10 +27,17 @@ public void testBWCFieldPermissions() throws Exception { byte[] bytes = Files.readAllBytes(path); String roleString = new String(bytes, Charset.defaultCharset()); try (Client client = new NoOpClient("testBWCFieldPermissions")) { - ElasticsearchParseException e = expectThrows(ElasticsearchParseException.class, - () -> new PutRoleRequestBuilder(client).source("role1", new BytesArray(roleString), XContentType.JSON)); - assertThat(e.getDetailedMessage(), containsString("\"fields\": [...]] format has changed for field permissions in role " + - "[role1], use [\"field_security\": {\"grant\":[...],\"except\":[...]}] instead")); + ElasticsearchParseException e = expectThrows( + ElasticsearchParseException.class, + () -> new PutRoleRequestBuilder(client).source("role1", new BytesArray(roleString), XContentType.JSON) + ); + assertThat( + e.getDetailedMessage(), + containsString( + "\"fields\": [...]] format has changed for field permissions in role " + + "[role1], use [\"field_security\": {\"grant\":[...],\"except\":[...]}] instead" + ) + ); } } } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/role/TransportDeleteRoleActionTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/role/TransportDeleteRoleActionTests.java index 8b43a55a66e94..c4846ca72f467 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/role/TransportDeleteRoleActionTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/role/TransportDeleteRoleActionTests.java @@ -42,10 +42,16 @@ public class TransportDeleteRoleActionTests extends ESTestCase { public void testReservedRole() { final String roleName = randomFrom(new ArrayList<>(ReservedRolesStore.names())); NativeRolesStore rolesStore = mock(NativeRolesStore.class); - TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, (x) -> null, null, Collections.emptySet()); - TransportDeleteRoleAction action = new TransportDeleteRoleAction(mock(ActionFilters.class), - rolesStore, transportService); + TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + (x) -> null, + null, + Collections.emptySet() + ); + TransportDeleteRoleAction action = new TransportDeleteRoleAction(mock(ActionFilters.class), rolesStore, transportService); DeleteRoleRequest request = new DeleteRoleRequest(); request.name(roleName); @@ -73,10 +79,16 @@ public void onFailure(Exception e) { public void testValidRole() { final String roleName = randomFrom("admin", "dept_a", "restricted"); NativeRolesStore rolesStore = mock(NativeRolesStore.class); - TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, (x) -> null, null, Collections.emptySet()); - TransportDeleteRoleAction action = new TransportDeleteRoleAction(mock(ActionFilters.class), - rolesStore, transportService); + TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + (x) -> null, + null, + Collections.emptySet() + ); + TransportDeleteRoleAction action = new TransportDeleteRoleAction(mock(ActionFilters.class), rolesStore, transportService); DeleteRoleRequest request = new DeleteRoleRequest(); request.name(roleName); @@ -115,8 +127,15 @@ public void testException() { final Exception e = randomFrom(new ElasticsearchSecurityException(""), new IllegalStateException()); final String roleName = randomFrom("admin", "dept_a", "restricted"); NativeRolesStore rolesStore = mock(NativeRolesStore.class); - TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, (x) -> null, null, Collections.emptySet()); + TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + (x) -> null, + null, + Collections.emptySet() + ); TransportDeleteRoleAction action = new TransportDeleteRoleAction(mock(ActionFilters.class), rolesStore, transportService); DeleteRoleRequest request = new DeleteRoleRequest(); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/role/TransportGetRolesActionTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/role/TransportGetRolesActionTests.java index eb8ba4ad9c7c4..fe249baa2143c 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/role/TransportGetRolesActionTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/role/TransportGetRolesActionTests.java @@ -47,10 +47,21 @@ public class TransportGetRolesActionTests extends ESTestCase { public void testReservedRoles() { NativeRolesStore rolesStore = mock(NativeRolesStore.class); - TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet()); - TransportGetRolesAction action = new TransportGetRolesAction(mock(ActionFilters.class), - rolesStore, transportService, new ReservedRolesStore()); + TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); + TransportGetRolesAction action = new TransportGetRolesAction( + mock(ActionFilters.class), + rolesStore, + transportService, + new ReservedRolesStore() + ); final int size = randomIntBetween(1, ReservedRolesStore.names().size()); final List names = randomSubsetOf(size, ReservedRolesStore.names()); @@ -85,8 +96,10 @@ public void onFailure(Exception e) { assertThat(throwableRef.get(), is(nullValue())); assertThat(responseRef.get(), is(notNullValue())); - List retrievedRoleNames = - Arrays.asList(responseRef.get().roles()).stream().map(RoleDescriptor::getName).collect(Collectors.toList()); + List retrievedRoleNames = Arrays.asList(responseRef.get().roles()) + .stream() + .map(RoleDescriptor::getName) + .collect(Collectors.toList()); assertThat(retrievedRoleNames, containsInAnyOrder(expectedNames.toArray(Strings.EMPTY_ARRAY))); verifyZeroInteractions(rolesStore); } @@ -94,10 +107,21 @@ public void onFailure(Exception e) { public void testStoreRoles() { final List storeRoleDescriptors = randomRoleDescriptors(); NativeRolesStore rolesStore = mock(NativeRolesStore.class); - TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet()); - TransportGetRolesAction action = new TransportGetRolesAction(mock(ActionFilters.class), - rolesStore, transportService, new ReservedRolesStore()); + TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); + TransportGetRolesAction action = new TransportGetRolesAction( + mock(ActionFilters.class), + rolesStore, + transportService, + new ReservedRolesStore() + ); GetRolesRequest request = new GetRolesRequest(); request.names(storeRoleDescriptors.stream().map(RoleDescriptor::getName).collect(Collectors.toList()).toArray(Strings.EMPTY_ARRAY)); @@ -127,8 +151,10 @@ public void onFailure(Exception e) { assertThat(throwableRef.get(), is(nullValue())); assertThat(responseRef.get(), is(notNullValue())); - List retrievedRoleNames = - Arrays.asList(responseRef.get().roles()).stream().map(RoleDescriptor::getName).collect(Collectors.toList()); + List retrievedRoleNames = Arrays.asList(responseRef.get().roles()) + .stream() + .map(RoleDescriptor::getName) + .collect(Collectors.toList()); assertThat(retrievedRoleNames, containsInAnyOrder(request.names())); } @@ -147,10 +173,21 @@ public void testGetAllOrMix() { } NativeRolesStore rolesStore = mock(NativeRolesStore.class); - TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet()); - TransportGetRolesAction action = new TransportGetRolesAction(mock(ActionFilters.class), - rolesStore, transportService, new ReservedRolesStore()); + TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); + TransportGetRolesAction action = new TransportGetRolesAction( + mock(ActionFilters.class), + rolesStore, + transportService, + new ReservedRolesStore() + ); final List expectedNames = new ArrayList<>(); if (all) { @@ -173,9 +210,11 @@ public void testGetAllOrMix() { if (requestedNames1.size() == 0) { listener.onResponse(RoleRetrievalResult.success(new HashSet<>(storeRoleDescriptors))); } else { - listener.onResponse(RoleRetrievalResult.success(storeRoleDescriptors.stream() - .filter(r -> requestedNames1.contains(r.getName())) - .collect(Collectors.toSet()))); + listener.onResponse( + RoleRetrievalResult.success( + storeRoleDescriptors.stream().filter(r -> requestedNames1.contains(r.getName())).collect(Collectors.toSet()) + ) + ); } return null; }).when(rolesStore).getRoleDescriptors(eq(new HashSet<>(specificStoreNames)), anyActionListener()); @@ -196,15 +235,16 @@ public void onFailure(Exception e) { assertThat(throwableRef.get(), is(nullValue())); assertThat(responseRef.get(), is(notNullValue())); - List retrievedRoleNames = - Arrays.asList(responseRef.get().roles()).stream().map(RoleDescriptor::getName).collect(Collectors.toList()); + List retrievedRoleNames = Arrays.asList(responseRef.get().roles()) + .stream() + .map(RoleDescriptor::getName) + .collect(Collectors.toList()); assertThat(retrievedRoleNames, containsInAnyOrder(expectedNames.toArray(Strings.EMPTY_ARRAY))); if (all) { verify(rolesStore, times(1)).getRoleDescriptors(eq(new HashSet<>()), anyActionListener()); } else { - verify(rolesStore, times(1)) - .getRoleDescriptors(eq(new HashSet<>(specificStoreNames)), anyActionListener()); + verify(rolesStore, times(1)).getRoleDescriptors(eq(new HashSet<>(specificStoreNames)), anyActionListener()); } } @@ -212,10 +252,21 @@ public void testException() { final Exception e = randomFrom(new ElasticsearchSecurityException(""), new IllegalStateException()); final List storeRoleDescriptors = randomRoleDescriptors(); NativeRolesStore rolesStore = mock(NativeRolesStore.class); - TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet()); - TransportGetRolesAction action = new TransportGetRolesAction(mock(ActionFilters.class), - rolesStore, transportService, new ReservedRolesStore()); + TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); + TransportGetRolesAction action = new TransportGetRolesAction( + mock(ActionFilters.class), + rolesStore, + transportService, + new ReservedRolesStore() + ); GetRolesRequest request = new GetRolesRequest(); request.names(storeRoleDescriptors.stream().map(RoleDescriptor::getName).collect(Collectors.toList()).toArray(Strings.EMPTY_ARRAY)); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/role/TransportPutRoleActionTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/role/TransportPutRoleActionTests.java index 70fc636bbff0c..83c822cf46db4 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/role/TransportPutRoleActionTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/role/TransportPutRoleActionTests.java @@ -14,8 +14,6 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.index.query.MatchAllQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.TermQueryBuilder; @@ -25,6 +23,8 @@ import org.elasticsearch.test.ESTestCase; import org.elasticsearch.transport.Transport; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xpack.core.security.action.role.PutRoleRequest; import org.elasticsearch.xpack.core.security.action.role.PutRoleResponse; import org.elasticsearch.xpack.core.security.authz.RoleDescriptor; @@ -55,24 +55,50 @@ public class TransportPutRoleActionTests extends ESTestCase { @Override protected NamedXContentRegistry xContentRegistry() { - return new NamedXContentRegistry(List.of( - new NamedXContentRegistry.Entry(QueryBuilder.class, new ParseField(MatchAllQueryBuilder.NAME), - (p, c) -> MatchAllQueryBuilder.fromXContent(p)), - new NamedXContentRegistry.Entry(QueryBuilder.class, new ParseField(HasChildQueryBuilder.NAME), - (p, c) -> HasChildQueryBuilder.fromXContent(p)), - new NamedXContentRegistry.Entry(QueryBuilder.class, new ParseField(HasParentQueryBuilder.NAME), - (p, c) -> HasParentQueryBuilder.fromXContent(p)), - new NamedXContentRegistry.Entry(QueryBuilder.class, new ParseField(TermQueryBuilder.NAME), - (p, c) -> TermQueryBuilder.fromXContent(p)))); + return new NamedXContentRegistry( + List.of( + new NamedXContentRegistry.Entry( + QueryBuilder.class, + new ParseField(MatchAllQueryBuilder.NAME), + (p, c) -> MatchAllQueryBuilder.fromXContent(p) + ), + new NamedXContentRegistry.Entry( + QueryBuilder.class, + new ParseField(HasChildQueryBuilder.NAME), + (p, c) -> HasChildQueryBuilder.fromXContent(p) + ), + new NamedXContentRegistry.Entry( + QueryBuilder.class, + new ParseField(HasParentQueryBuilder.NAME), + (p, c) -> HasParentQueryBuilder.fromXContent(p) + ), + new NamedXContentRegistry.Entry( + QueryBuilder.class, + new ParseField(TermQueryBuilder.NAME), + (p, c) -> TermQueryBuilder.fromXContent(p) + ) + ) + ); } public void testReservedRole() { final String roleName = randomFrom(new ArrayList<>(ReservedRolesStore.names())); NativeRolesStore rolesStore = mock(NativeRolesStore.class); - TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet()); - TransportPutRoleAction action = new TransportPutRoleAction(mock(ActionFilters.class), rolesStore, transportService, - xContentRegistry()); + TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); + TransportPutRoleAction action = new TransportPutRoleAction( + mock(ActionFilters.class), + rolesStore, + transportService, + xContentRegistry() + ); PutRoleRequest request = new PutRoleRequest(); request.name(roleName); @@ -100,10 +126,21 @@ public void onFailure(Exception e) { public void testValidRole() { final String roleName = randomFrom("admin", "dept_a", "restricted"); NativeRolesStore rolesStore = mock(NativeRolesStore.class); - TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet()); - TransportPutRoleAction action = new TransportPutRoleAction(mock(ActionFilters.class), rolesStore, transportService, - xContentRegistry()); + TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); + TransportPutRoleAction action = new TransportPutRoleAction( + mock(ActionFilters.class), + rolesStore, + transportService, + xContentRegistry() + ); final boolean created = randomBoolean(); PutRoleRequest request = new PutRoleRequest(); @@ -142,10 +179,21 @@ public void testException() { final Exception e = randomFrom(new ElasticsearchSecurityException(""), new IllegalStateException()); final String roleName = randomFrom("admin", "dept_a", "restricted"); NativeRolesStore rolesStore = mock(NativeRolesStore.class); - TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet()); - TransportPutRoleAction action = new TransportPutRoleAction(mock(ActionFilters.class), rolesStore, transportService, - xContentRegistry()); + TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); + TransportPutRoleAction action = new TransportPutRoleAction( + mock(ActionFilters.class), + rolesStore, + transportService, + xContentRegistry() + ); PutRoleRequest request = new PutRoleRequest(); request.name(roleName); @@ -181,18 +229,30 @@ public void onFailure(Exception e) { public void testCreationOfRoleWithMalformedQueryJsonFails() { NativeRolesStore rolesStore = mock(NativeRolesStore.class); - TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet()); - TransportPutRoleAction action = new TransportPutRoleAction(mock(ActionFilters.class), rolesStore, transportService, - xContentRegistry()); + TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); + TransportPutRoleAction action = new TransportPutRoleAction( + mock(ActionFilters.class), + rolesStore, + transportService, + xContentRegistry() + ); PutRoleRequest request = new PutRoleRequest(); request.name("test"); - String[] malformedQueryJson = new String[]{"{ \"match_all\": { \"unknown_field\": \"\" } }", + String[] malformedQueryJson = new String[] { + "{ \"match_all\": { \"unknown_field\": \"\" } }", "{ malformed JSON }", "{ \"unknown\": {\"\"} }", - "{}"}; + "{}" }; BytesReference query = new BytesArray(randomFrom(malformedQueryJson)); - request.addIndex(new String[]{"idx1"}, new String[]{"read"}, null, null, query, randomBoolean()); + request.addIndex(new String[] { "idx1" }, new String[] { "read" }, null, null, query, randomBoolean()); final AtomicReference throwableRef = new AtomicReference<>(); final AtomicReference responseRef = new AtomicReference<>(); @@ -212,23 +272,39 @@ public void onFailure(Exception e) { assertThat(throwableRef.get(), is(notNullValue())); Throwable t = throwableRef.get(); assertThat(t, instanceOf(ElasticsearchParseException.class)); - assertThat(t.getMessage(), containsString("failed to parse field 'query' for indices [" + - Strings.arrayToCommaDelimitedString(new String[]{"idx1"}) + - "] at index privilege [0] of role descriptor")); + assertThat( + t.getMessage(), + containsString( + "failed to parse field 'query' for indices [" + + Strings.arrayToCommaDelimitedString(new String[] { "idx1" }) + + "] at index privilege [0] of role descriptor" + ) + ); } public void testCreationOfRoleWithUnsupportedQueryFails() throws Exception { NativeRolesStore rolesStore = mock(NativeRolesStore.class); - TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet()); - TransportPutRoleAction action = new TransportPutRoleAction(mock(ActionFilters.class), rolesStore, transportService, - xContentRegistry()); + TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); + TransportPutRoleAction action = new TransportPutRoleAction( + mock(ActionFilters.class), + rolesStore, + transportService, + xContentRegistry() + ); PutRoleRequest request = new PutRoleRequest(); request.name("test"); String hasChildQuery = "{ \"has_child\": { \"type\": \"child\", \"query\": { \"match_all\": {} } } }"; String hasParentQuery = "{ \"has_parent\": { \"parent_type\": \"parent\", \"query\": { \"match_all\": {} } } }"; BytesReference query = new BytesArray(randomFrom(hasChildQuery, hasParentQuery)); - request.addIndex(new String[]{"idx1"}, new String[]{"read"}, null, null, query, randomBoolean()); + request.addIndex(new String[] { "idx1" }, new String[] { "read" }, null, null, query, randomBoolean()); final AtomicReference throwableRef = new AtomicReference<>(); final AtomicReference responseRef = new AtomicReference<>(); @@ -248,8 +324,13 @@ public void onFailure(Exception e) { assertThat(throwableRef.get(), is(notNullValue())); Throwable t = throwableRef.get(); assertThat(t, instanceOf(ElasticsearchParseException.class)); - assertThat(t.getMessage(), containsString("failed to parse field 'query' for indices [" + - Strings.arrayToCommaDelimitedString(new String[]{"idx1"}) + - "] at index privilege [0] of role descriptor")); + assertThat( + t.getMessage(), + containsString( + "failed to parse field 'query' for indices [" + + Strings.arrayToCommaDelimitedString(new String[] { "idx1" }) + + "] at index privilege [0] of role descriptor" + ) + ); } } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/rolemapping/PutRoleMappingRequestTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/rolemapping/PutRoleMappingRequestTests.java index f785b328e8b0f..f11f64c1cc961 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/rolemapping/PutRoleMappingRequestTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/rolemapping/PutRoleMappingRequestTests.java @@ -31,36 +31,26 @@ public void setupBuilder() { } public void testValidateMissingName() throws Exception { - final PutRoleMappingRequest request = builder - .roles("superuser") - .expression(Mockito.mock(RoleMapperExpression.class)) - .request(); + final PutRoleMappingRequest request = builder.roles("superuser").expression(Mockito.mock(RoleMapperExpression.class)).request(); assertValidationFailure(request, "name"); } public void testValidateMissingRoles() throws Exception { - final PutRoleMappingRequest request = builder - .name("test") - .expression(Mockito.mock(RoleMapperExpression.class)) - .request(); + final PutRoleMappingRequest request = builder.name("test").expression(Mockito.mock(RoleMapperExpression.class)).request(); assertValidationFailure(request, "roles"); } public void testValidateMissingRules() throws Exception { - final PutRoleMappingRequest request = builder - .name("test") - .roles("superuser") - .request(); + final PutRoleMappingRequest request = builder.name("test").roles("superuser").request(); assertValidationFailure(request, "rules"); } public void testValidateMetadataKeys() throws Exception { - final PutRoleMappingRequest request = builder - .name("test") - .roles("superuser") - .expression(Mockito.mock(RoleMapperExpression.class)) - .metadata(Collections.singletonMap("_secret", false)) - .request(); + final PutRoleMappingRequest request = builder.name("test") + .roles("superuser") + .expression(Mockito.mock(RoleMapperExpression.class)) + .metadata(Collections.singletonMap("_secret", false)) + .request(); assertValidationFailure(request, "metadata key"); } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/rolemapping/TransportGetRoleMappingsActionTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/rolemapping/TransportGetRoleMappingsActionTests.java index d2de8eb163f97..cfe056c80f919 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/rolemapping/TransportGetRoleMappingsActionTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/rolemapping/TransportGetRoleMappingsActionTests.java @@ -32,8 +32,8 @@ import static org.hamcrest.Matchers.arrayContainingInAnyOrder; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.notNullValue; -import static org.mockito.ArgumentMatchers.nullable; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.nullable; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; @@ -48,8 +48,15 @@ public class TransportGetRoleMappingsActionTests extends ESTestCase { @Before public void setupMocks() { store = mock(NativeRoleMappingStore.class); - TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet()); + TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); action = new TransportGetRoleMappingsAction(mock(ActionFilters.class), transportService, store); namesRef = new AtomicReference<>(null); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/rolemapping/TransportPutRoleMappingActionTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/rolemapping/TransportPutRoleMappingActionTests.java index b8a9307ae97ca..13fb17bddeca9 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/rolemapping/TransportPutRoleMappingActionTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/rolemapping/TransportPutRoleMappingActionTests.java @@ -45,8 +45,15 @@ public class TransportPutRoleMappingActionTests extends ESTestCase { @Before public void setupMocks() { store = mock(NativeRoleMappingStore.class); - TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet()); + TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); action = new TransportPutRoleMappingAction(mock(ActionFilters.class), transportService, store); requestRef = new AtomicReference<>(null); @@ -58,17 +65,12 @@ public void setupMocks() { ActionListener listener = (ActionListener) args[1]; listener.onResponse(true); return null; - }).when(store).putRoleMapping(any(PutRoleMappingRequest.class), any(ActionListener.class) - ); + }).when(store).putRoleMapping(any(PutRoleMappingRequest.class), any(ActionListener.class)); } public void testPutValidMapping() throws Exception { - final FieldExpression expression = new FieldExpression( - "username", - Collections.singletonList(new FieldExpression.FieldValue("*")) - ); - final PutRoleMappingResponse response = put("anarchy", expression, "superuser", - Collections.singletonMap("dumb", true)); + final FieldExpression expression = new FieldExpression("username", Collections.singletonList(new FieldExpression.FieldValue("*"))); + final PutRoleMappingResponse response = put("anarchy", expression, "superuser", Collections.singletonMap("dumb", true)); assertThat(response.isCreated(), equalTo(true)); @@ -82,8 +84,8 @@ public void testPutValidMapping() throws Exception { assertThat(mapping.getMetadata().get("dumb"), equalTo(true)); } - private PutRoleMappingResponse put(String name, FieldExpression expression, String role, - Map metadata) throws Exception { + private PutRoleMappingResponse put(String name, FieldExpression expression, String role, Map metadata) + throws Exception { final PutRoleMappingRequest request = new PutRoleMappingRequest(); request.setName(name); request.setRoles(Arrays.asList(role)); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/saml/TransportSamlInvalidateSessionActionTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/saml/TransportSamlInvalidateSessionActionTests.java index 1d4e45513aacf..9959fe577c690 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/saml/TransportSamlInvalidateSessionActionTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/saml/TransportSamlInvalidateSessionActionTests.java @@ -150,14 +150,16 @@ public void setup() throws Exception { final Client client = new NoOpClient(threadPool) { @Override @SuppressWarnings("unchecked") - protected - void doExecute(ActionType action, Request request, ActionListener listener) { + protected void doExecute( + ActionType action, + Request request, + ActionListener listener + ) { if (IndexAction.NAME.equals(action.name())) { assertThat(request, instanceOf(IndexRequest.class)); IndexRequest indexRequest = (IndexRequest) request; indexRequests.add(indexRequest); - final IndexResponse response = new IndexResponse( - new ShardId("test", "test", 0), indexRequest.id(), 1, 1, 1, true); + final IndexResponse response = new IndexResponse(new ShardId("test", "test", 0), indexRequest.id(), 1, 1, 1, true); listener.onResponse((Response) response); } else if (BulkAction.NAME.equals(action.name())) { assertThat(request, instanceOf(BulkRequest.class)); @@ -170,16 +172,46 @@ void doExecute(ActionType action, Request request, ActionListener action, Request request, ActionListener null, null, Collections.emptySet()); + final TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); final Realms realms = mock(Realms.class); - action = new TransportSamlInvalidateSessionAction(transportService, mock(ActionFilters.class),tokenService, realms); + action = new TransportSamlInvalidateSessionAction(transportService, mock(ActionFilters.class), tokenService, realms); final Environment env = TestEnvironment.newEnvironment(settings); - final RealmConfig realmConfig = new RealmConfig( - realmId, - settings, - env, threadContext); + final RealmConfig realmConfig = new RealmConfig(realmId, settings, env, threadContext); samlRealm = SamlRealmTestHelper.buildRealm(realmConfig, null); when(realms.realm(realmConfig.name())).thenReturn(samlRealm); when(realms.stream()).thenAnswer(i -> Stream.of(samlRealm)); logoutRequest = new SamlLogoutRequestHandler.Result( - randomAlphaOfLengthBetween(8, 24), - new SamlNameId(NameID.TRANSIENT, randomAlphaOfLengthBetween(8, 24), null, null, null), - randomAlphaOfLengthBetween(12, 16), - null + randomAlphaOfLengthBetween(8, 24), + new SamlNameId(NameID.TRANSIENT, randomAlphaOfLengthBetween(8, 24), null, null, null), + randomAlphaOfLengthBetween(12, 16), + null ); when(samlRealm.getLogoutHandler().parseFromQueryString(anyString())).thenReturn(logoutRequest); } @@ -244,7 +289,8 @@ void doExecute(ActionType action, Request request, ActionListener sourceMap = XContentType.JSON.xContent() - .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, source.streamInput()).map(); + .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, source.streamInput()) + .map(); @SuppressWarnings("unchecked") final Map accessToken = (Map) sourceMap.get("access_token"); @SuppressWarnings("unchecked") @@ -268,8 +314,12 @@ public void testInvalidateCorrectTokensFromLogoutRequest() throws Exception { final String userTokenId2 = UUIDs.randomBase64UUID(); final String refreshToken2 = UUIDs.randomBase64UUID(); storeToken(logoutRequest.getNameId(), randomAlphaOfLength(10)); - final TokenService.CreateTokenResult tokenToInvalidate1 = storeToken(userTokenId1, refreshToken1, logoutRequest.getNameId(), - logoutRequest.getSession()); + final TokenService.CreateTokenResult tokenToInvalidate1 = storeToken( + userTokenId1, + refreshToken1, + logoutRequest.getNameId(), + logoutRequest.getSession() + ); storeToken(userTokenId2, refreshToken2, logoutRequest.getNameId(), logoutRequest.getSession()); storeToken(new SamlNameId(NameID.PERSISTENT, randomAlphaOfLength(16), null, null, null), logoutRequest.getSession()); @@ -277,10 +327,10 @@ public void testInvalidateCorrectTokensFromLogoutRequest() throws Exception { final AtomicInteger counter = new AtomicInteger(); final SearchHit[] searchHits = indexRequests.stream() - .filter(r -> r.id().startsWith("token")) - .map(r -> tokenHit(counter.incrementAndGet(), r.source())) - .collect(Collectors.toList()) - .toArray(new SearchHit[0]); + .filter(r -> r.id().startsWith("token")) + .map(r -> tokenHit(counter.incrementAndGet(), r.source())) + .collect(Collectors.toList()) + .toArray(new SearchHit[0]); assertThat(searchHits.length, equalTo(4)); searchFunction = req1 -> { searchFunction = findTokenByRefreshToken(searchHits); @@ -331,11 +381,15 @@ public void testInvalidateCorrectTokensFromLogoutRequest() throws Exception { assertThat(filter1.get(1), instanceOf(TermQueryBuilder.class)); assertThat(((TermQueryBuilder) filter1.get(1)).fieldName(), equalTo("refresh_token.token")); - assertThat(((TermQueryBuilder) filter1.get(1)).value(), - equalTo(TokenService.hashTokenString(TokenService.unpackVersionAndPayload(tokenToInvalidate1.getRefreshToken()).v2()))); + assertThat( + ((TermQueryBuilder) filter1.get(1)).value(), + equalTo(TokenService.hashTokenString(TokenService.unpackVersionAndPayload(tokenToInvalidate1.getRefreshToken()).v2())) + ); - assertThat(tokenToInvalidate1.getAuthentication(), equalTo(new Authentication(new User("bob"), - new RealmRef("native", NativeRealmSettings.TYPE, "node01"), null))); + assertThat( + tokenToInvalidate1.getAuthentication(), + equalTo(new Authentication(new User("bob"), new RealmRef("native", NativeRealmSettings.TYPE, "node01"), null)) + ); assertThat(bulkRequests, hasSize(4)); // 4 updates (refresh-token + access-token) // Invalidate refresh token 1 @@ -372,7 +426,7 @@ private Function findTokenByRefreshToken(SearchHit[] @SuppressWarnings("unchecked") final Map refreshToken = (Map) hit.getSourceAsMap().get("refresh_token"); if (termQuery.value().equals(refreshToken.get("token"))) { - return new SearchHit[]{hit}; + return new SearchHit[] { hit }; } } return new SearchHit[0]; @@ -380,8 +434,11 @@ private Function findTokenByRefreshToken(SearchHit[] } private TokenService.CreateTokenResult storeToken(String userTokenId, String refreshToken, SamlNameId nameId, String session) { - Authentication authentication = new Authentication(new User("bob"), - new RealmRef("native", NativeRealmSettings.TYPE, "node01"), null); + Authentication authentication = new Authentication( + new User("bob"), + new RealmRef("native", NativeRealmSettings.TYPE, "node01"), + null + ); final Map metadata = samlRealm.createTokenMetadata(nameId, session); final PlainActionFuture future = new PlainActionFuture<>(); tokenService.createOAuth2Tokens(userTokenId, refreshToken, authentication, authentication, metadata, future); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/saml/TransportSamlLogoutActionTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/saml/TransportSamlLogoutActionTests.java index d528c2177bda0..7c37f12faca25 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/saml/TransportSamlLogoutActionTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/saml/TransportSamlLogoutActionTests.java @@ -85,9 +85,9 @@ import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.startsWith; -import static org.mockito.ArgumentMatchers.nullable; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.nullable; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -132,8 +132,7 @@ public void setup() throws Exception { when(client.settings()).thenReturn(settings); doAnswer(invocationOnMock -> { GetRequestBuilder builder = new GetRequestBuilder(client, GetAction.INSTANCE); - builder.setIndex((String) invocationOnMock.getArguments()[0]) - .setId((String) invocationOnMock.getArguments()[1]); + builder.setIndex((String) invocationOnMock.getArguments()[0]).setId((String) invocationOnMock.getArguments()[1]); return builder; }).when(client).prepareGet(nullable(String.class), nullable(String.class)); doAnswer(invocationOnMock -> { @@ -143,8 +142,7 @@ public void setup() throws Exception { }).when(client).prepareIndex(nullable(String.class)); doAnswer(invocationOnMock -> { UpdateRequestBuilder builder = new UpdateRequestBuilder(client, UpdateAction.INSTANCE); - builder.setIndex((String) invocationOnMock.getArguments()[0]) - .setId((String) invocationOnMock.getArguments()[1]); + builder.setIndex((String) invocationOnMock.getArguments()[0]).setId((String) invocationOnMock.getArguments()[1]); return builder; }).when(client).prepareUpdate(nullable(String.class), nullable(String.class)); doAnswer(invocationOnMock -> { @@ -172,8 +170,7 @@ public void setup() throws Exception { IndexRequest indexRequest = (IndexRequest) invocationOnMock.getArguments()[0]; ActionListener listener = (ActionListener) invocationOnMock.getArguments()[1]; indexRequests.add(indexRequest); - final IndexResponse response = new IndexResponse( - new ShardId("test", "test", 0), indexRequest.id(), 1, 1, 1, true); + final IndexResponse response = new IndexResponse(new ShardId("test", "test", 0), indexRequest.id(), 1, 1, 1, true); listener.onResponse(response); return Void.TYPE; }).when(client).index(any(IndexRequest.class), any(ActionListener.class)); @@ -181,8 +178,7 @@ public void setup() throws Exception { IndexRequest indexRequest = (IndexRequest) invocationOnMock.getArguments()[1]; ActionListener listener = (ActionListener) invocationOnMock.getArguments()[2]; indexRequests.add(indexRequest); - final IndexResponse response = new IndexResponse( - new ShardId("test", "test", 0), indexRequest.id(), 1, 1, 1, true); + final IndexResponse response = new IndexResponse(new ShardId("test", "test", 0), indexRequest.id(), 1, 1, 1, true); listener.onResponse(response); return Void.TYPE; }).when(client).execute(eq(IndexAction.INSTANCE), any(IndexRequest.class), any(ActionListener.class)); @@ -211,11 +207,26 @@ public void setup() throws Exception { when(licenseState.isAllowed(Security.TOKEN_SERVICE_FEATURE)).thenReturn(true); final ClusterService clusterService = ClusterServiceUtils.createClusterService(threadPool); final SecurityContext securityContext = new SecurityContext(settings, threadContext); - tokenService = new TokenService(settings, Clock.systemUTC(), client, licenseState, securityContext, securityIndex, securityIndex, - clusterService); + tokenService = new TokenService( + settings, + Clock.systemUTC(), + client, + licenseState, + securityContext, + securityIndex, + securityIndex, + clusterService + ); - final TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet()); + final TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); final Realms realms = mock(Realms.class); action = new TransportSamlLogoutAction(transportService, mock(ActionFilters.class), realms, tokenService); @@ -235,16 +246,23 @@ public void testLogoutInvalidatesToken() throws Exception { final String session = randomAlphaOfLengthBetween(12, 18); final String nameId = randomAlphaOfLengthBetween(6, 16); final Map userMetadata = MapBuilder.newMapBuilder() - .put(SamlRealm.USER_METADATA_NAMEID_FORMAT, NameID.TRANSIENT) - .put(SamlRealm.USER_METADATA_NAMEID_VALUE, nameId) - .map(); - final User user = new User("punisher", new String[]{"superuser"}, null, null, userMetadata, true); + .put(SamlRealm.USER_METADATA_NAMEID_FORMAT, NameID.TRANSIENT) + .put(SamlRealm.USER_METADATA_NAMEID_VALUE, nameId) + .map(); + final User user = new User("punisher", new String[] { "superuser" }, null, null, userMetadata, true); final Authentication.RealmRef realmRef = new Authentication.RealmRef(samlRealm.name(), SamlRealmSettings.TYPE, "node01"); final Map tokenMetadata = samlRealm.createTokenMetadata( - new SamlNameId(NameID.TRANSIENT, nameId, null, null, null), session); - final Authentication authentication = new Authentication(user, realmRef, null, null, Authentication.AuthenticationType.REALM, - tokenMetadata); - + new SamlNameId(NameID.TRANSIENT, nameId, null, null, null), + session + ); + final Authentication authentication = new Authentication( + user, + realmRef, + null, + null, + Authentication.AuthenticationType.REALM, + tokenMetadata + ); final PlainActionFuture future = new PlainActionFuture<>(); final String userTokenId = UUIDs.randomBase64UUID(); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/service/TransportCreateServiceAccountTokenActionTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/service/TransportCreateServiceAccountTokenActionTests.java index 8747188e8a1fc..eb7c923e5e0ec 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/service/TransportCreateServiceAccountTokenActionTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/service/TransportCreateServiceAccountTokenActionTests.java @@ -40,8 +40,11 @@ public void init() throws IOException { serviceAccountService = mock(ServiceAccountService.class); securityContext = mock(SecurityContext.class); transportCreateServiceAccountTokenAction = new TransportCreateServiceAccountTokenAction( - mock(TransportService.class), new ActionFilters(Collections.emptySet()), - serviceAccountService, securityContext); + mock(TransportService.class), + new ActionFilters(Collections.emptySet()), + serviceAccountService, + securityContext + ); } public void testAuthenticationIsRequired() { diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/service/TransportDeleteServiceAccountTokenActionTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/service/TransportDeleteServiceAccountTokenActionTests.java index 7d622ee3ab046..8b0530e48c560 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/service/TransportDeleteServiceAccountTokenActionTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/service/TransportDeleteServiceAccountTokenActionTests.java @@ -13,8 +13,8 @@ import org.elasticsearch.test.ESTestCase; import org.elasticsearch.transport.TransportService; import org.elasticsearch.xpack.core.security.action.service.DeleteServiceAccountTokenRequest; -import org.elasticsearch.xpack.security.authc.service.ServiceAccountService; import org.elasticsearch.xpack.core.security.action.service.DeleteServiceAccountTokenResponse; +import org.elasticsearch.xpack.security.authc.service.ServiceAccountService; import org.junit.Before; import java.util.Collections; @@ -33,13 +33,20 @@ public class TransportDeleteServiceAccountTokenActionTests extends ESTestCase { public void init() { serviceAccountService = mock(ServiceAccountService.class); transportDeleteServiceAccountTokenAction = new TransportDeleteServiceAccountTokenAction( - mock(TransportService.class), new ActionFilters(Collections.emptySet()), serviceAccountService); + mock(TransportService.class), + new ActionFilters(Collections.emptySet()), + serviceAccountService + ); } public void testDoExecuteWillDelegate() { final DeleteServiceAccountTokenRequest request = new DeleteServiceAccountTokenRequest( - randomAlphaOfLengthBetween(3, 8), randomAlphaOfLengthBetween(3, 8), randomAlphaOfLengthBetween(3, 8)); - @SuppressWarnings("unchecked") final ActionListener listener = mock(ActionListener.class); + randomAlphaOfLengthBetween(3, 8), + randomAlphaOfLengthBetween(3, 8), + randomAlphaOfLengthBetween(3, 8) + ); + @SuppressWarnings("unchecked") + final ActionListener listener = mock(ActionListener.class); transportDeleteServiceAccountTokenAction.doExecute(mock(Task.class), request, listener); verify(serviceAccountService).deleteIndexToken(eq(request), anyActionListener()); } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/service/TransportGetServiceAccountActionTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/service/TransportGetServiceAccountActionTests.java index ac670e909a4c2..04432131fc9ff 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/service/TransportGetServiceAccountActionTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/service/TransportGetServiceAccountActionTests.java @@ -38,8 +38,10 @@ public void init() { } public void testDoExecute() { - final GetServiceAccountRequest request1 = randomFrom(new GetServiceAccountRequest(null, null), - new GetServiceAccountRequest("elastic", null)); + final GetServiceAccountRequest request1 = randomFrom( + new GetServiceAccountRequest(null, null), + new GetServiceAccountRequest("elastic", null) + ); final PlainActionFuture future1 = new PlainActionFuture<>(); transportGetServiceAccountAction.doExecute(mock(Task.class), request1, future1); final GetServiceAccountResponse getServiceAccountResponse1 = future1.actionGet(); @@ -61,7 +63,8 @@ public void testDoExecute() { final GetServiceAccountRequest request3 = randomFrom( new GetServiceAccountRequest("foo", null), new GetServiceAccountRequest("elastic", "foo"), - new GetServiceAccountRequest("foo", "bar")); + new GetServiceAccountRequest("foo", "bar") + ); final PlainActionFuture future3 = new PlainActionFuture<>(); transportGetServiceAccountAction.doExecute(mock(Task.class), request3, future3); final GetServiceAccountResponse getServiceAccountResponse3 = future3.actionGet(); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/service/TransportGetServiceAccountCredentialsActionTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/service/TransportGetServiceAccountCredentialsActionTests.java index 19520fbf7b8a1..202e2793b41e7 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/service/TransportGetServiceAccountCredentialsActionTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/service/TransportGetServiceAccountCredentialsActionTests.java @@ -40,9 +40,7 @@ public class TransportGetServiceAccountCredentialsActionTests extends ESTestCase @Before @SuppressForbidden(reason = "Allow accessing localhost") public void init() throws UnknownHostException { - final Settings.Builder builder = Settings.builder() - .put("node.name", "node_name") - .put("xpack.security.enabled", true); + final Settings.Builder builder = Settings.builder().put("node.name", "node_name").put("xpack.security.enabled", true); transport = mock(Transport.class); final TransportAddress transportAddress; if (randomBoolean()) { @@ -55,17 +53,21 @@ public void init() throws UnknownHostException { } else { builder.put("discovery.type", "single-node"); } - when(transport.boundAddress()).thenReturn( - new BoundTransportAddress(new TransportAddress[] { transportAddress }, transportAddress)); + when(transport.boundAddress()).thenReturn(new BoundTransportAddress(new TransportAddress[] { transportAddress }, transportAddress)); final Settings settings = builder.build(); serviceAccountService = mock(ServiceAccountService.class); transportGetServiceAccountCredentialsAction = new TransportGetServiceAccountCredentialsAction( - mock(TransportService.class), new ActionFilters(Collections.emptySet()), serviceAccountService); + mock(TransportService.class), + new ActionFilters(Collections.emptySet()), + serviceAccountService + ); } public void testDoExecuteWillDelegate() { - final GetServiceAccountCredentialsRequest request = - new GetServiceAccountCredentialsRequest(randomAlphaOfLengthBetween(3, 8), randomAlphaOfLengthBetween(3, 8)); + final GetServiceAccountCredentialsRequest request = new GetServiceAccountCredentialsRequest( + randomAlphaOfLengthBetween(3, 8), + randomAlphaOfLengthBetween(3, 8) + ); @SuppressWarnings("unchecked") final ActionListener listener = mock(ActionListener.class); transportGetServiceAccountCredentialsAction.doExecute(mock(Task.class), request, listener); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/token/TransportCreateTokenActionTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/token/TransportCreateTokenActionTests.java index b1b3f3c8d456b..c758d957bb8c0 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/token/TransportCreateTokenActionTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/token/TransportCreateTokenActionTests.java @@ -34,7 +34,6 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.license.MockLicenseState; -import org.mockito.Mockito; import org.elasticsearch.node.Node; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.test.ClusterServiceUtils; @@ -58,6 +57,7 @@ import org.elasticsearch.xpack.security.support.SecurityIndexManager; import org.junit.After; import org.junit.Before; +import org.mockito.Mockito; import java.nio.charset.StandardCharsets; import java.time.Clock; @@ -71,18 +71,20 @@ import static org.elasticsearch.test.ActionListenerUtils.anyActionListener; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.is; -import static org.mockito.ArgumentMatchers.nullable; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.nullable; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; public class TransportCreateTokenActionTests extends ESTestCase { - private static final Settings SETTINGS = Settings.builder().put(Node.NODE_NAME_SETTING.getKey(), "TokenServiceTests") - .put(XPackSettings.TOKEN_SERVICE_ENABLED_SETTING.getKey(), true).build(); + private static final Settings SETTINGS = Settings.builder() + .put(Node.NODE_NAME_SETTING.getKey(), "TokenServiceTests") + .put(XPackSettings.TOKEN_SERVICE_ENABLED_SETTING.getKey(), true) + .build(); private ThreadPool threadPool; private Client client; @@ -103,8 +105,7 @@ public void setupClient() { when(client.settings()).thenReturn(SETTINGS); doAnswer(invocationOnMock -> { GetRequestBuilder builder = new GetRequestBuilder(client, GetAction.INSTANCE); - builder.setIndex((String) invocationOnMock.getArguments()[0]) - .setId((String) invocationOnMock.getArguments()[1]); + builder.setIndex((String) invocationOnMock.getArguments()[0]).setId((String) invocationOnMock.getArguments()[1]); return builder; }).when(client).prepareGet(anyString(), anyString()); when(client.prepareMultiGet()).thenReturn(new MultiGetRequestBuilder(client, MultiGetAction.INSTANCE)); @@ -125,16 +126,24 @@ public void setupClient() { listener.onResponse(response); return Void.TYPE; }).when(client).multiGet(any(MultiGetRequest.class), anyActionListener()); - when(client.prepareIndex(nullable(String.class))) - .thenReturn(new IndexRequestBuilder(client, IndexAction.INSTANCE)); - when(client.prepareUpdate(any(String.class), any(String.class))) - .thenReturn(new UpdateRequestBuilder(client, UpdateAction.INSTANCE)); + when(client.prepareIndex(nullable(String.class))).thenReturn(new IndexRequestBuilder(client, IndexAction.INSTANCE)); + when(client.prepareUpdate(any(String.class), any(String.class))).thenReturn( + new UpdateRequestBuilder(client, UpdateAction.INSTANCE) + ); doAnswer(invocationOnMock -> { idxReqReference.set((IndexRequest) invocationOnMock.getArguments()[1]); @SuppressWarnings("unchecked") ActionListener responseActionListener = (ActionListener) invocationOnMock.getArguments()[2]; - responseActionListener.onResponse(new IndexResponse(new ShardId(".security", UUIDs.randomBase64UUID(), randomInt()), - randomAlphaOfLength(4), randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), true)); + responseActionListener.onResponse( + new IndexResponse( + new ShardId(".security", UUIDs.randomBase64UUID(), randomInt()), + randomAlphaOfLength(4), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + true + ) + ); return null; }).when(client).execute(eq(IndexAction.INSTANCE), any(IndexRequest.class), anyActionListener()); @@ -159,7 +168,7 @@ public void setupClient() { } else if (authToken instanceof KerberosAuthenticationToken) { KerberosAuthenticationToken token = (KerberosAuthenticationToken) invocationOnMock.getArguments()[2]; if (token.credentials() instanceof byte[] - && new String((byte[]) token.credentials(), StandardCharsets.UTF_8).equals("fail")) { + && new String((byte[]) token.credentials(), StandardCharsets.UTF_8).equals("fail")) { String errorMessage = "failed to authenticate user, gss context negotiation not complete"; ElasticsearchSecurityException ese = new ElasticsearchSecurityException(errorMessage, RestStatus.UNAUTHORIZED); ese.addHeader(KerberosAuthenticationToken.WWW_AUTHENTICATE, "Negotiate FAIL"); @@ -173,8 +182,8 @@ && new String((byte[]) token.credentials(), StandardCharsets.UTF_8).equals("fail authentication.writeToContext(threadPool.getThreadContext()); authListener.onResponse(authentication); return Void.TYPE; - }).when(authenticationService).authenticate(eq(CreateTokenAction.NAME), any(CreateTokenRequest.class), - any(AuthenticationToken.class), anyActionListener()); + }).when(authenticationService) + .authenticate(eq(CreateTokenAction.NAME), any(CreateTokenRequest.class), any(AuthenticationToken.class), anyActionListener()); this.clusterService = ClusterServiceUtils.createClusterService(threadPool); @@ -190,14 +199,27 @@ public void stopThreadPool() throws Exception { } public void testClientCredentialsCreatesWithoutRefreshToken() throws Exception { - final TokenService tokenService = new TokenService(SETTINGS, Clock.systemUTC(), client, license, securityContext, - securityIndex, securityIndex, clusterService); + final TokenService tokenService = new TokenService( + SETTINGS, + Clock.systemUTC(), + client, + license, + securityContext, + securityIndex, + securityIndex, + clusterService + ); Authentication authentication = new Authentication(new User("joe"), new Authentication.RealmRef("realm", "type", "node"), null); authentication.writeToContext(threadPool.getThreadContext()); - final TransportCreateTokenAction action = new TransportCreateTokenAction(threadPool, - mock(TransportService.class), new ActionFilters(Collections.emptySet()), tokenService, - authenticationService, securityContext); + final TransportCreateTokenAction action = new TransportCreateTokenAction( + threadPool, + mock(TransportService.class), + new ActionFilters(Collections.emptySet()), + tokenService, + authenticationService, + securityContext + ); final CreateTokenRequest createTokenRequest = new CreateTokenRequest(); createTokenRequest.setGrantType("client_credentials"); @@ -215,14 +237,27 @@ public void testClientCredentialsCreatesWithoutRefreshToken() throws Exception { } public void testPasswordGrantTypeCreatesWithRefreshToken() throws Exception { - final TokenService tokenService = new TokenService(SETTINGS, Clock.systemUTC(), client, license, securityContext, - securityIndex, securityIndex, clusterService); + final TokenService tokenService = new TokenService( + SETTINGS, + Clock.systemUTC(), + client, + license, + securityContext, + securityIndex, + securityIndex, + clusterService + ); Authentication authentication = new Authentication(new User("joe"), new Authentication.RealmRef("realm", "type", "node"), null); authentication.writeToContext(threadPool.getThreadContext()); - final TransportCreateTokenAction action = new TransportCreateTokenAction(threadPool, - mock(TransportService.class), new ActionFilters(Collections.emptySet()), tokenService, - authenticationService, securityContext); + final TransportCreateTokenAction action = new TransportCreateTokenAction( + threadPool, + mock(TransportService.class), + new ActionFilters(Collections.emptySet()), + tokenService, + authenticationService, + securityContext + ); final CreateTokenRequest createTokenRequest = new CreateTokenRequest(); createTokenRequest.setGrantType("password"); createTokenRequest.setUsername("user"); @@ -242,14 +277,27 @@ public void testPasswordGrantTypeCreatesWithRefreshToken() throws Exception { } public void testKerberosGrantTypeCreatesWithRefreshToken() throws Exception { - final TokenService tokenService = new TokenService(SETTINGS, Clock.systemUTC(), client, license, securityContext, - securityIndex, securityIndex, clusterService); + final TokenService tokenService = new TokenService( + SETTINGS, + Clock.systemUTC(), + client, + license, + securityContext, + securityIndex, + securityIndex, + clusterService + ); Authentication authentication = new Authentication(new User("joe"), new Authentication.RealmRef("realm", "type", "node"), null); authentication.writeToContext(threadPool.getThreadContext()); - final TransportCreateTokenAction action = new TransportCreateTokenAction(threadPool, - mock(TransportService.class), new ActionFilters(Collections.emptySet()), tokenService, - authenticationService, securityContext); + final TransportCreateTokenAction action = new TransportCreateTokenAction( + threadPool, + mock(TransportService.class), + new ActionFilters(Collections.emptySet()), + tokenService, + authenticationService, + securityContext + ); final CreateTokenRequest createTokenRequest = new CreateTokenRequest(); createTokenRequest.setGrantType("_kerberos"); String failOrSuccess = randomBoolean() ? "fail" : "success"; @@ -279,20 +327,34 @@ public void testKerberosGrantTypeCreatesWithRefreshToken() throws Exception { } public void testKerberosGrantTypeWillFailOnBase64DecodeError() throws Exception { - final TokenService tokenService = new TokenService(SETTINGS, Clock.systemUTC(), client, license, securityContext, - securityIndex, securityIndex, clusterService); + final TokenService tokenService = new TokenService( + SETTINGS, + Clock.systemUTC(), + client, + license, + securityContext, + securityIndex, + securityIndex, + clusterService + ); Authentication authentication = new Authentication(new User("joe"), new Authentication.RealmRef("realm", "type", "node"), null); authentication.writeToContext(threadPool.getThreadContext()); - final TransportCreateTokenAction action = new TransportCreateTokenAction(threadPool, - mock(TransportService.class), new ActionFilters(Collections.emptySet()), tokenService, - authenticationService, securityContext); + final TransportCreateTokenAction action = new TransportCreateTokenAction( + threadPool, + mock(TransportService.class), + new ActionFilters(Collections.emptySet()), + tokenService, + authenticationService, + securityContext + ); final CreateTokenRequest createTokenRequest = new CreateTokenRequest(); createTokenRequest.setGrantType("_kerberos"); final char[] invalidBase64Chars = "!\"#$%&\\'()*,.:;<>?@[]^_`{|}~\t\n\r".toCharArray(); final String kerberosTicketValue = Strings.arrayToDelimitedString( - randomArray(1, 10, Character[]::new, - () -> invalidBase64Chars[randomIntBetween(0, invalidBase64Chars.length - 1)]), ""); + randomArray(1, 10, Character[]::new, () -> invalidBase64Chars[randomIntBetween(0, invalidBase64Chars.length - 1)]), + "" + ); createTokenRequest.setKerberosTicket(new SecureString(kerberosTicketValue.toCharArray())); PlainActionFuture tokenResponseFuture = new PlainActionFuture<>(); @@ -304,16 +366,31 @@ public void testKerberosGrantTypeWillFailOnBase64DecodeError() throws Exception } public void testServiceAccountCannotCreateOAuthToken() throws Exception { - final TokenService tokenService = new TokenService(SETTINGS, Clock.systemUTC(), client, license, securityContext, - securityIndex, securityIndex, clusterService); + final TokenService tokenService = new TokenService( + SETTINGS, + Clock.systemUTC(), + client, + license, + securityContext, + securityIndex, + securityIndex, + clusterService + ); Authentication authentication = new Authentication( new User(randomAlphaOfLengthBetween(3, 8) + "/" + randomAlphaOfLengthBetween(3, 8)), - new Authentication.RealmRef("_service_account", "_service_account", "node"), null); + new Authentication.RealmRef("_service_account", "_service_account", "node"), + null + ); authentication.writeToContext(threadPool.getThreadContext()); - final TransportCreateTokenAction action = new TransportCreateTokenAction(threadPool, - mock(TransportService.class), new ActionFilters(Collections.emptySet()), tokenService, - authenticationService, securityContext); + final TransportCreateTokenAction action = new TransportCreateTokenAction( + threadPool, + mock(TransportService.class), + new ActionFilters(Collections.emptySet()), + tokenService, + authenticationService, + securityContext + ); final CreateTokenRequest createTokenRequest = new CreateTokenRequest(); createTokenRequest.setGrantType("client_credentials"); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/token/TransportInvalidateTokenActionTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/token/TransportInvalidateTokenActionTests.java index b845e2c0c1e9a..1767475393a8d 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/token/TransportInvalidateTokenActionTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/token/TransportInvalidateTokenActionTests.java @@ -51,8 +51,10 @@ public class TransportInvalidateTokenActionTests extends ESTestCase { - private static final Settings SETTINGS = Settings.builder().put(Node.NODE_NAME_SETTING.getKey(), "TokenServiceTests") - .put(XPackSettings.TOKEN_SERVICE_ENABLED_SETTING.getKey(), true).build(); + private static final Settings SETTINGS = Settings.builder() + .put(Node.NODE_NAME_SETTING.getKey(), "TokenServiceTests") + .put(XPackSettings.TOKEN_SERVICE_ENABLED_SETTING.getKey(), true) + .build(); private ThreadPool threadPool; private Client client; @@ -78,10 +80,21 @@ public void testInvalidateTokensWhenIndexUnavailable() throws Exception { when(securityIndex.isAvailable()).thenReturn(false); when(securityIndex.indexExists()).thenReturn(true); when(securityIndex.freeze()).thenReturn(securityIndex); - final TokenService tokenService = new TokenService(SETTINGS, Clock.systemUTC(), client, license, securityContext, - securityIndex, securityIndex, clusterService); - final TransportInvalidateTokenAction action = new TransportInvalidateTokenAction(mock(TransportService.class), - new ActionFilters(Collections.emptySet()), tokenService); + final TokenService tokenService = new TokenService( + SETTINGS, + Clock.systemUTC(), + client, + license, + securityContext, + securityIndex, + securityIndex, + clusterService + ); + final TransportInvalidateTokenAction action = new TransportInvalidateTokenAction( + mock(TransportService.class), + new ActionFilters(Collections.emptySet()), + tokenService + ); InvalidateTokenRequest request = new InvalidateTokenRequest(generateAccessTokenString(), ACCESS_TOKEN.getValue(), null, null); PlainActionFuture accessTokenfuture = new PlainActionFuture<>(); @@ -90,8 +103,12 @@ public void testInvalidateTokensWhenIndexUnavailable() throws Exception { assertThat(ese.getMessage(), containsString("unable to perform requested action")); assertThat(ese.status(), equalTo(RestStatus.SERVICE_UNAVAILABLE)); - request = new InvalidateTokenRequest(TokenService.prependVersionAndEncodeRefreshToken(Version.CURRENT, UUIDs.randomBase64UUID()), - REFRESH_TOKEN.getValue(), null, null); + request = new InvalidateTokenRequest( + TokenService.prependVersionAndEncodeRefreshToken(Version.CURRENT, UUIDs.randomBase64UUID()), + REFRESH_TOKEN.getValue(), + null, + null + ); PlainActionFuture refreshTokenfuture = new PlainActionFuture<>(); action.doExecute(null, request, refreshTokenfuture); ElasticsearchSecurityException ese2 = expectThrows(ElasticsearchSecurityException.class, refreshTokenfuture::actionGet); @@ -103,12 +120,24 @@ public void testInvalidateTokensWhenIndexClosed() throws Exception { when(securityIndex.isAvailable()).thenReturn(false); when(securityIndex.indexExists()).thenReturn(true); when(securityIndex.freeze()).thenReturn(securityIndex); - when(securityIndex.getUnavailableReason()).thenReturn(new IndexClosedException(new Index(INTERNAL_SECURITY_TOKENS_INDEX_7, - ClusterState.UNKNOWN_UUID))); - final TokenService tokenService = new TokenService(SETTINGS, Clock.systemUTC(), client, license, securityContext, - securityIndex, securityIndex, clusterService); - final TransportInvalidateTokenAction action = new TransportInvalidateTokenAction(mock(TransportService.class), - new ActionFilters(Collections.emptySet()), tokenService); + when(securityIndex.getUnavailableReason()).thenReturn( + new IndexClosedException(new Index(INTERNAL_SECURITY_TOKENS_INDEX_7, ClusterState.UNKNOWN_UUID)) + ); + final TokenService tokenService = new TokenService( + SETTINGS, + Clock.systemUTC(), + client, + license, + securityContext, + securityIndex, + securityIndex, + clusterService + ); + final TransportInvalidateTokenAction action = new TransportInvalidateTokenAction( + mock(TransportService.class), + new ActionFilters(Collections.emptySet()), + tokenService + ); InvalidateTokenRequest request = new InvalidateTokenRequest(generateAccessTokenString(), ACCESS_TOKEN.getValue(), null, null); PlainActionFuture accessTokenfuture = new PlainActionFuture<>(); @@ -117,8 +146,12 @@ public void testInvalidateTokensWhenIndexClosed() throws Exception { assertThat(ese.getMessage(), containsString("failed to invalidate token")); assertThat(ese.status(), equalTo(RestStatus.BAD_REQUEST)); - request = new InvalidateTokenRequest(TokenService.prependVersionAndEncodeRefreshToken(Version.CURRENT, UUIDs.randomBase64UUID()), - REFRESH_TOKEN.getValue(), null, null); + request = new InvalidateTokenRequest( + TokenService.prependVersionAndEncodeRefreshToken(Version.CURRENT, UUIDs.randomBase64UUID()), + REFRESH_TOKEN.getValue(), + null, + null + ); PlainActionFuture refreshTokenfuture = new PlainActionFuture<>(); action.doExecute(null, request, refreshTokenfuture); ElasticsearchSecurityException ese2 = expectThrows(ElasticsearchSecurityException.class, refreshTokenfuture::actionGet); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/ChangePasswordRequestBuilderTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/ChangePasswordRequestBuilderTests.java index 143aafd2d3d6e..c0a9d1c239dae 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/ChangePasswordRequestBuilderTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/ChangePasswordRequestBuilderTests.java @@ -11,9 +11,9 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.SecureString; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.security.action.user.ChangePasswordRequest; import org.elasticsearch.xpack.core.security.action.user.ChangePasswordRequestBuilder; import org.elasticsearch.xpack.core.security.authc.support.Hasher; @@ -32,41 +32,36 @@ public class ChangePasswordRequestBuilderTests extends ESTestCase { public void testWithCleartextPassword() throws IOException { final Hasher hasher = getFastStoredHashAlgoForTests(); - final String json = "{\n" + - " \"password\": \"superlongpassword\"" + - "}"; + final String json = "{\n" + " \"password\": \"superlongpassword\"" + "}"; ChangePasswordRequestBuilder builder = new ChangePasswordRequestBuilder(mock(Client.class)); - ChangePasswordRequest request = builder.source( - new BytesArray(json.getBytes(StandardCharsets.UTF_8)), XContentType.JSON, hasher).request(); + ChangePasswordRequest request = builder.source(new BytesArray(json.getBytes(StandardCharsets.UTF_8)), XContentType.JSON, hasher) + .request(); assertThat(hasher.verify(new SecureString("superlongpassword".toCharArray()), request.passwordHash()), equalTo(true)); } public void testWithHashedPassword() throws IOException { final Hasher hasher = getFastStoredHashAlgoForTests(); final char[] hash = hasher.hash(new SecureString("superlongpassword".toCharArray())); - final String json = "{\n" + - " \"password_hash\": \"" + new String(hash) + "\"" + - "}"; + final String json = "{\n" + " \"password_hash\": \"" + new String(hash) + "\"" + "}"; ChangePasswordRequestBuilder builder = new ChangePasswordRequestBuilder(mock(Client.class)); - ChangePasswordRequest request = builder.source( - new BytesArray(json.getBytes(StandardCharsets.UTF_8)), XContentType.JSON, hasher).request(); + ChangePasswordRequest request = builder.source(new BytesArray(json.getBytes(StandardCharsets.UTF_8)), XContentType.JSON, hasher) + .request(); assertThat(request.passwordHash(), equalTo(hash)); } public void testWithHashedPasswordWithWrongAlgo() { final Hasher systemHasher = getFastStoredHashAlgoForTests(); Hasher userHasher = getFastStoredHashAlgoForTests(); - while (userHasher.name().equals(systemHasher.name())){ + while (userHasher.name().equals(systemHasher.name())) { userHasher = getFastStoredHashAlgoForTests(); } final char[] hash = userHasher.hash(new SecureString("superlongpassword".toCharArray())); - final String json = "{\n" + - " \"password_hash\": \"" + new String(hash) + "\"" + - "}"; + final String json = "{\n" + " \"password_hash\": \"" + new String(hash) + "\"" + "}"; ChangePasswordRequestBuilder builder = new ChangePasswordRequestBuilder(mock(Client.class)); - final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> { - builder.source(new BytesArray(json.getBytes(StandardCharsets.UTF_8)), XContentType.JSON, systemHasher).request(); - }); + final IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> { builder.source(new BytesArray(json.getBytes(StandardCharsets.UTF_8)), XContentType.JSON, systemHasher).request(); } + ); assertThat(e.getMessage(), containsString(userHasher.name())); assertThat(e.getMessage(), containsString(systemHasher.name())); } @@ -74,13 +69,12 @@ public void testWithHashedPasswordWithWrongAlgo() { public void testWithHashedPasswordNotHash() { final Hasher systemHasher = getFastStoredHashAlgoForTests(); final char[] hash = randomAlphaOfLength(20).toCharArray(); - final String json = "{\n" + - " \"password_hash\": \"" + new String(hash) + "\"" + - "}"; + final String json = "{\n" + " \"password_hash\": \"" + new String(hash) + "\"" + "}"; ChangePasswordRequestBuilder builder = new ChangePasswordRequestBuilder(mock(Client.class)); - final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> { - builder.source(new BytesArray(json.getBytes(StandardCharsets.UTF_8)), XContentType.JSON, systemHasher).request(); - }); + final IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> { builder.source(new BytesArray(json.getBytes(StandardCharsets.UTF_8)), XContentType.JSON, systemHasher).request(); } + ); assertThat(e.getMessage(), containsString(Hasher.NOOP.name())); assertThat(e.getMessage(), containsString(systemHasher.name())); } @@ -92,13 +86,15 @@ public void testWithPasswordAndHash() throws IOException { final LinkedHashMap fields = new LinkedHashMap<>(); fields.put("password", password); fields.put("password_hash", new String(hash)); - BytesReference json = BytesReference.bytes(XContentBuilder.builder(XContentType.JSON.xContent()) - .map(shuffleMap(fields, Collections.emptySet()))); + BytesReference json = BytesReference.bytes( + XContentBuilder.builder(XContentType.JSON.xContent()).map(shuffleMap(fields, Collections.emptySet())) + ); ChangePasswordRequestBuilder builder = new ChangePasswordRequestBuilder(mock(Client.class)); - final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> { - builder.source(json, XContentType.JSON, hasher).request(); - }); + final IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> { builder.source(json, XContentType.JSON, hasher).request(); } + ); assertThat(e.getMessage(), containsString("password_hash has already been set")); } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/HasPrivilegesRequestBuilderTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/HasPrivilegesRequestBuilderTests.java index 2d4bbda40da76..0e7c690cea1d3 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/HasPrivilegesRequestBuilderTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/HasPrivilegesRequestBuilderTests.java @@ -11,8 +11,8 @@ import org.elasticsearch.action.admin.cluster.stats.ClusterStatsAction; import org.elasticsearch.client.Client; import org.elasticsearch.common.bytes.BytesArray; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.security.action.user.HasPrivilegesRequest; import org.elasticsearch.xpack.core.security.action.user.HasPrivilegesRequestBuilder; import org.elasticsearch.xpack.core.security.authz.RoleDescriptor; @@ -28,14 +28,14 @@ public class HasPrivilegesRequestBuilderTests extends ESTestCase { public void testParseValidJsonWithClusterAndIndexPrivileges() throws Exception { String json = "{ " - + " \"cluster\":[ \"all\"]," - + " \"index\":[ " - + " { \"names\": [ \".kibana\", \".reporting\" ], " - + " \"privileges\" : [ \"read\", \"write\" ] }, " - + " { \"names\": [ \".security\" ], " - + " \"privileges\" : [ \"manage\" ] } " - + " ]" - + "}"; + + " \"cluster\":[ \"all\"]," + + " \"index\":[ " + + " { \"names\": [ \".kibana\", \".reporting\" ], " + + " \"privileges\" : [ \"read\", \"write\" ] }, " + + " { \"names\": [ \".security\" ], " + + " \"privileges\" : [ \"manage\" ] } " + + " ]" + + "}"; final HasPrivilegesRequestBuilder builder = new HasPrivilegesRequestBuilder(mock(Client.class)); builder.source("elastic", new BytesArray(json.getBytes(StandardCharsets.UTF_8)), XContentType.JSON); @@ -57,11 +57,11 @@ public void testParseValidJsonWithClusterAndIndexPrivileges() throws Exception { public void testParseValidJsonWithJustIndexPrivileges() throws Exception { String json = "{ \"index\":[ " - + "{ \"names\": [ \".kibana\", \".reporting\" ], " - + " \"privileges\" : [ \"read\", \"write\" ] }, " - + "{ \"names\": [ \".security\" ], " - + " \"privileges\" : [ \"manage\" ] } " - + "] }"; + + "{ \"names\": [ \".kibana\", \".reporting\" ], " + + " \"privileges\" : [ \"read\", \"write\" ] }, " + + "{ \"names\": [ \".security\" ], " + + " \"privileges\" : [ \"manage\" ] } " + + "] }"; final HasPrivilegesRequestBuilder builder = new HasPrivilegesRequestBuilder(mock(Client.class)); builder.source("elastic", new BytesArray(json.getBytes(StandardCharsets.UTF_8)), XContentType.JSON); @@ -81,10 +81,14 @@ public void testParseValidJsonWithJustIndexPrivileges() throws Exception { public void testParseValidJsonWithJustClusterPrivileges() throws Exception { String json = "{ \"cluster\":[ " - + "\"manage\"," - + "\"" + ClusterHealthAction.NAME + "\"," - + "\"" + ClusterStatsAction.NAME + "\"" - + "] }"; + + "\"manage\"," + + "\"" + + ClusterHealthAction.NAME + + "\"," + + "\"" + + ClusterStatsAction.NAME + + "\"" + + "] }"; final HasPrivilegesRequestBuilder builder = new HasPrivilegesRequestBuilder(mock(Client.class)); builder.source("elastic", new BytesArray(json.getBytes(StandardCharsets.UTF_8)), XContentType.JSON); @@ -96,15 +100,16 @@ public void testParseValidJsonWithJustClusterPrivileges() throws Exception { public void testUseOfFieldLevelSecurityThrowsException() throws Exception { String json = "{ \"index\":[ " - + "{" - + " \"names\": [ \"employees\" ], " - + " \"privileges\" : [ \"read\", \"write\" ] ," - + " \"field_security\": { \"grant\": [ \"name\", \"department\", \"title\" ] }" - + "} ] }"; + + "{" + + " \"names\": [ \"employees\" ], " + + " \"privileges\" : [ \"read\", \"write\" ] ," + + " \"field_security\": { \"grant\": [ \"name\", \"department\", \"title\" ] }" + + "} ] }"; final HasPrivilegesRequestBuilder builder = new HasPrivilegesRequestBuilder(mock(Client.class)); - final ElasticsearchParseException parseException = expectThrows(ElasticsearchParseException.class, - () -> builder.source("elastic", new BytesArray(json.getBytes(StandardCharsets.UTF_8)), XContentType.JSON) + final ElasticsearchParseException parseException = expectThrows( + ElasticsearchParseException.class, + () -> builder.source("elastic", new BytesArray(json.getBytes(StandardCharsets.UTF_8)), XContentType.JSON) ); assertThat(parseException.getMessage(), containsString("[field_security]")); } @@ -112,8 +117,9 @@ public void testUseOfFieldLevelSecurityThrowsException() throws Exception { public void testMissingPrivilegesThrowsException() throws Exception { String json = "{ }"; final HasPrivilegesRequestBuilder builder = new HasPrivilegesRequestBuilder(mock(Client.class)); - final ElasticsearchParseException parseException = expectThrows(ElasticsearchParseException.class, - () -> builder.source("elastic", new BytesArray(json.getBytes(StandardCharsets.UTF_8)), XContentType.JSON) + final ElasticsearchParseException parseException = expectThrows( + ElasticsearchParseException.class, + () -> builder.source("elastic", new BytesArray(json.getBytes(StandardCharsets.UTF_8)), XContentType.JSON) ); assertThat(parseException.getMessage(), containsString("[cluster,index,applications] are missing")); } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/PutUserRequestBuilderTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/PutUserRequestBuilderTests.java index aee71d6cc3a71..a58c898d0c2fc 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/PutUserRequestBuilderTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/PutUserRequestBuilderTests.java @@ -12,9 +12,9 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.SecureString; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.security.action.user.PutUserRequest; import org.elasticsearch.xpack.core.security.action.user.PutUserRequestBuilder; import org.elasticsearch.xpack.core.security.authc.support.Hasher; @@ -35,14 +35,14 @@ public class PutUserRequestBuilderTests extends ESTestCase { public void testNullValuesForEmailAndFullName() throws IOException { - final String json = "{\n" + - " \"roles\": [\n" + - " \"kibana4\"\n" + - " ],\n" + - " \"full_name\": null,\n" + - " \"email\": null,\n" + - " \"metadata\": {}\n" + - "}"; + final String json = "{\n" + + " \"roles\": [\n" + + " \"kibana4\"\n" + + " ],\n" + + " \"full_name\": null,\n" + + " \"email\": null,\n" + + " \"metadata\": {}\n" + + "}"; PutUserRequestBuilder builder = new PutUserRequestBuilder(mock(Client.class)); builder.source("kibana4", new BytesArray(json.getBytes(StandardCharsets.UTF_8)), XContentType.JSON, Hasher.BCRYPT); @@ -57,12 +57,7 @@ public void testNullValuesForEmailAndFullName() throws IOException { } public void testMissingEmailFullName() throws Exception { - final String json = "{\n" + - " \"roles\": [\n" + - " \"kibana4\"\n" + - " ],\n" + - " \"metadata\": {}\n" + - "}"; + final String json = "{\n" + " \"roles\": [\n" + " \"kibana4\"\n" + " ],\n" + " \"metadata\": {}\n" + "}"; PutUserRequestBuilder builder = new PutUserRequestBuilder(mock(Client.class)); builder.source("kibana4", new BytesArray(json.getBytes(StandardCharsets.UTF_8)), XContentType.JSON, Hasher.BCRYPT); @@ -76,14 +71,14 @@ public void testMissingEmailFullName() throws Exception { } public void testWithFullNameAndEmail() throws IOException { - final String json = "{\n" + - " \"roles\": [\n" + - " \"kibana4\"\n" + - " ],\n" + - " \"full_name\": \"Kibana User\",\n" + - " \"email\": \"kibana@elastic.co\",\n" + - " \"metadata\": {}\n" + - "}"; + final String json = "{\n" + + " \"roles\": [\n" + + " \"kibana4\"\n" + + " ],\n" + + " \"full_name\": \"Kibana User\",\n" + + " \"email\": \"kibana@elastic.co\",\n" + + " \"metadata\": {}\n" + + "}"; PutUserRequestBuilder builder = new PutUserRequestBuilder(mock(Client.class)); builder.source("kibana4", new BytesArray(json.getBytes(StandardCharsets.UTF_8)), XContentType.JSON, Hasher.BCRYPT); @@ -97,65 +92,74 @@ public void testWithFullNameAndEmail() throws IOException { } public void testInvalidFullname() throws IOException { - final String json = "{\n" + - " \"roles\": [\n" + - " \"kibana4\"\n" + - " ],\n" + - " \"full_name\": [ \"Kibana User\" ],\n" + - " \"email\": \"kibana@elastic.co\",\n" + - " \"metadata\": {}\n" + - "}"; + final String json = "{\n" + + " \"roles\": [\n" + + " \"kibana4\"\n" + + " ],\n" + + " \"full_name\": [ \"Kibana User\" ],\n" + + " \"email\": \"kibana@elastic.co\",\n" + + " \"metadata\": {}\n" + + "}"; PutUserRequestBuilder builder = new PutUserRequestBuilder(mock(Client.class)); - ElasticsearchParseException e = expectThrows(ElasticsearchParseException.class, - () -> builder.source("kibana4", new BytesArray(json.getBytes(StandardCharsets.UTF_8)), XContentType.JSON, Hasher.BCRYPT)); + ElasticsearchParseException e = expectThrows( + ElasticsearchParseException.class, + () -> builder.source("kibana4", new BytesArray(json.getBytes(StandardCharsets.UTF_8)), XContentType.JSON, Hasher.BCRYPT) + ); assertThat(e.getMessage(), containsString("expected field [full_name] to be of type string")); } public void testInvalidEmail() throws IOException { - final String json = "{\n" + - " \"roles\": [\n" + - " \"kibana4\"\n" + - " ],\n" + - " \"full_name\": \"Kibana User\",\n" + - " \"email\": [ \"kibana@elastic.co\" ],\n" + - " \"metadata\": {}\n" + - "}"; + final String json = "{\n" + + " \"roles\": [\n" + + " \"kibana4\"\n" + + " ],\n" + + " \"full_name\": \"Kibana User\",\n" + + " \"email\": [ \"kibana@elastic.co\" ],\n" + + " \"metadata\": {}\n" + + "}"; PutUserRequestBuilder builder = new PutUserRequestBuilder(mock(Client.class)); - ElasticsearchParseException e = expectThrows(ElasticsearchParseException.class, - () -> builder.source("kibana4", new BytesArray(json.getBytes(StandardCharsets.UTF_8)), XContentType.JSON, Hasher.BCRYPT)); + ElasticsearchParseException e = expectThrows( + ElasticsearchParseException.class, + () -> builder.source("kibana4", new BytesArray(json.getBytes(StandardCharsets.UTF_8)), XContentType.JSON, Hasher.BCRYPT) + ); assertThat(e.getMessage(), containsString("expected field [email] to be of type string")); } public void testWithEnabled() throws IOException { - final String json = "{\n" + - " \"roles\": [\n" + - " \"kibana4\"\n" + - " ],\n" + - " \"full_name\": \"Kibana User\",\n" + - " \"email\": \"kibana@elastic.co\",\n" + - " \"metadata\": {}\n," + - " \"enabled\": false\n" + - "}"; + final String json = "{\n" + + " \"roles\": [\n" + + " \"kibana4\"\n" + + " ],\n" + + " \"full_name\": \"Kibana User\",\n" + + " \"email\": \"kibana@elastic.co\",\n" + + " \"metadata\": {}\n," + + " \"enabled\": false\n" + + "}"; PutUserRequestBuilder builder = new PutUserRequestBuilder(mock(Client.class)); - PutUserRequest request = - builder.source("kibana4", new BytesArray(json.getBytes(StandardCharsets.UTF_8)), XContentType.JSON, Hasher.BCRYPT).request(); + PutUserRequest request = builder.source( + "kibana4", + new BytesArray(json.getBytes(StandardCharsets.UTF_8)), + XContentType.JSON, + Hasher.BCRYPT + ).request(); assertFalse(request.enabled()); } public void testWithValidPasswordHash() throws IOException { final Hasher hasher = getFastStoredHashAlgoForTests(); final char[] hash = hasher.hash(new SecureString("secretpassword".toCharArray())); - final String json = "{\n" + - " \"password_hash\": \"" + new String(hash) + "\"," + - " \"roles\": []\n" + - "}"; + final String json = "{\n" + " \"password_hash\": \"" + new String(hash) + "\"," + " \"roles\": []\n" + "}"; PutUserRequestBuilder requestBuilder = new PutUserRequestBuilder(mock(Client.class)); - PutUserRequest request = requestBuilder.source("hash_user", - new BytesArray(json.getBytes(StandardCharsets.UTF_8)), XContentType.JSON, hasher).request(); + PutUserRequest request = requestBuilder.source( + "hash_user", + new BytesArray(json.getBytes(StandardCharsets.UTF_8)), + XContentType.JSON, + hasher + ).request(); assertThat(request.passwordHash(), equalTo(hash)); assertThat(request.username(), equalTo("hash_user")); } @@ -163,34 +167,36 @@ public void testWithValidPasswordHash() throws IOException { public void testWithMismatchedPasswordHashingAlgorithm() throws IOException { final Hasher systemHasher = getFastStoredHashAlgoForTests(); Hasher userHasher = getFastStoredHashAlgoForTests(); - while (userHasher.name().equals(systemHasher.name())){ + while (userHasher.name().equals(systemHasher.name())) { userHasher = getFastStoredHashAlgoForTests(); } final char[] hash = userHasher.hash(new SecureString("secretpassword".toCharArray())); - final String json = "{\n" + - " \"password_hash\": \"" + new String(hash) + "\"," + - " \"roles\": []\n" + - "}"; + final String json = "{\n" + " \"password_hash\": \"" + new String(hash) + "\"," + " \"roles\": []\n" + "}"; PutUserRequestBuilder builder = new PutUserRequestBuilder(mock(Client.class)); - final IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> { - builder.source("hash_user", new BytesArray(json.getBytes(StandardCharsets.UTF_8)), XContentType.JSON, systemHasher).request(); - }); + final IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> { + builder.source("hash_user", new BytesArray(json.getBytes(StandardCharsets.UTF_8)), XContentType.JSON, systemHasher) + .request(); + } + ); assertThat(ex.getMessage(), containsString(userHasher.name())); assertThat(ex.getMessage(), containsString(systemHasher.name())); } public void testWithPasswordHashThatsNotReallyAHash() throws IOException { final Hasher systemHasher = Hasher.PBKDF2; - final String json = "{\n" + - " \"password_hash\": \"not-a-hash\"," + - " \"roles\": []\n" + - "}"; + final String json = "{\n" + " \"password_hash\": \"not-a-hash\"," + " \"roles\": []\n" + "}"; PutUserRequestBuilder builder = new PutUserRequestBuilder(mock(Client.class)); - final IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> { - builder.source("hash_user", new BytesArray(json.getBytes(StandardCharsets.UTF_8)), XContentType.JSON, systemHasher).request(); - }); + final IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> { + builder.source("hash_user", new BytesArray(json.getBytes(StandardCharsets.UTF_8)), XContentType.JSON, systemHasher) + .request(); + } + ); assertThat(ex.getMessage(), containsString(Hasher.NOOP.name())); assertThat(ex.getMessage(), containsString(systemHasher.name())); } @@ -203,13 +209,15 @@ public void testWithBothPasswordAndHash() throws IOException { fields.put("password", password); fields.put("password_hash", new String(hash)); fields.put("roles", Collections.emptyList()); - BytesReference json = BytesReference.bytes(XContentBuilder.builder(XContentType.JSON.xContent()) - .map(shuffleMap(fields, Collections.emptySet()))); + BytesReference json = BytesReference.bytes( + XContentBuilder.builder(XContentType.JSON.xContent()).map(shuffleMap(fields, Collections.emptySet())) + ); PutUserRequestBuilder builder = new PutUserRequestBuilder(mock(Client.class)); - final IllegalArgumentException ex = expectThrows(ValidationException.class, () -> { - builder.source("hash_user", json, XContentType.JSON, hasher).request(); - }); + final IllegalArgumentException ex = expectThrows( + ValidationException.class, + () -> { builder.source("hash_user", json, XContentType.JSON, hasher).request(); } + ); assertThat(ex.getMessage(), containsString("password_hash has already been set")); } } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/TransportAuthenticateActionTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/TransportAuthenticateActionTests.java index 2eeea462953c8..488a52a77762f 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/TransportAuthenticateActionTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/TransportAuthenticateActionTests.java @@ -43,14 +43,27 @@ public class TransportAuthenticateActionTests extends ESTestCase { public void testInternalUser() { SecurityContext securityContext = mock(SecurityContext.class); - final Authentication authentication = new Authentication(randomFrom(SystemUser.INSTANCE, XPackUser.INSTANCE, - XPackSecurityUser.INSTANCE, AsyncSearchUser.INSTANCE), - new Authentication.RealmRef("native", "default_native", "node1"), null); + final Authentication authentication = new Authentication( + randomFrom(SystemUser.INSTANCE, XPackUser.INSTANCE, XPackSecurityUser.INSTANCE, AsyncSearchUser.INSTANCE), + new Authentication.RealmRef("native", "default_native", "node1"), + null + ); when(securityContext.getAuthentication()).thenReturn(authentication); - TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet()); - TransportAuthenticateAction action = new TransportAuthenticateAction(transportService, - mock(ActionFilters.class), securityContext, prepareAnonymousUser()); + TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); + TransportAuthenticateAction action = new TransportAuthenticateAction( + transportService, + mock(ActionFilters.class), + securityContext, + prepareAnonymousUser() + ); final AtomicReference throwableRef = new AtomicReference<>(); final AtomicReference responseRef = new AtomicReference<>(); @@ -73,10 +86,21 @@ public void onFailure(Exception e) { public void testNullUser() { SecurityContext securityContext = mock(SecurityContext.class); - TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet()); - TransportAuthenticateAction action = new TransportAuthenticateAction(transportService, - mock(ActionFilters.class), securityContext, prepareAnonymousUser()); + TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); + TransportAuthenticateAction action = new TransportAuthenticateAction( + transportService, + mock(ActionFilters.class), + securityContext, + prepareAnonymousUser() + ); final AtomicReference throwableRef = new AtomicReference<>(); final AtomicReference responseRef = new AtomicReference<>(); @@ -97,19 +121,33 @@ public void onFailure(Exception e) { assertThat(throwableRef.get().getMessage(), containsString("did not find an authenticated user")); } - public void testValidAuthentication(){ + public void testValidAuthentication() { final User user = randomFrom(new ElasticUser(true), new KibanaUser(true), new User("joe")); - final Authentication authentication = new Authentication(user, new Authentication.RealmRef("native_realm", "native", "node1"), - null); + final Authentication authentication = new Authentication( + user, + new Authentication.RealmRef("native_realm", "native", "node1"), + null + ); SecurityContext securityContext = mock(SecurityContext.class); when(securityContext.getAuthentication()).thenReturn(authentication); when(securityContext.getUser()).thenReturn(user); final AnonymousUser anonymousUser = prepareAnonymousUser(); - TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet()); - TransportAuthenticateAction action = new TransportAuthenticateAction(transportService, - mock(ActionFilters.class), securityContext, anonymousUser); + TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); + TransportAuthenticateAction action = new TransportAuthenticateAction( + transportService, + mock(ActionFilters.class), + securityContext, + anonymousUser + ); final AtomicReference throwableRef = new AtomicReference<>(); final AtomicReference responseRef = new AtomicReference<>(); @@ -147,8 +185,7 @@ private AnonymousUser prepareAnonymousUser() { final AnonymousUser anonymousUser = mock(AnonymousUser.class); if (randomBoolean()) { when(anonymousUser.enabled()).thenReturn(true); - when(anonymousUser.roles()).thenReturn( - randomList(1, 4, () -> randomAlphaOfLengthBetween(4, 12)).toArray(new String[0])); + when(anonymousUser.roles()).thenReturn(randomList(1, 4, () -> randomAlphaOfLengthBetween(4, 12)).toArray(new String[0])); } else { when(anonymousUser.enabled()).thenReturn(false); } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/TransportChangePasswordActionTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/TransportChangePasswordActionTests.java index 14885f4d9c934..f729bff2a84f9 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/TransportChangePasswordActionTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/TransportChangePasswordActionTests.java @@ -52,14 +52,27 @@ public class TransportChangePasswordActionTests extends ESTestCase { public void testAnonymousUser() { final Hasher hasher = getFastStoredHashAlgoForTests(); - Settings settings = Settings.builder().put(AnonymousUser.ROLES_SETTING.getKey(), "superuser") - .put(XPackSettings.PASSWORD_HASHING_ALGORITHM.getKey(), hasher.name()).build(); + Settings settings = Settings.builder() + .put(AnonymousUser.ROLES_SETTING.getKey(), "superuser") + .put(XPackSettings.PASSWORD_HASHING_ALGORITHM.getKey(), hasher.name()) + .build(); AnonymousUser anonymousUser = new AnonymousUser(settings); NativeUsersStore usersStore = mock(NativeUsersStore.class); - TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet()); - TransportChangePasswordAction action = new TransportChangePasswordAction(settings, transportService, - mock(ActionFilters.class), usersStore); + TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); + TransportChangePasswordAction action = new TransportChangePasswordAction( + settings, + transportService, + mock(ActionFilters.class), + usersStore + ); // Request will fail before the request hashing algorithm is checked, but we use the same algorithm as in settings for consistency ChangePasswordRequest request = new ChangePasswordRequest(); request.username(anonymousUser.principal()); @@ -88,16 +101,32 @@ public void onFailure(Exception e) { public void testInternalUsers() { final Hasher hasher = getFastStoredHashAlgoForTests(); NativeUsersStore usersStore = mock(NativeUsersStore.class); - Settings passwordHashingSettings = Settings.builder(). - put(XPackSettings.PASSWORD_HASHING_ALGORITHM.getKey(), hasher.name()).build(); - TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet()); - TransportChangePasswordAction action = new TransportChangePasswordAction(passwordHashingSettings, transportService, - mock(ActionFilters.class), usersStore); + Settings passwordHashingSettings = Settings.builder().put(XPackSettings.PASSWORD_HASHING_ALGORITHM.getKey(), hasher.name()).build(); + TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); + TransportChangePasswordAction action = new TransportChangePasswordAction( + passwordHashingSettings, + transportService, + mock(ActionFilters.class), + usersStore + ); // Request will fail before the request hashing algorithm is checked, but we use the same algorithm as in settings for consistency ChangePasswordRequest request = new ChangePasswordRequest(); - request.username(randomFrom(SystemUser.INSTANCE.principal(), XPackUser.INSTANCE.principal(), - XPackSecurityUser.INSTANCE.principal(), AsyncSearchUser.INSTANCE.principal())); + request.username( + randomFrom( + SystemUser.INSTANCE.principal(), + XPackUser.INSTANCE.principal(), + XPackSecurityUser.INSTANCE.principal(), + AsyncSearchUser.INSTANCE.principal() + ) + ); request.passwordHash(hasher.hash(SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)); final AtomicReference throwableRef = new AtomicReference<>(); @@ -135,12 +164,22 @@ public void testValidUser() { listener.onResponse(null); return null; }).when(usersStore).changePassword(eq(request), anyActionListener()); - TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet()); - Settings passwordHashingSettings = Settings.builder(). - put(XPackSettings.PASSWORD_HASHING_ALGORITHM.getKey(), hasher.name()).build(); - TransportChangePasswordAction action = new TransportChangePasswordAction(passwordHashingSettings, transportService, - mock(ActionFilters.class), usersStore); + TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); + Settings passwordHashingSettings = Settings.builder().put(XPackSettings.PASSWORD_HASHING_ALGORITHM.getKey(), hasher.name()).build(); + TransportChangePasswordAction action = new TransportChangePasswordAction( + passwordHashingSettings, + transportService, + mock(ActionFilters.class), + usersStore + ); final AtomicReference throwableRef = new AtomicReference<>(); final AtomicReference responseRef = new AtomicReference<>(); action.doExecute(mock(Task.class), request, new ActionListener<>() { @@ -170,14 +209,26 @@ public void testIncorrectPasswordHashingAlgorithm() { request.passwordHash(hasher.hash(SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)); final AtomicReference throwableRef = new AtomicReference<>(); final AtomicReference responseRef = new AtomicReference<>(); - TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet()); + TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); final String systemHash = randomValueOtherThan( hasher.name().toLowerCase(Locale.ROOT), - () -> randomFrom("pbkdf2_50000", "pbkdf2_100000", "bcrypt11", "bcrypt8", "bcrypt")); + () -> randomFrom("pbkdf2_50000", "pbkdf2_100000", "bcrypt11", "bcrypt8", "bcrypt") + ); Settings passwordHashingSettings = Settings.builder().put(XPackSettings.PASSWORD_HASHING_ALGORITHM.getKey(), systemHash).build(); - TransportChangePasswordAction action = new TransportChangePasswordAction(passwordHashingSettings, transportService, - mock(ActionFilters.class), usersStore); + TransportChangePasswordAction action = new TransportChangePasswordAction( + passwordHashingSettings, + transportService, + mock(ActionFilters.class), + usersStore + ); action.doExecute(mock(Task.class), request, new ActionListener<>() { @Override public void onResponse(ActionResponse.Empty changePasswordResponse) { @@ -212,12 +263,22 @@ public void testException() { listener.onFailure(e); return null; }).when(usersStore).changePassword(eq(request), anyActionListener()); - TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet()); - Settings passwordHashingSettings = Settings.builder(). - put(XPackSettings.PASSWORD_HASHING_ALGORITHM.getKey(), hasher.name()).build(); - TransportChangePasswordAction action = new TransportChangePasswordAction(passwordHashingSettings, transportService, - mock(ActionFilters.class), usersStore); + TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); + Settings passwordHashingSettings = Settings.builder().put(XPackSettings.PASSWORD_HASHING_ALGORITHM.getKey(), hasher.name()).build(); + TransportChangePasswordAction action = new TransportChangePasswordAction( + passwordHashingSettings, + transportService, + mock(ActionFilters.class), + usersStore + ); final AtomicReference throwableRef = new AtomicReference<>(); final AtomicReference responseRef = new AtomicReference<>(); action.doExecute(mock(Task.class), request, new ActionListener<>() { diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/TransportDeleteUserActionTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/TransportDeleteUserActionTests.java index b3cdc65544895..8a603f6fbbb9c 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/TransportDeleteUserActionTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/TransportDeleteUserActionTests.java @@ -48,8 +48,15 @@ public class TransportDeleteUserActionTests extends ESTestCase { public void testAnonymousUser() { Settings settings = Settings.builder().put(AnonymousUser.ROLES_SETTING.getKey(), "superuser").build(); NativeUsersStore usersStore = mock(NativeUsersStore.class); - TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet()); + TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); TransportDeleteUserAction action = new TransportDeleteUserAction(settings, mock(ActionFilters.class), usersStore, transportService); DeleteUserRequest request = new DeleteUserRequest(new AnonymousUser(settings).principal()); @@ -76,13 +83,30 @@ public void onFailure(Exception e) { public void testInternalUser() { NativeUsersStore usersStore = mock(NativeUsersStore.class); - TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet()); - TransportDeleteUserAction action = new TransportDeleteUserAction(Settings.EMPTY, mock(ActionFilters.class), - usersStore, transportService); + TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); + TransportDeleteUserAction action = new TransportDeleteUserAction( + Settings.EMPTY, + mock(ActionFilters.class), + usersStore, + transportService + ); - DeleteUserRequest request = new DeleteUserRequest(randomFrom(SystemUser.INSTANCE.principal(), XPackUser.INSTANCE.principal(), - XPackSecurityUser.INSTANCE.principal(), AsyncSearchUser.INSTANCE.principal())); + DeleteUserRequest request = new DeleteUserRequest( + randomFrom( + SystemUser.INSTANCE.principal(), + XPackUser.INSTANCE.principal(), + XPackSecurityUser.INSTANCE.principal(), + AsyncSearchUser.INSTANCE.principal() + ) + ); final AtomicReference throwableRef = new AtomicReference<>(); final AtomicReference responseRef = new AtomicReference<>(); @@ -107,10 +131,21 @@ public void onFailure(Exception e) { public void testReservedUser() { final User reserved = randomFrom(new ElasticUser(true), new KibanaUser(true)); NativeUsersStore usersStore = mock(NativeUsersStore.class); - TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet()); - TransportDeleteUserAction action = new TransportDeleteUserAction(Settings.EMPTY, mock(ActionFilters.class), - usersStore, transportService); + TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); + TransportDeleteUserAction action = new TransportDeleteUserAction( + Settings.EMPTY, + mock(ActionFilters.class), + usersStore, + transportService + ); DeleteUserRequest request = new DeleteUserRequest(reserved.principal()); @@ -137,10 +172,21 @@ public void onFailure(Exception e) { public void testValidUser() { final User user = new User("joe"); NativeUsersStore usersStore = mock(NativeUsersStore.class); - TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet()); - TransportDeleteUserAction action = new TransportDeleteUserAction(Settings.EMPTY, mock(ActionFilters.class), - usersStore, transportService); + TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); + TransportDeleteUserAction action = new TransportDeleteUserAction( + Settings.EMPTY, + mock(ActionFilters.class), + usersStore, + transportService + ); final boolean found = randomBoolean(); final DeleteUserRequest request = new DeleteUserRequest(user.principal()); @@ -177,10 +223,21 @@ public void testException() { final Exception e = randomFrom(new ElasticsearchSecurityException(""), new IllegalStateException(), new RuntimeException()); final User user = new User("joe"); NativeUsersStore usersStore = mock(NativeUsersStore.class); - TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet()); - TransportDeleteUserAction action = new TransportDeleteUserAction(Settings.EMPTY, mock(ActionFilters.class), - usersStore, transportService); + TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); + TransportDeleteUserAction action = new TransportDeleteUserAction( + Settings.EMPTY, + mock(ActionFilters.class), + usersStore, + transportService + ); final DeleteUserRequest request = new DeleteUserRequest(user.principal()); doAnswer(invocation -> { diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/TransportGetUsersActionTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/TransportGetUsersActionTests.java index 5fdfbfffd23cf..b7bf84b312658 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/TransportGetUsersActionTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/TransportGetUsersActionTests.java @@ -90,12 +90,23 @@ public void testAnonymousUser() { SecurityIndexManager securityIndex = mock(SecurityIndexManager.class); when(securityIndex.isAvailable()).thenReturn(true); AnonymousUser anonymousUser = new AnonymousUser(settings); - ReservedRealm reservedRealm = - new ReservedRealm(mock(Environment.class), settings, usersStore, anonymousUser, threadPool); - TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet()); - TransportGetUsersAction action = new TransportGetUsersAction(Settings.EMPTY, mock(ActionFilters.class), - usersStore, transportService, reservedRealm); + ReservedRealm reservedRealm = new ReservedRealm(mock(Environment.class), settings, usersStore, anonymousUser, threadPool); + TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); + TransportGetUsersAction action = new TransportGetUsersAction( + Settings.EMPTY, + mock(ActionFilters.class), + usersStore, + transportService, + reservedRealm + ); GetUsersRequest request = new GetUsersRequest(); request.usernames(anonymousUser.principal()); @@ -127,14 +138,32 @@ public void onFailure(Exception e) { public void testInternalUser() { NativeUsersStore usersStore = mock(NativeUsersStore.class); - TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet()); - TransportGetUsersAction action = new TransportGetUsersAction(Settings.EMPTY, mock(ActionFilters.class), - usersStore, transportService, mock(ReservedRealm.class)); + TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); + TransportGetUsersAction action = new TransportGetUsersAction( + Settings.EMPTY, + mock(ActionFilters.class), + usersStore, + transportService, + mock(ReservedRealm.class) + ); GetUsersRequest request = new GetUsersRequest(); - request.usernames(randomFrom(SystemUser.INSTANCE.principal(), XPackUser.INSTANCE.principal(), - XPackSecurityUser.INSTANCE.principal(), AsyncSearchUser.INSTANCE.principal())); + request.usernames( + randomFrom( + SystemUser.INSTANCE.principal(), + XPackUser.INSTANCE.principal(), + XPackSecurityUser.INSTANCE.principal(), + AsyncSearchUser.INSTANCE.principal() + ) + ); final AtomicReference throwableRef = new AtomicReference<>(); final AtomicReference responseRef = new AtomicReference<>(); @@ -162,18 +191,35 @@ public void testReservedUsersOnly() { when(securityIndex.isAvailable()).thenReturn(true); ReservedRealmTests.mockGetAllReservedUserInfo(usersStore, Collections.emptyMap()); - ReservedRealm reservedRealm = - new ReservedRealm(mock(Environment.class), settings, usersStore, new AnonymousUser(settings), threadPool); + ReservedRealm reservedRealm = new ReservedRealm( + mock(Environment.class), + settings, + usersStore, + new AnonymousUser(settings), + threadPool + ); PlainActionFuture> userFuture = new PlainActionFuture<>(); reservedRealm.users(userFuture); final Collection allReservedUsers = userFuture.actionGet(); final int size = randomIntBetween(1, allReservedUsers.size()); final List reservedUsers = randomSubsetOf(size, allReservedUsers); final List names = reservedUsers.stream().map(User::principal).collect(Collectors.toList()); - TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet()); - TransportGetUsersAction action = new TransportGetUsersAction(Settings.EMPTY, mock(ActionFilters.class), - usersStore, transportService, reservedRealm); + TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); + TransportGetUsersAction action = new TransportGetUsersAction( + Settings.EMPTY, + mock(ActionFilters.class), + usersStore, + transportService, + reservedRealm + ); logger.error("names {}", names); GetUsersRequest request = new GetUsersRequest(); @@ -189,7 +235,7 @@ public void onResponse(GetUsersResponse response) { @Override public void onFailure(Exception e) { - logger.warn("Request failed", e); + logger.warn("Request failed", e); throwableRef.set(e); } }); @@ -202,18 +248,39 @@ public void onFailure(Exception e) { } public void testGetAllUsers() { - final List storeUsers = randomFrom(Collections.emptyList(), Collections.singletonList(new User("joe")), - Arrays.asList(new User("jane"), new User("fred")), randomUsers()); + final List storeUsers = randomFrom( + Collections.emptyList(), + Collections.singletonList(new User("joe")), + Arrays.asList(new User("jane"), new User("fred")), + randomUsers() + ); NativeUsersStore usersStore = mock(NativeUsersStore.class); SecurityIndexManager securityIndex = mock(SecurityIndexManager.class); when(securityIndex.isAvailable()).thenReturn(true); ReservedRealmTests.mockGetAllReservedUserInfo(usersStore, Collections.emptyMap()); - ReservedRealm reservedRealm = new ReservedRealm(mock(Environment.class), settings, usersStore, new AnonymousUser(settings), - threadPool); - TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet()); - TransportGetUsersAction action = new TransportGetUsersAction(Settings.EMPTY, mock(ActionFilters.class), - usersStore, transportService, reservedRealm); + ReservedRealm reservedRealm = new ReservedRealm( + mock(Environment.class), + settings, + usersStore, + new AnonymousUser(settings), + threadPool + ); + TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); + TransportGetUsersAction action = new TransportGetUsersAction( + Settings.EMPTY, + mock(ActionFilters.class), + usersStore, + transportService, + reservedRealm + ); GetUsersRequest request = new GetUsersRequest(); doAnswer(invocation -> { @@ -252,14 +319,29 @@ public void onFailure(Exception e) { } public void testGetStoreOnlyUsers() { - final List storeUsers = - randomFrom(Collections.singletonList(new User("joe")), Arrays.asList(new User("jane"), new User("fred")), randomUsers()); + final List storeUsers = randomFrom( + Collections.singletonList(new User("joe")), + Arrays.asList(new User("jane"), new User("fred")), + randomUsers() + ); final String[] storeUsernames = storeUsers.stream().map(User::principal).collect(Collectors.toList()).toArray(Strings.EMPTY_ARRAY); NativeUsersStore usersStore = mock(NativeUsersStore.class); - TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet()); - TransportGetUsersAction action = new TransportGetUsersAction(Settings.EMPTY, mock(ActionFilters.class), - usersStore, transportService, mock(ReservedRealm.class)); + TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); + TransportGetUsersAction action = new TransportGetUsersAction( + Settings.EMPTY, + mock(ActionFilters.class), + usersStore, + transportService, + mock(ReservedRealm.class) + ); GetUsersRequest request = new GetUsersRequest(); request.usernames(storeUsernames); @@ -295,20 +377,35 @@ public void onFailure(Exception e) { if (storeUsers.size() > 1) { verify(usersStore, times(1)).getUsers(aryEq(storeUsernames), anyActionListener()); } else { - verify(usersStore, times(1)).getUsers(aryEq(new String[] {storeUsernames[0]}), anyActionListener()); + verify(usersStore, times(1)).getUsers(aryEq(new String[] { storeUsernames[0] }), anyActionListener()); } } public void testException() { final Exception e = randomFrom(new ElasticsearchSecurityException(""), new IllegalStateException(), new ValidationException()); - final List storeUsers = - randomFrom(Collections.singletonList(new User("joe")), Arrays.asList(new User("jane"), new User("fred")), randomUsers()); + final List storeUsers = randomFrom( + Collections.singletonList(new User("joe")), + Arrays.asList(new User("jane"), new User("fred")), + randomUsers() + ); final String[] storeUsernames = storeUsers.stream().map(User::principal).collect(Collectors.toList()).toArray(Strings.EMPTY_ARRAY); NativeUsersStore usersStore = mock(NativeUsersStore.class); - TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet()); - TransportGetUsersAction action = new TransportGetUsersAction(Settings.EMPTY, mock(ActionFilters.class), - usersStore, transportService, mock(ReservedRealm.class)); + TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); + TransportGetUsersAction action = new TransportGetUsersAction( + Settings.EMPTY, + mock(ActionFilters.class), + usersStore, + transportService, + mock(ReservedRealm.class) + ); GetUsersRequest request = new GetUsersRequest(); request.usernames(storeUsernames); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/TransportHasPrivilegesActionTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/TransportHasPrivilegesActionTests.java index 56dbc06102850..beab9bcc685a3 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/TransportHasPrivilegesActionTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/TransportHasPrivilegesActionTests.java @@ -49,13 +49,22 @@ public void testHasPrivilegesRequestDoesNotAllowDLSRoleQueryBasedIndicesPrivileg final ThreadContext threadContext = threadPool.getThreadContext(); final SecurityContext context = mock(SecurityContext.class); final User user = new User("user-1", "superuser"); - final Authentication authentication = new Authentication(user, - new Authentication.RealmRef("native", "default_native", "node1"), null); + final Authentication authentication = new Authentication( + user, + new Authentication.RealmRef("native", "default_native", "node1"), + null + ); when(context.getAuthentication()).thenReturn(authentication); threadContext.putTransient(AuthenticationField.AUTHENTICATION_KEY, authentication); - final TransportHasPrivilegesAction transportHasPrivilegesAction = new TransportHasPrivilegesAction(threadPool, - mock(TransportService.class), mock(ActionFilters.class), mock(AuthorizationService.class), mock(NativePrivilegeStore.class), - context, xContentRegistry()); + final TransportHasPrivilegesAction transportHasPrivilegesAction = new TransportHasPrivilegesAction( + threadPool, + mock(TransportService.class), + mock(ActionFilters.class), + mock(AuthorizationService.class), + mock(NativePrivilegeStore.class), + context, + xContentRegistry() + ); final HasPrivilegesRequest request = new HasPrivilegesRequest(); final RoleDescriptor.IndicesPrivileges[] indicesPrivileges = new RoleDescriptor.IndicesPrivileges[randomIntBetween(1, 5)]; diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/TransportPutUserActionTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/TransportPutUserActionTests.java index ec80446a596ff..1ed2930da18cd 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/TransportPutUserActionTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/TransportPutUserActionTests.java @@ -61,8 +61,15 @@ public void testAnonymousUser() { Settings settings = Settings.builder().put(AnonymousUser.ROLES_SETTING.getKey(), "superuser").build(); final AnonymousUser anonymousUser = new AnonymousUser(settings); NativeUsersStore usersStore = mock(NativeUsersStore.class); - TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet()); + TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); TransportPutUserAction action = new TransportPutUserAction(settings, mock(ActionFilters.class), usersStore, transportService); PutUserRequest request = new PutUserRequest(); @@ -90,13 +97,26 @@ public void onFailure(Exception e) { public void testSystemUser() { NativeUsersStore usersStore = mock(NativeUsersStore.class); - TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet()); + TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); TransportPutUserAction action = new TransportPutUserAction(Settings.EMPTY, mock(ActionFilters.class), usersStore, transportService); PutUserRequest request = new PutUserRequest(); - request.username(randomFrom(SystemUser.INSTANCE.principal(), XPackUser.INSTANCE.principal(), - XPackSecurityUser.INSTANCE.principal(), AsyncSearchUser.INSTANCE.principal())); + request.username( + randomFrom( + SystemUser.INSTANCE.principal(), + XPackUser.INSTANCE.principal(), + XPackSecurityUser.INSTANCE.principal(), + AsyncSearchUser.INSTANCE.principal() + ) + ); final AtomicReference throwableRef = new AtomicReference<>(); final AtomicReference responseRef = new AtomicReference<>(); @@ -124,15 +144,26 @@ public void testReservedUser() { Settings settings = Settings.builder().put("path.home", createTempDir()).build(); final ThreadPool threadPool = mock(ThreadPool.class); when(threadPool.getThreadContext()).thenReturn(new ThreadContext(settings)); - ReservedRealm reservedRealm = new ReservedRealm(TestEnvironment.newEnvironment(settings), settings, usersStore, - new AnonymousUser(settings), threadPool); + ReservedRealm reservedRealm = new ReservedRealm( + TestEnvironment.newEnvironment(settings), + settings, + usersStore, + new AnonymousUser(settings), + threadPool + ); PlainActionFuture> userFuture = new PlainActionFuture<>(); reservedRealm.users(userFuture); final User reserved = randomFrom(userFuture.actionGet().toArray(new User[0])); - TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet()); - TransportPutUserAction action = new TransportPutUserAction(Settings.EMPTY, mock(ActionFilters.class), - usersStore, transportService); + TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); + TransportPutUserAction action = new TransportPutUserAction(Settings.EMPTY, mock(ActionFilters.class), usersStore, transportService); PutUserRequest request = new PutUserRequest(); request.username(reserved.principal()); @@ -159,10 +190,16 @@ public void onFailure(Exception e) { public void testValidUser() { final User user = new User("joe"); NativeUsersStore usersStore = mock(NativeUsersStore.class); - TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet()); - TransportPutUserAction action = new TransportPutUserAction(Settings.EMPTY, mock(ActionFilters.class), - usersStore, transportService); + TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); + TransportPutUserAction action = new TransportPutUserAction(Settings.EMPTY, mock(ActionFilters.class), usersStore, transportService); final boolean isCreate = randomBoolean(); final PutUserRequest request = new PutUserRequest(); @@ -203,10 +240,16 @@ public void onFailure(Exception e) { public void testInvalidUser() { NativeUsersStore usersStore = mock(NativeUsersStore.class); - TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet()); - TransportPutUserAction action = new TransportPutUserAction(Settings.EMPTY, mock(ActionFilters.class), - usersStore, transportService); + TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); + TransportPutUserAction action = new TransportPutUserAction(Settings.EMPTY, mock(ActionFilters.class), usersStore, transportService); final PutUserRequest request = new PutUserRequest(); request.username("fóóbár"); @@ -225,10 +268,16 @@ public void testException() { final Exception e = randomFrom(new ElasticsearchSecurityException(""), new IllegalStateException(), new ValidationException()); final User user = new User("joe"); NativeUsersStore usersStore = mock(NativeUsersStore.class); - TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet()); - TransportPutUserAction action = new TransportPutUserAction(Settings.EMPTY, mock(ActionFilters.class), - usersStore, transportService); + TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); + TransportPutUserAction action = new TransportPutUserAction(Settings.EMPTY, mock(ActionFilters.class), usersStore, transportService); final PutUserRequest request = new PutUserRequest(); request.username(user.principal()); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/TransportSetEnabledActionTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/TransportSetEnabledActionTests.java index ddd8b423bfcd7..0019458686197 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/TransportSetEnabledActionTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/TransportSetEnabledActionTests.java @@ -67,11 +67,23 @@ public void testAnonymousUser() throws Exception { new AuthenticationContextSerializer().writeToContext(authentication, threadContext); NativeUsersStore usersStore = mock(NativeUsersStore.class); - TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet()); + TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); final SecurityContext securityContext = new SecurityContext(Settings.EMPTY, threadContext); - TransportSetEnabledAction action = new TransportSetEnabledAction(settings, transportService, mock(ActionFilters.class), - securityContext, usersStore); + TransportSetEnabledAction action = new TransportSetEnabledAction( + settings, + transportService, + mock(ActionFilters.class), + securityContext, + usersStore + ); SetEnabledRequest request = new SetEnabledRequest(); request.username(new AnonymousUser(settings).principal()); @@ -109,15 +121,33 @@ public void testInternalUser() throws Exception { new AuthenticationContextSerializer().writeToContext(authentication, threadContext); NativeUsersStore usersStore = mock(NativeUsersStore.class); - TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet()); + TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); final SecurityContext securityContext = new SecurityContext(Settings.EMPTY, threadContext); - TransportSetEnabledAction action = new TransportSetEnabledAction(Settings.EMPTY, transportService, - mock(ActionFilters.class), securityContext, usersStore); + TransportSetEnabledAction action = new TransportSetEnabledAction( + Settings.EMPTY, + transportService, + mock(ActionFilters.class), + securityContext, + usersStore + ); SetEnabledRequest request = new SetEnabledRequest(); - request.username(randomFrom(SystemUser.INSTANCE.principal(), XPackUser.INSTANCE.principal(), - XPackSecurityUser.INSTANCE.principal(), AsyncSearchUser.INSTANCE.principal())); + request.username( + randomFrom( + SystemUser.INSTANCE.principal(), + XPackUser.INSTANCE.principal(), + XPackSecurityUser.INSTANCE.principal(), + AsyncSearchUser.INSTANCE.principal() + ) + ); request.enabled(randomBoolean()); final AtomicReference throwableRef = new AtomicReference<>(); @@ -164,13 +194,24 @@ public void testValidUser() throws Exception { ActionListener listener = (ActionListener) args[3]; listener.onResponse(null); return null; - }).when(usersStore) - .setEnabled(eq(user.principal()), eq(request.enabled()), eq(request.getRefreshPolicy()), anyActionListener()); - TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet()); + }).when(usersStore).setEnabled(eq(user.principal()), eq(request.enabled()), eq(request.getRefreshPolicy()), anyActionListener()); + TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); final SecurityContext securityContext = new SecurityContext(Settings.EMPTY, threadContext); - TransportSetEnabledAction action = new TransportSetEnabledAction(Settings.EMPTY, transportService, - mock(ActionFilters.class), securityContext, usersStore); + TransportSetEnabledAction action = new TransportSetEnabledAction( + Settings.EMPTY, + transportService, + mock(ActionFilters.class), + securityContext, + usersStore + ); final AtomicReference throwableRef = new AtomicReference<>(); final AtomicReference responseRef = new AtomicReference<>(); @@ -189,8 +230,12 @@ public void onFailure(Exception e) { assertThat(responseRef.get(), is(notNullValue())); assertSame(responseRef.get(), ActionResponse.Empty.INSTANCE); assertThat(throwableRef.get(), is(nullValue())); - verify(usersStore, times(1)) - .setEnabled(eq(user.principal()), eq(request.enabled()), eq(request.getRefreshPolicy()), anyActionListener()); + verify(usersStore, times(1)).setEnabled( + eq(user.principal()), + eq(request.enabled()), + eq(request.getRefreshPolicy()), + anyActionListener() + ); } public void testException() throws Exception { @@ -218,13 +263,24 @@ public void testException() throws Exception { ActionListener listener = (ActionListener) args[3]; listener.onFailure(e); return null; - }).when(usersStore) - .setEnabled(eq(user.principal()), eq(request.enabled()), eq(request.getRefreshPolicy()), anyActionListener()); - TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet()); + }).when(usersStore).setEnabled(eq(user.principal()), eq(request.enabled()), eq(request.getRefreshPolicy()), anyActionListener()); + TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); final SecurityContext securityContext = new SecurityContext(Settings.EMPTY, threadContext); - TransportSetEnabledAction action = new TransportSetEnabledAction(Settings.EMPTY, transportService, - mock(ActionFilters.class), securityContext, usersStore); + TransportSetEnabledAction action = new TransportSetEnabledAction( + Settings.EMPTY, + transportService, + mock(ActionFilters.class), + securityContext, + usersStore + ); final AtomicReference throwableRef = new AtomicReference<>(); final AtomicReference responseRef = new AtomicReference<>(); @@ -243,8 +299,12 @@ public void onFailure(Exception e) { assertThat(responseRef.get(), is(nullValue())); assertThat(throwableRef.get(), is(notNullValue())); assertThat(throwableRef.get(), sameInstance(e)); - verify(usersStore, times(1)) - .setEnabled(eq(user.principal()), eq(request.enabled()), eq(request.getRefreshPolicy()), anyActionListener()); + verify(usersStore, times(1)).setEnabled( + eq(user.principal()), + eq(request.enabled()), + eq(request.getRefreshPolicy()), + anyActionListener() + ); } public void testUserModifyingThemselves() throws Exception { @@ -263,11 +323,23 @@ public void testUserModifyingThemselves() throws Exception { request.username(user.principal()); request.enabled(randomBoolean()); request.setRefreshPolicy(randomFrom(RefreshPolicy.values())); - TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, - TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet()); + TransportService transportService = new TransportService( + Settings.EMPTY, + mock(Transport.class), + null, + TransportService.NOOP_TRANSPORT_INTERCEPTOR, + x -> null, + null, + Collections.emptySet() + ); final SecurityContext securityContext = new SecurityContext(Settings.EMPTY, threadContext); - TransportSetEnabledAction action = new TransportSetEnabledAction(Settings.EMPTY, transportService, - mock(ActionFilters.class), securityContext, usersStore); + TransportSetEnabledAction action = new TransportSetEnabledAction( + Settings.EMPTY, + transportService, + mock(ActionFilters.class), + securityContext, + usersStore + ); final AtomicReference throwableRef = new AtomicReference<>(); final AtomicReference responseRef = new AtomicReference<>(); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/audit/AuditLevelTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/audit/AuditLevelTests.java index ae6981deecd87..6e0fda2c6c743 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/audit/AuditLevelTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/audit/AuditLevelTests.java @@ -7,7 +7,6 @@ package org.elasticsearch.xpack.security.audit; import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.xpack.security.audit.AuditLevel; import java.util.Collections; import java.util.EnumSet; diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/audit/AuditTrailServiceTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/audit/AuditTrailServiceTests.java index f0e97f7e87569..1a146fa32a155 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/audit/AuditTrailServiceTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/audit/AuditTrailServiceTests.java @@ -73,21 +73,27 @@ public void testLogWhenLicenseProhibitsAuditing() throws Exception { Loggers.addAppender(auditTrailServiceLogger, mockLogAppender); when(licenseState.getOperationMode()).thenReturn(randomFrom(License.OperationMode.values())); if (isAuditingAllowed) { - mockLogAppender.addExpectation(new MockLogAppender.UnseenEventExpectation( + mockLogAppender.addExpectation( + new MockLogAppender.UnseenEventExpectation( "audit disabled because of license", AuditTrailService.class.getName(), Level.WARN, - "Auditing logging is DISABLED because the currently active license [" + - licenseState.getOperationMode() + "] does not permit it" - )); + "Auditing logging is DISABLED because the currently active license [" + + licenseState.getOperationMode() + + "] does not permit it" + ) + ); } else { - mockLogAppender.addExpectation(new MockLogAppender.SeenEventExpectation( + mockLogAppender.addExpectation( + new MockLogAppender.SeenEventExpectation( "audit disabled because of license", AuditTrailService.class.getName(), Level.WARN, - "Auditing logging is DISABLED because the currently active license [" + - licenseState.getOperationMode() + "] does not permit it" - )); + "Auditing logging is DISABLED because the currently active license [" + + licenseState.getOperationMode() + + "] does not permit it" + ) + ); } for (int i = 1; i <= randomIntBetween(2, 6); i++) { service.get(); @@ -102,12 +108,14 @@ public void testNoLogRecentlyWhenLicenseProhibitsAuditing() throws Exception { Logger auditTrailServiceLogger = LogManager.getLogger(AuditTrailService.class); Loggers.addAppender(auditTrailServiceLogger, mockLogAppender); service.nextLogInstantAtomic.set(randomFrom(Instant.now().minus(Duration.ofMinutes(5)), Instant.now())); - mockLogAppender.addExpectation(new MockLogAppender.UnseenEventExpectation( + mockLogAppender.addExpectation( + new MockLogAppender.UnseenEventExpectation( "audit disabled because of license", AuditTrailService.class.getName(), Level.WARN, "Security auditing is DISABLED because the currently active license [*] does not permit it" - )); + ) + ); for (int i = 1; i <= randomIntBetween(2, 6); i++) { service.get(); } @@ -207,10 +215,15 @@ public void testAnonymousAccess() throws Exception { } public void testAccessGranted() throws Exception { - Authentication authentication =new Authentication(new User("_username", "r1"), new RealmRef(null, null, null), - new RealmRef(null, null, null)); - AuthorizationInfo authzInfo = - () -> Collections.singletonMap(PRINCIPAL_ROLES_FIELD_NAME, new String[] { randomAlphaOfLengthBetween(1, 6) }); + Authentication authentication = new Authentication( + new User("_username", "r1"), + new RealmRef(null, null, null), + new RealmRef(null, null, null) + ); + AuthorizationInfo authzInfo = () -> Collections.singletonMap( + PRINCIPAL_ROLES_FIELD_NAME, + new String[] { randomAlphaOfLengthBetween(1, 6) } + ); final String requestId = randomAlphaOfLengthBetween(6, 12); service.get().accessGranted(requestId, authentication, "_action", request, authzInfo); verify(licenseState).isAllowed(Security.AUDITING_FEATURE); @@ -224,10 +237,15 @@ public void testAccessGranted() throws Exception { } public void testAccessDenied() throws Exception { - Authentication authentication = new Authentication(new User("_username", "r1"), new RealmRef(null, null, null), - new RealmRef(null, null, null)); - AuthorizationInfo authzInfo = - () -> Collections.singletonMap(PRINCIPAL_ROLES_FIELD_NAME, new String[] { randomAlphaOfLengthBetween(1, 6) }); + Authentication authentication = new Authentication( + new User("_username", "r1"), + new RealmRef(null, null, null), + new RealmRef(null, null, null) + ); + AuthorizationInfo authzInfo = () -> Collections.singletonMap( + PRINCIPAL_ROLES_FIELD_NAME, + new String[] { randomAlphaOfLengthBetween(1, 6) } + ); final String requestId = randomAlphaOfLengthBetween(6, 12); service.get().accessDenied(requestId, authentication, "_action", request, authzInfo); verify(licenseState).isAllowed(Security.AUDITING_FEATURE); @@ -269,8 +287,11 @@ public void testConnectionDenied() throws Exception { } public void testAuthenticationSuccessRest() throws Exception { - Authentication authentication = new Authentication(new User("_username", "r1"), new RealmRef("_realm", null, null), - new RealmRef(null, null, null)); + Authentication authentication = new Authentication( + new User("_username", "r1"), + new RealmRef("_realm", null, null), + new RealmRef(null, null, null) + ); final String requestId = randomAlphaOfLengthBetween(6, 12); service.get().authenticationSuccess(requestId, authentication, restRequest); verify(licenseState).isAllowed(Security.AUDITING_FEATURE); @@ -284,8 +305,11 @@ public void testAuthenticationSuccessRest() throws Exception { } public void testAuthenticationSuccessTransport() throws Exception { - Authentication authentication = new Authentication(new User("_username", "r1"), new RealmRef("_realm", null, null), - new RealmRef(null, null, null)); + Authentication authentication = new Authentication( + new User("_username", "r1"), + new RealmRef("_realm", null, null), + new RealmRef(null, null, null) + ); final String requestId = randomAlphaOfLengthBetween(6, 12); service.get().authenticationSuccess(requestId, authentication, "_action", request); verify(licenseState).isAllowed(Security.AUDITING_FEATURE); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/audit/AuditUtilTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/audit/AuditUtilTests.java index 346e64370c277..3dd76f9fa125f 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/audit/AuditUtilTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/audit/AuditUtilTests.java @@ -23,7 +23,7 @@ public class AuditUtilTests extends ESTestCase { public void testIndicesRequest() { - assertNull(AuditUtil.indices(new MockIndicesRequest(null, (String[])null))); + assertNull(AuditUtil.indices(new MockIndicesRequest(null, (String[]) null))); final int numberOfIndices = randomIntBetween(1, 100); List expectedIndices = new ArrayList<>(); final boolean includeDuplicates = randomBoolean(); @@ -35,8 +35,9 @@ public void testIndicesRequest() { } } final Set uniqueExpectedIndices = new HashSet<>(expectedIndices); - final Set result = AuditUtil.indices(new MockIndicesRequest(null, - expectedIndices.toArray(new String[expectedIndices.size()]))); + final Set result = AuditUtil.indices( + new MockIndicesRequest(null, expectedIndices.toArray(new String[expectedIndices.size()])) + ); assertNotNull(result); assertEquals(uniqueExpectedIndices.size(), result.size()); assertThat(result, hasItems(uniqueExpectedIndices.toArray(Strings.EMPTY_ARRAY))); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/audit/logfile/LoggingAuditTrailFilterTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/audit/logfile/LoggingAuditTrailFilterTests.java index 7228823f39403..a4486e2ff2be2 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/audit/logfile/LoggingAuditTrailFilterTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/audit/logfile/LoggingAuditTrailFilterTests.java @@ -17,15 +17,14 @@ import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentType; -import org.mockito.Mockito; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.rest.FakeRestRequest; import org.elasticsearch.test.rest.FakeRestRequest.Builder; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportRequest; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.security.audit.logfile.CapturingLogger; import org.elasticsearch.xpack.core.security.authc.Authentication; import org.elasticsearch.xpack.core.security.authc.Authentication.RealmRef; @@ -42,6 +41,7 @@ import org.elasticsearch.xpack.security.support.SecurityIndexManager; import org.elasticsearch.xpack.security.transport.filter.SecurityIpFilterRule; import org.junit.Before; +import org.mockito.Mockito; import org.mockito.stubbing.Answer; import java.io.IOException; @@ -75,12 +75,12 @@ public class LoggingAuditTrailFilterTests extends ESTestCase { @Before public void init() throws Exception { settings = Settings.builder() - .put(LoggingAuditTrail.EMIT_HOST_ADDRESS_SETTING.getKey(), randomBoolean()) - .put(LoggingAuditTrail.EMIT_HOST_NAME_SETTING.getKey(), randomBoolean()) - .put(LoggingAuditTrail.EMIT_NODE_NAME_SETTING.getKey(), randomBoolean()) - .put(LoggingAuditTrail.INCLUDE_REQUEST_BODY.getKey(), randomBoolean()) - .put(LoggingAuditTrail.INCLUDE_EVENT_SETTINGS.getKey(), "_all") - .build(); + .put(LoggingAuditTrail.EMIT_HOST_ADDRESS_SETTING.getKey(), randomBoolean()) + .put(LoggingAuditTrail.EMIT_HOST_NAME_SETTING.getKey(), randomBoolean()) + .put(LoggingAuditTrail.EMIT_NODE_NAME_SETTING.getKey(), randomBoolean()) + .put(LoggingAuditTrail.INCLUDE_REQUEST_BODY.getKey(), randomBoolean()) + .put(LoggingAuditTrail.INCLUDE_EVENT_SETTINGS.getKey(), "_all") + .build(); localNode = mock(DiscoveryNode.class); when(localNode.getHostAddress()).thenReturn(buildNewFakeTransportAddress().toString()); clusterService = mock(ClusterService.class); @@ -92,8 +92,15 @@ public void init() throws Exception { arg0.updateLocalNodeInfo(localNode); return null; }).when(clusterService).addListener(Mockito.isA(LoggingAuditTrail.class)); - apiKeyService = new ApiKeyService(settings, Clock.systemUTC(), mock(Client.class), mock(SecurityIndexManager.class), clusterService, - mock(CacheInvalidatorRegistry.class), mock(ThreadPool.class)); + apiKeyService = new ApiKeyService( + settings, + Clock.systemUTC(), + mock(Client.class), + mock(SecurityIndexManager.class), + clusterService, + mock(CacheInvalidatorRegistry.class), + mock(ThreadPool.class) + ); } public void testPolicyDoesNotMatchNullValuesInEvent() throws Exception { @@ -121,68 +128,163 @@ public void testPolicyDoesNotMatchNullValuesInEvent() throws Exception { settingsBuilder.putList("xpack.security.audit.logfile.events.ignore_filters.indicesPolicy.indices", filteredIndices); // filter by actions final List filteredActions = randomNonEmptyListOfFilteredActions(); - settingsBuilder.putList("xpack.security.audit.logfile.events.ignore_filters.actionsPolicy.actions", - filteredActions); + settingsBuilder.putList("xpack.security.audit.logfile.events.ignore_filters.actionsPolicy.actions", filteredActions); final LoggingAuditTrail auditTrail = new LoggingAuditTrail(settingsBuilder.build(), clusterService, logger, threadContext); // user field matches - assertTrue("Matches the user filter predicate.", auditTrail.eventFilterPolicyRegistry.ignorePredicate().test( - new AuditEventMetaInfo(Optional.of(randomFrom(filteredUsers)), Optional.empty(), Optional.empty(), Optional.empty(), - Optional.empty()))); + assertTrue( + "Matches the user filter predicate.", + auditTrail.eventFilterPolicyRegistry.ignorePredicate() + .test( + new AuditEventMetaInfo( + Optional.of(randomFrom(filteredUsers)), + Optional.empty(), + Optional.empty(), + Optional.empty(), + Optional.empty() + ) + ) + ); final User unfilteredUser = mock(User.class); if (randomBoolean()) { when(unfilteredUser.authenticatedUser()).thenReturn(new User(randomFrom(filteredUsernames))); } // null user field does NOT match - assertFalse("Does not match the user filter predicate because of null username.", - auditTrail.eventFilterPolicyRegistry.ignorePredicate() - .test(new AuditEventMetaInfo(Optional.of(unfilteredUser), Optional.empty(), Optional.empty(), Optional.empty(), - Optional.empty()))); + assertFalse( + "Does not match the user filter predicate because of null username.", + auditTrail.eventFilterPolicyRegistry.ignorePredicate() + .test( + new AuditEventMetaInfo( + Optional.of(unfilteredUser), + Optional.empty(), + Optional.empty(), + Optional.empty(), + Optional.empty() + ) + ) + ); // realm field matches - assertTrue("Matches the realm filter predicate.", auditTrail.eventFilterPolicyRegistry.ignorePredicate().test( - new AuditEventMetaInfo(Optional.empty(), Optional.of(randomFrom(filteredRealms)), Optional.empty(), Optional.empty(), - Optional.empty()))); + assertTrue( + "Matches the realm filter predicate.", + auditTrail.eventFilterPolicyRegistry.ignorePredicate() + .test( + new AuditEventMetaInfo( + Optional.empty(), + Optional.of(randomFrom(filteredRealms)), + Optional.empty(), + Optional.empty(), + Optional.empty() + ) + ) + ); // null realm field does NOT match - assertFalse("Does not match the realm filter predicate because of null realm.", - auditTrail.eventFilterPolicyRegistry.ignorePredicate() - .test(new AuditEventMetaInfo(Optional.empty(), Optional.ofNullable(null), Optional.empty(), Optional.empty(), - Optional.empty()))); + assertFalse( + "Does not match the realm filter predicate because of null realm.", + auditTrail.eventFilterPolicyRegistry.ignorePredicate() + .test( + new AuditEventMetaInfo( + Optional.empty(), + Optional.ofNullable(null), + Optional.empty(), + Optional.empty(), + Optional.empty() + ) + ) + ); // role field matches - assertTrue("Matches the role filter predicate.", auditTrail.eventFilterPolicyRegistry.ignorePredicate() - .test(new AuditEventMetaInfo(Optional.empty(), Optional.empty(), - Optional.of(authzInfo( - randomSubsetOf(randomIntBetween(1, filteredRoles.size()), filteredRoles).toArray(new String[0]))), - Optional.empty(), Optional.empty()))); + assertTrue( + "Matches the role filter predicate.", + auditTrail.eventFilterPolicyRegistry.ignorePredicate() + .test( + new AuditEventMetaInfo( + Optional.empty(), + Optional.empty(), + Optional.of( + authzInfo(randomSubsetOf(randomIntBetween(1, filteredRoles.size()), filteredRoles).toArray(new String[0])) + ), + Optional.empty(), + Optional.empty() + ) + ) + ); // action field matches Random random = random(); - assertTrue("Matches the actions filter predicate.", auditTrail.eventFilterPolicyRegistry.ignorePredicate().test( - new AuditEventMetaInfo(Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), - Optional.of(randomFrom(filteredActions))))); + assertTrue( + "Matches the actions filter predicate.", + auditTrail.eventFilterPolicyRegistry.ignorePredicate() + .test( + new AuditEventMetaInfo( + Optional.empty(), + Optional.empty(), + Optional.empty(), + Optional.empty(), + Optional.of(randomFrom(filteredActions)) + ) + ) + ); // null privilege field does NOT match - assertFalse("Does not matches the actions filter predicate.", auditTrail.eventFilterPolicyRegistry.ignorePredicate() - .test(new AuditEventMetaInfo(Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), - Optional.ofNullable(null)))); + assertFalse( + "Does not matches the actions filter predicate.", + auditTrail.eventFilterPolicyRegistry.ignorePredicate() + .test( + new AuditEventMetaInfo( + Optional.empty(), + Optional.empty(), + Optional.empty(), + Optional.empty(), + Optional.ofNullable(null) + ) + ) + ); final List unfilteredRoles = new ArrayList<>(); unfilteredRoles.add(null); unfilteredRoles.addAll(randomSubsetOf(randomIntBetween(1, filteredRoles.size()), filteredRoles)); // null role among roles field does NOT match - assertFalse("Does not match the role filter predicate because of null role.", - auditTrail.eventFilterPolicyRegistry.ignorePredicate().test(new AuditEventMetaInfo(Optional.empty(), Optional.empty(), - Optional.of(authzInfo(unfilteredRoles.toArray(new String[0]))), Optional.empty(), Optional.empty()))); + assertFalse( + "Does not match the role filter predicate because of null role.", + auditTrail.eventFilterPolicyRegistry.ignorePredicate() + .test( + new AuditEventMetaInfo( + Optional.empty(), + Optional.empty(), + Optional.of(authzInfo(unfilteredRoles.toArray(new String[0]))), + Optional.empty(), + Optional.empty() + ) + ) + ); // indices field matches - assertTrue("Matches the index filter predicate.", - auditTrail.eventFilterPolicyRegistry.ignorePredicate().test(new AuditEventMetaInfo(Optional.empty(), Optional.empty(), + assertTrue( + "Matches the index filter predicate.", + auditTrail.eventFilterPolicyRegistry.ignorePredicate() + .test( + new AuditEventMetaInfo( + Optional.empty(), + Optional.empty(), Optional.empty(), Optional.of(randomSubsetOf(randomIntBetween(1, filteredIndices.size()), filteredIndices).toArray(new String[0])), - Optional.empty()))); + Optional.empty() + ) + ) + ); final List unfilteredIndices = new ArrayList<>(); unfilteredIndices.add(null); unfilteredIndices.addAll(randomSubsetOf(randomIntBetween(1, filteredIndices.size()), filteredIndices)); // null index among indices field does NOT match - assertFalse("Does not match the indices filter predicate because of null index.", - auditTrail.eventFilterPolicyRegistry.ignorePredicate().test(new AuditEventMetaInfo(Optional.empty(), Optional.empty(), - Optional.empty(), Optional.of(unfilteredIndices.toArray(new String[0])), Optional.empty()))); + assertFalse( + "Does not match the indices filter predicate because of null index.", + auditTrail.eventFilterPolicyRegistry.ignorePredicate() + .test( + new AuditEventMetaInfo( + Optional.empty(), + Optional.empty(), + Optional.empty(), + Optional.of(unfilteredIndices.toArray(new String[0])), + Optional.empty() + ) + ) + ); } public void testSingleCompletePolicyPredicate() throws Exception { @@ -211,96 +313,180 @@ public void testSingleCompletePolicyPredicate() throws Exception { settingsBuilder.putList("xpack.security.audit.logfile.events.ignore_filters.completeFilterPolicy.indices", filteredIndices); // filter by actions final List filteredActions = randomNonEmptyListOfFilteredActions(); - settingsBuilder.putList("xpack.security.audit.logfile.events.ignore_filters.completeFilterPolicy.actions", - filteredActions); + settingsBuilder.putList("xpack.security.audit.logfile.events.ignore_filters.completeFilterPolicy.actions", filteredActions); final LoggingAuditTrail auditTrail = new LoggingAuditTrail(settingsBuilder.build(), clusterService, logger, threadContext); // all fields match Random random = random(); - assertTrue("Matches the filter predicate.", auditTrail.eventFilterPolicyRegistry.ignorePredicate() - .test(new AuditEventMetaInfo( - Optional.of(randomFrom(filteredUsers)), Optional.of(randomFrom(filteredRealms)), - Optional.of(authzInfo(randomSubsetOf(randomIntBetween(1, filteredRoles.size()), filteredRoles).toArray(new String[0]))), - Optional.of(randomSubsetOf(randomIntBetween(1, filteredIndices.size()), filteredIndices).toArray(new String[0])), - Optional.of(randomFrom(filteredActions))))); + assertTrue( + "Matches the filter predicate.", + auditTrail.eventFilterPolicyRegistry.ignorePredicate() + .test( + new AuditEventMetaInfo( + Optional.of(randomFrom(filteredUsers)), + Optional.of(randomFrom(filteredRealms)), + Optional.of( + authzInfo(randomSubsetOf(randomIntBetween(1, filteredRoles.size()), filteredRoles).toArray(new String[0])) + ), + Optional.of(randomSubsetOf(randomIntBetween(1, filteredIndices.size()), filteredIndices).toArray(new String[0])), + Optional.of(randomFrom(filteredActions)) + ) + ) + ); final User unfilteredUser; if (randomBoolean()) { unfilteredUser = new User(UNFILTER_MARKER + randomAlphaOfLengthBetween(1, 8)); } else { - unfilteredUser = new User(new User(UNFILTER_MARKER + randomAlphaOfLengthBetween(1, 8)), - new User(randomFrom(filteredUsers).principal())); + unfilteredUser = new User( + new User(UNFILTER_MARKER + randomAlphaOfLengthBetween(1, 8)), + new User(randomFrom(filteredUsers).principal()) + ); } // one field does not match or is empty - assertFalse("Does not match the filter predicate because of the user.", - auditTrail.eventFilterPolicyRegistry.ignorePredicate().test(new AuditEventMetaInfo(Optional.of(unfilteredUser), + assertFalse( + "Does not match the filter predicate because of the user.", + auditTrail.eventFilterPolicyRegistry.ignorePredicate() + .test( + new AuditEventMetaInfo( + Optional.of(unfilteredUser), Optional.of(randomFrom(filteredRealms)), - Optional.of(authzInfo(randomSubsetOf(randomIntBetween(1, filteredRoles.size()), filteredRoles) - .toArray(new String[0]))), + Optional.of( + authzInfo(randomSubsetOf(randomIntBetween(1, filteredRoles.size()), filteredRoles).toArray(new String[0])) + ), Optional.of(randomSubsetOf(randomIntBetween(1, filteredIndices.size()), filteredIndices).toArray(new String[0])), - Optional.of(randomFrom(filteredActions))))); - assertFalse("Does not match the filter predicate because of the empty user.", - auditTrail.eventFilterPolicyRegistry.ignorePredicate().test(new AuditEventMetaInfo(Optional.empty(), + Optional.of(randomFrom(filteredActions)) + ) + ) + ); + assertFalse( + "Does not match the filter predicate because of the empty user.", + auditTrail.eventFilterPolicyRegistry.ignorePredicate() + .test( + new AuditEventMetaInfo( + Optional.empty(), Optional.of(randomFrom(filteredRealms)), - Optional.of(authzInfo(randomSubsetOf(randomIntBetween(1, filteredRoles.size()), filteredRoles) - .toArray(new String[0]))), + Optional.of( + authzInfo(randomSubsetOf(randomIntBetween(1, filteredRoles.size()), filteredRoles).toArray(new String[0])) + ), Optional.of(randomSubsetOf(randomIntBetween(1, filteredIndices.size()), filteredIndices).toArray(new String[0])), - Optional.of(randomFrom(filteredActions))))); - assertFalse("Does not match the filter predicate because of the realm.", - auditTrail.eventFilterPolicyRegistry.ignorePredicate().test(new AuditEventMetaInfo(Optional.of(randomFrom(filteredUsers)), + Optional.of(randomFrom(filteredActions)) + ) + ) + ); + assertFalse( + "Does not match the filter predicate because of the realm.", + auditTrail.eventFilterPolicyRegistry.ignorePredicate() + .test( + new AuditEventMetaInfo( + Optional.of(randomFrom(filteredUsers)), Optional.of(UNFILTER_MARKER + randomAlphaOfLengthBetween(1, 8)), - Optional.of(authzInfo(randomSubsetOf(randomIntBetween(1, filteredRoles.size()), filteredRoles) - .toArray(new String[0]))), + Optional.of( + authzInfo(randomSubsetOf(randomIntBetween(1, filteredRoles.size()), filteredRoles).toArray(new String[0])) + ), Optional.of(randomSubsetOf(randomIntBetween(1, filteredIndices.size()), filteredIndices).toArray(new String[0])), - Optional.of(randomFrom(filteredActions))))); - assertFalse("Does not match the filter predicate because of the empty realm.", - auditTrail.eventFilterPolicyRegistry.ignorePredicate().test(new AuditEventMetaInfo(Optional.of(randomFrom(filteredUsers)), + Optional.of(randomFrom(filteredActions)) + ) + ) + ); + assertFalse( + "Does not match the filter predicate because of the empty realm.", + auditTrail.eventFilterPolicyRegistry.ignorePredicate() + .test( + new AuditEventMetaInfo( + Optional.of(randomFrom(filteredUsers)), Optional.empty(), - Optional.of(authzInfo(randomSubsetOf(randomIntBetween(1, filteredRoles.size()), filteredRoles) - .toArray(new String[0]))), + Optional.of( + authzInfo(randomSubsetOf(randomIntBetween(1, filteredRoles.size()), filteredRoles).toArray(new String[0])) + ), + Optional.of(randomSubsetOf(randomIntBetween(1, filteredIndices.size()), filteredIndices).toArray(new String[0])), + Optional.of(randomFrom(filteredActions)) + ) + ) + ); + assertFalse( + "Does not match the filter predicate because of the empty actions.", + auditTrail.eventFilterPolicyRegistry.ignorePredicate() + .test( + new AuditEventMetaInfo( + Optional.of(randomFrom(filteredUsers)), + Optional.of(randomFrom(filteredRealms)), + Optional.of( + authzInfo(randomSubsetOf(randomIntBetween(1, filteredRoles.size()), filteredRoles).toArray(new String[0])) + ), Optional.of(randomSubsetOf(randomIntBetween(1, filteredIndices.size()), filteredIndices).toArray(new String[0])), - Optional.of(randomFrom(filteredActions))))); - assertFalse("Does not match the filter predicate because of the empty actions.", - auditTrail.eventFilterPolicyRegistry.ignorePredicate().test(new AuditEventMetaInfo(Optional.of(randomFrom(filteredUsers)), - Optional.of(randomFrom(filteredRealms)), - Optional.of(authzInfo(randomSubsetOf(randomIntBetween(1, filteredRoles.size()), filteredRoles) - .toArray(new String[0]))), - Optional.of(randomSubsetOf(randomIntBetween(1, filteredIndices.size()), filteredIndices).toArray(new String[0])), - Optional.empty()))); + Optional.empty() + ) + ) + ); final List someRolesDoNotMatch = new ArrayList<>(randomSubsetOf(randomIntBetween(0, filteredRoles.size()), filteredRoles)); for (int i = 0; i < randomIntBetween(1, 8); i++) { someRolesDoNotMatch.add(UNFILTER_MARKER + randomAlphaOfLengthBetween(1, 8)); } - assertFalse("Does not match the filter predicate because of some of the roles.", - auditTrail.eventFilterPolicyRegistry.ignorePredicate().test(new AuditEventMetaInfo(Optional.of(randomFrom(filteredUsers)), - Optional.of(randomFrom(filteredRealms)), Optional.of(authzInfo(someRolesDoNotMatch.toArray(new String[0]))), + assertFalse( + "Does not match the filter predicate because of some of the roles.", + auditTrail.eventFilterPolicyRegistry.ignorePredicate() + .test( + new AuditEventMetaInfo( + Optional.of(randomFrom(filteredUsers)), + Optional.of(randomFrom(filteredRealms)), + Optional.of(authzInfo(someRolesDoNotMatch.toArray(new String[0]))), Optional.of(randomSubsetOf(randomIntBetween(1, filteredIndices.size()), filteredIndices).toArray(new String[0])), - Optional.of(randomFrom(filteredActions))))); + Optional.of(randomFrom(filteredActions)) + ) + ) + ); final Optional emptyRoles = randomBoolean() ? Optional.empty() : Optional.of(authzInfo(new String[0])); - assertFalse("Does not match the filter predicate because of the empty roles.", - auditTrail.eventFilterPolicyRegistry.ignorePredicate().test(new AuditEventMetaInfo(Optional.of(randomFrom(filteredUsers)), - Optional.of(randomFrom(filteredRealms)), emptyRoles, + assertFalse( + "Does not match the filter predicate because of the empty roles.", + auditTrail.eventFilterPolicyRegistry.ignorePredicate() + .test( + new AuditEventMetaInfo( + Optional.of(randomFrom(filteredUsers)), + Optional.of(randomFrom(filteredRealms)), + emptyRoles, Optional.of(randomSubsetOf(randomIntBetween(1, filteredIndices.size()), filteredIndices).toArray(new String[0])), - Optional.of(randomFrom(filteredActions))))); + Optional.of(randomFrom(filteredActions)) + ) + ) + ); final List someIndicesDoNotMatch = new ArrayList<>( - randomSubsetOf(randomIntBetween(0, filteredIndices.size()), filteredIndices)); + randomSubsetOf(randomIntBetween(0, filteredIndices.size()), filteredIndices) + ); for (int i = 0; i < randomIntBetween(1, 8); i++) { someIndicesDoNotMatch.add(UNFILTER_MARKER + randomAlphaOfLengthBetween(1, 8)); } - assertFalse("Does not match the filter predicate because of some of the indices.", - auditTrail.eventFilterPolicyRegistry.ignorePredicate() - .test(new AuditEventMetaInfo(Optional.of(randomFrom(filteredUsers)), Optional.of(randomFrom(filteredRealms)), - Optional.of(authzInfo(randomSubsetOf(randomIntBetween(1, filteredRoles.size()), filteredRoles) - .toArray(new String[0]))), + assertFalse( + "Does not match the filter predicate because of some of the indices.", + auditTrail.eventFilterPolicyRegistry.ignorePredicate() + .test( + new AuditEventMetaInfo( + Optional.of(randomFrom(filteredUsers)), + Optional.of(randomFrom(filteredRealms)), + Optional.of( + authzInfo(randomSubsetOf(randomIntBetween(1, filteredRoles.size()), filteredRoles).toArray(new String[0])) + ), Optional.of(someIndicesDoNotMatch.toArray(new String[0])), - Optional.of(randomFrom(filteredActions))))); + Optional.of(randomFrom(filteredActions)) + ) + ) + ); final Optional emptyIndices = randomBoolean() ? Optional.empty() : Optional.of(new String[0]); - assertFalse("Does not match the filter predicate because of the empty indices.", - auditTrail.eventFilterPolicyRegistry.ignorePredicate() - .test(new AuditEventMetaInfo(Optional.of(randomFrom(filteredUsers)), Optional.of(randomFrom(filteredRealms)), - Optional.of(authzInfo(randomSubsetOf(randomIntBetween(1, filteredRoles.size()), filteredRoles) - .toArray(new String[0]))), - emptyIndices, Optional.of(randomFrom(filteredActions))))); + assertFalse( + "Does not match the filter predicate because of the empty indices.", + auditTrail.eventFilterPolicyRegistry.ignorePredicate() + .test( + new AuditEventMetaInfo( + Optional.of(randomFrom(filteredUsers)), + Optional.of(randomFrom(filteredRealms)), + Optional.of( + authzInfo(randomSubsetOf(randomIntBetween(1, filteredRoles.size()), filteredRoles).toArray(new String[0])) + ), + emptyIndices, + Optional.of(randomFrom(filteredActions)) + ) + ) + ); } public void testSingleCompleteWithEmptyFieldPolicyPredicate() throws Exception { @@ -336,106 +522,209 @@ public void testSingleCompleteWithEmptyFieldPolicyPredicate() throws Exception { filteredIndices.remove(""); // filter by actions final List filteredActions = randomNonEmptyListOfFilteredActions(); - settingsBuilder.putList("xpack.security.audit.logfile.events.ignore_filters.completeFilterPolicy.actions", - filteredActions); + settingsBuilder.putList("xpack.security.audit.logfile.events.ignore_filters.completeFilterPolicy.actions", filteredActions); final LoggingAuditTrail auditTrail = new LoggingAuditTrail(settingsBuilder.build(), clusterService, logger, threadContext); // all fields match Random random = random(); - assertTrue("Matches the filter predicate.", - auditTrail.eventFilterPolicyRegistry.ignorePredicate().test(new AuditEventMetaInfo(Optional.of(randomFrom(filteredUsers)), + assertTrue( + "Matches the filter predicate.", + auditTrail.eventFilterPolicyRegistry.ignorePredicate() + .test( + new AuditEventMetaInfo( + Optional.of(randomFrom(filteredUsers)), Optional.of(randomFrom(filteredRealms)), - Optional.of(authzInfo(randomSubsetOf(randomIntBetween(1, filteredRoles.size()), filteredRoles) - .toArray(new String[0]))), + Optional.of( + authzInfo(randomSubsetOf(randomIntBetween(1, filteredRoles.size()), filteredRoles).toArray(new String[0])) + ), Optional.of(randomSubsetOf(randomIntBetween(1, filteredIndices.size()), filteredIndices).toArray(new String[0])), - Optional.of(randomFrom(filteredActions))))); + Optional.of(randomFrom(filteredActions)) + ) + ) + ); final User unfilteredUser; if (randomBoolean()) { unfilteredUser = new User(UNFILTER_MARKER + randomAlphaOfLengthBetween(1, 8)); } else { - unfilteredUser = new User(new User(UNFILTER_MARKER + randomAlphaOfLengthBetween(1, 8)), - new User(randomFrom(filteredUsers).principal())); + unfilteredUser = new User( + new User(UNFILTER_MARKER + randomAlphaOfLengthBetween(1, 8)), + new User(randomFrom(filteredUsers).principal()) + ); } // one field does not match or is empty - assertFalse("Does not match the filter predicate because of the user.", - auditTrail.eventFilterPolicyRegistry.ignorePredicate().test(new AuditEventMetaInfo(Optional.of(unfilteredUser), + assertFalse( + "Does not match the filter predicate because of the user.", + auditTrail.eventFilterPolicyRegistry.ignorePredicate() + .test( + new AuditEventMetaInfo( + Optional.of(unfilteredUser), Optional.of(randomFrom(filteredRealms)), - Optional.of(authzInfo(randomSubsetOf(randomIntBetween(1, filteredRoles.size()), filteredRoles) - .toArray(new String[0]))), + Optional.of( + authzInfo(randomSubsetOf(randomIntBetween(1, filteredRoles.size()), filteredRoles).toArray(new String[0])) + ), Optional.of(randomSubsetOf(randomIntBetween(1, filteredIndices.size()), filteredIndices).toArray(new String[0])), - Optional.of(randomFrom(filteredActions))))); - assertTrue("Matches the filter predicate because of the empty user.", - auditTrail.eventFilterPolicyRegistry.ignorePredicate().test(new AuditEventMetaInfo(Optional.empty(), + Optional.of(randomFrom(filteredActions)) + ) + ) + ); + assertTrue( + "Matches the filter predicate because of the empty user.", + auditTrail.eventFilterPolicyRegistry.ignorePredicate() + .test( + new AuditEventMetaInfo( + Optional.empty(), Optional.of(randomFrom(filteredRealms)), - Optional.of(authzInfo(randomSubsetOf(randomIntBetween(1, filteredRoles.size()), filteredRoles) - .toArray(new String[0]))), + Optional.of( + authzInfo(randomSubsetOf(randomIntBetween(1, filteredRoles.size()), filteredRoles).toArray(new String[0])) + ), Optional.of(randomSubsetOf(randomIntBetween(1, filteredIndices.size()), filteredIndices).toArray(new String[0])), - Optional.of(randomFrom(filteredActions))))); - assertFalse("Does not match the filter predicate because of the realm.", - auditTrail.eventFilterPolicyRegistry.ignorePredicate().test(new AuditEventMetaInfo(Optional.of(randomFrom(filteredUsers)), + Optional.of(randomFrom(filteredActions)) + ) + ) + ); + assertFalse( + "Does not match the filter predicate because of the realm.", + auditTrail.eventFilterPolicyRegistry.ignorePredicate() + .test( + new AuditEventMetaInfo( + Optional.of(randomFrom(filteredUsers)), Optional.of(UNFILTER_MARKER + randomAlphaOfLengthBetween(1, 8)), - Optional.of(authzInfo(randomSubsetOf(randomIntBetween(1, filteredRoles.size()), filteredRoles) - .toArray(new String[0]))), + Optional.of( + authzInfo(randomSubsetOf(randomIntBetween(1, filteredRoles.size()), filteredRoles).toArray(new String[0])) + ), Optional.of(randomSubsetOf(randomIntBetween(1, filteredIndices.size()), filteredIndices).toArray(new String[0])), - Optional.of(randomFrom(filteredActions))))); - assertTrue("Matches the filter predicate because of the empty realm.", - auditTrail.eventFilterPolicyRegistry.ignorePredicate().test(new AuditEventMetaInfo(Optional.of(randomFrom(filteredUsers)), + Optional.of(randomFrom(filteredActions)) + ) + ) + ); + assertTrue( + "Matches the filter predicate because of the empty realm.", + auditTrail.eventFilterPolicyRegistry.ignorePredicate() + .test( + new AuditEventMetaInfo( + Optional.of(randomFrom(filteredUsers)), Optional.empty(), - Optional.of(authzInfo(randomSubsetOf(randomIntBetween(1, filteredRoles.size()), filteredRoles) - .toArray(new String[0]))), + Optional.of( + authzInfo(randomSubsetOf(randomIntBetween(1, filteredRoles.size()), filteredRoles).toArray(new String[0])) + ), Optional.of(randomSubsetOf(randomIntBetween(1, filteredIndices.size()), filteredIndices).toArray(new String[0])), - Optional.of(randomFrom(filteredActions))))); - assertFalse("Does not match the filter predicate because of the pivilege.", - auditTrail.eventFilterPolicyRegistry.ignorePredicate().test(new AuditEventMetaInfo(Optional.of(randomFrom(filteredUsers)), - Optional.of(randomFrom(filteredRealms)), - Optional.of(authzInfo(randomSubsetOf(randomIntBetween(1, filteredRoles.size()), filteredRoles) - .toArray(new String[0]))), - Optional.of(randomSubsetOf(randomIntBetween(1, filteredIndices.size()), filteredIndices).toArray(new String[0])), - Optional.of(UNFILTER_MARKER + randomAlphaOfLengthBetween(1, 8))))); + Optional.of(randomFrom(filteredActions)) + ) + ) + ); + assertFalse( + "Does not match the filter predicate because of the pivilege.", + auditTrail.eventFilterPolicyRegistry.ignorePredicate() + .test( + new AuditEventMetaInfo( + Optional.of(randomFrom(filteredUsers)), + Optional.of(randomFrom(filteredRealms)), + Optional.of( + authzInfo(randomSubsetOf(randomIntBetween(1, filteredRoles.size()), filteredRoles).toArray(new String[0])) + ), + Optional.of(randomSubsetOf(randomIntBetween(1, filteredIndices.size()), filteredIndices).toArray(new String[0])), + Optional.of(UNFILTER_MARKER + randomAlphaOfLengthBetween(1, 8)) + ) + ) + ); final List someRolesDoNotMatch = new ArrayList<>(randomSubsetOf(randomIntBetween(0, filteredRoles.size()), filteredRoles)); for (int i = 0; i < randomIntBetween(1, 8); i++) { someRolesDoNotMatch.add(UNFILTER_MARKER + randomAlphaOfLengthBetween(1, 8)); } - assertFalse("Does not match the filter predicate because of some of the roles.", - auditTrail.eventFilterPolicyRegistry.ignorePredicate().test(new AuditEventMetaInfo(Optional.of(randomFrom(filteredUsers)), - Optional.of(randomFrom(filteredRealms)), Optional.of(authzInfo(someRolesDoNotMatch.toArray(new String[0]))), + assertFalse( + "Does not match the filter predicate because of some of the roles.", + auditTrail.eventFilterPolicyRegistry.ignorePredicate() + .test( + new AuditEventMetaInfo( + Optional.of(randomFrom(filteredUsers)), + Optional.of(randomFrom(filteredRealms)), + Optional.of(authzInfo(someRolesDoNotMatch.toArray(new String[0]))), Optional.of(randomSubsetOf(randomIntBetween(1, filteredIndices.size()), filteredIndices).toArray(new String[0])), - Optional.of(randomFrom(filteredActions))))); + Optional.of(randomFrom(filteredActions)) + ) + ) + ); final Optional emptyRoles = randomBoolean() ? Optional.empty() : Optional.of(authzInfo(new String[0])); - assertTrue("Matches the filter predicate because of the empty roles.", - auditTrail.eventFilterPolicyRegistry.ignorePredicate().test(new AuditEventMetaInfo(Optional.of(randomFrom(filteredUsers)), - Optional.of(randomFrom(filteredRealms)), emptyRoles, + assertTrue( + "Matches the filter predicate because of the empty roles.", + auditTrail.eventFilterPolicyRegistry.ignorePredicate() + .test( + new AuditEventMetaInfo( + Optional.of(randomFrom(filteredUsers)), + Optional.of(randomFrom(filteredRealms)), + emptyRoles, Optional.of(randomSubsetOf(randomIntBetween(1, filteredIndices.size()), filteredIndices).toArray(new String[0])), - Optional.of(randomFrom(filteredActions))))); + Optional.of(randomFrom(filteredActions)) + ) + ) + ); final List someIndicesDoNotMatch = new ArrayList<>( - randomSubsetOf(randomIntBetween(0, filteredIndices.size()), filteredIndices)); + randomSubsetOf(randomIntBetween(0, filteredIndices.size()), filteredIndices) + ); for (int i = 0; i < randomIntBetween(1, 8); i++) { someIndicesDoNotMatch.add(UNFILTER_MARKER + randomAlphaOfLengthBetween(1, 8)); } - assertFalse("Does not match the filter predicate because of some of the indices.", - auditTrail.eventFilterPolicyRegistry.ignorePredicate() - .test(new AuditEventMetaInfo(Optional.of(randomFrom(filteredUsers)), Optional.of(randomFrom(filteredRealms)), - Optional.of(authzInfo(randomSubsetOf(randomIntBetween(1, filteredRoles.size()), filteredRoles) - .toArray(new String[0]))), + assertFalse( + "Does not match the filter predicate because of some of the indices.", + auditTrail.eventFilterPolicyRegistry.ignorePredicate() + .test( + new AuditEventMetaInfo( + Optional.of(randomFrom(filteredUsers)), + Optional.of(randomFrom(filteredRealms)), + Optional.of( + authzInfo(randomSubsetOf(randomIntBetween(1, filteredRoles.size()), filteredRoles).toArray(new String[0])) + ), Optional.of(someIndicesDoNotMatch.toArray(new String[0])), - Optional.of(randomFrom(filteredActions))))); - assertTrue("Matches the filter predicate because of the empty indices.", auditTrail.eventFilterPolicyRegistry.ignorePredicate() - .test(new AuditEventMetaInfo(Optional.of(randomFrom(filteredUsers)), Optional.of(randomFrom(filteredRealms)), - Optional.of(authzInfo( - randomSubsetOf(randomIntBetween(1, filteredRoles.size()), filteredRoles).toArray(new String[0]))), - Optional.empty(), Optional.of(randomFrom(filteredActions))))); - assertTrue("Matches the filter predicate because of the empty indices.", auditTrail.eventFilterPolicyRegistry.ignorePredicate() - .test(new AuditEventMetaInfo(Optional.of(randomFrom(filteredUsers)), Optional.of(randomFrom(filteredRealms)), - Optional.of(authzInfo( - randomSubsetOf(randomIntBetween(1, filteredRoles.size()), filteredRoles).toArray(new String[0]))), - Optional.of(new String[0]), Optional.of(randomFrom(filteredActions))))); - assertTrue("Matches the filter predicate because of the empty indices.", auditTrail.eventFilterPolicyRegistry.ignorePredicate() - .test(new AuditEventMetaInfo(Optional.of(randomFrom(filteredUsers)), Optional.of(randomFrom(filteredRealms)), - Optional.of(authzInfo( - randomSubsetOf(randomIntBetween(1, filteredRoles.size()), filteredRoles).toArray(new String[0]))), + Optional.of(randomFrom(filteredActions)) + ) + ) + ); + assertTrue( + "Matches the filter predicate because of the empty indices.", + auditTrail.eventFilterPolicyRegistry.ignorePredicate() + .test( + new AuditEventMetaInfo( + Optional.of(randomFrom(filteredUsers)), + Optional.of(randomFrom(filteredRealms)), + Optional.of( + authzInfo(randomSubsetOf(randomIntBetween(1, filteredRoles.size()), filteredRoles).toArray(new String[0])) + ), + Optional.empty(), + Optional.of(randomFrom(filteredActions)) + ) + ) + ); + assertTrue( + "Matches the filter predicate because of the empty indices.", + auditTrail.eventFilterPolicyRegistry.ignorePredicate() + .test( + new AuditEventMetaInfo( + Optional.of(randomFrom(filteredUsers)), + Optional.of(randomFrom(filteredRealms)), + Optional.of( + authzInfo(randomSubsetOf(randomIntBetween(1, filteredRoles.size()), filteredRoles).toArray(new String[0])) + ), + Optional.of(new String[0]), + Optional.of(randomFrom(filteredActions)) + ) + ) + ); + assertTrue( + "Matches the filter predicate because of the empty indices.", + auditTrail.eventFilterPolicyRegistry.ignorePredicate() + .test( + new AuditEventMetaInfo( + Optional.of(randomFrom(filteredUsers)), + Optional.of(randomFrom(filteredRealms)), + Optional.of( + authzInfo(randomSubsetOf(randomIntBetween(1, filteredRoles.size()), filteredRoles).toArray(new String[0])) + ), Optional.of(new String[] { null }), - Optional.of(randomFrom(filteredActions))))); + Optional.of(randomFrom(filteredActions)) + ) + ) + ); } public void testTwoPolicyPredicatesWithMissingFields() throws Exception { @@ -467,47 +756,81 @@ public void testTwoPolicyPredicatesWithMissingFields() throws Exception { if (randomBoolean()) { unfilteredUser = new User(UNFILTER_MARKER + randomAlphaOfLengthBetween(1, 8)); } else { - unfilteredUser = new User(new User(UNFILTER_MARKER + randomAlphaOfLengthBetween(1, 8)), - new User(randomFrom(filteredUsers).principal())); + unfilteredUser = new User( + new User(UNFILTER_MARKER + randomAlphaOfLengthBetween(1, 8)), + new User(randomFrom(filteredUsers).principal()) + ); } final List someRolesDoNotMatch = new ArrayList<>(randomSubsetOf(randomIntBetween(0, filteredRoles.size()), filteredRoles)); for (int i = 0; i < randomIntBetween(1, 8); i++) { someRolesDoNotMatch.add(UNFILTER_MARKER + randomAlphaOfLengthBetween(1, 8)); } final List someIndicesDoNotMatch = new ArrayList<>( - randomSubsetOf(randomIntBetween(0, filteredIndices.size()), filteredIndices)); + randomSubsetOf(randomIntBetween(0, filteredIndices.size()), filteredIndices) + ); for (int i = 0; i < randomIntBetween(1, 8); i++) { someIndicesDoNotMatch.add(UNFILTER_MARKER + randomAlphaOfLengthBetween(1, 8)); } // matches both the first and the second policies - assertTrue("Matches both the first and the second filter predicates.", - auditTrail.eventFilterPolicyRegistry.ignorePredicate().test(new AuditEventMetaInfo(Optional.of(randomFrom(filteredUsers)), + assertTrue( + "Matches both the first and the second filter predicates.", + auditTrail.eventFilterPolicyRegistry.ignorePredicate() + .test( + new AuditEventMetaInfo( + Optional.of(randomFrom(filteredUsers)), Optional.of(randomFrom(filteredRealms)), - Optional.of(authzInfo(randomSubsetOf(randomIntBetween(1, filteredRoles.size()), filteredRoles) - .toArray(new String[0]))), + Optional.of( + authzInfo(randomSubsetOf(randomIntBetween(1, filteredRoles.size()), filteredRoles).toArray(new String[0])) + ), Optional.of(randomSubsetOf(randomIntBetween(1, filteredIndices.size()), filteredIndices).toArray(new String[0])), - Optional.empty()))); + Optional.empty() + ) + ) + ); // matches first policy but not the second - assertTrue("Matches the first filter predicate but not the second.", - auditTrail.eventFilterPolicyRegistry.ignorePredicate().test(new AuditEventMetaInfo(Optional.of(unfilteredUser), + assertTrue( + "Matches the first filter predicate but not the second.", + auditTrail.eventFilterPolicyRegistry.ignorePredicate() + .test( + new AuditEventMetaInfo( + Optional.of(unfilteredUser), Optional.of(randomFrom(filteredRealms)), - Optional.of(authzInfo(randomSubsetOf(randomIntBetween(1, filteredRoles.size()), filteredRoles) - .toArray(new String[0]))), - Optional.of(someIndicesDoNotMatch.toArray(new String[0])), Optional.of("_action")))); + Optional.of( + authzInfo(randomSubsetOf(randomIntBetween(1, filteredRoles.size()), filteredRoles).toArray(new String[0])) + ), + Optional.of(someIndicesDoNotMatch.toArray(new String[0])), + Optional.of("_action") + ) + ) + ); // matches the second policy but not the first - assertTrue("Matches the second filter predicate but not the first.", - auditTrail.eventFilterPolicyRegistry.ignorePredicate().test(new AuditEventMetaInfo(Optional.of(randomFrom(filteredUsers)), + assertTrue( + "Matches the second filter predicate but not the first.", + auditTrail.eventFilterPolicyRegistry.ignorePredicate() + .test( + new AuditEventMetaInfo( + Optional.of(randomFrom(filteredUsers)), Optional.of(UNFILTER_MARKER + randomAlphaOfLengthBetween(1, 8)), Optional.of(authzInfo(someRolesDoNotMatch.toArray(new String[0]))), Optional.of(randomSubsetOf(randomIntBetween(1, filteredIndices.size()), filteredIndices).toArray(new String[0])), - Optional.empty()))); + Optional.empty() + ) + ) + ); // matches neither the first nor the second policies - assertFalse("Matches neither the first nor the second filter predicates.", - auditTrail.eventFilterPolicyRegistry.ignorePredicate() - .test(new AuditEventMetaInfo(Optional.of(unfilteredUser), + assertFalse( + "Matches neither the first nor the second filter predicates.", + auditTrail.eventFilterPolicyRegistry.ignorePredicate() + .test( + new AuditEventMetaInfo( + Optional.of(unfilteredUser), Optional.of(UNFILTER_MARKER + randomAlphaOfLengthBetween(1, 8)), Optional.of(authzInfo(someRolesDoNotMatch.toArray(new String[0]))), - Optional.of(someIndicesDoNotMatch.toArray(new String[0])), Optional.empty()))); + Optional.of(someIndicesDoNotMatch.toArray(new String[0])), + Optional.empty() + ) + ) + ); } public void testUsersFilter() throws Exception { @@ -532,35 +855,46 @@ public void testUsersFilter() throws Exception { filteredUsers.add(""); settingsBuilder.putList("xpack.security.audit.logfile.events.ignore_filters.missingPolicy.users", filteredUsers); } else { - settingsBuilder.putList("xpack.security.audit.logfile.events.ignore_filters.missingPolicy.users", - Collections.emptyList()); + settingsBuilder.putList("xpack.security.audit.logfile.events.ignore_filters.missingPolicy.users", Collections.emptyList()); } } Authentication filteredAuthentication; if (randomBoolean()) { filteredAuthentication = createAuthentication( - new User(randomFrom(allFilteredUsers), new String[] { "r1" }, new User("authUsername", new String[] { "r2" })), - "effectiveRealmName"); + new User(randomFrom(allFilteredUsers), new String[] { "r1" }, new User("authUsername", new String[] { "r2" })), + "effectiveRealmName" + ); } else { - filteredAuthentication = createAuthentication(new User(randomFrom(allFilteredUsers), new String[] { "r1" }), - "effectiveRealmName"); + filteredAuthentication = createAuthentication( + new User(randomFrom(allFilteredUsers), new String[] { "r1" }), + "effectiveRealmName" + ); } if (randomBoolean()) { filteredAuthentication = createApiKeyAuthentication(apiKeyService, filteredAuthentication); } Authentication unfilteredAuthentication; if (randomBoolean()) { - unfilteredAuthentication = createAuthentication(new User(UNFILTER_MARKER + randomAlphaOfLengthBetween(1, 4), - new String[] { "r1" }, new User("authUsername", new String[] { "r2" })), "effectiveRealmName"); + unfilteredAuthentication = createAuthentication( + new User( + UNFILTER_MARKER + randomAlphaOfLengthBetween(1, 4), + new String[] { "r1" }, + new User("authUsername", new String[] { "r2" }) + ), + "effectiveRealmName" + ); } else { unfilteredAuthentication = createAuthentication( - new User(UNFILTER_MARKER + randomAlphaOfLengthBetween(1, 4), new String[] { "r1" }), "effectiveRealmName"); + new User(UNFILTER_MARKER + randomAlphaOfLengthBetween(1, 4), new String[] { "r1" }), + "effectiveRealmName" + ); } if (randomBoolean()) { unfilteredAuthentication = createApiKeyAuthentication(apiKeyService, unfilteredAuthentication); } - final TransportRequest request = randomBoolean() ? new MockRequest(threadContext) - : new MockIndicesRequest(threadContext, new String[] { "idx1", "idx2" }); + final TransportRequest request = randomBoolean() + ? new MockRequest(threadContext) + : new MockIndicesRequest(threadContext, new String[] { "idx1", "idx2" }); final MockToken filteredToken = new MockToken(randomFrom(allFilteredUsers)); final MockToken unfilteredToken = new MockToken(UNFILTER_MARKER + randomAlphaOfLengthBetween(1, 4)); @@ -655,51 +989,79 @@ public void testUsersFilter() throws Exception { logOutput.clear(); threadContext.stashContext(); - auditTrail.accessGranted(randomAlphaOfLength(8), createAuthentication(SystemUser.INSTANCE, "effectiveRealmName"), - "internal:_action", request, authzInfo(new String[] { "role1" })); + auditTrail.accessGranted( + randomAlphaOfLength(8), + createAuthentication(SystemUser.INSTANCE, "effectiveRealmName"), + "internal:_action", + request, + authzInfo(new String[] { "role1" }) + ); assertThat("AccessGranted internal message: system user is filtered out", logOutput.size(), is(1)); logOutput.clear(); threadContext.stashContext(); - auditTrail.accessGranted(randomAlphaOfLength(8), unfilteredAuthentication, "internal:_action", request, - authzInfo(new String[] { "role1" })); + auditTrail.accessGranted( + randomAlphaOfLength(8), + unfilteredAuthentication, + "internal:_action", + request, + authzInfo(new String[] { "role1" }) + ); assertThat("AccessGranted internal message: unfiltered user is filtered out", logOutput.size(), is(1)); logOutput.clear(); threadContext.stashContext(); - auditTrail.accessGranted(randomAlphaOfLength(8), filteredAuthentication, "internal:_action", request, - authzInfo(new String[] { "role1" })); + auditTrail.accessGranted( + randomAlphaOfLength(8), + filteredAuthentication, + "internal:_action", + request, + authzInfo(new String[] { "role1" }) + ); assertThat("AccessGranted internal message: filtered user is not filtered out", logOutput.size(), is(0)); logOutput.clear(); threadContext.stashContext(); // accessDenied - auditTrail.accessDenied(randomAlphaOfLength(8), unfilteredAuthentication, "_action", request, - authzInfo(new String[] { "role1" })); + auditTrail.accessDenied(randomAlphaOfLength(8), unfilteredAuthentication, "_action", request, authzInfo(new String[] { "role1" })); assertThat("AccessDenied message: unfiltered user is filtered out", logOutput.size(), is(1)); logOutput.clear(); threadContext.stashContext(); - auditTrail.accessDenied(randomAlphaOfLength(8), filteredAuthentication, "_action", request, - authzInfo(new String[] { "role1" })); + auditTrail.accessDenied(randomAlphaOfLength(8), filteredAuthentication, "_action", request, authzInfo(new String[] { "role1" })); assertThat("AccessDenied message: filtered user is not filtered out", logOutput.size(), is(0)); logOutput.clear(); threadContext.stashContext(); - auditTrail.accessDenied(randomAlphaOfLength(8), createAuthentication(SystemUser.INSTANCE, "effectiveRealmName"), "internal:_action", - request, authzInfo(new String[] { "role1" })); + auditTrail.accessDenied( + randomAlphaOfLength(8), + createAuthentication(SystemUser.INSTANCE, "effectiveRealmName"), + "internal:_action", + request, + authzInfo(new String[] { "role1" }) + ); assertThat("AccessDenied internal message: system user is filtered out", logOutput.size(), is(1)); logOutput.clear(); threadContext.stashContext(); - auditTrail.accessDenied(randomAlphaOfLength(8), unfilteredAuthentication, "internal:_action", request, - authzInfo(new String[] { "role1" })); + auditTrail.accessDenied( + randomAlphaOfLength(8), + unfilteredAuthentication, + "internal:_action", + request, + authzInfo(new String[] { "role1" }) + ); assertThat("AccessDenied internal message: unfiltered user is filtered out", logOutput.size(), is(1)); logOutput.clear(); threadContext.stashContext(); - auditTrail.accessDenied(randomAlphaOfLength(8), filteredAuthentication, "internal:_action", request, - authzInfo(new String[] { "role1" })); + auditTrail.accessDenied( + randomAlphaOfLength(8), + filteredAuthentication, + "internal:_action", + request, + authzInfo(new String[] { "role1" }) + ); assertThat("AccessDenied internal request: filtered user is not filtered out", logOutput.size(), is(0)); logOutput.clear(); threadContext.stashContext(); @@ -754,27 +1116,47 @@ public void testUsersFilter() throws Exception { threadContext.stashContext(); // runAsGranted - auditTrail.runAsGranted(randomAlphaOfLength(8), unfilteredAuthentication, "_action", new MockRequest(threadContext), - authzInfo(new String[] { "role1" })); + auditTrail.runAsGranted( + randomAlphaOfLength(8), + unfilteredAuthentication, + "_action", + new MockRequest(threadContext), + authzInfo(new String[] { "role1" }) + ); assertThat("RunAsGranted message: unfiltered user is filtered out", logOutput.size(), is(1)); logOutput.clear(); threadContext.stashContext(); - auditTrail.runAsGranted(randomAlphaOfLength(8), filteredAuthentication, "_action", new MockRequest(threadContext), - authzInfo(new String[] { "role1" })); + auditTrail.runAsGranted( + randomAlphaOfLength(8), + filteredAuthentication, + "_action", + new MockRequest(threadContext), + authzInfo(new String[] { "role1" }) + ); assertThat("RunAsGranted message: filtered user is not filtered out", logOutput.size(), is(0)); logOutput.clear(); threadContext.stashContext(); // runAsDenied - auditTrail.runAsDenied(randomAlphaOfLength(8), unfilteredAuthentication, "_action", new MockRequest(threadContext), - authzInfo(new String[] { "role1" })); + auditTrail.runAsDenied( + randomAlphaOfLength(8), + unfilteredAuthentication, + "_action", + new MockRequest(threadContext), + authzInfo(new String[] { "role1" }) + ); assertThat("RunAsDenied message: unfiltered user is filtered out", logOutput.size(), is(1)); logOutput.clear(); threadContext.stashContext(); - auditTrail.runAsDenied(randomAlphaOfLength(8), filteredAuthentication, "_action", new MockRequest(threadContext), - authzInfo(new String[] { "role1" })); + auditTrail.runAsDenied( + randomAlphaOfLength(8), + filteredAuthentication, + "_action", + new MockRequest(threadContext), + authzInfo(new String[] { "role1" }) + ); assertThat("RunAsDenied message: filtered user is not filtered out", logOutput.size(), is(0)); logOutput.clear(); threadContext.stashContext(); @@ -833,8 +1215,7 @@ public void testRealmsFilter() throws Exception { filteredRealms.add(""); settingsBuilder.putList("xpack.security.audit.logfile.events.ignore_filters.missingPolicy.realms", filteredRealms); } else { - settingsBuilder.putList("xpack.security.audit.logfile.events.ignore_filters.missingPolicy.realms", - Collections.emptyList()); + settingsBuilder.putList("xpack.security.audit.logfile.events.ignore_filters.missingPolicy.realms", Collections.emptyList()); } } final String filteredRealm = randomFrom(allFilteredRealms); @@ -845,8 +1226,9 @@ public void testRealmsFilter() throws Exception { } else { user = new User("user1", new String[] { "r1" }); } - final TransportRequest request = randomBoolean() ? new MockRequest(threadContext) - : new MockIndicesRequest(threadContext, new String[] { "idx1", "idx2" }); + final TransportRequest request = randomBoolean() + ? new MockRequest(threadContext) + : new MockIndicesRequest(threadContext, new String[] { "idx1", "idx2" }); final MockToken authToken = new MockToken("token1"); final LoggingAuditTrail auditTrail = new LoggingAuditTrail(settingsBuilder.build(), clusterService, logger, threadContext); @@ -928,11 +1310,12 @@ public void testRealmsFilter() throws Exception { threadContext.stashContext(); // accessGranted - Authentication authentication = randomBoolean() ? createAuthentication(user, filteredRealm) : - createApiKeyAuthentication(apiKeyService, createAuthentication(user, filteredRealm)); - auditTrail.accessGranted(randomAlphaOfLength(8), authentication, "_action", request, authzInfo(new String[]{"role1"})); - if (authentication.getAuthenticationType() == Authentication.AuthenticationType.API_KEY && - false == authentication.getMetadata().containsKey(ApiKeyService.API_KEY_CREATOR_REALM_NAME)) { + Authentication authentication = randomBoolean() + ? createAuthentication(user, filteredRealm) + : createApiKeyAuthentication(apiKeyService, createAuthentication(user, filteredRealm)); + auditTrail.accessGranted(randomAlphaOfLength(8), authentication, "_action", request, authzInfo(new String[] { "role1" })); + if (authentication.getAuthenticationType() == Authentication.AuthenticationType.API_KEY + && false == authentication.getMetadata().containsKey(ApiKeyService.API_KEY_CREATOR_REALM_NAME)) { if (filterMissingRealm) { assertThat("AccessGranted message: not filtered out by the missing realm filter", logOutput.size(), is(0)); } else { @@ -944,11 +1327,12 @@ public void testRealmsFilter() throws Exception { logOutput.clear(); threadContext.stashContext(); - authentication = randomBoolean() ? createAuthentication(user, unfilteredRealm) : - createApiKeyAuthentication(apiKeyService, createAuthentication(user, unfilteredRealm)); - auditTrail.accessGranted(randomAlphaOfLength(8), authentication, "_action", request, authzInfo(new String[]{"role1"})); - if (authentication.getAuthenticationType() == Authentication.AuthenticationType.API_KEY && - false == authentication.getMetadata().containsKey(ApiKeyService.API_KEY_CREATOR_REALM_NAME)) { + authentication = randomBoolean() + ? createAuthentication(user, unfilteredRealm) + : createApiKeyAuthentication(apiKeyService, createAuthentication(user, unfilteredRealm)); + auditTrail.accessGranted(randomAlphaOfLength(8), authentication, "_action", request, authzInfo(new String[] { "role1" })); + if (authentication.getAuthenticationType() == Authentication.AuthenticationType.API_KEY + && false == authentication.getMetadata().containsKey(ApiKeyService.API_KEY_CREATOR_REALM_NAME)) { if (filterMissingRealm) { assertThat("AccessGranted message: not filtered out by the missing realm filter", logOutput.size(), is(0)); } else { @@ -960,23 +1344,34 @@ public void testRealmsFilter() throws Exception { logOutput.clear(); threadContext.stashContext(); - auditTrail.accessGranted(randomAlphaOfLength(8), createAuthentication(SystemUser.INSTANCE, filteredRealm), "internal:_action", - request, authzInfo(new String[] { "role1" })); + auditTrail.accessGranted( + randomAlphaOfLength(8), + createAuthentication(SystemUser.INSTANCE, filteredRealm), + "internal:_action", + request, + authzInfo(new String[] { "role1" }) + ); assertThat("AccessGranted internal message system user: filtered realm is not filtered out", logOutput.size(), is(0)); logOutput.clear(); threadContext.stashContext(); - auditTrail.accessGranted(randomAlphaOfLength(8), createAuthentication(SystemUser.INSTANCE, unfilteredRealm), "internal:_action", - request, authzInfo(new String[] { "role1" })); + auditTrail.accessGranted( + randomAlphaOfLength(8), + createAuthentication(SystemUser.INSTANCE, unfilteredRealm), + "internal:_action", + request, + authzInfo(new String[] { "role1" }) + ); assertThat("AccessGranted internal message system user: unfiltered realm is filtered out", logOutput.size(), is(1)); logOutput.clear(); threadContext.stashContext(); - authentication = randomBoolean() ? createAuthentication(user, filteredRealm) : - createApiKeyAuthentication(apiKeyService, createAuthentication(user, filteredRealm)); - auditTrail.accessGranted(randomAlphaOfLength(8), authentication, "internal:_action", request, authzInfo(new String[]{"role1"})); - if (authentication.getAuthenticationType() == Authentication.AuthenticationType.API_KEY && - false == authentication.getMetadata().containsKey(ApiKeyService.API_KEY_CREATOR_REALM_NAME)) { + authentication = randomBoolean() + ? createAuthentication(user, filteredRealm) + : createApiKeyAuthentication(apiKeyService, createAuthentication(user, filteredRealm)); + auditTrail.accessGranted(randomAlphaOfLength(8), authentication, "internal:_action", request, authzInfo(new String[] { "role1" })); + if (authentication.getAuthenticationType() == Authentication.AuthenticationType.API_KEY + && false == authentication.getMetadata().containsKey(ApiKeyService.API_KEY_CREATOR_REALM_NAME)) { if (filterMissingRealm) { assertThat("AccessGranted internal message: not filtered out by the missing realm filter", logOutput.size(), is(0)); } else { @@ -988,11 +1383,12 @@ public void testRealmsFilter() throws Exception { logOutput.clear(); threadContext.stashContext(); - authentication = randomBoolean() ? createAuthentication(user, unfilteredRealm) : - createApiKeyAuthentication(apiKeyService, createAuthentication(user, unfilteredRealm)); + authentication = randomBoolean() + ? createAuthentication(user, unfilteredRealm) + : createApiKeyAuthentication(apiKeyService, createAuthentication(user, unfilteredRealm)); auditTrail.accessGranted(randomAlphaOfLength(8), authentication, "internal:_action", request, authzInfo(new String[] { "role1" })); - if (authentication.getAuthenticationType() == Authentication.AuthenticationType.API_KEY && - false == authentication.getMetadata().containsKey(ApiKeyService.API_KEY_CREATOR_REALM_NAME)) { + if (authentication.getAuthenticationType() == Authentication.AuthenticationType.API_KEY + && false == authentication.getMetadata().containsKey(ApiKeyService.API_KEY_CREATOR_REALM_NAME)) { if (filterMissingRealm) { assertThat("AccessGranted internal message: not filtered out by the missing realm filter", logOutput.size(), is(0)); } else { @@ -1005,11 +1401,12 @@ public void testRealmsFilter() throws Exception { threadContext.stashContext(); // accessDenied - authentication = randomBoolean() ? createAuthentication(user, filteredRealm) : - createApiKeyAuthentication(apiKeyService, createAuthentication(user, filteredRealm)); - auditTrail.accessDenied(randomAlphaOfLength(8), authentication, "_action", request, authzInfo(new String[]{"role1"})); - if (authentication.getAuthenticationType() == Authentication.AuthenticationType.API_KEY && - false == authentication.getMetadata().containsKey(ApiKeyService.API_KEY_CREATOR_REALM_NAME)) { + authentication = randomBoolean() + ? createAuthentication(user, filteredRealm) + : createApiKeyAuthentication(apiKeyService, createAuthentication(user, filteredRealm)); + auditTrail.accessDenied(randomAlphaOfLength(8), authentication, "_action", request, authzInfo(new String[] { "role1" })); + if (authentication.getAuthenticationType() == Authentication.AuthenticationType.API_KEY + && false == authentication.getMetadata().containsKey(ApiKeyService.API_KEY_CREATOR_REALM_NAME)) { if (filterMissingRealm) { assertThat("AccessDenied message: not filtered out by the missing realm filter", logOutput.size(), is(0)); } else { @@ -1021,11 +1418,12 @@ public void testRealmsFilter() throws Exception { logOutput.clear(); threadContext.stashContext(); - authentication = randomBoolean() ? createAuthentication(user, unfilteredRealm) : - createApiKeyAuthentication(apiKeyService, createAuthentication(user, unfilteredRealm)); - auditTrail.accessDenied(randomAlphaOfLength(8), authentication, "_action", request, authzInfo(new String[]{"role1"})); - if (authentication.getAuthenticationType() == Authentication.AuthenticationType.API_KEY && - false == authentication.getMetadata().containsKey(ApiKeyService.API_KEY_CREATOR_REALM_NAME)) { + authentication = randomBoolean() + ? createAuthentication(user, unfilteredRealm) + : createApiKeyAuthentication(apiKeyService, createAuthentication(user, unfilteredRealm)); + auditTrail.accessDenied(randomAlphaOfLength(8), authentication, "_action", request, authzInfo(new String[] { "role1" })); + if (authentication.getAuthenticationType() == Authentication.AuthenticationType.API_KEY + && false == authentication.getMetadata().containsKey(ApiKeyService.API_KEY_CREATOR_REALM_NAME)) { if (filterMissingRealm) { assertThat("AccessDenied message: not filtered out by the missing realm filter", logOutput.size(), is(0)); } else { @@ -1037,23 +1435,34 @@ public void testRealmsFilter() throws Exception { logOutput.clear(); threadContext.stashContext(); - auditTrail.accessDenied(randomAlphaOfLength(8), createAuthentication(SystemUser.INSTANCE, filteredRealm), "internal:_action", - request, authzInfo(new String[] { "role1" })); + auditTrail.accessDenied( + randomAlphaOfLength(8), + createAuthentication(SystemUser.INSTANCE, filteredRealm), + "internal:_action", + request, + authzInfo(new String[] { "role1" }) + ); assertThat("AccessDenied internal message system user: filtered realm is not filtered out", logOutput.size(), is(0)); logOutput.clear(); threadContext.stashContext(); - auditTrail.accessDenied(randomAlphaOfLength(8), createAuthentication(SystemUser.INSTANCE, unfilteredRealm), "internal:_action", - request, authzInfo(new String[] { "role1" })); + auditTrail.accessDenied( + randomAlphaOfLength(8), + createAuthentication(SystemUser.INSTANCE, unfilteredRealm), + "internal:_action", + request, + authzInfo(new String[] { "role1" }) + ); assertThat("AccessDenied internal message system user: unfiltered realm is filtered out", logOutput.size(), is(1)); logOutput.clear(); threadContext.stashContext(); - authentication = randomBoolean() ? createAuthentication(user, filteredRealm) : - createApiKeyAuthentication(apiKeyService, createAuthentication(user, filteredRealm)); - auditTrail.accessDenied(randomAlphaOfLength(8), authentication, "internal:_action", request, authzInfo(new String[]{"role1"})); - if (authentication.getAuthenticationType() == Authentication.AuthenticationType.API_KEY && - false == authentication.getMetadata().containsKey(ApiKeyService.API_KEY_CREATOR_REALM_NAME)) { + authentication = randomBoolean() + ? createAuthentication(user, filteredRealm) + : createApiKeyAuthentication(apiKeyService, createAuthentication(user, filteredRealm)); + auditTrail.accessDenied(randomAlphaOfLength(8), authentication, "internal:_action", request, authzInfo(new String[] { "role1" })); + if (authentication.getAuthenticationType() == Authentication.AuthenticationType.API_KEY + && false == authentication.getMetadata().containsKey(ApiKeyService.API_KEY_CREATOR_REALM_NAME)) { if (filterMissingRealm) { assertThat("AccessDenied internal message: not filtered out by the missing realm filter", logOutput.size(), is(0)); } else { @@ -1065,12 +1474,12 @@ public void testRealmsFilter() throws Exception { logOutput.clear(); threadContext.stashContext(); - authentication = randomBoolean() ? createAuthentication(user, unfilteredRealm) : - createApiKeyAuthentication(apiKeyService, createAuthentication(user, unfilteredRealm)); - auditTrail.accessDenied(randomAlphaOfLength(8), authentication, "internal:_action", - request, authzInfo(new String[]{"role1"})); - if (authentication.getAuthenticationType() == Authentication.AuthenticationType.API_KEY && - false == authentication.getMetadata().containsKey(ApiKeyService.API_KEY_CREATOR_REALM_NAME)) { + authentication = randomBoolean() + ? createAuthentication(user, unfilteredRealm) + : createApiKeyAuthentication(apiKeyService, createAuthentication(user, unfilteredRealm)); + auditTrail.accessDenied(randomAlphaOfLength(8), authentication, "internal:_action", request, authzInfo(new String[] { "role1" })); + if (authentication.getAuthenticationType() == Authentication.AuthenticationType.API_KEY + && false == authentication.getMetadata().containsKey(ApiKeyService.API_KEY_CREATOR_REALM_NAME)) { if (filterMissingRealm) { assertThat("AccessDenied internal message: not filtered out by the missing realm filter", logOutput.size(), is(0)); } else { @@ -1101,11 +1510,12 @@ public void testRealmsFilter() throws Exception { logOutput.clear(); threadContext.stashContext(); - authentication = randomBoolean() ? createAuthentication(user, filteredRealm) : - createApiKeyAuthentication(apiKeyService, createAuthentication(user, filteredRealm)); + authentication = randomBoolean() + ? createAuthentication(user, filteredRealm) + : createApiKeyAuthentication(apiKeyService, createAuthentication(user, filteredRealm)); auditTrail.tamperedRequest(randomAlphaOfLength(8), authentication, "_action", request); - if (authentication.getAuthenticationType() == Authentication.AuthenticationType.API_KEY && - false == authentication.getMetadata().containsKey(ApiKeyService.API_KEY_CREATOR_REALM_NAME)) { + if (authentication.getAuthenticationType() == Authentication.AuthenticationType.API_KEY + && false == authentication.getMetadata().containsKey(ApiKeyService.API_KEY_CREATOR_REALM_NAME)) { if (filterMissingRealm) { assertThat("Tampered message: not filtered out by the missing realm filter", logOutput.size(), is(0)); } else { @@ -1117,11 +1527,12 @@ public void testRealmsFilter() throws Exception { logOutput.clear(); threadContext.stashContext(); - authentication = randomBoolean() ? createAuthentication(user, unfilteredRealm) : - createApiKeyAuthentication(apiKeyService, createAuthentication(user, unfilteredRealm)); + authentication = randomBoolean() + ? createAuthentication(user, unfilteredRealm) + : createApiKeyAuthentication(apiKeyService, createAuthentication(user, unfilteredRealm)); auditTrail.tamperedRequest(randomAlphaOfLength(8), authentication, "_action", request); - if (authentication.getAuthenticationType() == Authentication.AuthenticationType.API_KEY && - false == authentication.getMetadata().containsKey(ApiKeyService.API_KEY_CREATOR_REALM_NAME)) { + if (authentication.getAuthenticationType() == Authentication.AuthenticationType.API_KEY + && false == authentication.getMetadata().containsKey(ApiKeyService.API_KEY_CREATOR_REALM_NAME)) { if (filterMissingRealm) { assertThat("Tampered message: not filtered out by the missing realm filter", logOutput.size(), is(0)); } else { @@ -1154,39 +1565,67 @@ public void testRealmsFilter() throws Exception { threadContext.stashContext(); // runAsGranted - auditTrail.runAsGranted(randomAlphaOfLength(8), createAuthentication(user, filteredRealm), "_action", - new MockRequest(threadContext), authzInfo(new String[] { "role1" })); + auditTrail.runAsGranted( + randomAlphaOfLength(8), + createAuthentication(user, filteredRealm), + "_action", + new MockRequest(threadContext), + authzInfo(new String[] { "role1" }) + ); assertThat("RunAsGranted message: filtered realm is not filtered out", logOutput.size(), is(0)); logOutput.clear(); threadContext.stashContext(); - auditTrail.runAsGranted(randomAlphaOfLength(8), createAuthentication(user, unfilteredRealm), "_action", - new MockRequest(threadContext), authzInfo(new String[] { "role1" })); + auditTrail.runAsGranted( + randomAlphaOfLength(8), + createAuthentication(user, unfilteredRealm), + "_action", + new MockRequest(threadContext), + authzInfo(new String[] { "role1" }) + ); assertThat("RunAsGranted message: unfiltered realm is filtered out", logOutput.size(), is(1)); logOutput.clear(); threadContext.stashContext(); // runAsDenied - auditTrail.runAsDenied(randomAlphaOfLength(8), createAuthentication(user, filteredRealm), "_action", new MockRequest(threadContext), - authzInfo(new String[] { "role1" })); + auditTrail.runAsDenied( + randomAlphaOfLength(8), + createAuthentication(user, filteredRealm), + "_action", + new MockRequest(threadContext), + authzInfo(new String[] { "role1" }) + ); assertThat("RunAsDenied message: filtered realm is not filtered out", logOutput.size(), is(0)); logOutput.clear(); threadContext.stashContext(); - auditTrail.runAsDenied(randomAlphaOfLength(8), createAuthentication(user, unfilteredRealm), "_action", - new MockRequest(threadContext), authzInfo(new String[] { "role1" })); + auditTrail.runAsDenied( + randomAlphaOfLength(8), + createAuthentication(user, unfilteredRealm), + "_action", + new MockRequest(threadContext), + authzInfo(new String[] { "role1" }) + ); assertThat("RunAsDenied message: unfiltered realm is filtered out", logOutput.size(), is(1)); logOutput.clear(); threadContext.stashContext(); - auditTrail.runAsDenied(randomAlphaOfLength(8), createAuthentication(user, filteredRealm), getRestRequest(), - authzInfo(new String[] { "role1" })); + auditTrail.runAsDenied( + randomAlphaOfLength(8), + createAuthentication(user, filteredRealm), + getRestRequest(), + authzInfo(new String[] { "role1" }) + ); assertThat("RunAsDenied rest request: filtered realm is not filtered out", logOutput.size(), is(0)); logOutput.clear(); threadContext.stashContext(); - auditTrail.runAsDenied(randomAlphaOfLength(8), createAuthentication(user, unfilteredRealm), getRestRequest(), - authzInfo(new String[] { "role1" })); + auditTrail.runAsDenied( + randomAlphaOfLength(8), + createAuthentication(user, unfilteredRealm), + getRestRequest(), + authzInfo(new String[] { "role1" }) + ); assertThat("RunAsDenied rest request: unfiltered realm is filtered out", logOutput.size(), is(1)); logOutput.clear(); threadContext.stashContext(); @@ -1237,8 +1676,7 @@ public void testRolesFilter() throws Exception { filteredRoles.add(""); settingsBuilder.putList("xpack.security.audit.logfile.events.ignore_filters.missingPolicy.roles", filteredRoles); } else { - settingsBuilder.putList("xpack.security.audit.logfile.events.ignore_filters.missingPolicy.roles", - Collections.emptyList()); + settingsBuilder.putList("xpack.security.audit.logfile.events.ignore_filters.missingPolicy.roles", Collections.emptyList()); } } // filtered roles are a subset of the roles of any policy @@ -1264,16 +1702,19 @@ public void testRolesFilter() throws Exception { final String[] unfilteredRoles = _unfilteredRoles.toArray(new String[0]); Authentication authentication; if (randomBoolean()) { - authentication = createAuthentication(new User("user1", new String[] { "r1" }, new User("authUsername", new String[] { "r2" })), - "effectiveRealmName"); + authentication = createAuthentication( + new User("user1", new String[] { "r1" }, new User("authUsername", new String[] { "r2" })), + "effectiveRealmName" + ); } else { authentication = createAuthentication(new User("user1", new String[] { "r1" }), "effectiveRealmName"); } if (randomBoolean()) { authentication = createApiKeyAuthentication(apiKeyService, authentication); } - final TransportRequest request = randomBoolean() ? new MockRequest(threadContext) - : new MockIndicesRequest(threadContext, new String[] { "idx1", "idx2" }); + final TransportRequest request = randomBoolean() + ? new MockRequest(threadContext) + : new MockIndicesRequest(threadContext, new String[] { "idx1", "idx2" }); final MockToken authToken = new MockToken("token1"); final LoggingAuditTrail auditTrail = new LoggingAuditTrail(settingsBuilder.build(), clusterService, logger, threadContext); @@ -1363,14 +1804,24 @@ public void testRolesFilter() throws Exception { logOutput.clear(); threadContext.stashContext(); - auditTrail.accessGranted(randomAlphaOfLength(8), createAuthentication(SystemUser.INSTANCE, "effectiveRealmName"), - "internal:_action", request, authzInfo(unfilteredRoles)); + auditTrail.accessGranted( + randomAlphaOfLength(8), + createAuthentication(SystemUser.INSTANCE, "effectiveRealmName"), + "internal:_action", + request, + authzInfo(unfilteredRoles) + ); assertThat("AccessGranted internal message system user: unfiltered roles filtered out", logOutput.size(), is(1)); logOutput.clear(); threadContext.stashContext(); - auditTrail.accessGranted(randomAlphaOfLength(8), createAuthentication(SystemUser.INSTANCE, "effectiveRealmName"), - "internal:_action", request, authzInfo(filteredRoles)); + auditTrail.accessGranted( + randomAlphaOfLength(8), + createAuthentication(SystemUser.INSTANCE, "effectiveRealmName"), + "internal:_action", + request, + authzInfo(filteredRoles) + ); assertThat("AccessGranted internal message system user: filtered roles not filtered out", logOutput.size(), is(0)); logOutput.clear(); threadContext.stashContext(); @@ -1396,14 +1847,24 @@ public void testRolesFilter() throws Exception { logOutput.clear(); threadContext.stashContext(); - auditTrail.accessDenied(randomAlphaOfLength(8), createAuthentication(SystemUser.INSTANCE, "effectiveRealmName"), "internal:_action", - request, authzInfo(unfilteredRoles)); + auditTrail.accessDenied( + randomAlphaOfLength(8), + createAuthentication(SystemUser.INSTANCE, "effectiveRealmName"), + "internal:_action", + request, + authzInfo(unfilteredRoles) + ); assertThat("AccessDenied internal message system user: unfiltered roles filtered out", logOutput.size(), is(1)); logOutput.clear(); threadContext.stashContext(); - auditTrail.accessDenied(randomAlphaOfLength(8), createAuthentication(SystemUser.INSTANCE, "effectiveRealmName"), "internal:_action", - request, authzInfo(filteredRoles)); + auditTrail.accessDenied( + randomAlphaOfLength(8), + createAuthentication(SystemUser.INSTANCE, "effectiveRealmName"), + "internal:_action", + request, + authzInfo(filteredRoles) + ); assertThat("AccessDenied internal message system user: filtered roles not filtered out", logOutput.size(), is(0)); logOutput.clear(); threadContext.stashContext(); @@ -1439,21 +1900,36 @@ public void testRolesFilter() throws Exception { threadContext.stashContext(); // runAsGranted - auditTrail.runAsGranted(randomAlphaOfLength(8), authentication, "_action", new MockRequest(threadContext), - authzInfo(unfilteredRoles)); + auditTrail.runAsGranted( + randomAlphaOfLength(8), + authentication, + "_action", + new MockRequest(threadContext), + authzInfo(unfilteredRoles) + ); assertThat("RunAsGranted message: unfiltered roles filtered out", logOutput.size(), is(1)); logOutput.clear(); threadContext.stashContext(); - auditTrail.runAsGranted(randomAlphaOfLength(8), authentication, "_action", new MockRequest(threadContext), - authzInfo(filteredRoles)); + auditTrail.runAsGranted( + randomAlphaOfLength(8), + authentication, + "_action", + new MockRequest(threadContext), + authzInfo(filteredRoles) + ); assertThat("RunAsGranted message: filtered roles not filtered out", logOutput.size(), is(0)); logOutput.clear(); threadContext.stashContext(); // runAsDenied - auditTrail.runAsDenied(randomAlphaOfLength(8), authentication, "_action", new MockRequest(threadContext), - authzInfo(unfilteredRoles)); + auditTrail.runAsDenied( + randomAlphaOfLength(8), + authentication, + "_action", + new MockRequest(threadContext), + authzInfo(unfilteredRoles) + ); assertThat("RunAsDenied message: unfiltered roles filtered out", logOutput.size(), is(1)); logOutput.clear(); threadContext.stashContext(); @@ -1517,8 +1993,10 @@ public void testIndicesFilter() throws Exception { filteredIndices.add(""); settingsBuilder.putList("xpack.security.audit.logfile.events.ignore_filters.missingPolicy.indices", filteredIndices); } else { - settingsBuilder.putList("xpack.security.audit.logfile.events.ignore_filters.missingPolicy.indices", - Collections.emptyList()); + settingsBuilder.putList( + "xpack.security.audit.logfile.events.ignore_filters.missingPolicy.indices", + Collections.emptyList() + ); } } // filtered indices are a subset of the indices of any policy @@ -1544,8 +2022,10 @@ public void testIndicesFilter() throws Exception { final String[] unfilteredIndices = _unfilteredIndices.toArray(new String[0]); Authentication authentication; if (randomBoolean()) { - authentication = createAuthentication(new User("user1", new String[] { "r1" }, new User("authUsername", new String[] { "r2" })), - "effectiveRealmName"); + authentication = createAuthentication( + new User("user1", new String[] { "r1" }, new User("authUsername", new String[] { "r2" })), + "effectiveRealmName" + ); } else { authentication = createAuthentication(new User("user1", new String[] { "r1" }), "effectiveRealmName"); } @@ -1589,8 +2069,11 @@ public void testIndicesFilter() throws Exception { // authenticationFailed auditTrail.authenticationFailed(randomAlphaOfLength(8), getRestRequest()); if (filterMissingIndices) { - assertThat("AuthenticationFailed no token rest request: not filtered out by the missing indices filter", logOutput.size(), - is(0)); + assertThat( + "AuthenticationFailed no token rest request: not filtered out by the missing indices filter", + logOutput.size(), + is(0) + ); } else { assertThat("AuthenticationFailed no token rest request: filtered out by indices filters", logOutput.size(), is(1)); } @@ -1599,30 +2082,44 @@ public void testIndicesFilter() throws Exception { auditTrail.authenticationFailed(randomAlphaOfLength(8), authToken, "_action", noIndexRequest); if (filterMissingIndices) { - assertThat("AuthenticationFailed token request no index: not filtered out by the missing indices filter", logOutput.size(), - is(0)); + assertThat( + "AuthenticationFailed token request no index: not filtered out by the missing indices filter", + logOutput.size(), + is(0) + ); } else { assertThat("AuthenticationFailed token request no index: filtered out by indices filter", logOutput.size(), is(1)); } logOutput.clear(); threadContext.stashContext(); - auditTrail.authenticationFailed(randomAlphaOfLength(8), authToken, "_action", - new MockIndicesRequest(threadContext, unfilteredIndices)); + auditTrail.authenticationFailed( + randomAlphaOfLength(8), + authToken, + "_action", + new MockIndicesRequest(threadContext, unfilteredIndices) + ); assertThat("AuthenticationFailed token request unfiltered indices: filtered out by indices filter", logOutput.size(), is(1)); logOutput.clear(); threadContext.stashContext(); - auditTrail.authenticationFailed(randomAlphaOfLength(8), authToken, "_action", - new MockIndicesRequest(threadContext, filteredIndices)); + auditTrail.authenticationFailed( + randomAlphaOfLength(8), + authToken, + "_action", + new MockIndicesRequest(threadContext, filteredIndices) + ); assertThat("AuthenticationFailed token request filtered indices: not filtered out by indices filter", logOutput.size(), is(0)); logOutput.clear(); threadContext.stashContext(); auditTrail.authenticationFailed(randomAlphaOfLength(8), "_action", noIndexRequest); if (filterMissingIndices) { - assertThat("AuthenticationFailed no token message no index: not filtered out by the missing indices filter", logOutput.size(), - is(0)); + assertThat( + "AuthenticationFailed no token message no index: not filtered out by the missing indices filter", + logOutput.size(), + is(0) + ); } else { assertThat("AuthenticationFailed no token message: filtered out by indices filter", logOutput.size(), is(1)); } @@ -1650,22 +2147,35 @@ public void testIndicesFilter() throws Exception { auditTrail.authenticationFailed(randomAlphaOfLength(8), "_realm", authToken, "_action", noIndexRequest); if (filterMissingIndices) { - assertThat("AuthenticationFailed realm message no index: not filtered out by the missing indices filter", logOutput.size(), - is(0)); + assertThat( + "AuthenticationFailed realm message no index: not filtered out by the missing indices filter", + logOutput.size(), + is(0) + ); } else { assertThat("AuthenticationFailed realm message no index: filtered out by indices filter", logOutput.size(), is(1)); } logOutput.clear(); threadContext.stashContext(); - auditTrail.authenticationFailed(randomAlphaOfLength(8), "_realm", authToken, "_action", - new MockIndicesRequest(threadContext, unfilteredIndices)); + auditTrail.authenticationFailed( + randomAlphaOfLength(8), + "_realm", + authToken, + "_action", + new MockIndicesRequest(threadContext, unfilteredIndices) + ); assertThat("AuthenticationFailed realm message unfiltered indices: filtered out by indices filter", logOutput.size(), is(1)); logOutput.clear(); threadContext.stashContext(); - auditTrail.authenticationFailed(randomAlphaOfLength(8), "_realm", authToken, "_action", - new MockIndicesRequest(threadContext, filteredIndices)); + auditTrail.authenticationFailed( + randomAlphaOfLength(8), + "_realm", + authToken, + "_action", + new MockIndicesRequest(threadContext, filteredIndices) + ); assertThat("AuthenticationFailed realm message filtered indices: not filtered out by indices filter", logOutput.size(), is(0)); logOutput.clear(); threadContext.stashContext(); @@ -1689,38 +2199,65 @@ public void testIndicesFilter() throws Exception { logOutput.clear(); threadContext.stashContext(); - auditTrail.accessGranted(randomAlphaOfLength(8), authentication, "_action", + auditTrail.accessGranted( + randomAlphaOfLength(8), + authentication, + "_action", new MockIndicesRequest(threadContext, unfilteredIndices), - authzInfo(new String[] { "role1" })); + authzInfo(new String[] { "role1" }) + ); assertThat("AccessGranted message unfiltered indices: filtered out by indices filter", logOutput.size(), is(1)); logOutput.clear(); threadContext.stashContext(); - auditTrail.accessGranted(randomAlphaOfLength(8), authentication, "_action", new MockIndicesRequest(threadContext, filteredIndices), - authzInfo(new String[] { "role1" })); + auditTrail.accessGranted( + randomAlphaOfLength(8), + authentication, + "_action", + new MockIndicesRequest(threadContext, filteredIndices), + authzInfo(new String[] { "role1" }) + ); assertThat("AccessGranted message filtered indices: not filtered out by indices filter", logOutput.size(), is(0)); logOutput.clear(); threadContext.stashContext(); - auditTrail.accessGranted(randomAlphaOfLength(8), createAuthentication(SystemUser.INSTANCE, "effectiveRealmName"), - "internal:_action", noIndexRequest, authzInfo(new String[] { "role1" })); + auditTrail.accessGranted( + randomAlphaOfLength(8), + createAuthentication(SystemUser.INSTANCE, "effectiveRealmName"), + "internal:_action", + noIndexRequest, + authzInfo(new String[] { "role1" }) + ); if (filterMissingIndices) { - assertThat("AccessGranted message system user no index: not filtered out by the missing indices filter", logOutput.size(), - is(0)); + assertThat( + "AccessGranted message system user no index: not filtered out by the missing indices filter", + logOutput.size(), + is(0) + ); } else { assertThat("AccessGranted message system user no index: filtered out by indices filter", logOutput.size(), is(1)); } logOutput.clear(); threadContext.stashContext(); - auditTrail.accessGranted(randomAlphaOfLength(8), createAuthentication(SystemUser.INSTANCE, "effectiveRealmName"), - "internal:_action", new MockIndicesRequest(threadContext, unfilteredIndices), authzInfo(new String[] { "role1" })); + auditTrail.accessGranted( + randomAlphaOfLength(8), + createAuthentication(SystemUser.INSTANCE, "effectiveRealmName"), + "internal:_action", + new MockIndicesRequest(threadContext, unfilteredIndices), + authzInfo(new String[] { "role1" }) + ); assertThat("AccessGranted message system user unfiltered indices: filtered out by indices filter", logOutput.size(), is(1)); logOutput.clear(); threadContext.stashContext(); - auditTrail.accessGranted(randomAlphaOfLength(8), createAuthentication(SystemUser.INSTANCE, "effectiveRealmName"), - "internal:_action", new MockIndicesRequest(threadContext, filteredIndices), authzInfo(new String[] { "role1" })); + auditTrail.accessGranted( + randomAlphaOfLength(8), + createAuthentication(SystemUser.INSTANCE, "effectiveRealmName"), + "internal:_action", + new MockIndicesRequest(threadContext, filteredIndices), + authzInfo(new String[] { "role1" }) + ); assertThat("AccessGranted message system user filtered indices: not filtered out by indices filter", logOutput.size(), is(0)); logOutput.clear(); threadContext.stashContext(); @@ -1735,39 +2272,65 @@ public void testIndicesFilter() throws Exception { logOutput.clear(); threadContext.stashContext(); - auditTrail.accessDenied(randomAlphaOfLength(8), authentication, "_action", new MockIndicesRequest(threadContext, unfilteredIndices), - authzInfo(new String[] { "role1" })); + auditTrail.accessDenied( + randomAlphaOfLength(8), + authentication, + "_action", + new MockIndicesRequest(threadContext, unfilteredIndices), + authzInfo(new String[] { "role1" }) + ); assertThat("AccessDenied message unfiltered indices: filtered out by indices filter", logOutput.size(), is(1)); logOutput.clear(); threadContext.stashContext(); - auditTrail.accessDenied(randomAlphaOfLength(8), authentication, "_action", new MockIndicesRequest(threadContext, filteredIndices), - authzInfo(new String[] { "role1" })); + auditTrail.accessDenied( + randomAlphaOfLength(8), + authentication, + "_action", + new MockIndicesRequest(threadContext, filteredIndices), + authzInfo(new String[] { "role1" }) + ); assertThat("AccessDenied message filtered indices: not filtered out by indices filter", logOutput.size(), is(0)); logOutput.clear(); threadContext.stashContext(); - auditTrail.accessDenied(randomAlphaOfLength(8), createAuthentication(SystemUser.INSTANCE, "effectiveRealmName"), "internal:_action", - noIndexRequest, authzInfo(new String[] { "role1" })); + auditTrail.accessDenied( + randomAlphaOfLength(8), + createAuthentication(SystemUser.INSTANCE, "effectiveRealmName"), + "internal:_action", + noIndexRequest, + authzInfo(new String[] { "role1" }) + ); if (filterMissingIndices) { - assertThat("AccessDenied message system user no index: not filtered out by the missing indices filter", logOutput.size(), - is(0)); + assertThat( + "AccessDenied message system user no index: not filtered out by the missing indices filter", + logOutput.size(), + is(0) + ); } else { assertThat("AccessDenied message system user no index: filtered out by indices filter", logOutput.size(), is(1)); } logOutput.clear(); threadContext.stashContext(); - auditTrail.accessDenied(randomAlphaOfLength(8), createAuthentication(SystemUser.INSTANCE, "effectiveRealmName"), "internal:_action", - new MockIndicesRequest(threadContext, unfilteredIndices), - authzInfo(new String[] { "role1" })); + auditTrail.accessDenied( + randomAlphaOfLength(8), + createAuthentication(SystemUser.INSTANCE, "effectiveRealmName"), + "internal:_action", + new MockIndicesRequest(threadContext, unfilteredIndices), + authzInfo(new String[] { "role1" }) + ); assertThat("AccessDenied message system user unfiltered indices: filtered out by indices filter", logOutput.size(), is(1)); logOutput.clear(); threadContext.stashContext(); - auditTrail.accessDenied(randomAlphaOfLength(8), createAuthentication(SystemUser.INSTANCE, "effectiveRealmName"), "internal:_action", - new MockIndicesRequest(threadContext, filteredIndices), - authzInfo(new String[] { "role1" })); + auditTrail.accessDenied( + randomAlphaOfLength(8), + createAuthentication(SystemUser.INSTANCE, "effectiveRealmName"), + "internal:_action", + new MockIndicesRequest(threadContext, filteredIndices), + authzInfo(new String[] { "role1" }) + ); assertThat("AccessGranted message system user filtered indices: not filtered out by indices filter", logOutput.size(), is(0)); logOutput.clear(); threadContext.stashContext(); @@ -1802,14 +2365,24 @@ public void testIndicesFilter() throws Exception { logOutput.clear(); threadContext.stashContext(); - auditTrail.runAsGranted(randomAlphaOfLength(8), authentication, "_action", new MockIndicesRequest(threadContext, unfilteredIndices), - authzInfo(new String[] { "role1" })); + auditTrail.runAsGranted( + randomAlphaOfLength(8), + authentication, + "_action", + new MockIndicesRequest(threadContext, unfilteredIndices), + authzInfo(new String[] { "role1" }) + ); assertThat("RunAsGranted message unfiltered indices: filtered out by indices filter", logOutput.size(), is(1)); logOutput.clear(); threadContext.stashContext(); - auditTrail.runAsGranted(randomAlphaOfLength(8), authentication, "_action", new MockIndicesRequest(threadContext, filteredIndices), - authzInfo(new String[] { "role1" })); + auditTrail.runAsGranted( + randomAlphaOfLength(8), + authentication, + "_action", + new MockIndicesRequest(threadContext, filteredIndices), + authzInfo(new String[] { "role1" }) + ); assertThat("RunAsGranted message filtered indices: not filtered out by indices filter", logOutput.size(), is(0)); logOutput.clear(); threadContext.stashContext(); @@ -1824,14 +2397,24 @@ public void testIndicesFilter() throws Exception { logOutput.clear(); threadContext.stashContext(); - auditTrail.runAsDenied(randomAlphaOfLength(8), authentication, "_action", new MockIndicesRequest(threadContext, unfilteredIndices), - authzInfo(new String[] { "role1" })); + auditTrail.runAsDenied( + randomAlphaOfLength(8), + authentication, + "_action", + new MockIndicesRequest(threadContext, unfilteredIndices), + authzInfo(new String[] { "role1" }) + ); assertThat("RunAsDenied message unfiltered indices: filtered out by indices filter", logOutput.size(), is(1)); logOutput.clear(); threadContext.stashContext(); - auditTrail.runAsDenied(randomAlphaOfLength(8), authentication, "_action", new MockIndicesRequest(threadContext, filteredIndices), - authzInfo(new String[] { "role1" })); + auditTrail.runAsDenied( + randomAlphaOfLength(8), + authentication, + "_action", + new MockIndicesRequest(threadContext, filteredIndices), + authzInfo(new String[] { "role1" }) + ); assertThat("RunAsDenied message filtered indices: not filtered out by indices filter", logOutput.size(), is(0)); logOutput.clear(); threadContext.stashContext(); @@ -1864,14 +2447,22 @@ public void testIndicesFilter() throws Exception { logOutput.clear(); threadContext.stashContext(); - auditTrail.authenticationSuccess(randomAlphaOfLength(8), authentication, "_action", new MockIndicesRequest(threadContext, - unfilteredIndices)); + auditTrail.authenticationSuccess( + randomAlphaOfLength(8), + authentication, + "_action", + new MockIndicesRequest(threadContext, unfilteredIndices) + ); assertThat("AuthenticationSuccess message unfiltered indices: filtered out by indices filter", logOutput.size(), is(1)); logOutput.clear(); threadContext.stashContext(); - auditTrail.authenticationSuccess(randomAlphaOfLength(8), authentication, "_action", - new MockIndicesRequest(threadContext, filteredIndices)); + auditTrail.authenticationSuccess( + randomAlphaOfLength(8), + authentication, + "_action", + new MockIndicesRequest(threadContext, filteredIndices) + ); assertThat("AuthenticationSuccess message filtered indices: not filtered out by indices filter", logOutput.size(), is(0)); logOutput.clear(); threadContext.stashContext(); @@ -1883,19 +2474,19 @@ public void testActionsFilter() throws Exception { final List filteredActions = randomNonEmptyListOfFilteredActions(); final Settings.Builder settingsBuilder = Settings.builder().put(settings); - settingsBuilder.putList("xpack.security.audit.logfile.events.ignore_filters.actionsPolicy.actions", - filteredActions); + settingsBuilder.putList("xpack.security.audit.logfile.events.ignore_filters.actionsPolicy.actions", filteredActions); // a filter for a field consisting of an empty string ("") or an empty list([]) // will match events that lack that field final boolean filterMissingAction = randomBoolean(); if (filterMissingAction) { if (randomBoolean()) { filteredActions.add(""); - settingsBuilder.putList("xpack.security.audit.logfile.events.ignore_filters.missingPolicy.actions", - filteredActions); + settingsBuilder.putList("xpack.security.audit.logfile.events.ignore_filters.missingPolicy.actions", filteredActions); } else { - settingsBuilder.putList("xpack.security.audit.logfile.events.ignore_filters.missingPolicy.actions", - Collections.emptyList()); + settingsBuilder.putList( + "xpack.security.audit.logfile.events.ignore_filters.missingPolicy.actions", + Collections.emptyList() + ); } } final String filteredAction = randomFrom(filteredActions); @@ -1906,7 +2497,8 @@ public void testActionsFilter() throws Exception { } else { user = new User("user1", new String[] { "r1" }); } - final TransportRequest request = randomBoolean() ? new MockRequest(threadContext) + final TransportRequest request = randomBoolean() + ? new MockRequest(threadContext) : new MockIndicesRequest(threadContext, new String[] { "idx1", "idx2" }); final MockToken authToken = new MockToken("token1"); final LoggingAuditTrail auditTrail = new LoggingAuditTrail(settingsBuilder.build(), clusterService, logger, threadContext); @@ -1919,7 +2511,7 @@ public void testActionsFilter() throws Exception { threadContext.stashContext(); auditTrail.anonymousAccessDenied(randomAlphaOfLength(8), getRestRequest()); - if (filterMissingAction){ + if (filterMissingAction) { assertThat("Anonymous rest request: not filtered out by the missing action filter", logOutput.size(), is(0)); } else { assertThat("Anonymous rest request: filtered out by action filter", logOutput.size(), is(1)); @@ -1929,7 +2521,7 @@ public void testActionsFilter() throws Exception { // authenticationFailed auditTrail.authenticationFailed(randomAlphaOfLength(8), getRestRequest()); - if (filterMissingAction){ + if (filterMissingAction) { assertThat("AuthenticationFailed: not filtered out by the missing action filter", logOutput.size(), is(0)); } else { assertThat("AuthenticationFailed: filtered out by action filter", logOutput.size(), is(1)); @@ -1977,12 +2569,12 @@ public void testActionsFilter() throws Exception { // accessGranted Authentication authentication = createAuthentication(user, "realm"); - auditTrail.accessGranted(randomAlphaOfLength(8), authentication, filteredAction, request, authzInfo(new String[]{"role1"})); + auditTrail.accessGranted(randomAlphaOfLength(8), authentication, filteredAction, request, authzInfo(new String[] { "role1" })); assertThat("AccessGranted message: not filtered out by the action filters", logOutput.size(), is(0)); logOutput.clear(); threadContext.stashContext(); - auditTrail.accessGranted(randomAlphaOfLength(8), authentication, unfilteredAction, request, authzInfo(new String[]{"role1"})); + auditTrail.accessGranted(randomAlphaOfLength(8), authentication, unfilteredAction, request, authzInfo(new String[] { "role1" })); assertThat("AccessGranted message: unfiltered action filtered out by the action filter", logOutput.size(), is(1)); logOutput.clear(); threadContext.stashContext(); @@ -1990,12 +2582,12 @@ public void testActionsFilter() throws Exception { threadContext.stashContext(); // accessDenied - auditTrail.accessDenied(randomAlphaOfLength(8), authentication, filteredAction, request, authzInfo(new String[]{"role1"})); + auditTrail.accessDenied(randomAlphaOfLength(8), authentication, filteredAction, request, authzInfo(new String[] { "role1" })); assertThat("AccessDenied message: not filtered out by the action filters", logOutput.size(), is(0)); logOutput.clear(); threadContext.stashContext(); - auditTrail.accessDenied(randomAlphaOfLength(8), authentication, unfilteredAction, request, authzInfo(new String[]{"role1"})); + auditTrail.accessDenied(randomAlphaOfLength(8), authentication, unfilteredAction, request, authzInfo(new String[] { "role1" })); assertThat("AccessDenied message: unfiltered action filtered out by the action filter", logOutput.size(), is(1)); logOutput.clear(); threadContext.stashContext(); @@ -2046,33 +2638,57 @@ public void testActionsFilter() throws Exception { threadContext.stashContext(); // runAsGranted - auditTrail.runAsGranted(randomAlphaOfLength(8), createAuthentication(user, "realm"), filteredAction, - new MockRequest(threadContext), authzInfo(new String[] { "role1" })); + auditTrail.runAsGranted( + randomAlphaOfLength(8), + createAuthentication(user, "realm"), + filteredAction, + new MockRequest(threadContext), + authzInfo(new String[] { "role1" }) + ); assertThat("RunAsGranted message: not filtered out by the action filters", logOutput.size(), is(0)); logOutput.clear(); threadContext.stashContext(); - auditTrail.runAsGranted(randomAlphaOfLength(8), createAuthentication(user, "realm"), unfilteredAction, - new MockRequest(threadContext), authzInfo(new String[] { "role1" })); + auditTrail.runAsGranted( + randomAlphaOfLength(8), + createAuthentication(user, "realm"), + unfilteredAction, + new MockRequest(threadContext), + authzInfo(new String[] { "role1" }) + ); assertThat("RunAsGranted message: unfiltered action is filtered out", logOutput.size(), is(1)); logOutput.clear(); threadContext.stashContext(); // runAsDenied - auditTrail.runAsDenied(randomAlphaOfLength(8), createAuthentication(user, "realm"), filteredAction, new MockRequest(threadContext), - authzInfo(new String[] { "role1" })); + auditTrail.runAsDenied( + randomAlphaOfLength(8), + createAuthentication(user, "realm"), + filteredAction, + new MockRequest(threadContext), + authzInfo(new String[] { "role1" }) + ); assertThat("RunAsDenied message: not filtered out by the action filters", logOutput.size(), is(0)); logOutput.clear(); threadContext.stashContext(); - auditTrail.runAsDenied(randomAlphaOfLength(8), createAuthentication(user, "realm"), unfilteredAction, - new MockRequest(threadContext), authzInfo(new String[] { "role1" })); + auditTrail.runAsDenied( + randomAlphaOfLength(8), + createAuthentication(user, "realm"), + unfilteredAction, + new MockRequest(threadContext), + authzInfo(new String[] { "role1" }) + ); assertThat("RunAsDenied message: unfiltered action filtered out by the action filters", logOutput.size(), is(1)); logOutput.clear(); threadContext.stashContext(); - auditTrail.runAsDenied(randomAlphaOfLength(8), createAuthentication(user, "realm"), getRestRequest(), - authzInfo(new String[] { "role1" })); + auditTrail.runAsDenied( + randomAlphaOfLength(8), + createAuthentication(user, "realm"), + getRestRequest(), + authzInfo(new String[] { "role1" }) + ); if (filterMissingAction) { assertThat("RunAsDenied rest request: not filtered out by the missing action filter", logOutput.size(), is(0)); } else { @@ -2114,8 +2730,11 @@ private List randomListFromLengthBetween(List l, int min, int max) { private static Authentication createAuthentication(User user, String effectiveRealmName) { if (user.isRunAs()) { - return new Authentication(user, - new RealmRef(UNFILTER_MARKER + randomAlphaOfLength(4), "test", "foo"), new RealmRef(effectiveRealmName, "up", "by")); + return new Authentication( + user, + new RealmRef(UNFILTER_MARKER + randomAlphaOfLength(4), "test", "foo"), + new RealmRef(effectiveRealmName, "up", "by") + ); } else { return new Authentication(user, new RealmRef(effectiveRealmName, "test", "foo"), null); } @@ -2186,11 +2805,11 @@ private List randomNonEmptyListOfFilteredActions() { "cluster:admin/xpack/ccr/*", "cluster:admin/ilm/*", "cluster:admin/slm/*", - "cluster:admin/xpack/enrich/*"}; + "cluster:admin/xpack/enrich/*" }; Random random = random(); for (int i = 0; i < randomIntBetween(1, 4); i++) { Object name = actionPatterns[random.nextInt(actionPatterns.length)]; - filtered.add((String)name); + filtered.add((String) name); } return filtered; } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/audit/logfile/LoggingAuditTrailTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/audit/logfile/LoggingAuditTrailTests.java index c31e58288c439..e9163f783b960 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/audit/logfile/LoggingAuditTrailTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/audit/logfile/LoggingAuditTrailTests.java @@ -21,7 +21,6 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.collect.MapBuilder; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.logging.Loggers; import org.elasticsearch.common.network.NetworkAddress; @@ -30,12 +29,9 @@ import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.TransportAddress; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentType; -import org.mockito.Mockito; +import org.elasticsearch.core.TimeValue; +import org.elasticsearch.core.Tuple; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.tasks.Task; import org.elasticsearch.test.ESTestCase; @@ -43,6 +39,9 @@ import org.elasticsearch.test.rest.FakeRestRequest.Builder; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportRequest; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.xpack.core.security.action.CreateApiKeyAction; import org.elasticsearch.xpack.core.security.action.CreateApiKeyRequest; @@ -112,6 +111,7 @@ import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; +import org.mockito.Mockito; import org.mockito.stubbing.Answer; import java.io.BufferedInputStream; @@ -256,15 +256,15 @@ public static void releasePatternLayout() { public void init() throws Exception { includeRequestBody = randomBoolean(); settings = Settings.builder() - .put(LoggingAuditTrail.EMIT_HOST_ADDRESS_SETTING.getKey(), randomBoolean()) - .put(LoggingAuditTrail.EMIT_HOST_NAME_SETTING.getKey(), randomBoolean()) - .put(LoggingAuditTrail.EMIT_NODE_NAME_SETTING.getKey(), randomBoolean()) - .put(LoggingAuditTrail.EMIT_NODE_ID_SETTING.getKey(), randomBoolean()) - .put(LoggingAuditTrail.INCLUDE_REQUEST_BODY.getKey(), includeRequestBody) - .put(XPackSettings.RESERVED_REALM_ENABLED_SETTING.getKey(), reservedRealmEnabled) - .put(AnonymousUser.USERNAME_SETTING.getKey(), customAnonymousUsername) - .putList(AnonymousUser.ROLES_SETTING.getKey(), randomFrom(List.of(), List.of("smth"))) - .build(); + .put(LoggingAuditTrail.EMIT_HOST_ADDRESS_SETTING.getKey(), randomBoolean()) + .put(LoggingAuditTrail.EMIT_HOST_NAME_SETTING.getKey(), randomBoolean()) + .put(LoggingAuditTrail.EMIT_NODE_NAME_SETTING.getKey(), randomBoolean()) + .put(LoggingAuditTrail.EMIT_NODE_ID_SETTING.getKey(), randomBoolean()) + .put(LoggingAuditTrail.INCLUDE_REQUEST_BODY.getKey(), includeRequestBody) + .put(XPackSettings.RESERVED_REALM_ENABLED_SETTING.getKey(), reservedRealmEnabled) + .put(AnonymousUser.USERNAME_SETTING.getKey(), customAnonymousUsername) + .putList(AnonymousUser.ROLES_SETTING.getKey(), randomFrom(List.of(), List.of("smth"))) + .build(); localNode = mock(DiscoveryNode.class); when(localNode.getId()).thenReturn(randomAlphaOfLength(16)); when(localNode.getAddress()).thenReturn(buildNewFakeTransportAddress()); @@ -277,14 +277,24 @@ public void init() throws Exception { arg0.updateLocalNodeInfo(localNode); return null; }).when(clusterService).addListener(Mockito.isA(LoggingAuditTrail.class)); - final ClusterSettings clusterSettings = new ClusterSettings(settings, - Set.of(LoggingAuditTrail.EMIT_HOST_ADDRESS_SETTING, LoggingAuditTrail.EMIT_HOST_NAME_SETTING, - LoggingAuditTrail.EMIT_NODE_NAME_SETTING, LoggingAuditTrail.EMIT_NODE_ID_SETTING, - LoggingAuditTrail.INCLUDE_EVENT_SETTINGS, LoggingAuditTrail.EXCLUDE_EVENT_SETTINGS, - LoggingAuditTrail.INCLUDE_REQUEST_BODY, LoggingAuditTrail.FILTER_POLICY_IGNORE_PRINCIPALS, - LoggingAuditTrail.FILTER_POLICY_IGNORE_REALMS, LoggingAuditTrail.FILTER_POLICY_IGNORE_ROLES, - LoggingAuditTrail.FILTER_POLICY_IGNORE_INDICES, LoggingAuditTrail.FILTER_POLICY_IGNORE_ACTIONS, - Loggers.LOG_LEVEL_SETTING)); + final ClusterSettings clusterSettings = new ClusterSettings( + settings, + Set.of( + LoggingAuditTrail.EMIT_HOST_ADDRESS_SETTING, + LoggingAuditTrail.EMIT_HOST_NAME_SETTING, + LoggingAuditTrail.EMIT_NODE_NAME_SETTING, + LoggingAuditTrail.EMIT_NODE_ID_SETTING, + LoggingAuditTrail.INCLUDE_EVENT_SETTINGS, + LoggingAuditTrail.EXCLUDE_EVENT_SETTINGS, + LoggingAuditTrail.INCLUDE_REQUEST_BODY, + LoggingAuditTrail.FILTER_POLICY_IGNORE_PRINCIPALS, + LoggingAuditTrail.FILTER_POLICY_IGNORE_REALMS, + LoggingAuditTrail.FILTER_POLICY_IGNORE_ROLES, + LoggingAuditTrail.FILTER_POLICY_IGNORE_INDICES, + LoggingAuditTrail.FILTER_POLICY_IGNORE_ACTIONS, + Loggers.LOG_LEVEL_SETTING + ) + ); when(clusterService.getClusterSettings()).thenReturn(clusterSettings); commonFields = new LoggingAuditTrail.EntryCommonFields(settings, localNode).commonFields; threadContext = new ThreadContext(Settings.EMPTY); @@ -292,13 +302,22 @@ public void init() throws Exception { threadContext.putHeader(Task.X_OPAQUE_ID, randomAlphaOfLengthBetween(1, 4)); } if (randomBoolean()) { - threadContext.putHeader(AuditTrail.X_FORWARDED_FOR_HEADER, - randomFrom("2001:db8:85a3:8d3:1319:8a2e:370:7348", "203.0.113.195", "203.0.113.195, 70.41.3.18, 150.172.238.178")); + threadContext.putHeader( + AuditTrail.X_FORWARDED_FOR_HEADER, + randomFrom("2001:db8:85a3:8d3:1319:8a2e:370:7348", "203.0.113.195", "203.0.113.195, 70.41.3.18, 150.172.238.178") + ); } logger = CapturingLogger.newCapturingLogger(randomFrom(Level.OFF, Level.FATAL, Level.ERROR, Level.WARN, Level.INFO), patternLayout); auditTrail = new LoggingAuditTrail(settings, clusterService, logger, threadContext); - apiKeyService = new ApiKeyService(settings, Clock.systemUTC(), client, securityIndexManager, clusterService, - mock(CacheInvalidatorRegistry.class), mock(ThreadPool.class)); + apiKeyService = new ApiKeyService( + settings, + Clock.systemUTC(), + client, + securityIndexManager, + clusterService, + mock(CacheInvalidatorRegistry.class), + mock(ThreadPool.class) + ); } @After @@ -309,8 +328,10 @@ public void clearLog() throws Exception { public void testEventsSettingValidation() { final String prefix = "xpack.security.audit.logfile.events."; Settings settings = Settings.builder().putList(prefix + "include", Arrays.asList("access_granted", "bogus")).build(); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> LoggingAuditTrail.INCLUDE_EVENT_SETTINGS.get(settings)); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> LoggingAuditTrail.INCLUDE_EVENT_SETTINGS.get(settings) + ); assertThat(e, hasToString(containsString("invalid event name specified [bogus]"))); Settings settings2 = Settings.builder().putList(prefix + "exclude", Arrays.asList("access_denied", "foo")).build(); @@ -320,54 +341,78 @@ public void testEventsSettingValidation() { public void testAuditFilterSettingValidation() { final String prefix = "xpack.security.audit.logfile.events."; - Settings settings = - Settings.builder().putList(prefix + "ignore_filters.filter1.users", Arrays.asList("mickey", "/bogus")).build(); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> LoggingAuditTrail.FILTER_POLICY_IGNORE_PRINCIPALS.getConcreteSettingForNamespace("filter1").get(settings)); + Settings settings = Settings.builder().putList(prefix + "ignore_filters.filter1.users", Arrays.asList("mickey", "/bogus")).build(); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> LoggingAuditTrail.FILTER_POLICY_IGNORE_PRINCIPALS.getConcreteSettingForNamespace("filter1").get(settings) + ); assertThat(e, hasToString(containsString("invalid pattern [/bogus]"))); Settings settings2 = Settings.builder() - .putList(prefix + "ignore_filters.filter2.users", Arrays.asList("tom", "cruise")) - .putList(prefix + "ignore_filters.filter2.realms", Arrays.asList("native", "/foo")).build(); - assertThat(LoggingAuditTrail.FILTER_POLICY_IGNORE_PRINCIPALS.getConcreteSettingForNamespace("filter2").get(settings2), - containsInAnyOrder("tom", "cruise")); - e = expectThrows(IllegalArgumentException.class, - () -> LoggingAuditTrail.FILTER_POLICY_IGNORE_REALMS.getConcreteSettingForNamespace("filter2").get(settings2)); + .putList(prefix + "ignore_filters.filter2.users", Arrays.asList("tom", "cruise")) + .putList(prefix + "ignore_filters.filter2.realms", Arrays.asList("native", "/foo")) + .build(); + assertThat( + LoggingAuditTrail.FILTER_POLICY_IGNORE_PRINCIPALS.getConcreteSettingForNamespace("filter2").get(settings2), + containsInAnyOrder("tom", "cruise") + ); + e = expectThrows( + IllegalArgumentException.class, + () -> LoggingAuditTrail.FILTER_POLICY_IGNORE_REALMS.getConcreteSettingForNamespace("filter2").get(settings2) + ); assertThat(e, hasToString(containsString("invalid pattern [/foo]"))); Settings settings3 = Settings.builder() - .putList(prefix + "ignore_filters.filter3.realms", Arrays.asList("native", "oidc1")) - .putList(prefix + "ignore_filters.filter3.roles", Arrays.asList("kibana", "/wrong")).build(); - assertThat(LoggingAuditTrail.FILTER_POLICY_IGNORE_REALMS.getConcreteSettingForNamespace("filter3").get(settings3), - containsInAnyOrder("native", "oidc1")); - e = expectThrows(IllegalArgumentException.class, - () -> LoggingAuditTrail.FILTER_POLICY_IGNORE_ROLES.getConcreteSettingForNamespace("filter3").get(settings3)); + .putList(prefix + "ignore_filters.filter3.realms", Arrays.asList("native", "oidc1")) + .putList(prefix + "ignore_filters.filter3.roles", Arrays.asList("kibana", "/wrong")) + .build(); + assertThat( + LoggingAuditTrail.FILTER_POLICY_IGNORE_REALMS.getConcreteSettingForNamespace("filter3").get(settings3), + containsInAnyOrder("native", "oidc1") + ); + e = expectThrows( + IllegalArgumentException.class, + () -> LoggingAuditTrail.FILTER_POLICY_IGNORE_ROLES.getConcreteSettingForNamespace("filter3").get(settings3) + ); assertThat(e, hasToString(containsString("invalid pattern [/wrong]"))); Settings settings4 = Settings.builder() - .putList(prefix + "ignore_filters.filter4.roles", Arrays.asList("kibana", "elastic")) - .putList(prefix + "ignore_filters.filter4.indices", Arrays.asList("index-1", "/no-inspiration")).build(); - assertThat(LoggingAuditTrail.FILTER_POLICY_IGNORE_ROLES.getConcreteSettingForNamespace("filter4").get(settings4), - containsInAnyOrder("kibana", "elastic")); - e = expectThrows(IllegalArgumentException.class, - () -> LoggingAuditTrail.FILTER_POLICY_IGNORE_INDICES.getConcreteSettingForNamespace("filter4").get(settings4)); + .putList(prefix + "ignore_filters.filter4.roles", Arrays.asList("kibana", "elastic")) + .putList(prefix + "ignore_filters.filter4.indices", Arrays.asList("index-1", "/no-inspiration")) + .build(); + assertThat( + LoggingAuditTrail.FILTER_POLICY_IGNORE_ROLES.getConcreteSettingForNamespace("filter4").get(settings4), + containsInAnyOrder("kibana", "elastic") + ); + e = expectThrows( + IllegalArgumentException.class, + () -> LoggingAuditTrail.FILTER_POLICY_IGNORE_INDICES.getConcreteSettingForNamespace("filter4").get(settings4) + ); assertThat(e, hasToString(containsString("invalid pattern [/no-inspiration]"))); Settings settings5 = Settings.builder() .putList(prefix + "ignore_filters.filter2.users", Arrays.asList("tom", "cruise")) - .putList(prefix + "ignore_filters.filter2.actions", Arrays.asList("indices:data/read/*", "/foo")).build(); - assertThat(LoggingAuditTrail.FILTER_POLICY_IGNORE_PRINCIPALS.getConcreteSettingForNamespace("filter2").get(settings5), - containsInAnyOrder("tom", "cruise")); - e = expectThrows(IllegalArgumentException.class, - () -> LoggingAuditTrail.FILTER_POLICY_IGNORE_ACTIONS.getConcreteSettingForNamespace("filter2").get(settings5)); + .putList(prefix + "ignore_filters.filter2.actions", Arrays.asList("indices:data/read/*", "/foo")) + .build(); + assertThat( + LoggingAuditTrail.FILTER_POLICY_IGNORE_PRINCIPALS.getConcreteSettingForNamespace("filter2").get(settings5), + containsInAnyOrder("tom", "cruise") + ); + e = expectThrows( + IllegalArgumentException.class, + () -> LoggingAuditTrail.FILTER_POLICY_IGNORE_ACTIONS.getConcreteSettingForNamespace("filter2").get(settings5) + ); assertThat(e, hasToString(containsString("invalid pattern [/foo]"))); } public void testSecurityConfigChangeEventFormattingForRoles() throws IOException { final Path path = getDataPath("/org/elasticsearch/xpack/security/audit/logfile/audited_roles.txt"); final Map auditedRolesMap = new HashMap<>(); - try (BufferedReader reader = new BufferedReader(new InputStreamReader(new BufferedInputStream(Files.newInputStream(path)), - StandardCharsets.UTF_8))) { + try ( + BufferedReader reader = new BufferedReader( + new InputStreamReader(new BufferedInputStream(Files.newInputStream(path)), StandardCharsets.UTF_8) + ) + ) { String line; while ((line = reader.readLine()) != null) { // even number of lines @@ -375,95 +420,108 @@ public void testSecurityConfigChangeEventFormattingForRoles() throws IOException } } - RoleDescriptor nullRoleDescriptor = new RoleDescriptor("null_role", randomFrom((String[]) null, new String[0]), - randomFrom((RoleDescriptor.IndicesPrivileges[]) null, new RoleDescriptor.IndicesPrivileges[0]), - randomFrom((RoleDescriptor.ApplicationResourcePrivileges[])null, new RoleDescriptor.ApplicationResourcePrivileges[0]), - randomFrom((ConfigurableClusterPrivilege[])null, new ConfigurableClusterPrivilege[0]), - randomFrom((String[])null, new String[0]), - randomFrom((Map)null, Map.of()), - Map.of("transient", "meta", "is", "ignored")); - RoleDescriptor roleDescriptor1 = new RoleDescriptor("role_descriptor1", new String[]{"monitor"}, - new RoleDescriptor.IndicesPrivileges[]{RoleDescriptor.IndicesPrivileges.builder() - .indices("test*") - .privileges("read", "create_index") - .grantedFields("grantedField1") - .query("{\"match_all\":{}}") - .allowRestrictedIndices(true) - .build()}, - randomFrom((RoleDescriptor.ApplicationResourcePrivileges[]) null, new RoleDescriptor.ApplicationResourcePrivileges[0]), - randomFrom((ConfigurableClusterPrivilege[]) null, new ConfigurableClusterPrivilege[0]), - randomFrom((String[]) null, new String[0]), - randomFrom((Map) null, Map.of()), - Map.of() - ); - RoleDescriptor roleDescriptor2 = new RoleDescriptor("role_descriptor2", randomFrom((String[]) null, new String[0]), - new RoleDescriptor.IndicesPrivileges[]{ - RoleDescriptor.IndicesPrivileges.builder() - .indices("na\"me", "*") - .privileges("manage_ilm") - .deniedFields("denied*") - .query("{\"match\": {\"category\": \"click\"}}") - .build(), - RoleDescriptor.IndicesPrivileges.builder() - .indices("/@&~(\\.security.*)/") - .privileges("all", "cluster:a_wrong_*_one") - .build()}, - new RoleDescriptor.ApplicationResourcePrivileges[] { - RoleDescriptor.ApplicationResourcePrivileges.builder() - .application("maps") - .resources("raster:*") - .privileges("coming", "up", "with", "random", "names", "is", "hard") - .build()}, - randomFrom((ConfigurableClusterPrivilege[]) null, new ConfigurableClusterPrivilege[0]), - new String[] {"impersonated???"}, - randomFrom((Map) null, Map.of()), - Map.of() - ); - RoleDescriptor roleDescriptor3 = new RoleDescriptor("role_descriptor3", randomFrom((String[]) null, new String[0]), - randomFrom((RoleDescriptor.IndicesPrivileges[]) null, new RoleDescriptor.IndicesPrivileges[0]), - new RoleDescriptor.ApplicationResourcePrivileges[] { - RoleDescriptor.ApplicationResourcePrivileges.builder() - .application("maps") - .resources("raster:*") - .privileges("{", "}", "\n", "\\", "\"") - .build(), - RoleDescriptor.ApplicationResourcePrivileges.builder() - .application("maps") - .resources("noooooo!!\n\n\f\\\\r", "{") - .privileges("*:*") - .build()}, - randomFrom((ConfigurableClusterPrivilege[]) null, new ConfigurableClusterPrivilege[0]), - new String[] {"jack", "nich*", "//\""}, - Map.of("some meta", 42), - Map.of() + RoleDescriptor nullRoleDescriptor = new RoleDescriptor( + "null_role", + randomFrom((String[]) null, new String[0]), + randomFrom((RoleDescriptor.IndicesPrivileges[]) null, new RoleDescriptor.IndicesPrivileges[0]), + randomFrom((RoleDescriptor.ApplicationResourcePrivileges[]) null, new RoleDescriptor.ApplicationResourcePrivileges[0]), + randomFrom((ConfigurableClusterPrivilege[]) null, new ConfigurableClusterPrivilege[0]), + randomFrom((String[]) null, new String[0]), + randomFrom((Map) null, Map.of()), + Map.of("transient", "meta", "is", "ignored") + ); + RoleDescriptor roleDescriptor1 = new RoleDescriptor( + "role_descriptor1", + new String[] { "monitor" }, + new RoleDescriptor.IndicesPrivileges[] { + RoleDescriptor.IndicesPrivileges.builder() + .indices("test*") + .privileges("read", "create_index") + .grantedFields("grantedField1") + .query("{\"match_all\":{}}") + .allowRestrictedIndices(true) + .build() }, + randomFrom((RoleDescriptor.ApplicationResourcePrivileges[]) null, new RoleDescriptor.ApplicationResourcePrivileges[0]), + randomFrom((ConfigurableClusterPrivilege[]) null, new ConfigurableClusterPrivilege[0]), + randomFrom((String[]) null, new String[0]), + randomFrom((Map) null, Map.of()), + Map.of() + ); + RoleDescriptor roleDescriptor2 = new RoleDescriptor( + "role_descriptor2", + randomFrom((String[]) null, new String[0]), + new RoleDescriptor.IndicesPrivileges[] { + RoleDescriptor.IndicesPrivileges.builder() + .indices("na\"me", "*") + .privileges("manage_ilm") + .deniedFields("denied*") + .query("{\"match\": {\"category\": \"click\"}}") + .build(), + RoleDescriptor.IndicesPrivileges.builder() + .indices("/@&~(\\.security.*)/") + .privileges("all", "cluster:a_wrong_*_one") + .build() }, + new RoleDescriptor.ApplicationResourcePrivileges[] { + RoleDescriptor.ApplicationResourcePrivileges.builder() + .application("maps") + .resources("raster:*") + .privileges("coming", "up", "with", "random", "names", "is", "hard") + .build() }, + randomFrom((ConfigurableClusterPrivilege[]) null, new ConfigurableClusterPrivilege[0]), + new String[] { "impersonated???" }, + randomFrom((Map) null, Map.of()), + Map.of() + ); + RoleDescriptor roleDescriptor3 = new RoleDescriptor( + "role_descriptor3", + randomFrom((String[]) null, new String[0]), + randomFrom((RoleDescriptor.IndicesPrivileges[]) null, new RoleDescriptor.IndicesPrivileges[0]), + new RoleDescriptor.ApplicationResourcePrivileges[] { + RoleDescriptor.ApplicationResourcePrivileges.builder() + .application("maps") + .resources("raster:*") + .privileges("{", "}", "\n", "\\", "\"") + .build(), + RoleDescriptor.ApplicationResourcePrivileges.builder() + .application("maps") + .resources("noooooo!!\n\n\f\\\\r", "{") + .privileges("*:*") + .build() }, + randomFrom((ConfigurableClusterPrivilege[]) null, new ConfigurableClusterPrivilege[0]), + new String[] { "jack", "nich*", "//\"" }, + Map.of("some meta", 42), + Map.of() ); Map metaMap = new TreeMap<>(); metaMap.put("?list", List.of("e1", "e2", "*")); metaMap.put("some other meta", Map.of("r", "t")); - RoleDescriptor roleDescriptor4 = new RoleDescriptor("role_descriptor4", new String[] {"manage_ml", "grant_api_key", - "manage_rollup"}, - new RoleDescriptor.IndicesPrivileges[]{ - RoleDescriptor.IndicesPrivileges.builder() - .indices("/. ? + * | { } [ ] ( ) \" \\/", "*") - .privileges("read", "read_cross_cluster") - .grantedFields("almost", "all*") - .deniedFields("denied*") - .build()}, - randomFrom((RoleDescriptor.ApplicationResourcePrivileges[]) null, new RoleDescriptor.ApplicationResourcePrivileges[0]), - new ConfigurableClusterPrivilege[] { - new ConfigurableClusterPrivileges.ManageApplicationPrivileges(Set.of("a+b+|b+a+")) - }, - new String[] {"//+a+\"[a]/"}, - metaMap, - Map.of("ignored", 2) + RoleDescriptor roleDescriptor4 = new RoleDescriptor( + "role_descriptor4", + new String[] { "manage_ml", "grant_api_key", "manage_rollup" }, + new RoleDescriptor.IndicesPrivileges[] { + RoleDescriptor.IndicesPrivileges.builder() + .indices("/. ? + * | { } [ ] ( ) \" \\/", "*") + .privileges("read", "read_cross_cluster") + .grantedFields("almost", "all*") + .deniedFields("denied*") + .build() }, + randomFrom((RoleDescriptor.ApplicationResourcePrivileges[]) null, new RoleDescriptor.ApplicationResourcePrivileges[0]), + new ConfigurableClusterPrivilege[] { new ConfigurableClusterPrivileges.ManageApplicationPrivileges(Set.of("a+b+|b+a+")) }, + new String[] { "//+a+\"[a]/" }, + metaMap, + Map.of("ignored", 2) ); String keyName = randomAlphaOfLength(4); TimeValue expiration = randomFrom(new TimeValue(randomNonNegativeLong(), randomFrom(TimeUnit.values())), null); - List allTestRoleDescriptors = List.of(nullRoleDescriptor, roleDescriptor1, roleDescriptor2, roleDescriptor3, - roleDescriptor4); + List allTestRoleDescriptors = List.of( + nullRoleDescriptor, + roleDescriptor1, + roleDescriptor2, + roleDescriptor3, + roleDescriptor4 + ); List keyRoleDescriptors = randomSubsetOf(allTestRoleDescriptors); - StringBuilder roleDescriptorsStringBuilder = new StringBuilder() - .append("\"role_descriptors\":["); + StringBuilder roleDescriptorsStringBuilder = new StringBuilder().append("\"role_descriptors\":["); keyRoleDescriptors.forEach(roleDescriptor -> { roleDescriptorsStringBuilder.append(auditedRolesMap.get(roleDescriptor.getName())); roleDescriptorsStringBuilder.append(','); @@ -481,10 +539,13 @@ public void testSecurityConfigChangeEventFormattingForRoles() throws IOException CreateApiKeyRequest createApiKeyRequest = new CreateApiKeyRequest(keyName, keyRoleDescriptors, expiration); createApiKeyRequest.setRefreshPolicy(randomFrom(WriteRequest.RefreshPolicy.values())); auditTrail.accessGranted(requestId, authentication, CreateApiKeyAction.NAME, createApiKeyRequest, authorizationInfo); - String expectedCreateKeyAuditEventString = "\"create\":{\"apikey\":{\"name\":\"" + keyName + "\",\"expiration\":" + - (expiration != null ? "\"" + expiration.toString() + "\"" : "null") + "," + - roleDescriptorsStringBuilder + - "}}"; + String expectedCreateKeyAuditEventString = "\"create\":{\"apikey\":{\"name\":\"" + + keyName + + "\",\"expiration\":" + + (expiration != null ? "\"" + expiration.toString() + "\"" : "null") + + "," + + roleDescriptorsStringBuilder + + "}}"; List output = CapturingLogger.output(logger.getName(), Level.INFO); assertThat(output.size(), is(2)); String generatedCreateKeyAuditEventString = output.get(1); @@ -494,9 +555,9 @@ public void testSecurityConfigChangeEventFormattingForRoles() throws IOException checkedFields.remove(LoggingAuditTrail.ORIGIN_ADDRESS_FIELD_NAME); checkedFields.remove(LoggingAuditTrail.ORIGIN_TYPE_FIELD_NAME); checkedFields.put("type", "audit") - .put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, "security_config_change") - .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "create_apikey") - .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); + .put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, "security_config_change") + .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "create_apikey") + .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); assertMsg(generatedCreateKeyAuditEventString, checkedFields.immutableMap()); // clear log CapturingLogger.output(logger.getName(), Level.INFO).clear(); @@ -512,18 +573,24 @@ public void testSecurityConfigChangeEventFormattingForRoles() throws IOException output = CapturingLogger.output(logger.getName(), Level.INFO); assertThat(output.size(), is(2)); String generatedGrantKeyAuditEventString = output.get(1); - StringBuilder grantKeyAuditEventStringBuilder = new StringBuilder() - .append("\"create\":{\"apikey\":{\"name\":\"").append(keyName) - .append("\",\"expiration\":").append(expiration != null ? "\"" + expiration + "\"" : "null").append(",") - .append(roleDescriptorsStringBuilder).append("},\"grant\":{\"type\":"); + StringBuilder grantKeyAuditEventStringBuilder = new StringBuilder().append("\"create\":{\"apikey\":{\"name\":\"") + .append(keyName) + .append("\",\"expiration\":") + .append(expiration != null ? "\"" + expiration + "\"" : "null") + .append(",") + .append(roleDescriptorsStringBuilder) + .append("},\"grant\":{\"type\":"); if (grantApiKeyRequest.getGrant().getType() != null) { grantKeyAuditEventStringBuilder.append("\"").append(grantApiKeyRequest.getGrant().getType()).append("\""); } else { grantKeyAuditEventStringBuilder.append("null"); } if (grantApiKeyRequest.getGrant().getUsername() != null) { - grantKeyAuditEventStringBuilder.append(",\"user\":{\"name\":\"").append(grantApiKeyRequest.getGrant().getUsername()) - .append("\",\"has_password\":").append(grantApiKeyRequest.getGrant().getPassword() != null).append("}"); + grantKeyAuditEventStringBuilder.append(",\"user\":{\"name\":\"") + .append(grantApiKeyRequest.getGrant().getUsername()) + .append("\",\"has_password\":") + .append(grantApiKeyRequest.getGrant().getPassword() != null) + .append("}"); } if (grantApiKeyRequest.getGrant().getAccessToken() != null) { grantKeyAuditEventStringBuilder.append(",\"has_access_token\":").append(true); @@ -536,9 +603,9 @@ public void testSecurityConfigChangeEventFormattingForRoles() throws IOException checkedFields.remove(LoggingAuditTrail.ORIGIN_ADDRESS_FIELD_NAME); checkedFields.remove(LoggingAuditTrail.ORIGIN_TYPE_FIELD_NAME); checkedFields.put("type", "audit") - .put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, "security_config_change") - .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "create_apikey") - .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); + .put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, "security_config_change") + .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "create_apikey") + .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); assertMsg(generatedGrantKeyAuditEventString, checkedFields.immutableMap()); // clear log CapturingLogger.output(logger.getName(), Level.INFO).clear(); @@ -557,19 +624,21 @@ public void testSecurityConfigChangeEventFormattingForRoles() throws IOException output = CapturingLogger.output(logger.getName(), Level.INFO); assertThat(output.size(), is(2)); String generatedPutRoleAuditEventString = output.get(1); - String expectedPutRoleAuditEventString = "\"put\":{\"role\":{\"name\":\"" + putRoleRequest.name() + "\"," + - "\"role_descriptor\":" + - auditedRolesMap.get(putRoleRequest.name()) + - "}}"; + String expectedPutRoleAuditEventString = "\"put\":{\"role\":{\"name\":\"" + + putRoleRequest.name() + + "\"," + + "\"role_descriptor\":" + + auditedRolesMap.get(putRoleRequest.name()) + + "}}"; assertThat(generatedPutRoleAuditEventString, containsString(expectedPutRoleAuditEventString)); generatedPutRoleAuditEventString = generatedPutRoleAuditEventString.replace(", " + expectedPutRoleAuditEventString, ""); checkedFields = new MapBuilder<>(commonFields); checkedFields.remove(LoggingAuditTrail.ORIGIN_ADDRESS_FIELD_NAME); checkedFields.remove(LoggingAuditTrail.ORIGIN_TYPE_FIELD_NAME); checkedFields.put("type", "audit") - .put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, "security_config_change") - .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "put_role") - .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); + .put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, "security_config_change") + .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "put_role") + .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); assertMsg(generatedPutRoleAuditEventString, checkedFields.immutableMap()); // clear log CapturingLogger.output(logger.getName(), Level.INFO).clear(); @@ -581,8 +650,7 @@ public void testSecurityConfigChangeEventFormattingForRoles() throws IOException output = CapturingLogger.output(logger.getName(), Level.INFO); assertThat(output.size(), is(2)); String generatedDeleteRoleAuditEventString = output.get(1); - StringBuilder deleteRoleStringBuilder = new StringBuilder() - .append("\"delete\":{\"role\":{\"name\":"); + StringBuilder deleteRoleStringBuilder = new StringBuilder().append("\"delete\":{\"role\":{\"name\":"); if (deleteRoleRequest.name() == null) { deleteRoleStringBuilder.append("null"); } else { @@ -591,15 +659,14 @@ public void testSecurityConfigChangeEventFormattingForRoles() throws IOException deleteRoleStringBuilder.append("}}"); String expectedDeleteRoleAuditEventString = deleteRoleStringBuilder.toString(); assertThat(generatedDeleteRoleAuditEventString, containsString(expectedDeleteRoleAuditEventString)); - generatedDeleteRoleAuditEventString = - generatedDeleteRoleAuditEventString.replace(", " + expectedDeleteRoleAuditEventString,""); + generatedDeleteRoleAuditEventString = generatedDeleteRoleAuditEventString.replace(", " + expectedDeleteRoleAuditEventString, ""); checkedFields = new MapBuilder<>(commonFields); checkedFields.remove(LoggingAuditTrail.ORIGIN_ADDRESS_FIELD_NAME); checkedFields.remove(LoggingAuditTrail.ORIGIN_TYPE_FIELD_NAME); checkedFields.put("type", "audit") - .put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, "security_config_change") - .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "delete_role") - .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); + .put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, "security_config_change") + .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "delete_role") + .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); assertMsg(generatedDeleteRoleAuditEventString, checkedFields.immutableMap()); } @@ -614,15 +681,14 @@ public void testSecurityConfigChangeEventFormattingForApiKeyInvalidation() throw randomFrom(randomAlphaOfLength(8), null), randomFrom(randomAlphaOfLength(8), null), randomBoolean(), - randomFrom(randomArray(1,3, String[]::new, () -> randomAlphaOfLength(8)), null) + randomFrom(randomArray(1, 3, String[]::new, () -> randomAlphaOfLength(8)), null) ); auditTrail.accessGranted(requestId, authentication, InvalidateApiKeyAction.NAME, invalidateApiKeyRequest, authorizationInfo); List output = CapturingLogger.output(logger.getName(), Level.INFO); assertThat(output.size(), is(2)); String generatedInvalidateKeyAuditEventString = output.get(1); - StringBuilder invalidateKeyEventStringBuilder = new StringBuilder() - .append("\"invalidate\":{\"apikeys\":{"); + StringBuilder invalidateKeyEventStringBuilder = new StringBuilder().append("\"invalidate\":{\"apikeys\":{"); if (invalidateApiKeyRequest.getIds() != null && invalidateApiKeyRequest.getIds().length > 0) { invalidateKeyEventStringBuilder.append("\"ids\":["); for (String apiKeyId : invalidateApiKeyRequest.getIds()) { @@ -636,7 +702,7 @@ public void testSecurityConfigChangeEventFormattingForApiKeyInvalidation() throw invalidateKeyEventStringBuilder.append("\"name\":\"").append(invalidateApiKeyRequest.getName()).append("\","); } invalidateKeyEventStringBuilder.append("\"owned_by_authenticated_user\":") - .append(invalidateApiKeyRequest.ownedByAuthenticatedUser()); + .append(invalidateApiKeyRequest.ownedByAuthenticatedUser()); if (Strings.hasLength(invalidateApiKeyRequest.getUserName()) || Strings.hasLength(invalidateApiKeyRequest.getRealmName())) { invalidateKeyEventStringBuilder.append(",\"user\":{\"name\":"); if (Strings.hasLength(invalidateApiKeyRequest.getUserName())) { @@ -655,15 +721,17 @@ public void testSecurityConfigChangeEventFormattingForApiKeyInvalidation() throw invalidateKeyEventStringBuilder.append("}}"); String expectedInvalidateKeyEventString = invalidateKeyEventStringBuilder.toString(); assertThat(generatedInvalidateKeyAuditEventString, containsString(expectedInvalidateKeyEventString)); - generatedInvalidateKeyAuditEventString = generatedInvalidateKeyAuditEventString - .replace(", " + expectedInvalidateKeyEventString, ""); + generatedInvalidateKeyAuditEventString = generatedInvalidateKeyAuditEventString.replace( + ", " + expectedInvalidateKeyEventString, + "" + ); MapBuilder checkedFields = new MapBuilder<>(commonFields); checkedFields.remove(LoggingAuditTrail.ORIGIN_ADDRESS_FIELD_NAME); checkedFields.remove(LoggingAuditTrail.ORIGIN_TYPE_FIELD_NAME); checkedFields.put("type", "audit") - .put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, "security_config_change") - .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "invalidate_apikeys") - .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); + .put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, "security_config_change") + .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "invalidate_apikeys") + .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); assertMsg(generatedInvalidateKeyAuditEventString, checkedFields.immutableMap()); } @@ -694,13 +762,16 @@ public void testSecurityConfigChangeEventFormattingForApplicationPrivileges() th List output = CapturingLogger.output(logger.getName(), Level.INFO); assertThat(output.size(), is(2)); String generatedPutPrivilegesAuditEventString = output.get(1); - StringBuilder putPrivilegesAuditEventStringBuilder = new StringBuilder() - .append("\"put\":{\"privileges\":["); + StringBuilder putPrivilegesAuditEventStringBuilder = new StringBuilder().append("\"put\":{\"privileges\":["); if (false == putPrivilegesRequest.getPrivileges().isEmpty()) { for (ApplicationPrivilegeDescriptor appPriv : putPrivilegesRequest.getPrivileges()) { - putPrivilegesAuditEventStringBuilder.append("{\"application\":\"").append(appPriv.getApplication()).append("\"") - .append(",\"name\":\"").append(appPriv.getName()).append("\"") - .append(",\"actions\":["); + putPrivilegesAuditEventStringBuilder.append("{\"application\":\"") + .append(appPriv.getApplication()) + .append("\"") + .append(",\"name\":\"") + .append(appPriv.getName()) + .append("\"") + .append(",\"actions\":["); if (appPriv.getActions().isEmpty()) { putPrivilegesAuditEventStringBuilder.append("]"); } else { @@ -719,27 +790,30 @@ public void testSecurityConfigChangeEventFormattingForApplicationPrivileges() th putPrivilegesAuditEventStringBuilder.append("]}"); String expectedPutPrivilegesEventString = putPrivilegesAuditEventStringBuilder.toString(); assertThat(generatedPutPrivilegesAuditEventString, containsString(expectedPutPrivilegesEventString)); - generatedPutPrivilegesAuditEventString = generatedPutPrivilegesAuditEventString - .replace(", " + expectedPutPrivilegesEventString, ""); + generatedPutPrivilegesAuditEventString = generatedPutPrivilegesAuditEventString.replace( + ", " + expectedPutPrivilegesEventString, + "" + ); MapBuilder checkedFields = new MapBuilder<>(commonFields); checkedFields.remove(LoggingAuditTrail.ORIGIN_ADDRESS_FIELD_NAME); checkedFields.remove(LoggingAuditTrail.ORIGIN_TYPE_FIELD_NAME); checkedFields.put("type", "audit") - .put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, "security_config_change") - .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "put_privileges") - .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); + .put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, "security_config_change") + .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "put_privileges") + .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); assertMsg(generatedPutPrivilegesAuditEventString, checkedFields.immutableMap()); // clear log CapturingLogger.output(logger.getName(), Level.INFO).clear(); - DeletePrivilegesRequest deletePrivilegesRequest = new DeletePrivilegesRequest(randomFrom(randomAlphaOfLength(8), null), - generateRandomStringArray(4, 4, true)); + DeletePrivilegesRequest deletePrivilegesRequest = new DeletePrivilegesRequest( + randomFrom(randomAlphaOfLength(8), null), + generateRandomStringArray(4, 4, true) + ); deletePrivilegesRequest.setRefreshPolicy(randomFrom(WriteRequest.RefreshPolicy.values())); auditTrail.accessGranted(requestId, authentication, DeletePrivilegesAction.NAME, deletePrivilegesRequest, authorizationInfo); output = CapturingLogger.output(logger.getName(), Level.INFO); assertThat(output.size(), is(2)); String generatedDeletePrivilegesAuditEventString = output.get(1); - StringBuilder deletePrivilegesAuditEventStringBuilder = new StringBuilder() - .append("\"delete\":{\"privileges\":{\"application\":"); + StringBuilder deletePrivilegesAuditEventStringBuilder = new StringBuilder().append("\"delete\":{\"privileges\":{\"application\":"); if (deletePrivilegesRequest.application() != null) { deletePrivilegesAuditEventStringBuilder.append("\"").append(deletePrivilegesRequest.application()).append("\""); } else { @@ -766,15 +840,17 @@ public void testSecurityConfigChangeEventFormattingForApplicationPrivileges() th deletePrivilegesAuditEventStringBuilder.append("}}"); String expectedDeletePrivilegesEventString = deletePrivilegesAuditEventStringBuilder.toString(); assertThat(generatedDeletePrivilegesAuditEventString, containsString(expectedDeletePrivilegesEventString)); - generatedDeletePrivilegesAuditEventString = generatedDeletePrivilegesAuditEventString - .replace(", " + expectedDeletePrivilegesEventString, ""); + generatedDeletePrivilegesAuditEventString = generatedDeletePrivilegesAuditEventString.replace( + ", " + expectedDeletePrivilegesEventString, + "" + ); checkedFields = new MapBuilder<>(commonFields); checkedFields.remove(LoggingAuditTrail.ORIGIN_ADDRESS_FIELD_NAME); checkedFields.remove(LoggingAuditTrail.ORIGIN_TYPE_FIELD_NAME); checkedFields.put("type", "audit") - .put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, "security_config_change") - .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "delete_privileges") - .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); + .put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, "security_config_change") + .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "delete_privileges") + .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); assertMsg(generatedDeletePrivilegesAuditEventString, checkedFields.immutableMap()); } @@ -789,9 +865,18 @@ public void testSecurityConfigChangeEventFormattingForRoleMapping() throws IOExc putRoleMappingRequest.setName(randomFrom(randomAlphaOfLength(8), null)); putRoleMappingRequest.setEnabled(randomBoolean()); putRoleMappingRequest.setRoles(Arrays.asList(randomArray(4, String[]::new, () -> randomAlphaOfLength(4)))); - putRoleMappingRequest.setRoleTemplates(Arrays.asList(randomArray(4, TemplateRoleName[]::new, - () -> new TemplateRoleName(new BytesArray(randomAlphaOfLengthBetween(0, 8)), - randomFrom(TemplateRoleName.Format.values()))))); + putRoleMappingRequest.setRoleTemplates( + Arrays.asList( + randomArray( + 4, + TemplateRoleName[]::new, + () -> new TemplateRoleName( + new BytesArray(randomAlphaOfLengthBetween(0, 8)), + randomFrom(TemplateRoleName.Format.values()) + ) + ) + ) + ); RoleMapperExpression mockRoleMapperExpression = new RoleMapperExpression() { @Override public boolean match(ExpressionModel model) { @@ -831,8 +916,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws List output = CapturingLogger.output(logger.getName(), Level.INFO); assertThat(output.size(), is(2)); String generatedPutRoleMappingAuditEventString = output.get(1); - StringBuilder putRoleMappingAuditEventStringBuilder = new StringBuilder() - .append("\"put\":{\"role_mapping\":{\"name\":"); + StringBuilder putRoleMappingAuditEventStringBuilder = new StringBuilder().append("\"put\":{\"role_mapping\":{\"name\":"); if (putRoleMappingRequest.getName() != null) { putRoleMappingAuditEventStringBuilder.append("\"").append(putRoleMappingRequest.getName()).append("\""); } else { @@ -851,10 +935,10 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws putRoleMappingAuditEventStringBuilder.append(",\"role_templates\":["); for (TemplateRoleName templateRoleName : putRoleMappingRequest.getRoleTemplates()) { putRoleMappingAuditEventStringBuilder.append("{\"template\":\"") - .append(templateRoleName.getTemplate().utf8ToString()) - .append("\",\"format\":\"") - .append(templateRoleName.getFormat().toString().toLowerCase(Locale.ROOT)) - .append("\"},"); + .append(templateRoleName.getTemplate().utf8ToString()) + .append("\",\"format\":\"") + .append(templateRoleName.getFormat().toString().toLowerCase(Locale.ROOT)) + .append("\"},"); } // delete last comma putRoleMappingAuditEventStringBuilder.deleteCharAt(putRoleMappingAuditEventStringBuilder.length() - 1); @@ -873,15 +957,17 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws } String expectedPutRoleMappingAuditEventString = putRoleMappingAuditEventStringBuilder.toString(); assertThat(generatedPutRoleMappingAuditEventString, containsString(expectedPutRoleMappingAuditEventString)); - generatedPutRoleMappingAuditEventString = generatedPutRoleMappingAuditEventString - .replace(", " + expectedPutRoleMappingAuditEventString, ""); + generatedPutRoleMappingAuditEventString = generatedPutRoleMappingAuditEventString.replace( + ", " + expectedPutRoleMappingAuditEventString, + "" + ); MapBuilder checkedFields = new MapBuilder<>(commonFields); checkedFields.remove(LoggingAuditTrail.ORIGIN_ADDRESS_FIELD_NAME); checkedFields.remove(LoggingAuditTrail.ORIGIN_TYPE_FIELD_NAME); checkedFields.put("type", "audit") - .put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, "security_config_change") - .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "put_role_mapping") - .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); + .put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, "security_config_change") + .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "put_role_mapping") + .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); assertMsg(generatedPutRoleMappingAuditEventString, checkedFields.immutableMap()); // clear log CapturingLogger.output(logger.getName(), Level.INFO).clear(); @@ -893,8 +979,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws output = CapturingLogger.output(logger.getName(), Level.INFO); assertThat(output.size(), is(2)); String generatedDeleteRoleMappingAuditEventString = output.get(1); - StringBuilder deleteRoleMappingStringBuilder = new StringBuilder() - .append("\"delete\":{\"role_mapping\":{\"name\":"); + StringBuilder deleteRoleMappingStringBuilder = new StringBuilder().append("\"delete\":{\"role_mapping\":{\"name\":"); if (deleteRoleMappingRequest.getName() == null) { deleteRoleMappingStringBuilder.append("null"); } else { @@ -903,15 +988,17 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws deleteRoleMappingStringBuilder.append("}}"); String expectedDeleteRoleMappingAuditEventString = deleteRoleMappingStringBuilder.toString(); assertThat(generatedDeleteRoleMappingAuditEventString, containsString(expectedDeleteRoleMappingAuditEventString)); - generatedDeleteRoleMappingAuditEventString = - generatedDeleteRoleMappingAuditEventString.replace(", " + expectedDeleteRoleMappingAuditEventString,""); + generatedDeleteRoleMappingAuditEventString = generatedDeleteRoleMappingAuditEventString.replace( + ", " + expectedDeleteRoleMappingAuditEventString, + "" + ); checkedFields = new MapBuilder<>(commonFields); checkedFields.remove(LoggingAuditTrail.ORIGIN_ADDRESS_FIELD_NAME); checkedFields.remove(LoggingAuditTrail.ORIGIN_TYPE_FIELD_NAME); checkedFields.put("type", "audit") - .put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, "security_config_change") - .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "delete_role_mapping") - .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); + .put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, "security_config_change") + .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "delete_role_mapping") + .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); assertMsg(generatedDeleteRoleMappingAuditEventString, checkedFields.immutableMap()); } @@ -922,8 +1009,13 @@ public void testSecurityConfigChangeEventFormattingForUsers() throws IOException final Authentication authentication = createAuthentication(); PutUserRequest putUserRequest = new PutUserRequest(); - String username = randomFrom(randomAlphaOfLength(3), customAnonymousUsername, AnonymousUser.DEFAULT_ANONYMOUS_USERNAME, - UsernamesField.ELASTIC_NAME, UsernamesField.KIBANA_NAME); + String username = randomFrom( + randomAlphaOfLength(3), + customAnonymousUsername, + AnonymousUser.DEFAULT_ANONYMOUS_USERNAME, + UsernamesField.ELASTIC_NAME, + UsernamesField.KIBANA_NAME + ); putUserRequest.username(username); putUserRequest.roles(randomFrom(randomArray(4, String[]::new, () -> randomAlphaOfLength(8)), null)); putUserRequest.fullName(randomFrom(randomAlphaOfLength(8), null)); @@ -944,9 +1036,10 @@ public void testSecurityConfigChangeEventFormattingForUsers() throws IOException assertThat(output.size(), is(2)); String generatedPutUserAuditEventString = output.get(1); - StringBuilder putUserAuditEventStringBuilder = new StringBuilder() - .append("\"put\":{\"user\":{\"name\":") - .append("\"").append(putUserRequest.username()).append("\"") + StringBuilder putUserAuditEventStringBuilder = new StringBuilder().append("\"put\":{\"user\":{\"name\":") + .append("\"") + .append(putUserRequest.username()) + .append("\"") .append(",\"enabled\":") .append(putUserRequest.enabled()) .append(",\"roles\":"); @@ -982,9 +1075,9 @@ public void testSecurityConfigChangeEventFormattingForUsers() throws IOException checkedFields.remove(LoggingAuditTrail.ORIGIN_ADDRESS_FIELD_NAME); checkedFields.remove(LoggingAuditTrail.ORIGIN_TYPE_FIELD_NAME); checkedFields.put("type", "audit") - .put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, "security_config_change") - .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "put_user") - .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); + .put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, "security_config_change") + .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "put_user") + .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); assertMsg(generatedPutUserAuditEventString, checkedFields.immutableMap()); // clear log CapturingLogger.output(logger.getName(), Level.INFO).clear(); @@ -1005,9 +1098,9 @@ public void testSecurityConfigChangeEventFormattingForUsers() throws IOException checkedFields.remove(LoggingAuditTrail.ORIGIN_ADDRESS_FIELD_NAME); checkedFields.remove(LoggingAuditTrail.ORIGIN_TYPE_FIELD_NAME); checkedFields.put("type", "audit") - .put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, "security_config_change") - .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "change_enable_user") - .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); + .put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, "security_config_change") + .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "change_enable_user") + .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); assertMsg(generatedEnableUserAuditEventString, checkedFields.immutableMap()); // clear log CapturingLogger.output(logger.getName(), Level.INFO).clear(); @@ -1028,9 +1121,9 @@ public void testSecurityConfigChangeEventFormattingForUsers() throws IOException checkedFields.remove(LoggingAuditTrail.ORIGIN_ADDRESS_FIELD_NAME); checkedFields.remove(LoggingAuditTrail.ORIGIN_TYPE_FIELD_NAME); checkedFields.put("type", "audit") - .put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, "security_config_change") - .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "change_disable_user") - .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); + .put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, "security_config_change") + .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "change_disable_user") + .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); assertMsg(generatedDisableUserAuditEventString, checkedFields.immutableMap()); // clear log CapturingLogger.output(logger.getName(), Level.INFO).clear(); @@ -1045,16 +1138,17 @@ public void testSecurityConfigChangeEventFormattingForUsers() throws IOException String generatedChangePasswordAuditEventString = output.get(1); String expectedChangePasswordAuditEventString = "\"change\":{\"password\":{\"user\":{\"name\":\"" + username + "\"}}}"; assertThat(generatedChangePasswordAuditEventString, containsString(expectedChangePasswordAuditEventString)); - generatedChangePasswordAuditEventString = - generatedChangePasswordAuditEventString.replace(", " + expectedChangePasswordAuditEventString, - ""); + generatedChangePasswordAuditEventString = generatedChangePasswordAuditEventString.replace( + ", " + expectedChangePasswordAuditEventString, + "" + ); checkedFields = new MapBuilder<>(commonFields); checkedFields.remove(LoggingAuditTrail.ORIGIN_ADDRESS_FIELD_NAME); checkedFields.remove(LoggingAuditTrail.ORIGIN_TYPE_FIELD_NAME); checkedFields.put("type", "audit") - .put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, "security_config_change") - .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "change_password") - .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); + .put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, "security_config_change") + .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "change_password") + .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); assertMsg(generatedChangePasswordAuditEventString, checkedFields.immutableMap()); // clear log CapturingLogger.output(logger.getName(), Level.INFO).clear(); @@ -1068,15 +1162,14 @@ public void testSecurityConfigChangeEventFormattingForUsers() throws IOException String generatedDeleteUserAuditEventString = output.get(1); String expectedDeleteUserAuditEventString = "\"delete\":{\"user\":{\"name\":\"" + username + "\"}}"; assertThat(generatedDeleteUserAuditEventString, containsString(expectedDeleteUserAuditEventString)); - generatedDeleteUserAuditEventString = - generatedDeleteUserAuditEventString.replace(", " + expectedDeleteUserAuditEventString,""); + generatedDeleteUserAuditEventString = generatedDeleteUserAuditEventString.replace(", " + expectedDeleteUserAuditEventString, ""); checkedFields = new MapBuilder<>(commonFields); checkedFields.remove(LoggingAuditTrail.ORIGIN_ADDRESS_FIELD_NAME); checkedFields.remove(LoggingAuditTrail.ORIGIN_TYPE_FIELD_NAME); checkedFields.put("type", "audit") - .put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, "security_config_change") - .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "delete_user") - .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); + .put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, "security_config_change") + .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "delete_user") + .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); assertMsg(generatedDeleteUserAuditEventString, checkedFields.immutableMap()); } @@ -1090,21 +1183,34 @@ public void testSecurityConfigChangeEventFormattingForServiceAccountToken() { final String serviceName = randomAlphaOfLengthBetween(3, 8); final String tokenName = randomAlphaOfLengthBetween(3, 8); final CreateServiceAccountTokenRequest createServiceAccountTokenRequest = new CreateServiceAccountTokenRequest( - namespace, serviceName, tokenName); + namespace, + serviceName, + tokenName + ); - auditTrail.accessGranted(requestId, authentication, CreateServiceAccountTokenAction.NAME, - createServiceAccountTokenRequest, authorizationInfo); + auditTrail.accessGranted( + requestId, + authentication, + CreateServiceAccountTokenAction.NAME, + createServiceAccountTokenRequest, + authorizationInfo + ); List output = CapturingLogger.output(logger.getName(), Level.INFO); assertThat(output.size(), is(2)); String generatedCreateServiceAccountTokenAuditEventString = output.get(1); - final String expectedCreateServiceAccountTokenAuditEventString = - String.format(Locale.ROOT, - "\"create\":{\"service_token\":{\"namespace\":\"%s\",\"service\":\"%s\",\"name\":\"%s\"}}", - namespace, serviceName, tokenName); + final String expectedCreateServiceAccountTokenAuditEventString = String.format( + Locale.ROOT, + "\"create\":{\"service_token\":{\"namespace\":\"%s\",\"service\":\"%s\",\"name\":\"%s\"}}", + namespace, + serviceName, + tokenName + ); assertThat(generatedCreateServiceAccountTokenAuditEventString, containsString(expectedCreateServiceAccountTokenAuditEventString)); - generatedCreateServiceAccountTokenAuditEventString = - generatedCreateServiceAccountTokenAuditEventString.replace(", " + expectedCreateServiceAccountTokenAuditEventString, ""); + generatedCreateServiceAccountTokenAuditEventString = generatedCreateServiceAccountTokenAuditEventString.replace( + ", " + expectedCreateServiceAccountTokenAuditEventString, + "" + ); MapBuilder checkedFields = new MapBuilder<>(commonFields); checkedFields.remove(LoggingAuditTrail.ORIGIN_ADDRESS_FIELD_NAME); checkedFields.remove(LoggingAuditTrail.ORIGIN_TYPE_FIELD_NAME); @@ -1116,22 +1222,35 @@ public void testSecurityConfigChangeEventFormattingForServiceAccountToken() { // clear log CapturingLogger.output(logger.getName(), Level.INFO).clear(); - final DeleteServiceAccountTokenRequest deleteServiceAccountTokenRequest = - new DeleteServiceAccountTokenRequest(namespace, serviceName, tokenName); + final DeleteServiceAccountTokenRequest deleteServiceAccountTokenRequest = new DeleteServiceAccountTokenRequest( + namespace, + serviceName, + tokenName + ); - auditTrail.accessGranted(requestId, authentication, DeleteServiceAccountTokenAction.NAME, - deleteServiceAccountTokenRequest, authorizationInfo); + auditTrail.accessGranted( + requestId, + authentication, + DeleteServiceAccountTokenAction.NAME, + deleteServiceAccountTokenRequest, + authorizationInfo + ); output = CapturingLogger.output(logger.getName(), Level.INFO); assertThat(output.size(), is(2)); String generatedDeleteServiceAccountTokenAuditEventString = output.get(1); - final String expectedDeleteServiceAccountTokenAuditEventString = - String.format(Locale.ROOT, - "\"delete\":{\"service_token\":{\"namespace\":\"%s\",\"service\":\"%s\",\"name\":\"%s\"}}", - namespace, serviceName, tokenName); + final String expectedDeleteServiceAccountTokenAuditEventString = String.format( + Locale.ROOT, + "\"delete\":{\"service_token\":{\"namespace\":\"%s\",\"service\":\"%s\",\"name\":\"%s\"}}", + namespace, + serviceName, + tokenName + ); assertThat(generatedDeleteServiceAccountTokenAuditEventString, containsString(expectedDeleteServiceAccountTokenAuditEventString)); - generatedDeleteServiceAccountTokenAuditEventString = - generatedDeleteServiceAccountTokenAuditEventString.replace(", " + expectedDeleteServiceAccountTokenAuditEventString, ""); + generatedDeleteServiceAccountTokenAuditEventString = generatedDeleteServiceAccountTokenAuditEventString.replace( + ", " + expectedDeleteServiceAccountTokenAuditEventString, + "" + ); checkedFields = new MapBuilder<>(commonFields); checkedFields.remove(LoggingAuditTrail.ORIGIN_ADDRESS_FIELD_NAME); checkedFields.remove(LoggingAuditTrail.ORIGIN_TYPE_FIELD_NAME); @@ -1163,17 +1282,18 @@ public void testAnonymousAccessDeniedTransport() throws Exception { // test disabled CapturingLogger.output(logger.getName(), Level.INFO).clear(); - updateLoggerSettings(Settings.builder() - .put(settings) - .put("xpack.security.audit.logfile.events.exclude", "anonymous_access_denied") - .build()); + updateLoggerSettings( + Settings.builder().put(settings).put("xpack.security.audit.logfile.events.exclude", "anonymous_access_denied").build() + ); auditTrail.anonymousAccessDenied(requestId, "_action", request); assertEmptyLog(logger); } public void testAnonymousAccessDeniedRest() throws Exception { - final InetSocketAddress address = new InetSocketAddress(forge("_hostname", randomBoolean() ? "127.0.0.1" : "::1"), - randomIntBetween(9200, 9300)); + final InetSocketAddress address = new InetSocketAddress( + forge("_hostname", randomBoolean() ? "127.0.0.1" : "::1"), + randomIntBetween(9200, 9300) + ); final Tuple tuple = prepareRestContent("_uri", address); final String expectedMessage = tuple.v1().expectedMessage(); final RestRequest request = tuple.v2(); @@ -1182,12 +1302,12 @@ public void testAnonymousAccessDeniedRest() throws Exception { auditTrail.anonymousAccessDenied(requestId, request); final MapBuilder checkedFields = new MapBuilder<>(commonFields); checkedFields.put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, LoggingAuditTrail.REST_ORIGIN_FIELD_VALUE) - .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "anonymous_access_denied") - .put(LoggingAuditTrail.ORIGIN_TYPE_FIELD_NAME, LoggingAuditTrail.REST_ORIGIN_FIELD_VALUE) - .put(LoggingAuditTrail.ORIGIN_ADDRESS_FIELD_NAME, NetworkAddress.format(address)) - .put(LoggingAuditTrail.REQUEST_METHOD_FIELD_NAME, request.method().toString()) - .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId) - .put(LoggingAuditTrail.URL_PATH_FIELD_NAME, "_uri"); + .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "anonymous_access_denied") + .put(LoggingAuditTrail.ORIGIN_TYPE_FIELD_NAME, LoggingAuditTrail.REST_ORIGIN_FIELD_VALUE) + .put(LoggingAuditTrail.ORIGIN_ADDRESS_FIELD_NAME, NetworkAddress.format(address)) + .put(LoggingAuditTrail.REQUEST_METHOD_FIELD_NAME, request.method().toString()) + .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId) + .put(LoggingAuditTrail.URL_PATH_FIELD_NAME, "_uri"); if (includeRequestBody && Strings.hasLength(expectedMessage)) { checkedFields.put(LoggingAuditTrail.REQUEST_BODY_FIELD_NAME, expectedMessage); } @@ -1197,10 +1317,9 @@ public void testAnonymousAccessDeniedRest() throws Exception { // test disabled CapturingLogger.output(logger.getName(), Level.INFO).clear(); - updateLoggerSettings(Settings.builder() - .put(settings) - .put("xpack.security.audit.logfile.events.exclude", "anonymous_access_denied") - .build()); + updateLoggerSettings( + Settings.builder().put(settings).put("xpack.security.audit.logfile.events.exclude", "anonymous_access_denied").build() + ); auditTrail.anonymousAccessDenied(requestId, request); assertEmptyLog(logger); } @@ -1214,11 +1333,11 @@ public void testAuthenticationFailed() throws Exception { final MapBuilder checkedArrayFields = new MapBuilder<>(); final MapBuilder checkedFields = new MapBuilder<>(commonFields); checkedFields.put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, LoggingAuditTrail.TRANSPORT_ORIGIN_FIELD_VALUE) - .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "authentication_failed") - .put(LoggingAuditTrail.ACTION_FIELD_NAME, "_action") - .put(LoggingAuditTrail.PRINCIPAL_FIELD_NAME, authToken.principal()) - .put(LoggingAuditTrail.REQUEST_NAME_FIELD_NAME, request.getClass().getSimpleName()) - .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); + .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "authentication_failed") + .put(LoggingAuditTrail.ACTION_FIELD_NAME, "_action") + .put(LoggingAuditTrail.PRINCIPAL_FIELD_NAME, authToken.principal()) + .put(LoggingAuditTrail.REQUEST_NAME_FIELD_NAME, request.getClass().getSimpleName()) + .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); if (authToken instanceof ServiceAccountToken) { checkedFields.put(LoggingAuditTrail.SERVICE_TOKEN_NAME_FIELD_NAME, ((ServiceAccountToken) authToken).getTokenName()); } @@ -1230,10 +1349,9 @@ public void testAuthenticationFailed() throws Exception { // test disabled CapturingLogger.output(logger.getName(), Level.INFO).clear(); - updateLoggerSettings(Settings.builder() - .put(settings) - .put("xpack.security.audit.logfile.events.exclude", "authentication_failed") - .build()); + updateLoggerSettings( + Settings.builder().put(settings).put("xpack.security.audit.logfile.events.exclude", "authentication_failed").build() + ); auditTrail.authenticationFailed(requestId, createAuthenticationToken(), "_action", request); assertEmptyLog(logger); } @@ -1246,10 +1364,10 @@ public void testAuthenticationFailedNoToken() throws Exception { final MapBuilder checkedFields = new MapBuilder<>(commonFields); final MapBuilder checkedArrayFields = new MapBuilder<>(); checkedFields.put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, LoggingAuditTrail.TRANSPORT_ORIGIN_FIELD_VALUE) - .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "authentication_failed") - .put(LoggingAuditTrail.ACTION_FIELD_NAME, "_action") - .put(LoggingAuditTrail.REQUEST_NAME_FIELD_NAME, request.getClass().getSimpleName()) - .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); + .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "authentication_failed") + .put(LoggingAuditTrail.ACTION_FIELD_NAME, "_action") + .put(LoggingAuditTrail.REQUEST_NAME_FIELD_NAME, request.getClass().getSimpleName()) + .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); restOrTransportOrigin(request, threadContext, checkedFields); indicesRequest(request, checkedFields, checkedArrayFields); opaqueId(threadContext, checkedFields); @@ -1258,10 +1376,9 @@ public void testAuthenticationFailedNoToken() throws Exception { // test disabled CapturingLogger.output(logger.getName(), Level.INFO).clear(); - updateLoggerSettings(Settings.builder() - .put(settings) - .put("xpack.security.audit.logfile.events.exclude", "authentication_failed") - .build()); + updateLoggerSettings( + Settings.builder().put(settings).put("xpack.security.audit.logfile.events.exclude", "authentication_failed").build() + ); auditTrail.authenticationFailed(requestId, "_action", request); assertEmptyLog(logger); } @@ -1271,8 +1388,10 @@ public void testAuthenticationFailedRest() throws Exception { if (randomBoolean()) { params.put("foo", "bar"); } - final InetSocketAddress address = new InetSocketAddress(forge("_hostname", randomBoolean() ? "127.0.0.1" : "::1"), - randomIntBetween(9200, 9300)); + final InetSocketAddress address = new InetSocketAddress( + forge("_hostname", randomBoolean() ? "127.0.0.1" : "::1"), + randomIntBetween(9200, 9300) + ); final Tuple tuple = prepareRestContent("_uri", address, params); final String expectedMessage = tuple.v1().expectedMessage(); final RestRequest request = tuple.v2(); @@ -1282,13 +1401,13 @@ public void testAuthenticationFailedRest() throws Exception { auditTrail.authenticationFailed(requestId, authToken, request); final MapBuilder checkedFields = new MapBuilder<>(commonFields); checkedFields.put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, LoggingAuditTrail.REST_ORIGIN_FIELD_VALUE) - .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "authentication_failed") - .put(LoggingAuditTrail.PRINCIPAL_FIELD_NAME, authToken.principal()) - .put(LoggingAuditTrail.ORIGIN_TYPE_FIELD_NAME, LoggingAuditTrail.REST_ORIGIN_FIELD_VALUE) - .put(LoggingAuditTrail.ORIGIN_ADDRESS_FIELD_NAME, NetworkAddress.format(address)) - .put(LoggingAuditTrail.REQUEST_METHOD_FIELD_NAME, request.method().toString()) - .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId) - .put(LoggingAuditTrail.URL_PATH_FIELD_NAME, "_uri"); + .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "authentication_failed") + .put(LoggingAuditTrail.PRINCIPAL_FIELD_NAME, authToken.principal()) + .put(LoggingAuditTrail.ORIGIN_TYPE_FIELD_NAME, LoggingAuditTrail.REST_ORIGIN_FIELD_VALUE) + .put(LoggingAuditTrail.ORIGIN_ADDRESS_FIELD_NAME, NetworkAddress.format(address)) + .put(LoggingAuditTrail.REQUEST_METHOD_FIELD_NAME, request.method().toString()) + .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId) + .put(LoggingAuditTrail.URL_PATH_FIELD_NAME, "_uri"); if (authToken instanceof ServiceAccountToken) { checkedFields.put(LoggingAuditTrail.SERVICE_TOKEN_NAME_FIELD_NAME, ((ServiceAccountToken) authToken).getTokenName()); } @@ -1304,10 +1423,9 @@ public void testAuthenticationFailedRest() throws Exception { // test disabled CapturingLogger.output(logger.getName(), Level.INFO).clear(); - updateLoggerSettings(Settings.builder() - .put(settings) - .put("xpack.security.audit.logfile.events.exclude", "authentication_failed") - .build()); + updateLoggerSettings( + Settings.builder().put(settings).put("xpack.security.audit.logfile.events.exclude", "authentication_failed").build() + ); auditTrail.authenticationFailed(requestId, createAuthenticationToken(), request); assertEmptyLog(logger); } @@ -1317,8 +1435,10 @@ public void testAuthenticationFailedRestNoToken() throws Exception { if (randomBoolean()) { params.put("bar", "baz"); } - final InetSocketAddress address = new InetSocketAddress(forge("_hostname", randomBoolean() ? "127.0.0.1" : "::1"), - randomIntBetween(9200, 9300)); + final InetSocketAddress address = new InetSocketAddress( + forge("_hostname", randomBoolean() ? "127.0.0.1" : "::1"), + randomIntBetween(9200, 9300) + ); final Tuple tuple = prepareRestContent("_uri", address, params); final String expectedMessage = tuple.v1().expectedMessage(); final RestRequest request = tuple.v2(); @@ -1327,12 +1447,12 @@ public void testAuthenticationFailedRestNoToken() throws Exception { auditTrail.authenticationFailed(requestId, request); final MapBuilder checkedFields = new MapBuilder<>(commonFields); checkedFields.put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, LoggingAuditTrail.REST_ORIGIN_FIELD_VALUE) - .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "authentication_failed") - .put(LoggingAuditTrail.ORIGIN_TYPE_FIELD_NAME, LoggingAuditTrail.REST_ORIGIN_FIELD_VALUE) - .put(LoggingAuditTrail.ORIGIN_ADDRESS_FIELD_NAME, NetworkAddress.format(address)) - .put(LoggingAuditTrail.REQUEST_METHOD_FIELD_NAME, request.method().toString()) - .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId) - .put(LoggingAuditTrail.URL_PATH_FIELD_NAME, "_uri"); + .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "authentication_failed") + .put(LoggingAuditTrail.ORIGIN_TYPE_FIELD_NAME, LoggingAuditTrail.REST_ORIGIN_FIELD_VALUE) + .put(LoggingAuditTrail.ORIGIN_ADDRESS_FIELD_NAME, NetworkAddress.format(address)) + .put(LoggingAuditTrail.REQUEST_METHOD_FIELD_NAME, request.method().toString()) + .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId) + .put(LoggingAuditTrail.URL_PATH_FIELD_NAME, "_uri"); if (includeRequestBody && Strings.hasLength(expectedMessage)) { checkedFields.put(LoggingAuditTrail.REQUEST_BODY_FIELD_NAME, expectedMessage); } @@ -1345,10 +1465,9 @@ public void testAuthenticationFailedRestNoToken() throws Exception { // test disabled CapturingLogger.output(logger.getName(), Level.INFO).clear(); - updateLoggerSettings(Settings.builder() - .put(settings) - .put("xpack.security.audit.logfile.events.exclude", "authentication_failed") - .build()); + updateLoggerSettings( + Settings.builder().put(settings).put("xpack.security.audit.logfile.events.exclude", "authentication_failed").build() + ); auditTrail.authenticationFailed(requestId, request); assertEmptyLog(logger); } @@ -1362,20 +1481,19 @@ public void testAuthenticationFailedRealm() throws Exception { assertEmptyLog(logger); // test enabled - updateLoggerSettings(Settings.builder() - .put(settings) - .put("xpack.security.audit.logfile.events.include", "realm_authentication_failed") - .build()); + updateLoggerSettings( + Settings.builder().put(settings).put("xpack.security.audit.logfile.events.include", "realm_authentication_failed").build() + ); auditTrail.authenticationFailed(requestId, realm, authToken, "_action", request); final MapBuilder checkedFields = new MapBuilder<>(commonFields); final MapBuilder checkedArrayFields = new MapBuilder<>(); checkedFields.put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, LoggingAuditTrail.TRANSPORT_ORIGIN_FIELD_VALUE) - .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "realm_authentication_failed") - .put(LoggingAuditTrail.REALM_FIELD_NAME, realm) - .put(LoggingAuditTrail.PRINCIPAL_FIELD_NAME, authToken.principal()) - .put(LoggingAuditTrail.ACTION_FIELD_NAME, "_action") - .put(LoggingAuditTrail.REQUEST_NAME_FIELD_NAME, request.getClass().getSimpleName()) - .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); + .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "realm_authentication_failed") + .put(LoggingAuditTrail.REALM_FIELD_NAME, realm) + .put(LoggingAuditTrail.PRINCIPAL_FIELD_NAME, authToken.principal()) + .put(LoggingAuditTrail.ACTION_FIELD_NAME, "_action") + .put(LoggingAuditTrail.REQUEST_NAME_FIELD_NAME, request.getClass().getSimpleName()) + .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); restOrTransportOrigin(request, threadContext, checkedFields); indicesRequest(request, checkedFields, checkedArrayFields); opaqueId(threadContext, checkedFields); @@ -1388,8 +1506,10 @@ public void testAuthenticationFailedRealmRest() throws Exception { if (randomBoolean()) { params.put("_param", "baz"); } - final InetSocketAddress address = new InetSocketAddress(forge("_hostname", randomBoolean() ? "127.0.0.1" : "::1"), - randomIntBetween(9200, 9300)); + final InetSocketAddress address = new InetSocketAddress( + forge("_hostname", randomBoolean() ? "127.0.0.1" : "::1"), + randomIntBetween(9200, 9300) + ); final Tuple tuple = prepareRestContent("_uri", address, params); final String expectedMessage = tuple.v1().expectedMessage(); final RestRequest request = tuple.v2(); @@ -1400,21 +1520,20 @@ public void testAuthenticationFailedRealmRest() throws Exception { assertEmptyLog(logger); // test enabled - updateLoggerSettings(Settings.builder() - .put(settings) - .put("xpack.security.audit.logfile.events.include", "realm_authentication_failed") - .build()); + updateLoggerSettings( + Settings.builder().put(settings).put("xpack.security.audit.logfile.events.include", "realm_authentication_failed").build() + ); auditTrail.authenticationFailed(requestId, realm, authToken, request); final MapBuilder checkedFields = new MapBuilder<>(commonFields); checkedFields.put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, LoggingAuditTrail.REST_ORIGIN_FIELD_VALUE) - .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "realm_authentication_failed") - .put(LoggingAuditTrail.REALM_FIELD_NAME, realm) - .put(LoggingAuditTrail.ORIGIN_TYPE_FIELD_NAME, LoggingAuditTrail.REST_ORIGIN_FIELD_VALUE) - .put(LoggingAuditTrail.ORIGIN_ADDRESS_FIELD_NAME, NetworkAddress.format(address)) - .put(LoggingAuditTrail.PRINCIPAL_FIELD_NAME, authToken.principal()) - .put(LoggingAuditTrail.REQUEST_METHOD_FIELD_NAME, request.method().toString()) - .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId) - .put(LoggingAuditTrail.URL_PATH_FIELD_NAME, "_uri"); + .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "realm_authentication_failed") + .put(LoggingAuditTrail.REALM_FIELD_NAME, realm) + .put(LoggingAuditTrail.ORIGIN_TYPE_FIELD_NAME, LoggingAuditTrail.REST_ORIGIN_FIELD_VALUE) + .put(LoggingAuditTrail.ORIGIN_ADDRESS_FIELD_NAME, NetworkAddress.format(address)) + .put(LoggingAuditTrail.PRINCIPAL_FIELD_NAME, authToken.principal()) + .put(LoggingAuditTrail.REQUEST_METHOD_FIELD_NAME, request.method().toString()) + .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId) + .put(LoggingAuditTrail.URL_PATH_FIELD_NAME, "_uri"); if (includeRequestBody && Strings.hasLength(expectedMessage)) { checkedFields.put(LoggingAuditTrail.REQUEST_BODY_FIELD_NAME, expectedMessage); } @@ -1437,10 +1556,10 @@ public void testAccessGranted() throws Exception { Authentication authentication = createAuthentication(); auditTrail.accessGranted(requestId, authentication, "_action", request, authorizationInfo); checkedFields.put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, LoggingAuditTrail.TRANSPORT_ORIGIN_FIELD_VALUE) - .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "access_granted") - .put(LoggingAuditTrail.ACTION_FIELD_NAME, "_action") - .put(LoggingAuditTrail.REQUEST_NAME_FIELD_NAME, request.getClass().getSimpleName()) - .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); + .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "access_granted") + .put(LoggingAuditTrail.ACTION_FIELD_NAME, "_action") + .put(LoggingAuditTrail.REQUEST_NAME_FIELD_NAME, request.getClass().getSimpleName()) + .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); checkedArrayFields.put(PRINCIPAL_ROLES_FIELD_NAME, (String[]) authorizationInfo.asMap().get(PRINCIPAL_ROLES_FIELD_NAME)); authentication(authentication, checkedFields); @@ -1458,10 +1577,10 @@ public void testAccessGranted() throws Exception { checkedArrayFields = new MapBuilder<>(); auditTrail.accessGranted(requestId, authentication, "_action", request, authorizationInfo); checkedFields.put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, LoggingAuditTrail.TRANSPORT_ORIGIN_FIELD_VALUE) - .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "access_granted") - .put(LoggingAuditTrail.ACTION_FIELD_NAME, "_action") - .put(LoggingAuditTrail.REQUEST_NAME_FIELD_NAME, request.getClass().getSimpleName()) - .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); + .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "access_granted") + .put(LoggingAuditTrail.ACTION_FIELD_NAME, "_action") + .put(LoggingAuditTrail.REQUEST_NAME_FIELD_NAME, request.getClass().getSimpleName()) + .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); checkedArrayFields.put(PRINCIPAL_ROLES_FIELD_NAME, (String[]) authorizationInfo.asMap().get(PRINCIPAL_ROLES_FIELD_NAME)); authentication(authentication, checkedFields); restOrTransportOrigin(request, threadContext, checkedFields); @@ -1472,10 +1591,7 @@ public void testAccessGranted() throws Exception { // test disabled CapturingLogger.output(logger.getName(), Level.INFO).clear(); - updateLoggerSettings(Settings.builder() - .put(settings) - .put("xpack.security.audit.logfile.events.exclude", "access_granted") - .build()); + updateLoggerSettings(Settings.builder().put(settings).put("xpack.security.audit.logfile.events.exclude", "access_granted").build()); auditTrail.accessGranted(requestId, authentication, "_action", request, authorizationInfo); assertEmptyLog(logger); } @@ -1488,41 +1604,43 @@ public void testSecurityConfigChangedEventSelection() { final String namespace = randomAlphaOfLengthBetween(3, 8); final String serviceName = randomAlphaOfLengthBetween(3, 8); final String tokenName = randomAlphaOfLengthBetween(3, 8); - Tuple actionAndRequest = randomFrom(new Tuple<>(PutUserAction.NAME, new PutUserRequest()), - new Tuple<>(PutRoleAction.NAME, new PutRoleRequest()), - new Tuple<>(PutRoleMappingAction.NAME, new PutRoleMappingRequest()), - new Tuple<>(SetEnabledAction.NAME, new SetEnabledRequest()), - new Tuple<>(ChangePasswordAction.NAME, new ChangePasswordRequest()), - new Tuple<>(CreateApiKeyAction.NAME, new CreateApiKeyRequest()), - new Tuple<>(GrantApiKeyAction.NAME, new GrantApiKeyRequest()), - new Tuple<>(PutPrivilegesAction.NAME, new PutPrivilegesRequest()), - new Tuple<>(DeleteUserAction.NAME, new DeleteUserRequest()), - new Tuple<>(DeleteRoleAction.NAME, new DeleteRoleRequest()), - new Tuple<>(DeleteRoleMappingAction.NAME, new DeleteRoleMappingRequest()), - new Tuple<>(InvalidateApiKeyAction.NAME, new InvalidateApiKeyRequest()), - new Tuple<>(DeletePrivilegesAction.NAME, new DeletePrivilegesRequest()), - new Tuple<>(CreateServiceAccountTokenAction.NAME, new CreateServiceAccountTokenRequest(namespace, serviceName, tokenName)), - new Tuple<>(DeleteServiceAccountTokenAction.NAME, new DeleteServiceAccountTokenRequest(namespace, serviceName, tokenName)) + Tuple actionAndRequest = randomFrom( + new Tuple<>(PutUserAction.NAME, new PutUserRequest()), + new Tuple<>(PutRoleAction.NAME, new PutRoleRequest()), + new Tuple<>(PutRoleMappingAction.NAME, new PutRoleMappingRequest()), + new Tuple<>(SetEnabledAction.NAME, new SetEnabledRequest()), + new Tuple<>(ChangePasswordAction.NAME, new ChangePasswordRequest()), + new Tuple<>(CreateApiKeyAction.NAME, new CreateApiKeyRequest()), + new Tuple<>(GrantApiKeyAction.NAME, new GrantApiKeyRequest()), + new Tuple<>(PutPrivilegesAction.NAME, new PutPrivilegesRequest()), + new Tuple<>(DeleteUserAction.NAME, new DeleteUserRequest()), + new Tuple<>(DeleteRoleAction.NAME, new DeleteRoleRequest()), + new Tuple<>(DeleteRoleMappingAction.NAME, new DeleteRoleMappingRequest()), + new Tuple<>(InvalidateApiKeyAction.NAME, new InvalidateApiKeyRequest()), + new Tuple<>(DeletePrivilegesAction.NAME, new DeletePrivilegesRequest()), + new Tuple<>(CreateServiceAccountTokenAction.NAME, new CreateServiceAccountTokenRequest(namespace, serviceName, tokenName)), + new Tuple<>(DeleteServiceAccountTokenAction.NAME, new DeleteServiceAccountTokenRequest(namespace, serviceName, tokenName)) ); auditTrail.accessGranted(requestId, authentication, actionAndRequest.v1(), actionAndRequest.v2(), authorizationInfo); List output = CapturingLogger.output(logger.getName(), Level.INFO); assertThat(output.size(), is(2)); assertThat(output.get(1), containsString("security_config_change")); CapturingLogger.output(logger.getName(), Level.INFO).clear(); - updateLoggerSettings(Settings.builder() - .put(settings) - .put("xpack.security.audit.logfile.events.exclude", "security_config_change") - .build()); + updateLoggerSettings( + Settings.builder().put(settings).put("xpack.security.audit.logfile.events.exclude", "security_config_change").build() + ); auditTrail.accessGranted(requestId, authentication, actionAndRequest.v1(), actionAndRequest.v2(), authorizationInfo); output = CapturingLogger.output(logger.getName(), Level.INFO); assertThat(output.size(), is(1)); assertThat(output.get(0), not(containsString("security_config_change"))); CapturingLogger.output(logger.getName(), Level.INFO).clear(); - updateLoggerSettings(Settings.builder() + updateLoggerSettings( + Settings.builder() .put(settings) .put("xpack.security.audit.logfile.events.include", "security_config_change") .put("xpack.security.audit.logfile.events.exclude", "access_granted") - .build()); + .build() + ); auditTrail.accessGranted(requestId, authentication, actionAndRequest.v1(), actionAndRequest.v2(), authorizationInfo); output = CapturingLogger.output(logger.getName(), Level.INFO); assertThat(output.size(), is(1)); @@ -1540,29 +1658,33 @@ public void testSystemAccessGranted() throws Exception { auditTrail.accessGranted(requestId, authentication, "_action", request, authorizationInfo); // system user assertEmptyLog(logger); - auditTrail.explicitIndexAccessEvent(requestId, randomFrom(AuditLevel.ACCESS_GRANTED, AuditLevel.SYSTEM_ACCESS_GRANTED), - authentication, "_action", randomFrom(randomAlphaOfLengthBetween(1, 4), null), - BulkItemRequest.class.getName(), - request.remoteAddress(), - authorizationInfo); + auditTrail.explicitIndexAccessEvent( + requestId, + randomFrom(AuditLevel.ACCESS_GRANTED, AuditLevel.SYSTEM_ACCESS_GRANTED), + authentication, + "_action", + randomFrom(randomAlphaOfLengthBetween(1, 4), null), + BulkItemRequest.class.getName(), + request.remoteAddress(), + authorizationInfo + ); // system user assertEmptyLog(logger); // enable system user for access granted events - updateLoggerSettings(Settings.builder() - .put(settings) - .put("xpack.security.audit.logfile.events.include", "system_access_granted") - .build()); + updateLoggerSettings( + Settings.builder().put(settings).put("xpack.security.audit.logfile.events.include", "system_access_granted").build() + ); auditTrail.accessGranted(requestId, authentication, "_action", request, authorizationInfo); MapBuilder checkedFields = new MapBuilder<>(commonFields); MapBuilder checkedArrayFields = new MapBuilder<>(); checkedFields.put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, LoggingAuditTrail.TRANSPORT_ORIGIN_FIELD_VALUE) - .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "access_granted") - .put(LoggingAuditTrail.ACTION_FIELD_NAME, "_action") - .put(LoggingAuditTrail.REQUEST_NAME_FIELD_NAME, request.getClass().getSimpleName()) - .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); + .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "access_granted") + .put(LoggingAuditTrail.ACTION_FIELD_NAME, "_action") + .put(LoggingAuditTrail.REQUEST_NAME_FIELD_NAME, request.getClass().getSimpleName()) + .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); checkedArrayFields.put(PRINCIPAL_ROLES_FIELD_NAME, (String[]) authorizationInfo.asMap().get(PRINCIPAL_ROLES_FIELD_NAME)); authentication(authentication, checkedFields); restOrTransportOrigin(request, threadContext, checkedFields); @@ -1573,23 +1695,31 @@ public void testSystemAccessGranted() throws Exception { clearLog(); String index = randomFrom(randomAlphaOfLengthBetween(1, 4), null); - auditTrail.explicitIndexAccessEvent(requestId, randomFrom(AuditLevel.ACCESS_GRANTED, AuditLevel.SYSTEM_ACCESS_GRANTED), - authentication, "_action", index, BulkItemRequest.class.getName(), request.remoteAddress(), authorizationInfo); + auditTrail.explicitIndexAccessEvent( + requestId, + randomFrom(AuditLevel.ACCESS_GRANTED, AuditLevel.SYSTEM_ACCESS_GRANTED), + authentication, + "_action", + index, + BulkItemRequest.class.getName(), + request.remoteAddress(), + authorizationInfo + ); checkedFields = new MapBuilder<>(commonFields); checkedArrayFields = new MapBuilder<>(); checkedFields.put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, LoggingAuditTrail.TRANSPORT_ORIGIN_FIELD_VALUE) - .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "access_granted") - .put(LoggingAuditTrail.ACTION_FIELD_NAME, "_action") - .put(LoggingAuditTrail.REQUEST_NAME_FIELD_NAME, BulkItemRequest.class.getName()) - .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); + .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "access_granted") + .put(LoggingAuditTrail.ACTION_FIELD_NAME, "_action") + .put(LoggingAuditTrail.REQUEST_NAME_FIELD_NAME, BulkItemRequest.class.getName()) + .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); checkedArrayFields.put(PRINCIPAL_ROLES_FIELD_NAME, (String[]) authorizationInfo.asMap().get(PRINCIPAL_ROLES_FIELD_NAME)); authentication(authentication, checkedFields); restOrTransportOrigin(request, threadContext, checkedFields); opaqueId(threadContext, checkedFields); forwardedFor(threadContext, checkedFields); if (index != null) { - checkedArrayFields.put(LoggingAuditTrail.INDICES_FIELD_NAME, new String[]{index}); + checkedArrayFields.put(LoggingAuditTrail.INDICES_FIELD_NAME, new String[] { index }); } assertMsg(logger, checkedFields.immutableMap(), checkedArrayFields.immutableMap()); } @@ -1605,21 +1735,20 @@ public void testAccessGrantedInternalSystemAction() throws Exception { assertEmptyLog(logger); // test enabled - updateLoggerSettings(Settings.builder() - .put(settings) - .put("xpack.security.audit.logfile.events.include", "system_access_granted") - .build()); + updateLoggerSettings( + Settings.builder().put(settings).put("xpack.security.audit.logfile.events.include", "system_access_granted").build() + ); auditTrail.accessGranted(requestId, authentication, "internal:_action", request, authorizationInfo); final MapBuilder checkedFields = new MapBuilder<>(commonFields); final MapBuilder checkedArrayFields = new MapBuilder<>(); checkedFields.put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, LoggingAuditTrail.TRANSPORT_ORIGIN_FIELD_VALUE) - .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "access_granted") - .put(LoggingAuditTrail.AUTHENTICATION_TYPE_FIELD_NAME, AuthenticationType.REALM.toString()) - .put(LoggingAuditTrail.PRINCIPAL_FIELD_NAME, systemUser.principal()) - .put(LoggingAuditTrail.PRINCIPAL_REALM_FIELD_NAME, "_reserved") - .put(LoggingAuditTrail.ACTION_FIELD_NAME, "internal:_action") - .put(LoggingAuditTrail.REQUEST_NAME_FIELD_NAME, request.getClass().getSimpleName()) - .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); + .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "access_granted") + .put(LoggingAuditTrail.AUTHENTICATION_TYPE_FIELD_NAME, AuthenticationType.REALM.toString()) + .put(LoggingAuditTrail.PRINCIPAL_FIELD_NAME, systemUser.principal()) + .put(LoggingAuditTrail.PRINCIPAL_REALM_FIELD_NAME, "_reserved") + .put(LoggingAuditTrail.ACTION_FIELD_NAME, "internal:_action") + .put(LoggingAuditTrail.REQUEST_NAME_FIELD_NAME, request.getClass().getSimpleName()) + .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); checkedArrayFields.put(PRINCIPAL_ROLES_FIELD_NAME, (String[]) authorizationInfo.asMap().get(PRINCIPAL_ROLES_FIELD_NAME)); restOrTransportOrigin(request, threadContext, checkedFields); indicesRequest(request, checkedFields, checkedArrayFields); @@ -1659,10 +1788,10 @@ public void testAccessGrantedInternalSystemActionNonSystemUser() throws Exceptio checkedArrayFields = new MapBuilder<>(); auditTrail.accessGranted(requestId, authentication, "internal:_action", request, authorizationInfo); checkedFields.put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, LoggingAuditTrail.TRANSPORT_ORIGIN_FIELD_VALUE) - .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "access_granted") - .put(LoggingAuditTrail.ACTION_FIELD_NAME, "internal:_action") - .put(LoggingAuditTrail.REQUEST_NAME_FIELD_NAME, request.getClass().getSimpleName()) - .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); + .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "access_granted") + .put(LoggingAuditTrail.ACTION_FIELD_NAME, "internal:_action") + .put(LoggingAuditTrail.REQUEST_NAME_FIELD_NAME, request.getClass().getSimpleName()) + .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); checkedArrayFields.put(PRINCIPAL_ROLES_FIELD_NAME, (String[]) authorizationInfo.asMap().get(PRINCIPAL_ROLES_FIELD_NAME)); authentication(authentication, checkedFields); restOrTransportOrigin(request, threadContext, checkedFields); @@ -1673,10 +1802,7 @@ public void testAccessGrantedInternalSystemActionNonSystemUser() throws Exceptio // test disabled CapturingLogger.output(logger.getName(), Level.INFO).clear(); - updateLoggerSettings(Settings.builder() - .put(settings) - .put("xpack.security.audit.logfile.events.exclude", "access_granted") - .build()); + updateLoggerSettings(Settings.builder().put(settings).put("xpack.security.audit.logfile.events.exclude", "access_granted").build()); auditTrail.accessGranted(requestId, authentication, "internal:_action", request, authorizationInfo); assertEmptyLog(logger); } @@ -1692,14 +1818,16 @@ public void testAccessDenied() throws Exception { Authentication authentication = createAuthentication(); auditTrail.accessDenied(requestId, authentication, "_action/bar", request, authorizationInfo); checkedFields.put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, LoggingAuditTrail.TRANSPORT_ORIGIN_FIELD_VALUE) - .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "access_denied") - .put(LoggingAuditTrail.ACTION_FIELD_NAME, "_action/bar") - .put(LoggingAuditTrail.REQUEST_NAME_FIELD_NAME, request.getClass().getSimpleName()) - .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); + .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "access_denied") + .put(LoggingAuditTrail.ACTION_FIELD_NAME, "_action/bar") + .put(LoggingAuditTrail.REQUEST_NAME_FIELD_NAME, request.getClass().getSimpleName()) + .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); if (authentication.isServiceAccount()) { checkedFields.put(LoggingAuditTrail.SERVICE_TOKEN_NAME_FIELD_NAME, (String) authentication.getMetadata().get(TOKEN_NAME_FIELD)) - .put(LoggingAuditTrail.SERVICE_TOKEN_TYPE_FIELD_NAME, - ServiceAccountSettings.REALM_TYPE + "_" + authentication.getMetadata().get(TOKEN_SOURCE_FIELD)); + .put( + LoggingAuditTrail.SERVICE_TOKEN_TYPE_FIELD_NAME, + ServiceAccountSettings.REALM_TYPE + "_" + authentication.getMetadata().get(TOKEN_SOURCE_FIELD) + ); } checkedArrayFields.put(PRINCIPAL_ROLES_FIELD_NAME, (String[]) authorizationInfo.asMap().get(PRINCIPAL_ROLES_FIELD_NAME)); authentication(authentication, checkedFields); @@ -1717,10 +1845,10 @@ public void testAccessDenied() throws Exception { checkedArrayFields = new MapBuilder<>(); auditTrail.accessDenied(requestId, authentication, "_action/bar", request, authorizationInfo); checkedFields.put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, LoggingAuditTrail.TRANSPORT_ORIGIN_FIELD_VALUE) - .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "access_denied") - .put(LoggingAuditTrail.ACTION_FIELD_NAME, "_action/bar") - .put(LoggingAuditTrail.REQUEST_NAME_FIELD_NAME, request.getClass().getSimpleName()) - .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); + .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "access_denied") + .put(LoggingAuditTrail.ACTION_FIELD_NAME, "_action/bar") + .put(LoggingAuditTrail.REQUEST_NAME_FIELD_NAME, request.getClass().getSimpleName()) + .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); checkedArrayFields.put(PRINCIPAL_ROLES_FIELD_NAME, (String[]) authorizationInfo.asMap().get(PRINCIPAL_ROLES_FIELD_NAME)); authentication(authentication, checkedFields); restOrTransportOrigin(request, threadContext, checkedFields); @@ -1731,10 +1859,7 @@ public void testAccessDenied() throws Exception { // test disabled CapturingLogger.output(logger.getName(), Level.INFO).clear(); - updateLoggerSettings(Settings.builder() - .put(settings) - .put("xpack.security.audit.logfile.events.exclude", "access_denied") - .build()); + updateLoggerSettings(Settings.builder().put(settings).put("xpack.security.audit.logfile.events.exclude", "access_denied").build()); auditTrail.accessDenied(requestId, authentication, "_action", request, authorizationInfo); assertEmptyLog(logger); } @@ -1744,8 +1869,10 @@ public void testTamperedRequestRest() throws Exception { if (randomBoolean()) { params.put("_param", "baz"); } - final InetSocketAddress address = new InetSocketAddress(forge("_hostname", randomBoolean() ? "127.0.0.1" : "::1"), - randomIntBetween(9200, 9300)); + final InetSocketAddress address = new InetSocketAddress( + forge("_hostname", randomBoolean() ? "127.0.0.1" : "::1"), + randomIntBetween(9200, 9300) + ); final Tuple tuple = prepareRestContent("_uri", address, params); final String expectedMessage = tuple.v1().expectedMessage(); final RestRequest request = tuple.v2(); @@ -1753,12 +1880,12 @@ public void testTamperedRequestRest() throws Exception { auditTrail.tamperedRequest(requestId, request); final MapBuilder checkedFields = new MapBuilder<>(commonFields); checkedFields.put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, LoggingAuditTrail.REST_ORIGIN_FIELD_VALUE) - .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "tampered_request") - .put(LoggingAuditTrail.ORIGIN_TYPE_FIELD_NAME, LoggingAuditTrail.REST_ORIGIN_FIELD_VALUE) - .put(LoggingAuditTrail.ORIGIN_ADDRESS_FIELD_NAME, NetworkAddress.format(address)) - .put(LoggingAuditTrail.REQUEST_METHOD_FIELD_NAME, request.method().toString()) - .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId) - .put(LoggingAuditTrail.URL_PATH_FIELD_NAME, "_uri"); + .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "tampered_request") + .put(LoggingAuditTrail.ORIGIN_TYPE_FIELD_NAME, LoggingAuditTrail.REST_ORIGIN_FIELD_VALUE) + .put(LoggingAuditTrail.ORIGIN_ADDRESS_FIELD_NAME, NetworkAddress.format(address)) + .put(LoggingAuditTrail.REQUEST_METHOD_FIELD_NAME, request.method().toString()) + .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId) + .put(LoggingAuditTrail.URL_PATH_FIELD_NAME, "_uri"); if (includeRequestBody && Strings.hasLength(expectedMessage)) { checkedFields.put(LoggingAuditTrail.REQUEST_BODY_FIELD_NAME, expectedMessage); } @@ -1771,10 +1898,9 @@ public void testTamperedRequestRest() throws Exception { // test disabled CapturingLogger.output(logger.getName(), Level.INFO).clear(); - updateLoggerSettings(Settings.builder() - .put(settings) - .put("xpack.security.audit.logfile.events.exclude", "tampered_request") - .build()); + updateLoggerSettings( + Settings.builder().put(settings).put("xpack.security.audit.logfile.events.exclude", "tampered_request").build() + ); auditTrail.tamperedRequest(requestId, request); assertEmptyLog(logger); } @@ -1787,10 +1913,10 @@ public void testTamperedRequest() throws Exception { final MapBuilder checkedFields = new MapBuilder<>(commonFields); final MapBuilder checkedArrayFields = new MapBuilder<>(); checkedFields.put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, LoggingAuditTrail.TRANSPORT_ORIGIN_FIELD_VALUE) - .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "tampered_request") - .put(LoggingAuditTrail.ACTION_FIELD_NAME, "_action") - .put(LoggingAuditTrail.REQUEST_NAME_FIELD_NAME, request.getClass().getSimpleName()) - .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); + .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "tampered_request") + .put(LoggingAuditTrail.ACTION_FIELD_NAME, "_action") + .put(LoggingAuditTrail.REQUEST_NAME_FIELD_NAME, request.getClass().getSimpleName()) + .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); restOrTransportOrigin(request, threadContext, checkedFields); indicesRequest(request, checkedFields, checkedArrayFields); opaqueId(threadContext, checkedFields); @@ -1799,10 +1925,9 @@ public void testTamperedRequest() throws Exception { // test disabled CapturingLogger.output(logger.getName(), Level.INFO).clear(); - updateLoggerSettings(Settings.builder() - .put(settings) - .put("xpack.security.audit.logfile.events.exclude", "tampered_request") - .build()); + updateLoggerSettings( + Settings.builder().put(settings).put("xpack.security.audit.logfile.events.exclude", "tampered_request").build() + ); auditTrail.tamperedRequest(requestId, "_action", request); assertEmptyLog(logger); } @@ -1816,10 +1941,10 @@ public void testTamperedRequestWithUser() throws Exception { Authentication authentication = createAuthentication(); auditTrail.tamperedRequest(requestId, authentication, "_action", request); checkedFields.put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, LoggingAuditTrail.TRANSPORT_ORIGIN_FIELD_VALUE) - .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "tampered_request") - .put(LoggingAuditTrail.ACTION_FIELD_NAME, "_action") - .put(LoggingAuditTrail.REQUEST_NAME_FIELD_NAME, request.getClass().getSimpleName()) - .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); + .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "tampered_request") + .put(LoggingAuditTrail.ACTION_FIELD_NAME, "_action") + .put(LoggingAuditTrail.REQUEST_NAME_FIELD_NAME, request.getClass().getSimpleName()) + .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); authentication(authentication, checkedFields); restOrTransportOrigin(request, threadContext, checkedFields); indicesRequest(request, checkedFields, checkedArrayFields); @@ -1835,10 +1960,10 @@ public void testTamperedRequestWithUser() throws Exception { checkedArrayFields = new MapBuilder<>(); auditTrail.tamperedRequest(requestId, authentication, "_action", request); checkedFields.put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, LoggingAuditTrail.TRANSPORT_ORIGIN_FIELD_VALUE) - .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "tampered_request") - .put(LoggingAuditTrail.ACTION_FIELD_NAME, "_action") - .put(LoggingAuditTrail.REQUEST_NAME_FIELD_NAME, request.getClass().getSimpleName()) - .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); + .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "tampered_request") + .put(LoggingAuditTrail.ACTION_FIELD_NAME, "_action") + .put(LoggingAuditTrail.REQUEST_NAME_FIELD_NAME, request.getClass().getSimpleName()) + .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); authentication(authentication, checkedFields); restOrTransportOrigin(request, threadContext, checkedFields); indicesRequest(request, checkedFields, checkedArrayFields); @@ -1848,10 +1973,9 @@ public void testTamperedRequestWithUser() throws Exception { // test disabled CapturingLogger.output(logger.getName(), Level.INFO).clear(); - updateLoggerSettings(Settings.builder() - .put(settings) - .put("xpack.security.audit.logfile.events.exclude", "tampered_request") - .build()); + updateLoggerSettings( + Settings.builder().put(settings).put("xpack.security.audit.logfile.events.exclude", "tampered_request").build() + ); auditTrail.tamperedRequest(requestId, authentication, "_action", request); assertEmptyLog(logger); } @@ -1864,23 +1988,25 @@ public void testConnectionDenied() throws Exception { auditTrail.connectionDenied(inetAddress, profile, rule); final MapBuilder checkedFields = new MapBuilder<>(commonFields); checkedFields.put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, LoggingAuditTrail.IP_FILTER_ORIGIN_FIELD_VALUE) - .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "connection_denied") - .put(LoggingAuditTrail.ORIGIN_TYPE_FIELD_NAME, - IPFilter.HTTP_PROFILE_NAME.equals(profile) ? LoggingAuditTrail.REST_ORIGIN_FIELD_VALUE - : LoggingAuditTrail.TRANSPORT_ORIGIN_FIELD_VALUE) - .put(LoggingAuditTrail.ORIGIN_ADDRESS_FIELD_NAME, NetworkAddress.format(inetAddress)) - .put(LoggingAuditTrail.TRANSPORT_PROFILE_FIELD_NAME, profile) - .put(LoggingAuditTrail.RULE_FIELD_NAME, "deny _all"); + .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "connection_denied") + .put( + LoggingAuditTrail.ORIGIN_TYPE_FIELD_NAME, + IPFilter.HTTP_PROFILE_NAME.equals(profile) + ? LoggingAuditTrail.REST_ORIGIN_FIELD_VALUE + : LoggingAuditTrail.TRANSPORT_ORIGIN_FIELD_VALUE + ) + .put(LoggingAuditTrail.ORIGIN_ADDRESS_FIELD_NAME, NetworkAddress.format(inetAddress)) + .put(LoggingAuditTrail.TRANSPORT_PROFILE_FIELD_NAME, profile) + .put(LoggingAuditTrail.RULE_FIELD_NAME, "deny _all"); opaqueId(threadContext, checkedFields); forwardedFor(threadContext, checkedFields); assertMsg(logger, checkedFields.immutableMap()); // test disabled CapturingLogger.output(logger.getName(), Level.INFO).clear(); - updateLoggerSettings(Settings.builder() - .put(settings) - .put("xpack.security.audit.logfile.events.exclude", "connection_denied") - .build()); + updateLoggerSettings( + Settings.builder().put(settings).put("xpack.security.audit.logfile.events.exclude", "connection_denied").build() + ); auditTrail.connectionDenied(inetAddress, profile, rule); assertEmptyLog(logger); } @@ -1894,20 +2020,22 @@ public void testConnectionGranted() throws Exception { assertEmptyLog(logger); // test enabled - updateLoggerSettings(Settings.builder() - .put(settings) - .put("xpack.security.audit.logfile.events.include", "connection_granted") - .build()); + updateLoggerSettings( + Settings.builder().put(settings).put("xpack.security.audit.logfile.events.include", "connection_granted").build() + ); auditTrail.connectionGranted(inetAddress, profile, rule); final MapBuilder checkedFields = new MapBuilder<>(commonFields); checkedFields.put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, LoggingAuditTrail.IP_FILTER_ORIGIN_FIELD_VALUE) - .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "connection_granted") - .put(LoggingAuditTrail.ORIGIN_TYPE_FIELD_NAME, - IPFilter.HTTP_PROFILE_NAME.equals(profile) ? LoggingAuditTrail.REST_ORIGIN_FIELD_VALUE - : LoggingAuditTrail.TRANSPORT_ORIGIN_FIELD_VALUE) - .put(LoggingAuditTrail.ORIGIN_ADDRESS_FIELD_NAME, NetworkAddress.format(inetAddress)) - .put(LoggingAuditTrail.TRANSPORT_PROFILE_FIELD_NAME, profile) - .put(LoggingAuditTrail.RULE_FIELD_NAME, "allow default:accept_all"); + .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "connection_granted") + .put( + LoggingAuditTrail.ORIGIN_TYPE_FIELD_NAME, + IPFilter.HTTP_PROFILE_NAME.equals(profile) + ? LoggingAuditTrail.REST_ORIGIN_FIELD_VALUE + : LoggingAuditTrail.TRANSPORT_ORIGIN_FIELD_VALUE + ) + .put(LoggingAuditTrail.ORIGIN_ADDRESS_FIELD_NAME, NetworkAddress.format(inetAddress)) + .put(LoggingAuditTrail.TRANSPORT_PROFILE_FIELD_NAME, profile) + .put(LoggingAuditTrail.RULE_FIELD_NAME, "allow default:accept_all"); opaqueId(threadContext, checkedFields); forwardedFor(threadContext, checkedFields); assertMsg(logger, checkedFields.immutableMap()); @@ -1918,23 +2046,24 @@ public void testRunAsGranted() throws Exception { final String[] expectedRoles = randomArray(0, 4, String[]::new, () -> randomBoolean() ? null : randomAlphaOfLengthBetween(1, 4)); final AuthorizationInfo authorizationInfo = () -> Collections.singletonMap(PRINCIPAL_ROLES_FIELD_NAME, expectedRoles); final Authentication authentication = new Authentication( - new User("running as", new String[] { "r2" }, new User("_username", new String[] { "r1" })), - new RealmRef("authRealm", "test", "foo"), - new RealmRef("lookRealm", "up", "by")); + new User("running as", new String[] { "r2" }, new User("_username", new String[] { "r1" })), + new RealmRef("authRealm", "test", "foo"), + new RealmRef("lookRealm", "up", "by") + ); final String requestId = randomRequestId(); auditTrail.runAsGranted(requestId, authentication, "_action", request, authorizationInfo); final MapBuilder checkedFields = new MapBuilder<>(commonFields); final MapBuilder checkedArrayFields = new MapBuilder<>(); checkedFields.put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, LoggingAuditTrail.TRANSPORT_ORIGIN_FIELD_VALUE) - .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "run_as_granted") - .put(LoggingAuditTrail.PRINCIPAL_FIELD_NAME, "_username") - .put(LoggingAuditTrail.PRINCIPAL_REALM_FIELD_NAME, "authRealm") - .put(LoggingAuditTrail.PRINCIPAL_RUN_AS_FIELD_NAME, "running as") - .put(LoggingAuditTrail.PRINCIPAL_RUN_AS_REALM_FIELD_NAME, "lookRealm") - .put(LoggingAuditTrail.ACTION_FIELD_NAME, "_action") - .put(LoggingAuditTrail.REQUEST_NAME_FIELD_NAME, request.getClass().getSimpleName()) - .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); + .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "run_as_granted") + .put(LoggingAuditTrail.PRINCIPAL_FIELD_NAME, "_username") + .put(LoggingAuditTrail.PRINCIPAL_REALM_FIELD_NAME, "authRealm") + .put(LoggingAuditTrail.PRINCIPAL_RUN_AS_FIELD_NAME, "running as") + .put(LoggingAuditTrail.PRINCIPAL_RUN_AS_REALM_FIELD_NAME, "lookRealm") + .put(LoggingAuditTrail.ACTION_FIELD_NAME, "_action") + .put(LoggingAuditTrail.REQUEST_NAME_FIELD_NAME, request.getClass().getSimpleName()) + .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); checkedArrayFields.put(PRINCIPAL_ROLES_FIELD_NAME, (String[]) authorizationInfo.asMap().get(PRINCIPAL_ROLES_FIELD_NAME)); restOrTransportOrigin(request, threadContext, checkedFields); indicesRequest(request, checkedFields, checkedArrayFields); @@ -1944,10 +2073,7 @@ public void testRunAsGranted() throws Exception { // test disabled CapturingLogger.output(logger.getName(), Level.INFO).clear(); - updateLoggerSettings(Settings.builder() - .put(settings) - .put("xpack.security.audit.logfile.events.exclude", "run_as_granted") - .build()); + updateLoggerSettings(Settings.builder().put(settings).put("xpack.security.audit.logfile.events.exclude", "run_as_granted").build()); auditTrail.runAsGranted(requestId, authentication, "_action", request, authorizationInfo); assertEmptyLog(logger); } @@ -1957,23 +2083,24 @@ public void testRunAsDenied() throws Exception { final String[] expectedRoles = randomArray(0, 4, String[]::new, () -> randomBoolean() ? null : randomAlphaOfLengthBetween(1, 4)); final AuthorizationInfo authorizationInfo = () -> Collections.singletonMap(PRINCIPAL_ROLES_FIELD_NAME, expectedRoles); final Authentication authentication = new Authentication( - new User("running as", new String[] { "r2" }, new User("_username", new String[] { "r1" })), - new RealmRef("authRealm", "test", "foo"), - new RealmRef("lookRealm", "up", "by")); + new User("running as", new String[] { "r2" }, new User("_username", new String[] { "r1" })), + new RealmRef("authRealm", "test", "foo"), + new RealmRef("lookRealm", "up", "by") + ); final String requestId = randomRequestId(); auditTrail.runAsDenied(requestId, authentication, "_action", request, authorizationInfo); final MapBuilder checkedFields = new MapBuilder<>(commonFields); final MapBuilder checkedArrayFields = new MapBuilder<>(); checkedFields.put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, LoggingAuditTrail.TRANSPORT_ORIGIN_FIELD_VALUE) - .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "run_as_denied") - .put(LoggingAuditTrail.PRINCIPAL_FIELD_NAME, "_username") - .put(LoggingAuditTrail.PRINCIPAL_REALM_FIELD_NAME, "authRealm") - .put(LoggingAuditTrail.PRINCIPAL_RUN_AS_FIELD_NAME, "running as") - .put(LoggingAuditTrail.PRINCIPAL_RUN_AS_REALM_FIELD_NAME, "lookRealm") - .put(LoggingAuditTrail.ACTION_FIELD_NAME, "_action") - .put(LoggingAuditTrail.REQUEST_NAME_FIELD_NAME, request.getClass().getSimpleName()) - .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); + .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "run_as_denied") + .put(LoggingAuditTrail.PRINCIPAL_FIELD_NAME, "_username") + .put(LoggingAuditTrail.PRINCIPAL_REALM_FIELD_NAME, "authRealm") + .put(LoggingAuditTrail.PRINCIPAL_RUN_AS_FIELD_NAME, "running as") + .put(LoggingAuditTrail.PRINCIPAL_RUN_AS_REALM_FIELD_NAME, "lookRealm") + .put(LoggingAuditTrail.ACTION_FIELD_NAME, "_action") + .put(LoggingAuditTrail.REQUEST_NAME_FIELD_NAME, request.getClass().getSimpleName()) + .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); checkedArrayFields.put(PRINCIPAL_ROLES_FIELD_NAME, (String[]) authorizationInfo.asMap().get(PRINCIPAL_ROLES_FIELD_NAME)); restOrTransportOrigin(request, threadContext, checkedFields); indicesRequest(request, checkedFields, checkedArrayFields); @@ -1983,10 +2110,7 @@ public void testRunAsDenied() throws Exception { // test disabled CapturingLogger.output(logger.getName(), Level.INFO).clear(); - updateLoggerSettings(Settings.builder() - .put(settings) - .put("xpack.security.audit.logfile.events.exclude", "run_as_denied") - .build()); + updateLoggerSettings(Settings.builder().put(settings).put("xpack.security.audit.logfile.events.exclude", "run_as_denied").build()); auditTrail.runAsDenied(requestId, authentication, "_action", request, authorizationInfo); assertEmptyLog(logger); } @@ -1997,8 +2121,10 @@ public void testAuthenticationSuccessRest() throws Exception { params.put("foo", "bar"); params.put("evac", "true"); } - final InetSocketAddress address = new InetSocketAddress(forge("_hostname", randomBoolean() ? "127.0.0.1" : "::1"), - randomIntBetween(9200, 9300)); + final InetSocketAddress address = new InetSocketAddress( + forge("_hostname", randomBoolean() ? "127.0.0.1" : "::1"), + randomIntBetween(9200, 9300) + ); final Tuple tuple = prepareRestContent("_uri", address, params); final String expectedMessage = tuple.v1().expectedMessage(); final RestRequest request = tuple.v2(); @@ -2010,19 +2136,18 @@ public void testAuthenticationSuccessRest() throws Exception { auditTrail.authenticationSuccess(requestId, authentication, request); assertEmptyLog(logger); - updateLoggerSettings(Settings.builder() - .put(this.settings) - .put("xpack.security.audit.logfile.events.include", "authentication_success") - .build()); + updateLoggerSettings( + Settings.builder().put(this.settings).put("xpack.security.audit.logfile.events.include", "authentication_success").build() + ); auditTrail.authenticationSuccess(requestId, authentication, request); checkedFields.put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, LoggingAuditTrail.REST_ORIGIN_FIELD_VALUE) - .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "authentication_success") - .put(LoggingAuditTrail.REALM_FIELD_NAME, authentication.getAuthenticatedBy().getName()) - .put(LoggingAuditTrail.ORIGIN_TYPE_FIELD_NAME, LoggingAuditTrail.REST_ORIGIN_FIELD_VALUE) - .put(LoggingAuditTrail.ORIGIN_ADDRESS_FIELD_NAME, NetworkAddress.format(address)) - .put(LoggingAuditTrail.REQUEST_METHOD_FIELD_NAME, request.method().toString()) - .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId) - .put(LoggingAuditTrail.URL_PATH_FIELD_NAME, "_uri"); + .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "authentication_success") + .put(LoggingAuditTrail.REALM_FIELD_NAME, authentication.getAuthenticatedBy().getName()) + .put(LoggingAuditTrail.ORIGIN_TYPE_FIELD_NAME, LoggingAuditTrail.REST_ORIGIN_FIELD_VALUE) + .put(LoggingAuditTrail.ORIGIN_ADDRESS_FIELD_NAME, NetworkAddress.format(address)) + .put(LoggingAuditTrail.REQUEST_METHOD_FIELD_NAME, request.method().toString()) + .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId) + .put(LoggingAuditTrail.URL_PATH_FIELD_NAME, "_uri"); if (includeRequestBody && Strings.hasLength(expectedMessage)) { checkedFields.put(LoggingAuditTrail.REQUEST_BODY_FIELD_NAME, expectedMessage); } @@ -2041,13 +2166,13 @@ public void testAuthenticationSuccessRest() throws Exception { checkedFields = new MapBuilder<>(commonFields); auditTrail.authenticationSuccess(requestId, authentication, request); checkedFields.put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, LoggingAuditTrail.REST_ORIGIN_FIELD_VALUE) - .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "authentication_success") - .put(LoggingAuditTrail.REALM_FIELD_NAME, "_es_api_key") - .put(LoggingAuditTrail.ORIGIN_TYPE_FIELD_NAME, LoggingAuditTrail.REST_ORIGIN_FIELD_VALUE) - .put(LoggingAuditTrail.ORIGIN_ADDRESS_FIELD_NAME, NetworkAddress.format(address)) - .put(LoggingAuditTrail.REQUEST_METHOD_FIELD_NAME, request.method().toString()) - .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId) - .put(LoggingAuditTrail.URL_PATH_FIELD_NAME, "_uri"); + .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "authentication_success") + .put(LoggingAuditTrail.REALM_FIELD_NAME, "_es_api_key") + .put(LoggingAuditTrail.ORIGIN_TYPE_FIELD_NAME, LoggingAuditTrail.REST_ORIGIN_FIELD_VALUE) + .put(LoggingAuditTrail.ORIGIN_ADDRESS_FIELD_NAME, NetworkAddress.format(address)) + .put(LoggingAuditTrail.REQUEST_METHOD_FIELD_NAME, request.method().toString()) + .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId) + .put(LoggingAuditTrail.URL_PATH_FIELD_NAME, "_uri"); if (includeRequestBody && Strings.hasLength(expectedMessage)) { checkedFields.put(LoggingAuditTrail.REQUEST_BODY_FIELD_NAME, expectedMessage); } @@ -2071,16 +2196,15 @@ public void testAuthenticationSuccessTransport() throws Exception { auditTrail.authenticationSuccess(requestId, authentication, "_action", request); assertEmptyLog(logger); - updateLoggerSettings(Settings.builder() - .put(this.settings) - .put("xpack.security.audit.logfile.events.include", "authentication_success") - .build()); + updateLoggerSettings( + Settings.builder().put(this.settings).put("xpack.security.audit.logfile.events.include", "authentication_success").build() + ); auditTrail.authenticationSuccess(requestId, authentication, "_action", request); checkedFields.put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, LoggingAuditTrail.TRANSPORT_ORIGIN_FIELD_VALUE) - .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "authentication_success") - .put(LoggingAuditTrail.ACTION_FIELD_NAME, "_action") - .put(LoggingAuditTrail.REQUEST_NAME_FIELD_NAME, request.getClass().getSimpleName()) - .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); + .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "authentication_success") + .put(LoggingAuditTrail.ACTION_FIELD_NAME, "_action") + .put(LoggingAuditTrail.REQUEST_NAME_FIELD_NAME, request.getClass().getSimpleName()) + .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); authentication(authentication, checkedFields); restOrTransportOrigin(request, threadContext, checkedFields); indicesRequest(request, checkedFields, checkedArrayFields); @@ -2096,10 +2220,10 @@ public void testAuthenticationSuccessTransport() throws Exception { checkedArrayFields = new MapBuilder<>(); auditTrail.authenticationSuccess(requestId, authentication, "_action", request); checkedFields.put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, LoggingAuditTrail.TRANSPORT_ORIGIN_FIELD_VALUE) - .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "authentication_success") - .put(LoggingAuditTrail.ACTION_FIELD_NAME, "_action") - .put(LoggingAuditTrail.REQUEST_NAME_FIELD_NAME, request.getClass().getSimpleName()) - .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); + .put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "authentication_success") + .put(LoggingAuditTrail.ACTION_FIELD_NAME, "_action") + .put(LoggingAuditTrail.REQUEST_NAME_FIELD_NAME, request.getClass().getSimpleName()) + .put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId); authentication(authentication, checkedFields); restOrTransportOrigin(request, threadContext, checkedFields); indicesRequest(request, checkedFields, checkedArrayFields); @@ -2109,18 +2233,18 @@ public void testAuthenticationSuccessTransport() throws Exception { } public void testRequestsWithoutIndices() throws Exception { - settings = Settings.builder() - .put(settings) - .put("xpack.security.audit.logfile.events.include", "_all") - .build(); + settings = Settings.builder().put(settings).put("xpack.security.audit.logfile.events.include", "_all").build(); auditTrail = new LoggingAuditTrail(settings, clusterService, logger, threadContext); - final AuthorizationInfo authorizationInfo = - () -> Collections.singletonMap(PRINCIPAL_ROLES_FIELD_NAME, new String[] { randomAlphaOfLengthBetween(1, 6) }); + final AuthorizationInfo authorizationInfo = () -> Collections.singletonMap( + PRINCIPAL_ROLES_FIELD_NAME, + new String[] { randomAlphaOfLengthBetween(1, 6) } + ); final String realm = randomAlphaOfLengthBetween(1, 6); // transport messages without indices - final TransportRequest[] requests = new TransportRequest[] { new MockRequest(threadContext), - new org.elasticsearch.action.MockIndicesRequest(IndicesOptions.strictExpandOpenAndForbidClosed(), new String[0]), - new org.elasticsearch.action.MockIndicesRequest(IndicesOptions.strictExpandOpenAndForbidClosed(), (String[]) null) }; + final TransportRequest[] requests = new TransportRequest[] { + new MockRequest(threadContext), + new org.elasticsearch.action.MockIndicesRequest(IndicesOptions.strictExpandOpenAndForbidClosed(), new String[0]), + new org.elasticsearch.action.MockIndicesRequest(IndicesOptions.strictExpandOpenAndForbidClosed(), (String[]) null) }; final List output = CapturingLogger.output(logger.getName(), Level.INFO); int logEntriesCount = 1; for (final TransportRequest request : requests) { @@ -2136,32 +2260,59 @@ public void testRequestsWithoutIndices() throws Exception { auditTrail.authenticationFailed("_req_id", realm, mockToken(), "_action", request); assertThat(output.size(), is(logEntriesCount++)); assertThat(output.get(logEntriesCount - 2), not(containsString("indices="))); - auditTrail.accessGranted("_req_id", randomBoolean() ? createAuthentication() : createApiKeyAuthentication(apiKeyService, - createAuthentication()), "_action", request, authorizationInfo); + auditTrail.accessGranted( + "_req_id", + randomBoolean() ? createAuthentication() : createApiKeyAuthentication(apiKeyService, createAuthentication()), + "_action", + request, + authorizationInfo + ); assertThat(output.size(), is(logEntriesCount++)); assertThat(output.get(logEntriesCount - 2), not(containsString("indices="))); - auditTrail.accessDenied("_req_id", randomBoolean() ? createAuthentication() : createApiKeyAuthentication(apiKeyService, - createAuthentication()), "_action", request, authorizationInfo); + auditTrail.accessDenied( + "_req_id", + randomBoolean() ? createAuthentication() : createApiKeyAuthentication(apiKeyService, createAuthentication()), + "_action", + request, + authorizationInfo + ); assertThat(output.size(), is(logEntriesCount++)); assertThat(output.get(logEntriesCount - 2), not(containsString("indices="))); auditTrail.tamperedRequest("_req_id", "_action", request); assertThat(output.size(), is(logEntriesCount++)); assertThat(output.get(logEntriesCount - 2), not(containsString("indices="))); - auditTrail.tamperedRequest("_req_id", randomBoolean() ? createAuthentication() : createApiKeyAuthentication(apiKeyService, - createAuthentication()), "_action", request); + auditTrail.tamperedRequest( + "_req_id", + randomBoolean() ? createAuthentication() : createApiKeyAuthentication(apiKeyService, createAuthentication()), + "_action", + request + ); assertThat(output.size(), is(logEntriesCount++)); assertThat(output.get(logEntriesCount - 2), not(containsString("indices="))); - auditTrail.runAsGranted("_req_id", randomBoolean() ? createAuthentication() : createApiKeyAuthentication(apiKeyService, - createAuthentication()), "_action", request, authorizationInfo); + auditTrail.runAsGranted( + "_req_id", + randomBoolean() ? createAuthentication() : createApiKeyAuthentication(apiKeyService, createAuthentication()), + "_action", + request, + authorizationInfo + ); assertThat(output.size(), is(logEntriesCount++)); assertThat(output.get(logEntriesCount - 2), not(containsString("indices="))); - auditTrail.runAsDenied("_req_id", randomBoolean() ? createAuthentication() : createApiKeyAuthentication(apiKeyService, - createAuthentication()), "_action", request, authorizationInfo); + auditTrail.runAsDenied( + "_req_id", + randomBoolean() ? createAuthentication() : createApiKeyAuthentication(apiKeyService, createAuthentication()), + "_action", + request, + authorizationInfo + ); assertThat(output.size(), is(logEntriesCount++)); assertThat(output.get(logEntriesCount - 2), not(containsString("indices="))); - auditTrail.authenticationSuccess("_req_id", randomBoolean() ? createAuthentication() : - createApiKeyAuthentication(apiKeyService, createAuthentication()), - "_action", request); + auditTrail.authenticationSuccess( + "_req_id", + randomBoolean() ? createAuthentication() : createApiKeyAuthentication(apiKeyService, createAuthentication()), + "_action", + request + ); assertThat(output.size(), is(logEntriesCount++)); assertThat(output.get(logEntriesCount - 2), not(containsString("indices="))); } @@ -2201,13 +2352,19 @@ private void assertMsg(String logLine, Map checkFields, Map checkField : checkFields.entrySet()) { if (null == checkField.getValue()) { // null checkField means that the field does not exist - assertThat("Field: " + checkField.getKey() + " should be missing.", - logLine.contains(Pattern.quote("\"" + checkField.getKey() + "\":")), is(false)); + assertThat( + "Field: " + checkField.getKey() + " should be missing.", + logLine.contains(Pattern.quote("\"" + checkField.getKey() + "\":")), + is(false) + ); } else { final String quotedValue = "\"" + checkField.getValue().replaceAll("\"", "\\\\\"") + "\""; final Pattern logEntryFieldPattern = Pattern.compile(Pattern.quote("\"" + checkField.getKey() + "\":" + quotedValue)); - assertThat("Field " + checkField.getKey() + " value mismatch. Expected " + quotedValue, - logEntryFieldPattern.matcher(logLine).find(), is(true)); + assertThat( + "Field " + checkField.getKey() + " value mismatch. Expected " + quotedValue, + logEntryFieldPattern.matcher(logLine).find(), + is(true) + ); // remove checked field logLine = logEntryFieldPattern.matcher(logLine).replaceFirst(""); } @@ -2216,25 +2373,33 @@ private void assertMsg(String logLine, Map checkFields, Map checkArrayField : checkArrayFields.entrySet()) { if (null == checkArrayField.getValue()) { // null checkField means that the field does not exist - assertThat("Field: " + checkArrayField.getKey() + " should be missing.", - logLine.contains(Pattern.quote("\"" + checkArrayField.getKey() + "\":")), is(false)); + assertThat( + "Field: " + checkArrayField.getKey() + " should be missing.", + logLine.contains(Pattern.quote("\"" + checkArrayField.getKey() + "\":")), + is(false) + ); } else { - final String quotedValue = "[" + Arrays.asList(checkArrayField.getValue()) + final String quotedValue = "[" + + Arrays.asList(checkArrayField.getValue()) .stream() .filter(s -> s != null) .map(s -> "\"" + s.replaceAll("\"", "\\\\\"") + "\"") .reduce((x, y) -> x + "," + y) - .orElse("") + "]"; + .orElse("") + + "]"; final Pattern logEntryFieldPattern = Pattern.compile(Pattern.quote("\"" + checkArrayField.getKey() + "\":" + quotedValue)); - assertThat("Field " + checkArrayField.getKey() + " value mismatch. Expected " + quotedValue + ".\nLog line: " + logLine, - logEntryFieldPattern.matcher(logLine).find(), is(true)); + assertThat( + "Field " + checkArrayField.getKey() + " value mismatch. Expected " + quotedValue + ".\nLog line: " + logLine, + logEntryFieldPattern.matcher(logLine).find(), + is(true) + ); // remove checked field logLine = logEntryFieldPattern.matcher(logLine).replaceFirst(""); } } logLine = logLine.replaceFirst("\"" + LoggingAuditTrail.LOG_TYPE + "\":\"audit\", ", "") - .replaceFirst("\"" + LoggingAuditTrail.TIMESTAMP + "\":\"[^\"]*\"", "") - .replaceAll("[{},]", ""); + .replaceFirst("\"" + LoggingAuditTrail.TIMESTAMP + "\":\"[^\"]*\"", "") + .replaceAll("[{},]", ""); // check no extra fields assertThat("Log event has extra unexpected content: " + logLine, Strings.hasText(logLine), is(false)); } @@ -2288,28 +2453,46 @@ private Authentication createAuthentication() { user = new User(randomAlphaOfLength(4), new String[] { "r1" }, new User("authenticated_username", "r2")); lookedUpBy = new RealmRef(randomAlphaOfLength(4), "lookup", "by"); authBy = new RealmRef("authRealm", "auth", "foo"); - authenticationType= randomFrom(AuthenticationType.REALM, AuthenticationType.TOKEN, - AuthenticationType.INTERNAL, AuthenticationType.ANONYMOUS); + authenticationType = randomFrom( + AuthenticationType.REALM, + AuthenticationType.TOKEN, + AuthenticationType.INTERNAL, + AuthenticationType.ANONYMOUS + ); authMetadata = Map.of(); break; case 1: user = new User(randomAlphaOfLength(4), "r1"); lookedUpBy = null; authBy = new RealmRef(randomAlphaOfLength(4), "auth", "by"); - authenticationType= randomFrom(AuthenticationType.REALM, AuthenticationType.TOKEN, - AuthenticationType.INTERNAL, AuthenticationType.ANONYMOUS); + authenticationType = randomFrom( + AuthenticationType.REALM, + AuthenticationType.TOKEN, + AuthenticationType.INTERNAL, + AuthenticationType.ANONYMOUS + ); authMetadata = Map.of(); break; default: // service account final String principal = randomAlphaOfLengthBetween(3, 8) + "/" + randomAlphaOfLengthBetween(3, 8); - user = new User(principal, Strings.EMPTY_ARRAY, "Service account - " + principal, null, - Map.of("_elastic_service_account", true), true); + user = new User( + principal, + Strings.EMPTY_ARRAY, + "Service account - " + principal, + null, + Map.of("_elastic_service_account", true), + true + ); lookedUpBy = null; authBy = new RealmRef("_service_account", "_service_account", randomAlphaOfLengthBetween(3, 8)); authenticationType = AuthenticationType.TOKEN; final TokenInfo.TokenSource tokenSource = randomFrom(TokenInfo.TokenSource.values()); - authMetadata = Map.of("_token_name", ValidationTests.randomTokenName(), - "_token_source", tokenSource.name().toLowerCase(Locale.ROOT)); + authMetadata = Map.of( + "_token_name", + ValidationTests.randomTokenName(), + "_token_source", + tokenSource.name().toLowerCase(Locale.ROOT) + ); } return new Authentication(user, authBy, lookedUpBy, Version.CURRENT, authenticationType, authMetadata); } @@ -2338,7 +2521,7 @@ public Object credentials() { } @Override - public void clearCredentials() { } + public void clearCredentials() {} }; } @@ -2371,8 +2554,10 @@ public void writeTo(StreamOutput out) throws IOException {} static class MockIndicesRequest extends org.elasticsearch.action.MockIndicesRequest { MockIndicesRequest(ThreadContext threadContext) throws IOException { - super(IndicesOptions.strictExpandOpenAndForbidClosed(), - randomArray(0, 4, String[]::new, () -> randomBoolean() ? null : randomAlphaOfLengthBetween(1, 4))); + super( + IndicesOptions.strictExpandOpenAndForbidClosed(), + randomArray(0, 4, String[]::new, () -> randomBoolean() ? null : randomAlphaOfLengthBetween(1, 4)) + ); if (randomBoolean()) { remoteAddress(buildNewFakeTransportAddress()); } @@ -2394,17 +2579,20 @@ private String randomRequestId() { return randomBoolean() ? randomAlphaOfLengthBetween(8, 24) : AuditUtil.generateRequestId(threadContext); } - private static void restOrTransportOrigin(TransportRequest request, ThreadContext threadContext, - MapBuilder checkedFields) { + private static void restOrTransportOrigin( + TransportRequest request, + ThreadContext threadContext, + MapBuilder checkedFields + ) { final InetSocketAddress restAddress = RemoteHostHeader.restRemoteAddress(threadContext); if (restAddress != null) { checkedFields.put(LoggingAuditTrail.ORIGIN_TYPE_FIELD_NAME, LoggingAuditTrail.REST_ORIGIN_FIELD_VALUE) - .put(LoggingAuditTrail.ORIGIN_ADDRESS_FIELD_NAME, NetworkAddress.format(restAddress)); + .put(LoggingAuditTrail.ORIGIN_ADDRESS_FIELD_NAME, NetworkAddress.format(restAddress)); } else { final TransportAddress address = request.remoteAddress(); if (address != null) { checkedFields.put(LoggingAuditTrail.ORIGIN_TYPE_FIELD_NAME, LoggingAuditTrail.TRANSPORT_ORIGIN_FIELD_VALUE) - .put(LoggingAuditTrail.ORIGIN_ADDRESS_FIELD_NAME, NetworkAddress.format(address.address())); + .put(LoggingAuditTrail.ORIGIN_ADDRESS_FIELD_NAME, NetworkAddress.format(address.address())); } } } @@ -2414,8 +2602,10 @@ private static void authentication(Authentication authentication, MapBuilder checkedFields) { + private static void forwardedFor(ThreadContext threadContext, MapBuilder checkedFields) { final String forwardedFor = threadContext.getHeader(AuditTrail.X_FORWARDED_FOR_HEADER); if (forwardedFor != null) { checkedFields.put(LoggingAuditTrail.X_FORWARDED_FOR_FIELD_NAME, forwardedFor); } } - private static void indicesRequest(TransportRequest request, MapBuilder checkedFields, - MapBuilder checkedArrayFields) { + private static void indicesRequest( + TransportRequest request, + MapBuilder checkedFields, + MapBuilder checkedArrayFields + ) { if (request instanceof IndicesRequest) { checkedFields.put(LoggingAuditTrail.REQUEST_NAME_FIELD_NAME, MockIndicesRequest.class.getSimpleName()); checkedArrayFields.put(LoggingAuditTrail.INDICES_FIELD_NAME, ((IndicesRequest) request).indices()); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ApiKeyServiceTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ApiKeyServiceTests.java index 93c75e4ebdcd8..2e2b8b32f861c 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ApiKeyServiceTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ApiKeyServiceTests.java @@ -29,21 +29,16 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.cache.Cache; import org.elasticsearch.common.logging.Loggers; -import org.elasticsearch.common.util.concurrent.ListenableFuture; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.AbstractRunnable; import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException; +import org.elasticsearch.common.util.concurrent.ListenableFuture; import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.xcontent.json.JsonXContent; +import org.elasticsearch.core.TimeValue; +import org.elasticsearch.core.Tuple; import org.elasticsearch.index.get.GetResult; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.test.ClusterServiceUtils; @@ -54,6 +49,11 @@ import org.elasticsearch.threadpool.FixedExecutorBuilder; import org.elasticsearch.threadpool.TestThreadPool; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.xpack.core.security.SecurityContext; import org.elasticsearch.xpack.core.security.action.ApiKeyTests; @@ -148,9 +148,17 @@ public class ApiKeyServiceTests extends ESTestCase { @Before public void createThreadPool() { threadPool = Mockito.spy( - new TestThreadPool("api key service tests", - new FixedExecutorBuilder(Settings.EMPTY, SECURITY_CRYPTO_THREAD_POOL_NAME, 1, 1000, - "xpack.security.crypto.thread_pool", false)) + new TestThreadPool( + "api key service tests", + new FixedExecutorBuilder( + Settings.EMPTY, + SECURITY_CRYPTO_THREAD_POOL_NAME, + 1, + 1000, + "xpack.security.crypto.thread_pool", + false + ) + ) ); } @@ -172,7 +180,8 @@ public void testCreateApiKeyUsesBulkIndexAction() throws Exception { final Authentication authentication = new Authentication( new User("alice", "superuser"), new RealmRef("file", "file", "node-1"), - null); + null + ); final CreateApiKeyRequest createApiKeyRequest = new CreateApiKeyRequest("key-1", null, null); when(client.prepareIndex(anyString())).thenReturn(new IndexRequestBuilder(client, IndexAction.INSTANCE)); when(client.threadPool()).thenReturn(threadPool); @@ -197,7 +206,8 @@ public void testCreateApiKeyWillCacheOnCreation() { final Authentication authentication = new Authentication( new User(randomAlphaOfLengthBetween(8, 16), "superuser"), new RealmRef(randomAlphaOfLengthBetween(3, 8), randomAlphaOfLengthBetween(3, 8), randomAlphaOfLengthBetween(3, 8)), - null); + null + ); final CreateApiKeyRequest createApiKeyRequest = new CreateApiKeyRequest(randomAlphaOfLengthBetween(3, 8), null, null); when(client.prepareIndex(anyString())).thenReturn(new IndexRequestBuilder(client, IndexAction.INSTANCE)); when(client.threadPool()).thenReturn(threadPool); @@ -207,10 +217,18 @@ public void testCreateApiKeyWillCacheOnCreation() { final ActionListener listener = (ActionListener) args[2]; final IndexResponse indexResponse = new IndexResponse( new ShardId(INTERNAL_SECURITY_MAIN_INDEX_7, randomAlphaOfLength(22), randomIntBetween(0, 1)), - createApiKeyRequest.getId(), randomLongBetween(1, 99), randomLongBetween(1, 99), randomIntBetween(1, 99), true); - listener.onResponse(new BulkResponse(new BulkItemResponse[]{ - BulkItemResponse.success(randomInt(), DocWriteRequest.OpType.INDEX, indexResponse) - }, randomLongBetween(0, 100))); + createApiKeyRequest.getId(), + randomLongBetween(1, 99), + randomLongBetween(1, 99), + randomIntBetween(1, 99), + true + ); + listener.onResponse( + new BulkResponse( + new BulkItemResponse[] { BulkItemResponse.success(randomInt(), DocWriteRequest.OpType.INDEX, indexResponse) }, + randomLongBetween(0, 100) + ) + ); return null; }).when(client).execute(eq(BulkAction.INSTANCE), any(BulkRequest.class), any()); @@ -255,8 +273,10 @@ public void testGetCredentialsFromThreadContext() { headerValue = apiKeyAuthScheme + " " + Base64.getEncoder().encodeToString((id + key).getBytes(StandardCharsets.UTF_8)); try (ThreadContext.StoredContext ignore = threadContext.stashContext()) { threadContext.putHeader("Authorization", headerValue); - IllegalArgumentException e = - expectThrows(IllegalArgumentException.class, () -> apiKeyService.getCredentialsFromHeader(threadContext)); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> apiKeyService.getCredentialsFromHeader(threadContext) + ); assertEquals("invalid ApiKey value", e.getMessage()); } } @@ -271,10 +291,11 @@ public void testAuthenticateWithApiKey() throws Exception { final User user; if (randomBoolean()) { user = new User( - new User("hulk", new String[]{"superuser"}, "Bruce Banner", "hulk@test.com", Map.of(), true), - new User("authenticated_user", new String[]{"other"})); + new User("hulk", new String[] { "superuser" }, "Bruce Banner", "hulk@test.com", Map.of(), true), + new User("authenticated_user", new String[] { "other" }) + ); } else { - user = new User("hulk", new String[]{"superuser"}, "Bruce Banner", "hulk@test.com", Map.of(), true); + user = new User("hulk", new String[] { "superuser" }, "Bruce Banner", "hulk@test.com", Map.of(), true); } final Map metadata = mockKeyDocument(service, id, key, user); @@ -380,30 +401,59 @@ private Map mockKeyDocument(ApiKeyService service, String id, St return mockKeyDocument(service, id, key, user, false, Duration.ofSeconds(3600)); } - private Map mockKeyDocument(ApiKeyService service, String id, String key, User user, boolean invalidated, - Duration expiry) throws IOException { + private Map mockKeyDocument( + ApiKeyService service, + String id, + String key, + User user, + boolean invalidated, + Duration expiry + ) throws IOException { return mockKeyDocument(service, id, key, user, invalidated, expiry, null); } - private Map mockKeyDocument(ApiKeyService service, String id, String key, User user, boolean invalidated, - Duration expiry, List keyRoles) throws IOException { + private Map mockKeyDocument( + ApiKeyService service, + String id, + String key, + User user, + boolean invalidated, + Duration expiry, + List keyRoles + ) throws IOException { final Authentication authentication; if (user.isRunAs()) { - authentication = new Authentication(user, new RealmRef("authRealm", "test", "foo"), - new RealmRef("realm1", "native", "node01"), Version.CURRENT, - randomFrom(AuthenticationType.REALM, AuthenticationType.TOKEN, AuthenticationType.INTERNAL, - AuthenticationType.ANONYMOUS), Collections.emptyMap()); + authentication = new Authentication( + user, + new RealmRef("authRealm", "test", "foo"), + new RealmRef("realm1", "native", "node01"), + Version.CURRENT, + randomFrom(AuthenticationType.REALM, AuthenticationType.TOKEN, AuthenticationType.INTERNAL, AuthenticationType.ANONYMOUS), + Collections.emptyMap() + ); } else { - authentication = new Authentication(user, new RealmRef("realm1", "native", "node01"), null, - Version.CURRENT, randomFrom(AuthenticationType.REALM, AuthenticationType.TOKEN, AuthenticationType.INTERNAL, - AuthenticationType.ANONYMOUS), Collections.emptyMap()); + authentication = new Authentication( + user, + new RealmRef("realm1", "native", "node01"), + null, + Version.CURRENT, + randomFrom(AuthenticationType.REALM, AuthenticationType.TOKEN, AuthenticationType.INTERNAL, AuthenticationType.ANONYMOUS), + Collections.emptyMap() + ); } @SuppressWarnings("unchecked") final Map metadata = ApiKeyTests.randomMetadata(); XContentBuilder docSource = service.newDocument( - getFastStoredHashAlgoForTests().hash(new SecureString(key.toCharArray())),"test", authentication, - Collections.singleton(SUPERUSER_ROLE_DESCRIPTOR), Instant.now(), Instant.now().plus(expiry), keyRoles, - Version.CURRENT, metadata); + getFastStoredHashAlgoForTests().hash(new SecureString(key.toCharArray())), + "test", + authentication, + Collections.singleton(SUPERUSER_ROLE_DESCRIPTOR), + Instant.now(), + Instant.now().plus(expiry), + keyRoles, + Version.CURRENT, + metadata + ); if (invalidated) { Map map = XContentHelper.convertToMap(BytesReference.bytes(docSource), true, XContentType.JSON).v2(); map.put("api_key_invalidated", true); @@ -438,8 +488,13 @@ public void testValidateApiKey() throws Exception { ApiKeyService service = createApiKeyService(Settings.EMPTY); PlainActionFuture> future = new PlainActionFuture<>(); - service.validateApiKeyCredentials(apiKeyId, apiKeyDoc, - new ApiKeyCredentials(apiKeyId, new SecureString(apiKey.toCharArray())), Clock.systemUTC(), future); + service.validateApiKeyCredentials( + apiKeyId, + apiKeyDoc, + new ApiKeyCredentials(apiKeyId, new SecureString(apiKey.toCharArray())), + Clock.systemUTC(), + future + ); AuthenticationResult result = future.get(); assertNotNull(result); assertTrue(result.isAuthenticated()); @@ -449,14 +504,18 @@ public void testValidateApiKey() throws Exception { assertThat(result.getValue().roles(), is(emptyArray())); assertThat(result.getValue().metadata(), is(Collections.emptyMap())); assertThat(result.getMetadata().get(API_KEY_ROLE_DESCRIPTORS_KEY), equalTo(apiKeyDoc.roleDescriptorsBytes)); - assertThat(result.getMetadata().get(API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY), - equalTo(apiKeyDoc.limitedByRoleDescriptorsBytes)); + assertThat(result.getMetadata().get(API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY), equalTo(apiKeyDoc.limitedByRoleDescriptorsBytes)); assertThat(result.getMetadata().get(ApiKeyService.API_KEY_CREATOR_REALM_NAME), is("realm1")); apiKeyDoc = buildApiKeyDoc(hash, Clock.systemUTC().instant().plus(1L, ChronoUnit.HOURS).toEpochMilli(), false); future = new PlainActionFuture<>(); - service.validateApiKeyCredentials(apiKeyId, apiKeyDoc, - new ApiKeyCredentials(apiKeyId, new SecureString(apiKey.toCharArray())), Clock.systemUTC(), future); + service.validateApiKeyCredentials( + apiKeyId, + apiKeyDoc, + new ApiKeyCredentials(apiKeyId, new SecureString(apiKey.toCharArray())), + Clock.systemUTC(), + future + ); result = future.get(); assertNotNull(result); assertTrue(result.isAuthenticated()); @@ -466,14 +525,18 @@ public void testValidateApiKey() throws Exception { assertThat(result.getValue().roles(), is(emptyArray())); assertThat(result.getValue().metadata(), is(Collections.emptyMap())); assertThat(result.getMetadata().get(API_KEY_ROLE_DESCRIPTORS_KEY), equalTo(apiKeyDoc.roleDescriptorsBytes)); - assertThat(result.getMetadata().get(API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY), - equalTo(apiKeyDoc.limitedByRoleDescriptorsBytes)); + assertThat(result.getMetadata().get(API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY), equalTo(apiKeyDoc.limitedByRoleDescriptorsBytes)); assertThat(result.getMetadata().get(ApiKeyService.API_KEY_CREATOR_REALM_NAME), is("realm1")); apiKeyDoc = buildApiKeyDoc(hash, Clock.systemUTC().instant().minus(1L, ChronoUnit.HOURS).toEpochMilli(), false); future = new PlainActionFuture<>(); - service.validateApiKeyCredentials(apiKeyId, apiKeyDoc, - new ApiKeyCredentials(apiKeyId, new SecureString(apiKey.toCharArray())), Clock.systemUTC(), future); + service.validateApiKeyCredentials( + apiKeyId, + apiKeyDoc, + new ApiKeyCredentials(apiKeyId, new SecureString(apiKey.toCharArray())), + Clock.systemUTC(), + future + ); result = future.get(); assertNotNull(result); assertFalse(result.isAuthenticated()); @@ -483,9 +546,13 @@ public void testValidateApiKey() throws Exception { service.getApiKeyAuthCache().put(apiKeyId, new ListenableFuture<>()); assertNotNull(service.getApiKeyAuthCache().get(apiKeyId)); future = new PlainActionFuture<>(); - service.validateApiKeyCredentials(apiKeyId, apiKeyDoc, + service.validateApiKeyCredentials( + apiKeyId, + apiKeyDoc, new ApiKeyCredentials(apiKeyId, new SecureString(randomAlphaOfLength(15).toCharArray())), - Clock.systemUTC(), future); + Clock.systemUTC(), + future + ); result = future.get(); assertNotNull(result); assertFalse(result.isAuthenticated()); @@ -496,22 +563,28 @@ public void testValidateApiKey() throws Exception { public void testGetRolesForApiKeyNotInContext() throws Exception { Map superUserRdMap; try (XContentBuilder builder = JsonXContent.contentBuilder()) { - superUserRdMap = XContentHelper.convertToMap(XContentType.JSON.xContent(), - BytesReference.bytes(SUPERUSER_ROLE_DESCRIPTOR - .toXContent(builder, ToXContent.EMPTY_PARAMS, true)) - .streamInput(), - false); + superUserRdMap = XContentHelper.convertToMap( + XContentType.JSON.xContent(), + BytesReference.bytes(SUPERUSER_ROLE_DESCRIPTOR.toXContent(builder, ToXContent.EMPTY_PARAMS, true)).streamInput(), + false + ); } Map authMetadata = new HashMap<>(); authMetadata.put(ApiKeyService.API_KEY_ID_KEY, randomAlphaOfLength(12)); - authMetadata.put(API_KEY_ROLE_DESCRIPTORS_KEY, - Collections.singletonMap(SUPERUSER_ROLE_DESCRIPTOR.getName(), superUserRdMap)); - authMetadata.put(API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY, - Collections.singletonMap(SUPERUSER_ROLE_DESCRIPTOR.getName(), superUserRdMap)); + authMetadata.put(API_KEY_ROLE_DESCRIPTORS_KEY, Collections.singletonMap(SUPERUSER_ROLE_DESCRIPTOR.getName(), superUserRdMap)); + authMetadata.put( + API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY, + Collections.singletonMap(SUPERUSER_ROLE_DESCRIPTOR.getName(), superUserRdMap) + ); - final Authentication authentication = new Authentication(new User("joe"), new RealmRef("apikey", "apikey", "node"), null, + final Authentication authentication = new Authentication( + new User("joe"), + new RealmRef("apikey", "apikey", "node"), + null, VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, Version.V_7_8_1), - AuthenticationType.API_KEY, authMetadata); + AuthenticationType.API_KEY, + authMetadata + ); ApiKeyService service = createApiKeyService(Settings.EMPTY); PlainActionFuture roleFuture = new PlainActionFuture<>(); @@ -526,47 +599,62 @@ public void testGetRolesForApiKey() throws Exception { Map authMetadata = new HashMap<>(); authMetadata.put(ApiKeyService.API_KEY_ID_KEY, randomAlphaOfLength(12)); boolean emptyApiKeyRoleDescriptor = randomBoolean(); - final RoleDescriptor roleARoleDescriptor = new RoleDescriptor("a role", new String[] { "monitor" }, + final RoleDescriptor roleARoleDescriptor = new RoleDescriptor( + "a role", + new String[] { "monitor" }, new RoleDescriptor.IndicesPrivileges[] { RoleDescriptor.IndicesPrivileges.builder().indices("*").privileges("monitor").build() }, - null); + null + ); Map roleARDMap; try (XContentBuilder builder = JsonXContent.contentBuilder()) { - roleARDMap = XContentHelper.convertToMap(XContentType.JSON.xContent(), - BytesReference.bytes(roleARoleDescriptor.toXContent(builder, ToXContent.EMPTY_PARAMS, true)).streamInput(), false); + roleARDMap = XContentHelper.convertToMap( + XContentType.JSON.xContent(), + BytesReference.bytes(roleARoleDescriptor.toXContent(builder, ToXContent.EMPTY_PARAMS, true)).streamInput(), + false + ); } - authMetadata.put(API_KEY_ROLE_DESCRIPTORS_KEY, - (emptyApiKeyRoleDescriptor) ? randomFrom(Arrays.asList(null, Collections.emptyMap())) - : Collections.singletonMap("a role", roleARDMap)); + authMetadata.put( + API_KEY_ROLE_DESCRIPTORS_KEY, + (emptyApiKeyRoleDescriptor) + ? randomFrom(Arrays.asList(null, Collections.emptyMap())) + : Collections.singletonMap("a role", roleARDMap) + ); - final RoleDescriptor limitedRoleDescriptor = new RoleDescriptor("limited role", new String[] { "all" }, - new RoleDescriptor.IndicesPrivileges[] { - RoleDescriptor.IndicesPrivileges.builder().indices("*").privileges("all").build() }, - null); + final RoleDescriptor limitedRoleDescriptor = new RoleDescriptor( + "limited role", + new String[] { "all" }, + new RoleDescriptor.IndicesPrivileges[] { RoleDescriptor.IndicesPrivileges.builder().indices("*").privileges("all").build() }, + null + ); Map limitedRdMap; try (XContentBuilder builder = JsonXContent.contentBuilder()) { - limitedRdMap = XContentHelper.convertToMap(XContentType.JSON.xContent(), - BytesReference.bytes(limitedRoleDescriptor - .toXContent(builder, ToXContent.EMPTY_PARAMS, true)) - .streamInput(), - false); + limitedRdMap = XContentHelper.convertToMap( + XContentType.JSON.xContent(), + BytesReference.bytes(limitedRoleDescriptor.toXContent(builder, ToXContent.EMPTY_PARAMS, true)).streamInput(), + false + ); } authMetadata.put(API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY, Collections.singletonMap("limited role", limitedRdMap)); - final Authentication authentication = new Authentication(new User("joe"), new RealmRef("apikey", "apikey", "node"), null, + final Authentication authentication = new Authentication( + new User("joe"), + new RealmRef("apikey", "apikey", "node"), + null, VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, Version.V_7_8_1), - AuthenticationType.API_KEY, authMetadata); + AuthenticationType.API_KEY, + authMetadata + ); final NativePrivilegeStore privilegesStore = mock(NativePrivilegeStore.class); doAnswer(i -> { - assertThat(i.getArguments().length, equalTo(3)); - final Object arg2 = i.getArguments()[2]; - assertThat(arg2, instanceOf(ActionListener.class)); - ActionListener> listener = (ActionListener>) arg2; - listener.onResponse(Collections.emptyList()); - return null; - } - ).when(privilegesStore).getPrivileges(any(Collection.class), any(Collection.class), anyActionListener()); + assertThat(i.getArguments().length, equalTo(3)); + final Object arg2 = i.getArguments()[2]; + assertThat(arg2, instanceOf(ActionListener.class)); + ActionListener> listener = (ActionListener>) arg2; + listener.onResponse(Collections.emptyList()); + return null; + }).when(privilegesStore).getPrivileges(any(Collection.class), any(Collection.class), anyActionListener()); ApiKeyService service = createApiKeyService(Settings.EMPTY); PlainActionFuture roleFuture = new PlainActionFuture<>(); @@ -593,8 +681,14 @@ public void testGetApiKeyIdAndRoleBytes() { authMetadata.put(API_KEY_ROLE_DESCRIPTORS_KEY, roleBytes); authMetadata.put(API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY, limitedByRoleBytes); - final Authentication authentication = new Authentication(new User("joe"), new RealmRef("apikey", "apikey", "node"), null, - Version.CURRENT, AuthenticationType.API_KEY, authMetadata); + final Authentication authentication = new Authentication( + new User("joe"), + new RealmRef("apikey", "apikey", "node"), + null, + Version.CURRENT, + AuthenticationType.API_KEY, + authMetadata + ); ApiKeyService service = createApiKeyService(Settings.EMPTY); Tuple apiKeyIdAndRoleBytes = service.getApiKeyIdAndRoleBytes(authentication, false); @@ -620,24 +714,28 @@ public void testParseRoleDescriptors() { assertEquals(0, roleDescriptors.get(0).getApplicationPrivileges().length); roleBytes = new BytesArray( - "{\"reporting_user\":{\"cluster\":[],\"indices\":[],\"applications\":[],\"run_as\":[],\"metadata\":{\"_reserved\":true}," + - "\"transient_metadata\":{\"enabled\":true}},\"superuser\":{\"cluster\":[\"all\"],\"indices\":[{\"names\":[\"*\"]," + - "\"privileges\":[\"all\"],\"allow_restricted_indices\":true}],\"applications\":[{\"application\":\"*\"," + - "\"privileges\":[\"*\"],\"resources\":[\"*\"]}],\"run_as\":[\"*\"],\"metadata\":{\"_reserved\":true}," + - "\"transient_metadata\":{}}}\n"); + "{\"reporting_user\":{\"cluster\":[],\"indices\":[],\"applications\":[],\"run_as\":[],\"metadata\":{\"_reserved\":true}," + + "\"transient_metadata\":{\"enabled\":true}},\"superuser\":{\"cluster\":[\"all\"],\"indices\":[{\"names\":[\"*\"]," + + "\"privileges\":[\"all\"],\"allow_restricted_indices\":true}],\"applications\":[{\"application\":\"*\"," + + "\"privileges\":[\"*\"],\"resources\":[\"*\"]}],\"run_as\":[\"*\"],\"metadata\":{\"_reserved\":true}," + + "\"transient_metadata\":{}}}\n" + ); roleDescriptors = service.parseRoleDescriptors(apiKeyId, roleBytes); assertEquals(2, roleDescriptors.size()); assertEquals( Set.of("reporting_user", "superuser"), - roleDescriptors.stream().map(RoleDescriptor::getName).collect(Collectors.toSet())); + roleDescriptors.stream().map(RoleDescriptor::getName).collect(Collectors.toSet()) + ); } public void testApiKeyServiceDisabled() throws Exception { final Settings settings = Settings.builder().put(XPackSettings.API_KEY_SERVICE_ENABLED_SETTING.getKey(), false).build(); final ApiKeyService service = createApiKeyService(settings); - ElasticsearchException e = expectThrows(ElasticsearchException.class, - () -> service.getApiKeys(randomAlphaOfLength(6), randomAlphaOfLength(8), null, null, new PlainActionFuture<>())); + ElasticsearchException e = expectThrows( + ElasticsearchException.class, + () -> service.getApiKeys(randomAlphaOfLength(6), randomAlphaOfLength(8), null, null, new PlainActionFuture<>()) + ); assertThat(e, instanceOf(FeatureNotEnabledException.class)); // Older Kibana version looked for this exact text: @@ -703,7 +801,8 @@ public void testApiKeyCache() throws IOException { public void testApiKeyAuthCacheWillTraceLogOnEvictionDueToCacheSize() throws IllegalAccessException { final int cacheSize = randomIntBetween(2, 8); ApiKeyService service = createApiKeyService( - Settings.builder().put("xpack.security.authc.api_key.cache.max_keys", cacheSize).build()); + Settings.builder().put("xpack.security.authc.api_key.cache.max_keys", cacheSize).build() + ); final Cache> apiKeyAuthCache = service.getApiKeyAuthCache(); // Fill the cache @@ -717,28 +816,44 @@ public void testApiKeyAuthCacheWillTraceLogOnEvictionDueToCacheSize() throws Ill appender.start(); try { - appender.addExpectation(new MockLogAppender.PatternSeenEventExpectation( - "evict", ApiKeyService.class.getName(), Level.TRACE, - "API key with ID \\[" + idPrefix + "[0-9]+\\] was evicted from the authentication cache.*" - )); - appender.addExpectation(new MockLogAppender.UnseenEventExpectation( - "no-thrashing", ApiKeyService.class.getName(), Level.WARN, - "Possible thrashing for API key authentication cache,*" - )); + appender.addExpectation( + new MockLogAppender.PatternSeenEventExpectation( + "evict", + ApiKeyService.class.getName(), + Level.TRACE, + "API key with ID \\[" + idPrefix + "[0-9]+\\] was evicted from the authentication cache.*" + ) + ); + appender.addExpectation( + new MockLogAppender.UnseenEventExpectation( + "no-thrashing", + ApiKeyService.class.getName(), + Level.WARN, + "Possible thrashing for API key authentication cache,*" + ) + ); apiKeyAuthCache.put(idPrefix + count.incrementAndGet(), new ListenableFuture<>()); appender.assertAllExpectationsMatched(); - appender.addExpectation(new MockLogAppender.UnseenEventExpectation( - "replace", ApiKeyService.class.getName(), Level.TRACE, - "API key with ID [" + idPrefix + "*] was evicted from the authentication cache*" - )); + appender.addExpectation( + new MockLogAppender.UnseenEventExpectation( + "replace", + ApiKeyService.class.getName(), + Level.TRACE, + "API key with ID [" + idPrefix + "*] was evicted from the authentication cache*" + ) + ); apiKeyAuthCache.put(idPrefix + count.get(), new ListenableFuture<>()); appender.assertAllExpectationsMatched(); - appender.addExpectation(new MockLogAppender.UnseenEventExpectation( - "invalidate", ApiKeyService.class.getName(), Level.TRACE, - "API key with ID [" + idPrefix + "*] was evicted from the authentication cache*" - )); + appender.addExpectation( + new MockLogAppender.UnseenEventExpectation( + "invalidate", + ApiKeyService.class.getName(), + Level.TRACE, + "API key with ID [" + idPrefix + "*] was evicted from the authentication cache*" + ) + ); apiKeyAuthCache.invalidate(idPrefix + count.get(), new ListenableFuture<>()); apiKeyAuthCache.invalidateAll(); appender.assertAllExpectationsMatched(); @@ -750,10 +865,12 @@ public void testApiKeyAuthCacheWillTraceLogOnEvictionDueToCacheSize() throws Ill } public void testApiKeyCacheWillNotTraceLogOnEvictionDueToCacheTtl() throws IllegalAccessException, InterruptedException { - ApiKeyService service = createApiKeyService(Settings.builder() - .put("xpack.security.authc.api_key.cache.max_keys", 2) - .put("xpack.security.authc.api_key.cache.ttl", TimeValue.timeValueMillis(100)) - .build()); + ApiKeyService service = createApiKeyService( + Settings.builder() + .put("xpack.security.authc.api_key.cache.max_keys", 2) + .put("xpack.security.authc.api_key.cache.ttl", TimeValue.timeValueMillis(100)) + .build() + ); final Cache> apiKeyAuthCache = service.getApiKeyAuthCache(); final String apiKeyId = randomAlphaOfLength(22); @@ -764,10 +881,14 @@ public void testApiKeyCacheWillNotTraceLogOnEvictionDueToCacheTtl() throws Illeg appender.start(); try { - appender.addExpectation(new MockLogAppender.UnseenEventExpectation( - "evict", ApiKeyService.class.getName(), Level.TRACE, - "API key with ID [" + apiKeyId + "] was evicted from the authentication cache*" - )); + appender.addExpectation( + new MockLogAppender.UnseenEventExpectation( + "evict", + ApiKeyService.class.getName(), + Level.TRACE, + "API key with ID [" + apiKeyId + "] was evicted from the authentication cache*" + ) + ); apiKeyAuthCache.put(apiKeyId, new ListenableFuture<>()); // Wait for the entry to expire Thread.sleep(200); @@ -784,8 +905,7 @@ public void testApiKeyCacheWillNotTraceLogOnEvictionDueToCacheTtl() throws Illeg } public void testApiKeyAuthCacheWillLogWarningOnPossibleThrashing() throws Exception { - ApiKeyService service = createApiKeyService( - Settings.builder().put("xpack.security.authc.api_key.cache.max_keys", 2).build()); + ApiKeyService service = createApiKeyService(Settings.builder().put("xpack.security.authc.api_key.cache.max_keys", 2).build()); final Cache> apiKeyAuthCache = service.getApiKeyAuthCache(); // Fill the cache @@ -809,14 +929,22 @@ public void testApiKeyAuthCacheWillLogWarningOnPossibleThrashing() throws Except service.getLastEvictionCheckedAt().set(lastCheckedAt); // Ensure the counter is updated assertBusy(() -> assertThat(service.getEvictionCounter().longValue() >= 4500, is(true))); - appender.addExpectation(new MockLogAppender.SeenEventExpectation( - "evict", ApiKeyService.class.getName(), Level.TRACE, - "API key with ID [*] was evicted from the authentication cache*" - )); - appender.addExpectation(new MockLogAppender.SeenEventExpectation( - "thrashing", ApiKeyService.class.getName(), Level.WARN, - "Possible thrashing for API key authentication cache,*" - )); + appender.addExpectation( + new MockLogAppender.SeenEventExpectation( + "evict", + ApiKeyService.class.getName(), + Level.TRACE, + "API key with ID [*] was evicted from the authentication cache*" + ) + ); + appender.addExpectation( + new MockLogAppender.SeenEventExpectation( + "thrashing", + ApiKeyService.class.getName(), + Level.WARN, + "Possible thrashing for API key authentication cache,*" + ) + ); apiKeyAuthCache.put(randomAlphaOfLength(22), new ListenableFuture<>()); appender.assertAllExpectationsMatched(); @@ -825,14 +953,22 @@ public void testApiKeyAuthCacheWillLogWarningOnPossibleThrashing() throws Except assertBusy(() -> assertThat(service.getEvictionCounter().longValue(), equalTo(0L))); // Will not log warning again for the next eviction because of throttling - appender.addExpectation(new MockLogAppender.SeenEventExpectation( - "evict-again", ApiKeyService.class.getName(), Level.TRACE, - "API key with ID [*] was evicted from the authentication cache*" - )); - appender.addExpectation(new MockLogAppender.UnseenEventExpectation( - "throttling", ApiKeyService.class.getName(), Level.WARN, - "Possible thrashing for API key authentication cache,*" - )); + appender.addExpectation( + new MockLogAppender.SeenEventExpectation( + "evict-again", + ApiKeyService.class.getName(), + Level.TRACE, + "API key with ID [*] was evicted from the authentication cache*" + ) + ); + appender.addExpectation( + new MockLogAppender.UnseenEventExpectation( + "throttling", + ApiKeyService.class.getName(), + Level.WARN, + "Possible thrashing for API key authentication cache,*" + ) + ); apiKeyAuthCache.put(randomAlphaOfLength(23), new ListenableFuture<>()); appender.assertAllExpectationsMatched(); } finally { @@ -851,7 +987,7 @@ public void testAuthenticateWhileCacheBeingPopulated() throws Exception { final Object metadata = sourceMap.get("metadata_flattened"); ApiKeyService realService = createApiKeyService(Settings.EMPTY); - ApiKeyService service = Mockito.spy(realService); + ApiKeyService service = Mockito.spy(realService); // Used to block the hashing of the first api-key secret so that we can guarantee // that a second api key authentication takes place while hashing is "in progress". @@ -870,8 +1006,14 @@ public void testAuthenticateWhileCacheBeingPopulated() throws Exception { mockSourceDocument(apiKeyId, sourceMap); // This needs to be done in another thread, because we need it to not complete until we say so, but it should not block this test - this.threadPool.generic().execute(() -> service.tryAuthenticate(threadPool.getThreadContext(), - new ApiKeyCredentials(apiKeyId, new SecureString(apiKey.toCharArray())), future1)); + this.threadPool.generic() + .execute( + () -> service.tryAuthenticate( + threadPool.getThreadContext(), + new ApiKeyCredentials(apiKeyId, new SecureString(apiKey.toCharArray())), + future1 + ) + ); // Wait for the first credential validation to get to the blocked state assertBusy(() -> assertThat(hashCounter.get(), equalTo(1))); @@ -883,8 +1025,11 @@ public void testAuthenticateWhileCacheBeingPopulated() throws Exception { // The second authentication should pass (but not immediately, but will not block) PlainActionFuture> future2 = new PlainActionFuture<>(); - service.tryAuthenticate(threadPool.getThreadContext(), - new ApiKeyCredentials(apiKeyId, new SecureString(apiKey.toCharArray())), future2); + service.tryAuthenticate( + threadPool.getThreadContext(), + new ApiKeyCredentials(apiKeyId, new SecureString(apiKey.toCharArray())), + future2 + ); assertThat(hashCounter.get(), equalTo(1)); if (future2.isDone()) { @@ -911,9 +1056,7 @@ public void testApiKeyCacheDisabled() throws IOException { final String apiKey = randomAlphaOfLength(16); Hasher hasher = getFastStoredHashAlgoForTests(); final char[] hash = hasher.hash(new SecureString(apiKey.toCharArray())); - final Settings settings = Settings.builder() - .put(ApiKeyService.CACHE_TTL_SETTING.getKey(), "0s") - .build(); + final Settings settings = Settings.builder().put(ApiKeyService.CACHE_TTL_SETTING.getKey(), "0s").build(); ApiKeyDoc apiKeyDoc = buildApiKeyDoc(hash, -1, false); @@ -933,9 +1076,7 @@ public void testApiKeyDocCacheCanBeDisabledSeparately() throws IOException { final String apiKey = randomAlphaOfLength(16); Hasher hasher = getFastStoredHashAlgoForTests(); final char[] hash = hasher.hash(new SecureString(apiKey.toCharArray())); - final Settings settings = Settings.builder() - .put(ApiKeyService.DOC_CACHE_TTL_SETTING.getKey(), "0s") - .build(); + final Settings settings = Settings.builder().put(ApiKeyService.DOC_CACHE_TTL_SETTING.getKey(), "0s").build(); ApiKeyDoc apiKeyDoc = buildApiKeyDoc(hash, -1, false); @@ -962,19 +1103,24 @@ public void testApiKeyDocCache() throws IOException, ExecutionException, Interru final String docId = randomAlphaOfLength(16); final String apiKey = randomAlphaOfLength(16); ApiKeyCredentials apiKeyCredentials = new ApiKeyCredentials(docId, new SecureString(apiKey.toCharArray())); - final Map metadata = - mockKeyDocument(service, docId, apiKey, new User("hulk", "superuser"), false, Duration.ofSeconds(3600)); + final Map metadata = mockKeyDocument( + service, + docId, + apiKey, + new User("hulk", "superuser"), + false, + Duration.ofSeconds(3600) + ); PlainActionFuture> future = new PlainActionFuture<>(); service.loadApiKeyAndValidateCredentials(threadContext, apiKeyCredentials, future); final ApiKeyService.CachedApiKeyDoc cachedApiKeyDoc = service.getDocCache().get(docId); assertNotNull(cachedApiKeyDoc); assertEquals("hulk", cachedApiKeyDoc.creator.get("principal")); - final BytesReference roleDescriptorsBytes = - service.getRoleDescriptorsBytesCache().get(cachedApiKeyDoc.roleDescriptorsHash); + final BytesReference roleDescriptorsBytes = service.getRoleDescriptorsBytesCache().get(cachedApiKeyDoc.roleDescriptorsHash); assertNotNull(roleDescriptorsBytes); assertEquals("{}", roleDescriptorsBytes.utf8ToString()); - final BytesReference limitedByRoleDescriptorsBytes = - service.getRoleDescriptorsBytesCache().get(cachedApiKeyDoc.limitedByRoleDescriptorsHash); + final BytesReference limitedByRoleDescriptorsBytes = service.getRoleDescriptorsBytesCache() + .get(cachedApiKeyDoc.limitedByRoleDescriptorsHash); assertNotNull(limitedByRoleDescriptorsBytes); final List limitedByRoleDescriptors = service.parseRoleDescriptors(docId, limitedByRoleDescriptorsBytes); assertEquals(1, limitedByRoleDescriptors.size()); @@ -989,18 +1135,23 @@ public void testApiKeyDocCache() throws IOException, ExecutionException, Interru final String docId2 = randomAlphaOfLength(16); final String apiKey2 = randomAlphaOfLength(16); ApiKeyCredentials apiKeyCredentials2 = new ApiKeyCredentials(docId2, new SecureString(apiKey2.toCharArray())); - final Map metadata2 = - mockKeyDocument(service, docId2, apiKey2, new User("thor", "superuser"), false, Duration.ofSeconds(3600)); + final Map metadata2 = mockKeyDocument( + service, + docId2, + apiKey2, + new User("thor", "superuser"), + false, + Duration.ofSeconds(3600) + ); PlainActionFuture> future2 = new PlainActionFuture<>(); service.loadApiKeyAndValidateCredentials(threadContext, apiKeyCredentials2, future2); final ApiKeyService.CachedApiKeyDoc cachedApiKeyDoc2 = service.getDocCache().get(docId2); assertNotNull(cachedApiKeyDoc2); assertEquals("thor", cachedApiKeyDoc2.creator.get("principal")); - final BytesReference roleDescriptorsBytes2 = - service.getRoleDescriptorsBytesCache().get(cachedApiKeyDoc2.roleDescriptorsHash); + final BytesReference roleDescriptorsBytes2 = service.getRoleDescriptorsBytesCache().get(cachedApiKeyDoc2.roleDescriptorsHash); assertSame(roleDescriptorsBytes, roleDescriptorsBytes2); - final BytesReference limitedByRoleDescriptorsBytes2 = - service.getRoleDescriptorsBytesCache().get(cachedApiKeyDoc2.limitedByRoleDescriptorsHash); + final BytesReference limitedByRoleDescriptorsBytes2 = service.getRoleDescriptorsBytesCache() + .get(cachedApiKeyDoc2.limitedByRoleDescriptorsHash); assertSame(limitedByRoleDescriptorsBytes, limitedByRoleDescriptorsBytes2); if (metadata2 == null) { assertNull(cachedApiKeyDoc2.metadataFlattened); @@ -1012,18 +1163,28 @@ public void testApiKeyDocCache() throws IOException, ExecutionException, Interru final String docId3 = randomAlphaOfLength(16); final String apiKey3 = randomAlphaOfLength(16); ApiKeyCredentials apiKeyCredentials3 = new ApiKeyCredentials(docId3, new SecureString(apiKey3.toCharArray())); - final List keyRoles = - List.of(RoleDescriptor.parse("key-role", new BytesArray("{\"cluster\":[\"monitor\"]}"), true, XContentType.JSON)); - final Map metadata3 = - mockKeyDocument(service, docId3, apiKey3, new User("banner", "superuser"), false, Duration.ofSeconds(3600), keyRoles); + final List keyRoles = List.of( + RoleDescriptor.parse("key-role", new BytesArray("{\"cluster\":[\"monitor\"]}"), true, XContentType.JSON) + ); + final Map metadata3 = mockKeyDocument( + service, + docId3, + apiKey3, + new User("banner", "superuser"), + false, + Duration.ofSeconds(3600), + keyRoles + ); PlainActionFuture> future3 = new PlainActionFuture<>(); service.loadApiKeyAndValidateCredentials(threadContext, apiKeyCredentials3, future3); final ApiKeyService.CachedApiKeyDoc cachedApiKeyDoc3 = service.getDocCache().get(docId3); assertNotNull(cachedApiKeyDoc3); assertEquals("banner", cachedApiKeyDoc3.creator.get("principal")); // Shared bytes for limitedBy role since it is the same - assertSame(limitedByRoleDescriptorsBytes, - service.getRoleDescriptorsBytesCache().get(cachedApiKeyDoc3.limitedByRoleDescriptorsHash)); + assertSame( + limitedByRoleDescriptorsBytes, + service.getRoleDescriptorsBytesCache().get(cachedApiKeyDoc3.limitedByRoleDescriptorsHash) + ); // But role descriptors bytes are different final BytesReference roleDescriptorsBytes3 = service.getRoleDescriptorsBytesCache().get(cachedApiKeyDoc3.roleDescriptorsHash); assertNotSame(roleDescriptorsBytes, roleDescriptorsBytes3); @@ -1035,13 +1196,22 @@ public void testApiKeyDocCache() throws IOException, ExecutionException, Interru } // 4. Will fetch document from security index if role descriptors are not found even when - // cachedApiKeyDoc is available + // cachedApiKeyDoc is available service.getRoleDescriptorsBytesCache().invalidateAll(); - final Map metadata4 = - mockKeyDocument(service, docId, apiKey, new User("hulk", "superuser"), false, Duration.ofSeconds(3600)); + final Map metadata4 = mockKeyDocument( + service, + docId, + apiKey, + new User("hulk", "superuser"), + false, + Duration.ofSeconds(3600) + ); PlainActionFuture> future4 = new PlainActionFuture<>(); - service.loadApiKeyAndValidateCredentials(threadContext, - new ApiKeyCredentials(docId, new SecureString(apiKey.toCharArray())), future4); + service.loadApiKeyAndValidateCredentials( + threadContext, + new ApiKeyCredentials(docId, new SecureString(apiKey.toCharArray())), + future4 + ); verify(client, times(4)).get(any(GetRequest.class), anyActionListener()); assertEquals(2, service.getRoleDescriptorsBytesCache().count()); final AuthenticationResult authResult4 = future4.get(); @@ -1051,8 +1221,11 @@ public void testApiKeyDocCache() throws IOException, ExecutionException, Interru // 5. Cached entries will be used for the same API key doc SecurityMocks.mockGetRequestException(client, new EsRejectedExecutionException("rejected")); PlainActionFuture> future5 = new PlainActionFuture<>(); - service.loadApiKeyAndValidateCredentials(threadContext, - new ApiKeyCredentials(docId, new SecureString(apiKey.toCharArray())), future5); + service.loadApiKeyAndValidateCredentials( + threadContext, + new ApiKeyCredentials(docId, new SecureString(apiKey.toCharArray())), + future5 + ); final AuthenticationResult authResult5 = future5.get(); assertSame(AuthenticationResult.Status.SUCCESS, authResult5.getStatus()); checkAuthApiKeyMetadata(metadata4, authResult5); @@ -1066,9 +1239,22 @@ public void testWillInvalidateAuthCacheWhenDocNotFound() { ApiKeyCredentials apiKeyCredentials = new ApiKeyCredentials(docId, new SecureString(apiKey.toCharArray())); service.getApiKeyAuthCache().put(docId, new ListenableFuture<>()); assertNotNull(service.getApiKeyAuthCache().get(docId)); - SecurityMocks.mockGetRequest(client, SECURITY_MAIN_ALIAS, docId, - new GetResult(INTERNAL_SECURITY_MAIN_INDEX_7, docId, UNASSIGNED_SEQ_NO, UNASSIGNED_PRIMARY_TERM, - randomLongBetween(0, 9), false, null, null, null)); + SecurityMocks.mockGetRequest( + client, + SECURITY_MAIN_ALIAS, + docId, + new GetResult( + INTERNAL_SECURITY_MAIN_INDEX_7, + docId, + UNASSIGNED_SEQ_NO, + UNASSIGNED_PRIMARY_TERM, + randomLongBetween(0, 9), + false, + null, + null, + null + ) + ); PlainActionFuture> future = new PlainActionFuture<>(); service.loadApiKeyAndValidateCredentials(threadContext, apiKeyCredentials, future); assertNull(service.getApiKeyAuthCache().get(docId)); @@ -1077,16 +1263,14 @@ public void testWillInvalidateAuthCacheWhenDocNotFound() { public void testWillGetLookedUpByRealmNameIfExists() { final Authentication.RealmRef authenticatedBy = new Authentication.RealmRef("auth_by", "auth_by_type", "node"); final Authentication.RealmRef lookedUpBy = new Authentication.RealmRef("looked_up_by", "looked_up_by_type", "node"); - final Authentication authentication = new Authentication( - new User("user"), authenticatedBy, lookedUpBy); + final Authentication authentication = new Authentication(new User("user"), authenticatedBy, lookedUpBy); assertEquals("looked_up_by", ApiKeyService.getCreatorRealmName(authentication)); } public void testWillGetLookedUpByRealmTypeIfExists() { final Authentication.RealmRef authenticatedBy = new Authentication.RealmRef("auth_by", "auth_by_type", "node"); final Authentication.RealmRef lookedUpBy = new Authentication.RealmRef("looked_up_by", "looked_up_by_type", "node"); - final Authentication authentication = new Authentication( - new User("user"), authenticatedBy, lookedUpBy); + final Authentication authentication = new Authentication(new User("user"), authenticatedBy, lookedUpBy); assertEquals("looked_up_by_type", ApiKeyService.getCreatorRealmType(authentication)); } @@ -1162,7 +1346,7 @@ public void testCachedApiKeyValidationWillNotBeBlockedByUnCachedApiKey() throws service.tryAuthenticate(threadPool.getThreadContext(), creds, future); final AuthenticationResult authenticationResult = future.get(); assertEquals(AuthenticationResult.Status.SUCCESS, authenticationResult.getStatus()); - checkAuthApiKeyMetadata(metadata,authenticationResult); + checkAuthApiKeyMetadata(metadata, authenticationResult); // Now force the hashing thread pool to saturate so that any un-cached keys cannot be validated final ExecutorService mockExecutorService = mock(ExecutorService.class); @@ -1187,8 +1371,11 @@ public void testCachedApiKeyValidationWillNotBeBlockedByUnCachedApiKey() throws // The cached API key should not be affected mockSourceDocument(apiKeyId1, sourceMap); final PlainActionFuture> future3 = new PlainActionFuture<>(); - service.tryAuthenticate(threadPool.getThreadContext(), - new ApiKeyCredentials(apiKeyId1, new SecureString(apiKey1.toCharArray())), future3); + service.tryAuthenticate( + threadPool.getThreadContext(), + new ApiKeyCredentials(apiKeyId1, new SecureString(apiKey1.toCharArray())), + future3 + ); final AuthenticationResult authenticationResult3 = future3.get(); assertEquals(AuthenticationResult.Status.SUCCESS, authenticationResult3.getStatus()); checkAuthApiKeyMetadata(metadata, authenticationResult3); @@ -1197,16 +1384,20 @@ public void testCachedApiKeyValidationWillNotBeBlockedByUnCachedApiKey() throws @SuppressWarnings("unchecked") public void testApiKeyDocDeserialization() throws IOException { final String apiKeyDocumentSource = - "{\"doc_type\":\"api_key\",\"creation_time\":1591919944598,\"expiration_time\":1591919944599,\"api_key_invalidated\":false," + - "\"api_key_hash\":\"{PBKDF2}10000$abc\",\"role_descriptors\":{\"a\":{\"cluster\":[\"all\"]}}," + - "\"limited_by_role_descriptors\":{\"limited_by\":{\"cluster\":[\"all\"]," + - "\"metadata\":{\"_reserved\":true},\"type\":\"role\"}}," + - "\"name\":\"key-1\",\"version\":7000099," + - "\"creator\":{\"principal\":\"admin\",\"metadata\":{\"foo\":\"bar\"},\"realm\":\"file1\",\"realm_type\":\"file\"}}"; - final ApiKeyDoc apiKeyDoc = ApiKeyDoc.fromXContent(XContentHelper.createParser(NamedXContentRegistry.EMPTY, - LoggingDeprecationHandler.INSTANCE, - new BytesArray(apiKeyDocumentSource), - XContentType.JSON)); + "{\"doc_type\":\"api_key\",\"creation_time\":1591919944598,\"expiration_time\":1591919944599,\"api_key_invalidated\":false," + + "\"api_key_hash\":\"{PBKDF2}10000$abc\",\"role_descriptors\":{\"a\":{\"cluster\":[\"all\"]}}," + + "\"limited_by_role_descriptors\":{\"limited_by\":{\"cluster\":[\"all\"]," + + "\"metadata\":{\"_reserved\":true},\"type\":\"role\"}}," + + "\"name\":\"key-1\",\"version\":7000099," + + "\"creator\":{\"principal\":\"admin\",\"metadata\":{\"foo\":\"bar\"},\"realm\":\"file1\",\"realm_type\":\"file\"}}"; + final ApiKeyDoc apiKeyDoc = ApiKeyDoc.fromXContent( + XContentHelper.createParser( + NamedXContentRegistry.EMPTY, + LoggingDeprecationHandler.INSTANCE, + new BytesArray(apiKeyDocumentSource), + XContentType.JSON + ) + ); assertEquals("api_key", apiKeyDoc.docType); assertEquals(1591919944598L, apiKeyDoc.creationTime); assertEquals(1591919944599L, apiKeyDoc.expirationTime); @@ -1215,27 +1406,33 @@ public void testApiKeyDocDeserialization() throws IOException { assertEquals("key-1", apiKeyDoc.name); assertEquals(7000099, apiKeyDoc.version); assertEquals(new BytesArray("{\"a\":{\"cluster\":[\"all\"]}}"), apiKeyDoc.roleDescriptorsBytes); - assertEquals(new BytesArray("{\"limited_by\":{\"cluster\":[\"all\"],\"metadata\":{\"_reserved\":true},\"type\":\"role\"}}"), - apiKeyDoc.limitedByRoleDescriptorsBytes); + assertEquals( + new BytesArray("{\"limited_by\":{\"cluster\":[\"all\"],\"metadata\":{\"_reserved\":true},\"type\":\"role\"}}"), + apiKeyDoc.limitedByRoleDescriptorsBytes + ); final Map creator = apiKeyDoc.creator; assertEquals("admin", creator.get("principal")); assertEquals("file1", creator.get("realm")); assertEquals("file", creator.get("realm_type")); - assertEquals("bar", ((Map)creator.get("metadata")).get("foo")); + assertEquals("bar", ((Map) creator.get("metadata")).get("foo")); } public void testApiKeyDocDeserializationWithNullValues() throws IOException { final String apiKeyDocumentSource = - "{\"doc_type\":\"api_key\",\"creation_time\":1591919944598,\"expiration_time\":null,\"api_key_invalidated\":false," + - "\"api_key_hash\":\"{PBKDF2}10000$abc\",\"role_descriptors\":{}," + - "\"limited_by_role_descriptors\":{\"limited_by\":{\"cluster\":[\"all\"]}}," + - "\"name\":null,\"version\":7000099," + - "\"creator\":{\"principal\":\"admin\",\"metadata\":{},\"realm\":\"file1\"}}"; - final ApiKeyDoc apiKeyDoc = ApiKeyDoc.fromXContent(XContentHelper.createParser(NamedXContentRegistry.EMPTY, - LoggingDeprecationHandler.INSTANCE, - new BytesArray(apiKeyDocumentSource), - XContentType.JSON)); + "{\"doc_type\":\"api_key\",\"creation_time\":1591919944598,\"expiration_time\":null,\"api_key_invalidated\":false," + + "\"api_key_hash\":\"{PBKDF2}10000$abc\",\"role_descriptors\":{}," + + "\"limited_by_role_descriptors\":{\"limited_by\":{\"cluster\":[\"all\"]}}," + + "\"name\":null,\"version\":7000099," + + "\"creator\":{\"principal\":\"admin\",\"metadata\":{},\"realm\":\"file1\"}}"; + final ApiKeyDoc apiKeyDoc = ApiKeyDoc.fromXContent( + XContentHelper.createParser( + NamedXContentRegistry.EMPTY, + LoggingDeprecationHandler.INSTANCE, + new BytesArray(apiKeyDocumentSource), + XContentType.JSON + ) + ); assertEquals(-1L, apiKeyDoc.expirationTime); assertNull(apiKeyDoc.name); assertEquals(new BytesArray("{}"), apiKeyDoc.roleDescriptorsBytes); @@ -1261,9 +1458,12 @@ public void testGetApiKeyMetadata() throws IOException { final Authentication authentication = mock(Authentication.class); when(authentication.getAuthenticationType()).thenReturn( - randomValueOtherThan(AuthenticationType.API_KEY, () -> randomFrom(AuthenticationType.values()))); - final IllegalArgumentException e = - expectThrows(IllegalArgumentException.class, () -> ApiKeyService.getApiKeyMetadata(authentication)); + randomValueOtherThan(AuthenticationType.API_KEY, () -> randomFrom(AuthenticationType.values())) + ); + final IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> ApiKeyService.getApiKeyMetadata(authentication) + ); assertThat(e.getMessage(), containsString("authentication type must be [api_key]")); } @@ -1271,22 +1471,39 @@ public static class Utils { private static final AuthenticationContextSerializer authenticationContextSerializer = new AuthenticationContextSerializer(); - public static Authentication createApiKeyAuthentication(ApiKeyService apiKeyService, - Authentication authentication, - Set userRoles, - List keyRoles, - Version version) throws Exception { + public static Authentication createApiKeyAuthentication( + ApiKeyService apiKeyService, + Authentication authentication, + Set userRoles, + List keyRoles, + Version version + ) throws Exception { XContentBuilder keyDocSource = apiKeyService.newDocument( - getFastStoredHashAlgoForTests().hash(new SecureString(randomAlphaOfLength(16).toCharArray())), "test", authentication, - userRoles, Instant.now(), Instant.now().plus(Duration.ofSeconds(3600)), keyRoles, Version.CURRENT, - randomBoolean() ? null : Map.of(randomAlphaOfLengthBetween(3, 8), randomAlphaOfLengthBetween(3, 8))); + getFastStoredHashAlgoForTests().hash(new SecureString(randomAlphaOfLength(16).toCharArray())), + "test", + authentication, + userRoles, + Instant.now(), + Instant.now().plus(Duration.ofSeconds(3600)), + keyRoles, + Version.CURRENT, + randomBoolean() ? null : Map.of(randomAlphaOfLengthBetween(3, 8), randomAlphaOfLengthBetween(3, 8)) + ); final ApiKeyDoc apiKeyDoc = ApiKeyDoc.fromXContent( - XContentHelper.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, - BytesReference.bytes(keyDocSource), XContentType.JSON)); + XContentHelper.createParser( + NamedXContentRegistry.EMPTY, + LoggingDeprecationHandler.INSTANCE, + BytesReference.bytes(keyDocSource), + XContentType.JSON + ) + ); PlainActionFuture> authenticationResultFuture = PlainActionFuture.newFuture(); - apiKeyService.validateApiKeyExpiration(apiKeyDoc, new ApiKeyService.ApiKeyCredentials("id", - new SecureString(randomAlphaOfLength(16).toCharArray())), - Clock.systemUTC(), authenticationResultFuture); + apiKeyService.validateApiKeyExpiration( + apiKeyDoc, + new ApiKeyService.ApiKeyCredentials("id", new SecureString(randomAlphaOfLength(16).toCharArray())), + Clock.systemUTC(), + authenticationResultFuture + ); AuthenticationResult authenticationResult = authenticationResultFuture.get(); if (randomBoolean()) { @@ -1307,7 +1524,9 @@ public static Authentication createApiKeyAuthentication(ApiKeyService apiKeyServ final ThreadContext threadContext = new ThreadContext(Settings.EMPTY); final SecurityContext securityContext = new SecurityContext(Settings.EMPTY, threadContext); authenticationContextSerializer.writeToContext( - apiKeyService.createApiKeyAuthentication(authenticationResult, "node01"), threadContext); + apiKeyService.createApiKeyAuthentication(authenticationResult, "node01"), + threadContext + ); final CompletableFuture authFuture = new CompletableFuture<>(); securityContext.executeAfterRewritingAuthentication((c) -> { try { @@ -1319,11 +1538,15 @@ public static Authentication createApiKeyAuthentication(ApiKeyService apiKeyServ return authFuture.get(); } - public static Authentication createApiKeyAuthentication(ApiKeyService apiKeyService, - Authentication authentication) throws Exception { - return createApiKeyAuthentication(apiKeyService, authentication, - Collections.singleton(new RoleDescriptor("user_role_" + randomAlphaOfLength(4), new String[]{"manage"}, null, null)), - null, Version.CURRENT); + public static Authentication createApiKeyAuthentication(ApiKeyService apiKeyService, Authentication authentication) + throws Exception { + return createApiKeyAuthentication( + apiKeyService, + authentication, + Collections.singleton(new RoleDescriptor("user_role_" + randomAlphaOfLength(4), new String[] { "manage" }, null, null)), + null, + Version.CURRENT + ); } } @@ -1338,9 +1561,14 @@ private ApiKeyService createApiKeyService(Settings baseSettings) { .put(baseSettings) .build(); final ApiKeyService service = new ApiKeyService( - settings, Clock.systemUTC(), client, securityIndex, + settings, + Clock.systemUTC(), + client, + securityIndex, ClusterServiceUtils.createClusterService(threadPool), - cacheInvalidatorRegistry, threadPool); + cacheInvalidatorRegistry, + threadPool + ); if ("0s".equals(settings.get(ApiKeyService.CACHE_TTL_SETTING.getKey()))) { verify(cacheInvalidatorRegistry, never()).registerCacheInvalidator(eq("api_key"), any()); } else { @@ -1366,15 +1594,18 @@ private Map buildApiKeySourceDoc(char[] hash) { creatorMap.put("metadata", Collections.emptyMap()); sourceMap.put("creator", creatorMap); sourceMap.put("api_key_invalidated", false); - //noinspection unchecked + // noinspection unchecked sourceMap.put("metadata_flattened", ApiKeyTests.randomMetadata()); return sourceMap; } private void writeCredentialsToThreadContext(ApiKeyCredentials creds) { final String credentialString = creds.getId() + ":" + creds.getKey(); - this.threadPool.getThreadContext().putHeader("Authorization", - "ApiKey " + Base64.getEncoder().encodeToString(credentialString.getBytes(StandardCharsets.US_ASCII))); + this.threadPool.getThreadContext() + .putHeader( + "Authorization", + "ApiKey " + Base64.getEncoder().encodeToString(credentialString.getBytes(StandardCharsets.US_ASCII)) + ); } private void mockSourceDocument(String id, Map sourceMap) throws IOException { @@ -1385,8 +1616,7 @@ private void mockSourceDocument(String id, Map sourceMap) throws } private ApiKeyDoc buildApiKeyDoc(char[] hash, long expirationTime, boolean invalidated) throws IOException { - final BytesReference metadataBytes = - XContentTestUtils.convertToXContent(ApiKeyTests.randomMetadata(), XContentType.JSON); + final BytesReference metadataBytes = XContentTestUtils.convertToXContent(ApiKeyTests.randomMetadata(), XContentType.JSON); return new ApiKeyDoc( "api_key", Clock.systemUTC().instant().toEpochMilli(), @@ -1398,12 +1628,18 @@ private ApiKeyDoc buildApiKeyDoc(char[] hash, long expirationTime, boolean inval new BytesArray("{\"a role\": {\"cluster\": [\"all\"]}}"), new BytesArray("{\"limited role\": {\"cluster\": [\"all\"]}}"), Map.of( - "principal", "test_user", - "full_name", "test user", - "email", "test@user.com", - "realm", "realm1", - "realm_type", "realm_type1", - "metadata", Map.of() + "principal", + "test_user", + "full_name", + "test user", + "email", + "test@user.com", + "realm", + "realm1", + "realm_type", + "realm_type1", + "metadata", + Map.of() ), metadataBytes ); @@ -1416,7 +1652,8 @@ private void checkAuthApiKeyMetadata(Object metadata, AuthenticationResult } else { assertThat( authResult1.getMetadata().get(API_KEY_METADATA_KEY), - equalTo(XContentTestUtils.convertToXContent((Map) metadata, XContentType.JSON))); + equalTo(XContentTestUtils.convertToXContent((Map) metadata, XContentType.JSON)) + ); } } } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/AuthenticationServiceTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/AuthenticationServiceTests.java index a65ab372a5e2a..c932e53fef5f1 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/AuthenticationServiceTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/AuthenticationServiceTests.java @@ -145,11 +145,11 @@ import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.Matchers.sameInstance; -import static org.mockito.ArgumentMatchers.nullable; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.argThat; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.nullable; import static org.mockito.ArgumentMatchers.same; import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.doAnswer; @@ -164,7 +164,6 @@ import static org.mockito.Mockito.verifyZeroInteractions; import static org.mockito.Mockito.when; - /** * Unit tests for the {@link AuthenticationService} */ @@ -198,7 +197,9 @@ public class AuthenticationServiceTests extends ESTestCase { @SuppressForbidden(reason = "Allow accessing localhost") public void init() throws Exception { concreteSecurityIndexName = randomFrom( - RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_6, RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7); + RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_6, + RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7 + ); token = mock(AuthenticationToken.class); when(token.principal()).thenReturn(randomAlphaOfLength(5)); @@ -238,10 +239,18 @@ public void init() throws Exception { ReservedRealm reservedRealm = mock(ReservedRealm.class); when(reservedRealm.type()).thenReturn("reserved"); when(reservedRealm.name()).thenReturn("reserved_realm"); - realms = spy(new TestRealms(Settings.EMPTY, TestEnvironment.newEnvironment(settings), - Map.of(FileRealmSettings.TYPE, this::mockRealm, NativeRealmSettings.TYPE, this::mockRealm), - licenseState, threadContext, reservedRealm, Arrays.asList(firstRealm, secondRealm), - Arrays.asList(firstRealm))); + realms = spy( + new TestRealms( + Settings.EMPTY, + TestEnvironment.newEnvironment(settings), + Map.of(FileRealmSettings.TYPE, this::mockRealm, NativeRealmSettings.TYPE, this::mockRealm), + licenseState, + threadContext, + reservedRealm, + Arrays.asList(firstRealm, secondRealm), + Arrays.asList(firstRealm) + ) + ); // Needed because this is calculated in the constructor, which means the override doesn't get called correctly realms.recomputeActiveRealms(); @@ -250,30 +259,36 @@ public void init() throws Exception { auditTrail = mock(AuditTrail.class); auditTrailService = new AuditTrailService(Collections.singletonList(auditTrail), licenseState); client = mock(Client.class); - threadPool = new ThreadPool(settings, - new FixedExecutorBuilder(settings, THREAD_POOL_NAME, 1, 1000, - "xpack.security.authc.token.thread_pool", false), - new FixedExecutorBuilder(Settings.EMPTY, SECURITY_CRYPTO_THREAD_POOL_NAME, 1, 1000, - "xpack.security.crypto.thread_pool", false) + threadPool = new ThreadPool( + settings, + new FixedExecutorBuilder(settings, THREAD_POOL_NAME, 1, 1000, "xpack.security.authc.token.thread_pool", false), + new FixedExecutorBuilder(Settings.EMPTY, SECURITY_CRYPTO_THREAD_POOL_NAME, 1, 1000, "xpack.security.crypto.thread_pool", false) ); threadContext = threadPool.getThreadContext(); when(client.threadPool()).thenReturn(threadPool); when(client.settings()).thenReturn(settings); - when(client.prepareIndex(nullable(String.class))) - .thenReturn(new IndexRequestBuilder(client, IndexAction.INSTANCE)); - when(client.prepareUpdate(nullable(String.class), nullable(String.class))) - .thenReturn(new UpdateRequestBuilder(client, UpdateAction.INSTANCE)); + when(client.prepareIndex(nullable(String.class))).thenReturn(new IndexRequestBuilder(client, IndexAction.INSTANCE)); + when(client.prepareUpdate(nullable(String.class), nullable(String.class))).thenReturn( + new UpdateRequestBuilder(client, UpdateAction.INSTANCE) + ); doAnswer(invocationOnMock -> { @SuppressWarnings("unchecked") ActionListener responseActionListener = (ActionListener) invocationOnMock.getArguments()[2]; - responseActionListener.onResponse(new IndexResponse(new ShardId(".security", UUIDs.randomBase64UUID(), randomInt()), - randomAlphaOfLength(4), randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), true)); + responseActionListener.onResponse( + new IndexResponse( + new ShardId(".security", UUIDs.randomBase64UUID(), randomInt()), + randomAlphaOfLength(4), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + true + ) + ); return null; }).when(client).execute(eq(IndexAction.INSTANCE), any(IndexRequest.class), anyActionListener()); doAnswer(invocationOnMock -> { GetRequestBuilder builder = new GetRequestBuilder(client, GetAction.INSTANCE); - builder.setIndex((String) invocationOnMock.getArguments()[0]) - .setId((String) invocationOnMock.getArguments()[1]); + builder.setIndex((String) invocationOnMock.getArguments()[0]).setId((String) invocationOnMock.getArguments()[1]); return builder; }).when(client).prepareGet(nullable(String.class), nullable(String.class)); securityIndex = mock(SecurityIndexManager.class); @@ -289,10 +304,25 @@ public void init() throws Exception { }).when(securityIndex).checkIndexVersionThenExecute(anyConsumer(), any(Runnable.class)); ClusterService clusterService = ClusterServiceUtils.createClusterService(threadPool); final SecurityContext securityContext = new SecurityContext(settings, threadContext); - apiKeyService = new ApiKeyService(settings, Clock.systemUTC(), client, securityIndex, clusterService, - mock(CacheInvalidatorRegistry.class), threadPool); - tokenService = new TokenService(settings, Clock.systemUTC(), client, licenseState, securityContext, securityIndex, securityIndex, - clusterService); + apiKeyService = new ApiKeyService( + settings, + Clock.systemUTC(), + client, + securityIndex, + clusterService, + mock(CacheInvalidatorRegistry.class), + threadPool + ); + tokenService = new TokenService( + settings, + Clock.systemUTC(), + client, + licenseState, + securityContext, + securityIndex, + securityIndex, + clusterService + ); serviceAccountService = mock(ServiceAccountService.class); doAnswer(invocationOnMock -> { @SuppressWarnings("unchecked") @@ -302,10 +332,18 @@ public void init() throws Exception { }).when(serviceAccountService).authenticateToken(any(), any(), any()); operatorPrivilegesService = mock(OperatorPrivileges.OperatorPrivilegesService.class); - service = new AuthenticationService(settings, realms, auditTrailService, + service = new AuthenticationService( + settings, + realms, + auditTrailService, new DefaultAuthenticationFailureHandler(Collections.emptyMap()), - threadPool, new AnonymousUser(settings), tokenService, apiKeyService, serviceAccountService, - operatorPrivilegesService); + threadPool, + new AnonymousUser(settings), + tokenService, + apiKeyService, + serviceAccountService, + operatorPrivilegesService + ); } private Realm mockRealm(RealmConfig config) { @@ -344,8 +382,8 @@ public void testTokenFirstMissingSecondFound() throws Exception { doAnswer(invocationOnMock -> { final Object[] arguments = invocationOnMock.getArguments(); assertThat(arguments[0], is(token)); - @SuppressWarnings("unchecked") final ActionListener> listener = - (ActionListener>) arguments[1]; + @SuppressWarnings("unchecked") + final ActionListener> listener = (ActionListener>) arguments[1]; listener.onResponse(authenticationResult); return null; }).when(secondRealm).authenticate(eq(token), anyActionListener()); @@ -367,12 +405,15 @@ public void testTokenMissing() throws Exception { mockAppender.start(); try { Loggers.addAppender(unlicensedRealmsLogger, mockAppender); - mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation( - "unlicensed realms", - RealmsAuthenticator.class.getName(), Level.WARN, - "No authentication credential could be extracted using realms [file_realm/file]. " + - "Realms [second_realm/second] were skipped because they are not permitted on the current license" - )); + mockAppender.addExpectation( + new MockLogAppender.SeenEventExpectation( + "unlicensed realms", + RealmsAuthenticator.class.getName(), + Level.WARN, + "No authentication credential could be extracted using realms [file_realm/file]. " + + "Realms [second_realm/second] were skipped because they are not permitted on the current license" + ) + ); Mockito.doReturn(List.of(secondRealm)).when(realms).getUnlicensedRealms(); Mockito.doReturn(List.of(firstRealm)).when(realms).getActiveRealms(); @@ -386,9 +427,7 @@ public void testTokenMissing() throws Exception { final boolean isRestRequest = randomBoolean() && false == requestIdAlreadyPresent; final AtomicBoolean completed = new AtomicBoolean(false); - final ActionListener listener = ActionListener.wrap(authentication -> { - fail("should not reach here"); - }, e -> { + final ActionListener listener = ActionListener.wrap(authentication -> { fail("should not reach here"); }, e -> { if (requestIdAlreadyPresent) { assertThat(expectAuditRequestId(threadContext), is(reqId.get())); } else { @@ -583,19 +622,27 @@ public void testCacheClearOnSecurityIndexChange() { // green to yellow or yellow to green previousState = dummyState(randomFrom(ClusterHealthStatus.GREEN, ClusterHealthStatus.YELLOW)); - currentState = dummyState(previousState.indexHealth == ClusterHealthStatus.GREEN ? - ClusterHealthStatus.YELLOW : ClusterHealthStatus.GREEN); + currentState = dummyState( + previousState.indexHealth == ClusterHealthStatus.GREEN ? ClusterHealthStatus.YELLOW : ClusterHealthStatus.GREEN + ); service.onSecurityIndexStateChange(previousState, currentState); assertEquals(expectedInvalidation, service.getNumInvalidation()); } public void testAuthenticateSmartRealmOrderingDisabled() { - final Settings settings = Settings.builder() - .put(AuthenticationService.SUCCESS_AUTH_CACHE_ENABLED.getKey(), false) - .build(); - service = new AuthenticationService(settings, realms, auditTrailService, - new DefaultAuthenticationFailureHandler(Collections.emptyMap()), threadPool, new AnonymousUser(Settings.EMPTY), - tokenService, apiKeyService, serviceAccountService, operatorPrivilegesService); + final Settings settings = Settings.builder().put(AuthenticationService.SUCCESS_AUTH_CACHE_ENABLED.getKey(), false).build(); + service = new AuthenticationService( + settings, + realms, + auditTrailService, + new DefaultAuthenticationFailureHandler(Collections.emptyMap()), + threadPool, + new AnonymousUser(Settings.EMPTY), + tokenService, + apiKeyService, + serviceAccountService, + operatorPrivilegesService + ); User user = new User("_username", "r1"); when(firstRealm.supports(token)).thenReturn(true); mockAuthenticate(firstRealm, token, null); @@ -710,8 +757,9 @@ public void testAuthenticateCached() throws Exception { } public void testAuthenticateNonExistentRestRequestUserThrowsAuthenticationException() throws Exception { - when(firstRealm.token(threadContext)).thenReturn(new UsernamePasswordToken("idonotexist", - new SecureString("passwd".toCharArray()))); + when(firstRealm.token(threadContext)).thenReturn( + new UsernamePasswordToken("idonotexist", new SecureString("passwd".toCharArray())) + ); try { authenticateBlocking(restRequest, null); fail("Authentication was successful but should not"); @@ -773,7 +821,7 @@ public void testAuthenticateTransportAnonymous() throws Exception { verify(auditTrail).anonymousAccessDenied(reqId.get(), "_action", transportRequest); } - public void testAuthenticateRestAnonymous() throws Exception { + public void testAuthenticateRestAnonymous() throws Exception { when(firstRealm.token(threadContext)).thenReturn(null); when(secondRealm.token(threadContext)).thenReturn(null); try { @@ -822,8 +870,10 @@ public void testAuthenticateTransportDisabledUser() throws Exception { when(firstRealm.supports(token)).thenReturn(true); mockAuthenticate(firstRealm, token, user); - ElasticsearchSecurityException e = - expectThrows(ElasticsearchSecurityException.class, () -> authenticateBlocking("_action", transportRequest, fallback, null)); + ElasticsearchSecurityException e = expectThrows( + ElasticsearchSecurityException.class, + () -> authenticateBlocking("_action", transportRequest, fallback, null) + ); if (requestIdAlreadyPresent) { assertThat(expectAuditRequestId(threadContext), is(reqId.get())); } else { @@ -841,8 +891,10 @@ public void testAuthenticateRestDisabledUser() throws Exception { when(firstRealm.supports(token)).thenReturn(true); mockAuthenticate(firstRealm, token, user); - ElasticsearchSecurityException e = - expectThrows(ElasticsearchSecurityException.class, () -> authenticateBlocking(restRequest, null)); + ElasticsearchSecurityException e = expectThrows( + ElasticsearchSecurityException.class, + () -> authenticateBlocking(restRequest, null) + ); String reqId = expectAuditRequestId(threadContext); verify(auditTrail).authenticationFailed(reqId, token, restRequest); verifyNoMoreInteractions(auditTrail); @@ -925,20 +977,20 @@ public void testAuthenticateTransportContextAndHeader() throws Exception { reqId.set(AuditUtil.getOrGenerateRequestId(threadContext)); } service.authenticate("_action", transportRequest, SystemUser.INSTANCE, ActionListener.wrap(authentication -> { - if (requestIdAlreadyPresent) { - assertThat(expectAuditRequestId(threadContext), is(reqId.get())); - } else { - reqId.set(expectAuditRequestId(threadContext)); - } - assertThat(authentication, notNullValue()); - assertThat(authentication.getUser(), sameInstance(user1)); - assertThat(authentication.getAuthenticationType(), is(AuthenticationType.REALM)); - assertThreadContextContainsAuthentication(authentication); - authRef.set(authentication); - authHeaderRef.set(threadContext.getHeader(AuthenticationField.AUTHENTICATION_KEY)); - setCompletedToTrue(completed); - verify(operatorPrivilegesService).maybeMarkOperatorUser(eq(authentication), eq(threadContext)); - }, this::logAndFail)); + if (requestIdAlreadyPresent) { + assertThat(expectAuditRequestId(threadContext), is(reqId.get())); + } else { + reqId.set(expectAuditRequestId(threadContext)); + } + assertThat(authentication, notNullValue()); + assertThat(authentication.getUser(), sameInstance(user1)); + assertThat(authentication.getAuthenticationType(), is(AuthenticationType.REALM)); + assertThreadContextContainsAuthentication(authentication); + authRef.set(authentication); + authHeaderRef.set(threadContext.getHeader(AuthenticationField.AUTHENTICATION_KEY)); + setCompletedToTrue(completed); + verify(operatorPrivilegesService).maybeMarkOperatorUser(eq(authentication), eq(threadContext)); + }, this::logAndFail)); } assertTrue(completed.compareAndSet(true, false)); reset(firstRealm); @@ -949,9 +1001,18 @@ public void testAuthenticateTransportContextAndHeader() throws Exception { Mockito.reset(operatorPrivilegesService); try { ThreadContext threadContext1 = threadPool1.getThreadContext(); - service = new AuthenticationService(Settings.EMPTY, realms, auditTrailService, - new DefaultAuthenticationFailureHandler(Collections.emptyMap()), threadPool1, new AnonymousUser(Settings.EMPTY), - tokenService, apiKeyService, serviceAccountService, operatorPrivilegesService); + service = new AuthenticationService( + Settings.EMPTY, + realms, + auditTrailService, + new DefaultAuthenticationFailureHandler(Collections.emptyMap()), + threadPool1, + new AnonymousUser(Settings.EMPTY), + tokenService, + apiKeyService, + serviceAccountService, + operatorPrivilegesService + ); boolean requestIdAlreadyPresent = randomBoolean(); SetOnce reqId = new SetOnce<>(); if (requestIdAlreadyPresent) { @@ -961,16 +1022,16 @@ public void testAuthenticateTransportContextAndHeader() throws Exception { threadContext1.putTransient(AuthenticationField.AUTHENTICATION_KEY, authRef.get()); threadContext1.putHeader(AuthenticationField.AUTHENTICATION_KEY, authHeaderRef.get()); service.authenticate("_action", message1, SystemUser.INSTANCE, ActionListener.wrap(ctxAuth -> { - if (requestIdAlreadyPresent) { - assertThat(expectAuditRequestId(threadContext1), is(reqId.get())); - } else { - reqId.set(expectAuditRequestId(threadContext1)); - } - assertThat(ctxAuth, sameInstance(authRef.get())); - assertThat(threadContext1.getHeader(AuthenticationField.AUTHENTICATION_KEY), sameInstance(authHeaderRef.get())); - setCompletedToTrue(completed); - verify(operatorPrivilegesService, times(1)).maybeMarkOperatorUser(ctxAuth, threadContext1); - }, this::logAndFail)); + if (requestIdAlreadyPresent) { + assertThat(expectAuditRequestId(threadContext1), is(reqId.get())); + } else { + reqId.set(expectAuditRequestId(threadContext1)); + } + assertThat(ctxAuth, sameInstance(authRef.get())); + assertThat(threadContext1.getHeader(AuthenticationField.AUTHENTICATION_KEY), sameInstance(authHeaderRef.get())); + setCompletedToTrue(completed); + verify(operatorPrivilegesService, times(1)).maybeMarkOperatorUser(ctxAuth, threadContext1); + }, this::logAndFail)); assertTrue(completed.compareAndSet(true, false)); verifyNoMoreInteractions(firstRealm); reset(firstRealm); @@ -990,9 +1051,18 @@ public void testAuthenticateTransportContextAndHeader() throws Exception { } final String header; try (ThreadContext.StoredContext ignore = threadContext2.stashContext()) { - service = new AuthenticationService(Settings.EMPTY, realms, auditTrailService, - new DefaultAuthenticationFailureHandler(Collections.emptyMap()), threadPool2, new AnonymousUser(Settings.EMPTY), - tokenService, apiKeyService, serviceAccountService, operatorPrivilegesService); + service = new AuthenticationService( + Settings.EMPTY, + realms, + auditTrailService, + new DefaultAuthenticationFailureHandler(Collections.emptyMap()), + threadPool2, + new AnonymousUser(Settings.EMPTY), + tokenService, + apiKeyService, + serviceAccountService, + operatorPrivilegesService + ); threadContext2.putHeader(AuthenticationField.AUTHENTICATION_KEY, authHeaderRef.get()); BytesStreamOutput output = new BytesStreamOutput(); @@ -1004,21 +1074,30 @@ public void testAuthenticateTransportContextAndHeader() throws Exception { } threadPool2.getThreadContext().putHeader(AuthenticationField.AUTHENTICATION_KEY, header); - service = new AuthenticationService(Settings.EMPTY, realms, auditTrailService, - new DefaultAuthenticationFailureHandler(Collections.emptyMap()), threadPool2, new AnonymousUser(Settings.EMPTY), - tokenService, apiKeyService, serviceAccountService, operatorPrivilegesService); + service = new AuthenticationService( + Settings.EMPTY, + realms, + auditTrailService, + new DefaultAuthenticationFailureHandler(Collections.emptyMap()), + threadPool2, + new AnonymousUser(Settings.EMPTY), + tokenService, + apiKeyService, + serviceAccountService, + operatorPrivilegesService + ); service.authenticate("_action", new InternalRequest(), SystemUser.INSTANCE, ActionListener.wrap(result -> { - if (requestIdAlreadyPresent) { - assertThat(expectAuditRequestId(threadPool2.getThreadContext()), is(reqId.get())); - } else { - reqId.set(expectAuditRequestId(threadPool2.getThreadContext())); - } - assertThat(result, notNullValue()); - assertThat(result.getUser(), equalTo(user1)); - assertThat(result.getAuthenticationType(), is(AuthenticationType.REALM)); - setCompletedToTrue(completed); - verify(operatorPrivilegesService, times(1)).maybeMarkOperatorUser(result, threadPool2.getThreadContext()); - }, this::logAndFail)); + if (requestIdAlreadyPresent) { + assertThat(expectAuditRequestId(threadPool2.getThreadContext()), is(reqId.get())); + } else { + reqId.set(expectAuditRequestId(threadPool2.getThreadContext())); + } + assertThat(result, notNullValue()); + assertThat(result.getUser(), equalTo(user1)); + assertThat(result.getAuthenticationType(), is(AuthenticationType.REALM)); + setCompletedToTrue(completed); + verify(operatorPrivilegesService, times(1)).maybeMarkOperatorUser(result, threadPool2.getThreadContext()); + }, this::logAndFail)); assertTrue(completed.get()); verifyNoMoreInteractions(firstRealm); } finally { @@ -1037,7 +1116,7 @@ public void testAuthenticateTamperedUser() throws Exception { try { authenticateBlocking("_action", message, randomBoolean() ? SystemUser.INSTANCE : null, null); } catch (Exception e) { - //expected + // expected if (requestIdAlreadyPresent) { assertThat(expectAuditRequestId(threadContext), is(reqId.get())); } else { @@ -1051,16 +1130,24 @@ public void testAuthenticateTamperedUser() throws Exception { public void testWrongTokenDoesNotFallbackToAnonymous() { String username = randomBoolean() ? AnonymousUser.DEFAULT_ANONYMOUS_USERNAME : "user1"; - Settings.Builder builder = Settings.builder() - .putList(AnonymousUser.ROLES_SETTING.getKey(), "r1", "r2", "r3"); + Settings.Builder builder = Settings.builder().putList(AnonymousUser.ROLES_SETTING.getKey(), "r1", "r2", "r3"); if (username.equals(AnonymousUser.DEFAULT_ANONYMOUS_USERNAME) == false) { builder.put(AnonymousUser.USERNAME_SETTING.getKey(), username); } Settings anonymousEnabledSettings = builder.build(); final AnonymousUser anonymousUser = new AnonymousUser(anonymousEnabledSettings); - service = new AuthenticationService(anonymousEnabledSettings, realms, auditTrailService, - new DefaultAuthenticationFailureHandler(Collections.emptyMap()), threadPool, anonymousUser, - tokenService, apiKeyService, serviceAccountService, operatorPrivilegesService); + service = new AuthenticationService( + anonymousEnabledSettings, + realms, + auditTrailService, + new DefaultAuthenticationFailureHandler(Collections.emptyMap()), + threadPool, + anonymousUser, + tokenService, + apiKeyService, + serviceAccountService, + operatorPrivilegesService + ); try (ThreadContext.StoredContext ignore = threadContext.stashContext()) { boolean requestIdAlreadyPresent = randomBoolean(); @@ -1069,8 +1156,10 @@ public void testWrongTokenDoesNotFallbackToAnonymous() { reqId.set(AuditUtil.getOrGenerateRequestId(threadContext)); } threadContext.putHeader("Authorization", "Bearer thisisaninvalidtoken"); - ElasticsearchSecurityException e = - expectThrows(ElasticsearchSecurityException.class, () -> authenticateBlocking("_action", transportRequest, null, null)); + ElasticsearchSecurityException e = expectThrows( + ElasticsearchSecurityException.class, + () -> authenticateBlocking("_action", transportRequest, null, null) + ); if (requestIdAlreadyPresent) { assertThat(expectAuditRequestId(threadContext), is(reqId.get())); } else { @@ -1085,23 +1174,43 @@ public void testWrongTokenDoesNotFallbackToAnonymous() { public void testWrongApiKeyDoesNotFallbackToAnonymous() { String username = randomBoolean() ? AnonymousUser.DEFAULT_ANONYMOUS_USERNAME : "user1"; - Settings.Builder builder = Settings.builder() - .putList(AnonymousUser.ROLES_SETTING.getKey(), "r1", "r2", "r3"); + Settings.Builder builder = Settings.builder().putList(AnonymousUser.ROLES_SETTING.getKey(), "r1", "r2", "r3"); if (username.equals(AnonymousUser.DEFAULT_ANONYMOUS_USERNAME) == false) { builder.put(AnonymousUser.USERNAME_SETTING.getKey(), username); } Settings anonymousEnabledSettings = builder.build(); final AnonymousUser anonymousUser = new AnonymousUser(anonymousEnabledSettings); - service = new AuthenticationService(anonymousEnabledSettings, realms, auditTrailService, - new DefaultAuthenticationFailureHandler(Collections.emptyMap()), threadPool, anonymousUser, - tokenService, apiKeyService, serviceAccountService, operatorPrivilegesService); + service = new AuthenticationService( + anonymousEnabledSettings, + realms, + auditTrailService, + new DefaultAuthenticationFailureHandler(Collections.emptyMap()), + threadPool, + anonymousUser, + tokenService, + apiKeyService, + serviceAccountService, + operatorPrivilegesService + ); doAnswer(invocationOnMock -> { final GetRequest request = (GetRequest) invocationOnMock.getArguments()[0]; @SuppressWarnings("unchecked") final ActionListener listener = (ActionListener) invocationOnMock.getArguments()[1]; - listener.onResponse(new GetResponse(new GetResult(request.index(), request.id(), - SequenceNumbers.UNASSIGNED_SEQ_NO, UNASSIGNED_PRIMARY_TERM, -1L, false, null, - Collections.emptyMap(), Collections.emptyMap()))); + listener.onResponse( + new GetResponse( + new GetResult( + request.index(), + request.id(), + SequenceNumbers.UNASSIGNED_SEQ_NO, + UNASSIGNED_PRIMARY_TERM, + -1L, + false, + null, + Collections.emptyMap(), + Collections.emptyMap() + ) + ) + ); return Void.TYPE; }).when(client).get(any(GetRequest.class), anyActionListener()); try (ThreadContext.StoredContext ignore = threadContext.stashContext()) { @@ -1111,8 +1220,10 @@ public void testWrongApiKeyDoesNotFallbackToAnonymous() { reqId.set(AuditUtil.getOrGenerateRequestId(threadContext)); } threadContext.putHeader("Authorization", "ApiKey dGhpc2lzYW5pbnZhbGlkaWQ6dGhpc2lzYW5pbnZhbGlkc2VjcmV0"); - ElasticsearchSecurityException e = - expectThrows(ElasticsearchSecurityException.class, () -> authenticateBlocking("_action", transportRequest, null, null)); + ElasticsearchSecurityException e = expectThrows( + ElasticsearchSecurityException.class, + () -> authenticateBlocking("_action", transportRequest, null, null) + ); if (requestIdAlreadyPresent) { assertThat(expectAuditRequestId(threadContext), is(reqId.get())); } else { @@ -1127,16 +1238,24 @@ public void testWrongApiKeyDoesNotFallbackToAnonymous() { public void testAnonymousUserRest() throws Exception { String username = randomBoolean() ? AnonymousUser.DEFAULT_ANONYMOUS_USERNAME : "user1"; - Settings.Builder builder = Settings.builder() - .putList(AnonymousUser.ROLES_SETTING.getKey(), "r1", "r2", "r3"); + Settings.Builder builder = Settings.builder().putList(AnonymousUser.ROLES_SETTING.getKey(), "r1", "r2", "r3"); if (username.equals(AnonymousUser.DEFAULT_ANONYMOUS_USERNAME) == false) { builder.put(AnonymousUser.USERNAME_SETTING.getKey(), username); } Settings settings = builder.build(); final AnonymousUser anonymousUser = new AnonymousUser(settings); - service = new AuthenticationService(settings, realms, auditTrailService, + service = new AuthenticationService( + settings, + realms, + auditTrailService, new DefaultAuthenticationFailureHandler(Collections.emptyMap()), - threadPool, anonymousUser, tokenService, apiKeyService, serviceAccountService, operatorPrivilegesService); + threadPool, + anonymousUser, + tokenService, + apiKeyService, + serviceAccountService, + operatorPrivilegesService + ); RestRequest request = new FakeRestRequest(); authenticateBlocking(request, result -> { @@ -1153,17 +1272,25 @@ public void testAnonymousUserRest() throws Exception { public void testAuthenticateRestRequestDisallowAnonymous() throws Exception { final String username = randomBoolean() ? AnonymousUser.DEFAULT_ANONYMOUS_USERNAME : "_anon_" + randomAlphaOfLengthBetween(2, 6); - final Settings.Builder builder = Settings.builder() - .putList(AnonymousUser.ROLES_SETTING.getKey(), "r1", "r2", "r3"); + final Settings.Builder builder = Settings.builder().putList(AnonymousUser.ROLES_SETTING.getKey(), "r1", "r2", "r3"); if (username.equals(AnonymousUser.DEFAULT_ANONYMOUS_USERNAME) == false) { builder.put(AnonymousUser.USERNAME_SETTING.getKey(), username); } Settings settings = builder.build(); final AnonymousUser anonymousUser = new AnonymousUser(settings); - service = new AuthenticationService(settings, realms, auditTrailService, + service = new AuthenticationService( + settings, + realms, + auditTrailService, new DefaultAuthenticationFailureHandler(Collections.emptyMap()), - threadPool, anonymousUser, tokenService, apiKeyService, serviceAccountService, operatorPrivilegesService); + threadPool, + anonymousUser, + tokenService, + apiKeyService, + serviceAccountService, + operatorPrivilegesService + ); RestRequest request = new FakeRestRequest(); PlainActionFuture future = new PlainActionFuture<>(); @@ -1181,13 +1308,20 @@ public void testAuthenticateRestRequestDisallowAnonymous() throws Exception { } public void testAnonymousUserTransportNoDefaultUser() throws Exception { - Settings settings = Settings.builder() - .putList(AnonymousUser.ROLES_SETTING.getKey(), "r1", "r2", "r3") - .build(); + Settings settings = Settings.builder().putList(AnonymousUser.ROLES_SETTING.getKey(), "r1", "r2", "r3").build(); final AnonymousUser anonymousUser = new AnonymousUser(settings); - service = new AuthenticationService(settings, realms, auditTrailService, + service = new AuthenticationService( + settings, + realms, + auditTrailService, new DefaultAuthenticationFailureHandler(Collections.emptyMap()), - threadPool, anonymousUser, tokenService, apiKeyService, serviceAccountService, operatorPrivilegesService); + threadPool, + anonymousUser, + tokenService, + apiKeyService, + serviceAccountService, + operatorPrivilegesService + ); InternalRequest message = new InternalRequest(); boolean requestIdAlreadyPresent = randomBoolean(); SetOnce reqId = new SetOnce<>(); @@ -1208,13 +1342,20 @@ public void testAnonymousUserTransportNoDefaultUser() throws Exception { } public void testAnonymousUserTransportWithDefaultUser() throws Exception { - Settings settings = Settings.builder() - .putList(AnonymousUser.ROLES_SETTING.getKey(), "r1", "r2", "r3") - .build(); + Settings settings = Settings.builder().putList(AnonymousUser.ROLES_SETTING.getKey(), "r1", "r2", "r3").build(); final AnonymousUser anonymousUser = new AnonymousUser(settings); - service = new AuthenticationService(settings, realms, auditTrailService, + service = new AuthenticationService( + settings, + realms, + auditTrailService, new DefaultAuthenticationFailureHandler(Collections.emptyMap()), - threadPool, anonymousUser, tokenService, apiKeyService, serviceAccountService, operatorPrivilegesService); + threadPool, + anonymousUser, + tokenService, + apiKeyService, + serviceAccountService, + operatorPrivilegesService + ); InternalRequest message = new InternalRequest(); boolean requestIdAlreadyPresent = randomBoolean(); @@ -1327,8 +1468,10 @@ public void testRealmAuthenticateTerminateAuthenticationProcessWithException() { } mockAuthenticate(secondRealm, token, throwE, true); - ElasticsearchSecurityException e = - expectThrows(ElasticsearchSecurityException.class, () -> authenticateBlocking("_action", transportRequest, null, null)); + ElasticsearchSecurityException e = expectThrows( + ElasticsearchSecurityException.class, + () -> authenticateBlocking("_action", transportRequest, null, null) + ); if (throwElasticsearchSecurityException) { assertThat(e.getMessage(), is("authentication error")); if (withAuthenticateHeader) { @@ -1365,10 +1508,12 @@ public void testRealmAuthenticateGracefulTerminateAuthenticationProcess() { final String basicScheme = "Basic realm=\"" + XPackField.SECURITY + "\" charset=\"UTF-8\""; mockAuthenticate(firstRealm, token, null, true); - ElasticsearchSecurityException e = - expectThrows(ElasticsearchSecurityException.class, () -> authenticateBlocking("_action", transportRequest, null, null)); - assertThat(e.getMessage(), is("unable to authenticate user [" + principal + "] for action [_action]")); - assertThat(e.getHeader("WWW-Authenticate"), contains(basicScheme)); + ElasticsearchSecurityException e = expectThrows( + ElasticsearchSecurityException.class, + () -> authenticateBlocking("_action", transportRequest, null, null) + ); + assertThat(e.getMessage(), is("unable to authenticate user [" + principal + "] for action [_action]")); + assertThat(e.getHeader("WWW-Authenticate"), contains(basicScheme)); if (requestIdAlreadyPresent) { assertThat(expectAuditRequestId(threadContext), is(reqId.get())); } else { @@ -1385,8 +1530,7 @@ public void testRealmAuthenticateThrowingException() throws Exception { when(token.principal()).thenReturn(randomAlphaOfLength(5)); when(secondRealm.token(threadContext)).thenReturn(token); when(secondRealm.supports(token)).thenReturn(true); - doThrow(authenticationError("realm doesn't like authenticate")) - .when(secondRealm).authenticate(eq(token), anyActionListener()); + doThrow(authenticationError("realm doesn't like authenticate")).when(secondRealm).authenticate(eq(token), anyActionListener()); boolean requestIdAlreadyPresent = randomBoolean(); SetOnce reqId = new SetOnce<>(); if (requestIdAlreadyPresent) { @@ -1412,8 +1556,7 @@ public void testRealmAuthenticateThrowingExceptionRest() throws Exception { when(token.principal()).thenReturn(randomAlphaOfLength(5)); when(secondRealm.token(threadContext)).thenReturn(token); when(secondRealm.supports(token)).thenReturn(true); - doThrow(authenticationError("realm doesn't like authenticate")) - .when(secondRealm).authenticate(eq(token), anyActionListener()); + doThrow(authenticationError("realm doesn't like authenticate")).when(secondRealm).authenticate(eq(token), anyActionListener()); try { authenticateBlocking(restRequest, null); fail("exception should bubble out"); @@ -1431,10 +1574,9 @@ public void testRealmLookupThrowingException() throws Exception { threadContext.putHeader(AuthenticationServiceField.RUN_AS_USER_HEADER, "run_as"); when(secondRealm.token(threadContext)).thenReturn(token); when(secondRealm.supports(token)).thenReturn(true); - mockAuthenticate(secondRealm, token, new User("lookup user", new String[]{"user"})); + mockAuthenticate(secondRealm, token, new User("lookup user", new String[] { "user" })); mockRealmLookupReturnsNull(firstRealm, "run_as"); - doThrow(authenticationError("realm doesn't want to lookup")) - .when(secondRealm).lookupUser(eq("run_as"), anyActionListener()); + doThrow(authenticationError("realm doesn't want to lookup")).when(secondRealm).lookupUser(eq("run_as"), anyActionListener()); boolean requestIdAlreadyPresent = randomBoolean(); SetOnce reqId = new SetOnce<>(); if (requestIdAlreadyPresent) { @@ -1461,10 +1603,9 @@ public void testRealmLookupThrowingExceptionRest() throws Exception { threadContext.putHeader(AuthenticationServiceField.RUN_AS_USER_HEADER, "run_as"); when(secondRealm.token(threadContext)).thenReturn(token); when(secondRealm.supports(token)).thenReturn(true); - mockAuthenticate(secondRealm, token, new User("lookup user", new String[]{"user"})); + mockAuthenticate(secondRealm, token, new User("lookup user", new String[] { "user" })); mockRealmLookupReturnsNull(firstRealm, "run_as"); - doThrow(authenticationError("realm doesn't want to lookup")) - .when(secondRealm).lookupUser(eq("run_as"), anyActionListener()); + doThrow(authenticationError("realm doesn't want to lookup")).when(secondRealm).lookupUser(eq("run_as"), anyActionListener()); try { authenticateBlocking(restRequest, null); fail("exception should bubble out"); @@ -1488,14 +1629,13 @@ public void testRunAsLookupSameRealm() throws Exception { threadContext.putHeader(AuthenticationServiceField.RUN_AS_USER_HEADER, "run_as"); when(secondRealm.token(threadContext)).thenReturn(token); when(secondRealm.supports(token)).thenReturn(true); - final User user = new User("lookup user", new String[]{"user"}, "lookup user", "lookup@foo.foo", - Map.of("foo", "bar"), true); + final User user = new User("lookup user", new String[] { "user" }, "lookup user", "lookup@foo.foo", Map.of("foo", "bar"), true); mockAuthenticate(secondRealm, token, user); mockRealmLookupReturnsNull(firstRealm, "run_as"); doAnswer((i) -> { @SuppressWarnings("unchecked") ActionListener listener = (ActionListener) i.getArguments()[1]; - listener.onResponse(new User("looked up user", new String[]{"some role"})); + listener.onResponse(new User("looked up user", new String[] { "some role" })); return null; }).when(secondRealm).lookupUser(eq("run_as"), anyActionListener()); @@ -1549,10 +1689,10 @@ public void testRunAsLookupDifferentRealm() throws Exception { threadContext.putHeader(AuthenticationServiceField.RUN_AS_USER_HEADER, "run_as"); when(secondRealm.token(threadContext)).thenReturn(token); when(secondRealm.supports(token)).thenReturn(true); - mockAuthenticate(secondRealm, token, new User("lookup user", new String[]{"user"})); + mockAuthenticate(secondRealm, token, new User("lookup user", new String[] { "user" })); doAnswer((i) -> { ActionListener listener = (ActionListener) i.getArguments()[1]; - listener.onResponse(new User("looked up user", new String[]{"some role"})); + listener.onResponse(new User("looked up user", new String[] { "some role" })); return null; }).when(firstRealm).lookupUser(eq("run_as"), anyActionListener()); @@ -1590,7 +1730,7 @@ public void testRunAsLookupDifferentRealm() throws Exception { public void testRunAsWithEmptyRunAsUsernameRest() throws Exception { AuthenticationToken token = mock(AuthenticationToken.class); when(token.principal()).thenReturn(randomAlphaOfLength(5)); - User user = new User("lookup user", new String[]{"user"}); + User user = new User("lookup user", new String[] { "user" }); threadContext.putHeader(AuthenticationServiceField.RUN_AS_USER_HEADER, ""); when(secondRealm.token(threadContext)).thenReturn(token); when(secondRealm.supports(token)).thenReturn(true); @@ -1610,7 +1750,7 @@ public void testRunAsWithEmptyRunAsUsernameRest() throws Exception { public void testRunAsWithEmptyRunAsUsername() throws Exception { AuthenticationToken token = mock(AuthenticationToken.class); when(token.principal()).thenReturn(randomAlphaOfLength(5)); - User user = new User("lookup user", new String[]{"user"}); + User user = new User("lookup user", new String[] { "user" }); threadContext.putHeader(AuthenticationServiceField.RUN_AS_USER_HEADER, ""); boolean requestIdAlreadyPresent = randomBoolean(); SetOnce reqId = new SetOnce<>(); @@ -1630,8 +1770,13 @@ public void testRunAsWithEmptyRunAsUsername() throws Exception { } else { reqId.set(expectAuditRequestId(threadContext)); } - verify(auditTrail).runAsDenied(eq(reqId.get()), any(Authentication.class), eq("_action"), eq(transportRequest), - eq(EmptyAuthorizationInfo.INSTANCE)); + verify(auditTrail).runAsDenied( + eq(reqId.get()), + any(Authentication.class), + eq("_action"), + eq(transportRequest), + eq(EmptyAuthorizationInfo.INSTANCE) + ); verifyNoMoreInteractions(auditTrail); verifyZeroInteractions(operatorPrivilegesService); } @@ -1649,16 +1794,18 @@ public void testAuthenticateTransportDisabledRunAsUser() throws Exception { } when(secondRealm.token(threadContext)).thenReturn(token); when(secondRealm.supports(token)).thenReturn(true); - mockAuthenticate(secondRealm, token, new User("lookup user", new String[]{"user"})); + mockAuthenticate(secondRealm, token, new User("lookup user", new String[] { "user" })); mockRealmLookupReturnsNull(firstRealm, "run_as"); doAnswer((i) -> { ActionListener listener = (ActionListener) i.getArguments()[1]; - listener.onResponse(new User("looked up user", new String[]{"some role"}, null, null, Map.of(), false)); + listener.onResponse(new User("looked up user", new String[] { "some role" }, null, null, Map.of(), false)); return null; }).when(secondRealm).lookupUser(eq("run_as"), anyActionListener()); User fallback = randomBoolean() ? SystemUser.INSTANCE : null; - ElasticsearchSecurityException e = - expectThrows(ElasticsearchSecurityException.class, () -> authenticateBlocking("_action", transportRequest, fallback, null)); + ElasticsearchSecurityException e = expectThrows( + ElasticsearchSecurityException.class, + () -> authenticateBlocking("_action", transportRequest, fallback, null) + ); if (requestIdAlreadyPresent) { assertThat(expectAuditRequestId(threadContext), is(reqId.get())); } else { @@ -1676,17 +1823,19 @@ public void testAuthenticateRestDisabledRunAsUser() throws Exception { threadContext.putHeader(AuthenticationServiceField.RUN_AS_USER_HEADER, "run_as"); when(secondRealm.token(threadContext)).thenReturn(token); when(secondRealm.supports(token)).thenReturn(true); - mockAuthenticate(secondRealm, token, new User("lookup user", new String[]{"user"})); + mockAuthenticate(secondRealm, token, new User("lookup user", new String[] { "user" })); mockRealmLookupReturnsNull(firstRealm, "run_as"); doAnswer((i) -> { @SuppressWarnings("unchecked") ActionListener listener = (ActionListener) i.getArguments()[1]; - listener.onResponse(new User("looked up user", new String[]{"some role"}, null, null, Map.of(), false)); + listener.onResponse(new User("looked up user", new String[] { "some role" }, null, null, Map.of(), false)); return null; }).when(secondRealm).lookupUser(eq("run_as"), anyActionListener()); - ElasticsearchSecurityException e = - expectThrows(ElasticsearchSecurityException.class, () -> authenticateBlocking(restRequest, null)); + ElasticsearchSecurityException e = expectThrows( + ElasticsearchSecurityException.class, + () -> authenticateBlocking(restRequest, null) + ); String reqId = expectAuditRequestId(threadContext); verify(auditTrail).authenticationFailed(reqId, token, restRequest); verifyNoMoreInteractions(auditTrail); @@ -1719,21 +1868,21 @@ public void testAuthenticateWithToken() throws Exception { reqId.set(AuditUtil.getOrGenerateRequestId(threadContext)); } service.authenticate("_action", transportRequest, true, ActionListener.wrap(result -> { - assertThat(result, notNullValue()); - assertThat(result.getUser(), is(user)); - assertThat(result.getLookedUpBy(), is(nullValue())); - assertThat(result.getAuthenticatedBy(), is(notNullValue())); - assertThat(result.getAuthenticatedBy().getName(), is("realm")); // TODO implement equals - assertThat(result.getAuthenticationType(), is(AuthenticationType.TOKEN)); - if (requestIdAlreadyPresent) { - assertThat(expectAuditRequestId(threadContext), is(reqId.get())); - } else { - reqId.set(expectAuditRequestId(threadContext)); - } - verify(operatorPrivilegesService).maybeMarkOperatorUser(eq(result), eq(threadContext)); - setCompletedToTrue(completed); - verify(auditTrail).authenticationSuccess(eq(reqId.get()), eq(result), eq("_action"), same(transportRequest)); - }, this::logAndFail)); + assertThat(result, notNullValue()); + assertThat(result.getUser(), is(user)); + assertThat(result.getLookedUpBy(), is(nullValue())); + assertThat(result.getAuthenticatedBy(), is(notNullValue())); + assertThat(result.getAuthenticatedBy().getName(), is("realm")); // TODO implement equals + assertThat(result.getAuthenticationType(), is(AuthenticationType.TOKEN)); + if (requestIdAlreadyPresent) { + assertThat(expectAuditRequestId(threadContext), is(reqId.get())); + } else { + reqId.set(expectAuditRequestId(threadContext)); + } + verify(operatorPrivilegesService).maybeMarkOperatorUser(eq(result), eq(threadContext)); + setCompletedToTrue(completed); + verify(auditTrail).authenticationSuccess(eq(reqId.get()), eq(result), eq("_action"), same(transportRequest)); + }, this::logAndFail)); } assertTrue(completed.get()); verifyNoMoreInteractions(auditTrail); @@ -1780,7 +1929,7 @@ public void testInvalidToken() throws Exception { reqId.set(expectAuditRequestId(threadContext)); } if (e instanceof IllegalStateException) { - assertThat(e.getMessage(), containsString("array length must be <= to " + ArrayUtil.MAX_ARRAY_LENGTH + " but was: ")); + assertThat(e.getMessage(), containsString("array length must be <= to " + ArrayUtil.MAX_ARRAY_LENGTH + " but was: ")); latch.countDown(); } else if (e instanceof NegativeArraySizeException) { assertThat(e.getMessage(), containsString("array size must be positive but was: ")); @@ -1792,7 +1941,7 @@ public void testInvalidToken() throws Exception { } })); } catch (IllegalStateException ex) { - assertThat(ex.getMessage(), containsString("array length must be <= to " + ArrayUtil.MAX_ARRAY_LENGTH + " but was: ")); + assertThat(ex.getMessage(), containsString("array length must be <= to " + ArrayUtil.MAX_ARRAY_LENGTH + " but was: ")); latch.countDown(); } catch (NegativeArraySizeException ex) { assertThat(ex.getMessage(), containsString("array size must be positive but was: ")); @@ -1835,8 +1984,10 @@ public void testExpiredToken() throws Exception { reqId.set(AuditUtil.getOrGenerateRequestId(threadContext)); } threadContext.putHeader("Authorization", "Bearer " + token); - ElasticsearchSecurityException e = - expectThrows(ElasticsearchSecurityException.class, () -> authenticateBlocking("_action", transportRequest, null, null)); + ElasticsearchSecurityException e = expectThrows( + ElasticsearchSecurityException.class, + () -> authenticateBlocking("_action", transportRequest, null, null) + ); if (requestIdAlreadyPresent) { assertThat(expectAuditRequestId(threadContext), is(reqId.get())); } @@ -1855,15 +2006,19 @@ public void testApiKeyAuthInvalidHeader() { } final String invalidHeader = randomFrom("apikey", "apikey ", "apikey foo"); threadContext.putHeader("Authorization", invalidHeader); - ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, - () -> authenticateBlocking("_action", transportRequest, null, null)); + ElasticsearchSecurityException e = expectThrows( + ElasticsearchSecurityException.class, + () -> authenticateBlocking("_action", transportRequest, null, null) + ); if (requestIdAlreadyPresent) { assertThat(expectAuditRequestId(threadContext), is(reqId.get())); } assertEquals(RestStatus.UNAUTHORIZED, e.status()); if (invalidHeader.equals("apikey foo")) { - assertThat(e.getMessage(), containsString( - "unable to authenticate with provided credentials and anonymous access is not allowed for this request")); + assertThat( + e.getMessage(), + containsString("unable to authenticate with provided credentials and anonymous access is not allowed for this request") + ); } else { assertThat(e.getMessage(), containsString("missing authentication credentials")); } @@ -1887,8 +2042,10 @@ public void testApiKeyAuth() { source.put("api_key_invalidated", false); source.put("api_key_hash", new String(Hasher.BCRYPT4.hash(new SecureString(key.toCharArray())))); source.put("role_descriptors", Collections.singletonMap("api key role", Collections.singletonMap("cluster", "all"))); - source.put("limited_by_role_descriptors", - Collections.singletonMap("limited api key role", Collections.singletonMap("cluster", "all"))); + source.put( + "limited_by_role_descriptors", + Collections.singletonMap("limited api key role", Collections.singletonMap("cluster", "all")) + ); source.put("name", "my api key for testApiKeyAuth"); source.put("version", 0); Map creatorMap = new HashMap<>(); @@ -1898,13 +2055,36 @@ public void testApiKeyAuth() { creatorMap.put("metadata", Collections.emptyMap()); creatorMap.put("realm", "auth realm"); source.put("creator", creatorMap); - GetResponse getResponse = new GetResponse(new GetResult(request.index(), request.id(), 0, 1, 1L, true, - BytesReference.bytes(JsonXContent.contentBuilder().map(source)), Collections.emptyMap(), Collections.emptyMap())); + GetResponse getResponse = new GetResponse( + new GetResult( + request.index(), + request.id(), + 0, + 1, + 1L, + true, + BytesReference.bytes(JsonXContent.contentBuilder().map(source)), + Collections.emptyMap(), + Collections.emptyMap() + ) + ); listener.onResponse(getResponse); } else { - listener.onResponse(new GetResponse(new GetResult(request.index(), request.id(), - SequenceNumbers.UNASSIGNED_SEQ_NO, 1, -1L, false, null, - Collections.emptyMap(), Collections.emptyMap()))); + listener.onResponse( + new GetResponse( + new GetResult( + request.index(), + request.id(), + SequenceNumbers.UNASSIGNED_SEQ_NO, + 1, + -1L, + false, + null, + Collections.emptyMap(), + Collections.emptyMap() + ) + ) + ); } return Void.TYPE; }).when(client).get(any(GetRequest.class), anyActionListener()); @@ -1952,13 +2132,36 @@ public void testExpiredApiKey() { creatorMap.put("metadata", Collections.emptyMap()); creatorMap.put("realm", "auth realm"); source.put("creator", creatorMap); - GetResponse getResponse = new GetResponse(new GetResult(request.index(), request.id(), 0, 1, 1L, true, - BytesReference.bytes(JsonXContent.contentBuilder().map(source)), Collections.emptyMap(), Collections.emptyMap())); + GetResponse getResponse = new GetResponse( + new GetResult( + request.index(), + request.id(), + 0, + 1, + 1L, + true, + BytesReference.bytes(JsonXContent.contentBuilder().map(source)), + Collections.emptyMap(), + Collections.emptyMap() + ) + ); listener.onResponse(getResponse); } else { - listener.onResponse(new GetResponse(new GetResult(request.index(), request.id(), - SequenceNumbers.UNASSIGNED_SEQ_NO, 1, -1L, false, null, - Collections.emptyMap(), Collections.emptyMap()))); + listener.onResponse( + new GetResponse( + new GetResult( + request.index(), + request.id(), + SequenceNumbers.UNASSIGNED_SEQ_NO, + 1, + -1L, + false, + null, + Collections.emptyMap(), + Collections.emptyMap() + ) + ) + ); } return Void.TYPE; }).when(client).get(any(GetRequest.class), anyActionListener()); @@ -1970,8 +2173,10 @@ public void testExpiredApiKey() { reqId.set(AuditUtil.getOrGenerateRequestId(threadContext)); } threadContext.putHeader("Authorization", headerValue); - ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, - () -> authenticateBlocking("_action", transportRequest, null, null)); + ElasticsearchSecurityException e = expectThrows( + ElasticsearchSecurityException.class, + () -> authenticateBlocking("_action", transportRequest, null, null) + ); if (requestIdAlreadyPresent) { assertThat(expectAuditRequestId(threadContext), is(reqId.get())); } @@ -1985,9 +2190,12 @@ public void testCanAuthenticateServiceAccount() throws ExecutionException, Inter final TokenInfo.TokenSource tokenSource = randomFrom(TokenInfo.TokenSource.values()); final Authentication authentication = new Authentication( new User("elastic/fleet-server"), - new RealmRef("_service_account", "_service_account", "foo"), null, - Version.CURRENT, AuthenticationType.TOKEN, - Map.of("_token_name", ValidationTests.randomTokenName(), "_token_source", tokenSource.name().toLowerCase(Locale.ROOT))); + new RealmRef("_service_account", "_service_account", "foo"), + null, + Version.CURRENT, + AuthenticationType.TOKEN, + Map.of("_token_name", ValidationTests.randomTokenName(), "_token_source", tokenSource.name().toLowerCase(Locale.ROOT)) + ); try (ThreadContext.StoredContext ignored = threadContext.newStoredContext(false)) { boolean requestIdAlreadyPresent = randomBoolean(); SetOnce reqId = new SetOnce<>(); @@ -2031,8 +2239,10 @@ public void testServiceAccountFailureWillNotFallthrough() throws IOException { listener.onFailure(bailOut); return null; }).when(serviceAccountService).authenticateToken(any(), any(), any()); - final ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, - () -> authenticateBlocking("_action", transportRequest, null, null)); + final ElasticsearchSecurityException e = expectThrows( + ElasticsearchSecurityException.class, + () -> authenticateBlocking("_action", transportRequest, null, null) + ); if (requestIdAlreadyPresent) { assertThat(expectAuditRequestId(threadContext), is(reqId.get())); } else { @@ -2041,9 +2251,12 @@ public void testServiceAccountFailureWillNotFallthrough() throws IOException { assertThat(e, sameInstance(bailOut)); verifyZeroInteractions(operatorPrivilegesService); final ServiceAccountToken serviceToken = ServiceAccountToken.fromBearerString(new SecureString(bearerString.toCharArray())); - verify(auditTrail).authenticationFailed(eq(reqId.get()), + verify(auditTrail).authenticationFailed( + eq(reqId.get()), argThat(o -> ((ServiceAccountToken) o).getTokenId().equals(serviceToken.getTokenId())), - eq("_action"), eq(transportRequest)); + eq("_action"), + eq(transportRequest) + ); } } @@ -2099,8 +2312,7 @@ private void mockAuthenticate(Realm realm, AuthenticationToken token, Exception }).when(realm).authenticate(eq(token), anyActionListener()); } - private void authenticateBlocking(RestRequest restRequest, - Consumer> verifier) { + private void authenticateBlocking(RestRequest restRequest, Consumer> verifier) { SetOnce reqId = new SetOnce<>(); PlainActionFuture future = new PlainActionFuture<>() { @Override @@ -2124,8 +2336,12 @@ public void onFailure(Exception e) { assertThat(expectAuditRequestId(threadContext), is(reqId.get())); } - private void authenticateBlocking(String action, TransportRequest transportRequest, User fallbackUser, - Consumer> verifier) { + private void authenticateBlocking( + String action, + TransportRequest transportRequest, + User fallbackUser, + Consumer> verifier + ) { SetOnce reqId = new SetOnce<>(); PlainActionFuture future = new PlainActionFuture<>() { @Override @@ -2225,7 +2441,17 @@ private void setCompletedToTrue(AtomicBoolean completed) { private SecurityIndexManager.State dummyState(ClusterHealthStatus indexStatus) { return new SecurityIndexManager.State( - Instant.now(), true, true, true, null, concreteSecurityIndexName, indexStatus, IndexMetadata.State.OPEN, null, "my_uuid"); + Instant.now(), + true, + true, + true, + null, + concreteSecurityIndexName, + indexStatus, + IndexMetadata.State.OPEN, + null, + "my_uuid" + ); } @SuppressWarnings("unchecked") diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/AuthenticatorChainTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/AuthenticatorChainTests.java index b29da570c423d..9ddea77b219a5 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/AuthenticatorChainTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/AuthenticatorChainTests.java @@ -87,14 +87,16 @@ public void init() { authentication = mock(Authentication.class); when(authentication.getUser()).thenReturn(user); fallbackUser = mock(User.class); - authenticatorChain = new AuthenticatorChain(settings, + authenticatorChain = new AuthenticatorChain( + settings, operatorPrivilegesService, anonymousUser, authenticationContextSerializer, serviceAccountAuthenticator, oAuth2TokenAuthenticator, apiKeyAuthenticator, - realmsAuthenticator); + realmsAuthenticator + ); } public void testAuthenticateWillLookForExistingAuthenticationFirst() throws IOException { @@ -128,8 +130,9 @@ public void testAuthenticateWithServiceAccount() throws IOException { final Authenticator.Context context = createAuthenticatorContext(); when(serviceAccountAuthenticator.extractCredentials(context)).thenReturn(mock(ServiceAccountToken.class)); doAnswer(invocationOnMock -> { - @SuppressWarnings("unchecked") final ActionListener> listener = - (ActionListener>) invocationOnMock.getArguments()[1]; + @SuppressWarnings("unchecked") + final ActionListener> listener = (ActionListener< + AuthenticationResult>) invocationOnMock.getArguments()[1]; listener.onResponse(AuthenticationResult.success(authentication)); return null; }).when(serviceAccountAuthenticator).authenticate(eq(context), any()); @@ -148,8 +151,9 @@ public void testAuthenticateWithOAuth2Token() throws IOException { final Authenticator.Context context = createAuthenticatorContext(); when(oAuth2TokenAuthenticator.extractCredentials(context)).thenReturn(mock(BearerToken.class)); doAnswer(invocationOnMock -> { - @SuppressWarnings("unchecked") final ActionListener> listener = - (ActionListener>) invocationOnMock.getArguments()[1]; + @SuppressWarnings("unchecked") + final ActionListener> listener = (ActionListener< + AuthenticationResult>) invocationOnMock.getArguments()[1]; listener.onResponse(AuthenticationResult.success(authentication)); return null; }).when(oAuth2TokenAuthenticator).authenticate(eq(context), any()); @@ -168,10 +172,12 @@ public void testAuthenticateWithOAuth2Token() throws IOException { public void testAuthenticateWithApiKey() throws IOException { final Authenticator.Context context = createAuthenticatorContext(); when(apiKeyAuthenticator.extractCredentials(context)).thenReturn( - new ApiKeyCredentials(randomAlphaOfLength(20), new SecureString(randomAlphaOfLength(22).toCharArray()))); + new ApiKeyCredentials(randomAlphaOfLength(20), new SecureString(randomAlphaOfLength(22).toCharArray())) + ); doAnswer(invocationOnMock -> { - @SuppressWarnings("unchecked") final ActionListener> listener = - (ActionListener>) invocationOnMock.getArguments()[1]; + @SuppressWarnings("unchecked") + final ActionListener> listener = (ActionListener< + AuthenticationResult>) invocationOnMock.getArguments()[1]; listener.onResponse(AuthenticationResult.success(authentication)); return null; }).when(apiKeyAuthenticator).authenticate(eq(context), any()); @@ -192,8 +198,9 @@ public void testAuthenticateWithRealms() throws IOException { final Authenticator.Context context = createAuthenticatorContext(); when(realmsAuthenticator.extractCredentials(context)).thenReturn(mock(AuthenticationToken.class)); doAnswer(invocationOnMock -> { - @SuppressWarnings("unchecked") final ActionListener> listener = - (ActionListener>) invocationOnMock.getArguments()[1]; + @SuppressWarnings("unchecked") + final ActionListener> listener = (ActionListener< + AuthenticationResult>) invocationOnMock.getArguments()[1]; listener.onResponse(AuthenticationResult.success(authentication)); return null; }).when(realmsAuthenticator).authenticate(eq(context), any()); @@ -244,18 +251,21 @@ public void testUnsuccessfulOAuth2TokenOrApiKeyWillNotFallToAnonymousOrReportMis threadContext.putHeader("Authorization", unsuccessfulApiKey ? "ApiKey key_id:key_secret" : "Bearer some_token_value"); if (unsuccessfulApiKey) { when(apiKeyAuthenticator.extractCredentials(context)).thenReturn( - new ApiKeyCredentials(randomAlphaOfLength(20), new SecureString(randomAlphaOfLength(22).toCharArray()))); + new ApiKeyCredentials(randomAlphaOfLength(20), new SecureString(randomAlphaOfLength(22).toCharArray())) + ); doAnswer(invocationOnMock -> { - @SuppressWarnings("unchecked") final ActionListener> listener = - (ActionListener>) invocationOnMock.getArguments()[1]; + @SuppressWarnings("unchecked") + final ActionListener> listener = (ActionListener< + AuthenticationResult>) invocationOnMock.getArguments()[1]; listener.onResponse(AuthenticationResult.unsuccessful("unsuccessful api key", null)); return null; }).when(apiKeyAuthenticator).authenticate(eq(context), any()); } else { when(oAuth2TokenAuthenticator.extractCredentials(context)).thenReturn(mock(BearerToken.class)); doAnswer(invocationOnMock -> { - @SuppressWarnings("unchecked") final ActionListener> listener = - (ActionListener>) invocationOnMock.getArguments()[1]; + @SuppressWarnings("unchecked") + final ActionListener> listener = (ActionListener< + AuthenticationResult>) invocationOnMock.getArguments()[1]; listener.onResponse(AuthenticationResult.unsuccessful("unsuccessful bearer token", null)); return null; }).when(oAuth2TokenAuthenticator).authenticate(eq(context), any()); @@ -263,12 +273,15 @@ public void testUnsuccessfulOAuth2TokenOrApiKeyWillNotFallToAnonymousOrReportMis final PlainActionFuture future = new PlainActionFuture<>(); authenticatorChain.authenticateAsync(context, future); - final ElasticsearchSecurityException e = - expectThrows(ElasticsearchSecurityException.class, future::actionGet); - assertThat(e.getMessage(), containsString("" + - "unable to authenticate with provided credentials and anonymous access is not allowed for this request")); - assertThat(e.getMetadata("es.additional_unsuccessful_credentials"), - hasItem(containsString(unsuccessfulApiKey ? "unsuccessful api key" : "unsuccessful bearer token"))); + final ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, future::actionGet); + assertThat( + e.getMessage(), + containsString("" + "unable to authenticate with provided credentials and anonymous access is not allowed for this request") + ); + assertThat( + e.getMetadata("es.additional_unsuccessful_credentials"), + hasItem(containsString(unsuccessfulApiKey ? "unsuccessful api key" : "unsuccessful bearer token")) + ); } private Authenticator.Context createAuthenticatorContext() { diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/InternalRealmsTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/InternalRealmsTests.java index cf4c87dfc2074..f871827c47d66 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/InternalRealmsTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/InternalRealmsTests.java @@ -29,7 +29,6 @@ import java.util.Map; import java.util.function.BiConsumer; -import static org.mockito.Mockito.times; import static org.hamcrest.CoreMatchers.nullValue; import static org.hamcrest.Matchers.any; import static org.hamcrest.Matchers.hasEntry; @@ -37,6 +36,7 @@ import static org.hamcrest.Matchers.notNullValue; import static org.mockito.ArgumentMatchers.isA; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyZeroInteractions; diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/RealmSettingsTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/RealmSettingsTests.java index 3b5ef1cd9bbaf..0dcc46efe97b3 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/RealmSettingsTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/RealmSettingsTests.java @@ -82,23 +82,25 @@ public void testPkiRealmWithFullSslSettingsDoesNotValidate() throws Exception { public void testSettingsWithMultipleRealmsValidatesSuccessfully() throws Exception { final Settings settings = Settings.builder() - .put(fileRealm("file1").build()) - .put(nativeRealm("native2").build()) - .put(ldapRealm("ldap3", true, false).build()) - .put(activeDirectoryRealm("ad4", false).build()) // don't load SSL twice - .put(pkiRealm("pki5", false).build()) - .build(); + .put(fileRealm("file1").build()) + .put(nativeRealm("native2").build()) + .put(ldapRealm("ldap3", true, false).build()) + .put(activeDirectoryRealm("ad4", false).build()) // don't load SSL twice + .put(pkiRealm("pki5", false).build()) + .build(); assertSuccess(settings); } public void testSettingsWithKeystoreOnlyRealmDoesNotValidate() throws Exception { final String securePasswordKey = RealmSettings.getFullSettingKey( - new RealmConfig.RealmIdentifier("ldap", "ldap_1"), PoolingSessionFactorySettings.SECURE_BIND_PASSWORD); - final Settings.Builder builder = Settings.builder() - .put(ldapRealm("ldap-1", randomBoolean(), randomBoolean()).build()); - SecuritySettingsSource.addSecureSettings(builder, secureSettings -> { - secureSettings.setString(securePasswordKey, "secret-password"); - }); + new RealmConfig.RealmIdentifier("ldap", "ldap_1"), + PoolingSessionFactorySettings.SECURE_BIND_PASSWORD + ); + final Settings.Builder builder = Settings.builder().put(ldapRealm("ldap-1", randomBoolean(), randomBoolean()).build()); + SecuritySettingsSource.addSecureSettings( + builder, + secureSettings -> { secureSettings.setString(securePasswordKey, "secret-password"); } + ); final Settings settings = builder.build(); final SettingsException exception = expectThrows(SettingsException.class, () -> RealmSettings.getRealmSettings(settings)); assertThat(exception.getMessage(), containsString("elasticsearch.keystore")); @@ -127,13 +129,13 @@ private Settings.Builder ldapRealm(String name, boolean userSearch, boolean grou } private Settings.Builder ldapSettings(boolean userSearch, boolean groupSearch) { - final Settings.Builder builder = commonLdapSettings("ldap", true) - .put("bind_dn", "elasticsearch") - .put("follow_referrals", randomBoolean()); + final Settings.Builder builder = commonLdapSettings("ldap", true).put("bind_dn", "elasticsearch") + .put("follow_referrals", randomBoolean()); - SecuritySettingsSource.addSecureSettings(builder, secureSettings -> { - secureSettings.setString("secure_bind_password", "t0p_s3cr3t"); - }); + SecuritySettingsSource.addSecureSettings( + builder, + secureSettings -> { secureSettings.setString("secure_bind_password", "t0p_s3cr3t"); } + ); if (userSearch) { builder.put("user_search.base_dn", "o=people, dc=example, dc=com"); @@ -146,9 +148,11 @@ private Settings.Builder ldapSettings(boolean userSearch, boolean groupSearch) { builder.put("user_search.pool.health_check.dn", randomAlphaOfLength(32)); builder.put("user_search.pool.health_check.interval", randomPositiveTimeValue()); } else { - builder.putList("user_dn_templates", - "cn={0}, ou=staff, o=people, dc=example, dc=com", - "cn={0}, ou=visitors, o=people, dc=example, dc=com"); + builder.putList( + "user_dn_templates", + "cn={0}, ou=staff, o=people, dc=example, dc=com", + "cn={0}, ou=visitors, o=people, dc=example, dc=com" + ); } if (groupSearch) { @@ -167,8 +171,7 @@ private Settings.Builder activeDirectoryRealm(String name, boolean configureSSL) } private Settings.Builder activeDirectorySettings(boolean configureSSL) { - final Settings.Builder builder = commonLdapSettings("active_directory", configureSSL) - .put("domain_name", "MEGACORP"); + final Settings.Builder builder = commonLdapSettings("active_directory", configureSSL).put("domain_name", "MEGACORP"); builder.put("user_search.base_dn", "o=people, dc.example, dc.com"); builder.put("user_search.scope", "sub_tree"); builder.put("user_search.filter", randomAlphaOfLength(5) + "={0}"); @@ -178,15 +181,19 @@ private Settings.Builder activeDirectorySettings(boolean configureSSL) { } private Settings.Builder commonLdapSettings(String type, boolean configureSSL) { - final Settings.Builder builder = baseSettings(true) - .putList("url", "ldap://dir1.internal:9876", "ldap://dir2.internal:9876", "ldap://dir3.internal:9876") - .put("load_balance.type", "round_robin") - .put("load_balance.cache_ttl", randomTimeValue()) - .put("unmapped_groups_as_roles", randomBoolean()) - .put("files.role_mapping", "x-pack/" + randomAlphaOfLength(8) + ".yml") - .put("timeout.tcp_connect", randomPositiveTimeValue()) - .put("timeout.response", randomPositiveTimeValue()) - .put("timeout.ldap_search", randomPositiveTimeValue()); + final Settings.Builder builder = baseSettings(true).putList( + "url", + "ldap://dir1.internal:9876", + "ldap://dir2.internal:9876", + "ldap://dir3.internal:9876" + ) + .put("load_balance.type", "round_robin") + .put("load_balance.cache_ttl", randomTimeValue()) + .put("unmapped_groups_as_roles", randomBoolean()) + .put("files.role_mapping", "x-pack/" + randomAlphaOfLength(8) + ".yml") + .put("timeout.tcp_connect", randomPositiveTimeValue()) + .put("timeout.response", randomPositiveTimeValue()) + .put("timeout.ldap_search", randomPositiveTimeValue()); if (configureSSL) { configureSsl("ssl.", builder, randomBoolean(), randomBoolean()); } @@ -198,15 +205,15 @@ private Settings.Builder pkiRealm(String name, boolean useTrustStore) { } private Settings.Builder pkiSettings(boolean useTrustStore) { - final Settings.Builder builder = baseSettings(false) - .put("username_pattern", "CN=\\D(\\d+)(?:,\\|$)") - .put("files.role_mapping", "x-pack/" + randomAlphaOfLength(8) + ".yml"); + final Settings.Builder builder = baseSettings(false).put("username_pattern", "CN=\\D(\\d+)(?:,\\|$)") + .put("files.role_mapping", "x-pack/" + randomAlphaOfLength(8) + ".yml"); if (useTrustStore) { builder.put("truststore.path", randomAlphaOfLengthBetween(8, 32)); - SecuritySettingsSource.addSecureSettings(builder, secureSettings -> { - secureSettings.setString("truststore.secure_password", randomAlphaOfLength(8)); - }); + SecuritySettingsSource.addSecureSettings( + builder, + secureSettings -> { secureSettings.setString("truststore.secure_password", randomAlphaOfLength(8)); } + ); builder.put("truststore.algorithm", randomAlphaOfLengthBetween(6, 10)); } else { builder.putList("certificate_authorities", generateRandomStringArray(5, 32, false, false)); @@ -226,16 +233,20 @@ private Settings.Builder configureSsl(String prefix, Settings.Builder builder, b }); } else { builder.put(prefix + "key", "x-pack/ssl/" + randomAlphaOfLength(5) + ".key"); - SecuritySettingsSource.addSecureSettings(builder, secureSettings -> - secureSettings.setString(prefix + "secure_key_passphrase", randomAlphaOfLength(32))); + SecuritySettingsSource.addSecureSettings( + builder, + secureSettings -> secureSettings.setString(prefix + "secure_key_passphrase", randomAlphaOfLength(32)) + ); builder.put(prefix + "certificate", "ssl/" + randomAlphaOfLength(5) + ".cert"); } if (useTrustStore) { builder.put(prefix + "truststore.path", "x-pack/ssl/" + randomAlphaOfLength(5) + ".jts"); - SecuritySettingsSource.addSecureSettings(builder, secureSettings -> - secureSettings.setString(prefix + "truststore.secure_password", randomAlphaOfLength(8))); + SecuritySettingsSource.addSecureSettings( + builder, + secureSettings -> secureSettings.setString(prefix + "truststore.secure_password", randomAlphaOfLength(8)) + ); } else { builder.put(prefix + "certificate_authorities", "ssl/" + randomAlphaOfLength(8) + ".ca"); } @@ -248,9 +259,7 @@ private Settings.Builder configureSsl(String prefix, Settings.Builder builder, b } private Settings.Builder baseSettings(boolean withCacheSettings) { - final Settings.Builder builder = Settings.builder() - .put("order", randomInt()) - .put("enabled", true); + final Settings.Builder builder = Settings.builder().put("order", randomInt()).put("enabled", true); if (withCacheSettings) { builder.put("cache.ttl", randomPositiveTimeValue()) .put("cache.max_users", randomIntBetween(1_000, 1_000_000)) @@ -308,17 +317,19 @@ private void assertErrorWithMessage(String realmType, String realmName, String m } private IllegalArgumentException assertError(String realmType, String realmName, Settings settings) { - final IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, - () -> validate(settings) - ); + final IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> validate(settings)); assertThat(exception.getMessage(), containsString(realmPrefix(realmType, realmName))); return exception; } private void validate(Settings settings) { final Set> settingsSet = new HashSet<>(InternalRealmsSettings.getSettings()); - final AbstractScopedSettings validator = new AbstractScopedSettings(settings, settingsSet, Collections.emptySet(), - Setting.Property.NodeScope) { + final AbstractScopedSettings validator = new AbstractScopedSettings( + settings, + settingsSet, + Collections.emptySet(), + Setting.Property.NodeScope + ) { }; validator.validate(settings, false); } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/RealmsAuthenticatorTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/RealmsAuthenticatorTests.java index 37ba7295d27d9..b3a4642dfd203 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/RealmsAuthenticatorTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/RealmsAuthenticatorTests.java @@ -77,9 +77,9 @@ public void init() throws Exception { when(realms.getActiveRealms()).thenReturn(List.of(realm1, realm2)); when(realms.getUnlicensedRealms()).thenReturn(List.of(realm3)); - request = randomBoolean() ? - mock(AuthenticationService.AuditableRestRequest.class) : - mock(AuthenticationService.AuditableTransportRequest.class); + request = randomBoolean() + ? mock(AuthenticationService.AuditableRestRequest.class) + : mock(AuthenticationService.AuditableTransportRequest.class); authenticationToken = mock(AuthenticationToken.class); username = randomAlphaOfLength(5); when(authenticationToken.principal()).thenReturn(username); @@ -105,8 +105,10 @@ public void testWillAuditOnCredentialsExtractionFailure() { final ElasticsearchSecurityException wrapped = new ElasticsearchSecurityException("wrapped"); when(request.exceptionProcessingRequest(cause, null)).thenReturn(wrapped); doThrow(cause).when(randomBoolean() ? realm1 : realm2).token(threadContext); - assertThat(expectThrows(ElasticsearchSecurityException.class, - () -> realmsAuthenticator.extractCredentials(createAuthenticatorContext())), is(wrapped)); + assertThat( + expectThrows(ElasticsearchSecurityException.class, () -> realmsAuthenticator.extractCredentials(createAuthenticatorContext())), + is(wrapped) + ); } public void testAuthenticate() { @@ -123,16 +125,18 @@ public void testAuthenticate() { when(successfulRealm.supports(authenticationToken)).thenReturn(true); doAnswer(invocationOnMock -> { - @SuppressWarnings("unchecked") final ActionListener> listener = - (ActionListener>) invocationOnMock.getArguments()[1]; + @SuppressWarnings("unchecked") + final ActionListener> listener = (ActionListener>) invocationOnMock + .getArguments()[1]; listener.onResponse(AuthenticationResult.success(user)); return null; }).when(successfulRealm).authenticate(eq(authenticationToken), any()); when(unsuccessfulRealm.supports(authenticationToken)).thenReturn(randomBoolean()); doAnswer(invocationOnMock -> { - @SuppressWarnings("unchecked") final ActionListener> listener = - (ActionListener>) invocationOnMock.getArguments()[1]; + @SuppressWarnings("unchecked") + final ActionListener> listener = (ActionListener>) invocationOnMock + .getArguments()[1]; listener.onResponse(AuthenticationResult.unsuccessful("unsuccessful", null)); return null; }).when(unsuccessfulRealm).authenticate(eq(authenticationToken), any()); @@ -145,8 +149,10 @@ public void testAuthenticate() { assertThat(result.getStatus(), is(AuthenticationResult.Status.SUCCESS)); final Authentication authentication = result.getValue(); assertThat(authentication.getUser(), is(user)); - assertThat(authentication.getAuthenticatedBy(), - equalTo(new Authentication.RealmRef(successfulRealm.name(), successfulRealm.type(), nodeName))); + assertThat( + authentication.getAuthenticatedBy(), + equalTo(new Authentication.RealmRef(successfulRealm.name(), successfulRealm.type(), nodeName)) + ); } public void testNullUser() throws IllegalAccessException { @@ -173,12 +179,15 @@ public void testNullUser() throws IllegalAccessException { final MockLogAppender mockAppender = new MockLogAppender(); mockAppender.start(); try { - mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation( - "unlicensed realms", - RealmsAuthenticator.class.getName(), Level.WARN, - "Authentication failed using realms [realm1/realm1,realm2/reaml2]." - + " Realms [realm3/realm3] were skipped because they are not permitted on the current license" - )); + mockAppender.addExpectation( + new MockLogAppender.SeenEventExpectation( + "unlicensed realms", + RealmsAuthenticator.class.getName(), + Level.WARN, + "Authentication failed using realms [realm1/realm1,realm2/reaml2]." + + " Realms [realm3/realm3] were skipped because they are not permitted on the current license" + ) + ); final PlainActionFuture> future = new PlainActionFuture<>(); realmsAuthenticator.authenticate(context, future); assertThat(expectThrows(ElasticsearchSecurityException.class, future::actionGet), is(e)); @@ -202,8 +211,11 @@ public void testLookupRunAsUser() { } final Realm authRealm = randomFrom(realm1, realm2); - final Authentication authentication = - new Authentication(user, new Authentication.RealmRef(authRealm.name(), authRealm.type(), nodeName), null); + final Authentication authentication = new Authentication( + user, + new Authentication.RealmRef(authRealm.name(), authRealm.type(), nodeName), + null + ); final PlainActionFuture> future = new PlainActionFuture<>(); realmsAuthenticator.lookupRunAsUser(createAuthenticatorContext(), authentication, future); final Tuple tuple = future.actionGet(); @@ -220,8 +232,11 @@ public void testNullRunAsUser() { public void testEmptyRunAsUsernameWillFail() { threadContext.putHeader(AuthenticationServiceField.RUN_AS_USER_HEADER, ""); final Realm authRealm = randomFrom(realm1, realm2); - final Authentication authentication = - new Authentication(user, new Authentication.RealmRef(authRealm.name(), authRealm.type(), nodeName), null); + final Authentication authentication = new Authentication( + user, + new Authentication.RealmRef(authRealm.name(), authRealm.type(), nodeName), + null + ); final PlainActionFuture> future = new PlainActionFuture<>(); final ElasticsearchSecurityException e = new ElasticsearchSecurityException("fail"); when(request.runAsDenied(any(), any())).thenReturn(e); @@ -231,8 +246,9 @@ public void testEmptyRunAsUsernameWillFail() { private void configureRealmAuthResponse(Realm realm, AuthenticationResult authenticationResult) { doAnswer(invocationOnMock -> { - @SuppressWarnings("unchecked") final ActionListener> listener = - (ActionListener>) invocationOnMock.getArguments()[1]; + @SuppressWarnings("unchecked") + final ActionListener> listener = (ActionListener>) invocationOnMock + .getArguments()[1]; listener.onResponse(authenticationResult); return null; }).when(realm).authenticate(eq(authenticationToken), any()); @@ -240,8 +256,8 @@ private void configureRealmAuthResponse(Realm realm, AuthenticationResult private void configureRealmUserResponse(Realm realm, String runAsUsername) { doAnswer(invocationOnMock -> { - @SuppressWarnings("unchecked") final ActionListener listener = - (ActionListener) invocationOnMock.getArguments()[1]; + @SuppressWarnings("unchecked") + final ActionListener listener = (ActionListener) invocationOnMock.getArguments()[1]; listener.onResponse(runAsUsername == null ? null : new User(runAsUsername)); return null; }).when(realm).lookupUser(runAsUsername == null ? anyString() : eq(runAsUsername), any()); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/RealmsTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/RealmsTests.java index d76c3a09529d9..87df7b24a129c 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/RealmsTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/RealmsTests.java @@ -782,10 +782,18 @@ public void testInitRealmsFailsForMultipleKerberosRealms() throws IOException { builder.put("xpack.security.authc.realms.kerberos.realm_2.order", 2); final Settings settings = builder.build(); Environment env = TestEnvironment.newEnvironment(settings); - final IllegalArgumentException iae = expectThrows(IllegalArgumentException.class, - () -> new Realms(settings, env, factories, licenseState, threadContext, reservedRealm)); - assertThat(iae.getMessage(), is(equalTo( - "multiple realms [realm_1, realm_2] configured of type [kerberos], [kerberos] can only have one such realm configured"))); + final IllegalArgumentException iae = expectThrows( + IllegalArgumentException.class, + () -> new Realms(settings, env, factories, licenseState, threadContext, reservedRealm) + ); + assertThat( + iae.getMessage(), + is( + equalTo( + "multiple realms [realm_1, realm_2] configured of type [kerberos], [kerberos] can only have one such realm configured" + ) + ) + ); } public void testWarningsForReservedPrefixedRealmNames() throws Exception { diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/TokenServiceMock.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/TokenServiceMock.java index eb5856ffc02a6..49f7fb8af141b 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/TokenServiceMock.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/TokenServiceMock.java @@ -11,8 +11,8 @@ import org.elasticsearch.client.Client; import org.elasticsearch.common.UUIDs; import org.elasticsearch.common.settings.SecureString; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.test.XContentTestUtils; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.security.authc.Authentication; import org.elasticsearch.xpack.core.security.index.RestrictedIndicesNames; import org.elasticsearch.xpack.security.test.SecurityMocks; @@ -65,7 +65,11 @@ public void defineToken(MockToken token, Authentication authentication, boolean final Map document = new HashMap<>(); document.put("access_token", Map.of("user_token", userToken, "invalidated", valid == false)); - SecurityMocks.mockGetRequest(client, RestrictedIndicesNames.SECURITY_TOKENS_ALIAS, "token_" + token.hashedToken, - XContentTestUtils.convertToXContent(document, XContentType.JSON)); + SecurityMocks.mockGetRequest( + client, + RestrictedIndicesNames.SECURITY_TOKENS_ALIAS, + "token_" + token.hashedToken, + XContentTestUtils.convertToXContent(document, XContentType.JSON) + ); } } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/TokenServiceTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/TokenServiceTests.java index 8d9bb2f855712..12f78b32181b8 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/TokenServiceTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/TokenServiceTests.java @@ -100,6 +100,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.Map; + import javax.crypto.SecretKey; import static java.time.Clock.systemUTC; @@ -126,8 +127,10 @@ public class TokenServiceTests extends ESTestCase { private static ThreadPool threadPool; - private static final Settings settings = Settings.builder().put(Node.NODE_NAME_SETTING.getKey(), "TokenServiceTests") - .put(XPackSettings.TOKEN_SERVICE_ENABLED_SETTING.getKey(), true).build(); + private static final Settings settings = Settings.builder() + .put(Node.NODE_NAME_SETTING.getKey(), "TokenServiceTests") + .put(XPackSettings.TOKEN_SERVICE_ENABLED_SETTING.getKey(), true) + .build(); private Client client; private SecurityIndexManager securityMainIndex; @@ -135,7 +138,8 @@ public class TokenServiceTests extends ESTestCase { private ClusterService clusterService; private DiscoveryNode oldNode; private Settings tokenServiceEnabledSettings = Settings.builder() - .put(XPackSettings.TOKEN_SERVICE_ENABLED_SETTING.getKey(), true).build(); + .put(XPackSettings.TOKEN_SERVICE_ENABLED_SETTING.getKey(), true) + .build(); private MockLicenseState licenseState; private SecurityContext securityContext; @@ -146,27 +150,30 @@ public void setupClient() { when(client.settings()).thenReturn(settings); doAnswer(invocationOnMock -> { GetRequestBuilder builder = new GetRequestBuilder(client, GetAction.INSTANCE); - builder.setIndex((String) invocationOnMock.getArguments()[0]) - .setId((String) invocationOnMock.getArguments()[1]); + builder.setIndex((String) invocationOnMock.getArguments()[0]).setId((String) invocationOnMock.getArguments()[1]); return builder; }).when(client).prepareGet(anyString(), anyString()); - when(client.prepareIndex(any(String.class))) - .thenReturn(new IndexRequestBuilder(client, IndexAction.INSTANCE)); - when(client.prepareBulk()) - .thenReturn(new BulkRequestBuilder(client, BulkAction.INSTANCE)); - when(client.prepareUpdate(any(String.class), any(String.class))) - .thenAnswer(inv -> { - final String index = (String) inv.getArguments()[0]; - final String id = (String) inv.getArguments()[1]; - return new UpdateRequestBuilder(client, UpdateAction.INSTANCE).setIndex(index).setId(id); - }); - when(client.prepareSearch(any(String.class))) - .thenReturn(new SearchRequestBuilder(client, SearchAction.INSTANCE)); + when(client.prepareIndex(any(String.class))).thenReturn(new IndexRequestBuilder(client, IndexAction.INSTANCE)); + when(client.prepareBulk()).thenReturn(new BulkRequestBuilder(client, BulkAction.INSTANCE)); + when(client.prepareUpdate(any(String.class), any(String.class))).thenAnswer(inv -> { + final String index = (String) inv.getArguments()[0]; + final String id = (String) inv.getArguments()[1]; + return new UpdateRequestBuilder(client, UpdateAction.INSTANCE).setIndex(index).setId(id); + }); + when(client.prepareSearch(any(String.class))).thenReturn(new SearchRequestBuilder(client, SearchAction.INSTANCE)); doAnswer(invocationOnMock -> { @SuppressWarnings("unchecked") ActionListener responseActionListener = (ActionListener) invocationOnMock.getArguments()[2]; - responseActionListener.onResponse(new IndexResponse(new ShardId(".security", UUIDs.randomBase64UUID(), randomInt()), - randomAlphaOfLength(4), randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), true)); + responseActionListener.onResponse( + new IndexResponse( + new ShardId(".security", UUIDs.randomBase64UUID(), randomInt()), + randomAlphaOfLength(4), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + true + ) + ); return null; }).when(client).execute(eq(IndexAction.INSTANCE), any(IndexRequest.class), anyActionListener()); doAnswer(invocationOnMock -> { @@ -179,8 +186,14 @@ public void setupClient() { var shardId = new ShardId(securityTokensIndex.aliasName(), indexUUID, 1); var docId = request.requests().get(i).id(); var result = new GetResult(shardId.getIndexName(), docId, 1, 1, 1, true, null, null, null); - final UpdateResponse response = new UpdateResponse(shardId, result.getId(), result.getSeqNo(), result.getPrimaryTerm(), - result.getVersion() + 1, DocWriteResponse.Result.UPDATED); + final UpdateResponse response = new UpdateResponse( + shardId, + result.getId(), + result.getSeqNo(), + result.getPrimaryTerm(), + result.getVersion() + 1, + DocWriteResponse.Result.UPDATED + ); response.setGetResult(result); responses[i] = BulkItemResponse.success(i, DocWriteRequest.OpType.UPDATE, response); } @@ -214,9 +227,10 @@ public void tearDown() throws Exception { @BeforeClass public static void startThreadPool() throws IOException { - threadPool = new ThreadPool(settings, - new FixedExecutorBuilder(settings, TokenService.THREAD_POOL_NAME, 1, 1000, "xpack.security.authc.token.thread_pool", - false)); + threadPool = new ThreadPool( + settings, + new FixedExecutorBuilder(settings, TokenService.THREAD_POOL_NAME, 1, 1000, "xpack.security.authc.token.thread_pool", false) + ); new Authentication(new User("foo"), new RealmRef("realm", "type", "node"), null).writeToContext(threadPool.getThreadContext()); } @@ -314,8 +328,14 @@ public void testRotateKey() throws Exception { PlainActionFuture newTokenFuture = new PlainActionFuture<>(); final String newUserTokenId = UUIDs.randomBase64UUID(); final String newRefreshToken = UUIDs.randomBase64UUID(); - tokenService.createOAuth2Tokens(newUserTokenId, newRefreshToken, authentication, authentication, Collections.emptyMap(), - newTokenFuture); + tokenService.createOAuth2Tokens( + newUserTokenId, + newRefreshToken, + authentication, + authentication, + Collections.emptyMap(), + newTokenFuture + ); final String newAccessToken = newTokenFuture.get().getAccessToken(); assertNotNull(newAccessToken); assertNotEquals(newAccessToken, accessToken); @@ -428,8 +448,14 @@ public void testPruneKeys() throws Exception { PlainActionFuture newTokenFuture = new PlainActionFuture<>(); final String newUserTokenId = UUIDs.randomBase64UUID(); final String newRefreshToken = UUIDs.randomBase64UUID(); - tokenService.createOAuth2Tokens(newUserTokenId, newRefreshToken, authentication, authentication, Collections.emptyMap(), - newTokenFuture); + tokenService.createOAuth2Tokens( + newUserTokenId, + newRefreshToken, + authentication, + authentication, + Collections.emptyMap(), + newTokenFuture + ); final String newAccessToken = newTokenFuture.get().getAccessToken(); assertNotNull(newAccessToken); assertNotEquals(newAccessToken, accessToken); @@ -579,7 +605,9 @@ public void testInvalidateRefreshTokenThatIsAlreadyInvalidated() throws Exceptio final String accessToken = tokenFuture.get().getAccessToken(); final String clientRefreshToken = tokenFuture.get().getRefreshToken(); assertNotNull(accessToken); - mockFindTokenFromRefreshToken(rawRefreshToken, buildUserToken(tokenService, userTokenId, authentication), + mockFindTokenFromRefreshToken( + rawRefreshToken, + buildUserToken(tokenService, userTokenId, authentication), new RefreshTokenStatus(true, randomAlphaOfLength(12), randomAlphaOfLength(6), false, null, null, null, null) ); @@ -603,10 +631,16 @@ private void storeTokenHeader(ThreadContext requestContext, String tokenString) public void testComputeSecretKeyIsConsistent() throws Exception { byte[] saltArr = new byte[32]; random().nextBytes(saltArr); - SecretKey key = - TokenService.computeSecretKey("some random passphrase".toCharArray(), saltArr, TokenService.TOKEN_SERVICE_KEY_ITERATIONS); - SecretKey key2 = - TokenService.computeSecretKey("some random passphrase".toCharArray(), saltArr, TokenService.TOKEN_SERVICE_KEY_ITERATIONS); + SecretKey key = TokenService.computeSecretKey( + "some random passphrase".toCharArray(), + saltArr, + TokenService.TOKEN_SERVICE_KEY_ITERATIONS + ); + SecretKey key2 = TokenService.computeSecretKey( + "some random passphrase".toCharArray(), + saltArr, + TokenService.TOKEN_SERVICE_KEY_ITERATIONS + ); assertArrayEquals(key.getEncoded(), key2.getEncoded()); } @@ -623,14 +657,20 @@ public void testTokenExpiryConfig() { assertThat(expiration, equalTo(TimeValue.timeValueHours(1L))); // Outside range should fail tokenServiceEnabledSettings = Settings.builder().put(TokenService.TOKEN_EXPIRATION.getKey(), "1ms").build(); - IllegalArgumentException ile = expectThrows(IllegalArgumentException.class, - () -> TokenService.TOKEN_EXPIRATION.get(tokenServiceEnabledSettings)); - assertThat(ile.getMessage(), - containsString("failed to parse value [1ms] for setting [xpack.security.authc.token.timeout], must be >= [1s]")); + IllegalArgumentException ile = expectThrows( + IllegalArgumentException.class, + () -> TokenService.TOKEN_EXPIRATION.get(tokenServiceEnabledSettings) + ); + assertThat( + ile.getMessage(), + containsString("failed to parse value [1ms] for setting [xpack.security.authc.token.timeout], must be >= [1s]") + ); tokenServiceEnabledSettings = Settings.builder().put(TokenService.TOKEN_EXPIRATION.getKey(), "120m").build(); ile = expectThrows(IllegalArgumentException.class, () -> TokenService.TOKEN_EXPIRATION.get(tokenServiceEnabledSettings)); - assertThat(ile.getMessage(), - containsString("failed to parse value [120m] for setting [xpack.security.authc.token.timeout], must be <= [1h]")); + assertThat( + ile.getMessage(), + containsString("failed to parse value [120m] for setting [xpack.security.authc.token.timeout], must be <= [1h]") + ); } public void testTokenExpiry() throws Exception { @@ -638,10 +678,17 @@ public void testTokenExpiry() throws Exception { TokenService tokenService = createTokenService(tokenServiceEnabledSettings, clock); Authentication authentication = new Authentication(new User("joe", "admin"), new RealmRef("native_realm", "native", "node1"), null); final String userTokenId = UUIDs.randomBase64UUID(); - UserToken userToken = new UserToken(userTokenId, tokenService.getTokenVersionCompatibility(), authentication, - tokenService.getExpirationTime(), Collections.emptyMap()); + UserToken userToken = new UserToken( + userTokenId, + tokenService.getTokenVersionCompatibility(), + authentication, + tokenService.getExpirationTime(), + Collections.emptyMap() + ); mockGetTokenFromId(userToken, false); - final String accessToken = tokenService.prependVersionAndEncodeAccessToken(tokenService.getTokenVersionCompatibility(), userTokenId + final String accessToken = tokenService.prependVersionAndEncodeAccessToken( + tokenService.getTokenVersionCompatibility(), + userTokenId ); ThreadContext requestContext = new ThreadContext(Settings.EMPTY); @@ -689,12 +736,20 @@ public void testTokenExpiry() throws Exception { } public void testTokenServiceDisabled() throws Exception { - TokenService tokenService = new TokenService(Settings.builder() - .put(XPackSettings.TOKEN_SERVICE_ENABLED_SETTING.getKey(), false) - .build(), - Clock.systemUTC(), client, licenseState, securityContext, securityMainIndex, securityTokensIndex, clusterService); - ElasticsearchException e = expectThrows(ElasticsearchException.class, - () -> tokenService.createOAuth2Tokens(null, null, null, true, null)); + TokenService tokenService = new TokenService( + Settings.builder().put(XPackSettings.TOKEN_SERVICE_ENABLED_SETTING.getKey(), false).build(), + Clock.systemUTC(), + client, + licenseState, + securityContext, + securityMainIndex, + securityTokensIndex, + clusterService + ); + ElasticsearchException e = expectThrows( + ElasticsearchException.class, + () -> tokenService.createOAuth2Tokens(null, null, null, true, null) + ); assertThat(e, throwableWithMessage("security tokens are not enabled")); assertThat(e, instanceOf(FeatureNotEnabledException.class)); // Client can check the metadata for this value, and depend on an exact string match: @@ -851,10 +906,17 @@ public void testGetAuthenticationWorksWithExpiredUserToken() throws Exception { TokenService tokenService = createTokenService(tokenServiceEnabledSettings, Clock.systemUTC()); Authentication authentication = new Authentication(new User("joe", "admin"), new RealmRef("native_realm", "native", "node1"), null); final String userTokenId = UUIDs.randomBase64UUID(); - UserToken expired = new UserToken(userTokenId, tokenService.getTokenVersionCompatibility(), authentication, - Instant.now().minus(3L, ChronoUnit.DAYS), Collections.emptyMap()); + UserToken expired = new UserToken( + userTokenId, + tokenService.getTokenVersionCompatibility(), + authentication, + Instant.now().minus(3L, ChronoUnit.DAYS), + Collections.emptyMap() + ); mockGetTokenFromId(expired, false); - final String accessToken = tokenService.prependVersionAndEncodeAccessToken(tokenService.getTokenVersionCompatibility(), userTokenId + final String accessToken = tokenService.prependVersionAndEncodeAccessToken( + tokenService.getTokenVersionCompatibility(), + userTokenId ); PlainActionFuture>> authFuture = new PlainActionFuture<>(); tokenService.getAuthenticationAndMetadata(accessToken, authFuture); @@ -872,22 +934,31 @@ public void testSupercedingTokenEncryption() throws Exception { final byte[] iv = tokenService.getRandomBytes(TokenService.IV_BYTES); final byte[] salt = tokenService.getRandomBytes(TokenService.SALT_BYTES); final Version version = tokenService.getTokenVersionCompatibility(); - String encryptedTokens = tokenService.encryptSupersedingTokens(newAccessToken, newRefreshToken, refrehToken, iv, - salt); - RefreshTokenStatus refreshTokenStatus = new RefreshTokenStatus(false, - authentication.getUser().principal(), authentication.getAuthenticatedBy().getName(), true, - Instant.now().minusSeconds(5L), encryptedTokens, Base64.getEncoder().encodeToString(iv), - Base64.getEncoder().encodeToString(salt)); + String encryptedTokens = tokenService.encryptSupersedingTokens(newAccessToken, newRefreshToken, refrehToken, iv, salt); + RefreshTokenStatus refreshTokenStatus = new RefreshTokenStatus( + false, + authentication.getUser().principal(), + authentication.getAuthenticatedBy().getName(), + true, + Instant.now().minusSeconds(5L), + encryptedTokens, + Base64.getEncoder().encodeToString(iv), + Base64.getEncoder().encodeToString(salt) + ); refreshTokenStatus.setVersion(version); mockGetTokenAsyncForDecryptedToken(newAccessToken); tokenService.decryptAndReturnSupersedingTokens(refrehToken, refreshTokenStatus, securityTokensIndex, authentication, tokenFuture); if (version.onOrAfter(TokenService.VERSION_ACCESS_TOKENS_AS_UUIDS)) { // previous versions serialized the access token encrypted and the cipher text was different each time (due to different IVs) - assertThat(tokenService.prependVersionAndEncodeAccessToken(version, newAccessToken), - equalTo(tokenFuture.get().getAccessToken())); + assertThat( + tokenService.prependVersionAndEncodeAccessToken(version, newAccessToken), + equalTo(tokenFuture.get().getAccessToken()) + ); } - assertThat(TokenService.prependVersionAndEncodeRefreshToken(version, newRefreshToken), - equalTo(tokenFuture.get().getRefreshToken())); + assertThat( + TokenService.prependVersionAndEncodeRefreshToken(version, newRefreshToken), + equalTo(tokenFuture.get().getRefreshToken()) + ); } public void testCannotValidateTokenIfLicenseDoesNotAllowTokens() throws Exception { @@ -895,10 +966,17 @@ public void testCannotValidateTokenIfLicenseDoesNotAllowTokens() throws Exceptio TokenService tokenService = createTokenService(tokenServiceEnabledSettings, Clock.systemUTC()); Authentication authentication = new Authentication(new User("joe", "admin"), new RealmRef("native_realm", "native", "node1"), null); final String userTokenId = UUIDs.randomBase64UUID(); - UserToken token = new UserToken(userTokenId, tokenService.getTokenVersionCompatibility(), authentication, - Instant.now().plusSeconds(180), Collections.emptyMap()); + UserToken token = new UserToken( + userTokenId, + tokenService.getTokenVersionCompatibility(), + authentication, + Instant.now().plusSeconds(180), + Collections.emptyMap() + ); mockGetTokenFromId(token, false); - final String accessToken = tokenService.prependVersionAndEncodeAccessToken(tokenService.getTokenVersionCompatibility(), userTokenId + final String accessToken = tokenService.prependVersionAndEncodeAccessToken( + tokenService.getTokenVersionCompatibility(), + userTokenId ); final ThreadContext threadContext = new ThreadContext(Settings.EMPTY); storeTokenHeader(threadContext, tokenService.prependVersionAndEncodeAccessToken(token.getVersion(), accessToken)); @@ -917,16 +995,29 @@ public void testHashedTokenIsUrlSafe() { } private TokenService createTokenService(Settings settings, Clock clock) throws GeneralSecurityException { - return new TokenService(settings, clock, client, licenseState, securityContext, securityMainIndex, securityTokensIndex, - clusterService); + return new TokenService( + settings, + clock, + client, + licenseState, + securityContext, + securityMainIndex, + securityTokensIndex, + clusterService + ); } private void mockGetTokenFromId(TokenService tokenService, String accessToken, Authentication authentication, boolean isExpired) { mockGetTokenFromId(tokenService, accessToken, authentication, isExpired, client); } - public static void mockGetTokenFromId(TokenService tokenService, String userTokenId, Authentication authentication, boolean isExpired, - Client client) { + public static void mockGetTokenFromId( + TokenService tokenService, + String userTokenId, + Authentication authentication, + boolean isExpired, + Client client + ) { doAnswer(invocationOnMock -> { GetRequest request = (GetRequest) invocationOnMock.getArguments()[0]; @SuppressWarnings("unchecked") @@ -946,8 +1037,10 @@ public static void mockGetTokenFromId(TokenService tokenService, String userToke try (XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent())) { userToken.toXContent(builder, ToXContent.EMPTY_PARAMS); Map accessTokenMap = new HashMap<>(); - accessTokenMap.put("user_token", - XContentHelper.convertToMap(XContentType.JSON.xContent(), Strings.toString(builder), false)); + accessTokenMap.put( + "user_token", + XContentHelper.convertToMap(XContentType.JSON.xContent(), Strings.toString(builder), false) + ); accessTokenMap.put("invalidated", isExpired); sourceMap.put("access_token", accessTokenMap); } @@ -967,10 +1060,21 @@ protected static UserToken buildUserToken(TokenService tokenService, String user possiblyHashedUserTokenId = userTokenId; } - final Authentication tokenAuth = new Authentication(authentication.getUser(), authentication.getAuthenticatedBy(), - authentication.getLookedUpBy(), tokenVersion, AuthenticationType.TOKEN, authentication.getMetadata()); - final UserToken userToken = new UserToken(possiblyHashedUserTokenId, tokenVersion, tokenAuth, - tokenService.getExpirationTime(), authentication.getMetadata()); + final Authentication tokenAuth = new Authentication( + authentication.getUser(), + authentication.getAuthenticatedBy(), + authentication.getLookedUpBy(), + tokenVersion, + AuthenticationType.TOKEN, + authentication.getMetadata() + ); + final UserToken userToken = new UserToken( + possiblyHashedUserTokenId, + tokenVersion, + tokenAuth, + tokenService.getExpirationTime(), + authentication.getMetadata() + ); return userToken; } @@ -992,8 +1096,11 @@ private void mockGetTokenFromId(UserToken userToken, boolean isExpired) { try (XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent())) { userToken.toXContent(builder, ToXContent.EMPTY_PARAMS); Map accessTokenMap = new HashMap<>(); - Map userTokenMap = XContentHelper.convertToMap(XContentType.JSON.xContent(), - Strings.toString(builder), false); + Map userTokenMap = XContentHelper.convertToMap( + XContentType.JSON.xContent(), + Strings.toString(builder), + false + ); userTokenMap.put("id", possiblyHashedUserTokenId); accessTokenMap.put("user_token", userTokenMap); accessTokenMap.put("invalidated", isExpired); @@ -1036,10 +1143,13 @@ private void mockFindTokenFromRefreshToken(String refreshToken, UserToken userTo final RealmRef realmRef = new RealmRef( refreshTokenStatus == null ? randomAlphaOfLength(6) : refreshTokenStatus.getAssociatedRealm(), "test", - randomAlphaOfLength(12)); + randomAlphaOfLength(12) + ); final Authentication clientAuthentication = new Authentication( new User(refreshTokenStatus == null ? randomAlphaOfLength(8) : refreshTokenStatus.getAssociatedUser()), - realmRef, realmRef); + realmRef, + realmRef + ); final SearchHit hit = new SearchHit(randomInt(), "token_" + TokenService.hashTokenString(userToken.getId()), null, null); BytesReference source = TokenService.createTokenDocument(userToken, storedRefreshToken, clientAuthentication, Instant.now()); @@ -1053,7 +1163,7 @@ private void mockFindTokenFromRefreshToken(String refreshToken, UserToken userTo } hit.sourceRef(source); - final SearchHits hits = new SearchHits(new SearchHit[]{hit}, null, 1); + final SearchHits hits = new SearchHits(new SearchHit[] { hit }, null, 1); when(response.getHits()).thenReturn(hits); listener.onResponse(response); return Void.TYPE; @@ -1084,8 +1194,13 @@ public static void assertAuthentication(Authentication result, Authentication ex private DiscoveryNode addAnotherDataNodeWithVersion(ClusterService clusterService, Version version) { final ClusterState currentState = clusterService.state(); final DiscoveryNodes.Builder discoBuilder = DiscoveryNodes.builder(currentState.getNodes()); - final DiscoveryNode anotherDataNode = new DiscoveryNode("another_data_node#" + version, buildNewFakeTransportAddress(), - Collections.emptyMap(), Collections.singleton(DiscoveryNodeRole.DATA_ROLE), version); + final DiscoveryNode anotherDataNode = new DiscoveryNode( + "another_data_node#" + version, + buildNewFakeTransportAddress(), + Collections.emptyMap(), + Collections.singleton(DiscoveryNodeRole.DATA_ROLE), + version + ); discoBuilder.add(anotherDataNode); final ClusterState.Builder newStateBuilder = ClusterState.builder(currentState); newStateBuilder.nodes(discoBuilder); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/esnative/NativeRealmTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/esnative/NativeRealmTests.java index 3556f826c9e33..101a9705e876d 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/esnative/NativeRealmTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/esnative/NativeRealmTests.java @@ -27,11 +27,23 @@ public class NativeRealmTests extends ESTestCase { private final String concreteSecurityIndexName = randomFrom( - RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_6, RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7); + RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_6, + RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7 + ); private SecurityIndexManager.State dummyState(ClusterHealthStatus indexStatus) { return new SecurityIndexManager.State( - Instant.now(), true, true, true, null, concreteSecurityIndexName, indexStatus, IndexMetadata.State.OPEN, null, "my_uuid"); + Instant.now(), + true, + true, + true, + null, + concreteSecurityIndexName, + indexStatus, + IndexMetadata.State.OPEN, + null, + "my_uuid" + ); } public void testCacheClearOnIndexHealthChange() { @@ -41,8 +53,10 @@ public void testCacheClearOnIndexHealthChange() { final AtomicInteger numInvalidation = new AtomicInteger(0); int expectedInvalidation = 0; RealmConfig.RealmIdentifier realmId = new RealmConfig.RealmIdentifier("native", "native"); - Settings settings = Settings.builder().put("path.home", createTempDir()) - .put(RealmSettings.realmSettingPrefix(realmId) + "order", 0).build(); + Settings settings = Settings.builder() + .put("path.home", createTempDir()) + .put(RealmSettings.realmSettingPrefix(realmId) + "order", 0) + .build(); RealmConfig config = new RealmConfig(realmId, settings, TestEnvironment.newEnvironment(settings), new ThreadContext(settings)); final NativeRealm nativeRealm = new NativeRealm(config, mock(NativeUsersStore.class), threadPool) { @Override @@ -77,8 +91,9 @@ void clearCache() { // green to yellow or yellow to green previousState = dummyState(randomFrom(ClusterHealthStatus.GREEN, ClusterHealthStatus.YELLOW)); - currentState = dummyState(previousState.indexHealth == ClusterHealthStatus.GREEN ? - ClusterHealthStatus.YELLOW : ClusterHealthStatus.GREEN); + currentState = dummyState( + previousState.indexHealth == ClusterHealthStatus.GREEN ? ClusterHealthStatus.YELLOW : ClusterHealthStatus.GREEN + ); nativeRealm.onSecurityIndexStateChange(previousState, currentState); assertEquals(expectedInvalidation, numInvalidation.get()); } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/esnative/NativeUsersStoreTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/esnative/NativeUsersStoreTests.java index 765de7dc6f8dd..6343a187763d0 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/esnative/NativeUsersStoreTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/esnative/NativeUsersStoreTests.java @@ -54,8 +54,8 @@ import java.util.concurrent.ExecutionException; import java.util.function.Consumer; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.index.seqno.SequenceNumbers.UNASSIGNED_SEQ_NO; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.notNullValue; @@ -85,8 +85,11 @@ public void setupMocks() { client = new FilterClient(mockClient) { @Override - protected - void doExecute(ActionType action, Request request, ActionListener listener) { + protected void doExecute( + ActionType action, + Request request, + ActionListener listener + ) { requests.add(new Tuple<>(request, listener)); } }; @@ -95,8 +98,15 @@ void doExecute(ActionType action, Request request, ActionListener future = new PlainActionFuture<>(); nativeUsersStore.setEnabled(user, true, WriteRequest.RefreshPolicy.IMMEDIATE, future); @@ -114,20 +124,30 @@ public void testPasswordUpsertWhenSetEnabledOnReservedUser() throws Exception { public void testBlankPasswordInIndexImpliesDefaultPassword() throws Exception { final NativeUsersStore nativeUsersStore = startNativeUsersStore(); - final String user = randomFrom(ElasticUser.NAME, KibanaUser.NAME, KibanaSystemUser.NAME, - LogstashSystemUser.NAME, BeatsSystemUser.NAME, APMSystemUser.NAME, RemoteMonitoringUser.NAME); + final String user = randomFrom( + ElasticUser.NAME, + KibanaUser.NAME, + KibanaSystemUser.NAME, + LogstashSystemUser.NAME, + BeatsSystemUser.NAME, + APMSystemUser.NAME, + RemoteMonitoringUser.NAME + ); final Map values = new HashMap<>(); values.put(ENABLED_FIELD, Boolean.TRUE); values.put(PASSWORD_FIELD, BLANK_PASSWORD); final GetResult result = new GetResult( - RestrictedIndicesNames.SECURITY_MAIN_ALIAS, - NativeUsersStore.getIdForUser(NativeUsersStore.RESERVED_USER_TYPE, randomAlphaOfLength(12)), - 0, 1, 1L, - true, - BytesReference.bytes(jsonBuilder().map(values)), - Collections.emptyMap(), - Collections.emptyMap()); + RestrictedIndicesNames.SECURITY_MAIN_ALIAS, + NativeUsersStore.getIdForUser(NativeUsersStore.RESERVED_USER_TYPE, randomAlphaOfLength(12)), + 0, + 1, + 1L, + true, + BytesReference.bytes(jsonBuilder().map(values)), + Collections.emptyMap(), + Collections.emptyMap() + ); final PlainActionFuture future = new PlainActionFuture<>(); nativeUsersStore.getReservedUserInfo(user, future); @@ -190,13 +210,16 @@ public void testVerifyNonExistentUser() throws Exception { nativeUsersStore.verifyPassword(username, password, future); final GetResult getResult = new GetResult( - RestrictedIndicesNames.SECURITY_MAIN_ALIAS, - NativeUsersStore.getIdForUser(NativeUsersStore.USER_DOC_TYPE, username), - UNASSIGNED_SEQ_NO, 0, 1L, - false, - null, - Collections.emptyMap(), - Collections.emptyMap()); + RestrictedIndicesNames.SECURITY_MAIN_ALIAS, + NativeUsersStore.getIdForUser(NativeUsersStore.USER_DOC_TYPE, username), + UNASSIGNED_SEQ_NO, + 0, + 1L, + false, + null, + Collections.emptyMap(), + Collections.emptyMap() + ); actionRespond(GetRequest.class, new GetResponse(getResult)); @@ -210,8 +233,10 @@ public void testVerifyNonExistentUser() throws Exception { public void testDefaultReservedUserInfoPasswordEmpty() { NativeUsersStore.ReservedUserInfo disabledUserInfo = NativeUsersStore.ReservedUserInfo.defaultDisabledUserInfo(); NativeUsersStore.ReservedUserInfo enabledUserInfo = NativeUsersStore.ReservedUserInfo.defaultEnabledUserInfo(); - NativeUsersStore.ReservedUserInfo constructedUserInfo = - new NativeUsersStore.ReservedUserInfo(Hasher.PBKDF2.hash(new SecureString(randomAlphaOfLength(14))), randomBoolean()); + NativeUsersStore.ReservedUserInfo constructedUserInfo = new NativeUsersStore.ReservedUserInfo( + Hasher.PBKDF2.hash(new SecureString(randomAlphaOfLength(14))), + randomBoolean() + ); assertThat(disabledUserInfo.hasEmptyPassword(), equalTo(true)); assertThat(enabledUserInfo.hasEmptyPassword(), equalTo(true)); @@ -249,19 +274,21 @@ public void testCreateElasticUser() throws Exception { } @SuppressWarnings("unchecked") - private ARequest actionRespond(Class requestClass, - AResponse response) { + private ARequest actionRespond( + Class requestClass, + AResponse response + ) { Tuple> tuple = findRequest(requestClass); ((ActionListener) tuple.v2()).onResponse(response); return tuple.v1(); } - private Tuple> findRequest( - Class requestClass) { + private Tuple> findRequest(Class requestClass) { return this.requests.stream() .filter(t -> requestClass.isInstance(t.v1())) .map(t -> new Tuple>(requestClass.cast(t.v1()), t.v2())) - .findFirst().orElseThrow(() -> new RuntimeException("Cannot find request of type " + requestClass)); + .findFirst() + .orElseThrow(() -> new RuntimeException("Cannot find request of type " + requestClass)); } private void respondToGetUserRequest(String username, SecureString password, String[] roles) throws IOException { @@ -274,13 +301,16 @@ private void respondToGetUserRequest(String username, SecureString password, Str values.put(User.Fields.TYPE.getPreferredName(), NativeUsersStore.USER_DOC_TYPE); final BytesReference source = BytesReference.bytes(jsonBuilder().map(values)); final GetResult getResult = new GetResult( - RestrictedIndicesNames.SECURITY_MAIN_ALIAS, - NativeUsersStore.getIdForUser(NativeUsersStore.USER_DOC_TYPE, username), - 0, 1, 1L, - true, - source, - Collections.emptyMap(), - Collections.emptyMap()); + RestrictedIndicesNames.SECURITY_MAIN_ALIAS, + NativeUsersStore.getIdForUser(NativeUsersStore.USER_DOC_TYPE, username), + 0, + 1, + 1L, + true, + source, + Collections.emptyMap(), + Collections.emptyMap() + ); actionRespond(GetRequest.class, new GetResponse(getResult)); } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/esnative/ReservedRealmTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/esnative/ReservedRealmTests.java index cd24e697bd18d..425d6d435d9c1 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/esnative/ReservedRealmTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/esnative/ReservedRealmTests.java @@ -85,34 +85,42 @@ public void setupMocks() throws Exception { public void testInvalidHashingAlgorithmFails() { final String invalidAlgoId = randomFrom("sha1", "md5", "noop"); final Settings invalidSettings = Settings.builder().put("xpack.security.authc.password_hashing.algorithm", invalidAlgoId).build(); - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> new ReservedRealm(mock(Environment.class), - invalidSettings, usersStore, new AnonymousUser(Settings.EMPTY), threadPool)); + IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> new ReservedRealm(mock(Environment.class), invalidSettings, usersStore, new AnonymousUser(Settings.EMPTY), threadPool) + ); assertThat(exception.getMessage(), containsString(invalidAlgoId)); assertThat(exception.getMessage(), containsString("Invalid algorithm")); } public void testInvalidAutoConfigPasswordHashFails() { - char[] invalidAutoConfHash = - randomFrom(Hasher.MD5.hash(new SecureString(randomAlphaOfLengthBetween(0, 8).toCharArray())), - Hasher.SHA1.hash(new SecureString(randomAlphaOfLengthBetween(0, 8).toCharArray())), - Hasher.SSHA256.hash(new SecureString(randomAlphaOfLengthBetween(0, 8).toCharArray())), - randomAlphaOfLengthBetween(1, 16).toCharArray(), - new char[0] - ); + char[] invalidAutoConfHash = randomFrom( + Hasher.MD5.hash(new SecureString(randomAlphaOfLengthBetween(0, 8).toCharArray())), + Hasher.SHA1.hash(new SecureString(randomAlphaOfLengthBetween(0, 8).toCharArray())), + Hasher.SSHA256.hash(new SecureString(randomAlphaOfLengthBetween(0, 8).toCharArray())), + randomAlphaOfLengthBetween(1, 16).toCharArray(), + new char[0] + ); MockSecureSettings mockSecureSettings = new MockSecureSettings(); mockSecureSettings.setString("autoconfiguration.password_hash", new String(invalidAutoConfHash)); if (randomBoolean()) { mockSecureSettings.setString("bootstrap.password", "foobar longer than 14 chars because of FIPS"); } Settings invalidSettings = Settings.builder().setSecureSettings(mockSecureSettings).build(); - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> new ReservedRealm(mock(Environment.class), - invalidSettings, usersStore, new AnonymousUser(Settings.EMPTY), threadPool)); + IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> new ReservedRealm(mock(Environment.class), invalidSettings, usersStore, new AnonymousUser(Settings.EMPTY), threadPool) + ); assertThat(exception.getMessage(), containsString("Invalid password hash for elastic user auto configuration")); } public void testReservedUserEmptyPasswordAuthenticationFails() throws Throwable { - final String principal = randomFrom(UsernamesField.ELASTIC_NAME, UsernamesField.KIBANA_NAME, UsernamesField.LOGSTASH_NAME, - UsernamesField.BEATS_NAME); + final String principal = randomFrom( + UsernamesField.ELASTIC_NAME, + UsernamesField.KIBANA_NAME, + UsernamesField.LOGSTASH_NAME, + UsernamesField.BEATS_NAME + ); SecureString password = new SecureString("password longer than 14 chars because of FIPS".toCharArray()); // Mocked users store is initiated with default hashing algorithm final Hasher hasher = Hasher.resolve("bcrypt"); @@ -120,8 +128,13 @@ public void testReservedUserEmptyPasswordAuthenticationFails() throws Throwable ReservedUserInfo userInfo = new ReservedUserInfo(hash, true); mockGetAllReservedUserInfo(usersStore, Collections.singletonMap(principal, userInfo)); - final ReservedRealm reservedRealm = new ReservedRealm(mock(Environment.class), Settings.EMPTY, usersStore, - new AnonymousUser(Settings.EMPTY), threadPool); + final ReservedRealm reservedRealm = new ReservedRealm( + mock(Environment.class), + Settings.EMPTY, + usersStore, + new AnonymousUser(Settings.EMPTY), + threadPool + ); PlainActionFuture> listener = new PlainActionFuture<>(); @@ -135,17 +148,26 @@ public void testAuthenticationDisabled() throws Throwable { mockSecureSettings.setString("bootstrap.password", "foobar longer than 14 chars because of FIPS"); } if (randomBoolean()) { - mockSecureSettings.setString("autoconfiguration.password_hash", - new String(randomFrom(Hasher.BCRYPT, Hasher.PBKDF2).hash( - new SecureString("barbaz longer than 14 chars because of FIPS".toCharArray())))); + mockSecureSettings.setString( + "autoconfiguration.password_hash", + new String( + randomFrom(Hasher.BCRYPT, Hasher.PBKDF2).hash( + new SecureString("barbaz longer than 14 chars because of FIPS".toCharArray()) + ) + ) + ); } Settings settings = Settings.builder() .put(XPackSettings.RESERVED_REALM_ENABLED_SETTING.getKey(), false) .setSecureSettings(mockSecureSettings) .build(); - final ReservedRealm reservedRealm = - new ReservedRealm(mock(Environment.class), settings, usersStore, - new AnonymousUser(settings), threadPool); + final ReservedRealm reservedRealm = new ReservedRealm( + mock(Environment.class), + settings, + usersStore, + new AnonymousUser(settings), + threadPool + ); final User expected = randomReservedUser(true); final String principal = expected.principal(); @@ -166,8 +188,13 @@ public void testAuthenticationDisabledUserWithStoredPassword() throws Throwable } private void verifySuccessfulAuthentication(boolean enabled) throws Exception { - final ReservedRealm reservedRealm = new ReservedRealm(mock(Environment.class), Settings.EMPTY, usersStore, - new AnonymousUser(Settings.EMPTY), threadPool); + final ReservedRealm reservedRealm = new ReservedRealm( + mock(Environment.class), + Settings.EMPTY, + usersStore, + new AnonymousUser(Settings.EMPTY), + threadPool + ); final User expectedUser = randomReservedUser(enabled); final String principal = expectedUser.principal(); final SecureString newPassword = new SecureString("foobar longer than 14 chars because of FIPS".toCharArray()); @@ -194,8 +221,10 @@ private void verifySuccessfulAuthentication(boolean enabled) throws Exception { verifyNoMoreInteractions(usersStore); if (new KibanaUser(enabled).equals(expectedUser)) { - assertWarnings("The user [kibana] is deprecated and will be removed in a future version of Elasticsearch. " + - "Please use the [kibana_system] user instead."); + assertWarnings( + "The user [kibana] is deprecated and will be removed in a future version of Elasticsearch. " + + "Please use the [kibana_system] user instead." + ); } } @@ -217,16 +246,22 @@ public void testLookup() throws Exception { mockSecureSettings.setString("bootstrap.password", "foobar longer than 14 chars because of FIPS"); } if (randomBoolean()) { - mockSecureSettings.setString("autoconfiguration.password_hash", - new String(randomFrom(Hasher.BCRYPT, Hasher.PBKDF2).hash( - new SecureString("barbaz longer than 14 chars because of FIPS".toCharArray())))); + mockSecureSettings.setString( + "autoconfiguration.password_hash", + new String( + randomFrom(Hasher.BCRYPT, Hasher.PBKDF2).hash( + new SecureString("barbaz longer than 14 chars because of FIPS".toCharArray()) + ) + ) + ); } - final ReservedRealm reservedRealm = - new ReservedRealm(mock(Environment.class), - Settings.builder() - .setSecureSettings(mockSecureSettings) - .build(), usersStore, - new AnonymousUser(Settings.EMPTY), threadPool); + final ReservedRealm reservedRealm = new ReservedRealm( + mock(Environment.class), + Settings.builder().setSecureSettings(mockSecureSettings).build(), + usersStore, + new AnonymousUser(Settings.EMPTY), + threadPool + ); PlainActionFuture listener = new PlainActionFuture<>(); reservedRealm.doLookupUser(principal, listener); @@ -247,17 +282,26 @@ public void testLookupDisabled() throws Exception { mockSecureSettings.setString("bootstrap.password", "foobar longer than 14 chars because of FIPS"); } if (randomBoolean()) { - mockSecureSettings.setString("autoconfiguration.password_hash", - new String(randomFrom(Hasher.BCRYPT, Hasher.PBKDF2).hash( - new SecureString("barbaz longer than 14 chars because of FIPS".toCharArray())))); + mockSecureSettings.setString( + "autoconfiguration.password_hash", + new String( + randomFrom(Hasher.BCRYPT, Hasher.PBKDF2).hash( + new SecureString("barbaz longer than 14 chars because of FIPS".toCharArray()) + ) + ) + ); } Settings settings = Settings.builder() .put(XPackSettings.RESERVED_REALM_ENABLED_SETTING.getKey(), false) .setSecureSettings(mockSecureSettings) .build(); - final ReservedRealm reservedRealm = - new ReservedRealm(mock(Environment.class), settings, usersStore, new AnonymousUser(settings), - threadPool); + final ReservedRealm reservedRealm = new ReservedRealm( + mock(Environment.class), + settings, + usersStore, + new AnonymousUser(settings), + threadPool + ); final User expectedUser = randomReservedUser(true); final String principal = expectedUser.principal(); @@ -274,18 +318,27 @@ public void testLookupDisabledAnonymous() throws Exception { mockSecureSettings.setString("bootstrap.password", "foobar longer than 14 chars because of FIPS"); } if (randomBoolean()) { - mockSecureSettings.setString("autoconfiguration.password_hash", - new String(randomFrom(Hasher.BCRYPT, Hasher.PBKDF2).hash( - new SecureString("barbaz longer than 14 chars because of FIPS".toCharArray())))); + mockSecureSettings.setString( + "autoconfiguration.password_hash", + new String( + randomFrom(Hasher.BCRYPT, Hasher.PBKDF2).hash( + new SecureString("barbaz longer than 14 chars because of FIPS".toCharArray()) + ) + ) + ); } Settings settings = Settings.builder() .put(XPackSettings.RESERVED_REALM_ENABLED_SETTING.getKey(), false) .put(AnonymousUser.ROLES_SETTING.getKey(), "anonymous") .setSecureSettings(mockSecureSettings) .build(); - final ReservedRealm reservedRealm = - new ReservedRealm(mock(Environment.class), settings, usersStore, new AnonymousUser(settings), - threadPool); + final ReservedRealm reservedRealm = new ReservedRealm( + mock(Environment.class), + settings, + usersStore, + new AnonymousUser(settings), + threadPool + ); final User expectedUser = new AnonymousUser(settings); final String principal = expectedUser.principal(); @@ -296,9 +349,13 @@ public void testLookupDisabledAnonymous() throws Exception { } public void testLookupThrows() throws Exception { - final ReservedRealm reservedRealm = - new ReservedRealm(mock(Environment.class), Settings.EMPTY, usersStore, - new AnonymousUser(Settings.EMPTY), threadPool); + final ReservedRealm reservedRealm = new ReservedRealm( + mock(Environment.class), + Settings.EMPTY, + usersStore, + new AnonymousUser(Settings.EMPTY), + threadPool + ); final User expectedUser = randomReservedUser(true); final String principal = expectedUser.principal(); final RuntimeException e = new RuntimeException("store threw"); @@ -338,13 +395,27 @@ public void testIsReservedDisabled() { } public void testGetUsers() { - final ReservedRealm reservedRealm = new ReservedRealm(mock(Environment.class), Settings.EMPTY, usersStore, - new AnonymousUser(Settings.EMPTY), threadPool); + final ReservedRealm reservedRealm = new ReservedRealm( + mock(Environment.class), + Settings.EMPTY, + usersStore, + new AnonymousUser(Settings.EMPTY), + threadPool + ); PlainActionFuture> userFuture = new PlainActionFuture<>(); reservedRealm.users(userFuture); - assertThat(userFuture.actionGet(), - containsInAnyOrder(new ElasticUser(true), new KibanaUser(true), new KibanaSystemUser(true), - new LogstashSystemUser(true), new BeatsSystemUser(true), new APMSystemUser(true), new RemoteMonitoringUser(true))); + assertThat( + userFuture.actionGet(), + containsInAnyOrder( + new ElasticUser(true), + new KibanaUser(true), + new KibanaSystemUser(true), + new LogstashSystemUser(true), + new BeatsSystemUser(true), + new APMSystemUser(true), + new RemoteMonitoringUser(true) + ) + ); } public void testGetUsersDisabled() { @@ -354,9 +425,14 @@ public void testGetUsersDisabled() { mockSecureSettings.setString("bootstrap.password", "foobar longer than 14 chars because of FIPS"); } if (randomBoolean()) { - mockSecureSettings.setString("autoconfiguration.password_hash", - new String(randomFrom(Hasher.BCRYPT, Hasher.PBKDF2).hash( - new SecureString("barbaz longer than 14 chars because of FIPS".toCharArray())))); + mockSecureSettings.setString( + "autoconfiguration.password_hash", + new String( + randomFrom(Hasher.BCRYPT, Hasher.PBKDF2).hash( + new SecureString("barbaz longer than 14 chars because of FIPS".toCharArray()) + ) + ) + ); } Settings settings = Settings.builder() .put(XPackSettings.RESERVED_REALM_ENABLED_SETTING.getKey(), false) @@ -364,8 +440,7 @@ public void testGetUsersDisabled() { .setSecureSettings(mockSecureSettings) .build(); final AnonymousUser anonymousUser = new AnonymousUser(settings); - final ReservedRealm reservedRealm = new ReservedRealm(mock(Environment.class), settings, usersStore, anonymousUser, - threadPool); + final ReservedRealm reservedRealm = new ReservedRealm(mock(Environment.class), settings, usersStore, anonymousUser, threadPool); PlainActionFuture> userFuture = new PlainActionFuture<>(); reservedRealm.users(userFuture); if (anonymousEnabled) { @@ -381,9 +456,14 @@ public void testFailedAuthentication() throws Exception { mockSecureSettings.setString("bootstrap.password", "foobar longer than 14 chars because of FIPS"); } if (randomBoolean()) { - mockSecureSettings.setString("autoconfiguration.password_hash", - new String(randomFrom(Hasher.BCRYPT, Hasher.PBKDF2).hash( - new SecureString("barbaz longer than 14 chars because of FIPS".toCharArray())))); + mockSecureSettings.setString( + "autoconfiguration.password_hash", + new String( + randomFrom(Hasher.BCRYPT, Hasher.PBKDF2).hash( + new SecureString("barbaz longer than 14 chars because of FIPS".toCharArray()) + ) + ) + ); } SecureString password = new SecureString("password".toCharArray()); // Mocked users store is initiated with default hashing algorithm @@ -394,10 +474,13 @@ public void testFailedAuthentication() throws Exception { String principal = reservedUser.principal(); ReservedUserInfo userInfo = new ReservedUserInfo(hash, true); mockGetAllReservedUserInfo(usersStore, Collections.singletonMap(principal, userInfo)); - final ReservedRealm reservedRealm = new ReservedRealm(mock(Environment.class), + final ReservedRealm reservedRealm = new ReservedRealm( + mock(Environment.class), Settings.builder().setSecureSettings(mockSecureSettings).build(), usersStore, - new AnonymousUser(Settings.EMPTY), threadPool); + new AnonymousUser(Settings.EMPTY), + threadPool + ); if (randomBoolean()) { PlainActionFuture> future = new PlainActionFuture<>(); @@ -405,14 +488,18 @@ public void testFailedAuthentication() throws Exception { User user = future.actionGet().getValue(); assertEquals(reservedUser, user); if (new KibanaUser(enabled).equals(reservedUser)) { - assertWarnings("The user [kibana] is deprecated and will be removed in a future version of Elasticsearch. " + - "Please use the [kibana_system] user instead."); + assertWarnings( + "The user [kibana] is deprecated and will be removed in a future version of Elasticsearch. " + + "Please use the [kibana_system] user instead." + ); } } PlainActionFuture> future = new PlainActionFuture<>(); - reservedRealm.authenticate(new UsernamePasswordToken(principal, - new SecureString("foobar longer than 14 chars because of FIPS".toCharArray())), future); + reservedRealm.authenticate( + new UsernamePasswordToken(principal, new SecureString("foobar longer than 14 chars because of FIPS".toCharArray())), + future + ); assertFailedAuthentication(future, principal); } @@ -437,48 +524,72 @@ public void testBootstrapElasticPasswordWorksWhenElasticUserIsMissing() throws E mockSecureSettings.setString("bootstrap.password", "foobar longer than 14 chars because of FIPS"); Settings settings = Settings.builder().setSecureSettings(mockSecureSettings).build(); - ReservedRealm reservedRealm = new ReservedRealm(mock(Environment.class), settings, usersStore, - new AnonymousUser(Settings.EMPTY), threadPool); + ReservedRealm reservedRealm = new ReservedRealm( + mock(Environment.class), + settings, + usersStore, + new AnonymousUser(Settings.EMPTY), + threadPool + ); PlainActionFuture> listener = new PlainActionFuture<>(); - reservedRealm.doAuthenticate(new UsernamePasswordToken(new ElasticUser(true).principal(), - mockSecureSettings.getString("bootstrap.password")), - listener); + reservedRealm.doAuthenticate( + new UsernamePasswordToken(new ElasticUser(true).principal(), mockSecureSettings.getString("bootstrap.password")), + listener + ); AuthenticationResult result = listener.get(); assertThat(result.getStatus(), is(AuthenticationResult.Status.SUCCESS)); // add auto configured password which should be ignored because the bootstrap password has priority - mockSecureSettings.setString("autoconfiguration.password_hash", new String(randomFrom(Hasher.BCRYPT, Hasher.PBKDF2) - .hash(new SecureString("bazbar longer than 14 chars because of FIPS".toCharArray())))); + mockSecureSettings.setString( + "autoconfiguration.password_hash", + new String( + randomFrom(Hasher.BCRYPT, Hasher.PBKDF2).hash(new SecureString("bazbar longer than 14 chars because of FIPS".toCharArray())) + ) + ); settings = Settings.builder().setSecureSettings(mockSecureSettings).build(); reservedRealm = new ReservedRealm(mock(Environment.class), settings, usersStore, new AnonymousUser(Settings.EMPTY), threadPool); // authn still works for the bootstrap password listener = new PlainActionFuture<>(); - reservedRealm.doAuthenticate(new UsernamePasswordToken(new ElasticUser(true).principal(), - new SecureString("foobar longer than 14 chars because of FIPS".toCharArray())), - listener); + reservedRealm.doAuthenticate( + new UsernamePasswordToken( + new ElasticUser(true).principal(), + new SecureString("foobar longer than 14 chars because of FIPS".toCharArray()) + ), + listener + ); result = listener.get(); assertThat(result.getStatus(), is(AuthenticationResult.Status.SUCCESS)); // authn fails for the auto configured password hash listener = new PlainActionFuture<>(); - reservedRealm.doAuthenticate(new UsernamePasswordToken(new ElasticUser(true).principal(), - new SecureString("bazbar longer than 14 chars because of FIPS".toCharArray())), - listener); + reservedRealm.doAuthenticate( + new UsernamePasswordToken( + new ElasticUser(true).principal(), + new SecureString("bazbar longer than 14 chars because of FIPS".toCharArray()) + ), + listener + ); assertFailedAuthentication(listener, ElasticUser.NAME); } public void testAutoconfigElasticPasswordWorksWhenElasticUserIsMissing() throws Exception { MockSecureSettings mockSecureSettings = new MockSecureSettings(); char[] autoconfHash = randomFrom(Hasher.BCRYPT, Hasher.PBKDF2).hash( - new SecureString("foobar longer than 14 chars because of FIPS".toCharArray())); + new SecureString("foobar longer than 14 chars because of FIPS".toCharArray()) + ); mockSecureSettings.setString("autoconfiguration.password_hash", new String(autoconfHash)); Settings settings = Settings.builder().setSecureSettings(mockSecureSettings).build(); - final ReservedRealm reservedRealm = new ReservedRealm(mock(Environment.class), settings, usersStore, - new AnonymousUser(Settings.EMPTY), threadPool); + final ReservedRealm reservedRealm = new ReservedRealm( + mock(Environment.class), + settings, + usersStore, + new AnonymousUser(Settings.EMPTY), + threadPool + ); PlainActionFuture> listener = new PlainActionFuture<>(); doAnswer((i) -> { @@ -494,9 +605,13 @@ public void testAutoconfigElasticPasswordWorksWhenElasticUserIsMissing() throws callback.onResponse(null); return null; }).when(usersStore).createElasticUser(any(char[].class), anyActionListener()); - reservedRealm.doAuthenticate(new UsernamePasswordToken(new ElasticUser(true).principal(), - new SecureString("foobar longer than 14 chars because of FIPS".toCharArray())), - listener); + reservedRealm.doAuthenticate( + new UsernamePasswordToken( + new ElasticUser(true).principal(), + new SecureString("foobar longer than 14 chars because of FIPS".toCharArray()) + ), + listener + ); AuthenticationResult result = listener.get(); assertThat(result.getStatus(), is(AuthenticationResult.Status.SUCCESS)); verify(usersStore).getReservedUserInfo(eq("elastic"), anyActionListener()); @@ -506,9 +621,10 @@ public void testAutoconfigElasticPasswordWorksWhenElasticUserIsMissing() throws // wrong password doesn't attempt to promote listener = new PlainActionFuture<>(); - reservedRealm.doAuthenticate(new UsernamePasswordToken(new ElasticUser(true).principal(), - new SecureString("wrong password".toCharArray())), - listener); + reservedRealm.doAuthenticate( + new UsernamePasswordToken(new ElasticUser(true).principal(), new SecureString("wrong password".toCharArray())), + listener + ); assertFailedAuthentication(listener, ElasticUser.NAME); verify(usersStore, times(2)).getReservedUserInfo(eq("elastic"), anyActionListener()); verify(usersStore).createElasticUser(any(char[].class), anyActionListener()); @@ -517,12 +633,18 @@ public void testAutoconfigElasticPasswordWorksWhenElasticUserIsMissing() throws public void testAutoconfigElasticPasswordAuthnErrorWhenHashPromotionFails() throws Exception { MockSecureSettings mockSecureSettings = new MockSecureSettings(); char[] autoconfHash = randomFrom(Hasher.BCRYPT, Hasher.PBKDF2).hash( - new SecureString("foobar longer than 14 chars because of FIPS".toCharArray())); + new SecureString("foobar longer than 14 chars because of FIPS".toCharArray()) + ); mockSecureSettings.setString("autoconfiguration.password_hash", new String(autoconfHash)); Settings settings = Settings.builder().setSecureSettings(mockSecureSettings).build(); - final ReservedRealm reservedRealm = new ReservedRealm(mock(Environment.class), settings, usersStore, - new AnonymousUser(Settings.EMPTY), threadPool); + final ReservedRealm reservedRealm = new ReservedRealm( + mock(Environment.class), + settings, + usersStore, + new AnonymousUser(Settings.EMPTY), + threadPool + ); PlainActionFuture> listener = new PlainActionFuture<>(); doAnswer((i) -> { @@ -538,12 +660,16 @@ public void testAutoconfigElasticPasswordAuthnErrorWhenHashPromotionFails() thro callback.onFailure(new Exception("any failure to promote the auto configured password")); return null; }).when(usersStore).createElasticUser(any(char[].class), anyActionListener()); - reservedRealm.doAuthenticate(new UsernamePasswordToken(new ElasticUser(true).principal(), - new SecureString("foobar longer than 14 chars because of FIPS".toCharArray())), - listener); + reservedRealm.doAuthenticate( + new UsernamePasswordToken( + new ElasticUser(true).principal(), + new SecureString("foobar longer than 14 chars because of FIPS".toCharArray()) + ), + listener + ); ExecutionException exception = expectThrows(ExecutionException.class, () -> listener.get()); assertThat(exception.getCause(), instanceOf(ElasticsearchAuthenticationProcessingError.class)); - assertThat(((ElasticsearchAuthenticationProcessingError)exception.getCause()).status(), is(RestStatus.INTERNAL_SERVER_ERROR)); + assertThat(((ElasticsearchAuthenticationProcessingError) exception.getCause()).status(), is(RestStatus.INTERNAL_SERVER_ERROR)); verify(usersStore).getReservedUserInfo(eq("elastic"), anyActionListener()); ArgumentCaptor userHashCaptor = ArgumentCaptor.forClass(char[].class); verify(usersStore).createElasticUser(userHashCaptor.capture(), anyActionListener()); @@ -554,14 +680,24 @@ public void testBootstrapElasticPasswordFailsOnceElasticUserExists() throws Exce MockSecureSettings mockSecureSettings = new MockSecureSettings(); mockSecureSettings.setString("bootstrap.password", "foobar longer than 14 chars because of FIPS"); if (randomBoolean()) { - mockSecureSettings.setString("autoconfiguration.password_hash", - new String(randomFrom(Hasher.BCRYPT, Hasher.PBKDF2).hash( - new SecureString("barbaz longer than 14 chars because of FIPS".toCharArray())))); + mockSecureSettings.setString( + "autoconfiguration.password_hash", + new String( + randomFrom(Hasher.BCRYPT, Hasher.PBKDF2).hash( + new SecureString("barbaz longer than 14 chars because of FIPS".toCharArray()) + ) + ) + ); } Settings settings = Settings.builder().setSecureSettings(mockSecureSettings).build(); - final ReservedRealm reservedRealm = new ReservedRealm(mock(Environment.class), settings, usersStore, - new AnonymousUser(Settings.EMPTY), threadPool); + final ReservedRealm reservedRealm = new ReservedRealm( + mock(Environment.class), + settings, + usersStore, + new AnonymousUser(Settings.EMPTY), + threadPool + ); PlainActionFuture> listener = new PlainActionFuture<>(); SecureString password = new SecureString("password".toCharArray()); // Mocked users store is initiated with default hashing algorithm @@ -574,12 +710,19 @@ public void testBootstrapElasticPasswordFailsOnceElasticUserExists() throws Exce callback.onResponse(userInfo); return null; }).when(usersStore).getReservedUserInfo(eq("elastic"), anyActionListener()); - reservedRealm.doAuthenticate(new UsernamePasswordToken(new ElasticUser(true).principal(), - mockSecureSettings.getString("bootstrap.password")), listener); + reservedRealm.doAuthenticate( + new UsernamePasswordToken(new ElasticUser(true).principal(), mockSecureSettings.getString("bootstrap.password")), + listener + ); assertFailedAuthentication(listener, "elastic"); listener = new PlainActionFuture<>(); - reservedRealm.doAuthenticate(new UsernamePasswordToken(new ElasticUser(true).principal(), - new SecureString("barbaz longer than 14 chars because of FIPS".toCharArray())), listener); + reservedRealm.doAuthenticate( + new UsernamePasswordToken( + new ElasticUser(true).principal(), + new SecureString("barbaz longer than 14 chars because of FIPS".toCharArray()) + ), + listener + ); assertFailedAuthentication(listener, "elastic"); // now try with the real password listener = new PlainActionFuture<>(); @@ -590,26 +733,46 @@ public void testBootstrapElasticPasswordFailsOnceElasticUserExists() throws Exce public void testAutoconfigPasswordHashFailsOnceElasticUserExists() throws Exception { MockSecureSettings mockSecureSettings = new MockSecureSettings(); - mockSecureSettings.setString("autoconfiguration.password_hash", - new String(randomFrom(Hasher.BCRYPT, Hasher.PBKDF2).hash( - new SecureString("auto_password longer than 14 chars because of FIPS".toCharArray())))); + mockSecureSettings.setString( + "autoconfiguration.password_hash", + new String( + randomFrom(Hasher.BCRYPT, Hasher.PBKDF2).hash( + new SecureString("auto_password longer than 14 chars because of FIPS".toCharArray()) + ) + ) + ); Settings settings = Settings.builder().setSecureSettings(mockSecureSettings).build(); - final ReservedRealm reservedRealm = new ReservedRealm(mock(Environment.class), settings, usersStore, - new AnonymousUser(Settings.EMPTY), threadPool); + final ReservedRealm reservedRealm = new ReservedRealm( + mock(Environment.class), + settings, + usersStore, + new AnonymousUser(Settings.EMPTY), + threadPool + ); PlainActionFuture> listener = new PlainActionFuture<>(); // Mocked users store is initiated with default hashing algorithm final Hasher hasher = Hasher.resolve("bcrypt"); doAnswer(getAnswer(true, new SecureString("password longer than 14 chars because of FIPS".toCharArray()), hasher)).when(usersStore) .getReservedUserInfo(eq("elastic"), anyActionListener()); - reservedRealm.doAuthenticate(new UsernamePasswordToken(new ElasticUser(true).principal(), - new SecureString("password longer than 14 chars because of FIPS".toCharArray())), listener); + reservedRealm.doAuthenticate( + new UsernamePasswordToken( + new ElasticUser(true).principal(), + new SecureString("password longer than 14 chars because of FIPS".toCharArray()) + ), + listener + ); final AuthenticationResult result = listener.get(); assertThat(result.getStatus(), is(AuthenticationResult.Status.SUCCESS)); // but auto config password does not work listener = new PlainActionFuture<>(); - reservedRealm.doAuthenticate(new UsernamePasswordToken(new ElasticUser(true).principal(), - new SecureString("auto_password longer than 14 chars because of FIPS".toCharArray())), listener); + reservedRealm.doAuthenticate( + new UsernamePasswordToken( + new ElasticUser(true).principal(), + new SecureString("auto_password longer than 14 chars because of FIPS".toCharArray()) + ), + listener + ); assertFailedAuthentication(listener, "elastic"); verify(usersStore, times(2)).getReservedUserInfo(eq("elastic"), anyActionListener()); verify(usersStore, times(0)).createElasticUser(any(char[].class), anyActionListener()); @@ -620,18 +783,34 @@ public void testNonElasticUsersCannotUseBootstrapPassword() throws Exception { final String password = randomAlphaOfLengthBetween(15, 24); mockSecureSettings.setString("bootstrap.password", password); if (randomBoolean()) { - mockSecureSettings.setString("autoconfiguration.password_hash", - new String(randomFrom(Hasher.BCRYPT, Hasher.PBKDF2).hash( - new SecureString("barbaz longer than 14 chars because of FIPS".toCharArray())))); + mockSecureSettings.setString( + "autoconfiguration.password_hash", + new String( + randomFrom(Hasher.BCRYPT, Hasher.PBKDF2).hash( + new SecureString("barbaz longer than 14 chars because of FIPS".toCharArray()) + ) + ) + ); } Settings settings = Settings.builder().setSecureSettings(mockSecureSettings).build(); - final ReservedRealm reservedRealm = new ReservedRealm(mock(Environment.class), settings, usersStore, - new AnonymousUser(Settings.EMPTY), threadPool); + final ReservedRealm reservedRealm = new ReservedRealm( + mock(Environment.class), + settings, + usersStore, + new AnonymousUser(Settings.EMPTY), + threadPool + ); PlainActionFuture> listener = new PlainActionFuture<>(); - final String principal = randomFrom(KibanaUser.NAME, KibanaSystemUser.NAME, LogstashSystemUser.NAME, BeatsSystemUser.NAME, - APMSystemUser.NAME, RemoteMonitoringUser.NAME); + final String principal = randomFrom( + KibanaUser.NAME, + KibanaSystemUser.NAME, + LogstashSystemUser.NAME, + BeatsSystemUser.NAME, + APMSystemUser.NAME, + RemoteMonitoringUser.NAME + ); doAnswer((i) -> { ActionListener callback = (ActionListener) i.getArguments()[1]; callback.onResponse(null); @@ -645,16 +824,29 @@ public void testNonElasticUsersCannotUseBootstrapPassword() throws Exception { public void testNonElasticUsersCannotUseAutoconfigPasswordHash() throws Exception { final MockSecureSettings mockSecureSettings = new MockSecureSettings(); final String password = randomAlphaOfLengthBetween(15, 24); - mockSecureSettings.setString("autoconfiguration.password_hash", - new String(randomFrom(Hasher.BCRYPT, Hasher.PBKDF2).hash(new SecureString(password.toCharArray())))); + mockSecureSettings.setString( + "autoconfiguration.password_hash", + new String(randomFrom(Hasher.BCRYPT, Hasher.PBKDF2).hash(new SecureString(password.toCharArray()))) + ); Settings settings = Settings.builder().setSecureSettings(mockSecureSettings).build(); - final ReservedRealm reservedRealm = new ReservedRealm(mock(Environment.class), settings, usersStore, - new AnonymousUser(Settings.EMPTY), threadPool); + final ReservedRealm reservedRealm = new ReservedRealm( + mock(Environment.class), + settings, + usersStore, + new AnonymousUser(Settings.EMPTY), + threadPool + ); PlainActionFuture> listener = new PlainActionFuture<>(); - final String principal = randomFrom(KibanaUser.NAME, KibanaSystemUser.NAME, LogstashSystemUser.NAME, BeatsSystemUser.NAME, - APMSystemUser.NAME, RemoteMonitoringUser.NAME); + final String principal = randomFrom( + KibanaUser.NAME, + KibanaSystemUser.NAME, + LogstashSystemUser.NAME, + BeatsSystemUser.NAME, + APMSystemUser.NAME, + RemoteMonitoringUser.NAME + ); doAnswer((i) -> { ActionListener callback = (ActionListener) i.getArguments()[1]; callback.onResponse(null); @@ -665,8 +857,15 @@ public void testNonElasticUsersCannotUseAutoconfigPasswordHash() throws Exceptio } private User randomReservedUser(boolean enabled) { - return randomFrom(new ElasticUser(enabled), new KibanaUser(enabled), new KibanaSystemUser(enabled), - new LogstashSystemUser(enabled), new BeatsSystemUser(enabled), new APMSystemUser(enabled), new RemoteMonitoringUser(enabled)); + return randomFrom( + new ElasticUser(enabled), + new KibanaUser(enabled), + new KibanaSystemUser(enabled), + new LogstashSystemUser(enabled), + new BeatsSystemUser(enabled), + new APMSystemUser(enabled), + new RemoteMonitoringUser(enabled) + ); } /* diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/esnative/tool/CommandLineHttpClientTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/esnative/tool/CommandLineHttpClientTests.java index 1725c89652fee..f5e773b2e8e21 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/esnative/tool/CommandLineHttpClientTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/esnative/tool/CommandLineHttpClientTests.java @@ -15,12 +15,12 @@ import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.http.MockResponse; import org.elasticsearch.test.http.MockWebServer; -import org.elasticsearch.xpack.core.ssl.CertParsingUtils; -import org.elasticsearch.xpack.core.ssl.SSLConfigurationSettingsTests; -import org.elasticsearch.xpack.core.ssl.TestsSSLService; import org.elasticsearch.xpack.core.security.CommandLineHttpClient; import org.elasticsearch.xpack.core.security.HttpResponse; import org.elasticsearch.xpack.core.security.HttpResponse.HttpResponseBuilder; +import org.elasticsearch.xpack.core.ssl.CertParsingUtils; +import org.elasticsearch.xpack.core.ssl.SSLConfigurationSettingsTests; +import org.elasticsearch.xpack.core.ssl.TestsSSLService; import org.junit.After; import org.junit.Before; @@ -62,13 +62,18 @@ public void shutdown() { } public void testCommandLineHttpClientCanExecuteAndReturnCorrectResultUsingSSLSettings() throws Exception { - Settings settings = getHttpSslSettings() - .put("xpack.security.http.ssl.certificate_authorities", caCertPath.toString()) + Settings settings = getHttpSslSettings().put("xpack.security.http.ssl.certificate_authorities", caCertPath.toString()) .put("xpack.security.http.ssl.verification_mode", SslVerificationMode.CERTIFICATE) .build(); CommandLineHttpClient client = new CommandLineHttpClient(TestEnvironment.newEnvironment(settings)); - HttpResponse httpResponse = client.execute("GET", new URL("https://localhost:" + webServer.getPort() + "/test"), "u1", - new SecureString(new char[]{'p'}), () -> null, is -> responseBuilder(is)); + HttpResponse httpResponse = client.execute( + "GET", + new URL("https://localhost:" + webServer.getPort() + "/test"), + "u1", + new SecureString(new char[] { 'p' }), + () -> null, + is -> responseBuilder(is) + ); assertNotNull("Should have http response", httpResponse); assertEquals("Http status code does not match", 200, httpResponse.getHttpStatus()); @@ -81,8 +86,14 @@ public void testCommandLineClientCanTrustPinnedCaCertificateFingerprint() throws (TestEnvironment.newEnvironment(Settings.builder().put("path.home", createTempDir()).build())), SslUtil.calculateFingerprint(caCert, "SHA-256") ); - HttpResponse httpResponse = client.execute("GET", new URL("https://localhost:" + webServer.getPort() + "/test"), "u1", - new SecureString(new char[]{'p'}), () -> null, is -> responseBuilder(is)); + HttpResponse httpResponse = client.execute( + "GET", + new URL("https://localhost:" + webServer.getPort() + "/test"), + "u1", + new SecureString(new char[] { 'p' }), + () -> null, + is -> responseBuilder(is) + ); assertNotNull("Should have http response", httpResponse); assertEquals("Http status code does not match", 200, httpResponse.getHttpStatus()); @@ -90,13 +101,12 @@ public void testCommandLineClientCanTrustPinnedCaCertificateFingerprint() throws } public void testGetDefaultURLFailsWithHelpfulMessage() { - Settings settings = Settings.builder() - .put("path.home", createTempDir()) - .put("network.host", "_ec2:privateIpv4_") - .build(); + Settings settings = Settings.builder().put("path.home", createTempDir()).put("network.host", "_ec2:privateIpv4_").build(); CommandLineHttpClient client = new CommandLineHttpClient(TestEnvironment.newEnvironment(settings)); - assertThat(expectThrows(IllegalStateException.class, () -> client.getDefaultURL()).getMessage(), - containsString("unable to determine default URL from settings, please use the -u option to explicitly provide the url")); + assertThat( + expectThrows(IllegalStateException.class, () -> client.getDefaultURL()).getMessage(), + containsString("unable to determine default URL from settings, please use the -u option to explicitly provide the url") + ); } private MockWebServer createMockWebServer() { diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/esnative/tool/SetupPasswordToolTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/esnative/tool/SetupPasswordToolTests.java index 9f19d43daf605..1393fa0bed271 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/esnative/tool/SetupPasswordToolTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/esnative/tool/SetupPasswordToolTests.java @@ -17,22 +17,22 @@ import org.elasticsearch.common.settings.KeyStoreWrapper; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.env.Environment; import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.protocol.xpack.XPackInfoResponse; import org.elasticsearch.protocol.xpack.XPackInfoResponse.FeatureSetsInfo; import org.elasticsearch.protocol.xpack.XPackInfoResponse.FeatureSetsInfo.FeatureSet; import org.elasticsearch.rest.RestStatus; -import org.elasticsearch.xpack.core.security.support.Validation; -import org.elasticsearch.xpack.core.security.user.ElasticUser; -import org.elasticsearch.xpack.security.authc.esnative.ReservedRealm; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.security.CommandLineHttpClient; import org.elasticsearch.xpack.core.security.HttpResponse; import org.elasticsearch.xpack.core.security.HttpResponse.HttpResponseBuilder; +import org.elasticsearch.xpack.core.security.support.Validation; +import org.elasticsearch.xpack.core.security.user.ElasticUser; +import org.elasticsearch.xpack.security.authc.esnative.ReservedRealm; import org.hamcrest.Matchers; import org.junit.Before; import org.junit.Rule; @@ -54,6 +54,7 @@ import java.util.List; import java.util.Map; import java.util.Set; + import javax.net.ssl.SSLException; import static org.elasticsearch.test.CheckedFunctionUtils.anyCheckedFunction; @@ -85,8 +86,9 @@ public class SetupPasswordToolTests extends CommandTestCase { public void setSecretsAndKeyStore() throws Exception { // sometimes we fall back to the keystore seed as this is the default when a new node starts boolean useFallback = randomBoolean(); - bootstrapPassword = useFallback ? new SecureString("0xCAFEBABE".toCharArray()) : - new SecureString("bootstrap-password".toCharArray()); + bootstrapPassword = useFallback + ? new SecureString("0xCAFEBABE".toCharArray()) + : new SecureString("bootstrap-password".toCharArray()); keyStore = mockKeystore(false, useFallback); // create a password protected keystore eitherway, so that it can be used for SetupPasswordToolTests#testWrongKeystorePassword passwordProtectedKeystore = mockKeystore(true, useFallback); @@ -99,18 +101,42 @@ public void setSecretsAndKeyStore() throws Exception { when(httpClient.getDefaultURL()).thenReturn("http://localhost:9200"); HttpResponse httpResponse = new HttpResponse(HttpURLConnection.HTTP_OK, new HashMap<>()); - when(httpClient.execute(anyString(), any(URL.class), anyString(), any(SecureString.class), anyCheckedSupplier(), - anyCheckedFunction())).thenReturn(httpResponse); + when( + httpClient.execute( + anyString(), + any(URL.class), + anyString(), + any(SecureString.class), + anyCheckedSupplier(), + anyCheckedFunction() + ) + ).thenReturn(httpResponse); URL url = new URL(httpClient.getDefaultURL()); httpResponse = new HttpResponse(HttpURLConnection.HTTP_OK, Collections.singletonMap("status", randomFrom("yellow", "green"))); - when(httpClient.execute(anyString(), eq(clusterHealthUrl(url)), anyString(), any(SecureString.class), anyCheckedSupplier(), - anyCheckedFunction())).thenReturn(httpResponse); + when( + httpClient.execute( + anyString(), + eq(clusterHealthUrl(url)), + anyString(), + any(SecureString.class), + anyCheckedSupplier(), + anyCheckedFunction() + ) + ).thenReturn(httpResponse); URL xpackSecurityPluginQueryURL = queryXPackSecurityFeatureConfigURL(url); HttpResponse queryXPackSecurityConfigHttpResponse = new HttpResponse(HttpURLConnection.HTTP_OK, new HashMap<>()); - when(httpClient.execute(eq("GET"), eq(xpackSecurityPluginQueryURL), anyString(), any(SecureString.class), - anyCheckedSupplier(), anyCheckedFunction())).thenReturn(queryXPackSecurityConfigHttpResponse); + when( + httpClient.execute( + eq("GET"), + eq(xpackSecurityPluginQueryURL), + anyString(), + any(SecureString.class), + anyCheckedSupplier(), + anyCheckedFunction() + ) + ).thenReturn(queryXPackSecurityConfigHttpResponse); // elastic user is updated last usersInSetOrder = new ArrayList<>(SetupPasswordTool.USERS); @@ -145,8 +171,9 @@ private KeyStoreWrapper mockKeystore(boolean isPasswordProtected, boolean useFal KeyStoreWrapper keyStore = mock(KeyStoreWrapper.class); when(keyStore.isLoaded()).thenReturn(true); if (useFallback) { - when(keyStore.getSettingNames()).thenReturn(new HashSet<>(Arrays.asList(ReservedRealm.BOOTSTRAP_ELASTIC_PASSWORD.getKey(), - KeyStoreWrapper.SEED_SETTING.getKey()))); + when(keyStore.getSettingNames()).thenReturn( + new HashSet<>(Arrays.asList(ReservedRealm.BOOTSTRAP_ELASTIC_PASSWORD.getKey(), KeyStoreWrapper.SEED_SETTING.getKey())) + ); when(keyStore.getString(ReservedRealm.BOOTSTRAP_ELASTIC_PASSWORD.getKey())).thenReturn(bootstrapPassword); } else { when(keyStore.getSettingNames()).thenReturn(Collections.singleton(KeyStoreWrapper.SEED_SETTING.getKey())); @@ -155,8 +182,8 @@ private KeyStoreWrapper mockKeystore(boolean isPasswordProtected, boolean useFal if (isPasswordProtected) { when(keyStore.hasPassword()).thenReturn(true); doNothing().when(keyStore).decrypt("keystore-password".toCharArray()); - doThrow(new SecurityException("Provided keystore password was incorrect", new IOException())) - .when(keyStore).decrypt("wrong-password".toCharArray()); + doThrow(new SecurityException("Provided keystore password was incorrect", new IOException())).when(keyStore) + .decrypt("wrong-password".toCharArray()); } return keyStore; } @@ -184,29 +211,36 @@ public void testAutoSetup() throws Exception { InOrder inOrder = Mockito.inOrder(httpClient); URL checkUrl = authenticateUrl(url); - inOrder.verify(httpClient).execute(eq("GET"), eq(checkUrl), eq(ElasticUser.NAME), eq(bootstrapPassword), anyCheckedSupplier(), - anyCheckedFunction()); + inOrder.verify(httpClient) + .execute(eq("GET"), eq(checkUrl), eq(ElasticUser.NAME), eq(bootstrapPassword), anyCheckedSupplier(), anyCheckedFunction()); Map capturedPasswords = new HashMap<>(usersInSetOrder.size()); for (String user : usersInSetOrder) { URL urlWithRoute = passwordUrl(url, user); - @SuppressWarnings({"unchecked", "rawtypes"}) + @SuppressWarnings({ "unchecked", "rawtypes" }) ArgumentCaptor> passwordCaptor = ArgumentCaptor.forClass((Class) CheckedSupplier.class); - inOrder.verify(httpClient).execute(eq("PUT"), eq(urlWithRoute), eq(ElasticUser.NAME), eq(bootstrapPassword), - passwordCaptor.capture(), anyCheckedFunction()); + inOrder.verify(httpClient) + .execute( + eq("PUT"), + eq(urlWithRoute), + eq(ElasticUser.NAME), + eq(bootstrapPassword), + passwordCaptor.capture(), + anyCheckedFunction() + ); String userPassword = passwordCaptor.getValue().get(); capturedPasswords.put(user, userPassword); } - for (Map.Entry entry : SetupPasswordTool.USERS_WITH_SHARED_PASSWORDS.entrySet()) { - assertEquals(capturedPasswords.get(entry.getKey()), capturedPasswords.get(entry.getValue())); + for (Map.Entry entry : SetupPasswordTool.USERS_WITH_SHARED_PASSWORDS.entrySet()) { + assertEquals(capturedPasswords.get(entry.getKey()), capturedPasswords.get(entry.getValue())); - capturedPasswords.remove(entry.getKey()); - capturedPasswords.remove(entry.getValue()); - } + capturedPasswords.remove(entry.getKey()); + capturedPasswords.remove(entry.getValue()); + } Set uniqueCapturedPasswords = new HashSet<>(capturedPasswords.values()); - assertEquals(uniqueCapturedPasswords.size(), capturedPasswords.size()); + assertEquals(uniqueCapturedPasswords.size(), capturedPasswords.size()); } public void testAuthnFail() throws Exception { @@ -215,8 +249,16 @@ public void testAuthnFail() throws Exception { HttpResponse httpResponse = new HttpResponse(HttpURLConnection.HTTP_UNAUTHORIZED, new HashMap<>()); - when(httpClient.execute(eq("GET"), eq(authnURL), eq(ElasticUser.NAME), any(SecureString.class), anyCheckedSupplier(), - anyCheckedFunction())).thenReturn(httpResponse); + when( + httpClient.execute( + eq("GET"), + eq(authnURL), + eq(ElasticUser.NAME), + any(SecureString.class), + anyCheckedSupplier(), + anyCheckedFunction() + ) + ).thenReturn(httpResponse); try { execute(randomBoolean() ? "auto" : "interactive", pathHomeParameter); @@ -231,13 +273,22 @@ public void testErrorMessagesWhenXPackIsNotAvailableOnNode() throws Exception { URL authnURL = authenticateUrl(url); HttpResponse httpResponse = new HttpResponse(HttpURLConnection.HTTP_NOT_FOUND, new HashMap<>()); - when(httpClient.execute(eq("GET"), eq(authnURL), eq(ElasticUser.NAME), any(SecureString.class), anyCheckedSupplier(), - anyCheckedFunction())).thenReturn(httpResponse); + when( + httpClient.execute( + eq("GET"), + eq(authnURL), + eq(ElasticUser.NAME), + any(SecureString.class), + anyCheckedSupplier(), + anyCheckedFunction() + ) + ).thenReturn(httpResponse); URL xpackSecurityPluginQueryURL = queryXPackSecurityFeatureConfigURL(url); String securityPluginQueryResponseBody = null; - final IllegalArgumentException illegalArgException = - new IllegalArgumentException("request [/_xpack] contains unrecognized parameter: [categories]"); + final IllegalArgumentException illegalArgException = new IllegalArgumentException( + "request [/_xpack] contains unrecognized parameter: [categories]" + ); try (XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON)) { builder.startObject(); ElasticsearchException.generateFailureXContent(builder, ToXContent.EMPTY_PARAMS, illegalArgException, true); @@ -245,9 +296,16 @@ public void testErrorMessagesWhenXPackIsNotAvailableOnNode() throws Exception { builder.endObject(); securityPluginQueryResponseBody = Strings.toString(builder); } - when(httpClient.execute(eq("GET"), eq(xpackSecurityPluginQueryURL), eq(ElasticUser.NAME), any(SecureString.class), - anyCheckedSupplier(), anyCheckedFunction())) - .thenReturn(createHttpResponse(HttpURLConnection.HTTP_BAD_REQUEST, securityPluginQueryResponseBody)); + when( + httpClient.execute( + eq("GET"), + eq(xpackSecurityPluginQueryURL), + eq(ElasticUser.NAME), + any(SecureString.class), + anyCheckedSupplier(), + anyCheckedFunction() + ) + ).thenReturn(createHttpResponse(HttpURLConnection.HTTP_BAD_REQUEST, securityPluginQueryResponseBody)); thrown.expect(UserException.class); thrown.expectMessage("X-Pack is not available on this Elasticsearch node."); @@ -259,8 +317,16 @@ public void testErrorMessagesWhenXPackIsAvailableWithCorrectLicenseAndIsEnabledB URL authnURL = authenticateUrl(url); HttpResponse httpResponse = new HttpResponse(HttpURLConnection.HTTP_NOT_FOUND, new HashMap<>()); - when(httpClient.execute(eq("GET"), eq(authnURL), eq(ElasticUser.NAME), any(SecureString.class), anyCheckedSupplier(), - anyCheckedFunction())).thenReturn(httpResponse); + when( + httpClient.execute( + eq("GET"), + eq(authnURL), + eq(ElasticUser.NAME), + any(SecureString.class), + anyCheckedSupplier(), + anyCheckedFunction() + ) + ).thenReturn(httpResponse); URL xpackSecurityPluginQueryURL = queryXPackSecurityFeatureConfigURL(url); @@ -276,9 +342,16 @@ public void testErrorMessagesWhenXPackIsAvailableWithCorrectLicenseAndIsEnabledB builder.endObject(); securityPluginQueryResponseBody = Strings.toString(builder); } - when(httpClient.execute(eq("GET"), eq(xpackSecurityPluginQueryURL), eq(ElasticUser.NAME), any(SecureString.class), - anyCheckedSupplier(), anyCheckedFunction())) - .thenReturn(createHttpResponse(HttpURLConnection.HTTP_OK, securityPluginQueryResponseBody)); + when( + httpClient.execute( + eq("GET"), + eq(xpackSecurityPluginQueryURL), + eq(ElasticUser.NAME), + any(SecureString.class), + anyCheckedSupplier(), + anyCheckedFunction() + ) + ).thenReturn(createHttpResponse(HttpURLConnection.HTTP_OK, securityPluginQueryResponseBody)); thrown.expect(UserException.class); thrown.expectMessage("Unknown error"); @@ -292,8 +365,16 @@ public void testErrorMessagesWhenXPackPluginIsAvailableButNoSecurityLicense() th URL xpackSecurityPluginQueryURL = queryXPackSecurityFeatureConfigURL(url); HttpResponse httpResponse = new HttpResponse(HttpURLConnection.HTTP_NOT_FOUND, new HashMap<>()); - when(httpClient.execute(eq("GET"), eq(authnURL), eq(ElasticUser.NAME), any(SecureString.class), anyCheckedSupplier(), - anyCheckedFunction())).thenReturn(httpResponse); + when( + httpClient.execute( + eq("GET"), + eq(authnURL), + eq(ElasticUser.NAME), + any(SecureString.class), + anyCheckedSupplier(), + anyCheckedFunction() + ) + ).thenReturn(httpResponse); Set featureSets = new HashSet<>(); featureSets.add(new FeatureSet("logstash", true, true)); @@ -307,9 +388,16 @@ public void testErrorMessagesWhenXPackPluginIsAvailableButNoSecurityLicense() th builder.endObject(); securityPluginQueryResponseBody = Strings.toString(builder); } - when(httpClient.execute(eq("GET"), eq(xpackSecurityPluginQueryURL), eq(ElasticUser.NAME), any(SecureString.class), - anyCheckedSupplier(), anyCheckedFunction())) - .thenReturn(createHttpResponse(HttpURLConnection.HTTP_OK, securityPluginQueryResponseBody)); + when( + httpClient.execute( + eq("GET"), + eq(xpackSecurityPluginQueryURL), + eq(ElasticUser.NAME), + any(SecureString.class), + anyCheckedSupplier(), + anyCheckedFunction() + ) + ).thenReturn(createHttpResponse(HttpURLConnection.HTTP_OK, securityPluginQueryResponseBody)); thrown.expect(UserException.class); thrown.expectMessage("X-Pack Security is not available."); @@ -323,8 +411,16 @@ public void testErrorMessagesWhenXPackPluginIsAvailableWithValidLicenseButDisabl URL xpackSecurityPluginQueryURL = queryXPackSecurityFeatureConfigURL(url); HttpResponse httpResponse = new HttpResponse(HttpURLConnection.HTTP_NOT_FOUND, new HashMap<>()); - when(httpClient.execute(eq("GET"), eq(authnURL), eq(ElasticUser.NAME), any(SecureString.class), anyCheckedSupplier(), - anyCheckedFunction())).thenReturn(httpResponse); + when( + httpClient.execute( + eq("GET"), + eq(authnURL), + eq(ElasticUser.NAME), + any(SecureString.class), + anyCheckedSupplier(), + anyCheckedFunction() + ) + ).thenReturn(httpResponse); Set featureSets = new HashSet<>(); featureSets.add(new FeatureSet("logstash", true, true)); @@ -338,9 +434,16 @@ public void testErrorMessagesWhenXPackPluginIsAvailableWithValidLicenseButDisabl builder.endObject(); securityPluginQueryResponseBody = Strings.toString(builder); } - when(httpClient.execute(eq("GET"), eq(xpackSecurityPluginQueryURL), eq(ElasticUser.NAME), any(SecureString.class), - anyCheckedSupplier(), anyCheckedFunction())) - .thenReturn(createHttpResponse(HttpURLConnection.HTTP_OK, securityPluginQueryResponseBody)); + when( + httpClient.execute( + eq("GET"), + eq(xpackSecurityPluginQueryURL), + eq(ElasticUser.NAME), + any(SecureString.class), + anyCheckedSupplier(), + anyCheckedFunction() + ) + ).thenReturn(createHttpResponse(HttpURLConnection.HTTP_OK, securityPluginQueryResponseBody)); thrown.expect(UserException.class); thrown.expectMessage("X-Pack Security is disabled by configuration."); @@ -350,8 +453,8 @@ public void testErrorMessagesWhenXPackPluginIsAvailableWithValidLicenseButDisabl public void testWrongServer() throws Exception { URL url = new URL(httpClient.getDefaultURL()); URL authnURL = authenticateUrl(url); - doThrow(randomFrom(new IOException(), new SSLException(""))).when(httpClient).execute(eq("GET"), eq(authnURL), eq(ElasticUser.NAME), - any(SecureString.class), anyCheckedSupplier(), anyCheckedFunction()); + doThrow(randomFrom(new IOException(), new SSLException(""))).when(httpClient) + .execute(eq("GET"), eq(authnURL), eq(ElasticUser.NAME), any(SecureString.class), anyCheckedSupplier(), anyCheckedFunction()); try { execute(randomBoolean() ? "auto" : "interactive", pathHomeParameter); @@ -365,13 +468,35 @@ public void testRedCluster() throws Exception { URL url = new URL(httpClient.getDefaultURL()); HttpResponse httpResponse = new HttpResponse(HttpURLConnection.HTTP_OK, new HashMap<>()); - when(httpClient.execute(eq("GET"), eq(authenticateUrl(url)), eq(ElasticUser.NAME), any(SecureString.class), - anyCheckedSupplier(), anyCheckedFunction())).thenReturn(httpResponse); - - httpResponse = new HttpResponse(HttpURLConnection.HTTP_OK, MapBuilder.newMapBuilder() - .put("cluster_name", "elasticsearch").put("status", "red").put("number_of_nodes", 1).map()); - when(httpClient.execute(eq("GET"), eq(clusterHealthUrl(url)), eq(ElasticUser.NAME), any(SecureString.class), - anyCheckedSupplier(), anyCheckedFunction())).thenReturn(httpResponse); + when( + httpClient.execute( + eq("GET"), + eq(authenticateUrl(url)), + eq(ElasticUser.NAME), + any(SecureString.class), + anyCheckedSupplier(), + anyCheckedFunction() + ) + ).thenReturn(httpResponse); + + httpResponse = new HttpResponse( + HttpURLConnection.HTTP_OK, + MapBuilder.newMapBuilder() + .put("cluster_name", "elasticsearch") + .put("status", "red") + .put("number_of_nodes", 1) + .map() + ); + when( + httpClient.execute( + eq("GET"), + eq(clusterHealthUrl(url)), + eq(ElasticUser.NAME), + any(SecureString.class), + anyCheckedSupplier(), + anyCheckedFunction() + ) + ).thenReturn(httpResponse); terminal.addTextInput("n"); try { @@ -390,12 +515,19 @@ public void testUrlOption() throws Exception { InOrder inOrder = Mockito.inOrder(httpClient); URL checkUrl = authenticateUrl(url); - inOrder.verify(httpClient).execute(eq("GET"), eq(checkUrl), eq(ElasticUser.NAME), eq(bootstrapPassword), anyCheckedSupplier(), - anyCheckedFunction()); + inOrder.verify(httpClient) + .execute(eq("GET"), eq(checkUrl), eq(ElasticUser.NAME), eq(bootstrapPassword), anyCheckedSupplier(), anyCheckedFunction()); for (String user : usersInSetOrder) { URL urlWithRoute = passwordUrl(url, user); - inOrder.verify(httpClient).execute(eq("PUT"), eq(urlWithRoute), eq(ElasticUser.NAME), eq(bootstrapPassword), - anyCheckedSupplier(), anyCheckedFunction()); + inOrder.verify(httpClient) + .execute( + eq("PUT"), + eq(urlWithRoute), + eq(ElasticUser.NAME), + eq(bootstrapPassword), + anyCheckedSupplier(), + anyCheckedFunction() + ); } } @@ -404,8 +536,8 @@ public void testSetUserPassFail() throws Exception { String userToFail = randomFrom(SetupPasswordTool.USERS); URL userToFailURL = passwordUrl(url, userToFail); - doThrow(new IOException()).when(httpClient).execute(eq("PUT"), eq(userToFailURL), anyString(), any(SecureString.class), - anyCheckedSupplier(), anyCheckedFunction()); + doThrow(new IOException()).when(httpClient) + .execute(eq("PUT"), eq(userToFailURL), anyString(), any(SecureString.class), anyCheckedSupplier(), anyCheckedFunction()); try { execute(randomBoolean() ? "auto" : "interactive", pathHomeParameter, "-b"); fail("Should have thrown exception"); @@ -423,15 +555,22 @@ public void testInteractiveSetup() throws Exception { InOrder inOrder = Mockito.inOrder(httpClient); URL checkUrl = authenticateUrl(url); - inOrder.verify(httpClient).execute(eq("GET"), eq(checkUrl), eq(ElasticUser.NAME), eq(bootstrapPassword), anyCheckedSupplier(), - anyCheckedFunction()); + inOrder.verify(httpClient) + .execute(eq("GET"), eq(checkUrl), eq(ElasticUser.NAME), eq(bootstrapPassword), anyCheckedSupplier(), anyCheckedFunction()); for (String user : usersInSetOrder) { URL urlWithRoute = passwordUrl(url, user); @SuppressWarnings("unchecked") ArgumentCaptor> passwordCaptor = ArgumentCaptor.forClass((Class) CheckedSupplier.class); - inOrder.verify(httpClient).execute(eq("PUT"), eq(urlWithRoute), eq(ElasticUser.NAME), eq(bootstrapPassword), - passwordCaptor.capture(), anyCheckedFunction()); + inOrder.verify(httpClient) + .execute( + eq("PUT"), + eq(urlWithRoute), + eq(ElasticUser.NAME), + eq(bootstrapPassword), + passwordCaptor.capture(), + anyCheckedFunction() + ); assertThat(passwordCaptor.getValue().get(), containsString(getExpectedPasswordForUser(user))); } } @@ -470,14 +609,21 @@ public void testInteractivePasswordsFatFingers() throws Exception { InOrder inOrder = Mockito.inOrder(httpClient); URL checkUrl = authenticateUrl(url); - inOrder.verify(httpClient).execute(eq("GET"), eq(checkUrl), eq(ElasticUser.NAME), eq(bootstrapPassword), anyCheckedSupplier(), - anyCheckedFunction()); + inOrder.verify(httpClient) + .execute(eq("GET"), eq(checkUrl), eq(ElasticUser.NAME), eq(bootstrapPassword), anyCheckedSupplier(), anyCheckedFunction()); for (String user : usersInSetOrder) { URL urlWithRoute = passwordUrl(url, user); - @SuppressWarnings({"unchecked", "rawtypes"}) + @SuppressWarnings({ "unchecked", "rawtypes" }) ArgumentCaptor> passwordCaptor = ArgumentCaptor.forClass((Class) CheckedSupplier.class); - inOrder.verify(httpClient).execute(eq("PUT"), eq(urlWithRoute), eq(ElasticUser.NAME), eq(bootstrapPassword), - passwordCaptor.capture(), anyCheckedFunction()); + inOrder.verify(httpClient) + .execute( + eq("PUT"), + eq(urlWithRoute), + eq(ElasticUser.NAME), + eq(bootstrapPassword), + passwordCaptor.capture(), + anyCheckedFunction() + ); assertThat(passwordCaptor.getValue().get(), containsString(getExpectedPasswordForUser(user))); } } @@ -510,8 +656,7 @@ private URL clusterHealthUrl(URL url) throws MalformedURLException, URISyntaxExc } private URL queryXPackSecurityFeatureConfigURL(URL url) throws MalformedURLException, URISyntaxException { - return new URL(url, - (url.toURI().getPath() + "/_xpack").replaceAll("/+", "/") + "?categories=features&human=false&pretty"); + return new URL(url, (url.toURI().getPath() + "/_xpack").replaceAll("/+", "/") + "?categories=features&human=false&pretty"); } private HttpResponse createHttpResponse(final int httpStatus, final String responseJson) throws IOException { @@ -554,7 +699,7 @@ protected Environment createEnv(Map settings) throws UserExcepti private String getExpectedPasswordForUser(String user) throws Exception { if (SetupPasswordTool.USERS_WITH_SHARED_PASSWORDS.containsValue(user)) { - for(Map.Entry entry : SetupPasswordTool.USERS_WITH_SHARED_PASSWORDS.entrySet()) { + for (Map.Entry entry : SetupPasswordTool.USERS_WITH_SHARED_PASSWORDS.entrySet()) { if (entry.getValue().equals(user)) { return entry.getKey() + "-password"; } @@ -562,5 +707,5 @@ private String getExpectedPasswordForUser(String user) throws Exception { throw new Exception("Expected to find corresponding user for " + user); } return user + "-password"; - } + } } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/file/FileRealmTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/file/FileRealmTests.java index 590694034376a..c141ce141ce71 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/file/FileRealmTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/file/FileRealmTests.java @@ -66,15 +66,17 @@ public void init() throws Exception { globalSettings = Settings.builder() .put("path.home", createTempDir()) .put("xpack.security.authc.password_hashing.algorithm", getFastStoredHashAlgoForTests().name()) - .put(RealmSettings.realmSettingPrefix(REALM_IDENTIFIER) + "order", 0).build(); + .put(RealmSettings.realmSettingPrefix(REALM_IDENTIFIER) + "order", 0) + .build(); threadPool = mock(ThreadPool.class); threadContext = new ThreadContext(globalSettings); when(threadPool.getThreadContext()).thenReturn(threadContext); } public void testAuthenticate() throws Exception { - when(userPasswdStore.verifyPassword(eq("user1"), eq(new SecureString("longtestpassword")), anySupplier())) - .thenAnswer(VERIFY_PASSWORD_ANSWER); + when(userPasswdStore.verifyPassword(eq("user1"), eq(new SecureString("longtestpassword")), anySupplier())).thenAnswer( + VERIFY_PASSWORD_ANSWER + ); when(userRolesStore.roles("user1")).thenReturn(new String[] { "role1", "role2" }); RealmConfig config = getRealmConfig(globalSettings); FileRealm realm = new FileRealm(config, userPasswdStore, userRolesStore, threadPool); @@ -96,14 +98,14 @@ private RealmConfig getRealmConfig(Settings settings) { public void testAuthenticateCaching() throws Exception { Settings settings = Settings.builder() - .put(RealmSettings.realmSettingPrefix(REALM_IDENTIFIER) + "cache.hash_algo", - randomFrom(Hasher.getAvailableAlgoCacheHash())) + .put(RealmSettings.realmSettingPrefix(REALM_IDENTIFIER) + "cache.hash_algo", randomFrom(Hasher.getAvailableAlgoCacheHash())) .put(globalSettings) .build(); RealmConfig config = getRealmConfig(settings); - when(userPasswdStore.verifyPassword(eq("user1"), eq(new SecureString("longtestpassword")), anySupplier())) - .thenAnswer(VERIFY_PASSWORD_ANSWER); - when(userRolesStore.roles("user1")).thenReturn(new String[]{"role1", "role2"}); + when(userPasswdStore.verifyPassword(eq("user1"), eq(new SecureString("longtestpassword")), anySupplier())).thenAnswer( + VERIFY_PASSWORD_ANSWER + ); + when(userRolesStore.roles("user1")).thenReturn(new String[] { "role1", "role2" }); FileRealm realm = new FileRealm(config, userPasswdStore, userRolesStore, threadPool); PlainActionFuture> future = new PlainActionFuture<>(); realm.authenticate(new UsernamePasswordToken("user1", new SecureString("longtestpassword")), future); @@ -118,8 +120,9 @@ public void testAuthenticateCachingRefresh() throws Exception { RealmConfig config = getRealmConfig(globalSettings); userPasswdStore = spy(new UserPasswdStore(config)); userRolesStore = spy(new UserRolesStore(config)); - when(userPasswdStore.verifyPassword(eq("user1"), eq(new SecureString("longtestpassword")), anySupplier())) - .thenAnswer(VERIFY_PASSWORD_ANSWER); + when(userPasswdStore.verifyPassword(eq("user1"), eq(new SecureString("longtestpassword")), anySupplier())).thenAnswer( + VERIFY_PASSWORD_ANSWER + ); doReturn(new String[] { "role1", "role2" }).when(userRolesStore).roles("user1"); FileRealm realm = new FileRealm(config, userPasswdStore, userRolesStore, threadPool); PlainActionFuture> future = new PlainActionFuture<>(); @@ -155,14 +158,14 @@ public void testAuthenticateCachingRefresh() throws Exception { public void testToken() throws Exception { RealmConfig config = getRealmConfig(globalSettings); - when(userPasswdStore.verifyPassword(eq("user1"), eq(new SecureString("longtestpassword")), anySupplier())) - .thenAnswer(VERIFY_PASSWORD_ANSWER); - when(userRolesStore.roles("user1")).thenReturn(new String[]{"role1", "role2"}); + when(userPasswdStore.verifyPassword(eq("user1"), eq(new SecureString("longtestpassword")), anySupplier())).thenAnswer( + VERIFY_PASSWORD_ANSWER + ); + when(userRolesStore.roles("user1")).thenReturn(new String[] { "role1", "role2" }); FileRealm realm = new FileRealm(config, userPasswdStore, userRolesStore, threadPool); ThreadContext threadContext = new ThreadContext(Settings.EMPTY); - UsernamePasswordToken.putTokenHeader(threadContext, - new UsernamePasswordToken("user1", new SecureString("longtestpassword"))); + UsernamePasswordToken.putTokenHeader(threadContext, new UsernamePasswordToken("user1", new SecureString("longtestpassword"))); UsernamePasswordToken token = realm.token(threadContext); assertThat(token, notNullValue()); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/file/FileUserPasswdStoreTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/file/FileUserPasswdStoreTests.java index c310d9d90f0f1..b84282bd40660 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/file/FileUserPasswdStoreTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/file/FileUserPasswdStoreTests.java @@ -54,8 +54,9 @@ public class FileUserPasswdStoreTests extends ESTestCase { @Before public void init() { - final String hashingAlgorithm = inFipsJvm() ? randomFrom("pbkdf2", "pbkdf2_1000", "pbkdf2_50000", "pbkdf2_stretch") : - randomFrom("bcrypt", "bcrypt11", "pbkdf2", "pbkdf2_1000", "pbkdf2_50000", "pbkdf2_stretch"); + final String hashingAlgorithm = inFipsJvm() + ? randomFrom("pbkdf2", "pbkdf2_1000", "pbkdf2_50000", "pbkdf2_stretch") + : randomFrom("bcrypt", "bcrypt11", "pbkdf2", "pbkdf2_1000", "pbkdf2_50000", "pbkdf2_stretch"); settings = Settings.builder() .put("resource.reload.interval.high", "100ms") .put("path.home", createTempDir()) @@ -97,7 +98,7 @@ public void testStore_AutoReload() throws Exception { final CountDownLatch latch = new CountDownLatch(1); FileUserPasswdStore store = new FileUserPasswdStore(config, watcherService, latch::countDown); - //Test users share the hashing algorithm name for convenience + // Test users share the hashing algorithm name for convenience String username = settings.get("xpack.security.authc.password_hashing.algorithm"); User user = new User(username); assertThat(store.userExists(username), is(true)); @@ -138,9 +139,12 @@ public void testStore_AutoReload() throws Exception { private RealmConfig getRealmConfig() { final RealmConfig.RealmIdentifier identifier = new RealmConfig.RealmIdentifier("file", "file-test"); - return new RealmConfig(identifier, + return new RealmConfig( + identifier, Settings.builder().put(settings).put(RealmSettings.getFullSettingKey(identifier, RealmSettings.ORDER_SETTING), 0).build(), - env, threadPool.getThreadContext()); + env, + threadPool.getThreadContext() + ); } public void testStore_AutoReload_WithParseFailures() throws Exception { @@ -155,7 +159,7 @@ public void testStore_AutoReload_WithParseFailures() throws Exception { final CountDownLatch latch = new CountDownLatch(1); FileUserPasswdStore store = new FileUserPasswdStore(config, watcherService, latch::countDown); - //Test users share the hashing algorithm name for convenience + // Test users share the hashing algorithm name for convenience String username = settings.get("xpack.security.authc.password_hashing.algorithm"); User user = new User(username); final String password = username.startsWith("pbkdf2") ? "longertestpassword" : "test123"; @@ -192,16 +196,24 @@ public void testParseFile() throws Exception { assertThat(users.get("sha"), notNullValue()); assertThat(new String(users.get("sha")), equalTo("{SHA}cojt0Pw//L6ToM8G41aOKFIWh7w=")); assertThat(users.get("pbkdf2"), notNullValue()); - assertThat(new String(users.get("pbkdf2")), - equalTo("{PBKDF2}10000$NB6kwTrIPrwJJTu+KXiPUkW5bMf1oG2BMzDJLA479Bk=$CvCgHb5UkalUiNPicqMDOzIsnh3ppyz3SZOp+Gjv+hc=")); + assertThat( + new String(users.get("pbkdf2")), + equalTo("{PBKDF2}10000$NB6kwTrIPrwJJTu+KXiPUkW5bMf1oG2BMzDJLA479Bk=$CvCgHb5UkalUiNPicqMDOzIsnh3ppyz3SZOp+Gjv+hc=") + ); assertThat(users.get("pbkdf2_1000"), notNullValue()); - assertThat(new String(users.get("pbkdf2_1000")), - equalTo("{PBKDF2}1000$cofpEhehEObS+tNtS8/t9Zpf6UgwqkgkQFct2hhmGWA=$9Qb0S04fkF+Ebz1sGIaB9S6huZAXDihopPc6Z748f3E=")); + assertThat( + new String(users.get("pbkdf2_1000")), + equalTo("{PBKDF2}1000$cofpEhehEObS+tNtS8/t9Zpf6UgwqkgkQFct2hhmGWA=$9Qb0S04fkF+Ebz1sGIaB9S6huZAXDihopPc6Z748f3E=") + ); assertThat(users.get("pbkdf2_50000"), notNullValue()); - assertThat(new String(users.get("pbkdf2_50000")), - equalTo("{PBKDF2}50000$riPhBgfrNIpsN91QmF5mQNCwxHfJm0q2XtGt0x5+PRM=$v2j/DD+aFIRrusEeSDUO+eX3IrBPiG+ysgc9y0RDmhs=")); - assertThat(new String(users.get("pbkdf2_stretch")), - equalTo("{PBKDF2_STRETCH}10000$s1y/xv1T1iJxS9BKQ1FkZpSO19dSs6vsGgOb14d+KkU=$PtdgZoRGCSaim033lz/RcEoyhXQ/3WU4E6hfeKGsGes=")); + assertThat( + new String(users.get("pbkdf2_50000")), + equalTo("{PBKDF2}50000$riPhBgfrNIpsN91QmF5mQNCwxHfJm0q2XtGt0x5+PRM=$v2j/DD+aFIRrusEeSDUO+eX3IrBPiG+ysgc9y0RDmhs=") + ); + assertThat( + new String(users.get("pbkdf2_stretch")), + equalTo("{PBKDF2_STRETCH}10000$s1y/xv1T1iJxS9BKQ1FkZpSO19dSs6vsGgOb14d+KkU=$PtdgZoRGCSaim033lz/RcEoyhXQ/3WU4E6hfeKGsGes=") + ); } public void testParseFile_Empty() throws Exception { diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/file/FileUserRolesStoreTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/file/FileUserRolesStoreTests.java index 395e87f511065..4ad193efab7a1 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/file/FileUserRolesStoreTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/file/FileUserRolesStoreTests.java @@ -55,10 +55,7 @@ public class FileUserRolesStoreTests extends ESTestCase { @Before public void init() { - settings = Settings.builder() - .put("resource.reload.interval.high", "100ms") - .put("path.home", createTempDir()) - .build(); + settings = Settings.builder().put("resource.reload.interval.high", "100ms").put("path.home", createTempDir()).build(); env = TestEnvironment.newEnvironment(settings); threadPool = new TestThreadPool("test"); } @@ -77,9 +74,12 @@ public void testStore_ConfiguredWithUnreadableFile() throws Exception { Files.write(file, lines, StandardCharsets.UTF_16); RealmConfig.RealmIdentifier realmId = new RealmConfig.RealmIdentifier("file", "file-test"); - RealmConfig config = new RealmConfig(realmId, + RealmConfig config = new RealmConfig( + realmId, Settings.builder().put(settings).put(RealmSettings.getFullSettingKey(realmId, RealmSettings.ORDER_SETTING), 0).build(), - env, new ThreadContext(Settings.EMPTY)); + env, + new ThreadContext(Settings.EMPTY) + ); try (ResourceWatcherService watcherService = new ResourceWatcherService(settings, threadPool)) { FileUserRolesStore store = new FileUserRolesStore(config, watcherService); assertThat(store.entriesCount(), is(0)); @@ -92,9 +92,12 @@ public void testStoreAutoReload() throws Exception { Files.copy(users, tmp, StandardCopyOption.REPLACE_EXISTING); final RealmConfig.RealmIdentifier realmId = new RealmConfig.RealmIdentifier("file", "file-test"); - RealmConfig config = new RealmConfig(realmId, + RealmConfig config = new RealmConfig( + realmId, Settings.builder().put(settings).put(RealmSettings.getFullSettingKey(realmId, RealmSettings.ORDER_SETTING), 0).build(), - env, new ThreadContext(Settings.EMPTY)); + env, + new ThreadContext(Settings.EMPTY) + ); try (ResourceWatcherService watcherService = new ResourceWatcherService(settings, threadPool)) { final CountDownLatch latch = new CountDownLatch(1); @@ -139,9 +142,12 @@ public void testStoreAutoReloadWithParseFailure() throws Exception { Files.copy(users, tmp, StandardCopyOption.REPLACE_EXISTING); final RealmConfig.RealmIdentifier realmId = new RealmConfig.RealmIdentifier("file", "file-test"); - RealmConfig config = new RealmConfig(realmId, + RealmConfig config = new RealmConfig( + realmId, Settings.builder().put(settings).put(RealmSettings.getFullSettingKey(realmId, RealmSettings.ORDER_SETTING), 0).build(), - env, new ThreadContext(Settings.EMPTY)); + env, + new ThreadContext(Settings.EMPTY) + ); try (ResourceWatcherService watcherService = new ResourceWatcherService(settings, threadPool)) { final CountDownLatch latch = new CountDownLatch(1); @@ -225,10 +231,10 @@ public void testParseFileEmptyRolesDoesNotCauseNPE() throws Exception { final RealmConfig.RealmIdentifier realmId = new RealmConfig.RealmIdentifier("file", "file-test"); Settings settings = Settings.builder() - .put(XPackSettings.WATCHER_ENABLED.getKey(), "false") - .put("path.home", createTempDir()) - .put(RealmSettings.getFullSettingKey(realmId, RealmSettings.ORDER_SETTING), 0) - .build(); + .put(XPackSettings.WATCHER_ENABLED.getKey(), "false") + .put("path.home", createTempDir()) + .put(RealmSettings.getFullSettingKey(realmId, RealmSettings.ORDER_SETTING), 0) + .build(); Environment env = TestEnvironment.newEnvironment(settings); RealmConfig config = new RealmConfig(realmId, settings, env, new ThreadContext(Settings.EMPTY)); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosAuthenticationTokenTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosAuthenticationTokenTests.java index 9bbb744969798..3b7dc5632a10c 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosAuthenticationTokenTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosAuthenticationTokenTests.java @@ -20,8 +20,8 @@ import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasSize; -import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.instanceOf; +import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; public class KerberosAuthenticationTokenTests extends ESTestCase { @@ -47,46 +47,59 @@ public void testExtractTokenForInvalidNegotiateAuthorizationHeaderShouldReturnNu public void testExtractTokenForNegotiateAuthorizationHeaderWithNoTokenShouldThrowException() throws IOException { final String header = randomFrom(KerberosAuthenticationToken.NEGOTIATE_AUTH_HEADER_PREFIX, "negotiate ", "Negotiate "); - final ElasticsearchSecurityException e = - expectThrows(ElasticsearchSecurityException.class, () -> KerberosAuthenticationToken.extractToken(header)); - assertThat(e.getMessage(), - equalTo("invalid negotiate authentication header value, expected base64 encoded token but value is empty")); + final ElasticsearchSecurityException e = expectThrows( + ElasticsearchSecurityException.class, + () -> KerberosAuthenticationToken.extractToken(header) + ); + assertThat( + e.getMessage(), + equalTo("invalid negotiate authentication header value, expected base64 encoded token but value is empty") + ); assertContainsAuthenticateHeader(e); } public void testExtractTokenForNotBase64EncodedTokenThrowsException() throws IOException { final String notBase64Token = "[B@6499375d"; - final ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, - () -> KerberosAuthenticationToken.extractToken(KerberosAuthenticationToken.NEGOTIATE_AUTH_HEADER_PREFIX + notBase64Token)); - assertThat(e.getMessage(), - equalTo("invalid negotiate authentication header value, could not decode base64 token " + notBase64Token)); + final ElasticsearchSecurityException e = expectThrows( + ElasticsearchSecurityException.class, + () -> KerberosAuthenticationToken.extractToken(KerberosAuthenticationToken.NEGOTIATE_AUTH_HEADER_PREFIX + notBase64Token) + ); + assertThat( + e.getMessage(), + equalTo("invalid negotiate authentication header value, could not decode base64 token " + notBase64Token) + ); assertContainsAuthenticateHeader(e); } public void testKerberoAuthenticationTokenClearCredentials() { byte[] inputBytes = randomByteArrayOfLength(5); final String base64Token = Base64.getEncoder().encodeToString(inputBytes); - final KerberosAuthenticationToken kerbAuthnToken = - KerberosAuthenticationToken.extractToken(KerberosAuthenticationToken.NEGOTIATE_AUTH_HEADER_PREFIX + base64Token); + final KerberosAuthenticationToken kerbAuthnToken = KerberosAuthenticationToken.extractToken( + KerberosAuthenticationToken.NEGOTIATE_AUTH_HEADER_PREFIX + base64Token + ); kerbAuthnToken.clearCredentials(); Arrays.fill(inputBytes, (byte) 0); assertArrayEquals(inputBytes, (byte[]) kerbAuthnToken.credentials()); } public void testEqualsHashCode() { - final KerberosAuthenticationToken kerberosAuthenticationToken = - new KerberosAuthenticationToken("base64EncodedToken".getBytes(StandardCharsets.UTF_8)); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(kerberosAuthenticationToken, (original) -> { - return new KerberosAuthenticationToken((byte[]) original.credentials()); - }); + final KerberosAuthenticationToken kerberosAuthenticationToken = new KerberosAuthenticationToken( + "base64EncodedToken".getBytes(StandardCharsets.UTF_8) + ); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + kerberosAuthenticationToken, + (original) -> { return new KerberosAuthenticationToken((byte[]) original.credentials()); } + ); EqualsHashCodeTestUtils.checkEqualsAndHashCode(kerberosAuthenticationToken, (original) -> { byte[] originalCreds = (byte[]) original.credentials(); return new KerberosAuthenticationToken(Arrays.copyOf(originalCreds, originalCreds.length)); }); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(kerberosAuthenticationToken, (original) -> { - return new KerberosAuthenticationToken((byte[]) original.credentials()); - }, KerberosAuthenticationTokenTests::mutateTestItem); + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + kerberosAuthenticationToken, + (original) -> { return new KerberosAuthenticationToken((byte[]) original.credentials()); }, + KerberosAuthenticationTokenTests::mutateTestItem + ); } private static KerberosAuthenticationToken mutateTestItem(KerberosAuthenticationToken original) { diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosRealmAuthenticateFailedTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosRealmAuthenticateFailedTests.java index dcd0652e0b51e..da8bd2b4a61a2 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosRealmAuthenticateFailedTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosRealmAuthenticateFailedTests.java @@ -29,6 +29,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; + import javax.security.auth.login.LoginException; import static org.elasticsearch.test.ActionListenerUtils.anyActionListener; @@ -47,8 +48,10 @@ public class KerberosRealmAuthenticateFailedTests extends KerberosRealmTestCase public void testAuthenticateWithNonKerberosAuthenticationToken() { final KerberosRealm kerberosRealm = createKerberosRealm(randomAlphaOfLength(5)); - final UsernamePasswordToken usernamePasswordToken = - new UsernamePasswordToken(randomAlphaOfLength(5), new SecureString(new char[] { 'a', 'b', 'c' })); + final UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken( + randomAlphaOfLength(5), + new SecureString(new char[] { 'a', 'b', 'c' }) + ); expectThrows(AssertionError.class, () -> kerberosRealm.authenticate(usernamePasswordToken, PlainActionFuture.newFuture())); } @@ -76,11 +79,14 @@ public void testAuthenticateDifferentFailureScenarios() throws LoginException, G } } final boolean nullKerberosAuthnToken = rarely(); - final KerberosAuthenticationToken kerberosAuthenticationToken = - nullKerberosAuthnToken ? null : new KerberosAuthenticationToken(decodedTicket); + final KerberosAuthenticationToken kerberosAuthenticationToken = nullKerberosAuthnToken + ? null + : new KerberosAuthenticationToken(decodedTicket); if (nullKerberosAuthnToken) { - expectThrows(AssertionError.class, - () -> kerberosRealm.authenticate(kerberosAuthenticationToken, PlainActionFuture.newFuture())); + expectThrows( + AssertionError.class, + () -> kerberosRealm.authenticate(kerberosAuthenticationToken, PlainActionFuture.newFuture()) + ); } else { final PlainActionFuture> future = new PlainActionFuture<>(); kerberosRealm.authenticate(kerberosAuthenticationToken, future); @@ -97,8 +103,9 @@ public void testAuthenticateDifferentFailureScenarios() throws LoginException, G assertThat(result.getStatus(), is(equalTo(AuthenticationResult.Status.TERMINATE))); if (throwExceptionForInvalidTicket == false) { assertThat(result.getException(), is(instanceOf(ElasticsearchSecurityException.class))); - final List wwwAuthnHeader = ((ElasticsearchSecurityException) result.getException()) - .getHeader(KerberosAuthenticationToken.WWW_AUTHENTICATE); + final List wwwAuthnHeader = ((ElasticsearchSecurityException) result.getException()).getHeader( + KerberosAuthenticationToken.WWW_AUTHENTICATE + ); assertThat(wwwAuthnHeader, is(notNullValue())); assertThat(wwwAuthnHeader.get(0), is(equalTo(KerberosAuthenticationToken.NEGOTIATE_AUTH_HEADER_PREFIX + outToken))); assertThat(result.getMessage(), is(equalTo("failed to authenticate user, gss context negotiation not complete"))); @@ -109,24 +116,31 @@ public void testAuthenticateDifferentFailureScenarios() throws LoginException, G assertThat(result.getMessage(), is(equalTo("failed to authenticate user, gss context negotiation failure"))); } assertThat(result.getException(), is(instanceOf(ElasticsearchSecurityException.class))); - final List wwwAuthnHeader = ((ElasticsearchSecurityException) result.getException()) - .getHeader(KerberosAuthenticationToken.WWW_AUTHENTICATE); + final List wwwAuthnHeader = ((ElasticsearchSecurityException) result.getException()).getHeader( + KerberosAuthenticationToken.WWW_AUTHENTICATE + ); assertThat(wwwAuthnHeader, is(notNullValue())); assertThat(wwwAuthnHeader.get(0), is(equalTo(KerberosAuthenticationToken.NEGOTIATE_SCHEME_NAME))); } } - verify(mockKerberosTicketValidator).validateTicket(aryEq(decodedTicket), eq(keytabPath), eq(krbDebug), - anyActionListener()); + verify(mockKerberosTicketValidator).validateTicket(aryEq(decodedTicket), eq(keytabPath), eq(krbDebug), anyActionListener()); } } public void testDelegatedAuthorizationFailedToResolve() throws Exception { final String username = randomPrincipalName(); RealmConfig.RealmIdentifier realmIdentifier = new RealmConfig.RealmIdentifier("mock", "other_realm"); - final MockLookupRealm otherRealm = new MockLookupRealm(new RealmConfig(realmIdentifier, - Settings.builder().put(globalSettings) - .put(RealmSettings.getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0).build(), - TestEnvironment.newEnvironment(globalSettings), new ThreadContext(globalSettings))); + final MockLookupRealm otherRealm = new MockLookupRealm( + new RealmConfig( + realmIdentifier, + Settings.builder() + .put(globalSettings) + .put(RealmSettings.getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0) + .build(), + TestEnvironment.newEnvironment(globalSettings), + new ThreadContext(globalSettings) + ) + ); final User lookupUser = new User(randomAlphaOfLength(5)); otherRealm.registerUser(lookupUser); @@ -143,8 +157,12 @@ public void testDelegatedAuthorizationFailedToResolve() throws Exception { AuthenticationResult result = future.actionGet(); assertThat(result.getStatus(), is(equalTo(AuthenticationResult.Status.CONTINUE))); - verify(mockKerberosTicketValidator, times(1)).validateTicket(aryEq(decodedTicket), eq(keytabPath), eq(krbDebug), - anyActionListener()); + verify(mockKerberosTicketValidator, times(1)).validateTicket( + aryEq(decodedTicket), + eq(keytabPath), + eq(krbDebug), + anyActionListener() + ); verify(mockNativeRoleMappingStore).refreshRealmOnChange(kerberosRealm); verifyNoMoreInteractions(mockKerberosTicketValidator, mockNativeRoleMappingStore); } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosRealmCacheTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosRealmCacheTests.java index 63cb4ab26f815..dce1019dd6f3b 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosRealmCacheTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosRealmCacheTests.java @@ -21,6 +21,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; + import javax.security.auth.login.LoginException; import static org.elasticsearch.test.ActionListenerUtils.anyActionListener; @@ -58,8 +59,12 @@ public void testAuthenticateWithCache() throws LoginException, GSSException { final User user2 = authenticateAndAssertResult(kerberosRealm, expectedUser, kerberosAuthenticationToken, outToken); assertThat(user1, sameInstance(user2)); - verify(mockKerberosTicketValidator, times(2)).validateTicket(aryEq(decodedTicket), eq(keytabPath), eq(krbDebug), - anyActionListener()); + verify(mockKerberosTicketValidator, times(2)).validateTicket( + aryEq(decodedTicket), + eq(keytabPath), + eq(krbDebug), + anyActionListener() + ); verify(mockNativeRoleMappingStore).refreshRealmOnChange(kerberosRealm); verify(mockNativeRoleMappingStore).resolveRoles(any(UserData.class), anyActionListener()); verifyNoMoreInteractions(mockKerberosTicketValidator, mockNativeRoleMappingStore); @@ -102,16 +107,26 @@ public void testCacheInvalidationScenarios() throws LoginException, GSSException assertThat(user1, sameInstance(user2)); verify(mockNativeRoleMappingStore).resolveRoles(any(UserData.class), anyActionListener()); } - verify(mockKerberosTicketValidator, times(2)).validateTicket(aryEq(decodedTicket), eq(keytabPath), eq(krbDebug), - anyActionListener()); + verify(mockKerberosTicketValidator, times(2)).validateTicket( + aryEq(decodedTicket), + eq(keytabPath), + eq(krbDebug), + anyActionListener() + ); verifyNoMoreInteractions(mockKerberosTicketValidator, mockNativeRoleMappingStore); } - public void testAuthenticateWithValidTicketSucessAuthnWithUserDetailsWhenCacheDisabled() - throws LoginException, GSSException, IOException { + public void testAuthenticateWithValidTicketSucessAuthnWithUserDetailsWhenCacheDisabled() throws LoginException, GSSException, + IOException { // if cache.ttl <= 0 then the cache is disabled - settings = buildKerberosRealmSettings(REALM_NAME, - writeKeyTab(dir.resolve("key.keytab"), randomAlphaOfLength(4)).toString(), 100, "0m", true, randomBoolean()); + settings = buildKerberosRealmSettings( + REALM_NAME, + writeKeyTab(dir.resolve("key.keytab"), randomAlphaOfLength(4)).toString(), + 100, + "0m", + true, + randomBoolean() + ); final String username = randomPrincipalName(); final String outToken = randomAlphaOfLength(10); final KerberosRealm kerberosRealm = createKerberosRealm(username); @@ -133,15 +148,23 @@ public void testAuthenticateWithValidTicketSucessAuthnWithUserDetailsWhenCacheDi final User user2 = authenticateAndAssertResult(kerberosRealm, expectedUser, kerberosAuthenticationToken, outToken); assertThat(user1, not(sameInstance(user2))); - verify(mockKerberosTicketValidator, times(2)).validateTicket(aryEq(decodedTicket), eq(keytabPath), eq(krbDebug), - anyActionListener()); + verify(mockKerberosTicketValidator, times(2)).validateTicket( + aryEq(decodedTicket), + eq(keytabPath), + eq(krbDebug), + anyActionListener() + ); verify(mockNativeRoleMappingStore).refreshRealmOnChange(kerberosRealm); verify(mockNativeRoleMappingStore, times(2)).resolveRoles(any(UserData.class), anyActionListener()); verifyNoMoreInteractions(mockKerberosTicketValidator, mockNativeRoleMappingStore); } - private User authenticateAndAssertResult(final KerberosRealm kerberosRealm, final User expectedUser, - final KerberosAuthenticationToken kerberosAuthenticationToken, String outToken) { + private User authenticateAndAssertResult( + final KerberosRealm kerberosRealm, + final User expectedUser, + final KerberosAuthenticationToken kerberosAuthenticationToken, + String outToken + ) { final PlainActionFuture> future = PlainActionFuture.newFuture(); kerberosRealm.authenticate(kerberosAuthenticationToken, future); final AuthenticationResult result = future.actionGet(); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosRealmSettingsTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosRealmSettingsTests.java index 4538b3f0569c3..9a6e62fbe871f 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosRealmSettingsTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosRealmSettingsTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.xpack.security.authc.kerberos; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.security.authc.RealmConfig; @@ -37,15 +37,27 @@ public void testKerberosRealmSettings() throws IOException { final String cacheTTL = randomLongBetween(10L, 100L) + "m"; final boolean enableDebugLogs = randomBoolean(); final boolean removeRealmName = randomBoolean(); - final Settings settings = KerberosRealmTestCase.buildKerberosRealmSettings(KerberosRealmTestCase.REALM_NAME, - keytabPathConfig, maxUsers, cacheTTL, enableDebugLogs, removeRealmName); + final Settings settings = KerberosRealmTestCase.buildKerberosRealmSettings( + KerberosRealmTestCase.REALM_NAME, + keytabPathConfig, + maxUsers, + cacheTTL, + enableDebugLogs, + removeRealmName + ); final RealmIdentifier identifier = new RealmIdentifier(KerberosRealmSettings.TYPE, KerberosRealmTestCase.REALM_NAME); - final RealmConfig config = new RealmConfig(identifier, - settings, TestEnvironment.newEnvironment(settings), new ThreadContext(settings)); + final RealmConfig config = new RealmConfig( + identifier, + settings, + TestEnvironment.newEnvironment(settings), + new ThreadContext(settings) + ); assertThat(config.getSetting(KerberosRealmSettings.HTTP_SERVICE_KEYTAB_PATH), equalTo(keytabPathConfig)); - assertThat(config.getSetting(KerberosRealmSettings.CACHE_TTL_SETTING), - equalTo(TimeValue.parseTimeValue(cacheTTL, KerberosRealmSettings.CACHE_TTL_SETTING.getKey()))); + assertThat( + config.getSetting(KerberosRealmSettings.CACHE_TTL_SETTING), + equalTo(TimeValue.parseTimeValue(cacheTTL, KerberosRealmSettings.CACHE_TTL_SETTING.getKey())) + ); assertThat(config.getSetting(KerberosRealmSettings.CACHE_MAX_USERS_SETTING), equalTo(maxUsers)); assertThat(config.getSetting(KerberosRealmSettings.SETTING_KRB_DEBUG_ENABLE), is(enableDebugLogs)); assertThat(config.getSetting(KerberosRealmSettings.SETTING_REMOVE_REALM_NAME), is(removeRealmName)); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosRealmTestCase.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosRealmTestCase.java index 96bcf337d3c51..3378bbaee0c01 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosRealmTestCase.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosRealmTestCase.java @@ -82,8 +82,14 @@ public void setup() throws Exception { resourceWatcherService = new ResourceWatcherService(Settings.EMPTY, threadPool); dir = createTempDir(); globalSettings = Settings.builder().put("path.home", dir).build(); - settings = buildKerberosRealmSettings(REALM_NAME, - writeKeyTab(dir.resolve("key.keytab"), "asa").toString(), 100, "10m", true, randomBoolean()); + settings = buildKerberosRealmSettings( + REALM_NAME, + writeKeyTab(dir.resolve("key.keytab"), "asa").toString(), + 100, + "10m", + true, + randomBoolean() + ); licenseState = mock(MockLicenseState.class); when(licenseState.isAllowed(Security.DELEGATED_AUTHORIZATION_FEATURE)).thenReturn(true); } @@ -95,8 +101,13 @@ public void shutdown() throws InterruptedException { } @SuppressWarnings("unchecked") - protected void mockKerberosTicketValidator(final byte[] decodedTicket, final Path keytabPath, final boolean krbDebug, - final Tuple value, final Exception e) { + protected void mockKerberosTicketValidator( + final byte[] decodedTicket, + final Path keytabPath, + final boolean krbDebug, + final Tuple value, + final Exception e + ) { assert value != null || e != null; doAnswer((i) -> { ActionListener> listener = (ActionListener>) i.getArguments()[3]; @@ -110,15 +121,19 @@ protected void mockKerberosTicketValidator(final byte[] decodedTicket, final Pat } protected void assertSuccessAuthenticationResult( - final User expectedUser, final String outToken, final AuthenticationResult result + final User expectedUser, + final String outToken, + final AuthenticationResult result ) { assertThat(result, is(notNullValue())); assertThat(result.getStatus(), is(equalTo(AuthenticationResult.Status.SUCCESS))); assertThat(result.getValue(), is(equalTo(expectedUser))); final Map> responseHeaders = threadPool.getThreadContext().getResponseHeaders(); assertThat(responseHeaders, is(notNullValue())); - assertThat(responseHeaders.get(KerberosAuthenticationToken.WWW_AUTHENTICATE).get(0), - is(equalTo(KerberosAuthenticationToken.NEGOTIATE_AUTH_HEADER_PREFIX + outToken))); + assertThat( + responseHeaders.get(KerberosAuthenticationToken.WWW_AUTHENTICATE).get(0), + is(equalTo(KerberosAuthenticationToken.NEGOTIATE_AUTH_HEADER_PREFIX + outToken)) + ); } protected KerberosRealm createKerberosRealm(final String... userForRoleMapping) { @@ -127,19 +142,29 @@ protected KerberosRealm createKerberosRealm(final String... userForRoleMapping) protected KerberosRealm createKerberosRealm(final List delegatedRealms, final String... userForRoleMapping) { final RealmConfig.RealmIdentifier id = new RealmConfig.RealmIdentifier(KerberosRealmSettings.TYPE, REALM_NAME); - config = new RealmConfig(id, merge(id, settings, globalSettings), - TestEnvironment.newEnvironment(globalSettings), new ThreadContext(globalSettings)); + config = new RealmConfig( + id, + merge(id, settings, globalSettings), + TestEnvironment.newEnvironment(globalSettings), + new ThreadContext(globalSettings) + ); mockNativeRoleMappingStore = roleMappingStore(Arrays.asList(userForRoleMapping)); mockKerberosTicketValidator = mock(KerberosTicketValidator.class); - final KerberosRealm kerberosRealm = - new KerberosRealm(config, mockNativeRoleMappingStore, mockKerberosTicketValidator, threadPool, null); + final KerberosRealm kerberosRealm = new KerberosRealm( + config, + mockNativeRoleMappingStore, + mockKerberosTicketValidator, + threadPool, + null + ); Collections.shuffle(delegatedRealms, random()); kerberosRealm.initialize(delegatedRealms, licenseState); return kerberosRealm; } private Settings merge(RealmConfig.RealmIdentifier identifier, Settings realmSettings, Settings globalSettings) { - return Settings.builder().put(realmSettings) + return Settings.builder() + .put(realmSettings) .normalizePrefix(RealmSettings.realmSettingPrefix(identifier)) .put(globalSettings) .put(RealmSettings.getFullSettingKey(identifier, RealmSettings.ORDER_SETTING), 0) @@ -153,8 +178,12 @@ protected NativeRoleMappingStore roleMappingStore(final List userNames) when(mockClient.threadPool()).thenReturn(threadPool); when(mockClient.settings()).thenReturn(settings); - final NativeRoleMappingStore store = new NativeRoleMappingStore(Settings.EMPTY, mockClient, mock(SecurityIndexManager.class), - mock(ScriptService.class)); + final NativeRoleMappingStore store = new NativeRoleMappingStore( + Settings.EMPTY, + mockClient, + mock(SecurityIndexManager.class), + mock(ScriptService.class) + ); final NativeRoleMappingStore roleMapper = spy(store); doAnswer(invocation -> { @@ -164,7 +193,8 @@ protected NativeRoleMappingStore roleMappingStore(final List userNames) listener.onResponse(roles); } else { listener.onFailure( - Exceptions.authorizationError("Expected UPN '" + expectedUserNames + "' but was '" + userData.getUsername() + "'")); + Exceptions.authorizationError("Expected UPN '" + expectedUserNames + "' but was '" + userData.getUsername() + "'") + ); } return null; }).when(roleMapper).resolveRoles(any(UserRoleMapper.UserData.class), any(ActionListener.class)); @@ -241,12 +271,18 @@ public static Path writeKeyTab(final Path keytabPath, final String content) thro * @param keytabPath key tab file path * @return {@link Settings} for kerberos realm */ - public static Settings buildKerberosRealmSettings(final String realmName,final String keytabPath) { + public static Settings buildKerberosRealmSettings(final String realmName, final String keytabPath) { return buildKerberosRealmSettings(realmName, keytabPath, 100, "10m", true, false); } - public static Settings buildKerberosRealmSettings(String realmName, String keytabPath, int maxUsersInCache, String cacheTTL, - boolean enableDebugging, boolean removeRealmName) { + public static Settings buildKerberosRealmSettings( + String realmName, + String keytabPath, + int maxUsersInCache, + String cacheTTL, + boolean enableDebugging, + boolean removeRealmName + ) { final Settings global = Settings.builder().put("path.home", createTempDir()).build(); return buildKerberosRealmSettings(realmName, keytabPath, maxUsersInCache, cacheTTL, enableDebugging, removeRealmName, global); } @@ -264,8 +300,15 @@ public static Settings buildKerberosRealmSettings(String realmName, String keyta * @return {@link Settings} for kerberos realm */ - public static Settings buildKerberosRealmSettings(String realmName, String keytabPath, int maxUsersInCache, String cacheTTL, - boolean enableDebugging, boolean removeRealmName, Settings globalSettings) { + public static Settings buildKerberosRealmSettings( + String realmName, + String keytabPath, + int maxUsersInCache, + String cacheTTL, + boolean enableDebugging, + boolean removeRealmName, + Settings globalSettings + ) { final Settings.Builder builder = Settings.builder() .put(RealmSettings.getFullSettingKey(realmName, KerberosRealmSettings.HTTP_SERVICE_KEYTAB_PATH), keytabPath) .put(RealmSettings.getFullSettingKey(realmName, KerberosRealmSettings.CACHE_MAX_USERS_SETTING), maxUsersInCache) diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosRealmTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosRealmTests.java index 6e3113e71c335..ac98ee6d19b18 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosRealmTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosRealmTests.java @@ -43,6 +43,7 @@ import java.util.Locale; import java.util.Map; import java.util.Set; + import javax.security.auth.login.LoginException; import static org.elasticsearch.test.ActionListenerUtils.anyActionListener; @@ -65,8 +66,10 @@ public void testSupports() { final KerberosAuthenticationToken kerberosAuthenticationToken = new KerberosAuthenticationToken(randomByteArrayOfLength(5)); assertThat(kerberosRealm.supports(kerberosAuthenticationToken), is(true)); - final UsernamePasswordToken usernamePasswordToken = - new UsernamePasswordToken(randomAlphaOfLength(5), new SecureString(new char[] { 'a', 'b', 'c' })); + final UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken( + randomAlphaOfLength(5), + new SecureString(new char[] { 'a', 'b', 'c' }) + ); assertThat(kerberosRealm.supports(usernamePasswordToken), is(false)); } @@ -88,8 +91,12 @@ public void testAuthenticateWithValidTicketSucessAuthnWithUserDetails() throws L kerberosRealm.authenticate(kerberosAuthenticationToken, future); assertSuccessAuthenticationResult(expectedUser, "out-token", future.actionGet()); - verify(mockKerberosTicketValidator, times(1)).validateTicket(aryEq(decodedTicket), eq(keytabPath), eq(krbDebug), - anyActionListener()); + verify(mockKerberosTicketValidator, times(1)).validateTicket( + aryEq(decodedTicket), + eq(keytabPath), + eq(krbDebug), + anyActionListener() + ); verify(mockNativeRoleMappingStore).refreshRealmOnChange(kerberosRealm); verify(mockNativeRoleMappingStore).resolveRoles(any(UserData.class), anyActionListener()); verifyNoMoreInteractions(mockKerberosTicketValidator, mockNativeRoleMappingStore); @@ -108,8 +115,16 @@ public void testFailedAuthorization() throws LoginException, GSSException { ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, future::actionGet); assertThat(e.status(), is(RestStatus.FORBIDDEN)); - assertThat(e.getMessage(), equalTo("Expected UPN '" + Arrays.asList(maybeRemoveRealmName(username)) + "' but was '" - + maybeRemoveRealmName("does-not-exist@REALM") + "'")); + assertThat( + e.getMessage(), + equalTo( + "Expected UPN '" + + Arrays.asList(maybeRemoveRealmName(username)) + + "' but was '" + + maybeRemoveRealmName("does-not-exist@REALM") + + "'" + ) + ); } public void testLookupUser() { @@ -137,23 +152,35 @@ public void testKerberosRealmThrowsErrorWhenKeytabFileDoesNotExist() throws IOEx } public void testKerberosRealmThrowsErrorWhenKeytabFileHasNoReadPermissions() throws IOException { - assumeFalse("Not running this test on Windows, as it requires additional access permissions for test framework.", - Constants.WINDOWS); + assumeFalse( + "Not running this test on Windows, as it requires additional access permissions for test framework.", + Constants.WINDOWS + ); final Set supportedAttributes = dir.getFileSystem().supportedFileAttributeViews(); final String keytabFileName = randomAlphaOfLength(5) + ".keytab"; final Path keytabPath; if (supportedAttributes.contains("posix")) { final Set filePerms = PosixFilePermissions.fromString("---------"); final FileAttribute> fileAttributes = PosixFilePermissions.asFileAttribute(filePerms); - try (SeekableByteChannel byteChannel = Files.newByteChannel(dir.resolve(keytabFileName), - EnumSet.of(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE), fileAttributes)) { + try ( + SeekableByteChannel byteChannel = Files.newByteChannel( + dir.resolve(keytabFileName), + EnumSet.of(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE), + fileAttributes + ) + ) { byteChannel.write(ByteBuffer.wrap(randomByteArrayOfLength(10))); } keytabPath = dir.resolve(keytabFileName); } else { throw new UnsupportedOperationException( - String.format(Locale.ROOT, "Don't know how to make file [%s] non-readable on a file system with attributes [%s]", - dir.resolve(keytabFileName), supportedAttributes)); + String.format( + Locale.ROOT, + "Don't know how to make file [%s] non-readable on a file system with attributes [%s]", + dir.resolve(keytabFileName), + supportedAttributes + ) + ); } final String expectedErrorMessage = "configured service key tab file [" + keytabPath + "] must have read permission"; @@ -163,12 +190,18 @@ public void testKerberosRealmThrowsErrorWhenKeytabFileHasNoReadPermissions() thr private void assertKerberosRealmConstructorFails(final String keytabPath, final String expectedErrorMessage) { final String realmName = "test-kerb-realm"; settings = buildKerberosRealmSettings(realmName, keytabPath, 100, "10m", true, randomBoolean(), globalSettings); - config = new RealmConfig(new RealmConfig.RealmIdentifier(KerberosRealmSettings.TYPE, realmName), settings, - TestEnvironment.newEnvironment(settings), new ThreadContext(settings)); + config = new RealmConfig( + new RealmConfig.RealmIdentifier(KerberosRealmSettings.TYPE, realmName), + settings, + TestEnvironment.newEnvironment(settings), + new ThreadContext(settings) + ); mockNativeRoleMappingStore = roleMappingStore(Arrays.asList("user")); mockKerberosTicketValidator = mock(KerberosTicketValidator.class); - final IllegalArgumentException iae = expectThrows(IllegalArgumentException.class, - () -> new KerberosRealm(config, mockNativeRoleMappingStore, mockKerberosTicketValidator, threadPool, null)); + final IllegalArgumentException iae = expectThrows( + IllegalArgumentException.class, + () -> new KerberosRealm(config, mockNativeRoleMappingStore, mockKerberosTicketValidator, threadPool, null) + ); assertThat(iae.getMessage(), is(equalTo(expectedErrorMessage))); } @@ -176,13 +209,27 @@ public void testDelegatedAuthorization() throws Exception { final String username = randomPrincipalName(); final String expectedUsername = maybeRemoveRealmName(username); RealmConfig.RealmIdentifier realmIdentifier = new RealmConfig.RealmIdentifier("mock", "other_realm"); - final MockLookupRealm otherRealm = spy(new MockLookupRealm(new RealmConfig( - realmIdentifier, - Settings.builder().put(globalSettings) - .put(RealmSettings.getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0).build(), - TestEnvironment.newEnvironment(globalSettings), new ThreadContext(globalSettings)))); - final User lookupUser = new User(expectedUsername, new String[] { "admin-role" }, expectedUsername, - expectedUsername + "@example.com", Collections.singletonMap("k1", "v1"), true); + final MockLookupRealm otherRealm = spy( + new MockLookupRealm( + new RealmConfig( + realmIdentifier, + Settings.builder() + .put(globalSettings) + .put(RealmSettings.getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0) + .build(), + TestEnvironment.newEnvironment(globalSettings), + new ThreadContext(globalSettings) + ) + ) + ); + final User lookupUser = new User( + expectedUsername, + new String[] { "admin-role" }, + expectedUsername, + expectedUsername + "@example.com", + Collections.singletonMap("k1", "v1"), + true + ); otherRealm.registerUser(lookupUser); settings = Settings.builder().put(settings).putList("authorization_realms", "other_realm").build(); @@ -202,11 +249,14 @@ public void testDelegatedAuthorization() throws Exception { kerberosRealm.authenticate(kerberosAuthenticationToken, future); assertSuccessAuthenticationResult(expectedUser, "out-token", future.actionGet()); - verify(mockKerberosTicketValidator, times(2)).validateTicket(aryEq(decodedTicket), eq(keytabPath), eq(krbDebug), - anyActionListener()); + verify(mockKerberosTicketValidator, times(2)).validateTicket( + aryEq(decodedTicket), + eq(keytabPath), + eq(krbDebug), + anyActionListener() + ); verify(mockNativeRoleMappingStore).refreshRealmOnChange(kerberosRealm); verifyNoMoreInteractions(mockKerberosTicketValidator, mockNativeRoleMappingStore); verify(otherRealm, times(2)).lookupUser(eq(expectedUsername), anyActionListener()); } } - diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/ActiveDirectorySIDUtilTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/ActiveDirectorySIDUtilTests.java index 7654803d564dd..203c7a179cf56 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/ActiveDirectorySIDUtilTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/ActiveDirectorySIDUtilTests.java @@ -13,7 +13,7 @@ public class ActiveDirectorySIDUtilTests extends ESTestCase { - private static final String USER_SID_HEX ="01050000000000051500000050bd51b583ef8ebc4c75521ae9030000"; + private static final String USER_SID_HEX = "01050000000000051500000050bd51b583ef8ebc4c75521ae9030000"; private static final String USER_STRING_SID = "S-1-5-21-3042032976-3163484035-441611596-1001"; public void testSidConversion() throws Exception { diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/CancellableLdapRunnableTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/CancellableLdapRunnableTests.java index 9f8de13b9fac0..473404fcf8749 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/CancellableLdapRunnableTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/CancellableLdapRunnableTests.java @@ -25,32 +25,30 @@ public class CancellableLdapRunnableTests extends ESTestCase { public void testTimingOutARunnable() { AtomicReference exceptionAtomicReference = new AtomicReference<>(); - final CancellableLdapRunnable runnable = - new CancellableLdapRunnable<>(ActionListener.wrap(user -> { - throw new AssertionError("onResponse should not be called"); - }, exceptionAtomicReference::set), e -> null, () -> { - throw new AssertionError("runnable should not be executed"); - }, logger); + final CancellableLdapRunnable runnable = new CancellableLdapRunnable<>( + ActionListener.wrap(user -> { throw new AssertionError("onResponse should not be called"); }, exceptionAtomicReference::set), + e -> null, + () -> { throw new AssertionError("runnable should not be executed"); }, + logger + ); runnable.maybeTimeout(); runnable.run(); assertNotNull(exceptionAtomicReference.get()); assertThat(exceptionAtomicReference.get(), instanceOf(ElasticsearchTimeoutException.class)); - assertThat(exceptionAtomicReference.get().getMessage(), - containsString("timed out waiting for execution")); + assertThat(exceptionAtomicReference.get().getMessage(), containsString("timed out waiting for execution")); } public void testCallTimeOutAfterRunning() { final AtomicBoolean ran = new AtomicBoolean(false); final AtomicBoolean listenerCalled = new AtomicBoolean(false); - final CancellableLdapRunnable runnable = - new CancellableLdapRunnable<>(ActionListener.wrap(user -> { - listenerCalled.set(true); - throw new AssertionError("onResponse should not be called"); - }, e -> { - listenerCalled.set(true); - throw new AssertionError("onFailure should not be called"); - }), e -> null, () -> ran.set(ran.get() == false), logger); + final CancellableLdapRunnable runnable = new CancellableLdapRunnable<>(ActionListener.wrap(user -> { + listenerCalled.set(true); + throw new AssertionError("onResponse should not be called"); + }, e -> { + listenerCalled.set(true); + throw new AssertionError("onFailure should not be called"); + }), e -> null, () -> ran.set(ran.get() == false), logger); runnable.run(); assertTrue(ran.get()); @@ -63,12 +61,12 @@ public void testCallTimeOutAfterRunning() { public void testRejectingExecution() { AtomicReference exceptionAtomicReference = new AtomicReference<>(); - final CancellableLdapRunnable runnable = - new CancellableLdapRunnable<>(ActionListener.wrap(user -> { - throw new AssertionError("onResponse should not be called"); - }, exceptionAtomicReference::set), e -> null, () -> { - throw new AssertionError("runnable should not be executed"); - }, logger); + final CancellableLdapRunnable runnable = new CancellableLdapRunnable<>( + ActionListener.wrap(user -> { throw new AssertionError("onResponse should not be called"); }, exceptionAtomicReference::set), + e -> null, + () -> { throw new AssertionError("runnable should not be executed"); }, + logger + ); final Exception e = new RuntimeException("foo"); runnable.onRejection(e); @@ -81,11 +79,10 @@ public void testTimeoutDuringExecution() throws InterruptedException { final CountDownLatch listenerCalledLatch = new CountDownLatch(1); final CountDownLatch timeoutCalledLatch = new CountDownLatch(1); final CountDownLatch runningLatch = new CountDownLatch(1); - final ActionListener listener = ActionListener.wrap(user -> { - listenerCalledLatch.countDown(); - }, e -> { - throw new AssertionError("onFailure should not be executed"); - }); + final ActionListener listener = ActionListener.wrap( + user -> { listenerCalledLatch.countDown(); }, + e -> { throw new AssertionError("onFailure should not be executed"); } + ); final CancellableLdapRunnable runnable = new CancellableLdapRunnable<>(listener, e -> null, () -> { runningLatch.countDown(); try { @@ -107,14 +104,17 @@ public void testTimeoutDuringExecution() throws InterruptedException { public void testExceptionInRunnable() { AtomicReference resultRef = new AtomicReference<>(); - final ActionListener listener = ActionListener.wrap(resultRef::set, e -> { - throw new AssertionError("onFailure should not be executed"); - }); + final ActionListener listener = ActionListener.wrap( + resultRef::set, + e -> { throw new AssertionError("onFailure should not be executed"); } + ); String defaultValue = randomAlphaOfLengthBetween(2, 10); - final CancellableLdapRunnable runnable = new CancellableLdapRunnable<>(listener, e -> defaultValue, - () -> { - throw new RuntimeException("runnable intentionally failed"); - }, logger); + final CancellableLdapRunnable runnable = new CancellableLdapRunnable<>( + listener, + e -> defaultValue, + () -> { throw new RuntimeException("runnable intentionally failed"); }, + logger + ); runnable.run(); assertThat(resultRef.get(), equalTo(defaultValue)); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/GroupsResolverTestCase.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/GroupsResolverTestCase.java index 188537613d95f..57bda2ad9cc1d 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/GroupsResolverTestCase.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/GroupsResolverTestCase.java @@ -9,16 +9,17 @@ import com.unboundid.ldap.sdk.Attribute; import com.unboundid.ldap.sdk.LDAPConnection; import com.unboundid.ldap.sdk.LDAPInterface; + import org.apache.logging.log4j.Logger; import org.elasticsearch.action.support.PlainActionFuture; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.env.TestEnvironment; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.security.authc.RealmConfig; import org.elasticsearch.xpack.core.security.authc.RealmSettings; import org.elasticsearch.xpack.security.authc.ldap.support.LdapSession.GroupsResolver; -import org.elasticsearch.test.ESTestCase; import org.junit.After; import org.junit.Before; @@ -34,7 +35,9 @@ public abstract class GroupsResolverTestCase extends ESTestCase { protected static RealmConfig config(RealmConfig.RealmIdentifier realmId, Settings settings) { if (settings.hasValue("path.home") == false) { - settings = Settings.builder().put(settings).put("path.home", createTempDir()) + settings = Settings.builder() + .put(settings) + .put("path.home", createTempDir()) .put(getFullSettingKey(realmId, RealmSettings.ORDER_SETTING), 0) .build(); } @@ -62,8 +65,14 @@ public void tearDownLdapConnection() throws Exception { } } - protected static List resolveBlocking(GroupsResolver resolver, LDAPInterface ldapConnection, String dn, TimeValue timeLimit, - Logger logger, Collection attributes) { + protected static List resolveBlocking( + GroupsResolver resolver, + LDAPInterface ldapConnection, + String dn, + TimeValue timeLimit, + Logger logger, + Collection attributes + ) { PlainActionFuture> future = new PlainActionFuture<>(); resolver.resolve(ldapConnection, dn, timeLimit, logger, attributes, future); return future.actionGet(); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/LdapRealmTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/LdapRealmTests.java index ca403cb9892f2..7a7ec59a6ba22 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/LdapRealmTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/LdapRealmTests.java @@ -124,8 +124,7 @@ public void testAuthenticateSubTreeGroupSearch() throws Exception { .build(); RealmConfig config = getRealmConfig(REALM_IDENTIFIER, settings); LdapSessionFactory ldapFactory = new LdapSessionFactory(config, sslService, threadPool); - LdapRealm ldap = new LdapRealm(config, ldapFactory, buildGroupAsRoleMapper(resourceWatcherService), - threadPool); + LdapRealm ldap = new LdapRealm(config, ldapFactory, buildGroupAsRoleMapper(resourceWatcherService), threadPool); ldap.initialize(Collections.singleton(ldap), licenseState); PlainActionFuture> future = new PlainActionFuture<>(); @@ -150,15 +149,14 @@ public void testAuthenticateOneLevelGroupSearch() throws Exception { String groupSearchBase = "ou=crews,ou=groups,o=sevenSeas"; String userTemplate = VALID_USER_TEMPLATE; Settings settings = Settings.builder() - .put(defaultGlobalSettings) - .put(buildLdapSettings(ldapUrls(), userTemplate, groupSearchBase, LdapSearchScope.ONE_LEVEL)) - .put(getFullSettingKey(REALM_IDENTIFIER, RealmSettings.ORDER_SETTING), 0) - .build(); + .put(defaultGlobalSettings) + .put(buildLdapSettings(ldapUrls(), userTemplate, groupSearchBase, LdapSearchScope.ONE_LEVEL)) + .put(getFullSettingKey(REALM_IDENTIFIER, RealmSettings.ORDER_SETTING), 0) + .build(); RealmConfig config = getRealmConfig(REALM_IDENTIFIER, settings); LdapSessionFactory ldapFactory = new LdapSessionFactory(config, sslService, threadPool); - LdapRealm ldap = - new LdapRealm(config, ldapFactory, buildGroupAsRoleMapper(resourceWatcherService), threadPool); + LdapRealm ldap = new LdapRealm(config, ldapFactory, buildGroupAsRoleMapper(resourceWatcherService), threadPool); ldap.initialize(Collections.singleton(ldap), licenseState); PlainActionFuture> future = new PlainActionFuture<>(); @@ -178,16 +176,15 @@ public void testAuthenticateCaching() throws Exception { String groupSearchBase = "o=sevenSeas"; String userTemplate = VALID_USER_TEMPLATE; Settings settings = Settings.builder() - .put(defaultGlobalSettings) - .put(buildLdapSettings(ldapUrls(), userTemplate, groupSearchBase, LdapSearchScope.SUB_TREE)) - .put(getFullSettingKey(REALM_IDENTIFIER, RealmSettings.ORDER_SETTING), 0) - .build(); + .put(defaultGlobalSettings) + .put(buildLdapSettings(ldapUrls(), userTemplate, groupSearchBase, LdapSearchScope.SUB_TREE)) + .put(getFullSettingKey(REALM_IDENTIFIER, RealmSettings.ORDER_SETTING), 0) + .build(); RealmConfig config = getRealmConfig(REALM_IDENTIFIER, settings); LdapSessionFactory ldapFactory = new LdapSessionFactory(config, sslService, threadPool); ldapFactory = spy(ldapFactory); - LdapRealm ldap = - new LdapRealm(config, ldapFactory, buildGroupAsRoleMapper(resourceWatcherService), threadPool); + LdapRealm ldap = new LdapRealm(config, ldapFactory, buildGroupAsRoleMapper(resourceWatcherService), threadPool); ldap.initialize(Collections.singleton(ldap), licenseState); PlainActionFuture> future = new PlainActionFuture<>(); @@ -198,7 +195,7 @@ public void testAuthenticateCaching() throws Exception { ldap.authenticate(new UsernamePasswordToken(VALID_USERNAME, new SecureString(PASSWORD)), future); assertThat(future.actionGet().getStatus(), is(AuthenticationResult.Status.SUCCESS)); - //verify one and only one session -> caching is working + // verify one and only one session -> caching is working verify(ldapFactory, times(1)).session(anyString(), any(SecureString.class), anyActionListener()); } @@ -206,10 +203,10 @@ public void testAuthenticateCachingRefresh() throws Exception { String groupSearchBase = "o=sevenSeas"; String userTemplate = VALID_USER_TEMPLATE; Settings settings = Settings.builder() - .put(buildLdapSettings(ldapUrls(), userTemplate, groupSearchBase, LdapSearchScope.SUB_TREE)) - .put(defaultGlobalSettings) - .put(getFullSettingKey(REALM_IDENTIFIER, RealmSettings.ORDER_SETTING), 0) - .build(); + .put(buildLdapSettings(ldapUrls(), userTemplate, groupSearchBase, LdapSearchScope.SUB_TREE)) + .put(defaultGlobalSettings) + .put(getFullSettingKey(REALM_IDENTIFIER, RealmSettings.ORDER_SETTING), 0) + .build(); RealmConfig config = getRealmConfig(REALM_IDENTIFIER, settings); LdapSessionFactory ldapFactory = new LdapSessionFactory(config, sslService, threadPool); @@ -225,7 +222,7 @@ public void testAuthenticateCachingRefresh() throws Exception { ldap.authenticate(new UsernamePasswordToken(VALID_USERNAME, new SecureString(PASSWORD)), future); future.actionGet(); - //verify one and only one session -> caching is working + // verify one and only one session -> caching is working verify(ldapFactory, times(1)).session(anyString(), any(SecureString.class), anyActionListener()); roleMapper.notifyRefresh(); @@ -234,7 +231,7 @@ public void testAuthenticateCachingRefresh() throws Exception { ldap.authenticate(new UsernamePasswordToken(VALID_USERNAME, new SecureString(PASSWORD)), future); future.actionGet(); - //we need to session again + // we need to session again verify(ldapFactory, times(2)).session(anyString(), any(SecureString.class), anyActionListener()); } @@ -242,17 +239,16 @@ public void testAuthenticateNoncaching() throws Exception { String groupSearchBase = "o=sevenSeas"; String userTemplate = VALID_USER_TEMPLATE; Settings settings = Settings.builder() - .put(defaultGlobalSettings) - .put(buildLdapSettings(ldapUrls(), userTemplate, groupSearchBase, LdapSearchScope.SUB_TREE)) - .put(getFullSettingKey(REALM_IDENTIFIER, CachingUsernamePasswordRealmSettings.CACHE_TTL_SETTING), -1) - .put(getFullSettingKey(REALM_IDENTIFIER, RealmSettings.ORDER_SETTING), 0) - .build(); + .put(defaultGlobalSettings) + .put(buildLdapSettings(ldapUrls(), userTemplate, groupSearchBase, LdapSearchScope.SUB_TREE)) + .put(getFullSettingKey(REALM_IDENTIFIER, CachingUsernamePasswordRealmSettings.CACHE_TTL_SETTING), -1) + .put(getFullSettingKey(REALM_IDENTIFIER, RealmSettings.ORDER_SETTING), 0) + .build(); RealmConfig config = getRealmConfig(REALM_IDENTIFIER, settings); LdapSessionFactory ldapFactory = new LdapSessionFactory(config, sslService, threadPool); ldapFactory = spy(ldapFactory); - LdapRealm ldap = - new LdapRealm(config, ldapFactory, buildGroupAsRoleMapper(resourceWatcherService), threadPool); + LdapRealm ldap = new LdapRealm(config, ldapFactory, buildGroupAsRoleMapper(resourceWatcherService), threadPool); ldap.initialize(Collections.singleton(ldap), licenseState); PlainActionFuture> future = new PlainActionFuture<>(); @@ -262,7 +258,7 @@ public void testAuthenticateNoncaching() throws Exception { ldap.authenticate(new UsernamePasswordToken(VALID_USERNAME, new SecureString(PASSWORD)), future); future.actionGet(); - //verify two and only two binds -> caching is disabled + // verify two and only two binds -> caching is disabled verify(ldapFactory, times(2)).session(anyString(), any(SecureString.class), anyActionListener()); } @@ -288,11 +284,17 @@ public void testDelegatedAuthorization() throws Exception { final LdapRealm ldap = new LdapRealm(config, ldapFactory, roleMapper, threadPool); RealmConfig.RealmIdentifier realmIdentifier = new RealmConfig.RealmIdentifier("mock", "mock_lookup"); - final MockLookupRealm mockLookup = new MockLookupRealm(new RealmConfig( - realmIdentifier, - Settings.builder().put(defaultGlobalSettings) - .put(getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0).build(), - env, threadPool.getThreadContext())); + final MockLookupRealm mockLookup = new MockLookupRealm( + new RealmConfig( + realmIdentifier, + Settings.builder() + .put(defaultGlobalSettings) + .put(getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0) + .build(), + env, + threadPool.getThreadContext() + ) + ); ldap.initialize(Arrays.asList(ldap, mockLookup), licenseState); mockLookup.initialize(Arrays.asList(ldap, mockLookup), licenseState); @@ -301,8 +303,10 @@ public void testDelegatedAuthorization() throws Exception { ldap.authenticate(new UsernamePasswordToken(VALID_USERNAME, new SecureString(PASSWORD)), future); final AuthenticationResult result1 = future.actionGet(); assertThat(result1.getStatus(), equalTo(AuthenticationResult.Status.CONTINUE)); - assertThat(result1.getMessage(), - equalTo("the principal [" + VALID_USERNAME + "] was authenticated, but no user could be found in realms [mock/mock_lookup]")); + assertThat( + result1.getMessage(), + equalTo("the principal [" + VALID_USERNAME + "] was authenticated, but no user could be found in realms [mock/mock_lookup]") + ); future = new PlainActionFuture<>(); final User fakeUser = new User(VALID_USERNAME, "fake_role"); @@ -318,15 +322,15 @@ public void testLdapRealmSelectsLdapSessionFactory() throws Exception { String groupSearchBase = "o=sevenSeas"; String userTemplate = VALID_USER_TEMPLATE; Settings settings = Settings.builder() - .put(defaultGlobalSettings) - - .putList(getFullSettingKey(identifier, URLS_SETTING), ldapUrls()) - .putList(getFullSettingKey(identifier.getName(), LdapSessionFactorySettings.USER_DN_TEMPLATES_SETTING), userTemplate) - .put(getFullSettingKey(identifier, SearchGroupsResolverSettings.BASE_DN), groupSearchBase) - .put(getFullSettingKey(identifier, SearchGroupsResolverSettings.SCOPE), LdapSearchScope.SUB_TREE) - .put(getFullSettingKey(identifier, VERIFICATION_MODE_SETTING_REALM), SslVerificationMode.CERTIFICATE) - .put(getFullSettingKey(identifier, RealmSettings.ORDER_SETTING), 0) - .build(); + .put(defaultGlobalSettings) + + .putList(getFullSettingKey(identifier, URLS_SETTING), ldapUrls()) + .putList(getFullSettingKey(identifier.getName(), LdapSessionFactorySettings.USER_DN_TEMPLATES_SETTING), userTemplate) + .put(getFullSettingKey(identifier, SearchGroupsResolverSettings.BASE_DN), groupSearchBase) + .put(getFullSettingKey(identifier, SearchGroupsResolverSettings.SCOPE), LdapSearchScope.SUB_TREE) + .put(getFullSettingKey(identifier, VERIFICATION_MODE_SETTING_REALM), SslVerificationMode.CERTIFICATE) + .put(getFullSettingKey(identifier, RealmSettings.ORDER_SETTING), 0) + .build(); RealmConfig config = getRealmConfig(identifier, settings); final SSLService ssl = new SSLService(config.env()); SessionFactory sessionFactory = LdapRealm.sessionFactory(config, ssl, threadPool); @@ -334,21 +338,22 @@ public void testLdapRealmSelectsLdapSessionFactory() throws Exception { } public void testLdapRealmSelectsLdapUserSearchSessionFactory() throws Exception { - final RealmConfig.RealmIdentifier identifier - = new RealmConfig.RealmIdentifier(LdapRealmSettings.LDAP_TYPE, "test-ldap-realm-user-search"); + final RealmConfig.RealmIdentifier identifier = new RealmConfig.RealmIdentifier( + LdapRealmSettings.LDAP_TYPE, + "test-ldap-realm-user-search" + ); String groupSearchBase = "o=sevenSeas"; Settings settings = Settings.builder() - .put(defaultGlobalSettings) - .putList(getFullSettingKey(identifier, URLS_SETTING), ldapUrls()) - .put(getFullSettingKey(identifier.getName(), LdapUserSearchSessionFactorySettings.SEARCH_BASE_DN), "") - .put(getFullSettingKey(identifier, PoolingSessionFactorySettings.BIND_DN), - "cn=Thomas Masterman Hardy,ou=people,o=sevenSeas") - .setSecureSettings(secureSettings(PoolingSessionFactorySettings.SECURE_BIND_PASSWORD, identifier, PASSWORD)) - .put(getFullSettingKey(identifier, SearchGroupsResolverSettings.BASE_DN), groupSearchBase) - .put(getFullSettingKey(identifier, SearchGroupsResolverSettings.SCOPE), LdapSearchScope.SUB_TREE) - .put(getFullSettingKey(identifier, VERIFICATION_MODE_SETTING_REALM), SslVerificationMode.CERTIFICATE) - .put(getFullSettingKey(identifier, RealmSettings.ORDER_SETTING), 0) - .build(); + .put(defaultGlobalSettings) + .putList(getFullSettingKey(identifier, URLS_SETTING), ldapUrls()) + .put(getFullSettingKey(identifier.getName(), LdapUserSearchSessionFactorySettings.SEARCH_BASE_DN), "") + .put(getFullSettingKey(identifier, PoolingSessionFactorySettings.BIND_DN), "cn=Thomas Masterman Hardy,ou=people,o=sevenSeas") + .setSecureSettings(secureSettings(PoolingSessionFactorySettings.SECURE_BIND_PASSWORD, identifier, PASSWORD)) + .put(getFullSettingKey(identifier, SearchGroupsResolverSettings.BASE_DN), groupSearchBase) + .put(getFullSettingKey(identifier, SearchGroupsResolverSettings.SCOPE), LdapSearchScope.SUB_TREE) + .put(getFullSettingKey(identifier, VERIFICATION_MODE_SETTING_REALM), SslVerificationMode.CERTIFICATE) + .put(getFullSettingKey(identifier, RealmSettings.ORDER_SETTING), 0) + .build(); final RealmConfig config = getRealmConfig(identifier, settings); SessionFactory sessionFactory = LdapRealm.sessionFactory(config, new SSLService(config.env()), threadPool); try { @@ -359,61 +364,72 @@ public void testLdapRealmSelectsLdapUserSearchSessionFactory() throws Exception } public void testLdapRealmThrowsExceptionForUserTemplateAndSearchSettings() throws Exception { - final RealmConfig.RealmIdentifier identifier - = new RealmConfig.RealmIdentifier(LdapRealmSettings.LDAP_TYPE, "test-ldap-realm-user-search"); + final RealmConfig.RealmIdentifier identifier = new RealmConfig.RealmIdentifier( + LdapRealmSettings.LDAP_TYPE, + "test-ldap-realm-user-search" + ); Settings settings = Settings.builder() - .put(defaultGlobalSettings) - .putList(getFullSettingKey(identifier, URLS_SETTING), ldapUrls()) - .putList(getFullSettingKey(identifier.getName(), LdapSessionFactorySettings.USER_DN_TEMPLATES_SETTING), "cn=foo") - .put(getFullSettingKey(identifier.getName(), LdapUserSearchSessionFactorySettings.SEARCH_BASE_DN), "cn=bar") - .put(getFullSettingKey(identifier, SearchGroupsResolverSettings.BASE_DN), "") - .put(getFullSettingKey(identifier, SearchGroupsResolverSettings.SCOPE), LdapSearchScope.SUB_TREE) - .put(getFullSettingKey(identifier, VERIFICATION_MODE_SETTING_REALM), SslVerificationMode.CERTIFICATE) - .put(getFullSettingKey(identifier, RealmSettings.ORDER_SETTING), 0) - .build(); + .put(defaultGlobalSettings) + .putList(getFullSettingKey(identifier, URLS_SETTING), ldapUrls()) + .putList(getFullSettingKey(identifier.getName(), LdapSessionFactorySettings.USER_DN_TEMPLATES_SETTING), "cn=foo") + .put(getFullSettingKey(identifier.getName(), LdapUserSearchSessionFactorySettings.SEARCH_BASE_DN), "cn=bar") + .put(getFullSettingKey(identifier, SearchGroupsResolverSettings.BASE_DN), "") + .put(getFullSettingKey(identifier, SearchGroupsResolverSettings.SCOPE), LdapSearchScope.SUB_TREE) + .put(getFullSettingKey(identifier, VERIFICATION_MODE_SETTING_REALM), SslVerificationMode.CERTIFICATE) + .put(getFullSettingKey(identifier, RealmSettings.ORDER_SETTING), 0) + .build(); RealmConfig config = getRealmConfig(identifier, settings); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> LdapRealm.sessionFactory(config, null, threadPool)); - assertThat(e.getMessage(), - containsString("settings were found for both" + - " user search [xpack.security.authc.realms.ldap.test-ldap-realm-user-search.user_search.base_dn] and" + - " user template [xpack.security.authc.realms.ldap.test-ldap-realm-user-search.user_dn_templates]")); + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> LdapRealm.sessionFactory(config, null, threadPool)); + assertThat( + e.getMessage(), + containsString( + "settings were found for both" + + " user search [xpack.security.authc.realms.ldap.test-ldap-realm-user-search.user_search.base_dn] and" + + " user template [xpack.security.authc.realms.ldap.test-ldap-realm-user-search.user_dn_templates]" + ) + ); } public void testLdapRealmThrowsExceptionWhenNeitherUserTemplateNorSearchSettingsProvided() throws Exception { - final RealmConfig.RealmIdentifier identifier - = new RealmConfig.RealmIdentifier(LdapRealmSettings.LDAP_TYPE, "test-ldap-realm-user-search"); + final RealmConfig.RealmIdentifier identifier = new RealmConfig.RealmIdentifier( + LdapRealmSettings.LDAP_TYPE, + "test-ldap-realm-user-search" + ); Settings settings = Settings.builder() - .put(defaultGlobalSettings) - .putList(getFullSettingKey(identifier, URLS_SETTING), ldapUrls()) - .put(getFullSettingKey(identifier, SearchGroupsResolverSettings.BASE_DN), "") - .put(getFullSettingKey(identifier, SearchGroupsResolverSettings.SCOPE), LdapSearchScope.SUB_TREE) - .put(getFullSettingKey(identifier, VERIFICATION_MODE_SETTING_REALM), SslVerificationMode.CERTIFICATE) - .put(getFullSettingKey(identifier, RealmSettings.ORDER_SETTING), 0) - .build(); + .put(defaultGlobalSettings) + .putList(getFullSettingKey(identifier, URLS_SETTING), ldapUrls()) + .put(getFullSettingKey(identifier, SearchGroupsResolverSettings.BASE_DN), "") + .put(getFullSettingKey(identifier, SearchGroupsResolverSettings.SCOPE), LdapSearchScope.SUB_TREE) + .put(getFullSettingKey(identifier, VERIFICATION_MODE_SETTING_REALM), SslVerificationMode.CERTIFICATE) + .put(getFullSettingKey(identifier, RealmSettings.ORDER_SETTING), 0) + .build(); RealmConfig config = getRealmConfig(identifier, settings); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> LdapRealm.sessionFactory(config, null, threadPool)); - assertThat(e.getMessage(), - containsString("settings were not found for either" + - " user search [xpack.security.authc.realms.ldap.test-ldap-realm-user-search.user_search.base_dn] or" + - " user template [xpack.security.authc.realms.ldap.test-ldap-realm-user-search.user_dn_templates]")); + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> LdapRealm.sessionFactory(config, null, threadPool)); + assertThat( + e.getMessage(), + containsString( + "settings were not found for either" + + " user search [xpack.security.authc.realms.ldap.test-ldap-realm-user-search.user_search.base_dn] or" + + " user template [xpack.security.authc.realms.ldap.test-ldap-realm-user-search.user_dn_templates]" + ) + ); } public void testLdapRealmMapsUserDNToRole() throws Exception { String groupSearchBase = "o=sevenSeas"; String userTemplate = VALID_USER_TEMPLATE; Settings settings = Settings.builder() - .put(defaultGlobalSettings) - .put(buildLdapSettings(ldapUrls(), userTemplate, groupSearchBase, LdapSearchScope.SUB_TREE)) - .put(getFullSettingKey(REALM_IDENTIFIER, DnRoleMapperSettings.ROLE_MAPPING_FILE_SETTING), - getDataPath("/org/elasticsearch/xpack/security/authc/support/role_mapping.yml")) - .build(); + .put(defaultGlobalSettings) + .put(buildLdapSettings(ldapUrls(), userTemplate, groupSearchBase, LdapSearchScope.SUB_TREE)) + .put( + getFullSettingKey(REALM_IDENTIFIER, DnRoleMapperSettings.ROLE_MAPPING_FILE_SETTING), + getDataPath("/org/elasticsearch/xpack/security/authc/support/role_mapping.yml") + ) + .build(); RealmConfig config = getRealmConfig(REALM_IDENTIFIER, settings); LdapSessionFactory ldapFactory = new LdapSessionFactory(config, sslService, threadPool); - LdapRealm ldap = new LdapRealm(config, ldapFactory, - new DnRoleMapper(config, resourceWatcherService), threadPool); + LdapRealm ldap = new LdapRealm(config, ldapFactory, new DnRoleMapper(config, resourceWatcherService), threadPool); ldap.initialize(Collections.singleton(ldap), licenseState); PlainActionFuture> future = new PlainActionFuture<>(); @@ -434,11 +450,16 @@ public void testLdapRealmWithTemplatedRoleMapping() throws Exception { String groupSearchBase = "o=sevenSeas"; String userTemplate = VALID_USER_TEMPLATE; Settings settings = Settings.builder() - .put(defaultGlobalSettings) - .put(buildLdapSettings(ldapUrls(), userTemplate, groupSearchBase, LdapSearchScope.SUB_TREE)) - .put(getFullSettingKey(REALM_IDENTIFIER.getName(), - LdapMetadataResolverSettings.ADDITIONAL_METADATA_SETTING.apply(LdapRealmSettings.LDAP_TYPE)), "uid") - .build(); + .put(defaultGlobalSettings) + .put(buildLdapSettings(ldapUrls(), userTemplate, groupSearchBase, LdapSearchScope.SUB_TREE)) + .put( + getFullSettingKey( + REALM_IDENTIFIER.getName(), + LdapMetadataResolverSettings.ADDITIONAL_METADATA_SETTING.apply(LdapRealmSettings.LDAP_TYPE) + ), + "uid" + ) + .build(); RealmConfig config = getRealmConfig(REALM_IDENTIFIER, settings); SecurityIndexManager mockSecurityIndex = mock(SecurityIndexManager.class); @@ -448,41 +469,62 @@ public void testLdapRealmWithTemplatedRoleMapping() throws Exception { Client mockClient = mock(Client.class); when(mockClient.threadPool()).thenReturn(threadPool); - final ScriptService scriptService = new ScriptService(defaultGlobalSettings, - Collections.singletonMap(MustacheScriptEngine.NAME, new MustacheScriptEngine()), ScriptModule.CORE_CONTEXTS); - NativeRoleMappingStore roleMapper = new NativeRoleMappingStore(defaultGlobalSettings, mockClient, mockSecurityIndex, - scriptService) { + final ScriptService scriptService = new ScriptService( + defaultGlobalSettings, + Collections.singletonMap(MustacheScriptEngine.NAME, new MustacheScriptEngine()), + ScriptModule.CORE_CONTEXTS + ); + NativeRoleMappingStore roleMapper = new NativeRoleMappingStore( + defaultGlobalSettings, + mockClient, + mockSecurityIndex, + scriptService + ) { @Override protected void loadMappings(ActionListener> listener) { listener.onResponse( Arrays.asList( - this.buildMapping("m1", new BytesArray("{" + - "\"role_templates\":[{\"template\":{\"source\":\"_user_{{metadata.uid}}\"}}]," + - "\"enabled\":true," + - "\"rules\":{ \"any\":[" + - " { \"field\":{\"realm.name\":\"ldap1\"}}," + - " { \"field\":{\"realm.name\":\"ldap2\"}}" + - "]}}")), - this.buildMapping("m2", new BytesArray("{" + - "\"roles\":[\"should_not_happen\"]," + - "\"enabled\":true," + - "\"rules\":{ \"all\":[" + - " { \"field\":{\"realm.name\":\"ldap1\"}}," + - " { \"field\":{\"realm.name\":\"ldap2\"}}" + - "]}}")), - this.buildMapping("m3", new BytesArray("{" + - "\"roles\":[\"sales_admin\"]," + - "\"enabled\":true," + - "\"rules\":" + - " { \"field\":{\"dn\":\"*,ou=people,o=sevenSeas\"}}" + - "}")) + this.buildMapping( + "m1", + new BytesArray( + "{" + + "\"role_templates\":[{\"template\":{\"source\":\"_user_{{metadata.uid}}\"}}]," + + "\"enabled\":true," + + "\"rules\":{ \"any\":[" + + " { \"field\":{\"realm.name\":\"ldap1\"}}," + + " { \"field\":{\"realm.name\":\"ldap2\"}}" + + "]}}" + ) + ), + this.buildMapping( + "m2", + new BytesArray( + "{" + + "\"roles\":[\"should_not_happen\"]," + + "\"enabled\":true," + + "\"rules\":{ \"all\":[" + + " { \"field\":{\"realm.name\":\"ldap1\"}}," + + " { \"field\":{\"realm.name\":\"ldap2\"}}" + + "]}}" + ) + ), + this.buildMapping( + "m3", + new BytesArray( + "{" + + "\"roles\":[\"sales_admin\"]," + + "\"enabled\":true," + + "\"rules\":" + + " { \"field\":{\"dn\":\"*,ou=people,o=sevenSeas\"}}" + + "}" + ) + ) ) ); } }; LdapSessionFactory ldapFactory = new LdapSessionFactory(config, sslService, threadPool); - LdapRealm ldap = new LdapRealm(config, ldapFactory, - roleMapper, threadPool); + LdapRealm ldap = new LdapRealm(config, ldapFactory, roleMapper, threadPool); ldap.initialize(Collections.singleton(ldap), licenseState); PlainActionFuture> future = new PlainActionFuture<>(); @@ -507,13 +549,12 @@ public void testLdapConnectionFailureIsTreatedAsAuthenticationFailure() throws E String userTemplate = VALID_USER_TEMPLATE; Settings settings = Settings.builder() .put(defaultGlobalSettings) - .put(buildLdapSettings(new String[]{url.toString()}, userTemplate, groupSearchBase, LdapSearchScope.SUB_TREE)) + .put(buildLdapSettings(new String[] { url.toString() }, userTemplate, groupSearchBase, LdapSearchScope.SUB_TREE)) .put(getFullSettingKey(REALM_IDENTIFIER, RealmSettings.ORDER_SETTING), 0) .build(); RealmConfig config = getRealmConfig(REALM_IDENTIFIER, settings); LdapSessionFactory ldapFactory = new LdapSessionFactory(config, sslService, threadPool); - LdapRealm ldap = new LdapRealm(config, ldapFactory, buildGroupAsRoleMapper(resourceWatcherService), - threadPool); + LdapRealm ldap = new LdapRealm(config, ldapFactory, buildGroupAsRoleMapper(resourceWatcherService), threadPool); ldap.initialize(Collections.singleton(ldap), licenseState); PlainActionFuture> future = new PlainActionFuture<>(); @@ -530,15 +571,14 @@ public void testUsageStats() throws Exception { final RealmConfig.RealmIdentifier identifier = new RealmConfig.RealmIdentifier(LdapRealmSettings.LDAP_TYPE, "ldap-realm"); String groupSearchBase = "o=sevenSeas"; Settings.Builder settings = Settings.builder() - .put(defaultGlobalSettings) - .putList(getFullSettingKey(identifier, URLS_SETTING), ldapUrls()) - .put(getFullSettingKey(identifier, PoolingSessionFactorySettings.BIND_DN), - "cn=Thomas Masterman Hardy,ou=people,o=sevenSeas") - .put(getFullSettingKey(identifier, PoolingSessionFactorySettings.LEGACY_BIND_PASSWORD), PASSWORD) - .put(getFullSettingKey(identifier, SearchGroupsResolverSettings.BASE_DN), groupSearchBase) - .put(getFullSettingKey(identifier, SearchGroupsResolverSettings.SCOPE), LdapSearchScope.SUB_TREE) - .put(getFullSettingKey(identifier.getName(), LdapSessionFactorySettings.USER_DN_TEMPLATES_SETTING), "--") - .put(getFullSettingKey(identifier, VERIFICATION_MODE_SETTING_REALM), SslVerificationMode.CERTIFICATE); + .put(defaultGlobalSettings) + .putList(getFullSettingKey(identifier, URLS_SETTING), ldapUrls()) + .put(getFullSettingKey(identifier, PoolingSessionFactorySettings.BIND_DN), "cn=Thomas Masterman Hardy,ou=people,o=sevenSeas") + .put(getFullSettingKey(identifier, PoolingSessionFactorySettings.LEGACY_BIND_PASSWORD), PASSWORD) + .put(getFullSettingKey(identifier, SearchGroupsResolverSettings.BASE_DN), groupSearchBase) + .put(getFullSettingKey(identifier, SearchGroupsResolverSettings.SCOPE), LdapSearchScope.SUB_TREE) + .put(getFullSettingKey(identifier.getName(), LdapSessionFactorySettings.USER_DN_TEMPLATES_SETTING), "--") + .put(getFullSettingKey(identifier, VERIFICATION_MODE_SETTING_REALM), SslVerificationMode.CERTIFICATE); int order = randomIntBetween(0, 10); settings.put(getFullSettingKey(identifier, RealmSettings.ORDER_SETTING), order); @@ -565,8 +605,11 @@ public void testUsageStats() throws Exception { assertThat(stats, hasEntry("user_search", userSearch)); } - private SecureSettings secureSettings(Function> settingFactory, - RealmConfig.RealmIdentifier identifier, String value) { + private SecureSettings secureSettings( + Function> settingFactory, + RealmConfig.RealmIdentifier identifier, + String value + ) { final MockSecureSettings secureSettings = new MockSecureSettings(); secureSettings.setString(getFullSettingKey(identifier, settingFactory), value); return secureSettings; diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/LdapSessionFactoryTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/LdapSessionFactoryTests.java index 529dba07bbe08..17a8e5a84744d 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/LdapSessionFactoryTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/LdapSessionFactoryTests.java @@ -41,6 +41,7 @@ import java.security.GeneralSecurityException; import java.util.List; import java.util.concurrent.ExecutionException; + import javax.net.ssl.SSLException; import static org.elasticsearch.test.TestMatchers.throwableWithMessage; @@ -87,28 +88,41 @@ public void testBindWithReadTimeout() throws Exception { if (listenAddress == null) { listenAddress = InetAddress.getLoopbackAddress(); } - String ldapUrl = new LDAPURL(protocol, NetworkAddress.format(listenAddress), ldapServer.getListenPort(protocol), - null, null, null, null).toString(); + String ldapUrl = new LDAPURL( + protocol, + NetworkAddress.format(listenAddress), + ldapServer.getListenPort(protocol), + null, + null, + null, + null + ).toString(); String groupSearchBase = "o=sevenSeas"; String userTemplates = "cn={0},ou=people,o=sevenSeas"; Settings settings = Settings.builder() - .put(globalSettings) - .put(buildLdapSettings(ldapUrl, userTemplates, groupSearchBase, LdapSearchScope.SUB_TREE)) - .put(RealmSettings.getFullSettingKey(REALM_IDENTIFIER, SessionFactorySettings.TIMEOUT_RESPONSE_SETTING), "1ms") - .put("path.home", createTempDir()) - .build(); - - RealmConfig config = new RealmConfig(REALM_IDENTIFIER, settings, - TestEnvironment.newEnvironment(globalSettings), new ThreadContext(globalSettings)); + .put(globalSettings) + .put(buildLdapSettings(ldapUrl, userTemplates, groupSearchBase, LdapSearchScope.SUB_TREE)) + .put(RealmSettings.getFullSettingKey(REALM_IDENTIFIER, SessionFactorySettings.TIMEOUT_RESPONSE_SETTING), "1ms") + .put("path.home", createTempDir()) + .build(); + + RealmConfig config = new RealmConfig( + REALM_IDENTIFIER, + settings, + TestEnvironment.newEnvironment(globalSettings), + new ThreadContext(globalSettings) + ); LdapSessionFactory sessionFactory = new LdapSessionFactory(config, sslService, threadPool); String user = "Horatio Hornblower"; SecureString userPass = new SecureString("pass"); ldapServer.setProcessingDelayMillis(500L); try { - UncategorizedExecutionException e = - expectThrows(UncategorizedExecutionException.class, () -> session(sessionFactory, user, userPass)); + UncategorizedExecutionException e = expectThrows( + UncategorizedExecutionException.class, + () -> session(sessionFactory, user, userPass) + ); assertThat(e.getCause(), instanceOf(ExecutionException.class)); assertThat(e.getCause().getCause(), instanceOf(LDAPException.class)); assertThat(e.getCause().getCause().getMessage(), containsString("A client-side timeout was encountered while waiting ")); @@ -119,17 +133,21 @@ public void testBindWithReadTimeout() throws Exception { public void testBindWithTemplates() throws Exception { String groupSearchBase = "o=sevenSeas"; - String[] userTemplates = new String[]{ - "cn={0},ou=something,ou=obviously,ou=incorrect,o=sevenSeas", - "wrongname={0},ou=people,o=sevenSeas", - "cn={0},ou=people,o=sevenSeas", //this last one should work + String[] userTemplates = new String[] { + "cn={0},ou=something,ou=obviously,ou=incorrect,o=sevenSeas", + "wrongname={0},ou=people,o=sevenSeas", + "cn={0},ou=people,o=sevenSeas", // this last one should work }; Settings settings = Settings.builder() .put(globalSettings) .put(buildLdapSettings(ldapUrls(), userTemplates, groupSearchBase, LdapSearchScope.SUB_TREE)) .build(); - RealmConfig config = new RealmConfig(REALM_IDENTIFIER, settings, - TestEnvironment.newEnvironment(globalSettings), new ThreadContext(globalSettings)); + RealmConfig config = new RealmConfig( + REALM_IDENTIFIER, + settings, + TestEnvironment.newEnvironment(globalSettings), + new ThreadContext(globalSettings) + ); LdapSessionFactory sessionFactory = new LdapSessionFactory(config, sslService, threadPool); @@ -146,17 +164,21 @@ public void testBindWithTemplates() throws Exception { public void testBindWithBogusTemplates() throws Exception { String groupSearchBase = "o=sevenSeas"; - String[] userTemplates = new String[]{ - "cn={0},ou=something,ou=obviously,ou=incorrect,o=sevenSeas", - "wrongname={0},ou=people,o=sevenSeas", - "asdf={0},ou=people,o=sevenSeas", //none of these should work + String[] userTemplates = new String[] { + "cn={0},ou=something,ou=obviously,ou=incorrect,o=sevenSeas", + "wrongname={0},ou=people,o=sevenSeas", + "asdf={0},ou=people,o=sevenSeas", // none of these should work }; Settings settings = Settings.builder() .put(globalSettings) .put(buildLdapSettings(ldapUrls(), userTemplates, groupSearchBase, LdapSearchScope.SUB_TREE)) .build(); - RealmConfig config = new RealmConfig(REALM_IDENTIFIER, settings, - TestEnvironment.newEnvironment(globalSettings), new ThreadContext(globalSettings)); + RealmConfig config = new RealmConfig( + REALM_IDENTIFIER, + settings, + TestEnvironment.newEnvironment(globalSettings), + new ThreadContext(globalSettings) + ); LdapSessionFactory ldapFac = new LdapSessionFactory(config, sslService, threadPool); @@ -177,8 +199,12 @@ public void testGroupLookupSubtree() throws Exception { .put(globalSettings) .put(buildLdapSettings(ldapUrls(), userTemplate, groupSearchBase, LdapSearchScope.SUB_TREE)) .build(); - RealmConfig config = new RealmConfig(REALM_IDENTIFIER, settings, - TestEnvironment.newEnvironment(globalSettings), new ThreadContext(globalSettings)); + RealmConfig config = new RealmConfig( + REALM_IDENTIFIER, + settings, + TestEnvironment.newEnvironment(globalSettings), + new ThreadContext(globalSettings) + ); LdapSessionFactory ldapFac = new LdapSessionFactory(config, sslService, threadPool); @@ -200,8 +226,12 @@ public void testGroupLookupOneLevel() throws Exception { .put(globalSettings) .put(buildLdapSettings(ldapUrls(), userTemplate, groupSearchBase, LdapSearchScope.ONE_LEVEL)) .build(); - RealmConfig config = new RealmConfig(REALM_IDENTIFIER, settings, - TestEnvironment.newEnvironment(globalSettings), new ThreadContext(globalSettings)); + RealmConfig config = new RealmConfig( + REALM_IDENTIFIER, + settings, + TestEnvironment.newEnvironment(globalSettings), + new ThreadContext(globalSettings) + ); LdapSessionFactory ldapFac = new LdapSessionFactory(config, sslService, threadPool); @@ -222,8 +252,12 @@ public void testGroupLookupBase() throws Exception { .put(globalSettings) .put(buildLdapSettings(ldapUrls(), userTemplate, groupSearchBase, LdapSearchScope.BASE)) .build(); - RealmConfig config = new RealmConfig(REALM_IDENTIFIER, settings, - TestEnvironment.newEnvironment(globalSettings), new ThreadContext(globalSettings)); + RealmConfig config = new RealmConfig( + REALM_IDENTIFIER, + settings, + TestEnvironment.newEnvironment(globalSettings), + new ThreadContext(globalSettings) + ); LdapSessionFactory ldapFac = new LdapSessionFactory(config, sslService, threadPool); @@ -257,8 +291,15 @@ public void testSslTrustIsReloaded() throws Exception { if (listenAddress == null) { listenAddress = InetAddress.getLoopbackAddress(); } - String ldapUrl = new LDAPURL("ldaps", NetworkAddress.format(listenAddress), ldapServer.getListenPort("ldaps"), - null, null, null, null).toString(); + String ldapUrl = new LDAPURL( + "ldaps", + NetworkAddress.format(listenAddress), + ldapServer.getListenPort("ldaps"), + null, + null, + null, + null + ).toString(); String groupSearchBase = "o=sevenSeas"; String userTemplates = "cn={0},ou=people,o=sevenSeas"; @@ -271,20 +312,20 @@ public void testSslTrustIsReloaded() throws Exception { final Path fakeCa = getDataPath("/org/elasticsearch/xpack/security/authc/ldap/support/smb_ca.crt"); final Environment environment = TestEnvironment.newEnvironment(settings); - RealmConfig config = new RealmConfig(REALM_IDENTIFIER, settings, - environment, new ThreadContext(settings)); + RealmConfig config = new RealmConfig(REALM_IDENTIFIER, settings, environment, new ThreadContext(settings)); LdapSessionFactory sessionFactory = new LdapSessionFactory(config, sslService, threadPool); String user = "Horatio Hornblower"; SecureString userPass = new SecureString("pass"); try (ResourceWatcherService resourceWatcher = new ResourceWatcherService(settings, threadPool)) { - new SSLConfigurationReloader(resourceWatcher, SSLService.getSSLConfigurations(environment).values()) - .setSSLService(sslService); + new SSLConfigurationReloader(resourceWatcher, SSLService.getSSLConfigurations(environment).values()).setSSLService(sslService); Files.copy(fakeCa, ldapCaPath, StandardCopyOption.REPLACE_EXISTING); resourceWatcher.notifyNow(ResourceWatcherService.Frequency.HIGH); - UncategorizedExecutionException e = - expectThrows(UncategorizedExecutionException.class, () -> session(sessionFactory, user, userPass)); + UncategorizedExecutionException e = expectThrows( + UncategorizedExecutionException.class, + () -> session(sessionFactory, user, userPass) + ); final Throwable immediateCause = e.getCause(); assertThat(immediateCause, instanceOf(ExecutionException.class)); final Throwable sdkCause = immediateCause.getCause(); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/LdapTestUtils.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/LdapTestUtils.java index 607fda04b44f3..c7bdea0b611b3 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/LdapTestUtils.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/LdapTestUtils.java @@ -52,7 +52,15 @@ public static LDAPConnection openConnection(String url, String bindDN, String bi options.setResponseTimeoutMillis(SessionFactorySettings.TIMEOUT_DEFAULT.millis()); final SslConfiguration sslConfiguration = sslService.getSSLConfiguration("xpack.security.authc.realms.ldap.foo.ssl"); - return LdapUtils.privilegedConnect(() -> new LDAPConnection(sslService.sslSocketFactory(sslConfiguration), options, - ldapurl.getHost(), ldapurl.getPort(), bindDN, bindPassword)); + return LdapUtils.privilegedConnect( + () -> new LDAPConnection( + sslService.sslSocketFactory(sslConfiguration), + options, + ldapurl.getHost(), + ldapurl.getPort(), + bindDN, + bindPassword + ) + ); } } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/LdapUserSearchSessionFactoryTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/LdapUserSearchSessionFactoryTests.java index b1ef66821436e..3ce9d25811484 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/LdapUserSearchSessionFactoryTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/LdapUserSearchSessionFactoryTests.java @@ -14,14 +14,15 @@ import com.unboundid.ldap.sdk.LDAPURL; import com.unboundid.ldap.sdk.SimpleBindRequest; import com.unboundid.ldap.sdk.SingleServerSet; + import org.elasticsearch.action.support.PlainActionFuture; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.MockSecureSettings; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.threadpool.TestThreadPool; import org.elasticsearch.threadpool.ThreadPool; @@ -85,12 +86,11 @@ private MockSecureSettings newSecureSettings(String key, String value) { public void testSupportsUnauthenticatedSessions() throws Exception { final boolean useAttribute = randomBoolean(); Settings.Builder builder = Settings.builder() - .put(globalSettings) - .put(buildLdapSettings(ldapUrls(), Strings.EMPTY_ARRAY, "", LdapSearchScope.SUB_TREE)) - .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.SEARCH_BASE_DN), "") - .put(getFullSettingKey(REALM_IDENTIFIER, PoolingSessionFactorySettings.BIND_DN), - "cn=Horatio Hornblower,ou=people,o=sevenSeas") - .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.POOL_ENABLED), randomBoolean()); + .put(globalSettings) + .put(buildLdapSettings(ldapUrls(), Strings.EMPTY_ARRAY, "", LdapSearchScope.SUB_TREE)) + .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.SEARCH_BASE_DN), "") + .put(getFullSettingKey(REALM_IDENTIFIER, PoolingSessionFactorySettings.BIND_DN), "cn=Horatio Hornblower,ou=people,o=sevenSeas") + .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.POOL_ENABLED), randomBoolean()); final boolean useLegacyBindPassword = configureBindPassword(builder); if (useAttribute) { builder.put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.SEARCH_ATTRIBUTE), "cn"); @@ -120,12 +120,11 @@ public void testUserSearchSubTree() throws Exception { final boolean useAttribute = randomBoolean(); Settings.Builder builder = Settings.builder() - .put(globalSettings) - .put(buildLdapSettings(ldapUrls(), Strings.EMPTY_ARRAY, groupSearchBase, LdapSearchScope.SUB_TREE)) - .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.SEARCH_BASE_DN), userSearchBase) - .put(getFullSettingKey(REALM_IDENTIFIER, PoolingSessionFactorySettings.BIND_DN), - "cn=Horatio Hornblower,ou=people,o=sevenSeas") - .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.POOL_ENABLED), randomBoolean()); + .put(globalSettings) + .put(buildLdapSettings(ldapUrls(), Strings.EMPTY_ARRAY, groupSearchBase, LdapSearchScope.SUB_TREE)) + .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.SEARCH_BASE_DN), userSearchBase) + .put(getFullSettingKey(REALM_IDENTIFIER, PoolingSessionFactorySettings.BIND_DN), "cn=Horatio Hornblower,ou=people,o=sevenSeas") + .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.POOL_ENABLED), randomBoolean()); final boolean useLegacyBindPassword = configureBindPassword(builder); if (useAttribute) { builder.put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.SEARCH_ATTRIBUTE), "cn"); @@ -147,7 +146,7 @@ public void testUserSearchSubTree() throws Exception { assertThat(dn, containsString(user)); } - //lookup + // lookup try (LdapSession ldap = unauthenticatedSession(sessionFactory, user)) { assertConnectionValid(ldap.getConnection(), sessionFactory.bindCredentials); String dn = ldap.userDn(); @@ -166,13 +165,12 @@ public void testUserSearchBaseScopeFailsWithWrongBaseDN() throws Exception { final boolean useAttribute = randomBoolean(); Settings.Builder builder = Settings.builder() - .put(globalSettings) - .put(buildLdapSettings(ldapUrls(), Strings.EMPTY_ARRAY, groupSearchBase, LdapSearchScope.SUB_TREE)) - .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.SEARCH_BASE_DN), userSearchBase) - .put(getFullSettingKey(REALM_IDENTIFIER, PoolingSessionFactorySettings.BIND_DN), - "cn=Horatio Hornblower,ou=people,o=sevenSeas") - .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.SEARCH_SCOPE), LdapSearchScope.BASE) - .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.POOL_ENABLED), randomBoolean()); + .put(globalSettings) + .put(buildLdapSettings(ldapUrls(), Strings.EMPTY_ARRAY, groupSearchBase, LdapSearchScope.SUB_TREE)) + .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.SEARCH_BASE_DN), userSearchBase) + .put(getFullSettingKey(REALM_IDENTIFIER, PoolingSessionFactorySettings.BIND_DN), "cn=Horatio Hornblower,ou=people,o=sevenSeas") + .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.SEARCH_SCOPE), LdapSearchScope.BASE) + .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.POOL_ENABLED), randomBoolean()); final boolean useLegacyBindPassword = configureBindPassword(builder); if (useAttribute) { builder.put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.SEARCH_ATTRIBUTE), "cn"); @@ -201,13 +199,12 @@ public void testUserSearchBaseScopePassesWithCorrectBaseDN() throws Exception { String userSearchBase = "cn=William Bush,ou=people,o=sevenSeas"; Settings.Builder builder = Settings.builder() - .put(globalSettings) - .put(buildLdapSettings(ldapUrls(), Strings.EMPTY_ARRAY, groupSearchBase, LdapSearchScope.SUB_TREE)) - .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.SEARCH_BASE_DN), userSearchBase) - .put(getFullSettingKey(REALM_IDENTIFIER, PoolingSessionFactorySettings.BIND_DN), - "cn=Horatio Hornblower,ou=people,o=sevenSeas") - .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.SEARCH_SCOPE), LdapSearchScope.BASE) - .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.POOL_ENABLED), randomBoolean()); + .put(globalSettings) + .put(buildLdapSettings(ldapUrls(), Strings.EMPTY_ARRAY, groupSearchBase, LdapSearchScope.SUB_TREE)) + .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.SEARCH_BASE_DN), userSearchBase) + .put(getFullSettingKey(REALM_IDENTIFIER, PoolingSessionFactorySettings.BIND_DN), "cn=Horatio Hornblower,ou=people,o=sevenSeas") + .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.SEARCH_SCOPE), LdapSearchScope.BASE) + .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.POOL_ENABLED), randomBoolean()); final boolean useLegacyBindPassword = configureBindPassword(builder); final boolean useAttribute = randomBoolean(); if (useAttribute) { @@ -230,7 +227,7 @@ public void testUserSearchBaseScopePassesWithCorrectBaseDN() throws Exception { assertThat(dn, containsString(user)); } - //lookup + // lookup try (LdapSession ldap = unauthenticatedSession(sessionFactory, user)) { assertConnectionValid(ldap.getConnection(), sessionFactory.bindCredentials); String dn = ldap.userDn(); @@ -248,14 +245,15 @@ public void testUserSearchOneLevelScopeFailsWithWrongBaseDN() throws Exception { String userSearchBase = "o=sevenSeas"; Settings.Builder builder = Settings.builder() - .put(globalSettings) - .put(buildLdapSettings(ldapUrls(), Strings.EMPTY_ARRAY, groupSearchBase, LdapSearchScope.SUB_TREE)) - .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.SEARCH_BASE_DN), userSearchBase) - .put(getFullSettingKey(REALM_IDENTIFIER, PoolingSessionFactorySettings.BIND_DN), - "cn=Horatio Hornblower,ou=people,o=sevenSeas") - .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.SEARCH_SCOPE), - LdapSearchScope.ONE_LEVEL) - .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.POOL_ENABLED), randomBoolean()); + .put(globalSettings) + .put(buildLdapSettings(ldapUrls(), Strings.EMPTY_ARRAY, groupSearchBase, LdapSearchScope.SUB_TREE)) + .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.SEARCH_BASE_DN), userSearchBase) + .put(getFullSettingKey(REALM_IDENTIFIER, PoolingSessionFactorySettings.BIND_DN), "cn=Horatio Hornblower,ou=people,o=sevenSeas") + .put( + getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.SEARCH_SCOPE), + LdapSearchScope.ONE_LEVEL + ) + .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.POOL_ENABLED), randomBoolean()); final boolean useLegacyBindPassword = configureBindPassword(builder); final boolean useAttribute = randomBoolean(); if (useAttribute) { @@ -285,14 +283,15 @@ public void testUserSearchOneLevelScopePassesWithCorrectBaseDN() throws Exceptio String userSearchBase = "ou=people,o=sevenSeas"; Settings.Builder builder = Settings.builder() - .put(globalSettings) - .put(buildLdapSettings(ldapUrls(), Strings.EMPTY_ARRAY, groupSearchBase, LdapSearchScope.SUB_TREE)) - .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.SEARCH_BASE_DN), userSearchBase) - .put(getFullSettingKey(REALM_IDENTIFIER, PoolingSessionFactorySettings.BIND_DN), - "cn=Horatio Hornblower,ou=people,o=sevenSeas") - .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.SEARCH_SCOPE), - LdapSearchScope.ONE_LEVEL) - .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.POOL_ENABLED), randomBoolean()); + .put(globalSettings) + .put(buildLdapSettings(ldapUrls(), Strings.EMPTY_ARRAY, groupSearchBase, LdapSearchScope.SUB_TREE)) + .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.SEARCH_BASE_DN), userSearchBase) + .put(getFullSettingKey(REALM_IDENTIFIER, PoolingSessionFactorySettings.BIND_DN), "cn=Horatio Hornblower,ou=people,o=sevenSeas") + .put( + getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.SEARCH_SCOPE), + LdapSearchScope.ONE_LEVEL + ) + .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.POOL_ENABLED), randomBoolean()); final boolean useLegacyBindPassword = configureBindPassword(builder); final boolean useAttribute = randomBoolean(); if (useAttribute) { @@ -308,14 +307,14 @@ public void testUserSearchOneLevelScopePassesWithCorrectBaseDN() throws Exceptio SecureString userPass = new SecureString("pass"); try { - //auth + // auth try (LdapSession ldap = session(sessionFactory, user, userPass)) { assertConnectionValid(ldap.getConnection(), sessionFactory.bindCredentials); String dn = ldap.userDn(); assertThat(dn, containsString(user)); } - //lookup + // lookup try (LdapSession ldap = unauthenticatedSession(sessionFactory, user)) { assertConnectionValid(ldap.getConnection(), sessionFactory.bindCredentials); String dn = ldap.userDn(); @@ -333,12 +332,11 @@ public void testUserSearchWithBadAttributeFails() throws Exception { String userSearchBase = "o=sevenSeas"; Settings.Builder builder = Settings.builder() - .put(globalSettings) - .put(buildLdapSettings(ldapUrls(), Strings.EMPTY_ARRAY, groupSearchBase, LdapSearchScope.SUB_TREE)) - .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.SEARCH_BASE_DN), userSearchBase) - .put(getFullSettingKey(REALM_IDENTIFIER, PoolingSessionFactorySettings.BIND_DN), - "cn=Horatio Hornblower,ou=people,o=sevenSeas") - .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.POOL_ENABLED), randomBoolean()); + .put(globalSettings) + .put(buildLdapSettings(ldapUrls(), Strings.EMPTY_ARRAY, groupSearchBase, LdapSearchScope.SUB_TREE)) + .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.SEARCH_BASE_DN), userSearchBase) + .put(getFullSettingKey(REALM_IDENTIFIER, PoolingSessionFactorySettings.BIND_DN), "cn=Horatio Hornblower,ou=people,o=sevenSeas") + .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.POOL_ENABLED), randomBoolean()); final boolean useLegacyBindPassword = configureBindPassword(builder); final boolean useAttribute = randomBoolean(); if (useAttribute) { @@ -368,12 +366,11 @@ public void testUserSearchWithoutAttributePasses() throws Exception { String userSearchBase = "o=sevenSeas"; final Settings.Builder realmSettings = Settings.builder() - .put(globalSettings) - .put(buildLdapSettings(ldapUrls(), Strings.EMPTY_ARRAY, groupSearchBase, LdapSearchScope.SUB_TREE)) - .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.SEARCH_BASE_DN), userSearchBase) - .put(getFullSettingKey(REALM_IDENTIFIER, PoolingSessionFactorySettings.BIND_DN), - "cn=Horatio Hornblower,ou=people,o=sevenSeas") - .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.POOL_ENABLED), randomBoolean()); + .put(globalSettings) + .put(buildLdapSettings(ldapUrls(), Strings.EMPTY_ARRAY, groupSearchBase, LdapSearchScope.SUB_TREE)) + .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.SEARCH_BASE_DN), userSearchBase) + .put(getFullSettingKey(REALM_IDENTIFIER, PoolingSessionFactorySettings.BIND_DN), "cn=Horatio Hornblower,ou=people,o=sevenSeas") + .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.POOL_ENABLED), randomBoolean()); final boolean useLegacyBindPassword = configureBindPassword(realmSettings); RealmConfig config = getRealmConfig(realmSettings.build()); @@ -383,14 +380,14 @@ public void testUserSearchWithoutAttributePasses() throws Exception { SecureString userPass = new SecureString("pass"); try { - //auth + // auth try (LdapSession ldap = session(sessionFactory, user, userPass)) { assertConnectionValid(ldap.getConnection(), sessionFactory.bindCredentials); String dn = ldap.userDn(); assertThat(dn, containsString("William Bush")); } - //lookup + // lookup try (LdapSession ldap = unauthenticatedSession(sessionFactory, user)) { assertConnectionValid(ldap.getConnection(), sessionFactory.bindCredentials); String dn = ldap.userDn(); @@ -407,23 +404,27 @@ public void testConnectionPoolDefaultSettings() throws Exception { String groupSearchBase = "o=sevenSeas"; String userSearchBase = "o=sevenSeas"; final Settings.Builder realmSettings = Settings.builder() - .put(globalSettings) - .put(buildLdapSettings(ldapUrls(), Strings.EMPTY_ARRAY, groupSearchBase, LdapSearchScope.SUB_TREE)) - .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.SEARCH_BASE_DN), userSearchBase) - .put(getFullSettingKey(REALM_IDENTIFIER, PoolingSessionFactorySettings.BIND_DN), - "cn=Horatio Hornblower,ou=people,o=sevenSeas"); + .put(globalSettings) + .put(buildLdapSettings(ldapUrls(), Strings.EMPTY_ARRAY, groupSearchBase, LdapSearchScope.SUB_TREE)) + .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.SEARCH_BASE_DN), userSearchBase) + .put(getFullSettingKey(REALM_IDENTIFIER, PoolingSessionFactorySettings.BIND_DN), "cn=Horatio Hornblower,ou=people,o=sevenSeas"); configureBindPassword(realmSettings); RealmConfig config = getRealmConfig(realmSettings.build()); - LDAPConnectionPool connectionPool = LdapUserSearchSessionFactory.createConnectionPool(config, new SingleServerSet("localhost", - randomFrom(ldapServers).getListenPort()), TimeValue.timeValueSeconds(5), NoOpLogger.INSTANCE, - new SimpleBindRequest("cn=Horatio Hornblower,ou=people,o=sevenSeas", "pass"), - () -> "cn=Horatio Hornblower,ou=people,o=sevenSeas"); + LDAPConnectionPool connectionPool = LdapUserSearchSessionFactory.createConnectionPool( + config, + new SingleServerSet("localhost", randomFrom(ldapServers).getListenPort()), + TimeValue.timeValueSeconds(5), + NoOpLogger.INSTANCE, + new SimpleBindRequest("cn=Horatio Hornblower,ou=people,o=sevenSeas", "pass"), + () -> "cn=Horatio Hornblower,ou=people,o=sevenSeas" + ); try { - assertThat(connectionPool.getCurrentAvailableConnections(), - is(PoolingSessionFactorySettings.DEFAULT_CONNECTION_POOL_INITIAL_SIZE)); - assertThat(connectionPool.getMaximumAvailableConnections(), - is(PoolingSessionFactorySettings.DEFAULT_CONNECTION_POOL_SIZE)); + assertThat( + connectionPool.getCurrentAvailableConnections(), + is(PoolingSessionFactorySettings.DEFAULT_CONNECTION_POOL_INITIAL_SIZE) + ); + assertThat(connectionPool.getMaximumAvailableConnections(), is(PoolingSessionFactorySettings.DEFAULT_CONNECTION_POOL_SIZE)); assertEquals(connectionPool.getHealthCheck().getClass(), GetEntryLDAPConnectionPoolHealthCheck.class); GetEntryLDAPConnectionPoolHealthCheck healthCheck = (GetEntryLDAPConnectionPoolHealthCheck) connectionPool.getHealthCheck(); assertThat(healthCheck.getEntryDN(), is("cn=Horatio Hornblower,ou=people,o=sevenSeas")); @@ -437,21 +438,24 @@ public void testConnectionPoolSettings() throws Exception { String groupSearchBase = "o=sevenSeas"; String userSearchBase = "o=sevenSeas"; final Settings.Builder realmSettings = Settings.builder() - .put(globalSettings) - .put(buildLdapSettings(ldapUrls(), Strings.EMPTY_ARRAY, groupSearchBase, LdapSearchScope.SUB_TREE)) - .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.SEARCH_BASE_DN), userSearchBase) - .put(getFullSettingKey(REALM_IDENTIFIER, PoolingSessionFactorySettings.BIND_DN), - "cn=Horatio Hornblower,ou=people,o=sevenSeas") - .put(getFullSettingKey(REALM_IDENTIFIER, PoolingSessionFactorySettings.POOL_INITIAL_SIZE), 10) - .put(getFullSettingKey(REALM_IDENTIFIER, PoolingSessionFactorySettings.POOL_SIZE), 12) - .put(getFullSettingKey(REALM_IDENTIFIER, PoolingSessionFactorySettings.HEALTH_CHECK_ENABLED), false); + .put(globalSettings) + .put(buildLdapSettings(ldapUrls(), Strings.EMPTY_ARRAY, groupSearchBase, LdapSearchScope.SUB_TREE)) + .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.SEARCH_BASE_DN), userSearchBase) + .put(getFullSettingKey(REALM_IDENTIFIER, PoolingSessionFactorySettings.BIND_DN), "cn=Horatio Hornblower,ou=people,o=sevenSeas") + .put(getFullSettingKey(REALM_IDENTIFIER, PoolingSessionFactorySettings.POOL_INITIAL_SIZE), 10) + .put(getFullSettingKey(REALM_IDENTIFIER, PoolingSessionFactorySettings.POOL_SIZE), 12) + .put(getFullSettingKey(REALM_IDENTIFIER, PoolingSessionFactorySettings.HEALTH_CHECK_ENABLED), false); configureBindPassword(realmSettings); RealmConfig config = getRealmConfig(realmSettings.build()); - LDAPConnectionPool connectionPool = LdapUserSearchSessionFactory.createConnectionPool(config, new SingleServerSet("localhost", - randomFrom(ldapServers).getListenPort()), TimeValue.timeValueSeconds(5), NoOpLogger.INSTANCE, - new SimpleBindRequest("cn=Horatio Hornblower,ou=people,o=sevenSeas", "pass"), - () -> "cn=Horatio Hornblower,ou=people,o=sevenSeas"); + LDAPConnectionPool connectionPool = LdapUserSearchSessionFactory.createConnectionPool( + config, + new SingleServerSet("localhost", randomFrom(ldapServers).getListenPort()), + TimeValue.timeValueSeconds(5), + NoOpLogger.INSTANCE, + new SimpleBindRequest("cn=Horatio Hornblower,ou=people,o=sevenSeas", "pass"), + () -> "cn=Horatio Hornblower,ou=people,o=sevenSeas" + ); try { assertThat(connectionPool.getCurrentAvailableConnections(), is(10)); assertThat(connectionPool.getMaximumAvailableConnections(), is(12)); @@ -516,12 +520,16 @@ public void testEmptyBindDNReturnsAnonymousBindRequest() throws LDAPException { String groupSearchBase = "o=sevenSeas"; String userSearchBase = "o=sevenSeas"; final Settings.Builder realmSettings = Settings.builder() - .put(globalSettings) - .put(buildLdapSettings(ldapUrls(), Strings.EMPTY_ARRAY, groupSearchBase, LdapSearchScope.SUB_TREE)) - .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.SEARCH_BASE_DN), userSearchBase); + .put(globalSettings) + .put(buildLdapSettings(ldapUrls(), Strings.EMPTY_ARRAY, groupSearchBase, LdapSearchScope.SUB_TREE)) + .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.SEARCH_BASE_DN), userSearchBase); final boolean useLegacyBindPassword = configureBindPassword(realmSettings); - RealmConfig config = new RealmConfig(REALM_IDENTIFIER, realmSettings.build(), - TestEnvironment.newEnvironment(globalSettings), new ThreadContext(globalSettings)); + RealmConfig config = new RealmConfig( + REALM_IDENTIFIER, + realmSettings.build(), + TestEnvironment.newEnvironment(globalSettings), + new ThreadContext(globalSettings) + ); try (LdapUserSearchSessionFactory searchSessionFactory = getLdapUserSearchSessionFactory(config, sslService, threadPool)) { assertThat(searchSessionFactory.bindCredentials, notNullValue()); assertThat(searchSessionFactory.bindCredentials.getBindDN(), is(emptyString())); @@ -533,13 +541,17 @@ public void testThatBindRequestReturnsSimpleBindRequest() throws LDAPException { String groupSearchBase = "o=sevenSeas"; String userSearchBase = "o=sevenSeas"; final Settings.Builder realmSettings = Settings.builder() - .put(globalSettings) - .put(buildLdapSettings(ldapUrls(), Strings.EMPTY_ARRAY, groupSearchBase, LdapSearchScope.SUB_TREE)) - .put(getFullSettingKey(REALM_IDENTIFIER, PoolingSessionFactorySettings.BIND_DN), "cn=ironman") - .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.SEARCH_BASE_DN), userSearchBase); + .put(globalSettings) + .put(buildLdapSettings(ldapUrls(), Strings.EMPTY_ARRAY, groupSearchBase, LdapSearchScope.SUB_TREE)) + .put(getFullSettingKey(REALM_IDENTIFIER, PoolingSessionFactorySettings.BIND_DN), "cn=ironman") + .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.SEARCH_BASE_DN), userSearchBase); final boolean useLegacyBindPassword = configureBindPassword(realmSettings); - RealmConfig config = new RealmConfig(REALM_IDENTIFIER, realmSettings.build(), - TestEnvironment.newEnvironment(globalSettings), new ThreadContext(globalSettings)); + RealmConfig config = new RealmConfig( + REALM_IDENTIFIER, + realmSettings.build(), + TestEnvironment.newEnvironment(globalSettings), + new ThreadContext(globalSettings) + ); try (LdapUserSearchSessionFactory searchSessionFactory = getLdapUserSearchSessionFactory(config, sslService, threadPool)) { assertThat(searchSessionFactory.bindCredentials, notNullValue()); assertThat(searchSessionFactory.bindCredentials.getBindDN(), is("cn=ironman")); @@ -557,16 +569,15 @@ public void testThatConnectErrorIsNotThrownOnConstruction() throws Exception { inMemoryDirectoryServer.shutDown(true); final Settings.Builder ldapSettingsBuilder = Settings.builder() - .put(globalSettings) - .put(LdapTestCase.buildLdapSettings(new String[]{ldapUrl}, Strings.EMPTY_ARRAY, - groupSearchBase, LdapSearchScope.SUB_TREE)) - .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.SEARCH_BASE_DN), userSearchBase) - .put(getFullSettingKey(REALM_IDENTIFIER, PoolingSessionFactorySettings.BIND_DN), "ironman@ad.test.elasticsearch.com") - .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.SEARCH_ATTRIBUTE), "cn") - .put("timeout.tcp_connect", "500ms") - .put("type", "ldap") - .put("user_search.pool.health_check.enabled", false) - .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.POOL_ENABLED), randomBoolean()); + .put(globalSettings) + .put(LdapTestCase.buildLdapSettings(new String[] { ldapUrl }, Strings.EMPTY_ARRAY, groupSearchBase, LdapSearchScope.SUB_TREE)) + .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.SEARCH_BASE_DN), userSearchBase) + .put(getFullSettingKey(REALM_IDENTIFIER, PoolingSessionFactorySettings.BIND_DN), "ironman@ad.test.elasticsearch.com") + .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.SEARCH_ATTRIBUTE), "cn") + .put("timeout.tcp_connect", "500ms") + .put("type", "ldap") + .put("user_search.pool.health_check.enabled", false) + .put(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapUserSearchSessionFactorySettings.POOL_ENABLED), randomBoolean()); final boolean useLegacyBindPassword = configureBindPassword(ldapSettingsBuilder); RealmConfig config = getRealmConfig(ldapSettingsBuilder.build()); @@ -585,14 +596,15 @@ public void testThatConnectErrorIsNotThrownOnConstruction() throws Exception { private void assertDeprecationWarnings(RealmConfig.RealmIdentifier realmIdentifier, boolean useAttribute, boolean legacyBindPassword) { List> deprecatedSettings = new ArrayList<>(); if (useAttribute) { - deprecatedSettings.add(LdapUserSearchSessionFactorySettings.SEARCH_ATTRIBUTE - .getConcreteSettingForNamespace(realmIdentifier.getName()) + deprecatedSettings.add( + LdapUserSearchSessionFactorySettings.SEARCH_ATTRIBUTE.getConcreteSettingForNamespace(realmIdentifier.getName()) ); } if (legacyBindPassword) { - deprecatedSettings.add(PoolingSessionFactorySettings.LEGACY_BIND_PASSWORD - .apply(realmIdentifier.getType()) - .getConcreteSettingForNamespace(realmIdentifier.getName())); + deprecatedSettings.add( + PoolingSessionFactorySettings.LEGACY_BIND_PASSWORD.apply(realmIdentifier.getType()) + .getConcreteSettingForNamespace(realmIdentifier.getName()) + ); } if (deprecatedSettings.size() > 0) { assertSettingDeprecationsAndWarnings(deprecatedSettings.toArray(new Setting[deprecatedSettings.size()])); @@ -611,7 +623,7 @@ private boolean configureBindPassword(Settings.Builder builder) { } static LdapUserSearchSessionFactory getLdapUserSearchSessionFactory(RealmConfig config, SSLService sslService, ThreadPool threadPool) - throws LDAPException { + throws LDAPException { LdapUserSearchSessionFactory sessionFactory = new LdapUserSearchSessionFactory(config, sslService, threadPool); if (sessionFactory.getConnectionPool() != null) { // don't use this in production diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/SearchGroupsResolverInMemoryTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/SearchGroupsResolverInMemoryTests.java index 9378a7f5a31b4..022ded23ce491 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/SearchGroupsResolverInMemoryTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/SearchGroupsResolverInMemoryTests.java @@ -14,10 +14,11 @@ import com.unboundid.ldap.sdk.ResultCode; import com.unboundid.ldap.sdk.SimpleBindRequest; import com.unboundid.ldap.sdk.SingleServerSet; + import org.elasticsearch.action.support.PlainActionFuture; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.xpack.core.security.authc.RealmConfig; import org.elasticsearch.xpack.core.security.authc.RealmSettings; @@ -63,9 +64,9 @@ public void testSearchTimeoutIsFailure() throws Exception { connect(options); final Settings settings = Settings.builder() - .put(getFullSettingKey(REALM_IDENTIFIER, SearchGroupsResolverSettings.BASE_DN), "ou=groups,o=sevenSeas") - .put(getFullSettingKey(REALM_IDENTIFIER, SearchGroupsResolverSettings.SCOPE), LdapSearchScope.SUB_TREE) - .build(); + .put(getFullSettingKey(REALM_IDENTIFIER, SearchGroupsResolverSettings.BASE_DN), "ou=groups,o=sevenSeas") + .put(getFullSettingKey(REALM_IDENTIFIER, SearchGroupsResolverSettings.SCOPE), LdapSearchScope.SUB_TREE) + .build(); final SearchGroupsResolver resolver = new SearchGroupsResolver(getConfig(settings)); final PlainActionFuture> future = new PlainActionFuture<>(); resolver.resolve(connection, WILLIAM_BUSH, TimeValue.timeValueSeconds(30), logger, null, future); @@ -83,9 +84,9 @@ public void testResolveWithDefaultUserAttribute() throws Exception { connect(new LDAPConnectionOptions()); Settings settings = Settings.builder() - .put(getFullSettingKey(REALM_IDENTIFIER, SearchGroupsResolverSettings.BASE_DN), "ou=groups,o=sevenSeas") - .put(getFullSettingKey(REALM_IDENTIFIER, SearchGroupsResolverSettings.SCOPE), LdapSearchScope.SUB_TREE) - .build(); + .put(getFullSettingKey(REALM_IDENTIFIER, SearchGroupsResolverSettings.BASE_DN), "ou=groups,o=sevenSeas") + .put(getFullSettingKey(REALM_IDENTIFIER, SearchGroupsResolverSettings.SCOPE), LdapSearchScope.SUB_TREE) + .build(); final List groups = resolveGroups(settings, WILLIAM_BUSH); assertThat(groups, iterableWithSize(1)); @@ -99,9 +100,9 @@ public void testResolveWithExplicitDnAttribute() throws Exception { connect(new LDAPConnectionOptions()); Settings settings = Settings.builder() - .put(getFullSettingKey(REALM_IDENTIFIER, SearchGroupsResolverSettings.BASE_DN), "ou=groups,o=sevenSeas") - .put(getFullSettingKey(REALM_IDENTIFIER.getName(), SearchGroupsResolverSettings.USER_ATTRIBUTE), "dn") - .build(); + .put(getFullSettingKey(REALM_IDENTIFIER, SearchGroupsResolverSettings.BASE_DN), "ou=groups,o=sevenSeas") + .put(getFullSettingKey(REALM_IDENTIFIER.getName(), SearchGroupsResolverSettings.USER_ATTRIBUTE), "dn") + .build(); final List groups = resolveGroups(settings, WILLIAM_BUSH); assertThat(groups, iterableWithSize(1)); @@ -115,9 +116,9 @@ public void testResolveWithMissingAttribute() throws Exception { connect(new LDAPConnectionOptions()); Settings settings = Settings.builder() - .put(getFullSettingKey(REALM_IDENTIFIER, SearchGroupsResolverSettings.BASE_DN), "ou=groups,o=sevenSeas") - .put(getFullSettingKey(REALM_IDENTIFIER.getName(), SearchGroupsResolverSettings.USER_ATTRIBUTE), "no-such-attribute") - .build(); + .put(getFullSettingKey(REALM_IDENTIFIER, SearchGroupsResolverSettings.BASE_DN), "ou=groups,o=sevenSeas") + .put(getFullSettingKey(REALM_IDENTIFIER.getName(), SearchGroupsResolverSettings.USER_ATTRIBUTE), "no-such-attribute") + .build(); final List groups = resolveGroups(settings, WILLIAM_BUSH); assertThat(groups, iterableWithSize(0)); @@ -128,19 +129,17 @@ public void testSearchWithConnectionPoolForOneResult() throws Exception { try (LDAPConnectionPool pool = createConnectionPool(ldapurl)) { final Settings settings = Settings.builder() - .put(getFullSettingKey(REALM_IDENTIFIER, PoolingSessionFactorySettings.BIND_DN), - "cn=Horatio Hornblower,ou=people,o=sevenSeas") - .put(getFullSettingKey(REALM_IDENTIFIER, PoolingSessionFactorySettings.LEGACY_BIND_PASSWORD), "pass") - .put(getFullSettingKey(REALM_IDENTIFIER, SearchGroupsResolverSettings.BASE_DN), "ou=groups,o=sevenSeas") - .put(getFullSettingKey(REALM_IDENTIFIER, SearchGroupsResolverSettings.SCOPE), LdapSearchScope.SUB_TREE) - .build(); + .put( + getFullSettingKey(REALM_IDENTIFIER, PoolingSessionFactorySettings.BIND_DN), + "cn=Horatio Hornblower,ou=people,o=sevenSeas" + ) + .put(getFullSettingKey(REALM_IDENTIFIER, PoolingSessionFactorySettings.LEGACY_BIND_PASSWORD), "pass") + .put(getFullSettingKey(REALM_IDENTIFIER, SearchGroupsResolverSettings.BASE_DN), "ou=groups,o=sevenSeas") + .put(getFullSettingKey(REALM_IDENTIFIER, SearchGroupsResolverSettings.SCOPE), LdapSearchScope.SUB_TREE) + .build(); final SearchGroupsResolver resolver = new SearchGroupsResolver(getConfig(settings)); final PlainActionFuture> future = new PlainActionFuture<>(); - resolver.resolve(pool, - "cn=Moultrie Crystal,ou=people,o=sevenSeas", - TimeValue.timeValueSeconds(30), - logger, - null, future); + resolver.resolve(pool, "cn=Moultrie Crystal,ou=people,o=sevenSeas", TimeValue.timeValueSeconds(30), logger, null, future); List resolvedDNs = future.actionGet(); assertEquals(1, resolvedDNs.size()); } @@ -161,8 +160,9 @@ private LDAPConnectionPool createConnectionPool(LDAPURL ldapurl) throws LDAPExce private void connect(LDAPConnectionOptions options) throws LDAPException { if (connection != null) { - throw new IllegalStateException("Already connected (" + connection.getConnectionName() + ' ' - + connection.getConnectedAddress() + ')'); + throw new IllegalStateException( + "Already connected (" + connection.getConnectionName() + ' ' + connection.getConnectedAddress() + ')' + ); } final LDAPURL ldapurl = new LDAPURL(ldapUrls()[0]); this.connection = LdapUtils.privilegedConnect(() -> { @@ -183,9 +183,12 @@ private RealmConfig getConfig(Settings settings) { if (settings.hasValue("path.home") == false) { settings = Settings.builder().put(settings).put("path.home", createTempDir()).build(); } - return new RealmConfig(REALM_IDENTIFIER, + return new RealmConfig( + REALM_IDENTIFIER, Settings.builder().put(settings).put(getFullSettingKey(REALM_IDENTIFIER, RealmSettings.ORDER_SETTING), 0).build(), - TestEnvironment.newEnvironment(settings), new ThreadContext(settings)); + TestEnvironment.newEnvironment(settings), + new ThreadContext(settings) + ); } } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/support/LdapLoadBalancingTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/support/LdapLoadBalancingTests.java index d1760c3348b90..d5f96e7c39d44 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/support/LdapLoadBalancingTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/support/LdapLoadBalancingTests.java @@ -10,6 +10,7 @@ import com.unboundid.ldap.sdk.RoundRobinDNSServerSet; import com.unboundid.ldap.sdk.RoundRobinServerSet; import com.unboundid.ldap.sdk.ServerSet; + import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.env.TestEnvironment; @@ -40,16 +41,16 @@ public void testBadTypeThrowsException() { public Settings getSettings(String loadBalancerType) { return Settings.builder() - .put(getFullSettingKey(REALM_IDENTIFIER, LdapLoadBalancingSettings.LOAD_BALANCE_TYPE_SETTING), loadBalancerType) - .put("path.home", createTempDir()) - .put(getFullSettingKey(REALM_IDENTIFIER, RealmSettings.ORDER_SETTING), 0) - .build(); + .put(getFullSettingKey(REALM_IDENTIFIER, LdapLoadBalancingSettings.LOAD_BALANCE_TYPE_SETTING), loadBalancerType) + .put("path.home", createTempDir()) + .put(getFullSettingKey(REALM_IDENTIFIER, RealmSettings.ORDER_SETTING), 0) + .build(); } public void testFailoverServerSet() { Settings settings = getSettings("failover"); - String[] address = new String[]{"localhost"}; - int[] ports = new int[]{26000}; + String[] address = new String[] { "localhost" }; + int[] ports = new int[] { 26000 }; ServerSet serverSet = LdapLoadBalancing.serverSet(address, ports, getConfig(settings), null, null); assertThat(serverSet, instanceOf(FailoverServerSet.class)); assertThat(((FailoverServerSet) serverSet).reOrderOnFailover(), is(true)); @@ -57,19 +58,21 @@ public void testFailoverServerSet() { public void testDnsFailover() { Settings settings = getSettings("dns_failover"); - String[] address = new String[]{"foo.bar"}; - int[] ports = new int[]{26000}; + String[] address = new String[] { "foo.bar" }; + int[] ports = new int[] { 26000 }; ServerSet serverSet = LdapLoadBalancing.serverSet(address, ports, getConfig(settings), null, null); assertThat(serverSet, instanceOf(RoundRobinDNSServerSet.class)); - assertThat(((RoundRobinDNSServerSet) serverSet).getAddressSelectionMode(), - is(RoundRobinDNSServerSet.AddressSelectionMode.FAILOVER)); + assertThat( + ((RoundRobinDNSServerSet) serverSet).getAddressSelectionMode(), + is(RoundRobinDNSServerSet.AddressSelectionMode.FAILOVER) + ); } public void testDnsFailoverBadArgs() { final Settings settings = getSettings("dns_failover"); final RealmConfig config = getConfig(settings); - String[] addresses = new String[]{"foo.bar", "localhost"}; - int[] ports = new int[]{26000, 389}; + String[] addresses = new String[] { "foo.bar", "localhost" }; + int[] ports = new int[] { 26000, 389 }; try { LdapLoadBalancing.serverSet(addresses, ports, config, null, null); fail("dns server sets only support a single URL"); @@ -78,7 +81,7 @@ public void testDnsFailoverBadArgs() { } try { - LdapLoadBalancing.serverSet(new String[]{"127.0.0.1"}, new int[]{389}, config, null, null); + LdapLoadBalancing.serverSet(new String[] { "127.0.0.1" }, new int[] { 389 }, config, null, null); fail("dns server sets only support DNS names"); } catch (IllegalArgumentException e) { assertThat(e.getMessage(), containsString("DNS name")); @@ -87,27 +90,29 @@ public void testDnsFailoverBadArgs() { public void testRoundRobin() { Settings settings = getSettings("round_robin"); - String[] address = new String[]{"localhost", "foo.bar"}; - int[] ports = new int[]{389, 389}; + String[] address = new String[] { "localhost", "foo.bar" }; + int[] ports = new int[] { 389, 389 }; ServerSet serverSet = LdapLoadBalancing.serverSet(address, ports, getConfig(settings), null, null); assertThat(serverSet, instanceOf(RoundRobinServerSet.class)); } public void testDnsRoundRobin() { Settings settings = getSettings("dns_round_robin"); - String[] address = new String[]{"foo.bar"}; - int[] ports = new int[]{26000}; + String[] address = new String[] { "foo.bar" }; + int[] ports = new int[] { 26000 }; ServerSet serverSet = LdapLoadBalancing.serverSet(address, ports, getConfig(settings), null, null); assertThat(serverSet, instanceOf(RoundRobinDNSServerSet.class)); - assertThat(((RoundRobinDNSServerSet) serverSet).getAddressSelectionMode(), - is(RoundRobinDNSServerSet.AddressSelectionMode.ROUND_ROBIN)); + assertThat( + ((RoundRobinDNSServerSet) serverSet).getAddressSelectionMode(), + is(RoundRobinDNSServerSet.AddressSelectionMode.ROUND_ROBIN) + ); } public void testDnsRoundRobinBadArgs() { final Settings settings = getSettings("dns_round_robin"); final RealmConfig config = getConfig(settings); - String[] addresses = new String[]{"foo.bar", "localhost"}; - int[] ports = new int[]{26000, 389}; + String[] addresses = new String[] { "foo.bar", "localhost" }; + int[] ports = new int[] { 26000, 389 }; try { LdapLoadBalancing.serverSet(addresses, ports, config, null, null); fail("dns server sets only support a single URL"); @@ -116,7 +121,7 @@ public void testDnsRoundRobinBadArgs() { } try { - LdapLoadBalancing.serverSet(new String[]{"127.0.0.1"}, new int[]{389}, config, null, null); + LdapLoadBalancing.serverSet(new String[] { "127.0.0.1" }, new int[] { 389 }, config, null, null); fail("dns server sets only support DNS names"); } catch (IllegalArgumentException e) { assertThat(e.getMessage(), containsString("DNS name")); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/support/LdapMetadataResolverTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/support/LdapMetadataResolverTests.java index 70a20299438e8..77be366c8b894 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/support/LdapMetadataResolverTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/support/LdapMetadataResolverTests.java @@ -7,10 +7,11 @@ package org.elasticsearch.xpack.security.authc.ldap.support; import com.unboundid.ldap.sdk.Attribute; + import org.elasticsearch.action.support.PlainActionFuture; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.security.authc.RealmConfig; @@ -40,14 +41,18 @@ public class LdapMetadataResolverTests extends ESTestCase { public void testParseSettings() throws Exception { final RealmConfig.RealmIdentifier realmId = new RealmConfig.RealmIdentifier(LdapRealmSettings.LDAP_TYPE, "my_ldap"); final Settings settings = Settings.builder() - .put("path.home", createTempDir()) - .putList(RealmSettings.getFullSettingKey(realmId.getName(), - LdapMetadataResolverSettings.ADDITIONAL_METADATA_SETTING.apply(LdapRealmSettings.LDAP_TYPE)), - "cn", "uid") - .put(RealmSettings.getFullSettingKey(realmId, RealmSettings.ORDER_SETTING), 0) - .build(); - RealmConfig config = new RealmConfig(realmId, - settings, TestEnvironment.newEnvironment(settings), new ThreadContext(settings)); + .put("path.home", createTempDir()) + .putList( + RealmSettings.getFullSettingKey( + realmId.getName(), + LdapMetadataResolverSettings.ADDITIONAL_METADATA_SETTING.apply(LdapRealmSettings.LDAP_TYPE) + ), + "cn", + "uid" + ) + .put(RealmSettings.getFullSettingKey(realmId, RealmSettings.ORDER_SETTING), 0) + .build(); + RealmConfig config = new RealmConfig(realmId, settings, TestEnvironment.newEnvironment(settings), new ThreadContext(settings)); resolver = new LdapMetadataResolver(config, false); assertThat(resolver.attributeNames(), arrayContaining("cn", "uid")); } @@ -55,10 +60,10 @@ public void testParseSettings() throws Exception { public void testResolveSingleValuedAttributeFromCachedAttributes() throws Exception { resolver = new LdapMetadataResolver(Arrays.asList("cn", "uid"), true); final Collection attributes = Arrays.asList( - new Attribute("cn", "Clint Barton"), - new Attribute("uid", "hawkeye"), - new Attribute("email", "clint.barton@shield.gov"), - new Attribute("memberOf", "cn=staff,ou=groups,dc=example,dc=com", "cn=admin,ou=groups,dc=example,dc=com") + new Attribute("cn", "Clint Barton"), + new Attribute("uid", "hawkeye"), + new Attribute("email", "clint.barton@shield.gov"), + new Attribute("memberOf", "cn=staff,ou=groups,dc=example,dc=com", "cn=admin,ou=groups,dc=example,dc=com") ); final Map map = resolve(attributes); assertThat(map, aMapWithSize(2)); @@ -69,8 +74,8 @@ public void testResolveSingleValuedAttributeFromCachedAttributes() throws Except public void testResolveMultiValuedAttributeFromCachedAttributes() throws Exception { resolver = new LdapMetadataResolver(Arrays.asList("cn", "uid"), true); final Collection attributes = Arrays.asList( - new Attribute("cn", "Clint Barton", "hawkeye"), - new Attribute("uid", "hawkeye") + new Attribute("cn", "Clint Barton", "hawkeye"), + new Attribute("uid", "hawkeye") ); final Map map = resolve(attributes); assertThat(map, aMapWithSize(2)); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/support/LdapTestCase.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/support/LdapTestCase.java index e0f9ee73250dd..64422ad424a10 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/support/LdapTestCase.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/support/LdapTestCase.java @@ -25,8 +25,8 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.ssl.KeyStoreUtil; import org.elasticsearch.common.ssl.SslVerificationMode; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.watcher.ResourceWatcherService; @@ -60,6 +60,7 @@ import java.util.Locale; import java.util.Objects; import java.util.stream.Collectors; + import javax.net.ssl.KeyManager; import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.SSLContext; @@ -104,8 +105,11 @@ public void startLdap() throws Exception { getDataPath("/org/elasticsearch/xpack/security/authc/ldap/support/ldap-test-case.key"), ldapPassword ); - final X509ExtendedKeyManager keyManager - = KeyStoreUtil.createKeyManager(ks, ldapPassword, KeyManagerFactory.getDefaultAlgorithm()); + final X509ExtendedKeyManager keyManager = KeyStoreUtil.createKeyManager( + ks, + ldapPassword, + KeyManagerFactory.getDefaultAlgorithm() + ); final SSLContext context = SSLContext.getInstance(XPackSettings.DEFAULT_SUPPORTED_PROTOCOLS.get(0)); context.init(new KeyManager[] { keyManager }, null, null); SSLServerSocketFactory serverSocketFactory = context.getServerSocketFactory(); @@ -114,10 +118,15 @@ public void startLdap() throws Exception { } serverConfig.setListenerConfigs(listeners); InMemoryDirectoryServer ldapServer = new InMemoryDirectoryServer(serverConfig); - ldapServer.add("o=sevenSeas", new Attribute("dc", "UnboundID"), - new Attribute("objectClass", "top", "domain", "extensibleObject")); - ldapServer.importFromLDIF(false, - getDataPath("/org/elasticsearch/xpack/security/authc/ldap/support/seven-seas.ldif").toString()); + ldapServer.add( + "o=sevenSeas", + new Attribute("dc", "UnboundID"), + new Attribute("objectClass", "top", "domain", "extensibleObject") + ); + ldapServer.importFromLDIF( + false, + getDataPath("/org/elasticsearch/xpack/security/authc/ldap/support/seven-seas.ldif").toString() + ); // Must have privileged access because underlying server will accept socket connections AccessController.doPrivileged((PrivilegedExceptionAction) () -> { ldapServer.startListening(); @@ -189,41 +198,54 @@ private InetAddress resolveListenAddress(InetAddress configuredAddress) { } public static Settings buildLdapSettings(String ldapUrl, String userTemplate, String groupSearchBase, LdapSearchScope scope) { - return buildLdapSettings(new String[]{ldapUrl}, new String[]{userTemplate}, groupSearchBase, scope); + return buildLdapSettings(new String[] { ldapUrl }, new String[] { userTemplate }, groupSearchBase, scope); } public static Settings buildLdapSettings(String[] ldapUrl, String userTemplate, String groupSearchBase, LdapSearchScope scope) { - return buildLdapSettings(ldapUrl, new String[]{userTemplate}, groupSearchBase, scope); + return buildLdapSettings(ldapUrl, new String[] { userTemplate }, groupSearchBase, scope); } public static Settings buildLdapSettings(String[] ldapUrl, String[] userTemplate, String groupSearchBase, LdapSearchScope scope) { return buildLdapSettings(ldapUrl, userTemplate, groupSearchBase, scope, null); } - public static Settings buildLdapSettings(String[] ldapUrl, String[] userTemplate, - String groupSearchBase, LdapSearchScope scope, - LdapLoadBalancing serverSetType) { - return buildLdapSettings(ldapUrl, userTemplate, groupSearchBase, scope, - serverSetType, false); + public static Settings buildLdapSettings( + String[] ldapUrl, + String[] userTemplate, + String groupSearchBase, + LdapSearchScope scope, + LdapLoadBalancing serverSetType + ) { + return buildLdapSettings(ldapUrl, userTemplate, groupSearchBase, scope, serverSetType, false); } - public static Settings buildLdapSettings(String[] ldapUrl, String[] userTemplate, - String groupSearchBase, LdapSearchScope scope, - LdapLoadBalancing serverSetType, - boolean ignoreReferralErrors) { + public static Settings buildLdapSettings( + String[] ldapUrl, + String[] userTemplate, + String groupSearchBase, + LdapSearchScope scope, + LdapLoadBalancing serverSetType, + boolean ignoreReferralErrors + ) { return buildLdapSettings(REALM_IDENTIFIER, ldapUrl, userTemplate, groupSearchBase, scope, serverSetType, ignoreReferralErrors); } - public static Settings buildLdapSettings(RealmConfig.RealmIdentifier realmId, String[] ldapUrl, String[] userTemplate, - String groupSearchBase, LdapSearchScope scope, LdapLoadBalancing serverSetType, - boolean ignoreReferralErrors) { + public static Settings buildLdapSettings( + RealmConfig.RealmIdentifier realmId, + String[] ldapUrl, + String[] userTemplate, + String groupSearchBase, + LdapSearchScope scope, + LdapLoadBalancing serverSetType, + boolean ignoreReferralErrors + ) { Settings.Builder builder = Settings.builder() - .putList(getFullSettingKey(realmId, URLS_SETTING), ldapUrl) - .putList(getFullSettingKey(realmId.getName(), LdapSessionFactorySettings.USER_DN_TEMPLATES_SETTING), userTemplate) - .put(getFullSettingKey(realmId, SessionFactorySettings.TIMEOUT_TCP_CONNECTION_SETTING), TimeValue.timeValueSeconds(1L)) - .put(getFullSettingKey(realmId, SessionFactorySettings.IGNORE_REFERRAL_ERRORS_SETTING), ignoreReferralErrors) - .put(getFullSettingKey(realmId, SearchGroupsResolverSettings.BASE_DN), groupSearchBase) - .put(getFullSettingKey(realmId, SearchGroupsResolverSettings.SCOPE), scope); + .putList(getFullSettingKey(realmId, URLS_SETTING), ldapUrl) + .putList(getFullSettingKey(realmId.getName(), LdapSessionFactorySettings.USER_DN_TEMPLATES_SETTING), userTemplate) + .put(getFullSettingKey(realmId, SessionFactorySettings.TIMEOUT_TCP_CONNECTION_SETTING), TimeValue.timeValueSeconds(1L)) + .put(getFullSettingKey(realmId, SessionFactorySettings.IGNORE_REFERRAL_ERRORS_SETTING), ignoreReferralErrors) + .put(getFullSettingKey(realmId, SearchGroupsResolverSettings.BASE_DN), groupSearchBase) + .put(getFullSettingKey(realmId, SearchGroupsResolverSettings.SCOPE), scope); if (serverSetType != null) { builder.put(getFullSettingKey(realmId, LdapLoadBalancingSettings.LOAD_BALANCE_TYPE_SETTING), serverSetType.toString()); } @@ -233,11 +255,13 @@ public static Settings buildLdapSettings(RealmConfig.RealmIdentifier realmId, St public static Settings buildLdapSettings(String[] ldapUrl, String userTemplate, boolean hostnameVerification) { Settings.Builder builder = Settings.builder() - .putList(getFullSettingKey(REALM_IDENTIFIER, URLS_SETTING), ldapUrl) - .putList(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapSessionFactorySettings.USER_DN_TEMPLATES_SETTING), userTemplate); + .putList(getFullSettingKey(REALM_IDENTIFIER, URLS_SETTING), ldapUrl) + .putList(getFullSettingKey(REALM_IDENTIFIER.getName(), LdapSessionFactorySettings.USER_DN_TEMPLATES_SETTING), userTemplate); if (randomBoolean()) { - builder.put(getFullSettingKey(REALM_IDENTIFIER, SSLConfigurationSettings.VERIFICATION_MODE_SETTING_REALM), - hostnameVerification ? SslVerificationMode.FULL : SslVerificationMode.CERTIFICATE); + builder.put( + getFullSettingKey(REALM_IDENTIFIER, SSLConfigurationSettings.VERIFICATION_MODE_SETTING_REALM), + hostnameVerification ? SslVerificationMode.FULL : SslVerificationMode.CERTIFICATE + ); } else { builder.put(getFullSettingKey(REALM_IDENTIFIER, HOSTNAME_VERIFICATION_SETTING), hostnameVerification); } @@ -246,12 +270,16 @@ public static Settings buildLdapSettings(String[] ldapUrl, String userTemplate, protected DnRoleMapper buildGroupAsRoleMapper(ResourceWatcherService resourceWatcherService) { Settings settings = Settings.builder() - .put(getFullSettingKey(REALM_IDENTIFIER, DnRoleMapperSettings.USE_UNMAPPED_GROUPS_AS_ROLES_SETTING), true) - .put("path.home", createTempDir()) - .put(getFullSettingKey(REALM_IDENTIFIER, RealmSettings.ORDER_SETTING), 0) - .build(); - RealmConfig config = new RealmConfig(REALM_IDENTIFIER, settings, - TestEnvironment.newEnvironment(settings), new ThreadContext(Settings.EMPTY)); + .put(getFullSettingKey(REALM_IDENTIFIER, DnRoleMapperSettings.USE_UNMAPPED_GROUPS_AS_ROLES_SETTING), true) + .put("path.home", createTempDir()) + .put(getFullSettingKey(REALM_IDENTIFIER, RealmSettings.ORDER_SETTING), 0) + .build(); + RealmConfig config = new RealmConfig( + REALM_IDENTIFIER, + settings, + TestEnvironment.newEnvironment(settings), + new ThreadContext(Settings.EMPTY) + ); return new DnRoleMapper(config, resourceWatcherService); } @@ -282,8 +310,10 @@ public Void run() { try { if (conn instanceof LDAPConnection) { assertTrue(((LDAPConnection) conn).isConnected()); - assertEquals(bindRequest.getBindDN(), - ((SimpleBindRequest) ((LDAPConnection) conn).getLastBindRequest()).getBindDN()); + assertEquals( + bindRequest.getBindDN(), + ((SimpleBindRequest) ((LDAPConnection) conn).getLastBindRequest()).getBindDN() + ); ((LDAPConnection) conn).reconnect(); } else if (conn instanceof LDAPConnectionPool) { try (LDAPConnection c = ((LDAPConnectionPool) conn).getConnection()) { @@ -293,8 +323,11 @@ public Void run() { } } } catch (LDAPException e) { - fail("Connection is not valid. It will not work on follow referral flow." + - System.lineSeparator() + ExceptionsHelper.stackTrace(e)); + fail( + "Connection is not valid. It will not work on follow referral flow." + + System.lineSeparator() + + ExceptionsHelper.stackTrace(e) + ); } return null; } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/support/SessionFactoryLoadBalancingTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/support/SessionFactoryLoadBalancingTests.java index cb49bd279f5c0..b243fb872002f 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/support/SessionFactoryLoadBalancingTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/support/SessionFactoryLoadBalancingTests.java @@ -129,8 +129,14 @@ public void testRoundRobinWithFailures() throws Exception { // NOTE: this is not perfect as there is a small amount of time between the shutdown // of the ldap server and the opening of the socket logger.debug("opening mock client sockets bound to [{}]", port); - Runnable runnable = new PortBlockingRunnable(mockServerSocket.getInetAddress(), mockServerSocket.getLocalPort(), port, - latch, closeLatch, success); + Runnable runnable = new PortBlockingRunnable( + mockServerSocket.getInetAddress(), + mockServerSocket.getLocalPort(), + port, + latch, + closeLatch, + success + ); Thread thread = new Thread(runnable); thread.start(); listenThreads.add(thread); @@ -140,9 +146,11 @@ public void testRoundRobinWithFailures() throws Exception { latch.await(); - assumeTrue("Failed to open sockets on all addresses with the port that an LDAP server was bound to. Some operating systems " + - "allow binding to an address and port combination even if an application is bound to the port on a wildcard address", - success.get()); + assumeTrue( + "Failed to open sockets on all addresses with the port that an LDAP server was bound to. Some operating systems " + + "allow binding to an address and port combination even if an application is bound to the port on a wildcard address", + success.get() + ); final int numberOfIterations = randomIntBetween(1, 5); logger.debug("list of all open ports {}", ports); // go one iteration through and attempt a bind @@ -221,7 +229,7 @@ public void testRoundRobinWithFailures() throws Exception { @SuppressForbidden(reason = "Allow opening socket for test") private MockSocket openMockSocket(InetAddress remoteAddress, int remotePort, InetAddress localAddress, int localPort) - throws IOException { + throws IOException { final MockSocket socket = new MockSocket(); socket.setReuseAddress(true); // allow binding even if the previous socket is in timed wait state. socket.setSoLinger(true, 0); // close immediately as we are not writing anything here. @@ -273,8 +281,14 @@ public void testFailover() throws Exception { // NOTE: this is not perfect as there is a small amount of time between the shutdown // of the ldap server and the opening of the socket logger.debug("opening mock server socket listening on [{}]", port); - Runnable runnable = new PortBlockingRunnable(mockServerSocket.getInetAddress(), mockServerSocket.getLocalPort(), port, - latch, closeLatch, success); + Runnable runnable = new PortBlockingRunnable( + mockServerSocket.getInetAddress(), + mockServerSocket.getLocalPort(), + port, + latch, + closeLatch, + success + ); Thread thread = new Thread(runnable); thread.start(); listenThreads.add(thread); @@ -285,9 +299,11 @@ public void testFailover() throws Exception { try { latch.await(); - assumeTrue("Failed to open sockets on all addresses with the port that an LDAP server was bound to. Some operating systems " + - "allow binding to an address and port combination even if an application is bound to the port on a wildcard address", - success.get()); + assumeTrue( + "Failed to open sockets on all addresses with the port that an LDAP server was bound to. Some operating systems " + + "allow binding to an address and port combination even if an application is bound to the port on a wildcard address", + success.get() + ); int firstNonStoppedPort = -1; // now we find the first that isn't stopped for (int i = 0; i < numberOfLdapServers; i++) { @@ -304,11 +320,15 @@ public void testFailover() throws Exception { LDAPConnection connection = null; try { do { - final LDAPConnection finalConnection = - LdapUtils.privilegedConnect(testSessionFactory.getServerSet()::getConnection); + final LDAPConnection finalConnection = LdapUtils.privilegedConnect( + testSessionFactory.getServerSet()::getConnection + ); connection = finalConnection; - logger.debug("established connection with port [{}] expected port [{}]", - finalConnection.getConnectedPort(), firstNonStoppedPort); + logger.debug( + "established connection with port [{}] expected port [{}]", + finalConnection.getConnectedPort(), + firstNonStoppedPort + ); if (finalConnection.getConnectedPort() != firstNonStoppedPort) { LDAPException e = expectThrows(LDAPException.class, () -> finalConnection.bind(new SimpleBindRequest())); assertThat(e.getMessage(), containsString("not connected")); @@ -336,13 +356,21 @@ public void testFailover() throws Exception { private TestSessionFactory createSessionFactory(LdapLoadBalancing loadBalancing) throws Exception { String groupSearchBase = "cn=HMS Lydia,ou=crews,ou=groups,o=sevenSeas"; String userTemplate = "cn={0},ou=people,o=sevenSeas"; - Settings settings = buildLdapSettings(ldapUrls(), new String[] { userTemplate }, groupSearchBase, - LdapSearchScope.SUB_TREE, loadBalancing); + Settings settings = buildLdapSettings( + ldapUrls(), + new String[] { userTemplate }, + groupSearchBase, + LdapSearchScope.SUB_TREE, + loadBalancing + ); Settings globalSettings = Settings.builder().put("path.home", createTempDir()).put(settings).build(); - RealmConfig config = new RealmConfig(REALM_IDENTIFIER, globalSettings, - TestEnvironment.newEnvironment(globalSettings), new ThreadContext(Settings.EMPTY)); - return new TestSessionFactory(config, new SSLService(TestEnvironment.newEnvironment(config.settings())), - threadPool); + RealmConfig config = new RealmConfig( + REALM_IDENTIFIER, + globalSettings, + TestEnvironment.newEnvironment(globalSettings), + new ThreadContext(Settings.EMPTY) + ); + return new TestSessionFactory(config, new SSLService(TestEnvironment.newEnvironment(config.settings())), threadPool); } private class PortBlockingRunnable implements Runnable { @@ -354,8 +382,14 @@ private class PortBlockingRunnable implements Runnable { private final CountDownLatch closeLatch; private final AtomicBoolean success; - private PortBlockingRunnable(InetAddress serverAddress, int serverPort, int portToBind, CountDownLatch latch, - CountDownLatch closeLatch, AtomicBoolean success) { + private PortBlockingRunnable( + InetAddress serverAddress, + int serverPort, + int portToBind, + CountDownLatch latch, + CountDownLatch closeLatch, + AtomicBoolean success + ) { this.serverAddress = serverAddress; this.serverPort = serverPort; this.portToBind = portToBind; diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/support/SessionFactoryTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/support/SessionFactoryTests.java index 7b851c9a76840..a49070786bb0e 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/support/SessionFactoryTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/support/SessionFactoryTests.java @@ -55,13 +55,13 @@ public void shutdown() throws InterruptedException { public void testConnectionFactoryReturnsCorrectLDAPConnectionOptionsWithDefaultSettings() throws Exception { final RealmConfig.RealmIdentifier realmIdentifier = new RealmConfig.RealmIdentifier("ldap", "conn_settings"); final Environment environment = TestEnvironment.newEnvironment( - Settings.builder().put("path.home", createTempDir()) - .put(getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0).build()); - RealmConfig realmConfig = new RealmConfig( - realmIdentifier, - environment.settings(), environment, new ThreadContext(Settings.EMPTY)); - LDAPConnectionOptions options = SessionFactory.connectionOptions(realmConfig, new SSLService(environment), - logger); + Settings.builder() + .put("path.home", createTempDir()) + .put(getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0) + .build() + ); + RealmConfig realmConfig = new RealmConfig(realmIdentifier, environment.settings(), environment, new ThreadContext(Settings.EMPTY)); + LDAPConnectionOptions options = SessionFactory.connectionOptions(realmConfig, new SSLService(environment), logger); assertThat(options.followReferrals(), is(equalTo(true))); assertThat(options.allowConcurrentSocketFactoryUse(), is(equalTo(true))); assertThat(options.getConnectTimeoutMillis(), is(equalTo(5000))); @@ -74,10 +74,10 @@ public void testSessionFactoryWithResponseTimeout() throws Exception { final Path pathHome = createTempDir(); { Settings settings = Settings.builder() - .put(getFullSettingKey(realmId, SessionFactorySettings.TIMEOUT_RESPONSE_SETTING), "10s") - .put(getFullSettingKey(realmId, RealmSettings.ORDER_SETTING), 0) - .put("path.home", pathHome) - .build(); + .put(getFullSettingKey(realmId, SessionFactorySettings.TIMEOUT_RESPONSE_SETTING), "10s") + .put(getFullSettingKey(realmId, RealmSettings.ORDER_SETTING), 0) + .put("path.home", pathHome) + .build(); final Environment environment = TestEnvironment.newEnvironment(settings); RealmConfig realmConfig = new RealmConfig(realmId, settings, environment, new ThreadContext(settings)); @@ -86,39 +86,48 @@ public void testSessionFactoryWithResponseTimeout() throws Exception { } { Settings settings = Settings.builder() - .put(getFullSettingKey(realmId, SessionFactorySettings.TIMEOUT_TCP_READ_SETTING), "7s") - .put(getFullSettingKey(realmId, RealmSettings.ORDER_SETTING), 0) - .put("path.home", pathHome) - .build(); + .put(getFullSettingKey(realmId, SessionFactorySettings.TIMEOUT_TCP_READ_SETTING), "7s") + .put(getFullSettingKey(realmId, RealmSettings.ORDER_SETTING), 0) + .put("path.home", pathHome) + .build(); final Environment environment = TestEnvironment.newEnvironment(settings); RealmConfig realmConfig = new RealmConfig(realmId, settings, environment, new ThreadContext(settings)); LDAPConnectionOptions options = SessionFactory.connectionOptions(realmConfig, new SSLService(settings, environment), logger); assertThat(options.getResponseTimeoutMillis(), is(equalTo(7000L))); - assertSettingDeprecationsAndWarnings(new Setting[]{SessionFactorySettings.TIMEOUT_TCP_READ_SETTING.apply("ldap") - .getConcreteSettingForNamespace("response_settings")}); + assertSettingDeprecationsAndWarnings( + new Setting[] { + SessionFactorySettings.TIMEOUT_TCP_READ_SETTING.apply("ldap").getConcreteSettingForNamespace("response_settings") } + ); } { Settings settings = Settings.builder() - .put(getFullSettingKey(realmId, SessionFactorySettings.TIMEOUT_RESPONSE_SETTING), "11s") - .put(getFullSettingKey(realmId, SessionFactorySettings.TIMEOUT_TCP_READ_SETTING), "6s") - .put(getFullSettingKey(realmId, RealmSettings.ORDER_SETTING), 0) - .put("path.home", pathHome) - .build(); + .put(getFullSettingKey(realmId, SessionFactorySettings.TIMEOUT_RESPONSE_SETTING), "11s") + .put(getFullSettingKey(realmId, SessionFactorySettings.TIMEOUT_TCP_READ_SETTING), "6s") + .put(getFullSettingKey(realmId, RealmSettings.ORDER_SETTING), 0) + .put("path.home", pathHome) + .build(); final Environment environment = TestEnvironment.newEnvironment(settings); RealmConfig realmConfig = new RealmConfig(realmId, settings, environment, new ThreadContext(settings)); - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> SessionFactory.connectionOptions(realmConfig - , new SSLService(settings, environment), logger)); - assertThat(ex.getMessage(), is("[xpack.security.authc.realms.ldap.response_settings.timeout.tcp_read] and [xpack.security" + - ".authc.realms.ldap.response_settings.timeout.response] may not be used at the same time")); + IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> SessionFactory.connectionOptions(realmConfig, new SSLService(settings, environment), logger) + ); + assertThat( + ex.getMessage(), + is( + "[xpack.security.authc.realms.ldap.response_settings.timeout.tcp_read] and [xpack.security" + + ".authc.realms.ldap.response_settings.timeout.response] may not be used at the same time" + ) + ); } { Settings settings = Settings.builder() - .put(getFullSettingKey(realmId, SessionFactorySettings.TIMEOUT_LDAP_SETTING), "750ms") - .put(getFullSettingKey(realmId, RealmSettings.ORDER_SETTING), 0) - .put("path.home", pathHome) - .build(); + .put(getFullSettingKey(realmId, SessionFactorySettings.TIMEOUT_LDAP_SETTING), "750ms") + .put(getFullSettingKey(realmId, RealmSettings.ORDER_SETTING), 0) + .put("path.home", pathHome) + .build(); final Environment environment = TestEnvironment.newEnvironment(settings); RealmConfig realmConfig = new RealmConfig(realmId, settings, environment, new ThreadContext(settings)); @@ -131,13 +140,13 @@ public void testConnectionFactoryReturnsCorrectLDAPConnectionOptions() throws Ex final RealmConfig.RealmIdentifier realmId = new RealmConfig.RealmIdentifier("ldap", "conn_settings"); final Path pathHome = createTempDir(); Settings settings = Settings.builder() - .put(getFullSettingKey(realmId, SessionFactorySettings.TIMEOUT_TCP_CONNECTION_SETTING), "10ms") - .put(getFullSettingKey(realmId, SessionFactorySettings.HOSTNAME_VERIFICATION_SETTING), "false") - .put(getFullSettingKey(realmId, SessionFactorySettings.TIMEOUT_RESPONSE_SETTING), "20ms") - .put(getFullSettingKey(realmId, SessionFactorySettings.FOLLOW_REFERRALS_SETTING), "false") - .put(getFullSettingKey(realmId, RealmSettings.ORDER_SETTING), 0) - .put("path.home", pathHome) - .build(); + .put(getFullSettingKey(realmId, SessionFactorySettings.TIMEOUT_TCP_CONNECTION_SETTING), "10ms") + .put(getFullSettingKey(realmId, SessionFactorySettings.HOSTNAME_VERIFICATION_SETTING), "false") + .put(getFullSettingKey(realmId, SessionFactorySettings.TIMEOUT_RESPONSE_SETTING), "20ms") + .put(getFullSettingKey(realmId, SessionFactorySettings.FOLLOW_REFERRALS_SETTING), "false") + .put(getFullSettingKey(realmId, RealmSettings.ORDER_SETTING), 0) + .put("path.home", pathHome) + .build(); Environment environment = TestEnvironment.newEnvironment(settings); RealmConfig realmConfig = new RealmConfig(realmId, settings, environment, new ThreadContext(settings)); @@ -147,14 +156,16 @@ public void testConnectionFactoryReturnsCorrectLDAPConnectionOptions() throws Ex assertThat(options.getConnectTimeoutMillis(), is(equalTo(10))); assertThat(options.getResponseTimeoutMillis(), is(equalTo(20L))); assertThat(options.getSSLSocketVerifier(), is(instanceOf(TrustAllSSLSocketVerifier.class))); - assertWarnings("the setting [xpack.security.authc.realms.ldap.conn_settings.hostname_verification] has been deprecated and will be " - + "removed in a future version. use [xpack.security.authc.realms.ldap.conn_settings.ssl.verification_mode] instead"); + assertWarnings( + "the setting [xpack.security.authc.realms.ldap.conn_settings.hostname_verification] has been deprecated and will be " + + "removed in a future version. use [xpack.security.authc.realms.ldap.conn_settings.ssl.verification_mode] instead" + ); settings = Settings.builder() - .put(getFullSettingKey(realmId, SSLConfigurationSettings.VERIFICATION_MODE_SETTING_REALM), SslVerificationMode.CERTIFICATE) - .put("path.home", pathHome) - .put(getFullSettingKey(realmId, RealmSettings.ORDER_SETTING), 0) - .build(); + .put(getFullSettingKey(realmId, SSLConfigurationSettings.VERIFICATION_MODE_SETTING_REALM), SslVerificationMode.CERTIFICATE) + .put("path.home", pathHome) + .put(getFullSettingKey(realmId, RealmSettings.ORDER_SETTING), 0) + .build(); realmConfig = new RealmConfig(realmId, settings, environment, new ThreadContext(settings)); options = SessionFactory.connectionOptions(realmConfig, new SSLService(TestEnvironment.newEnvironment(settings)), logger); assertThat(options.getSSLSocketVerifier(), is(instanceOf(TrustAllSSLSocketVerifier.class))); @@ -162,10 +173,10 @@ public void testConnectionFactoryReturnsCorrectLDAPConnectionOptions() throws Ex // Can't run in FIPS with verification_mode none, disable this check instead of duplicating the test case if (inFipsJvm() == false) { settings = Settings.builder() - .put(getFullSettingKey(realmId, SSLConfigurationSettings.VERIFICATION_MODE_SETTING_REALM), SslVerificationMode.NONE) - .put("path.home", pathHome) - .put(getFullSettingKey(realmId, RealmSettings.ORDER_SETTING), 0) - .build(); + .put(getFullSettingKey(realmId, SSLConfigurationSettings.VERIFICATION_MODE_SETTING_REALM), SslVerificationMode.NONE) + .put("path.home", pathHome) + .put(getFullSettingKey(realmId, RealmSettings.ORDER_SETTING), 0) + .build(); environment = TestEnvironment.newEnvironment(settings); realmConfig = new RealmConfig(realmId, settings, environment, new ThreadContext(settings)); options = SessionFactory.connectionOptions(realmConfig, new SSLService(environment), logger); @@ -173,10 +184,10 @@ public void testConnectionFactoryReturnsCorrectLDAPConnectionOptions() throws Ex } settings = Settings.builder() - .put(getFullSettingKey(realmId, SSLConfigurationSettings.VERIFICATION_MODE_SETTING_REALM), SslVerificationMode.FULL) - .put("path.home", pathHome) - .put(getFullSettingKey(realmId, RealmSettings.ORDER_SETTING), 0) - .build(); + .put(getFullSettingKey(realmId, SSLConfigurationSettings.VERIFICATION_MODE_SETTING_REALM), SslVerificationMode.FULL) + .put("path.home", pathHome) + .put(getFullSettingKey(realmId, RealmSettings.ORDER_SETTING), 0) + .build(); environment = TestEnvironment.newEnvironment(settings); realmConfig = new RealmConfig(realmId, settings, environment, new ThreadContext(settings)); options = SessionFactory.connectionOptions(realmConfig, new SSLService(environment), logger); @@ -188,21 +199,26 @@ public void testSessionFactoryDoesNotSupportUnauthenticated() { } public void testUnauthenticatedSessionThrowsUnsupportedOperationException() throws Exception { - UnsupportedOperationException e = expectThrows(UnsupportedOperationException.class, - () -> createSessionFactory().unauthenticatedSession(randomAlphaOfLength(5), new PlainActionFuture<>())); + UnsupportedOperationException e = expectThrows( + UnsupportedOperationException.class, + () -> createSessionFactory().unauthenticatedSession(randomAlphaOfLength(5), new PlainActionFuture<>()) + ); assertThat(e.getMessage(), containsString("unauthenticated sessions")); } private SessionFactory createSessionFactory() { Settings global = Settings.builder().put("path.home", createTempDir()).build(); final RealmConfig.RealmIdentifier realmIdentifier = new RealmConfig.RealmIdentifier("ldap", "_name"); - final RealmConfig realmConfig = new RealmConfig(realmIdentifier, - Settings.builder() - .put(getFullSettingKey(realmIdentifier, SessionFactorySettings.URLS_SETTING), "ldap://localhost:389") - .put(global) - .put(getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0) - .build(), - TestEnvironment.newEnvironment(global), new ThreadContext(Settings.EMPTY)); + final RealmConfig realmConfig = new RealmConfig( + realmIdentifier, + Settings.builder() + .put(getFullSettingKey(realmIdentifier, SessionFactorySettings.URLS_SETTING), "ldap://localhost:389") + .put(global) + .put(getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0) + .build(), + TestEnvironment.newEnvironment(global), + new ThreadContext(Settings.EMPTY) + ); return new SessionFactory(realmConfig, null, threadPool) { @Override diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectAuthenticatorTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectAuthenticatorTests.java index 5c8f978130832..247f93882d7c8 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectAuthenticatorTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectAuthenticatorTests.java @@ -75,6 +75,7 @@ import java.util.Date; import java.util.Map; import java.util.UUID; + import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; @@ -96,8 +97,10 @@ public class OpenIdConnectAuthenticatorTests extends OpenIdConnectTestCase { @Before public void setup() { - final Settings globalSettings = Settings.builder().put("path.home", createTempDir()) - .put("xpack.security.authc.realms.oidc.oidc-realm.ssl.verification_mode", "certificate").build(); + final Settings globalSettings = Settings.builder() + .put("path.home", createTempDir()) + .put("xpack.security.authc.realms.oidc.oidc-realm.ssl.verification_mode", "certificate") + .build(); env = TestEnvironment.newEnvironment(globalSettings); threadContext = new ThreadContext(globalSettings); callsToReloadJwk = 0; @@ -115,22 +118,26 @@ private OpenIdConnectAuthenticator buildAuthenticator() throws URISyntaxExceptio return new OpenIdConnectAuthenticator(config, getOpConfig(), getDefaultRpConfig(), new SSLService(env), null); } - private OpenIdConnectAuthenticator buildAuthenticator(OpenIdConnectProviderConfiguration opConfig, RelyingPartyConfiguration rpConfig, - OpenIdConnectAuthenticator.ReloadableJWKSource jwkSource) { + private OpenIdConnectAuthenticator buildAuthenticator( + OpenIdConnectProviderConfiguration opConfig, + RelyingPartyConfiguration rpConfig, + OpenIdConnectAuthenticator.ReloadableJWKSource jwkSource + ) { final RealmConfig config = buildConfig(getBasicRealmSettings().build(), threadContext); final JWSVerificationKeySelector keySelector = new JWSVerificationKeySelector<>(rpConfig.getSignatureAlgorithm(), jwkSource); final IDTokenValidator validator = new IDTokenValidator(opConfig.getIssuer(), rpConfig.getClientId(), keySelector, null); - return new OpenIdConnectAuthenticator(config, opConfig, rpConfig, new SSLService(env), validator, - null); + return new OpenIdConnectAuthenticator(config, opConfig, rpConfig, new SSLService(env), validator, null); } - private OpenIdConnectAuthenticator buildAuthenticator(OpenIdConnectProviderConfiguration opConfig, - RelyingPartyConfiguration rpConfig) { + private OpenIdConnectAuthenticator buildAuthenticator(OpenIdConnectProviderConfiguration opConfig, RelyingPartyConfiguration rpConfig) { final RealmConfig config = buildConfig(getBasicRealmSettings().build(), threadContext); - final IDTokenValidator validator = new IDTokenValidator(opConfig.getIssuer(), rpConfig.getClientId(), - rpConfig.getSignatureAlgorithm(), new Secret(rpConfig.getClientSecret().toString())); - return new OpenIdConnectAuthenticator(config, opConfig, rpConfig, new SSLService(env), validator, - null); + final IDTokenValidator validator = new IDTokenValidator( + opConfig.getIssuer(), + rpConfig.getClientId(), + rpConfig.getSignatureAlgorithm(), + new Secret(rpConfig.getClientSecret().toString()) + ); + return new OpenIdConnectAuthenticator(config, opConfig, rpConfig, new SSLService(env), validator, null); } public void testEmptyRedirectUrlIsRejected() throws Exception { @@ -139,8 +146,7 @@ public void testEmptyRedirectUrlIsRejected() throws Exception { OpenIdConnectToken token = new OpenIdConnectToken(null, new State(), new Nonce(), authenticatingRealm); final PlainActionFuture future = new PlainActionFuture<>(); authenticator.authenticate(token, future); - ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, - future::actionGet); + ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, future::actionGet); assertThat(e.getMessage(), containsString("Failed to consume the OpenID connect response")); } @@ -151,11 +157,10 @@ public void testInvalidStateIsRejected() throws URISyntaxException { final String invalidState = state.concat(randomAlphaOfLength(2)); final String redirectUrl = "https://rp.elastic.co/cb?code=" + code + "&state=" + state; final String authenticatingRealm = randomBoolean() ? REALM_NAME : null; - OpenIdConnectToken token = new OpenIdConnectToken(redirectUrl, new State(invalidState), new Nonce(),authenticatingRealm); + OpenIdConnectToken token = new OpenIdConnectToken(redirectUrl, new State(invalidState), new Nonce(), authenticatingRealm); final PlainActionFuture future = new PlainActionFuture<>(); authenticator.authenticate(token, future); - ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, - future::actionGet); + ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, future::actionGet); assertThat(e.getMessage(), containsString("Invalid state parameter")); } @@ -183,8 +188,7 @@ public void testInvalidNonceIsRejected() throws Exception { final OpenIdConnectToken token = new OpenIdConnectToken(responseUrl, state, nonce, authenticatingRealm); final PlainActionFuture future = new PlainActionFuture<>(); authenticator.authenticate(token, future); - ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, - future::actionGet); + ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, future::actionGet); assertThat(e.getMessage(), containsString("Failed to parse or validate the ID Token")); assertThat(e.getCause(), instanceOf(BadJWTException.class)); assertThat(e.getCause().getMessage(), containsString("Unexpected JWT nonce")); @@ -272,8 +276,7 @@ public void testClockSkewIsHonored() throws Exception { final Nonce nonce = new Nonce(); final String subject = "janedoe"; final String keyId = (jwk.getAlgorithm().getName().startsWith("HS")) ? null : jwk.getKeyID(); - JWTClaimsSet.Builder idTokenBuilder = new JWTClaimsSet.Builder() - .jwtID(randomAlphaOfLength(8)) + JWTClaimsSet.Builder idTokenBuilder = new JWTClaimsSet.Builder().jwtID(randomAlphaOfLength(8)) .audience(rpConfig.getClientId().getValue()) // Expired 55 seconds ago with an allowed clock skew of 60 seconds .expirationTime(Date.from(now().minusSeconds(55))) @@ -282,8 +285,15 @@ public void testClockSkewIsHonored() throws Exception { .notBeforeTime(Date.from(now().minusSeconds(200))) .claim("nonce", nonce) .subject(subject); - final Tuple tokens = buildTokens(idTokenBuilder.build(), key, jwk.getAlgorithm().getName(), keyId, subject, - true, false); + final Tuple tokens = buildTokens( + idTokenBuilder.build(), + key, + jwk.getAlgorithm().getName(), + keyId, + subject, + true, + false + ); final String responseUrl = buildAuthResponse(tokens.v2(), tokens.v1(), state, rpConfig.getRedirectUri()); final String authenticatingRealm = randomBoolean() ? REALM_NAME : null; final OpenIdConnectToken token = new OpenIdConnectToken(responseUrl, state, nonce, authenticatingRealm); @@ -310,8 +320,7 @@ public void testImplicitFlowFailsWithExpiredToken() throws Exception { final Nonce nonce = new Nonce(); final String subject = "janedoe"; final String keyId = (jwk.getAlgorithm().getName().startsWith("HS")) ? null : jwk.getKeyID(); - JWTClaimsSet.Builder idTokenBuilder = new JWTClaimsSet.Builder() - .jwtID(randomAlphaOfLength(8)) + JWTClaimsSet.Builder idTokenBuilder = new JWTClaimsSet.Builder().jwtID(randomAlphaOfLength(8)) .audience(rpConfig.getClientId().getValue()) // Expired 65 seconds ago with an allowed clock skew of 60 seconds .expirationTime(Date.from(now().minusSeconds(65))) @@ -320,15 +329,21 @@ public void testImplicitFlowFailsWithExpiredToken() throws Exception { .notBeforeTime(Date.from(now().minusSeconds(200))) .claim("nonce", nonce) .subject(subject); - final Tuple tokens = buildTokens(idTokenBuilder.build(), key, jwk.getAlgorithm().getName(), keyId, - subject, true, false); + final Tuple tokens = buildTokens( + idTokenBuilder.build(), + key, + jwk.getAlgorithm().getName(), + keyId, + subject, + true, + false + ); final String responseUrl = buildAuthResponse(tokens.v2(), tokens.v1(), state, rpConfig.getRedirectUri()); final String authenticatingRealm = randomBoolean() ? REALM_NAME : null; final OpenIdConnectToken token = new OpenIdConnectToken(responseUrl, state, nonce, authenticatingRealm); final PlainActionFuture future = new PlainActionFuture<>(); authenticator.authenticate(token, future); - ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, - future::actionGet); + ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, future::actionGet); assertThat(e.getMessage(), containsString("Failed to parse or validate the ID Token")); assertThat(e.getCause(), instanceOf(BadJWTException.class)); assertThat(e.getCause().getMessage(), containsString("Expired JWT")); @@ -355,8 +370,7 @@ public void testImplicitFlowFailsNotYetIssuedToken() throws Exception { final Nonce nonce = new Nonce(); final String subject = "janedoe"; final String keyId = (jwk.getAlgorithm().getName().startsWith("HS")) ? null : jwk.getKeyID(); - JWTClaimsSet.Builder idTokenBuilder = new JWTClaimsSet.Builder() - .jwtID(randomAlphaOfLength(8)) + JWTClaimsSet.Builder idTokenBuilder = new JWTClaimsSet.Builder().jwtID(randomAlphaOfLength(8)) .audience(rpConfig.getClientId().getValue()) .expirationTime(Date.from(now().plusSeconds(3600))) .issuer(opConfig.getIssuer().getValue()) @@ -365,15 +379,21 @@ public void testImplicitFlowFailsNotYetIssuedToken() throws Exception { .notBeforeTime(Date.from(now().minusSeconds(80))) .claim("nonce", nonce) .subject(subject); - final Tuple tokens = buildTokens(idTokenBuilder.build(), key, jwk.getAlgorithm().getName(), keyId, - subject, true, false); + final Tuple tokens = buildTokens( + idTokenBuilder.build(), + key, + jwk.getAlgorithm().getName(), + keyId, + subject, + true, + false + ); final String responseUrl = buildAuthResponse(tokens.v2(), tokens.v1(), state, rpConfig.getRedirectUri()); final String authenticatingRealm = randomBoolean() ? REALM_NAME : null; final OpenIdConnectToken token = new OpenIdConnectToken(responseUrl, state, nonce, authenticatingRealm); final PlainActionFuture future = new PlainActionFuture<>(); authenticator.authenticate(token, future); - ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, - future::actionGet); + ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, future::actionGet); assertThat(e.getMessage(), containsString("Failed to parse or validate the ID Token")); assertThat(e.getCause(), instanceOf(BadJWTException.class)); assertThat(e.getCause().getMessage(), containsString("JWT issue time ahead of current time")); @@ -400,8 +420,7 @@ public void testImplicitFlowFailsInvalidIssuer() throws Exception { final Nonce nonce = new Nonce(); final String subject = "janedoe"; final String keyId = (jwk.getAlgorithm().getName().startsWith("HS")) ? null : jwk.getKeyID(); - JWTClaimsSet.Builder idTokenBuilder = new JWTClaimsSet.Builder() - .jwtID(randomAlphaOfLength(8)) + JWTClaimsSet.Builder idTokenBuilder = new JWTClaimsSet.Builder().jwtID(randomAlphaOfLength(8)) .audience(rpConfig.getClientId().getValue()) .expirationTime(Date.from(now().plusSeconds(3600))) .issuer("https://another.op.org") @@ -409,15 +428,21 @@ public void testImplicitFlowFailsInvalidIssuer() throws Exception { .notBeforeTime(Date.from(now().minusSeconds(200))) .claim("nonce", nonce) .subject(subject); - final Tuple tokens = buildTokens(idTokenBuilder.build(), key, jwk.getAlgorithm().getName(), keyId, - subject, true, false); + final Tuple tokens = buildTokens( + idTokenBuilder.build(), + key, + jwk.getAlgorithm().getName(), + keyId, + subject, + true, + false + ); final String responseUrl = buildAuthResponse(tokens.v2(), tokens.v1(), state, rpConfig.getRedirectUri()); final String authenticatingRealm = randomBoolean() ? REALM_NAME : null; final OpenIdConnectToken token = new OpenIdConnectToken(responseUrl, state, nonce, authenticatingRealm); final PlainActionFuture future = new PlainActionFuture<>(); authenticator.authenticate(token, future); - ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, - future::actionGet); + ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, future::actionGet); assertThat(e.getMessage(), containsString("Failed to parse or validate the ID Token")); assertThat(e.getCause(), instanceOf(BadJWTException.class)); assertThat(e.getCause().getMessage(), containsString("Unexpected JWT issuer")); @@ -444,8 +469,7 @@ public void testImplicitFlowFailsInvalidAudience() throws Exception { final Nonce nonce = new Nonce(); final String subject = "janedoe"; final String keyId = (jwk.getAlgorithm().getName().startsWith("HS")) ? null : jwk.getKeyID(); - JWTClaimsSet.Builder idTokenBuilder = new JWTClaimsSet.Builder() - .jwtID(randomAlphaOfLength(8)) + JWTClaimsSet.Builder idTokenBuilder = new JWTClaimsSet.Builder().jwtID(randomAlphaOfLength(8)) .audience("some-other-RP") .expirationTime(Date.from(now().plusSeconds(3600))) .issuer(opConfig.getIssuer().getValue()) @@ -453,15 +477,21 @@ public void testImplicitFlowFailsInvalidAudience() throws Exception { .notBeforeTime(Date.from(now().minusSeconds(80))) .claim("nonce", nonce) .subject(subject); - final Tuple tokens = buildTokens(idTokenBuilder.build(), key, jwk.getAlgorithm().getName(), keyId, - subject, true, false); + final Tuple tokens = buildTokens( + idTokenBuilder.build(), + key, + jwk.getAlgorithm().getName(), + keyId, + subject, + true, + false + ); final String responseUrl = buildAuthResponse(tokens.v2(), tokens.v1(), state, rpConfig.getRedirectUri()); final String authenticatingRealm = randomBoolean() ? REALM_NAME : null; final OpenIdConnectToken token = new OpenIdConnectToken(responseUrl, state, nonce, authenticatingRealm); final PlainActionFuture future = new PlainActionFuture<>(); authenticator.authenticate(token, future); - ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, - future::actionGet); + ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, future::actionGet); assertThat(e.getMessage(), containsString("Failed to parse or validate the ID Token")); assertThat(e.getCause(), instanceOf(BadJWTException.class)); assertThat(e.getCause().getMessage(), containsString("Unexpected JWT audience")); @@ -490,8 +520,7 @@ public void testAuthenticateImplicitFlowFailsWithForgedRsaIdToken() throws Excep final OpenIdConnectToken token = new OpenIdConnectToken(responseUrl, state, nonce, authenticatingRealm); final PlainActionFuture future = new PlainActionFuture<>(); authenticator.authenticate(token, future); - ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, - future::actionGet); + ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, future::actionGet); assertThat(e.getMessage(), containsString("Failed to parse or validate the ID Token")); assertThat(e.getCause(), instanceOf(BadJWSException.class)); assertThat(e.getCause().getMessage(), containsString("Signed JWT rejected: Invalid signature")); @@ -516,8 +545,7 @@ public void testAuthenticateImplicitFlowFailsWithForgedEcsdsaIdToken() throws Ex final OpenIdConnectToken token = new OpenIdConnectToken(responseUrl, state, nonce, authenticatingRealm); final PlainActionFuture future = new PlainActionFuture<>(); authenticator.authenticate(token, future); - ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, - future::actionGet); + ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, future::actionGet); assertThat(e.getMessage(), containsString("Failed to parse or validate the ID Token")); assertThat(e.getCause(), instanceOf(BadJWSException.class)); assertThat(e.getCause().getMessage(), containsString("Signed JWT rejected: Invalid signature")); @@ -541,8 +569,7 @@ public void testAuthenticateImplicitFlowFailsWithForgedHmacIdToken() throws Exce final OpenIdConnectToken token = new OpenIdConnectToken(responseUrl, state, nonce, authenticatingRealm); final PlainActionFuture future = new PlainActionFuture<>(); authenticator.authenticate(token, future); - ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, - future::actionGet); + ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, future::actionGet); assertThat(e.getMessage(), containsString("Failed to parse or validate the ID Token")); assertThat(e.getCause(), instanceOf(BadJWSException.class)); assertThat(e.getCause().getMessage(), containsString("Signed JWT rejected: Invalid signature")); @@ -566,14 +593,17 @@ public void testAuthenticateImplicitFlowFailsWithForgedAccessToken() throws Exce final String subject = "janedoe"; final String keyId = (jwk.getAlgorithm().getName().startsWith("HS")) ? null : jwk.getKeyID(); final Tuple tokens = buildTokens(nonce, key, jwk.getAlgorithm().getName(), keyId, subject, true, false); - final String responseUrl = buildAuthResponse(tokens.v2(), new BearerAccessToken("someforgedAccessToken"), state, - rpConfig.getRedirectUri()); + final String responseUrl = buildAuthResponse( + tokens.v2(), + new BearerAccessToken("someforgedAccessToken"), + state, + rpConfig.getRedirectUri() + ); final String authenticatingRealm = randomBoolean() ? REALM_NAME : null; final OpenIdConnectToken token = new OpenIdConnectToken(responseUrl, state, nonce, authenticatingRealm); final PlainActionFuture future = new PlainActionFuture<>(); authenticator.authenticate(token, future); - ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, - future::actionGet); + ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, future::actionGet); assertThat(e.getMessage(), containsString("Failed to verify access token")); assertThat(e.getCause(), instanceOf(InvalidHashException.class)); assertThat(e.getCause().getMessage(), containsString("Access token hash (at_hash) mismatch")); @@ -602,8 +632,7 @@ public void testImplicitFlowFailsWithNoneAlgorithm() throws Exception { String[] serializedParts = idToken.serialize().split("\\."); String legitimateHeader = new String(Base64.getUrlDecoder().decode(serializedParts[0]), StandardCharsets.UTF_8); String forgedHeader = legitimateHeader.replace(jwk.getAlgorithm().getName(), "NONE"); - String encodedForgedHeader = - Base64.getUrlEncoder().withoutPadding().encodeToString(forgedHeader.getBytes(StandardCharsets.UTF_8)); + String encodedForgedHeader = Base64.getUrlEncoder().withoutPadding().encodeToString(forgedHeader.getBytes(StandardCharsets.UTF_8)); String fordedTokenString = encodedForgedHeader + "." + serializedParts[1] + "." + serializedParts[2]; idToken = SignedJWT.parse(fordedTokenString); final String responseUrl = buildAuthResponse(idToken, tokens.v1(), state, rpConfig.getRedirectUri()); @@ -611,8 +640,7 @@ public void testImplicitFlowFailsWithNoneAlgorithm() throws Exception { final OpenIdConnectToken token = new OpenIdConnectToken(responseUrl, state, nonce, authenticatingRealm); final PlainActionFuture future = new PlainActionFuture<>(); authenticator.authenticate(token, future); - ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, - future::actionGet); + ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, future::actionGet); assertThat(e.getMessage(), containsString("Failed to parse or validate the ID Token")); assertThat(e.getCause(), instanceOf(BadJOSEException.class)); assertThat(e.getCause().getMessage(), containsString("Another algorithm expected, or no matching key(s) found")); @@ -634,17 +662,17 @@ public void testImplicitFlowFailsWithAlgorithmMixupAttack() throws Exception { final State state = new State(); final Nonce nonce = new Nonce(); final String subject = "janedoe"; - SecretKeySpec hmacKey = new SecretKeySpec("thisismysupersupersupersupersupersuperlongsecret".getBytes(StandardCharsets.UTF_8), - "HmacSha384"); - final Tuple tokens = buildTokens(nonce, hmacKey, "HS384", null, subject, - true, false); + SecretKeySpec hmacKey = new SecretKeySpec( + "thisismysupersupersupersupersupersuperlongsecret".getBytes(StandardCharsets.UTF_8), + "HmacSha384" + ); + final Tuple tokens = buildTokens(nonce, hmacKey, "HS384", null, subject, true, false); final String responseUrl = buildAuthResponse(tokens.v2(), tokens.v1(), state, rpConfig.getRedirectUri()); final String authenticatingRealm = randomBoolean() ? REALM_NAME : null; final OpenIdConnectToken token = new OpenIdConnectToken(responseUrl, state, nonce, authenticatingRealm); final PlainActionFuture future = new PlainActionFuture<>(); authenticator.authenticate(token, future); - ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, - future::actionGet); + ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, future::actionGet); assertThat(e.getMessage(), containsString("Failed to parse or validate the ID Token")); assertThat(e.getCause(), instanceOf(BadJOSEException.class)); assertThat(e.getCause().getMessage(), containsString("Another algorithm expected, or no matching key(s) found")); @@ -665,8 +693,7 @@ public void testImplicitFlowFailsWithUnsignedJwt() throws Exception { final State state = new State(); final Nonce nonce = new Nonce(); final String subject = "janedoe"; - JWTClaimsSet.Builder idTokenBuilder = new JWTClaimsSet.Builder() - .jwtID(randomAlphaOfLength(8)) + JWTClaimsSet.Builder idTokenBuilder = new JWTClaimsSet.Builder().jwtID(randomAlphaOfLength(8)) .audience(rpConfig.getClientId().getValue()) .expirationTime(Date.from(now().plusSeconds(3600))) .issuer(opConfig.getIssuer().getValue()) @@ -675,14 +702,12 @@ public void testImplicitFlowFailsWithUnsignedJwt() throws Exception { .claim("nonce", nonce) .subject(subject); - final String responseUrl = buildAuthResponse(new PlainJWT(idTokenBuilder.build()), null, state, - rpConfig.getRedirectUri()); + final String responseUrl = buildAuthResponse(new PlainJWT(idTokenBuilder.build()), null, state, rpConfig.getRedirectUri()); final String authenticatingRealm = randomBoolean() ? REALM_NAME : null; final OpenIdConnectToken token = new OpenIdConnectToken(responseUrl, state, nonce, authenticatingRealm); final PlainActionFuture future = new PlainActionFuture<>(); authenticator.authenticate(token, future); - ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, - future::actionGet); + ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, future::actionGet); assertThat(e.getMessage(), containsString("Failed to parse or validate the ID Token")); assertThat(e.getCause(), instanceOf(BadJWTException.class)); assertThat(e.getCause().getMessage(), containsString("Signed ID token expected")); @@ -700,15 +725,13 @@ public void testJsonObjectMerging() throws Exception { final JWK jwk = keyMaterial.v2().getKeys().get(0); RelyingPartyConfiguration rpConfig = getRpConfig(jwk.getAlgorithm().getName()); OpenIdConnectProviderConfiguration opConfig = getOpConfig(); - Map address = new JWTClaimsSet.Builder() - .claim("street_name", "12, Test St.") + Map address = new JWTClaimsSet.Builder().claim("street_name", "12, Test St.") .claim("locality", "New York") .claim("region", "NY") .claim("country", "USA") .build() .toJSONObject(); - Map idTokenObject = new JWTClaimsSet.Builder() - .jwtID(randomAlphaOfLength(8)) + Map idTokenObject = new JWTClaimsSet.Builder().jwtID(randomAlphaOfLength(8)) .audience(rpConfig.getClientId().getValue()) .expirationTime(Date.from(now().plusSeconds(3600))) .issuer(opConfig.getIssuer().getValue()) @@ -726,8 +749,7 @@ public void testJsonObjectMerging() throws Exception { .build() .toJSONObject(); - Map userinfoObject = new JWTClaimsSet.Builder() - .claim("given_name", "Jane Doe") + Map userinfoObject = new JWTClaimsSet.Builder().claim("given_name", "Jane Doe") .claim("family_name", "Doe") .claim("profile", "https://test-profiles.com/jane.doe") .claim("name", "Jane") @@ -754,8 +776,7 @@ public void testJsonObjectMerging() throws Exception { assertTrue(idTokenObject.containsKey("email")); // Claims with different types throw an error - Map wrongTypeInfo = new JWTClaimsSet.Builder() - .claim("given_name", "Jane Doe") + Map wrongTypeInfo = new JWTClaimsSet.Builder().claim("given_name", "Jane Doe") .claim("family_name", 123334434) .claim("profile", "https://test-profiles.com/jane.doe") .claim("name", "Jane") @@ -764,13 +785,13 @@ public void testJsonObjectMerging() throws Exception { .build() .toJSONObject(); - final IllegalStateException e = expectThrows(IllegalStateException.class, () -> { - OpenIdConnectAuthenticator.mergeObjects(idTokenObject, wrongTypeInfo); - }); + final IllegalStateException e = expectThrows( + IllegalStateException.class, + () -> { OpenIdConnectAuthenticator.mergeObjects(idTokenObject, wrongTypeInfo); } + ); // Userinfo Claims overwrite ID Token claims - Map overwriteUserInfo = new JWTClaimsSet.Builder() - .claim("given_name", "Jane Doe") + Map overwriteUserInfo = new JWTClaimsSet.Builder().claim("given_name", "Jane Doe") .claim("family_name", "Doe") .claim("profile", "https://test-profiles.com/jane.doe2") .claim("name", "Jane") @@ -784,8 +805,7 @@ public void testJsonObjectMerging() throws Exception { assertThat(idTokenObject.get("profile"), equalTo("https://test-profiles.com/jane.doe")); // Merging Arrays - Map userInfoWithRoles = new JWTClaimsSet.Builder() - .claim("given_name", "Jane Doe") + Map userInfoWithRoles = new JWTClaimsSet.Builder().claim("given_name", "Jane Doe") .claim("family_name", "Doe") .claim("profile", "https://test-profiles.com/jane.doe") .claim("name", "Jane") @@ -799,14 +819,12 @@ public void testJsonObjectMerging() throws Exception { assertThat((JSONArray) idTokenObject.get("roles"), containsInAnyOrder("role1", "role2", "role3", "role4", "role5")); // Merging nested objects - Map addressUserInfo = new JWTClaimsSet.Builder() - .claim("street_name", "12, Test St.") + Map addressUserInfo = new JWTClaimsSet.Builder().claim("street_name", "12, Test St.") .claim("locality", "New York") .claim("postal_code", "10024") .build() .toJSONObject(); - Map userInfoWithAddress = new JWTClaimsSet.Builder() - .claim("given_name", "Jane Doe") + Map userInfoWithAddress = new JWTClaimsSet.Builder().claim("given_name", "Jane Doe") .claim("family_name", "Doe") .claim("profile", "https://test-profiles.com/jane.doe") .claim("name", "Jane") @@ -829,15 +847,13 @@ public void testJsonObjectMerging() throws Exception { } public void testJsonObjectMergingWithBooleanLeniency() { - final Map idTokenObject = new JWTClaimsSet.Builder() - .claim("email_verified", true) + final Map idTokenObject = new JWTClaimsSet.Builder().claim("email_verified", true) .claim("email_verified_1", "true") .claim("email_verified_2", false) .claim("email_verified_3", "false") .build() .toJSONObject(); - final Map userInfoObject = new JWTClaimsSet.Builder() - .claim("email_verified", "true") + final Map userInfoObject = new JWTClaimsSet.Builder().claim("email_verified", "true") .claim("email_verified_1", true) .claim("email_verified_2", "false") .claim("email_verified_3", false) @@ -849,26 +865,16 @@ public void testJsonObjectMergingWithBooleanLeniency() { assertSame(Boolean.FALSE, idTokenObject.get("email_verified_2")); assertSame(Boolean.FALSE, idTokenObject.get("email_verified_3")); - final Map idTokenObject1 = new JWTClaimsSet.Builder() - .claim("email_verified", true) - .build() - .toJSONObject(); - final Map userInfoObject1 = new JWTClaimsSet.Builder() - .claim("email_verified", "false") - .build() - .toJSONObject(); - IllegalStateException e = - expectThrows(IllegalStateException.class, () -> OpenIdConnectAuthenticator.mergeObjects(idTokenObject1, userInfoObject1)); + final Map idTokenObject1 = new JWTClaimsSet.Builder().claim("email_verified", true).build().toJSONObject(); + final Map userInfoObject1 = new JWTClaimsSet.Builder().claim("email_verified", "false").build().toJSONObject(); + IllegalStateException e = expectThrows( + IllegalStateException.class, + () -> OpenIdConnectAuthenticator.mergeObjects(idTokenObject1, userInfoObject1) + ); assertThat(e.getMessage(), containsString("Cannot merge [java.lang.Boolean] with [java.lang.String]")); - final Map idTokenObject2 = new JWTClaimsSet.Builder() - .claim("email_verified", true) - .build() - .toJSONObject(); - final Map userInfoObject2 = new JWTClaimsSet.Builder() - .claim("email_verified", "yes") - .build() - .toJSONObject(); + final Map idTokenObject2 = new JWTClaimsSet.Builder().claim("email_verified", true).build().toJSONObject(); + final Map userInfoObject2 = new JWTClaimsSet.Builder().claim("email_verified", "yes").build().toJSONObject(); e = expectThrows(IllegalStateException.class, () -> OpenIdConnectAuthenticator.mergeObjects(idTokenObject2, userInfoObject2)); assertThat(e.getMessage(), containsString("Cannot merge [java.lang.Boolean] with [java.lang.String]")); } @@ -880,7 +886,8 @@ private OpenIdConnectProviderConfiguration getOpConfig() throws URISyntaxExcepti new URI("https://op.example.org/login"), new URI("https://op.example.org/token"), null, - new URI("https://op.example.org/logout")); + new URI("https://op.example.org/logout") + ); } private RelyingPartyConfiguration getDefaultRpConfig() throws URISyntaxException { @@ -893,7 +900,8 @@ private RelyingPartyConfiguration getDefaultRpConfig() throws URISyntaxException JWSAlgorithm.RS384, ClientAuthenticationMethod.CLIENT_SECRET_BASIC, JWSAlgorithm.HS384, - new URI("https://rp.elastic.co/successfull_logout")); + new URI("https://rp.elastic.co/successfull_logout") + ); } private RelyingPartyConfiguration getRpConfig(String alg) throws URISyntaxException { @@ -906,7 +914,8 @@ private RelyingPartyConfiguration getRpConfig(String alg) throws URISyntaxExcept JWSAlgorithm.parse(alg), ClientAuthenticationMethod.CLIENT_SECRET_BASIC, JWSAlgorithm.HS384, - new URI("https://rp.elastic.co/successfull_logout")); + new URI("https://rp.elastic.co/successfull_logout") + ); } private RelyingPartyConfiguration getRpConfigNoAccessToken(String alg) throws URISyntaxException { @@ -919,7 +928,8 @@ private RelyingPartyConfiguration getRpConfigNoAccessToken(String alg) throws UR JWSAlgorithm.parse(alg), ClientAuthenticationMethod.CLIENT_SECRET_BASIC, JWSAlgorithm.HS384, - new URI("https://rp.elastic.co/successfull_logout")); + new URI("https://rp.elastic.co/successfull_logout") + ); } private String buildAuthResponse(JWT idToken, @Nullable AccessToken accessToken, State state, URI redirectUri) { @@ -930,14 +940,14 @@ private String buildAuthResponse(JWT idToken, @Nullable AccessToken accessToken, accessToken, state, null, - null); + null + ); return response.toURI().toString(); } @SuppressWarnings("unchecked") private OpenIdConnectAuthenticator.ReloadableJWKSource mockSource(JWK jwk) { - OpenIdConnectAuthenticator.ReloadableJWKSource jwkSource = - mock(OpenIdConnectAuthenticator.ReloadableJWKSource.class); + OpenIdConnectAuthenticator.ReloadableJWKSource jwkSource = mock(OpenIdConnectAuthenticator.ReloadableJWKSource.class); when(jwkSource.get(any(), any())).thenReturn(Collections.singletonList(jwk)); Mockito.doAnswer(invocation -> { @SuppressWarnings("unchecked") @@ -949,8 +959,15 @@ private OpenIdConnectAuthenticator.ReloadableJWKSource mockSource(JWK jwk) { return jwkSource; } - private Tuple buildTokens(JWTClaimsSet idToken, Key key, String alg, String keyId, - String subject, boolean withAccessToken, boolean forged) throws Exception { + private Tuple buildTokens( + JWTClaimsSet idToken, + Key key, + String alg, + String keyId, + String subject, + boolean withAccessToken, + boolean forged + ) throws Exception { AccessToken accessToken = null; if (withAccessToken) { accessToken = new BearerAccessToken(Base64.getUrlEncoder().encodeToString(randomByteArrayOfLength(32))); @@ -961,9 +978,7 @@ private Tuple buildTokens(JWTClaimsSet idToken, Key key, Strin idTokenMap.put("nonce", idTokenMap.get("nonce").toString()); idToken = JWTClaimsSet.parse(idTokenMap); } - SignedJWT jwt = new SignedJWT( - new JWSHeader.Builder(JWSAlgorithm.parse(alg)).keyID(keyId).build(), - idToken); + SignedJWT jwt = new SignedJWT(new JWSHeader.Builder(JWSAlgorithm.parse(alg)).keyID(keyId).build(), idToken); if (key instanceof RSAPrivateKey) { jwt.sign(new RSASSASigner((PrivateKey) key)); @@ -977,20 +992,27 @@ private Tuple buildTokens(JWTClaimsSet idToken, Key key, Strin String[] serializedParts = jwt.serialize().split("\\."); String legitimatePayload = new String(Base64.getUrlDecoder().decode(serializedParts[1]), StandardCharsets.UTF_8); String forgedPayload = legitimatePayload.replace(subject, "attacker"); - String encodedForgedPayload = - Base64.getUrlEncoder().withoutPadding().encodeToString(forgedPayload.getBytes(StandardCharsets.UTF_8)); + String encodedForgedPayload = Base64.getUrlEncoder() + .withoutPadding() + .encodeToString(forgedPayload.getBytes(StandardCharsets.UTF_8)); String fordedTokenString = serializedParts[0] + "." + encodedForgedPayload + "." + serializedParts[2]; jwt = SignedJWT.parse(fordedTokenString); } return new Tuple<>(accessToken, jwt); } - private Tuple buildTokens(Nonce nonce, Key key, String alg, String keyId, String subject, boolean withAccessToken, - boolean forged) throws Exception { + private Tuple buildTokens( + Nonce nonce, + Key key, + String alg, + String keyId, + String subject, + boolean withAccessToken, + boolean forged + ) throws Exception { RelyingPartyConfiguration rpConfig = getRpConfig(alg); OpenIdConnectProviderConfiguration opConfig = getOpConfig(); - JWTClaimsSet.Builder idTokenBuilder = new JWTClaimsSet.Builder() - .jwtID(randomAlphaOfLength(8)) + JWTClaimsSet.Builder idTokenBuilder = new JWTClaimsSet.Builder().jwtID(randomAlphaOfLength(8)) .audience(rpConfig.getClientId().getValue()) .expirationTime(Date.from(now().plusSeconds(3600))) .issuer(opConfig.getIssuer().getValue()) @@ -1013,8 +1035,7 @@ private Tuple getRandomJwkForType(String type) throws Exception { gen.initialize(keySize); KeyPair keyPair = gen.generateKeyPair(); key = keyPair.getPrivate(); - jwk = new RSAKey.Builder((RSAPublicKey) keyPair.getPublic()) - .privateKey((RSAPrivateKey) keyPair.getPrivate()) + jwk = new RSAKey.Builder((RSAPublicKey) keyPair.getPublic()).privateKey((RSAPrivateKey) keyPair.getPrivate()) .keyUse(KeyUse.SIGNATURE) .keyID(UUID.randomUUID().toString()) .algorithm(JWSAlgorithm.parse(type + hashSize)) @@ -1022,12 +1043,13 @@ private Tuple getRandomJwkForType(String type) throws Exception { } else if (type.equals("HS")) { hashSize = randomFrom(256, 384); - SecretKeySpec hmacKey = new SecretKeySpec("thisismysupersupersupersupersupersuperlongsecret".getBytes(StandardCharsets.UTF_8), - "HmacSha" + hashSize); - //SecretKey hmacKey = KeyGenerator.getInstance("HmacSha" + hashSize).generateKey(); + SecretKeySpec hmacKey = new SecretKeySpec( + "thisismysupersupersupersupersupersuperlongsecret".getBytes(StandardCharsets.UTF_8), + "HmacSha" + hashSize + ); + // SecretKey hmacKey = KeyGenerator.getInstance("HmacSha" + hashSize).generateKey(); key = hmacKey; - jwk = new OctetSequenceKey.Builder(hmacKey) - .keyID(UUID.randomUUID().toString()) + jwk = new OctetSequenceKey.Builder(hmacKey).keyID(UUID.randomUUID().toString()) .algorithm(JWSAlgorithm.parse(type + hashSize)) .build(); @@ -1038,8 +1060,7 @@ private Tuple getRandomJwkForType(String type) throws Exception { gen.initialize(curve.toECParameterSpec()); KeyPair keyPair = gen.generateKeyPair(); key = keyPair.getPrivate(); - jwk = new ECKey.Builder(curve, (ECPublicKey) keyPair.getPublic()) - .privateKey((ECPrivateKey) keyPair.getPrivate()) + jwk = new ECKey.Builder(curve, (ECPublicKey) keyPair.getPublic()).privateKey((ECPrivateKey) keyPair.getPrivate()) .algorithm(JWSAlgorithm.parse(type + hashSize)) .build(); } else { diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectRealmSettingsTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectRealmSettingsTests.java index 479784e9cdd31..74381350d734b 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectRealmSettingsTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectRealmSettingsTests.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.xpack.security.authc.oidc; - import org.elasticsearch.common.settings.MockSecureSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsException; @@ -52,13 +51,19 @@ public void testAllSettings() { .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_CLIENT_ID), "rp-my") .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_RESPONSE_TYPE), "code") .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_REQUESTED_SCOPES), "openid") - .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_SIGNATURE_ALGORITHM), - randomFrom(OpenIdConnectRealmSettings.SUPPORTED_SIGNATURE_ALGORITHMS)) + .put( + getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_SIGNATURE_ALGORITHM), + randomFrom(OpenIdConnectRealmSettings.SUPPORTED_SIGNATURE_ALGORITHMS) + ) .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_POST_LOGOUT_REDIRECT_URI), "https://my.rp.com/logout") - .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_CLIENT_AUTH_METHOD), - randomFrom(OpenIdConnectRealmSettings.CLIENT_AUTH_METHODS)) - .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_CLIENT_AUTH_JWT_SIGNATURE_ALGORITHM), - randomFrom(OpenIdConnectRealmSettings.SUPPORTED_CLIENT_AUTH_JWT_ALGORITHMS)) + .put( + getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_CLIENT_AUTH_METHOD), + randomFrom(OpenIdConnectRealmSettings.CLIENT_AUTH_METHODS) + ) + .put( + getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_CLIENT_AUTH_JWT_SIGNATURE_ALGORITHM), + randomFrom(OpenIdConnectRealmSettings.SUPPORTED_CLIENT_AUTH_JWT_ALGORITHMS) + ) .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.HTTP_CONNECT_TIMEOUT), "5s") .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.HTTP_CONNECTION_READ_TIMEOUT), "5s") .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.HTTP_SOCKET_TIMEOUT), "5s") @@ -83,11 +88,14 @@ public void testIncorrectResponseTypeThrowsError() { .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_CLIENT_ID), "rp-my") .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_RESPONSE_TYPE), "hybrid"); settingsBuilder.setSecureSettings(getSecureSettings()); - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> { - new OpenIdConnectRealm(buildConfig(settingsBuilder.build()), null, null); - }); - assertThat(exception.getMessage(), Matchers.containsString(getFullSettingKey(REALM_NAME, - OpenIdConnectRealmSettings.RP_RESPONSE_TYPE))); + IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> { new OpenIdConnectRealm(buildConfig(settingsBuilder.build()), null, null); } + ); + assertThat( + exception.getMessage(), + Matchers.containsString(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_RESPONSE_TYPE)) + ); } public void testMissingAuthorizationEndpointThrowsError() { @@ -100,11 +108,14 @@ public void testMissingAuthorizationEndpointThrowsError() { .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_CLIENT_ID), "rp-my") .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_RESPONSE_TYPE), "code"); settingsBuilder.setSecureSettings(getSecureSettings()); - SettingsException exception = expectThrows(SettingsException.class, () -> { - new OpenIdConnectRealm(buildConfig(settingsBuilder.build()), null, null); - }); - assertThat(exception.getMessage(), - Matchers.containsString(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.OP_AUTHORIZATION_ENDPOINT))); + SettingsException exception = expectThrows( + SettingsException.class, + () -> { new OpenIdConnectRealm(buildConfig(settingsBuilder.build()), null, null); } + ); + assertThat( + exception.getMessage(), + Matchers.containsString(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.OP_AUTHORIZATION_ENDPOINT)) + ); } public void testInvalidAuthorizationEndpointThrowsError() { @@ -118,11 +129,14 @@ public void testInvalidAuthorizationEndpointThrowsError() { .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_CLIENT_ID), "rp-my") .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_RESPONSE_TYPE), "code"); settingsBuilder.setSecureSettings(getSecureSettings()); - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> { - new OpenIdConnectRealm(buildConfig(settingsBuilder.build()), null, null); - }); - assertThat(exception.getMessage(), - Matchers.containsString(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.OP_AUTHORIZATION_ENDPOINT))); + IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> { new OpenIdConnectRealm(buildConfig(settingsBuilder.build()), null, null); } + ); + assertThat( + exception.getMessage(), + Matchers.containsString(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.OP_AUTHORIZATION_ENDPOINT)) + ); } public void testMissingTokenEndpointThrowsErrorInCodeFlow() { @@ -135,11 +149,14 @@ public void testMissingTokenEndpointThrowsErrorInCodeFlow() { .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_CLIENT_ID), "rp-my") .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_RESPONSE_TYPE), "code"); settingsBuilder.setSecureSettings(getSecureSettings()); - SettingsException exception = expectThrows(SettingsException.class, () -> { - new OpenIdConnectRealm(buildConfig(settingsBuilder.build()), null, null); - }); - assertThat(exception.getMessage(), - Matchers.containsString(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.OP_TOKEN_ENDPOINT))); + SettingsException exception = expectThrows( + SettingsException.class, + () -> { new OpenIdConnectRealm(buildConfig(settingsBuilder.build()), null, null); } + ); + assertThat( + exception.getMessage(), + Matchers.containsString(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.OP_TOKEN_ENDPOINT)) + ); } public void testMissingTokenEndpointIsAllowedInImplicitFlow() { @@ -167,11 +184,14 @@ public void testInvalidTokenEndpointThrowsError() { .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_CLIENT_ID), "rp-my") .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_RESPONSE_TYPE), "code"); settingsBuilder.setSecureSettings(getSecureSettings()); - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> { - new OpenIdConnectRealm(buildConfig(settingsBuilder.build()), null, null); - }); - assertThat(exception.getMessage(), - Matchers.containsString(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.OP_TOKEN_ENDPOINT))); + IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> { new OpenIdConnectRealm(buildConfig(settingsBuilder.build()), null, null); } + ); + assertThat( + exception.getMessage(), + Matchers.containsString(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.OP_TOKEN_ENDPOINT)) + ); } public void testMissingJwksUrlThrowsError() { @@ -183,11 +203,14 @@ public void testMissingJwksUrlThrowsError() { .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_CLIENT_ID), "rp-my") .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_RESPONSE_TYPE), "code"); settingsBuilder.setSecureSettings(getSecureSettings()); - SettingsException exception = expectThrows(SettingsException.class, () -> { - new OpenIdConnectRealm(buildConfig(settingsBuilder.build()), null, null); - }); - assertThat(exception.getMessage(), - Matchers.containsString(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.OP_JWKSET_PATH))); + SettingsException exception = expectThrows( + SettingsException.class, + () -> { new OpenIdConnectRealm(buildConfig(settingsBuilder.build()), null, null); } + ); + assertThat( + exception.getMessage(), + Matchers.containsString(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.OP_JWKSET_PATH)) + ); } public void testMissingIssuerThrowsError() { @@ -200,11 +223,11 @@ public void testMissingIssuerThrowsError() { .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_CLIENT_ID), "rp-my") .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_RESPONSE_TYPE), "code"); settingsBuilder.setSecureSettings(getSecureSettings()); - SettingsException exception = expectThrows(SettingsException.class, () -> { - new OpenIdConnectRealm(buildConfig(settingsBuilder.build()), null, null); - }); - assertThat(exception.getMessage(), - Matchers.containsString(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.OP_ISSUER))); + SettingsException exception = expectThrows( + SettingsException.class, + () -> { new OpenIdConnectRealm(buildConfig(settingsBuilder.build()), null, null); } + ); + assertThat(exception.getMessage(), Matchers.containsString(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.OP_ISSUER))); } public void testMissingRedirectUriThrowsError() { @@ -217,11 +240,14 @@ public void testMissingRedirectUriThrowsError() { .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_CLIENT_ID), "rp-my") .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_RESPONSE_TYPE), "code"); settingsBuilder.setSecureSettings(getSecureSettings()); - SettingsException exception = expectThrows(SettingsException.class, () -> { - new OpenIdConnectRealm(buildConfig(settingsBuilder.build()), null, null); - }); - assertThat(exception.getMessage(), - Matchers.containsString(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_REDIRECT_URI))); + SettingsException exception = expectThrows( + SettingsException.class, + () -> { new OpenIdConnectRealm(buildConfig(settingsBuilder.build()), null, null); } + ); + assertThat( + exception.getMessage(), + Matchers.containsString(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_REDIRECT_URI)) + ); } public void testMissingClientIdThrowsError() { @@ -234,11 +260,11 @@ public void testMissingClientIdThrowsError() { .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_REDIRECT_URI), "https://rp.my.com") .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_RESPONSE_TYPE), "code"); settingsBuilder.setSecureSettings(getSecureSettings()); - SettingsException exception = expectThrows(SettingsException.class, () -> { - new OpenIdConnectRealm(buildConfig(settingsBuilder.build()), null, null); - }); - assertThat(exception.getMessage(), - Matchers.containsString(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_CLIENT_ID))); + SettingsException exception = expectThrows( + SettingsException.class, + () -> { new OpenIdConnectRealm(buildConfig(settingsBuilder.build()), null, null); } + ); + assertThat(exception.getMessage(), Matchers.containsString(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_CLIENT_ID))); } public void testMissingPrincipalClaimThrowsError() { @@ -250,14 +276,19 @@ public void testMissingPrincipalClaimThrowsError() { .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_REDIRECT_URI), "https://rp.my.com/cb") .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_CLIENT_ID), "rp-my") .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_RESPONSE_TYPE), "code") - .putList(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_REQUESTED_SCOPES), - Arrays.asList("openid", "scope1", "scope2")); + .putList( + getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_REQUESTED_SCOPES), + Arrays.asList("openid", "scope1", "scope2") + ); settingsBuilder.setSecureSettings(getSecureSettings()); - SettingsException exception = expectThrows(SettingsException.class, () -> { - new OpenIdConnectRealm(buildConfig(settingsBuilder.build()), null, null); - }); - assertThat(exception.getMessage(), - Matchers.containsString(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.PRINCIPAL_CLAIM.getClaim()))); + SettingsException exception = expectThrows( + SettingsException.class, + () -> { new OpenIdConnectRealm(buildConfig(settingsBuilder.build()), null, null); } + ); + assertThat( + exception.getMessage(), + Matchers.containsString(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.PRINCIPAL_CLAIM.getClaim())) + ); } public void testPatternWithoutSettingThrowsError() { @@ -271,16 +302,23 @@ public void testPatternWithoutSettingThrowsError() { .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_REDIRECT_URI), "https://rp.my.com/cb") .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_CLIENT_ID), "rp-my") .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_RESPONSE_TYPE), "code") - .putList(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_REQUESTED_SCOPES), - Arrays.asList("openid", "scope1", "scope2")); + .putList( + getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_REQUESTED_SCOPES), + Arrays.asList("openid", "scope1", "scope2") + ); settingsBuilder.setSecureSettings(getSecureSettings()); - SettingsException exception = expectThrows(SettingsException.class, () -> { - new OpenIdConnectRealm(buildConfig(settingsBuilder.build()), null, null); - }); - assertThat(exception.getMessage(), - Matchers.containsString(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.NAME_CLAIM.getClaim()))); - assertThat(exception.getMessage(), - Matchers.containsString(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.NAME_CLAIM.getPattern()))); + SettingsException exception = expectThrows( + SettingsException.class, + () -> { new OpenIdConnectRealm(buildConfig(settingsBuilder.build()), null, null); } + ); + assertThat( + exception.getMessage(), + Matchers.containsString(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.NAME_CLAIM.getClaim())) + ); + assertThat( + exception.getMessage(), + Matchers.containsString(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.NAME_CLAIM.getPattern())) + ); } public void testMissingClientSecretThrowsError() { @@ -293,11 +331,14 @@ public void testMissingClientSecretThrowsError() { .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_REDIRECT_URI), "https://rp.my.com") .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_CLIENT_ID), "rp-my") .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_RESPONSE_TYPE), "code"); - SettingsException exception = expectThrows(SettingsException.class, () -> { - new OpenIdConnectRealm(buildConfig(settingsBuilder.build()), null, null); - }); - assertThat(exception.getMessage(), - Matchers.containsString(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_CLIENT_SECRET))); + SettingsException exception = expectThrows( + SettingsException.class, + () -> { new OpenIdConnectRealm(buildConfig(settingsBuilder.build()), null, null); } + ); + assertThat( + exception.getMessage(), + Matchers.containsString(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_CLIENT_SECRET)) + ); } public void testInvalidProxySchemeThrowsError() { @@ -313,11 +354,14 @@ public void testInvalidProxySchemeThrowsError() { .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.HTTP_PROXY_HOST), "proxyhostname.org") .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.HTTP_PROXY_SCHEME), "invalid"); settingsBuilder.setSecureSettings(getSecureSettings()); - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> { - buildConfig(settingsBuilder.build()).getSetting(OpenIdConnectRealmSettings.HTTP_PROXY_SCHEME); - }); - assertThat(exception.getMessage(), - Matchers.containsString(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.HTTP_PROXY_SCHEME))); + IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> { buildConfig(settingsBuilder.build()).getSetting(OpenIdConnectRealmSettings.HTTP_PROXY_SCHEME); } + ); + assertThat( + exception.getMessage(), + Matchers.containsString(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.HTTP_PROXY_SCHEME)) + ); } public void testInvalidProxyPortThrowsError() { @@ -333,11 +377,14 @@ public void testInvalidProxyPortThrowsError() { .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.HTTP_PROXY_HOST), "proxyhostname.org") .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.HTTP_PROXY_PORT), 123456); settingsBuilder.setSecureSettings(getSecureSettings()); - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> { - buildConfig(settingsBuilder.build()).getSetting(OpenIdConnectRealmSettings.HTTP_PROXY_PORT); - }); - assertThat(exception.getMessage(), - Matchers.containsString(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.HTTP_PROXY_PORT))); + IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> { buildConfig(settingsBuilder.build()).getSetting(OpenIdConnectRealmSettings.HTTP_PROXY_PORT); } + ); + assertThat( + exception.getMessage(), + Matchers.containsString(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.HTTP_PROXY_PORT)) + ); } public void testInvalidProxyHostThrowsError() { @@ -353,14 +400,18 @@ public void testInvalidProxyHostThrowsError() { .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.HTTP_PROXY_HOST), "proxy hostname.org") .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.HTTP_PROXY_PORT), 8080); settingsBuilder.setSecureSettings(getSecureSettings()); - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> { - buildConfig(settingsBuilder.build()).getSetting(OpenIdConnectRealmSettings.HTTP_PROXY_HOST); - }); - assertThat(exception.getMessage(), Matchers.allOf( - Matchers.containsString(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.HTTP_PROXY_HOST)), - Matchers.containsString(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.HTTP_PROXY_PORT)), - Matchers.containsString(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.HTTP_PROXY_SCHEME)) - )); + IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> { buildConfig(settingsBuilder.build()).getSetting(OpenIdConnectRealmSettings.HTTP_PROXY_HOST); } + ); + assertThat( + exception.getMessage(), + Matchers.allOf( + Matchers.containsString(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.HTTP_PROXY_HOST)), + Matchers.containsString(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.HTTP_PROXY_PORT)), + Matchers.containsString(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.HTTP_PROXY_SCHEME)) + ) + ); } public void testInvalidClientAuthenticationMethodThrowsError() { @@ -375,11 +426,14 @@ public void testInvalidClientAuthenticationMethodThrowsError() { .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_CLIENT_AUTH_METHOD), "none") .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_RESPONSE_TYPE), "code"); settingsBuilder.setSecureSettings(getSecureSettings()); - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> { - new OpenIdConnectRealm(buildConfig(settingsBuilder.build()), null, null); - }); - assertThat(exception.getMessage(), - Matchers.containsString(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_CLIENT_AUTH_METHOD))); + IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> { new OpenIdConnectRealm(buildConfig(settingsBuilder.build()), null, null); } + ); + assertThat( + exception.getMessage(), + Matchers.containsString(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_CLIENT_AUTH_METHOD)) + ); } public void testInvalidClientAuthenticationJwtAlgorithmThrowsError() { @@ -395,17 +449,22 @@ public void testInvalidClientAuthenticationJwtAlgorithmThrowsError() { .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_CLIENT_AUTH_METHOD), "client_secret_jwt") .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_CLIENT_AUTH_JWT_SIGNATURE_ALGORITHM), "AB234"); settingsBuilder.setSecureSettings(getSecureSettings()); - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> { - new OpenIdConnectRealm(buildConfig(settingsBuilder.build()), null, null); - }); - assertThat(exception.getMessage(), - Matchers.containsString(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_CLIENT_AUTH_JWT_SIGNATURE_ALGORITHM))); + IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> { new OpenIdConnectRealm(buildConfig(settingsBuilder.build()), null, null); } + ); + assertThat( + exception.getMessage(), + Matchers.containsString(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_CLIENT_AUTH_JWT_SIGNATURE_ALGORITHM)) + ); } private MockSecureSettings getSecureSettings() { MockSecureSettings secureSettings = new MockSecureSettings(); - secureSettings.setString(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_CLIENT_SECRET), - randomAlphaOfLengthBetween(12, 18)); + secureSettings.setString( + getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_CLIENT_SECRET), + randomAlphaOfLengthBetween(12, 18) + ); return secureSettings; } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectRealmTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectRealmTests.java index 04d9dcbe15dbc..a2fc78ba86b5e 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectRealmTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectRealmTests.java @@ -87,7 +87,13 @@ public void testAuthentication() throws Exception { final boolean notPopulateMetadata = randomBoolean(); final String authenticatingRealm = randomBoolean() ? REALM_NAME : null; AuthenticationResult result = authenticateWithOidc( - principal, roleMapper, notPopulateMetadata, false, authenticatingRealm, null); + principal, + roleMapper, + notPopulateMetadata, + false, + authenticatingRealm, + null + ); assertThat(result, notNullValue()); assertThat(result.getStatus(), equalTo(AuthenticationResult.Status.SUCCESS)); assertThat(result.getValue().principal(), equalTo(principal)); @@ -112,14 +118,18 @@ public void testClaimPropertyMapping() throws Exception { AtomicReference userData = new AtomicReference<>(); doAnswer(getAnswer(userData)).when(roleMapper).resolveRoles(any(UserRoleMapper.UserData.class), anyActionListener()); Map claimsWithObject = Map.of( - "groups", List.of(Map.of("key1", List.of("value1", "value2")), Map.of("key2", List.of("value1", "value2"))) + "groups", + List.of(Map.of("key1", List.of("value1", "value2")), Map.of("key2", List.of("value1", "value2"))) + ); + Map claimsWithNumber = Map.of("groups", List.of(2, "value2")); + Exception e = expectThrows( + Exception.class, + () -> authenticateWithOidc(principal, roleMapper, false, false, REALM_NAME, claimsWithObject) + ); + Exception e2 = expectThrows( + Exception.class, + () -> authenticateWithOidc(principal, roleMapper, false, false, REALM_NAME, claimsWithNumber) ); - Map claimsWithNumber = Map.of( - "groups", List.of(2, "value2")); - Exception e = expectThrows(Exception.class, () -> authenticateWithOidc(principal, roleMapper, false, false, - REALM_NAME, claimsWithObject)); - Exception e2 = expectThrows(Exception.class, () -> authenticateWithOidc(principal, roleMapper, false, false, - REALM_NAME, claimsWithNumber)); assertThat(e.getCause().getMessage(), containsString("expects a claim with String or a String Array value")); assertThat(e2.getCause().getMessage(), containsString("expects a claim with String or a String Array value")); } @@ -130,14 +140,22 @@ public void testClaimMetadataMapping() throws Exception { AtomicReference userData = new AtomicReference<>(); doAnswer(getAnswer(userData)).when(roleMapper).resolveRoles(any(UserRoleMapper.UserData.class), anyActionListener()); Map claims = Map.of( - "string", "String", - "number", 232, - "boolean", true, - "string_array", List.of("one", "two", "three"), - "number_array", List.of(1, 2, 3), - "boolean_array", List.of(true, false, true), - "object", Map.of("key", List.of("value1", "value2")), - "object_array", List.of(Map.of("key1", List.of("value1", "value2")), Map.of("key2", List.of("value1", "value2"))) + "string", + "String", + "number", + 232, + "boolean", + true, + "string_array", + List.of("one", "two", "three"), + "number_array", + List.of(1, 2, 3), + "boolean_array", + List.of(true, false, true), + "object", + Map.of("key", List.of("value1", "value2")), + "object_array", + List.of(Map.of("key1", List.of("value1", "value2")), Map.of("key2", List.of("value1", "value2"))) ); AuthenticationResult result = authenticateWithOidc(principal, roleMapper, false, false, REALM_NAME, claims); assertThat(result, notNullValue()); @@ -183,10 +201,16 @@ public void testWithAuthorizingRealm() throws Exception { assertThat(tokenMetadata.get("id_token_hint"), equalTo("thisis.aserialized.jwt")); } - public void testAuthenticationWithWrongRealm() throws Exception{ + public void testAuthenticationWithWrongRealm() throws Exception { final String principal = randomAlphaOfLength(12); - AuthenticationResult result = authenticateWithOidc(principal, mock(UserRoleMapper.class), randomBoolean(), true, - REALM_NAME + randomAlphaOfLength(8), null); + AuthenticationResult result = authenticateWithOidc( + principal, + mock(UserRoleMapper.class), + randomBoolean(), + true, + REALM_NAME + randomAlphaOfLength(8), + null + ); assertThat(result, notNullValue()); assertThat(result.getStatus(), equalTo(AuthenticationResult.Status.CONTINUE)); } @@ -197,8 +221,7 @@ public void testClaimPatternParsing() throws Exception { final RealmConfig config = buildConfig(builder.build(), threadContext); final OpenIdConnectRealmSettings.ClaimSetting principalSetting = new OpenIdConnectRealmSettings.ClaimSetting("principal"); final OpenIdConnectRealm.ClaimParser parser = OpenIdConnectRealm.ClaimParser.forSetting(logger, principalSetting, config, true); - final JWTClaimsSet claims = new JWTClaimsSet.Builder() - .subject("OIDC-cbarton") + final JWTClaimsSet claims = new JWTClaimsSet.Builder().subject("OIDC-cbarton") .audience("https://rp.elastic.co/cb") .expirationTime(Date.from(now().plusSeconds(3600))) .issueTime(Date.from(now().minusSeconds(5))) @@ -216,8 +239,7 @@ public void testInvalidPrincipalClaimPatternParsing() { builder.put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.PRINCIPAL_CLAIM.getPattern()), "^OIDC-(.+)"); final RealmConfig config = buildConfig(builder.build(), threadContext); final OpenIdConnectRealm realm = new OpenIdConnectRealm(config, authenticator, null); - final JWTClaimsSet claims = new JWTClaimsSet.Builder() - .subject("cbarton@avengers.com") + final JWTClaimsSet claims = new JWTClaimsSet.Builder().subject("cbarton@avengers.com") .audience("https://rp.elastic.co/cb") .expirationTime(Date.from(now().plusSeconds(3600))) .issueTime(Date.from(now().minusSeconds(5))) @@ -250,17 +272,23 @@ public void testBuildRelyingPartyConfigWithoutOpenIdScope() { .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_REDIRECT_URI), "https://rp.my.com/cb") .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_CLIENT_ID), "rp-my") .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_RESPONSE_TYPE), "code") - .putList(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_REQUESTED_SCOPES), - Arrays.asList("scope1", "scope2")) + .putList(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_REQUESTED_SCOPES), Arrays.asList("scope1", "scope2")) .setSecureSettings(getSecureSettings()); - final OpenIdConnectRealm realm = new OpenIdConnectRealm(buildConfig(settingsBuilder.build(), threadContext), null, - null); + final OpenIdConnectRealm realm = new OpenIdConnectRealm(buildConfig(settingsBuilder.build(), threadContext), null, null); final OpenIdConnectPrepareAuthenticationResponse response = realm.buildAuthenticationRequestUri(null, null, null); final String state = response.getState(); final String nonce = response.getNonce(); - assertThat(response.getAuthenticationRequestUrl(), - equalTo("https://op.example.com/login?scope=scope1+scope2+openid&response_type=code" + - "&redirect_uri=https%3A%2F%2Frp.my.com%2Fcb&state=" + state + "&nonce=" + nonce + "&client_id=rp-my")); + assertThat( + response.getAuthenticationRequestUrl(), + equalTo( + "https://op.example.com/login?scope=scope1+scope2+openid&response_type=code" + + "&redirect_uri=https%3A%2F%2Frp.my.com%2Fcb&state=" + + state + + "&nonce=" + + nonce + + "&client_id=rp-my" + ) + ); assertThat(response.getRealmName(), equalTo(REALM_NAME)); } @@ -274,17 +302,26 @@ public void testBuildingAuthenticationRequest() { .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_REDIRECT_URI), "https://rp.my.com/cb") .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_CLIENT_ID), "rp-my") .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_RESPONSE_TYPE), "code") - .putList(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_REQUESTED_SCOPES), - Arrays.asList("openid", "scope1", "scope2")) + .putList( + getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_REQUESTED_SCOPES), + Arrays.asList("openid", "scope1", "scope2") + ) .setSecureSettings(getSecureSettings()); - final OpenIdConnectRealm realm = new OpenIdConnectRealm(buildConfig(settingsBuilder.build(), threadContext), null, - null); + final OpenIdConnectRealm realm = new OpenIdConnectRealm(buildConfig(settingsBuilder.build(), threadContext), null, null); final OpenIdConnectPrepareAuthenticationResponse response = realm.buildAuthenticationRequestUri(null, null, null); final String state = response.getState(); final String nonce = response.getNonce(); - assertThat(response.getAuthenticationRequestUrl(), - equalTo("https://op.example.com/login?scope=openid+scope1+scope2&response_type=code" + - "&redirect_uri=https%3A%2F%2Frp.my.com%2Fcb&state=" + state + "&nonce=" + nonce + "&client_id=rp-my")); + assertThat( + response.getAuthenticationRequestUrl(), + equalTo( + "https://op.example.com/login?scope=openid+scope1+scope2&response_type=code" + + "&redirect_uri=https%3A%2F%2Frp.my.com%2Fcb&state=" + + state + + "&nonce=" + + nonce + + "&client_id=rp-my" + ) + ); assertThat(response.getRealmName(), equalTo(REALM_NAME)); } @@ -300,19 +337,26 @@ public void testBuilidingAuthenticationRequestWithDefaultScope() { .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_RESPONSE_TYPE), "code") .setSecureSettings(getSecureSettings()); ; - final OpenIdConnectRealm realm = new OpenIdConnectRealm(buildConfig(settingsBuilder.build(), threadContext), null, - null); + final OpenIdConnectRealm realm = new OpenIdConnectRealm(buildConfig(settingsBuilder.build(), threadContext), null, null); final OpenIdConnectPrepareAuthenticationResponse response = realm.buildAuthenticationRequestUri(null, null, null); final String state = response.getState(); final String nonce = response.getNonce(); - assertThat(response.getAuthenticationRequestUrl(), equalTo("https://op.example.com/login?scope=openid&response_type=code" + - "&redirect_uri=https%3A%2F%2Frp.my.com%2Fcb&state=" + state + "&nonce=" + nonce + "&client_id=rp-my")); + assertThat( + response.getAuthenticationRequestUrl(), + equalTo( + "https://op.example.com/login?scope=openid&response_type=code" + + "&redirect_uri=https%3A%2F%2Frp.my.com%2Fcb&state=" + + state + + "&nonce=" + + nonce + + "&client_id=rp-my" + ) + ); assertThat(response.getRealmName(), equalTo(REALM_NAME)); } public void testBuildLogoutResponse() throws Exception { - final OpenIdConnectRealm realm = new OpenIdConnectRealm(buildConfig(getBasicRealmSettings().build(), threadContext), null, - null); + final OpenIdConnectRealm realm = new OpenIdConnectRealm(buildConfig(getBasicRealmSettings().build(), threadContext), null, null); // Random strings, as we will not validate the token here final JWT idToken = generateIdToken(randomAlphaOfLength(8), randomAlphaOfLength(8), randomAlphaOfLength(8)); final OpenIdConnectLogoutResponse logoutResponse = realm.buildLogoutResponse(idToken); @@ -327,10 +371,15 @@ public void testBuildLogoutResponse() throws Exception { public void testBuildLogoutResponseFromEndsessionEndpointWithExistingParameters() throws Exception { final Settings.Builder realmSettingsWithFunkyEndpoint = getBasicRealmSettings(); - realmSettingsWithFunkyEndpoint.put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.OP_ENDSESSION_ENDPOINT), - "https://op.example.org/logout?parameter=123"); - final OpenIdConnectRealm realm = new OpenIdConnectRealm(buildConfig(realmSettingsWithFunkyEndpoint.build(), threadContext), null, - null); + realmSettingsWithFunkyEndpoint.put( + getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.OP_ENDSESSION_ENDPOINT), + "https://op.example.org/logout?parameter=123" + ); + final OpenIdConnectRealm realm = new OpenIdConnectRealm( + buildConfig(realmSettingsWithFunkyEndpoint.build(), threadContext), + null, + null + ); // Random strings, as we will not validate the token here final JWT idToken = generateIdToken(randomAlphaOfLength(8), randomAlphaOfLength(8), randomAlphaOfLength(8)); @@ -357,14 +406,22 @@ public void testBuildingAuthenticationRequestWithExistingStateAndNonce() { .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_RESPONSE_TYPE), "code") .setSecureSettings(getSecureSettings()); ; - final OpenIdConnectRealm realm = new OpenIdConnectRealm(buildConfig(settingsBuilder.build(), threadContext), null, - null); + final OpenIdConnectRealm realm = new OpenIdConnectRealm(buildConfig(settingsBuilder.build(), threadContext), null, null); final String state = new State().getValue(); final String nonce = new Nonce().getValue(); final OpenIdConnectPrepareAuthenticationResponse response = realm.buildAuthenticationRequestUri(state, nonce, null); - assertThat(response.getAuthenticationRequestUrl(), equalTo("https://op.example.com/login?scope=openid&response_type=code" + - "&redirect_uri=https%3A%2F%2Frp.my.com%2Fcb&state=" + state + "&nonce=" + nonce + "&client_id=rp-my")); + assertThat( + response.getAuthenticationRequestUrl(), + equalTo( + "https://op.example.com/login?scope=openid&response_type=code" + + "&redirect_uri=https%3A%2F%2Frp.my.com%2Fcb&state=" + + state + + "&nonce=" + + nonce + + "&client_id=rp-my" + ) + ); assertThat(response.getRealmName(), equalTo(REALM_NAME)); } @@ -380,48 +437,71 @@ public void testBuildingAuthenticationRequestWithLoginHint() { .put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_RESPONSE_TYPE), "code") .setSecureSettings(getSecureSettings()); ; - final OpenIdConnectRealm realm = new OpenIdConnectRealm(buildConfig(settingsBuilder.build(), threadContext), null, - null); + final OpenIdConnectRealm realm = new OpenIdConnectRealm(buildConfig(settingsBuilder.build(), threadContext), null, null); final String state = new State().getValue(); final String nonce = new Nonce().getValue(); final String thehint = randomAlphaOfLength(8); final OpenIdConnectPrepareAuthenticationResponse response = realm.buildAuthenticationRequestUri(state, nonce, thehint); - assertThat(response.getAuthenticationRequestUrl(), equalTo("https://op.example.com/login?login_hint=" + thehint + - "&scope=openid&response_type=code&redirect_uri=https%3A%2F%2Frp.my.com%2Fcb&state=" + - state + "&nonce=" + nonce + "&client_id=rp-my")); + assertThat( + response.getAuthenticationRequestUrl(), + equalTo( + "https://op.example.com/login?login_hint=" + + thehint + + "&scope=openid&response_type=code&redirect_uri=https%3A%2F%2Frp.my.com%2Fcb&state=" + + state + + "&nonce=" + + nonce + + "&client_id=rp-my" + ) + ); assertThat(response.getRealmName(), equalTo(REALM_NAME)); } - private AuthenticationResult authenticateWithOidc(String principal, UserRoleMapper roleMapper, boolean notPopulateMetadata, - boolean useAuthorizingRealm, String authenticatingRealm, - @Nullable Map additionalClaims) - throws Exception { + private AuthenticationResult authenticateWithOidc( + String principal, + UserRoleMapper roleMapper, + boolean notPopulateMetadata, + boolean useAuthorizingRealm, + String authenticatingRealm, + @Nullable Map additionalClaims + ) throws Exception { RealmConfig.RealmIdentifier realmIdentifier = new RealmConfig.RealmIdentifier("mock", "mock_lookup"); final MockLookupRealm lookupRealm = new MockLookupRealm( - new RealmConfig(realmIdentifier, - Settings.builder().put(globalSettings) - .put(getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0).build(), - env, threadContext)); + new RealmConfig( + realmIdentifier, + Settings.builder().put(globalSettings).put(getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0).build(), + env, + threadContext + ) + ); final OpenIdConnectAuthenticator authenticator = mock(OpenIdConnectAuthenticator.class); final Settings.Builder builder = getBasicRealmSettings(); if (notPopulateMetadata) { - builder.put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.POPULATE_USER_METADATA), - false); + builder.put(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.POPULATE_USER_METADATA), false); } if (useAuthorizingRealm) { - builder.putList(getFullSettingKey(new RealmConfig.RealmIdentifier("oidc", REALM_NAME), - DelegatedAuthorizationSettings.AUTHZ_REALMS), lookupRealm.name()); - lookupRealm.registerUser(new User(principal, new String[]{"lookup_user_role"}, "Clinton Barton", "cbarton@shield.gov", - Collections.singletonMap("is_lookup", true), true)); + builder.putList( + getFullSettingKey(new RealmConfig.RealmIdentifier("oidc", REALM_NAME), DelegatedAuthorizationSettings.AUTHZ_REALMS), + lookupRealm.name() + ); + lookupRealm.registerUser( + new User( + principal, + new String[] { "lookup_user_role" }, + "Clinton Barton", + "cbarton@shield.gov", + Collections.singletonMap("is_lookup", true), + true + ) + ); } final RealmConfig config = buildConfig(builder.build(), threadContext); final OpenIdConnectRealm realm = new OpenIdConnectRealm(config, authenticator, roleMapper); initializeRealms(realm, lookupRealm); final OpenIdConnectToken token = new OpenIdConnectToken("", new State(), new Nonce(), authenticatingRealm); - final JWTClaimsSet.Builder claimsBuilder = new JWTClaimsSet.Builder() - .subject(principal) + final JWTClaimsSet.Builder claimsBuilder = new JWTClaimsSet.Builder().subject(principal) .audience("https://rp.elastic.co/cb") .expirationTime(Date.from(now().plusSeconds(3600))) .issueTime(Date.from(now().minusSeconds(5))) diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectTestCase.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectTestCase.java index 3e3c01061d51d..5c161a516a258 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectTestCase.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectTestCase.java @@ -13,6 +13,7 @@ import com.nimbusds.jwt.JWTClaimsSet; import com.nimbusds.jwt.SignedJWT; import com.nimbusds.openid.connect.sdk.Nonce; + import org.elasticsearch.common.settings.MockSecureSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; @@ -61,8 +62,10 @@ protected static Settings.Builder getBasicRealmSettings() { protected static MockSecureSettings getSecureSettings() { MockSecureSettings secureSettings = new MockSecureSettings(); - secureSettings.setString(getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_CLIENT_SECRET), - randomAlphaOfLengthBetween(12, 18)); + secureSettings.setString( + getFullSettingKey(REALM_NAME, OpenIdConnectRealmSettings.RP_CLIENT_SECRET), + randomAlphaOfLengthBetween(12, 18) + ); return secureSettings; } @@ -72,8 +75,7 @@ protected JWT generateIdToken(String subject, String audience, String issuer) th KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA"); gen.initialize(keySize); KeyPair keyPair = gen.generateKeyPair(); - JWTClaimsSet idTokenClaims = new JWTClaimsSet.Builder() - .jwtID(randomAlphaOfLength(8)) + JWTClaimsSet idTokenClaims = new JWTClaimsSet.Builder().jwtID(randomAlphaOfLength(8)) .audience(audience) .expirationTime(Date.from(now().plusSeconds(3600))) .issuer(issuer) @@ -83,9 +85,7 @@ protected JWT generateIdToken(String subject, String audience, String issuer) th .subject(subject) .build(); - SignedJWT jwt = new SignedJWT( - new JWSHeader.Builder(JWSAlgorithm.parse("RS" + hashSize)).build(), - idTokenClaims); + SignedJWT jwt = new SignedJWT(new JWSHeader.Builder(JWSAlgorithm.parse("RS" + hashSize)).build(), idTokenClaims); jwt.sign(new RSASSASigner(keyPair.getPrivate())); return jwt; } @@ -102,26 +102,29 @@ protected RealmConfig buildConfig(Settings realmSettings, ThreadContext threadCo } public static void writeJwkSetToFile(Path file) throws IOException { - Files.write(file, Arrays.asList( - "{\n" + - " \"keys\": [\n" + - " {\n" + - " \"kty\": \"RSA\",\n" + - " \"d\": \"lT2V49RNsu0eTroQDqFCiHY-CkPWdKfKAf66sJrWPNpSX8URa6pTCruFQMsb9ZSqQ8eIvqys9I9rq6Wpaxn1aGRahVzxp7nsBPZYw" + - "SY09LRzhvAxJwWdwtF-ogrV5-p99W9mhEa0khot3myzzfWNnGzcf1IudqvkqE9zrlUJg-kvA3icbs6HgaZVAevb_mx-bgbtJdnUxyPGwXLyQ7g6hlntQ" + - "R_vpzTnK7XFU6fvkrojh7UPJkanKAH0gf3qPrB-Y2gQML7RSlKo-ZfJNHa83G4NRLHKuWTI6dSKJlqmS9zWGmyC3dx5kGjgqD6YgwtWlip8q-U839zxt" + - "z25yeslsQ\",\n" + - " \"e\": \"AQAB\",\n" + - " \"use\": \"sig\",\n" + - " \"kid\": \"testkey\",\n" + - " \"alg\": \"RS256\",\n" + - " \"n\": \"lXBe4UngWJiUfbqbeOvwbH04kYLCpeH4k0o3ngScZDo6ydc_gBDEVwPLQpi8D930aIzr3XHP3RCj0hnpxUun7MNMhWxJZVOd1eg5u" + - "uO-nPIhkqr9iGKV5srJk0Dvw0wBaGZuXMBheY2ViNaKTR9EEtjNwU2d2-I5U3YlrnFR6nj-Pn_hWaiCbb_pSFM4w9QpoLDmuwMRanHY_YK7Td2WMICSG" + - "P3IRGmbecRZCqgkWVZk396EMoMLNxi8WcErYknyY9r-QeJMruRkr27kgx78L7KZ9uBmu9oKXRQl15ZDYe7Bnt9E5wSdOCV9R9h5VRVUur-_129XkDeAX" + - "-6re63_Mw\"\n" + - " }\n" + - " ]\n" + - "}" - )); + Files.write( + file, + Arrays.asList( + "{\n" + + " \"keys\": [\n" + + " {\n" + + " \"kty\": \"RSA\",\n" + + " \"d\": \"lT2V49RNsu0eTroQDqFCiHY-CkPWdKfKAf66sJrWPNpSX8URa6pTCruFQMsb9ZSqQ8eIvqys9I9rq6Wpaxn1aGRahVzxp7nsBPZYw" + + "SY09LRzhvAxJwWdwtF-ogrV5-p99W9mhEa0khot3myzzfWNnGzcf1IudqvkqE9zrlUJg-kvA3icbs6HgaZVAevb_mx-bgbtJdnUxyPGwXLyQ7g6hlntQ" + + "R_vpzTnK7XFU6fvkrojh7UPJkanKAH0gf3qPrB-Y2gQML7RSlKo-ZfJNHa83G4NRLHKuWTI6dSKJlqmS9zWGmyC3dx5kGjgqD6YgwtWlip8q-U839zxt" + + "z25yeslsQ\",\n" + + " \"e\": \"AQAB\",\n" + + " \"use\": \"sig\",\n" + + " \"kid\": \"testkey\",\n" + + " \"alg\": \"RS256\",\n" + + " \"n\": \"lXBe4UngWJiUfbqbeOvwbH04kYLCpeH4k0o3ngScZDo6ydc_gBDEVwPLQpi8D930aIzr3XHP3RCj0hnpxUun7MNMhWxJZVOd1eg5u" + + "uO-nPIhkqr9iGKV5srJk0Dvw0wBaGZuXMBheY2ViNaKTR9EEtjNwU2d2-I5U3YlrnFR6nj-Pn_hWaiCbb_pSFM4w9QpoLDmuwMRanHY_YK7Td2WMICSG" + + "P3IRGmbecRZCqgkWVZk396EMoMLNxi8WcErYknyY9r-QeJMruRkr27kgx78L7KZ9uBmu9oKXRQl15ZDYe7Bnt9E5wSdOCV9R9h5VRVUur-_129XkDeAX" + + "-6re63_Mw\"\n" + + " }\n" + + " ]\n" + + "}" + ) + ); } } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/pki/PkiRealmTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/pki/PkiRealmTests.java index 50a8f08c82160..66ae2c035238f 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/pki/PkiRealmTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/pki/PkiRealmTests.java @@ -49,6 +49,7 @@ import java.util.Map; import java.util.Set; import java.util.regex.Pattern; + import javax.security.auth.x500.X500Principal; import static org.elasticsearch.test.ActionListenerUtils.anyActionListener; @@ -78,34 +79,44 @@ public class PkiRealmTests extends ESTestCase { public void setup() throws Exception { RealmConfig.RealmIdentifier realmIdentifier = new RealmConfig.RealmIdentifier(PkiRealmSettings.TYPE, REALM_NAME); globalSettings = Settings.builder() - .put("path.home", createTempDir()) - .put(RealmSettings.getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0) - .build(); + .put("path.home", createTempDir()) + .put(RealmSettings.getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0) + .build(); licenseState = mock(MockLicenseState.class); when(licenseState.isAllowed(Security.DELEGATED_AUTHORIZATION_FEATURE)).thenReturn(true); } public void testTokenSupport() throws Exception { - RealmConfig config = new RealmConfig(new RealmConfig.RealmIdentifier(PkiRealmSettings.TYPE, REALM_NAME), + RealmConfig config = new RealmConfig( + new RealmConfig.RealmIdentifier(PkiRealmSettings.TYPE, REALM_NAME), globalSettings, - TestEnvironment.newEnvironment(globalSettings), new ThreadContext(globalSettings)); + TestEnvironment.newEnvironment(globalSettings), + new ThreadContext(globalSettings) + ); PkiRealm realm = new PkiRealm(config, mock(UserRoleMapper.class)); assertRealmUsageStats(realm, false, false, true, false); assertThat(realm.supports(null), is(false)); assertThat(realm.supports(new UsernamePasswordToken("", new SecureString(new char[0]))), is(false)); X509AuthenticationToken token = randomBoolean() - ? X509AuthenticationToken.delegated(new X509Certificate[0], mock(Authentication.class)) - : new X509AuthenticationToken(new X509Certificate[0]); + ? X509AuthenticationToken.delegated(new X509Certificate[0], mock(Authentication.class)) + : new X509AuthenticationToken(new X509Certificate[0]); assertThat(realm.supports(token), is(true)); } public void testExtractToken() throws Exception { X509Certificate certificate = readCert(getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt")); ThreadContext threadContext = new ThreadContext(Settings.EMPTY); - threadContext.putTransient(PkiRealm.PKI_CERT_HEADER_NAME, new X509Certificate[]{certificate}); - PkiRealm realm = new PkiRealm(new RealmConfig(new RealmConfig.RealmIdentifier(PkiRealmSettings.TYPE, REALM_NAME), globalSettings, - TestEnvironment.newEnvironment(globalSettings), threadContext), mock(UserRoleMapper.class)); + threadContext.putTransient(PkiRealm.PKI_CERT_HEADER_NAME, new X509Certificate[] { certificate }); + PkiRealm realm = new PkiRealm( + new RealmConfig( + new RealmConfig.RealmIdentifier(PkiRealmSettings.TYPE, REALM_NAME), + globalSettings, + TestEnvironment.newEnvironment(globalSettings), + threadContext + ), + mock(UserRoleMapper.class) + ); X509AuthenticationToken token = realm.token(threadContext); assertThat(token, is(notNullValue())); @@ -130,8 +141,11 @@ private void assertSuccessfulAuthentication(Set roles) throws Exception PkiRealm realm = buildRealm(roleMapper, globalSettings); verify(roleMapper).refreshRealmOnChange(realm); - final String expectedUsername = PkiRealm.getPrincipalFromSubjectDN(Pattern.compile(PkiRealmSettings.DEFAULT_USERNAME_PATTERN), - token, NoOpLogger.INSTANCE); + final String expectedUsername = PkiRealm.getPrincipalFromSubjectDN( + Pattern.compile(PkiRealmSettings.DEFAULT_USERNAME_PATTERN), + token, + NoOpLogger.INSTANCE + ); final AuthenticationResult result = authenticate(token, realm); assertThat(result.getStatus(), is(AuthenticationResult.Status.SUCCESS)); User user = result.getValue(); @@ -189,8 +203,12 @@ private UserRoleMapper buildRoleMapper(Set roles, String dn) { } private PkiRealm buildRealm(UserRoleMapper roleMapper, Settings settings, Realm... otherRealms) { - final RealmConfig config = new RealmConfig(new RealmConfig.RealmIdentifier(PkiRealmSettings.TYPE, REALM_NAME), settings, - TestEnvironment.newEnvironment(settings), new ThreadContext(settings)); + final RealmConfig config = new RealmConfig( + new RealmConfig.RealmIdentifier(PkiRealmSettings.TYPE, REALM_NAME), + settings, + TestEnvironment.newEnvironment(settings), + new ThreadContext(settings) + ); PkiRealm realm = new PkiRealm(config, roleMapper); List allRealms = CollectionUtils.arrayAsArrayList(otherRealms); allRealms.add(realm); @@ -201,7 +219,7 @@ private PkiRealm buildRealm(UserRoleMapper roleMapper, Settings settings, Realm. private X509AuthenticationToken buildToken() throws Exception { X509Certificate certificate = readCert(getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt")); - return new X509AuthenticationToken(new X509Certificate[]{certificate}); + return new X509AuthenticationToken(new X509Certificate[] { certificate }); } private AuthenticationResult authenticate(X509AuthenticationToken token, PkiRealm realm) { @@ -212,9 +230,9 @@ private AuthenticationResult authenticate(X509AuthenticationToken token, P public void testCustomUsernamePatternMatches() throws Exception { final Settings settings = Settings.builder() - .put(globalSettings) - .put("xpack.security.authc.realms.pki.my_pki.username_pattern", "OU=(.*?),") - .build(); + .put(globalSettings) + .put("xpack.security.authc.realms.pki.my_pki.username_pattern", "OU=(.*?),") + .build(); ThreadContext threadContext = new ThreadContext(settings); X509Certificate certificate = readCert(getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt")); UserRoleMapper roleMapper = buildRoleMapper(); @@ -232,9 +250,9 @@ public void testCustomUsernamePatternMatches() throws Exception { public void testCustomUsernamePatternMismatchesAndNullToken() throws Exception { final Settings settings = Settings.builder() - .put(globalSettings) - .put("xpack.security.authc.realms.pki.my_pki.username_pattern", "OU=(mismatch.*?),") - .build(); + .put(globalSettings) + .put("xpack.security.authc.realms.pki.my_pki.username_pattern", "OU=(mismatch.*?),") + .build(); ThreadContext threadContext = new ThreadContext(settings); X509Certificate certificate = readCert(getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt")); UserRoleMapper roleMapper = buildRoleMapper(); @@ -254,11 +272,13 @@ public void testVerificationUsingATruststore() throws Exception { MockSecureSettings secureSettings = new MockSecureSettings(); secureSettings.setString("xpack.security.authc.realms.pki.my_pki.truststore.secure_password", "testnode"); Settings settings = Settings.builder() - .put(globalSettings) - .put("xpack.security.authc.realms.pki.my_pki.truststore.path", - getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.jks")) - .setSecureSettings(secureSettings) - .build(); + .put(globalSettings) + .put( + "xpack.security.authc.realms.pki.my_pki.truststore.path", + getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.jks") + ) + .setSecureSettings(secureSettings) + .build(); ThreadContext threadContext = new ThreadContext(globalSettings); PkiRealm realm = buildRealm(roleMapper, settings); assertRealmUsageStats(realm, true, false, true, false); @@ -280,9 +300,9 @@ public void testVerificationUsingCertificateAuthorities() throws Exception { UserRoleMapper roleMapper = buildRoleMapper(); Settings settings = Settings.builder() - .put(globalSettings) - .putList("xpack.security.authc.realms.pki.my_pki.certificate_authorities", caPath.toString()) - .build(); + .put(globalSettings) + .putList("xpack.security.authc.realms.pki.my_pki.certificate_authorities", caPath.toString()) + .build(); ThreadContext threadContext = new ThreadContext(globalSettings); PkiRealm realm = buildRealm(roleMapper, settings); assertRealmUsageStats(realm, true, false, true, false); @@ -300,33 +320,59 @@ public void testVerificationUsingCertificateAuthorities() throws Exception { public void testAuthenticationDelegationFailsWithoutTokenServiceAndTruststore() throws Exception { ThreadContext threadContext = new ThreadContext(Settings.EMPTY); Settings settings = Settings.builder() - .put(globalSettings) - .put("xpack.security.authc.realms.pki.my_pki.delegation.enabled", true) - .build(); - IllegalStateException e = expectThrows(IllegalStateException.class, - () -> new PkiRealm(new RealmConfig(new RealmConfig.RealmIdentifier(PkiRealmSettings.TYPE, REALM_NAME), settings, - TestEnvironment.newEnvironment(globalSettings), threadContext), mock(UserRoleMapper.class))); - assertThat(e.getMessage(), - is("PKI realms with delegation enabled require a trust configuration " - + "(xpack.security.authc.realms.pki.my_pki.certificate_authorities or " - + "xpack.security.authc.realms.pki.my_pki.truststore.path)" - + " and that the token service be also enabled (xpack.security.authc.token.enabled)")); + .put(globalSettings) + .put("xpack.security.authc.realms.pki.my_pki.delegation.enabled", true) + .build(); + IllegalStateException e = expectThrows( + IllegalStateException.class, + () -> new PkiRealm( + new RealmConfig( + new RealmConfig.RealmIdentifier(PkiRealmSettings.TYPE, REALM_NAME), + settings, + TestEnvironment.newEnvironment(globalSettings), + threadContext + ), + mock(UserRoleMapper.class) + ) + ); + assertThat( + e.getMessage(), + is( + "PKI realms with delegation enabled require a trust configuration " + + "(xpack.security.authc.realms.pki.my_pki.certificate_authorities or " + + "xpack.security.authc.realms.pki.my_pki.truststore.path)" + + " and that the token service be also enabled (xpack.security.authc.token.enabled)" + ) + ); } public void testAuthenticationDelegationFailsWithoutTruststore() throws Exception { ThreadContext threadContext = new ThreadContext(Settings.EMPTY); Settings settings = Settings.builder() - .put(globalSettings) - .put("xpack.security.authc.realms.pki.my_pki.delegation.enabled", true) - .put("xpack.security.authc.token.enabled", true) - .build(); - IllegalStateException e = expectThrows(IllegalStateException.class, - () -> new PkiRealm(new RealmConfig(new RealmConfig.RealmIdentifier(PkiRealmSettings.TYPE, REALM_NAME), settings, - TestEnvironment.newEnvironment(globalSettings), threadContext), mock(UserRoleMapper.class))); - assertThat(e.getMessage(), - is("PKI realms with delegation enabled require a trust configuration " - + "(xpack.security.authc.realms.pki.my_pki.certificate_authorities " - + "or xpack.security.authc.realms.pki.my_pki.truststore.path)")); + .put(globalSettings) + .put("xpack.security.authc.realms.pki.my_pki.delegation.enabled", true) + .put("xpack.security.authc.token.enabled", true) + .build(); + IllegalStateException e = expectThrows( + IllegalStateException.class, + () -> new PkiRealm( + new RealmConfig( + new RealmConfig.RealmIdentifier(PkiRealmSettings.TYPE, REALM_NAME), + settings, + TestEnvironment.newEnvironment(globalSettings), + threadContext + ), + mock(UserRoleMapper.class) + ) + ); + assertThat( + e.getMessage(), + is( + "PKI realms with delegation enabled require a trust configuration " + + "(xpack.security.authc.realms.pki.my_pki.certificate_authorities " + + "or xpack.security.authc.realms.pki.my_pki.truststore.path)" + ) + ); } public void testAuthenticationDelegationSuccess() throws Exception { @@ -339,20 +385,24 @@ public void testAuthenticationDelegationSuccess() throws Exception { when(mockRealmRef.getName()).thenReturn("mockup_delegate_realm"); when(mockAuthentication.getUser()).thenReturn(mockUser); when(mockAuthentication.getAuthenticatedBy()).thenReturn(mockRealmRef); - X509AuthenticationToken delegatedToken = X509AuthenticationToken.delegated(new X509Certificate[] { certificate }, - mockAuthentication); + X509AuthenticationToken delegatedToken = X509AuthenticationToken.delegated( + new X509Certificate[] { certificate }, + mockAuthentication + ); UserRoleMapper roleMapper = buildRoleMapper(); MockSecureSettings secureSettings = new MockSecureSettings(); secureSettings.setString("xpack.security.authc.realms.pki.my_pki.truststore.secure_password", "testnode"); Settings settings = Settings.builder() - .put(globalSettings) - .put("xpack.security.authc.realms.pki.my_pki.truststore.path", - getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.jks")) - .put("xpack.security.authc.realms.pki.my_pki.delegation.enabled", true) - .put("xpack.security.authc.token.enabled", true) - .setSecureSettings(secureSettings) - .build(); + .put(globalSettings) + .put( + "xpack.security.authc.realms.pki.my_pki.truststore.path", + getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.jks") + ) + .put("xpack.security.authc.realms.pki.my_pki.delegation.enabled", true) + .put("xpack.security.authc.token.enabled", true) + .setSecureSettings(secureSettings) + .build(); PkiRealm realmWithDelegation = buildRealm(roleMapper, settings); assertRealmUsageStats(realmWithDelegation, true, false, true, true); @@ -369,18 +419,22 @@ public void testAuthenticationDelegationSuccess() throws Exception { public void testAuthenticationDelegationFailure() throws Exception { assumeFalse("Can't run in a FIPS JVM, JKS keystores can't be used", inFipsJvm()); X509Certificate certificate = readCert(getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt")); - X509AuthenticationToken delegatedToken = X509AuthenticationToken.delegated(new X509Certificate[] { certificate }, - mock(Authentication.class)); + X509AuthenticationToken delegatedToken = X509AuthenticationToken.delegated( + new X509Certificate[] { certificate }, + mock(Authentication.class) + ); UserRoleMapper roleMapper = buildRoleMapper(); MockSecureSettings secureSettings = new MockSecureSettings(); secureSettings.setString("xpack.security.authc.realms.pki.my_pki.truststore.secure_password", "testnode"); Settings settings = Settings.builder() - .put(globalSettings) - .put("xpack.security.authc.realms.pki.my_pki.truststore.path", - getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.jks")) - .setSecureSettings(secureSettings) - .build(); + .put(globalSettings) + .put( + "xpack.security.authc.realms.pki.my_pki.truststore.path", + getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.jks") + ) + .setSecureSettings(secureSettings) + .build(); PkiRealm realmNoDelegation = buildRealm(roleMapper, settings); assertRealmUsageStats(realmNoDelegation, true, false, true, false); @@ -397,11 +451,13 @@ public void testVerificationFailsUsingADifferentTruststore() throws Exception { MockSecureSettings secureSettings = new MockSecureSettings(); secureSettings.setString("xpack.security.authc.realms.pki.my_pki.truststore.secure_password", "testnode-client-profile"); Settings settings = Settings.builder() - .put(globalSettings) - .put("xpack.security.authc.realms.pki.my_pki.truststore.path", - getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode-client-profile.jks")) - .setSecureSettings(secureSettings) - .build(); + .put(globalSettings) + .put( + "xpack.security.authc.realms.pki.my_pki.truststore.path", + getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode-client-profile.jks") + ) + .setSecureSettings(secureSettings) + .build(); ThreadContext threadContext = new ThreadContext(settings); PkiRealm realm = buildRealm(roleMapper, settings); assertRealmUsageStats(realm, true, false, true, false); @@ -418,14 +474,23 @@ public void testVerificationFailsUsingADifferentTruststore() throws Exception { public void testTruststorePathWithoutPasswordThrowsException() throws Exception { assumeFalse("Can't run in a FIPS JVM, JKS keystores can't be used", inFipsJvm()); Settings settings = Settings.builder() - .put(globalSettings) - .put("xpack.security.authc.realms.pki.my_pki.truststore.path", - getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode-client-profile.jks")) - .build(); - SslConfigException e = expectThrows(SslConfigException.class, () -> - new PkiRealm(new RealmConfig(new RealmConfig.RealmIdentifier(PkiRealmSettings.TYPE, REALM_NAME), settings, - TestEnvironment.newEnvironment(settings), new ThreadContext(settings)), - mock(UserRoleMapper.class)) + .put(globalSettings) + .put( + "xpack.security.authc.realms.pki.my_pki.truststore.path", + getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode-client-profile.jks") + ) + .build(); + SslConfigException e = expectThrows( + SslConfigException.class, + () -> new PkiRealm( + new RealmConfig( + new RealmConfig.RealmIdentifier(PkiRealmSettings.TYPE, REALM_NAME), + settings, + TestEnvironment.newEnvironment(settings), + new ThreadContext(settings) + ), + mock(UserRoleMapper.class) + ) ); assertThat(e, throwableWithMessage(containsString("incorrect password; (no password"))); } @@ -433,16 +498,25 @@ public void testTruststorePathWithoutPasswordThrowsException() throws Exception public void testTruststorePathWithLegacyPasswordDoesNotThrow() throws Exception { assumeFalse("Can't run in a FIPS JVM, JKS keystores can't be used", inFipsJvm()); Settings settings = Settings.builder() - .put(globalSettings) - .put("xpack.security.authc.realms.pki.my_pki.truststore.path", - getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode-client-profile.jks")) - .put("xpack.security.authc.realms.pki.my_pki.truststore.password", "testnode-client-profile") - .build(); - new PkiRealm(new RealmConfig(new RealmConfig.RealmIdentifier(PkiRealmSettings.TYPE, REALM_NAME), settings, - TestEnvironment.newEnvironment(settings), new ThreadContext(settings)), mock(UserRoleMapper.class)); - assertSettingDeprecationsAndWarnings(new Setting[]{ - PkiRealmSettings.LEGACY_TRUST_STORE_PASSWORD.getConcreteSettingForNamespace(REALM_NAME) - }); + .put(globalSettings) + .put( + "xpack.security.authc.realms.pki.my_pki.truststore.path", + getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode-client-profile.jks") + ) + .put("xpack.security.authc.realms.pki.my_pki.truststore.password", "testnode-client-profile") + .build(); + new PkiRealm( + new RealmConfig( + new RealmConfig.RealmIdentifier(PkiRealmSettings.TYPE, REALM_NAME), + settings, + TestEnvironment.newEnvironment(settings), + new ThreadContext(settings) + ), + mock(UserRoleMapper.class) + ); + assertSettingDeprecationsAndWarnings( + new Setting[] { PkiRealmSettings.LEGACY_TRUST_STORE_PASSWORD.getConcreteSettingForNamespace(REALM_NAME) } + ); } public void testCertificateWithOnlyCnExtractsProperly() throws Exception { @@ -450,12 +524,15 @@ public void testCertificateWithOnlyCnExtractsProperly() throws Exception { X500Principal principal = new X500Principal("CN=PKI Client"); when(certificate.getSubjectX500Principal()).thenReturn(principal); - X509AuthenticationToken token = new X509AuthenticationToken(new X509Certificate[]{certificate}); + X509AuthenticationToken token = new X509AuthenticationToken(new X509Certificate[] { certificate }); assertThat(token, notNullValue()); assertThat(token.dn(), is("CN=PKI Client")); - String parsedPrincipal = PkiRealm.getPrincipalFromSubjectDN(Pattern.compile(PkiRealmSettings.DEFAULT_USERNAME_PATTERN), token, - NoOpLogger.INSTANCE); + String parsedPrincipal = PkiRealm.getPrincipalFromSubjectDN( + Pattern.compile(PkiRealmSettings.DEFAULT_USERNAME_PATTERN), + token, + NoOpLogger.INSTANCE + ); assertThat(parsedPrincipal, is("PKI Client")); } @@ -464,12 +541,15 @@ public void testCertificateWithCnAndOuExtractsProperly() throws Exception { X500Principal principal = new X500Principal("CN=PKI Client, OU=Security"); when(certificate.getSubjectX500Principal()).thenReturn(principal); - X509AuthenticationToken token = new X509AuthenticationToken(new X509Certificate[]{certificate}); + X509AuthenticationToken token = new X509AuthenticationToken(new X509Certificate[] { certificate }); assertThat(token, notNullValue()); assertThat(token.dn(), is("CN=PKI Client, OU=Security")); - String parsedPrincipal = PkiRealm.getPrincipalFromSubjectDN(Pattern.compile(PkiRealmSettings.DEFAULT_USERNAME_PATTERN), token, - NoOpLogger.INSTANCE); + String parsedPrincipal = PkiRealm.getPrincipalFromSubjectDN( + Pattern.compile(PkiRealmSettings.DEFAULT_USERNAME_PATTERN), + token, + NoOpLogger.INSTANCE + ); assertThat(parsedPrincipal, is("PKI Client")); } @@ -478,42 +558,54 @@ public void testCertificateWithCnInMiddle() throws Exception { X500Principal principal = new X500Principal("EMAILADDRESS=pki@elastic.co, CN=PKI Client, OU=Security"); when(certificate.getSubjectX500Principal()).thenReturn(principal); - X509AuthenticationToken token = new X509AuthenticationToken(new X509Certificate[]{certificate}); + X509AuthenticationToken token = new X509AuthenticationToken(new X509Certificate[] { certificate }); assertThat(token, notNullValue()); assertThat(token.dn(), is("EMAILADDRESS=pki@elastic.co, CN=PKI Client, OU=Security")); - String parsedPrincipal = PkiRealm.getPrincipalFromSubjectDN(Pattern.compile(PkiRealmSettings.DEFAULT_USERNAME_PATTERN), token, - NoOpLogger.INSTANCE); + String parsedPrincipal = PkiRealm.getPrincipalFromSubjectDN( + Pattern.compile(PkiRealmSettings.DEFAULT_USERNAME_PATTERN), + token, + NoOpLogger.INSTANCE + ); assertThat(parsedPrincipal, is("PKI Client")); } public void testPKIRealmSettingsPassValidation() throws Exception { Settings settings = Settings.builder() - .put("xpack.security.authc.realms.pki.pki1.order", "1") - .put("xpack.security.authc.realms.pki.pki1.truststore.path", "/foo/bar") - .put("xpack.security.authc.realms.pki.pki1.truststore.password", "supersecret") - .build(); + .put("xpack.security.authc.realms.pki.pki1.order", "1") + .put("xpack.security.authc.realms.pki.pki1.truststore.path", "/foo/bar") + .put("xpack.security.authc.realms.pki.pki1.truststore.password", "supersecret") + .build(); List> settingList = new ArrayList<>(); settingList.addAll(InternalRealmsSettings.getSettings()); ClusterSettings clusterSettings = new ClusterSettings(settings, new HashSet<>(settingList)); clusterSettings.validate(settings, true); - assertSettingDeprecationsAndWarnings(new Setting[]{ - PkiRealmSettings.LEGACY_TRUST_STORE_PASSWORD.getConcreteSettingForNamespace("pki1") - }); + assertSettingDeprecationsAndWarnings( + new Setting[] { PkiRealmSettings.LEGACY_TRUST_STORE_PASSWORD.getConcreteSettingForNamespace("pki1") } + ); } public void testDelegatedAuthorization() throws Exception { final X509AuthenticationToken token = buildToken(); - String parsedPrincipal = PkiRealm.getPrincipalFromSubjectDN(Pattern.compile(PkiRealmSettings.DEFAULT_USERNAME_PATTERN), token, - NoOpLogger.INSTANCE); + String parsedPrincipal = PkiRealm.getPrincipalFromSubjectDN( + Pattern.compile(PkiRealmSettings.DEFAULT_USERNAME_PATTERN), + token, + NoOpLogger.INSTANCE + ); RealmConfig.RealmIdentifier realmIdentifier = new RealmConfig.RealmIdentifier("mock", "other_realm"); - final MockLookupRealm otherRealm = new MockLookupRealm(new RealmConfig( - realmIdentifier, - Settings.builder().put(globalSettings) - .put(RealmSettings.getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0).build(), - TestEnvironment.newEnvironment(globalSettings), new ThreadContext(globalSettings))); + final MockLookupRealm otherRealm = new MockLookupRealm( + new RealmConfig( + realmIdentifier, + Settings.builder() + .put(globalSettings) + .put(RealmSettings.getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0) + .build(), + TestEnvironment.newEnvironment(globalSettings), + new ThreadContext(globalSettings) + ) + ); final User lookupUser = new User(parsedPrincipal); otherRealm.registerUser(lookupUser); @@ -548,8 +640,13 @@ public void testX509AuthenticationTokenOrdered() throws Exception { assertThat(e.getMessage(), is("certificates chain array is not ordered")); } - private void assertRealmUsageStats(Realm realm, Boolean hasTruststore, Boolean hasAuthorizationRealms, - Boolean hasDefaultUsernamePattern, Boolean isAuthenticationDelegated) throws Exception { + private void assertRealmUsageStats( + Realm realm, + Boolean hasTruststore, + Boolean hasAuthorizationRealms, + Boolean hasDefaultUsernamePattern, + Boolean isAuthenticationDelegated + ) throws Exception { final PlainActionFuture> future = new PlainActionFuture<>(); realm.usageStats(future); Map usage = future.get(); @@ -570,8 +667,9 @@ public void testX509AuthenticationTokenCaching() throws Exception { when(mockCertChain[1].getEncoded()).thenReturn(randomByteArrayOfLength(3)); BytesKey cacheKey = PkiRealm.computeTokenFingerprint(new X509AuthenticationToken(mockCertChain)); - BytesKey sameCacheKey = PkiRealm - .computeTokenFingerprint(new X509AuthenticationToken(new X509Certificate[] { mockCertChain[0], mockCertChain[1] })); + BytesKey sameCacheKey = PkiRealm.computeTokenFingerprint( + new X509AuthenticationToken(new X509Certificate[] { mockCertChain[0], mockCertChain[1] }) + ); assertThat(cacheKey, is(sameCacheKey)); BytesKey cacheKeyClient = PkiRealm.computeTokenFingerprint(new X509AuthenticationToken(new X509Certificate[] { mockCertChain[0] })); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlAuthenticatorTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlAuthenticatorTests.java index de8616d01bbcb..aa6aa2e7cadb2 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlAuthenticatorTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlAuthenticatorTests.java @@ -10,10 +10,10 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.ElasticsearchSecurityException; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.logging.Loggers; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.NamedFormatter; +import org.elasticsearch.core.TimeValue; +import org.elasticsearch.core.Tuple; import org.elasticsearch.test.MockLogAppender; import org.elasticsearch.xpack.core.watcher.watch.ClockMock; import org.hamcrest.Matchers; @@ -51,14 +51,9 @@ import org.opensaml.xmlsec.signature.support.SignatureException; import org.w3c.dom.Document; import org.w3c.dom.Element; - import org.xml.sax.InputSource; import org.xml.sax.SAXException; -import javax.crypto.KeyGenerator; -import javax.crypto.SecretKey; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; import java.io.IOException; import java.io.StringReader; import java.nio.charset.StandardCharsets; @@ -74,6 +69,11 @@ import java.util.function.Supplier; import java.util.stream.Collectors; +import javax.crypto.KeyGenerator; +import javax.crypto.SecretKey; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + import static java.util.Collections.emptyList; import static java.util.Collections.singletonList; import static javax.xml.crypto.dsig.CanonicalizationMethod.EXCLUSIVE; @@ -115,18 +115,22 @@ private SamlAuthenticator buildAuthenticator(Supplier> credenti throws Exception { final IdpConfiguration idp = new IdpConfiguration(IDP_ENTITY_ID, credentials); - final SigningConfiguration signingConfiguration = new SigningConfiguration(Collections.singleton("*"), - (X509Credential) buildOpenSamlCredential(spSigningCertificatePair).get(0)); + final SigningConfiguration signingConfiguration = new SigningConfiguration( + Collections.singleton("*"), + (X509Credential) buildOpenSamlCredential(spSigningCertificatePair).get(0) + ); final List spEncryptionCredentials = buildOpenSamlCredential(spEncryptionCertificatePairs).stream() - .map((cred) -> (X509Credential) cred).collect(Collectors.toList()); - final SpConfiguration sp = new SpConfiguration(SP_ENTITY_ID, SP_ACS_URL, null, signingConfiguration, spEncryptionCredentials, - reqAuthnCtxClassRef); - return new SamlAuthenticator( - clock, - idp, - sp, - maxSkew + .map((cred) -> (X509Credential) cred) + .collect(Collectors.toList()); + final SpConfiguration sp = new SpConfiguration( + SP_ENTITY_ID, + SP_ACS_URL, + null, + signingConfiguration, + spEncryptionCredentials, + reqAuthnCtxClassRef ); + return new SamlAuthenticator(clock, idp, sp, maxSkew); } public void testParseEmptyContentIsRejected() throws Exception { @@ -272,11 +276,9 @@ public void testSuccessfullyParseContentFromEncryptedAttribute() throws Exceptio * would decrypt the EncryptedAttribute, there would be no NS declaration for saml2 and parsing would fail with * org.xml.sax.SAXParseException: The prefix "saml2" for element "saml2:Attribute" is not bound. */ - xml = xml.replace("", "") - .replace("", ""); final Response encrypted = encryptAttributes(xml, randomFrom(spEncryptionCertificatePairs)); String encryptedString = SamlUtils.getXmlContent(encrypted, false); @@ -408,8 +410,13 @@ public void testIncorrectRequestIdIsRejected() throws Exception { public void testIncorrectRecipientIsRejected() throws Exception { Instant now = clock.instant(); final Response response = getSimpleResponse(now); - response.getAssertions().get(0).getSubject().getSubjectConfirmations().get(0).getSubjectConfirmationData() - .setRecipient(SP_ACS_URL+"/fake"); + response.getAssertions() + .get(0) + .getSubject() + .getSubjectConfirmations() + .get(0) + .getSubjectConfirmationData() + .setRecipient(SP_ACS_URL + "/fake"); final String xml = SamlUtils.getXmlContent(response, false); SamlToken token = token(signResponse(xml)); @@ -471,8 +478,10 @@ public void testIncorrectAuthnContextClassRefIsRejected() throws Exception { Instant now = clock.instant(); String xml = getSimpleResponseAsString(now); - SamlAuthenticator authenticatorWithReqAuthnCtx = buildAuthenticator(() -> buildOpenSamlCredential(idpSigningCertificatePair), - Arrays.asList(X509_AUTHN_CTX, KERBEROS_AUTHN_CTX)); + SamlAuthenticator authenticatorWithReqAuthnCtx = buildAuthenticator( + () -> buildOpenSamlCredential(idpSigningCertificatePair), + Arrays.asList(X509_AUTHN_CTX, KERBEROS_AUTHN_CTX) + ); SamlToken token = token(signResponse(xml)); final ElasticsearchSecurityException exception = expectSamlException(() -> authenticatorWithReqAuthnCtx.authenticate(token)); assertThat(exception.getMessage(), containsString("Rejecting SAML assertion as the AuthnContextClassRef")); @@ -518,8 +527,13 @@ public void testAssetionWithoutBearerSubjectConfirmationMethodIsRejected() throw public void testIncorrectSubjectConfirmationDataInResponseToIsRejected() throws Exception { Instant now = clock.instant(); Response response = getSimpleResponse(now); - response.getAssertions().get(0).getSubject().getSubjectConfirmations().get(0).getSubjectConfirmationData().setInResponseTo( - "incorrectId"); + response.getAssertions() + .get(0) + .getSubject() + .getSubjectConfirmations() + .get(0) + .getSubjectConfirmationData() + .setInResponseTo("incorrectId"); final String xml = SamlUtils.getXmlContent(response, false); SamlToken token = token(signResponse(xml)); final ElasticsearchSecurityException exception = expectSamlException(() -> authenticator.authenticate(token)); @@ -671,8 +685,10 @@ public void testExpiredContentIsRejected() throws Exception { public void testContentIsRejectedIfRestrictedToADifferentAudience() throws Exception { final String audience = "https://some.other.sp/SAML2"; final Response response = getSimpleResponse(Instant.now()); - AudienceRestriction audienceRestriction = SamlUtils.buildObject(AudienceRestriction.class, - AudienceRestriction.DEFAULT_ELEMENT_NAME); + AudienceRestriction audienceRestriction = SamlUtils.buildObject( + AudienceRestriction.class, + AudienceRestriction.DEFAULT_ELEMENT_NAME + ); Audience falseAudience = SamlUtils.buildObject(Audience.class, Audience.DEFAULT_ELEMENT_NAME); falseAudience.setURI(audience); audienceRestriction.getAudiences().add(falseAudience); @@ -692,8 +708,10 @@ public void testLoggingWhenAudienceCheckFails() throws Exception { final String similarAudienceString = SP_ENTITY_ID.replaceFirst("/$", ":80/"); final String wrongAudienceString = "http://" + randomAlphaOfLengthBetween(4, 12) + "." + randomAlphaOfLengthBetween(6, 8) + "/"; final Response response = getSimpleResponse(Instant.now()); - AudienceRestriction invalidAudienceRestriction = SamlUtils.buildObject(AudienceRestriction.class, - AudienceRestriction.DEFAULT_ELEMENT_NAME); + AudienceRestriction invalidAudienceRestriction = SamlUtils.buildObject( + AudienceRestriction.class, + AudienceRestriction.DEFAULT_ELEMENT_NAME + ); Audience similarAudience = SamlUtils.buildObject(Audience.class, Audience.DEFAULT_ELEMENT_NAME); similarAudience.setURI(similarAudienceString); Audience wrongAudience = SamlUtils.buildObject(Audience.class, Audience.DEFAULT_ELEMENT_NAME); @@ -711,19 +729,28 @@ public void testLoggingWhenAudienceCheckFails() throws Exception { try { Loggers.addAppender(samlLogger, mockAppender); - mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation( - "similar audience", - authenticator.getClass().getName(), - Level.INFO, - "Audience restriction [" + similarAudienceString + "] does not match required audience [" + SP_ENTITY_ID + - "] (difference starts at character [#" + (SP_ENTITY_ID.length() - 1) + "] [:80/] vs [/])" - )); - mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation( - "not similar audience", - authenticator.getClass().getName(), - Level.INFO, - "Audience restriction [" + wrongAudienceString + "] does not match required audience [" + SP_ENTITY_ID + "]" - )); + mockAppender.addExpectation( + new MockLogAppender.SeenEventExpectation( + "similar audience", + authenticator.getClass().getName(), + Level.INFO, + "Audience restriction [" + + similarAudienceString + + "] does not match required audience [" + + SP_ENTITY_ID + + "] (difference starts at character [#" + + (SP_ENTITY_ID.length() - 1) + + "] [:80/] vs [/])" + ) + ); + mockAppender.addExpectation( + new MockLogAppender.SeenEventExpectation( + "not similar audience", + authenticator.getClass().getName(), + Level.INFO, + "Audience restriction [" + wrongAudienceString + "] does not match required audience [" + SP_ENTITY_ID + "]" + ) + ); final ElasticsearchSecurityException exception = expectSamlException(() -> authenticator.authenticate(token)); assertThat(exception.getMessage(), containsString("required audience")); mockAppender.assertAllExpectationsMatched(); @@ -770,11 +797,11 @@ public void testSignatureWrappingAttackOne() throws Exception { */ final Element response = (Element) legitimateDocument.getElementsByTagNameNS(SAML20P_NS, "Response").item(0); final Element clonedResponse = (Element) response.cloneNode(true); - final Element clonedSignature = (Element) clonedResponse. - getElementsByTagNameNS("http://www.w3.org/2000/09/xmldsig#", "Signature").item(0); + final Element clonedSignature = (Element) clonedResponse.getElementsByTagNameNS("http://www.w3.org/2000/09/xmldsig#", "Signature") + .item(0); clonedResponse.removeChild(clonedSignature); - final Element legitimateSignature = (Element) response. - getElementsByTagNameNS("http://www.w3.org/2000/09/xmldsig#", "Signature").item(0); + final Element legitimateSignature = (Element) response.getElementsByTagNameNS("http://www.w3.org/2000/09/xmldsig#", "Signature") + .item(0); legitimateSignature.appendChild(clonedResponse); response.setAttribute("ID", "_forged_ID"); final SamlToken forgedToken = token(SamlUtils.toString((legitimateDocument.getDocumentElement()))); @@ -804,11 +831,11 @@ public void testSignatureWrappingAttackTwo() throws Exception { */ final Element response = (Element) legitimateDocument.getElementsByTagNameNS(SAML20P_NS, "Response").item(0); final Element clonedResponse = (Element) response.cloneNode(true); - final Element clonedSignature = (Element) clonedResponse. - getElementsByTagNameNS("http://www.w3.org/2000/09/xmldsig#", "Signature").item(0); + final Element clonedSignature = (Element) clonedResponse.getElementsByTagNameNS("http://www.w3.org/2000/09/xmldsig#", "Signature") + .item(0); clonedResponse.removeChild(clonedSignature); - final Element legitimateSignature = (Element) response. - getElementsByTagNameNS("http://www.w3.org/2000/09/xmldsig#", "Signature").item(0); + final Element legitimateSignature = (Element) response.getElementsByTagNameNS("http://www.w3.org/2000/09/xmldsig#", "Signature") + .item(0); response.insertBefore(clonedResponse, legitimateSignature); response.setAttribute("ID", "_forged_ID"); final SamlToken forgedToken = token(SamlUtils.toString((legitimateDocument.getDocumentElement()))); @@ -841,12 +868,11 @@ public void testSignatureWrappingAttackThree() throws Exception { */ final Element response = (Element) legitimateDocument.getElementsByTagNameNS(SAML20P_NS, "Response").item(0); - final Element assertion = (Element) legitimateDocument. - getElementsByTagNameNS(SAML20_NS, "Assertion").item(0); + final Element assertion = (Element) legitimateDocument.getElementsByTagNameNS(SAML20_NS, "Assertion").item(0); final Element forgedAssertion = (Element) assertion.cloneNode(true); forgedAssertion.setAttribute("ID", "_forged_assertion_id"); - final Element clonedSignature = (Element) forgedAssertion. - getElementsByTagNameNS("http://www.w3.org/2000/09/xmldsig#", "Signature").item(0); + final Element clonedSignature = (Element) forgedAssertion.getElementsByTagNameNS("http://www.w3.org/2000/09/xmldsig#", "Signature") + .item(0); forgedAssertion.removeChild(clonedSignature); response.insertBefore(forgedAssertion, assertion); final SamlToken forgedToken = token(SamlUtils.toString((legitimateDocument.getDocumentElement()))); @@ -857,7 +883,6 @@ public void testSignatureWrappingAttackThree() throws Exception { } - public void testSignatureWrappingAttackFour() throws Exception { final Instant now = clock.instant(); final String xml = getSimpleResponseAsString(now); @@ -883,8 +908,8 @@ public void testSignatureWrappingAttackFour() throws Exception { final Element assertion = (Element) legitimateDocument.getElementsByTagNameNS(SAML20_NS, "Assertion").item(0); final Element forgedAssertion = (Element) assertion.cloneNode(true); forgedAssertion.setAttribute("ID", "_forged_assertion_id"); - final Element clonedSignature = (Element) forgedAssertion. - getElementsByTagNameNS("http://www.w3.org/2000/09/xmldsig#", "Signature").item(0); + final Element clonedSignature = (Element) forgedAssertion.getElementsByTagNameNS("http://www.w3.org/2000/09/xmldsig#", "Signature") + .item(0); forgedAssertion.removeChild(clonedSignature); response.appendChild(forgedAssertion); forgedAssertion.appendChild(assertion); @@ -916,8 +941,7 @@ public void testSignatureWrappingAttackFive() throws Exception { */ final Element response = (Element) legitimateDocument.getElementsByTagNameNS(SAML20P_NS, "Response").item(0); final Element assertion = (Element) legitimateDocument.getElementsByTagNameNS(SAML20_NS, "Assertion").item(0); - final Element signature = (Element) assertion. - getElementsByTagNameNS("http://www.w3.org/2000/09/xmldsig#", "Signature").item(0); + final Element signature = (Element) assertion.getElementsByTagNameNS("http://www.w3.org/2000/09/xmldsig#", "Signature").item(0); assertion.removeChild(signature); final Element forgedAssertion = (Element) assertion.cloneNode(true); forgedAssertion.setAttribute("ID", "_forged_assertion_id"); @@ -954,10 +978,9 @@ public void testSignatureWrappingAttackSix() throws Exception { final Element assertion = (Element) legitimateDocument.getElementsByTagNameNS(SAML20_NS, "Assertion").item(0); final Element forgedAssertion = (Element) assertion.cloneNode(true); forgedAssertion.setAttribute("ID", "_forged_assertion_id"); - final Element signature = (Element) assertion. - getElementsByTagNameNS("http://www.w3.org/2000/09/xmldsig#", "Signature").item(0); - final Element forgedSignature = (Element) forgedAssertion. - getElementsByTagNameNS("http://www.w3.org/2000/09/xmldsig#", "Signature").item(0); + final Element signature = (Element) assertion.getElementsByTagNameNS("http://www.w3.org/2000/09/xmldsig#", "Signature").item(0); + final Element forgedSignature = (Element) forgedAssertion.getElementsByTagNameNS("http://www.w3.org/2000/09/xmldsig#", "Signature") + .item(0); forgedAssertion.removeChild(forgedSignature); assertion.removeChild(signature); final Element issuer = (Element) forgedAssertion.getElementsByTagNameNS(SAML20_NS, "Issuer").item(0); @@ -997,8 +1020,8 @@ public void testSignatureWrappingAttackSeven() throws Exception { response.insertBefore(extensions, assertion); final Element forgedAssertion = (Element) assertion.cloneNode(true); forgedAssertion.setAttribute("ID", "_forged_assertion_id"); - final Element forgedSignature = (Element) forgedAssertion. - getElementsByTagNameNS("http://www.w3.org/2000/09/xmldsig#", "Signature").item(0); + final Element forgedSignature = (Element) forgedAssertion.getElementsByTagNameNS("http://www.w3.org/2000/09/xmldsig#", "Signature") + .item(0); forgedAssertion.removeChild(forgedSignature); extensions.appendChild(forgedAssertion); final SamlToken forgedToken = token(SamlUtils.toString((legitimateDocument.getDocumentElement()))); @@ -1034,10 +1057,9 @@ public void testSignatureWrappingAttackEight() throws Exception { final Element assertion = (Element) legitimateDocument.getElementsByTagNameNS(SAML20_NS, "Assertion").item(0); final Element forgedAssertion = (Element) assertion.cloneNode(true); forgedAssertion.setAttribute("ID", "_forged_assertion_id"); - final Element signature = (Element) assertion. - getElementsByTagNameNS("http://www.w3.org/2000/09/xmldsig#", "Signature").item(0); - final Element forgedSignature = (Element) forgedAssertion. - getElementsByTagNameNS("http://www.w3.org/2000/09/xmldsig#", "Signature").item(0); + final Element signature = (Element) assertion.getElementsByTagNameNS("http://www.w3.org/2000/09/xmldsig#", "Signature").item(0); + final Element forgedSignature = (Element) forgedAssertion.getElementsByTagNameNS("http://www.w3.org/2000/09/xmldsig#", "Signature") + .item(0); forgedAssertion.removeChild(forgedSignature); assertion.removeChild(signature); final Element issuer = (Element) forgedAssertion.getElementsByTagNameNS(SAML20_NS, "Issuer").item(0); @@ -1054,9 +1076,9 @@ public void testSignatureWrappingAttackEight() throws Exception { } public void testXXE() throws Exception { - String xml = "\n" + - " ]>" + - "&xxe;"; + String xml = "\n" + + " ]>" + + "&xxe;"; final SamlToken token = token(xml); final ElasticsearchSecurityException exception = expectSamlException(() -> authenticator.authenticate(token)); assertThat(exception.getCause(), instanceOf(SAXException.class)); @@ -1065,11 +1087,11 @@ public void testXXE() throws Exception { public void testBillionLaughsAttack() throws Exception { // There is no need to go up to N iterations - String xml = "\n" + - " \n" + - "]>\n" + - "&lol1;"; + String xml = "\n" + + " \n" + + "]>\n" + + "&lol1;"; final SamlToken token = token(xml); final ElasticsearchSecurityException exception = expectSamlException(() -> authenticator.authenticate(token)); assertThat(exception.getCause(), instanceOf(SAXException.class)); @@ -1114,7 +1136,7 @@ public void testIgnoredCommentsInLegitimateResponses() throws Exception { public void testIgnoredCommentsInResponseUsingCanonicalizationWithComments() throws Exception { assumeFalse("Can't run in a FIPS JVM, there is no DOM XMLSignature Factory so we can't sign XML documents", inFipsJvm()); - assumeFalse("Can't run in Azul Zulu JVM",System.getProperty("java.vendor", "").contains("Azul")); + assumeFalse("Can't run in Azul Zulu JVM", System.getProperty("java.vendor", "").contains("Azul")); final String nameId = "useradmin@example.com"; final String sanitizedNameId = "useradmin@example.com"; @@ -1144,7 +1166,7 @@ public void testFailureWhenIdPCredentialsAreEmpty() throws Exception { assertThat(exception.getCause(), nullValue()); assertThat(exception.getMessage(), containsString("SAML Signature")); assertThat(exception.getMessage(), containsString("could not be validated")); - //Restore the authenticator with credentials for the rest of the test cases + // Restore the authenticator with credentials for the rest of the test cases authenticator = buildAuthenticator(() -> buildOpenSamlCredential(idpSigningCertificatePair), emptyList()); } @@ -1156,7 +1178,7 @@ public void testFailureWhenIdPCredentialsAreNull() throws Exception { assertThat(exception.getCause(), nullValue()); assertThat(exception.getMessage(), containsString("SAML Signature")); assertThat(exception.getMessage(), containsString("could not be validated")); - //Restore the authenticator with credentials for the rest of the test cases + // Restore the authenticator with credentials for the rest of the test cases authenticator = buildAuthenticator(() -> buildOpenSamlCredential(idpSigningCertificatePair), emptyList()); } @@ -1213,14 +1235,14 @@ private Response encryptAttributes(String xml, Tuple keyPair) throws Exception{ + private Encrypter getEncrypter(Tuple keyPair) throws Exception { final int keyLength = randomFrom(supportedAesKeyLengths); final KeyGenerator aesGenerator = KeyGenerator.getInstance("AES"); aesGenerator.init(keyLength); @@ -1233,8 +1255,9 @@ private Encrypter getEncrypter(Tuple keyPair) throw final Credential keyEncryptionCredential = new BasicCredential(keyPair.v1().getPublicKey(), keyPair.v2()); KeyEncryptionParameters keyEncryptionParameters = new KeyEncryptionParameters(); keyEncryptionParameters.setEncryptionCredential(keyEncryptionCredential); - keyEncryptionParameters.setAlgorithm(randomFrom(EncryptionConstants.ALGO_ID_KEYTRANSPORT_RSAOAEP, - EncryptionConstants.ALGO_ID_KEYTRANSPORT_RSA15)); + keyEncryptionParameters.setAlgorithm( + randomFrom(EncryptionConstants.ALGO_ID_KEYTRANSPORT_RSAOAEP, EncryptionConstants.ALGO_ID_KEYTRANSPORT_RSA15) + ); final Encrypter samlEncrypter = new Encrypter(encryptionParameters, keyEncryptionParameters); samlEncrypter.setKeyPlacement(Encrypter.KeyPlacement.INLINE); @@ -1286,8 +1309,13 @@ private Response getSimpleResponse(Instant now, String nameId, String sessionind return getSimpleResponse(now, nameId, sessionindex, subjectConfirmationValidUntil, sessionValidUntil); } - private Response getSimpleResponse(Instant now, String nameId, String sessionindex, Instant subjectConfirmationValidUntil, - Instant sessionValidUntil) { + private Response getSimpleResponse( + Instant now, + String nameId, + String sessionindex, + Instant subjectConfirmationValidUntil, + Instant sessionValidUntil + ) { final Response response = SamlUtils.buildObject(Response.class, Response.DEFAULT_ELEMENT_NAME); response.setDestination(SP_ACS_URL); response.setID(randomId()); @@ -1307,8 +1335,10 @@ private Response getSimpleResponse(Instant now, String nameId, String sessionind final Issuer assertionIssuer = SamlUtils.buildObject(Issuer.class, Issuer.DEFAULT_ELEMENT_NAME); assertionIssuer.setValue(IDP_ENTITY_ID); assertion.setIssuer(assertionIssuer); - AudienceRestriction audienceRestriction = SamlUtils.buildObject(AudienceRestriction.class, - AudienceRestriction.DEFAULT_ELEMENT_NAME); + AudienceRestriction audienceRestriction = SamlUtils.buildObject( + AudienceRestriction.class, + AudienceRestriction.DEFAULT_ELEMENT_NAME + ); Audience audience = SamlUtils.buildObject(Audience.class, Audience.DEFAULT_ELEMENT_NAME); audience.setURI(SP_ENTITY_ID); audienceRestriction.getAudiences().add(audience); @@ -1321,10 +1351,14 @@ private Response getSimpleResponse(Instant now, String nameId, String sessionind nameIDElement.setNameQualifier(IDP_ENTITY_ID); nameIDElement.setSPNameQualifier(SP_ENTITY_ID); nameIDElement.setValue(nameId); - final SubjectConfirmation subjectConfirmation = SamlUtils.buildObject(SubjectConfirmation.class, - SubjectConfirmation.DEFAULT_ELEMENT_NAME); - final SubjectConfirmationData subjectConfirmationData = SamlUtils.buildObject(SubjectConfirmationData.class, - SubjectConfirmationData.DEFAULT_ELEMENT_NAME); + final SubjectConfirmation subjectConfirmation = SamlUtils.buildObject( + SubjectConfirmation.class, + SubjectConfirmation.DEFAULT_ELEMENT_NAME + ); + final SubjectConfirmationData subjectConfirmationData = SamlUtils.buildObject( + SubjectConfirmationData.class, + SubjectConfirmationData.DEFAULT_ELEMENT_NAME + ); subjectConfirmationData.setNotOnOrAfter(subjectConfirmationValidUntil); subjectConfirmationData.setRecipient(SP_ACS_URL); subjectConfirmationData.setInResponseTo(requestId); @@ -1333,8 +1367,10 @@ private Response getSimpleResponse(Instant now, String nameId, String sessionind subject.setNameID(nameIDElement); subject.getSubjectConfirmations().add(subjectConfirmation); assertion.setSubject(subject); - final AuthnContextClassRef authnContextClassRef = SamlUtils.buildObject(AuthnContextClassRef.class, - AuthnContextClassRef.DEFAULT_ELEMENT_NAME); + final AuthnContextClassRef authnContextClassRef = SamlUtils.buildObject( + AuthnContextClassRef.class, + AuthnContextClassRef.DEFAULT_ELEMENT_NAME + ); authnContextClassRef.setURI(PASSWORD_AUTHN_CTX); final AuthnContext authnContext = SamlUtils.buildObject(AuthnContext.class, AuthnContext.DEFAULT_ELEMENT_NAME); authnContext.setAuthnContextClassRef(authnContextClassRef); @@ -1344,8 +1380,10 @@ private Response getSimpleResponse(Instant now, String nameId, String sessionind authnStatement.setSessionIndex(sessionindex); authnStatement.setSessionNotOnOrAfter(sessionValidUntil); assertion.getAuthnStatements().add(authnStatement); - final AttributeStatement attributeStatement = SamlUtils.buildObject(AttributeStatement.class, - AttributeStatement.DEFAULT_ELEMENT_NAME); + final AttributeStatement attributeStatement = SamlUtils.buildObject( + AttributeStatement.class, + AttributeStatement.DEFAULT_ELEMENT_NAME + ); final Attribute attribute1 = SamlUtils.buildObject(Attribute.class, Attribute.DEFAULT_ELEMENT_NAME); attribute1.setNameFormat("urn:oasis:names:tc:SAML:2.0:attrname-format:uri"); attribute1.setName(UID_OID); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlAuthnRequestBuilderTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlAuthnRequestBuilderTests.java index 0c11161a6f32a..11bb21de22d11 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlAuthnRequestBuilderTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlAuthnRequestBuilderTests.java @@ -6,18 +6,18 @@ */ package org.elasticsearch.xpack.security.authc.saml; -import java.time.Clock; -import java.time.Instant; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - import org.junit.Before; import org.opensaml.saml.common.xml.SAMLConstants; import org.opensaml.saml.saml2.core.AuthnRequest; import org.opensaml.saml.saml2.core.NameID; import org.opensaml.saml.saml2.metadata.EntityDescriptor; +import java.time.Clock; +import java.time.Instant; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.hasSize; @@ -44,9 +44,12 @@ public void init() throws Exception { public void testBuildRequestWithDefaultSettingsHasNoNameIdPolicy() { SpConfiguration sp = new SpConfiguration(SP_ENTITY_ID, ACS_URL, null, null, null, Collections.emptyList()); final SamlAuthnRequestBuilder builder = new SamlAuthnRequestBuilder( - sp, SAMLConstants.SAML2_POST_BINDING_URI, - idpDescriptor, SAMLConstants.SAML2_REDIRECT_BINDING_URI, - Clock.systemUTC()); + sp, + SAMLConstants.SAML2_POST_BINDING_URI, + idpDescriptor, + SAMLConstants.SAML2_REDIRECT_BINDING_URI, + Clock.systemUTC() + ); final AuthnRequest request = buildAndValidateAuthnRequest(builder); @@ -64,9 +67,12 @@ public void testBuildRequestWithDefaultSettingsHasNoNameIdPolicy() { public void testBuildRequestWithPersistentNameAndNoForceAuth() throws Exception { SpConfiguration sp = new SpConfiguration(SP_ENTITY_ID, ACS_URL, null, null, null, Collections.emptyList()); final SamlAuthnRequestBuilder builder = new SamlAuthnRequestBuilder( - sp, SAMLConstants.SAML2_POST_BINDING_URI, - idpDescriptor, SAMLConstants.SAML2_REDIRECT_BINDING_URI, - Clock.systemUTC()); + sp, + SAMLConstants.SAML2_POST_BINDING_URI, + idpDescriptor, + SAMLConstants.SAML2_REDIRECT_BINDING_URI, + Clock.systemUTC() + ); builder.nameIDPolicy(new SamlAuthnRequestBuilder.NameIDPolicySettings(NameID.PERSISTENT, false, SP_ENTITY_ID)); builder.forceAuthn(null); @@ -89,9 +95,12 @@ public void testBuildRequestWithPersistentNameAndNoForceAuth() throws Exception public void testBuildRequestWithTransientNameAndForceAuthTrue() throws Exception { SpConfiguration sp = new SpConfiguration(SP_ENTITY_ID, ACS_URL, null, null, null, Collections.emptyList()); final SamlAuthnRequestBuilder builder = new SamlAuthnRequestBuilder( - sp, SAMLConstants.SAML2_POST_BINDING_URI, - idpDescriptor, SAMLConstants.SAML2_REDIRECT_BINDING_URI, - Clock.systemUTC()); + sp, + SAMLConstants.SAML2_POST_BINDING_URI, + idpDescriptor, + SAMLConstants.SAML2_REDIRECT_BINDING_URI, + Clock.systemUTC() + ); final String noSpNameQualifier = randomBoolean() ? "" : null; builder.nameIDPolicy(new SamlAuthnRequestBuilder.NameIDPolicySettings(NameID.TRANSIENT, true, noSpNameQualifier)); @@ -114,12 +123,14 @@ public void testBuildRequestWithTransientNameAndForceAuthTrue() throws Exception } public void testBuildRequestWithRequestedAuthnContext() throws Exception { - SpConfiguration sp = new SpConfiguration(SP_ENTITY_ID, ACS_URL, null, null, null, - Collections.singletonList(KERBEROS_AUTHN_CTX)); + SpConfiguration sp = new SpConfiguration(SP_ENTITY_ID, ACS_URL, null, null, null, Collections.singletonList(KERBEROS_AUTHN_CTX)); final SamlAuthnRequestBuilder builder = new SamlAuthnRequestBuilder( - sp, SAMLConstants.SAML2_POST_BINDING_URI, - idpDescriptor, SAMLConstants.SAML2_REDIRECT_BINDING_URI, - Clock.systemUTC()); + sp, + SAMLConstants.SAML2_POST_BINDING_URI, + idpDescriptor, + SAMLConstants.SAML2_REDIRECT_BINDING_URI, + Clock.systemUTC() + ); builder.nameIDPolicy(new SamlAuthnRequestBuilder.NameIDPolicySettings(NameID.PERSISTENT, false, SP_ENTITY_ID)); builder.forceAuthn(null); @@ -141,14 +152,15 @@ public void testBuildRequestWithRequestedAuthnContext() throws Exception { } public void testBuildRequestWithRequestedAuthnContexts() throws Exception { - List reqAuthnCtxClassRef = Arrays.asList(KERBEROS_AUTHN_CTX, - SMARTCARD_AUTHN_CTX, - "http://an.arbitrary/mfa-profile"); + List reqAuthnCtxClassRef = Arrays.asList(KERBEROS_AUTHN_CTX, SMARTCARD_AUTHN_CTX, "http://an.arbitrary/mfa-profile"); SpConfiguration sp = new SpConfiguration(SP_ENTITY_ID, ACS_URL, null, null, null, reqAuthnCtxClassRef); final SamlAuthnRequestBuilder builder = new SamlAuthnRequestBuilder( - sp, SAMLConstants.SAML2_POST_BINDING_URI, - idpDescriptor, SAMLConstants.SAML2_REDIRECT_BINDING_URI, - Clock.systemUTC()); + sp, + SAMLConstants.SAML2_POST_BINDING_URI, + idpDescriptor, + SAMLConstants.SAML2_REDIRECT_BINDING_URI, + Clock.systemUTC() + ); builder.nameIDPolicy(new SamlAuthnRequestBuilder.NameIDPolicySettings(NameID.PERSISTENT, false, SP_ENTITY_ID)); builder.forceAuthn(null); final AuthnRequest request = buildAndValidateAuthnRequest(builder); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlLogoutRequestHandlerTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlLogoutRequestHandlerTests.java index 91b3133467d67..c10581dd56d3c 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlLogoutRequestHandlerTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlLogoutRequestHandlerTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.security.authc.saml; import org.elasticsearch.ElasticsearchSecurityException; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.set.Sets; +import org.elasticsearch.core.TimeValue; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; @@ -109,8 +109,8 @@ public void testLogoutWithSwitchedSignatureFailsValidation() throws Exception { final String realQuery = buildSignedQueryString(realLogoutRequest); final String tamperedQuery = fakeQuery.replaceFirst("&Signature=.*$", "") - + "&Signature=" - + realQuery.replaceFirst("^.*&Signature=", ""); + + "&Signature=" + + realQuery.replaceFirst("^.*&Signature=", ""); final SamlLogoutRequestHandler handler = buildHandler(); final ElasticsearchSecurityException exception = expectSamlException(() -> handler.parseFromQueryString(tamperedQuery)); @@ -123,8 +123,9 @@ public void testLogoutWithSwitchedAlgorithmFailsValidation() throws Exception { final String realQuery = buildSignedQueryString(logoutRequest); final String tamperedQuery = realQuery.replaceFirst( - urlEncode(SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA256), - urlEncode(SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA1)); + urlEncode(SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA256), + urlEncode(SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA1) + ); final SamlLogoutRequestHandler handler = buildHandler(); assertThat(handler.parseFromQueryString(realQuery), notNullValue()); @@ -205,14 +206,15 @@ private SamlLogoutRequestHandler buildHandler() throws Exception { final X509Credential spCredential = (X509Credential) buildOpenSamlCredential(readRandomKeyPair()).get(0); final SigningConfiguration signingConfiguration = new SigningConfiguration(Collections.singleton("*"), spCredential); - final SpConfiguration sp = new SpConfiguration("https://sp.test/", "https://sp.test/saml/asc", LOGOUT_URL, - signingConfiguration, Arrays.asList(spCredential), Collections.emptyList()); - return new SamlLogoutRequestHandler( - clock, - idp, - sp, - TimeValue.timeValueSeconds(1) + final SpConfiguration sp = new SpConfiguration( + "https://sp.test/", + "https://sp.test/saml/asc", + LOGOUT_URL, + signingConfiguration, + Arrays.asList(spCredential), + Collections.emptyList() ); + return new SamlLogoutRequestHandler(clock, idp, sp, TimeValue.timeValueSeconds(1)); } } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlLogoutRequestMessageBuilderTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlLogoutRequestMessageBuilderTests.java index ad2cd46598103..928cf7ac146ed 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlLogoutRequestMessageBuilderTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlLogoutRequestMessageBuilderTests.java @@ -6,11 +6,6 @@ */ package org.elasticsearch.xpack.security.authc.saml; -import java.time.Clock; -import java.time.Instant; -import java.time.ZoneOffset; -import java.util.Collections; - import org.hamcrest.Matchers; import org.junit.Before; import org.opensaml.saml.common.xml.SAMLConstants; @@ -20,6 +15,11 @@ import org.opensaml.saml.saml2.metadata.IDPSSODescriptor; import org.opensaml.saml.saml2.metadata.SingleLogoutService; +import java.time.Clock; +import java.time.Instant; +import java.time.ZoneOffset; +import java.util.Collections; + import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.iterableWithSize; @@ -56,16 +56,19 @@ public void testBuildNullRequestWhenLogoutNotSupportedByIdp() throws Exception { } public void testBuildValidRequest() throws Exception { - final SingleLogoutService sloPost = logoutService(SAMLConstants.SAML2_POST_BINDING_URI, - "http://idp.example.com/saml/logout/post"); + final SingleLogoutService sloPost = logoutService(SAMLConstants.SAML2_POST_BINDING_URI, "http://idp.example.com/saml/logout/post"); idpRole.getSingleLogoutServices().add(sloPost); - final SingleLogoutService sloRedirect = logoutService(SAMLConstants.SAML2_REDIRECT_BINDING_URI, - "http://idp.example.com/saml/logout/redirect"); + final SingleLogoutService sloRedirect = logoutService( + SAMLConstants.SAML2_REDIRECT_BINDING_URI, + "http://idp.example.com/saml/logout/redirect" + ); idpRole.getSingleLogoutServices().add(sloRedirect); - final SingleLogoutService sloArtifact = logoutService(SAMLConstants.SAML2_ARTIFACT_BINDING_URI, - "http://idp.example.com/saml/logout/artifact"); + final SingleLogoutService sloArtifact = logoutService( + SAMLConstants.SAML2_ARTIFACT_BINDING_URI, + "http://idp.example.com/saml/logout/artifact" + ); idpRole.getSingleLogoutServices().add(sloArtifact); Clock fixedClock = Clock.fixed(Instant.now(), ZoneOffset.UTC); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlLogoutResponseHandlerHttpPostTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlLogoutResponseHandlerHttpPostTests.java index 800e92aea2016..f138118954067 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlLogoutResponseHandlerHttpPostTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlLogoutResponseHandlerHttpPostTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.xpack.security.authc.saml; import org.elasticsearch.ElasticsearchSecurityException; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.NamedFormatter; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.xpack.core.watcher.watch.ClockMock; import org.junit.Before; import org.opensaml.saml.saml2.core.LogoutResponse; @@ -34,10 +34,12 @@ public void setupHandler() { clock = new ClockMock(); maxSkew = TimeValue.timeValueMinutes(1); requestId = randomId(); - samlLogoutResponseHandler = new SamlLogoutResponseHandler(clock, + samlLogoutResponseHandler = new SamlLogoutResponseHandler( + clock, getIdpConfiguration(() -> buildOpenSamlCredential(idpSigningCertificatePair)), getSpConfiguration(emptyList()), - maxSkew); + maxSkew + ); } public void testHandlerWorksWithHttpPostBinding() throws Exception { @@ -47,8 +49,9 @@ public void testHandlerWorksWithHttpPostBinding() throws Exception { public void testHandlerFailsWithHttpPostBindingAndNoSignature() throws Exception { final String payload = buildLogoutResponsePayload(emptyMap(), false); - final ElasticsearchSecurityException e = - expectSamlException(() -> samlLogoutResponseHandler.handle(false, payload, List.of(requestId))); + final ElasticsearchSecurityException e = expectSamlException( + () -> samlLogoutResponseHandler.handle(false, payload, List.of(requestId)) + ); assertThat(e.getMessage(), containsString("is not signed")); } @@ -56,8 +59,9 @@ public void testHandlerWillThrowWhenStatusIsNotSuccess() throws Exception { final Map replacements = new HashMap<>(); replacements.put("status", "urn:oasis:names:tc:SAML:2.0:status:Requester"); final String payload = buildLogoutResponsePayload(replacements, true); - final ElasticsearchSecurityException e = - expectSamlException(() -> samlLogoutResponseHandler.handle(false, payload, List.of(requestId))); + final ElasticsearchSecurityException e = expectSamlException( + () -> samlLogoutResponseHandler.handle(false, payload, List.of(requestId)) + ); assertThat(e.getMessage(), containsString("not a 'success' response")); } @@ -87,8 +91,10 @@ private String buildLogoutResponsePayload(Map data, boolean shou } private String signLogoutResponseString(String xml) throws Exception { - final LogoutResponse logoutResponse = - samlLogoutResponseHandler.buildXmlObject(parseDocument(xml).getDocumentElement(), LogoutResponse.class); + final LogoutResponse logoutResponse = samlLogoutResponseHandler.buildXmlObject( + parseDocument(xml).getDocumentElement(), + LogoutResponse.class + ); signSignableObject(logoutResponse, EXCLUSIVE, idpSigningCertificatePair); return SamlUtils.getXmlContent(logoutResponse, false); } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlLogoutResponseHandlerHttpRedirectTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlLogoutResponseHandlerHttpRedirectTests.java index 4946619daf167..90c13b2f2762a 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlLogoutResponseHandlerHttpRedirectTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlLogoutResponseHandlerHttpRedirectTests.java @@ -61,7 +61,8 @@ public void setupHandler() throws Exception { LOGOUT_URL, signingConfiguration, List.of(spCredential), - Collections.emptyList()); + Collections.emptyList() + ); samlLogoutResponseHandler = new SamlLogoutResponseHandler(clock, idp, sp, TimeValue.timeValueSeconds(1)); } @@ -97,8 +98,9 @@ public void testHandlerFailsIfStatusIsNotSuccess() { logoutResponse.setIssuer(issuer); final String url = new SamlRedirect(logoutResponse, signingConfiguration).getRedirectUrl(); - final ElasticsearchSecurityException e = - expectSamlException(() -> samlLogoutResponseHandler.handle(true, new URI(url).getRawQuery(), List.of(requestId))); + final ElasticsearchSecurityException e = expectSamlException( + () -> samlLogoutResponseHandler.handle(true, new URI(url).getRawQuery(), List.of(requestId)) + ); assertThat(e.getMessage(), containsString("is not a 'success' response")); } @@ -116,8 +118,9 @@ public void testHandlerWillFailWhenQueryStringNotSigned() { issuer.setValue(IDP_ENTITY_ID); logoutResponse.setIssuer(issuer); final String url = new SamlRedirect(logoutResponse, signingConfiguration).getRedirectUrl(); - final ElasticsearchSecurityException e = - expectSamlException(() -> samlLogoutResponseHandler.handle(true, new URI(url).getRawQuery(), List.of(requestId))); + final ElasticsearchSecurityException e = expectSamlException( + () -> samlLogoutResponseHandler.handle(true, new URI(url).getRawQuery(), List.of(requestId)) + ); assertThat(e.getMessage(), containsString("Query string is not signed, but is required for HTTP-Redirect binding")); } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlMetadataCommandTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlMetadataCommandTests.java index e33f1495b8622..5d3a4ff62d360 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlMetadataCommandTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlMetadataCommandTests.java @@ -74,8 +74,8 @@ public void setup() throws Exception { when(passwordProtectedKeystore.isLoaded()).thenReturn(true); when(passwordProtectedKeystore.hasPassword()).thenReturn(true); doNothing().when(passwordProtectedKeystore).decrypt("keystore-password".toCharArray()); - doThrow(new SecurityException("Provided keystore password was incorrect", new IOException())) - .when(passwordProtectedKeystore).decrypt("wrong-password".toCharArray()); + doThrow(new SecurityException("Provided keystore password was incorrect", new IOException())).when(passwordProtectedKeystore) + .decrypt("wrong-password".toCharArray()); } public void testDefaultOptions() throws Exception { @@ -88,16 +88,16 @@ public void testDefaultOptions() throws Exception { final boolean useSigningCredentials = randomBoolean(); final Settings.Builder settingsBuilder = Settings.builder() - .put("path.home", createTempDir()) - .put(RealmSettings.PREFIX + "saml.my_saml.order", 1) - .put(RealmSettings.PREFIX + "saml.my_saml.idp.entity_id", "https://okta.my.corp/") - .put(RealmSettings.PREFIX + "saml.my_saml.sp.entity_id", "https://kibana.my.corp/") - .put(RealmSettings.PREFIX + "saml.my_saml.sp.acs", "https://kibana.my.corp/saml/login") - .put(RealmSettings.PREFIX + "saml.my_saml.sp.logout", "https://kibana.my.corp/saml/logout") - .put(RealmSettings.PREFIX + "saml.my_saml.attributes.principal", "urn:oid:0.9.2342.19200300.100.1.1"); + .put("path.home", createTempDir()) + .put(RealmSettings.PREFIX + "saml.my_saml.order", 1) + .put(RealmSettings.PREFIX + "saml.my_saml.idp.entity_id", "https://okta.my.corp/") + .put(RealmSettings.PREFIX + "saml.my_saml.sp.entity_id", "https://kibana.my.corp/") + .put(RealmSettings.PREFIX + "saml.my_saml.sp.acs", "https://kibana.my.corp/saml/login") + .put(RealmSettings.PREFIX + "saml.my_saml.sp.logout", "https://kibana.my.corp/saml/logout") + .put(RealmSettings.PREFIX + "saml.my_saml.attributes.principal", "urn:oid:0.9.2342.19200300.100.1.1"); if (useSigningCredentials) { settingsBuilder.put(RealmSettings.PREFIX + "saml.my_saml.signing.certificate", certPath.toString()) - .put(RealmSettings.PREFIX + "saml.my_saml.signing.key", keyPath.toString()); + .put(RealmSettings.PREFIX + "saml.my_saml.signing.key", keyPath.toString()); } final Settings settings = settingsBuilder.build(); final Environment env = TestEnvironment.newEnvironment(settings); @@ -163,14 +163,14 @@ public void testDefaultOptions() throws Exception { public void testFailIfMultipleRealmsExist() throws Exception { final KeyStoreWrapper usedKeyStore = randomFrom(keyStore, passwordProtectedKeystore); final Settings settings = Settings.builder() - .put("path.home", createTempDir()) - .put(RealmSettings.PREFIX + "saml.saml_a.type", "saml") - .put(RealmSettings.PREFIX + "saml.saml_a.sp.entity_id", "https://saml.a/") - .put(RealmSettings.PREFIX + "saml.saml_a.sp.acs", "https://saml.a/") - .put(RealmSettings.PREFIX + "saml.saml_b.type", "saml") - .put(RealmSettings.PREFIX + "saml.saml_b.sp.entity_id", "https://saml.b/") - .put(RealmSettings.PREFIX + "saml.saml_b.sp.acs", "https://saml.b/") - .build(); + .put("path.home", createTempDir()) + .put(RealmSettings.PREFIX + "saml.saml_a.type", "saml") + .put(RealmSettings.PREFIX + "saml.saml_a.sp.entity_id", "https://saml.a/") + .put(RealmSettings.PREFIX + "saml.saml_a.sp.acs", "https://saml.a/") + .put(RealmSettings.PREFIX + "saml.saml_b.type", "saml") + .put(RealmSettings.PREFIX + "saml.saml_b.sp.entity_id", "https://saml.b/") + .put(RealmSettings.PREFIX + "saml.saml_b.sp.acs", "https://saml.b/") + .build(); final Environment env = TestEnvironment.newEnvironment(settings); final SamlMetadataCommand command = new SamlMetadataCommand((e) -> usedKeyStore); @@ -187,22 +187,20 @@ public void testFailIfMultipleRealmsExist() throws Exception { public void testSpecifyRealmNameAsParameter() throws Exception { final KeyStoreWrapper usedKeyStore = randomFrom(keyStore, passwordProtectedKeystore); final Settings settings = Settings.builder() - .put("path.home", createTempDir()) - .put(RealmSettings.PREFIX + "saml.saml_a.order", 1) - .put(RealmSettings.PREFIX + "saml.saml_a.type", "saml") - .put(RealmSettings.PREFIX + "saml.saml_a.sp.entity_id", "https://saml.a/") - .put(RealmSettings.PREFIX + "saml.saml_a.sp.acs", "https://saml.a/acs") - .put(RealmSettings.PREFIX + "saml.saml_b.order", 2) - .put(RealmSettings.PREFIX + "saml.saml_b.type", "saml") - .put(RealmSettings.PREFIX + "saml.saml_b.sp.entity_id", "https://saml.b/") - .put(RealmSettings.PREFIX + "saml.saml_b.sp.acs", "https://saml.b/acs") - .build(); + .put("path.home", createTempDir()) + .put(RealmSettings.PREFIX + "saml.saml_a.order", 1) + .put(RealmSettings.PREFIX + "saml.saml_a.type", "saml") + .put(RealmSettings.PREFIX + "saml.saml_a.sp.entity_id", "https://saml.a/") + .put(RealmSettings.PREFIX + "saml.saml_a.sp.acs", "https://saml.a/acs") + .put(RealmSettings.PREFIX + "saml.saml_b.order", 2) + .put(RealmSettings.PREFIX + "saml.saml_b.type", "saml") + .put(RealmSettings.PREFIX + "saml.saml_b.sp.entity_id", "https://saml.b/") + .put(RealmSettings.PREFIX + "saml.saml_b.sp.acs", "https://saml.b/acs") + .build(); final Environment env = TestEnvironment.newEnvironment(settings); final SamlMetadataCommand command = new SamlMetadataCommand((e) -> usedKeyStore); - final OptionSet options = command.getParser().parse(new String[] { - "-realm", "saml_b" - }); + final OptionSet options = command.getParser().parse(new String[] { "-realm", "saml_b" }); final MockTerminal terminal = getTerminalPossiblyWithPassword(usedKeyStore); final EntityDescriptor descriptor = command.buildEntityDescriptor(terminal, options, env); @@ -221,21 +219,19 @@ public void testSpecifyRealmNameAsParameter() throws Exception { public void testHandleAttributes() throws Exception { final KeyStoreWrapper usedKeyStore = randomFrom(keyStore, passwordProtectedKeystore); final Settings settings = Settings.builder() - .put("path.home", createTempDir()) - .put(RealmSettings.PREFIX + "saml.saml1.order", 1) - .put(RealmSettings.PREFIX + "saml.saml1.type", "saml") - .put(RealmSettings.PREFIX + "saml.saml1.sp.entity_id", "https://saml.example.com/") - .put(RealmSettings.PREFIX + "saml.saml1.sp.acs", "https://saml.example.com/") - .put(RealmSettings.PREFIX + "saml.saml1.attributes.principal", "urn:oid:0.9.2342.19200300.100.1.1") - .put(RealmSettings.PREFIX + "saml.saml1.attributes.name", "displayName") - .build(); + .put("path.home", createTempDir()) + .put(RealmSettings.PREFIX + "saml.saml1.order", 1) + .put(RealmSettings.PREFIX + "saml.saml1.type", "saml") + .put(RealmSettings.PREFIX + "saml.saml1.sp.entity_id", "https://saml.example.com/") + .put(RealmSettings.PREFIX + "saml.saml1.sp.acs", "https://saml.example.com/") + .put(RealmSettings.PREFIX + "saml.saml1.attributes.principal", "urn:oid:0.9.2342.19200300.100.1.1") + .put(RealmSettings.PREFIX + "saml.saml1.attributes.name", "displayName") + .build(); final Environment env = TestEnvironment.newEnvironment(settings); final SamlMetadataCommand command = new SamlMetadataCommand((e) -> usedKeyStore); - final OptionSet options = command.getParser().parse(new String[] { - "-attribute", "urn:oid:0.9.2342.19200300.100.1.3", - "-attribute", "groups" - }); + final OptionSet options = command.getParser() + .parse(new String[] { "-attribute", "urn:oid:0.9.2342.19200300.100.1.3", "-attribute", "groups" }); final MockTerminal terminal = getTerminalPossiblyWithPassword(usedKeyStore); // What is the friendly name for command line attribute "urn:oid:0.9.2342.19200300.100.1.3" [default: none] @@ -276,20 +272,17 @@ public void testHandleAttributes() throws Exception { public void testHandleAttributesInBatchMode() throws Exception { final KeyStoreWrapper usedKeyStore = randomFrom(keyStore, passwordProtectedKeystore); final Settings settings = Settings.builder() - .put("path.home", createTempDir()) - .put(RealmSettings.PREFIX + "saml.saml1.order", 1) - .put(RealmSettings.PREFIX + "saml.saml1.type", "saml") - .put(RealmSettings.PREFIX + "saml.saml1.sp.entity_id", "https://saml.example.com/") - .put(RealmSettings.PREFIX + "saml.saml1.sp.acs", "https://saml.example.com/") - .put(RealmSettings.PREFIX + "saml.saml1.attributes.principal", "urn:oid:0.9.2342.19200300.100.1.1") - .build(); + .put("path.home", createTempDir()) + .put(RealmSettings.PREFIX + "saml.saml1.order", 1) + .put(RealmSettings.PREFIX + "saml.saml1.type", "saml") + .put(RealmSettings.PREFIX + "saml.saml1.sp.entity_id", "https://saml.example.com/") + .put(RealmSettings.PREFIX + "saml.saml1.sp.acs", "https://saml.example.com/") + .put(RealmSettings.PREFIX + "saml.saml1.attributes.principal", "urn:oid:0.9.2342.19200300.100.1.1") + .build(); final Environment env = TestEnvironment.newEnvironment(settings); final SamlMetadataCommand command = new SamlMetadataCommand((e) -> usedKeyStore); - final OptionSet options = command.getParser().parse(new String[] { - "-attribute", "urn:oid:0.9.2342.19200300.100.1.3", - "-batch" - }); + final OptionSet options = command.getParser().parse(new String[] { "-attribute", "urn:oid:0.9.2342.19200300.100.1.3", "-batch" }); final MockTerminal terminal = getTerminalPossiblyWithPassword(usedKeyStore); final EntityDescriptor descriptor = command.buildEntityDescriptor(terminal, options, env); @@ -319,23 +312,21 @@ public void testSigningMetadataWithPfx() throws Exception { final Path keyPath = getDataPath("saml.key"); final Path p12Path = getDataPath("saml.p12"); final SamlMetadataCommand command = new SamlMetadataCommand((e) -> usedKeyStore); - final OptionSet options = command.getParser().parse(new String[]{ - "-signing-bundle", p12Path.toString() - }); + final OptionSet options = command.getParser().parse(new String[] { "-signing-bundle", p12Path.toString() }); final boolean useSigningCredentials = randomBoolean(); final Settings.Builder settingsBuilder = Settings.builder() - .put("path.home", createTempDir()) - .put(RealmSettings.PREFIX + "saml.my_saml.type", "saml") - .put(RealmSettings.PREFIX + "saml.my_saml.order", 1) - .put(RealmSettings.PREFIX + "saml.my_saml.idp.entity_id", "https://okta.my.corp/") - .put(RealmSettings.PREFIX + "saml.my_saml.sp.entity_id", "https://kibana.my.corp/") - .put(RealmSettings.PREFIX + "saml.my_saml.sp.acs", "https://kibana.my.corp/saml/login") - .put(RealmSettings.PREFIX + "saml.my_saml.sp.logout", "https://kibana.my.corp/saml/logout") - .put(RealmSettings.PREFIX + "saml.my_saml.attributes.principal", "urn:oid:0.9.2342.19200300.100.1.1"); + .put("path.home", createTempDir()) + .put(RealmSettings.PREFIX + "saml.my_saml.type", "saml") + .put(RealmSettings.PREFIX + "saml.my_saml.order", 1) + .put(RealmSettings.PREFIX + "saml.my_saml.idp.entity_id", "https://okta.my.corp/") + .put(RealmSettings.PREFIX + "saml.my_saml.sp.entity_id", "https://kibana.my.corp/") + .put(RealmSettings.PREFIX + "saml.my_saml.sp.acs", "https://kibana.my.corp/saml/login") + .put(RealmSettings.PREFIX + "saml.my_saml.sp.logout", "https://kibana.my.corp/saml/logout") + .put(RealmSettings.PREFIX + "saml.my_saml.attributes.principal", "urn:oid:0.9.2342.19200300.100.1.1"); if (useSigningCredentials) { settingsBuilder.put(RealmSettings.PREFIX + "saml.my_saml.signing.certificate", certPath.toString()) - .put(RealmSettings.PREFIX + "saml.my_saml.signing.key", keyPath.toString()); + .put(RealmSettings.PREFIX + "saml.my_saml.signing.key", keyPath.toString()); } final Settings settings = settingsBuilder.build(); final Environment env = TestEnvironment.newEnvironment(settings); @@ -379,23 +370,21 @@ public void testSigningMetadataWithPasswordProtectedPfx() throws Exception { final Path keyPath = getDataPath("saml.key"); final Path p12Path = getDataPath("saml_with_password.p12"); final SamlMetadataCommand command = new SamlMetadataCommand((e) -> usedKeyStore); - final OptionSet options = command.getParser().parse(new String[]{ - "-signing-bundle", p12Path.toString(), - "-signing-key-password", "saml" - }); + final OptionSet options = command.getParser() + .parse(new String[] { "-signing-bundle", p12Path.toString(), "-signing-key-password", "saml" }); final boolean useSigningCredentials = randomBoolean(); final Settings.Builder settingsBuilder = Settings.builder() - .put("path.home", createTempDir()) - .put(RealmSettings.PREFIX + "saml.my_saml.type", "saml") - .put(RealmSettings.PREFIX + "saml.my_saml.order", 1) - .put(RealmSettings.PREFIX + "saml.my_saml.idp.entity_id", "https://okta.my.corp/") - .put(RealmSettings.PREFIX + "saml.my_saml.sp.entity_id", "https://kibana.my.corp/") - .put(RealmSettings.PREFIX + "saml.my_saml.sp.acs", "https://kibana.my.corp/saml/login") - .put(RealmSettings.PREFIX + "saml.my_saml.sp.logout", "https://kibana.my.corp/saml/logout"); + .put("path.home", createTempDir()) + .put(RealmSettings.PREFIX + "saml.my_saml.type", "saml") + .put(RealmSettings.PREFIX + "saml.my_saml.order", 1) + .put(RealmSettings.PREFIX + "saml.my_saml.idp.entity_id", "https://okta.my.corp/") + .put(RealmSettings.PREFIX + "saml.my_saml.sp.entity_id", "https://kibana.my.corp/") + .put(RealmSettings.PREFIX + "saml.my_saml.sp.acs", "https://kibana.my.corp/saml/login") + .put(RealmSettings.PREFIX + "saml.my_saml.sp.logout", "https://kibana.my.corp/saml/logout"); if (useSigningCredentials) { settingsBuilder.put(RealmSettings.PREFIX + "saml.my_saml.signing.certificate", certPath.toString()) - .put(RealmSettings.PREFIX + "saml.my_saml.signing.key", keyPath.toString()); + .put(RealmSettings.PREFIX + "saml.my_saml.signing.key", keyPath.toString()); } final Settings settings = settingsBuilder.build(); final Environment env = TestEnvironment.newEnvironment(settings); @@ -415,61 +404,67 @@ public void testErrorSigningMetadataWithWrongPassword() throws Exception { final Path keyPath = getDataPath("saml.key"); final Path signingKeyPath = getDataPath("saml_with_password.key"); final SamlMetadataCommand command = new SamlMetadataCommand((e) -> keyStore); - final OptionSet options = command.getParser().parse(new String[]{ - "-signing-cert", certPath.toString(), - "-signing-key", signingKeyPath.toString(), - "-signing-key-password", "wrongpassword" + final OptionSet options = command.getParser() + .parse( + new String[] { + "-signing-cert", + certPath.toString(), + "-signing-key", + signingKeyPath.toString(), + "-signing-key-password", + "wrongpassword" - }); + } + ); final boolean useSigningCredentials = randomBoolean(); final Settings.Builder settingsBuilder = Settings.builder() - .put("path.home", createTempDir()) - .put(RealmSettings.PREFIX + "saml.my_saml.type", "saml") - .put(RealmSettings.PREFIX + "saml.my_saml.order", 1) - .put(RealmSettings.PREFIX + "saml.my_saml.idp.entity_id", "https://okta.my.corp/") - .put(RealmSettings.PREFIX + "saml.my_saml.sp.entity_id", "https://kibana.my.corp/") - .put(RealmSettings.PREFIX + "saml.my_saml.sp.acs", "https://kibana.my.corp/saml/login") - .put(RealmSettings.PREFIX + "saml.my_saml.sp.logout", "https://kibana.my.corp/saml/logout"); + .put("path.home", createTempDir()) + .put(RealmSettings.PREFIX + "saml.my_saml.type", "saml") + .put(RealmSettings.PREFIX + "saml.my_saml.order", 1) + .put(RealmSettings.PREFIX + "saml.my_saml.idp.entity_id", "https://okta.my.corp/") + .put(RealmSettings.PREFIX + "saml.my_saml.sp.entity_id", "https://kibana.my.corp/") + .put(RealmSettings.PREFIX + "saml.my_saml.sp.acs", "https://kibana.my.corp/saml/login") + .put(RealmSettings.PREFIX + "saml.my_saml.sp.logout", "https://kibana.my.corp/saml/logout"); if (useSigningCredentials) { settingsBuilder.put(RealmSettings.PREFIX + "saml.my_saml.signing.certificate", certPath.toString()) - .put(RealmSettings.PREFIX + "saml.my_saml.signing.key", keyPath.toString()); + .put(RealmSettings.PREFIX + "saml.my_saml.signing.key", keyPath.toString()); } final Settings settings = settingsBuilder.build(); final Environment env = TestEnvironment.newEnvironment(settings); final MockTerminal terminal = getTerminalPossiblyWithPassword(usedKeyStore); final EntityDescriptor descriptor = command.buildEntityDescriptor(terminal, options, env); - final UserException userException = expectThrows(UserException.class, () -> command.possiblySignDescriptor(terminal, options, - descriptor, env)); + final UserException userException = expectThrows( + UserException.class, + () -> command.possiblySignDescriptor(terminal, options, descriptor, env) + ); assertThat(userException.getMessage(), containsString("Unable to create metadata document")); assertThat(terminal.getErrorOutput(), containsString("cannot load PEM private key from [")); } public void testSigningMetadataWithPem() throws Exception { final KeyStoreWrapper usedKeyStore = randomFrom(keyStore, passwordProtectedKeystore); - //Use this keypair for signing the metadata also + // Use this keypair for signing the metadata also final Path certPath = getDataPath("saml.crt"); final Path keyPath = getDataPath("saml.key"); final SamlMetadataCommand command = new SamlMetadataCommand((e) -> keyStore); - final OptionSet options = command.getParser().parse(new String[]{ - "-signing-cert", certPath.toString(), - "-signing-key", keyPath.toString() - }); + final OptionSet options = command.getParser() + .parse(new String[] { "-signing-cert", certPath.toString(), "-signing-key", keyPath.toString() }); final boolean useSigningCredentials = randomBoolean(); final Settings.Builder settingsBuilder = Settings.builder() - .put("path.home", createTempDir()) - .put(RealmSettings.PREFIX + "saml.my_saml.type", "saml") - .put(RealmSettings.PREFIX + "saml.my_saml.order", 1) - .put(RealmSettings.PREFIX + "saml.my_saml.idp.entity_id", "https://okta.my.corp/") - .put(RealmSettings.PREFIX + "saml.my_saml.sp.entity_id", "https://kibana.my.corp/") - .put(RealmSettings.PREFIX + "saml.my_saml.sp.acs", "https://kibana.my.corp/saml/login") - .put(RealmSettings.PREFIX + "saml.my_saml.sp.logout", "https://kibana.my.corp/saml/logout"); + .put("path.home", createTempDir()) + .put(RealmSettings.PREFIX + "saml.my_saml.type", "saml") + .put(RealmSettings.PREFIX + "saml.my_saml.order", 1) + .put(RealmSettings.PREFIX + "saml.my_saml.idp.entity_id", "https://okta.my.corp/") + .put(RealmSettings.PREFIX + "saml.my_saml.sp.entity_id", "https://kibana.my.corp/") + .put(RealmSettings.PREFIX + "saml.my_saml.sp.acs", "https://kibana.my.corp/saml/login") + .put(RealmSettings.PREFIX + "saml.my_saml.sp.logout", "https://kibana.my.corp/saml/logout"); if (useSigningCredentials) { settingsBuilder.put(RealmSettings.PREFIX + "saml.my_saml.signing.certificate", certPath.toString()) - .put(RealmSettings.PREFIX + "saml.my_saml.signing.key", keyPath.toString()); + .put(RealmSettings.PREFIX + "saml.my_saml.signing.key", keyPath.toString()); } final Settings settings = settingsBuilder.build(); final Environment env = TestEnvironment.newEnvironment(settings); @@ -485,32 +480,38 @@ public void testSigningMetadataWithPem() throws Exception { public void testSigningMetadataWithPasswordProtectedPem() throws Exception { final KeyStoreWrapper usedKeyStore = randomFrom(keyStore, passwordProtectedKeystore); - //Use same keypair for signing the metadata + // Use same keypair for signing the metadata final Path signingKeyPath = getDataPath("saml_with_password.key"); final Path certPath = getDataPath("saml.crt"); final Path keyPath = getDataPath("saml.key"); final SamlMetadataCommand command = new SamlMetadataCommand((e) -> usedKeyStore); - final OptionSet options = command.getParser().parse(new String[]{ - "-signing-cert", certPath.toString(), - "-signing-key", signingKeyPath.toString(), - "-signing-key-password", "saml" + final OptionSet options = command.getParser() + .parse( + new String[] { + "-signing-cert", + certPath.toString(), + "-signing-key", + signingKeyPath.toString(), + "-signing-key-password", + "saml" - }); + } + ); final boolean useSigningCredentials = randomBoolean(); final Settings.Builder settingsBuilder = Settings.builder() - .put("path.home", createTempDir()) - .put(RealmSettings.PREFIX + "saml.my_saml.type", "saml") - .put(RealmSettings.PREFIX + "saml.my_saml.order", 1) - .put(RealmSettings.PREFIX + "saml.my_saml.idp.entity_id", "https://okta.my.corp/") - .put(RealmSettings.PREFIX + "saml.my_saml.sp.entity_id", "https://kibana.my.corp/") - .put(RealmSettings.PREFIX + "saml.my_saml.sp.acs", "https://kibana.my.corp/saml/login") - .put(RealmSettings.PREFIX + "saml.my_saml.sp.logout", "https://kibana.my.corp/saml/logout"); + .put("path.home", createTempDir()) + .put(RealmSettings.PREFIX + "saml.my_saml.type", "saml") + .put(RealmSettings.PREFIX + "saml.my_saml.order", 1) + .put(RealmSettings.PREFIX + "saml.my_saml.idp.entity_id", "https://okta.my.corp/") + .put(RealmSettings.PREFIX + "saml.my_saml.sp.entity_id", "https://kibana.my.corp/") + .put(RealmSettings.PREFIX + "saml.my_saml.sp.acs", "https://kibana.my.corp/saml/login") + .put(RealmSettings.PREFIX + "saml.my_saml.sp.logout", "https://kibana.my.corp/saml/logout"); if (useSigningCredentials) { settingsBuilder.put(RealmSettings.PREFIX + "saml.my_saml.signing.certificate", certPath.toString()) - .put(RealmSettings.PREFIX + "saml.my_saml.signing.key", keyPath.toString()); + .put(RealmSettings.PREFIX + "saml.my_saml.signing.key", keyPath.toString()); } final Settings settings = settingsBuilder.build(); final Environment env = TestEnvironment.newEnvironment(settings); @@ -526,31 +527,30 @@ public void testSigningMetadataWithPasswordProtectedPem() throws Exception { public void testSigningMetadataWithPasswordProtectedPemInTerminal() throws Exception { final KeyStoreWrapper usedKeyStore = randomFrom(keyStore, passwordProtectedKeystore); - //Use same keypair for signing the metadata + // Use same keypair for signing the metadata final Path signingKeyPath = getDataPath("saml_with_password.key"); final Path certPath = getDataPath("saml.crt"); final Path keyPath = getDataPath("saml.key"); final SamlMetadataCommand command = new SamlMetadataCommand((e) -> usedKeyStore); - final OptionSet options = command.getParser().parse(new String[]{ - "-signing-cert", certPath.toString(), - "-signing-key", signingKeyPath.toString() + final OptionSet options = command.getParser() + .parse(new String[] { "-signing-cert", certPath.toString(), "-signing-key", signingKeyPath.toString() - }); + }); final boolean useSigningCredentials = randomBoolean(); final Settings.Builder settingsBuilder = Settings.builder() - .put("path.home", createTempDir()) - .put(RealmSettings.PREFIX + "saml.my_saml.type", "saml") - .put(RealmSettings.PREFIX + "saml.my_saml.order", 1) - .put(RealmSettings.PREFIX + "saml.my_saml.idp.entity_id", "https://okta.my.corp/") - .put(RealmSettings.PREFIX + "saml.my_saml.sp.entity_id", "https://kibana.my.corp/") - .put(RealmSettings.PREFIX + "saml.my_saml.sp.acs", "https://kibana.my.corp/saml/login") - .put(RealmSettings.PREFIX + "saml.my_saml.sp.logout", "https://kibana.my.corp/saml/logout"); + .put("path.home", createTempDir()) + .put(RealmSettings.PREFIX + "saml.my_saml.type", "saml") + .put(RealmSettings.PREFIX + "saml.my_saml.order", 1) + .put(RealmSettings.PREFIX + "saml.my_saml.idp.entity_id", "https://okta.my.corp/") + .put(RealmSettings.PREFIX + "saml.my_saml.sp.entity_id", "https://kibana.my.corp/") + .put(RealmSettings.PREFIX + "saml.my_saml.sp.acs", "https://kibana.my.corp/saml/login") + .put(RealmSettings.PREFIX + "saml.my_saml.sp.logout", "https://kibana.my.corp/saml/logout"); if (useSigningCredentials) { settingsBuilder.put(RealmSettings.PREFIX + "saml.my_saml.signing.certificate", certPath.toString()) - .put(RealmSettings.PREFIX + "saml.my_saml.signing.key", keyPath.toString()); + .put(RealmSettings.PREFIX + "saml.my_saml.signing.key", keyPath.toString()); } final Settings settings = settingsBuilder.build(); final Environment env = TestEnvironment.newEnvironment(settings); @@ -576,10 +576,18 @@ public void testDefaultOptionsWithSigningAndMultipleEncryptionKeys() throws Exce final Tuple certEncKeyPair2 = readKeyPair("RSA_4096"); final KeyStore ksEncrypt = KeyStore.getInstance("PKCS12"); ksEncrypt.load(null); - ksEncrypt.setKeyEntry(getAliasName(certEncKeyPair1), certEncKeyPair1.v2(), "key-password".toCharArray(), - new Certificate[] { certEncKeyPair1.v1() }); - ksEncrypt.setKeyEntry(getAliasName(certEncKeyPair2), certEncKeyPair2.v2(), "key-password".toCharArray(), - new Certificate[] { certEncKeyPair2.v1() }); + ksEncrypt.setKeyEntry( + getAliasName(certEncKeyPair1), + certEncKeyPair1.v2(), + "key-password".toCharArray(), + new Certificate[] { certEncKeyPair1.v1() } + ); + ksEncrypt.setKeyEntry( + getAliasName(certEncKeyPair2), + certEncKeyPair2.v2(), + "key-password".toCharArray(), + new Certificate[] { certEncKeyPair2.v1() } + ); try (OutputStream out = Files.newOutputStream(ksEncryptionFile)) { ksEncrypt.store(out, "ks-password".toCharArray()); } @@ -588,8 +596,12 @@ public void testDefaultOptionsWithSigningAndMultipleEncryptionKeys() throws Exce final Tuple certKeyPairSign = readRandomKeyPair("RSA"); final KeyStore ksSign = KeyStore.getInstance("PKCS12"); ksSign.load(null); - ksSign.setKeyEntry(getAliasName(certKeyPairSign), certKeyPairSign.v2(), "key-password".toCharArray(), - new Certificate[] { certKeyPairSign.v1() }); + ksSign.setKeyEntry( + getAliasName(certKeyPairSign), + certKeyPairSign.v2(), + "key-password".toCharArray(), + new Certificate[] { certKeyPairSign.v1() } + ); try (OutputStream out = Files.newOutputStream(ksSigningFile)) { ksSign.store(out, "ks-password".toCharArray()); } @@ -605,7 +617,8 @@ public void testDefaultOptionsWithSigningAndMultipleEncryptionKeys() throws Exce final boolean useSigningCredentials = randomBoolean(); final boolean useEncryptionCredentials = randomBoolean(); - final Settings.Builder settingsBuilder = Settings.builder().put("path.home", dir) + final Settings.Builder settingsBuilder = Settings.builder() + .put("path.home", dir) .put(RealmSettings.PREFIX + "saml.my_saml.type", "saml") .put(RealmSettings.PREFIX + "saml.my_saml.order", 1) .put(RealmSettings.PREFIX + "saml.my_saml.idp.entity_id", "https://okta.my.corp/") @@ -688,8 +701,10 @@ public void testDefaultOptionsWithSigningAndMultipleEncryptionKeys() throws Exce assertEquals("Signing Certificate from SP metadata does not match", certKeyPairSign.v1(), javaCert); } else if (usageType == UsageType.ENCRYPTION) { assertTrue(useEncryptionCredentials); - assertTrue("Encryption Certificate was not found in encryption certificates", - encryptionCertificatesToMatch.remove(javaCert)); + assertTrue( + "Encryption Certificate was not found in encryption certificates", + encryptionCertificatesToMatch.remove(javaCert) + ); } else { fail("Usage type should have been either SIGNING or ENCRYPTION"); } @@ -705,26 +720,24 @@ public void testWrongKeystorePassword() { final Path keyPath = getDataPath("saml.key"); final SamlMetadataCommand command = new SamlMetadataCommand((e) -> passwordProtectedKeystore); - final OptionSet options = command.getParser().parse(new String[]{ - "-signing-cert", certPath.toString(), - "-signing-key", keyPath.toString() - }); + final OptionSet options = command.getParser() + .parse(new String[] { "-signing-cert", certPath.toString(), "-signing-key", keyPath.toString() }); final Settings settings = Settings.builder().put("path.home", createTempDir()).build(); final Environment env = TestEnvironment.newEnvironment(settings); final MockTerminal terminal = new MockTerminal(); terminal.addSecretInput("wrong-password"); - UserException e = expectThrows(UserException.class, () -> { - command.buildEntityDescriptor(terminal, options, env); - }); + UserException e = expectThrows(UserException.class, () -> { command.buildEntityDescriptor(terminal, options, env); }); assertThat(e.getMessage(), CoreMatchers.containsString("Provided keystore password was incorrect")); } private String getAliasName(final Tuple certKeyPair) { // Keys are pre-generated with the same name, so add the serial no to the alias so that keystore entries won't be overwritten - return certKeyPair.v1().getSubjectX500Principal().getName().toLowerCase(Locale.US) + "-"+ - certKeyPair.v1().getSerialNumber()+"-alias"; + return certKeyPair.v1().getSubjectX500Principal().getName().toLowerCase(Locale.US) + + "-" + + certKeyPair.v1().getSerialNumber() + + "-alias"; } private boolean validateSignature(Signature signature) { diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlRealmTestHelper.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlRealmTestHelper.java index ab518262af9b5..fb866cd892361 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlRealmTestHelper.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlRealmTestHelper.java @@ -6,12 +6,6 @@ */ package org.elasticsearch.xpack.security.authc.saml; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.Arrays; -import java.util.Collections; - import org.elasticsearch.core.Nullable; import org.elasticsearch.xpack.core.security.authc.RealmConfig; import org.elasticsearch.xpack.core.security.authc.support.UserRoleMapper; @@ -21,6 +15,12 @@ import org.opensaml.saml.saml2.metadata.SingleLogoutService; import org.opensaml.security.x509.X509Credential; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Arrays; +import java.util.Collections; + import static org.mockito.Mockito.mock; public class SamlRealmTestHelper { @@ -41,21 +41,36 @@ public static SamlRealm buildRealm(RealmConfig realmConfig, @Nullable X509Creden slo.setBinding(SAMLConstants.SAML2_REDIRECT_BINDING_URI); slo.setLocation(IDP_LOGOUT_URL); - final SpConfiguration spConfiguration = new SpConfiguration(SP_ENTITY_ID, SP_ACS_URL, SP_LOGOUT_URL, - new SigningConfiguration(Collections.singleton("*"), credential), Arrays.asList(credential), Collections.emptyList()); - return new SamlRealm(realmConfig, mock(UserRoleMapper.class), mock(SamlAuthenticator.class), - mock(SamlLogoutRequestHandler.class), mock(SamlLogoutResponseHandler.class), - () -> idpDescriptor, spConfiguration); + final SpConfiguration spConfiguration = new SpConfiguration( + SP_ENTITY_ID, + SP_ACS_URL, + SP_LOGOUT_URL, + new SigningConfiguration(Collections.singleton("*"), credential), + Arrays.asList(credential), + Collections.emptyList() + ); + return new SamlRealm( + realmConfig, + mock(UserRoleMapper.class), + mock(SamlAuthenticator.class), + mock(SamlLogoutRequestHandler.class), + mock(SamlLogoutResponseHandler.class), + () -> idpDescriptor, + spConfiguration + ); } public static void writeIdpMetadata(Path path, String idpEntityId) throws IOException { - Files.write(path, Arrays.asList( - "", - "", - "", - "", - "", - "" - )); + Files.write( + path, + Arrays.asList( + "", + "", + "", + "", + "", + "" + ) + ); } } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlRealmTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlRealmTests.java index 2630182204ac3..8fafa495e6144 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlRealmTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlRealmTests.java @@ -121,8 +121,12 @@ public void testReadIdpMetadataFromFile() throws Exception { final Path path = getDataPath("idp1.xml"); Tuple config = buildConfig(path.toString()); final ResourceWatcherService watcherService = mock(ResourceWatcherService.class); - Tuple> tuple - = SamlRealm.initializeResolver(logger, config.v1(), config.v2(), watcherService); + Tuple> tuple = SamlRealm.initializeResolver( + logger, + config.v1(), + config.v2(), + watcherService + ); try { assertIdp1MetadataParsedCorrectly(tuple.v2().get()); } finally { @@ -137,19 +141,21 @@ public void testReadIdpMetadataFromHttps() throws Exception { mockSecureSettings.setString("xpack.security.http.ssl.secure_key_passphrase", "testnode"); final Settings settings = Settings.builder() .put("xpack.security.http.ssl.enabled", true) - .put("xpack.security.http.ssl.key", - getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.pem")) - .put("xpack.security.http.ssl.certificate", - getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt")) - .put("xpack.security.http.ssl.certificate_authorities", - getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt")) + .put("xpack.security.http.ssl.key", getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.pem")) + .put( + "xpack.security.http.ssl.certificate", + getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt") + ) + .put( + "xpack.security.http.ssl.certificate_authorities", + getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt") + ) .putList("xpack.security.http.ssl.supported_protocols", getProtocols()) .put("path.home", createTempDir()) .setSecureSettings(mockSecureSettings) .build(); TestsSSLService sslService = new TestsSSLService(TestEnvironment.newEnvironment(settings)); - try (MockWebServer proxyServer = - new MockWebServer(sslService.sslContext("xpack.security.http.ssl"), false)) { + try (MockWebServer proxyServer = new MockWebServer(sslService.sslContext("xpack.security.http.ssl"), false)) { proxyServer.start(); proxyServer.enqueue(new MockResponse().setResponseCode(200).setBody(body).addHeader("Content-Type", "application/xml")); proxyServer.enqueue(new MockResponse().setResponseCode(200).setBody(body).addHeader("Content-Type", "application/xml")); @@ -158,8 +164,12 @@ public void testReadIdpMetadataFromHttps() throws Exception { Tuple config = buildConfig("https://localhost:" + proxyServer.getPort()); logger.info("Settings\n{}", config.v1().settings().toDelimitedString('\n')); final ResourceWatcherService watcherService = mock(ResourceWatcherService.class); - Tuple> tuple - = SamlRealm.initializeResolver(logger, config.v1(), config.v2(), watcherService); + Tuple> tuple = SamlRealm.initializeResolver( + logger, + config.v1(), + config.v2(), + watcherService + ); try { final int firstRequestCount = proxyServer.requests().size(); @@ -189,8 +199,13 @@ public void testAuthenticateWithRoleMapping() throws Exception { final Boolean populateUserMetadata = randomFrom(Boolean.TRUE, Boolean.FALSE, null); final String authenticatingRealm = randomBoolean() ? REALM_NAME : null; AuthenticationResult result = performAuthentication( - roleMapper, useNameId, principalIsEmailAddress, populateUserMetadata, false, - authenticatingRealm); + roleMapper, + useNameId, + principalIsEmailAddress, + populateUserMetadata, + false, + authenticatingRealm + ); assertThat(result, notNullValue()); assertThat(result.getStatus(), equalTo(AuthenticationResult.Status.SUCCESS)); assertThat(result.getValue().principal(), equalTo(useNameId ? "clint.barton" : "cbarton")); @@ -224,8 +239,14 @@ public void testAuthenticateWithAuthorizingRealm() throws Exception { final boolean useNameId = randomBoolean(); final boolean principalIsEmailAddress = randomBoolean(); final String authenticatingRealm = randomBoolean() ? REALM_NAME : null; - AuthenticationResult result = performAuthentication(roleMapper, useNameId, principalIsEmailAddress, null, true, - authenticatingRealm); + AuthenticationResult result = performAuthentication( + roleMapper, + useNameId, + principalIsEmailAddress, + null, + true, + authenticatingRealm + ); assertThat(result, notNullValue()); assertThat(result.getStatus(), equalTo(AuthenticationResult.Status.SUCCESS)); assertThat(result.getValue().principal(), equalTo(useNameId ? "clint.barton" : "cbarton")); @@ -237,15 +258,26 @@ public void testAuthenticateWithAuthorizingRealm() throws Exception { } public void testAuthenticateWithWrongRealmName() throws Exception { - AuthenticationResult result = performAuthentication(mock(UserRoleMapper.class), randomBoolean(), randomBoolean(), null, true, - REALM_NAME+randomAlphaOfLength(8)); + AuthenticationResult result = performAuthentication( + mock(UserRoleMapper.class), + randomBoolean(), + randomBoolean(), + null, + true, + REALM_NAME + randomAlphaOfLength(8) + ); assertThat(result, notNullValue()); assertThat(result.getStatus(), equalTo(AuthenticationResult.Status.CONTINUE)); } - private AuthenticationResult performAuthentication(UserRoleMapper roleMapper, boolean useNameId, boolean principalIsEmailAddress, - Boolean populateUserMetadata, boolean useAuthorizingRealm, - String authenticatingRealm) throws Exception { + private AuthenticationResult performAuthentication( + UserRoleMapper roleMapper, + boolean useNameId, + boolean principalIsEmailAddress, + Boolean populateUserMetadata, + boolean useAuthorizingRealm, + String authenticatingRealm + ) throws Exception { final EntityDescriptor idp = mockIdp(); final SpConfiguration sp = new SpConfiguration("", "https://saml/", null, null, null, Collections.emptyList()); final SamlAuthenticator authenticator = mock(SamlAuthenticator.class); @@ -257,28 +289,46 @@ private AuthenticationResult performAuthentication(UserRoleMapper roleMapp final RealmConfig.RealmIdentifier realmIdentifier = new RealmConfig.RealmIdentifier("mock", "mock_lookup"); final MockLookupRealm lookupRealm = new MockLookupRealm( - new RealmConfig(realmIdentifier, + new RealmConfig( + realmIdentifier, Settings.builder().put(globalSettings).put(getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0).build(), - env, threadContext)); + env, + threadContext + ) + ); final Settings.Builder settingsBuilder = Settings.builder() - .put(getFullSettingKey(REALM_NAME, SamlRealmSettings.PRINCIPAL_ATTRIBUTE.getAttribute()), useNameId ? "nameid" : "uid") - .put(getFullSettingKey(REALM_NAME, SamlRealmSettings.GROUPS_ATTRIBUTE.getAttribute()), "groups") - .put(getFullSettingKey(REALM_NAME, SamlRealmSettings.MAIL_ATTRIBUTE.getAttribute()), "mail"); + .put(getFullSettingKey(REALM_NAME, SamlRealmSettings.PRINCIPAL_ATTRIBUTE.getAttribute()), useNameId ? "nameid" : "uid") + .put(getFullSettingKey(REALM_NAME, SamlRealmSettings.GROUPS_ATTRIBUTE.getAttribute()), "groups") + .put(getFullSettingKey(REALM_NAME, SamlRealmSettings.MAIL_ATTRIBUTE.getAttribute()), "mail"); if (principalIsEmailAddress) { final boolean anchoredMatch = randomBoolean(); - settingsBuilder.put(getFullSettingKey(REALM_NAME, SamlRealmSettings.PRINCIPAL_ATTRIBUTE.getPattern()), - anchoredMatch ? "^([^@]+)@shield.gov$" : "^([^@]+)@"); + settingsBuilder.put( + getFullSettingKey(REALM_NAME, SamlRealmSettings.PRINCIPAL_ATTRIBUTE.getPattern()), + anchoredMatch ? "^([^@]+)@shield.gov$" : "^([^@]+)@" + ); } if (populateUserMetadata != null) { - settingsBuilder.put(getFullSettingKey(REALM_NAME, SamlRealmSettings.POPULATE_USER_METADATA), - populateUserMetadata.booleanValue()); + settingsBuilder.put( + getFullSettingKey(REALM_NAME, SamlRealmSettings.POPULATE_USER_METADATA), + populateUserMetadata.booleanValue() + ); } if (useAuthorizingRealm) { - settingsBuilder.putList(getFullSettingKey(new RealmConfig.RealmIdentifier("saml", REALM_NAME), - DelegatedAuthorizationSettings.AUTHZ_REALMS), lookupRealm.name()); - lookupRealm.registerUser(new User(userPrincipal, new String[]{ "lookup_user_role" }, "Clinton Barton", "cbarton@shield.gov", - Collections.singletonMap("is_lookup", true), true)); + settingsBuilder.putList( + getFullSettingKey(new RealmConfig.RealmIdentifier("saml", REALM_NAME), DelegatedAuthorizationSettings.AUTHZ_REALMS), + lookupRealm.name() + ); + lookupRealm.registerUser( + new User( + userPrincipal, + new String[] { "lookup_user_role" }, + "Clinton Barton", + "cbarton@shield.gov", + Collections.singletonMap("is_lookup", true), + true + ) + ); } final Settings realmSettings = settingsBuilder.build(); @@ -289,13 +339,14 @@ private AuthenticationResult performAuthentication(UserRoleMapper roleMapp final SamlToken token = new SamlToken(new byte[0], Collections.singletonList(""), authenticatingRealm); final SamlAttributes attributes = new SamlAttributes( - new SamlNameId(NameIDType.PERSISTENT, nameIdValue, idp.getEntityID(), sp.getEntityId(), null), - randomAlphaOfLength(16), - Arrays.asList( - new SamlAttributes.SamlAttribute("urn:oid:0.9.2342.19200300.100.1.1", "uid", Collections.singletonList(uidValue)), - new SamlAttributes.SamlAttribute("urn:oid:1.3.6.1.4.1.5923.1.5.1.1", "groups", Arrays.asList("avengers", "shield")), - new SamlAttributes.SamlAttribute("urn:oid:0.9.2342.19200300.100.1.3", "mail", Arrays.asList("cbarton@shield.gov")) - )); + new SamlNameId(NameIDType.PERSISTENT, nameIdValue, idp.getEntityID(), sp.getEntityId(), null), + randomAlphaOfLength(16), + Arrays.asList( + new SamlAttributes.SamlAttribute("urn:oid:0.9.2342.19200300.100.1.1", "uid", Collections.singletonList(uidValue)), + new SamlAttributes.SamlAttribute("urn:oid:1.3.6.1.4.1.5923.1.5.1.1", "groups", Arrays.asList("avengers", "shield")), + new SamlAttributes.SamlAttribute("urn:oid:0.9.2342.19200300.100.1.3", "mail", Arrays.asList("cbarton@shield.gov")) + ) + ); when(authenticator.authenticate(token)).thenReturn(attributes); final PlainActionFuture> future = new PlainActionFuture<>(); @@ -313,8 +364,14 @@ private void initializeRealms(Realm... realms) { } } - public SamlRealm buildRealm(RealmConfig config, UserRoleMapper roleMapper, SamlAuthenticator authenticator, - SamlLogoutRequestHandler logoutHandler, EntityDescriptor idp, SpConfiguration sp) throws Exception { + public SamlRealm buildRealm( + RealmConfig config, + UserRoleMapper roleMapper, + SamlAuthenticator authenticator, + SamlLogoutRequestHandler logoutHandler, + EntityDescriptor idp, + SpConfiguration sp + ) throws Exception { try { return new SamlRealm(config, roleMapper, authenticator, logoutHandler, mock(SamlLogoutResponseHandler.class), () -> idp, sp); } catch (SettingsException e) { @@ -326,9 +383,9 @@ public SamlRealm buildRealm(RealmConfig config, UserRoleMapper roleMapper, SamlA public void testAttributeSelectionWithRegex() throws Exception { final boolean useFriendlyName = randomBoolean(); final Settings settings = Settings.builder() - .put(REALM_SETTINGS_PREFIX + ".attributes.principal", useFriendlyName ? "mail" : "urn:oid:0.9.2342.19200300.100.1.3") - .put(REALM_SETTINGS_PREFIX + ".attribute_patterns.principal", "^(.+)@\\w+.example.com$") - .build(); + .put(REALM_SETTINGS_PREFIX + ".attributes.principal", useFriendlyName ? "mail" : "urn:oid:0.9.2342.19200300.100.1.3") + .put(REALM_SETTINGS_PREFIX + ".attribute_patterns.principal", "^(.+)@\\w+.example.com$") + .build(); final RealmConfig config = buildConfig(settings); @@ -336,11 +393,16 @@ public void testAttributeSelectionWithRegex() throws Exception { final SamlRealm.AttributeParser parser = SamlRealm.AttributeParser.forSetting(logger, principalSetting, config, false); final SamlAttributes attributes = new SamlAttributes( - new SamlNameId(NameIDType.TRANSIENT, randomAlphaOfLength(24), null, null, null), - randomAlphaOfLength(16), - Collections.singletonList(new SamlAttributes.SamlAttribute("urn:oid:0.9.2342.19200300.100.1.3", "mail", - Arrays.asList("john.smith@personal.example.net", "john.smith@corporate.example.com", "jsmith@corporate.example.com") - ))); + new SamlNameId(NameIDType.TRANSIENT, randomAlphaOfLength(24), null, null, null), + randomAlphaOfLength(16), + Collections.singletonList( + new SamlAttributes.SamlAttribute( + "urn:oid:0.9.2342.19200300.100.1.3", + "mail", + Arrays.asList("john.smith@personal.example.net", "john.smith@corporate.example.com", "jsmith@corporate.example.com") + ) + ) + ); final List strings = parser.getAttribute(attributes); assertThat("For attributes: " + strings, strings, contains("john.smith", "jsmith")); @@ -348,9 +410,9 @@ public void testAttributeSelectionWithRegex() throws Exception { public void testSettingPatternWithoutAttributeThrowsSettingsException() throws Exception { final Settings realmSettings = Settings.builder() - .put(getFullSettingKey(REALM_NAME, SamlRealmSettings.PRINCIPAL_ATTRIBUTE.getAttribute()), "nameid") - .put(getFullSettingKey(REALM_NAME, SamlRealmSettings.NAME_ATTRIBUTE.getPattern()), "^\\s*(\\S.*\\S)\\s*$") - .build(); + .put(getFullSettingKey(REALM_NAME, SamlRealmSettings.PRINCIPAL_ATTRIBUTE.getAttribute()), "nameid") + .put(getFullSettingKey(REALM_NAME, SamlRealmSettings.NAME_ATTRIBUTE.getPattern()), "^\\s*(\\S.*\\S)\\s*$") + .build(); final RealmConfig config = buildConfig(realmSettings); final UserRoleMapper roleMapper = mock(UserRoleMapper.class); @@ -359,8 +421,10 @@ public void testSettingPatternWithoutAttributeThrowsSettingsException() throws E final EntityDescriptor idp = mockIdp(); final SpConfiguration sp = new SpConfiguration("", "https://saml/", null, null, null, Collections.emptyList()); - final SettingsException settingsException = expectThrows(SettingsException.class, - () -> buildRealm(config, roleMapper, authenticator, logoutHandler, idp, sp)); + final SettingsException settingsException = expectThrows( + SettingsException.class, + () -> buildRealm(config, roleMapper, authenticator, logoutHandler, idp, sp) + ); assertThat(settingsException.getMessage(), containsString(REALM_SETTINGS_PREFIX + ".attribute_patterns.name")); assertThat(settingsException.getMessage(), containsString(REALM_SETTINGS_PREFIX + ".attributes.name")); } @@ -375,8 +439,10 @@ public void testMissingPrincipalSettingThrowsSettingsException() throws Exceptio final EntityDescriptor idp = mockIdp(); final SpConfiguration sp = new SpConfiguration("", "https://saml/", null, null, null, Collections.emptyList()); - final SettingsException settingsException = expectThrows(SettingsException.class, - () -> buildRealm(config, roleMapper, authenticator, logoutHandler, idp, sp)); + final SettingsException settingsException = expectThrows( + SettingsException.class, + () -> buildRealm(config, roleMapper, authenticator, logoutHandler, idp, sp) + ); assertThat(settingsException.getMessage(), containsString(REALM_SETTINGS_PREFIX + ".attributes.principal")); } @@ -388,9 +454,9 @@ public void testNonMatchingPrincipalPatternThrowsSamlException() throws Exceptio final SamlLogoutRequestHandler logoutHandler = mock(SamlLogoutRequestHandler.class); final Settings realmSettings = Settings.builder() - .put(getFullSettingKey(REALM_NAME, SamlRealmSettings.PRINCIPAL_ATTRIBUTE.getAttribute()), "mail") - .put(getFullSettingKey(REALM_NAME, SamlRealmSettings.PRINCIPAL_ATTRIBUTE.getPattern()), "^([^@]+)@mycorp\\.example\\.com$") - .build(); + .put(getFullSettingKey(REALM_NAME, SamlRealmSettings.PRINCIPAL_ATTRIBUTE.getAttribute()), "mail") + .put(getFullSettingKey(REALM_NAME, SamlRealmSettings.PRINCIPAL_ATTRIBUTE.getPattern()), "^([^@]+)@mycorp\\.example\\.com$") + .build(); final RealmConfig config = buildConfig(realmSettings); @@ -400,11 +466,12 @@ public void testNonMatchingPrincipalPatternThrowsSamlException() throws Exceptio for (String mail : Arrays.asList("john@your-corp.example.com", "john@mycorp.example.com.example.net", "john")) { final SamlAttributes attributes = new SamlAttributes( - new SamlNameId(NameIDType.TRANSIENT, randomAlphaOfLength(12), null, null, null), - randomAlphaOfLength(16), - Collections.singletonList( - new SamlAttributes.SamlAttribute("urn:oid:0.9.2342.19200300.100.1.3", "mail", Collections.singletonList(mail)) - )); + new SamlNameId(NameIDType.TRANSIENT, randomAlphaOfLength(12), null, null, null), + randomAlphaOfLength(16), + Collections.singletonList( + new SamlAttributes.SamlAttribute("urn:oid:0.9.2342.19200300.100.1.3", "mail", Collections.singletonList(mail)) + ) + ); when(authenticator.authenticate(token)).thenReturn(attributes); final PlainActionFuture> future = new PlainActionFuture<>(); @@ -442,20 +509,26 @@ public void testCreateCredentialFromPemFiles() throws Exception { public void testCreateEncryptionCredentialFromKeyStore() throws Exception { assumeFalse("Can't run in a FIPS JVM, PKCS12 keystores are not usable", inFipsJvm()); final Path dir = createTempDir(); - final Settings.Builder builder = Settings.builder() - .put(REALM_SETTINGS_PREFIX + ".type", "saml") - .put("path.home", dir); + final Settings.Builder builder = Settings.builder().put(REALM_SETTINGS_PREFIX + ".type", "saml").put("path.home", dir); final Path ksFile = dir.resolve("cred.p12"); final boolean testMultipleEncryptionKeyPair = randomBoolean(); final Tuple certKeyPair1 = readKeyPair("RSA_4096"); final Tuple certKeyPair2 = readKeyPair("RSA_2048"); final KeyStore ks = KeyStore.getInstance("PKCS12"); ks.load(null); - ks.setKeyEntry(getAliasName(certKeyPair1), certKeyPair1.v2(), "key-password".toCharArray(), - new Certificate[] { certKeyPair1.v1() }); + ks.setKeyEntry( + getAliasName(certKeyPair1), + certKeyPair1.v2(), + "key-password".toCharArray(), + new Certificate[] { certKeyPair1.v1() } + ); if (testMultipleEncryptionKeyPair) { - ks.setKeyEntry(getAliasName(certKeyPair2), certKeyPair2.v2(), "key-password".toCharArray(), - new Certificate[] { certKeyPair2.v1() }); + ks.setKeyEntry( + getAliasName(certKeyPair2), + certKeyPair2.v2(), + "key-password".toCharArray(), + new Certificate[] { certKeyPair2.v1() } + ); } try (OutputStream out = Files.newOutputStream(ksFile)) { ks.store(out, "ks-password".toCharArray()); @@ -480,11 +553,15 @@ public void testCreateEncryptionCredentialFromKeyStore() throws Exception { final int expectedCredentials = (isEncryptionKeyStoreAliasSet) ? 1 : (testMultipleEncryptionKeyPair) ? 2 : 1; assertEquals("Expected encryption credentials size does not match", expectedCredentials, credentials.size()); credentials.stream().forEach((credential) -> { - assertTrue("Unexpected private key in the list of encryption credentials", - Arrays.asList(new PrivateKey[] { certKeyPair1.v2(), certKeyPair2.v2() }).contains(credential.getPrivateKey())); - assertTrue("Unexpected public key in the list of encryption credentials", - Arrays.asList(new PublicKey[] { (certKeyPair1.v1()).getPublicKey(), certKeyPair2.v1().getPublicKey() }) - .contains(credential.getPublicKey())); + assertTrue( + "Unexpected private key in the list of encryption credentials", + Arrays.asList(new PrivateKey[] { certKeyPair1.v2(), certKeyPair2.v2() }).contains(credential.getPrivateKey()) + ); + assertTrue( + "Unexpected public key in the list of encryption credentials", + Arrays.asList(new PublicKey[] { (certKeyPair1.v1()).getPublicKey(), certKeyPair2.v1().getPublicKey() }) + .contains(credential.getPublicKey()) + ); }); } @@ -498,10 +575,18 @@ public void testCreateSigningCredentialFromKeyStoreSuccessScenarios() throws Exc final KeyStore ks = KeyStore.getInstance("PKCS12"); ks.load(null); - ks.setKeyEntry(getAliasName(certKeyPair1), certKeyPair1.v2(), "key-password".toCharArray(), - new Certificate[] { certKeyPair1.v1() }); - ks.setKeyEntry(getAliasName(certKeyPair2), certKeyPair2.v2(), "key-password".toCharArray(), - new Certificate[] { certKeyPair2.v1() }); + ks.setKeyEntry( + getAliasName(certKeyPair1), + certKeyPair1.v2(), + "key-password".toCharArray(), + new Certificate[] { certKeyPair1.v1() } + ); + ks.setKeyEntry( + getAliasName(certKeyPair2), + certKeyPair2.v2(), + "key-password".toCharArray(), + new Certificate[] { certKeyPair2.v1() } + ); try (OutputStream out = Files.newOutputStream(ksFile)) { ks.store(out, "ks-password".toCharArray()); } @@ -541,13 +626,25 @@ public void testCreateSigningCredentialFromKeyStoreFailureScenarios() throws Exc ks.load(null); final boolean noRSAKeysInKS = randomBoolean(); if (noRSAKeysInKS == false) { - ks.setKeyEntry(getAliasName(certKeyPair1), certKeyPair1.v2(), "key-password".toCharArray(), - new Certificate[] { certKeyPair1.v1() }); - ks.setKeyEntry(getAliasName(certKeyPair2), certKeyPair2.v2(), "key-password".toCharArray(), - new Certificate[] { certKeyPair2.v1() }); + ks.setKeyEntry( + getAliasName(certKeyPair1), + certKeyPair1.v2(), + "key-password".toCharArray(), + new Certificate[] { certKeyPair1.v1() } + ); + ks.setKeyEntry( + getAliasName(certKeyPair2), + certKeyPair2.v2(), + "key-password".toCharArray(), + new Certificate[] { certKeyPair2.v1() } + ); } - ks.setKeyEntry(getAliasName(certKeyPair3), certKeyPair3.v2(), "key-password".toCharArray(), - new Certificate[] { certKeyPair3.v1() }); + ks.setKeyEntry( + getAliasName(certKeyPair3), + certKeyPair3.v2(), + "key-password".toCharArray(), + new Certificate[] { certKeyPair3.v1() } + ); try (OutputStream out = Files.newOutputStream(ksFile)) { ks.store(out, "ks-password".toCharArray()); } @@ -580,41 +677,61 @@ public void testCreateSigningCredentialFromKeyStoreFailureScenarios() throws Exc if (isSigningKeyStoreAliasSet) { if (chosenAliasCertKeyPair == null) { // Unknown alias, this must throw exception - final IllegalArgumentException illegalArgumentException = - expectThrows(IllegalArgumentException.class, () -> SamlRealm.buildSigningConfiguration(realmConfig)); + final IllegalArgumentException illegalArgumentException = expectThrows( + IllegalArgumentException.class, + () -> SamlRealm.buildSigningConfiguration(realmConfig) + ); final String expectedErrorMessage = "The configured key store for " - + RealmSettings.realmSettingPrefix(realmConfig.identifier()) + "signing." - + " does not have a key associated with alias [" + unknownAlias + "] " + "(from setting " - + RealmSettings.getFullSettingKey(realmConfig, SamlRealmSettings.SIGNING_KEY_ALIAS) + ")"; + + RealmSettings.realmSettingPrefix(realmConfig.identifier()) + + "signing." + + " does not have a key associated with alias [" + + unknownAlias + + "] " + + "(from setting " + + RealmSettings.getFullSettingKey(realmConfig, SamlRealmSettings.SIGNING_KEY_ALIAS) + + ")"; assertEquals(expectedErrorMessage, illegalArgumentException.getLocalizedMessage()); } else { final String chosenAliasName = getAliasName(chosenAliasCertKeyPair); // Since this is unsupported key type, this must throw exception - final IllegalArgumentException illegalArgumentException = - expectThrows(IllegalArgumentException.class, () -> SamlRealm.buildSigningConfiguration(realmConfig)); - final String expectedErrorMessage = "The key associated with alias [" + chosenAliasName + "] " + "(from setting " - + RealmSettings.getFullSettingKey(realmConfig, SamlRealmSettings.SIGNING_KEY_ALIAS) - + ") uses unsupported key algorithm type [" + chosenAliasCertKeyPair.v2().getAlgorithm() - + "], only RSA is supported"; + final IllegalArgumentException illegalArgumentException = expectThrows( + IllegalArgumentException.class, + () -> SamlRealm.buildSigningConfiguration(realmConfig) + ); + final String expectedErrorMessage = "The key associated with alias [" + + chosenAliasName + + "] " + + "(from setting " + + RealmSettings.getFullSettingKey(realmConfig, SamlRealmSettings.SIGNING_KEY_ALIAS) + + ") uses unsupported key algorithm type [" + + chosenAliasCertKeyPair.v2().getAlgorithm() + + "], only RSA is supported"; assertEquals(expectedErrorMessage, illegalArgumentException.getLocalizedMessage()); } } else { if (noRSAKeysInKS) { // Should throw exception as no RSA keys in the keystore - final IllegalArgumentException illegalArgumentException = - expectThrows(IllegalArgumentException.class, () -> SamlRealm.buildSigningConfiguration(realmConfig)); + final IllegalArgumentException illegalArgumentException = expectThrows( + IllegalArgumentException.class, + () -> SamlRealm.buildSigningConfiguration(realmConfig) + ); final String expectedErrorMessage = "The configured key store for " - + RealmSettings.realmSettingPrefix(realmConfig.identifier()) + "signing." - + " does not contain any RSA key pairs"; + + RealmSettings.realmSettingPrefix(realmConfig.identifier()) + + "signing." + + " does not contain any RSA key pairs"; assertEquals(expectedErrorMessage, illegalArgumentException.getLocalizedMessage()); } else { // Should throw exception when multiple signing keys found and alias not set - final IllegalArgumentException illegalArgumentException = - expectThrows(IllegalArgumentException.class, () -> SamlRealm.buildSigningConfiguration(realmConfig)); + final IllegalArgumentException illegalArgumentException = expectThrows( + IllegalArgumentException.class, + () -> SamlRealm.buildSigningConfiguration(realmConfig) + ); final String expectedErrorMessage = "The configured key store for " - + RealmSettings.realmSettingPrefix(realmConfig.identifier()) + "signing." - + " has multiple keys but no alias has been specified (from setting " - + RealmSettings.getFullSettingKey(realmConfig, SamlRealmSettings.SIGNING_KEY_ALIAS) + ")"; + + RealmSettings.realmSettingPrefix(realmConfig.identifier()) + + "signing." + + " has multiple keys but no alias has been specified (from setting " + + RealmSettings.getFullSettingKey(realmConfig, SamlRealmSettings.SIGNING_KEY_ALIAS) + + ")"; assertEquals(expectedErrorMessage, illegalArgumentException.getLocalizedMessage()); } } @@ -622,8 +739,10 @@ public void testCreateSigningCredentialFromKeyStoreFailureScenarios() throws Exc private String getAliasName(final Tuple certKeyPair) { // Keys are pre-generated with the same name, so add the serial no to the alias so that keystore entries won't be overwritten - return certKeyPair.v1().getSubjectX500Principal().getName().toLowerCase(Locale.US) + "-"+ - certKeyPair.v1().getSerialNumber()+"-alias"; + return certKeyPair.v1().getSubjectX500Principal().getName().toLowerCase(Locale.US) + + "-" + + certKeyPair.v1().getSerialNumber() + + "-alias"; } public void testBuildLogoutRequest() throws Exception { @@ -642,7 +761,7 @@ public void testBuildLogoutRequest() throws Exception { final SamlLogoutRequestHandler logoutHandler = mock(SamlLogoutRequestHandler.class); final Settings.Builder realmSettings = Settings.builder() - .put(getFullSettingKey(REALM_NAME, SamlRealmSettings.PRINCIPAL_ATTRIBUTE.getAttribute()), "uid"); + .put(getFullSettingKey(REALM_NAME, SamlRealmSettings.PRINCIPAL_ATTRIBUTE.getAttribute()), "uid"); if (useSingleLogout != null) { realmSettings.put(getFullSettingKey(REALM_NAME, SamlRealmSettings.IDP_SINGLE_LOGOUT), useSingleLogout.booleanValue()); } @@ -715,12 +834,18 @@ private Settings.Builder buildSettings(String idpMetadataPath) { secureSettings.setString(REALM_SETTINGS_PREFIX + ".ssl.secure_key_passphrase", "testnode"); return Settings.builder() .put(REALM_SETTINGS_PREFIX + ".ssl.verification_mode", "certificate") - .put(REALM_SETTINGS_PREFIX + ".ssl.key", - getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.pem")) - .put(REALM_SETTINGS_PREFIX + ".ssl.certificate", - getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt")) - .put(REALM_SETTINGS_PREFIX + ".ssl.certificate_authorities", - getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt")) + .put( + REALM_SETTINGS_PREFIX + ".ssl.key", + getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.pem") + ) + .put( + REALM_SETTINGS_PREFIX + ".ssl.certificate", + getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt") + ) + .put( + REALM_SETTINGS_PREFIX + ".ssl.certificate_authorities", + getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt") + ) .put(getFullSettingKey(REALM_NAME, SamlRealmSettings.IDP_METADATA_PATH), idpMetadataPath) .put(getFullSettingKey(REALM_NAME, SamlRealmSettings.IDP_ENTITY_ID), TEST_IDP_ENTITY_ID) .put(getFullSettingKey(REALM_NAME, SamlRealmSettings.IDP_METADATA_HTTP_REFRESH), METADATA_REFRESH + "ms") @@ -729,23 +854,26 @@ private Settings.Builder buildSettings(String idpMetadataPath) { } private RealmConfig buildConfig(Settings realmSettings) { - final Settings settings = Settings.builder() - .put("path.home", createTempDir()) - .put(realmSettings).build(); + final Settings settings = Settings.builder().put("path.home", createTempDir()).put(realmSettings).build(); final Environment env = TestEnvironment.newEnvironment(settings); final RealmConfig.RealmIdentifier realmIdentifier = new RealmConfig.RealmIdentifier("saml", REALM_NAME); - return new RealmConfig(realmIdentifier, + return new RealmConfig( + realmIdentifier, Settings.builder().put(settings).put(getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0).build(), - env, threadContext); + env, + threadContext + ); } private RealmConfig realmConfigFromGlobalSettings(Settings globalSettings) { final Environment env = TestEnvironment.newEnvironment(globalSettings); final RealmConfig.RealmIdentifier realmIdentifier = new RealmConfig.RealmIdentifier("saml", REALM_NAME); - return new RealmConfig(realmIdentifier, + return new RealmConfig( + realmIdentifier, Settings.builder().put(globalSettings).put(getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0).build(), env, - new ThreadContext(globalSettings)); + new ThreadContext(globalSettings) + ); } private void assertIdp1MetadataParsedCorrectly(EntityDescriptor descriptor) { @@ -765,9 +893,9 @@ private static List getProtocols() { if (JavaVersion.current().compareTo(JavaVersion.parse("12")) < 0) { return List.of("TLSv1.2"); } else { - JavaVersion full = - AccessController.doPrivileged( - (PrivilegedAction) () -> JavaVersion.parse(System.getProperty("java.version"))); + JavaVersion full = AccessController.doPrivileged( + (PrivilegedAction) () -> JavaVersion.parse(System.getProperty("java.version")) + ); if (full.compareTo(JavaVersion.parse("12.0.1")) < 0) { return List.of("TLSv1.2"); } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlRedirectTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlRedirectTests.java index 8c77e56dc7195..fee65587a296a 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlRedirectTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlRedirectTests.java @@ -46,44 +46,70 @@ public class SamlRedirectTests extends SamlTestCase { public void testRedirectUrlWithoutRelayStateOrSigning() { final SamlRedirect redirect = new SamlRedirect(buildLogoutRequest(LOGOUT_URL), NO_SIGNING); final String url = redirect.getRedirectUrl(); - assertThat(url, equalTo(LOGOUT_URL + "?SAMLRequest=nZFBa4QwFIT%2FSnh3Naa2ax%2FqsiAFYdtDu91DLyVo2AY0cX2x9Oc36gpLCz30mAwz3" + - "wwv2351LftUA2lrcohDDkyZ2jbanHJ4PTwEKWyLjGTXih739mRH96zOoyLHvNMQLlIO42DQStKERnaK0NX4snvcowg59oN1trYtsNIbtZFupn04" + - "1xNGkW760HkhmrKidoYAq8oc3nUTi5vk9m6T3vsfolFVhpw0LgfB4zTgcRAnByEw2SDnIef8DdhxnePZcCmPs3m4Lv13Z0mkhqknFL96ZtF15kp" + - "48hlV%2BS%2FCJAbL0sBP5StgiSwuzx8HKL4B")); + assertThat( + url, + equalTo( + LOGOUT_URL + + "?SAMLRequest=nZFBa4QwFIT%2FSnh3Naa2ax%2FqsiAFYdtDu91DLyVo2AY0cX2x9Oc36gpLCz30mAwz3" + + "wwv2351LftUA2lrcohDDkyZ2jbanHJ4PTwEKWyLjGTXih739mRH96zOoyLHvNMQLlIO42DQStKERnaK0NX4snvcowg59oN1trYtsNIbtZFupn04" + + "1xNGkW760HkhmrKidoYAq8oc3nUTi5vk9m6T3vsfolFVhpw0LgfB4zTgcRAnByEw2SDnIef8DdhxnePZcCmPs3m4Lv13Z0mkhqknFL96ZtF15kp" + + "48hlV%2BS%2FCJAbL0sBP5StgiSwuzx8HKL4B" + ) + ); } public void testRedirectUrlWithRelayStateAndSigning() throws Exception { - final SigningConfiguration signing = - new SigningConfiguration(singleton("*"), (X509Credential) buildOpenSamlCredential(readRandomKeyPair()).get(0)); + final SigningConfiguration signing = new SigningConfiguration( + singleton("*"), + (X509Credential) buildOpenSamlCredential(readRandomKeyPair()).get(0) + ); final SamlRedirect redirect = new SamlRedirect(buildLogoutRequest(LOGOUT_URL), signing); final String url = redirect.getRedirectUrl("hello"); - assertThat(url, startsWith(LOGOUT_URL + "?SAMLRequest=nZFBa4QwFIT%2FSnh3Naa2ax%2FqsiAFYdtDu91DLyVo2AY0cX2x9Oc36gpLC" + - "z30mAwz3wwv2351LftUA2lrcohDDkyZ2jbanHJ4PTwEKWyLjGTXih739mRH96zOoyLHvNMQLlIO42DQStKERnaK0NX4snvcowg59oN1trY" + - "tsNIbtZFupn041xNGkW760HkhmrKidoYAq8oc3nUTi5vk9m6T3vsfolFVhpw0LgfB4zTgcRAnByEw2SDnIef8DdhxnePZcCmPs3m4Lv13Z" + - "0mkhqknFL96ZtF15kp48hlV%2BS%2FCJAbL0sBP5StgiSwuzx8HKL4B" + - "&RelayState=hello" + - "&SigAlg=http%3A%2F%2Fwww.w3.org%2F2001%2F04%2Fxmldsig-more%23rsa-sha256" + - "&Signature=")); + assertThat( + url, + startsWith( + LOGOUT_URL + + "?SAMLRequest=nZFBa4QwFIT%2FSnh3Naa2ax%2FqsiAFYdtDu91DLyVo2AY0cX2x9Oc36gpLC" + + "z30mAwz3wwv2351LftUA2lrcohDDkyZ2jbanHJ4PTwEKWyLjGTXih739mRH96zOoyLHvNMQLlIO42DQStKERnaK0NX4snvcowg59oN1trY" + + "tsNIbtZFupn041xNGkW760HkhmrKidoYAq8oc3nUTi5vk9m6T3vsfolFVhpw0LgfB4zTgcRAnByEw2SDnIef8DdhxnePZcCmPs3m4Lv13Z" + + "0mkhqknFL96ZtF15kp48hlV%2BS%2FCJAbL0sBP5StgiSwuzx8HKL4B" + + "&RelayState=hello" + + "&SigAlg=http%3A%2F%2Fwww.w3.org%2F2001%2F04%2Fxmldsig-more%23rsa-sha256" + + "&Signature=" + ) + ); } public void testRedirectUrlWithExistingParameters() { final SamlRedirect redirect = new SamlRedirect(buildLogoutRequest(LOGOUT_URL + "?a=xyz"), NO_SIGNING); final String url = redirect.getRedirectUrl("foo"); - assertThat(url, equalTo(LOGOUT_URL + "?a=xyz" + - "&SAMLRequest=nZFBS8QwFIT%2FSnn3tmmsbn00LUIRCqsHXT14kdCGNdAmtS%2BV1V9v2u7CouDBYzLMzDe8vDz0XfChRtLWCE" + - "giBoEyjW212Qt42t2GGZRFTrLv%2BIBbu7eTe1DvkyIXeKchXCUB02jQStKERvaK0DX4eHO3RR4xHEbrbGM7CCpv1Ea6pe3NuYE" + - "wjnU7RM4L8ZwVd0tJKcXh8wuCuhLwqtuEX6SXV5vs2v8QTao25KRxAjhLspAlYZLuOMd0g4xFjLEXCJ5PozwBHCfgYh7P0f8ml0" + - "RqnGmh%2BEWbx%2BeZp4Z7n1FX%2F2qYxXBdGvqp7FSwRhbH548zFN8%3D" + - "&RelayState=foo")); + assertThat( + url, + equalTo( + LOGOUT_URL + + "?a=xyz" + + "&SAMLRequest=nZFBS8QwFIT%2FSnn3tmmsbn00LUIRCqsHXT14kdCGNdAmtS%2BV1V9v2u7CouDBYzLMzDe8vDz0XfChRtLWCE" + + "giBoEyjW212Qt42t2GGZRFTrLv%2BIBbu7eTe1DvkyIXeKchXCUB02jQStKERvaK0DX4eHO3RR4xHEbrbGM7CCpv1Ea6pe3NuYE" + + "wjnU7RM4L8ZwVd0tJKcXh8wuCuhLwqtuEX6SXV5vs2v8QTao25KRxAjhLspAlYZLuOMd0g4xFjLEXCJ5PozwBHCfgYh7P0f8ml0" + + "RqnGmh%2BEWbx%2BeZp4Z7n1FX%2F2qYxXBdGvqp7FSwRhbH548zFN8%3D" + + "&RelayState=foo" + ) + ); } public void testRedirectUrlWithTrailingQuestionMark() { final SamlRedirect redirect = new SamlRedirect(buildLogoutRequest(LOGOUT_URL + "?"), NO_SIGNING); final String url = redirect.getRedirectUrl(); - assertThat(url, equalTo(LOGOUT_URL + "?SAMLRequest=nZFPS8QwFMS%2FSnj3tmmsbn30D0IRCqsHXffgRUIb1kCb1L5U%2FPim7R" + - "YWBQ8ek2HmN8PLyq%2B%2BY59qJG1NDnHIgSnT2FabUw4vh%2FsghbLISPadGHBvT3ZyT%2BpjUuSYdxrCVcphGg1aSZrQyF4Rug" + - "af7x72KEKOw2idbWwHrPJGbaRbaO%2FODYRRpNshdF6I5qyoWyAlsLrK4U23sbhKrm926a3%2FIZpUbchJ43IQPE4DHgdxchACkx" + - "1yHnLOX4Edtz0eDuf2uJjHy9Z%2Fl5ZEapyLQvGraBZdZm6ER59RV%2F8izGKwLg38VL4B1sji%2FPxxgeIb")); + assertThat( + url, + equalTo( + LOGOUT_URL + + "?SAMLRequest=nZFPS8QwFMS%2FSnj3tmmsbn30D0IRCqsHXffgRUIb1kCb1L5U%2FPim7R" + + "YWBQ8ek2HmN8PLyq%2B%2BY59qJG1NDnHIgSnT2FabUw4vh%2FsghbLISPadGHBvT3ZyT%2BpjUuSYdxrCVcphGg1aSZrQyF4Rug" + + "af7x72KEKOw2idbWwHrPJGbaRbaO%2FODYRRpNshdF6I5qyoWyAlsLrK4U23sbhKrm926a3%2FIZpUbchJ43IQPE4DHgdxchACkx" + + "1yHnLOX4Edtz0eDuf2uJjHy9Z%2Fl5ZEapyLQvGraBZdZm6ER59RV%2F8izGKwLg38VL4B1sji%2FPxxgeIb" + ) + ); } public void testLogoutRequestSigning() throws Exception { @@ -92,14 +118,13 @@ public void testLogoutRequestSigning() throws Exception { while (invalidCredential.getEntityCertificate().getSerialNumber().equals(credential.getEntityCertificate().getSerialNumber())) { invalidCredential = (X509Credential) buildOpenSamlCredential(readRandomKeyPair()).get(0); } - final SigningConfiguration spConfig = - new SigningConfiguration(singleton("*"), credential); + final SigningConfiguration spConfig = new SigningConfiguration(singleton("*"), credential); final SamlRedirect redirect = new SamlRedirect(buildLogoutRequest(LOGOUT_URL + "?"), spConfig); final String url = redirect.getRedirectUrl(); final String queryParam = url.split("\\?")[1].split("&Signature")[0]; final String signature = validateUrlAndGetSignature(redirect.getRedirectUrl()); - assertThat(validateSignature(queryParam, signature, credential), equalTo(true)); - assertThat(validateSignature(queryParam, signature, invalidCredential), equalTo(false)); + assertThat(validateSignature(queryParam, signature, credential), equalTo(true)); + assertThat(validateSignature(queryParam, signature, invalidCredential), equalTo(false)); assertThat(validateSignature(queryParam.substring(0, queryParam.length() - 5), signature, credential), equalTo(false)); } @@ -114,15 +139,22 @@ public void testAuthnRequestSigning() throws Exception { EntityDescriptor idpDescriptor = buildIdPDescriptor(IDP_URL, IDP_ENTITY_ID); - final SamlRedirect redirect = new SamlRedirect(new SamlAuthnRequestBuilder(sp, SAMLConstants.SAML2_POST_BINDING_URI, - idpDescriptor, SAMLConstants.SAML2_REDIRECT_BINDING_URI, Clock.systemUTC()).build(), signingConfig); + final SamlRedirect redirect = new SamlRedirect( + new SamlAuthnRequestBuilder( + sp, + SAMLConstants.SAML2_POST_BINDING_URI, + idpDescriptor, + SAMLConstants.SAML2_REDIRECT_BINDING_URI, + Clock.systemUTC() + ).build(), + signingConfig + ); final String url = redirect.getRedirectUrl(); final String queryParam = url.split("\\?")[1].split("&Signature")[0]; final String signature = validateUrlAndGetSignature(redirect.getRedirectUrl()); assertThat(validateSignature(queryParam, signature, credential), equalTo(true)); assertThat(validateSignature(queryParam, signature, invalidCredential), equalTo(false)); - assertThat(validateSignature(queryParam.substring(0, queryParam.length() - 5), signature, credential), - equalTo(false)); + assertThat(validateSignature(queryParam.substring(0, queryParam.length() - 5), signature, credential), equalTo(false)); } private String parseAndUrlDecodeParameter(String parameter) throws UnsupportedEncodingException { diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlResponseHandlerTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlResponseHandlerTests.java index 7deb79225cb63..c63f3c2376564 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlResponseHandlerTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlResponseHandlerTests.java @@ -10,8 +10,8 @@ import org.apache.logging.log4j.LogManager; import org.apache.xml.security.Init; import org.apache.xml.security.encryption.XMLCipher; -import org.elasticsearch.core.Tuple; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.core.Tuple; import org.elasticsearch.xpack.core.watcher.watch.ClockMock; import org.junit.AfterClass; import org.junit.BeforeClass; @@ -40,6 +40,7 @@ import java.util.List; import java.util.function.Supplier; import java.util.stream.Collectors; + import javax.crypto.Cipher; import javax.xml.crypto.dsig.CanonicalizationMethod; import javax.xml.crypto.dsig.DigestMethod; @@ -126,11 +127,19 @@ public static void cleanup() { protected SpConfiguration getSpConfiguration(List reqAuthnCtxClassRef) { final SigningConfiguration signingConfiguration = new SigningConfiguration( Collections.singleton("*"), - (X509Credential) buildOpenSamlCredential(spSigningCertificatePair).get(0)); + (X509Credential) buildOpenSamlCredential(spSigningCertificatePair).get(0) + ); final List spEncryptionCredentials = buildOpenSamlCredential(spEncryptionCertificatePairs).stream() - .map((cred) -> (X509Credential) cred).collect(Collectors.toList()); - return new SpConfiguration(SP_ENTITY_ID, SP_ACS_URL, SP_LOGOUT_URL, signingConfiguration, spEncryptionCredentials, - reqAuthnCtxClassRef); + .map((cred) -> (X509Credential) cred) + .collect(Collectors.toList()); + return new SpConfiguration( + SP_ENTITY_ID, + SP_ACS_URL, + SP_LOGOUT_URL, + signingConfiguration, + spEncryptionCredentials, + reqAuthnCtxClassRef + ); } protected IdpConfiguration getIdpConfiguration(Supplier> credentials) { @@ -159,26 +168,33 @@ protected Document parseDocument(String xml) throws ParserConfigurationException protected String getSignatureAlgorithmURI(PrivateKey key) { String algoUri = null; switch (key.getAlgorithm()) { - case "RSA": - algoUri = randomFrom("http://www.w3.org/2001/04/xmldsig-more#rsa-sha256", - "http://www.w3.org/2001/04/xmldsig-more#rsa-sha512"); - break; - case "DSA": - algoUri = "http://www.w3.org/2009/xmldsig11#dsa-sha256"; - break; - case "EC": - algoUri = randomFrom("http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha256", - "http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha512"); - break; - default: - throw new IllegalArgumentException("Unsupported algorithm : " + key.getAlgorithm() - + " for signature, allowed values for private key algorithm are [RSA, DSA, EC]"); + case "RSA": + algoUri = randomFrom( + "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256", + "http://www.w3.org/2001/04/xmldsig-more#rsa-sha512" + ); + break; + case "DSA": + algoUri = "http://www.w3.org/2009/xmldsig11#dsa-sha256"; + break; + case "EC": + algoUri = randomFrom( + "http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha256", + "http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha512" + ); + break; + default: + throw new IllegalArgumentException( + "Unsupported algorithm : " + + key.getAlgorithm() + + " for signature, allowed values for private key algorithm are [RSA, DSA, EC]" + ); } return algoUri; } protected void signElement(Element parent, String c14nMethod) throws Exception { - //We need to explicitly set the Id attribute, "ID" is just our convention + // We need to explicitly set the Id attribute, "ID" is just our convention parent.setIdAttribute("ID", true); final String refID = "#" + parent.getAttribute("ID"); final X509Certificate certificate = idpSigningCertificatePair.v1(); @@ -211,13 +227,14 @@ protected void signElement(Element parent, String c14nMethod) throws Exception { signature.sign(dsc); } - protected void signSignableObject( - SignableSAMLObject signableObject, String c14nMethod, Tuple keyPair) + protected void signSignableObject(SignableSAMLObject signableObject, String c14nMethod, Tuple keyPair) throws Exception { final Signature signature = SamlUtils.buildObject(Signature.class, Signature.DEFAULT_ELEMENT_NAME); final Credential credential = new BasicCredential(keyPair.v1().getPublicKey(), keyPair.v2()); - final org.opensaml.xmlsec.signature.KeyInfo kf = SamlUtils.buildObject(org.opensaml.xmlsec.signature.KeyInfo.class, - org.opensaml.xmlsec.signature.KeyInfo.DEFAULT_ELEMENT_NAME); + final org.opensaml.xmlsec.signature.KeyInfo kf = SamlUtils.buildObject( + org.opensaml.xmlsec.signature.KeyInfo.class, + org.opensaml.xmlsec.signature.KeyInfo.DEFAULT_ELEMENT_NAME + ); KeyInfoSupport.addCertificate(kf, keyPair.v1()); signature.setSigningCredential(credential); signature.setSignatureAlgorithm(getSignatureAlgorithmURI(keyPair.v2())); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlSpMetadataBuilderTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlSpMetadataBuilderTests.java index 2f5529ef6a014..50584c46666b2 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlSpMetadataBuilderTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlSpMetadataBuilderTests.java @@ -49,41 +49,44 @@ public void setup() throws Exception { public void testBuildMinimalistMetadata() throws Exception { final EntityDescriptor descriptor = new SamlSpMetadataBuilder(Locale.getDefault(), "https://my.sp.example.net/") - .assertionConsumerServiceUrl("https://my.sp.example.net/saml/acs/post") - .build(); + .assertionConsumerServiceUrl("https://my.sp.example.net/saml/acs/post") + .build(); final Element element = new EntityDescriptorMarshaller().marshall(descriptor); final String xml = SamlUtils.toString(element); - assertThat(xml, equalTo("" + - "" + - "" + - "" + - "" + - "" - )); + assertThat( + xml, + equalTo( + "" + + "" + + "" + + "" + + "" + + "" + ) + ); assertValidXml(xml); } public void testBuildFullMetadata() throws Exception { - final EntityDescriptor descriptor = new SamlSpMetadataBuilder(Locale.US, "https://kibana.apps.hydra/") - .serviceName("Hydra Kibana") - .nameIdFormat(NameID.PERSISTENT) - .withAttribute("uid", "urn:oid:0.9.2342.19200300.100.1.1") - .withAttribute("mail", "urn:oid:0.9.2342.19200300.100.1.3") - .withAttribute("groups", "urn:oid:1.3.6.1.4.1.5923.1.5.1.1") - .withAttribute(null, "urn:oid:2.16.840.1.113730.3.1.241") - .withAttribute(null, "urn:oid:1.3.6.1.4.1.5923.1.1.1.6") - .assertionConsumerServiceUrl("https://kibana.apps.hydra/saml/acs") - .singleLogoutServiceUrl("https://kibana.apps.hydra/saml/logout") - .authnRequestsSigned(true) - .signingCertificate(certificate) - .encryptionCertificates(Arrays.asList(certificate)) - .organization("Hydra", "Hydra", "https://hail.hydra/") - .withContact("administrative", "Wolfgang", "von Strucker", "baron.strucker@supreme.hydra") - .withContact("technical", "Paul", "Ebersol", "pne@tech.hydra") - .build(); + final EntityDescriptor descriptor = new SamlSpMetadataBuilder(Locale.US, "https://kibana.apps.hydra/").serviceName("Hydra Kibana") + .nameIdFormat(NameID.PERSISTENT) + .withAttribute("uid", "urn:oid:0.9.2342.19200300.100.1.1") + .withAttribute("mail", "urn:oid:0.9.2342.19200300.100.1.3") + .withAttribute("groups", "urn:oid:1.3.6.1.4.1.5923.1.5.1.1") + .withAttribute(null, "urn:oid:2.16.840.1.113730.3.1.241") + .withAttribute(null, "urn:oid:1.3.6.1.4.1.5923.1.1.1.6") + .assertionConsumerServiceUrl("https://kibana.apps.hydra/saml/acs") + .singleLogoutServiceUrl("https://kibana.apps.hydra/saml/logout") + .authnRequestsSigned(true) + .signingCertificate(certificate) + .encryptionCertificates(Arrays.asList(certificate)) + .organization("Hydra", "Hydra", "https://hail.hydra/") + .withContact("administrative", "Wolfgang", "von Strucker", "baron.strucker@supreme.hydra") + .withContact("technical", "Paul", "Ebersol", "pne@tech.hydra") + .build(); final Element element = new EntityDescriptorMarshaller().marshall(descriptor); final String xml = SamlUtils.toString(element); @@ -172,23 +175,22 @@ public void testBuildFullMetadata() throws Exception { } public void testBuildFullMetadataWithSigningAndTwoEncryptionCerts() throws Exception { - final EntityDescriptor descriptor = new SamlSpMetadataBuilder(Locale.US, "https://kibana.apps.hydra/") - .serviceName("Hydra Kibana") - .nameIdFormat(NameID.PERSISTENT) - .withAttribute("uid", "urn:oid:0.9.2342.19200300.100.1.1") - .withAttribute("mail", "urn:oid:0.9.2342.19200300.100.1.3") - .withAttribute("groups", "urn:oid:1.3.6.1.4.1.5923.1.5.1.1") - .withAttribute(null, "urn:oid:2.16.840.1.113730.3.1.241") - .withAttribute(null, "urn:oid:1.3.6.1.4.1.5923.1.1.1.6") - .assertionConsumerServiceUrl("https://kibana.apps.hydra/saml/acs") - .singleLogoutServiceUrl("https://kibana.apps.hydra/saml/logout") - .authnRequestsSigned(true) - .signingCertificate(threeCertificates[0]) - .encryptionCertificates(Arrays.asList(threeCertificates[1], threeCertificates[2])) - .organization("Hydra", "Hydra", "https://hail.hydra/") - .withContact("administrative", "Wolfgang", "von Strucker", "baron.strucker@supreme.hydra") - .withContact("technical", "Paul", "Ebersol", "pne@tech.hydra") - .build(); + final EntityDescriptor descriptor = new SamlSpMetadataBuilder(Locale.US, "https://kibana.apps.hydra/").serviceName("Hydra Kibana") + .nameIdFormat(NameID.PERSISTENT) + .withAttribute("uid", "urn:oid:0.9.2342.19200300.100.1.1") + .withAttribute("mail", "urn:oid:0.9.2342.19200300.100.1.3") + .withAttribute("groups", "urn:oid:1.3.6.1.4.1.5923.1.5.1.1") + .withAttribute(null, "urn:oid:2.16.840.1.113730.3.1.241") + .withAttribute(null, "urn:oid:1.3.6.1.4.1.5923.1.1.1.6") + .assertionConsumerServiceUrl("https://kibana.apps.hydra/saml/acs") + .singleLogoutServiceUrl("https://kibana.apps.hydra/saml/logout") + .authnRequestsSigned(true) + .signingCertificate(threeCertificates[0]) + .encryptionCertificates(Arrays.asList(threeCertificates[1], threeCertificates[2])) + .organization("Hydra", "Hydra", "https://hail.hydra/") + .withContact("administrative", "Wolfgang", "von Strucker", "baron.strucker@supreme.hydra") + .withContact("technical", "Paul", "Ebersol", "pne@tech.hydra") + .build(); final Element element = new EntityDescriptorMarshaller().marshall(descriptor); final String xml = SamlUtils.toString(element); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlTestCase.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlTestCase.java index 3bfb726d2b4d4..7a942b6b1974a 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlTestCase.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlTestCase.java @@ -12,8 +12,8 @@ import org.elasticsearch.ExceptionsHelper; import org.elasticsearch.common.ssl.KeyStoreUtil; import org.elasticsearch.common.ssl.PemUtils; -import org.elasticsearch.core.Tuple; import org.elasticsearch.core.PathUtils; +import org.elasticsearch.core.Tuple; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.ssl.CertParsingUtils; import org.junit.AfterClass; @@ -55,7 +55,7 @@ public static void setupSaml() throws Exception { private static boolean isTurkishLocale() { return Locale.getDefault().getLanguage().equals(new Locale("tr").getLanguage()) - || Locale.getDefault().getLanguage().equals(new Locale("az").getLanguage()); + || Locale.getDefault().getLanguage().equals(new Locale("az").getLanguage()); } @AfterClass @@ -103,20 +103,26 @@ protected static Tuple readRandomKeyPair(String alg default: keySize = 2048; } - Path keyPath = PathUtils.get(SamlTestCase.class.getResource - ("/org/elasticsearch/xpack/security/authc/saml/saml_" + algorithm + "_" + keySize + ".key").toURI()); - Path certPath = PathUtils.get(SamlTestCase.class.getResource - ("/org/elasticsearch/xpack/security/authc/saml/saml_" + algorithm + "_" + keySize + ".crt").toURI()); + Path keyPath = PathUtils.get( + SamlTestCase.class.getResource("/org/elasticsearch/xpack/security/authc/saml/saml_" + algorithm + "_" + keySize + ".key") + .toURI() + ); + Path certPath = PathUtils.get( + SamlTestCase.class.getResource("/org/elasticsearch/xpack/security/authc/saml/saml_" + algorithm + "_" + keySize + ".crt") + .toURI() + ); X509Certificate certificate = CertParsingUtils.readX509Certificates(Collections.singletonList(certPath))[0]; PrivateKey privateKey = PemUtils.readPrivateKey(keyPath, ""::toCharArray); return new Tuple<>(certificate, privateKey); } protected static Tuple readKeyPair(String keyName) throws Exception { - Path keyPath = PathUtils.get(SamlTestCase.class.getResource - ("/org/elasticsearch/xpack/security/authc/saml/saml_" + keyName + ".key").toURI()); - Path certPath = PathUtils.get(SamlTestCase.class.getResource - ("/org/elasticsearch/xpack/security/authc/saml/saml_" + keyName+ ".crt").toURI()); + Path keyPath = PathUtils.get( + SamlTestCase.class.getResource("/org/elasticsearch/xpack/security/authc/saml/saml_" + keyName + ".key").toURI() + ); + Path certPath = PathUtils.get( + SamlTestCase.class.getResource("/org/elasticsearch/xpack/security/authc/saml/saml_" + keyName + ".crt").toURI() + ); X509Certificate certificate = CertParsingUtils.readX509Certificates(Collections.singletonList(certPath))[0]; PrivateKey privateKey = PemUtils.readPrivateKey(keyPath, ""::toCharArray); return new Tuple<>(certificate, privateKey); @@ -124,8 +130,12 @@ protected static Tuple readKeyPair(String keyName) protected static List buildOpenSamlCredential(final Tuple keyPair) { try { - return Arrays.asList(new X509KeyManagerX509CredentialAdapter( - KeyStoreUtil.createKeyManager(new Certificate[]{keyPair.v1()}, keyPair.v2(), new char[0]), "key")); + return Arrays.asList( + new X509KeyManagerX509CredentialAdapter( + KeyStoreUtil.createKeyManager(new Certificate[] { keyPair.v1() }, keyPair.v2(), new char[0]), + "key" + ) + ); } catch (Exception e) { throw ExceptionsHelper.convertToRuntime(e); @@ -136,7 +146,9 @@ protected static List buildOpenSamlCredential(final List credentials = keyPairs.stream().map((keyPair) -> { try { return new X509KeyManagerX509CredentialAdapter( - KeyStoreUtil.createKeyManager(new Certificate[]{keyPair.v1()}, keyPair.v2(), new char[0]), "key"); + KeyStoreUtil.createKeyManager(new Certificate[] { keyPair.v1() }, keyPair.v2(), new char[0]), + "key" + ); } catch (Exception e) { throw ExceptionsHelper.convertToRuntime(e); } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SigningConfigurationTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SigningConfigurationTests.java index 7191aea929f27..e1428888d47c1 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SigningConfigurationTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SigningConfigurationTests.java @@ -6,8 +6,6 @@ */ package org.elasticsearch.xpack.security.authc.saml; -import java.util.Arrays; - import org.elasticsearch.common.util.set.Sets; import org.junit.AfterClass; import org.junit.BeforeClass; @@ -16,6 +14,8 @@ import org.opensaml.saml.saml2.core.LogoutRequest; import org.opensaml.security.x509.X509Credential; +import java.util.Arrays; + public class SigningConfigurationTests extends SamlTestCase { private static X509Credential credential; diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/service/CachingServiceAccountTokenStoreTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/service/CachingServiceAccountTokenStoreTests.java index 5eceaf7082446..c09c7a1479131 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/service/CachingServiceAccountTokenStoreTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/service/CachingServiceAccountTokenStoreTests.java @@ -194,13 +194,17 @@ TokenSource getTokenSource() { final ArrayList tokens = new ArrayList<>(); IntStream.range(0, randomIntBetween(3, 8)).forEach(i -> { - final ServiceAccountToken token = ServiceAccountToken.newToken(accountId, - randomValueOtherThanMany(n -> n.length() > 248, ValidationTests::randomTokenName)); + final ServiceAccountToken token = ServiceAccountToken.newToken( + accountId, + randomValueOtherThanMany(n -> n.length() > 248, ValidationTests::randomTokenName) + ); tokens.add(token); store.authenticate(token, mock(ActionListener.class)); - final ServiceAccountToken tokenWithSuffix = - ServiceAccountToken.newToken(accountId, token.getTokenName() + randomAlphaOfLengthBetween(3, 8)); + final ServiceAccountToken tokenWithSuffix = ServiceAccountToken.newToken( + accountId, + token.getTokenName() + randomAlphaOfLengthBetween(3, 8) + ); tokens.add(tokenWithSuffix); store.authenticate(tokenWithSuffix, mock(ActionListener.class)); }); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/service/CompositeServiceAccountTokenStoreTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/service/CompositeServiceAccountTokenStoreTests.java index 2f96e691bee4d..29f9649bf0453 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/service/CompositeServiceAccountTokenStoreTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/service/CompositeServiceAccountTokenStoreTests.java @@ -55,24 +55,25 @@ public void testAuthenticate() throws ExecutionException, InterruptedException { final TokenSource tokenSource = randomFrom(TokenSource.values()); doAnswer(invocationOnMock -> { - @SuppressWarnings("unchecked") final ActionListener listener = - (ActionListener) invocationOnMock.getArguments()[1]; + @SuppressWarnings("unchecked") + final ActionListener listener = (ActionListener) invocationOnMock + .getArguments()[1]; listener.onResponse(new StoreAuthenticationResult(store1Success, tokenSource)); return null; }).when(store1).authenticate(eq(token), any()); doAnswer(invocationOnMock -> { @SuppressWarnings("unchecked") - final ActionListener listener = - (ActionListener) invocationOnMock.getArguments()[1]; + final ActionListener listener = (ActionListener) invocationOnMock + .getArguments()[1]; listener.onResponse(new StoreAuthenticationResult(store2Success, tokenSource)); return null; }).when(store2).authenticate(eq(token), any()); doAnswer(invocationOnMock -> { @SuppressWarnings("unchecked") - final ActionListener listener = - (ActionListener) invocationOnMock.getArguments()[1]; + final ActionListener listener = (ActionListener) invocationOnMock + .getArguments()[1]; listener.onResponse(new StoreAuthenticationResult(store3Success, tokenSource)); return null; }).when(store3).authenticate(eq(token), any()); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/service/ElasticServiceAccountsTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/service/ElasticServiceAccountsTests.java index 43a87171a5a17..a63617940a97e 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/service/ElasticServiceAccountsTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/service/ElasticServiceAccountsTests.java @@ -119,49 +119,65 @@ public void testKibanaSystemPrivileges() { final RoleDescriptor serviceAccountRoleDescriptor = ElasticServiceAccounts.ACCOUNTS.get("elastic/kibana").roleDescriptor(); final RoleDescriptor reservedRolesStoreRoleDescriptor = ReservedRolesStore.kibanaSystemRoleDescriptor(KibanaSystemUser.ROLE_NAME); assertThat(serviceAccountRoleDescriptor.getClusterPrivileges(), equalTo(reservedRolesStoreRoleDescriptor.getClusterPrivileges())); - assertThat(serviceAccountRoleDescriptor.getApplicationPrivileges(), - equalTo(reservedRolesStoreRoleDescriptor.getApplicationPrivileges())); + assertThat( + serviceAccountRoleDescriptor.getApplicationPrivileges(), + equalTo(reservedRolesStoreRoleDescriptor.getApplicationPrivileges()) + ); assertThat(serviceAccountRoleDescriptor.getIndicesPrivileges(), equalTo(reservedRolesStoreRoleDescriptor.getIndicesPrivileges())); - assertThat(serviceAccountRoleDescriptor.getConditionalClusterPrivileges(), - equalTo(reservedRolesStoreRoleDescriptor.getConditionalClusterPrivileges())); + assertThat( + serviceAccountRoleDescriptor.getConditionalClusterPrivileges(), + equalTo(reservedRolesStoreRoleDescriptor.getConditionalClusterPrivileges()) + ); assertThat(serviceAccountRoleDescriptor.getRunAs(), equalTo(reservedRolesStoreRoleDescriptor.getRunAs())); assertThat(serviceAccountRoleDescriptor.getMetadata(), equalTo(reservedRolesStoreRoleDescriptor.getMetadata())); } public void testElasticFleetServerPrivileges() { final Role role = Role.builder( - ElasticServiceAccounts.ACCOUNTS.get("elastic/fleet-server").roleDescriptor(), null, RESTRICTED_INDICES_AUTOMATON).build(); + ElasticServiceAccounts.ACCOUNTS.get("elastic/fleet-server").roleDescriptor(), + null, + RESTRICTED_INDICES_AUTOMATON + ).build(); final Authentication authentication = mock(Authentication.class); - assertThat(role.cluster().check(CreateApiKeyAction.NAME, - new CreateApiKeyRequest(randomAlphaOfLengthBetween(3, 8), null, null), authentication), is(true)); + assertThat( + role.cluster() + .check(CreateApiKeyAction.NAME, new CreateApiKeyRequest(randomAlphaOfLengthBetween(3, 8), null, null), authentication), + is(true) + ); assertThat(role.cluster().check(GetApiKeyAction.NAME, GetApiKeyRequest.forOwnedApiKeys(), authentication), is(true)); assertThat(role.cluster().check(InvalidateApiKeyAction.NAME, InvalidateApiKeyRequest.forOwnedApiKeys(), authentication), is(true)); assertThat(role.cluster().check(GetApiKeyAction.NAME, randomFrom(GetApiKeyRequest.forAllApiKeys()), authentication), is(false)); - assertThat(role.cluster().check(InvalidateApiKeyAction.NAME, - InvalidateApiKeyRequest.usingUserName(randomAlphaOfLengthBetween(3, 16)), authentication), is(false)); + assertThat( + role.cluster() + .check( + InvalidateApiKeyAction.NAME, + InvalidateApiKeyRequest.usingUserName(randomAlphaOfLengthBetween(3, 16)), + authentication + ), + is(false) + ); List.of( "logs-" + randomAlphaOfLengthBetween(1, 20), "metrics-" + randomAlphaOfLengthBetween(1, 20), "traces-" + randomAlphaOfLengthBetween(1, 20), "synthetics-" + randomAlphaOfLengthBetween(1, 20), - ".logs-endpoint.diagnostic.collection-" + randomAlphaOfLengthBetween(1, 20)) - .stream().map(this::mockIndexAbstraction) - .forEach(index -> { - assertThat(role.indices().allowedIndicesMatcher(AutoPutMappingAction.NAME).test(index), is(true)); - assertThat(role.indices().allowedIndicesMatcher(AutoCreateAction.NAME).test(index), is(true)); - assertThat(role.indices().allowedIndicesMatcher(DeleteAction.NAME).test(index), is(true)); - assertThat(role.indices().allowedIndicesMatcher(CreateIndexAction.NAME).test(index), is(true)); - assertThat(role.indices().allowedIndicesMatcher(IndexAction.NAME).test(index), is(true)); - assertThat(role.indices().allowedIndicesMatcher(BulkAction.NAME).test(index), is(true)); - assertThat(role.indices().allowedIndicesMatcher(DeleteIndexAction.NAME).test(index), is(false)); - assertThat(role.indices().allowedIndicesMatcher(GetAction.NAME).test(index), is(false)); - assertThat(role.indices().allowedIndicesMatcher(MultiGetAction.NAME).test(index), is(false)); - assertThat(role.indices().allowedIndicesMatcher(SearchAction.NAME).test(index), is(false)); - assertThat(role.indices().allowedIndicesMatcher(MultiSearchAction.NAME).test(index), is(false)); - assertThat(role.indices().allowedIndicesMatcher(UpdateSettingsAction.NAME).test(index), is(false)); - }); + ".logs-endpoint.diagnostic.collection-" + randomAlphaOfLengthBetween(1, 20) + ).stream().map(this::mockIndexAbstraction).forEach(index -> { + assertThat(role.indices().allowedIndicesMatcher(AutoPutMappingAction.NAME).test(index), is(true)); + assertThat(role.indices().allowedIndicesMatcher(AutoCreateAction.NAME).test(index), is(true)); + assertThat(role.indices().allowedIndicesMatcher(DeleteAction.NAME).test(index), is(true)); + assertThat(role.indices().allowedIndicesMatcher(CreateIndexAction.NAME).test(index), is(true)); + assertThat(role.indices().allowedIndicesMatcher(IndexAction.NAME).test(index), is(true)); + assertThat(role.indices().allowedIndicesMatcher(BulkAction.NAME).test(index), is(true)); + assertThat(role.indices().allowedIndicesMatcher(DeleteIndexAction.NAME).test(index), is(false)); + assertThat(role.indices().allowedIndicesMatcher(GetAction.NAME).test(index), is(false)); + assertThat(role.indices().allowedIndicesMatcher(MultiGetAction.NAME).test(index), is(false)); + assertThat(role.indices().allowedIndicesMatcher(SearchAction.NAME).test(index), is(false)); + assertThat(role.indices().allowedIndicesMatcher(MultiSearchAction.NAME).test(index), is(false)); + assertThat(role.indices().allowedIndicesMatcher(UpdateSettingsAction.NAME).test(index), is(false)); + }); List.of( ".fleet-" + randomAlphaOfLengthBetween(1, 20), @@ -191,54 +207,80 @@ public void testElasticFleetServerPrivileges() { final String kibanaApplication = "kibana-" + randomFrom(randomAlphaOfLengthBetween(8, 24), ".kibana"); final String privilegeName = randomAlphaOfLengthBetween(3, 16); - assertThat(role.application().grants( - new ApplicationPrivilege( - kibanaApplication, privilegeName, "reserved_fleet-setup"), "*"), - is(true)); + assertThat( + role.application().grants(new ApplicationPrivilege(kibanaApplication, privilegeName, "reserved_fleet-setup"), "*"), + is(true) + ); - final String otherApplication = randomValueOtherThanMany(s -> s.startsWith("kibana"), - () -> randomAlphaOfLengthBetween(3, 8)) + "-" + randomAlphaOfLengthBetween(8, 24); - assertThat(role.application().grants( - new ApplicationPrivilege(otherApplication, privilegeName, "reserved_fleet-setup"), "*"), - is(false)); + final String otherApplication = randomValueOtherThanMany(s -> s.startsWith("kibana"), () -> randomAlphaOfLengthBetween(3, 8)) + + "-" + + randomAlphaOfLengthBetween(8, 24); + assertThat( + role.application().grants(new ApplicationPrivilege(otherApplication, privilegeName, "reserved_fleet-setup"), "*"), + is(false) + ); - assertThat(role.application().grants( - new ApplicationPrivilege(kibanaApplication, privilegeName, - randomArray(1, 5, String[]::new, () -> randomAlphaOfLengthBetween(3, 16))), "*"), - is(false)); + assertThat( + role.application() + .grants( + new ApplicationPrivilege( + kibanaApplication, + privilegeName, + randomArray(1, 5, String[]::new, () -> randomAlphaOfLengthBetween(3, 16)) + ), + "*" + ), + is(false) + ); } public void testElasticServiceAccount() { final String serviceName = randomAlphaOfLengthBetween(3, 8); final String principal = ElasticServiceAccounts.NAMESPACE + "/" + serviceName; final RoleDescriptor roleDescriptor1 = new RoleDescriptor(principal, null, null, null); - final ElasticServiceAccount serviceAccount = new ElasticServiceAccount( - serviceName, roleDescriptor1); + final ElasticServiceAccount serviceAccount = new ElasticServiceAccount(serviceName, roleDescriptor1); assertThat(serviceAccount.id(), equalTo(new ServiceAccount.ServiceAccountId(ElasticServiceAccounts.NAMESPACE, serviceName))); assertThat(serviceAccount.roleDescriptor(), equalTo(roleDescriptor1)); - assertThat(serviceAccount.asUser(), equalTo(new User(principal, Strings.EMPTY_ARRAY, - "Service account - " + principal, null, - Map.of("_elastic_service_account", true), - true))); + assertThat( + serviceAccount.asUser(), + equalTo( + new User( + principal, + Strings.EMPTY_ARRAY, + "Service account - " + principal, + null, + Map.of("_elastic_service_account", true), + true + ) + ) + ); - final NullPointerException e1 = - expectThrows(NullPointerException.class, () -> new ElasticServiceAccount(serviceName, null)); + final NullPointerException e1 = expectThrows(NullPointerException.class, () -> new ElasticServiceAccount(serviceName, null)); assertThat(e1.getMessage(), containsString("Role descriptor cannot be null")); - final RoleDescriptor roleDescriptor2 = new RoleDescriptor(randomAlphaOfLengthBetween(6, 16), - null, null, null); - final IllegalArgumentException e2 = - expectThrows(IllegalArgumentException.class, () -> new ElasticServiceAccount(serviceName, roleDescriptor2)); - assertThat(e2.getMessage(), containsString( - "the provided role descriptor [" + roleDescriptor2.getName() - + "] must have the same name as the service account [" + principal + "]")); + final RoleDescriptor roleDescriptor2 = new RoleDescriptor(randomAlphaOfLengthBetween(6, 16), null, null, null); + final IllegalArgumentException e2 = expectThrows( + IllegalArgumentException.class, + () -> new ElasticServiceAccount(serviceName, roleDescriptor2) + ); + assertThat( + e2.getMessage(), + containsString( + "the provided role descriptor [" + + roleDescriptor2.getName() + + "] must have the same name as the service account [" + + principal + + "]" + ) + ); } private IndexAbstraction mockIndexAbstraction(String name) { IndexAbstraction mock = mock(IndexAbstraction.class); when(mock.getName()).thenReturn(name); - when(mock.getType()).thenReturn(randomFrom(IndexAbstraction.Type.CONCRETE_INDEX, - IndexAbstraction.Type.ALIAS, IndexAbstraction.Type.DATA_STREAM)); + when(mock.getType()).thenReturn( + randomFrom(IndexAbstraction.Type.CONCRETE_INDEX, IndexAbstraction.Type.ALIAS, IndexAbstraction.Type.DATA_STREAM) + ); return mock; } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/service/FileServiceAccountTokenStoreTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/service/FileServiceAccountTokenStoreTests.java index 9ec740d6a9bd0..00b55e5b48337 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/service/FileServiceAccountTokenStoreTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/service/FileServiceAccountTokenStoreTests.java @@ -39,8 +39,8 @@ import java.util.Map; import java.util.Set; import java.util.concurrent.TimeUnit; -import java.util.stream.Collectors; import java.util.concurrent.atomic.AtomicInteger; +import java.util.stream.Collectors; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; @@ -61,8 +61,9 @@ public class FileServiceAccountTokenStoreTests extends ESTestCase { @Before public void init() { - final String hashingAlgorithm = inFipsJvm() ? randomFrom("pbkdf2", "pbkdf2_50000", "pbkdf2_stretch") : - randomFrom("bcrypt", "bcrypt10", "pbkdf2", "pbkdf2_50000", "pbkdf2_stretch"); + final String hashingAlgorithm = inFipsJvm() + ? randomFrom("pbkdf2", "pbkdf2_50000", "pbkdf2_stretch") + : randomFrom("bcrypt", "bcrypt10", "pbkdf2", "pbkdf2_50000", "pbkdf2_stretch"); settings = Settings.builder() .put("resource.reload.interval.high", "100ms") .put("path.home", createTempDir()) @@ -87,17 +88,27 @@ public void testParseFile() throws Exception { assertThat(parsedTokenHashes, notNullValue()); assertThat(parsedTokenHashes.size(), is(5)); - assertThat(new String(parsedTokenHashes.get("elastic/fleet-server/bcrypt")), - equalTo("$2a$10$uuCzGHRrEz/QMB/.bmL8qOKXHhPNt57dYBbWCH/Hbb3SjUyZ.Hf1i")); - assertThat(new String(parsedTokenHashes.get("elastic/fleet-server/bcrypt10")), - equalTo("$2a$10$ML0BUUxdzs8ApPNf1ayAwuh61ZhfqlzN/1DgZWZn6vNiUhpu1GKTe")); + assertThat( + new String(parsedTokenHashes.get("elastic/fleet-server/bcrypt")), + equalTo("$2a$10$uuCzGHRrEz/QMB/.bmL8qOKXHhPNt57dYBbWCH/Hbb3SjUyZ.Hf1i") + ); + assertThat( + new String(parsedTokenHashes.get("elastic/fleet-server/bcrypt10")), + equalTo("$2a$10$ML0BUUxdzs8ApPNf1ayAwuh61ZhfqlzN/1DgZWZn6vNiUhpu1GKTe") + ); - assertThat(new String(parsedTokenHashes.get("elastic/fleet-server/pbkdf2")), - equalTo("{PBKDF2}10000$0N2h5/AsDS5uO0/A+B6y8AnTCJ3Tqo8nygbzu1gkgpo=$5aTcCtteHf2g2ye7Y3p6jSZBoGhNJ7l6F3tmUhPTwRo=")); - assertThat(new String(parsedTokenHashes.get("elastic/fleet-server/pbkdf2_50000")), - equalTo("{PBKDF2}50000$IMzlphNClmrP/du40yxGM3fNjklg8CuACds12+Ry0jM=$KEC1S9a0NOs3OJKM4gEeBboU18EP4+3m/pyIA4MBDGk=")); - assertThat(new String(parsedTokenHashes.get("elastic/fleet-server/pbkdf2_stretch")), - equalTo("{PBKDF2_STRETCH}10000$Pa3oNkj8xTD8j2gTgjWnTvnE6jseKApWMFjcNCLxX1U=$84ECweHFZQ2DblHEjHTRWA+fG6h5bVMyTSJUmFvTo1o=")); + assertThat( + new String(parsedTokenHashes.get("elastic/fleet-server/pbkdf2")), + equalTo("{PBKDF2}10000$0N2h5/AsDS5uO0/A+B6y8AnTCJ3Tqo8nygbzu1gkgpo=$5aTcCtteHf2g2ye7Y3p6jSZBoGhNJ7l6F3tmUhPTwRo=") + ); + assertThat( + new String(parsedTokenHashes.get("elastic/fleet-server/pbkdf2_50000")), + equalTo("{PBKDF2}50000$IMzlphNClmrP/du40yxGM3fNjklg8CuACds12+Ry0jM=$KEC1S9a0NOs3OJKM4gEeBboU18EP4+3m/pyIA4MBDGk=") + ); + assertThat( + new String(parsedTokenHashes.get("elastic/fleet-server/pbkdf2_stretch")), + equalTo("{PBKDF2_STRETCH}10000$Pa3oNkj8xTD8j2gTgjWnTvnE6jseKApWMFjcNCLxX1U=$84ECweHFZQ2DblHEjHTRWA+fG6h5bVMyTSJUmFvTo1o=") + ); assertThat(parsedTokenHashes.get("elastic/fleet-server/plain"), nullValue()); } @@ -106,8 +117,10 @@ public void testParseFileNotExists() throws IllegalAccessException, IOException Logger logger = CapturingLogger.newCapturingLogger(Level.TRACE, null); final List events = CapturingLogger.output(logger.getName(), Level.TRACE); events.clear(); - final Map tokenHashes = - FileServiceAccountTokenStore.parseFile(getDataPath("service_tokens").getParent().resolve("does-not-exist"), logger); + final Map tokenHashes = FileServiceAccountTokenStore.parseFile( + getDataPath("service_tokens").getParent().resolve("does-not-exist"), + logger + ); assertThat(tokenHashes.isEmpty(), is(true)); assertThat(events, hasSize(2)); assertThat(events.get(1), containsString("does not exist")); @@ -124,10 +137,15 @@ public void testAutoReload() throws Exception { try (ResourceWatcherService watcherService = new ResourceWatcherService(settings, threadPool)) { final AtomicInteger counter = new AtomicInteger(0); - FileServiceAccountTokenStore store = new FileServiceAccountTokenStore(env, watcherService, threadPool, - clusterService, mock(CacheInvalidatorRegistry.class)); + FileServiceAccountTokenStore store = new FileServiceAccountTokenStore( + env, + watcherService, + threadPool, + clusterService, + mock(CacheInvalidatorRegistry.class) + ); store.addListener(counter::getAndIncrement); - //Token name shares the hashing algorithm name for convenience + // Token name shares the hashing algorithm name for convenience final String qualifiedTokenName = "elastic/fleet-server/" + hashingAlgo; assertThat(store.getTokenHashes().containsKey(qualifiedTokenName), is(true)); @@ -144,8 +162,9 @@ public void testAutoReload() throws Exception { // Add a new entry final int oldValue2 = counter.get(); - final char[] newTokenHash = - hasher.hash(new SecureString("46ToAwIHZWxhc3RpYwVmbGVldAZ0b2tlbjEWWkYtQ3dlWlVTZldJX3p5Vk9ySnlSQQAAAAAAAAA".toCharArray())); + final char[] newTokenHash = hasher.hash( + new SecureString("46ToAwIHZWxhc3RpYwVmbGVldAZ0b2tlbjEWWkYtQ3dlWlVTZldJX3p5Vk9ySnlSQQAAAAAAAAA".toCharArray()) + ); try (BufferedWriter writer = Files.newBufferedWriter(targetFile, StandardCharsets.UTF_8, StandardOpenOption.APPEND)) { writer.newLine(); writer.append("elastic/fleet-server/token1:").append(new String(newTokenHash)); @@ -210,17 +229,28 @@ public void testFindTokensFor() throws IOException { Files.createDirectories(configDir); Path targetFile = configDir.resolve("service_tokens"); Files.copy(serviceTokensSourceFile, targetFile, StandardCopyOption.REPLACE_EXISTING); - FileServiceAccountTokenStore store = new FileServiceAccountTokenStore(env, mock(ResourceWatcherService.class), threadPool, - clusterService, mock(CacheInvalidatorRegistry.class)); + FileServiceAccountTokenStore store = new FileServiceAccountTokenStore( + env, + mock(ResourceWatcherService.class), + threadPool, + clusterService, + mock(CacheInvalidatorRegistry.class) + ); final ServiceAccountId accountId = new ServiceAccountId("elastic", "fleet-server"); final List tokenInfos = store.findTokensFor(accountId); assertThat(tokenInfos, hasSize(5)); - assertThat(tokenInfos.stream().map(TokenInfo::getName).collect(Collectors.toUnmodifiableSet()), - equalTo(Set.of("pbkdf2", "bcrypt10", "pbkdf2_stretch", "pbkdf2_50000", "bcrypt"))); - assertThat(tokenInfos.stream().map(TokenInfo::getSource).collect(Collectors.toUnmodifiableSet()), - equalTo(EnumSet.of(TokenInfo.TokenSource.FILE))); - assertThat(tokenInfos.stream().map(TokenInfo::getNodeNames).collect(Collectors.toUnmodifiableSet()), - equalTo(Set.of(List.of("node")))); + assertThat( + tokenInfos.stream().map(TokenInfo::getName).collect(Collectors.toUnmodifiableSet()), + equalTo(Set.of("pbkdf2", "bcrypt10", "pbkdf2_stretch", "pbkdf2_50000", "bcrypt")) + ); + assertThat( + tokenInfos.stream().map(TokenInfo::getSource).collect(Collectors.toUnmodifiableSet()), + equalTo(EnumSet.of(TokenInfo.TokenSource.FILE)) + ); + assertThat( + tokenInfos.stream().map(TokenInfo::getNodeNames).collect(Collectors.toUnmodifiableSet()), + equalTo(Set.of(List.of("node"))) + ); } } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/service/IndexServiceAccountTokenStoreTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/service/IndexServiceAccountTokenStoreTests.java index 4fc20bf186afb..5d58fa61f34db 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/service/IndexServiceAccountTokenStoreTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/service/IndexServiceAccountTokenStoreTests.java @@ -35,10 +35,9 @@ import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.node.DiscoveryNodes; import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.core.CharArrays; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.core.CharArrays; import org.elasticsearch.index.get.GetResult; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.search.SearchHit; @@ -47,6 +46,7 @@ import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.XContentTestUtils; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.security.action.ClearSecurityCacheRequest; import org.elasticsearch.xpack.core.security.action.ClearSecurityCacheResponse; import org.elasticsearch.xpack.core.security.action.service.CreateServiceAccountTokenRequest; @@ -114,8 +114,11 @@ public void init() { client = new FilterClient(mockClient) { @Override @SuppressWarnings("unchecked") - protected - void doExecute(ActionType action, Request request, ActionListener listener) { + protected void doExecute( + ActionType action, + Request request, + ActionListener listener + ) { requestHolder.set(request); responseProviderHolder.get().accept(request, (ActionListener) listener); } @@ -143,12 +146,15 @@ void doExecute(ActionType action, Request request, ActionListener l.onResponse(createSingleBulkResponse())); @@ -230,13 +239,19 @@ public void testCreateTokenWillFailForInvalidServiceAccount() { final Authentication authentication = createAuthentication(); final CreateServiceAccountTokenRequest request = randomValueOtherThanMany( r -> "elastic".equals(r.getNamespace()) && "fleet-server".equals(r.getServiceName()), - () -> new CreateServiceAccountTokenRequest(randomAlphaOfLengthBetween(3, 8), - randomAlphaOfLengthBetween(3, 8), randomAlphaOfLengthBetween(3, 8))); + () -> new CreateServiceAccountTokenRequest( + randomAlphaOfLengthBetween(3, 8), + randomAlphaOfLengthBetween(3, 8), + randomAlphaOfLengthBetween(3, 8) + ) + ); final PlainActionFuture future = new PlainActionFuture<>(); store.createToken(authentication, request, future); final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, future::actionGet); - assertThat(e.getMessage(), - containsString("service account [" + request.getNamespace() + "/" + request.getServiceName() + "] does not exist")); + assertThat( + e.getMessage(), + containsString("service account [" + request.getNamespace() + "/" + request.getServiceName() + "] does not exist") + ); } public void testFindTokensFor() { @@ -247,19 +262,36 @@ public void testFindTokensFor() { responseProviderHolder.set((r, l) -> { if (r instanceof SearchRequest) { final SearchHit[] hits = IntStream.range(0, nhits) - .mapToObj(i -> - new SearchHit(randomIntBetween(0, Integer.MAX_VALUE), - SERVICE_ACCOUNT_TOKEN_DOC_TYPE + "-" + accountId.asPrincipal() + "/" + tokenNames[i], Map.of(), Map.of())) + .mapToObj( + i -> new SearchHit( + randomIntBetween(0, Integer.MAX_VALUE), + SERVICE_ACCOUNT_TOKEN_DOC_TYPE + "-" + accountId.asPrincipal() + "/" + tokenNames[i], + Map.of(), + Map.of() + ) + ) .toArray(SearchHit[]::new); final InternalSearchResponse internalSearchResponse; - internalSearchResponse = new InternalSearchResponse(new SearchHits(hits, - new TotalHits(nhits, TotalHits.Relation.EQUAL_TO), - randomFloat(), null, null, null), - null, null, null, false, null, 0); - - final SearchResponse searchResponse = - new SearchResponse(internalSearchResponse, randomAlphaOfLengthBetween(3, 8), - 1, 1, 0, 10, null, null); + internalSearchResponse = new InternalSearchResponse( + new SearchHits(hits, new TotalHits(nhits, TotalHits.Relation.EQUAL_TO), randomFloat(), null, null, null), + null, + null, + null, + false, + null, + 0 + ); + + final SearchResponse searchResponse = new SearchResponse( + internalSearchResponse, + randomAlphaOfLengthBetween(3, 8), + 1, + 1, + 0, + 10, + null, + null + ); l.onResponse(searchResponse); } else if (r instanceof ClearScrollRequest) { l.onResponse(new ClearScrollResponse(true, 1)); @@ -272,16 +304,13 @@ public void testFindTokensFor() { store.findTokensFor(accountId, future); final Collection tokenInfos = future.actionGet(); assertThat(tokenInfos.stream().map(TokenInfo::getSource).allMatch(TokenSource.INDEX::equals), is(true)); - assertThat(tokenInfos.stream().map(TokenInfo::getName).collect(Collectors.toUnmodifiableSet()), - equalTo(Set.of(tokenNames))); + assertThat(tokenInfos.stream().map(TokenInfo::getName).collect(Collectors.toUnmodifiableSet()), equalTo(Set.of(tokenNames))); } public void testFindTokensForException() { final ServiceAccountId accountId = new ServiceAccountId(randomAlphaOfLengthBetween(3, 8), randomAlphaOfLengthBetween(3, 8)); final RuntimeException e = new RuntimeException("fail"); - responseProviderHolder.set((r, l) -> { - l.onFailure(e); - }); + responseProviderHolder.set((r, l) -> { l.onFailure(e); }); final PlainActionFuture> future = new PlainActionFuture<>(); store.findTokensFor(accountId, future); @@ -295,19 +324,31 @@ public void testDeleteToken() { if (r instanceof DeleteRequest) { final DeleteRequest dr = (DeleteRequest) r; final boolean found = dr.id().equals(SERVICE_ACCOUNT_TOKEN_DOC_TYPE + "-elastic/fleet-server/token1"); - l.onResponse(new DeleteResponse(mock(ShardId.class), randomAlphaOfLengthBetween(3, 8), - randomLong(), randomLong(), randomLong(), found)); + l.onResponse( + new DeleteResponse( + mock(ShardId.class), + randomAlphaOfLengthBetween(3, 8), + randomLong(), + randomLong(), + randomLong(), + found + ) + ); } else if (r instanceof ClearSecurityCacheRequest) { cacheCleared.set(true); - l.onResponse(new ClearSecurityCacheResponse(mock(ClusterName.class), - List.of(mock(ClearSecurityCacheResponse.Node.class)), List.of())); + l.onResponse( + new ClearSecurityCacheResponse(mock(ClusterName.class), List.of(mock(ClearSecurityCacheResponse.Node.class)), List.of()) + ); } else { fail("unexpected request " + r); } }); final DeleteServiceAccountTokenRequest deleteServiceAccountTokenRequest1 = new DeleteServiceAccountTokenRequest( - "elastic", "fleet-server", "token1"); + "elastic", + "fleet-server", + "token1" + ); final PlainActionFuture future1 = new PlainActionFuture<>(); store.deleteToken(deleteServiceAccountTokenRequest1, future1); assertThat(future1.actionGet(), is(true)); @@ -315,14 +356,20 @@ public void testDeleteToken() { // non-exist token name final DeleteServiceAccountTokenRequest deleteServiceAccountTokenRequest2 = new DeleteServiceAccountTokenRequest( - "elastic", "fleet-server", randomAlphaOfLengthBetween(3, 8)); + "elastic", + "fleet-server", + randomAlphaOfLengthBetween(3, 8) + ); final PlainActionFuture future2 = new PlainActionFuture<>(); store.deleteToken(deleteServiceAccountTokenRequest2, future2); assertThat(future2.actionGet(), is(false)); // Invalid service account final DeleteServiceAccountTokenRequest deleteServiceAccountTokenRequest3 = new DeleteServiceAccountTokenRequest( - randomValueOtherThan("elastic", () -> randomAlphaOfLengthBetween(3, 8)), "fleet-server", "token1"); + randomValueOtherThan("elastic", () -> randomAlphaOfLengthBetween(3, 8)), + "fleet-server", + "token1" + ); final PlainActionFuture future3 = new PlainActionFuture<>(); store.deleteToken(deleteServiceAccountTokenRequest3, future3); assertThat(future3.actionGet(), is(false)); @@ -340,7 +387,10 @@ public void testIndexStateIssues() { assertThat(future1.actionGet(), equalTo(List.of())); final DeleteServiceAccountTokenRequest deleteServiceAccountTokenRequest = new DeleteServiceAccountTokenRequest( - randomAlphaOfLengthBetween(3, 8), randomAlphaOfLengthBetween(3, 8), randomAlphaOfLengthBetween(3, 8)); + randomAlphaOfLengthBetween(3, 8), + randomAlphaOfLengthBetween(3, 8), + randomAlphaOfLengthBetween(3, 8) + ); final PlainActionFuture future2 = new PlainActionFuture<>(); store.deleteToken(deleteServiceAccountTokenRequest, future2); assertThat(future2.actionGet(), is(false)); @@ -367,30 +417,50 @@ public void testIndexStateIssues() { private GetResponse createGetResponse(ServiceAccountToken serviceAccountToken, boolean exists) throws IOException { final char[] hash = Hasher.PBKDF2_STRETCH.hash(serviceAccountToken.getSecret()); final Map documentMap = Map.of("password", new String(CharArrays.toUtf8Bytes(hash), StandardCharsets.UTF_8)); - return new GetResponse(new GetResult( - randomAlphaOfLengthBetween(3, 8), randomAlphaOfLengthBetween(3, 8), - exists ? randomLongBetween(0, Long.MAX_VALUE) : UNASSIGNED_SEQ_NO, - exists ? randomLongBetween(1, Long.MAX_VALUE) : UNASSIGNED_PRIMARY_TERM, randomLong(), exists, - XContentTestUtils.convertToXContent(documentMap, XContentType.JSON), - Map.of(), Map.of())); + return new GetResponse( + new GetResult( + randomAlphaOfLengthBetween(3, 8), + randomAlphaOfLengthBetween(3, 8), + exists ? randomLongBetween(0, Long.MAX_VALUE) : UNASSIGNED_SEQ_NO, + exists ? randomLongBetween(1, Long.MAX_VALUE) : UNASSIGNED_PRIMARY_TERM, + randomLong(), + exists, + XContentTestUtils.convertToXContent(documentMap, XContentType.JSON), + Map.of(), + Map.of() + ) + ); } private Authentication createAuthentication() { - return new Authentication(new User(randomAlphaOfLengthBetween(3, 8)), - new Authentication.RealmRef(randomAlphaOfLengthBetween(3, 8), + return new Authentication( + new User(randomAlphaOfLengthBetween(3, 8)), + new Authentication.RealmRef( randomAlphaOfLengthBetween(3, 8), - randomAlphaOfLengthBetween(3, 8)), - randomFrom(new Authentication.RealmRef(randomAlphaOfLengthBetween(3, 8), randomAlphaOfLengthBetween(3, 8), - randomAlphaOfLengthBetween(3, 8)), null)); + randomAlphaOfLengthBetween(3, 8) + ), + randomFrom( + new Authentication.RealmRef( + randomAlphaOfLengthBetween(3, 8), + randomAlphaOfLengthBetween(3, 8), + randomAlphaOfLengthBetween(3, 8) + ), + null + ) + ); } private BulkResponse createSingleBulkResponse() { - return new BulkResponse(new BulkItemResponse[] { - BulkItemResponse.success(randomInt(), OpType.CREATE, new IndexResponse( - mock(ShardId.class), randomAlphaOfLengthBetween(3, 8), randomLong(), randomLong(), randomLong(), true - )) - }, randomLong()); + return new BulkResponse( + new BulkItemResponse[] { + BulkItemResponse.success( + randomInt(), + OpType.CREATE, + new IndexResponse(mock(ShardId.class), randomAlphaOfLengthBetween(3, 8), randomLong(), randomLong(), randomLong(), true) + ) }, + randomLong() + ); } @SuppressWarnings("unchecked") diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/service/ServiceAccountIdTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/service/ServiceAccountIdTests.java index 2665a0b0d1e7f..ed6ac0f6de435 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/service/ServiceAccountIdTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/service/ServiceAccountIdTests.java @@ -35,39 +35,55 @@ public void testFromPrincipalAndInstantiate() { // No '/' final String principal2 = randomAlphaOfLengthBetween(6, 16); - final IllegalArgumentException e2 = - expectThrows(IllegalArgumentException.class, () -> ServiceAccount.ServiceAccountId.fromPrincipal(principal2)); - assertThat(e2.getMessage(), containsString( - "a service account ID must be in the form {namespace}/{service-name}, but was [" + principal2 + "]")); + final IllegalArgumentException e2 = expectThrows( + IllegalArgumentException.class, + () -> ServiceAccount.ServiceAccountId.fromPrincipal(principal2) + ); + assertThat( + e2.getMessage(), + containsString("a service account ID must be in the form {namespace}/{service-name}, but was [" + principal2 + "]") + ); // blank namespace final IllegalArgumentException e3; if (randomBoolean()) { - e3 = expectThrows(IllegalArgumentException.class, + e3 = expectThrows( + IllegalArgumentException.class, () -> ServiceAccount.ServiceAccountId.fromPrincipal( - randomFrom("", " ", "\t", " \t") + "/" + randomAlphaOfLengthBetween(3, 8))); + randomFrom("", " ", "\t", " \t") + "/" + randomAlphaOfLengthBetween(3, 8) + ) + ); } else { - e3 = expectThrows(IllegalArgumentException.class, - () -> new ServiceAccount.ServiceAccountId(randomFrom("", " ", "\t", " \t", null), randomAlphaOfLengthBetween(3, 8))); + e3 = expectThrows( + IllegalArgumentException.class, + () -> new ServiceAccount.ServiceAccountId(randomFrom("", " ", "\t", " \t", null), randomAlphaOfLengthBetween(3, 8)) + ); } assertThat(e3.getMessage(), containsString("the namespace of a service account ID must not be empty")); // blank service-name final IllegalArgumentException e4; if (randomBoolean()) { - e4 = expectThrows(IllegalArgumentException.class, + e4 = expectThrows( + IllegalArgumentException.class, () -> ServiceAccount.ServiceAccountId.fromPrincipal( - randomAlphaOfLengthBetween(3, 8) + "/" + randomFrom("", " ", "\t", " \t"))); + randomAlphaOfLengthBetween(3, 8) + "/" + randomFrom("", " ", "\t", " \t") + ) + ); } else { - e4 = expectThrows(IllegalArgumentException.class, - () -> new ServiceAccount.ServiceAccountId(randomAlphaOfLengthBetween(3, 8), randomFrom("", " ", "\t", " \t", null))); + e4 = expectThrows( + IllegalArgumentException.class, + () -> new ServiceAccount.ServiceAccountId(randomAlphaOfLengthBetween(3, 8), randomFrom("", " ", "\t", " \t", null)) + ); } assertThat(e4.getMessage(), containsString("the service-name of a service account ID must not be empty")); } public void testStreamReadWrite() throws IOException { - final ServiceAccount.ServiceAccountId accountId = - new ServiceAccount.ServiceAccountId(randomAlphaOfLengthBetween(3, 8), randomAlphaOfLengthBetween(3, 8)); + final ServiceAccount.ServiceAccountId accountId = new ServiceAccount.ServiceAccountId( + randomAlphaOfLengthBetween(3, 8), + randomAlphaOfLengthBetween(3, 8) + ); try (BytesStreamOutput out = new BytesStreamOutput()) { accountId.write(out); try (StreamInput in = out.bytes().streamInput()) { diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/service/ServiceAccountServiceTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/service/ServiceAccountServiceTests.java index 874f579eaa881..a4fb66bdc7d3e 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/service/ServiceAccountServiceTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/service/ServiceAccountServiceTests.java @@ -84,8 +84,7 @@ public void init() throws UnknownHostException { indexServiceAccountTokenStore = mock(IndexServiceAccountTokenStore.class); when(fileServiceAccountTokenStore.getTokenSource()).thenReturn(TokenInfo.TokenSource.FILE); when(indexServiceAccountTokenStore.getTokenSource()).thenReturn(TokenInfo.TokenSource.INDEX); - final Settings.Builder builder = Settings.builder() - .put("xpack.security.enabled", true); + final Settings.Builder builder = Settings.builder().put("xpack.security.enabled", true); client = mock(Client.class); when(client.threadPool()).thenReturn(threadPool); serviceAccountService = new ServiceAccountService(client, fileServiceAccountTokenStore, indexServiceAccountTokenStore); @@ -97,8 +96,7 @@ public void stopThreadPool() { } public void testGetServiceAccountPrincipals() { - assertThat(ServiceAccountService.getServiceAccountPrincipals(), - containsInAnyOrder("elastic/fleet-server", "elastic/kibana")); + assertThat(ServiceAccountService.getServiceAccountPrincipals(), containsInAnyOrder("elastic/fleet-server", "elastic/kibana")); } public void testTryParseToken() throws IOException, IllegalAccessException { @@ -119,68 +117,104 @@ public void testTryParseToken() throws IOException, IllegalAccessException { try { // Less than 4 bytes - appender.addExpectation(new MockLogAppender.SeenEventExpectation( - "less than 4 bytes", ServiceAccountToken.class.getName(), Level.TRACE, - "service account token expects the 4 leading bytes") + appender.addExpectation( + new MockLogAppender.SeenEventExpectation( + "less than 4 bytes", + ServiceAccountToken.class.getName(), + Level.TRACE, + "service account token expects the 4 leading bytes" + ) ); final SecureString bearerString0 = createBearerString(List.of(Arrays.copyOfRange(magicBytes, 0, randomIntBetween(0, 3)))); assertNull(ServiceAccountService.tryParseToken(bearerString0)); appender.assertAllExpectationsMatched(); // Prefix mismatch - appender.addExpectation(new MockLogAppender.SeenEventExpectation( - "prefix mismatch", ServiceAccountToken.class.getName(), Level.TRACE, - "service account token expects the 4 leading bytes" - )); - final SecureString bearerString1 = createBearerString(List.of( - new byte[] { randomValueOtherThan((byte) 0, ESTestCase::randomByte) }, - randomByteArrayOfLength(randomIntBetween(30, 50)))); + appender.addExpectation( + new MockLogAppender.SeenEventExpectation( + "prefix mismatch", + ServiceAccountToken.class.getName(), + Level.TRACE, + "service account token expects the 4 leading bytes" + ) + ); + final SecureString bearerString1 = createBearerString( + List.of( + new byte[] { randomValueOtherThan((byte) 0, ESTestCase::randomByte) }, + randomByteArrayOfLength(randomIntBetween(30, 50)) + ) + ); assertNull(ServiceAccountService.tryParseToken(bearerString1)); appender.assertAllExpectationsMatched(); // No colon - appender.addExpectation(new MockLogAppender.SeenEventExpectation( - "no colon", ServiceAccountToken.class.getName(), Level.TRACE, - "failed to extract qualified service token name and secret, missing ':'" - )); - final SecureString bearerString2 = createBearerString(List.of( - magicBytes, - randomAlphaOfLengthBetween(30, 50).getBytes(StandardCharsets.UTF_8))); + appender.addExpectation( + new MockLogAppender.SeenEventExpectation( + "no colon", + ServiceAccountToken.class.getName(), + Level.TRACE, + "failed to extract qualified service token name and secret, missing ':'" + ) + ); + final SecureString bearerString2 = createBearerString( + List.of(magicBytes, randomAlphaOfLengthBetween(30, 50).getBytes(StandardCharsets.UTF_8)) + ); assertNull(ServiceAccountService.tryParseToken(bearerString2)); appender.assertAllExpectationsMatched(); // Invalid delimiter for qualified name - appender.addExpectation(new MockLogAppender.SeenEventExpectation( - "invalid delimiter for qualified name", ServiceAccountToken.class.getName(), Level.TRACE, - "The qualified name of a service token should take format of 'namespace/service_name/token_name'" - )); + appender.addExpectation( + new MockLogAppender.SeenEventExpectation( + "invalid delimiter for qualified name", + ServiceAccountToken.class.getName(), + Level.TRACE, + "The qualified name of a service token should take format of 'namespace/service_name/token_name'" + ) + ); if (randomBoolean()) { - final SecureString bearerString3 = createBearerString(List.of( - magicBytes, - (randomAlphaOfLengthBetween(10, 20) + ":" + randomAlphaOfLengthBetween(10, 20)).getBytes(StandardCharsets.UTF_8) - )); + final SecureString bearerString3 = createBearerString( + List.of( + magicBytes, + (randomAlphaOfLengthBetween(10, 20) + ":" + randomAlphaOfLengthBetween(10, 20)).getBytes(StandardCharsets.UTF_8) + ) + ); assertNull(ServiceAccountService.tryParseToken(bearerString3)); } else { - final SecureString bearerString3 = createBearerString(List.of( - magicBytes, - (randomAlphaOfLengthBetween(3, 8) + "/" + randomAlphaOfLengthBetween(3, 8) - + ":" + randomAlphaOfLengthBetween(10, 20)).getBytes(StandardCharsets.UTF_8) - )); + final SecureString bearerString3 = createBearerString( + List.of( + magicBytes, + (randomAlphaOfLengthBetween(3, 8) + + "/" + + randomAlphaOfLengthBetween(3, 8) + + ":" + + randomAlphaOfLengthBetween(10, 20)).getBytes(StandardCharsets.UTF_8) + ) + ); assertNull(ServiceAccountService.tryParseToken(bearerString3)); } appender.assertAllExpectationsMatched(); // Invalid token name - appender.addExpectation(new MockLogAppender.SeenEventExpectation( - "invalid token name", ServiceAccountService.class.getName(), Level.TRACE, - "Cannot parse possible service account token" - )); - final SecureString bearerString4 = createBearerString(List.of( - magicBytes, - (randomAlphaOfLengthBetween(3, 8) + "/" + randomAlphaOfLengthBetween(3, 8) - + "/" + randomValueOtherThanMany(n -> n.contains("/"), ValidationTests::randomInvalidTokenName) - + ":" + randomAlphaOfLengthBetween(10, 20)).getBytes(StandardCharsets.UTF_8) - )); + appender.addExpectation( + new MockLogAppender.SeenEventExpectation( + "invalid token name", + ServiceAccountService.class.getName(), + Level.TRACE, + "Cannot parse possible service account token" + ) + ); + final SecureString bearerString4 = createBearerString( + List.of( + magicBytes, + (randomAlphaOfLengthBetween(3, 8) + + "/" + + randomAlphaOfLengthBetween(3, 8) + + "/" + + randomValueOtherThanMany(n -> n.contains("/"), ValidationTests::randomInvalidTokenName) + + ":" + + randomAlphaOfLengthBetween(10, 20)).getBytes(StandardCharsets.UTF_8) + ) + ); assertNull(ServiceAccountService.tryParseToken(bearerString4)); appender.assertAllExpectationsMatched(); @@ -190,13 +224,15 @@ public void testTryParseToken() throws IOException, IllegalAccessException { final String tokenName = ValidationTests.randomTokenName(); final ServiceAccountId accountId = new ServiceAccountId(namespace, serviceName); final String secret = randomAlphaOfLengthBetween(10, 20); - final SecureString bearerString5 = createBearerString(List.of( - magicBytes, - (namespace + "/" + serviceName + "/" + tokenName + ":" + secret).getBytes(StandardCharsets.UTF_8) - )); + final SecureString bearerString5 = createBearerString( + List.of(magicBytes, (namespace + "/" + serviceName + "/" + tokenName + ":" + secret).getBytes(StandardCharsets.UTF_8)) + ); final ServiceAccountToken serviceAccountToken1 = ServiceAccountService.tryParseToken(bearerString5); - final ServiceAccountToken serviceAccountToken2 = new ServiceAccountToken(accountId, tokenName, - new SecureString(secret.toCharArray())); + final ServiceAccountToken serviceAccountToken2 = new ServiceAccountToken( + accountId, + tokenName, + new SecureString(secret.toCharArray()) + ); assertThat(serviceAccountToken1, equalTo(serviceAccountToken2)); // Serialise and de-serialise service account token @@ -204,46 +240,74 @@ public void testTryParseToken() throws IOException, IllegalAccessException { assertThat(parsedToken, equalTo(serviceAccountToken2)); // Invalid magic byte - appender.addExpectation(new MockLogAppender.SeenEventExpectation( - "invalid magic byte again", ServiceAccountToken.class.getName(), Level.TRACE, - "service account token expects the 4 leading bytes" - )); - assertNull(ServiceAccountService.tryParseToken( - new SecureString("AQEAAWVsYXN0aWMvZmxlZXQvdG9rZW4xOnN1cGVyc2VjcmV0".toCharArray()))); + appender.addExpectation( + new MockLogAppender.SeenEventExpectation( + "invalid magic byte again", + ServiceAccountToken.class.getName(), + Level.TRACE, + "service account token expects the 4 leading bytes" + ) + ); + assertNull( + ServiceAccountService.tryParseToken(new SecureString("AQEAAWVsYXN0aWMvZmxlZXQvdG9rZW4xOnN1cGVyc2VjcmV0".toCharArray())) + ); appender.assertAllExpectationsMatched(); // No colon - appender.addExpectation(new MockLogAppender.SeenEventExpectation( - "no colon again", ServiceAccountToken.class.getName(), Level.TRACE, - "failed to extract qualified service token name and secret, missing ':'" - )); - assertNull(ServiceAccountService.tryParseToken( - new SecureString("AAEAAWVsYXN0aWMvZmxlZXQvdG9rZW4xX3N1cGVyc2VjcmV0".toCharArray()))); + appender.addExpectation( + new MockLogAppender.SeenEventExpectation( + "no colon again", + ServiceAccountToken.class.getName(), + Level.TRACE, + "failed to extract qualified service token name and secret, missing ':'" + ) + ); + assertNull( + ServiceAccountService.tryParseToken(new SecureString("AAEAAWVsYXN0aWMvZmxlZXQvdG9rZW4xX3N1cGVyc2VjcmV0".toCharArray())) + ); appender.assertAllExpectationsMatched(); // Invalid qualified name - appender.addExpectation(new MockLogAppender.SeenEventExpectation( - "invalid delimiter for qualified name again", ServiceAccountToken.class.getName(), Level.TRACE, - "The qualified name of a service token should take format of 'namespace/service_name/token_name'" - )); - assertNull(ServiceAccountService.tryParseToken( - new SecureString("AAEAAWVsYXN0aWMvZmxlZXRfdG9rZW4xOnN1cGVyc2VjcmV0".toCharArray()))); + appender.addExpectation( + new MockLogAppender.SeenEventExpectation( + "invalid delimiter for qualified name again", + ServiceAccountToken.class.getName(), + Level.TRACE, + "The qualified name of a service token should take format of 'namespace/service_name/token_name'" + ) + ); + assertNull( + ServiceAccountService.tryParseToken(new SecureString("AAEAAWVsYXN0aWMvZmxlZXRfdG9rZW4xOnN1cGVyc2VjcmV0".toCharArray())) + ); appender.assertAllExpectationsMatched(); // Invalid token name - appender.addExpectation(new MockLogAppender.SeenEventExpectation( - "invalid token name again", ServiceAccountService.class.getName(), Level.TRACE, - "Cannot parse possible service account token" - )); - assertNull(ServiceAccountService.tryParseToken( - new SecureString("AAEAAWVsYXN0aWMvZmxlZXQvdG9rZW4hOnN1cGVyc2VjcmV0".toCharArray()))); + appender.addExpectation( + new MockLogAppender.SeenEventExpectation( + "invalid token name again", + ServiceAccountService.class.getName(), + Level.TRACE, + "Cannot parse possible service account token" + ) + ); + assertNull( + ServiceAccountService.tryParseToken(new SecureString("AAEAAWVsYXN0aWMvZmxlZXQvdG9rZW4hOnN1cGVyc2VjcmV0".toCharArray())) + ); appender.assertAllExpectationsMatched(); // everything is fine - assertThat(ServiceAccountService.tryParseToken( - new SecureString("AAEAAWVsYXN0aWMvZmxlZXQtc2VydmVyL3Rva2VuMTpzdXBlcnNlY3JldA".toCharArray())), - equalTo(new ServiceAccountToken(new ServiceAccountId("elastic", "fleet-server"), "token1", - new SecureString("supersecret".toCharArray())))); + assertThat( + ServiceAccountService.tryParseToken( + new SecureString("AAEAAWVsYXN0aWMvZmxlZXQtc2VydmVyL3Rva2VuMTpzdXBlcnNlY3JldA".toCharArray()) + ), + equalTo( + new ServiceAccountToken( + new ServiceAccountId("elastic", "fleet-server"), + "token1", + new SecureString("supersecret".toCharArray()) + ) + ) + ); } finally { appender.stop(); Loggers.setLevel(satLogger, Level.INFO); @@ -261,29 +325,45 @@ public void testTryAuthenticateBearerToken() throws ExecutionException, Interrup Stream.of(fileServiceAccountTokenStore, indexServiceAccountTokenStore).forEach(store -> { doAnswer(invocationOnMock -> { @SuppressWarnings("unchecked") - final ActionListener listener = - (ActionListener) invocationOnMock.getArguments()[1]; + final ActionListener listener = (ActionListener< + ServiceAccountTokenStore.StoreAuthenticationResult>) invocationOnMock.getArguments()[1]; listener.onResponse( - new ServiceAccountTokenStore.StoreAuthenticationResult(store == authenticatingStore, store.getTokenSource())); + new ServiceAccountTokenStore.StoreAuthenticationResult(store == authenticatingStore, store.getTokenSource()) + ); return null; }).when(store).authenticate(any(), any()); }); final String nodeName = randomAlphaOfLengthBetween(3, 8); serviceAccountService.authenticateToken( - new ServiceAccountToken(new ServiceAccountId("elastic", "fleet-server"), "token1", - new SecureString("super-secret-value".toCharArray())), - nodeName, future5); - assertThat(future5.get(), equalTo( - new Authentication( - new User("elastic/fleet-server", Strings.EMPTY_ARRAY, "Service account - elastic/fleet-server", null, - Map.of("_elastic_service_account", true), true), - new Authentication.RealmRef(ServiceAccountSettings.REALM_NAME, ServiceAccountSettings.REALM_TYPE, nodeName), - null, Version.CURRENT, Authentication.AuthenticationType.TOKEN, - Map.of("_token_name", "token1", - "_token_source", authenticatingStore.getTokenSource().name().toLowerCase(Locale.ROOT)) + new ServiceAccountToken( + new ServiceAccountId("elastic", "fleet-server"), + "token1", + new SecureString("super-secret-value".toCharArray()) + ), + nodeName, + future5 + ); + assertThat( + future5.get(), + equalTo( + new Authentication( + new User( + "elastic/fleet-server", + Strings.EMPTY_ARRAY, + "Service account - elastic/fleet-server", + null, + Map.of("_elastic_service_account", true), + true + ), + new Authentication.RealmRef(ServiceAccountSettings.REALM_NAME, ServiceAccountSettings.REALM_TYPE, nodeName), + null, + Version.CURRENT, + Authentication.AuthenticationType.TOKEN, + Map.of("_token_name", "token1", "_token_source", authenticatingStore.getTokenSource().name().toLowerCase(Locale.ROOT)) + ) ) - )); + ); } public void testAuthenticateWithToken() throws ExecutionException, InterruptedException, IllegalAccessException { @@ -298,53 +378,92 @@ public void testAuthenticateWithToken() throws ExecutionException, InterruptedEx // non-elastic service account final ServiceAccountId accountId1 = new ServiceAccountId( randomValueOtherThan(ElasticServiceAccounts.NAMESPACE, () -> randomAlphaOfLengthBetween(3, 8)), - randomAlphaOfLengthBetween(3, 8)); - appender.addExpectation(new MockLogAppender.SeenEventExpectation( - "non-elastic service account", ServiceAccountService.class.getName(), Level.DEBUG, - "only [elastic] service accounts are supported, but received [" + accountId1.asPrincipal() + "]" - )); + randomAlphaOfLengthBetween(3, 8) + ); + appender.addExpectation( + new MockLogAppender.SeenEventExpectation( + "non-elastic service account", + ServiceAccountService.class.getName(), + Level.DEBUG, + "only [elastic] service accounts are supported, but received [" + accountId1.asPrincipal() + "]" + ) + ); final SecureString secret = new SecureString(randomAlphaOfLength(20).toCharArray()); final ServiceAccountToken token1 = new ServiceAccountToken(accountId1, randomAlphaOfLengthBetween(3, 8), secret); final PlainActionFuture future1 = new PlainActionFuture<>(); serviceAccountService.authenticateToken(token1, randomAlphaOfLengthBetween(3, 8), future1); final ExecutionException e1 = expectThrows(ExecutionException.class, future1::get); assertThat(e1.getCause().getClass(), is(ElasticsearchSecurityException.class)); - assertThat(e1.getMessage(), containsString("failed to authenticate service account [" - + token1.getAccountId().asPrincipal() + "] with token name [" + token1.getTokenName() + "]")); + assertThat( + e1.getMessage(), + containsString( + "failed to authenticate service account [" + + token1.getAccountId().asPrincipal() + + "] with token name [" + + token1.getTokenName() + + "]" + ) + ); appender.assertAllExpectationsMatched(); // Unknown elastic service name final ServiceAccountId accountId2 = new ServiceAccountId( ElasticServiceAccounts.NAMESPACE, - randomValueOtherThan("fleet-server", () -> randomAlphaOfLengthBetween(3, 8))); - appender.addExpectation(new MockLogAppender.SeenEventExpectation( - "unknown elastic service name", ServiceAccountService.class.getName(), Level.DEBUG, - "the [" + accountId2.asPrincipal() + "] service account does not exist" - )); + randomValueOtherThan("fleet-server", () -> randomAlphaOfLengthBetween(3, 8)) + ); + appender.addExpectation( + new MockLogAppender.SeenEventExpectation( + "unknown elastic service name", + ServiceAccountService.class.getName(), + Level.DEBUG, + "the [" + accountId2.asPrincipal() + "] service account does not exist" + ) + ); final ServiceAccountToken token2 = new ServiceAccountToken(accountId2, randomAlphaOfLengthBetween(3, 8), secret); final PlainActionFuture future2 = new PlainActionFuture<>(); serviceAccountService.authenticateToken(token2, randomAlphaOfLengthBetween(3, 8), future2); final ExecutionException e2 = expectThrows(ExecutionException.class, future2::get); assertThat(e2.getCause().getClass(), is(ElasticsearchSecurityException.class)); - assertThat(e2.getMessage(), containsString("failed to authenticate service account [" - + token2.getAccountId().asPrincipal() + "] with token name [" + token2.getTokenName() + "]")); + assertThat( + e2.getMessage(), + containsString( + "failed to authenticate service account [" + + token2.getAccountId().asPrincipal() + + "] with token name [" + + token2.getTokenName() + + "]" + ) + ); appender.assertAllExpectationsMatched(); // Length of secret value is too short final ServiceAccountId accountId3 = new ServiceAccountId(ElasticServiceAccounts.NAMESPACE, "fleet-server"); final SecureString secret3 = new SecureString(randomAlphaOfLengthBetween(1, 9).toCharArray()); final ServiceAccountToken token3 = new ServiceAccountToken(accountId3, randomAlphaOfLengthBetween(3, 8), secret3); - appender.addExpectation(new MockLogAppender.SeenEventExpectation( - "secret value too short", ServiceAccountService.class.getName(), Level.DEBUG, - "the provided credential has length [" + secret3.length() - + "] but a token's secret value must be at least [10] characters" - )); + appender.addExpectation( + new MockLogAppender.SeenEventExpectation( + "secret value too short", + ServiceAccountService.class.getName(), + Level.DEBUG, + "the provided credential has length [" + + secret3.length() + + "] but a token's secret value must be at least [10] characters" + ) + ); final PlainActionFuture future3 = new PlainActionFuture<>(); serviceAccountService.authenticateToken(token3, randomAlphaOfLengthBetween(3, 8), future3); final ExecutionException e3 = expectThrows(ExecutionException.class, future3::get); assertThat(e3.getCause().getClass(), is(ElasticsearchSecurityException.class)); - assertThat(e3.getMessage(), containsString("failed to authenticate service account [" - + token3.getAccountId().asPrincipal() + "] with token name [" + token3.getTokenName() + "]")); + assertThat( + e3.getMessage(), + containsString( + "failed to authenticate service account [" + + token3.getAccountId().asPrincipal() + + "] with token name [" + + token3.getTokenName() + + "]" + ) + ); appender.assertAllExpectationsMatched(); final TokenInfo.TokenSource tokenSource = randomFrom(TokenInfo.TokenSource.values()); @@ -361,29 +480,32 @@ public void testAuthenticateWithToken() throws ExecutionException, InterruptedEx // Success based on credential store final ServiceAccountId accountId4 = new ServiceAccountId(ElasticServiceAccounts.NAMESPACE, "fleet-server"); final ServiceAccountToken token4 = new ServiceAccountToken(accountId4, randomAlphaOfLengthBetween(3, 8), secret); - final ServiceAccountToken token5 = new ServiceAccountToken(accountId4, randomAlphaOfLengthBetween(3, 8), - new SecureString(randomAlphaOfLength(20).toCharArray())); + final ServiceAccountToken token5 = new ServiceAccountToken( + accountId4, + randomAlphaOfLengthBetween(3, 8), + new SecureString(randomAlphaOfLength(20).toCharArray()) + ); final String nodeName = randomAlphaOfLengthBetween(3, 8); doAnswer(invocationOnMock -> { @SuppressWarnings("unchecked") - final ActionListener listener = - (ActionListener) invocationOnMock.getArguments()[1]; + final ActionListener listener = (ActionListener< + ServiceAccountTokenStore.StoreAuthenticationResult>) invocationOnMock.getArguments()[1]; listener.onResponse(new ServiceAccountTokenStore.StoreAuthenticationResult(true, store.getTokenSource())); return null; }).when(store).authenticate(eq(token4), any()); doAnswer(invocationOnMock -> { @SuppressWarnings("unchecked") - final ActionListener listener = - (ActionListener) invocationOnMock.getArguments()[1]; + final ActionListener listener = (ActionListener< + ServiceAccountTokenStore.StoreAuthenticationResult>) invocationOnMock.getArguments()[1]; listener.onResponse(new ServiceAccountTokenStore.StoreAuthenticationResult(false, store.getTokenSource())); return null; }).when(store).authenticate(eq(token5), any()); doAnswer(invocationOnMock -> { @SuppressWarnings("unchecked") - final ActionListener listener = - (ActionListener) invocationOnMock.getArguments()[1]; + final ActionListener listener = (ActionListener< + ServiceAccountTokenStore.StoreAuthenticationResult>) invocationOnMock.getArguments()[1]; listener.onResponse(new ServiceAccountTokenStore.StoreAuthenticationResult(false, otherStore.getTokenSource())); return null; }).when(otherStore).authenticate(any(), any()); @@ -391,27 +513,53 @@ public void testAuthenticateWithToken() throws ExecutionException, InterruptedEx final PlainActionFuture future4 = new PlainActionFuture<>(); serviceAccountService.authenticateToken(token4, nodeName, future4); final Authentication authentication = future4.get(); - assertThat(authentication, equalTo(new Authentication( - new User("elastic/fleet-server", Strings.EMPTY_ARRAY, - "Service account - elastic/fleet-server", null, - Map.of("_elastic_service_account", true), - true), - new Authentication.RealmRef(ServiceAccountSettings.REALM_NAME, ServiceAccountSettings.REALM_TYPE, nodeName), - null, Version.CURRENT, Authentication.AuthenticationType.TOKEN, - Map.of("_token_name", token4.getTokenName(), "_token_source", tokenSource.name().toLowerCase(Locale.ROOT)) - ))); - - appender.addExpectation(new MockLogAppender.SeenEventExpectation( - "invalid credential", ServiceAccountService.class.getName(), Level.DEBUG, - "failed to authenticate service account [" + token5.getAccountId().asPrincipal() - + "] with token name [" + token5.getTokenName() + "]" - )); + assertThat( + authentication, + equalTo( + new Authentication( + new User( + "elastic/fleet-server", + Strings.EMPTY_ARRAY, + "Service account - elastic/fleet-server", + null, + Map.of("_elastic_service_account", true), + true + ), + new Authentication.RealmRef(ServiceAccountSettings.REALM_NAME, ServiceAccountSettings.REALM_TYPE, nodeName), + null, + Version.CURRENT, + Authentication.AuthenticationType.TOKEN, + Map.of("_token_name", token4.getTokenName(), "_token_source", tokenSource.name().toLowerCase(Locale.ROOT)) + ) + ) + ); + + appender.addExpectation( + new MockLogAppender.SeenEventExpectation( + "invalid credential", + ServiceAccountService.class.getName(), + Level.DEBUG, + "failed to authenticate service account [" + + token5.getAccountId().asPrincipal() + + "] with token name [" + + token5.getTokenName() + + "]" + ) + ); final PlainActionFuture future5 = new PlainActionFuture<>(); serviceAccountService.authenticateToken(token5, nodeName, future5); final ExecutionException e5 = expectThrows(ExecutionException.class, future5::get); assertThat(e5.getCause().getClass(), is(ElasticsearchSecurityException.class)); - assertThat(e5.getMessage(), containsString("failed to authenticate service account [" - + token5.getAccountId().asPrincipal() + "] with token name [" + token5.getTokenName() + "]")); + assertThat( + e5.getMessage(), + containsString( + "failed to authenticate service account [" + + token5.getAccountId().asPrincipal() + + "] with token name [" + + token5.getTokenName() + + "]" + ) + ); appender.assertAllExpectationsMatched(); } finally { appender.stop(); @@ -423,18 +571,24 @@ public void testAuthenticateWithToken() throws ExecutionException, InterruptedEx public void testGetRoleDescriptor() throws ExecutionException, InterruptedException { final TokenInfo.TokenSource tokenSource = randomFrom(TokenInfo.TokenSource.values()); final Authentication auth1 = new Authentication( - new User("elastic/fleet-server", + new User( + "elastic/fleet-server", Strings.EMPTY_ARRAY, "Service account - elastic/fleet-server", null, Map.of("_elastic_service_account", true), - true), + true + ), new Authentication.RealmRef( - ServiceAccountSettings.REALM_NAME, ServiceAccountSettings.REALM_TYPE, randomAlphaOfLengthBetween(3, 8)), + ServiceAccountSettings.REALM_NAME, + ServiceAccountSettings.REALM_TYPE, + randomAlphaOfLengthBetween(3, 8) + ), null, Version.CURRENT, Authentication.AuthenticationType.TOKEN, - Map.of("_token_name", randomAlphaOfLengthBetween(3, 8), "_token_source", tokenSource.name().toLowerCase(Locale.ROOT))); + Map.of("_token_name", randomAlphaOfLengthBetween(3, 8), "_token_source", tokenSource.name().toLowerCase(Locale.ROOT)) + ); final PlainActionFuture future1 = new PlainActionFuture<>(); serviceAccountService.getRoleDescriptor(auth1, future1); @@ -442,22 +596,26 @@ ServiceAccountSettings.REALM_NAME, ServiceAccountSettings.REALM_TYPE, randomAlph assertNotNull(roleDescriptor1); assertThat(roleDescriptor1.getName(), equalTo("elastic/fleet-server")); - final String username = - randomValueOtherThan("elastic/fleet-server", () -> randomAlphaOfLengthBetween(3, 8) + "/" + randomAlphaOfLengthBetween(3, 8)); + final String username = randomValueOtherThan( + "elastic/fleet-server", + () -> randomAlphaOfLengthBetween(3, 8) + "/" + randomAlphaOfLengthBetween(3, 8) + ); final Authentication auth2 = new Authentication( - new User(username, Strings.EMPTY_ARRAY, "Service account - " + username, null, - Map.of("_elastic_service_account", true), true), + new User(username, Strings.EMPTY_ARRAY, "Service account - " + username, null, Map.of("_elastic_service_account", true), true), new Authentication.RealmRef( - ServiceAccountSettings.REALM_NAME, ServiceAccountSettings.REALM_TYPE, randomAlphaOfLengthBetween(3, 8)), + ServiceAccountSettings.REALM_NAME, + ServiceAccountSettings.REALM_TYPE, + randomAlphaOfLengthBetween(3, 8) + ), null, Version.CURRENT, Authentication.AuthenticationType.TOKEN, - Map.of("_token_name", randomAlphaOfLengthBetween(3, 8), "_token_source", tokenSource.name().toLowerCase(Locale.ROOT))); + Map.of("_token_name", randomAlphaOfLengthBetween(3, 8), "_token_source", tokenSource.name().toLowerCase(Locale.ROOT)) + ); final PlainActionFuture future2 = new PlainActionFuture<>(); serviceAccountService.getRoleDescriptor(auth2, future2); final ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, future2::actionGet); - assertThat(e.getMessage(), containsString( - "cannot load role for service account [" + username + "] - no such service account")); + assertThat(e.getMessage(), containsString("cannot load role for service account [" + username + "] - no such service account")); } public void testCreateIndexTokenWillDelegate() { @@ -497,12 +655,12 @@ public void testFindTokensFor() { doAnswer(inv -> { final Object[] args = inv.getArguments(); @SuppressWarnings("unchecked") - final ActionListener listener = - (ActionListener) args[2]; + final ActionListener listener = (ActionListener< + GetServiceAccountCredentialsNodesResponse>) args[2]; listener.onResponse(fileTokensResponse); return null; - }).when(client).execute(eq(GetServiceAccountNodesCredentialsAction.INSTANCE), - any(GetServiceAccountCredentialsNodesRequest.class), any()); + }).when(client) + .execute(eq(GetServiceAccountNodesCredentialsAction.INSTANCE), any(GetServiceAccountCredentialsNodesRequest.class), any()); final PlainActionFuture future = new PlainActionFuture<>(); serviceAccountService.findTokensFor(new GetServiceAccountCredentialsRequest(namespace, serviceName), future); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/service/ServiceAccountTokenTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/service/ServiceAccountTokenTests.java index 9346033f64550..6c8c625c0ceea 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/service/ServiceAccountTokenTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/service/ServiceAccountTokenTests.java @@ -25,13 +25,17 @@ public void testNewToken() { ServiceAccountToken.newToken(accountId, ValidationTests.randomTokenName()); final String invalidTokeName = ValidationTests.randomInvalidTokenName(); - final IllegalArgumentException e1 = expectThrows(IllegalArgumentException.class, - () -> ServiceAccountToken.newToken(accountId, invalidTokeName)); + final IllegalArgumentException e1 = expectThrows( + IllegalArgumentException.class, + () -> ServiceAccountToken.newToken(accountId, invalidTokeName) + ); assertThat(e1.getMessage(), containsString(Validation.INVALID_SERVICE_ACCOUNT_TOKEN_NAME_MESSAGE)); assertThat(e1.getMessage(), containsString("invalid service token name [" + invalidTokeName + "]")); - final NullPointerException e2 = - expectThrows(NullPointerException.class, () -> ServiceAccountToken.newToken(null, ValidationTests.randomTokenName())); + final NullPointerException e2 = expectThrows( + NullPointerException.class, + () -> ServiceAccountToken.newToken(null, ValidationTests.randomTokenName()) + ); assertThat(e2.getMessage(), containsString("service account ID cannot be null")); } @@ -40,30 +44,41 @@ public void testServiceAccountTokenNew() { final SecureString secret = new SecureString(randomAlphaOfLength(20).toCharArray()); new ServiceAccountToken(accountId, ValidationTests.randomTokenName(), secret); - final NullPointerException e1 = - expectThrows(NullPointerException.class, () -> new ServiceAccountToken(null, ValidationTests.randomTokenName(), secret)); + final NullPointerException e1 = expectThrows( + NullPointerException.class, + () -> new ServiceAccountToken(null, ValidationTests.randomTokenName(), secret) + ); assertThat(e1.getMessage(), containsString("service account ID cannot be null")); final String invalidTokenName = ValidationTests.randomInvalidTokenName(); - final IllegalArgumentException e2 = expectThrows(IllegalArgumentException.class, - () -> new ServiceAccountToken(accountId, invalidTokenName, secret)); + final IllegalArgumentException e2 = expectThrows( + IllegalArgumentException.class, + () -> new ServiceAccountToken(accountId, invalidTokenName, secret) + ); assertThat(e2.getMessage(), containsString(Validation.INVALID_SERVICE_ACCOUNT_TOKEN_NAME_MESSAGE)); assertThat(e2.getMessage(), containsString("invalid service token name [" + invalidTokenName + "]")); - final NullPointerException e3 = - expectThrows(NullPointerException.class, () -> new ServiceAccountToken(accountId, ValidationTests.randomTokenName(), null)); + final NullPointerException e3 = expectThrows( + NullPointerException.class, + () -> new ServiceAccountToken(accountId, ValidationTests.randomTokenName(), null) + ); assertThat(e3.getMessage(), containsString("service account token secret cannot be null")); } public void testBearerString() throws IOException { - final ServiceAccountToken serviceAccountToken = - new ServiceAccountToken(new ServiceAccountId("elastic", "fleet-server"), - "token1", new SecureString("supersecret".toCharArray())); + final ServiceAccountToken serviceAccountToken = new ServiceAccountToken( + new ServiceAccountId("elastic", "fleet-server"), + "token1", + new SecureString("supersecret".toCharArray()) + ); assertThat(serviceAccountToken.asBearerString(), equalTo("AAEAAWVsYXN0aWMvZmxlZXQtc2VydmVyL3Rva2VuMTpzdXBlcnNlY3JldA")); - assertThat(ServiceAccountToken.fromBearerString( - new SecureString("AAEAAWVsYXN0aWMvZmxlZXQtc2VydmVyL3Rva2VuMTpzdXBlcnNlY3JldA".toCharArray())), - equalTo(serviceAccountToken)); + assertThat( + ServiceAccountToken.fromBearerString( + new SecureString("AAEAAWVsYXN0aWMvZmxlZXQtc2VydmVyL3Rva2VuMTpzdXBlcnNlY3JldA".toCharArray()) + ), + equalTo(serviceAccountToken) + ); final ServiceAccountId accountId = new ServiceAccountId(randomAlphaOfLengthBetween(3, 8), randomAlphaOfLengthBetween(3, 8)); final ServiceAccountToken serviceAccountToken1 = ServiceAccountToken.newToken(accountId, ValidationTests.randomTokenName()); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/ApiKeyGeneratorTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/ApiKeyGeneratorTests.java index 84dc1b7b2421e..bb58e60287d88 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/ApiKeyGeneratorTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/ApiKeyGeneratorTests.java @@ -11,8 +11,8 @@ import org.elasticsearch.action.support.PlainActionFuture; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.util.set.Sets; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.security.action.CreateApiKeyRequest; import org.elasticsearch.xpack.core.security.action.CreateApiKeyResponse; import org.elasticsearch.xpack.core.security.authc.Authentication; @@ -44,7 +44,8 @@ public void testGenerateApiKeySuccessfully() { final Authentication authentication = new Authentication( new User("test", userRoleNames.toArray(String[]::new)), new Authentication.RealmRef("realm-name", "realm-type", "node-name"), - null); + null + ); final CreateApiKeyRequest request = new CreateApiKeyRequest("name", null, null); final Set roleDescriptors = randomSubsetOf(userRoleNames).stream() @@ -64,7 +65,11 @@ public void testGenerateApiKeySuccessfully() { }).when(rolesStore).getRoleDescriptors(anySetOf(String.class), any(ActionListener.class)); CreateApiKeyResponse response = new CreateApiKeyResponse( - "name", randomAlphaOfLength(18), new SecureString(randomAlphaOfLength(24).toCharArray()), null); + "name", + randomAlphaOfLength(18), + new SecureString(randomAlphaOfLength(24).toCharArray()), + null + ); doAnswer(inv -> { final Object[] args = inv.getArguments(); assertThat(args, arrayWithSize(4)); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/CachingUsernamePasswordRealmTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/CachingUsernamePasswordRealmTests.java index 4c62185aa5464..4aa6f34257c51 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/CachingUsernamePasswordRealmTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/CachingUsernamePasswordRealmTests.java @@ -10,8 +10,8 @@ import org.elasticsearch.action.support.PlainActionFuture; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.env.Environment; import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.test.ESTestCase; @@ -77,19 +77,23 @@ public void testCacheSettings() { TimeValue ttl = TimeValue.timeValueMinutes(randomIntBetween(10, 20)); final RealmConfig.RealmIdentifier identifier = new RealmConfig.RealmIdentifier("caching", "test_realm"); Settings settings = Settings.builder() - .put(globalSettings) - .put(getFullSettingKey(identifier, CachingUsernamePasswordRealmSettings.CACHE_HASH_ALGO_SETTING), cachingHashAlgo) - .put(getFullSettingKey(identifier, CachingUsernamePasswordRealmSettings.CACHE_MAX_USERS_SETTING), maxUsers) - .put(getFullSettingKey(identifier, CachingUsernamePasswordRealmSettings.CACHE_TTL_SETTING), ttl) - .put(getFullSettingKey(identifier, RealmSettings.ORDER_SETTING), 0) - .build(); - - RealmConfig config = new RealmConfig(identifier, settings, - TestEnvironment.newEnvironment(globalSettings), new ThreadContext(Settings.EMPTY)); + .put(globalSettings) + .put(getFullSettingKey(identifier, CachingUsernamePasswordRealmSettings.CACHE_HASH_ALGO_SETTING), cachingHashAlgo) + .put(getFullSettingKey(identifier, CachingUsernamePasswordRealmSettings.CACHE_MAX_USERS_SETTING), maxUsers) + .put(getFullSettingKey(identifier, CachingUsernamePasswordRealmSettings.CACHE_TTL_SETTING), ttl) + .put(getFullSettingKey(identifier, RealmSettings.ORDER_SETTING), 0) + .build(); + + RealmConfig config = new RealmConfig( + identifier, + settings, + TestEnvironment.newEnvironment(globalSettings), + new ThreadContext(Settings.EMPTY) + ); CachingUsernamePasswordRealm realm = new CachingUsernamePasswordRealm(config, threadPool) { @Override protected void doAuthenticate(UsernamePasswordToken token, ActionListener> listener) { - listener.onResponse(AuthenticationResult.success(new User("username", new String[]{"r1", "r2", "r3"}))); + listener.onResponse(AuthenticationResult.success(new User("username", new String[] { "r1", "r2", "r3" }))); } @Override @@ -103,17 +107,21 @@ protected void doLookupUser(String username, ActionListener listener) { public void testCacheSizeWhenCacheDisabled() { final RealmConfig.RealmIdentifier identifier = new RealmConfig.RealmIdentifier("caching", "test_realm"); final Settings settings = Settings.builder() - .put(globalSettings) - .put(getFullSettingKey(identifier, CachingUsernamePasswordRealmSettings.CACHE_TTL_SETTING), -1) - .put(getFullSettingKey(identifier, RealmSettings.ORDER_SETTING), 0) - .build(); + .put(globalSettings) + .put(getFullSettingKey(identifier, CachingUsernamePasswordRealmSettings.CACHE_TTL_SETTING), -1) + .put(getFullSettingKey(identifier, RealmSettings.ORDER_SETTING), 0) + .build(); - final RealmConfig config = - new RealmConfig(identifier, settings, TestEnvironment.newEnvironment(globalSettings), new ThreadContext(Settings.EMPTY)); + final RealmConfig config = new RealmConfig( + identifier, + settings, + TestEnvironment.newEnvironment(globalSettings), + new ThreadContext(Settings.EMPTY) + ); final CachingUsernamePasswordRealm realm = new CachingUsernamePasswordRealm(config, threadPool) { @Override protected void doAuthenticate(UsernamePasswordToken token, ActionListener> listener) { - listener.onResponse(AuthenticationResult.success(new User("username", new String[]{"r1", "r2", "r3"}))); + listener.onResponse(AuthenticationResult.success(new User("username", new String[] { "r1", "r2", "r3" }))); } @Override @@ -216,7 +224,7 @@ public void testLookupAndAuthCache() { assertThat(realm.lookupInvocationCounter.intValue(), is(1)); assertThat(realm.authInvocationCounter.intValue(), is(2)); assertThat(user.roles(), arrayContaining("testRole1", "testRole2")); - //now lookup b + // now lookup b lookupFuture = new PlainActionFuture<>(); realm.lookupUser("b", lookupFuture); lookedUp = lookupFuture.actionGet(); @@ -288,8 +296,12 @@ public void testCacheWithVeryLowTtlExpiresBetweenAuthenticateCalls() throws Inte .put(getFullSettingKey(identifier, CachingUsernamePasswordRealmSettings.CACHE_TTL_SETTING), ttl) .put(getFullSettingKey(identifier, RealmSettings.ORDER_SETTING), 0) .build(); - RealmConfig config = new RealmConfig(identifier, settings, - TestEnvironment.newEnvironment(globalSettings), new ThreadContext(Settings.EMPTY)); + RealmConfig config = new RealmConfig( + identifier, + settings, + TestEnvironment.newEnvironment(globalSettings), + new ThreadContext(Settings.EMPTY) + ); AlwaysAuthenticateCachingRealm realm = new AlwaysAuthenticateCachingRealm(config, threadPool); final UsernamePasswordToken authToken = new UsernamePasswordToken("the-user", new SecureString("the-password")); @@ -316,12 +328,16 @@ public void testReadsDoNotPreventCacheExpiry() throws InterruptedException { TimeValue ttl = TimeValue.timeValueMillis(250); final RealmConfig.RealmIdentifier identifier = new RealmConfig.RealmIdentifier("caching", "test_cache_ttl"); Settings settings = Settings.builder() - .put(globalSettings) - .put(getFullSettingKey(identifier, CachingUsernamePasswordRealmSettings.CACHE_TTL_SETTING), ttl) - .put(getFullSettingKey(identifier, RealmSettings.ORDER_SETTING), 0) - .build(); - RealmConfig config = new RealmConfig(identifier, settings, - TestEnvironment.newEnvironment(globalSettings), new ThreadContext(Settings.EMPTY)); + .put(globalSettings) + .put(getFullSettingKey(identifier, CachingUsernamePasswordRealmSettings.CACHE_TTL_SETTING), ttl) + .put(getFullSettingKey(identifier, RealmSettings.ORDER_SETTING), 0) + .build(); + RealmConfig config = new RealmConfig( + identifier, + settings, + TestEnvironment.newEnvironment(globalSettings), + new ThreadContext(Settings.EMPTY) + ); AlwaysAuthenticateCachingRealm realm = new AlwaysAuthenticateCachingRealm(config, threadPool); final UsernamePasswordToken authToken = new UsernamePasswordToken("the-user", new SecureString("the-password")); @@ -427,16 +443,17 @@ public void testSingleAuthPerUserLimit() throws Exception { final RealmConfig.RealmIdentifier realmIdentifier = new RealmConfig.RealmIdentifier("caching", "test_realm"); RealmConfig config = new RealmConfig( realmIdentifier, - Settings.builder().put(globalSettings) - .put(getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0).build(), - TestEnvironment.newEnvironment(globalSettings), new ThreadContext(Settings.EMPTY)); + Settings.builder().put(globalSettings).put(getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0).build(), + TestEnvironment.newEnvironment(globalSettings), + new ThreadContext(Settings.EMPTY) + ); final CachingUsernamePasswordRealm realm = new CachingUsernamePasswordRealm(config, threadPool) { @Override protected void doAuthenticate(UsernamePasswordToken token, ActionListener> listener) { authCounter.incrementAndGet(); // do something slow if (pwdHasher.verify(token.credentials(), passwordHash.toCharArray())) { - listener.onResponse(AuthenticationResult.success(new User(username, new String[]{"r1", "r2", "r3"}))); + listener.onResponse(AuthenticationResult.success(new User(username, new String[] { "r1", "r2", "r3" }))); } else { listener.onFailure(new IllegalStateException("password auth should never fail")); } @@ -497,9 +514,10 @@ public void testUnauthenticatedResultPropagatesWithSameCreds() throws Exception final RealmConfig.RealmIdentifier realmIdentifier = new RealmConfig.RealmIdentifier("caching", "test_realm"); RealmConfig config = new RealmConfig( realmIdentifier, - Settings.builder().put(globalSettings) - .put(getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0).build(), - TestEnvironment.newEnvironment(globalSettings), new ThreadContext(Settings.EMPTY)); + Settings.builder().put(globalSettings).put(getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0).build(), + TestEnvironment.newEnvironment(globalSettings), + new ThreadContext(Settings.EMPTY) + ); final int numberOfProcessors = Runtime.getRuntime().availableProcessors(); final int numberOfThreads = scaledRandomIntBetween((numberOfProcessors + 1) / 2, numberOfProcessors * 3); @@ -582,16 +600,18 @@ public void testCacheConcurrency() throws Exception { final Hasher localHasher = getFastStoredHashAlgoForTests(); final String passwordHash = new String(localHasher.hash(password)); final RealmConfig.RealmIdentifier realmIdentifier = new RealmConfig.RealmIdentifier("caching", "test_realm"); - RealmConfig config = new RealmConfig(realmIdentifier, - Settings.builder().put(globalSettings) - .put(getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0).build(), - TestEnvironment.newEnvironment(globalSettings), new ThreadContext(Settings.EMPTY)); + RealmConfig config = new RealmConfig( + realmIdentifier, + Settings.builder().put(globalSettings).put(getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0).build(), + TestEnvironment.newEnvironment(globalSettings), + new ThreadContext(Settings.EMPTY) + ); final CachingUsernamePasswordRealm realm = new CachingUsernamePasswordRealm(config, threadPool) { @Override protected void doAuthenticate(UsernamePasswordToken token, ActionListener> listener) { // do something slow if (localHasher.verify(token.credentials(), passwordHash.toCharArray())) { - listener.onResponse(AuthenticationResult.success(new User(username, new String[]{"r1", "r2", "r3"}))); + listener.onResponse(AuthenticationResult.success(new User(username, new String[] { "r1", "r2", "r3" }))); } else { listener.onResponse(AuthenticationResult.unsuccessful("Incorrect password", null)); } @@ -653,10 +673,12 @@ public void testUserLookupConcurrency() throws Exception { final AtomicInteger lookupCounter = new AtomicInteger(0); final RealmConfig.RealmIdentifier realmIdentifier = new RealmConfig.RealmIdentifier("caching", "test_realm"); - RealmConfig config = new RealmConfig(realmIdentifier, - Settings.builder().put(globalSettings) - .put(getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0).build(), - TestEnvironment.newEnvironment(globalSettings), new ThreadContext(Settings.EMPTY)); + RealmConfig config = new RealmConfig( + realmIdentifier, + Settings.builder().put(globalSettings).put(getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0).build(), + TestEnvironment.newEnvironment(globalSettings), + new ThreadContext(Settings.EMPTY) + ); final CachingUsernamePasswordRealm realm = new CachingUsernamePasswordRealm(config, threadPool) { @Override protected void doAuthenticate(UsernamePasswordToken token, ActionListener> listener) { @@ -666,7 +688,7 @@ protected void doAuthenticate(UsernamePasswordToken token, ActionListener listener) { lookupCounter.incrementAndGet(); - listener.onResponse(new User(username, new String[]{"r1", "r2", "r3"})); + listener.onResponse(new User(username, new String[] { "r1", "r2", "r3" })); } }; @@ -744,16 +766,18 @@ public void testAuthenticateDisabled() throws Exception { static class FailingAuthenticationRealm extends CachingUsernamePasswordRealm { FailingAuthenticationRealm(Settings global, ThreadPool threadPool) { - super(new RealmConfig( - new RealmConfig.RealmIdentifier("caching", "failing-test"), - Settings.builder() - .put(global) - .put(getFullSettingKey( - new RealmConfig.RealmIdentifier("caching", "failing-test"), - RealmSettings.ORDER_SETTING), 0) - .build(), - TestEnvironment.newEnvironment(global), - threadPool.getThreadContext()), threadPool); + super( + new RealmConfig( + new RealmConfig.RealmIdentifier("caching", "failing-test"), + Settings.builder() + .put(global) + .put(getFullSettingKey(new RealmConfig.RealmIdentifier("caching", "failing-test"), RealmSettings.ORDER_SETTING), 0) + .build(), + TestEnvironment.newEnvironment(global), + threadPool.getThreadContext() + ), + threadPool + ); } @Override @@ -770,14 +794,18 @@ protected void doLookupUser(String username, ActionListener listener) { static class ThrowingAuthenticationRealm extends CachingUsernamePasswordRealm { ThrowingAuthenticationRealm(Settings globalSettings, ThreadPool threadPool) { - super(new RealmConfig( - new RealmConfig.RealmIdentifier("caching", "throwing-test"), - Settings.builder() - .put(globalSettings) - .put(getFullSettingKey(new RealmConfig.RealmIdentifier("caching", "throwing-test"), RealmSettings.ORDER_SETTING), 0) - .build(), - TestEnvironment.newEnvironment(globalSettings), - threadPool.getThreadContext()), threadPool); + super( + new RealmConfig( + new RealmConfig.RealmIdentifier("caching", "throwing-test"), + Settings.builder() + .put(globalSettings) + .put(getFullSettingKey(new RealmConfig.RealmIdentifier("caching", "throwing-test"), RealmSettings.ORDER_SETTING), 0) + .build(), + TestEnvironment.newEnvironment(globalSettings), + threadPool.getThreadContext() + ), + threadPool + ); } @Override @@ -799,14 +827,18 @@ static class AlwaysAuthenticateCachingRealm extends CachingUsernamePasswordRealm private boolean usersEnabled = true; AlwaysAuthenticateCachingRealm(Settings globalSettings, ThreadPool threadPool) { - this(new RealmConfig( - new RealmConfig.RealmIdentifier("caching", "always-test"), - Settings.builder() - .put(globalSettings) - .put(getFullSettingKey(new RealmConfig.RealmIdentifier("caching", "always-test"), RealmSettings.ORDER_SETTING), 0) - .build(), - TestEnvironment.newEnvironment(globalSettings), - threadPool.getThreadContext()), threadPool); + this( + new RealmConfig( + new RealmConfig.RealmIdentifier("caching", "always-test"), + Settings.builder() + .put(globalSettings) + .put(getFullSettingKey(new RealmConfig.RealmIdentifier("caching", "always-test"), RealmSettings.ORDER_SETTING), 0) + .build(), + TestEnvironment.newEnvironment(globalSettings), + threadPool.getThreadContext() + ), + threadPool + ); } AlwaysAuthenticateCachingRealm(RealmConfig config, ThreadPool threadPool) { @@ -820,14 +852,14 @@ void setUsersEnabled(boolean usersEnabled) { @Override protected void doAuthenticate(UsernamePasswordToken token, ActionListener> listener) { authInvocationCounter.incrementAndGet(); - final User user = new User(token.principal(), new String[]{"testRole1", "testRole2"}, null, null, emptyMap(), usersEnabled); + final User user = new User(token.principal(), new String[] { "testRole1", "testRole2" }, null, null, emptyMap(), usersEnabled); listener.onResponse(AuthenticationResult.success(user)); } @Override protected void doLookupUser(String username, ActionListener listener) { lookupInvocationCounter.incrementAndGet(); - listener.onResponse(new User(username, new String[]{"lookupRole1", "lookupRole2"})); + listener.onResponse(new User(username, new String[] { "lookupRole1", "lookupRole2" })); } } } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/DelegatedAuthorizationSupportTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/DelegatedAuthorizationSupportTests.java index 9a46ba2e3f38f..1bf7ae133e118 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/DelegatedAuthorizationSupportTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/DelegatedAuthorizationSupportTests.java @@ -47,9 +47,7 @@ public class DelegatedAuthorizationSupportTests extends ESTestCase { @Before public void setupRealms() { - globalSettings = Settings.builder() - .put("path.home", createTempDir()) - .build(); + globalSettings = Settings.builder().put("path.home", createTempDir()).build(); env = TestEnvironment.newEnvironment(globalSettings); threadContext = new ThreadContext(globalSettings); @@ -70,12 +68,15 @@ private RealmConfig buildRealmConfig(String name, Settings settings) { RealmConfig.RealmIdentifier realmIdentifier = new RealmConfig.RealmIdentifier("test", name); return new RealmConfig( realmIdentifier, - Settings.builder().put(settings) + Settings.builder() + .put(settings) .normalizePrefix("xpack.security.authc.realms.test." + name + ".") .put(globalSettings) .put(RealmSettings.getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0) .build(), - env, threadContext); + env, + threadContext + ); } public void testEmptyDelegationList() throws ExecutionException, InterruptedException { @@ -92,29 +93,29 @@ public void testEmptyDelegationList() throws ExecutionException, InterruptedExce public void testMissingRealmInDelegationList() { final XPackLicenseState license = getLicenseState(true); - final Settings settings = Settings.builder() - .putList("authorization_realms", "no-such-realm") - .build(); - final IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> - new DelegatedAuthorizationSupport(realms, buildRealmConfig("r", settings), license) + final Settings settings = Settings.builder().putList("authorization_realms", "no-such-realm").build(); + final IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> new DelegatedAuthorizationSupport(realms, buildRealmConfig("r", settings), license) ); assertThat(ex.getMessage(), equalTo("configured authorization realm [no-such-realm] does not exist (or is not enabled)")); } public void testDelegationChainsAreRejected() { final XPackLicenseState license = getLicenseState(true); - final Settings settings = Settings.builder() - .putList("authorization_realms", "lookup-1", "lookup-2", "lookup-3") - .build(); + final Settings settings = Settings.builder().putList("authorization_realms", "lookup-1", "lookup-2", "lookup-3").build(); globalSettings = Settings.builder() .put(globalSettings) .putList("xpack.security.authc.realms.test.lookup-2.authorization_realms", "lookup-1") .build(); - final IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> - new DelegatedAuthorizationSupport(realms, buildRealmConfig("realm1", settings), license) + final IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> new DelegatedAuthorizationSupport(realms, buildRealmConfig("realm1", settings), license) + ); + assertThat( + ex.getMessage(), + equalTo("cannot use realm [test/lookup-2] as an authorization realm - it is already delegating authorization to [[lookup-1]]") ); - assertThat(ex.getMessage(), - equalTo("cannot use realm [test/lookup-2] as an authorization realm - it is already delegating authorization to [[lookup-1]]")); } public void testMatchInDelegationList() throws Exception { @@ -138,9 +139,7 @@ public void testRealmsAreOrdered() throws Exception { final XPackLicenseState license = getLicenseState(true); final List useRealms = shuffle(randomSubsetOf(randomIntBetween(3, realms.size()), realms)); final List names = useRealms.stream().map(Realm::name).collect(Collectors.toList()); - final Settings settings = Settings.builder() - .putList("authorization_realms", names) - .build(); + final Settings settings = Settings.builder().putList("authorization_realms", names).build(); final List users = new ArrayList<>(names.size()); final String username = randomAlphaOfLength(8); for (MockLookupRealm r : useRealms) { @@ -172,15 +171,19 @@ public void testNoMatchInDelegationList() throws Exception { final AuthenticationResult result = future.get(); assertThat(result.getStatus(), equalTo(AuthenticationResult.Status.CONTINUE)); assertThat(result.getValue(), nullValue()); - assertThat(result.getMessage(), equalTo("the principal [my_user] was authenticated, but no user could be found in realms [" + - collectionToDelimitedString(useRealms.stream().map(Realm::toString).collect(Collectors.toList()), ",") + "]")); + assertThat( + result.getMessage(), + equalTo( + "the principal [my_user] was authenticated, but no user could be found in realms [" + + collectionToDelimitedString(useRealms.stream().map(Realm::toString).collect(Collectors.toList()), ",") + + "]" + ) + ); } public void testLicenseRejection() throws Exception { final XPackLicenseState license = getLicenseState(false); - final Settings settings = Settings.builder() - .putList("authorization_realms", realms.get(0).name()) - .build(); + final Settings settings = Settings.builder().putList("authorization_realms", realms.get(0).name()).build(); final DelegatedAuthorizationSupport das = new DelegatedAuthorizationSupport(realms, buildRealmConfig("r", settings), license); assertThat(das.hasDelegation(), equalTo(true)); final PlainActionFuture> future = new PlainActionFuture<>(); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/DistinguishedNamePredicateTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/DistinguishedNamePredicateTests.java index abd557aa8ae15..444cc850af02a 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/DistinguishedNamePredicateTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/DistinguishedNamePredicateTests.java @@ -7,6 +7,7 @@ package org.elasticsearch.xpack.security.authc.support; import com.unboundid.ldap.sdk.DN; + import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.security.authc.support.UserRoleMapper; import org.elasticsearch.xpack.core.security.authc.support.mapper.expressiondsl.FieldExpression.FieldValue; @@ -19,9 +20,12 @@ public class DistinguishedNamePredicateTests extends ESTestCase { public void testMatching() throws Exception { - String randomDn = "CN=" + randomAlphaOfLengthBetween(3, 12) - + ",OU=" + randomAlphaOfLength(4) - + ", O=" + randomAlphaOfLengthBetween(2, 6); + String randomDn = "CN=" + + randomAlphaOfLengthBetween(3, 12) + + ",OU=" + + randomAlphaOfLength(4) + + ", O=" + + randomAlphaOfLengthBetween(2, 6); // Randomly enter the DN in mixed case, lower case or upper case; final String inputDn; diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/DnRoleMapperTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/DnRoleMapperTests.java index 9a0c043b66f4b..29b0437885452 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/DnRoleMapperTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/DnRoleMapperTests.java @@ -7,6 +7,7 @@ package org.elasticsearch.xpack.security.authc.support; import com.unboundid.ldap.sdk.DN; + import org.apache.logging.log4j.Level; import org.apache.logging.log4j.Logger; import org.elasticsearch.ElasticsearchException; @@ -53,16 +54,15 @@ public class DnRoleMapperTests extends ESTestCase { - private static final String[] STARK_GROUP_DNS = new String[]{ - //groups can be named by different attributes, depending on the directory, - //we don't care what it is named by - "cn=shield,ou=marvel,o=superheros", - "cn=avengers,ou=marvel,o=superheros", - "group=genius, dc=mit, dc=edu", - "groupName = billionaire , ou = acme", - "gid = playboy , dc = example , dc = com", - "groupid=philanthropist,ou=groups,dc=unitedway,dc=org" - }; + private static final String[] STARK_GROUP_DNS = new String[] { + // groups can be named by different attributes, depending on the directory, + // we don't care what it is named by + "cn=shield,ou=marvel,o=superheros", + "cn=avengers,ou=marvel,o=superheros", + "group=genius, dc=mit, dc=edu", + "groupName = billionaire , ou = acme", + "gid = playboy , dc = example , dc = com", + "groupid=philanthropist,ou=groups,dc=unitedway,dc=org" }; protected Settings settings; protected Environment env; @@ -70,10 +70,7 @@ public class DnRoleMapperTests extends ESTestCase { @Before public void init() throws IOException { - settings = Settings.builder() - .put("resource.reload.interval.high", "100ms") - .put("path.home", createTempDir()) - .build(); + settings = Settings.builder().put("resource.reload.interval.high", "100ms").put("path.home", createTempDir()).build(); env = TestEnvironment.newEnvironment(settings); if (Files.exists(env.configFile()) == false) { Files.createDirectory(env.configFile()); @@ -127,8 +124,7 @@ public void testMapper_AutoReload() throws Exception { try (BufferedWriter writer = Files.newBufferedWriter(file, StandardCharsets.UTF_8, StandardOpenOption.APPEND)) { writer.newLine(); - writer.append("fantastic_four:\n") - .append(" - \"cn=fantastic_four,ou=marvel,o=superheros\""); + writer.append("fantastic_four:\n").append(" - \"cn=fantastic_four,ou=marvel,o=superheros\""); } if (latch.await(5, TimeUnit.SECONDS) == false) { @@ -183,15 +179,11 @@ public void testMapperAutoReloadWithoutListener() throws Exception { try (BufferedWriter writer = Files.newBufferedWriter(file, StandardCharsets.UTF_8, StandardOpenOption.APPEND)) { writer.newLine(); - writer.append("fantastic_four:\n") - .append(" - \"cn=fantastic_four,ou=marvel,o=superheros\""); + writer.append("fantastic_four:\n").append(" - \"cn=fantastic_four,ou=marvel,o=superheros\""); } assertBusy(() -> { - Set resolvedRoles = mapper.resolveRoles( - "", - Collections.singletonList("cn=fantastic_four,ou=marvel,o=superheros") - ); + Set resolvedRoles = mapper.resolveRoles("", Collections.singletonList("cn=fantastic_four,ou=marvel,o=superheros")); assertThat(resolvedRoles, notNullValue()); assertThat(resolvedRoles.size(), is(1)); assertThat(resolvedRoles, contains("fantastic_four")); @@ -258,9 +250,10 @@ public void testParseFile_WhenFileDoesNotExist() throws Exception { assertThat(mappings, notNullValue()); assertThat(mappings.isEmpty(), is(true)); - final ElasticsearchException exception = expectThrows(ElasticsearchException.class, () -> { - DnRoleMapper.parseFile(file, logger, "_type", "_name", true); - }); + final ElasticsearchException exception = expectThrows( + ElasticsearchException.class, + () -> { DnRoleMapper.parseFile(file, logger, "_type", "_name", true); } + ); assertThat(exception.getMessage(), containsString(file.toString())); assertThat(exception.getMessage(), containsString("does not exist")); assertThat(exception.getMessage(), containsString("_name")); @@ -301,15 +294,19 @@ public void testYaml() throws Exception { .put(getFullSettingKey(realmIdentifier, DnRoleMapperSettings.ROLE_MAPPING_FILE_SETTING), file.toAbsolutePath()) .put(getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0) .build(); - RealmConfig config = new RealmConfig(realmIdentifier, ldapSettings, - TestEnvironment.newEnvironment(settings), new ThreadContext(Settings.EMPTY)); + RealmConfig config = new RealmConfig( + realmIdentifier, + ldapSettings, + TestEnvironment.newEnvironment(settings), + new ThreadContext(Settings.EMPTY) + ); try (ResourceWatcherService watcherService = new ResourceWatcherService(settings, threadPool)) { DnRoleMapper mapper = new DnRoleMapper(config, watcherService); Set roles = mapper.resolveRoles("", Arrays.asList(STARK_GROUP_DNS)); - //verify + // verify assertThat(roles, hasItems("security", "avenger")); } } @@ -317,12 +314,16 @@ public void testYaml() throws Exception { public void testRelativeDN() { final RealmConfig.RealmIdentifier realmIdentifier = new RealmConfig.RealmIdentifier("ldap", "ldap1"); Settings ldapSettings = Settings.builder() - .put(settings) - .put(getFullSettingKey(realmIdentifier, DnRoleMapperSettings.USE_UNMAPPED_GROUPS_AS_ROLES_SETTING), true) - .put(getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0) - .build(); - RealmConfig config = new RealmConfig(realmIdentifier, ldapSettings, - TestEnvironment.newEnvironment(settings), new ThreadContext(Settings.EMPTY)); + .put(settings) + .put(getFullSettingKey(realmIdentifier, DnRoleMapperSettings.USE_UNMAPPED_GROUPS_AS_ROLES_SETTING), true) + .put(getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0) + .build(); + RealmConfig config = new RealmConfig( + realmIdentifier, + ldapSettings, + TestEnvironment.newEnvironment(settings), + new ThreadContext(Settings.EMPTY) + ); try (ResourceWatcherService watcherService = new ResourceWatcherService(settings, threadPool)) { DnRoleMapper mapper = new DnRoleMapper(config, watcherService); @@ -336,13 +337,17 @@ public void testUserDNMapping() throws Exception { final RealmConfig.RealmIdentifier realmIdentifier = new RealmConfig.RealmIdentifier("ldap", "ldap-userdn-role"); Path file = getDataPath("role_mapping.yml"); Settings ldapSettings = Settings.builder() - .put(settings) - .put(getFullSettingKey(realmIdentifier, DnRoleMapperSettings.ROLE_MAPPING_FILE_SETTING), file.toAbsolutePath()) - .put(getFullSettingKey(realmIdentifier, DnRoleMapperSettings.USE_UNMAPPED_GROUPS_AS_ROLES_SETTING), false) - .put(getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0) - .build(); - RealmConfig config = new RealmConfig(realmIdentifier, ldapSettings, - TestEnvironment.newEnvironment(settings), new ThreadContext(Settings.EMPTY)); + .put(settings) + .put(getFullSettingKey(realmIdentifier, DnRoleMapperSettings.ROLE_MAPPING_FILE_SETTING), file.toAbsolutePath()) + .put(getFullSettingKey(realmIdentifier, DnRoleMapperSettings.USE_UNMAPPED_GROUPS_AS_ROLES_SETTING), false) + .put(getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0) + .build(); + RealmConfig config = new RealmConfig( + realmIdentifier, + ldapSettings, + TestEnvironment.newEnvironment(settings), + new ThreadContext(Settings.EMPTY) + ); try (ResourceWatcherService watcherService = new ResourceWatcherService(settings, threadPool)) { DnRoleMapper mapper = new DnRoleMapper(config, watcherService); @@ -355,10 +360,10 @@ public void testUserDNMapping() throws Exception { protected DnRoleMapper createMapper(Path file, ResourceWatcherService watcherService) { final RealmConfig.RealmIdentifier identifier = new RealmConfig.RealmIdentifier("ldap", "ad-group-mapper-test"); Settings mergedSettings = Settings.builder() - .put(settings) - .put(getFullSettingKey(identifier, DnRoleMapperSettings.ROLE_MAPPING_FILE_SETTING), file.toAbsolutePath()) - .put(getFullSettingKey(identifier, RealmSettings.ORDER_SETTING), 0) - .build(); + .put(settings) + .put(getFullSettingKey(identifier, DnRoleMapperSettings.ROLE_MAPPING_FILE_SETTING), file.toAbsolutePath()) + .put(getFullSettingKey(identifier, RealmSettings.ORDER_SETTING), 0) + .build(); RealmConfig config = new RealmConfig(identifier, mergedSettings, env, new ThreadContext(Settings.EMPTY)); return new DnRoleMapper(config, watcherService); } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/DummyUsernamePasswordRealm.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/DummyUsernamePasswordRealm.java index bd002aa81aab7..19698e8a0d29f 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/DummyUsernamePasswordRealm.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/DummyUsernamePasswordRealm.java @@ -8,8 +8,8 @@ package org.elasticsearch.xpack.security.authc.support; import org.elasticsearch.action.ActionListener; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.settings.SecureString; +import org.elasticsearch.core.Tuple; import org.elasticsearch.xpack.core.security.authc.AuthenticationResult; import org.elasticsearch.xpack.core.security.authc.AuthenticationToken; import org.elasticsearch.xpack.core.security.authc.RealmConfig; diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/HasherTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/HasherTests.java index 062cc2d8a45d0..762af46bbac1f 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/HasherTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/HasherTests.java @@ -115,79 +115,137 @@ public void testResolve() { assertThat(Hasher.resolve("ssha256"), sameInstance(Hasher.SSHA256)); assertThat(Hasher.resolve("noop"), sameInstance(Hasher.NOOP)); assertThat(Hasher.resolve("clear_text"), sameInstance(Hasher.NOOP)); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> { - Hasher.resolve("unknown_hasher"); - }); + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> { Hasher.resolve("unknown_hasher"); }); assertThat(e.getMessage(), containsString("unknown hash function ")); } public void testResolveFromHash() { - assertThat(Hasher.resolveFromHash("$2a$10$1oZj.8KmlwiCy4DWKvDH3OU0Ko4WRF4FknyvCh3j/ZtaRCNYA6Xzm".toCharArray()), - sameInstance(Hasher.BCRYPT)); - assertThat(Hasher.resolveFromHash("$2a$04$GwJtIQiGMHASEYphMiCpjeZh1cDyYC5U.DKfNKa4i/y0IbOvc2LiG".toCharArray()), - sameInstance(Hasher.BCRYPT4)); - assertThat(Hasher.resolveFromHash("$2a$05$xLmwSB7Nw7PcqP.6hXdc4eUZbT.4.iAZ3CTPzSaUibrrYjC6Vwq1m".toCharArray()), - sameInstance(Hasher.BCRYPT5)); - assertThat(Hasher.resolveFromHash("$2a$06$WQX1MALAjVOhR2YKmLcHYed2oROzBl3OZPtvq3FkVZYwm9X2LVKYm".toCharArray()), - sameInstance(Hasher.BCRYPT6)); - assertThat(Hasher.resolveFromHash("$2a$07$Satxnu2fCvwYXpHIk8A2sO2uwROrsV7WrNiRJPq1oXEl5lc9FE.7S".toCharArray()), - sameInstance(Hasher.BCRYPT7)); - assertThat(Hasher.resolveFromHash("$2a$08$LLfkTt2C9TUl5sDtgqmE3uRw9nHt748d3eMSGfbFYgQQQhjbXHFo2".toCharArray()), - sameInstance(Hasher.BCRYPT8)); - assertThat(Hasher.resolveFromHash("$2a$09$.VCWA3yFVdd6gfI526TUrufb4TvxMuhW0jIuMfhd4/fy1Ak/zrSFe".toCharArray()), - sameInstance(Hasher.BCRYPT9)); - assertThat(Hasher.resolveFromHash("$2a$10$OEiXFrUUY02Nm7YsEgzFuuJ3yO3HAYzJUU7omseluy28s7FYaictu".toCharArray()), - sameInstance(Hasher.BCRYPT)); - assertThat(Hasher.resolveFromHash("$2a$11$Ya53LCozFlKABu05xsAbj.9xmrczyuAY/fTvxKkDiHOJc5GYcaNRy".toCharArray()), - sameInstance(Hasher.BCRYPT11)); - assertThat(Hasher.resolveFromHash("$2a$12$oUW2hiWBHYwbJamWi6YDPeKS2NBCvD4GR50zh9QZCcgssNFcbpg/a".toCharArray()), - sameInstance(Hasher.BCRYPT12)); - assertThat(Hasher.resolveFromHash("$2a$13$0PDx6mxKK4bLSgpc5H6eaeylWub7UFghjxV03lFYSz4WS4slDT30q".toCharArray()), - sameInstance(Hasher.BCRYPT13)); - assertThat(Hasher.resolveFromHash("$2a$14$lFyXmX7p9/FHr7W4nxTnfuCkjAoBHv6awQlv8jlKZ/YCMI65i38e6".toCharArray()), - sameInstance(Hasher.BCRYPT14)); - assertThat(Hasher.resolveFromHash( - "{PBKDF2}1000$oNl3JWiDZhXqhrpk9Kl+T0tKpVNNV3UHNxENPePpo2M=$g9lERDX5op20eX534bHdQy7ySRwobxwtaxxsz3AYPIU=".toCharArray()), - sameInstance(Hasher.PBKDF2_1000)); - assertThat(Hasher.resolveFromHash( - "{PBKDF2}10000$UrwrHBY4GA1na9KxRpoFkUiICTeZe+mMZCZOg6bRSLc=$1Wl32wRQ9Q3Sv1IFoNwgSrUa5YifLv0MoxAO6leyip8=".toCharArray()), - sameInstance(Hasher.PBKDF2)); - assertThat(Hasher.resolveFromHash( - "{PBKDF2}50000$mxa5m9AlgtKLUXKi/pE5+4w7ZexGSOtlUHD043NHVdc=$LE5Ncph672M8PtugfRgk2k3ue9qY2cKgiguuAd+e3I0=".toCharArray()), - sameInstance(Hasher.PBKDF2_50000)); - assertThat(Hasher.resolveFromHash( - "{PBKDF2}100000$qFs8H0FjietnI7sgr/1Av4H+Z7d/9dehfZ2ptU474jk=$OFj40Ha0XcHWUXSspRx6EeXnTcuN0Nva2/i2c/hvnZE=".toCharArray()), - sameInstance(Hasher.PBKDF2_100000)); - assertThat(Hasher.resolveFromHash( - "{PBKDF2}500000$wyttuDlppd5KYD35uDZN6vudB50Cjshm5efZhOxZZQI=$ADZpOHY6llJZsjupZCn6s4Eocg0dKKdBiNjDBYqhlzA=".toCharArray()), - sameInstance(Hasher.PBKDF2_500000)); - assertThat(Hasher.resolveFromHash( - "{PBKDF2}1000000$UuyhtjDEzWmE2wyY80akZKPWWpy2r2X50so41YML82U=$WFasYLelqbjQwt3EqFlUcwHiC38EZC45Iu/Iz0xL1GQ=".toCharArray()), - sameInstance(Hasher.PBKDF2_1000000)); - assertThat(Hasher.resolveFromHash( - "{PBKDF2_STRETCH}1000$sTyix9e0zNINzq2aDZ+GD5+QlO94xVyf/bv4pWNhBxo=$4KuzGPy9HXnhY3ANHn8rcIRQuJHPB6cEtLwnOhDI5d4=" - .toCharArray()), - sameInstance(Hasher.PBKDF2_STRETCH_1000)); - assertThat(Hasher.resolveFromHash( - "{PBKDF2_STRETCH}10000$8M9+Ww0xkdY250CROEutsd8UP6CrJESw7ZAFu1NGORo=$ai0gxBPtHTfZU/nbNGwL5zjC+eo2/ANQM17L/tllVeo=" - .toCharArray()), - sameInstance(Hasher.PBKDF2_STRETCH)); - assertThat(Hasher.resolveFromHash( - "{PBKDF2_STRETCH}50000$uupwXiq8W0+jrLtC3/aqzuvyZlRarlmx1+CQGEnomlk=$by8q/+oRPPWwDE6an7B9/ndz7UZ1UQpaGY4CGurtPTI=" - .toCharArray()), - sameInstance(Hasher.PBKDF2_STRETCH_50000)); - assertThat(Hasher.resolveFromHash( - "{PBKDF2_STRETCH}100000$E9VqtV76PcrQuCZ6wOMMNvs4CMPcANTpzRw8Wjd24PU=$j56uKUvwbvmgQgNFkbV7SRQVZ2QOarokAgBeA8xcFD8=" - .toCharArray()), - sameInstance(Hasher.PBKDF2_STRETCH_100000)); - assertThat(Hasher.resolveFromHash( - "{PBKDF2_STRETCH}500000$4dpTEbu4jfjhDOjWY6xdsnxuQs4dg4QbNzZJ0Z1Tm4s=$Us/yrlCxVaW7mz0go1qIygFqGgcfUMgCZfIl2AvI4I8=" - .toCharArray()), - sameInstance(Hasher.PBKDF2_STRETCH_500000)); - assertThat(Hasher.resolveFromHash( - "{PBKDF2_STRETCH}1000000$eKeQvMztiIcqBynTNDFBseOBww3GBpHDZI6EPPVHYUw=$4587yrxUa02RZ1jeW1WOaMjRn5qT9iQ5/DIHk0nW2bE=" - .toCharArray()), - sameInstance(Hasher.PBKDF2_STRETCH_1000000)); + assertThat( + Hasher.resolveFromHash("$2a$10$1oZj.8KmlwiCy4DWKvDH3OU0Ko4WRF4FknyvCh3j/ZtaRCNYA6Xzm".toCharArray()), + sameInstance(Hasher.BCRYPT) + ); + assertThat( + Hasher.resolveFromHash("$2a$04$GwJtIQiGMHASEYphMiCpjeZh1cDyYC5U.DKfNKa4i/y0IbOvc2LiG".toCharArray()), + sameInstance(Hasher.BCRYPT4) + ); + assertThat( + Hasher.resolveFromHash("$2a$05$xLmwSB7Nw7PcqP.6hXdc4eUZbT.4.iAZ3CTPzSaUibrrYjC6Vwq1m".toCharArray()), + sameInstance(Hasher.BCRYPT5) + ); + assertThat( + Hasher.resolveFromHash("$2a$06$WQX1MALAjVOhR2YKmLcHYed2oROzBl3OZPtvq3FkVZYwm9X2LVKYm".toCharArray()), + sameInstance(Hasher.BCRYPT6) + ); + assertThat( + Hasher.resolveFromHash("$2a$07$Satxnu2fCvwYXpHIk8A2sO2uwROrsV7WrNiRJPq1oXEl5lc9FE.7S".toCharArray()), + sameInstance(Hasher.BCRYPT7) + ); + assertThat( + Hasher.resolveFromHash("$2a$08$LLfkTt2C9TUl5sDtgqmE3uRw9nHt748d3eMSGfbFYgQQQhjbXHFo2".toCharArray()), + sameInstance(Hasher.BCRYPT8) + ); + assertThat( + Hasher.resolveFromHash("$2a$09$.VCWA3yFVdd6gfI526TUrufb4TvxMuhW0jIuMfhd4/fy1Ak/zrSFe".toCharArray()), + sameInstance(Hasher.BCRYPT9) + ); + assertThat( + Hasher.resolveFromHash("$2a$10$OEiXFrUUY02Nm7YsEgzFuuJ3yO3HAYzJUU7omseluy28s7FYaictu".toCharArray()), + sameInstance(Hasher.BCRYPT) + ); + assertThat( + Hasher.resolveFromHash("$2a$11$Ya53LCozFlKABu05xsAbj.9xmrczyuAY/fTvxKkDiHOJc5GYcaNRy".toCharArray()), + sameInstance(Hasher.BCRYPT11) + ); + assertThat( + Hasher.resolveFromHash("$2a$12$oUW2hiWBHYwbJamWi6YDPeKS2NBCvD4GR50zh9QZCcgssNFcbpg/a".toCharArray()), + sameInstance(Hasher.BCRYPT12) + ); + assertThat( + Hasher.resolveFromHash("$2a$13$0PDx6mxKK4bLSgpc5H6eaeylWub7UFghjxV03lFYSz4WS4slDT30q".toCharArray()), + sameInstance(Hasher.BCRYPT13) + ); + assertThat( + Hasher.resolveFromHash("$2a$14$lFyXmX7p9/FHr7W4nxTnfuCkjAoBHv6awQlv8jlKZ/YCMI65i38e6".toCharArray()), + sameInstance(Hasher.BCRYPT14) + ); + assertThat( + Hasher.resolveFromHash( + "{PBKDF2}1000$oNl3JWiDZhXqhrpk9Kl+T0tKpVNNV3UHNxENPePpo2M=$g9lERDX5op20eX534bHdQy7ySRwobxwtaxxsz3AYPIU=".toCharArray() + ), + sameInstance(Hasher.PBKDF2_1000) + ); + assertThat( + Hasher.resolveFromHash( + "{PBKDF2}10000$UrwrHBY4GA1na9KxRpoFkUiICTeZe+mMZCZOg6bRSLc=$1Wl32wRQ9Q3Sv1IFoNwgSrUa5YifLv0MoxAO6leyip8=".toCharArray() + ), + sameInstance(Hasher.PBKDF2) + ); + assertThat( + Hasher.resolveFromHash( + "{PBKDF2}50000$mxa5m9AlgtKLUXKi/pE5+4w7ZexGSOtlUHD043NHVdc=$LE5Ncph672M8PtugfRgk2k3ue9qY2cKgiguuAd+e3I0=".toCharArray() + ), + sameInstance(Hasher.PBKDF2_50000) + ); + assertThat( + Hasher.resolveFromHash( + "{PBKDF2}100000$qFs8H0FjietnI7sgr/1Av4H+Z7d/9dehfZ2ptU474jk=$OFj40Ha0XcHWUXSspRx6EeXnTcuN0Nva2/i2c/hvnZE=".toCharArray() + ), + sameInstance(Hasher.PBKDF2_100000) + ); + assertThat( + Hasher.resolveFromHash( + "{PBKDF2}500000$wyttuDlppd5KYD35uDZN6vudB50Cjshm5efZhOxZZQI=$ADZpOHY6llJZsjupZCn6s4Eocg0dKKdBiNjDBYqhlzA=".toCharArray() + ), + sameInstance(Hasher.PBKDF2_500000) + ); + assertThat( + Hasher.resolveFromHash( + "{PBKDF2}1000000$UuyhtjDEzWmE2wyY80akZKPWWpy2r2X50so41YML82U=$WFasYLelqbjQwt3EqFlUcwHiC38EZC45Iu/Iz0xL1GQ=".toCharArray() + ), + sameInstance(Hasher.PBKDF2_1000000) + ); + assertThat( + Hasher.resolveFromHash( + "{PBKDF2_STRETCH}1000$sTyix9e0zNINzq2aDZ+GD5+QlO94xVyf/bv4pWNhBxo=$4KuzGPy9HXnhY3ANHn8rcIRQuJHPB6cEtLwnOhDI5d4=" + .toCharArray() + ), + sameInstance(Hasher.PBKDF2_STRETCH_1000) + ); + assertThat( + Hasher.resolveFromHash( + "{PBKDF2_STRETCH}10000$8M9+Ww0xkdY250CROEutsd8UP6CrJESw7ZAFu1NGORo=$ai0gxBPtHTfZU/nbNGwL5zjC+eo2/ANQM17L/tllVeo=" + .toCharArray() + ), + sameInstance(Hasher.PBKDF2_STRETCH) + ); + assertThat( + Hasher.resolveFromHash( + "{PBKDF2_STRETCH}50000$uupwXiq8W0+jrLtC3/aqzuvyZlRarlmx1+CQGEnomlk=$by8q/+oRPPWwDE6an7B9/ndz7UZ1UQpaGY4CGurtPTI=" + .toCharArray() + ), + sameInstance(Hasher.PBKDF2_STRETCH_50000) + ); + assertThat( + Hasher.resolveFromHash( + "{PBKDF2_STRETCH}100000$E9VqtV76PcrQuCZ6wOMMNvs4CMPcANTpzRw8Wjd24PU=$j56uKUvwbvmgQgNFkbV7SRQVZ2QOarokAgBeA8xcFD8=" + .toCharArray() + ), + sameInstance(Hasher.PBKDF2_STRETCH_100000) + ); + assertThat( + Hasher.resolveFromHash( + "{PBKDF2_STRETCH}500000$4dpTEbu4jfjhDOjWY6xdsnxuQs4dg4QbNzZJ0Z1Tm4s=$Us/yrlCxVaW7mz0go1qIygFqGgcfUMgCZfIl2AvI4I8=" + .toCharArray() + ), + sameInstance(Hasher.PBKDF2_STRETCH_500000) + ); + assertThat( + Hasher.resolveFromHash( + "{PBKDF2_STRETCH}1000000$eKeQvMztiIcqBynTNDFBseOBww3GBpHDZI6EPPVHYUw=$4587yrxUa02RZ1jeW1WOaMjRn5qT9iQ5/DIHk0nW2bE=" + .toCharArray() + ), + sameInstance(Hasher.PBKDF2_STRETCH_1000000) + ); assertThat(Hasher.resolveFromHash("notavalidhashformat".toCharArray()), sameInstance(Hasher.NOOP)); } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/RealmUserLookupTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/RealmUserLookupTests.java index 39f3a98b94d82..52afe4a7a9106 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/RealmUserLookupTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/RealmUserLookupTests.java @@ -9,9 +9,9 @@ import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.support.PlainActionFuture; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.Tuple; import org.elasticsearch.env.Environment; import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.test.ESTestCase; @@ -41,9 +41,7 @@ public class RealmUserLookupTests extends ESTestCase { @Before public void setup() { - globalSettings = Settings.builder() - .put("path.home", createTempDir()) - .build(); + globalSettings = Settings.builder().put("path.home", createTempDir()).build(); env = TestEnvironment.newEnvironment(globalSettings); threadContext = new ThreadContext(globalSettings); } @@ -88,10 +86,17 @@ public void testUserNotFound() throws Exception { public void testRealmException() { RealmIdentifier realmIdentifier = new RealmIdentifier("test", "test"); - final Realm realm = new Realm(new RealmConfig(realmIdentifier, - Settings.builder().put(globalSettings) - .put(RealmSettings.getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0).build(), - env, threadContext)) { + final Realm realm = new Realm( + new RealmConfig( + realmIdentifier, + Settings.builder() + .put(globalSettings) + .put(RealmSettings.getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0) + .build(), + env, + threadContext + ) + ) { @Override public boolean supports(AuthenticationToken token) { return false; @@ -123,11 +128,15 @@ private List buildRealms(int realmCount) { final List realms = new ArrayList<>(realmCount); for (int i = 1; i <= realmCount; i++) { RealmIdentifier realmIdentifier = new RealmIdentifier("mock", "lookup-" + i); - final RealmConfig config = new RealmConfig(realmIdentifier, - Settings.builder().put(globalSettings) - .put(RealmSettings.getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0).build(), + final RealmConfig config = new RealmConfig( + realmIdentifier, + Settings.builder() + .put(globalSettings) + .put(RealmSettings.getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0) + .build(), env, - threadContext); + threadContext + ); final MockLookupRealm realm = new MockLookupRealm(config); for (int j = 0; j < 5; j++) { realm.registerUser(new User(randomAlphaOfLengthBetween(6, 12))); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/RoleMappingFileBootstrapCheckTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/RoleMappingFileBootstrapCheckTests.java index 51f2aeee5425e..b827bf4a2e870 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/RoleMappingFileBootstrapCheckTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/RoleMappingFileBootstrapCheckTests.java @@ -30,24 +30,20 @@ public class RoleMappingFileBootstrapCheckTests extends AbstractBootstrapCheckTe private static final RealmConfig.RealmIdentifier REALM_ID = new RealmConfig.RealmIdentifier("ldap", "ldap-realm-name"); private static final String ROLE_MAPPING_FILE_SETTING = RealmSettings.getFullSettingKey( - REALM_ID, DnRoleMapperSettings.ROLE_MAPPING_FILE_SETTING); + REALM_ID, + DnRoleMapperSettings.ROLE_MAPPING_FILE_SETTING + ); protected Settings settings; @Before public void init() throws IOException { - settings = Settings.builder() - .put("resource.reload.interval.high", "100ms") - .put("path.home", createTempDir()) - .build(); + settings = Settings.builder().put("resource.reload.interval.high", "100ms").put("path.home", createTempDir()).build(); } public void testBootstrapCheckOfValidFile() { Path file = getDataPath("role_mapping.yml"); - Settings ldapSettings = Settings.builder() - .put(settings) - .put(ROLE_MAPPING_FILE_SETTING, file.toAbsolutePath()) - .build(); + Settings ldapSettings = Settings.builder().put(settings).put(ROLE_MAPPING_FILE_SETTING, file.toAbsolutePath()).build(); RealmConfig config = getRealmConfig(ldapSettings); final BootstrapCheck check = RoleMappingFileBootstrapCheck.create(config); assertThat(check, notNullValue()); @@ -56,18 +52,18 @@ public void testBootstrapCheckOfValidFile() { } private static RealmConfig getRealmConfig(Settings settings) { - return new RealmConfig(REALM_ID, + return new RealmConfig( + REALM_ID, Settings.builder().put(settings).put(RealmSettings.getFullSettingKey(REALM_ID, RealmSettings.ORDER_SETTING), 0).build(), - TestEnvironment.newEnvironment(settings), new ThreadContext(Settings.EMPTY)); + TestEnvironment.newEnvironment(settings), + new ThreadContext(Settings.EMPTY) + ); } public void testBootstrapCheckOfMissingFile() { final String fileName = randomAlphaOfLength(10); Path file = createTempDir().resolve(fileName); - Settings ldapSettings = Settings.builder() - .put(settings) - .put(ROLE_MAPPING_FILE_SETTING, file.toAbsolutePath()) - .build(); + Settings ldapSettings = Settings.builder().put(settings).put(ROLE_MAPPING_FILE_SETTING, file.toAbsolutePath()).build(); RealmConfig config = getRealmConfig(ldapSettings); final BootstrapCheck check = RoleMappingFileBootstrapCheck.create(config); assertThat(check, notNullValue()); @@ -84,10 +80,7 @@ public void testBootstrapCheckWithInvalidYaml() throws IOException { // writing in utf_16 should cause a parsing error as we try to read the file in utf_8 Files.write(file, Collections.singletonList("junk"), StandardCharsets.UTF_16); - Settings ldapSettings = Settings.builder() - .put(settings) - .put(ROLE_MAPPING_FILE_SETTING, file.toAbsolutePath()) - .build(); + Settings ldapSettings = Settings.builder().put(settings).put(ROLE_MAPPING_FILE_SETTING, file.toAbsolutePath()).build(); RealmConfig config = getRealmConfig(ldapSettings); final BootstrapCheck check = RoleMappingFileBootstrapCheck.create(config); assertThat(check, notNullValue()); @@ -104,10 +97,7 @@ public void testBootstrapCheckWithInvalidDn() throws IOException { // A DN must have at least 1 '=' symbol Files.write(file, Collections.singletonList("role: not-a-dn")); - Settings ldapSettings = Settings.builder() - .put(settings) - .put(ROLE_MAPPING_FILE_SETTING, file.toAbsolutePath()) - .build(); + Settings ldapSettings = Settings.builder().put(settings).put(ROLE_MAPPING_FILE_SETTING, file.toAbsolutePath()).build(); RealmConfig config = getRealmConfig(ldapSettings); final BootstrapCheck check = RoleMappingFileBootstrapCheck.create(config); assertThat(check, notNullValue()); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/SecondaryAuthenticatorTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/SecondaryAuthenticatorTests.java index 879d866efbe04..2a88f652bc7c7 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/SecondaryAuthenticatorTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/SecondaryAuthenticatorTests.java @@ -126,8 +126,15 @@ public void setupMocks() throws Exception { securityContext = new SecurityContext(settings, threadContext); tokenService = new TokenService(settings, clock, client, licenseState, securityContext, securityIndex, tokensIndex, clusterService); - final ApiKeyService apiKeyService = new ApiKeyService(settings, clock, client, securityIndex, clusterService, - mock(CacheInvalidatorRegistry.class),threadPool); + final ApiKeyService apiKeyService = new ApiKeyService( + settings, + clock, + client, + securityIndex, + clusterService, + mock(CacheInvalidatorRegistry.class), + threadPool + ); final ServiceAccountService serviceAccountService = mock(ServiceAccountService.class); doAnswer(invocationOnMock -> { @SuppressWarnings("unchecked") @@ -135,8 +142,18 @@ public void setupMocks() throws Exception { listener.onResponse(null); return null; }).when(serviceAccountService).authenticateToken(any(), any(), any()); - authenticationService = new AuthenticationService(settings, realms, auditTrail, failureHandler, threadPool, anonymous, - tokenService, apiKeyService, serviceAccountService, OperatorPrivileges.NOOP_OPERATOR_PRIVILEGES_SERVICE); + authenticationService = new AuthenticationService( + settings, + realms, + auditTrail, + failureHandler, + threadPool, + anonymous, + tokenService, + apiKeyService, + serviceAccountService, + OperatorPrivileges.NOOP_OPERATOR_PRIVILEGES_SERVICE + ); authenticator = new SecondaryAuthenticator(securityContext, authenticationService); } @@ -168,8 +185,10 @@ public void testAuthenticateTransportRequestFailsIfHeaderHasUnrecognizedCredenti final PlainActionFuture future = new PlainActionFuture<>(); authenticator.authenticate(AuthenticateAction.NAME, request, future); - final ElasticsearchSecurityException ex = expectThrows(ElasticsearchSecurityException.class, - () -> future.actionGet(0, TimeUnit.MILLISECONDS)); + final ElasticsearchSecurityException ex = expectThrows( + ElasticsearchSecurityException.class, + () -> future.actionGet(0, TimeUnit.MILLISECONDS) + ); assertThat(ex, TestMatchers.throwableWithMessage(Matchers.containsString("secondary user"))); assertThat(ex.getCause(), TestMatchers.throwableWithMessage(Matchers.containsString("credentials"))); } @@ -180,8 +199,10 @@ public void testAuthenticateRestRequestFailsIfHeaderHasUnrecognizedCredentials() final PlainActionFuture future = new PlainActionFuture<>(); authenticator.authenticateAndAttachToContext(request, future); - final ElasticsearchSecurityException ex = expectThrows(ElasticsearchSecurityException.class, - () -> future.actionGet(0, TimeUnit.MILLISECONDS)); + final ElasticsearchSecurityException ex = expectThrows( + ElasticsearchSecurityException.class, + () -> future.actionGet(0, TimeUnit.MILLISECONDS) + ); assertThat(ex, TestMatchers.throwableWithMessage(Matchers.containsString("secondary user"))); assertThat(ex.getCause(), TestMatchers.throwableWithMessage(Matchers.containsString("credentials"))); @@ -209,18 +230,18 @@ private SecondaryAuthentication assertAuthenticateWithBasicAuthentication(Consum final SecureString password = new SecureString(randomAlphaOfLengthBetween(8, 24).toCharArray()); realm.defineUser(user, password); - threadPool.getThreadContext().putHeader(SECONDARY_AUTH_HEADER_NAME, "Basic " + - Base64.getEncoder().encodeToString((user + ":" + password).getBytes(StandardCharsets.UTF_8))); + threadPool.getThreadContext() + .putHeader( + SECONDARY_AUTH_HEADER_NAME, + "Basic " + Base64.getEncoder().encodeToString((user + ":" + password).getBytes(StandardCharsets.UTF_8)) + ); final PlainActionFuture future = new PlainActionFuture<>(); final AtomicReference listenerContext = new AtomicReference<>(); - consumer.accept(ActionListener.wrap( - result -> { - listenerContext.set(securityContext.getThreadContext().newStoredContext(false)); - future.onResponse(result); - }, - e -> future.onFailure(e) - )); + consumer.accept(ActionListener.wrap(result -> { + listenerContext.set(securityContext.getThreadContext().newStoredContext(false)); + future.onResponse(result); + }, e -> future.onFailure(e))); final SecondaryAuthentication secondaryAuthentication = future.get(0, TimeUnit.MILLISECONDS); assertThat(secondaryAuthentication, Matchers.notNullValue()); @@ -252,21 +273,23 @@ private void assertAuthenticateWithIncorrectPassword(Consumer future = new PlainActionFuture<>(); final AtomicReference listenerContext = new AtomicReference<>(); - consumer.accept(ActionListener.wrap( - future::onResponse, - e -> { - listenerContext.set(securityContext.getThreadContext().newStoredContext(false)); - future.onFailure(e); - } - )); + consumer.accept(ActionListener.wrap(future::onResponse, e -> { + listenerContext.set(securityContext.getThreadContext().newStoredContext(false)); + future.onFailure(e); + })); - final ElasticsearchSecurityException ex = expectThrows(ElasticsearchSecurityException.class, - () -> future.actionGet(0, TimeUnit.MILLISECONDS)); + final ElasticsearchSecurityException ex = expectThrows( + ElasticsearchSecurityException.class, + () -> future.actionGet(0, TimeUnit.MILLISECONDS) + ); assertThat(ex, TestMatchers.throwableWithMessage(Matchers.containsString("secondary user"))); assertThat(ex.getCause(), TestMatchers.throwableWithMessage(Matchers.containsString(user))); @@ -276,9 +299,11 @@ private void assertAuthenticateWithIncorrectPassword(Consumer tokenDocId = new AtomicReference<>(); final AtomicReference tokenSource = new AtomicReference<>(); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/TokensInvalidationResultTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/TokensInvalidationResultTests.java index f7a4b45a1771e..eab7e0ca40470 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/TokensInvalidationResultTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/TokensInvalidationResultTests.java @@ -9,11 +9,11 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.Strings; +import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.rest.RestStatus; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.security.authc.support.TokensInvalidationResult; import java.util.Arrays; @@ -23,51 +23,60 @@ public class TokensInvalidationResultTests extends ESTestCase { - public void testToXcontent() throws Exception{ - TokensInvalidationResult result = new TokensInvalidationResult(Arrays.asList("token1", "token2"), + public void testToXcontent() throws Exception { + TokensInvalidationResult result = new TokensInvalidationResult( + Arrays.asList("token1", "token2"), Arrays.asList("token3", "token4"), - Arrays.asList(new ElasticsearchException("foo", new IllegalStateException("bar")), - new ElasticsearchException("boo", new IllegalStateException("far"))), - RestStatus.OK); + Arrays.asList( + new ElasticsearchException("foo", new IllegalStateException("bar")), + new ElasticsearchException("boo", new IllegalStateException("far")) + ), + RestStatus.OK + ); try (XContentBuilder builder = JsonXContent.contentBuilder()) { result.toXContent(builder, ToXContent.EMPTY_PARAMS); - assertThat(Strings.toString(builder), + assertThat( + Strings.toString(builder), equalTo( - "{\"invalidated_tokens\":2," + - "\"previously_invalidated_tokens\":2," + - "\"error_count\":2," + - "\"error_details\":[" + - "{\"type\":\"exception\"," + - "\"reason\":\"foo\"," + - "\"caused_by\":{" + - "\"type\":\"illegal_state_exception\"," + - "\"reason\":\"bar\"" + - "}" + - "}," + - "{\"type\":\"exception\"," + - "\"reason\":\"boo\"," + - "\"caused_by\":{" + - "\"type\":\"illegal_state_exception\"," + - "\"reason\":\"far\"" + - "}" + - "}" + - "]" + - "}")); + "{\"invalidated_tokens\":2," + + "\"previously_invalidated_tokens\":2," + + "\"error_count\":2," + + "\"error_details\":[" + + "{\"type\":\"exception\"," + + "\"reason\":\"foo\"," + + "\"caused_by\":{" + + "\"type\":\"illegal_state_exception\"," + + "\"reason\":\"bar\"" + + "}" + + "}," + + "{\"type\":\"exception\"," + + "\"reason\":\"boo\"," + + "\"caused_by\":{" + + "\"type\":\"illegal_state_exception\"," + + "\"reason\":\"far\"" + + "}" + + "}" + + "]" + + "}" + ) + ); } } - public void testToXcontentWithNoErrors() throws Exception{ - TokensInvalidationResult result = new TokensInvalidationResult(Arrays.asList("token1", "token2"), Collections.emptyList(), - Collections.emptyList(), RestStatus.OK); + public void testToXcontentWithNoErrors() throws Exception { + TokensInvalidationResult result = new TokensInvalidationResult( + Arrays.asList("token1", "token2"), + Collections.emptyList(), + Collections.emptyList(), + RestStatus.OK + ); try (XContentBuilder builder = JsonXContent.contentBuilder()) { result.toXContent(builder, ToXContent.EMPTY_PARAMS); - assertThat(Strings.toString(builder), - equalTo( - "{\"invalidated_tokens\":2," + - "\"previously_invalidated_tokens\":0," + - "\"error_count\":0" + - "}")); + assertThat( + Strings.toString(builder), + equalTo("{\"invalidated_tokens\":2," + "\"previously_invalidated_tokens\":0," + "\"error_count\":0" + "}") + ); } } } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/UsernamePasswordTokenTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/UsernamePasswordTokenTests.java index 3f2568adf0cd2..0eeda6db8030a 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/UsernamePasswordTokenTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/UsernamePasswordTokenTests.java @@ -46,8 +46,8 @@ public void testPutToken() throws Exception { public void testExtractToken() throws Exception { ThreadContext threadContext = new ThreadContext(Settings.EMPTY); - final String header = randomFrom("Basic ", "basic ", "BASIC ") - + Base64.getEncoder().encodeToString("user1:test123".getBytes(StandardCharsets.UTF_8)); + final String header = randomFrom("Basic ", "basic ", "BASIC ") + Base64.getEncoder() + .encodeToString("user1:test123".getBytes(StandardCharsets.UTF_8)); threadContext.putHeader(UsernamePasswordToken.BASIC_AUTH_HEADER, header); UsernamePasswordToken token = UsernamePasswordToken.extractToken(threadContext); assertThat(token, notNullValue()); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/mapper/ExpressionRoleMappingTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/mapper/ExpressionRoleMappingTests.java index 47e4611fa9403..a4e351ab24bad 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/mapper/ExpressionRoleMappingTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/mapper/ExpressionRoleMappingTests.java @@ -18,24 +18,24 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.env.Environment; +import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.VersionUtils; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.env.Environment; -import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.test.VersionUtils; import org.elasticsearch.xpack.core.XPackClientPlugin; import org.elasticsearch.xpack.core.security.authc.RealmConfig; import org.elasticsearch.xpack.core.security.authc.RealmSettings; +import org.elasticsearch.xpack.core.security.authc.support.UserRoleMapper; import org.elasticsearch.xpack.core.security.authc.support.mapper.ExpressionRoleMapping; import org.elasticsearch.xpack.core.security.authc.support.mapper.TemplateRoleName; import org.elasticsearch.xpack.core.security.authc.support.mapper.expressiondsl.AllExpression; import org.elasticsearch.xpack.core.security.authc.support.mapper.expressiondsl.AnyExpression; import org.elasticsearch.xpack.core.security.authc.support.mapper.expressiondsl.FieldExpression; -import org.elasticsearch.xpack.core.security.authc.support.UserRoleMapper; import org.hamcrest.Matchers; import org.junit.Before; import org.mockito.Mockito; @@ -63,7 +63,9 @@ public void setupMapping() throws Exception { realm = new RealmConfig( realmIdentifier, Settings.builder().put(RealmSettings.getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0).build(), - Mockito.mock(Environment.class), new ThreadContext(Settings.EMPTY)); + Mockito.mock(Environment.class), + new ThreadContext(Settings.EMPTY) + ); } public void testValidExpressionWithFixedRoleNames() throws Exception { @@ -81,31 +83,50 @@ public void testValidExpressionWithFixedRoleNames() throws Exception { assertThat(mapping.getExpression(), instanceOf(AllExpression.class)); final UserRoleMapper.UserData user1a = new UserRoleMapper.UserData( - "john.smith", "cn=john.smith,ou=sales,dc=example,dc=com", - List.of(), Map.of("active", true), realm + "john.smith", + "cn=john.smith,ou=sales,dc=example,dc=com", + List.of(), + Map.of("active", true), + realm ); final UserRoleMapper.UserData user1b = new UserRoleMapper.UserData( - user1a.getUsername(), user1a.getDn().toUpperCase(Locale.US), user1a.getGroups(), user1a.getMetadata(), user1a.getRealm() + user1a.getUsername(), + user1a.getDn().toUpperCase(Locale.US), + user1a.getGroups(), + user1a.getMetadata(), + user1a.getRealm() ); final UserRoleMapper.UserData user1c = new UserRoleMapper.UserData( - user1a.getUsername(), user1a.getDn().replaceAll(",", ", "), user1a.getGroups(), user1a.getMetadata(), user1a.getRealm() + user1a.getUsername(), + user1a.getDn().replaceAll(",", ", "), + user1a.getGroups(), + user1a.getMetadata(), + user1a.getRealm() ); final UserRoleMapper.UserData user1d = new UserRoleMapper.UserData( - user1a.getUsername(), user1a.getDn().replaceAll("dc=", "DC="), user1a.getGroups(), user1a.getMetadata(), user1a.getRealm() + user1a.getUsername(), + user1a.getDn().replaceAll("dc=", "DC="), + user1a.getGroups(), + user1a.getMetadata(), + user1a.getRealm() ); final UserRoleMapper.UserData user2 = new UserRoleMapper.UserData( - "jamie.perez", "cn=jamie.perez,ou=sales,dc=example,dc=com", - List.of(), Map.of("active", false), realm + "jamie.perez", + "cn=jamie.perez,ou=sales,dc=example,dc=com", + List.of(), + Map.of("active", false), + realm ); final UserRoleMapper.UserData user3 = new UserRoleMapper.UserData( - "simone.ng", "cn=simone.ng,ou=finance,dc=example,dc=com", - List.of(), Map.of("active", true), realm + "simone.ng", + "cn=simone.ng,ou=finance,dc=example,dc=com", + List.of(), + Map.of("active", true), + realm ); - final UserRoleMapper.UserData user4 = new UserRoleMapper.UserData( - "peter.null", null, List.of(), Map.of("active", true), realm - ); + final UserRoleMapper.UserData user4 = new UserRoleMapper.UserData("peter.null", null, List.of(), Map.of("active", true), realm); assertThat(mapping.getExpression().match(user1a.asModel()), equalTo(true)); assertThat(mapping.getExpression().match(user1b.asModel()), equalTo(true)); @@ -117,26 +138,38 @@ public void testValidExpressionWithFixedRoleNames() throws Exception { // expression without dn json = "{" - + "\"roles\": [ \"superuser\", \"system_admin\", \"admin\" ], " - + "\"enabled\": true, " - + "\"rules\": { " - + " \"any\": [ " - + " { \"field\": { \"username\" : \"tony.stark\" } }, " - + " { \"field\": { \"groups\": \"cn=admins,dc=stark-enterprises,dc=com\" } }" - + " ]}" - + "}"; + + "\"roles\": [ \"superuser\", \"system_admin\", \"admin\" ], " + + "\"enabled\": true, " + + "\"rules\": { " + + " \"any\": [ " + + " { \"field\": { \"username\" : \"tony.stark\" } }, " + + " { \"field\": { \"groups\": \"cn=admins,dc=stark-enterprises,dc=com\" } }" + + " ]}" + + "}"; mapping = parse(json, "stark_admin"); - assertThat(mapping.getRoles(), Matchers.containsInAnyOrder("superuser", "system_admin", "admin")); - assertThat(mapping.getExpression(), instanceOf(AnyExpression.class)); + assertThat(mapping.getRoles(), Matchers.containsInAnyOrder("superuser", "system_admin", "admin")); + assertThat(mapping.getExpression(), instanceOf(AnyExpression.class)); final UserRoleMapper.UserData userTony = new UserRoleMapper.UserData( - "tony.stark", null, List.of("Audi R8 owners"), Map.of("boss", true), realm + "tony.stark", + null, + List.of("Audi R8 owners"), + Map.of("boss", true), + realm ); final UserRoleMapper.UserData userPepper = new UserRoleMapper.UserData( - "pepper.potts", null, List.of("marvel", "cn=admins,dc=stark-enterprises,dc=com"), Map.of(), realm + "pepper.potts", + null, + List.of("marvel", "cn=admins,dc=stark-enterprises,dc=com"), + Map.of(), + realm ); final UserRoleMapper.UserData userMax = new UserRoleMapper.UserData( - "max.rockatansky", null, List.of("bronze"), Map.of("mad", true), realm + "max.rockatansky", + null, + List.of("bronze"), + Map.of("mad", true), + realm ); assertThat(mapping.getExpression().match(userTony.asModel()), equalTo(true)); assertThat(mapping.getExpression().match(userPepper.asModel()), equalTo(true)); @@ -168,10 +201,7 @@ public void testParseValidJsonWithTemplatedRoleNames() throws Exception { } public void testParsingFailsIfRulesAreMissing() throws Exception { - String json = "{" - + "\"roles\": [ \"kibana_user\", \"sales\" ], " - + "\"enabled\": true " - + "}"; + String json = "{" + "\"roles\": [ \"kibana_user\", \"sales\" ], " + "\"enabled\": true " + "}"; ParsingException ex = expectThrows(ParsingException.class, () -> parse(json, "bad_json")); assertThat(ex.getMessage(), containsString("rules")); } @@ -241,17 +271,20 @@ public void testToXContentWithRoleNames() throws Exception { assertThat(mapping.getRoles(), iterableWithSize(2)); final String xcontent = Strings.toString(mapping); - assertThat(xcontent, equalTo( - "{" - + "\"enabled\":true," - + "\"roles\":[" - + "\"kibana_user\"," - + "\"sales\"" - + "]," - + "\"rules\":{\"field\":{\"realm.name\":\"saml1\"}}," - + "\"metadata\":{}" - + "}" - )); + assertThat( + xcontent, + equalTo( + "{" + + "\"enabled\":true," + + "\"roles\":[" + + "\"kibana_user\"," + + "\"sales\"" + + "]," + + "\"rules\":{\"field\":{\"realm.name\":\"saml1\"}}," + + "\"metadata\":{}" + + "}" + ) + ); } public void testToXContentWithTemplates() throws Exception { @@ -268,18 +301,21 @@ public void testToXContentWithTemplates() throws Exception { assertThat(mapping.getRoleTemplates(), iterableWithSize(2)); final String xcontent = Strings.toString(mapping.toXContent(XContentFactory.jsonBuilder(), ToXContent.EMPTY_PARAMS, true)); - assertThat(xcontent, equalTo( - "{" - + "\"enabled\":false," - + "\"role_templates\":[" - + "{\"template\":\"{\\\"source\\\":\\\"_user_{{username}}\\\"}\",\"format\":\"string\"}," - + "{\"template\":\"{\\\"source\\\":\\\"{{#tojson}}groups{{/tojson}}\\\"}\",\"format\":\"json\"}" - + "]," - + "\"rules\":{\"field\":{\"realm.name\":\"saml1\"}}," - + "\"metadata\":{\"answer\":42}," - + "\"doc_type\":\"role-mapping\"" - + "}" - )); + assertThat( + xcontent, + equalTo( + "{" + + "\"enabled\":false," + + "\"role_templates\":[" + + "{\"template\":\"{\\\"source\\\":\\\"_user_{{username}}\\\"}\",\"format\":\"string\"}," + + "{\"template\":\"{\\\"source\\\":\\\"{{#tojson}}groups{{/tojson}}\\\"}\",\"format\":\"json\"}" + + "]," + + "\"rules\":{\"field\":{\"realm.name\":\"saml1\"}}," + + "\"metadata\":{\"answer\":42}," + + "\"doc_type\":\"role-mapping\"" + + "}" + ) + ); final ExpressionRoleMapping parsed = parse(xcontent, getTestName(), true); assertThat(parsed.getRoles(), iterableWithSize(0)); @@ -296,8 +332,10 @@ public void testSerialization() throws Exception { original.writeTo(output); final NamedWriteableRegistry registry = new NamedWriteableRegistry(new XPackClientPlugin(Settings.EMPTY).getNamedWriteables()); - StreamInput streamInput = new NamedWriteableAwareStreamInput(ByteBufferStreamInput.wrap(BytesReference.toBytes(output.bytes())), - registry); + StreamInput streamInput = new NamedWriteableAwareStreamInput( + ByteBufferStreamInput.wrap(BytesReference.toBytes(output.bytes())), + registry + ); streamInput.setVersion(version); final ExpressionRoleMapping serialized = new ExpressionRoleMapping(streamInput); assertEquals(original, serialized); @@ -312,8 +350,10 @@ public void testSerializationPreV71() throws Exception { original.writeTo(output); final NamedWriteableRegistry registry = new NamedWriteableRegistry(new XPackClientPlugin(Settings.EMPTY).getNamedWriteables()); - StreamInput streamInput = new NamedWriteableAwareStreamInput(ByteBufferStreamInput.wrap(BytesReference.toBytes(output.bytes())), - registry); + StreamInput streamInput = new NamedWriteableAwareStreamInput( + ByteBufferStreamInput.wrap(BytesReference.toBytes(output.bytes())), + registry + ); streamInput.setVersion(version); final ExpressionRoleMapping serialized = new ExpressionRoleMapping(streamInput); assertEquals(original, serialized); @@ -339,17 +379,27 @@ private ExpressionRoleMapping randomRoleMapping(boolean acceptRoleTemplates) { final List templates; if (useTemplate) { roles = Collections.emptyList(); - templates = Arrays.asList(randomArray(1, 5, TemplateRoleName[]::new, () -> - new TemplateRoleName(new BytesArray(randomAlphaOfLengthBetween(10, 25)), randomFrom(TemplateRoleName.Format.values())) - )); + templates = Arrays.asList( + randomArray( + 1, + 5, + TemplateRoleName[]::new, + () -> new TemplateRoleName( + new BytesArray(randomAlphaOfLengthBetween(10, 25)), + randomFrom(TemplateRoleName.Format.values()) + ) + ) + ); } else { roles = Arrays.asList(randomArray(1, 5, String[]::new, () -> randomAlphaOfLengthBetween(4, 12))); templates = Collections.emptyList(); } return new ExpressionRoleMapping( randomAlphaOfLengthBetween(3, 8), - new FieldExpression(randomAlphaOfLengthBetween(4, 12), - Collections.singletonList(new FieldExpression.FieldValue(randomInt(99)))), + new FieldExpression( + randomAlphaOfLengthBetween(4, 12), + Collections.singletonList(new FieldExpression.FieldValue(randomInt(99))) + ), roles, templates, Collections.singletonMap(randomAlphaOfLengthBetween(3, 12), randomIntBetween(30, 90)), diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/mapper/NativeRoleMappingStoreTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/mapper/NativeRoleMappingStoreTests.java index b4f57938cd1d4..dea8e63ad0c4e 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/mapper/NativeRoleMappingStoreTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/mapper/NativeRoleMappingStoreTests.java @@ -29,6 +29,7 @@ import org.elasticsearch.xpack.core.security.authc.AuthenticationResult; import org.elasticsearch.xpack.core.security.authc.RealmConfig; import org.elasticsearch.xpack.core.security.authc.RealmSettings; +import org.elasticsearch.xpack.core.security.authc.support.UserRoleMapper; import org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken; import org.elasticsearch.xpack.core.security.authc.support.mapper.ExpressionRoleMapping; import org.elasticsearch.xpack.core.security.authc.support.mapper.TemplateRoleName; @@ -37,7 +38,6 @@ import org.elasticsearch.xpack.core.security.index.RestrictedIndicesNames; import org.elasticsearch.xpack.core.security.user.User; import org.elasticsearch.xpack.security.authc.support.CachingUsernamePasswordRealm; -import org.elasticsearch.xpack.core.security.authc.support.UserRoleMapper; import org.elasticsearch.xpack.security.support.SecurityIndexManager; import org.hamcrest.Matchers; @@ -60,39 +60,70 @@ public class NativeRoleMappingStoreTests extends ESTestCase { private final String concreteSecurityIndexName = randomFrom( - RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_6, RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7); + RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_6, + RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7 + ); public void testResolveRoles() throws Exception { // Does match DN - final ExpressionRoleMapping mapping1 = new ExpressionRoleMapping("dept_h", - new FieldExpression("dn", Collections.singletonList(new FieldValue("*,ou=dept_h,o=forces,dc=gc,dc=ca"))), - Arrays.asList("dept_h", "defence"), Collections.emptyList(), Collections.emptyMap(), true); + final ExpressionRoleMapping mapping1 = new ExpressionRoleMapping( + "dept_h", + new FieldExpression("dn", Collections.singletonList(new FieldValue("*,ou=dept_h,o=forces,dc=gc,dc=ca"))), + Arrays.asList("dept_h", "defence"), + Collections.emptyList(), + Collections.emptyMap(), + true + ); // Does not match - user is not in this group - final ExpressionRoleMapping mapping2 = new ExpressionRoleMapping("admin", - new FieldExpression("groups", Collections.singletonList( - new FieldValue(randomiseDn("cn=esadmin,ou=groups,ou=dept_h,o=forces,dc=gc,dc=ca")))), - Arrays.asList("admin"), Collections.emptyList(), Collections.emptyMap(), true); + final ExpressionRoleMapping mapping2 = new ExpressionRoleMapping( + "admin", + new FieldExpression( + "groups", + Collections.singletonList(new FieldValue(randomiseDn("cn=esadmin,ou=groups,ou=dept_h,o=forces,dc=gc,dc=ca"))) + ), + Arrays.asList("admin"), + Collections.emptyList(), + Collections.emptyMap(), + true + ); // Does match - user is one of these groups - final ExpressionRoleMapping mapping3 = new ExpressionRoleMapping("flight", - new FieldExpression("groups", Arrays.asList( - new FieldValue(randomiseDn("cn=alphaflight,ou=groups,ou=dept_h,o=forces,dc=gc,dc=ca")), - new FieldValue(randomiseDn("cn=betaflight,ou=groups,ou=dept_h,o=forces,dc=gc,dc=ca")), - new FieldValue(randomiseDn("cn=gammaflight,ou=groups,ou=dept_h,o=forces,dc=gc,dc=ca")) - )), + final ExpressionRoleMapping mapping3 = new ExpressionRoleMapping( + "flight", + new FieldExpression( + "groups", + Arrays.asList( + new FieldValue(randomiseDn("cn=alphaflight,ou=groups,ou=dept_h,o=forces,dc=gc,dc=ca")), + new FieldValue(randomiseDn("cn=betaflight,ou=groups,ou=dept_h,o=forces,dc=gc,dc=ca")), + new FieldValue(randomiseDn("cn=gammaflight,ou=groups,ou=dept_h,o=forces,dc=gc,dc=ca")) + ) + ), Collections.emptyList(), - Arrays.asList(new TemplateRoleName(new BytesArray("{ \"source\":\"{{metadata.extra_group}}\" }"), - TemplateRoleName.Format.STRING)), - Collections.emptyMap(), true); + Arrays.asList( + new TemplateRoleName(new BytesArray("{ \"source\":\"{{metadata.extra_group}}\" }"), TemplateRoleName.Format.STRING) + ), + Collections.emptyMap(), + true + ); // Does not match - mapping is not enabled - final ExpressionRoleMapping mapping4 = new ExpressionRoleMapping("mutants", - new FieldExpression("groups", Collections.singletonList( - new FieldValue(randomiseDn("cn=mutants,ou=groups,ou=dept_h,o=forces,dc=gc,dc=ca")))), - Arrays.asList("mutants"), Collections.emptyList(), Collections.emptyMap(), false); + final ExpressionRoleMapping mapping4 = new ExpressionRoleMapping( + "mutants", + new FieldExpression( + "groups", + Collections.singletonList(new FieldValue(randomiseDn("cn=mutants,ou=groups,ou=dept_h,o=forces,dc=gc,dc=ca"))) + ), + Arrays.asList("mutants"), + Collections.emptyList(), + Collections.emptyMap(), + false + ); final Client client = mock(Client.class); SecurityIndexManager securityIndex = mock(SecurityIndexManager.class); - ScriptService scriptService = new ScriptService(Settings.EMPTY, - Collections.singletonMap(MustacheScriptEngine.NAME, new MustacheScriptEngine()), ScriptModule.CORE_CONTEXTS); + ScriptService scriptService = new ScriptService( + Settings.EMPTY, + Collections.singletonMap(MustacheScriptEngine.NAME, new MustacheScriptEngine()), + ScriptModule.CORE_CONTEXTS + ); when(securityIndex.isAvailable()).thenReturn(true); final NativeRoleMappingStore store = new NativeRoleMappingStore(Settings.EMPTY, client, securityIndex, scriptService) { @@ -106,17 +137,21 @@ protected void loadMappings(ActionListener> listener RealmConfig.RealmIdentifier realmIdentifier = new RealmConfig.RealmIdentifier("ldap", "ldap1"); final Settings settings = Settings.builder() - .put(RealmSettings.getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0).build(); - final RealmConfig realm = new RealmConfig(realmIdentifier, settings, - mock(Environment.class), new ThreadContext(settings)); + .put(RealmSettings.getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0) + .build(); + final RealmConfig realm = new RealmConfig(realmIdentifier, settings, mock(Environment.class), new ThreadContext(settings)); final PlainActionFuture> future = new PlainActionFuture<>(); - final UserRoleMapper.UserData user = new UserRoleMapper.UserData("sasquatch", - randomiseDn("cn=walter.langowski,ou=people,ou=dept_h,o=forces,dc=gc,dc=ca"), - List.of( - randomiseDn("cn=alphaflight,ou=groups,ou=dept_h,o=forces,dc=gc,dc=ca"), - randomiseDn("cn=mutants,ou=groups,ou=dept_h,o=forces,dc=gc,dc=ca") - ), Map.of("extra_group", "flight"), realm); + final UserRoleMapper.UserData user = new UserRoleMapper.UserData( + "sasquatch", + randomiseDn("cn=walter.langowski,ou=people,ou=dept_h,o=forces,dc=gc,dc=ca"), + List.of( + randomiseDn("cn=alphaflight,ou=groups,ou=dept_h,o=forces,dc=gc,dc=ca"), + randomiseDn("cn=mutants,ou=groups,ou=dept_h,o=forces,dc=gc,dc=ca") + ), + Map.of("extra_group", "flight"), + realm + ); logger.info("UserData is [{}]", user); store.resolveRoles(user, future); @@ -152,7 +187,16 @@ private SecurityIndexManager.State dummyState(ClusterHealthStatus indexStatus) { private SecurityIndexManager.State indexState(boolean isUpToDate, ClusterHealthStatus healthStatus) { return new SecurityIndexManager.State( - Instant.now(), isUpToDate, true, true, null, concreteSecurityIndexName, healthStatus, IndexMetadata.State.OPEN, null, "my_uuid" + Instant.now(), + isUpToDate, + true, + true, + null, + concreteSecurityIndexName, + healthStatus, + IndexMetadata.State.OPEN, + null, + "my_uuid" ); } @@ -187,8 +231,9 @@ public void testCacheClearOnIndexHealthChange() { // green to yellow or yellow to green previousState = dummyState(randomFrom(ClusterHealthStatus.GREEN, ClusterHealthStatus.YELLOW)); - currentState = dummyState(previousState.indexHealth == ClusterHealthStatus.GREEN ? - ClusterHealthStatus.YELLOW : ClusterHealthStatus.GREEN); + currentState = dummyState( + previousState.indexHealth == ClusterHealthStatus.GREEN ? ClusterHealthStatus.YELLOW : ClusterHealthStatus.GREEN + ); store.onSecurityIndexStateChange(previousState, currentState); assertEquals(expectedInvalidation, numInvalidation.get()); } @@ -219,12 +264,14 @@ public void testPutRoleMappingWillValidateTemplateRoleNamesBeforeSave() { final TemplateRoleName templateRoleName = mock(TemplateRoleName.class); final ScriptService scriptService = mock(ScriptService.class); when(putRoleMappingRequest.getRoleTemplates()).thenReturn(Collections.singletonList(templateRoleName)); - doAnswer(invocationOnMock -> { - throw new IllegalArgumentException(); - }).when(templateRoleName).validate(scriptService); + doAnswer(invocationOnMock -> { throw new IllegalArgumentException(); }).when(templateRoleName).validate(scriptService); - final NativeRoleMappingStore nativeRoleMappingStore = - new NativeRoleMappingStore(Settings.EMPTY, mock(Client.class), mock(SecurityIndexManager.class), scriptService); + final NativeRoleMappingStore nativeRoleMappingStore = new NativeRoleMappingStore( + Settings.EMPTY, + mock(Client.class), + mock(SecurityIndexManager.class), + scriptService + ); expectThrows(IllegalArgumentException.class, () -> nativeRoleMappingStore.putRoleMapping(putRoleMappingRequest, null)); } @@ -252,16 +299,22 @@ private NativeRoleMappingStore buildRoleMappingStoreForInvalidationTesting(Atomi return null; }).when(client).execute(eq(ClearRealmCacheAction.INSTANCE), any(ClearRealmCacheRequest.class), anyActionListener()); - final NativeRoleMappingStore store = new NativeRoleMappingStore(Settings.EMPTY, client, mock(SecurityIndexManager.class), - mock(ScriptService.class)); + final NativeRoleMappingStore store = new NativeRoleMappingStore( + Settings.EMPTY, + client, + mock(SecurityIndexManager.class), + mock(ScriptService.class) + ); if (attachRealm) { final Environment env = TestEnvironment.newEnvironment(settings); final RealmConfig.RealmIdentifier identifier = new RealmConfig.RealmIdentifier("ldap", realmName); - final RealmConfig realmConfig = new RealmConfig(identifier, - Settings.builder().put(settings) - .put(RealmSettings.getFullSettingKey(identifier, RealmSettings.ORDER_SETTING), 0).build(), - env, threadContext); + final RealmConfig realmConfig = new RealmConfig( + identifier, + Settings.builder().put(settings).put(RealmSettings.getFullSettingKey(identifier, RealmSettings.ORDER_SETTING), 0).build(), + env, + threadContext + ); final CachingUsernamePasswordRealm mockRealm = new CachingUsernamePasswordRealm(realmConfig, threadPool) { @Override protected void doAuthenticate(UsernamePasswordToken token, ActionListener> listener) { diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/AuthorizationServiceTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/AuthorizationServiceTests.java index 37ff07e5f5652..c981dfa9e7c88 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/AuthorizationServiceTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/AuthorizationServiceTests.java @@ -243,9 +243,7 @@ public class AuthorizationServiceTests extends ESTestCase { public void setup() { rolesStore = mock(CompositeRolesStore.class); clusterService = mock(ClusterService.class); - final Settings settings = Settings.builder() - .put("cluster.remote.other_cluster.seeds", "localhost:9999") - .build(); + final Settings settings = Settings.builder().put("cluster.remote.other_cluster.seeds", "localhost:9999").build(); final ClusterSettings clusterSettings = new ClusterSettings(settings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); when(clusterService.getClusterSettings()).thenReturn(clusterSettings); when(clusterService.state()).thenReturn(ClusterState.EMPTY_STATE); @@ -260,14 +258,13 @@ public void setup() { final NativePrivilegeStore privilegesStore = mock(NativePrivilegeStore.class); doAnswer(i -> { - assertThat(i.getArguments().length, equalTo(3)); - final Object arg2 = i.getArguments()[2]; - assertThat(arg2, instanceOf(ActionListener.class)); - ActionListener> listener = (ActionListener>) arg2; - listener.onResponse(Collections.emptyList()); - return null; - } - ).when(privilegesStore).getPrivileges(any(Collection.class), any(Collection.class), anyActionListener()); + assertThat(i.getArguments().length, equalTo(3)); + final Object arg2 = i.getArguments()[2]; + assertThat(arg2, instanceOf(ActionListener.class)); + ActionListener> listener = (ActionListener>) arg2; + listener.onResponse(Collections.emptyList()); + return null; + }).when(privilegesStore).getPrivileges(any(Collection.class), any(Collection.class), anyActionListener()); final Map, Role> roleCache = new HashMap<>(); doAnswer((i) -> { @@ -278,9 +275,20 @@ public void setup() { }).when(rolesStore).getRoles(any(User.class), any(Authentication.class), anyActionListener()); roleMap.put(ReservedRolesStore.SUPERUSER_ROLE_DESCRIPTOR.getName(), ReservedRolesStore.SUPERUSER_ROLE_DESCRIPTOR); operatorPrivilegesService = mock(OperatorPrivileges.OperatorPrivilegesService.class); - authorizationService = new AuthorizationService(settings, rolesStore, clusterService, - auditTrailService, new DefaultAuthenticationFailureHandler(Collections.emptyMap()), threadPool, new AnonymousUser(settings), - null, Collections.emptySet(), licenseState, TestIndexNameExpressionResolver.newInstance(), operatorPrivilegesService); + authorizationService = new AuthorizationService( + settings, + rolesStore, + clusterService, + auditTrailService, + new DefaultAuthenticationFailureHandler(Collections.emptyMap()), + threadPool, + new AnonymousUser(settings), + null, + Collections.emptySet(), + licenseState, + TestIndexNameExpressionResolver.newInstance(), + operatorPrivilegesService + ); } private void buildRole( @@ -312,8 +320,12 @@ private void buildRole( if (roleDescriptors.isEmpty()) { listener.onResponse(Role.EMPTY); } else { - CompositeRolesStore.buildRoleFromDescriptors(roleDescriptors, fieldPermissionsCache, privilegesStore, - RESTRICTED_INDICES_AUTOMATON, ActionListener.wrap(r -> { + CompositeRolesStore.buildRoleFromDescriptors( + roleDescriptors, + fieldPermissionsCache, + privilegesStore, + RESTRICTED_INDICES_AUTOMATON, + ActionListener.wrap(r -> { roleCache.put(names, r); listener.onResponse(r); }, listener::onFailure) @@ -361,8 +373,9 @@ private void authorize( } Mockito.reset(operatorPrivilegesService); final AtomicBoolean operatorPrivilegesChecked = new AtomicBoolean(false); - final ElasticsearchSecurityException operatorPrivilegesException = - new ElasticsearchSecurityException("Operator privileges check failed"); + final ElasticsearchSecurityException operatorPrivilegesException = new ElasticsearchSecurityException( + "Operator privileges check failed" + ); if (shouldFailOperatorPrivilegesCheck) { when(operatorPrivilegesService.check(authentication, action, request, threadContext)).thenAnswer(invocationOnMock -> { operatorPrivilegesChecked.set(true); @@ -442,11 +455,16 @@ public void testActionsForSystemUserIsAuthorized() throws IOException { "indices:admin/seq_no/add_retention_lease", "indices:admin/seq_no/remove_retention_lease", "indices:admin/seq_no/renew_retention_lease", - "indices:admin/settings/update"}; + "indices:admin/settings/update" }; for (String action : actions) { authorize(authentication, action, request); - verify(auditTrail).accessGranted(eq(requestId), eq(authentication), eq(action), eq(request), - authzInfoRoles(new String[]{SystemUser.ROLE_NAME})); + verify(auditTrail).accessGranted( + eq(requestId), + eq(authentication), + eq(action), + eq(request), + authzInfoRoles(new String[] { SystemUser.ROLE_NAME }) + ); } verifyNoMoreInteractions(auditTrail); @@ -455,14 +473,27 @@ public void testActionsForSystemUserIsAuthorized() throws IOException { public void testAuthorizationForSecurityChange() { final Authentication authentication = createAuthentication(new User("user", "manage_security_role")); final String requestId = AuditUtil.getOrGenerateRequestId(threadContext); - RoleDescriptor role = new RoleDescriptor("manage_security_role", new String[]{ClusterPrivilegeResolver.MANAGE_SECURITY.name()}, - null, null, null, null, null, null); + RoleDescriptor role = new RoleDescriptor( + "manage_security_role", + new String[] { ClusterPrivilegeResolver.MANAGE_SECURITY.name() }, + null, + null, + null, + null, + null, + null + ); roleMap.put("manage_security_role", role); for (String action : LoggingAuditTrail.SECURITY_CHANGE_ACTIONS) { TransportRequest request = mock(TransportRequest.class); authorize(authentication, action, request); - verify(auditTrail).accessGranted(eq(requestId), eq(authentication), eq(action), eq(request), - authzInfoRoles(new String[]{role.getName()})); + verify(auditTrail).accessGranted( + eq(requestId), + eq(authentication), + eq(action), + eq(request), + authzInfoRoles(new String[] { role.getName() }) + ); } verifyNoMoreInteractions(auditTrail); } @@ -473,9 +504,16 @@ public void testIndicesActionsForSystemUserWhichAreNotAuthorized() throws IOExce final String requestId = AuditUtil.getOrGenerateRequestId(threadContext); assertThrowsAuthorizationException( () -> authorize(authentication, "indices:", request), - "indices:", SystemUser.INSTANCE.principal()); - verify(auditTrail).accessDenied(eq(requestId), eq(authentication), eq("indices:"), eq(request), - authzInfoRoles(new String[]{SystemUser.ROLE_NAME})); + "indices:", + SystemUser.INSTANCE.principal() + ); + verify(auditTrail).accessDenied( + eq(requestId), + eq(authentication), + eq("indices:"), + eq(request), + authzInfoRoles(new String[] { SystemUser.ROLE_NAME }) + ); verifyNoMoreInteractions(auditTrail); } @@ -485,9 +523,16 @@ public void testClusterAdminActionsForSystemUserWhichAreNotAuthorized() throws I final String requestId = AuditUtil.getOrGenerateRequestId(threadContext); assertThrowsAuthorizationException( () -> authorize(authentication, "cluster:admin/whatever", request), - "cluster:admin/whatever", SystemUser.INSTANCE.principal()); - verify(auditTrail).accessDenied(eq(requestId), eq(authentication), eq("cluster:admin/whatever"), eq(request), - authzInfoRoles(new String[]{SystemUser.ROLE_NAME})); + "cluster:admin/whatever", + SystemUser.INSTANCE.principal() + ); + verify(auditTrail).accessDenied( + eq(requestId), + eq(authentication), + eq("cluster:admin/whatever"), + eq(request), + authzInfoRoles(new String[] { SystemUser.ROLE_NAME }) + ); verifyNoMoreInteractions(auditTrail); } @@ -497,9 +542,16 @@ public void testClusterAdminSnapshotStatusActionForSystemUserWhichIsNotAuthorize final String requestId = AuditUtil.getOrGenerateRequestId(threadContext); assertThrowsAuthorizationException( () -> authorize(authentication, "cluster:admin/snapshot/status", request), - "cluster:admin/snapshot/status", SystemUser.INSTANCE.principal()); - verify(auditTrail).accessDenied(eq(requestId), eq(authentication), eq("cluster:admin/snapshot/status"), eq(request), - authzInfoRoles(new String[]{SystemUser.ROLE_NAME})); + "cluster:admin/snapshot/status", + SystemUser.INSTANCE.principal() + ); + verify(auditTrail).accessDenied( + eq(requestId), + eq(authentication), + eq("cluster:admin/snapshot/status"), + eq(request), + authzInfoRoles(new String[] { SystemUser.ROLE_NAME }) + ); verifyNoMoreInteractions(auditTrail); } @@ -511,21 +563,28 @@ public void testAuthorizeUsingConditionalPrivileges() throws IOException { @Override public ClusterPermission.Builder buildPermission(ClusterPermission.Builder builder) { final Predicate requestPredicate = r -> r == request; - builder.add(this, ((ActionClusterPrivilege) ClusterPrivilegeResolver.MANAGE_SECURITY).getAllowedActionPatterns(), - requestPredicate); + builder.add( + this, + ((ActionClusterPrivilege) ClusterPrivilegeResolver.MANAGE_SECURITY).getAllowedActionPatterns(), + requestPredicate + ); return builder; } }; - final ConfigurableClusterPrivilege[] configurableClusterPrivileges = new ConfigurableClusterPrivilege[]{ - configurableClusterPrivilege - }; + final ConfigurableClusterPrivilege[] configurableClusterPrivileges = new ConfigurableClusterPrivilege[] { + configurableClusterPrivilege }; final String requestId = AuditUtil.getOrGenerateRequestId(threadContext); RoleDescriptor role = new RoleDescriptor("role1", null, null, null, configurableClusterPrivileges, null, null, null); roleMap.put("role1", role); authorize(authentication, DeletePrivilegesAction.NAME, request); - verify(auditTrail).accessGranted(eq(requestId), eq(authentication), eq(DeletePrivilegesAction.NAME), eq(request), - authzInfoRoles(new String[]{role.getName()})); + verify(auditTrail).accessGranted( + eq(requestId), + eq(authentication), + eq(DeletePrivilegesAction.NAME), + eq(request), + authzInfoRoles(new String[] { role.getName() }) + ); verifyNoMoreInteractions(auditTrail); } @@ -537,23 +596,32 @@ public void testAuthorizationDeniedWhenConditionalPrivilegesDoNotMatch() throws @Override public ClusterPermission.Builder buildPermission(ClusterPermission.Builder builder) { final Predicate requestPredicate = r -> false; - builder.add(this, ((ActionClusterPrivilege) ClusterPrivilegeResolver.MANAGE_SECURITY).getAllowedActionPatterns(), - requestPredicate); + builder.add( + this, + ((ActionClusterPrivilege) ClusterPrivilegeResolver.MANAGE_SECURITY).getAllowedActionPatterns(), + requestPredicate + ); return builder; } }; - final ConfigurableClusterPrivilege[] configurableClusterPrivileges = new ConfigurableClusterPrivilege[]{ - configurableClusterPrivilege - }; + final ConfigurableClusterPrivilege[] configurableClusterPrivileges = new ConfigurableClusterPrivilege[] { + configurableClusterPrivilege }; final String requestId = AuditUtil.getOrGenerateRequestId(threadContext); RoleDescriptor role = new RoleDescriptor("role1", null, null, null, configurableClusterPrivileges, null, null, null); roleMap.put("role1", role); assertThrowsAuthorizationException( () -> authorize(authentication, DeletePrivilegesAction.NAME, request), - DeletePrivilegesAction.NAME, "user1"); - verify(auditTrail).accessDenied(eq(requestId), eq(authentication), eq(DeletePrivilegesAction.NAME), eq(request), - authzInfoRoles(new String[]{role.getName()})); + DeletePrivilegesAction.NAME, + "user1" + ); + verify(auditTrail).accessDenied( + eq(requestId), + eq(authentication), + eq(DeletePrivilegesAction.NAME), + eq(request), + authzInfoRoles(new String[] { role.getName() }) + ); verifyNoMoreInteractions(auditTrail); } @@ -562,11 +630,14 @@ public void testNoRolesCausesDenial() throws IOException { final Authentication authentication = createAuthentication(new User("test user")); mockEmptyMetadata(); final String requestId = AuditUtil.getOrGenerateRequestId(threadContext); - assertThrowsAuthorizationException( - () -> authorize(authentication, "indices:a", request), - "indices:a", "test user"); - verify(auditTrail).accessDenied(eq(requestId), eq(authentication), eq("indices:a"), eq(request), - authzInfoRoles(Role.EMPTY.names())); + assertThrowsAuthorizationException(() -> authorize(authentication, "indices:a", request), "indices:a", "test user"); + verify(auditTrail).accessDenied( + eq(requestId), + eq(authentication), + eq("indices:a"), + eq(request), + authzInfoRoles(Role.EMPTY.names()) + ); verifyNoMoreInteractions(auditTrail); } @@ -577,8 +648,13 @@ public void testUserWithNoRolesCanPerformRemoteSearch() throws IOException { mockEmptyMetadata(); final String requestId = AuditUtil.getOrGenerateRequestId(threadContext); authorize(authentication, SearchAction.NAME, request); - verify(auditTrail).accessGranted(eq(requestId), eq(authentication), eq(SearchAction.NAME), eq(request), - authzInfoRoles(Role.EMPTY.names())); + verify(auditTrail).accessGranted( + eq(requestId), + eq(authentication), + eq(SearchAction.NAME), + eq(request), + authzInfoRoles(Role.EMPTY.names()) + ); verifyNoMoreInteractions(auditTrail); } @@ -594,15 +670,25 @@ public void testUserWithNoRolesPerformsRemoteSearchWithScroll() { if (hasLocalIndices) { assertThrowsAuthorizationException( () -> authorize(authentication, SearchScrollAction.NAME, searchScrollRequest), - "indices:data/read/scroll", "test user" + "indices:data/read/scroll", + "test user" + ); + verify(auditTrail).accessDenied( + eq(requestId), + eq(authentication), + eq("indices:data/read/scroll"), + eq(searchScrollRequest), + authzInfoRoles(Role.EMPTY.names()) ); - verify(auditTrail).accessDenied(eq(requestId), eq(authentication), - eq("indices:data/read/scroll"), eq(searchScrollRequest), - authzInfoRoles(Role.EMPTY.names())); } else { authorize(authentication, SearchScrollAction.NAME, searchScrollRequest); - verify(auditTrail).accessGranted(eq(requestId), eq(authentication), eq(SearchScrollAction.NAME), eq(searchScrollRequest), - authzInfoRoles(Role.EMPTY.names())); + verify(auditTrail).accessGranted( + eq(requestId), + eq(authentication), + eq(SearchScrollAction.NAME), + eq(searchScrollRequest), + authzInfoRoles(Role.EMPTY.names()) + ); } verifyNoMoreInteractions(auditTrail); } @@ -619,11 +705,14 @@ public void testUserWithNoRolesCannotPerformLocalSearch() throws IOException { final Authentication authentication = createAuthentication(new User("test user")); mockEmptyMetadata(); final String requestId = AuditUtil.getOrGenerateRequestId(threadContext); - assertThrowsAuthorizationException( - () -> authorize(authentication, SearchAction.NAME, request), - SearchAction.NAME, "test user"); - verify(auditTrail).accessDenied(eq(requestId), eq(authentication), eq(SearchAction.NAME), eq(request), - authzInfoRoles(Role.EMPTY.names())); + assertThrowsAuthorizationException(() -> authorize(authentication, SearchAction.NAME, request), SearchAction.NAME, "test user"); + verify(auditTrail).accessDenied( + eq(requestId), + eq(authentication), + eq(SearchAction.NAME), + eq(request), + authzInfoRoles(Role.EMPTY.names()) + ); verifyNoMoreInteractions(auditTrail); } @@ -637,11 +726,14 @@ public void testUserWithNoRolesCanPerformMultiClusterSearch() throws IOException final Authentication authentication = createAuthentication(new User("test user")); mockEmptyMetadata(); final String requestId = AuditUtil.getOrGenerateRequestId(threadContext); - assertThrowsAuthorizationException( - () -> authorize(authentication, SearchAction.NAME, request), - SearchAction.NAME, "test user"); - verify(auditTrail).accessDenied(eq(requestId), eq(authentication), eq(SearchAction.NAME), eq(request), - authzInfoRoles(Role.EMPTY.names())); + assertThrowsAuthorizationException(() -> authorize(authentication, SearchAction.NAME, request), SearchAction.NAME, "test user"); + verify(auditTrail).accessDenied( + eq(requestId), + eq(authentication), + eq(SearchAction.NAME), + eq(request), + authzInfoRoles(Role.EMPTY.names()) + ); verifyNoMoreInteractions(auditTrail); } @@ -650,11 +742,14 @@ public void testUserWithNoRolesCannotSql() throws IOException { Authentication authentication = createAuthentication(new User("test user")); mockEmptyMetadata(); final String requestId = AuditUtil.getOrGenerateRequestId(threadContext); - assertThrowsAuthorizationException( - () -> authorize(authentication, SqlQueryAction.NAME, request), - SqlQueryAction.NAME, "test user"); - verify(auditTrail).accessDenied(eq(requestId), eq(authentication), eq(SqlQueryAction.NAME), eq(request), - authzInfoRoles(Role.EMPTY.names())); + assertThrowsAuthorizationException(() -> authorize(authentication, SqlQueryAction.NAME, request), SqlQueryAction.NAME, "test user"); + verify(auditTrail).accessDenied( + eq(requestId), + eq(authentication), + eq(SqlQueryAction.NAME), + eq(request), + authzInfoRoles(Role.EMPTY.names()) + ); verifyNoMoreInteractions(auditTrail); } @@ -670,9 +765,16 @@ public void testRemoteIndicesOnlyWorkWithApplicableRequestTypes() throws IOExcep final String requestId = AuditUtil.getOrGenerateRequestId(threadContext); assertThrowsAuthorizationException( () -> authorize(authentication, DeleteIndexAction.NAME, request), - DeleteIndexAction.NAME, "test user"); - verify(auditTrail).accessDenied(eq(requestId), eq(authentication), eq(DeleteIndexAction.NAME), eq(request), - authzInfoRoles(Role.EMPTY.names())); + DeleteIndexAction.NAME, + "test user" + ); + verify(auditTrail).accessDenied( + eq(requestId), + eq(authentication), + eq(DeleteIndexAction.NAME), + eq(request), + authzInfoRoles(Role.EMPTY.names()) + ); verifyNoMoreInteractions(auditTrail); } @@ -681,14 +783,14 @@ public void testUserWithNoRolesOpenPointInTimeWithRemoteIndices() { mockEmptyMetadata(); final String requestId = AuditUtil.getOrGenerateRequestId(threadContext); for (final boolean hasLocalIndices : List.of(true, false)) { - final String[] indices = new String[]{ - hasLocalIndices ? - randomAlphaOfLength(5) : - "other_cluster:" + randomFrom(randomAlphaOfLength(5), "*", randomAlphaOfLength(4) + "*"), - "other_cluster:" + randomFrom(randomAlphaOfLength(5), "*", randomAlphaOfLength(4) + "*") - }; - final OpenPointInTimeRequest openPointInTimeRequest = new OpenPointInTimeRequest(indices) - .keepAlive(TimeValue.timeValueMinutes(randomLongBetween(1, 10))); + final String[] indices = new String[] { + hasLocalIndices + ? randomAlphaOfLength(5) + : "other_cluster:" + randomFrom(randomAlphaOfLength(5), "*", randomAlphaOfLength(4) + "*"), + "other_cluster:" + randomFrom(randomAlphaOfLength(5), "*", randomAlphaOfLength(4) + "*") }; + final OpenPointInTimeRequest openPointInTimeRequest = new OpenPointInTimeRequest(indices).keepAlive( + TimeValue.timeValueMinutes(randomLongBetween(1, 10)) + ); if (randomBoolean()) { openPointInTimeRequest.routing(randomAlphaOfLength(5)); } @@ -698,16 +800,25 @@ public void testUserWithNoRolesOpenPointInTimeWithRemoteIndices() { if (hasLocalIndices) { assertThrowsAuthorizationException( () -> authorize(authentication, OpenPointInTimeAction.NAME, openPointInTimeRequest), - "indices:data/read/open_point_in_time", "test user" + "indices:data/read/open_point_in_time", + "test user" + ); + verify(auditTrail).accessDenied( + eq(requestId), + eq(authentication), + eq("indices:data/read/open_point_in_time"), + eq(openPointInTimeRequest), + authzInfoRoles(Role.EMPTY.names()) ); - verify(auditTrail).accessDenied(eq(requestId), eq(authentication), - eq("indices:data/read/open_point_in_time"), eq(openPointInTimeRequest), - authzInfoRoles(Role.EMPTY.names())); } else { authorize(authentication, OpenPointInTimeAction.NAME, openPointInTimeRequest); - verify(auditTrail).accessGranted(eq(requestId), eq(authentication), - eq("indices:data/read/open_point_in_time"), eq(openPointInTimeRequest), - authzInfoRoles(Role.EMPTY.names())); + verify(auditTrail).accessGranted( + eq(requestId), + eq(authentication), + eq("indices:data/read/open_point_in_time"), + eq(openPointInTimeRequest), + authzInfoRoles(Role.EMPTY.names()) + ); } verifyNoMoreInteractions(auditTrail); } @@ -719,28 +830,36 @@ public void testUserWithNoRolesCanClosePointInTime() { mockEmptyMetadata(); final String requestId = AuditUtil.getOrGenerateRequestId(threadContext); authorize(authentication, ClosePointInTimeAction.NAME, closePointInTimeRequest); - verify(auditTrail).accessGranted(eq(requestId), eq(authentication), - eq("indices:data/read/close_point_in_time"), eq(closePointInTimeRequest), - authzInfoRoles(Role.EMPTY.names())); + verify(auditTrail).accessGranted( + eq(requestId), + eq(authentication), + eq("indices:data/read/close_point_in_time"), + eq(closePointInTimeRequest), + authzInfoRoles(Role.EMPTY.names()) + ); verifyNoMoreInteractions(auditTrail); } public void testUnknownRoleCausesDenial() throws IOException { - Tuple tuple = randomFrom(asList( - new Tuple<>(SearchAction.NAME, new SearchRequest()), - new Tuple<>(SqlQueryAction.NAME, new SqlQueryRequest()))); + Tuple tuple = randomFrom( + asList(new Tuple<>(SearchAction.NAME, new SearchRequest()), new Tuple<>(SqlQueryAction.NAME, new SqlQueryRequest())) + ); String action = tuple.v1(); TransportRequest request = tuple.v2(); final Authentication authentication = createAuthentication(new User("test user", "non-existent-role")); final String requestId = AuditUtil.getOrGenerateRequestId(threadContext); mockEmptyMetadata(); - ElasticsearchSecurityException securityException = expectThrows(ElasticsearchSecurityException.class, - () -> authorize(authentication, action, request)); - assertThat(securityException, throwableWithMessage(containsString( - "[" + action + "] is unauthorized" + - " for user [test user]" + - " with roles [non-existent-role],"))); + ElasticsearchSecurityException securityException = expectThrows( + ElasticsearchSecurityException.class, + () -> authorize(authentication, action, request) + ); + assertThat( + securityException, + throwableWithMessage( + containsString("[" + action + "] is unauthorized" + " for user [test user]" + " with roles [non-existent-role],") + ) + ); assertThat(securityException, throwableWithMessage(containsString("this action is granted by the index privileges [read,all]"))); verify(auditTrail).accessDenied(eq(requestId), eq(authentication), eq(action), eq(request), authzInfoRoles(Role.EMPTY.names())); @@ -748,21 +867,23 @@ public void testUnknownRoleCausesDenial() throws IOException { } public void testServiceAccountDenial() { - Tuple tuple = randomFrom(asList( - new Tuple<>(SearchAction.NAME, new SearchRequest()), - new Tuple<>(SqlQueryAction.NAME, new SqlQueryRequest()))); + Tuple tuple = randomFrom( + asList(new Tuple<>(SearchAction.NAME, new SearchRequest()), new Tuple<>(SqlQueryAction.NAME, new SqlQueryRequest())) + ); String action = tuple.v1(); TransportRequest request = tuple.v2(); final String requestId = AuditUtil.getOrGenerateRequestId(threadContext); mockEmptyMetadata(); final User serviceUser = new User(randomAlphaOfLengthBetween(3, 8) + "/" + randomAlphaOfLengthBetween(3, 8)); - final Authentication authentication = new Authentication(serviceUser, + final Authentication authentication = new Authentication( + serviceUser, new RealmRef("_service_account", "_service_account", randomAlphaOfLengthBetween(3, 8)), null, Version.CURRENT, Authentication.AuthenticationType.TOKEN, - Map.of()); + Map.of() + ); Mockito.reset(rolesStore); doAnswer(invocationOnMock -> { @SuppressWarnings("unchecked") @@ -771,11 +892,14 @@ public void testServiceAccountDenial() { return null; }).when(rolesStore).getRoles(any(User.class), any(Authentication.class), anyActionListener()); - ElasticsearchSecurityException securityException = expectThrows(ElasticsearchSecurityException.class, - () -> authorize(authentication, action, request)); - assertThat(securityException, throwableWithMessage(containsString( - "[" + action + "] is unauthorized" + - " for user [" + serviceUser.principal() + "],"))); + ElasticsearchSecurityException securityException = expectThrows( + ElasticsearchSecurityException.class, + () -> authorize(authentication, action, request) + ); + assertThat( + securityException, + throwableWithMessage(containsString("[" + action + "] is unauthorized" + " for user [" + serviceUser.principal() + "],")) + ); assertThat(securityException, throwableWithMessage(containsString("this action is granted by the index privileges [read,all]"))); verify(auditTrail).accessDenied(eq(requestId), eq(authentication), eq(action), eq(request), authzInfoRoles(Role.EMPTY.names())); verifyNoMoreInteractions(auditTrail); @@ -785,22 +909,30 @@ public void testThatNonIndicesAndNonClusterActionIsDenied() throws IOException { final TransportRequest request = mock(TransportRequest.class); final String requestId = AuditUtil.getOrGenerateRequestId(threadContext); final Authentication authentication = createAuthentication(new User("test user", "a_all")); - final RoleDescriptor role = new RoleDescriptor("a_all", null, - new IndicesPrivileges[]{IndicesPrivileges.builder().indices("a").privileges("all").build()}, null); + final RoleDescriptor role = new RoleDescriptor( + "a_all", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices("a").privileges("all").build() }, + null + ); roleMap.put("a_all", role); - assertThrowsAuthorizationException( - () -> authorize(authentication, "whatever", request), - "whatever", "test user"); - verify(auditTrail).accessDenied(eq(requestId), eq(authentication), eq("whatever"), eq(request), - authzInfoRoles(new String[]{role.getName()})); + assertThrowsAuthorizationException(() -> authorize(authentication, "whatever", request), "whatever", "test user"); + verify(auditTrail).accessDenied( + eq(requestId), + eq(authentication), + eq("whatever"), + eq(request), + authzInfoRoles(new String[] { role.getName() }) + ); verifyNoMoreInteractions(auditTrail); } public void testThatRoleWithNoIndicesIsDenied() throws IOException { Tuple tuple = randomFrom( new Tuple<>(SearchAction.NAME, new SearchRequest()), - new Tuple<>(SqlQueryAction.NAME, new SqlQueryRequest())); + new Tuple<>(SqlQueryAction.NAME, new SqlQueryRequest()) + ); String action = tuple.v1(); TransportRequest request = tuple.v2(); final String requestId = AuditUtil.getOrGenerateRequestId(threadContext); @@ -809,16 +941,23 @@ public void testThatRoleWithNoIndicesIsDenied() throws IOException { roleMap.put("no_indices", role); mockEmptyMetadata(); - ElasticsearchSecurityException securityException = expectThrows(ElasticsearchSecurityException.class, - () -> authorize(authentication, action, request)); - assertThat(securityException, throwableWithMessage(containsString( - "[" + action + "] is unauthorized" + - " for user [test user]" + - " with roles [no_indices],"))); + ElasticsearchSecurityException securityException = expectThrows( + ElasticsearchSecurityException.class, + () -> authorize(authentication, action, request) + ); + assertThat( + securityException, + throwableWithMessage(containsString("[" + action + "] is unauthorized" + " for user [test user]" + " with roles [no_indices],")) + ); assertThat(securityException, throwableWithMessage(containsString("this action is granted by the index privileges [read,all]"))); - verify(auditTrail).accessDenied(eq(requestId), eq(authentication), eq(action), eq(request), - authzInfoRoles(new String[]{role.getName()})); + verify(auditTrail).accessDenied( + eq(requestId), + eq(authentication), + eq(action), + eq(request), + authzInfoRoles(new String[] { role.getName() }) + ); verifyNoMoreInteractions(auditTrail); } @@ -828,52 +967,74 @@ public void testElasticUserAuthorizedForNonChangePasswordRequestsWhenNotInSetupM final Tuple request = randomCompositeRequest(); authorize(authentication, request.v1(), request.v2()); - verify(auditTrail).accessGranted(eq(requestId), eq(authentication), eq(request.v1()), eq(request.v2()), - authzInfoRoles(new String[]{ElasticUser.ROLE_NAME})); + verify(auditTrail).accessGranted( + eq(requestId), + eq(authentication), + eq(request.v1()), + eq(request.v2()), + authzInfoRoles(new String[] { ElasticUser.ROLE_NAME }) + ); } public void testSearchAgainstEmptyCluster() throws Exception { - RoleDescriptor role = new RoleDescriptor("a_all", null, - new IndicesPrivileges[]{IndicesPrivileges.builder().indices("a").privileges("all").build()}, null); + RoleDescriptor role = new RoleDescriptor( + "a_all", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices("a").privileges("all").build() }, + null + ); final Authentication authentication = createAuthentication(new User("test user", "a_all")); final String requestId = AuditUtil.getOrGenerateRequestId(threadContext); roleMap.put("a_all", role); mockEmptyMetadata(); { - //ignore_unavailable set to false, user is not authorized for this index nor does it exist - SearchRequest searchRequest = new SearchRequest("does_not_exist") - .indicesOptions(IndicesOptions.fromOptions(false, true, - true, false)); + // ignore_unavailable set to false, user is not authorized for this index nor does it exist + SearchRequest searchRequest = new SearchRequest("does_not_exist").indicesOptions( + IndicesOptions.fromOptions(false, true, true, false) + ); assertThrowsAuthorizationException( () -> authorize(authentication, SearchAction.NAME, searchRequest), - SearchAction.NAME, "test user"); - verify(auditTrail).accessDenied(eq(requestId), eq(authentication), eq(SearchAction.NAME), eq(searchRequest), - authzInfoRoles(new String[]{role.getName()})); + SearchAction.NAME, + "test user" + ); + verify(auditTrail).accessDenied( + eq(requestId), + eq(authentication), + eq(SearchAction.NAME), + eq(searchRequest), + authzInfoRoles(new String[] { role.getName() }) + ); verifyNoMoreInteractions(auditTrail); } { - //ignore_unavailable and allow_no_indices both set to true, user is not authorized for this index nor does it exist - SearchRequest searchRequest = new SearchRequest("does_not_exist") - .indicesOptions(IndicesOptions.fromOptions(true, true, true, false)); + // ignore_unavailable and allow_no_indices both set to true, user is not authorized for this index nor does it exist + SearchRequest searchRequest = new SearchRequest("does_not_exist").indicesOptions( + IndicesOptions.fromOptions(true, true, true, false) + ); final ActionListener listener = ActionListener.wrap(ignore -> { - final IndicesAccessControl indicesAccessControl = - threadContext.getTransient(AuthorizationServiceField.INDICES_PERMISSIONS_KEY); + final IndicesAccessControl indicesAccessControl = threadContext.getTransient( + AuthorizationServiceField.INDICES_PERMISSIONS_KEY + ); assertNotNull(indicesAccessControl); - final IndicesAccessControl.IndexAccessControl indexAccessControl = - indicesAccessControl.getIndexPermissions(IndicesAndAliasesResolverField.NO_INDEX_PLACEHOLDER); + final IndicesAccessControl.IndexAccessControl indexAccessControl = indicesAccessControl.getIndexPermissions( + IndicesAndAliasesResolverField.NO_INDEX_PLACEHOLDER + ); assertFalse(indexAccessControl.getFieldPermissions().hasFieldLevelSecurity()); assertFalse(indexAccessControl.getDocumentPermissions().hasDocumentLevelPermissions()); - }, e -> { - fail(e.getMessage()); - }); + }, e -> { fail(e.getMessage()); }); final CountDownLatch latch = new CountDownLatch(1); authorizationService.authorize(authentication, SearchAction.NAME, searchRequest, new LatchedActionListener<>(listener, latch)); latch.await(); - verify(auditTrail).accessGranted(eq(requestId), eq(authentication), eq(SearchAction.NAME), eq(searchRequest), - authzInfoRoles(new String[]{role.getName()})); + verify(auditTrail).accessGranted( + eq(requestId), + eq(authentication), + eq(SearchAction.NAME), + eq(searchRequest), + authzInfoRoles(new String[] { role.getName() }) + ); } } @@ -911,18 +1072,12 @@ public void testSearchAgainstIndex() throws Exception { verify(rolesStore).getRoles(Mockito.same(user), Mockito.same(authentication), Mockito.any()); IndicesAccessControl iac = threadContext.getTransient(AuthorizationServiceField.INDICES_PERMISSIONS_KEY); // Within the action handler, execute a child action (the query phase of search) - authorize( - authentication, - SearchTransportService.QUERY_ACTION_NAME, - shardRequest, - false, - () -> { - // This child action triggers a second interaction with the role store (which is cached) - verify(rolesStore, times(2)).getRoles(Mockito.same(user), Mockito.same(authentication), Mockito.any()); - // But it does not create a new IndicesAccessControl - assertThat(threadContext.getTransient(AuthorizationServiceField.INDICES_PERMISSIONS_KEY), sameInstance(iac)); - } - ); + authorize(authentication, SearchTransportService.QUERY_ACTION_NAME, shardRequest, false, () -> { + // This child action triggers a second interaction with the role store (which is cached) + verify(rolesStore, times(2)).getRoles(Mockito.same(user), Mockito.same(authentication), Mockito.any()); + // But it does not create a new IndicesAccessControl + assertThat(threadContext.getTransient(AuthorizationServiceField.INDICES_PERMISSIONS_KEY), sameInstance(iac)); + }); }); verify(auditTrail).accessGranted( eq(requestId), @@ -942,8 +1097,12 @@ public void testSearchAgainstIndex() throws Exception { } public void testScrollRelatedRequestsAllowed() { - RoleDescriptor role = new RoleDescriptor("a_all", null, - new IndicesPrivileges[]{IndicesPrivileges.builder().indices("a").privileges("all").build()}, null); + RoleDescriptor role = new RoleDescriptor( + "a_all", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices("a").privileges("all").build() }, + null + ); final Authentication authentication = createAuthentication(new User("test user", "a_all")); roleMap.put("a_all", role); mockEmptyMetadata(); @@ -951,55 +1110,97 @@ public void testScrollRelatedRequestsAllowed() { final ClearScrollRequest clearScrollRequest = new ClearScrollRequest(); authorize(authentication, ClearScrollAction.NAME, clearScrollRequest); - verify(auditTrail).accessGranted(eq(requestId), eq(authentication), eq(ClearScrollAction.NAME), eq(clearScrollRequest), - authzInfoRoles(new String[]{role.getName()})); + verify(auditTrail).accessGranted( + eq(requestId), + eq(authentication), + eq(ClearScrollAction.NAME), + eq(clearScrollRequest), + authzInfoRoles(new String[] { role.getName() }) + ); final ParsedScrollId parsedScrollId = mock(ParsedScrollId.class); when(parsedScrollId.hasLocalIndices()).thenReturn(true); final SearchScrollRequest searchScrollRequest = mock(SearchScrollRequest.class); when(searchScrollRequest.parseScrollId()).thenReturn(parsedScrollId); authorize(authentication, SearchScrollAction.NAME, searchScrollRequest); - verify(auditTrail).accessGranted(eq(requestId), eq(authentication), eq(SearchScrollAction.NAME), eq(searchScrollRequest), - authzInfoRoles(new String[]{role.getName()})); + verify(auditTrail).accessGranted( + eq(requestId), + eq(authentication), + eq(SearchScrollAction.NAME), + eq(searchScrollRequest), + authzInfoRoles(new String[] { role.getName() }) + ); // We have to use a mock request for other Scroll actions as the actual requests are package private to SearchTransportService final TransportRequest request = mock(TransportRequest.class); authorize(authentication, SearchTransportService.CLEAR_SCROLL_CONTEXTS_ACTION_NAME, request); - verify(auditTrail).accessGranted(eq(requestId), eq(authentication), eq(SearchTransportService.CLEAR_SCROLL_CONTEXTS_ACTION_NAME), - eq(request), authzInfoRoles(new String[]{role.getName()})); + verify(auditTrail).accessGranted( + eq(requestId), + eq(authentication), + eq(SearchTransportService.CLEAR_SCROLL_CONTEXTS_ACTION_NAME), + eq(request), + authzInfoRoles(new String[] { role.getName() }) + ); authorize(authentication, SearchTransportService.FETCH_ID_SCROLL_ACTION_NAME, request); - verify(auditTrail).accessGranted(eq(requestId), eq(authentication), eq(SearchTransportService.FETCH_ID_SCROLL_ACTION_NAME), - eq(request), authzInfoRoles(new String[]{role.getName()})); + verify(auditTrail).accessGranted( + eq(requestId), + eq(authentication), + eq(SearchTransportService.FETCH_ID_SCROLL_ACTION_NAME), + eq(request), + authzInfoRoles(new String[] { role.getName() }) + ); authorize(authentication, SearchTransportService.QUERY_FETCH_SCROLL_ACTION_NAME, request); - verify(auditTrail).accessGranted(eq(requestId), eq(authentication), eq(SearchTransportService.QUERY_FETCH_SCROLL_ACTION_NAME), - eq(request), authzInfoRoles(new String[]{role.getName()})); + verify(auditTrail).accessGranted( + eq(requestId), + eq(authentication), + eq(SearchTransportService.QUERY_FETCH_SCROLL_ACTION_NAME), + eq(request), + authzInfoRoles(new String[] { role.getName() }) + ); authorize(authentication, SearchTransportService.QUERY_SCROLL_ACTION_NAME, request); - verify(auditTrail).accessGranted(eq(requestId), eq(authentication), eq(SearchTransportService.QUERY_SCROLL_ACTION_NAME), - eq(request), authzInfoRoles(new String[]{role.getName()})); + verify(auditTrail).accessGranted( + eq(requestId), + eq(authentication), + eq(SearchTransportService.QUERY_SCROLL_ACTION_NAME), + eq(request), + authzInfoRoles(new String[] { role.getName() }) + ); authorize(authentication, SearchTransportService.FREE_CONTEXT_SCROLL_ACTION_NAME, request); - verify(auditTrail).accessGranted(eq(requestId), eq(authentication), eq(SearchTransportService.FREE_CONTEXT_SCROLL_ACTION_NAME), - eq(request), authzInfoRoles(new String[]{role.getName()})); + verify(auditTrail).accessGranted( + eq(requestId), + eq(authentication), + eq(SearchTransportService.FREE_CONTEXT_SCROLL_ACTION_NAME), + eq(request), + authzInfoRoles(new String[] { role.getName() }) + ); verifyNoMoreInteractions(auditTrail); } public void testAuthorizeIndicesFailures() throws IOException { TransportRequest request = new GetIndexRequest().indices("b"); ClusterState state = mockEmptyMetadata(); - RoleDescriptor role = new RoleDescriptor("a_all", null, - new IndicesPrivileges[]{IndicesPrivileges.builder().indices("a").privileges("all").build()}, null); + RoleDescriptor role = new RoleDescriptor( + "a_all", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices("a").privileges("all").build() }, + null + ); final Authentication authentication = createAuthentication(new User("test user", "a_all")); roleMap.put("a_all", role); final String requestId = AuditUtil.getOrGenerateRequestId(threadContext); - assertThrowsAuthorizationException( - () -> authorize(authentication, "indices:a", request), - "indices:a", "test user"); - verify(auditTrail).accessDenied(eq(requestId), eq(authentication), eq("indices:a"), eq(request), - authzInfoRoles(new String[]{role.getName()})); + assertThrowsAuthorizationException(() -> authorize(authentication, "indices:a", request), "indices:a", "test user"); + verify(auditTrail).accessDenied( + eq(requestId), + eq(authentication), + eq("indices:a"), + eq(request), + authzInfoRoles(new String[] { role.getName() }) + ); verifyNoMoreInteractions(auditTrail); verify(clusterService, times(1)).state(); verify(state, times(1)).metadata(); @@ -1009,19 +1210,35 @@ public void testCreateIndexWithAliasWithoutPermissions() throws IOException { CreateIndexRequest request = new CreateIndexRequest("a"); request.alias(new Alias("a2")); ClusterState state = mockEmptyMetadata(); - RoleDescriptor role = new RoleDescriptor("a_all", null, - new IndicesPrivileges[]{IndicesPrivileges.builder().indices("a").privileges("all").build()}, null); + RoleDescriptor role = new RoleDescriptor( + "a_all", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices("a").privileges("all").build() }, + null + ); final Authentication authentication = createAuthentication(new User("test user", "a_all")); roleMap.put("a_all", role); final String requestId = AuditUtil.getOrGenerateRequestId(threadContext); assertThrowsAuthorizationException( () -> authorize(authentication, CreateIndexAction.NAME, request), - IndicesAliasesAction.NAME, "test user"); - verify(auditTrail).accessGranted(eq(requestId), eq(authentication), eq(CreateIndexAction.NAME), eq(request), - authzInfoRoles(new String[]{role.getName()})); - verify(auditTrail).accessDenied(eq(requestId), eq(authentication), eq(IndicesAliasesAction.NAME), eq(request), - authzInfoRoles(new String[]{role.getName()})); + IndicesAliasesAction.NAME, + "test user" + ); + verify(auditTrail).accessGranted( + eq(requestId), + eq(authentication), + eq(CreateIndexAction.NAME), + eq(request), + authzInfoRoles(new String[] { role.getName() }) + ); + verify(auditTrail).accessDenied( + eq(requestId), + eq(authentication), + eq(IndicesAliasesAction.NAME), + eq(request), + authzInfoRoles(new String[] { role.getName() }) + ); verifyNoMoreInteractions(auditTrail); verify(clusterService).state(); verify(state, times(1)).metadata(); @@ -1031,30 +1248,48 @@ public void testCreateIndexWithAlias() throws IOException { CreateIndexRequest request = new CreateIndexRequest("a"); request.alias(new Alias("a2")); ClusterState state = mockEmptyMetadata(); - RoleDescriptor role = new RoleDescriptor("a_all", null, - new IndicesPrivileges[]{IndicesPrivileges.builder().indices("a", "a2").privileges("all").build()}, null); + RoleDescriptor role = new RoleDescriptor( + "a_all", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices("a", "a2").privileges("all").build() }, + null + ); final Authentication authentication = createAuthentication(new User("test user", "a_all")); roleMap.put("a_all", role); final String requestId = AuditUtil.getOrGenerateRequestId(threadContext); authorize(authentication, CreateIndexAction.NAME, request); - verify(auditTrail).accessGranted(eq(requestId), eq(authentication), eq(CreateIndexAction.NAME), eq(request), - authzInfoRoles(new String[]{role.getName()})); - verify(auditTrail).accessGranted(eq(requestId), eq(authentication), eq("indices:admin/aliases"), eq(request), - authzInfoRoles(new String[]{role.getName()})); + verify(auditTrail).accessGranted( + eq(requestId), + eq(authentication), + eq(CreateIndexAction.NAME), + eq(request), + authzInfoRoles(new String[] { role.getName() }) + ); + verify(auditTrail).accessGranted( + eq(requestId), + eq(authentication), + eq("indices:admin/aliases"), + eq(request), + authzInfoRoles(new String[] { role.getName() }) + ); verifyNoMoreInteractions(auditTrail); verify(clusterService).state(); verify(state, times(1)).metadata(); } public void testDenialErrorMessagesForSearchAction() throws IOException { - RoleDescriptor indexRole = new RoleDescriptor("some_indices_" + randomAlphaOfLengthBetween(3, 6), null, new IndicesPrivileges[]{ - IndicesPrivileges.builder().indices("all*").privileges("all").build(), - IndicesPrivileges.builder().indices("read*").privileges("read").build(), - IndicesPrivileges.builder().indices("write*").privileges("write").build() - }, null); - RoleDescriptor emptyRole = new RoleDescriptor("empty_role_" + randomAlphaOfLengthBetween(1,4), null, null, null); + RoleDescriptor indexRole = new RoleDescriptor( + "some_indices_" + randomAlphaOfLengthBetween(3, 6), + null, + new IndicesPrivileges[] { + IndicesPrivileges.builder().indices("all*").privileges("all").build(), + IndicesPrivileges.builder().indices("read*").privileges("read").build(), + IndicesPrivileges.builder().indices("write*").privileges("write").build() }, + null + ); + RoleDescriptor emptyRole = new RoleDescriptor("empty_role_" + randomAlphaOfLengthBetween(1, 4), null, null, null); User user = new User(randomAlphaOfLengthBetween(6, 8), indexRole.getName(), emptyRole.getName()); final Authentication authentication = createAuthentication(user); roleMap.put(indexRole.getName(), indexRole); @@ -1064,13 +1299,29 @@ public void testDenialErrorMessagesForSearchAction() throws IOException { TransportRequest request = new SearchRequest("all-1", "read-2", "write-3", "other-4"); - ElasticsearchSecurityException securityException = expectThrows(ElasticsearchSecurityException.class, - () -> authorize(authentication, SearchAction.NAME, request)); - assertThat(securityException, throwableWithMessage(containsString( - "[" + SearchAction.NAME + "] is unauthorized" + - " for user [" + user.principal() + "]" + - " with roles [" + indexRole.getName() + "," + emptyRole.getName() + "]" + - " on indices ["))); + ElasticsearchSecurityException securityException = expectThrows( + ElasticsearchSecurityException.class, + () -> authorize(authentication, SearchAction.NAME, request) + ); + assertThat( + securityException, + throwableWithMessage( + containsString( + "[" + + SearchAction.NAME + + "] is unauthorized" + + " for user [" + + user.principal() + + "]" + + " with roles [" + + indexRole.getName() + + "," + + emptyRole.getName() + + "]" + + " on indices [" + ) + ) + ); assertThat(securityException, throwableWithMessage(containsString("write-3"))); assertThat(securityException, throwableWithMessage(containsString("other-4"))); assertThat(securityException, throwableWithMessage(not(containsString("all-1")))); @@ -1080,9 +1331,12 @@ public void testDenialErrorMessagesForSearchAction() throws IOException { public void testDenialErrorMessagesForBulkIngest() throws Exception { final String index = randomAlphaOfLengthBetween(5, 12); - RoleDescriptor role = new RoleDescriptor("some_indices_" + randomAlphaOfLengthBetween(3, 6), null, new IndicesPrivileges[]{ - IndicesPrivileges.builder().indices(index).privileges(BulkAction.NAME).build() - }, null); + RoleDescriptor role = new RoleDescriptor( + "some_indices_" + randomAlphaOfLengthBetween(3, 6), + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices(index).privileges(BulkAction.NAME).build() }, + null + ); User user = new User(randomAlphaOfLengthBetween(6, 8), role.getName()); final Authentication authentication = createAuthentication(user); roleMap.put(role.getName(), role); @@ -1092,13 +1346,17 @@ public void testDenialErrorMessagesForBulkIngest() throws Exception { final BulkShardRequest request = new BulkShardRequest( new ShardId(index, randomAlphaOfLength(24), 1), WriteRequest.RefreshPolicy.NONE, - new BulkItemRequest[]{ - new BulkItemRequest(0, - new IndexRequest(index).id("doc-1").opType(DocWriteRequest.OpType.CREATE).source(Map.of("field", "value"))), - new BulkItemRequest(1, - new IndexRequest(index).id("doc-2").opType(DocWriteRequest.OpType.INDEX).source(Map.of("field", "value"))), - new BulkItemRequest(2, new DeleteRequest(index, "doc-3")) - }); + new BulkItemRequest[] { + new BulkItemRequest( + 0, + new IndexRequest(index).id("doc-1").opType(DocWriteRequest.OpType.CREATE).source(Map.of("field", "value")) + ), + new BulkItemRequest( + 1, + new IndexRequest(index).id("doc-2").opType(DocWriteRequest.OpType.INDEX).source(Map.of("field", "value")) + ), + new BulkItemRequest(2, new DeleteRequest(index, "doc-3")) } + ); authorize(authentication, TransportShardBulkAction.ACTION_NAME, request); @@ -1108,8 +1366,17 @@ public void testDenialErrorMessagesForBulkIngest() throws Exception { IndexShard indexShard = mock(IndexShard.class); when(indexShard.getBulkOperationListener()).thenReturn(new BulkOperationListener() { }); - TransportShardBulkAction.performOnPrimary(request, indexShard, new UpdateHelper(mock(ScriptService.class)), - System::currentTimeMillis, mappingUpdater, waitForMappingUpdate, future, threadPool, Names.WRITE); + TransportShardBulkAction.performOnPrimary( + request, + indexShard, + new UpdateHelper(mock(ScriptService.class)), + System::currentTimeMillis, + mappingUpdater, + waitForMappingUpdate, + future, + threadPool, + Names.WRITE + ); TransportReplicationAction.PrimaryResult result = future.get(); BulkShardResponse response = result.finalResponseIfSuccessful; @@ -1123,9 +1390,12 @@ public void testDenialErrorMessagesForBulkIngest() throws Exception { } public void testDenialErrorMessagesForClusterHealthAction() throws IOException { - RoleDescriptor role = new RoleDescriptor("role_" + randomAlphaOfLengthBetween(3, 6), + RoleDescriptor role = new RoleDescriptor( + "role_" + randomAlphaOfLengthBetween(3, 6), new String[0], // no cluster privileges - new IndicesPrivileges[]{IndicesPrivileges.builder().indices("index-*").privileges("all").build()}, null); + new IndicesPrivileges[] { IndicesPrivileges.builder().indices("index-*").privileges("all").build() }, + null + ); User user = new User(randomAlphaOfLengthBetween(6, 8), role.getName()); final Authentication authentication = createAuthentication(user); roleMap.put(role.getName(), role); @@ -1134,12 +1404,18 @@ public void testDenialErrorMessagesForClusterHealthAction() throws IOException { TransportRequest request = new ClusterHealthRequest(); - ElasticsearchSecurityException securityException = expectThrows(ElasticsearchSecurityException.class, - () -> authorize(authentication, ClusterHealthAction.NAME, request)); - assertThat(securityException, throwableWithMessage( - containsString("[" + ClusterHealthAction.NAME + "] is unauthorized for user [" + user.principal() + "]"))); - assertThat(securityException, - throwableWithMessage(containsString("this action is granted by the cluster privileges [monitor,manage,all]"))); + ElasticsearchSecurityException securityException = expectThrows( + ElasticsearchSecurityException.class, + () -> authorize(authentication, ClusterHealthAction.NAME, request) + ); + assertThat( + securityException, + throwableWithMessage(containsString("[" + ClusterHealthAction.NAME + "] is unauthorized for user [" + user.principal() + "]")) + ); + assertThat( + securityException, + throwableWithMessage(containsString("this action is granted by the cluster privileges [monitor,manage,all]")) + ); } /** @@ -1148,10 +1424,12 @@ public void testDenialErrorMessagesForClusterHealthAction() throws IOException { * This test case checks that the error message for these actions handles this edge-case. */ public void testDenialErrorMessagesForIndexTemplateAction() throws IOException { - RoleDescriptor role = new RoleDescriptor("role_" + randomAlphaOfLengthBetween(3, 6), + RoleDescriptor role = new RoleDescriptor( + "role_" + randomAlphaOfLengthBetween(3, 6), new String[0], // no cluster privileges new IndicesPrivileges[0], // no index privileges - null); + null + ); User user = new User(randomAlphaOfLengthBetween(6, 8), role.getName()); final Authentication authentication = createAuthentication(user); roleMap.put(role.getName(), role); @@ -1160,18 +1438,29 @@ public void testDenialErrorMessagesForIndexTemplateAction() throws IOException { TransportRequest request = new PutIndexTemplateRequest(randomAlphaOfLengthBetween(4, 20)); - ElasticsearchSecurityException securityException = expectThrows(ElasticsearchSecurityException.class, - () -> authorize(authentication, PutIndexTemplateAction.NAME, request)); - assertThat(securityException, throwableWithMessage( - containsString("[" + PutIndexTemplateAction.NAME + "] is unauthorized for user [" + user.principal() + "]"))); - assertThat(securityException, - throwableWithMessage(containsString("this action is granted by the cluster privileges [manage_index_templates,manage,all]"))); + ElasticsearchSecurityException securityException = expectThrows( + ElasticsearchSecurityException.class, + () -> authorize(authentication, PutIndexTemplateAction.NAME, request) + ); + assertThat( + securityException, + throwableWithMessage( + containsString("[" + PutIndexTemplateAction.NAME + "] is unauthorized for user [" + user.principal() + "]") + ) + ); + assertThat( + securityException, + throwableWithMessage(containsString("this action is granted by the cluster privileges [manage_index_templates,manage,all]")) + ); } public void testDenialErrorMessagesForInvalidateApiKeyAction() throws IOException { - RoleDescriptor role = new RoleDescriptor("role_" + randomAlphaOfLengthBetween(3, 6), + RoleDescriptor role = new RoleDescriptor( + "role_" + randomAlphaOfLengthBetween(3, 6), new String[0], // no cluster privileges - new IndicesPrivileges[]{IndicesPrivileges.builder().indices("index-*").privileges("all").build()}, null); + new IndicesPrivileges[] { IndicesPrivileges.builder().indices("index-*").privileges("all").build() }, + null + ); User user = new User(randomAlphaOfLengthBetween(6, 8), role.getName()); final Authentication authentication = createAuthentication(user); roleMap.put(role.getName(), role); @@ -1182,25 +1471,46 @@ public void testDenialErrorMessagesForInvalidateApiKeyAction() throws IOExceptio { TransportRequest request = new InvalidateApiKeyRequest(null, null, null, true, null); - ElasticsearchSecurityException securityException = expectThrows(ElasticsearchSecurityException.class, - () -> authorize(authentication, InvalidateApiKeyAction.NAME, request)); - assertThat(securityException, throwableWithMessage( - containsString("[" + InvalidateApiKeyAction.NAME + "] is unauthorized for user [" + user.principal() + "]"))); - assertThat(securityException, throwableWithMessage( - containsString("this action is granted by the cluster privileges [manage_own_api_key,manage_api_key,manage_security,all]") - )); + ElasticsearchSecurityException securityException = expectThrows( + ElasticsearchSecurityException.class, + () -> authorize(authentication, InvalidateApiKeyAction.NAME, request) + ); + assertThat( + securityException, + throwableWithMessage( + containsString("[" + InvalidateApiKeyAction.NAME + "] is unauthorized for user [" + user.principal() + "]") + ) + ); + assertThat( + securityException, + throwableWithMessage( + containsString( + "this action is granted by the cluster privileges [manage_own_api_key,manage_api_key,manage_security,all]" + ) + ) + ); } // All API Keys { TransportRequest request = new InvalidateApiKeyRequest(null, null, null, false, null); - ElasticsearchSecurityException securityException = expectThrows(ElasticsearchSecurityException.class, - () -> authorize(authentication, InvalidateApiKeyAction.NAME, request)); - assertThat(securityException, throwableWithMessage( - containsString("[" + InvalidateApiKeyAction.NAME + "] is unauthorized for user [" + user.principal() + "]"))); - assertThat(securityException, throwableWithMessage( - containsString("this action is granted by the cluster privileges [manage_api_key,manage_security,all]"))); + ElasticsearchSecurityException securityException = expectThrows( + ElasticsearchSecurityException.class, + () -> authorize(authentication, InvalidateApiKeyAction.NAME, request) + ); + assertThat( + securityException, + throwableWithMessage( + containsString("[" + InvalidateApiKeyAction.NAME + "] is unauthorized for user [" + user.principal() + "]") + ) + ); + assertThat( + securityException, + throwableWithMessage( + containsString("this action is granted by the cluster privileges [manage_api_key,manage_security,all]") + ) + ); } } @@ -1209,21 +1519,39 @@ public void testDenialForAnonymousUser() throws IOException { ClusterState state = mockEmptyMetadata(); Settings settings = Settings.builder().put(AnonymousUser.ROLES_SETTING.getKey(), "a_all").build(); final AnonymousUser anonymousUser = new AnonymousUser(settings); - authorizationService = new AuthorizationService(settings, rolesStore, clusterService, auditTrailService, - new DefaultAuthenticationFailureHandler(Collections.emptyMap()), threadPool, anonymousUser, null, Collections.emptySet(), - new XPackLicenseState(() -> 0), TestIndexNameExpressionResolver.newInstance(), operatorPrivilegesService); + authorizationService = new AuthorizationService( + settings, + rolesStore, + clusterService, + auditTrailService, + new DefaultAuthenticationFailureHandler(Collections.emptyMap()), + threadPool, + anonymousUser, + null, + Collections.emptySet(), + new XPackLicenseState(() -> 0), + TestIndexNameExpressionResolver.newInstance(), + operatorPrivilegesService + ); - RoleDescriptor role = new RoleDescriptor("a_all", null, - new IndicesPrivileges[]{IndicesPrivileges.builder().indices("a").privileges("all").build()}, null); + RoleDescriptor role = new RoleDescriptor( + "a_all", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices("a").privileges("all").build() }, + null + ); roleMap.put("a_all", role); final String requestId = AuditUtil.getOrGenerateRequestId(threadContext); final Authentication authentication = createAuthentication(anonymousUser); - assertThrowsAuthorizationException( - () -> authorize(authentication, "indices:a", request), - "indices:a", anonymousUser.principal()); - verify(auditTrail).accessDenied(eq(requestId), eq(authentication), eq("indices:a"), eq(request), - authzInfoRoles(new String[]{role.getName()})); + assertThrowsAuthorizationException(() -> authorize(authentication, "indices:a", request), "indices:a", anonymousUser.principal()); + verify(auditTrail).accessDenied( + eq(requestId), + eq(authentication), + eq("indices:a"), + eq(request), + authzInfoRoles(new String[] { role.getName() }) + ); verifyNoMoreInteractions(auditTrail); verify(clusterService, times(1)).state(); verify(state, times(1)).metadata(); @@ -1237,21 +1565,42 @@ public void testDenialForAnonymousUserAuthorizationExceptionDisabled() throws IO .put(AuthorizationService.ANONYMOUS_AUTHORIZATION_EXCEPTION_SETTING.getKey(), false) .build(); final Authentication authentication = createAuthentication(new AnonymousUser(settings)); - authorizationService = new AuthorizationService(settings, rolesStore, clusterService, auditTrailService, - new DefaultAuthenticationFailureHandler(Collections.emptyMap()), threadPool, new AnonymousUser(settings), null, - Collections.emptySet(), new XPackLicenseState(() -> 0), TestIndexNameExpressionResolver.newInstance(), - operatorPrivilegesService); + authorizationService = new AuthorizationService( + settings, + rolesStore, + clusterService, + auditTrailService, + new DefaultAuthenticationFailureHandler(Collections.emptyMap()), + threadPool, + new AnonymousUser(settings), + null, + Collections.emptySet(), + new XPackLicenseState(() -> 0), + TestIndexNameExpressionResolver.newInstance(), + operatorPrivilegesService + ); - RoleDescriptor role = new RoleDescriptor("a_all", null, - new IndicesPrivileges[]{IndicesPrivileges.builder().indices("a").privileges("all").build()}, null); + RoleDescriptor role = new RoleDescriptor( + "a_all", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices("a").privileges("all").build() }, + null + ); roleMap.put("a_all", role); final String requestId = AuditUtil.getOrGenerateRequestId(threadContext); - final ElasticsearchSecurityException securityException = expectThrows(ElasticsearchSecurityException.class, - () -> authorize(authentication, "indices:a", request)); + final ElasticsearchSecurityException securityException = expectThrows( + ElasticsearchSecurityException.class, + () -> authorize(authentication, "indices:a", request) + ); assertAuthenticationException(securityException, containsString("action [indices:a] requires authentication")); - verify(auditTrail).accessDenied(eq(requestId), eq(authentication), eq("indices:a"), eq(request), - authzInfoRoles(new String[]{role.getName()})); + verify(auditTrail).accessDenied( + eq(requestId), + eq(authentication), + eq("indices:a"), + eq(request), + authzInfoRoles(new String[] { role.getName() }) + ); verifyNoMoreInteractions(auditTrail); verify(clusterService, times(1)).state(); verify(state, times(1)).metadata(); @@ -1261,19 +1610,29 @@ public void testAuditTrailIsRecordedWhenIndexWildcardThrowsError() throws IOExce IndicesOptions options = IndicesOptions.fromOptions(false, false, true, true); TransportRequest request = new GetIndexRequest().indices("not-an-index-*").indicesOptions(options); ClusterState state = mockEmptyMetadata(); - RoleDescriptor role = new RoleDescriptor("a_all", null, - new IndicesPrivileges[]{IndicesPrivileges.builder().indices("a").privileges("all").build()}, null); + RoleDescriptor role = new RoleDescriptor( + "a_all", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices("a").privileges("all").build() }, + null + ); final Authentication authentication = createAuthentication(new User("test user", "a_all")); roleMap.put("a_all", role); final String requestId = AuditUtil.getOrGenerateRequestId(threadContext); final IndexNotFoundException nfe = expectThrows( IndexNotFoundException.class, - () -> authorize(authentication, GetIndexAction.NAME, request)); + () -> authorize(authentication, GetIndexAction.NAME, request) + ); assertThat(nfe.getIndex(), is(notNullValue())); assertThat(nfe.getIndex().getName(), is("not-an-index-*")); - verify(auditTrail).accessDenied(eq(requestId), eq(authentication), eq(GetIndexAction.NAME), eq(request), - authzInfoRoles(new String[]{role.getName()})); + verify(auditTrail).accessDenied( + eq(requestId), + eq(authentication), + eq(GetIndexAction.NAME), + eq(request), + authzInfoRoles(new String[] { role.getName() }) + ); verifyNoMoreInteractions(auditTrail); verify(clusterService).state(); verify(state, times(1)).metadata(); @@ -1286,9 +1645,11 @@ public void testRunAsRequestWithNoRolesUser() throws IOException { assertNotEquals(authentication.getUser().authenticatedUser(), authentication); assertThrowsAuthorizationExceptionRunAs( () -> authorize(authentication, "indices:a", request), - "indices:a", "test user", "run as me"); // run as [run as me] - verify(auditTrail).runAsDenied(eq(requestId), eq(authentication), eq("indices:a"), eq(request), - authzInfoRoles(Role.EMPTY.names())); + "indices:a", + "test user", + "run as me" + ); // run as [run as me] + verify(auditTrail).runAsDenied(eq(requestId), eq(authentication), eq("indices:a"), eq(request), authzInfoRoles(Role.EMPTY.names())); verifyNoMoreInteractions(auditTrail); } @@ -1296,50 +1657,76 @@ public void testRunAsRequestWithoutLookedUpBy() throws IOException { final String requestId = AuditUtil.getOrGenerateRequestId(threadContext); AuthenticateRequest request = new AuthenticateRequest("run as me"); roleMap.put("superuser", ReservedRolesStore.SUPERUSER_ROLE_DESCRIPTOR); - User user = new User("run as me", Strings.EMPTY_ARRAY, new User("test user", new String[]{"superuser"})); + User user = new User("run as me", Strings.EMPTY_ARRAY, new User("test user", new String[] { "superuser" })); Authentication authentication = new Authentication(user, new RealmRef("foo", "bar", "baz"), null); authentication.writeToContext(threadContext); assertNotEquals(user.authenticatedUser(), user); assertThrowsAuthorizationExceptionRunAs( () -> authorize(authentication, AuthenticateAction.NAME, request), - AuthenticateAction.NAME, "test user", "run as me"); // run as [run as me] - verify(auditTrail).runAsDenied(eq(requestId), eq(authentication), eq(AuthenticateAction.NAME), eq(request), - authzInfoRoles(new String[]{ReservedRolesStore.SUPERUSER_ROLE_DESCRIPTOR.getName()})); + AuthenticateAction.NAME, + "test user", + "run as me" + ); // run as [run as me] + verify(auditTrail).runAsDenied( + eq(requestId), + eq(authentication), + eq(AuthenticateAction.NAME), + eq(request), + authzInfoRoles(new String[] { ReservedRolesStore.SUPERUSER_ROLE_DESCRIPTOR.getName() }) + ); verifyNoMoreInteractions(auditTrail); } public void testRunAsRequestRunningAsUnAllowedUser() throws IOException { TransportRequest request = mock(TransportRequest.class); - User user = new User("run as me", new String[]{"doesn't exist"}, new User("test user", "can run as")); + User user = new User("run as me", new String[] { "doesn't exist" }, new User("test user", "can run as")); assertNotEquals(user.authenticatedUser(), user); final Authentication authentication = createAuthentication(user); - final RoleDescriptor role = new RoleDescriptor("can run as", null, - new IndicesPrivileges[]{IndicesPrivileges.builder().indices("a").privileges("all").build()}, - new String[]{"not the right user"}); + final RoleDescriptor role = new RoleDescriptor( + "can run as", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices("a").privileges("all").build() }, + new String[] { "not the right user" } + ); roleMap.put("can run as", role); final String requestId = AuditUtil.getOrGenerateRequestId(threadContext); assertThrowsAuthorizationExceptionRunAs( () -> authorize(authentication, "indices:a", request), - "indices:a", "test user", "run as me"); - verify(auditTrail).runAsDenied(eq(requestId), eq(authentication), eq("indices:a"), eq(request), - authzInfoRoles(new String[]{role.getName()})); + "indices:a", + "test user", + "run as me" + ); + verify(auditTrail).runAsDenied( + eq(requestId), + eq(authentication), + eq("indices:a"), + eq(request), + authzInfoRoles(new String[] { role.getName() }) + ); verifyNoMoreInteractions(auditTrail); } public void testRunAsRequestWithRunAsUserWithoutPermission() throws IOException { TransportRequest request = new GetIndexRequest().indices("a"); User authenticatedUser = new User("test user", "can run as"); - User user = new User("run as me", new String[]{"b"}, authenticatedUser); + User user = new User("run as me", new String[] { "b" }, authenticatedUser); assertNotEquals(user.authenticatedUser(), user); final Authentication authentication = createAuthentication(user); - final RoleDescriptor runAsRole = new RoleDescriptor("can run as", null, - new IndicesPrivileges[]{IndicesPrivileges.builder().indices("a").privileges("all").build()}, - new String[]{"run as me"}); + final RoleDescriptor runAsRole = new RoleDescriptor( + "can run as", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices("a").privileges("all").build() }, + new String[] { "run as me" } + ); roleMap.put("can run as", runAsRole); - RoleDescriptor bRole = new RoleDescriptor("b", null, - new IndicesPrivileges[]{IndicesPrivileges.builder().indices("b").privileges("all").build()}, null); + RoleDescriptor bRole = new RoleDescriptor( + "b", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices("b").privileges("all").build() }, + null + ); boolean indexExists = randomBoolean(); if (indexExists) { mockMetadataWithIndex("a"); @@ -1351,107 +1738,197 @@ public void testRunAsRequestWithRunAsUserWithoutPermission() throws IOException assertThrowsAuthorizationExceptionRunAs( () -> authorize(authentication, "indices:a", request), - "indices:a", "test user", "run as me"); - verify(auditTrail).runAsGranted(eq(requestId), eq(authentication), eq("indices:a"), eq(request), - authzInfoRoles(new String[]{runAsRole.getName()})); + "indices:a", + "test user", + "run as me" + ); + verify(auditTrail).runAsGranted( + eq(requestId), + eq(authentication), + eq("indices:a"), + eq(request), + authzInfoRoles(new String[] { runAsRole.getName() }) + ); if (indexExists) { - verify(auditTrail).accessDenied(eq(requestId), eq(authentication), eq("indices:a"), eq(request), - authzInfoRoles(new String[]{bRole.getName()})); + verify(auditTrail).accessDenied( + eq(requestId), + eq(authentication), + eq("indices:a"), + eq(request), + authzInfoRoles(new String[] { bRole.getName() }) + ); } else { - verify(auditTrail).accessDenied(eq(requestId), eq(authentication), eq("indices:a"), eq(request), - authzInfoRoles(Role.EMPTY.names())); + verify(auditTrail).accessDenied( + eq(requestId), + eq(authentication), + eq("indices:a"), + eq(request), + authzInfoRoles(Role.EMPTY.names()) + ); } verifyNoMoreInteractions(auditTrail); } public void testRunAsRequestWithValidPermissions() throws IOException { TransportRequest request = new GetIndexRequest().indices("b"); - User authenticatedUser = new User("test user", new String[]{"can run as"}); - User user = new User("run as me", new String[]{"b"}, authenticatedUser); + User authenticatedUser = new User("test user", new String[] { "can run as" }); + User user = new User("run as me", new String[] { "b" }, authenticatedUser); assertNotEquals(user.authenticatedUser(), user); final Authentication authentication = createAuthentication(user); - final RoleDescriptor runAsRole = new RoleDescriptor("can run as", null, - new IndicesPrivileges[]{IndicesPrivileges.builder().indices("a").privileges("all").build()}, - new String[]{"run as me"}); + final RoleDescriptor runAsRole = new RoleDescriptor( + "can run as", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices("a").privileges("all").build() }, + new String[] { "run as me" } + ); roleMap.put("can run as", runAsRole); mockMetadataWithIndex("b"); - RoleDescriptor bRole = new RoleDescriptor("b", null, - new IndicesPrivileges[]{IndicesPrivileges.builder().indices("b").privileges("all").build()}, null); + RoleDescriptor bRole = new RoleDescriptor( + "b", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices("b").privileges("all").build() }, + null + ); roleMap.put("b", bRole); final String requestId = AuditUtil.getOrGenerateRequestId(threadContext); authorize(authentication, "indices:a", request); - verify(auditTrail).runAsGranted(eq(requestId), eq(authentication), eq("indices:a"), eq(request), - authzInfoRoles(new String[]{runAsRole.getName()})); - verify(auditTrail).accessGranted(eq(requestId), eq(authentication), eq("indices:a"), eq(request), - authzInfoRoles(new String[]{bRole.getName()})); + verify(auditTrail).runAsGranted( + eq(requestId), + eq(authentication), + eq("indices:a"), + eq(request), + authzInfoRoles(new String[] { runAsRole.getName() }) + ); + verify(auditTrail).accessGranted( + eq(requestId), + eq(authentication), + eq("indices:a"), + eq(request), + authzInfoRoles(new String[] { bRole.getName() }) + ); verifyNoMoreInteractions(auditTrail); } public void testGrantAllRestrictedUserCannotExecuteOperationAgainstSecurityIndices() throws IOException { - RoleDescriptor role = new RoleDescriptor("all access", new String[]{"all"}, - new IndicesPrivileges[]{IndicesPrivileges.builder().indices("*").privileges("all").build()}, null); + RoleDescriptor role = new RoleDescriptor( + "all access", + new String[] { "all" }, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices("*").privileges("all").build() }, + null + ); final Authentication authentication = createAuthentication(new User("all_access_user", "all_access")); roleMap.put("all_access", role); - ClusterState state = mockClusterState(Metadata.builder() - .put(new IndexMetadata.Builder(INTERNAL_SECURITY_MAIN_INDEX_7) - .putAlias(new AliasMetadata.Builder(SECURITY_MAIN_ALIAS).build()) - .settings(Settings.builder().put("index.version.created", Version.CURRENT).build()) - .numberOfShards(1) - .numberOfReplicas(0) - .build(), true) - .build()); + ClusterState state = mockClusterState( + Metadata.builder() + .put( + new IndexMetadata.Builder(INTERNAL_SECURITY_MAIN_INDEX_7).putAlias( + new AliasMetadata.Builder(SECURITY_MAIN_ALIAS).build() + ) + .settings(Settings.builder().put("index.version.created", Version.CURRENT).build()) + .numberOfShards(1) + .numberOfReplicas(0) + .build(), + true + ) + .build() + ); final String requestId = AuditUtil.getOrGenerateRequestId(threadContext); List> requests = new ArrayList<>(); - requests.add(new Tuple<>(BulkAction.NAME + "[s]", - new DeleteRequest(randomFrom(SECURITY_MAIN_ALIAS, INTERNAL_SECURITY_MAIN_INDEX_7), "id"))); requests.add( - new Tuple<>(UpdateAction.NAME, new UpdateRequest(randomFrom(SECURITY_MAIN_ALIAS, INTERNAL_SECURITY_MAIN_INDEX_7), "id"))); + new Tuple<>(BulkAction.NAME + "[s]", new DeleteRequest(randomFrom(SECURITY_MAIN_ALIAS, INTERNAL_SECURITY_MAIN_INDEX_7), "id")) + ); + requests.add( + new Tuple<>(UpdateAction.NAME, new UpdateRequest(randomFrom(SECURITY_MAIN_ALIAS, INTERNAL_SECURITY_MAIN_INDEX_7), "id")) + ); requests.add( - new Tuple<>(BulkAction.NAME + "[s]", new IndexRequest(randomFrom(SECURITY_MAIN_ALIAS, INTERNAL_SECURITY_MAIN_INDEX_7)))); + new Tuple<>(BulkAction.NAME + "[s]", new IndexRequest(randomFrom(SECURITY_MAIN_ALIAS, INTERNAL_SECURITY_MAIN_INDEX_7))) + ); requests.add(new Tuple<>(SearchAction.NAME, new SearchRequest(randomFrom(SECURITY_MAIN_ALIAS, INTERNAL_SECURITY_MAIN_INDEX_7)))); - requests.add(new Tuple<>(TermVectorsAction.NAME, - new TermVectorsRequest(randomFrom(SECURITY_MAIN_ALIAS, INTERNAL_SECURITY_MAIN_INDEX_7), "id"))); + requests.add( + new Tuple<>( + TermVectorsAction.NAME, + new TermVectorsRequest(randomFrom(SECURITY_MAIN_ALIAS, INTERNAL_SECURITY_MAIN_INDEX_7), "id") + ) + ); requests.add(new Tuple<>(GetAction.NAME, new GetRequest(randomFrom(SECURITY_MAIN_ALIAS, INTERNAL_SECURITY_MAIN_INDEX_7), "id"))); - requests.add(new Tuple<>(IndicesAliasesAction.NAME, new IndicesAliasesRequest() - .addAliasAction(AliasActions.add().alias("security_alias").index(INTERNAL_SECURITY_MAIN_INDEX_7)))); - requests.add(new Tuple<>(UpdateSettingsAction.NAME, - new UpdateSettingsRequest().indices(randomFrom(SECURITY_MAIN_ALIAS, INTERNAL_SECURITY_MAIN_INDEX_7)))); + requests.add( + new Tuple<>( + IndicesAliasesAction.NAME, + new IndicesAliasesRequest().addAliasAction(AliasActions.add().alias("security_alias").index(INTERNAL_SECURITY_MAIN_INDEX_7)) + ) + ); + requests.add( + new Tuple<>( + UpdateSettingsAction.NAME, + new UpdateSettingsRequest().indices(randomFrom(SECURITY_MAIN_ALIAS, INTERNAL_SECURITY_MAIN_INDEX_7)) + ) + ); // cannot execute monitor operations - requests.add(new Tuple<>(IndicesStatsAction.NAME, - new IndicesStatsRequest().indices(randomFrom(SECURITY_MAIN_ALIAS, INTERNAL_SECURITY_MAIN_INDEX_7)))); - requests.add(new Tuple<>(RecoveryAction.NAME, - new RecoveryRequest().indices(randomFrom(SECURITY_MAIN_ALIAS, INTERNAL_SECURITY_MAIN_INDEX_7)))); - requests.add(new Tuple<>(IndicesSegmentsAction.NAME, - new IndicesSegmentsRequest().indices(randomFrom(SECURITY_MAIN_ALIAS, INTERNAL_SECURITY_MAIN_INDEX_7)))); - requests.add(new Tuple<>(GetSettingsAction.NAME, - new GetSettingsRequest().indices(randomFrom(SECURITY_MAIN_ALIAS, INTERNAL_SECURITY_MAIN_INDEX_7)))); - requests.add(new Tuple<>(IndicesShardStoresAction.NAME, - new IndicesShardStoresRequest().indices(randomFrom(SECURITY_MAIN_ALIAS, INTERNAL_SECURITY_MAIN_INDEX_7)))); + requests.add( + new Tuple<>( + IndicesStatsAction.NAME, + new IndicesStatsRequest().indices(randomFrom(SECURITY_MAIN_ALIAS, INTERNAL_SECURITY_MAIN_INDEX_7)) + ) + ); + requests.add( + new Tuple<>(RecoveryAction.NAME, new RecoveryRequest().indices(randomFrom(SECURITY_MAIN_ALIAS, INTERNAL_SECURITY_MAIN_INDEX_7))) + ); + requests.add( + new Tuple<>( + IndicesSegmentsAction.NAME, + new IndicesSegmentsRequest().indices(randomFrom(SECURITY_MAIN_ALIAS, INTERNAL_SECURITY_MAIN_INDEX_7)) + ) + ); + requests.add( + new Tuple<>( + GetSettingsAction.NAME, + new GetSettingsRequest().indices(randomFrom(SECURITY_MAIN_ALIAS, INTERNAL_SECURITY_MAIN_INDEX_7)) + ) + ); + requests.add( + new Tuple<>( + IndicesShardStoresAction.NAME, + new IndicesShardStoresRequest().indices(randomFrom(SECURITY_MAIN_ALIAS, INTERNAL_SECURITY_MAIN_INDEX_7)) + ) + ); for (Tuple requestTuple : requests) { String action = requestTuple.v1(); TransportRequest request = requestTuple.v2(); - assertThrowsAuthorizationException( - () -> authorize(authentication, action, request), - action, "all_access_user"); - verify(auditTrail).accessDenied(eq(requestId), eq(authentication), eq(action), eq(request), - authzInfoRoles(new String[]{role.getName()})); + assertThrowsAuthorizationException(() -> authorize(authentication, action, request), action, "all_access_user"); + verify(auditTrail).accessDenied( + eq(requestId), + eq(authentication), + eq(action), + eq(request), + authzInfoRoles(new String[] { role.getName() }) + ); verifyNoMoreInteractions(auditTrail); } // we should allow waiting for the health of the index or any index if the user has this permission ClusterHealthRequest request = new ClusterHealthRequest(randomFrom(SECURITY_MAIN_ALIAS, INTERNAL_SECURITY_MAIN_INDEX_7)); authorize(authentication, ClusterHealthAction.NAME, request); - verify(auditTrail).accessGranted(eq(requestId), eq(authentication), eq(ClusterHealthAction.NAME), eq(request), - authzInfoRoles(new String[]{role.getName()})); + verify(auditTrail).accessGranted( + eq(requestId), + eq(authentication), + eq(ClusterHealthAction.NAME), + eq(request), + authzInfoRoles(new String[] { role.getName() }) + ); // multiple indices request = new ClusterHealthRequest(SECURITY_MAIN_ALIAS, INTERNAL_SECURITY_MAIN_INDEX_7, "foo", "bar"); authorize(authentication, ClusterHealthAction.NAME, request); - verify(auditTrail).accessGranted(eq(requestId), eq(authentication), eq(ClusterHealthAction.NAME), eq(request), - authzInfoRoles(new String[]{role.getName()})); + verify(auditTrail).accessGranted( + eq(requestId), + eq(authentication), + eq(ClusterHealthAction.NAME), + eq(request), + authzInfoRoles(new String[] { role.getName() }) + ); verifyNoMoreInteractions(auditTrail); final SearchRequest searchRequest = new SearchRequest("_all"); @@ -1461,20 +1938,34 @@ public void testGrantAllRestrictedUserCannotExecuteOperationAgainstSecurityIndic } public void testMonitoringOperationsAgainstSecurityIndexRequireAllowRestricted() throws IOException { - final RoleDescriptor restrictedMonitorRole = new RoleDescriptor("restricted_monitor", null, - new IndicesPrivileges[]{IndicesPrivileges.builder().indices("*").privileges("monitor").build()}, null); - final RoleDescriptor unrestrictedMonitorRole = new RoleDescriptor("unrestricted_monitor", null, new IndicesPrivileges[]{ - IndicesPrivileges.builder().indices("*").privileges("monitor").allowRestrictedIndices(true).build()}, null); + final RoleDescriptor restrictedMonitorRole = new RoleDescriptor( + "restricted_monitor", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices("*").privileges("monitor").build() }, + null + ); + final RoleDescriptor unrestrictedMonitorRole = new RoleDescriptor( + "unrestricted_monitor", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices("*").privileges("monitor").allowRestrictedIndices(true).build() }, + null + ); roleMap.put("restricted_monitor", restrictedMonitorRole); roleMap.put("unrestricted_monitor", unrestrictedMonitorRole); - ClusterState state = mockClusterState(Metadata.builder() - .put(new IndexMetadata.Builder(INTERNAL_SECURITY_MAIN_INDEX_7) - .putAlias(new AliasMetadata.Builder(SECURITY_MAIN_ALIAS).build()) - .settings(Settings.builder().put("index.version.created", Version.CURRENT).build()) - .numberOfShards(1) - .numberOfReplicas(0) - .build(), true) - .build()); + ClusterState state = mockClusterState( + Metadata.builder() + .put( + new IndexMetadata.Builder(INTERNAL_SECURITY_MAIN_INDEX_7).putAlias( + new AliasMetadata.Builder(SECURITY_MAIN_ALIAS).build() + ) + .settings(Settings.builder().put("index.version.created", Version.CURRENT).build()) + .numberOfShards(1) + .numberOfReplicas(0) + .build(), + true + ) + .build() + ); List> requests = new ArrayList<>(); requests.add(new Tuple<>(IndicesStatsAction.NAME, new IndicesStatsRequest().indices(SECURITY_MAIN_ALIAS))); @@ -1490,16 +1981,26 @@ public void testMonitoringOperationsAgainstSecurityIndexRequireAllowRestricted() final String requestId = AuditUtil.getOrGenerateRequestId(threadContext); final Authentication restrictedUserAuthn = createAuthentication(new User("restricted_user", "restricted_monitor")); assertThrowsAuthorizationException(() -> authorize(restrictedUserAuthn, action, request), action, "restricted_user"); - verify(auditTrail).accessDenied(eq(requestId), eq(restrictedUserAuthn), eq(action), eq(request), - authzInfoRoles(new String[]{"restricted_monitor"})); + verify(auditTrail).accessDenied( + eq(requestId), + eq(restrictedUserAuthn), + eq(action), + eq(request), + authzInfoRoles(new String[] { "restricted_monitor" }) + ); verifyNoMoreInteractions(auditTrail); } try (StoredContext ignore = threadContext.stashContext()) { final String requestId = AuditUtil.getOrGenerateRequestId(threadContext); final Authentication unrestrictedUserAuthn = createAuthentication(new User("unrestricted_user", "unrestricted_monitor")); authorize(unrestrictedUserAuthn, action, request); - verify(auditTrail).accessGranted(eq(requestId), eq(unrestrictedUserAuthn), eq(action), eq(request), - authzInfoRoles(new String[]{"unrestricted_monitor"})); + verify(auditTrail).accessGranted( + eq(requestId), + eq(unrestrictedUserAuthn), + eq(action), + eq(request), + authzInfoRoles(new String[] { "unrestricted_monitor" }) + ); verifyNoMoreInteractions(auditTrail); } } @@ -1508,40 +2009,74 @@ public void testMonitoringOperationsAgainstSecurityIndexRequireAllowRestricted() public void testSuperusersCanExecuteOperationAgainstSecurityIndex() throws IOException { final User superuser = new User("custom_admin", ReservedRolesStore.SUPERUSER_ROLE_DESCRIPTOR.getName()); roleMap.put(ReservedRolesStore.SUPERUSER_ROLE_DESCRIPTOR.getName(), ReservedRolesStore.SUPERUSER_ROLE_DESCRIPTOR); - ClusterState state = mockClusterState(Metadata.builder() - .put(new IndexMetadata.Builder(INTERNAL_SECURITY_MAIN_INDEX_7) - .putAlias(new AliasMetadata.Builder(SECURITY_MAIN_ALIAS).build()) - .settings(Settings.builder().put("index.version.created", Version.CURRENT).build()) - .numberOfShards(1) - .numberOfReplicas(0) - .build(), true) - .build()); + ClusterState state = mockClusterState( + Metadata.builder() + .put( + new IndexMetadata.Builder(INTERNAL_SECURITY_MAIN_INDEX_7).putAlias( + new AliasMetadata.Builder(SECURITY_MAIN_ALIAS).build() + ) + .settings(Settings.builder().put("index.version.created", Version.CURRENT).build()) + .numberOfShards(1) + .numberOfReplicas(0) + .build(), + true + ) + .build() + ); final String requestId = AuditUtil.getOrGenerateRequestId(threadContext); List> requests = new ArrayList<>(); requests.add( - new Tuple<>(DeleteAction.NAME, new DeleteRequest(randomFrom(SECURITY_MAIN_ALIAS, INTERNAL_SECURITY_MAIN_INDEX_7), "id"))); - requests.add(new Tuple<>(BulkAction.NAME + "[s]", - createBulkShardRequest(randomFrom(SECURITY_MAIN_ALIAS, INTERNAL_SECURITY_MAIN_INDEX_7), DeleteRequest::new))); + new Tuple<>(DeleteAction.NAME, new DeleteRequest(randomFrom(SECURITY_MAIN_ALIAS, INTERNAL_SECURITY_MAIN_INDEX_7), "id")) + ); requests.add( - new Tuple<>(UpdateAction.NAME, new UpdateRequest(randomFrom(SECURITY_MAIN_ALIAS, INTERNAL_SECURITY_MAIN_INDEX_7), "id"))); + new Tuple<>( + BulkAction.NAME + "[s]", + createBulkShardRequest(randomFrom(SECURITY_MAIN_ALIAS, INTERNAL_SECURITY_MAIN_INDEX_7), DeleteRequest::new) + ) + ); + requests.add( + new Tuple<>(UpdateAction.NAME, new UpdateRequest(randomFrom(SECURITY_MAIN_ALIAS, INTERNAL_SECURITY_MAIN_INDEX_7), "id")) + ); requests.add(new Tuple<>(IndexAction.NAME, new IndexRequest(randomFrom(SECURITY_MAIN_ALIAS, INTERNAL_SECURITY_MAIN_INDEX_7)))); - requests.add(new Tuple<>(BulkAction.NAME + "[s]", - createBulkShardRequest(randomFrom(SECURITY_MAIN_ALIAS, INTERNAL_SECURITY_MAIN_INDEX_7), - (index, id) -> new IndexRequest(index).id(id)))); + requests.add( + new Tuple<>( + BulkAction.NAME + "[s]", + createBulkShardRequest( + randomFrom(SECURITY_MAIN_ALIAS, INTERNAL_SECURITY_MAIN_INDEX_7), + (index, id) -> new IndexRequest(index).id(id) + ) + ) + ); requests.add(new Tuple<>(SearchAction.NAME, new SearchRequest(randomFrom(SECURITY_MAIN_ALIAS, INTERNAL_SECURITY_MAIN_INDEX_7)))); - requests.add(new Tuple<>(TermVectorsAction.NAME, - new TermVectorsRequest(randomFrom(SECURITY_MAIN_ALIAS, INTERNAL_SECURITY_MAIN_INDEX_7), "id"))); requests.add( - new Tuple<>(GetAction.NAME, new GetRequest(randomFrom(SECURITY_MAIN_ALIAS, INTERNAL_SECURITY_MAIN_INDEX_7), "id"))); - requests.add(new Tuple<>(TermVectorsAction.NAME, - new TermVectorsRequest(randomFrom(SECURITY_MAIN_ALIAS, INTERNAL_SECURITY_MAIN_INDEX_7), "id"))); - requests.add(new Tuple<>(IndicesAliasesAction.NAME, new IndicesAliasesRequest() - .addAliasAction(AliasActions.add().alias("security_alias").index(INTERNAL_SECURITY_MAIN_INDEX_7)))); - requests.add(new Tuple<>(ClusterHealthAction.NAME, - new ClusterHealthRequest(randomFrom(SECURITY_MAIN_ALIAS, INTERNAL_SECURITY_MAIN_INDEX_7)))); - requests.add(new Tuple<>(ClusterHealthAction.NAME, - new ClusterHealthRequest(randomFrom(SECURITY_MAIN_ALIAS, INTERNAL_SECURITY_MAIN_INDEX_7), "foo", "bar"))); + new Tuple<>( + TermVectorsAction.NAME, + new TermVectorsRequest(randomFrom(SECURITY_MAIN_ALIAS, INTERNAL_SECURITY_MAIN_INDEX_7), "id") + ) + ); + requests.add(new Tuple<>(GetAction.NAME, new GetRequest(randomFrom(SECURITY_MAIN_ALIAS, INTERNAL_SECURITY_MAIN_INDEX_7), "id"))); + requests.add( + new Tuple<>( + TermVectorsAction.NAME, + new TermVectorsRequest(randomFrom(SECURITY_MAIN_ALIAS, INTERNAL_SECURITY_MAIN_INDEX_7), "id") + ) + ); + requests.add( + new Tuple<>( + IndicesAliasesAction.NAME, + new IndicesAliasesRequest().addAliasAction(AliasActions.add().alias("security_alias").index(INTERNAL_SECURITY_MAIN_INDEX_7)) + ) + ); + requests.add( + new Tuple<>(ClusterHealthAction.NAME, new ClusterHealthRequest(randomFrom(SECURITY_MAIN_ALIAS, INTERNAL_SECURITY_MAIN_INDEX_7))) + ); + requests.add( + new Tuple<>( + ClusterHealthAction.NAME, + new ClusterHealthRequest(randomFrom(SECURITY_MAIN_ALIAS, INTERNAL_SECURITY_MAIN_INDEX_7), "foo", "bar") + ) + ); for (final Tuple requestTuple : requests) { final String action = requestTuple.v1(); @@ -1549,8 +2084,13 @@ public void testSuperusersCanExecuteOperationAgainstSecurityIndex() throws IOExc try (ThreadContext.StoredContext ignore = threadContext.newStoredContext(false)) { final Authentication authentication = createAuthentication(superuser); authorize(authentication, action, request); - verify(auditTrail).accessGranted(eq(requestId), eq(authentication), eq(action), eq(request), - authzInfoRoles(superuser.roles())); + verify(auditTrail).accessGranted( + eq(requestId), + eq(authentication), + eq(action), + eq(request), + authzInfoRoles(superuser.roles()) + ); } } } @@ -1559,14 +2099,20 @@ public void testSuperusersCanExecuteOperationAgainstSecurityIndexWithWildcard() final User superuser = new User("custom_admin", ReservedRolesStore.SUPERUSER_ROLE_DESCRIPTOR.getName()); final Authentication authentication = createAuthentication(superuser); roleMap.put(ReservedRolesStore.SUPERUSER_ROLE_DESCRIPTOR.getName(), ReservedRolesStore.SUPERUSER_ROLE_DESCRIPTOR); - ClusterState state = mockClusterState(Metadata.builder() - .put(new IndexMetadata.Builder(INTERNAL_SECURITY_MAIN_INDEX_7) - .putAlias(new AliasMetadata.Builder(SECURITY_MAIN_ALIAS).build()) - .settings(Settings.builder().put("index.version.created", Version.CURRENT).build()) - .numberOfShards(1) - .numberOfReplicas(0) - .build(), true) - .build()); + ClusterState state = mockClusterState( + Metadata.builder() + .put( + new IndexMetadata.Builder(INTERNAL_SECURITY_MAIN_INDEX_7).putAlias( + new AliasMetadata.Builder(SECURITY_MAIN_ALIAS).build() + ) + .settings(Settings.builder().put("index.version.created", Version.CURRENT).build()) + .numberOfShards(1) + .numberOfReplicas(0) + .build(), + true + ) + .build() + ); final String requestId = AuditUtil.getOrGenerateRequestId(threadContext); String action = SearchAction.NAME; @@ -1577,7 +2123,7 @@ public void testSuperusersCanExecuteOperationAgainstSecurityIndexWithWildcard() } public void testCompositeActionsAreImmediatelyRejected() { - //if the user has no permission for composite actions against any index, the request fails straight-away in the main action + // if the user has no permission for composite actions against any index, the request fails straight-away in the main action final Tuple compositeRequest = randomCompositeRequest(); final String action = compositeRequest.v1(); final TransportRequest request = compositeRequest.v2(); @@ -1586,28 +2132,40 @@ public void testCompositeActionsAreImmediatelyRejected() { roleMap.put("no_indices", role); final String requestId = AuditUtil.getOrGenerateRequestId(threadContext); - assertThrowsAuthorizationException( - () -> authorize(authentication, action, request), action, "test user"); - verify(auditTrail).accessDenied(eq(requestId), eq(authentication), eq(action), eq(request), - authzInfoRoles(new String[]{role.getName()})); + assertThrowsAuthorizationException(() -> authorize(authentication, action, request), action, "test user"); + verify(auditTrail).accessDenied( + eq(requestId), + eq(authentication), + eq(action), + eq(request), + authzInfoRoles(new String[] { role.getName() }) + ); verifyNoMoreInteractions(auditTrail); } public void testCompositeActionsIndicesAreNotChecked() throws IOException { - //if the user has permission for some index, the request goes through without looking at the indices, they will be checked later + // if the user has permission for some index, the request goes through without looking at the indices, they will be checked later final Tuple compositeRequest = randomCompositeRequest(); final String action = compositeRequest.v1(); final TransportRequest request = compositeRequest.v2(); final Authentication authentication = createAuthentication(new User("test user", "role")); - final RoleDescriptor role = new RoleDescriptor("role", null, - new IndicesPrivileges[]{IndicesPrivileges.builder().indices(randomBoolean() ? "a" : "index").privileges("all").build()}, - null); + final RoleDescriptor role = new RoleDescriptor( + "role", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices(randomBoolean() ? "a" : "index").privileges("all").build() }, + null + ); roleMap.put("role", role); final String requestId = AuditUtil.getOrGenerateRequestId(threadContext); authorize(authentication, action, request); - verify(auditTrail).accessGranted(eq(requestId), eq(authentication), eq(action), eq(request), - authzInfoRoles(new String[]{role.getName()})); + verify(auditTrail).accessGranted( + eq(requestId), + eq(authentication), + eq(action), + eq(request), + authzInfoRoles(new String[] { role.getName() }) + ); verifyNoMoreInteractions(auditTrail); } @@ -1616,11 +2174,19 @@ public void testCompositeActionsMustImplementCompositeIndicesRequest() throws IO TransportRequest request = mock(TransportRequest.class); final String requestId = AuditUtil.getOrGenerateRequestId(threadContext); User user = new User("test user", "role"); - roleMap.put("role", new RoleDescriptor("role", null, - new IndicesPrivileges[]{IndicesPrivileges.builder().indices(randomBoolean() ? "a" : "index").privileges("all").build()}, - null)); - IllegalStateException illegalStateException = expectThrows(IllegalStateException.class, - () -> authorize(createAuthentication(user), action, request)); + roleMap.put( + "role", + new RoleDescriptor( + "role", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices(randomBoolean() ? "a" : "index").privileges("all").build() }, + null + ) + ); + IllegalStateException illegalStateException = expectThrows( + IllegalStateException.class, + () -> authorize(createAuthentication(user), action, request) + ); assertThat(illegalStateException.getMessage(), containsString("Composite and bulk actions must implement CompositeIndicesRequest")); } @@ -1634,7 +2200,7 @@ public void testCompositeActionsIndicesAreCheckedAtTheShardLevel() throws IOExce request = mockRequest; break; case 1: - //reindex, msearch, search template, and multi search template delegate to search + // reindex, msearch, search template, and multi search template delegate to search action = SearchAction.NAME; request = mockRequest; break; @@ -1656,18 +2222,31 @@ public void testCompositeActionsIndicesAreCheckedAtTheShardLevel() throws IOExce logger.info("--> action: {}", action); User userAllowed = new User("userAllowed", "roleAllowed"); - roleMap.put("roleAllowed", new RoleDescriptor("roleAllowed", null, - new IndicesPrivileges[]{IndicesPrivileges.builder().indices("index").privileges("all").build()}, null)); + roleMap.put( + "roleAllowed", + new RoleDescriptor( + "roleAllowed", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices("index").privileges("all").build() }, + null + ) + ); User userDenied = new User("userDenied", "roleDenied"); - roleMap.put("roleDenied", new RoleDescriptor("roleDenied", null, - new IndicesPrivileges[]{IndicesPrivileges.builder().indices("a").privileges("all").build()}, null)); + roleMap.put( + "roleDenied", + new RoleDescriptor( + "roleDenied", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices("a").privileges("all").build() }, + null + ) + ); AuditUtil.getOrGenerateRequestId(threadContext); mockEmptyMetadata(); try (ThreadContext.StoredContext ignore = threadContext.newStoredContext(false)) { authorize(createAuthentication(userAllowed), action, request); } - assertThrowsAuthorizationException( - () -> authorize(createAuthentication(userDenied), action, request), action, "userDenied"); + assertThrowsAuthorizationException(() -> authorize(createAuthentication(userDenied), action, request), action, "userDenied"); } public void testAuthorizationOfIndividualBulkItems() throws IOException { @@ -1678,43 +2257,93 @@ public void testAuthorizationOfIndividualBulkItems() throws IOException { new BulkItemRequest(3, new DeleteRequest("alias-1", "a1a")), new BulkItemRequest(4, new IndexRequest("alias-1").id("a1b")), new BulkItemRequest(5, new DeleteRequest("alias-2", "a2a")), - new BulkItemRequest(6, new IndexRequest("alias-2").id("a2b")) - }; + new BulkItemRequest(6, new IndexRequest("alias-2").id("a2b")) }; final ShardId shardId = new ShardId("concrete-index", UUID.randomUUID().toString(), 1); final TransportRequest request = new BulkShardRequest(shardId, WriteRequest.RefreshPolicy.IMMEDIATE, items); final Authentication authentication = createAuthentication(new User("user", "my-role")); - RoleDescriptor role = new RoleDescriptor("my-role", null, new IndicesPrivileges[]{ - IndicesPrivileges.builder().indices("concrete-index").privileges("all").build(), - IndicesPrivileges.builder().indices("alias-1").privileges("index").build(), - IndicesPrivileges.builder().indices("alias-2").privileges("delete").build() - }, null); + RoleDescriptor role = new RoleDescriptor( + "my-role", + null, + new IndicesPrivileges[] { + IndicesPrivileges.builder().indices("concrete-index").privileges("all").build(), + IndicesPrivileges.builder().indices("alias-1").privileges("index").build(), + IndicesPrivileges.builder().indices("alias-2").privileges("delete").build() }, + null + ); roleMap.put("my-role", role); mockEmptyMetadata(); final String requestId = AuditUtil.getOrGenerateRequestId(threadContext); authorize(authentication, action, request); - verify(auditTrail).explicitIndexAccessEvent(eq(requestId), eq(AuditLevel.ACCESS_GRANTED), eq(authentication), - eq(DeleteAction.NAME), eq("concrete-index"), eq(BulkItemRequest.class.getSimpleName()), - eq(request.remoteAddress()), authzInfoRoles(new String[]{role.getName()})); - verify(auditTrail).explicitIndexAccessEvent(eq(requestId), eq(AuditLevel.ACCESS_GRANTED), eq(authentication), - eq(DeleteAction.NAME), eq("alias-2"), eq(BulkItemRequest.class.getSimpleName()), - eq(request.remoteAddress()), authzInfoRoles(new String[]{role.getName()})); - verify(auditTrail).explicitIndexAccessEvent(eq(requestId), eq(AuditLevel.ACCESS_GRANTED), eq(authentication), - eq(IndexAction.NAME + ":op_type/index"), eq("concrete-index"), eq(BulkItemRequest.class.getSimpleName()), - eq(request.remoteAddress()), authzInfoRoles(new String[]{role.getName()})); - verify(auditTrail).explicitIndexAccessEvent(eq(requestId), eq(AuditLevel.ACCESS_GRANTED), eq(authentication), - eq(IndexAction.NAME + ":op_type/index"), eq("alias-1"), eq(BulkItemRequest.class.getSimpleName()), - eq(request.remoteAddress()), authzInfoRoles(new String[]{role.getName()})); - verify(auditTrail).explicitIndexAccessEvent(eq(requestId), eq(AuditLevel.ACCESS_DENIED), eq(authentication), - eq(DeleteAction.NAME), eq("alias-1"), eq(BulkItemRequest.class.getSimpleName()), - eq(request.remoteAddress()), authzInfoRoles(new String[]{role.getName()})); - verify(auditTrail).explicitIndexAccessEvent(eq(requestId), eq(AuditLevel.ACCESS_DENIED), eq(authentication), - eq(IndexAction.NAME + ":op_type/index"), eq("alias-2"), eq(BulkItemRequest.class.getSimpleName()), - eq(request.remoteAddress()), authzInfoRoles(new String[]{role.getName()})); - verify(auditTrail).accessGranted(eq(requestId), eq(authentication), eq(action), eq(request), - authzInfoRoles(new String[]{role.getName()})); // bulk request is allowed + verify(auditTrail).explicitIndexAccessEvent( + eq(requestId), + eq(AuditLevel.ACCESS_GRANTED), + eq(authentication), + eq(DeleteAction.NAME), + eq("concrete-index"), + eq(BulkItemRequest.class.getSimpleName()), + eq(request.remoteAddress()), + authzInfoRoles(new String[] { role.getName() }) + ); + verify(auditTrail).explicitIndexAccessEvent( + eq(requestId), + eq(AuditLevel.ACCESS_GRANTED), + eq(authentication), + eq(DeleteAction.NAME), + eq("alias-2"), + eq(BulkItemRequest.class.getSimpleName()), + eq(request.remoteAddress()), + authzInfoRoles(new String[] { role.getName() }) + ); + verify(auditTrail).explicitIndexAccessEvent( + eq(requestId), + eq(AuditLevel.ACCESS_GRANTED), + eq(authentication), + eq(IndexAction.NAME + ":op_type/index"), + eq("concrete-index"), + eq(BulkItemRequest.class.getSimpleName()), + eq(request.remoteAddress()), + authzInfoRoles(new String[] { role.getName() }) + ); + verify(auditTrail).explicitIndexAccessEvent( + eq(requestId), + eq(AuditLevel.ACCESS_GRANTED), + eq(authentication), + eq(IndexAction.NAME + ":op_type/index"), + eq("alias-1"), + eq(BulkItemRequest.class.getSimpleName()), + eq(request.remoteAddress()), + authzInfoRoles(new String[] { role.getName() }) + ); + verify(auditTrail).explicitIndexAccessEvent( + eq(requestId), + eq(AuditLevel.ACCESS_DENIED), + eq(authentication), + eq(DeleteAction.NAME), + eq("alias-1"), + eq(BulkItemRequest.class.getSimpleName()), + eq(request.remoteAddress()), + authzInfoRoles(new String[] { role.getName() }) + ); + verify(auditTrail).explicitIndexAccessEvent( + eq(requestId), + eq(AuditLevel.ACCESS_DENIED), + eq(authentication), + eq(IndexAction.NAME + ":op_type/index"), + eq("alias-2"), + eq(BulkItemRequest.class.getSimpleName()), + eq(request.remoteAddress()), + authzInfoRoles(new String[] { role.getName() }) + ); + verify(auditTrail).accessGranted( + eq(requestId), + eq(authentication), + eq(action), + eq(request), + authzInfoRoles(new String[] { role.getName() }) + ); // bulk request is allowed verifyNoMoreInteractions(auditTrail); } @@ -1722,18 +2351,20 @@ public void testAuthorizationOfIndividualBulkItemsWithDateMath() throws IOExcept final String action = BulkAction.NAME + "[s]"; final BulkItemRequest[] items = { new BulkItemRequest(1, new IndexRequest("").id("dy1")), - new BulkItemRequest(2, - new DeleteRequest("", "dy2")), // resolves to same as above + new BulkItemRequest(2, new DeleteRequest("", "dy2")), // resolves to same as above new BulkItemRequest(3, new IndexRequest("").id("dm1")), - new BulkItemRequest(4, - new DeleteRequest("", "dm2")), // resolves to same as above + new BulkItemRequest(4, new DeleteRequest("", "dm2")), // resolves to same as above }; final ShardId shardId = new ShardId("concrete-index", UUID.randomUUID().toString(), 1); final TransportRequest request = new BulkShardRequest(shardId, WriteRequest.RefreshPolicy.IMMEDIATE, items); final Authentication authentication = createAuthentication(new User("user", "my-role")); - final RoleDescriptor role = new RoleDescriptor("my-role", null, - new IndicesPrivileges[]{IndicesPrivileges.builder().indices("datemath-*").privileges("index").build()}, null); + final RoleDescriptor role = new RoleDescriptor( + "my-role", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices("datemath-*").privileges("index").build() }, + null + ); roleMap.put("my-role", role); final String requestId = AuditUtil.getOrGenerateRequestId(threadContext); @@ -1741,22 +2372,40 @@ public void testAuthorizationOfIndividualBulkItemsWithDateMath() throws IOExcept authorize(authentication, action, request); // both deletes should fail - verify(auditTrail, times(2)).explicitIndexAccessEvent(eq(requestId), eq(AuditLevel.ACCESS_DENIED), eq(authentication), - eq(DeleteAction.NAME), ArgumentMatchers.startsWith("datemath-"), eq(BulkItemRequest.class.getSimpleName()), - eq(request.remoteAddress()), authzInfoRoles(new String[]{role.getName()})); - verify(auditTrail, times(2)).explicitIndexAccessEvent(eq(requestId), eq(AuditLevel.ACCESS_GRANTED), eq(authentication), - eq(IndexAction.NAME + ":op_type/index"), ArgumentMatchers.startsWith("datemath-"), eq(BulkItemRequest.class.getSimpleName()), - eq(request.remoteAddress()), authzInfoRoles(new String[]{role.getName()})); + verify(auditTrail, times(2)).explicitIndexAccessEvent( + eq(requestId), + eq(AuditLevel.ACCESS_DENIED), + eq(authentication), + eq(DeleteAction.NAME), + ArgumentMatchers.startsWith("datemath-"), + eq(BulkItemRequest.class.getSimpleName()), + eq(request.remoteAddress()), + authzInfoRoles(new String[] { role.getName() }) + ); + verify(auditTrail, times(2)).explicitIndexAccessEvent( + eq(requestId), + eq(AuditLevel.ACCESS_GRANTED), + eq(authentication), + eq(IndexAction.NAME + ":op_type/index"), + ArgumentMatchers.startsWith("datemath-"), + eq(BulkItemRequest.class.getSimpleName()), + eq(request.remoteAddress()), + authzInfoRoles(new String[] { role.getName() }) + ); // bulk request is allowed - verify(auditTrail).accessGranted(eq(requestId), eq(authentication), eq(action), eq(request), - authzInfoRoles(new String[]{role.getName()})); + verify(auditTrail).accessGranted( + eq(requestId), + eq(authentication), + eq(action), + eq(request), + authzInfoRoles(new String[] { role.getName() }) + ); verifyNoMoreInteractions(auditTrail); } private BulkShardRequest createBulkShardRequest(String indexName, BiFunction> req) { - final BulkItemRequest[] items = {new BulkItemRequest(1, req.apply(indexName, "id"))}; - return new BulkShardRequest(new ShardId(indexName, UUID.randomUUID().toString(), 1), - WriteRequest.RefreshPolicy.IMMEDIATE, items); + final BulkItemRequest[] items = { new BulkItemRequest(1, req.apply(indexName, "id")) }; + return new BulkShardRequest(new ShardId(indexName, UUID.randomUUID().toString(), 1), WriteRequest.RefreshPolicy.IMMEDIATE, items); } private static Tuple randomCompositeRequest() { @@ -1782,8 +2431,7 @@ private static Tuple randomCompositeRequest() { } } - private static class MockCompositeIndicesRequest extends TransportRequest implements CompositeIndicesRequest { - } + private static class MockCompositeIndicesRequest extends TransportRequest implements CompositeIndicesRequest {} private Authentication createAuthentication(User user) { RealmRef lookedUpBy = user.authenticatedUser() == user ? null : new RealmRef("looked", "up", "by"); @@ -1821,12 +2469,16 @@ public void testProxyRequestFailsOnNonProxyAction() { TransportRequest transportRequest = TransportActionProxy.wrapRequest(node, request); final String requestId = AuditUtil.getOrGenerateRequestId(threadContext); User user = new User("test user", "role"); - ElasticsearchSecurityException ese = expectThrows(ElasticsearchSecurityException.class, - () -> authorize(createAuthentication(user), "indices:some/action", transportRequest)); + ElasticsearchSecurityException ese = expectThrows( + ElasticsearchSecurityException.class, + () -> authorize(createAuthentication(user), "indices:some/action", transportRequest) + ); assertThat(ese.getCause(), instanceOf(IllegalStateException.class)); IllegalStateException illegalStateException = (IllegalStateException) ese.getCause(); - assertThat(illegalStateException.getMessage(), - startsWith("originalRequest is a proxy request for: [org.elasticsearch.transport.TransportRequest$")); + assertThat( + illegalStateException.getMessage(), + startsWith("originalRequest is a proxy request for: [org.elasticsearch.transport.TransportRequest$") + ); assertThat(illegalStateException.getMessage(), endsWith("] but action: [indices:some/action] isn't")); } @@ -1834,14 +2486,20 @@ public void testProxyRequestFailsOnNonProxyRequest() { TransportRequest request = TransportRequest.Empty.INSTANCE; User user = new User("test user", "role"); AuditUtil.getOrGenerateRequestId(threadContext); - ElasticsearchSecurityException ese = expectThrows(ElasticsearchSecurityException.class, - () -> authorize(createAuthentication(user), TransportActionProxy.getProxyAction("indices:some/action"), request)); + ElasticsearchSecurityException ese = expectThrows( + ElasticsearchSecurityException.class, + () -> authorize(createAuthentication(user), TransportActionProxy.getProxyAction("indices:some/action"), request) + ); assertThat(ese.getCause(), instanceOf(IllegalStateException.class)); IllegalStateException illegalStateException = (IllegalStateException) ese.getCause(); - assertThat(illegalStateException.getMessage(), - startsWith("originalRequest is not a proxy request: [org.elasticsearch.transport.TransportRequest$")); - assertThat(illegalStateException.getMessage(), - endsWith("] but action: [internal:transport/proxy/indices:some/action] is a proxy action")); + assertThat( + illegalStateException.getMessage(), + startsWith("originalRequest is not a proxy request: [org.elasticsearch.transport.TransportRequest$") + ); + assertThat( + illegalStateException.getMessage(), + endsWith("] but action: [internal:transport/proxy/indices:some/action] is a proxy action") + ); } public void testProxyRequestAuthenticationDenied() throws IOException { @@ -1854,16 +2512,24 @@ public void testProxyRequestAuthenticationDenied() throws IOException { roleMap.put("no_indices", role); final String requestId = AuditUtil.getOrGenerateRequestId(threadContext); - assertThrowsAuthorizationException( - () -> authorize(authentication, action, transportRequest), action, "test user"); - verify(auditTrail).accessDenied(eq(requestId), eq(authentication), eq(action), eq(proxiedRequest), - authzInfoRoles(new String[]{role.getName()})); + assertThrowsAuthorizationException(() -> authorize(authentication, action, transportRequest), action, "test user"); + verify(auditTrail).accessDenied( + eq(requestId), + eq(authentication), + eq(action), + eq(proxiedRequest), + authzInfoRoles(new String[] { role.getName() }) + ); verifyNoMoreInteractions(auditTrail); } public void testProxyRequestAuthenticationGrantedWithAllPrivileges() { - RoleDescriptor role = new RoleDescriptor("a_all", null, - new IndicesPrivileges[]{IndicesPrivileges.builder().indices("a").privileges("all").build()}, null); + RoleDescriptor role = new RoleDescriptor( + "a_all", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices("a").privileges("all").build() }, + null + ); final Authentication authentication = createAuthentication(new User("test user", "a_all")); roleMap.put("a_all", role); final String requestId = AuditUtil.getOrGenerateRequestId(threadContext); @@ -1875,13 +2541,22 @@ public void testProxyRequestAuthenticationGrantedWithAllPrivileges() { final TransportRequest transportRequest = TransportActionProxy.wrapRequest(node, clearScrollRequest); final String action = TransportActionProxy.getProxyAction(SearchTransportService.CLEAR_SCROLL_CONTEXTS_ACTION_NAME); authorize(authentication, action, transportRequest); - verify(auditTrail).accessGranted(eq(requestId), eq(authentication), eq(action), eq(clearScrollRequest), - authzInfoRoles(new String[]{role.getName()})); + verify(auditTrail).accessGranted( + eq(requestId), + eq(authentication), + eq(action), + eq(clearScrollRequest), + authzInfoRoles(new String[] { role.getName() }) + ); } public void testProxyRequestAuthenticationGranted() { - RoleDescriptor role = new RoleDescriptor("a_all", null, - new IndicesPrivileges[]{IndicesPrivileges.builder().indices("a").privileges("read_cross_cluster").build()}, null); + RoleDescriptor role = new RoleDescriptor( + "a_all", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices("a").privileges("read_cross_cluster").build() }, + null + ); final Authentication authentication = createAuthentication(new User("test user", "a_all")); roleMap.put("a_all", role); mockEmptyMetadata(); @@ -1892,14 +2567,23 @@ public void testProxyRequestAuthenticationGranted() { final TransportRequest transportRequest = TransportActionProxy.wrapRequest(node, clearScrollRequest); final String action = TransportActionProxy.getProxyAction(SearchTransportService.CLEAR_SCROLL_CONTEXTS_ACTION_NAME); authorize(authentication, action, transportRequest); - verify(auditTrail).accessGranted(eq(requestId), eq(authentication), eq(action), eq(clearScrollRequest), - authzInfoRoles(new String[]{role.getName()})); + verify(auditTrail).accessGranted( + eq(requestId), + eq(authentication), + eq(action), + eq(clearScrollRequest), + authzInfoRoles(new String[] { role.getName() }) + ); } public void testProxyRequestAuthenticationDeniedWithReadPrivileges() throws IOException { final Authentication authentication = createAuthentication(new User("test user", "a_all")); - final RoleDescriptor role = new RoleDescriptor("a_all", null, - new IndicesPrivileges[]{IndicesPrivileges.builder().indices("a").privileges("read").build()}, null); + final RoleDescriptor role = new RoleDescriptor( + "a_all", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices("a").privileges("read").build() }, + null + ); roleMap.put("a_all", role); final String requestId = AuditUtil.getOrGenerateRequestId(threadContext); mockEmptyMetadata(); @@ -1907,10 +2591,14 @@ public void testProxyRequestAuthenticationDeniedWithReadPrivileges() throws IOEx ClearScrollRequest clearScrollRequest = new ClearScrollRequest(); TransportRequest transportRequest = TransportActionProxy.wrapRequest(node, clearScrollRequest); String action = TransportActionProxy.getProxyAction(SearchTransportService.CLEAR_SCROLL_CONTEXTS_ACTION_NAME); - assertThrowsAuthorizationException( - () -> authorize(authentication, action, transportRequest), action, "test user"); - verify(auditTrail).accessDenied(eq(requestId), eq(authentication), eq(action), eq(clearScrollRequest), - authzInfoRoles(new String[]{role.getName()})); + assertThrowsAuthorizationException(() -> authorize(authentication, action, transportRequest), action, "test user"); + verify(auditTrail).accessDenied( + eq(requestId), + eq(authentication), + eq(action), + eq(clearScrollRequest), + authzInfoRoles(new String[] { role.getName() }) + ); } public void testAuthorizationEngineSelection() { @@ -1921,59 +2609,92 @@ public void resolveAuthorizationInfo(RequestInfo requestInfo, ActionListener listener) { + public void authorizeRunAs( + RequestInfo requestInfo, + AuthorizationInfo authorizationInfo, + ActionListener listener + ) { throw new UnsupportedOperationException("not implemented"); } @Override - public void authorizeClusterAction(RequestInfo requestInfo, AuthorizationInfo authorizationInfo, - ActionListener listener) { + public void authorizeClusterAction( + RequestInfo requestInfo, + AuthorizationInfo authorizationInfo, + ActionListener listener + ) { throw new UnsupportedOperationException("not implemented"); } @Override - public void authorizeIndexAction(RequestInfo requestInfo, AuthorizationInfo authorizationInfo, - AsyncSupplier indicesAsyncSupplier, - Map aliasOrIndexLookup, - ActionListener listener) { + public void authorizeIndexAction( + RequestInfo requestInfo, + AuthorizationInfo authorizationInfo, + AsyncSupplier indicesAsyncSupplier, + Map aliasOrIndexLookup, + ActionListener listener + ) { throw new UnsupportedOperationException("not implemented"); } @Override - public void loadAuthorizedIndices(RequestInfo requestInfo, AuthorizationInfo authorizationInfo, - Map indicesLookup, ActionListener> listener) { + public void loadAuthorizedIndices( + RequestInfo requestInfo, + AuthorizationInfo authorizationInfo, + Map indicesLookup, + ActionListener> listener + ) { throw new UnsupportedOperationException("not implemented"); } @Override - public void validateIndexPermissionsAreSubset(RequestInfo requestInfo, AuthorizationInfo authorizationInfo, - Map> indexNameToNewNames, - ActionListener listener) { + public void validateIndexPermissionsAreSubset( + RequestInfo requestInfo, + AuthorizationInfo authorizationInfo, + Map> indexNameToNewNames, + ActionListener listener + ) { throw new UnsupportedOperationException("not implemented"); } @Override - public void checkPrivileges(Authentication authentication, AuthorizationInfo authorizationInfo, - HasPrivilegesRequest hasPrivilegesRequest, - Collection applicationPrivilegeDescriptors, - ActionListener listener) { + public void checkPrivileges( + Authentication authentication, + AuthorizationInfo authorizationInfo, + HasPrivilegesRequest hasPrivilegesRequest, + Collection applicationPrivilegeDescriptors, + ActionListener listener + ) { throw new UnsupportedOperationException("not implemented"); } @Override - public void getUserPrivileges(Authentication authentication, AuthorizationInfo authorizationInfo, - GetUserPrivilegesRequest request, ActionListener listener) { + public void getUserPrivileges( + Authentication authentication, + AuthorizationInfo authorizationInfo, + GetUserPrivilegesRequest request, + ActionListener listener + ) { throw new UnsupportedOperationException("not implemented"); } }; MockLicenseState licenseState = mock(MockLicenseState.class); when(licenseState.isAllowed(Security.AUTHORIZATION_ENGINE_FEATURE)).thenReturn(true); - authorizationService = new AuthorizationService(Settings.EMPTY, rolesStore, clusterService, - auditTrailService, new DefaultAuthenticationFailureHandler(Collections.emptyMap()), threadPool, - new AnonymousUser(Settings.EMPTY), engine, Collections.emptySet(), licenseState, - TestIndexNameExpressionResolver.newInstance(), operatorPrivilegesService); + authorizationService = new AuthorizationService( + Settings.EMPTY, + rolesStore, + clusterService, + auditTrailService, + new DefaultAuthenticationFailureHandler(Collections.emptyMap()), + threadPool, + new AnonymousUser(Settings.EMPTY), + engine, + Collections.emptySet(), + licenseState, + TestIndexNameExpressionResolver.newInstance(), + operatorPrivilegesService + ); Authentication authentication; try (ThreadContext.StoredContext ignore = threadContext.stashContext()) { authentication = createAuthentication(new User("test user", "a_all")); @@ -1984,7 +2705,7 @@ auditTrailService, new DefaultAuthenticationFailureHandler(Collections.emptyMap( when(licenseState.isAllowed(Security.AUTHORIZATION_ENGINE_FEATURE)).thenReturn(true); try (ThreadContext.StoredContext ignore = threadContext.stashContext()) { - authentication = createAuthentication(new User("runas", new String[]{"runas_role"}, new User("runner", "runner_role"))); + authentication = createAuthentication(new User("runas", new String[] { "runas_role" }, new User("runner", "runner_role"))); assertEquals(engine, authorizationService.getAuthorizationEngine(authentication)); assertEquals(engine, authorizationService.getRunAsAuthorizationEngine(authentication)); when(licenseState.isAllowed(Security.AUTHORIZATION_ENGINE_FEATURE)).thenReturn(false); @@ -1994,7 +2715,7 @@ auditTrailService, new DefaultAuthenticationFailureHandler(Collections.emptyMap( when(licenseState.isAllowed(Security.AUTHORIZATION_ENGINE_FEATURE)).thenReturn(true); try (ThreadContext.StoredContext ignore = threadContext.stashContext()) { - authentication = createAuthentication(new User("runas", new String[]{"runas_role"}, new ElasticUser(true))); + authentication = createAuthentication(new User("runas", new String[] { "runas_role" }, new ElasticUser(true))); assertEquals(engine, authorizationService.getAuthorizationEngine(authentication)); assertNotEquals(engine, authorizationService.getRunAsAuthorizationEngine(authentication)); assertThat(authorizationService.getRunAsAuthorizationEngine(authentication), instanceOf(RBACEngine.class)); @@ -2005,7 +2726,7 @@ auditTrailService, new DefaultAuthenticationFailureHandler(Collections.emptyMap( when(licenseState.isAllowed(Security.AUTHORIZATION_ENGINE_FEATURE)).thenReturn(true); try (ThreadContext.StoredContext ignore = threadContext.stashContext()) { - authentication = createAuthentication(new User("elastic", new String[]{"superuser"}, new User("runner", "runner_role"))); + authentication = createAuthentication(new User("elastic", new String[] { "superuser" }, new User("runner", "runner_role"))); assertNotEquals(engine, authorizationService.getAuthorizationEngine(authentication)); assertThat(authorizationService.getAuthorizationEngine(authentication), instanceOf(RBACEngine.class)); assertEquals(engine, authorizationService.getRunAsAuthorizationEngine(authentication)); @@ -2016,7 +2737,7 @@ auditTrailService, new DefaultAuthenticationFailureHandler(Collections.emptyMap( when(licenseState.isAllowed(Security.AUTHORIZATION_ENGINE_FEATURE)).thenReturn(true); try (ThreadContext.StoredContext ignore = threadContext.stashContext()) { - authentication = createAuthentication(new User("kibana", new String[]{"kibana_system"}, new ElasticUser(true))); + authentication = createAuthentication(new User("kibana", new String[] { "kibana_system" }, new ElasticUser(true))); assertNotEquals(engine, authorizationService.getAuthorizationEngine(authentication)); assertThat(authorizationService.getAuthorizationEngine(authentication), instanceOf(RBACEngine.class)); assertNotEquals(engine, authorizationService.getRunAsAuthorizationEngine(authentication)); @@ -2028,8 +2749,9 @@ auditTrailService, new DefaultAuthenticationFailureHandler(Collections.emptyMap( when(licenseState.isAllowed(Security.AUTHORIZATION_ENGINE_FEATURE)).thenReturn(true); try (ThreadContext.StoredContext ignore = threadContext.stashContext()) { - authentication = createAuthentication(randomFrom(XPackUser.INSTANCE, XPackSecurityUser.INSTANCE, - new ElasticUser(true), new KibanaUser(true))); + authentication = createAuthentication( + randomFrom(XPackUser.INSTANCE, XPackSecurityUser.INSTANCE, new ElasticUser(true), new KibanaUser(true)) + ); assertNotEquals(engine, authorizationService.getRunAsAuthorizationEngine(authentication)); assertThat(authorizationService.getRunAsAuthorizationEngine(authentication), instanceOf(RBACEngine.class)); when(licenseState.isAllowed(Security.AUTHORIZATION_ENGINE_FEATURE)).thenReturn(false); @@ -2044,7 +2766,9 @@ public void testOperatorPrivileges() { final Authentication authentication = createAuthentication(new User("user1", "role1")); assertThrowsAuthorizationException( () -> authorize(authentication, "cluster:admin/whatever", mock(TransportRequest.class)), - "cluster:admin/whatever", "user1"); + "cluster:admin/whatever", + "user1" + ); // The operator related exception is verified in the authorize(...) call verifyZeroInteractions(auditTrail); } @@ -2076,8 +2800,8 @@ public void testAuthorizedIndiciesTimeChecker() throws Exception { Pattern.quote("Resolving [0] indices for action [" + SearchAction.NAME + "] and user [slow-user] took [") + "\\d{3}" + Pattern.quote( - "ms] which is greater than the threshold of 200ms;" + - " The index privileges for this user may be too complex for this cluster." + "ms] which is greater than the threshold of 200ms;" + + " The index privileges for this user may be too complex for this cluster." ) ) ); @@ -2125,7 +2849,6 @@ public String getWriteableName() { } @Override - public void writeTo(StreamOutput out) throws IOException { - } + public void writeTo(StreamOutput out) throws IOException {} } } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/AuthorizationUtilsTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/AuthorizationUtilsTests.java index 1e3f40186cf07..beadd9009c652 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/AuthorizationUtilsTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/AuthorizationUtilsTests.java @@ -48,8 +48,10 @@ public void testSystemUserSwitchNonInternalAction() { } public void testSystemUserSwitchWithSystemUser() { - threadContext.putTransient(AuthenticationField.AUTHENTICATION_KEY, - new Authentication(SystemUser.INSTANCE, new RealmRef("test", "test", "foo"), null)); + threadContext.putTransient( + AuthenticationField.AUTHENTICATION_KEY, + new Authentication(SystemUser.INSTANCE, new RealmRef("test", "test", "foo"), null) + ); assertThat(AuthorizationUtils.shouldReplaceUserWithSystem(threadContext, "internal:something"), is(false)); } @@ -59,7 +61,7 @@ public void testSystemUserSwitchWithNullUser() { public void testSystemUserSwitchWithNonSystemUser() { User user = new User(randomAlphaOfLength(6), new String[] {}); - Authentication authentication = new Authentication(user, new RealmRef("test", "test", "foo"), null); + Authentication authentication = new Authentication(user, new RealmRef("test", "test", "foo"), null); threadContext.putTransient(AuthenticationField.AUTHENTICATION_KEY, authentication); threadContext.putTransient(AuthorizationServiceField.ORIGINATING_ACTION_KEY, randomFrom("indices:foo", "cluster:bar")); assertThat(AuthorizationUtils.shouldReplaceUserWithSystem(threadContext, "internal:something"), is(true)); @@ -67,7 +69,7 @@ public void testSystemUserSwitchWithNonSystemUser() { public void testSystemUserSwitchWithNonSystemUserAndInternalAction() { User user = new User(randomAlphaOfLength(6), new String[] {}); - Authentication authentication = new Authentication(user, new RealmRef("test", "test", "foo"), null); + Authentication authentication = new Authentication(user, new RealmRef("test", "test", "foo"), null); threadContext.putTransient(AuthenticationField.AUTHENTICATION_KEY, authentication); threadContext.putTransient(AuthorizationServiceField.ORIGINATING_ACTION_KEY, randomFrom("internal:foo/bar")); assertThat(AuthorizationUtils.shouldReplaceUserWithSystem(threadContext, "internal:something"), is(false)); @@ -82,7 +84,7 @@ public void testShouldSetUser() { // set authentication User user = new User(randomAlphaOfLength(6), new String[] {}); - Authentication authentication = new Authentication(user, new RealmRef("test", "test", "foo"), null); + Authentication authentication = new Authentication(user, new RealmRef("test", "test", "foo"), null); threadContext.putTransient(AuthenticationField.AUTHENTICATION_KEY, authentication); assertFalse(AuthorizationUtils.shouldSetUserBasedOnActionOrigin(threadContext)); @@ -100,8 +102,14 @@ public void testSwitchAndExecuteXpackSecurityUser() throws Exception { } public void testSwitchAndExecuteXpackUser() throws Exception { - for (String origin : Arrays.asList(ClientHelper.ML_ORIGIN, ClientHelper.WATCHER_ORIGIN, ClientHelper.DEPRECATION_ORIGIN, - ClientHelper.MONITORING_ORIGIN, PersistentTasksService.PERSISTENT_TASK_ORIGIN, ClientHelper.INDEX_LIFECYCLE_ORIGIN)) { + for (String origin : Arrays.asList( + ClientHelper.ML_ORIGIN, + ClientHelper.WATCHER_ORIGIN, + ClientHelper.DEPRECATION_ORIGIN, + ClientHelper.MONITORING_ORIGIN, + PersistentTasksService.PERSISTENT_TASK_ORIGIN, + ClientHelper.INDEX_LIFECYCLE_ORIGIN + )) { assertSwitchBasedOnOriginAndExecute(origin, XPackUser.INSTANCE); } } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/AuthorizedIndicesTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/AuthorizedIndicesTests.java index 5dd5fbc4e98f1..5ddac526d8b9f 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/AuthorizedIndicesTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/AuthorizedIndicesTests.java @@ -44,45 +44,70 @@ public class AuthorizedIndicesTests extends ESTestCase { public void testAuthorizedIndicesUserWithoutRoles() { - Set authorizedIndices = - RBACEngine.resolveAuthorizedIndicesFromRole(Role.EMPTY, getRequestInfo(""), Metadata.EMPTY_METADATA.getIndicesLookup()); + Set authorizedIndices = RBACEngine.resolveAuthorizedIndicesFromRole( + Role.EMPTY, + getRequestInfo(""), + Metadata.EMPTY_METADATA.getIndicesLookup() + ); assertTrue(authorizedIndices.isEmpty()); } public void testAuthorizedIndicesUserWithSomeRoles() { - RoleDescriptor aStarRole = new RoleDescriptor("a_star", null, - new IndicesPrivileges[] { IndicesPrivileges.builder().indices("a*").privileges("all").build() }, null); - RoleDescriptor bRole = new RoleDescriptor("b", null, - new IndicesPrivileges[] { IndicesPrivileges.builder().indices("b").privileges("READ").build() }, null); + RoleDescriptor aStarRole = new RoleDescriptor( + "a_star", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices("a*").privileges("all").build() }, + null + ); + RoleDescriptor bRole = new RoleDescriptor( + "b", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices("b").privileges("READ").build() }, + null + ); Settings indexSettings = Settings.builder().put("index.version.created", Version.CURRENT).build(); - final String internalSecurityIndex = randomFrom(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_6, - RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7); + final String internalSecurityIndex = randomFrom( + RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_6, + RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7 + ); Metadata metadata = Metadata.builder() - .put(new IndexMetadata.Builder("a1").settings(indexSettings).numberOfShards(1).numberOfReplicas(0).build(), true) - .put(new IndexMetadata.Builder("a2").settings(indexSettings).numberOfShards(1).numberOfReplicas(0).build(), true) - .put(new IndexMetadata.Builder("aaaaaa").settings(indexSettings).numberOfShards(1).numberOfReplicas(0).build(), true) - .put(new IndexMetadata.Builder("bbbbb").settings(indexSettings).numberOfShards(1).numberOfReplicas(0).build(), true) - .put(new IndexMetadata.Builder("b") - .settings(indexSettings) - .numberOfShards(1) - .numberOfReplicas(0) - .putAlias(new AliasMetadata.Builder("ab").build()) - .putAlias(new AliasMetadata.Builder("ba").build()) - .build(), true) - .put(new IndexMetadata.Builder(internalSecurityIndex) - .settings(indexSettings) - .numberOfShards(1) - .numberOfReplicas(0) - .putAlias(new AliasMetadata.Builder(RestrictedIndicesNames.SECURITY_MAIN_ALIAS).build()) - .build(), true) - .build(); + .put(new IndexMetadata.Builder("a1").settings(indexSettings).numberOfShards(1).numberOfReplicas(0).build(), true) + .put(new IndexMetadata.Builder("a2").settings(indexSettings).numberOfShards(1).numberOfReplicas(0).build(), true) + .put(new IndexMetadata.Builder("aaaaaa").settings(indexSettings).numberOfShards(1).numberOfReplicas(0).build(), true) + .put(new IndexMetadata.Builder("bbbbb").settings(indexSettings).numberOfShards(1).numberOfReplicas(0).build(), true) + .put( + new IndexMetadata.Builder("b").settings(indexSettings) + .numberOfShards(1) + .numberOfReplicas(0) + .putAlias(new AliasMetadata.Builder("ab").build()) + .putAlias(new AliasMetadata.Builder("ba").build()) + .build(), + true + ) + .put( + new IndexMetadata.Builder(internalSecurityIndex).settings(indexSettings) + .numberOfShards(1) + .numberOfReplicas(0) + .putAlias(new AliasMetadata.Builder(RestrictedIndicesNames.SECURITY_MAIN_ALIAS).build()) + .build(), + true + ) + .build(); final PlainActionFuture future = new PlainActionFuture<>(); final Set descriptors = Sets.newHashSet(aStarRole, bRole); CompositeRolesStore.buildRoleFromDescriptors( - descriptors, new FieldPermissionsCache(Settings.EMPTY), null, RESTRICTED_INDICES_AUTOMATON, future); + descriptors, + new FieldPermissionsCache(Settings.EMPTY), + null, + RESTRICTED_INDICES_AUTOMATON, + future + ); Role roles = future.actionGet(); - Set list = - RBACEngine.resolveAuthorizedIndicesFromRole(roles, getRequestInfo(SearchAction.NAME), metadata.getIndicesLookup()); + Set list = RBACEngine.resolveAuthorizedIndicesFromRole( + roles, + getRequestInfo(SearchAction.NAME), + metadata.getIndicesLookup() + ); assertThat(list, containsInAnyOrder("a1", "a2", "aaaaaa", "b", "ab")); assertFalse(list.contains("bbbbb")); assertFalse(list.contains("ba")); @@ -92,42 +117,55 @@ public void testAuthorizedIndicesUserWithSomeRoles() { public void testAuthorizedIndicesUserWithSomeRolesEmptyMetadata() { Role role = Role.builder(RESTRICTED_INDICES_AUTOMATON, "role").add(IndexPrivilege.ALL, "*").build(); - Set authorizedIndices = RBACEngine.resolveAuthorizedIndicesFromRole(role, getRequestInfo(SearchAction.NAME), - Metadata.EMPTY_METADATA.getIndicesLookup()); + Set authorizedIndices = RBACEngine.resolveAuthorizedIndicesFromRole( + role, + getRequestInfo(SearchAction.NAME), + Metadata.EMPTY_METADATA.getIndicesLookup() + ); assertTrue(authorizedIndices.isEmpty()); } public void testSecurityIndicesAreRemovedFromRegularUser() { Role role = Role.builder(RESTRICTED_INDICES_AUTOMATON, "user_role") - .add(IndexPrivilege.ALL, "*").cluster(Set.of("all"), Set.of()).build(); - Set authorizedIndices = RBACEngine.resolveAuthorizedIndicesFromRole(role, getRequestInfo(SearchAction.NAME), - Metadata.EMPTY_METADATA.getIndicesLookup()); + .add(IndexPrivilege.ALL, "*") + .cluster(Set.of("all"), Set.of()) + .build(); + Set authorizedIndices = RBACEngine.resolveAuthorizedIndicesFromRole( + role, + getRequestInfo(SearchAction.NAME), + Metadata.EMPTY_METADATA.getIndicesLookup() + ); assertTrue(authorizedIndices.isEmpty()); } public void testSecurityIndicesAreRestrictedForDefaultRole() { - Role role = Role.builder(RESTRICTED_INDICES_AUTOMATON, - randomFrom("user_role", ReservedRolesStore.SUPERUSER_ROLE_DESCRIPTOR.getName())) - .add(IndexPrivilege.ALL, "*") - .cluster(Set.of("all"), Set.of()) - .build(); + Role role = Role.builder( + RESTRICTED_INDICES_AUTOMATON, + randomFrom("user_role", ReservedRolesStore.SUPERUSER_ROLE_DESCRIPTOR.getName()) + ).add(IndexPrivilege.ALL, "*").cluster(Set.of("all"), Set.of()).build(); Settings indexSettings = Settings.builder().put("index.version.created", Version.CURRENT).build(); - final String internalSecurityIndex = randomFrom(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_6, - RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7); + final String internalSecurityIndex = randomFrom( + RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_6, + RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7 + ); Metadata metadata = Metadata.builder() - .put(new IndexMetadata.Builder("an-index").settings(indexSettings).numberOfShards(1).numberOfReplicas(0).build(), true) - .put(new IndexMetadata.Builder("another-index").settings(indexSettings).numberOfShards(1).numberOfReplicas(0).build(), true) - .put(new IndexMetadata.Builder( - internalSecurityIndex) - .settings(indexSettings) - .numberOfShards(1) - .numberOfReplicas(0) - .putAlias(new AliasMetadata.Builder(RestrictedIndicesNames.SECURITY_MAIN_ALIAS).build()) - .build(), true) - .build(); + .put(new IndexMetadata.Builder("an-index").settings(indexSettings).numberOfShards(1).numberOfReplicas(0).build(), true) + .put(new IndexMetadata.Builder("another-index").settings(indexSettings).numberOfShards(1).numberOfReplicas(0).build(), true) + .put( + new IndexMetadata.Builder(internalSecurityIndex).settings(indexSettings) + .numberOfShards(1) + .numberOfReplicas(0) + .putAlias(new AliasMetadata.Builder(RestrictedIndicesNames.SECURITY_MAIN_ALIAS).build()) + .build(), + true + ) + .build(); - Set authorizedIndices = - RBACEngine.resolveAuthorizedIndicesFromRole(role, getRequestInfo(SearchAction.NAME), metadata.getIndicesLookup()); + Set authorizedIndices = RBACEngine.resolveAuthorizedIndicesFromRole( + role, + getRequestInfo(SearchAction.NAME), + metadata.getIndicesLookup() + ); assertThat(authorizedIndices, containsInAnyOrder("an-index", "another-index")); assertThat(authorizedIndices, not(contains(internalSecurityIndex))); assertThat(authorizedIndices, not(contains(RestrictedIndicesNames.SECURITY_MAIN_ALIAS))); @@ -135,72 +173,113 @@ public void testSecurityIndicesAreRestrictedForDefaultRole() { public void testSecurityIndicesAreNotRemovedFromUnrestrictedRole() { Role role = Role.builder(RESTRICTED_INDICES_AUTOMATON, randomAlphaOfLength(8)) - .add(FieldPermissions.DEFAULT, null, IndexPrivilege.ALL, true, "*") - .cluster(Set.of("all"), Set.of()) - .build(); + .add(FieldPermissions.DEFAULT, null, IndexPrivilege.ALL, true, "*") + .cluster(Set.of("all"), Set.of()) + .build(); Settings indexSettings = Settings.builder().put("index.version.created", Version.CURRENT).build(); - final String internalSecurityIndex = randomFrom(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_6, - RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7); + final String internalSecurityIndex = randomFrom( + RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_6, + RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7 + ); Metadata metadata = Metadata.builder() - .put(new IndexMetadata.Builder("an-index").settings(indexSettings).numberOfShards(1).numberOfReplicas(0).build(), true) - .put(new IndexMetadata.Builder("another-index").settings(indexSettings).numberOfShards(1).numberOfReplicas(0).build(), true) - .put(new IndexMetadata.Builder(internalSecurityIndex) - .settings(indexSettings) - .numberOfShards(1) - .numberOfReplicas(0) - .putAlias(new AliasMetadata.Builder(RestrictedIndicesNames.SECURITY_MAIN_ALIAS).build()) - .build(), true) - .build(); + .put(new IndexMetadata.Builder("an-index").settings(indexSettings).numberOfShards(1).numberOfReplicas(0).build(), true) + .put(new IndexMetadata.Builder("another-index").settings(indexSettings).numberOfShards(1).numberOfReplicas(0).build(), true) + .put( + new IndexMetadata.Builder(internalSecurityIndex).settings(indexSettings) + .numberOfShards(1) + .numberOfReplicas(0) + .putAlias(new AliasMetadata.Builder(RestrictedIndicesNames.SECURITY_MAIN_ALIAS).build()) + .build(), + true + ) + .build(); - Set authorizedIndices = - RBACEngine.resolveAuthorizedIndicesFromRole(role, getRequestInfo(SearchAction.NAME), metadata.getIndicesLookup()); - assertThat(authorizedIndices, containsInAnyOrder( - "an-index", "another-index", RestrictedIndicesNames.SECURITY_MAIN_ALIAS, internalSecurityIndex)); + Set authorizedIndices = RBACEngine.resolveAuthorizedIndicesFromRole( + role, + getRequestInfo(SearchAction.NAME), + metadata.getIndicesLookup() + ); + assertThat( + authorizedIndices, + containsInAnyOrder("an-index", "another-index", RestrictedIndicesNames.SECURITY_MAIN_ALIAS, internalSecurityIndex) + ); - Set authorizedIndicesSuperUser = - RBACEngine.resolveAuthorizedIndicesFromRole(role, getRequestInfo(SearchAction.NAME), metadata.getIndicesLookup()); - assertThat(authorizedIndicesSuperUser, containsInAnyOrder( - "an-index", "another-index", RestrictedIndicesNames.SECURITY_MAIN_ALIAS, internalSecurityIndex)); + Set authorizedIndicesSuperUser = RBACEngine.resolveAuthorizedIndicesFromRole( + role, + getRequestInfo(SearchAction.NAME), + metadata.getIndicesLookup() + ); + assertThat( + authorizedIndicesSuperUser, + containsInAnyOrder("an-index", "another-index", RestrictedIndicesNames.SECURITY_MAIN_ALIAS, internalSecurityIndex) + ); } public void testDataStreamsAreNotIncludedInAuthorizedIndices() { - RoleDescriptor aStarRole = new RoleDescriptor("a_star", null, - new IndicesPrivileges[] { IndicesPrivileges.builder().indices("a*").privileges("all").build() }, null); - RoleDescriptor bRole = new RoleDescriptor("b", null, - new IndicesPrivileges[] { IndicesPrivileges.builder().indices("b").privileges("READ").build() }, null); + RoleDescriptor aStarRole = new RoleDescriptor( + "a_star", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices("a*").privileges("all").build() }, + null + ); + RoleDescriptor bRole = new RoleDescriptor( + "b", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices("b").privileges("READ").build() }, + null + ); Settings indexSettings = Settings.builder().put("index.version.created", Version.CURRENT).build(); - final String internalSecurityIndex = randomFrom(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_6, - RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7); + final String internalSecurityIndex = randomFrom( + RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_6, + RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7 + ); String backingIndex = DataStream.getDefaultBackingIndexName("adatastream1", 1); Metadata metadata = Metadata.builder() .put(new IndexMetadata.Builder("a1").settings(indexSettings).numberOfShards(1).numberOfReplicas(0).build(), true) .put(new IndexMetadata.Builder("a2").settings(indexSettings).numberOfShards(1).numberOfReplicas(0).build(), true) .put(new IndexMetadata.Builder("aaaaaa").settings(indexSettings).numberOfShards(1).numberOfReplicas(0).build(), true) .put(new IndexMetadata.Builder("bbbbb").settings(indexSettings).numberOfShards(1).numberOfReplicas(0).build(), true) - .put(new IndexMetadata.Builder("b") - .settings(indexSettings) - .numberOfShards(1) - .numberOfReplicas(0) - .putAlias(new AliasMetadata.Builder("ab").build()) - .putAlias(new AliasMetadata.Builder("ba").build()) - .build(), true) - .put(new IndexMetadata.Builder(internalSecurityIndex) - .settings(indexSettings) - .numberOfShards(1) - .numberOfReplicas(0) - .putAlias(new AliasMetadata.Builder(RestrictedIndicesNames.SECURITY_MAIN_ALIAS).build()) - .build(), true) + .put( + new IndexMetadata.Builder("b").settings(indexSettings) + .numberOfShards(1) + .numberOfReplicas(0) + .putAlias(new AliasMetadata.Builder("ab").build()) + .putAlias(new AliasMetadata.Builder("ba").build()) + .build(), + true + ) + .put( + new IndexMetadata.Builder(internalSecurityIndex).settings(indexSettings) + .numberOfShards(1) + .numberOfReplicas(0) + .putAlias(new AliasMetadata.Builder(RestrictedIndicesNames.SECURITY_MAIN_ALIAS).build()) + .build(), + true + ) .put(new IndexMetadata.Builder(backingIndex).settings(indexSettings).numberOfShards(1).numberOfReplicas(0).build(), true) - .put(new DataStream("adatastream1", createTimestampField("@timestamp"), - List.of(new Index(DataStream.getDefaultBackingIndexName("adatastream1", 1), "_na_")))) + .put( + new DataStream( + "adatastream1", + createTimestampField("@timestamp"), + List.of(new Index(DataStream.getDefaultBackingIndexName("adatastream1", 1), "_na_")) + ) + ) .build(); final PlainActionFuture future = new PlainActionFuture<>(); final Set descriptors = Sets.newHashSet(aStarRole, bRole); CompositeRolesStore.buildRoleFromDescriptors( - descriptors, new FieldPermissionsCache(Settings.EMPTY), null, RESTRICTED_INDICES_AUTOMATON, future); + descriptors, + new FieldPermissionsCache(Settings.EMPTY), + null, + RESTRICTED_INDICES_AUTOMATON, + future + ); Role roles = future.actionGet(); - Set list = - RBACEngine.resolveAuthorizedIndicesFromRole(roles, getRequestInfo(SearchAction.NAME), metadata.getIndicesLookup()); + Set list = RBACEngine.resolveAuthorizedIndicesFromRole( + roles, + getRequestInfo(SearchAction.NAME), + metadata.getIndicesLookup() + ); assertThat(list, containsInAnyOrder("a1", "a2", "aaaaaa", "b", "ab")); assertFalse(list.contains("bbbbb")); assertFalse(list.contains("ba")); @@ -210,45 +289,68 @@ public void testDataStreamsAreNotIncludedInAuthorizedIndices() { } public void testDataStreamsAreIncludedInAuthorizedIndices() { - RoleDescriptor aStarRole = new RoleDescriptor("a_star", null, - new IndicesPrivileges[] { IndicesPrivileges.builder().indices("a*").privileges("all").build() }, null); - RoleDescriptor bRole = new RoleDescriptor("b", null, - new IndicesPrivileges[] { IndicesPrivileges.builder().indices("b").privileges("READ").build() }, null); + RoleDescriptor aStarRole = new RoleDescriptor( + "a_star", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices("a*").privileges("all").build() }, + null + ); + RoleDescriptor bRole = new RoleDescriptor( + "b", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices("b").privileges("READ").build() }, + null + ); Settings indexSettings = Settings.builder().put("index.version.created", Version.CURRENT).build(); - final String internalSecurityIndex = randomFrom(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_6, - RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7); + final String internalSecurityIndex = randomFrom( + RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_6, + RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7 + ); String backingIndex = DataStream.getDefaultBackingIndexName("adatastream1", 1); Metadata metadata = Metadata.builder() .put(new IndexMetadata.Builder("a1").settings(indexSettings).numberOfShards(1).numberOfReplicas(0).build(), true) .put(new IndexMetadata.Builder("a2").settings(indexSettings).numberOfShards(1).numberOfReplicas(0).build(), true) .put(new IndexMetadata.Builder("aaaaaa").settings(indexSettings).numberOfShards(1).numberOfReplicas(0).build(), true) .put(new IndexMetadata.Builder("bbbbb").settings(indexSettings).numberOfShards(1).numberOfReplicas(0).build(), true) - .put(new IndexMetadata.Builder("b") - .settings(indexSettings) - .numberOfShards(1) - .numberOfReplicas(0) - .putAlias(new AliasMetadata.Builder("ab").build()) - .putAlias(new AliasMetadata.Builder("ba").build()) - .build(), true) - .put(new IndexMetadata.Builder(internalSecurityIndex) - .settings(indexSettings) - .numberOfShards(1) - .numberOfReplicas(0) - .putAlias(new AliasMetadata.Builder(RestrictedIndicesNames.SECURITY_MAIN_ALIAS).build()) - .build(), true) + .put( + new IndexMetadata.Builder("b").settings(indexSettings) + .numberOfShards(1) + .numberOfReplicas(0) + .putAlias(new AliasMetadata.Builder("ab").build()) + .putAlias(new AliasMetadata.Builder("ba").build()) + .build(), + true + ) + .put( + new IndexMetadata.Builder(internalSecurityIndex).settings(indexSettings) + .numberOfShards(1) + .numberOfReplicas(0) + .putAlias(new AliasMetadata.Builder(RestrictedIndicesNames.SECURITY_MAIN_ALIAS).build()) + .build(), + true + ) .put(new IndexMetadata.Builder(backingIndex).settings(indexSettings).numberOfShards(1).numberOfReplicas(0).build(), true) - .put(new DataStream("adatastream1", createTimestampField("@timestamp"), - List.of(new Index(DataStream.getDefaultBackingIndexName("adatastream1", 1), "_na_")))) + .put( + new DataStream( + "adatastream1", + createTimestampField("@timestamp"), + List.of(new Index(DataStream.getDefaultBackingIndexName("adatastream1", 1), "_na_")) + ) + ) .build(); final PlainActionFuture future = new PlainActionFuture<>(); final Set descriptors = Sets.newHashSet(aStarRole, bRole); CompositeRolesStore.buildRoleFromDescriptors( - descriptors, new FieldPermissionsCache(Settings.EMPTY), null, RESTRICTED_INDICES_AUTOMATON, future); + descriptors, + new FieldPermissionsCache(Settings.EMPTY), + null, + RESTRICTED_INDICES_AUTOMATON, + future + ); Role roles = future.actionGet(); - TransportRequest request = new ResolveIndexAction.Request(new String[]{"a*"}); - AuthorizationEngine.RequestInfo requestInfo = getRequestInfo( request, SearchAction.NAME); - Set list = - RBACEngine.resolveAuthorizedIndicesFromRole(roles, requestInfo, metadata.getIndicesLookup()); + TransportRequest request = new ResolveIndexAction.Request(new String[] { "a*" }); + AuthorizationEngine.RequestInfo requestInfo = getRequestInfo(request, SearchAction.NAME); + Set list = RBACEngine.resolveAuthorizedIndicesFromRole(roles, requestInfo, metadata.getIndicesLookup()); assertThat(list, containsInAnyOrder("a1", "a2", "aaaaaa", "b", "ab", "adatastream1", backingIndex)); assertFalse(list.contains("bbbbb")); assertFalse(list.contains("ba")); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/DlsFlsRequestCacheDifferentiatorTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/DlsFlsRequestCacheDifferentiatorTests.java index cbc778f781c99..39259d5f097ea 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/DlsFlsRequestCacheDifferentiatorTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/DlsFlsRequestCacheDifferentiatorTests.java @@ -56,7 +56,10 @@ public void init() throws IOException { out = new BytesStreamOutput(); final SecurityContext securityContext = new SecurityContext(Settings.EMPTY, threadContext); differentiator = new DlsFlsRequestCacheDifferentiator( - licenseState, new SetOnce<>(securityContext), new SetOnce<>(mock(ScriptService.class))); + licenseState, + new SetOnce<>(securityContext), + new SetOnce<>(mock(ScriptService.class)) + ); shardSearchRequest = mock(ShardSearchRequest.class); indexName = randomAlphaOfLengthBetween(3, 8); dlsIndexName = "dls-" + randomAlphaOfLengthBetween(3, 8); @@ -64,29 +67,37 @@ public void init() throws IOException { dlsFlsIndexName = "dls-fls-" + randomAlphaOfLengthBetween(3, 8); final DocumentPermissions documentPermissions1 = DocumentPermissions.filteredBy( - Set.of(new BytesArray("{\"term\":{\"number\":1}}"))); + Set.of(new BytesArray("{\"term\":{\"number\":1}}")) + ); - threadContext.putTransient(AuthorizationServiceField.INDICES_PERMISSIONS_KEY, - new IndicesAccessControl(true, + threadContext.putTransient( + AuthorizationServiceField.INDICES_PERMISSIONS_KEY, + new IndicesAccessControl( + true, Map.of( flsIndexName, - new IndicesAccessControl.IndexAccessControl(true, - new FieldPermissions(new FieldPermissionsDefinition(new String[]{"*"}, new String[]{"private"})), - DocumentPermissions.allowAll()), + new IndicesAccessControl.IndexAccessControl( + true, + new FieldPermissions(new FieldPermissionsDefinition(new String[] { "*" }, new String[] { "private" })), + DocumentPermissions.allowAll() + ), dlsIndexName, - new IndicesAccessControl.IndexAccessControl(true, - FieldPermissions.DEFAULT, documentPermissions1), + new IndicesAccessControl.IndexAccessControl(true, FieldPermissions.DEFAULT, documentPermissions1), dlsFlsIndexName, - new IndicesAccessControl.IndexAccessControl(true, - new FieldPermissions(new FieldPermissionsDefinition(new String[]{"*"}, new String[]{"private"})), - documentPermissions1) + new IndicesAccessControl.IndexAccessControl( + true, + new FieldPermissions(new FieldPermissionsDefinition(new String[] { "*" }, new String[] { "private" })), + documentPermissions1 + ) ) - )); + ) + ); } public void testWillWriteCacheKeyForAnyDlsOrFls() throws IOException { when(shardSearchRequest.shardId()).thenReturn( - new ShardId(randomFrom(dlsIndexName, flsIndexName, dlsFlsIndexName), randomAlphaOfLength(10), randomIntBetween(0, 3))); + new ShardId(randomFrom(dlsIndexName, flsIndexName, dlsFlsIndexName), randomAlphaOfLength(10), randomIntBetween(0, 3)) + ); differentiator.accept(shardSearchRequest, out); assertThat(out.position(), greaterThan(0L)); } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/IndicesAndAliasesResolverTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/IndicesAndAliasesResolverTests.java index fd3dcf028ad9f..af74961d26e06 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/IndicesAndAliasesResolverTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/IndicesAndAliasesResolverTests.java @@ -91,8 +91,8 @@ import static org.elasticsearch.test.ActionListenerUtils.anyActionListener; import static org.elasticsearch.test.TestMatchers.throwableWithMessage; import static org.elasticsearch.xpack.core.security.index.RestrictedIndicesNames.SECURITY_MAIN_ALIAS; -import static org.elasticsearch.xpack.security.authz.AuthorizedIndicesTests.getRequestInfo; import static org.elasticsearch.xpack.core.security.test.TestRestrictedIndices.RESTRICTED_INDICES_AUTOMATON; +import static org.elasticsearch.xpack.security.authz.AuthorizedIndicesTests.getRequestInfo; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.Matchers.arrayContaining; @@ -127,15 +127,15 @@ public class IndicesAndAliasesResolverTests extends ESTestCase { private String tomorrowSuffix; @Before -// @SuppressWarnings("unchecked") + // @SuppressWarnings("unchecked") public void setup() { Settings settings = Settings.builder() - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 2)) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, randomIntBetween(0, 1)) - .put("cluster.remote.remote.seeds", "127.0.0.1:" + randomIntBetween(9301, 9350)) - .put("cluster.remote.other_remote.seeds", "127.0.0.1:" + randomIntBetween(9351, 9399)) - .build(); + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 2)) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, randomIntBetween(0, 1)) + .put("cluster.remote.remote.seeds", "127.0.0.1:" + randomIntBetween(9301, 9350)) + .put("cluster.remote.other_remote.seeds", "127.0.0.1:" + randomIntBetween(9351, 9399)) + .build(); indexNameExpressionResolver = TestIndexNameExpressionResolver.newInstance(); @@ -151,53 +151,71 @@ public void setup() { IndexMetadata dataStreamIndex2 = DataStreamTestHelper.createBackingIndex(dataStreamName, 2).build(); IndexMetadata dataStreamIndex3 = DataStreamTestHelper.createBackingIndex(otherDataStreamName, 1).build(); Metadata metadata = Metadata.builder() - .put(indexBuilder("foo").putAlias(AliasMetadata.builder("foofoobar")) - .putAlias(AliasMetadata.builder("foounauthorized")).settings(settings)) - .put(indexBuilder("foobar").putAlias(AliasMetadata.builder("foofoobar")) - .putAlias(AliasMetadata.builder("foobarfoo")).settings(settings)) - .put(indexBuilder("closed").state(State.CLOSE) - .putAlias(AliasMetadata.builder("foofoobar")).settings(settings)) - .put(indexBuilder("foofoo-closed").state(State.CLOSE).settings(settings)) - .put(indexBuilder("foobar-closed").state(State.CLOSE).settings(settings)) - .put(indexBuilder("foofoo").putAlias(AliasMetadata.builder("barbaz")).settings(settings)) - .put(indexBuilder("bar").settings(settings)) - .put(indexBuilder("bar-closed").state(State.CLOSE).settings(settings)) - .put(indexBuilder("bar2").settings(settings)) - .put(indexBuilder(indexNameExpressionResolver.resolveDateMathExpression("")).settings(settings)) - .put(indexBuilder("-index10").settings(settings)) - .put(indexBuilder("-index11").settings(settings)) - .put(indexBuilder("-index20").settings(settings)) - .put(indexBuilder("-index21").settings(settings)) - .put(indexBuilder("logs-00001").putAlias(AliasMetadata.builder("logs-alias").writeIndex(false)).settings(settings)) - .put(indexBuilder("logs-00002").putAlias(AliasMetadata.builder("logs-alias").writeIndex(false)).settings(settings)) - .put(indexBuilder("logs-00003").putAlias(AliasMetadata.builder("logs-alias").writeIndex(true)).settings(settings)) - .put(indexBuilder("hidden-open").settings(Settings.builder().put(settings).put("index.hidden", true).build())) - .put(indexBuilder(".hidden-open").settings(Settings.builder().put(settings).put("index.hidden", true).build())) - .put(indexBuilder(".hidden-closed").state(State.CLOSE) - .settings(Settings.builder().put(settings).put("index.hidden", true).build())) - .put(indexBuilder("hidden-closed").state(State.CLOSE) - .settings(Settings.builder().put(settings).put("index.hidden", true).build())) - .put(indexBuilder("hidden-w-aliases").settings(Settings.builder().put(settings).put("index.hidden", true).build()) + .put( + indexBuilder("foo").putAlias(AliasMetadata.builder("foofoobar")) + .putAlias(AliasMetadata.builder("foounauthorized")) + .settings(settings) + ) + .put( + indexBuilder("foobar").putAlias(AliasMetadata.builder("foofoobar")) + .putAlias(AliasMetadata.builder("foobarfoo")) + .settings(settings) + ) + .put(indexBuilder("closed").state(State.CLOSE).putAlias(AliasMetadata.builder("foofoobar")).settings(settings)) + .put(indexBuilder("foofoo-closed").state(State.CLOSE).settings(settings)) + .put(indexBuilder("foobar-closed").state(State.CLOSE).settings(settings)) + .put(indexBuilder("foofoo").putAlias(AliasMetadata.builder("barbaz")).settings(settings)) + .put(indexBuilder("bar").settings(settings)) + .put(indexBuilder("bar-closed").state(State.CLOSE).settings(settings)) + .put(indexBuilder("bar2").settings(settings)) + .put(indexBuilder(indexNameExpressionResolver.resolveDateMathExpression("")).settings(settings)) + .put(indexBuilder("-index10").settings(settings)) + .put(indexBuilder("-index11").settings(settings)) + .put(indexBuilder("-index20").settings(settings)) + .put(indexBuilder("-index21").settings(settings)) + .put(indexBuilder("logs-00001").putAlias(AliasMetadata.builder("logs-alias").writeIndex(false)).settings(settings)) + .put(indexBuilder("logs-00002").putAlias(AliasMetadata.builder("logs-alias").writeIndex(false)).settings(settings)) + .put(indexBuilder("logs-00003").putAlias(AliasMetadata.builder("logs-alias").writeIndex(true)).settings(settings)) + .put(indexBuilder("hidden-open").settings(Settings.builder().put(settings).put("index.hidden", true).build())) + .put(indexBuilder(".hidden-open").settings(Settings.builder().put(settings).put("index.hidden", true).build())) + .put( + indexBuilder(".hidden-closed").state(State.CLOSE) + .settings(Settings.builder().put(settings).put("index.hidden", true).build()) + ) + .put( + indexBuilder("hidden-closed").state(State.CLOSE) + .settings(Settings.builder().put(settings).put("index.hidden", true).build()) + ) + .put( + indexBuilder("hidden-w-aliases").settings(Settings.builder().put(settings).put("index.hidden", true).build()) .putAlias(AliasMetadata.builder("alias-hidden").isHidden(true).build()) .putAlias(AliasMetadata.builder(".alias-hidden").isHidden(true).build()) - .putAlias(AliasMetadata.builder("alias-visible-mixed").isHidden(false).build())) - .put(indexBuilder("hidden-w-visible-alias").settings(Settings.builder().put(settings).put("index.hidden", true).build()) - .putAlias(AliasMetadata.builder("alias-visible").build())) - .put(indexBuilder("visible-w-aliases").settings(Settings.builder().put(settings).build()) + .putAlias(AliasMetadata.builder("alias-visible-mixed").isHidden(false).build()) + ) + .put( + indexBuilder("hidden-w-visible-alias").settings(Settings.builder().put(settings).put("index.hidden", true).build()) + .putAlias(AliasMetadata.builder("alias-visible").build()) + ) + .put( + indexBuilder("visible-w-aliases").settings(Settings.builder().put(settings).build()) .putAlias(AliasMetadata.builder("alias-visible").build()) - .putAlias(AliasMetadata.builder("alias-visible-mixed").isHidden(false).build())) - .put(indexBuilder("date-hidden-" + todaySuffix) - .settings(Settings.builder().put(settings).put("index.hidden", true).build())) - .put(indexBuilder("date-hidden-" + tomorrowSuffix) - .settings(Settings.builder().put(settings).put("index.hidden", true).build())) - .put(dataStreamIndex1, true) - .put(dataStreamIndex2, true) - .put(dataStreamIndex3, true) - .put(new DataStream(dataStreamName, createTimestampField("@timestamp"), - List.of(dataStreamIndex1.getIndex(), dataStreamIndex2.getIndex()))) - .put(new DataStream(otherDataStreamName, createTimestampField("@timestamp"), - List.of(dataStreamIndex3.getIndex()))) - .put(indexBuilder(securityIndexName).settings(settings)).build(); + .putAlias(AliasMetadata.builder("alias-visible-mixed").isHidden(false).build()) + ) + .put(indexBuilder("date-hidden-" + todaySuffix).settings(Settings.builder().put(settings).put("index.hidden", true).build())) + .put(indexBuilder("date-hidden-" + tomorrowSuffix).settings(Settings.builder().put(settings).put("index.hidden", true).build())) + .put(dataStreamIndex1, true) + .put(dataStreamIndex2, true) + .put(dataStreamIndex3, true) + .put( + new DataStream( + dataStreamName, + createTimestampField("@timestamp"), + List.of(dataStreamIndex1.getIndex(), dataStreamIndex2.getIndex()) + ) + ) + .put(new DataStream(otherDataStreamName, createTimestampField("@timestamp"), List.of(dataStreamIndex3.getIndex()))) + .put(indexBuilder(securityIndexName).settings(settings)) + .build(); if (withAlias) { metadata = SecurityTestUtils.addAliasToMetadata(metadata, securityIndexName); @@ -208,62 +226,110 @@ public void setup() { userDashIndices = new User("dash", "dash"); userNoIndices = new User("test", "test"); rolesStore = mock(CompositeRolesStore.class); - String[] authorizedIndices = new String[] { "bar", "bar-closed", "foofoobar", "foobarfoo", "foofoo", "missing", "foofoo-closed", - "hidden-open", "hidden-closed", ".hidden-open", ".hidden-closed", "date-hidden-" + todaySuffix, - "date-hidden-" + tomorrowSuffix}; - String[] dashIndices = new String[]{"-index10", "-index11", "-index20", "-index21"}; + String[] authorizedIndices = new String[] { + "bar", + "bar-closed", + "foofoobar", + "foobarfoo", + "foofoo", + "missing", + "foofoo-closed", + "hidden-open", + "hidden-closed", + ".hidden-open", + ".hidden-closed", + "date-hidden-" + todaySuffix, + "date-hidden-" + tomorrowSuffix }; + String[] dashIndices = new String[] { "-index10", "-index11", "-index20", "-index21" }; roleMap = new HashMap<>(); - roleMap.put("role", new RoleDescriptor("role", null, - new IndicesPrivileges[] { IndicesPrivileges.builder().indices(authorizedIndices).privileges("all").build() }, null)); - roleMap.put("dash", new RoleDescriptor("dash", null, - new IndicesPrivileges[] { IndicesPrivileges.builder().indices(dashIndices).privileges("all").build() }, null)); + roleMap.put( + "role", + new RoleDescriptor( + "role", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices(authorizedIndices).privileges("all").build() }, + null + ) + ); + roleMap.put( + "dash", + new RoleDescriptor( + "dash", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices(dashIndices).privileges("all").build() }, + null + ) + ); roleMap.put("test", new RoleDescriptor("test", new String[] { "monitor" }, null, null)); - roleMap.put("alias_read_write", new RoleDescriptor("alias_read_write", null, - new IndicesPrivileges[] { IndicesPrivileges.builder().indices("barbaz", "foofoobar").privileges("read", "write").build() }, - null)); - roleMap.put("hidden_alias_test", new RoleDescriptor("hidden_alias_test", null, - new IndicesPrivileges[] { - IndicesPrivileges.builder() - .indices("alias-visible", "alias-visible-mixed", "alias-hidden", ".alias-hidden", "hidden-open") - .privileges("all") - .build() - }, null)); + roleMap.put( + "alias_read_write", + new RoleDescriptor( + "alias_read_write", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices("barbaz", "foofoobar").privileges("read", "write").build() }, + null + ) + ); + roleMap.put( + "hidden_alias_test", + new RoleDescriptor( + "hidden_alias_test", + null, + new IndicesPrivileges[] { + IndicesPrivileges.builder() + .indices("alias-visible", "alias-visible-mixed", "alias-hidden", ".alias-hidden", "hidden-open") + .privileges("all") + .build() }, + null + ) + ); roleMap.put(ReservedRolesStore.SUPERUSER_ROLE_DESCRIPTOR.getName(), ReservedRolesStore.SUPERUSER_ROLE_DESCRIPTOR); - roleMap.put("data_stream_test1", new RoleDescriptor("data_stream_test1", null, - new IndicesPrivileges[] { - IndicesPrivileges.builder() - .indices(dataStreamName + "*") - .privileges("all") - .build() - }, null)); - roleMap.put("data_stream_test2", new RoleDescriptor("data_stream_test2", null, - new IndicesPrivileges[] { - IndicesPrivileges.builder() - .indices(otherDataStreamName + "*") - .privileges("all") - .build() - }, null)); - roleMap.put("data_stream_test3", new RoleDescriptor("data_stream_test3", null, - new IndicesPrivileges[] { - IndicesPrivileges.builder() - .indices("logs*") - .privileges("all") - .build() - }, null)); - roleMap.put("backing_index_test_wildcards", new RoleDescriptor("backing_index_test_wildcards", null, - new IndicesPrivileges[] { - IndicesPrivileges.builder() - .indices(".ds-logs*") - .privileges("all") - .build() - }, null)); - roleMap.put("backing_index_test_name", new RoleDescriptor("backing_index_test_name", null, - new IndicesPrivileges[] { - IndicesPrivileges.builder() - .indices(dataStreamIndex1.getIndex().getName()) - .privileges("all") - .build() - }, null)); + roleMap.put( + "data_stream_test1", + new RoleDescriptor( + "data_stream_test1", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices(dataStreamName + "*").privileges("all").build() }, + null + ) + ); + roleMap.put( + "data_stream_test2", + new RoleDescriptor( + "data_stream_test2", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices(otherDataStreamName + "*").privileges("all").build() }, + null + ) + ); + roleMap.put( + "data_stream_test3", + new RoleDescriptor( + "data_stream_test3", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices("logs*").privileges("all").build() }, + null + ) + ); + roleMap.put( + "backing_index_test_wildcards", + new RoleDescriptor( + "backing_index_test_wildcards", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices(".ds-logs*").privileges("all").build() }, + null + ) + ); + roleMap.put( + "backing_index_test_name", + new RoleDescriptor( + "backing_index_test_name", + null, + new IndicesPrivileges[] { + IndicesPrivileges.builder().indices(dataStreamIndex1.getIndex().getName()).privileges("all").build() }, + null + ) + ); final FieldPermissionsCache fieldPermissionsCache = new FieldPermissionsCache(Settings.EMPTY); doAnswer((i) -> { @SuppressWarnings("unchecked") @@ -282,8 +348,12 @@ public void setup() { if (roleDescriptors.isEmpty()) { callback.onResponse(Role.EMPTY); } else { - CompositeRolesStore.buildRoleFromDescriptors(roleDescriptors, fieldPermissionsCache, null, - RESTRICTED_INDICES_AUTOMATON, ActionListener.wrap(r -> callback.onResponse(r), callback::onFailure) + CompositeRolesStore.buildRoleFromDescriptors( + roleDescriptors, + fieldPermissionsCache, + null, + RESTRICTED_INDICES_AUTOMATON, + ActionListener.wrap(r -> callback.onResponse(r), callback::onFailure) ); } return Void.TYPE; @@ -298,16 +368,15 @@ public void setup() { return Void.TYPE; } if (XPackSecurityUser.is(user)) { - listener.onResponse(Role.builder( - ReservedRolesStore.SUPERUSER_ROLE_DESCRIPTOR, - fieldPermissionsCache, - RESTRICTED_INDICES_AUTOMATON - ).build()); + listener.onResponse( + Role.builder(ReservedRolesStore.SUPERUSER_ROLE_DESCRIPTOR, fieldPermissionsCache, RESTRICTED_INDICES_AUTOMATON).build() + ); return Void.TYPE; } if (AsyncSearchUser.is(user)) { listener.onResponse( - Role.builder(AsyncSearchUser.ROLE_DESCRIPTOR, fieldPermissionsCache, RESTRICTED_INDICES_AUTOMATON).build()); + Role.builder(AsyncSearchUser.ROLE_DESCRIPTOR, fieldPermissionsCache, RESTRICTED_INDICES_AUTOMATON).build() + ); return Void.TYPE; } i.callRealMethod(); @@ -316,25 +385,24 @@ public void setup() { ClusterService clusterService = mock(ClusterService.class); when(clusterService.getClusterSettings()).thenReturn(new ClusterSettings(settings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS)); - defaultIndicesResolver = - new IndicesAndAliasesResolver(settings, clusterService, indexNameExpressionResolver); + defaultIndicesResolver = new IndicesAndAliasesResolver(settings, clusterService, indexNameExpressionResolver); } public void testDashIndicesAreAllowedInShardLevelRequests() { - //indices with names starting with '-' or '+' can be created up to version 2.x and can be around in 5.x - //aliases with names starting with '-' or '+' can be created up to version 5.x and can be around in 6.x + // indices with names starting with '-' or '+' can be created up to version 2.x and can be around in 5.x + // aliases with names starting with '-' or '+' can be created up to version 5.x and can be around in 6.x ShardSearchRequest request = mock(ShardSearchRequest.class); - when(request.indices()).thenReturn(new String[]{"-index10", "-index20", "+index30"}); + when(request.indices()).thenReturn(new String[] { "-index10", "-index20", "+index30" }); List indices = defaultIndicesResolver.resolveIndicesAndAliasesWithoutWildcards(SearchAction.NAME + "[s]", request) - .getLocal(); - String[] expectedIndices = new String[]{"-index10", "-index20", "+index30"}; + .getLocal(); + String[] expectedIndices = new String[] { "-index10", "-index20", "+index30" }; assertThat(indices, hasSize(expectedIndices.length)); assertThat(indices, hasItems(expectedIndices)); } public void testWildcardsAreNotAllowedInShardLevelRequests() { ShardSearchRequest request = mock(ShardSearchRequest.class); - when(request.indices()).thenReturn(new String[]{"index*"}); + when(request.indices()).thenReturn(new String[] { "index*" }); IllegalArgumentException exception = expectThrows( IllegalArgumentException.class, () -> defaultIndicesResolver.resolveIndicesAndAliasesWithoutWildcards(SearchAction.NAME + "[s]", request) @@ -352,7 +420,7 @@ public void testAllIsNotAllowedInShardLevelRequests() { ShardSearchRequest request = mock(ShardSearchRequest.class); final boolean literalAll = randomBoolean(); if (literalAll) { - when(request.indices()).thenReturn(new String[]{"_all"}); + when(request.indices()).thenReturn(new String[] { "_all" }); } else { if (randomBoolean()) { when(request.indices()).thenReturn(Strings.EMPTY_ARRAY); @@ -378,9 +446,8 @@ public void testAllIsNotAllowedInShardLevelRequests() { public void testExplicitDashIndices() { SearchRequest request = new SearchRequest("-index10", "-index20"); - List indices = - resolveIndices(request, buildAuthorizedIndices(userDashIndices, SearchAction.NAME)).getLocal(); - String[] expectedIndices = new String[]{"-index10", "-index20"}; + List indices = resolveIndices(request, buildAuthorizedIndices(userDashIndices, SearchAction.NAME)).getLocal(); + String[] expectedIndices = new String[] { "-index10", "-index20" }; assertThat(indices, hasSize(expectedIndices.length)); assertThat(request.indices().length, equalTo(expectedIndices.length)); assertThat(indices, hasItems(expectedIndices)); @@ -394,9 +461,8 @@ public void testWildcardDashIndices() { } else { request = new SearchRequest("*", "--index20"); } - List indices = - resolveIndices(request, buildAuthorizedIndices(userDashIndices, SearchAction.NAME)).getLocal(); - String[] expectedIndices = new String[]{"-index10", "-index11", "-index21"}; + List indices = resolveIndices(request, buildAuthorizedIndices(userDashIndices, SearchAction.NAME)).getLocal(); + String[] expectedIndices = new String[] { "-index10", "-index11", "-index21" }; assertThat(indices, hasSize(expectedIndices.length)); assertThat(request.indices().length, equalTo(expectedIndices.length)); assertThat(indices, hasItems(expectedIndices)); @@ -405,9 +471,8 @@ public void testWildcardDashIndices() { public void testExplicitMixedWildcardDashIndices() { SearchRequest request = new SearchRequest("-index21", "-does_not_exist", "-index1*", "--index11"); - List indices = - resolveIndices(request, buildAuthorizedIndices(userDashIndices, SearchAction.NAME)).getLocal(); - String[] expectedIndices = new String[]{"-index10", "-index21", "-does_not_exist"}; + List indices = resolveIndices(request, buildAuthorizedIndices(userDashIndices, SearchAction.NAME)).getLocal(); + String[] expectedIndices = new String[] { "-index10", "-index21", "-does_not_exist" }; assertThat(indices, hasSize(expectedIndices.length)); assertThat(request.indices().length, equalTo(expectedIndices.length)); assertThat(indices, hasItems(expectedIndices)); @@ -417,9 +482,8 @@ public void testExplicitMixedWildcardDashIndices() { public void testDashIndicesNoExpandWildcard() { SearchRequest request = new SearchRequest("-index1*", "--index11"); request.indicesOptions(IndicesOptions.fromOptions(false, randomBoolean(), false, false)); - List indices = - resolveIndices(request, buildAuthorizedIndices(userDashIndices, SearchAction.NAME)).getLocal(); - String[] expectedIndices = new String[]{"-index1*", "--index11"}; + List indices = resolveIndices(request, buildAuthorizedIndices(userDashIndices, SearchAction.NAME)).getLocal(); + String[] expectedIndices = new String[] { "-index1*", "--index11" }; assertThat(indices, hasSize(expectedIndices.length)); assertThat(request.indices().length, equalTo(expectedIndices.length)); assertThat(indices, hasItems(expectedIndices)); @@ -429,9 +493,8 @@ public void testDashIndicesNoExpandWildcard() { public void testDashIndicesMinus() { SearchRequest request = new SearchRequest("-index10", "-index11", "--index11", "-index20"); request.indicesOptions(IndicesOptions.fromOptions(false, randomBoolean(), randomBoolean(), randomBoolean())); - List indices = - resolveIndices(request, buildAuthorizedIndices(userDashIndices, SearchAction.NAME)).getLocal(); - String[] expectedIndices = new String[]{"-index10", "-index11", "--index11", "-index20"}; + List indices = resolveIndices(request, buildAuthorizedIndices(userDashIndices, SearchAction.NAME)).getLocal(); + String[] expectedIndices = new String[] { "-index10", "-index11", "--index11", "-index20" }; assertThat(indices, hasSize(expectedIndices.length)); assertThat(request.indices().length, equalTo(expectedIndices.length)); assertThat(indices, hasItems(expectedIndices)); @@ -441,15 +504,17 @@ public void testDashIndicesMinus() { public void testDashIndicesPlus() { SearchRequest request = new SearchRequest("+bar"); request.indicesOptions(IndicesOptions.fromOptions(true, false, randomBoolean(), randomBoolean())); - expectThrows(IndexNotFoundException.class, - () -> resolveIndices(request, buildAuthorizedIndices(userDashIndices, SearchAction.NAME))); + expectThrows( + IndexNotFoundException.class, + () -> resolveIndices(request, buildAuthorizedIndices(userDashIndices, SearchAction.NAME)) + ); } public void testDashNotExistingIndex() { SearchRequest request = new SearchRequest("-does_not_exist"); request.indicesOptions(IndicesOptions.fromOptions(false, randomBoolean(), randomBoolean(), randomBoolean())); List indices = resolveIndices(request, buildAuthorizedIndices(userDashIndices, SearchAction.NAME)).getLocal(); - String[] expectedIndices = new String[]{"-does_not_exist"}; + String[] expectedIndices = new String[] { "-does_not_exist" }; assertThat(indices, hasSize(expectedIndices.length)); assertThat(request.indices().length, equalTo(expectedIndices.length)); assertThat(indices, hasItems(expectedIndices)); @@ -460,7 +525,7 @@ public void testResolveEmptyIndicesExpandWilcardsOpenAndClosed() { SearchRequest request = new SearchRequest(); request.indicesOptions(IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), true, true)); List indices = resolveIndices(request, buildAuthorizedIndices(user, SearchAction.NAME)).getLocal(); - String[] replacedIndices = new String[]{"bar", "bar-closed", "foofoobar", "foobarfoo", "foofoo", "foofoo-closed"}; + String[] replacedIndices = new String[] { "bar", "bar-closed", "foofoobar", "foobarfoo", "foofoo", "foofoo-closed" }; assertThat(indices, hasSize(replacedIndices.length)); assertThat(request.indices().length, equalTo(replacedIndices.length)); assertThat(indices, hasItems(replacedIndices)); @@ -471,7 +536,7 @@ public void testResolveEmptyIndicesExpandWilcardsOpen() { SearchRequest request = new SearchRequest(); request.indicesOptions(IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), true, false)); List indices = resolveIndices(request, buildAuthorizedIndices(user, SearchAction.NAME)).getLocal(); - String[] replacedIndices = new String[]{"bar", "foofoobar", "foobarfoo", "foofoo"}; + String[] replacedIndices = new String[] { "bar", "foofoobar", "foobarfoo", "foofoo" }; assertSameValues(indices, replacedIndices); assertThat(request.indices(), arrayContainingInAnyOrder(replacedIndices)); } @@ -480,7 +545,7 @@ public void testResolveAllExpandWilcardsOpenAndClosed() { SearchRequest request = new SearchRequest("_all"); request.indicesOptions(IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), true, true)); List indices = resolveIndices(request, buildAuthorizedIndices(user, SearchAction.NAME)).getLocal(); - String[] replacedIndices = new String[]{"bar", "bar-closed", "foofoobar", "foobarfoo", "foofoo", "foofoo-closed"}; + String[] replacedIndices = new String[] { "bar", "bar-closed", "foofoobar", "foobarfoo", "foofoo", "foofoo-closed" }; assertThat(indices, hasSize(replacedIndices.length)); assertThat(request.indices().length, equalTo(replacedIndices.length)); assertThat(indices, hasItems(replacedIndices)); @@ -491,7 +556,7 @@ public void testResolveAllExpandWilcardsOpen() { SearchRequest request = new SearchRequest("_all"); request.indicesOptions(IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), true, false)); List indices = resolveIndices(request, buildAuthorizedIndices(user, SearchAction.NAME)).getLocal(); - String[] replacedIndices = new String[]{"bar", "foofoobar", "foobarfoo", "foofoo"}; + String[] replacedIndices = new String[] { "bar", "foofoobar", "foobarfoo", "foofoo" }; assertThat(indices, hasSize(replacedIndices.length)); assertThat(request.indices().length, equalTo(replacedIndices.length)); assertThat(indices, hasItems(replacedIndices)); @@ -502,7 +567,7 @@ public void testResolveWildcardsStrictExpand() { SearchRequest request = new SearchRequest("barbaz", "foofoo*"); request.indicesOptions(IndicesOptions.fromOptions(false, randomBoolean(), true, true)); List indices = resolveIndices(request, buildAuthorizedIndices(user, SearchAction.NAME)).getLocal(); - String[] replacedIndices = new String[]{"barbaz", "foofoobar", "foofoo", "foofoo-closed"}; + String[] replacedIndices = new String[] { "barbaz", "foofoobar", "foofoo", "foofoo-closed" }; assertThat(indices, hasSize(replacedIndices.length)); assertThat(request.indices().length, equalTo(replacedIndices.length)); assertThat(indices, hasItems(replacedIndices)); @@ -513,7 +578,7 @@ public void testResolveWildcardsExpandOpenAndClosedIgnoreUnavailable() { SearchRequest request = new SearchRequest("barbaz", "foofoo*"); request.indicesOptions(IndicesOptions.fromOptions(true, randomBoolean(), true, true)); List indices = resolveIndices(request, buildAuthorizedIndices(user, SearchAction.NAME)).getLocal(); - String[] replacedIndices = new String[]{"foofoobar", "foofoo", "foofoo-closed"}; + String[] replacedIndices = new String[] { "foofoobar", "foofoo", "foofoo-closed" }; assertThat(indices, hasSize(replacedIndices.length)); assertThat(request.indices().length, equalTo(replacedIndices.length)); assertThat(indices, hasItems(replacedIndices)); @@ -524,7 +589,7 @@ public void testResolveWildcardsStrictExpandOpen() { SearchRequest request = new SearchRequest("barbaz", "foofoo*"); request.indicesOptions(IndicesOptions.fromOptions(false, randomBoolean(), true, false)); List indices = resolveIndices(request, buildAuthorizedIndices(user, SearchAction.NAME)).getLocal(); - String[] replacedIndices = new String[]{"barbaz", "foofoobar", "foofoo"}; + String[] replacedIndices = new String[] { "barbaz", "foofoobar", "foofoo" }; assertThat(indices, hasSize(replacedIndices.length)); assertThat(request.indices().length, equalTo(replacedIndices.length)); assertThat(indices, hasItems(replacedIndices)); @@ -535,7 +600,7 @@ public void testResolveWildcardsLenientExpandOpen() { SearchRequest request = new SearchRequest("barbaz", "foofoo*"); request.indicesOptions(IndicesOptions.fromOptions(true, randomBoolean(), true, false)); List indices = resolveIndices(request, buildAuthorizedIndices(user, SearchAction.NAME)).getLocal(); - String[] replacedIndices = new String[]{"foofoobar", "foofoo"}; + String[] replacedIndices = new String[] { "foofoobar", "foofoo" }; assertThat(indices, hasSize(replacedIndices.length)); assertThat(request.indices().length, equalTo(replacedIndices.length)); assertThat(indices, hasItems(replacedIndices)); @@ -546,7 +611,7 @@ public void testResolveWildcardsMinusExpandWilcardsOpen() { SearchRequest request = new SearchRequest("*", "-foofoo*"); request.indicesOptions(IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), true, false)); List indices = resolveIndices(request, buildAuthorizedIndices(user, SearchAction.NAME)).getLocal(); - String[] replacedIndices = new String[]{"bar", "foobarfoo"}; + String[] replacedIndices = new String[] { "bar", "foobarfoo" }; assertThat(indices, hasSize(replacedIndices.length)); assertThat(request.indices().length, equalTo(replacedIndices.length)); assertThat(indices, hasItems(replacedIndices)); @@ -557,7 +622,7 @@ public void testResolveWildcardsMinusExpandWilcardsOpenAndClosed() { SearchRequest request = new SearchRequest("*", "-foofoo*"); request.indicesOptions(IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), true, true)); List indices = resolveIndices(request, buildAuthorizedIndices(user, SearchAction.NAME)).getLocal(); - String[] replacedIndices = new String[]{"bar", "foobarfoo", "bar-closed"}; + String[] replacedIndices = new String[] { "bar", "foobarfoo", "bar-closed" }; assertThat(indices, hasSize(replacedIndices.length)); assertThat(request.indices().length, equalTo(replacedIndices.length)); assertThat(indices, hasItems(replacedIndices)); @@ -568,7 +633,7 @@ public void testResolveWildcardsExclusionsExpandWilcardsOpenStrict() { SearchRequest request = new SearchRequest("*", "-foofoo*", "barbaz", "foob*"); request.indicesOptions(IndicesOptions.fromOptions(false, true, true, false)); List indices = resolveIndices(request, buildAuthorizedIndices(user, SearchAction.NAME)).getLocal(); - String[] replacedIndices = new String[]{"bar", "foobarfoo", "barbaz"}; + String[] replacedIndices = new String[] { "bar", "foobarfoo", "barbaz" }; assertSameValues(indices, replacedIndices); assertThat(request.indices(), arrayContainingInAnyOrder("bar", "foobarfoo", "barbaz", "foobarfoo")); } @@ -577,7 +642,7 @@ public void testResolveWildcardsPlusAndMinusExpandWilcardsOpenIgnoreUnavailable( SearchRequest request = new SearchRequest("*", "-foofoo*", "+barbaz", "+foob*"); request.indicesOptions(IndicesOptions.fromOptions(true, true, true, false)); List indices = resolveIndices(request, buildAuthorizedIndices(user, SearchAction.NAME)).getLocal(); - String[] replacedIndices = new String[]{"bar", "foobarfoo"}; + String[] replacedIndices = new String[] { "bar", "foobarfoo" }; assertThat(indices, hasSize(replacedIndices.length)); assertThat(request.indices().length, equalTo(replacedIndices.length)); assertThat(indices, hasItems(replacedIndices)); @@ -588,7 +653,7 @@ public void testResolveWildcardsExclusionExpandWilcardsOpenAndClosedStrict() { SearchRequest request = new SearchRequest("*", "-foofoo*", "barbaz"); request.indicesOptions(IndicesOptions.fromOptions(false, randomBoolean(), true, true)); List indices = resolveIndices(request, buildAuthorizedIndices(user, SearchAction.NAME)).getLocal(); - String[] replacedIndices = new String[]{"bar", "bar-closed", "barbaz", "foobarfoo"}; + String[] replacedIndices = new String[] { "bar", "bar-closed", "barbaz", "foobarfoo" }; assertSameValues(indices, replacedIndices); assertThat(request.indices(), arrayContainingInAnyOrder(replacedIndices)); } @@ -597,7 +662,7 @@ public void testResolveWildcardsExclusionExpandWilcardsOpenAndClosedIgnoreUnavai SearchRequest request = new SearchRequest("*", "-foofoo*", "barbaz"); request.indicesOptions(IndicesOptions.fromOptions(true, randomBoolean(), true, true)); List indices = resolveIndices(request, buildAuthorizedIndices(user, SearchAction.NAME)).getLocal(); - String[] replacedIndices = new String[]{"bar", "bar-closed", "foobarfoo"}; + String[] replacedIndices = new String[] { "bar", "bar-closed", "foobarfoo" }; assertThat(indices, hasSize(replacedIndices.length)); assertThat(indices, hasItems(replacedIndices)); assertThat(request.indices(), arrayContainingInAnyOrder(replacedIndices)); @@ -612,8 +677,10 @@ public void testResolveNonMatchingIndicesAllowNoIndices() { public void testResolveNonMatchingIndicesDisallowNoIndices() { SearchRequest request = new SearchRequest("missing*"); request.indicesOptions(IndicesOptions.fromOptions(randomBoolean(), false, true, randomBoolean())); - IndexNotFoundException e = expectThrows(IndexNotFoundException.class, - () -> resolveIndices(request, buildAuthorizedIndices(user, SearchAction.NAME))); + IndexNotFoundException e = expectThrows( + IndexNotFoundException.class, + () -> resolveIndices(request, buildAuthorizedIndices(user, SearchAction.NAME)) + ); assertEquals("no such index [missing*]", e.getMessage()); } @@ -621,7 +688,7 @@ public void testResolveExplicitIndicesStrict() { SearchRequest request = new SearchRequest("missing", "bar", "barbaz"); request.indicesOptions(IndicesOptions.fromOptions(false, randomBoolean(), randomBoolean(), randomBoolean())); List indices = resolveIndices(request, buildAuthorizedIndices(user, SearchAction.NAME)).getLocal(); - String[] replacedIndices = new String[]{"missing", "bar", "barbaz"}; + String[] replacedIndices = new String[] { "missing", "bar", "barbaz" }; assertThat(indices, hasSize(replacedIndices.length)); assertThat(request.indices().length, equalTo(replacedIndices.length)); assertThat(indices, hasItems(replacedIndices)); @@ -632,7 +699,7 @@ public void testResolveExplicitIndicesIgnoreUnavailable() { SearchRequest request = new SearchRequest("missing", "bar", "barbaz"); request.indicesOptions(IndicesOptions.fromOptions(true, randomBoolean(), randomBoolean(), randomBoolean())); List indices = resolveIndices(request, buildAuthorizedIndices(user, SearchAction.NAME)).getLocal(); - String[] replacedIndices = new String[]{"bar"}; + String[] replacedIndices = new String[] { "bar" }; assertThat(indices, hasSize(replacedIndices.length)); assertThat(request.indices().length, equalTo(replacedIndices.length)); assertThat(indices, hasItems(replacedIndices)); @@ -642,15 +709,16 @@ public void testResolveExplicitIndicesIgnoreUnavailable() { public void testResolveNoAuthorizedIndicesAllowNoIndices() { SearchRequest request = new SearchRequest(); request.indicesOptions(IndicesOptions.fromOptions(randomBoolean(), true, true, randomBoolean())); - assertNoIndices(request, resolveIndices(request, - buildAuthorizedIndices(userNoIndices, SearchAction.NAME))); + assertNoIndices(request, resolveIndices(request, buildAuthorizedIndices(userNoIndices, SearchAction.NAME))); } public void testResolveNoAuthorizedIndicesDisallowNoIndices() { SearchRequest request = new SearchRequest(); request.indicesOptions(IndicesOptions.fromOptions(randomBoolean(), false, true, randomBoolean())); - IndexNotFoundException e = expectThrows(IndexNotFoundException.class, - () -> resolveIndices(request, buildAuthorizedIndices(userNoIndices, SearchAction.NAME))); + IndexNotFoundException e = expectThrows( + IndexNotFoundException.class, + () -> resolveIndices(request, buildAuthorizedIndices(userNoIndices, SearchAction.NAME)) + ); assertEquals("no such index [[]]", e.getMessage()); } @@ -658,7 +726,7 @@ public void testResolveMissingIndexStrict() { SearchRequest request = new SearchRequest("bar*", "missing"); request.indicesOptions(IndicesOptions.fromOptions(false, true, true, false)); List indices = resolveIndices(request, buildAuthorizedIndices(user, SearchAction.NAME)).getLocal(); - String[] expectedIndices = new String[]{"bar", "missing"}; + String[] expectedIndices = new String[] { "bar", "missing" }; assertThat(indices, hasSize(expectedIndices.length)); assertThat(request.indices().length, equalTo(expectedIndices.length)); assertThat(indices, hasItems(expectedIndices)); @@ -669,7 +737,7 @@ public void testResolveMissingIndexIgnoreUnavailable() { SearchRequest request = new SearchRequest("bar*", "missing"); request.indicesOptions(IndicesOptions.fromOptions(true, randomBoolean(), true, false)); List indices = resolveIndices(request, buildAuthorizedIndices(user, SearchAction.NAME)).getLocal(); - String[] expectedIndices = new String[]{"bar"}; + String[] expectedIndices = new String[] { "bar" }; assertThat(indices, hasSize(expectedIndices.length)); assertThat(request.indices().length, equalTo(expectedIndices.length)); assertThat(indices, hasItems(expectedIndices)); @@ -680,7 +748,7 @@ public void testResolveNonMatchingIndicesAndExplicit() { SearchRequest request = new SearchRequest("missing*", "bar"); request.indicesOptions(IndicesOptions.fromOptions(randomBoolean(), true, true, randomBoolean())); List indices = resolveIndices(request, buildAuthorizedIndices(user, SearchAction.NAME)).getLocal(); - String[] expectedIndices = new String[]{"bar"}; + String[] expectedIndices = new String[] { "bar" }; assertThat(indices.toArray(new String[indices.size()]), equalTo(expectedIndices)); assertThat(request.indices(), equalTo(expectedIndices)); } @@ -689,7 +757,7 @@ public void testResolveNoExpandStrict() { SearchRequest request = new SearchRequest("missing*"); request.indicesOptions(IndicesOptions.fromOptions(false, randomBoolean(), false, false)); List indices = resolveIndices(request, buildAuthorizedIndices(user, SearchAction.NAME)).getLocal(); - String[] expectedIndices = new String[]{"missing*"}; + String[] expectedIndices = new String[] { "missing*" }; assertThat(indices.toArray(new String[indices.size()]), equalTo(expectedIndices)); assertThat(request.indices(), equalTo(expectedIndices)); } @@ -725,18 +793,19 @@ public void testSearchWithRemoteAndLocalWildcards() { final ResolvedIndices resolved = resolveIndices(request, authorizedIndices); assertThat(resolved.getRemote(), containsInAnyOrder("remote:foo", "other_remote:foo", "remote:bar*", "remote:baz*")); assertThat(resolved.getLocal(), containsInAnyOrder("bar", "foofoo")); - assertThat(request.indices(), - arrayContainingInAnyOrder("remote:foo", "other_remote:foo", "remote:bar*", "remote:baz*", "bar", "foofoo")); + assertThat( + request.indices(), + arrayContainingInAnyOrder("remote:foo", "other_remote:foo", "remote:bar*", "remote:baz*", "bar", "foofoo") + ); } public void testResolveIndicesAliasesRequest() { IndicesAliasesRequest request = new IndicesAliasesRequest(); request.addAliasAction(AliasActions.add().alias("alias1").indices("foo", "foofoo")); request.addAliasAction(AliasActions.add().alias("alias2").indices("foo", "foobar")); - List indices = - resolveIndices(request, buildAuthorizedIndices(user, IndicesAliasesAction.NAME)).getLocal(); - //the union of all indices and aliases gets returned - String[] expectedIndices = new String[]{"alias1", "alias2", "foo", "foofoo", "foobar"}; + List indices = resolveIndices(request, buildAuthorizedIndices(user, IndicesAliasesAction.NAME)).getLocal(); + // the union of all indices and aliases gets returned + String[] expectedIndices = new String[] { "alias1", "alias2", "foo", "foofoo", "foobar" }; assertSameValues(indices, expectedIndices); assertThat(request.getAliasActions().get(0).indices(), arrayContainingInAnyOrder("foo", "foofoo")); assertThat(request.getAliasActions().get(0).aliases(), arrayContainingInAnyOrder("alias1")); @@ -748,10 +817,9 @@ public void testResolveIndicesAliasesRequestExistingAlias() { IndicesAliasesRequest request = new IndicesAliasesRequest(); request.addAliasAction(AliasActions.add().alias("alias1").indices("foo", "foofoo")); request.addAliasAction(AliasActions.add().alias("foofoobar").indices("foo", "foobar")); - List indices = - resolveIndices(request, buildAuthorizedIndices(user, IndicesAliasesAction.NAME)).getLocal(); - //the union of all indices and aliases gets returned, foofoobar is an existing alias but that doesn't make any difference - String[] expectedIndices = new String[]{"alias1", "foofoobar", "foo", "foofoo", "foobar"}; + List indices = resolveIndices(request, buildAuthorizedIndices(user, IndicesAliasesAction.NAME)).getLocal(); + // the union of all indices and aliases gets returned, foofoobar is an existing alias but that doesn't make any difference + String[] expectedIndices = new String[] { "alias1", "foofoobar", "foo", "foofoo", "foobar" }; assertSameValues(indices, expectedIndices); assertThat(request.getAliasActions().get(0).indices(), arrayContainingInAnyOrder("foo", "foofoo")); assertThat(request.getAliasActions().get(0).aliases(), arrayContainingInAnyOrder("alias1")); @@ -763,10 +831,9 @@ public void testResolveIndicesAliasesRequestMissingIndex() { IndicesAliasesRequest request = new IndicesAliasesRequest(); request.addAliasAction(AliasActions.add().alias("alias1").indices("foo", "foofoo")); request.addAliasAction(AliasActions.add().alias("alias2").index("missing")); - List indices = - resolveIndices(request, buildAuthorizedIndices(user, IndicesAliasesAction.NAME)).getLocal(); - //the union of all indices and aliases gets returned, missing is not an existing index/alias but that doesn't make any difference - String[] expectedIndices = new String[]{"alias1", "alias2", "foo", "foofoo", "missing"}; + List indices = resolveIndices(request, buildAuthorizedIndices(user, IndicesAliasesAction.NAME)).getLocal(); + // the union of all indices and aliases gets returned, missing is not an existing index/alias but that doesn't make any difference + String[] expectedIndices = new String[] { "alias1", "alias2", "foo", "foofoo", "missing" }; assertThat(indices, hasSize(expectedIndices.length)); assertThat(indices, hasItems(expectedIndices)); assertThat(request.getAliasActions().get(0).indices(), arrayContainingInAnyOrder("foo", "foofoo")); @@ -779,13 +846,12 @@ public void testResolveWildcardsIndicesAliasesRequest() { IndicesAliasesRequest request = new IndicesAliasesRequest(); request.addAliasAction(AliasActions.add().alias("foo-alias").index("foo*")); request.addAliasAction(AliasActions.add().alias("alias2").index("bar*")); - List indices = - resolveIndices(request, buildAuthorizedIndices(user, IndicesAliasesAction.NAME)).getLocal(); - //the union of all resolved indices and aliases gets returned, based on indices and aliases that user is authorized for - String[] expectedIndices = new String[]{"foo-alias", "alias2", "foofoo", "bar"}; + List indices = resolveIndices(request, buildAuthorizedIndices(user, IndicesAliasesAction.NAME)).getLocal(); + // the union of all resolved indices and aliases gets returned, based on indices and aliases that user is authorized for + String[] expectedIndices = new String[] { "foo-alias", "alias2", "foofoo", "bar" }; assertThat(indices, hasSize(expectedIndices.length)); assertThat(indices, hasItems(expectedIndices)); - //wildcards get replaced on each single action + // wildcards get replaced on each single action assertThat(request.getAliasActions().get(0).indices(), arrayContainingInAnyOrder("foofoo")); assertThat(request.getAliasActions().get(0).aliases(), arrayContainingInAnyOrder("foo-alias")); assertThat(request.getAliasActions().get(1).indices(), arrayContainingInAnyOrder("bar")); @@ -797,22 +863,20 @@ public void testResolveWildcardsIndicesAliasesRequestNoMatchingIndices() { request.addAliasAction(AliasActions.add().alias("alias1").index("foo*")); request.addAliasAction(AliasActions.add().alias("alias2").index("bar*")); request.addAliasAction(AliasActions.add().alias("alias3").index("non_matching_*")); - //if a single operation contains wildcards and ends up being resolved to no indices, it makes the whole request fail - expectThrows(IndexNotFoundException.class, - () -> resolveIndices(request, buildAuthorizedIndices(user, IndicesAliasesAction.NAME))); + // if a single operation contains wildcards and ends up being resolved to no indices, it makes the whole request fail + expectThrows(IndexNotFoundException.class, () -> resolveIndices(request, buildAuthorizedIndices(user, IndicesAliasesAction.NAME))); } public void testResolveAllIndicesAliasesRequest() { IndicesAliasesRequest request = new IndicesAliasesRequest(); request.addAliasAction(AliasActions.add().alias("alias1").index("_all")); request.addAliasAction(AliasActions.add().alias("alias2").index("_all")); - List indices = - resolveIndices(request, buildAuthorizedIndices(user, IndicesAliasesAction.NAME)).getLocal(); - //the union of all resolved indices and aliases gets returned - String[] expectedIndices = new String[]{"bar", "foofoo", "alias1", "alias2"}; + List indices = resolveIndices(request, buildAuthorizedIndices(user, IndicesAliasesAction.NAME)).getLocal(); + // the union of all resolved indices and aliases gets returned + String[] expectedIndices = new String[] { "bar", "foofoo", "alias1", "alias2" }; assertSameValues(indices, expectedIndices); - String[] replacedIndices = new String[]{"bar", "foofoo"}; - //_all gets replaced with all indices that user is authorized for, on each single action + String[] replacedIndices = new String[] { "bar", "foofoo" }; + // _all gets replaced with all indices that user is authorized for, on each single action assertThat(request.getAliasActions().get(0).indices(), arrayContainingInAnyOrder(replacedIndices)); assertThat(request.getAliasActions().get(0).aliases(), arrayContainingInAnyOrder("alias1")); assertThat(request.getAliasActions().get(1).indices(), arrayContainingInAnyOrder(replacedIndices)); @@ -822,17 +886,21 @@ public void testResolveAllIndicesAliasesRequest() { public void testResolveAllIndicesAliasesRequestNoAuthorizedIndices() { IndicesAliasesRequest request = new IndicesAliasesRequest(); request.addAliasAction(AliasActions.add().alias("alias1").index("_all")); - //current user is not authorized for any index, _all resolves to no indices, the request fails - expectThrows(IndexNotFoundException.class, () -> - resolveIndices(request, buildAuthorizedIndices(userNoIndices, IndicesAliasesAction.NAME))); + // current user is not authorized for any index, _all resolves to no indices, the request fails + expectThrows( + IndexNotFoundException.class, + () -> resolveIndices(request, buildAuthorizedIndices(userNoIndices, IndicesAliasesAction.NAME)) + ); } public void testResolveWildcardsIndicesAliasesRequestNoAuthorizedIndices() { IndicesAliasesRequest request = new IndicesAliasesRequest(); request.addAliasAction(AliasActions.add().alias("alias1").index("foo*")); - //current user is not authorized for any index, foo* resolves to no indices, the request fails - expectThrows(IndexNotFoundException.class, () -> resolveIndices( - request, buildAuthorizedIndices(userNoIndices, IndicesAliasesAction.NAME))); + // current user is not authorized for any index, foo* resolves to no indices, the request fails + expectThrows( + IndexNotFoundException.class, + () -> resolveIndices(request, buildAuthorizedIndices(userNoIndices, IndicesAliasesAction.NAME)) + ); } public void testResolveIndicesAliasesRequestDeleteActions() { @@ -841,8 +909,8 @@ public void testResolveIndicesAliasesRequestDeleteActions() { request.addAliasAction(AliasActions.remove().index("foofoo").alias("barbaz")); final Set authorizedIndices = buildAuthorizedIndices(user, IndicesAliasesAction.NAME); List indices = resolveIndices(request, authorizedIndices).getLocal(); - //the union of all indices and aliases gets returned - String[] expectedIndices = new String[]{"foo", "foofoobar", "foofoo", "barbaz"}; + // the union of all indices and aliases gets returned + String[] expectedIndices = new String[] { "foo", "foofoobar", "foofoo", "barbaz" }; assertThat(indices, hasSize(expectedIndices.length)); assertThat(indices, hasItems(expectedIndices)); assertThat(request.getAliasActions().get(0).indices(), arrayContainingInAnyOrder("foo")); @@ -857,8 +925,8 @@ public void testResolveIndicesAliasesRequestDeleteActionsMissingIndex() { request.addAliasAction(AliasActions.remove().index("missing_index").alias("missing_alias")); final Set authorizedIndices = buildAuthorizedIndices(user, IndicesAliasesAction.NAME); List indices = resolveIndices(request, authorizedIndices).getLocal(); - //the union of all indices and aliases gets returned, doesn't matter is some of them don't exist - String[] expectedIndices = new String[]{"foo", "foofoobar", "missing_index", "missing_alias"}; + // the union of all indices and aliases gets returned, doesn't matter is some of them don't exist + String[] expectedIndices = new String[] { "foo", "foofoobar", "missing_index", "missing_alias" }; assertThat(indices, hasSize(expectedIndices.length)); assertThat(indices, hasItems(expectedIndices)); assertThat(request.getAliasActions().get(0).indices(), arrayContainingInAnyOrder("foo")); @@ -873,11 +941,11 @@ public void testResolveWildcardsIndicesAliasesRequestDeleteActions() { request.addAliasAction(AliasActions.remove().index("bar*").alias("barbaz")); final Set authorizedIndices = buildAuthorizedIndices(user, IndicesAliasesAction.NAME); List indices = resolveIndices(request, authorizedIndices).getLocal(); - //union of all resolved indices and aliases gets returned, based on what user is authorized for - String[] expectedIndices = new String[]{"foofoobar", "foofoo", "bar", "barbaz"}; + // union of all resolved indices and aliases gets returned, based on what user is authorized for + String[] expectedIndices = new String[] { "foofoobar", "foofoo", "bar", "barbaz" }; assertThat(indices, hasSize(expectedIndices.length)); assertThat(indices, hasItems(expectedIndices)); - //wildcards get replaced within each single action + // wildcards get replaced within each single action assertThat(request.getAliasActions().get(0).indices(), arrayContainingInAnyOrder("foofoo")); assertThat(request.getAliasActions().get(0).aliases(), arrayContainingInAnyOrder("foofoobar")); assertThat(request.getAliasActions().get(1).indices(), arrayContainingInAnyOrder("bar")); @@ -890,12 +958,12 @@ public void testResolveAliasesWildcardsIndicesAliasesRequestDeleteActions() { request.addAliasAction(AliasActions.remove().index("*bar").alias("foo*")); final Set authorizedIndices = buildAuthorizedIndices(user, IndicesAliasesAction.NAME); List indices = resolveIndices(request, authorizedIndices).getLocal(); - //union of all resolved indices and aliases gets returned, based on what user is authorized for - //note that the index side will end up containing matching aliases too, which is fine, as es core would do - //the same and resolve those aliases to their corresponding concrete indices (which we let core do) - String[] expectedIndices = new String[]{"bar", "foofoobar", "foobarfoo", "foofoo"}; + // union of all resolved indices and aliases gets returned, based on what user is authorized for + // note that the index side will end up containing matching aliases too, which is fine, as es core would do + // the same and resolve those aliases to their corresponding concrete indices (which we let core do) + String[] expectedIndices = new String[] { "bar", "foofoobar", "foobarfoo", "foofoo" }; assertSameValues(indices, expectedIndices); - //alias foofoobar on both sides, that's fine, es core would do the same, same as above + // alias foofoobar on both sides, that's fine, es core would do the same, same as above assertThat(request.getAliasActions().get(0).indices(), arrayContainingInAnyOrder("bar", "foofoo")); assertThat(request.getAliasActions().get(0).aliases(), arrayContainingInAnyOrder("foofoobar", "foobarfoo")); assertThat(request.getAliasActions().get(1).indices(), arrayContainingInAnyOrder("bar")); @@ -908,12 +976,12 @@ public void testResolveAllAliasesWildcardsIndicesAliasesRequestDeleteActions() { request.addAliasAction(AliasActions.remove().index("_all").aliases("_all", "explicit")); final Set authorizedIndices = buildAuthorizedIndices(user, IndicesAliasesAction.NAME); List indices = resolveIndices(request, authorizedIndices).getLocal(); - //union of all resolved indices and aliases gets returned, based on what user is authorized for - //note that the index side will end up containing matching aliases too, which is fine, as es core would do - //the same and resolve those aliases to their corresponding concrete indices (which we let core do) - String[] expectedIndices = new String[]{"bar", "foofoobar", "foobarfoo", "foofoo", "explicit"}; + // union of all resolved indices and aliases gets returned, based on what user is authorized for + // note that the index side will end up containing matching aliases too, which is fine, as es core would do + // the same and resolve those aliases to their corresponding concrete indices (which we let core do) + String[] expectedIndices = new String[] { "bar", "foofoobar", "foobarfoo", "foofoo", "explicit" }; assertSameValues(indices, expectedIndices); - //alias foofoobar on both sides, that's fine, es core would do the same, same as above + // alias foofoobar on both sides, that's fine, es core would do the same, same as above assertThat(request.getAliasActions().get(0).indices(), arrayContainingInAnyOrder("bar", "foofoo")); assertThat(request.getAliasActions().get(0).aliases(), arrayContainingInAnyOrder("foofoobar", "foobarfoo")); assertThat(request.getAliasActions().get(0).indices(), arrayContainingInAnyOrder("bar", "foofoo")); @@ -946,10 +1014,10 @@ public void testResolveWildcardsIndicesAliasesRequestAddAndDeleteActions() { request.addAliasAction(AliasActions.add().index("bar*").alias("foofoobar")); final Set authorizedIndices = buildAuthorizedIndices(user, IndicesAliasesAction.NAME); List indices = resolveIndices(request, authorizedIndices).getLocal(); - //union of all resolved indices and aliases gets returned, based on what user is authorized for - String[] expectedIndices = new String[]{"foofoobar", "foofoo", "bar"}; + // union of all resolved indices and aliases gets returned, based on what user is authorized for + String[] expectedIndices = new String[] { "foofoobar", "foofoo", "bar" }; assertSameValues(indices, expectedIndices); - //every single action has its indices replaced with matching (authorized) ones + // every single action has its indices replaced with matching (authorized) ones assertThat(request.getAliasActions().get(0).indices(), arrayContainingInAnyOrder("foofoo")); assertThat(request.getAliasActions().get(0).aliases(), arrayContainingInAnyOrder("foofoobar")); assertThat(request.getAliasActions().get(1).indices(), arrayContainingInAnyOrder("bar")); @@ -961,8 +1029,8 @@ public void testResolveGetAliasesRequestStrict() { request.indicesOptions(IndicesOptions.fromOptions(false, randomBoolean(), randomBoolean(), randomBoolean())); final Set authorizedIndices = buildAuthorizedIndices(user, GetAliasesAction.NAME); List indices = resolveIndices(request, authorizedIndices).getLocal(); - //the union of all indices and aliases gets returned - String[] expectedIndices = new String[]{"alias1", "foo", "foofoo"}; + // the union of all indices and aliases gets returned + String[] expectedIndices = new String[] { "alias1", "foo", "foofoo" }; assertThat(indices, hasSize(expectedIndices.length)); assertThat(indices, hasItems(expectedIndices)); assertThat(request.indices(), arrayContainingInAnyOrder("foo", "foofoo")); @@ -974,7 +1042,7 @@ public void testResolveGetAliasesRequestIgnoreUnavailable() { request.indicesOptions(IndicesOptions.fromOptions(true, randomBoolean(), randomBoolean(), randomBoolean())); final Set authorizedIndices = buildAuthorizedIndices(user, GetAliasesAction.NAME); List indices = resolveIndices(request, authorizedIndices).getLocal(); - String[] expectedIndices = new String[]{"alias1", "foofoo"}; + String[] expectedIndices = new String[] { "alias1", "foofoo" }; assertThat(indices, hasSize(expectedIndices.length)); assertThat(indices, hasItems(expectedIndices)); assertThat(request.indices(), arrayContainingInAnyOrder("foofoo")); @@ -988,8 +1056,8 @@ public void testResolveGetAliasesRequestMissingIndexStrict() { request.aliases("alias2"); final Set authorizedIndices = buildAuthorizedIndices(user, GetAliasesAction.NAME); List indices = resolveIndices(request, authorizedIndices).getLocal(); - //the union of all indices and aliases gets returned, missing is not an existing index/alias but that doesn't make any difference - String[] expectedIndices = new String[]{"alias2", "missing"}; + // the union of all indices and aliases gets returned, missing is not an existing index/alias but that doesn't make any difference + String[] expectedIndices = new String[] { "alias2", "missing" }; assertThat(indices, hasSize(expectedIndices.length)); assertThat(indices, hasItems(expectedIndices)); assertThat(request.indices(), arrayContainingInAnyOrder("missing")); @@ -1001,8 +1069,10 @@ public void testGetAliasesRequestMissingIndexIgnoreUnavailableDisallowNoIndices( request.indicesOptions(IndicesOptions.fromOptions(true, false, randomBoolean(), randomBoolean())); request.indices("missing"); request.aliases("alias2"); - IndexNotFoundException exception = expectThrows(IndexNotFoundException.class, - () -> resolveIndices(request, buildAuthorizedIndices(user, GetAliasesAction.NAME)).getLocal()); + IndexNotFoundException exception = expectThrows( + IndexNotFoundException.class, + () -> resolveIndices(request, buildAuthorizedIndices(user, GetAliasesAction.NAME)).getLocal() + ); assertEquals("no such index [[missing]]", exception.getMessage()); } @@ -1021,7 +1091,7 @@ public void testGetAliasesRequestMissingIndexStrict() { request.aliases("alias2"); final Set authorizedIndices = buildAuthorizedIndices(user, GetAliasesAction.NAME); List indices = resolveIndices(request, authorizedIndices).getLocal(); - String[] expectedIndices = new String[]{"alias2", "missing"}; + String[] expectedIndices = new String[] { "alias2", "missing" }; assertThat(indices, hasSize(expectedIndices.length)); assertThat(indices, hasItems(expectedIndices)); assertThat(request.indices(), arrayContainingInAnyOrder("missing")); @@ -1035,11 +1105,11 @@ public void testResolveWildcardsGetAliasesRequestStrictExpand() { request.indices("foo*"); final Set authorizedIndices = buildAuthorizedIndices(user, GetAliasesAction.NAME); List indices = resolveIndices(request, authorizedIndices).getLocal(); - //the union of all resolved indices and aliases gets returned, based on indices and aliases that user is authorized for - String[] expectedIndices = new String[]{"alias1", "foofoo", "foofoo-closed", "foofoobar", "foobarfoo"}; + // the union of all resolved indices and aliases gets returned, based on indices and aliases that user is authorized for + String[] expectedIndices = new String[] { "alias1", "foofoo", "foofoo-closed", "foofoobar", "foobarfoo" }; assertThat(indices, hasSize(expectedIndices.length)); assertThat(indices, hasItems(expectedIndices)); - //wildcards get replaced on each single action + // wildcards get replaced on each single action assertThat(request.indices(), arrayContainingInAnyOrder("foofoobar", "foobarfoo", "foofoo", "foofoo-closed")); assertThat(request.aliases(), arrayContainingInAnyOrder("alias1")); } @@ -1051,11 +1121,11 @@ public void testResolveWildcardsGetAliasesRequestStrictExpandOpen() { request.indices("foo*"); final Set authorizedIndices = buildAuthorizedIndices(user, GetAliasesAction.NAME); List indices = resolveIndices(request, authorizedIndices).getLocal(); - //the union of all resolved indices and aliases gets returned, based on indices and aliases that user is authorized for - String[] expectedIndices = new String[]{"alias1", "foofoo", "foofoobar", "foobarfoo"}; + // the union of all resolved indices and aliases gets returned, based on indices and aliases that user is authorized for + String[] expectedIndices = new String[] { "alias1", "foofoo", "foofoobar", "foobarfoo" }; assertThat(indices, hasSize(expectedIndices.length)); assertThat(indices, hasItems(expectedIndices)); - //wildcards get replaced on each single action + // wildcards get replaced on each single action assertThat(request.indices(), arrayContainingInAnyOrder("foofoobar", "foobarfoo", "foofoo")); assertThat(request.aliases(), arrayContainingInAnyOrder("alias1")); } @@ -1067,11 +1137,11 @@ public void testResolveWildcardsGetAliasesRequestLenientExpandOpen() { request.indices("foo*", "bar", "missing"); final Set authorizedIndices = buildAuthorizedIndices(user, GetAliasesAction.NAME); List indices = resolveIndices(request, authorizedIndices).getLocal(); - //the union of all resolved indices and aliases gets returned, based on indices and aliases that user is authorized for - String[] expectedIndices = new String[]{"alias1", "foofoo", "foofoobar", "foobarfoo", "bar"}; + // the union of all resolved indices and aliases gets returned, based on indices and aliases that user is authorized for + String[] expectedIndices = new String[] { "alias1", "foofoo", "foofoobar", "foobarfoo", "bar" }; assertThat(indices, hasSize(expectedIndices.length)); assertThat(indices, hasItems(expectedIndices)); - //wildcards get replaced on each single action + // wildcards get replaced on each single action assertThat(request.indices(), arrayContainingInAnyOrder("foofoobar", "foobarfoo", "foofoo", "bar")); assertThat(request.aliases(), arrayContainingInAnyOrder("alias1")); } @@ -1081,8 +1151,10 @@ public void testWildcardsGetAliasesRequestNoMatchingIndicesDisallowNoIndices() { request.indicesOptions(IndicesOptions.fromOptions(randomBoolean(), false, true, randomBoolean())); request.aliases("alias3"); request.indices("non_matching_*"); - IndexNotFoundException e = expectThrows(IndexNotFoundException.class, - () -> resolveIndices(request, buildAuthorizedIndices(user, GetAliasesAction.NAME)).getLocal()); + IndexNotFoundException e = expectThrows( + IndexNotFoundException.class, + () -> resolveIndices(request, buildAuthorizedIndices(user, GetAliasesAction.NAME)).getLocal() + ); assertEquals("no such index [non_matching_*]", e.getMessage()); } @@ -1096,42 +1168,64 @@ public void testWildcardsGetAliasesRequestNoMatchingIndicesAllowNoIndices() { public void testResolveAllGetAliasesRequest() { GetAliasesRequest request = new GetAliasesRequest(); - //even if not set, empty means _all + // even if not set, empty means _all if (randomBoolean()) { request.indices("_all"); } request.aliases("alias1"); final Set authorizedIndices = buildAuthorizedIndices(user, GetAliasesAction.NAME); List indices = resolveIndices(request, authorizedIndices).getLocal(); - //the union of all resolved indices and aliases gets returned, including hidden indices as Get Aliases includes hidden by default - String[] expectedIndices = new String[]{"bar", "bar-closed", "foofoobar", "foobarfoo", "foofoo", "foofoo-closed", "alias1", - "hidden-open", "hidden-closed", ".hidden-open", ".hidden-closed", "date-hidden-" + todaySuffix, - "date-hidden-" + tomorrowSuffix}; + // the union of all resolved indices and aliases gets returned, including hidden indices as Get Aliases includes hidden by default + String[] expectedIndices = new String[] { + "bar", + "bar-closed", + "foofoobar", + "foobarfoo", + "foofoo", + "foofoo-closed", + "alias1", + "hidden-open", + "hidden-closed", + ".hidden-open", + ".hidden-closed", + "date-hidden-" + todaySuffix, + "date-hidden-" + tomorrowSuffix }; assertSameValues(indices, expectedIndices); - String[] replacedIndices = new String[]{"bar", "bar-closed", "foofoobar", "foobarfoo", "foofoo", "foofoo-closed", "hidden-open", - "hidden-closed", ".hidden-open", ".hidden-closed", "date-hidden-" + todaySuffix, "date-hidden-" + tomorrowSuffix}; - //_all gets replaced with all indices that user is authorized for + String[] replacedIndices = new String[] { + "bar", + "bar-closed", + "foofoobar", + "foobarfoo", + "foofoo", + "foofoo-closed", + "hidden-open", + "hidden-closed", + ".hidden-open", + ".hidden-closed", + "date-hidden-" + todaySuffix, + "date-hidden-" + tomorrowSuffix }; + // _all gets replaced with all indices that user is authorized for assertThat(request.indices(), arrayContainingInAnyOrder(replacedIndices)); assertThat(request.aliases(), arrayContainingInAnyOrder("alias1")); } public void testResolveAllGetAliasesRequestExpandWildcardsOpenOnly() { GetAliasesRequest request = new GetAliasesRequest(); - //set indices options to have wildcards resolved to open indices only (default is open and closed) + // set indices options to have wildcards resolved to open indices only (default is open and closed) request.indicesOptions(IndicesOptions.fromOptions(true, false, true, false)); - //even if not set, empty means _all + // even if not set, empty means _all if (randomBoolean()) { request.indices("_all"); } request.aliases("alias1"); final Set authorizedIndices = buildAuthorizedIndices(user, GetAliasesAction.NAME); List indices = resolveIndices(request, authorizedIndices).getLocal(); - //the union of all resolved indices and aliases gets returned - String[] expectedIndices = new String[]{"bar", "foofoobar", "foobarfoo", "foofoo", "alias1"}; + // the union of all resolved indices and aliases gets returned + String[] expectedIndices = new String[] { "bar", "foofoobar", "foobarfoo", "foofoo", "alias1" }; assertThat(indices, hasSize(expectedIndices.length)); assertThat(indices, hasItems(expectedIndices)); - String[] replacedIndices = new String[]{"bar", "foofoobar", "foobarfoo", "foofoo"}; - //_all gets replaced with all indices that user is authorized for + String[] replacedIndices = new String[] { "bar", "foofoobar", "foobarfoo", "foofoo" }; + // _all gets replaced with all indices that user is authorized for assertThat(request.indices(), arrayContainingInAnyOrder(replacedIndices)); assertThat(request.aliases(), arrayContainingInAnyOrder("alias1")); } @@ -1141,8 +1235,7 @@ public void testAllGetAliasesRequestNoAuthorizedIndicesAllowNoIndices() { request.indicesOptions(IndicesOptions.fromOptions(randomBoolean(), true, true, randomBoolean())); request.aliases("alias1"); request.indices("_all"); - assertNoIndices(request, resolveIndices(request, - buildAuthorizedIndices(userNoIndices, GetAliasesAction.NAME))); + assertNoIndices(request, resolveIndices(request, buildAuthorizedIndices(userNoIndices, GetAliasesAction.NAME))); } public void testAllGetAliasesRequestNoAuthorizedIndicesDisallowNoIndices() { @@ -1150,8 +1243,10 @@ public void testAllGetAliasesRequestNoAuthorizedIndicesDisallowNoIndices() { request.indicesOptions(IndicesOptions.fromOptions(randomBoolean(), false, true, randomBoolean())); request.aliases("alias1"); request.indices("_all"); - IndexNotFoundException e = expectThrows(IndexNotFoundException.class, - () -> resolveIndices(request, buildAuthorizedIndices(userNoIndices, GetAliasesAction.NAME))); + IndexNotFoundException e = expectThrows( + IndexNotFoundException.class, + () -> resolveIndices(request, buildAuthorizedIndices(userNoIndices, GetAliasesAction.NAME)) + ); assertEquals("no such index [[_all]]", e.getMessage()); } @@ -1160,8 +1255,7 @@ public void testWildcardsGetAliasesRequestNoAuthorizedIndicesAllowNoIndices() { request.aliases("alias1"); request.indices("foo*"); request.indicesOptions(IndicesOptions.fromOptions(randomBoolean(), true, true, randomBoolean())); - assertNoIndices(request, resolveIndices(request, - buildAuthorizedIndices(userNoIndices, GetAliasesAction.NAME))); + assertNoIndices(request, resolveIndices(request, buildAuthorizedIndices(userNoIndices, GetAliasesAction.NAME))); } public void testWildcardsGetAliasesRequestNoAuthorizedIndicesDisallowNoIndices() { @@ -1169,9 +1263,11 @@ public void testWildcardsGetAliasesRequestNoAuthorizedIndicesDisallowNoIndices() request.indicesOptions(IndicesOptions.fromOptions(randomBoolean(), false, true, randomBoolean())); request.aliases("alias1"); request.indices("foo*"); - //current user is not authorized for any index, foo* resolves to no indices, the request fails - IndexNotFoundException e = expectThrows(IndexNotFoundException.class, - () -> resolveIndices(request, buildAuthorizedIndices(userNoIndices, GetAliasesAction.NAME))); + // current user is not authorized for any index, foo* resolves to no indices, the request fails + IndexNotFoundException e = expectThrows( + IndexNotFoundException.class, + () -> resolveIndices(request, buildAuthorizedIndices(userNoIndices, GetAliasesAction.NAME)) + ); assertEquals("no such index [foo*]", e.getMessage()); } @@ -1185,47 +1281,94 @@ public void testResolveAllAliasesGetAliasesRequest() { } final Set authorizedIndices = buildAuthorizedIndices(user, GetAliasesAction.NAME); List indices = resolveIndices(request, authorizedIndices).getLocal(); - //the union of all resolved indices and aliases gets returned, including hidden indices as Get Aliases includes hidden by default - String[] expectedIndices = new String[]{"bar", "bar-closed", "foofoobar", "foobarfoo", "foofoo", "foofoo-closed", "hidden-open", - "hidden-closed", ".hidden-open", ".hidden-closed", "date-hidden-" + todaySuffix, "date-hidden-" + tomorrowSuffix}; + // the union of all resolved indices and aliases gets returned, including hidden indices as Get Aliases includes hidden by default + String[] expectedIndices = new String[] { + "bar", + "bar-closed", + "foofoobar", + "foobarfoo", + "foofoo", + "foofoo-closed", + "hidden-open", + "hidden-closed", + ".hidden-open", + ".hidden-closed", + "date-hidden-" + todaySuffix, + "date-hidden-" + tomorrowSuffix }; assertSameValues(indices, expectedIndices); - //_all gets replaced with all indices that user is authorized for + // _all gets replaced with all indices that user is authorized for assertThat(request.indices(), arrayContainingInAnyOrder(expectedIndices)); assertThat(request.aliases(), arrayContainingInAnyOrder("foofoobar", "foobarfoo")); } public void testResolveAllAndExplicitAliasesGetAliasesRequest() { - GetAliasesRequest request = new GetAliasesRequest(new String[]{"_all", "explicit"}); + GetAliasesRequest request = new GetAliasesRequest(new String[] { "_all", "explicit" }); if (randomBoolean()) { request.indices("_all"); } final Set authorizedIndices = buildAuthorizedIndices(user, GetAliasesAction.NAME); List indices = resolveIndices(request, authorizedIndices).getLocal(); - //the union of all resolved indices and aliases gets returned, including hidden indices as Get Aliases includes hidden by default - String[] expectedIndices = new String[]{"bar", "bar-closed", "foofoobar", "foobarfoo", "foofoo", "foofoo-closed", "explicit", - "hidden-open", "hidden-closed", ".hidden-open", ".hidden-closed", "date-hidden-" + todaySuffix, - "date-hidden-" + tomorrowSuffix}; + // the union of all resolved indices and aliases gets returned, including hidden indices as Get Aliases includes hidden by default + String[] expectedIndices = new String[] { + "bar", + "bar-closed", + "foofoobar", + "foobarfoo", + "foofoo", + "foofoo-closed", + "explicit", + "hidden-open", + "hidden-closed", + ".hidden-open", + ".hidden-closed", + "date-hidden-" + todaySuffix, + "date-hidden-" + tomorrowSuffix }; logger.info("indices: {}", indices); assertSameValues(indices, expectedIndices); - //_all gets replaced with all indices that user is authorized for - assertThat(request.indices(), arrayContainingInAnyOrder("bar", "bar-closed", "foofoobar", "foobarfoo", "foofoo", "foofoo-closed", - "hidden-open", "hidden-closed", ".hidden-open", ".hidden-closed", "date-hidden-" + todaySuffix, - "date-hidden-" + tomorrowSuffix)); + // _all gets replaced with all indices that user is authorized for + assertThat( + request.indices(), + arrayContainingInAnyOrder( + "bar", + "bar-closed", + "foofoobar", + "foobarfoo", + "foofoo", + "foofoo-closed", + "hidden-open", + "hidden-closed", + ".hidden-open", + ".hidden-closed", + "date-hidden-" + todaySuffix, + "date-hidden-" + tomorrowSuffix + ) + ); assertThat(request.aliases(), arrayContainingInAnyOrder("foofoobar", "foobarfoo", "explicit")); } public void testResolveAllAndWildcardsAliasesGetAliasesRequest() { - GetAliasesRequest request = new GetAliasesRequest(new String[]{"_all", "foo*", "non_matching_*"}); + GetAliasesRequest request = new GetAliasesRequest(new String[] { "_all", "foo*", "non_matching_*" }); if (randomBoolean()) { request.indices("_all"); } final Set authorizedIndices = buildAuthorizedIndices(user, GetAliasesAction.NAME); List indices = resolveIndices(request, authorizedIndices).getLocal(); - //the union of all resolved indices and aliases gets returned, including hidden indices as Get Aliases includes hidden by default - String[] expectedIndices = new String[]{"bar", "bar-closed", "foofoobar", "foobarfoo", "foofoo", "foofoo-closed", "hidden-open", - "hidden-closed", ".hidden-open", ".hidden-closed", "date-hidden-" + todaySuffix, "date-hidden-" + tomorrowSuffix}; + // the union of all resolved indices and aliases gets returned, including hidden indices as Get Aliases includes hidden by default + String[] expectedIndices = new String[] { + "bar", + "bar-closed", + "foofoobar", + "foobarfoo", + "foofoo", + "foofoo-closed", + "hidden-open", + "hidden-closed", + ".hidden-open", + ".hidden-closed", + "date-hidden-" + todaySuffix, + "date-hidden-" + tomorrowSuffix }; assertSameValues(indices, expectedIndices); - //_all gets replaced with all indices that user is authorized for + // _all gets replaced with all indices that user is authorized for assertThat(request.indices(), arrayContainingInAnyOrder(expectedIndices)); assertThat(request.aliases(), arrayContainingInAnyOrder("foofoobar", "foofoobar", "foobarfoo", "foobarfoo")); } @@ -1236,19 +1379,19 @@ public void testResolveAliasesWildcardsGetAliasesRequest() { request.aliases("foo*"); final Set authorizedIndices = buildAuthorizedIndices(user, GetAliasesAction.NAME); List indices = resolveIndices(request, authorizedIndices).getLocal(); - //union of all resolved indices and aliases gets returned, based on what user is authorized for - //note that the index side will end up containing matching aliases too, which is fine, as es core would do - //the same and resolve those aliases to their corresponding concrete indices (which we let core do) - String[] expectedIndices = new String[]{"bar", "foobarfoo", "foofoobar"}; + // union of all resolved indices and aliases gets returned, based on what user is authorized for + // note that the index side will end up containing matching aliases too, which is fine, as es core would do + // the same and resolve those aliases to their corresponding concrete indices (which we let core do) + String[] expectedIndices = new String[] { "bar", "foobarfoo", "foofoobar" }; assertSameValues(indices, expectedIndices); - //alias foofoobar on both sides, that's fine, es core would do the same, same as above + // alias foofoobar on both sides, that's fine, es core would do the same, same as above assertThat(request.indices(), arrayContainingInAnyOrder("bar", "foofoobar")); assertThat(request.aliases(), arrayContainingInAnyOrder("foofoobar", "foobarfoo")); } public void testResolveAliasesWildcardsGetAliasesRequestNoAuthorizedIndices() { GetAliasesRequest request = new GetAliasesRequest(); - //no authorized aliases match bar*, hence aliases are replaced with the no-aliases-expression + // no authorized aliases match bar*, hence aliases are replaced with the no-aliases-expression request.aliases("bar*"); request.indices("*bar"); resolveIndices(request, buildAuthorizedIndices(user, GetAliasesAction.NAME)); @@ -1257,20 +1400,45 @@ public void testResolveAliasesWildcardsGetAliasesRequestNoAuthorizedIndices() { public void testResolveAliasesExclusionWildcardsGetAliasesRequest() { GetAliasesRequest request = new GetAliasesRequest(); - request.aliases("foo*","-foobar*"); + request.aliases("foo*", "-foobar*"); final Set authorizedIndices = buildAuthorizedIndices(user, GetAliasesAction.NAME); List indices = resolveIndices(request, authorizedIndices).getLocal(); - //union of all resolved indices and aliases gets returned, based on what user is authorized for - //note that the index side will end up containing matching aliases too, which is fine, as es core would do - //the same and resolve those aliases to their corresponding concrete indices (which we let core do) - //also includes hidden indices as Get Aliases includes hidden by default - String[] expectedIndices = new String[]{"bar", "bar-closed", "foobarfoo", "foofoo", "foofoo-closed", "foofoobar", "hidden-open", - "hidden-closed", ".hidden-open", ".hidden-closed", "date-hidden-" + todaySuffix, "date-hidden-" + tomorrowSuffix}; + // union of all resolved indices and aliases gets returned, based on what user is authorized for + // note that the index side will end up containing matching aliases too, which is fine, as es core would do + // the same and resolve those aliases to their corresponding concrete indices (which we let core do) + // also includes hidden indices as Get Aliases includes hidden by default + String[] expectedIndices = new String[] { + "bar", + "bar-closed", + "foobarfoo", + "foofoo", + "foofoo-closed", + "foofoobar", + "hidden-open", + "hidden-closed", + ".hidden-open", + ".hidden-closed", + "date-hidden-" + todaySuffix, + "date-hidden-" + tomorrowSuffix }; assertSameValues(indices, expectedIndices); - //alias foofoobar on both sides, that's fine, es core would do the same, same as above - assertThat(request.indices(), arrayContainingInAnyOrder("bar", "bar-closed", "foobarfoo", "foofoo", "foofoo-closed", "foofoobar", - "hidden-open", "hidden-closed", ".hidden-open", ".hidden-closed", "date-hidden-" + todaySuffix, - "date-hidden-" + tomorrowSuffix)); + // alias foofoobar on both sides, that's fine, es core would do the same, same as above + assertThat( + request.indices(), + arrayContainingInAnyOrder( + "bar", + "bar-closed", + "foobarfoo", + "foofoo", + "foofoo-closed", + "foofoobar", + "hidden-open", + "hidden-closed", + ".hidden-open", + ".hidden-closed", + "date-hidden-" + todaySuffix, + "date-hidden-" + tomorrowSuffix + ) + ); assertThat(request.aliases(), arrayContainingInAnyOrder("foofoobar")); } @@ -1280,7 +1448,7 @@ public void testResolveAliasesAllGetAliasesRequestNoAuthorizedIndices() { request.aliases("_all"); } request.indices("non_existing"); - //current user is not authorized for any index, aliases are replaced with the no-aliases-expression + // current user is not authorized for any index, aliases are replaced with the no-aliases-expression ResolvedIndices resolvedIndices = resolveIndices(request, buildAuthorizedIndices(userNoIndices, GetAliasesAction.NAME)); assertThat(resolvedIndices.getLocal(), contains("non_existing")); assertThat(Arrays.asList(request.indices()), contains("non_existing")); @@ -1294,11 +1462,15 @@ public void testResolveAliasesAllGetAliasesRequestNoAuthorizedIndices() { public void testRemotableRequestsAllowRemoteIndices() { IndicesOptions options = IndicesOptions.fromOptions(true, false, false, false); Tuple tuple = randomFrom( - new Tuple(new SearchRequest("remote:foo").indicesOptions(options), SearchAction.NAME), - new Tuple(new FieldCapabilitiesRequest().indices("remote:foo").indicesOptions(options), - FieldCapabilitiesAction.NAME), - new Tuple(new GraphExploreRequest().indices("remote:foo").indicesOptions(options), - GraphExploreAction.NAME) + new Tuple(new SearchRequest("remote:foo").indicesOptions(options), SearchAction.NAME), + new Tuple( + new FieldCapabilitiesRequest().indices("remote:foo").indicesOptions(options), + FieldCapabilitiesAction.NAME + ), + new Tuple( + new GraphExploreRequest().indices("remote:foo").indicesOptions(options), + GraphExploreAction.NAME + ) ); final TransportRequest request = tuple.v1(); ResolvedIndices resolved = resolveIndices(request, buildAuthorizedIndices(user, tuple.v2())); @@ -1313,31 +1485,36 @@ public void testRemotableRequestsAllowRemoteIndices() { public void testNonRemotableRequestDoesNotAllowRemoteIndices() { IndicesOptions options = IndicesOptions.fromOptions(true, false, false, false); Tuple tuple = randomFrom( - new Tuple(new CloseIndexRequest("remote:foo").indicesOptions(options), CloseIndexAction.NAME), - new Tuple(new DeleteIndexRequest("remote:foo").indicesOptions(options), DeleteIndexAction.NAME), - new Tuple(new PutMappingRequest("remote:foo").indicesOptions(options), PutMappingAction.NAME) + new Tuple(new CloseIndexRequest("remote:foo").indicesOptions(options), CloseIndexAction.NAME), + new Tuple(new DeleteIndexRequest("remote:foo").indicesOptions(options), DeleteIndexAction.NAME), + new Tuple(new PutMappingRequest("remote:foo").indicesOptions(options), PutMappingAction.NAME) + ); + IndexNotFoundException e = expectThrows( + IndexNotFoundException.class, + () -> resolveIndices(tuple.v1(), buildAuthorizedIndices(user, tuple.v2())).getLocal() ); - IndexNotFoundException e = expectThrows(IndexNotFoundException.class, - () -> resolveIndices(tuple.v1(), buildAuthorizedIndices(user, tuple.v2())).getLocal()); assertEquals("no such index [[remote:foo]]", e.getMessage()); } public void testNonRemotableRequestDoesNotAllowRemoteWildcardIndices() { IndicesOptions options = IndicesOptions.fromOptions(randomBoolean(), true, true, true); Tuple tuple = randomFrom( - new Tuple(new CloseIndexRequest("*:*").indicesOptions(options), CloseIndexAction.NAME), - new Tuple(new DeleteIndexRequest("*:*").indicesOptions(options), DeleteIndexAction.NAME), - new Tuple(new PutMappingRequest("*:*").indicesOptions(options), PutMappingAction.NAME) + new Tuple(new CloseIndexRequest("*:*").indicesOptions(options), CloseIndexAction.NAME), + new Tuple(new DeleteIndexRequest("*:*").indicesOptions(options), DeleteIndexAction.NAME), + new Tuple(new PutMappingRequest("*:*").indicesOptions(options), PutMappingAction.NAME) ); final ResolvedIndices resolved = resolveIndices(tuple.v1(), buildAuthorizedIndices(user, tuple.v2())); assertNoIndices((IndicesRequest.Replaceable) tuple.v1(), resolved); } public void testCompositeIndicesRequestIsNotSupported() { - TransportRequest request = randomFrom(new MultiSearchRequest(), new MultiGetRequest(), - new MultiTermVectorsRequest(), new BulkRequest()); - expectThrows(IllegalStateException.class, () -> resolveIndices(request, - buildAuthorizedIndices(user, MultiSearchAction.NAME))); + TransportRequest request = randomFrom( + new MultiSearchRequest(), + new MultiGetRequest(), + new MultiTermVectorsRequest(), + new BulkRequest() + ); + expectThrows(IllegalStateException.class, () -> resolveIndices(request, buildAuthorizedIndices(user, MultiSearchAction.NAME))); } public void testResolveAdminAction() { @@ -1345,7 +1522,7 @@ public void testResolveAdminAction() { { RefreshRequest request = new RefreshRequest("*"); List indices = resolveIndices(request, authorizedIndices).getLocal(); - String[] expectedIndices = new String[]{"bar", "foofoobar", "foobarfoo", "foofoo"}; + String[] expectedIndices = new String[] { "bar", "foofoobar", "foobarfoo", "foofoo" }; assertThat(indices, hasSize(expectedIndices.length)); assertThat(indices, hasItems(expectedIndices)); assertThat(request.indices(), arrayContainingInAnyOrder(expectedIndices)); @@ -1353,7 +1530,7 @@ public void testResolveAdminAction() { { DeleteIndexRequest request = new DeleteIndexRequest("*"); List indices = resolveIndices(request, authorizedIndices).getLocal(); - String[] expectedIndices = new String[]{"bar", "bar-closed", "foofoo", "foofoo-closed"}; + String[] expectedIndices = new String[] { "bar", "bar-closed", "foofoo", "foofoo-closed" }; assertThat(indices, hasSize(expectedIndices.length)); assertThat(indices, hasItems(expectedIndices)); assertThat(request.indices(), arrayContainingInAnyOrder(expectedIndices)); @@ -1385,8 +1562,15 @@ public void testXPackUserDoesNotHaveAccessToSecurityIndex() { public void testNonXPackUserAccessingSecurityIndex() { User allAccessUser = new User("all_access", "all_access"); - roleMap.put("all_access", new RoleDescriptor("all_access", new String[] { "all" }, - new IndicesPrivileges[] { IndicesPrivileges.builder().indices("*").privileges("all").build() }, null)); + roleMap.put( + "all_access", + new RoleDescriptor( + "all_access", + new String[] { "all" }, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices("*").privileges("all").build() }, + null + ) + ); { SearchRequest request = new SearchRequest(); @@ -1413,28 +1597,39 @@ public void testUnauthorizedDateMathExpressionIgnoreUnavailable() { public void testUnauthorizedDateMathExpressionIgnoreUnavailableDisallowNoIndices() { SearchRequest request = new SearchRequest(""); request.indicesOptions(IndicesOptions.fromOptions(true, false, randomBoolean(), randomBoolean())); - IndexNotFoundException e = expectThrows(IndexNotFoundException.class, - () -> resolveIndices(request, buildAuthorizedIndices(user, SearchAction.NAME))); - assertEquals("no such index [[]]" , e.getMessage()); + IndexNotFoundException e = expectThrows( + IndexNotFoundException.class, + () -> resolveIndices(request, buildAuthorizedIndices(user, SearchAction.NAME)) + ); + assertEquals("no such index [[]]", e.getMessage()); } public void testUnauthorizedDateMathExpressionStrict() { - String expectedIndex = "datetime-" + DateTimeFormatter.ofPattern("uuuu.MM.dd", Locale.ROOT).format( - ZonedDateTime.now(ZoneOffset.UTC).withDayOfMonth(1)); + String expectedIndex = "datetime-" + + DateTimeFormatter.ofPattern("uuuu.MM.dd", Locale.ROOT).format(ZonedDateTime.now(ZoneOffset.UTC).withDayOfMonth(1)); SearchRequest request = new SearchRequest(""); request.indicesOptions(IndicesOptions.fromOptions(false, randomBoolean(), randomBoolean(), randomBoolean())); - IndexNotFoundException e = expectThrows(IndexNotFoundException.class, - () -> resolveIndices(request, buildAuthorizedIndices(user, SearchAction.NAME))); - assertEquals("no such index [" + expectedIndex + "]" , e.getMessage()); + IndexNotFoundException e = expectThrows( + IndexNotFoundException.class, + () -> resolveIndices(request, buildAuthorizedIndices(user, SearchAction.NAME)) + ); + assertEquals("no such index [" + expectedIndex + "]", e.getMessage()); } public void testResolveDateMathExpression() { // make the user authorized final String pattern = randomBoolean() ? "" : ""; String dateTimeIndex = indexNameExpressionResolver.resolveDateMathExpression(""); - String[] authorizedIndices = new String[] { "bar", "bar-closed", "foofoobar", "foofoo", "missing", "foofoo-closed", dateTimeIndex}; - roleMap.put("role", new RoleDescriptor("role", null, - new IndicesPrivileges[] { IndicesPrivileges.builder().indices(authorizedIndices).privileges("all").build() }, null)); + String[] authorizedIndices = new String[] { "bar", "bar-closed", "foofoobar", "foofoo", "missing", "foofoo-closed", dateTimeIndex }; + roleMap.put( + "role", + new RoleDescriptor( + "role", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices(authorizedIndices).privileges("all").build() }, + null + ) + ); SearchRequest request = new SearchRequest(pattern); if (randomBoolean()) { @@ -1455,32 +1650,48 @@ public void testMissingDateMathExpressionIgnoreUnavailable() { public void testMissingDateMathExpressionIgnoreUnavailableDisallowNoIndices() { SearchRequest request = new SearchRequest(""); request.indicesOptions(IndicesOptions.fromOptions(true, false, randomBoolean(), randomBoolean())); - IndexNotFoundException e = expectThrows(IndexNotFoundException.class, - () -> resolveIndices(request, buildAuthorizedIndices(user, SearchAction.NAME))); - assertEquals("no such index [[]]" , e.getMessage()); + IndexNotFoundException e = expectThrows( + IndexNotFoundException.class, + () -> resolveIndices(request, buildAuthorizedIndices(user, SearchAction.NAME)) + ); + assertEquals("no such index [[]]", e.getMessage()); } public void testMissingDateMathExpressionStrict() { - String expectedIndex = "foobar-" + DateTimeFormatter.ofPattern("uuuu.MM.dd", Locale.ROOT).format( - ZonedDateTime.now(ZoneOffset.UTC).withDayOfMonth(1)); + String expectedIndex = "foobar-" + + DateTimeFormatter.ofPattern("uuuu.MM.dd", Locale.ROOT).format(ZonedDateTime.now(ZoneOffset.UTC).withDayOfMonth(1)); SearchRequest request = new SearchRequest(""); request.indicesOptions(IndicesOptions.fromOptions(false, randomBoolean(), randomBoolean(), randomBoolean())); - IndexNotFoundException e = expectThrows(IndexNotFoundException.class, - () -> resolveIndices(request, buildAuthorizedIndices(user, SearchAction.NAME))); - assertEquals("no such index [" + expectedIndex + "]" , e.getMessage()); + IndexNotFoundException e = expectThrows( + IndexNotFoundException.class, + () -> resolveIndices(request, buildAuthorizedIndices(user, SearchAction.NAME)) + ); + assertEquals("no such index [" + expectedIndex + "]", e.getMessage()); } public void testAliasDateMathExpressionNotSupported() { // make the user authorized - String[] authorizedIndices = new String[] { "bar", "bar-closed", "foofoobar", "foofoo", "missing", "foofoo-closed", - indexNameExpressionResolver.resolveDateMathExpression("")}; - roleMap.put("role", new RoleDescriptor("role", null, - new IndicesPrivileges[] { IndicesPrivileges.builder().indices(authorizedIndices).privileges("all").build() }, null)); + String[] authorizedIndices = new String[] { + "bar", + "bar-closed", + "foofoobar", + "foofoo", + "missing", + "foofoo-closed", + indexNameExpressionResolver.resolveDateMathExpression("") }; + roleMap.put( + "role", + new RoleDescriptor( + "role", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices(authorizedIndices).privileges("all").build() }, + null + ) + ); GetAliasesRequest request = new GetAliasesRequest("").indices("foo", "foofoo"); - List indices = - resolveIndices(request, buildAuthorizedIndices(user, GetAliasesAction.NAME)).getLocal(); - //the union of all indices and aliases gets returned - String[] expectedIndices = new String[]{"", "foo", "foofoo"}; + List indices = resolveIndices(request, buildAuthorizedIndices(user, GetAliasesAction.NAME)).getLocal(); + // the union of all indices and aliases gets returned + String[] expectedIndices = new String[] { "", "foo", "foofoo" }; assertThat(indices, hasSize(expectedIndices.length)); assertThat(indices, hasItems(expectedIndices)); assertThat(request.indices(), arrayContainingInAnyOrder("foo", "foofoo")); @@ -1510,7 +1721,7 @@ public void testWhenAliasToMultipleIndicesAndUserIsAuthorizedUsingAliasReturnsAl assert metadata.getIndicesLookup().get("logs-alias").getIndices().size() == 3; String putMappingIndexOrAlias = IndicesAndAliasesResolver.getPutMappingIndexOrAlias(request, authorizedIndices, metadata); String message = "user is authorized to access `logs-alias` and the put mapping request is for a write index" - + "so this should have returned the alias name"; + + "so this should have returned the alias name"; assertEquals(message, "logs-alias", putMappingIndexOrAlias); } @@ -1521,7 +1732,7 @@ public void testWhenAliasToMultipleIndicesAndUserIsAuthorizedUsingAliasReturnsIn assert metadata.getIndicesLookup().get("logs-alias").getIndices().size() == 3; String putMappingIndexOrAlias = IndicesAndAliasesResolver.getPutMappingIndexOrAlias(request, authorizedIndices, metadata); String message = "user is authorized to access `logs-alias` and the put mapping request is for a read index" - + "so this should have returned the concrete index as fallback"; + + "so this should have returned the concrete index as fallback"; assertEquals(message, index, putMappingIndexOrAlias); } @@ -1529,20 +1740,48 @@ public void testHiddenIndicesResolution() { SearchRequest searchRequest = new SearchRequest(); searchRequest.indicesOptions(IndicesOptions.fromOptions(false, false, true, true, true)); Set authorizedIndices = buildAuthorizedIndices(user, SearchAction.NAME); - ResolvedIndices resolvedIndices - = defaultIndicesResolver.resolveIndicesAndAliases(SearchAction.NAME, searchRequest, metadata, authorizedIndices); - assertThat(resolvedIndices.getLocal(), containsInAnyOrder("bar", "bar-closed", "foofoobar", "foobarfoo", "foofoo", "foofoo-closed", - "hidden-open", "hidden-closed", ".hidden-open", ".hidden-closed", "date-hidden-" + todaySuffix, - "date-hidden-" + tomorrowSuffix)); + ResolvedIndices resolvedIndices = defaultIndicesResolver.resolveIndicesAndAliases( + SearchAction.NAME, + searchRequest, + metadata, + authorizedIndices + ); + assertThat( + resolvedIndices.getLocal(), + containsInAnyOrder( + "bar", + "bar-closed", + "foofoobar", + "foobarfoo", + "foofoo", + "foofoo-closed", + "hidden-open", + "hidden-closed", + ".hidden-open", + ".hidden-closed", + "date-hidden-" + todaySuffix, + "date-hidden-" + tomorrowSuffix + ) + ); assertThat(resolvedIndices.getRemote(), emptyIterable()); // open + hidden searchRequest = new SearchRequest(); searchRequest.indicesOptions(IndicesOptions.fromOptions(false, false, true, false, true)); resolvedIndices = defaultIndicesResolver.resolveIndicesAndAliases(SearchAction.NAME, searchRequest, metadata, authorizedIndices); - assertThat(resolvedIndices.getLocal(), - containsInAnyOrder("bar", "foofoobar", "foobarfoo", "foofoo", "hidden-open", ".hidden-open", "date-hidden-" + todaySuffix, - "date-hidden-" + tomorrowSuffix)); + assertThat( + resolvedIndices.getLocal(), + containsInAnyOrder( + "bar", + "foofoobar", + "foobarfoo", + "foofoo", + "hidden-open", + ".hidden-open", + "date-hidden-" + todaySuffix, + "date-hidden-" + tomorrowSuffix + ) + ); assertThat(resolvedIndices.getRemote(), emptyIterable()); // open + implicit hidden for . indices @@ -1592,8 +1831,12 @@ public void testHiddenAliasesResolution() { // Visible only SearchRequest searchRequest = new SearchRequest(); searchRequest.indicesOptions(IndicesOptions.fromOptions(false, false, true, false, false)); - ResolvedIndices resolvedIndices - = defaultIndicesResolver.resolveIndicesAndAliases(SearchAction.NAME, searchRequest, metadata, authorizedIndices); + ResolvedIndices resolvedIndices = defaultIndicesResolver.resolveIndicesAndAliases( + SearchAction.NAME, + searchRequest, + metadata, + authorizedIndices + ); assertThat(resolvedIndices.getLocal(), containsInAnyOrder("alias-visible", "alias-visible-mixed")); assertThat(resolvedIndices.getRemote(), emptyIterable()); @@ -1601,8 +1844,10 @@ public void testHiddenAliasesResolution() { searchRequest = new SearchRequest(); searchRequest.indicesOptions(IndicesOptions.fromOptions(false, false, true, false, true)); resolvedIndices = defaultIndicesResolver.resolveIndicesAndAliases(SearchAction.NAME, searchRequest, metadata, authorizedIndices); - assertThat(resolvedIndices.getLocal(), - containsInAnyOrder("alias-visible", "alias-visible-mixed", "alias-hidden", ".alias-hidden", "hidden-open")); + assertThat( + resolvedIndices.getLocal(), + containsInAnyOrder("alias-visible", "alias-visible-mixed", "alias-hidden", ".alias-hidden", "hidden-open") + ); assertThat(resolvedIndices.getRemote(), emptyIterable()); // Include hidden with a wildcard @@ -1710,7 +1955,8 @@ public void testDataStreamsAreNotVisibleWhenNotIncludedByRequestWithWildcard() { GetAliasesAction.NAME, request, metadata, - authorizedIndices); + authorizedIndices + ); for (String dsName : dataStreams) { assertThat(resolvedIndices.getLocal(), hasItem(dsName)); DataStream dataStream = metadata.dataStreams().get(dsName); @@ -2004,17 +2250,22 @@ private Set buildAuthorizedIndices(User user, String action) { private Set buildAuthorizedIndices(User user, String action, TransportRequest request) { PlainActionFuture rolesListener = new PlainActionFuture<>(); - final Authentication authentication = - new Authentication(user, new RealmRef("test", "indices-aliases-resolver-tests", "node"), null); + final Authentication authentication = new Authentication( + user, + new RealmRef("test", "indices-aliases-resolver-tests", "node"), + null + ); rolesStore.getRoles(user, authentication, rolesListener); - return RBACEngine.resolveAuthorizedIndicesFromRole(rolesListener.actionGet(), getRequestInfo(request, action), - metadata.getIndicesLookup()); + return RBACEngine.resolveAuthorizedIndicesFromRole( + rolesListener.actionGet(), + getRequestInfo(request, action), + metadata.getIndicesLookup() + ); } public static IndexMetadata.Builder indexBuilder(String index) { - return IndexMetadata.builder(index).settings(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)); + return IndexMetadata.builder(index) + .settings(Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0)); } private ResolvedIndices resolveIndices(TransportRequest request, Set authorizedIndices) { @@ -2034,7 +2285,7 @@ private static void assertNoIndices(IndicesRequest.Replaceable request, Resolved } private void assertSameValues(List indices, String[] expectedIndices) { - assertThat(indices.stream().distinct().count(), equalTo((long)expectedIndices.length)); + assertThat(indices.stream().distinct().count(), equalTo((long) expectedIndices.length)); assertThat(indices, hasItems(expectedIndices)); } } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/RBACEngineTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/RBACEngineTests.java index 0c94d6f861e99..b764d3436133b 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/RBACEngineTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/RBACEngineTests.java @@ -27,10 +27,10 @@ import org.elasticsearch.common.collect.MapBuilder; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.set.Sets; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.license.GetLicenseAction; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.transport.TransportRequest; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.XPackPlugin; import org.elasticsearch.xpack.core.security.action.GetApiKeyAction; import org.elasticsearch.xpack.core.security.action.GetApiKeyRequest; @@ -85,8 +85,8 @@ import static java.util.Collections.emptyMap; import static org.elasticsearch.common.util.set.Sets.newHashSet; -import static org.elasticsearch.xpack.security.authz.AuthorizedIndicesTests.getRequestInfo; import static org.elasticsearch.xpack.core.security.test.TestRestrictedIndices.RESTRICTED_INDICES_AUTOMATON; +import static org.elasticsearch.xpack.security.authz.AuthorizedIndicesTests.getRequestInfo; import static org.hamcrest.Matchers.aMapWithSize; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.emptyIterable; @@ -116,38 +116,39 @@ public void createEngine() { public void testSameUserPermission() { final User user = new User("joe"); final boolean changePasswordRequest = randomBoolean(); - final TransportRequest request = changePasswordRequest ? - new ChangePasswordRequestBuilder(mock(Client.class)).username(user.principal()).request() : - new AuthenticateRequestBuilder(mock(Client.class)).username(user.principal()).request(); + final TransportRequest request = changePasswordRequest + ? new ChangePasswordRequestBuilder(mock(Client.class)).username(user.principal()).request() + : new AuthenticateRequestBuilder(mock(Client.class)).username(user.principal()).request(); final String action = changePasswordRequest ? ChangePasswordAction.NAME : AuthenticateAction.NAME; final Authentication authentication = mock(Authentication.class); final Authentication.RealmRef authenticatedBy = mock(Authentication.RealmRef.class); when(authentication.getAuthenticationType()).thenReturn(Authentication.AuthenticationType.REALM); when(authentication.getUser()).thenReturn(user); when(authentication.getAuthenticatedBy()).thenReturn(authenticatedBy); - when(authenticatedBy.getType()) - .thenReturn(changePasswordRequest ? randomFrom(ReservedRealm.TYPE, NativeRealmSettings.TYPE) : - randomAlphaOfLengthBetween(4, 12)); + when(authenticatedBy.getType()).thenReturn( + changePasswordRequest ? randomFrom(ReservedRealm.TYPE, NativeRealmSettings.TYPE) : randomAlphaOfLengthBetween(4, 12) + ); assertThat(request, instanceOf(UserRequest.class)); assertTrue(engine.checkSameUserPermissions(action, request, authentication)); } public void testSameUserPermissionDoesNotAllowNonMatchingUsername() { - final User authUser = new User("admin", new String[]{"bar"}); + final User authUser = new User("admin", new String[] { "bar" }); final User user = new User("joe", null, authUser); final boolean changePasswordRequest = randomBoolean(); final String username = randomFrom("", "joe" + randomAlphaOfLengthBetween(1, 5), randomAlphaOfLengthBetween(3, 10)); - final TransportRequest request = changePasswordRequest ? - new ChangePasswordRequestBuilder(mock(Client.class)).username(username).request() : - new AuthenticateRequestBuilder(mock(Client.class)).username(username).request(); + final TransportRequest request = changePasswordRequest + ? new ChangePasswordRequestBuilder(mock(Client.class)).username(username).request() + : new AuthenticateRequestBuilder(mock(Client.class)).username(username).request(); final String action = changePasswordRequest ? ChangePasswordAction.NAME : AuthenticateAction.NAME; final Authentication authentication = mock(Authentication.class); final Authentication.RealmRef authenticatedBy = mock(Authentication.RealmRef.class); when(authentication.getUser()).thenReturn(user); when(authentication.getAuthenticatedBy()).thenReturn(authenticatedBy); - final String authenticationType = changePasswordRequest ? randomFrom(ReservedRealm.TYPE, NativeRealmSettings.TYPE) : - randomAlphaOfLengthBetween(4, 12); + final String authenticationType = changePasswordRequest + ? randomFrom(ReservedRealm.TYPE, NativeRealmSettings.TYPE) + : randomAlphaOfLengthBetween(4, 12); when(authenticatedBy.getType()).thenReturn(authenticationType); when(authentication.getAuthenticationType()).thenReturn(Authentication.AuthenticationType.REALM); @@ -157,9 +158,9 @@ public void testSameUserPermissionDoesNotAllowNonMatchingUsername() { when(authentication.getUser()).thenReturn(user); final Authentication.RealmRef lookedUpBy = mock(Authentication.RealmRef.class); when(authentication.getLookedUpBy()).thenReturn(lookedUpBy); - when(lookedUpBy.getType()) - .thenReturn(changePasswordRequest ? randomFrom(ReservedRealm.TYPE, NativeRealmSettings.TYPE) : - randomAlphaOfLengthBetween(4, 12)); + when(lookedUpBy.getType()).thenReturn( + changePasswordRequest ? randomFrom(ReservedRealm.TYPE, NativeRealmSettings.TYPE) : randomAlphaOfLengthBetween(4, 12) + ); // this should still fail since the username is still different assertFalse(engine.checkSameUserPermissions(action, request, authentication)); @@ -174,8 +175,14 @@ public void testSameUserPermissionDoesNotAllowNonMatchingUsername() { public void testSameUserPermissionDoesNotAllowOtherActions() { final User user = mock(User.class); final TransportRequest request = mock(TransportRequest.class); - final String action = randomFrom(PutUserAction.NAME, DeleteUserAction.NAME, ClusterHealthAction.NAME, ClusterStateAction.NAME, - ClusterStatsAction.NAME, GetLicenseAction.NAME); + final String action = randomFrom( + PutUserAction.NAME, + DeleteUserAction.NAME, + ClusterHealthAction.NAME, + ClusterStateAction.NAME, + ClusterStatsAction.NAME, + GetLicenseAction.NAME + ); final Authentication authentication = mock(Authentication.class); final Authentication.RealmRef authenticatedBy = mock(Authentication.RealmRef.class); final boolean runAs = randomBoolean(); @@ -183,21 +190,20 @@ public void testSameUserPermissionDoesNotAllowOtherActions() { when(user.authenticatedUser()).thenReturn(runAs ? new User("authUser") : user); when(user.isRunAs()).thenReturn(runAs); when(authentication.getAuthenticatedBy()).thenReturn(authenticatedBy); - when(authenticatedBy.getType()) - .thenReturn(randomAlphaOfLengthBetween(4, 12)); + when(authenticatedBy.getType()).thenReturn(randomAlphaOfLengthBetween(4, 12)); assertFalse(engine.checkSameUserPermissions(action, request, authentication)); verifyZeroInteractions(user, request, authentication); } public void testSameUserPermissionRunAsChecksAuthenticatedBy() { - final User authUser = new User("admin", new String[]{"bar"}); + final User authUser = new User("admin", new String[] { "bar" }); final String username = "joe"; final User user = new User(username, null, authUser); final boolean changePasswordRequest = randomBoolean(); - final TransportRequest request = changePasswordRequest ? - new ChangePasswordRequestBuilder(mock(Client.class)).username(username).request() : - new AuthenticateRequestBuilder(mock(Client.class)).username(username).request(); + final TransportRequest request = changePasswordRequest + ? new ChangePasswordRequestBuilder(mock(Client.class)).username(username).request() + : new AuthenticateRequestBuilder(mock(Client.class)).username(username).request(); final String action = changePasswordRequest ? ChangePasswordAction.NAME : AuthenticateAction.NAME; final Authentication authentication = mock(Authentication.class); final Authentication.RealmRef authenticatedBy = mock(Authentication.RealmRef.class); @@ -206,9 +212,9 @@ public void testSameUserPermissionRunAsChecksAuthenticatedBy() { when(authentication.getUser()).thenReturn(user); when(authentication.getAuthenticatedBy()).thenReturn(authenticatedBy); when(authentication.getLookedUpBy()).thenReturn(lookedUpBy); - when(lookedUpBy.getType()) - .thenReturn(changePasswordRequest ? randomFrom(ReservedRealm.TYPE, NativeRealmSettings.TYPE) : - randomAlphaOfLengthBetween(4, 12)); + when(lookedUpBy.getType()).thenReturn( + changePasswordRequest ? randomFrom(ReservedRealm.TYPE, NativeRealmSettings.TYPE) : randomAlphaOfLengthBetween(4, 12) + ); assertTrue(engine.checkSameUserPermissions(action, request, authentication)); when(authentication.getUser()).thenReturn(authUser); @@ -224,9 +230,15 @@ public void testSameUserPermissionDoesNotAllowChangePasswordForOtherRealms() { when(authentication.getAuthenticationType()).thenReturn(Authentication.AuthenticationType.REALM); when(authentication.getUser()).thenReturn(user); when(authentication.getAuthenticatedBy()).thenReturn(authenticatedBy); - when(authenticatedBy.getType()).thenReturn(randomFrom(LdapRealmSettings.LDAP_TYPE, FileRealmSettings.TYPE, - LdapRealmSettings.AD_TYPE, PkiRealmSettings.TYPE, - randomAlphaOfLengthBetween(4, 12))); + when(authenticatedBy.getType()).thenReturn( + randomFrom( + LdapRealmSettings.LDAP_TYPE, + FileRealmSettings.TYPE, + LdapRealmSettings.AD_TYPE, + PkiRealmSettings.TYPE, + randomAlphaOfLengthBetween(4, 12) + ) + ); assertThat(request, instanceOf(UserRequest.class)); assertFalse(engine.checkSameUserPermissions(action, request, authentication)); @@ -278,7 +290,7 @@ public void testSameUserPermissionDoesNotAllowChangePasswordForAccessToken() { } public void testSameUserPermissionDoesNotAllowChangePasswordForLookedUpByOtherRealms() { - final User authUser = new User("admin", new String[]{"bar"}); + final User authUser = new User("admin", new String[] { "bar" }); final User user = new User("joe", null, authUser); final ChangePasswordRequest request = new ChangePasswordRequestBuilder(mock(Client.class)).username(user.principal()).request(); final String action = ChangePasswordAction.NAME; @@ -289,9 +301,15 @@ public void testSameUserPermissionDoesNotAllowChangePasswordForLookedUpByOtherRe when(authentication.getUser()).thenReturn(user); when(authentication.getAuthenticatedBy()).thenReturn(authenticatedBy); when(authentication.getLookedUpBy()).thenReturn(lookedUpBy); - when(lookedUpBy.getType()).thenReturn(randomFrom(LdapRealmSettings.LDAP_TYPE, FileRealmSettings.TYPE, - LdapRealmSettings.AD_TYPE, PkiRealmSettings.TYPE, - randomAlphaOfLengthBetween(4, 12))); + when(lookedUpBy.getType()).thenReturn( + randomFrom( + LdapRealmSettings.LDAP_TYPE, + FileRealmSettings.TYPE, + LdapRealmSettings.AD_TYPE, + PkiRealmSettings.TYPE, + randomAlphaOfLengthBetween(4, 12) + ) + ); assertThat(request, instanceOf(UserRequest.class)); assertFalse(engine.checkSameUserPermissions(action, request, authentication)); @@ -343,8 +361,10 @@ public void testSameUserPermissionDeniesApiKeyInfoRetrievalWhenLookedupByIsPrese when(authentication.getAuthenticationType()).thenReturn(AuthenticationType.API_KEY); when(authentication.getMetadata()).thenReturn(Map.of(ApiKeyService.API_KEY_ID_KEY, randomAlphaOfLengthBetween(4, 7))); - final AssertionError assertionError = expectThrows(AssertionError.class, () -> engine.checkSameUserPermissions(GetApiKeyAction.NAME, - request, authentication)); + final AssertionError assertionError = expectThrows( + AssertionError.class, + () -> engine.checkSameUserPermissions(GetApiKeyAction.NAME, request, authentication) + ); assertNotNull(assertionError); assertThat(assertionError.getLocalizedMessage(), is("runAs not supported for api key authentication")); } @@ -366,10 +386,9 @@ public void testNamedIndexPrivilegesMatchApplicableActions() throws Exception { final HasPrivilegesRequest request = new HasPrivilegesRequest(); request.username(user.principal()); request.clusterPrivileges(ClusterHealthAction.NAME); - request.indexPrivileges(RoleDescriptor.IndicesPrivileges.builder() - .indices("academy") - .privileges(DeleteAction.NAME, IndexAction.NAME) - .build()); + request.indexPrivileges( + RoleDescriptor.IndicesPrivileges.builder().indices("academy").privileges(DeleteAction.NAME, IndexAction.NAME).build() + ); request.applicationPrivileges(new RoleDescriptor.ApplicationResourcePrivileges[0]); final PlainActionFuture future = new PlainActionFuture<>(); @@ -409,10 +428,12 @@ public void testMatchSubsetOfPrivileges() throws Exception { final HasPrivilegesRequest request = new HasPrivilegesRequest(); request.username(user.principal()); request.clusterPrivileges("monitor", "manage"); - request.indexPrivileges(RoleDescriptor.IndicesPrivileges.builder() - .indices("academy", "initiative", "school") - .privileges("delete", "index", "manage") - .build()); + request.indexPrivileges( + RoleDescriptor.IndicesPrivileges.builder() + .indices("academy", "initiative", "school") + .privileges("delete", "index", "manage") + .build() + ); request.applicationPrivileges(new RoleDescriptor.ApplicationResourcePrivileges[0]); final PlainActionFuture future = new PlainActionFuture<>(); engine.checkPrivileges(authentication, authzInfo, request, Collections.emptyList(), future); @@ -458,16 +479,16 @@ public void testMatchNothing() throws Exception { User user = new User(randomAlphaOfLengthBetween(4, 12)); Authentication authentication = mock(Authentication.class); when(authentication.getUser()).thenReturn(user); - Role role = Role.builder(RESTRICTED_INDICES_AUTOMATON, "test3") - .cluster(Set.of("monitor"), Set.of()) - .build(); + Role role = Role.builder(RESTRICTED_INDICES_AUTOMATON, "test3").cluster(Set.of("monitor"), Set.of()).build(); RBACAuthorizationInfo authzInfo = new RBACAuthorizationInfo(role, null); - final HasPrivilegesResponse response = hasPrivileges(RoleDescriptor.IndicesPrivileges.builder() - .indices("academy") - .privileges("read", "write") - .build(), - authentication, authzInfo, Collections.emptyList(), Strings.EMPTY_ARRAY); + final HasPrivilegesResponse response = hasPrivileges( + RoleDescriptor.IndicesPrivileges.builder().indices("academy").privileges("read", "write").build(), + authentication, + authzInfo, + Collections.emptyList(), + Strings.EMPTY_ARRAY + ); assertThat(response.getUsername(), is(user.principal())); assertThat(response.isCompleteMatch(), is(false)); assertThat(response.getIndexPrivileges(), Matchers.iterableWithSize(1)); @@ -486,14 +507,30 @@ public void testMatchNothing() throws Exception { */ public void testWildcardHandling() throws Exception { List privs = new ArrayList<>(); - final ApplicationPrivilege kibanaRead = defineApplicationPrivilege(privs, "kibana", "read", - "data:read/*", "action:login", "action:view/dashboard"); - final ApplicationPrivilege kibanaWrite = defineApplicationPrivilege(privs, "kibana", "write", - "data:write/*", "action:login", "action:view/dashboard"); - final ApplicationPrivilege kibanaAdmin = defineApplicationPrivilege(privs, "kibana", "admin", - "action:login", "action:manage/*"); - final ApplicationPrivilege kibanaViewSpace = defineApplicationPrivilege(privs, "kibana", "view-space", - "action:login", "space:view/*"); + final ApplicationPrivilege kibanaRead = defineApplicationPrivilege( + privs, + "kibana", + "read", + "data:read/*", + "action:login", + "action:view/dashboard" + ); + final ApplicationPrivilege kibanaWrite = defineApplicationPrivilege( + privs, + "kibana", + "write", + "data:write/*", + "action:login", + "action:view/dashboard" + ); + final ApplicationPrivilege kibanaAdmin = defineApplicationPrivilege(privs, "kibana", "admin", "action:login", "action:manage/*"); + final ApplicationPrivilege kibanaViewSpace = defineApplicationPrivilege( + privs, + "kibana", + "view-space", + "action:login", + "space:view/*" + ); User user = new User(randomAlphaOfLengthBetween(4, 12)); Authentication authentication = mock(Authentication.class); when(authentication.getUser()).thenReturn(user); @@ -561,27 +598,37 @@ public void testWildcardHandling() throws Exception { assertThat(response.getUsername(), is(user.principal())); assertThat(response.isCompleteMatch(), is(false)); assertThat(response.getIndexPrivileges(), Matchers.iterableWithSize(8)); - assertThat(response.getIndexPrivileges(), containsInAnyOrder( - ResourcePrivileges.builder("logstash-2016-*").addPrivileges(Collections.singletonMap("write", true)).build(), - ResourcePrivileges.builder("logstash-*").addPrivileges(Collections.singletonMap("read", true)).build(), - ResourcePrivileges.builder("log*").addPrivileges(Collections.singletonMap("manage", false)).build(), - ResourcePrivileges.builder("foo?").addPrivileges(Collections.singletonMap("read", true)).build(), - ResourcePrivileges.builder("foo*").addPrivileges(Collections.singletonMap("read", false)).build(), - ResourcePrivileges.builder("abcd*").addPrivileges(mapBuilder().put("read", true).put("write", false).map()).build(), - ResourcePrivileges.builder("abc*xyz") - .addPrivileges(mapBuilder().put("read", true).put("write", true).put("manage", false).map()).build(), - ResourcePrivileges.builder("a*xyz") - .addPrivileges(mapBuilder().put("read", false).put("write", true).put("manage", false).map()).build() - )); + assertThat( + response.getIndexPrivileges(), + containsInAnyOrder( + ResourcePrivileges.builder("logstash-2016-*").addPrivileges(Collections.singletonMap("write", true)).build(), + ResourcePrivileges.builder("logstash-*").addPrivileges(Collections.singletonMap("read", true)).build(), + ResourcePrivileges.builder("log*").addPrivileges(Collections.singletonMap("manage", false)).build(), + ResourcePrivileges.builder("foo?").addPrivileges(Collections.singletonMap("read", true)).build(), + ResourcePrivileges.builder("foo*").addPrivileges(Collections.singletonMap("read", false)).build(), + ResourcePrivileges.builder("abcd*").addPrivileges(mapBuilder().put("read", true).put("write", false).map()).build(), + ResourcePrivileges.builder("abc*xyz") + .addPrivileges(mapBuilder().put("read", true).put("write", true).put("manage", false).map()) + .build(), + ResourcePrivileges.builder("a*xyz") + .addPrivileges(mapBuilder().put("read", false).put("write", true).put("manage", false).map()) + .build() + ) + ); assertThat(response.getApplicationPrivileges().entrySet(), Matchers.iterableWithSize(1)); final Set kibanaPrivileges = response.getApplicationPrivileges().get("kibana"); assertThat(kibanaPrivileges, Matchers.iterableWithSize(3)); - assertThat(Strings.collectionToCommaDelimitedString(kibanaPrivileges), kibanaPrivileges, containsInAnyOrder( - ResourcePrivileges.builder("*").addPrivileges(mapBuilder().put("read", true).put("write", false).map()).build(), - ResourcePrivileges.builder("space/engineering/project-*") - .addPrivileges(Collections.singletonMap("space:view/dashboard", true)).build(), - ResourcePrivileges.builder("space/*").addPrivileges(Collections.singletonMap("space:view/dashboard", false)).build() - )); + assertThat( + Strings.collectionToCommaDelimitedString(kibanaPrivileges), + kibanaPrivileges, + containsInAnyOrder( + ResourcePrivileges.builder("*").addPrivileges(mapBuilder().put("read", true).put("write", false).map()).build(), + ResourcePrivileges.builder("space/engineering/project-*") + .addPrivileges(Collections.singletonMap("space:view/dashboard", true)) + .build(), + ResourcePrivileges.builder("space/*").addPrivileges(Collections.singletonMap("space:view/dashboard", false)).build() + ) + ); } public void testCheckingIndexPermissionsDefinedOnDifferentPatterns() throws Exception { @@ -594,131 +641,223 @@ public void testCheckingIndexPermissionsDefinedOnDifferentPatterns() throws Exce .build(); RBACAuthorizationInfo authzInfo = new RBACAuthorizationInfo(role, null); - final HasPrivilegesResponse response = hasPrivileges(RoleDescriptor.IndicesPrivileges.builder() - .indices("apache-2016-12", "apache-2017-01") - .privileges("index", "delete") - .build(), authentication, authzInfo, Collections.emptyList(), Strings.EMPTY_ARRAY); + final HasPrivilegesResponse response = hasPrivileges( + RoleDescriptor.IndicesPrivileges.builder().indices("apache-2016-12", "apache-2017-01").privileges("index", "delete").build(), + authentication, + authzInfo, + Collections.emptyList(), + Strings.EMPTY_ARRAY + ); assertThat(response.isCompleteMatch(), is(false)); assertThat(response.getIndexPrivileges(), Matchers.iterableWithSize(2)); - assertThat(response.getIndexPrivileges(), containsInAnyOrder( - ResourcePrivileges.builder("apache-2016-12") - .addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap()) - .put("index", true).put("delete", true).map()).build(), - ResourcePrivileges.builder("apache-2017-01") - .addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap()) - .put("index", true).put("delete", false).map()).build() - )); + assertThat( + response.getIndexPrivileges(), + containsInAnyOrder( + ResourcePrivileges.builder("apache-2016-12") + .addPrivileges( + MapBuilder.newMapBuilder(new LinkedHashMap()).put("index", true).put("delete", true).map() + ) + .build(), + ResourcePrivileges.builder("apache-2017-01") + .addPrivileges( + MapBuilder.newMapBuilder(new LinkedHashMap()).put("index", true).put("delete", false).map() + ) + .build() + ) + ); } public void testCheckRestrictedIndexPatternPermission() throws Exception { User user = new User(randomAlphaOfLengthBetween(4, 12)); Authentication authentication = mock(Authentication.class); when(authentication.getUser()).thenReturn(user); - final String patternPrefix = XPackPlugin.ASYNC_RESULTS_INDEX.substring(0, - randomIntBetween(2, XPackPlugin.ASYNC_RESULTS_INDEX.length() - 2)); + final String patternPrefix = XPackPlugin.ASYNC_RESULTS_INDEX.substring( + 0, + randomIntBetween(2, XPackPlugin.ASYNC_RESULTS_INDEX.length() - 2) + ); Role role = Role.builder(RESTRICTED_INDICES_AUTOMATON, "role") - .add(FieldPermissions.DEFAULT, null, IndexPrivilege.INDEX, false, patternPrefix + "*") - .build(); + .add(FieldPermissions.DEFAULT, null, IndexPrivilege.INDEX, false, patternPrefix + "*") + .build(); RBACAuthorizationInfo authzInfo = new RBACAuthorizationInfo(role, null); String prePatternPrefix = patternPrefix.substring(0, randomIntBetween(1, patternPrefix.length() - 1)) + "*"; - HasPrivilegesResponse response = hasPrivileges(RoleDescriptor.IndicesPrivileges.builder() + HasPrivilegesResponse response = hasPrivileges( + RoleDescriptor.IndicesPrivileges.builder() .indices(prePatternPrefix) .allowRestrictedIndices(randomBoolean()) .privileges("index") - .build(), authentication, authzInfo, Collections.emptyList(), Strings.EMPTY_ARRAY); + .build(), + authentication, + authzInfo, + Collections.emptyList(), + Strings.EMPTY_ARRAY + ); assertThat(response.isCompleteMatch(), is(false)); assertThat(response.getIndexPrivileges(), Matchers.iterableWithSize(1)); - assertThat(response.getIndexPrivileges(), containsInAnyOrder( + assertThat( + response.getIndexPrivileges(), + containsInAnyOrder( ResourcePrivileges.builder(prePatternPrefix) - .addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap()) - .put("index", false).map()).build())); + .addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap()).put("index", false).map()) + .build() + ) + ); String matchesPatternPrefix = XPackPlugin.ASYNC_RESULTS_INDEX.substring(0, patternPrefix.length() + 1); - response = hasPrivileges(RoleDescriptor.IndicesPrivileges.builder() + response = hasPrivileges( + RoleDescriptor.IndicesPrivileges.builder() .indices(matchesPatternPrefix + "*") .allowRestrictedIndices(false) .privileges("index") - .build(), authentication, authzInfo, Collections.emptyList(), Strings.EMPTY_ARRAY); + .build(), + authentication, + authzInfo, + Collections.emptyList(), + Strings.EMPTY_ARRAY + ); assertThat(response.isCompleteMatch(), is(true)); assertThat(response.getIndexPrivileges(), Matchers.iterableWithSize(1)); - assertThat(response.getIndexPrivileges(), containsInAnyOrder( + assertThat( + response.getIndexPrivileges(), + containsInAnyOrder( ResourcePrivileges.builder(matchesPatternPrefix + "*") - .addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap()) - .put("index", true).map()).build())); - response = hasPrivileges(RoleDescriptor.IndicesPrivileges.builder() + .addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap()).put("index", true).map()) + .build() + ) + ); + response = hasPrivileges( + RoleDescriptor.IndicesPrivileges.builder() .indices(matchesPatternPrefix + "*") .allowRestrictedIndices(true) .privileges("index") - .build(), authentication, authzInfo, Collections.emptyList(), Strings.EMPTY_ARRAY); + .build(), + authentication, + authzInfo, + Collections.emptyList(), + Strings.EMPTY_ARRAY + ); assertThat(response.isCompleteMatch(), is(false)); assertThat(response.getIndexPrivileges(), Matchers.iterableWithSize(1)); - assertThat(response.getIndexPrivileges(), containsInAnyOrder( + assertThat( + response.getIndexPrivileges(), + containsInAnyOrder( ResourcePrivileges.builder(matchesPatternPrefix + "*") - .addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap()) - .put("index", false).map()).build())); - response = hasPrivileges(RoleDescriptor.IndicesPrivileges.builder() + .addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap()).put("index", false).map()) + .build() + ) + ); + response = hasPrivileges( + RoleDescriptor.IndicesPrivileges.builder() .indices(matchesPatternPrefix) .allowRestrictedIndices(randomBoolean()) .privileges("index") - .build(), authentication, authzInfo, Collections.emptyList(), Strings.EMPTY_ARRAY); + .build(), + authentication, + authzInfo, + Collections.emptyList(), + Strings.EMPTY_ARRAY + ); assertThat(response.isCompleteMatch(), is(true)); assertThat(response.getIndexPrivileges(), Matchers.iterableWithSize(1)); - assertThat(response.getIndexPrivileges(), containsInAnyOrder( + assertThat( + response.getIndexPrivileges(), + containsInAnyOrder( ResourcePrivileges.builder(matchesPatternPrefix) - .addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap()) - .put("index", true).map()).build())); + .addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap()).put("index", true).map()) + .build() + ) + ); final String restrictedIndexMatchingWildcard = XPackPlugin.ASYNC_RESULTS_INDEX + randomAlphaOfLengthBetween(0, 2); - response = hasPrivileges(RoleDescriptor.IndicesPrivileges.builder() + response = hasPrivileges( + RoleDescriptor.IndicesPrivileges.builder() .indices(restrictedIndexMatchingWildcard + "*") .allowRestrictedIndices(true) .privileges("index") - .build(), authentication, authzInfo, Collections.emptyList(), Strings.EMPTY_ARRAY); + .build(), + authentication, + authzInfo, + Collections.emptyList(), + Strings.EMPTY_ARRAY + ); assertThat(response.isCompleteMatch(), is(false)); assertThat(response.getIndexPrivileges(), Matchers.iterableWithSize(1)); - assertThat(response.getIndexPrivileges(), containsInAnyOrder( + assertThat( + response.getIndexPrivileges(), + containsInAnyOrder( ResourcePrivileges.builder(restrictedIndexMatchingWildcard + "*") - .addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap()) - .put("index", false).map()).build())); - response = hasPrivileges(RoleDescriptor.IndicesPrivileges.builder() + .addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap()).put("index", false).map()) + .build() + ) + ); + response = hasPrivileges( + RoleDescriptor.IndicesPrivileges.builder() .indices(restrictedIndexMatchingWildcard + "*") .allowRestrictedIndices(false) .privileges("index") - .build(), authentication, authzInfo, Collections.emptyList(), Strings.EMPTY_ARRAY); + .build(), + authentication, + authzInfo, + Collections.emptyList(), + Strings.EMPTY_ARRAY + ); assertThat(response.isCompleteMatch(), is(false)); assertThat(response.getIndexPrivileges(), Matchers.iterableWithSize(1)); - assertThat(response.getIndexPrivileges(), containsInAnyOrder( + assertThat( + response.getIndexPrivileges(), + containsInAnyOrder( ResourcePrivileges.builder(restrictedIndexMatchingWildcard + "*") - .addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap()) - .put("index", false).map()).build())); - response = hasPrivileges(RoleDescriptor.IndicesPrivileges.builder() + .addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap()).put("index", false).map()) + .build() + ) + ); + response = hasPrivileges( + RoleDescriptor.IndicesPrivileges.builder() .indices(restrictedIndexMatchingWildcard) .allowRestrictedIndices(randomBoolean()) .privileges("index") - .build(), authentication, authzInfo, Collections.emptyList(), Strings.EMPTY_ARRAY); + .build(), + authentication, + authzInfo, + Collections.emptyList(), + Strings.EMPTY_ARRAY + ); assertThat(response.isCompleteMatch(), is(false)); assertThat(response.getIndexPrivileges(), Matchers.iterableWithSize(1)); - assertThat(response.getIndexPrivileges(), containsInAnyOrder( + assertThat( + response.getIndexPrivileges(), + containsInAnyOrder( ResourcePrivileges.builder(restrictedIndexMatchingWildcard) - .addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap()) - .put("index", false).map()).build())); + .addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap()).put("index", false).map()) + .build() + ) + ); role = Role.builder(RESTRICTED_INDICES_AUTOMATON, "role") - .add(FieldPermissions.DEFAULT, null, IndexPrivilege.INDEX, true, patternPrefix + "*") - .build(); + .add(FieldPermissions.DEFAULT, null, IndexPrivilege.INDEX, true, patternPrefix + "*") + .build(); authzInfo = new RBACAuthorizationInfo(role, null); - response = hasPrivileges(RoleDescriptor.IndicesPrivileges.builder() + response = hasPrivileges( + RoleDescriptor.IndicesPrivileges.builder() .indices(matchesPatternPrefix + "*") .allowRestrictedIndices(randomBoolean()) .privileges("index") - .build(), authentication, authzInfo, Collections.emptyList(), Strings.EMPTY_ARRAY); + .build(), + authentication, + authzInfo, + Collections.emptyList(), + Strings.EMPTY_ARRAY + ); assertThat(response.isCompleteMatch(), is(true)); assertThat(response.getIndexPrivileges(), Matchers.iterableWithSize(1)); - assertThat(response.getIndexPrivileges(), containsInAnyOrder( + assertThat( + response.getIndexPrivileges(), + containsInAnyOrder( ResourcePrivileges.builder(matchesPatternPrefix + "*") - .addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap()) - .put("index", true).map()).build())); + .addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap()).put("index", true).map()) + .build() + ) + ); } public void testCheckExplicitRestrictedIndexPermissions() throws Exception { @@ -734,36 +873,70 @@ public void testCheckExplicitRestrictedIndexPermissions() throws Exception { RBACAuthorizationInfo authzInfo = new RBACAuthorizationInfo(role, null); String explicitRestrictedIndex = randomFrom(RestrictedIndicesNames.RESTRICTED_NAMES); - HasPrivilegesResponse response = hasPrivileges(RoleDescriptor.IndicesPrivileges.builder() - .indices(new String[] {".secret-non-restricted", explicitRestrictedIndex}) + HasPrivilegesResponse response = hasPrivileges( + RoleDescriptor.IndicesPrivileges.builder() + .indices(new String[] { ".secret-non-restricted", explicitRestrictedIndex }) .privileges("index", "monitor") .allowRestrictedIndices(false) // explicit false for test - .build(), authentication, authzInfo, Collections.emptyList(), Strings.EMPTY_ARRAY); + .build(), + authentication, + authzInfo, + Collections.emptyList(), + Strings.EMPTY_ARRAY + ); assertThat(response.isCompleteMatch(), is(false)); assertThat(response.getIndexPrivileges(), Matchers.iterableWithSize(2)); - assertThat(response.getIndexPrivileges(), containsInAnyOrder( + assertThat( + response.getIndexPrivileges(), + containsInAnyOrder( ResourcePrivileges.builder(".secret-non-restricted") // matches ".sec*" but not ".security*" - .addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap()) - .put("index", true).put("monitor", false).map()).build(), + .addPrivileges( + MapBuilder.newMapBuilder(new LinkedHashMap()).put("index", true).put("monitor", false).map() + ) + .build(), ResourcePrivileges.builder(explicitRestrictedIndex) // matches both ".sec*" and ".security*" - .addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap()) - .put("index", restrictedIndexPermission).put("monitor", restrictedMonitorPermission).map()).build())); + .addPrivileges( + MapBuilder.newMapBuilder(new LinkedHashMap()) + .put("index", restrictedIndexPermission) + .put("monitor", restrictedMonitorPermission) + .map() + ) + .build() + ) + ); explicitRestrictedIndex = randomFrom(RestrictedIndicesNames.RESTRICTED_NAMES); - response = hasPrivileges(RoleDescriptor.IndicesPrivileges.builder() - .indices(new String[] {".secret-non-restricted", explicitRestrictedIndex}) + response = hasPrivileges( + RoleDescriptor.IndicesPrivileges.builder() + .indices(new String[] { ".secret-non-restricted", explicitRestrictedIndex }) .privileges("index", "monitor") .allowRestrictedIndices(true) // explicit true for test - .build(), authentication, authzInfo, Collections.emptyList(), Strings.EMPTY_ARRAY); + .build(), + authentication, + authzInfo, + Collections.emptyList(), + Strings.EMPTY_ARRAY + ); assertThat(response.isCompleteMatch(), is(false)); assertThat(response.getIndexPrivileges(), Matchers.iterableWithSize(2)); - assertThat(response.getIndexPrivileges(), containsInAnyOrder( + assertThat( + response.getIndexPrivileges(), + containsInAnyOrder( ResourcePrivileges.builder(".secret-non-restricted") // matches ".sec*" but not ".security*" - .addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap()) - .put("index", true).put("monitor", false).map()).build(), + .addPrivileges( + MapBuilder.newMapBuilder(new LinkedHashMap()).put("index", true).put("monitor", false).map() + ) + .build(), ResourcePrivileges.builder(explicitRestrictedIndex) // matches both ".sec*" and ".security*" - .addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap()) - .put("index", restrictedIndexPermission).put("monitor", restrictedMonitorPermission).map()).build())); + .addPrivileges( + MapBuilder.newMapBuilder(new LinkedHashMap()) + .put("index", restrictedIndexPermission) + .put("monitor", restrictedMonitorPermission) + .map() + ) + .build() + ) + ); } public void testCheckRestrictedIndexWildcardPermissions() throws Exception { @@ -776,73 +949,119 @@ public void testCheckRestrictedIndexWildcardPermissions() throws Exception { .build(); RBACAuthorizationInfo authzInfo = new RBACAuthorizationInfo(role, null); - HasPrivilegesResponse response = hasPrivileges(RoleDescriptor.IndicesPrivileges.builder() - .indices(".sec*", ".security*") - .privileges("index", "monitor") - .build(), authentication, authzInfo, Collections.emptyList(), Strings.EMPTY_ARRAY); + HasPrivilegesResponse response = hasPrivileges( + RoleDescriptor.IndicesPrivileges.builder().indices(".sec*", ".security*").privileges("index", "monitor").build(), + authentication, + authzInfo, + Collections.emptyList(), + Strings.EMPTY_ARRAY + ); assertThat(response.isCompleteMatch(), is(false)); assertThat(response.getIndexPrivileges(), Matchers.iterableWithSize(2)); - assertThat(response.getIndexPrivileges(), containsInAnyOrder( - ResourcePrivileges.builder(".sec*") - .addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap()) - .put("index", true).put("monitor", false).map()).build(), - ResourcePrivileges.builder(".security*") - .addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap()) - .put("index", true).put("monitor", true).map()).build() - )); - - response = hasPrivileges(RoleDescriptor.IndicesPrivileges.builder() + assertThat( + response.getIndexPrivileges(), + containsInAnyOrder( + ResourcePrivileges.builder(".sec*") + .addPrivileges( + MapBuilder.newMapBuilder(new LinkedHashMap()).put("index", true).put("monitor", false).map() + ) + .build(), + ResourcePrivileges.builder(".security*") + .addPrivileges( + MapBuilder.newMapBuilder(new LinkedHashMap()).put("index", true).put("monitor", true).map() + ) + .build() + ) + ); + + response = hasPrivileges( + RoleDescriptor.IndicesPrivileges.builder() .indices(".sec*", ".security*") .privileges("index", "monitor") .allowRestrictedIndices(true) - .build(), authentication, authzInfo, Collections.emptyList(), Strings.EMPTY_ARRAY); + .build(), + authentication, + authzInfo, + Collections.emptyList(), + Strings.EMPTY_ARRAY + ); assertThat(response.isCompleteMatch(), is(false)); assertThat(response.getIndexPrivileges(), Matchers.iterableWithSize(2)); - assertThat(response.getIndexPrivileges(), containsInAnyOrder( - ResourcePrivileges.builder(".sec*") - .addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap()) - .put("index", false).put("monitor", false).map()).build(), - ResourcePrivileges.builder(".security*") - .addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap()) - .put("index", false).put("monitor", true).map()).build() - )); + assertThat( + response.getIndexPrivileges(), + containsInAnyOrder( + ResourcePrivileges.builder(".sec*") + .addPrivileges( + MapBuilder.newMapBuilder(new LinkedHashMap()).put("index", false).put("monitor", false).map() + ) + .build(), + ResourcePrivileges.builder(".security*") + .addPrivileges( + MapBuilder.newMapBuilder(new LinkedHashMap()).put("index", false).put("monitor", true).map() + ) + .build() + ) + ); role = Role.builder(RESTRICTED_INDICES_AUTOMATON, "role") - .add(FieldPermissions.DEFAULT, null, IndexPrivilege.INDEX, true, ".sec*") - .add(FieldPermissions.DEFAULT, null, IndexPrivilege.MONITOR, false, ".security*") - .build(); + .add(FieldPermissions.DEFAULT, null, IndexPrivilege.INDEX, true, ".sec*") + .add(FieldPermissions.DEFAULT, null, IndexPrivilege.MONITOR, false, ".security*") + .build(); authzInfo = new RBACAuthorizationInfo(role, null); - response = hasPrivileges(RoleDescriptor.IndicesPrivileges.builder() - .indices(".sec*", ".security*") - .privileges("index", "monitor") - .build(), authentication, authzInfo, Collections.emptyList(), Strings.EMPTY_ARRAY); + response = hasPrivileges( + RoleDescriptor.IndicesPrivileges.builder().indices(".sec*", ".security*").privileges("index", "monitor").build(), + authentication, + authzInfo, + Collections.emptyList(), + Strings.EMPTY_ARRAY + ); assertThat(response.isCompleteMatch(), is(false)); assertThat(response.getIndexPrivileges(), Matchers.iterableWithSize(2)); - assertThat(response.getIndexPrivileges(), containsInAnyOrder( - ResourcePrivileges.builder(".sec*") - .addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap()) - .put("index", true).put("monitor", false).map()).build(), - ResourcePrivileges.builder(".security*") - .addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap()) - .put("index", true).put("monitor", true).map()).build() - )); - - response = hasPrivileges(RoleDescriptor.IndicesPrivileges.builder() + assertThat( + response.getIndexPrivileges(), + containsInAnyOrder( + ResourcePrivileges.builder(".sec*") + .addPrivileges( + MapBuilder.newMapBuilder(new LinkedHashMap()).put("index", true).put("monitor", false).map() + ) + .build(), + ResourcePrivileges.builder(".security*") + .addPrivileges( + MapBuilder.newMapBuilder(new LinkedHashMap()).put("index", true).put("monitor", true).map() + ) + .build() + ) + ); + + response = hasPrivileges( + RoleDescriptor.IndicesPrivileges.builder() .indices(".sec*", ".security*") .privileges("index", "monitor") .allowRestrictedIndices(true) - .build(), authentication, authzInfo, Collections.emptyList(), Strings.EMPTY_ARRAY); + .build(), + authentication, + authzInfo, + Collections.emptyList(), + Strings.EMPTY_ARRAY + ); assertThat(response.isCompleteMatch(), is(false)); assertThat(response.getIndexPrivileges(), Matchers.iterableWithSize(2)); - assertThat(response.getIndexPrivileges(), containsInAnyOrder( - ResourcePrivileges.builder(".sec*") - .addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap()) - .put("index", true).put("monitor", false).map()).build(), - ResourcePrivileges.builder(".security*") - .addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap()) - .put("index", true).put("monitor", false).map()).build() - )); + assertThat( + response.getIndexPrivileges(), + containsInAnyOrder( + ResourcePrivileges.builder(".sec*") + .addPrivileges( + MapBuilder.newMapBuilder(new LinkedHashMap()).put("index", true).put("monitor", false).map() + ) + .build(), + ResourcePrivileges.builder(".security*") + .addPrivileges( + MapBuilder.newMapBuilder(new LinkedHashMap()).put("index", true).put("monitor", false).map() + ) + .build() + ) + ); } public void testCheckingApplicationPrivilegesOnDifferentApplicationsAndResources() throws Exception { @@ -865,8 +1084,9 @@ public void testCheckingApplicationPrivilegesOnDifferentApplicationsAndResources .build(); RBACAuthorizationInfo authzInfo = new RBACAuthorizationInfo(role, null); - final HasPrivilegesResponse response = hasPrivileges(new RoleDescriptor.IndicesPrivileges[0], - new RoleDescriptor.ApplicationResourcePrivileges[]{ + final HasPrivilegesResponse response = hasPrivileges( + new RoleDescriptor.IndicesPrivileges[0], + new RoleDescriptor.ApplicationResourcePrivileges[] { RoleDescriptor.ApplicationResourcePrivileges.builder() .application("app1") .resources("foo/1", "foo/bar/2", "foo/bar/baz", "baz/bar/foo") @@ -876,36 +1096,104 @@ public void testCheckingApplicationPrivilegesOnDifferentApplicationsAndResources .application("app2") .resources("foo/1", "foo/bar/2", "foo/bar/baz", "baz/bar/foo") .privileges("read", "write", "all") - .build() - }, authentication, authzInfo, privs, Strings.EMPTY_ARRAY); + .build() }, + authentication, + authzInfo, + privs, + Strings.EMPTY_ARRAY + ); assertThat(response.isCompleteMatch(), is(false)); assertThat(response.getIndexPrivileges(), Matchers.emptyIterable()); assertThat(response.getApplicationPrivileges().entrySet(), Matchers.iterableWithSize(2)); final Set app1 = response.getApplicationPrivileges().get("app1"); assertThat(app1, Matchers.iterableWithSize(4)); - assertThat(Strings.collectionToCommaDelimitedString(app1), app1, containsInAnyOrder( - ResourcePrivileges.builder("foo/1").addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap()) - .put("read", true).put("write", false).put("all", false).map()).build(), - ResourcePrivileges.builder("foo/bar/2").addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap()) - .put("read", true).put("write", false).put("all", false).map()).build(), - ResourcePrivileges.builder("foo/bar/baz").addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap()) - .put("read", true).put("write", true).put("all", true).map()).build(), - ResourcePrivileges.builder("baz/bar/foo").addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap()) - .put("read", false).put("write", false).put("all", false).map()).build() - )); + assertThat( + Strings.collectionToCommaDelimitedString(app1), + app1, + containsInAnyOrder( + ResourcePrivileges.builder("foo/1") + .addPrivileges( + MapBuilder.newMapBuilder(new LinkedHashMap()) + .put("read", true) + .put("write", false) + .put("all", false) + .map() + ) + .build(), + ResourcePrivileges.builder("foo/bar/2") + .addPrivileges( + MapBuilder.newMapBuilder(new LinkedHashMap()) + .put("read", true) + .put("write", false) + .put("all", false) + .map() + ) + .build(), + ResourcePrivileges.builder("foo/bar/baz") + .addPrivileges( + MapBuilder.newMapBuilder(new LinkedHashMap()) + .put("read", true) + .put("write", true) + .put("all", true) + .map() + ) + .build(), + ResourcePrivileges.builder("baz/bar/foo") + .addPrivileges( + MapBuilder.newMapBuilder(new LinkedHashMap()) + .put("read", false) + .put("write", false) + .put("all", false) + .map() + ) + .build() + ) + ); final Set app2 = response.getApplicationPrivileges().get("app2"); assertThat(app2, Matchers.iterableWithSize(4)); - assertThat(Strings.collectionToCommaDelimitedString(app2), app2, containsInAnyOrder( - ResourcePrivileges.builder("foo/1").addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap()) - .put("read", false).put("write", false).put("all", false).map()).build(), - ResourcePrivileges.builder("foo/bar/2").addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap()) - .put("read", true).put("write", true).put("all", false).map()).build(), - ResourcePrivileges.builder("foo/bar/baz").addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap()) - .put("read", true).put("write", true).put("all", false).map()).build(), - ResourcePrivileges.builder("baz/bar/foo").addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap()) - .put("read", false).put("write", true).put("all", false).map()).build() - )); + assertThat( + Strings.collectionToCommaDelimitedString(app2), + app2, + containsInAnyOrder( + ResourcePrivileges.builder("foo/1") + .addPrivileges( + MapBuilder.newMapBuilder(new LinkedHashMap()) + .put("read", false) + .put("write", false) + .put("all", false) + .map() + ) + .build(), + ResourcePrivileges.builder("foo/bar/2") + .addPrivileges( + MapBuilder.newMapBuilder(new LinkedHashMap()) + .put("read", true) + .put("write", true) + .put("all", false) + .map() + ) + .build(), + ResourcePrivileges.builder("foo/bar/baz") + .addPrivileges( + MapBuilder.newMapBuilder(new LinkedHashMap()) + .put("read", true) + .put("write", true) + .put("all", false) + .map() + ) + .build(), + ResourcePrivileges.builder("baz/bar/foo") + .addPrivileges( + MapBuilder.newMapBuilder(new LinkedHashMap()) + .put("read", false) + .put("write", true) + .put("all", false) + .map() + ) + .build() + ) + ); } public void testCheckingApplicationPrivilegesWithComplexNames() throws Exception { @@ -927,25 +1215,36 @@ public void testCheckingApplicationPrivilegesWithComplexNames() throws Exception final HasPrivilegesResponse response = hasPrivileges( new RoleDescriptor.IndicesPrivileges[0], - new RoleDescriptor.ApplicationResourcePrivileges[]{ + new RoleDescriptor.ApplicationResourcePrivileges[] { RoleDescriptor.ApplicationResourcePrivileges.builder() .application(appName) .resources("user/hawkeye/name") .privileges("DATA:read/user/*", "ACTION:" + action1, "ACTION:" + action2, action1, action2) - .build() - }, authentication, authzInfo, privs, "monitor"); + .build() }, + authentication, + authzInfo, + privs, + "monitor" + ); assertThat(response.isCompleteMatch(), is(false)); assertThat(response.getApplicationPrivileges().keySet(), containsInAnyOrder(appName)); assertThat(response.getApplicationPrivileges().get(appName), iterableWithSize(1)); - assertThat(response.getApplicationPrivileges().get(appName), containsInAnyOrder( - ResourcePrivileges.builder("user/hawkeye/name").addPrivileges(MapBuilder.newMapBuilder(new LinkedHashMap()) - .put("DATA:read/user/*", true) - .put("ACTION:" + action1, true) - .put("ACTION:" + action2, false) - .put(action1, true) - .put(action2, false) - .map()).build() - )); + assertThat( + response.getApplicationPrivileges().get(appName), + containsInAnyOrder( + ResourcePrivileges.builder("user/hawkeye/name") + .addPrivileges( + MapBuilder.newMapBuilder(new LinkedHashMap()) + .put("DATA:read/user/*", true) + .put("ACTION:" + action1, true) + .put("ACTION:" + action2, false) + .put(action1, true) + .put(action2, false) + .map() + ) + .build() + ) + ); } public void testIsCompleteMatch() throws Exception { @@ -963,45 +1262,61 @@ public void testIsCompleteMatch() throws Exception { .build(); RBACAuthorizationInfo authzInfo = new RBACAuthorizationInfo(role, null); - - assertThat(hasPrivileges( - indexPrivileges("read", "read-123", "read-456", "all-999"), authentication, authzInfo, privs, "monitor").isCompleteMatch(), - is(true)); - assertThat(hasPrivileges( - indexPrivileges("read", "read-123", "read-456", "all-999"), authentication, authzInfo, privs, "manage").isCompleteMatch(), - is(false)); - assertThat(hasPrivileges( - indexPrivileges("write", "read-123", "read-456", "all-999"), authentication, authzInfo, privs, "monitor").isCompleteMatch(), - is(false)); - assertThat(hasPrivileges( - indexPrivileges("write", "read-123", "read-456", "all-999"), authentication, authzInfo, privs, "manage").isCompleteMatch(), - is(false)); - assertThat(hasPrivileges( - new RoleDescriptor.IndicesPrivileges[]{ - RoleDescriptor.IndicesPrivileges.builder() - .indices("read-a") - .privileges("read") - .build(), - RoleDescriptor.IndicesPrivileges.builder() - .indices("all-b") - .privileges("read", "write") - .build() - }, - new RoleDescriptor.ApplicationResourcePrivileges[]{ - RoleDescriptor.ApplicationResourcePrivileges.builder() - .application("kibana") - .resources("*") - .privileges("read") - .build() - }, authentication, authzInfo, privs, "monitor").isCompleteMatch(), is(true)); - assertThat(hasPrivileges( - new RoleDescriptor.IndicesPrivileges[]{indexPrivileges("read", "read-123", "read-456", "all-999")}, - new RoleDescriptor.ApplicationResourcePrivileges[]{ - RoleDescriptor.ApplicationResourcePrivileges.builder() - .application("kibana").resources("*").privileges("read").build(), - RoleDescriptor.ApplicationResourcePrivileges.builder() - .application("kibana").resources("*").privileges("write").build() - }, authentication, authzInfo, privs, "monitor").isCompleteMatch(), is(false)); + assertThat( + hasPrivileges(indexPrivileges("read", "read-123", "read-456", "all-999"), authentication, authzInfo, privs, "monitor") + .isCompleteMatch(), + is(true) + ); + assertThat( + hasPrivileges(indexPrivileges("read", "read-123", "read-456", "all-999"), authentication, authzInfo, privs, "manage") + .isCompleteMatch(), + is(false) + ); + assertThat( + hasPrivileges(indexPrivileges("write", "read-123", "read-456", "all-999"), authentication, authzInfo, privs, "monitor") + .isCompleteMatch(), + is(false) + ); + assertThat( + hasPrivileges(indexPrivileges("write", "read-123", "read-456", "all-999"), authentication, authzInfo, privs, "manage") + .isCompleteMatch(), + is(false) + ); + assertThat( + hasPrivileges( + new RoleDescriptor.IndicesPrivileges[] { + RoleDescriptor.IndicesPrivileges.builder().indices("read-a").privileges("read").build(), + RoleDescriptor.IndicesPrivileges.builder().indices("all-b").privileges("read", "write").build() }, + new RoleDescriptor.ApplicationResourcePrivileges[] { + RoleDescriptor.ApplicationResourcePrivileges.builder() + .application("kibana") + .resources("*") + .privileges("read") + .build() }, + authentication, + authzInfo, + privs, + "monitor" + ).isCompleteMatch(), + is(true) + ); + assertThat( + hasPrivileges( + new RoleDescriptor.IndicesPrivileges[] { indexPrivileges("read", "read-123", "read-456", "all-999") }, + new RoleDescriptor.ApplicationResourcePrivileges[] { + RoleDescriptor.ApplicationResourcePrivileges.builder().application("kibana").resources("*").privileges("read").build(), + RoleDescriptor.ApplicationResourcePrivileges.builder() + .application("kibana") + .resources("*") + .privileges("write") + .build() }, + authentication, + authzInfo, + privs, + "monitor" + ).isCompleteMatch(), + is(false) + ); } public void testBuildUserPrivilegeResponse() { @@ -1012,9 +1327,13 @@ public void testBuildUserPrivilegeResponse() { .add(IndexPrivilege.get(Sets.newHashSet("read", "write")), "index-1") .add(IndexPrivilege.ALL, "index-2", "index-3") .add( - new FieldPermissions(new FieldPermissionsDefinition(new String[]{ "public.*" }, new String[0])), + new FieldPermissions(new FieldPermissionsDefinition(new String[] { "public.*" }, new String[0])), Collections.singleton(query), - IndexPrivilege.READ, randomBoolean(), "index-4", "index-5") + IndexPrivilege.READ, + randomBoolean(), + "index-4", + "index-5" + ) .addApplicationPrivilege(new ApplicationPrivilege("app01", "read", "data:read"), Collections.singleton("*")) .runAs(new Privilege(Sets.newHashSet("user01", "user02"), "user01", "user02")) .build(); @@ -1038,12 +1357,17 @@ IndexPrivilege.READ, randomBoolean(), "index-4", "index-5") final GetUserPrivilegesResponse.Indices index4 = findIndexPrivilege(response.getIndexPrivileges(), "index-4"); assertThat(index4.getIndices(), containsInAnyOrder("index-4", "index-5")); assertThat(index4.getPrivileges(), containsInAnyOrder("read")); - assertThat(index4.getFieldSecurity(), containsInAnyOrder( - new FieldPermissionsDefinition.FieldGrantExcludeGroup(new String[]{ "public.*" }, new String[0]))); + assertThat( + index4.getFieldSecurity(), + containsInAnyOrder(new FieldPermissionsDefinition.FieldGrantExcludeGroup(new String[] { "public.*" }, new String[0])) + ); assertThat(index4.getQueries(), containsInAnyOrder(query)); - assertThat(response.getApplicationPrivileges(), containsInAnyOrder( - RoleDescriptor.ApplicationResourcePrivileges.builder().application("app01").privileges("read").resources("*").build()) + assertThat( + response.getApplicationPrivileges(), + containsInAnyOrder( + RoleDescriptor.ApplicationResourcePrivileges.builder().application("app01").privileges("read").resources("*").build() + ) ); assertThat(response.getRunAs(), containsInAnyOrder("user01", "user02")); @@ -1065,8 +1389,11 @@ public void testBackingIndicesAreIncludedForAuthorizedDataStreams() { for (int k = 0; k < numBackingIndices; k++) { backingIndices.add(DataStreamTestHelper.createBackingIndex(dataStreamName, k + 1).build()); } - DataStream ds = new DataStream(dataStreamName, null, - backingIndices.stream().map(IndexMetadata::getIndex).collect(Collectors.toList())); + DataStream ds = new DataStream( + dataStreamName, + null, + backingIndices.stream().map(IndexMetadata::getIndex).collect(Collectors.toList()) + ); IndexAbstraction.DataStream iads = new IndexAbstraction.DataStream(ds, List.of()); lookup.put(ds.getName(), iads); for (IndexMetadata im : backingIndices) { @@ -1074,11 +1401,16 @@ public void testBackingIndicesAreIncludedForAuthorizedDataStreams() { } SearchRequest request = new SearchRequest("*"); - Set authorizedIndices = - RBACEngine.resolveAuthorizedIndicesFromRole(role, getRequestInfo(request, SearchAction.NAME), lookup); + Set authorizedIndices = RBACEngine.resolveAuthorizedIndicesFromRole( + role, + getRequestInfo(request, SearchAction.NAME), + lookup + ); assertThat(authorizedIndices, hasItem(dataStreamName)); - assertThat(authorizedIndices, hasItems(backingIndices.stream() - .map(im -> im.getIndex().getName()).collect(Collectors.toList()).toArray(Strings.EMPTY_ARRAY))); + assertThat( + authorizedIndices, + hasItems(backingIndices.stream().map(im -> im.getIndex().getName()).collect(Collectors.toList()).toArray(Strings.EMPTY_ARRAY)) + ); } public void testExplicitMappingUpdatesAreNotGrantedWithIngestPrivileges() { @@ -1087,10 +1419,10 @@ public void testExplicitMappingUpdatesAreNotGrantedWithIngestPrivileges() { Authentication authentication = mock(Authentication.class); when(authentication.getUser()).thenReturn(user); Role role = Role.builder(RESTRICTED_INDICES_AUTOMATON, "test1") - .cluster(Collections.emptySet(), Collections.emptyList()) - .add(IndexPrivilege.CREATE, "my_*") - .add(IndexPrivilege.WRITE, "my_data*") - .build(); + .cluster(Collections.emptySet(), Collections.emptyList()) + .add(IndexPrivilege.CREATE, "my_*") + .add(IndexPrivilege.WRITE, "my_data*") + .build(); TreeMap lookup = new TreeMap<>(); List backingIndices = new ArrayList<>(); @@ -1098,8 +1430,11 @@ public void testExplicitMappingUpdatesAreNotGrantedWithIngestPrivileges() { for (int k = 0; k < numBackingIndices; k++) { backingIndices.add(DataStreamTestHelper.createBackingIndex(dataStreamName, k + 1).build()); } - DataStream ds = new DataStream(dataStreamName, null, - backingIndices.stream().map(IndexMetadata::getIndex).collect(Collectors.toList())); + DataStream ds = new DataStream( + dataStreamName, + null, + backingIndices.stream().map(IndexMetadata::getIndex).collect(Collectors.toList()) + ); IndexAbstraction.DataStream iads = new IndexAbstraction.DataStream(ds, List.of()); lookup.put(ds.getName(), iads); for (IndexMetadata im : backingIndices) { @@ -1107,11 +1442,12 @@ public void testExplicitMappingUpdatesAreNotGrantedWithIngestPrivileges() { } PutMappingRequest request = new PutMappingRequest("*"); - request.source("{ \"properties\": { \"message\": { \"type\": \"text\" } } }", - XContentType.JSON + request.source("{ \"properties\": { \"message\": { \"type\": \"text\" } } }", XContentType.JSON); + Set authorizedIndices = RBACEngine.resolveAuthorizedIndicesFromRole( + role, + getRequestInfo(request, PutMappingAction.NAME), + lookup ); - Set authorizedIndices = - RBACEngine.resolveAuthorizedIndicesFromRole(role, getRequestInfo(request, PutMappingAction.NAME), lookup); assertThat(authorizedIndices.isEmpty(), is(true)); } @@ -1120,36 +1456,44 @@ private GetUserPrivilegesResponse.Indices findIndexPrivilege(Set privs, String app, String name, - String ... actions) { + private ApplicationPrivilege defineApplicationPrivilege( + List privs, + String app, + String name, + String... actions + ) { privs.add(new ApplicationPrivilegeDescriptor(app, name, newHashSet(actions), emptyMap())); return new ApplicationPrivilege(app, name, actions); } - private HasPrivilegesResponse hasPrivileges(RoleDescriptor.IndicesPrivileges indicesPrivileges, Authentication authentication, - AuthorizationInfo authorizationInfo, - List applicationPrivilegeDescriptors, - String... clusterPrivileges) throws Exception { + private HasPrivilegesResponse hasPrivileges( + RoleDescriptor.IndicesPrivileges indicesPrivileges, + Authentication authentication, + AuthorizationInfo authorizationInfo, + List applicationPrivilegeDescriptors, + String... clusterPrivileges + ) throws Exception { return hasPrivileges( - new RoleDescriptor.IndicesPrivileges[]{indicesPrivileges}, + new RoleDescriptor.IndicesPrivileges[] { indicesPrivileges }, new RoleDescriptor.ApplicationResourcePrivileges[0], - authentication, authorizationInfo, applicationPrivilegeDescriptors, + authentication, + authorizationInfo, + applicationPrivilegeDescriptors, clusterPrivileges ); } - private HasPrivilegesResponse hasPrivileges(RoleDescriptor.IndicesPrivileges[] indicesPrivileges, - RoleDescriptor.ApplicationResourcePrivileges[] appPrivileges, - Authentication authentication, - AuthorizationInfo authorizationInfo, - List applicationPrivilegeDescriptors, - String... clusterPrivileges) throws Exception { + private HasPrivilegesResponse hasPrivileges( + RoleDescriptor.IndicesPrivileges[] indicesPrivileges, + RoleDescriptor.ApplicationResourcePrivileges[] appPrivileges, + Authentication authentication, + AuthorizationInfo authorizationInfo, + List applicationPrivilegeDescriptors, + String... clusterPrivileges + ) throws Exception { final HasPrivilegesRequest request = new HasPrivilegesRequest(); request.username(authentication.getUser().principal()); request.clusterPrivileges(clusterPrivileges); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/RoleDescriptorTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/RoleDescriptorTests.java index a8e2371e7a2ac..321ae550dd7b7 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/RoleDescriptorTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/RoleDescriptorTests.java @@ -17,12 +17,12 @@ import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.TestMatchers; import org.elasticsearch.test.VersionUtils; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.XPackClientPlugin; import org.elasticsearch.xpack.core.security.authz.RoleDescriptor; import org.elasticsearch.xpack.core.security.authz.privilege.ConfigurableClusterPrivilege; @@ -52,82 +52,100 @@ public class RoleDescriptorTests extends ESTestCase { public void testIndexGroup() throws Exception { RoleDescriptor.IndicesPrivileges privs = RoleDescriptor.IndicesPrivileges.builder() - .indices("idx") - .privileges("priv") - .allowRestrictedIndices(true) - .build(); + .indices("idx") + .privileges("priv") + .allowRestrictedIndices(true) + .build(); XContentBuilder b = jsonBuilder(); privs.toXContent(b, ToXContent.EMPTY_PARAMS); assertEquals("{\"names\":[\"idx\"],\"privileges\":[\"priv\"],\"allow_restricted_indices\":true}", Strings.toString(b)); } public void testEqualsOnEmptyRoles() { - RoleDescriptor nullRoleDescriptor = new RoleDescriptor("null_role", randomFrom((String[]) null, new String[0]), - randomFrom((RoleDescriptor.IndicesPrivileges[]) null, new RoleDescriptor.IndicesPrivileges[0]), - randomFrom((RoleDescriptor.ApplicationResourcePrivileges[])null, new RoleDescriptor.ApplicationResourcePrivileges[0]), - randomFrom((ConfigurableClusterPrivilege[])null, new ConfigurableClusterPrivilege[0]), - randomFrom((String[])null, new String[0]), - randomFrom((Map)null, Map.of()), - Map.of("transient", "meta", "is", "ignored")); + RoleDescriptor nullRoleDescriptor = new RoleDescriptor( + "null_role", + randomFrom((String[]) null, new String[0]), + randomFrom((RoleDescriptor.IndicesPrivileges[]) null, new RoleDescriptor.IndicesPrivileges[0]), + randomFrom((RoleDescriptor.ApplicationResourcePrivileges[]) null, new RoleDescriptor.ApplicationResourcePrivileges[0]), + randomFrom((ConfigurableClusterPrivilege[]) null, new ConfigurableClusterPrivilege[0]), + randomFrom((String[]) null, new String[0]), + randomFrom((Map) null, Map.of()), + Map.of("transient", "meta", "is", "ignored") + ); assertTrue(nullRoleDescriptor.equals(new RoleDescriptor("null_role", null, null, null, null, null, null, null))); } public void testToString() { RoleDescriptor.IndicesPrivileges[] groups = new RoleDescriptor.IndicesPrivileges[] { - RoleDescriptor.IndicesPrivileges.builder() - .indices("i1", "i2") - .privileges("read") - .grantedFields("body", "title") - .query("{\"match_all\": {}}") - .build() - }; + RoleDescriptor.IndicesPrivileges.builder() + .indices("i1", "i2") + .privileges("read") + .grantedFields("body", "title") + .query("{\"match_all\": {}}") + .build() }; final RoleDescriptor.ApplicationResourcePrivileges[] applicationPrivileges = { RoleDescriptor.ApplicationResourcePrivileges.builder() .application("my_app") .privileges("read", "write") .resources("*") - .build() - }; - - final ConfigurableClusterPrivilege[] configurableClusterPrivileges = new ConfigurableClusterPrivilege[]{ - new ConfigurableClusterPrivileges.ManageApplicationPrivileges(new LinkedHashSet<>(Arrays.asList("app01", "app02"))) - }; - - RoleDescriptor descriptor = new RoleDescriptor("test", new String[] { "all", "none" }, groups, applicationPrivileges, - configurableClusterPrivileges, new String[] { "sudo" }, Collections.emptyMap(), Collections.emptyMap()); - - assertThat(descriptor.toString(), is("Role[name=test, cluster=[all,none]" + - ", global=[{APPLICATION:manage:applications=app01,app02}]" + - ", indicesPrivileges=[IndicesPrivileges[indices=[i1,i2], allowRestrictedIndices=[false], privileges=[read]" + - ", field_security=[grant=[body,title], except=null], query={\"match_all\": {}}],]" + - ", applicationPrivileges=[ApplicationResourcePrivileges[application=my_app, privileges=[read,write], resources=[*]],]" + - ", runAs=[sudo], metadata=[{}]]")); + .build() }; + + final ConfigurableClusterPrivilege[] configurableClusterPrivileges = new ConfigurableClusterPrivilege[] { + new ConfigurableClusterPrivileges.ManageApplicationPrivileges(new LinkedHashSet<>(Arrays.asList("app01", "app02"))) }; + + RoleDescriptor descriptor = new RoleDescriptor( + "test", + new String[] { "all", "none" }, + groups, + applicationPrivileges, + configurableClusterPrivileges, + new String[] { "sudo" }, + Collections.emptyMap(), + Collections.emptyMap() + ); + + assertThat( + descriptor.toString(), + is( + "Role[name=test, cluster=[all,none]" + + ", global=[{APPLICATION:manage:applications=app01,app02}]" + + ", indicesPrivileges=[IndicesPrivileges[indices=[i1,i2], allowRestrictedIndices=[false], privileges=[read]" + + ", field_security=[grant=[body,title], except=null], query={\"match_all\": {}}],]" + + ", applicationPrivileges=[ApplicationResourcePrivileges[application=my_app, privileges=[read,write], resources=[*]],]" + + ", runAs=[sudo], metadata=[{}]]" + ) + ); } public void testToXContent() throws Exception { RoleDescriptor.IndicesPrivileges[] groups = new RoleDescriptor.IndicesPrivileges[] { - RoleDescriptor.IndicesPrivileges.builder() - .indices("i1", "i2") - .privileges("read") - .grantedFields("body", "title") - .allowRestrictedIndices(randomBoolean()) - .query("{\"match_all\": {}}") - .build() - }; + RoleDescriptor.IndicesPrivileges.builder() + .indices("i1", "i2") + .privileges("read") + .grantedFields("body", "title") + .allowRestrictedIndices(randomBoolean()) + .query("{\"match_all\": {}}") + .build() }; final RoleDescriptor.ApplicationResourcePrivileges[] applicationPrivileges = { RoleDescriptor.ApplicationResourcePrivileges.builder() .application("my_app") .privileges("read", "write") .resources("*") - .build() - }; + .build() }; final ConfigurableClusterPrivilege[] configurableClusterPrivileges = { - new ConfigurableClusterPrivileges.ManageApplicationPrivileges(new LinkedHashSet<>(Arrays.asList("app01", "app02"))) - }; + new ConfigurableClusterPrivileges.ManageApplicationPrivileges(new LinkedHashSet<>(Arrays.asList("app01", "app02"))) }; Map metadata = randomBoolean() ? MetadataUtils.DEFAULT_RESERVED_METADATA : null; - RoleDescriptor descriptor = new RoleDescriptor("test", new String[] { "all", "none" }, groups, applicationPrivileges, - configurableClusterPrivileges, new String[]{ "sudo" }, metadata, Collections.emptyMap()); + RoleDescriptor descriptor = new RoleDescriptor( + "test", + new String[] { "all", "none" }, + groups, + applicationPrivileges, + configurableClusterPrivileges, + new String[] { "sudo" }, + metadata, + Collections.emptyMap() + ); XContentBuilder builder = descriptor.toXContent(jsonBuilder(), ToXContent.EMPTY_PARAMS); RoleDescriptor parsed = RoleDescriptor.parse("test", BytesReference.bytes(builder), false, XContentType.JSON); assertThat(parsed, equalTo(descriptor)); @@ -149,19 +167,19 @@ public void testParse() throws Exception { assertEquals(0, rd.getIndicesPrivileges().length); assertArrayEquals(new String[] { "m", "n" }, rd.getRunAs()); - q = "{\"cluster\":[\"a\", \"b\"], \"run_as\": [\"m\", \"n\"], \"index\": [{\"names\": \"idx1\", \"privileges\": [\"p1\", " + - "\"p2\"]}, {\"names\": \"idx2\", \"allow_restricted_indices\": true, \"privileges\": [\"p3\"], \"field_security\": " + - "{\"grant\": [\"f1\", \"f2\"]}}, {\"names\": " + - "\"idx2\", \"allow_restricted_indices\": false," + - "\"privileges\": [\"p3\"], \"field_security\": {\"grant\": [\"f1\", \"f2\"]}, \"query\": {\"match_all\": {}} }]}"; + q = "{\"cluster\":[\"a\", \"b\"], \"run_as\": [\"m\", \"n\"], \"index\": [{\"names\": \"idx1\", \"privileges\": [\"p1\", " + + "\"p2\"]}, {\"names\": \"idx2\", \"allow_restricted_indices\": true, \"privileges\": [\"p3\"], \"field_security\": " + + "{\"grant\": [\"f1\", \"f2\"]}}, {\"names\": " + + "\"idx2\", \"allow_restricted_indices\": false," + + "\"privileges\": [\"p3\"], \"field_security\": {\"grant\": [\"f1\", \"f2\"]}, \"query\": {\"match_all\": {}} }]}"; rd = RoleDescriptor.parse("test", new BytesArray(q), false, XContentType.JSON); assertEquals("test", rd.getName()); assertArrayEquals(new String[] { "a", "b" }, rd.getClusterPrivileges()); assertEquals(3, rd.getIndicesPrivileges().length); assertArrayEquals(new String[] { "m", "n" }, rd.getRunAs()); - q = "{\"cluster\":[\"a\", \"b\"], \"run_as\": [\"m\", \"n\"], \"index\": [{\"names\": [\"idx1\",\"idx2\"], \"privileges\": " + - "[\"p1\", \"p2\"], \"allow_restricted_indices\": true}]}"; + q = "{\"cluster\":[\"a\", \"b\"], \"run_as\": [\"m\", \"n\"], \"index\": [{\"names\": [\"idx1\",\"idx2\"], \"privileges\": " + + "[\"p1\", \"p2\"], \"allow_restricted_indices\": true}]}"; rd = RoleDescriptor.parse("test", new BytesArray(q), false, XContentType.JSON); assertEquals("test", rd.getName()); assertArrayEquals(new String[] { "a", "b" }, rd.getClusterPrivileges()); @@ -181,14 +199,14 @@ public void testParse() throws Exception { assertThat(rd.getMetadata().size(), is(1)); assertThat(rd.getMetadata().get("foo"), is("bar")); - q = "{\"cluster\":[\"a\", \"b\"], \"run_as\": [\"m\", \"n\"]," + - " \"index\": [{\"names\": [\"idx1\",\"idx2\"], \"allow_restricted_indices\": false, \"privileges\": [\"p1\", \"p2\"]}]," + - " \"applications\": [" + - " {\"resources\": [\"object-123\",\"object-456\"], \"privileges\":[\"read\", \"delete\"], \"application\":\"app1\"}," + - " {\"resources\": [\"*\"], \"privileges\":[\"admin\"], \"application\":\"app2\" }" + - " ]," + - " \"global\": { \"application\": { \"manage\": { \"applications\" : [ \"kibana\", \"logstash\" ] } } }" + - "}"; + q = "{\"cluster\":[\"a\", \"b\"], \"run_as\": [\"m\", \"n\"]," + + " \"index\": [{\"names\": [\"idx1\",\"idx2\"], \"allow_restricted_indices\": false, \"privileges\": [\"p1\", \"p2\"]}]," + + " \"applications\": [" + + " {\"resources\": [\"object-123\",\"object-456\"], \"privileges\":[\"read\", \"delete\"], \"application\":\"app1\"}," + + " {\"resources\": [\"*\"], \"privileges\":[\"admin\"], \"application\":\"app2\" }" + + " ]," + + " \"global\": { \"application\": { \"manage\": { \"applications\" : [ \"kibana\", \"logstash\" ] } } }" + + "}"; rd = RoleDescriptor.parse("test", new BytesArray(q), false, XContentType.JSON); assertThat(rd.getName(), equalTo("test")); assertThat(rd.getClusterPrivileges(), arrayContaining("a", "b")); @@ -209,8 +227,10 @@ public void testParse() throws Exception { final ConfigurableClusterPrivilege conditionalPrivilege = rd.getConditionalClusterPrivileges()[0]; assertThat(conditionalPrivilege.getCategory(), equalTo(ConfigurableClusterPrivilege.Category.APPLICATION)); assertThat(conditionalPrivilege, instanceOf(ConfigurableClusterPrivileges.ManageApplicationPrivileges.class)); - assertThat(((ConfigurableClusterPrivileges.ManageApplicationPrivileges) conditionalPrivilege).getApplicationNames(), - containsInAnyOrder("kibana", "logstash")); + assertThat( + ((ConfigurableClusterPrivileges.ManageApplicationPrivileges) conditionalPrivilege).getApplicationNames(), + containsInAnyOrder("kibana", "logstash") + ); q = "{\"applications\": [{\"application\": \"myapp\", \"resources\": [\"*\"], \"privileges\": [\"login\" ]}] }"; rd = RoleDescriptor.parse("test", new BytesArray(q), false, XContentType.JSON); @@ -223,10 +243,12 @@ public void testParse() throws Exception { assertThat(rd.getApplicationPrivileges()[0].getApplication(), equalTo("myapp")); assertThat(rd.getConditionalClusterPrivileges(), Matchers.arrayWithSize(0)); - final String badJson - = "{\"applications\":[{\"not_supported\": true, \"resources\": [\"*\"], \"privileges\": [\"my-app:login\" ]}] }"; - final IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, - () -> RoleDescriptor.parse("test", new BytesArray(badJson), false, XContentType.JSON)); + final String badJson = + "{\"applications\":[{\"not_supported\": true, \"resources\": [\"*\"], \"privileges\": [\"my-app:login\" ]}] }"; + final IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> RoleDescriptor.parse("test", new BytesArray(badJson), false, XContentType.JSON) + ); assertThat(ex.getMessage(), containsString("not_supported")); } @@ -236,39 +258,46 @@ public void testSerializationForCurrentVersion() throws Exception { BytesStreamOutput output = new BytesStreamOutput(); output.setVersion(version); RoleDescriptor.IndicesPrivileges[] groups = new RoleDescriptor.IndicesPrivileges[] { - RoleDescriptor.IndicesPrivileges.builder() - .indices("i1", "i2") - .privileges("read") - .grantedFields("body", "title") - .query("{\"query\": {\"match_all\": {}}}") - .build() - }; + RoleDescriptor.IndicesPrivileges.builder() + .indices("i1", "i2") + .privileges("read") + .grantedFields("body", "title") + .query("{\"query\": {\"match_all\": {}}}") + .build() }; final RoleDescriptor.ApplicationResourcePrivileges[] applicationPrivileges = { RoleDescriptor.ApplicationResourcePrivileges.builder() .application("my_app") .privileges("read", "write") .resources("*") - .build() - }; + .build() }; final ConfigurableClusterPrivilege[] configurableClusterPrivileges = { - new ConfigurableClusterPrivileges.ManageApplicationPrivileges(new LinkedHashSet<>(Arrays.asList("app01", "app02"))) - }; + new ConfigurableClusterPrivileges.ManageApplicationPrivileges(new LinkedHashSet<>(Arrays.asList("app01", "app02"))) }; Map metadata = randomBoolean() ? MetadataUtils.DEFAULT_RESERVED_METADATA : null; - final RoleDescriptor descriptor = new RoleDescriptor("test", new String[]{"all", "none"}, groups, applicationPrivileges, - configurableClusterPrivileges, new String[] { "sudo" }, metadata, null); + final RoleDescriptor descriptor = new RoleDescriptor( + "test", + new String[] { "all", "none" }, + groups, + applicationPrivileges, + configurableClusterPrivileges, + new String[] { "sudo" }, + metadata, + null + ); descriptor.writeTo(output); final NamedWriteableRegistry registry = new NamedWriteableRegistry(new XPackClientPlugin(Settings.EMPTY).getNamedWriteables()); - StreamInput streamInput = new NamedWriteableAwareStreamInput(ByteBufferStreamInput.wrap(BytesReference.toBytes(output.bytes())), - registry); + StreamInput streamInput = new NamedWriteableAwareStreamInput( + ByteBufferStreamInput.wrap(BytesReference.toBytes(output.bytes())), + registry + ); streamInput.setVersion(version); final RoleDescriptor serialized = new RoleDescriptor(streamInput); assertEquals(descriptor, serialized); } public void testParseEmptyQuery() throws Exception { - String json = "{\"cluster\":[\"a\", \"b\"], \"run_as\": [\"m\", \"n\"], \"index\": [{\"names\": [\"idx1\",\"idx2\"], " + - "\"privileges\": [\"p1\", \"p2\"], \"query\": \"\"}]}"; + String json = "{\"cluster\":[\"a\", \"b\"], \"run_as\": [\"m\", \"n\"], \"index\": [{\"names\": [\"idx1\",\"idx2\"], " + + "\"privileges\": [\"p1\", \"p2\"], \"query\": \"\"}]}"; RoleDescriptor rd = RoleDescriptor.parse("test", new BytesArray(json), false, XContentType.JSON); assertEquals("test", rd.getName()); assertArrayEquals(new String[] { "a", "b" }, rd.getClusterPrivileges()); @@ -279,8 +308,8 @@ public void testParseEmptyQuery() throws Exception { } public void testParseNullQuery() throws Exception { - String json = "{\"cluster\":[\"a\", \"b\"], \"run_as\": [\"m\", \"n\"], \"index\": [{\"names\": [\"idx1\",\"idx2\"], " + - "\"privileges\": [\"p1\", \"p2\"], \"query\": null}]}"; + String json = "{\"cluster\":[\"a\", \"b\"], \"run_as\": [\"m\", \"n\"], \"index\": [{\"names\": [\"idx1\",\"idx2\"], " + + "\"privileges\": [\"p1\", \"p2\"], \"query\": null}]}"; RoleDescriptor rd = RoleDescriptor.parse("test", new BytesArray(json), false, XContentType.JSON); assertEquals("test", rd.getName()); assertArrayEquals(new String[] { "a", "b" }, rd.getClusterPrivileges()); @@ -291,8 +320,8 @@ public void testParseNullQuery() throws Exception { } public void testParseEmptyQueryUsingDeprecatedIndicesField() throws Exception { - String json = "{\"cluster\":[\"a\", \"b\"], \"run_as\": [\"m\", \"n\"], \"indices\": [{\"names\": [\"idx1\",\"idx2\"], " + - "\"privileges\": [\"p1\", \"p2\"], \"query\": \"\"}]}"; + String json = "{\"cluster\":[\"a\", \"b\"], \"run_as\": [\"m\", \"n\"], \"indices\": [{\"names\": [\"idx1\",\"idx2\"], " + + "\"privileges\": [\"p1\", \"p2\"], \"query\": \"\"}]}"; RoleDescriptor rd = RoleDescriptor.parse("test", new BytesArray(json), false, XContentType.JSON); assertEquals("test", rd.getName()); assertArrayEquals(new String[] { "a", "b" }, rd.getClusterPrivileges()); @@ -303,8 +332,16 @@ public void testParseEmptyQueryUsingDeprecatedIndicesField() throws Exception { } public void testParseIgnoresTransientMetadata() throws Exception { - final RoleDescriptor descriptor = new RoleDescriptor("test", new String[] { "all" }, null, null, null, null, - Collections.singletonMap("_unlicensed_feature", true), Collections.singletonMap("foo", "bar")); + final RoleDescriptor descriptor = new RoleDescriptor( + "test", + new String[] { "all" }, + null, + null, + null, + null, + Collections.singletonMap("_unlicensed_feature", true), + Collections.singletonMap("foo", "bar") + ); XContentBuilder b = jsonBuilder(); descriptor.toXContent(b, ToXContent.EMPTY_PARAMS); RoleDescriptor parsed = RoleDescriptor.parse("test", BytesReference.bytes(b), false, XContentType.JSON); @@ -318,22 +355,30 @@ public void testParseIndicesPrivilegesSucceedsWhenExceptFieldsIsSubsetOfGrantedF final String grant = grantAll ? "\"*\"" : "\"f1\",\"f2\""; final String except = grantAll ? "\"_fx\",\"f8\"" : "\"f1\""; - final String json = "{ \"indices\": [{\"names\": [\"idx1\",\"idx2\"], \"privileges\": [\"p1\", \"p2\"], \"field_security\" : { " + - "\"grant\" : [" + grant + "], \"except\" : [" + except + "] } }] }"; - final RoleDescriptor rd = RoleDescriptor.parse("test", - new BytesArray(json), false, XContentType.JSON); + final String json = "{ \"indices\": [{\"names\": [\"idx1\",\"idx2\"], \"privileges\": [\"p1\", \"p2\"], \"field_security\" : { " + + "\"grant\" : [" + + grant + + "], \"except\" : [" + + except + + "] } }] }"; + final RoleDescriptor rd = RoleDescriptor.parse("test", new BytesArray(json), false, XContentType.JSON); assertEquals("test", rd.getName()); assertEquals(1, rd.getIndicesPrivileges().length); - assertArrayEquals(new String[]{"idx1", "idx2"}, rd.getIndicesPrivileges()[0].getIndices()); - assertArrayEquals((grantAll) ? new String[]{"*"} : new String[]{"f1", "f2"}, rd.getIndicesPrivileges()[0].getGrantedFields()); - assertArrayEquals((grantAll) ? new String[]{"_fx", "f8"} : new String[]{"f1"}, rd.getIndicesPrivileges()[0].getDeniedFields()); + assertArrayEquals(new String[] { "idx1", "idx2" }, rd.getIndicesPrivileges()[0].getIndices()); + assertArrayEquals((grantAll) ? new String[] { "*" } : new String[] { "f1", "f2" }, rd.getIndicesPrivileges()[0].getGrantedFields()); + assertArrayEquals( + (grantAll) ? new String[] { "_fx", "f8" } : new String[] { "f1" }, + rd.getIndicesPrivileges()[0].getDeniedFields() + ); } public void testParseIndicesPrivilegesFailsWhenExceptFieldsAreNotSubsetOfGrantedFields() { - final String json = "{ \"indices\": [{\"names\": [\"idx1\",\"idx2\"], \"privileges\": [\"p1\", \"p2\"], \"field_security\" : { " + - "\"grant\" : [\"f1\",\"f2\"], \"except\" : [\"f3\"] } }] }"; - final ElasticsearchParseException epe = expectThrows(ElasticsearchParseException.class, () -> RoleDescriptor.parse("test", - new BytesArray(json), false, XContentType.JSON)); + final String json = "{ \"indices\": [{\"names\": [\"idx1\",\"idx2\"], \"privileges\": [\"p1\", \"p2\"], \"field_security\" : { " + + "\"grant\" : [\"f1\",\"f2\"], \"except\" : [\"f3\"] } }] }"; + final ElasticsearchParseException epe = expectThrows( + ElasticsearchParseException.class, + () -> RoleDescriptor.parse("test", new BytesArray(json), false, XContentType.JSON) + ); assertThat(epe, TestMatchers.throwableWithMessage(containsString("must be a subset of the granted fields "))); assertThat(epe, TestMatchers.throwableWithMessage(containsString("f1"))); assertThat(epe, TestMatchers.throwableWithMessage(containsString("f2"))); @@ -341,20 +386,20 @@ public void testParseIndicesPrivilegesFailsWhenExceptFieldsAreNotSubsetOfGranted } public void testIsEmpty() { - assertTrue(new RoleDescriptor( - randomAlphaOfLengthBetween(1, 10), null, null, null, null, null, null, null) - .isEmpty()); - - assertTrue(new RoleDescriptor( - randomAlphaOfLengthBetween(1, 10), - new String[0], - new RoleDescriptor.IndicesPrivileges[0], - new RoleDescriptor.ApplicationResourcePrivileges[0], - new ConfigurableClusterPrivilege[0], - new String[0], - new HashMap<>(), - new HashMap<>()) - .isEmpty()); + assertTrue(new RoleDescriptor(randomAlphaOfLengthBetween(1, 10), null, null, null, null, null, null, null).isEmpty()); + + assertTrue( + new RoleDescriptor( + randomAlphaOfLengthBetween(1, 10), + new String[0], + new RoleDescriptor.IndicesPrivileges[0], + new RoleDescriptor.ApplicationResourcePrivileges[0], + new ConfigurableClusterPrivilege[0], + new String[0], + new HashMap<>(), + new HashMap<>() + ).isEmpty() + ); final List booleans = Arrays.asList( randomBoolean(), @@ -362,27 +407,28 @@ public void testIsEmpty() { randomBoolean(), randomBoolean(), randomBoolean(), - randomBoolean()); + randomBoolean() + ); final RoleDescriptor roleDescriptor = new RoleDescriptor( randomAlphaOfLengthBetween(1, 10), booleans.get(0) ? new String[0] : new String[] { "foo" }, - booleans.get(1) ? - new RoleDescriptor.IndicesPrivileges[0] : - new RoleDescriptor.IndicesPrivileges[] { + booleans.get(1) + ? new RoleDescriptor.IndicesPrivileges[0] + : new RoleDescriptor.IndicesPrivileges[] { RoleDescriptor.IndicesPrivileges.builder().indices("idx").privileges("foo").build() }, - booleans.get(2) ? - new RoleDescriptor.ApplicationResourcePrivileges[0] : - new RoleDescriptor.ApplicationResourcePrivileges[] { - RoleDescriptor.ApplicationResourcePrivileges.builder() - .application("app").privileges("foo").resources("res").build() }, - booleans.get(3) ? - new ConfigurableClusterPrivilege[0] : - new ConfigurableClusterPrivilege[] { + booleans.get(2) + ? new RoleDescriptor.ApplicationResourcePrivileges[0] + : new RoleDescriptor.ApplicationResourcePrivileges[] { + RoleDescriptor.ApplicationResourcePrivileges.builder().application("app").privileges("foo").resources("res").build() }, + booleans.get(3) + ? new ConfigurableClusterPrivilege[0] + : new ConfigurableClusterPrivilege[] { new ConfigurableClusterPrivileges.ManageApplicationPrivileges(Collections.singleton("foo")) }, booleans.get(4) ? new String[0] : new String[] { "foo" }, booleans.get(5) ? new HashMap<>() : Collections.singletonMap("foo", "bar"), - Collections.singletonMap("foo", "bar")); + Collections.singletonMap("foo", "bar") + ); if (booleans.stream().anyMatch(e -> e.equals(false))) { assertFalse(roleDescriptor.isEmpty()); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/accesscontrol/FieldDataCacheWithFieldSubsetReaderTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/accesscontrol/FieldDataCacheWithFieldSubsetReaderTests.java index 6b72a8bddbca9..14f0c798e454a 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/accesscontrol/FieldDataCacheWithFieldSubsetReaderTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/accesscontrol/FieldDataCacheWithFieldSubsetReaderTests.java @@ -58,12 +58,22 @@ public void setup() throws Exception { CircuitBreakerService circuitBreakerService = new NoneCircuitBreakerService(); String name = "_field"; indexFieldDataCache = new DummyAccountingFieldDataCache(); - sortedSetOrdinalsIndexFieldData = new SortedSetOrdinalsIndexFieldData(indexFieldDataCache, name, - CoreValuesSourceType.KEYWORD, circuitBreakerService, AbstractLeafOrdinalsFieldData.DEFAULT_SCRIPT_FUNCTION); - pagedBytesIndexFieldData = new PagedBytesIndexFieldData(name, CoreValuesSourceType.KEYWORD, indexFieldDataCache, - circuitBreakerService, TextFieldMapper.Defaults.FIELDDATA_MIN_FREQUENCY, - TextFieldMapper.Defaults.FIELDDATA_MAX_FREQUENCY, - TextFieldMapper.Defaults.FIELDDATA_MIN_SEGMENT_SIZE); + sortedSetOrdinalsIndexFieldData = new SortedSetOrdinalsIndexFieldData( + indexFieldDataCache, + name, + CoreValuesSourceType.KEYWORD, + circuitBreakerService, + AbstractLeafOrdinalsFieldData.DEFAULT_SCRIPT_FUNCTION + ); + pagedBytesIndexFieldData = new PagedBytesIndexFieldData( + name, + CoreValuesSourceType.KEYWORD, + indexFieldDataCache, + circuitBreakerService, + TextFieldMapper.Defaults.FIELDDATA_MIN_FREQUENCY, + TextFieldMapper.Defaults.FIELDDATA_MAX_FREQUENCY, + TextFieldMapper.Defaults.FIELDDATA_MIN_SEGMENT_SIZE + ); dir = newDirectory(); IndexWriterConfig iwc = new IndexWriterConfig(null); @@ -156,26 +166,24 @@ private static class DummyAccountingFieldDataCache implements IndexFieldDataCach @Override public > FD load(LeafReaderContext context, IFD indexFieldData) - throws Exception { + throws Exception { leafLevelBuilds++; return indexFieldData.loadDirect(context); } @Override @SuppressWarnings("unchecked") - public > IFD load(DirectoryReader indexReader, - IFD indexFieldData) throws Exception { + public > IFD load(DirectoryReader indexReader, IFD indexFieldData) + throws Exception { topLevelBuilds++; return (IFD) indexFieldData.loadGlobalDirect(indexReader); } @Override - public void clear() { - } + public void clear() {} @Override - public void clear(String fieldName) { - } + public void clear(String fieldName) {} } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/accesscontrol/FieldExtractorTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/accesscontrol/FieldExtractorTests.java index fc30c61cd0aee..0a4a399ace999 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/accesscontrol/FieldExtractorTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/accesscontrol/FieldExtractorTests.java @@ -47,10 +47,10 @@ public void testBoolean() { public void testDisjunctionMax() { Set fields = new HashSet<>(); - DisjunctionMaxQuery query = new DisjunctionMaxQuery(Arrays.asList( - new TermQuery(new Term("one", "bar")), - new TermQuery(new Term("two", "baz")) - ), 1.0F); + DisjunctionMaxQuery query = new DisjunctionMaxQuery( + Arrays.asList(new TermQuery(new Term("one", "bar")), new TermQuery(new Term("two", "baz"))), + 1.0F + ); FieldExtractor.extractFields(query, fields); assertEquals(asSet("one", "two"), fields); } @@ -69,10 +69,7 @@ public void testTerm() { public void testSynonym() { Set fields = new HashSet<>(); - SynonymQuery query = new SynonymQuery.Builder("foo") - .addTerm(new Term("foo", "bar")) - .addTerm(new Term("foo", "baz")) - .build(); + SynonymQuery query = new SynonymQuery.Builder("foo").addTerm(new Term("foo", "bar")).addTerm(new Term("foo", "baz")).build(); FieldExtractor.extractFields(query, fields); assertEquals(asSet("foo"), fields); } @@ -139,9 +136,10 @@ public void testMatchNoDocs() { public void testUnsupported() { Set fields = new HashSet<>(); - expectThrows(UnsupportedOperationException.class, () -> { - FieldExtractor.extractFields(new AssertingQuery(random(), new MatchAllDocsQuery()), fields); - }); + expectThrows( + UnsupportedOperationException.class, + () -> { FieldExtractor.extractFields(new AssertingQuery(random(), new MatchAllDocsQuery()), fields); } + ); } public void testIndexOrDocValuesQuery() { diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/accesscontrol/IndicesAccessControlTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/accesscontrol/IndicesAccessControlTests.java index 44b82c536e11c..f2c592f040f95 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/accesscontrol/IndicesAccessControlTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/accesscontrol/IndicesAccessControlTests.java @@ -33,7 +33,7 @@ public class IndicesAccessControlTests extends ESTestCase { public void testEmptyIndicesAccessControl() { IndicesAccessControl indicesAccessControl = new IndicesAccessControl(true, Collections.emptyMap()); assertTrue(indicesAccessControl.isGranted()); - assertNull(indicesAccessControl.getIndexPermissions(randomAlphaOfLengthBetween(3,20))); + assertNull(indicesAccessControl.getIndexPermissions(randomAlphaOfLengthBetween(3, 20))); } public void testSLimitedIndicesAccessControl() { @@ -65,17 +65,23 @@ public void testSLimitedIndicesAccessControl() { assertThat(result.isGranted(), is(false)); assertThat(result.getIndexPermissions("_index"), is(nullValue())); - indicesAccessControl = new IndicesAccessControl(true, - Collections.singletonMap("_index", new IndexAccessControl(true, new FieldPermissions(), DocumentPermissions.allowAll()))); + indicesAccessControl = new IndicesAccessControl( + true, + Collections.singletonMap("_index", new IndexAccessControl(true, new FieldPermissions(), DocumentPermissions.allowAll())) + ); limitedByIndicesAccessControl = new IndicesAccessControl(true, Collections.emptyMap()); result = indicesAccessControl.limitIndicesAccessControl(limitedByIndicesAccessControl); assertThat(result, is(notNullValue())); assertThat(result.getIndexPermissions("_index"), is(nullValue())); - indicesAccessControl = new IndicesAccessControl(true, - Collections.singletonMap("_index", new IndexAccessControl(true, new FieldPermissions(), DocumentPermissions.allowAll()))); - limitedByIndicesAccessControl = new IndicesAccessControl(true, - Collections.singletonMap("_index", new IndexAccessControl(true, new FieldPermissions(), DocumentPermissions.allowAll()))); + indicesAccessControl = new IndicesAccessControl( + true, + Collections.singletonMap("_index", new IndexAccessControl(true, new FieldPermissions(), DocumentPermissions.allowAll())) + ); + limitedByIndicesAccessControl = new IndicesAccessControl( + true, + Collections.singletonMap("_index", new IndexAccessControl(true, new FieldPermissions(), DocumentPermissions.allowAll())) + ); result = indicesAccessControl.limitIndicesAccessControl(limitedByIndicesAccessControl); assertThat(result, is(notNullValue())); assertThat(result.getIndexPermissions("_index"), is(notNullValue())); @@ -84,13 +90,19 @@ public void testSLimitedIndicesAccessControl() { assertThat(result.getIndexPermissions("_index").getDocumentPermissions().hasDocumentLevelPermissions(), is(false)); final FieldPermissions fieldPermissions1 = new FieldPermissions( - new FieldPermissionsDefinition(new String[] { "f1", "f2", "f3*" }, new String[] { "f3" })); + new FieldPermissionsDefinition(new String[] { "f1", "f2", "f3*" }, new String[] { "f3" }) + ); final FieldPermissions fieldPermissions2 = new FieldPermissions( - new FieldPermissionsDefinition(new String[] { "f1", "f3*", "f4" }, new String[] { "f3" })); - indicesAccessControl = new IndicesAccessControl(true, - Collections.singletonMap("_index", new IndexAccessControl(true, fieldPermissions1, DocumentPermissions.allowAll()))); - limitedByIndicesAccessControl = new IndicesAccessControl(true, - Collections.singletonMap("_index", new IndexAccessControl(true, fieldPermissions2, DocumentPermissions.allowAll()))); + new FieldPermissionsDefinition(new String[] { "f1", "f3*", "f4" }, new String[] { "f3" }) + ); + indicesAccessControl = new IndicesAccessControl( + true, + Collections.singletonMap("_index", new IndexAccessControl(true, fieldPermissions1, DocumentPermissions.allowAll())) + ); + limitedByIndicesAccessControl = new IndicesAccessControl( + true, + Collections.singletonMap("_index", new IndexAccessControl(true, fieldPermissions2, DocumentPermissions.allowAll())) + ); result = indicesAccessControl.limitIndicesAccessControl(limitedByIndicesAccessControl); assertThat(result, is(notNullValue())); assertThat(result.getIndexPermissions("_index"), is(notNullValue())); @@ -105,16 +117,19 @@ public void testSLimitedIndicesAccessControl() { assertThat(resultFieldPermissions.grantsAccessTo("f4"), is(false)); Set queries = Collections.singleton(new BytesArray("{\"match_all\" : {}}")); - final DocumentPermissions documentPermissions = DocumentPermissions - .filteredBy(queries); + final DocumentPermissions documentPermissions = DocumentPermissions.filteredBy(queries); assertThat(documentPermissions, is(notNullValue())); assertThat(documentPermissions.hasDocumentLevelPermissions(), is(true)); assertThat(documentPermissions.getQueries(), equalTo(queries)); - indicesAccessControl = new IndicesAccessControl(true, - Collections.singletonMap("_index", new IndexAccessControl(true, new FieldPermissions(), DocumentPermissions.allowAll()))); - limitedByIndicesAccessControl = new IndicesAccessControl(true, - Collections.singletonMap("_index", new IndexAccessControl(true, new FieldPermissions(), documentPermissions))); + indicesAccessControl = new IndicesAccessControl( + true, + Collections.singletonMap("_index", new IndexAccessControl(true, new FieldPermissions(), DocumentPermissions.allowAll())) + ); + limitedByIndicesAccessControl = new IndicesAccessControl( + true, + Collections.singletonMap("_index", new IndexAccessControl(true, new FieldPermissions(), documentPermissions)) + ); result = indicesAccessControl.limitIndicesAccessControl(limitedByIndicesAccessControl); assertThat(result, is(notNullValue())); assertThat(result.getIndexPermissions("_index"), is(notNullValue())); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/accesscontrol/IndicesPermissionTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/accesscontrol/IndicesPermissionTests.java index 85fb0eb66147b..46fca97b9d666 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/accesscontrol/IndicesPermissionTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/accesscontrol/IndicesPermissionTests.java @@ -56,19 +56,20 @@ public class IndicesPermissionTests extends ESTestCase { public void testAuthorize() { IndexMetadata.Builder imbBuilder = IndexMetadata.builder("_index") - .settings(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - ) - .putAlias(AliasMetadata.builder("_alias")); + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + ) + .putAlias(AliasMetadata.builder("_alias")); Metadata md = Metadata.builder().put(imbBuilder).build(); FieldPermissionsCache fieldPermissionsCache = new FieldPermissionsCache(Settings.EMPTY); SortedMap lookup = md.getIndicesLookup(); // basics: Set query = Collections.singleton(new BytesArray("{}")); - String[] fields = new String[]{"_field"}; + String[] fields = new String[] { "_field" }; Role role = Role.builder(RESTRICTED_INDICES_AUTOMATON, "_role") .add(new FieldPermissions(fieldPermissionDef(fields, null)), query, IndexPrivilege.ALL, randomBoolean(), "_index") .build(); @@ -104,8 +105,8 @@ public void testAuthorize() { // index group associated with an alias: role = Role.builder(RESTRICTED_INDICES_AUTOMATON, "_role") - .add(new FieldPermissions(fieldPermissionDef(fields, null)), query, IndexPrivilege.ALL, randomBoolean(), "_alias") - .build(); + .add(new FieldPermissions(fieldPermissionDef(fields, null)), query, IndexPrivilege.ALL, randomBoolean(), "_alias") + .build(); permissions = role.authorize(SearchAction.NAME, Sets.newHashSet("_alias"), lookup, fieldPermissionsCache); assertThat(permissions.getIndexPermissions("_index"), notNullValue()); assertTrue(permissions.getIndexPermissions("_index").getFieldPermissions().grantsAccessTo("_field")); @@ -122,8 +123,11 @@ public void testAuthorize() { assertThat(permissions.getIndexPermissions("_alias").getDocumentPermissions().getQueries(), equalTo(query)); // match all fields - String[] allFields = randomFrom(new String[]{"*"}, new String[]{"foo", "*"}, - new String[]{randomAlphaOfLengthBetween(1, 10), "*"}); + String[] allFields = randomFrom( + new String[] { "*" }, + new String[] { "foo", "*" }, + new String[] { randomAlphaOfLengthBetween(1, 10), "*" } + ); role = Role.builder(RESTRICTED_INDICES_AUTOMATON, "_role") .add(new FieldPermissions(fieldPermissionDef(allFields, null)), query, IndexPrivilege.ALL, randomBoolean(), "_alias") .build(); @@ -141,19 +145,19 @@ public void testAuthorize() { assertThat(permissions.getIndexPermissions("_alias").getDocumentPermissions().getQueries(), equalTo(query)); IndexMetadata.Builder imbBuilder1 = IndexMetadata.builder("_index_1") - .settings(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - ) - .putAlias(AliasMetadata.builder("_alias")); + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + ) + .putAlias(AliasMetadata.builder("_alias")); md = Metadata.builder(md).put(imbBuilder1).build(); lookup = md.getIndicesLookup(); // match all fields with more than one permission Set fooQuery = Collections.singleton(new BytesArray("{foo}")); - allFields = randomFrom(new String[]{"*"}, new String[]{"foo", "*"}, - new String[]{randomAlphaOfLengthBetween(1, 10), "*"}); + allFields = randomFrom(new String[] { "*" }, new String[] { "foo", "*" }, new String[] { randomAlphaOfLengthBetween(1, 10), "*" }); role = Role.builder(RESTRICTED_INDICES_AUTOMATON, "_role") .add(new FieldPermissions(fieldPermissionDef(allFields, null)), fooQuery, IndexPrivilege.ALL, randomBoolean(), "_alias") .add(new FieldPermissions(fieldPermissionDef(allFields, null)), query, IndexPrivilege.ALL, randomBoolean(), "_alias") @@ -182,22 +186,23 @@ public void testAuthorize() { public void testAuthorizeMultipleGroupsMixedDls() { IndexMetadata.Builder imbBuilder = IndexMetadata.builder("_index") - .settings(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - ) - .putAlias(AliasMetadata.builder("_alias")); + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + ) + .putAlias(AliasMetadata.builder("_alias")); Metadata md = Metadata.builder().put(imbBuilder).build(); FieldPermissionsCache fieldPermissionsCache = new FieldPermissionsCache(Settings.EMPTY); SortedMap lookup = md.getIndicesLookup(); Set query = Collections.singleton(new BytesArray("{}")); - String[] fields = new String[]{"_field"}; + String[] fields = new String[] { "_field" }; Role role = Role.builder(RESTRICTED_INDICES_AUTOMATON, "_role") - .add(new FieldPermissions(fieldPermissionDef(fields, null)), query, IndexPrivilege.ALL, randomBoolean(), "_index") - .add(new FieldPermissions(fieldPermissionDef(null, null)), null, IndexPrivilege.ALL, randomBoolean(), "*") - .build(); + .add(new FieldPermissions(fieldPermissionDef(fields, null)), query, IndexPrivilege.ALL, randomBoolean(), "_index") + .add(new FieldPermissions(fieldPermissionDef(null, null)), null, IndexPrivilege.ALL, randomBoolean(), "*") + .build(); IndicesAccessControl permissions = role.authorize(SearchAction.NAME, Sets.newHashSet("_index"), lookup, fieldPermissionsCache); assertThat(permissions.getIndexPermissions("_index"), notNullValue()); assertTrue(permissions.getIndexPermissions("_index").getFieldPermissions().grantsAccessTo("_field")); @@ -207,9 +212,11 @@ public void testAuthorizeMultipleGroupsMixedDls() { public void testIndicesPrivilegesStreaming() throws IOException { BytesStreamOutput out = new BytesStreamOutput(); - String[] allowed = new String[]{randomAlphaOfLength(5) + "*", randomAlphaOfLength(5) + "*", randomAlphaOfLength(5) + "*"}; - String[] denied = new String[]{allowed[0] + randomAlphaOfLength(5), allowed[1] + randomAlphaOfLength(5), - allowed[2] + randomAlphaOfLength(5)}; + String[] allowed = new String[] { randomAlphaOfLength(5) + "*", randomAlphaOfLength(5) + "*", randomAlphaOfLength(5) + "*" }; + String[] denied = new String[] { + allowed[0] + randomAlphaOfLength(5), + allowed[1] + randomAlphaOfLength(5), + allowed[2] + randomAlphaOfLength(5) }; RoleDescriptor.IndicesPrivileges.Builder indicesPrivileges = RoleDescriptor.IndicesPrivileges.builder(); indicesPrivileges.grantedFields(allowed); indicesPrivileges.deniedFields(denied); @@ -241,24 +248,29 @@ public void testIndicesPrivilegesStreaming() throws IOException { // tests that field permissions are merged correctly when we authorize with several groups and don't crash when an index has no group public void testCorePermissionAuthorize() { final Settings indexSettings = Settings.builder().put("index.version.created", Version.CURRENT).build(); - final Metadata metadata = new Metadata.Builder() - .put(new IndexMetadata.Builder("a1").settings(indexSettings).numberOfShards(1).numberOfReplicas(0).build(), true) - .put(new IndexMetadata.Builder("a2").settings(indexSettings).numberOfShards(1).numberOfReplicas(0).build(), true) - .build(); + final Metadata metadata = new Metadata.Builder().put( + new IndexMetadata.Builder("a1").settings(indexSettings).numberOfShards(1).numberOfReplicas(0).build(), + true + ).put(new IndexMetadata.Builder("a2").settings(indexSettings).numberOfShards(1).numberOfReplicas(0).build(), true).build(); SortedMap lookup = metadata.getIndicesLookup(); FieldPermissionsCache fieldPermissionsCache = new FieldPermissionsCache(Settings.EMPTY); - IndicesPermission core = new IndicesPermission.Builder(RESTRICTED_INDICES_AUTOMATON) - .addGroup(IndexPrivilege.ALL, new FieldPermissions(), null, randomBoolean(), "a1") + IndicesPermission core = new IndicesPermission.Builder(RESTRICTED_INDICES_AUTOMATON).addGroup( + IndexPrivilege.ALL, + new FieldPermissions(), + null, + randomBoolean(), + "a1" + ) .addGroup( IndexPrivilege.READ, - new FieldPermissions(fieldPermissionDef(null, new String[]{"denied_field"})), + new FieldPermissions(fieldPermissionDef(null, new String[] { "denied_field" })), null, randomBoolean(), - "a1") + "a1" + ) .build(); - IndicesAccessControl iac = - core.authorize(SearchAction.NAME, Sets.newHashSet("a1", "ba"), lookup, fieldPermissionsCache); + IndicesAccessControl iac = core.authorize(SearchAction.NAME, Sets.newHashSet("a1", "ba"), lookup, fieldPermissionsCache); assertTrue(iac.getIndexPermissions("a1").getFieldPermissions().grantsAccessTo("denied_field")); assertTrue(iac.getIndexPermissions("a1").getFieldPermissions().grantsAccessTo(randomAlphaOfLength(5))); // did not define anything for ba so we allow all @@ -270,26 +282,34 @@ public void testCorePermissionAuthorize() { assertFalse(core.check("unknown")); // test with two indices - core = new IndicesPermission.Builder(RESTRICTED_INDICES_AUTOMATON) - .addGroup(IndexPrivilege.ALL, new FieldPermissions(), null, randomBoolean(), "a1") + core = new IndicesPermission.Builder(RESTRICTED_INDICES_AUTOMATON).addGroup( + IndexPrivilege.ALL, + new FieldPermissions(), + null, + randomBoolean(), + "a1" + ) .addGroup( IndexPrivilege.ALL, - new FieldPermissions(fieldPermissionDef(null, new String[]{"denied_field"})), + new FieldPermissions(fieldPermissionDef(null, new String[] { "denied_field" })), null, randomBoolean(), - "a1") + "a1" + ) .addGroup( IndexPrivilege.ALL, new FieldPermissions(fieldPermissionDef(new String[] { "*_field" }, new String[] { "denied_field" })), null, randomBoolean(), - "a2") + "a2" + ) .addGroup( IndexPrivilege.ALL, new FieldPermissions(fieldPermissionDef(new String[] { "*_field2" }, new String[] { "denied_field2" })), null, randomBoolean(), - "a2") + "a2" + ) .build(); iac = core.authorize(SearchAction.NAME, Sets.newHashSet("a1", "a2"), lookup, fieldPermissionsCache); assertFalse(iac.getIndexPermissions("a1").getFieldPermissions().hasFieldLevelSecurity()); @@ -312,45 +332,69 @@ public void testErrorMessageIfIndexPatternIsTooComplex() { String suffixBegin = randomAlphaOfLengthBetween(12, 36); indices.add("*" + prefix + "*" + suffixBegin + "*"); } - final ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, - () -> new IndicesPermission.Group(IndexPrivilege.ALL, new FieldPermissions(), null, randomBoolean(), - RESTRICTED_INDICES_AUTOMATON, indices.toArray(Strings.EMPTY_ARRAY))); + final ElasticsearchSecurityException e = expectThrows( + ElasticsearchSecurityException.class, + () -> new IndicesPermission.Group( + IndexPrivilege.ALL, + new FieldPermissions(), + null, + randomBoolean(), + RESTRICTED_INDICES_AUTOMATON, + indices.toArray(Strings.EMPTY_ARRAY) + ) + ); assertThat(e.getMessage(), containsString(indices.get(0))); assertThat(e.getMessage(), containsString("too complex to evaluate")); } public void testSecurityIndicesPermissions() { final Settings indexSettings = Settings.builder().put("index.version.created", Version.CURRENT).build(); - final String internalSecurityIndex = randomFrom(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_6, - RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7); - final Metadata metadata = new Metadata.Builder() - .put(new IndexMetadata.Builder(internalSecurityIndex) - .settings(indexSettings) - .numberOfShards(1) - .numberOfReplicas(0) - .putAlias(new AliasMetadata.Builder(RestrictedIndicesNames.SECURITY_MAIN_ALIAS).build()) - .build(), true) - .build(); + final String internalSecurityIndex = randomFrom( + RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_6, + RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7 + ); + final Metadata metadata = new Metadata.Builder().put( + new IndexMetadata.Builder(internalSecurityIndex).settings(indexSettings) + .numberOfShards(1) + .numberOfReplicas(0) + .putAlias(new AliasMetadata.Builder(RestrictedIndicesNames.SECURITY_MAIN_ALIAS).build()) + .build(), + true + ).build(); FieldPermissionsCache fieldPermissionsCache = new FieldPermissionsCache(Settings.EMPTY); SortedMap lookup = metadata.getIndicesLookup(); // allow_restricted_indices: false - IndicesPermission indicesPermission = new IndicesPermission.Builder(RESTRICTED_INDICES_AUTOMATON) - .addGroup(IndexPrivilege.ALL, new FieldPermissions(), null, false, "*") - .build(); - IndicesAccessControl iac = indicesPermission.authorize(SearchAction.NAME, - Sets.newHashSet(internalSecurityIndex, RestrictedIndicesNames.SECURITY_MAIN_ALIAS), lookup, - fieldPermissionsCache); + IndicesPermission indicesPermission = new IndicesPermission.Builder(RESTRICTED_INDICES_AUTOMATON).addGroup( + IndexPrivilege.ALL, + new FieldPermissions(), + null, + false, + "*" + ).build(); + IndicesAccessControl iac = indicesPermission.authorize( + SearchAction.NAME, + Sets.newHashSet(internalSecurityIndex, RestrictedIndicesNames.SECURITY_MAIN_ALIAS), + lookup, + fieldPermissionsCache + ); assertThat(iac.getIndexPermissions(internalSecurityIndex).isGranted(), is(false)); assertThat(iac.getIndexPermissions(RestrictedIndicesNames.SECURITY_MAIN_ALIAS).isGranted(), is(false)); // allow_restricted_indices: true - indicesPermission = new IndicesPermission.Builder(RESTRICTED_INDICES_AUTOMATON) - .addGroup(IndexPrivilege.ALL, new FieldPermissions(), null, true, "*") - .build(); - iac = indicesPermission.authorize(SearchAction.NAME, - Sets.newHashSet(internalSecurityIndex, RestrictedIndicesNames.SECURITY_MAIN_ALIAS), lookup, - fieldPermissionsCache); + indicesPermission = new IndicesPermission.Builder(RESTRICTED_INDICES_AUTOMATON).addGroup( + IndexPrivilege.ALL, + new FieldPermissions(), + null, + true, + "*" + ).build(); + iac = indicesPermission.authorize( + SearchAction.NAME, + Sets.newHashSet(internalSecurityIndex, RestrictedIndicesNames.SECURITY_MAIN_ALIAS), + lookup, + fieldPermissionsCache + ); assertThat(iac.getIndexPermissions(internalSecurityIndex).isGranted(), is(true)); assertThat(iac.getIndexPermissions(RestrictedIndicesNames.SECURITY_MAIN_ALIAS).isGranted(), is(true)); } @@ -358,30 +402,38 @@ public void testSecurityIndicesPermissions() { public void testAsyncSearchIndicesPermissions() { final Settings indexSettings = Settings.builder().put("index.version.created", Version.CURRENT).build(); final String asyncSearchIndex = XPackPlugin.ASYNC_RESULTS_INDEX + randomAlphaOfLengthBetween(0, 2); - final Metadata metadata = new Metadata.Builder() - .put(new IndexMetadata.Builder(asyncSearchIndex) - .settings(indexSettings) - .numberOfShards(1) - .numberOfReplicas(0) - .build(), true) - .build(); + final Metadata metadata = new Metadata.Builder().put( + new IndexMetadata.Builder(asyncSearchIndex).settings(indexSettings).numberOfShards(1).numberOfReplicas(0).build(), + true + ).build(); FieldPermissionsCache fieldPermissionsCache = new FieldPermissionsCache(Settings.EMPTY); SortedMap lookup = metadata.getIndicesLookup(); // allow_restricted_indices: false - IndicesPermission indicesPermission = new IndicesPermission.Builder(RESTRICTED_INDICES_AUTOMATON) - .addGroup(IndexPrivilege.ALL, new FieldPermissions(), null, false, "*") - .build(); - IndicesAccessControl iac = indicesPermission.authorize(SearchAction.NAME, - Sets.newHashSet(asyncSearchIndex), lookup, fieldPermissionsCache); + IndicesPermission indicesPermission = new IndicesPermission.Builder(RESTRICTED_INDICES_AUTOMATON).addGroup( + IndexPrivilege.ALL, + new FieldPermissions(), + null, + false, + "*" + ).build(); + IndicesAccessControl iac = indicesPermission.authorize( + SearchAction.NAME, + Sets.newHashSet(asyncSearchIndex), + lookup, + fieldPermissionsCache + ); assertThat(iac.getIndexPermissions(asyncSearchIndex).isGranted(), is(false)); // allow_restricted_indices: true - indicesPermission = new IndicesPermission.Builder(RESTRICTED_INDICES_AUTOMATON) - .addGroup(IndexPrivilege.ALL, new FieldPermissions(), null, true, "*") - .build(); - iac = indicesPermission.authorize(SearchAction.NAME, - Sets.newHashSet(asyncSearchIndex), lookup, fieldPermissionsCache); + indicesPermission = new IndicesPermission.Builder(RESTRICTED_INDICES_AUTOMATON).addGroup( + IndexPrivilege.ALL, + new FieldPermissions(), + null, + true, + "*" + ).build(); + iac = indicesPermission.authorize(SearchAction.NAME, Sets.newHashSet(asyncSearchIndex), lookup, fieldPermissionsCache); assertThat(iac.getIndexPermissions(asyncSearchIndex).isGranted(), is(true)); } @@ -393,8 +445,11 @@ public void testAuthorizationForBackingIndices() { for (int backingIndexNumber = 1; backingIndexNumber <= numBackingIndices; backingIndexNumber++) { backingIndices.add(createIndexMetadata(DataStream.getDefaultBackingIndexName(dataStreamName, backingIndexNumber))); } - DataStream ds = new DataStream(dataStreamName, createTimestampField("@timestamp"), - backingIndices.stream().map(IndexMetadata::getIndex).collect(Collectors.toList())); + DataStream ds = new DataStream( + dataStreamName, + createTimestampField("@timestamp"), + backingIndices.stream().map(IndexMetadata::getIndex).collect(Collectors.toList()) + ); builder.put(ds); for (IndexMetadata index : backingIndices) { builder.put(index, false); @@ -403,27 +458,37 @@ public void testAuthorizationForBackingIndices() { FieldPermissionsCache fieldPermissionsCache = new FieldPermissionsCache(Settings.EMPTY); SortedMap lookup = metadata.getIndicesLookup(); - IndicesPermission indicesPermission = new IndicesPermission.Builder(RESTRICTED_INDICES_AUTOMATON) - .addGroup(IndexPrivilege.READ, new FieldPermissions(), null, false, dataStreamName) - .build(); + IndicesPermission indicesPermission = new IndicesPermission.Builder(RESTRICTED_INDICES_AUTOMATON).addGroup( + IndexPrivilege.READ, + new FieldPermissions(), + null, + false, + dataStreamName + ).build(); IndicesAccessControl iac = indicesPermission.authorize( - SearchAction.NAME, - Sets.newHashSet(backingIndices.stream().map(im -> im.getIndex().getName()).collect(Collectors.toList())), - lookup, - fieldPermissionsCache); + SearchAction.NAME, + Sets.newHashSet(backingIndices.stream().map(im -> im.getIndex().getName()).collect(Collectors.toList())), + lookup, + fieldPermissionsCache + ); for (IndexMetadata im : backingIndices) { assertThat(iac.getIndexPermissions(im.getIndex().getName()).isGranted(), is(true)); } - indicesPermission = new IndicesPermission.Builder(RESTRICTED_INDICES_AUTOMATON) - .addGroup(IndexPrivilege.CREATE_DOC, new FieldPermissions(), null, false, dataStreamName) - .build(); + indicesPermission = new IndicesPermission.Builder(RESTRICTED_INDICES_AUTOMATON).addGroup( + IndexPrivilege.CREATE_DOC, + new FieldPermissions(), + null, + false, + dataStreamName + ).build(); iac = indicesPermission.authorize( - randomFrom(PutMappingAction.NAME, AutoPutMappingAction.NAME), - Sets.newHashSet(backingIndices.stream().map(im -> im.getIndex().getName()).collect(Collectors.toList())), - lookup, - fieldPermissionsCache); + randomFrom(PutMappingAction.NAME, AutoPutMappingAction.NAME), + Sets.newHashSet(backingIndices.stream().map(im -> im.getIndex().getName()).collect(Collectors.toList())), + lookup, + fieldPermissionsCache + ); for (IndexMetadata im : backingIndices) { assertThat(iac.getIndexPermissions(im.getIndex().getName()).isGranted(), is(false)); @@ -432,17 +497,21 @@ public void testAuthorizationForBackingIndices() { public void testAuthorizationForMappingUpdates() { final Settings indexSettings = Settings.builder().put("index.version.created", Version.CURRENT).build(); - final Metadata.Builder metadata = new Metadata.Builder() - .put(new IndexMetadata.Builder("test1").settings(indexSettings).numberOfShards(1).numberOfReplicas(0).build(), true) - .put(new IndexMetadata.Builder("test_write1").settings(indexSettings).numberOfShards(1).numberOfReplicas(0).build(), true); + final Metadata.Builder metadata = new Metadata.Builder().put( + new IndexMetadata.Builder("test1").settings(indexSettings).numberOfShards(1).numberOfReplicas(0).build(), + true + ).put(new IndexMetadata.Builder("test_write1").settings(indexSettings).numberOfShards(1).numberOfReplicas(0).build(), true); int numBackingIndices = randomIntBetween(1, 3); List backingIndices = new ArrayList<>(); for (int backingIndexNumber = 1; backingIndexNumber <= numBackingIndices; backingIndexNumber++) { backingIndices.add(createIndexMetadata(DataStream.getDefaultBackingIndexName("test_write2", backingIndexNumber))); } - DataStream ds = new DataStream("test_write2", createTimestampField("@timestamp"), - backingIndices.stream().map(IndexMetadata::getIndex).collect(Collectors.toList())); + DataStream ds = new DataStream( + "test_write2", + createTimestampField("@timestamp"), + backingIndices.stream().map(IndexMetadata::getIndex).collect(Collectors.toList()) + ); metadata.put(ds); for (IndexMetadata index : backingIndices) { metadata.put(index, false); @@ -451,53 +520,76 @@ public void testAuthorizationForMappingUpdates() { SortedMap lookup = metadata.build().getIndicesLookup(); FieldPermissionsCache fieldPermissionsCache = new FieldPermissionsCache(Settings.EMPTY); - IndicesPermission core = new IndicesPermission.Builder(RESTRICTED_INDICES_AUTOMATON) - .addGroup(IndexPrivilege.INDEX, new FieldPermissions(), null, randomBoolean(), "test*") + IndicesPermission core = new IndicesPermission.Builder(RESTRICTED_INDICES_AUTOMATON).addGroup( + IndexPrivilege.INDEX, + new FieldPermissions(), + null, + randomBoolean(), + "test*" + ) .addGroup( IndexPrivilege.WRITE, - new FieldPermissions(fieldPermissionDef(null, new String[]{"denied_field"})), + new FieldPermissions(fieldPermissionDef(null, new String[] { "denied_field" })), null, randomBoolean(), - "test_write*") + "test_write*" + ) .build(); - IndicesAccessControl iac = - core.authorize(PutMappingAction.NAME, Sets.newHashSet("test1", "test_write1"), lookup, fieldPermissionsCache); + IndicesAccessControl iac = core.authorize( + PutMappingAction.NAME, + Sets.newHashSet("test1", "test_write1"), + lookup, + fieldPermissionsCache + ); assertThat(iac.getIndexPermissions("test1").isGranted(), is(true)); assertThat(iac.getIndexPermissions("test_write1").isGranted(), is(true)); - assertWarnings("the index privilege [index] allowed the update mapping action [" + PutMappingAction.NAME + "] on " + - "index [test1], this privilege will not permit mapping updates in the next major release - " + - "users who require access to update mappings must be granted explicit privileges", - "the index privilege [index] allowed the update mapping action [" + PutMappingAction.NAME + "] on " + - "index [test_write1], this privilege will not permit mapping updates in the next major release - " + - "users who require access to update mappings must be granted explicit privileges", - "the index privilege [write] allowed the update mapping action [" + PutMappingAction.NAME + "] on " + - "index [test_write1], this privilege will not permit mapping updates in the next major release - " + - "users who require access to update mappings must be granted explicit privileges" + assertWarnings( + "the index privilege [index] allowed the update mapping action [" + + PutMappingAction.NAME + + "] on " + + "index [test1], this privilege will not permit mapping updates in the next major release - " + + "users who require access to update mappings must be granted explicit privileges", + "the index privilege [index] allowed the update mapping action [" + + PutMappingAction.NAME + + "] on " + + "index [test_write1], this privilege will not permit mapping updates in the next major release - " + + "users who require access to update mappings must be granted explicit privileges", + "the index privilege [write] allowed the update mapping action [" + + PutMappingAction.NAME + + "] on " + + "index [test_write1], this privilege will not permit mapping updates in the next major release - " + + "users who require access to update mappings must be granted explicit privileges" ); iac = core.authorize(AutoPutMappingAction.NAME, Sets.newHashSet("test1", "test_write1"), lookup, fieldPermissionsCache); assertThat(iac.getIndexPermissions("test1").isGranted(), is(true)); assertThat(iac.getIndexPermissions("test_write1").isGranted(), is(true)); - assertWarnings("the index privilege [index] allowed the update mapping action [" + AutoPutMappingAction.NAME + "] on " + - "index [test1], this privilege will not permit mapping updates in the next major release - " + - "users who require access to update mappings must be granted explicit privileges"); + assertWarnings( + "the index privilege [index] allowed the update mapping action [" + + AutoPutMappingAction.NAME + + "] on " + + "index [test1], this privilege will not permit mapping updates in the next major release - " + + "users who require access to update mappings must be granted explicit privileges" + ); iac = core.authorize(AutoPutMappingAction.NAME, Sets.newHashSet("test_write2"), lookup, fieldPermissionsCache); assertThat(iac.getIndexPermissions("test_write2").isGranted(), is(true)); iac = core.authorize(PutMappingAction.NAME, Sets.newHashSet("test_write2"), lookup, fieldPermissionsCache); assertThat(iac.getIndexPermissions("test_write2").isGranted(), is(false)); iac = core.authorize( - AutoPutMappingAction.NAME, - Sets.newHashSet(backingIndices.stream().map(im -> im.getIndex().getName()).collect(Collectors.toList())), - lookup, - fieldPermissionsCache); + AutoPutMappingAction.NAME, + Sets.newHashSet(backingIndices.stream().map(im -> im.getIndex().getName()).collect(Collectors.toList())), + lookup, + fieldPermissionsCache + ); for (IndexMetadata im : backingIndices) { assertThat(iac.getIndexPermissions(im.getIndex().getName()).isGranted(), is(true)); } iac = core.authorize( - PutMappingAction.NAME, - Sets.newHashSet(backingIndices.stream().map(im -> im.getIndex().getName()).collect(Collectors.toList())), - lookup, - fieldPermissionsCache); + PutMappingAction.NAME, + Sets.newHashSet(backingIndices.stream().map(im -> im.getIndex().getName()).collect(Collectors.toList())), + lookup, + fieldPermissionsCache + ); for (IndexMetadata im : backingIndices) { assertThat(iac.getIndexPermissions(im.getIndex().getName()).isGranted(), is(false)); } @@ -505,9 +597,9 @@ public void testAuthorizationForMappingUpdates() { public void testIndicesPermissionHasFieldOrDocumentLevelSecurity() { // Make sure we have at least one of fieldPermissions and documentPermission - final FieldPermissions fieldPermissions = randomBoolean() ? - new FieldPermissions(new FieldPermissionsDefinition(Strings.EMPTY_ARRAY, Strings.EMPTY_ARRAY)) : - FieldPermissions.DEFAULT; + final FieldPermissions fieldPermissions = randomBoolean() + ? new FieldPermissions(new FieldPermissionsDefinition(Strings.EMPTY_ARRAY, Strings.EMPTY_ARRAY)) + : FieldPermissions.DEFAULT; final Set queries; if (fieldPermissions == FieldPermissions.DEFAULT) { queries = Set.of(new BytesArray("a query")); @@ -515,22 +607,33 @@ public void testIndicesPermissionHasFieldOrDocumentLevelSecurity() { queries = randomBoolean() ? Set.of(new BytesArray("a query")) : null; } - final IndicesPermission indicesPermission1 = new IndicesPermission.Builder(RESTRICTED_INDICES_AUTOMATON) - .addGroup(IndexPrivilege.ALL, fieldPermissions, queries, randomBoolean(), "*") - .build(); + final IndicesPermission indicesPermission1 = new IndicesPermission.Builder(RESTRICTED_INDICES_AUTOMATON).addGroup( + IndexPrivilege.ALL, + fieldPermissions, + queries, + randomBoolean(), + "*" + ).build(); assertThat(indicesPermission1.hasFieldOrDocumentLevelSecurity(), is(true)); // IsTotal means no DLS/FLS - final IndicesPermission indicesPermission2 = new IndicesPermission.Builder(RESTRICTED_INDICES_AUTOMATON) - .addGroup(IndexPrivilege.ALL, FieldPermissions.DEFAULT, null, true, "*") - .build(); + final IndicesPermission indicesPermission2 = new IndicesPermission.Builder(RESTRICTED_INDICES_AUTOMATON).addGroup( + IndexPrivilege.ALL, + FieldPermissions.DEFAULT, + null, + true, + "*" + ).build(); assertThat(indicesPermission2.hasFieldOrDocumentLevelSecurity(), is(false)); // IsTotal means NO DLS/FLS even when there is another group that has DLS/FLS - final IndicesPermission indicesPermission3 = new IndicesPermission.Builder(RESTRICTED_INDICES_AUTOMATON) - .addGroup(IndexPrivilege.ALL, FieldPermissions.DEFAULT, null, true, "*") - .addGroup(IndexPrivilege.NONE, fieldPermissions, queries, randomBoolean(), "*") - .build(); + final IndicesPermission indicesPermission3 = new IndicesPermission.Builder(RESTRICTED_INDICES_AUTOMATON).addGroup( + IndexPrivilege.ALL, + FieldPermissions.DEFAULT, + null, + true, + "*" + ).addGroup(IndexPrivilege.NONE, fieldPermissions, queries, randomBoolean(), "*").build(); assertThat(indicesPermission3.hasFieldOrDocumentLevelSecurity(), is(false)); } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/accesscontrol/OptOutQueryCacheTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/accesscontrol/OptOutQueryCacheTests.java index 8975e72fe440d..4b22cf5975b1b 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/accesscontrol/OptOutQueryCacheTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/accesscontrol/OptOutQueryCacheTests.java @@ -70,65 +70,104 @@ public void testOptOutQueryCacheSafetyCheck() throws IOException { Weight weight = builder.build().createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1f); // whenever the allowed fields match the fields in the query and we do not deny access to any fields we allow caching. - IndicesAccessControl.IndexAccessControl permissions = new IndicesAccessControl.IndexAccessControl(true, - new FieldPermissions(fieldPermissionDef(new String[]{"foo", "no"}, null)), DocumentPermissions.allowAll()); + IndicesAccessControl.IndexAccessControl permissions = new IndicesAccessControl.IndexAccessControl( + true, + new FieldPermissions(fieldPermissionDef(new String[] { "foo", "no" }, null)), + DocumentPermissions.allowAll() + ); assertTrue(OptOutQueryCache.cachingIsSafe(weight, permissions)); - permissions = new IndicesAccessControl.IndexAccessControl(true, - new FieldPermissions(fieldPermissionDef(new String[]{"foo", "no"}, new String[]{})), DocumentPermissions.allowAll()); + permissions = new IndicesAccessControl.IndexAccessControl( + true, + new FieldPermissions(fieldPermissionDef(new String[] { "foo", "no" }, new String[] {})), + DocumentPermissions.allowAll() + ); assertTrue(OptOutQueryCache.cachingIsSafe(weight, permissions)); - permissions = new IndicesAccessControl.IndexAccessControl(true, - new FieldPermissions(fieldPermissionDef(new String[]{"*"}, new String[]{})), DocumentPermissions.allowAll()); + permissions = new IndicesAccessControl.IndexAccessControl( + true, + new FieldPermissions(fieldPermissionDef(new String[] { "*" }, new String[] {})), + DocumentPermissions.allowAll() + ); assertTrue(OptOutQueryCache.cachingIsSafe(weight, permissions)); - permissions = new IndicesAccessControl.IndexAccessControl(true, - new FieldPermissions(fieldPermissionDef(new String[]{"*"}, null)), DocumentPermissions.allowAll()); + permissions = new IndicesAccessControl.IndexAccessControl( + true, + new FieldPermissions(fieldPermissionDef(new String[] { "*" }, null)), + DocumentPermissions.allowAll() + ); assertTrue(OptOutQueryCache.cachingIsSafe(weight, permissions)); - permissions = new IndicesAccessControl.IndexAccessControl(true, - new FieldPermissions(fieldPermissionDef(new String[]{"*"}, new String[]{"oof"})), DocumentPermissions.allowAll()); + permissions = new IndicesAccessControl.IndexAccessControl( + true, + new FieldPermissions(fieldPermissionDef(new String[] { "*" }, new String[] { "oof" })), + DocumentPermissions.allowAll() + ); assertTrue(OptOutQueryCache.cachingIsSafe(weight, permissions)); - permissions = new IndicesAccessControl.IndexAccessControl(true, - new FieldPermissions(fieldPermissionDef(new String[]{"f*", "n*"}, new String[]{})), DocumentPermissions.allowAll()); + permissions = new IndicesAccessControl.IndexAccessControl( + true, + new FieldPermissions(fieldPermissionDef(new String[] { "f*", "n*" }, new String[] {})), + DocumentPermissions.allowAll() + ); assertTrue(OptOutQueryCache.cachingIsSafe(weight, permissions)); // check we don't cache if a field is not allowed - permissions = new IndicesAccessControl.IndexAccessControl(true, - new FieldPermissions(fieldPermissionDef(new String[]{"foo"}, null)), DocumentPermissions.allowAll()); + permissions = new IndicesAccessControl.IndexAccessControl( + true, + new FieldPermissions(fieldPermissionDef(new String[] { "foo" }, null)), + DocumentPermissions.allowAll() + ); assertFalse(OptOutQueryCache.cachingIsSafe(weight, permissions)); - permissions = new IndicesAccessControl.IndexAccessControl(true, - new FieldPermissions(fieldPermissionDef(new String[]{"a*"}, new String[]{"aa"})), DocumentPermissions.allowAll()); + permissions = new IndicesAccessControl.IndexAccessControl( + true, + new FieldPermissions(fieldPermissionDef(new String[] { "a*" }, new String[] { "aa" })), + DocumentPermissions.allowAll() + ); assertFalse(OptOutQueryCache.cachingIsSafe(weight, permissions)); - permissions = new IndicesAccessControl.IndexAccessControl(true, - new FieldPermissions(fieldPermissionDef(null, new String[]{"no"})), DocumentPermissions.allowAll()); + permissions = new IndicesAccessControl.IndexAccessControl( + true, + new FieldPermissions(fieldPermissionDef(null, new String[] { "no" })), + DocumentPermissions.allowAll() + ); assertFalse(OptOutQueryCache.cachingIsSafe(weight, permissions)); - permissions = new IndicesAccessControl.IndexAccessControl(true, - new FieldPermissions(fieldPermissionDef(null, new String[]{"*"})), DocumentPermissions.allowAll()); + permissions = new IndicesAccessControl.IndexAccessControl( + true, + new FieldPermissions(fieldPermissionDef(null, new String[] { "*" })), + DocumentPermissions.allowAll() + ); assertFalse(OptOutQueryCache.cachingIsSafe(weight, permissions)); - permissions = new IndicesAccessControl.IndexAccessControl(true, - new FieldPermissions(fieldPermissionDef(new String[]{"foo", "no"}, new String[]{"no"})), DocumentPermissions.allowAll()); + permissions = new IndicesAccessControl.IndexAccessControl( + true, + new FieldPermissions(fieldPermissionDef(new String[] { "foo", "no" }, new String[] { "no" })), + DocumentPermissions.allowAll() + ); assertFalse(OptOutQueryCache.cachingIsSafe(weight, permissions)); - permissions = new IndicesAccessControl.IndexAccessControl(true, - new FieldPermissions(fieldPermissionDef(new String[]{}, new String[]{})), DocumentPermissions.allowAll()); + permissions = new IndicesAccessControl.IndexAccessControl( + true, + new FieldPermissions(fieldPermissionDef(new String[] {}, new String[] {})), + DocumentPermissions.allowAll() + ); assertFalse(OptOutQueryCache.cachingIsSafe(weight, permissions)); - permissions = new IndicesAccessControl.IndexAccessControl(true, - new FieldPermissions(fieldPermissionDef(new String[]{}, null)), DocumentPermissions.allowAll()); + permissions = new IndicesAccessControl.IndexAccessControl( + true, + new FieldPermissions(fieldPermissionDef(new String[] {}, null)), + DocumentPermissions.allowAll() + ); assertFalse(OptOutQueryCache.cachingIsSafe(weight, permissions)); } public void testOptOutQueryCacheNoIndicesPermissions() { final Settings.Builder settings = Settings.builder() - .put("index.version.created", Version.CURRENT) - .put("index.number_of_shards", 1) - .put("index.number_of_replicas", 0); + .put("index.version.created", Version.CURRENT) + .put("index.number_of_shards", 1) + .put("index.number_of_replicas", 0); final IndexMetadata indexMetadata = IndexMetadata.builder("index").settings(settings).build(); final IndexSettings indexSettings = new IndexSettings(indexMetadata, Settings.EMPTY); final IndicesQueryCache indicesQueryCache = mock(IndicesQueryCache.class); @@ -143,9 +182,9 @@ public void testOptOutQueryCacheNoIndicesPermissions() { public void testOptOutQueryCacheIndexDoesNotHaveFieldLevelSecurity() { final Settings.Builder settings = Settings.builder() - .put("index.version.created", Version.CURRENT) - .put("index.number_of_shards", 1) - .put("index.number_of_replicas", 0); + .put("index.version.created", Version.CURRENT) + .put("index.number_of_shards", 1) + .put("index.number_of_replicas", 0); final IndexMetadata indexMetadata = IndexMetadata.builder("index").settings(settings).build(); final IndexSettings indexSettings = new IndexSettings(indexMetadata, Settings.EMPTY); final IndicesQueryCache indicesQueryCache = mock(IndicesQueryCache.class); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/interceptor/IndicesAliasesRequestInterceptorTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/interceptor/IndicesAliasesRequestInterceptorTests.java index b9b4a94a21a33..cee8270874278 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/interceptor/IndicesAliasesRequestInterceptorTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/interceptor/IndicesAliasesRequestInterceptorTests.java @@ -55,8 +55,11 @@ public void testInterceptorThrowsWhenFLSDLSEnabled() { when(licenseState.isAllowed(DOCUMENT_LEVEL_SECURITY_FEATURE)).thenReturn(true); ThreadContext threadContext = new ThreadContext(Settings.EMPTY); AuditTrailService auditTrailService = new AuditTrailService(Collections.emptyList(), licenseState); - Authentication authentication = new Authentication(new User("john", "role"), new RealmRef(null, null, null), - new RealmRef(null, null, null)); + Authentication authentication = new Authentication( + new User("john", "role"), + new RealmRef(null, null, null), + new RealmRef(null, null, null) + ); final FieldPermissions fieldPermissions; final boolean useFls = randomBoolean(); if (useFls) { @@ -72,13 +75,20 @@ public void testInterceptorThrowsWhenFLSDLSEnabled() { queries = null; } final String action = IndicesAliasesAction.NAME; - IndicesAccessControl accessControl = new IndicesAccessControl(true, Collections.singletonMap("foo", - new IndicesAccessControl.IndexAccessControl(true, fieldPermissions, - (useDls) ? DocumentPermissions.filteredBy(queries) : DocumentPermissions.allowAll()))); + IndicesAccessControl accessControl = new IndicesAccessControl( + true, + Collections.singletonMap( + "foo", + new IndicesAccessControl.IndexAccessControl( + true, + fieldPermissions, + (useDls) ? DocumentPermissions.filteredBy(queries) : DocumentPermissions.allowAll() + ) + ) + ); threadContext.putTransient(AuthorizationServiceField.INDICES_PERMISSIONS_KEY, accessControl); - IndicesAliasesRequestInterceptor interceptor = - new IndicesAliasesRequestInterceptor(threadContext, licenseState, auditTrailService); + IndicesAliasesRequestInterceptor interceptor = new IndicesAliasesRequestInterceptor(threadContext, licenseState, auditTrailService); IndicesAliasesRequest indicesAliasesRequest = new IndicesAliasesRequest(); if (randomBoolean()) { @@ -95,15 +105,16 @@ public void testInterceptorThrowsWhenFLSDLSEnabled() { ActionListener listener = (ActionListener) invocationOnMock.getArguments()[3]; listener.onResponse(AuthorizationResult.deny()); return null; - }).when(mockEngine).validateIndexPermissionsAreSubset(eq(requestInfo), eq(EmptyAuthorizationInfo.INSTANCE), anyMap(), - anyActionListener()); - ElasticsearchSecurityException securityException = expectThrows(ElasticsearchSecurityException.class, - () -> { - interceptor.intercept(requestInfo, mockEngine, EmptyAuthorizationInfo.INSTANCE, plainActionFuture); - plainActionFuture.actionGet(); - }); - assertEquals("Alias requests are not allowed for users who have field or document level security enabled on one of the indices", - securityException.getMessage()); + }).when(mockEngine) + .validateIndexPermissionsAreSubset(eq(requestInfo), eq(EmptyAuthorizationInfo.INSTANCE), anyMap(), anyActionListener()); + ElasticsearchSecurityException securityException = expectThrows(ElasticsearchSecurityException.class, () -> { + interceptor.intercept(requestInfo, mockEngine, EmptyAuthorizationInfo.INSTANCE, plainActionFuture); + plainActionFuture.actionGet(); + }); + assertEquals( + "Alias requests are not allowed for users who have field or document level security enabled on one of the indices", + securityException.getMessage() + ); } @SuppressWarnings("unchecked") @@ -114,13 +125,15 @@ public void testInterceptorThrowsWhenTargetHasGreaterPermissions() throws Except when(licenseState.isAllowed(DOCUMENT_LEVEL_SECURITY_FEATURE)).thenReturn(randomBoolean()); ThreadContext threadContext = new ThreadContext(Settings.EMPTY); AuditTrailService auditTrailService = new AuditTrailService(Collections.emptyList(), licenseState); - Authentication authentication = new Authentication(new User("john", "role"), new RealmRef(null, null, null), - new RealmRef(null, null, null)); + Authentication authentication = new Authentication( + new User("john", "role"), + new RealmRef(null, null, null), + new RealmRef(null, null, null) + ); final String action = IndicesAliasesAction.NAME; IndicesAccessControl accessControl = new IndicesAccessControl(true, Collections.emptyMap()); threadContext.putTransient(AuthorizationServiceField.INDICES_PERMISSIONS_KEY, accessControl); - IndicesAliasesRequestInterceptor interceptor = - new IndicesAliasesRequestInterceptor(threadContext, licenseState, auditTrailService); + IndicesAliasesRequestInterceptor interceptor = new IndicesAliasesRequestInterceptor(threadContext, licenseState, auditTrailService); final IndicesAliasesRequest indicesAliasesRequest = new IndicesAliasesRequest(); if (randomBoolean()) { @@ -139,15 +152,21 @@ public void testInterceptorThrowsWhenTargetHasGreaterPermissions() throws Except ActionListener listener = (ActionListener) invocationOnMock.getArguments()[3]; listener.onResponse(AuthorizationResult.deny()); return null; - }).when(mockEngine).validateIndexPermissionsAreSubset(eq(requestInfo), eq(EmptyAuthorizationInfo.INSTANCE), any(Map.class), - anyActionListener()); - ElasticsearchSecurityException securityException = expectThrows(ElasticsearchSecurityException.class, - () -> { - interceptor.intercept(requestInfo, mockEngine, EmptyAuthorizationInfo.INSTANCE, plainActionFuture); - plainActionFuture.actionGet(); - }); - assertEquals("Adding an alias is not allowed when the alias has more permissions than any of the indices", - securityException.getMessage()); + }).when(mockEngine) + .validateIndexPermissionsAreSubset( + eq(requestInfo), + eq(EmptyAuthorizationInfo.INSTANCE), + any(Map.class), + anyActionListener() + ); + ElasticsearchSecurityException securityException = expectThrows(ElasticsearchSecurityException.class, () -> { + interceptor.intercept(requestInfo, mockEngine, EmptyAuthorizationInfo.INSTANCE, plainActionFuture); + plainActionFuture.actionGet(); + }); + assertEquals( + "Adding an alias is not allowed when the alias has more permissions than any of the indices", + securityException.getMessage() + ); } // swap target and source for success @@ -167,8 +186,13 @@ public void testInterceptorThrowsWhenTargetHasGreaterPermissions() throws Except ActionListener listener = (ActionListener) invocationOnMock.getArguments()[3]; listener.onResponse(AuthorizationResult.granted()); return null; - }).when(mockEngine).validateIndexPermissionsAreSubset(eq(requestInfo), eq(EmptyAuthorizationInfo.INSTANCE), any(Map.class), - anyActionListener()); + }).when(mockEngine) + .validateIndexPermissionsAreSubset( + eq(requestInfo), + eq(EmptyAuthorizationInfo.INSTANCE), + any(Map.class), + anyActionListener() + ); interceptor.intercept(requestInfo, mockEngine, EmptyAuthorizationInfo.INSTANCE, plainActionFuture); plainActionFuture.actionGet(); } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/interceptor/ResizeRequestInterceptorTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/interceptor/ResizeRequestInterceptorTests.java index b3ac7883d8ad8..cfb10b19382bc 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/interceptor/ResizeRequestInterceptorTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/interceptor/ResizeRequestInterceptorTests.java @@ -78,13 +78,20 @@ public void testResizeRequestInterceptorThrowsWhenFLSDLSEnabled() { queries = null; } final String action = randomFrom(ShrinkAction.NAME, ResizeAction.NAME); - IndicesAccessControl accessControl = new IndicesAccessControl(true, Collections.singletonMap("foo", - new IndicesAccessControl.IndexAccessControl(true, fieldPermissions, - (useDls) ? DocumentPermissions.filteredBy(queries) : DocumentPermissions.allowAll()))); + IndicesAccessControl accessControl = new IndicesAccessControl( + true, + Collections.singletonMap( + "foo", + new IndicesAccessControl.IndexAccessControl( + true, + fieldPermissions, + (useDls) ? DocumentPermissions.filteredBy(queries) : DocumentPermissions.allowAll() + ) + ) + ); threadContext.putTransient(AuthorizationServiceField.INDICES_PERMISSIONS_KEY, accessControl); - ResizeRequestInterceptor resizeRequestInterceptor = - new ResizeRequestInterceptor(threadPool, licenseState, auditTrailService); + ResizeRequestInterceptor resizeRequestInterceptor = new ResizeRequestInterceptor(threadPool, licenseState, auditTrailService); PlainActionFuture plainActionFuture = new PlainActionFuture<>(); RequestInfo requestInfo = new RequestInfo(authentication, new ResizeRequest("bar", "foo"), action, null); @@ -93,15 +100,16 @@ public void testResizeRequestInterceptorThrowsWhenFLSDLSEnabled() { ActionListener listener = (ActionListener) invocationOnMock.getArguments()[3]; listener.onResponse(AuthorizationResult.deny()); return null; - }).when(mockEngine).validateIndexPermissionsAreSubset(eq(requestInfo), eq(EmptyAuthorizationInfo.INSTANCE), anyMap(), - anyActionListener()); - ElasticsearchSecurityException securityException = expectThrows(ElasticsearchSecurityException.class, - () -> { - resizeRequestInterceptor.intercept(requestInfo, mockEngine, EmptyAuthorizationInfo.INSTANCE, plainActionFuture); - plainActionFuture.actionGet(); - }); - assertEquals("Resize requests are not allowed for users when field or document level security is enabled on the source index", - securityException.getMessage()); + }).when(mockEngine) + .validateIndexPermissionsAreSubset(eq(requestInfo), eq(EmptyAuthorizationInfo.INSTANCE), anyMap(), anyActionListener()); + ElasticsearchSecurityException securityException = expectThrows(ElasticsearchSecurityException.class, () -> { + resizeRequestInterceptor.intercept(requestInfo, mockEngine, EmptyAuthorizationInfo.INSTANCE, plainActionFuture); + plainActionFuture.actionGet(); + }); + assertEquals( + "Resize requests are not allowed for users when field or document level security is enabled on the source index", + securityException.getMessage() + ); } @SuppressWarnings("unchecked") @@ -115,15 +123,11 @@ public void testResizeRequestInterceptorThrowsWhenTargetHasGreaterPermissions() when(threadPool.getThreadContext()).thenReturn(threadContext); AuditTrailService auditTrailService = new AuditTrailService(Collections.emptyList(), licenseState); final Authentication authentication = new Authentication(new User("john", "role"), new RealmRef(null, null, null), null); - Role role = Role.builder(Automatons.EMPTY) - .add(IndexPrivilege.ALL, "target") - .add(IndexPrivilege.READ, "source") - .build(); + Role role = Role.builder(Automatons.EMPTY).add(IndexPrivilege.ALL, "target").add(IndexPrivilege.READ, "source").build(); final String action = randomFrom(ShrinkAction.NAME, ResizeAction.NAME); IndicesAccessControl accessControl = new IndicesAccessControl(true, Collections.emptyMap()); threadContext.putTransient(AuthorizationServiceField.INDICES_PERMISSIONS_KEY, accessControl); - ResizeRequestInterceptor resizeRequestInterceptor = - new ResizeRequestInterceptor(threadPool, licenseState, auditTrailService); + ResizeRequestInterceptor resizeRequestInterceptor = new ResizeRequestInterceptor(threadPool, licenseState, auditTrailService); AuthorizationEngine mockEngine = mock(AuthorizationEngine.class); { @@ -133,15 +137,21 @@ public void testResizeRequestInterceptorThrowsWhenTargetHasGreaterPermissions() ActionListener listener = (ActionListener) invocationOnMock.getArguments()[3]; listener.onResponse(AuthorizationResult.deny()); return null; - }).when(mockEngine).validateIndexPermissionsAreSubset(eq(requestInfo), eq(EmptyAuthorizationInfo.INSTANCE), any(Map.class), - anyActionListener()); - ElasticsearchSecurityException securityException = expectThrows(ElasticsearchSecurityException.class, - () -> { - resizeRequestInterceptor.intercept(requestInfo, mockEngine, EmptyAuthorizationInfo.INSTANCE, plainActionFuture); - plainActionFuture.actionGet(); - }); - assertEquals("Resizing an index is not allowed when the target index has more permissions than the source index", - securityException.getMessage()); + }).when(mockEngine) + .validateIndexPermissionsAreSubset( + eq(requestInfo), + eq(EmptyAuthorizationInfo.INSTANCE), + any(Map.class), + anyActionListener() + ); + ElasticsearchSecurityException securityException = expectThrows(ElasticsearchSecurityException.class, () -> { + resizeRequestInterceptor.intercept(requestInfo, mockEngine, EmptyAuthorizationInfo.INSTANCE, plainActionFuture); + plainActionFuture.actionGet(); + }); + assertEquals( + "Resizing an index is not allowed when the target index has more permissions than the source index", + securityException.getMessage() + ); } // swap target and source for success @@ -152,8 +162,13 @@ public void testResizeRequestInterceptorThrowsWhenTargetHasGreaterPermissions() ActionListener listener = (ActionListener) invocationOnMock.getArguments()[3]; listener.onResponse(AuthorizationResult.granted()); return null; - }).when(mockEngine).validateIndexPermissionsAreSubset(eq(requestInfo), eq(EmptyAuthorizationInfo.INSTANCE), any(Map.class), - anyActionListener()); + }).when(mockEngine) + .validateIndexPermissionsAreSubset( + eq(requestInfo), + eq(EmptyAuthorizationInfo.INSTANCE), + any(Map.class), + anyActionListener() + ); resizeRequestInterceptor.intercept(requestInfo, mockEngine, EmptyAuthorizationInfo.INSTANCE, plainActionFuture); plainActionFuture.actionGet(); } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/interceptor/SearchRequestInterceptorTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/interceptor/SearchRequestInterceptorTests.java index f1d199f8b81e2..07a40b9d02eb3 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/interceptor/SearchRequestInterceptorTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/interceptor/SearchRequestInterceptorTests.java @@ -82,10 +82,14 @@ public void testRequestCacheWillBeDisabledWhenSearchRemoteIndices() { final SearchRequest searchRequest = mock(SearchRequest.class); when(searchRequest.source()).thenReturn(SearchSourceBuilder.searchSource()); final String[] localIndices = randomArray(0, 3, String[]::new, () -> randomAlphaOfLengthBetween(3, 8)); - final String[] remoteIndices = randomArray(0, 3, String[]::new, - () -> randomAlphaOfLengthBetween(0, 5) + ":" + randomAlphaOfLengthBetween(3, 8)); - final ArrayList allIndices = - Arrays.stream(ArrayUtils.concat(localIndices, remoteIndices)).collect(Collectors.toCollection(ArrayList::new)); + final String[] remoteIndices = randomArray( + 0, + 3, + String[]::new, + () -> randomAlphaOfLengthBetween(0, 5) + ":" + randomAlphaOfLengthBetween(3, 8) + ); + final ArrayList allIndices = Arrays.stream(ArrayUtils.concat(localIndices, remoteIndices)) + .collect(Collectors.toCollection(ArrayList::new)); Collections.shuffle(allIndices, random()); when(searchRequest.indices()).thenReturn(allIndices.toArray(String[]::new)); @@ -103,10 +107,14 @@ public void testHasRemoteIndices() { final SearchRequest searchRequest = mock(SearchRequest.class); when(searchRequest.source()).thenReturn(SearchSourceBuilder.searchSource()); final String[] localIndices = randomArray(0, 3, String[]::new, () -> randomAlphaOfLengthBetween(3, 8)); - final String[] remoteIndices = randomArray(0, 3, String[]::new, - () -> randomAlphaOfLengthBetween(0, 5) + ":" + randomAlphaOfLengthBetween(3, 8)); - final ArrayList allIndices = - Arrays.stream(ArrayUtils.concat(localIndices, remoteIndices)).collect(Collectors.toCollection(ArrayList::new)); + final String[] remoteIndices = randomArray( + 0, + 3, + String[]::new, + () -> randomAlphaOfLengthBetween(0, 5) + ":" + randomAlphaOfLengthBetween(3, 8) + ); + final ArrayList allIndices = Arrays.stream(ArrayUtils.concat(localIndices, remoteIndices)) + .collect(Collectors.toCollection(ArrayList::new)); Collections.shuffle(allIndices, random()); when(searchRequest.indices()).thenReturn(allIndices.toArray(String[]::new)); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/interceptor/ShardSearchRequestInterceptorTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/interceptor/ShardSearchRequestInterceptorTests.java index f67bf072895cc..bbca31604bff5 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/interceptor/ShardSearchRequestInterceptorTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/interceptor/ShardSearchRequestInterceptorTests.java @@ -66,14 +66,17 @@ private void configureMinMondeVersion(Version version) { public void testRequestCacheWillBeDisabledWhenDlsUsesStoredScripts() { configureMinMondeVersion(Version.CURRENT); final DocumentPermissions documentPermissions = DocumentPermissions.filteredBy( - Set.of(new BytesArray("{\"template\":{\"id\":\"my-script\"}}"))); + Set.of(new BytesArray("{\"template\":{\"id\":\"my-script\"}}")) + ); final ShardSearchRequest shardSearchRequest = mock(ShardSearchRequest.class); final String index = randomAlphaOfLengthBetween(3, 8); when(shardSearchRequest.shardId()).thenReturn(new ShardId(index, randomAlphaOfLength(22), randomInt(3))); final PlainActionFuture listener = new PlainActionFuture<>(); - interceptor.disableFeatures(shardSearchRequest, + interceptor.disableFeatures( + shardSearchRequest, Map.of(index, new IndicesAccessControl.IndexAccessControl(true, FieldPermissions.DEFAULT, documentPermissions)), - listener); + listener + ); listener.actionGet(); verify(shardSearchRequest).requestCache(false); } @@ -81,14 +84,17 @@ public void testRequestCacheWillBeDisabledWhenDlsUsesStoredScripts() { public void testRequestWillNotBeDisabledCacheWhenDlsUsesInlineScripts() { configureMinMondeVersion(Version.CURRENT); final DocumentPermissions documentPermissions = DocumentPermissions.filteredBy( - Set.of(new BytesArray("{\"term\":{\"username\":\"foo\"}}"))); + Set.of(new BytesArray("{\"term\":{\"username\":\"foo\"}}")) + ); final ShardSearchRequest shardSearchRequest = mock(ShardSearchRequest.class); final String index = randomAlphaOfLengthBetween(3, 8); when(shardSearchRequest.shardId()).thenReturn(new ShardId(index, randomAlphaOfLength(22), randomInt(3))); final PlainActionFuture listener = new PlainActionFuture<>(); - interceptor.disableFeatures(shardSearchRequest, + interceptor.disableFeatures( + shardSearchRequest, Map.of(index, new IndicesAccessControl.IndexAccessControl(true, FieldPermissions.DEFAULT, documentPermissions)), - listener); + listener + ); listener.actionGet(); verify(shardSearchRequest, never()).requestCache(false); } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/permission/FieldPermissionsTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/permission/FieldPermissionsTests.java index c7d8381607c4f..11941423f05c9 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/permission/FieldPermissionsTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/permission/FieldPermissionsTests.java @@ -8,8 +8,8 @@ import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.bytes.BytesArray; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.security.authz.RoleDescriptor; import org.elasticsearch.xpack.core.security.authz.permission.FieldPermissions; import org.elasticsearch.xpack.core.security.authz.permission.FieldPermissionsDefinition; @@ -24,177 +24,192 @@ public class FieldPermissionsTests extends ESTestCase { public void testParseFieldPermissions() throws Exception { - String q = "{\"indices\": [ {\"names\": \"idx2\", \"privileges\": [\"p3\"], " + - "\"field_security\": {" + - "\"grant\": [\"f1\", \"f2\", \"f3\", \"f4\"]," + - "\"except\": [\"f3\",\"f4\"]" + - "}}]}"; - RoleDescriptor rd = - RoleDescriptor.parse("test", new BytesArray(q), false, XContentType.JSON); - assertArrayEquals(rd.getIndicesPrivileges()[0].getGrantedFields(), - new String[] { "f1", "f2", "f3", "f4" }); - assertArrayEquals(rd.getIndicesPrivileges()[0].getDeniedFields(), - new String[] { "f3", "f4" }); - - q = "{\"indices\": [ {\"names\": \"idx2\", \"privileges\": [\"p3\"], " + - "\"field_security\": {" + - "\"except\": [\"f3\",\"f4\"]," + - "\"grant\": [\"f1\", \"f2\", \"f3\", \"f4\"]" + - "}}]}"; + String q = "{\"indices\": [ {\"names\": \"idx2\", \"privileges\": [\"p3\"], " + + "\"field_security\": {" + + "\"grant\": [\"f1\", \"f2\", \"f3\", \"f4\"]," + + "\"except\": [\"f3\",\"f4\"]" + + "}}]}"; + RoleDescriptor rd = RoleDescriptor.parse("test", new BytesArray(q), false, XContentType.JSON); + assertArrayEquals(rd.getIndicesPrivileges()[0].getGrantedFields(), new String[] { "f1", "f2", "f3", "f4" }); + assertArrayEquals(rd.getIndicesPrivileges()[0].getDeniedFields(), new String[] { "f3", "f4" }); + + q = "{\"indices\": [ {\"names\": \"idx2\", \"privileges\": [\"p3\"], " + + "\"field_security\": {" + + "\"except\": [\"f3\",\"f4\"]," + + "\"grant\": [\"f1\", \"f2\", \"f3\", \"f4\"]" + + "}}]}"; rd = RoleDescriptor.parse("test", new BytesArray(q), false, XContentType.JSON); - assertArrayEquals(rd.getIndicesPrivileges()[0].getGrantedFields(), - new String[] { "f1", "f2", "f3", "f4" }); - assertArrayEquals(rd.getIndicesPrivileges()[0].getDeniedFields(), - new String[] { "f3", "f4" }); - - q = "{\"indices\": [ {\"names\": \"idx2\", \"privileges\": [\"p3\"], " + - "\"field_security\": {" + - "\"grant\": [\"f1\", \"f2\"]" + - "}}]}"; + assertArrayEquals(rd.getIndicesPrivileges()[0].getGrantedFields(), new String[] { "f1", "f2", "f3", "f4" }); + assertArrayEquals(rd.getIndicesPrivileges()[0].getDeniedFields(), new String[] { "f3", "f4" }); + + q = "{\"indices\": [ {\"names\": \"idx2\", \"privileges\": [\"p3\"], " + + "\"field_security\": {" + + "\"grant\": [\"f1\", \"f2\"]" + + "}}]}"; rd = RoleDescriptor.parse("test", new BytesArray(q), false, XContentType.JSON); - assertArrayEquals(rd.getIndicesPrivileges()[0].getGrantedFields(), - new String[] { "f1", "f2" }); + assertArrayEquals(rd.getIndicesPrivileges()[0].getGrantedFields(), new String[] { "f1", "f2" }); assertNull(rd.getIndicesPrivileges()[0].getDeniedFields()); - q = "{\"indices\": [ {\"names\": \"idx2\", \"privileges\": [\"p3\"], " + - "\"field_security\": {" + - "\"grant\": []" + - "}}]}"; + q = "{\"indices\": [ {\"names\": \"idx2\", \"privileges\": [\"p3\"], " + "\"field_security\": {" + "\"grant\": []" + "}}]}"; rd = RoleDescriptor.parse("test", new BytesArray(q), false, XContentType.JSON); assertArrayEquals(rd.getIndicesPrivileges()[0].getGrantedFields(), new String[] {}); assertNull(rd.getIndicesPrivileges()[0].getDeniedFields()); - q = "{\"indices\": [ {\"names\": \"idx2\", \"privileges\": [\"p3\"], " + - "\"field_security\": {" + - "\"except\": []," + - "\"grant\": []" + - "}}]}"; + q = "{\"indices\": [ {\"names\": \"idx2\", \"privileges\": [\"p3\"], " + + "\"field_security\": {" + + "\"except\": []," + + "\"grant\": []" + + "}}]}"; rd = RoleDescriptor.parse("test", new BytesArray(q), false, XContentType.JSON); assertArrayEquals(rd.getIndicesPrivileges()[0].getGrantedFields(), new String[] {}); assertArrayEquals(rd.getIndicesPrivileges()[0].getDeniedFields(), new String[] {}); - final String exceptWithoutGrant = "{\"indices\": [ {\"names\": \"idx2\", \"privileges\":" + - " [\"p3\"], \"field_security\": {" + - "\"except\": [\"f1\"]" + - "}}]}"; - ElasticsearchParseException e = expectThrows(ElasticsearchParseException.class, - () -> RoleDescriptor.parse("test", new BytesArray(exceptWithoutGrant), false, - XContentType.JSON)); - assertThat(e.getDetailedMessage(), - containsString("failed to parse indices privileges for role [test]. field_security" - + " requires grant if except is given")); - - final String grantNull = "{\"indices\": [ {\"names\": \"idx2\", \"privileges\": [\"p3\"]," + - " \"field_security\": {" + - "\"grant\": null" + - "}}]}"; - e = expectThrows(ElasticsearchParseException.class, - () -> RoleDescriptor.parse("test", new BytesArray(grantNull), false, - XContentType.JSON)); - assertThat(e.getDetailedMessage(), containsString("failed to parse indices privileges for" + - " role [test]. grant must not be null.")); - - final String exceptNull = "{\"indices\": [ {\"names\": \"idx2\", \"privileges\": " + - "[\"p3\"], \"field_security\": {" + - "\"grant\": [\"*\"]," + - "\"except\": null" + - "}}]}"; - e = expectThrows(ElasticsearchParseException.class, - () -> RoleDescriptor.parse("test", new BytesArray(exceptNull), false, - XContentType.JSON)); - assertThat(e.getDetailedMessage(), - containsString("failed to parse indices privileges for role [test]. except must" + - " not be null.")); - - final String exceptGrantNull = "{\"indices\": [ {\"names\": \"idx2\", \"privileges\": " + - "[\"p3\"], \"field_security\": {" + - "\"grant\": null," + - "\"except\": null" + - "}}]}"; - e = expectThrows(ElasticsearchParseException.class, - () -> RoleDescriptor.parse("test", new BytesArray(exceptGrantNull), false, - XContentType.JSON)); - assertThat(e.getDetailedMessage(), containsString("failed to parse indices privileges " + - "for role [test]. grant must not be null.")); - - final String bothFieldsMissing = "{\"indices\": [ {\"names\": \"idx2\", \"privileges\": " + - "[\"p3\"], \"field_security\": {" + - "}}]}"; - e = expectThrows(ElasticsearchParseException.class, - () -> RoleDescriptor.parse("test", new BytesArray(bothFieldsMissing), false, - XContentType.JSON)); - assertThat(e.getDetailedMessage(), containsString("failed to parse indices privileges " + - "for role [test]. \"field_security\" must not be empty.")); + final String exceptWithoutGrant = "{\"indices\": [ {\"names\": \"idx2\", \"privileges\":" + + " [\"p3\"], \"field_security\": {" + + "\"except\": [\"f1\"]" + + "}}]}"; + ElasticsearchParseException e = expectThrows( + ElasticsearchParseException.class, + () -> RoleDescriptor.parse("test", new BytesArray(exceptWithoutGrant), false, XContentType.JSON) + ); + assertThat( + e.getDetailedMessage(), + containsString("failed to parse indices privileges for role [test]. field_security" + " requires grant if except is given") + ); + + final String grantNull = "{\"indices\": [ {\"names\": \"idx2\", \"privileges\": [\"p3\"]," + + " \"field_security\": {" + + "\"grant\": null" + + "}}]}"; + e = expectThrows( + ElasticsearchParseException.class, + () -> RoleDescriptor.parse("test", new BytesArray(grantNull), false, XContentType.JSON) + ); + assertThat( + e.getDetailedMessage(), + containsString("failed to parse indices privileges for" + " role [test]. grant must not be null.") + ); + + final String exceptNull = "{\"indices\": [ {\"names\": \"idx2\", \"privileges\": " + + "[\"p3\"], \"field_security\": {" + + "\"grant\": [\"*\"]," + + "\"except\": null" + + "}}]}"; + e = expectThrows( + ElasticsearchParseException.class, + () -> RoleDescriptor.parse("test", new BytesArray(exceptNull), false, XContentType.JSON) + ); + assertThat( + e.getDetailedMessage(), + containsString("failed to parse indices privileges for role [test]. except must" + " not be null.") + ); + + final String exceptGrantNull = "{\"indices\": [ {\"names\": \"idx2\", \"privileges\": " + + "[\"p3\"], \"field_security\": {" + + "\"grant\": null," + + "\"except\": null" + + "}}]}"; + e = expectThrows( + ElasticsearchParseException.class, + () -> RoleDescriptor.parse("test", new BytesArray(exceptGrantNull), false, XContentType.JSON) + ); + assertThat( + e.getDetailedMessage(), + containsString("failed to parse indices privileges " + "for role [test]. grant must not be null.") + ); + + final String bothFieldsMissing = "{\"indices\": [ {\"names\": \"idx2\", \"privileges\": " + + "[\"p3\"], \"field_security\": {" + + "}}]}"; + e = expectThrows( + ElasticsearchParseException.class, + () -> RoleDescriptor.parse("test", new BytesArray(bothFieldsMissing), false, XContentType.JSON) + ); + assertThat( + e.getDetailedMessage(), + containsString("failed to parse indices privileges " + "for role [test]. \"field_security\" must not be empty.") + ); // try with two indices and mix order a little - q = "{\"indices\": [ {\"names\": \"idx2\", \"privileges\": [\"p3\"], " + - "\"field_security\": {" + - "\"grant\": []" + - "}}," + - "{\"names\": \"idx3\",\n" + - " \"field_security\": {\n" + - " \"grant\": [\"*\"], \n" + - " \"except\": [\"f2\"]}," + - "\"privileges\": [\"p3\"]}]}"; + q = "{\"indices\": [ {\"names\": \"idx2\", \"privileges\": [\"p3\"], " + + "\"field_security\": {" + + "\"grant\": []" + + "}}," + + "{\"names\": \"idx3\",\n" + + " \"field_security\": {\n" + + " \"grant\": [\"*\"], \n" + + " \"except\": [\"f2\"]}," + + "\"privileges\": [\"p3\"]}]}"; rd = RoleDescriptor.parse("test", new BytesArray(q), false, XContentType.JSON); assertArrayEquals(rd.getIndicesPrivileges()[0].getGrantedFields(), new String[] {}); assertNull(rd.getIndicesPrivileges()[0].getDeniedFields()); - assertArrayEquals(rd.getIndicesPrivileges()[1].getGrantedFields(), new String[] {"*"}); - assertArrayEquals(rd.getIndicesPrivileges()[1].getDeniedFields(), new String[] {"f2"}); + assertArrayEquals(rd.getIndicesPrivileges()[1].getGrantedFields(), new String[] { "*" }); + assertArrayEquals(rd.getIndicesPrivileges()[1].getDeniedFields(), new String[] { "f2" }); } // test old syntax for field permissions public void testBWCFieldPermissions() throws Exception { - String q = "{\"indices\": [ {\"names\": \"idx2\", \"privileges\": [\"p3\"], " + - "\"fields\": [\"f1\", \"f2\"]" + - "}]}"; - RoleDescriptor rd = RoleDescriptor.parse("test", new BytesArray(q), true, - XContentType.JSON); - assertArrayEquals(rd.getIndicesPrivileges()[0].getGrantedFields(), - new String[]{"f1", "f2"}); + String q = "{\"indices\": [ {\"names\": \"idx2\", \"privileges\": [\"p3\"], " + "\"fields\": [\"f1\", \"f2\"]" + "}]}"; + RoleDescriptor rd = RoleDescriptor.parse("test", new BytesArray(q), true, XContentType.JSON); + assertArrayEquals(rd.getIndicesPrivileges()[0].getGrantedFields(), new String[] { "f1", "f2" }); assertNull(rd.getIndicesPrivileges()[0].getDeniedFields()); final String failingQuery = q; - ElasticsearchParseException e = expectThrows(ElasticsearchParseException.class, - () -> RoleDescriptor.parse("test", new BytesArray(failingQuery), false, - XContentType.JSON)); - assertThat(e.getDetailedMessage(), containsString("[\"fields\": [...]] format has " + - "changed for field permissions in role [test]" + - ", use [\"field_security\": {\"grant\":[...],\"except\":[...]}] instead")); - - q = "{\"indices\": [ {\"names\": \"idx2\", \"privileges\": [\"p3\"], " + - "\"fields\": []" + - "}]}"; + ElasticsearchParseException e = expectThrows( + ElasticsearchParseException.class, + () -> RoleDescriptor.parse("test", new BytesArray(failingQuery), false, XContentType.JSON) + ); + assertThat( + e.getDetailedMessage(), + containsString( + "[\"fields\": [...]] format has " + + "changed for field permissions in role [test]" + + ", use [\"field_security\": {\"grant\":[...],\"except\":[...]}] instead" + ) + ); + + q = "{\"indices\": [ {\"names\": \"idx2\", \"privileges\": [\"p3\"], " + "\"fields\": []" + "}]}"; rd = RoleDescriptor.parse("test", new BytesArray(q), true, XContentType.JSON); - assertArrayEquals(rd.getIndicesPrivileges()[0].getGrantedFields(), new String[]{}); + assertArrayEquals(rd.getIndicesPrivileges()[0].getGrantedFields(), new String[] {}); assertNull(rd.getIndicesPrivileges()[0].getDeniedFields()); final String failingQuery2 = q; - e = expectThrows(ElasticsearchParseException.class, - () -> RoleDescriptor.parse("test", new BytesArray(failingQuery2), false, - XContentType.JSON)); - assertThat(e.getDetailedMessage(), containsString("[\"fields\": [...]] format has " + - "changed for field permissions in role [test]" + - ", use [\"field_security\": {\"grant\":[...],\"except\":[...]}] instead")); - - q = "{\"indices\": [ {\"names\": \"idx2\", \"privileges\": [\"p3\"], " + - "\"fields\": null" + - "}]}"; + e = expectThrows( + ElasticsearchParseException.class, + () -> RoleDescriptor.parse("test", new BytesArray(failingQuery2), false, XContentType.JSON) + ); + assertThat( + e.getDetailedMessage(), + containsString( + "[\"fields\": [...]] format has " + + "changed for field permissions in role [test]" + + ", use [\"field_security\": {\"grant\":[...],\"except\":[...]}] instead" + ) + ); + + q = "{\"indices\": [ {\"names\": \"idx2\", \"privileges\": [\"p3\"], " + "\"fields\": null" + "}]}"; rd = RoleDescriptor.parse("test", new BytesArray(q), true, XContentType.JSON); assertNull(rd.getIndicesPrivileges()[0].getGrantedFields()); assertNull(rd.getIndicesPrivileges()[0].getDeniedFields()); final String failingQuery3 = q; - e = expectThrows(ElasticsearchParseException.class, - () -> RoleDescriptor.parse("test", new BytesArray(failingQuery3), false, - XContentType.JSON)); - assertThat(e.getDetailedMessage(), containsString("[\"fields\": [...]] format has " + - "changed for field permissions in role [test]" + - ", use [\"field_security\": {\"grant\":[...],\"except\":[...]}] instead")); + e = expectThrows( + ElasticsearchParseException.class, + () -> RoleDescriptor.parse("test", new BytesArray(failingQuery3), false, XContentType.JSON) + ); + assertThat( + e.getDetailedMessage(), + containsString( + "[\"fields\": [...]] format has " + + "changed for field permissions in role [test]" + + ", use [\"field_security\": {\"grant\":[...],\"except\":[...]}] instead" + ) + ); } public void testFieldPermissionsHashCodeThreadSafe() throws Exception { final int numThreads = scaledRandomIntBetween(4, 16); - final FieldPermissions fieldPermissions =new FieldPermissions( - new FieldPermissionsDefinition(new String[] { "*" }, new String[] { "foo" })); + final FieldPermissions fieldPermissions = new FieldPermissions( + new FieldPermissionsDefinition(new String[] { "*" }, new String[] { "foo" }) + ); final CountDownLatch latch = new CountDownLatch(numThreads + 1); final AtomicReferenceArray hashCodes = new AtomicReferenceArray<>(numThreads); List threads = new ArrayList<>(numThreads); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/permission/PermissionTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/permission/PermissionTests.java index ebb346fd13730..dd93ec9dfbce2 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/permission/PermissionTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/permission/PermissionTests.java @@ -79,9 +79,7 @@ public void testBuildEmptyRole() { } public void testRunAs() { - Role permission = Role.builder(Automatons.EMPTY, "some_role") - .runAs(new Privilege("name", "user1", "run*")) - .build(); + Role permission = Role.builder(Automatons.EMPTY, "some_role").runAs(new Privilege("name", "user1", "run*")).build(); assertThat(permission.runAs().check("user1"), is(true)); assertThat(permission.runAs().check("user"), is(false)); assertThat(permission.runAs().check("run" + randomAlphaOfLengthBetween(1, 10)), is(true)); @@ -99,8 +97,9 @@ private void testAllowedIndicesMatcher(Predicate indicesMatche private IndexAbstraction mockIndexAbstraction(String name) { IndexAbstraction mock = mock(IndexAbstraction.class); when(mock.getName()).thenReturn(name); - when(mock.getType()).thenReturn(randomFrom(IndexAbstraction.Type.CONCRETE_INDEX, - IndexAbstraction.Type.ALIAS, IndexAbstraction.Type.DATA_STREAM)); + when(mock.getType()).thenReturn( + randomFrom(IndexAbstraction.Type.CONCRETE_INDEX, IndexAbstraction.Type.ALIAS, IndexAbstraction.Type.DATA_STREAM) + ); return mock; } } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/store/CompositeRolesStoreTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/store/CompositeRolesStoreTests.java index c75f8db65f66e..769ec8d2dc261 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/store/CompositeRolesStoreTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/store/CompositeRolesStoreTests.java @@ -32,19 +32,19 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.common.util.set.Sets; -import org.elasticsearch.license.LicenseStateListener; -import org.elasticsearch.license.MockLicenseState; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.core.Nullable; import org.elasticsearch.index.query.QueryBuilders; +import org.elasticsearch.license.LicenseStateListener; +import org.elasticsearch.license.MockLicenseState; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.VersionUtils; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportRequest; import org.elasticsearch.transport.TransportRequest.Empty; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.XPackPlugin; import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.xpack.core.security.action.saml.SamlAuthenticateAction; @@ -70,6 +70,7 @@ import org.elasticsearch.xpack.core.security.index.IndexAuditTrailField; import org.elasticsearch.xpack.core.security.index.RestrictedIndicesNames; import org.elasticsearch.xpack.core.security.support.MetadataUtils; +import org.elasticsearch.xpack.core.security.test.TestRestrictedIndices; import org.elasticsearch.xpack.core.security.user.AnonymousUser; import org.elasticsearch.xpack.core.security.user.AsyncSearchUser; import org.elasticsearch.xpack.core.security.user.SystemUser; @@ -83,7 +84,6 @@ import org.elasticsearch.xpack.security.authc.service.ServiceAccountService; import org.elasticsearch.xpack.security.support.CacheInvalidatorRegistry; import org.elasticsearch.xpack.security.support.SecurityIndexManager; -import org.elasticsearch.xpack.core.security.test.TestRestrictedIndices; import org.hamcrest.Matchers; import java.io.IOException; @@ -109,15 +109,13 @@ import java.util.function.Function; import java.util.function.Predicate; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.elasticsearch.test.ActionListenerUtils.anyActionListener; import static org.elasticsearch.xpack.core.security.SecurityField.DOCUMENT_LEVEL_SECURITY_FEATURE; import static org.elasticsearch.xpack.core.security.authc.AuthenticationField.API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY; import static org.elasticsearch.xpack.core.security.authc.AuthenticationField.API_KEY_ROLE_DESCRIPTORS_KEY; +import static org.elasticsearch.xpack.core.security.test.TestRestrictedIndices.RESTRICTED_INDICES_AUTOMATON; import static org.elasticsearch.xpack.security.authc.ApiKeyService.API_KEY_ID_KEY; import static org.elasticsearch.xpack.security.authc.ApiKeyServiceTests.Utils.createApiKeyAuthentication; -import static org.elasticsearch.xpack.core.security.test.TestRestrictedIndices.RESTRICTED_INDICES_AUTOMATON; import static org.hamcrest.Matchers.anyOf; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.containsString; @@ -137,54 +135,58 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; public class CompositeRolesStoreTests extends ESTestCase { - private static final Settings SECURITY_ENABLED_SETTINGS = Settings.builder() - .put(XPackSettings.SECURITY_ENABLED.getKey(), true) - .build(); + private static final Settings SECURITY_ENABLED_SETTINGS = Settings.builder().put(XPackSettings.SECURITY_ENABLED.getKey(), true).build(); private final IndexNameExpressionResolver resolver = TestRestrictedIndices.RESOLVER; private final FieldPermissionsCache cache = new FieldPermissionsCache(Settings.EMPTY); private final String concreteSecurityIndexName = randomFrom( - RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_6, RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7); + RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_6, + RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7 + ); public void testRolesWhenDlsFlsUnlicensed() throws IOException { MockLicenseState licenseState = mock(MockLicenseState.class); when(licenseState.isAllowed(DOCUMENT_LEVEL_SECURITY_FEATURE)).thenReturn(false); - RoleDescriptor flsRole = new RoleDescriptor("fls", null, new IndicesPrivileges[] { - IndicesPrivileges.builder() - .grantedFields("*") - .deniedFields("foo") - .indices("*") - .privileges("read") - .build() - }, null); + RoleDescriptor flsRole = new RoleDescriptor( + "fls", + null, + new IndicesPrivileges[] { + IndicesPrivileges.builder().grantedFields("*").deniedFields("foo").indices("*").privileges("read").build() }, + null + ); BytesReference matchAllBytes = XContentHelper.toXContent(QueryBuilders.matchAllQuery(), XContentType.JSON, false); - RoleDescriptor dlsRole = new RoleDescriptor("dls", null, new IndicesPrivileges[] { - IndicesPrivileges.builder() - .indices("*") - .privileges("read") - .query(matchAllBytes) - .build() - }, null); - RoleDescriptor flsDlsRole = new RoleDescriptor("fls_dls", null, new IndicesPrivileges[] { - IndicesPrivileges.builder() - .indices("*") - .privileges("read") - .grantedFields("*") - .deniedFields("foo") - .query(matchAllBytes) - .build() - }, null); - RoleDescriptor noFlsDlsRole = new RoleDescriptor("no_fls_dls", null, new IndicesPrivileges[] { + RoleDescriptor dlsRole = new RoleDescriptor( + "dls", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices("*").privileges("read").query(matchAllBytes).build() }, + null + ); + RoleDescriptor flsDlsRole = new RoleDescriptor( + "fls_dls", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder() - .indices("*") - .privileges("read") - .build() - }, null); + .indices("*") + .privileges("read") + .grantedFields("*") + .deniedFields("foo") + .query(matchAllBytes) + .build() }, + null + ); + RoleDescriptor noFlsDlsRole = new RoleDescriptor( + "no_fls_dls", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices("*").privileges("read").build() }, + null + ); FileRolesStore fileRolesStore = mock(FileRolesStore.class); doCallRealMethod().when(fileRolesStore).accept(anySetOf(String.class), anyActionListener()); @@ -193,9 +195,18 @@ public void testRolesWhenDlsFlsUnlicensed() throws IOException { when(fileRolesStore.roleDescriptors(Collections.singleton("fls_dls"))).thenReturn(Collections.singleton(flsDlsRole)); when(fileRolesStore.roleDescriptors(Collections.singleton("no_fls_dls"))).thenReturn(Collections.singleton(noFlsDlsRole)); final AtomicReference> effectiveRoleDescriptors = new AtomicReference>(); - CompositeRolesStore compositeRolesStore = buildCompositeRolesStore(Settings.EMPTY, fileRolesStore, null, - null, null, licenseState, null, null, - null, rds -> effectiveRoleDescriptors.set(rds)); + CompositeRolesStore compositeRolesStore = buildCompositeRolesStore( + Settings.EMPTY, + fileRolesStore, + null, + null, + null, + licenseState, + null, + null, + null, + rds -> effectiveRoleDescriptors.set(rds) + ); PlainActionFuture roleFuture = new PlainActionFuture<>(); compositeRolesStore.roles(Collections.singleton("fls"), roleFuture); @@ -225,37 +236,39 @@ public void testRolesWhenDlsFlsUnlicensed() throws IOException { public void testRolesWhenDlsFlsLicensed() throws IOException { MockLicenseState licenseState = mock(MockLicenseState.class); when(licenseState.isAllowed(DOCUMENT_LEVEL_SECURITY_FEATURE)).thenReturn(true); - RoleDescriptor flsRole = new RoleDescriptor("fls", null, new IndicesPrivileges[] { - IndicesPrivileges.builder() - .grantedFields("*") - .deniedFields("foo") - .indices("*") - .privileges("read") - .build() - }, null); + RoleDescriptor flsRole = new RoleDescriptor( + "fls", + null, + new IndicesPrivileges[] { + IndicesPrivileges.builder().grantedFields("*").deniedFields("foo").indices("*").privileges("read").build() }, + null + ); BytesReference matchAllBytes = XContentHelper.toXContent(QueryBuilders.matchAllQuery(), XContentType.JSON, false); - RoleDescriptor dlsRole = new RoleDescriptor("dls", null, new IndicesPrivileges[] { - IndicesPrivileges.builder() - .indices("*") - .privileges("read") - .query(matchAllBytes) - .build() - }, null); - RoleDescriptor flsDlsRole = new RoleDescriptor("fls_dls", null, new IndicesPrivileges[] { - IndicesPrivileges.builder() - .indices("*") - .privileges("read") - .grantedFields("*") - .deniedFields("foo") - .query(matchAllBytes) - .build() - }, null); - RoleDescriptor noFlsDlsRole = new RoleDescriptor("no_fls_dls", null, new IndicesPrivileges[] { + RoleDescriptor dlsRole = new RoleDescriptor( + "dls", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices("*").privileges("read").query(matchAllBytes).build() }, + null + ); + RoleDescriptor flsDlsRole = new RoleDescriptor( + "fls_dls", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder() - .indices("*") - .privileges("read") - .build() - }, null); + .indices("*") + .privileges("read") + .grantedFields("*") + .deniedFields("foo") + .query(matchAllBytes) + .build() }, + null + ); + RoleDescriptor noFlsDlsRole = new RoleDescriptor( + "no_fls_dls", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices("*").privileges("read").build() }, + null + ); FileRolesStore fileRolesStore = mock(FileRolesStore.class); doCallRealMethod().when(fileRolesStore).accept(anySetOf(String.class), anyActionListener()); when(fileRolesStore.roleDescriptors(Collections.singleton("fls"))).thenReturn(Collections.singleton(flsRole)); @@ -263,9 +276,18 @@ public void testRolesWhenDlsFlsLicensed() throws IOException { when(fileRolesStore.roleDescriptors(Collections.singleton("fls_dls"))).thenReturn(Collections.singleton(flsDlsRole)); when(fileRolesStore.roleDescriptors(Collections.singleton("no_fls_dls"))).thenReturn(Collections.singleton(noFlsDlsRole)); final AtomicReference> effectiveRoleDescriptors = new AtomicReference>(); - CompositeRolesStore compositeRolesStore = buildCompositeRolesStore(Settings.EMPTY, fileRolesStore, null, - null, null, licenseState, null, null, - null, rds -> effectiveRoleDescriptors.set(rds)); + CompositeRolesStore compositeRolesStore = buildCompositeRolesStore( + Settings.EMPTY, + fileRolesStore, + null, + null, + null, + licenseState, + null, + null, + null, + rds -> effectiveRoleDescriptors.set(rds) + ); PlainActionFuture roleFuture = new PlainActionFuture<>(); compositeRolesStore.roles(Collections.singleton("fls"), roleFuture); @@ -309,16 +331,25 @@ public void testNegativeLookupsAreCached() { final NativePrivilegeStore nativePrivilegeStore = mock(NativePrivilegeStore.class); doAnswer((invocationOnMock) -> { @SuppressWarnings("unchecked") - ActionListener> callback = - (ActionListener>) invocationOnMock.getArguments()[2]; + ActionListener> callback = (ActionListener< + Collection>) invocationOnMock.getArguments()[2]; callback.onResponse(Collections.emptyList()); return null; }).when(nativePrivilegeStore).getPrivileges(anySetOf(String.class), anySetOf(String.class), anyActionListener()); final AtomicReference> effectiveRoleDescriptors = new AtomicReference>(); - final CompositeRolesStore compositeRolesStore = buildCompositeRolesStore(SECURITY_ENABLED_SETTINGS, - fileRolesStore, nativeRolesStore, reservedRolesStore, nativePrivilegeStore, null, null, null, - null, rds -> effectiveRoleDescriptors.set(rds)); + final CompositeRolesStore compositeRolesStore = buildCompositeRolesStore( + SECURITY_ENABLED_SETTINGS, + fileRolesStore, + nativeRolesStore, + reservedRolesStore, + nativePrivilegeStore, + null, + null, + null, + null, + rds -> effectiveRoleDescriptors.set(rds) + ); verify(fileRolesStore).addListener(anyConsumer()); // adds a listener in ctor final String roleName = randomAlphaOfLengthBetween(1, 10); @@ -336,9 +367,10 @@ public void testNegativeLookupsAreCached() { final int numberOfTimesToCall = scaledRandomIntBetween(0, 32); final boolean getSuperuserRole = randomBoolean() - && roleName.equals(ReservedRolesStore.SUPERUSER_ROLE_DESCRIPTOR.getName()) == false; - final Set names = getSuperuserRole ? Sets.newHashSet(roleName, ReservedRolesStore.SUPERUSER_ROLE_DESCRIPTOR.getName()) - : Collections.singleton(roleName); + && roleName.equals(ReservedRolesStore.SUPERUSER_ROLE_DESCRIPTOR.getName()) == false; + final Set names = getSuperuserRole + ? Sets.newHashSet(roleName, ReservedRolesStore.SUPERUSER_ROLE_DESCRIPTOR.getName()) + : Collections.singleton(roleName); for (int i = 0; i < numberOfTimesToCall; i++) { future = new PlainActionFuture<>(); compositeRolesStore.roles(names, future); @@ -373,7 +405,8 @@ public void testNegativeLookupsCacheDisabled() { }).when(nativeRolesStore).getRoleDescriptors(isASet(), anyActionListener()); final ReservedRolesStore reservedRolesStore = spy(new ReservedRolesStore()); - final Settings settings = Settings.builder().put(SECURITY_ENABLED_SETTINGS) + final Settings settings = Settings.builder() + .put(SECURITY_ENABLED_SETTINGS) .put("xpack.security.authz.store.roles.negative_lookup_cache.max_size", 0) .build(); final AtomicReference> effectiveRoleDescriptors = new AtomicReference>(); @@ -473,7 +506,6 @@ public void testNegativeLookupsAreNotCachedWithFailures() { verifyNoMoreInteractions(fileRolesStore, reservedRolesStore, nativeRolesStore); } - public void testCustomRolesProviders() { final FileRolesStore fileRolesStore = mock(FileRolesStore.class); doCallRealMethod().when(fileRolesStore).accept(anySetOf(String.class), anyActionListener()); @@ -488,10 +520,12 @@ public void testCustomRolesProviders() { }).when(nativeRolesStore).getRoleDescriptors(isASet(), anyActionListener()); final ReservedRolesStore reservedRolesStore = spy(new ReservedRolesStore()); - final RoleDescriptor roleAProvider1 = new RoleDescriptor("roleA", null, - new IndicesPrivileges[] { - IndicesPrivileges.builder().privileges("READ").indices("foo").grantedFields("*").build() - }, null); + final RoleDescriptor roleAProvider1 = new RoleDescriptor( + "roleA", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().privileges("READ").indices("foo").grantedFields("*").build() }, + null + ); final InMemoryRolesProvider inMemoryProvider1 = spy(new InMemoryRolesProvider((roles) -> { Set descriptors = new HashSet<>(); if (roles.contains("roleA")) { @@ -500,19 +534,25 @@ public void testCustomRolesProviders() { return RoleRetrievalResult.success(descriptors); })); - final RoleDescriptor roleBProvider2 = new RoleDescriptor("roleB", null, - new IndicesPrivileges[] { - IndicesPrivileges.builder().privileges("READ").indices("bar").grantedFields("*").build() - }, null); + final RoleDescriptor roleBProvider2 = new RoleDescriptor( + "roleB", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().privileges("READ").indices("bar").grantedFields("*").build() }, + null + ); final InMemoryRolesProvider inMemoryProvider2 = spy(new InMemoryRolesProvider((roles) -> { Set descriptors = new HashSet<>(); if (roles.contains("roleA")) { // both role providers can resolve role A, this makes sure that if the first // role provider in order resolves a role, the second provider does not override it - descriptors.add(new RoleDescriptor("roleA", null, - new IndicesPrivileges[] { - IndicesPrivileges.builder().privileges("WRITE").indices("*").grantedFields("*").build() - }, null)); + descriptors.add( + new RoleDescriptor( + "roleA", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().privileges("WRITE").indices("*").grantedFields("*").build() }, + null + ) + ); } if (roles.contains("roleB")) { descriptors.add(roleBProvider2); @@ -580,36 +620,53 @@ public void testCustomRolesProviders() { * permissions from different roles instead of properly creating a union of their languages */ public void testMergingRolesWithFls() { - RoleDescriptor flsRole = new RoleDescriptor("fls", null, new IndicesPrivileges[] { + RoleDescriptor flsRole = new RoleDescriptor( + "fls", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder() - .grantedFields("*") - .deniedFields("L1.*", "L2.*") - .indices("*") - .privileges("read") - .query("{ \"match\": {\"eventType.typeCode\": \"foo\"} }") - .build() - }, null); - RoleDescriptor addsL1Fields = new RoleDescriptor("dls", null, new IndicesPrivileges[] { + .grantedFields("*") + .deniedFields("L1.*", "L2.*") + .indices("*") + .privileges("read") + .query("{ \"match\": {\"eventType.typeCode\": \"foo\"} }") + .build() }, + null + ); + RoleDescriptor addsL1Fields = new RoleDescriptor( + "dls", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder() - .indices("*") - .grantedFields("L1.*") - .privileges("read") - .query("{ \"match\": {\"eventType.typeCode\": \"foo\"} }") - .build() - }, null); + .indices("*") + .grantedFields("L1.*") + .privileges("read") + .query("{ \"match\": {\"eventType.typeCode\": \"foo\"} }") + .build() }, + null + ); FieldPermissionsCache cache = new FieldPermissionsCache(Settings.EMPTY); PlainActionFuture future = new PlainActionFuture<>(); CompositeRolesStore.buildRoleFromDescriptors( - Sets.newHashSet(flsRole, addsL1Fields), cache, null, RESTRICTED_INDICES_AUTOMATON, future); + Sets.newHashSet(flsRole, addsL1Fields), + cache, + null, + RESTRICTED_INDICES_AUTOMATON, + future + ); Role role = future.actionGet(); Metadata metadata = Metadata.builder() - .put(new IndexMetadata.Builder("test") - .settings(Settings.builder().put("index.version.created", Version.CURRENT).build()) - .numberOfShards(1).numberOfReplicas(0).build(), true) - .build(); - IndicesAccessControl iac = role.indices().authorize("indices:data/read/search", - Collections.singleton("test"), metadata.getIndicesLookup(), cache); + .put( + new IndexMetadata.Builder("test").settings(Settings.builder().put("index.version.created", Version.CURRENT).build()) + .numberOfShards(1) + .numberOfReplicas(0) + .build(), + true + ) + .build(); + IndicesAccessControl iac = role.indices() + .authorize("indices:data/read/search", Collections.singleton("test"), metadata.getIndicesLookup(), cache); assertTrue(iac.getIndexPermissions("test").getFieldPermissions().grantsAccessTo("L1.foo")); assertFalse(iac.getIndexPermissions("test").getFieldPermissions().grantsAccessTo("L2.foo")); assertTrue(iac.getIndexPermissions("test").getFieldPermissions().grantsAccessTo("L3.foo")); @@ -624,60 +681,60 @@ public void testMergingBasicRoles() { ConfigurableClusterPrivilege ccp1 = new MockConfigurableClusterPrivilege() { @Override public ClusterPermission.Builder buildPermission(ClusterPermission.Builder builder) { - builder.add(this, ((ActionClusterPrivilege) ClusterPrivilegeResolver.MANAGE_SECURITY).getAllowedActionPatterns(), - req -> req == request1); + builder.add( + this, + ((ActionClusterPrivilege) ClusterPrivilegeResolver.MANAGE_SECURITY).getAllowedActionPatterns(), + req -> req == request1 + ); return builder; } }; - RoleDescriptor role1 = new RoleDescriptor("r1", new String[]{"monitor"}, new IndicesPrivileges[]{ - IndicesPrivileges.builder() - .indices("abc-*", "xyz-*") - .privileges("read") - .build(), - IndicesPrivileges.builder() - .indices("ind-1-*") - .privileges("all") - .build(), - }, new RoleDescriptor.ApplicationResourcePrivileges[]{ - RoleDescriptor.ApplicationResourcePrivileges.builder() - .application("app1") - .resources("user/*") - .privileges("read", "write") - .build(), - RoleDescriptor.ApplicationResourcePrivileges.builder() - .application("app1") - .resources("settings/*") - .privileges("read") - .build() - }, new ConfigurableClusterPrivilege[] { ccp1 }, - new String[]{"app-user-1"}, null, null); + RoleDescriptor role1 = new RoleDescriptor( + "r1", + new String[] { "monitor" }, + new IndicesPrivileges[] { + IndicesPrivileges.builder().indices("abc-*", "xyz-*").privileges("read").build(), + IndicesPrivileges.builder().indices("ind-1-*").privileges("all").build(), }, + new RoleDescriptor.ApplicationResourcePrivileges[] { + RoleDescriptor.ApplicationResourcePrivileges.builder() + .application("app1") + .resources("user/*") + .privileges("read", "write") + .build(), + RoleDescriptor.ApplicationResourcePrivileges.builder() + .application("app1") + .resources("settings/*") + .privileges("read") + .build() }, + new ConfigurableClusterPrivilege[] { ccp1 }, + new String[] { "app-user-1" }, + null, + null + ); ConfigurableClusterPrivilege ccp2 = new MockConfigurableClusterPrivilege() { @Override public ClusterPermission.Builder buildPermission(ClusterPermission.Builder builder) { - builder.add(this, ((ActionClusterPrivilege) ClusterPrivilegeResolver.MANAGE_SECURITY).getAllowedActionPatterns(), - req -> req == request2); + builder.add( + this, + ((ActionClusterPrivilege) ClusterPrivilegeResolver.MANAGE_SECURITY).getAllowedActionPatterns(), + req -> req == request2 + ); return builder; } }; - RoleDescriptor role2 = new RoleDescriptor("r2", new String[]{"manage_saml"}, new IndicesPrivileges[]{ - IndicesPrivileges.builder() - .indices("abc-*", "ind-2-*") - .privileges("all") - .build() - }, new RoleDescriptor.ApplicationResourcePrivileges[]{ - RoleDescriptor.ApplicationResourcePrivileges.builder() - .application("app2a") - .resources("*") - .privileges("all") - .build(), - RoleDescriptor.ApplicationResourcePrivileges.builder() - .application("app2b") - .resources("*") - .privileges("read") - .build() - }, new ConfigurableClusterPrivilege[] { ccp2 }, - new String[]{"app-user-2"}, null, null); + RoleDescriptor role2 = new RoleDescriptor( + "r2", + new String[] { "manage_saml" }, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices("abc-*", "ind-2-*").privileges("all").build() }, + new RoleDescriptor.ApplicationResourcePrivileges[] { + RoleDescriptor.ApplicationResourcePrivileges.builder().application("app2a").resources("*").privileges("all").build(), + RoleDescriptor.ApplicationResourcePrivileges.builder().application("app2b").resources("*").privileges("read").build() }, + new ConfigurableClusterPrivilege[] { ccp2 }, + new String[] { "app-user-2" }, + null, + null + ); FieldPermissionsCache cache = new FieldPermissionsCache(Settings.EMPTY); PlainActionFuture future = new PlainActionFuture<>(); @@ -685,26 +742,37 @@ public ClusterPermission.Builder buildPermission(ClusterPermission.Builder build doAnswer(inv -> { assertEquals(3, inv.getArguments().length); @SuppressWarnings("unchecked") - ActionListener> listener - = (ActionListener>) inv.getArguments()[2]; + ActionListener> listener = (ActionListener< + Collection>) inv.getArguments()[2]; Set set = new HashSet<>(); - Arrays.asList("app1", "app2a", "app2b").forEach( - app -> Arrays.asList("read", "write", "all").forEach( - perm -> set.add( - new ApplicationPrivilegeDescriptor(app, perm, Collections.emptySet(), Collections.emptyMap()) - ))); + Arrays.asList("app1", "app2a", "app2b") + .forEach( + app -> Arrays.asList("read", "write", "all") + .forEach( + perm -> set.add(new ApplicationPrivilegeDescriptor(app, perm, Collections.emptySet(), Collections.emptyMap())) + ) + ); listener.onResponse(set); return null; }).when(privilegeStore).getPrivileges(anyCollectionOf(String.class), anyCollectionOf(String.class), anyActionListener()); - CompositeRolesStore.buildRoleFromDescriptors(Sets.newHashSet(role1, role2), cache, privilegeStore, - RESTRICTED_INDICES_AUTOMATON, future); + CompositeRolesStore.buildRoleFromDescriptors( + Sets.newHashSet(role1, role2), + cache, + privilegeStore, + RESTRICTED_INDICES_AUTOMATON, + future + ); Role role = future.actionGet(); assertThat(role.cluster().check(ClusterStateAction.NAME, randomFrom(request1, request2, request3), authentication), equalTo(true)); - assertThat(role.cluster().check(SamlAuthenticateAction.NAME, randomFrom(request1, request2, request3), authentication), - equalTo(true)); - assertThat(role.cluster().check(ClusterUpdateSettingsAction.NAME, randomFrom(request1, request2, request3), authentication), - equalTo(false)); + assertThat( + role.cluster().check(SamlAuthenticateAction.NAME, randomFrom(request1, request2, request3), authentication), + equalTo(true) + ); + assertThat( + role.cluster().check(ClusterUpdateSettingsAction.NAME, randomFrom(request1, request2, request3), authentication), + equalTo(false) + ); assertThat(role.cluster().check(PutUserAction.NAME, randomFrom(request1, request2), authentication), equalTo(true)); assertThat(role.cluster().check(PutUserAction.NAME, request3, authentication), equalTo(false)); @@ -752,16 +820,22 @@ public void testCustomRolesProviderFailures() throws Exception { final InMemoryRolesProvider inMemoryProvider1 = new InMemoryRolesProvider((roles) -> { Set descriptors = new HashSet<>(); if (roles.contains("roleA")) { - descriptors.add(new RoleDescriptor("roleA", null, - new IndicesPrivileges[] { - IndicesPrivileges.builder().privileges("READ").indices("foo").grantedFields("*").build() - }, null)); + descriptors.add( + new RoleDescriptor( + "roleA", + null, + new IndicesPrivileges[] { + IndicesPrivileges.builder().privileges("READ").indices("foo").grantedFields("*").build() }, + null + ) + ); } return RoleRetrievalResult.success(descriptors); }); - final BiConsumer, ActionListener> failingProvider = - (roles, listener) -> listener.onFailure(new Exception("fake failure")); + final BiConsumer, ActionListener> failingProvider = (roles, listener) -> listener.onFailure( + new Exception("fake failure") + ); final AtomicReference> effectiveRoleDescriptors = new AtomicReference>(); final Map, ActionListener>>> customRoleProviders = randomBoolean() @@ -808,10 +882,12 @@ public void testCustomRolesProvidersLicensing() { }).when(nativeRolesStore).getRoleDescriptors(isASet(), anyActionListener()); final ReservedRolesStore reservedRolesStore = new ReservedRolesStore(); - final RoleDescriptor roleA = new RoleDescriptor("roleA", null, - new IndicesPrivileges[] { - IndicesPrivileges.builder().privileges("READ").indices("foo").grantedFields("*").build() - }, null); + final RoleDescriptor roleA = new RoleDescriptor( + "roleA", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().privileges("READ").indices("foo").grantedFields("*").build() }, + null + ); final InMemoryRolesProvider inMemoryProvider = new InMemoryRolesProvider((roles) -> { Set descriptors = new HashSet<>(); if (roles.contains("roleA")) { @@ -888,8 +964,17 @@ private SecurityIndexManager.State dummyState(ClusterHealthStatus indexStatus) { public SecurityIndexManager.State dummyIndexState(boolean isIndexUpToDate, ClusterHealthStatus healthStatus) { return new SecurityIndexManager.State( - Instant.now(), isIndexUpToDate, true, true, null, concreteSecurityIndexName, healthStatus, IndexMetadata.State.OPEN, null, - "my_uuid"); + Instant.now(), + isIndexUpToDate, + true, + true, + null, + concreteSecurityIndexName, + healthStatus, + IndexMetadata.State.OPEN, + null, + "my_uuid" + ); } public void testCacheClearOnIndexHealthChange() { @@ -944,8 +1029,9 @@ public void testCacheClearOnIndexHealthChange() { // green to yellow or yellow to green previousState = dummyState(randomFrom(ClusterHealthStatus.GREEN, ClusterHealthStatus.YELLOW)); - currentState = dummyState(previousState.indexHealth == ClusterHealthStatus.GREEN ? - ClusterHealthStatus.YELLOW : ClusterHealthStatus.GREEN); + currentState = dummyState( + previousState.indexHealth == ClusterHealthStatus.GREEN ? ClusterHealthStatus.YELLOW : ClusterHealthStatus.GREEN + ); compositeRolesStore.onSecurityIndexStateChange(previousState, currentState); assertEquals(expectedInvalidation, numInvalidation.get()); } @@ -995,9 +1081,18 @@ public void testDefaultRoleUserWithoutRoles() { }).when(nativeRolesStore).getRoleDescriptors(isASet(), anyActionListener()); final ReservedRolesStore reservedRolesStore = spy(new ReservedRolesStore()); - final CompositeRolesStore compositeRolesStore = buildCompositeRolesStore(SECURITY_ENABLED_SETTINGS, fileRolesStore, - nativeRolesStore, reservedRolesStore, mock(NativePrivilegeStore.class), null, mock(ApiKeyService.class), - mock(ServiceAccountService.class), null, null); + final CompositeRolesStore compositeRolesStore = buildCompositeRolesStore( + SECURITY_ENABLED_SETTINGS, + fileRolesStore, + nativeRolesStore, + reservedRolesStore, + mock(NativePrivilegeStore.class), + null, + mock(ApiKeyService.class), + mock(ServiceAccountService.class), + null, + null + ); verify(fileRolesStore).addListener(anyConsumer()); // adds a listener in ctor PlainActionFuture rolesFuture = new PlainActionFuture<>(); @@ -1025,8 +1120,7 @@ public void testAnonymousUserEnabledRoleAdded() { return Collections.singleton(rd); } return Collections.emptySet(); - }). - when(fileRolesStore).roleDescriptors(anySetOf(String.class)); + }).when(fileRolesStore).roleDescriptors(anySetOf(String.class)); doAnswer((invocationOnMock) -> { @SuppressWarnings("unchecked") ActionListener callback = (ActionListener) invocationOnMock.getArguments()[1]; @@ -1035,9 +1129,18 @@ public void testAnonymousUserEnabledRoleAdded() { }).when(nativeRolesStore).getRoleDescriptors(isASet(), anyActionListener()); final ReservedRolesStore reservedRolesStore = spy(new ReservedRolesStore()); - final CompositeRolesStore compositeRolesStore = buildCompositeRolesStore(settings, fileRolesStore, nativeRolesStore, - reservedRolesStore, mock(NativePrivilegeStore.class), null, mock(ApiKeyService.class), - mock(ServiceAccountService.class), null, null); + final CompositeRolesStore compositeRolesStore = buildCompositeRolesStore( + settings, + fileRolesStore, + nativeRolesStore, + reservedRolesStore, + mock(NativePrivilegeStore.class), + null, + mock(ApiKeyService.class), + mock(ServiceAccountService.class), + null, + null + ); verify(fileRolesStore).addListener(anyConsumer()); // adds a listener in ctor PlainActionFuture rolesFuture = new PlainActionFuture<>(); @@ -1063,19 +1166,18 @@ public void testDoesNotUseRolesStoreForXPacAndAsyncSearchUser() { final ReservedRolesStore reservedRolesStore = spy(new ReservedRolesStore()); final AtomicReference> effectiveRoleDescriptors = new AtomicReference>(); - final CompositeRolesStore compositeRolesStore = - buildCompositeRolesStore( - SECURITY_ENABLED_SETTINGS, - fileRolesStore, - nativeRolesStore, - reservedRolesStore, - null, - null, - null, - null, - null, - rds -> effectiveRoleDescriptors.set(rds) - ); + final CompositeRolesStore compositeRolesStore = buildCompositeRolesStore( + SECURITY_ENABLED_SETTINGS, + fileRolesStore, + nativeRolesStore, + reservedRolesStore, + null, + null, + null, + null, + null, + rds -> effectiveRoleDescriptors.set(rds) + ); verify(fileRolesStore).addListener(anyConsumer()); // adds a listener in ctor // test Xpack user short circuits to its own reserved role @@ -1125,8 +1227,10 @@ public void testGetRolesForSystemUserThrowsException() { rds -> effectiveRoleDescriptors.set(rds) ); verify(fileRolesStore).addListener(anyConsumer()); // adds a listener in ctor - IllegalArgumentException iae = expectThrows(IllegalArgumentException.class, - () -> compositeRolesStore.getRoles(SystemUser.INSTANCE, null, null)); + IllegalArgumentException iae = expectThrows( + IllegalArgumentException.class, + () -> compositeRolesStore.getRoles(SystemUser.INSTANCE, null, null) + ); assertThat(effectiveRoleDescriptors.get(), is(nullValue())); assertEquals("the user [_system] is the system user and we should never try to get its roles", iae.getMessage()); } @@ -1145,14 +1249,22 @@ public void testApiKeyAuthUsesApiKeyService() throws Exception { }).when(nativeRolesStore).getRoleDescriptors(isASet(), anyActionListener()); final ReservedRolesStore reservedRolesStore = spy(new ReservedRolesStore()); ThreadContext threadContext = new ThreadContext(SECURITY_ENABLED_SETTINGS); - ApiKeyService apiKeyService = spy(new ApiKeyService(SECURITY_ENABLED_SETTINGS, Clock.systemUTC(), mock(Client.class), - mock(SecurityIndexManager.class), mock(ClusterService.class), - mock(CacheInvalidatorRegistry.class), mock(ThreadPool.class))); + ApiKeyService apiKeyService = spy( + new ApiKeyService( + SECURITY_ENABLED_SETTINGS, + Clock.systemUTC(), + mock(Client.class), + mock(SecurityIndexManager.class), + mock(ClusterService.class), + mock(CacheInvalidatorRegistry.class), + mock(ThreadPool.class) + ) + ); NativePrivilegeStore nativePrivStore = mock(NativePrivilegeStore.class); doAnswer(invocationOnMock -> { @SuppressWarnings("unchecked") - ActionListener> listener = - (ActionListener>) invocationOnMock.getArguments()[2]; + ActionListener> listener = (ActionListener< + Collection>) invocationOnMock.getArguments()[2]; listener.onResponse(Collections.emptyList()); return Void.TYPE; }).when(nativePrivStore).getPrivileges(anyCollectionOf(String.class), anyCollectionOf(String.class), anyActionListener()); @@ -1172,10 +1284,13 @@ public void testApiKeyAuthUsesApiKeyService() throws Exception { ); AuditUtil.getOrGenerateRequestId(threadContext); final Version version = randomFrom(Version.CURRENT, VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, Version.V_7_8_1)); - final Authentication authentication = createApiKeyAuthentication(apiKeyService, createAuthentication(), - Collections.singleton(new RoleDescriptor("user_role_" + randomAlphaOfLength(4), new String[]{"manage"}, null, null)), + final Authentication authentication = createApiKeyAuthentication( + apiKeyService, + createAuthentication(), + Collections.singleton(new RoleDescriptor("user_role_" + randomAlphaOfLength(4), new String[] { "manage" }, null, null)), null, - version); + version + ); PlainActionFuture roleFuture = new PlainActionFuture<>(); compositeRolesStore.getRoles(authentication.getUser(), authentication, roleFuture); @@ -1206,14 +1321,22 @@ public void testApiKeyAuthUsesApiKeyServiceWithScopedRole() throws Exception { final ReservedRolesStore reservedRolesStore = spy(new ReservedRolesStore()); ThreadContext threadContext = new ThreadContext(SECURITY_ENABLED_SETTINGS); - ApiKeyService apiKeyService = spy(new ApiKeyService(SECURITY_ENABLED_SETTINGS, Clock.systemUTC(), mock(Client.class), - mock(SecurityIndexManager.class), mock(ClusterService.class), - mock(CacheInvalidatorRegistry.class), mock(ThreadPool.class))); + ApiKeyService apiKeyService = spy( + new ApiKeyService( + SECURITY_ENABLED_SETTINGS, + Clock.systemUTC(), + mock(Client.class), + mock(SecurityIndexManager.class), + mock(ClusterService.class), + mock(CacheInvalidatorRegistry.class), + mock(ThreadPool.class) + ) + ); NativePrivilegeStore nativePrivStore = mock(NativePrivilegeStore.class); doAnswer(invocationOnMock -> { @SuppressWarnings("unchecked") - ActionListener> listener = - (ActionListener>) invocationOnMock.getArguments()[2]; + ActionListener> listener = (ActionListener< + Collection>) invocationOnMock.getArguments()[2]; listener.onResponse(Collections.emptyList()); return Void.TYPE; }).when(nativePrivStore).getPrivileges(anyCollectionOf(String.class), anyCollectionOf(String.class), anyActionListener()); @@ -1233,10 +1356,13 @@ public void testApiKeyAuthUsesApiKeyServiceWithScopedRole() throws Exception { ); AuditUtil.getOrGenerateRequestId(threadContext); final Version version = randomFrom(Version.CURRENT, VersionUtils.randomVersionBetween(random(), Version.V_7_0_0, Version.V_7_8_1)); - final Authentication authentication = createApiKeyAuthentication(apiKeyService, createAuthentication(), - Collections.singleton(new RoleDescriptor("user_role_" + randomAlphaOfLength(4), new String[]{"manage"}, null, null)), - Collections.singletonList(new RoleDescriptor("key_role_" + randomAlphaOfLength(8), new String[]{"monitor"}, null, null)), - version); + final Authentication authentication = createApiKeyAuthentication( + apiKeyService, + createAuthentication(), + Collections.singleton(new RoleDescriptor("user_role_" + randomAlphaOfLength(4), new String[] { "manage" }, null, null)), + Collections.singletonList(new RoleDescriptor("key_role_" + randomAlphaOfLength(8), new String[] { "monitor" }, null, null)), + version + ); PlainActionFuture roleFuture = new PlainActionFuture<>(); compositeRolesStore.getRoles(authentication.getUser(), authentication, roleFuture); @@ -1271,9 +1397,17 @@ public void testUsageStats() { final DocumentSubsetBitsetCache documentSubsetBitsetCache = buildBitsetCache(); final CompositeRolesStore compositeRolesStore = buildCompositeRolesStore( - SECURITY_ENABLED_SETTINGS, fileRolesStore, nativeRolesStore, reservedRolesStore, null, null, - mock(ApiKeyService.class), mock(ServiceAccountService.class), - documentSubsetBitsetCache, null); + SECURITY_ENABLED_SETTINGS, + fileRolesStore, + nativeRolesStore, + reservedRolesStore, + null, + null, + mock(ApiKeyService.class), + mock(ServiceAccountService.class), + documentSubsetBitsetCache, + null + ); PlainActionFuture> usageStatsListener = new PlainActionFuture<>(); compositeRolesStore.usageStats(usageStatsListener); @@ -1286,8 +1420,15 @@ public void testUsageStats() { public void testLoggingOfDeprecatedRoles() { List descriptors = new ArrayList<>(); Function, RoleDescriptor> newRole = metadata -> new RoleDescriptor( - randomAlphaOfLengthBetween(4, 9), generateRandomStringArray(5, 5, false, true), - null, null, null, null, metadata, null); + randomAlphaOfLengthBetween(4, 9), + generateRandomStringArray(5, 5, false, true), + null, + null, + null, + null, + metadata, + null + ); RoleDescriptor deprecated1 = newRole.apply(MetadataUtils.getDeprecatedReservedMetadata("some reason")); RoleDescriptor deprecated2 = newRole.apply(MetadataUtils.getDeprecatedReservedMetadata("a different reason")); @@ -1296,7 +1437,7 @@ public void testLoggingOfDeprecatedRoles() { // so we clone metadata with a real value and then remove that key final Map nullReasonMetadata = new HashMap<>(deprecated2.getMetadata()); nullReasonMetadata.remove(MetadataUtils.DEPRECATED_REASON_METADATA_KEY); - assertThat(nullReasonMetadata.keySet(), hasSize(deprecated2.getMetadata().size() -1)); + assertThat(nullReasonMetadata.keySet(), hasSize(deprecated2.getMetadata().size() - 1)); RoleDescriptor deprecated3 = newRole.apply(nullReasonMetadata); descriptors.add(deprecated1); @@ -1316,20 +1457,35 @@ public void testLoggingOfDeprecatedRoles() { } Collections.shuffle(descriptors, random()); - final CompositeRolesStore compositeRolesStore = - buildCompositeRolesStore(SECURITY_ENABLED_SETTINGS, null, null, null, null, null, - null, mock(ServiceAccountService.class), null, null); + final CompositeRolesStore compositeRolesStore = buildCompositeRolesStore( + SECURITY_ENABLED_SETTINGS, + null, + null, + null, + null, + null, + null, + mock(ServiceAccountService.class), + null, + null + ); // Use a LHS so that the random-shufle-order of the list is preserved compositeRolesStore.logDeprecatedRoles(new LinkedHashSet<>(descriptors)); assertWarnings( - "The role [" + deprecated1.getName() + "] is deprecated and will be removed in a future version of Elasticsearch." + - " some reason", - "The role [" + deprecated2.getName() + "] is deprecated and will be removed in a future version of Elasticsearch." + - " a different reason", - "The role [" + deprecated3.getName() + "] is deprecated and will be removed in a future version of Elasticsearch." + - " Please check the documentation" + "The role [" + + deprecated1.getName() + + "] is deprecated and will be removed in a future version of Elasticsearch." + + " some reason", + "The role [" + + deprecated2.getName() + + "] is deprecated and will be removed in a future version of Elasticsearch." + + " a different reason", + "The role [" + + deprecated3.getName() + + "] is deprecated and will be removed in a future version of Elasticsearch." + + " Please check the documentation" ); } @@ -1351,14 +1507,15 @@ public void testCacheEntryIsReusedForIdenticalApiKeyRoles() { NativePrivilegeStore nativePrivStore = mock(NativePrivilegeStore.class); doAnswer(invocationOnMock -> { @SuppressWarnings("unchecked") - ActionListener> listener = - (ActionListener>) invocationOnMock.getArguments()[2]; + ActionListener> listener = (ActionListener< + Collection>) invocationOnMock.getArguments()[2]; listener.onResponse(Collections.emptyList()); return Void.TYPE; }).when(nativePrivStore).getPrivileges(anyCollectionOf(String.class), anyCollectionOf(String.class), anyActionListener()); final AtomicReference> effectiveRoleDescriptors = new AtomicReference>(); - final CompositeRolesStore compositeRolesStore = buildCompositeRolesStore(SECURITY_ENABLED_SETTINGS, + final CompositeRolesStore compositeRolesStore = buildCompositeRolesStore( + SECURITY_ENABLED_SETTINGS, fileRolesStore, nativeRolesStore, reservedRolesStore, @@ -1367,21 +1524,26 @@ public void testCacheEntryIsReusedForIdenticalApiKeyRoles() { apiKeyService, null, null, - rds -> effectiveRoleDescriptors.set(rds)); + rds -> effectiveRoleDescriptors.set(rds) + ); AuditUtil.getOrGenerateRequestId(threadContext); final BytesArray roleBytes = new BytesArray("{\"a role\": {\"cluster\": [\"all\"]}}"); final BytesArray limitedByRoleBytes = new BytesArray("{\"limitedBy role\": {\"cluster\": [\"all\"]}}"); - Authentication authentication = new Authentication(new User("test api key user", "superuser"), + Authentication authentication = new Authentication( + new User("test api key user", "superuser"), new RealmRef("_es_api_key", "_es_api_key", "node"), null, Version.CURRENT, AuthenticationType.API_KEY, - Map.of(API_KEY_ID_KEY, + Map.of( + API_KEY_ID_KEY, "key-id-1", API_KEY_ROLE_DESCRIPTORS_KEY, roleBytes, API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY, - limitedByRoleBytes)); + limitedByRoleBytes + ) + ); doCallRealMethod().when(apiKeyService).getApiKeyIdAndRoleBytes(eq(authentication), anyBoolean()); PlainActionFuture roleFuture = new PlainActionFuture<>(); @@ -1393,17 +1555,21 @@ public void testCacheEntryIsReusedForIdenticalApiKeyRoles() { verify(apiKeyService).parseRoleDescriptors("key-id-1", limitedByRoleBytes); // Different API key with the same roles should read from cache - authentication = new Authentication(new User("test api key user 2", "superuser"), + authentication = new Authentication( + new User("test api key user 2", "superuser"), new RealmRef("_es_api_key", "_es_api_key", "node"), null, Version.CURRENT, AuthenticationType.API_KEY, - Map.of(API_KEY_ID_KEY, + Map.of( + API_KEY_ID_KEY, "key-id-2", API_KEY_ROLE_DESCRIPTORS_KEY, roleBytes, API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY, - limitedByRoleBytes)); + limitedByRoleBytes + ) + ); doCallRealMethod().when(apiKeyService).getApiKeyIdAndRoleBytes(eq(authentication), anyBoolean()); roleFuture = new PlainActionFuture<>(); compositeRolesStore.getRoles(authentication.getUser(), authentication, roleFuture); @@ -1414,17 +1580,21 @@ public void testCacheEntryIsReusedForIdenticalApiKeyRoles() { // Different API key with the same limitedBy role should read from cache, new role should be built final BytesArray anotherRoleBytes = new BytesArray("{\"b role\": {\"cluster\": [\"manage_security\"]}}"); - authentication = new Authentication(new User("test api key user 2", "superuser"), + authentication = new Authentication( + new User("test api key user 2", "superuser"), new RealmRef("_es_api_key", "_es_api_key", "node"), null, Version.CURRENT, AuthenticationType.API_KEY, - Map.of(API_KEY_ID_KEY, + Map.of( + API_KEY_ID_KEY, "key-id-3", API_KEY_ROLE_DESCRIPTORS_KEY, anotherRoleBytes, API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY, - limitedByRoleBytes)); + limitedByRoleBytes + ) + ); doCallRealMethod().when(apiKeyService).getApiKeyIdAndRoleBytes(eq(authentication), anyBoolean()); roleFuture = new PlainActionFuture<>(); compositeRolesStore.getRoles(authentication.getUser(), authentication, roleFuture); @@ -1438,18 +1608,27 @@ private Authentication createAuthentication() { final RealmRef lookedUpBy; final User user; if (randomBoolean()) { - user = new User("_username", randomBoolean() ? new String[]{"r1"} : - new String[]{ReservedRolesStore.SUPERUSER_ROLE_DESCRIPTOR.getName()}, - new User("authenticated_username", new String[]{"r2"})); + user = new User( + "_username", + randomBoolean() ? new String[] { "r1" } : new String[] { ReservedRolesStore.SUPERUSER_ROLE_DESCRIPTOR.getName() }, + new User("authenticated_username", new String[] { "r2" }) + ); lookedUpBy = new RealmRef("lookRealm", "up", "by"); } else { - user = new User("_username", randomBoolean() ? new String[]{"r1"} : - new String[]{ReservedRolesStore.SUPERUSER_ROLE_DESCRIPTOR.getName()}); + user = new User( + "_username", + randomBoolean() ? new String[] { "r1" } : new String[] { ReservedRolesStore.SUPERUSER_ROLE_DESCRIPTOR.getName() } + ); lookedUpBy = null; } - return new Authentication(user, new RealmRef("authRealm", "test", "foo"), lookedUpBy, - Version.CURRENT, randomFrom(AuthenticationType.REALM, AuthenticationType.TOKEN, AuthenticationType.INTERNAL, - AuthenticationType.ANONYMOUS), Collections.emptyMap()); + return new Authentication( + user, + new RealmRef("authRealm", "test", "foo"), + lookedUpBy, + Version.CURRENT, + randomFrom(AuthenticationType.REALM, AuthenticationType.TOKEN, AuthenticationType.INTERNAL, AuthenticationType.ANONYMOUS), + Collections.emptyMap() + ); } public void testXPackUserCanAccessNonRestrictedIndices() { @@ -1473,8 +1652,10 @@ public void testXPackUserCannotAccessSecurityOrAsyncSearch() { for (String index : RestrictedIndicesNames.RESTRICTED_NAMES) { assertThat(predicate.test(mockIndexAbstraction(index)), Matchers.is(false)); } - assertThat(predicate.test(mockIndexAbstraction(XPackPlugin.ASYNC_RESULTS_INDEX + randomAlphaOfLengthBetween(0, 2))), - Matchers.is(false)); + assertThat( + predicate.test(mockIndexAbstraction(XPackPlugin.ASYNC_RESULTS_INDEX + randomAlphaOfLengthBetween(0, 2))), + Matchers.is(false) + ); } } @@ -1511,15 +1692,19 @@ public void testAsyncSearchUserCanAccessOnlyAsyncSearchRestrictedIndices() { for (String index : RestrictedIndicesNames.RESTRICTED_NAMES) { assertThat(predicate.test(mockIndexAbstraction(index)), Matchers.is(false)); } - assertThat(predicate.test(mockIndexAbstraction(XPackPlugin.ASYNC_RESULTS_INDEX + randomAlphaOfLengthBetween(0, 3))), - Matchers.is(true)); + assertThat( + predicate.test(mockIndexAbstraction(XPackPlugin.ASYNC_RESULTS_INDEX + randomAlphaOfLengthBetween(0, 3))), + Matchers.is(true) + ); } } public void testAsyncSearchUserHasNoClusterPrivileges() { for (String action : Arrays.asList(ClusterStateAction.NAME, GetWatchAction.NAME, ClusterStatsAction.NAME, NodesStatsAction.NAME)) { - assertThat(getAsyncSearchUserRole().cluster().check(action, mock(TransportRequest.class), mock(Authentication.class)), - Matchers.is(false)); + assertThat( + getAsyncSearchUserRole().cluster().check(action, mock(TransportRequest.class), mock(Authentication.class)), + Matchers.is(false) + ); } } @@ -1536,23 +1721,44 @@ public void testAsyncSearchUserCannotWriteToAuditTrail() { assertThat(predicate.test(mockIndexAbstraction(getAuditLogName())), Matchers.is(false)); } - public void testXpackUserHasClusterPrivileges() { for (String action : Arrays.asList(ClusterStateAction.NAME, GetWatchAction.NAME, ClusterStatsAction.NAME, NodesStatsAction.NAME)) { - assertThat(getXPackUserRole().cluster().check(action, mock(TransportRequest.class), mock(Authentication.class)), - Matchers.is(true)); + assertThat( + getXPackUserRole().cluster().check(action, mock(TransportRequest.class), mock(Authentication.class)), + Matchers.is(true) + ); } } private Role getXPackUserRole() { - CompositeRolesStore compositeRolesStore = - buildCompositeRolesStore(SECURITY_ENABLED_SETTINGS, null, null, null, null, null, null, null, null, null); + CompositeRolesStore compositeRolesStore = buildCompositeRolesStore( + SECURITY_ENABLED_SETTINGS, + null, + null, + null, + null, + null, + null, + null, + null, + null + ); return compositeRolesStore.getXpackUserRole(); } private Role getAsyncSearchUserRole() { - CompositeRolesStore compositeRolesStore = - buildCompositeRolesStore(SECURITY_ENABLED_SETTINGS, null, null, null, null, null, null, null, null, null); + CompositeRolesStore compositeRolesStore = buildCompositeRolesStore( + SECURITY_ENABLED_SETTINGS, + null, + null, + null, + null, + null, + null, + null, + null, + null + ); return compositeRolesStore.getAsyncSearchUserRole(); } @@ -1596,7 +1802,8 @@ private CompositeRolesStore buildCompositeRolesStore( @Nullable ServiceAccountService serviceAccountService, @Nullable DocumentSubsetBitsetCache documentSubsetBitsetCache, @Nullable Consumer> roleConsumer, - @Nullable Consumer onInvalidation) { + @Nullable Consumer onInvalidation + ) { if (licenseState == null) { licenseState = new XPackLicenseState(() -> 0); } @@ -1613,8 +1820,8 @@ private CompositeRolesStore buildCompositeRolesStore( privilegeStore = mock(NativePrivilegeStore.class); doAnswer((invocationOnMock) -> { @SuppressWarnings("unchecked") - ActionListener> callback = - (ActionListener>) invocationOnMock.getArguments()[2]; + ActionListener> callback = (ActionListener< + Collection>) invocationOnMock.getArguments()[2]; callback.onResponse(Collections.emptyList()); return null; }).when(privilegeStore).getPrivileges(isASet(), isASet(), anyActionListener()); @@ -1629,7 +1836,7 @@ private CompositeRolesStore buildCompositeRolesStore( documentSubsetBitsetCache = buildBitsetCache(); } if (roleConsumer == null) { - roleConsumer = rds -> { }; + roleConsumer = rds -> {}; } return new CompositeRolesStore( @@ -1688,13 +1895,7 @@ private RoleProviders buildRolesProvider( if (customRoleProviders == null) { customRoleProviders = Map.of(); } - return new RoleProviders( - reservedRolesStore, - fileRolesStore, - nativeRolesStore, - customRoleProviders, - licenseState - ); + return new RoleProviders(reservedRolesStore, fileRolesStore, nativeRolesStore, customRoleProviders, licenseState); } private DocumentSubsetBitsetCache buildBitsetCache() { @@ -1731,8 +1932,7 @@ public String getWriteableName() { } @Override - public void writeTo(StreamOutput out) throws IOException { - } + public void writeTo(StreamOutput out) throws IOException {} } private String getAuditLogName() { @@ -1744,8 +1944,9 @@ private String getAuditLogName() { private IndexAbstraction mockIndexAbstraction(String name) { IndexAbstraction mock = mock(IndexAbstraction.class); when(mock.getName()).thenReturn(name); - when(mock.getType()).thenReturn(randomFrom(IndexAbstraction.Type.CONCRETE_INDEX, - IndexAbstraction.Type.ALIAS, IndexAbstraction.Type.DATA_STREAM)); + when(mock.getType()).thenReturn( + randomFrom(IndexAbstraction.Type.CONCRETE_INDEX, IndexAbstraction.Type.ALIAS, IndexAbstraction.Type.DATA_STREAM) + ); return mock; } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/store/DeprecationRoleDescriptorConsumerTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/store/DeprecationRoleDescriptorConsumerTests.java index 33e2fc98ce551..89bcea4058e8f 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/store/DeprecationRoleDescriptorConsumerTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/store/DeprecationRoleDescriptorConsumerTests.java @@ -17,21 +17,21 @@ import org.elasticsearch.common.logging.DeprecationLogger; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.mockito.Mockito; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.VersionUtils; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.xpack.core.security.authz.RoleDescriptor; import org.junit.Before; +import org.mockito.Mockito; import org.mockito.stubbing.Answer; import java.util.Arrays; import java.util.concurrent.ExecutorService; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.when; public final class DeprecationRoleDescriptorConsumerTests extends ESTestCase { @@ -54,12 +54,23 @@ public void testSimpleAliasAndIndexPair() throws Exception { final DeprecationLogger deprecationLogger = mock(DeprecationLogger.class); final Metadata.Builder metadataBuilder = Metadata.builder(); addIndex(metadataBuilder, "index", "alias"); - final RoleDescriptor roleOverAlias = new RoleDescriptor("roleOverAlias", new String[] { "read" }, - new RoleDescriptor.IndicesPrivileges[] { indexPrivileges(randomFrom("read", "write", "delete", "index"), "alias") }, null); - final RoleDescriptor roleOverIndex = new RoleDescriptor("roleOverIndex", new String[] { "manage" }, - new RoleDescriptor.IndicesPrivileges[] { indexPrivileges(randomFrom("read", "write", "delete", "index"), "index") }, null); + final RoleDescriptor roleOverAlias = new RoleDescriptor( + "roleOverAlias", + new String[] { "read" }, + new RoleDescriptor.IndicesPrivileges[] { indexPrivileges(randomFrom("read", "write", "delete", "index"), "alias") }, + null + ); + final RoleDescriptor roleOverIndex = new RoleDescriptor( + "roleOverIndex", + new String[] { "manage" }, + new RoleDescriptor.IndicesPrivileges[] { indexPrivileges(randomFrom("read", "write", "delete", "index"), "index") }, + null + ); DeprecationRoleDescriptorConsumer deprecationConsumer = new DeprecationRoleDescriptorConsumer( - mockClusterService(metadataBuilder.build()), threadPool, deprecationLogger); + mockClusterService(metadataBuilder.build()), + threadPool, + deprecationLogger + ); deprecationConsumer.accept(Arrays.asList(roleOverAlias, roleOverIndex)); verifyLogger(deprecationLogger, "roleOverAlias", "alias", "index"); verifyNoMoreInteractions(deprecationLogger); @@ -70,12 +81,17 @@ public void testRoleGrantsOnIndexAndAliasPair() throws Exception { final Metadata.Builder metadataBuilder = Metadata.builder(); addIndex(metadataBuilder, "index", "alias"); addIndex(metadataBuilder, "index1", "alias2"); - final RoleDescriptor roleOverIndexAndAlias = new RoleDescriptor("roleOverIndexAndAlias", new String[] { "manage_watcher" }, - new RoleDescriptor.IndicesPrivileges[] { - indexPrivileges(randomFrom("read", "write", "delete", "index"), "index", "alias") }, - null); + final RoleDescriptor roleOverIndexAndAlias = new RoleDescriptor( + "roleOverIndexAndAlias", + new String[] { "manage_watcher" }, + new RoleDescriptor.IndicesPrivileges[] { indexPrivileges(randomFrom("read", "write", "delete", "index"), "index", "alias") }, + null + ); DeprecationRoleDescriptorConsumer deprecationConsumer = new DeprecationRoleDescriptorConsumer( - mockClusterService(metadataBuilder.build()), threadPool, deprecationLogger); + mockClusterService(metadataBuilder.build()), + threadPool, + deprecationLogger + ); deprecationConsumer.accept(Arrays.asList(roleOverIndexAndAlias)); verifyNoMoreInteractions(deprecationLogger); } @@ -85,13 +101,17 @@ public void testMultiplePrivilegesLoggedOnce() throws Exception { final Metadata.Builder metadataBuilder = Metadata.builder(); addIndex(metadataBuilder, "index", "alias"); addIndex(metadataBuilder, "index2", "alias2"); - final RoleDescriptor roleOverAlias = new RoleDescriptor("roleOverAlias", new String[] { "manage_watcher" }, - new RoleDescriptor.IndicesPrivileges[] { - indexPrivileges("write", "alias"), - indexPrivileges("manage_ilm", "alias") }, - null); + final RoleDescriptor roleOverAlias = new RoleDescriptor( + "roleOverAlias", + new String[] { "manage_watcher" }, + new RoleDescriptor.IndicesPrivileges[] { indexPrivileges("write", "alias"), indexPrivileges("manage_ilm", "alias") }, + null + ); DeprecationRoleDescriptorConsumer deprecationConsumer = new DeprecationRoleDescriptorConsumer( - mockClusterService(metadataBuilder.build()), threadPool, deprecationLogger); + mockClusterService(metadataBuilder.build()), + threadPool, + deprecationLogger + ); deprecationConsumer.accept(Arrays.asList(roleOverAlias)); verifyLogger(deprecationLogger, "roleOverAlias", "alias", "index"); verifyNoMoreInteractions(deprecationLogger); @@ -105,15 +125,21 @@ public void testMultiplePrivilegesLoggedForEachAlias() throws Exception { addIndex(metadataBuilder, "index3", "alias3", "alias"); addIndex(metadataBuilder, "index4", "alias4", "alias"); addIndex(metadataBuilder, "foo", "bar"); - final RoleDescriptor roleMultiplePrivileges = new RoleDescriptor("roleMultiplePrivileges", new String[] { "manage_watcher" }, - new RoleDescriptor.IndicesPrivileges[] { - indexPrivileges("write", "index2", "alias"), - indexPrivileges("read", "alias4"), - indexPrivileges("delete_index", "alias3", "index"), - indexPrivileges("create_index", "alias3", "index3")}, - null); + final RoleDescriptor roleMultiplePrivileges = new RoleDescriptor( + "roleMultiplePrivileges", + new String[] { "manage_watcher" }, + new RoleDescriptor.IndicesPrivileges[] { + indexPrivileges("write", "index2", "alias"), + indexPrivileges("read", "alias4"), + indexPrivileges("delete_index", "alias3", "index"), + indexPrivileges("create_index", "alias3", "index3") }, + null + ); DeprecationRoleDescriptorConsumer deprecationConsumer = new DeprecationRoleDescriptorConsumer( - mockClusterService(metadataBuilder.build()), threadPool, deprecationLogger); + mockClusterService(metadataBuilder.build()), + threadPool, + deprecationLogger + ); deprecationConsumer.accept(Arrays.asList(roleMultiplePrivileges)); verifyLogger(deprecationLogger, "roleMultiplePrivileges", "alias", "index, index3, index4"); verifyLogger(deprecationLogger, "roleMultiplePrivileges", "alias4", "index2, index4"); @@ -126,13 +152,19 @@ public void testPermissionsOverlapping() throws Exception { addIndex(metadataBuilder, "index1", "alias1", "bar"); addIndex(metadataBuilder, "index2", "alias2", "baz"); addIndex(metadataBuilder, "foo", "bar"); - final RoleDescriptor roleOverAliasAndIndex = new RoleDescriptor("roleOverAliasAndIndex", new String[] { "read_ilm" }, - new RoleDescriptor.IndicesPrivileges[] { - indexPrivileges("monitor", "index2", "alias1"), - indexPrivileges("monitor", "index1", "alias2")}, - null); + final RoleDescriptor roleOverAliasAndIndex = new RoleDescriptor( + "roleOverAliasAndIndex", + new String[] { "read_ilm" }, + new RoleDescriptor.IndicesPrivileges[] { + indexPrivileges("monitor", "index2", "alias1"), + indexPrivileges("monitor", "index1", "alias2") }, + null + ); DeprecationRoleDescriptorConsumer deprecationConsumer = new DeprecationRoleDescriptorConsumer( - mockClusterService(metadataBuilder.build()), threadPool, deprecationLogger); + mockClusterService(metadataBuilder.build()), + threadPool, + deprecationLogger + ); deprecationConsumer.accept(Arrays.asList(roleOverAliasAndIndex)); verifyNoMoreInteractions(deprecationLogger); } @@ -143,20 +175,29 @@ public void testOverlappingAcrossMultipleRoleDescriptors() throws Exception { addIndex(metadataBuilder, "index1", "alias1", "bar"); addIndex(metadataBuilder, "index2", "alias2", "baz"); addIndex(metadataBuilder, "foo", "bar"); - final RoleDescriptor role1 = new RoleDescriptor("role1", new String[] { "monitor_watcher" }, - new RoleDescriptor.IndicesPrivileges[] { - indexPrivileges("monitor", "index2", "alias1")}, - null); - final RoleDescriptor role2 = new RoleDescriptor("role2", new String[] { "read_ccr" }, - new RoleDescriptor.IndicesPrivileges[] { - indexPrivileges("monitor", "index1", "alias2")}, - null); - final RoleDescriptor role3 = new RoleDescriptor("role3", new String[] { "monitor_ml" }, - new RoleDescriptor.IndicesPrivileges[] { - indexPrivileges("index", "bar")}, - null); + final RoleDescriptor role1 = new RoleDescriptor( + "role1", + new String[] { "monitor_watcher" }, + new RoleDescriptor.IndicesPrivileges[] { indexPrivileges("monitor", "index2", "alias1") }, + null + ); + final RoleDescriptor role2 = new RoleDescriptor( + "role2", + new String[] { "read_ccr" }, + new RoleDescriptor.IndicesPrivileges[] { indexPrivileges("monitor", "index1", "alias2") }, + null + ); + final RoleDescriptor role3 = new RoleDescriptor( + "role3", + new String[] { "monitor_ml" }, + new RoleDescriptor.IndicesPrivileges[] { indexPrivileges("index", "bar") }, + null + ); DeprecationRoleDescriptorConsumer deprecationConsumer = new DeprecationRoleDescriptorConsumer( - mockClusterService(metadataBuilder.build()), threadPool, deprecationLogger); + mockClusterService(metadataBuilder.build()), + threadPool, + deprecationLogger + ); deprecationConsumer.accept(Arrays.asList(role1, role2, role3)); verifyLogger(deprecationLogger, "role1", "alias1", "index1"); verifyLogger(deprecationLogger, "role2", "alias2", "index2"); @@ -171,12 +212,17 @@ public void testDailyRoleCaching() throws Exception { addIndex(metadataBuilder, "index2", "alias2", "baz"); addIndex(metadataBuilder, "foo", "bar"); final Metadata metadata = metadataBuilder.build(); - RoleDescriptor someRole = new RoleDescriptor("someRole", new String[] { "monitor_rollup" }, - new RoleDescriptor.IndicesPrivileges[] { - indexPrivileges("monitor", "i*", "bar")}, - null); - final DeprecationRoleDescriptorConsumer deprecationConsumer = new DeprecationRoleDescriptorConsumer(mockClusterService(metadata), - threadPool, deprecationLogger); + RoleDescriptor someRole = new RoleDescriptor( + "someRole", + new String[] { "monitor_rollup" }, + new RoleDescriptor.IndicesPrivileges[] { indexPrivileges("monitor", "i*", "bar") }, + null + ); + final DeprecationRoleDescriptorConsumer deprecationConsumer = new DeprecationRoleDescriptorConsumer( + mockClusterService(metadata), + threadPool, + deprecationLogger + ); final String cacheKeyBefore = DeprecationRoleDescriptorConsumer.buildCacheKey(someRole); deprecationConsumer.accept(Arrays.asList(someRole)); verifyLogger(deprecationLogger, "someRole", "bar", "foo"); @@ -188,10 +234,12 @@ public void testDailyRoleCaching() throws Exception { return; } verifyNoMoreInteractions(deprecationLogger); - RoleDescriptor differentRoleSameName = new RoleDescriptor("someRole", new String[] { "manage_pipeline" }, - new RoleDescriptor.IndicesPrivileges[] { - indexPrivileges("write", "i*", "baz")}, - null); + RoleDescriptor differentRoleSameName = new RoleDescriptor( + "someRole", + new String[] { "manage_pipeline" }, + new RoleDescriptor.IndicesPrivileges[] { indexPrivileges("write", "i*", "baz") }, + null + ); deprecationConsumer.accept(Arrays.asList(differentRoleSameName)); final String cacheKeyAfterParty = DeprecationRoleDescriptorConsumer.buildCacheKey(differentRoleSameName); // we don't do this test if it crosses days @@ -210,35 +258,50 @@ public void testWildcards() throws Exception { addIndex(metadataBuilder, "index4", "alias", "alias4"); addIndex(metadataBuilder, "foo", "bar", "baz"); Metadata metadata = metadataBuilder.build(); - final RoleDescriptor roleGlobalWildcard = new RoleDescriptor("roleGlobalWildcard", new String[] { "manage_token" }, - new RoleDescriptor.IndicesPrivileges[] { - indexPrivileges(randomFrom("write", "delete_index", "read_cross_cluster"), "*")}, - null); - new DeprecationRoleDescriptorConsumer(mockClusterService(metadata), threadPool, deprecationLogger) - .accept(Arrays.asList(roleGlobalWildcard)); + final RoleDescriptor roleGlobalWildcard = new RoleDescriptor( + "roleGlobalWildcard", + new String[] { "manage_token" }, + new RoleDescriptor.IndicesPrivileges[] { indexPrivileges(randomFrom("write", "delete_index", "read_cross_cluster"), "*") }, + null + ); + new DeprecationRoleDescriptorConsumer(mockClusterService(metadata), threadPool, deprecationLogger).accept( + Arrays.asList(roleGlobalWildcard) + ); verifyNoMoreInteractions(deprecationLogger); - final RoleDescriptor roleGlobalWildcard2 = new RoleDescriptor("roleGlobalWildcard2", new String[] { "manage_index_templates" }, - new RoleDescriptor.IndicesPrivileges[] { - indexPrivileges(randomFrom("write", "delete_index", "read_cross_cluster"), "i*", "a*")}, - null); - new DeprecationRoleDescriptorConsumer(mockClusterService(metadata), threadPool, deprecationLogger) - .accept(Arrays.asList(roleGlobalWildcard2)); + final RoleDescriptor roleGlobalWildcard2 = new RoleDescriptor( + "roleGlobalWildcard2", + new String[] { "manage_index_templates" }, + new RoleDescriptor.IndicesPrivileges[] { + indexPrivileges(randomFrom("write", "delete_index", "read_cross_cluster"), "i*", "a*") }, + null + ); + new DeprecationRoleDescriptorConsumer(mockClusterService(metadata), threadPool, deprecationLogger).accept( + Arrays.asList(roleGlobalWildcard2) + ); verifyNoMoreInteractions(deprecationLogger); - final RoleDescriptor roleWildcardOnIndices = new RoleDescriptor("roleWildcardOnIndices", new String[] { "manage_watcher" }, - new RoleDescriptor.IndicesPrivileges[] { - indexPrivileges("write", "index*", "alias", "alias3"), - indexPrivileges("read", "foo")}, - null); - new DeprecationRoleDescriptorConsumer(mockClusterService(metadata), threadPool, deprecationLogger) - .accept(Arrays.asList(roleWildcardOnIndices)); + final RoleDescriptor roleWildcardOnIndices = new RoleDescriptor( + "roleWildcardOnIndices", + new String[] { "manage_watcher" }, + new RoleDescriptor.IndicesPrivileges[] { + indexPrivileges("write", "index*", "alias", "alias3"), + indexPrivileges("read", "foo") }, + null + ); + new DeprecationRoleDescriptorConsumer(mockClusterService(metadata), threadPool, deprecationLogger).accept( + Arrays.asList(roleWildcardOnIndices) + ); verifyNoMoreInteractions(deprecationLogger); - final RoleDescriptor roleWildcardOnAliases = new RoleDescriptor("roleWildcardOnAliases", new String[] { "manage_watcher" }, - new RoleDescriptor.IndicesPrivileges[] { - indexPrivileges("write", "alias*", "index", "index3"), - indexPrivileges("read", "foo", "index2")}, - null); - new DeprecationRoleDescriptorConsumer(mockClusterService(metadata), threadPool, deprecationLogger) - .accept(Arrays.asList(roleWildcardOnAliases)); + final RoleDescriptor roleWildcardOnAliases = new RoleDescriptor( + "roleWildcardOnAliases", + new String[] { "manage_watcher" }, + new RoleDescriptor.IndicesPrivileges[] { + indexPrivileges("write", "alias*", "index", "index3"), + indexPrivileges("read", "foo", "index2") }, + null + ); + new DeprecationRoleDescriptorConsumer(mockClusterService(metadata), threadPool, deprecationLogger).accept( + Arrays.asList(roleWildcardOnAliases) + ); verifyLogger(deprecationLogger, "roleWildcardOnAliases", "alias", "index2, index4"); verifyLogger(deprecationLogger, "roleWildcardOnAliases", "alias2", "index2"); verifyLogger(deprecationLogger, "roleWildcardOnAliases", "alias4", "index2, index4"); @@ -251,19 +314,26 @@ public void testMultipleIndicesSameAlias() throws Exception { addIndex(metadataBuilder, "index1", "alias1"); addIndex(metadataBuilder, "index2", "alias1", "alias2"); addIndex(metadataBuilder, "index3", "alias2"); - final RoleDescriptor roleOverAliasAndIndex = new RoleDescriptor("roleOverAliasAndIndex", new String[] { "manage_ml" }, - new RoleDescriptor.IndicesPrivileges[] { - indexPrivileges("delete_index", "alias1", "index1") }, - null); + final RoleDescriptor roleOverAliasAndIndex = new RoleDescriptor( + "roleOverAliasAndIndex", + new String[] { "manage_ml" }, + new RoleDescriptor.IndicesPrivileges[] { indexPrivileges("delete_index", "alias1", "index1") }, + null + ); DeprecationRoleDescriptorConsumer deprecationConsumer = new DeprecationRoleDescriptorConsumer( - mockClusterService(metadataBuilder.build()), threadPool, deprecationLogger); + mockClusterService(metadataBuilder.build()), + threadPool, + deprecationLogger + ); deprecationConsumer.accept(Arrays.asList(roleOverAliasAndIndex)); verifyLogger(deprecationLogger, "roleOverAliasAndIndex", "alias1", "index2"); verifyNoMoreInteractions(deprecationLogger); - final RoleDescriptor roleOverAliases = new RoleDescriptor("roleOverAliases", new String[] { "manage_security" }, - new RoleDescriptor.IndicesPrivileges[] { - indexPrivileges("monitor", "alias1", "alias2") }, - null); + final RoleDescriptor roleOverAliases = new RoleDescriptor( + "roleOverAliases", + new String[] { "manage_security" }, + new RoleDescriptor.IndicesPrivileges[] { indexPrivileges("monitor", "alias1", "alias2") }, + null + ); deprecationConsumer.accept(Arrays.asList(roleOverAliases)); verifyLogger(deprecationLogger, "roleOverAliases", "alias1", "index1, index2"); verifyLogger(deprecationLogger, "roleOverAliases", "alias2", "index2, index3"); @@ -272,9 +342,9 @@ public void testMultipleIndicesSameAlias() throws Exception { private void addIndex(Metadata.Builder metadataBuilder, String index, String... aliases) { final IndexMetadata.Builder indexMetadataBuilder = IndexMetadata.builder(index) - .settings(Settings.builder().put("index.version.created", VersionUtils.randomVersion(random()))) - .numberOfShards(1) - .numberOfReplicas(1); + .settings(Settings.builder().put("index.version.created", VersionUtils.randomVersion(random()))) + .numberOfShards(1) + .numberOfReplicas(1); for (final String alias : aliases) { indexMetadataBuilder.putAlias(AliasMetadata.builder(alias).build()); } @@ -290,18 +360,26 @@ private ClusterService mockClusterService(Metadata metadata) { private RoleDescriptor.IndicesPrivileges indexPrivileges(String priv, String... indicesOrAliases) { return RoleDescriptor.IndicesPrivileges.builder() - .indices(indicesOrAliases) - .privileges(priv) - .grantedFields(randomArray(0, 2, String[]::new, () -> randomBoolean() ? null : randomAlphaOfLengthBetween(1, 4))) - .query(randomBoolean() ? null : "{ }") - .build(); + .indices(indicesOrAliases) + .privileges(priv) + .grantedFields(randomArray(0, 2, String[]::new, () -> randomBoolean() ? null : randomAlphaOfLengthBetween(1, 4))) + .query(randomBoolean() ? null : "{ }") + .build(); } private void verifyLogger(DeprecationLogger deprecationLogger, String roleName, String aliasName, String indexNames) { - verify(deprecationLogger).critical(DeprecationCategory.SECURITY, "index_permissions_on_alias", - "Role [" + roleName + "] contains index privileges covering the [" + aliasName - + "] alias but which do not cover some of the indices that it points to [" + indexNames + "]. Granting privileges over an" + verify(deprecationLogger).critical( + DeprecationCategory.SECURITY, + "index_permissions_on_alias", + "Role [" + + roleName + + "] contains index privileges covering the [" + + aliasName + + "] alias but which do not cover some of the indices that it points to [" + + indexNames + + "]. Granting privileges over an" + " alias and hence granting privileges over all the indices that the alias points to is deprecated and will be removed" - + " in a future version of Elasticsearch. Instead define permissions exclusively on index names or index name patterns."); + + " in a future version of Elasticsearch. Instead define permissions exclusively on index names or index name patterns." + ); } } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/store/FileRolesStoreTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/store/FileRolesStoreTests.java index ccadd917df61a..5d5ccb2792b51 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/store/FileRolesStoreTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/store/FileRolesStoreTests.java @@ -11,20 +11,20 @@ import org.apache.lucene.util.automaton.Automaton; import org.apache.lucene.util.automaton.MinimizationOperations; import org.apache.lucene.util.automaton.Operations; -import org.elasticsearch.license.MockLicenseState; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.env.Environment; import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.index.query.MatchAllQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; +import org.elasticsearch.license.MockLicenseState; import org.elasticsearch.license.TestUtils; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.threadpool.TestThreadPool; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportRequest; import org.elasticsearch.watcher.ResourceWatcherService; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.xpack.core.security.audit.logfile.CapturingLogger; import org.elasticsearch.xpack.core.security.authc.Authentication; @@ -75,15 +75,26 @@ public class FileRolesStoreTests extends ESTestCase { @Override protected NamedXContentRegistry xContentRegistry() { - return new NamedXContentRegistry(singletonList(new NamedXContentRegistry.Entry(QueryBuilder.class, - new ParseField(MatchAllQueryBuilder.NAME), (p, c) -> MatchAllQueryBuilder.fromXContent(p)))); + return new NamedXContentRegistry( + singletonList( + new NamedXContentRegistry.Entry( + QueryBuilder.class, + new ParseField(MatchAllQueryBuilder.NAME), + (p, c) -> MatchAllQueryBuilder.fromXContent(p) + ) + ) + ); } public void testParseFile() throws Exception { Path path = getDataPath("roles.yml"); - Map roles = FileRolesStore.parseFile(path, logger, Settings.builder() - .put(XPackSettings.DLS_FLS_ENABLED.getKey(), true) - .build(), TestUtils.newTestLicenseState(), xContentRegistry()); + Map roles = FileRolesStore.parseFile( + path, + logger, + Settings.builder().put(XPackSettings.DLS_FLS_ENABLED.getKey(), true).build(), + TestUtils.newTestLicenseState(), + xContentRegistry() + ); assertThat(roles, notNullValue()); assertThat(roles.size(), is(9)); @@ -155,9 +166,15 @@ public void testParseFile() throws Exception { assertThat(group.indices().length, is(1)); assertThat(group.indices()[0], equalTo("/.*_.*/")); assertThat(group.privilege(), notNullValue()); - assertTrue(Operations.sameLanguage(group.privilege().getAutomaton(), - MinimizationOperations.minimize(Operations.union(IndexPrivilege.READ.getAutomaton(), IndexPrivilege.WRITE.getAutomaton()), - Operations.DEFAULT_DETERMINIZE_WORK_LIMIT))); + assertTrue( + Operations.sameLanguage( + group.privilege().getAutomaton(), + MinimizationOperations.minimize( + Operations.union(IndexPrivilege.READ.getAutomaton(), IndexPrivilege.WRITE.getAutomaton()), + Operations.DEFAULT_DETERMINIZE_WORK_LIMIT + ) + ) + ); descriptor = roles.get("role4"); assertNull(descriptor); @@ -262,9 +279,13 @@ public void testParseFileWithFLSAndDLSDisabled() throws Exception { Logger logger = CapturingLogger.newCapturingLogger(Level.ERROR, null); List events = CapturingLogger.output(logger.getName(), Level.ERROR); events.clear(); - Map roles = FileRolesStore.parseFile(path, logger, Settings.builder() - .put(XPackSettings.DLS_FLS_ENABLED.getKey(), false) - .build(), TestUtils.newTestLicenseState(), xContentRegistry()); + Map roles = FileRolesStore.parseFile( + path, + logger, + Settings.builder().put(XPackSettings.DLS_FLS_ENABLED.getKey(), false).build(), + TestUtils.newTestLicenseState(), + xContentRegistry() + ); assertThat(roles, notNullValue()); assertThat(roles.size(), is(6)); assertThat(roles.get("role_fields"), nullValue()); @@ -274,18 +295,37 @@ public void testParseFileWithFLSAndDLSDisabled() throws Exception { assertThat(events, hasSize(4)); assertThat( - events.get(0), - startsWith("invalid role definition [role_fields] in roles file [" + path.toAbsolutePath() + - "]. document and field level security is not enabled.")); - assertThat(events.get(1), - startsWith("invalid role definition [role_query] in roles file [" + path.toAbsolutePath() + - "]. document and field level security is not enabled.")); - assertThat(events.get(2), - startsWith("invalid role definition [role_query_fields] in roles file [" + path.toAbsolutePath() + - "]. document and field level security is not enabled.")); - assertThat(events.get(3), - startsWith("invalid role definition [role_query_invalid] in roles file [" + path.toAbsolutePath() + - "]. document and field level security is not enabled.")); + events.get(0), + startsWith( + "invalid role definition [role_fields] in roles file [" + + path.toAbsolutePath() + + "]. document and field level security is not enabled." + ) + ); + assertThat( + events.get(1), + startsWith( + "invalid role definition [role_query] in roles file [" + + path.toAbsolutePath() + + "]. document and field level security is not enabled." + ) + ); + assertThat( + events.get(2), + startsWith( + "invalid role definition [role_query_fields] in roles file [" + + path.toAbsolutePath() + + "]. document and field level security is not enabled." + ) + ); + assertThat( + events.get(3), + startsWith( + "invalid role definition [role_query_invalid] in roles file [" + + path.toAbsolutePath() + + "]. document and field level security is not enabled." + ) + ); } public void testParseFileWithFLSAndDLSUnlicensed() throws Exception { @@ -304,13 +344,19 @@ public void testParseFileWithFLSAndDLSUnlicensed() throws Exception { assertThat(events, hasSize(3)); assertThat( - events.get(0), - startsWith("role [role_fields] uses document and/or field level security, which is not enabled by the current license")); - assertThat(events.get(1), - startsWith("role [role_query] uses document and/or field level security, which is not enabled by the current license")); - assertThat(events.get(2), - startsWith("role [role_query_fields] uses document and/or field level security, which is not enabled by the current " + - "license")); + events.get(0), + startsWith("role [role_fields] uses document and/or field level security, which is not enabled by the current license") + ); + assertThat( + events.get(1), + startsWith("role [role_query] uses document and/or field level security, which is not enabled by the current license") + ); + assertThat( + events.get(2), + startsWith( + "role [role_query_fields] uses document and/or field level security, which is not enabled by the current " + "license" + ) + ); } /** @@ -319,8 +365,13 @@ public void testParseFileWithFLSAndDLSUnlicensed() throws Exception { public void testDefaultRolesFile() throws Exception { // TODO we should add the config dir to the resources so we don't copy this stuff around... Path path = getDataPath("default_roles.yml"); - Map roles = FileRolesStore.parseFile(path, logger, Settings.EMPTY, TestUtils.newTestLicenseState(), - xContentRegistry()); + Map roles = FileRolesStore.parseFile( + path, + logger, + Settings.EMPTY, + TestUtils.newTestLicenseState(), + xContentRegistry() + ); assertThat(roles, notNullValue()); assertThat(roles.size(), is(0)); } @@ -338,9 +389,7 @@ public void testAutoReload() throws Exception { Files.copy(roles, stream); } - Settings.Builder builder = Settings.builder() - .put("resource.reload.interval.high", "100ms") - .put("path.home", home); + Settings.Builder builder = Settings.builder().put("resource.reload.interval.high", "100ms").put("path.home", home); Settings settings = builder.build(); Environment env = TestEnvironment.newEnvironment(settings); threadPool = new TestThreadPool("test"); @@ -348,9 +397,9 @@ public void testAutoReload() throws Exception { final CountDownLatch latch = new CountDownLatch(1); final Set modifiedRoles = new HashSet<>(); FileRolesStore store = new FileRolesStore(settings, env, watcherService, roleSet -> { - modifiedRoles.addAll(roleSet); - latch.countDown(); - }, TestUtils.newTestLicenseState(), xContentRegistry()); + modifiedRoles.addAll(roleSet); + latch.countDown(); + }, TestUtils.newTestLicenseState(), xContentRegistry()); Set descriptors = store.roleDescriptors(Collections.singleton("role1")); assertThat(descriptors, notNullValue()); @@ -425,7 +474,7 @@ public void testAutoReload() throws Exception { descriptors = store.roleDescriptors(Collections.singleton("role5")); assertThat(descriptors, notNullValue()); assertEquals(1, descriptors.size()); - assertArrayEquals(new String[]{"MONITOR"}, descriptors.iterator().next().getClusterPrivileges()); + assertArrayEquals(new String[] { "MONITOR" }, descriptors.iterator().next().getClusterPrivileges()); // modify final Set modifiedFileRolesModified = new HashSet<>(); @@ -451,7 +500,7 @@ public void testAutoReload() throws Exception { descriptors = store.roleDescriptors(Collections.singleton("role5")); assertThat(descriptors, notNullValue()); assertEquals(1, descriptors.size()); - assertArrayEquals(new String[]{"ALL"}, descriptors.iterator().next().getClusterPrivileges()); + assertArrayEquals(new String[] { "ALL" }, descriptors.iterator().next().getClusterPrivileges()); } finally { if (watcherService != null) { watcherService.close(); @@ -463,8 +512,13 @@ public void testAutoReload() throws Exception { public void testThatEmptyFileDoesNotResultInLoop() throws Exception { Path file = createTempFile(); Files.write(file, Collections.singletonList("#"), StandardCharsets.UTF_8); - Map roles = FileRolesStore.parseFile(file, logger, Settings.EMPTY, TestUtils.newTestLicenseState(), - xContentRegistry()); + Map roles = FileRolesStore.parseFile( + file, + logger, + Settings.EMPTY, + TestUtils.newTestLicenseState(), + xContentRegistry() + ); assertThat(roles.keySet(), is(empty())); } @@ -473,8 +527,13 @@ public void testThatInvalidRoleDefinitions() throws Exception { Logger logger = CapturingLogger.newCapturingLogger(Level.ERROR, null); List entries = CapturingLogger.output(logger.getName(), Level.ERROR); entries.clear(); - Map roles = FileRolesStore.parseFile(path, logger, Settings.EMPTY, TestUtils.newTestLicenseState(), - xContentRegistry()); + Map roles = FileRolesStore.parseFile( + path, + logger, + Settings.EMPTY, + TestUtils.newTestLicenseState(), + xContentRegistry() + ); assertThat(roles.size(), is(1)); assertThat(roles, hasKey("valid_role")); RoleDescriptor descriptor = roles.get("valid_role"); @@ -485,11 +544,10 @@ public void testThatInvalidRoleDefinitions() throws Exception { assertThat(entries, hasSize(6)); assertThat( - entries.get(0), - startsWith("invalid role definition [fóóbár] in roles file [" + path.toAbsolutePath() + "]. invalid role name")); - assertThat( - entries.get(1), - startsWith("invalid role definition [role1] in roles file [" + path.toAbsolutePath() + "]")); + entries.get(0), + startsWith("invalid role definition [fóóbár] in roles file [" + path.toAbsolutePath() + "]. invalid role name") + ); + assertThat(entries.get(1), startsWith("invalid role definition [role1] in roles file [" + path.toAbsolutePath() + "]")); assertThat(entries.get(2), startsWith("failed to parse role [role2]")); assertThat(entries.get(3), startsWith("failed to parse role [role3]")); assertThat(entries.get(4), startsWith("failed to parse role [role4]")); @@ -507,8 +565,9 @@ public void testThatRoleNamesDoesNotResolvePermissions() throws Exception { assertThat(events, hasSize(1)); assertThat( - events.get(0), - startsWith("invalid role definition [fóóbár] in roles file [" + path.toAbsolutePath() + "]. invalid role name")); + events.get(0), + startsWith("invalid role definition [fóóbár] in roles file [" + path.toAbsolutePath() + "]. invalid role name") + ); } public void testReservedRoles() throws Exception { @@ -516,8 +575,13 @@ public void testReservedRoles() throws Exception { List events = CapturingLogger.output(logger.getName(), Level.ERROR); events.clear(); Path path = getDataPath("reserved_roles.yml"); - Map roles = FileRolesStore.parseFile(path, logger, Settings.EMPTY, TestUtils.newTestLicenseState(), - xContentRegistry()); + Map roles = FileRolesStore.parseFile( + path, + logger, + Settings.EMPTY, + TestUtils.newTestLicenseState(), + xContentRegistry() + ); assertThat(roles, notNullValue()); assertThat(roles.size(), is(1)); @@ -543,13 +607,18 @@ public void testUsageStats() throws Exception { final boolean flsDlsEnabled = randomBoolean(); Settings settings = Settings.builder() - .put("resource.reload.interval.high", "500ms") - .put("path.home", home) - .put(XPackSettings.DLS_FLS_ENABLED.getKey(), flsDlsEnabled) - .build(); + .put("resource.reload.interval.high", "500ms") + .put("path.home", home) + .put(XPackSettings.DLS_FLS_ENABLED.getKey(), flsDlsEnabled) + .build(); Environment env = TestEnvironment.newEnvironment(settings); - FileRolesStore store = new FileRolesStore(settings, env, mock(ResourceWatcherService.class), TestUtils.newTestLicenseState(), - xContentRegistry()); + FileRolesStore store = new FileRolesStore( + settings, + env, + mock(ResourceWatcherService.class), + TestUtils.newTestLicenseState(), + xContentRegistry() + ); Map usageStats = store.usageStats(); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/store/NativePrivilegeStoreTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/store/NativePrivilegeStoreTests.java index 762fd163401d6..bf8bcc5dcdcaf 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/store/NativePrivilegeStoreTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/store/NativePrivilegeStoreTests.java @@ -29,13 +29,13 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.CollectionUtils; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.client.NoOpClient; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.security.action.privilege.ClearPrivilegesCacheRequest; import org.elasticsearch.xpack.core.security.authz.privilege.ApplicationPrivilegeDescriptor; import org.elasticsearch.xpack.core.security.index.RestrictedIndicesNames; @@ -98,8 +98,11 @@ public void setup() { client = new NoOpClient(getTestName()) { @Override @SuppressWarnings("unchecked") - protected - void doExecute(ActionType action, Request request, ActionListener listener) { + protected void doExecute( + ActionType action, + Request request, + ActionListener listener + ) { NativePrivilegeStoreTests.this.requests.add(request); NativePrivilegeStoreTests.this.listener.set((ActionListener) listener); } @@ -131,9 +134,8 @@ public void cleanup() { public void testGetSinglePrivilegeByName() throws Exception { final List sourcePrivileges = List.of( - new ApplicationPrivilegeDescriptor("myapp", "admin", - newHashSet("action:admin/*", "action:login", "data:read/*"), emptyMap() - )); + new ApplicationPrivilegeDescriptor("myapp", "admin", newHashSet("action:admin/*", "action:login", "data:read/*"), emptyMap()) + ); final PlainActionFuture> future = new PlainActionFuture<>(); store.getPrivileges(List.of("myapp"), List.of("admin"), future); @@ -145,10 +147,27 @@ public void testGetSinglePrivilegeByName() throws Exception { assertThat(query, containsString("{\"term\":{\"type\":{\"value\":\"application-privilege\"")); final SearchHit[] hits = buildHits(sourcePrivileges); - listener.get().onResponse(new SearchResponse(new SearchResponseSections( - new SearchHits(hits, new TotalHits(hits.length, TotalHits.Relation.EQUAL_TO), 0f), - null, null, false, false, null, 1), - "_scrollId1", 1, 1, 0, 1, null, null)); + listener.get() + .onResponse( + new SearchResponse( + new SearchResponseSections( + new SearchHits(hits, new TotalHits(hits.length, TotalHits.Relation.EQUAL_TO), 0f), + null, + null, + false, + false, + null, + 1 + ), + "_scrollId1", + 1, + 1, + 0, + 1, + null, + null + ) + ); assertResult(sourcePrivileges, future); } @@ -157,10 +176,27 @@ public void testGetMissingPrivilege() throws InterruptedException, ExecutionExce final PlainActionFuture> future = new PlainActionFuture<>(); store.getPrivileges(List.of("myapp"), List.of("admin"), future); final SearchHit[] hits = new SearchHit[0]; - listener.get().onResponse(new SearchResponse(new SearchResponseSections( - new SearchHits(hits, new TotalHits(hits.length, TotalHits.Relation.EQUAL_TO), 0f), - null, null, false, false, null, 1), - "_scrollId1", 1, 1, 0, 1, null, null)); + listener.get() + .onResponse( + new SearchResponse( + new SearchResponseSections( + new SearchHits(hits, new TotalHits(hits.length, TotalHits.Relation.EQUAL_TO), 0f), + null, + null, + false, + false, + null, + 1 + ), + "_scrollId1", + 1, + 1, + 0, + 1, + null, + null + ) + ); final Collection applicationPrivilegeDescriptors = future.get(1, TimeUnit.SECONDS); assertThat(applicationPrivilegeDescriptors, empty()); @@ -181,16 +217,37 @@ public void testGetPrivilegesByApplicationName() throws Exception { assertThat(request.indices(), arrayContaining(RestrictedIndicesNames.SECURITY_MAIN_ALIAS)); final String query = Strings.toString(request.source().query()); - assertThat(query, anyOf( - containsString("{\"terms\":{\"application\":[\"myapp\",\"yourapp\"]"), - containsString("{\"terms\":{\"application\":[\"yourapp\",\"myapp\"]"))); + assertThat( + query, + anyOf( + containsString("{\"terms\":{\"application\":[\"myapp\",\"yourapp\"]"), + containsString("{\"terms\":{\"application\":[\"yourapp\",\"myapp\"]") + ) + ); assertThat(query, containsString("{\"term\":{\"type\":{\"value\":\"application-privilege\"")); final SearchHit[] hits = buildHits(sourcePrivileges); - listener.get().onResponse(new SearchResponse(new SearchResponseSections( - new SearchHits(hits, new TotalHits(hits.length, TotalHits.Relation.EQUAL_TO), 0f), - null, null, false, false, null, 1), - "_scrollId1", 1, 1, 0, 1, null, null)); + listener.get() + .onResponse( + new SearchResponse( + new SearchResponseSections( + new SearchHits(hits, new TotalHits(hits.length, TotalHits.Relation.EQUAL_TO), 0f), + null, + null, + false, + false, + null, + 1 + ), + "_scrollId1", + 1, + 1, + 0, + 1, + null, + null + ) + ); assertResult(sourcePrivileges, future); } @@ -209,10 +266,27 @@ public void testGetPrivilegesByWildcardApplicationName() throws Exception { assertThat(query, containsString("{\"term\":{\"type\":{\"value\":\"application-privilege\"")); final SearchHit[] hits = new SearchHit[0]; - listener.get().onResponse(new SearchResponse(new SearchResponseSections( - new SearchHits(hits, new TotalHits(hits.length, TotalHits.Relation.EQUAL_TO), 0f), - null, null, false, false, null, 1), - "_scrollId1", 1, 1, 0, 1, null, null)); + listener.get() + .onResponse( + new SearchResponse( + new SearchResponseSections( + new SearchHits(hits, new TotalHits(hits.length, TotalHits.Relation.EQUAL_TO), 0f), + null, + null, + false, + false, + null, + 1 + ), + "_scrollId1", + 1, + 1, + 0, + 1, + null, + null + ) + ); } public void testGetPrivilegesByStarApplicationName() throws Exception { @@ -228,10 +302,27 @@ public void testGetPrivilegesByStarApplicationName() throws Exception { assertThat(query, containsString("{\"term\":{\"type\":{\"value\":\"application-privilege\"")); final SearchHit[] hits = new SearchHit[0]; - listener.get().onResponse(new SearchResponse(new SearchResponseSections( - new SearchHits(hits, new TotalHits(hits.length, TotalHits.Relation.EQUAL_TO), 0f), - null, null, false, false, null, 1), - "_scrollId1", 1, 1, 0, 1, null, null)); + listener.get() + .onResponse( + new SearchResponse( + new SearchResponseSections( + new SearchHits(hits, new TotalHits(hits.length, TotalHits.Relation.EQUAL_TO), 0f), + null, + null, + false, + false, + null, + 1 + ), + "_scrollId1", + 1, + 1, + 0, + 1, + null, + null + ) + ); } public void testGetAllPrivileges() throws Exception { @@ -253,10 +344,27 @@ public void testGetAllPrivileges() throws Exception { assertThat(query, not(containsString("{\"terms\""))); final SearchHit[] hits = buildHits(sourcePrivileges); - listener.get().onResponse(new SearchResponse(new SearchResponseSections( - new SearchHits(hits, new TotalHits(hits.length, TotalHits.Relation.EQUAL_TO), 0f), - null, null, false, false, null, 1), - "_scrollId1", 1, 1, 0, 1, null, null)); + listener.get() + .onResponse( + new SearchResponse( + new SearchResponseSections( + new SearchHits(hits, new TotalHits(hits.length, TotalHits.Relation.EQUAL_TO), 0f), + null, + null, + false, + false, + null, + 1 + ), + "_scrollId1", + 1, + 1, + 0, + 1, + null, + null + ) + ); assertResult(sourcePrivileges, future); } @@ -272,10 +380,27 @@ public void testGetPrivilegesCacheByApplicationNames() throws Exception { store.getPrivileges(List.of("myapp", "yourapp"), null, future); final SearchHit[] hits = buildHits(sourcePrivileges); - listener.get().onResponse(new SearchResponse(new SearchResponseSections( - new SearchHits(hits, new TotalHits(hits.length, TotalHits.Relation.EQUAL_TO), 0f), - null, null, false, false, null, 1), - "_scrollId1", 1, 1, 0, 1, null, null)); + listener.get() + .onResponse( + new SearchResponse( + new SearchResponseSections( + new SearchHits(hits, new TotalHits(hits.length, TotalHits.Relation.EQUAL_TO), 0f), + null, + null, + false, + false, + null, + 1 + ), + "_scrollId1", + 1, + 1, + 0, + 1, + null, + null + ) + ); assertEquals(Set.of("myapp"), store.getApplicationNamesCache().get(Set.of("myapp", "yourapp"))); assertEquals(Set.copyOf(sourcePrivileges), store.getDescriptorsCache().get("myapp")); @@ -307,10 +432,27 @@ public void testGetPrivilegesCacheWithApplicationAndPrivilegeName() throws Excep store.getPrivileges(Collections.singletonList("myapp"), singletonList("user"), future); final SearchHit[] hits = buildHits(sourcePrivileges); - listener.get().onResponse(new SearchResponse(new SearchResponseSections( - new SearchHits(hits, new TotalHits(hits.length, TotalHits.Relation.EQUAL_TO), 0f), - null, null, false, false, null, 1), - "_scrollId1", 1, 1, 0, 1, null, null)); + listener.get() + .onResponse( + new SearchResponse( + new SearchResponseSections( + new SearchHits(hits, new TotalHits(hits.length, TotalHits.Relation.EQUAL_TO), 0f), + null, + null, + false, + false, + null, + 1 + ), + "_scrollId1", + 1, + 1, + 0, + 1, + null, + null + ) + ); // Not caching names with no wildcard assertNull(store.getApplicationNamesCache().get(singleton("myapp"))); @@ -329,10 +471,27 @@ public void testGetPrivilegesCacheWithNonExistentApplicationName() throws Except final PlainActionFuture> future = new PlainActionFuture<>(); store.getPrivileges(Collections.singletonList("no-such-app"), null, future); final SearchHit[] hits = buildHits(emptyList()); - listener.get().onResponse(new SearchResponse(new SearchResponseSections( - new SearchHits(hits, new TotalHits(hits.length, TotalHits.Relation.EQUAL_TO), 0f), - null, null, false, false, null, 1), - "_scrollId1", 1, 1, 0, 1, null, null) ); + listener.get() + .onResponse( + new SearchResponse( + new SearchResponseSections( + new SearchHits(hits, new TotalHits(hits.length, TotalHits.Relation.EQUAL_TO), 0f), + null, + null, + false, + false, + null, + 1 + ), + "_scrollId1", + 1, + 1, + 0, + 1, + null, + null + ) + ); assertEquals(emptySet(), store.getApplicationNamesCache().get(singleton("no-such-app"))); assertEquals(0, store.getDescriptorsCache().count()); @@ -349,10 +508,27 @@ public void testGetPrivilegesCacheWithDifferentMatchAllApplicationNames() throws final PlainActionFuture> future = new PlainActionFuture<>(); store.getPrivileges(emptyList(), null, future); final SearchHit[] hits = buildHits(emptyList()); - listener.get().onResponse(new SearchResponse(new SearchResponseSections( - new SearchHits(hits, new TotalHits(hits.length, TotalHits.Relation.EQUAL_TO), 0f), - null, null, false, false, null, 1), - "_scrollId1", 1, 1, 0, 1, null, null) ); + listener.get() + .onResponse( + new SearchResponse( + new SearchResponseSections( + new SearchHits(hits, new TotalHits(hits.length, TotalHits.Relation.EQUAL_TO), 0f), + null, + null, + false, + false, + null, + 1 + ), + "_scrollId1", + 1, + 1, + 0, + 1, + null, + null + ) + ); assertEquals(emptySet(), store.getApplicationNamesCache().get(singleton("*"))); assertEquals(1, store.getApplicationNamesCache().count()); assertResult(emptyList(), future); @@ -390,10 +566,27 @@ public void testStaleResultsWillNotBeCached() { // Before the results can be cached, invalidate the cache to simulate stale search results store.getDescriptorsAndApplicationNamesCache().invalidateAll(); final SearchHit[] hits = buildHits(sourcePrivileges); - listener.get().onResponse(new SearchResponse(new SearchResponseSections( - new SearchHits(hits, new TotalHits(hits.length, TotalHits.Relation.EQUAL_TO), 0f), - null, null, false, false, null, 1), - "_scrollId1", 1, 1, 0, 1, null, null)); + listener.get() + .onResponse( + new SearchResponse( + new SearchResponseSections( + new SearchHits(hits, new TotalHits(hits.length, TotalHits.Relation.EQUAL_TO), 0f), + null, + null, + false, + false, + null, + 1 + ), + "_scrollId1", + 1, + 1, + 0, + 1, + null, + null + ) + ); // Nothing should be cached since the results are stale assertEquals(0, store.getApplicationNamesCache().count()); @@ -412,12 +605,18 @@ public void testWhenStaleResultsAreCachedTheyWillBeCleared() throws InterruptedE // Hence the cache invalidation will be block at acquiring the write lock. // This simulates the scenario when stale results are cached just before the invalidation call arrives. // In this case, we guarantee the cache will be invalidate and the stale results won't stay for long. - final NativePrivilegeStore store1 = - new NativePrivilegeStore(Settings.EMPTY, client, securityIndex, new CacheInvalidatorRegistry()) { + final NativePrivilegeStore store1 = new NativePrivilegeStore( + Settings.EMPTY, + client, + securityIndex, + new CacheInvalidatorRegistry() + ) { @Override - protected void cacheFetchedDescriptors(Set applicationNamesCacheKey, - Map> mapOfFetchedDescriptors, - long invalidationCount) { + protected void cacheFetchedDescriptors( + Set applicationNamesCacheKey, + Map> mapOfFetchedDescriptors, + long invalidationCount + ) { getPrivilegeCountDown.countDown(); try { // wait till the invalidation call is at the door step @@ -434,10 +633,27 @@ protected void cacheFetchedDescriptors(Set applicationNamesCacheKey, final PlainActionFuture> future = new PlainActionFuture<>(); store1.getPrivileges(null, null, future); final SearchHit[] hits = buildHits(sourcePrivileges); - listener.get().onResponse(new SearchResponse(new SearchResponseSections( - new SearchHits(hits, new TotalHits(hits.length, TotalHits.Relation.EQUAL_TO), 0f), - null, null, false, false, null, 1), - "_scrollId1", 1, 1, 0, 1, null, null)); + listener.get() + .onResponse( + new SearchResponse( + new SearchResponseSections( + new SearchHits(hits, new TotalHits(hits.length, TotalHits.Relation.EQUAL_TO), 0f), + null, + null, + false, + false, + null, + 1 + ), + "_scrollId1", + 1, + 1, + 0, + 1, + null, + null + ) + ); // Make sure the caching is about to happen getPrivilegeCountDown.await(5, TimeUnit.SECONDS); @@ -474,16 +690,13 @@ public void testPutPrivileges() throws Exception { ApplicationPrivilegeDescriptor privilege = putPrivileges.get(i); IndexRequest request = indexRequests.get(i); assertThat(request.indices(), arrayContaining(RestrictedIndicesNames.SECURITY_MAIN_ALIAS)); - assertThat(request.id(), equalTo( - "application-privilege_" + privilege.getApplication() + ":" + privilege.getName() - )); + assertThat(request.id(), equalTo("application-privilege_" + privilege.getApplication() + ":" + privilege.getName())); final XContentBuilder builder = privilege.toXContent(XContentBuilder.builder(XContentType.JSON.xContent()), true); assertThat(request.source(), equalTo(BytesReference.bytes(builder))); final boolean created = privilege.getName().equals("user") == false; - indexListener.onResponse(new IndexResponse( - new ShardId(RestrictedIndicesNames.SECURITY_MAIN_ALIAS, uuid, i), - request.id(), 1, 1, 1, created - )); + indexListener.onResponse( + new IndexResponse(new ShardId(RestrictedIndicesNames.SECURITY_MAIN_ALIAS, uuid, i), request.id(), 1, 1, 1, created) + ); } assertBusy(() -> assertFalse(requests.isEmpty()), 1, TimeUnit.SECONDS); @@ -520,10 +733,9 @@ public void testDeletePrivileges() throws Exception { assertThat(request.indices(), arrayContaining(RestrictedIndicesNames.SECURITY_MAIN_ALIAS)); assertThat(request.id(), equalTo("application-privilege_app1:" + name)); final boolean found = name.equals("p2") == false; - deleteListener.onResponse(new DeleteResponse( - new ShardId(RestrictedIndicesNames.SECURITY_MAIN_ALIAS, uuid, i), - request.id(), 1, 1, 1, found - )); + deleteListener.onResponse( + new DeleteResponse(new ShardId(RestrictedIndicesNames.SECURITY_MAIN_ALIAS, uuid, i), request.id(), 1, 1, 1, found) + ); } assertBusy(() -> assertFalse(requests.isEmpty()), 1, TimeUnit.SECONDS); @@ -540,10 +752,8 @@ public void testDeletePrivileges() throws Exception { public void testInvalidate() { store.getApplicationNamesCache().put(singleton("*"), Set.of()); - store.getDescriptorsCache().put("app-1", - singleton(new ApplicationPrivilegeDescriptor("app-1", "read", emptySet(), emptyMap()))); - store.getDescriptorsCache().put("app-2", - singleton(new ApplicationPrivilegeDescriptor("app-2", "read", emptySet(), emptyMap()))); + store.getDescriptorsCache().put("app-1", singleton(new ApplicationPrivilegeDescriptor("app-1", "read", emptySet(), emptyMap()))); + store.getDescriptorsCache().put("app-2", singleton(new ApplicationPrivilegeDescriptor("app-2", "read", emptySet(), emptyMap()))); store.getDescriptorsAndApplicationNamesCache().invalidate(singletonList("app-1")); assertEquals(0, store.getApplicationNamesCache().count()); assertEquals(1, store.getDescriptorsCache().count()); @@ -551,10 +761,8 @@ public void testInvalidate() { public void testInvalidateAll() { store.getApplicationNamesCache().put(singleton("*"), Set.of()); - store.getDescriptorsCache().put("app-1", - singleton(new ApplicationPrivilegeDescriptor("app-1", "read", emptySet(), emptyMap()))); - store.getDescriptorsCache().put("app-2", - singleton(new ApplicationPrivilegeDescriptor("app-2", "read", emptySet(), emptyMap()))); + store.getDescriptorsCache().put("app-1", singleton(new ApplicationPrivilegeDescriptor("app-1", "read", emptySet(), emptyMap()))); + store.getDescriptorsCache().put("app-2", singleton(new ApplicationPrivilegeDescriptor("app-2", "read", emptySet(), emptyMap()))); store.getDescriptorsAndApplicationNamesCache().invalidateAll(); assertEquals(0, store.getApplicationNamesCache().count()); assertEquals(0, store.getDescriptorsCache().count()); @@ -562,29 +770,36 @@ public void testInvalidateAll() { public void testCacheClearOnIndexHealthChange() { final String securityIndexName = randomFrom( - RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_6, RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7); + RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_6, + RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7 + ); long count = store.getNumInvalidation(); // Cache should be cleared when security is back to green cacheInvalidatorRegistry.onSecurityIndexStateChange( dummyState(securityIndexName, true, randomFrom((ClusterHealthStatus) null, ClusterHealthStatus.RED)), - dummyState(securityIndexName, true, randomFrom(ClusterHealthStatus.GREEN, ClusterHealthStatus.YELLOW))); + dummyState(securityIndexName, true, randomFrom(ClusterHealthStatus.GREEN, ClusterHealthStatus.YELLOW)) + ); assertEquals(++count, store.getNumInvalidation()); // Cache should be cleared when security is deleted cacheInvalidatorRegistry.onSecurityIndexStateChange( dummyState(securityIndexName, true, randomFrom(ClusterHealthStatus.values())), - dummyState(securityIndexName, true, null)); + dummyState(securityIndexName, true, null) + ); assertEquals(++count, store.getNumInvalidation()); // Cache should be cleared if indexUpToDate changed final boolean isIndexUpToDate = randomBoolean(); - final List allPossibleHealthStatus = - CollectionUtils.appendToCopy(Arrays.asList(ClusterHealthStatus.values()), null); + final List allPossibleHealthStatus = CollectionUtils.appendToCopy( + Arrays.asList(ClusterHealthStatus.values()), + null + ); cacheInvalidatorRegistry.onSecurityIndexStateChange( dummyState(securityIndexName, isIndexUpToDate, randomFrom(allPossibleHealthStatus)), - dummyState(securityIndexName, isIndexUpToDate == false, randomFrom(allPossibleHealthStatus))); + dummyState(securityIndexName, isIndexUpToDate == false, randomFrom(allPossibleHealthStatus)) + ); assertEquals(++count, store.getNumInvalidation()); } @@ -594,6 +809,7 @@ public void testCacheWillBeDisabledWhenTtlIsZero() { assertNull(store1.getApplicationNamesCache()); assertNull(store1.getDescriptorsCache()); } + public void testGetPrivilegesWorkWithoutCache() throws Exception { final Settings settings = Settings.builder().put("xpack.security.authz.store.privileges.cache.ttl", 0).build(); final NativePrivilegeStore store1 = new NativePrivilegeStore(settings, client, securityIndex, new CacheInvalidatorRegistry()); @@ -604,19 +820,47 @@ public void testGetPrivilegesWorkWithoutCache() throws Exception { final PlainActionFuture> future = new PlainActionFuture<>(); store1.getPrivileges(singletonList("myapp"), null, future); final SearchHit[] hits = buildHits(sourcePrivileges); - listener.get().onResponse(new SearchResponse(new SearchResponseSections( - new SearchHits(hits, new TotalHits(hits.length, TotalHits.Relation.EQUAL_TO), 0f), - null, null, false, false, null, 1), - "_scrollId1", 1, 1, 0, 1, null, null)); + listener.get() + .onResponse( + new SearchResponse( + new SearchResponseSections( + new SearchHits(hits, new TotalHits(hits.length, TotalHits.Relation.EQUAL_TO), 0f), + null, + null, + false, + false, + null, + 1 + ), + "_scrollId1", + 1, + 1, + 0, + 1, + null, + null + ) + ); assertResult(sourcePrivileges, future); } private SecurityIndexManager.State dummyState( - String concreteSecurityIndexName, boolean isIndexUpToDate, ClusterHealthStatus healthStatus) { + String concreteSecurityIndexName, + boolean isIndexUpToDate, + ClusterHealthStatus healthStatus + ) { return new SecurityIndexManager.State( - Instant.now(), isIndexUpToDate, true, true, null, - concreteSecurityIndexName, healthStatus, IndexMetadata.State.OPEN, null, "my_uuid" + Instant.now(), + isIndexUpToDate, + true, + true, + null, + concreteSecurityIndexName, + healthStatus, + IndexMetadata.State.OPEN, + null, + "my_uuid" ); } @@ -630,8 +874,10 @@ private SearchHit[] buildHits(List sourcePrivile return hits; } - private void assertResult(List sourcePrivileges, - PlainActionFuture> future) throws Exception { + private void assertResult( + List sourcePrivileges, + PlainActionFuture> future + ) throws Exception { final Collection getPrivileges = future.get(1, TimeUnit.SECONDS); assertThat(getPrivileges, iterableWithSize(sourcePrivileges.size())); assertThat(new HashSet<>(getPrivileges), equalTo(new HashSet<>(sourcePrivileges))); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/store/NativeRolesStoreTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/store/NativeRolesStoreTests.java index 6b5f5a509eb40..925cc6c8dcfcc 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/store/NativeRolesStoreTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/store/NativeRolesStoreTests.java @@ -27,19 +27,19 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.license.MockLicenseState; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.Index; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.shard.ShardId; +import org.elasticsearch.license.MockLicenseState; import org.elasticsearch.license.TestUtils; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.threadpool.TestThreadPool; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.security.action.role.PutRoleRequest; import org.elasticsearch.xpack.core.security.authz.RoleDescriptor; import org.elasticsearch.xpack.core.security.authz.RoleDescriptor.IndicesPrivileges; @@ -84,8 +84,12 @@ public void testBWCFieldPermissions() throws IOException { Path path = getDataPath("roles2xformat.json"); byte[] bytes = Files.readAllBytes(path); String roleString = new String(bytes, Charset.defaultCharset()); - RoleDescriptor role = NativeRolesStore.transformRole(RoleDescriptor.ROLE_TYPE + "role1", - new BytesArray(roleString), logger, TestUtils.newTestLicenseState()); + RoleDescriptor role = NativeRolesStore.transformRole( + RoleDescriptor.ROLE_TYPE + "role1", + new BytesArray(roleString), + logger, + TestUtils.newTestLicenseState() + ); assertNotNull(role); assertNotNull(role.getIndicesPrivileges()); RoleDescriptor.IndicesPrivileges indicesPrivileges = role.getIndicesPrivileges()[0]; @@ -97,35 +101,46 @@ public void testBWCFieldPermissions() throws IOException { public void testRoleDescriptorWithFlsDlsLicensing() throws IOException { MockLicenseState licenseState = mock(MockLicenseState.class); when(licenseState.isAllowed(DOCUMENT_LEVEL_SECURITY_FEATURE)).thenReturn(false); - RoleDescriptor flsRole = new RoleDescriptor("fls", null, - new IndicesPrivileges[] { IndicesPrivileges.builder().privileges("READ").indices("*") - .grantedFields("*") - .deniedFields("foo") - .build() }, - null); + RoleDescriptor flsRole = new RoleDescriptor( + "fls", + null, + new IndicesPrivileges[] { + IndicesPrivileges.builder().privileges("READ").indices("*").grantedFields("*").deniedFields("foo").build() }, + null + ); assertFalse(flsRole.getTransientMetadata().containsKey("unlicensed_features")); BytesReference matchAllBytes = XContentHelper.toXContent(QueryBuilders.matchAllQuery(), XContentType.JSON, false); - RoleDescriptor dlsRole = new RoleDescriptor("dls", null, - new IndicesPrivileges[] { IndicesPrivileges.builder().indices("*").privileges("READ") - .query(matchAllBytes) - .build() }, - null); + RoleDescriptor dlsRole = new RoleDescriptor( + "dls", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices("*").privileges("READ").query(matchAllBytes).build() }, + null + ); assertFalse(dlsRole.getTransientMetadata().containsKey("unlicensed_features")); - RoleDescriptor flsDlsRole = new RoleDescriptor("fls_ dls", null, - new IndicesPrivileges[] { IndicesPrivileges.builder().indices("*").privileges("READ") - .grantedFields("*") - .deniedFields("foo") - .query(matchAllBytes) - .build() }, - null); + RoleDescriptor flsDlsRole = new RoleDescriptor( + "fls_ dls", + null, + new IndicesPrivileges[] { + IndicesPrivileges.builder() + .indices("*") + .privileges("READ") + .grantedFields("*") + .deniedFields("foo") + .query(matchAllBytes) + .build() }, + null + ); assertFalse(flsDlsRole.getTransientMetadata().containsKey("unlicensed_features")); - RoleDescriptor noFlsDlsRole = new RoleDescriptor("no_fls_dls", null, - new IndicesPrivileges[] { IndicesPrivileges.builder().indices("*").privileges("READ").build() }, - null); + RoleDescriptor noFlsDlsRole = new RoleDescriptor( + "no_fls_dls", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices("*").privileges("READ").build() }, + null + ); assertFalse(noFlsDlsRole.getTransientMetadata().containsKey("unlicensed_features")); XContentBuilder builder = flsRole.toXContent(XContentBuilder.builder(XContentType.JSON.xContent()), ToXContent.EMPTY_PARAMS); @@ -205,47 +220,59 @@ void innerPutRole(final PutRoleRequest request, final RoleDescriptor role, final } }; // setup the roles store so the security index exists - securityIndex.clusterChanged(new ClusterChangedEvent( - "fls_dls_license", getClusterStateWithSecurityIndex(), getEmptyClusterState())); + securityIndex.clusterChanged( + new ClusterChangedEvent("fls_dls_license", getClusterStateWithSecurityIndex(), getEmptyClusterState()) + ); PutRoleRequest putRoleRequest = new PutRoleRequest(); - RoleDescriptor flsRole = new RoleDescriptor("fls", null, - new IndicesPrivileges[] { IndicesPrivileges.builder().privileges("READ").indices("*") - .grantedFields("*") - .deniedFields("foo") - .build() }, - null); + RoleDescriptor flsRole = new RoleDescriptor( + "fls", + null, + new IndicesPrivileges[] { + IndicesPrivileges.builder().privileges("READ").indices("*").grantedFields("*").deniedFields("foo").build() }, + null + ); PlainActionFuture future = new PlainActionFuture<>(); rolesStore.putRole(putRoleRequest, flsRole, future); ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, future::actionGet); assertThat(e.getMessage(), containsString("field and document level security")); BytesReference matchAllBytes = XContentHelper.toXContent(QueryBuilders.matchAllQuery(), XContentType.JSON, false); - RoleDescriptor dlsRole = new RoleDescriptor("dls", null, - new IndicesPrivileges[] { IndicesPrivileges.builder().indices("*").privileges("READ") - .query(matchAllBytes) - .build() }, - null); + RoleDescriptor dlsRole = new RoleDescriptor( + "dls", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices("*").privileges("READ").query(matchAllBytes).build() }, + null + ); future = new PlainActionFuture<>(); rolesStore.putRole(putRoleRequest, dlsRole, future); e = expectThrows(ElasticsearchSecurityException.class, future::actionGet); assertThat(e.getMessage(), containsString("field and document level security")); - RoleDescriptor flsDlsRole = new RoleDescriptor("fls_ dls", null, - new IndicesPrivileges[] { IndicesPrivileges.builder().indices("*").privileges("READ") - .grantedFields("*") - .deniedFields("foo") - .query(matchAllBytes) - .build() }, - null); + RoleDescriptor flsDlsRole = new RoleDescriptor( + "fls_ dls", + null, + new IndicesPrivileges[] { + IndicesPrivileges.builder() + .indices("*") + .privileges("READ") + .grantedFields("*") + .deniedFields("foo") + .query(matchAllBytes) + .build() }, + null + ); future = new PlainActionFuture<>(); rolesStore.putRole(putRoleRequest, flsDlsRole, future); e = expectThrows(ElasticsearchSecurityException.class, future::actionGet); assertThat(e.getMessage(), containsString("field and document level security")); - RoleDescriptor noFlsDlsRole = new RoleDescriptor("no_fls_dls", null, - new IndicesPrivileges[] { IndicesPrivileges.builder().indices("*").privileges("READ").build() }, - null); + RoleDescriptor noFlsDlsRole = new RoleDescriptor( + "no_fls_dls", + null, + new IndicesPrivileges[] { IndicesPrivileges.builder().indices("*").privileges("READ").build() }, + null + ); future = new PlainActionFuture<>(); rolesStore.putRole(putRoleRequest, noFlsDlsRole, future); assertTrue(future.actionGet()); @@ -256,35 +283,32 @@ private ClusterState getClusterStateWithSecurityIndex() { final String securityIndexName = SECURITY_MAIN_ALIAS + (withAlias ? "-" + randomAlphaOfLength(5) : ""); Settings settings = Settings.builder() - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .build(); - Metadata metadata = Metadata.builder() - .put(IndexMetadata.builder(securityIndexName).settings(settings)) - .build(); + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .build(); + Metadata metadata = Metadata.builder().put(IndexMetadata.builder(securityIndexName).settings(settings)).build(); if (withAlias) { metadata = SecurityTestUtils.addAliasToMetadata(metadata, securityIndexName); } Index index = metadata.index(securityIndexName).getIndex(); - ShardRouting shardRouting = ShardRouting.newUnassigned(new ShardId(index, 0), true, - RecoverySource.ExistingStoreRecoverySource.INSTANCE, new UnassignedInfo(Reason.INDEX_CREATED, "")); - IndexShardRoutingTable table = new IndexShardRoutingTable.Builder(new ShardId(index, 0)) - .addShard(shardRouting.initialize(randomAlphaOfLength(8), null, shardRouting.getExpectedShardSize()).moveToStarted()) - .build(); - RoutingTable routingTable = RoutingTable.builder() - .add(IndexRoutingTable - .builder(index) - .addIndexShard(table) - .build()) - .build(); + ShardRouting shardRouting = ShardRouting.newUnassigned( + new ShardId(index, 0), + true, + RecoverySource.ExistingStoreRecoverySource.INSTANCE, + new UnassignedInfo(Reason.INDEX_CREATED, "") + ); + IndexShardRoutingTable table = new IndexShardRoutingTable.Builder(new ShardId(index, 0)).addShard( + shardRouting.initialize(randomAlphaOfLength(8), null, shardRouting.getExpectedShardSize()).moveToStarted() + ).build(); + RoutingTable routingTable = RoutingTable.builder().add(IndexRoutingTable.builder(index).addIndexShard(table).build()).build(); ClusterState clusterState = ClusterState.builder(new ClusterName(NativeRolesStoreTests.class.getName())) - .metadata(metadata) - .routingTable(routingTable) - .build(); + .metadata(metadata) + .routingTable(routingTable) + .build(); return clusterState; } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/enrollment/ExternalEnrollmentTokenGeneratorTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/enrollment/ExternalEnrollmentTokenGeneratorTests.java index 039e225dc701f..63824c270e7c9 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/enrollment/ExternalEnrollmentTokenGeneratorTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/enrollment/ExternalEnrollmentTokenGeneratorTests.java @@ -11,16 +11,16 @@ import org.elasticsearch.common.settings.MockSecureSettings; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.env.Environment; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.env.Environment; -import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.xpack.core.security.user.ElasticUser; import org.elasticsearch.xpack.core.security.CommandLineHttpClient; import org.elasticsearch.xpack.core.security.HttpResponse; +import org.elasticsearch.xpack.core.security.user.ElasticUser; import org.hamcrest.Matchers; import org.junit.Before; import org.junit.BeforeClass; @@ -89,8 +89,8 @@ public void testCreateSuccess() throws Exception { final URL getHttpInfoURL = externalEnrollmentTokenGenerator.getHttpInfoUrl(); final HttpResponse httpResponseOK = new HttpResponse(HttpURLConnection.HTTP_OK, new HashMap<>()); - when(client.execute(anyString(), any(URL.class), anyString(), any(SecureString.class), anyCheckedSupplier(), - anyCheckedFunction())).thenReturn(httpResponseOK); + when(client.execute(anyString(), any(URL.class), anyString(), any(SecureString.class), anyCheckedSupplier(), anyCheckedFunction())) + .thenReturn(httpResponseOK); String createApiKeyResponseBody; try (XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON)) { @@ -102,34 +102,48 @@ public void testCreateSuccess() throws Exception { .endObject(); createApiKeyResponseBody = Strings.toString(builder); } - when(client.execute(eq("POST"), eq(createAPIKeyURL), eq(ElasticUser.NAME), any(SecureString.class), - anyCheckedSupplier(), anyCheckedFunction())) - .thenReturn(createHttpResponse(HttpURLConnection.HTTP_OK, createApiKeyResponseBody)); + when( + client.execute( + eq("POST"), + eq(createAPIKeyURL), + eq(ElasticUser.NAME), + any(SecureString.class), + anyCheckedSupplier(), + anyCheckedFunction() + ) + ).thenReturn(createHttpResponse(HttpURLConnection.HTTP_OK, createApiKeyResponseBody)); String getHttpInfoResponseBody; try (XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON)) { builder.startObject() .startObject("nodes") - .startObject("sxLDrFu8SnKepObrEOjPZQ") - .field("version", "8.0.0") - .startObject("http") - .startArray("bound_address") - .value("[::1]:9200") - .value("127.0.0.1:9200") - .value("192.168.0.1:9201") - .value("172.16.254.1:9202") - .value("[2001:db8:0:1234:0:567:8:1]:9203") - .endArray() - .field("publish_address", "127.0.0.1:9200") - .endObject() - .endObject() + .startObject("sxLDrFu8SnKepObrEOjPZQ") + .field("version", "8.0.0") + .startObject("http") + .startArray("bound_address") + .value("[::1]:9200") + .value("127.0.0.1:9200") + .value("192.168.0.1:9201") + .value("172.16.254.1:9202") + .value("[2001:db8:0:1234:0:567:8:1]:9203") + .endArray() + .field("publish_address", "127.0.0.1:9200") + .endObject() + .endObject() .endObject() - .endObject(); + .endObject(); getHttpInfoResponseBody = Strings.toString(builder); } - when(client.execute(eq("GET"), eq(getHttpInfoURL), eq(ElasticUser.NAME), any(SecureString.class), - anyCheckedSupplier(), anyCheckedFunction())) - .thenReturn(createHttpResponse(HttpURLConnection.HTTP_OK, getHttpInfoResponseBody)); + when( + client.execute( + eq("GET"), + eq(getHttpInfoURL), + eq(ElasticUser.NAME), + any(SecureString.class), + anyCheckedSupplier(), + anyCheckedFunction() + ) + ).thenReturn(createHttpResponse(HttpURLConnection.HTTP_OK, getHttpInfoResponseBody)); final String tokenNode = externalEnrollmentTokenGenerator.createNodeEnrollmentToken( "elastic", @@ -142,8 +156,10 @@ public void testCreateSuccess() throws Exception { assertEquals("ce480d53728605674fcfd8ffb51000d8a33bf32de7c7f1e26b4d428f8a91362d", infoNode.get("fgr")); assertEquals("DR6CzXkBDf8amV_48yYX:x3YqU_rqQwm-ESrkExcnOg", infoNode.get("key")); - final String tokenKibana = externalEnrollmentTokenGenerator.createKibanaEnrollmentToken("elastic", - new SecureString("elastic".toCharArray())).getEncoded(); + final String tokenKibana = externalEnrollmentTokenGenerator.createKibanaEnrollmentToken( + "elastic", + new SecureString("elastic".toCharArray()) + ).getEncoded(); Map infoKibana = getDecoded(tokenKibana); assertEquals("8.0.0", infoKibana.get("ver")); @@ -159,11 +175,22 @@ public void testFailedCreateApiKey() throws Exception { final URL createAPIKeyURL = externalEnrollmentTokenGenerator.createAPIKeyUrl(); final HttpResponse httpResponseNotOK = new HttpResponse(HttpURLConnection.HTTP_BAD_REQUEST, new HashMap<>()); - when(client.execute(anyString(), eq(createAPIKeyURL), anyString(), any(SecureString.class), anyCheckedSupplier(), - anyCheckedFunction())).thenReturn(httpResponseNotOK); + when( + client.execute( + anyString(), + eq(createAPIKeyURL), + anyString(), + any(SecureString.class), + anyCheckedSupplier(), + anyCheckedFunction() + ) + ).thenReturn(httpResponseNotOK); - IllegalStateException ex = expectThrows(IllegalStateException.class, () -> - externalEnrollmentTokenGenerator.createNodeEnrollmentToken("elastic", new SecureString("elastic".toCharArray())).getEncoded()); + IllegalStateException ex = expectThrows( + IllegalStateException.class, + () -> externalEnrollmentTokenGenerator.createNodeEnrollmentToken("elastic", new SecureString("elastic".toCharArray())) + .getEncoded() + ); assertThat(ex.getMessage(), Matchers.containsString("Unexpected response code [400] from calling POST ")); } @@ -175,8 +202,16 @@ public void testFailedRetrieveHttpInfo() throws Exception { final URL getHttpInfoURL = externalEnrollmentTokenGenerator.getHttpInfoUrl(); final HttpResponse httpResponseOK = new HttpResponse(HttpURLConnection.HTTP_OK, new HashMap<>()); - when(client.execute(anyString(), eq(createAPIKeyURL), anyString(), any(SecureString.class), anyCheckedSupplier(), - anyCheckedFunction())).thenReturn(httpResponseOK); + when( + client.execute( + anyString(), + eq(createAPIKeyURL), + anyString(), + any(SecureString.class), + anyCheckedSupplier(), + anyCheckedFunction() + ) + ).thenReturn(httpResponseOK); String createApiKeyResponseBody; try (XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON)) { @@ -188,16 +223,34 @@ public void testFailedRetrieveHttpInfo() throws Exception { .endObject(); createApiKeyResponseBody = Strings.toString(builder); } - when(client.execute(eq("POST"), eq(createAPIKeyURL), eq(ElasticUser.NAME), any(SecureString.class), - anyCheckedSupplier(), anyCheckedFunction())) - .thenReturn(createHttpResponse(HttpURLConnection.HTTP_OK, createApiKeyResponseBody)); + when( + client.execute( + eq("POST"), + eq(createAPIKeyURL), + eq(ElasticUser.NAME), + any(SecureString.class), + anyCheckedSupplier(), + anyCheckedFunction() + ) + ).thenReturn(createHttpResponse(HttpURLConnection.HTTP_OK, createApiKeyResponseBody)); final HttpResponse httpResponseNotOK = new HttpResponse(HttpURLConnection.HTTP_BAD_REQUEST, new HashMap<>()); - when(client.execute(anyString(), eq(getHttpInfoURL), anyString(), any(SecureString.class), anyCheckedSupplier(), - anyCheckedFunction())).thenReturn(httpResponseNotOK); + when( + client.execute( + anyString(), + eq(getHttpInfoURL), + anyString(), + any(SecureString.class), + anyCheckedSupplier(), + anyCheckedFunction() + ) + ).thenReturn(httpResponseNotOK); - IllegalStateException ex = expectThrows(IllegalStateException.class, () -> - externalEnrollmentTokenGenerator.createNodeEnrollmentToken("elastic", new SecureString("elastic".toCharArray())).getEncoded()); + IllegalStateException ex = expectThrows( + IllegalStateException.class, + () -> externalEnrollmentTokenGenerator.createNodeEnrollmentToken("elastic", new SecureString("elastic".toCharArray())) + .getEncoded() + ); assertThat(ex.getMessage(), Matchers.containsString("Unexpected response code [400] from calling GET ")); } @@ -227,8 +280,16 @@ public void testFailedNoCaInKeystore() throws Exception { final URL getHttpInfoURL = externalEnrollmentTokenGenerator.getHttpInfoUrl(); final HttpResponse httpResponseOK = new HttpResponse(HttpURLConnection.HTTP_OK, new HashMap<>()); - when(client.execute(anyString(), eq(createAPIKeyURL), anyString(), any(SecureString.class), anyCheckedSupplier(), - anyCheckedFunction())).thenReturn(httpResponseOK); + when( + client.execute( + anyString(), + eq(createAPIKeyURL), + anyString(), + any(SecureString.class), + anyCheckedSupplier(), + anyCheckedFunction() + ) + ).thenReturn(httpResponseOK); String createApiKeyResponseBody; try (XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON)) { @@ -240,16 +301,34 @@ public void testFailedNoCaInKeystore() throws Exception { .endObject(); createApiKeyResponseBody = Strings.toString(builder); } - when(client.execute(eq("POST"), eq(createAPIKeyURL), eq(ElasticUser.NAME), any(SecureString.class), - anyCheckedSupplier(), anyCheckedFunction())) - .thenReturn(createHttpResponse(HttpURLConnection.HTTP_OK, createApiKeyResponseBody)); + when( + client.execute( + eq("POST"), + eq(createAPIKeyURL), + eq(ElasticUser.NAME), + any(SecureString.class), + anyCheckedSupplier(), + anyCheckedFunction() + ) + ).thenReturn(createHttpResponse(HttpURLConnection.HTTP_OK, createApiKeyResponseBody)); final HttpResponse httpResponseNotOK = new HttpResponse(HttpURLConnection.HTTP_BAD_REQUEST, new HashMap<>()); - when(client.execute(anyString(), eq(getHttpInfoURL), anyString(), any(SecureString.class), anyCheckedSupplier(), - anyCheckedFunction())).thenReturn(httpResponseNotOK); + when( + client.execute( + anyString(), + eq(getHttpInfoURL), + anyString(), + any(SecureString.class), + anyCheckedSupplier(), + anyCheckedFunction() + ) + ).thenReturn(httpResponseNotOK); - IllegalStateException ex = expectThrows(IllegalStateException.class, () -> - externalEnrollmentTokenGenerator.createNodeEnrollmentToken("elastic", new SecureString("elastic".toCharArray())).getEncoded()); + IllegalStateException ex = expectThrows( + IllegalStateException.class, + () -> externalEnrollmentTokenGenerator.createNodeEnrollmentToken("elastic", new SecureString("elastic".toCharArray())) + .getEncoded() + ); assertThat( ex.getMessage(), equalTo( @@ -285,8 +364,16 @@ public void testFailedManyCaInKeystore() throws Exception { final URL getHttpInfoURL = externalEnrollmentTokenGenerator.getHttpInfoUrl(); final HttpResponse httpResponseOK = new HttpResponse(HttpURLConnection.HTTP_OK, new HashMap<>()); - when(client.execute(anyString(), eq(createAPIKeyURL), anyString(), any(SecureString.class), anyCheckedSupplier(), - anyCheckedFunction())).thenReturn(httpResponseOK); + when( + client.execute( + anyString(), + eq(createAPIKeyURL), + anyString(), + any(SecureString.class), + anyCheckedSupplier(), + anyCheckedFunction() + ) + ).thenReturn(httpResponseOK); String createApiKeyResponseBody; try (XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON)) { @@ -298,18 +385,41 @@ public void testFailedManyCaInKeystore() throws Exception { .endObject(); createApiKeyResponseBody = Strings.toString(builder); } - when(client.execute(eq("POST"), eq(createAPIKeyURL), eq(ElasticUser.NAME), any(SecureString.class), - anyCheckedSupplier(), anyCheckedFunction())) - .thenReturn(createHttpResponse(HttpURLConnection.HTTP_OK, createApiKeyResponseBody)); + when( + client.execute( + eq("POST"), + eq(createAPIKeyURL), + eq(ElasticUser.NAME), + any(SecureString.class), + anyCheckedSupplier(), + anyCheckedFunction() + ) + ).thenReturn(createHttpResponse(HttpURLConnection.HTTP_OK, createApiKeyResponseBody)); final HttpResponse httpResponseNotOK = new HttpResponse(HttpURLConnection.HTTP_BAD_REQUEST, new HashMap<>()); - when(client.execute(anyString(), eq(getHttpInfoURL), anyString(), any(SecureString.class), anyCheckedSupplier(), - anyCheckedFunction())).thenReturn(httpResponseNotOK); + when( + client.execute( + anyString(), + eq(getHttpInfoURL), + anyString(), + any(SecureString.class), + anyCheckedSupplier(), + anyCheckedFunction() + ) + ).thenReturn(httpResponseNotOK); - IllegalStateException ex = expectThrows(IllegalStateException.class, () -> - externalEnrollmentTokenGenerator.createNodeEnrollmentToken("elastic", new SecureString("elastic".toCharArray())).getEncoded()); - assertThat(ex.getMessage(), equalTo("Unable to create an enrollment token. Elasticsearch node HTTP layer SSL " + - "configuration Keystore contains multiple PrivateKey entries where the associated certificate is a CA certificate")); + IllegalStateException ex = expectThrows( + IllegalStateException.class, + () -> externalEnrollmentTokenGenerator.createNodeEnrollmentToken("elastic", new SecureString("elastic".toCharArray())) + .getEncoded() + ); + assertThat( + ex.getMessage(), + equalTo( + "Unable to create an enrollment token. Elasticsearch node HTTP layer SSL " + + "configuration Keystore contains multiple PrivateKey entries where the associated certificate is a CA certificate" + ) + ); } public void testNoKeyStore() throws Exception { @@ -322,13 +432,20 @@ public void testNoKeyStore() throws Exception { final Environment environment_no_keystore = new Environment(settings, tempDir); final CommandLineHttpClient client = mock(CommandLineHttpClient.class); when(client.getDefaultURL()).thenReturn("http://localhost:9200"); - final ExternalEnrollmentTokenGenerator - externalEnrollmentTokenGenerator = new ExternalEnrollmentTokenGenerator(environment_no_keystore, client); + final ExternalEnrollmentTokenGenerator externalEnrollmentTokenGenerator = new ExternalEnrollmentTokenGenerator( + environment_no_keystore, + client + ); - IllegalStateException ex = expectThrows(IllegalStateException.class, () -> - externalEnrollmentTokenGenerator.createNodeEnrollmentToken("elastic", new SecureString("elastic".toCharArray())).getEncoded()); - assertThat(ex.getMessage(), Matchers.containsString("Elasticsearch node HTTP layer SSL configuration is not configured " + - "with a keystore")); + IllegalStateException ex = expectThrows( + IllegalStateException.class, + () -> externalEnrollmentTokenGenerator.createNodeEnrollmentToken("elastic", new SecureString("elastic".toCharArray())) + .getEncoded() + ); + assertThat( + ex.getMessage(), + Matchers.containsString("Elasticsearch node HTTP layer SSL configuration is not configured " + "with a keystore") + ); } public void testEnrollmentNotEnabled() throws Exception { @@ -351,22 +468,36 @@ public void testEnrollmentNotEnabled() throws Exception { final Environment environment_not_enabled = new Environment(settings, tempDir); final CommandLineHttpClient client = mock(CommandLineHttpClient.class); when(client.getDefaultURL()).thenReturn("http://localhost:9200"); - final ExternalEnrollmentTokenGenerator - externalEnrollmentTokenGenerator = new ExternalEnrollmentTokenGenerator(environment_not_enabled, client); + final ExternalEnrollmentTokenGenerator externalEnrollmentTokenGenerator = new ExternalEnrollmentTokenGenerator( + environment_not_enabled, + client + ); - IllegalStateException ex = expectThrows(IllegalStateException.class, () -> - externalEnrollmentTokenGenerator.createNodeEnrollmentToken("elastic", new SecureString("elastic".toCharArray())).getEncoded()); - assertThat(ex.getMessage(), equalTo("[xpack.security.enrollment.enabled] must be set to `true` to " + - "create an enrollment token")); + IllegalStateException ex = expectThrows( + IllegalStateException.class, + () -> externalEnrollmentTokenGenerator.createNodeEnrollmentToken("elastic", new SecureString("elastic".toCharArray())) + .getEncoded() + ); + assertThat( + ex.getMessage(), + equalTo("[xpack.security.enrollment.enabled] must be set to `true` to " + "create an enrollment token") + ); } public void testGetFilteredAddresses() throws Exception { - List addresses = Arrays.asList("[::1]:9200", "127.0.0.1:9200", "192.168.0.1:9201", "172.16.254.1:9202", - "[2001:db8:0:1234:0:567:8:1]:9203"); + List addresses = Arrays.asList( + "[::1]:9200", + "127.0.0.1:9200", + "192.168.0.1:9201", + "172.16.254.1:9202", + "[2001:db8:0:1234:0:567:8:1]:9203" + ); List filteredAddresses = getFilteredAddresses(addresses); assertThat(filteredAddresses, hasSize(3)); - assertThat(filteredAddresses, Matchers.containsInAnyOrder("192.168.0.1:9201", "172.16.254.1:9202", - "[2001:db8:0:1234:0:567:8:1]:9203")); + assertThat( + filteredAddresses, + Matchers.containsInAnyOrder("192.168.0.1:9201", "172.16.254.1:9202", "[2001:db8:0:1234:0:567:8:1]:9203") + ); assertThat(filteredAddresses.get(2), equalTo("[2001:db8:0:1234:0:567:8:1]:9203")); addresses = Arrays.asList("0.0.0.0:9200", "172.17.0.2:9200"); @@ -400,8 +531,10 @@ public void testGetFilteredAddresses() throws Exception { addresses = Arrays.asList("8.8.8.8:9200", "192.168.0.1:9201", "172.16.254.1:9202", "[2001:db8:0:1234:0:567:8:1]:9203"); filteredAddresses = getFilteredAddresses(addresses); assertThat(filteredAddresses, hasSize(4)); - assertThat(filteredAddresses, Matchers.containsInAnyOrder("8.8.8.8:9200", "192.168.0.1:9201", "172.16.254.1:9202", - "[2001:db8:0:1234:0:567:8:1]:9203")); + assertThat( + filteredAddresses, + Matchers.containsInAnyOrder("8.8.8.8:9200", "192.168.0.1:9201", "172.16.254.1:9202", "[2001:db8:0:1234:0:567:8:1]:9203") + ); assertThat(filteredAddresses.get(3), equalTo("[2001:db8:0:1234:0:567:8:1]:9203")); final List invalid_addresses = Arrays.asList("nldfnbndflbnl"); @@ -414,8 +547,7 @@ private Map getDecoded(String token) throws IOException { try (XContentParser parser = createParser(JsonXContent.jsonXContent, jsonString)) { final Map info = parser.map(); assertNotEquals(info, null); - return info.entrySet().stream() - .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue().toString())); + return info.entrySet().stream().collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue().toString())); } } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/ingest/SetSecurityUserProcessorTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/ingest/SetSecurityUserProcessorTests.java index 459f66444a999..4027a0e71d2ea 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/ingest/SetSecurityUserProcessorTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/ingest/SetSecurityUserProcessorTests.java @@ -10,11 +10,11 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.ingest.IngestDocument; import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.test.XContentTestUtils; +import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.xpack.core.security.SecurityContext; import org.elasticsearch.xpack.core.security.action.ApiKeyTests; import org.elasticsearch.xpack.core.security.action.service.TokenInfo; @@ -59,7 +59,13 @@ public void testProcessorWithData() throws Exception { IngestDocument ingestDocument = new IngestDocument(new HashMap<>(), new HashMap<>()); SetSecurityUserProcessor processor = new SetSecurityUserProcessor( - "_tag", null, securityContext, Settings.EMPTY, "_field", EnumSet.allOf(Property.class)); + "_tag", + null, + securityContext, + Settings.EMPTY, + "_field", + EnumSet.allOf(Property.class) + ); processor.execute(ingestDocument); Map result = ingestDocument.getFieldValue("_field", Map.class); @@ -101,7 +107,13 @@ public void testProcessorWithEmptyUserData() throws Exception { IngestDocument ingestDocument = new IngestDocument(new HashMap<>(), new HashMap<>()); SetSecurityUserProcessor processor = new SetSecurityUserProcessor( - "_tag", null, securityContext, Settings.EMPTY, "_field", EnumSet.allOf(Property.class)); + "_tag", + null, + securityContext, + Settings.EMPTY, + "_field", + EnumSet.allOf(Property.class) + ); processor.execute(ingestDocument); Map result = ingestDocument.getFieldValue("_field", Map.class); // Still holds data for realm and authentication type @@ -114,20 +126,39 @@ public void testProcessorWithEmptyUserData() throws Exception { public void testNoCurrentUser() throws Exception { IngestDocument ingestDocument = new IngestDocument(new HashMap<>(), new HashMap<>()); SetSecurityUserProcessor processor = new SetSecurityUserProcessor( - "_tag", null, securityContext, Settings.EMPTY, "_field", EnumSet.allOf(Property.class)); + "_tag", + null, + securityContext, + Settings.EMPTY, + "_field", + EnumSet.allOf(Property.class) + ); IllegalStateException e = expectThrows(IllegalStateException.class, () -> processor.execute(ingestDocument)); - assertThat(e.getMessage(), - equalTo("There is no authenticated user - the [set_security_user] processor requires an authenticated user")); + assertThat( + e.getMessage(), + equalTo("There is no authenticated user - the [set_security_user] processor requires an authenticated user") + ); } public void testSecurityDisabled() throws Exception { Settings securityDisabledSettings = Settings.builder().put(XPackSettings.SECURITY_ENABLED.getKey(), false).build(); IngestDocument ingestDocument = new IngestDocument(new HashMap<>(), new HashMap<>()); SetSecurityUserProcessor processor = new SetSecurityUserProcessor( - "_tag", null, securityContext, securityDisabledSettings, "_field", EnumSet.allOf(Property.class)); + "_tag", + null, + securityContext, + securityDisabledSettings, + "_field", + EnumSet.allOf(Property.class) + ); IllegalStateException e = expectThrows(IllegalStateException.class, () -> processor.execute(ingestDocument)); - assertThat(e.getMessage(), equalTo("Security (authentication) is not enabled on this cluster, so there is no active user" + - " - the [set_security_user] processor cannot be used without security")); + assertThat( + e.getMessage(), + equalTo( + "Security (authentication) is not enabled on this cluster, so there is no active user" + + " - the [set_security_user] processor cannot be used without security" + ) + ); } public void testUsernameProperties() throws Exception { @@ -136,7 +167,13 @@ public void testUsernameProperties() throws Exception { IngestDocument ingestDocument = new IngestDocument(new HashMap<>(), new HashMap<>()); SetSecurityUserProcessor processor = new SetSecurityUserProcessor( - "_tag", null, securityContext, Settings.EMPTY, "_field", EnumSet.of(Property.USERNAME)); + "_tag", + null, + securityContext, + Settings.EMPTY, + "_field", + EnumSet.of(Property.USERNAME) + ); processor.execute(ingestDocument); @SuppressWarnings("unchecked") @@ -151,7 +188,13 @@ public void testRolesProperties() throws Exception { IngestDocument ingestDocument = new IngestDocument(new HashMap<>(), new HashMap<>()); SetSecurityUserProcessor processor = new SetSecurityUserProcessor( - "_tag", null, securityContext, Settings.EMPTY, "_field", EnumSet.of(Property.ROLES)); + "_tag", + null, + securityContext, + Settings.EMPTY, + "_field", + EnumSet.of(Property.ROLES) + ); processor.execute(ingestDocument); @SuppressWarnings("unchecked") @@ -169,8 +212,14 @@ public void testFullNameProperties() throws Exception { authentication.writeToContext(threadContext); IngestDocument ingestDocument = new IngestDocument(new HashMap<>(), new HashMap<>()); - SetSecurityUserProcessor processor - = new SetSecurityUserProcessor("_tag", null, securityContext, Settings.EMPTY, "_field", EnumSet.of(Property.FULL_NAME)); + SetSecurityUserProcessor processor = new SetSecurityUserProcessor( + "_tag", + null, + securityContext, + Settings.EMPTY, + "_field", + EnumSet.of(Property.FULL_NAME) + ); processor.execute(ingestDocument); @SuppressWarnings("unchecked") @@ -185,7 +234,13 @@ public void testEmailProperties() throws Exception { IngestDocument ingestDocument = new IngestDocument(new HashMap<>(), new HashMap<>()); SetSecurityUserProcessor processor = new SetSecurityUserProcessor( - "_tag", null, securityContext, Settings.EMPTY, "_field", EnumSet.of(Property.EMAIL)); + "_tag", + null, + securityContext, + Settings.EMPTY, + "_field", + EnumSet.of(Property.EMAIL) + ); processor.execute(ingestDocument); @SuppressWarnings("unchecked") @@ -204,7 +259,13 @@ public void testMetadataProperties() throws Exception { IngestDocument ingestDocument = new IngestDocument(new HashMap<>(), new HashMap<>()); SetSecurityUserProcessor processor = new SetSecurityUserProcessor( - "_tag", null, securityContext, Settings.EMPTY, "_field", EnumSet.of(Property.METADATA)); + "_tag", + null, + securityContext, + Settings.EMPTY, + "_field", + EnumSet.of(Property.METADATA) + ); processor.execute(ingestDocument); @SuppressWarnings("unchecked") @@ -222,7 +283,13 @@ public void testOverwriteExistingField() throws Exception { authentication.writeToContext(threadContext); SetSecurityUserProcessor processor = new SetSecurityUserProcessor( - "_tag", null, securityContext, Settings.EMPTY, "_field", EnumSet.of(Property.USERNAME)); + "_tag", + null, + securityContext, + Settings.EMPTY, + "_field", + EnumSet.of(Property.USERNAME) + ); IngestDocument ingestDocument = new IngestDocument(new HashMap<>(), new HashMap<>()); ingestDocument.setFieldValue("_field", "test"); @@ -249,26 +316,40 @@ public void testOverwriteExistingField() throws Exception { public void testApiKeyPopulation() throws Exception { User user = new User(randomAlphaOfLengthBetween(4, 12), null, null); Authentication.RealmRef realmRef = new Authentication.RealmRef( - ApiKeyService.API_KEY_REALM_NAME, ApiKeyService.API_KEY_REALM_TYPE, "_node_name"); - - final Map authMetadata = new HashMap<>(Map.of( - ApiKeyService.API_KEY_ID_KEY, "api_key_id", - ApiKeyService.API_KEY_NAME_KEY, "api_key_name", - ApiKeyService.API_KEY_CREATOR_REALM_NAME, "creator_realm_name", - ApiKeyService.API_KEY_CREATOR_REALM_TYPE, "creator_realm_type" - )); + ApiKeyService.API_KEY_REALM_NAME, + ApiKeyService.API_KEY_REALM_TYPE, + "_node_name" + ); + + final Map authMetadata = new HashMap<>( + Map.of( + ApiKeyService.API_KEY_ID_KEY, + "api_key_id", + ApiKeyService.API_KEY_NAME_KEY, + "api_key_name", + ApiKeyService.API_KEY_CREATOR_REALM_NAME, + "creator_realm_name", + ApiKeyService.API_KEY_CREATOR_REALM_TYPE, + "creator_realm_type" + ) + ); final Map apiKeyMetadata = ApiKeyTests.randomMetadata(); if (apiKeyMetadata != null) { authMetadata.put(ApiKeyService.API_KEY_METADATA_KEY, XContentTestUtils.convertToXContent(apiKeyMetadata, XContentType.JSON)); } - Authentication auth = new Authentication(user, realmRef, null, Version.CURRENT, - AuthenticationType.API_KEY, authMetadata); + Authentication auth = new Authentication(user, realmRef, null, Version.CURRENT, AuthenticationType.API_KEY, authMetadata); auth.writeToContext(threadContext); IngestDocument ingestDocument = new IngestDocument(new HashMap<>(), new HashMap<>()); SetSecurityUserProcessor processor = new SetSecurityUserProcessor( - "_tag", null, securityContext, Settings.EMPTY, "_field", EnumSet.allOf(Property.class)); + "_tag", + null, + securityContext, + Settings.EMPTY, + "_field", + EnumSet.allOf(Property.class) + ); processor.execute(ingestDocument); Map result = ingestDocument.getFieldValue("_field", Map.class); @@ -290,28 +371,43 @@ public void testApiKeyPopulation() throws Exception { public void testWillNotOverwriteExistingApiKeyAndRealm() throws Exception { User user = new User(randomAlphaOfLengthBetween(4, 12), null, null); Authentication.RealmRef realmRef = new Authentication.RealmRef( - ApiKeyService.API_KEY_REALM_NAME, ApiKeyService.API_KEY_REALM_TYPE, "_node_name"); - - final Map authMetadata = new HashMap<>(Map.of( - ApiKeyService.API_KEY_ID_KEY, "api_key_id", - ApiKeyService.API_KEY_NAME_KEY, "api_key_name", - ApiKeyService.API_KEY_CREATOR_REALM_NAME, "creator_realm_name", - ApiKeyService.API_KEY_CREATOR_REALM_TYPE, "creator_realm_type" - )); + ApiKeyService.API_KEY_REALM_NAME, + ApiKeyService.API_KEY_REALM_TYPE, + "_node_name" + ); + + final Map authMetadata = new HashMap<>( + Map.of( + ApiKeyService.API_KEY_ID_KEY, + "api_key_id", + ApiKeyService.API_KEY_NAME_KEY, + "api_key_name", + ApiKeyService.API_KEY_CREATOR_REALM_NAME, + "creator_realm_name", + ApiKeyService.API_KEY_CREATOR_REALM_TYPE, + "creator_realm_type" + ) + ); final Map apiKeyMetadata = ApiKeyTests.randomMetadata(); if (apiKeyMetadata != null) { authMetadata.put(ApiKeyService.API_KEY_METADATA_KEY, XContentTestUtils.convertToXContent(apiKeyMetadata, XContentType.JSON)); } - Authentication auth = new Authentication(user, realmRef, null, Version.CURRENT, - AuthenticationType.API_KEY, authMetadata); + Authentication auth = new Authentication(user, realmRef, null, Version.CURRENT, AuthenticationType.API_KEY, authMetadata); auth.writeToContext(threadContext); - IngestDocument ingestDocument = new IngestDocument(IngestDocument.deepCopyMap(Map.of( - "_field", Map.of("api_key", Map.of("version", 42), "realm", Map.of("id", 7)) - )), new HashMap<>()); + IngestDocument ingestDocument = new IngestDocument( + IngestDocument.deepCopyMap(Map.of("_field", Map.of("api_key", Map.of("version", 42), "realm", Map.of("id", 7)))), + new HashMap<>() + ); SetSecurityUserProcessor processor = new SetSecurityUserProcessor( - "_tag", null, securityContext, Settings.EMPTY, "_field", EnumSet.allOf(Property.class)); + "_tag", + null, + securityContext, + Settings.EMPTY, + "_field", + EnumSet.allOf(Property.class) + ); processor.execute(ingestDocument); Map result = ingestDocument.getFieldValue("_field", Map.class); @@ -324,18 +420,35 @@ public void testWillNotOverwriteExistingApiKeyAndRealm() throws Exception { public void testWillSetRunAsRealmForNonApiKeyAuth() throws Exception { User user = new User(randomAlphaOfLengthBetween(4, 12), null, null); Authentication.RealmRef authRealmRef = new Authentication.RealmRef( - randomAlphaOfLengthBetween(4, 12), randomAlphaOfLengthBetween(4, 12), randomAlphaOfLengthBetween(4, 12)); + randomAlphaOfLengthBetween(4, 12), + randomAlphaOfLengthBetween(4, 12), + randomAlphaOfLengthBetween(4, 12) + ); Authentication.RealmRef lookedUpRealmRef = new Authentication.RealmRef( - randomAlphaOfLengthBetween(4, 12), randomAlphaOfLengthBetween(4, 12), randomAlphaOfLengthBetween(4, 12)); - - Authentication auth = new Authentication(user, authRealmRef, lookedUpRealmRef, Version.CURRENT, + randomAlphaOfLengthBetween(4, 12), + randomAlphaOfLengthBetween(4, 12), + randomAlphaOfLengthBetween(4, 12) + ); + + Authentication auth = new Authentication( + user, + authRealmRef, + lookedUpRealmRef, + Version.CURRENT, randomFrom(AuthenticationType.REALM, AuthenticationType.TOKEN, AuthenticationType.INTERNAL), - Collections.emptyMap()); + Collections.emptyMap() + ); auth.writeToContext(threadContext); IngestDocument ingestDocument = new IngestDocument(new HashMap<>(), new HashMap<>()); SetSecurityUserProcessor processor = new SetSecurityUserProcessor( - "_tag", null, securityContext, Settings.EMPTY, "_field", EnumSet.allOf(Property.class)); + "_tag", + null, + securityContext, + Settings.EMPTY, + "_field", + EnumSet.allOf(Property.class) + ); processor.execute(ingestDocument); Map result = ingestDocument.getFieldValue("_field", Map.class); @@ -355,14 +468,24 @@ private User randomUser() { private User doRandomUser() { if (randomIntBetween(0, 2) < 2) { - return new User(randomAlphaOfLengthBetween(3, 8), + return new User( + randomAlphaOfLengthBetween(3, 8), randomArray(1, 3, String[]::new, () -> randomAlphaOfLengthBetween(3, 8)), - randomAlphaOfLengthBetween(5, 20), randomAlphaOfLengthBetween(8, 20), - randomFrom(Map.of(), Map.of("key", "value")), true); + randomAlphaOfLengthBetween(5, 20), + randomAlphaOfLengthBetween(8, 20), + randomFrom(Map.of(), Map.of("key", "value")), + true + ); } else { final String principal = randomAlphaOfLengthBetween(3, 8) + "/" + randomAlphaOfLengthBetween(3, 8); - return new User(principal, Strings.EMPTY_ARRAY, "Service account - " + principal, null, - randomFrom(Map.of(), Map.of("_elastic_service_account", true)), true); + return new User( + principal, + Strings.EMPTY_ARRAY, + "Service account - " + principal, + null, + randomFrom(Map.of(), Map.of("_elastic_service_account", true)), + true + ); } } @@ -370,11 +493,20 @@ private Authentication randomAuthentication() { final User user = randomUser(); if (user.fullName().startsWith("Service account - ")) { assert false == user.isRunAs() : "cannot run-as service account"; - final Authentication.RealmRef authBy = - new Authentication.RealmRef("_service_account", "_service_account", randomAlphaOfLengthBetween(3, 8)); + final Authentication.RealmRef authBy = new Authentication.RealmRef( + "_service_account", + "_service_account", + randomAlphaOfLengthBetween(3, 8) + ); final TokenInfo.TokenSource tokenSource = randomFrom(TokenInfo.TokenSource.values()); - return new Authentication(user, authBy, null, Version.CURRENT, AuthenticationType.TOKEN, - Map.of("_token_name", ValidationTests.randomTokenName(), "_token_source", tokenSource.name().toLowerCase(Locale.ROOT))); + return new Authentication( + user, + authBy, + null, + Version.CURRENT, + AuthenticationType.TOKEN, + Map.of("_token_name", ValidationTests.randomTokenName(), "_token_source", tokenSource.name().toLowerCase(Locale.ROOT)) + ); } else { final Authentication.RealmRef lookupBy; final String nodeName = randomAlphaOfLengthBetween(3, 8); @@ -383,9 +515,13 @@ private Authentication randomAuthentication() { } else { lookupBy = null; } - final Authentication.RealmRef authBy = - new Authentication.RealmRef(randomAlphaOfLengthBetween(3, 8), randomAlphaOfLengthBetween(3, 8), nodeName); - final AuthenticationType authenticationType = user.isRunAs() ? AuthenticationType.REALM + final Authentication.RealmRef authBy = new Authentication.RealmRef( + randomAlphaOfLengthBetween(3, 8), + randomAlphaOfLengthBetween(3, 8), + nodeName + ); + final AuthenticationType authenticationType = user.isRunAs() + ? AuthenticationType.REALM : randomFrom(AuthenticationType.REALM, AuthenticationType.INTERNAL, AuthenticationType.TOKEN, AuthenticationType.ANONYMOUS); final Map metadata = user.isRunAs() ? Map.of() : randomFrom(Map.of(), Map.of("foo", "bar")); return new Authentication(user, authBy, lookupBy, Version.CURRENT, authenticationType, metadata); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/operator/FileOperatorUsersStoreTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/operator/FileOperatorUsersStoreTests.java index 7f0e73ae7ca2a..b6ce6bf510efd 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/operator/FileOperatorUsersStoreTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/operator/FileOperatorUsersStoreTests.java @@ -14,7 +14,6 @@ import org.elasticsearch.Version; import org.elasticsearch.common.logging.Loggers; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentParseException; import org.elasticsearch.env.Environment; import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.test.ESTestCase; @@ -22,6 +21,7 @@ import org.elasticsearch.threadpool.TestThreadPool; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.watcher.ResourceWatcherService; +import org.elasticsearch.xcontent.XContentParseException; import org.elasticsearch.xpack.core.security.audit.logfile.CapturingLogger; import org.elasticsearch.xpack.core.security.authc.Authentication; import org.elasticsearch.xpack.core.security.user.User; @@ -51,10 +51,7 @@ public class FileOperatorUsersStoreTests extends ESTestCase { @Before public void init() { - settings = Settings.builder() - .put("resource.reload.interval.high", "100ms") - .put("path.home", createTempDir()) - .build(); + settings = Settings.builder().put("resource.reload.interval.high", "100ms").put("path.home", createTempDir()).build(); env = TestEnvironment.newEnvironment(settings); threadPool = new TestThreadPool("test"); } @@ -78,9 +75,15 @@ public void testIsOperator() throws IOException { // user operator_3 is an operator and its file realm can have any name final Authentication.RealmRef anotherFileRealm = new Authentication.RealmRef( - randomAlphaOfLengthBetween(3, 8), "file", randomAlphaOfLength(8)); - assertTrue(fileOperatorUsersStore.isOperatorUser( - new Authentication(new User("operator_3", randomRoles()), anotherFileRealm, anotherFileRealm))); + randomAlphaOfLengthBetween(3, 8), + "file", + randomAlphaOfLength(8) + ); + assertTrue( + fileOperatorUsersStore.isOperatorUser( + new Authentication(new User("operator_3", randomRoles()), anotherFileRealm, anotherFileRealm) + ) + ); // user operator_1 from a different realm is not an operator final Authentication.RealmRef differentRealm = randomFrom( @@ -91,8 +94,11 @@ public void testIsOperator() throws IOException { assertFalse(fileOperatorUsersStore.isOperatorUser(new Authentication(operator_1, differentRealm, differentRealm))); // user operator_1 with non realm auth type is not an operator - assertFalse(fileOperatorUsersStore.isOperatorUser( - new Authentication(operator_1, fileRealm, fileRealm, Version.CURRENT, Authentication.AuthenticationType.TOKEN, Map.of()))); + assertFalse( + fileOperatorUsersStore.isOperatorUser( + new Authentication(operator_1, fileRealm, fileRealm, Version.CURRENT, Authentication.AuthenticationType.TOKEN, Map.of()) + ) + ); } public void testFileAutoReload() throws Exception { @@ -120,8 +126,7 @@ public void testFileAutoReload() throws Exception { final List groups = fileOperatorUsersStore.getOperatorUsersDescriptor().getGroups(); assertEquals(2, groups.size()); - assertEquals(new FileOperatorUsersStore.Group(Set.of("operator_1", "operator_2"), - "file"), groups.get(0)); + assertEquals(new FileOperatorUsersStore.Group(Set.of("operator_1", "operator_2"), "file"), groups.get(0)); assertEquals(new FileOperatorUsersStore.Group(Set.of("operator_3"), null), groups.get(1)); appender.assertAllExpectationsMatched(); @@ -176,8 +181,8 @@ public void testFileAutoReload() throws Exception { "file not exist warning", logger.getName(), Level.WARN, - "Operator privileges [xpack.security.operator_privileges.enabled] is enabled, " + - "but operator user file does not exist. No user will be able to perform operator-only actions." + "Operator privileges [xpack.security.operator_privileges.enabled] is enabled, " + + "but operator user file does not exist. No user will be able to perform operator-only actions." ) ); Files.delete(inUseFile); @@ -198,8 +203,10 @@ public void testMalFormattedOrEmptyFile() throws IOException { // Mal-formatted file is functionally equivalent to an empty file writeOperatorUsers(randomBoolean() ? "foobar" : ""); try (ResourceWatcherService watcherService = new ResourceWatcherService(settings, threadPool)) { - final ElasticsearchParseException e = - expectThrows(ElasticsearchParseException.class, () -> new FileOperatorUsersStore(env, watcherService)); + final ElasticsearchParseException e = expectThrows( + ElasticsearchParseException.class, + () -> new FileOperatorUsersStore(env, watcherService) + ); assertThat(e.getMessage(), containsString("Error parsing operator users file")); } } @@ -215,9 +222,7 @@ public void testParseFileWhenFileDoesNotExist() throws Exception { } public void testParseConfig() throws IOException { - String config = "" - + "operator:\n" - + " - usernames: [\"operator_1\"]\n"; + String config = "" + "operator:\n" + " - usernames: [\"operator_1\"]\n"; try (ByteArrayInputStream in = new ByteArrayInputStream(config.getBytes(StandardCharsets.UTF_8))) { final List groups = FileOperatorUsersStore.parseConfig(in).getGroups(); assertEquals(1, groups.size()); @@ -255,33 +260,22 @@ public void testParseConfig() throws IOException { } public void testParseInvalidConfig() throws IOException { - String config = "" - + "operator:\n" - + " - usernames: [\"operator_1\"]\n" - + " realm_type: \"native\"\n"; + String config = "" + "operator:\n" + " - usernames: [\"operator_1\"]\n" + " realm_type: \"native\"\n"; try (ByteArrayInputStream in = new ByteArrayInputStream(config.getBytes(StandardCharsets.UTF_8))) { - final XContentParseException e = expectThrows(XContentParseException.class, - () -> FileOperatorUsersStore.parseConfig(in)); + final XContentParseException e = expectThrows(XContentParseException.class, () -> FileOperatorUsersStore.parseConfig(in)); assertThat(e.getCause().getCause().getMessage(), containsString("[realm_type] only supports [file]")); } - config = "" - + "operator:\n" - + " - usernames: [\"operator_1\"]\n" - + " auth_type: \"token\"\n"; + config = "" + "operator:\n" + " - usernames: [\"operator_1\"]\n" + " auth_type: \"token\"\n"; try (ByteArrayInputStream in = new ByteArrayInputStream(config.getBytes(StandardCharsets.UTF_8))) { - final XContentParseException e = expectThrows(XContentParseException.class, - () -> FileOperatorUsersStore.parseConfig(in)); + final XContentParseException e = expectThrows(XContentParseException.class, () -> FileOperatorUsersStore.parseConfig(in)); assertThat(e.getCause().getCause().getMessage(), containsString("[auth_type] only supports [realm]")); } - config = "" - + "operator:\n" - + " auth_type: \"realm\"\n"; + config = "" + "operator:\n" + " auth_type: \"realm\"\n"; try (ByteArrayInputStream in = new ByteArrayInputStream(config.getBytes(StandardCharsets.UTF_8))) { - final XContentParseException e = expectThrows(XContentParseException.class, - () -> FileOperatorUsersStore.parseConfig(in)); + final XContentParseException e = expectThrows(XContentParseException.class, () -> FileOperatorUsersStore.parseConfig(in)); assertThat(e.getCause().getMessage(), containsString("Required [usernames]")); } } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/operator/OperatorOnlyRegistryTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/operator/OperatorOnlyRegistryTests.java index 69618d33f2aa6..56141b68814a1 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/operator/OperatorOnlyRegistryTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/operator/OperatorOnlyRegistryTests.java @@ -46,7 +46,8 @@ public class OperatorOnlyRegistryTests extends ESTestCase { PROFILE_FILTER_DENY_SETTING, PROFILE_FILTER_ALLOW_SETTING, HTTP_FILTER_ALLOW_SETTING, - HTTP_FILTER_DENY_SETTING); + HTTP_FILTER_DENY_SETTING + ); private OperatorOnlyRegistry operatorOnlyRegistry; @@ -67,7 +68,9 @@ public void testSimpleOperatorOnlyApi() { public void testNonOperatorOnlyApi() { final String actionName = randomValueOtherThanMany( - OperatorOnlyRegistry.SIMPLE_ACTIONS::contains, () -> randomAlphaOfLengthBetween(10, 40)); + OperatorOnlyRegistry.SIMPLE_ACTIONS::contains, + () -> randomAlphaOfLengthBetween(10, 40) + ); assertNull(operatorOnlyRegistry.check(actionName, null)); } @@ -81,27 +84,28 @@ public void testOperatorOnlySettings() { case 0: transientSetting = convertToConcreteSettingIfNecessary(randomFrom(IP_FILTER_SETTINGS)); persistentSetting = convertToConcreteSettingIfNecessary( - randomValueOtherThan(transientSetting, () -> randomFrom(IP_FILTER_SETTINGS))); + randomValueOtherThan(transientSetting, () -> randomFrom(IP_FILTER_SETTINGS)) + ); request = prepareClusterUpdateSettingsRequest(transientSetting, persistentSetting); violation = operatorOnlyRegistry.check(ClusterUpdateSettingsAction.NAME, request); - assertThat(violation.message(), containsString(String.format(Locale.ROOT, "settings [%s,%s]", - transientSetting.getKey(), persistentSetting.getKey()))); + assertThat( + violation.message(), + containsString(String.format(Locale.ROOT, "settings [%s,%s]", transientSetting.getKey(), persistentSetting.getKey())) + ); break; case 1: transientSetting = convertToConcreteSettingIfNecessary(randomFrom(IP_FILTER_SETTINGS)); persistentSetting = convertToConcreteSettingIfNecessary(randomFrom(DYNAMIC_SETTINGS)); request = prepareClusterUpdateSettingsRequest(transientSetting, persistentSetting); violation = operatorOnlyRegistry.check(ClusterUpdateSettingsAction.NAME, request); - assertThat(violation.message(), containsString(String.format(Locale.ROOT, "setting [%s]", - transientSetting.getKey()))); + assertThat(violation.message(), containsString(String.format(Locale.ROOT, "setting [%s]", transientSetting.getKey()))); break; case 2: transientSetting = convertToConcreteSettingIfNecessary(randomFrom(DYNAMIC_SETTINGS)); persistentSetting = convertToConcreteSettingIfNecessary(randomFrom(IP_FILTER_SETTINGS)); request = prepareClusterUpdateSettingsRequest(transientSetting, persistentSetting); violation = operatorOnlyRegistry.check(ClusterUpdateSettingsAction.NAME, request); - assertThat(violation.message(), containsString(String.format(Locale.ROOT, "setting [%s]", - persistentSetting.getKey()))); + assertThat(violation.message(), containsString(String.format(Locale.ROOT, "setting [%s]", persistentSetting.getKey()))); break; case 3: transientSetting = convertToConcreteSettingIfNecessary(randomFrom(DYNAMIC_SETTINGS)); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/operator/OperatorPrivilegesTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/operator/OperatorPrivilegesTests.java index 5d6e5fa231df9..836b5b52b8fb4 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/operator/OperatorPrivilegesTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/operator/OperatorPrivilegesTests.java @@ -75,16 +75,18 @@ public void testWillMarkThreadContextForAllLicenses() { public void testWillNotCheckWhenLicenseDoesNotSupport() { when(xPackLicenseState.isAllowed(Security.OPERATOR_PRIVILEGES_FEATURE)).thenReturn(false); final ThreadContext threadContext = new ThreadContext(Settings.EMPTY); - final ElasticsearchSecurityException e = - operatorPrivilegesService.check(mock(Authentication.class), "cluster:action", mock(TransportRequest.class), threadContext); + final ElasticsearchSecurityException e = operatorPrivilegesService.check( + mock(Authentication.class), + "cluster:action", + mock(TransportRequest.class), + threadContext + ); assertNull(e); verifyZeroInteractions(operatorOnlyRegistry); } public void testMarkOperatorUser() throws IllegalAccessException { - final Settings settings = Settings.builder() - .put("xpack.security.operator_privileges.enabled", true) - .build(); + final Settings settings = Settings.builder().put("xpack.security.operator_privileges.enabled", true).build(); when(xPackLicenseState.isAllowed(Security.OPERATOR_PRIVILEGES_FEATURE)).thenReturn(true); final Authentication operatorAuth = mock(Authentication.class); final Authentication nonOperatorAuth = mock(Authentication.class); @@ -112,8 +114,10 @@ public void testMarkOperatorUser() throws IllegalAccessException { ) ); operatorPrivilegesService.maybeMarkOperatorUser(operatorAuth, threadContext); - assertEquals(AuthenticationField.PRIVILEGE_CATEGORY_VALUE_OPERATOR, - threadContext.getHeader(AuthenticationField.PRIVILEGE_CATEGORY_KEY)); + assertEquals( + AuthenticationField.PRIVILEGE_CATEGORY_VALUE_OPERATOR, + threadContext.getHeader(AuthenticationField.PRIVILEGE_CATEGORY_KEY) + ); appender.assertAllExpectationsMatched(); } finally { Loggers.removeAppender(logger, appender); @@ -126,7 +130,8 @@ public void testMarkOperatorUser() throws IllegalAccessException { operatorPrivilegesService.maybeMarkOperatorUser(nonOperatorAuth, threadContext); assertThat( threadContext.getHeader(AuthenticationField.PRIVILEGE_CATEGORY_KEY), - equalTo(AuthenticationField.PRIVILEGE_CATEGORY_VALUE_EMPTY)); + equalTo(AuthenticationField.PRIVILEGE_CATEGORY_VALUE_EMPTY) + ); // Will mark empty for run_as user final Authentication runAsAuth = mock(Authentication.class); @@ -137,13 +142,15 @@ public void testMarkOperatorUser() throws IllegalAccessException { operatorPrivilegesService.maybeMarkOperatorUser(runAsAuth, threadContext); assertThat( threadContext.getHeader(AuthenticationField.PRIVILEGE_CATEGORY_KEY), - equalTo(AuthenticationField.PRIVILEGE_CATEGORY_VALUE_EMPTY)); + equalTo(AuthenticationField.PRIVILEGE_CATEGORY_VALUE_EMPTY) + ); verify(fileOperatorUsersStore, never()).isOperatorUser(any()); // Will not mark for internal users final Authentication internalAuth = mock(Authentication.class); when(internalAuth.getUser()).thenReturn( - randomFrom(SystemUser.INSTANCE, XPackUser.INSTANCE, XPackSecurityUser.INSTANCE, AsyncSearchUser.INSTANCE)); + randomFrom(SystemUser.INSTANCE, XPackUser.INSTANCE, XPackSecurityUser.INSTANCE, AsyncSearchUser.INSTANCE) + ); threadContext = new ThreadContext(settings); operatorPrivilegesService.maybeMarkOperatorUser(internalAuth, threadContext); assertNull(threadContext.getHeader(AuthenticationField.PRIVILEGE_CATEGORY_KEY)); @@ -159,9 +166,7 @@ public void testMarkOperatorUser() throws IllegalAccessException { } public void testCheck() { - final Settings settings = Settings.builder() - .put("xpack.security.operator_privileges.enabled", true) - .build(); + final Settings settings = Settings.builder().put("xpack.security.operator_privileges.enabled", true).build(); when(xPackLicenseState.isAllowed(Security.OPERATOR_PRIVILEGES_FEATURE)).thenReturn(true); final String operatorAction = "cluster:operator_only/action"; @@ -178,7 +183,11 @@ public void testCheck() { assertNull(operatorPrivilegesService.check(authentication, operatorAction, mock(TransportRequest.class), threadContext)); } else { final ElasticsearchSecurityException e = operatorPrivilegesService.check( - authentication, operatorAction, mock(TransportRequest.class), threadContext); + authentication, + operatorAction, + mock(TransportRequest.class), + threadContext + ); assertNotNull(e); assertThat(e.getMessage(), containsString("Operator privileges are required for " + message)); } @@ -190,9 +199,16 @@ public void testCheckWillPassForInternalUsers() { when(xPackLicenseState.isAllowed(Security.OPERATOR_PRIVILEGES_FEATURE)).thenReturn(true); final Authentication internalAuth = mock(Authentication.class); when(internalAuth.getUser()).thenReturn( - randomFrom(SystemUser.INSTANCE, XPackUser.INSTANCE, XPackSecurityUser.INSTANCE, AsyncSearchUser.INSTANCE)); - assertNull(operatorPrivilegesService.check( - internalAuth, randomAlphaOfLengthBetween(20, 30), mock(TransportRequest.class), new ThreadContext(Settings.EMPTY))); + randomFrom(SystemUser.INSTANCE, XPackUser.INSTANCE, XPackSecurityUser.INSTANCE, AsyncSearchUser.INSTANCE) + ); + assertNull( + operatorPrivilegesService.check( + internalAuth, + randomAlphaOfLengthBetween(20, 30), + mock(TransportRequest.class), + new ThreadContext(Settings.EMPTY) + ) + ); verify(operatorOnlyRegistry, never()).check(anyString(), any()); } @@ -240,8 +256,9 @@ public void testNoOpService() { assertNull(threadContext.getHeader(AuthenticationField.PRIVILEGE_CATEGORY_KEY)); final TransportRequest request = mock(TransportRequest.class); - assertNull(NOOP_OPERATOR_PRIVILEGES_SERVICE.check( - mock(Authentication.class), randomAlphaOfLengthBetween(10, 20), request, threadContext)); + assertNull( + NOOP_OPERATOR_PRIVILEGES_SERVICE.check(mock(Authentication.class), randomAlphaOfLengthBetween(10, 20), request, threadContext) + ); verifyZeroInteractions(request); } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/RestRequestFilterTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/RestRequestFilterTests.java index 0abe05308608c..22ccf41695c21 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/RestRequestFilterTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/RestRequestFilterTests.java @@ -8,13 +8,13 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.xcontent.DeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.rest.RestRequest; +import org.elasticsearch.rest.RestRequestFilter; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.rest.FakeRestRequest; -import org.elasticsearch.rest.RestRequestFilter; +import org.elasticsearch.xcontent.DeprecationHandler; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentType; import java.io.IOException; import java.net.InetAddress; @@ -27,14 +27,14 @@ public class RestRequestFilterTests extends ESTestCase { public void testFilteringItemsInSubLevels() throws IOException { BytesReference content = new BytesArray("{\"root\": {\"second\": {\"third\": \"password\", \"foo\": \"bar\"}}}"); RestRequestFilter filter = () -> Collections.singleton("root.second.third"); - FakeRestRequest restRequest = - new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withContent(content, XContentType.JSON).build(); + FakeRestRequest restRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withContent(content, XContentType.JSON) + .build(); RestRequest filtered = filter.getFilteredRequest(restRequest); assertNotEquals(content, filtered.content()); Map map = XContentType.JSON.xContent() - .createParser(NamedXContentRegistry.EMPTY, - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, filtered.content().streamInput()).map(); + .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, filtered.content().streamInput()) + .map(); @SuppressWarnings("unchecked") Map root = (Map) map.get("root"); assertNotNull(root); @@ -48,14 +48,14 @@ public void testFilteringItemsInSubLevels() throws IOException { public void testFilteringItemsInSubLevelsWithWildCard() throws IOException { BytesReference content = new BytesArray("{\"root\": {\"second\": {\"third\": \"password\", \"foo\": \"bar\"}}}"); RestRequestFilter filter = () -> Collections.singleton("root.*.third"); - FakeRestRequest restRequest = - new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withContent(content, XContentType.JSON).build(); + FakeRestRequest restRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withContent(content, XContentType.JSON) + .build(); RestRequest filtered = filter.getFilteredRequest(restRequest); assertNotEquals(content, filtered.content()); Map map = XContentType.JSON.xContent() - .createParser(NamedXContentRegistry.EMPTY, - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, filtered.content().streamInput()).map(); + .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, filtered.content().streamInput()) + .map(); @SuppressWarnings("unchecked") Map root = (Map) map.get("root"); assertNotNull(root); @@ -69,14 +69,14 @@ public void testFilteringItemsInSubLevelsWithWildCard() throws IOException { public void testFilteringItemsInSubLevelsWithLeadingWildCard() throws IOException { BytesReference content = new BytesArray("{\"root\": {\"second\": {\"third\": \"password\", \"foo\": \"bar\"}}}"); RestRequestFilter filter = () -> Collections.singleton("*.third"); - FakeRestRequest restRequest = - new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withContent(content, XContentType.JSON).build(); + FakeRestRequest restRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withContent(content, XContentType.JSON) + .build(); RestRequest filtered = filter.getFilteredRequest(restRequest); assertNotEquals(content, filtered.content()); Map map = XContentType.JSON.xContent() - .createParser(NamedXContentRegistry.EMPTY, - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, filtered.content().streamInput()).map(); + .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, filtered.content().streamInput()) + .map(); @SuppressWarnings("unchecked") Map root = (Map) map.get("root"); assertNotNull(root); @@ -91,9 +91,9 @@ public void testRemoteAddressWorks() throws IOException { BytesReference content = new BytesArray("{\"root\": {\"second\": {\"third\": \"password\", \"foo\": \"bar\"}}}"); RestRequestFilter filter = () -> Collections.singleton("*.third"); InetSocketAddress address = new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 32768); - FakeRestRequest restRequest = - new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withContent(content, XContentType.JSON) - .withRemoteAddress(address).build(); + FakeRestRequest restRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withContent(content, XContentType.JSON) + .withRemoteAddress(address) + .build(); RestRequest filtered = filter.getFilteredRequest(restRequest); assertEquals(address, filtered.getHttpChannel().getRemoteAddress()); } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/SecurityRestFilterTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/SecurityRestFilterTests.java index a705df99fb01a..813cde61d82aa 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/SecurityRestFilterTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/SecurityRestFilterTests.java @@ -7,6 +7,7 @@ package org.elasticsearch.xpack.security.rest; import com.nimbusds.jose.util.StandardCharset; + import org.apache.lucene.util.SetOnce; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.action.ActionListener; @@ -14,26 +15,26 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.DeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.http.HttpChannel; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestChannel; import org.elasticsearch.rest.RestHandler; import org.elasticsearch.rest.RestRequest; +import org.elasticsearch.rest.RestRequestFilter; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.SecuritySettingsSourceField; import org.elasticsearch.test.rest.FakeRestRequest; +import org.elasticsearch.xcontent.DeprecationHandler; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.xpack.core.security.SecurityContext; import org.elasticsearch.xpack.core.security.authc.Authentication; import org.elasticsearch.xpack.core.security.authc.Authentication.RealmRef; import org.elasticsearch.xpack.core.security.authc.support.SecondaryAuthentication; -import org.elasticsearch.rest.RestRequestFilter; import org.elasticsearch.xpack.core.security.user.XPackUser; import org.elasticsearch.xpack.security.authc.AuthenticationService; import org.elasticsearch.xpack.security.authc.support.SecondaryAuthenticator; @@ -129,8 +130,10 @@ public void testProcessSecondaryAuthentication() throws Exception { }).when(restHandler).handleRequest(request, channel, null); final String credentials = randomAlphaOfLengthBetween(4, 8) + ":" + randomAlphaOfLengthBetween(4, 12); - threadContext.putHeader(SecondaryAuthenticator.SECONDARY_AUTH_HEADER_NAME, - "Basic " + Base64.getEncoder().encodeToString(credentials.getBytes(StandardCharset.UTF_8))); + threadContext.putHeader( + SecondaryAuthenticator.SECONDARY_AUTH_HEADER_NAME, + "Basic " + Base64.getEncoder().encodeToString(credentials.getBytes(StandardCharset.UTF_8)) + ); filter.handleRequest(request, channel, null); verify(restHandler).handleRequest(request, channel, null); verifyZeroInteractions(channel); @@ -150,26 +153,60 @@ public void testProcessWithSecurityDisabled() throws Exception { public void testProcessAuthenticationFailedNoTrace() throws Exception { filter = new SecurityRestFilter(Settings.EMPTY, threadContext, authcService, secondaryAuthenticator, restHandler, false); - testProcessAuthenticationFailed(randomBoolean() ? authenticationError("failed authn") : authenticationError("failed authn with " + - "cause", new ElasticsearchException("cause")), RestStatus.UNAUTHORIZED, true, true, false); - testProcessAuthenticationFailed(randomBoolean() ? authenticationError("failed authn") : authenticationError("failed authn with " + - "cause", new ElasticsearchException("cause")), RestStatus.UNAUTHORIZED, true, false, false); - testProcessAuthenticationFailed(randomBoolean() ? authenticationError("failed authn") : authenticationError("failed authn with " + - "cause", new ElasticsearchException("cause")), RestStatus.UNAUTHORIZED, false, true, false); - testProcessAuthenticationFailed(randomBoolean() ? authenticationError("failed authn") : authenticationError("failed authn with " + - "cause", new ElasticsearchException("cause")), RestStatus.UNAUTHORIZED, false, false, false); + testProcessAuthenticationFailed( + randomBoolean() + ? authenticationError("failed authn") + : authenticationError("failed authn with " + "cause", new ElasticsearchException("cause")), + RestStatus.UNAUTHORIZED, + true, + true, + false + ); + testProcessAuthenticationFailed( + randomBoolean() + ? authenticationError("failed authn") + : authenticationError("failed authn with " + "cause", new ElasticsearchException("cause")), + RestStatus.UNAUTHORIZED, + true, + false, + false + ); + testProcessAuthenticationFailed( + randomBoolean() + ? authenticationError("failed authn") + : authenticationError("failed authn with " + "cause", new ElasticsearchException("cause")), + RestStatus.UNAUTHORIZED, + false, + true, + false + ); + testProcessAuthenticationFailed( + randomBoolean() + ? authenticationError("failed authn") + : authenticationError("failed authn with " + "cause", new ElasticsearchException("cause")), + RestStatus.UNAUTHORIZED, + false, + false, + false + ); testProcessAuthenticationFailed(new ElasticsearchException("dummy"), RestStatus.INTERNAL_SERVER_ERROR, false, false, false); testProcessAuthenticationFailed(new IllegalArgumentException("dummy"), RestStatus.BAD_REQUEST, true, false, false); testProcessAuthenticationFailed(new ElasticsearchException("dummy"), RestStatus.INTERNAL_SERVER_ERROR, false, true, false); testProcessAuthenticationFailed(new IllegalArgumentException("dummy"), RestStatus.BAD_REQUEST, true, true, true); } - private void testProcessAuthenticationFailed(Exception authnException, RestStatus expectedRestStatus, boolean errorTrace, - boolean detailedErrorsEnabled, boolean traceExists) throws Exception { + private void testProcessAuthenticationFailed( + Exception authnException, + RestStatus expectedRestStatus, + boolean errorTrace, + boolean detailedErrorsEnabled, + boolean traceExists + ) throws Exception { RestRequest request; if (errorTrace != ElasticsearchException.REST_EXCEPTION_SKIP_STACK_TRACE_DEFAULT == false || randomBoolean()) { - request = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY) - .withParams(Map.of("error_trace", Boolean.toString(errorTrace))).build(); + request = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withParams( + Map.of("error_trace", Boolean.toString(errorTrace)) + ).build(); } else { // sometimes do not fill in the default value request = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).build(); @@ -206,9 +243,10 @@ public void testProcessOptionsMethod() throws Exception { } public void testProcessFiltersBodyCorrectly() throws Exception { - FakeRestRequest restRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY) - .withContent(new BytesArray("{\"password\": \"" + SecuritySettingsSourceField.TEST_PASSWORD + "\", \"foo\": \"bar\"}"), - XContentType.JSON).build(); + FakeRestRequest restRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withContent( + new BytesArray("{\"password\": \"" + SecuritySettingsSourceField.TEST_PASSWORD + "\", \"foo\": \"bar\"}"), + XContentType.JSON + ).build(); when(channel.request()).thenReturn(restRequest); SetOnce handlerRequest = new SetOnce<>(); restHandler = new FilteredRestHandler() { @@ -237,8 +275,12 @@ public Set getFilteredFields() { assertEquals(restRequest, handlerRequest.get()); assertEquals(restRequest.content(), handlerRequest.get().content()); Map original = XContentType.JSON.xContent() - .createParser(NamedXContentRegistry.EMPTY, - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, handlerRequest.get().content().streamInput()).map(); + .createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + handlerRequest.get().content().streamInput() + ) + .map(); assertEquals(2, original.size()); assertEquals(SecuritySettingsSourceField.TEST_PASSWORD, original.get("password")); assertEquals("bar", original.get("foo")); @@ -247,12 +289,15 @@ public Set getFilteredFields() { assertNotEquals(restRequest.content(), authcServiceRequest.get().content()); Map map = XContentType.JSON.xContent() - .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, - authcServiceRequest.get().content().streamInput()).map(); + .createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + authcServiceRequest.get().content().streamInput() + ) + .map(); assertEquals(1, map.size()); assertEquals("bar", map.get("foo")); } - private interface FilteredRestHandler extends RestHandler, RestRequestFilter { - } + private interface FilteredRestHandler extends RestHandler, RestRequestFilter {} } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/SecurityRestFilterWarningHeadersTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/SecurityRestFilterWarningHeadersTests.java index 83fbcae00a9dc..1488d01b056a1 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/SecurityRestFilterWarningHeadersTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/SecurityRestFilterWarningHeadersTests.java @@ -12,8 +12,6 @@ import org.elasticsearch.common.collect.MapBuilder; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestChannel; import org.elasticsearch.rest.RestHandler; @@ -22,6 +20,8 @@ import org.elasticsearch.rest.RestStatus; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.rest.FakeRestRequest; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.security.authc.Authentication; import org.elasticsearch.xpack.security.authc.AuthenticationService; import org.elasticsearch.xpack.security.authc.support.SecondaryAuthenticator; @@ -32,10 +32,10 @@ import java.util.List; import java.util.Map; -import static org.mockito.Mockito.doThrow; import static org.elasticsearch.test.ActionListenerUtils.anyActionListener; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -89,8 +89,14 @@ public void testResponseHeadersOnFailure() throws Exception { private Map> testProcessRestHandlingFailed(RestStatus restStatus, MapBuilder> headers) throws Exception { RestChannel channel = mock(RestChannel.class); - SecurityRestFilter filter = new SecurityRestFilter(Settings.EMPTY, threadContext, authcService, secondaryAuthenticator, - restHandler, false); + SecurityRestFilter filter = new SecurityRestFilter( + Settings.EMPTY, + threadContext, + authcService, + secondaryAuthenticator, + restHandler, + false + ); RestRequest request = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).build(); Authentication primaryAuthentication = mock(Authentication.class); when(primaryAuthentication.encode()).thenReturn(randomAlphaOfLengthBetween(12, 36)); @@ -110,8 +116,8 @@ private Map> testProcessRestHandlingFailed(RestStatus restS callback.onResponse(secondaryAuthentication); return null; }).when(authcService).authenticate(eq(request), eq(false), anyActionListener()); - doThrow(new ElasticsearchStatusException("Rest handling failed", restStatus, "")) - .when(restHandler).handleRequest(request, channel, null); + doThrow(new ElasticsearchStatusException("Rest handling failed", restStatus, "")).when(restHandler) + .handleRequest(request, channel, null); when(channel.request()).thenReturn(request); when(channel.newErrorBuilder()).thenReturn(JsonXContent.contentBuilder()); filter.handleRequest(request, channel, null); @@ -124,8 +130,14 @@ private Map> testProcessRestHandlingFailed(RestStatus restS private Map> testProcessAuthenticationFailed(RestStatus restStatus, MapBuilder> headers) throws Exception { RestChannel channel = mock(RestChannel.class); - SecurityRestFilter filter = new SecurityRestFilter(Settings.EMPTY, threadContext, authcService, secondaryAuthenticator, - restHandler, false); + SecurityRestFilter filter = new SecurityRestFilter( + Settings.EMPTY, + threadContext, + authcService, + secondaryAuthenticator, + restHandler, + false + ); RestRequest request = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).build(); doAnswer((i) -> { ActionListener callback = (ActionListener) i.getArguments()[1]; diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/SecurityBaseRestHandlerTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/SecurityBaseRestHandlerTests.java index 32db18d9302f2..46609cc115ccf 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/SecurityBaseRestHandlerTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/SecurityBaseRestHandlerTests.java @@ -34,7 +34,8 @@ public void testSecurityBaseRestHandlerChecksLicenseState() throws Exception { final AtomicBoolean consumerCalled = new AtomicBoolean(false); final XPackLicenseState licenseState = mock(XPackLicenseState.class); when(licenseState.getOperationMode()).thenReturn( - randomFrom(License.OperationMode.BASIC, License.OperationMode.STANDARD, License.OperationMode.GOLD)); + randomFrom(License.OperationMode.BASIC, License.OperationMode.STANDARD, License.OperationMode.GOLD) + ); SecurityBaseRestHandler handler = new SecurityBaseRestHandler(settings, licenseState) { @Override diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/apikey/RestCreateApiKeyActionTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/apikey/RestCreateApiKeyActionTests.java index c1abf3df19c7d..761107322bbaa 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/apikey/RestCreateApiKeyActionTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/apikey/RestCreateApiKeyActionTests.java @@ -17,8 +17,6 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.env.Environment; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.AbstractRestChannel; @@ -28,6 +26,8 @@ import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.rest.FakeRestRequest; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.security.action.CreateApiKeyRequest; import org.elasticsearch.xpack.core.security.action.CreateApiKeyResponse; @@ -48,10 +48,10 @@ public class RestCreateApiKeyActionTests extends ESTestCase { public void setUp() throws Exception { super.setUp(); settings = Settings.builder() - .put("path.home", createTempDir().toString()) - .put("node.name", "test-" + getTestName()) - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) - .build(); + .put("path.home", createTempDir().toString()) + .put("node.name", "test-" + getTestName()) + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) + .build(); threadPool = new ThreadPool(settings); } @@ -61,13 +61,13 @@ public void tearDown() throws Exception { terminate(threadPool); } - @SuppressWarnings({ "unchecked"}) + @SuppressWarnings({ "unchecked" }) public void testCreateApiKeyApi() throws Exception { final String json = "{ \"name\" : \"my-api-key\", \"role_descriptors\": { \"role-a\": {\"cluster\":[\"a-1\", \"a-2\"]} } }"; - final FakeRestRequest restRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY) - .withContent(new BytesArray(json), XContentType.JSON) - .withParams(Collections.singletonMap("refresh", randomFrom("false", "true", "wait_for"))) - .build(); + final FakeRestRequest restRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withContent( + new BytesArray(json), + XContentType.JSON + ).withParams(Collections.singletonMap("refresh", randomFrom("false", "true", "wait_for"))).build(); final SetOnce responseSetOnce = new SetOnce<>(); final RestChannel restChannel = new AbstractRestChannel(restRequest, randomBoolean()) { @@ -77,13 +77,20 @@ public void sendResponse(RestResponse restResponse) { } }; - final CreateApiKeyResponse expected = new CreateApiKeyResponse("my-api-key", UUID.randomUUID().toString(), - new SecureString(randomAlphaOfLength(5)), Instant.now().plus(Duration.ofHours(5))); + final CreateApiKeyResponse expected = new CreateApiKeyResponse( + "my-api-key", + UUID.randomUUID().toString(), + new SecureString(randomAlphaOfLength(5)), + Instant.now().plus(Duration.ofHours(5)) + ); try (NodeClient client = new NodeClient(Settings.EMPTY, threadPool) { @Override - public - void doExecute(ActionType action, Request request, ActionListener listener) { + public void doExecute( + ActionType action, + Request request, + ActionListener listener + ) { CreateApiKeyRequest createApiKeyRequest = (CreateApiKeyRequest) request; @SuppressWarnings("unchecked") RestToXContentListener actionListener = (RestToXContentListener) listener; @@ -99,8 +106,10 @@ void doExecute(ActionType action, Request request, ActionListener param1 = mapBuilder().put("realm_name", "realm-1").put("username","user-x").map(); + final Map param1 = mapBuilder().put("realm_name", "realm-1").put("username", "user-x").map(); final Map param2 = mapBuilder().put("realm_name", "realm-1").map(); final Map param3 = mapBuilder().put("username", "user-x").map(); final Map param4 = mapBuilder().put("id", "api-key-id-1").map(); final Map param5 = mapBuilder().put("name", "api-key-name-1").map(); final Map params = randomFrom(param1, param2, param3, param4, param5); final boolean replyEmptyResponse = rarely(); - final FakeRestRequest restRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY) - .withParams(params).build(); + final FakeRestRequest restRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withParams(params).build(); final SetOnce responseSetOnce = new SetOnce<>(); final RestChannel restChannel = new AbstractRestChannel(restRequest, randomBoolean()) { @@ -86,14 +88,19 @@ public void sendResponse(RestResponse restResponse) { @SuppressWarnings("unchecked") final Map metadata = ApiKeyTests.randomMetadata(); final GetApiKeyResponse getApiKeyResponseExpected = new GetApiKeyResponse( - Collections.singletonList( - new ApiKey("api-key-name-1", "api-key-id-1", creation, expiration, false, "user-x", "realm-1", metadata))); + Collections.singletonList( + new ApiKey("api-key-name-1", "api-key-id-1", creation, expiration, false, "user-x", "realm-1", metadata) + ) + ); try (NodeClient client = new NodeClient(Settings.EMPTY, threadPool) { @SuppressWarnings("unchecked") @Override - public - void doExecute(ActionType action, Request request, ActionListener listener) { + public void doExecute( + ActionType action, + Request request, + ActionListener listener + ) { GetApiKeyRequest getApiKeyRequest = (GetApiKeyRequest) request; ActionRequestValidationException validationException = getApiKeyRequest.validate(); if (validationException != null) { @@ -101,9 +108,9 @@ void doExecute(ActionType action, Request request, ActionListener action, Request request, ActionListener responseSetOnce = new SetOnce<>(); final RestChannel restChannel = new AbstractRestChannel(restRequest, randomBoolean()) { @@ -157,18 +169,37 @@ public void sendResponse(RestResponse restResponse) { final Instant creation = Instant.now(); final Instant expiration = randomFrom(Arrays.asList(null, Instant.now().plus(10, ChronoUnit.DAYS))); - final ApiKey apiKey1 = new ApiKey("api-key-name-1", "api-key-id-1", creation, expiration, false, - "user-x", "realm-1", ApiKeyTests.randomMetadata()); - final ApiKey apiKey2 = new ApiKey("api-key-name-2", "api-key-id-2", creation, expiration, false, - "user-y", "realm-1", ApiKeyTests.randomMetadata()); + final ApiKey apiKey1 = new ApiKey( + "api-key-name-1", + "api-key-id-1", + creation, + expiration, + false, + "user-x", + "realm-1", + ApiKeyTests.randomMetadata() + ); + final ApiKey apiKey2 = new ApiKey( + "api-key-name-2", + "api-key-id-2", + creation, + expiration, + false, + "user-y", + "realm-1", + ApiKeyTests.randomMetadata() + ); final GetApiKeyResponse getApiKeyResponseExpectedWhenOwnerFlagIsTrue = new GetApiKeyResponse(Collections.singletonList(apiKey1)); final GetApiKeyResponse getApiKeyResponseExpectedWhenOwnerFlagIsFalse = new GetApiKeyResponse(List.of(apiKey1, apiKey2)); try (NodeClient client = new NodeClient(Settings.EMPTY, threadPool) { @SuppressWarnings("unchecked") @Override - public - void doExecute(ActionType action, Request request, ActionListener listener) { + public void doExecute( + ActionType action, + Request request, + ActionListener listener + ) { GetApiKeyRequest getApiKeyRequest = (GetApiKeyRequest) request; ActionRequestValidationException validationException = getApiKeyRequest.validate(); if (validationException != null) { @@ -190,16 +221,15 @@ void doExecute(ActionType action, Request request, ActionListener responseSetOnce = new SetOnce<>(); final RestChannel restChannel = new AbstractRestChannel(restRequest, randomBoolean()) { @@ -81,13 +83,19 @@ public void sendResponse(RestResponse restResponse) { }; final InvalidateApiKeyResponse invalidateApiKeyResponseExpected = new InvalidateApiKeyResponse( - Collections.singletonList("api-key-id-1"), Collections.emptyList(), null); + Collections.singletonList("api-key-id-1"), + Collections.emptyList(), + null + ); try (NodeClient client = new NodeClient(Settings.EMPTY, threadPool) { @Override @SuppressWarnings("unchecked") - public - void doExecute(ActionType action, Request request, ActionListener listener) { + public void doExecute( + ActionType action, + Request request, + ActionListener listener + ) { InvalidateApiKeyRequest invalidateApiKeyRequest = (InvalidateApiKeyRequest) request; ActionRequestValidationException validationException = invalidateApiKeyRequest.validate(); if (validationException != null) { @@ -95,10 +103,10 @@ void doExecute(ActionType action, Request request, ActionListener action, Request request, ActionListener responseSetOnce = new SetOnce<>(); final RestChannel restChannel = new AbstractRestChannel(restRequest, randomBoolean()) { @@ -142,15 +155,24 @@ public void sendResponse(RestResponse restResponse) { }; final InvalidateApiKeyResponse invalidateApiKeyResponseExpectedWhenOwnerFlagIsTrue = new InvalidateApiKeyResponse( - List.of("api-key-id-1"), Collections.emptyList(), null); + List.of("api-key-id-1"), + Collections.emptyList(), + null + ); final InvalidateApiKeyResponse invalidateApiKeyResponseExpectedWhenOwnerFlagIsFalse = new InvalidateApiKeyResponse( - List.of("api-key-id-1", "api-key-id-2"), Collections.emptyList(), null); + List.of("api-key-id-1", "api-key-id-2"), + Collections.emptyList(), + null + ); try (NodeClient client = new NodeClient(Settings.EMPTY, threadPool) { @SuppressWarnings("unchecked") @Override - public - void doExecute(ActionType action, Request request, ActionListener listener) { + public void doExecute( + ActionType action, + Request request, + ActionListener listener + ) { InvalidateApiKeyRequest invalidateApiKeyRequest = (InvalidateApiKeyRequest) request; ActionRequestValidationException validationException = invalidateApiKeyRequest.validate(); if (validationException != null) { @@ -172,16 +194,15 @@ void doExecute(ActionType action, Request request, ActionListener responseSetOnce = new SetOnce<>(); final RestChannel restChannel = new AbstractRestChannel(restRequest, randomBoolean()) { @@ -88,8 +92,11 @@ public void sendResponse(RestResponse restResponse) { try (NodeClient client = new NodeClient(Settings.EMPTY, threadPool) { @SuppressWarnings("unchecked") @Override - public - void doExecute(ActionType action, Request request, ActionListener listener) { + public void doExecute( + ActionType action, + Request request, + ActionListener listener + ) { QueryApiKeyRequest queryApiKeyRequest = (QueryApiKeyRequest) request; final QueryBuilder queryBuilder = queryApiKeyRequest.getQueryBuilder(); assertNotNull(queryBuilder); @@ -116,14 +123,14 @@ void doExecute(ActionType action, Request request, ActionListener responseSetOnce = new SetOnce<>(); final RestChannel restChannel = new AbstractRestChannel(restRequest, randomBoolean()) { @@ -136,8 +143,11 @@ public void sendResponse(RestResponse restResponse) { try (NodeClient client = new NodeClient(Settings.EMPTY, threadPool) { @SuppressWarnings("unchecked") @Override - public - void doExecute(ActionType action, Request request, ActionListener listener) { + public void doExecute( + ActionType action, + Request request, + ActionListener listener + ) { QueryApiKeyRequest queryApiKeyRequest = (QueryApiKeyRequest) request; final QueryBuilder queryBuilder = queryApiKeyRequest.getQueryBuilder(); assertNotNull(queryBuilder); @@ -150,13 +160,15 @@ void doExecute(ActionType action, Request request, ActionListener responseSetOnce = new SetOnce<>(); RestChannel restChannel = new AbstractRestChannel(restRequest, randomBoolean()) { @Override @@ -55,8 +54,8 @@ public void sendResponse(RestResponse restResponse) { RestResponse response = responseSetOnce.get(); assertNotNull(response); - Map map = XContentHelper.convertToMap(response.content(), false, - XContentType.fromMediaType(response.contentType())).v2(); + Map map = XContentHelper.convertToMap(response.content(), false, XContentType.fromMediaType(response.contentType())) + .v2(); assertThat(map, hasEntry("error", "unsupported_grant_type")); assertThat(map, hasEntry("error_description", ve.getMessage())); assertEquals(2, map.size()); @@ -73,18 +72,25 @@ public void sendResponse(RestResponse restResponse) { } }; CreateTokenResponseActionListener listener = new CreateTokenResponseActionListener(restChannel, restRequest, NoOpLogger.INSTANCE); - CreateTokenResponse createTokenResponse = - new CreateTokenResponse(randomAlphaOfLengthBetween(1, 256), TimeValue.timeValueHours(1L), null, randomAlphaOfLength(4), - randomAlphaOfLength(5), new Authentication(new User("joe", new String[]{"custom_superuser"}, - new User("bar", "not_superuser")), new Authentication.RealmRef("test", "test", "node"), - new Authentication.RealmRef("test", "test", "node"))); + CreateTokenResponse createTokenResponse = new CreateTokenResponse( + randomAlphaOfLengthBetween(1, 256), + TimeValue.timeValueHours(1L), + null, + randomAlphaOfLength(4), + randomAlphaOfLength(5), + new Authentication( + new User("joe", new String[] { "custom_superuser" }, new User("bar", "not_superuser")), + new Authentication.RealmRef("test", "test", "node"), + new Authentication.RealmRef("test", "test", "node") + ) + ); listener.onResponse(createTokenResponse); RestResponse response = responseSetOnce.get(); assertNotNull(response); - Map map = XContentHelper.convertToMap(response.content(), false, - XContentType.fromMediaType(response.contentType())).v2(); + Map map = XContentHelper.convertToMap(response.content(), false, XContentType.fromMediaType(response.contentType())) + .v2(); assertEquals(RestStatus.OK, response.status()); assertThat(map, hasEntry("type", "Bearer")); assertThat(map, hasEntry("access_token", createTokenResponse.getTokenString())); @@ -117,8 +123,8 @@ public void sendResponse(RestResponse restResponse) { RestResponse response = responseSetOnce.get(); assertNotNull(response); - Map map = XContentHelper.convertToMap(response.content(), false, - XContentType.fromMediaType(response.contentType())).v2(); + Map map = XContentHelper.convertToMap(response.content(), false, XContentType.fromMediaType(response.contentType())) + .v2(); assertThat(map, hasEntry("error", RestGetTokenAction.TokenRequestError._UNAUTHORIZED.name().toLowerCase(Locale.ROOT))); if (addBase64EncodedToken) { assertThat(map, hasEntry("error_description", "FAIL")); @@ -130,14 +136,18 @@ public void sendResponse(RestResponse restResponse) { } public void testParser() throws Exception { - final String request = "{" + - "\"grant_type\": \"password\"," + - "\"username\": \"user1\"," + - "\"password\": \"" + SecuritySettingsSourceField.TEST_PASSWORD + "\"," + - "\"scope\": \"FULL\"" + - "}"; - try (XContentParser parser = XContentType.JSON.xContent() - .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, request)) { + final String request = "{" + + "\"grant_type\": \"password\"," + + "\"username\": \"user1\"," + + "\"password\": \"" + + SecuritySettingsSourceField.TEST_PASSWORD + + "\"," + + "\"scope\": \"FULL\"" + + "}"; + try ( + XContentParser parser = XContentType.JSON.xContent() + .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, request) + ) { CreateTokenRequest createTokenRequest = RestGetTokenAction.PARSER.parse(parser, null); assertEquals("password", createTokenRequest.getGrantType()); assertEquals("user1", createTokenRequest.getUsername()); @@ -148,13 +158,17 @@ public void testParser() throws Exception { public void testParserRefreshRequest() throws Exception { final String token = randomAlphaOfLengthBetween(4, 32); - final String request = "{" + - "\"grant_type\": \"refresh_token\"," + - "\"refresh_token\": \"" + token + "\"," + - "\"scope\": \"FULL\"" + - "}"; - try (XContentParser parser = XContentType.JSON.xContent() - .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, request)) { + final String request = "{" + + "\"grant_type\": \"refresh_token\"," + + "\"refresh_token\": \"" + + token + + "\"," + + "\"scope\": \"FULL\"" + + "}"; + try ( + XContentParser parser = XContentType.JSON.xContent() + .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, request) + ) { CreateTokenRequest createTokenRequest = RestGetTokenAction.PARSER.parse(parser, null); assertEquals("refresh_token", createTokenRequest.getGrantType()); assertEquals(token, createTokenRequest.getRefreshToken()); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/oauth2/RestInvalidateTokenActionTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/oauth2/RestInvalidateTokenActionTests.java index 4047e317b856d..e7f14118b39ff 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/oauth2/RestInvalidateTokenActionTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/oauth2/RestInvalidateTokenActionTests.java @@ -6,11 +6,11 @@ */ package org.elasticsearch.xpack.security.rest.action.oauth2; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.security.action.token.InvalidateTokenRequest; import static org.hamcrest.Matchers.containsString; @@ -18,12 +18,11 @@ public class RestInvalidateTokenActionTests extends ESTestCase { public void testParserForUserAndRealm() throws Exception { - final String request = "{" + - "\"username\": \"user1\"," + - "\"realm_name\": \"realm1\"" + - "}"; - try (XContentParser parser = XContentType.JSON.xContent() - .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, request)) { + final String request = "{" + "\"username\": \"user1\"," + "\"realm_name\": \"realm1\"" + "}"; + try ( + XContentParser parser = XContentType.JSON.xContent() + .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, request) + ) { InvalidateTokenRequest invalidateTokenRequest = RestInvalidateTokenAction.PARSER.parse(parser, null); assertEquals("user1", invalidateTokenRequest.getUserName()); assertEquals("realm1", invalidateTokenRequest.getRealmName()); @@ -33,11 +32,11 @@ public void testParserForUserAndRealm() throws Exception { } public void testParserForToken() throws Exception { - final String request = "{" + - "\"refresh_token\": \"refresh_token_string\"" + - "}"; - try (XContentParser parser = XContentType.JSON.xContent() - .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, request)) { + final String request = "{" + "\"refresh_token\": \"refresh_token_string\"" + "}"; + try ( + XContentParser parser = XContentType.JSON.xContent() + .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, request) + ) { InvalidateTokenRequest invalidateTokenRequest = RestInvalidateTokenAction.PARSER.parse(parser, null); assertEquals("refresh_token_string", invalidateTokenRequest.getTokenString()); assertEquals("refresh_token", invalidateTokenRequest.getTokenType().getValue()); @@ -47,14 +46,15 @@ public void testParserForToken() throws Exception { } public void testParserForIncorrectInput() throws Exception { - final String request = "{" + - "\"refresh_token\": \"refresh_token_string\"," + - "\"token\": \"access_token_string\"" + - "}"; - try (XContentParser parser = XContentType.JSON.xContent() - .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, request)) { - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> RestInvalidateTokenAction.PARSER.parse(parser, - null)); + final String request = "{" + "\"refresh_token\": \"refresh_token_string\"," + "\"token\": \"access_token_string\"" + "}"; + try ( + XContentParser parser = XContentType.JSON.xContent() + .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, request) + ) { + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> RestInvalidateTokenAction.PARSER.parse(parser, null) + ); assertThat(e.getCause().getMessage(), containsString("only one of [token, refresh_token] may be sent per request")); } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/saml/SamlBaseRestHandlerTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/saml/SamlBaseRestHandlerTests.java index d622e047acb24..3843956b4694a 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/saml/SamlBaseRestHandlerTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/saml/SamlBaseRestHandlerTests.java @@ -27,14 +27,16 @@ public class SamlBaseRestHandlerTests extends ESTestCase { public void testSamlAvailableOnTrialAndPlatinum() { - final SamlBaseRestHandler handler = buildHandler(randomFrom( - License.OperationMode.TRIAL, License.OperationMode.PLATINUM, License.OperationMode.ENTERPRISE)); + final SamlBaseRestHandler handler = buildHandler( + randomFrom(License.OperationMode.TRIAL, License.OperationMode.PLATINUM, License.OperationMode.ENTERPRISE) + ); assertThat(handler.checkFeatureAvailable(new FakeRestRequest()), Matchers.nullValue()); } public void testSamlNotAvailableOnBasicStandardOrGold() { - final SamlBaseRestHandler handler = buildHandler(randomFrom(License.OperationMode.BASIC, License.OperationMode.STANDARD, - License.OperationMode.GOLD)); + final SamlBaseRestHandler handler = buildHandler( + randomFrom(License.OperationMode.BASIC, License.OperationMode.STANDARD, License.OperationMode.GOLD) + ); Exception e = handler.checkFeatureAvailable(new FakeRestRequest()); assertThat(e, instanceOf(ElasticsearchException.class)); ElasticsearchException elasticsearchException = (ElasticsearchException) e; @@ -42,9 +44,7 @@ public void testSamlNotAvailableOnBasicStandardOrGold() { } private SamlBaseRestHandler buildHandler(License.OperationMode licenseMode) { - final Settings settings = Settings.builder() - .put(XPackSettings.SECURITY_ENABLED.getKey(), true) - .build(); + final Settings settings = Settings.builder().put(XPackSettings.SECURITY_ENABLED.getKey(), true).build(); final TestUtils.UpdatableLicenseState licenseState = new TestUtils.UpdatableLicenseState(settings); licenseState.update(licenseMode, true, null); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/service/RestClearServiceAccountTokenStoreCacheActionTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/service/RestClearServiceAccountTokenStoreCacheActionTests.java index 5b05d7b004607..3cfaf0af49bb9 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/service/RestClearServiceAccountTokenStoreCacheActionTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/service/RestClearServiceAccountTokenStoreCacheActionTests.java @@ -11,11 +11,11 @@ import org.elasticsearch.cluster.ClusterName; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.test.rest.FakeRestRequest; import org.elasticsearch.test.rest.RestActionTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.xpack.core.security.action.ClearSecurityCacheRequest; import org.elasticsearch.xpack.core.security.action.ClearSecurityCacheResponse; @@ -60,51 +60,70 @@ public void testInnerPrepareRequestWithEmptyTokenName() { final String namespace = randomAlphaOfLengthBetween(3, 8); final String service = randomAlphaOfLengthBetween(3, 8); final String name = randomFrom("", "*", "_all"); - final FakeRestRequest restRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY) - .withMethod(RestRequest.Method.POST) + final FakeRestRequest restRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withMethod(RestRequest.Method.POST) .withPath("/_security/service/" + namespace + "/" + service + "/credential/token/" + name + "/_clear_cache") .build(); dispatchRequest(restRequest); final ClearSecurityCacheRequest clearSecurityCacheRequest = requestHolder.get(); - assertThat(clearSecurityCacheRequest.keys(), equalTo(new String[]{ namespace + "/" + service + "/"})); + assertThat(clearSecurityCacheRequest.keys(), equalTo(new String[] { namespace + "/" + service + "/" })); } public void testInnerPrepareRequestWithValidTokenNames() { final String namespace = randomAlphaOfLengthBetween(3, 8); final String service = randomAlphaOfLengthBetween(3, 8); final String[] names = randomArray(1, 3, String[]::new, ValidationTests::randomTokenName); - final FakeRestRequest restRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY) - .withMethod(RestRequest.Method.POST) - .withPath("/_security/service/" + namespace + "/" + service + "/credential/token/" - + Strings.arrayToCommaDelimitedString(names) + "/_clear_cache") + final FakeRestRequest restRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withMethod(RestRequest.Method.POST) + .withPath( + "/_security/service/" + + namespace + + "/" + + service + + "/credential/token/" + + Strings.arrayToCommaDelimitedString(names) + + "/_clear_cache" + ) .build(); dispatchRequest(restRequest); final ClearSecurityCacheRequest clearSecurityCacheRequest = requestHolder.get(); - assertThat(Set.of(clearSecurityCacheRequest.keys()), - equalTo(Arrays.stream(names).map(n -> namespace + "/" + service + "/" + n).collect(Collectors.toUnmodifiableSet()))); + assertThat( + Set.of(clearSecurityCacheRequest.keys()), + equalTo(Arrays.stream(names).map(n -> namespace + "/" + service + "/" + n).collect(Collectors.toUnmodifiableSet())) + ); } public void testInnerPrepareRequestWillThrowErrorOnInvalidTokenNames() { - final RestClearServiceAccountTokenStoreCacheAction restAction = - new RestClearServiceAccountTokenStoreCacheAction(Settings.EMPTY, mock(XPackLicenseState.class)); - final String[] names = randomArray(2, 4, String[]::new, - () -> randomValueOtherThanMany(n -> n.contains(","), ValidationTests::randomInvalidTokenName)); + final RestClearServiceAccountTokenStoreCacheAction restAction = new RestClearServiceAccountTokenStoreCacheAction( + Settings.EMPTY, + mock(XPackLicenseState.class) + ); + final String[] names = randomArray( + 2, + 4, + String[]::new, + () -> randomValueOtherThanMany(n -> n.contains(","), ValidationTests::randomInvalidTokenName) + ); // Add a valid name in the mix, we should still have one invalid name names[names.length - 1] = ValidationTests.randomTokenName(); - final FakeRestRequest fakeRestRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY) - .withParams(Map.of( - "namespace", randomAlphaOfLengthBetween(3, 8), - "service", randomAlphaOfLengthBetween(3, 8), - "name", Strings.arrayToCommaDelimitedString(names))) - .build(); - - final IllegalArgumentException e = - expectThrows(IllegalArgumentException.class, () -> restAction.innerPrepareRequest(fakeRestRequest, mock(NodeClient.class))); + final FakeRestRequest fakeRestRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withParams( + Map.of( + "namespace", + randomAlphaOfLengthBetween(3, 8), + "service", + randomAlphaOfLengthBetween(3, 8), + "name", + Strings.arrayToCommaDelimitedString(names) + ) + ).build(); + + final IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> restAction.innerPrepareRequest(fakeRestRequest, mock(NodeClient.class)) + ); assertThat(e.getMessage(), containsString(Validation.INVALID_SERVICE_ACCOUNT_TOKEN_NAME_MESSAGE)); assertThat(e.getMessage(), containsString("invalid service token name [" + names[0] + "]")); } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/user/RestGetUserPrivilegesActionTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/user/RestGetUserPrivilegesActionTests.java index 9981176f98a45..e8671c5de1ce4 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/user/RestGetUserPrivilegesActionTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/user/RestGetUserPrivilegesActionTests.java @@ -12,7 +12,6 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.set.Sets; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.license.License; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.RestStatus; @@ -20,6 +19,7 @@ import org.elasticsearch.test.client.NoOpNodeClient; import org.elasticsearch.test.rest.FakeRestChannel; import org.elasticsearch.test.rest.FakeRestRequest; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.xpack.core.security.SecurityContext; import org.elasticsearch.xpack.core.security.action.user.GetUserPrivilegesResponse; @@ -46,8 +46,11 @@ public void testSecurityDisabled() throws Exception { final Settings securityDisabledSettings = Settings.builder().put(XPackSettings.SECURITY_ENABLED.getKey(), false).build(); final XPackLicenseState licenseState = mock(XPackLicenseState.class); when(licenseState.getOperationMode()).thenReturn(License.OperationMode.BASIC); - final RestGetUserPrivilegesAction action = - new RestGetUserPrivilegesAction(securityDisabledSettings, mock(SecurityContext.class), licenseState); + final RestGetUserPrivilegesAction action = new RestGetUserPrivilegesAction( + securityDisabledSettings, + mock(SecurityContext.class), + licenseState + ); final FakeRestRequest request = new FakeRestRequest(); final FakeRestChannel channel = new FakeRestChannel(request, true, 1); try (NodeClient nodeClient = new NoOpNodeClient(this.getTestName())) { @@ -55,31 +58,46 @@ public void testSecurityDisabled() throws Exception { } assertThat(channel.capturedResponse(), notNullValue()); assertThat(channel.capturedResponse().status(), equalTo(RestStatus.INTERNAL_SERVER_ERROR)); - assertThat(channel.capturedResponse().content().utf8ToString(), - containsString("Security is not enabled but a security rest handler is registered")); + assertThat( + channel.capturedResponse().content().utf8ToString(), + containsString("Security is not enabled but a security rest handler is registered") + ); } public void testBuildResponse() throws Exception { final RestGetUserPrivilegesAction.RestListener listener = new RestGetUserPrivilegesAction.RestListener(null); final Set cluster = new LinkedHashSet<>(Arrays.asList("monitor", "manage_ml", "manage_watcher")); final Set conditionalCluster = Collections.singleton( - new ConfigurableClusterPrivileges.ManageApplicationPrivileges(new LinkedHashSet<>(Arrays.asList("app01", "app02")))); - final Set index = new LinkedHashSet<>(Arrays.asList( - new GetUserPrivilegesResponse.Indices(Arrays.asList("index-1", "index-2", "index-3-*"), Arrays.asList("read", "write"), - new LinkedHashSet<>(Arrays.asList( - new FieldPermissionsDefinition.FieldGrantExcludeGroup(new String[]{"public.*"}, new String[0]), - new FieldPermissionsDefinition.FieldGrantExcludeGroup(new String[]{"*"}, new String[]{"private.*"}) - )), - new LinkedHashSet<>(Arrays.asList( - new BytesArray("{ \"term\": { \"access\": \"public\" } }"), - new BytesArray("{ \"term\": { \"access\": \"standard\" } }") - )), - false - ), - new GetUserPrivilegesResponse.Indices(Arrays.asList("index-4"), Collections.singleton("all"), - Collections.emptySet(), Collections.emptySet(), true + new ConfigurableClusterPrivileges.ManageApplicationPrivileges(new LinkedHashSet<>(Arrays.asList("app01", "app02"))) + ); + final Set index = new LinkedHashSet<>( + Arrays.asList( + new GetUserPrivilegesResponse.Indices( + Arrays.asList("index-1", "index-2", "index-3-*"), + Arrays.asList("read", "write"), + new LinkedHashSet<>( + Arrays.asList( + new FieldPermissionsDefinition.FieldGrantExcludeGroup(new String[] { "public.*" }, new String[0]), + new FieldPermissionsDefinition.FieldGrantExcludeGroup(new String[] { "*" }, new String[] { "private.*" }) + ) + ), + new LinkedHashSet<>( + Arrays.asList( + new BytesArray("{ \"term\": { \"access\": \"public\" } }"), + new BytesArray("{ \"term\": { \"access\": \"standard\" } }") + ) + ), + false + ), + new GetUserPrivilegesResponse.Indices( + Arrays.asList("index-4"), + Collections.singleton("all"), + Collections.emptySet(), + Collections.emptySet(), + true + ) ) - )); + ); final Set application = Sets.newHashSet( ApplicationResourcePrivileges.builder().application("app01").privileges("read", "write").resources("*").build(), ApplicationResourcePrivileges.builder().application("app01").privileges("admin").resources("department/1").build(), @@ -91,34 +109,38 @@ public void testBuildResponse() throws Exception { listener.buildResponse(response, builder); String json = Strings.toString(builder); - assertThat(json, equalTo("{" + - "\"cluster\":[\"monitor\",\"manage_ml\",\"manage_watcher\"]," + - "\"global\":[" + - "{\"application\":{\"manage\":{\"applications\":[\"app01\",\"app02\"]}}}" + - "]," + - "\"indices\":[" + - "{\"names\":[\"index-1\",\"index-2\",\"index-3-*\"]," + - "\"privileges\":[\"read\",\"write\"]," + - "\"field_security\":[" + - "{\"grant\":[\"*\"],\"except\":[\"private.*\"]}," + - "{\"grant\":[\"public.*\"]}" + - "]," + - "\"query\":[" + - "\"{ \\\"term\\\": { \\\"access\\\": \\\"public\\\" } }\"," + - "\"{ \\\"term\\\": { \\\"access\\\": \\\"standard\\\" } }\"" + - "]," + - "\"allow_restricted_indices\":false" + - "}," + - "{\"names\":[\"index-4\"],\"privileges\":[\"all\"],\"allow_restricted_indices\":true}" + - "]," + - "\"applications\":[" + - "{\"application\":\"app01\",\"privileges\":[\"read\",\"write\"],\"resources\":[\"*\"]}," + - "{\"application\":\"app01\",\"privileges\":[\"admin\"],\"resources\":[\"department/1\"]}," + - "{\"application\":\"app02\",\"privileges\":[\"all\"],\"resources\":[\"tenant/42\",\"tenant/99\"]}" + - "]," + - "\"run_as\":[\"app-user-*\",\"backup-user\"]" + - "}" - )); + assertThat( + json, + equalTo( + "{" + + "\"cluster\":[\"monitor\",\"manage_ml\",\"manage_watcher\"]," + + "\"global\":[" + + "{\"application\":{\"manage\":{\"applications\":[\"app01\",\"app02\"]}}}" + + "]," + + "\"indices\":[" + + "{\"names\":[\"index-1\",\"index-2\",\"index-3-*\"]," + + "\"privileges\":[\"read\",\"write\"]," + + "\"field_security\":[" + + "{\"grant\":[\"*\"],\"except\":[\"private.*\"]}," + + "{\"grant\":[\"public.*\"]}" + + "]," + + "\"query\":[" + + "\"{ \\\"term\\\": { \\\"access\\\": \\\"public\\\" } }\"," + + "\"{ \\\"term\\\": { \\\"access\\\": \\\"standard\\\" } }\"" + + "]," + + "\"allow_restricted_indices\":false" + + "}," + + "{\"names\":[\"index-4\"],\"privileges\":[\"all\"],\"allow_restricted_indices\":true}" + + "]," + + "\"applications\":[" + + "{\"application\":\"app01\",\"privileges\":[\"read\",\"write\"],\"resources\":[\"*\"]}," + + "{\"application\":\"app01\",\"privileges\":[\"admin\"],\"resources\":[\"department/1\"]}," + + "{\"application\":\"app02\",\"privileges\":[\"all\"],\"resources\":[\"tenant/42\",\"tenant/99\"]}" + + "]," + + "\"run_as\":[\"app-user-*\",\"backup-user\"]" + + "}" + ) + ); } } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/user/RestHasPrivilegesActionTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/user/RestHasPrivilegesActionTests.java index 95fc553e51d96..eabcf366ec754 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/user/RestHasPrivilegesActionTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/user/RestHasPrivilegesActionTests.java @@ -10,9 +10,6 @@ import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.license.License; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.RestChannel; @@ -22,6 +19,9 @@ import org.elasticsearch.test.client.NoOpNodeClient; import org.elasticsearch.test.rest.FakeRestChannel; import org.elasticsearch.test.rest.FakeRestRequest; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.xpack.core.security.SecurityContext; @@ -42,17 +42,19 @@ public class RestHasPrivilegesActionTests extends ESTestCase { */ public void testBodyConsumed() throws Exception { final XPackLicenseState licenseState = mock(XPackLicenseState.class); - final RestHasPrivilegesAction action = - new RestHasPrivilegesAction(Settings.EMPTY, mock(SecurityContext.class), licenseState); - try (XContentBuilder bodyBuilder = JsonXContent.contentBuilder().startObject().endObject(); - NodeClient client = new NoOpNodeClient(this.getTestName())) { - final RestRequest request = new FakeRestRequest.Builder(xContentRegistry()) - .withPath("/_security/user/_has_privileges/") + final RestHasPrivilegesAction action = new RestHasPrivilegesAction(Settings.EMPTY, mock(SecurityContext.class), licenseState); + try ( + XContentBuilder bodyBuilder = JsonXContent.contentBuilder().startObject().endObject(); + NodeClient client = new NoOpNodeClient(this.getTestName()) + ) { + final RestRequest request = new FakeRestRequest.Builder(xContentRegistry()).withPath("/_security/user/_has_privileges/") .withContent(new BytesArray(bodyBuilder.toString()), XContentType.JSON) .build(); final RestChannel channel = new FakeRestChannel(request, true, 1); - ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, () -> - action.handleRequest(request, channel, client)); + ElasticsearchSecurityException e = expectThrows( + ElasticsearchSecurityException.class, + () -> action.handleRequest(request, channel, client) + ); assertThat(e.getMessage(), equalTo("there is no authenticated user")); } } @@ -61,12 +63,16 @@ public void testSecurityDisabled() throws Exception { final XPackLicenseState licenseState = mock(XPackLicenseState.class); final Settings securityDisabledSettings = Settings.builder().put(XPackSettings.SECURITY_ENABLED.getKey(), false).build(); when(licenseState.getOperationMode()).thenReturn(License.OperationMode.BASIC); - final RestHasPrivilegesAction action = - new RestHasPrivilegesAction(securityDisabledSettings, mock(SecurityContext.class), licenseState); - try (XContentBuilder bodyBuilder = JsonXContent.contentBuilder().startObject().endObject(); - NodeClient client = new NoOpNodeClient(this.getTestName())) { - final RestRequest request = new FakeRestRequest.Builder(xContentRegistry()) - .withPath("/_security/user/_has_privileges/") + final RestHasPrivilegesAction action = new RestHasPrivilegesAction( + securityDisabledSettings, + mock(SecurityContext.class), + licenseState + ); + try ( + XContentBuilder bodyBuilder = JsonXContent.contentBuilder().startObject().endObject(); + NodeClient client = new NoOpNodeClient(this.getTestName()) + ) { + final RestRequest request = new FakeRestRequest.Builder(xContentRegistry()).withPath("/_security/user/_has_privileges/") .withContent(new BytesArray(bodyBuilder.toString()), XContentType.JSON) .build(); final FakeRestChannel channel = new FakeRestChannel(request, true, 1); @@ -75,7 +81,8 @@ public void testSecurityDisabled() throws Exception { assertThat(channel.capturedResponse().status(), equalTo(RestStatus.INTERNAL_SERVER_ERROR)); assertThat( channel.capturedResponse().content().utf8ToString(), - containsString("Security is not enabled but a security rest handler is registered")); + containsString("Security is not enabled but a security rest handler is registered") + ); } } } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/support/ApiKeyBoolQueryBuilderTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/support/ApiKeyBoolQueryBuilderTests.java index cf46a5cb2a93e..4c1718e2d0f5c 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/support/ApiKeyBoolQueryBuilderTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/support/ApiKeyBoolQueryBuilderTests.java @@ -73,8 +73,9 @@ public void testBuildFromBoolQuery() { bq1.should(QueryBuilders.wildcardQuery("name", "*-east-*")); } if (randomBoolean()) { - bq1.filter(QueryBuilders.termsQuery("name", - randomArray(3, 8, String[]::new, () -> "prod-" + randomInt() + "-east-" + randomInt()))); + bq1.filter( + QueryBuilders.termsQuery("name", randomArray(3, 8, String[]::new, () -> "prod-" + randomInt() + "-east-" + randomInt())) + ); } if (randomBoolean()) { bq1.mustNot(QueryBuilders.idsQuery().addIds(randomArray(1, 3, String[]::new, () -> randomAlphaOfLength(22)))); @@ -120,7 +121,6 @@ public void testFieldNameTranslation() { assertCommonFilterQueries(apiKeyQb3, authentication); assertThat(apiKeyQb3.must().get(0), equalTo(QueryBuilders.wildcardQuery("creator.realm", q3.value()))); - // creation_time final TermQueryBuilder q4 = QueryBuilders.termQuery("creation", randomLongBetween(0, Long.MAX_VALUE)); final ApiKeyBoolQueryBuilder apiKeyQb4 = ApiKeyBoolQueryBuilder.build(q4, authentication); @@ -137,23 +137,30 @@ public void testFieldNameTranslation() { public void testAllowListOfFieldNames() { final Authentication authentication = randomBoolean() ? AuthenticationTests.randomAuthentication(null, null) : null; - final String randomFieldName = randomValueOtherThanMany(s -> FIELD_NAME_TRANSLATORS.stream().anyMatch(t -> t.supports(s)), - () -> randomAlphaOfLengthBetween(3, 20)); + final String randomFieldName = randomValueOtherThanMany( + s -> FIELD_NAME_TRANSLATORS.stream().anyMatch(t -> t.supports(s)), + () -> randomAlphaOfLengthBetween(3, 20) + ); final String fieldName = randomFrom( - randomFieldName, - "api_key_hash", - "api_key_invalidated", - "doc_type", - "role_descriptors", - "limited_by_role_descriptors", - "version", - "creator", "creator.metadata"); + randomFieldName, + "api_key_hash", + "api_key_invalidated", + "doc_type", + "role_descriptors", + "limited_by_role_descriptors", + "version", + "creator", + "creator.metadata" + ); final QueryBuilder q1 = randomValueOtherThanMany( q -> q.getClass() == IdsQueryBuilder.class || q.getClass() == MatchAllQueryBuilder.class, - () -> randomSimpleQuery(fieldName)); - final IllegalArgumentException e1 = - expectThrows(IllegalArgumentException.class, () -> ApiKeyBoolQueryBuilder.build(q1, authentication)); + () -> randomSimpleQuery(fieldName) + ); + final IllegalArgumentException e1 = expectThrows( + IllegalArgumentException.class, + () -> ApiKeyBoolQueryBuilder.build(q1, authentication) + ); assertThat(e1.getMessage(), containsString("Field [" + fieldName + "] is not allowed for API Key query")); } @@ -161,16 +168,20 @@ public void testAllowListOfFieldNames() { public void testTermsLookupIsNotAllowed() { final Authentication authentication = randomBoolean() ? AuthenticationTests.randomAuthentication(null, null) : null; final TermsQueryBuilder q1 = QueryBuilders.termsLookupQuery("name", new TermsLookup("lookup", "1", "names")); - final IllegalArgumentException e1 = - expectThrows(IllegalArgumentException.class, () -> ApiKeyBoolQueryBuilder.build(q1, authentication)); + final IllegalArgumentException e1 = expectThrows( + IllegalArgumentException.class, + () -> ApiKeyBoolQueryBuilder.build(q1, authentication) + ); assertThat(e1.getMessage(), containsString("terms query with terms lookup is not supported for API Key query")); } public void testRangeQueryWithRelationIsNotAllowed() { final Authentication authentication = randomBoolean() ? AuthenticationTests.randomAuthentication(null, null) : null; final RangeQueryBuilder q1 = QueryBuilders.rangeQuery("creation").relation("contains"); - final IllegalArgumentException e1 = - expectThrows(IllegalArgumentException.class, () -> ApiKeyBoolQueryBuilder.build(q1, authentication)); + final IllegalArgumentException e1 = expectThrows( + IllegalArgumentException.class, + () -> ApiKeyBoolQueryBuilder.build(q1, authentication) + ); assertThat(e1.getMessage(), containsString("range query with relation is not supported for API Key query")); } @@ -186,9 +197,11 @@ public void testDisallowedQueryTypes() { QueryBuilders.simpleQueryStringQuery(randomAlphaOfLength(5)), QueryBuilders.combinedFieldsQuery(randomAlphaOfLength(5)), QueryBuilders.disMaxQuery(), - QueryBuilders.distanceFeatureQuery(randomAlphaOfLength(5), + QueryBuilders.distanceFeatureQuery( + randomAlphaOfLength(5), mock(DistanceFeatureQueryBuilder.Origin.class), - randomAlphaOfLength(5)), + randomAlphaOfLength(5) + ), QueryBuilders.fieldMaskingSpanQuery(mock(SpanQueryBuilder.class), randomAlphaOfLength(5)), QueryBuilders.functionScoreQuery(mock(QueryBuilder.class)), QueryBuilders.fuzzyQuery(randomAlphaOfLength(5), randomAlphaOfLength(5)), @@ -214,14 +227,18 @@ public void testDisallowedQueryTypes() { QueryBuilders.geoShapeQuery(randomAlphaOfLength(5), randomAlphaOfLength(5)) ); - final IllegalArgumentException e1 = - expectThrows(IllegalArgumentException.class, () -> ApiKeyBoolQueryBuilder.build(q1, authentication)); + final IllegalArgumentException e1 = expectThrows( + IllegalArgumentException.class, + () -> ApiKeyBoolQueryBuilder.build(q1, authentication) + ); assertThat(e1.getMessage(), containsString("Query type [" + q1.getName() + "] is not supported for API Key query")); } public void testWillSetAllowedFields() throws IOException { - final ApiKeyBoolQueryBuilder apiKeyQb1 = ApiKeyBoolQueryBuilder.build(randomSimpleQuery("name"), - randomBoolean() ? AuthenticationTests.randomAuthentication(null, null) : null); + final ApiKeyBoolQueryBuilder apiKeyQb1 = ApiKeyBoolQueryBuilder.build( + randomSimpleQuery("name"), + randomBoolean() ? AuthenticationTests.randomAuthentication(null, null) : null + ); final SearchExecutionContext context1 = mock(SearchExecutionContext.class); doAnswer(invocationOnMock -> { @@ -253,7 +270,8 @@ private void testAllowedIndexFieldName(Predicate predicate) { "creation_time", "expiration_time", "metadata_flattened." + randomAlphaOfLengthBetween(1, 10), - "creator." + randomAlphaOfLengthBetween(1, 10)); + "creator." + randomAlphaOfLengthBetween(1, 10) + ); assertTrue(predicate.test(allowedField)); final String disallowedField = randomBoolean() ? (randomAlphaOfLengthBetween(1, 3) + allowedField) : (allowedField.substring(1)); @@ -270,10 +288,13 @@ private void assertCommonFilterQueries(ApiKeyBoolQueryBuilder qb, Authentication if (authentication == null) { return; } - assertTrue(tqb.stream() - .anyMatch(q -> q.equals(QueryBuilders.termQuery("creator.principal", authentication.getUser().principal())))); - assertTrue(tqb.stream() - .anyMatch(q -> q.equals(QueryBuilders.termQuery("creator.realm", ApiKeyService.getCreatorRealmName(authentication))))); + assertTrue( + tqb.stream().anyMatch(q -> q.equals(QueryBuilders.termQuery("creator.principal", authentication.getUser().principal()))) + ); + assertTrue( + tqb.stream() + .anyMatch(q -> q.equals(QueryBuilders.termQuery("creator.realm", ApiKeyService.getCreatorRealmName(authentication)))) + ); } private QueryBuilder randomSimpleQuery(String name) { diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/support/CacheInvalidatorRegistryTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/support/CacheInvalidatorRegistryTests.java index 7a512b38642c5..d8fc00a2f1560 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/support/CacheInvalidatorRegistryTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/support/CacheInvalidatorRegistryTests.java @@ -38,7 +38,8 @@ public void testRegistryWillNotAllowInvalidatorsWithDuplicatedName() { cacheInvalidatorRegistry.registerCacheInvalidator("service1", mock(CacheInvalidator.class)); final IllegalArgumentException e = expectThrows( IllegalArgumentException.class, - () -> cacheInvalidatorRegistry.registerCacheInvalidator("service1", mock(CacheInvalidator.class))); + () -> cacheInvalidatorRegistry.registerCacheInvalidator("service1", mock(CacheInvalidator.class)) + ); assertThat(e.getMessage(), containsString("already has an entry with name: [service1]")); } @@ -54,8 +55,17 @@ public void testSecurityIndexStateChangeWillInvalidateAllRegisteredInvalidators( final SecurityIndexManager.State previousState = SecurityIndexManager.State.UNRECOVERED_STATE; final SecurityIndexManager.State currentState = new SecurityIndexManager.State( - Instant.now(), true, true, true, Version.CURRENT, - ".security", ClusterHealthStatus.GREEN, IndexMetadata.State.OPEN, null, "my_uuid"); + Instant.now(), + true, + true, + true, + Version.CURRENT, + ".security", + ClusterHealthStatus.GREEN, + IndexMetadata.State.OPEN, + null, + "my_uuid" + ); cacheInvalidatorRegistry.onSecurityIndexStateChange(previousState, currentState); verify(invalidator1).invalidateAll(); @@ -74,9 +84,10 @@ public void testInvalidateByKeyCallsCorrectInvalidatorObject() { verify(invalidator2).invalidate(List.of("k1", "k2")); // Trying to invalidate entries from a non-existing cache will throw error - final IllegalArgumentException e = - expectThrows(IllegalArgumentException.class, - () -> cacheInvalidatorRegistry.invalidateByKey("non-exist", List.of("k1", "k2"))); + final IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> cacheInvalidatorRegistry.invalidateByKey("non-exist", List.of("k1", "k2")) + ); assertThat(e.getMessage(), containsString("No cache named [non-exist] is found")); } @@ -91,9 +102,10 @@ public void testInvalidateCache() { verify(invalidator2, never()).invalidateAll(); // Trying to invalidate entries from a non-existing cache will throw error - final IllegalArgumentException e = - expectThrows(IllegalArgumentException.class, - () -> cacheInvalidatorRegistry.invalidateCache("non-exist")); + final IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> cacheInvalidatorRegistry.invalidateCache("non-exist") + ); assertThat(e.getMessage(), containsString("No cache named [non-exist] is found")); } @@ -103,18 +115,24 @@ public void testRegisterAlias() { final CacheInvalidator invalidator2 = mock(CacheInvalidator.class); cacheInvalidatorRegistry.registerCacheInvalidator("cache2", invalidator2); - final NullPointerException e1 = - expectThrows(NullPointerException.class, () -> cacheInvalidatorRegistry.registerAlias(null, Set.of())); + final NullPointerException e1 = expectThrows( + NullPointerException.class, + () -> cacheInvalidatorRegistry.registerAlias(null, Set.of()) + ); assertThat(e1.getMessage(), containsString("cache alias cannot be null")); - final IllegalArgumentException e2 = - expectThrows(IllegalArgumentException.class, () -> cacheInvalidatorRegistry.registerAlias("alias1", Set.of())); + final IllegalArgumentException e2 = expectThrows( + IllegalArgumentException.class, + () -> cacheInvalidatorRegistry.registerAlias("alias1", Set.of()) + ); assertThat(e2.getMessage(), containsString("cache names cannot be empty for aliasing")); cacheInvalidatorRegistry.registerAlias("alias1", randomFrom(Set.of("cache1"), Set.of("cache1", "cache2"))); - final IllegalArgumentException e3 = - expectThrows(IllegalArgumentException.class, () -> cacheInvalidatorRegistry.registerAlias("alias1", Set.of("cache1"))); + final IllegalArgumentException e3 = expectThrows( + IllegalArgumentException.class, + () -> cacheInvalidatorRegistry.registerAlias("alias1", Set.of("cache1")) + ); assertThat(e3.getMessage(), containsString("cache alias already exists")); // validation should pass @@ -125,8 +143,7 @@ public void testValidateWillThrowForClashingAliasAndCacheNames() { final CacheInvalidator invalidator1 = mock(CacheInvalidator.class); cacheInvalidatorRegistry.registerCacheInvalidator("cache1", invalidator1); cacheInvalidatorRegistry.registerAlias("cache1", Set.of("cache1")); - final IllegalStateException e = - expectThrows(IllegalStateException.class, () -> cacheInvalidatorRegistry.validate()); + final IllegalStateException e = expectThrows(IllegalStateException.class, () -> cacheInvalidatorRegistry.validate()); assertThat(e.getMessage(), containsString("cache alias cannot clash with cache name")); } @@ -134,8 +151,7 @@ public void testValidateWillThrowForNotFoundCacheNames() { final CacheInvalidator invalidator1 = mock(CacheInvalidator.class); cacheInvalidatorRegistry.registerCacheInvalidator("cache1", invalidator1); cacheInvalidatorRegistry.registerAlias("alias1", Set.of("cache1", "cache2")); - final IllegalStateException e = - expectThrows(IllegalStateException.class, () -> cacheInvalidatorRegistry.validate()); + final IllegalStateException e = expectThrows(IllegalStateException.class, () -> cacheInvalidatorRegistry.validate()); assertThat(e.getMessage(), containsString("cache names not found: [cache2]")); } } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/support/FileLineParserTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/support/FileLineParserTests.java index 75932992d503d..c2e5268c9b4b6 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/support/FileLineParserTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/support/FileLineParserTests.java @@ -22,18 +22,24 @@ public class FileLineParserTests extends ESTestCase { public void testParse() throws IOException { Path path = getDataPath("../authc/support/role_mapping.yml"); - final Map lines = new HashMap<>(Map.of( - 7, "security:", - 8, " - \"cn=avengers,ou=marvel,o=superheros\"", - 9, " - \"cn=shield,ou=marvel,o=superheros\"", - 10, "avenger:", - 11, " - \"cn=avengers,ou=marvel,o=superheros\"", - 12, " - \"cn=Horatio Hornblower,ou=people,o=sevenSeas\"" - )); - - FileLineParser.parse(path, (lineNumber, line) -> { - assertThat(lines.remove(lineNumber), equalTo(line)); - }); + final Map lines = new HashMap<>( + Map.of( + 7, + "security:", + 8, + " - \"cn=avengers,ou=marvel,o=superheros\"", + 9, + " - \"cn=shield,ou=marvel,o=superheros\"", + 10, + "avenger:", + 11, + " - \"cn=avengers,ou=marvel,o=superheros\"", + 12, + " - \"cn=Horatio Hornblower,ou=people,o=sevenSeas\"" + ) + ); + + FileLineParser.parse(path, (lineNumber, line) -> { assertThat(lines.remove(lineNumber), equalTo(line)); }); assertThat(lines.isEmpty(), is(true)); } } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/support/FileReloadListenerTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/support/FileReloadListenerTests.java index 5a7c4a99eecec..068a65a7f9b1e 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/support/FileReloadListenerTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/support/FileReloadListenerTests.java @@ -22,8 +22,11 @@ public void testCallback() { final CountDownLatch latch = new CountDownLatch(2); final FileReloadListener fileReloadListener = new FileReloadListener(PathUtils.get("foo", "bar"), latch::countDown); - Consumer consumer = - randomFrom(fileReloadListener::onFileCreated, fileReloadListener::onFileChanged, fileReloadListener::onFileDeleted); + Consumer consumer = randomFrom( + fileReloadListener::onFileCreated, + fileReloadListener::onFileChanged, + fileReloadListener::onFileDeleted + ); consumer.accept(PathUtils.get("foo", "bar")); assertThat(latch.getCount(), equalTo(1L)); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/support/LockingAtomicCounterTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/support/LockingAtomicCounterTests.java index c7138d7a40ee7..53611690c7472 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/support/LockingAtomicCounterTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/support/LockingAtomicCounterTests.java @@ -40,7 +40,7 @@ public void testIncrementAndRun() { throw new RuntimeException(e); } }); - assertThat((long)loop, equalTo(lockingAtomicCounter.get())); + assertThat((long) loop, equalTo(lockingAtomicCounter.get())); } public void testRunnableWillNotRunIfCounterHasChanged() throws Exception { diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/support/SecurityIndexManagerTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/support/SecurityIndexManagerTests.java index d83b6e0891b9c..eeebdca90211d 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/support/SecurityIndexManagerTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/support/SecurityIndexManagerTests.java @@ -36,7 +36,6 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.EsExecutors; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.gateway.GatewayService; import org.elasticsearch.index.Index; import org.elasticsearch.index.shard.ShardId; @@ -45,6 +44,7 @@ import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.client.NoOpClient; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.security.index.RestrictedIndicesNames; import org.elasticsearch.xpack.security.Security; import org.elasticsearch.xpack.security.test.SecurityTestUtils; @@ -107,8 +107,10 @@ protected void public void testIndexWithUpToDateMappingAndTemplate() { assertInitialState(); - final ClusterState.Builder clusterStateBuilder = createClusterState(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7, - RestrictedIndicesNames.SECURITY_MAIN_ALIAS); + final ClusterState.Builder clusterStateBuilder = createClusterState( + RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7, + RestrictedIndicesNames.SECURITY_MAIN_ALIAS + ); manager.clusterChanged(event(markShardsAvailable(clusterStateBuilder))); assertThat(manager.indexExists(), Matchers.equalTo(true)); @@ -119,20 +121,24 @@ public void testIndexWithUpToDateMappingAndTemplate() { public void testIndexWithoutPrimaryShards() { assertInitialState(); - final ClusterState cs = createClusterState(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7, - RestrictedIndicesNames.SECURITY_MAIN_ALIAS).build(); + final ClusterState cs = createClusterState( + RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7, + RestrictedIndicesNames.SECURITY_MAIN_ALIAS + ).build(); final ClusterState.Builder clusterStateBuilder = ClusterState.builder(cs); Index index = cs.metadata().index(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7).getIndex(); - ShardRouting shardRouting = ShardRouting.newUnassigned(new ShardId(index, 0), true, - RecoverySource.ExistingStoreRecoverySource.INSTANCE, new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "")); + ShardRouting shardRouting = ShardRouting.newUnassigned( + new ShardId(index, 0), + true, + RecoverySource.ExistingStoreRecoverySource.INSTANCE, + new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "") + ); String nodeId = ESTestCase.randomAlphaOfLength(8); - IndexShardRoutingTable table = new IndexShardRoutingTable.Builder(new ShardId(index, 0)) - .addShard(shardRouting.initialize(nodeId, null, shardRouting.getExpectedShardSize()) - .moveToUnassigned(new UnassignedInfo(UnassignedInfo.Reason.ALLOCATION_FAILED, ""))) - .build(); - clusterStateBuilder.routingTable(RoutingTable.builder() - .add(IndexRoutingTable.builder(index).addIndexShard(table).build()) - .build()); + IndexShardRoutingTable table = new IndexShardRoutingTable.Builder(new ShardId(index, 0)).addShard( + shardRouting.initialize(nodeId, null, shardRouting.getExpectedShardSize()) + .moveToUnassigned(new UnassignedInfo(UnassignedInfo.Reason.ALLOCATION_FAILED, "")) + ).build(); + clusterStateBuilder.routingTable(RoutingTable.builder().add(IndexRoutingTable.builder(index).addIndexShard(table).build()).build()); manager.clusterChanged(event(clusterStateBuilder.build())); assertIndexUpToDateButNotAvailable(); @@ -154,8 +160,10 @@ public void testIndexHealthChangeListeners() { manager.addStateListener(listener); // index doesn't exist and now exists - final ClusterState.Builder clusterStateBuilder = createClusterState(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7, - RestrictedIndicesNames.SECURITY_MAIN_ALIAS); + final ClusterState.Builder clusterStateBuilder = createClusterState( + RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7, + RestrictedIndicesNames.SECURITY_MAIN_ALIAS + ); final ClusterState clusterState = markShardsAvailable(clusterStateBuilder); manager.clusterChanged(event(clusterState)); @@ -179,16 +187,27 @@ public void testIndexHealthChangeListeners() { previousState.set(null); currentState.set(null); Index prevIndex = clusterState.getRoutingTable().index(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7).getIndex(); - final ClusterState newClusterState = ClusterState.builder(clusterState).routingTable(RoutingTable.builder() - .add(IndexRoutingTable.builder(prevIndex) - .addIndexShard(new IndexShardRoutingTable.Builder(new ShardId(prevIndex, 0)) - .addShard(ShardRouting.newUnassigned(new ShardId(prevIndex, 0), true, - RecoverySource.ExistingStoreRecoverySource.INSTANCE, - new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "")) + final ClusterState newClusterState = ClusterState.builder(clusterState) + .routingTable( + RoutingTable.builder() + .add( + IndexRoutingTable.builder(prevIndex) + .addIndexShard( + new IndexShardRoutingTable.Builder(new ShardId(prevIndex, 0)).addShard( + ShardRouting.newUnassigned( + new ShardId(prevIndex, 0), + true, + RecoverySource.ExistingStoreRecoverySource.INSTANCE, + new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "") + ) .initialize(UUIDs.randomBase64UUID(random()), null, 0L) - .moveToUnassigned(new UnassignedInfo(UnassignedInfo.Reason.ALLOCATION_FAILED, ""))) - .build())) - .build()).build(); + .moveToUnassigned(new UnassignedInfo(UnassignedInfo.Reason.ALLOCATION_FAILED, "")) + ).build() + ) + ) + .build() + ) + .build(); event = new ClusterChangedEvent("different index health", newClusterState, clusterState); manager.clusterChanged(event); @@ -213,7 +232,7 @@ public void testWriteBeforeStateNotRecovered() { manager.prepareIndexIfNeededThenExecute(prepareException::set, () -> prepareRunnableCalled.set(true)); assertThat(prepareException.get(), is(notNullValue())); assertThat(prepareException.get(), instanceOf(ElasticsearchStatusException.class)); - assertThat(((ElasticsearchStatusException)prepareException.get()).status(), is(RestStatus.SERVICE_UNAVAILABLE)); + assertThat(((ElasticsearchStatusException) prepareException.get()).status(), is(RestStatus.SERVICE_UNAVAILABLE)); assertThat(prepareRunnableCalled.get(), is(false)); prepareException.set(null); @@ -224,14 +243,17 @@ public void testWriteBeforeStateNotRecovered() { manager.prepareIndexIfNeededThenExecute(prepareException::set, () -> prepareRunnableCalled.set(true)); assertThat(prepareException.get(), is(notNullValue())); assertThat(prepareException.get(), instanceOf(ElasticsearchStatusException.class)); - assertThat(((ElasticsearchStatusException)prepareException.get()).status(), is(RestStatus.SERVICE_UNAVAILABLE)); + assertThat(((ElasticsearchStatusException) prepareException.get()).status(), is(RestStatus.SERVICE_UNAVAILABLE)); assertThat(prepareRunnableCalled.get(), is(false)); prepareException.set(null); prepareRunnableCalled.set(false); // state recovered with index - ClusterState.Builder clusterStateBuilder = createClusterState(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7, - RestrictedIndicesNames.SECURITY_MAIN_ALIAS, SecurityIndexManager.INTERNAL_MAIN_INDEX_FORMAT); + ClusterState.Builder clusterStateBuilder = createClusterState( + RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7, + RestrictedIndicesNames.SECURITY_MAIN_ALIAS, + SecurityIndexManager.INTERNAL_MAIN_INDEX_FORMAT + ); manager.clusterChanged(event(markShardsAvailable(clusterStateBuilder))); manager.prepareIndexIfNeededThenExecute(prepareException::set, () -> prepareRunnableCalled.set(true)); assertThat(prepareException.get(), is(nullValue())); @@ -312,8 +334,11 @@ public void testListenerNotCalledBeforeStateNotRecovered() { assertThat(manager.isStateRecovered(), is(false)); assertThat(listenerCalled.get(), is(false)); // state recovered with index - ClusterState.Builder clusterStateBuilder = createClusterState(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7, - RestrictedIndicesNames.SECURITY_MAIN_ALIAS, SecurityIndexManager.INTERNAL_MAIN_INDEX_FORMAT); + ClusterState.Builder clusterStateBuilder = createClusterState( + RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7, + RestrictedIndicesNames.SECURITY_MAIN_ALIAS, + SecurityIndexManager.INTERNAL_MAIN_INDEX_FORMAT + ); manager.clusterChanged(event(markShardsAvailable(clusterStateBuilder))); assertThat(manager.isStateRecovered(), is(true)); assertThat(listenerCalled.get(), is(true)); @@ -334,8 +359,11 @@ public void testIndexOutOfDateListeners() { assertTrue(manager.isIndexUpToDate()); // index doesn't exist and now exists with wrong format - ClusterState.Builder clusterStateBuilder = createClusterState(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7, - RestrictedIndicesNames.SECURITY_MAIN_ALIAS, SecurityIndexManager.INTERNAL_MAIN_INDEX_FORMAT - 1); + ClusterState.Builder clusterStateBuilder = createClusterState( + RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7, + RestrictedIndicesNames.SECURITY_MAIN_ALIAS, + SecurityIndexManager.INTERNAL_MAIN_INDEX_FORMAT - 1 + ); manager.clusterChanged(event(markShardsAvailable(clusterStateBuilder))); assertTrue(listenerCalled.get()); assertTrue(upToDateChanged.get()); @@ -350,8 +378,11 @@ public void testIndexOutOfDateListeners() { listenerCalled.set(false); // index doesn't exist and now exists with correct format - clusterStateBuilder = createClusterState(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7, - RestrictedIndicesNames.SECURITY_MAIN_ALIAS, SecurityIndexManager.INTERNAL_MAIN_INDEX_FORMAT); + clusterStateBuilder = createClusterState( + RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7, + RestrictedIndicesNames.SECURITY_MAIN_ALIAS, + SecurityIndexManager.INTERNAL_MAIN_INDEX_FORMAT + ); manager.clusterChanged(event(markShardsAvailable(clusterStateBuilder))); assertTrue(listenerCalled.get()); assertFalse(upToDateChanged.get()); @@ -360,15 +391,21 @@ public void testIndexOutOfDateListeners() { public void testProcessClosedIndexState() { // Index initially exists - final ClusterState.Builder indexAvailable = createClusterState(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7, - RestrictedIndicesNames.SECURITY_MAIN_ALIAS, IndexMetadata.State.OPEN); + final ClusterState.Builder indexAvailable = createClusterState( + RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7, + RestrictedIndicesNames.SECURITY_MAIN_ALIAS, + IndexMetadata.State.OPEN + ); manager.clusterChanged(event(markShardsAvailable(indexAvailable))); assertThat(manager.indexExists(), is(true)); assertThat(manager.isAvailable(), is(true)); // Now close it - ClusterState.Builder indexClosed = createClusterState(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7, - RestrictedIndicesNames.SECURITY_MAIN_ALIAS, IndexMetadata.State.CLOSE); + ClusterState.Builder indexClosed = createClusterState( + RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7, + RestrictedIndicesNames.SECURITY_MAIN_ALIAS, + IndexMetadata.State.CLOSE + ); if (randomBoolean()) { // In old/mixed cluster versions closed indices have no routing table indexClosed.routingTable(RoutingTable.EMPTY_ROUTING_TABLE); @@ -407,8 +444,13 @@ public static ClusterState.Builder createClusterState(String indexName, String a return createClusterState(indexName, aliasName, format, IndexMetadata.State.OPEN, getMappings()); } - private static ClusterState.Builder createClusterState(String indexName, String aliasName, int format, IndexMetadata.State state, - String mappings) { + private static ClusterState.Builder createClusterState( + String indexName, + String aliasName, + int format, + IndexMetadata.State state, + String mappings + ) { IndexMetadata.Builder indexMeta = getIndexMetadata(indexName, aliasName, format, state, mappings); Metadata.Builder metadataBuilder = new Metadata.Builder(); @@ -419,28 +461,36 @@ private static ClusterState.Builder createClusterState(String indexName, String private ClusterState markShardsAvailable(ClusterState.Builder clusterStateBuilder) { final ClusterState cs = clusterStateBuilder.build(); - return ClusterState.builder(cs).routingTable( + return ClusterState.builder(cs) + .routingTable( SecurityTestUtils.buildIndexRoutingTable( - cs.metadata().index(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7).getIndex())).build(); + cs.metadata().index(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7).getIndex() + ) + ) + .build(); } private static ClusterState state() { final DiscoveryNodes nodes = DiscoveryNodes.builder().masterNodeId("1").localNodeId("1").build(); - return ClusterState.builder(CLUSTER_NAME) - .nodes(nodes) - .metadata(Metadata.builder().generateClusterUuidIfNeeded()) - .build(); + return ClusterState.builder(CLUSTER_NAME).nodes(nodes).metadata(Metadata.builder().generateClusterUuidIfNeeded()).build(); } - private static IndexMetadata.Builder getIndexMetadata(String indexName, String aliasName, int format, IndexMetadata.State state, - String mappings) { + private static IndexMetadata.Builder getIndexMetadata( + String indexName, + String aliasName, + int format, + IndexMetadata.State state, + String mappings + ) { IndexMetadata.Builder indexMetadata = IndexMetadata.builder(indexName); - indexMetadata.settings(Settings.builder() + indexMetadata.settings( + Settings.builder() .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) .put(IndexMetadata.INDEX_FORMAT_SETTING.getKey(), format) - .build()); + .build() + ); indexMetadata.putAlias(AliasMetadata.builder(aliasName).build()); indexMetadata.state(state); if (mappings != null) { diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/test/SecurityMocks.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/test/SecurityMocks.java index 49afba367d792..0d81e50cd021a 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/test/SecurityMocks.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/test/SecurityMocks.java @@ -98,8 +98,7 @@ public static void mockGetRequest(Client client, String documentId, BytesReferen } public static void mockGetRequest(Client client, String indexAliasName, String documentId, BytesReference source) { - GetResult result = new GetResult(indexAliasName, documentId, 0, 1, 1, true, source, - emptyMap(), emptyMap()); + GetResult result = new GetResult(indexAliasName, documentId, 0, 1, 1, true, source, emptyMap(), emptyMap()); mockGetRequest(client, indexAliasName, documentId, result); } @@ -169,8 +168,16 @@ public static TokenServiceMock tokenService(boolean enabled, ThreadPool threadPo final ClusterService clusterService = mock(ClusterService.class); final SecurityContext securityContext = new SecurityContext(settings, threadPool.getThreadContext()); - final TokenService service = new TokenService(settings, clock, client, licenseState, securityContext, - mockSecurityIndexManager(SECURITY_MAIN_ALIAS), mockSecurityIndexManager(SECURITY_TOKENS_ALIAS), clusterService); + final TokenService service = new TokenService( + settings, + clock, + client, + licenseState, + securityContext, + mockSecurityIndexManager(SECURITY_MAIN_ALIAS), + mockSecurityIndexManager(SECURITY_TOKENS_ALIAS), + clusterService + ); return new TokenServiceMock(service, client); } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/test/SecurityTestUtils.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/test/SecurityTestUtils.java index 9717c93b5ddbe..82f266b53bc69 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/test/SecurityTestUtils.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/test/SecurityTestUtils.java @@ -67,15 +67,17 @@ public static String writeFile(Path folder, String name, String content) { } public static RoutingTable buildIndexRoutingTable(Index index) { - ShardRouting shardRouting = ShardRouting.newUnassigned(new ShardId(index, 0), true, ExistingStoreRecoverySource.INSTANCE, - new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "")); + ShardRouting shardRouting = ShardRouting.newUnassigned( + new ShardId(index, 0), + true, + ExistingStoreRecoverySource.INSTANCE, + new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "") + ); String nodeId = ESTestCase.randomAlphaOfLength(8); - IndexShardRoutingTable table = new IndexShardRoutingTable.Builder(new ShardId(index, 0)) - .addShard(shardRouting.initialize(nodeId, null, shardRouting.getExpectedShardSize()).moveToStarted()) - .build(); - return RoutingTable.builder() - .add(IndexRoutingTable.builder(index).addIndexShard(table).build()) - .build(); + IndexShardRoutingTable table = new IndexShardRoutingTable.Builder(new ShardId(index, 0)).addShard( + shardRouting.initialize(nodeId, null, shardRouting.getExpectedShardSize()).moveToStarted() + ).build(); + return RoutingTable.builder().add(IndexRoutingTable.builder(index).addIndexShard(table).build()).build(); } /** diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/AbstractSimpleSecurityTransportTestCase.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/AbstractSimpleSecurityTransportTestCase.java index b45968754c26d..cdeb78f884718 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/AbstractSimpleSecurityTransportTestCase.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/AbstractSimpleSecurityTransportTestCase.java @@ -10,7 +10,6 @@ import org.elasticsearch.action.support.PlainActionFuture; import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.node.DiscoveryNodeRole; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.common.io.stream.OutputStreamStreamOutput; import org.elasticsearch.common.settings.MockSecureSettings; import org.elasticsearch.common.settings.Setting; @@ -18,6 +17,7 @@ import org.elasticsearch.common.ssl.SslClientAuthenticationMode; import org.elasticsearch.common.ssl.SslConfiguration; import org.elasticsearch.common.transport.TransportAddress; +import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.test.transport.MockTransportService; @@ -34,17 +34,6 @@ import org.elasticsearch.xpack.core.common.socket.SocketAccess; import org.elasticsearch.xpack.core.ssl.SSLService; -import javax.net.SocketFactory; -import javax.net.ssl.HandshakeCompletedListener; -import javax.net.ssl.SNIHostName; -import javax.net.ssl.SNIMatcher; -import javax.net.ssl.SNIServerName; -import javax.net.ssl.SSLContext; -import javax.net.ssl.SSLEngine; -import javax.net.ssl.SSLParameters; -import javax.net.ssl.SSLServerSocket; -import javax.net.ssl.SSLServerSocketFactory; -import javax.net.ssl.SSLSocket; import java.io.IOException; import java.io.UncheckedIOException; import java.net.InetAddress; @@ -62,6 +51,18 @@ import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; +import javax.net.SocketFactory; +import javax.net.ssl.HandshakeCompletedListener; +import javax.net.ssl.SNIHostName; +import javax.net.ssl.SNIMatcher; +import javax.net.ssl.SNIServerName; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLEngine; +import javax.net.ssl.SSLParameters; +import javax.net.ssl.SSLServerSocket; +import javax.net.ssl.SSLServerSocketFactory; +import javax.net.ssl.SSLSocket; + import static java.util.Collections.emptyMap; import static java.util.Collections.emptySet; import static org.hamcrest.Matchers.containsString; @@ -106,8 +107,16 @@ protected Set> getSupportedSettings() { public void testConnectException() throws UnknownHostException { try { - connectToNode(serviceA, new DiscoveryNode("C", new TransportAddress(InetAddress.getByName("localhost"), 9876), - emptyMap(), emptySet(), Version.CURRENT)); + connectToNode( + serviceA, + new DiscoveryNode( + "C", + new TransportAddress(InetAddress.getByName("localhost"), 9876), + emptyMap(), + emptySet(), + Version.CURRENT + ) + ); fail("Expected ConnectTransportException"); } catch (ConnectTransportException e) { assertThat(e.getMessage(), containsString("connect_exception")); @@ -125,8 +134,14 @@ public void testTcpHandshake() { ConnectionProfile connectionProfile = ConnectionProfile.buildDefaultConnectionProfile(Settings.EMPTY); try (TransportService service = buildService("TS_TPC", Version.CURRENT, Settings.EMPTY)) { - DiscoveryNode node = new DiscoveryNode("TS_TPC", "TS_TPC", service.boundAddress().publishAddress(), emptyMap(), emptySet(), - version0); + DiscoveryNode node = new DiscoveryNode( + "TS_TPC", + "TS_TPC", + service.boundAddress().publishAddress(), + emptyMap(), + emptySet(), + version0 + ); PlainActionFuture future = PlainActionFuture.newFuture(); originalTransport.openConnection(node, connectionProfile, future); try (TcpTransport.NodeChannels connection = (TcpTransport.NodeChannels) future.actionGet()) { @@ -137,11 +152,11 @@ public void testTcpHandshake() { @SuppressForbidden(reason = "Need to open socket connection") public void testRenegotiation() throws Exception { - assumeFalse("BCTLS doesn't support renegotiation: https://github.com/bcgit/bc-java/issues/593#issuecomment-533518845", - inFipsJvm()); + assumeFalse("BCTLS doesn't support renegotiation: https://github.com/bcgit/bc-java/issues/593#issuecomment-533518845", inFipsJvm()); // force TLSv1.2 since renegotiation is not supported by 1.3 - SSLService sslService = - createSSLService(Settings.builder().put("xpack.security.transport.ssl.supported_protocols", "TLSv1.2").build()); + SSLService sslService = createSSLService( + Settings.builder().put("xpack.security.transport.ssl.supported_protocols", "TLSv1.2").build() + ); final SslConfiguration sslConfiguration = sslService.getSSLConfiguration("xpack.security.transport.ssl"); SocketFactory factory = sslService.sslSocketFactory(sslConfiguration); try (SSLSocket socket = (SSLSocket) factory.createSocket()) { @@ -234,14 +249,17 @@ public boolean matches(SNIServerName sniServerName) { InetSocketAddress serverAddress = (InetSocketAddress) SocketAccess.doPrivileged(sslServerSocket::getLocalSocketAddress); - Settings settings = Settings.builder() - .put("xpack.security.transport.ssl.verification_mode", "none") - .build(); + Settings settings = Settings.builder().put("xpack.security.transport.ssl.verification_mode", "none").build(); try (MockTransportService serviceC = buildService("TS_C", version0, settings)) { HashMap attributes = new HashMap<>(); attributes.put("server_name", sniIp); - DiscoveryNode node = new DiscoveryNode("server_node_id", new TransportAddress(serverAddress), attributes, - DiscoveryNodeRole.roles(), Version.CURRENT); + DiscoveryNode node = new DiscoveryNode( + "server_node_id", + new TransportAddress(serverAddress), + attributes, + DiscoveryNodeRole.roles(), + Version.CURRENT + ); new Thread(() -> { try { @@ -279,17 +297,22 @@ public void testInvalidSNIServerName() throws Exception { InetSocketAddress serverAddress = (InetSocketAddress) SocketAccess.doPrivileged(sslServerSocket::getLocalSocketAddress); - Settings settings = Settings.builder() - .put("xpack.security.transport.ssl.verification_mode", "none") - .build(); + Settings settings = Settings.builder().put("xpack.security.transport.ssl.verification_mode", "none").build(); try (MockTransportService serviceC = buildService("TS_C", version0, settings)) { HashMap attributes = new HashMap<>(); attributes.put("server_name", sniIp); - DiscoveryNode node = new DiscoveryNode("server_node_id", new TransportAddress(serverAddress), attributes, - DiscoveryNodeRole.roles(), Version.CURRENT); - - ConnectTransportException connectException = expectThrows(ConnectTransportException.class, - () -> connectToNode(serviceC, node, TestProfiles.LIGHT_PROFILE)); + DiscoveryNode node = new DiscoveryNode( + "server_node_id", + new TransportAddress(serverAddress), + attributes, + DiscoveryNodeRole.roles(), + Version.CURRENT + ); + + ConnectTransportException connectException = expectThrows( + ConnectTransportException.class, + () -> connectToNode(serviceC, node, TestProfiles.LIGHT_PROFILE) + ); assertThat(connectException.getMessage(), containsString("invalid DiscoveryNode server_name [invalid_hostname]")); } @@ -419,8 +442,9 @@ private TcpChannel getAcceptedChannel(TcpTransport transport, Transport.Connecti InetSocketAddress localAddress = getSingleChannel(connection).getLocalAddress(); AtomicReference accepted = new AtomicReference<>(); assertBusy(() -> { - Optional maybeAccepted = getAcceptedChannels(transport) - .stream().filter(c -> c.getRemoteAddress().equals(localAddress)).findFirst(); + Optional maybeAccepted = getAcceptedChannels(transport).stream() + .filter(c -> c.getRemoteAddress().equals(localAddress)) + .findFirst(); assertTrue(maybeAccepted.isPresent()); accepted.set(maybeAccepted.get()); }); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/SecurityHttpSettingsTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/SecurityHttpSettingsTests.java index 7f16601ce34a4..b7ee9b846d64f 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/SecurityHttpSettingsTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/SecurityHttpSettingsTests.java @@ -16,8 +16,7 @@ public class SecurityHttpSettingsTests extends ESTestCase { public void testDisablesCompressionByDefaultForSsl() { - Settings settings = Settings.builder() - .put(XPackSettings.HTTP_SSL_ENABLED.getKey(), true).build(); + Settings settings = Settings.builder().put(XPackSettings.HTTP_SSL_ENABLED.getKey(), true).build(); Settings.Builder pluginSettingsBuilder = Settings.builder(); SecurityHttpSettings.overrideSettings(pluginSettingsBuilder, settings); @@ -25,8 +24,7 @@ public void testDisablesCompressionByDefaultForSsl() { } public void testLeavesCompressionOnIfNotSsl() { - Settings settings = Settings.builder() - .put(XPackSettings.HTTP_SSL_ENABLED.getKey(), false).build(); + Settings settings = Settings.builder().put(XPackSettings.HTTP_SSL_ENABLED.getKey(), false).build(); Settings.Builder pluginSettingsBuilder = Settings.builder(); SecurityHttpSettings.overrideSettings(pluginSettingsBuilder, settings); assertThat(pluginSettingsBuilder.build().isEmpty(), is(true)); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/SecurityServerTransportInterceptorTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/SecurityServerTransportInterceptorTests.java index a571f916dc2a7..9a2ef77815056 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/SecurityServerTransportInterceptorTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/SecurityServerTransportInterceptorTests.java @@ -81,24 +81,37 @@ public void stopThreadPool() throws Exception { terminate(threadPool); } - public void testSendAsync() throws Exception { final User authUser = randomBoolean() ? new User("authenticator") : null; final User user = new User("test", randomRoles(), authUser); final Authentication authentication = new Authentication(user, new RealmRef("ldap", "foo", "node1"), null); authentication.writeToContext(threadContext); - SecurityServerTransportInterceptor interceptor = new SecurityServerTransportInterceptor(settings, threadPool, - mock(AuthenticationService.class), mock(AuthorizationService.class), mock(SSLService.class), - securityContext, new DestructiveOperations(Settings.EMPTY, new ClusterSettings(Settings.EMPTY, - Collections.singleton(DestructiveOperations.REQUIRES_NAME_SETTING))), clusterService); + SecurityServerTransportInterceptor interceptor = new SecurityServerTransportInterceptor( + settings, + threadPool, + mock(AuthenticationService.class), + mock(AuthorizationService.class), + mock(SSLService.class), + securityContext, + new DestructiveOperations( + Settings.EMPTY, + new ClusterSettings(Settings.EMPTY, Collections.singleton(DestructiveOperations.REQUIRES_NAME_SETTING)) + ), + clusterService + ); ClusterServiceUtils.setState(clusterService, clusterService.state()); // force state update to trigger listener AtomicBoolean calledWrappedSender = new AtomicBoolean(false); AtomicReference sendingUser = new AtomicReference<>(); AsyncSender sender = interceptor.interceptSender(new AsyncSender() { @Override - public void sendRequest(Transport.Connection connection, String action, TransportRequest request, - TransportRequestOptions options, TransportResponseHandler handler) { + public void sendRequest( + Transport.Connection connection, + String action, + TransportRequest request, + TransportRequestOptions options, + TransportResponseHandler handler + ) { if (calledWrappedSender.compareAndSet(false, true) == false) { fail("sender called more than once!"); } @@ -121,18 +134,32 @@ public void testSendAsyncSwitchToSystem() throws Exception { authentication.writeToContext(threadContext); threadContext.putTransient(AuthorizationServiceField.ORIGINATING_ACTION_KEY, "indices:foo"); - SecurityServerTransportInterceptor interceptor = new SecurityServerTransportInterceptor(settings, threadPool, - mock(AuthenticationService.class), mock(AuthorizationService.class), mock(SSLService.class), - securityContext, new DestructiveOperations(Settings.EMPTY, new ClusterSettings(Settings.EMPTY, - Collections.singleton(DestructiveOperations.REQUIRES_NAME_SETTING))), clusterService); + SecurityServerTransportInterceptor interceptor = new SecurityServerTransportInterceptor( + settings, + threadPool, + mock(AuthenticationService.class), + mock(AuthorizationService.class), + mock(SSLService.class), + securityContext, + new DestructiveOperations( + Settings.EMPTY, + new ClusterSettings(Settings.EMPTY, Collections.singleton(DestructiveOperations.REQUIRES_NAME_SETTING)) + ), + clusterService + ); ClusterServiceUtils.setState(clusterService, clusterService.state()); // force state update to trigger listener AtomicBoolean calledWrappedSender = new AtomicBoolean(false); AtomicReference sendingUser = new AtomicReference<>(); AsyncSender sender = interceptor.interceptSender(new AsyncSender() { @Override - public void sendRequest(Transport.Connection connection, String action, TransportRequest request, - TransportRequestOptions options, TransportResponseHandler handler) { + public void sendRequest( + Transport.Connection connection, + String action, + TransportRequest request, + TransportRequestOptions options, + TransportResponseHandler handler + ) { if (calledWrappedSender.compareAndSet(false, true) == false) { fail("sender called more than once!"); } @@ -150,28 +177,43 @@ public void sendRequest(Transport.Connection conne } public void testSendWithoutUser() throws Exception { - SecurityServerTransportInterceptor interceptor = new SecurityServerTransportInterceptor(settings, threadPool, - mock(AuthenticationService.class), mock(AuthorizationService.class), mock(SSLService.class), - securityContext, new DestructiveOperations(Settings.EMPTY, new ClusterSettings(Settings.EMPTY, - Collections.singleton(DestructiveOperations.REQUIRES_NAME_SETTING))), clusterService) { + SecurityServerTransportInterceptor interceptor = new SecurityServerTransportInterceptor( + settings, + threadPool, + mock(AuthenticationService.class), + mock(AuthorizationService.class), + mock(SSLService.class), + securityContext, + new DestructiveOperations( + Settings.EMPTY, + new ClusterSettings(Settings.EMPTY, Collections.singleton(DestructiveOperations.REQUIRES_NAME_SETTING)) + ), + clusterService + ) { @Override - void assertNoAuthentication(String action) { - } + void assertNoAuthentication(String action) {} }; ClusterServiceUtils.setState(clusterService, clusterService.state()); // force state update to trigger listener assertNull(securityContext.getUser()); AsyncSender sender = interceptor.interceptSender(new AsyncSender() { @Override - public void sendRequest(Transport.Connection connection, String action, TransportRequest request, - TransportRequestOptions options, TransportResponseHandler handler) { + public void sendRequest( + Transport.Connection connection, + String action, + TransportRequest request, + TransportRequestOptions options, + TransportResponseHandler handler + ) { fail("sender should not be called!"); } }); Transport.Connection connection = mock(Transport.Connection.class); when(connection.getVersion()).thenReturn(Version.CURRENT); - IllegalStateException e = - expectThrows(IllegalStateException.class, () -> sender.sendRequest(connection, "indices:foo", null, null, null)); + IllegalStateException e = expectThrows( + IllegalStateException.class, + () -> sender.sendRequest(connection, "indices:foo", null, null, null) + ); assertEquals("there should always be a user when sending a message for action [indices:foo]", e.getMessage()); assertNull(securityContext.getUser()); verify(securityContext, never()).executeAsUser(any(User.class), anyConsumer(), any(Version.class)); @@ -184,10 +226,19 @@ public void testSendToNewerVersionSetsCorrectVersion() throws Exception { authentication.writeToContext(threadContext); threadContext.putTransient(AuthorizationServiceField.ORIGINATING_ACTION_KEY, "indices:foo"); - SecurityServerTransportInterceptor interceptor = new SecurityServerTransportInterceptor(settings, threadPool, - mock(AuthenticationService.class), mock(AuthorizationService.class), mock(SSLService.class), - securityContext, new DestructiveOperations(Settings.EMPTY, new ClusterSettings(Settings.EMPTY, - Collections.singleton(DestructiveOperations.REQUIRES_NAME_SETTING))), clusterService); + SecurityServerTransportInterceptor interceptor = new SecurityServerTransportInterceptor( + settings, + threadPool, + mock(AuthenticationService.class), + mock(AuthorizationService.class), + mock(SSLService.class), + securityContext, + new DestructiveOperations( + Settings.EMPTY, + new ClusterSettings(Settings.EMPTY, Collections.singleton(DestructiveOperations.REQUIRES_NAME_SETTING)) + ), + clusterService + ); ClusterServiceUtils.setState(clusterService, clusterService.state()); // force state update to trigger listener AtomicBoolean calledWrappedSender = new AtomicBoolean(false); @@ -195,8 +246,13 @@ securityContext, new DestructiveOperations(Settings.EMPTY, new ClusterSettings(S AtomicReference authRef = new AtomicReference<>(); AsyncSender intercepted = new AsyncSender() { @Override - public void sendRequest(Transport.Connection connection, String action, TransportRequest request, - TransportRequestOptions options, TransportResponseHandler handler) { + public void sendRequest( + Transport.Connection connection, + String action, + TransportRequest request, + TransportRequestOptions options, + TransportResponseHandler handler + ) { if (calledWrappedSender.compareAndSet(false, true) == false) { fail("sender called more than once!"); } @@ -225,10 +281,19 @@ public void testSendToOlderVersionSetsCorrectVersion() throws Exception { authentication.writeToContext(threadContext); threadContext.putTransient(AuthorizationServiceField.ORIGINATING_ACTION_KEY, "indices:foo"); - SecurityServerTransportInterceptor interceptor = new SecurityServerTransportInterceptor(settings, threadPool, - mock(AuthenticationService.class), mock(AuthorizationService.class), mock(SSLService.class), - securityContext, new DestructiveOperations(Settings.EMPTY, new ClusterSettings(Settings.EMPTY, - Collections.singleton(DestructiveOperations.REQUIRES_NAME_SETTING))), clusterService); + SecurityServerTransportInterceptor interceptor = new SecurityServerTransportInterceptor( + settings, + threadPool, + mock(AuthenticationService.class), + mock(AuthorizationService.class), + mock(SSLService.class), + securityContext, + new DestructiveOperations( + Settings.EMPTY, + new ClusterSettings(Settings.EMPTY, Collections.singleton(DestructiveOperations.REQUIRES_NAME_SETTING)) + ), + clusterService + ); ClusterServiceUtils.setState(clusterService, clusterService.state()); // force state update to trigger listener AtomicBoolean calledWrappedSender = new AtomicBoolean(false); @@ -236,8 +301,13 @@ securityContext, new DestructiveOperations(Settings.EMPTY, new ClusterSettings(S AtomicReference authRef = new AtomicReference<>(); AsyncSender intercepted = new AsyncSender() { @Override - public void sendRequest(Transport.Connection connection, String action, TransportRequest request, - TransportRequestOptions options, TransportResponseHandler handler) { + public void sendRequest( + Transport.Connection connection, + String action, + TransportRequest request, + TransportRequestOptions options, + TransportResponseHandler handler + ) { if (calledWrappedSender.compareAndSet(false, true) == false) { fail("sender called more than once!"); } @@ -268,19 +338,21 @@ public void testContextRestoreResponseHandler() throws Exception { threadContext.putTransient("foo", "different_bar"); threadContext.putHeader("key", "value2"); TransportResponseHandler handler = new TransportService.ContextRestoreResponseHandler<>( - threadContext.wrapRestorable(storedContext), new TransportResponseHandler.Empty() { - @Override - public void handleResponse(TransportResponse.Empty response) { - assertEquals("bar", threadContext.getTransient("foo")); - assertEquals("value", threadContext.getHeader("key")); + threadContext.wrapRestorable(storedContext), + new TransportResponseHandler.Empty() { + @Override + public void handleResponse(TransportResponse.Empty response) { + assertEquals("bar", threadContext.getTransient("foo")); + assertEquals("value", threadContext.getHeader("key")); + } + + @Override + public void handleException(TransportException exp) { + assertEquals("bar", threadContext.getTransient("foo")); + assertEquals("value", threadContext.getHeader("key")); + } } - - @Override - public void handleException(TransportException exp) { - assertEquals("bar", threadContext.getTransient("foo")); - assertEquals("value", threadContext.getHeader("key")); - } - }); + ); handler.handleResponse(null); handler.handleException(null); @@ -295,20 +367,22 @@ public void testContextRestoreResponseHandlerRestoreOriginalContext() throws Exc try (ThreadContext.StoredContext ignore = threadContext.stashContext()) { threadContext.putTransient("foo", "different_bar"); threadContext.putHeader("key", "value2"); - handler = new TransportService.ContextRestoreResponseHandler<>(threadContext.newRestorableContext(true), - new TransportResponseHandler.Empty() { - @Override - public void handleResponse(TransportResponse.Empty response) { - assertEquals("different_bar", threadContext.getTransient("foo")); - assertEquals("value2", threadContext.getHeader("key")); - } - - @Override - public void handleException(TransportException exp) { - assertEquals("different_bar", threadContext.getTransient("foo")); - assertEquals("value2", threadContext.getHeader("key")); - } - }); + handler = new TransportService.ContextRestoreResponseHandler<>( + threadContext.newRestorableContext(true), + new TransportResponseHandler.Empty() { + @Override + public void handleResponse(TransportResponse.Empty response) { + assertEquals("different_bar", threadContext.getTransient("foo")); + assertEquals("value2", threadContext.getHeader("key")); + } + + @Override + public void handleException(TransportException exp) { + assertEquals("different_bar", threadContext.getTransient("foo")); + assertEquals("value2", threadContext.getHeader("key")); + } + } + ); } assertEquals("bar", threadContext.getTransient("foo")); @@ -327,26 +401,19 @@ public void testProfileSecuredRequestHandlerDecrementsRefCountOnFailure() throws final String profileName = "some-profile"; final DestructiveOperations destructiveOperations = new DestructiveOperations(Settings.EMPTY, clusterService.getClusterSettings()); final SecurityServerTransportInterceptor.ProfileSecuredRequestHandler requestHandler = - new SecurityServerTransportInterceptor.ProfileSecuredRequestHandler<>( - logger, - DeleteIndexAction.NAME, - randomBoolean(), - randomBoolean() ? ThreadPool.Names.SAME : ThreadPool.Names.GENERIC, - (request, channel, task) -> fail("should fail at destructive operations check to trigger listener failure"), - Map.of( - profileName, - new ServerTransportFilter( - null, - null, - threadContext, - randomBoolean(), - destructiveOperations, - securityContext - ) - ), - settings, - threadPool - ); + new SecurityServerTransportInterceptor.ProfileSecuredRequestHandler<>( + logger, + DeleteIndexAction.NAME, + randomBoolean(), + randomBoolean() ? ThreadPool.Names.SAME : ThreadPool.Names.GENERIC, + (request, channel, task) -> fail("should fail at destructive operations check to trigger listener failure"), + Map.of( + profileName, + new ServerTransportFilter(null, null, threadContext, randomBoolean(), destructiveOperations, securityContext) + ), + settings, + threadPool + ); final TransportChannel channel = mock(TransportChannel.class); when(channel.getProfileName()).thenReturn(profileName); final AtomicBoolean exceptionSent = new AtomicBoolean(false); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/ServerTransportFilterTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/ServerTransportFilterTests.java index 15cf54666c3a0..106aa4d133d6b 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/ServerTransportFilterTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/ServerTransportFilterTests.java @@ -67,10 +67,11 @@ public void init() throws Exception { when(channel.getProfileName()).thenReturn(TransportSettings.DEFAULT_PROFILE); when(channel.getVersion()).thenReturn(Version.CURRENT); failDestructiveOperations = randomBoolean(); - Settings settings = Settings.builder() - .put(DestructiveOperations.REQUIRES_NAME_SETTING.getKey(), failDestructiveOperations).build(); - destructiveOperations = new DestructiveOperations(settings, - new ClusterSettings(settings, Collections.singleton(DestructiveOperations.REQUIRES_NAME_SETTING))); + Settings settings = Settings.builder().put(DestructiveOperations.REQUIRES_NAME_SETTING.getKey(), failDestructiveOperations).build(); + destructiveOperations = new DestructiveOperations( + settings, + new ClusterSettings(settings, Collections.singleton(DestructiveOperations.REQUIRES_NAME_SETTING)) + ); } public void testInbound() throws Exception { @@ -82,15 +83,16 @@ public void testInbound() throws Exception { ServerTransportFilter filter = getNodeFilter(); PlainActionFuture future = new PlainActionFuture<>(); filter.inbound("_action", request, channel, future); - //future.get(); // don't block it's not called really just mocked + // future.get(); // don't block it's not called really just mocked verify(authzService).authorize(eq(authentication), eq("_action"), eq(request), anyActionListener()); } public void testInboundDestructiveOperations() throws Exception { String action = randomFrom(CloseIndexAction.NAME, OpenIndexAction.NAME, DeleteIndexAction.NAME); TransportRequest request = new MockIndicesRequest( - IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean()), - randomFrom("*", "_all", "test*")); + IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean()), + randomFrom("*", "_all", "test*") + ); Authentication authentication = mock(Authentication.class); when(authentication.getVersion()).thenReturn(Version.CURRENT); when(authentication.getUser()).thenReturn(SystemUser.INSTANCE); @@ -138,8 +140,8 @@ public void testInboundAuthorizationException() throws Exception { when(authentication.getVersion()).thenReturn(Version.CURRENT); when(authentication.getUser()).thenReturn(XPackUser.INSTANCE); PlainActionFuture future = new PlainActionFuture<>(); - doThrow(authorizationError("authz failed")) - .when(authzService).authorize(eq(authentication), eq("_action"), eq(request), anyActionListener()); + doThrow(authorizationError("authz failed")).when(authzService) + .authorize(eq(authentication), eq("_action"), eq(request), anyActionListener()); ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, () -> { filter.inbound("_action", request, channel, future); future.actionGet(); @@ -181,7 +183,13 @@ private static Answer> getAnswer(Authentication authentication) { private ServerTransportFilter getNodeFilter() { Settings settings = Settings.builder().put("path.home", createTempDir()).build(); ThreadContext threadContext = new ThreadContext(settings); - return new ServerTransportFilter(authcService, authzService, threadContext, false, destructiveOperations, - new SecurityContext(settings, threadContext)); + return new ServerTransportFilter( + authcService, + authzService, + threadContext, + false, + destructiveOperations, + new SecurityContext(settings, threadContext) + ); } } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/filter/IPFilterTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/filter/IPFilterTests.java index d8fc1be9672db..b690c7c47db84 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/filter/IPFilterTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/filter/IPFilterTests.java @@ -66,15 +66,21 @@ public void init() { when(licenseState.isAllowed(Security.AUDITING_FEATURE)).thenReturn(true); auditTrail = mock(AuditTrail.class); auditTrailService = new AuditTrailService(Collections.singletonList(auditTrail), licenseState); - clusterSettings = new ClusterSettings(Settings.EMPTY, new HashSet<>(Arrays.asList( - IPFilter.HTTP_FILTER_ALLOW_SETTING, - IPFilter.HTTP_FILTER_DENY_SETTING, - IPFilter.IP_FILTER_ENABLED_HTTP_SETTING, - IPFilter.IP_FILTER_ENABLED_SETTING, - IPFilter.TRANSPORT_FILTER_ALLOW_SETTING, - IPFilter.TRANSPORT_FILTER_DENY_SETTING, - IPFilter.PROFILE_FILTER_ALLOW_SETTING, - IPFilter.PROFILE_FILTER_DENY_SETTING))); + clusterSettings = new ClusterSettings( + Settings.EMPTY, + new HashSet<>( + Arrays.asList( + IPFilter.HTTP_FILTER_ALLOW_SETTING, + IPFilter.HTTP_FILTER_DENY_SETTING, + IPFilter.IP_FILTER_ENABLED_HTTP_SETTING, + IPFilter.IP_FILTER_ENABLED_SETTING, + IPFilter.TRANSPORT_FILTER_ALLOW_SETTING, + IPFilter.TRANSPORT_FILTER_DENY_SETTING, + IPFilter.PROFILE_FILTER_ALLOW_SETTING, + IPFilter.PROFILE_FILTER_DENY_SETTING + ) + ) + ); httpTransport = mock(HttpServerTransport.class); TransportAddress httpAddress = new TransportAddress(InetAddress.getLoopbackAddress(), 9200); @@ -83,20 +89,21 @@ public void init() { transport = mock(Transport.class); TransportAddress address = new TransportAddress(InetAddress.getLoopbackAddress(), 9300); - when(transport.boundAddress()).thenReturn(new BoundTransportAddress(new TransportAddress[]{ address }, address)); + when(transport.boundAddress()).thenReturn(new BoundTransportAddress(new TransportAddress[] { address }, address)); when(transport.lifecycleState()).thenReturn(Lifecycle.State.STARTED); - Map profileBoundAddresses = Collections.singletonMap("client", - new BoundTransportAddress(new TransportAddress[]{ new TransportAddress(InetAddress.getLoopbackAddress(), 9500) }, - address)); + Map profileBoundAddresses = Collections.singletonMap( + "client", + new BoundTransportAddress(new TransportAddress[] { new TransportAddress(InetAddress.getLoopbackAddress(), 9500) }, address) + ); when(transport.profileBoundAddresses()).thenReturn(profileBoundAddresses); } public void testThatIpV4AddressesCanBeProcessed() throws Exception { Settings settings = Settings.builder() - .put("xpack.security.transport.filter.allow", "127.0.0.1") - .put("xpack.security.transport.filter.deny", "10.0.0.0/8") - .build(); + .put("xpack.security.transport.filter.allow", "127.0.0.1") + .put("xpack.security.transport.filter.deny", "10.0.0.0/8") + .build(); ipFilter = new IPFilter(settings, auditTrailService, clusterSettings, licenseState); ipFilter.setBoundTransportAddress(transport.boundAddress(), transport.profileBoundAddresses()); assertAddressIsAllowed("127.0.0.1"); @@ -107,9 +114,9 @@ public void testThatIpV6AddressesCanBeProcessed() throws Exception { // you have to use the shortest possible notation in order to match, so // 1234:0db8:85a3:0000:0000:8a2e:0370:7334 becomes 1234:db8:85a3:0:0:8a2e:370:7334 Settings settings = Settings.builder() - .put("xpack.security.transport.filter.allow", "2001:0db8:1234::/48") - .putList("xpack.security.transport.filter.deny", "1234:db8:85a3:0:0:8a2e:370:7334", "4321:db8:1234::/48") - .build(); + .put("xpack.security.transport.filter.allow", "2001:0db8:1234::/48") + .putList("xpack.security.transport.filter.deny", "1234:db8:85a3:0:0:8a2e:370:7334", "4321:db8:1234::/48") + .build(); ipFilter = new IPFilter(settings, auditTrailService, clusterSettings, licenseState); ipFilter.setBoundTransportAddress(transport.boundAddress(), transport.profileBoundAddresses()); @@ -121,10 +128,10 @@ public void testThatIpV6AddressesCanBeProcessed() throws Exception { @Network // requires network for name resolution public void testThatHostnamesCanBeProcessed() throws Exception { Settings settings = Settings.builder() - .put("xpack.ml.autodetect_process", false) - .put("xpack.security.transport.filter.allow", "127.0.0.1") - .put("xpack.security.transport.filter.deny", "*.google.com") - .build(); + .put("xpack.ml.autodetect_process", false) + .put("xpack.security.transport.filter.allow", "127.0.0.1") + .put("xpack.security.transport.filter.deny", "*.google.com") + .build(); ipFilter = new IPFilter(settings, auditTrailService, clusterSettings, licenseState); ipFilter.setBoundTransportAddress(transport.boundAddress(), transport.profileBoundAddresses()); @@ -133,9 +140,7 @@ public void testThatHostnamesCanBeProcessed() throws Exception { } public void testThatAnAllowAllAuthenticatorWorks() throws Exception { - Settings settings = Settings.builder() - .put("xpack.security.transport.filter.allow", "_all") - .build(); + Settings settings = Settings.builder().put("xpack.security.transport.filter.allow", "_all").build(); ipFilter = new IPFilter(settings, auditTrailService, clusterSettings, licenseState); ipFilter.setBoundTransportAddress(transport.boundAddress(), transport.profileBoundAddresses()); assertAddressIsAllowed("127.0.0.1"); @@ -144,11 +149,11 @@ public void testThatAnAllowAllAuthenticatorWorks() throws Exception { public void testThatProfilesAreSupported() throws Exception { Settings settings = Settings.builder() - .put("xpack.security.transport.filter.allow", "localhost") - .put("xpack.security.transport.filter.deny", "_all") - .put("transport.profiles.client.xpack.security.filter.allow", "192.168.0.1") - .put("transport.profiles.client.xpack.security.filter.deny", "_all") - .build(); + .put("xpack.security.transport.filter.allow", "localhost") + .put("xpack.security.transport.filter.deny", "_all") + .put("transport.profiles.client.xpack.security.filter.allow", "192.168.0.1") + .put("transport.profiles.client.xpack.security.filter.deny", "_all") + .build(); ipFilter = new IPFilter(settings, auditTrailService, clusterSettings, licenseState); ipFilter.setBoundTransportAddress(transport.boundAddress(), transport.profileBoundAddresses()); assertAddressIsAllowed("127.0.0.1"); @@ -160,16 +165,17 @@ public void testThatProfilesAreSupported() throws Exception { public void testThatProfilesAreUpdateable() throws Exception { Settings settings = Settings.builder() - .put("xpack.security.transport.filter.allow", "localhost") - .put("xpack.security.transport.filter.deny", "_all") - .put("transport.profiles.client.xpack.security.filter.allow", "192.168.0.1") - .put("transport.profiles.client.xpack.security.filter.deny", "_all") - .build(); + .put("xpack.security.transport.filter.allow", "localhost") + .put("xpack.security.transport.filter.deny", "_all") + .put("transport.profiles.client.xpack.security.filter.allow", "192.168.0.1") + .put("transport.profiles.client.xpack.security.filter.deny", "_all") + .build(); ipFilter = new IPFilter(settings, auditTrailService, clusterSettings, licenseState); ipFilter.setBoundTransportAddress(transport.boundAddress(), transport.profileBoundAddresses()); - Settings newSettings = Settings.builder().putList("transport.profiles.client.xpack.security.filter.allow", "192.168.0.1", - "192.168.0.2") - .put("transport.profiles.client.xpack.security.filter.deny", "192.168.0.3").build(); + Settings newSettings = Settings.builder() + .putList("transport.profiles.client.xpack.security.filter.allow", "192.168.0.1", "192.168.0.2") + .put("transport.profiles.client.xpack.security.filter.deny", "192.168.0.3") + .build(); Settings.Builder updatedSettingsBuilder = Settings.builder(); clusterSettings.updateDynamicSettings(newSettings, updatedSettingsBuilder, Settings.builder(), "test"); clusterSettings.applySettings(updatedSettingsBuilder.build()); @@ -182,9 +188,9 @@ public void testThatProfilesAreUpdateable() throws Exception { public void testThatAllowWinsOverDeny() throws Exception { Settings settings = Settings.builder() - .put("xpack.security.transport.filter.allow", "10.0.0.1") - .put("xpack.security.transport.filter.deny", "10.0.0.0/8") - .build(); + .put("xpack.security.transport.filter.allow", "10.0.0.1") + .put("xpack.security.transport.filter.deny", "10.0.0.0/8") + .build(); ipFilter = new IPFilter(settings, auditTrailService, clusterSettings, licenseState); ipFilter.setBoundTransportAddress(transport.boundAddress(), transport.profileBoundAddresses()); assertAddressIsAllowed("10.0.0.1"); @@ -201,11 +207,11 @@ public void testDefaultAllow() throws Exception { public void testThatHttpWorks() throws Exception { Settings settings = Settings.builder() - .put("xpack.security.transport.filter.allow", "127.0.0.1") - .put("xpack.security.transport.filter.deny", "10.0.0.0/8") - .put("xpack.security.http.filter.allow", "10.0.0.0/8") - .put("xpack.security.http.filter.deny", "192.168.0.1") - .build(); + .put("xpack.security.transport.filter.allow", "127.0.0.1") + .put("xpack.security.transport.filter.deny", "10.0.0.0/8") + .put("xpack.security.http.filter.allow", "10.0.0.0/8") + .put("xpack.security.http.filter.deny", "192.168.0.1") + .build(); ipFilter = new IPFilter(settings, auditTrailService, clusterSettings, licenseState); ipFilter.setBoundHttpTransportAddress(httpTransport.boundAddress()); ipFilter.setBoundTransportAddress(transport.boundAddress(), transport.profileBoundAddresses()); @@ -215,9 +221,9 @@ public void testThatHttpWorks() throws Exception { public void testThatHttpFallsbackToDefault() throws Exception { Settings settings = Settings.builder() - .put("xpack.security.transport.filter.allow", "127.0.0.1") - .put("xpack.security.transport.filter.deny", "10.0.0.0/8") - .build(); + .put("xpack.security.transport.filter.allow", "127.0.0.1") + .put("xpack.security.transport.filter.deny", "10.0.0.0/8") + .build(); ipFilter = new IPFilter(settings, auditTrailService, clusterSettings, licenseState); ipFilter.setBoundHttpTransportAddress(httpTransport.boundAddress()); ipFilter.setBoundTransportAddress(transport.boundAddress(), transport.profileBoundAddresses()); @@ -234,8 +240,9 @@ public void testThatBoundAddressIsNeverRejected() throws Exception { Settings settings; if (randomBoolean()) { - settings = Settings.builder().putList("xpack.security.transport.filter.deny", - addressStrings.toArray(new String[addressStrings.size()])).build(); + settings = Settings.builder() + .putList("xpack.security.transport.filter.deny", addressStrings.toArray(new String[addressStrings.size()])) + .build(); } else { settings = Settings.builder().put("xpack.security.transport.filter.deny", "_all").build(); } @@ -250,9 +257,7 @@ public void testThatBoundAddressIsNeverRejected() throws Exception { } public void testThatAllAddressesAreAllowedWhenLicenseDisablesSecurity() { - Settings settings = Settings.builder() - .put("xpack.security.transport.filter.deny", "_all") - .build(); + Settings settings = Settings.builder().put("xpack.security.transport.filter.deny", "_all").build(); when(licenseState.isAllowed(Security.IP_FILTERING_FEATURE)).thenReturn(false); ipFilter = new IPFilter(settings, auditTrailService, clusterSettings, licenseState); ipFilter.setBoundTransportAddress(transport.boundAddress(), transport.profileBoundAddresses()); @@ -274,16 +279,16 @@ public void testThatAllAddressesAreAllowedWhenLicenseDisablesSecurity() { @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/62298") public void testThatNodeStartsWithIPFilterDisabled() throws Exception { Settings settings = Settings.builder() - .put("path.home", createTempDir()) - .put("xpack.security.transport.filter.enabled", randomBoolean()) - .put("xpack.security.http.filter.enabled", randomBoolean()) - .build(); + .put("path.home", createTempDir()) + .put("xpack.security.transport.filter.enabled", randomBoolean()) + .put("xpack.security.http.filter.enabled", randomBoolean()) + .build(); try (Node node = new MockNode(settings, Arrays.asList(LocalStateSecurity.class))) { assertNotNull(node); } } - private void assertAddressIsAllowedForProfile(String profile, String ... inetAddresses) { + private void assertAddressIsAllowedForProfile(String profile, String... inetAddresses) { for (String inetAddress : inetAddresses) { String message = String.format(Locale.ROOT, "Expected address %s to be allowed", inetAddress); InetAddress address = InetAddresses.forString(inetAddress); @@ -294,11 +299,11 @@ private void assertAddressIsAllowedForProfile(String profile, String ... inetAdd } } - private void assertAddressIsAllowed(String ... inetAddresses) { + private void assertAddressIsAllowed(String... inetAddresses) { assertAddressIsAllowedForProfile("default", inetAddresses); } - private void assertAddressIsDeniedForProfile(String profile, String ... inetAddresses) { + private void assertAddressIsDeniedForProfile(String profile, String... inetAddresses) { for (String inetAddress : inetAddresses) { String message = String.format(Locale.ROOT, "Expected address %s to be denied", inetAddress); InetAddress address = InetAddresses.forString(inetAddress); @@ -309,7 +314,7 @@ private void assertAddressIsDeniedForProfile(String profile, String ... inetAddr } } - private void assertAddressIsDenied(String ... inetAddresses) { + private void assertAddressIsDenied(String... inetAddresses) { assertAddressIsDeniedForProfile("default", inetAddresses); } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/filter/PatternRuleTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/filter/PatternRuleTests.java index bbe73d1ceaddf..acf550d9e12d7 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/filter/PatternRuleTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/filter/PatternRuleTests.java @@ -7,6 +7,7 @@ package org.elasticsearch.xpack.security.transport.filter; import io.netty.handler.ipfilter.IpFilterRuleType; + import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.test.ESTestCase; diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/filter/SecurityIpFilterRuleTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/filter/SecurityIpFilterRuleTests.java index 9510e4337c66e..b426916c86b65 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/filter/SecurityIpFilterRuleTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/filter/SecurityIpFilterRuleTests.java @@ -9,8 +9,9 @@ import io.netty.handler.ipfilter.IpFilterRule; import io.netty.handler.ipfilter.IpFilterRuleType; import io.netty.handler.ipfilter.IpSubnetFilterRule; -import org.elasticsearch.core.Tuple; + import org.elasticsearch.common.network.NetworkAddress; +import org.elasticsearch.core.Tuple; import org.elasticsearch.test.ESTestCase; import java.net.InetAddress; @@ -63,7 +64,7 @@ public void testParseIpSubnetFilterRuleWithOtherValues() throws Exception { getRule(randomBoolean(), "127.0.0.0/24," + randomFrom("name", "127.0.0.1", "192.0.0.0/24")); fail("expected an exception to be thrown because only one subnet can be specified at a time"); } catch (IllegalArgumentException e) { - //expected + // expected } } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/netty4/IpFilterRemoteAddressFilterTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/netty4/IpFilterRemoteAddressFilterTests.java index 7ac3ff3cf270e..1acb3da7c9f00 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/netty4/IpFilterRemoteAddressFilterTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/netty4/IpFilterRemoteAddressFilterTests.java @@ -7,6 +7,7 @@ package org.elasticsearch.xpack.security.transport.netty4; import io.netty.channel.ChannelHandlerContext; + import org.elasticsearch.common.component.Lifecycle; import org.elasticsearch.common.network.InetAddresses; import org.elasticsearch.common.settings.ClusterSettings; @@ -39,9 +40,9 @@ public class IpFilterRemoteAddressFilterTests extends ESTestCase { @Before public void init() throws Exception { Settings settings = Settings.builder() - .put("xpack.security.transport.filter.allow", "127.0.0.1") - .put("xpack.security.transport.filter.deny", "10.0.0.0/8") - .build(); + .put("xpack.security.transport.filter.allow", "127.0.0.1") + .put("xpack.security.transport.filter.deny", "10.0.0.0/8") + .build(); boolean isHttpEnabled = randomBoolean(); @@ -49,15 +50,21 @@ public void init() throws Exception { TransportAddress address = new TransportAddress(InetAddress.getLoopbackAddress(), 9300); when(transport.boundAddress()).thenReturn(new BoundTransportAddress(new TransportAddress[] { address }, address)); when(transport.lifecycleState()).thenReturn(Lifecycle.State.STARTED); - ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, new HashSet<>(Arrays.asList( - IPFilter.HTTP_FILTER_ALLOW_SETTING, - IPFilter.HTTP_FILTER_DENY_SETTING, - IPFilter.IP_FILTER_ENABLED_HTTP_SETTING, - IPFilter.IP_FILTER_ENABLED_SETTING, - IPFilter.TRANSPORT_FILTER_ALLOW_SETTING, - IPFilter.TRANSPORT_FILTER_DENY_SETTING, - IPFilter.PROFILE_FILTER_ALLOW_SETTING, - IPFilter.PROFILE_FILTER_DENY_SETTING))); + ClusterSettings clusterSettings = new ClusterSettings( + Settings.EMPTY, + new HashSet<>( + Arrays.asList( + IPFilter.HTTP_FILTER_ALLOW_SETTING, + IPFilter.HTTP_FILTER_DENY_SETTING, + IPFilter.IP_FILTER_ENABLED_HTTP_SETTING, + IPFilter.IP_FILTER_ENABLED_SETTING, + IPFilter.TRANSPORT_FILTER_ALLOW_SETTING, + IPFilter.TRANSPORT_FILTER_DENY_SETTING, + IPFilter.PROFILE_FILTER_ALLOW_SETTING, + IPFilter.PROFILE_FILTER_DENY_SETTING + ) + ) + ); MockLicenseState licenseState = TestUtils.newMockLicenceState(); when(licenseState.isAllowed(Security.IP_FILTERING_FEATURE)).thenReturn(true); AuditTrailService auditTrailService = new AuditTrailService(Collections.emptyList(), licenseState); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/netty4/SecurityNetty4HttpServerTransportTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/netty4/SecurityNetty4HttpServerTransportTests.java index 8d66c91e82fab..7fe9c51e2f56e 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/netty4/SecurityNetty4HttpServerTransportTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/netty4/SecurityNetty4HttpServerTransportTests.java @@ -29,6 +29,7 @@ import java.nio.file.Path; import java.util.Collections; + import javax.net.ssl.SSLEngine; import static org.hamcrest.Matchers.arrayContaining; @@ -43,6 +44,7 @@ public class SecurityNetty4HttpServerTransportTests extends AbstractHttpServerTr private Environment env; private Path testnodeCert; private Path testnodeKey; + @Before public void createSSLService() { testnodeCert = getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt"); @@ -62,15 +64,19 @@ public void createSSLService() { } public void testDefaultClientAuth() throws Exception { - Settings settings = Settings.builder() - .put(env.settings()) - .put(XPackSettings.HTTP_SSL_ENABLED.getKey(), true).build(); + Settings settings = Settings.builder().put(env.settings()).put(XPackSettings.HTTP_SSL_ENABLED.getKey(), true).build(); sslService = new SSLService(TestEnvironment.newEnvironment(settings)); - SecurityNetty4HttpServerTransport transport = new SecurityNetty4HttpServerTransport(settings, - new NetworkService(Collections.emptyList()), mock(BigArrays.class), mock(IPFilter.class), sslService, - mock(ThreadPool.class), xContentRegistry(), new NullDispatcher(), - randomClusterSettings(), - new SharedGroupFactory(settings) + SecurityNetty4HttpServerTransport transport = new SecurityNetty4HttpServerTransport( + settings, + new NetworkService(Collections.emptyList()), + mock(BigArrays.class), + mock(IPFilter.class), + sslService, + mock(ThreadPool.class), + xContentRegistry(), + new NullDispatcher(), + randomClusterSettings(), + new SharedGroupFactory(settings) ); ChannelHandler handler = transport.configureServerChannelHandler(); final EmbeddedChannel ch = new EmbeddedChannel(handler); @@ -81,14 +87,23 @@ public void testDefaultClientAuth() throws Exception { public void testOptionalClientAuth() throws Exception { String value = AbstractSimpleSecurityTransportTestCase.randomCapitalization(SslClientAuthenticationMode.OPTIONAL); Settings settings = Settings.builder() - .put(env.settings()) - .put(XPackSettings.HTTP_SSL_ENABLED.getKey(), true) - .put("xpack.security.http.ssl.client_authentication", value).build(); + .put(env.settings()) + .put(XPackSettings.HTTP_SSL_ENABLED.getKey(), true) + .put("xpack.security.http.ssl.client_authentication", value) + .build(); sslService = new SSLService(TestEnvironment.newEnvironment(settings)); - SecurityNetty4HttpServerTransport transport = new SecurityNetty4HttpServerTransport(settings, - new NetworkService(Collections.emptyList()), mock(BigArrays.class), mock(IPFilter.class), sslService, - mock(ThreadPool.class), xContentRegistry(), new NullDispatcher(), - randomClusterSettings(), new SharedGroupFactory(settings)); + SecurityNetty4HttpServerTransport transport = new SecurityNetty4HttpServerTransport( + settings, + new NetworkService(Collections.emptyList()), + mock(BigArrays.class), + mock(IPFilter.class), + sslService, + mock(ThreadPool.class), + xContentRegistry(), + new NullDispatcher(), + randomClusterSettings(), + new SharedGroupFactory(settings) + ); ChannelHandler handler = transport.configureServerChannelHandler(); final EmbeddedChannel ch = new EmbeddedChannel(handler); assertThat(ch.pipeline().get(SslHandler.class).engine().getNeedClientAuth(), is(false)); @@ -98,14 +113,23 @@ public void testOptionalClientAuth() throws Exception { public void testRequiredClientAuth() throws Exception { String value = AbstractSimpleSecurityTransportTestCase.randomCapitalization(SslClientAuthenticationMode.REQUIRED); Settings settings = Settings.builder() - .put(env.settings()) - .put(XPackSettings.HTTP_SSL_ENABLED.getKey(), true) - .put("xpack.security.http.ssl.client_authentication", value).build(); + .put(env.settings()) + .put(XPackSettings.HTTP_SSL_ENABLED.getKey(), true) + .put("xpack.security.http.ssl.client_authentication", value) + .build(); sslService = new SSLService(TestEnvironment.newEnvironment(settings)); - SecurityNetty4HttpServerTransport transport = new SecurityNetty4HttpServerTransport(settings, - new NetworkService(Collections.emptyList()), mock(BigArrays.class), mock(IPFilter.class), sslService, - mock(ThreadPool.class), xContentRegistry(), new NullDispatcher(), - randomClusterSettings(), new SharedGroupFactory(settings)); + SecurityNetty4HttpServerTransport transport = new SecurityNetty4HttpServerTransport( + settings, + new NetworkService(Collections.emptyList()), + mock(BigArrays.class), + mock(IPFilter.class), + sslService, + mock(ThreadPool.class), + xContentRegistry(), + new NullDispatcher(), + randomClusterSettings(), + new SharedGroupFactory(settings) + ); ChannelHandler handler = transport.configureServerChannelHandler(); final EmbeddedChannel ch = new EmbeddedChannel(handler); assertThat(ch.pipeline().get(SslHandler.class).engine().getNeedClientAuth(), is(true)); @@ -115,14 +139,23 @@ public void testRequiredClientAuth() throws Exception { public void testNoClientAuth() throws Exception { String value = AbstractSimpleSecurityTransportTestCase.randomCapitalization(SslClientAuthenticationMode.NONE); Settings settings = Settings.builder() - .put(env.settings()) - .put(XPackSettings.HTTP_SSL_ENABLED.getKey(), true) - .put("xpack.security.http.ssl.client_authentication", value).build(); + .put(env.settings()) + .put(XPackSettings.HTTP_SSL_ENABLED.getKey(), true) + .put("xpack.security.http.ssl.client_authentication", value) + .build(); sslService = new SSLService(TestEnvironment.newEnvironment(settings)); - SecurityNetty4HttpServerTransport transport = new SecurityNetty4HttpServerTransport(settings, - new NetworkService(Collections.emptyList()), mock(BigArrays.class), mock(IPFilter.class), sslService, - mock(ThreadPool.class), xContentRegistry(), new NullDispatcher(), - randomClusterSettings(), new SharedGroupFactory(settings)); + SecurityNetty4HttpServerTransport transport = new SecurityNetty4HttpServerTransport( + settings, + new NetworkService(Collections.emptyList()), + mock(BigArrays.class), + mock(IPFilter.class), + sslService, + mock(ThreadPool.class), + xContentRegistry(), + new NullDispatcher(), + randomClusterSettings(), + new SharedGroupFactory(settings) + ); ChannelHandler handler = transport.configureServerChannelHandler(); final EmbeddedChannel ch = new EmbeddedChannel(handler); assertThat(ch.pipeline().get(SslHandler.class).engine().getNeedClientAuth(), is(false)); @@ -130,27 +163,42 @@ public void testNoClientAuth() throws Exception { } public void testCustomSSLConfiguration() throws Exception { - Settings settings = Settings.builder() - .put(env.settings()) - .put(XPackSettings.HTTP_SSL_ENABLED.getKey(), true).build(); + Settings settings = Settings.builder().put(env.settings()).put(XPackSettings.HTTP_SSL_ENABLED.getKey(), true).build(); sslService = new SSLService(TestEnvironment.newEnvironment(settings)); - SecurityNetty4HttpServerTransport transport = new SecurityNetty4HttpServerTransport(settings, - new NetworkService(Collections.emptyList()), mock(BigArrays.class), mock(IPFilter.class), sslService, - mock(ThreadPool.class), xContentRegistry(), new NullDispatcher(), - randomClusterSettings(), new SharedGroupFactory(settings)); + SecurityNetty4HttpServerTransport transport = new SecurityNetty4HttpServerTransport( + settings, + new NetworkService(Collections.emptyList()), + mock(BigArrays.class), + mock(IPFilter.class), + sslService, + mock(ThreadPool.class), + xContentRegistry(), + new NullDispatcher(), + randomClusterSettings(), + new SharedGroupFactory(settings) + ); ChannelHandler handler = transport.configureServerChannelHandler(); EmbeddedChannel ch = new EmbeddedChannel(handler); SSLEngine defaultEngine = ch.pipeline().get(SslHandler.class).engine(); settings = Settings.builder() - .put(env.settings()) - .put(XPackSettings.HTTP_SSL_ENABLED.getKey(), true) - .put("xpack.security.http.ssl.supported_protocols", "TLSv1.2") - .build(); + .put(env.settings()) + .put(XPackSettings.HTTP_SSL_ENABLED.getKey(), true) + .put("xpack.security.http.ssl.supported_protocols", "TLSv1.2") + .build(); sslService = new SSLService(TestEnvironment.newEnvironment(settings)); - transport = new SecurityNetty4HttpServerTransport(settings, new NetworkService(Collections.emptyList()), - mock(BigArrays.class), mock(IPFilter.class), sslService, mock(ThreadPool.class), xContentRegistry(), new NullDispatcher(), - randomClusterSettings(), new SharedGroupFactory(settings)); + transport = new SecurityNetty4HttpServerTransport( + settings, + new NetworkService(Collections.emptyList()), + mock(BigArrays.class), + mock(IPFilter.class), + sslService, + mock(ThreadPool.class), + xContentRegistry(), + new NullDispatcher(), + randomClusterSettings(), + new SharedGroupFactory(settings) + ); handler = transport.configureServerChannelHandler(); ch = new EmbeddedChannel(handler); SSLEngine customEngine = ch.pipeline().get(SslHandler.class).engine(); @@ -170,10 +218,18 @@ public void testNoExceptionWhenConfiguredWithoutSslKeySSLDisabled() throws Excep .build(); env = TestEnvironment.newEnvironment(settings); sslService = new SSLService(env); - SecurityNetty4HttpServerTransport transport = new SecurityNetty4HttpServerTransport(settings, - new NetworkService(Collections.emptyList()), mock(BigArrays.class), mock(IPFilter.class), sslService, - mock(ThreadPool.class), xContentRegistry(), new NullDispatcher(), - randomClusterSettings(), new SharedGroupFactory(settings)); + SecurityNetty4HttpServerTransport transport = new SecurityNetty4HttpServerTransport( + settings, + new NetworkService(Collections.emptyList()), + mock(BigArrays.class), + mock(IPFilter.class), + sslService, + mock(ThreadPool.class), + xContentRegistry(), + new NullDispatcher(), + randomClusterSettings(), + new SharedGroupFactory(settings) + ); assertNotNull(transport.configureServerChannelHandler()); } } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/netty4/SimpleSecurityNetty4ServerTransportTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/netty4/SimpleSecurityNetty4ServerTransportTests.java index d228c0ace6973..717546bf9ea94 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/netty4/SimpleSecurityNetty4ServerTransportTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/netty4/SimpleSecurityNetty4ServerTransportTests.java @@ -16,16 +16,16 @@ import org.elasticsearch.common.util.PageCacheRecycler; import org.elasticsearch.indices.breaker.NoneCircuitBreakerService; import org.elasticsearch.transport.ConnectionProfile; -import org.elasticsearch.transport.netty4.SharedGroupFactory; import org.elasticsearch.transport.TcpChannel; import org.elasticsearch.transport.Transport; +import org.elasticsearch.transport.netty4.SharedGroupFactory; import org.elasticsearch.xpack.security.transport.AbstractSimpleSecurityTransportTestCase; import java.util.Collections; public class SimpleSecurityNetty4ServerTransportTests extends AbstractSimpleSecurityTransportTestCase { - @AwaitsFix( bugUrl = "https://github.com/elastic/elasticsearch/issues/67427") + @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/67427") @Override public void testThreadContext() { // This empty method is here just for the purpose of muting the @@ -36,16 +36,27 @@ public void testThreadContext() { protected Transport build(Settings settings, final Version version, ClusterSettings clusterSettings, boolean doHandshake) { NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(Collections.emptyList()); NetworkService networkService = new NetworkService(Collections.emptyList()); - Settings settings1 = Settings.builder() - .put(settings) - .put("xpack.security.transport.ssl.enabled", true).build(); - return new SecurityNetty4ServerTransport(settings1, version, threadPool, - networkService, PageCacheRecycler.NON_RECYCLING_INSTANCE, namedWriteableRegistry, - new NoneCircuitBreakerService(), null, createSSLService(settings1), new SharedGroupFactory(settings1)) { + Settings settings1 = Settings.builder().put(settings).put("xpack.security.transport.ssl.enabled", true).build(); + return new SecurityNetty4ServerTransport( + settings1, + version, + threadPool, + networkService, + PageCacheRecycler.NON_RECYCLING_INSTANCE, + namedWriteableRegistry, + new NoneCircuitBreakerService(), + null, + createSSLService(settings1), + new SharedGroupFactory(settings1) + ) { @Override - public void executeHandshake(DiscoveryNode node, TcpChannel channel, ConnectionProfile profile, - ActionListener listener) { + public void executeHandshake( + DiscoveryNode node, + TcpChannel channel, + ConnectionProfile profile, + ActionListener listener + ) { if (doHandshake) { super.executeHandshake(node, channel, profile, listener); } else { diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/nio/NioIPFilterTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/nio/NioIPFilterTests.java index 8c3c3f0647f0c..5436cc8cf729a 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/nio/NioIPFilterTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/nio/NioIPFilterTests.java @@ -52,15 +52,21 @@ public void init() throws Exception { TransportAddress address = new TransportAddress(InetAddress.getLoopbackAddress(), 9300); when(transport.boundAddress()).thenReturn(new BoundTransportAddress(new TransportAddress[] { address }, address)); when(transport.lifecycleState()).thenReturn(Lifecycle.State.STARTED); - ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, new HashSet<>(Arrays.asList( - IPFilter.HTTP_FILTER_ALLOW_SETTING, - IPFilter.HTTP_FILTER_DENY_SETTING, - IPFilter.IP_FILTER_ENABLED_HTTP_SETTING, - IPFilter.IP_FILTER_ENABLED_SETTING, - IPFilter.TRANSPORT_FILTER_ALLOW_SETTING, - IPFilter.TRANSPORT_FILTER_DENY_SETTING, - IPFilter.PROFILE_FILTER_ALLOW_SETTING, - IPFilter.PROFILE_FILTER_DENY_SETTING))); + ClusterSettings clusterSettings = new ClusterSettings( + Settings.EMPTY, + new HashSet<>( + Arrays.asList( + IPFilter.HTTP_FILTER_ALLOW_SETTING, + IPFilter.HTTP_FILTER_DENY_SETTING, + IPFilter.IP_FILTER_ENABLED_HTTP_SETTING, + IPFilter.IP_FILTER_ENABLED_SETTING, + IPFilter.TRANSPORT_FILTER_ALLOW_SETTING, + IPFilter.TRANSPORT_FILTER_DENY_SETTING, + IPFilter.PROFILE_FILTER_ALLOW_SETTING, + IPFilter.PROFILE_FILTER_DENY_SETTING + ) + ) + ); MockLicenseState licenseState = TestUtils.newMockLicenceState(); when(licenseState.isAllowed(Security.IP_FILTERING_FEATURE)).thenReturn(true); AuditTrailService auditTrailService = new AuditTrailService(Collections.emptyList(), licenseState); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/nio/SSLChannelContextTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/nio/SSLChannelContextTests.java index 4a96f532ee68b..724f9c50c4c6f 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/nio/SSLChannelContextTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/nio/SSLChannelContextTests.java @@ -31,6 +31,7 @@ import java.nio.channels.SocketChannel; import java.util.function.BiConsumer; import java.util.function.Consumer; + import javax.net.ssl.SSLException; import static org.mockito.ArgumentMatchers.any; @@ -77,8 +78,18 @@ public void init() { outboundBuffer = new SSLOutboundBuffer((n) -> new Page(ByteBuffer.allocate(n), () -> {})); when(channel.getRawChannel()).thenReturn(rawChannel); exceptionHandler = mock(Consumer.class); - socketConfig = new Config.Socket(randomBoolean(), randomBoolean(), -1, -1, -1, randomBoolean(), -1, -1, - mock(InetSocketAddress.class), false); + socketConfig = new Config.Socket( + randomBoolean(), + randomBoolean(), + -1, + -1, + -1, + randomBoolean(), + -1, + -1, + mock(InetSocketAddress.class), + false + ); context = new SSLChannelContext(channel, selector, socketConfig, exceptionHandler, sslDriver, readWriteHandler, channelBuffer); context.setSelectionKey(mock(SelectionKey.class)); @@ -140,7 +151,6 @@ public void testPartialRead() throws IOException { }); doAnswer(getReadAnswerForBytes(bytes)).when(sslDriver).read(any(InboundChannelBuffer.class), eq(channelBuffer)); - when(readConsumer.apply(channelBuffer)).thenReturn(0); assertEquals(messageLength, context.read()); @@ -229,7 +239,7 @@ public void testFirstFlushMustFinishForWriteToContinue() throws Exception { } public void testQueuedWriteIsFlushedInFlushCall() throws Exception { - ByteBuffer[] buffers = {ByteBuffer.allocate(10)}; + ByteBuffer[] buffers = { ByteBuffer.allocate(10) }; FlushReadyWrite flushOperation = new FlushReadyWrite(context, buffers, listener); context.queueWriteOperation(flushOperation); @@ -245,7 +255,7 @@ public void testQueuedWriteIsFlushedInFlushCall() throws Exception { } public void testPartialFlush() throws IOException { - ByteBuffer[] buffers = {ByteBuffer.allocate(5)}; + ByteBuffer[] buffers = { ByteBuffer.allocate(5) }; FlushReadyWrite flushOperation = new FlushReadyWrite(context, buffers, listener); context.queueWriteOperation(flushOperation); @@ -262,8 +272,8 @@ public void testPartialFlush() throws IOException { @SuppressWarnings("unchecked") public void testMultipleWritesPartialFlushes() throws IOException { BiConsumer listener2 = mock(BiConsumer.class); - ByteBuffer[] buffers1 = {ByteBuffer.allocate(10)}; - ByteBuffer[] buffers2 = {ByteBuffer.allocate(5)}; + ByteBuffer[] buffers1 = { ByteBuffer.allocate(10) }; + ByteBuffer[] buffers2 = { ByteBuffer.allocate(5) }; FlushReadyWrite flushOperation1 = new FlushReadyWrite(context, buffers1, listener); FlushReadyWrite flushOperation2 = new FlushReadyWrite(context, buffers2, listener2); context.queueWriteOperation(flushOperation1); @@ -281,7 +291,7 @@ public void testMultipleWritesPartialFlushes() throws IOException { } public void testWhenIOExceptionThrownListenerIsCalled() throws IOException { - ByteBuffer[] buffers = {ByteBuffer.allocate(5)}; + ByteBuffer[] buffers = { ByteBuffer.allocate(5) }; FlushReadyWrite flushOperation = new FlushReadyWrite(context, buffers, listener); context.queueWriteOperation(flushOperation); @@ -375,8 +385,7 @@ public void testInitiateUnregisteredScheduledDirectClose() throws SSLException { @SuppressWarnings("unchecked") public void testActiveInitiatesDriver() throws IOException { - try (Selector realSelector = Selector.open(); - SocketChannel realSocket = SocketChannel.open()) { + try (Selector realSelector = Selector.open(); SocketChannel realSocket = SocketChannel.open()) { realSocket.configureBlocking(false); when(selector.rawSelector()).thenReturn(realSelector); when(channel.getRawChannel()).thenReturn(realSocket); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/nio/SSLDriverTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/nio/SSLDriverTests.java index 08b1cdc42b75d..3edf9fa793d5b 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/nio/SSLDriverTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/nio/SSLDriverTests.java @@ -24,6 +24,7 @@ import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.IntFunction; + import javax.net.ssl.KeyManager; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLEngine; @@ -50,13 +51,13 @@ public void testPingPongAndClose() throws Exception { handshake(clientDriver, serverDriver); - ByteBuffer[] buffers = {ByteBuffer.wrap("ping".getBytes(StandardCharsets.UTF_8))}; + ByteBuffer[] buffers = { ByteBuffer.wrap("ping".getBytes(StandardCharsets.UTF_8)) }; sendAppData(clientDriver, buffers); serverDriver.read(networkReadBuffer, applicationBuffer); assertEquals(ByteBuffer.wrap("ping".getBytes(StandardCharsets.UTF_8)), applicationBuffer.sliceBuffersTo(4)[0]); applicationBuffer.release(4); - ByteBuffer[] buffers2 = {ByteBuffer.wrap("pong".getBytes(StandardCharsets.UTF_8))}; + ByteBuffer[] buffers2 = { ByteBuffer.wrap("pong".getBytes(StandardCharsets.UTF_8)) }; sendAppData(serverDriver, buffers2); clientDriver.read(networkReadBuffer, applicationBuffer); assertEquals(ByteBuffer.wrap("pong".getBytes(StandardCharsets.UTF_8)), applicationBuffer.sliceBuffersTo(4)[0]); @@ -73,7 +74,7 @@ public void testDataStoredInOutboundBufferIsClosed() throws Exception { handshake(clientDriver, serverDriver); - ByteBuffer[] buffers = {ByteBuffer.wrap("ping".getBytes(StandardCharsets.UTF_8))}; + ByteBuffer[] buffers = { ByteBuffer.wrap("ping".getBytes(StandardCharsets.UTF_8)) }; serverDriver.write(new FlushOperation(buffers, (v, e) -> {})); expectThrows(SSLException.class, serverDriver::close); @@ -81,24 +82,23 @@ public void testDataStoredInOutboundBufferIsClosed() throws Exception { } public void testRenegotiate() throws Exception { - assumeFalse("BCTLS doesn't support renegotiation: https://github.com/bcgit/bc-java/issues/593#issuecomment-533518845", - inFipsJvm()); + assumeFalse("BCTLS doesn't support renegotiation: https://github.com/bcgit/bc-java/issues/593#issuecomment-533518845", inFipsJvm()); SSLContext sslContext = getSSLContext(); SSLEngine serverEngine = sslContext.createSSLEngine(); SSLEngine clientEngine = sslContext.createSSLEngine(); // Lock the protocol to 1.2 as 1.3 does not support renegotiation - String[] serverProtocols = {"TLSv1.2"}; + String[] serverProtocols = { "TLSv1.2" }; serverEngine.setEnabledProtocols(serverProtocols); - String[] clientProtocols = {"TLSv1.2"}; + String[] clientProtocols = { "TLSv1.2" }; clientEngine.setEnabledProtocols(clientProtocols); SSLDriver clientDriver = getDriver(clientEngine, true); SSLDriver serverDriver = getDriver(serverEngine, false); handshake(clientDriver, serverDriver); - ByteBuffer[] buffers = {ByteBuffer.wrap("ping".getBytes(StandardCharsets.UTF_8))}; + ByteBuffer[] buffers = { ByteBuffer.wrap("ping".getBytes(StandardCharsets.UTF_8)) }; sendAppData(clientDriver, buffers); serverDriver.read(networkReadBuffer, applicationBuffer); assertEquals(ByteBuffer.wrap("ping".getBytes(StandardCharsets.UTF_8)), applicationBuffer.sliceBuffersTo(4)[0]); @@ -108,7 +108,7 @@ public void testRenegotiate() throws Exception { assertFalse(clientDriver.readyForApplicationData()); // This tests that the client driver can still receive data based on the prior handshake - ByteBuffer[] buffers2 = {ByteBuffer.wrap("pong".getBytes(StandardCharsets.UTF_8))}; + ByteBuffer[] buffers2 = { ByteBuffer.wrap("pong".getBytes(StandardCharsets.UTF_8)) }; sendAppData(serverDriver, buffers2); clientDriver.read(networkReadBuffer, applicationBuffer); assertEquals(ByteBuffer.wrap("pong".getBytes(StandardCharsets.UTF_8)), applicationBuffer.sliceBuffersTo(4)[0]); @@ -141,7 +141,7 @@ public void testBigApplicationData() throws Exception { buffer.put((byte) (i % 127)); } buffer.flip(); - ByteBuffer[] buffers = {buffer}; + ByteBuffer[] buffers = { buffer }; sendAppData(clientDriver, buffers); serverDriver.read(networkReadBuffer, applicationBuffer); ByteBuffer[] buffers1 = applicationBuffer.sliceBuffersFrom(0); @@ -149,7 +149,7 @@ public void testBigApplicationData() throws Exception { assertEquals((byte) (32767 % 127), buffers1[1].get(16383)); applicationBuffer.release(1 << 15); - ByteBuffer[] buffers2 = {ByteBuffer.wrap("pong".getBytes(StandardCharsets.UTF_8))}; + ByteBuffer[] buffers2 = { ByteBuffer.wrap("pong".getBytes(StandardCharsets.UTF_8)) }; sendAppData(serverDriver, buffers2); clientDriver.read(networkReadBuffer, applicationBuffer); assertEquals(ByteBuffer.wrap("pong".getBytes(StandardCharsets.UTF_8)), applicationBuffer.sliceBuffersTo(4)[0]); @@ -169,20 +169,21 @@ public void testHandshakeFailureBecauseProtocolMismatch() throws Exception { if (inFipsJvm()) { // fips JSSE does not support TLSv1.3 yet - serverProtocols = new String[]{"TLSv1.2"}; - clientProtocols = new String[]{"TLSv1.1"}; + serverProtocols = new String[] { "TLSv1.2" }; + clientProtocols = new String[] { "TLSv1.1" }; expectedMessageMatcher = is("org.bouncycastle.tls.TlsFatalAlert: protocol_version(70)"); } else if (JavaVersion.current().compareTo(JavaVersion.parse("16")) >= 0) { // JDK16 https://jdk.java.net/16/release-notes does not permit protocol TLSv1.1 OOB - serverProtocols = new String[]{"TLSv1.3"}; - clientProtocols = new String[]{"TLSv1.2"}; + serverProtocols = new String[] { "TLSv1.3" }; + clientProtocols = new String[] { "TLSv1.2" }; expectedMessageMatcher = is("The client supported protocol versions [TLSv1.2] are not accepted by server preferences [TLS13]"); } else { - serverProtocols = new String[]{"TLSv1.2"}; - clientProtocols = new String[]{"TLSv1.1"}; + serverProtocols = new String[] { "TLSv1.2" }; + clientProtocols = new String[] { "TLSv1.1" }; expectedMessageMatcher = anyOf( is("No appropriate protocol (protocol is disabled or cipher suites are inappropriate)"), - is("The client supported protocol versions [TLSv1.1] are not accepted by server preferences [TLS12]")); + is("The client supported protocol versions [TLSv1.1] are not accepted by server preferences [TLS12]") + ); } serverEngine.setEnabledProtocols(serverProtocols); @@ -203,8 +204,11 @@ public void testHandshakeFailureBecauseProtocolMismatch() throws Exception { clientDriver.close(); assertTrue(clientDriver.isClosed()); } else { - failedCloseAlert(serverDriver, clientDriver, Arrays.asList("Received fatal alert: protocol_version", - "Received fatal alert: handshake_failure")); + failedCloseAlert( + serverDriver, + clientDriver, + Arrays.asList("Received fatal alert: protocol_version", "Received fatal alert: handshake_failure") + ); } } } @@ -226,15 +230,16 @@ public void testHandshakeFailureBecauseNoCiphers() throws Exception { // Prior to JDK11 we still need to send a close alert if (serverDriver.isClosed() == false) { - List messages = Arrays.asList("Received fatal alert: handshake_failure", - "Received close_notify during handshake"); + List messages = Arrays.asList("Received fatal alert: handshake_failure", "Received close_notify during handshake"); failedCloseAlert(serverDriver, clientDriver, messages); } } public void testCloseDuringHandshakeJDK11() throws Exception { - assumeTrue("this tests ssl engine for JDK11", - JavaVersion.current().compareTo(JavaVersion.parse("11")) >= 0 && inFipsJvm() == false); + assumeTrue( + "this tests ssl engine for JDK11", + JavaVersion.current().compareTo(JavaVersion.parse("11")) >= 0 && inFipsJvm() == false + ); SSLContext sslContext = getSSLContext(); SSLDriver clientDriver = getDriver(sslContext.createSSLEngine(), true); SSLDriver serverDriver = getDriver(sslContext.createSSLEngine(), false); @@ -313,8 +318,10 @@ private void failedCloseAlert(SSLDriver sendDriver, SSLDriver receiveDriver, Lis sendDriver.close(); SSLException sslException = expectThrows(SSLException.class, () -> receiveDriver.read(networkReadBuffer, applicationBuffer)); - assertTrue("Expected one of the following exception messages: " + messages + ". Found: " + sslException.getMessage(), - messages.stream().anyMatch(m -> sslException.getMessage().equals(m))); + assertTrue( + "Expected one of the following exception messages: " + messages + ". Found: " + sslException.getMessage(), + messages.stream().anyMatch(m -> sslException.getMessage().equals(m)) + ); assertTrue(receiveDriver.isClosed()); receiveDriver.close(); } @@ -326,7 +333,7 @@ private SSLContext getSSLContext() throws Exception { TrustManager tm = CertParsingUtils.getTrustManagerFromPEM(List.of(certPath)); KeyManager km = CertParsingUtils.getKeyManagerFromPEM(certPath, keyPath, "testclient".toCharArray()); sslContext = SSLContext.getInstance(inFipsJvm() ? "TLSv1.2" : randomFrom("TLSv1.2", "TLSv1.3")); - sslContext.init(new KeyManager[]{km}, new TrustManager[]{tm}, new SecureRandom()); + sslContext.init(new KeyManager[] { km }, new TrustManager[] { tm }, new SecureRandom()); return sslContext; } @@ -397,7 +404,6 @@ private void handshake(SSLDriver clientDriver, SSLDriver serverDriver, boolean i assertTrue(serverDriver.readyForApplicationData()); } - } private void sendHandshakeMessages(SSLDriver sendDriver, SSLDriver receiveDriver) throws IOException { diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/nio/SecurityNioHttpServerTransportTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/nio/SecurityNioHttpServerTransportTests.java index fd82652af9f5b..bf6e272c6e949 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/nio/SecurityNioHttpServerTransportTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/nio/SecurityNioHttpServerTransportTests.java @@ -34,6 +34,7 @@ import java.nio.channels.SocketChannel; import java.nio.file.Path; import java.util.Collections; + import javax.net.ssl.SSLEngine; import static org.hamcrest.Matchers.arrayContaining; @@ -68,15 +69,22 @@ public void createSSLService() { } public void testDefaultClientAuth() throws IOException { - Settings settings = Settings.builder() - .put(env.settings()) - .put(XPackSettings.HTTP_SSL_ENABLED.getKey(), true).build(); + Settings settings = Settings.builder().put(env.settings()).put(XPackSettings.HTTP_SSL_ENABLED.getKey(), true).build(); nioGroupFactory = new NioGroupFactory(settings, logger); sslService = new SSLService(TestEnvironment.newEnvironment(settings)); - SecurityNioHttpServerTransport transport = new SecurityNioHttpServerTransport(settings, - new NetworkService(Collections.emptyList()), mock(BigArrays.class), mock(PageCacheRecycler.class), mock(ThreadPool.class), - xContentRegistry(), new NullDispatcher(), mock(IPFilter.class), sslService, nioGroupFactory, - randomClusterSettings()); + SecurityNioHttpServerTransport transport = new SecurityNioHttpServerTransport( + settings, + new NetworkService(Collections.emptyList()), + mock(BigArrays.class), + mock(PageCacheRecycler.class), + mock(ThreadPool.class), + xContentRegistry(), + new NullDispatcher(), + mock(IPFilter.class), + sslService, + nioGroupFactory, + randomClusterSettings() + ); SecurityNioHttpServerTransport.SecurityHttpChannelFactory factory = transport.channelFactory(); SocketChannel socketChannel = mock(SocketChannel.class); when(socketChannel.getRemoteAddress()).thenReturn(address); @@ -92,13 +100,23 @@ public void testOptionalClientAuth() throws IOException { Settings settings = Settings.builder() .put(env.settings()) .put(XPackSettings.HTTP_SSL_ENABLED.getKey(), true) - .put("xpack.security.http.ssl.client_authentication", value).build(); + .put("xpack.security.http.ssl.client_authentication", value) + .build(); sslService = new SSLService(TestEnvironment.newEnvironment(settings)); nioGroupFactory = new NioGroupFactory(settings, logger); - SecurityNioHttpServerTransport transport = new SecurityNioHttpServerTransport(settings, - new NetworkService(Collections.emptyList()), mock(BigArrays.class), mock(PageCacheRecycler.class), mock(ThreadPool.class), - xContentRegistry(), new NullDispatcher(), mock(IPFilter.class), sslService, nioGroupFactory, - randomClusterSettings()); + SecurityNioHttpServerTransport transport = new SecurityNioHttpServerTransport( + settings, + new NetworkService(Collections.emptyList()), + mock(BigArrays.class), + mock(PageCacheRecycler.class), + mock(ThreadPool.class), + xContentRegistry(), + new NullDispatcher(), + mock(IPFilter.class), + sslService, + nioGroupFactory, + randomClusterSettings() + ); SecurityNioHttpServerTransport.SecurityHttpChannelFactory factory = transport.channelFactory(); SocketChannel socketChannel = mock(SocketChannel.class); @@ -114,13 +132,23 @@ public void testRequiredClientAuth() throws IOException { Settings settings = Settings.builder() .put(env.settings()) .put(XPackSettings.HTTP_SSL_ENABLED.getKey(), true) - .put("xpack.security.http.ssl.client_authentication", value).build(); + .put("xpack.security.http.ssl.client_authentication", value) + .build(); nioGroupFactory = new NioGroupFactory(settings, logger); sslService = new SSLService(TestEnvironment.newEnvironment(settings)); - SecurityNioHttpServerTransport transport = new SecurityNioHttpServerTransport(settings, - new NetworkService(Collections.emptyList()), mock(BigArrays.class), mock(PageCacheRecycler.class), mock(ThreadPool.class), - xContentRegistry(), new NullDispatcher(), mock(IPFilter.class), sslService, nioGroupFactory, - randomClusterSettings()); + SecurityNioHttpServerTransport transport = new SecurityNioHttpServerTransport( + settings, + new NetworkService(Collections.emptyList()), + mock(BigArrays.class), + mock(PageCacheRecycler.class), + mock(ThreadPool.class), + xContentRegistry(), + new NullDispatcher(), + mock(IPFilter.class), + sslService, + nioGroupFactory, + randomClusterSettings() + ); SecurityNioHttpServerTransport.SecurityHttpChannelFactory factory = transport.channelFactory(); SocketChannel socketChannel = mock(SocketChannel.class); @@ -136,13 +164,23 @@ public void testNoClientAuth() throws IOException { Settings settings = Settings.builder() .put(env.settings()) .put(XPackSettings.HTTP_SSL_ENABLED.getKey(), true) - .put("xpack.security.http.ssl.client_authentication", value).build(); + .put("xpack.security.http.ssl.client_authentication", value) + .build(); sslService = new SSLService(TestEnvironment.newEnvironment(settings)); nioGroupFactory = new NioGroupFactory(settings, logger); - SecurityNioHttpServerTransport transport = new SecurityNioHttpServerTransport(settings, - new NetworkService(Collections.emptyList()), mock(BigArrays.class), mock(PageCacheRecycler.class), mock(ThreadPool.class), - xContentRegistry(), new NullDispatcher(), mock(IPFilter.class), sslService, nioGroupFactory, - randomClusterSettings()); + SecurityNioHttpServerTransport transport = new SecurityNioHttpServerTransport( + settings, + new NetworkService(Collections.emptyList()), + mock(BigArrays.class), + mock(PageCacheRecycler.class), + mock(ThreadPool.class), + xContentRegistry(), + new NullDispatcher(), + mock(IPFilter.class), + sslService, + nioGroupFactory, + randomClusterSettings() + ); SecurityNioHttpServerTransport.SecurityHttpChannelFactory factory = transport.channelFactory(); SocketChannel socketChannel = mock(SocketChannel.class); @@ -154,15 +192,22 @@ public void testNoClientAuth() throws IOException { } public void testCustomSSLConfiguration() throws IOException { - Settings settings = Settings.builder() - .put(env.settings()) - .put(XPackSettings.HTTP_SSL_ENABLED.getKey(), true).build(); + Settings settings = Settings.builder().put(env.settings()).put(XPackSettings.HTTP_SSL_ENABLED.getKey(), true).build(); sslService = new SSLService(TestEnvironment.newEnvironment(settings)); nioGroupFactory = new NioGroupFactory(settings, logger); - SecurityNioHttpServerTransport transport = new SecurityNioHttpServerTransport(settings, - new NetworkService(Collections.emptyList()), mock(BigArrays.class), mock(PageCacheRecycler.class), mock(ThreadPool.class), - xContentRegistry(), new NullDispatcher(), mock(IPFilter.class), sslService, nioGroupFactory, - randomClusterSettings()); + SecurityNioHttpServerTransport transport = new SecurityNioHttpServerTransport( + settings, + new NetworkService(Collections.emptyList()), + mock(BigArrays.class), + mock(PageCacheRecycler.class), + mock(ThreadPool.class), + xContentRegistry(), + new NullDispatcher(), + mock(IPFilter.class), + sslService, + nioGroupFactory, + randomClusterSettings() + ); SecurityNioHttpServerTransport.SecurityHttpChannelFactory factory = transport.channelFactory(); SocketChannel socketChannel = mock(SocketChannel.class); when(socketChannel.getRemoteAddress()).thenReturn(address); @@ -176,10 +221,19 @@ public void testCustomSSLConfiguration() throws IOException { .build(); sslService = new SSLService(TestEnvironment.newEnvironment(settings)); nioGroupFactory = new NioGroupFactory(settings, logger); - transport = new SecurityNioHttpServerTransport(settings, - new NetworkService(Collections.emptyList()), mock(BigArrays.class), mock(PageCacheRecycler.class), mock(ThreadPool.class), - xContentRegistry(), new NullDispatcher(), mock(IPFilter.class), sslService, nioGroupFactory, - randomClusterSettings()); + transport = new SecurityNioHttpServerTransport( + settings, + new NetworkService(Collections.emptyList()), + mock(BigArrays.class), + mock(PageCacheRecycler.class), + mock(ThreadPool.class), + xContentRegistry(), + new NullDispatcher(), + mock(IPFilter.class), + sslService, + nioGroupFactory, + randomClusterSettings() + ); factory = transport.channelFactory(); channel = factory.createChannel(mock(NioSelector.class), socketChannel, mock(Config.Socket.class)); SSLEngine customEngine = SSLEngineUtils.getSSLEngine(channel); @@ -192,17 +246,28 @@ public void testNoExceptionWhenConfiguredWithoutSslKeySSLDisabled() { secureSettings.setString("xpack.security.http.ssl.truststore.secure_password", "testnode"); Settings settings = Settings.builder() .put("xpack.security.http.ssl.enabled", false) - .put("xpack.security.http.ssl.truststore.path", - getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.jks")) + .put( + "xpack.security.http.ssl.truststore.path", + getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.jks") + ) .setSecureSettings(secureSettings) .put("path.home", createTempDir()) .build(); env = TestEnvironment.newEnvironment(settings); sslService = new SSLService(env); nioGroupFactory = new NioGroupFactory(settings, logger); - SecurityNioHttpServerTransport transport = new SecurityNioHttpServerTransport(settings, - new NetworkService(Collections.emptyList()), mock(BigArrays.class), mock(PageCacheRecycler.class), mock(ThreadPool.class), - xContentRegistry(), new NullDispatcher(), mock(IPFilter.class), sslService, nioGroupFactory, - randomClusterSettings()); + SecurityNioHttpServerTransport transport = new SecurityNioHttpServerTransport( + settings, + new NetworkService(Collections.emptyList()), + mock(BigArrays.class), + mock(PageCacheRecycler.class), + mock(ThreadPool.class), + xContentRegistry(), + new NullDispatcher(), + mock(IPFilter.class), + sslService, + nioGroupFactory, + randomClusterSettings() + ); } } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/nio/SimpleSecurityNioTransportTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/nio/SimpleSecurityNioTransportTests.java index 1dd92af1f3b37..d07755dfb4199 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/nio/SimpleSecurityNioTransportTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/nio/SimpleSecurityNioTransportTests.java @@ -29,16 +29,27 @@ public class SimpleSecurityNioTransportTests extends AbstractSimpleSecurityTrans protected Transport build(Settings settings, final Version version, ClusterSettings clusterSettings, boolean doHandshake) { NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(Collections.emptyList()); NetworkService networkService = new NetworkService(Collections.emptyList()); - Settings settings1 = Settings.builder() - .put(settings) - .put("xpack.security.transport.ssl.enabled", true).build(); - return new SecurityNioTransport(settings1, version, threadPool, networkService, new MockPageCacheRecycler(settings), - namedWriteableRegistry, new NoneCircuitBreakerService(), null, createSSLService(settings1), - new NioGroupFactory(settings, logger)) { + Settings settings1 = Settings.builder().put(settings).put("xpack.security.transport.ssl.enabled", true).build(); + return new SecurityNioTransport( + settings1, + version, + threadPool, + networkService, + new MockPageCacheRecycler(settings), + namedWriteableRegistry, + new NoneCircuitBreakerService(), + null, + createSSLService(settings1), + new NioGroupFactory(settings, logger) + ) { @Override - public void executeHandshake(DiscoveryNode node, TcpChannel channel, ConnectionProfile profile, - ActionListener listener) { + public void executeHandshake( + DiscoveryNode node, + TcpChannel channel, + ConnectionProfile profile, + ActionListener listener + ) { if (doHandshake) { super.executeHandshake(node, channel, profile, listener); } else { diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/user/AnonymousUserTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/user/AnonymousUserTests.java index 52037435ae111..66a07751e0523 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/user/AnonymousUserTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/user/AnonymousUserTests.java @@ -20,25 +20,23 @@ public class AnonymousUserTests extends ESTestCase { public void testResolveAnonymousUser() throws Exception { Settings settings = Settings.builder() - .put(AnonymousUser.USERNAME_SETTING.getKey(), "anonym1") - .putList(AnonymousUser.ROLES_SETTING.getKey(), "r1", "r2", "r3") - .build(); + .put(AnonymousUser.USERNAME_SETTING.getKey(), "anonym1") + .putList(AnonymousUser.ROLES_SETTING.getKey(), "r1", "r2", "r3") + .build(); AnonymousUser user = new AnonymousUser(settings); assertThat(user.principal(), equalTo("anonym1")); assertThat(user.roles(), arrayContainingInAnyOrder("r1", "r2", "r3")); - settings = Settings.builder() - .putList(AnonymousUser.ROLES_SETTING.getKey(), "r1", "r2", "r3") - .build(); + settings = Settings.builder().putList(AnonymousUser.ROLES_SETTING.getKey(), "r1", "r2", "r3").build(); user = new AnonymousUser(settings); assertThat(user.principal(), equalTo(AnonymousUser.DEFAULT_ANONYMOUS_USERNAME)); assertThat(user.roles(), arrayContainingInAnyOrder("r1", "r2", "r3")); } public void testResolveAnonymousUser_NoSettings() throws Exception { - Settings settings = randomBoolean() ? - Settings.EMPTY : - Settings.builder().put(AnonymousUser.USERNAME_SETTING.getKey(), "user1").build(); + Settings settings = randomBoolean() + ? Settings.EMPTY + : Settings.builder().put(AnonymousUser.USERNAME_SETTING.getKey(), "user1").build(); assertThat(AnonymousUser.isAnonymousEnabled(settings), is(false)); } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/user/UserSerializationTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/user/UserSerializationTests.java index ca8b0768a2730..e29c2cf0c380e 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/user/UserSerializationTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/user/UserSerializationTests.java @@ -27,8 +27,7 @@ public class UserSerializationTests extends ESTestCase { public void testWriteToAndReadFrom() throws Exception { - User user = new User(randomAlphaOfLengthBetween(4, 30), - generateRandomStringArray(20, 30, false)); + User user = new User(randomAlphaOfLengthBetween(4, 30), generateRandomStringArray(20, 30, false)); BytesStreamOutput output = new BytesStreamOutput(); User.writeTo(user, output); @@ -42,9 +41,11 @@ public void testWriteToAndReadFrom() throws Exception { public void testWriteToAndReadFromWithRunAs() throws Exception { User authUser = new User(randomAlphaOfLengthBetween(4, 30), generateRandomStringArray(20, 30, false)); - User user = new User(randomAlphaOfLengthBetween(4, 30), + User user = new User( + randomAlphaOfLengthBetween(4, 30), randomBoolean() ? generateRandomStringArray(20, 30, false) : null, - authUser); + authUser + ); BytesStreamOutput output = new BytesStreamOutput(); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/ssl/SSLErrorMessageCertificateVerificationTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/ssl/SSLErrorMessageCertificateVerificationTests.java index 21d6e93756a5d..cf216bb9082ee 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/ssl/SSLErrorMessageCertificateVerificationTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/ssl/SSLErrorMessageCertificateVerificationTests.java @@ -40,6 +40,7 @@ import java.nio.file.Path; import java.util.Locale; import java.util.regex.Pattern; + import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLHandshakeException; import javax.net.ssl.SSLParameters; @@ -57,13 +58,16 @@ public class SSLErrorMessageCertificateVerificationTests extends ESTestCase { private static final String HTTP_CLIENT_SSL = "xpack.http.ssl"; public void testMessageForHttpClientHostnameVerificationFailure() throws IOException, URISyntaxException { - final Settings sslSetup = getPemSSLSettings(HTTP_SERVER_SSL, "not-this-host.crt", "not-this-host.key", - SslClientAuthenticationMode.NONE, SslVerificationMode.FULL, null) - .putList("xpack.http.ssl.certificate_authorities", getPath("ca1.crt")) - .build(); + final Settings sslSetup = getPemSSLSettings( + HTTP_SERVER_SSL, + "not-this-host.crt", + "not-this-host.key", + SslClientAuthenticationMode.NONE, + SslVerificationMode.FULL, + null + ).putList("xpack.http.ssl.certificate_authorities", getPath("ca1.crt")).build(); final SSLService sslService = new SSLService(TestEnvironment.newEnvironment(buildEnvSettings(sslSetup))); - try (MockWebServer webServer = initWebServer(sslService); - CloseableHttpClient client = buildHttpClient(sslService)) { + try (MockWebServer webServer = initWebServer(sslService); CloseableHttpClient client = buildHttpClient(sslService)) { final HttpGet request = new HttpGet(webServer.getUri("/")); try (CloseableHttpResponse ignore = SocketAccess.doPrivileged(() -> client.execute(request))) { fail("Expected hostname verification exception"); @@ -77,8 +81,14 @@ public void testMessageForHttpClientHostnameVerificationFailure() throws IOExcep } public void testMessageForRestClientHostnameVerificationFailure() throws IOException, URISyntaxException { - final Settings sslSetup = getPemSSLSettings(HTTP_SERVER_SSL, "not-this-host.crt", "not-this-host.key", - SslClientAuthenticationMode.NONE, SslVerificationMode.FULL, null) + final Settings sslSetup = getPemSSLSettings( + HTTP_SERVER_SSL, + "not-this-host.crt", + "not-this-host.key", + SslClientAuthenticationMode.NONE, + SslVerificationMode.FULL, + null + ) // Client .putList("xpack.http.ssl.certificate_authorities", getPath("ca1.crt")) .build(); @@ -98,10 +108,14 @@ public void testMessageForRestClientHostnameVerificationFailure() throws IOExcep public void testDiagnosticTrustManagerForHostnameVerificationFailure() throws Exception { assumeFalse("https://github.com/elastic/elasticsearch/issues/49094", inFipsJvm()); - final Settings settings = getPemSSLSettings(HTTP_SERVER_SSL, "not-this-host.crt", "not-this-host.key", - SslClientAuthenticationMode.NONE, SslVerificationMode.FULL, null) - .putList("xpack.http.ssl.certificate_authorities", getPath("ca1.crt")) - .build(); + final Settings settings = getPemSSLSettings( + HTTP_SERVER_SSL, + "not-this-host.crt", + "not-this-host.key", + SslClientAuthenticationMode.NONE, + SslVerificationMode.FULL, + null + ).putList("xpack.http.ssl.certificate_authorities", getPath("ca1.crt")).build(); final SSLService sslService = new SSLService(TestEnvironment.newEnvironment(buildEnvSettings(settings))); final SslConfiguration clientSslConfig = sslService.getSSLConfiguration(HTTP_CLIENT_SSL); final SSLSocketFactory clientSocketFactory = sslService.sslSocketFactory(clientSslConfig); @@ -112,35 +126,40 @@ public void testDiagnosticTrustManagerForHostnameVerificationFailure() throws Ex // Apache clients implement their own hostname checking, but we don't want that. // We use a raw socket so we get the builtin JDK checking (which is what we use for transport protocol SSL checks) - try (MockWebServer webServer = initWebServer(sslService); - SSLSocket clientSocket = (SSLSocket) clientSocketFactory.createSocket()) { + try (MockWebServer webServer = initWebServer(sslService); SSLSocket clientSocket = (SSLSocket) clientSocketFactory.createSocket()) { Loggers.addAppender(diagnosticLogger, mockAppender); String fileName = "/x-pack/plugin/security/build/resources/test/org/elasticsearch/xpack/ssl/SSLErrorMessageTests/ca1.crt" .replace('/', platformFileSeparator()); - mockAppender.addExpectation(new MockLogAppender.PatternSeenEventExpectation( - "ssl diagnostic", - DiagnosticTrustManager.class.getName(), - Level.WARN, - "failed to establish trust with server at \\[" + Pattern.quote(webServer.getHostName()) + "\\];" + - " the server provided a certificate with subject name \\[CN=not-this-host\\]," + - " fingerprint \\[[0-9a-f]{40}\\], no keyUsage and no extendedKeyUsage;" + - " the session uses cipher suite \\[TLS_[A-Z0-9_]*\\] and protocol \\[TLSv[0-9.]*\\];" + - " the certificate has subject alternative names \\[DNS:not\\.this\\.host\\];" + - " the certificate is issued by \\[CN=Certificate Authority 1,OU=ssl-error-message-test,DC=elastic,DC=co\\]" + - " but the server did not provide a copy of the issuing certificate in the certificate chain;" + - " the issuing certificate with fingerprint \\[[0-9a-f]{40}\\]" + - " is trusted in this ssl context " + - Pattern.quote("([" + HTTP_CLIENT_SSL + " (with trust configuration: PEM-trust{") + - "\\S+" + - Pattern.quote(fileName + "})])") - )); + mockAppender.addExpectation( + new MockLogAppender.PatternSeenEventExpectation( + "ssl diagnostic", + DiagnosticTrustManager.class.getName(), + Level.WARN, + "failed to establish trust with server at \\[" + + Pattern.quote(webServer.getHostName()) + + "\\];" + + " the server provided a certificate with subject name \\[CN=not-this-host\\]," + + " fingerprint \\[[0-9a-f]{40}\\], no keyUsage and no extendedKeyUsage;" + + " the session uses cipher suite \\[TLS_[A-Z0-9_]*\\] and protocol \\[TLSv[0-9.]*\\];" + + " the certificate has subject alternative names \\[DNS:not\\.this\\.host\\];" + + " the certificate is issued by \\[CN=Certificate Authority 1,OU=ssl-error-message-test,DC=elastic,DC=co\\]" + + " but the server did not provide a copy of the issuing certificate in the certificate chain;" + + " the issuing certificate with fingerprint \\[[0-9a-f]{40}\\]" + + " is trusted in this ssl context " + + Pattern.quote("([" + HTTP_CLIENT_SSL + " (with trust configuration: PEM-trust{") + + "\\S+" + + Pattern.quote(fileName + "})])") + ) + ); enableHttpsHostnameChecking(clientSocket); connect(clientSocket, webServer); assertThat(clientSocket.isConnected(), is(true)); - final SSLHandshakeException handshakeException = expectThrows(SSLHandshakeException.class, - () -> clientSocket.getInputStream().read()); + final SSLHandshakeException handshakeException = expectThrows( + SSLHandshakeException.class, + () -> clientSocket.getInputStream().read() + ); assertThat(handshakeException, throwableWithMessage(containsStringIgnoringCase("subject alternative names"))); assertThat(handshakeException, throwableWithMessage(containsString(webServer.getHostName()))); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/ssl/SSLErrorMessageFileTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/ssl/SSLErrorMessageFileTests.java index 093ca0b237f52..d414dac19df4a 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/ssl/SSLErrorMessageFileTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/ssl/SSLErrorMessageFileTests.java @@ -144,9 +144,21 @@ public void testMessageForTransportSslEnabledWithoutKeys() throws Exception { } Throwable exception = expectFailure(settings); - assertThat(exception, throwableWithMessage("invalid SSL configuration for " + prefix + - " - server ssl configuration requires a key and certificate, but these have not been configured;" + - " you must set either [" + prefix + ".keystore.path], or both [" + prefix + ".key] and [" + prefix + ".certificate]")); + assertThat( + exception, + throwableWithMessage( + "invalid SSL configuration for " + + prefix + + " - server ssl configuration requires a key and certificate, but these have not been configured;" + + " you must set either [" + + prefix + + ".keystore.path], or both [" + + prefix + + ".key] and [" + + prefix + + ".certificate]" + ) + ); assertThat(exception, instanceOf(ElasticsearchException.class)); } @@ -165,29 +177,33 @@ public void testNoErrorIfTransportSslDisabledWithoutKeys() throws Exception { public void testMessageForTransportNotEnabledButKeystoreConfigured() throws Exception { assumeFalse("Cannot run in a FIPS JVM since it uses a PKCS12 keystore", inFipsJvm()); final String prefix = "xpack.security.transport.ssl"; - checkUnusedConfiguration(prefix, prefix + ".keystore.path," + prefix + ".keystore.secure_password", - this::configureWorkingKeystore); + checkUnusedConfiguration(prefix, prefix + ".keystore.path," + prefix + ".keystore.secure_password", this::configureWorkingKeystore); } public void testMessageForTransportNotEnabledButTruststoreConfigured() throws Exception { assumeFalse("Cannot run in a FIPS JVM since it uses a PKCS12 keystore", inFipsJvm()); final String prefix = "xpack.security.transport.ssl"; - checkUnusedConfiguration(prefix, prefix + ".truststore.path," + prefix + ".truststore.secure_password", - this::configureWorkingTruststore); + checkUnusedConfiguration( + prefix, + prefix + ".truststore.path," + prefix + ".truststore.secure_password", + this::configureWorkingTruststore + ); } public void testMessageForHttpsNotEnabledButKeystoreConfigured() throws Exception { assumeFalse("Cannot run in a FIPS JVM since it uses a PKCS12 keystore", inFipsJvm()); final String prefix = "xpack.security.http.ssl"; - checkUnusedConfiguration(prefix, prefix + ".keystore.path," + prefix + ".keystore.secure_password", - this::configureWorkingKeystore); + checkUnusedConfiguration(prefix, prefix + ".keystore.path," + prefix + ".keystore.secure_password", this::configureWorkingKeystore); } public void testMessageForHttpsNotEnabledButTruststoreConfigured() throws Exception { assumeFalse("Cannot run in a FIPS JVM since it uses a PKCS12 keystore", inFipsJvm()); final String prefix = "xpack.security.http.ssl"; - checkUnusedConfiguration(prefix, prefix + ".truststore.path," + prefix + ".truststore.secure_password", - this::configureWorkingTruststore); + checkUnusedConfiguration( + prefix, + prefix + ".truststore.path," + prefix + ".truststore.secure_password", + this::configureWorkingTruststore + ); } private void checkMissingKeyManagerResource(String fileType, String configKey, @Nullable Settings.Builder additionalSettings) { @@ -209,10 +225,18 @@ private void checkMissingTrustManagerResource(String fileType, String configKey) checkMissingResource(fileType, configKey, this::configureWorkingKeystore); } - private void checkUnreadableKeyManagerResource(String fromResource, String fileType, String configKey, - @Nullable Settings.Builder additionalSettings) throws Exception { - checkUnreadableResource(fromResource, fileType, configKey, - (prefix, builder) -> buildKeyConfigSettings(additionalSettings, prefix, builder)); + private void checkUnreadableKeyManagerResource( + String fromResource, + String fileType, + String configKey, + @Nullable Settings.Builder additionalSettings + ) throws Exception { + checkUnreadableResource( + fromResource, + fileType, + configKey, + (prefix, builder) -> buildKeyConfigSettings(additionalSettings, prefix, builder) + ); } private void checkUnreadableTrustManagerResource(String fromResource, String fileType, String configKey) throws Exception { @@ -220,16 +244,19 @@ private void checkUnreadableTrustManagerResource(String fromResource, String fil } private void checkBlockedKeyManagerResource(String fileType, String configKey, Settings.Builder additionalSettings) throws Exception { - checkBlockedResource("KeyManager", fileType, configKey, - (prefix, builder) -> buildKeyConfigSettings(additionalSettings, prefix, builder)); + checkBlockedResource( + "KeyManager", + fileType, + configKey, + (prefix, builder) -> buildKeyConfigSettings(additionalSettings, prefix, builder) + ); } private void checkBlockedTrustManagerResource(String fileType, String configKey) throws Exception { checkBlockedResource("TrustManager", fileType, configKey, this::configureWorkingKeystore); } - private void checkMissingResource(String fileType, String configKey, - BiConsumer configure) { + private void checkMissingResource(String fileType, String configKey, BiConsumer configure) { final String prefix = randomSslPrefix(); final Settings.Builder settings = Settings.builder(); configure.accept(prefix, settings); @@ -252,8 +279,12 @@ private void checkMissingResource(String fileType, String configKey, assertThat(exception, throwableWithMessage(fileName)); } - private void checkUnreadableResource(String fromResource, String fileType, String configKey, - BiConsumer configure) throws Exception { + private void checkUnreadableResource( + String fromResource, + String fileType, + String configKey, + BiConsumer configure + ) throws Exception { final String prefix = randomSslPrefix(); final Settings.Builder settings = Settings.builder(); configure.accept(prefix, settings); @@ -277,8 +308,12 @@ private void checkUnreadableResource(String fromResource, String fileType, Strin assertThat(exception, throwableWithMessage(fileName)); } - private void checkBlockedResource(String sslManagerType, String fileType, String configKey, - BiConsumer configure) throws Exception { + private void checkBlockedResource( + String sslManagerType, + String fileType, + String configKey, + BiConsumer configure + ) throws Exception { final String prefix = randomSslPrefix(); final Settings.Builder settings = Settings.builder(); configure.accept(prefix, settings); @@ -287,9 +322,13 @@ private void checkBlockedResource(String sslManagerType, String fileType, String final String key = prefix + "." + configKey; settings.put(key, fileName); - final String fileErrorMessage = "cannot read configured " + fileType + " [" + fileName + final String fileErrorMessage = "cannot read configured " + + fileType + + " [" + + fileName + "] because access to read the file is blocked; SSL resources should be placed in the [" - + env.configFile().toAbsolutePath().toString() + "] directory"; + + env.configFile().toAbsolutePath().toString() + + "] directory"; Throwable exception = expectFailure(settings); assertThat(exception, throwableWithMessage("failed to load SSL configuration [" + prefix + "] - " + fileErrorMessage)); @@ -309,8 +348,19 @@ private void checkUnusedConfiguration(String prefix, String settingsConfigured, configure.accept(prefix, settings); Throwable exception = expectFailure(settings); - assertThat(exception, throwableWithMessage("invalid configuration for " + prefix + " - [" + prefix + ".enabled] is not set," + - " but the following settings have been configured in elasticsearch.yml : [" + settingsConfigured + "]")); + assertThat( + exception, + throwableWithMessage( + "invalid configuration for " + + prefix + + " - [" + + prefix + + ".enabled] is not set," + + " but the following settings have been configured in elasticsearch.yml : [" + + settingsConfigured + + "]" + ) + ); assertThat(exception, instanceOf(ElasticsearchException.class)); } @@ -340,8 +390,10 @@ private String copy(Path fromPath, Path toPath, Set permiss Files.deleteIfExists(toPath); final FileAttribute> fileAttributes = PosixFilePermissions.asFileAttribute(permissions); final EnumSet options = EnumSet.of(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE); - try (SeekableByteChannel channel = Files.newByteChannel(toPath, options, fileAttributes); - OutputStream out = Channels.newOutputStream(channel)) { + try ( + SeekableByteChannel channel = Files.newByteChannel(toPath, options, fileAttributes); + OutputStream out = Channels.newOutputStream(channel) + ) { Files.copy(fromPath, out); } return toPath.toString(); @@ -375,8 +427,10 @@ private Settings.Builder configureWorkingKeystore(String prefix, Settings.Builde } private ElasticsearchException expectFailure(Settings.Builder settings) { - return expectThrows(ElasticsearchException.class, - () -> new SSLService(new Environment(buildEnvSettings(settings.build()), env.configFile()))); + return expectThrows( + ElasticsearchException.class, + () -> new SSLService(new Environment(buildEnvSettings(settings.build()), env.configFile())) + ); } private SSLService expectSuccess(Settings.Builder settings) { diff --git a/x-pack/plugin/shutdown/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/shutdown/NodeShutdownIT.java b/x-pack/plugin/shutdown/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/shutdown/NodeShutdownIT.java index 39d010cca3407..e2968d22e55ee 100644 --- a/x-pack/plugin/shutdown/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/shutdown/NodeShutdownIT.java +++ b/x-pack/plugin/shutdown/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/shutdown/NodeShutdownIT.java @@ -14,11 +14,11 @@ import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.test.rest.ESRestTestCase; import org.elasticsearch.xcontent.ObjectPath; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.test.rest.ESRestTestCase; import java.io.IOException; import java.util.List; @@ -114,7 +114,6 @@ private void checkPutShutdownIdempotency(String type) throws Exception { } } - public void testPutShutdownCanChangeTypeFromRestartToRemove() throws Exception { checkTypeChange("RESTART", "REMOVE"); } @@ -279,7 +278,7 @@ public void testShardsCanBeAllocatedAfterShutdownDeleted() throws Exception { @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/77456") public void testStalledShardMigrationProperlyDetected() throws Exception { String nodeIdToShutdown = getRandomNodeId(); - int numberOfShards = randomIntBetween(1,5); + int numberOfShards = randomIntBetween(1, 5); // Create an index, pin the allocation to the node we're about to shut down final String indexName = "test-idx"; @@ -322,7 +321,7 @@ public void testStalledShardMigrationProperlyDetected() throws Exception { Map status = entityAsMap(statusResponse); assertThat(ObjectPath.eval("nodes.0.shard_migration.status", status), equalTo("COMPLETE")); assertThat(ObjectPath.eval("nodes.0.shard_migration.shard_migrations_remaining", status), equalTo(0)); - assertThat(ObjectPath.eval("nodes.0.shard_migration.explanation", status), nullValue()); + assertThat(ObjectPath.eval("nodes.0.shard_migration.explanation", status), nullValue()); }); } @@ -394,11 +393,7 @@ private void putNodeShutdown(String nodeIdToShutdown, String type, @Nullable Str putBody.field("allocation_delay", allocationDelay); } if (targetNodeName != null) { - assertThat( - "target node name parameter is only valid for REPLACE-type shutdowns", - type, - equalToIgnoringCase("replace") - ); + assertThat("target node name parameter is only valid for REPLACE-type shutdowns", type, equalToIgnoringCase("replace")); putBody.field("target_node_name", targetNodeName); } else { assertThat("target node name is required for REPALCE-type shutdowns", type, not(equalToIgnoringCase("replace"))); diff --git a/x-pack/plugin/snapshot-repo-test-kit/qa/rest/src/test/java/org/elasticsearch/repositories/blobstore/testkit/rest/SnapshotRepoTestKitClientYamlTestSuiteIT.java b/x-pack/plugin/snapshot-repo-test-kit/qa/rest/src/test/java/org/elasticsearch/repositories/blobstore/testkit/rest/SnapshotRepoTestKitClientYamlTestSuiteIT.java index a5c1daeda11fa..d1b078fa1f322 100644 --- a/x-pack/plugin/snapshot-repo-test-kit/qa/rest/src/test/java/org/elasticsearch/repositories/blobstore/testkit/rest/SnapshotRepoTestKitClientYamlTestSuiteIT.java +++ b/x-pack/plugin/snapshot-repo-test-kit/qa/rest/src/test/java/org/elasticsearch/repositories/blobstore/testkit/rest/SnapshotRepoTestKitClientYamlTestSuiteIT.java @@ -8,6 +8,7 @@ package org.elasticsearch.repositories.blobstore.testkit.rest; import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; + import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate; import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase; diff --git a/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/GeoBoundingBoxQueryGeoShapeWithDocValuesIT.java b/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/GeoBoundingBoxQueryGeoShapeWithDocValuesIT.java index 5ac7a160fc8ba..8206454e926b7 100644 --- a/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/GeoBoundingBoxQueryGeoShapeWithDocValuesIT.java +++ b/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/GeoBoundingBoxQueryGeoShapeWithDocValuesIT.java @@ -8,11 +8,11 @@ package org.elasticsearch.xpack.spatial.search; import org.elasticsearch.Version; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.search.geo.GeoBoundingBoxQueryIntegTestCase; import org.elasticsearch.test.VersionUtils; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xpack.spatial.LocalStateSpatialPlugin; import java.io.IOException; @@ -27,14 +27,18 @@ protected boolean addMockGeoShapeFieldMapper() { } @Override - protected Collection> nodePlugins() { + protected Collection> nodePlugins() { return Collections.singleton(LocalStateSpatialPlugin.class); } @Override public XContentBuilder getMapping() throws IOException { - XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().startObject().startObject("_doc") - .startObject("properties").startObject("location").field("type", "geo_shape"); + XContentBuilder xContentBuilder = XContentFactory.jsonBuilder() + .startObject() + .startObject("_doc") + .startObject("properties") + .startObject("location") + .field("type", "geo_shape"); xContentBuilder.endObject().endObject().endObject().endObject(); return xContentBuilder; } @@ -44,4 +48,3 @@ public Version randomSupportedVersion() { return VersionUtils.randomIndexCompatibleVersion(random()); } } - diff --git a/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/GeoBoundingBoxQueryLegacyGeoShapeWithDocValuesIT.java b/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/GeoBoundingBoxQueryLegacyGeoShapeWithDocValuesIT.java index 9b77f243fecc1..2073eac56218f 100644 --- a/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/GeoBoundingBoxQueryLegacyGeoShapeWithDocValuesIT.java +++ b/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/GeoBoundingBoxQueryLegacyGeoShapeWithDocValuesIT.java @@ -8,11 +8,11 @@ package org.elasticsearch.xpack.spatial.search; import org.elasticsearch.Version; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.search.geo.GeoBoundingBoxQueryIntegTestCase; import org.elasticsearch.test.VersionUtils; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xpack.spatial.LocalStateSpatialPlugin; import java.io.IOException; @@ -33,9 +33,17 @@ protected Collection> nodePlugins() { @Override public XContentBuilder getMapping() throws IOException { - return XContentFactory.jsonBuilder().startObject().startObject("_doc") - .startObject("properties").startObject("location").field("type", "geo_shape").field("strategy", "recursive") - .endObject().endObject().endObject().endObject(); + return XContentFactory.jsonBuilder() + .startObject() + .startObject("_doc") + .startObject("properties") + .startObject("location") + .field("type", "geo_shape") + .field("strategy", "recursive") + .endObject() + .endObject() + .endObject() + .endObject(); } @Override @@ -43,4 +51,3 @@ public Version randomSupportedVersion() { return VersionUtils.randomPreviousCompatibleVersion(random(), Version.V_8_0_0); } } - diff --git a/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/GeoShapeScriptDocValuesIT.java b/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/GeoShapeScriptDocValuesIT.java index 0267220a251f3..7ba889db0661d 100644 --- a/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/GeoShapeScriptDocValuesIT.java +++ b/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/GeoShapeScriptDocValuesIT.java @@ -9,8 +9,6 @@ import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.common.document.DocumentField; import org.elasticsearch.common.geo.GeoBoundingBox; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.geo.GeometryTestUtils; import org.elasticsearch.geometry.Geometry; import org.elasticsearch.geometry.LinearRing; @@ -23,6 +21,8 @@ import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptType; import org.elasticsearch.test.ESSingleNodeTestCase; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xpack.core.LocalStateCompositeXPackPlugin; import org.elasticsearch.xpack.spatial.LocalStateSpatialPlugin; import org.elasticsearch.xpack.spatial.index.fielddata.GeoShapeValues; @@ -38,9 +38,9 @@ import java.util.Map; import java.util.function.Function; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.greaterThanOrEqualTo; import static org.hamcrest.Matchers.lessThanOrEqualTo; @@ -65,7 +65,6 @@ protected Map, Object>> pluginScripts() { return scripts; } - private double scriptHeight(Map vars) { Map doc = (Map) vars.get("doc"); ScriptDocValues.Geometry geometry = assertGeometry(doc); @@ -123,8 +122,12 @@ protected boolean forbidPrivateIndexSettings() { @Before public void setupTestIndex() throws IOException { - XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().startObject().startObject("_doc") - .startObject("properties").startObject("location").field("type", "geo_shape"); + XContentBuilder xContentBuilder = XContentFactory.jsonBuilder() + .startObject() + .startObject("_doc") + .startObject("properties") + .startObject("location") + .field("type", "geo_shape"); xContentBuilder.endObject().endObject().endObject().endObject(); assertAcked(client().admin().indices().prepareCreate("test").setMapping(xContentBuilder)); ensureGreen(); @@ -144,23 +147,24 @@ public void testRandomShape() throws Exception { } public void testPolygonDateline() throws Exception { - Geometry geometry = new Polygon(new LinearRing(new double[]{170, 190, 190, 170, 170}, new double[]{-5, -5, 5, 5, -5})); + Geometry geometry = new Polygon(new LinearRing(new double[] { 170, 190, 190, 170, 170 }, new double[] { -5, -5, 5, 5, -5 })); doTestGeometry(geometry); } - private void doTestGeometry(Geometry geometry) throws IOException { - client().prepareIndex("test").setId("1") - .setSource(jsonBuilder().startObject() - .field("name", "TestPosition") - .field("location", WellKnownText.toWKT(geometry)) - .endObject()) + private void doTestGeometry(Geometry geometry) throws IOException { + client().prepareIndex("test") + .setId("1") + .setSource( + jsonBuilder().startObject().field("name", "TestPosition").field("location", WellKnownText.toWKT(geometry)).endObject() + ) .get(); client().admin().indices().prepareRefresh("test").get(); GeoShapeValues.GeoShapeValue value = GeoTestUtils.geoShapeValue(geometry); - SearchResponse searchResponse = client().prepareSearch().addStoredField("_source") + SearchResponse searchResponse = client().prepareSearch() + .addStoredField("_source") .addScriptField("lat", new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "lat", Collections.emptyMap())) .addScriptField("lon", new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "lon", Collections.emptyMap())) .addScriptField("height", new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "height", Collections.emptyMap())) @@ -175,16 +179,15 @@ private void doTestGeometry(Geometry geometry) throws IOException { } public void testNullShape() throws Exception { - client().prepareIndex("test").setId("1") - .setSource(jsonBuilder().startObject() - .field("name", "TestPosition") - .nullField("location") - .endObject()) + client().prepareIndex("test") + .setId("1") + .setSource(jsonBuilder().startObject().field("name", "TestPosition").nullField("location").endObject()) .get(); client().admin().indices().prepareRefresh("test").get(); - SearchResponse searchResponse = client().prepareSearch().addStoredField("_source") + SearchResponse searchResponse = client().prepareSearch() + .addStoredField("_source") .addScriptField("lat", new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "lat", Collections.emptyMap())) .addScriptField("lon", new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "lon", Collections.emptyMap())) .addScriptField("height", new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "height", Collections.emptyMap())) diff --git a/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/GeoShapeWithDocValuesIT.java b/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/GeoShapeWithDocValuesIT.java index 46fbfa775faf8..bd4ea458d0a25 100644 --- a/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/GeoShapeWithDocValuesIT.java +++ b/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/GeoShapeWithDocValuesIT.java @@ -8,12 +8,12 @@ package org.elasticsearch.xpack.spatial.search; import org.elasticsearch.Version; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.mapper.MapperParsingException; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.search.geo.GeoShapeIntegTestCase; import org.elasticsearch.test.VersionUtils; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.spatial.LocalStateSpatialPlugin; import java.io.IOException; @@ -31,7 +31,7 @@ protected boolean addMockGeoShapeFieldMapper() { } @Override - protected Collection> nodePlugins() { + protected Collection> nodePlugins() { return Collections.singleton(LocalStateSpatialPlugin.class); } @@ -53,31 +53,43 @@ protected boolean allowExpensiveQueries() { public void testMappingUpdate() { // create index Version version = randomSupportedVersion(); - assertAcked(client().admin().indices().prepareCreate("test").setSettings(settings(version).build()) - .setMapping("shape", "type=geo_shape").get()); + assertAcked( + client().admin() + .indices() + .prepareCreate("test") + .setSettings(settings(version).build()) + .setMapping("shape", "type=geo_shape") + .get() + ); ensureGreen(); - String update ="{\n" + - " \"properties\": {\n" + - " \"shape\": {\n" + - " \"type\": \"geo_shape\"," + - " \"strategy\": \"recursive\"" + - " }\n" + - " }\n" + - "}"; + String update = "{\n" + + " \"properties\": {\n" + + " \"shape\": {\n" + + " \"type\": \"geo_shape\"," + + " \"strategy\": \"recursive\"" + + " }\n" + + " }\n" + + "}"; if (version.before(Version.V_8_0_0)) { - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> client().admin().indices() - .preparePutMapping("test") - .setSource(update, XContentType.JSON).get()); - assertThat(e.getMessage(), - containsString("mapper [shape] of type [geo_shape] cannot change strategy from [BKD] to [recursive]")); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> client().admin().indices().preparePutMapping("test").setSource(update, XContentType.JSON).get() + ); + assertThat( + e.getMessage(), + containsString("mapper [shape] of type [geo_shape] cannot change strategy from [BKD] to [recursive]") + ); } else { - MapperParsingException e = expectThrows(MapperParsingException.class, () -> client().admin().indices() - .preparePutMapping("test") - .setSource(update, XContentType.JSON).get()); - assertThat(e.getMessage(), - containsString("using deprecated parameters [strategy] in mapper [shape] of type [geo_shape] is no longer allowed")); + MapperParsingException e = expectThrows( + MapperParsingException.class, + () -> client().admin().indices().preparePutMapping("test").setSource(update, XContentType.JSON).get() + ); + assertThat( + e.getMessage(), + containsString("using deprecated parameters [strategy] in mapper [shape] of type [geo_shape] is no longer allowed") + ); } } } diff --git a/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/GeoShapeWithDocValuesQueryTests.java b/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/GeoShapeWithDocValuesQueryTests.java index 41649db585a5e..4a3ed02a62d7a 100644 --- a/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/GeoShapeWithDocValuesQueryTests.java +++ b/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/GeoShapeWithDocValuesQueryTests.java @@ -11,12 +11,12 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.geo.GeoJson; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.geo.GeometryTestUtils; import org.elasticsearch.geometry.MultiPoint; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.search.geo.GeoShapeQueryTestCase; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xpack.spatial.LocalStateSpatialPlugin; import java.io.IOException; @@ -24,8 +24,8 @@ import java.util.Collections; import static org.elasticsearch.action.support.WriteRequest.RefreshPolicy.IMMEDIATE; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.index.query.QueryBuilders.geoShapeQuery; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; public class GeoShapeWithDocValuesQueryTests extends GeoShapeQueryTestCase { @@ -36,38 +36,44 @@ protected Collection> getPlugins() { @Override protected void createMapping(String indexName, String fieldName, Settings settings) throws Exception { - XContentBuilder xcb = XContentFactory.jsonBuilder().startObject() - .startObject("properties").startObject(fieldName) - .field("type", "geo_shape") - .endObject().endObject().endObject(); - client().admin().indices().prepareCreate(indexName).setMapping(xcb).setSettings(settings).get(); - } - - public void testFieldAlias() throws IOException { - String mapping = Strings.toString(XContentFactory.jsonBuilder() + XContentBuilder xcb = XContentFactory.jsonBuilder() .startObject() .startObject("properties") - .startObject(defaultGeoFieldName) + .startObject(fieldName) .field("type", "geo_shape") .endObject() - .startObject("alias") - .field("type", "alias") - .field("path", defaultGeoFieldName) - .endObject() .endObject() - .endObject()); + .endObject(); + client().admin().indices().prepareCreate(indexName).setMapping(xcb).setSettings(settings).get(); + } + + public void testFieldAlias() throws IOException { + String mapping = Strings.toString( + XContentFactory.jsonBuilder() + .startObject() + .startObject("properties") + .startObject(defaultGeoFieldName) + .field("type", "geo_shape") + .endObject() + .startObject("alias") + .field("type", "alias") + .field("path", defaultGeoFieldName) + .endObject() + .endObject() + .endObject() + ); client().admin().indices().prepareCreate(defaultIndexName).setMapping(mapping).get(); ensureGreen(); MultiPoint multiPoint = GeometryTestUtils.randomMultiPoint(false); - client().prepareIndex(defaultIndexName).setId("1") + client().prepareIndex(defaultIndexName) + .setId("1") .setSource(GeoJson.toXContent(multiPoint, jsonBuilder().startObject().field(defaultGeoFieldName), null).endObject()) - .setRefreshPolicy(IMMEDIATE).get(); - - SearchResponse response = client().prepareSearch(defaultIndexName) - .setQuery(geoShapeQuery("alias", multiPoint)) + .setRefreshPolicy(IMMEDIATE) .get(); + + SearchResponse response = client().prepareSearch(defaultIndexName).setQuery(geoShapeQuery("alias", multiPoint)).get(); assertEquals(1, response.getHits().getTotalHits().value); } } diff --git a/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/LegacyGeoShapeWithDocValuesIT.java b/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/LegacyGeoShapeWithDocValuesIT.java index 333914d0e1880..1bb457d8732a9 100644 --- a/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/LegacyGeoShapeWithDocValuesIT.java +++ b/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/LegacyGeoShapeWithDocValuesIT.java @@ -9,13 +9,13 @@ import org.elasticsearch.Version; import org.elasticsearch.action.search.SearchResponse; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.geometry.Circle; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.search.geo.GeoShapeIntegTestCase; import org.elasticsearch.test.VersionUtils; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.spatial.LocalStateSpatialPlugin; import java.io.IOException; @@ -58,21 +58,28 @@ protected boolean allowExpensiveQueries() { public void testMappingUpdate() { // create index - assertAcked(client().admin().indices().prepareCreate("test").setSettings(settings(randomSupportedVersion()).build()) - .setMapping("shape", "type=geo_shape,strategy=recursive").get()); + assertAcked( + client().admin() + .indices() + .prepareCreate("test") + .setSettings(settings(randomSupportedVersion()).build()) + .setMapping("shape", "type=geo_shape,strategy=recursive") + .get() + ); ensureGreen(); - String update ="{\n" + - " \"properties\": {\n" + - " \"shape\": {\n" + - " \"type\": \"geo_shape\"" + - " }\n" + - " }\n" + - "}"; - - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> client().admin().indices() - .preparePutMapping("test") - .setSource(update, XContentType.JSON).get()); + String update = "{\n" + + " \"properties\": {\n" + + " \"shape\": {\n" + + " \"type\": \"geo_shape\"" + + " }\n" + + " }\n" + + "}"; + + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> client().admin().indices().preparePutMapping("test").setSource(update, XContentType.JSON).get() + ); assertThat(e.getMessage(), containsString("mapper [shape] of type [geo_shape] cannot change strategy from [recursive] to [BKD]")); } @@ -81,21 +88,27 @@ public void testMappingUpdate() { */ public void testLegacyCircle() throws Exception { // create index - assertAcked(prepareCreate("test").setSettings(settings(randomSupportedVersion()).build()) - .setMapping("shape", "type=geo_shape,strategy=recursive,tree=geohash").get()); + assertAcked( + prepareCreate("test").setSettings(settings(randomSupportedVersion()).build()) + .setMapping("shape", "type=geo_shape,strategy=recursive,tree=geohash") + .get() + ); ensureGreen(); indexRandom(true, client().prepareIndex("test").setId("0").setSource("shape", (ToXContent) (builder, params) -> { - builder.startObject().field("type", "circle") - .startArray("coordinates").value(30).value(50).endArray() - .field("radius","77km") + builder.startObject() + .field("type", "circle") + .startArray("coordinates") + .value(30) + .value(50) + .endArray() + .field("radius", "77km") .endObject(); return builder; })); // test self crossing of circles - SearchResponse searchResponse = client().prepareSearch("test").setQuery(geoShapeQuery("shape", - new Circle(30, 50, 77000))).get(); + SearchResponse searchResponse = client().prepareSearch("test").setQuery(geoShapeQuery("shape", new Circle(30, 50, 77000))).get(); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); } } diff --git a/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/ShapeQueryOverPointTests.java b/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/ShapeQueryOverPointTests.java index 7180f1ff2efc3..36dbdbb7f09d5 100644 --- a/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/ShapeQueryOverPointTests.java +++ b/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/ShapeQueryOverPointTests.java @@ -11,8 +11,6 @@ import org.elasticsearch.action.search.SearchRequestBuilder; import org.elasticsearch.common.Strings; import org.elasticsearch.common.geo.ShapeRelation; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.geometry.Line; import org.elasticsearch.geometry.LinearRing; import org.elasticsearch.geometry.MultiLine; @@ -20,6 +18,8 @@ import org.elasticsearch.geometry.Point; import org.elasticsearch.geometry.Rectangle; import org.elasticsearch.geometry.ShapeType; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xpack.spatial.index.query.ShapeQueryBuilder; import org.hamcrest.CoreMatchers; @@ -28,10 +28,14 @@ public class ShapeQueryOverPointTests extends ShapeQueryTestCase { @Override protected XContentBuilder createDefaultMapping() throws Exception { - XContentBuilder xcb = XContentFactory.jsonBuilder().startObject() - .startObject("properties").startObject(defaultFieldName) + XContentBuilder xcb = XContentFactory.jsonBuilder() + .startObject() + .startObject("properties") + .startObject(defaultFieldName) .field("type", "point") - .endObject().endObject().endObject(); + .endObject() + .endObject() + .endObject(); return xcb; } @@ -45,14 +49,16 @@ public void testProcessRelationSupport() throws Exception { for (ShapeRelation shapeRelation : ShapeRelation.values()) { if (shapeRelation.equals(ShapeRelation.INTERSECTS) == false) { - SearchPhaseExecutionException e = expectThrows(SearchPhaseExecutionException.class, () -> - client().prepareSearch("test") - .setQuery(new ShapeQueryBuilder(defaultFieldName, rectangle) - .relation(shapeRelation)) - .get()); - assertThat(e.getCause().getMessage(), - CoreMatchers.containsString(shapeRelation - + " query relation not supported for Field [" + defaultFieldName + "]")); + SearchPhaseExecutionException e = expectThrows( + SearchPhaseExecutionException.class, + () -> client().prepareSearch("test") + .setQuery(new ShapeQueryBuilder(defaultFieldName, rectangle).relation(shapeRelation)) + .get() + ); + assertThat( + e.getCause().getMessage(), + CoreMatchers.containsString(shapeRelation + " query relation not supported for Field [" + defaultFieldName + "]") + ); } } } @@ -62,15 +68,12 @@ public void testQueryLine() throws Exception { client().admin().indices().prepareCreate("test").setMapping(mapping).get(); ensureGreen(); - Line line = new Line(new double[]{-25, -25}, new double[]{-35, -35}); + Line line = new Line(new double[] { -25, -25 }, new double[] { -35, -35 }); try { - client().prepareSearch("test") - .setQuery(new ShapeQueryBuilder(defaultFieldName, line)).get(); - } catch ( - SearchPhaseExecutionException e) { - assertThat(e.getCause().getMessage(), - CoreMatchers.containsString("does not support " + ShapeType.LINESTRING + " queries")); + client().prepareSearch("test").setQuery(new ShapeQueryBuilder(defaultFieldName, line)).get(); + } catch (SearchPhaseExecutionException e) { + assertThat(e.getCause().getMessage(), CoreMatchers.containsString("does not support " + ShapeType.LINESTRING + " queries")); } } @@ -79,7 +82,7 @@ public void testQueryLinearRing() throws Exception { client().admin().indices().prepareCreate("test").setMapping(mapping).get(); ensureGreen(); - LinearRing linearRing = new LinearRing(new double[]{-25,-35,-25}, new double[]{-25,-35,-25}); + LinearRing linearRing = new LinearRing(new double[] { -25, -35, -25 }, new double[] { -25, -35, -25 }); try { // LinearRing extends Line implements Geometry: expose the build process @@ -88,10 +91,11 @@ public void testQueryLinearRing() throws Exception { searchRequestBuilder.setQuery(queryBuilder); searchRequestBuilder.setIndices("test"); searchRequestBuilder.get(); - } catch ( - SearchPhaseExecutionException e) { - assertThat(e.getCause().getMessage(), - CoreMatchers.containsString("Field [" + defaultFieldName + "] does not support LINEARRING queries")); + } catch (SearchPhaseExecutionException e) { + assertThat( + e.getCause().getMessage(), + CoreMatchers.containsString("Field [" + defaultFieldName + "] does not support LINEARRING queries") + ); } } @@ -100,22 +104,17 @@ public void testQueryMultiLine() throws Exception { client().admin().indices().prepareCreate("test").setMapping(mapping).get(); ensureGreen(); - Line lsb1 = new Line( - new double[] {-35, -25}, - new double[] {-35, -25} - ); - Line lsb2 = new Line( - new double[] {-15, -5}, - new double[] {-15, -5} - ); + Line lsb1 = new Line(new double[] { -35, -25 }, new double[] { -35, -25 }); + Line lsb2 = new Line(new double[] { -15, -5 }, new double[] { -15, -5 }); MultiLine multiline = new MultiLine(List.of(lsb1, lsb2)); try { - client().prepareSearch("test") - .setQuery(new ShapeQueryBuilder(defaultFieldName, multiline)).get(); + client().prepareSearch("test").setQuery(new ShapeQueryBuilder(defaultFieldName, multiline)).get(); } catch (Exception e) { - assertThat(e.getCause().getMessage(), - CoreMatchers.containsString("does not support " + ShapeType.MULTILINESTRING + " queries")); + assertThat( + e.getCause().getMessage(), + CoreMatchers.containsString("does not support " + ShapeType.MULTILINESTRING + " queries") + ); } } @@ -124,14 +123,12 @@ public void testQueryMultiPoint() throws Exception { client().admin().indices().prepareCreate("test").setMapping(mapping).get(); ensureGreen(); - MultiPoint multiPoint =new MultiPoint(List.of(new Point(-35,-25), new Point(-15,-5))); + MultiPoint multiPoint = new MultiPoint(List.of(new Point(-35, -25), new Point(-15, -5))); try { - client().prepareSearch("test") - .setQuery(new ShapeQueryBuilder(defaultFieldName, multiPoint)).get(); + client().prepareSearch("test").setQuery(new ShapeQueryBuilder(defaultFieldName, multiPoint)).get(); } catch (Exception e) { - assertThat(e.getCause().getMessage(), - CoreMatchers.containsString("does not support " + ShapeType.MULTIPOINT + " queries")); + assertThat(e.getCause().getMessage(), CoreMatchers.containsString("does not support " + ShapeType.MULTIPOINT + " queries")); } } @@ -143,11 +140,9 @@ public void testQueryPoint() throws Exception { Point point = new Point(-35, -2); try { - client().prepareSearch("test") - .setQuery(new ShapeQueryBuilder(defaultFieldName, point)).get(); + client().prepareSearch("test").setQuery(new ShapeQueryBuilder(defaultFieldName, point)).get(); } catch (Exception e) { - assertThat(e.getCause().getMessage(), - CoreMatchers.containsString("does not support " + ShapeType.POINT + " queries")); + assertThat(e.getCause().getMessage(), CoreMatchers.containsString("does not support " + ShapeType.POINT + " queries")); } } diff --git a/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/ShapeQueryOverShapeTests.java b/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/ShapeQueryOverShapeTests.java index c93c84d9f4518..e84cf58bf897c 100644 --- a/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/ShapeQueryOverShapeTests.java +++ b/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/ShapeQueryOverShapeTests.java @@ -12,9 +12,6 @@ import org.elasticsearch.common.geo.GeoJson; import org.elasticsearch.common.geo.ShapeRelation; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.geometry.Geometry; import org.elasticsearch.geometry.GeometryCollection; import org.elasticsearch.geometry.MultiPoint; @@ -24,6 +21,9 @@ import org.elasticsearch.geometry.utils.WellKnownText; import org.elasticsearch.index.query.ExistsQueryBuilder; import org.elasticsearch.index.query.QueryBuilders; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.spatial.index.query.ShapeQueryBuilder; import org.elasticsearch.xpack.spatial.util.ShapeTestUtils; @@ -32,11 +32,11 @@ import java.util.Locale; import static org.elasticsearch.action.support.WriteRequest.RefreshPolicy.IMMEDIATE; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.nullValue; @@ -52,10 +52,14 @@ public class ShapeQueryOverShapeTests extends ShapeQueryTestCase { @Override protected XContentBuilder createDefaultMapping() throws Exception { - XContentBuilder xcb = XContentFactory.jsonBuilder().startObject() - .startObject("properties").startObject(defaultFieldName) + XContentBuilder xcb = XContentFactory.jsonBuilder() + .startObject() + .startObject("properties") + .startObject(defaultFieldName) .field("type", "shape") - .endObject().endObject().endObject(); + .endObject() + .endObject() + .endObject(); return xcb; } @@ -65,11 +69,17 @@ public void setUp() throws Exception { super.setUp(); // create test index - assertAcked(client().admin().indices().prepareCreate(INDEX) - .setMapping(FIELD, "type=shape", "alias", "type=alias,path=" + FIELD).get()); + assertAcked( + client().admin().indices().prepareCreate(INDEX).setMapping(FIELD, "type=shape", "alias", "type=alias,path=" + FIELD).get() + ); // create index that ignores malformed geometry - assertAcked(client().admin().indices().prepareCreate(IGNORE_MALFORMED_INDEX) - .setMapping(FIELD, "type=shape,ignore_malformed=true", "_source", "enabled=false").get()); + assertAcked( + client().admin() + .indices() + .prepareCreate(IGNORE_MALFORMED_INDEX) + .setMapping(FIELD, "type=shape,ignore_malformed=true", "_source", "enabled=false") + .get() + ); ensureGreen(); // index random shapes @@ -83,8 +93,8 @@ public void setUp() throws Exception { if (queryGeometry == null && geometry.type() != ShapeType.MULTIPOINT) { queryGeometry = geometry; } - XContentBuilder geoJson = GeoJson.toXContent(geometry, XContentFactory.jsonBuilder() - .startObject().field(FIELD), null).endObject(); + XContentBuilder geoJson = GeoJson.toXContent(geometry, XContentFactory.jsonBuilder().startObject().field(FIELD), null) + .endObject(); try { client().prepareIndex(INDEX).setSource(geoJson).setRefreshPolicy(IMMEDIATE).get(); @@ -103,11 +113,18 @@ public void setUp() throws Exception { public void testIndexedShapeReferenceSourceDisabled() throws Exception { Rectangle rectangle = new Rectangle(-45, 45, 45, -45); - client().prepareIndex(IGNORE_MALFORMED_INDEX).setId("Big_Rectangle").setSource(jsonBuilder().startObject() - .field(FIELD, WellKnownText.toWKT(rectangle)).endObject()).setRefreshPolicy(IMMEDIATE).get(); + client().prepareIndex(IGNORE_MALFORMED_INDEX) + .setId("Big_Rectangle") + .setSource(jsonBuilder().startObject().field(FIELD, WellKnownText.toWKT(rectangle)).endObject()) + .setRefreshPolicy(IMMEDIATE) + .get(); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> client().prepareSearch(IGNORE_MALFORMED_INDEX) - .setQuery(new ShapeQueryBuilder(FIELD, "Big_Rectangle").indexedShapeIndex(IGNORE_MALFORMED_INDEX)).get()); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> client().prepareSearch(IGNORE_MALFORMED_INDEX) + .setQuery(new ShapeQueryBuilder(FIELD, "Big_Rectangle").indexedShapeIndex(IGNORE_MALFORMED_INDEX)) + .get() + ); assertThat(e.getMessage(), containsString("source disabled")); } @@ -119,75 +136,89 @@ public void testShapeFetchingPath() throws Exception { String location = "\"location\" : {\"type\":\"polygon\", \"coordinates\":[[[-10,-10],[10,-10],[10,10],[-10,10],[-10,-10]]]}"; - client().prepareIndex(indexName).setId("1") + client().prepareIndex(indexName) + .setId("1") + .setSource( + String.format(Locale.ROOT, "{ %s, \"1\" : { %s, \"2\" : { %s, \"3\" : { %s } }} }", location, location, location, location), + XContentType.JSON + ) + .setRefreshPolicy(IMMEDIATE) + .get(); + client().prepareIndex(searchIndex) + .setId("1") .setSource( - String.format( - Locale.ROOT, "{ %s, \"1\" : { %s, \"2\" : { %s, \"3\" : { %s } }} }", location, location, location, location - ), XContentType.JSON) - .setRefreshPolicy(IMMEDIATE).get(); - client().prepareIndex(searchIndex).setId("1") - .setSource(jsonBuilder().startObject().startObject("location") - .field("type", "polygon") - .startArray("coordinates").startArray() - .startArray().value(-20).value(-20).endArray() - .startArray().value(20).value(-20).endArray() - .startArray().value(20).value(20).endArray() - .startArray().value(-20).value(20).endArray() - .startArray().value(-20).value(-20).endArray() - .endArray().endArray() - .endObject().endObject()).setRefreshPolicy(IMMEDIATE).get(); + jsonBuilder().startObject() + .startObject("location") + .field("type", "polygon") + .startArray("coordinates") + .startArray() + .startArray() + .value(-20) + .value(-20) + .endArray() + .startArray() + .value(20) + .value(-20) + .endArray() + .startArray() + .value(20) + .value(20) + .endArray() + .startArray() + .value(-20) + .value(20) + .endArray() + .startArray() + .value(-20) + .value(-20) + .endArray() + .endArray() + .endArray() + .endObject() + .endObject() + ) + .setRefreshPolicy(IMMEDIATE) + .get(); ShapeQueryBuilder filter = new ShapeQueryBuilder("location", "1").relation(ShapeRelation.INTERSECTS) .indexedShapeIndex(indexName) .indexedShapePath("location"); - SearchResponse result = client().prepareSearch(searchIndex).setQuery(QueryBuilders.matchAllQuery()) - .setPostFilter(filter).get(); + SearchResponse result = client().prepareSearch(searchIndex).setQuery(QueryBuilders.matchAllQuery()).setPostFilter(filter).get(); assertSearchResponse(result); assertHitCount(result, 1); filter = new ShapeQueryBuilder("location", "1").relation(ShapeRelation.INTERSECTS) .indexedShapeIndex(indexName) .indexedShapePath("1.location"); - result = client().prepareSearch(searchIndex).setQuery(QueryBuilders.matchAllQuery()) - .setPostFilter(filter).get(); + result = client().prepareSearch(searchIndex).setQuery(QueryBuilders.matchAllQuery()).setPostFilter(filter).get(); assertSearchResponse(result); assertHitCount(result, 1); filter = new ShapeQueryBuilder("location", "1").relation(ShapeRelation.INTERSECTS) .indexedShapeIndex(indexName) .indexedShapePath("1.2.location"); - result = client().prepareSearch(searchIndex).setQuery(QueryBuilders.matchAllQuery()) - .setPostFilter(filter).get(); + result = client().prepareSearch(searchIndex).setQuery(QueryBuilders.matchAllQuery()).setPostFilter(filter).get(); assertSearchResponse(result); assertHitCount(result, 1); filter = new ShapeQueryBuilder("location", "1").relation(ShapeRelation.INTERSECTS) .indexedShapeIndex(indexName) .indexedShapePath("1.2.3.location"); - result = client().prepareSearch(searchIndex).setQuery(QueryBuilders.matchAllQuery()) - .setPostFilter(filter).get(); + result = client().prepareSearch(searchIndex).setQuery(QueryBuilders.matchAllQuery()).setPostFilter(filter).get(); assertSearchResponse(result); assertHitCount(result, 1); // now test the query variant - ShapeQueryBuilder query = new ShapeQueryBuilder("location", "1") - .indexedShapeIndex(indexName) - .indexedShapePath("location"); + ShapeQueryBuilder query = new ShapeQueryBuilder("location", "1").indexedShapeIndex(indexName).indexedShapePath("location"); result = client().prepareSearch(searchIndex).setQuery(query).get(); assertSearchResponse(result); assertHitCount(result, 1); - query = new ShapeQueryBuilder("location", "1") - .indexedShapeIndex(indexName) - .indexedShapePath("1.location"); + query = new ShapeQueryBuilder("location", "1").indexedShapeIndex(indexName).indexedShapePath("1.location"); result = client().prepareSearch(searchIndex).setQuery(query).get(); assertSearchResponse(result); assertHitCount(result, 1); - query = new ShapeQueryBuilder("location", "1") - .indexedShapeIndex(indexName) - .indexedShapePath("1.2.location"); + query = new ShapeQueryBuilder("location", "1").indexedShapeIndex(indexName).indexedShapePath("1.2.location"); result = client().prepareSearch(searchIndex).setQuery(query).get(); assertSearchResponse(result); assertHitCount(result, 1); - query = new ShapeQueryBuilder("location", "1") - .indexedShapeIndex(indexName) - .indexedShapePath("1.2.3.location"); + query = new ShapeQueryBuilder("location", "1").indexedShapeIndex(indexName).indexedShapePath("1.2.3.location"); result = client().prepareSearch(searchIndex).setQuery(query).get(); assertSearchResponse(result); assertHitCount(result, 1); @@ -204,30 +235,43 @@ public void testIgnoreMalformed() { * Test that the indexed shape routing can be provided if it is required */ public void testIndexShapeRouting() { - String source = "{\n" + - " \"shape\" : {\n" + - " \"type\" : \"bbox\",\n" + - " \"coordinates\" : [[" + -Float.MAX_VALUE + "," + Float.MAX_VALUE + "], [" + Float.MAX_VALUE + ", " + -Float.MAX_VALUE - + "]]\n" + - " }\n" + - "}"; + String source = "{\n" + + " \"shape\" : {\n" + + " \"type\" : \"bbox\",\n" + + " \"coordinates\" : [[" + + -Float.MAX_VALUE + + "," + + Float.MAX_VALUE + + "], [" + + Float.MAX_VALUE + + ", " + + -Float.MAX_VALUE + + "]]\n" + + " }\n" + + "}"; client().prepareIndex(INDEX).setId("0").setSource(source, XContentType.JSON).setRouting("ABC").get(); client().admin().indices().prepareRefresh(INDEX).get(); - SearchResponse searchResponse = client().prepareSearch(INDEX).setQuery( - new ShapeQueryBuilder(FIELD, "0").indexedShapeIndex(INDEX).indexedShapeRouting("ABC") - ).get(); + SearchResponse searchResponse = client().prepareSearch(INDEX) + .setQuery(new ShapeQueryBuilder(FIELD, "0").indexedShapeIndex(INDEX).indexedShapeRouting("ABC")) + .get(); - assertThat(searchResponse.getHits().getTotalHits().value, equalTo((long)numDocs+1)); + assertThat(searchResponse.getHits().getTotalHits().value, equalTo((long) numDocs + 1)); } public void testNullShape() { // index a null shape - client().prepareIndex(INDEX).setId("aNullshape").setSource("{\"" + FIELD + "\": null}", XContentType.JSON) - .setRefreshPolicy(IMMEDIATE).get(); - client().prepareIndex(IGNORE_MALFORMED_INDEX).setId("aNullshape").setSource("{\"" + FIELD + "\": null}", - XContentType.JSON).setRefreshPolicy(IMMEDIATE).get(); + client().prepareIndex(INDEX) + .setId("aNullshape") + .setSource("{\"" + FIELD + "\": null}", XContentType.JSON) + .setRefreshPolicy(IMMEDIATE) + .get(); + client().prepareIndex(IGNORE_MALFORMED_INDEX) + .setId("aNullshape") + .setSource("{\"" + FIELD + "\": null}", XContentType.JSON) + .setRefreshPolicy(IMMEDIATE) + .get(); GetResponse result = client().prepareGet(INDEX, "aNullshape").get(); assertThat(result.getField(FIELD), nullValue()); } @@ -248,8 +292,7 @@ public void testFieldAlias() { public void testContainsShapeQuery() { - client().admin().indices().prepareCreate("test_contains").setMapping("location", "type=shape") - .execute().actionGet(); + client().admin().indices().prepareCreate("test_contains").setMapping("location", "type=shape").execute().actionGet(); String doc = "{\"location\" : {\"type\":\"envelope\", \"coordinates\":[ [-100.0, 100.0], [100.0, -100.0]]}}"; client().prepareIndex("test_contains").setId("1").setSource(doc, XContentType.JSON).setRefreshPolicy(IMMEDIATE).get(); @@ -264,10 +307,13 @@ public void testContainsShapeQuery() { } public void testGeometryCollectionRelations() throws IOException { - XContentBuilder mapping = XContentFactory.jsonBuilder().startObject() + XContentBuilder mapping = XContentFactory.jsonBuilder() + .startObject() .startObject("_doc") .startObject("properties") - .startObject("geometry").field("type", "shape").endObject() + .startObject("geometry") + .field("type", "shape") + .endObject() .endObject() .endObject() .endObject(); @@ -276,9 +322,11 @@ public void testGeometryCollectionRelations() throws IOException { Rectangle rectangle = new Rectangle(-10, 10, 10, -10); - client().index(new IndexRequest("test_collections") - .source(jsonBuilder().startObject().field("geometry", WellKnownText.toWKT(rectangle)).endObject()) - .setRefreshPolicy(IMMEDIATE)).actionGet(); + client().index( + new IndexRequest("test_collections").source( + jsonBuilder().startObject().field("geometry", WellKnownText.toWKT(rectangle)).endObject() + ).setRefreshPolicy(IMMEDIATE) + ).actionGet(); { // A geometry collection that is fully within the indexed shape diff --git a/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/ShapeQueryTestCase.java b/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/ShapeQueryTestCase.java index 8f95ba0789b70..31497b6fba9a2 100644 --- a/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/ShapeQueryTestCase.java +++ b/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/ShapeQueryTestCase.java @@ -11,9 +11,6 @@ import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.common.Strings; import org.elasticsearch.common.geo.ShapeRelation; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.geometry.Circle; import org.elasticsearch.geometry.LinearRing; import org.elasticsearch.geometry.MultiPolygon; @@ -24,6 +21,9 @@ import org.elasticsearch.plugins.Plugin; import org.elasticsearch.search.SearchHits; import org.elasticsearch.test.ESSingleNodeTestCase; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.LocalStateCompositeXPackPlugin; import org.elasticsearch.xpack.spatial.LocalStateSpatialPlugin; import org.elasticsearch.xpack.spatial.index.query.ShapeQueryBuilder; @@ -32,8 +32,8 @@ import java.util.List; import static org.elasticsearch.action.support.WriteRequest.RefreshPolicy.IMMEDIATE; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.nullValue; @@ -58,7 +58,8 @@ public void testNullShape() throws Exception { client().prepareIndex(defaultIndexName) .setId("aNullshape") .setSource("{\"geo\": null}", XContentType.JSON) - .setRefreshPolicy(IMMEDIATE).get(); + .setRefreshPolicy(IMMEDIATE) + .get(); GetResponse result = client().prepareGet(defaultIndexName, "aNullshape").get(); assertThat(result.getField("location"), nullValue()); }; @@ -68,23 +69,22 @@ public void testIndexPointsFilterRectangle() throws Exception { client().admin().indices().prepareCreate(defaultIndexName).setMapping(mapping).get(); ensureGreen(); - client().prepareIndex(defaultIndexName).setId("1").setSource(jsonBuilder() - .startObject() - .field("name", "Document 1") - .field(defaultFieldName, "POINT(-30 -30)") - .endObject()).setRefreshPolicy(IMMEDIATE).get(); + client().prepareIndex(defaultIndexName) + .setId("1") + .setSource(jsonBuilder().startObject().field("name", "Document 1").field(defaultFieldName, "POINT(-30 -30)").endObject()) + .setRefreshPolicy(IMMEDIATE) + .get(); - client().prepareIndex(defaultIndexName).setId("2").setSource(jsonBuilder() - .startObject() - .field("name", "Document 2") - .field(defaultFieldName, "POINT(-45 -50)") - .endObject()).setRefreshPolicy(IMMEDIATE).get(); + client().prepareIndex(defaultIndexName) + .setId("2") + .setSource(jsonBuilder().startObject().field("name", "Document 2").field(defaultFieldName, "POINT(-45 -50)").endObject()) + .setRefreshPolicy(IMMEDIATE) + .get(); Rectangle rectangle = new Rectangle(-45, 45, 45, -45); SearchResponse searchResponse = client().prepareSearch(defaultIndexName) - .setQuery(new ShapeQueryBuilder(defaultFieldName, rectangle) - .relation(ShapeRelation.INTERSECTS)) + .setQuery(new ShapeQueryBuilder(defaultFieldName, rectangle).relation(ShapeRelation.INTERSECTS)) .get(); assertSearchResponse(searchResponse); @@ -93,9 +93,7 @@ public void testIndexPointsFilterRectangle() throws Exception { assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("1")); // default query, without specifying relation (expect intersects) - searchResponse = client().prepareSearch(defaultIndexName) - .setQuery(new ShapeQueryBuilder(defaultFieldName, rectangle)) - .get(); + searchResponse = client().prepareSearch(defaultIndexName).setQuery(new ShapeQueryBuilder(defaultFieldName, rectangle)).get(); assertSearchResponse(searchResponse); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); @@ -108,23 +106,22 @@ public void testIndexPointsCircle() throws Exception { client().admin().indices().prepareCreate(defaultIndexName).setMapping(mapping).get(); ensureGreen(); - client().prepareIndex(defaultIndexName).setId("1").setSource(jsonBuilder() - .startObject() - .field("name", "Document 1") - .field(defaultFieldName, "POINT(-30 -30)") - .endObject()).setRefreshPolicy(IMMEDIATE).get(); + client().prepareIndex(defaultIndexName) + .setId("1") + .setSource(jsonBuilder().startObject().field("name", "Document 1").field(defaultFieldName, "POINT(-30 -30)").endObject()) + .setRefreshPolicy(IMMEDIATE) + .get(); - client().prepareIndex(defaultIndexName).setId("2").setSource(jsonBuilder() - .startObject() - .field("name", "Document 2") - .field(defaultFieldName, "POINT(-45 -50)") - .endObject()).setRefreshPolicy(IMMEDIATE).get(); + client().prepareIndex(defaultIndexName) + .setId("2") + .setSource(jsonBuilder().startObject().field("name", "Document 2").field(defaultFieldName, "POINT(-45 -50)").endObject()) + .setRefreshPolicy(IMMEDIATE) + .get(); Circle circle = new Circle(-30, -30, 1); SearchResponse searchResponse = client().prepareSearch(defaultIndexName) - .setQuery(new ShapeQueryBuilder(defaultFieldName, circle) - .relation(ShapeRelation.INTERSECTS)) + .setQuery(new ShapeQueryBuilder(defaultFieldName, circle).relation(ShapeRelation.INTERSECTS)) .get(); assertSearchResponse(searchResponse); @@ -138,26 +135,22 @@ public void testIndexPointsPolygon() throws Exception { client().admin().indices().prepareCreate(defaultIndexName).setMapping(mapping).get(); ensureGreen(); - client().prepareIndex(defaultIndexName).setId("1").setSource(jsonBuilder() - .startObject() - .field(defaultFieldName, "POINT(-30 -30)") - .endObject()).setRefreshPolicy(IMMEDIATE).get(); + client().prepareIndex(defaultIndexName) + .setId("1") + .setSource(jsonBuilder().startObject().field(defaultFieldName, "POINT(-30 -30)").endObject()) + .setRefreshPolicy(IMMEDIATE) + .get(); - client().prepareIndex(defaultIndexName).setId("2").setSource(jsonBuilder() - .startObject() - .field(defaultFieldName, "POINT(-45 -50)") - .endObject()).setRefreshPolicy(IMMEDIATE).get(); + client().prepareIndex(defaultIndexName) + .setId("2") + .setSource(jsonBuilder().startObject().field(defaultFieldName, "POINT(-45 -50)").endObject()) + .setRefreshPolicy(IMMEDIATE) + .get(); - Polygon polygon = new Polygon( - new LinearRing( - new double[] {-35, -35, -25, -25, -35}, - new double[] {-35, -25, -25, -35, -35} - ) - ); + Polygon polygon = new Polygon(new LinearRing(new double[] { -35, -35, -25, -25, -35 }, new double[] { -35, -25, -25, -35, -35 })); SearchResponse searchResponse = client().prepareSearch(defaultIndexName) - .setQuery(new ShapeQueryBuilder(defaultFieldName, polygon) - .relation(ShapeRelation.INTERSECTS)) + .setQuery(new ShapeQueryBuilder(defaultFieldName, polygon).relation(ShapeRelation.INTERSECTS)) .get(); assertSearchResponse(searchResponse); @@ -171,42 +164,35 @@ public void testIndexPointsMultiPolygon() throws Exception { client().admin().indices().prepareCreate(defaultIndexName).setMapping(mapping).get(); ensureGreen(); - client().prepareIndex(defaultIndexName).setId("1").setSource(jsonBuilder() - .startObject() - .field("name", "Document 1") - .field(defaultFieldName, "POINT(-30 -30)") - .endObject()).setRefreshPolicy(IMMEDIATE).get(); + client().prepareIndex(defaultIndexName) + .setId("1") + .setSource(jsonBuilder().startObject().field("name", "Document 1").field(defaultFieldName, "POINT(-30 -30)").endObject()) + .setRefreshPolicy(IMMEDIATE) + .get(); - client().prepareIndex(defaultIndexName).setId("2").setSource(jsonBuilder() - .startObject() - .field("name", "Document 2") - .field(defaultFieldName, "POINT(-40 -40)") - .endObject()).setRefreshPolicy(IMMEDIATE).get(); + client().prepareIndex(defaultIndexName) + .setId("2") + .setSource(jsonBuilder().startObject().field("name", "Document 2").field(defaultFieldName, "POINT(-40 -40)").endObject()) + .setRefreshPolicy(IMMEDIATE) + .get(); - client().prepareIndex(defaultIndexName).setId("3").setSource(jsonBuilder() - .startObject() - .field("name", "Document 3") - .field(defaultFieldName, "POINT(-50 -50)") - .endObject()).setRefreshPolicy(IMMEDIATE).get(); + client().prepareIndex(defaultIndexName) + .setId("3") + .setSource(jsonBuilder().startObject().field("name", "Document 3").field(defaultFieldName, "POINT(-50 -50)").endObject()) + .setRefreshPolicy(IMMEDIATE) + .get(); Polygon encloseDocument1Shape = new Polygon( - new LinearRing( - new double[] {-35, -35, -25, -25, -35}, - new double[] {-35, -25, -25, -35, -35} - ) + new LinearRing(new double[] { -35, -35, -25, -25, -35 }, new double[] { -35, -25, -25, -35, -35 }) ); Polygon encloseDocument2Shape = new Polygon( - new LinearRing( - new double[] {-55, -55, -45, -45, -55}, - new double[] {-55, -45, -45, -55, -55} - ) + new LinearRing(new double[] { -55, -55, -45, -45, -55 }, new double[] { -55, -45, -45, -55, -55 }) ); MultiPolygon mp = new MultiPolygon(List.of(encloseDocument1Shape, encloseDocument2Shape)); SearchResponse searchResponse = client().prepareSearch(defaultIndexName) - .setQuery(new ShapeQueryBuilder(defaultFieldName, mp) - .relation(ShapeRelation.INTERSECTS)) + .setQuery(new ShapeQueryBuilder(defaultFieldName, mp).relation(ShapeRelation.INTERSECTS)) .get(); assertSearchResponse(searchResponse); @@ -221,23 +207,22 @@ public void testIndexPointsRectangle() throws Exception { client().admin().indices().prepareCreate(defaultIndexName).setMapping(mapping).get(); ensureGreen(); - client().prepareIndex(defaultIndexName).setId("1").setSource(jsonBuilder() - .startObject() - .field("name", "Document 1") - .field(defaultFieldName, "POINT(-30 -30)") - .endObject()).setRefreshPolicy(IMMEDIATE).get(); + client().prepareIndex(defaultIndexName) + .setId("1") + .setSource(jsonBuilder().startObject().field("name", "Document 1").field(defaultFieldName, "POINT(-30 -30)").endObject()) + .setRefreshPolicy(IMMEDIATE) + .get(); - client().prepareIndex(defaultIndexName).setId("2").setSource(jsonBuilder() - .startObject() - .field("name", "Document 2") - .field(defaultFieldName, "POINT(-45 -50)") - .endObject()).setRefreshPolicy(IMMEDIATE).get(); + client().prepareIndex(defaultIndexName) + .setId("2") + .setSource(jsonBuilder().startObject().field("name", "Document 2").field(defaultFieldName, "POINT(-45 -50)").endObject()) + .setRefreshPolicy(IMMEDIATE) + .get(); Rectangle rectangle = new Rectangle(-50, -40, -45, -55); SearchResponse searchResponse = client().prepareSearch(defaultIndexName) - .setQuery(new ShapeQueryBuilder(defaultFieldName, rectangle) - .relation(ShapeRelation.INTERSECTS)) + .setQuery(new ShapeQueryBuilder(defaultFieldName, rectangle).relation(ShapeRelation.INTERSECTS)) .get(); assertSearchResponse(searchResponse); @@ -251,42 +236,51 @@ public void testIndexPointsIndexedRectangle() throws Exception { client().admin().indices().prepareCreate(defaultIndexName).setMapping(mapping).get(); ensureGreen(); - client().prepareIndex(defaultIndexName).setId("point1").setSource(jsonBuilder() - .startObject() - .field(defaultFieldName, "POINT(-30 -30)") - .endObject()).setRefreshPolicy(IMMEDIATE).get(); + client().prepareIndex(defaultIndexName) + .setId("point1") + .setSource(jsonBuilder().startObject().field(defaultFieldName, "POINT(-30 -30)").endObject()) + .setRefreshPolicy(IMMEDIATE) + .get(); - client().prepareIndex(defaultIndexName).setId("point2").setSource(jsonBuilder() - .startObject() - .field(defaultFieldName, "POINT(-45 -50)") - .endObject()).setRefreshPolicy(IMMEDIATE).get(); + client().prepareIndex(defaultIndexName) + .setId("point2") + .setSource(jsonBuilder().startObject().field(defaultFieldName, "POINT(-45 -50)").endObject()) + .setRefreshPolicy(IMMEDIATE) + .get(); String indexedShapeIndex = "indexed_query_shapes"; String indexedShapePath = "shape"; - String queryShapesMapping = Strings.toString(XContentFactory.jsonBuilder().startObject() - .startObject("properties").startObject(indexedShapePath) - .field("type", "shape") - .endObject() - .endObject() - .endObject()); + String queryShapesMapping = Strings.toString( + XContentFactory.jsonBuilder() + .startObject() + .startObject("properties") + .startObject(indexedShapePath) + .field("type", "shape") + .endObject() + .endObject() + .endObject() + ); client().admin().indices().prepareCreate(indexedShapeIndex).setMapping(queryShapesMapping).get(); ensureGreen(); - client().prepareIndex(indexedShapeIndex).setId("shape1").setSource(jsonBuilder() - .startObject() - .field(indexedShapePath, "BBOX(-50, -40, -45, -55)") - .endObject()).setRefreshPolicy(IMMEDIATE).get(); + client().prepareIndex(indexedShapeIndex) + .setId("shape1") + .setSource(jsonBuilder().startObject().field(indexedShapePath, "BBOX(-50, -40, -45, -55)").endObject()) + .setRefreshPolicy(IMMEDIATE) + .get(); - client().prepareIndex(indexedShapeIndex).setId("shape2").setSource(jsonBuilder() - .startObject() - .field(indexedShapePath, "BBOX(-60, -50, -50, -60)") - .endObject()).setRefreshPolicy(IMMEDIATE).get(); + client().prepareIndex(indexedShapeIndex) + .setId("shape2") + .setSource(jsonBuilder().startObject().field(indexedShapePath, "BBOX(-60, -50, -50, -60)").endObject()) + .setRefreshPolicy(IMMEDIATE) + .get(); SearchResponse searchResponse = client().prepareSearch(defaultIndexName) - .setQuery(new ShapeQueryBuilder(defaultFieldName, "shape1") - .relation(ShapeRelation.INTERSECTS) - .indexedShapeIndex(indexedShapeIndex) - .indexedShapePath(indexedShapePath)) + .setQuery( + new ShapeQueryBuilder(defaultFieldName, "shape1").relation(ShapeRelation.INTERSECTS) + .indexedShapeIndex(indexedShapeIndex) + .indexedShapePath(indexedShapePath) + ) .get(); assertSearchResponse(searchResponse); @@ -295,34 +289,42 @@ public void testIndexPointsIndexedRectangle() throws Exception { assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("point2")); searchResponse = client().prepareSearch(defaultIndexName) - .setQuery(new ShapeQueryBuilder(defaultFieldName, "shape2") - .relation(ShapeRelation.INTERSECTS) - .indexedShapeIndex(indexedShapeIndex) - .indexedShapePath(indexedShapePath)) + .setQuery( + new ShapeQueryBuilder(defaultFieldName, "shape2").relation(ShapeRelation.INTERSECTS) + .indexedShapeIndex(indexedShapeIndex) + .indexedShapePath(indexedShapePath) + ) .get(); assertSearchResponse(searchResponse); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(0L)); } public void testDistanceQuery() throws Exception { - client().admin().indices().prepareCreate("test_distance").setMapping("location", "type=shape") - .execute().actionGet(); + client().admin().indices().prepareCreate("test_distance").setMapping("location", "type=shape").execute().actionGet(); ensureGreen(); Circle circle = new Circle(1, 0, 10); - client().index(new IndexRequest("test_distance") - .source(jsonBuilder().startObject().field("location", WellKnownText.toWKT(new Point(2, 2))).endObject()) - .setRefreshPolicy(IMMEDIATE)).actionGet(); - client().index(new IndexRequest("test_distance") - .source(jsonBuilder().startObject().field("location", WellKnownText.toWKT(new Point(3, 1))).endObject()) - .setRefreshPolicy(IMMEDIATE)).actionGet(); - client().index(new IndexRequest("test_distance") - .source(jsonBuilder().startObject().field("location", WellKnownText.toWKT(new Point(-20, -30))).endObject()) - .setRefreshPolicy(IMMEDIATE)).actionGet(); - client().index(new IndexRequest("test_distance") - .source(jsonBuilder().startObject().field("location", WellKnownText.toWKT(new Point(20, 30))).endObject()) - .setRefreshPolicy(IMMEDIATE)).actionGet(); + client().index( + new IndexRequest("test_distance").source( + jsonBuilder().startObject().field("location", WellKnownText.toWKT(new Point(2, 2))).endObject() + ).setRefreshPolicy(IMMEDIATE) + ).actionGet(); + client().index( + new IndexRequest("test_distance").source( + jsonBuilder().startObject().field("location", WellKnownText.toWKT(new Point(3, 1))).endObject() + ).setRefreshPolicy(IMMEDIATE) + ).actionGet(); + client().index( + new IndexRequest("test_distance").source( + jsonBuilder().startObject().field("location", WellKnownText.toWKT(new Point(-20, -30))).endObject() + ).setRefreshPolicy(IMMEDIATE) + ).actionGet(); + client().index( + new IndexRequest("test_distance").source( + jsonBuilder().startObject().field("location", WellKnownText.toWKT(new Point(20, 30))).endObject() + ).setRefreshPolicy(IMMEDIATE) + ).actionGet(); SearchResponse response = client().prepareSearch("test_distance") .setQuery(new ShapeQueryBuilder("location", circle).relation(ShapeRelation.WITHIN)) diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/SpatialPlugin.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/SpatialPlugin.java index 307ee130e5ed4..064e43e2b9e90 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/SpatialPlugin.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/SpatialPlugin.java @@ -10,7 +10,6 @@ import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionResponse; import org.elasticsearch.common.geo.GeoFormatterFactory; -import org.elasticsearch.xcontent.ContextParser; import org.elasticsearch.geometry.Geometry; import org.elasticsearch.index.mapper.Mapper; import org.elasticsearch.ingest.Processor; @@ -33,6 +32,7 @@ import org.elasticsearch.search.aggregations.metrics.ValueCountAggregationBuilder; import org.elasticsearch.search.aggregations.metrics.ValueCountAggregator; import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry; +import org.elasticsearch.xcontent.ContextParser; import org.elasticsearch.xpack.core.XPackPlugin; import org.elasticsearch.xpack.core.action.XPackInfoFeatureAction; import org.elasticsearch.xpack.core.action.XPackUsageFeatureAction; @@ -71,17 +71,27 @@ public class SpatialPlugin extends Plugin implements ActionPlugin, MapperPlugin, SearchPlugin, IngestPlugin, ExtensiblePlugin { private final SpatialUsage usage = new SpatialUsage(); - private final LicensedFeature.Momentary GEO_CENTROID_AGG_FEATURE = - LicensedFeature.momentary("spatial", "geo-centroid-agg", License.OperationMode.GOLD); - private final LicensedFeature.Momentary GEO_GRID_AGG_FEATURE = - LicensedFeature.momentary("spatial", "geo-grid-agg", License.OperationMode.GOLD); - private final LicensedFeature.Momentary GEO_LINE_AGG_FEATURE = - LicensedFeature.momentary("spatial", "geo-line-agg", License.OperationMode.GOLD); + private final LicensedFeature.Momentary GEO_CENTROID_AGG_FEATURE = LicensedFeature.momentary( + "spatial", + "geo-centroid-agg", + License.OperationMode.GOLD + ); + private final LicensedFeature.Momentary GEO_GRID_AGG_FEATURE = LicensedFeature.momentary( + "spatial", + "geo-grid-agg", + License.OperationMode.GOLD + ); + private final LicensedFeature.Momentary GEO_LINE_AGG_FEATURE = LicensedFeature.momentary( + "spatial", + "geo-line-agg", + License.OperationMode.GOLD + ); // to be overriden by tests protected XPackLicenseState getLicenseState() { return XPackPlugin.getSharedLicenseState(); } + // register the vector tile factory from a different module private final SetOnce> geoFormatterFactory = new SetOnce<>(); @@ -90,15 +100,19 @@ protected XPackLicenseState getLicenseState() { return Arrays.asList( new ActionPlugin.ActionHandler<>(XPackUsageFeatureAction.SPATIAL, SpatialUsageTransportAction.class), new ActionPlugin.ActionHandler<>(XPackInfoFeatureAction.SPATIAL, SpatialInfoTransportAction.class), - new ActionPlugin.ActionHandler<>(SpatialStatsAction.INSTANCE, SpatialStatsTransportAction.class)); + new ActionPlugin.ActionHandler<>(SpatialStatsAction.INSTANCE, SpatialStatsTransportAction.class) + ); } @Override public Map getMappers() { return Map.of( - ShapeFieldMapper.CONTENT_TYPE, ShapeFieldMapper.PARSER, - PointFieldMapper.CONTENT_TYPE, PointFieldMapper.PARSER, - GeoShapeWithDocValuesFieldMapper.CONTENT_TYPE, new GeoShapeWithDocValuesFieldMapper.TypeParser(geoFormatterFactory.get()) + ShapeFieldMapper.CONTENT_TYPE, + ShapeFieldMapper.PARSER, + PointFieldMapper.CONTENT_TYPE, + PointFieldMapper.PARSER, + GeoShapeWithDocValuesFieldMapper.CONTENT_TYPE, + new GeoShapeWithDocValuesFieldMapper.TypeParser(geoFormatterFactory.get()) ); } @@ -122,12 +136,11 @@ public List> getAggregationExtentions() { public List getAggregations() { return List.of( new AggregationSpec( - GeoLineAggregationBuilder.NAME, - GeoLineAggregationBuilder::new, - usage.track(SpatialStatsAction.Item.GEOLINE, - checkLicense(GeoLineAggregationBuilder.PARSER, GEO_LINE_AGG_FEATURE))) - .addResultReader(InternalGeoLine::new) - .setAggregatorRegistrar(GeoLineAggregationBuilder::registerUsage)); + GeoLineAggregationBuilder.NAME, + GeoLineAggregationBuilder::new, + usage.track(SpatialStatsAction.Item.GEOLINE, checkLicense(GeoLineAggregationBuilder.PARSER, GEO_LINE_AGG_FEATURE)) + ).addResultReader(InternalGeoLine::new).setAggregatorRegistrar(GeoLineAggregationBuilder::registerUsage) + ); } @Override @@ -145,9 +158,10 @@ private static void registerGeoShapeBoundsAggregator(ValuesSourceRegistry.Builde } private void registerGeoShapeCentroidAggregator(ValuesSourceRegistry.Builder builder) { - builder.register(GeoCentroidAggregationBuilder.REGISTRY_KEY, GeoShapeValuesSourceType.instance(), - (name, valuesSourceConfig, context, parent, metadata) - -> { + builder.register( + GeoCentroidAggregationBuilder.REGISTRY_KEY, + GeoShapeValuesSourceType.instance(), + (name, valuesSourceConfig, context, parent, metadata) -> { if (GEO_CENTROID_AGG_FEATURE.check(getLicenseState())) { return new GeoShapeCentroidAggregator(name, context, parent, valuesSourceConfig, metadata); } @@ -158,9 +172,21 @@ private void registerGeoShapeCentroidAggregator(ValuesSourceRegistry.Builder bui } private void registerGeoShapeGridAggregators(ValuesSourceRegistry.Builder builder) { - builder.register(GeoHashGridAggregationBuilder.REGISTRY_KEY, GeoShapeValuesSourceType.instance(), - (name, factories, valuesSource, precision, geoBoundingBox, requiredSize, shardSize, - aggregationContext, parent, collectsFromSingleBucket, metadata) -> { + builder.register( + GeoHashGridAggregationBuilder.REGISTRY_KEY, + GeoShapeValuesSourceType.instance(), + ( + name, + factories, + valuesSource, + precision, + geoBoundingBox, + requiredSize, + shardSize, + aggregationContext, + parent, + collectsFromSingleBucket, + metadata) -> { if (GEO_GRID_AGG_FEATURE.check(getLicenseState())) { final GeoGridTiler tiler; if (geoBoundingBox.isUnbounded()) { @@ -169,8 +195,17 @@ private void registerGeoShapeGridAggregators(ValuesSourceRegistry.Builder builde tiler = new BoundedGeoHashGridTiler(precision, geoBoundingBox); } GeoShapeCellIdSource cellIdSource = new GeoShapeCellIdSource((GeoShapeValuesSource) valuesSource, tiler); - GeoShapeHashGridAggregator agg = new GeoShapeHashGridAggregator(name, factories, cellIdSource, requiredSize, shardSize, - aggregationContext, parent, collectsFromSingleBucket, metadata); + GeoShapeHashGridAggregator agg = new GeoShapeHashGridAggregator( + name, + factories, + cellIdSource, + requiredSize, + shardSize, + aggregationContext, + parent, + collectsFromSingleBucket, + metadata + ); // this would ideally be something set in an immutable way on the ValuesSource cellIdSource.setCircuitBreakerConsumer(agg::addRequestBytes); return agg; @@ -180,9 +215,21 @@ private void registerGeoShapeGridAggregators(ValuesSourceRegistry.Builder builde true ); - builder.register(GeoTileGridAggregationBuilder.REGISTRY_KEY, GeoShapeValuesSourceType.instance(), - (name, factories, valuesSource, precision, geoBoundingBox, requiredSize, shardSize, - context, parent, collectsFromSingleBucket, metadata) -> { + builder.register( + GeoTileGridAggregationBuilder.REGISTRY_KEY, + GeoShapeValuesSourceType.instance(), + ( + name, + factories, + valuesSource, + precision, + geoBoundingBox, + requiredSize, + shardSize, + context, + parent, + collectsFromSingleBucket, + metadata) -> { if (GEO_GRID_AGG_FEATURE.check(getLicenseState())) { final GeoGridTiler tiler; if (geoBoundingBox.isUnbounded()) { @@ -191,8 +238,17 @@ private void registerGeoShapeGridAggregators(ValuesSourceRegistry.Builder builde tiler = new BoundedGeoTileGridTiler(precision, geoBoundingBox); } GeoShapeCellIdSource cellIdSource = new GeoShapeCellIdSource((GeoShapeValuesSource) valuesSource, tiler); - GeoShapeTileGridAggregator agg = new GeoShapeTileGridAggregator(name, factories, cellIdSource, requiredSize, shardSize, - context, parent, collectsFromSingleBucket, metadata); + GeoShapeTileGridAggregator agg = new GeoShapeTileGridAggregator( + name, + factories, + cellIdSource, + requiredSize, + shardSize, + context, + parent, + collectsFromSingleBucket, + metadata + ); // this would ideally be something set in an immutable way on the ValuesSource cellIdSource.setCircuitBreakerConsumer(agg::addRequestBytes); return agg; @@ -224,7 +280,9 @@ private ContextParser checkLicense(ContextParser realP public void loadExtensions(ExtensionLoader loader) { // we only expect one vector tile extension that comes from the vector tile module. List> formatterFactories = new ArrayList<>(); - loader.loadExtensions(GeometryFormatterExtension.class).stream().map(GeometryFormatterExtension::getGeometryFormatterFactories) + loader.loadExtensions(GeometryFormatterExtension.class) + .stream() + .map(GeometryFormatterExtension::getGeometryFormatterFactories) .forEach(formatterFactories::addAll); geoFormatterFactory.set(new GeoFormatterFactory<>(formatterFactories)); } diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/SpatialUsage.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/SpatialUsage.java index 3c1b9bebd4a59..d1a9f0e5cf7a6 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/SpatialUsage.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/SpatialUsage.java @@ -19,8 +19,7 @@ public class SpatialUsage { private final EnumCounters counters = new EnumCounters<>(SpatialStatsAction.Item.class); - public SpatialUsage() { - } + public SpatialUsage() {} /** * Track successful parsing. diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/SpatialUtils.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/SpatialUtils.java index 080ca9f05a693..5d263926cd025 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/SpatialUtils.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/SpatialUtils.java @@ -7,10 +7,10 @@ package org.elasticsearch.xpack.spatial; import org.apache.lucene.util.SloppyMath; -import org.elasticsearch.index.mapper.GeoShapeIndexer; import org.elasticsearch.geometry.Circle; import org.elasticsearch.geometry.LinearRing; import org.elasticsearch.geometry.Polygon; +import org.elasticsearch.index.mapper.GeoShapeIndexer; /** * Utility class for storing different helpful re-usable spatial functions @@ -32,17 +32,19 @@ private SpatialUtils() {} * */ public static Polygon createRegularGeoShapePolygon(Circle circle, int gons) { if (SloppyMath.haversinMeters(circle.getLat(), circle.getLon(), 90, 0) < circle.getRadiusMeters()) { - throw new IllegalArgumentException("circle [" + circle.toString() + "] contains the north pole. " + - "It cannot be translated to a polygon"); + throw new IllegalArgumentException( + "circle [" + circle.toString() + "] contains the north pole. " + "It cannot be translated to a polygon" + ); } if (SloppyMath.haversinMeters(circle.getLat(), circle.getLon(), -90, 0) < circle.getRadiusMeters()) { - throw new IllegalArgumentException("circle [" + circle.toString() + "] contains the south pole. " + - "It cannot be translated to a polygon"); + throw new IllegalArgumentException( + "circle [" + circle.toString() + "] contains the south pole. " + "It cannot be translated to a polygon" + ); } double[][] result = new double[2][]; - result[0] = new double[gons+1]; - result[1] = new double[gons+1]; - for(int i=0; i { +public class SpatialStatsTransportAction extends TransportNodesAction< + SpatialStatsAction.Request, + SpatialStatsAction.Response, + SpatialStatsAction.NodeRequest, + SpatialStatsAction.NodeResponse> { private final SpatialUsage usage; @Inject - public SpatialStatsTransportAction(TransportService transportService, ClusterService clusterService, - ThreadPool threadPool, ActionFilters actionFilters, SpatialUsage usage) { - super(SpatialStatsAction.NAME, threadPool, clusterService, transportService, actionFilters, - SpatialStatsAction.Request::new, SpatialStatsAction.NodeRequest::new, ThreadPool.Names.MANAGEMENT, - SpatialStatsAction.NodeResponse.class); + public SpatialStatsTransportAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + SpatialUsage usage + ) { + super( + SpatialStatsAction.NAME, + threadPool, + clusterService, + transportService, + actionFilters, + SpatialStatsAction.Request::new, + SpatialStatsAction.NodeRequest::new, + ThreadPool.Names.MANAGEMENT, + SpatialStatsAction.NodeResponse.class + ); this.usage = usage; } @Override - protected SpatialStatsAction.Response newResponse(SpatialStatsAction.Request request, - List nodes, - List failures) { + protected SpatialStatsAction.Response newResponse( + SpatialStatsAction.Request request, + List nodes, + List failures + ) { return new SpatialStatsAction.Response(clusterService.getClusterName(), nodes, failures); } diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/action/SpatialUsageTransportAction.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/action/SpatialUsageTransportAction.java index de73101bddf5f..65d22856b5d89 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/action/SpatialUsageTransportAction.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/action/SpatialUsageTransportAction.java @@ -28,21 +28,38 @@ public class SpatialUsageTransportAction extends XPackUsageFeatureTransportActio private final Client client; @Inject - public SpatialUsageTransportAction(TransportService transportService, ClusterService clusterService, ThreadPool threadPool, - ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver, - Client client) { - super(XPackUsageFeatureAction.SPATIAL.name(), transportService, clusterService, - threadPool, actionFilters, indexNameExpressionResolver); + public SpatialUsageTransportAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver, + Client client + ) { + super( + XPackUsageFeatureAction.SPATIAL.name(), + transportService, + clusterService, + threadPool, + actionFilters, + indexNameExpressionResolver + ); this.client = client; } @Override - protected void masterOperation(Task task, XPackUsageRequest request, ClusterState state, - ActionListener listener) { + protected void masterOperation( + Task task, + XPackUsageRequest request, + ClusterState state, + ActionListener listener + ) { SpatialStatsAction.Request statsRequest = new SpatialStatsAction.Request(); statsRequest.setParentTask(clusterService.localNode().getId(), task.getId()); - client.execute(SpatialStatsAction.INSTANCE, statsRequest, ActionListener.wrap(r -> - listener.onResponse(new XPackUsageFeatureResponse(new SpatialFeatureSetUsage(r))), - listener::onFailure)); + client.execute( + SpatialStatsAction.INSTANCE, + statsRequest, + ActionListener.wrap(r -> listener.onResponse(new XPackUsageFeatureResponse(new SpatialFeatureSetUsage(r))), listener::onFailure) + ); } } diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/common/CartesianPoint.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/common/CartesianPoint.java index 9ff460e1ac038..eed96ba9fff16 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/common/CartesianPoint.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/common/CartesianPoint.java @@ -8,18 +8,18 @@ package org.elasticsearch.xpack.spatial.common; import org.elasticsearch.ElasticsearchParseException; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.geometry.Geometry; +import org.elasticsearch.geometry.ShapeType; +import org.elasticsearch.geometry.utils.StandardValidator; +import org.elasticsearch.geometry.utils.WellKnownText; import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentFragment; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentSubParser; import org.elasticsearch.xcontent.support.MapXContentParser; -import org.elasticsearch.geometry.Geometry; -import org.elasticsearch.geometry.ShapeType; -import org.elasticsearch.geometry.utils.StandardValidator; -import org.elasticsearch.geometry.utils.WellKnownText; import org.elasticsearch.xpack.spatial.index.mapper.PointFieldMapper; import java.io.IOException; @@ -39,8 +39,7 @@ public class CartesianPoint implements ToXContentFragment { protected double x; protected double y; - public CartesianPoint() { - } + public CartesianPoint() {} public CartesianPoint(double x, double y) { this.x = x; @@ -61,33 +60,37 @@ public CartesianPoint resetFromString(String value, final boolean ignoreZValue) } } - public CartesianPoint resetFromCoordinates(String value, final boolean ignoreZValue) { String[] vals = value.split(","); if (vals.length > 3 || vals.length < 2) { - throw new ElasticsearchParseException("failed to parse [{}], expected 2 or 3 coordinates " - + "but found: [{}]", vals, vals.length); + throw new ElasticsearchParseException( + "failed to parse [{}], expected 2 or 3 coordinates " + "but found: [{}]", + vals, + vals.length + ); } final double x; final double y; try { x = Double.parseDouble(vals[0].trim()); if (Double.isFinite(x) == false) { - throw new ElasticsearchParseException("invalid [{}] value [{}]; " + - "must be between -3.4028234663852886E38 and 3.4028234663852886E38", + throw new ElasticsearchParseException( + "invalid [{}] value [{}]; " + "must be between -3.4028234663852886E38 and 3.4028234663852886E38", X_FIELD.getPreferredName(), - x); + x + ); } - } catch (NumberFormatException ex) { + } catch (NumberFormatException ex) { throw new ElasticsearchParseException("[{}]] must be a number", X_FIELD.getPreferredName()); } try { y = Double.parseDouble(vals[1].trim()); if (Double.isFinite(y) == false) { - throw new ElasticsearchParseException("invalid [{}] value [{}]; " + - "must be between -3.4028234663852886E38 and 3.4028234663852886E38", + throw new ElasticsearchParseException( + "invalid [{}] value [{}]; " + "must be between -3.4028234663852886E38 and 3.4028234663852886E38", Y_FIELD.getPreferredName(), - y); + y + ); } } catch (NumberFormatException ex) { throw new ElasticsearchParseException("[{}]] must be a number", Y_FIELD.getPreferredName()); @@ -110,8 +113,11 @@ private CartesianPoint resetFromWKT(String value, boolean ignoreZValue) { throw new ElasticsearchParseException("Invalid WKT format", e); } if (geometry.type() != ShapeType.POINT) { - throw new ElasticsearchParseException("[{}] supports only POINT among WKT primitives, " + - "but found {}", PointFieldMapper.CONTENT_TYPE, geometry.type()); + throw new ElasticsearchParseException( + "[{}] supports only POINT among WKT primitives, " + "but found {}", + PointFieldMapper.CONTENT_TYPE, + geometry.type() + ); } org.elasticsearch.geometry.Point point = (org.elasticsearch.geometry.Point) geometry; return reset(point.getX(), point.getY()); @@ -153,13 +159,13 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws return builder.startObject().field(X_FIELD.getPreferredName(), x).field(Y_FIELD.getPreferredName(), y).endObject(); } - public static CartesianPoint parsePoint(XContentParser parser, CartesianPoint point, boolean ignoreZvalue) - throws IOException, ElasticsearchParseException { + public static CartesianPoint parsePoint(XContentParser parser, CartesianPoint point, boolean ignoreZvalue) throws IOException, + ElasticsearchParseException { double x = Double.NaN; double y = Double.NaN; NumberFormatException numberFormatException = null; - if(parser.currentToken() == XContentParser.Token.START_OBJECT) { + if (parser.currentToken() == XContentParser.Token.START_OBJECT) { try (XContentSubParser subParser = new XContentSubParser(parser)) { while (subParser.nextToken() != XContentParser.Token.END_OBJECT) { if (subParser.currentToken() == XContentParser.Token.FIELD_NAME) { @@ -176,8 +182,7 @@ public static CartesianPoint parsePoint(XContentParser parser, CartesianPoint po } break; default: - throw new ElasticsearchParseException("[{}] must be a number", - X_FIELD.getPreferredName()); + throw new ElasticsearchParseException("[{}] must be a number", X_FIELD.getPreferredName()); } } else if (field.equals(Y_FIELD.getPreferredName())) { subParser.nextToken(); @@ -191,8 +196,7 @@ public static CartesianPoint parsePoint(XContentParser parser, CartesianPoint po } break; default: - throw new ElasticsearchParseException("[{}] must be a number", - Y_FIELD.getPreferredName()); + throw new ElasticsearchParseException("[{}] must be a number", Y_FIELD.getPreferredName()); } } else if (field.equals(Z_FIELD.getPreferredName())) { subParser.nextToken(); @@ -200,29 +204,33 @@ public static CartesianPoint parsePoint(XContentParser parser, CartesianPoint po case VALUE_NUMBER: case VALUE_STRING: try { - CartesianPoint.assertZValue(ignoreZvalue, subParser.doubleValue(true)); + CartesianPoint.assertZValue(ignoreZvalue, subParser.doubleValue(true)); } catch (NumberFormatException e) { numberFormatException = e; } break; default: - throw new ElasticsearchParseException("[{}] must be a number", - Z_FIELD.getPreferredName()); + throw new ElasticsearchParseException("[{}] must be a number", Z_FIELD.getPreferredName()); } } else { - throw new ElasticsearchParseException("field must be either [{}] or [{}]", + throw new ElasticsearchParseException( + "field must be either [{}] or [{}]", X_FIELD.getPreferredName(), - Y_FIELD.getPreferredName()); + Y_FIELD.getPreferredName() + ); } } else { throw new ElasticsearchParseException("token [{}] not allowed", subParser.currentToken()); } } } - if (numberFormatException != null) { - throw new ElasticsearchParseException("[{}] and [{}] must be valid double values", numberFormatException, + if (numberFormatException != null) { + throw new ElasticsearchParseException( + "[{}] and [{}] must be valid double values", + numberFormatException, X_FIELD.getPreferredName(), - Y_FIELD.getPreferredName()); + Y_FIELD.getPreferredName() + ); } else if (Double.isNaN(x)) { throw new ElasticsearchParseException("field [{}] missing", X_FIELD.getPreferredName()); } else if (Double.isNaN(y)) { @@ -231,7 +239,7 @@ public static CartesianPoint parsePoint(XContentParser parser, CartesianPoint po return point.reset(x, y); } - } else if(parser.currentToken() == XContentParser.Token.START_ARRAY) { + } else if (parser.currentToken() == XContentParser.Token.START_ARRAY) { try (XContentSubParser subParser = new XContentSubParser(parser)) { int element = 0; while (subParser.nextToken() != XContentParser.Token.END_ARRAY) { @@ -242,8 +250,10 @@ public static CartesianPoint parsePoint(XContentParser parser, CartesianPoint po } else if (element == 2) { y = subParser.doubleValue(); } else { - throw new ElasticsearchParseException("[{}}] field type does not accept > 2 dimensions", - PointFieldMapper.CONTENT_TYPE); + throw new ElasticsearchParseException( + "[{}}] field type does not accept > 2 dimensions", + PointFieldMapper.CONTENT_TYPE + ); } } else { throw new ElasticsearchParseException("numeric value expected"); @@ -251,7 +261,7 @@ public static CartesianPoint parsePoint(XContentParser parser, CartesianPoint po } } return point.reset(x, y); - } else if(parser.currentToken() == XContentParser.Token.VALUE_STRING) { + } else if (parser.currentToken() == XContentParser.Token.VALUE_STRING) { String val = parser.text(); return point.resetFromString(val, ignoreZvalue); } else { @@ -264,8 +274,14 @@ public static CartesianPoint parsePoint(Object value, boolean ignoreZValue) thro } public static CartesianPoint parsePoint(Object value, CartesianPoint point, boolean ignoreZValue) throws ElasticsearchParseException { - try (XContentParser parser = new MapXContentParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, - Collections.singletonMap("null_value", value), null)) { + try ( + XContentParser parser = new MapXContentParser( + NamedXContentRegistry.EMPTY, + LoggingDeprecationHandler.INSTANCE, + Collections.singletonMap("null_value", value), + null + ) + ) { parser.nextToken(); // start object parser.nextToken(); // field name parser.nextToken(); // field value @@ -277,14 +293,18 @@ public static CartesianPoint parsePoint(Object value, CartesianPoint point, bool public static double assertZValue(final boolean ignoreZValue, double zValue) { if (ignoreZValue == false) { - throw new ElasticsearchParseException("Exception parsing coordinates: found Z value [{}] but [ignore_z_value] " - + "parameter is [{}]", zValue, ignoreZValue); + throw new ElasticsearchParseException( + "Exception parsing coordinates: found Z value [{}] but [ignore_z_value] " + "parameter is [{}]", + zValue, + ignoreZValue + ); } if (Double.isFinite(zValue) == false) { - throw new ElasticsearchParseException("invalid [{}] value [{}]; " + - "must be between -3.4028234663852886E38 and 3.4028234663852886E38", + throw new ElasticsearchParseException( + "invalid [{}] value [{}]; " + "must be between -3.4028234663852886E38 and 3.4028234663852886E38", Z_FIELD.getPreferredName(), - zValue); + zValue + ); } return zValue; } diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/common/ShapeUtils.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/common/ShapeUtils.java index 30a9b4e784749..289fbe6e707ca 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/common/ShapeUtils.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/common/ShapeUtils.java @@ -17,30 +17,32 @@ */ public class ShapeUtils { // no instance: - private ShapeUtils() { - } + private ShapeUtils() {} public static org.apache.lucene.geo.XYPolygon toLuceneXYPolygon(Polygon polygon) { org.apache.lucene.geo.XYPolygon[] holes = new org.apache.lucene.geo.XYPolygon[polygon.getNumberOfHoles()]; - for(int i = 0; i= 0 ? x : Integer.MAX_VALUE, - x >= 0 ? x : Integer.MIN_VALUE); + x >= 0 ? x : Integer.MIN_VALUE + ); } /** @@ -257,12 +260,12 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Extent extent = (Extent) o; - return top == extent.top && - bottom == extent.bottom && - negLeft == extent.negLeft && - negRight == extent.negRight && - posLeft == extent.posLeft && - posRight == extent.posRight; + return top == extent.top + && bottom == extent.bottom + && negLeft == extent.negLeft + && negRight == extent.negRight + && posLeft == extent.posLeft + && posRight == extent.posRight; } @Override diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/GeoShapeValues.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/GeoShapeValues.java index bb9f64f7af874..aaa6b15758566 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/GeoShapeValues.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/GeoShapeValues.java @@ -8,14 +8,14 @@ package org.elasticsearch.xpack.spatial.index.fielddata; import org.apache.lucene.util.BytesRef; -import org.elasticsearch.xcontent.ToXContentFragment; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.geometry.Geometry; import org.elasticsearch.geometry.Rectangle; import org.elasticsearch.geometry.utils.GeographyValidator; import org.elasticsearch.geometry.utils.WellKnownText; import org.elasticsearch.index.mapper.GeoShapeIndexer; import org.elasticsearch.search.aggregations.support.ValuesSourceType; +import org.elasticsearch.xcontent.ToXContentFragment; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.spatial.index.mapper.BinaryGeoShapeDocValuesField; import org.elasticsearch.xpack.spatial.search.aggregations.support.GeoShapeValuesSourceType; @@ -41,6 +41,7 @@ public abstract class GeoShapeValues { public static GeoShapeValues EMPTY = new GeoShapeValues() { private final GeoShapeValuesSourceType DEFAULT_VALUES_SOURCE_TYPE = GeoShapeValuesSourceType.instance(); + @Override public boolean advanceExact(int doc) { return false; @@ -60,8 +61,7 @@ public GeoShapeValue value() { /** * Creates a new {@link GeoShapeValues} instance */ - protected GeoShapeValues() { - } + protected GeoShapeValues() {} /** * Advance this instance to the given document id @@ -69,7 +69,6 @@ protected GeoShapeValues() { */ public abstract boolean advanceExact(int doc) throws IOException; - public abstract ValuesSourceType valuesSourceType(); /** @@ -89,7 +88,7 @@ public static class GeoShapeValue implements ToXContentFragment { private final BoundingBox boundingBox; private final Tile2DVisitor tile2DVisitor; - public GeoShapeValue() { + public GeoShapeValue() { this.reader = new GeometryDocValueReader(); this.boundingBox = new BoundingBox(); this.tile2DVisitor = new Tile2DVisitor(); @@ -141,8 +140,9 @@ public double lon() throws IOException { public static GeoShapeValue missing(String missing) { try { - final Geometry geometry = - MISSING_GEOSHAPE_INDEXER.prepareForIndexing(WellKnownText.fromWKT(GeographyValidator.instance(true), true, missing)); + final Geometry geometry = MISSING_GEOSHAPE_INDEXER.prepareForIndexing( + WellKnownText.fromWKT(GeographyValidator.instance(true), true, missing) + ); final BinaryGeoShapeDocValuesField field = new BinaryGeoShapeDocValuesField("missing"); field.add(MISSING_GEOSHAPE_INDEXER.indexShape(geometry), geometry); final GeoShapeValue value = new GeoShapeValue(); @@ -167,8 +167,7 @@ public static class BoundingBox { public double posLeft; public double posRight; - private BoundingBox() { - } + private BoundingBox() {} private void reset(Extent extent, CoordinateEncoder coordinateEncoder) { this.top = coordinateEncoder.decodeY(extent.top); diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/GeometryDocValueReader.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/GeometryDocValueReader.java index ec19ccb8179cd..04c9216e484cb 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/GeometryDocValueReader.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/GeometryDocValueReader.java @@ -104,7 +104,7 @@ public void visit(TriangleTreeReader.Visitor visitor) throws IOException { int thisMinX = extent.minX(); int thisMaxY = extent.maxY(); int thisMinY = extent.minY(); - if(visitor.push(thisMinX, thisMinY, thisMaxX, thisMaxY)) { + if (visitor.push(thisMinX, thisMinY, thisMaxX, thisMaxY)) { TriangleTreeReader.visit(input, visitor, thisMaxX, thisMaxY); } } diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/GeometryDocValueWriter.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/GeometryDocValueWriter.java index 393ec9ccbee06..d168ca9563b57 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/GeometryDocValueWriter.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/GeometryDocValueWriter.java @@ -20,13 +20,11 @@ */ public class GeometryDocValueWriter { - private GeometryDocValueWriter() { - } + private GeometryDocValueWriter() {} /*** Serialize the triangle tree in a BytesRef */ - public static BytesRef write(List fields, - CoordinateEncoder coordinateEncoder, - CentroidCalculator centroidCalculator) throws IOException { + public static BytesRef write(List fields, CoordinateEncoder coordinateEncoder, CentroidCalculator centroidCalculator) + throws IOException { final BytesStreamOutput out = new BytesStreamOutput(); // normalization may be required due to floating point precision errors out.writeInt(coordinateEncoder.encodeX(coordinateEncoder.normalizeX(centroidCalculator.getX()))); diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/IndexGeoShapeFieldData.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/IndexGeoShapeFieldData.java index db9aa7a96f781..2752fc2710203 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/IndexGeoShapeFieldData.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/IndexGeoShapeFieldData.java @@ -7,11 +7,9 @@ package org.elasticsearch.xpack.spatial.index.fielddata; - import org.elasticsearch.index.fielddata.IndexFieldData; /** * Specialization of {@link IndexFieldData} for geo shapes. */ -public interface IndexGeoShapeFieldData extends IndexFieldData { -} +public interface IndexGeoShapeFieldData extends IndexFieldData {} diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/Tile2DVisitor.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/Tile2DVisitor.java index a9db3f9d48047..8150e711cd9d4 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/Tile2DVisitor.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/Tile2DVisitor.java @@ -26,8 +26,7 @@ class Tile2DVisitor implements TriangleTreeReader.Visitor { private int minY; private int maxY; - Tile2DVisitor() { - } + Tile2DVisitor() {} public void reset(int minX, int minY, int maxX, int maxY) { this.minX = minX; @@ -195,32 +194,31 @@ private GeoRelation relateTriangle(int aX, int aY, boolean ab, int bX, int bY, b */ private boolean edgeIntersectsQuery(int ax, int ay, int bx, int by) { // shortcut: check bboxes of edges are disjoint - if (boxesAreDisjoint(Math.min(ax, bx), Math.max(ax, bx), Math.min(ay, by), Math.max(ay, by), - minX, maxX, minY, maxY)) { + if (boxesAreDisjoint(Math.min(ax, bx), Math.max(ax, bx), Math.min(ay, by), Math.max(ay, by), minX, maxX, minY, maxY)) { return false; } // top - if (orient(ax, ay, bx, by, minX, maxY) * orient(ax, ay, bx, by, maxX, maxY) <= 0 && - orient(minX, maxY, maxX, maxY, ax, ay) * orient(minX, maxY, maxX, maxY, bx, by) <= 0) { + if (orient(ax, ay, bx, by, minX, maxY) * orient(ax, ay, bx, by, maxX, maxY) <= 0 + && orient(minX, maxY, maxX, maxY, ax, ay) * orient(minX, maxY, maxX, maxY, bx, by) <= 0) { return true; } // right - if (orient(ax, ay, bx, by, maxX, maxY) * orient(ax, ay, bx, by, maxX, minY) <= 0 && - orient(maxX, maxY, maxX, minY, ax, ay) * orient(maxX, maxY, maxX, minY, bx, by) <= 0) { + if (orient(ax, ay, bx, by, maxX, maxY) * orient(ax, ay, bx, by, maxX, minY) <= 0 + && orient(maxX, maxY, maxX, minY, ax, ay) * orient(maxX, maxY, maxX, minY, bx, by) <= 0) { return true; } // bottom - if (orient(ax, ay, bx, by, maxX, minY) * orient(ax, ay, bx, by, minX, minY) <= 0 && - orient(maxX, minY, minX, minY, ax, ay) * orient(maxX, minY, minX, minY, bx, by) <= 0) { + if (orient(ax, ay, bx, by, maxX, minY) * orient(ax, ay, bx, by, minX, minY) <= 0 + && orient(maxX, minY, minX, minY, ax, ay) * orient(maxX, minY, minX, minY, bx, by) <= 0) { return true; } // left - if (orient(ax, ay, bx, by, minX, minY) * orient(ax, ay, bx, by, minX, maxY) <= 0 && - orient(minX, minY, minX, maxY, ax, ay) * orient(minX, minY, minX, maxY, bx, by) <= 0) { + if (orient(ax, ay, bx, by, minX, minY) * orient(ax, ay, bx, by, minX, maxY) <= 0 + && orient(minX, minY, minX, maxY, ax, ay) * orient(minX, minY, minX, maxY, bx, by) <= 0) { return true; } @@ -230,10 +228,22 @@ private boolean edgeIntersectsQuery(int ax, int ay, int bx, int by) { /** * Compute whether the given x, y point is in a triangle; uses the winding order method */ - private static boolean pointInTriangle(double minX, double maxX, double minY, double maxY, double x, double y, - double aX, double aY, double bX, double bY, double cX, double cY) { - //check the bounding box because if the triangle is degenerated, e.g points and lines, we need to filter out - //coplanar points that are not part of the triangle. + private static boolean pointInTriangle( + double minX, + double maxX, + double minY, + double maxY, + double x, + double y, + double aX, + double aY, + double bX, + double bY, + double cX, + double cY + ) { + // check the bounding box because if the triangle is degenerated, e.g points and lines, we need to filter out + // coplanar points that are not part of the triangle. if (x >= minX && x <= maxX && y >= minY && y <= maxY) { int a = orient(x, y, aX, aY, bX, bY); int b = orient(x, y, bX, bY, cX, cY); @@ -250,8 +260,16 @@ private static boolean pointInTriangle(double minX, double maxX, double minY, do /** * utility method to check if two boxes are disjoint */ - private static boolean boxesAreDisjoint(final int aMinX, final int aMaxX, final int aMinY, final int aMaxY, - final int bMinX, final int bMaxX, final int bMinY, final int bMaxY) { + private static boolean boxesAreDisjoint( + final int aMinX, + final int aMaxX, + final int aMinY, + final int aMaxY, + final int bMinX, + final int bMaxX, + final int bMinY, + final int bMaxY + ) { return (aMaxX < bMinX || aMinX > bMaxX || aMaxY < bMinY || aMinY > bMaxY); } } diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/TriangleTreeReader.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/TriangleTreeReader.java index 24d791f513fe3..46e7baef0f08d 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/TriangleTreeReader.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/TriangleTreeReader.java @@ -20,8 +20,7 @@ */ class TriangleTreeReader { - private TriangleTreeReader() { - } + private TriangleTreeReader() {} /** * Visit the Triangle tree using the {@link Visitor} provided. @@ -31,8 +30,14 @@ public static void visit(ByteArrayStreamInput input, TriangleTreeReader.Visitor visit(input, visitor, true, thisMaxX, thisMaxY, true); } - private static boolean visit(ByteArrayStreamInput input, TriangleTreeReader.Visitor visitor, - boolean splitX, int thisMaxX, int thisMaxY, boolean isRoot) throws IOException { + private static boolean visit( + ByteArrayStreamInput input, + TriangleTreeReader.Visitor visitor, + boolean splitX, + int thisMaxX, + int thisMaxY, + boolean isRoot + ) throws IOException { byte metadata = input.readByte(); int thisMinX; int thisMinY; @@ -85,8 +90,13 @@ private static boolean visit(ByteArrayStreamInput input, TriangleTreeReader.Visi return visitor.push(); } - private static boolean pushLeft(ByteArrayStreamInput input, TriangleTreeReader.Visitor visitor, - int thisMaxX, int thisMaxY, boolean splitX) throws IOException { + private static boolean pushLeft( + ByteArrayStreamInput input, + TriangleTreeReader.Visitor visitor, + int thisMaxX, + int thisMaxY, + boolean splitX + ) throws IOException { int nextMaxX = Math.toIntExact(thisMaxX - input.readVLong()); int nextMaxY = Math.toIntExact(thisMaxY - input.readVLong()); int size = input.readVInt(); @@ -98,8 +108,16 @@ private static boolean pushLeft(ByteArrayStreamInput input, TriangleTreeReader.V } } - private static boolean pushRight(ByteArrayStreamInput input, TriangleTreeReader.Visitor visitor, int thisMaxX, - int thisMaxY, int thisMinX, int thisMinY, boolean splitX, int rightSize) throws IOException { + private static boolean pushRight( + ByteArrayStreamInput input, + TriangleTreeReader.Visitor visitor, + int thisMaxX, + int thisMaxY, + int thisMinX, + int thisMinY, + boolean splitX, + int rightSize + ) throws IOException { if ((splitX == false && visitor.pushY(thisMinY)) || (splitX && visitor.pushX(thisMinX))) { int nextMaxX = Math.toIntExact(thisMaxX - input.readVLong()); int nextMaxY = Math.toIntExact(thisMaxY - input.readVLong()); @@ -116,7 +134,7 @@ private static boolean pushRight(ByteArrayStreamInput input, TriangleTreeReader. } /** Visitor for triangle interval tree */ - interface Visitor { + interface Visitor { /** visit a node point. */ void visitPoint(int x, int y); diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/TriangleTreeWriter.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/TriangleTreeWriter.java index 5a637709deca2..4510101796549 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/TriangleTreeWriter.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/TriangleTreeWriter.java @@ -24,13 +24,13 @@ */ class TriangleTreeWriter { - private TriangleTreeWriter() { - } + private TriangleTreeWriter() {} /*** Serialize the interval tree in the provided data output */ public static void writeTo(StreamOutput out, List fields) throws IOException { final Extent extent = new Extent(); - final TriangleTreeNode node = build(fields, extent); ; + final TriangleTreeNode node = build(fields, extent); + ; extent.writeCompressed(out); node.writeTo(out); } @@ -38,7 +38,7 @@ public static void writeTo(StreamOutput out, List fields) throws private static TriangleTreeNode build(List fields, Extent extent) { final byte[] scratch = new byte[7 * Integer.BYTES]; if (fields.size() == 1) { - final TriangleTreeNode triangleTreeNode = new TriangleTreeNode(toDecodedTriangle(fields.get(0), scratch)); + final TriangleTreeNode triangleTreeNode = new TriangleTreeNode(toDecodedTriangle(fields.get(0), scratch)); extent.addRectangle(triangleTreeNode.minX, triangleTreeNode.minY, triangleTreeNode.maxX, triangleTreeNode.maxY); return triangleTreeNode; } @@ -127,8 +127,7 @@ private void writeTo(StreamOutput out) throws IOException { } } - private void writeNode(StreamOutput out, int parentMaxX, int parentMaxY, - BytesStreamOutput scratchBuffer) throws IOException { + private void writeNode(StreamOutput out, int parentMaxX, int parentMaxY, BytesStreamOutput scratchBuffer) throws IOException { out.writeVLong((long) parentMaxX - maxX); out.writeVLong((long) parentMaxY - maxY); int size = nodeSize(false, parentMaxX, parentMaxY, scratchBuffer); @@ -166,7 +165,7 @@ private void writeComponent(StreamOutput out) throws IOException { out.writeVLong((long) maxX - component.aX); out.writeVLong((long) maxY - component.aY); if (component.type == ShapeField.DecodedTriangle.TYPE.POINT) { - return; + return; } out.writeVLong((long) maxX - component.bX); out.writeVLong((long) maxY - component.bY); @@ -178,8 +177,8 @@ private void writeComponent(StreamOutput out) throws IOException { } private int nodeSize(boolean includeBox, int parentMaxX, int parentMaxY, BytesStreamOutput scratchBuffer) throws IOException { - int size =0; - size++; //metadata + int size = 0; + size++; // metadata size += componentSize(scratchBuffer); if (left != null) { size += left.nodeSize(true, maxX, maxY, scratchBuffer); diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/plain/AbstractAtomicGeoShapeShapeFieldData.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/plain/AbstractAtomicGeoShapeShapeFieldData.java index 921789a2bef41..768ff44ba853e 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/plain/AbstractAtomicGeoShapeShapeFieldData.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/plain/AbstractAtomicGeoShapeShapeFieldData.java @@ -50,8 +50,7 @@ public Collection getChildResources() { } @Override - public void close() { - } + public void close() {} @Override public GeoShapeValues getGeoShapeValues() { diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/plain/AbstractLatLonShapeIndexFieldData.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/plain/AbstractLatLonShapeIndexFieldData.java index 03629aef2c043..c258399ad24f2 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/plain/AbstractLatLonShapeIndexFieldData.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/fielddata/plain/AbstractLatLonShapeIndexFieldData.java @@ -12,8 +12,8 @@ import org.apache.lucene.index.LeafReader; import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.search.SortField; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.util.BigArrays; +import org.elasticsearch.core.Nullable; import org.elasticsearch.index.fielddata.IndexFieldData; import org.elasticsearch.index.fielddata.IndexFieldDataCache; import org.elasticsearch.indices.breaker.CircuitBreakerService; @@ -45,8 +45,12 @@ public ValuesSourceType getValuesSourceType() { } @Override - public SortField sortField(@Nullable Object missingValue, MultiValueMode sortMode, XFieldComparatorSource.Nested nested, - boolean reverse) { + public SortField sortField( + @Nullable Object missingValue, + MultiValueMode sortMode, + XFieldComparatorSource.Nested nested, + boolean reverse + ) { throw new IllegalArgumentException("can't sort on geo_shape field without using specific sorting feature, like geo_distance"); } @@ -71,20 +75,32 @@ public LeafGeoShapeFieldData loadDirect(LeafReaderContext context) throws Except } @Override - public BucketedSort newBucketedSort(BigArrays bigArrays, Object missingValue, MultiValueMode sortMode, - IndexFieldData.XFieldComparatorSource.Nested nested, SortOrder sortOrder, DocValueFormat format, - int bucketSize, BucketedSort.ExtraData extra) { + public BucketedSort newBucketedSort( + BigArrays bigArrays, + Object missingValue, + MultiValueMode sortMode, + IndexFieldData.XFieldComparatorSource.Nested nested, + SortOrder sortOrder, + DocValueFormat format, + int bucketSize, + BucketedSort.ExtraData extra + ) { throw new IllegalArgumentException("can't sort on geo_shape field without using specific sorting feature, like geo_distance"); } /** helper: checks a fieldinfo and throws exception if its definitely not a LatLonDocValuesField */ static void checkCompatible(FieldInfo fieldInfo) { // dv properties could be "unset", if you e.g. used only StoredField with this same name in the segment. - if (fieldInfo.getDocValuesType() != DocValuesType.NONE - && fieldInfo.getDocValuesType() != DocValuesType.BINARY) { - throw new IllegalArgumentException("field=\"" + fieldInfo.name + "\" was indexed with docValuesType=" - + fieldInfo.getDocValuesType() + " but this type has docValuesType=" - + DocValuesType.BINARY + ", is the field really a geo-shape field?"); + if (fieldInfo.getDocValuesType() != DocValuesType.NONE && fieldInfo.getDocValuesType() != DocValuesType.BINARY) { + throw new IllegalArgumentException( + "field=\"" + + fieldInfo.name + + "\" was indexed with docValuesType=" + + fieldInfo.getDocValuesType() + + " but this type has docValuesType=" + + DocValuesType.BINARY + + ", is the field really a geo-shape field?" + ); } } } diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/GeoShapeWithDocValuesFieldMapper.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/GeoShapeWithDocValuesFieldMapper.java index a1cd1af686d8d..4b929edcab452 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/GeoShapeWithDocValuesFieldMapper.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/GeoShapeWithDocValuesFieldMapper.java @@ -79,7 +79,7 @@ public class GeoShapeWithDocValuesFieldMapper extends AbstractShapeGeometryField private static final DeprecationLogger DEPRECATION_LOGGER = DeprecationLogger.getLogger(GeoShapeFieldMapper.class); private static Builder builder(FieldMapper in) { - return ((GeoShapeWithDocValuesFieldMapper)in).builder; + return ((GeoShapeWithDocValuesFieldMapper) in).builder; } public static class Builder extends FieldMapper.Builder { @@ -97,15 +97,19 @@ public static class Builder extends FieldMapper.Builder { private final Version version; private final GeoFormatterFactory geoFormatterFactory; - public Builder(String name, Version version, boolean ignoreMalformedByDefault, boolean coerceByDefault, - GeoFormatterFactory geoFormatterFactory) { + public Builder( + String name, + Version version, + boolean ignoreMalformedByDefault, + boolean coerceByDefault, + GeoFormatterFactory geoFormatterFactory + ) { super(name); this.version = version; this.geoFormatterFactory = geoFormatterFactory; this.ignoreMalformed = ignoreMalformedParam(m -> builder(m).ignoreMalformed.get(), ignoreMalformedByDefault); this.coerce = coerceParam(m -> builder(m).coerce.get(), coerceByDefault); - this.hasDocValues - = Parameter.docValuesParam(m -> builder(m).hasDocValues.get(), Version.V_7_8_0.onOrBefore(version)); + this.hasDocValues = Parameter.docValuesParam(m -> builder(m).hasDocValues.get(), Version.V_7_8_0.onOrBefore(version)); } @Override @@ -125,7 +129,8 @@ public GeoShapeWithDocValuesFieldMapper build(MapperBuilderContext context) { GeometryParser geometryParser = new GeometryParser( orientation.get().value().getAsBoolean(), coerce.get().value(), - ignoreZValue.get().value()); + ignoreZValue.get().value() + ); GeoShapeParser parser = new GeoShapeParser(geometryParser); GeoShapeWithDocValuesFieldType ft = new GeoShapeWithDocValuesFieldType( context.buildFullName(name), @@ -134,10 +139,17 @@ public GeoShapeWithDocValuesFieldMapper build(MapperBuilderContext context) { orientation.get().value(), parser, geoFormatterFactory, - meta.get()); - return new GeoShapeWithDocValuesFieldMapper(name, ft, - multiFieldsBuilder.build(this, context), copyTo.build(), - new GeoShapeIndexer(orientation.get().value().getAsBoolean(), ft.name()), parser, this); + meta.get() + ); + return new GeoShapeWithDocValuesFieldMapper( + name, + ft, + multiFieldsBuilder.build(this, context), + copyTo.build(), + new GeoShapeIndexer(orientation.get().value().getAsBoolean(), ft.name()), + parser, + this + ); } } @@ -145,9 +157,16 @@ public GeoShapeWithDocValuesFieldMapper build(MapperBuilderContext context) { public static final class GeoShapeWithDocValuesFieldType extends AbstractShapeGeometryFieldType implements GeoShapeQueryable { private final GeoFormatterFactory geoFormatterFactory; - public GeoShapeWithDocValuesFieldType(String name, boolean indexed, boolean hasDocValues, - Orientation orientation, GeoShapeParser parser, - GeoFormatterFactory geoFormatterFactory, Map meta) { + + public GeoShapeWithDocValuesFieldType( + String name, + boolean indexed, + boolean hasDocValues, + Orientation orientation, + GeoShapeParser parser, + GeoFormatterFactory geoFormatterFactory, + Map meta + ) { super(name, indexed, false, hasDocValues, parser, orientation, meta); this.geoFormatterFactory = geoFormatterFactory; } @@ -166,8 +185,10 @@ public String typeName() { public Query geoShapeQuery(Geometry shape, String fieldName, ShapeRelation relation, SearchExecutionContext context) { // CONTAINS queries are not supported by VECTOR strategy for indices created before version 7.5.0 (Lucene 8.3.0) if (relation == ShapeRelation.CONTAINS && context.indexVersionCreated().before(Version.V_7_5_0)) { - throw new QueryShardException(context, - ShapeRelation.CONTAINS + " query relation not supported for Field [" + fieldName + "]."); + throw new QueryShardException( + context, + ShapeRelation.CONTAINS + " query relation not supported for Field [" + fieldName + "]." + ); } final LatLonGeometry[] luceneGeometries = GeoShapeUtils.toLuceneGeometry(fieldName, context, shape, relation); if (luceneGeometries.length == 0) { @@ -176,7 +197,7 @@ public Query geoShapeQuery(Geometry shape, String fieldName, ShapeRelation relat Query query = LatLonShape.newGeometryQuery(fieldName, relation.getLuceneRelation(), luceneGeometries); if (hasDocValues()) { final Query queryDocValues = new LatLonShapeDocValuesQuery(fieldName, relation.getLuceneRelation(), luceneGeometries); - query = new IndexOrDocValuesQuery(query, queryDocValues); + query = new IndexOrDocValuesQuery(query, queryDocValues); } return query; } @@ -205,21 +226,28 @@ public Mapper.Builder parse(String name, Map node, MappingParser if (LegacyGeoShapeFieldMapper.containsDeprecatedParameter(node.keySet())) { if (parserContext.indexVersionCreated().onOrAfter(Version.V_8_0_0)) { Set deprecatedParams = LegacyGeoShapeFieldMapper.getDeprecatedParameters(node.keySet()); - throw new IllegalArgumentException("using deprecated parameters " + Arrays.toString(deprecatedParams.toArray()) - + " in mapper [" + name + "] of type [geo_shape] is no longer allowed"); + throw new IllegalArgumentException( + "using deprecated parameters " + + Arrays.toString(deprecatedParams.toArray()) + + " in mapper [" + + name + + "] of type [geo_shape] is no longer allowed" + ); } builder = new LegacyGeoShapeFieldMapper.Builder( name, parserContext.indexVersionCreated(), ignoreMalformedByDefault, - coerceByDefault); + coerceByDefault + ); } else { builder = new GeoShapeWithDocValuesFieldMapper.Builder( name, parserContext.indexVersionCreated(), ignoreMalformedByDefault, coerceByDefault, - geoFormatterFactory); + geoFormatterFactory + ); } builder.parse(name, parserContext, node); return builder; @@ -229,12 +257,26 @@ public Mapper.Builder parse(String name, Map node, MappingParser private final Builder builder; private final GeoShapeIndexer indexer; - public GeoShapeWithDocValuesFieldMapper(String simpleName, MappedFieldType mappedFieldType, - MultiFields multiFields, CopyTo copyTo, - GeoShapeIndexer indexer, GeoShapeParser parser, Builder builder) { - super(simpleName, mappedFieldType, builder.ignoreMalformed.get(), builder.coerce.get(), - builder.ignoreZValue.get(), builder.orientation.get(), - multiFields, copyTo, parser); + public GeoShapeWithDocValuesFieldMapper( + String simpleName, + MappedFieldType mappedFieldType, + MultiFields multiFields, + CopyTo copyTo, + GeoShapeIndexer indexer, + GeoShapeParser parser, + Builder builder + ) { + super( + simpleName, + mappedFieldType, + builder.ignoreMalformed.get(), + builder.coerce.get(), + builder.ignoreZValue.get(), + builder.orientation.get(), + multiFields, + copyTo, + parser + ); this.builder = builder; this.indexer = indexer; } @@ -286,8 +328,9 @@ public GeoShapeWithDocValuesFieldType fieldType() { @Override protected void checkIncomingMergeType(FieldMapper mergeWith) { if (mergeWith instanceof GeoShapeWithDocValuesFieldMapper == false && CONTENT_TYPE.equals(mergeWith.typeName())) { - throw new IllegalArgumentException("mapper [" + name() - + "] of type [geo_shape] cannot change strategy from [BKD] to [recursive]"); + throw new IllegalArgumentException( + "mapper [" + name() + "] of type [geo_shape] cannot change strategy from [BKD] to [recursive]" + ); } super.checkIncomingMergeType(mergeWith); } diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/LatLonShapeDocValuesQuery.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/LatLonShapeDocValuesQuery.java index 0ff6e94d2e481..1a615281ba9ad 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/LatLonShapeDocValuesQuery.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/LatLonShapeDocValuesQuery.java @@ -88,11 +88,11 @@ public void visit(QueryVisitor visitor) { @Override public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) { - if (relation == ShapeField.QueryRelation.CONTAINS) { - return getContainsWeight(scoreMode, boost); - } else { + if (relation == ShapeField.QueryRelation.CONTAINS) { + return getContainsWeight(scoreMode, boost); + } else { return getStandardWeight(scoreMode, boost); - } + } } private ConstantScoreWeight getStandardWeight(ScoreMode scoreMode, float boost) { diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/PointFieldMapper.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/PointFieldMapper.java index 979e657d3bf5e..5f79824bed16f 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/PointFieldMapper.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/PointFieldMapper.java @@ -16,7 +16,6 @@ import org.elasticsearch.common.geo.ShapeRelation; import org.elasticsearch.common.logging.DeprecationCategory; import org.elasticsearch.common.logging.DeprecationLogger; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.geometry.Geometry; import org.elasticsearch.geometry.Point; import org.elasticsearch.index.mapper.AbstractPointGeometryFieldMapper; @@ -26,6 +25,7 @@ import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.MapperBuilderContext; import org.elasticsearch.index.query.SearchExecutionContext; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.spatial.common.CartesianPoint; import org.elasticsearch.xpack.spatial.index.query.ShapeQueryPointProcessor; @@ -47,7 +47,7 @@ public class PointFieldMapper extends AbstractPointGeometryFieldMapper builder(m).nullValue.get(), (n, c, o) -> o == null ? null : parseNullValue(o, ignoreZValue.get().value(), ignoreMalformed.get().value()), - () -> null).acceptsNull(); + () -> null + ).acceptsNull(); } @Override @@ -98,20 +99,19 @@ public FieldMapper build(MapperBuilderContext context) { "Adding multifields to [point] mappers has no effect and will be forbidden in future" ); } - CartesianPointParser parser - = new CartesianPointParser( - name, - CartesianPoint::new, - (p, point) -> { - CartesianPoint.parsePoint(p, point, ignoreZValue.get().value()); - return point; - }, - nullValue.get(), - ignoreZValue.get().value(), ignoreMalformed.get().value()); - PointFieldType ft - = new PointFieldType(context.buildFullName(name), indexed.get(), stored.get(), hasDocValues.get(), parser, meta.get()); - return new PointFieldMapper(name, ft, multiFieldsBuilder.build(this, context), - copyTo.build(), parser, this); + CartesianPointParser parser = new CartesianPointParser(name, CartesianPoint::new, (p, point) -> { + CartesianPoint.parsePoint(p, point, ignoreZValue.get().value()); + return point; + }, nullValue.get(), ignoreZValue.get().value(), ignoreMalformed.get().value()); + PointFieldType ft = new PointFieldType( + context.buildFullName(name), + indexed.get(), + stored.get(), + hasDocValues.get(), + parser, + meta.get() + ); + return new PointFieldMapper(name, ft, multiFieldsBuilder.build(this, context), copyTo.build(), parser, this); } } @@ -120,11 +120,24 @@ public FieldMapper build(MapperBuilderContext context) { private final Builder builder; - public PointFieldMapper(String simpleName, MappedFieldType mappedFieldType, - MultiFields multiFields, CopyTo copyTo, - CartesianPointParser parser, Builder builder) { - super(simpleName, mappedFieldType, multiFields, - builder.ignoreMalformed.get(), builder.ignoreZValue.get(), builder.nullValue.get(), copyTo, parser); + public PointFieldMapper( + String simpleName, + MappedFieldType mappedFieldType, + MultiFields multiFields, + CopyTo copyTo, + CartesianPointParser parser, + Builder builder + ) { + super( + simpleName, + mappedFieldType, + multiFields, + builder.ignoreMalformed.get(), + builder.ignoreZValue.get(), + builder.nullValue.get(), + copyTo, + parser + ); this.builder = builder; } @@ -162,8 +175,14 @@ public static class PointFieldType extends AbstractGeometryFieldType meta) { + private PointFieldType( + String name, + boolean indexed, + boolean stored, + boolean hasDocValues, + CartesianPointParser parser, + Map meta + ) { super(name, indexed, stored, hasDocValues, parser, meta); this.queryProcessor = new ShapeQueryPointProcessor(); } @@ -180,19 +199,21 @@ public Query shapeQuery(Geometry shape, String fieldName, ShapeRelation relation @Override protected Function, List> getFormatter(String format) { - return GeometryFormatterFactory.getFormatter(format, p -> new Point(p.getX(), p.getY())); + return GeometryFormatterFactory.getFormatter(format, p -> new Point(p.getX(), p.getY())); } } /** CartesianPoint parser implementation */ private static class CartesianPointParser extends PointParser { - CartesianPointParser(String field, - Supplier pointSupplier, - CheckedBiFunction objectParser, - CartesianPoint nullValue, - boolean ignoreZValue, - boolean ignoreMalformed) { + CartesianPointParser( + String field, + Supplier pointSupplier, + CheckedBiFunction objectParser, + CartesianPoint nullValue, + boolean ignoreZValue, + boolean ignoreMalformed + ) { super(field, pointSupplier, objectParser, nullValue, ignoreZValue, ignoreMalformed); } diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/ShapeFieldMapper.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/ShapeFieldMapper.java index 8e47ca8cd91e9..edefb428a8ba1 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/ShapeFieldMapper.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/ShapeFieldMapper.java @@ -54,7 +54,7 @@ public class ShapeFieldMapper extends AbstractShapeGeometryFieldMapper private static final DeprecationLogger DEPRECATION_LOGGER = DeprecationLogger.getLogger(GeoShapeFieldMapper.class); private static Builder builder(FieldMapper in) { - return ((ShapeFieldMapper)in).builder; + return ((ShapeFieldMapper) in).builder; } public static class Builder extends FieldMapper.Builder { @@ -88,27 +88,32 @@ public ShapeFieldMapper build(MapperBuilderContext context) { "Adding multifields to [shape] mappers has no effect and will be forbidden in future" ); } - GeometryParser geometryParser - = new GeometryParser(orientation.get().value().getAsBoolean(), coerce.get().value(), ignoreZValue.get().value()); + GeometryParser geometryParser = new GeometryParser( + orientation.get().value().getAsBoolean(), + coerce.get().value(), + ignoreZValue.get().value() + ); Parser parser = new GeoShapeParser(geometryParser); - ShapeFieldType ft - = new ShapeFieldType(context.buildFullName(name), indexed.get(), orientation.get().value(), parser, meta.get()); - return new ShapeFieldMapper(name, ft, - multiFieldsBuilder.build(this, context), copyTo.build(), parser, this); + ShapeFieldType ft = new ShapeFieldType( + context.buildFullName(name), + indexed.get(), + orientation.get().value(), + parser, + meta.get() + ); + return new ShapeFieldMapper(name, ft, multiFieldsBuilder.build(this, context), copyTo.build(), parser, this); } } - public static TypeParser PARSER = new TypeParser((n, c) -> new Builder(n, - IGNORE_MALFORMED_SETTING.get(c.getSettings()), - COERCE_SETTING.get(c.getSettings()))); + public static TypeParser PARSER = new TypeParser( + (n, c) -> new Builder(n, IGNORE_MALFORMED_SETTING.get(c.getSettings()), COERCE_SETTING.get(c.getSettings())) + ); - public static final class ShapeFieldType extends AbstractShapeGeometryFieldType - implements ShapeQueryable { + public static final class ShapeFieldType extends AbstractShapeGeometryFieldType implements ShapeQueryable { private final ShapeQueryProcessor queryProcessor; - public ShapeFieldType(String name, boolean indexed, Orientation orientation, - Parser parser, Map meta) { + public ShapeFieldType(String name, boolean indexed, Orientation orientation, Parser parser, Map meta) { super(name, indexed, false, false, parser, orientation, meta); this.queryProcessor = new ShapeQueryProcessor(); } @@ -132,12 +137,25 @@ protected Function, List> getFormatter(String format) { private final Builder builder; private final ShapeIndexer indexer; - public ShapeFieldMapper(String simpleName, MappedFieldType mappedFieldType, - MultiFields multiFields, CopyTo copyTo, - Parser parser, Builder builder) { - super(simpleName, mappedFieldType, builder.ignoreMalformed.get(), - builder.coerce.get(), builder.ignoreZValue.get(), builder.orientation.get(), - multiFields, copyTo, parser); + public ShapeFieldMapper( + String simpleName, + MappedFieldType mappedFieldType, + MultiFields multiFields, + CopyTo copyTo, + Parser parser, + Builder builder + ) { + super( + simpleName, + mappedFieldType, + builder.ignoreMalformed.get(), + builder.coerce.get(), + builder.ignoreZValue.get(), + builder.orientation.get(), + multiFields, + copyTo, + parser + ); this.builder = builder; this.indexer = new ShapeIndexer(mappedFieldType.name()); } @@ -158,8 +176,9 @@ protected String contentType() { @Override public FieldMapper.Builder getMergeBuilder() { - return new Builder(simpleName(), builder.ignoreMalformed.getDefaultValue().value(), builder.coerce.getDefaultValue().value()) - .init(this); + return new Builder(simpleName(), builder.ignoreMalformed.getDefaultValue().value(), builder.coerce.getDefaultValue().value()).init( + this + ); } @Override diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/ShapeIndexer.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/ShapeIndexer.java index 95998cc5001e4..e8fb72cc72b79 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/ShapeIndexer.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/ShapeIndexer.java @@ -27,7 +27,7 @@ import java.util.Collections; import java.util.List; -public class ShapeIndexer { +public class ShapeIndexer { private final String name; public ShapeIndexer(String name) { @@ -85,7 +85,7 @@ public Void visit(MultiLine multiLine) { @Override public Void visit(MultiPoint multiPoint) { - for(Point point : multiPoint) { + for (Point point : multiPoint) { visit(point); } return null; @@ -93,7 +93,7 @@ public Void visit(MultiPoint multiPoint) { @Override public Void visit(MultiPolygon multiPolygon) { - for(Polygon polygon : multiPolygon) { + for (Polygon polygon : multiPolygon) { visit(polygon); } return null; diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/query/ShapeQueryBuilder.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/query/ShapeQueryBuilder.java index 26866f3337c4d..dcf2c1d70dadd 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/query/ShapeQueryBuilder.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/query/ShapeQueryBuilder.java @@ -11,14 +11,14 @@ import org.elasticsearch.common.geo.GeometryParser; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.geometry.Geometry; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.query.AbstractGeometryQueryBuilder; import org.elasticsearch.index.query.QueryRewriteContext; -import org.elasticsearch.index.query.SearchExecutionContext; import org.elasticsearch.index.query.QueryShardException; +import org.elasticsearch.index.query.SearchExecutionContext; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.spatial.index.mapper.ShapeQueryable; import java.io.IOException; @@ -88,8 +88,10 @@ protected ShapeQueryBuilder newShapeQueryBuilder(String fieldName, Supplier collection) { @Override public Query visit(org.elasticsearch.geometry.Line line) { - throw new QueryShardException(context, "Field [" + fieldName + "] does not support " - + ShapeType.LINESTRING + " queries"); + throw new QueryShardException(context, "Field [" + fieldName + "] does not support " + ShapeType.LINESTRING + " queries"); } @Override // don't think this is called directly public Query visit(LinearRing ring) { - throw new QueryShardException(context, "Field [" + fieldName + "] does not support " - + ShapeType.LINEARRING + " queries"); + throw new QueryShardException(context, "Field [" + fieldName + "] does not support " + ShapeType.LINEARRING + " queries"); } @Override public Query visit(MultiLine multiLine) { - throw new QueryShardException(context, "Field [" + fieldName + "] does not support " - + ShapeType.MULTILINESTRING + " queries"); + throw new QueryShardException(context, "Field [" + fieldName + "] does not support " + ShapeType.MULTILINESTRING + " queries"); } @Override public Query visit(MultiPoint multiPoint) { - throw new QueryShardException(context, "Field [" + fieldName + "] does not support " - + ShapeType.MULTIPOINT + " queries"); + throw new QueryShardException(context, "Field [" + fieldName + "] does not support " + ShapeType.MULTIPOINT + " queries"); } @Override public Query visit(MultiPolygon multiPolygon) { - org.apache.lucene.geo.XYPolygon[] lucenePolygons = - new org.apache.lucene.geo.XYPolygon[multiPolygon.size()]; + org.apache.lucene.geo.XYPolygon[] lucenePolygons = new org.apache.lucene.geo.XYPolygon[multiPolygon.size()]; for (int i = 0; i < multiPolygon.size(); i++) { lucenePolygons[i] = ShapeUtils.toLuceneXYPolygon(multiPolygon.get(i)); } @@ -143,8 +136,7 @@ public Query visit(MultiPolygon multiPolygon) { @Override public Query visit(Point point) { // not currently supported - throw new QueryShardException(context, "Field [" + fieldName + "] does not support " + ShapeType.POINT + - " queries"); + throw new QueryShardException(context, "Field [" + fieldName + "] does not support " + ShapeType.POINT + " queries"); } @Override @@ -164,7 +156,12 @@ public Query visit(Rectangle r) { Query query = XYPointField.newBoxQuery(fieldName, xyRectangle.minX, xyRectangle.maxX, xyRectangle.minY, xyRectangle.maxY); if (fieldType.hasDocValues()) { Query dvQuery = XYDocValuesField.newSlowBoxQuery( - fieldName, xyRectangle.minX, xyRectangle.maxX, xyRectangle.minY, xyRectangle.maxY); + fieldName, + xyRectangle.minX, + xyRectangle.maxX, + xyRectangle.minY, + xyRectangle.maxY + ); query = new IndexOrDocValuesQuery(query, dvQuery); } return query; diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/query/ShapeQueryProcessor.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/query/ShapeQueryProcessor.java index 2eb1760e7266e..5bd7d83da7820 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/query/ShapeQueryProcessor.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/query/ShapeQueryProcessor.java @@ -25,23 +25,21 @@ import org.elasticsearch.geometry.Polygon; import org.elasticsearch.geometry.Rectangle; import org.elasticsearch.index.mapper.MappedFieldType; -import org.elasticsearch.index.query.SearchExecutionContext; import org.elasticsearch.index.query.QueryShardException; +import org.elasticsearch.index.query.SearchExecutionContext; import org.elasticsearch.xpack.spatial.common.ShapeUtils; import org.elasticsearch.xpack.spatial.index.mapper.ShapeFieldMapper; import java.util.ArrayList; import java.util.List; - -public class ShapeQueryProcessor { +public class ShapeQueryProcessor { public Query shapeQuery(Geometry shape, String fieldName, ShapeRelation relation, SearchExecutionContext context) { validateIsShapeFieldType(fieldName, context); // CONTAINS queries are not supported by VECTOR strategy for indices created before version 7.5.0 (Lucene 8.3.0); if (relation == ShapeRelation.CONTAINS && context.indexVersionCreated().before(Version.V_7_5_0)) { - throw new QueryShardException(context, - ShapeRelation.CONTAINS + " query relation not supported for Field [" + fieldName + "]."); + throw new QueryShardException(context, ShapeRelation.CONTAINS + " query relation not supported for Field [" + fieldName + "]."); } if (shape == null) { return new MatchNoDocsQuery(); @@ -52,8 +50,10 @@ public Query shapeQuery(Geometry shape, String fieldName, ShapeRelation relation private void validateIsShapeFieldType(String fieldName, SearchExecutionContext context) { MappedFieldType fieldType = context.getFieldType(fieldName); if (fieldType instanceof ShapeFieldMapper.ShapeFieldType == false) { - throw new QueryShardException(context, "Expected " + ShapeFieldMapper.CONTENT_TYPE - + " field type for Field [" + fieldName + "] but found " + fieldType.typeName()); + throw new QueryShardException( + context, + "Expected " + ShapeFieldMapper.CONTENT_TYPE + " field type for Field [" + fieldName + "] but found " + fieldType.typeName() + ); } } @@ -64,8 +64,7 @@ private Query getVectorQueryFromShape(Geometry queryShape, String fieldName, Sha if (geometries.size() == 0) { return new MatchNoDocsQuery(); } - return XYShape.newGeometryQuery(fieldName, relation.getLuceneRelation(), - geometries.toArray(new XYGeometry[geometries.size()])); + return XYShape.newGeometryQuery(fieldName, relation.getLuceneRelation(), geometries.toArray(new XYGeometry[geometries.size()])); } private static class LuceneGeometryCollector implements GeometryVisitor { diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/ingest/CircleProcessor.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/ingest/CircleProcessor.java index 991a0e4f92e17..79425187c1e5c 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/ingest/CircleProcessor.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/ingest/CircleProcessor.java @@ -8,14 +8,7 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.geo.GeometryParserFormat; -import org.elasticsearch.xcontent.DeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.xcontent.support.MapXContentParser; import org.elasticsearch.geometry.Circle; import org.elasticsearch.geometry.Geometry; import org.elasticsearch.geometry.ShapeType; @@ -24,6 +17,13 @@ import org.elasticsearch.ingest.ConfigurationUtils; import org.elasticsearch.ingest.IngestDocument; import org.elasticsearch.ingest.Processor; +import org.elasticsearch.xcontent.DeprecationHandler; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.xcontent.support.MapXContentParser; import org.elasticsearch.xpack.spatial.SpatialUtils; import java.util.Arrays; @@ -45,8 +45,15 @@ public final class CircleProcessor extends AbstractProcessor { private final double errorDistance; private final CircleShapeFieldType circleShapeFieldType; - CircleProcessor(String tag, String description, String field, String targetField, boolean ignoreMissing, double errorDistance, - CircleShapeFieldType circleShapeFieldType) { + CircleProcessor( + String tag, + String description, + String field, + String targetField, + boolean ignoreMissing, + double errorDistance, + CircleShapeFieldType circleShapeFieldType + ) { super(tag, description); this.field = field; this.targetField = targetField; @@ -55,7 +62,6 @@ public final class CircleProcessor extends AbstractProcessor { this.circleShapeFieldType = circleShapeFieldType; } - @Override @SuppressWarnings("unchecked") public IngestDocument execute(IngestDocument ingestDocument) { @@ -74,8 +80,12 @@ public IngestDocument execute(IngestDocument ingestDocument) { throw new IllegalArgumentException("field [" + field + "] must be a WKT Circle or a GeoJSON Circle value"); } - MapXContentParser parser = new MapXContentParser(NamedXContentRegistry.EMPTY, - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, valueWrapper, XContentType.JSON); + MapXContentParser parser = new MapXContentParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + valueWrapper, + XContentType.JSON + ); try { parser.nextToken(); // START_OBJECT parser.nextToken(); // "shape" field key @@ -99,8 +109,8 @@ public IngestDocument execute(IngestDocument ingestDocument) { XContentBuilder newValueBuilder = XContentFactory.jsonBuilder().startObject().field("val"); geometryFormat.toXContent(polygonizedCircle, newValueBuilder, ToXContent.EMPTY_PARAMS); newValueBuilder.endObject(); - Map newObj = XContentHelper.convertToMap( - BytesReference.bytes(newValueBuilder), true, XContentType.JSON).v2(); + Map newObj = XContentHelper.convertToMap(BytesReference.bytes(newValueBuilder), true, XContentType.JSON) + .v2(); ingestDocument.setFieldValue(targetField, newObj.get("val")); } else { throw new IllegalArgumentException("found [" + geometry.type() + "] instead of circle"); @@ -138,31 +148,37 @@ int numSides(double radiusMeters) { return Math.min(MAXIMUM_NUMBER_OF_SIDES, Math.max(MINIMUM_NUMBER_OF_SIDES, val)); } - public static final class Factory implements Processor.Factory { - public CircleProcessor create(Map registry, String processorTag, String description, - Map config) { + public CircleProcessor create( + Map registry, + String processorTag, + String description, + Map config + ) { String field = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "field"); String targetField = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "target_field", field); boolean ignoreMissing = ConfigurationUtils.readBooleanProperty(TYPE, processorTag, config, "ignore_missing", false); double radiusDistance = Math.abs(ConfigurationUtils.readDoubleProperty(TYPE, processorTag, config, "error_distance")); CircleShapeFieldType circleFieldType = CircleShapeFieldType.parse( - ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "shape_type")); + ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "shape_type") + ); return new CircleProcessor(processorTag, description, field, targetField, ignoreMissing, radiusDistance, circleFieldType); } } enum CircleShapeFieldType { - SHAPE, GEO_SHAPE; + SHAPE, + GEO_SHAPE; public static CircleShapeFieldType parse(String value) { EnumSet validValues = EnumSet.allOf(CircleShapeFieldType.class); try { return valueOf(value.toUpperCase(Locale.ROOT)); } catch (IllegalArgumentException e) { - throw new IllegalArgumentException("illegal [shape_type] value [" + value + "]. valid values are " + - Arrays.toString(validValues.toArray())); + throw new IllegalArgumentException( + "illegal [shape_type] value [" + value + "]. valid values are " + Arrays.toString(validValues.toArray()) + ); } } } diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/GeoLineAggregationBuilder.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/GeoLineAggregationBuilder.java index 55d04a25f0f13..4147ae66783fe 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/GeoLineAggregationBuilder.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/GeoLineAggregationBuilder.java @@ -6,12 +6,8 @@ */ package org.elasticsearch.xpack.spatial.search.aggregations; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.xcontent.ObjectParser; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.AggregationBuilder; @@ -28,13 +24,16 @@ import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry; import org.elasticsearch.search.aggregations.support.ValuesSourceType; import org.elasticsearch.search.sort.SortOrder; +import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.Map; import java.util.Objects; -public class GeoLineAggregationBuilder - extends MultiValuesSourceAggregationBuilder.LeafOnly { +public class GeoLineAggregationBuilder extends MultiValuesSourceAggregationBuilder.LeafOnly { static final ParseField POINT_FIELD = new ParseField("point"); static final ParseField SORT_FIELD = new ParseField("sort"); @@ -44,8 +43,10 @@ public class GeoLineAggregationBuilder public static final String NAME = "geo_line"; - public static final ObjectParser PARSER = - ObjectParser.fromBuilder(NAME, GeoLineAggregationBuilder::new); + public static final ObjectParser PARSER = ObjectParser.fromBuilder( + NAME, + GeoLineAggregationBuilder::new + ); static { MultiValuesSourceParseHelper.declareCommon(PARSER, true, ValueType.NUMERIC); MultiValuesSourceParseHelper.declareField(POINT_FIELD.getPreferredName(), PARSER, true, false, false, false); @@ -68,8 +69,11 @@ public GeoLineAggregationBuilder(String name) { super(name); } - private GeoLineAggregationBuilder(GeoLineAggregationBuilder clone, - AggregatorFactories.Builder factoriesBuilder, Map metaData) { + private GeoLineAggregationBuilder( + GeoLineAggregationBuilder clone, + AggregatorFactories.Builder factoriesBuilder, + Map metaData + ) { super(clone, factoriesBuilder, metaData); } @@ -95,8 +99,7 @@ public GeoLineAggregationBuilder sortOrder(SortOrder sortOrder) { public GeoLineAggregationBuilder size(int size) { if (size <= 0 || size > MAX_PATH_SIZE) { - throw new IllegalArgumentException("invalid [size] value [" + size + "] must be a positive integer <= " - + MAX_PATH_SIZE); + throw new IllegalArgumentException("invalid [size] value [" + size + "] must be a positive integer <= " + MAX_PATH_SIZE); } this.size = size; return this; @@ -125,14 +128,26 @@ protected ValuesSourceType defaultValueSourceType() { } @Override - protected MultiValuesSourceAggregatorFactory innerBuild(AggregationContext aggregationContext, - Map configs, - Map filters, - DocValueFormat format, - AggregatorFactory parent, - AggregatorFactories.Builder subFactoriesBuilder) throws IOException { - return new GeoLineAggregatorFactory(name, configs, format, aggregationContext, parent, subFactoriesBuilder, metadata, - includeSort, sortOrder, size); + protected MultiValuesSourceAggregatorFactory innerBuild( + AggregationContext aggregationContext, + Map configs, + Map filters, + DocValueFormat format, + AggregatorFactory parent, + AggregatorFactories.Builder subFactoriesBuilder + ) throws IOException { + return new GeoLineAggregatorFactory( + name, + configs, + format, + aggregationContext, + parent, + subFactoriesBuilder, + metadata, + includeSort, + sortOrder, + size + ); } public GeoLineAggregationBuilder point(MultiValuesSourceFieldConfig pointConfig) { diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/GeoLineAggregator.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/GeoLineAggregator.java index 58eb7c1bd42e2..14653fc202aa5 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/GeoLineAggregator.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/GeoLineAggregator.java @@ -8,9 +8,9 @@ import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.search.ScoreMode; -import org.elasticsearch.core.Releasables; import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.common.util.LongArray; +import org.elasticsearch.core.Releasables; import org.elasticsearch.search.aggregations.Aggregator; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.LeafBucketCollector; @@ -38,9 +38,16 @@ final class GeoLineAggregator extends MetricsAggregator { private final SortOrder sortOrder; private final int size; - GeoLineAggregator(String name, GeoLineMultiValuesSource valuesSources, AggregationContext context, - Aggregator parent, Map metaData, boolean includeSorts, SortOrder sortOrder, - int size) throws IOException { + GeoLineAggregator( + String name, + GeoLineMultiValuesSource valuesSources, + AggregationContext context, + Aggregator parent, + Map metaData, + boolean includeSorts, + SortOrder sortOrder, + int size + ) throws IOException { super(name, context, parent, metaData); this.valuesSources = valuesSources; this.bigArrays = context.bigArrays(); @@ -66,14 +73,13 @@ public ScoreMode scoreMode() { } @Override - public LeafBucketCollector getLeafCollector(LeafReaderContext ctx, - final LeafBucketCollector sub) throws IOException { + public LeafBucketCollector getLeafCollector(LeafReaderContext ctx, final LeafBucketCollector sub) throws IOException { if (valuesSources == null) { return LeafBucketCollector.NO_OP_COLLECTOR; } BucketedSort.Leaf leafSort = sort.forLeaf(ctx); - return new LeafBucketCollector(){ + return new LeafBucketCollector() { @Override public void collect(int doc, long bucket) throws IOException { leafSort.collect(doc, bucket); diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/GeoLineAggregatorFactory.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/GeoLineAggregatorFactory.java index 4c16768e4709a..0993f19019cc7 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/GeoLineAggregatorFactory.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/GeoLineAggregatorFactory.java @@ -26,11 +26,18 @@ final class GeoLineAggregatorFactory extends MultiValuesSourceAggregatorFactory private SortOrder sortOrder; private int size; - GeoLineAggregatorFactory(String name, - Map configs, - DocValueFormat format, AggregationContext aggregationContext, AggregatorFactory parent, - AggregatorFactories.Builder subFactoriesBuilder, - Map metaData, boolean includeSort, SortOrder sortOrder, int size) throws IOException { + GeoLineAggregatorFactory( + String name, + Map configs, + DocValueFormat format, + AggregationContext aggregationContext, + AggregatorFactory parent, + AggregatorFactories.Builder subFactoriesBuilder, + Map metaData, + boolean includeSort, + SortOrder sortOrder, + int size + ) throws IOException { super(name, configs, format, aggregationContext, parent, subFactoriesBuilder, metaData); this.includeSort = includeSort; this.sortOrder = sortOrder; @@ -38,17 +45,18 @@ final class GeoLineAggregatorFactory extends MultiValuesSourceAggregatorFactory } @Override - protected Aggregator createUnmapped(Aggregator parent, - Map metaData) throws IOException { + protected Aggregator createUnmapped(Aggregator parent, Map metaData) throws IOException { return new GeoLineAggregator(name, null, context, parent, metaData, includeSort, sortOrder, size); } @Override - protected Aggregator doCreateInternal(Map configs, - DocValueFormat format, - Aggregator parent, - CardinalityUpperBound cardinality, - Map metaData) throws IOException { + protected Aggregator doCreateInternal( + Map configs, + DocValueFormat format, + Aggregator parent, + CardinalityUpperBound cardinality, + Map metaData + ) throws IOException { GeoLineMultiValuesSource valuesSources = new GeoLineMultiValuesSource(configs); return new GeoLineAggregator(name, valuesSources, context, parent, metaData, includeSort, sortOrder, size); } diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/GeoLineBucketedSort.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/GeoLineBucketedSort.java index f6e971d51468e..fe51da69d052e 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/GeoLineBucketedSort.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/GeoLineBucketedSort.java @@ -10,19 +10,19 @@ import org.apache.lucene.geo.GeoEncodingUtils; import org.apache.lucene.index.LeafReaderContext; import org.elasticsearch.common.geo.GeoPoint; -import org.elasticsearch.core.Releasable; -import org.elasticsearch.core.Releasables; import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.common.util.DoubleArray; import org.elasticsearch.common.util.LongArray; +import org.elasticsearch.core.Releasable; +import org.elasticsearch.core.Releasables; import org.elasticsearch.index.fielddata.MultiGeoPointValues; import org.elasticsearch.index.fielddata.SortedNumericDoubleValues; import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.AggregationExecutionException; -import org.elasticsearch.xpack.spatial.search.aggregations.support.GeoLineMultiValuesSource; import org.elasticsearch.search.sort.BucketedSort; import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.xpack.core.common.search.aggregations.MissingHelper; +import org.elasticsearch.xpack.spatial.search.aggregations.support.GeoLineMultiValuesSource; import java.io.IOException; @@ -38,8 +38,14 @@ public class GeoLineBucketedSort extends BucketedSort.ForDoubles { private final GeoLineMultiValuesSource valuesSources; - public GeoLineBucketedSort(BigArrays bigArrays, SortOrder sortOrder, DocValueFormat format, int bucketSize, - GeoLineMultiValuesSource valuesSources, GeoLineBucketedSort.Extra extra) { + public GeoLineBucketedSort( + BigArrays bigArrays, + SortOrder sortOrder, + DocValueFormat format, + int bucketSize, + GeoLineMultiValuesSource valuesSources, + GeoLineBucketedSort.Extra extra + ) { super(bigArrays, sortOrder, format, bucketSize, extra); this.valuesSources = valuesSources; } @@ -72,7 +78,7 @@ public double[] getSortValues(long bucket) { long rootIndex = bucket * bucketSize; if (rootIndex >= values().size()) { // We've never seen this bucket. - return new double[]{}; + return new double[] {}; } long start = inHeapMode(bucket) ? rootIndex : (rootIndex + getNextGatherOffset(rootIndex) + 1); long end = rootIndex + bucketSize; @@ -80,7 +86,7 @@ public double[] getSortValues(long bucket) { int i = 0; for (long index = start; index < end; index++) { if (((Extra) extra).empty.isEmpty(index) == false) { - double timestampValue = ((DoubleArray)values()).get(index); + double timestampValue = ((DoubleArray) values()).get(index); result[i++] = timestampValue; } } @@ -96,7 +102,7 @@ public long[] getPoints(long bucket) { long rootIndex = bucket * bucketSize; if (rootIndex >= values().size()) { // We've never seen this bucket. - return new long[]{}; + return new long[] {}; } long start = inHeapMode(bucket) ? rootIndex : (rootIndex + getNextGatherOffset(rootIndex) + 1); long end = rootIndex + bucketSize; @@ -121,8 +127,10 @@ public BucketedSort.Leaf forLeaf(LeafReaderContext ctx) throws IOException { protected boolean advanceExact(int doc) throws IOException { if (docSortValues.advanceExact(doc)) { if (docSortValues.docValueCount() > 1) { - throw new AggregationExecutionException("Encountered more than one sort value for a " + - "single document. Use a script to combine multiple sort-values-per-doc into a single value."); + throw new AggregationExecutionException( + "Encountered more than one sort value for a " + + "single document. Use a script to combine multiple sort-values-per-doc into a single value." + ); } // There should always be one weight if advanceExact lands us here, either @@ -171,8 +179,10 @@ public void swap(long lhs, long rhs) { @Override public Loader loader(LeafReaderContext ctx) throws IOException { - final MultiGeoPointValues docGeoPointValues = valuesSources - .getGeoPointField(GeoLineAggregationBuilder.POINT_FIELD.getPreferredName(), ctx); + final MultiGeoPointValues docGeoPointValues = valuesSources.getGeoPointField( + GeoLineAggregationBuilder.POINT_FIELD.getPreferredName(), + ctx + ); return (index, doc) -> { if (false == docGeoPointValues.advanceExact(doc)) { empty.markMissing(index); @@ -180,8 +190,10 @@ public Loader loader(LeafReaderContext ctx) throws IOException { } if (docGeoPointValues.docValueCount() > 1) { - throw new AggregationExecutionException("Encountered more than one geo_point value for a " + - "single document. Use a script to combine multiple geo_point-values-per-doc into a single value."); + throw new AggregationExecutionException( + "Encountered more than one geo_point value for a " + + "single document. Use a script to combine multiple geo_point-values-per-doc into a single value." + ); } if (index >= values.size()) { diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/InternalGeoLine.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/InternalGeoLine.java index 14813472f4974..0cdc49c8ba84f 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/InternalGeoLine.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/InternalGeoLine.java @@ -10,9 +10,9 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.sort.SortOrder; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.spatial.search.aggregations.GeoShapeMetricAggregation; import java.io.IOException; @@ -49,8 +49,16 @@ public class InternalGeoLine extends InternalAggregation implements GeoShapeMetr * @param sortOrder the {@link SortOrder} for the line. Whether the points are to be plotted in asc or desc order * @param size the max length of the line-string. */ - InternalGeoLine(String name, long[] line, double[] sortVals, Map metadata, boolean complete, - boolean includeSorts, SortOrder sortOrder, int size) { + InternalGeoLine( + String name, + long[] line, + double[] sortVals, + Map metadata, + boolean complete, + boolean includeSorts, + SortOrder sortOrder, + int size + ) { super(name, metadata); this.line = line; this.sortVals = sortVals; @@ -105,8 +113,16 @@ public InternalAggregation reduce(List aggregations, Reduce if (reduceContext.isFinalReduce() && SortOrder.DESC.equals(sortOrder)) { new PathArraySorter(mergedGeoLines.getFinalPoints(), mergedGeoLines.getFinalSortValues(), SortOrder.ASC).sort(); } - return new InternalGeoLine(name, mergedGeoLines.getFinalPoints(), mergedGeoLines.getFinalSortValues(), getMetadata(), complete, - includeSorts, sortOrder, size); + return new InternalGeoLine( + name, + mergedGeoLines.getFinalPoints(), + mergedGeoLines.getFinalSortValues(), + getMetadata(), + complete, + includeSorts, + sortOrder, + size + ); } @Override @@ -149,11 +165,7 @@ public int size() { @Override public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException { - builder - .field("type", "Feature") - .field("geometry", geoJSONGeometry()) - .startObject("properties") - .field("complete", isComplete()); + builder.field("type", "Feature").field("geometry", geoJSONGeometry()).startObject("properties").field("complete", isComplete()); if (includeSorts) { builder.field("sort_values", sortVals); } @@ -209,10 +221,9 @@ public Map geoJSONGeometry() { for (int i = 0; i < line.length; i++) { int x = (int) (line[i] >> 32); int y = (int) line[i]; - coordinates.add(new double[] { - roundDegrees(GeoEncodingUtils.decodeLongitude(x)), - roundDegrees(GeoEncodingUtils.decodeLatitude(y)) - }); + coordinates.add( + new double[] { roundDegrees(GeoEncodingUtils.decodeLongitude(x)), roundDegrees(GeoEncodingUtils.decodeLatitude(y)) } + ); } final Map geoJSON = new HashMap<>(); geoJSON.put("type", "LineString"); diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/AbstractGeoHashGridTiler.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/AbstractGeoHashGridTiler.java index dbdafa30fdf82..9237d42e99416 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/AbstractGeoHashGridTiler.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/AbstractGeoHashGridTiler.java @@ -34,7 +34,7 @@ public long encode(double x, double y) { public int setValues(GeoShapeCellValues values, GeoShapeValues.GeoShapeValue geoValue) throws IOException { if (precision == 0) { - return 1; + return 1; } GeoShapeValues.BoundingBox bounds = geoValue.boundingBox(); assert bounds.minX() <= bounds.maxX(); @@ -43,15 +43,18 @@ public int setValues(GeoShapeCellValues values, GeoShapeValues.GeoShapeValue geo if (bounds.minX() == bounds.maxX() && bounds.minY() == bounds.maxY()) { return setValue(values, geoValue, bounds); } - // TODO: optimize for when a shape fits in a single tile an - // for when brute-force is expected to be faster than rasterization, which - // is when the number of tiles expected is less than the precision + // TODO: optimize for when a shape fits in a single tile an + // for when brute-force is expected to be faster than rasterization, which + // is when the number of tiles expected is less than the precision return setValuesByRasterization("", values, 0, geoValue); } - protected int setValuesByBruteForceScan(GeoShapeCellValues values, GeoShapeValues.GeoShapeValue geoValue, - GeoShapeValues.BoundingBox bounds) throws IOException { - // TODO: This way to discover cells inside of a bounding box seems not to work as expected. I can + protected int setValuesByBruteForceScan( + GeoShapeCellValues values, + GeoShapeValues.GeoShapeValue geoValue, + GeoShapeValues.BoundingBox bounds + ) throws IOException { + // TODO: This way to discover cells inside of a bounding box seems not to work as expected. I can // see that eventually we will be visiting twice the same cell which should not happen. int idx = 0; String min = Geohash.stringEncode(bounds.minX(), bounds.minY(), precision); @@ -67,7 +70,7 @@ protected int setValuesByBruteForceScan(GeoShapeCellValues values, GeoShapeValue GeoRelation relation = relateTile(geoValue, hash); if (relation != GeoRelation.QUERY_DISJOINT) { values.resizeCell(idx + 1); - values.add(idx++, encode(i, j)); + values.add(idx++, encode(i, j)); } } } @@ -92,8 +95,8 @@ private GeoRelation relateTile(GeoShapeValues.GeoShapeValue geoValue, String has return validHash(hash) ? geoValue.relate(Geohash.toBoundingBox(hash)) : GeoRelation.QUERY_DISJOINT; } - protected int setValuesByRasterization(String hash, GeoShapeCellValues values, int valuesIndex, - GeoShapeValues.GeoShapeValue geoValue) throws IOException { + protected int setValuesByRasterization(String hash, GeoShapeCellValues values, int valuesIndex, GeoShapeValues.GeoShapeValue geoValue) + throws IOException { String[] hashes = Geohash.getSubGeohashes(hash); for (int i = 0; i < hashes.length; i++) { GeoRelation relation = relateTile(geoValue, hashes[i]); @@ -102,8 +105,7 @@ protected int setValuesByRasterization(String hash, GeoShapeCellValues values, i values.resizeCell(valuesIndex + 1); values.add(valuesIndex++, Geohash.longEncode(hashes[i])); } else { - valuesIndex = - setValuesByRasterization(hashes[i], values, valuesIndex, geoValue); + valuesIndex = setValuesByRasterization(hashes[i], values, valuesIndex, geoValue); } } else if (relation == GeoRelation.QUERY_INSIDE) { if (hashes[i].length() == precision) { @@ -112,7 +114,7 @@ protected int setValuesByRasterization(String hash, GeoShapeCellValues values, i } else { int numTilesAtPrecision = getNumTilesAtPrecision(precision, hash.length()); values.resizeCell(getNewSize(valuesIndex, numTilesAtPrecision + 1)); - valuesIndex = setValuesForFullyContainedTile(hashes[i],values, valuesIndex, precision); + valuesIndex = setValuesForFullyContainedTile(hashes[i], values, valuesIndex, precision); } } } @@ -120,7 +122,7 @@ protected int setValuesByRasterization(String hash, GeoShapeCellValues values, i } private int getNewSize(int valuesIndex, int increment) { - long newSize = (long) valuesIndex + increment; + long newSize = (long) valuesIndex + increment; if (newSize > Integer.MAX_VALUE) { throw new IllegalArgumentException("Tile aggregation array overflow"); } @@ -128,15 +130,14 @@ private int getNewSize(int valuesIndex, int increment) { } private int getNumTilesAtPrecision(int finalPrecision, int currentPrecision) { - final long numTilesAtPrecision = Math.min((long) Math.pow(32, finalPrecision - currentPrecision) + 1, getMaxCells()); + final long numTilesAtPrecision = Math.min((long) Math.pow(32, finalPrecision - currentPrecision) + 1, getMaxCells()); if (numTilesAtPrecision > Integer.MAX_VALUE) { throw new IllegalArgumentException("Tile aggregation array overflow"); } return (int) numTilesAtPrecision; } - protected int setValuesForFullyContainedTile(String hash, GeoShapeCellValues values, - int valuesIndex, int targetPrecision) { + protected int setValuesForFullyContainedTile(String hash, GeoShapeCellValues values, int valuesIndex, int targetPrecision) { String[] hashes = Geohash.getSubGeohashes(hash); for (int i = 0; i < hashes.length; i++) { if (validHash(hashes[i])) { diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/AbstractGeoTileGridTiler.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/AbstractGeoTileGridTiler.java index a5f3adb20f8a1..9640acf671782 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/AbstractGeoTileGridTiler.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/AbstractGeoTileGridTiler.java @@ -75,8 +75,9 @@ public int setValues(GeoShapeCellValues values, GeoShapeValues.GeoShapeValue geo } private GeoRelation relateTile(GeoShapeValues.GeoShapeValue geoValue, int xTile, int yTile, int precision) throws IOException { - return validTile(xTile, yTile, precision) ? - geoValue.relate(GeoTileUtils.toBoundingBox(xTile, yTile, precision)) : GeoRelation.QUERY_DISJOINT; + return validTile(xTile, yTile, precision) + ? geoValue.relate(GeoTileUtils.toBoundingBox(xTile, yTile, precision)) + : GeoRelation.QUERY_DISJOINT; } /** @@ -97,8 +98,14 @@ protected int setValue(GeoShapeCellValues docValues, int xTile, int yTile) { * @param geoValue the shape value * @return the number of buckets the geoValue is found in */ - protected int setValuesByBruteForceScan(GeoShapeCellValues values, GeoShapeValues.GeoShapeValue geoValue, - int minXTile, int minYTile, int maxXTile, int maxYTile) throws IOException { + protected int setValuesByBruteForceScan( + GeoShapeCellValues values, + GeoShapeValues.GeoShapeValue geoValue, + int minXTile, + int minYTile, + int maxXTile, + int maxYTile + ) throws IOException { int idx = 0; for (int i = minXTile; i <= maxXTile; i++) { for (int j = minYTile; j <= maxYTile; j++) { @@ -112,8 +119,14 @@ protected int setValuesByBruteForceScan(GeoShapeCellValues values, GeoShapeValue return idx; } - protected int setValuesByRasterization(int xTile, int yTile, int zTile, GeoShapeCellValues values, int valuesIndex, - GeoShapeValues.GeoShapeValue geoValue) throws IOException { + protected int setValuesByRasterization( + int xTile, + int yTile, + int zTile, + GeoShapeCellValues values, + int valuesIndex, + GeoShapeValues.GeoShapeValue geoValue + ) throws IOException { zTile++; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { @@ -134,8 +147,7 @@ protected int setValuesByRasterization(int xTile, int yTile, int zTile, GeoShape values.resizeCell(getNewSize(valuesIndex, 1)); values.add(valuesIndex++, GeoTileUtils.longEncodeTiles(zTile, nextX, nextY)); } else { - valuesIndex = - setValuesByRasterization(nextX, nextY, zTile, values, valuesIndex, geoValue); + valuesIndex = setValuesByRasterization(nextX, nextY, zTile, values, valuesIndex, geoValue); } } } @@ -144,7 +156,7 @@ protected int setValuesByRasterization(int xTile, int yTile, int zTile, GeoShape } private int getNewSize(int valuesIndex, int increment) { - long newSize = (long) valuesIndex + increment; + long newSize = (long) valuesIndex + increment; if (newSize > Integer.MAX_VALUE) { throw new IllegalArgumentException("Tile aggregation array overflow"); } @@ -152,7 +164,7 @@ private int getNewSize(int valuesIndex, int increment) { } private int getNumTilesAtPrecision(int finalPrecision, int currentPrecision) { - final long numTilesAtPrecision = Math.min(1L << (2 * (finalPrecision - currentPrecision)), getMaxCells()); + final long numTilesAtPrecision = Math.min(1L << (2 * (finalPrecision - currentPrecision)), getMaxCells()); if (numTilesAtPrecision > Integer.MAX_VALUE) { throw new IllegalArgumentException("Tile aggregation array overflow"); } diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/BoundedGeoTileGridTiler.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/BoundedGeoTileGridTiler.java index a8f4afe78f3d7..ad085b1512e31 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/BoundedGeoTileGridTiler.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/BoundedGeoTileGridTiler.java @@ -75,7 +75,7 @@ protected long getMaxCells() { protected int setValuesForFullyContainedTile(int xTile, int yTile, int zTile, GeoShapeCellValues values, int valuesIndex) { // For every level we go down, we half each dimension. The total number of splits is equal to 1 << (levelEnd - levelStart) final int splits = 1 << precision - zTile; - // The start value of a dimension is calculated by multiplying the value of that dimension at the start level + // The start value of a dimension is calculated by multiplying the value of that dimension at the start level // by the number of splits. Choose the max value with respect to the bounding box. final int minY = Math.max(this.minY, yTile * splits); // The end value of a dimension is calculated by adding to the start value the number of splits. diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/GeoGridTiler.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/GeoGridTiler.java index fa13df4dbe59f..97150fe53d671 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/GeoGridTiler.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/GeoGridTiler.java @@ -49,4 +49,3 @@ public int precision() { /** Maximum number of cells that can be created by this tiler */ protected abstract long getMaxCells(); } - diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/GeoShapeCellIdSource.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/GeoShapeCellIdSource.java index 914531efb0255..cfd1bce97ab24 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/GeoShapeCellIdSource.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/GeoShapeCellIdSource.java @@ -19,7 +19,7 @@ import java.util.function.LongConsumer; -public class GeoShapeCellIdSource extends ValuesSource.Numeric { +public class GeoShapeCellIdSource extends ValuesSource.Numeric { private final GeoShapeValuesSource valuesSource; private final GeoGridTiler encoder; private LongConsumer circuitBreakerConsumer; diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/GeoShapeCellValues.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/GeoShapeCellValues.java index e4abca4013749..ba8d2c11681be 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/GeoShapeCellValues.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/GeoShapeCellValues.java @@ -17,8 +17,7 @@ class GeoShapeCellValues extends ByteTrackingSortingNumericDocValues { private final GeoShapeValues geoShapeValues; protected final GeoGridTiler tiler; - protected GeoShapeCellValues(GeoShapeValues geoShapeValues, GeoGridTiler tiler, - LongConsumer circuitBreakerConsumer) { + protected GeoShapeCellValues(GeoShapeValues geoShapeValues, GeoGridTiler tiler, LongConsumer circuitBreakerConsumer) { super(circuitBreakerConsumer); this.geoShapeValues = geoShapeValues; this.tiler = tiler; @@ -45,9 +44,7 @@ void resizeCell(int newSize) { resize(newSize); } - protected void add(int idx, long value) { values[idx] = value; } } - diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/GeoShapeHashGridAggregator.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/GeoShapeHashGridAggregator.java index fb087a7a0db0d..7ba28a46badf9 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/GeoShapeHashGridAggregator.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/GeoShapeHashGridAggregator.java @@ -18,9 +18,17 @@ import java.util.Map; public class GeoShapeHashGridAggregator extends GeoHashGridAggregator { - public GeoShapeHashGridAggregator(String name, AggregatorFactories factories, ValuesSource.Numeric valuesSource, int requiredSize, - int shardSize, AggregationContext context, Aggregator parent, - CardinalityUpperBound cardinality, Map metadata) throws IOException { + public GeoShapeHashGridAggregator( + String name, + AggregatorFactories factories, + ValuesSource.Numeric valuesSource, + int requiredSize, + int shardSize, + AggregationContext context, + Aggregator parent, + CardinalityUpperBound cardinality, + Map metadata + ) throws IOException { super(name, factories, valuesSource, requiredSize, shardSize, context, parent, cardinality, metadata); } diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/GeoShapeTileGridAggregator.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/GeoShapeTileGridAggregator.java index eb0eb6de0189c..2552ae65c9e6b 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/GeoShapeTileGridAggregator.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/GeoShapeTileGridAggregator.java @@ -18,9 +18,17 @@ import java.util.Map; public class GeoShapeTileGridAggregator extends GeoTileGridAggregator { - public GeoShapeTileGridAggregator(String name, AggregatorFactories factories, ValuesSource.Numeric valuesSource, int requiredSize, - int shardSize, AggregationContext context, Aggregator parent, - CardinalityUpperBound cardinality, Map metadata) throws IOException { + public GeoShapeTileGridAggregator( + String name, + AggregatorFactories factories, + ValuesSource.Numeric valuesSource, + int requiredSize, + int shardSize, + AggregationContext context, + Aggregator parent, + CardinalityUpperBound cardinality, + Map metadata + ) throws IOException { super(name, factories, valuesSource, requiredSize, shardSize, context, parent, cardinality, metadata); } diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/UnboundedGeoHashGridTiler.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/UnboundedGeoHashGridTiler.java index aab6c36cb519d..36c138b74878c 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/UnboundedGeoHashGridTiler.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/UnboundedGeoHashGridTiler.java @@ -7,7 +7,6 @@ package org.elasticsearch.xpack.spatial.search.aggregations.bucket.geogrid; - /** * Unbounded geohash aggregation. It accepts any hash. */ @@ -15,7 +14,6 @@ public class UnboundedGeoHashGridTiler extends AbstractGeoHashGridTiler { private final long maxHashes; - public UnboundedGeoHashGridTiler(int precision) { super(precision); this.maxHashes = (long) Math.pow(32, precision); @@ -23,7 +21,7 @@ public UnboundedGeoHashGridTiler(int precision) { @Override protected boolean validHash(String hash) { - return true; + return true; } @Override diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/UnboundedGeoTileGridTiler.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/UnboundedGeoTileGridTiler.java index 6128548d414d0..5bb18ccc5e1bd 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/UnboundedGeoTileGridTiler.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/UnboundedGeoTileGridTiler.java @@ -7,7 +7,6 @@ package org.elasticsearch.xpack.spatial.search.aggregations.bucket.geogrid; - import org.elasticsearch.search.aggregations.bucket.geogrid.GeoTileUtils; /** @@ -23,7 +22,7 @@ public UnboundedGeoTileGridTiler(int precision) { @Override protected boolean validTile(int x, int y, int z) { - return true; + return true; } @Override @@ -35,7 +34,7 @@ protected long getMaxCells() { protected int setValuesForFullyContainedTile(int xTile, int yTile, int zTile, GeoShapeCellValues values, int valuesIndex) { // For every level we go down, we half each dimension. The total number of splits is equal to 1 << (levelEnd - levelStart) final int splits = 1 << precision - zTile; - // The start value of a dimension is calculated by multiplying the value of that dimension at the start level + // The start value of a dimension is calculated by multiplying the value of that dimension at the start level // by the number of splits final int minX = xTile * splits; final int minY = yTile * splits; diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/metrics/GeoShapeBoundsAggregator.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/metrics/GeoShapeBoundsAggregator.java index b9fd8f315e93a..b8f9f49047d33 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/metrics/GeoShapeBoundsAggregator.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/metrics/GeoShapeBoundsAggregator.java @@ -8,8 +8,8 @@ package org.elasticsearch.xpack.spatial.search.aggregations.metrics; import org.apache.lucene.index.LeafReaderContext; -import org.elasticsearch.core.Releasables; import org.elasticsearch.common.util.DoubleArray; +import org.elasticsearch.core.Releasables; import org.elasticsearch.search.aggregations.Aggregator; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.LeafBucketCollector; @@ -62,8 +62,7 @@ public GeoShapeBoundsAggregator( } @Override - public LeafBucketCollector getLeafCollector(LeafReaderContext ctx, - LeafBucketCollector sub) { + public LeafBucketCollector getLeafCollector(LeafReaderContext ctx, LeafBucketCollector sub) { if (valuesSource == null) { return LeafBucketCollector.NO_OP_COLLECTOR; } @@ -120,8 +119,17 @@ public InternalAggregation buildAggregation(long owningBucketOrdinal) { @Override public InternalAggregation buildEmptyAggregation() { - return new InternalGeoBounds(name, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, - Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, wrapLongitude, metadata()); + return new InternalGeoBounds( + name, + Double.NEGATIVE_INFINITY, + Double.POSITIVE_INFINITY, + Double.POSITIVE_INFINITY, + Double.NEGATIVE_INFINITY, + Double.POSITIVE_INFINITY, + Double.NEGATIVE_INFINITY, + wrapLongitude, + metadata() + ); } @Override diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/metrics/GeoShapeCentroidAggregator.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/metrics/GeoShapeCentroidAggregator.java index 8db66c1b156d4..5458c5cc746d7 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/metrics/GeoShapeCentroidAggregator.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/metrics/GeoShapeCentroidAggregator.java @@ -5,15 +5,14 @@ * 2.0. */ - package org.elasticsearch.xpack.spatial.search.aggregations.metrics; import org.apache.lucene.index.LeafReaderContext; import org.elasticsearch.common.geo.GeoPoint; -import org.elasticsearch.core.Releasables; import org.elasticsearch.common.util.ByteArray; import org.elasticsearch.common.util.DoubleArray; import org.elasticsearch.common.util.LongArray; +import org.elasticsearch.core.Releasables; import org.elasticsearch.search.aggregations.Aggregator; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.LeafBucketCollector; @@ -95,7 +94,7 @@ public void collect(int doc, long bucket) throws IOException { // shape with the same dimensional value compensatedSumLat.reset(latSum.get(bucket), latCompensations.get(bucket)); compensatedSumLon.reset(lonSum.get(bucket), lonCompensations.get(bucket)); - compensatedSumWeight.reset(weightSum.get(bucket), weightCompensations.get(bucket)); + compensatedSumWeight.reset(weightSum.get(bucket), weightCompensations.get(bucket)); final double coordinateWeight = value.weight(); compensatedSumLat.add(coordinateWeight * value.lat()); compensatedSumLon.add(coordinateWeight * value.lon()); @@ -136,7 +135,7 @@ public InternalAggregation buildAggregation(long bucket) { final GeoPoint bucketCentroid = (bucketWeight > 0) ? new GeoPoint(latSum.get(bucket) / bucketWeight, lonSum.get(bucket) / bucketWeight) : null; - return new InternalGeoCentroid(name, bucketCentroid , bucketCount, metadata()); + return new InternalGeoCentroid(name, bucketCentroid, bucketCount, metadata()); } @Override @@ -146,7 +145,15 @@ public InternalAggregation buildEmptyAggregation() { @Override public void doClose() { - Releasables.close(latSum, latCompensations, lonSum, lonCompensations, counts, weightSum, weightCompensations, - dimensionalShapeTypes); + Releasables.close( + latSum, + latCompensations, + lonSum, + lonCompensations, + counts, + weightSum, + weightCompensations, + dimensionalShapeTypes + ); } } diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/support/GeoLineMultiValuesSource.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/support/GeoLineMultiValuesSource.java index cd61cefc09c73..e3b0bed70f530 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/support/GeoLineMultiValuesSource.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/support/GeoLineMultiValuesSource.java @@ -24,10 +24,10 @@ public GeoLineMultiValuesSource(Map valuesSourceConf values = new HashMap<>(valuesSourceConfigs.size()); for (Map.Entry entry : valuesSourceConfigs.entrySet()) { final ValuesSource valuesSource = entry.getValue().getValuesSource(); - if (valuesSource instanceof ValuesSource.Numeric == false - && valuesSource instanceof ValuesSource.GeoPoint == false) { - throw new AggregationExecutionException("ValuesSource type " + valuesSource.toString() + - "is not supported for multi-valued aggregation"); + if (valuesSource instanceof ValuesSource.Numeric == false && valuesSource instanceof ValuesSource.GeoPoint == false) { + throw new AggregationExecutionException( + "ValuesSource type " + valuesSource.toString() + "is not supported for multi-valued aggregation" + ); } values.put(entry.getKey(), valuesSource); } diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/support/GeoShapeValuesSource.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/support/GeoShapeValuesSource.java index 42b640cb0efd3..093cf8b7cf39e 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/support/GeoShapeValuesSource.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/support/GeoShapeValuesSource.java @@ -14,8 +14,8 @@ import org.elasticsearch.index.fielddata.SortedBinaryDocValues; import org.elasticsearch.search.aggregations.AggregationExecutionException; import org.elasticsearch.search.aggregations.support.ValuesSource; -import org.elasticsearch.xpack.spatial.index.fielddata.IndexGeoShapeFieldData; import org.elasticsearch.xpack.spatial.index.fielddata.GeoShapeValues; +import org.elasticsearch.xpack.spatial.index.fielddata.IndexGeoShapeFieldData; import java.io.IOException; import java.util.function.Function; diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/support/GeoShapeValuesSourceType.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/support/GeoShapeValuesSourceType.java index 54a62f91f4cb5..139f462575349 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/support/GeoShapeValuesSourceType.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/search/aggregations/support/GeoShapeValuesSourceType.java @@ -21,8 +21,8 @@ import org.elasticsearch.search.aggregations.support.ValueType; import org.elasticsearch.search.aggregations.support.ValuesSource; import org.elasticsearch.search.aggregations.support.ValuesSourceType; -import org.elasticsearch.xpack.spatial.index.fielddata.IndexGeoShapeFieldData; import org.elasticsearch.xpack.spatial.index.fielddata.GeoShapeValues; +import org.elasticsearch.xpack.spatial.index.fielddata.IndexGeoShapeFieldData; import java.io.IOException; @@ -50,8 +50,13 @@ public ValuesSource getField(FieldContext fieldContext, AggregationScript.LeafFa boolean isGeoPoint = fieldContext.indexFieldData() instanceof IndexGeoPointFieldData; boolean isGeoShape = fieldContext.indexFieldData() instanceof IndexGeoShapeFieldData; if (isGeoPoint == false && isGeoShape == false) { - throw new IllegalArgumentException("Expected geo_point or geo_shape type on field [" + fieldContext.field() + - "], but got [" + fieldContext.fieldType().typeName() + "]"); + throw new IllegalArgumentException( + "Expected geo_point or geo_shape type on field [" + + fieldContext.field() + + "], but got [" + + fieldContext.fieldType().typeName() + + "]" + ); } if (isGeoPoint) { return new ValuesSource.GeoPoint.Fielddata((IndexGeoPointFieldData) fieldContext.indexFieldData()); @@ -91,7 +96,7 @@ public ValuesSourceType valuesSourceType() { @Override public GeoShapeValue value() throws IOException { - return exists ? values.value() : missing; + return exists ? values.value() : missing; } @Override diff --git a/x-pack/plugin/spatial/src/test/java/org/apache/lucene/geo/XShapeTestUtil.java b/x-pack/plugin/spatial/src/test/java/org/apache/lucene/geo/XShapeTestUtil.java index b9648ff2c7a2c..757e2e6aa273d 100644 --- a/x-pack/plugin/spatial/src/test/java/org/apache/lucene/geo/XShapeTestUtil.java +++ b/x-pack/plugin/spatial/src/test/java/org/apache/lucene/geo/XShapeTestUtil.java @@ -18,6 +18,7 @@ import com.carrotsearch.randomizedtesting.RandomizedContext; import com.carrotsearch.randomizedtesting.generators.BiasedNumbers; + import org.apache.lucene.util.TestUtil; import java.util.ArrayList; @@ -120,7 +121,7 @@ private static XYPolygon boxPolygon(XYRectangle box) { private static XYPolygon surpriseMePolygon() { // repeat until we get a poly that doesn't cross dateline: while (true) { - //System.out.println("\nPOLY ITER"); + // System.out.println("\nPOLY ITER"); double centerX = nextDouble(); double centerY = nextDouble(); double radius = 0.1 + 20 * random().nextDouble(); @@ -130,8 +131,8 @@ private static XYPolygon surpriseMePolygon() { ArrayList yList = new ArrayList<>(); double angle = 0.0; while (true) { - angle += random().nextDouble()*40.0; - //System.out.println(" angle " + angle); + angle += random().nextDouble() * 40.0; + // System.out.println(" angle " + angle); if (angle > 360) { break; } @@ -141,14 +142,14 @@ private static XYPolygon surpriseMePolygon() { len = StrictMath.min(len, StrictMath.min(maxX, maxY)); - //System.out.println(" len=" + len); - float x = (float)(centerX + len * Math.cos(Math.toRadians(angle))); - float y = (float)(centerY + len * Math.sin(Math.toRadians(angle))); + // System.out.println(" len=" + len); + float x = (float) (centerX + len * Math.cos(Math.toRadians(angle))); + float y = (float) (centerY + len * Math.sin(Math.toRadians(angle))); xList.add(x); yList.add(y); - //System.out.println(" lat=" + lats.get(lats.size()-1) + " lon=" + lons.get(lons.size()-1)); + // System.out.println(" lat=" + lats.get(lats.size()-1) + " lon=" + lons.get(lons.size()-1)); } // close it @@ -157,7 +158,7 @@ private static XYPolygon surpriseMePolygon() { float[] xArray = new float[xList.size()]; float[] yArray = new float[yList.size()]; - for(int i=0;i centroidSupplier.build(null, null, null, null, null)); - assertThat(exception.getMessage(), - equalTo("current license is non-compliant for [geo_centroid aggregation on geo_shape fields]")); + if (License.OperationMode.TRIAL != operationMode + && License.OperationMode.compare(operationMode, License.OperationMode.GOLD) < 0) { + ElasticsearchSecurityException exception = expectThrows( + ElasticsearchSecurityException.class, + () -> centroidSupplier.build(null, null, null, null, null) + ); + assertThat( + exception.getMessage(), + equalTo("current license is non-compliant for [geo_centroid aggregation on geo_shape fields]") + ); } } } @@ -65,12 +69,14 @@ public void testGeoGridLicenseCheck() { registryKey, new ValuesSourceConfig(GeoShapeValuesSourceType.instance(), null, true, null, null, null, null, null, null) ); - if (License.OperationMode.TRIAL != operationMode && - License.OperationMode.compare(operationMode, License.OperationMode.GOLD) < 0) { - ElasticsearchSecurityException exception = expectThrows(ElasticsearchSecurityException.class, - () -> supplier.build(null, null, null, 0, null, - 0,0, null, null, CardinalityUpperBound.NONE, null)); - assertThat(exception.getMessage(), + if (License.OperationMode.TRIAL != operationMode + && License.OperationMode.compare(operationMode, License.OperationMode.GOLD) < 0) { + ElasticsearchSecurityException exception = expectThrows( + ElasticsearchSecurityException.class, + () -> supplier.build(null, null, null, 0, null, 0, 0, null, null, CardinalityUpperBound.NONE, null) + ); + assertThat( + exception.getMessage(), equalTo("current license is non-compliant for [" + registryKey.getName() + " aggregation on geo_shape fields]") ); } diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/SpatialUtilsTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/SpatialUtilsTests.java index 4d130c7e34e4a..d3601fdcf0d44 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/SpatialUtilsTests.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/SpatialUtilsTests.java @@ -23,14 +23,16 @@ public void testCreateRegularGeoShapePolygon() { final Circle circle = randomValueOtherThanMany( c -> SloppyMath.haversinMeters(c.getLat(), c.getLon(), 90, 0) < c.getRadiusMeters() || SloppyMath.haversinMeters(c.getLat(), c.getLon(), -90, 0) < c.getRadiusMeters(), - () -> GeometryTestUtils.randomCircle(true)); + () -> GeometryTestUtils.randomCircle(true) + ); doRegularGeoShapePolygon(circle); } public void testCircleContainsNorthPole() { final Circle circle = randomValueOtherThanMany( c -> SloppyMath.haversinMeters(c.getLat(), c.getLon(), 90, 0) >= c.getRadiusMeters(), - () -> GeometryTestUtils.randomCircle(true)); + () -> GeometryTestUtils.randomCircle(true) + ); IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> doRegularGeoShapePolygon(circle)); assertThat(ex.getMessage(), containsString("contains the north pole")); } @@ -38,7 +40,8 @@ public void testCircleContainsNorthPole() { public void testCircleContainsSouthPole() { final Circle circle = randomValueOtherThanMany( c -> SloppyMath.haversinMeters(c.getLat(), c.getLon(), -90, 0) >= c.getRadiusMeters(), - () -> GeometryTestUtils.randomCircle(true)); + () -> GeometryTestUtils.randomCircle(true) + ); IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> doRegularGeoShapePolygon(circle)); assertThat(ex.getMessage(), containsString("contains the south pole")); } @@ -54,9 +57,8 @@ private void doRegularGeoShapePolygon(Circle circle) { // check there are numSides edges assertThat(numPoints, equalTo(numSides + 1)); // check that all the points are about a radius away from the center - for (int i = 0; i < numPoints ; i++) { - double actualDistance = SloppyMath - .haversinMeters(circle.getY(), circle.getX(), outerShell.getY(i), outerShell.getX(i)); + for (int i = 0; i < numPoints; i++) { + double actualDistance = SloppyMath.haversinMeters(circle.getY(), circle.getX(), outerShell.getY(i), outerShell.getX(i)); assertThat(actualDistance, closeTo(circle.getRadiusMeters(), 0.1)); } } @@ -76,7 +78,7 @@ public void testCreateRegularShapePolygon() { // check there are numSides edges assertThat(numPoints, equalTo(numSides + 1)); // check that all the points are about a radius away from the center - for (int i = 0; i < numPoints ; i++) { + for (int i = 0; i < numPoints; i++) { double deltaX = circle.getX() - outerShell.getX(i); double deltaY = circle.getY() - outerShell.getY(i); double distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY); diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/action/SpatialInfoTransportActionTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/action/SpatialInfoTransportActionTests.java index 843935b469907..b3f80a32499ad 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/action/SpatialInfoTransportActionTests.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/action/SpatialInfoTransportActionTests.java @@ -55,12 +55,17 @@ public void init() { } public void testAvailable() throws Exception { - SpatialInfoTransportAction featureSet = new SpatialInfoTransportAction( - mock(TransportService.class), mock(ActionFilters.class)); + SpatialInfoTransportAction featureSet = new SpatialInfoTransportAction(mock(TransportService.class), mock(ActionFilters.class)); assertThat(featureSet.available(), is(true)); - var usageAction = new SpatialUsageTransportAction(mock(TransportService.class), clusterService, null, - mock(ActionFilters.class), null, mockClient()); + var usageAction = new SpatialUsageTransportAction( + mock(TransportService.class), + clusterService, + null, + mock(ActionFilters.class), + null, + mockClient() + ); PlainActionFuture future = new PlainActionFuture<>(); Task task = new Task(1L, "_type", "_action", "_description", null, Collections.emptyMap()); usageAction.masterOperation(task, null, clusterService.state(), future); @@ -74,13 +79,18 @@ public void testAvailable() throws Exception { } public void testEnabled() throws Exception { - SpatialInfoTransportAction featureSet = new SpatialInfoTransportAction( - mock(TransportService.class), mock(ActionFilters.class)); + SpatialInfoTransportAction featureSet = new SpatialInfoTransportAction(mock(TransportService.class), mock(ActionFilters.class)); assertThat(featureSet.enabled(), is(true)); assertTrue(featureSet.enabled()); - SpatialUsageTransportAction usageAction = new SpatialUsageTransportAction(mock(TransportService.class), - clusterService, null, mock(ActionFilters.class), null, mockClient()); + SpatialUsageTransportAction usageAction = new SpatialUsageTransportAction( + mock(TransportService.class), + clusterService, + null, + mock(ActionFilters.class), + null, + mockClient() + ); PlainActionFuture future = new PlainActionFuture<>(); usageAction.masterOperation(mock(Task.class), null, clusterService.state(), future); XPackFeatureSet.Usage usage = future.get().getUsage(); @@ -96,10 +106,11 @@ private Client mockClient() { Client client = mock(Client.class); doAnswer((Answer) invocation -> { @SuppressWarnings("unchecked") - ActionListener listener = - (ActionListener) invocation.getArguments()[2]; - listener.onResponse(new SpatialStatsAction.Response(clusterService.getClusterName(), - Collections.emptyList(), Collections.emptyList())); + ActionListener listener = (ActionListener) invocation + .getArguments()[2]; + listener.onResponse( + new SpatialStatsAction.Response(clusterService.getClusterName(), Collections.emptyList(), Collections.emptyList()) + ); return null; }).when(client).execute(eq(SpatialStatsAction.INSTANCE), any(), any()); return client; diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/action/SpatialStatsTransportActionTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/action/SpatialStatsTransportActionTests.java index ef9b2368c3e4d..b999089abf43d 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/action/SpatialStatsTransportActionTests.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/action/SpatialStatsTransportActionTests.java @@ -14,14 +14,14 @@ import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.xcontent.ContextParser; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.rest.yaml.ObjectPath; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.ContextParser; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.spatial.action.SpatialStatsAction; import org.elasticsearch.xpack.spatial.SpatialUsage; import org.junit.Before; @@ -69,13 +69,18 @@ public void testUsage() throws IOException { assertNull(parser.parse(null, null)); } ObjectPath used = buildSpatialStatsResponse(usage); - assertThat(item.name(), used.evaluate("stats." + item.name().toLowerCase(Locale.ROOT) + "_usage"),equalTo(count)); + assertThat(item.name(), used.evaluate("stats." + item.name().toLowerCase(Locale.ROOT) + "_usage"), equalTo(count)); } } private SpatialStatsTransportAction toAction(SpatialUsage nodeUsage) { - return new SpatialStatsTransportAction(transportService, clusterService, threadPool, - new ActionFilters(Collections.emptySet()), nodeUsage); + return new SpatialStatsTransportAction( + transportService, + clusterService, + threadPool, + new ActionFilters(Collections.emptySet()), + nodeUsage + ); } private ObjectPath buildSpatialStatsResponse(SpatialUsage... nodeUsages) throws IOException { @@ -83,8 +88,7 @@ private ObjectPath buildSpatialStatsResponse(SpatialUsage... nodeUsages) throws List nodeResponses = Arrays.stream(nodeUsages) .map(usage -> toAction(usage).nodeOperation(new SpatialStatsAction.NodeRequest(request), null)) .collect(Collectors.toList()); - SpatialStatsAction.Response response = new SpatialStatsAction.Response( - new ClusterName("cluster_name"), nodeResponses, emptyList()); + SpatialStatsAction.Response response = new SpatialStatsAction.Response(new ClusterName("cluster_name"), nodeResponses, emptyList()); try (XContentBuilder builder = jsonBuilder()) { response.toXContent(builder, ToXContent.EMPTY_PARAMS); return ObjectPath.createFromXContent(JsonXContent.jsonXContent, BytesReference.bytes(builder)); diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/fielddata/CentroidCalculatorTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/fielddata/CentroidCalculatorTests.java index 1d1ddd74df7a4..aa817db92c35a 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/fielddata/CentroidCalculatorTests.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/fielddata/CentroidCalculatorTests.java @@ -50,36 +50,38 @@ public void testPoint() { } public void testPolygonWithSmallTrianglesOfZeroWeight() throws Exception { - Geometry geometry = WellKnownText.fromWKT(GeographyValidator.instance(true), false, - "POLYGON((-4.385064 55.2259599,-4.385056 55.2259224,-4.3850466 55.2258994,-4.3849755 55.2258574," + - "-4.3849339 55.2258589,-4.3847033 55.2258742,-4.3846805 55.2258818,-4.3846282 55.2259132,-4.3846215 55.2259247," + - "-4.3846121 55.2259683,-4.3846147 55.2259798,-4.3846369 55.2260157,-4.3846472 55.2260241," + - "-4.3846697 55.2260409,-4.3846952 55.2260562,-4.384765 55.22608,-4.3848199 55.2260861,-4.3848481 55.2260845," + - "-4.3849245 55.2260761,-4.3849393 55.22607,-4.3849996 55.2260432,-4.3850131 55.2260364,-4.3850426 55.2259989," + - "-4.385064 55.2259599),(-4.3850104 55.2259583,-4.385005 55.2259752,-4.384997 55.2259892,-4.3849339 55.2259981," + - "-4.3849272 55.2259308,-4.3850016 55.2259262,-4.385005 55.2259377,-4.3850104 55.2259583)," + - "(-4.3849996 55.2259193,-4.3847502 55.2259331,-4.3847548 55.2258921,-4.3848012 55.2258895," + - "-4.3849219 55.2258811,-4.3849514 55.2258818,-4.3849728 55.2258933,-4.3849996 55.2259193)," + - "(-4.3849917 55.2259984,-4.3849849 55.2260103,-4.3849771 55.2260192,-4.3849701 55.2260019,-4.3849917 55.2259984)," + - "(-4.3846608 55.2259374,-4.384663 55.2259316,-4.3846711 55.2259201,-4.3846992 55.225904," + - "-4.384718 55.2258941,-4.3847434 55.2258927,-4.3847314 55.2259407,-4.3849098 55.2259316,-4.3849098 55.2259492," + - "-4.3848843 55.2259515,-4.3849017 55.2260119,-4.3849567 55.226005,-4.3849701 55.2260272,-4.3849299 55.2260486," + - "-4.3849192 55.2260295,-4.384883 55.2260188,-4.3848776 55.2260119,-4.3848441 55.2260149,-4.3848441 55.2260226," + - "-4.3847864 55.2260241,-4.384722 55.2259652,-4.3847053 55.2259706,-4.384683 55.225954,-4.3846608 55.2259374)," + - "(-4.3846541 55.2259549,-4.384698 55.2259883,-4.3847173 55.2259828,-4.3847743 55.2260333,-4.3847891 55.2260356," + - "-4.3848146 55.226031,-4.3848199 55.2260409,-4.3848387 55.2260417,-4.3848494 55.2260593,-4.3848092 55.2260616," + - "-4.3847623 55.2260539,-4.3847341 55.2260432,-4.3847046 55.2260279,-4.3846738 55.2260062,-4.3846496 55.2259844," + - "-4.3846429 55.2259737,-4.3846523 55.2259714,-4.384651 55.2259629,-4.3846541 55.2259549)," + - "(-4.3846608 55.2259374,-4.3846559 55.2259502,-4.3846541 55.2259549,-4.3846608 55.2259374))"); + Geometry geometry = WellKnownText.fromWKT( + GeographyValidator.instance(true), + false, + "POLYGON((-4.385064 55.2259599,-4.385056 55.2259224,-4.3850466 55.2258994,-4.3849755 55.2258574," + + "-4.3849339 55.2258589,-4.3847033 55.2258742,-4.3846805 55.2258818,-4.3846282 55.2259132,-4.3846215 55.2259247," + + "-4.3846121 55.2259683,-4.3846147 55.2259798,-4.3846369 55.2260157,-4.3846472 55.2260241," + + "-4.3846697 55.2260409,-4.3846952 55.2260562,-4.384765 55.22608,-4.3848199 55.2260861,-4.3848481 55.2260845," + + "-4.3849245 55.2260761,-4.3849393 55.22607,-4.3849996 55.2260432,-4.3850131 55.2260364,-4.3850426 55.2259989," + + "-4.385064 55.2259599),(-4.3850104 55.2259583,-4.385005 55.2259752,-4.384997 55.2259892,-4.3849339 55.2259981," + + "-4.3849272 55.2259308,-4.3850016 55.2259262,-4.385005 55.2259377,-4.3850104 55.2259583)," + + "(-4.3849996 55.2259193,-4.3847502 55.2259331,-4.3847548 55.2258921,-4.3848012 55.2258895," + + "-4.3849219 55.2258811,-4.3849514 55.2258818,-4.3849728 55.2258933,-4.3849996 55.2259193)," + + "(-4.3849917 55.2259984,-4.3849849 55.2260103,-4.3849771 55.2260192,-4.3849701 55.2260019,-4.3849917 55.2259984)," + + "(-4.3846608 55.2259374,-4.384663 55.2259316,-4.3846711 55.2259201,-4.3846992 55.225904," + + "-4.384718 55.2258941,-4.3847434 55.2258927,-4.3847314 55.2259407,-4.3849098 55.2259316,-4.3849098 55.2259492," + + "-4.3848843 55.2259515,-4.3849017 55.2260119,-4.3849567 55.226005,-4.3849701 55.2260272,-4.3849299 55.2260486," + + "-4.3849192 55.2260295,-4.384883 55.2260188,-4.3848776 55.2260119,-4.3848441 55.2260149,-4.3848441 55.2260226," + + "-4.3847864 55.2260241,-4.384722 55.2259652,-4.3847053 55.2259706,-4.384683 55.225954,-4.3846608 55.2259374)," + + "(-4.3846541 55.2259549,-4.384698 55.2259883,-4.3847173 55.2259828,-4.3847743 55.2260333,-4.3847891 55.2260356," + + "-4.3848146 55.226031,-4.3848199 55.2260409,-4.3848387 55.2260417,-4.3848494 55.2260593,-4.3848092 55.2260616," + + "-4.3847623 55.2260539,-4.3847341 55.2260432,-4.3847046 55.2260279,-4.3846738 55.2260062,-4.3846496 55.2259844," + + "-4.3846429 55.2259737,-4.3846523 55.2259714,-4.384651 55.2259629,-4.3846541 55.2259549)," + + "(-4.3846608 55.2259374,-4.3846559 55.2259502,-4.3846541 55.2259549,-4.3846608 55.2259374))" + ); CentroidCalculator calculator = new CentroidCalculator(); calculator.add(geometry); - assertThat(calculator.getX(), closeTo( -4.3848, 1e-4)); + assertThat(calculator.getX(), closeTo(-4.3848, 1e-4)); assertThat(calculator.getY(), closeTo(55.22595, 1e-4)); assertThat(calculator.sumWeight(), closeTo(0, 1e-5)); assertThat(calculator.getDimensionalShapeType(), equalTo(POLYGON)); } - public void testLine() { double[] y = new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; double[] x = new double[] { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 }; @@ -96,7 +98,7 @@ public void testLine() { double[] subY = new double[i + 1]; System.arraycopy(x, 0, subX, 0, i + 1); System.arraycopy(y, 0, subY, 0, i + 1); - Geometry geometry = new Line(subX, subY); + Geometry geometry = new Line(subX, subY); calculator = new CentroidCalculator(); calculator.add(geometry); assertEquals(xRunningAvg[i], calculator.getX(), DELTA); @@ -144,25 +146,25 @@ public void testRoundingErrorAndNormalization() throws IOException { double lonB = randomValueOtherThanMany((l) -> Math.abs(l - lonA) <= GeoUtils.TOLERANCE, GeometryTestUtils::randomLon); double latB = randomValueOtherThanMany((l) -> Math.abs(l - latA) <= GeoUtils.TOLERANCE, GeometryTestUtils::randomLat); { - Line line = new Line(new double[]{180.0, 180.0}, new double[]{latA, latB}); + Line line = new Line(new double[] { 180.0, 180.0 }, new double[] { latA, latB }); GeoShapeValues.GeoShapeValue value = GeoTestUtils.geoShapeValue(line); assertThat(value.lon(), anyOf(equalTo(179.99999991618097), equalTo(-180.0))); } { - Line line = new Line(new double[]{-180.0, -180.0}, new double[]{latA, latB}); + Line line = new Line(new double[] { -180.0, -180.0 }, new double[] { latA, latB }); GeoShapeValues.GeoShapeValue value = GeoTestUtils.geoShapeValue(line); assertThat(value.lon(), anyOf(equalTo(179.99999991618097), equalTo(-180.0))); } { - Line line = new Line(new double[]{lonA, lonB}, new double[] { 90.0, 90.0 }); + Line line = new Line(new double[] { lonA, lonB }, new double[] { 90.0, 90.0 }); GeoShapeValues.GeoShapeValue value = GeoTestUtils.geoShapeValue(line); assertThat(value.lat(), equalTo(89.99999995809048)); } { - Line line = new Line(new double[]{lonA, lonB}, new double[] { -90.0, -90.0 }); + Line line = new Line(new double[] { lonA, lonB }, new double[] { -90.0, -90.0 }); GeoShapeValues.GeoShapeValue value = GeoTestUtils.geoShapeValue(line); assertThat(value.lat(), equalTo(-90.0)); } @@ -174,14 +176,14 @@ public void testPolyonWithHole() { for (boolean ccwInner : List.of(true, false)) { final LinearRing outer, inner; if (ccwOuter) { - outer = new LinearRing(new double[]{-50, 50, 50, -50, -50}, new double[]{-50, -50, 50, 50, -50}); + outer = new LinearRing(new double[] { -50, 50, 50, -50, -50 }, new double[] { -50, -50, 50, 50, -50 }); } else { - outer = new LinearRing(new double[]{-50, -50, 50, 50, -50}, new double[]{-50, 50, 50, -50, -50}); + outer = new LinearRing(new double[] { -50, -50, 50, 50, -50 }, new double[] { -50, 50, 50, -50, -50 }); } if (ccwInner) { - inner = new LinearRing(new double[]{-40, 30, 30, -40, -40}, new double[]{-40, -40, 30, 30, -40}); + inner = new LinearRing(new double[] { -40, 30, 30, -40, -40 }, new double[] { -40, -40, 30, 30, -40 }); } else { - inner = new LinearRing(new double[]{-40, -40, 30, 30, -40}, new double[]{-40, 30, 30, -40, -40}); + inner = new LinearRing(new double[] { -40, -40, 30, 30, -40 }, new double[] { -40, 30, 30, -40, -40 }); } final double POLY_CENTROID = 4.803921568627451; CentroidCalculator calculator = new CentroidCalculator(); @@ -205,7 +207,7 @@ public void testLineAsClosedPoint() { double lon = GeometryTestUtils.randomLon(); double lat = GeometryTestUtils.randomLat(); CentroidCalculator calculator = new CentroidCalculator(); - calculator.add(new Line(new double[] {lon, lon}, new double[] { lat, lat})); + calculator.add(new Line(new double[] { lon, lon }, new double[] { lat, lat })); assertThat(calculator.getX(), equalTo(lon)); assertThat(calculator.getY(), equalTo(lat)); assertThat(calculator.sumWeight(), equalTo(1.0)); @@ -248,8 +250,10 @@ public void testPolygonAsLine() { } public void testPolygonWithEqualSizedHole() { - Polygon polyWithHole = new Polygon(new LinearRing(new double[]{-50, 50, 50, -50, -50}, new double[]{-50, -50, 50, 50, -50}), - Collections.singletonList(new LinearRing(new double[]{-50, -50, 50, 50, -50}, new double[]{-50, 50, 50, -50, -50}))); + Polygon polyWithHole = new Polygon( + new LinearRing(new double[] { -50, 50, 50, -50, -50 }, new double[] { -50, -50, 50, 50, -50 }), + Collections.singletonList(new LinearRing(new double[] { -50, -50, 50, 50, -50 }, new double[] { -50, 50, 50, -50, -50 })) + ); CentroidCalculator calculator = new CentroidCalculator(); calculator.add(polyWithHole); assertThat(calculator.getX(), equalTo(0.0)); @@ -260,8 +264,12 @@ public void testPolygonWithEqualSizedHole() { public void testPolygonAsPoint() { Point point = GeometryTestUtils.randomPoint(false); - Polygon polygon = new Polygon(new LinearRing(new double[] { point.getX(), point.getX(), point.getX(), point.getX() }, - new double[] { point.getY(), point.getY(), point.getY(), point.getY() })); + Polygon polygon = new Polygon( + new LinearRing( + new double[] { point.getX(), point.getX(), point.getX(), point.getX() }, + new double[] { point.getY(), point.getY(), point.getY(), point.getY() } + ) + ); CentroidCalculator calculator = new CentroidCalculator(); calculator.add(polygon); // make calculation to account for floating-point arithmetic @@ -309,9 +317,9 @@ public void testGeometryCollection() { // addFromCalculator is only adding from shapes with the highest dimensionalShapeType CentroidCalculator addFromCalculator = new CentroidCalculator(); for (Geometry shape : shapes) { - if ((shape.type() == ShapeType.MULTIPOLYGON || shape.type() == ShapeType.POLYGON) || - (dimensionalShapeType == LINE && (shape.type() == ShapeType.LINESTRING || shape.type() == ShapeType.MULTILINESTRING)) || - (dimensionalShapeType == POINT && (shape.type() == ShapeType.POINT || shape.type() == ShapeType.MULTIPOINT))) { + if ((shape.type() == ShapeType.MULTIPOLYGON || shape.type() == ShapeType.POLYGON) + || (dimensionalShapeType == LINE && (shape.type() == ShapeType.LINESTRING || shape.type() == ShapeType.MULTILINESTRING)) + || (dimensionalShapeType == POINT && (shape.type() == ShapeType.POINT || shape.type() == ShapeType.MULTIPOINT))) { addFromCalculator.add(shape); } } @@ -427,4 +435,3 @@ public void testAddDifferentDimensionalType() { } } } - diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/fielddata/GeoShapeCoordinateEncoderTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/fielddata/GeoShapeCoordinateEncoderTests.java index bdd46a9f9d181..130658307dd09 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/fielddata/GeoShapeCoordinateEncoderTests.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/fielddata/GeoShapeCoordinateEncoderTests.java @@ -18,8 +18,7 @@ public class GeoShapeCoordinateEncoderTests extends ESTestCase { public void testLongitude() { double randomLon = randomDoubleBetween(-180, 180, true); - double randomInvalidLon = randomFrom(randomDoubleBetween(-1000, -180.01, true), - randomDoubleBetween(180.01, 1000, true)); + double randomInvalidLon = randomFrom(randomDoubleBetween(-1000, -180.01, true), randomDoubleBetween(180.01, 1000, true)); assertThat(CoordinateEncoder.GEO.encodeX(Double.POSITIVE_INFINITY), equalTo(Integer.MAX_VALUE)); assertThat(CoordinateEncoder.GEO.encodeX(Double.NEGATIVE_INFINITY), equalTo(Integer.MIN_VALUE)); @@ -35,8 +34,7 @@ public void testLongitude() { public void testLatitude() { double randomLat = randomDoubleBetween(-90, 90, true); - double randomInvalidLat = randomFrom(randomDoubleBetween(-1000, -90.01, true), - randomDoubleBetween(90.01, 1000, true)); + double randomInvalidLat = randomFrom(randomDoubleBetween(-1000, -90.01, true), randomDoubleBetween(90.01, 1000, true)); assertThat(CoordinateEncoder.GEO.encodeY(Double.POSITIVE_INFINITY), equalTo(Integer.MAX_VALUE)); assertThat(CoordinateEncoder.GEO.encodeY(Double.NEGATIVE_INFINITY), equalTo(Integer.MIN_VALUE)); diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/fielddata/GeometryDocValueTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/fielddata/GeometryDocValueTests.java index 57c8e8a7d900e..7d5020aca76aa 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/fielddata/GeometryDocValueTests.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/fielddata/GeometryDocValueTests.java @@ -54,25 +54,40 @@ public void testDimensionalShapeType() throws IOException { }, () -> randomMultiPolygon(false))); assertDimensionalShapeType(randoPoly, DimensionalShapeType.POLYGON); assertDimensionalShapeType(randoMultiPoly, DimensionalShapeType.POLYGON); - assertDimensionalShapeType(randomFrom( - new GeometryCollection<>(List.of(randomPoint(false))), - new GeometryCollection<>(List.of(randomMultiPoint(false))), - new GeometryCollection<>(Collections.singletonList( - new GeometryCollection<>(List.of(randomPoint(false), randomMultiPoint(false)))))) - , DimensionalShapeType.POINT); - assertDimensionalShapeType(randomFrom( - new GeometryCollection<>(List.of(randomPoint(false), randomLine(false))), - new GeometryCollection<>(List.of(randomMultiPoint(false), randomMultiLine(false))), - new GeometryCollection<>(Collections.singletonList( - new GeometryCollection<>(List.of(randomPoint(false), randomLine(false)))))) - , DimensionalShapeType.LINE); - assertDimensionalShapeType(randomFrom( - new GeometryCollection<>(List.of(randomPoint(false), indexer.prepareForIndexing(randomLine(false)), randoPoly)), - new GeometryCollection<>(List.of(randomMultiPoint(false), randoMultiPoly)), - new GeometryCollection<>(Collections.singletonList( - new GeometryCollection<>(List.of(indexer.prepareForIndexing(randomLine(false)), - indexer.prepareForIndexing(randoPoly)))))) - , DimensionalShapeType.POLYGON); + assertDimensionalShapeType( + randomFrom( + new GeometryCollection<>(List.of(randomPoint(false))), + new GeometryCollection<>(List.of(randomMultiPoint(false))), + new GeometryCollection<>( + Collections.singletonList(new GeometryCollection<>(List.of(randomPoint(false), randomMultiPoint(false)))) + ) + ), + DimensionalShapeType.POINT + ); + assertDimensionalShapeType( + randomFrom( + new GeometryCollection<>(List.of(randomPoint(false), randomLine(false))), + new GeometryCollection<>(List.of(randomMultiPoint(false), randomMultiLine(false))), + new GeometryCollection<>( + Collections.singletonList(new GeometryCollection<>(List.of(randomPoint(false), randomLine(false)))) + ) + ), + DimensionalShapeType.LINE + ); + assertDimensionalShapeType( + randomFrom( + new GeometryCollection<>(List.of(randomPoint(false), indexer.prepareForIndexing(randomLine(false)), randoPoly)), + new GeometryCollection<>(List.of(randomMultiPoint(false), randoMultiPoly)), + new GeometryCollection<>( + Collections.singletonList( + new GeometryCollection<>( + List.of(indexer.prepareForIndexing(randomLine(false)), indexer.prepareForIndexing(randoPoly)) + ) + ) + ) + ), + DimensionalShapeType.POLYGON + ); } public void testRectangleShape() throws IOException { @@ -84,7 +99,7 @@ public void testRectangleShape() throws IOException { Geometry rectangle = new Rectangle(minX, maxX, maxY, minY); GeometryDocValueReader reader = GeoTestUtils.geometryDocValueReader(rectangle, CoordinateEncoder.GEO); - Extent expectedExtent = getExtentFromBox(minX, minY, maxX, maxY); + Extent expectedExtent = getExtentFromBox(minX, minY, maxX, maxY); assertThat(expectedExtent, equalTo(reader.getExtent())); // centroid is calculated using original double values but then loses precision as it is serialized as an integer int encodedCentroidX = CoordinateEncoder.GEO.encodeX(((double) minX + maxX) / 2); @@ -95,10 +110,12 @@ public void testRectangleShape() throws IOException { } private static Extent getExtentFromBox(double bottomLeftX, double bottomLeftY, double topRightX, double topRightY) { - return Extent.fromPoints(CoordinateEncoder.GEO.encodeX(bottomLeftX), + return Extent.fromPoints( + CoordinateEncoder.GEO.encodeX(bottomLeftX), CoordinateEncoder.GEO.encodeY(bottomLeftY), CoordinateEncoder.GEO.encodeX(topRightX), - CoordinateEncoder.GEO.encodeY(topRightY)); + CoordinateEncoder.GEO.encodeY(topRightY) + ); } diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/fielddata/Tile2DVisitorTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/fielddata/Tile2DVisitorTests.java index 50836f00a5635..f6a0ff1e83ea7 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/fielddata/Tile2DVisitorTests.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/fielddata/Tile2DVisitorTests.java @@ -38,8 +38,8 @@ public class Tile2DVisitorTests extends ESTestCase { public void testPacManPolygon() throws Exception { // pacman - double[] px = {0, 10, 10, 0, -8, -10, -8, 0, 10, 10, 0}; - double[] py = {0, -5, -9, -10, -9, 0, 9, 10, 9, 5, 0}; + double[] px = { 0, 10, 10, 0, -8, -10, -8, 0, 10, 10, 0 }; + double[] py = { 0, -5, -9, -10, -9, 0, 9, 10, 9, 5, 0 }; // test cell crossing poly Polygon pacMan = new Polygon(new LinearRing(py, px), Collections.emptyList()); @@ -52,8 +52,10 @@ public void testPacManPolygon() throws Exception { // adapted from org.apache.lucene.geo.TestPolygon2D#testMultiPolygon public void testPolygonWithHole() throws Exception { - Polygon polyWithHole = new Polygon(new LinearRing(new double[]{-50, 50, 50, -50, -50}, new double[]{-50, -50, 50, 50, -50}), - Collections.singletonList(new LinearRing(new double[]{-10, 10, 10, -10, -10}, new double[]{-10, -10, 10, 10, -10}))); + Polygon polyWithHole = new Polygon( + new LinearRing(new double[] { -50, 50, 50, -50, -50 }, new double[] { -50, -50, 50, 50, -50 }), + Collections.singletonList(new LinearRing(new double[] { -10, 10, 10, -10, -10 }, new double[] { -10, -10, 10, 10, -10 })) + ); GeometryDocValueReader reader = GeoTestUtils.geometryDocValueReader(polyWithHole, CoordinateEncoder.GEO); @@ -66,11 +68,11 @@ public void testPolygonWithHole() throws Exception { } public void testCombPolygon() throws Exception { - double[] px = {0, 10, 10, 20, 20, 30, 30, 40, 40, 50, 50, 0, 0}; - double[] py = {0, 0, 20, 20, 0, 0, 20, 20, 0, 0, 30, 30, 0}; + double[] px = { 0, 10, 10, 20, 20, 30, 30, 40, 40, 50, 50, 0, 0 }; + double[] py = { 0, 0, 20, 20, 0, 0, 20, 20, 0, 0, 30, 30, 0 }; - double[] hx = {21, 21, 29, 29, 21}; - double[] hy = {1, 20, 20, 1, 1}; + double[] hx = { 21, 21, 29, 29, 21 }; + double[] hy = { 1, 20, 20, 1, 1 }; Polygon polyWithHole = new Polygon(new LinearRing(px, py), Collections.singletonList(new LinearRing(hx, hy))); GeometryDocValueReader reader = GeoTestUtils.geometryDocValueReader(polyWithHole, CoordinateEncoder.GEO); @@ -82,8 +84,8 @@ public void testCombPolygon() throws Exception { public void testPacManClosedLineString() throws Exception { // pacman - double[] px = {0, 10, 10, 0, -8, -10, -8, 0, 10, 10, 0}; - double[] py = {0, 5, 9, 10, 9, 0, -9, -10, -9, -5, 0}; + double[] px = { 0, 10, 10, 0, -8, -10, -8, 0, 10, 10, 0 }; + double[] py = { 0, 5, 9, 10, 9, 0, -9, -10, -9, -5, 0 }; // test cell crossing poly GeometryDocValueReader reader = GeoTestUtils.geometryDocValueReader(new Line(px, py), CoordinateEncoder.GEO); @@ -95,8 +97,8 @@ public void testPacManClosedLineString() throws Exception { public void testPacManLineString() throws Exception { // pacman - double[] px = {0, 10, 10, 0, -8, -10, -8, 0, 10, 10}; - double[] py = {0, 5, 9, 10, 9, 0, -9, -10, -9, -5}; + double[] px = { 0, 10, 10, 0, -8, -10, -8, 0, 10, 10 }; + double[] py = { 0, 5, 9, 10, 9, 0, -9, -10, -9, -5 }; // test cell crossing poly GeometryDocValueReader reader = GeoTestUtils.geometryDocValueReader(new Line(px, py), CoordinateEncoder.GEO); @@ -121,7 +123,6 @@ public void testPacManPoints() throws Exception { new Point(-5, 10) ); - // candidate intersects cell int xMin = 0; int xMax = 11; @@ -142,19 +143,29 @@ public void testRandomMultiLineIntersections() throws IOException { for (Line line : geometry) { Extent lineExtent = GeoTestUtils.geometryDocValueReader(line, CoordinateEncoder.GEO).getExtent(); - if (lineExtent.minX() != Integer.MIN_VALUE && lineExtent.maxX() != Integer.MAX_VALUE - && lineExtent.minY() != Integer.MIN_VALUE && lineExtent.maxY() != Integer.MAX_VALUE) { - assertRelation(GeoRelation.QUERY_CROSSES, reader, Extent.fromPoints(lineExtent.minX() - 1, lineExtent.minY() - 1, - lineExtent.maxX() + 1, lineExtent.maxY() + 1)); + if (lineExtent.minX() != Integer.MIN_VALUE + && lineExtent.maxX() != Integer.MAX_VALUE + && lineExtent.minY() != Integer.MIN_VALUE + && lineExtent.maxY() != Integer.MAX_VALUE) { + assertRelation( + GeoRelation.QUERY_CROSSES, + reader, + Extent.fromPoints(lineExtent.minX() - 1, lineExtent.minY() - 1, lineExtent.maxX() + 1, lineExtent.maxY() + 1) + ); } } // extent that fully encloses the MultiLine assertRelation(GeoRelation.QUERY_CROSSES, reader, reader.getExtent()); - if (readerExtent.minX() != Integer.MIN_VALUE && readerExtent.maxX() != Integer.MAX_VALUE - && readerExtent.minY() != Integer.MIN_VALUE && readerExtent.maxY() != Integer.MAX_VALUE) { - assertRelation(GeoRelation.QUERY_CROSSES, reader, Extent.fromPoints(readerExtent.minX() - 1, readerExtent.minY() - 1, - readerExtent.maxX() + 1, readerExtent.maxY() + 1)); + if (readerExtent.minX() != Integer.MIN_VALUE + && readerExtent.maxX() != Integer.MAX_VALUE + && readerExtent.minY() != Integer.MIN_VALUE + && readerExtent.maxY() != Integer.MAX_VALUE) { + assertRelation( + GeoRelation.QUERY_CROSSES, + reader, + Extent.fromPoints(readerExtent.minX() - 1, readerExtent.minY() - 1, readerExtent.maxX() + 1, readerExtent.maxY() + 1) + ); } } @@ -191,10 +202,12 @@ private Extent bufferedExtentFromGeoPoint(double x, double y, double extentSize) } private static Extent getExtentFromBox(double bottomLeftX, double bottomLeftY, double topRightX, double topRightY) { - return Extent.fromPoints(CoordinateEncoder.GEO.encodeX(bottomLeftX), + return Extent.fromPoints( + CoordinateEncoder.GEO.encodeX(bottomLeftX), CoordinateEncoder.GEO.encodeY(bottomLeftY), CoordinateEncoder.GEO.encodeX(topRightX), - CoordinateEncoder.GEO.encodeY(topRightY)); + CoordinateEncoder.GEO.encodeY(topRightY) + ); } @@ -207,7 +220,6 @@ private boolean intersects(Geometry g, Point p, double extentSize) throws IOExce return tile2DVisitor.relation() == GeoRelation.QUERY_CROSSES || tile2DVisitor.relation() == GeoRelation.QUERY_INSIDE; } - /** * Preforms left fold operation on all primitive geometries (points, lines polygons, circles and rectangles). * All collection geometries are iterated depth first. @@ -245,7 +257,8 @@ public R visit(MultiLine multiLine) throws E { @Override public R visit(MultiPoint multiPoint) throws E { - return visit((GeometryCollection) multiPoint); } + return visit((GeometryCollection) multiPoint); + } @Override public R visit(MultiPolygon multiPolygon) throws E { diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/fielddata/TriangleTreeTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/fielddata/TriangleTreeTests.java index e6254df7eb763..287c408ee2d38 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/fielddata/TriangleTreeTests.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/fielddata/TriangleTreeTests.java @@ -41,7 +41,7 @@ public void testVisitAllTriangles() throws IOException { assertThat(fieldList.size(), equalTo(visitor.counter)); } - private static class TriangleCounterVisitor implements TriangleTreeReader.Visitor { + private static class TriangleCounterVisitor implements TriangleTreeReader.Visitor { int counter; diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/CartesianFieldMapperTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/CartesianFieldMapperTests.java index 9c205e2a4ba8e..45f7eccde95ca 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/CartesianFieldMapperTests.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/CartesianFieldMapperTests.java @@ -7,13 +7,13 @@ package org.elasticsearch.xpack.spatial.index.mapper; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.MapperParsingException; import org.elasticsearch.index.mapper.MapperTestCase; import org.elasticsearch.index.mapper.ParsedDocument; import org.elasticsearch.plugins.Plugin; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.spatial.LocalStateSpatialPlugin; import java.io.IOException; @@ -25,7 +25,7 @@ import static org.hamcrest.Matchers.nullValue; /** Base class for testing cartesian field mappers */ -public abstract class CartesianFieldMapperTests extends MapperTestCase { +public abstract class CartesianFieldMapperTests extends MapperTestCase { static final String FIELD_NAME = "field"; @@ -35,8 +35,7 @@ protected Collection getPlugins() { } @Override - protected void assertSearchable(MappedFieldType fieldType) { - } + protected void assertSearchable(MappedFieldType fieldType) {} @Override protected void minimalMapping(XContentBuilder b) throws IOException { @@ -50,14 +49,12 @@ protected Object getSampleValueForDocument() { protected abstract String getFieldName(); - public void testWKT() throws IOException { DocumentMapper mapper = createDocumentMapper(fieldMapping(this::minimalMapping)); ParsedDocument doc = mapper.parse(source(b -> b.field(FIELD_NAME, "POINT (2000.1 305.6)"))); assertThat(doc.rootDoc().getField(FIELD_NAME), notNullValue()); } - public void testInvalidPointValuesIgnored() throws IOException { DocumentMapper mapper = createDocumentMapper(fieldMapping(b -> { b.field("type", getFieldName()); @@ -66,13 +63,13 @@ public void testInvalidPointValuesIgnored() throws IOException { assertThat(mapper.parse(source(b -> b.field(FIELD_NAME, "1234.333"))).rootDoc().getField(FIELD_NAME), nullValue()); - assertThat(mapper.parse( - source(b -> b.startObject(FIELD_NAME).field("x", 1.3).field("y", "-").endObject())).rootDoc().getField(FIELD_NAME), + assertThat( + mapper.parse(source(b -> b.startObject(FIELD_NAME).field("x", 1.3).field("y", "-").endObject())).rootDoc().getField(FIELD_NAME), nullValue() ); - assertThat(mapper.parse( - source(b -> b.startObject(FIELD_NAME).field("x", "-").field("y", 1.3).endObject())).rootDoc().getField(FIELD_NAME), + assertThat( + mapper.parse(source(b -> b.startObject(FIELD_NAME).field("x", "-").field("y", 1.3).endObject())).rootDoc().getField(FIELD_NAME), nullValue() ); @@ -80,33 +77,45 @@ public void testInvalidPointValuesIgnored() throws IOException { assertThat(mapper.parse(source(b -> b.field(FIELD_NAME, "1.3,-"))).rootDoc().getField(FIELD_NAME), nullValue()); - assertThat(mapper.parse( - source(b -> b.startObject(FIELD_NAME).field("lon", 1.3).field("y", 1.3).endObject())).rootDoc().getField(FIELD_NAME), + assertThat( + mapper.parse(source(b -> b.startObject(FIELD_NAME).field("lon", 1.3).field("y", 1.3).endObject())) + .rootDoc() + .getField(FIELD_NAME), nullValue() ); - assertThat(mapper.parse( - source(b -> b.startObject(FIELD_NAME).field("x", 1.3).field("lat", 1.3).endObject())).rootDoc().getField(FIELD_NAME), + assertThat( + mapper.parse(source(b -> b.startObject(FIELD_NAME).field("x", 1.3).field("lat", 1.3).endObject())) + .rootDoc() + .getField(FIELD_NAME), nullValue() ); - assertThat(mapper.parse( - source(b -> b.startObject(FIELD_NAME).field("x", "NaN").field("y", "NaN").endObject())).rootDoc().getField(FIELD_NAME), + assertThat( + mapper.parse(source(b -> b.startObject(FIELD_NAME).field("x", "NaN").field("y", "NaN").endObject())) + .rootDoc() + .getField(FIELD_NAME), nullValue() ); - assertThat(mapper.parse( - source(b -> b.startObject(FIELD_NAME).field("x", "NaN").field("y", 1.3).endObject())).rootDoc().getField(FIELD_NAME), + assertThat( + mapper.parse(source(b -> b.startObject(FIELD_NAME).field("x", "NaN").field("y", 1.3).endObject())) + .rootDoc() + .getField(FIELD_NAME), nullValue() ); - assertThat(mapper.parse( - source(b -> b.startObject(FIELD_NAME).field("x", 1.3).field("y", "NaN").endObject())).rootDoc().getField(FIELD_NAME), + assertThat( + mapper.parse(source(b -> b.startObject(FIELD_NAME).field("x", 1.3).field("y", "NaN").endObject())) + .rootDoc() + .getField(FIELD_NAME), nullValue() ); - assertThat(mapper.parse( - source(b -> b.startObject(FIELD_NAME).field("x", 1.3).field("y", "NaN").endObject())).rootDoc().getField(FIELD_NAME), + assertThat( + mapper.parse(source(b -> b.startObject(FIELD_NAME).field("x", 1.3).field("y", "NaN").endObject())) + .rootDoc() + .getField(FIELD_NAME), nullValue() ); @@ -116,13 +125,13 @@ public void testInvalidPointValuesIgnored() throws IOException { assertThat(mapper.parse(source(b -> b.field(FIELD_NAME, "NaN,12"))).rootDoc().getField(FIELD_NAME), nullValue()); - assertThat(mapper.parse( - source(b -> b.startObject(FIELD_NAME).field("x", 1.3).nullField("y").endObject())).rootDoc().getField(FIELD_NAME), + assertThat( + mapper.parse(source(b -> b.startObject(FIELD_NAME).field("x", 1.3).nullField("y").endObject())).rootDoc().getField(FIELD_NAME), nullValue() ); - assertThat(mapper.parse( - source(b -> b.startObject(FIELD_NAME).nullField("x").field("y", 1.3).endObject())).rootDoc().getField(FIELD_NAME), + assertThat( + mapper.parse(source(b -> b.startObject(FIELD_NAME).nullField("x").field("y", 1.3).endObject())).rootDoc().getField(FIELD_NAME), nullValue() ); } @@ -142,11 +151,11 @@ public void testZValue() throws IOException { b.field("ignore_z_value", false); })); - MapperParsingException e = expectThrows(MapperParsingException.class, + MapperParsingException e = expectThrows( + MapperParsingException.class, () -> mapper2.parse(source(b -> b.field(FIELD_NAME, "POINT (2000.1 305.6 34567.33)"))) ); assertThat(e.getMessage(), containsString("failed to parse field [" + FIELD_NAME + "] of type")); - assertThat(e.getRootCause().getMessage(), - containsString("found Z value [34567.33] but [ignore_z_value] parameter is [false]")); + assertThat(e.getRootCause().getMessage(), containsString("found Z value [34567.33] but [ignore_z_value] parameter is [false]")); } } diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/GeoShapeWithDocValuesFieldMapperTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/GeoShapeWithDocValuesFieldMapperTests.java index 744f3f7799361..8de5a396aa673 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/GeoShapeWithDocValuesFieldMapperTests.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/GeoShapeWithDocValuesFieldMapperTests.java @@ -11,10 +11,6 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.geo.Orientation; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.Mapper; @@ -25,6 +21,10 @@ import org.elasticsearch.index.mapper.SourceToParse; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.test.VersionUtils; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.spatial.LocalStateSpatialPlugin; import java.io.IOException; @@ -119,7 +119,7 @@ public void testOrientationParsing() throws IOException { Mapper fieldMapper = defaultMapper.mappers().getMapper("field"); assertThat(fieldMapper, instanceOf(GeoShapeWithDocValuesFieldMapper.class)); - Orientation orientation = ((GeoShapeWithDocValuesFieldMapper)fieldMapper).fieldType().orientation(); + Orientation orientation = ((GeoShapeWithDocValuesFieldMapper) fieldMapper).fieldType().orientation(); assertThat(orientation, equalTo(Orientation.CLOCKWISE)); assertThat(orientation, equalTo(Orientation.LEFT)); assertThat(orientation, equalTo(Orientation.CW)); @@ -132,7 +132,7 @@ public void testOrientationParsing() throws IOException { fieldMapper = defaultMapper.mappers().getMapper("field"); assertThat(fieldMapper, instanceOf(GeoShapeWithDocValuesFieldMapper.class)); - orientation = ((GeoShapeWithDocValuesFieldMapper)fieldMapper).fieldType().orientation(); + orientation = ((GeoShapeWithDocValuesFieldMapper) fieldMapper).fieldType().orientation(); assertThat(orientation, equalTo(Orientation.COUNTER_CLOCKWISE)); assertThat(orientation, equalTo(Orientation.RIGHT)); assertThat(orientation, equalTo(Orientation.CCW)); @@ -150,7 +150,7 @@ public void testCoerceParsing() throws IOException { Mapper fieldMapper = defaultMapper.mappers().getMapper("field"); assertThat(fieldMapper, instanceOf(GeoShapeWithDocValuesFieldMapper.class)); - boolean coerce = ((GeoShapeWithDocValuesFieldMapper)fieldMapper).coerce(); + boolean coerce = ((GeoShapeWithDocValuesFieldMapper) fieldMapper).coerce(); assertThat(coerce, equalTo(true)); defaultMapper = createDocumentMapper(fieldMapping(b -> { @@ -160,12 +160,11 @@ public void testCoerceParsing() throws IOException { fieldMapper = defaultMapper.mappers().getMapper("field"); assertThat(fieldMapper, instanceOf(GeoShapeWithDocValuesFieldMapper.class)); - coerce = ((GeoShapeWithDocValuesFieldMapper)fieldMapper).coerce(); + coerce = ((GeoShapeWithDocValuesFieldMapper) fieldMapper).coerce(); assertThat(coerce, equalTo(false)); } - /** * Test that accept_z_value parameter correctly parses */ @@ -177,7 +176,7 @@ public void testIgnoreZValue() throws IOException { Mapper fieldMapper = defaultMapper.mappers().getMapper("field"); assertThat(fieldMapper, instanceOf(GeoShapeWithDocValuesFieldMapper.class)); - boolean ignoreZValue = ((GeoShapeWithDocValuesFieldMapper)fieldMapper).ignoreZValue(); + boolean ignoreZValue = ((GeoShapeWithDocValuesFieldMapper) fieldMapper).ignoreZValue(); assertThat(ignoreZValue, equalTo(true)); // explicit false accept_z_value test @@ -188,7 +187,7 @@ public void testIgnoreZValue() throws IOException { fieldMapper = defaultMapper.mappers().getMapper("field"); assertThat(fieldMapper, instanceOf(GeoShapeWithDocValuesFieldMapper.class)); - ignoreZValue = ((GeoShapeWithDocValuesFieldMapper)fieldMapper).ignoreZValue(); + ignoreZValue = ((GeoShapeWithDocValuesFieldMapper) fieldMapper).ignoreZValue(); assertThat(ignoreZValue, equalTo(false)); } @@ -204,7 +203,7 @@ public void testIgnoreMalformedParsing() throws IOException { Mapper fieldMapper = defaultMapper.mappers().getMapper("field"); assertThat(fieldMapper, instanceOf(GeoShapeWithDocValuesFieldMapper.class)); - boolean ignoreMalformed = ((GeoShapeWithDocValuesFieldMapper)fieldMapper).ignoreMalformed(); + boolean ignoreMalformed = ((GeoShapeWithDocValuesFieldMapper) fieldMapper).ignoreMalformed(); assertThat(ignoreMalformed, equalTo(true)); // explicit false ignore_malformed test @@ -215,7 +214,7 @@ public void testIgnoreMalformedParsing() throws IOException { fieldMapper = defaultMapper.mappers().getMapper("field"); assertThat(fieldMapper, instanceOf(GeoShapeWithDocValuesFieldMapper.class)); - ignoreMalformed = ((GeoShapeWithDocValuesFieldMapper)fieldMapper).ignoreMalformed(); + ignoreMalformed = ((GeoShapeWithDocValuesFieldMapper) fieldMapper).ignoreMalformed(); assertThat(ignoreMalformed, equalTo(false)); } @@ -231,10 +230,8 @@ public void testIgnoreMalformedValues() throws IOException { })); { - BytesReference arrayedDoc = BytesReference.bytes(XContentFactory.jsonBuilder() - .startObject() - .field("field", "Bad shape") - .endObject() + BytesReference arrayedDoc = BytesReference.bytes( + XContentFactory.jsonBuilder().startObject().field("field", "Bad shape").endObject() ); SourceToParse sourceToParse = new SourceToParse("test", "1", arrayedDoc, XContentType.JSON); ParsedDocument document = ignoreMapper.parse(sourceToParse); @@ -243,11 +240,15 @@ public void testIgnoreMalformedValues() throws IOException { assertThat(exception.getCause().getMessage(), containsString("Unknown geometry type: bad")); } { - BytesReference arrayedDoc = BytesReference.bytes(XContentFactory.jsonBuilder() - .startObject() - .field("field", "POLYGON ((18.9401790919516 -33.9681188869036, 18.9401790919516 -33.9681188869037, 18.9401790919517 " + - "-33.9681188869037, 18.9401790919517 -33.9681188869036, 18.9401790919516 -33.9681188869036))") - .endObject() + BytesReference arrayedDoc = BytesReference.bytes( + XContentFactory.jsonBuilder() + .startObject() + .field( + "field", + "POLYGON ((18.9401790919516 -33.9681188869036, 18.9401790919516 -33.9681188869037, 18.9401790919517 " + + "-33.9681188869037, 18.9401790919517 -33.9681188869036, 18.9401790919516 -33.9681188869036))" + ) + .endObject() ); SourceToParse sourceToParse = new SourceToParse("test", "1", arrayedDoc, XContentType.JSON); ParsedDocument document = ignoreMapper.parse(sourceToParse); @@ -269,7 +270,7 @@ public void testDocValues() throws IOException { Mapper fieldMapper = defaultMapper.mappers().getMapper("field"); assertThat(fieldMapper, instanceOf(GeoShapeWithDocValuesFieldMapper.class)); - boolean hasDocValues = ((GeoShapeWithDocValuesFieldMapper)fieldMapper).fieldType().hasDocValues(); + boolean hasDocValues = ((GeoShapeWithDocValuesFieldMapper) fieldMapper).fieldType().hasDocValues(); assertTrue(hasDocValues); // explicit false doc_values @@ -280,7 +281,7 @@ public void testDocValues() throws IOException { fieldMapper = defaultMapper.mappers().getMapper("field"); assertThat(fieldMapper, instanceOf(GeoShapeWithDocValuesFieldMapper.class)); - hasDocValues = ((GeoShapeWithDocValuesFieldMapper)fieldMapper).fieldType().hasDocValues(); + hasDocValues = ((GeoShapeWithDocValuesFieldMapper) fieldMapper).fieldType().hasDocValues(); assertFalse(hasDocValues); } @@ -303,39 +304,40 @@ public void testGeoShapeMapperMerge() throws Exception { } public void testInvalidCurrentVersion() { - MapperParsingException e = - expectThrows(MapperParsingException.class, - () -> super.createMapperService(Version.CURRENT, fieldMapping((b) -> { - b.field("type", "geo_shape").field("strategy", "recursive"); - }))); - assertThat(e.getMessage(), - containsString("using deprecated parameters [strategy] " + - "in mapper [field] of type [geo_shape] is no longer allowed")); + MapperParsingException e = expectThrows( + MapperParsingException.class, + () -> super.createMapperService( + Version.CURRENT, + fieldMapping((b) -> { b.field("type", "geo_shape").field("strategy", "recursive"); }) + ) + ); + assertThat( + e.getMessage(), + containsString("using deprecated parameters [strategy] " + "in mapper [field] of type [geo_shape] is no longer allowed") + ); } public void testGeoShapeLegacyMerge() throws Exception { Version version = VersionUtils.randomPreviousCompatibleVersion(random(), Version.V_8_0_0); MapperService m = createMapperService(version, fieldMapping(b -> b.field("type", "geo_shape"))); - Exception e = expectThrows(IllegalArgumentException.class, - () -> merge(m, fieldMapping(b -> b.field("type", "geo_shape").field("strategy", "recursive")))); + Exception e = expectThrows( + IllegalArgumentException.class, + () -> merge(m, fieldMapping(b -> b.field("type", "geo_shape").field("strategy", "recursive"))) + ); - assertThat(e.getMessage(), - containsString("mapper [field] of type [geo_shape] cannot change strategy from [BKD] to [recursive]")); + assertThat(e.getMessage(), containsString("mapper [field] of type [geo_shape] cannot change strategy from [BKD] to [recursive]")); assertFieldWarnings("strategy"); MapperService lm = createMapperService(version, fieldMapping(b -> b.field("type", "geo_shape").field("strategy", "recursive"))); - e = expectThrows(IllegalArgumentException.class, - () -> merge(lm, fieldMapping(b -> b.field("type", "geo_shape")))); - assertThat(e.getMessage(), - containsString("mapper [field] of type [geo_shape] cannot change strategy from [recursive] to [BKD]")); + e = expectThrows(IllegalArgumentException.class, () -> merge(lm, fieldMapping(b -> b.field("type", "geo_shape")))); + assertThat(e.getMessage(), containsString("mapper [field] of type [geo_shape] cannot change strategy from [recursive] to [BKD]")); assertFieldWarnings("strategy"); } private void assertFieldWarnings(String... fieldNames) { String[] warnings = new String[fieldNames.length]; for (int i = 0; i < fieldNames.length; ++i) { - warnings[i] = "Parameter [" + fieldNames[i] + "] " - + "is deprecated and will be removed in a future version"; + warnings[i] = "Parameter [" + fieldNames[i] + "] " + "is deprecated and will be removed in a future version"; } assertWarnings(warnings); } @@ -343,8 +345,7 @@ private void assertFieldWarnings(String... fieldNames) { public void testSerializeDefaults() throws Exception { DocumentMapper defaultMapper = createDocumentMapper(fieldMapping(this::minimalMapping)); String serialized = toXContentString((GeoShapeWithDocValuesFieldMapper) defaultMapper.mappers().getMapper("field")); - assertTrue(serialized, serialized.contains("\"orientation\":\"" + - Orientation.RIGHT + "\"")); + assertTrue(serialized, serialized.contains("\"orientation\":\"" + Orientation.RIGHT + "\"")); assertTrue(serialized, serialized.contains("\"doc_values\":true")); } @@ -355,8 +356,7 @@ public void testSerializeDocValues() throws IOException { b.field("doc_values", docValues); })); String serialized = toXContentString((GeoShapeWithDocValuesFieldMapper) mapper.mappers().getMapper("field")); - assertTrue(serialized, serialized.contains("\"orientation\":\"" + - Orientation.RIGHT + "\"")); + assertTrue(serialized, serialized.contains("\"orientation\":\"" + Orientation.RIGHT + "\"")); assertTrue(serialized, serialized.contains("\"doc_values\":" + docValues)); } @@ -364,19 +364,26 @@ public void testGeoShapeArrayParsing() throws Exception { DocumentMapper mapper = createDocumentMapper(fieldMapping(this::minimalMapping)); - BytesReference arrayedDoc = BytesReference.bytes(XContentFactory.jsonBuilder() - .startObject() - .startArray("shape") - .startObject() - .field("type", "Point") - .startArray("coordinates").value(176.0).value(15.0).endArray() - .endObject() - .startObject() - .field("type", "Point") - .startArray("coordinates").value(76.0).value(-15.0).endArray() - .endObject() - .endArray() - .endObject() + BytesReference arrayedDoc = BytesReference.bytes( + XContentFactory.jsonBuilder() + .startObject() + .startArray("shape") + .startObject() + .field("type", "Point") + .startArray("coordinates") + .value(176.0) + .value(15.0) + .endArray() + .endObject() + .startObject() + .field("type", "Point") + .startArray("coordinates") + .value(76.0) + .value(-15.0) + .endArray() + .endObject() + .endArray() + .endObject() ); SourceToParse sourceToParse = new SourceToParse("test", "1", arrayedDoc, XContentType.JSON); diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/GeoShapeWithDocValuesFieldTypeTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/GeoShapeWithDocValuesFieldTypeTests.java index 29a064e2bcdcc..983bfaeccd25c 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/GeoShapeWithDocValuesFieldTypeTests.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/GeoShapeWithDocValuesFieldTypeTests.java @@ -27,13 +27,9 @@ public class GeoShapeWithDocValuesFieldTypeTests extends FieldTypeTestCase { public void testFetchSourceValue() throws IOException { - MappedFieldType mapper - = new GeoShapeFieldMapper.Builder("field", true, true) - .build(MapperBuilderContext.ROOT) - .fieldType(); + MappedFieldType mapper = new GeoShapeFieldMapper.Builder("field", true, true).build(MapperBuilderContext.ROOT).fieldType(); - Map jsonLineString = Map.of("type", "LineString", "coordinates", - List.of(List.of(42.0, 27.1), List.of(30.0, 50.0))); + Map jsonLineString = Map.of("type", "LineString", "coordinates", List.of(List.of(42.0, 27.1), List.of(30.0, 50.0))); Map jsonPoint = Map.of("type", "Point", "coordinates", List.of(14.0, 15.0)); Map jsonMalformed = Map.of("type", "Point", "coordinates", "foo"); String wktLineString = "LINESTRING (42.0 27.1, 30.0 50.0)"; @@ -93,11 +89,15 @@ public void testFetchVectorTile() throws IOException { private void fetchVectorTile(Geometry geometry) throws IOException { final GeoFormatterFactory geoFormatterFactory = new GeoFormatterFactory<>( - new SpatialGeometryFormatterExtension().getGeometryFormatterFactories()); - final MappedFieldType mapper - = new GeoShapeWithDocValuesFieldMapper.Builder("field", Version.CURRENT, false, false, geoFormatterFactory) - .build(MapperBuilderContext.ROOT) - .fieldType(); + new SpatialGeometryFormatterExtension().getGeometryFormatterFactories() + ); + final MappedFieldType mapper = new GeoShapeWithDocValuesFieldMapper.Builder( + "field", + Version.CURRENT, + false, + false, + geoFormatterFactory + ).build(MapperBuilderContext.ROOT).fieldType(); final int z = randomIntBetween(1, 10); int x = randomIntBetween(0, (1 << z) - 1); int y = randomIntBetween(0, (1 << z) - 1); diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/LatLonShapeDocValuesQueryTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/LatLonShapeDocValuesQueryTests.java index 5ad7e9832924a..7790d84a86fbb 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/LatLonShapeDocValuesQueryTests.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/LatLonShapeDocValuesQueryTests.java @@ -42,15 +42,15 @@ public class LatLonShapeDocValuesQueryTests extends ESTestCase { public void testEqualsAndHashcode() { Polygon polygon = GeoTestUtil.nextPolygon(); - Query q1 = new LatLonShapeDocValuesQuery(FIELD_NAME,ShapeField.QueryRelation.INTERSECTS, polygon); - Query q2 = new LatLonShapeDocValuesQuery(FIELD_NAME,ShapeField.QueryRelation.INTERSECTS, polygon); + Query q1 = new LatLonShapeDocValuesQuery(FIELD_NAME, ShapeField.QueryRelation.INTERSECTS, polygon); + Query q2 = new LatLonShapeDocValuesQuery(FIELD_NAME, ShapeField.QueryRelation.INTERSECTS, polygon); QueryUtils.checkEqual(q1, q2); - Query q3 = new LatLonShapeDocValuesQuery(FIELD_NAME + "x",ShapeField.QueryRelation.INTERSECTS, polygon); + Query q3 = new LatLonShapeDocValuesQuery(FIELD_NAME + "x", ShapeField.QueryRelation.INTERSECTS, polygon); QueryUtils.checkUnequal(q1, q3); - Rectangle rectangle = GeoTestUtil.nextBox(); - Query q4 = new LatLonShapeDocValuesQuery(FIELD_NAME,ShapeField.QueryRelation.INTERSECTS, rectangle); + Rectangle rectangle = GeoTestUtil.nextBox(); + Query q4 = new LatLonShapeDocValuesQuery(FIELD_NAME, ShapeField.QueryRelation.INTERSECTS, rectangle); QueryUtils.checkUnequal(q1, q4); } @@ -67,7 +67,8 @@ public void testIndexSimpleShapes() throws Exception { GeoShapeIndexer indexer = new GeoShapeIndexer(true, FIELD_NAME); for (int id = 0; id < numDocs; id++) { Document doc = new Document(); - @SuppressWarnings("unchecked") Function geometryFunc = ESTestCase.randomFrom( + @SuppressWarnings("unchecked") + Function geometryFunc = ESTestCase.randomFrom( GeometryTestUtils::randomLine, GeometryTestUtils::randomPoint, GeometryTestUtils::randomPolygon @@ -159,10 +160,14 @@ private LatLonGeometry[] randomLuceneQueryGeometries() { private LatLonGeometry randomLuceneQueryGeometry() { switch (randomInt(3)) { - case 0: return GeoTestUtil.nextPolygon(); - case 1: return GeoTestUtil.nextCircle(); - case 2: return new Point(GeoTestUtil.nextLatitude(), GeoTestUtil.nextLongitude()); - default: return GeoTestUtil.nextBox(); + case 0: + return GeoTestUtil.nextPolygon(); + case 1: + return GeoTestUtil.nextCircle(); + case 2: + return new Point(GeoTestUtil.nextLatitude(), GeoTestUtil.nextLongitude()); + default: + return GeoTestUtil.nextBox(); } } } diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/PointFieldMapperTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/PointFieldMapperTests.java index e4f7a189bed15..4285051e606ca 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/PointFieldMapperTests.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/PointFieldMapperTests.java @@ -7,13 +7,13 @@ package org.elasticsearch.xpack.spatial.index.mapper; import org.apache.lucene.util.BytesRef; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.Mapper; import org.elasticsearch.index.mapper.ParsedDocument; import org.elasticsearch.index.mapper.SourceToParse; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xpack.spatial.common.CartesianPoint; import java.io.IOException; @@ -61,11 +61,18 @@ public void testArrayValues() throws Exception { b.field("store", true); })); - SourceToParse sourceToParse = source(b -> - b.startArray(FIELD_NAME) - .startObject().field("x", 1.2).field("y", 1.3).endObject() - .startObject().field("x", 1.4).field("y", 1.5).endObject() - .endArray()); + SourceToParse sourceToParse = source( + b -> b.startArray(FIELD_NAME) + .startObject() + .field("x", 1.2) + .field("y", 1.3) + .endObject() + .startObject() + .field("x", 1.4) + .field("y", 1.5) + .endObject() + .endArray() + ); ParsedDocument doc = mapper.parse(sourceToParse); // doc values are enabled by default, but in this test we disable them; we should only have 2 points @@ -80,7 +87,6 @@ public void testXYInOneValue() throws Exception { assertThat(doc.rootDoc().getField(FIELD_NAME), notNullValue()); } - public void testInOneValueStored() throws Exception { DocumentMapper mapper = createDocumentMapper(fieldMapping(b -> { b.field("type", "point"); @@ -147,11 +153,18 @@ public void testArrayArrayStored() throws Exception { b.field("doc_values", false); })); - SourceToParse sourceToParse = source(b -> - b.startArray(FIELD_NAME) - .startArray().value(1.3).value(1.2).endArray() - .startArray().value(1.5).value(1.4).endArray() - .endArray()); + SourceToParse sourceToParse = source( + b -> b.startArray(FIELD_NAME) + .startArray() + .value(1.3) + .value(1.2) + .endArray() + .startArray() + .value(1.5) + .value(1.4) + .endArray() + .endArray() + ); ParsedDocument doc = mapper.parse(sourceToParse); assertThat(doc.rootDoc().getFields(FIELD_NAME), notNullValue()); diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/PointFieldTypeTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/PointFieldTypeTests.java index e93a6cd2846d9..1467a10850188 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/PointFieldTypeTests.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/PointFieldTypeTests.java @@ -18,9 +18,7 @@ public class PointFieldTypeTests extends FieldTypeTestCase { public void testFetchSourceValue() throws IOException { - MappedFieldType mapper = new PointFieldMapper.Builder("field", false) - .build(MapperBuilderContext.ROOT) - .fieldType(); + MappedFieldType mapper = new PointFieldMapper.Builder("field", false).build(MapperBuilderContext.ROOT).fieldType(); Map jsonPoint = Map.of("type", "Point", "coordinates", List.of(42.0, 27.1)); String wktPoint = "POINT (42.0 27.1)"; diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/ShapeFieldMapperTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/ShapeFieldMapperTests.java index 94579496213f3..ab7c18b4acbdb 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/ShapeFieldMapperTests.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/ShapeFieldMapperTests.java @@ -8,14 +8,14 @@ import org.apache.lucene.index.IndexableField; import org.elasticsearch.common.Strings; -import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.common.geo.Orientation; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.Mapper; import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.mapper.ParsedDocument; import org.elasticsearch.index.mapper.SourceToParse; -import org.elasticsearch.common.geo.Orientation; +import org.elasticsearch.xcontent.ToXContent; import java.io.IOException; import java.util.Collections; @@ -67,7 +67,6 @@ public void testDefaultConfiguration() throws IOException { assertThat(shapeFieldMapper.fieldType().orientation(), equalTo(Orientation.RIGHT)); } - /** * Test that orientation parameter correctly parses */ @@ -80,7 +79,7 @@ public void testOrientationParsing() throws IOException { Mapper fieldMapper = defaultMapper.mappers().getMapper(FIELD_NAME); assertThat(fieldMapper, instanceOf(ShapeFieldMapper.class)); - Orientation orientation = ((ShapeFieldMapper)fieldMapper).fieldType().orientation(); + Orientation orientation = ((ShapeFieldMapper) fieldMapper).fieldType().orientation(); assertThat(orientation, equalTo(Orientation.CLOCKWISE)); assertThat(orientation, equalTo(Orientation.LEFT)); assertThat(orientation, equalTo(Orientation.CW)); @@ -93,7 +92,7 @@ public void testOrientationParsing() throws IOException { fieldMapper = defaultMapper.mappers().getMapper(FIELD_NAME); assertThat(fieldMapper, instanceOf(ShapeFieldMapper.class)); - orientation = ((ShapeFieldMapper)fieldMapper).fieldType().orientation(); + orientation = ((ShapeFieldMapper) fieldMapper).fieldType().orientation(); assertThat(orientation, equalTo(Orientation.COUNTER_CLOCKWISE)); assertThat(orientation, equalTo(Orientation.RIGHT)); assertThat(orientation, equalTo(Orientation.CCW)); @@ -111,7 +110,7 @@ public void testCoerceParsing() throws IOException { Mapper fieldMapper = defaultMapper.mappers().getMapper(FIELD_NAME); assertThat(fieldMapper, instanceOf(ShapeFieldMapper.class)); - boolean coerce = ((ShapeFieldMapper)fieldMapper).coerce(); + boolean coerce = ((ShapeFieldMapper) fieldMapper).coerce(); assertThat(coerce, equalTo(true)); defaultMapper = createDocumentMapper(fieldMapping(b -> { @@ -121,12 +120,11 @@ public void testCoerceParsing() throws IOException { fieldMapper = defaultMapper.mappers().getMapper(FIELD_NAME); assertThat(fieldMapper, instanceOf(ShapeFieldMapper.class)); - coerce = ((ShapeFieldMapper)fieldMapper).coerce(); + coerce = ((ShapeFieldMapper) fieldMapper).coerce(); assertThat(coerce, equalTo(false)); } - /** * Test that accept_z_value parameter correctly parses */ @@ -138,7 +136,7 @@ public void testIgnoreZValue() throws IOException { Mapper fieldMapper = defaultMapper.mappers().getMapper(FIELD_NAME); assertThat(fieldMapper, instanceOf(ShapeFieldMapper.class)); - boolean ignoreZValue = ((ShapeFieldMapper)fieldMapper).ignoreZValue(); + boolean ignoreZValue = ((ShapeFieldMapper) fieldMapper).ignoreZValue(); assertThat(ignoreZValue, equalTo(true)); // explicit false accept_z_value test @@ -149,7 +147,7 @@ public void testIgnoreZValue() throws IOException { fieldMapper = defaultMapper.mappers().getMapper(FIELD_NAME); assertThat(fieldMapper, instanceOf(ShapeFieldMapper.class)); - ignoreZValue = ((ShapeFieldMapper)fieldMapper).ignoreZValue(); + ignoreZValue = ((ShapeFieldMapper) fieldMapper).ignoreZValue(); assertThat(ignoreZValue, equalTo(false)); } @@ -165,7 +163,7 @@ public void testIgnoreMalformedParsing() throws IOException { Mapper fieldMapper = defaultMapper.mappers().getMapper(FIELD_NAME); assertThat(fieldMapper, instanceOf(ShapeFieldMapper.class)); - boolean ignoreMalformed = ((ShapeFieldMapper)fieldMapper).ignoreMalformed(); + boolean ignoreMalformed = ((ShapeFieldMapper) fieldMapper).ignoreMalformed(); assertThat(ignoreMalformed, equalTo(true)); // explicit false ignore_malformed test @@ -176,7 +174,7 @@ public void testIgnoreMalformedParsing() throws IOException { fieldMapper = defaultMapper.mappers().getMapper(FIELD_NAME); assertThat(fieldMapper, instanceOf(ShapeFieldMapper.class)); - ignoreMalformed = ((ShapeFieldMapper)fieldMapper).ignoreMalformed(); + ignoreMalformed = ((ShapeFieldMapper) fieldMapper).ignoreMalformed(); assertThat(ignoreMalformed, equalTo(false)); } @@ -201,8 +199,7 @@ public void testGeoShapeMapperMerge() throws Exception { public void testSerializeDefaults() throws Exception { DocumentMapper defaultMapper = createDocumentMapper(fieldMapping(this::minimalMapping)); String serialized = toXContentString((ShapeFieldMapper) defaultMapper.mappers().getMapper(FIELD_NAME)); - assertTrue(serialized, serialized.contains("\"orientation\":\"" + - Orientation.RIGHT + "\"")); + assertTrue(serialized, serialized.contains("\"orientation\":\"" + Orientation.RIGHT + "\"")); } public void testShapeArrayParsing() throws Exception { @@ -210,18 +207,23 @@ public void testShapeArrayParsing() throws Exception { DocumentMapper mapper = createDocumentMapper(fieldMapping(this::minimalMapping)); SourceToParse sourceToParse = source(b -> { - b.startArray("shape") - .startObject() - .field("type", "Point") - .startArray("coordinates").value(176.0).value(15.0).endArray() - .endObject() - .startObject() - .field("type", "Point") - .startArray("coordinates").value(76.0).value(-15.0).endArray() - .endObject() - .endArray(); - } - ); + b.startArray("shape") + .startObject() + .field("type", "Point") + .startArray("coordinates") + .value(176.0) + .value(15.0) + .endArray() + .endObject() + .startObject() + .field("type", "Point") + .startArray("coordinates") + .value(76.0) + .value(-15.0) + .endArray() + .endObject() + .endArray(); + }); ParsedDocument document = mapper.parse(sourceToParse); assertThat(document.docs(), hasSize(1)); @@ -248,7 +250,7 @@ public void testMultiFieldsDeprecationWarning() throws Exception { assertWarnings("Adding multifields to [shape] mappers has no effect and will be forbidden in future"); } - public String toXContentString(ShapeFieldMapper mapper) { + public String toXContentString(ShapeFieldMapper mapper) { return toXContentString(mapper, true); } diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/ShapeFieldTypeTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/ShapeFieldTypeTests.java index f08e9944accaf..efa127a5aa3b4 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/ShapeFieldTypeTests.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/ShapeFieldTypeTests.java @@ -18,12 +18,9 @@ public class ShapeFieldTypeTests extends FieldTypeTestCase { public void testFetchSourceValue() throws IOException { - MappedFieldType mapper = new ShapeFieldMapper.Builder("field", false, true) - .build(MapperBuilderContext.ROOT) - .fieldType(); + MappedFieldType mapper = new ShapeFieldMapper.Builder("field", false, true).build(MapperBuilderContext.ROOT).fieldType(); - Map jsonLineString = Map.of("type", "LineString", "coordinates", - List.of(List.of(42.0, 27.1), List.of(30.0, 50.0))); + Map jsonLineString = Map.of("type", "LineString", "coordinates", List.of(List.of(42.0, 27.1), List.of(30.0, 50.0))); Map jsonPoint = Map.of("type", "Point", "coordinates", List.of(14.3, 15.0)); Map jsonMalformed = Map.of("type", "Point", "coordinates", "foo"); String wktLineString = "LINESTRING (42.0 27.1, 30.0 50.0)"; diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/query/GeoShapeWithDocValuesQueryBuilderTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/query/GeoShapeWithDocValuesQueryBuilderTests.java index 0a86cbd0d8923..48f1c4401d3be 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/query/GeoShapeWithDocValuesQueryBuilderTests.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/query/GeoShapeWithDocValuesQueryBuilderTests.java @@ -41,11 +41,17 @@ protected Collection> getPlugins() { @Override protected void initializeAdditionalMappings(MapperService mapperService) throws IOException { if (randomBoolean()) { - mapperService.merge("_doc", new CompressedXContent(Strings.toString(PutMappingRequest.simpleMapping( - "test", "type=geo_shape"))), MapperService.MergeReason.MAPPING_UPDATE); + mapperService.merge( + "_doc", + new CompressedXContent(Strings.toString(PutMappingRequest.simpleMapping("test", "type=geo_shape"))), + MapperService.MergeReason.MAPPING_UPDATE + ); } else { - mapperService.merge("_doc", new CompressedXContent(Strings.toString(PutMappingRequest.simpleMapping( - "test", "type=geo_shape,doc_values=false"))), MapperService.MergeReason.MAPPING_UPDATE); + mapperService.merge( + "_doc", + new CompressedXContent(Strings.toString(PutMappingRequest.simpleMapping("test", "type=geo_shape,doc_values=false"))), + MapperService.MergeReason.MAPPING_UPDATE + ); } } @@ -54,7 +60,8 @@ protected GeoShapeQueryBuilder doCreateTestQueryBuilder() { Geometry geometry = randomFrom( GeometryTestUtils.randomPoint(false), GeometryTestUtils.randomLine(false), - GeometryTestUtils.randomPolygon(false)); + GeometryTestUtils.randomPolygon(false) + ); return new GeoShapeQueryBuilder("test", geometry); } diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/query/GeoShapeWithDocValuesQueryTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/query/GeoShapeWithDocValuesQueryTests.java index 79add0358a40e..f579ffe79853a 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/query/GeoShapeWithDocValuesQueryTests.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/query/GeoShapeWithDocValuesQueryTests.java @@ -11,12 +11,12 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.geo.GeoJson; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.geo.GeometryTestUtils; import org.elasticsearch.geometry.MultiPoint; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.search.geo.GeoShapeQueryTestCase; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xpack.spatial.LocalStateSpatialPlugin; import java.io.IOException; @@ -24,8 +24,8 @@ import java.util.Collections; import static org.elasticsearch.action.support.WriteRequest.RefreshPolicy.IMMEDIATE; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.index.query.QueryBuilders.geoShapeQuery; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; public class GeoShapeWithDocValuesQueryTests extends GeoShapeQueryTestCase { @@ -36,38 +36,44 @@ protected Collection> getPlugins() { @Override protected void createMapping(String indexName, String fieldName, Settings settings) throws Exception { - XContentBuilder xcb = XContentFactory.jsonBuilder().startObject() - .startObject("properties").startObject(fieldName) - .field("type", "geo_shape") - .endObject().endObject().endObject(); - client().admin().indices().prepareCreate(indexName).setMapping(xcb).setSettings(settings).get(); - } - - public void testFieldAlias() throws IOException { - String mapping = Strings.toString(XContentFactory.jsonBuilder() + XContentBuilder xcb = XContentFactory.jsonBuilder() .startObject() .startObject("properties") - .startObject(defaultGeoFieldName) + .startObject(fieldName) .field("type", "geo_shape") .endObject() - .startObject("alias") - .field("type", "alias") - .field("path", defaultGeoFieldName) - .endObject() .endObject() - .endObject()); + .endObject(); + client().admin().indices().prepareCreate(indexName).setMapping(xcb).setSettings(settings).get(); + } + + public void testFieldAlias() throws IOException { + String mapping = Strings.toString( + XContentFactory.jsonBuilder() + .startObject() + .startObject("properties") + .startObject(defaultGeoFieldName) + .field("type", "geo_shape") + .endObject() + .startObject("alias") + .field("type", "alias") + .field("path", defaultGeoFieldName) + .endObject() + .endObject() + .endObject() + ); client().admin().indices().prepareCreate(defaultIndexName).setMapping(mapping).get(); ensureGreen(); MultiPoint multiPoint = GeometryTestUtils.randomMultiPoint(false); - client().prepareIndex(defaultIndexName).setId("1") + client().prepareIndex(defaultIndexName) + .setId("1") .setSource(GeoJson.toXContent(multiPoint, jsonBuilder().startObject().field(defaultGeoFieldName), null).endObject()) - .setRefreshPolicy(IMMEDIATE).get(); - - SearchResponse response = client().prepareSearch(defaultIndexName) - .setQuery(geoShapeQuery("alias", multiPoint)) + .setRefreshPolicy(IMMEDIATE) .get(); + + SearchResponse response = client().prepareSearch(defaultIndexName).setQuery(geoShapeQuery("alias", multiPoint)).get(); assertEquals(1, response.getHits().getTotalHits().value); } } diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/query/LegacyGeoShapeWithDocValuesQueryTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/query/LegacyGeoShapeWithDocValuesQueryTests.java index e98e8a5c77b25..0a0dc383bc72c 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/query/LegacyGeoShapeWithDocValuesQueryTests.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/query/LegacyGeoShapeWithDocValuesQueryTests.java @@ -12,8 +12,6 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.geo.GeoJson; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.geo.GeometryTestUtils; import org.elasticsearch.geometry.Geometry; import org.elasticsearch.geometry.MultiPoint; @@ -23,6 +21,8 @@ import org.elasticsearch.plugins.Plugin; import org.elasticsearch.search.geo.GeoShapeQueryTestCase; import org.elasticsearch.test.VersionUtils; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xpack.spatial.LocalStateSpatialPlugin; import java.io.IOException; @@ -30,30 +30,30 @@ import java.util.Collections; import static org.elasticsearch.action.support.WriteRequest.RefreshPolicy.IMMEDIATE; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.index.query.QueryBuilders.geoIntersectionQuery; import static org.elasticsearch.index.query.QueryBuilders.geoShapeQuery; import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.Matchers.containsString; public class LegacyGeoShapeWithDocValuesQueryTests extends GeoShapeQueryTestCase { - @SuppressWarnings( "deprecation" ) + @SuppressWarnings("deprecation") private static final String[] PREFIX_TREES = new String[] { LegacyGeoShapeFieldMapper.PrefixTrees.GEOHASH, - LegacyGeoShapeFieldMapper.PrefixTrees.QUADTREE - }; + LegacyGeoShapeFieldMapper.PrefixTrees.QUADTREE }; @Override protected Collection> getPlugins() { return Collections.singleton(LocalStateSpatialPlugin.class); } - @Override protected void createMapping(String indexName, String fieldName, Settings settings) throws Exception { - final XContentBuilder xcb = XContentFactory.jsonBuilder().startObject() - .startObject("properties").startObject(fieldName) + final XContentBuilder xcb = XContentFactory.jsonBuilder() + .startObject() + .startObject("properties") + .startObject(fieldName) .field("type", "geo_shape") .field("tree", randomFrom(PREFIX_TREES)) .endObject() @@ -61,11 +61,14 @@ protected void createMapping(String indexName, String fieldName, Settings settin .endObject(); final Settings finalSetting; - MapperParsingException ex = - expectThrows(MapperParsingException.class, - () -> client().admin().indices().prepareCreate(indexName).setMapping(xcb).setSettings(settings).get()); - assertThat(ex.getMessage(), - containsString("using deprecated parameters [tree] in mapper [" + fieldName + "] of type [geo_shape] is no longer allowed")); + MapperParsingException ex = expectThrows( + MapperParsingException.class, + () -> client().admin().indices().prepareCreate(indexName).setMapping(xcb).setSettings(settings).get() + ); + assertThat( + ex.getMessage(), + containsString("using deprecated parameters [tree] in mapper [" + fieldName + "] of type [geo_shape] is no longer allowed") + ); Version version = VersionUtils.randomPreviousCompatibleVersion(random(), Version.V_8_0_0); finalSetting = settings(version).put(settings).build(); client().admin().indices().prepareCreate(indexName).setMapping(xcb).setSettings(finalSetting).get(); @@ -77,22 +80,32 @@ protected boolean forbidPrivateIndexSettings() { } public void testPointsOnlyExplicit() throws Exception { - String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject() - .startObject("properties").startObject(defaultGeoFieldName) - .field("type", "geo_shape") - .field("tree", randomBoolean() ? "quadtree" : "geohash") - .field("tree_levels", "6") - .field("distance_error_pct", "0.01") - .field("points_only", true) - .endObject() - .endObject().endObject()); - - MapperParsingException ex = - expectThrows(MapperParsingException.class, - () -> client().admin().indices().prepareCreate("geo_points_only").setMapping(mapping).get()); - assertThat(ex.getMessage(), - containsString("using deprecated parameters [points_only, tree, distance_error_pct, tree_levels] " + - "in mapper [geo] of type [geo_shape] is no longer allowed")); + String mapping = Strings.toString( + XContentFactory.jsonBuilder() + .startObject() + .startObject("properties") + .startObject(defaultGeoFieldName) + .field("type", "geo_shape") + .field("tree", randomBoolean() ? "quadtree" : "geohash") + .field("tree_levels", "6") + .field("distance_error_pct", "0.01") + .field("points_only", true) + .endObject() + .endObject() + .endObject() + ); + + MapperParsingException ex = expectThrows( + MapperParsingException.class, + () -> client().admin().indices().prepareCreate("geo_points_only").setMapping(mapping).get() + ); + assertThat( + ex.getMessage(), + containsString( + "using deprecated parameters [points_only, tree, distance_error_pct, tree_levels] " + + "in mapper [geo] of type [geo_shape] is no longer allowed" + ) + ); Version version = VersionUtils.randomPreviousCompatibleVersion(random(), Version.V_8_0_0); Settings settings = settings(version).build(); @@ -101,41 +114,53 @@ public void testPointsOnlyExplicit() throws Exception { // MULTIPOINT MultiPoint multiPoint = GeometryTestUtils.randomMultiPoint(false); - client().prepareIndex("geo_points_only").setId("1") + client().prepareIndex("geo_points_only") + .setId("1") .setSource(GeoJson.toXContent(multiPoint, jsonBuilder().startObject().field(defaultGeoFieldName), null).endObject()) - .setRefreshPolicy(IMMEDIATE).get(); + .setRefreshPolicy(IMMEDIATE) + .get(); // POINT - Point point = GeometryTestUtils.randomPoint(false); - client().prepareIndex("geo_points_only").setId("2") + Point point = GeometryTestUtils.randomPoint(false); + client().prepareIndex("geo_points_only") + .setId("2") .setSource(GeoJson.toXContent(point, jsonBuilder().startObject().field(defaultGeoFieldName), null).endObject()) - .setRefreshPolicy(IMMEDIATE).get(); + .setRefreshPolicy(IMMEDIATE) + .get(); // test that point was inserted - SearchResponse response = client().prepareSearch("geo_points_only") - .setQuery(matchAllQuery()) - .get(); + SearchResponse response = client().prepareSearch("geo_points_only").setQuery(matchAllQuery()).get(); assertEquals(2, response.getHits().getTotalHits().value); } public void testPointsOnly() throws Exception { - String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject() - .startObject("properties").startObject(defaultGeoFieldName) - .field("type", "geo_shape") - .field("tree", randomBoolean() ? "quadtree" : "geohash") - .field("tree_levels", "6") - .field("distance_error_pct", "0.01") - .field("points_only", true) - .endObject() - .endObject().endObject()); - - MapperParsingException ex = - expectThrows(MapperParsingException.class, - () -> client().admin().indices().prepareCreate("geo_points_only").setMapping(mapping).get()); - assertThat(ex.getMessage(), - containsString("using deprecated parameters [points_only, tree, distance_error_pct, tree_levels] " + - "in mapper [geo] of type [geo_shape] is no longer allowed")); + String mapping = Strings.toString( + XContentFactory.jsonBuilder() + .startObject() + .startObject("properties") + .startObject(defaultGeoFieldName) + .field("type", "geo_shape") + .field("tree", randomBoolean() ? "quadtree" : "geohash") + .field("tree_levels", "6") + .field("distance_error_pct", "0.01") + .field("points_only", true) + .endObject() + .endObject() + .endObject() + ); + + MapperParsingException ex = expectThrows( + MapperParsingException.class, + () -> client().admin().indices().prepareCreate("geo_points_only").setMapping(mapping).get() + ); + assertThat( + ex.getMessage(), + containsString( + "using deprecated parameters [points_only, tree, distance_error_pct, tree_levels] " + + "in mapper [geo] of type [geo_shape] is no longer allowed" + ) + ); Version version = VersionUtils.randomPreviousCompatibleVersion(random(), Version.V_8_0_0); Settings settings = settings(version).build(); @@ -144,9 +169,11 @@ public void testPointsOnly() throws Exception { Geometry geometry = GeometryTestUtils.randomGeometry(false); try { - client().prepareIndex("geo_points_only").setId("1") + client().prepareIndex("geo_points_only") + .setId("1") .setSource(GeoJson.toXContent(geometry, jsonBuilder().startObject().field(defaultGeoFieldName), null).endObject()) - .setRefreshPolicy(IMMEDIATE).get(); + .setRefreshPolicy(IMMEDIATE) + .get(); } catch (MapperParsingException e) { // Random geometry generator created something other than a POINT type, verify the correct exception is thrown assertThat(e.getMessage(), containsString("is configured for points only")); @@ -154,31 +181,37 @@ public void testPointsOnly() throws Exception { } // test that point was inserted - SearchResponse response = - client().prepareSearch("geo_points_only").setQuery(geoIntersectionQuery(defaultGeoFieldName, geometry)).get(); + SearchResponse response = client().prepareSearch("geo_points_only") + .setQuery(geoIntersectionQuery(defaultGeoFieldName, geometry)) + .get(); assertEquals(1, response.getHits().getTotalHits().value); } public void testFieldAlias() throws IOException { - String mapping = Strings.toString(XContentFactory.jsonBuilder() - .startObject() - .startObject("properties") - .startObject(defaultGeoFieldName) - .field("type", "geo_shape") - .field("tree", randomBoolean() ? "quadtree" : "geohash") - .endObject() - .startObject("alias") - .field("type", "alias") - .field("path", defaultGeoFieldName) - .endObject() - .endObject() - .endObject()); - - MapperParsingException ex = - expectThrows(MapperParsingException.class, - () -> client().admin().indices().prepareCreate(defaultIndexName).setMapping(mapping).get()); - assertThat(ex.getMessage(), - containsString("using deprecated parameters [tree] in mapper [geo] of type [geo_shape] is no longer allowed")); + String mapping = Strings.toString( + XContentFactory.jsonBuilder() + .startObject() + .startObject("properties") + .startObject(defaultGeoFieldName) + .field("type", "geo_shape") + .field("tree", randomBoolean() ? "quadtree" : "geohash") + .endObject() + .startObject("alias") + .field("type", "alias") + .field("path", defaultGeoFieldName) + .endObject() + .endObject() + .endObject() + ); + + MapperParsingException ex = expectThrows( + MapperParsingException.class, + () -> client().admin().indices().prepareCreate(defaultIndexName).setMapping(mapping).get() + ); + assertThat( + ex.getMessage(), + containsString("using deprecated parameters [tree] in mapper [geo] of type [geo_shape] is no longer allowed") + ); Version version = VersionUtils.randomPreviousCompatibleVersion(random(), Version.V_8_0_0); Settings settings = settings(version).build(); @@ -186,13 +219,13 @@ public void testFieldAlias() throws IOException { ensureGreen(); MultiPoint multiPoint = GeometryTestUtils.randomMultiPoint(false); - client().prepareIndex(defaultIndexName).setId("1") + client().prepareIndex(defaultIndexName) + .setId("1") .setSource(GeoJson.toXContent(multiPoint, jsonBuilder().startObject().field(defaultGeoFieldName), null).endObject()) - .setRefreshPolicy(IMMEDIATE).get(); - - SearchResponse response = client().prepareSearch(defaultIndexName) - .setQuery(geoShapeQuery("alias", multiPoint)) + .setRefreshPolicy(IMMEDIATE) .get(); + + SearchResponse response = client().prepareSearch(defaultIndexName).setQuery(geoShapeQuery("alias", multiPoint)).get(); assertEquals(1, response.getHits().getTotalHits().value); } } diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/query/ShapeQueryBuilderOverPointTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/query/ShapeQueryBuilderOverPointTests.java index 9b1df1acb2333..90b57b95b03e3 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/query/ShapeQueryBuilderOverPointTests.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/query/ShapeQueryBuilderOverPointTests.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.xpack.spatial.index.query; - import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest; import org.elasticsearch.common.Strings; import org.elasticsearch.common.compress.CompressedXContent; @@ -18,13 +17,15 @@ import java.io.IOException; - public class ShapeQueryBuilderOverPointTests extends ShapeQueryBuilderTests { @Override protected void initializeAdditionalMappings(MapperService mapperService) throws IOException { - mapperService.merge(docType, new CompressedXContent(Strings.toString(PutMappingRequest.simpleMapping( - fieldName(), "type=point"))), MapperService.MergeReason.MAPPING_UPDATE); + mapperService.merge( + docType, + new CompressedXContent(Strings.toString(PutMappingRequest.simpleMapping(fieldName(), "type=point"))), + MapperService.MergeReason.MAPPING_UPDATE + ); } @Override diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/query/ShapeQueryBuilderOverShapeTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/query/ShapeQueryBuilderOverShapeTests.java index 6bd672ec4abfd..cf0ebb1e43af2 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/query/ShapeQueryBuilderOverShapeTests.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/query/ShapeQueryBuilderOverShapeTests.java @@ -23,8 +23,11 @@ public class ShapeQueryBuilderOverShapeTests extends ShapeQueryBuilderTests { @Override protected void initializeAdditionalMappings(MapperService mapperService) throws IOException { - mapperService.merge(docType, new CompressedXContent(Strings.toString(PutMappingRequest.simpleMapping( - fieldName(), "type=shape"))), MapperService.MergeReason.MAPPING_UPDATE); + mapperService.merge( + docType, + new CompressedXContent(Strings.toString(PutMappingRequest.simpleMapping(fieldName(), "type=shape"))), + MapperService.MergeReason.MAPPING_UPDATE + ); } @Override @@ -34,8 +37,7 @@ protected ShapeRelation getShapeRelation(ShapeType type) { if (type == ShapeType.LINESTRING || type == ShapeType.MULTILINESTRING) { return randomFrom(ShapeRelation.DISJOINT, ShapeRelation.INTERSECTS, ShapeRelation.CONTAINS); } else { - return randomFrom(ShapeRelation.DISJOINT, ShapeRelation.INTERSECTS, - ShapeRelation.WITHIN, ShapeRelation.CONTAINS); + return randomFrom(ShapeRelation.DISJOINT, ShapeRelation.INTERSECTS, ShapeRelation.WITHIN, ShapeRelation.CONTAINS); } } else { if (type == ShapeType.LINESTRING || type == ShapeType.MULTILINESTRING) { diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/query/ShapeQueryBuilderTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/query/ShapeQueryBuilderTests.java index c146d939299e2..7f6aef1fe30a6 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/query/ShapeQueryBuilderTests.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/query/ShapeQueryBuilderTests.java @@ -18,20 +18,20 @@ import org.elasticsearch.common.geo.GeoJson; import org.elasticsearch.common.geo.ShapeRelation; import org.elasticsearch.common.io.stream.BytesStreamOutput; -import org.elasticsearch.xcontent.ToXContentObject; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.geometry.Geometry; import org.elasticsearch.geometry.ShapeType; import org.elasticsearch.index.get.GetResult; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; -import org.elasticsearch.index.query.SearchExecutionContext; import org.elasticsearch.index.query.QueryShardException; import org.elasticsearch.index.query.Rewriteable; +import org.elasticsearch.index.query.SearchExecutionContext; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.test.AbstractQueryTestCase; +import org.elasticsearch.xcontent.ToXContentObject; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.spatial.LocalStateSpatialPlugin; import org.junit.After; @@ -141,8 +141,7 @@ public void testNoShape() { } public void testNoIndexedShape() { - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> new ShapeQueryBuilder(fieldName(), null, null)); + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> new ShapeQueryBuilder(fieldName(), null, null)); assertEquals("either shape or indexedShapeId is required", e.getMessage()); } @@ -154,20 +153,19 @@ public void testNoRelation() { } public void testFromJson() throws IOException { - String json = - "{\n" + - " \"shape\" : {\n" + - " \"geometry\" : {\n" + - " \"shape\" : {\n" + - " \"type\" : \"envelope\",\n" + - " \"coordinates\" : [ [ 1300.0, 5300.0 ], [ 1400.0, 5200.0 ] ]\n" + - " },\n" + - " \"relation\" : \"intersects\"\n" + - " },\n" + - " \"ignore_unmapped\" : false,\n" + - " \"boost\" : 42.0\n" + - " }\n" + - "}"; + String json = "{\n" + + " \"shape\" : {\n" + + " \"geometry\" : {\n" + + " \"shape\" : {\n" + + " \"type\" : \"envelope\",\n" + + " \"coordinates\" : [ [ 1300.0, 5300.0 ], [ 1400.0, 5200.0 ] ]\n" + + " },\n" + + " \"relation\" : \"intersects\"\n" + + " },\n" + + " \"ignore_unmapped\" : false,\n" + + " \"boost\" : 42.0\n" + + " }\n" + + "}"; ShapeQueryBuilder parsed = (ShapeQueryBuilder) parseQuery(json); checkGeneratedJson(json.replaceAll("envelope", "Envelope"), parsed); assertEquals(json, 42.0, parsed.boost(), 0.0001); @@ -177,8 +175,10 @@ public void testFromJson() throws IOException { public void testMustRewrite() { ShapeQueryBuilder query = doCreateTestQueryBuilder(true); - UnsupportedOperationException e = expectThrows(UnsupportedOperationException.class, - () -> query.toQuery(createSearchExecutionContext())); + UnsupportedOperationException e = expectThrows( + UnsupportedOperationException.class, + () -> query.toQuery(createSearchExecutionContext()) + ); assertEquals("query must be rewritten first", e.getMessage()); QueryBuilder rewrite = rewriteAndFetch(query, createSearchExecutionContext()); ShapeQueryBuilder geoShapeQueryBuilder = new ShapeQueryBuilder(fieldName(), indexedShapeToReturn); @@ -188,17 +188,13 @@ public void testMustRewrite() { public void testMultipleRewrite() { ShapeQueryBuilder shape = doCreateTestQueryBuilder(true); - QueryBuilder builder = new BoolQueryBuilder() - .should(shape) - .should(shape); + QueryBuilder builder = new BoolQueryBuilder().should(shape).should(shape); builder = rewriteAndFetch(builder, createSearchExecutionContext()); ShapeQueryBuilder expectedShape = new ShapeQueryBuilder(fieldName(), indexedShapeToReturn); expectedShape.relation(shape.relation()); - QueryBuilder expected = new BoolQueryBuilder() - .should(expectedShape) - .should(expectedShape); + QueryBuilder expected = new BoolQueryBuilder().should(expectedShape).should(expectedShape); assertEquals(expected, builder); } @@ -265,8 +261,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws } catch (IOException ex) { throw new ElasticsearchException("boom", ex); } - return new GetResponse(new GetResult(indexedShapeIndex, indexedShapeId, 0, 1, 0, true, new BytesArray(json), - null, null)); + return new GetResponse(new GetResult(indexedShapeIndex, indexedShapeId, 0, 1, 0, true, new BytesArray(json), null, null)); } @Override diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/ingest/CircleProcessorFactoryTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/ingest/CircleProcessorFactoryTests.java index 9483aa1ffb44d..9c036b68e3acc 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/ingest/CircleProcessorFactoryTests.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/ingest/CircleProcessorFactoryTests.java @@ -8,7 +8,6 @@ import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.test.ESTestCase; - import org.junit.Before; import java.util.HashMap; @@ -66,8 +65,10 @@ public void testCreateInvalidShapeType() { public void testCreateMissingField() { Map config = new HashMap<>(); String processorTag = randomAlphaOfLength(10); - ElasticsearchParseException e = expectThrows(ElasticsearchParseException.class, - () -> factory.create(null, processorTag, null, config)); + ElasticsearchParseException e = expectThrows( + ElasticsearchParseException.class, + () -> factory.create(null, processorTag, null, config) + ); assertThat(e.getMessage(), equalTo("[field] required property is missing")); } @@ -90,8 +91,10 @@ public void testCreateWithNoErrorDistanceDefined() { Map config = new HashMap<>(); config.put("field", "field1"); String processorTag = randomAlphaOfLength(10); - ElasticsearchParseException e = expectThrows(ElasticsearchParseException.class, - () -> factory.create(null, processorTag, null, config)); + ElasticsearchParseException e = expectThrows( + ElasticsearchParseException.class, + () -> factory.create(null, processorTag, null, config) + ); assertThat(e.getMessage(), equalTo("[error_distance] required property is missing")); } } diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/ingest/CircleProcessorTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/ingest/CircleProcessorTests.java index 5b449b855e842..7ac90673a557d 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/ingest/CircleProcessorTests.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/ingest/CircleProcessorTests.java @@ -14,15 +14,11 @@ import org.apache.lucene.search.Query; import org.apache.lucene.store.Directory; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.geo.GeoJson; -import org.elasticsearch.common.geo.ShapeRelation; import org.elasticsearch.common.geo.Orientation; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.common.geo.ShapeRelation; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.core.Tuple; import org.elasticsearch.geometry.Circle; import org.elasticsearch.geometry.Geometry; import org.elasticsearch.geometry.Point; @@ -34,6 +30,10 @@ import org.elasticsearch.ingest.IngestDocument; import org.elasticsearch.ingest.RandomDocumentPicks; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.spatial.SpatialUtils; import org.elasticsearch.xpack.spatial.index.mapper.GeoShapeWithDocValuesFieldMapper.GeoShapeWithDocValuesFieldType; import org.elasticsearch.xpack.spatial.index.mapper.ShapeFieldMapper.ShapeFieldType; @@ -71,7 +71,7 @@ public void testNumSides() { // radius is much larger than error distance assertThat(processor.numSides(Math.pow(radiusDistanceMeters, 100)), equalTo(1000)); // radius is 5 times longer than error distance - assertThat(processor.numSides(5*radiusDistanceMeters), equalTo(10)); + assertThat(processor.numSides(5 * radiusDistanceMeters), equalTo(10)); } @@ -122,8 +122,11 @@ public void testJson() throws IOException { Map polyMap = ingestDocument.getFieldValue("field", Map.class); XContentBuilder builder = XContentFactory.jsonBuilder(); GeoJson.toXContent(expectedPoly, builder, ToXContent.EMPTY_PARAMS); - Tuple> expected = XContentHelper.convertToMap(BytesReference.bytes(builder), - true, XContentType.JSON); + Tuple> expected = XContentHelper.convertToMap( + BytesReference.bytes(builder), + true, + XContentType.JSON + ); assertThat(polyMap, equalTo(expected.v2())); } @@ -133,7 +136,7 @@ public void testWKT() { map.put("field", WellKnownText.toWKT(circle)); Geometry expectedPoly = SpatialUtils.createRegularGeoShapePolygon(circle, 4); IngestDocument ingestDocument = new IngestDocument(map, Collections.emptyMap()); - CircleProcessor processor = new CircleProcessor("tag", null, "field", "field",false, 2, GEO_SHAPE); + CircleProcessor processor = new CircleProcessor("tag", null, "field", "field", false, 2, GEO_SHAPE); processor.execute(ingestDocument); String polyString = ingestDocument.getFieldValue("field", String.class); assertThat(polyString, equalTo(WellKnownText.toWKT(expectedPoly))); @@ -167,7 +170,7 @@ public void testInvalidType() { IngestDocument ingestDocument = new IngestDocument(map, Collections.emptyMap()); CircleProcessor processor = new CircleProcessor("tag", null, "field", "field", false, 10, GEO_SHAPE); - for (Object value : new Object[] { null, 4.0, "not_circle"}) { + for (Object value : new Object[] { null, 4.0, "not_circle" }) { field.put("type", value); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> processor.execute(ingestDocument)); assertThat(e.getMessage(), equalTo("invalid circle definition")); @@ -183,7 +186,7 @@ public void testInvalidCoordinates() { IngestDocument ingestDocument = new IngestDocument(map, Collections.emptyMap()); CircleProcessor processor = new CircleProcessor("tag", null, "field", "field", false, 10, GEO_SHAPE); - for (Object value : new Object[] { null, "not_circle"}) { + for (Object value : new Object[] { null, "not_circle" }) { field.put("coordinates", value); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> processor.execute(ingestDocument)); assertThat(e.getMessage(), equalTo("invalid circle definition")); @@ -199,7 +202,7 @@ public void testInvalidRadius() { IngestDocument ingestDocument = new IngestDocument(map, Collections.emptyMap()); CircleProcessor processor = new CircleProcessor("tag", null, "field", "field", false, 10, GEO_SHAPE); - for (Object value : new Object[] { null, "NotNumber", "10.0fs"}) { + for (Object value : new Object[] { null, "NotNumber", "10.0fs" }) { field.put("radius", value); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> processor.execute(ingestDocument)); assertThat(e.getMessage(), equalTo("invalid circle definition")); @@ -212,14 +215,25 @@ public void testGeoShapeQueryAcrossDateline() throws IOException { int numSides = randomIntBetween(4, 1000); Geometry geometry = SpatialUtils.createRegularGeoShapePolygon(circle, numSides); - GeoShapeWithDocValuesFieldType shapeType - = new GeoShapeWithDocValuesFieldType(fieldName, true, false, Orientation.RIGHT, null, null, Collections.emptyMap()); + GeoShapeWithDocValuesFieldType shapeType = new GeoShapeWithDocValuesFieldType( + fieldName, + true, + false, + Orientation.RIGHT, + null, + null, + Collections.emptyMap() + ); SearchExecutionContext mockedContext = mock(SearchExecutionContext.class); when(mockedContext.getFieldType(any())).thenReturn(shapeType); Query sameShapeQuery = shapeType.geoShapeQuery(geometry, fieldName, ShapeRelation.INTERSECTS, mockedContext); - Query pointOnDatelineQuery = shapeType.geoShapeQuery(new Point(180, circle.getLat()), fieldName, - ShapeRelation.INTERSECTS, mockedContext); + Query pointOnDatelineQuery = shapeType.geoShapeQuery( + new Point(180, circle.getLat()), + fieldName, + ShapeRelation.INTERSECTS, + mockedContext + ); try (Directory dir = newDirectory(); RandomIndexWriter w = new RandomIndexWriter(random(), dir)) { Document doc = new Document(); @@ -249,8 +263,12 @@ public void testShapeQuery() throws IOException { SearchExecutionContext mockedContext = mock(SearchExecutionContext.class); when(mockedContext.getFieldType(any())).thenReturn(shapeType); Query sameShapeQuery = processor.shapeQuery(geometry, fieldName, ShapeRelation.INTERSECTS, mockedContext); - Query centerPointQuery = processor.shapeQuery(new Point(circle.getLon(), circle.getLat()), fieldName, - ShapeRelation.INTERSECTS, mockedContext); + Query centerPointQuery = processor.shapeQuery( + new Point(circle.getLon(), circle.getLat()), + fieldName, + ShapeRelation.INTERSECTS, + mockedContext + ); try (Directory dir = newDirectory(); RandomIndexWriter w = new RandomIndexWriter(random(), dir)) { Document doc = new Document(); diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/GeoLineAggregationBuilderTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/GeoLineAggregationBuilderTests.java index b97efa6eff4d6..79349b79f8890 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/GeoLineAggregationBuilderTests.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/GeoLineAggregationBuilderTests.java @@ -8,10 +8,10 @@ package org.elasticsearch.xpack.spatial.search.aggregations; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.aggregations.support.MultiValuesSourceFieldConfig; import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -40,14 +40,9 @@ protected Writeable.Reader instanceReader() { @Override protected GeoLineAggregationBuilder createTestInstance() { - MultiValuesSourceFieldConfig pointConfig = new MultiValuesSourceFieldConfig.Builder() - .setFieldName(randomAlphaOfLength(5)) - .build(); - MultiValuesSourceFieldConfig sortConfig = new MultiValuesSourceFieldConfig.Builder() - .setFieldName(randomAlphaOfLength(6)).build(); - GeoLineAggregationBuilder lineAggregationBuilder = new GeoLineAggregationBuilder("_name") - .point(pointConfig) - .sort(sortConfig); + MultiValuesSourceFieldConfig pointConfig = new MultiValuesSourceFieldConfig.Builder().setFieldName(randomAlphaOfLength(5)).build(); + MultiValuesSourceFieldConfig sortConfig = new MultiValuesSourceFieldConfig.Builder().setFieldName(randomAlphaOfLength(6)).build(); + GeoLineAggregationBuilder lineAggregationBuilder = new GeoLineAggregationBuilder("_name").point(pointConfig).sort(sortConfig); if (randomBoolean()) { SortOrder sortOrder = randomFrom(SortOrder.values()); lineAggregationBuilder.sortOrder(sortOrder); @@ -62,16 +57,13 @@ protected GeoLineAggregationBuilder createTestInstance() { } public void testInvalidSize() { - MultiValuesSourceFieldConfig pointConfig = new MultiValuesSourceFieldConfig.Builder() - .setFieldName(randomAlphaOfLength(5)) - .build(); - MultiValuesSourceFieldConfig sortConfig = new MultiValuesSourceFieldConfig.Builder() - .setFieldName(randomAlphaOfLength(6)).build(); - GeoLineAggregationBuilder lineAggregationBuilder = new GeoLineAggregationBuilder("_name") - .point(pointConfig) - .sort(sortConfig); + MultiValuesSourceFieldConfig pointConfig = new MultiValuesSourceFieldConfig.Builder().setFieldName(randomAlphaOfLength(5)).build(); + MultiValuesSourceFieldConfig sortConfig = new MultiValuesSourceFieldConfig.Builder().setFieldName(randomAlphaOfLength(6)).build(); + GeoLineAggregationBuilder lineAggregationBuilder = new GeoLineAggregationBuilder("_name").point(pointConfig).sort(sortConfig); expectThrows(IllegalArgumentException.class, () -> lineAggregationBuilder.size(0)); - expectThrows(IllegalArgumentException.class, - () -> lineAggregationBuilder.size(GeoLineAggregationBuilder.MAX_PATH_SIZE + randomIntBetween(1, 10))); + expectThrows( + IllegalArgumentException.class, + () -> lineAggregationBuilder.size(GeoLineAggregationBuilder.MAX_PATH_SIZE + randomIntBetween(1, 10)) + ); } } diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/GeoLineAggregatorTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/GeoLineAggregatorTests.java index bbd8de860c0d4..85f736bdb1bcf 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/GeoLineAggregatorTests.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/GeoLineAggregatorTests.java @@ -55,39 +55,32 @@ protected List getSearchPlugins() { // test that missing values are ignored public void testMixedMissingValues() throws IOException { - MultiValuesSourceFieldConfig valueConfig = new MultiValuesSourceFieldConfig.Builder() - .setFieldName("value_field") - .build(); + MultiValuesSourceFieldConfig valueConfig = new MultiValuesSourceFieldConfig.Builder().setFieldName("value_field").build(); MultiValuesSourceFieldConfig sortConfig = new MultiValuesSourceFieldConfig.Builder().setFieldName("sort_field").build(); - GeoLineAggregationBuilder lineAggregationBuilder = new GeoLineAggregationBuilder("_name") - .point(valueConfig) + GeoLineAggregationBuilder lineAggregationBuilder = new GeoLineAggregationBuilder("_name").point(valueConfig) .sortOrder(SortOrder.ASC) .sort(sortConfig) .size(10); - TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder("_name") - .field("group_id") + TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder("_name").field("group_id") .subAggregation(lineAggregationBuilder); long lonLat = (((long) GeoEncodingUtils.encodeLongitude(90.0)) << 32) | GeoEncodingUtils.encodeLatitude(45.0) & 0xffffffffL; // input documents for testing // ---------------------------- - // | sort_field | value_field | + // | sort_field | value_field | // ---------------------------- - // | N/A | lonLat | - // | 1 | N/A | - // | 2 | lonLat | - // | N/A | N/A | - // | 4 | lonLat | + // | N/A | lonLat | + // | 1 | N/A | + // | 2 | lonLat | + // | N/A | N/A | + // | 4 | lonLat | // ---------------------------- - double[] sortValues = new double[]{ -1, 1, 2, -1, 4 }; - long[] points = new long[] { lonLat, -1, lonLat, -1,lonLat }; - //expected + double[] sortValues = new double[] { -1, 1, 2, -1, 4 }; + long[] points = new long[] { lonLat, -1, lonLat, -1, lonLat }; + // expected long[] expectedAggPoints = new long[] { lonLat, lonLat }; - double[] expectedAggSortValues = new double[]{ - NumericUtils.doubleToSortableLong(2), - NumericUtils.doubleToSortableLong(4) - }; + double[] expectedAggSortValues = new double[] { NumericUtils.doubleToSortableLong(2), NumericUtils.doubleToSortableLong(4) }; testCase(new MatchAllDocsQuery(), aggregationBuilder, iw -> { for (int i = 0; i < points.length; i++) { @@ -112,29 +105,27 @@ public void testMixedMissingValues() throws IOException { } public void testMissingGeoPointValueField() throws IOException { - MultiValuesSourceFieldConfig valueConfig = new MultiValuesSourceFieldConfig.Builder() - .setFieldName("value_field") - .build(); + MultiValuesSourceFieldConfig valueConfig = new MultiValuesSourceFieldConfig.Builder().setFieldName("value_field").build(); MultiValuesSourceFieldConfig sortConfig = new MultiValuesSourceFieldConfig.Builder().setFieldName("sort_field").build(); - GeoLineAggregationBuilder lineAggregationBuilder = new GeoLineAggregationBuilder("_name") - .point(valueConfig) + GeoLineAggregationBuilder lineAggregationBuilder = new GeoLineAggregationBuilder("_name").point(valueConfig) .sortOrder(SortOrder.ASC) .sort(sortConfig) .size(10); - TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder("_name") - .field("group_id") + TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder("_name").field("group_id") .subAggregation(lineAggregationBuilder); - //input - double[] sortValues = new double[]{1, 0, 2, 0, 3, 4, 5}; + // input + double[] sortValues = new double[] { 1, 0, 2, 0, 3, 4, 5 }; testCase(new MatchAllDocsQuery(), aggregationBuilder, iw -> { for (int i = 0; i < sortValues.length; i++) { - iw.addDocument(Arrays.asList( - new SortedNumericDocValuesField("sort_field", NumericUtils.doubleToSortableLong(sortValues[i])), - new SortedDocValuesField("group_id", new BytesRef("group") - ))); + iw.addDocument( + Arrays.asList( + new SortedNumericDocValuesField("sort_field", NumericUtils.doubleToSortableLong(sortValues[i])), + new SortedDocValuesField("group_id", new BytesRef("group")) + ) + ); } }, terms -> { assertThat(terms.getBuckets().size(), equalTo(1)); @@ -145,37 +136,36 @@ public void testMissingGeoPointValueField() throws IOException { } public void testMissingSortField() throws IOException { - MultiValuesSourceFieldConfig valueConfig = new MultiValuesSourceFieldConfig.Builder() - .setFieldName("value_field") - .build(); + MultiValuesSourceFieldConfig valueConfig = new MultiValuesSourceFieldConfig.Builder().setFieldName("value_field").build(); MultiValuesSourceFieldConfig sortConfig = new MultiValuesSourceFieldConfig.Builder().setFieldName("sort_field").build(); - GeoLineAggregationBuilder lineAggregationBuilder = new GeoLineAggregationBuilder("_name") - .point(valueConfig) + GeoLineAggregationBuilder lineAggregationBuilder = new GeoLineAggregationBuilder("_name").point(valueConfig) .sortOrder(SortOrder.ASC) .sort(sortConfig) .size(10); - TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder("_name") - .field("group_id") + TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder("_name").field("group_id") .subAggregation(lineAggregationBuilder); long lonLat = (((long) GeoEncodingUtils.encodeLongitude(90.0)) << 32) | GeoEncodingUtils.encodeLatitude(45.0) & 0xffffffffL; - //input - long[] points = new long[] {lonLat, 0, lonLat, 0,lonLat, lonLat, lonLat}; - //expected - long[] expectedAggPoints = new long[] {lonLat, lonLat, lonLat, lonLat, lonLat}; - double[] expectedAggSortValues = new double[]{ + // input + long[] points = new long[] { lonLat, 0, lonLat, 0, lonLat, lonLat, lonLat }; + // expected + long[] expectedAggPoints = new long[] { lonLat, lonLat, lonLat, lonLat, lonLat }; + double[] expectedAggSortValues = new double[] { NumericUtils.doubleToSortableLong(1), NumericUtils.doubleToSortableLong(2), NumericUtils.doubleToSortableLong(3), NumericUtils.doubleToSortableLong(4), - NumericUtils.doubleToSortableLong(5) - }; + NumericUtils.doubleToSortableLong(5) }; testCase(new MatchAllDocsQuery(), aggregationBuilder, iw -> { for (int i = 0; i < points.length; i++) { - iw.addDocument(Arrays.asList(new LatLonDocValuesField("value_field", 45.0, 90.0), - new SortedDocValuesField("group_id", new BytesRef("group")))); + iw.addDocument( + Arrays.asList( + new LatLonDocValuesField("value_field", 45.0, 90.0), + new SortedDocValuesField("group_id", new BytesRef("group")) + ) + ); } }, terms -> { assertThat(terms.getBuckets().size(), equalTo(1)); @@ -203,17 +193,13 @@ public void testComplete() throws IOException { } public void testCompleteForSizeAndNumDocuments(int size, int numPoints, boolean complete) throws IOException { - MultiValuesSourceFieldConfig valueConfig = new MultiValuesSourceFieldConfig.Builder() - .setFieldName("value_field") - .build(); + MultiValuesSourceFieldConfig valueConfig = new MultiValuesSourceFieldConfig.Builder().setFieldName("value_field").build(); MultiValuesSourceFieldConfig sortConfig = new MultiValuesSourceFieldConfig.Builder().setFieldName("sort_field").build(); - GeoLineAggregationBuilder lineAggregationBuilder = new GeoLineAggregationBuilder("_name") - .point(valueConfig) + GeoLineAggregationBuilder lineAggregationBuilder = new GeoLineAggregationBuilder("_name").point(valueConfig) .sortOrder(SortOrder.ASC) .sort(sortConfig) .size(size); - TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder("_name") - .field("group_id") + TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder("_name").field("group_id") .subAggregation(lineAggregationBuilder); Map lines = new HashMap<>(1); @@ -235,18 +221,19 @@ public void testCompleteForSizeAndNumDocuments(int size, int numPoints, boolean double[] lineSorts = Arrays.copyOf(sortValues, lineSize); new PathArraySorter(linePoints, lineSorts, SortOrder.ASC).sort(); - lines.put(groupOrd, new InternalGeoLine("_name", - linePoints, lineSorts, null, complete, true, SortOrder.ASC, size)); + lines.put(groupOrd, new InternalGeoLine("_name", linePoints, lineSorts, null, complete, true, SortOrder.ASC, size)); testCase(new MatchAllDocsQuery(), aggregationBuilder, iw -> { for (int i = 0; i < points.length; i++) { int x = (int) (points[i] >> 32); int y = (int) points[i]; - iw.addDocument(Arrays.asList(new LatLonDocValuesField("value_field", - GeoEncodingUtils.decodeLatitude(y), - GeoEncodingUtils.decodeLongitude(x)), - new SortedNumericDocValuesField("sort_field", NumericUtils.doubleToSortableLong(sortValues[i])), - new SortedDocValuesField("group_id", new BytesRef(groupOrd)))); + iw.addDocument( + Arrays.asList( + new LatLonDocValuesField("value_field", GeoEncodingUtils.decodeLatitude(y), GeoEncodingUtils.decodeLongitude(x)), + new SortedNumericDocValuesField("sort_field", NumericUtils.doubleToSortableLong(sortValues[i])), + new SortedDocValuesField("group_id", new BytesRef(groupOrd)) + ) + ); } }, terms -> { for (Terms.Bucket bucket : terms.getBuckets()) { @@ -265,37 +252,26 @@ public void testCompleteForSizeAndNumDocuments(int size, int numPoints, boolean public void testEmpty() throws IOException { int size = randomIntBetween(1, GeoLineAggregationBuilder.MAX_PATH_SIZE); - MultiValuesSourceFieldConfig valueConfig = new MultiValuesSourceFieldConfig.Builder() - .setFieldName("value_field") - .build(); + MultiValuesSourceFieldConfig valueConfig = new MultiValuesSourceFieldConfig.Builder().setFieldName("value_field").build(); MultiValuesSourceFieldConfig sortConfig = new MultiValuesSourceFieldConfig.Builder().setFieldName("sort_field").build(); - GeoLineAggregationBuilder lineAggregationBuilder = new GeoLineAggregationBuilder("_name") - .point(valueConfig) + GeoLineAggregationBuilder lineAggregationBuilder = new GeoLineAggregationBuilder("_name").point(valueConfig) .sortOrder(SortOrder.ASC) .sort(sortConfig) .size(size); - TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder("_name") - .field("group_id") + TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder("_name").field("group_id") .subAggregation(lineAggregationBuilder); - testCase(new MatchAllDocsQuery(), aggregationBuilder, iw -> { - }, terms -> { - assertTrue(terms.getBuckets().isEmpty()); - }); + testCase(new MatchAllDocsQuery(), aggregationBuilder, iw -> {}, terms -> { assertTrue(terms.getBuckets().isEmpty()); }); } private void testAggregator(SortOrder sortOrder) throws IOException { int size = randomIntBetween(1, GeoLineAggregationBuilder.MAX_PATH_SIZE); - MultiValuesSourceFieldConfig valueConfig = new MultiValuesSourceFieldConfig.Builder() - .setFieldName("value_field") - .build(); + MultiValuesSourceFieldConfig valueConfig = new MultiValuesSourceFieldConfig.Builder().setFieldName("value_field").build(); MultiValuesSourceFieldConfig sortConfig = new MultiValuesSourceFieldConfig.Builder().setFieldName("sort_field").build(); - GeoLineAggregationBuilder lineAggregationBuilder = new GeoLineAggregationBuilder("_name") - .point(valueConfig) + GeoLineAggregationBuilder lineAggregationBuilder = new GeoLineAggregationBuilder("_name").point(valueConfig) .sortOrder(sortOrder) .sort(sortConfig) .size(size); - TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder("_name") - .field("group_id") + TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder("_name").field("group_id") .subAggregation(lineAggregationBuilder); int numGroups = randomIntBetween(1, 2); @@ -321,8 +297,7 @@ private void testAggregator(SortOrder sortOrder) throws IOException { double[] lineSorts = Arrays.copyOf(sortValues, lineSize); new PathArraySorter(linePoints, lineSorts, SortOrder.ASC).sort(); - lines.put(String.valueOf(groupOrd), new InternalGeoLine("_name", - linePoints, lineSorts, null, complete, true, sortOrder, size)); + lines.put(String.valueOf(groupOrd), new InternalGeoLine("_name", linePoints, lineSorts, null, complete, true, sortOrder, size)); for (int i = 0; i < randomIntBetween(1, numPoints); i++) { int idx1 = randomIntBetween(0, numPoints - 1); @@ -338,7 +313,6 @@ private void testAggregator(SortOrder sortOrder) throws IOException { indexedSortValues.put(groupOrd, sortValues); } - testCase(new MatchAllDocsQuery(), aggregationBuilder, iw -> { for (int group = 0; group < numGroups; group++) { long[] points = indexedPoints.get(group); @@ -346,11 +320,17 @@ private void testAggregator(SortOrder sortOrder) throws IOException { for (int i = 0; i < points.length; i++) { int x = (int) (points[i] >> 32); int y = (int) points[i]; - iw.addDocument(Arrays.asList(new LatLonDocValuesField("value_field", - GeoEncodingUtils.decodeLatitude(y), - GeoEncodingUtils.decodeLongitude(x)), - new SortedNumericDocValuesField("sort_field", NumericUtils.doubleToSortableLong(sortValues[i])), - new SortedDocValuesField("group_id", new BytesRef(String.valueOf(group))))); + iw.addDocument( + Arrays.asList( + new LatLonDocValuesField( + "value_field", + GeoEncodingUtils.decodeLatitude(y), + GeoEncodingUtils.decodeLongitude(x) + ), + new SortedNumericDocValuesField("sort_field", NumericUtils.doubleToSortableLong(sortValues[i])), + new SortedDocValuesField("group_id", new BytesRef(String.valueOf(group))) + ) + ); } } }, terms -> { @@ -368,16 +348,22 @@ private void testAggregator(SortOrder sortOrder) throws IOException { }); } - private void testCase(Query query, TermsAggregationBuilder aggregationBuilder, - CheckedConsumer buildIndex, - Consumer verify) throws IOException { + private void testCase( + Query query, + TermsAggregationBuilder aggregationBuilder, + CheckedConsumer buildIndex, + Consumer verify + ) throws IOException { testCase(query, aggregationBuilder, buildIndex, verify, NumberFieldMapper.NumberType.LONG); } - private void testCase(Query query, TermsAggregationBuilder aggregationBuilder, - CheckedConsumer buildIndex, - Consumer verify, - NumberFieldMapper.NumberType fieldNumberType) throws IOException { + private void testCase( + Query query, + TermsAggregationBuilder aggregationBuilder, + CheckedConsumer buildIndex, + Consumer verify, + NumberFieldMapper.NumberType fieldNumberType + ) throws IOException { Directory directory = newDirectory(); RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory); @@ -391,8 +377,14 @@ private void testCase(Query query, TermsAggregationBuilder aggregationBuilder, MappedFieldType groupFieldType = new KeywordFieldMapper.KeywordFieldType("group_id", false, true, Collections.emptyMap()); MappedFieldType fieldType2 = new NumberFieldMapper.NumberFieldType("sort_field", fieldNumberType); - Terms terms = searchAndReduce(indexSearcher, new MatchAllDocsQuery(), aggregationBuilder, - fieldType, fieldType2, groupFieldType); + Terms terms = searchAndReduce( + indexSearcher, + new MatchAllDocsQuery(), + aggregationBuilder, + fieldType, + fieldType2, + groupFieldType + ); verify.accept(terms); } finally { indexReader.close(); diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/InternalGeoLineTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/InternalGeoLineTests.java index 8c775c17d44e2..da2cef23f5319 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/InternalGeoLineTests.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/InternalGeoLineTests.java @@ -6,14 +6,14 @@ */ package org.elasticsearch.xpack.spatial.search.aggregations; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.util.CollectionUtils; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.plugins.SearchPlugin; import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.ParsedAggregation; import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.test.InternalAggregationTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xpack.spatial.SpatialPlugin; import java.io.IOException; @@ -161,12 +161,12 @@ protected void assertFromXContent(InternalGeoLine aggregation, ParsedAggregation @Override protected List getNamedXContents() { - return CollectionUtils.appendToCopy(super.getNamedXContents(), new NamedXContentRegistry.Entry(Aggregation.class, - new ParseField(GeoLineAggregationBuilder.NAME), - (p, c) -> { - assumeTrue("There is no ParsedGeoLine yet", false); - return null; - } - )); + return CollectionUtils.appendToCopy( + super.getNamedXContents(), + new NamedXContentRegistry.Entry(Aggregation.class, new ParseField(GeoLineAggregationBuilder.NAME), (p, c) -> { + assumeTrue("There is no ParsedGeoLine yet", false); + return null; + }) + ); } } diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/MergedGeoLinesTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/MergedGeoLinesTests.java index e2cf2795c20d1..f3bd21c2c469b 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/MergedGeoLinesTests.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/MergedGeoLinesTests.java @@ -55,8 +55,16 @@ public void testMergeWithEmptyGeoLine() { int maxLength = 10; SortOrder sortOrder = SortOrder.ASC; InternalGeoLine lineWithPoints = randomLine(sortOrder, maxLength, 0.0); - InternalGeoLine emptyLine = new InternalGeoLine("name", new long[]{}, new double[]{}, Collections.emptyMap(), - true, randomBoolean(), sortOrder, maxLength); + InternalGeoLine emptyLine = new InternalGeoLine( + "name", + new long[] {}, + new double[] {}, + Collections.emptyMap(), + true, + randomBoolean(), + sortOrder, + maxLength + ); List geoLines = List.of(lineWithPoints, emptyLine); MergedGeoLines mergedGeoLines = new MergedGeoLines(geoLines, lineWithPoints.length(), sortOrder); mergedGeoLines.merge(); diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/GeoGridTilerTestCase.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/GeoGridTilerTestCase.java index 3e97832f0fcca..0480b3b4f649f 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/GeoGridTilerTestCase.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/GeoGridTilerTestCase.java @@ -112,10 +112,13 @@ public void testGeoGridSetValuesBoundingBoxes_BoundedGeoShapeCellValues() throws } }, () -> boxToGeo(randomBBox()))); - GeoBoundingBox geoBoundingBox = randomValueOtherThanMany(b -> b.right() == -180 && b.left() == 180,() -> randomBBox()); + GeoBoundingBox geoBoundingBox = randomValueOtherThanMany(b -> b.right() == -180 && b.left() == 180, () -> randomBBox()); GeoShapeValues.GeoShapeValue value = geoShapeValue(geometry); - GeoShapeCellValues cellValues = - new GeoShapeCellValues(makeGeoShapeValues(value), getBoundedGridTiler(geoBoundingBox, precision), NOOP_BREAKER); + GeoShapeCellValues cellValues = new GeoShapeCellValues( + makeGeoShapeValues(value), + getBoundedGridTiler(geoBoundingBox, precision), + NOOP_BREAKER + ); assertTrue(cellValues.advanceExact(0)); int numBuckets = cellValues.docValueCount(); @@ -130,8 +133,11 @@ public void testGeoGridSetValuesBoundingBoxes_coversAllLongitudeValues() throws Geometry geometry = new Rectangle(-92, 180, 0.99, -89); GeoBoundingBox geoBoundingBox = new GeoBoundingBox(new GeoPoint(5, 0.6), new GeoPoint(-5, 0.5)); GeoShapeValues.GeoShapeValue value = geoShapeValue(geometry); - GeoShapeCellValues cellValues = - new GeoShapeCellValues(makeGeoShapeValues(value), getBoundedGridTiler(geoBoundingBox, precision), NOOP_BREAKER); + GeoShapeCellValues cellValues = new GeoShapeCellValues( + makeGeoShapeValues(value), + getBoundedGridTiler(geoBoundingBox, precision), + NOOP_BREAKER + ); assertTrue(cellValues.advanceExact(0)); int numBuckets = cellValues.docValueCount(); @@ -141,7 +147,7 @@ public void testGeoGridSetValuesBoundingBoxes_coversAllLongitudeValues() throws public void testGeoGridSetValuesBoundingBoxes_UnboundedGeoShapeCellValues() throws Exception { for (int i = 0; i < 1000; i++) { - int precision = randomIntBetween(0, 3); + int precision = randomIntBetween(0, 3); GeoShapeIndexer indexer = new GeoShapeIndexer(true, "test"); Geometry geometry = indexer.prepareForIndexing(randomValueOtherThanMany(g -> { try { @@ -153,8 +159,11 @@ public void testGeoGridSetValuesBoundingBoxes_UnboundedGeoShapeCellValues() thro }, () -> boxToGeo(randomBBox()))); GeoShapeValues.GeoShapeValue value = geoShapeValue(geometry); - GeoShapeCellValues unboundedCellValues = - new GeoShapeCellValues(makeGeoShapeValues(value), getUnboundedGridTiler(precision), NOOP_BREAKER); + GeoShapeCellValues unboundedCellValues = new GeoShapeCellValues( + makeGeoShapeValues(value), + getUnboundedGridTiler(precision), + NOOP_BREAKER + ); assertTrue(unboundedCellValues.advanceExact(0)); int numTiles = unboundedCellValues.docValueCount(); int expected = expectedBuckets(value, precision, null); @@ -162,7 +171,6 @@ public void testGeoGridSetValuesBoundingBoxes_UnboundedGeoShapeCellValues() thro } } - public void testGeoTileShapeContainsBoundDateLine() throws Exception { Rectangle tile = new Rectangle(178, -178, 2, -2); Rectangle shapeRectangle = new Rectangle(170, -170, 10, -10); @@ -173,8 +181,7 @@ public void testGeoTileShapeContainsBoundDateLine() throws Exception { new GeoPoint(tile.getMinLat(), tile.getMaxLon()) ); - GeoShapeCellValues values = - new GeoShapeCellValues(makeGeoShapeValues(value), getBoundedGridTiler(boundingBox, 4), NOOP_BREAKER); + GeoShapeCellValues values = new GeoShapeCellValues(makeGeoShapeValues(value), getBoundedGridTiler(boundingBox, 4), NOOP_BREAKER); assertTrue(values.advanceExact(0)); int numTiles = values.docValueCount(); int expectedTiles = expectedBuckets(value, 4, boundingBox); @@ -193,9 +200,10 @@ public void testBoundsExcludeTouchingTiles() throws Exception { Math.max(-180, rectangle.getMinX() - 1), Math.min(180, rectangle.getMaxX() + 1), Math.min(90, rectangle.getMaxY() + 1), - Math.max(-90, rectangle.getMinY() - 1)); + Math.max(-90, rectangle.getMinY() - 1) + ); final GeoShapeValues.GeoShapeValue value = geoShapeValue(other); - for (int i = 0; i < 4; i++) { + for (int i = 0; i < 4; i++) { final GeoGridTiler bounded = getBoundedGridTiler(box, precision + i); final GeoShapeCellValues values = new GeoShapeCellValues(makeGeoShapeValues(value), bounded, NOOP_BREAKER); assertTrue(values.advanceExact(0)); @@ -227,9 +235,11 @@ public void testGridCircuitBreaker() throws IOException { maxNumBytes = oldNumBytes + curNumBytes; } - CircuitBreakerService service = new HierarchyCircuitBreakerService(Settings.EMPTY, + CircuitBreakerService service = new HierarchyCircuitBreakerService( + Settings.EMPTY, Collections.singletonList(new BreakerSettings("limited", maxNumBytes - 1, 1.0)), - new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS)); + new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS) + ); CircuitBreaker limitedBreaker = service.getBreaker("limited"); LongConsumer circuitBreakerConsumer = (l) -> limitedBreaker.addEstimateBytesAndMaybeBreak(l, "agg"); @@ -270,19 +280,30 @@ public GeoShapeValue value() { private static Geometry boxToGeo(GeoBoundingBox geoBox) { // turn into polygon if (geoBox.right() < geoBox.left() && geoBox.right() != -180) { - return new MultiPolygon(List.of( - new Polygon(new LinearRing( - new double[] { -180, geoBox.right(), geoBox.right(), -180, -180 }, - new double[] { geoBox.bottom(), geoBox.bottom(), geoBox.top(), geoBox.top(), geoBox.bottom() })), - new Polygon(new LinearRing( - new double[] { geoBox.left(), 180, 180, geoBox.left(), geoBox.left() }, - new double[] { geoBox.bottom(), geoBox.bottom(), geoBox.top(), geoBox.top(), geoBox.bottom() })) - )); + return new MultiPolygon( + List.of( + new Polygon( + new LinearRing( + new double[] { -180, geoBox.right(), geoBox.right(), -180, -180 }, + new double[] { geoBox.bottom(), geoBox.bottom(), geoBox.top(), geoBox.top(), geoBox.bottom() } + ) + ), + new Polygon( + new LinearRing( + new double[] { geoBox.left(), 180, 180, geoBox.left(), geoBox.left() }, + new double[] { geoBox.bottom(), geoBox.bottom(), geoBox.top(), geoBox.top(), geoBox.bottom() } + ) + ) + ) + ); } else { double right = GeoUtils.normalizeLon(geoBox.right()); - return new Polygon(new LinearRing( - new double[] { geoBox.left(), right, right, geoBox.left(), geoBox.left() }, - new double[] { geoBox.bottom(), geoBox.bottom(), geoBox.top(), geoBox.top(), geoBox.bottom() })); + return new Polygon( + new LinearRing( + new double[] { geoBox.left(), right, right, geoBox.left(), geoBox.left() }, + new double[] { geoBox.bottom(), geoBox.bottom(), geoBox.top(), geoBox.top(), geoBox.bottom() } + ) + ); } } } diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/GeoHashTilerTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/GeoHashTilerTests.java index 8c8156a84fd01..c2700e3fddeb1 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/GeoHashTilerTests.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/GeoHashTilerTests.java @@ -27,7 +27,6 @@ protected GeoGridTiler getUnboundedGridTiler(int precision) { return new UnboundedGeoHashGridTiler(precision); } - @Override protected GeoGridTiler getBoundedGridTiler(GeoBoundingBox bbox, int precision) { return new BoundedGeoHashGridTiler(precision, bbox); @@ -43,8 +42,7 @@ protected Rectangle getCell(double lon, double lat, int precision) { if (precision == 0) { return new Rectangle(-180, 180, 90, -90); } - final String hash = - Geohash.stringEncode(lon, lat, precision); + final String hash = Geohash.stringEncode(lon, lat, precision); return Geohash.toBoundingBox(hash); } @@ -92,11 +90,11 @@ protected int expectedBuckets(GeoShapeValues.GeoShapeValue geoValue, int precisi } return 0; } - return computeBuckets("", bbox, geoValue, precision); + return computeBuckets("", bbox, geoValue, precision); } - private int computeBuckets(String hash, GeoBoundingBox bbox, - GeoShapeValues.GeoShapeValue geoValue, int finalPrecision) throws IOException { + private int computeBuckets(String hash, GeoBoundingBox bbox, GeoShapeValues.GeoShapeValue geoValue, int finalPrecision) + throws IOException { int count = 0; String[] hashes = Geohash.getSubGeohashes(hash); for (int i = 0; i < hashes.length; i++) { @@ -106,17 +104,15 @@ private int computeBuckets(String hash, GeoBoundingBox bbox, GeoRelation relation = geoValue.relate(Geohash.toBoundingBox(hashes[i])); if (relation != GeoRelation.QUERY_DISJOINT) { if (hashes[i].length() == finalPrecision) { - count++; + count++; } else { - count += - computeBuckets(hashes[i], bbox, geoValue, finalPrecision); + count += computeBuckets(hashes[i], bbox, geoValue, finalPrecision); } } } return count; } - private boolean hashIntersectsBounds(String hash, GeoBoundingBox bbox) { if (bbox == null) { return true; @@ -141,8 +137,12 @@ public void testGeoHash() throws Exception { Rectangle tile = Geohash.toBoundingBox(Geohash.stringEncode(x, y, 5)); - Rectangle shapeRectangle = new Rectangle(tile.getMinX() + 0.00001, tile.getMaxX() - 0.00001, - tile.getMaxY() - 0.00001, tile.getMinY() + 0.00001); + Rectangle shapeRectangle = new Rectangle( + tile.getMinX() + 0.00001, + tile.getMaxX() - 0.00001, + tile.getMaxY() - 0.00001, + tile.getMinY() + 0.00001 + ); GeoShapeValues.GeoShapeValue value = geoShapeValue(shapeRectangle); // test shape within tile bounds diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/GeoShapeGeoGridTestCase.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/GeoShapeGeoGridTestCase.java index 4c119a6482a21..d448c2a9320c2 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/GeoShapeGeoGridTestCase.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/GeoShapeGeoGridTestCase.java @@ -18,9 +18,9 @@ import org.apache.lucene.search.Query; import org.apache.lucene.store.Directory; import org.apache.lucene.util.BytesRef; -import org.elasticsearch.core.CheckedConsumer; import org.elasticsearch.common.geo.GeoBoundingBox; import org.elasticsearch.common.geo.Orientation; +import org.elasticsearch.core.CheckedConsumer; import org.elasticsearch.geometry.Geometry; import org.elasticsearch.geometry.MultiPoint; import org.elasticsearch.geometry.Point; @@ -110,45 +110,43 @@ protected AggregationBuilder createAggBuilderForTypeTest(MappedFieldType fieldTy public void testNoDocs() throws IOException { testCase(new MatchAllDocsQuery(), FIELD_NAME, randomPrecision(), null, iw -> { // Intentionally not writing any docs - }, geoGrid -> { - assertEquals(0, geoGrid.getBuckets().size()); - }); + }, geoGrid -> { assertEquals(0, geoGrid.getBuckets().size()); }); } public void testUnmapped() throws IOException { - testCase(new MatchAllDocsQuery(), "wrong_field", randomPrecision(), null, iw -> { - iw.addDocument( - Collections.singleton(GeoTestUtils.binaryGeoShapeDocValuesField(FIELD_NAME, new Point(10D, 10D))) - ); - }, geoGrid -> { - assertEquals(0, geoGrid.getBuckets().size()); - }); + testCase( + new MatchAllDocsQuery(), + "wrong_field", + randomPrecision(), + null, + iw -> { iw.addDocument(Collections.singleton(GeoTestUtils.binaryGeoShapeDocValuesField(FIELD_NAME, new Point(10D, 10D)))); }, + geoGrid -> { assertEquals(0, geoGrid.getBuckets().size()); } + ); } - public void testUnmappedMissingGeoShape() throws IOException { // default value type for agg is GEOPOINT, so missing value is parsed as a GEOPOINT - GeoGridAggregationBuilder builder = createBuilder("_name") - .field("wrong_field") - .missing("-34.0,53.4"); - testCase(new MatchAllDocsQuery(), 1, null, - iw -> { - iw.addDocument( - Collections.singleton(GeoTestUtils.binaryGeoShapeDocValuesField(FIELD_NAME, new Point(10D, 10D))) - ); - }, - geoGrid -> assertEquals(1, geoGrid.getBuckets().size()), builder); + GeoGridAggregationBuilder builder = createBuilder("_name").field("wrong_field").missing("-34.0,53.4"); + testCase( + new MatchAllDocsQuery(), + 1, + null, + iw -> { iw.addDocument(Collections.singleton(GeoTestUtils.binaryGeoShapeDocValuesField(FIELD_NAME, new Point(10D, 10D)))); }, + geoGrid -> assertEquals(1, geoGrid.getBuckets().size()), + builder + ); } public void testMappedMissingGeoShape() throws IOException { - GeoGridAggregationBuilder builder = createBuilder("_name") - .field(FIELD_NAME) - .missing("LINESTRING (30 10, 10 30, 40 40)"); - testCase(new MatchAllDocsQuery(), 1, null, - iw -> { - iw.addDocument(Collections.singleton(new SortedSetDocValuesField("string", new BytesRef("a")))); - }, - geoGrid -> assertEquals(1, geoGrid.getBuckets().size()), builder); + GeoGridAggregationBuilder builder = createBuilder("_name").field(FIELD_NAME).missing("LINESTRING (30 10, 10 30, 40 40)"); + testCase( + new MatchAllDocsQuery(), + 1, + null, + iw -> { iw.addDocument(Collections.singleton(new SortedSetDocValuesField("string", new BytesRef("a")))); }, + geoGrid -> assertEquals(1, geoGrid.getBuckets().size()), + builder + ); } public void testGeoShapeBounds() throws IOException { @@ -190,10 +188,11 @@ public void testGeoShapeBounds() throws IOException { Rectangle pointTile = getTile(x, y, precision); GeoShapeValues.GeoShapeValue value = geoShapeValue(p); - GeoRelation tileRelation = value.relate(pointTile); - boolean intersectsBounds = boundsTop > pointTile.getMinY() && boundsBottom < pointTile.getMaxY() + GeoRelation tileRelation = value.relate(pointTile); + boolean intersectsBounds = boundsTop > pointTile.getMinY() + && boundsBottom < pointTile.getMaxY() && (boundsEastLeft < pointTile.getMaxX() && boundsEastRight > pointTile.getMinX() - || (crossesDateline && boundsWestLeft < pointTile.getMaxX() && boundsWestRight > pointTile.getMinX())); + || (crossesDateline && boundsWestLeft < pointTile.getMaxX() && boundsWestRight > pointTile.getMinX())); if (tileRelation != GeoRelation.QUERY_DISJOINT && intersectsBounds) { numDocsWithin += 1; } @@ -204,18 +203,17 @@ public void testGeoShapeBounds() throws IOException { final long numDocsInBucket = numDocsWithin; testCase(new MatchAllDocsQuery(), FIELD_NAME, precision, bbox, iw -> { - for (BinaryGeoShapeDocValuesField docField : docs) { - iw.addDocument(Collections.singletonList(docField)); - } - }, - geoGrid -> { - assertThat(AggregationInspectionHelper.hasValue(geoGrid), equalTo(numDocsInBucket > 0)); - long docCount = 0; - for (int i = 0; i < geoGrid.getBuckets().size(); i++) { - docCount += geoGrid.getBuckets().get(i).getDocCount(); - } - assertThat(docCount, equalTo(numDocsInBucket)); - }); + for (BinaryGeoShapeDocValuesField docField : docs) { + iw.addDocument(Collections.singletonList(docField)); + } + }, geoGrid -> { + assertThat(AggregationInspectionHelper.hasValue(geoGrid), equalTo(numDocsInBucket > 0)); + long docCount = 0; + for (int i = 0; i < geoGrid.getBuckets().size(); i++) { + docCount += geoGrid.getBuckets().get(i).getDocCount(); + } + assertThat(docCount, equalTo(numDocsInBucket)); + }); } public void testGeoShapeWithSeveralDocs() throws IOException { @@ -267,17 +265,26 @@ public void testGeoShapeWithSeveralDocs() throws IOException { }); } - private void testCase(Query query, String field, int precision, GeoBoundingBox geoBoundingBox, - CheckedConsumer buildIndex, - Consumer> verify) throws IOException { + private void testCase( + Query query, + String field, + int precision, + GeoBoundingBox geoBoundingBox, + CheckedConsumer buildIndex, + Consumer> verify + ) throws IOException { testCase(query, precision, geoBoundingBox, buildIndex, verify, createBuilder("_name").field(field)); } @SuppressWarnings("unchecked") - private void testCase(Query query, int precision, GeoBoundingBox geoBoundingBox, - CheckedConsumer buildIndex, - Consumer> verify, - GeoGridAggregationBuilder aggregationBuilder) throws IOException { + private void testCase( + Query query, + int precision, + GeoBoundingBox geoBoundingBox, + CheckedConsumer buildIndex, + Consumer> verify, + GeoGridAggregationBuilder aggregationBuilder + ) throws IOException { Directory directory = newDirectory(); RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory); buildIndex.accept(indexWriter); @@ -292,8 +299,15 @@ private void testCase(Query query, int precision, GeoBoundingBox geoBoundingBox, assertThat(aggregationBuilder.geoBoundingBox(), equalTo(geoBoundingBox)); } - MappedFieldType fieldType - = new GeoShapeWithDocValuesFieldType(FIELD_NAME, true, true, Orientation.RIGHT, null, null, Collections.emptyMap()); + MappedFieldType fieldType = new GeoShapeWithDocValuesFieldType( + FIELD_NAME, + true, + true, + Orientation.RIGHT, + null, + null, + Collections.emptyMap() + ); Aggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType); aggregator.preCollection(); diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/GeoShapeGeoTileGridAggregatorTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/GeoShapeGeoTileGridAggregatorTests.java index c84adb2ea1147..ae0ce5ba4cff6 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/GeoShapeGeoTileGridAggregatorTests.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/GeoShapeGeoTileGridAggregatorTests.java @@ -31,15 +31,18 @@ protected String hashAsString(double lng, double lat, int precision) { @Override protected Point randomPoint() { - return new Point(randomDoubleBetween(GeoUtils.MIN_LON, GeoUtils.MAX_LON, true), - randomDoubleBetween(-GeoTileUtils.LATITUDE_MASK, GeoTileUtils.LATITUDE_MASK, false)); + return new Point( + randomDoubleBetween(GeoUtils.MIN_LON, GeoUtils.MAX_LON, true), + randomDoubleBetween(-GeoTileUtils.LATITUDE_MASK, GeoTileUtils.LATITUDE_MASK, false) + ); } @Override protected GeoBoundingBox randomBBox() { - GeoBoundingBox bbox = randomValueOtherThanMany( + GeoBoundingBox bbox = randomValueOtherThanMany( (b) -> b.top() > GeoTileUtils.LATITUDE_MASK || b.bottom() < -GeoTileUtils.LATITUDE_MASK, - GeoTestUtils::randomBBox); + GeoTestUtils::randomBBox + ); // Avoid numerical errors for sub-atomic values double left = GeoTestUtils.encodeDecodeLon(bbox.left()); double right = GeoTestUtils.encodeDecodeLon(bbox.right()); diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/GeoTileTilerTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/GeoTileTilerTests.java index d7ed4209bba16..e05d786f48ae7 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/GeoTileTilerTests.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/GeoTileTilerTests.java @@ -42,7 +42,7 @@ protected GeoGridTiler getBoundedGridTiler(GeoBoundingBox bbox, int precision) { @Override protected Rectangle getCell(double lon, double lat, int precision) { - return GeoTileUtils.toBoundingBox(GeoTileUtils.longEncode(lon, lat, precision)); + return GeoTileUtils.toBoundingBox(GeoTileUtils.longEncode(lon, lat, precision)); } @Override @@ -52,7 +52,7 @@ protected int maxPrecision() { @Override protected long getCellsForDiffPrecision(int precisionDiff) { - return (1L << precisionDiff) * (1L << precisionDiff); + return (1L << precisionDiff) * (1L << precisionDiff); } @Override @@ -91,15 +91,15 @@ protected int expectedBuckets(GeoShapeValues.GeoShapeValue geoValue, int precisi GeoShapeValues.BoundingBox bounds = geoValue.boundingBox(); int count = 0; - if (bounds.bottom > GeoTileUtils.NORMALIZED_LATITUDE_MASK || bounds.top < GeoTileUtils.NORMALIZED_NEGATIVE_LATITUDE_MASK) { + if (bounds.bottom > GeoTileUtils.NORMALIZED_LATITUDE_MASK || bounds.top < GeoTileUtils.NORMALIZED_NEGATIVE_LATITUDE_MASK) { return 0; } - if (bbox != null) { - if (bbox.bottom() > GeoTileUtils.NORMALIZED_LATITUDE_MASK || bbox.top() < GeoTileUtils.NORMALIZED_NEGATIVE_LATITUDE_MASK) { - return 0; - } - } + if (bbox != null) { + if (bbox.bottom() > GeoTileUtils.NORMALIZED_LATITUDE_MASK || bbox.top() < GeoTileUtils.NORMALIZED_NEGATIVE_LATITUDE_MASK) { + return 0; + } + } if (precision == 0) { return 1; @@ -108,7 +108,7 @@ protected int expectedBuckets(GeoShapeValues.GeoShapeValue geoValue, int precisi final double tiles = 1 << precision; int minYTile = GeoTileUtils.getYTile(bounds.maxY(), (long) tiles); int maxYTile = GeoTileUtils.getYTile(bounds.minY(), (long) tiles); - if ((bounds.posLeft >= 0 && bounds.posRight >= 0) && (bounds.negLeft < 0 && bounds.negRight < 0)) { + if ((bounds.posLeft >= 0 && bounds.posRight >= 0) && (bounds.negLeft < 0 && bounds.negRight < 0)) { // box one int minXTileNeg = GeoTileUtils.getXTile(bounds.negLeft, (long) tiles); int maxXTileNeg = GeoTileUtils.getXTile(bounds.negRight, (long) tiles); @@ -201,8 +201,12 @@ public void testGeoTile() throws Exception { // create rectangle within tile and check bound counts Rectangle tile = GeoTileUtils.toBoundingBox(1309, 3166, 13); - Rectangle shapeRectangle = new Rectangle(tile.getMinX() + 0.00001, tile.getMaxX() - 0.00001, - tile.getMaxY() - 0.00001, tile.getMinY() + 0.00001); + Rectangle shapeRectangle = new Rectangle( + tile.getMinX() + 0.00001, + tile.getMaxX() - 0.00001, + tile.getMaxY() - 0.00001, + tile.getMinY() + 0.00001 + ); GeoShapeValues.GeoShapeValue value = geoShapeValue(shapeRectangle); // test shape within tile bounds { @@ -238,16 +242,16 @@ public void testMaxCellsBoundedWithAnotherCell() { } public void testBoundGridOutOfRange() throws Exception { - GeoBoundingBox boundingBox = new GeoBoundingBox( - new GeoPoint(90, -180), - new GeoPoint(89, 180) - ); + GeoBoundingBox boundingBox = new GeoBoundingBox(new GeoPoint(90, -180), new GeoPoint(89, 180)); double lon = GeoTestUtil.nextLongitude(); double lat = GeoTestUtil.nextLatitude(); GeoShapeValues.GeoShapeValue value = geoShapeValue(new Point(lon, lat)); for (int i = 0; i < maxPrecision(); i++) { - GeoShapeCellValues values = - new GeoShapeCellValues(makeGeoShapeValues(value), getBoundedGridTiler(boundingBox, i), NOOP_BREAKER); + GeoShapeCellValues values = new GeoShapeCellValues( + makeGeoShapeValues(value), + getBoundedGridTiler(boundingBox, i), + NOOP_BREAKER + ); assertTrue(values.advanceExact(0)); int numTiles = values.docValueCount(); assertThat(numTiles, equalTo(0)); @@ -271,16 +275,18 @@ public void testTilerMatchPoint() throws Exception { new Point(bbox.getMinX(), (bbox.getMinY() + bbox.getMaxY()) / 2), new Point(bbox.getMaxX(), (bbox.getMinY() + bbox.getMaxY()) / 2), new Point((bbox.getMinX() + bbox.getMaxX()) / 2, bbox.getMinY()), - new Point((bbox.getMinX() + bbox.getMaxX()) / 2, bbox.getMaxY()), - }; + new Point((bbox.getMinX() + bbox.getMaxX()) / 2, bbox.getMaxY()), }; for (Point point : pointCorners) { if (point.getX() == GeoUtils.MAX_LON || point.getY() == -LATITUDE_MASK) { continue; } GeoShapeValues.GeoShapeValue value = geoShapeValue(point); - GeoShapeCellValues unboundedCellValues = - new GeoShapeCellValues(makeGeoShapeValues(value), new UnboundedGeoTileGridTiler(precision), NOOP_BREAKER); + GeoShapeCellValues unboundedCellValues = new GeoShapeCellValues( + makeGeoShapeValues(value), + new UnboundedGeoTileGridTiler(precision), + NOOP_BREAKER + ); assertTrue(unboundedCellValues.advanceExact(0)); int numTiles = unboundedCellValues.docValueCount(); assertThat(numTiles, equalTo(1)); diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/metrics/GeoShapeBoundsAggregatorTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/metrics/GeoShapeBoundsAggregatorTests.java index 4934c302e833c..afa37276b9b8b 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/metrics/GeoShapeBoundsAggregatorTests.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/metrics/GeoShapeBoundsAggregatorTests.java @@ -53,12 +53,17 @@ protected List getSearchPlugins() { public void testEmpty() throws Exception { try (Directory dir = newDirectory(); RandomIndexWriter w = new RandomIndexWriter(random(), dir)) { - GeoBoundsAggregationBuilder aggBuilder = new GeoBoundsAggregationBuilder("my_agg") - .field("field") - .wrapLongitude(false); - - MappedFieldType fieldType - = new GeoShapeWithDocValuesFieldType("field", true, true, Orientation.RIGHT, null, null, Collections.emptyMap()); + GeoBoundsAggregationBuilder aggBuilder = new GeoBoundsAggregationBuilder("my_agg").field("field").wrapLongitude(false); + + MappedFieldType fieldType = new GeoShapeWithDocValuesFieldType( + "field", + true, + true, + Orientation.RIGHT, + null, + null, + Collections.emptyMap() + ); try (IndexReader reader = w.getReader()) { IndexSearcher searcher = new IndexSearcher(reader); InternalGeoBounds bounds = searchAndReduce(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType); @@ -81,12 +86,17 @@ public void testUnmappedFieldWithDocs() throws Exception { w.addDocument(doc); } - GeoBoundsAggregationBuilder aggBuilder = new GeoBoundsAggregationBuilder("my_agg") - .field("non_existent") - .wrapLongitude(false); - - MappedFieldType fieldType - = new GeoShapeWithDocValuesFieldType("field", true, true, Orientation.RIGHT, null, null, Collections.emptyMap()); + GeoBoundsAggregationBuilder aggBuilder = new GeoBoundsAggregationBuilder("my_agg").field("non_existent").wrapLongitude(false); + + MappedFieldType fieldType = new GeoShapeWithDocValuesFieldType( + "field", + true, + true, + Orientation.RIGHT, + null, + null, + Collections.emptyMap() + ); try (IndexReader reader = w.getReader()) { IndexSearcher searcher = new IndexSearcher(reader); InternalGeoBounds bounds = searchAndReduce(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType); @@ -107,16 +117,22 @@ public void testMissing() throws Exception { doc.add(new NumericDocValuesField("not_field", 1000L)); w.addDocument(doc); - MappedFieldType fieldType - = new GeoShapeWithDocValuesFieldType("field", true, true, Orientation.RIGHT, null, null, Collections.emptyMap()); + MappedFieldType fieldType = new GeoShapeWithDocValuesFieldType( + "field", + true, + true, + Orientation.RIGHT, + null, + null, + Collections.emptyMap() + ); Point point = GeometryTestUtils.randomPoint(false); double lon = GeoEncodingUtils.decodeLongitude(GeoEncodingUtils.encodeLongitude(point.getX())); double lat = GeoEncodingUtils.decodeLatitude(GeoEncodingUtils.encodeLatitude(point.getY())); Object missingVal = "POINT(" + lon + " " + lat + ")"; - GeoBoundsAggregationBuilder aggBuilder = new GeoBoundsAggregationBuilder("my_agg") - .field("field") + GeoBoundsAggregationBuilder aggBuilder = new GeoBoundsAggregationBuilder("my_agg").field("field") .missing(missingVal) .wrapLongitude(false); @@ -139,17 +155,25 @@ public void testInvalidMissing() throws Exception { doc.add(new NumericDocValuesField("not_field", 1000L)); w.addDocument(doc); - MappedFieldType fieldType - = new GeoShapeWithDocValuesFieldType("field", true, true, Orientation.RIGHT, null, null, Collections.emptyMap()); - - GeoBoundsAggregationBuilder aggBuilder = new GeoBoundsAggregationBuilder("my_agg") - .field("field") + MappedFieldType fieldType = new GeoShapeWithDocValuesFieldType( + "field", + true, + true, + Orientation.RIGHT, + null, + null, + Collections.emptyMap() + ); + + GeoBoundsAggregationBuilder aggBuilder = new GeoBoundsAggregationBuilder("my_agg").field("field") .missing("invalid") .wrapLongitude(false); try (IndexReader reader = w.getReader()) { IndexSearcher searcher = new IndexSearcher(reader); - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, - () -> searchAndReduce(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType)); + IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> searchAndReduce(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType) + ); assertThat(exception.getMessage(), startsWith("Unknown geometry type")); } } @@ -163,8 +187,7 @@ public void testRandomShapes() throws Exception { double negLeft = Double.POSITIVE_INFINITY; double negRight = Double.NEGATIVE_INFINITY; int numDocs = randomIntBetween(50, 100); - try (Directory dir = newDirectory(); - RandomIndexWriter w = new RandomIndexWriter(random(), dir)) { + try (Directory dir = newDirectory(); RandomIndexWriter w = new RandomIndexWriter(random(), dir)) { for (int i = 0; i < numDocs; i++) { Document doc = new Document(); int numValues = randomIntBetween(1, 5); @@ -195,12 +218,17 @@ public void testRandomShapes() throws Exception { doc.add(GeoTestUtils.binaryGeoShapeDocValuesField("field", geometry)); w.addDocument(doc); } - GeoBoundsAggregationBuilder aggBuilder = new GeoBoundsAggregationBuilder("my_agg") - .field("field") - .wrapLongitude(false); - - MappedFieldType fieldType - = new GeoShapeWithDocValuesFieldType("field", true, true, Orientation.RIGHT, null, null, Collections.emptyMap()); + GeoBoundsAggregationBuilder aggBuilder = new GeoBoundsAggregationBuilder("my_agg").field("field").wrapLongitude(false); + + MappedFieldType fieldType = new GeoShapeWithDocValuesFieldType( + "field", + true, + true, + Orientation.RIGHT, + null, + null, + Collections.emptyMap() + ); try (IndexReader reader = w.getReader()) { IndexSearcher searcher = new IndexSearcher(reader); InternalGeoBounds bounds = searchAndReduce(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType); diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/metrics/GeoShapeCentroidAggregatorTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/metrics/GeoShapeCentroidAggregatorTests.java index ab7a22445fe9e..c89ffd7d8d055 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/metrics/GeoShapeCentroidAggregatorTests.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/metrics/GeoShapeCentroidAggregatorTests.java @@ -57,13 +57,18 @@ protected List getSearchPlugins() { } public void testEmpty() throws Exception { - try (Directory dir = newDirectory(); - RandomIndexWriter w = new RandomIndexWriter(random(), dir)) { - GeoCentroidAggregationBuilder aggBuilder = new GeoCentroidAggregationBuilder("my_agg") - .field("field"); - - MappedFieldType fieldType - = new GeoShapeWithDocValuesFieldType("field", true, true, Orientation.RIGHT, null, null, Collections.emptyMap()); + try (Directory dir = newDirectory(); RandomIndexWriter w = new RandomIndexWriter(random(), dir)) { + GeoCentroidAggregationBuilder aggBuilder = new GeoCentroidAggregationBuilder("my_agg").field("field"); + + MappedFieldType fieldType = new GeoShapeWithDocValuesFieldType( + "field", + true, + true, + Orientation.RIGHT, + null, + null, + Collections.emptyMap() + ); try (IndexReader reader = w.getReader()) { IndexSearcher searcher = new IndexSearcher(reader); InternalGeoCentroid result = searchAndReduce(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType); @@ -74,10 +79,8 @@ public void testEmpty() throws Exception { } public void testUnmapped() throws Exception { - try (Directory dir = newDirectory(); - RandomIndexWriter w = new RandomIndexWriter(random(), dir)) { - GeoCentroidAggregationBuilder aggBuilder = new GeoCentroidAggregationBuilder("my_agg") - .field("another_field"); + try (Directory dir = newDirectory(); RandomIndexWriter w = new RandomIndexWriter(random(), dir)) { + GeoCentroidAggregationBuilder aggBuilder = new GeoCentroidAggregationBuilder("my_agg").field("another_field"); Document document = new Document(); document.add(new LatLonDocValuesField("field", 10, 10)); @@ -85,13 +88,19 @@ public void testUnmapped() throws Exception { try (IndexReader reader = w.getReader()) { IndexSearcher searcher = new IndexSearcher(reader); - MappedFieldType fieldType = new GeoShapeWithDocValuesFieldType("another_field", - true, true, Orientation.RIGHT, null, null, Collections.emptyMap()); + MappedFieldType fieldType = new GeoShapeWithDocValuesFieldType( + "another_field", + true, + true, + Orientation.RIGHT, + null, + null, + Collections.emptyMap() + ); InternalGeoCentroid result = searchAndReduce(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType); assertNull(result.centroid()); - fieldType = new GeoShapeWithDocValuesFieldType("field", - true, true, Orientation.RIGHT, null, null, Collections.emptyMap()); + fieldType = new GeoShapeWithDocValuesFieldType("field", true, true, Orientation.RIGHT, null, null, Collections.emptyMap()); result = searchAndReduce(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType); assertNull(result.centroid()); assertFalse(AggregationInspectionHelper.hasValue(result)); @@ -100,10 +109,8 @@ public void testUnmapped() throws Exception { } public void testUnmappedWithMissing() throws Exception { - try (Directory dir = newDirectory(); - RandomIndexWriter w = new RandomIndexWriter(random(), dir)) { - GeoCentroidAggregationBuilder aggBuilder = new GeoCentroidAggregationBuilder("my_agg") - .field("another_field") + try (Directory dir = newDirectory(); RandomIndexWriter w = new RandomIndexWriter(random(), dir)) { + GeoCentroidAggregationBuilder aggBuilder = new GeoCentroidAggregationBuilder("my_agg").field("another_field") .missing("POINT(6.475031 53.69437)"); double normalizedLat = GeoEncodingUtils.decodeLatitude(GeoEncodingUtils.encodeLatitude(53.69437)); @@ -115,8 +122,15 @@ public void testUnmappedWithMissing() throws Exception { try (IndexReader reader = w.getReader()) { IndexSearcher searcher = new IndexSearcher(reader); - MappedFieldType fieldType = new GeoShapeWithDocValuesFieldType("another_field", - true, true, Orientation.RIGHT, null, null, Collections.emptyMap()); + MappedFieldType fieldType = new GeoShapeWithDocValuesFieldType( + "another_field", + true, + true, + Orientation.RIGHT, + null, + null, + Collections.emptyMap() + ); InternalGeoCentroid result = searchAndReduce(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType); assertThat(result.centroid(), equalTo(expectedCentroid)); assertTrue(AggregationInspectionHelper.hasValue(result)); @@ -151,8 +165,7 @@ public void testSingleValuedField() throws Exception { DimensionalShapeType geometryShapeType = centroidCalculator.getDimensionalShapeType(); targetShapeType = targetShapeType.compareTo(geometryShapeType) >= 0 ? targetShapeType : geometryShapeType; } - try (Directory dir = newDirectory(); - RandomIndexWriter w = new RandomIndexWriter(random(), dir)) { + try (Directory dir = newDirectory(); RandomIndexWriter w = new RandomIndexWriter(random(), dir)) { CompensatedSum compensatedSumLon = new CompensatedSum(0, 0); CompensatedSum compensatedSumLat = new CompensatedSum(0, 0); CompensatedSum compensatedSumWeight = new CompensatedSum(0, 0); @@ -171,17 +184,25 @@ public void testSingleValuedField() throws Exception { } // force using a single aggregator to compute the centroid w.forceMerge(1); - GeoPoint expectedCentroid = new GeoPoint(compensatedSumLat.value() / compensatedSumWeight.value(), - compensatedSumLon.value() / compensatedSumWeight.value()); + GeoPoint expectedCentroid = new GeoPoint( + compensatedSumLat.value() / compensatedSumWeight.value(), + compensatedSumLon.value() / compensatedSumWeight.value() + ); assertCentroid(w, expectedCentroid); } } private void assertCentroid(RandomIndexWriter w, GeoPoint expectedCentroid) throws IOException { - MappedFieldType fieldType = new GeoShapeWithDocValuesFieldType("field", - true, true, Orientation.RIGHT, null, null, Collections.emptyMap()); - GeoCentroidAggregationBuilder aggBuilder = new GeoCentroidAggregationBuilder("my_agg") - .field("field"); + MappedFieldType fieldType = new GeoShapeWithDocValuesFieldType( + "field", + true, + true, + Orientation.RIGHT, + null, + null, + Collections.emptyMap() + ); + GeoCentroidAggregationBuilder aggBuilder = new GeoCentroidAggregationBuilder("my_agg").field("field"); try (IndexReader reader = w.getReader()) { IndexSearcher searcher = new IndexSearcher(reader); InternalGeoCentroid result = searchAndReduce(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType); diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/util/GeoTestUtils.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/util/GeoTestUtils.java index f89303658f2ec..ab9c4818bd3fb 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/util/GeoTestUtils.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/util/GeoTestUtils.java @@ -14,18 +14,18 @@ import org.elasticsearch.common.geo.GeoJson; import org.elasticsearch.common.geo.GeoPoint; import org.elasticsearch.common.geo.GeometryParser; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.geo.GeometryTestUtils; +import org.elasticsearch.geometry.Geometry; +import org.elasticsearch.geometry.Rectangle; +import org.elasticsearch.index.mapper.GeoShapeIndexer; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.geo.GeometryTestUtils; -import org.elasticsearch.geometry.Geometry; -import org.elasticsearch.geometry.Rectangle; -import org.elasticsearch.index.mapper.GeoShapeIndexer; import org.elasticsearch.xpack.spatial.index.fielddata.CentroidCalculator; import org.elasticsearch.xpack.spatial.index.fielddata.CoordinateEncoder; import org.elasticsearch.xpack.spatial.index.fielddata.GeoShapeValues; @@ -51,7 +51,7 @@ public static BinaryGeoShapeDocValuesField binaryGeoShapeDocValuesField(String n GeoShapeIndexer indexer = new GeoShapeIndexer(true, name); geometry = indexer.prepareForIndexing(geometry); BinaryGeoShapeDocValuesField field = new BinaryGeoShapeDocValuesField(name); - field.add(indexer.indexShape(geometry) , geometry); + field.add(indexer.indexShape(geometry), geometry); return field; } @@ -63,8 +63,10 @@ public static GeoShapeValues.GeoShapeValue geoShapeValue(Geometry geometry) thro public static GeoBoundingBox randomBBox() { Rectangle rectangle = GeometryTestUtils.randomRectangle(); - return new GeoBoundingBox(new GeoPoint(rectangle.getMaxLat(), rectangle.getMinLon()), - new GeoPoint(rectangle.getMinLat(), rectangle.getMaxLon())); + return new GeoBoundingBox( + new GeoPoint(rectangle.getMaxLat(), rectangle.getMinLon()), + new GeoPoint(rectangle.getMinLat(), rectangle.getMaxLon()) + ); } public static double encodeDecodeLat(double lat) { @@ -82,8 +84,12 @@ public static String toGeoJsonString(Geometry geometry) throws IOException { } public static Geometry fromGeoJsonString(String geoJson) throws Exception { - XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, - new BytesArray(geoJson), XContentType.JSON); + XContentParser parser = XContentHelper.createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + new BytesArray(geoJson), + XContentType.JSON + ); parser.nextToken(); Geometry geometry = new GeometryParser(true, true, true).parse(parser); return new GeoShapeIndexer(true, "indexer").prepareForIndexing(geometry); diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/util/ShapeTestUtils.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/util/ShapeTestUtils.java index 4b4a2e6d50fbb..953f937b86c84 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/util/ShapeTestUtils.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/util/ShapeTestUtils.java @@ -75,8 +75,10 @@ public static Polygon randomPolygon(boolean hasAlt) { XYPolygon poly = luceneHoles[i]; holes.add(linearRing(floatsToDoubles(poly.getPolyX()), floatsToDoubles(poly.getPolyY()), hasAlt)); } - return new Polygon(linearRing(floatsToDoubles(lucenePolygon.getPolyX()), floatsToDoubles(lucenePolygon.getPolyY()), hasAlt), - holes); + return new Polygon( + linearRing(floatsToDoubles(lucenePolygon.getPolyX()), floatsToDoubles(lucenePolygon.getPolyY()), hasAlt), + holes + ); } return new Polygon(linearRing(floatsToDoubles(lucenePolygon.getPolyX()), floatsToDoubles(lucenePolygon.getPolyY()), hasAlt)); } @@ -84,11 +86,10 @@ public static Polygon randomPolygon(boolean hasAlt) { static double[] floatsToDoubles(float[] f) { double[] d = new double[f.length]; for (int i = 0; i < f.length; i++) { - d[i] = f[i]; + d[i] = f[i]; } return d; - } - + } public static Rectangle randomRectangle() { org.apache.lucene.geo.XYRectangle rectangle = XShapeTestUtil.nextBox(); @@ -140,7 +141,8 @@ public static Geometry randomGeometry(boolean hasAlt) { } protected static Geometry randomGeometry(int level, boolean hasAlt) { - @SuppressWarnings("unchecked") Function geometry = ESTestCase.randomFrom( + @SuppressWarnings("unchecked") + Function geometry = ESTestCase.randomFrom( ShapeTestUtils::randomLine, ShapeTestUtils::randomPoint, ShapeTestUtils::randomPolygon, diff --git a/x-pack/plugin/spatial/src/yamlRestTest/java/org/elasticsearch/xpack/spatial/SpatialClientYamlTestSuiteIT.java b/x-pack/plugin/spatial/src/yamlRestTest/java/org/elasticsearch/xpack/spatial/SpatialClientYamlTestSuiteIT.java index 2e9f70c0f7bd4..c386f25af8acc 100644 --- a/x-pack/plugin/spatial/src/yamlRestTest/java/org/elasticsearch/xpack/spatial/SpatialClientYamlTestSuiteIT.java +++ b/x-pack/plugin/spatial/src/yamlRestTest/java/org/elasticsearch/xpack/spatial/SpatialClientYamlTestSuiteIT.java @@ -8,6 +8,7 @@ import com.carrotsearch.randomizedtesting.annotations.Name; import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; + import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate; import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase; diff --git a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/Debug.java b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/Debug.java index c79b3db9f3f7c..18f7d67b6c700 100644 --- a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/Debug.java +++ b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/Debug.java @@ -8,7 +8,6 @@ import org.elasticsearch.xpack.sql.client.SuppressForbidden; -import javax.sql.DataSource; import java.io.OutputStreamWriter; import java.io.PrintStream; import java.io.PrintWriter; @@ -29,6 +28,8 @@ import java.util.HashMap; import java.util.Map; +import javax.sql.DataSource; + /** * Class handling debug logging. Typically disabled (hence why it's called debug). * JDBC carries a lot of legacy conventions, logging being one of them - in JDBC logging was expected to @@ -93,8 +94,7 @@ static Statement proxy(Object statement, StatementProxy handler) { if (statement instanceof CallableStatement) { i = CallableStatement.class; - } - else if (statement instanceof PreparedStatement) { + } else if (statement instanceof PreparedStatement) { i = PreparedStatement.class; } @@ -201,8 +201,7 @@ static void release(JdbcConfiguration info) { d.print.close(); } } - } - else { + } else { OUTPUT_REFS.put(out, Integer.valueOf(r - 1)); } } diff --git a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/DebugLog.java b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/DebugLog.java index f801d57d2478e..0d85dc865259b 100644 --- a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/DebugLog.java +++ b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/DebugLog.java @@ -27,13 +27,18 @@ final class DebugLog { void logMethod(Method m, Object[] args) { long time = System.currentTimeMillis(); - print.printf(Locale.ROOT, HEADER + "Invoke %s#%s(%s)%n", - time, time, time, - //m.getReturnType().getSimpleName(), - m.getDeclaringClass().getSimpleName(), - m.getName(), - //array(m.getParameterTypes()), - array(args)); + print.printf( + Locale.ROOT, + HEADER + "Invoke %s#%s(%s)%n", + time, + time, + time, + // m.getReturnType().getSimpleName(), + m.getDeclaringClass().getSimpleName(), + m.getName(), + // array(m.getParameterTypes()), + array(args) + ); if (flushAlways) { print.flush(); } @@ -41,14 +46,19 @@ void logMethod(Method m, Object[] args) { void logResult(Method m, Object[] args, Object r) { long time = System.currentTimeMillis(); - print.printf(Locale.ROOT, HEADER + "%s#%s(%s) returned %s%n", - time, time, time, - //m.getReturnType().getSimpleName(), - m.getDeclaringClass().getSimpleName(), - m.getName(), - //array(m.getParameterTypes()), - array(args), - r); + print.printf( + Locale.ROOT, + HEADER + "%s#%s(%s) returned %s%n", + time, + time, + time, + // m.getReturnType().getSimpleName(), + m.getDeclaringClass().getSimpleName(), + m.getName(), + // array(m.getParameterTypes()), + array(args), + r + ); if (flushAlways) { print.flush(); } @@ -56,31 +66,39 @@ void logResult(Method m, Object[] args, Object r) { void logException(Method m, Object[] args, Throwable t) { long time = System.currentTimeMillis(); - print.printf(Locale.ROOT, HEADER + "%s#%s(%s) threw ", - time, time, time, - m.getDeclaringClass().getSimpleName(), - m.getName(), - array(args)); + print.printf( + Locale.ROOT, + HEADER + "%s#%s(%s) threw ", + time, + time, + time, + m.getDeclaringClass().getSimpleName(), + m.getName(), + array(args) + ); t.printStackTrace(print); print.flush(); } void logSystemInfo() { long time = System.currentTimeMillis(); - print.printf(Locale.ROOT, HEADER + "OS[%s/%s/%s], JVM[%s/%s/%s/%s]", - time, time, time, - System.getProperty("os.name"), - System.getProperty("os.version"), - System.getProperty("os.arch"), - System.getProperty("java.vm.vendor"), - System.getProperty("java.vm.name"), - System.getProperty("java.version"), - System.getProperty("java.vm.version")); + print.printf( + Locale.ROOT, + HEADER + "OS[%s/%s/%s], JVM[%s/%s/%s/%s]", + time, + time, + time, + System.getProperty("os.name"), + System.getProperty("os.version"), + System.getProperty("os.arch"), + System.getProperty("java.vm.vendor"), + System.getProperty("java.vm.name"), + System.getProperty("java.version"), + System.getProperty("java.vm.version") + ); print.println(); time = System.currentTimeMillis(); - print.printf(Locale.ROOT, HEADER + "JVM default timezone: %s", - time, time, time, - java.util.TimeZone.getDefault().toString()); + print.printf(Locale.ROOT, HEADER + "JVM default timezone: %s", time, time, time, java.util.TimeZone.getDefault().toString()); print.println(); print.flush(); } @@ -95,7 +113,7 @@ private static String array(Object[] a) { StringBuilder b = new StringBuilder(); int iMax = a.length - 1; - for (int i = 0; ; i++) { + for (int i = 0;; i++) { b.append(handleArray(a[i])); if (i == iMax) { return b.toString(); @@ -110,8 +128,7 @@ private static String handleArray(Object o) { int l = Array.getLength(o); int iMax = l - 1; - if (iMax == -1) - return "[]"; + if (iMax == -1) return "[]"; b.append('['); for (int i = 0; i < l; i++) { diff --git a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/DefaultCursor.java b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/DefaultCursor.java index 03407607ccb8a..066a763272bbe 100644 --- a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/DefaultCursor.java +++ b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/DefaultCursor.java @@ -39,8 +39,7 @@ public boolean next() throws SQLException { if (row < rows.size() - 1) { row++; return true; - } - else { + } else { if (cursor.isEmpty() == false) { Tuple>> nextPage = client.nextPage(cursor, meta); cursor = nextPage.v1(); diff --git a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/EsDataSource.java b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/EsDataSource.java index 7dcd37f7f17d0..ad8c39b6345ba 100644 --- a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/EsDataSource.java +++ b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/EsDataSource.java @@ -9,7 +9,6 @@ import org.elasticsearch.xpack.sql.client.ClientVersion; import org.elasticsearch.xpack.sql.client.ConnectionConfiguration; -import javax.sql.DataSource; import java.io.PrintWriter; import java.sql.Connection; import java.sql.SQLException; @@ -18,6 +17,8 @@ import java.util.Properties; import java.util.logging.Logger; +import javax.sql.DataSource; + /** * Factory for connections to Elasticsearch SQL. */ diff --git a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/EsType.java b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/EsType.java index 02e97f9c9606c..5b4a8afd2a40c 100644 --- a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/EsType.java +++ b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/EsType.java @@ -7,48 +7,47 @@ package org.elasticsearch.xpack.sql.jdbc; - import java.sql.SQLType; import java.sql.Types; public enum EsType implements SQLType { - NULL( Types.NULL), - UNSUPPORTED( Types.OTHER), - BOOLEAN( Types.BOOLEAN), - BYTE( Types.TINYINT), - SHORT( Types.SMALLINT), - INTEGER( Types.INTEGER), - LONG( Types.BIGINT), - DOUBLE( Types.DOUBLE), - FLOAT( Types.REAL), - HALF_FLOAT( Types.FLOAT), - SCALED_FLOAT( Types.FLOAT), - KEYWORD( Types.VARCHAR), - TEXT( Types.VARCHAR), - OBJECT( Types.STRUCT), - NESTED( Types.STRUCT), - BINARY( Types.VARBINARY), - DATE( Types.DATE), - TIME( Types.TIME), - DATETIME( Types.TIMESTAMP), - IP( Types.VARCHAR), - INTERVAL_YEAR( ExtraTypes.INTERVAL_YEAR), - INTERVAL_MONTH( ExtraTypes.INTERVAL_MONTH), - INTERVAL_YEAR_TO_MONTH( ExtraTypes.INTERVAL_YEAR_MONTH), - INTERVAL_DAY( ExtraTypes.INTERVAL_DAY), - INTERVAL_HOUR( ExtraTypes.INTERVAL_HOUR), - INTERVAL_MINUTE( ExtraTypes.INTERVAL_MINUTE), - INTERVAL_SECOND( ExtraTypes.INTERVAL_SECOND), - INTERVAL_DAY_TO_HOUR( ExtraTypes.INTERVAL_DAY_HOUR), - INTERVAL_DAY_TO_MINUTE( ExtraTypes.INTERVAL_DAY_MINUTE), - INTERVAL_DAY_TO_SECOND( ExtraTypes.INTERVAL_DAY_SECOND), - INTERVAL_HOUR_TO_MINUTE( ExtraTypes.INTERVAL_HOUR_MINUTE), - INTERVAL_HOUR_TO_SECOND( ExtraTypes.INTERVAL_HOUR_SECOND), + NULL(Types.NULL), + UNSUPPORTED(Types.OTHER), + BOOLEAN(Types.BOOLEAN), + BYTE(Types.TINYINT), + SHORT(Types.SMALLINT), + INTEGER(Types.INTEGER), + LONG(Types.BIGINT), + DOUBLE(Types.DOUBLE), + FLOAT(Types.REAL), + HALF_FLOAT(Types.FLOAT), + SCALED_FLOAT(Types.FLOAT), + KEYWORD(Types.VARCHAR), + TEXT(Types.VARCHAR), + OBJECT(Types.STRUCT), + NESTED(Types.STRUCT), + BINARY(Types.VARBINARY), + DATE(Types.DATE), + TIME(Types.TIME), + DATETIME(Types.TIMESTAMP), + IP(Types.VARCHAR), + INTERVAL_YEAR(ExtraTypes.INTERVAL_YEAR), + INTERVAL_MONTH(ExtraTypes.INTERVAL_MONTH), + INTERVAL_YEAR_TO_MONTH(ExtraTypes.INTERVAL_YEAR_MONTH), + INTERVAL_DAY(ExtraTypes.INTERVAL_DAY), + INTERVAL_HOUR(ExtraTypes.INTERVAL_HOUR), + INTERVAL_MINUTE(ExtraTypes.INTERVAL_MINUTE), + INTERVAL_SECOND(ExtraTypes.INTERVAL_SECOND), + INTERVAL_DAY_TO_HOUR(ExtraTypes.INTERVAL_DAY_HOUR), + INTERVAL_DAY_TO_MINUTE(ExtraTypes.INTERVAL_DAY_MINUTE), + INTERVAL_DAY_TO_SECOND(ExtraTypes.INTERVAL_DAY_SECOND), + INTERVAL_HOUR_TO_MINUTE(ExtraTypes.INTERVAL_HOUR_MINUTE), + INTERVAL_HOUR_TO_SECOND(ExtraTypes.INTERVAL_HOUR_SECOND), INTERVAL_MINUTE_TO_SECOND(ExtraTypes.INTERVAL_MINUTE_SECOND), - GEO_POINT( ExtraTypes.GEOMETRY), - GEO_SHAPE( ExtraTypes.GEOMETRY), - SHAPE( ExtraTypes.GEOMETRY); + GEO_POINT(ExtraTypes.GEOMETRY), + GEO_SHAPE(ExtraTypes.GEOMETRY), + SHAPE(ExtraTypes.GEOMETRY); private final Integer type; diff --git a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/InfoResponse.java b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/InfoResponse.java index fb9b5054cd87f..7dc69ef1587b7 100644 --- a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/InfoResponse.java +++ b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/InfoResponse.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.xpack.sql.jdbc; - import org.elasticsearch.xpack.sql.proto.SqlVersion; /** diff --git a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcColumnInfo.java b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcColumnInfo.java index 84241a14bf29c..54d33f75dfe13 100644 --- a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcColumnInfo.java +++ b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcColumnInfo.java @@ -79,12 +79,12 @@ public boolean equals(Object obj) { } JdbcColumnInfo other = (JdbcColumnInfo) obj; return name.equals(other.name) - && type.equals(other.type) - && table.equals(other.table) - && catalog.equals(other.catalog) - && schema.equals(other.schema) - && label.equals(other.label) - && displaySize == other.displaySize; + && type.equals(other.type) + && table.equals(other.table) + && catalog.equals(other.catalog) + && schema.equals(other.schema) + && label.equals(other.label) + && displaySize == other.displaySize; } @Override diff --git a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcConfiguration.java b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcConfiguration.java index 262e3dee195b5..3c46543244e33 100644 --- a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcConfiguration.java +++ b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcConfiguration.java @@ -37,13 +37,12 @@ / / Additional properties can be specified either through the Properties object or in the URL. In case of duplicates, the URL wins. */ -//TODO: beef this up for Security/SSL +// TODO: beef this up for Security/SSL public class JdbcConfiguration extends ConnectionConfiguration { static final String URL_PREFIX = "jdbc:es://"; static final String URL_FULL_PREFIX = "jdbc:elasticsearch://"; public static URI DEFAULT_URI = URI.create("http://localhost:9200/"); - static final String DEBUG = "debug"; static final String DEBUG_DEFAULT = "false"; @@ -70,10 +69,10 @@ public class JdbcConfiguration extends ConnectionConfiguration { static final String INDEX_INCLUDE_FROZEN = "index.include.frozen"; static final String INDEX_INCLUDE_FROZEN_DEFAULT = "false"; - // options that don't change at runtime private static final Set OPTION_NAMES = new LinkedHashSet<>( - Arrays.asList(TIME_ZONE, CATALOG, FIELD_MULTI_VALUE_LENIENCY, INDEX_INCLUDE_FROZEN, DEBUG, DEBUG_OUTPUT, DEBUG_FLUSH_ALWAYS)); + Arrays.asList(TIME_ZONE, CATALOG, FIELD_MULTI_VALUE_LENIENCY, INDEX_INCLUDE_FROZEN, DEBUG, DEBUG_OUTPUT, DEBUG_FLUSH_ALWAYS) + ); static { // trigger version initialization @@ -171,16 +170,28 @@ private JdbcConfiguration(URI baseURI, String u, Properties props) throws JdbcSQ this.debug = parseValue(DEBUG, props.getProperty(DEBUG, DEBUG_DEFAULT), Boolean::parseBoolean); this.debugOut = props.getProperty(DEBUG_OUTPUT, DEBUG_OUTPUT_DEFAULT); - this.flushAlways = parseValue(DEBUG_FLUSH_ALWAYS, props.getProperty(DEBUG_FLUSH_ALWAYS, DEBUG_FLUSH_ALWAYS_DEFAULT), - Boolean::parseBoolean); - - this.zoneId = parseValue(TIME_ZONE, props.getProperty(TIME_ZONE, TIME_ZONE_DEFAULT), - s -> TimeZone.getTimeZone(s).toZoneId().normalized()); + this.flushAlways = parseValue( + DEBUG_FLUSH_ALWAYS, + props.getProperty(DEBUG_FLUSH_ALWAYS, DEBUG_FLUSH_ALWAYS_DEFAULT), + Boolean::parseBoolean + ); + + this.zoneId = parseValue( + TIME_ZONE, + props.getProperty(TIME_ZONE, TIME_ZONE_DEFAULT), + s -> TimeZone.getTimeZone(s).toZoneId().normalized() + ); this.catalog = props.getProperty(CATALOG); - this.fieldMultiValueLeniency = parseValue(FIELD_MULTI_VALUE_LENIENCY, - props.getProperty(FIELD_MULTI_VALUE_LENIENCY, FIELD_MULTI_VALUE_LENIENCY_DEFAULT), Boolean::parseBoolean); - this.includeFrozen = parseValue(INDEX_INCLUDE_FROZEN, props.getProperty(INDEX_INCLUDE_FROZEN, INDEX_INCLUDE_FROZEN_DEFAULT), - Boolean::parseBoolean); + this.fieldMultiValueLeniency = parseValue( + FIELD_MULTI_VALUE_LENIENCY, + props.getProperty(FIELD_MULTI_VALUE_LENIENCY, FIELD_MULTI_VALUE_LENIENCY_DEFAULT), + Boolean::parseBoolean + ); + this.includeFrozen = parseValue( + INDEX_INCLUDE_FROZEN, + props.getProperty(INDEX_INCLUDE_FROZEN, INDEX_INCLUDE_FROZEN_DEFAULT), + Boolean::parseBoolean + ); } @Override @@ -222,8 +233,7 @@ public boolean indexIncludeFrozen() { public static boolean canAccept(String url) { String u = url.trim(); - return (StringUtils.hasText(u) && - (u.startsWith(JdbcConfiguration.URL_PREFIX) || u.startsWith(JdbcConfiguration.URL_FULL_PREFIX))); + return (StringUtils.hasText(u) && (u.startsWith(JdbcConfiguration.URL_PREFIX) || u.startsWith(JdbcConfiguration.URL_FULL_PREFIX))); } public DriverPropertyInfo[] driverPropertyInfo() { diff --git a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcConnection.java b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcConnection.java index 818f631da9557..27a7e6d29123b 100644 --- a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcConnection.java +++ b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcConnection.java @@ -262,7 +262,7 @@ public Statement createStatement(int resultSetType, int resultSetConcurrency, in @Override public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) - throws SQLException { + throws SQLException { checkOpen(); checkHoldability(resultSetHoldability); return prepareStatement(sql, resultSetType, resultSetConcurrency); @@ -270,7 +270,7 @@ public PreparedStatement prepareStatement(String sql, int resultSetType, int res @Override public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) - throws SQLException { + throws SQLException { checkOpen(); checkHoldability(resultSetHoldability); return prepareCall(sql, resultSetType, resultSetConcurrency); diff --git a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcDatabaseMetaData.java b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcDatabaseMetaData.java index a44add5f46788..2284e6f2c9dc7 100644 --- a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcDatabaseMetaData.java +++ b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcDatabaseMetaData.java @@ -145,7 +145,7 @@ public boolean storesLowerCaseIdentifiers() throws SQLException { @Override public boolean storesMixedCaseIdentifiers() throws SQLException { - //TODO: is the javadoc accurate + // TODO: is the javadoc accurate return false; } @@ -182,33 +182,33 @@ public String getSQLKeywords() throws SQLException { @Override public String getNumericFunctions() throws SQLException { - //https://docs.microsoft.com/en-us/sql/odbc/reference/appendixes/numeric-functions?view=sql-server-2017 + // https://docs.microsoft.com/en-us/sql/odbc/reference/appendixes/numeric-functions?view=sql-server-2017 return "ABS,ACOS,ASIN,ATAN,ATAN2," - + "CEILING,COS," - + "DEGREES," - + "EXP," - + "FLOOR," - + "LOG,LOG10," - + "MOD," - + "PI,POWER," - + "RADIANS,RAND,ROUND," - + "SIGN,SIN,SQRT," - + "TAN,TRUNCATE"; + + "CEILING,COS," + + "DEGREES," + + "EXP," + + "FLOOR," + + "LOG,LOG10," + + "MOD," + + "PI,POWER," + + "RADIANS,RAND,ROUND," + + "SIGN,SIN,SQRT," + + "TAN,TRUNCATE"; } @Override public String getStringFunctions() throws SQLException { - //https://docs.microsoft.com/en-us/sql/odbc/reference/appendixes/string-functions?view=sql-server-2017 + // https://docs.microsoft.com/en-us/sql/odbc/reference/appendixes/string-functions?view=sql-server-2017 return "ASCII," - + "BIT_LENGTH," - + "CHAR,CHAR_LENGTH,CHARACTER_LENGTH,CONCAT," - + "INSERT," - + "LCASE,LEFT,LENGTH,LOCATE,LTRIM," - + "OCTET_LENGTH," - + "POSITION," - + "REPEAT,REPLACE,RIGHT,RTRIM," - + "SPACE,SUBSTRING," - + "UCASE"; + + "BIT_LENGTH," + + "CHAR,CHAR_LENGTH,CHARACTER_LENGTH,CONCAT," + + "INSERT," + + "LCASE,LEFT,LENGTH,LOCATE,LTRIM," + + "OCTET_LENGTH," + + "POSITION," + + "REPEAT,REPLACE,RIGHT,RTRIM," + + "SPACE,SUBSTRING," + + "UCASE"; } @Override @@ -219,15 +219,15 @@ public String getSystemFunctions() throws SQLException { @Override public String getTimeDateFunctions() throws SQLException { - //https://docs.microsoft.com/en-us/sql/odbc/reference/appendixes/time-date-and-interval-functions?view=sql-server-2017 + // https://docs.microsoft.com/en-us/sql/odbc/reference/appendixes/time-date-and-interval-functions?view=sql-server-2017 return "DAYNAME,DAYOFMONTH,DAYOFWEEK,DAYOFYEAR" - + "EXTRACT," - + "HOUR," - + "MINUTE,MONTH,MONTHNAME" - + "QUARTER," - + "SECOND," - + "WEEK," - + "YEAR"; + + "EXTRACT," + + "HOUR," + + "MINUTE,MONTH,MONTHNAME" + + "QUARTER," + + "SECOND," + + "WEEK," + + "YEAR"; } @Override @@ -668,42 +668,63 @@ public boolean dataDefinitionIgnoredInTransactions() throws SQLException { // https://www.postgresql.org/docs/9.0/static/infoschema-routines.html @Override public ResultSet getProcedures(String catalog, String schemaPattern, String procedureNamePattern) throws SQLException { - return emptySet(con.cfg, "ROUTINES", - "PROCEDURE_CAT", - "PROCEDURE_SCHEM", - "PROCEDURE_NAME", - "NUM_INPUT_PARAMS", INTEGER, - "NUM_OUTPUT_PARAMS", INTEGER, - "NUM_RESULT_SETS", INTEGER, - "REMARKS", - "PROCEDURE_TYPE", SMALLINT, - "SPECIFIC_NAME"); + return emptySet( + con.cfg, + "ROUTINES", + "PROCEDURE_CAT", + "PROCEDURE_SCHEM", + "PROCEDURE_NAME", + "NUM_INPUT_PARAMS", + INTEGER, + "NUM_OUTPUT_PARAMS", + INTEGER, + "NUM_RESULT_SETS", + INTEGER, + "REMARKS", + "PROCEDURE_TYPE", + SMALLINT, + "SPECIFIC_NAME" + ); } @Override public ResultSet getProcedureColumns(String catalog, String schemaPattern, String procedureNamePattern, String columnNamePattern) - throws SQLException { - return emptySet(con.cfg, "ROUTINES_COLUMNS", - "PROCEDURE_CAT", - "PROCEDURE_SCHEM", - "PROCEDURE_NAME", - "COLUMN_NAME", - "COLUMN_TYPE", SMALLINT, - "DATA_TYPE", INTEGER, - "TYPE_NAME", - "PRECISION", INTEGER, - "LENGTH", INTEGER, - "SCALE", SMALLINT, - "RADIX", SMALLINT, - "NULLABLE", SMALLINT, - "REMARKS", - "COLUMN_DEF", - "SQL_DATA_TYPE", INTEGER, - "SQL_DATETIME_SUB", INTEGER, - "CHAR_OCTET_LENGTH", INTEGER, - "ORDINAL_POSITION", INTEGER, - "IS_NULLABLE", - "SPECIFIC_NAME"); + throws SQLException { + return emptySet( + con.cfg, + "ROUTINES_COLUMNS", + "PROCEDURE_CAT", + "PROCEDURE_SCHEM", + "PROCEDURE_NAME", + "COLUMN_NAME", + "COLUMN_TYPE", + SMALLINT, + "DATA_TYPE", + INTEGER, + "TYPE_NAME", + "PRECISION", + INTEGER, + "LENGTH", + INTEGER, + "SCALE", + SMALLINT, + "RADIX", + SMALLINT, + "NULLABLE", + SMALLINT, + "REMARKS", + "COLUMN_DEF", + "SQL_DATA_TYPE", + INTEGER, + "SQL_DATETIME_SUB", + INTEGER, + "CHAR_OCTET_LENGTH", + INTEGER, + "ORDINAL_POSITION", + INTEGER, + "IS_NULLABLE", + "SPECIFIC_NAME" + ); } // return the cluster name as the catalog (database) @@ -755,9 +776,7 @@ public ResultSet getTables(String catalog, String schemaPattern, String tableNam @Override public ResultSet getSchemas() throws SQLException { - return emptySet(con.cfg, "SCHEMATA", - "TABLE_SCHEM", - "TABLE_CATALOG"); + return emptySet(con.cfg, "SCHEMATA", "TABLE_SCHEM", "TABLE_CATALOG"); } @Override @@ -779,10 +798,9 @@ public ResultSet getTableTypes() throws SQLException { return memorySet(con.cfg, columnInfo("TABLE_TYPES", "TABLE_TYPE"), data); } - @Override public ResultSet getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern) - throws SQLException { + throws SQLException { PreparedStatement ps = con.prepareStatement("SYS COLUMNS CATALOG ? TABLE LIKE ? ESCAPE '\\' LIKE ? ESCAPE '\\'"); // NB: catalog is not a pattern hence why null is send instead ps.setString(1, catalog != null ? catalog.trim() : null); @@ -793,128 +811,160 @@ public ResultSet getColumns(String catalog, String schemaPattern, String tableNa @Override public ResultSet getColumnPrivileges(String catalog, String schema, String table, String columnNamePattern) throws SQLException { - return emptySet(con.cfg, "", - "TABLE_CAT", - "TABLE_SCHEM", - "TABLE_NAME", - "COLUMN_NAME", - "GRANTOR", - "GRANTEE", - "PRIVILEGE", - "IS_GRANTABLE"); + return emptySet( + con.cfg, + "", + "TABLE_CAT", + "TABLE_SCHEM", + "TABLE_NAME", + "COLUMN_NAME", + "GRANTOR", + "GRANTEE", + "PRIVILEGE", + "IS_GRANTABLE" + ); } @Override public ResultSet getTablePrivileges(String catalog, String schemaPattern, String tableNamePattern) throws SQLException { - return emptySet(con.cfg, "", - "TABLE_CAT", - "TABLE_SCHEM", - "TABLE_NAME", - "GRANTOR", - "GRANTEE", - "PRIVILEGE", - "IS_GRANTABLE"); + return emptySet(con.cfg, "", "TABLE_CAT", "TABLE_SCHEM", "TABLE_NAME", "GRANTOR", "GRANTEE", "PRIVILEGE", "IS_GRANTABLE"); } @Override public ResultSet getBestRowIdentifier(String catalog, String schema, String table, int scope, boolean nullable) throws SQLException { - return emptySet(con.cfg, "", - "SCOPE", SMALLINT, - "COLUMN_NAME", - "DATA_TYPE", INTEGER, - "TYPE_NAME", - "COLUMN_SIZE", INTEGER, - "BUFFER_LENGTH", INTEGER, - "DECIMAL_DIGITS", SMALLINT, - "PSEUDO_COLUMN", SMALLINT); + return emptySet( + con.cfg, + "", + "SCOPE", + SMALLINT, + "COLUMN_NAME", + "DATA_TYPE", + INTEGER, + "TYPE_NAME", + "COLUMN_SIZE", + INTEGER, + "BUFFER_LENGTH", + INTEGER, + "DECIMAL_DIGITS", + SMALLINT, + "PSEUDO_COLUMN", + SMALLINT + ); } @Override public ResultSet getVersionColumns(String catalog, String schema, String table) throws SQLException { - return emptySet(con.cfg, "", - "SCOPE", SMALLINT, - "COLUMN_NAME", - "DATA_TYPE", INTEGER, - "TYPE_NAME", - "COLUMN_SIZE", INTEGER, - "BUFFER_LENGTH", INTEGER, - "DECIMAL_DIGITS", SMALLINT, - "PSEUDO_COLUMN", SMALLINT); + return emptySet( + con.cfg, + "", + "SCOPE", + SMALLINT, + "COLUMN_NAME", + "DATA_TYPE", + INTEGER, + "TYPE_NAME", + "COLUMN_SIZE", + INTEGER, + "BUFFER_LENGTH", + INTEGER, + "DECIMAL_DIGITS", + SMALLINT, + "PSEUDO_COLUMN", + SMALLINT + ); } @Override public ResultSet getPrimaryKeys(String catalog, String schema, String table) throws SQLException { - return emptySet(con.cfg, "", - "TABLE_CAT", - "TABLE_SCHEM", - "TABLE_NAME", - "COLUMN_NAME", - "KEY_SEQ", SMALLINT, - "PK_NAME"); + return emptySet(con.cfg, "", "TABLE_CAT", "TABLE_SCHEM", "TABLE_NAME", "COLUMN_NAME", "KEY_SEQ", SMALLINT, "PK_NAME"); } @Override public ResultSet getImportedKeys(String catalog, String schema, String table) throws SQLException { - return emptySet(con.cfg, "", - "PKTABLE_CAT", - "PKTABLE_SCHEM", - "PKTABLE_NAME", - "PKCOLUMN_NAME", - "FKTABLE_CAT", - "FKTABLE_SCHEM", - "FKTABLE_NAME", - "FKCOLUMN_NAME", - "KEY_SEQ", SMALLINT, - "UPDATE_RULE ", SMALLINT, - "DELETE_RULE ", SMALLINT, - "FK_NAME", - "PK_NAME ", - "DEFERRABILITY", SMALLINT, - "IS_NULLABLE" - ); + return emptySet( + con.cfg, + "", + "PKTABLE_CAT", + "PKTABLE_SCHEM", + "PKTABLE_NAME", + "PKCOLUMN_NAME", + "FKTABLE_CAT", + "FKTABLE_SCHEM", + "FKTABLE_NAME", + "FKCOLUMN_NAME", + "KEY_SEQ", + SMALLINT, + "UPDATE_RULE ", + SMALLINT, + "DELETE_RULE ", + SMALLINT, + "FK_NAME", + "PK_NAME ", + "DEFERRABILITY", + SMALLINT, + "IS_NULLABLE" + ); } @Override public ResultSet getExportedKeys(String catalog, String schema, String table) throws SQLException { - return emptySet(con.cfg, "", - "PKTABLE_CAT", - "PKTABLE_SCHEM", - "PKTABLE_NAME", - "PKCOLUMN_NAME", - "FKTABLE_CAT", - "FKTABLE_SCHEM", - "FKTABLE_NAME", - "FKCOLUMN_NAME", - "KEY_SEQ", SMALLINT, - "UPDATE_RULE ", SMALLINT, - "DELETE_RULE ", SMALLINT, - "FK_NAME", - "PK_NAME ", - "DEFERRABILITY", SMALLINT, - "IS_NULLABLE" - ); - } - - @Override - public ResultSet getCrossReference(String parentCatalog, String parentSchema, String parentTable, String foreignCatalog, - String foreignSchema, String foreignTable) throws SQLException { - return emptySet(con.cfg, "", - "PKTABLE_CAT", - "PKTABLE_SCHEM", - "PKTABLE_NAME", - "PKCOLUMN_NAME", - "FKTABLE_CAT", - "FKTABLE_SCHEM", - "FKTABLE_NAME", - "FKCOLUMN_NAME", - "KEY_SEQ", SMALLINT, - "UPDATE_RULE ", SMALLINT, - "DELETE_RULE ", SMALLINT, - "FK_NAME", - "PK_NAME ", - "DEFERRABILITY", SMALLINT, - "IS_NULLABLE" - ); + return emptySet( + con.cfg, + "", + "PKTABLE_CAT", + "PKTABLE_SCHEM", + "PKTABLE_NAME", + "PKCOLUMN_NAME", + "FKTABLE_CAT", + "FKTABLE_SCHEM", + "FKTABLE_NAME", + "FKCOLUMN_NAME", + "KEY_SEQ", + SMALLINT, + "UPDATE_RULE ", + SMALLINT, + "DELETE_RULE ", + SMALLINT, + "FK_NAME", + "PK_NAME ", + "DEFERRABILITY", + SMALLINT, + "IS_NULLABLE" + ); + } + + @Override + public ResultSet getCrossReference( + String parentCatalog, + String parentSchema, + String parentTable, + String foreignCatalog, + String foreignSchema, + String foreignTable + ) throws SQLException { + return emptySet( + con.cfg, + "", + "PKTABLE_CAT", + "PKTABLE_SCHEM", + "PKTABLE_NAME", + "PKCOLUMN_NAME", + "FKTABLE_CAT", + "FKTABLE_SCHEM", + "FKTABLE_NAME", + "FKCOLUMN_NAME", + "KEY_SEQ", + SMALLINT, + "UPDATE_RULE ", + SMALLINT, + "DELETE_RULE ", + SMALLINT, + "FK_NAME", + "PK_NAME ", + "DEFERRABILITY", + SMALLINT, + "IS_NULLABLE" + ); } @Override @@ -924,22 +974,29 @@ public ResultSet getTypeInfo() throws SQLException { @Override public ResultSet getIndexInfo(String catalog, String schema, String table, boolean unique, boolean approximate) throws SQLException { - return emptySet(con.cfg, "", - "TABLE_CAT", - "TABLE_SCHEM", - "TABLE_NAME", - "NON_UNIQUE", BOOLEAN, - "INDEX_QUALIFIER", - "INDEX_NAME", - "TYPE", SMALLINT, - "ORDINAL_POSITION", SMALLINT, - "COLUMN_NAME", - "ASC_OR_DESC", - "CARDINALITY", BIGINT, - "PAGES", BIGINT, - "FILTER_CONDITION", - "TYPE_NAME" - ); + return emptySet( + con.cfg, + "", + "TABLE_CAT", + "TABLE_SCHEM", + "TABLE_NAME", + "NON_UNIQUE", + BOOLEAN, + "INDEX_QUALIFIER", + "INDEX_NAME", + "TYPE", + SMALLINT, + "ORDINAL_POSITION", + SMALLINT, + "COLUMN_NAME", + "ASC_OR_DESC", + "CARDINALITY", + BIGINT, + "PAGES", + BIGINT, + "FILTER_CONDITION", + "TYPE_NAME" + ); } @Override @@ -1004,15 +1061,20 @@ public boolean supportsBatchUpdates() throws SQLException { @Override public ResultSet getUDTs(String catalog, String schemaPattern, String typeNamePattern, int[] types) throws SQLException { - return emptySet(con.cfg, "", - "USER_DEFINED_TYPES", - "TYPE_CAT", - "TYPE_SCHEM", - "TYPE_NAME", - "CLASS_NAME", - "DATA_TYPE", INTEGER, - "REMARKS", - "BASE_TYPE", SMALLINT); + return emptySet( + con.cfg, + "", + "USER_DEFINED_TYPES", + "TYPE_CAT", + "TYPE_SCHEM", + "TYPE_NAME", + "CLASS_NAME", + "DATA_TYPE", + INTEGER, + "REMARKS", + "BASE_TYPE", + SMALLINT + ); } @Override @@ -1042,52 +1104,64 @@ public boolean supportsGetGeneratedKeys() throws SQLException { @Override public ResultSet getSuperTypes(String catalog, String schemaPattern, String typeNamePattern) throws SQLException { - return emptySet(con.cfg, "", - "SUPER_TYPES", - "TYPE_CAT", - "TYPE_SCHEM", - "TYPE_NAME", - "SUPERTYPE_CAT", - "SUPERTYPE_SCHEM", - "SUPERTYPE_NAME", - "BASE_TYPE"); + return emptySet( + con.cfg, + "", + "SUPER_TYPES", + "TYPE_CAT", + "TYPE_SCHEM", + "TYPE_NAME", + "SUPERTYPE_CAT", + "SUPERTYPE_SCHEM", + "SUPERTYPE_NAME", + "BASE_TYPE" + ); } @Override public ResultSet getSuperTables(String catalog, String schemaPattern, String tableNamePattern) throws SQLException { - return emptySet(con.cfg, "", - "TABLE_CAT", - "TABLE_SCHEM", - "TABLE_NAME", - "SUPERTABLE_NAME"); + return emptySet(con.cfg, "", "TABLE_CAT", "TABLE_SCHEM", "TABLE_NAME", "SUPERTABLE_NAME"); } @Override public ResultSet getAttributes(String catalog, String schemaPattern, String typeNamePattern, String attributeNamePattern) - throws SQLException { - return emptySet(con.cfg, "", - "ATTRIBUTES", - "TYPE_CAT", - "TYPE_SCHEM", - "TYPE_NAME", - "ATTR_NAME", - "DATA_TYPE", INTEGER, - "ATTR_TYPE_NAME", - "ATTR_SIZE", INTEGER, - "DECIMAL_DIGITS", INTEGER, - "NUM_PREC_RADIX", INTEGER, - "NULLABLE", INTEGER, - "REMARKS", - "ATTR_DEF", - "SQL_DATA_TYPE", INTEGER, - "SQL_DATETIME_SUB", INTEGER, - "CHAR_OCTET_LENGTH", INTEGER, - "ORDINAL_POSITION", INTEGER, - "IS_NULLABLE", - "SCOPE_CATALOG", - "SCOPE_SCHEMA", - "SCOPE_TABLE", - "SOURCE_DATA_TYPE", SMALLINT); + throws SQLException { + return emptySet( + con.cfg, + "", + "ATTRIBUTES", + "TYPE_CAT", + "TYPE_SCHEM", + "TYPE_NAME", + "ATTR_NAME", + "DATA_TYPE", + INTEGER, + "ATTR_TYPE_NAME", + "ATTR_SIZE", + INTEGER, + "DECIMAL_DIGITS", + INTEGER, + "NUM_PREC_RADIX", + INTEGER, + "NULLABLE", + INTEGER, + "REMARKS", + "ATTR_DEF", + "SQL_DATA_TYPE", + INTEGER, + "SQL_DATETIME_SUB", + INTEGER, + "CHAR_OCTET_LENGTH", + INTEGER, + "ORDINAL_POSITION", + INTEGER, + "IS_NULLABLE", + "SCOPE_CATALOG", + "SCOPE_SCHEMA", + "SCOPE_TABLE", + "SOURCE_DATA_TYPE", + SMALLINT + ); } @Override @@ -1163,64 +1237,82 @@ public ResultSet getClientInfoProperties() throws SQLException { data[i][3] = EMPTY; } - return memorySet(con.cfg, columnInfo("", - "NAME", - "MAX_LEN", INTEGER, - "DEFAULT_VALUE", - "DESCRIPTION"), data); + return memorySet(con.cfg, columnInfo("", "NAME", "MAX_LEN", INTEGER, "DEFAULT_VALUE", "DESCRIPTION"), data); } @Override public ResultSet getFunctions(String catalog, String schemaPattern, String functionNamePattern) throws SQLException { - return emptySet(con.cfg, "", - "FUNCTIONS", - "FUNCTION_CAT", - "FUNCTION_SCHEM", - "FUNCTION_NAME", - "REMARKS", - "FUNCTION_TYPE", SMALLINT, - "SPECIFIC_NAME"); + return emptySet( + con.cfg, + "", + "FUNCTIONS", + "FUNCTION_CAT", + "FUNCTION_SCHEM", + "FUNCTION_NAME", + "REMARKS", + "FUNCTION_TYPE", + SMALLINT, + "SPECIFIC_NAME" + ); } @Override public ResultSet getFunctionColumns(String catalog, String schemaPattern, String functionNamePattern, String columnNamePattern) - throws SQLException { - return emptySet(con.cfg, "", - "FUNCTION_COLUMNS", - "FUNCTION_CAT", - "FUNCTION_SCHEM", - "FUNCTION_NAME", - "COLUMN_NAME", - "DATA_TYPE", INTEGER, - "TYPE_NAME", - "PRECISION", INTEGER, - "LENGTH", INTEGER, - "SCALE", SMALLINT, - "RADIX", SMALLINT, - "NULLABLE", SMALLINT, - "REMARKS", - "CHAR_OCTET_LENGTH", INTEGER, - "ORDINAL_POSITION", INTEGER, - "IS_NULLABLE", - "SPECIFIC_NAME"); + throws SQLException { + return emptySet( + con.cfg, + "", + "FUNCTION_COLUMNS", + "FUNCTION_CAT", + "FUNCTION_SCHEM", + "FUNCTION_NAME", + "COLUMN_NAME", + "DATA_TYPE", + INTEGER, + "TYPE_NAME", + "PRECISION", + INTEGER, + "LENGTH", + INTEGER, + "SCALE", + SMALLINT, + "RADIX", + SMALLINT, + "NULLABLE", + SMALLINT, + "REMARKS", + "CHAR_OCTET_LENGTH", + INTEGER, + "ORDINAL_POSITION", + INTEGER, + "IS_NULLABLE", + "SPECIFIC_NAME" + ); } @Override public ResultSet getPseudoColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern) - throws SQLException { - return emptySet(con.cfg, "", - "PSEUDO_COLUMNS", - "TABLE_CAT", - "TABLE_SCHEM", - "TABLE_NAME", - "COLUMN_NAME", - "DATA_TYPE", INTEGER, - "COLUMN_SIZE", INTEGER, - "DECIMAL_DIGITS", INTEGER, - "NUM_PREC_RADIX", INTEGER, - "REMARKS", - "COLUMN_USAGE", - "IS_NULLABLE"); + throws SQLException { + return emptySet( + con.cfg, + "", + "PSEUDO_COLUMNS", + "TABLE_CAT", + "TABLE_SCHEM", + "TABLE_NAME", + "COLUMN_NAME", + "DATA_TYPE", + INTEGER, + "COLUMN_SIZE", + INTEGER, + "DECIMAL_DIGITS", + INTEGER, + "NUM_PREC_RADIX", + INTEGER, + "REMARKS", + "COLUMN_USAGE", + "IS_NULLABLE" + ); } @Override @@ -1247,7 +1339,6 @@ private static Object[][] queryColumn(JdbcConnection con, String query, int... c return data.toArray(new Object[][] {}); } - private static List columnInfo(String tableName, Object... cols) throws JdbcSQLException { List columns = new ArrayList<>(); @@ -1270,8 +1361,7 @@ private static List columnInfo(String tableName, Object... cols) // it's not, use the default and move on } columns.add(new JdbcColumnInfo(name, type, tableName, "INFORMATION_SCHEMA", EMPTY, EMPTY, 0)); - } - else { + } else { throw new JdbcSQLException("Invalid metadata schema definition"); } } diff --git a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcDateUtils.java b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcDateUtils.java index a4c7479fea0b8..7bd0431f194a9 100644 --- a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcDateUtils.java +++ b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcDateUtils.java @@ -70,6 +70,7 @@ static Timestamp asTimestamp(String date) { static Timestamp timeAsTimestamp(String date) { return new Timestamp(timeAsMillisSinceEpoch(date)); } + /* * Handles the value received as parameter, as either String (a ZonedDateTime formatted in ISO 8601 standard with millis) - * date fields being returned formatted like this. Or a Long value, in case of Histograms. diff --git a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcHttpClient.java b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcHttpClient.java index f0b3506d9a1ba..0a3696647d82d 100644 --- a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcHttpClient.java +++ b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcHttpClient.java @@ -60,19 +60,23 @@ boolean ping(long timeoutInMs) throws SQLException { Cursor query(String sql, List params, RequestMeta meta) throws SQLException { int fetch = meta.fetchSize() > 0 ? meta.fetchSize() : conCfg.pageSize(); - SqlQueryRequest sqlRequest = new SqlQueryRequest(sql, params, conCfg.zoneId(), - jdbcConn.getCatalog(), - fetch, - TimeValue.timeValueMillis(meta.queryTimeoutInMs()), - TimeValue.timeValueMillis(meta.pageTimeoutInMs()), - null, - Boolean.FALSE, - null, - new RequestInfo(Mode.JDBC, ClientVersion.CURRENT), - conCfg.fieldMultiValueLeniency(), - conCfg.indexIncludeFrozen(), - conCfg.binaryCommunication(), - emptyMap()); + SqlQueryRequest sqlRequest = new SqlQueryRequest( + sql, + params, + conCfg.zoneId(), + jdbcConn.getCatalog(), + fetch, + TimeValue.timeValueMillis(meta.queryTimeoutInMs()), + TimeValue.timeValueMillis(meta.pageTimeoutInMs()), + null, + Boolean.FALSE, + null, + new RequestInfo(Mode.JDBC, ClientVersion.CURRENT), + conCfg.fieldMultiValueLeniency(), + conCfg.indexIncludeFrozen(), + conCfg.binaryCommunication(), + emptyMap() + ); SqlQueryResponse response = httpClient.query(sqlRequest); return new DefaultCursor(this, response.cursor(), toJdbcColumnInfo(response.columns()), response.rows(), meta); } @@ -112,9 +116,12 @@ private InfoResponse fetchServerInfo() throws SQLException { private void checkServerVersion() throws SQLException { if (ClientVersion.isServerCompatible(serverInfo.version) == false) { - throw new SQLException("This version of the JDBC driver is only compatible with Elasticsearch version " + - ClientVersion.CURRENT.majorMinorToString() + " or newer; attempting to connect to a server version " + - serverInfo.version.toString()); + throw new SQLException( + "This version of the JDBC driver is only compatible with Elasticsearch version " + + ClientVersion.CURRENT.majorMinorToString() + + " or newer; attempting to connect to a server version " + + serverInfo.version.toString() + ); } } diff --git a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcPreparedStatement.java b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcPreparedStatement.java index 3f087c608a47c..0b77f714f2540 100644 --- a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcPreparedStatement.java +++ b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcPreparedStatement.java @@ -81,8 +81,9 @@ private void setParam(int parameterIndex, Object value, EsType type) throws SQLE checkOpen(); if (parameterIndex < 0 || parameterIndex > query.paramCount()) { - throw new SQLException("Invalid parameter index [ " + parameterIndex + "; needs to be between 1 and [" + query.paramCount() + - "]"); + throw new SQLException( + "Invalid parameter index [ " + parameterIndex + "; needs to be between 1 and [" + query.paramCount() + "]" + ); } query.setParam(parameterIndex, value, type); @@ -211,7 +212,6 @@ public void setObject(int parameterIndex, Object x) throws SQLException { setObject(parameterIndex, x, TypeUtils.of(x.getClass()).getVendorTypeNumber(), 0); } - @Override public void addBatch() throws SQLException { throw new SQLFeatureNotSupportedException("Batching not supported"); @@ -367,20 +367,18 @@ private void setObject(int parameterIndex, Object x, EsType dataType, String typ checkKnownUnsupportedTypes(x); if (x instanceof byte[]) { if (dataType != EsType.BINARY) { - throw new SQLFeatureNotSupportedException( - "Conversion from type [byte[]] to [" + typeString + "] not supported"); + throw new SQLFeatureNotSupportedException("Conversion from type [byte[]] to [" + typeString + "] not supported"); } setParam(parameterIndex, x, EsType.BINARY); return; } if (x instanceof Timestamp - || x instanceof Calendar - || x instanceof Date - || x instanceof LocalDateTime - || x instanceof Time - || x instanceof java.util.Date) - { + || x instanceof Calendar + || x instanceof Date + || x instanceof LocalDateTime + || x instanceof Time + || x instanceof java.util.Date) { if (dataType == EsType.DATETIME || dataType == EsType.TIME) { // converting to {@code java.util.Date} because this is the type supported by {@code XContentBuilder} for serialization @@ -411,36 +409,55 @@ private void setObject(int parameterIndex, Object x, EsType dataType, String typ } // anything else other than VARCHAR and TIMESTAMP is not supported in this JDBC driver throw new SQLFeatureNotSupportedException( - "Conversion from type [" + x.getClass().getName() + "] to [" + typeString + "] not supported"); + "Conversion from type [" + x.getClass().getName() + "] to [" + typeString + "] not supported" + ); } if (x instanceof Boolean - || x instanceof Byte - || x instanceof Short - || x instanceof Integer - || x instanceof Long - || x instanceof Float - || x instanceof Double - || x instanceof String) { - setParam(parameterIndex, - TypeConverter.convert(x, TypeUtils.of(x.getClass()), (Class) TypeUtils.classOf(dataType), typeString), - dataType); + || x instanceof Byte + || x instanceof Short + || x instanceof Integer + || x instanceof Long + || x instanceof Float + || x instanceof Double + || x instanceof String) { + setParam( + parameterIndex, + TypeConverter.convert(x, TypeUtils.of(x.getClass()), (Class) TypeUtils.classOf(dataType), typeString), + dataType + ); return; } throw new SQLFeatureNotSupportedException( - "Conversion from type [" + x.getClass().getName() + "] to [" + typeString + "] not supported"); + "Conversion from type [" + x.getClass().getName() + "] to [" + typeString + "] not supported" + ); } private void checkKnownUnsupportedTypes(Object x) throws SQLFeatureNotSupportedException { - List> unsupportedTypes = new ArrayList<>(Arrays.asList(Struct.class, Array.class, SQLXML.class, - RowId.class, Ref.class, Blob.class, NClob.class, Clob.class, LocalDate.class, LocalTime.class, - OffsetTime.class, OffsetDateTime.class, URL.class, BigDecimal.class)); - - for (Class clazz:unsupportedTypes) { - if (clazz.isAssignableFrom(x.getClass())) { + List> unsupportedTypes = new ArrayList<>( + Arrays.asList( + Struct.class, + Array.class, + SQLXML.class, + RowId.class, + Ref.class, + Blob.class, + NClob.class, + Clob.class, + LocalDate.class, + LocalTime.class, + OffsetTime.class, + OffsetDateTime.class, + URL.class, + BigDecimal.class + ) + ); + + for (Class clazz : unsupportedTypes) { + if (clazz.isAssignableFrom(x.getClass())) { throw new SQLFeatureNotSupportedException("Objects of type [" + clazz.getName() + "] are not supported"); - } + } } } diff --git a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcResultSet.java b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcResultSet.java index 411d5bb7ed71b..5cf51dbcb1712 100644 --- a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcResultSet.java +++ b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcResultSet.java @@ -6,6 +6,8 @@ */ package org.elasticsearch.xpack.sql.jdbc; +import org.elasticsearch.core.SuppressForbidden; + import java.io.InputStream; import java.io.Reader; import java.math.BigDecimal; @@ -32,8 +34,6 @@ import java.util.Locale; import java.util.Map; -import org.elasticsearch.core.SuppressForbidden; - import static java.lang.String.format; import static org.elasticsearch.xpack.sql.jdbc.EsType.DATE; import static org.elasticsearch.xpack.sql.jdbc.EsType.DATETIME; @@ -289,7 +289,9 @@ private Long dateTimeAsMillis(int columnIndex) throws SQLException { return (Long) val; } catch (ClassCastException cce) { throw new SQLException( - format(Locale.ROOT, "Unable to convert value [%.128s] of type [%s] to a Long", val, type.getName()), cce); + format(Locale.ROOT, "Unable to convert value [%.128s] of type [%s] to a Long", val, type.getName()), + cce + ); } } @@ -308,8 +310,7 @@ private Date asDate(int columnIndex) throws SQLException { try { return JdbcDateUtils.asDate(val.toString()); } catch (Exception e) { - throw new SQLException( - format(Locale.ROOT, "Unable to convert value [%.128s] of type [%s] to a Date", val, type.getName()), e); + throw new SQLException(format(Locale.ROOT, "Unable to convert value [%.128s] of type [%s] to a Date", val, type.getName()), e); } } @@ -331,8 +332,7 @@ private Time asTime(int columnIndex) throws SQLException { } return JdbcDateUtils.asTime(val.toString()); } catch (Exception e) { - throw new SQLException( - format(Locale.ROOT, "Unable to convert value [%.128s] of type [%s] to a Time", val, type.getName()), e); + throw new SQLException(format(Locale.ROOT, "Unable to convert value [%.128s] of type [%s] to a Time", val, type.getName()), e); } } @@ -354,7 +354,9 @@ private Timestamp asTimeStamp(int columnIndex) throws SQLException { return asTimestamp(val.toString()); } catch (Exception e) { throw new SQLException( - format(Locale.ROOT, "Unable to convert value [%.128s] of type [%s] to a Timestamp", val, type.getName()), e); + format(Locale.ROOT, "Unable to convert value [%.128s] of type [%s] to a Timestamp", val, type.getName()), + e + ); } } @@ -559,7 +561,7 @@ public InputStream getBinaryStream(int columnIndex) throws SQLException { @Override @Deprecated - @SuppressForbidden(reason="implementing deprecated method") + @SuppressForbidden(reason = "implementing deprecated method") public BigDecimal getBigDecimal(String columnLabel, int scale) throws SQLException { return getBigDecimal(column(columnLabel), scale); } @@ -1245,7 +1247,13 @@ public void updateNClob(String columnLabel, Reader reader) throws SQLException { @Override public String toString() { - return format(Locale.ROOT, "%s:row %d:cursor size %d:%s", getClass().getSimpleName(), rowNumber, cursor.batchSize(), - cursor.columns()); + return format( + Locale.ROOT, + "%s:row %d:cursor size %d:%s", + getClass().getSimpleName(), + rowNumber, + cursor.batchSize(), + cursor.columns() + ); } } diff --git a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcStatement.java b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcStatement.java index f85964614bc6b..3090e6f1fe4e4 100644 --- a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcStatement.java +++ b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/JdbcStatement.java @@ -39,7 +39,7 @@ class JdbcStatement implements Statement, JdbcWrapper { @Override public ResultSet executeQuery(String sql) throws SQLException { if (execute(sql) == false) { - throw new SQLException("Invalid sql query [" + sql + "]"); + throw new SQLException("Invalid sql query [" + sql + "]"); } return rs; } @@ -81,7 +81,6 @@ public int getMaxRows() throws SQLException { return Math.toIntExact(result); } - @Override public long getLargeMaxRows() throws SQLException { checkOpen(); @@ -190,9 +189,7 @@ public boolean getMoreResults() throws SQLException { @Override public void setFetchDirection(int direction) throws SQLException { checkOpen(); - if (ResultSet.FETCH_REVERSE != direction - || ResultSet.FETCH_FORWARD != direction - || ResultSet.FETCH_UNKNOWN != direction) { + if (ResultSet.FETCH_REVERSE != direction || ResultSet.FETCH_FORWARD != direction || ResultSet.FETCH_UNKNOWN != direction) { throw new SQLException("Invalid direction specified"); } } diff --git a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/Nullable.java b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/Nullable.java index 8dc9eb1cccbbc..ffe1928084c5f 100644 --- a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/Nullable.java +++ b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/Nullable.java @@ -19,6 +19,6 @@ */ @Documented @Retention(RetentionPolicy.RUNTIME) -@Target({ElementType.PARAMETER, ElementType.FIELD, ElementType.METHOD}) +@Target({ ElementType.PARAMETER, ElementType.FIELD, ElementType.METHOD }) @interface Nullable { } diff --git a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/PreparedQuery.java b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/PreparedQuery.java index 8ac74c59c9f7f..da12a3664bf89 100644 --- a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/PreparedQuery.java +++ b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/PreparedQuery.java @@ -70,9 +70,9 @@ String sql() { * Returns the parameters if the SQL statement is parametrized */ List params() { - return Arrays.stream(this.params).map( - paramInfo -> new SqlTypedParamValue(paramInfo.type.name(), paramInfo.value) - ).collect(Collectors.toList()); + return Arrays.stream(this.params) + .map(paramInfo -> new SqlTypedParamValue(paramInfo.type.name(), paramInfo.value)) + .collect(Collectors.toList()); } @Override diff --git a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/SqlQueryParameterAnalyzer.java b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/SqlQueryParameterAnalyzer.java index be3605fde2ce7..770da9c8106d9 100644 --- a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/SqlQueryParameterAnalyzer.java +++ b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/SqlQueryParameterAnalyzer.java @@ -42,7 +42,7 @@ public static int parametersCount(String sql) throws SQLException { i = skipString(i, sql, c); break; case '?': - params ++; + params++; break; case '-': if (i + 1 < l && sql.charAt(i + 1) == '-') { @@ -68,7 +68,6 @@ private static int skipJdbcEscape(int i, String sql) throws SQLException { throw new SQLException("Jdbc escape sequences are not supported yet"); } - /** * Skips a line comment starting at the current position i, returns the length of the comment */ diff --git a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/TypeConverter.java b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/TypeConverter.java index 54f6a8ce7dbe5..8f698ec37ad0f 100644 --- a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/TypeConverter.java +++ b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/TypeConverter.java @@ -116,9 +116,8 @@ static long convertFromCalendarToUTC(long value, Calendar cal) { Calendar c = (Calendar) cal.clone(); c.setTimeInMillis(value); - ZonedDateTime convertedDateTime = ZonedDateTime - .ofInstant(c.toInstant(), c.getTimeZone().toZoneId()) - .withZoneSameLocal(ZoneOffset.UTC); + ZonedDateTime convertedDateTime = ZonedDateTime.ofInstant(c.toInstant(), c.getTimeZone().toZoneId()) + .withZoneSameLocal(ZoneOffset.UTC); return convertedDateTime.toInstant().toEpochMilli(); } @@ -308,9 +307,8 @@ private static T failConversion(Object value, EsType columnType, String type } private static T failConversion(Object value, EsType columnType, String typeString, Class target, Exception e) - throws SQLException { - String message = format(Locale.ROOT, "Unable to convert value [%.128s] of type [%s] to [%s]", value, columnType, - typeString); + throws SQLException { + String message = format(Locale.ROOT, "Unable to convert value [%.128s] of type [%s] to [%s]", value, columnType, typeString); throw e != null ? new SQLException(message, e) : new SQLException(message); } @@ -427,11 +425,11 @@ private static Long asLong(Object val, EsType columnType, String typeString) thr case SCALED_FLOAT: case DOUBLE: return safeToLong(((Number) val).doubleValue()); - //TODO: should we support conversion to TIMESTAMP? - //The spec says that getLong() should support the following types conversions: - //TINYINT, SMALLINT, INTEGER, BIGINT, REAL, FLOAT, DOUBLE, DECIMAL, NUMERIC, BIT, BOOLEAN, CHAR, VARCHAR, LONGVARCHAR - //case TIMESTAMP: - // return ((Number) val).longValue(); + // TODO: should we support conversion to TIMESTAMP? + // The spec says that getLong() should support the following types conversions: + // TINYINT, SMALLINT, INTEGER, BIGINT, REAL, FLOAT, DOUBLE, DECIMAL, NUMERIC, BIT, BOOLEAN, CHAR, VARCHAR, LONGVARCHAR + // case TIMESTAMP: + // return ((Number) val).longValue(); case KEYWORD: case TEXT: try { @@ -557,8 +555,8 @@ private static BigDecimal asBigDecimal(Object val, EsType columnType, String typ } catch (NumberFormatException nfe) { return failConversion(val, columnType, typeString, BigDecimal.class, nfe); } - // TODO: should we implement numeric - interval types conversions too; ever needed? ODBC does mandate it - // https://docs.microsoft.com/en-us/sql/odbc/reference/appendixes/converting-data-from-c-to-sql-data-types + // TODO: should we implement numeric - interval types conversions too; ever needed? ODBC does mandate it + // https://docs.microsoft.com/en-us/sql/odbc/reference/appendixes/converting-data-from-c-to-sql-data-types } return failConversion(val, columnType, typeString, BigDecimal.class); } diff --git a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/TypeUtils.java b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/TypeUtils.java index a1ac2b45227a5..cde126d79b4a8 100644 --- a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/TypeUtils.java +++ b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/TypeUtils.java @@ -36,10 +36,17 @@ private TypeUtils() {} private static final Map ENUM_NAME_TO_TYPE; private static final Map SQL_TO_TYPE; - private static final Set SIGNED_TYPE = EnumSet.of(EsType.BYTE, - EsType.SHORT, EsType.INTEGER, EsType.LONG, - EsType.FLOAT, EsType.HALF_FLOAT, EsType.SCALED_FLOAT, EsType.DOUBLE, EsType.DATETIME); - + private static final Set SIGNED_TYPE = EnumSet.of( + EsType.BYTE, + EsType.SHORT, + EsType.INTEGER, + EsType.LONG, + EsType.FLOAT, + EsType.HALF_FLOAT, + EsType.SCALED_FLOAT, + EsType.DOUBLE, + EsType.DATETIME + ); static { Map, EsType> aMap = new LinkedHashMap<>(); @@ -99,7 +106,6 @@ private TypeUtils() {} TYPE_TO_CLASS = unmodifiableMap(types); - Map strings = new LinkedHashMap<>(); Map numbers = new LinkedHashMap<>(); diff --git a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/XContentSqlExtension.java b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/XContentSqlExtension.java index 28b67ac5008fc..c55f56559987a 100644 --- a/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/XContentSqlExtension.java +++ b/x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/XContentSqlExtension.java @@ -25,9 +25,7 @@ public class XContentSqlExtension implements XContentBuilderExtension { @Override public Map, XContentBuilder.Writer> getXContentWriters() { - return Map.of( - Date.class, (b, v) -> b.value(((Date) v).getTime()), - ZonedDateTime.class, (b, v) -> b.value(StringUtils.toString(v))); + return Map.of(Date.class, (b, v) -> b.value(((Date) v).getTime()), ZonedDateTime.class, (b, v) -> b.value(StringUtils.toString(v))); } @Override @@ -37,8 +35,6 @@ public Map, XContentBuilder.HumanReadableTransformer> getXContentHumanR @Override public Map, Function> getDateTransformers() { - return Map.of( - Date.class, d -> ((Date) d).getTime(), - ZonedDateTime.class, StringUtils::toString); + return Map.of(Date.class, d -> ((Date) d).getTime(), ZonedDateTime.class, StringUtils::toString); } } diff --git a/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/ColumnInfoTests.java b/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/ColumnInfoTests.java index fa2871cff147d..e18789edd8348 100644 --- a/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/ColumnInfoTests.java +++ b/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/ColumnInfoTests.java @@ -24,10 +24,11 @@ static JdbcColumnInfo doubleInfo(String name) { } public void testToString() { - assertEquals("test.doc.a", - new JdbcColumnInfo("a", EsType.KEYWORD, "test.doc", "as", "ads", "lab", 0).toString()); - assertEquals("test.doc.a", - new JdbcColumnInfo("a", EsType.KEYWORD, "test.doc", EMPTY, EMPTY, EMPTY, 0).toString()); + assertEquals( + "test.doc.a", + new JdbcColumnInfo("a", EsType.KEYWORD, "test.doc", "as", "ads", "lab", 0).toString() + ); + assertEquals("test.doc.a", new JdbcColumnInfo("a", EsType.KEYWORD, "test.doc", EMPTY, EMPTY, EMPTY, 0).toString()); assertEquals("string", varcharInfo("string").toString()); assertEquals("int", intInfo("int").toString()); assertEquals("d", doubleInfo("d").toString()); diff --git a/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/JdbcConfigurationDataSourceTests.java b/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/JdbcConfigurationDataSourceTests.java index 0ff5be89a3809..36aa192b10a60 100644 --- a/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/JdbcConfigurationDataSourceTests.java +++ b/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/JdbcConfigurationDataSourceTests.java @@ -25,8 +25,11 @@ public class JdbcConfigurationDataSourceTests extends WebServerTestCase { public void testDataSourceConfigurationWithSSLInURL() throws SQLException, URISyntaxException, IOException { - webServer().enqueue(new MockResponse().setResponseCode(200).addHeader("Content-Type", "application/json").setBody( - XContentHelper.toXContent(createCurrentVersionMainResponse(), XContentType.JSON, false).utf8ToString())); + webServer().enqueue( + new MockResponse().setResponseCode(200) + .addHeader("Content-Type", "application/json") + .setBody(XContentHelper.toXContent(createCurrentVersionMainResponse(), XContentType.JSON, false).utf8ToString()) + ); Map urlPropMap = JdbcConfigurationTests.sslProperties(); Properties allProps = new Properties(); @@ -59,17 +62,18 @@ public void testTimeoutsInUrl() throws IOException, SQLException { ); EsDataSource dataSource = new EsDataSource(); - String address = "jdbc:es://" + webServerAddress() - + "/?binary.format=false&query.timeout=" + queryTimeout - + "&page.timeout=" + pageTimeout; + String address = "jdbc:es://" + + webServerAddress() + + "/?binary.format=false&query.timeout=" + + queryTimeout + + "&page.timeout=" + + pageTimeout; dataSource.setUrl(address); Connection connection = dataSource.getConnection(); webServer().takeRequest(); webServer().enqueue( - new MockResponse().setResponseCode(200) - .addHeader("Content-Type", "application/json") - .setBody("{\"rows\":[],\"columns\":[]}") + new MockResponse().setResponseCode(200).addHeader("Content-Type", "application/json").setBody("{\"rows\":[],\"columns\":[]}") ); PreparedStatement statement = connection.prepareStatement("SELECT 1"); statement.execute(); diff --git a/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/JdbcConfigurationTests.java b/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/JdbcConfigurationTests.java index 612d2240d8c5b..a5c39eed02ece 100644 --- a/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/JdbcConfigurationTests.java +++ b/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/JdbcConfigurationTests.java @@ -43,9 +43,12 @@ private JdbcConfiguration ci(String url) throws SQLException { public void testInvalidUrl() { JdbcSQLException e = expectThrows(JdbcSQLException.class, () -> ci("jdbc:es://localhost9200/?ssl=#5#")); - assertEquals("Invalid URL: Invalid connection configuration: Illegal character in fragment at index 28: " - + "http://localhost9200/?ssl=#5#; format should be " - + "[jdbc:[es|elasticsearch]://[[http|https]://]?[host[:port]]?/[prefix]?[\\?[option=value]&]*]", e.getMessage()); + assertEquals( + "Invalid URL: Invalid connection configuration: Illegal character in fragment at index 28: " + + "http://localhost9200/?ssl=#5#; format should be " + + "[jdbc:[es|elasticsearch]://[[http|https]://]?[host[:port]]?/[prefix]?[\\?[option=value]&]*]", + e.getMessage() + ); } public void testJustThePrefix() throws Exception { @@ -179,10 +182,12 @@ public void testValidateProperties() { e = expectThrows(JdbcSQLException.class, () -> ci(jdbcPrefix() + "test:9200?&validate.properties=true&something=some_value")); assertEquals("Unknown parameter [something]; did you mean []", e.getMessage()); - Properties properties = new Properties(); + Properties properties = new Properties(); properties.setProperty(PROPERTIES_VALIDATION, "true"); - e = expectThrows(JdbcSQLException.class, - () -> JdbcConfiguration.create(jdbcPrefix() + "test:9200?something=some_value", properties, 0)); + e = expectThrows( + JdbcSQLException.class, + () -> JdbcConfiguration.create(jdbcPrefix() + "test:9200?something=some_value", properties, 0) + ); assertEquals("Unknown parameter [something]; did you mean []", e.getMessage()); } @@ -197,9 +202,19 @@ public void testNoPropertiesValidation() throws SQLException { long pageTimeout = randomNonNegativeLong(); int pageSize = randomIntBetween(0, Integer.MAX_VALUE); - ci = ci(jdbcPrefix() + "test:9200?validate.properties=false&something=some_value&query.timeout=" + queryTimeout - + "&connect.timeout=" + connectTimeout + "&network.timeout=" + networkTimeout + "&page.timeout=" + pageTimeout - + "&page.size=" + pageSize); + ci = ci( + jdbcPrefix() + + "test:9200?validate.properties=false&something=some_value&query.timeout=" + + queryTimeout + + "&connect.timeout=" + + connectTimeout + + "&network.timeout=" + + networkTimeout + + "&page.timeout=" + + pageTimeout + + "&page.size=" + + pageSize + ); assertEquals(false, ci.validateProperties()); assertEquals(queryTimeout, ci.queryTimeout()); assertEquals(connectTimeout, ci.connectTimeout()); @@ -208,7 +223,7 @@ public void testNoPropertiesValidation() throws SQLException { assertEquals(pageSize, ci.pageSize()); // Properties test - Properties properties = new Properties(); + Properties properties = new Properties(); properties.setProperty(PROPERTIES_VALIDATION, "false"); properties.put(QUERY_TIMEOUT, Long.toString(queryTimeout)); properties.put(PAGE_TIMEOUT, Long.toString(pageTimeout)); @@ -227,7 +242,7 @@ public void testNoPropertiesValidation() throws SQLException { } public void testTimoutOverride() throws Exception { - Properties properties = new Properties(); + Properties properties = new Properties(); properties.setProperty(CONNECT_TIMEOUT, "3"); // Should be overridden properties.setProperty(PAGE_TIMEOUT, "4"); @@ -322,7 +337,7 @@ public void testDriverConfigurationWithSSLInURL() { } } - public void testTyposInSslConfigInUrl(){ + public void testTyposInSslConfigInUrl() { assertJdbcSqlExceptionFromUrl("ssl.protocl", "ssl.protocol"); assertJdbcSqlExceptionFromUrl("sssl", "ssl"); assertJdbcSqlExceptionFromUrl("ssl.keystore.lction", "ssl.keystore.location"); @@ -380,8 +395,7 @@ private void assertJdbcSqlExceptionFromProperties(String wrongSetting, String co } private void assertJdbcSqlException(String wrongSetting, String correctSetting, String url, Properties props) { - JdbcSQLException ex = expectThrows(JdbcSQLException.class, - () -> JdbcConfiguration.create(url, props, 0)); + JdbcSQLException ex = expectThrows(JdbcSQLException.class, () -> JdbcConfiguration.create(url, props, 0)); assertEquals("Unknown parameter [" + wrongSetting + "]; did you mean [" + correctSetting + "]", ex.getMessage()); } } diff --git a/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/JdbcDatabaseMetaDataTests.java b/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/JdbcDatabaseMetaDataTests.java index fa51a590f99e2..8979913af9fa8 100644 --- a/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/JdbcDatabaseMetaDataTests.java +++ b/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/JdbcDatabaseMetaDataTests.java @@ -21,7 +21,8 @@ public class JdbcDatabaseMetaDataTests extends ESTestCase { { try { md = new JdbcDatabaseMetaData( - new JdbcConnection(JdbcConfiguration.create("jdbc:es://localhost:9200/", new Properties(), 10), false)); + new JdbcConnection(JdbcConfiguration.create("jdbc:es://localhost:9200/", new Properties(), 10), false) + ); } catch (Exception ex) { throw new RuntimeException(ex); } diff --git a/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/JdbcHttpClientRequestTests.java b/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/JdbcHttpClientRequestTests.java index 1dd0d99c9659e..af28adfe70c01 100644 --- a/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/JdbcHttpClientRequestTests.java +++ b/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/JdbcHttpClientRequestTests.java @@ -16,14 +16,14 @@ import org.apache.logging.log4j.message.ParameterizedMessage; import org.apache.logging.log4j.util.Supplier; import org.elasticsearch.common.Strings; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.Streams; import org.elasticsearch.common.util.concurrent.ConcurrentCollections; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.mocksocket.MockHttpServer; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.sql.client.ConnectionConfiguration; import org.junit.AfterClass; import org.junit.BeforeClass; @@ -78,8 +78,11 @@ private void assertBinaryRequest(boolean isBinary, XContentType xContentType) th prepareMockResponse(); try { - httpClient.query(randomAlphaOfLength(256), null, - new RequestMeta(randomIntBetween(1, 100), randomNonNegativeLong(), randomNonNegativeLong())); + httpClient.query( + randomAlphaOfLength(256), + null, + new RequestMeta(randomIntBetween(1, 100), randomNonNegativeLong(), randomNonNegativeLong()) + ); } catch (SQLException e) { logger.info("Ignored SQLException", e); } @@ -108,10 +111,9 @@ private void assertValues(boolean isBinary, XContentType xContentType) { } private void prepareMockResponse() { - webServer.enqueue(new Response() - .setResponseCode(200) - .addHeader("Content-Type", "application/json") - .setBody("{\"rows\":[],\"columns\":[]}")); + webServer.enqueue( + new Response().setResponseCode(200).addHeader("Content-Type", "application/json").setBody("{\"rows\":[],\"columns\":[]}") + ); } @SuppressForbidden(reason = "use http server") @@ -122,8 +124,7 @@ private static class RawRequestMockWebServer implements Closeable { private String hostname; private int port; - RawRequestMockWebServer() { - } + RawRequestMockWebServer() {} void start() throws IOException { InetSocketAddress address = new InetSocketAddress(InetAddress.getLoopbackAddress().getHostAddress(), 0); @@ -152,8 +153,14 @@ void start() throws IOException { } } } catch (Exception e) { - logger.error((Supplier) () -> new ParameterizedMessage("failed to respond to request [{} {}]", - s.getRequestMethod(), s.getRequestURI()), e); + logger.error( + (Supplier) () -> new ParameterizedMessage( + "failed to respond to request [{} {}]", + s.getRequestMethod(), + s.getRequestURI() + ), + e + ); } finally { s.close(); } diff --git a/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/JdbcPreparedStatementTests.java b/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/JdbcPreparedStatementTests.java index 31e17ecac19cb..fce0b19da5a33 100644 --- a/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/JdbcPreparedStatementTests.java +++ b/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/JdbcPreparedStatementTests.java @@ -198,8 +198,7 @@ public void testThrownExceptionsWhenSettingIntegerValues() throws SQLException { int someInt = randomInt(); SQLException sqle = expectThrows(SQLException.class, () -> jps.setObject(1, someInt, Types.TIMESTAMP)); - assertEquals(format(Locale.ROOT, "Unable to convert value [%.128s] of type [INTEGER] to [TIMESTAMP]", someInt), - sqle.getMessage()); + assertEquals(format(Locale.ROOT, "Unable to convert value [%.128s] of type [INTEGER] to [TIMESTAMP]", someInt), sqle.getMessage()); Integer randomIntNotShort = randomIntBetween(32768, Integer.MAX_VALUE); sqle = expectThrows(SQLException.class, () -> jps.setObject(1, randomIntNotShort, Types.SMALLINT)); @@ -240,8 +239,7 @@ public void testThrownExceptionsWhenSettingLongValues() throws SQLException { long someLong = randomLong(); SQLException sqle = expectThrows(SQLException.class, () -> jps.setObject(1, someLong, Types.TIMESTAMP)); - assertEquals(format(Locale.ROOT, "Unable to convert value [%.128s] of type [LONG] to [TIMESTAMP]", someLong), - sqle.getMessage()); + assertEquals(format(Locale.ROOT, "Unable to convert value [%.128s] of type [LONG] to [TIMESTAMP]", someLong), sqle.getMessage()); Long randomLongNotShort = randomLongBetween(Integer.MAX_VALUE + 1, Long.MAX_VALUE); sqle = expectThrows(SQLException.class, () -> jps.setObject(1, randomLongNotShort, Types.INTEGER)); @@ -282,17 +280,14 @@ public void testThrownExceptionsWhenSettingFloatValues() throws SQLException { float someFloat = randomFloat(); SQLException sqle = expectThrows(SQLException.class, () -> jps.setObject(1, someFloat, Types.TIMESTAMP)); - assertEquals(format(Locale.ROOT, "Unable to convert value [%.128s] of type [FLOAT] to [TIMESTAMP]", someFloat), - sqle.getMessage()); + assertEquals(format(Locale.ROOT, "Unable to convert value [%.128s] of type [FLOAT] to [TIMESTAMP]", someFloat), sqle.getMessage()); - Float floatNotInt = 5_155_000_000f; + Float floatNotInt = 5_155_000_000f; sqle = expectThrows(SQLException.class, () -> jps.setObject(1, floatNotInt, Types.INTEGER)); - assertEquals(LoggerMessageFormat.format("Numeric {} out of range", - Math.round(floatNotInt.doubleValue())), sqle.getMessage()); + assertEquals(LoggerMessageFormat.format("Numeric {} out of range", Math.round(floatNotInt.doubleValue())), sqle.getMessage()); sqle = expectThrows(SQLException.class, () -> jps.setObject(1, floatNotInt, Types.SMALLINT)); - assertEquals(LoggerMessageFormat.format("Numeric {} out of range", - Math.round(floatNotInt.doubleValue())), sqle.getMessage()); + assertEquals(LoggerMessageFormat.format("Numeric {} out of range", Math.round(floatNotInt.doubleValue())), sqle.getMessage()); } public void testSettingDoubleValues() throws SQLException { @@ -323,13 +318,13 @@ public void testThrownExceptionsWhenSettingDoubleValues() throws SQLException { SQLException sqle = expectThrows(SQLException.class, () -> jps.setObject(1, someDouble, Types.TIMESTAMP)); assertEquals( - format(Locale.ROOT, "Unable to convert value [%.128s] of type [DOUBLE] to [TIMESTAMP]", someDouble), - sqle.getMessage()); + format(Locale.ROOT, "Unable to convert value [%.128s] of type [DOUBLE] to [TIMESTAMP]", someDouble), + sqle.getMessage() + ); Double doubleNotInt = 5_155_000_000d; sqle = expectThrows(SQLException.class, () -> jps.setObject(1, doubleNotInt, Types.INTEGER)); - assertEquals(LoggerMessageFormat.format("Numeric {} out of range", - ((Number) doubleNotInt).longValue()), sqle.getMessage()); + assertEquals(LoggerMessageFormat.format("Numeric {} out of range", ((Number) doubleNotInt).longValue()), sqle.getMessage()); } public void testUnsupportedClasses() throws SQLException { @@ -339,10 +334,12 @@ public void testUnsupportedClasses() throws SQLException { public String getSQLTypeName() throws SQLException { return null; } + @Override public Object[] getAttributes(Map> map) throws SQLException { return null; } + @Override public Object[] getAttributes() throws SQLException { return null; @@ -362,8 +359,10 @@ public Object[] getAttributes() throws SQLException { SQLException se = expectThrows(SQLException.class, () -> jps.setObject(1, this, 1_000_000)); assertEquals("Unsupported SQL type [1000000]", se.getMessage()); - SQLFeatureNotSupportedException iae = expectThrows(SQLFeatureNotSupportedException.class, - () -> jps.setObject(1, randomShort(), Types.CHAR)); + SQLFeatureNotSupportedException iae = expectThrows( + SQLFeatureNotSupportedException.class, + () -> jps.setObject(1, randomShort(), Types.CHAR) + ); assertEquals("Unsupported SQL type [CHAR]", iae.getMessage()); } @@ -421,7 +420,7 @@ public void testSettingTimeValues() throws SQLException { Calendar nonDefaultCal = randomCalendar(); jps.setTime(1, time, nonDefaultCal); - assertEquals(4675000, convertFromUTCtoCalendar(((Date)value(jps)), nonDefaultCal)); + assertEquals(4675000, convertFromUTCtoCalendar(((Date) value(jps)), nonDefaultCal)); assertEquals(TIME, jdbcType(jps)); assertTrue(value(jps) instanceof java.util.Date); @@ -443,13 +442,13 @@ public void testSettingSqlDateValues() throws SQLException { java.sql.Date someSqlDate = new java.sql.Date(randomLong()); jps.setDate(1, someSqlDate); - assertEquals(someSqlDate.getTime(), ((Date)value(jps)).getTime()); + assertEquals(someSqlDate.getTime(), ((Date) value(jps)).getTime()); assertEquals(DATETIME, jdbcType(jps)); someSqlDate = new java.sql.Date(randomLong()); Calendar nonDefaultCal = randomCalendar(); jps.setDate(1, someSqlDate, nonDefaultCal); - assertEquals(someSqlDate.getTime(), convertFromUTCtoCalendar(((Date)value(jps)), nonDefaultCal)); + assertEquals(someSqlDate.getTime(), convertFromUTCtoCalendar(((Date) value(jps)), nonDefaultCal)); assertEquals(DATETIME, jdbcType(jps)); assertTrue(value(jps) instanceof java.util.Date); @@ -462,8 +461,10 @@ public void testThrownExceptionsWhenSettingSqlDateValues() throws SQLException { JdbcPreparedStatement jps = createJdbcPreparedStatement(); java.sql.Date someSqlDate = new java.sql.Date(randomLong()); - SQLException sqle = expectThrows(SQLFeatureNotSupportedException.class, - () -> jps.setObject(1, new java.sql.Date(randomLong()), Types.DOUBLE)); + SQLException sqle = expectThrows( + SQLFeatureNotSupportedException.class, + () -> jps.setObject(1, new java.sql.Date(randomLong()), Types.DOUBLE) + ); assertEquals("Conversion from type [" + someSqlDate.getClass().getName() + "] to [DOUBLE] not supported", sqle.getMessage()); } @@ -614,8 +615,6 @@ private long convertFromUTCtoCalendar(Date date, Calendar nonDefaultCal) { * the values correctly to UTC. */ private long convertFromUTCtoCalendar(ZonedDateTime zdt, Calendar nonDefaultCal) { - return zdt.withZoneSameInstant(UTC) - .withZoneSameLocal(nonDefaultCal.getTimeZone().toZoneId()) - .toInstant().toEpochMilli(); + return zdt.withZoneSameInstant(UTC).withZoneSameLocal(nonDefaultCal.getTimeZone().toZoneId()).toInstant().toEpochMilli(); } } diff --git a/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/JdbcResultSetMetaDataTests.java b/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/JdbcResultSetMetaDataTests.java index 761919aa832e5..bfc45a713b8cf 100644 --- a/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/JdbcResultSetMetaDataTests.java +++ b/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/JdbcResultSetMetaDataTests.java @@ -19,11 +19,11 @@ public class JdbcResultSetMetaDataTests extends ESTestCase { private final List columns = Arrays.asList( - new JdbcColumnInfo("test_keyword", EsType.KEYWORD, EMPTY, EMPTY, EMPTY, EMPTY, 0), - new JdbcColumnInfo("test_integer", EsType.INTEGER, EMPTY, EMPTY, EMPTY, EMPTY, 11), - new JdbcColumnInfo("test_double", EsType.DOUBLE, EMPTY, EMPTY, EMPTY, EMPTY, 25), - new JdbcColumnInfo("test_long", EsType.LONG, "test_table", "test", "schema", "custom_label", 20) - ); + new JdbcColumnInfo("test_keyword", EsType.KEYWORD, EMPTY, EMPTY, EMPTY, EMPTY, 0), + new JdbcColumnInfo("test_integer", EsType.INTEGER, EMPTY, EMPTY, EMPTY, EMPTY, 11), + new JdbcColumnInfo("test_double", EsType.DOUBLE, EMPTY, EMPTY, EMPTY, EMPTY, 25), + new JdbcColumnInfo("test_long", EsType.LONG, "test_table", "test", "schema", "custom_label", 20) + ); private final JdbcResultSetMetaData metaData = new JdbcResultSetMetaData(null, columns); public void testColumnsProperties() throws SQLException { diff --git a/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/SqlQueryParameterAnalyzerTests.java b/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/SqlQueryParameterAnalyzerTests.java index e358cd26e78b6..d2b63303b5f4c 100644 --- a/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/SqlQueryParameterAnalyzerTests.java +++ b/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/SqlQueryParameterAnalyzerTests.java @@ -23,14 +23,15 @@ public void testNoParameters() throws Exception { } - public void testSingleParameter() throws Exception { assertEquals(1, SqlQueryParameterAnalyzer.parametersCount("SELECT * FROM 'table' WHERE s = '?' AND b = ?")); assertEquals(1, SqlQueryParameterAnalyzer.parametersCount("SELECT * FROM 'table' WHERE b = ? AND s = '?'")); - assertEquals(1, SqlQueryParameterAnalyzer.parametersCount("SELECT ?/10 /* multiline \n" + - " * query \n" + - " * more ? /* lines */ ? here \n" + - " */ FROM foo")); + assertEquals( + 1, + SqlQueryParameterAnalyzer.parametersCount( + "SELECT ?/10 /* multiline \n" + " * query \n" + " * more ? /* lines */ ? here \n" + " */ FROM foo" + ) + ); assertEquals(1, SqlQueryParameterAnalyzer.parametersCount("SELECT ?")); } @@ -39,9 +40,14 @@ public void testMultipleParameters() throws Exception { assertEquals(4, SqlQueryParameterAnalyzer.parametersCount("SELECT ?, ?, ? , ?")); assertEquals(3, SqlQueryParameterAnalyzer.parametersCount("SELECT ?, ?, '?' , ?")); assertEquals(3, SqlQueryParameterAnalyzer.parametersCount("SELECT ?, ?\n, '?' , ?")); - assertEquals(3, SqlQueryParameterAnalyzer.parametersCount("SELECT ? - 10 -- first parameter with ????\n" + - ", ? -- second parameter with random \" and ' \n" + - ", ? -- last parameter without new line")); + assertEquals( + 3, + SqlQueryParameterAnalyzer.parametersCount( + "SELECT ? - 10 -- first parameter with ????\n" + + ", ? -- second parameter with random \" and ' \n" + + ", ? -- last parameter without new line" + ) + ); } public void testUnclosedJdbcEscape() { diff --git a/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/TypeConverterTests.java b/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/TypeConverterTests.java index 10cc03ab236cd..137d9e4dacc33 100644 --- a/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/TypeConverterTests.java +++ b/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/TypeConverterTests.java @@ -7,10 +7,10 @@ package org.elasticsearch.xpack.sql.jdbc; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.json.JsonXContent; import java.sql.Date; import java.sql.Timestamp; @@ -20,7 +20,6 @@ import static org.elasticsearch.xpack.sql.jdbc.JdbcTestUtils.nowWithMillisResolution; import static org.hamcrest.Matchers.instanceOf; - public class TypeConverterTests extends ESTestCase { private static final ZoneId UTC = ZoneId.of("Z"); diff --git a/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/VersionParityTests.java b/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/VersionParityTests.java index 2657aa8d2f34a..4713d6dcba0f7 100644 --- a/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/VersionParityTests.java +++ b/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/VersionParityTests.java @@ -10,9 +10,9 @@ import org.elasticsearch.Version; import org.elasticsearch.action.main.MainResponse; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.test.VersionUtils; import org.elasticsearch.test.http.MockResponse; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.sql.client.ClientVersion; import org.elasticsearch.xpack.sql.proto.SqlVersion; @@ -39,11 +39,18 @@ public void testExceptionThrownOnIncompatibleVersions() throws IOException, SQLE // Client's version is wired up to patch level, excluding the qualifier => generate the test version as the server does it. String versionString = SqlVersion.fromString(version.toString()).toString(); - SQLException ex = expectThrows(SQLException.class, () -> new JdbcHttpClient(new JdbcConnection( - JdbcConfiguration.create(url, null, 0), false))); - assertEquals("This version of the JDBC driver is only compatible with Elasticsearch version " + - ClientVersion.CURRENT.majorMinorToString() + " or newer; attempting to connect to a server " + - "version " + versionString, ex.getMessage()); + SQLException ex = expectThrows( + SQLException.class, + () -> new JdbcHttpClient(new JdbcConnection(JdbcConfiguration.create(url, null, 0), false)) + ); + assertEquals( + "This version of the JDBC driver is only compatible with Elasticsearch version " + + ClientVersion.CURRENT.majorMinorToString() + + " or newer; attempting to connect to a server " + + "version " + + versionString, + ex.getMessage() + ); } while (version.compareTo(firstVersion) > 0); } @@ -63,7 +70,10 @@ public void testNoExceptionThrownForCompatibleVersions() throws IOException { void prepareResponse(Version version) throws IOException { MainResponse response = version == null ? createCurrentVersionMainResponse() : createMainResponse(version); - webServer().enqueue(new MockResponse().setResponseCode(200).addHeader("Content-Type", "application/json").setBody( - XContentHelper.toXContent(response, XContentType.JSON, false).utf8ToString())); + webServer().enqueue( + new MockResponse().setResponseCode(200) + .addHeader("Content-Type", "application/json") + .setBody(XContentHelper.toXContent(response, XContentType.JSON, false).utf8ToString()) + ); } } diff --git a/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/WebServerTestCase.java b/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/WebServerTestCase.java index 1f62d45c397d7..3a2922987bb1b 100644 --- a/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/WebServerTestCase.java +++ b/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/WebServerTestCase.java @@ -49,10 +49,14 @@ MainResponse createMainResponse(Version version) { String nodeName = randomAlphaOfLength(10); final String date = new Date(randomNonNegativeLong()).toString(); Build build = new Build( - Build.Flavor.UNKNOWN, Build.Type.UNKNOWN, randomAlphaOfLength(8), date, randomBoolean(), + Build.Flavor.UNKNOWN, + Build.Type.UNKNOWN, + randomAlphaOfLength(8), + date, + randomBoolean(), version.toString() ); - return new MainResponse(nodeName, version, clusterName, clusterUuid , build); + return new MainResponse(nodeName, version, clusterName, clusterUuid, build); } String webServerAddress() { diff --git a/x-pack/plugin/sql/qa/jdbc/security/src/test/java/org/elasticsearch/xpack/sql/qa/jdbc/security/JdbcConnectionIT.java b/x-pack/plugin/sql/qa/jdbc/security/src/test/java/org/elasticsearch/xpack/sql/qa/jdbc/security/JdbcConnectionIT.java index b53b97250fdad..701bb7917a635 100644 --- a/x-pack/plugin/sql/qa/jdbc/security/src/test/java/org/elasticsearch/xpack/sql/qa/jdbc/security/JdbcConnectionIT.java +++ b/x-pack/plugin/sql/qa/jdbc/security/src/test/java/org/elasticsearch/xpack/sql/qa/jdbc/security/JdbcConnectionIT.java @@ -6,11 +6,11 @@ */ package org.elasticsearch.xpack.sql.qa.jdbc.security; -import org.elasticsearch.core.Booleans; -import org.elasticsearch.core.PathUtils; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.Booleans; +import org.elasticsearch.core.PathUtils; import org.elasticsearch.test.rest.ESRestTestCase; import org.elasticsearch.xpack.sql.qa.jdbc.ConnectionTestCase; diff --git a/x-pack/plugin/sql/qa/jdbc/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/JdbcTestUtils.java b/x-pack/plugin/sql/qa/jdbc/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/JdbcTestUtils.java index 66af539bacb51..7d11bfb113bad 100644 --- a/x-pack/plugin/sql/qa/jdbc/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/JdbcTestUtils.java +++ b/x-pack/plugin/sql/qa/jdbc/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/JdbcTestUtils.java @@ -7,7 +7,6 @@ package org.elasticsearch.xpack.sql.qa.jdbc; import org.elasticsearch.Version; -import org.elasticsearch.common.time.DateUtils; import org.elasticsearch.xpack.sql.proto.StringUtils; import java.sql.Date; diff --git a/x-pack/plugin/sql/qa/jdbc/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/PreparedStatementTestCase.java b/x-pack/plugin/sql/qa/jdbc/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/PreparedStatementTestCase.java index f4c78bd42f4f8..5bee5fd209ce9 100644 --- a/x-pack/plugin/sql/qa/jdbc/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/PreparedStatementTestCase.java +++ b/x-pack/plugin/sql/qa/jdbc/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/PreparedStatementTestCase.java @@ -6,9 +6,9 @@ */ package org.elasticsearch.xpack.sql.qa.jdbc; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.time.DateUtils; +import org.elasticsearch.core.Tuple; import java.io.IOException; import java.math.BigDecimal; @@ -155,16 +155,19 @@ public void testDatetime() throws IOException, SQLException { } public void testDatetimeWithNanos() throws IOException, SQLException { - assumeTrue("Driver version [" + JDBC_DRIVER_VERSION + "] doesn't support DATETIME with nanosecond resolution]", - versionSupportsDateNanos()); + assumeTrue( + "Driver version [" + JDBC_DRIVER_VERSION + "] doesn't support DATETIME with nanosecond resolution]", + versionSupportsDateNanos() + ); long randomTimestampWitnNanos = randomTimeInNanos(); int randomNanosOnly = extractNanosOnly(randomTimestampWitnNanos); setupIndexForDateTimeTestsWithNanos(randomTimestampWitnNanos); try (Connection connection = esJdbc()) { - try (PreparedStatement statement = connection.prepareStatement( - "SELECT id, test_date_nanos FROM emps WHERE test_date_nanos = ?")) { + try ( + PreparedStatement statement = connection.prepareStatement("SELECT id, test_date_nanos FROM emps WHERE test_date_nanos = ?") + ) { Timestamp ts = new Timestamp(toMilliSeconds(randomTimestampWitnNanos)); statement.setObject(1, ts); try (ResultSet results = statement.executeQuery()) { @@ -184,16 +187,19 @@ public void testDatetimeWithNanos() throws IOException, SQLException { } public void testDateTimeWithNanosAgainstDriverWithoutSupport() throws IOException, SQLException { - assumeFalse("Driver version [" + JDBC_DRIVER_VERSION + "] doesn't support DATETIME with nanosecond resolution]", - versionSupportsDateNanos()); + assumeFalse( + "Driver version [" + JDBC_DRIVER_VERSION + "] doesn't support DATETIME with nanosecond resolution]", + versionSupportsDateNanos() + ); long randomTimestampWitnNanos = randomTimeInNanos(); int randomNanosOnly = extractNanosOnly(randomTimestampWitnNanos); setupIndexForDateTimeTestsWithNanos(randomTimestampWitnNanos); try (Connection connection = esJdbc()) { - try (PreparedStatement statement = connection.prepareStatement( - "SELECT id, test_date_nanos FROM emps WHERE test_date_nanos = ?")) { + try ( + PreparedStatement statement = connection.prepareStatement("SELECT id, test_date_nanos FROM emps WHERE test_date_nanos = ?") + ) { Timestamp ts = new Timestamp(toMilliSeconds(randomTimestampWitnNanos)); statement.setObject(1, ts); try (ResultSet results = statement.executeQuery()) { diff --git a/x-pack/plugin/sql/qa/jdbc/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/ResultSetTestCase.java b/x-pack/plugin/sql/qa/jdbc/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/ResultSetTestCase.java index fddf02096b67b..ca3b8216cf1b1 100644 --- a/x-pack/plugin/sql/qa/jdbc/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/ResultSetTestCase.java +++ b/x-pack/plugin/sql/qa/jdbc/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/ResultSetTestCase.java @@ -95,7 +95,7 @@ public abstract class ResultSetTestCase extends JdbcIntegrationTestCase { ).collect(Collectors.toCollection(HashSet::new)); static final Map, SQLType> dateTimeTestingFields = new HashMap<>(); static final String SELECT_ALL_FIELDS = "SELECT test_boolean, test_byte, test_integer," - + "test_long, test_short, test_double, test_float, test_keyword, test_date, test_date_nanos FROM test"; + + "test_long, test_short, test_double, test_float, test_keyword, test_date, test_date_nanos FROM test"; static final String SELECT_WILDCARD = "SELECT * FROM test"; static { dateTimeTestingFields.put(new Tuple<>("test_boolean", true), EsType.BOOLEAN); @@ -701,11 +701,7 @@ public void testGettingValidDoubleWithCasting() throws IOException, SQLException for (Entry e : map.entrySet()) { if (e.getValue() instanceof Float) { assertEquals("For field " + e.getKey(), e.getValue(), Double.valueOf(results.getDouble(e.getKey())).floatValue()); - assertEquals( - "For field " + e.getKey(), - e.getValue(), - results.getObject(e.getKey(), Double.class).floatValue() - ); + assertEquals("For field " + e.getKey(), e.getValue(), results.getObject(e.getKey(), Double.class).floatValue()); } else { assertEquals("For field " + e.getKey(), e.getValue().doubleValue(), results.getDouble(e.getKey()), 0.0d); assertEquals("For field " + e.getKey(), e.getValue().doubleValue(), results.getObject(e.getKey(), Double.class), 0.0d); @@ -930,54 +926,54 @@ public void testGettingValidBigDecimalFromBooleanWithoutCasting() throws IOExcep public void testGettingValidBigDecimalFromByteWithoutCasting() throws IOException, SQLException { List byteTestValues = createTestDataForNumericValueTests(ESTestCase::randomByte); doWithQuery( - "SELECT test_byte, test_null_byte, test_keyword FROM test", - byteTestValues, - ResultSetTestCase::validateBigDecimalWithoutCasting + "SELECT test_byte, test_null_byte, test_keyword FROM test", + byteTestValues, + ResultSetTestCase::validateBigDecimalWithoutCasting ); } public void testGettingValidBigDecimalFromShortWithoutCasting() throws IOException, SQLException { List shortTestValues = createTestDataForNumericValueTests(ESTestCase::randomShort); doWithQuery( - "SELECT test_short, test_null_short, test_keyword FROM test", - shortTestValues, - ResultSetTestCase::validateBigDecimalWithoutCasting + "SELECT test_short, test_null_short, test_keyword FROM test", + shortTestValues, + ResultSetTestCase::validateBigDecimalWithoutCasting ); } public void testGettingValidBigDecimalFromIntegerWithoutCasting() throws IOException, SQLException { List integerTestValues = createTestDataForNumericValueTests(ESTestCase::randomInt); doWithQuery( - "SELECT test_integer, test_null_integer, test_keyword FROM test", - integerTestValues, - ResultSetTestCase::validateBigDecimalWithoutCasting + "SELECT test_integer, test_null_integer, test_keyword FROM test", + integerTestValues, + ResultSetTestCase::validateBigDecimalWithoutCasting ); } public void testGettingValidBigDecimalFromLongWithoutCasting() throws IOException, SQLException { List longTestValues = createTestDataForNumericValueTests(ESTestCase::randomLong); doWithQuery( - "SELECT test_long, test_null_long, test_keyword FROM test", - longTestValues, - ResultSetTestCase::validateBigDecimalWithoutCasting + "SELECT test_long, test_null_long, test_keyword FROM test", + longTestValues, + ResultSetTestCase::validateBigDecimalWithoutCasting ); } public void testGettingValidBigDecimalFromFloatWithoutCasting() throws IOException, SQLException { List floatTestValues = createTestDataForNumericValueTests(ESTestCase::randomFloat); doWithQuery( - "SELECT test_float, test_null_float, test_keyword FROM test", - floatTestValues, - ResultSetTestCase::validateBigDecimalWithoutCasting + "SELECT test_float, test_null_float, test_keyword FROM test", + floatTestValues, + ResultSetTestCase::validateBigDecimalWithoutCasting ); } public void testGettingValidBigDecimalFromDoubleWithoutCasting() throws IOException, SQLException { List doubleTestValues = createTestDataForNumericValueTests(ESTestCase::randomDouble); doWithQuery( - "SELECT test_double, test_null_double, test_keyword FROM test", - doubleTestValues, - ResultSetTestCase::validateBigDecimalWithoutCasting + "SELECT test_double, test_null_double, test_keyword FROM test", + doubleTestValues, + ResultSetTestCase::validateBigDecimalWithoutCasting ); } @@ -1026,13 +1022,13 @@ public void testGettingInvalidBigDecimal() throws IOException, SQLException { SQLException sqle = expectThrows(SQLException.class, () -> results.getBigDecimal("test_keyword")); assertEquals( - format(Locale.ROOT, "Unable to convert value [%.128s] of type [KEYWORD] to [BigDecimal]", randomString), - sqle.getMessage() + format(Locale.ROOT, "Unable to convert value [%.128s] of type [KEYWORD] to [BigDecimal]", randomString), + sqle.getMessage() ); sqle = expectThrows(SQLException.class, () -> results.getObject("test_keyword", BigDecimal.class)); assertEquals( - format(Locale.ROOT, "Unable to convert value [%.128s] of type [KEYWORD] to [BigDecimal]", randomString), - sqle.getMessage() + format(Locale.ROOT, "Unable to convert value [%.128s] of type [KEYWORD] to [BigDecimal]", randomString), + sqle.getMessage() ); sqle = expectThrows(SQLException.class, () -> results.getBigDecimal("test_date")); @@ -1184,8 +1180,10 @@ public void testGettingDateWithoutCalendar() throws Exception { } public void testGettingDateWithoutCalendarWithNanos() throws Exception { - assumeTrue("Driver version [" + JDBC_DRIVER_VERSION + "] doesn't support DATETIME with nanosecond resolution]", - versionSupportsDateNanos()); + assumeTrue( + "Driver version [" + JDBC_DRIVER_VERSION + "] doesn't support DATETIME with nanosecond resolution]", + versionSupportsDateNanos() + ); long randomLongDate = randomMillisUpToYear9999(); long randomLongDateNanos = randomTimeInNanos(); setupDataForDateTimeTests(randomLongDate, randomLongDateNanos); @@ -1239,8 +1237,10 @@ public void testGettingDateWithCalendar() throws Exception { } public void testGettingDateWithCalendarWithNanos() throws Exception { - assumeTrue("Driver version [" + JDBC_DRIVER_VERSION + "] doesn't support DATETIME with nanosecond resolution]", - versionSupportsDateNanos()); + assumeTrue( + "Driver version [" + JDBC_DRIVER_VERSION + "] doesn't support DATETIME with nanosecond resolution]", + versionSupportsDateNanos() + ); long randomLongDate = randomMillisUpToYear9999(); long randomLongDateNanos = randomTimeInNanos(); setupDataForDateTimeTests(randomLongDate, randomLongDateNanos); @@ -1292,8 +1292,10 @@ public void testGettingTimeWithoutCalendar() throws Exception { } public void testGettingTimeWithoutCalendarWithNanos() throws Exception { - assumeTrue("Driver version [" + JDBC_DRIVER_VERSION + "] doesn't support DATETIME with nanosecond resolution]", - versionSupportsDateNanos()); + assumeTrue( + "Driver version [" + JDBC_DRIVER_VERSION + "] doesn't support DATETIME with nanosecond resolution]", + versionSupportsDateNanos() + ); long randomLongDate = randomMillisUpToYear9999(); long randomLongDateNanos = randomTimeInNanos(); setupDataForDateTimeTests(randomLongDate, randomLongDateNanos); @@ -1345,8 +1347,10 @@ public void testGettingTimeWithCalendar() throws Exception { } public void testGettingTimeWithCalendarWithNanos() throws Exception { - assumeTrue("Driver version [" + JDBC_DRIVER_VERSION + "] doesn't support DATETIME with nanosecond resolution]", - versionSupportsDateNanos()); + assumeTrue( + "Driver version [" + JDBC_DRIVER_VERSION + "] doesn't support DATETIME with nanosecond resolution]", + versionSupportsDateNanos() + ); long randomLongDate = randomMillisUpToYear9999(); long randomLongDateNanos = randomTimeInNanos(); setupDataForDateTimeTests(randomLongDate, randomLongDateNanos); @@ -1396,8 +1400,10 @@ public void testGettingTimestampWithoutCalendar() throws Exception { } public void testGettingTimestampWithoutCalendarWithNanos() throws Exception { - assumeTrue("Driver version [" + JDBC_DRIVER_VERSION + "] doesn't support DATETIME with nanosecond resolution]", - versionSupportsDateNanos()); + assumeTrue( + "Driver version [" + JDBC_DRIVER_VERSION + "] doesn't support DATETIME with nanosecond resolution]", + versionSupportsDateNanos() + ); long randomLongDate = randomMillisUpToYear9999(); long randomLongDateNanos = randomTimeInNanos(); setupDataForDateTimeTests(randomLongDate, randomLongDateNanos); @@ -1418,8 +1424,10 @@ public void testGettingTimestampWithoutCalendarWithNanos() throws Exception { } public void testGettingTimestampWithoutCalendarWithNanosAgainstDriverWithoutSupport() throws Exception { - assumeFalse("Driver version [" + JDBC_DRIVER_VERSION + "] supports DATETIME with nanosecond resolution]", - versionSupportsDateNanos()); + assumeFalse( + "Driver version [" + JDBC_DRIVER_VERSION + "] supports DATETIME with nanosecond resolution]", + versionSupportsDateNanos() + ); long randomLongDate = randomMillisUpToYear9999(); long randomLongDateNanos = randomTimeInNanos(); setupDataForDateTimeTests(randomLongDate, randomLongDateNanos); @@ -1456,8 +1464,10 @@ public void testGettingTimestampWithCalendar() throws IOException, SQLException } public void testGettingTimestampWithCalendar_DateNanos() throws IOException, SQLException { - assumeTrue("Driver version [" + JDBC_DRIVER_VERSION + "] doesn't support DATETIME with nanosecond resolution]", - versionSupportsDateNanos()); + assumeTrue( + "Driver version [" + JDBC_DRIVER_VERSION + "] doesn't support DATETIME with nanosecond resolution]", + versionSupportsDateNanos() + ); long randomLongDate = randomMillisUpToYear9999(); long randomLongDateNanos = randomTimeInNanos(); setupDataForDateTimeTests(randomLongDate, randomLongDateNanos); @@ -1541,8 +1551,10 @@ public void testScalarOnDates() throws IOException, SQLException { } public void testScalarOnDates_DateNanos() throws IOException, SQLException { - assumeTrue("Driver version [" + JDBC_DRIVER_VERSION + "] doesn't support DATETIME with nanosecond resolution]", - versionSupportsDateNanos()); + assumeTrue( + "Driver version [" + JDBC_DRIVER_VERSION + "] doesn't support DATETIME with nanosecond resolution]", + versionSupportsDateNanos() + ); createIndex("test"); updateMapping("test", builder -> builder.startObject("test_date_nanos").field("type", "date_nanos").endObject()); @@ -2021,18 +2033,18 @@ private void doWithQuery(CheckedSupplier con, String q } private void doWithQuery( - String query, - List testValues, - CheckedBiConsumer, SQLException> biConsumer + String query, + List testValues, + CheckedBiConsumer, SQLException> biConsumer ) throws SQLException { doWithQuery(() -> esJdbc(timeZoneId), query, testValues, biConsumer); } private void doWithQuery( - CheckedSupplier con, - String query, - List testValues, - CheckedBiConsumer, SQLException> biConsumer + CheckedSupplier con, + String query, + List testValues, + CheckedBiConsumer, SQLException> biConsumer ) throws SQLException { try (Connection connection = con.get()) { try (PreparedStatement statement = connection.prepareStatement(query)) { @@ -2269,8 +2281,9 @@ private void assertErrorMessageForDateTimeValues(Exception ex, Class expected } private void assertErrorMessageForDateTimeValues(Exception ex, Class expectedType, long epochMillis, Integer nanos) { - Pattern expectedPattern = compile(quote("Unable to convert value [") + "(?.*?)" - + quote("] of type [DATETIME] to [" + expectedType.getSimpleName() + "]")); + Pattern expectedPattern = compile( + quote("Unable to convert value [") + "(?.*?)" + quote("] of type [DATETIME] to [" + expectedType.getSimpleName() + "]") + ); Matcher matcher = expectedPattern.matcher(ex.getMessage()); assertTrue(matcher.matches()); OffsetDateTime odt = OffsetDateTime.parse(matcher.group("instant")); @@ -2285,8 +2298,7 @@ private void validateErrorsForDateTimeTestsWithoutCalendar(CheckedFunction, SQLType> field : dateTimeTestingFields.entrySet()) { sqle = expectThrows(SQLException.class, () -> method.apply(field.getKey().v1())); assertEquals( - format(Locale.ROOT, "Unable to convert value [%.128s] of type [%s] to a " + type, - field.getKey().v2(), field.getValue()), + format(Locale.ROOT, "Unable to convert value [%.128s] of type [%s] to a " + type, field.getKey().v2(), field.getValue()), sqle.getMessage() ); } @@ -2297,10 +2309,15 @@ private void validateErrorsForDateTimeTestsWithCalendar(Calendar c, CheckedBiFun for (Entry, SQLType> field : dateTimeTestingFields.entrySet()) { sqle = expectThrows(SQLException.class, () -> method.apply(field.getKey().v1(), c)); assertThat( - sqle.getMessage(), - matchesPattern( - format(Locale.ROOT, "Unable to convert value \\[%.128s\\] of type \\[%s\\] to a (Long|Timestamp)", - field.getKey().v2(), field.getValue())) + sqle.getMessage(), + matchesPattern( + format( + Locale.ROOT, + "Unable to convert value \\[%.128s\\] of type \\[%s\\] to a (Long|Timestamp)", + field.getKey().v2(), + field.getValue() + ) + ) ); } } diff --git a/x-pack/plugin/sql/qa/mixed-node/src/test/java/org/elasticsearch/xpack/sql/qa/mixed_node/SqlCompatIT.java b/x-pack/plugin/sql/qa/mixed-node/src/test/java/org/elasticsearch/xpack/sql/qa/mixed_node/SqlCompatIT.java index cd9dd1d8fcc8a..434b656850809 100644 --- a/x-pack/plugin/sql/qa/mixed-node/src/test/java/org/elasticsearch/xpack/sql/qa/mixed_node/SqlCompatIT.java +++ b/x-pack/plugin/sql/qa/mixed-node/src/test/java/org/elasticsearch/xpack/sql/qa/mixed_node/SqlCompatIT.java @@ -13,11 +13,11 @@ import org.elasticsearch.client.Response; import org.elasticsearch.client.RestClient; import org.elasticsearch.common.Strings; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.xpack.ql.TestNode; import org.elasticsearch.xpack.ql.TestNodes; import org.elasticsearch.xpack.sql.qa.rest.BaseRestSqlTestCase; diff --git a/x-pack/plugin/sql/qa/mixed-node/src/test/java/org/elasticsearch/xpack/sql/qa/mixed_node/SqlSearchIT.java b/x-pack/plugin/sql/qa/mixed-node/src/test/java/org/elasticsearch/xpack/sql/qa/mixed_node/SqlSearchIT.java index 02074ad9119f9..634f033fd5580 100644 --- a/x-pack/plugin/sql/qa/mixed-node/src/test/java/org/elasticsearch/xpack/sql/qa/mixed_node/SqlSearchIT.java +++ b/x-pack/plugin/sql/qa/mixed-node/src/test/java/org/elasticsearch/xpack/sql/qa/mixed_node/SqlSearchIT.java @@ -16,9 +16,9 @@ import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.test.NotEqualMessageBuilder; import org.elasticsearch.test.rest.ESRestTestCase; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.ql.TestNode; import org.elasticsearch.xpack.ql.TestNodes; import org.junit.After; @@ -86,89 +86,85 @@ public void cleanUpIndex() throws IOException { } public void testAllTypesWithRequestToOldNodes() throws Exception { - Map expectedResponse = prepareTestData( - columns -> { - columns.add(columnInfo("geo_point_field", "geo_point")); - columns.add(columnInfo("float_field", "float")); - columns.add(columnInfo("half_float_field", "half_float")); - }, - (builder, fieldValues) -> { - Float randomFloat = randomFloat(); - // before "fields" API being added to QL, numbers were re-parsed from _source with a similar approach to - // indexing docvalues and for floating point numbers this may be different from the actual value passed in the _source - // floats were indexed as Doubles and the values returned had a greater precision and more decimals - builder.append(","); - if (isBwcNodeBeforeFieldsApiInQL) { - builder.append("\"geo_point_field\":{\"lat\":\"37.386483\", \"lon\":\"-122.083843\"},"); - fieldValues.put("geo_point_field", "POINT (-122.08384302444756 37.38648299127817)"); - builder.append("\"float_field\":" + randomFloat + ","); - fieldValues.put("float_field", Double.valueOf(randomFloat)); - builder.append("\"half_float_field\":123.456"); - fieldValues.put("half_float_field", 123.45600128173828d); - } else { - builder.append("\"geo_point_field\":{\"lat\":\"37.386483\", \"lon\":\"-122.083843\"},"); - fieldValues.put("geo_point_field", "POINT (-122.083843 37.386483)"); - builder.append("\"float_field\":" + randomFloat + ","); - /* - * Double.valueOf(float.toString) gets a `double` representing - * the `float` that we'd get by going through json which is - * base 10. just casting the `float` to a `double` will get - * a lower number with a lot more trailing digits because - * the cast adds *binary* 0s to the end. And those binary - * 0s don't translate the same as json's decimal 0s. - */ - fieldValues.put("float_field", Double.valueOf(Float.valueOf(randomFloat).toString())); - float roundedHalfFloat = HalfFloatPoint.sortableShortToHalfFloat(HalfFloatPoint.halfFloatToSortableShort(randomFloat)); - builder.append("\"half_float_field\":\"" + randomFloat + "\""); - fieldValues.put("half_float_field", Double.valueOf(Float.toString(roundedHalfFloat))); - } + Map expectedResponse = prepareTestData(columns -> { + columns.add(columnInfo("geo_point_field", "geo_point")); + columns.add(columnInfo("float_field", "float")); + columns.add(columnInfo("half_float_field", "half_float")); + }, (builder, fieldValues) -> { + Float randomFloat = randomFloat(); + // before "fields" API being added to QL, numbers were re-parsed from _source with a similar approach to + // indexing docvalues and for floating point numbers this may be different from the actual value passed in the _source + // floats were indexed as Doubles and the values returned had a greater precision and more decimals + builder.append(","); + if (isBwcNodeBeforeFieldsApiInQL) { + builder.append("\"geo_point_field\":{\"lat\":\"37.386483\", \"lon\":\"-122.083843\"},"); + fieldValues.put("geo_point_field", "POINT (-122.08384302444756 37.38648299127817)"); + builder.append("\"float_field\":" + randomFloat + ","); + fieldValues.put("float_field", Double.valueOf(randomFloat)); + builder.append("\"half_float_field\":123.456"); + fieldValues.put("half_float_field", 123.45600128173828d); + } else { + builder.append("\"geo_point_field\":{\"lat\":\"37.386483\", \"lon\":\"-122.083843\"},"); + fieldValues.put("geo_point_field", "POINT (-122.083843 37.386483)"); + builder.append("\"float_field\":" + randomFloat + ","); + /* + * Double.valueOf(float.toString) gets a `double` representing + * the `float` that we'd get by going through json which is + * base 10. just casting the `float` to a `double` will get + * a lower number with a lot more trailing digits because + * the cast adds *binary* 0s to the end. And those binary + * 0s don't translate the same as json's decimal 0s. + */ + fieldValues.put("float_field", Double.valueOf(Float.valueOf(randomFloat).toString())); + float roundedHalfFloat = HalfFloatPoint.sortableShortToHalfFloat(HalfFloatPoint.halfFloatToSortableShort(randomFloat)); + builder.append("\"half_float_field\":\"" + randomFloat + "\""); + fieldValues.put("half_float_field", Double.valueOf(Float.toString(roundedHalfFloat))); } - ); + }); assertAllTypesWithNodes(expectedResponse, bwcNodes); } public void testAllTypesWithRequestToUpgradedNodes() throws Exception { - Map expectedResponse = prepareTestData( - columns -> { - columns.add(columnInfo("geo_point_field", "geo_point")); - columns.add(columnInfo("float_field", "float")); - columns.add(columnInfo("half_float_field", "half_float")); - }, - (builder, fieldValues) -> { - Float randomFloat = randomFloat(); - builder.append(","); - if (isBwcNodeBeforeFieldsApiInQL) { - builder.append("\"geo_point_field\":{\"lat\":\"37.386483\", \"lon\":\"-122.083843\"},"); - fieldValues.put("geo_point_field", "POINT (-122.08384302444756 37.38648299127817)"); - builder.append("\"float_field\":" + randomFloat + ","); - fieldValues.put("float_field", Double.valueOf(randomFloat)); - builder.append("\"half_float_field\":123.456"); - fieldValues.put("half_float_field", 123.45600128173828d); - } else { - builder.append("\"geo_point_field\":{\"lat\":\"37.386483\", \"lon\":\"-122.083843\"},"); - fieldValues.put("geo_point_field", "POINT (-122.083843 37.386483)"); - builder.append("\"float_field\":" + randomFloat + ","); - /* - * Double.valueOf(float.toString) gets a `double` representing - * the `float` that we'd get by going through json which is - * base 10. just casting the `float` to a `double` will get - * a lower number with a lot more trailing digits because - * the cast adds *binary* 0s to the end. And those binary - * 0s don't translate the same as json's decimal 0s. - */ - fieldValues.put("float_field", Double.valueOf(Float.valueOf(randomFloat).toString())); - float roundedHalfFloat = HalfFloatPoint.sortableShortToHalfFloat(HalfFloatPoint.halfFloatToSortableShort(randomFloat)); - builder.append("\"half_float_field\":\"" + randomFloat + "\""); - fieldValues.put("half_float_field", Double.valueOf(Float.toString(roundedHalfFloat))); - } + Map expectedResponse = prepareTestData(columns -> { + columns.add(columnInfo("geo_point_field", "geo_point")); + columns.add(columnInfo("float_field", "float")); + columns.add(columnInfo("half_float_field", "half_float")); + }, (builder, fieldValues) -> { + Float randomFloat = randomFloat(); + builder.append(","); + if (isBwcNodeBeforeFieldsApiInQL) { + builder.append("\"geo_point_field\":{\"lat\":\"37.386483\", \"lon\":\"-122.083843\"},"); + fieldValues.put("geo_point_field", "POINT (-122.08384302444756 37.38648299127817)"); + builder.append("\"float_field\":" + randomFloat + ","); + fieldValues.put("float_field", Double.valueOf(randomFloat)); + builder.append("\"half_float_field\":123.456"); + fieldValues.put("half_float_field", 123.45600128173828d); + } else { + builder.append("\"geo_point_field\":{\"lat\":\"37.386483\", \"lon\":\"-122.083843\"},"); + fieldValues.put("geo_point_field", "POINT (-122.083843 37.386483)"); + builder.append("\"float_field\":" + randomFloat + ","); + /* + * Double.valueOf(float.toString) gets a `double` representing + * the `float` that we'd get by going through json which is + * base 10. just casting the `float` to a `double` will get + * a lower number with a lot more trailing digits because + * the cast adds *binary* 0s to the end. And those binary + * 0s don't translate the same as json's decimal 0s. + */ + fieldValues.put("float_field", Double.valueOf(Float.valueOf(randomFloat).toString())); + float roundedHalfFloat = HalfFloatPoint.sortableShortToHalfFloat(HalfFloatPoint.halfFloatToSortableShort(randomFloat)); + builder.append("\"half_float_field\":\"" + randomFloat + "\""); + fieldValues.put("half_float_field", Double.valueOf(Float.toString(roundedHalfFloat))); } - ); + }); assertAllTypesWithNodes(expectedResponse, newNodes); } @SuppressWarnings("unchecked") - private Map prepareTestData(Consumer>> additionalColumns, - BiConsumer> additionalValues) throws IOException { + private Map prepareTestData( + Consumer>> additionalColumns, + BiConsumer> additionalValues + ) throws IOException { Map expectedResponse = new HashMap<>(); List> columns = new ArrayList<>(); columns.add(columnInfo("interval_year", "interval_year")); @@ -214,8 +210,9 @@ private Map prepareTestData(Consumer>> builder.append("\"ip_field\":\"" + fieldValues.computeIfAbsent("ip_field", v -> "123.123.123.123") + "\","); builder.append("\"text_field\": \"" + fieldValues.computeIfAbsent("text_field", v -> randomAlphaOfLength(5)) + "\","); builder.append("\"keyword_field\": \"" + fieldValues.computeIfAbsent("keyword_field", v -> randomAlphaOfLength(5)) + "\","); - builder.append("\"constant_keyword_field\": \"" + fieldValues.computeIfAbsent("constant_keyword_field", - v -> constantKeywordValue) + "\","); + builder.append( + "\"constant_keyword_field\": \"" + fieldValues.computeIfAbsent("constant_keyword_field", v -> constantKeywordValue) + "\"," + ); builder.append("\"wildcard_field\": \"" + fieldValues.computeIfAbsent("wildcard_field", v -> randomAlphaOfLength(5)) + "\","); builder.append("\"geo_point_no_dv_field\":{\"lat\":\"40.123456\", \"lon\":\"100.234567\"},"); fieldValues.put("geo_point_no_dv_field", "POINT (100.234567 40.123456)"); @@ -243,11 +240,12 @@ private Map columnInfo(String name, String type) { return unmodifiableMap(column); } - private void assertAllTypesWithNodes(Map expectedResponse, List nodesList) - throws Exception { + private void assertAllTypesWithNodes(Map expectedResponse, List nodesList) throws Exception { try ( - RestClient client = buildClient(restClientSettings(), - nodesList.stream().map(TestNode::getPublishAddress).toArray(HttpHost[]::new)) + RestClient client = buildClient( + restClientSettings(), + nodesList.stream().map(TestNode::getPublishAddress).toArray(HttpHost[]::new) + ) ) { @SuppressWarnings("unchecked") List> columns = (List>) expectedResponse.get("columns"); @@ -266,9 +264,7 @@ private void assertAllTypesWithNodes(Map expectedResponse, List< Request request = new Request("POST", "_sql"); request.setJsonEntity(SqlCompatIT.sqlQueryEntityWithOptionalMode(query, bwcVersion)); - assertBusy(() -> { - assertResponse(expectedResponse, dropDisplaySizes(runSql(client, request))); - }); + assertBusy(() -> { assertResponse(expectedResponse, dropDisplaySizes(runSql(client, request))); }); } } diff --git a/x-pack/plugin/sql/qa/server/multi-node/src/test/java/org/elasticsearch/xpack/sql/qa/multi_node/RestSqlMultinodeIT.java b/x-pack/plugin/sql/qa/server/multi-node/src/test/java/org/elasticsearch/xpack/sql/qa/multi_node/RestSqlMultinodeIT.java index 75a26d6642559..db3d2695f02c1 100644 --- a/x-pack/plugin/sql/qa/server/multi-node/src/test/java/org/elasticsearch/xpack/sql/qa/multi_node/RestSqlMultinodeIT.java +++ b/x-pack/plugin/sql/qa/server/multi-node/src/test/java/org/elasticsearch/xpack/sql/qa/multi_node/RestSqlMultinodeIT.java @@ -11,10 +11,10 @@ import org.elasticsearch.client.Response; import org.elasticsearch.client.RestClient; import org.elasticsearch.common.Strings; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.test.NotEqualMessageBuilder; import org.elasticsearch.test.rest.ESRestTestCase; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.json.JsonXContent; import java.io.IOException; import java.nio.charset.UnsupportedCharsetException; diff --git a/x-pack/plugin/sql/qa/server/single-node/src/test/java/org/elasticsearch/xpack/sql/qa/single_node/ConsistentFunctionArgHandlingIT.java b/x-pack/plugin/sql/qa/server/single-node/src/test/java/org/elasticsearch/xpack/sql/qa/single_node/ConsistentFunctionArgHandlingIT.java index d85fbe771ef33..4d12eab4cc8e2 100644 --- a/x-pack/plugin/sql/qa/server/single-node/src/test/java/org/elasticsearch/xpack/sql/qa/single_node/ConsistentFunctionArgHandlingIT.java +++ b/x-pack/plugin/sql/qa/server/single-node/src/test/java/org/elasticsearch/xpack/sql/qa/single_node/ConsistentFunctionArgHandlingIT.java @@ -9,10 +9,10 @@ import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; -import org.elasticsearch.core.CheckedConsumer; import org.elasticsearch.common.UUIDs; -import org.elasticsearch.core.Tuple; +import org.elasticsearch.core.CheckedConsumer; import org.elasticsearch.core.PathUtils; +import org.elasticsearch.core.Tuple; import org.elasticsearch.xpack.sql.qa.jdbc.JdbcIntegrationTestCase; import java.io.IOException; diff --git a/x-pack/plugin/sql/qa/server/single-node/src/test/java/org/elasticsearch/xpack/sql/qa/single_node/GeoJdbcCsvSpecIT.java b/x-pack/plugin/sql/qa/server/single-node/src/test/java/org/elasticsearch/xpack/sql/qa/single_node/GeoJdbcCsvSpecIT.java index 3c17993d94de6..b41dc5630f408 100644 --- a/x-pack/plugin/sql/qa/server/single-node/src/test/java/org/elasticsearch/xpack/sql/qa/single_node/GeoJdbcCsvSpecIT.java +++ b/x-pack/plugin/sql/qa/server/single-node/src/test/java/org/elasticsearch/xpack/sql/qa/single_node/GeoJdbcCsvSpecIT.java @@ -8,6 +8,7 @@ package org.elasticsearch.xpack.sql.qa.single_node; import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; + import org.elasticsearch.xpack.sql.qa.geo.GeoCsvSpecTestCase; import org.elasticsearch.xpack.sql.qa.jdbc.CsvTestUtils.CsvTestCase; diff --git a/x-pack/plugin/sql/qa/server/single-node/src/test/java/org/elasticsearch/xpack/sql/qa/single_node/JdbcDocCsvSpecIT.java b/x-pack/plugin/sql/qa/server/single-node/src/test/java/org/elasticsearch/xpack/sql/qa/single_node/JdbcDocCsvSpecIT.java index 342aea9daffa0..17f39ea295f16 100644 --- a/x-pack/plugin/sql/qa/server/single-node/src/test/java/org/elasticsearch/xpack/sql/qa/single_node/JdbcDocCsvSpecIT.java +++ b/x-pack/plugin/sql/qa/server/single-node/src/test/java/org/elasticsearch/xpack/sql/qa/single_node/JdbcDocCsvSpecIT.java @@ -7,6 +7,7 @@ package org.elasticsearch.xpack.sql.qa.single_node; import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; + import org.apache.logging.log4j.Logger; import org.elasticsearch.client.RestClient; import org.elasticsearch.xpack.sql.qa.jdbc.CsvTestUtils.CsvTestCase; diff --git a/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/AbstractSqlQueryRequest.java b/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/AbstractSqlQueryRequest.java index 5c6d43b2165e1..fdee714f3e5e9 100644 --- a/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/AbstractSqlQueryRequest.java +++ b/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/AbstractSqlQueryRequest.java @@ -9,22 +9,22 @@ import org.elasticsearch.Version; import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.CompositeIndicesRequest; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.index.query.AbstractQueryBuilder; +import org.elasticsearch.index.query.QueryBuilder; +import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.xcontent.ObjectParser; import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentFragment; import org.elasticsearch.xcontent.XContentLocation; import org.elasticsearch.xcontent.XContentParseException; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentParser.Token; -import org.elasticsearch.index.query.AbstractQueryBuilder; -import org.elasticsearch.index.query.QueryBuilder; -import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.xpack.sql.proto.Mode; import org.elasticsearch.xpack.sql.proto.Protocol; import org.elasticsearch.xpack.sql.proto.RequestInfo; @@ -89,8 +89,18 @@ public AbstractSqlQueryRequest() { super(); } - public AbstractSqlQueryRequest(String query, List params, QueryBuilder filter, Map runtimeMappings, - ZoneId zoneId, String catalog, int fetchSize, TimeValue requestTimeout, TimeValue pageTimeout, RequestInfo requestInfo) { + public AbstractSqlQueryRequest( + String query, + List params, + QueryBuilder filter, + Map runtimeMappings, + ZoneId zoneId, + String catalog, + int fetchSize, + TimeValue requestTimeout, + TimeValue pageTimeout, + RequestInfo requestInfo + ) { super(requestInfo); this.query = query; this.params = params; @@ -115,11 +125,14 @@ protected static ObjectParser objec parser.declareString((request, zoneId) -> request.zoneId(ZoneId.of(zoneId)), TIME_ZONE); parser.declareString(AbstractSqlQueryRequest::catalog, CATALOG); parser.declareInt(AbstractSqlQueryRequest::fetchSize, FETCH_SIZE); - parser.declareString((request, timeout) -> request.requestTimeout(TimeValue.parseTimeValue(timeout, Protocol.REQUEST_TIMEOUT, - REQUEST_TIMEOUT_NAME)), REQUEST_TIMEOUT); parser.declareString( - (request, timeout) -> request.pageTimeout(TimeValue.parseTimeValue(timeout, Protocol.PAGE_TIMEOUT, PAGE_TIMEOUT_NAME)), - PAGE_TIMEOUT); + (request, timeout) -> request.requestTimeout(TimeValue.parseTimeValue(timeout, Protocol.REQUEST_TIMEOUT, REQUEST_TIMEOUT_NAME)), + REQUEST_TIMEOUT + ); + parser.declareString( + (request, timeout) -> request.pageTimeout(TimeValue.parseTimeValue(timeout, Protocol.PAGE_TIMEOUT, PAGE_TIMEOUT_NAME)), + PAGE_TIMEOUT + ); parser.declareObject(AbstractSqlQueryRequest::filter, (p, c) -> AbstractQueryBuilder.parseInnerQueryBuilder(p), FILTER); parser.declareObject(AbstractSqlQueryRequest::runtimeMappings, (p, c) -> p.map(), SearchSourceBuilder.RUNTIME_MAPPINGS_FIELD); return parser; @@ -224,14 +237,18 @@ private static List parseParams(XContentParser p) throws IOE } protected static void validateParams(List params, Mode mode) { - for(SqlTypedParamValue param : params) { + for (SqlTypedParamValue param : params) { if (Mode.isDriver(mode) && param.hasExplicitType() == false) { - throw new XContentParseException(param.tokenLocation(), "[params] must be an array where each entry is an object with a " - + "value/type pair"); + throw new XContentParseException( + param.tokenLocation(), + "[params] must be an array where each entry is an object with a " + "value/type pair" + ); } if (Mode.isDriver(mode) == false && param.hasExplicitType()) { - throw new XContentParseException(param.tokenLocation(), "[params] must be an array where each entry is a single field (no " - + "objects supported)"); + throw new XContentParseException( + param.tokenLocation(), + "[params] must be an array where each entry is a single field (no " + "objects supported)" + ); } } } @@ -244,13 +261,23 @@ public ActionRequestValidationException validate() { if (Mode.isDedicatedClient(mode)) { if (requestInfo().version() == null) { if (Strings.hasText(query())) { - validationException = addValidationError("[version] is required for the [" + mode.toString() + "] client", - validationException); + validationException = addValidationError( + "[version] is required for the [" + mode.toString() + "] client", + validationException + ); } } else if (SqlVersion.isClientCompatible(SqlVersion.fromId(CURRENT.id), requestInfo().version()) == false) { - validationException = addValidationError("The [" + requestInfo().version() + "] version of the [" + - mode.toString() + "] " + "client is not compatible with Elasticsearch version [" + CURRENT + "]", - validationException); + validationException = addValidationError( + "The [" + + requestInfo().version() + + "] version of the [" + + mode.toString() + + "] " + + "client is not compatible with Elasticsearch version [" + + CURRENT + + "]", + validationException + ); } } if (runtimeMappings != null) { @@ -260,8 +287,10 @@ public ActionRequestValidationException validate() { return validationException; } - private static ActionRequestValidationException validateRuntimeMappings(Map runtimeMappings, - ActionRequestValidationException validationException) { + private static ActionRequestValidationException validateRuntimeMappings( + Map runtimeMappings, + ActionRequestValidationException validationException + ) { for (Map.Entry entry : runtimeMappings.entrySet()) { // top level objects are fields String fieldName = entry.getKey(); @@ -272,8 +301,10 @@ private static ActionRequestValidationException validateRuntimeMappings(Map columns, List> rows } sb.append('\n'); - /* Now format the results. Sadly, this means that column * widths are entirely determined by the first batch of * results. */ diff --git a/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlClearCursorRequest.java b/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlClearCursorRequest.java index 6b0ce56a53749..553a0a3e6d6c1 100644 --- a/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlClearCursorRequest.java +++ b/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlClearCursorRequest.java @@ -22,9 +22,9 @@ import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg; import static org.elasticsearch.xcontent.ConstructingObjectParser.optionalConstructorArg; import static org.elasticsearch.xpack.sql.action.AbstractSqlQueryRequest.CLIENT_ID; -import static org.elasticsearch.xpack.sql.action.AbstractSqlQueryRequest.VERSION; import static org.elasticsearch.xpack.sql.action.AbstractSqlQueryRequest.CURSOR; import static org.elasticsearch.xpack.sql.action.AbstractSqlQueryRequest.MODE; +import static org.elasticsearch.xpack.sql.action.AbstractSqlQueryRequest.VERSION; /** * Request to clean all SQL resources associated with the cursor @@ -34,10 +34,9 @@ public class SqlClearCursorRequest extends AbstractSqlRequest { private static final ConstructingObjectParser PARSER = // here the position in "objects" is the same as the fields parser declarations below new ConstructingObjectParser<>(SqlClearCursorAction.NAME, objects -> { - RequestInfo requestInfo = new RequestInfo(Mode.fromString((String) objects[1]), - (String) objects[2]); + RequestInfo requestInfo = new RequestInfo(Mode.fromString((String) objects[1]), (String) objects[2]); return new SqlClearCursorRequest(requestInfo, (String) objects[0]); - }); + }); static { // "cursor" is required constructor parameter @@ -49,8 +48,7 @@ public class SqlClearCursorRequest extends AbstractSqlRequest { private String cursor; - public SqlClearCursorRequest() { - } + public SqlClearCursorRequest() {} public SqlClearCursorRequest(RequestInfo requestInfo, String cursor) { super(requestInfo); diff --git a/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlClearCursorRequestBuilder.java b/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlClearCursorRequestBuilder.java index e29cc5fc49f0a..aaef92666fbc4 100644 --- a/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlClearCursorRequestBuilder.java +++ b/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlClearCursorRequestBuilder.java @@ -9,8 +9,7 @@ import org.elasticsearch.action.ActionRequestBuilder; import org.elasticsearch.client.ElasticsearchClient; -public class SqlClearCursorRequestBuilder extends - ActionRequestBuilder { +public class SqlClearCursorRequestBuilder extends ActionRequestBuilder { public SqlClearCursorRequestBuilder(ElasticsearchClient client, SqlClearCursorAction action) { super(client, action, new SqlClearCursorRequest()); diff --git a/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlClearCursorResponse.java b/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlClearCursorResponse.java index 204d692abd5b4..ce638c9ee6750 100644 --- a/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlClearCursorResponse.java +++ b/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlClearCursorResponse.java @@ -10,8 +10,8 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.StatusToXContentObject; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.Objects; diff --git a/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlQueryRequest.java b/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlQueryRequest.java index 481306064fd42..be5bfde084ff2 100644 --- a/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlQueryRequest.java +++ b/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlQueryRequest.java @@ -8,17 +8,17 @@ import org.elasticsearch.Version; import org.elasticsearch.action.ActionRequestValidationException; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.ObjectParser; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.tasks.Task; import org.elasticsearch.tasks.TaskId; +import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.sql.proto.Protocol; import org.elasticsearch.xpack.sql.proto.RequestInfo; import org.elasticsearch.xpack.sql.proto.SqlTypedParamValue; @@ -61,12 +61,19 @@ public class SqlQueryRequest extends AbstractSqlQueryRequest { PARSER.declareBoolean(SqlQueryRequest::fieldMultiValueLeniency, FIELD_MULTI_VALUE_LENIENCY); PARSER.declareBoolean(SqlQueryRequest::indexIncludeFrozen, INDEX_INCLUDE_FROZEN); PARSER.declareBoolean(SqlQueryRequest::binaryCommunication, BINARY_COMMUNICATION); - PARSER.declareField(SqlQueryRequest::waitForCompletionTimeout, - (p, c) -> TimeValue.parseTimeValue(p.text(), WAIT_FOR_COMPLETION_TIMEOUT_NAME), WAIT_FOR_COMPLETION_TIMEOUT, - ObjectParser.ValueType.VALUE); + PARSER.declareField( + SqlQueryRequest::waitForCompletionTimeout, + (p, c) -> TimeValue.parseTimeValue(p.text(), WAIT_FOR_COMPLETION_TIMEOUT_NAME), + WAIT_FOR_COMPLETION_TIMEOUT, + ObjectParser.ValueType.VALUE + ); PARSER.declareBoolean(SqlQueryRequest::keepOnCompletion, KEEP_ON_COMPLETION); - PARSER.declareField(SqlQueryRequest::keepAlive, - (p, c) -> TimeValue.parseTimeValue(p.text(), KEEP_ALIVE_NAME), KEEP_ALIVE, ObjectParser.ValueType.VALUE); + PARSER.declareField( + SqlQueryRequest::keepAlive, + (p, c) -> TimeValue.parseTimeValue(p.text(), KEEP_ALIVE_NAME), + KEEP_ALIVE, + ObjectParser.ValueType.VALUE + ); } private String cursor = ""; @@ -90,10 +97,25 @@ public SqlQueryRequest() { super(); } - public SqlQueryRequest(String query, List params, QueryBuilder filter, Map runtimeMappings, - ZoneId zoneId, String catalog, int fetchSize, TimeValue requestTimeout, TimeValue pageTimeout, Boolean columnar, - String cursor, RequestInfo requestInfo, boolean fieldMultiValueLeniency, boolean indexIncludeFrozen, - TimeValue waitForCompletionTimeout, boolean keepOnCompletion, TimeValue keepAlive) { + public SqlQueryRequest( + String query, + List params, + QueryBuilder filter, + Map runtimeMappings, + ZoneId zoneId, + String catalog, + int fetchSize, + TimeValue requestTimeout, + TimeValue pageTimeout, + Boolean columnar, + String cursor, + RequestInfo requestInfo, + boolean fieldMultiValueLeniency, + boolean indexIncludeFrozen, + TimeValue waitForCompletionTimeout, + boolean keepOnCompletion, + TimeValue keepAlive + ) { super(query, params, filter, runtimeMappings, zoneId, catalog, fetchSize, requestTimeout, pageTimeout, requestInfo); this.cursor = cursor; this.columnar = columnar; @@ -206,8 +228,20 @@ public TimeValue keepAlive() { @Override public Task createTask(long id, String type, String action, TaskId parentTaskId, Map headers) { - return new SqlQueryTask(id, type, action, getDescription(), parentTaskId, headers, null, null, keepAlive, - mode(), version(), columnar()); + return new SqlQueryTask( + id, + type, + action, + getDescription(), + parentTaskId, + headers, + null, + null, + keepAlive, + mode(), + version(), + columnar() + ); } public SqlQueryRequest(StreamInput in) throws IOException { @@ -241,21 +275,30 @@ public void writeTo(StreamOutput out) throws IOException { @Override public int hashCode() { - return Objects.hash(super.hashCode(), cursor, columnar, fieldMultiValueLeniency, indexIncludeFrozen, binaryCommunication, - waitForCompletionTimeout, keepOnCompletion, keepAlive); + return Objects.hash( + super.hashCode(), + cursor, + columnar, + fieldMultiValueLeniency, + indexIncludeFrozen, + binaryCommunication, + waitForCompletionTimeout, + keepOnCompletion, + keepAlive + ); } @Override public boolean equals(Object obj) { return super.equals(obj) - && Objects.equals(cursor, ((SqlQueryRequest) obj).cursor) - && Objects.equals(columnar, ((SqlQueryRequest) obj).columnar) - && fieldMultiValueLeniency == ((SqlQueryRequest) obj).fieldMultiValueLeniency - && indexIncludeFrozen == ((SqlQueryRequest) obj).indexIncludeFrozen - && binaryCommunication == ((SqlQueryRequest) obj).binaryCommunication - && Objects.equals(waitForCompletionTimeout, ((SqlQueryRequest) obj).waitForCompletionTimeout) - && keepOnCompletion == ((SqlQueryRequest) obj).keepOnCompletion - && Objects.equals(keepAlive, ((SqlQueryRequest) obj).keepAlive); + && Objects.equals(cursor, ((SqlQueryRequest) obj).cursor) + && Objects.equals(columnar, ((SqlQueryRequest) obj).columnar) + && fieldMultiValueLeniency == ((SqlQueryRequest) obj).fieldMultiValueLeniency + && indexIncludeFrozen == ((SqlQueryRequest) obj).indexIncludeFrozen + && binaryCommunication == ((SqlQueryRequest) obj).binaryCommunication + && Objects.equals(waitForCompletionTimeout, ((SqlQueryRequest) obj).waitForCompletionTimeout) + && keepOnCompletion == ((SqlQueryRequest) obj).keepOnCompletion + && Objects.equals(keepAlive, ((SqlQueryRequest) obj).keepAlive); } @Override @@ -266,14 +309,30 @@ public String getDescription() { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { // This is needed just to test round-trip compatibility with proto.SqlQueryRequest - return new org.elasticsearch.xpack.sql.proto.SqlQueryRequest(query(), params(), zoneId(), catalog(), fetchSize(), requestTimeout(), - pageTimeout(), filter(), columnar(), cursor(), requestInfo(), fieldMultiValueLeniency(), indexIncludeFrozen(), - binaryCommunication(), runtimeMappings(), waitForCompletionTimeout(), keepOnCompletion(), keepAlive()) - .toXContent(builder, params); + return new org.elasticsearch.xpack.sql.proto.SqlQueryRequest( + query(), + params(), + zoneId(), + catalog(), + fetchSize(), + requestTimeout(), + pageTimeout(), + filter(), + columnar(), + cursor(), + requestInfo(), + fieldMultiValueLeniency(), + indexIncludeFrozen(), + binaryCommunication(), + runtimeMappings(), + waitForCompletionTimeout(), + keepOnCompletion(), + keepAlive() + ).toXContent(builder, params); } public static SqlQueryRequest fromXContent(XContentParser parser) { - SqlQueryRequest request = PARSER.apply(parser, null); + SqlQueryRequest request = PARSER.apply(parser, null); validateParams(request.params(), request.mode()); return request; } diff --git a/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlQueryRequestBuilder.java b/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlQueryRequestBuilder.java index 7f87b3ed6c551..8e1ac5cedf53e 100644 --- a/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlQueryRequestBuilder.java +++ b/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlQueryRequestBuilder.java @@ -28,20 +28,73 @@ public class SqlQueryRequestBuilder extends ActionRequestBuilder { public SqlQueryRequestBuilder(ElasticsearchClient client, SqlQueryAction action) { - this(client, action, "", emptyList(), null, emptyMap(), Protocol.TIME_ZONE, null, Protocol.FETCH_SIZE, - Protocol.REQUEST_TIMEOUT, Protocol.PAGE_TIMEOUT, false, "", new RequestInfo(Mode.PLAIN), Protocol.FIELD_MULTI_VALUE_LENIENCY, - Protocol.INDEX_INCLUDE_FROZEN, Protocol.DEFAULT_WAIT_FOR_COMPLETION_TIMEOUT, Protocol.DEFAULT_KEEP_ON_COMPLETION, - Protocol.DEFAULT_KEEP_ALIVE); - } - - public SqlQueryRequestBuilder(ElasticsearchClient client, SqlQueryAction action, String query, List params, - QueryBuilder filter, Map runtimeMappings, ZoneId zoneId, String catalog, int fetchSize, - TimeValue requestTimeout, TimeValue pageTimeout, boolean columnar, String nextPageInfo, RequestInfo requestInfo, - boolean multiValueFieldLeniency, boolean indexIncludeFrozen, TimeValue waitForCompletionTimeout, boolean keepOnCompletion, - TimeValue keepAlive) { - super(client, action, new SqlQueryRequest(query, params, filter, runtimeMappings, zoneId, catalog, fetchSize, requestTimeout, - pageTimeout, columnar, nextPageInfo, requestInfo, multiValueFieldLeniency, indexIncludeFrozen, waitForCompletionTimeout, - keepOnCompletion, keepAlive)); + this( + client, + action, + "", + emptyList(), + null, + emptyMap(), + Protocol.TIME_ZONE, + null, + Protocol.FETCH_SIZE, + Protocol.REQUEST_TIMEOUT, + Protocol.PAGE_TIMEOUT, + false, + "", + new RequestInfo(Mode.PLAIN), + Protocol.FIELD_MULTI_VALUE_LENIENCY, + Protocol.INDEX_INCLUDE_FROZEN, + Protocol.DEFAULT_WAIT_FOR_COMPLETION_TIMEOUT, + Protocol.DEFAULT_KEEP_ON_COMPLETION, + Protocol.DEFAULT_KEEP_ALIVE + ); + } + + public SqlQueryRequestBuilder( + ElasticsearchClient client, + SqlQueryAction action, + String query, + List params, + QueryBuilder filter, + Map runtimeMappings, + ZoneId zoneId, + String catalog, + int fetchSize, + TimeValue requestTimeout, + TimeValue pageTimeout, + boolean columnar, + String nextPageInfo, + RequestInfo requestInfo, + boolean multiValueFieldLeniency, + boolean indexIncludeFrozen, + TimeValue waitForCompletionTimeout, + boolean keepOnCompletion, + TimeValue keepAlive + ) { + super( + client, + action, + new SqlQueryRequest( + query, + params, + filter, + runtimeMappings, + zoneId, + catalog, + fetchSize, + requestTimeout, + pageTimeout, + columnar, + nextPageInfo, + requestInfo, + multiValueFieldLeniency, + indexIncludeFrozen, + waitForCompletionTimeout, + keepOnCompletion, + keepAlive + ) + ); } public SqlQueryRequestBuilder query(String query) { diff --git a/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlQueryResponse.java b/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlQueryResponse.java index e494dd1cdfec6..2413ee02c040e 100644 --- a/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlQueryResponse.java +++ b/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlQueryResponse.java @@ -11,9 +11,9 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.core.Nullable; import org.elasticsearch.xpack.ql.async.QlStatusResponse; import org.elasticsearch.xpack.sql.proto.ColumnInfo; import org.elasticsearch.xpack.sql.proto.Mode; @@ -269,8 +269,7 @@ public static XContentBuilder value(XContentBuilder builder, Mode mode, SqlVersi // use the SQL format for intervals when sending back the response for CLI // all other clients will receive ISO 8601 formatted intervals builder.value(value.toString()); - } - else { + } else { builder.value(value); } return builder; @@ -320,9 +319,7 @@ public boolean equals(Object o) { return false; } SqlQueryResponse that = (SqlQueryResponse) o; - return Objects.equals(cursor, that.cursor) && - Objects.equals(columns, that.columns) && - Objects.equals(rows, that.rows); + return Objects.equals(cursor, that.cursor) && Objects.equals(columns, that.columns) && Objects.equals(rows, that.rows); } @Override diff --git a/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlQueryTask.java b/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlQueryTask.java index 710f5309a49aa..1faa153ce827f 100644 --- a/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlQueryTask.java +++ b/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlQueryTask.java @@ -24,9 +24,20 @@ public class SqlQueryTask extends StoredAsyncTask { private final SqlVersion sqlVersion; private final boolean columnar; - public SqlQueryTask(long id, String type, String action, String description, TaskId parentTaskId, Map headers, - Map originHeaders, AsyncExecutionId asyncExecutionId, TimeValue keepAlive, Mode mode, - SqlVersion sqlVersion, boolean columnar) { + public SqlQueryTask( + long id, + String type, + String action, + String description, + TaskId parentTaskId, + Map headers, + Map originHeaders, + AsyncExecutionId asyncExecutionId, + TimeValue keepAlive, + Mode mode, + SqlVersion sqlVersion, + boolean columnar + ) { super(id, type, action, description, parentTaskId, headers, originHeaders, asyncExecutionId, keepAlive); this.mode = mode; this.sqlVersion = sqlVersion; @@ -38,7 +49,6 @@ public SqlQueryResponse getCurrentResult() { // for Ql searches we never store a search response in the task (neither partial, nor final) // we kill the task on final response, so if the task is still present, it means the search is still running // NB: the schema is only returned in the actual first (and currently last) response to the query - return new SqlQueryResponse("", mode, sqlVersion, columnar, null, emptyList(), - getExecutionId().getEncoded(), true, true); + return new SqlQueryResponse("", mode, sqlVersion, columnar, null, emptyList(), getExecutionId().getEncoded(), true, true); } } diff --git a/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlTranslateRequest.java b/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlTranslateRequest.java index f9b7dd1dc19cc..099a241610b36 100644 --- a/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlTranslateRequest.java +++ b/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlTranslateRequest.java @@ -10,10 +10,10 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.xcontent.ObjectParser; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.xpack.sql.proto.RequestInfo; import org.elasticsearch.xpack.sql.proto.SqlQueryRequest; import org.elasticsearch.xpack.sql.proto.SqlTypedParamValue; @@ -35,8 +35,17 @@ public SqlTranslateRequest() { super(); } - public SqlTranslateRequest(String query, List params, QueryBuilder filter, Map runtimeMappings, - ZoneId zoneId, int fetchSize, TimeValue requestTimeout, TimeValue pageTimeout, RequestInfo requestInfo) { + public SqlTranslateRequest( + String query, + List params, + QueryBuilder filter, + Map runtimeMappings, + ZoneId zoneId, + int fetchSize, + TimeValue requestTimeout, + TimeValue pageTimeout, + RequestInfo requestInfo + ) { super(query, params, filter, runtimeMappings, zoneId, null, fetchSize, requestTimeout, pageTimeout, requestInfo); } @@ -67,7 +76,14 @@ public static SqlTranslateRequest fromXContent(XContentParser parser) { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { // This is needed just to test parsing of SqlTranslateRequest, so we can reuse SqlQuerySerialization - return new SqlQueryRequest(query(), params(), zoneId(), null, fetchSize(), requestTimeout(), pageTimeout(), + return new SqlQueryRequest( + query(), + params(), + zoneId(), + null, + fetchSize(), + requestTimeout(), + pageTimeout(), filter(), null, null, @@ -78,6 +94,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws runtimeMappings(), null, false, - null).toXContent(builder, params); + null + ).toXContent(builder, params); } } diff --git a/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlTranslateRequestBuilder.java b/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlTranslateRequestBuilder.java index 9b16968bfd006..a86e14a00d9bb 100644 --- a/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlTranslateRequestBuilder.java +++ b/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlTranslateRequestBuilder.java @@ -27,15 +27,39 @@ */ public class SqlTranslateRequestBuilder extends ActionRequestBuilder { public SqlTranslateRequestBuilder(ElasticsearchClient client, SqlTranslateAction action) { - this(client, action, null, null, emptyMap(), emptyList(), Protocol.TIME_ZONE, Protocol.FETCH_SIZE, - Protocol.REQUEST_TIMEOUT, Protocol.PAGE_TIMEOUT, new RequestInfo(Mode.PLAIN)); + this( + client, + action, + null, + null, + emptyMap(), + emptyList(), + Protocol.TIME_ZONE, + Protocol.FETCH_SIZE, + Protocol.REQUEST_TIMEOUT, + Protocol.PAGE_TIMEOUT, + new RequestInfo(Mode.PLAIN) + ); } - public SqlTranslateRequestBuilder(ElasticsearchClient client, SqlTranslateAction action, String query, QueryBuilder filter, - Map runtimeMappings, List params, ZoneId zoneId, int fetchSize, TimeValue requestTimeout, - TimeValue pageTimeout, RequestInfo requestInfo) { - super(client, action, new SqlTranslateRequest(query, params, filter, runtimeMappings, zoneId, fetchSize, requestTimeout, - pageTimeout, requestInfo)); + public SqlTranslateRequestBuilder( + ElasticsearchClient client, + SqlTranslateAction action, + String query, + QueryBuilder filter, + Map runtimeMappings, + List params, + ZoneId zoneId, + int fetchSize, + TimeValue requestTimeout, + TimeValue pageTimeout, + RequestInfo requestInfo + ) { + super( + client, + action, + new SqlTranslateRequest(query, params, filter, runtimeMappings, zoneId, fetchSize, requestTimeout, pageTimeout, requestInfo) + ); } public SqlTranslateRequestBuilder query(String query) { diff --git a/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlTranslateResponse.java b/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlTranslateResponse.java index 10a01ea566600..62ae0d54eb584 100644 --- a/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlTranslateResponse.java +++ b/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlTranslateResponse.java @@ -9,9 +9,9 @@ import org.elasticsearch.action.ActionResponse; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.search.builder.SearchSourceBuilder; import java.io.IOException; import java.util.Objects; diff --git a/x-pack/plugin/sql/sql-action/src/test/java/org/elasticsearch/xpack/sql/action/SqlClearCursorRequestTests.java b/x-pack/plugin/sql/sql-action/src/test/java/org/elasticsearch/xpack/sql/action/SqlClearCursorRequestTests.java index 4829302e9bc69..b48cae980b211 100644 --- a/x-pack/plugin/sql/sql-action/src/test/java/org/elasticsearch/xpack/sql/action/SqlClearCursorRequestTests.java +++ b/x-pack/plugin/sql/sql-action/src/test/java/org/elasticsearch/xpack/sql/action/SqlClearCursorRequestTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.sql.action; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.sql.proto.Mode; import org.elasticsearch.xpack.sql.proto.RequestInfo; import org.junit.Before; @@ -24,8 +24,7 @@ public class SqlClearCursorRequestTests extends AbstractSerializingTestCase mutator = randomFrom( - request -> request.requestInfo(randomValueOtherThan(request.requestInfo(), this::randomRequestInfo)), - request -> request.setCursor(randomValueOtherThan(request.getCursor(), SqlQueryResponseTests::randomStringCursor)) + request -> request.requestInfo(randomValueOtherThan(request.requestInfo(), this::randomRequestInfo)), + request -> request.setCursor(randomValueOtherThan(request.getCursor(), SqlQueryResponseTests::randomStringCursor)) ); SqlClearCursorRequest newRequest = new SqlClearCursorRequest(instance.requestInfo(), instance.getCursor()); mutator.accept(newRequest); return newRequest; } } - diff --git a/x-pack/plugin/sql/sql-action/src/test/java/org/elasticsearch/xpack/sql/action/SqlClearCursorResponseTests.java b/x-pack/plugin/sql/sql-action/src/test/java/org/elasticsearch/xpack/sql/action/SqlClearCursorResponseTests.java index 403a30f705916..2f0ba57239982 100644 --- a/x-pack/plugin/sql/sql-action/src/test/java/org/elasticsearch/xpack/sql/action/SqlClearCursorResponseTests.java +++ b/x-pack/plugin/sql/sql-action/src/test/java/org/elasticsearch/xpack/sql/action/SqlClearCursorResponseTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.sql.action; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; public class SqlClearCursorResponseTests extends AbstractSerializingTestCase { @@ -29,8 +29,8 @@ protected SqlClearCursorResponse mutateInstance(SqlClearCursorResponse instance) @Override protected SqlClearCursorResponse doParseInstance(XContentParser parser) { - org.elasticsearch.xpack.sql.proto.SqlClearCursorResponse response = - org.elasticsearch.xpack.sql.proto.SqlClearCursorResponse.fromXContent(parser); + org.elasticsearch.xpack.sql.proto.SqlClearCursorResponse response = org.elasticsearch.xpack.sql.proto.SqlClearCursorResponse + .fromXContent(parser); return new SqlClearCursorResponse(response.isSucceeded()); } } diff --git a/x-pack/plugin/sql/sql-action/src/test/java/org/elasticsearch/xpack/sql/action/SqlQueryRequestTests.java b/x-pack/plugin/sql/sql-action/src/test/java/org/elasticsearch/xpack/sql/action/SqlQueryRequestTests.java index 4aba8c1dffef2..8ed2428e1d21e 100644 --- a/x-pack/plugin/sql/sql-action/src/test/java/org/elasticsearch/xpack/sql/action/SqlQueryRequestTests.java +++ b/x-pack/plugin/sql/sql-action/src/test/java/org/elasticsearch/xpack/sql/action/SqlQueryRequestTests.java @@ -11,13 +11,13 @@ import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.search.SearchModule; +import org.elasticsearch.test.AbstractWireSerializingTestCase; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.search.SearchModule; -import org.elasticsearch.test.AbstractWireSerializingTestCase; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.sql.proto.Mode; import org.elasticsearch.xpack.sql.proto.Protocol; import org.elasticsearch.xpack.sql.proto.RequestInfo; @@ -66,8 +66,7 @@ public class SqlQueryRequestTests extends AbstractWireSerializingTestCase instanceReader() { protected SqlQueryRequest mutateInstance(SqlQueryRequest instance) { @SuppressWarnings("unchecked") Consumer mutator = randomFrom( - request -> mutateRequestInfo(instance, request), - request -> request.query(randomValueOtherThan(request.query(), () -> randomAlphaOfLength(5))), - request -> request.params(randomValueOtherThan(request.params(), this::randomParameters)), - request -> request.zoneId(randomValueOtherThan(request.zoneId(), ESTestCase::randomZone)), - request -> request.catalog(randomValueOtherThan(request.catalog(), () -> randomAlphaOfLength(10))), - request -> request.fetchSize(randomValueOtherThan(request.fetchSize(), () -> between(1, Integer.MAX_VALUE))), - request -> request.requestTimeout(randomValueOtherThan(request.requestTimeout(), this::randomTV)), - request -> request.filter(randomValueOtherThan(request.filter(), - () -> request.filter() == null ? randomFilter(random()) : randomFilterOrNull(random()))), - request -> request.columnar(randomValueOtherThan(request.columnar(), ESTestCase::randomBoolean)), - request -> request.cursor(randomValueOtherThan(request.cursor(), SqlQueryResponseTests::randomStringCursor)), - request -> request.waitForCompletionTimeout(randomValueOtherThan(request.waitForCompletionTimeout(), this::randomTV)), - request -> request.keepOnCompletion(randomValueOtherThan(request.keepOnCompletion(), ESTestCase::randomBoolean)), - request -> request.keepAlive(randomValueOtherThan(request.keepAlive(), () -> randomTVGreaterThan(MIN_KEEP_ALIVE))) + request -> mutateRequestInfo(instance, request), + request -> request.query(randomValueOtherThan(request.query(), () -> randomAlphaOfLength(5))), + request -> request.params(randomValueOtherThan(request.params(), this::randomParameters)), + request -> request.zoneId(randomValueOtherThan(request.zoneId(), ESTestCase::randomZone)), + request -> request.catalog(randomValueOtherThan(request.catalog(), () -> randomAlphaOfLength(10))), + request -> request.fetchSize(randomValueOtherThan(request.fetchSize(), () -> between(1, Integer.MAX_VALUE))), + request -> request.requestTimeout(randomValueOtherThan(request.requestTimeout(), this::randomTV)), + request -> request.filter( + randomValueOtherThan( + request.filter(), + () -> request.filter() == null ? randomFilter(random()) : randomFilterOrNull(random()) + ) + ), + request -> request.columnar(randomValueOtherThan(request.columnar(), ESTestCase::randomBoolean)), + request -> request.cursor(randomValueOtherThan(request.cursor(), SqlQueryResponseTests::randomStringCursor)), + request -> request.waitForCompletionTimeout(randomValueOtherThan(request.waitForCompletionTimeout(), this::randomTV)), + request -> request.keepOnCompletion(randomValueOtherThan(request.keepOnCompletion(), ESTestCase::randomBoolean)), + request -> request.keepAlive(randomValueOtherThan(request.keepAlive(), () -> randomTVGreaterThan(MIN_KEEP_ALIVE))) + ); + SqlQueryRequest newRequest = new SqlQueryRequest( + instance.query(), + instance.params(), + instance.filter(), + instance.runtimeMappings(), + instance.zoneId(), + instance.catalog(), + instance.fetchSize(), + instance.requestTimeout(), + instance.pageTimeout(), + instance.columnar(), + instance.cursor(), + instance.requestInfo(), + instance.fieldMultiValueLeniency(), + instance.indexIncludeFrozen(), + instance.waitForCompletionTimeout(), + instance.keepOnCompletion(), + instance.keepAlive() ); - SqlQueryRequest newRequest = new SqlQueryRequest(instance.query(), instance.params(), instance.filter(), instance.runtimeMappings(), - instance.zoneId(), instance.catalog(), instance.fetchSize(), instance.requestTimeout(), instance.pageTimeout(), - instance.columnar(), instance.cursor(), instance.requestInfo(), instance.fieldMultiValueLeniency(), - instance.indexIncludeFrozen(), instance.waitForCompletionTimeout(), instance.keepOnCompletion(), instance.keepAlive()); mutator.accept(newRequest); return newRequest; } @@ -127,12 +159,12 @@ private AbstractSqlQueryRequest mutateRequestInfo(SqlQueryRequest oldRequest, Sq RequestInfo requestInfo = randomValueOtherThan(newRequest.requestInfo(), this::randomRequestInfo); newRequest.requestInfo(requestInfo); if (Mode.isDriver(oldRequest.requestInfo().mode()) && Mode.isDriver(requestInfo.mode()) == false) { - for(SqlTypedParamValue param : oldRequest.params()) { + for (SqlTypedParamValue param : oldRequest.params()) { param.hasExplicitType(false); } } if (Mode.isDriver(oldRequest.requestInfo().mode()) == false && Mode.isDriver(requestInfo.mode())) { - for(SqlTypedParamValue param : oldRequest.params()) { + for (SqlTypedParamValue param : oldRequest.params()) { param.hasExplicitType(true); } } @@ -181,12 +213,13 @@ public List randomParameters() { List arr = new ArrayList<>(len); boolean hasExplicitType = Mode.isDriver(this.requestInfo.mode()); for (int i = 0; i < len; i++) { - @SuppressWarnings("unchecked") Supplier supplier = randomFrom( - () -> new SqlTypedParamValue("boolean", randomBoolean(), hasExplicitType), - () -> new SqlTypedParamValue("long", randomLong(), hasExplicitType), - () -> new SqlTypedParamValue("double", randomDouble(), hasExplicitType), - () -> new SqlTypedParamValue("null", null, hasExplicitType), - () -> new SqlTypedParamValue("keyword", randomAlphaOfLength(10), hasExplicitType) + @SuppressWarnings("unchecked") + Supplier supplier = randomFrom( + () -> new SqlTypedParamValue("boolean", randomBoolean(), hasExplicitType), + () -> new SqlTypedParamValue("long", randomLong(), hasExplicitType), + () -> new SqlTypedParamValue("double", randomDouble(), hasExplicitType), + () -> new SqlTypedParamValue("null", null, hasExplicitType), + () -> new SqlTypedParamValue("keyword", randomAlphaOfLength(10), hasExplicitType) ); arr.add(supplier.get()); } diff --git a/x-pack/plugin/sql/sql-action/src/test/java/org/elasticsearch/xpack/sql/action/SqlQueryResponseTests.java b/x-pack/plugin/sql/sql-action/src/test/java/org/elasticsearch/xpack/sql/action/SqlQueryResponseTests.java index 8eff11810fc44..c655bddfd27a6 100644 --- a/x-pack/plugin/sql/sql-action/src/test/java/org/elasticsearch/xpack/sql/action/SqlQueryResponseTests.java +++ b/x-pack/plugin/sql/sql-action/src/test/java/org/elasticsearch/xpack/sql/action/SqlQueryResponseTests.java @@ -9,12 +9,12 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.sql.proto.ColumnInfo; import org.elasticsearch.xpack.sql.proto.Mode; @@ -42,8 +42,14 @@ static String randomStringCursor() { @Override protected SqlQueryResponse createTestInstance() { - return createRandomInstance(randomStringCursor(), randomFrom(Mode.values()), randomBoolean(), - rarely() ? null : randomAlphaOfLength(100), randomBoolean(), randomBoolean()); + return createRandomInstance( + randomStringCursor(), + randomFrom(Mode.values()), + randomBoolean(), + rarely() ? null : randomAlphaOfLength(100), + randomBoolean(), + randomBoolean() + ); } @Override @@ -51,16 +57,28 @@ protected Writeable.Reader instanceReader() { return SqlQueryResponse::new; } - public static SqlQueryResponse createRandomInstance(String cursor, Mode mode, boolean columnar, String asyncExecutionId, - boolean isPartial, boolean isRunning) { + public static SqlQueryResponse createRandomInstance( + String cursor, + Mode mode, + boolean columnar, + String asyncExecutionId, + boolean isPartial, + boolean isRunning + ) { int columnCount = between(1, 10); List columns = null; if (randomBoolean()) { columns = new ArrayList<>(columnCount); for (int i = 0; i < columnCount; i++) { - columns.add(new ColumnInfo(randomAlphaOfLength(10), randomAlphaOfLength(10), randomAlphaOfLength(10), - randomBoolean() ? null : randomInt(25))); + columns.add( + new ColumnInfo( + randomAlphaOfLength(10), + randomAlphaOfLength(10), + randomAlphaOfLength(10), + randomBoolean() ? null : randomInt(25) + ) + ); } } @@ -79,11 +97,9 @@ public static SqlQueryResponse createRandomInstance(String cursor, Mode mode, bo for (int r = 0; r < rowCount; r++) { List row = new ArrayList<>(rowCount); for (int c = 0; c < columnCount; c++) { - Supplier value = randomFrom(Arrays.asList( - () -> randomAlphaOfLength(10), - ESTestCase::randomLong, - ESTestCase::randomDouble, - () -> null)); + Supplier value = randomFrom( + Arrays.asList(() -> randomAlphaOfLength(10), ESTestCase::randomLong, ESTestCase::randomDouble, () -> null) + ); row.add(value.get()); } rows.add(row); @@ -140,9 +156,19 @@ public void testToXContent() throws IOException { @Override protected SqlQueryResponse doParseInstance(XContentParser parser) { - org.elasticsearch.xpack.sql.proto.SqlQueryResponse response = - org.elasticsearch.xpack.sql.proto.SqlQueryResponse.fromXContent(parser); - return new SqlQueryResponse(response.cursor(), Mode.JDBC, DATE_NANOS_SUPPORT_VERSION, false, response.columns(), response.rows(), - response.id(), response.isPartial(), response.isRunning()); + org.elasticsearch.xpack.sql.proto.SqlQueryResponse response = org.elasticsearch.xpack.sql.proto.SqlQueryResponse.fromXContent( + parser + ); + return new SqlQueryResponse( + response.cursor(), + Mode.JDBC, + DATE_NANOS_SUPPORT_VERSION, + false, + response.columns(), + response.rows(), + response.id(), + response.isPartial(), + response.isRunning() + ); } } diff --git a/x-pack/plugin/sql/sql-action/src/test/java/org/elasticsearch/xpack/sql/action/SqlRequestParsersTests.java b/x-pack/plugin/sql/sql-action/src/test/java/org/elasticsearch/xpack/sql/action/SqlRequestParsersTests.java index c0f4f06941546..9c1a6d73da952 100644 --- a/x-pack/plugin/sql/sql-action/src/test/java/org/elasticsearch/xpack/sql/action/SqlRequestParsersTests.java +++ b/x-pack/plugin/sql/sql-action/src/test/java/org/elasticsearch/xpack/sql/action/SqlRequestParsersTests.java @@ -9,12 +9,12 @@ import org.elasticsearch.Version; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParseException; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.sql.proto.Mode; import org.elasticsearch.xpack.sql.proto.SqlTypedParamValue; @@ -35,32 +35,46 @@ public void testUnknownFieldParsingErrors() throws IOException { } public void testUnknownModeFieldParsingErrors() throws IOException { - assertParsingErrorMessageReason("{\"cursor\":\"foo\",\"mode\" : \"value\"}", - "No enum constant org.elasticsearch.xpack.sql.proto.Mode.VALUE", SqlClearCursorRequest::fromXContent); - assertParsingErrorMessageReason("{\"cursor\":\"foo\",\"mode\" : \"value\"}", - "No enum constant org.elasticsearch.xpack.sql.proto.Mode.VALUE", SqlQueryRequest::fromXContent); - assertParsingErrorMessageReason("{\"mode\" : \"value\"}", - "No enum constant org.elasticsearch.xpack.sql.proto.Mode.VALUE", SqlTranslateRequest::fromXContent); + assertParsingErrorMessageReason( + "{\"cursor\":\"foo\",\"mode\" : \"value\"}", + "No enum constant org.elasticsearch.xpack.sql.proto.Mode.VALUE", + SqlClearCursorRequest::fromXContent + ); + assertParsingErrorMessageReason( + "{\"cursor\":\"foo\",\"mode\" : \"value\"}", + "No enum constant org.elasticsearch.xpack.sql.proto.Mode.VALUE", + SqlQueryRequest::fromXContent + ); + assertParsingErrorMessageReason( + "{\"mode\" : \"value\"}", + "No enum constant org.elasticsearch.xpack.sql.proto.Mode.VALUE", + SqlTranslateRequest::fromXContent + ); } public void testClearCursorRequestParser() throws IOException { assertParsingErrorMessage("{\"mode\" : \"jdbc\"}", "Required [cursor]", SqlClearCursorRequest::fromXContent); - assertParsingErrorMessage("{\"cursor\" : \"whatever\", \"fetch_size\":123}", "unknown field [fetch_size]", - SqlClearCursorRequest::fromXContent); + assertParsingErrorMessage( + "{\"cursor\" : \"whatever\", \"fetch_size\":123}", + "unknown field [fetch_size]", + SqlClearCursorRequest::fromXContent + ); Mode randomMode = randomFrom(Mode.values()); - SqlClearCursorRequest request = generateRequest("{\"cursor\" : \"whatever\", \"mode\" : \"" - + randomMode.toString() + "\", \"client_id\" : \"bla\", \"version\": \"1.2.3\"}", - SqlClearCursorRequest::fromXContent); + SqlClearCursorRequest request = generateRequest( + "{\"cursor\" : \"whatever\", \"mode\" : \"" + randomMode.toString() + "\", \"client_id\" : \"bla\", \"version\": \"1.2.3\"}", + SqlClearCursorRequest::fromXContent + ); assertNull(request.clientId()); assertNull(request.version()); assertEquals(randomMode, request.mode()); assertEquals("whatever", request.getCursor()); randomMode = randomFrom(Mode.values()); - request = generateRequest("{\"cursor\" : \"whatever\", \"mode\" : \"" - + randomMode.toString() + "\", \"client_id\" : \"bla\"}", - SqlClearCursorRequest::fromXContent); + request = generateRequest( + "{\"cursor\" : \"whatever\", \"mode\" : \"" + randomMode.toString() + "\", \"client_id\" : \"bla\"}", + SqlClearCursorRequest::fromXContent + ); assertNull(request.clientId()); assertEquals(randomMode, request.mode()); assertEquals("whatever", request.getCursor()); @@ -70,15 +84,16 @@ public void testClearCursorRequestParser() throws IOException { assertEquals(Mode.PLAIN, request.mode()); assertEquals("whatever", request.getCursor()); - request = generateRequest("{\"cursor\" : \"whatever\", \"client_id\" : \"CLI\", \"version\": \"1.2.3\"}", - SqlClearCursorRequest::fromXContent); + request = generateRequest( + "{\"cursor\" : \"whatever\", \"client_id\" : \"CLI\", \"version\": \"1.2.3\"}", + SqlClearCursorRequest::fromXContent + ); assertNull(request.clientId()); assertNull(request.version()); assertEquals(Mode.PLAIN, request.mode()); assertEquals("whatever", request.getCursor()); - request = generateRequest("{\"cursor\" : \"whatever\", \"client_id\" : \"cANVAs\"}", - SqlClearCursorRequest::fromXContent); + request = generateRequest("{\"cursor\" : \"whatever\", \"client_id\" : \"cANVAs\"}", SqlClearCursorRequest::fromXContent); assertEquals("canvas", request.clientId()); assertEquals(Mode.PLAIN, request.mode()); assertEquals("whatever", request.getCursor()); @@ -92,32 +107,43 @@ public void testTranslateRequestParser() throws IOException { assertEquals(Mode.PLAIN, request.mode()); Mode randomMode = randomFrom(Mode.values()); - request = generateRequest("{\"query\" : \"whatever\", \"client_id\" : \"foo\", \"mode\":\"" - + randomMode.toString() + "\"}", - SqlTranslateRequest::fromXContent); + request = generateRequest( + "{\"query\" : \"whatever\", \"client_id\" : \"foo\", \"mode\":\"" + randomMode.toString() + "\"}", + SqlTranslateRequest::fromXContent + ); assertNull(request.clientId()); assertEquals(randomMode, request.mode()); } public void testQueryRequestParser() throws IOException { assertParsingErrorMessage("{\"mode\" : 123}", "mode doesn't support values of type: VALUE_NUMBER", SqlQueryRequest::fromXContent); - assertParsingErrorMessage("{\"cursor\" : \"whatever\", \"fetch_size\":\"abc\"}", "failed to parse field [fetch_size]", - SqlQueryRequest::fromXContent); - assertParsingErrorMessage("{\"client_id\":123}", "client_id doesn't support values of type: VALUE_NUMBER", - SqlQueryRequest::fromXContent); - assertParsingErrorMessage("{\"version\":123}", "version doesn't support values of type: VALUE_NUMBER", - SqlQueryRequest::fromXContent); + assertParsingErrorMessage( + "{\"cursor\" : \"whatever\", \"fetch_size\":\"abc\"}", + "failed to parse field [fetch_size]", + SqlQueryRequest::fromXContent + ); + assertParsingErrorMessage( + "{\"client_id\":123}", + "client_id doesn't support values of type: VALUE_NUMBER", + SqlQueryRequest::fromXContent + ); + assertParsingErrorMessage( + "{\"version\":123}", + "version doesn't support values of type: VALUE_NUMBER", + SqlQueryRequest::fromXContent + ); assertParsingErrorMessage("{\"params\":[{\"value\":123}]}", "failed to parse field [params]", SqlQueryRequest::fromXContent); - assertParsingErrorMessage("{\"time_zone\":12}", "time_zone doesn't support values of type: VALUE_NUMBER", - SqlQueryRequest::fromXContent); + assertParsingErrorMessage( + "{\"time_zone\":12}", + "time_zone doesn't support values of type: VALUE_NUMBER", + SqlQueryRequest::fromXContent + ); Mode randomMode = randomFrom(Mode.values()); String params; List list = new ArrayList<>(1); - final String clientVersion = Mode.isDedicatedClient(randomMode) - ? "\"version\": \"" + Version.CURRENT.toString() + "\"," - : ""; + final String clientVersion = Mode.isDedicatedClient(randomMode) ? "\"version\": \"" + Version.CURRENT.toString() + "\"," : ""; if (Mode.isDriver(randomMode)) { params = "{\"value\":123, \"type\":\"whatever\"}"; list.add(new SqlTypedParamValue("whatever", 123, true)); @@ -126,13 +152,19 @@ public void testQueryRequestParser() throws IOException { list.add(new SqlTypedParamValue("integer", 123, false)); } - SqlQueryRequest request = generateRequest("{\"cursor\" : \"whatever\", \"mode\" : \"" - + randomMode.toString() + "\", \"client_id\" : \"bla\"," + SqlQueryRequest request = generateRequest( + "{\"cursor\" : \"whatever\", \"mode\" : \"" + + randomMode.toString() + + "\", \"client_id\" : \"bla\"," + clientVersion + "\"query\":\"select\"," - + "\"params\":[" + params + "]," + + "\"params\":[" + + params + + "]," + " \"time_zone\":\"UTC\"," - + "\"request_timeout\":\"5s\",\"page_timeout\":\"10s\"}", SqlQueryRequest::fromXContent); + + "\"request_timeout\":\"5s\",\"page_timeout\":\"10s\"}", + SqlQueryRequest::fromXContent + ); assertNull(request.clientId()); assertEquals(randomMode, request.mode()); if (Mode.isDedicatedClient(randomMode)) { @@ -149,13 +181,15 @@ public void testQueryRequestParser() throws IOException { public void testParamsSuccessfulParsingInDriverMode() throws IOException { Mode driverMode = randomValueOtherThanMany((m) -> Mode.isDriver(m) == false, () -> randomFrom(Mode.values())); - String json = "{" + - " \"params\":[{\"type\":\"integer\",\"value\":35000}," - + " {\"type\":\"date\",\"value\":\"1960-01-01\"}," - + " {\"type\":\"boolean\",\"value\":false}," - + " {\"type\":\"keyword\",\"value\":\"foo\"}]," + - " \"mode\": \"" + driverMode.toString() + "\"" + - "}"; + String json = "{" + + " \"params\":[{\"type\":\"integer\",\"value\":35000}," + + " {\"type\":\"date\",\"value\":\"1960-01-01\"}," + + " {\"type\":\"boolean\",\"value\":false}," + + " {\"type\":\"keyword\",\"value\":\"foo\"}]," + + " \"mode\": \"" + + driverMode.toString() + + "\"" + + "}"; SqlQueryRequest request = generateRequest(json, SqlQueryRequest::fromXContent); List params = request.params(); assertEquals(4, params.size()); @@ -179,10 +213,12 @@ public void testParamsSuccessfulParsingInDriverMode() throws IOException { public void testParamsSuccessfulParsingInNonDriverMode() throws IOException { Mode nonDriverMode = randomValueOtherThanMany(Mode::isDriver, () -> randomFrom(Mode.values())); - String json = "{" + - " \"params\":[35000,\"1960-01-01\",false,\"foo\"]," + - " \"mode\": \"" + nonDriverMode.toString() + "\"" + - "}"; + String json = "{" + + " \"params\":[35000,\"1960-01-01\",false,\"foo\"]," + + " \"mode\": \"" + + nonDriverMode.toString() + + "\"" + + "}"; SqlQueryRequest request = generateRequest(json, SqlQueryRequest::fromXContent); List params = request.params(); assertEquals(4, params.size()); @@ -206,54 +242,71 @@ public void testParamsSuccessfulParsingInNonDriverMode() throws IOException { public void testParamsParsingFailure_QueryRequest_NonDriver() throws IOException { Mode m = randomValueOtherThanMany(Mode::isDriver, () -> randomFrom(Mode.values())); - assertXContentParsingErrorMessage("{\"params\":[{\"whatever\":35000},\"1960-01-01\",false,\"foo\"],\"mode\": \"" - + m.toString() + "\"}", - "[sql/query] failed to parse field [params]", - SqlQueryRequest::fromXContent); - assertXContentParsingErrorMessage("{\"params\":[350.123,\"1960-01-01\",{\"foobar\":false},\"foo\"],\"mode\": \"}" - + m.toString() + "\"}", - "[sql/query] failed to parse field [params]", - SqlQueryRequest::fromXContent); - assertXContentParsingErrorMessage("{\"mode\": \"" + m.toString() + "\",\"params\":[350.123,\"1960-01-01\",false," - + "{\"type\":\"keyword\",\"value\":\"foo\"}]}", - "[params] must be an array where each entry is a single field (no objects supported)", - SqlQueryRequest::fromXContent); + assertXContentParsingErrorMessage( + "{\"params\":[{\"whatever\":35000},\"1960-01-01\",false,\"foo\"],\"mode\": \"" + m.toString() + "\"}", + "[sql/query] failed to parse field [params]", + SqlQueryRequest::fromXContent + ); + assertXContentParsingErrorMessage( + "{\"params\":[350.123,\"1960-01-01\",{\"foobar\":false},\"foo\"],\"mode\": \"}" + m.toString() + "\"}", + "[sql/query] failed to parse field [params]", + SqlQueryRequest::fromXContent + ); + assertXContentParsingErrorMessage( + "{\"mode\": \"" + m.toString() + "\",\"params\":[350.123,\"1960-01-01\",false," + "{\"type\":\"keyword\",\"value\":\"foo\"}]}", + "[params] must be an array where each entry is a single field (no objects supported)", + SqlQueryRequest::fromXContent + ); } public void testParamsParsingFailure_TranslateRequest_NonDriver() throws IOException { Mode m = randomValueOtherThanMany(Mode::isDriver, () -> randomFrom(Mode.values())); - assertXContentParsingErrorMessage("{\"params\":[{\"whatever\":35000},\"1960-01-01\",false,\"foo\"],\"mode\": \"" - + m.toString() + "\"}", - "[sql/query] failed to parse field [params]", - SqlTranslateRequest::fromXContent); - assertXContentParsingErrorMessage("{\"params\":[350.123,\"1960-01-01\",{\"foobar\":false},\"foo\"],\"mode\": \"}" - + m.toString() + "\"}", - "[sql/query] failed to parse field [params]", - SqlTranslateRequest::fromXContent); - assertXContentParsingErrorMessage("{\"mode\": \"" + m.toString() + "\",\"params\":[350.123,\"1960-01-01\",false," - + "{\"type\":\"keyword\",\"value\":\"foo\"}]}", - "[params] must be an array where each entry is a single field (no objects supported)", - SqlTranslateRequest::fromXContent); + assertXContentParsingErrorMessage( + "{\"params\":[{\"whatever\":35000},\"1960-01-01\",false,\"foo\"],\"mode\": \"" + m.toString() + "\"}", + "[sql/query] failed to parse field [params]", + SqlTranslateRequest::fromXContent + ); + assertXContentParsingErrorMessage( + "{\"params\":[350.123,\"1960-01-01\",{\"foobar\":false},\"foo\"],\"mode\": \"}" + m.toString() + "\"}", + "[sql/query] failed to parse field [params]", + SqlTranslateRequest::fromXContent + ); + assertXContentParsingErrorMessage( + "{\"mode\": \"" + m.toString() + "\",\"params\":[350.123,\"1960-01-01\",false," + "{\"type\":\"keyword\",\"value\":\"foo\"}]}", + "[params] must be an array where each entry is a single field (no objects supported)", + SqlTranslateRequest::fromXContent + ); } public void testParamsParsingFailure_Driver() throws IOException { Mode m = randomValueOtherThanMany((t) -> Mode.isDriver(t) == false, () -> randomFrom(Mode.values())); - assertXContentParsingErrorMessage("{\"params\":[35000,{\"value\":\"1960-01-01\",\"type\":\"date\"},{\"value\":\"foo\"," - + "\"type\":\"keyword\"}],\"mode\": \"" + m.toString() + "\"}", - "[params] must be an array where each entry is an object with a value/type pair", - SqlQueryRequest::fromXContent); - assertXContentParsingErrorMessage("{\"params\":[{\"value\":10,\"type\":\"integer\"},{\"value\":\"1960-01-01\",\"type\":\"date\"}," - + "false,\"foo\"],\"mode\": \"" + m.toString() + "\"}", - "[params] must be an array where each entry is an object with a value/type pair", - SqlQueryRequest::fromXContent); - assertXContentParsingErrorMessage("{\"mode\": \"" + m.toString() + "\",\"params\":[{\"value\":10,\"type\":\"integer\"}," + assertXContentParsingErrorMessage( + "{\"params\":[35000,{\"value\":\"1960-01-01\",\"type\":\"date\"},{\"value\":\"foo\"," + + "\"type\":\"keyword\"}],\"mode\": \"" + + m.toString() + + "\"}", + "[params] must be an array where each entry is an object with a value/type pair", + SqlQueryRequest::fromXContent + ); + assertXContentParsingErrorMessage( + "{\"params\":[{\"value\":10,\"type\":\"integer\"},{\"value\":\"1960-01-01\",\"type\":\"date\"}," + + "false,\"foo\"],\"mode\": \"" + + m.toString() + + "\"}", + "[params] must be an array where each entry is an object with a value/type pair", + SqlQueryRequest::fromXContent + ); + assertXContentParsingErrorMessage( + "{\"mode\": \"" + + m.toString() + + "\",\"params\":[{\"value\":10,\"type\":\"integer\"}," + "{\"value\":\"1960-01-01\",\"type\":\"date\"},{\"foo\":\"bar\"}]}", - "[sql/query] failed to parse field [params]", - SqlQueryRequest::fromXContent); + "[sql/query] failed to parse field [params]", + SqlQueryRequest::fromXContent + ); } - private R generateRequest(String json, Function fromXContent) - throws IOException { + private R generateRequest(String json, Function fromXContent) throws IOException { XContentParser parser = parser(json); return fromXContent.apply(parser); } @@ -278,7 +331,6 @@ private void assertXContentParsingErrorMessage(String json, String errorMessage, private XContentParser parser(String content) throws IOException { XContentType xContentType = XContentType.JSON; - return xContentType.xContent().createParser( - NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, content); + return xContentType.xContent().createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, content); } } diff --git a/x-pack/plugin/sql/sql-action/src/test/java/org/elasticsearch/xpack/sql/action/SqlTestUtils.java b/x-pack/plugin/sql/sql-action/src/test/java/org/elasticsearch/xpack/sql/action/SqlTestUtils.java index f0e48bd54bf85..b20b141405ad2 100644 --- a/x-pack/plugin/sql/sql-action/src/test/java/org/elasticsearch/xpack/sql/action/SqlTestUtils.java +++ b/x-pack/plugin/sql/sql-action/src/test/java/org/elasticsearch/xpack/sql/action/SqlTestUtils.java @@ -7,6 +7,7 @@ package org.elasticsearch.xpack.sql.action; import com.carrotsearch.randomizedtesting.generators.RandomStrings; + import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.RangeQueryBuilder; @@ -35,8 +36,7 @@ public static QueryBuilder randomFilterOrNull(Random random) { * Returns a random QueryBuilder */ public static QueryBuilder randomFilter(Random random) { - return new RangeQueryBuilder(RandomStrings.randomAsciiLettersOfLength(random, 10)) - .gt(random.nextInt()); + return new RangeQueryBuilder(RandomStrings.randomAsciiLettersOfLength(random, 10)).gt(random.nextInt()); } } diff --git a/x-pack/plugin/sql/sql-action/src/test/java/org/elasticsearch/xpack/sql/action/SqlTranslateRequestTests.java b/x-pack/plugin/sql/sql-action/src/test/java/org/elasticsearch/xpack/sql/action/SqlTranslateRequestTests.java index f6a58203cbad8..8e16b61bb3fb1 100644 --- a/x-pack/plugin/sql/sql-action/src/test/java/org/elasticsearch/xpack/sql/action/SqlTranslateRequestTests.java +++ b/x-pack/plugin/sql/sql-action/src/test/java/org/elasticsearch/xpack/sql/action/SqlTranslateRequestTests.java @@ -10,11 +10,11 @@ import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.search.SearchModule; import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.sql.proto.Mode; import org.elasticsearch.xpack.sql.proto.RequestInfo; import org.junit.Before; @@ -38,8 +38,17 @@ public void setup() { @Override protected SqlTranslateRequest createTestInstance() { - return new SqlTranslateRequest(randomAlphaOfLength(10), emptyList(), randomFilterOrNull(random()), - randomRuntimeMappings(),randomZone(), between(1, Integer.MAX_VALUE), randomTV(), randomTV(), new RequestInfo(testMode)); + return new SqlTranslateRequest( + randomAlphaOfLength(10), + emptyList(), + randomFilterOrNull(random()), + randomRuntimeMappings(), + randomZone(), + between(1, Integer.MAX_VALUE), + randomTV(), + randomTV(), + new RequestInfo(testMode) + ); } @Override @@ -72,18 +81,30 @@ protected SqlTranslateRequest doParseInstance(XContentParser parser) { protected SqlTranslateRequest mutateInstance(SqlTranslateRequest instance) throws IOException { @SuppressWarnings("unchecked") Consumer mutator = randomFrom( - request -> request.query(randomValueOtherThan(request.query(), () -> randomAlphaOfLength(5))), - request -> request.zoneId(randomValueOtherThan(request.zoneId(), ESTestCase::randomZone)), - request -> request.catalog(randomValueOtherThan(request.catalog(), () -> randomAlphaOfLength(10))), - request -> request.fetchSize(randomValueOtherThan(request.fetchSize(), () -> between(1, Integer.MAX_VALUE))), - request -> request.requestTimeout(randomValueOtherThan(request.requestTimeout(), this::randomTV)), - request -> request.filter(randomValueOtherThan(request.filter(), - () -> request.filter() == null ? randomFilter(random()) : randomFilterOrNull(random()))), - request -> request.runtimeMappings(randomValueOtherThan(request.runtimeMappings(), () -> randomRuntimeMappings())) + request -> request.query(randomValueOtherThan(request.query(), () -> randomAlphaOfLength(5))), + request -> request.zoneId(randomValueOtherThan(request.zoneId(), ESTestCase::randomZone)), + request -> request.catalog(randomValueOtherThan(request.catalog(), () -> randomAlphaOfLength(10))), + request -> request.fetchSize(randomValueOtherThan(request.fetchSize(), () -> between(1, Integer.MAX_VALUE))), + request -> request.requestTimeout(randomValueOtherThan(request.requestTimeout(), this::randomTV)), + request -> request.filter( + randomValueOtherThan( + request.filter(), + () -> request.filter() == null ? randomFilter(random()) : randomFilterOrNull(random()) + ) + ), + request -> request.runtimeMappings(randomValueOtherThan(request.runtimeMappings(), () -> randomRuntimeMappings())) + ); + SqlTranslateRequest newRequest = new SqlTranslateRequest( + instance.query(), + instance.params(), + instance.filter(), + instance.runtimeMappings(), + instance.zoneId(), + instance.fetchSize(), + instance.requestTimeout(), + instance.pageTimeout(), + instance.requestInfo() ); - SqlTranslateRequest newRequest = new SqlTranslateRequest(instance.query(), instance.params(), instance.filter(), - instance.runtimeMappings(), instance.zoneId(), instance.fetchSize(), instance.requestTimeout(), instance.pageTimeout(), - instance.requestInfo()); mutator.accept(newRequest); return newRequest; } diff --git a/x-pack/plugin/sql/sql-cli/src/main/java/org/elasticsearch/xpack/sql/cli/Cli.java b/x-pack/plugin/sql/sql-cli/src/main/java/org/elasticsearch/xpack/sql/cli/Cli.java index d1194dbe58b77..d60e83bc4b536 100644 --- a/x-pack/plugin/sql/sql-cli/src/main/java/org/elasticsearch/xpack/sql/cli/Cli.java +++ b/x-pack/plugin/sql/sql-cli/src/main/java/org/elasticsearch/xpack/sql/cli/Cli.java @@ -51,12 +51,17 @@ public class Cli extends LoggingAwareCommand { */ public static void main(String[] args) throws Exception { configureJLineLogging(); - final Cli cli = new Cli(new JLineTerminal(TerminalBuilder.builder() - .name("Elasticsearch SQL CLI") - // remove jansi since it has issues on Windows in closing terminals - // the CLI uses JNA anyway - .jansi(false) - .build(), true)); + final Cli cli = new Cli( + new JLineTerminal( + TerminalBuilder.builder() + .name("Elasticsearch SQL CLI") + // remove jansi since it has issues on Windows in closing terminals + // the CLI uses JNA anyway + .jansi(false) + .build(), + true + ) + ); int status = cli.main(args, Terminal.DEFAULT); if (status != ExitCodes.OK) { exit(status); @@ -82,20 +87,20 @@ public Cli(CliTerminal cliTerminal) { super("Elasticsearch SQL CLI"); this.cliTerminal = cliTerminal; parser.acceptsAll(Arrays.asList("d", "debug"), "Enable debug logging"); - this.binaryCommunication = parser.acceptsAll(Arrays.asList("b", "binary"), "Disable binary communication. " - + "Enabled by default. Accepts 'true' or 'false' values.") - .withRequiredArg().ofType(Boolean.class) - .defaultsTo(Boolean.parseBoolean(System.getProperty("binary", "true"))); + this.binaryCommunication = parser.acceptsAll( + Arrays.asList("b", "binary"), + "Disable binary communication. " + "Enabled by default. Accepts 'true' or 'false' values." + ).withRequiredArg().ofType(Boolean.class).defaultsTo(Boolean.parseBoolean(System.getProperty("binary", "true"))); this.keystoreLocation = parser.acceptsAll( - Arrays.asList("k", "keystore_location"), - "Location of a keystore to use when setting up SSL. " + Arrays.asList("k", "keystore_location"), + "Location of a keystore to use when setting up SSL. " + "If specified then the CLI will prompt for a keystore password. " - + "If specified when the uri isn't https then an error is thrown.") - .withRequiredArg().ofType(String.class); - this.checkOption = parser.acceptsAll(Arrays.asList("c", "check"), - "Enable initial connection check on startup") - .withRequiredArg().ofType(Boolean.class) - .defaultsTo(Boolean.parseBoolean(System.getProperty("cli.check", "true"))); + + "If specified when the uri isn't https then an error is thrown." + ).withRequiredArg().ofType(String.class); + this.checkOption = parser.acceptsAll(Arrays.asList("c", "check"), "Enable initial connection check on startup") + .withRequiredArg() + .ofType(Boolean.class) + .defaultsTo(Boolean.parseBoolean(System.getProperty("cli.check", "true"))); this.connectionString = parser.nonOptions("uri"); } @@ -119,12 +124,12 @@ protected void execute(org.elasticsearch.cli.Terminal terminal, OptionSet option private void execute(String uri, boolean debug, boolean binary, String keystoreLocation, boolean checkConnection) throws Exception { CliCommand cliCommand = new CliCommands( - new PrintLogoCommand(), - new ClearScreenCliCommand(), - new FetchSizeCliCommand(), - new FetchSeparatorCliCommand(), - new ServerInfoCliCommand(), - new ServerQueryCliCommand() + new PrintLogoCommand(), + new ClearScreenCliCommand(), + new FetchSizeCliCommand(), + new FetchSeparatorCliCommand(), + new ServerInfoCliCommand(), + new ServerQueryCliCommand() ); try { ConnectionBuilder connectionBuilder = new ConnectionBuilder(cliTerminal); @@ -152,17 +157,24 @@ private void checkConnection(CliSession cliSession, CliTerminal cliTerminal, Con } if (ex.getCause() != null && ex.getCause() instanceof ConnectException) { // Most likely Elasticsearch is not running - throw new UserException(ExitCodes.IO_ERROR, - "Cannot connect to the server " + con.connectionString() + " - " + ex.getCause().getMessage()); + throw new UserException( + ExitCodes.IO_ERROR, + "Cannot connect to the server " + con.connectionString() + " - " + ex.getCause().getMessage() + ); } else if (ex.getCause() != null && ex.getCause() instanceof SQLInvalidAuthorizationSpecException) { - throw new UserException(ExitCodes.NOPERM, - "Cannot establish a secure connection to the server " + - con.connectionString() + " - " + ex.getCause().getMessage()); + throw new UserException( + ExitCodes.NOPERM, + "Cannot establish a secure connection to the server " + con.connectionString() + " - " + ex.getCause().getMessage() + ); } else { // Most likely we connected to something other than Elasticsearch - throw new UserException(ExitCodes.DATA_ERROR, - "Cannot communicate with the server " + con.connectionString() + - ". This version of CLI only works with Elasticsearch version " + ClientVersion.CURRENT.toString()); + throw new UserException( + ExitCodes.DATA_ERROR, + "Cannot communicate with the server " + + con.connectionString() + + ". This version of CLI only works with Elasticsearch version " + + ClientVersion.CURRENT.toString() + ); } } } diff --git a/x-pack/plugin/sql/sql-cli/src/main/java/org/elasticsearch/xpack/sql/cli/CliTerminal.java b/x-pack/plugin/sql/sql-cli/src/main/java/org/elasticsearch/xpack/sql/cli/CliTerminal.java index 9190f72acd468..7d66547255638 100644 --- a/x-pack/plugin/sql/sql-cli/src/main/java/org/elasticsearch/xpack/sql/cli/CliTerminal.java +++ b/x-pack/plugin/sql/sql-cli/src/main/java/org/elasticsearch/xpack/sql/cli/CliTerminal.java @@ -6,9 +6,10 @@ */ package org.elasticsearch.xpack.sql.cli; -import java.io.IOException; import org.elasticsearch.cli.UserException; +import java.io.IOException; + /** * Represents a terminal endpoint */ diff --git a/x-pack/plugin/sql/sql-cli/src/main/java/org/elasticsearch/xpack/sql/cli/Completers.java b/x-pack/plugin/sql/sql-cli/src/main/java/org/elasticsearch/xpack/sql/cli/Completers.java index 09cbee08f4fcb..c99b60ff4d8ec 100644 --- a/x-pack/plugin/sql/sql-cli/src/main/java/org/elasticsearch/xpack/sql/cli/Completers.java +++ b/x-pack/plugin/sql/sql-cli/src/main/java/org/elasticsearch/xpack/sql/cli/Completers.java @@ -12,9 +12,10 @@ import org.jline.reader.impl.completer.StringsCompleter; class Completers { - //TODO: need tree structure + // TODO: need tree structure static final Completer INSTANCE = new AggregateCompleter( - new ArgumentCompleter(new StringsCompleter("", "EXPLAIN", "SHOW", "SELECT", "SET")), - new ArgumentCompleter(new StringsCompleter("SHOW", "TABLE", "COLUMNS", "FUNCTIONS"))); + new ArgumentCompleter(new StringsCompleter("", "EXPLAIN", "SHOW", "SELECT", "SET")), + new ArgumentCompleter(new StringsCompleter("SHOW", "TABLE", "COLUMNS", "FUNCTIONS")) + ); } diff --git a/x-pack/plugin/sql/sql-cli/src/main/java/org/elasticsearch/xpack/sql/cli/ConnectionBuilder.java b/x-pack/plugin/sql/sql-cli/src/main/java/org/elasticsearch/xpack/sql/cli/ConnectionBuilder.java index 20a6cac83a82b..4766548ada963 100644 --- a/x-pack/plugin/sql/sql-cli/src/main/java/org/elasticsearch/xpack/sql/cli/ConnectionBuilder.java +++ b/x-pack/plugin/sql/sql-cli/src/main/java/org/elasticsearch/xpack/sql/cli/ConnectionBuilder.java @@ -41,8 +41,8 @@ public ConnectionBuilder(CliTerminal cliTerminal) { * @param binaryCommunication should the communication between the CLI and server be binary (CBOR) * @throws UserException if there is a problem with the information provided by the user */ - public ConnectionConfiguration buildConnection(String connectionStringArg, String keystoreLocation, - boolean binaryCommunication) throws UserException { + public ConnectionConfiguration buildConnection(String connectionStringArg, String keystoreLocation, boolean binaryCommunication) + throws UserException { final URI uri; final String connectionString; Properties properties = new Properties(); @@ -112,10 +112,10 @@ protected ConnectionConfiguration newConnectionConfiguration(URI uri, String con protected void checkIfExists(String name, Path p) throws UserException { if (false == Files.exists(p)) { throw new UserException(ExitCodes.USAGE, name + " [" + p + "] doesn't exist"); - } - if (false == Files.isRegularFile(p)) { - throw new UserException(ExitCodes.USAGE, name + " [" + p + "] isn't a regular file"); - } + } + if (false == Files.isRegularFile(p)) { + throw new UserException(ExitCodes.USAGE, name + " [" + p + "] isn't a regular file"); + } } } diff --git a/x-pack/plugin/sql/sql-cli/src/main/java/org/elasticsearch/xpack/sql/cli/FatalCliException.java b/x-pack/plugin/sql/sql-cli/src/main/java/org/elasticsearch/xpack/sql/cli/FatalCliException.java index de41ebff53efb..69f628623f4cb 100644 --- a/x-pack/plugin/sql/sql-cli/src/main/java/org/elasticsearch/xpack/sql/cli/FatalCliException.java +++ b/x-pack/plugin/sql/sql-cli/src/main/java/org/elasticsearch/xpack/sql/cli/FatalCliException.java @@ -9,7 +9,7 @@ /** * Throwing this except will cause the CLI to terminate */ -public class FatalCliException extends RuntimeException { +public class FatalCliException extends RuntimeException { public FatalCliException(String message, Throwable cause) { super(message, cause); } diff --git a/x-pack/plugin/sql/sql-cli/src/main/java/org/elasticsearch/xpack/sql/cli/JLineTerminal.java b/x-pack/plugin/sql/sql-cli/src/main/java/org/elasticsearch/xpack/sql/cli/JLineTerminal.java index a56dfe45cc10e..40e23b67c9bcc 100644 --- a/x-pack/plugin/sql/sql-cli/src/main/java/org/elasticsearch/xpack/sql/cli/JLineTerminal.java +++ b/x-pack/plugin/sql/sql-cli/src/main/java/org/elasticsearch/xpack/sql/cli/JLineTerminal.java @@ -41,12 +41,7 @@ public class JLineTerminal implements CliTerminal { * enabled in production because it is fairly nice. */ public JLineTerminal(Terminal terminal, boolean enableMatchBracket) { - this(terminal, - LineReaderBuilder.builder() - .terminal(terminal) - .completer(Completers.INSTANCE) - .build(), - enableMatchBracket); + this(terminal, LineReaderBuilder.builder().terminal(terminal).completer(Completers.INSTANCE).build(), enableMatchBracket); } /** @@ -158,7 +153,6 @@ public LineBuilder em(String text) { return this; } - public LineBuilder error(String text) { line.append(text, BOLD.foreground(RED)); return this; diff --git a/x-pack/plugin/sql/sql-cli/src/main/java/org/elasticsearch/xpack/sql/cli/command/AbstractServerCliCommand.java b/x-pack/plugin/sql/sql-cli/src/main/java/org/elasticsearch/xpack/sql/cli/command/AbstractServerCliCommand.java index 23cda145f99d7..a3ede76da53a7 100644 --- a/x-pack/plugin/sql/sql-cli/src/main/java/org/elasticsearch/xpack/sql/cli/command/AbstractServerCliCommand.java +++ b/x-pack/plugin/sql/sql-cli/src/main/java/org/elasticsearch/xpack/sql/cli/command/AbstractServerCliCommand.java @@ -10,8 +10,7 @@ public abstract class AbstractServerCliCommand implements CliCommand { - public AbstractServerCliCommand() { - } + public AbstractServerCliCommand() {} @Override public final boolean handle(CliTerminal terminal, CliSession cliSession, String line) { @@ -30,12 +29,14 @@ public final boolean handle(CliTerminal terminal, CliSession cliSession, String * into a method so that tests can bubble the failure. */ protected void handleExceptionWhileCommunicatingWithServer(CliTerminal terminal, CliSession cliSession, RuntimeException e) { - terminal.line().error("Communication error [").param(e.getMessage() == null ? e.getClass().getName() : e.getMessage()).error("]") - .ln(); + terminal.line() + .error("Communication error [") + .param(e.getMessage() == null ? e.getClass().getName() : e.getMessage()) + .error("]") + .ln(); if (cliSession.isDebug()) { terminal.printStackTrace(e); } } - } diff --git a/x-pack/plugin/sql/sql-cli/src/main/java/org/elasticsearch/xpack/sql/cli/command/CliSession.java b/x-pack/plugin/sql/sql-cli/src/main/java/org/elasticsearch/xpack/sql/cli/command/CliSession.java index ed90bb68e8674..b76e2ba6dcef5 100644 --- a/x-pack/plugin/sql/sql-cli/src/main/java/org/elasticsearch/xpack/sql/cli/command/CliSession.java +++ b/x-pack/plugin/sql/sql-cli/src/main/java/org/elasticsearch/xpack/sql/cli/command/CliSession.java @@ -77,9 +77,12 @@ public void checkConnection() throws ClientException { } SqlVersion version = SqlVersion.fromString(response.getVersion()); if (ClientVersion.isServerCompatible(version) == false) { - throw new ClientException("This version of the CLI is only compatible with Elasticsearch version " + - ClientVersion.CURRENT.majorMinorToString() + " or newer; attempting to connect to a server version " + - version.toString()); + throw new ClientException( + "This version of the CLI is only compatible with Elasticsearch version " + + ClientVersion.CURRENT.majorMinorToString() + + " or newer; attempting to connect to a server version " + + version.toString() + ); } } } diff --git a/x-pack/plugin/sql/sql-cli/src/main/java/org/elasticsearch/xpack/sql/cli/command/ServerInfoCliCommand.java b/x-pack/plugin/sql/sql-cli/src/main/java/org/elasticsearch/xpack/sql/cli/command/ServerInfoCliCommand.java index ebe18ac49b909..cf6c3875b97be 100644 --- a/x-pack/plugin/sql/sql-cli/src/main/java/org/elasticsearch/xpack/sql/cli/command/ServerInfoCliCommand.java +++ b/x-pack/plugin/sql/sql-cli/src/main/java/org/elasticsearch/xpack/sql/cli/command/ServerInfoCliCommand.java @@ -14,8 +14,7 @@ public class ServerInfoCliCommand extends AbstractServerCliCommand { - public ServerInfoCliCommand() { - } + public ServerInfoCliCommand() {} @Override public boolean doHandle(CliTerminal terminal, CliSession cliSession, String line) { @@ -30,10 +29,13 @@ public boolean doHandle(CliTerminal terminal, CliSession cliSession, String line return true; } terminal.line() - .text("Node:").em(info.getNodeName()) - .text(" Cluster:").em(info.getClusterName()) - .text(" Version:").em(info.getVersion()) - .ln(); + .text("Node:") + .em(info.getNodeName()) + .text(" Cluster:") + .em(info.getClusterName()) + .text(" Version:") + .em(info.getVersion()) + .ln(); return true; } } diff --git a/x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/CliReplTests.java b/x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/CliReplTests.java index d46b82c03744e..de935c4cbfc6f 100644 --- a/x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/CliReplTests.java +++ b/x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/CliReplTests.java @@ -18,11 +18,7 @@ public class CliReplTests extends SqlCliTestCase { public void testBasicCliFunctionality() throws Exception { - CliTerminal cliTerminal = new TestTerminal( - "test;", - "notest;", - "exit;" - ); + CliTerminal cliTerminal = new TestTerminal("test;", "notest;", "exit;"); CliSession mockSession = mock(CliSession.class); CliCommand mockCommand = mock(CliCommand.class); when(mockCommand.handle(cliTerminal, mockSession, "logo")).thenReturn(true); @@ -43,13 +39,7 @@ public void testBasicCliFunctionality() throws Exception { * just new lines. */ public void testEmptyNotSent() { - CliTerminal cliTerminal = new TestTerminal( - ";", - "", - "", - ";", - "exit;" - ); + CliTerminal cliTerminal = new TestTerminal(";", "", "", ";", "exit;"); CliSession mockSession = mock(CliSession.class); CliCommand mockCommand = mock(CliCommand.class); @@ -62,10 +52,7 @@ public void testEmptyNotSent() { } public void testFatalCliExceptionHandling() throws Exception { - CliTerminal cliTerminal = new TestTerminal( - "test;", - "fail;" - ); + CliTerminal cliTerminal = new TestTerminal("test;", "fail;"); CliSession mockSession = mock(CliSession.class); CliCommand mockCommand = mock(CliCommand.class); diff --git a/x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/CliSessionTests.java b/x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/CliSessionTests.java index 29c2057ac77bd..bcb7fefec4559 100644 --- a/x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/CliSessionTests.java +++ b/x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/CliSessionTests.java @@ -29,8 +29,14 @@ public class CliSessionTests extends SqlCliTestCase { public void testProperConnection() throws Exception { HttpClient httpClient = mock(HttpClient.class); - when(httpClient.serverInfo()).thenReturn(new MainResponse(randomAlphaOfLength(5), ClientVersion.CURRENT.toString(), - ClusterName.DEFAULT.value(), UUIDs.randomBase64UUID())); + when(httpClient.serverInfo()).thenReturn( + new MainResponse( + randomAlphaOfLength(5), + ClientVersion.CURRENT.toString(), + ClusterName.DEFAULT.value(), + UUIDs.randomBase64UUID() + ) + ); CliSession cliSession = new CliSession(httpClient); cliSession.checkConnection(); verify(httpClient, times(1)).serverInfo(); @@ -51,13 +57,18 @@ public void testWrongServerVersion() throws Exception { HttpClient httpClient = mock(HttpClient.class); Version v = VersionUtils.randomVersionBetween(random(), null, VersionUtils.getPreviousVersion(Version.V_7_7_0)); SqlVersion version = new SqlVersion(v.major, v.minor, v.revision); - when(httpClient.serverInfo()).thenReturn(new MainResponse(randomAlphaOfLength(5), version.toString(), - ClusterName.DEFAULT.value(), UUIDs.randomBase64UUID())); + when(httpClient.serverInfo()).thenReturn( + new MainResponse(randomAlphaOfLength(5), version.toString(), ClusterName.DEFAULT.value(), UUIDs.randomBase64UUID()) + ); CliSession cliSession = new CliSession(httpClient); Throwable throwable = expectThrows(ClientException.class, cliSession::checkConnection); - assertEquals("This version of the CLI is only compatible with Elasticsearch version " + - ClientVersion.CURRENT.majorMinorToString() + " or newer; attempting to connect to a server version " + version.toString(), - throwable.getMessage()); + assertEquals( + "This version of the CLI is only compatible with Elasticsearch version " + + ClientVersion.CURRENT.majorMinorToString() + + " or newer; attempting to connect to a server version " + + version.toString(), + throwable.getMessage() + ); verify(httpClient, times(1)).serverInfo(); verifyNoMoreInteractions(httpClient); } @@ -66,8 +77,9 @@ public void testHigherServerVersion() throws Exception { HttpClient httpClient = mock(HttpClient.class); Version v = VersionUtils.randomVersionBetween(random(), Version.V_7_7_0, null); SqlVersion version = new SqlVersion(v.major, v.minor, v.revision); - when(httpClient.serverInfo()).thenReturn(new MainResponse(randomAlphaOfLength(5), version.toString(), - ClusterName.DEFAULT.value(), UUIDs.randomBase64UUID())); + when(httpClient.serverInfo()).thenReturn( + new MainResponse(randomAlphaOfLength(5), version.toString(), ClusterName.DEFAULT.value(), UUIDs.randomBase64UUID()) + ); CliSession cliSession = new CliSession(httpClient); cliSession.checkConnection(); verify(httpClient, times(1)).serverInfo(); diff --git a/x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/ConnectionBuilderTests.java b/x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/ConnectionBuilderTests.java index 8436b4a197c9a..c004dc869c680 100644 --- a/x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/ConnectionBuilderTests.java +++ b/x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/ConnectionBuilderTests.java @@ -89,8 +89,7 @@ protected void checkIfExists(String name, Path p) { } @Override - protected ConnectionConfiguration newConnectionConfiguration(URI uri, String connectionString, - Properties properties) { + protected ConnectionConfiguration newConnectionConfiguration(URI uri, String connectionString, Properties properties) { // Stub building the actual configuration because we don't have permission to read the keystore. assertEquals("true", properties.get(SslConfig.SSL)); assertEquals("keystore_location", properties.get(SslConfig.SSL_KEYSTORE_LOCATION)); @@ -113,8 +112,10 @@ public void testUserGaveUpOnPassword() throws Exception { UserException ue = new UserException(random().nextInt(), randomAlphaOfLength(5)); when(testTerminal.readPassword("password: ")).thenThrow(ue); ConnectionBuilder connectionBuilder = new ConnectionBuilder(testTerminal); - UserException actual = expectThrows(UserException.class, () -> - buildConnection(connectionBuilder, "http://user@foobar:9242/", null)); + UserException actual = expectThrows( + UserException.class, + () -> buildConnection(connectionBuilder, "http://user@foobar:9242/", null) + ); assertSame(actual, ue); } @@ -129,13 +130,15 @@ protected void checkIfExists(String name, Path p) { // Stubbed so we don't need permission to read the file } }; - UserException actual = expectThrows(UserException.class, () -> - buildConnection(connectionBuilder, "https://user@foobar:9242/", "keystore_location")); + UserException actual = expectThrows( + UserException.class, + () -> buildConnection(connectionBuilder, "https://user@foobar:9242/", "keystore_location") + ); assertSame(actual, ue); } - private ConnectionConfiguration buildConnection(ConnectionBuilder builder, String connectionStringArg, - String keystoreLocation) throws UserException { + private ConnectionConfiguration buildConnection(ConnectionBuilder builder, String connectionStringArg, String keystoreLocation) + throws UserException { return builder.buildConnection(connectionStringArg, keystoreLocation, randomBoolean()); } } diff --git a/x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/SqlCliTestCase.java b/x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/SqlCliTestCase.java index 8956518fc0ca0..7f24854dcf44a 100644 --- a/x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/SqlCliTestCase.java +++ b/x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/SqlCliTestCase.java @@ -11,6 +11,7 @@ import com.carrotsearch.randomizedtesting.annotations.ThreadLeakLingering; import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope; import com.carrotsearch.randomizedtesting.annotations.TimeoutSuite; + import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.TimeUnits; import org.elasticsearch.test.junit.listeners.LoggingListener; @@ -18,21 +19,30 @@ import static com.carrotsearch.randomizedtesting.RandomizedTest.randomAsciiLettersOfLength; -@Listeners({ - ReproduceInfoPrinter.class, - LoggingListener.class -}) +@Listeners({ ReproduceInfoPrinter.class, LoggingListener.class }) @ThreadLeakScope(ThreadLeakScope.Scope.SUITE) @ThreadLeakLingering(linger = 5000) // 5 sec lingering @TimeoutSuite(millis = 20 * TimeUnits.MINUTE) @LuceneTestCase.SuppressSysoutChecks(bugUrl = "we log a lot on purpose") // we suppress pretty much all the lucene codecs for now, except asserting // assertingcodec is the winner for a codec here: it finds bugs and gives clear exceptions. -@LuceneTestCase.SuppressCodecs({ - "SimpleText", "Memory", "CheapBastard", "Direct", "Compressing", "FST50", "FSTOrd50", - "TestBloomFilteredLucenePostings", "MockRandom", "BlockTreeOrds", "LuceneFixedGap", - "LuceneVarGapFixedInterval", "LuceneVarGapDocFreqInterval", "Lucene50" -}) +@LuceneTestCase.SuppressCodecs( + { + "SimpleText", + "Memory", + "CheapBastard", + "Direct", + "Compressing", + "FST50", + "FSTOrd50", + "TestBloomFilteredLucenePostings", + "MockRandom", + "BlockTreeOrds", + "LuceneFixedGap", + "LuceneVarGapFixedInterval", + "LuceneVarGapDocFreqInterval", + "Lucene50" } +) @LuceneTestCase.SuppressReproduceLine public abstract class SqlCliTestCase extends LuceneTestCase { diff --git a/x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/TestTerminal.java b/x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/TestTerminal.java index 756e739ec2d3f..0b9c89b8b0581 100644 --- a/x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/TestTerminal.java +++ b/x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/TestTerminal.java @@ -19,7 +19,7 @@ public class TestTerminal implements CliTerminal { private boolean closed = false; private Iterator inputLines; - public TestTerminal(String ... inputLines) { + public TestTerminal(String... inputLines) { this.inputLines = Arrays.asList(inputLines).iterator(); } diff --git a/x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/command/BuiltinCommandTests.java b/x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/command/BuiltinCommandTests.java index 22bacc20791ef..6c935885662a4 100644 --- a/x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/command/BuiltinCommandTests.java +++ b/x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/command/BuiltinCommandTests.java @@ -15,7 +15,6 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verifyNoMoreInteractions; - public class BuiltinCommandTests extends SqlCliTestCase { public void testInvalidCommand() throws Exception { diff --git a/x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/command/CliCommandsTests.java b/x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/command/CliCommandsTests.java index 5c3b1ea3fd56b..ee944789c8019 100644 --- a/x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/command/CliCommandsTests.java +++ b/x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/command/CliCommandsTests.java @@ -20,9 +20,9 @@ public void testCliCommands() { HttpClient httpClient = mock(HttpClient.class); CliSession cliSession = new CliSession(httpClient); CliCommands cliCommands = new CliCommands( - (terminal, session, line) -> line.equals("foo"), - (terminal, session, line) -> line.equals("bar"), - (terminal, session, line) -> line.equals("baz") + (terminal, session, line) -> line.equals("foo"), + (terminal, session, line) -> line.equals("bar"), + (terminal, session, line) -> line.equals("baz") ); assertTrue(cliCommands.handle(testTerminal, cliSession, "foo")); diff --git a/x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/command/ServerInfoCliCommandTests.java b/x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/command/ServerInfoCliCommandTests.java index 612db8853531a..f299c3cdd9a22 100644 --- a/x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/command/ServerInfoCliCommandTests.java +++ b/x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/command/ServerInfoCliCommandTests.java @@ -35,8 +35,9 @@ public void testShowInfo() throws Exception { TestTerminal testTerminal = new TestTerminal(); HttpClient client = mock(HttpClient.class); CliSession cliSession = new CliSession(client); - when(client.serverInfo()).thenReturn(new MainResponse("my_node", "1.2.3", - new ClusterName("my_cluster").value(), UUIDs.randomBase64UUID())); + when(client.serverInfo()).thenReturn( + new MainResponse("my_node", "1.2.3", new ClusterName("my_cluster").value(), UUIDs.randomBase64UUID()) + ); ServerInfoCliCommand cliCommand = new ServerInfoCliCommand(); assertTrue(cliCommand.handle(testTerminal, cliSession, "info")); assertEquals(testTerminal.toString(), "Node:my_node Cluster:my_cluster Version:1.2.3\n"); diff --git a/x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/command/ServerQueryCliCommandTests.java b/x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/command/ServerQueryCliCommandTests.java index 3ecc4473fadca..1c20b36e20aa0 100644 --- a/x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/command/ServerQueryCliCommandTests.java +++ b/x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/command/ServerQueryCliCommandTests.java @@ -63,8 +63,10 @@ public void testThreePageQuery() throws Exception { when(client.nextPage("my_cursor2")).thenReturn(fakeResponse("", false, "third")); ServerQueryCliCommand cliCommand = new ServerQueryCliCommand(); assertTrue(cliCommand.handle(testTerminal, cliSession, "test query")); - assertEquals(" field \n---------------\nfirst \nsecond \nthird \n", - testTerminal.toString()); + assertEquals( + " field \n---------------\nfirst \nsecond \nthird \n", + testTerminal.toString() + ); verify(client, times(1)).basicQuery(eq("test query"), eq(10)); verify(client, times(2)).nextPage(any()); verifyNoMoreInteractions(client); @@ -81,8 +83,7 @@ public void testTwoPageQueryWithSeparator() throws Exception { when(client.nextPage("my_cursor1")).thenReturn(fakeResponse("", false, "second")); ServerQueryCliCommand cliCommand = new ServerQueryCliCommand(); assertTrue(cliCommand.handle(testTerminal, cliSession, "test query")); - assertEquals(" field \n---------------\nfirst \n-----\nsecond \n", - testTerminal.toString()); + assertEquals(" field \n---------------\nfirst \n-----\nsecond \n", testTerminal.toString()); verify(client, times(1)).basicQuery(eq("test query"), eq(15)); verify(client, times(1)).nextPage(any()); verifyNoMoreInteractions(client); @@ -98,8 +99,10 @@ public void testCursorCleanupOnError() throws Exception { when(client.queryClose("my_cursor1", Mode.CLI)).thenReturn(true); ServerQueryCliCommand cliCommand = new ServerQueryCliCommand(); assertTrue(cliCommand.handle(testTerminal, cliSession, "test query")); - assertEquals(" field \n---------------\nfirst \n" + - "Bad request [test exception]\n", testTerminal.toString()); + assertEquals( + " field \n---------------\nfirst \n" + "Bad request [test exception]\n", + testTerminal.toString() + ); verify(client, times(1)).basicQuery(eq("test query"), eq(15)); verify(client, times(1)).nextPage(any()); verify(client, times(1)).queryClose(eq("my_cursor1"), eq(Mode.CLI)); diff --git a/x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/ClientVersion.java b/x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/ClientVersion.java index f5b01b81f94b7..63e31e6c9a107 100644 --- a/x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/ClientVersion.java +++ b/x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/ClientVersion.java @@ -55,7 +55,8 @@ public class ClientVersion { int foundJars = 0; if (normalized.size() > 1) { StringBuilder sb = new StringBuilder( - "Multiple Elasticsearch JDBC versions detected in the classpath; please use only one\n"); + "Multiple Elasticsearch JDBC versions detected in the classpath; please use only one\n" + ); for (String s : normalized) { if (s.contains("jar:")) { foundJars++; @@ -78,7 +79,7 @@ public class ClientVersion { // (1) a file URL: file:.jar // (2) jar file URL pointing to a JAR file: jar:.jar!/ // (3) jar file URL pointing to a JAR file entry (likely a fat JAR, but other types are possible): jar:!/driver name>.jar!/ - @SuppressForbidden(reason="java.util.jar.JarFile must be explicitly closed on Windows") + @SuppressForbidden(reason = "java.util.jar.JarFile must be explicitly closed on Windows") static Manifest getManifest(URL url) throws IOException { String urlStr = url.toString(); if (urlStr.endsWith(".jar") || urlStr.endsWith(".jar!/")) { diff --git a/x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/ConnectionConfiguration.java b/x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/ConnectionConfiguration.java index 7842a3ba68b17..0253dbdc7cd59 100644 --- a/x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/ConnectionConfiguration.java +++ b/x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/ConnectionConfiguration.java @@ -70,8 +70,19 @@ public class ConnectionConfiguration { private static final String CATALOG = "catalog"; protected static final Set OPTION_NAMES = new LinkedHashSet<>( - Arrays.asList(PROPERTIES_VALIDATION, BINARY_COMMUNICATION, CONNECT_TIMEOUT, NETWORK_TIMEOUT, QUERY_TIMEOUT, PAGE_TIMEOUT, - PAGE_SIZE, AUTH_USER, AUTH_PASS, CATALOG)); + Arrays.asList( + PROPERTIES_VALIDATION, + BINARY_COMMUNICATION, + CONNECT_TIMEOUT, + NETWORK_TIMEOUT, + QUERY_TIMEOUT, + PAGE_TIMEOUT, + PAGE_SIZE, + AUTH_USER, + AUTH_PASS, + CATALOG + ) + ); static { OPTION_NAMES.addAll(SslConfig.OPTION_NAMES); @@ -102,14 +113,20 @@ public ConnectionConfiguration(URI baseURI, String connectionString, Properties this.connectionString = connectionString; Properties settings = props != null ? props : new Properties(); - validateProperties = parseValue(PROPERTIES_VALIDATION, settings.getProperty(PROPERTIES_VALIDATION, PROPERTIES_VALIDATION_DEFAULT), - Boolean::parseBoolean); + validateProperties = parseValue( + PROPERTIES_VALIDATION, + settings.getProperty(PROPERTIES_VALIDATION, PROPERTIES_VALIDATION_DEFAULT), + Boolean::parseBoolean + ); if (validateProperties) { checkPropertyNames(settings, optionNames()); } - binaryCommunication = parseValue(BINARY_COMMUNICATION, settings.getProperty(BINARY_COMMUNICATION, BINARY_COMMUNICATION_DEFAULT), - Boolean::parseBoolean); + binaryCommunication = parseValue( + BINARY_COMMUNICATION, + settings.getProperty(BINARY_COMMUNICATION, BINARY_COMMUNICATION_DEFAULT), + Boolean::parseBoolean + ); connectTimeout = parseValue(CONNECT_TIMEOUT, settings.getProperty(CONNECT_TIMEOUT, CONNECT_TIMEOUT_DEFAULT), Long::parseLong); networkTimeout = parseValue(NETWORK_TIMEOUT, settings.getProperty(NETWORK_TIMEOUT, NETWORK_TIMEOUT_DEFAULT), Long::parseLong); @@ -128,9 +145,21 @@ public ConnectionConfiguration(URI baseURI, String connectionString, Properties this.baseURI = normalizeSchema(baseURI, connectionString, sslConfig.isEnabled()); } - public ConnectionConfiguration(URI baseURI, String connectionString, boolean validateProperties, boolean binaryCommunication, - long connectTimeout, long networkTimeout, long queryTimeout, long pageTimeout, int pageSize, - String user, String pass, SslConfig sslConfig, ProxyConfig proxyConfig) throws ClientException { + public ConnectionConfiguration( + URI baseURI, + String connectionString, + boolean validateProperties, + boolean binaryCommunication, + long connectTimeout, + long networkTimeout, + long queryTimeout, + long pageTimeout, + int pageSize, + String user, + String pass, + SslConfig sslConfig, + ProxyConfig proxyConfig + ) throws ClientException { this.validateProperties = validateProperties; this.binaryCommunication = binaryCommunication; this.connectionString = connectionString; @@ -150,11 +179,17 @@ public ConnectionConfiguration(URI baseURI, String connectionString, boolean val this.baseURI = baseURI; } - - private static URI normalizeSchema(URI uri, String connectionString, boolean isSSLEnabled) { + private static URI normalizeSchema(URI uri, String connectionString, boolean isSSLEnabled) { try { - return new URI(isSSLEnabled ? "https" : "http", null, uri.getHost(), uri.getPort(), uri.getPath(), uri.getQuery(), - uri.getFragment()); + return new URI( + isSSLEnabled ? "https" : "http", + null, + uri.getHost(), + uri.getPort(), + uri.getPath(), + uri.getQuery(), + uri.getFragment() + ); } catch (URISyntaxException ex) { throw new ClientException("Cannot parse process baseURI [" + connectionString + "] " + ex.getMessage()); } diff --git a/x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/HttpClient.java b/x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/HttpClient.java index 942f6e0606c78..2e836b829c4bc 100644 --- a/x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/HttpClient.java +++ b/x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/HttpClient.java @@ -6,15 +6,15 @@ */ package org.elasticsearch.xpack.sql.client; -import org.elasticsearch.core.Tuple; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.core.Tuple; +import org.elasticsearch.core.internal.io.Streams; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.core.internal.io.Streams; import org.elasticsearch.xpack.sql.client.JreHttpUrlConnection.ResponseOrException; import org.elasticsearch.xpack.sql.proto.AbstractSqlRequest; import org.elasticsearch.xpack.sql.proto.MainResponse; @@ -66,18 +66,23 @@ public MainResponse serverInfo() throws SQLException { public SqlQueryResponse basicQuery(String query, int fetchSize) throws SQLException { // TODO allow customizing the time zone - this is what session set/reset/get should be about // method called only from CLI - SqlQueryRequest sqlRequest = new SqlQueryRequest(query, emptyList(), Protocol.TIME_ZONE, null, - fetchSize, - TimeValue.timeValueMillis(cfg.queryTimeout()), - TimeValue.timeValueMillis(cfg.pageTimeout()), - null, - Boolean.FALSE, - null, - new RequestInfo(Mode.CLI, ClientVersion.CURRENT), - false, - false, - cfg.binaryCommunication(), - emptyMap()); + SqlQueryRequest sqlRequest = new SqlQueryRequest( + query, + emptyList(), + Protocol.TIME_ZONE, + null, + fetchSize, + TimeValue.timeValueMillis(cfg.queryTimeout()), + TimeValue.timeValueMillis(cfg.pageTimeout()), + null, + Boolean.FALSE, + null, + new RequestInfo(Mode.CLI, ClientVersion.CURRENT), + false, + false, + cfg.binaryCommunication(), + emptyMap() + ); return query(sqlRequest); } @@ -87,43 +92,68 @@ public SqlQueryResponse query(SqlQueryRequest sqlRequest) throws SQLException { public SqlQueryResponse nextPage(String cursor) throws SQLException { // method called only from CLI - SqlQueryRequest sqlRequest = new SqlQueryRequest(cursor, TimeValue.timeValueMillis(cfg.queryTimeout()), - TimeValue.timeValueMillis(cfg.pageTimeout()), new RequestInfo(Mode.CLI), cfg.binaryCommunication()); + SqlQueryRequest sqlRequest = new SqlQueryRequest( + cursor, + TimeValue.timeValueMillis(cfg.queryTimeout()), + TimeValue.timeValueMillis(cfg.pageTimeout()), + new RequestInfo(Mode.CLI), + cfg.binaryCommunication() + ); return post(Protocol.SQL_QUERY_REST_ENDPOINT, sqlRequest, SqlQueryResponse::fromXContent); } public boolean queryClose(String cursor, Mode mode) throws SQLException { - SqlClearCursorResponse response = post(Protocol.CLEAR_CURSOR_REST_ENDPOINT, + SqlClearCursorResponse response = post( + Protocol.CLEAR_CURSOR_REST_ENDPOINT, new SqlClearCursorRequest(cursor, new RequestInfo(mode)), - SqlClearCursorResponse::fromXContent); + SqlClearCursorResponse::fromXContent + ); return response.isSucceeded(); } - private Response post(String path, Request request, - CheckedFunction responseParser) - throws SQLException { + private Response post( + String path, + Request request, + CheckedFunction responseParser + ) throws SQLException { byte[] requestBytes = toXContent(request); String query = "error_trace"; - Tuple response = - AccessController.doPrivileged((PrivilegedAction>>) () -> - JreHttpUrlConnection.http(path, query, cfg, con -> - con.request( - (out) -> out.write(requestBytes), - this::readFrom, - "POST", - requestBodyContentType.mediaTypeWithoutParameters() // "application/cbor" or "application/json" - ) - )).getResponseOrThrowException(); + Tuple response = AccessController.doPrivileged( + (PrivilegedAction>>) () -> JreHttpUrlConnection.http( + path, + query, + cfg, + con -> con.request( + (out) -> out.write(requestBytes), + this::readFrom, + "POST", + requestBodyContentType.mediaTypeWithoutParameters() // "application/cbor" or "application/json" + ) + ) + ).getResponseOrThrowException(); return fromXContent(response.v1(), response.v2(), responseParser); } private boolean head(String path, long timeoutInMs) throws SQLException { - ConnectionConfiguration pingCfg = new ConnectionConfiguration(cfg.baseUri(), cfg.connectionString(), cfg.validateProperties(), - cfg.binaryCommunication(), cfg.connectTimeout(), timeoutInMs, cfg.queryTimeout(), cfg.pageTimeout(), cfg.pageSize(), - cfg.authUser(), cfg.authPass(), cfg.sslConfig(), cfg.proxyConfig()); + ConnectionConfiguration pingCfg = new ConnectionConfiguration( + cfg.baseUri(), + cfg.connectionString(), + cfg.validateProperties(), + cfg.binaryCommunication(), + cfg.connectTimeout(), + timeoutInMs, + cfg.queryTimeout(), + cfg.pageTimeout(), + cfg.pageSize(), + cfg.authUser(), + cfg.authPass(), + cfg.sslConfig(), + cfg.proxyConfig() + ); try { - return AccessController.doPrivileged((PrivilegedAction) () -> - JreHttpUrlConnection.http(path, "error_trace", pingCfg, JreHttpUrlConnection::head)); + return AccessController.doPrivileged( + (PrivilegedAction) () -> JreHttpUrlConnection.http(path, "error_trace", pingCfg, JreHttpUrlConnection::head) + ); } catch (ClientException ex) { throw new SQLException("Cannot ping server", ex); } @@ -131,20 +161,19 @@ private boolean head(String path, long timeoutInMs) throws SQLException { private Response get(String path, CheckedFunction responseParser) throws SQLException { - Tuple response = - AccessController.doPrivileged((PrivilegedAction>>) () -> - JreHttpUrlConnection.http(path, "error_trace", cfg, con -> - con.request( - null, - this::readFrom, - "GET" - ) - )).getResponseOrThrowException(); + Tuple response = AccessController.doPrivileged( + (PrivilegedAction>>) () -> JreHttpUrlConnection.http( + path, + "error_trace", + cfg, + con -> con.request(null, this::readFrom, "GET") + ) + ).getResponseOrThrowException(); return fromXContent(response.v1(), response.v2(), responseParser); } private byte[] toXContent(Request xContent) { - try(ByteArrayOutputStream buffer = new ByteArrayOutputStream()) { + try (ByteArrayOutputStream buffer = new ByteArrayOutputStream()) { try (XContentBuilder xContentBuilder = new XContentBuilder(requestBodyContentType.xContent(), buffer)) { if (xContent.isFragment()) { xContentBuilder.startObject(); @@ -176,11 +205,15 @@ private Tuple readFrom(InputStream inputStream, Function Response fromXContent(XContentType xContentType, byte[] bytesReference, - CheckedFunction responseParser) { - try (InputStream stream = new ByteArrayInputStream(bytesReference); - XContentParser parser = xContentType.xContent().createParser(registry, - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, stream)) { + private Response fromXContent( + XContentType xContentType, + byte[] bytesReference, + CheckedFunction responseParser + ) { + try ( + InputStream stream = new ByteArrayInputStream(bytesReference); + XContentParser parser = xContentType.xContent().createParser(registry, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, stream) + ) { return responseParser.apply(parser); } catch (IOException ex) { throw new ClientException("Cannot parse response", ex); diff --git a/x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/JreHttpUrlConnection.java b/x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/JreHttpUrlConnection.java index fb8bde4c022e5..32849cd18f49a 100644 --- a/x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/JreHttpUrlConnection.java +++ b/x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/JreHttpUrlConnection.java @@ -50,16 +50,24 @@ public class JreHttpUrlConnection implements Closeable { * error. */ public static final String SQL_STATE_BAD_SERVER = "bad_server"; - private static final String SQL_NOT_AVAILABLE_ERROR_MESSAGE = "Incorrect HTTP method for uri [" + SQL_QUERY_REST_ENDPOINT - + "?error_trace] and method [POST], allowed:"; + private static final String SQL_NOT_AVAILABLE_ERROR_MESSAGE = "Incorrect HTTP method for uri [" + + SQL_QUERY_REST_ENDPOINT + + "?error_trace] and method [POST], allowed:"; public static R http(String path, String query, ConnectionConfiguration cfg, Function handler) { final URI uriPath = appendSegmentToPath(cfg.baseUri(), path); // update path if needed final String uriQuery = query == null ? uriPath.getQuery() : query; // update query if needed final URL url; try { - url = new URI(uriPath.getScheme(), null, uriPath.getHost(), uriPath.getPort(), uriPath.getPath(), uriQuery, - uriPath.getFragment()).toURL(); + url = new URI( + uriPath.getScheme(), + null, + uriPath.getHost(), + uriPath.getPort(), + uriPath.getPath(), + uriQuery, + uriPath.getFragment() + ).toURL(); } catch (URISyntaxException | MalformedURLException ex) { throw new ClientException("Cannot build url using base: [" + uriPath + "] query: [" + query + "] path: [" + path + "]", ex); } @@ -102,7 +110,7 @@ private void setupConnection(ConnectionConfiguration cfg) { // HttpURL adds this header by default, HttpS does not // adding it here to be consistent con.setRequestProperty("Accept-Charset", "UTF-8"); - //con.setRequestProperty("Accept-Encoding", GZIP); + // con.setRequestProperty("Accept-Encoding", GZIP); setupSSL(cfg); setupBasicAuth(cfg); @@ -138,17 +146,18 @@ public boolean head() throws ClientException { } public ResponseOrException request( - CheckedConsumer doc, - CheckedBiFunction, R, IOException> parser, - String requestMethod + CheckedConsumer doc, + CheckedBiFunction, R, IOException> parser, + String requestMethod ) throws ClientException { return request(doc, parser, requestMethod, "application/json"); } public ResponseOrException request( - CheckedConsumer doc, - CheckedBiFunction, R, IOException> parser, - String requestMethod, String contentTypeHeader + CheckedConsumer doc, + CheckedBiFunction, R, IOException> parser, + String requestMethod, + String contentTypeHeader ) throws ClientException { try { con.setRequestMethod(requestMethod); @@ -162,10 +171,7 @@ public ResponseOrException request( } if (shouldParseBody(con.getResponseCode())) { try (InputStream stream = getStream(con, con.getInputStream())) { - return new ResponseOrException<>(parser.apply( - new BufferedInputStream(stream), - con::getHeaderField - )); + return new ResponseOrException<>(parser.apply(new BufferedInputStream(stream), con::getHeaderField)); } } return parserError(); @@ -184,26 +190,43 @@ private ResponseOrException parserError() throws IOException { failure = RemoteFailure.parseFromResponse(stream); } if (con.getResponseCode() >= 500) { - return new ResponseOrException<>(new SQLException("Server encountered an error [" - + failure.reason() + "]. [" + failure.remoteTrace() + "]", SQL_STATE_BAD_SERVER)); + return new ResponseOrException<>( + new SQLException( + "Server encountered an error [" + failure.reason() + "]. [" + failure.remoteTrace() + "]", + SQL_STATE_BAD_SERVER + ) + ); } SqlExceptionType type = SqlExceptionType.fromRemoteFailureType(failure.type()); if (type == null) { // check if x-pack or sql are not available (x-pack not installed or sql not enabled) // by checking the error message the server is sending back if (con.getResponseCode() >= HttpURLConnection.HTTP_BAD_REQUEST && failure.reason().contains(SQL_NOT_AVAILABLE_ERROR_MESSAGE)) { - return new ResponseOrException<>(new SQLException("X-Pack/SQL does not seem to be available" - + " on the Elasticsearch node using the access path '" - + con.getURL().getHost() - + (con.getURL().getPort() > 0 ? ":" + con.getURL().getPort() : "") - + "'." - + " Please verify X-Pack is installed and SQL enabled. Alternatively, check if any proxy is interfering" - + " the communication to Elasticsearch", - SQL_STATE_BAD_SERVER)); + return new ResponseOrException<>( + new SQLException( + "X-Pack/SQL does not seem to be available" + + " on the Elasticsearch node using the access path '" + + con.getURL().getHost() + + (con.getURL().getPort() > 0 ? ":" + con.getURL().getPort() : "") + + "'." + + " Please verify X-Pack is installed and SQL enabled. Alternatively, check if any proxy is interfering" + + " the communication to Elasticsearch", + SQL_STATE_BAD_SERVER + ) + ); } - return new ResponseOrException<>(new SQLException("Server sent bad type [" - + failure.type() + "]. Original type was [" + failure.reason() + "]. [" - + failure.remoteTrace() + "]", SQL_STATE_BAD_SERVER)); + return new ResponseOrException<>( + new SQLException( + "Server sent bad type [" + + failure.type() + + "]. Original type was [" + + failure.reason() + + "]. [" + + failure.remoteTrace() + + "]", + SQL_STATE_BAD_SERVER + ) + ); } return new ResponseOrException<>(type.asException(failure.reason())); } diff --git a/x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/RemoteFailure.java b/x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/RemoteFailure.java index 3a027194d2b63..35120d45e531d 100644 --- a/x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/RemoteFailure.java +++ b/x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/RemoteFailure.java @@ -88,12 +88,14 @@ public static RemoteFailure parseFromResponse(InputStream stream) throws IOExcep private final Map> metadata; private final RemoteFailure cause; - RemoteFailure(String type, - String reason, - String remoteTrace, - Map headers, - Map> metadata, - RemoteFailure cause) { + RemoteFailure( + String type, + String reason, + String remoteTrace, + Map headers, + Map> metadata, + RemoteFailure cause + ) { this.type = type; this.reason = reason; this.remoteTrace = remoteTrace; @@ -146,8 +148,9 @@ private static RemoteFailure parseResponseTopLevel(JsonParser parser) throws IOE * but, alas, we aren't going to modularize those out any time soon. */ JsonToken token = parser.nextToken(); if (token != JsonToken.START_OBJECT) { - throw new IllegalArgumentException("Expected error to start with [START_OBJECT] but started with [" + token - + "][" + parser.getText() + "]"); + throw new IllegalArgumentException( + "Expected error to start with [START_OBJECT] but started with [" + token + "][" + parser.getText() + "]" + ); } String fieldName = null; while ((token = parser.nextToken()) != JsonToken.END_OBJECT) { @@ -155,25 +158,26 @@ private static RemoteFailure parseResponseTopLevel(JsonParser parser) throws IOE fieldName = parser.getCurrentName(); } else { switch (fieldName) { - case "error": - if (token != JsonToken.START_OBJECT && token != JsonToken.VALUE_STRING) { - throw new IOException("Expected [error] to be an object or string but was [" + token + "][" - + parser.getText() + "]"); - } - if (token == JsonToken.VALUE_STRING) { - exception = new RemoteFailure(StringUtils.EMPTY, parser.getText(), null, null, null, null); - } else { - exception = parseFailure(parser); - } - continue; - case "status": - if (token != JsonToken.VALUE_NUMBER_INT) { - throw new IOException("Expected [status] to be a string but was [" + token + "][" + parser.getText() + "]"); - } - // Intentionally ignored - continue; - default: - throw new IOException("Expected one of [error, status] but got [" + fieldName + "][" + parser.getText() + "]"); + case "error": + if (token != JsonToken.START_OBJECT && token != JsonToken.VALUE_STRING) { + throw new IOException( + "Expected [error] to be an object or string but was [" + token + "][" + parser.getText() + "]" + ); + } + if (token == JsonToken.VALUE_STRING) { + exception = new RemoteFailure(StringUtils.EMPTY, parser.getText(), null, null, null, null); + } else { + exception = parseFailure(parser); + } + continue; + case "status": + if (token != JsonToken.VALUE_NUMBER_INT) { + throw new IOException("Expected [status] to be a string but was [" + token + "][" + parser.getText() + "]"); + } + // Intentionally ignored + continue; + default: + throw new IOException("Expected one of [error, status] but got [" + fieldName + "][" + parser.getText() + "]"); } } } @@ -198,49 +202,51 @@ private static RemoteFailure parseFailure(JsonParser parser) throws IOException fieldName = parser.getCurrentName(); } else { switch (fieldName) { - case "caused_by": - if (token != JsonToken.START_OBJECT) { - throw new IOException("Expected [caused_by] to be an object but was [" + token + "][" + parser.getText() + "]"); - } - cause = parseFailure(parser); - break; - case "header": - if (token != JsonToken.START_OBJECT) { - throw new IOException("Expected [header] to be an object but was [" + token + "][" + parser.getText() + "]"); - } - headers = parseHeaders(parser); - break; - case "reason": - switch (token) { - case VALUE_STRING: - reason = parser.getText(); + case "caused_by": + if (token != JsonToken.START_OBJECT) { + throw new IOException("Expected [caused_by] to be an object but was [" + token + "][" + parser.getText() + "]"); + } + cause = parseFailure(parser); + break; + case "header": + if (token != JsonToken.START_OBJECT) { + throw new IOException("Expected [header] to be an object but was [" + token + "][" + parser.getText() + "]"); + } + headers = parseHeaders(parser); + break; + case "reason": + switch (token) { + case VALUE_STRING: + reason = parser.getText(); + break; + case VALUE_NULL: + break; + default: + throw new IOException("Expected [reason] to be a string but was [" + token + "][" + parser.getText() + "]"); + } + break; + case "root_cause": + if (token != JsonToken.START_ARRAY) { + throw new IOException("Expected [root_cause] to be an array but was [" + token + "][" + parser.getText() + "]"); + } + parser.skipChildren(); // Intentionally ignored + break; + case "stack_trace": + if (token != JsonToken.VALUE_STRING) { + throw new IOException( + "Expected [stack_trace] to be a string but was [" + token + "][" + parser.getText() + "]" + ); + } + remoteTrace = parser.getText(); break; - case VALUE_NULL: + case "type": + if (token != JsonToken.VALUE_STRING) { + throw new IOException("Expected [type] to be a string but was [" + token + "][" + parser.getText() + "]"); + } + type = parser.getText(); break; default: - throw new IOException("Expected [reason] to be a string but was [" + token + "][" + parser.getText() + "]"); - } - break; - case "root_cause": - if (token != JsonToken.START_ARRAY) { - throw new IOException("Expected [root_cause] to be an array but was [" + token + "][" + parser.getText() + "]"); - } - parser.skipChildren(); // Intentionally ignored - break; - case "stack_trace": - if (token != JsonToken.VALUE_STRING) { - throw new IOException("Expected [stack_trace] to be a string but was [" + token + "][" + parser.getText() + "]"); - } - remoteTrace = parser.getText(); - break; - case "type": - if (token != JsonToken.VALUE_STRING) { - throw new IOException("Expected [type] to be a string but was [" + token + "][" + parser.getText() + "]"); - } - type = parser.getText(); - break; - default: - metadata.putAll(parseMetadata(parser)); + metadata.putAll(parseMetadata(parser)); } } } @@ -286,7 +292,7 @@ private static Map> parseMetadata(final JsonParser parser) // Arrays of objects are not supported yet and just ignored and skipped. final List values = new ArrayList<>(); while ((token = parser.nextToken()) != JsonToken.END_ARRAY) { - if (token ==JsonToken.VALUE_STRING) { + if (token == JsonToken.VALUE_STRING) { values.add(parser.getText()); } else { parser.skipChildren(); @@ -335,9 +341,8 @@ private static String parseErrorMessage(String message, InputStream stream, Json } String parserLocation = ""; if (parser != null) { - parserLocation = " at [line " + parser.getTokenLocation().getLineNr() - + " col " + parser.getTokenLocation().getColumnNr() + "]"; + parserLocation = " at [line " + parser.getTokenLocation().getLineNr() + " col " + parser.getTokenLocation().getColumnNr() + "]"; } - return "Can't parse error from Elasticsearch [" + message + "]" + parserLocation + ". " + responseMessage; + return "Can't parse error from Elasticsearch [" + message + "]" + parserLocation + ". " + responseMessage; } } diff --git a/x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/SslConfig.java b/x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/SslConfig.java index 60e07736ebb1e..e1645b3ed5833 100644 --- a/x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/SslConfig.java +++ b/x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/SslConfig.java @@ -54,9 +54,18 @@ public class SslConfig { public static final String SSL_TRUSTSTORE_TYPE = "ssl.truststore.type"; private static final String SSL_TRUSTSTORE_TYPE_DEFAULT = "JKS"; - static final Set OPTION_NAMES = new LinkedHashSet<>(Arrays.asList(SSL, SSL_PROTOCOL, - SSL_KEYSTORE_LOCATION, SSL_KEYSTORE_PASS, SSL_KEYSTORE_TYPE, - SSL_TRUSTSTORE_LOCATION, SSL_TRUSTSTORE_PASS, SSL_TRUSTSTORE_TYPE)); + static final Set OPTION_NAMES = new LinkedHashSet<>( + Arrays.asList( + SSL, + SSL_PROTOCOL, + SSL_KEYSTORE_LOCATION, + SSL_KEYSTORE_PASS, + SSL_KEYSTORE_TYPE, + SSL_TRUSTSTORE_LOCATION, + SSL_TRUSTSTORE_PASS, + SSL_TRUSTSTORE_TYPE + ) + ); private final boolean enabled; private final String protocol, keystoreLocation, keystorePass, keystoreType; @@ -121,14 +130,14 @@ private KeyManager[] loadKeyManagers() throws GeneralSecurityException, IOExcept return kmFactory.getKeyManagers(); } - private KeyStore loadKeyStore(String source, char[] pass, String keyStoreType) throws GeneralSecurityException, IOException { KeyStore keyStore = KeyStore.getInstance(keyStoreType); Path path = Paths.get(source); if (Files.exists(path) == false) { - throw new ClientException( - "Expected to find keystore file at [" + source + "] but was unable to. Make sure you have specified a valid URI."); + throw new ClientException( + "Expected to find keystore file at [" + source + "] but was unable to. Make sure you have specified a valid URI." + ); } try (InputStream in = Files.newInputStream(Paths.get(source), StandardOpenOption.READ)) { @@ -166,13 +175,13 @@ public boolean equals(Object obj) { SslConfig other = (SslConfig) obj; return Objects.equals(enabled, other.enabled) - && Objects.equals(protocol, other.protocol) - && Objects.equals(keystoreLocation, other.keystoreLocation) - && Objects.equals(keystorePass, other.keystorePass) - && Objects.equals(keystoreType, other.keystoreType) - && Objects.equals(truststoreLocation, other.truststoreLocation) - && Objects.equals(truststorePass, other.truststorePass) - && Objects.equals(truststoreType, other.truststoreType); + && Objects.equals(protocol, other.protocol) + && Objects.equals(keystoreLocation, other.keystoreLocation) + && Objects.equals(keystorePass, other.keystorePass) + && Objects.equals(keystoreType, other.keystoreType) + && Objects.equals(truststoreLocation, other.truststoreLocation) + && Objects.equals(truststorePass, other.truststorePass) + && Objects.equals(truststoreType, other.truststoreType); } @Override diff --git a/x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/StringUtils.java b/x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/StringUtils.java index 3a596cbea644b..bfd565f6d09a2 100644 --- a/x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/StringUtils.java +++ b/x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/StringUtils.java @@ -59,8 +59,7 @@ public static String[] splitToIndexAndType(String pattern) { if (tokens.size() == 2) { results[0] = tokens.get(0); results[1] = tokens.get(1); - } - else { + } else { results[0] = nullAsEmpty(pattern); results[1] = EMPTY; } @@ -128,8 +127,7 @@ public static String normalize(String path) { prefix = pathToUse.substring(0, prefixIndex + 1); if (prefix.contains(SLASH)) { prefix = ""; - } - else { + } else { pathToUse = pathToUse.substring(prefixIndex + 1); } } @@ -146,17 +144,14 @@ public static String normalize(String path) { String element = pathList.get(i); if (PATH_CURRENT.equals(element)) { // current folder, ignore it - } - else if (PATH_TOP.equals(element)) { + } else if (PATH_TOP.equals(element)) { // top folder, skip previous element tops++; - } - else { + } else { if (tops > 0) { // should it be skipped? tops--; - } - else { + } else { pathTokens.add(0, element); } } @@ -190,8 +185,7 @@ private static int levenshteinDistance(CharSequence one, CharSequence another, i // if one string is empty, the edit distance is necessarily the length of the other if (n == 0) { return m <= threshold ? m : -1; - } - else if (m == 0) { + } else if (m == 0) { return n <= threshold ? n : -1; } @@ -242,8 +236,7 @@ else if (m == 0) { if (one.charAt(i - 1) == t_j) { // diagonally left and up d[i] = p[i - 1]; - } - else { + } else { // 1 + minimum of cell to the left, to the top, diagonally left and up d[i] = 1 + Math.min(Math.min(d[i - 1], p[i]), p[i - 1]); } @@ -276,8 +269,7 @@ public static List findSimilar(CharSequence match, Collection po maxDistance = dist; list.clear(); list.add(string); - } - else if (dist == maxDistance) { + } else if (dist == maxDistance) { list.add(string); } } @@ -287,10 +279,13 @@ else if (dist == maxDistance) { } public static boolean parseBoolean(String input) { - switch(input) { - case "true": return true; - case "false": return false; - default: throw new IllegalArgumentException("must be [true] or [false]"); + switch (input) { + case "true": + return true; + case "false": + return false; + default: + throw new IllegalArgumentException("must be [true] or [false]"); } } diff --git a/x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/UriUtils.java b/x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/UriUtils.java index ede626ef525f6..b3c962531f0d5 100644 --- a/x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/UriUtils.java +++ b/x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/UriUtils.java @@ -78,8 +78,14 @@ private static URI parseMaybeWithScheme(String connectionString, String defaultP if (hasAnHttpPrefix == false) { if (uri.getHost() != null) { // URI is valid and with a host, so there's a scheme (otherwise host==null), but just not HTTP(S) throw new IllegalArgumentException( - "Invalid connection scheme [" + uri.getScheme() + "] configuration: only " + HTTP_SCHEME + " and " + HTTPS_SCHEME - + " protocols are supported"); + "Invalid connection scheme [" + + uri.getScheme() + + "] configuration: only " + + HTTP_SCHEME + + " and " + + HTTPS_SCHEME + + " protocols are supported" + ); } // no host and either (1) no scheme (like for input 'host') or (2) invalid scheme (produced by parsing 'user:pass@host' or // 'host:9200' or just erroneous: 'ftp:/?foo' etc.): try with a HTTP scheme @@ -108,8 +114,12 @@ private static String redactAttributeInString(String string, String attrName, Ch return string; } - private static void redactValueForSimilarKey(String key, List options, List> attrs, - Character replacement) { + private static void redactValueForSimilarKey( + String key, + List options, + List> attrs, + Character replacement + ) { List similar = StringUtils.findSimilar(key, options); for (String k : similar) { for (Map.Entry e : attrs) { @@ -204,8 +214,10 @@ private static String editURI(URI uri, List> fault if (idx >= sb.length()) { sb.append(e.getValue()); } else { - sb.insert(idx, - (sb.charAt(idx) == '\0' && (idx + 1 >= sb.length() || sb.charAt(idx + 1) == '\0')) ? '\0' : e.getValue()); + sb.insert( + idx, + (sb.charAt(idx) == '\0' && (idx + 1 >= sb.length() || sb.charAt(idx + 1) == '\0')) ? '\0' : e.getValue() + ); } } @@ -219,7 +231,7 @@ private static String redactCredentialsInURLString(String urlString) { List> faults = new ArrayList<>(); boolean hasPort = false; - for (StringBuilder sb = new StringBuilder(urlString); sb.length() > 0; ) { + for (StringBuilder sb = new StringBuilder(urlString); sb.length() > 0;) { try { // parse as URL; ex. `http://ho~st` parses as URI, but with unparsable authority URI uri = new URI(sb.toString()).parseServerAuthority(); @@ -299,8 +311,15 @@ public static URI appendSegmentToPath(URI uri, String segment) { concatenatedPath = path + "/" + cleanSegment; } try { - return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), concatenatedPath, - uri.getQuery(), uri.getFragment()); + return new URI( + uri.getScheme(), + uri.getUserInfo(), + uri.getHost(), + uri.getPort(), + concatenatedPath, + uri.getQuery(), + uri.getFragment() + ); } catch (URISyntaxException e) { throw new IllegalArgumentException("Invalid segment [" + segment + "] for URI [" + uri + "]: " + e.getMessage(), e); } diff --git a/x-pack/plugin/sql/sql-client/src/test/java/org/elasticsearch/xpack/sql/client/HttpClientRequestTests.java b/x-pack/plugin/sql/sql-client/src/test/java/org/elasticsearch/xpack/sql/client/HttpClientRequestTests.java index 17dc468150dec..ad8e8dfe4c485 100644 --- a/x-pack/plugin/sql/sql-client/src/test/java/org/elasticsearch/xpack/sql/client/HttpClientRequestTests.java +++ b/x-pack/plugin/sql/sql-client/src/test/java/org/elasticsearch/xpack/sql/client/HttpClientRequestTests.java @@ -16,15 +16,15 @@ import org.apache.logging.log4j.message.ParameterizedMessage; import org.apache.logging.log4j.util.Supplier; import org.elasticsearch.common.Strings; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.Streams; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.ConcurrentCollections; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.core.SuppressForbidden; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.mocksocket.MockHttpServer; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.sql.proto.Mode; import org.elasticsearch.xpack.sql.proto.RequestInfo; import org.elasticsearch.xpack.sql.proto.SqlQueryRequest; @@ -160,21 +160,23 @@ private void assertBinaryRequestForDrivers(boolean isBinary, XContentType xConte HttpClient httpClient = new HttpClient(conCfg); Mode mode = randomFrom(Mode.JDBC, Mode.ODBC); - SqlQueryRequest request = new SqlQueryRequest(query, - null, - ZoneId.of("Z"), - randomAlphaOfLength(10), - randomIntBetween(1, 100), - TimeValue.timeValueMillis(randomNonNegativeLong()), - TimeValue.timeValueMillis(randomNonNegativeLong()), - null, - randomBoolean(), - randomAlphaOfLength(128), - new RequestInfo(mode, ClientVersion.CURRENT), - randomBoolean(), - randomBoolean(), - isBinary, - Collections.emptyMap()); + SqlQueryRequest request = new SqlQueryRequest( + query, + null, + ZoneId.of("Z"), + randomAlphaOfLength(10), + randomIntBetween(1, 100), + TimeValue.timeValueMillis(randomNonNegativeLong()), + TimeValue.timeValueMillis(randomNonNegativeLong()), + null, + randomBoolean(), + randomAlphaOfLength(128), + new RequestInfo(mode, ClientVersion.CURRENT), + randomBoolean(), + randomBoolean(), + isBinary, + Collections.emptyMap() + ); prepareMockResponse(); try { @@ -208,8 +210,7 @@ private static class RawRequestMockWebServer implements Closeable { private String hostname; private int port; - RawRequestMockWebServer() { - } + RawRequestMockWebServer() {} void start() throws IOException { InetSocketAddress address = new InetSocketAddress(InetAddress.getLoopbackAddress().getHostAddress(), 0); @@ -238,8 +239,14 @@ void start() throws IOException { } } } catch (Exception e) { - logger.error((Supplier) () -> new ParameterizedMessage("failed to respond to request [{} {}]", - s.getRequestMethod(), s.getRequestURI()), e); + logger.error( + (Supplier) () -> new ParameterizedMessage( + "failed to respond to request [{} {}]", + s.getRequestMethod(), + s.getRequestURI() + ), + e + ); } finally { s.close(); } @@ -285,7 +292,6 @@ public void close() { } } - private static class RawRequest { private final String method; diff --git a/x-pack/plugin/sql/sql-client/src/test/java/org/elasticsearch/xpack/sql/client/RemoteFailureTests.java b/x-pack/plugin/sql/sql-client/src/test/java/org/elasticsearch/xpack/sql/client/RemoteFailureTests.java index cf6ab612ec6bb..765e864f76fc7 100644 --- a/x-pack/plugin/sql/sql-client/src/test/java/org/elasticsearch/xpack/sql/client/RemoteFailureTests.java +++ b/x-pack/plugin/sql/sql-client/src/test/java/org/elasticsearch/xpack/sql/client/RemoteFailureTests.java @@ -8,7 +8,6 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.xpack.sql.client.RemoteFailure; import java.io.IOException; import java.io.InputStream; @@ -27,8 +26,10 @@ public void testParseBasic() throws IOException { RemoteFailure failure = parse("basic.json"); assertEquals("illegal_argument_exception", failure.type()); assertEquals("[sql/query] unknown field [test], parser not found", failure.reason()); - assertThat(failure.remoteTrace(), - containsString("at org.elasticsearch.common.xcontent.ObjectParser.getParser(ObjectParser.java:346)")); + assertThat( + failure.remoteTrace(), + containsString("at org.elasticsearch.common.xcontent.ObjectParser.getParser(ObjectParser.java:346)") + ); assertNull(failure.cause()); assertEquals(emptyMap(), failure.headers()); } @@ -37,15 +38,19 @@ public void testParseNested() throws IOException { RemoteFailure failure = parse("nested.json"); assertEquals("parsing_exception", failure.type()); assertEquals("line 1:1: no viable alternative at input 'test'", failure.reason()); - assertThat(failure.remoteTrace(), - containsString("at org.elasticsearch.xpack.sql.parser.SqlParser$1.syntaxError(SqlParser.java:151)")); + assertThat( + failure.remoteTrace(), + containsString("at org.elasticsearch.xpack.sql.parser.SqlParser$1.syntaxError(SqlParser.java:151)") + ); assertNotNull(failure.cause()); failure = failure.cause(); assertEquals("no_viable_alt_exception", failure.type()); assertEquals(null, failure.reason()); - assertThat(failure.remoteTrace(), - containsString("at org.antlr.v4.runtime.atn.ParserATNSimulator.noViableAlt(ParserATNSimulator.java:1886)")); + assertThat( + failure.remoteTrace(), + containsString("at org.antlr.v4.runtime.atn.ParserATNSimulator.noViableAlt(ParserATNSimulator.java:1886)") + ); assertNull(failure.cause()); assertEquals(emptyMap(), failure.headers()); } @@ -54,18 +59,17 @@ public void testParseMissingAuth() throws IOException { RemoteFailure failure = parse("missing_auth.json"); assertEquals("security_exception", failure.type()); assertEquals("missing authentication token for REST request [/?pretty&error_trace]", failure.reason()); - assertThat(failure.remoteTrace(), - containsString("DefaultAuthenticationFailureHandler.missingToken")); + assertThat(failure.remoteTrace(), containsString("DefaultAuthenticationFailureHandler.missingToken")); assertNull(failure.cause()); - assertEquals(singletonMap("WWW-Authenticate", "Basic realm=\"security\" charset=\"UTF-8\""), - failure.headers()); + assertEquals(singletonMap("WWW-Authenticate", "Basic realm=\"security\" charset=\"UTF-8\""), failure.headers()); } public void testNoError() { IOException e = expectThrows(IOException.class, () -> parse("no_error.json")); assertEquals( - "Can't parse error from Elasticsearch [Expected [error] but didn't see it.] at [line 1 col 2]. Response:\n{}", - e.getMessage()); + "Can't parse error from Elasticsearch [Expected [error] but didn't see it.] at [line 1 col 2]. Response:\n{}", + e.getMessage() + ); } public void testBogusError() { @@ -74,20 +78,27 @@ public void testBogusError() { "Can't parse error from Elasticsearch [Expected [error] to be an object or string but was [START_ARRAY][[]] " + "at [line 1 col 12]. Response:\n" + "{ \"error\": [\"bogus\"] }", - e.getMessage()); + e.getMessage() + ); } public void testNoStack() { IOException e = expectThrows(IOException.class, () -> parse("no_stack.json")); - assertThat(e.getMessage(), - startsWith("Can't parse error from Elasticsearch [expected [stack_trace] cannot but " - + "didn't see it] at [line 5 col 3]. Response:\n{")); + assertThat( + e.getMessage(), + startsWith( + "Can't parse error from Elasticsearch [expected [stack_trace] cannot but " + + "didn't see it] at [line 5 col 3]. Response:\n{" + ) + ); } public void testNoType() { IOException e = expectThrows(IOException.class, () -> parse("no_type.json")); - assertThat(e.getMessage(), - startsWith("Can't parse error from Elasticsearch [expected [type] but didn't see it] at [line 5 col 3]. Response:\n{")); + assertThat( + e.getMessage(), + startsWith("Can't parse error from Elasticsearch [expected [type] but didn't see it] at [line 5 col 3]. Response:\n{") + ); } public void testInvalidJson() { @@ -97,7 +108,8 @@ public void testInvalidJson() { + "was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')] " + "at [line 1 col 1]. Response:\n" + "I'm not json at all", - e.getMessage()); + e.getMessage() + ); } public void testExceptionBuildingParser() { @@ -109,19 +121,32 @@ public int read() throws IOException { })); assertEquals( "Can't parse error from Elasticsearch [Testing error]. Attempted to include response but failed because [Testing error].", - e.getMessage()); + e.getMessage() + ); } public void testTotalGarbage() { - IOException e = expectThrows(IOException.class, () -> - RemoteFailure.parseFromResponse(new BytesArray(new byte[] { - (byte) 0xEF, (byte) 0xBB, (byte) 0xBF, // The UTF-8 BOM - (byte) 0xFF // An invalid UTF-8 character - }).streamInput())); - assertThat(e.getMessage(), - startsWith("Can't parse error from Elasticsearch [Unrecognized token 'ÿ': " - + "was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')] " - + "at [line 1 col 4]. Response:\n")); + IOException e = expectThrows( + IOException.class, + () -> RemoteFailure.parseFromResponse( + new BytesArray( + new byte[] { + (byte) 0xEF, + (byte) 0xBB, + (byte) 0xBF, // The UTF-8 BOM + (byte) 0xFF // An invalid UTF-8 character + } + ).streamInput() + ) + ); + assertThat( + e.getMessage(), + startsWith( + "Can't parse error from Elasticsearch [Unrecognized token 'ÿ': " + + "was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')] " + + "at [line 1 col 4]. Response:\n" + ) + ); } public void testTooBig() { @@ -133,18 +158,22 @@ public void testTooBig() { tooBig.append(" \"header\" : {\n"); int i = 0; while (tooBig.length() < RemoteFailure.MAX_RAW_RESPONSE) { - tooBig.append(" \"").append(String.format(Locale.ROOT, "%04d", i++)) + tooBig.append(" \"") + .append(String.format(Locale.ROOT, "%04d", i++)) .append("\" : \"lots and lots and lots and lots and lots of words\",\n"); } tooBig.append(" \"end\" : \"lots and lots and lots and lots and lots of words\"\n"); tooBig.append(" }\n"); tooBig.append("}\n"); - IOException e = expectThrows(IOException.class, () -> - RemoteFailure.parseFromResponse(new BytesArray(tooBig.toString()).streamInput())); + IOException e = expectThrows( + IOException.class, + () -> RemoteFailure.parseFromResponse(new BytesArray(tooBig.toString()).streamInput()) + ); assertEquals( "Can't parse error from Elasticsearch [expected [stack_trace] cannot but didn't see it] " + "at [line 7951 col 1]. Attempted to include response but failed because [Response too large].", - e.getMessage()); + e.getMessage() + ); } public void testFailureWithMetadata() throws IOException { @@ -196,8 +225,10 @@ public void testFailureWithMetadataAndRootCause() throws IOException { RemoteFailure failure = RemoteFailure.parseFromResponse(new BytesArray(json.toString()).streamInput()); assertEquals("invalid_index_name_exception", failure.type()); assertEquals("Invalid index name [_foo], must not start with '_'.", failure.reason()); - assertThat(failure.remoteTrace(), - containsString("[_foo] InvalidIndexNameException[Invalid index name [_foo], must not start with '_'.]")); + assertThat( + failure.remoteTrace(), + containsString("[_foo] InvalidIndexNameException[Invalid index name [_foo], must not start with '_'.]") + ); assertEquals(emptyMap(), failure.headers()); assertNotNull(failure.metadata()); assertEquals(2, failure.metadata().size()); @@ -207,8 +238,10 @@ public void testFailureWithMetadataAndRootCause() throws IOException { RemoteFailure cause = failure.cause(); assertEquals("invalid_index_name_exception", cause.type()); assertEquals("Invalid index name [_root], must not start with '_'.", cause.reason()); - assertThat(cause.remoteTrace(), - containsString("[_root] InvalidIndexNameException[Invalid index name [_root], must not start with '_'.]")); + assertThat( + cause.remoteTrace(), + containsString("[_root] InvalidIndexNameException[Invalid index name [_root], must not start with '_'.]") + ); assertEquals(emptyMap(), failure.headers()); assertNotNull(cause.metadata()); assertEquals(2, cause.metadata().size()); diff --git a/x-pack/plugin/sql/sql-client/src/test/java/org/elasticsearch/xpack/sql/client/UriUtilsTests.java b/x-pack/plugin/sql/sql-client/src/test/java/org/elasticsearch/xpack/sql/client/UriUtilsTests.java index e0c757bd9038d..af25b40220979 100644 --- a/x-pack/plugin/sql/sql-client/src/test/java/org/elasticsearch/xpack/sql/client/UriUtilsTests.java +++ b/x-pack/plugin/sql/sql-client/src/test/java/org/elasticsearch/xpack/sql/client/UriUtilsTests.java @@ -12,9 +12,9 @@ import java.util.Arrays; import static org.elasticsearch.xpack.sql.client.UriUtils.CredentialsRedaction.REDACTION_CHAR; +import static org.elasticsearch.xpack.sql.client.UriUtils.CredentialsRedaction.redactCredentialsInConnectionString; import static org.elasticsearch.xpack.sql.client.UriUtils.appendSegmentToPath; import static org.elasticsearch.xpack.sql.client.UriUtils.parseURI; -import static org.elasticsearch.xpack.sql.client.UriUtils.CredentialsRedaction.redactCredentialsInConnectionString; import static org.elasticsearch.xpack.sql.client.UriUtils.removeQuery; public class UriUtilsTests extends ESTestCase { @@ -82,28 +82,38 @@ public void testHttpQuery() throws Exception { } public void testUnsupportedProtocol() throws Exception { - assertEquals("Invalid connection scheme [ftp] configuration: only http and https protocols are supported", - expectThrows(IllegalArgumentException.class, () -> parseURI("ftp://server:9201/", DEFAULT_URI)).getMessage()); + assertEquals( + "Invalid connection scheme [ftp] configuration: only http and https protocols are supported", + expectThrows(IllegalArgumentException.class, () -> parseURI("ftp://server:9201/", DEFAULT_URI)).getMessage() + ); } public void testMalformedWhiteSpace() throws Exception { - assertEquals("Invalid connection configuration: Illegal character in authority at index 7: http:// ", - expectThrows(IllegalArgumentException.class, () -> parseURI(" ", DEFAULT_URI)).getMessage()); + assertEquals( + "Invalid connection configuration: Illegal character in authority at index 7: http:// ", + expectThrows(IllegalArgumentException.class, () -> parseURI(" ", DEFAULT_URI)).getMessage() + ); } public void testNoRedaction() { - assertEquals("Invalid connection configuration: Illegal character in fragment at index 16: HTTP://host#frag#ment", - expectThrows(IllegalArgumentException.class, () -> parseURI("HTTP://host#frag#ment", DEFAULT_URI)).getMessage()); + assertEquals( + "Invalid connection configuration: Illegal character in fragment at index 16: HTTP://host#frag#ment", + expectThrows(IllegalArgumentException.class, () -> parseURI("HTTP://host#frag#ment", DEFAULT_URI)).getMessage() + ); } public void testSimpleUriRedaction() { - assertEquals("http://*************@host:9200/path?user=****&password=****", - redactCredentialsInConnectionString("http://user:password@host:9200/path?user=user&password=pass")); + assertEquals( + "http://*************@host:9200/path?user=****&password=****", + redactCredentialsInConnectionString("http://user:password@host:9200/path?user=user&password=pass") + ); } public void testSimpleConnectionStringRedaction() { - assertEquals("*************@host:9200/path?user=****&password=****", - redactCredentialsInConnectionString("user:password@host:9200/path?user=user&password=pass")); + assertEquals( + "*************@host:9200/path?user=****&password=****", + redactCredentialsInConnectionString("user:password@host:9200/path?user=user&password=pass") + ); } public void testNoRedactionInvalidHost() { @@ -111,43 +121,59 @@ public void testNoRedactionInvalidHost() { } public void testUriRedactionInvalidUserPart() { - assertEquals("http://*************@@host:9200/path?user=****&password=****&at=@sign", - redactCredentialsInConnectionString("http://user:password@@host:9200/path?user=user&password=pass&at=@sign")); + assertEquals( + "http://*************@@host:9200/path?user=****&password=****&at=@sign", + redactCredentialsInConnectionString("http://user:password@@host:9200/path?user=user&password=pass&at=@sign") + ); } public void testUriRedactionInvalidHost() { - assertEquals("http://*************@ho%st:9200/path?user=****&password=****&at=@sign", - redactCredentialsInConnectionString("http://user:password@ho%st:9200/path?user=user&password=pass&at=@sign")); + assertEquals( + "http://*************@ho%st:9200/path?user=****&password=****&at=@sign", + redactCredentialsInConnectionString("http://user:password@ho%st:9200/path?user=user&password=pass&at=@sign") + ); } public void testUriRedactionInvalidPort() { - assertEquals("http://*************@host:port/path?user=****&password=****&at=@sign", - redactCredentialsInConnectionString("http://user:password@host:port/path?user=user&password=pass&at=@sign")); + assertEquals( + "http://*************@host:port/path?user=****&password=****&at=@sign", + redactCredentialsInConnectionString("http://user:password@host:port/path?user=user&password=pass&at=@sign") + ); } public void testUriRedactionInvalidPath() { - assertEquals("http://*************@host:9200/pa^th?user=****&password=****", - redactCredentialsInConnectionString("http://user:password@host:9200/pa^th?user=user&password=pass")); + assertEquals( + "http://*************@host:9200/pa^th?user=****&password=****", + redactCredentialsInConnectionString("http://user:password@host:9200/pa^th?user=user&password=pass") + ); } public void testUriRedactionInvalidQuery() { - assertEquals("http://*************@host:9200/path?user=****&password=****&invali^d", - redactCredentialsInConnectionString("http://user:password@host:9200/path?user=user&password=pass&invali^d")); + assertEquals( + "http://*************@host:9200/path?user=****&password=****&invali^d", + redactCredentialsInConnectionString("http://user:password@host:9200/path?user=user&password=pass&invali^d") + ); } public void testUriRedactionInvalidFragment() { - assertEquals("https://host:9200/path?usr=****&passwo=****#ssl=5#", - redactCredentialsInConnectionString("https://host:9200/path?usr=user&passwo=pass#ssl=5#")); + assertEquals( + "https://host:9200/path?usr=****&passwo=****#ssl=5#", + redactCredentialsInConnectionString("https://host:9200/path?usr=user&passwo=pass#ssl=5#") + ); } public void testUriRedactionMisspelledUser() { - assertEquals("https://host:9200/path?usr=****&password=****", - redactCredentialsInConnectionString("https://host:9200/path?usr=user&password=pass")); + assertEquals( + "https://host:9200/path?usr=****&password=****", + redactCredentialsInConnectionString("https://host:9200/path?usr=user&password=pass") + ); } public void testUriRedactionMisspelledUserAndPassword() { - assertEquals("https://host:9200/path?usr=****&passwo=****", - redactCredentialsInConnectionString("https://host:9200/path?usr=user&passwo=pass")); + assertEquals( + "https://host:9200/path?usr=****&passwo=****", + redactCredentialsInConnectionString("https://host:9200/path?usr=user&passwo=pass") + ); } public void testUriRedactionNoScheme() { @@ -216,23 +242,44 @@ public void testUriRandomRedact() { } public void testUriRedactionMissingSeparatorBetweenUserAndPassword() { - assertEquals("https://host:9200/path?user=*****************", - redactCredentialsInConnectionString("https://host:9200/path?user=userpassword=pass")); + assertEquals( + "https://host:9200/path?user=*****************", + redactCredentialsInConnectionString("https://host:9200/path?user=userpassword=pass") + ); } public void testUriRedactionMissingSeparatorBeforePassword() { - assertEquals("https://host:9200/path?user=****&foo=barpassword=********&bar=foo", - redactCredentialsInConnectionString("https://host:9200/path?user=user&foo=barpassword=password&bar=foo")); + assertEquals( + "https://host:9200/path?user=****&foo=barpassword=********&bar=foo", + redactCredentialsInConnectionString("https://host:9200/path?user=user&foo=barpassword=password&bar=foo") + ); } // tests that no other option is "similar" to the credential options and be inadvertently redacted public void testUriRedactionAllOptions() { StringBuilder cs = new StringBuilder("https://host:9200/path?"); - String[] options = {"timezone", "connect.timeout", "network.timeout", "page.timeout", "page.size", "query.timeout", "user", - "password", "ssl", "ssl.keystore.location", "ssl.keystore.pass", "ssl.keystore.type", "ssl.truststore.location", - "ssl.truststore.pass", "ssl.truststore.type", "ssl.protocol", "proxy.http", "proxy.socks", "field.multi.value.leniency", - "index.include.frozen", "validate.properties" - }; + String[] options = { + "timezone", + "connect.timeout", + "network.timeout", + "page.timeout", + "page.size", + "query.timeout", + "user", + "password", + "ssl", + "ssl.keystore.location", + "ssl.keystore.pass", + "ssl.keystore.type", + "ssl.truststore.location", + "ssl.truststore.pass", + "ssl.truststore.type", + "ssl.protocol", + "proxy.http", + "proxy.socks", + "field.multi.value.leniency", + "index.include.frozen", + "validate.properties" }; Arrays.stream(options).forEach(e -> cs.append(e).append("=").append(e).append("&")); String connStr = cs.substring(0, cs.length() - 1); String expected = connStr.replace("user=user", "user=****"); @@ -245,18 +292,24 @@ public void testUriRedactionBrokenHost() { } public void testUriRedactionDisabled() { - assertEquals("HTTPS://host:9200/path?user=user;password=pass", - redactCredentialsInConnectionString("HTTPS://host:9200/path?user=user;password=pass")); + assertEquals( + "HTTPS://host:9200/path?user=user;password=pass", + redactCredentialsInConnectionString("HTTPS://host:9200/path?user=user;password=pass") + ); } public void testRemoveQuery() throws Exception { - assertEquals(URI.create("http://server:9100"), - removeQuery(URI.create("http://server:9100?query"), "http://server:9100?query", DEFAULT_URI)); + assertEquals( + URI.create("http://server:9100"), + removeQuery(URI.create("http://server:9100?query"), "http://server:9100?query", DEFAULT_URI) + ); } public void testRemoveQueryTrailingSlash() throws Exception { - assertEquals(URI.create("http://server:9100/"), - removeQuery(URI.create("http://server:9100/?query"), "http://server:9100/?query", DEFAULT_URI)); + assertEquals( + URI.create("http://server:9100/"), + removeQuery(URI.create("http://server:9100/?query"), "http://server:9100/?query", DEFAULT_URI) + ); } public void testRemoveQueryNoQuery() throws Exception { @@ -272,8 +325,10 @@ public void testAppendNullSegmentToPath() throws Exception { } public void testAppendSegmentToNullPath() throws Exception { - assertEquals("URI must not be null", - expectThrows(IllegalArgumentException.class, () -> appendSegmentToPath(null, "/_sql")).getMessage()); + assertEquals( + "URI must not be null", + expectThrows(IllegalArgumentException.class, () -> appendSegmentToPath(null, "/_sql")).getMessage() + ); } public void testAppendSegmentToEmptyPath() throws Exception { @@ -293,17 +348,17 @@ public void testAppendSqlSegmentNoSlashToPath() throws Exception { } public void testAppendSegmentToPath() throws Exception { - assertEquals(URI.create("http://server:9100/es_rest/_sql"), - appendSegmentToPath(URI.create("http://server:9100/es_rest"), "/_sql")); + assertEquals(URI.create("http://server:9100/es_rest/_sql"), appendSegmentToPath(URI.create("http://server:9100/es_rest"), "/_sql")); } public void testAppendSegmentNoSlashToPath() throws Exception { - assertEquals(URI.create("http://server:9100/es_rest/_sql"), - appendSegmentToPath(URI.create("http://server:9100/es_rest"), "_sql")); + assertEquals(URI.create("http://server:9100/es_rest/_sql"), appendSegmentToPath(URI.create("http://server:9100/es_rest"), "_sql")); } public void testAppendSegmentTwoSlashesToPath() throws Exception { - assertEquals(URI.create("https://server:9100/es_rest/_sql"), - appendSegmentToPath(URI.create("https://server:9100/es_rest/"), "/_sql")); + assertEquals( + URI.create("https://server:9100/es_rest/_sql"), + appendSegmentToPath(URI.create("https://server:9100/es_rest/"), "/_sql") + ); } } diff --git a/x-pack/plugin/sql/sql-client/src/test/java/org/elasticsearch/xpack/sql/client/VersionTests.java b/x-pack/plugin/sql/sql-client/src/test/java/org/elasticsearch/xpack/sql/client/VersionTests.java index 62446e49e2417..1e33fafbe56fe 100644 --- a/x-pack/plugin/sql/sql-client/src/test/java/org/elasticsearch/xpack/sql/client/VersionTests.java +++ b/x-pack/plugin/sql/sql-client/src/test/java/org/elasticsearch/xpack/sql/client/VersionTests.java @@ -32,7 +32,6 @@ public void testInvalidVersion() { assertEquals("Invalid version format [7.1]", err.getMessage()); } - private static final String JAR_PATH_SEPARATOR = "!/"; private static String versionString(byte[] parts) { @@ -46,7 +45,7 @@ private static String versionString(byte[] parts) { private static byte[] randomVersion() { byte[] parts = new byte[3]; - for (int i = 0; i < parts.length; i ++) { + for (int i = 0; i < parts.length; i++) { parts[i] = (byte) randomIntBetween(0, SqlVersion.REVISION_MULTIPLIER - 1); } return parts; @@ -103,8 +102,10 @@ public void testVersionFromJarInJar() throws IOException { Path innerJarPath = createDriverJar(parts); // create the uberjar and embed the jdbc driver one into it - try (BufferedInputStream in = new BufferedInputStream(Files.newInputStream(innerJarPath)); - JarOutputStream out = new JarOutputStream(Files.newOutputStream(jarPath, StandardOpenOption.CREATE), new Manifest())) { + try ( + BufferedInputStream in = new BufferedInputStream(Files.newInputStream(innerJarPath)); + JarOutputStream out = new JarOutputStream(Files.newOutputStream(jarPath, StandardOpenOption.CREATE), new Manifest()) + ) { JarEntry entry = new JarEntry(innerJarPath.getFileName() + JAR_PATH_SEPARATOR); out.putNextEntry(entry); @@ -118,8 +119,9 @@ public void testVersionFromJarInJar() throws IOException { } } - URL jarInJar = new URL("jar:" + jarPath.toUri().toURL().toString() + JAR_PATH_SEPARATOR + innerJarPath.getFileName() + - JAR_PATH_SEPARATOR); + URL jarInJar = new URL( + "jar:" + jarPath.toUri().toURL().toString() + JAR_PATH_SEPARATOR + innerJarPath.getFileName() + JAR_PATH_SEPARATOR + ); SqlVersion version = ClientVersion.extractVersion(jarInJar); assertEquals(parts[0], version.major); diff --git a/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/ColumnInfo.java b/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/ColumnInfo.java index b6781bc8bc46c..bf02d192bfdec 100644 --- a/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/ColumnInfo.java +++ b/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/ColumnInfo.java @@ -6,8 +6,8 @@ */ package org.elasticsearch.xpack.sql.proto; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -25,13 +25,16 @@ */ public class ColumnInfo implements ToXContentObject { - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("column_info", true, objects -> - new ColumnInfo( - objects[0] == null ? "" : (String) objects[0], - (String) objects[1], - (String) objects[2], - (Integer) objects[3])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "column_info", + true, + objects -> new ColumnInfo( + objects[0] == null ? "" : (String) objects[0], + (String) objects[1], + (String) objects[2], + (Integer) objects[3] + ) + ); private static final ParseField TABLE = new ParseField("table"); private static final ParseField NAME = new ParseField("name"); @@ -78,7 +81,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws return builder.endObject(); } - public static ColumnInfo fromXContent(XContentParser parser) { return PARSER.apply(parser, null); } @@ -120,10 +122,10 @@ public boolean equals(Object o) { return false; } ColumnInfo that = (ColumnInfo) o; - return Objects.equals(displaySize, that.displaySize) && - Objects.equals(table, that.table) && - Objects.equals(name, that.name) && - Objects.equals(esType, that.esType); + return Objects.equals(displaySize, that.displaySize) + && Objects.equals(table, that.table) + && Objects.equals(name, that.name) + && Objects.equals(esType, that.esType); } @Override diff --git a/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/MainResponse.java b/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/MainResponse.java index 54c5c8db3be6c..f30fefe148397 100644 --- a/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/MainResponse.java +++ b/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/MainResponse.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.sql.proto; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.util.Objects; @@ -22,8 +22,7 @@ public class MainResponse { private String clusterName; private String clusterUuid; - private MainResponse() { - } + private MainResponse() {} public MainResponse(String nodeName, String version, String clusterName, String clusterUuid) { this.nodeName = nodeName; @@ -49,18 +48,22 @@ public String getClusterUuid() { return clusterUuid; } - private static final ObjectParser PARSER = new ObjectParser<>(MainResponse.class.getName(), true, - MainResponse::new); + private static final ObjectParser PARSER = new ObjectParser<>( + MainResponse.class.getName(), + true, + MainResponse::new + ); static { PARSER.declareString((response, value) -> response.nodeName = value, new ParseField("name")); PARSER.declareString((response, value) -> response.clusterName = value, new ParseField("cluster_name")); PARSER.declareString((response, value) -> response.clusterUuid = value, new ParseField("cluster_uuid")); - PARSER.declareString((response, value) -> { - }, new ParseField("tagline")); - PARSER.declareObject((response, value) -> { - response.version = (String) value.get("number"); - }, (parser, context) -> parser.map(), new ParseField("version")); + PARSER.declareString((response, value) -> {}, new ParseField("tagline")); + PARSER.declareObject( + (response, value) -> { response.version = (String) value.get("number"); }, + (parser, context) -> parser.map(), + new ParseField("version") + ); } public static MainResponse fromXContent(XContentParser parser) { @@ -76,10 +79,10 @@ public boolean equals(Object o) { return false; } MainResponse other = (MainResponse) o; - return Objects.equals(nodeName, other.nodeName) && - Objects.equals(version, other.version) && - Objects.equals(clusterUuid, other.clusterUuid) && - Objects.equals(clusterName, other.clusterName); + return Objects.equals(nodeName, other.nodeName) + && Objects.equals(version, other.version) + && Objects.equals(clusterUuid, other.clusterUuid) + && Objects.equals(clusterName, other.clusterName); } @Override diff --git a/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/Mode.java b/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/Mode.java index 3b968b159d6d1..1f324a87560ee 100644 --- a/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/Mode.java +++ b/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/Mode.java @@ -25,7 +25,6 @@ public static Mode fromString(String mode) { return Mode.valueOf(mode.toUpperCase(Locale.ROOT)); } - @Override public String toString() { return this.name().toLowerCase(Locale.ROOT); diff --git a/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/ProtoUtils.java b/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/ProtoUtils.java index ff8fc65b29a2f..1fb7217c97b6f 100644 --- a/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/ProtoUtils.java +++ b/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/ProtoUtils.java @@ -19,8 +19,7 @@ public final class ProtoUtils { - private ProtoUtils() { - } + private ProtoUtils() {} /** * Parses a generic value from the XContent stream @@ -28,7 +27,7 @@ private ProtoUtils() { public static Object parseFieldsValue(XContentParser parser) throws IOException { XContentParser.Token token = parser.currentToken(); if (token == XContentParser.Token.VALUE_STRING) { - //binary values will be parsed back and returned as base64 strings when reading from json and yaml + // binary values will be parsed back and returned as base64 strings when reading from json and yaml return parser.text(); } else if (token == XContentParser.Token.VALUE_NUMBER) { return parser.numberValue(); diff --git a/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/SqlClearCursorResponse.java b/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/SqlClearCursorResponse.java index 437b8b7cccfea..b41e1521f126f 100644 --- a/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/SqlClearCursorResponse.java +++ b/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/SqlClearCursorResponse.java @@ -6,8 +6,8 @@ */ package org.elasticsearch.xpack.sql.proto; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.util.Objects; @@ -20,15 +20,16 @@ public class SqlClearCursorResponse { public static final ParseField SUCCEEDED = new ParseField("succeeded"); - public static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>(SqlClearCursorResponse.class.getName(), true, - objects -> new SqlClearCursorResponse(objects[0] == null ? false : (boolean) objects[0])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + SqlClearCursorResponse.class.getName(), + true, + objects -> new SqlClearCursorResponse(objects[0] == null ? false : (boolean) objects[0]) + ); static { PARSER.declareBoolean(optionalConstructorArg(), SUCCEEDED); } - private final boolean succeeded; public SqlClearCursorResponse(boolean succeeded) { diff --git a/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/SqlQueryRequest.java b/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/SqlQueryRequest.java index be79da8891764..b1386f6122c37 100644 --- a/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/SqlQueryRequest.java +++ b/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/SqlQueryRequest.java @@ -67,11 +67,26 @@ public class SqlQueryRequest extends AbstractSqlRequest { private final boolean keepOnCompletion; private final TimeValue keepAlive; - public SqlQueryRequest(String query, List params, ZoneId zoneId, String catalog, int fetchSize, - TimeValue requestTimeout, TimeValue pageTimeout, ToXContent filter, Boolean columnar, - String cursor, RequestInfo requestInfo, boolean fieldMultiValueLeniency, boolean indexIncludeFrozen, - Boolean binaryCommunication, Map runtimeMappings, TimeValue waitForCompletionTimeout, - boolean keepOnCompletion, TimeValue keepAlive) { + public SqlQueryRequest( + String query, + List params, + ZoneId zoneId, + String catalog, + int fetchSize, + TimeValue requestTimeout, + TimeValue pageTimeout, + ToXContent filter, + Boolean columnar, + String cursor, + RequestInfo requestInfo, + boolean fieldMultiValueLeniency, + boolean indexIncludeFrozen, + Boolean binaryCommunication, + Map runtimeMappings, + TimeValue waitForCompletionTimeout, + boolean keepOnCompletion, + TimeValue keepAlive + ) { super(requestInfo); this.query = query; this.params = params; @@ -92,18 +107,69 @@ public SqlQueryRequest(String query, List params, ZoneId zon this.keepAlive = keepAlive; } - public SqlQueryRequest(String query, List params, ZoneId zoneId, String catalog, int fetchSize, - TimeValue requestTimeout, TimeValue pageTimeout, ToXContent filter, Boolean columnar, - String cursor, RequestInfo requestInfo, boolean fieldMultiValueLeniency, boolean indexIncludeFrozen, - Boolean binaryCommunication, Map runtimeMappings) { - this(query, params, zoneId, catalog, fetchSize, requestTimeout, pageTimeout, filter, columnar, cursor, requestInfo, - fieldMultiValueLeniency, indexIncludeFrozen, binaryCommunication, runtimeMappings, - Protocol.DEFAULT_WAIT_FOR_COMPLETION_TIMEOUT, Protocol.DEFAULT_KEEP_ON_COMPLETION, Protocol.DEFAULT_KEEP_ALIVE); + public SqlQueryRequest( + String query, + List params, + ZoneId zoneId, + String catalog, + int fetchSize, + TimeValue requestTimeout, + TimeValue pageTimeout, + ToXContent filter, + Boolean columnar, + String cursor, + RequestInfo requestInfo, + boolean fieldMultiValueLeniency, + boolean indexIncludeFrozen, + Boolean binaryCommunication, + Map runtimeMappings + ) { + this( + query, + params, + zoneId, + catalog, + fetchSize, + requestTimeout, + pageTimeout, + filter, + columnar, + cursor, + requestInfo, + fieldMultiValueLeniency, + indexIncludeFrozen, + binaryCommunication, + runtimeMappings, + Protocol.DEFAULT_WAIT_FOR_COMPLETION_TIMEOUT, + Protocol.DEFAULT_KEEP_ON_COMPLETION, + Protocol.DEFAULT_KEEP_ALIVE + ); } - public SqlQueryRequest(String cursor, TimeValue requestTimeout, TimeValue pageTimeout, RequestInfo requestInfo, - boolean binaryCommunication) { - this("", emptyList(), Protocol.TIME_ZONE, null, Protocol.FETCH_SIZE, requestTimeout, pageTimeout, null, false, - cursor, requestInfo, Protocol.FIELD_MULTI_VALUE_LENIENCY, Protocol.INDEX_INCLUDE_FROZEN, binaryCommunication, emptyMap()); + + public SqlQueryRequest( + String cursor, + TimeValue requestTimeout, + TimeValue pageTimeout, + RequestInfo requestInfo, + boolean binaryCommunication + ) { + this( + "", + emptyList(), + Protocol.TIME_ZONE, + null, + Protocol.FETCH_SIZE, + requestTimeout, + pageTimeout, + null, + false, + cursor, + requestInfo, + Protocol.FIELD_MULTI_VALUE_LENIENCY, + Protocol.INDEX_INCLUDE_FROZEN, + binaryCommunication, + emptyMap() + ); } /** @@ -139,7 +205,6 @@ public String catalog() { return catalog; } - /** * Hint about how many results to fetch at once. */ @@ -217,29 +282,45 @@ public boolean equals(Object o) { } SqlQueryRequest that = (SqlQueryRequest) o; return fetchSize == that.fetchSize - && Objects.equals(query, that.query) - && Objects.equals(params, that.params) - && Objects.equals(zoneId, that.zoneId) - && Objects.equals(catalog, that.catalog) - && Objects.equals(requestTimeout, that.requestTimeout) - && Objects.equals(pageTimeout, that.pageTimeout) - && Objects.equals(filter, that.filter) - && Objects.equals(columnar, that.columnar) - && Objects.equals(cursor, that.cursor) - && fieldMultiValueLeniency == that.fieldMultiValueLeniency - && indexIncludeFrozen == that.indexIncludeFrozen - && Objects.equals(binaryCommunication, that.binaryCommunication) - && Objects.equals(runtimeMappings, that.runtimeMappings) - && Objects.equals(waitForCompletionTimeout, that.waitForCompletionTimeout) - && keepOnCompletion == that.keepOnCompletion - && Objects.equals(keepAlive, that.keepAlive); + && Objects.equals(query, that.query) + && Objects.equals(params, that.params) + && Objects.equals(zoneId, that.zoneId) + && Objects.equals(catalog, that.catalog) + && Objects.equals(requestTimeout, that.requestTimeout) + && Objects.equals(pageTimeout, that.pageTimeout) + && Objects.equals(filter, that.filter) + && Objects.equals(columnar, that.columnar) + && Objects.equals(cursor, that.cursor) + && fieldMultiValueLeniency == that.fieldMultiValueLeniency + && indexIncludeFrozen == that.indexIncludeFrozen + && Objects.equals(binaryCommunication, that.binaryCommunication) + && Objects.equals(runtimeMappings, that.runtimeMappings) + && Objects.equals(waitForCompletionTimeout, that.waitForCompletionTimeout) + && keepOnCompletion == that.keepOnCompletion + && Objects.equals(keepAlive, that.keepAlive); } @Override public int hashCode() { - return Objects.hash(super.hashCode(), query, zoneId, catalog, fetchSize, requestTimeout, pageTimeout, - filter, columnar, cursor, fieldMultiValueLeniency, indexIncludeFrozen, binaryCommunication, runtimeMappings, - waitForCompletionTimeout, keepOnCompletion, keepAlive); + return Objects.hash( + super.hashCode(), + query, + zoneId, + catalog, + fetchSize, + requestTimeout, + pageTimeout, + filter, + columnar, + cursor, + fieldMultiValueLeniency, + indexIncludeFrozen, + binaryCommunication, + runtimeMappings, + waitForCompletionTimeout, + keepOnCompletion, + keepAlive + ); } @Override diff --git a/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/SqlQueryResponse.java b/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/SqlQueryResponse.java index e70ef6134ca2a..972e263ad4765 100644 --- a/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/SqlQueryResponse.java +++ b/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/SqlQueryResponse.java @@ -7,9 +7,9 @@ package org.elasticsearch.xpack.sql.proto; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -32,14 +32,18 @@ public class SqlQueryResponse { @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("sql", true, - objects -> new SqlQueryResponse( - objects[0] == null ? "" : (String) objects[0], - (List) objects[1], - (List>) objects[2], - (String) objects[3], - objects[4] != null && (boolean) objects[4], - objects[5] != null && (boolean) objects[5])); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "sql", + true, + objects -> new SqlQueryResponse( + objects[0] == null ? "" : (String) objects[0], + (List) objects[1], + (List>) objects[2], + (String) objects[3], + objects[4] != null && (boolean) objects[4], + objects[5] != null && (boolean) objects[5] + ) + ); public static final ParseField CURSOR = new ParseField(CURSOR_NAME); public static final ParseField COLUMNS = new ParseField(COLUMNS_NAME); @@ -71,8 +75,14 @@ public SqlQueryResponse(String cursor, @Nullable List columns, List< this(cursor, columns, rows, null, false, false); } - public SqlQueryResponse(String cursor, @Nullable List columns, List> rows, String asyncExecutionId, - boolean isPartial, boolean isRunning) { + public SqlQueryResponse( + String cursor, + @Nullable List columns, + List> rows, + String asyncExecutionId, + boolean isPartial, + boolean isRunning + ) { this.cursor = cursor; this.columns = columns; this.rows = rows; @@ -148,12 +158,12 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; SqlQueryResponse that = (SqlQueryResponse) o; - return Objects.equals(cursor, that.cursor) && - Objects.equals(columns, that.columns) && - Objects.equals(rows, that.rows) && - Objects.equals(asyncExecutionId, that.asyncExecutionId) && - isPartial == that.isPartial && - isRunning == that.isRunning; + return Objects.equals(cursor, that.cursor) + && Objects.equals(columns, that.columns) + && Objects.equals(rows, that.rows) + && Objects.equals(asyncExecutionId, that.asyncExecutionId) + && isPartial == that.isPartial + && isRunning == that.isRunning; } @Override diff --git a/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/SqlTypedParamValue.java b/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/SqlTypedParamValue.java index bbdf362a4366c..f01a691a61dbc 100644 --- a/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/SqlTypedParamValue.java +++ b/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/SqlTypedParamValue.java @@ -6,9 +6,9 @@ */ package org.elasticsearch.xpack.sql.proto; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentLocation; @@ -24,10 +24,11 @@ * Represent a strongly typed parameter value */ public class SqlTypedParamValue implements ToXContentObject { - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>("params", true, objects -> - new SqlTypedParamValue((String) objects[1], objects[0] - )); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "params", + true, + objects -> new SqlTypedParamValue((String) objects[1], objects[0]) + ); private static final ParseField VALUE = new ParseField("value"); private static final ParseField TYPE = new ParseField("type"); @@ -91,8 +92,8 @@ public boolean equals(Object o) { } SqlTypedParamValue that = (SqlTypedParamValue) o; return Objects.equals(value, that.value) - && Objects.equals(type, that.type) - && Objects.equals(hasExplicitType, that.hasExplicitType); + && Objects.equals(type, that.type) + && Objects.equals(hasExplicitType, that.hasExplicitType); } @Override diff --git a/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/SqlVersion.java b/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/SqlVersion.java index 32cf6f7a870ca..ff74c14797a50 100644 --- a/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/SqlVersion.java +++ b/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/SqlVersion.java @@ -17,7 +17,7 @@ * aid to establish the compatibility between them. *

    */ -public class SqlVersion implements Comparable{ +public class SqlVersion implements Comparable { public final int id; public final String version; // originally provided String representation @@ -54,16 +54,17 @@ protected SqlVersion(String version, byte... parts) { revision = parts[2]; build = (parts.length >= 4) ? parts[3] : REVISION_MULTIPLIER - 1; - if ((major | minor | revision | build) < 0 || - minor >= REVISION_MULTIPLIER || revision >= REVISION_MULTIPLIER || build >= REVISION_MULTIPLIER) { - throw new InvalidParameterException("Invalid version initialisers [" + major + ", " + minor + ", " + revision + ", " + - build + "]"); + if ((major | minor | revision | build) < 0 + || minor >= REVISION_MULTIPLIER + || revision >= REVISION_MULTIPLIER + || build >= REVISION_MULTIPLIER) { + throw new InvalidParameterException( + "Invalid version initialisers [" + major + ", " + minor + ", " + revision + ", " + build + "]" + ); } - id = Integer.valueOf(major) * MAJOR_MULTIPLIER - + Integer.valueOf(minor) * MINOR_MULTIPLIER - + Integer.valueOf(revision) * REVISION_MULTIPLIER - + Integer.valueOf(build); + id = Integer.valueOf(major) * MAJOR_MULTIPLIER + Integer.valueOf(minor) * MINOR_MULTIPLIER + Integer.valueOf(revision) + * REVISION_MULTIPLIER + Integer.valueOf(build); } public static SqlVersion fromString(String version) { @@ -100,7 +101,7 @@ protected static byte[] from(String ver) { private static String toString(byte... parts) { assert parts.length >= 1 : "Version must contain at least a Major component"; String ver = String.valueOf(parts[0]); - for (int i = 1; i < parts.length; i ++) { + for (int i = 1; i < parts.length; i++) { ver += "." + parts[i]; } return ver; diff --git a/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/StringUtils.java b/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/StringUtils.java index e28962bd08740..cf0a189a44b96 100644 --- a/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/StringUtils.java +++ b/x-pack/plugin/sql/sql-proto/src/main/java/org/elasticsearch/xpack/sql/proto/StringUtils.java @@ -30,53 +30,49 @@ public final class StringUtils { public static final String EMPTY = ""; - public static final DateTimeFormatter ISO_DATETIME_WITH_NANOS = new DateTimeFormatterBuilder() - .parseCaseInsensitive() - .append(ISO_LOCAL_DATE) - .appendLiteral('T') - .appendValue(HOUR_OF_DAY, 2) - .appendLiteral(':') - .appendValue(MINUTE_OF_HOUR, 2) - .appendLiteral(':') - .appendValue(SECOND_OF_MINUTE, 2) - .appendFraction(NANO_OF_SECOND, 3, 9, true) - .appendOffsetId() - .toFormatter(Locale.ROOT); - - public static final DateTimeFormatter ISO_DATETIME_WITH_MILLIS= new DateTimeFormatterBuilder() - .parseCaseInsensitive() - .append(ISO_LOCAL_DATE) - .appendLiteral('T') - .appendValue(HOUR_OF_DAY, 2) - .appendLiteral(':') - .appendValue(MINUTE_OF_HOUR, 2) - .appendLiteral(':') - .appendValue(SECOND_OF_MINUTE, 2) - .appendFraction(MILLI_OF_SECOND, 3, 3, true) - .appendOffsetId() - .toFormatter(Locale.ROOT); - - public static final DateTimeFormatter ISO_TIME_WITH_NANOS = new DateTimeFormatterBuilder() - .parseCaseInsensitive() - .appendValue(HOUR_OF_DAY, 2) - .appendLiteral(':') - .appendValue(MINUTE_OF_HOUR, 2) - .appendLiteral(':') - .appendValue(SECOND_OF_MINUTE, 2) - .appendFraction(NANO_OF_SECOND, 3, 9, true) - .appendOffsetId() - .toFormatter(Locale.ROOT); - - public static final DateTimeFormatter ISO_TIME_WITH_MILLIS = new DateTimeFormatterBuilder() - .parseCaseInsensitive() - .appendValue(HOUR_OF_DAY, 2) - .appendLiteral(':') - .appendValue(MINUTE_OF_HOUR, 2) - .appendLiteral(':') - .appendValue(SECOND_OF_MINUTE, 2) - .appendFraction(MILLI_OF_SECOND, 3, 3, true) - .appendOffsetId() - .toFormatter(Locale.ROOT); + public static final DateTimeFormatter ISO_DATETIME_WITH_NANOS = new DateTimeFormatterBuilder().parseCaseInsensitive() + .append(ISO_LOCAL_DATE) + .appendLiteral('T') + .appendValue(HOUR_OF_DAY, 2) + .appendLiteral(':') + .appendValue(MINUTE_OF_HOUR, 2) + .appendLiteral(':') + .appendValue(SECOND_OF_MINUTE, 2) + .appendFraction(NANO_OF_SECOND, 3, 9, true) + .appendOffsetId() + .toFormatter(Locale.ROOT); + + public static final DateTimeFormatter ISO_DATETIME_WITH_MILLIS = new DateTimeFormatterBuilder().parseCaseInsensitive() + .append(ISO_LOCAL_DATE) + .appendLiteral('T') + .appendValue(HOUR_OF_DAY, 2) + .appendLiteral(':') + .appendValue(MINUTE_OF_HOUR, 2) + .appendLiteral(':') + .appendValue(SECOND_OF_MINUTE, 2) + .appendFraction(MILLI_OF_SECOND, 3, 3, true) + .appendOffsetId() + .toFormatter(Locale.ROOT); + + public static final DateTimeFormatter ISO_TIME_WITH_NANOS = new DateTimeFormatterBuilder().parseCaseInsensitive() + .appendValue(HOUR_OF_DAY, 2) + .appendLiteral(':') + .appendValue(MINUTE_OF_HOUR, 2) + .appendLiteral(':') + .appendValue(SECOND_OF_MINUTE, 2) + .appendFraction(NANO_OF_SECOND, 3, 9, true) + .appendOffsetId() + .toFormatter(Locale.ROOT); + + public static final DateTimeFormatter ISO_TIME_WITH_MILLIS = new DateTimeFormatterBuilder().parseCaseInsensitive() + .appendValue(HOUR_OF_DAY, 2) + .appendLiteral(':') + .appendValue(MINUTE_OF_HOUR, 2) + .appendLiteral(':') + .appendValue(SECOND_OF_MINUTE, 2) + .appendFraction(MILLI_OF_SECOND, 3, 3, true) + .appendOffsetId() + .toFormatter(Locale.ROOT); private static final int SECONDS_PER_MINUTE = 60; private static final int SECONDS_PER_HOUR = SECONDS_PER_MINUTE * 60; diff --git a/x-pack/plugin/sql/sql-proto/src/test/java/org/elasticsearch/xpack/sql/proto/ProtoUtilsTests.java b/x-pack/plugin/sql/sql-proto/src/test/java/org/elasticsearch/xpack/sql/proto/ProtoUtilsTests.java index fc59c6e176d63..d7dedc57b1e32 100644 --- a/x-pack/plugin/sql/sql-proto/src/test/java/org/elasticsearch/xpack/sql/proto/ProtoUtilsTests.java +++ b/x-pack/plugin/sql/sql-proto/src/test/java/org/elasticsearch/xpack/sql/proto/ProtoUtilsTests.java @@ -7,11 +7,11 @@ package org.elasticsearch.xpack.sql.proto; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.ESTestCase; import java.io.IOException; @@ -27,8 +27,11 @@ public void testGenericValueParsing() throws IOException { return builder; }); - XContentParser parser = - JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json); + XContentParser parser = JsonXContent.jsonXContent.createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + json + ); assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken()); while (parser.nextToken() != XContentParser.Token.END_OBJECT) { diff --git a/x-pack/plugin/sql/sql-proto/src/test/java/org/elasticsearch/xpack/sql/proto/SqlVersionTests.java b/x-pack/plugin/sql/sql-proto/src/test/java/org/elasticsearch/xpack/sql/proto/SqlVersionTests.java index f88a0ff9d86a4..65fccf9fc4c44 100644 --- a/x-pack/plugin/sql/sql-proto/src/test/java/org/elasticsearch/xpack/sql/proto/SqlVersionTests.java +++ b/x-pack/plugin/sql/sql-proto/src/test/java/org/elasticsearch/xpack/sql/proto/SqlVersionTests.java @@ -98,8 +98,11 @@ public void testVersionCompatibilityClientTooOld() { } public void testVersionCompatibile() { - SqlVersion client = new SqlVersion(randomIntBetween(V_7_7_0.major, 99 - 1), randomIntBetween(V_7_7_0.minor, 99), - randomIntBetween(0, 99)); + SqlVersion client = new SqlVersion( + randomIntBetween(V_7_7_0.major, 99 - 1), + randomIntBetween(V_7_7_0.minor, 99), + randomIntBetween(0, 99) + ); int serverMajor = client.major + (randomBoolean() ? 0 : 1); int serverMinor = randomIntBetween(client.major == serverMajor ? client.minor : 0, 99); int serverRevision = randomIntBetween(client.major == serverMajor && client.minor == serverMinor ? client.revision : 0, 99); diff --git a/x-pack/plugin/sql/src/internalClusterTest/java/org/elasticsearch/xpack/sql/action/AbstractSqlBlockingIntegTestCase.java b/x-pack/plugin/sql/src/internalClusterTest/java/org/elasticsearch/xpack/sql/action/AbstractSqlBlockingIntegTestCase.java index 37689f2e4995e..42ce6a30877b4 100644 --- a/x-pack/plugin/sql/src/internalClusterTest/java/org/elasticsearch/xpack/sql/action/AbstractSqlBlockingIntegTestCase.java +++ b/x-pack/plugin/sql/src/internalClusterTest/java/org/elasticsearch/xpack/sql/action/AbstractSqlBlockingIntegTestCase.java @@ -165,7 +165,6 @@ public void enableSearchBlock() { shouldBlockOnSearch.set(true); } - public void disableFieldCapBlock() { shouldBlockOnFieldCapabilities.set(false); } @@ -211,7 +210,8 @@ public void app String action, Request request, ActionListener listener, - ActionFilterChain chain) { + ActionFilterChain chain + ) { if (action.equals(FieldCapabilitiesAction.NAME)) { final Consumer actionWrapper = resp -> { @@ -227,7 +227,10 @@ public void app } logger.trace("unblocking field caps on " + nodeId); }; - chain.proceed(task, action, request, + chain.proceed( + task, + action, + request, ActionListener.wrap(resp -> executorService.execute(() -> actionWrapper.accept(resp)), listener::onFailure) ); } else { @@ -250,7 +253,7 @@ protected TaskId findTaskWithXOpaqueId(String id, String action) { if (taskInfo != null) { return taskInfo.getTaskId(); } else { - return null; + return null; } } diff --git a/x-pack/plugin/sql/src/internalClusterTest/java/org/elasticsearch/xpack/sql/action/AsyncSqlSearchActionIT.java b/x-pack/plugin/sql/src/internalClusterTest/java/org/elasticsearch/xpack/sql/action/AsyncSqlSearchActionIT.java index 39b6733a00051..420018a03c41c 100644 --- a/x-pack/plugin/sql/src/internalClusterTest/java/org/elasticsearch/xpack/sql/action/AsyncSqlSearchActionIT.java +++ b/x-pack/plugin/sql/src/internalClusterTest/java/org/elasticsearch/xpack/sql/action/AsyncSqlSearchActionIT.java @@ -52,9 +52,9 @@ import java.util.concurrent.Executors; import java.util.function.Function; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertFutureThrows; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; @@ -66,8 +66,9 @@ public class AsyncSqlSearchActionIT extends AbstractSqlBlockingIntegTestCase { private final ExecutorService executorService = Executors.newFixedThreadPool(1); - NamedWriteableRegistry registry = new NamedWriteableRegistry(new SearchModule(Settings.EMPTY, - Collections.emptyList()).getNamedWriteables()); + NamedWriteableRegistry registry = new NamedWriteableRegistry( + new SearchModule(Settings.EMPTY, Collections.emptyList()).getNamedWriteables() + ); /** * Shutdown the executor so we don't leak threads into other test runs. @@ -78,9 +79,13 @@ public void shutdownExec() { } private void prepareIndex() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test") - .setMapping("val", "type=integer", "event_type", "type=keyword", "@timestamp", "type=date", "i", "type=integer") - .get()); + assertAcked( + client().admin() + .indices() + .prepareCreate("test") + .setMapping("val", "type=integer", "event_type", "type=keyword", "@timestamp", "type=date", "i", "type=integer") + .get() + ); createIndex("idx_unmapped"); int numDocs = randomIntBetween(6, 20); @@ -89,13 +94,17 @@ private void prepareIndex() throws Exception { for (int i = 0; i < numDocs; i++) { int fieldValue = randomIntBetween(0, 10); - builders.add(client().prepareIndex("test").setSource( - jsonBuilder().startObject() - .field("val", fieldValue) - .field("event_type", "my_event") - .field("@timestamp", "2020-04-09T12:35:48Z") - .field("i", i) - .endObject())); + builders.add( + client().prepareIndex("test") + .setSource( + jsonBuilder().startObject() + .field("val", fieldValue) + .field("event_type", "my_event") + .field("@timestamp", "2020-04-09T12:35:48Z") + .field("i", i) + .endObject() + ) + ); } indexRandom(true, builders); } @@ -105,8 +114,8 @@ public void testBasicAsyncExecution() throws Exception { boolean success = randomBoolean(); String query = "SELECT event_type FROM test WHERE " + (success ? "i=1" : "10/i=1"); - SqlQueryRequestBuilder builder = new SqlQueryRequestBuilder(client(), SqlQueryAction.INSTANCE) - .query(query).waitForCompletionTimeout(TimeValue.timeValueMillis(1)); + SqlQueryRequestBuilder builder = new SqlQueryRequestBuilder(client(), SqlQueryAction.INSTANCE).query(query) + .waitForCompletionTimeout(TimeValue.timeValueMillis(1)); List plugins = initBlockFactory(true, false); @@ -122,8 +131,7 @@ public void testBasicAsyncExecution() throws Exception { if (randomBoolean()) { // let's timeout first - GetAsyncResultRequest getResultsRequest = new GetAsyncResultRequest(response.id()) - .setKeepAlive(TimeValue.timeValueMinutes(10)) + GetAsyncResultRequest getResultsRequest = new GetAsyncResultRequest(response.id()).setKeepAlive(TimeValue.timeValueMinutes(10)) .setWaitForCompletionTimeout(TimeValue.timeValueMillis(10)); SqlQueryResponse responseWithTimeout = client().execute(SqlAsyncGetResultsAction.INSTANCE, getResultsRequest).get(); assertThat(responseWithTimeout.isRunning(), is(true)); @@ -132,8 +140,7 @@ public void testBasicAsyncExecution() throws Exception { } // Now we wait - GetAsyncResultRequest getResultsRequest = new GetAsyncResultRequest(response.id()) - .setKeepAlive(TimeValue.timeValueMinutes(10)) + GetAsyncResultRequest getResultsRequest = new GetAsyncResultRequest(response.id()).setKeepAlive(TimeValue.timeValueMinutes(10)) .setWaitForCompletionTimeout(TimeValue.timeValueSeconds(10)); ActionFuture future = client().execute(SqlAsyncGetResultsAction.INSTANCE, getResultsRequest); disableBlocks(plugins); @@ -145,8 +152,10 @@ public void testBasicAsyncExecution() throws Exception { Exception ex = expectThrows(Exception.class, future::actionGet); assertThat(ex.getCause().getMessage(), containsString("by zero")); } - AcknowledgedResponse deleteResponse = - client().execute(DeleteAsyncResultAction.INSTANCE, new DeleteAsyncResultRequest(response.id())).actionGet(); + AcknowledgedResponse deleteResponse = client().execute( + DeleteAsyncResultAction.INSTANCE, + new DeleteAsyncResultRequest(response.id()) + ).actionGet(); assertThat(deleteResponse.isAcknowledged(), equalTo(true)); } @@ -155,8 +164,8 @@ public void testGoingAsync() throws Exception { boolean success = randomBoolean(); String query = "SELECT event_type FROM test WHERE " + (success ? "i=1" : "10/i=1"); - SqlQueryRequestBuilder builder = new SqlQueryRequestBuilder(client(), SqlQueryAction.INSTANCE) - .query(query).waitForCompletionTimeout(TimeValue.timeValueMillis(1)); + SqlQueryRequestBuilder builder = new SqlQueryRequestBuilder(client(), SqlQueryAction.INSTANCE).query(query) + .waitForCompletionTimeout(TimeValue.timeValueMillis(1)); boolean customKeepAlive = randomBoolean(); TimeValue keepAliveValue; @@ -172,7 +181,8 @@ public void testGoingAsync() throws Exception { String opaqueId = randomAlphaOfLength(10); logger.trace("Starting async search"); SqlQueryResponse response = client().filterWithHeader(Collections.singletonMap(Task.X_OPAQUE_ID, opaqueId)) - .execute(SqlQueryAction.INSTANCE, builder.request()).get(); + .execute(SqlQueryAction.INSTANCE, builder.request()) + .get(); assertThat(response.isRunning(), is(true)); assertThat(response.isPartial(), is(true)); assertThat(response.id(), notNullValue()); @@ -190,8 +200,10 @@ public void testGoingAsync() throws Exception { assertBusy(() -> assertThat(findTaskWithXOpaqueId(opaqueId, SqlQueryAction.NAME + "[a]"), nullValue())); StoredAsyncResponse doc = getStoredRecord(id); // Make sure that the expiration time is not more than 1 min different from the current time + keep alive - assertThat(System.currentTimeMillis() + keepAliveValue.getMillis() - doc.getExpirationTime(), - lessThan(doc.getExpirationTime() + TimeValue.timeValueMinutes(1).getMillis())); + assertThat( + System.currentTimeMillis() + keepAliveValue.getMillis() - doc.getExpirationTime(), + lessThan(doc.getExpirationTime() + TimeValue.timeValueMinutes(1).getMillis()) + ); if (success) { assertThat(doc.getException(), nullValue()); assertThat(doc.getResponse(), notNullValue()); @@ -208,8 +220,8 @@ public void testAsyncCancellation() throws Exception { boolean success = randomBoolean(); String query = "SELECT event_type FROM test WHERE " + (success ? "i=1" : "10/i=1"); - SqlQueryRequestBuilder builder = new SqlQueryRequestBuilder(client(), SqlQueryAction.INSTANCE) - .query(query).waitForCompletionTimeout(TimeValue.timeValueMillis(1)); + SqlQueryRequestBuilder builder = new SqlQueryRequestBuilder(client(), SqlQueryAction.INSTANCE).query(query) + .waitForCompletionTimeout(TimeValue.timeValueMillis(1)); boolean customKeepAlive = randomBoolean(); final TimeValue keepAliveValue; @@ -223,7 +235,8 @@ public void testAsyncCancellation() throws Exception { String opaqueId = randomAlphaOfLength(10); logger.trace("Starting async search"); SqlQueryResponse response = client().filterWithHeader(Collections.singletonMap(Task.X_OPAQUE_ID, opaqueId)) - .execute(SqlQueryAction.INSTANCE, builder.request()).get(); + .execute(SqlQueryAction.INSTANCE, builder.request()) + .get(); assertThat(response.isRunning(), is(true)); assertThat(response.isPartial(), is(true)); assertThat(response.id(), notNullValue()); @@ -232,8 +245,10 @@ public void testAsyncCancellation() throws Exception { awaitForBlockedSearches(plugins, "test"); logger.trace("Block is established"); - ActionFuture deleteResponse = - client().execute(DeleteAsyncResultAction.INSTANCE, new DeleteAsyncResultRequest(response.id())); + ActionFuture deleteResponse = client().execute( + DeleteAsyncResultAction.INSTANCE, + new DeleteAsyncResultRequest(response.id()) + ); disableBlocks(plugins); assertThat(deleteResponse.actionGet().isAcknowledged(), equalTo(true)); @@ -247,8 +262,8 @@ public void testFinishingBeforeTimeout() throws Exception { boolean success = randomBoolean(); boolean keepOnCompletion = randomBoolean(); String query = "SELECT event_type FROM test WHERE " + (success ? "i=1" : "10/i=1"); - SqlQueryRequestBuilder builder = new SqlQueryRequestBuilder(client(), SqlQueryAction.INSTANCE) - .query(query).waitForCompletionTimeout(TimeValue.timeValueSeconds(10)); + SqlQueryRequestBuilder builder = new SqlQueryRequestBuilder(client(), SqlQueryAction.INSTANCE).query(query) + .waitForCompletionTimeout(TimeValue.timeValueSeconds(10)); if (keepOnCompletion || randomBoolean()) { builder.keepOnCompletion(keepOnCompletion); } @@ -266,22 +281,24 @@ public void testFinishingBeforeTimeout() throws Exception { assertThat(doc.getException(), nullValue()); assertThat(doc.getResponse(), notNullValue()); assertThat(doc.getResponse().rows().size(), equalTo(1)); - SqlQueryResponse storedResponse = client().execute(SqlAsyncGetResultsAction.INSTANCE, - new GetAsyncResultRequest(response.id())).actionGet(); + SqlQueryResponse storedResponse = client().execute( + SqlAsyncGetResultsAction.INSTANCE, + new GetAsyncResultRequest(response.id()) + ).actionGet(); assertThat(storedResponse, equalTo(response)); - AcknowledgedResponse deleteResponse = - client().execute(DeleteAsyncResultAction.INSTANCE, new DeleteAsyncResultRequest(response.id())).actionGet(); + AcknowledgedResponse deleteResponse = client().execute( + DeleteAsyncResultAction.INSTANCE, + new DeleteAsyncResultRequest(response.id()) + ).actionGet(); assertThat(deleteResponse.isAcknowledged(), equalTo(true)); } } else { - Exception ex = expectThrows(Exception.class, - () -> client().execute(SqlQueryAction.INSTANCE, request).get()); + Exception ex = expectThrows(Exception.class, () -> client().execute(SqlQueryAction.INSTANCE, request).get()); assertThat(ex.getMessage(), containsString("by zero")); } } - public StoredAsyncResponse getStoredRecord(String id) throws Exception { try { GetResponse doc = client().prepareGet(XPackPlugin.ASYNC_RESULTS_INDEX, AsyncExecutionId.decode(id).getDocId()).get(); @@ -307,8 +324,11 @@ public static class FakePainlessScriptPlugin extends MockScriptPlugin { @Override protected Map, Object>> pluginScripts() { Map, Object>> scripts = new HashMap<>(); - scripts.put("InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.eq(InternalSqlScriptUtils.div(" + - "params.v0,InternalQlScriptUtils.docValue(doc,params.v1)),params.v2))", FakePainlessScriptPlugin::fail); + scripts.put( + "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.eq(InternalSqlScriptUtils.div(" + + "params.v0,InternalQlScriptUtils.docValue(doc,params.v1)),params.v2))", + FakePainlessScriptPlugin::fail + ); return scripts; } diff --git a/x-pack/plugin/sql/src/internalClusterTest/java/org/elasticsearch/xpack/sql/action/RestSqlCancellationIT.java b/x-pack/plugin/sql/src/internalClusterTest/java/org/elasticsearch/xpack/sql/action/RestSqlCancellationIT.java index 69cbff5964645..f7149e24ba779 100644 --- a/x-pack/plugin/sql/src/internalClusterTest/java/org/elasticsearch/xpack/sql/action/RestSqlCancellationIT.java +++ b/x-pack/plugin/sql/src/internalClusterTest/java/org/elasticsearch/xpack/sql/action/RestSqlCancellationIT.java @@ -20,8 +20,8 @@ import org.elasticsearch.tasks.Task; import org.elasticsearch.tasks.TaskInfo; import org.elasticsearch.test.junit.annotations.TestLogging; -import org.elasticsearch.transport.netty4.Netty4Plugin; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.transport.netty4.Netty4Plugin; import org.elasticsearch.transport.nio.NioTransportPlugin; import org.elasticsearch.xpack.sql.proto.Protocol; import org.junit.BeforeClass; @@ -33,8 +33,8 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicReference; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.notNullValue; @@ -58,7 +58,8 @@ protected boolean addMockHttpTransport() { protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { return Settings.builder() .put(super.nodeSettings(nodeOrdinal, otherSettings)) - .put(NetworkModule.HTTP_TYPE_KEY, nodeHttpTypeKey).build(); + .put(NetworkModule.HTTP_TYPE_KEY, nodeHttpTypeKey) + .build(); } private static String getHttpTypeKey(Class clazz) { @@ -81,9 +82,13 @@ protected Collection> nodePlugins() { @TestLogging(value = "org.elasticsearch.xpack.sql:TRACE", reason = "debug") public void testRestCancellation() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test") - .setMapping("val", "type=integer", "event_type", "type=keyword", "@timestamp", "type=date") - .get()); + assertAcked( + client().admin() + .indices() + .prepareCreate("test") + .setMapping("val", "type=integer", "event_type", "type=keyword", "@timestamp", "type=date") + .get() + ); createIndex("idx_unmapped"); int numDocs = randomIntBetween(6, 20); @@ -92,18 +97,25 @@ public void testRestCancellation() throws Exception { for (int i = 0; i < numDocs; i++) { int fieldValue = randomIntBetween(0, 10); - builders.add(client().prepareIndex("test").setSource( - jsonBuilder().startObject() - .field("val", fieldValue).field("event_type", "my_event").field("@timestamp", "2020-04-09T12:35:48Z") - .endObject())); + builders.add( + client().prepareIndex("test") + .setSource( + jsonBuilder().startObject() + .field("val", fieldValue) + .field("event_type", "my_event") + .field("@timestamp", "2020-04-09T12:35:48Z") + .endObject() + ) + ); } indexRandom(true, builders); // We are cancelling during both mapping and searching but we cancel during mapping so we should never reach the second block List plugins = initBlockFactory(true, true); - SqlQueryRequest sqlRequest = new SqlQueryRequestBuilder(client(), SqlQueryAction.INSTANCE) - .query("SELECT event_type FROM test WHERE val=1").request(); + SqlQueryRequest sqlRequest = new SqlQueryRequestBuilder(client(), SqlQueryAction.INSTANCE).query( + "SELECT event_type FROM test WHERE val=1" + ).request(); String id = randomAlphaOfLength(10); Request request = new Request("POST", Protocol.SQL_QUERY_REST_ENDPOINT); @@ -153,9 +165,7 @@ public void onFailure(Exception exception) { logger.trace("Disabling field cap blocks"); disableFieldCapBlocks(plugins); // The task should be cancelled before ever reaching search blocks - assertBusy(() -> { - assertThat(getTaskInfoWithXOpaqueId(id, SqlQueryAction.NAME), nullValue()); - }); + assertBusy(() -> { assertThat(getTaskInfoWithXOpaqueId(id, SqlQueryAction.NAME), nullValue()); }); // Make sure it didn't reach search blocks assertThat(getNumberOfContexts(plugins), equalTo(0)); disableSearchBlocks(plugins); diff --git a/x-pack/plugin/sql/src/internalClusterTest/java/org/elasticsearch/xpack/sql/action/SqlActionIT.java b/x-pack/plugin/sql/src/internalClusterTest/java/org/elasticsearch/xpack/sql/action/SqlActionIT.java index 5b34b412c48cc..b3377f2ba2983 100644 --- a/x-pack/plugin/sql/src/internalClusterTest/java/org/elasticsearch/xpack/sql/action/SqlActionIT.java +++ b/x-pack/plugin/sql/src/internalClusterTest/java/org/elasticsearch/xpack/sql/action/SqlActionIT.java @@ -22,16 +22,17 @@ public class SqlActionIT extends AbstractSqlIntegTestCase { public void testSqlAction() { assertAcked(client().admin().indices().prepareCreate("test").get()); client().prepareBulk() - .add(new IndexRequest("test").id("1").source("data", "bar", "count", 42)) - .add(new IndexRequest("test").id("2").source("data", "baz", "count", 43)) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .get(); + .add(new IndexRequest("test").id("1").source("data", "bar", "count", 42)) + .add(new IndexRequest("test").id("2").source("data", "baz", "count", 43)) + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + .get(); ensureYellow("test"); boolean dataBeforeCount = randomBoolean(); String columns = dataBeforeCount ? "data, count" : "count, data"; - SqlQueryResponse response = new SqlQueryRequestBuilder(client(), SqlQueryAction.INSTANCE) - .query("SELECT " + columns + " FROM test ORDER BY count").mode(Mode.JDBC).version(Version.CURRENT.toString()).get(); + SqlQueryResponse response = new SqlQueryRequestBuilder(client(), SqlQueryAction.INSTANCE).query( + "SELECT " + columns + " FROM test ORDER BY count" + ).mode(Mode.JDBC).version(Version.CURRENT.toString()).get(); assertThat(response.size(), equalTo(2L)); assertThat(response.columns(), hasSize(2)); int dataIndex = dataBeforeCount ? 0 : 1; @@ -47,16 +48,18 @@ public void testSqlAction() { } public void testSqlActionCurrentVersion() { - SqlQueryResponse response = new SqlQueryRequestBuilder(client(), SqlQueryAction.INSTANCE) - .query("SELECT true").mode(randomFrom(Mode.CLI, Mode.JDBC)).version(Version.CURRENT.toString()).get(); + SqlQueryResponse response = new SqlQueryRequestBuilder(client(), SqlQueryAction.INSTANCE).query("SELECT true") + .mode(randomFrom(Mode.CLI, Mode.JDBC)) + .version(Version.CURRENT.toString()) + .get(); assertThat(response.size(), equalTo(1L)); assertEquals(true, response.rows().get(0).get(0)); } public void testSqlActionOutdatedVersion() { - SqlQueryRequestBuilder request = new SqlQueryRequestBuilder(client(), SqlQueryAction.INSTANCE) - .query("SELECT true").mode(randomFrom(Mode.CLI, Mode.JDBC)).version("1.2.3"); + SqlQueryRequestBuilder request = new SqlQueryRequestBuilder(client(), SqlQueryAction.INSTANCE).query("SELECT true") + .mode(randomFrom(Mode.CLI, Mode.JDBC)) + .version("1.2.3"); assertRequestBuilderThrows(request, org.elasticsearch.action.ActionRequestValidationException.class); } } - diff --git a/x-pack/plugin/sql/src/internalClusterTest/java/org/elasticsearch/xpack/sql/action/SqlCancellationIT.java b/x-pack/plugin/sql/src/internalClusterTest/java/org/elasticsearch/xpack/sql/action/SqlCancellationIT.java index 784a31dccd6a3..38d8c0c364f53 100644 --- a/x-pack/plugin/sql/src/internalClusterTest/java/org/elasticsearch/xpack/sql/action/SqlCancellationIT.java +++ b/x-pack/plugin/sql/src/internalClusterTest/java/org/elasticsearch/xpack/sql/action/SqlCancellationIT.java @@ -21,8 +21,8 @@ import java.util.concurrent.Executors; import java.util.concurrent.Future; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.instanceOf; @@ -39,9 +39,13 @@ public void shutdownExec() { } public void testCancellation() throws Exception { - assertAcked(client().admin().indices().prepareCreate("test") - .setMapping("val", "type=integer", "event_type", "type=keyword", "@timestamp", "type=date") - .get()); + assertAcked( + client().admin() + .indices() + .prepareCreate("test") + .setMapping("val", "type=integer", "event_type", "type=keyword", "@timestamp", "type=date") + .get() + ); createIndex("idx_unmapped"); int numDocs = randomIntBetween(6, 20); @@ -50,22 +54,29 @@ public void testCancellation() throws Exception { for (int i = 0; i < numDocs; i++) { int fieldValue = randomIntBetween(0, 10); - builders.add(client().prepareIndex("test").setSource( - jsonBuilder().startObject() - .field("val", fieldValue).field("event_type", "my_event").field("@timestamp", "2020-04-09T12:35:48Z") - .endObject())); + builders.add( + client().prepareIndex("test") + .setSource( + jsonBuilder().startObject() + .field("val", fieldValue) + .field("event_type", "my_event") + .field("@timestamp", "2020-04-09T12:35:48Z") + .endObject() + ) + ); } indexRandom(true, builders); boolean cancelDuringSearch = randomBoolean(); List plugins = initBlockFactory(cancelDuringSearch, cancelDuringSearch == false); - SqlQueryRequest request = new SqlQueryRequestBuilder(client(), SqlQueryAction.INSTANCE) - .query("SELECT event_type FROM test WHERE val=1").request(); + SqlQueryRequest request = new SqlQueryRequestBuilder(client(), SqlQueryAction.INSTANCE).query( + "SELECT event_type FROM test WHERE val=1" + ).request(); String id = randomAlphaOfLength(10); logger.trace("Preparing search"); // We might perform field caps on the same thread if it is local client, so we cannot use the standard mechanism - Future future = executorService.submit(() -> - client().filterWithHeader(Collections.singletonMap(Task.X_OPAQUE_ID, id)).execute(SqlQueryAction.INSTANCE, request).get() + Future future = executorService.submit( + () -> client().filterWithHeader(Collections.singletonMap(Task.X_OPAQUE_ID, id)).execute(SqlQueryAction.INSTANCE, request).get() ); logger.trace("Waiting for block to be established"); if (cancelDuringSearch) { diff --git a/x-pack/plugin/sql/src/internalClusterTest/java/org/elasticsearch/xpack/sql/action/SqlClearCursorActionIT.java b/x-pack/plugin/sql/src/internalClusterTest/java/org/elasticsearch/xpack/sql/action/SqlClearCursorActionIT.java index 0dd4d72ba3c20..c50d86856023f 100644 --- a/x-pack/plugin/sql/src/internalClusterTest/java/org/elasticsearch/xpack/sql/action/SqlClearCursorActionIT.java +++ b/x-pack/plugin/sql/src/internalClusterTest/java/org/elasticsearch/xpack/sql/action/SqlClearCursorActionIT.java @@ -34,16 +34,18 @@ public void testSqlClearCursorAction() { int fetchSize = randomIntBetween(5, 20); logger.info("Fetching {} records at a time", fetchSize); - SqlQueryResponse sqlQueryResponse = new SqlQueryRequestBuilder(client(), SqlQueryAction.INSTANCE) - .query("SELECT * FROM test").fetchSize(fetchSize).get(); + SqlQueryResponse sqlQueryResponse = new SqlQueryRequestBuilder(client(), SqlQueryAction.INSTANCE).query("SELECT * FROM test") + .fetchSize(fetchSize) + .get(); assertEquals(fetchSize, sqlQueryResponse.size()); assertThat(getNumberOfSearchContexts(), greaterThan(0L)); assertThat(sqlQueryResponse.cursor(), notNullValue()); assertThat(sqlQueryResponse.cursor(), not(equalTo(Cursor.EMPTY))); - SqlClearCursorResponse cleanCursorResponse = new SqlClearCursorRequestBuilder(client(), SqlClearCursorAction.INSTANCE) - .cursor(sqlQueryResponse.cursor()).get(); + SqlClearCursorResponse cleanCursorResponse = new SqlClearCursorRequestBuilder(client(), SqlClearCursorAction.INSTANCE).cursor( + sqlQueryResponse.cursor() + ).get(); assertTrue(cleanCursorResponse.isSucceeded()); assertEquals(0, getNumberOfSearchContexts()); @@ -64,8 +66,9 @@ public void testAutoCursorCleanup() { int fetchSize = randomIntBetween(5, 20); logger.info("Fetching {} records at a time", fetchSize); - SqlQueryResponse sqlQueryResponse = new SqlQueryRequestBuilder(client(), SqlQueryAction.INSTANCE) - .query("SELECT * FROM test").fetchSize(fetchSize).get(); + SqlQueryResponse sqlQueryResponse = new SqlQueryRequestBuilder(client(), SqlQueryAction.INSTANCE).query("SELECT * FROM test") + .fetchSize(fetchSize) + .get(); assertEquals(fetchSize, sqlQueryResponse.size()); assertThat(getNumberOfSearchContexts(), greaterThan(0L)); @@ -79,15 +82,24 @@ public void testAutoCursorCleanup() { } while (sqlQueryResponse.cursor().isEmpty() == false); assertEquals(indexSize, fetched); - SqlClearCursorResponse cleanCursorResponse = new SqlClearCursorRequestBuilder(client(), SqlClearCursorAction.INSTANCE) - .cursor(sqlQueryResponse.cursor()).get(); + SqlClearCursorResponse cleanCursorResponse = new SqlClearCursorRequestBuilder(client(), SqlClearCursorAction.INSTANCE).cursor( + sqlQueryResponse.cursor() + ).get(); assertFalse(cleanCursorResponse.isSucceeded()); assertEquals(0, getNumberOfSearchContexts()); } private long getNumberOfSearchContexts() { - return client().admin().indices().prepareStats("test").clear().setSearch(true).get() - .getIndex("test").getTotal().getSearch().getOpenContexts(); + return client().admin() + .indices() + .prepareStats("test") + .clear() + .setSearch(true) + .get() + .getIndex("test") + .getTotal() + .getSearch() + .getOpenContexts(); } } diff --git a/x-pack/plugin/sql/src/internalClusterTest/java/org/elasticsearch/xpack/sql/action/SqlLicenseIT.java b/x-pack/plugin/sql/src/internalClusterTest/java/org/elasticsearch/xpack/sql/action/SqlLicenseIT.java index b6d46a7e76a99..7ea60f0efd066 100644 --- a/x-pack/plugin/sql/src/internalClusterTest/java/org/elasticsearch/xpack/sql/action/SqlLicenseIT.java +++ b/x-pack/plugin/sql/src/internalClusterTest/java/org/elasticsearch/xpack/sql/action/SqlLicenseIT.java @@ -12,8 +12,8 @@ import org.elasticsearch.action.support.WriteRequest; import org.elasticsearch.common.network.NetworkModule; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.CollectionUtils; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.license.AbstractLicensesIntegrationTestCase; import org.elasticsearch.license.License; import org.elasticsearch.license.License.OperationMode; @@ -62,10 +62,7 @@ protected boolean addMockHttpTransport() { protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { // Enable http so we can test JDBC licensing because only exists on the REST layer. String httpPlugin = randomBoolean() ? Netty4Plugin.NETTY_HTTP_TRANSPORT_NAME : NioTransportPlugin.NIO_TRANSPORT_NAME; - return Settings.builder() - .put(super.nodeSettings(nodeOrdinal, otherSettings)) - .put(NetworkModule.HTTP_TYPE_KEY, httpPlugin) - .build(); + return Settings.builder().put(super.nodeSettings(nodeOrdinal, otherSettings)).put(NetworkModule.HTTP_TYPE_KEY, httpPlugin).build(); } private static OperationMode randomValidSqlLicenseType() { @@ -115,8 +112,10 @@ public void testSqlQueryActionLicense() throws Exception { setupTestIndex(); disableSqlLicensing(); - ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, - () -> new SqlQueryRequestBuilder(client(), SqlQueryAction.INSTANCE).query("SELECT * FROM test").get()); + ElasticsearchSecurityException e = expectThrows( + ElasticsearchSecurityException.class, + () -> new SqlQueryRequestBuilder(client(), SqlQueryAction.INSTANCE).query("SELECT * FROM test").get() + ); assertThat(e.getMessage(), equalTo("current license is non-compliant for [sql]")); enableSqlLicensing(); @@ -124,18 +123,20 @@ public void testSqlQueryActionLicense() throws Exception { assertThat(response.size(), Matchers.equalTo(2L)); } - public void testSqlQueryActionJdbcModeLicense() throws Exception { setupTestIndex(); disableJdbcLicensing(); - ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, - () -> new SqlQueryRequestBuilder(client(), SqlQueryAction.INSTANCE).query("SELECT * FROM test").mode("jdbc").get()); + ElasticsearchSecurityException e = expectThrows( + ElasticsearchSecurityException.class, + () -> new SqlQueryRequestBuilder(client(), SqlQueryAction.INSTANCE).query("SELECT * FROM test").mode("jdbc").get() + ); assertThat(e.getMessage(), equalTo("current license is non-compliant for [jdbc]")); enableJdbcLicensing(); - SqlQueryResponse response = new SqlQueryRequestBuilder(client(), SqlQueryAction.INSTANCE) - .query("SELECT * FROM test").mode("jdbc").get(); + SqlQueryResponse response = new SqlQueryRequestBuilder(client(), SqlQueryAction.INSTANCE).query("SELECT * FROM test") + .mode("jdbc") + .get(); assertThat(response.size(), Matchers.equalTo(2L)); } @@ -143,16 +144,17 @@ public void testSqlTranslateActionLicense() throws Exception { setupTestIndex(); disableSqlLicensing(); - ElasticsearchSecurityException e = expectThrows(ElasticsearchSecurityException.class, - () -> new SqlTranslateRequestBuilder(client(), SqlTranslateAction.INSTANCE).query("SELECT * FROM test").get()); + ElasticsearchSecurityException e = expectThrows( + ElasticsearchSecurityException.class, + () -> new SqlTranslateRequestBuilder(client(), SqlTranslateAction.INSTANCE).query("SELECT * FROM test").get() + ); assertThat(e.getMessage(), equalTo("current license is non-compliant for [sql]")); enableSqlLicensing(); - SqlTranslateResponse response = new SqlTranslateRequestBuilder(client(), SqlTranslateAction.INSTANCE) - .query("SELECT * FROM test").get(); + SqlTranslateResponse response = new SqlTranslateRequestBuilder(client(), SqlTranslateAction.INSTANCE).query("SELECT * FROM test") + .get(); SearchSourceBuilder source = response.source(); - assertThat(source.docValueFields(), Matchers.contains( - new FieldAndFormat("count", null))); + assertThat(source.docValueFields(), Matchers.contains(new FieldAndFormat("count", null))); FetchSourceContext fetchSource = source.fetchSource(); assertThat(fetchSource.includes(), Matchers.arrayContaining("data")); } @@ -162,10 +164,10 @@ public void testSqlTranslateActionLicense() throws Exception { private void setupTestIndex() { ElasticsearchAssertions.assertAcked(client().admin().indices().prepareCreate("test").get()); client().prepareBulk() - .add(new IndexRequest("test").id("1").source("data", "bar", "count", 42)) - .add(new IndexRequest("test").id("2").source("data", "baz", "count", 43)) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .get(); + .add(new IndexRequest("test").id("1").source("data", "bar", "count", 42)) + .add(new IndexRequest("test").id("2").source("data", "baz", "count", 43)) + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + .get(); } } diff --git a/x-pack/plugin/sql/src/internalClusterTest/java/org/elasticsearch/xpack/sql/action/SqlTranslateActionIT.java b/x-pack/plugin/sql/src/internalClusterTest/java/org/elasticsearch/xpack/sql/action/SqlTranslateActionIT.java index 05f42cebc82e4..90cf11e3f0a61 100644 --- a/x-pack/plugin/sql/src/internalClusterTest/java/org/elasticsearch/xpack/sql/action/SqlTranslateActionIT.java +++ b/x-pack/plugin/sql/src/internalClusterTest/java/org/elasticsearch/xpack/sql/action/SqlTranslateActionIT.java @@ -6,15 +6,15 @@ */ package org.elasticsearch.xpack.sql.action; -import java.util.ArrayList; -import java.util.List; - import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.support.WriteRequest; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.fetch.subphase.FieldAndFormat; import org.elasticsearch.search.sort.SortBuilders; +import java.util.ArrayList; +import java.util.List; + import static java.util.Collections.singletonList; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; @@ -23,16 +23,17 @@ public class SqlTranslateActionIT extends AbstractSqlIntegTestCase { public void testSqlTranslateAction() { assertAcked(client().admin().indices().prepareCreate("test").get()); client().prepareBulk() - .add(new IndexRequest("test").id("1").source("data", "bar", "count", 42, "date", "1984-01-04")) - .add(new IndexRequest("test").id("2").source("data", "baz", "count", 43, "date", "1989-12-19")) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .get(); + .add(new IndexRequest("test").id("1").source("data", "bar", "count", 42, "date", "1984-01-04")) + .add(new IndexRequest("test").id("2").source("data", "baz", "count", 43, "date", "1989-12-19")) + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + .get(); ensureYellow("test"); boolean columnOrder = randomBoolean(); String columns = columnOrder ? "data, count, date" : "date, data, count"; - SqlTranslateResponse response = new SqlTranslateRequestBuilder(client(), SqlTranslateAction.INSTANCE) - .query("SELECT " + columns + " FROM test ORDER BY count").get(); + SqlTranslateResponse response = new SqlTranslateRequestBuilder(client(), SqlTranslateAction.INSTANCE).query( + "SELECT " + columns + " FROM test ORDER BY count" + ).get(); SearchSourceBuilder source = response.source(); List actualFields = source.fetchFields(); List expectedFields = new ArrayList<>(3); diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/SqlInfoTransportAction.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/SqlInfoTransportAction.java index da328fc1da20b..d7908b93b3323 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/SqlInfoTransportAction.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/SqlInfoTransportAction.java @@ -19,8 +19,7 @@ public class SqlInfoTransportAction extends XPackInfoFeatureTransportAction { private final XPackLicenseState licenseState; @Inject - public SqlInfoTransportAction(TransportService transportService, ActionFilters actionFilters, - XPackLicenseState licenseState) { + public SqlInfoTransportAction(TransportService transportService, ActionFilters actionFilters, XPackLicenseState licenseState) { super(XPackInfoFeatureAction.SQL.name(), transportService, actionFilters); this.licenseState = licenseState; } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/SqlUsageTransportAction.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/SqlUsageTransportAction.java index bbbf775516937..5a402ae2fa151 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/SqlUsageTransportAction.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/SqlUsageTransportAction.java @@ -36,18 +36,27 @@ public class SqlUsageTransportAction extends XPackUsageFeatureTransportAction { private final Client client; @Inject - public SqlUsageTransportAction(TransportService transportService, ClusterService clusterService, ThreadPool threadPool, - ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver, - XPackLicenseState licenseState, Client client) { - super(XPackUsageFeatureAction.SQL.name(), transportService, clusterService, threadPool, actionFilters, - indexNameExpressionResolver); + public SqlUsageTransportAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver, + XPackLicenseState licenseState, + Client client + ) { + super(XPackUsageFeatureAction.SQL.name(), transportService, clusterService, threadPool, actionFilters, indexNameExpressionResolver); this.licenseState = licenseState; this.client = client; } @Override - protected void masterOperation(Task task, XPackUsageRequest request, ClusterState state, - ActionListener listener) { + protected void masterOperation( + Task task, + XPackUsageRequest request, + ClusterState state, + ActionListener listener + ) { SqlStatsRequest sqlRequest = new SqlStatsRequest(); sqlRequest.includeStats(true); sqlRequest.setParentTask(clusterService.localNode().getId(), task.getId()); diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/analysis/analyzer/Analyzer.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/analysis/analyzer/Analyzer.java index f183b42fc65d7..36d6c56402e32 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/analysis/analyzer/Analyzer.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/analysis/analyzer/Analyzer.java @@ -6,8 +6,8 @@ */ package org.elasticsearch.xpack.sql.analysis.analyzer; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.logging.LoggerMessageFormat; +import org.elasticsearch.core.Tuple; import org.elasticsearch.xpack.ql.analyzer.AnalyzerRules.AddMissingEqualsToBoolField; import org.elasticsearch.xpack.ql.capabilities.Resolvables; import org.elasticsearch.xpack.ql.common.Failure; @@ -107,28 +107,29 @@ public Analyzer(Configuration configuration, FunctionRegistry functionRegistry, @Override protected Iterable.Batch> batches() { - Batch substitution = new Batch("Substitution", - new CTESubstitution()); - Batch resolution = new Batch("Resolution", - new ResolveTable(), - new ResolveRefs(), - new ResolveOrdinalInOrderByAndGroupBy(), - new ResolveMissingRefs(), - new ResolveFilterRefs(), - new ResolveFunctions(), - new ResolveAliases(), - new ProjectedAggregations(), - new HavingOverProject(), - new ResolveAggsInHaving(), - new ResolveAggsInOrderBy() - //new ImplicitCasting() - ); - Batch finish = new Batch("Finish Analysis", - new ReplaceSubQueryAliases(), // Should be run before pruning SubqueryAliases - new PruneSubQueryAliases(), - new AddMissingEqualsToBoolField(), - CleanAliases.INSTANCE - ); + Batch substitution = new Batch("Substitution", new CTESubstitution()); + Batch resolution = new Batch( + "Resolution", + new ResolveTable(), + new ResolveRefs(), + new ResolveOrdinalInOrderByAndGroupBy(), + new ResolveMissingRefs(), + new ResolveFilterRefs(), + new ResolveFunctions(), + new ResolveAliases(), + new ProjectedAggregations(), + new HavingOverProject(), + new ResolveAggsInHaving(), + new ResolveAggsInOrderBy() + // new ImplicitCasting() + ); + Batch finish = new Batch( + "Finish Analysis", + new ReplaceSubQueryAliases(), // Should be run before pruning SubqueryAliases + new PruneSubQueryAliases(), + new AddMissingEqualsToBoolField(), + CleanAliases.INSTANCE + ); return Arrays.asList(substitution, resolution, finish); } @@ -182,13 +183,12 @@ private static Attribute resolveAgainstList(UnresolvedAttribute u, Collection refs = matches.stream() - .sorted((a, b) -> { - int lineDiff = a.sourceLocation().getLineNumber() - b.sourceLocation().getLineNumber(); - int colDiff = a.sourceLocation().getColumnNumber() - b.sourceLocation().getColumnNumber(); - return lineDiff != 0 ? lineDiff : (colDiff != 0 ? colDiff : a.qualifiedName().compareTo(b.qualifiedName())); - }) - .map(a -> "line " + a.sourceLocation().toString().substring(1) + " [" + - (a.qualifier() != null ? "\"" + a.qualifier() + "\".\"" + a.name() + "\"" : a.name()) + "]") + List refs = matches.stream().sorted((a, b) -> { + int lineDiff = a.sourceLocation().getLineNumber() - b.sourceLocation().getLineNumber(); + int colDiff = a.sourceLocation().getColumnNumber() - b.sourceLocation().getColumnNumber(); + return lineDiff != 0 ? lineDiff : (colDiff != 0 ? colDiff : a.qualifiedName().compareTo(b.qualifiedName())); + }) + .map( + a -> "line " + + a.sourceLocation().toString().substring(1) + + " [" + + (a.qualifier() != null ? "\"" + a.qualifier() + "\".\"" + a.name() + "\"" : a.name()) + + "]" + ) .collect(toList()); - return u.withUnresolvedMessage("Reference [" + u.qualifiedName() + "] is ambiguous (to disambiguate use quotes or qualifiers); " + - "matches any of " + refs); + return u.withUnresolvedMessage( + "Reference [" + u.qualifiedName() + "] is ambiguous (to disambiguate use quotes or qualifiers); " + "matches any of " + refs + ); } private static Attribute handleSpecialFields(UnresolvedAttribute u, Attribute named, boolean allowCompound) { @@ -225,25 +230,35 @@ private static Attribute handleSpecialFields(UnresolvedAttribute u, Attribute na // incompatible mappings if (fa.field() instanceof InvalidMappedField) { - named = u.withUnresolvedMessage("Cannot use field [" + fa.name() + "] due to ambiguities being " - + ((InvalidMappedField) fa.field()).errorMessage()); + named = u.withUnresolvedMessage( + "Cannot use field [" + fa.name() + "] due to ambiguities being " + ((InvalidMappedField) fa.field()).errorMessage() + ); } // unsupported types else if (DataTypes.isUnsupported(fa.dataType())) { UnsupportedEsField unsupportedField = (UnsupportedEsField) fa.field(); if (unsupportedField.hasInherited()) { named = u.withUnresolvedMessage( - "Cannot use field [" + fa.name() + "] with unsupported type [" + unsupportedField.getOriginalType() + "] " - + "in hierarchy (field [" + unsupportedField.getInherited() + "])"); + "Cannot use field [" + + fa.name() + + "] with unsupported type [" + + unsupportedField.getOriginalType() + + "] " + + "in hierarchy (field [" + + unsupportedField.getInherited() + + "])" + ); } else { named = u.withUnresolvedMessage( - "Cannot use field [" + fa.name() + "] with unsupported type [" + unsupportedField.getOriginalType() + "]"); + "Cannot use field [" + fa.name() + "] with unsupported type [" + unsupportedField.getOriginalType() + "]" + ); } } // compound fields else if (allowCompound == false && DataTypes.isPrimitive(fa.dataType()) == false) { named = u.withUnresolvedMessage( - "Cannot use field [" + fa.name() + "] type [" + fa.dataType().typeName() + "] only its subfields"); + "Cannot use field [" + fa.name() + "] type [" + fa.dataType().typeName() + "] only its subfields" + ); } } return named; @@ -303,13 +318,19 @@ private class ResolveTable extends AnalyzerRule { @Override protected LogicalPlan rule(UnresolvedRelation plan) { if (indexResolution.isValid() == false) { - return plan.unresolvedMessage().equals(indexResolution.toString()) ? plan : - new UnresolvedRelation(plan.source(), plan.table(), plan.alias(), plan.frozen(), indexResolution.toString()); + return plan.unresolvedMessage().equals(indexResolution.toString()) + ? plan + : new UnresolvedRelation(plan.source(), plan.table(), plan.alias(), plan.frozen(), indexResolution.toString()); } TableIdentifier table = plan.table(); if (indexResolution.matches(table.index()) == false) { - new UnresolvedRelation(plan.source(), plan.table(), plan.alias(), plan.frozen(), - "invalid [" + table + "] resolution to [" + indexResolution + "]"); + new UnresolvedRelation( + plan.source(), + plan.table(), + plan.alias(), + plan.frozen(), + "invalid [" + table + "] resolution to [" + indexResolution + "]" + ); } LogicalPlan logicalPlan = new EsRelation(plan.source(), indexResolution.get(), plan.frozen()); SubQueryAlias sa = new SubQueryAlias(plan.source(), logicalPlan, indexResolution.get().toString()); @@ -331,12 +352,10 @@ protected LogicalPlan doRule(LogicalPlan plan) { if (hasStar(p.projections())) { return new Project(p.source(), p.child(), expandProjections(p.projections(), p.child())); } - } - else if (plan instanceof Aggregate) { + } else if (plan instanceof Aggregate) { Aggregate a = (Aggregate) plan; if (hasStar(a.aggregates())) { - return new Aggregate(a.source(), a.child(), a.groupings(), - expandProjections(a.aggregates(), a.child())); + return new Aggregate(a.source(), a.child(), a.groupings(), expandProjections(a.aggregates(), a.child())); } // if the grouping is unresolved but the aggs are, use the latter to resolve the former. // solves the case of queries declaring an alias in SELECT and referring to it in GROUP BY. @@ -349,8 +368,10 @@ else if (plan instanceof Aggregate) { boolean changed = false; for (Expression grouping : groupings) { if (grouping instanceof UnresolvedAttribute) { - Attribute maybeResolved = resolveAgainstList((UnresolvedAttribute) grouping, - resolvedAliases.stream().map(Tuple::v1).collect(toList())); + Attribute maybeResolved = resolveAgainstList( + (UnresolvedAttribute) grouping, + resolvedAliases.stream().map(Tuple::v1).collect(toList()) + ); if (maybeResolved != null) { changed = true; if (maybeResolved.resolved()) { @@ -408,7 +429,7 @@ else if (plan instanceof OrderBy) { } return named; } - //TODO: likely have to expand * inside functions as well + // TODO: likely have to expand * inside functions as well return u; }); } @@ -492,8 +513,9 @@ private LogicalPlan dedupRight(LogicalPlan left, LogicalPlan right) { AttributeSet conflicting = left.outputSet().intersect(right.outputSet()); if (log.isTraceEnabled()) { - log.trace("Trying to resolve conflicts " + conflicting + " between left " + left.nodeString() - + " and right " + right.nodeString()); + log.trace( + "Trying to resolve conflicts " + conflicting + " between left " + left.nodeString() + " and right " + right.nodeString() + ); } throw new UnsupportedOperationException("don't know how to resolve conficting IDs yet"); @@ -525,18 +547,26 @@ protected LogicalPlan doRule(LogicalPlan plan) { if (ordinal != null) { changed = true; if (ordinal > 0 && ordinal <= max) { - newOrder.add(new Order(order.source(), orderBy.child().output().get(ordinal - 1), order.direction(), - order.nullsPosition())); - } - else { + newOrder.add( + new Order( + order.source(), + orderBy.child().output().get(ordinal - 1), + order.direction(), + order.nullsPosition() + ) + ); + } else { // report error - String message = LoggerMessageFormat.format("Invalid ordinal [{}] specified in [{}] (valid range is [1, {}])", - ordinal, orderBy.sourceText(), max); + String message = LoggerMessageFormat.format( + "Invalid ordinal [{}] specified in [{}] (valid range is [1, {}])", + ordinal, + orderBy.sourceText(), + max + ); UnresolvedAttribute ua = new UnresolvedAttribute(child.source(), orderBy.sourceText(), null, message); newOrder.add(new Order(order.source(), ua, order.direction(), order.nullsPosition())); } - } - else { + } else { newOrder.add(order); } } @@ -565,22 +595,27 @@ protected LogicalPlan doRule(LogicalPlan plan) { NamedExpression reference = aggregates.get(ordinal - 1); if (containsAggregate(reference)) { errorMessage = LoggerMessageFormat.format( - "Ordinal [{}] in [{}] refers to an invalid argument, aggregate function [{}]", - ordinal, agg.sourceText(), reference.sourceText()); + "Ordinal [{}] in [{}] refers to an invalid argument, aggregate function [{}]", + ordinal, + agg.sourceText(), + reference.sourceText() + ); } else { newGroupings.add(reference); } - } - else { - errorMessage = LoggerMessageFormat.format("Invalid ordinal [{}] specified in [{}] (valid range is [1, {}])", - ordinal, agg.sourceText(), max); + } else { + errorMessage = LoggerMessageFormat.format( + "Invalid ordinal [{}] specified in [{}] (valid range is [1, {}])", + ordinal, + agg.sourceText(), + max + ); } if (errorMessage != null) { newGroupings.add(new UnresolvedAttribute(exp.source(), agg.sourceText(), null, errorMessage)); } - } - else { + } else { newGroupings.add(exp); } } @@ -627,13 +662,12 @@ protected LogicalPlan doRule(LogicalPlan plan) { maybeResolved.add(or.resolved() ? or : tryResolveExpression(or, child)); } - Stream referencesStream = maybeResolved.stream() - .filter(Expression::resolved); + Stream referencesStream = maybeResolved.stream().filter(Expression::resolved); // if there are any references in the output // try and resolve them to the source in order to compare the source expressions // e.g. ORDER BY a + 1 - // \ SELECT a + 1 + // \ SELECT a + 1 // a + 1 in SELECT is actually Alias("a + 1", a + 1) and translates to ReferenceAttribute // in the output. However it won't match the unnamed a + 1 despite being the same expression // so explicitly compare the source @@ -674,7 +708,8 @@ protected LogicalPlan doRule(LogicalPlan plan) { List newOrders = new ArrayList<>(); // transform the orders with the failed information for (Order order : o.order()) { - Order transformed = (Order) order.transformUp(UnresolvedAttribute.class, + Order transformed = (Order) order.transformUp( + UnresolvedAttribute.class, ua -> resolveMetadataToMessage(ua, failedAttrs, "order") ); newOrders.add(order.equals(transformed) ? order : transformed); @@ -696,9 +731,9 @@ protected LogicalPlan doRule(LogicalPlan plan) { Filter f = (Filter) plan; Expression maybeResolved = tryResolveExpression(f.condition(), f.child()); - AttributeSet resolvedRefs = new AttributeSet(maybeResolved.references().stream() - .filter(Expression::resolved) - .collect(toList())); + AttributeSet resolvedRefs = new AttributeSet( + maybeResolved.references().stream().filter(Expression::resolved).collect(toList()) + ); AttributeSet missing = resolvedRefs.subtract(f.child().outputSet()); @@ -710,9 +745,8 @@ protected LogicalPlan doRule(LogicalPlan plan) { // resolution failed and the failed expressions might contain resolution information so copy it over if (failedAttrs.isEmpty() == false) { // transform the orders with the failed information - Expression transformed = f.condition().transformUp(UnresolvedAttribute.class, - ua -> resolveMetadataToMessage(ua, failedAttrs, "filter") - ); + Expression transformed = f.condition() + .transformUp(UnresolvedAttribute.class, ua -> resolveMetadataToMessage(ua, failedAttrs, "filter")); return f.condition().equals(transformed) ? f : f.with(transformed); } @@ -751,7 +785,6 @@ static E tryResolveExpression(E exp, LogicalPlan plan) { return resolved; } - private static LogicalPlan propagateMissing(LogicalPlan plan, AttributeSet missing, List failed) { // no more attributes, bail out if (missing.isEmpty()) { @@ -773,8 +806,14 @@ private static LogicalPlan propagateMissing(LogicalPlan plan, AttributeSet missi // but we can't add an agg if the group is missing if (Expressions.match(a.groupings(), m::semanticEquals) == false) { // pass failure information to help the verifier - m = new UnresolvedAttribute(m.source(), m.name(), m.qualifier(), null, null, - new AggGroupingFailure(Expressions.names(a.groupings()))); + m = new UnresolvedAttribute( + m.source(), + m.name(), + m.qualifier(), + null, + null, + new AggGroupingFailure(Expressions.names(a.groupings())) + ); failed.add(m); } } @@ -804,7 +843,8 @@ private static UnresolvedAttribute resolveMetadataToMessage(UnresolvedAttribute if (metadata instanceof AggGroupingFailure) { List names = ((AggGroupingFailure) metadata).expectedGrouping; return ua.withUnresolvedMessage( - "Cannot " + actionName + " by non-grouped column [" + ua.qualifiedName() + "], expected " + names); + "Cannot " + actionName + " by non-grouped column [" + ua.qualifiedName() + "], expected " + names + ); } } } @@ -866,10 +906,10 @@ private Expression replaceAliases(Expression condition, List e == u) == false && - (qualified ? - Objects.equals(alias.qualifiedName(), u.qualifiedName()) : - Objects.equals(alias.name(), u.name()))) { + if (alias.anyMatch(e -> e == u) == false + && (qualified + ? Objects.equals(alias.qualifiedName(), u.qualifiedName()) + : Objects.equals(alias.name(), u.name()))) { return alias; } } @@ -897,8 +937,12 @@ protected LogicalPlan rule(LogicalPlan plan) { uf = uf.withMessage("Can't extract from *"); } else { if (uf.name().toUpperCase(Locale.ROOT).equals("COUNT")) { - uf = new UnresolvedFunction(uf.source(), uf.name(), strategy, - singletonList(new Literal(uf.arguments().get(0).source(), Integer.valueOf(1), DataTypes.INTEGER))); + uf = new UnresolvedFunction( + uf.source(), + uf.name(), + strategy, + singletonList(new Literal(uf.arguments().get(0).source(), Integer.valueOf(1), DataTypes.INTEGER)) + ); } } if (uf.analyzed()) { @@ -983,7 +1027,6 @@ private List assignAliases(List expr } } - // // Replace a project with aggregation into an aggregation // @@ -1017,9 +1060,9 @@ protected LogicalPlan rule(Filter f) { } // no literal or aggregates - it's a 'regular' projection if (n.foldable() == false && Functions.isAggregate(n) == false - // folding might not work (it might wait for the optimizer) - // so check whether any column is referenced - && n.anyMatch(FieldAttribute.class::isInstance)) { + // folding might not work (it might wait for the optimizer) + // so check whether any column is referenced + && n.anyMatch(FieldAttribute.class::isInstance)) { return f; } } @@ -1063,16 +1106,19 @@ protected LogicalPlan rule(Filter f) { // so try resolving the condition in one go through a 'dummy' aggregate if (condition.resolved() == false) { // that's why try to resolve the condition - Aggregate tryResolvingCondition = new Aggregate(agg.source(), agg.child(), agg.groupings(), - combine(agg.aggregates(), new Alias(f.source(), ".having", condition))); + Aggregate tryResolvingCondition = new Aggregate( + agg.source(), + agg.child(), + agg.groupings(), + combine(agg.aggregates(), new Alias(f.source(), ".having", condition)) + ); tryResolvingCondition = (Aggregate) analyze(tryResolvingCondition, false); // if it got resolved if (tryResolvingCondition.resolved()) { // replace the condition with the resolved one - condition = ((Alias) tryResolvingCondition.aggregates() - .get(tryResolvingCondition.aggregates().size() - 1)).child(); + condition = ((Alias) tryResolvingCondition.aggregates().get(tryResolvingCondition.aggregates().size() - 1)).child(); } else { // else bail out return f; @@ -1111,7 +1157,6 @@ private Set findMissingAggregate(Aggregate target, Expression f } } - // // Handle aggs in ORDER BY. To help folding any aggs not found in Aggregation // will be pushed down to the Aggregate and then projected. This also simplifies the Verifier's job. @@ -1229,14 +1274,14 @@ protected LogicalPlan rule(UnaryPlan plan) { if (plan.child() instanceof SubQueryAlias) { SubQueryAlias a = (SubQueryAlias) plan.child(); return plan.transformExpressionsDown(FieldAttribute.class, f -> { - if (f.qualifier() != null && f.qualifier().equals(a.alias())) { - // Find the underlying concrete relation (EsIndex) and its name as the new qualifier - List children = a.collectFirstChildren(p -> p instanceof EsRelation); - if (children.isEmpty() == false) { - return f.withQualifier(((EsRelation) children.get(0)).index().name()); - } - } - return f; + if (f.qualifier() != null && f.qualifier().equals(a.alias())) { + // Find the underlying concrete relation (EsIndex) and its name as the new qualifier + List children = a.collectFirstChildren(p -> p instanceof EsRelation); + if (children.isEmpty() == false) { + return f.withQualifier(((EsRelation) children.get(0)).index().name()); + } + } + return f; }); } return plan; @@ -1281,8 +1326,13 @@ protected LogicalPlan rule(LogicalPlan plan) { if (plan instanceof Pivot) { Pivot p = (Pivot) plan; - return new Pivot(p.source(), p.child(), trimAliases(p.column()), cleanChildrenAliases(p.values()), - cleanChildrenAliases(p.aggregates())); + return new Pivot( + p.source(), + p.child(), + trimAliases(p.column()), + cleanChildrenAliases(p.values()), + cleanChildrenAliases(p.aggregates()) + ); } return plan.transformExpressionsOnly(Alias.class, a -> a.child()); diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/analysis/analyzer/Verifier.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/analysis/analyzer/Verifier.java index a20283f95c7c6..635db2287f402 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/analysis/analyzer/Verifier.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/analysis/analyzer/Verifier.java @@ -281,9 +281,9 @@ private void checkNestedAggregation(LogicalPlan p, Set localFailures, A private void checkFullTextSearchInSelect(LogicalPlan plan, Set localFailures) { plan.forEachUp(Project.class, p -> { for (NamedExpression ne : p.projections()) { - ne.forEachUp(FullTextPredicate.class, (e) -> - localFailures.add(fail(e, "Cannot use MATCH() or QUERY() full-text search " + - "functions in the SELECT clause")) + ne.forEachUp( + FullTextPredicate.class, + (e) -> localFailures.add(fail(e, "Cannot use MATCH() or QUERY() full-text search " + "functions in the SELECT clause")) ); } }); @@ -300,18 +300,26 @@ private void checkFullTextSearchInSelect(LogicalPlan plan, Set localFai * 2a. HAVING also requires an Aggregate function * 3. composite agg (used for GROUP BY) allows ordering only on the group keys */ - private static boolean checkGroupBy(LogicalPlan p, Set localFailures, AttributeMap attributeRefs, - Set groupingFailures) { + private static boolean checkGroupBy( + LogicalPlan p, + Set localFailures, + AttributeMap attributeRefs, + Set groupingFailures + ) { return checkGroupByInexactField(p, localFailures) - && checkGroupByAgg(p, localFailures, attributeRefs) - && checkGroupByOrder(p, localFailures, groupingFailures, attributeRefs) - && checkGroupByHaving(p, localFailures, groupingFailures, attributeRefs) - && checkGroupByTime(p, localFailures); + && checkGroupByAgg(p, localFailures, attributeRefs) + && checkGroupByOrder(p, localFailures, groupingFailures, attributeRefs) + && checkGroupByHaving(p, localFailures, groupingFailures, attributeRefs) + && checkGroupByTime(p, localFailures); } // check whether an orderBy failed or if it occurs on a non-key - private static boolean checkGroupByOrder(LogicalPlan p, Set localFailures, Set groupingFailures, - AttributeMap attributeRefs) { + private static boolean checkGroupByOrder( + LogicalPlan p, + Set localFailures, + Set groupingFailures, + AttributeMap attributeRefs + ) { if (p instanceof OrderBy) { OrderBy o = (OrderBy) p; LogicalPlan child = o.child(); @@ -354,12 +362,11 @@ private static boolean checkGroupByOrder(LogicalPlan p, Set localFailur // e.g.: if "GROUP BY f2(f1(field))" you can "ORDER BY f4(f3(f2(f1(field))))" // // Also, make sure to compare attributes directly - if (resolvedE.anyMatch(expression -> Expressions.anyMatch(groupingAndMatchingAggregatesAliases, - g -> { - Expression resolvedG = attributeRefs.resolve(g, g); - resolvedG = expression instanceof Attribute ? Expressions.attribute(resolvedG) : resolvedG; - return expression.semanticEquals(resolvedG); - }))) { + if (resolvedE.anyMatch(expression -> Expressions.anyMatch(groupingAndMatchingAggregatesAliases, g -> { + Expression resolvedG = attributeRefs.resolve(g, g); + resolvedG = expression instanceof Attribute ? Expressions.attribute(resolvedG) : resolvedG; + return expression.semanticEquals(resolvedG); + }))) { return; } @@ -371,10 +378,13 @@ private static boolean checkGroupByOrder(LogicalPlan p, Set localFailur String plural = missing.size() > 1 ? "s" : StringUtils.EMPTY; // get the location of the first missing expression as the order by might be on a different line localFailures.add( - fail(missing.values().iterator().next(), - "Cannot order by non-grouped column" + plural + " {}, expected {} or an aggregate function", - Expressions.names(missing.keySet()), - Expressions.names(a.groupings()))); + fail( + missing.values().iterator().next(), + "Cannot order by non-grouped column" + plural + " {}, expected {} or an aggregate function", + Expressions.names(missing.keySet()), + Expressions.names(a.groupings()) + ) + ); groupingFailures.add(a); return false; } @@ -383,8 +393,12 @@ private static boolean checkGroupByOrder(LogicalPlan p, Set localFailur return true; } - private static boolean checkGroupByHaving(LogicalPlan p, Set localFailures, - Set groupingFailures, AttributeMap attributeRefs) { + private static boolean checkGroupByHaving( + LogicalPlan p, + Set localFailures, + Set groupingFailures, + AttributeMap attributeRefs + ) { if (p instanceof Having) { Having h = (Having) p; if (h.child() instanceof Aggregate) { @@ -399,8 +413,12 @@ private static boolean checkGroupByHaving(LogicalPlan p, Set localFailu if (missing.isEmpty() == false) { String plural = missing.size() > 1 ? "s" : StringUtils.EMPTY; localFailures.add( - fail(condition, "Cannot use HAVING filter on non-aggregate" + plural + " {}; use WHERE instead", - Expressions.names(missing))); + fail( + condition, + "Cannot use HAVING filter on non-aggregate" + plural + " {}; use WHERE instead", + Expressions.names(missing) + ) + ); groupingFailures.add(a); return false; } @@ -408,8 +426,8 @@ private static boolean checkGroupByHaving(LogicalPlan p, Set localFailu if (unsupported.isEmpty() == false) { String plural = unsupported.size() > 1 ? "s" : StringUtils.EMPTY; localFailures.add( - fail(condition, "HAVING filter is unsupported for function" + plural + " {}", - Expressions.names(unsupported))); + fail(condition, "HAVING filter is unsupported for function" + plural + " {}", Expressions.names(unsupported)) + ); groupingFailures.add(a); return false; } @@ -418,9 +436,12 @@ private static boolean checkGroupByHaving(LogicalPlan p, Set localFailu return true; } - - private static boolean checkGroupByHavingHasOnlyAggs(Expression e, Set missing, - Set unsupported, AttributeMap attributeRefs) { + private static boolean checkGroupByHavingHasOnlyAggs( + Expression e, + Set missing, + Set unsupported, + AttributeMap attributeRefs + ) { // resolve FunctionAttribute to backing functions if (e instanceof ReferenceAttribute) { @@ -487,8 +508,15 @@ private static boolean onlyExactFields(List expressions, Set e.forEachUp(FieldAttribute.class, c -> { EsField.Exact exact = c.getExactInfo(); if (exact.hasExact() == false) { - localFailures.add(fail(c, "Field [{}] of data type [{}] cannot be used for grouping; {}", c.sourceText(), - c.dataType().typeName(), exact.errorMsg())); + localFailures.add( + fail( + c, + "Field [{}] of data type [{}] cannot be used for grouping; {}", + c.sourceText(), + c.dataType().typeName(), + exact.errorMsg() + ) + ); onlyExact.set(Boolean.FALSE); } })); @@ -496,8 +524,11 @@ private static boolean onlyExactFields(List expressions, Set expressions, Set localFailures, - AttributeMap attributeRefs) { + private static boolean onlyRawFields( + Iterable expressions, + Set localFailures, + AttributeMap attributeRefs + ) { Holder onlyExact = new Holder<>(Boolean.TRUE); expressions.forEach(e -> e.forEachDown(c -> { @@ -520,8 +551,17 @@ private static boolean checkGroupByTime(LogicalPlan p, Set localFailure // https://github.com/elastic/elasticsearch/issues/40639 a.groupings().forEach(f -> { if (f.dataType() == SqlDataTypes.TIME) { - localFailures.add(fail(f, "Function [" + f.sourceText() + "] with data type [" + f.dataType().typeName() + - "] " + "cannot be used for grouping")); + localFailures.add( + fail( + f, + "Function [" + + f.sourceText() + + "] with data type [" + + f.dataType().typeName() + + "] " + + "cannot be used for grouping" + ) + ); } }); } @@ -547,10 +587,15 @@ private static boolean checkGroupByAgg(LogicalPlan p, Set localFailures if (Functions.isGrouping(e) == false) { e.collectFirstChildren(c -> { if (Functions.isGrouping(c)) { - localFailures.add(fail(c, + localFailures.add( + fail( + c, "Cannot combine [{}] grouping function inside GROUP BY, found [{}];" - + " consider moving the expression inside the histogram", - Expressions.name(c), Expressions.name(e))); + + " consider moving the expression inside the histogram", + Expressions.name(c), + Expressions.name(e) + ) + ); return true; } return false; @@ -566,20 +611,24 @@ private static boolean checkGroupByAgg(LogicalPlan p, Set localFailures // 1. plain column - in which case, there should be an equivalent in groupings // 2. aggregate over non-grouped column // 3. scalar function on top of 1 and/or 2. the function needs unfolding to make sure - // the 'source' is valid. + // the 'source' is valid. // Note that grouping can be done by a function (GROUP BY YEAR(date)) which means date // cannot be used as a plain column, only YEAR(date) or aggs(?) on top of it Map> missing = new LinkedHashMap<>(); - a.aggregates().forEach(ne -> - ne.collectFirstChildren(c -> checkGroupMatch(c, ne, a.groupings(), missing, attributeRefs))); + a.aggregates().forEach(ne -> ne.collectFirstChildren(c -> checkGroupMatch(c, ne, a.groupings(), missing, attributeRefs))); if (missing.isEmpty() == false) { String plural = missing.size() > 1 ? "s" : StringUtils.EMPTY; - localFailures.add(fail(missing.values().iterator().next(), "Cannot use non-grouped column" + plural + " {}, expected {}", + localFailures.add( + fail( + missing.values().iterator().next(), + "Cannot use non-grouped column" + plural + " {}, expected {}", Expressions.names(missing.keySet()), - Expressions.names(a.groupings()))); + Expressions.names(a.groupings()) + ) + ); return false; } } @@ -587,8 +636,13 @@ private static boolean checkGroupByAgg(LogicalPlan p, Set localFailures return true; } - private static boolean checkGroupMatch(Expression e, Node source, List groupings, - Map> missing, AttributeMap attributeRefs) { + private static boolean checkGroupMatch( + Expression e, + Node source, + List groupings, + Map> missing, + AttributeMap attributeRefs + ) { // 1:1 match if (Expressions.match(groupings, e::semanticEquals)) { @@ -650,32 +704,40 @@ private static void checkGroupingFunctionInGroupBy(LogicalPlan p, Set l // check if the query has a grouping function (Histogram) but no GROUP BY if (p instanceof Project) { Project proj = (Project) p; - proj.projections().forEach(e -> e.forEachDown(GroupingFunction.class, f -> - localFailures.add(fail(f, "[{}] needs to be part of the grouping", Expressions.name(f))))); + proj.projections() + .forEach( + e -> e.forEachDown( + GroupingFunction.class, + f -> localFailures.add(fail(f, "[{}] needs to be part of the grouping", Expressions.name(f))) + ) + ); } // if it does have a GROUP BY, check if the groupings contain the grouping functions (Histograms) else if (p instanceof Aggregate) { Aggregate a = (Aggregate) p; a.aggregates().forEach(agg -> agg.forEachDown(GroupingFunction.class, e -> { - if (a.groupings().size() == 0 - || Expressions.anyMatch(a.groupings(), g -> g instanceof Function && e.equals(g)) == false) { + if (a.groupings().size() == 0 || Expressions.anyMatch(a.groupings(), g -> g instanceof Function && e.equals(g)) == false) { localFailures.add(fail(e, "[{}] needs to be part of the grouping", Expressions.name(e))); } else { checkGroupingFunctionTarget(e, localFailures); } })); - a.groupings().forEach(g -> g.forEachDown(GroupingFunction.class, e -> { - checkGroupingFunctionTarget(e, localFailures); - })); + a.groupings().forEach(g -> g.forEachDown(GroupingFunction.class, e -> { checkGroupingFunctionTarget(e, localFailures); })); } } private static void checkGroupingFunctionTarget(GroupingFunction f, Set localFailures) { f.field().forEachDown(e -> { if (e instanceof GroupingFunction) { - localFailures.add(fail(f.field(), "Cannot embed grouping functions within each other, found [{}] in [{}]", - Expressions.name(f.field()), Expressions.name(f))); + localFailures.add( + fail( + f.field(), + "Cannot embed grouping functions within each other, found [{}] in [{}]", + Expressions.name(f.field()), + Expressions.name(f) + ) + ); } }); } @@ -687,13 +749,21 @@ private static void checkFilterOnAggs(LogicalPlan p, Set localFailures, filter.condition().forEachDown(Expression.class, e -> { if (Functions.isAggregate(attributeRefs.resolve(e, e))) { if (filter.child() instanceof Project) { - filter.condition().forEachDown(FieldAttribute.class, - f -> localFailures.add(fail(e, "[{}] field must appear in the GROUP BY clause or in an aggregate function", - Expressions.name(f))) - ); + filter.condition() + .forEachDown( + FieldAttribute.class, + f -> localFailures.add( + fail( + e, + "[{}] field must appear in the GROUP BY clause or in an aggregate function", + Expressions.name(f) + ) + ) + ); } else { - localFailures.add(fail(e, "Cannot use WHERE filtering on aggregate function [{}], use HAVING instead", - Expressions.name(e))); + localFailures.add( + fail(e, "Cannot use WHERE filtering on aggregate function [{}], use HAVING instead", Expressions.name(e)) + ); } } @@ -709,93 +779,101 @@ private static void checkFilterOnAggs(LogicalPlan p, Set localFailures, if (unsupported.isEmpty() == false) { String plural = unsupported.size() > 1 ? "s" : StringUtils.EMPTY; localFailures.add( - fail(filter.condition(), "filtering is unsupported for function" + plural + " {}", - Expressions.names(unsupported))); + fail(filter.condition(), "filtering is unsupported for function" + plural + " {}", Expressions.names(unsupported)) + ); } } } } - private static void checkFilterOnGrouping(LogicalPlan p, Set localFailures, AttributeMap attributeRefs) { if (p instanceof Filter) { Filter filter = (Filter) p; filter.condition().forEachDown(Expression.class, e -> { if (Functions.isGrouping(attributeRefs.resolve(e, e))) { - localFailures - .add(fail(e, "Cannot filter on grouping function [{}], use its argument instead", Expressions.name(e))); + localFailures.add(fail(e, "Cannot filter on grouping function [{}], use its argument instead", Expressions.name(e))); } }); } } - private static void checkForScoreInsideFunctions(LogicalPlan p, Set localFailures) { // Make sure that SCORE is only used as a "top level" function - p.forEachExpression(Function.class, f -> - f.arguments().stream() + p.forEachExpression( + Function.class, + f -> f.arguments() + .stream() .filter(exp -> exp.anyMatch(Score.class::isInstance)) - .forEach(exp -> localFailures.add(fail(exp, - "[SCORE()] cannot be used in expressions, does not support further processing"))) + .forEach( + exp -> localFailures.add(fail(exp, "[SCORE()] cannot be used in expressions, does not support further processing")) + ) ); } - private static void checkNestedUsedInGroupByOrHavingOrWhereOrOrderBy(LogicalPlan p, Set localFailures, - AttributeMap attributeRefs) { + private static void checkNestedUsedInGroupByOrHavingOrWhereOrOrderBy( + LogicalPlan p, + Set localFailures, + AttributeMap attributeRefs + ) { List nested = new ArrayList<>(); Consumer matchNested = fa -> { if (fa.isNested()) { nested.add(fa); } }; - Consumer checkForNested = e -> - attributeRefs.resolve(e, e).forEachUp(FieldAttribute.class, matchNested); - Consumer checkForNestedInFunction = f -> f.arguments().forEach( - arg -> arg.forEachUp(FieldAttribute.class, matchNested)); + Consumer checkForNested = e -> attributeRefs.resolve(e, e).forEachUp(FieldAttribute.class, matchNested); + Consumer checkForNestedInFunction = f -> f.arguments() + .forEach(arg -> arg.forEachUp(FieldAttribute.class, matchNested)); // nested fields shouldn't be used in aggregates or having (yet) p.forEachDown(Aggregate.class, a -> a.groupings().forEach(agg -> agg.forEachUp(checkForNested))); if (nested.isEmpty() == false) { localFailures.add( - fail(nested.get(0), "Grouping isn't (yet) compatible with nested fields " + new AttributeSet(nested).names())); + fail(nested.get(0), "Grouping isn't (yet) compatible with nested fields " + new AttributeSet(nested).names()) + ); nested.clear(); } // check in having p.forEachDown(Filter.class, f -> f.forEachDown(Aggregate.class, a -> f.condition().forEachUp(checkForNested))); if (nested.isEmpty() == false) { - localFailures.add( - fail(nested.get(0), "HAVING isn't (yet) compatible with nested fields " + new AttributeSet(nested).names())); + localFailures.add(fail(nested.get(0), "HAVING isn't (yet) compatible with nested fields " + new AttributeSet(nested).names())); nested.clear(); } // check in where (scalars not allowed) - p.forEachDown(Filter.class, f -> f.condition().forEachUp(e -> - attributeRefs.resolve(e, e).forEachUp(ScalarFunction.class, sf -> { - if (sf instanceof BinaryComparison == false && - sf instanceof IsNull == false && - sf instanceof IsNotNull == false && - sf instanceof Not == false && - sf instanceof BinaryLogic == false) { - checkForNestedInFunction.accept(sf); - } - }) - )); + p.forEachDown(Filter.class, f -> f.condition().forEachUp(e -> attributeRefs.resolve(e, e).forEachUp(ScalarFunction.class, sf -> { + if (sf instanceof BinaryComparison == false + && sf instanceof IsNull == false + && sf instanceof IsNotNull == false + && sf instanceof Not == false + && sf instanceof BinaryLogic == false) { + checkForNestedInFunction.accept(sf); + } + }))); if (nested.isEmpty() == false) { localFailures.add( - fail(nested.get(0), "WHERE isn't (yet) compatible with scalar functions on nested fields " + - new AttributeSet(nested).names())); + fail( + nested.get(0), + "WHERE isn't (yet) compatible with scalar functions on nested fields " + new AttributeSet(nested).names() + ) + ); nested.clear(); } // check in order by (scalars not allowed) - p.forEachDown(OrderBy.class, ob -> ob.order().forEach(o -> o.forEachUp(e -> - attributeRefs.resolve(e, e).forEachUp(ScalarFunction.class, checkForNestedInFunction) - ))); + p.forEachDown( + OrderBy.class, + ob -> ob.order() + .forEach(o -> o.forEachUp(e -> attributeRefs.resolve(e, e).forEachUp(ScalarFunction.class, checkForNestedInFunction))) + ); if (nested.isEmpty() == false) { localFailures.add( - fail(nested.get(0), "ORDER BY isn't (yet) compatible with scalar functions on nested fields " + - new AttributeSet(nested).names())); + fail( + nested.get(0), + "ORDER BY isn't (yet) compatible with scalar functions on nested fields " + new AttributeSet(nested).names() + ) + ); } } @@ -825,7 +903,6 @@ private static void checkForGeoFunctionsOnDocValues(LogicalPlan p, Set } }))); - // geo shape fields shouldn't be used in order by clauses p.forEachDown(OrderBy.class, o -> o.order().forEach(agg -> agg.forEachUp(FieldAttribute.class, fa -> { if (fa.field().getDataType() == GEO_SHAPE) { @@ -853,14 +930,21 @@ private static void checkPivot(LogicalPlan p, Set localFailures, Attrib Expression ex = v instanceof Alias ? ((Alias) v).child() : v; if (ex instanceof Literal == false) { localFailures.add(fail(v, "Non-literal [{}] found inside PIVOT values", v.name())); - } - else if (ex.foldable() && ex.fold() == null) { + } else if (ex.foldable() && ex.fold() == null) { localFailures.add(fail(v, "Null not allowed as a PIVOT value", v.name())); } // and that their type is compatible with that of the column else if (SqlDataTypes.areCompatible(colType, v.dataType()) == false) { - localFailures.add(fail(v, "Literal [{}] of type [{}] does not match type [{}] of PIVOT column [{}]", v.name(), - v.dataType().typeName(), colType.typeName(), pv.column().sourceText())); + localFailures.add( + fail( + v, + "Literal [{}] of type [{}] does not match type [{}] of PIVOT column [{}]", + v.name(), + v.dataType().typeName(), + colType.typeName(), + pv.column().sourceText() + ) + ); } } @@ -909,11 +993,16 @@ private static void checkCastOnInexact(LogicalPlan p, Set localFailures p.forEachDown(Filter.class, f -> f.forEachExpressionUp(Cast.class, c -> { if (c.field() instanceof FieldAttribute) { EsField.Exact exactInfo = ((FieldAttribute) c.field()).getExactInfo(); - if (exactInfo.hasExact() == false - || ((FieldAttribute) c.field()).exactAttribute().equals(c.field()) == false) { - localFailures.add(fail(c.field(), - "[{}] of data type [{}] cannot be used for [{}()] inside the WHERE clause", - c.field().sourceText(), c.field().dataType().typeName(), c.functionName())); + if (exactInfo.hasExact() == false || ((FieldAttribute) c.field()).exactAttribute().equals(c.field()) == false) { + localFailures.add( + fail( + c.field(), + "[{}] of data type [{}] cannot be used for [{}()] inside the WHERE clause", + c.field().sourceText(), + c.field().dataType().typeName(), + c.functionName() + ) + ); } } })); @@ -923,16 +1012,26 @@ private static void checkCastOnInexact(LogicalPlan p, Set localFailures private static void checkBinaryHasDocValues(LogicalPlan plan, Set localFailures) { List> fields = new ArrayList<>(); - plan.forEachDown(Filter.class, e -> e.condition().forEachDown(FieldAttribute.class, - f -> fields.add(Tuple.tuple(f, "for filtering")))); - plan.forEachDown(Aggregate.class, e -> e.groupings().forEach(g -> g.forEachDown(FieldAttribute.class, - f -> fields.add(Tuple.tuple(f, "in aggregations"))))); - plan.forEachDown(OrderBy.class, e -> e.order().forEach(o -> o.child().forEachDown(FieldAttribute.class, - f -> fields.add(Tuple.tuple(f, "for ordering"))))); + plan.forEachDown( + Filter.class, + e -> e.condition().forEachDown(FieldAttribute.class, f -> fields.add(Tuple.tuple(f, "for filtering"))) + ); + plan.forEachDown( + Aggregate.class, + e -> e.groupings().forEach(g -> g.forEachDown(FieldAttribute.class, f -> fields.add(Tuple.tuple(f, "in aggregations")))) + ); + plan.forEachDown( + OrderBy.class, + e -> e.order().forEach(o -> o.child().forEachDown(FieldAttribute.class, f -> fields.add(Tuple.tuple(f, "for ordering")))) + ); fields.stream().filter(t -> t.v1().dataType() == BINARY && t.v1().field().isAggregatable() == false).forEach(t -> { - localFailures.add(fail(t.v1(), "Binary field [" + t.v1().name() + "] cannot be used " + t.v2() + " unless it has the " - + "doc_values setting enabled")); + localFailures.add( + fail( + t.v1(), + "Binary field [" + t.v1().name() + "] cannot be used " + t.v2() + " unless it has the " + "doc_values setting enabled" + ) + ); }); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/PlanExecutor.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/PlanExecutor.java index 47afcdc838aa9..7ae91fef25a1d 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/PlanExecutor.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/PlanExecutor.java @@ -67,8 +67,12 @@ private SqlSession newSession(SqlConfiguration cfg) { return new SqlSession(cfg, client, functionRegistry, indexResolver, preAnalyzer, verifier, optimizer, planner, this); } - public void searchSource(SqlConfiguration cfg, String sql, List params, - ActionListener listener) { + public void searchSource( + SqlConfiguration cfg, + String sql, + List params, + ActionListener listener + ) { metrics.translate(); newSession(cfg).sqlExecutable(sql, params, wrap(exec -> { @@ -80,11 +84,10 @@ public void searchSource(SqlConfiguration cfg, String sql, List exts, BitSet mask, int remainingLimit, boolean includeFrozen, - String... indices) { + CompositeAggCursor(byte[] next, List exts, BitSet mask, int remainingLimit, boolean includeFrozen, String... indices) { this.indices = indices; this.nextQuery = next; this.extractors = exts; @@ -138,12 +137,15 @@ public void nextPage(SqlConfiguration cfg, Client client, NamedWriteableRegistry client.search(request, new ActionListener.Delegating<>(listener) { @Override public void onResponse(SearchResponse response) { - handle(response, request.source(), - makeRowSet(response), - makeCursor(), - () -> client.search(request, this), - delegate, - Schema.EMPTY); + handle( + response, + request.source(), + makeRowSet(response), + makeCursor(), + () -> client.search(request, this), + delegate, + Schema.EMPTY + ); } }); } @@ -156,12 +158,15 @@ protected BiFunction makeCursor( return (q, r) -> new CompositeAggCursor(q, r.extractors(), r.mask(), r.remainingData(), includeFrozen, indices); } - static void handle(SearchResponse response, SearchSourceBuilder source, - Supplier makeRowSet, - BiFunction makeCursor, - Runnable retry, - ActionListener listener, - Schema schema) { + static void handle( + SearchResponse response, + SearchSourceBuilder source, + Supplier makeRowSet, + BiFunction makeCursor, + Runnable retry, + ActionListener listener, + Schema schema + ) { if (log.isTraceEnabled()) { Querier.logSearchResponse(response, log); @@ -185,9 +190,7 @@ static void handle(SearchResponse response, SearchSourceBuilder source, queryAsBytes = serializeQuery(source); } - Cursor next = rowSet.remainingData() == 0 - ? Cursor.EMPTY - : makeCursor.apply(queryAsBytes, rowSet); + Cursor next = rowSet.remainingData() == 0 ? Cursor.EMPTY : makeCursor.apply(queryAsBytes, rowSet); listener.onResponse(new Page(rowSet, next)); } catch (Exception ex) { listener.onFailure(ex); @@ -265,7 +268,6 @@ private static byte[] serializeQuery(SearchSourceBuilder source) throws IOExcept } } - @Override public void clear(Client client, ActionListener listener) { listener.onResponse(true); @@ -283,10 +285,10 @@ public boolean equals(Object obj) { } CompositeAggCursor other = (CompositeAggCursor) obj; return Arrays.equals(indices, other.indices) - && Arrays.equals(nextQuery, other.nextQuery) - && Objects.equals(extractors, other.extractors) - && Objects.equals(limit, other.limit) - && Objects.equals(includeFrozen, other.includeFrozen); + && Arrays.equals(nextQuery, other.nextQuery) + && Objects.equals(extractors, other.extractors) + && Objects.equals(limit, other.limit) + && Objects.equals(includeFrozen, other.includeFrozen); } @Override diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/PivotCursor.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/PivotCursor.java index 943659f29afc2..66cb6bda21ede 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/PivotCursor.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/PivotCursor.java @@ -27,9 +27,15 @@ public class PivotCursor extends CompositeAggCursor { private final Map previousKey; - PivotCursor(Map previousKey, byte[] next, List exts, BitSet mask, int remainingLimit, - boolean includeFrozen, - String... indices) { + PivotCursor( + Map previousKey, + byte[] next, + List exts, + BitSet mask, + int remainingLimit, + boolean includeFrozen, + String... indices + ) { super(next, exts, mask, remainingLimit, includeFrozen, indices); this.previousKey = previousKey; } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/PivotRowSet.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/PivotRowSet.java index 46c087fabfaed..84cdcf14b64ee 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/PivotRowSet.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/PivotRowSet.java @@ -26,8 +26,14 @@ class PivotRowSet extends SchemaCompositeAggRowSet { private final List data; private final Map lastAfterKey; - PivotRowSet(Schema schema, List exts, BitSet mask, SearchResponse response, int limit, - Map previousLastKey) { + PivotRowSet( + Schema schema, + List exts, + BitSet mask, + SearchResponse response, + int limit, + Map previousLastKey + ) { super(schema, exts, mask, response, limit); data = buckets.isEmpty() ? emptyList() : new ArrayList<>(); diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/Querier.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/Querier.java index 7603a07f7c59e..53104ace2e545 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/Querier.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/Querier.java @@ -118,8 +118,12 @@ public void query(List output, QueryContainer query, String index, Ac log.trace("About to execute query {} on {}", StringUtils.toString(sourceBuilder), index); } - SearchRequest search = prepareRequest(sourceBuilder, cfg.requestTimeout(), query.shouldIncludeFrozen(), - Strings.commaDelimitedListToStringArray(index)); + SearchRequest search = prepareRequest( + sourceBuilder, + cfg.requestTimeout(), + query.shouldIncludeFrozen(), + Strings.commaDelimitedListToStringArray(index) + ); @SuppressWarnings("rawtypes") List> sortingColumns = query.sortingColumns(); @@ -144,8 +148,7 @@ public void query(List output, QueryContainer query, String index, Ac client.search(search, l); } - public static SearchRequest prepareRequest(SearchSourceBuilder source, TimeValue timeout, boolean includeFrozen, - String... indices) { + public static SearchRequest prepareRequest(SearchSourceBuilder source, TimeValue timeout, boolean includeFrozen, String... indices) { source.timeout(timeout); SearchRequest searchRequest = new SearchRequest(INTRODUCING_MISSING_ORDER_IN_COMPOSITE_AGGS_VERSION); @@ -153,7 +156,8 @@ public static SearchRequest prepareRequest(SearchSourceBuilder source, TimeValue searchRequest.source(source); searchRequest.allowPartialSearchResults(false); searchRequest.indicesOptions( - includeFrozen ? IndexResolver.FIELD_CAPS_FROZEN_INDICES_OPTIONS : IndexResolver.FIELD_CAPS_INDICES_OPTIONS); + includeFrozen ? IndexResolver.FIELD_CAPS_FROZEN_INDICES_OPTIONS : IndexResolver.FIELD_CAPS_INDICES_OPTIONS + ); return searchRequest; } @@ -168,18 +172,20 @@ protected static void logSearchResponse(SearchResponse response, Logger logger) aggsNames.append(aggs.get(i).getName() + (i + 1 == aggs.size() ? "" : ", ")); } - logger.trace("Got search response [hits {} {}, {} aggregations: [{}], {} failed shards, {} skipped shards, " + logger.trace( + "Got search response [hits {} {}, {} aggregations: [{}], {} failed shards, {} skipped shards, " + "{} successful shards, {} total shards, took {}, timed out [{}]]", - response.getHits().getTotalHits().relation.toString(), - response.getHits().getTotalHits().value, - aggs.size(), - aggsNames, - response.getFailedShards(), - response.getSkippedShards(), - response.getSuccessfulShards(), - response.getTotalShards(), - response.getTook(), - response.isTimedOut()); + response.getHits().getTotalHits().relation.toString(), + response.getHits().getTotalHits().value, + aggs.size(), + aggsNames, + response.getFailedShards(), + response.getSkippedShards(), + response.getSuccessfulShards(), + response.getTotalShards(), + response.getTook(), + response.isTimedOut() + ); } /** @@ -256,8 +262,12 @@ private boolean consumeRowSet(RowSet rowSet) { rrs.forEachResultColumn(row::add); // if the queue overflows and no limit was specified, throw an error if (data.insertWithOverflow(new Tuple<>(row, counter.getAndIncrement())) != null && noLimit) { - onFailure(new SqlIllegalArgumentException( - "The default limit [{}] for aggregate sorting has been reached; please specify a LIMIT", MAXIMUM_SIZE)); + onFailure( + new SqlIllegalArgumentException( + "The default limit [{}] for aggregate sorting has been reached; please specify a LIMIT", + MAXIMUM_SIZE + ) + ); return false; } } @@ -302,8 +312,14 @@ public Aggregations getAggregations() { } }); - ImplicitGroupActionListener(ActionListener listener, Client client, SqlConfiguration cfg, List output, - QueryContainer query, SearchRequest request) { + ImplicitGroupActionListener( + ActionListener listener, + Client client, + SqlConfiguration cfg, + List output, + QueryContainer query, + SearchRequest request + ) { super(listener, client, cfg, output, query, request); } @@ -344,13 +360,14 @@ private void handleBuckets(List buckets, SearchResponse respon } else if (buckets.isEmpty()) { delegate.onResponse(Page.last(Rows.empty(schema))); } else { - throw new SqlIllegalArgumentException("Too many groups returned by the implicit group; expected 1, received {}", - buckets.size()); + throw new SqlIllegalArgumentException( + "Too many groups returned by the implicit group; expected 1, received {}", + buckets.size() + ); } } } - /** * Dedicated listener for composite aggs/group-by results. */ @@ -358,8 +375,14 @@ static class CompositeActionListener extends BaseAggActionListener { private final boolean isPivot; - CompositeActionListener(ActionListener listener, Client client, SqlConfiguration cfg, List output, - QueryContainer query, SearchRequest request) { + CompositeActionListener( + ActionListener listener, + Client client, + SqlConfiguration cfg, + List output, + QueryContainer query, + SearchRequest request + ) { super(listener, client, cfg, output, query, request); isPivot = query.fields().stream().anyMatch(t -> t.v1() instanceof PivotColumnRef); @@ -368,19 +391,53 @@ static class CompositeActionListener extends BaseAggActionListener { @Override protected void handleResponse(SearchResponse response, ActionListener listener) { - Supplier makeRowSet = isPivot ? () -> new PivotRowSet(schema, initBucketExtractors(response), mask, - response, query.sortingColumns().isEmpty() ? query.limit() : -1, null) : () -> new SchemaCompositeAggRowSet(schema, - initBucketExtractors(response), mask, response, query.sortingColumns().isEmpty() ? query.limit() : -1); + Supplier makeRowSet = isPivot + ? () -> new PivotRowSet( + schema, + initBucketExtractors(response), + mask, + response, + query.sortingColumns().isEmpty() ? query.limit() : -1, + null + ) + : () -> new SchemaCompositeAggRowSet( + schema, + initBucketExtractors(response), + mask, + response, + query.sortingColumns().isEmpty() ? query.limit() : -1 + ); BiFunction makeCursor = isPivot ? (q, r) -> { Map lastAfterKey = r instanceof PivotRowSet ? ((PivotRowSet) r).lastAfterKey() : null; - return new PivotCursor(lastAfterKey, q, r.extractors(), r.mask(), r.remainingData(), query.shouldIncludeFrozen(), - request.indices()); - } : (q, r) -> new CompositeAggCursor(q, r.extractors(), r.mask(), r.remainingData, query.shouldIncludeFrozen(), - request.indices()); - - CompositeAggCursor.handle(response, request.source(), makeRowSet, makeCursor, () -> client.search(request, this), listener, - schema); + return new PivotCursor( + lastAfterKey, + q, + r.extractors(), + r.mask(), + r.remainingData(), + query.shouldIncludeFrozen(), + request.indices() + ); + } + : (q, r) -> new CompositeAggCursor( + q, + r.extractors(), + r.mask(), + r.remainingData, + query.shouldIncludeFrozen(), + request.indices() + ); + + CompositeAggCursor.handle( + response, + request.source(), + makeRowSet, + makeCursor, + () -> client.search(request, this), + listener, + schema + ); } } @@ -389,8 +446,14 @@ abstract static class BaseAggActionListener extends BaseActionListener { final SearchRequest request; final BitSet mask; - BaseAggActionListener(ActionListener listener, Client client, SqlConfiguration cfg, List output, - QueryContainer query, SearchRequest request) { + BaseAggActionListener( + ActionListener listener, + Client client, + SqlConfiguration cfg, + List output, + QueryContainer query, + SearchRequest request + ) { super(listener, client, cfg, output); this.query = query; @@ -459,8 +522,13 @@ static class ScrollActionListener extends BaseActionListener { private final BitSet mask; private final boolean multiValueFieldLeniency; - ScrollActionListener(ActionListener listener, Client client, SqlConfiguration cfg, List output, - QueryContainer query) { + ScrollActionListener( + ActionListener listener, + Client client, + SqlConfiguration cfg, + List output, + QueryContainer query + ) { super(listener, client, cfg, output); this.query = query; this.mask = query.columnMask(output); @@ -477,9 +545,13 @@ protected void handleResponse(SearchResponse response, ActionListener list exts.add(createExtractor(ref.v1())); } - ScrollCursor.handle(response, () -> new SchemaSearchHitRowSet(schema, exts, mask, query.limit(), response), - p -> listener.onResponse(p), - p -> clear(response.getScrollId(), wrap(success -> listener.onResponse(p), listener::onFailure)), schema); + ScrollCursor.handle( + response, + () -> new SchemaSearchHitRowSet(schema, exts, mask, query.limit(), response), + p -> listener.onResponse(p), + p -> clear(response.getScrollId(), wrap(success -> listener.onResponse(p), listener::onFailure)), + schema + ); } private HitExtractor createExtractor(FieldExtraction ref) { @@ -556,12 +628,13 @@ public void onResponse(final SearchResponse response) { // clean-up the scroll in case of exception protected final void cleanup(SearchResponse response, Exception ex) { if (response != null && response.getScrollId() != null) { - client.prepareClearScroll().addScrollId(response.getScrollId()) - // in case of failure, report the initial exception instead of the one resulting from cleaning the scroll - .execute(ActionListener.wrap(r -> delegate.onFailure(ex), e -> { - ex.addSuppressed(e); - delegate.onFailure(ex); - })); + client.prepareClearScroll() + .addScrollId(response.getScrollId()) + // in case of failure, report the initial exception instead of the one resulting from cleaning the scroll + .execute(ActionListener.wrap(r -> delegate.onFailure(ex), e -> { + ex.addSuppressed(e); + delegate.onFailure(ex); + })); } else { delegate.onFailure(ex); } @@ -569,8 +642,14 @@ protected final void cleanup(SearchResponse response, Exception ex) { protected final void clear(String scrollId, ActionListener listener) { if (scrollId != null) { - client.prepareClearScroll().addScrollId(scrollId).execute(ActionListener - .wrap(clearScrollResponse -> listener.onResponse(clearScrollResponse.isSucceeded()), listener::onFailure)); + client.prepareClearScroll() + .addScrollId(scrollId) + .execute( + ActionListener.wrap( + clearScrollResponse -> listener.onResponse(clearScrollResponse.isSucceeded()), + listener::onFailure + ) + ); } else { listener.onResponse(false); } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/ResultRowSet.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/ResultRowSet.java index c2c89256d3f19..ce475e746317e 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/ResultRowSet.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/ResultRowSet.java @@ -74,6 +74,5 @@ void forEachResultColumn(Consumer action) { } } - protected abstract Object extractValue(E e); } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/ScrollCursor.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/ScrollCursor.java index 3a32a54c99108..ea0f7c8370339 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/ScrollCursor.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/ScrollCursor.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.xpack.sql.execution.search; - import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.action.ActionListener; @@ -90,6 +89,7 @@ List extractors() { int limit() { return limit; } + @Override public void nextPage(SqlConfiguration cfg, Client client, NamedWriteableRegistry registry, ActionListener listener) { if (log.isTraceEnabled()) { @@ -98,22 +98,32 @@ public void nextPage(SqlConfiguration cfg, Client client, NamedWriteableRegistry SearchScrollRequest request = new SearchScrollRequest(scrollId).scroll(cfg.pageTimeout()); client.searchScroll(request, wrap(response -> { - handle(response, () -> new SearchHitRowSet(extractors, mask, limit, response), - p -> listener.onResponse(p), - p -> clear(client, wrap(success -> listener.onResponse(p), listener::onFailure)), - Schema.EMPTY); + handle( + response, + () -> new SearchHitRowSet(extractors, mask, limit, response), + p -> listener.onResponse(p), + p -> clear(client, wrap(success -> listener.onResponse(p), listener::onFailure)), + Schema.EMPTY + ); }, listener::onFailure)); } @Override public void clear(Client client, ActionListener listener) { - cleanCursor(client, scrollId, wrap( - clearScrollResponse -> listener.onResponse(clearScrollResponse.isSucceeded()), - listener::onFailure)); + cleanCursor( + client, + scrollId, + wrap(clearScrollResponse -> listener.onResponse(clearScrollResponse.isSucceeded()), listener::onFailure) + ); } - static void handle(SearchResponse response, Supplier makeRowHit, Consumer onPage, Consumer clearScroll, - Schema schema) { + static void handle( + SearchResponse response, + Supplier makeRowHit, + Consumer onPage, + Consumer clearScroll, + Schema schema + ) { if (log.isTraceEnabled()) { Querier.logSearchResponse(response, log); } @@ -144,8 +154,8 @@ public boolean equals(Object obj) { } ScrollCursor other = (ScrollCursor) obj; return Objects.equals(scrollId, other.scrollId) - && Objects.equals(extractors, other.extractors) - && Objects.equals(limit, other.limit); + && Objects.equals(extractors, other.extractors) + && Objects.equals(limit, other.limit); } @Override diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/SearchHitRowSet.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/SearchHitRowSet.java index 8e6f180e4dd87..2b453ff827df5 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/SearchHitRowSet.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/SearchHitRowSet.java @@ -46,9 +46,9 @@ class SearchHitRowSet extends ResultRowSet { this.hits = response.getHits().getHits(); - // Since the results might contain nested docs, the iteration is similar to that of Aggregation - // namely it discovers the nested docs and then, for iteration, increments the deepest level first - // and eventually carries that over to the top level + // Since the results might contain nested docs, the iteration is similar to that of Aggregation + // namely it discovers the nested docs and then, for iteration, increments the deepest level first + // and eventually carries that over to the top level String innerHit = null; for (HitExtractor ex : exts) { @@ -98,7 +98,7 @@ class SearchHitRowSet extends ResultRowSet { int remainingLimit = limit < 0 ? limit : limit - size; // if the computed limit is zero, or the size is zero it means either there's nothing left or the limit has been reached if (size == 0 || remainingLimit == 0 - // or the scroll has ended + // or the scroll has ended || totalHits != null && totalHits.value == hits.length) { nextScrollData = null; } else { @@ -117,7 +117,7 @@ protected Object extractValue(HitExtractor e) { SearchHit hit = null; SearchHit[] sh = hits; - for (int lvl = 0; lvl <= extractorLevel ; lvl++) { + for (int lvl = 0; lvl <= extractorLevel; lvl++) { // TODO: add support for multi-nested doc if (hit != null) { SearchHit[] innerHits = flatInnerHits.get(hit).get(innerHit); @@ -156,7 +156,7 @@ private SearchHit[] getAllInnerHits(SearchHit hit, String path) { } private class NestedHitOffsetComparator implements Comparator { - @Override + @Override public int compare(SearchHit sh1, SearchHit sh2) { if (sh1 == null && sh2 == null) { return 0; @@ -192,8 +192,7 @@ protected boolean doNext() { // restart the loop lvl = 0; sh = hits; - } - else { + } else { SearchHit h = sh[indexPerLevel[lvl]]; // TODO: improve this for multi-nested responses String path = lvl == 0 ? innerHit : null; diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/SourceGenerator.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/SourceGenerator.java index ab4da4c96be6f..28d8f85ad1d51 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/SourceGenerator.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/SourceGenerator.java @@ -115,14 +115,12 @@ private static void sorting(QueryContainer container, SearchSourceBuilder source if (attr instanceof FieldAttribute) { FieldAttribute fa = ((FieldAttribute) attr).exactAttribute(); - sortBuilder = fieldSort(fa.name()) - .missing(as.missing().searchOrder(as.direction())) - .unmappedType(fa.dataType().esType()); + sortBuilder = fieldSort(fa.name()).missing(as.missing().searchOrder(as.direction())) + .unmappedType(fa.dataType().esType()); if (fa.isNested()) { - FieldSortBuilder fieldSort = fieldSort(fa.name()) - .missing(as.missing().searchOrder(as.direction())) - .unmappedType(fa.dataType().esType()); + FieldSortBuilder fieldSort = fieldSort(fa.name()).missing(as.missing().searchOrder(as.direction())) + .unmappedType(fa.dataType().esType()); NestedSortBuilder newSort = new NestedSortBuilder(fa.nestedParent().name()); NestedSortBuilder nestedSort = fieldSort.getNestedSort(); @@ -146,8 +144,10 @@ private static void sorting(QueryContainer container, SearchSourceBuilder source } } else if (sortable instanceof ScriptSort) { ScriptSort ss = (ScriptSort) sortable; - sortBuilder = scriptSort(ss.script().toPainless(), - ss.script().outputType().isNumeric() ? ScriptSortType.NUMBER : ScriptSortType.STRING); + sortBuilder = scriptSort( + ss.script().toPainless(), + ss.script().outputType().isNumeric() ? ScriptSortType.NUMBER : ScriptSortType.STRING + ); } else if (sortable instanceof ScoreSort) { sortBuilder = scoreSort(); } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/extractor/CompositeKeyExtractor.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/extractor/CompositeKeyExtractor.java index 7b29feeb3ac5f..41f6345ccf34b 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/extractor/CompositeKeyExtractor.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/extractor/CompositeKeyExtractor.java @@ -122,9 +122,9 @@ public boolean equals(Object obj) { CompositeKeyExtractor other = (CompositeKeyExtractor) obj; return Objects.equals(key, other.key) - && Objects.equals(property, other.property) - && Objects.equals(zoneId, other.zoneId) - && Objects.equals(isDateTimeBased, other.isDateTimeBased); + && Objects.equals(property, other.property) + && Objects.equals(zoneId, other.zoneId) + && Objects.equals(isDateTimeBased, other.isDateTimeBased); } @Override diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/extractor/FieldHitExtractor.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/extractor/FieldHitExtractor.java index dce29c587a5d5..989aa2c9ded85 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/extractor/FieldHitExtractor.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/extractor/FieldHitExtractor.java @@ -19,6 +19,7 @@ import org.elasticsearch.xpack.sql.expression.literal.geo.GeoShape; import org.elasticsearch.xpack.sql.type.SqlDataTypes; import org.elasticsearch.xpack.sql.util.DateUtils; + import java.io.IOException; import java.time.ZoneId; import java.util.List; @@ -49,8 +50,7 @@ public FieldHitExtractor(String name, DataType dataType, ZoneId zoneId) { super(name, dataType, zoneId); } - public FieldHitExtractor(String name, DataType dataType, ZoneId zoneId, String hitName, - boolean arrayLeniency) { + public FieldHitExtractor(String name, DataType dataType, ZoneId zoneId, String hitName, boolean arrayLeniency) { super(name, dataType, zoneId, hitName, arrayLeniency); } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/extractor/MetricAggExtractor.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/extractor/MetricAggExtractor.java index ff647ced96927..926d986ebef5f 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/extractor/MetricAggExtractor.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/extractor/MetricAggExtractor.java @@ -6,9 +6,9 @@ */ package org.elasticsearch.xpack.sql.execution.search.extractor; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.core.Nullable; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket; import org.elasticsearch.search.aggregations.bucket.filter.InternalFilter; @@ -29,6 +29,7 @@ import org.elasticsearch.xpack.sql.querydsl.agg.Aggs; import org.elasticsearch.xpack.sql.type.SqlDataTypes; import org.elasticsearch.xpack.sql.util.DateUtils; + import java.io.IOException; import java.time.ZoneId; import java.util.Map; @@ -116,10 +117,10 @@ public Object extract(Bucket bucket) { } if (agg instanceof InternalNumericMetricsAggregation.MultiValue) { - //TODO: need to investigate when this can be not-null - //if (innerKey == null) { - // throw new SqlIllegalArgumentException("Invalid innerKey {} specified for aggregation {}", innerKey, name); - //} + // TODO: need to investigate when this can be not-null + // if (innerKey == null) { + // throw new SqlIllegalArgumentException("Invalid innerKey {} specified for aggregation {}", innerKey, name); + // } return handleTargetType(((InternalNumericMetricsAggregation.MultiValue) agg).value(property)); } else if (agg instanceof InternalFilter) { // COUNT(expr) and COUNT(ALL expr) uses this type of aggregation to account for non-null values only @@ -170,7 +171,7 @@ private static boolean containsValues(InternalAggregation agg) { return hasValue((InternalSum) agg); } if (agg instanceof InternalTDigestPercentileRanks) { - return hasValue((InternalTDigestPercentileRanks) agg); + return hasValue((InternalTDigestPercentileRanks) agg); } if (agg instanceof InternalTDigestPercentiles) { return hasValue((InternalTDigestPercentiles) agg); @@ -194,9 +195,7 @@ public boolean equals(Object obj) { } MetricAggExtractor other = (MetricAggExtractor) obj; - return Objects.equals(name, other.name) - && Objects.equals(property, other.property) - && Objects.equals(innerKey, other.innerKey); + return Objects.equals(name, other.name) && Objects.equals(property, other.property) && Objects.equals(innerKey, other.innerKey); } @Override diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/extractor/PivotExtractor.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/extractor/PivotExtractor.java index bdfcacf8a48c7..2fb1e9d6af2e3 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/extractor/PivotExtractor.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/extractor/PivotExtractor.java @@ -67,7 +67,7 @@ public boolean equals(Object obj) { } PivotExtractor other = (PivotExtractor) obj; return Objects.equals(groupExtractor, other.groupExtractor) - && Objects.equals(metricExtractor, other.metricExtractor) - && Objects.equals(value, other.value); + && Objects.equals(metricExtractor, other.metricExtractor) + && Objects.equals(value, other.value); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/extractor/TopHitsAggExtractor.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/extractor/TopHitsAggExtractor.java index 323a20177c626..16b6cb2c1525b 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/extractor/TopHitsAggExtractor.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/extractor/TopHitsAggExtractor.java @@ -16,6 +16,7 @@ import org.elasticsearch.xpack.sql.common.io.SqlStreamInput; import org.elasticsearch.xpack.sql.type.SqlDataTypes; import org.elasticsearch.xpack.sql.util.DateUtils; + import java.io.IOException; import java.time.ZoneId; import java.util.Objects; diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/SqlTypeResolutions.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/SqlTypeResolutions.java index 8ef14e2d6dfbc..8d30e6f29d08b 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/SqlTypeResolutions.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/SqlTypeResolutions.java @@ -31,13 +31,20 @@ public static TypeResolution isDateOrInterval(Expression e, String operationName } public static TypeResolution isNumericOrDate(Expression e, String operationName, ParamOrdinal paramOrd) { - return isType(e, dt -> dt.isNumeric() || SqlDataTypes.isDateBased(dt), operationName, paramOrd, - "date", "datetime", "numeric"); + return isType(e, dt -> dt.isNumeric() || SqlDataTypes.isDateBased(dt), operationName, paramOrd, "date", "datetime", "numeric"); } public static TypeResolution isNumericOrDateOrTime(Expression e, String operationName, ParamOrdinal paramOrd) { - return isType(e, dt -> dt.isNumeric() || SqlDataTypes.isDateOrTimeBased(dt), operationName, paramOrd, - "date", "time", "datetime", "numeric"); + return isType( + e, + dt -> dt.isNumeric() || SqlDataTypes.isDateOrTimeBased(dt), + operationName, + paramOrd, + "date", + "time", + "datetime", + "numeric" + ); } public static TypeResolution isGeo(Expression e, String operationName, ParamOrdinal paramOrd) { diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/SqlFunctionRegistry.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/SqlFunctionRegistry.java index 8dcfe8686ceee..46326e71a3a67 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/SqlFunctionRegistry.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/SqlFunctionRegistry.java @@ -149,19 +149,18 @@ public SqlFunctionRegistry() { } private FunctionDefinition[][] functions() { - return new FunctionDefinition[][]{ + return new FunctionDefinition[][] { // Aggregate functions - new FunctionDefinition[]{ + new FunctionDefinition[] { def(Avg.class, Avg::new, "AVG"), def(Count.class, Count::new, "COUNT"), def(First.class, First::new, "FIRST", "FIRST_VALUE"), def(Last.class, Last::new, "LAST", "LAST_VALUE"), def(Max.class, Max::new, "MAX"), def(Min.class, Min::new, "MIN"), - def(Sum.class, Sum::new, "SUM") - }, + def(Sum.class, Sum::new, "SUM") }, // Statistics - new FunctionDefinition[]{ + new FunctionDefinition[] { def(Kurtosis.class, Kurtosis::new, "KURTOSIS"), def(MedianAbsoluteDeviation.class, MedianAbsoluteDeviation::new, "MAD"), def(Percentile.class, Percentile::new, "PERCENTILE"), @@ -171,25 +170,21 @@ private FunctionDefinition[][] functions() { def(StddevSamp.class, StddevSamp::new, "STDDEV_SAMP"), def(SumOfSquares.class, SumOfSquares::new, "SUM_OF_SQUARES"), def(VarPop.class, VarPop::new, "VAR_POP"), - def(VarSamp.class, VarSamp::new, "VAR_SAMP") - }, + def(VarSamp.class, VarSamp::new, "VAR_SAMP") }, // histogram - new FunctionDefinition[]{ - def(Histogram.class, Histogram::new, "HISTOGRAM") - }, + new FunctionDefinition[] { def(Histogram.class, Histogram::new, "HISTOGRAM") }, // Scalar functions // Conditional - new FunctionDefinition[]{ + new FunctionDefinition[] { def(Case.class, Case::new, "CASE"), def(Coalesce.class, Coalesce::new, "COALESCE"), def(Iif.class, Iif::new, "IIF"), def(IfNull.class, (BinaryBuilder) IfNull::new, "IFNULL", "ISNULL", "NVL"), def(NullIf.class, NullIf::new, "NULLIF"), def(Greatest.class, Greatest::new, "GREATEST"), - def(Least.class, Least::new, "LEAST") - }, + def(Least.class, Least::new, "LEAST") }, // Date - new FunctionDefinition[]{ + new FunctionDefinition[] { def(CurrentDate.class, CurrentDate::new, "CURRENT_DATE", "CURDATE", "TODAY"), def(CurrentTime.class, CurrentTime::new, "CURRENT_TIME", "CURTIME"), def(CurrentDateTime.class, CurrentDateTime::new, "CURRENT_TIMESTAMP", "NOW"), @@ -217,10 +212,9 @@ private FunctionDefinition[][] functions() { def(TimeParse.class, TimeParse::new, "TIME_PARSE"), def(Quarter.class, Quarter::new, "QUARTER"), def(Year.class, Year::new, "YEAR"), - def(WeekOfYear.class, WeekOfYear::new, "WEEK_OF_YEAR", "WEEK") - }, + def(WeekOfYear.class, WeekOfYear::new, "WEEK_OF_YEAR", "WEEK") }, // Math - new FunctionDefinition[]{ + new FunctionDefinition[] { def(Abs.class, Abs::new, "ABS"), def(ACos.class, ACos::new, "ACOS"), def(ASin.class, ASin::new, "ASIN"), @@ -250,10 +244,9 @@ private FunctionDefinition[][] functions() { def(Sinh.class, Sinh::new, "SINH"), def(Sqrt.class, Sqrt::new, "SQRT"), def(Tan.class, Tan::new, "TAN"), - def(Truncate.class, Truncate::new, "TRUNCATE", "TRUNC") - }, + def(Truncate.class, Truncate::new, "TRUNCATE", "TRUNC") }, // String - new FunctionDefinition[]{ + new FunctionDefinition[] { def(Ascii.class, Ascii::new, "ASCII"), def(BitLength.class, BitLength::new, "BIT_LENGTH"), def(Char.class, Char::new, "CHAR"), @@ -275,32 +268,22 @@ private FunctionDefinition[][] functions() { def(StartsWith.class, StartsWith::new, "STARTS_WITH"), def(Substring.class, Substring::new, "SUBSTRING"), def(Trim.class, Trim::new, "TRIM"), - def(UCase.class, UCase::new, "UCASE") - }, + def(UCase.class, UCase::new, "UCASE") }, // DataType conversion - new FunctionDefinition[]{ - def(Cast.class, Cast::new, "CAST", "CONVERT") - }, + new FunctionDefinition[] { def(Cast.class, Cast::new, "CAST", "CONVERT") }, // Scalar "meta" functions - new FunctionDefinition[]{ - def(Database.class, Database::new, "DATABASE"), - def(User.class, User::new, "USER") - }, + new FunctionDefinition[] { def(Database.class, Database::new, "DATABASE"), def(User.class, User::new, "USER") }, // Geo Functions - new FunctionDefinition[]{ + new FunctionDefinition[] { def(StAswkt.class, StAswkt::new, "ST_ASWKT", "ST_ASTEXT"), def(StDistance.class, StDistance::new, "ST_DISTANCE"), def(StWkttosql.class, StWkttosql::new, "ST_WKTTOSQL", "ST_GEOMFROMTEXT"), def(StGeometryType.class, StGeometryType::new, "ST_GEOMETRYTYPE"), def(StX.class, StX::new, "ST_X"), def(StY.class, StY::new, "ST_Y"), - def(StZ.class, StZ::new, "ST_Z") - }, + def(StZ.class, StZ::new, "ST_Z") }, // Special - new FunctionDefinition[]{ - def(Score.class, Score::new, "SCORE") - } - }; + new FunctionDefinition[] { def(Score.class, Score::new, "SCORE") } }; } /** @@ -315,10 +298,12 @@ protected interface SqlFunctionBuilder { * Main method to register a function. */ @SuppressWarnings("overloads") - protected static FunctionDefinition def(Class function, - SqlFunctionBuilder builder, - boolean datetime, - String... names) { + protected static FunctionDefinition def( + Class function, + SqlFunctionBuilder builder, + boolean datetime, + String... names + ) { Check.isTrue(names.length > 0, "At least one name must be provided for the function"); String primaryName = names[0]; List aliases = Arrays.asList(names).subList(1, names.length); @@ -392,9 +377,7 @@ protected interface BinaryZoneIdAwareBuilder { * Build a {@linkplain FunctionDefinition} for a three-args function that requires a timezone. */ @SuppressWarnings("overloads") // These are ambiguous if you aren't using ctor references but we always do - protected static FunctionDefinition def(Class function, - TernaryZoneIdAwareBuilder ctorRef, - String... names) { + protected static FunctionDefinition def(Class function, TernaryZoneIdAwareBuilder ctorRef, String... names) { SqlFunctionBuilder builder = (source, children, cfg, distinct) -> { if (children.size() != 3) { throw new QlIllegalArgumentException("expects three arguments"); @@ -409,7 +392,6 @@ protected interface TernaryZoneIdAwareBuilder { T build(Source source, Expression first, Expression second, Expression third, ZoneId zi); } - /** * Special method to create function definition for Cast as its signature is not compatible with {@link UnresolvedFunction}. */ diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/aggregate/PercentileAggregate.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/aggregate/PercentileAggregate.java index ed09bfa600f24..642785e606fa5 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/aggregate/PercentileAggregate.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/aggregate/PercentileAggregate.java @@ -43,16 +43,13 @@ abstract class PercentileAggregate extends NumericAggregate implements EnclosedA // contains all the possible PercentilesMethods that we know of and are capable of parameterizing at the moment private static final Map METHOD_CONFIGURATORS = new LinkedHashMap<>(); static { - Arrays.asList( - new MethodConfigurator(PercentilesMethod.TDIGEST, TypeResolutions::isNumeric, methodParameter -> { - Double compression = foldNullSafe(methodParameter, DataTypes.DOUBLE); - return compression == null ? new PercentilesConfig.TDigest() : new PercentilesConfig.TDigest(compression); - }), - new MethodConfigurator(PercentilesMethod.HDR, TypeResolutions::isInteger, methodParameter -> { - Integer numOfDigits = foldNullSafe(methodParameter, DataTypes.INTEGER); - return numOfDigits == null ? new PercentilesConfig.Hdr() : new PercentilesConfig.Hdr(numOfDigits); - })) - .forEach(c -> METHOD_CONFIGURATORS.put(c.method.getParseField().getPreferredName(), c)); + Arrays.asList(new MethodConfigurator(PercentilesMethod.TDIGEST, TypeResolutions::isNumeric, methodParameter -> { + Double compression = foldNullSafe(methodParameter, DataTypes.DOUBLE); + return compression == null ? new PercentilesConfig.TDigest() : new PercentilesConfig.TDigest(compression); + }), new MethodConfigurator(PercentilesMethod.HDR, TypeResolutions::isInteger, methodParameter -> { + Integer numOfDigits = foldNullSafe(methodParameter, DataTypes.INTEGER); + return numOfDigits == null ? new PercentilesConfig.Hdr() : new PercentilesConfig.Hdr(numOfDigits); + })).forEach(c -> METHOD_CONFIGURATORS.put(c.method.getParseField().getPreferredName(), c)); } private static class MethodConfigurator { @@ -67,7 +64,10 @@ private interface MethodParameterResolver { private final Function parameterToConfig; MethodConfigurator( - PercentilesMethod method, MethodParameterResolver resolver, Function parameterToConfig) { + PercentilesMethod method, + MethodParameterResolver resolver, + Function parameterToConfig + ) { this.method = method; this.resolver = resolver; this.parameterToConfig = parameterToConfig; @@ -79,8 +79,7 @@ private interface MethodParameterResolver { private final Expression method; private final Expression methodParameter; - PercentileAggregate(Source source, Expression field, Expression parameter, Expression method, Expression methodParameter) - { + PercentileAggregate(Source source, Expression field, Expression parameter, Expression method, Expression methodParameter) { super(source, field, singletonList(parameter)); this.parameter = parameter; this.method = method; @@ -121,9 +120,16 @@ protected TypeResolution resolveType() { MethodConfigurator methodConfigurator = METHOD_CONFIGURATORS.get(methodName); if (methodConfigurator == null) { - return new TypeResolution(format(null, "{}argument of [{}] must be one of {}, received [{}]", - methodOrdinal.name().toLowerCase(Locale.ROOT) + " ", sourceText(), - METHOD_CONFIGURATORS.keySet(), methodName)); + return new TypeResolution( + format( + null, + "{}argument of [{}] must be one of {}, received [{}]", + methodOrdinal.name().toLowerCase(Locale.ROOT) + " ", + sourceText(), + METHOD_CONFIGURATORS.keySet(), + methodName + ) + ); } // if method is null, the method parameter is not checked @@ -199,7 +205,6 @@ public boolean equals(Object o) { PercentileAggregate that = (PercentileAggregate) o; - return Objects.equals(method, that.method) - && Objects.equals(methodParameter, that.methodParameter); + return Objects.equals(method, that.method) && Objects.equals(methodParameter, that.methodParameter); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/aggregate/PercentileCompoundAggregate.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/aggregate/PercentileCompoundAggregate.java index 0f67985694009..f7a27e006afff 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/aggregate/PercentileCompoundAggregate.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/aggregate/PercentileCompoundAggregate.java @@ -17,8 +17,7 @@ public abstract class PercentileCompoundAggregate extends CompoundNumericAggregate { protected final PercentilesConfig percentilesConfig; - public PercentileCompoundAggregate( - Source source, Expression field, List arguments, PercentilesConfig percentilesConfig) { + public PercentileCompoundAggregate(Source source, Expression field, List arguments, PercentilesConfig percentilesConfig) { super(source, field, arguments); this.percentilesConfig = percentilesConfig; } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/grouping/Histogram.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/grouping/Histogram.java index 4df65e8575321..5f9cd8443d50b 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/grouping/Histogram.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/grouping/Histogram.java @@ -55,13 +55,7 @@ protected TypeResolution resolveType() { if (resolution == TypeResolution.TYPE_RESOLVED) { // interval must be Literal interval if (SqlDataTypes.isDateBased(field().dataType())) { - resolution = isType( - interval, - SqlDataTypes::isInterval, - "(Date) HISTOGRAM", - SECOND, - "interval" - ); + resolution = isType(interval, SqlDataTypes::isInterval, "(Date) HISTOGRAM", SECOND, "interval"); } else { resolution = isNumeric(interval, "(Numeric) HISTOGRAM", SECOND); } @@ -94,8 +88,7 @@ public int hashCode() { public boolean equals(Object obj) { if (super.equals(obj)) { Histogram other = (Histogram) obj; - return Objects.equals(interval, other.interval) - && Objects.equals(zoneId, other.zoneId); + return Objects.equals(interval, other.interval) && Objects.equals(zoneId, other.zoneId); } return false; } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/Cast.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/Cast.java index 629f3ed399b6e..f6b27aac96e21 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/Cast.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/Cast.java @@ -71,9 +71,9 @@ public Nullability nullable() { @Override protected TypeResolution resolveType() { - return SqlDataTypeConverter.canConvert(from(), to()) ? - TypeResolution.TYPE_RESOLVED : - new TypeResolution("Cannot cast [" + from() + "] to [" + to()+ "]"); + return SqlDataTypeConverter.canConvert(from(), to()) + ? TypeResolution.TYPE_RESOLVED + : new TypeResolution("Cannot cast [" + from() + "] to [" + to() + "]"); } @Override @@ -85,12 +85,10 @@ protected Processor makeProcessor() { public ScriptTemplate asScript() { ScriptTemplate fieldAsScript = asScript(field()); return new ScriptTemplate( - formatTemplate(format("{sql}.", "cast({},{})", fieldAsScript.template())), - paramsBuilder() - .script(fieldAsScript.params()) - .variable(dataType.name()) - .build(), - dataType()); + formatTemplate(format("{sql}.", "cast({},{})", fieldAsScript.template())), + paramsBuilder().script(fieldAsScript.params()).variable(dataType.name()).build(), + dataType() + ); } @Override @@ -107,7 +105,6 @@ public boolean equals(Object obj) { return false; } Cast other = (Cast) obj; - return Objects.equals(dataType, other.dataType()) - && Objects.equals(field(), other.field()); + return Objects.equals(dataType, other.dataType()) && Objects.equals(field(), other.field()); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/BaseDateTimeFunction.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/BaseDateTimeFunction.java index e2f46b45c8c3b..a568589ef1615 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/BaseDateTimeFunction.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/BaseDateTimeFunction.java @@ -64,7 +64,6 @@ public boolean equals(Object obj) { return false; } BaseDateTimeFunction other = (BaseDateTimeFunction) obj; - return Objects.equals(other.field(), field()) - && Objects.equals(other.zoneId(), zoneId()); + return Objects.equals(other.field(), field()) && Objects.equals(other.zoneId(), zoneId()); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/BinaryDateTimeFunction.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/BinaryDateTimeFunction.java index 20a6d0493e2be..5e952f7a119e0 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/BinaryDateTimeFunction.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/BinaryDateTimeFunction.java @@ -44,14 +44,10 @@ protected Pipe makePipe() { @Override protected ScriptTemplate asScriptFrom(ScriptTemplate leftScript, ScriptTemplate rightScript) { return new ScriptTemplate( - formatTemplate("{sql}." + scriptMethodName() + - "(" + leftScript.template() + "," + rightScript.template()+ ",{})"), - paramsBuilder() - .script(leftScript.params()) - .script(rightScript.params()) - .variable(zoneId.getId()) - .build(), - dataType()); + formatTemplate("{sql}." + scriptMethodName() + "(" + leftScript.template() + "," + rightScript.template() + ",{})"), + paramsBuilder().script(leftScript.params()).script(rightScript.params()).variable(zoneId.getId()).build(), + dataType() + ); } @Override diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/BinaryDateTimeProcessor.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/BinaryDateTimeProcessor.java index c9289a8ad4917..816c9c113a01d 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/BinaryDateTimeProcessor.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/BinaryDateTimeProcessor.java @@ -31,8 +31,7 @@ public BinaryDateTimeProcessor(StreamInput in) throws IOException { } @Override - protected void doWrite(StreamOutput out) throws IOException { - } + protected void doWrite(StreamOutput out) throws IOException {} ZoneId zoneId() { return zoneId; @@ -57,8 +56,6 @@ public boolean equals(Object obj) { } BinaryDateTimeProcessor other = (BinaryDateTimeProcessor) obj; - return Objects.equals(left(), other.left()) - && Objects.equals(right(), other.right()) - && Objects.equals(zoneId(), other.zoneId()); + return Objects.equals(left(), other.left()) && Objects.equals(right(), other.right()) && Objects.equals(zoneId(), other.zoneId()); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/CurrentTime.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/CurrentTime.java index a5021a9ab9e21..434f8984faa34 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/CurrentTime.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/CurrentTime.java @@ -22,8 +22,7 @@ public class CurrentTime extends CurrentFunction { private final Expression precision; public CurrentTime(Source source, Expression precision, Configuration configuration) { - super(source, configuration, nanoPrecision(configuration.now().toOffsetDateTime().toOffsetTime(), precision), - SqlDataTypes.TIME); + super(source, configuration, nanoPrecision(configuration.now().toOffsetDateTime().toOffsetTime(), precision), SqlDataTypes.TIME); this.precision = precision; } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateAdd.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateAdd.java index a3efb09aac7d9..f255ea61f89c7 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateAdd.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateAdd.java @@ -39,8 +39,8 @@ public enum Part implements DateTimeField { DAYOFYEAR((dt, i) -> dt.plus(i, ChronoUnit.DAYS), "dy", "y"), DAY((dt, i) -> dt.plus(i, ChronoUnit.DAYS), "days", "dd", "d"), WEEK((dt, i) -> dt.plus(i, ChronoUnit.WEEKS), "weeks", "wk", "ww"), - WEEKDAY((dt, i) -> dt.plus(i, ChronoUnit.DAYS), "weekdays", "dw"), - HOUR((dt, i) -> dt.plus(i, ChronoUnit.HOURS), "hours", "hh"), + WEEKDAY((dt, i) -> dt.plus(i, ChronoUnit.DAYS), "weekdays", "dw"), + HOUR((dt, i) -> dt.plus(i, ChronoUnit.HOURS), "hours", "hh"), MINUTE((dt, i) -> dt.plus(i, ChronoUnit.MINUTES), "minutes", "mi", "n"), SECOND((dt, i) -> dt.plus(i, ChronoUnit.SECONDS), "seconds", "ss", "s"), MILLISECOND((dt, i) -> dt.plus(i, ChronoUnit.MILLIS), "milliseconds", "ms"), @@ -97,15 +97,25 @@ protected TypeResolution resolveType() { if (datePartValue != null && resolveDateTimeField(datePartValue) == false) { List similar = findSimilarDateTimeFields(datePartValue); if (similar.isEmpty()) { - return new TypeResolution(format(null, "first argument of [{}] must be one of {} or their aliases; found value [{}]", - sourceText(), - validDateTimeFieldValues(), - Expressions.name(first()))); + return new TypeResolution( + format( + null, + "first argument of [{}] must be one of {} or their aliases; found value [{}]", + sourceText(), + validDateTimeFieldValues(), + Expressions.name(first()) + ) + ); } else { - return new TypeResolution(format(null, "Unknown value [{}] for first argument of [{}]; did you mean {}?", - Expressions.name(first()), - sourceText(), - similar)); + return new TypeResolution( + format( + null, + "Unknown value [{}] for first argument of [{}]; did you mean {}?", + Expressions.name(first()), + sourceText(), + similar + ) + ); } } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateAddProcessor.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateAddProcessor.java index 989ed2e584f5f..54bdbeb0b2441 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateAddProcessor.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateAddProcessor.java @@ -52,11 +52,13 @@ public static Object process(Object unit, Object numberOfUnits, Object timestamp if (datePartField == null) { List similar = Part.findSimilar((String) unit); if (similar.isEmpty()) { - throw new SqlIllegalArgumentException("A value of {} or their aliases is required; received [{}]", - Part.values(), unit); + throw new SqlIllegalArgumentException("A value of {} or their aliases is required; received [{}]", Part.values(), unit); } else { - throw new SqlIllegalArgumentException("Received value [{}] is not valid date part to add; " + - "did you mean {}?", unit, similar); + throw new SqlIllegalArgumentException( + "Received value [{}] is not valid date part to add; " + "did you mean {}?", + unit, + similar + ); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateDiff.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateDiff.java index 75a032c60c17e..2ceca156af458 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateDiff.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateDiff.java @@ -37,30 +37,36 @@ public class DateDiff extends ThreeArgsDateTimeFunction { public enum Part implements DateTimeField { - YEAR((start, end) -> end.getYear() - start.getYear(), "years", "yyyy", "yy"), - QUARTER((start, end) -> QuarterProcessor.quarter(end) - QuarterProcessor.quarter(start) + (YEAR.diff(start, end) * 4), - "quarters", "qq", "q"), - MONTH((start, end) -> safeInt(end.getLong(ChronoField.PROLEPTIC_MONTH) - start.getLong(ChronoField.PROLEPTIC_MONTH)), - "months", "mm", "m"), + YEAR((start, end) -> end.getYear() - start.getYear(), "years", "yyyy", "yy"), + QUARTER( + (start, end) -> QuarterProcessor.quarter(end) - QuarterProcessor.quarter(start) + (YEAR.diff(start, end) * 4), + "quarters", + "qq", + "q" + ), + MONTH( + (start, end) -> safeInt(end.getLong(ChronoField.PROLEPTIC_MONTH) - start.getLong(ChronoField.PROLEPTIC_MONTH)), + "months", + "mm", + "m" + ), DAYOFYEAR((start, end) -> safeInt(diffInDays(start, end)), "dy", "y"), DAY(DAYOFYEAR::diff, "days", "dd", "d"), WEEK((start, end) -> { - long startInDays = start.toInstant().toEpochMilli() / DAY_IN_MILLIS - - DatePart.Part.WEEKDAY.extract(start.withZoneSameInstant(UTC)); - long endInDays = end.toInstant().toEpochMilli() / DAY_IN_MILLIS - - DatePart.Part.WEEKDAY.extract(end.withZoneSameInstant(UTC)); + long startInDays = start.toInstant().toEpochMilli() / DAY_IN_MILLIS - DatePart.Part.WEEKDAY.extract( + start.withZoneSameInstant(UTC) + ); + long endInDays = end.toInstant().toEpochMilli() / DAY_IN_MILLIS - DatePart.Part.WEEKDAY.extract(end.withZoneSameInstant(UTC)); return safeInt((endInDays - startInDays) / 7); }, "weeks", "wk", "ww"), - WEEKDAY(DAYOFYEAR::diff, "weekdays", "dw"), - HOUR((start, end) -> safeInt(diffInHours(start, end)), "hours", "hh"), + WEEKDAY(DAYOFYEAR::diff, "weekdays", "dw"), + HOUR((start, end) -> safeInt(diffInHours(start, end)), "hours", "hh"), MINUTE((start, end) -> safeInt(diffInMinutes(start, end)), "minutes", "mi", "n"), SECOND((start, end) -> safeInt(end.toEpochSecond() - start.toEpochSecond()), "seconds", "ss", "s"), - MILLISECOND((start, end) -> safeInt(end.toInstant().toEpochMilli() - start.toInstant().toEpochMilli()), - "milliseconds", "ms"), + MILLISECOND((start, end) -> safeInt(end.toInstant().toEpochMilli() - start.toInstant().toEpochMilli()), "milliseconds", "ms"), MICROSECOND((start, end) -> { long secondsDiff = diffInSeconds(start, end); - long microsDiff = end.toInstant().getLong(ChronoField.MICRO_OF_SECOND) - - start.toInstant().getLong(ChronoField.MICRO_OF_SECOND); + long microsDiff = end.toInstant().getLong(ChronoField.MICRO_OF_SECOND) - start.toInstant().getLong(ChronoField.MICRO_OF_SECOND); return safeInt(secondsDiff * 1_000_000L + microsDiff); }, "microseconds", "mcs"), NANOSECOND((start, end) -> { @@ -108,8 +114,10 @@ private static long diffInSeconds(ZonedDateTime start, ZonedDateTime end) { private static int safeInt(long diff) { if (diff > Integer.MAX_VALUE || diff < Integer.MIN_VALUE) { - throw new SqlIllegalArgumentException("The DATE_DIFF function resulted in an overflow; the number of units " + - "separating two date/datetime instances is too large. Try to use DATE_DIFF with a less precise unit."); + throw new SqlIllegalArgumentException( + "The DATE_DIFF function resulted in an overflow; the number of units " + + "separating two date/datetime instances is too large. Try to use DATE_DIFF with a less precise unit." + ); } else { return Long.valueOf(diff).intValue(); } @@ -145,15 +153,25 @@ protected TypeResolution resolveType() { if (datePartValue != null && resolveDateTimeField(datePartValue) == false) { List similar = findSimilarDateTimeFields(datePartValue); if (similar.isEmpty()) { - return new TypeResolution(format(null, "first argument of [{}] must be one of {} or their aliases; found value [{}]", - sourceText(), - validDateTimeFieldValues(), - Expressions.name(first()))); + return new TypeResolution( + format( + null, + "first argument of [{}] must be one of {} or their aliases; found value [{}]", + sourceText(), + validDateTimeFieldValues(), + Expressions.name(first()) + ) + ); } else { - return new TypeResolution(format(null, "Unknown value [{}] for first argument of [{}]; did you mean {}?", - Expressions.name(first()), - sourceText(), - similar)); + return new TypeResolution( + format( + null, + "Unknown value [{}] for first argument of [{}]; did you mean {}?", + Expressions.name(first()), + sourceText(), + similar + ) + ); } } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateDiffProcessor.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateDiffProcessor.java index 7faf3c2762163..225bcecacd183 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateDiffProcessor.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateDiffProcessor.java @@ -52,11 +52,13 @@ public static Object process(Object unit, Object startTimestamp, Object endTimes if (datePartField == null) { List similar = Part.findSimilar((String) unit); if (similar.isEmpty()) { - throw new SqlIllegalArgumentException("A value of {} or their aliases is required; received [{}]", - Part.values(), unit); + throw new SqlIllegalArgumentException("A value of {} or their aliases is required; received [{}]", Part.values(), unit); } else { - throw new SqlIllegalArgumentException("Received value [{}] is not valid date part to add; " + - "did you mean {}?", unit, similar); + throw new SqlIllegalArgumentException( + "Received value [{}] is not valid date part to add; " + "did you mean {}?", + unit, + similar + ); } } @@ -68,7 +70,9 @@ public static Object process(Object unit, Object startTimestamp, Object endTimes throw new SqlIllegalArgumentException("A date/datetime is required; received [{}]", endTimestamp); } - return datePartField.diff(((ZonedDateTime) startTimestamp).withZoneSameInstant(zoneId), - ((ZonedDateTime) endTimestamp).withZoneSameInstant(zoneId)); + return datePartField.diff( + ((ZonedDateTime) startTimestamp).withZoneSameInstant(zoneId), + ((ZonedDateTime) endTimestamp).withZoneSameInstant(zoneId) + ); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DatePartProcessor.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DatePartProcessor.java index df57803dee7b7..c9ef6431369e4 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DatePartProcessor.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DatePartProcessor.java @@ -52,11 +52,13 @@ public static Object process(Object part, Object timestamp, ZoneId zoneId) { if (datePartField == null) { List similar = Part.findSimilar((String) part); if (similar.isEmpty()) { - throw new SqlIllegalArgumentException("A value of {} or their aliases is required; received [{}]", - Part.values(), part); + throw new SqlIllegalArgumentException("A value of {} or their aliases is required; received [{}]", Part.values(), part); } else { - throw new SqlIllegalArgumentException("Received value [{}] is not valid date part for extraction; " + - "did you mean {}?", part, similar); + throw new SqlIllegalArgumentException( + "Received value [{}] is not valid date part for extraction; " + "did you mean {}?", + part, + similar + ); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeFormatProcessor.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeFormatProcessor.java index 2ad0be3c2f783..689b109af4901 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeFormatProcessor.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeFormatProcessor.java @@ -28,16 +28,15 @@ public class DateTimeFormatProcessor extends BinaryDateTimeProcessor { public static final String NAME = "dtformat"; private static final String[][] JAVA_TIME_FORMAT_REPLACEMENTS = { - {"tt", "a"}, - {"t", "a"}, - {"dddd", "eeee"}, - {"ddd", "eee"}, - {"K", "v"}, - {"g", "G"}, - {"f", "S"}, - {"F", "S"}, - {"z", "X"} - }; + { "tt", "a" }, + { "t", "a" }, + { "dddd", "eeee" }, + { "ddd", "eee" }, + { "K", "v" }, + { "g", "G" }, + { "f", "S" }, + { "F", "S" }, + { "z", "X" } }; private final Formatter formatter; diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeFunction.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeFunction.java index 7799e5705c09e..8d3ecb9559298 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeFunction.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeFunction.java @@ -34,9 +34,7 @@ public ScriptTemplate asScript() { ScriptTemplate script = super.asScript(); String template = formatTemplate("{sql}.dateTimeExtract(" + script.template() + ", {}, {})"); - params.script(script.params()) - .variable(zoneId().getId()) - .variable(extractor.name()); + params.script(script.params()).variable(zoneId().getId()).variable(extractor.name()); return new ScriptTemplate(template, params.build(), dataType()); } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeParse.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeParse.java index 9f1f383396d4a..68ee72ff71f30 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeParse.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeParse.java @@ -15,8 +15,8 @@ import java.time.ZoneId; -import static org.elasticsearch.xpack.sql.expression.function.scalar.datetime.DateTimeParseProcessor.Parser.DATE_TIME; import static org.elasticsearch.xpack.sql.expression.function.scalar.datetime.DateTimeParseProcessor.Parser; +import static org.elasticsearch.xpack.sql.expression.function.scalar.datetime.DateTimeParseProcessor.Parser.DATE_TIME; public class DateTimeParse extends BaseDateTimeParseFunction { diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeParsePipe.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeParsePipe.java index 160af76ce5620..a984cbb8426e2 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeParsePipe.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeParsePipe.java @@ -9,9 +9,9 @@ import org.elasticsearch.xpack.ql.expression.Expression; import org.elasticsearch.xpack.ql.expression.gen.pipeline.Pipe; import org.elasticsearch.xpack.ql.expression.gen.processor.Processor; -import org.elasticsearch.xpack.sql.expression.function.scalar.datetime.DateTimeParseProcessor.Parser; import org.elasticsearch.xpack.ql.tree.NodeInfo; import org.elasticsearch.xpack.ql.tree.Source; +import org.elasticsearch.xpack.sql.expression.function.scalar.datetime.DateTimeParseProcessor.Parser; import java.time.ZoneId; import java.util.Objects; diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeParseProcessor.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeParseProcessor.java index 5e5ec40cdb18e..0497261c47fdd 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeParseProcessor.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeParseProcessor.java @@ -37,7 +37,7 @@ public class DateTimeParseProcessor extends BinaryDateTimeProcessor { public enum Parser { DATE_TIME(DataTypes.DATETIME, ZonedDateTime::from, LocalDateTime::from), TIME(SqlDataTypes.TIME, OffsetTime::from, LocalTime::from), - DATE(SqlDataTypes.DATE, LocalDate::from, (TemporalAccessor ta) -> {throw new DateTimeException("InvalidDate");}); + DATE(SqlDataTypes.DATE, LocalDate::from, (TemporalAccessor ta) -> { throw new DateTimeException("InvalidDate"); }); private final BiFunction parser; @@ -45,8 +45,7 @@ public enum Parser { Parser(DataType parseType, TemporalQuery... queries) { this.parseType = parseType.typeName(); - this.parser = (timestampStr, pattern) -> DateTimeFormatter.ofPattern(pattern, Locale.ROOT) - .parseBest(timestampStr, queries); + this.parser = (timestampStr, pattern) -> DateTimeFormatter.ofPattern(pattern, Locale.ROOT).parseBest(timestampStr, queries); } public Object parse(Object timestamp, Object pattern, ZoneId zoneId) { diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeProcessor.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeProcessor.java index b969c37b467cc..f0f66bfbdbad6 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeProcessor.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeProcessor.java @@ -37,7 +37,7 @@ public enum DateTimeExtractor { DateTimeExtractor(TemporalField field) { this.field = field; } - + public int extract(Temporal time) { return time.get(field); } @@ -95,8 +95,7 @@ public boolean equals(Object obj) { return false; } DateTimeProcessor other = (DateTimeProcessor) obj; - return Objects.equals(extractor, other.extractor) - && Objects.equals(zoneId(), other.zoneId()); + return Objects.equals(extractor, other.extractor) && Objects.equals(zoneId(), other.zoneId()); } @Override diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTrunc.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTrunc.java index 07c50185eef46..156dde6130707 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTrunc.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTrunc.java @@ -15,6 +15,7 @@ import org.elasticsearch.xpack.ql.type.DataTypes; import org.elasticsearch.xpack.sql.expression.literal.interval.IntervalDayTime; import org.elasticsearch.xpack.sql.expression.literal.interval.IntervalYearMonth; + import java.time.Duration; import java.time.Period; import java.time.ZoneId; @@ -39,125 +40,124 @@ public class DateTrunc extends BinaryDateTimeDatePartFunction { public enum Part implements DateTimeField { MILLENNIUM(dt -> { - int year = dt.getYear(); - int firstYearOfMillennium = year - (year % 1000); - return dt - .with(ChronoField.YEAR, firstYearOfMillennium) - .with(ChronoField.MONTH_OF_YEAR, 1) - .with(ChronoField.DAY_OF_MONTH, 1) - .toLocalDate().atStartOfDay(dt.getZone()); - }, - idt -> new IntervalDayTime(Duration.ZERO, idt.dataType()), - iym -> { - Period period = iym.interval(); - int year = period.getYears(); - int firstYearOfMillennium = year - (year % 1000); - return new IntervalYearMonth(Period.ZERO.plusYears(firstYearOfMillennium), iym.dataType()); - }, "millennia"), + int year = dt.getYear(); + int firstYearOfMillennium = year - (year % 1000); + return dt.with(ChronoField.YEAR, firstYearOfMillennium) + .with(ChronoField.MONTH_OF_YEAR, 1) + .with(ChronoField.DAY_OF_MONTH, 1) + .toLocalDate() + .atStartOfDay(dt.getZone()); + }, idt -> new IntervalDayTime(Duration.ZERO, idt.dataType()), iym -> { + Period period = iym.interval(); + int year = period.getYears(); + int firstYearOfMillennium = year - (year % 1000); + return new IntervalYearMonth(Period.ZERO.plusYears(firstYearOfMillennium), iym.dataType()); + }, "millennia"), CENTURY(dt -> { - int year = dt.getYear(); - int firstYearOfCentury = year - (year % 100); - return dt - .with(ChronoField.YEAR, firstYearOfCentury) - .with(ChronoField.MONTH_OF_YEAR, 1) - .with(ChronoField.DAY_OF_MONTH, 1) - .toLocalDate().atStartOfDay(dt.getZone()); - }, - idt -> new IntervalDayTime(Duration.ZERO, idt.dataType()), - iym -> { - Period period = iym.interval(); - int year = period.getYears(); - int firstYearOfCentury = year - (year % 100); - return new IntervalYearMonth(Period.ZERO.plusYears(firstYearOfCentury), iym.dataType()); - }, "centuries"), + int year = dt.getYear(); + int firstYearOfCentury = year - (year % 100); + return dt.with(ChronoField.YEAR, firstYearOfCentury) + .with(ChronoField.MONTH_OF_YEAR, 1) + .with(ChronoField.DAY_OF_MONTH, 1) + .toLocalDate() + .atStartOfDay(dt.getZone()); + }, idt -> new IntervalDayTime(Duration.ZERO, idt.dataType()), iym -> { + Period period = iym.interval(); + int year = period.getYears(); + int firstYearOfCentury = year - (year % 100); + return new IntervalYearMonth(Period.ZERO.plusYears(firstYearOfCentury), iym.dataType()); + }, "centuries"), DECADE(dt -> { - int year = dt.getYear(); - int firstYearOfDecade = year - (year % 10); - return dt - .with(ChronoField.YEAR, firstYearOfDecade) - .with(ChronoField.MONTH_OF_YEAR, 1) - .with(ChronoField.DAY_OF_MONTH, 1) - .toLocalDate().atStartOfDay(dt.getZone()); - }, - idt -> new IntervalDayTime(Duration.ZERO, idt.dataType()), - iym -> { - Period period = iym.interval(); - int year = period.getYears(); - int firstYearOfDecade = year - (year % 10); - return new IntervalYearMonth(Period.ZERO.plusYears(firstYearOfDecade), iym.dataType()); - }, "decades"), - YEAR(dt -> { - return dt.with(ChronoField.MONTH_OF_YEAR, 1) - .with(ChronoField.DAY_OF_MONTH, 1) - .toLocalDate().atStartOfDay(dt.getZone()); + int year = dt.getYear(); + int firstYearOfDecade = year - (year % 10); + return dt.with(ChronoField.YEAR, firstYearOfDecade) + .with(ChronoField.MONTH_OF_YEAR, 1) + .with(ChronoField.DAY_OF_MONTH, 1) + .toLocalDate() + .atStartOfDay(dt.getZone()); + }, idt -> new IntervalDayTime(Duration.ZERO, idt.dataType()), iym -> { + Period period = iym.interval(); + int year = period.getYears(); + int firstYearOfDecade = year - (year % 10); + return new IntervalYearMonth(Period.ZERO.plusYears(firstYearOfDecade), iym.dataType()); + }, "decades"), + YEAR( + dt -> { + return dt.with(ChronoField.MONTH_OF_YEAR, 1).with(ChronoField.DAY_OF_MONTH, 1).toLocalDate().atStartOfDay(dt.getZone()); }, idt -> new IntervalDayTime(Duration.ZERO, idt.dataType()), iym -> { Period period = iym.interval(); int year = period.getYears(); return new IntervalYearMonth(Period.ZERO.plusYears(year), iym.dataType()); - }, "years", "yy", "yyyy"), - QUARTER(dt -> { - int month = dt.getMonthValue(); - int firstMonthOfQuarter = (((month - 1) / 3) * 3) + 1; - return dt - .with(ChronoField.MONTH_OF_YEAR, firstMonthOfQuarter) - .with(ChronoField.DAY_OF_MONTH, 1) - .toLocalDate().atStartOfDay(dt.getZone()); - }, - idt -> new IntervalDayTime(Duration.ZERO, (idt.dataType())), - iym -> { - Period period = iym.interval(); - int month = period.getMonths(); - int year = period.getYears(); - int firstMonthOfQuarter = (month / 3) * 3; - return new IntervalYearMonth(Period.ZERO.plusYears(year).plusMonths(firstMonthOfQuarter), iym.dataType()); - }, "quarters", "qq", "q"), - MONTH(dt -> { - return dt.with(ChronoField.DAY_OF_MONTH, 1) - .toLocalDate().atStartOfDay(dt.getZone()); }, + "years", + "yy", + "yyyy" + ), + QUARTER(dt -> { + int month = dt.getMonthValue(); + int firstMonthOfQuarter = (((month - 1) / 3) * 3) + 1; + return dt.with(ChronoField.MONTH_OF_YEAR, firstMonthOfQuarter) + .with(ChronoField.DAY_OF_MONTH, 1) + .toLocalDate() + .atStartOfDay(dt.getZone()); + }, idt -> new IntervalDayTime(Duration.ZERO, (idt.dataType())), iym -> { + Period period = iym.interval(); + int month = period.getMonths(); + int year = period.getYears(); + int firstMonthOfQuarter = (month / 3) * 3; + return new IntervalYearMonth(Period.ZERO.plusYears(year).plusMonths(firstMonthOfQuarter), iym.dataType()); + }, "quarters", "qq", "q"), + MONTH( + dt -> { return dt.with(ChronoField.DAY_OF_MONTH, 1).toLocalDate().atStartOfDay(dt.getZone()); }, idt -> new IntervalDayTime(Duration.ZERO, idt.dataType()), - iym -> iym, "months", "mm", "m"), - WEEK(dt -> { - return dt.with(ChronoField.DAY_OF_WEEK, 1) - .toLocalDate().atStartOfDay(dt.getZone()); - }, + iym -> iym, + "months", + "mm", + "m" + ), + WEEK( + dt -> { return dt.with(ChronoField.DAY_OF_WEEK, 1).toLocalDate().atStartOfDay(dt.getZone()); }, idt -> new IntervalDayTime(Duration.ZERO, idt.dataType()), - iym -> iym, "weeks", "wk", "ww"), - DAY(dt -> dt.toLocalDate().atStartOfDay(dt.getZone()), + iym -> iym, + "weeks", + "wk", + "ww" + ), + DAY( + dt -> dt.toLocalDate().atStartOfDay(dt.getZone()), idt -> truncateIntervalSmallerThanWeek(idt, ChronoUnit.DAYS), - iym -> iym, "days", "dd", "d"), + iym -> iym, + "days", + "dd", + "d" + ), HOUR(dt -> { - int hour = dt.getHour(); - return dt.toLocalDate().atStartOfDay(dt.getZone()) - .with(ChronoField.HOUR_OF_DAY, hour); - }, - idt -> truncateIntervalSmallerThanWeek(idt, ChronoUnit.HOURS), - iym -> iym, "hours", "hh"), + int hour = dt.getHour(); + return dt.toLocalDate().atStartOfDay(dt.getZone()).with(ChronoField.HOUR_OF_DAY, hour); + }, idt -> truncateIntervalSmallerThanWeek(idt, ChronoUnit.HOURS), iym -> iym, "hours", "hh"), MINUTE(dt -> { - int hour = dt.getHour(); - int minute = dt.getMinute(); - return dt.toLocalDate().atStartOfDay(dt.getZone()) - .with(ChronoField.HOUR_OF_DAY, hour) - .with(ChronoField.MINUTE_OF_HOUR, minute); - }, - idt -> truncateIntervalSmallerThanWeek(idt, ChronoUnit.MINUTES), - iym -> iym, "minutes", "mi", "n"), - SECOND(dt -> dt.with(ChronoField.NANO_OF_SECOND, 0), + int hour = dt.getHour(); + int minute = dt.getMinute(); + return dt.toLocalDate().atStartOfDay(dt.getZone()).with(ChronoField.HOUR_OF_DAY, hour).with(ChronoField.MINUTE_OF_HOUR, minute); + }, idt -> truncateIntervalSmallerThanWeek(idt, ChronoUnit.MINUTES), iym -> iym, "minutes", "mi", "n"), + SECOND( + dt -> dt.with(ChronoField.NANO_OF_SECOND, 0), idt -> truncateIntervalSmallerThanWeek(idt, ChronoUnit.SECONDS), - iym -> iym, "seconds", "ss", "s"), + iym -> iym, + "seconds", + "ss", + "s" + ), MILLISECOND(dt -> { - int micros = dt.get(ChronoField.MICRO_OF_SECOND); - return dt.with(ChronoField.MILLI_OF_SECOND, (micros / 1000)); - }, - idt -> truncateIntervalSmallerThanWeek(idt, ChronoUnit.MILLIS), - iym -> iym, "milliseconds", "ms"), + int micros = dt.get(ChronoField.MICRO_OF_SECOND); + return dt.with(ChronoField.MILLI_OF_SECOND, (micros / 1000)); + }, idt -> truncateIntervalSmallerThanWeek(idt, ChronoUnit.MILLIS), iym -> iym, "milliseconds", "ms"), MICROSECOND(dt -> { - int nanos = dt.getNano(); - return dt.with(ChronoField.MICRO_OF_SECOND, (nanos / 1000)); - }, - idt -> idt, iym -> iym, "microseconds", "mcs"), + int nanos = dt.getNano(); + return dt.with(ChronoField.MICRO_OF_SECOND, (nanos / 1000)); + }, idt -> idt, iym -> iym, "microseconds", "mcs"), NANOSECOND(dt -> dt, idt -> idt, iym -> iym, "nanoseconds", "ns"); private static final Map NAME_TO_PART; @@ -173,8 +173,12 @@ public enum Part implements DateTimeField { private UnaryOperator truncateFunctionIntervalDayTime; private Set aliases; - Part(UnaryOperator truncateFunctionZonedDateTime, UnaryOperator truncateFunctionIntervalDayTime, - UnaryOperator truncateFunctionIntervalYearMonth, String... aliases) { + Part( + UnaryOperator truncateFunctionZonedDateTime, + UnaryOperator truncateFunctionIntervalDayTime, + UnaryOperator truncateFunctionIntervalYearMonth, + String... aliases + ) { this.truncateFunctionIntervalYearMonth = truncateFunctionIntervalYearMonth; this.truncateFunctionZonedDateTime = truncateFunctionZonedDateTime; this.truncateFunctionIntervalDayTime = truncateFunctionIntervalDayTime; diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTruncProcessor.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTruncProcessor.java index f86f2f00f20e6..03c0e88852964 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTruncProcessor.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTruncProcessor.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.sql.expression.function.scalar.datetime; import org.elasticsearch.common.io.stream.StreamInput; -import org.elasticsearch.xpack.sql.SqlIllegalArgumentException; import org.elasticsearch.xpack.ql.expression.gen.processor.Processor; +import org.elasticsearch.xpack.sql.SqlIllegalArgumentException; import org.elasticsearch.xpack.sql.expression.literal.interval.IntervalDayTime; import org.elasticsearch.xpack.sql.expression.literal.interval.IntervalYearMonth; @@ -55,15 +55,22 @@ public static Object process(Object truncateTo, Object timestamp, ZoneId zoneId) if (truncateDateField == null) { List similar = Part.findSimilar((String) truncateTo); if (similar.isEmpty()) { - throw new SqlIllegalArgumentException("A value of {} or their aliases is required; received [{}]", - Part.values(), truncateTo); + throw new SqlIllegalArgumentException( + "A value of {} or their aliases is required; received [{}]", + Part.values(), + truncateTo + ); } else { - throw new SqlIllegalArgumentException("Received value [{}] is not valid date part for truncation; " + - "did you mean {}?", truncateTo, similar); + throw new SqlIllegalArgumentException( + "Received value [{}] is not valid date part for truncation; " + "did you mean {}?", + truncateTo, + similar + ); } } - if (timestamp instanceof ZonedDateTime == false && timestamp instanceof IntervalYearMonth == false + if (timestamp instanceof ZonedDateTime == false + && timestamp instanceof IntervalYearMonth == false && timestamp instanceof IntervalDayTime == false) { throw new SqlIllegalArgumentException("A date/datetime/interval is required; received [{}]", timestamp); } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/NamedDateTimeFunction.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/NamedDateTimeFunction.java index 0d54936d55977..fa3161bddb52f 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/NamedDateTimeFunction.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/NamedDateTimeFunction.java @@ -35,8 +35,9 @@ abstract class NamedDateTimeFunction extends BaseDateTimeFunction { @Override public ScriptTemplate asScript() { ScriptTemplate script = super.asScript(); - String template = formatTemplate("{sql}." + StringUtils.underscoreToLowerCamelCase(nameExtractor.name()) - + "(" + script.template() + ", {})"); + String template = formatTemplate( + "{sql}." + StringUtils.underscoreToLowerCamelCase(nameExtractor.name()) + "(" + script.template() + ", {})" + ); ParamsBuilder params = paramsBuilder().script(script.params()).variable(zoneId().getId()); diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/NamedDateTimeProcessor.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/NamedDateTimeProcessor.java index 2ab5604fbd881..1d2e2735d86e3 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/NamedDateTimeProcessor.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/NamedDateTimeProcessor.java @@ -43,7 +43,6 @@ public final String extract(ZonedDateTime millis, String tzId) { private static final DateTimeFormatter DAY_NAME_FORMATTER = DateTimeFormatter.ofPattern("EEEE", Locale.ROOT); private static final DateTimeFormatter MONTH_NAME_FORMATTER = DateTimeFormatter.ofPattern("MMMM", Locale.ROOT); - private final NameExtractor extractor; public NamedDateTimeProcessor(NameExtractor extractor, ZoneId zoneId) { @@ -86,8 +85,7 @@ public boolean equals(Object obj) { return false; } NamedDateTimeProcessor other = (NamedDateTimeProcessor) obj; - return Objects.equals(extractor, other.extractor) - && Objects.equals(zoneId(), other.zoneId()); + return Objects.equals(extractor, other.extractor) && Objects.equals(zoneId(), other.zoneId()); } @Override diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/NonIsoDateTimeFunction.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/NonIsoDateTimeFunction.java index 0d3c3a11bca61..68e6b0c1013f3 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/NonIsoDateTimeFunction.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/NonIsoDateTimeFunction.java @@ -35,8 +35,9 @@ abstract class NonIsoDateTimeFunction extends BaseDateTimeFunction { @Override public ScriptTemplate asScript() { ScriptTemplate script = super.asScript(); - String template = formatTemplate("{sql}." + StringUtils.underscoreToLowerCamelCase(extractor.name()) - + "(" + script.template() + ", {})"); + String template = formatTemplate( + "{sql}." + StringUtils.underscoreToLowerCamelCase(extractor.name()) + "(" + script.template() + ", {})" + ); ParamsBuilder params = paramsBuilder().script(script.params()).variable(zoneId().getId()); diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/NonIsoDateTimeProcessor.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/NonIsoDateTimeProcessor.java index 92c9d1987a206..4952b76c0474c 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/NonIsoDateTimeProcessor.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/NonIsoDateTimeProcessor.java @@ -27,9 +27,7 @@ public enum NonIsoDateTimeExtractor { int dayOfWeek = zdt.get(ChronoField.DAY_OF_WEEK) + 1; return dayOfWeek == 8 ? 1 : dayOfWeek; }), - WEEK_OF_YEAR(zdt -> { - return zdt.get(WeekFields.SUNDAY_START.weekOfYear()); - }); + WEEK_OF_YEAR(zdt -> { return zdt.get(WeekFields.SUNDAY_START.weekOfYear()); }); private final Function apply; @@ -90,8 +88,7 @@ public boolean equals(Object obj) { return false; } NonIsoDateTimeProcessor other = (NonIsoDateTimeProcessor) obj; - return Objects.equals(extractor, other.extractor) - && Objects.equals(zoneId(), other.zoneId()); + return Objects.equals(extractor, other.extractor) && Objects.equals(zoneId(), other.zoneId()); } @Override diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/QuarterProcessor.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/QuarterProcessor.java index 3f0bc739277a9..8515897b481db 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/QuarterProcessor.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/QuarterProcessor.java @@ -22,7 +22,6 @@ public class QuarterProcessor extends BaseDateTimeProcessor { public static final String NAME = "q"; private static final DateTimeFormatter QUARTER_FORMAT = DateTimeFormatter.ofPattern("q", Locale.ROOT); - public QuarterProcessor(ZoneId zoneId) { super(zoneId); } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/ThreeArgsDateTimeFunction.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/ThreeArgsDateTimeFunction.java index 62b15a34b2916..4ca64f42019b0 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/ThreeArgsDateTimeFunction.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/ThreeArgsDateTimeFunction.java @@ -82,15 +82,24 @@ public ScriptTemplate asScript() { protected ScriptTemplate asScriptFrom(ScriptTemplate firstScript, ScriptTemplate secondScript, ScriptTemplate thirdScript) { return new ScriptTemplate( - formatTemplate("{sql}." + scriptMethodName() + - "(" + firstScript.template() + "," + secondScript.template() + "," + thirdScript.template() + ",{})"), - paramsBuilder() - .script(firstScript.params()) + formatTemplate( + "{sql}." + + scriptMethodName() + + "(" + + firstScript.template() + + "," + + secondScript.template() + + "," + + thirdScript.template() + + ",{})" + ), + paramsBuilder().script(firstScript.params()) .script(secondScript.params()) .script(thirdScript.params()) .variable(zoneId.getId()) .build(), - dataType()); + dataType() + ); } protected String scriptMethodName() { diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/ThreeArgsDateTimePipe.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/ThreeArgsDateTimePipe.java index 8fc79bda0ef15..33b6c3276d5c2 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/ThreeArgsDateTimePipe.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/ThreeArgsDateTimePipe.java @@ -76,9 +76,6 @@ public boolean equals(Object o) { return false; } ThreeArgsDateTimePipe that = (ThreeArgsDateTimePipe) o; - return first.equals(that.first) && - second.equals(that.second) && - third.equals(that.third) && - zoneId.equals(that.zoneId); + return first.equals(that.first) && second.equals(that.second) && third.equals(that.third) && zoneId.equals(that.zoneId); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/ThreeArgsDateTimeProcessor.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/ThreeArgsDateTimeProcessor.java index 475a520ddbc03..3e603ac4ed990 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/ThreeArgsDateTimeProcessor.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/ThreeArgsDateTimeProcessor.java @@ -91,9 +91,9 @@ public boolean equals(Object o) { return false; } ThreeArgsDateTimeProcessor that = (ThreeArgsDateTimeProcessor) o; - return Objects.equals(first, that.first) && - Objects.equals(second, that.second) && - Objects.equals(third, that.third) && - Objects.equals(zoneId, that.zoneId); + return Objects.equals(first, that.first) + && Objects.equals(second, that.second) + && Objects.equals(third, that.third) + && Objects.equals(zoneId, that.zoneId); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/TimeParse.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/TimeParse.java index c8bc219263755..e7dbe57a17327 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/TimeParse.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/TimeParse.java @@ -15,8 +15,8 @@ import java.time.ZoneId; -import static org.elasticsearch.xpack.sql.expression.function.scalar.datetime.DateTimeParseProcessor.Parser.TIME; import static org.elasticsearch.xpack.sql.expression.function.scalar.datetime.DateTimeParseProcessor.Parser; +import static org.elasticsearch.xpack.sql.expression.function.scalar.datetime.DateTimeParseProcessor.Parser.TIME; public class TimeParse extends BaseDateTimeParseFunction { diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/TimeProcessor.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/TimeProcessor.java index 53c89411ced66..2053ab7d1fe8f 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/TimeProcessor.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/TimeProcessor.java @@ -43,7 +43,7 @@ public Object process(Object input) { private Object doProcess(OffsetTime time) { return extractor().extract(time); } - + public static Integer doProcess(OffsetTime dateTime, String tzId, String extractorName) { return DateTimeProcessor.doProcess(asTimeAtZone(dateTime, ZoneId.of(tzId)), extractorName); } @@ -59,7 +59,6 @@ public boolean equals(Object obj) { return false; } TimeProcessor other = (TimeProcessor) obj; - return Objects.equals(extractor(), other.extractor()) - && Objects.equals(zoneId(), other.zoneId()); + return Objects.equals(extractor(), other.extractor()) && Objects.equals(zoneId(), other.zoneId()); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/ToCharFormatter.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/ToCharFormatter.java index 28600eee7488a..6e7b0fcb47a06 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/ToCharFormatter.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/ToCharFormatter.java @@ -91,9 +91,12 @@ class ToCharFormatter { of("Dy").formatFn("E").text(), of("dy").formatFn("E", x -> x.toLowerCase(Locale.ROOT)).text(), of("DDD").formatFn("DDD").numeric(), - of("IDDD").formatFn(t -> String.format(Locale.ROOT, - "%03d", - (t.get(WeekFields.ISO.weekOfWeekBasedYear()) - 1) * 7 + t.get(ChronoField.DAY_OF_WEEK)) + of("IDDD").formatFn( + t -> String.format( + Locale.ROOT, + "%03d", + (t.get(WeekFields.ISO.weekOfWeekBasedYear()) - 1) * 7 + t.get(ChronoField.DAY_OF_WEEK) + ) ).numeric(), of("DD").formatFn("d", x -> String.format(Locale.ROOT, "%02d", parseInt(x))).numeric(), of("ID").formatFn(t -> String.valueOf(t.get(ChronoField.DAY_OF_WEEK))).numeric(), @@ -108,8 +111,7 @@ class ToCharFormatter { of("J").formatFn(t -> String.valueOf(t.getLong(JulianFields.JULIAN_DAY))).numeric(), of("Q").formatFn("Q").numeric(), of("RM").formatFn("MM", month -> String.format(Locale.ROOT, "%-4s", monthToRoman(parseInt(month)))).text(), - of("rm") - .formatFn("MM", month -> String.format(Locale.ROOT, "%-4s", monthToRoman(parseInt(month)).toLowerCase(Locale.ROOT))) + of("rm").formatFn("MM", month -> String.format(Locale.ROOT, "%-4s", monthToRoman(parseInt(month)).toLowerCase(Locale.ROOT))) .text(), of("TZ").formatFn(ToCharFormatter::zoneAbbreviationOf).text(), of("tz").formatFn(t -> zoneAbbreviationOf(t).toLowerCase(Locale.ROOT)).text(), @@ -131,10 +133,13 @@ class ToCharFormatter { FORMATTER_MAP = formatterMap; } - private static final int MAX_TO_CHAR_FORMAT_STRING_LENGTH = - FORMATTER_MAP.keySet().stream().mapToInt(String::length).max().orElse(Integer.MAX_VALUE); + private static final int MAX_TO_CHAR_FORMAT_STRING_LENGTH = FORMATTER_MAP.keySet() + .stream() + .mapToInt(String::length) + .max() + .orElse(Integer.MAX_VALUE); - private static final String[] ROMAN_NUMBERS = {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI", "XII"}; + private static final String[] ROMAN_NUMBERS = { "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI", "XII" }; private final String pattern; private final boolean acceptsLowercase; @@ -149,7 +154,8 @@ private ToCharFormatter( boolean acceptsLowercase, Function fillModeFn, boolean hasOrdinalSuffix, - Function formatter) { + Function formatter + ) { this.pattern = pattern; this.acceptsLowercase = acceptsLowercase; diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StAswkt.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StAswkt.java index 117a15b66bbc3..f83d48ccbcc8d 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StAswkt.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StAswkt.java @@ -7,7 +7,6 @@ package org.elasticsearch.xpack.sql.expression.function.scalar.geo; - import org.elasticsearch.xpack.ql.expression.Expression; import org.elasticsearch.xpack.ql.tree.NodeInfo; import org.elasticsearch.xpack.ql.tree.Source; diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StDistance.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StDistance.java index ac248611374a6..14e2ba3f02fc6 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StDistance.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StDistance.java @@ -50,9 +50,11 @@ protected NodeInfo info() { @Override public ScriptTemplate scriptWithField(FieldAttribute field) { - return new ScriptTemplate(processScript("{sql}.geoDocValue(doc,{})"), + return new ScriptTemplate( + processScript("{sql}.geoDocValue(doc,{})"), paramsBuilder().variable(field.exactAttribute().name()).build(), - dataType()); + dataType() + ); } @Override diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StDistanceFunction.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StDistanceFunction.java index 509268506e866..74af1ec419c43 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StDistanceFunction.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StDistanceFunction.java @@ -9,7 +9,7 @@ import org.elasticsearch.xpack.ql.expression.predicate.PredicateBiFunction; -class StDistanceFunction implements PredicateBiFunction { +class StDistanceFunction implements PredicateBiFunction { @Override public String name() { diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StDistancePipe.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StDistancePipe.java index f210786e53d73..0d70c2041ba83 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StDistancePipe.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StDistancePipe.java @@ -51,7 +51,6 @@ public boolean equals(Object obj) { } StDistancePipe other = (StDistancePipe) obj; - return Objects.equals(left(), other.left()) - && Objects.equals(right(), other.right()); + return Objects.equals(left(), other.left()) && Objects.equals(right(), other.right()); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StDistanceProcessor.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StDistanceProcessor.java index c1c3d0f697561..a494b0ed6e4ca 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StDistanceProcessor.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StDistanceProcessor.java @@ -78,8 +78,7 @@ public boolean equals(Object obj) { } StDistanceProcessor other = (StDistanceProcessor) obj; - return Objects.equals(left(), other.left()) - && Objects.equals(right(), other.right()); + return Objects.equals(left(), other.left()) && Objects.equals(right(), other.right()); } @Override diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StGeometryType.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StGeometryType.java index e72c356349159..4f8dd557ce251 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StGeometryType.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StGeometryType.java @@ -7,7 +7,6 @@ package org.elasticsearch.xpack.sql.expression.function.scalar.geo; - import org.elasticsearch.xpack.ql.expression.Expression; import org.elasticsearch.xpack.ql.tree.NodeInfo; import org.elasticsearch.xpack.ql.tree.Source; diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StWkttosqlProcessor.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StWkttosqlProcessor.java index 2bdfbd942cffe..c56feacdb9d0c 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StWkttosqlProcessor.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StWkttosqlProcessor.java @@ -22,11 +22,9 @@ public class StWkttosqlProcessor implements Processor { public static final String NAME = "geo_wkttosql"; - StWkttosqlProcessor() { - } + StWkttosqlProcessor() {} - public StWkttosqlProcessor(StreamInput in) throws IOException { - } + public StWkttosqlProcessor(StreamInput in) throws IOException {} @Override public Object process(Object input) { diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StX.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StX.java index 0523bf226d096..3ea0a26d9a658 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StX.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StX.java @@ -7,7 +7,6 @@ package org.elasticsearch.xpack.sql.expression.function.scalar.geo; - import org.elasticsearch.xpack.ql.expression.Expression; import org.elasticsearch.xpack.ql.tree.NodeInfo; import org.elasticsearch.xpack.ql.tree.Source; diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StY.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StY.java index 401f1b278d224..999978c67009e 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StY.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StY.java @@ -7,7 +7,6 @@ package org.elasticsearch.xpack.sql.expression.function.scalar.geo; - import org.elasticsearch.xpack.ql.expression.Expression; import org.elasticsearch.xpack.ql.tree.NodeInfo; import org.elasticsearch.xpack.ql.tree.Source; diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StZ.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StZ.java index f0954b4c1eb52..88a0bbaa1cb47 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StZ.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StZ.java @@ -7,7 +7,6 @@ package org.elasticsearch.xpack.sql.expression.function.scalar.geo; - import org.elasticsearch.xpack.ql.expression.Expression; import org.elasticsearch.xpack.ql.tree.NodeInfo; import org.elasticsearch.xpack.ql.tree.Source; diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/UnaryGeoFunction.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/UnaryGeoFunction.java index 59eb689f1c51b..d85ac3a33dc5c 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/UnaryGeoFunction.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/UnaryGeoFunction.java @@ -54,19 +54,20 @@ protected Processor makeProcessor() { @Override public ScriptTemplate scriptWithField(FieldAttribute field) { - //TODO change this to use _source instead of the exact form (aka field.keyword for geo shape fields) - return new ScriptTemplate(processScript("{sql}.geoDocValue(doc,{})"), + // TODO change this to use _source instead of the exact form (aka field.keyword for geo shape fields) + return new ScriptTemplate( + processScript("{sql}.geoDocValue(doc,{})"), paramsBuilder().variable(field.exactAttribute().name()).build(), - dataType()); + dataType() + ); } @Override public String processScript(String template) { // basically, transform the script to InternalSqlScriptUtils.[function_name](other_function_or_field_name) return super.processScript( - format(Locale.ROOT, "{sql}.%s(%s)", - StringUtils.underscoreToLowerCamelCase("ST_" + operation().name()), - template)); + format(Locale.ROOT, "{sql}.%s(%s)", StringUtils.underscoreToLowerCamelCase("ST_" + operation().name()), template) + ); } @Override diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/math/ACos.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/math/ACos.java index d4233aba978e1..38a80d1fb4073 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/math/ACos.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/math/ACos.java @@ -11,7 +11,6 @@ import org.elasticsearch.xpack.ql.tree.Source; import org.elasticsearch.xpack.sql.expression.function.scalar.math.MathProcessor.MathOperation; - /** * Arc cosine * function. diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/math/BinaryNumericFunction.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/math/BinaryNumericFunction.java index 8afd37514a54e..ae12994dd4a94 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/math/BinaryNumericFunction.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/math/BinaryNumericFunction.java @@ -70,8 +70,6 @@ public boolean equals(Object obj) { return false; } BinaryNumericFunction other = (BinaryNumericFunction) obj; - return Objects.equals(other.left(), left()) - && Objects.equals(other.right(), right()) - && Objects.equals(other.operation, operation); + return Objects.equals(other.left(), left()) && Objects.equals(other.right(), right()) && Objects.equals(other.operation, operation); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/math/BinaryOptionalMathPipe.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/math/BinaryOptionalMathPipe.java index 5250d1105e156..2201dddf1493c 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/math/BinaryOptionalMathPipe.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/math/BinaryOptionalMathPipe.java @@ -105,8 +105,6 @@ public boolean equals(Object obj) { } BinaryOptionalMathPipe other = (BinaryOptionalMathPipe) obj; - return Objects.equals(left, other.left) - && Objects.equals(right, other.right) - && Objects.equals(operation, other.operation); + return Objects.equals(left, other.left) && Objects.equals(right, other.right) && Objects.equals(operation, other.operation); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/math/BinaryOptionalMathProcessor.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/math/BinaryOptionalMathProcessor.java index 3232e7daa559f..134fa766cf56c 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/math/BinaryOptionalMathProcessor.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/math/BinaryOptionalMathProcessor.java @@ -127,8 +127,8 @@ public boolean equals(Object obj) { BinaryOptionalMathProcessor other = (BinaryOptionalMathProcessor) obj; return Objects.equals(left(), other.left()) - && Objects.equals(right(), other.right()) - && Objects.equals(operation(), other.operation()); + && Objects.equals(right(), other.right()) + && Objects.equals(operation(), other.operation()); } @Override diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/math/BinaryOptionalNumericFunction.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/math/BinaryOptionalNumericFunction.java index 595e7ce34a3e6..252f1124fe577 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/math/BinaryOptionalNumericFunction.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/math/BinaryOptionalNumericFunction.java @@ -56,18 +56,20 @@ protected TypeResolution resolveType() { @Override protected Pipe makePipe() { - return new BinaryOptionalMathPipe(source(), this, + return new BinaryOptionalMathPipe( + source(), + this, Expressions.pipe(left), right == null ? null : Expressions.pipe(right), - operation()); + operation() + ); } protected abstract BinaryOptionalMathOperation operation(); @Override public boolean foldable() { - return left.foldable() - && (right == null || right.foldable()); + return left.foldable() && (right == null || right.foldable()); } @Override @@ -91,13 +93,17 @@ public ScriptTemplate asScript() { } private ScriptTemplate asScriptFrom(ScriptTemplate leftScript, ScriptTemplate rightScript) { - return new ScriptTemplate(format(Locale.ROOT, formatTemplate("{sql}.%s(%s,%s)"), + return new ScriptTemplate( + format( + Locale.ROOT, + formatTemplate("{sql}.%s(%s,%s)"), operation().name().toLowerCase(Locale.ROOT), leftScript.template(), - rightScript.template()), - paramsBuilder() - .script(leftScript.params()).script(rightScript.params()) - .build(), dataType()); + rightScript.template() + ), + paramsBuilder().script(leftScript.params()).script(rightScript.params()).build(), + dataType() + ); } @Override diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/math/E.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/math/E.java index 5b596d09ad4dc..7f8d370dc02fc 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/math/E.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/math/E.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.xpack.sql.expression.function.scalar.math; - import org.elasticsearch.xpack.ql.expression.Expression; import org.elasticsearch.xpack.ql.expression.Literal; import org.elasticsearch.xpack.ql.expression.gen.script.Params; diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/math/MathFunction.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/math/MathFunction.java index 673b871ca11d9..6b70276260460 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/math/MathFunction.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/math/MathFunction.java @@ -43,8 +43,7 @@ public Object fold() { @Override public String processScript(String template) { - return super.processScript(format( - Locale.ROOT, "{sql}.%s(%s)", getClass().getSimpleName().toLowerCase(Locale.ROOT), template)); + return super.processScript(format(Locale.ROOT, "{sql}.%s(%s)", getClass().getSimpleName().toLowerCase(Locale.ROOT), template)); } @Override diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/math/MathProcessor.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/math/MathProcessor.java index f8c3d2b7e3ef4..9b45b18ea7236 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/math/MathProcessor.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/math/MathProcessor.java @@ -73,9 +73,7 @@ public enum MathOperation { LOG10(Math::log10), PI(() -> Math.PI), RADIANS(Math::toRadians), - RANDOM((Object l) -> l != null ? - new Random(((Number) l).longValue()).nextDouble() : - Randomness.get().nextDouble(), true), + RANDOM((Object l) -> l != null ? new Random(((Number) l).longValue()).nextDouble() : Randomness.get().nextDouble(), true), SIGN((Object l) -> { if (l instanceof Double) { return (int) Math.signum((Double) l); diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/math/Pi.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/math/Pi.java index 1023e6996899b..74b93a4539913 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/math/Pi.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/math/Pi.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.xpack.sql.expression.function.scalar.math; - import org.elasticsearch.xpack.ql.expression.Expression; import org.elasticsearch.xpack.ql.expression.Literal; import org.elasticsearch.xpack.ql.expression.gen.script.Params; diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/BinaryStringFunction.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/BinaryStringFunction.java index 98777be7f21d9..e597c56098525 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/BinaryStringFunction.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/BinaryStringFunction.java @@ -25,7 +25,7 @@ * Base class for binary functions that have the first parameter a string, the second parameter a number * or a string and the result can be a string or a number. */ -public abstract class BinaryStringFunction extends BinaryScalarFunction { +public abstract class BinaryStringFunction extends BinaryScalarFunction { protected BinaryStringFunction(Source source, Expression left, Expression right) { super(source, left, right); @@ -68,9 +68,11 @@ protected String scriptMethodName() { @Override public ScriptTemplate scriptWithField(FieldAttribute field) { - return new ScriptTemplate(processScript(Scripts.DOC_VALUE), - paramsBuilder().variable(field.exactAttribute().name()).build(), - dataType()); + return new ScriptTemplate( + processScript(Scripts.DOC_VALUE), + paramsBuilder().variable(field.exactAttribute().name()).build(), + dataType() + ); } @Override @@ -83,8 +85,7 @@ public boolean equals(Object obj) { if (obj == null || obj.getClass() != getClass()) { return false; } - BinaryStringFunction other = (BinaryStringFunction) obj; - return Objects.equals(other.left(), left()) - && Objects.equals(other.right(), right()); + BinaryStringFunction other = (BinaryStringFunction) obj; + return Objects.equals(other.left(), left()) && Objects.equals(other.right(), right()); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/BinaryStringNumericPipe.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/BinaryStringNumericPipe.java index 942212bcb3888..0d5f5bf957804 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/BinaryStringNumericPipe.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/BinaryStringNumericPipe.java @@ -22,8 +22,7 @@ public class BinaryStringNumericPipe extends BinaryPipe { private final BinaryStringNumericOperation operation; - public BinaryStringNumericPipe(Source source, Expression expression, Pipe left, Pipe right, - BinaryStringNumericOperation operation) { + public BinaryStringNumericPipe(Source source, Expression expression, Pipe left, Pipe right, BinaryStringNumericOperation operation) { super(source, expression, left, right); this.operation = operation; } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/BinaryStringNumericProcessor.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/BinaryStringNumericProcessor.java index 202c1425ce4ca..434c5a66cd06e 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/BinaryStringNumericProcessor.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/BinaryStringNumericProcessor.java @@ -23,21 +23,21 @@ public class BinaryStringNumericProcessor extends FunctionalEnumBinaryProcessor { public enum BinaryStringNumericOperation implements BiFunction { - LEFT((s,c) -> { + LEFT((s, c) -> { int i = c.intValue(); if (i < 0) { return ""; } return i > s.length() ? s : s.substring(0, i); }), - RIGHT((s,c) -> { + RIGHT((s, c) -> { int i = c.intValue(); if (i < 0) { return ""; } return i > s.length() ? s : s.substring(s.length() - i); }), - REPEAT((s,c) -> { + REPEAT((s, c) -> { int i = c.intValue(); if (i <= 0) { return null; diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/BinaryStringStringProcessor.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/BinaryStringStringProcessor.java index e849154be9c17..7b2aaa9bb726e 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/BinaryStringStringProcessor.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/BinaryStringStringProcessor.java @@ -21,9 +21,9 @@ public class BinaryStringStringProcessor extends FunctionalEnumBinaryProcessor { public enum BinaryStringStringOperation implements BiFunction { - POSITION((sub,str) -> { + POSITION((sub, str) -> { int pos = str.indexOf(sub); - return pos < 0 ? 0 : pos+1; + return pos < 0 ? 0 : pos + 1; }); BinaryStringStringOperation(BiFunction op) { diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/BitLength.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/BitLength.java index a93f8f24a373c..803a92ff72801 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/BitLength.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/BitLength.java @@ -39,7 +39,7 @@ protected StringOperation operation() { @Override public DataType dataType() { - //TODO investigate if a data type Long (BIGINT) wouldn't be more appropriate here + // TODO investigate if a data type Long (BIGINT) wouldn't be more appropriate here return DataTypes.INTEGER; } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/Concat.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/Concat.java index ef348204d4201..d2c27c15cffe4 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/Concat.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/Concat.java @@ -82,9 +82,11 @@ protected NodeInfo info() { @Override public ScriptTemplate scriptWithField(FieldAttribute field) { - return new ScriptTemplate(processScript(Scripts.DOC_VALUE), - paramsBuilder().variable(field.exactAttribute().name()).build(), - dataType()); + return new ScriptTemplate( + processScript(Scripts.DOC_VALUE), + paramsBuilder().variable(field.exactAttribute().name()).build(), + dataType() + ); } @Override diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/ConcatFunctionPipe.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/ConcatFunctionPipe.java index df859d9f71c8c..e01a93f1dfbb3 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/ConcatFunctionPipe.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/ConcatFunctionPipe.java @@ -51,7 +51,6 @@ public boolean equals(Object obj) { } ConcatFunctionPipe other = (ConcatFunctionPipe) obj; - return Objects.equals(left(), other.left()) - && Objects.equals(right(), other.right()); + return Objects.equals(left(), other.left()) && Objects.equals(right(), other.right()); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/ConcatFunctionProcessor.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/ConcatFunctionProcessor.java index 2d5ff7086b9d5..3d7e403468b7d 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/ConcatFunctionProcessor.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/ConcatFunctionProcessor.java @@ -81,8 +81,7 @@ public boolean equals(Object obj) { } ConcatFunctionProcessor other = (ConcatFunctionProcessor) obj; - return Objects.equals(left(), other.left()) - && Objects.equals(right(), other.right()); + return Objects.equals(left(), other.left()) && Objects.equals(right(), other.right()); } @Override @@ -91,6 +90,5 @@ public int hashCode() { } @Override - protected void doWrite(StreamOutput out) throws IOException { - } + protected void doWrite(StreamOutput out) throws IOException {} } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/Insert.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/Insert.java index 1fba81cc65679..f614f1adea5bc 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/Insert.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/Insert.java @@ -74,10 +74,7 @@ protected TypeResolution resolveType() { @Override public boolean foldable() { - return input.foldable() - && start.foldable() - && length.foldable() - && replacement.foldable(); + return input.foldable() && start.foldable() && length.foldable() && replacement.foldable(); } @Override @@ -87,11 +84,14 @@ public Object fold() { @Override protected Pipe makePipe() { - return new InsertFunctionPipe(source(), this, - Expressions.pipe(input), - Expressions.pipe(start), - Expressions.pipe(length), - Expressions.pipe(replacement)); + return new InsertFunctionPipe( + source(), + this, + Expressions.pipe(input), + Expressions.pipe(start), + Expressions.pipe(length), + Expressions.pipe(replacement) + ); } @Override @@ -109,26 +109,39 @@ public ScriptTemplate asScript() { return asScriptFrom(inputScript, startScript, lengthScript, replacementScript); } - private ScriptTemplate asScriptFrom(ScriptTemplate inputScript, ScriptTemplate startScript, - ScriptTemplate lengthScript, ScriptTemplate replacementScript) { + private ScriptTemplate asScriptFrom( + ScriptTemplate inputScript, + ScriptTemplate startScript, + ScriptTemplate lengthScript, + ScriptTemplate replacementScript + ) { // basically, transform the script to InternalSqlScriptUtils.[function_name](function_or_field1, function_or_field2,...) - return new ScriptTemplate(format(Locale.ROOT, formatTemplate("{sql}.%s(%s,%s,%s,%s)"), + return new ScriptTemplate( + format( + Locale.ROOT, + formatTemplate("{sql}.%s(%s,%s,%s,%s)"), "insert", inputScript.template(), startScript.template(), lengthScript.template(), - replacementScript.template()), - paramsBuilder() - .script(inputScript.params()).script(startScript.params()) - .script(lengthScript.params()).script(replacementScript.params()) - .build(), dataType()); + replacementScript.template() + ), + paramsBuilder().script(inputScript.params()) + .script(startScript.params()) + .script(lengthScript.params()) + .script(replacementScript.params()) + .build(), + dataType() + ); } @Override public ScriptTemplate scriptWithField(FieldAttribute field) { - return new ScriptTemplate(processScript(Scripts.DOC_VALUE), - paramsBuilder().variable(field.exactAttribute().name()).build(), - dataType()); + return new ScriptTemplate( + processScript(Scripts.DOC_VALUE), + paramsBuilder().variable(field.exactAttribute().name()).build(), + dataType() + ); } @Override diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/InsertFunctionPipe.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/InsertFunctionPipe.java index af66822d5d54c..32c02f0b5dc5e 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/InsertFunctionPipe.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/InsertFunctionPipe.java @@ -20,9 +20,7 @@ public class InsertFunctionPipe extends Pipe { private final Pipe input, start, length, replacement; - public InsertFunctionPipe(Source source, Expression expression, - Pipe input, Pipe start, - Pipe length, Pipe replacement) { + public InsertFunctionPipe(Source source, Expression expression, Pipe input, Pipe start, Pipe length, Pipe replacement) { super(source, expression, Arrays.asList(input, start, length, replacement)); this.input = input; this.start = start; @@ -41,10 +39,7 @@ public final Pipe resolveAttributes(AttributeResolver resolver) { Pipe newStart = start.resolveAttributes(resolver); Pipe newLength = length.resolveAttributes(resolver); Pipe newReplacement = replacement.resolveAttributes(resolver); - if (newInput == input - && newStart == start - && newLength == length - && newReplacement == replacement) { + if (newInput == input && newStart == start && newLength == length && newReplacement == replacement) { return this; } return replaceChildren(newInput, newStart, newLength, newReplacement); @@ -53,9 +48,9 @@ public final Pipe resolveAttributes(AttributeResolver resolver) { @Override public boolean supportedByAggsOnlyQuery() { return input.supportedByAggsOnlyQuery() - && start.supportedByAggsOnlyQuery() - && length.supportedByAggsOnlyQuery() - && replacement.supportedByAggsOnlyQuery(); + && start.supportedByAggsOnlyQuery() + && length.supportedByAggsOnlyQuery() + && replacement.supportedByAggsOnlyQuery(); } @Override @@ -63,10 +58,7 @@ public boolean resolved() { return input.resolved() && start.resolved() && length.resolved() && replacement.resolved(); } - protected Pipe replaceChildren(Pipe newInput, - Pipe newStart, - Pipe newLength, - Pipe newReplacement) { + protected Pipe replaceChildren(Pipe newInput, Pipe newStart, Pipe newLength, Pipe newReplacement) { return new InsertFunctionPipe(source(), expression(), newInput, newStart, newLength, newReplacement); } @@ -121,8 +113,8 @@ public boolean equals(Object obj) { InsertFunctionPipe other = (InsertFunctionPipe) obj; return Objects.equals(input, other.input) - && Objects.equals(start, other.start) - && Objects.equals(length, other.length) - && Objects.equals(replacement, other.replacement); + && Objects.equals(start, other.start) + && Objects.equals(length, other.length) + && Objects.equals(replacement, other.replacement); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/InsertFunctionProcessor.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/InsertFunctionProcessor.java index 1b6865be2d51e..ebd3dec176258 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/InsertFunctionProcessor.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/InsertFunctionProcessor.java @@ -71,9 +71,7 @@ public static Object doProcess(Object input, Object start, Object length, Object StringBuilder sb = new StringBuilder(input.toString()); String replString = (replacement.toString()); - return sb.replace(realStart, - realStart + ((Number) length).intValue(), - replString).toString(); + return sb.replace(realStart, realStart + ((Number) length).intValue(), replString).toString(); } @Override @@ -88,9 +86,9 @@ public boolean equals(Object obj) { InsertFunctionProcessor other = (InsertFunctionProcessor) obj; return Objects.equals(input(), other.input()) - && Objects.equals(start(), other.start()) - && Objects.equals(length(), other.length()) - && Objects.equals(replacement(), other.replacement()); + && Objects.equals(start(), other.start()) + && Objects.equals(length(), other.length()) + && Objects.equals(replacement(), other.replacement()); } @Override diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/Locate.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/Locate.java index 8940a5d7ac69d..450d2d5ccc154 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/Locate.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/Locate.java @@ -72,10 +72,13 @@ protected TypeResolution resolveType() { @Override protected Pipe makePipe() { - return new LocateFunctionPipe(source(), this, + return new LocateFunctionPipe( + source(), + this, Expressions.pipe(pattern), Expressions.pipe(input), - start == null ? null : Expressions.pipe(start)); + start == null ? null : Expressions.pipe(start) + ); } @Override @@ -90,9 +93,7 @@ public Nullability nullable() { @Override public boolean foldable() { - return pattern.foldable() - && input.foldable() - && (start == null || start.foldable()); + return pattern.foldable() && input.foldable() && (start == null || start.foldable()); } @Override @@ -111,31 +112,34 @@ public ScriptTemplate asScript() { private ScriptTemplate asScriptFrom(ScriptTemplate patternScript, ScriptTemplate inputScript, ScriptTemplate startScript) { if (start == null) { - return new ScriptTemplate(format(Locale.ROOT, formatTemplate("{sql}.%s(%s,%s)"), - "locate", - patternScript.template(), - inputScript.template()), - paramsBuilder() - .script(patternScript.params()).script(inputScript.params()) - .build(), dataType()); + return new ScriptTemplate( + format(Locale.ROOT, formatTemplate("{sql}.%s(%s,%s)"), "locate", patternScript.template(), inputScript.template()), + paramsBuilder().script(patternScript.params()).script(inputScript.params()).build(), + dataType() + ); } // basically, transform the script to InternalSqlScriptUtils.[function_name](function_or_field1, function_or_field2,...) - return new ScriptTemplate(format(Locale.ROOT, formatTemplate("{sql}.%s(%s,%s,%s)"), + return new ScriptTemplate( + format( + Locale.ROOT, + formatTemplate("{sql}.%s(%s,%s,%s)"), "locate", patternScript.template(), inputScript.template(), - startScript.template()), - paramsBuilder() - .script(patternScript.params()).script(inputScript.params()) - .script(startScript.params()) - .build(), dataType()); + startScript.template() + ), + paramsBuilder().script(patternScript.params()).script(inputScript.params()).script(startScript.params()).build(), + dataType() + ); } @Override public ScriptTemplate scriptWithField(FieldAttribute field) { - return new ScriptTemplate(processScript(Scripts.DOC_VALUE), - paramsBuilder().variable(field.exactAttribute().name()).build(), - dataType()); + return new ScriptTemplate( + processScript(Scripts.DOC_VALUE), + paramsBuilder().variable(field.exactAttribute().name()).build(), + dataType() + ); } @Override diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/LocateFunctionPipe.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/LocateFunctionPipe.java index dfdf1132da6f0..10b6633b362be 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/LocateFunctionPipe.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/LocateFunctionPipe.java @@ -45,8 +45,9 @@ public final Pipe resolveAttributes(AttributeResolver resolver) { @Override public boolean supportedByAggsOnlyQuery() { - return pattern.supportedByAggsOnlyQuery() && input.supportedByAggsOnlyQuery() - && (start == null || start.supportedByAggsOnlyQuery()); + return pattern.supportedByAggsOnlyQuery() + && input.supportedByAggsOnlyQuery() + && (start == null || start.supportedByAggsOnlyQuery()); } @Override diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/LocateFunctionProcessor.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/LocateFunctionProcessor.java index b8fe75160308f..3641238a654f3 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/LocateFunctionProcessor.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/LocateFunctionProcessor.java @@ -43,7 +43,7 @@ public final void writeTo(StreamOutput out) throws IOException { public Object process(Object input) { return doProcess(pattern().process(input), input().process(input), start() == null ? null : start().process(input)); } - + public static Integer doProcess(Object pattern, Object input, Object start) { if (pattern == null || input == null) { return null; @@ -63,7 +63,6 @@ public static Integer doProcess(Object pattern, Object input, Object start) { String stringInput = input instanceof Character ? input.toString() : (String) input; String stringPattern = pattern instanceof Character ? pattern.toString() : (String) pattern; - int startIndex = start == null ? 0 : ((Number) start).intValue() - 1; return 1 + stringInput.indexOf(stringPattern, startIndex); } @@ -80,8 +79,8 @@ public boolean equals(Object obj) { LocateFunctionProcessor other = (LocateFunctionProcessor) obj; return Objects.equals(pattern(), other.pattern()) - && Objects.equals(input(), other.input()) - && Objects.equals(start(), other.start()); + && Objects.equals(input(), other.input()) + && Objects.equals(start(), other.start()); } @Override diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/Position.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/Position.java index 47f253b2b6628..d4eed8528091f 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/Position.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/Position.java @@ -36,10 +36,13 @@ protected Position replaceChildren(Expression newLeft, Expression newRight) { @Override protected Pipe makePipe() { - return new BinaryStringStringPipe(source(), this, - Expressions.pipe(left()), - Expressions.pipe(right()), - BinaryStringStringOperation.POSITION); + return new BinaryStringStringPipe( + source(), + this, + Expressions.pipe(left()), + Expressions.pipe(right()), + BinaryStringStringOperation.POSITION + ); } @Override diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/Replace.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/Replace.java index ed338d8b6b20f..b225704daf7e9 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/Replace.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/Replace.java @@ -65,10 +65,7 @@ protected TypeResolution resolveType() { @Override protected Pipe makePipe() { - return new ReplaceFunctionPipe(source(), this, - Expressions.pipe(input), - Expressions.pipe(pattern), - Expressions.pipe(replacement)); + return new ReplaceFunctionPipe(source(), this, Expressions.pipe(input), Expressions.pipe(pattern), Expressions.pipe(replacement)); } @Override @@ -78,9 +75,7 @@ protected NodeInfo info() { @Override public boolean foldable() { - return input.foldable() - && pattern.foldable() - && replacement.foldable(); + return input.foldable() && pattern.foldable() && replacement.foldable(); } @Override @@ -99,22 +94,27 @@ public ScriptTemplate asScript() { private ScriptTemplate asScriptFrom(ScriptTemplate inputScript, ScriptTemplate patternScript, ScriptTemplate replacementScript) { // basically, transform the script to InternalSqlScriptUtils.[function_name](function_or_field1, function_or_field2,...) - return new ScriptTemplate(format(Locale.ROOT, formatTemplate("{sql}.%s(%s,%s,%s)"), + return new ScriptTemplate( + format( + Locale.ROOT, + formatTemplate("{sql}.%s(%s,%s,%s)"), "replace", inputScript.template(), patternScript.template(), - replacementScript.template()), - paramsBuilder() - .script(inputScript.params()).script(patternScript.params()) - .script(replacementScript.params()) - .build(), dataType()); + replacementScript.template() + ), + paramsBuilder().script(inputScript.params()).script(patternScript.params()).script(replacementScript.params()).build(), + dataType() + ); } @Override public ScriptTemplate scriptWithField(FieldAttribute field) { - return new ScriptTemplate(processScript(Scripts.DOC_VALUE), - paramsBuilder().variable(field.exactAttribute().name()).build(), - dataType()); + return new ScriptTemplate( + processScript(Scripts.DOC_VALUE), + paramsBuilder().variable(field.exactAttribute().name()).build(), + dataType() + ); } @Override diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/ReplaceFunctionPipe.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/ReplaceFunctionPipe.java index 694261eb69aa8..f8bfc0ec2691a 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/ReplaceFunctionPipe.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/ReplaceFunctionPipe.java @@ -103,7 +103,7 @@ public boolean equals(Object obj) { ReplaceFunctionPipe other = (ReplaceFunctionPipe) obj; return Objects.equals(input, other.input) - && Objects.equals(pattern, other.pattern) - && Objects.equals(replacement, other.replacement); + && Objects.equals(pattern, other.pattern) + && Objects.equals(replacement, other.replacement); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/ReplaceFunctionProcessor.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/ReplaceFunctionProcessor.java index 293ab72301d92..98c35036c272c 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/ReplaceFunctionProcessor.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/ReplaceFunctionProcessor.java @@ -58,9 +58,11 @@ public static Object doProcess(Object input, Object pattern, Object replacement) throw new SqlIllegalArgumentException("A string/char is required; received [{}]", replacement); } - return Strings.replace(input instanceof Character ? input.toString() : (String) input, - pattern instanceof Character ? pattern.toString() : (String) pattern, - replacement instanceof Character ? replacement.toString() : (String) replacement); + return Strings.replace( + input instanceof Character ? input.toString() : (String) input, + pattern instanceof Character ? pattern.toString() : (String) pattern, + replacement instanceof Character ? replacement.toString() : (String) replacement + ); } @Override @@ -75,8 +77,8 @@ public boolean equals(Object obj) { ReplaceFunctionProcessor other = (ReplaceFunctionProcessor) obj; return Objects.equals(input(), other.input()) - && Objects.equals(pattern(), other.pattern()) - && Objects.equals(replacement(), other.replacement()); + && Objects.equals(pattern(), other.pattern()) + && Objects.equals(replacement(), other.replacement()); } @Override diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/Substring.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/Substring.java index 8cf79ecc5036b..34521439e9050 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/Substring.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/Substring.java @@ -93,25 +93,29 @@ public ScriptTemplate asScript() { return asScriptFrom(inputScript, startScript, lengthScript); } - protected ScriptTemplate asScriptFrom(ScriptTemplate inputScript, ScriptTemplate startScript, - ScriptTemplate lengthScript) { + protected ScriptTemplate asScriptFrom(ScriptTemplate inputScript, ScriptTemplate startScript, ScriptTemplate lengthScript) { // basically, transform the script to InternalSqlScriptUtils.[function_name](function_or_field1, function_or_field2,...) - return new ScriptTemplate(format(Locale.ROOT, formatTemplate("{sql}.%s(%s,%s,%s)"), + return new ScriptTemplate( + format( + Locale.ROOT, + formatTemplate("{sql}.%s(%s,%s,%s)"), "substring", inputScript.template(), startScript.template(), - lengthScript.template()), - paramsBuilder() - .script(inputScript.params()).script(startScript.params()) - .script(lengthScript.params()) - .build(), dataType()); + lengthScript.template() + ), + paramsBuilder().script(inputScript.params()).script(startScript.params()).script(lengthScript.params()).build(), + dataType() + ); } @Override public ScriptTemplate scriptWithField(FieldAttribute field) { - return new ScriptTemplate(processScript("doc[{}].value"), - paramsBuilder().variable(field.exactAttribute().name()).build(), - dataType()); + return new ScriptTemplate( + processScript("doc[{}].value"), + paramsBuilder().variable(field.exactAttribute().name()).build(), + dataType() + ); } @Override diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/SubstringFunctionProcessor.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/SubstringFunctionProcessor.java index fa43a168f7535..f31264350b069 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/SubstringFunctionProcessor.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/SubstringFunctionProcessor.java @@ -59,9 +59,11 @@ public static Object doProcess(Object input, Object start, Object length) { Check.isFixedNumberAndInRange(start, "start", (long) Integer.MIN_VALUE + 1, (long) Integer.MAX_VALUE); Check.isFixedNumberAndInRange(length, "length", 0L, (long) Integer.MAX_VALUE); - return StringFunctionUtils.substring(input instanceof Character ? input.toString() : (String) input, - ((Number) start).intValue() - 1, // SQL is 1-based when it comes to string manipulation - ((Number) length).intValue()); + return StringFunctionUtils.substring( + input instanceof Character ? input.toString() : (String) input, + ((Number) start).intValue() - 1, // SQL is 1-based when it comes to string manipulation + ((Number) length).intValue() + ); } protected Processor input() { @@ -87,9 +89,7 @@ public boolean equals(Object obj) { } SubstringFunctionProcessor other = (SubstringFunctionProcessor) obj; - return Objects.equals(input(), other.input()) - && Objects.equals(start(), other.start()) - && Objects.equals(length(), other.length()); + return Objects.equals(input(), other.input()) && Objects.equals(start(), other.start()) && Objects.equals(length(), other.length()); } @Override @@ -97,7 +97,6 @@ public int hashCode() { return Objects.hash(input(), start(), length()); } - @Override public String getWriteableName() { return NAME; diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/UnaryStringFunction.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/UnaryStringFunction.java index 6037d5c571206..5229cf5bdb73a 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/UnaryStringFunction.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/UnaryStringFunction.java @@ -57,18 +57,17 @@ protected Processor makeProcessor() { @Override public ScriptTemplate scriptWithField(FieldAttribute field) { - //TODO change this to use _source instead of the exact form (aka field.keyword for text fields) - return new ScriptTemplate(processScript(Scripts.DOC_VALUE), - paramsBuilder().variable(field.exactAttribute().name()).build(), - dataType()); + // TODO change this to use _source instead of the exact form (aka field.keyword for text fields) + return new ScriptTemplate( + processScript(Scripts.DOC_VALUE), + paramsBuilder().variable(field.exactAttribute().name()).build(), + dataType() + ); } @Override public String processScript(String template) { - return formatTemplate( - format(Locale.ROOT, "{sql}.%s(%s)", - StringUtils.underscoreToLowerCamelCase(operation().name()), - template)); + return formatTemplate(format(Locale.ROOT, "{sql}.%s(%s)", StringUtils.underscoreToLowerCamelCase(operation().name()), template)); } @Override diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/UnaryStringIntFunction.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/UnaryStringIntFunction.java index 064aaa361f5a3..8e6574faeb3e5 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/UnaryStringIntFunction.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/UnaryStringIntFunction.java @@ -59,17 +59,12 @@ protected Processor makeProcessor() { @Override public ScriptTemplate scriptWithField(FieldAttribute field) { - return new ScriptTemplate(processScript(Scripts.DOC_VALUE), - paramsBuilder().variable(field.name()).build(), - dataType()); + return new ScriptTemplate(processScript(Scripts.DOC_VALUE), paramsBuilder().variable(field.name()).build(), dataType()); } @Override public String processScript(String template) { - return super.processScript( - format(Locale.ROOT, "{sql}.%s(%s)", - operation().toString().toLowerCase(Locale.ROOT), - template)); + return super.processScript(format(Locale.ROOT, "{sql}.%s(%s)", operation().toString().toLowerCase(Locale.ROOT), template)); } @Override diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/whitelist/InternalSqlScriptUtils.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/whitelist/InternalSqlScriptUtils.java index 53016c81a862c..5d06a41e93180 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/whitelist/InternalSqlScriptUtils.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/whitelist/InternalSqlScriptUtils.java @@ -217,8 +217,6 @@ public static Number tan(Number value) { return MathOperation.TAN.apply(value); } - - // // Date/Time functions // @@ -284,16 +282,16 @@ public static Integer weekOfYear(Object dateTime, String tzId) { } public static ZonedDateTime dateAdd(String dateField, Integer numberOfUnits, Object dateTime, String tzId) { - return (ZonedDateTime) DateAddProcessor.process(dateField, numberOfUnits, asDateTime(dateTime) , ZoneId.of(tzId)); + return (ZonedDateTime) DateAddProcessor.process(dateField, numberOfUnits, asDateTime(dateTime), ZoneId.of(tzId)); } public static Integer dateDiff(String dateField, Object dateTime1, Object dateTime2, String tzId) { - return (Integer) DateDiffProcessor.process(dateField, asDateTime(dateTime1), asDateTime(dateTime2) , ZoneId.of(tzId)); + return (Integer) DateDiffProcessor.process(dateField, asDateTime(dateTime1), asDateTime(dateTime2), ZoneId.of(tzId)); } public static Object dateTrunc(String truncateTo, Object dateTimeOrInterval, String tzId) { if (dateTimeOrInterval instanceof IntervalDayTime || dateTimeOrInterval instanceof IntervalYearMonth) { - return DateTruncProcessor.process(truncateTo, dateTimeOrInterval, ZoneId.of(tzId)); + return DateTruncProcessor.process(truncateTo, dateTimeOrInterval, ZoneId.of(tzId)); } return DateTruncProcessor.process(truncateTo, asDateTime(dateTimeOrInterval), ZoneId.of(tzId)); } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/literal/geo/GeoShape.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/literal/geo/GeoShape.java index c7d47c84b41e4..386686f4f17d7 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/literal/geo/GeoShape.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/literal/geo/GeoShape.java @@ -12,11 +12,6 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.ToXContentFragment; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.geometry.Circle; import org.elasticsearch.geometry.Geometry; import org.elasticsearch.geometry.GeometryCollection; @@ -30,6 +25,11 @@ import org.elasticsearch.geometry.Polygon; import org.elasticsearch.geometry.Rectangle; import org.elasticsearch.geometry.utils.WellKnownText; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ToXContentFragment; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.ql.QlIllegalArgumentException; import org.elasticsearch.xpack.ql.expression.gen.processor.ConstantNamedWriteable; @@ -110,7 +110,7 @@ public Point visit(GeometryCollection collection) { @Override public Point visit(Line line) { if (line.length() > 0) { - return new Point(line.getX(0), line.getY(0), line.hasZ() ? line.getZ(0) : Double.NaN); + return new Point(line.getX(0), line.getY(0), line.hasZ() ? line.getZ(0) : Double.NaN); } return null; } @@ -213,9 +213,14 @@ private static Geometry parse(Object value) throws IOException, ParseException { content.field("value", value); content.endObject(); - try (InputStream stream = BytesReference.bytes(content).streamInput(); - XContentParser parser = JsonXContent.jsonXContent.createParser( - NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) { + try ( + InputStream stream = BytesReference.bytes(content).streamInput(); + XContentParser parser = JsonXContent.jsonXContent.createParser( + NamedXContentRegistry.EMPTY, + LoggingDeprecationHandler.INSTANCE, + stream + ) + ) { parser.nextToken(); // start object parser.nextToken(); // field name parser.nextToken(); // field value diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/literal/interval/Interval.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/literal/interval/Interval.java index 1796d816c3c4b..78da4442a37ae 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/literal/interval/Interval.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/literal/interval/Interval.java @@ -67,8 +67,7 @@ public boolean equals(Object obj) { } Interval other = (Interval) obj; - return Objects.equals(other.interval, interval) - && Objects.equals(other.intervalType, intervalType); + return Objects.equals(other.interval, interval) && Objects.equals(other.intervalType, intervalType); } @Override diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/literal/interval/IntervalYearMonth.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/literal/interval/IntervalYearMonth.java index 17fb22eeed574..bedd625c64bb9 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/literal/interval/IntervalYearMonth.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/literal/interval/IntervalYearMonth.java @@ -51,14 +51,18 @@ public String getWriteableName() { @Override public IntervalYearMonth add(Interval interval) { - return new IntervalYearMonth(interval().plus(interval.interval()).normalized(), - Intervals.compatibleInterval(dataType(), interval.dataType())); + return new IntervalYearMonth( + interval().plus(interval.interval()).normalized(), + Intervals.compatibleInterval(dataType(), interval.dataType()) + ); } @Override public IntervalYearMonth sub(Interval interval) { - return new IntervalYearMonth(interval().minus(interval.interval()).normalized(), - Intervals.compatibleInterval(dataType(), interval.dataType())); + return new IntervalYearMonth( + interval().minus(interval.interval()).normalized(), + Intervals.compatibleInterval(dataType(), interval.dataType()) + ); } @Override diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/literal/interval/Intervals.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/literal/interval/Intervals.java index 0b5d0073806ab..cfc59c6ee63de 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/literal/interval/Intervals.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/literal/interval/Intervals.java @@ -50,7 +50,13 @@ public final class Intervals { * for exposing it in ODBC. */ public enum TimeUnit { - YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, MILLISECOND; + YEAR, + MONTH, + DAY, + HOUR, + MINUTE, + SECOND, + MILLISECOND; } private Intervals() {} @@ -199,7 +205,6 @@ private static String intervalUnit(char unitChar) { } } - // // String parsers // @@ -302,16 +307,24 @@ TemporalAmount parse(Source source, String string) { if (token.optional) { break; } - throw new ParsingException(source, invalidIntervalMessage(string) + ": incorrect format, expecting {}", - Strings.collectionToDelimitedString(tokens, "")); + throw new ParsingException( + source, + invalidIntervalMessage(string) + ": incorrect format, expecting {}", + Strings.collectionToDelimitedString(tokens, "") + ); } // char token if (token.ch != 0) { char found = string.charAt(startToken); if (found != token.ch) { - throw new ParsingException(source, invalidIntervalMessage(string) + ": expected [{}] (at [{}]) but found [{}]", - token.ch, startToken, found); + throw new ParsingException( + source, + invalidIntervalMessage(string) + ": expected [{}] (at [{}]) but found [{}]", + token.ch, + startToken, + found + ); } startToken++; } @@ -322,25 +335,33 @@ TemporalAmount parse(Source source, String string) { } if (endToken == startToken) { - throw new ParsingException(source, - invalidIntervalMessage(string) + ": expected digit (at [{}]) but found [{}]", - endToken, string.charAt(endToken)); + throw new ParsingException( + source, + invalidIntervalMessage(string) + ": expected digit (at [{}]) but found [{}]", + endToken, + string.charAt(endToken) + ); } String number = string.substring(startToken, endToken); try { long v = StringUtils.parseLong(number); if (token.maxValue > 0 && v > token.maxValue) { - throw new ParsingException(source, - invalidIntervalMessage(string) - + ": [{}] unit has illegal value [{}], expected a positive number up to [{}]", - units.get(unitIndex).name(), v, token.maxValue); + throw new ParsingException( + source, + invalidIntervalMessage(string) + + ": [{}] unit has illegal value [{}], expected a positive number up to [{}]", + units.get(unitIndex).name(), + v, + token.maxValue + ); } if (v < 0) { - throw new ParsingException(source, - invalidIntervalMessage(string) - + ": negative value [{}] not allowed (negate the entire interval instead)", - v); + throw new ParsingException( + source, + invalidIntervalMessage(string) + ": negative value [{}] not allowed (negate the entire interval instead)", + v + ); } if (units.get(unitIndex) == TimeUnit.MILLISECOND && number.length() < 3) { // normalize the number past DOT to millis @@ -355,8 +376,11 @@ TemporalAmount parse(Source source, String string) { } if (endToken <= string.length() - 1) { - throw new ParsingException(source, invalidIntervalMessage(string) + ": unexpected trailing characters found [{}]", - string.substring(endToken)); + throw new ParsingException( + source, + invalidIntervalMessage(string) + ": unexpected trailing characters found [{}]", + string.substring(endToken) + ); } TemporalAmount interval = units.get(0) == TimeUnit.YEAR || units.get(0) == TimeUnit.MONTH ? Period.ZERO : Duration.ZERO; @@ -414,35 +438,35 @@ public static TemporalAmount negate(TemporalAmount interval) { PARSERS.put(INTERVAL_DAY, new ParserBuilder(INTERVAL_DAY).unit(TimeUnit.DAY).build()); PARSERS.put(INTERVAL_HOUR, new ParserBuilder(INTERVAL_HOUR).unit(TimeUnit.HOUR).build()); PARSERS.put(INTERVAL_MINUTE, new ParserBuilder(INTERVAL_MINUTE).unit(TimeUnit.MINUTE).build()); - PARSERS.put(INTERVAL_SECOND, new ParserBuilder(INTERVAL_SECOND) - .unit(TimeUnit.SECOND) - .optional() - .separator(DOT).unit(TimeUnit.MILLISECOND, MAX_MILLI) - .build()); + PARSERS.put( + INTERVAL_SECOND, + new ParserBuilder(INTERVAL_SECOND).unit(TimeUnit.SECOND).optional().separator(DOT).unit(TimeUnit.MILLISECOND, MAX_MILLI).build() + ); // patterns - PARSERS.put(INTERVAL_YEAR_TO_MONTH, new ParserBuilder(INTERVAL_YEAR_TO_MONTH) - .unit(TimeUnit.YEAR) - .separator(MINUS) - .unit(TimeUnit.MONTH, MAX_MONTH) - .build()); - - PARSERS.put(INTERVAL_DAY_TO_HOUR, new ParserBuilder(INTERVAL_DAY_TO_HOUR) - .unit(TimeUnit.DAY) - .separator(SPACE) - .unit(TimeUnit.HOUR, MAX_HOUR) - .build()); - - PARSERS.put(INTERVAL_DAY_TO_MINUTE, new ParserBuilder(INTERVAL_DAY_TO_MINUTE) - .unit(TimeUnit.DAY) + PARSERS.put( + INTERVAL_YEAR_TO_MONTH, + new ParserBuilder(INTERVAL_YEAR_TO_MONTH).unit(TimeUnit.YEAR).separator(MINUS).unit(TimeUnit.MONTH, MAX_MONTH).build() + ); + + PARSERS.put( + INTERVAL_DAY_TO_HOUR, + new ParserBuilder(INTERVAL_DAY_TO_HOUR).unit(TimeUnit.DAY).separator(SPACE).unit(TimeUnit.HOUR, MAX_HOUR).build() + ); + + PARSERS.put( + INTERVAL_DAY_TO_MINUTE, + new ParserBuilder(INTERVAL_DAY_TO_MINUTE).unit(TimeUnit.DAY) .separator(SPACE) .unit(TimeUnit.HOUR, MAX_HOUR) .separator(COLON) .unit(TimeUnit.MINUTE, MAX_MINUTE) - .build()); + .build() + ); - PARSERS.put(INTERVAL_DAY_TO_SECOND, new ParserBuilder(INTERVAL_DAY_TO_SECOND) - .unit(TimeUnit.DAY) + PARSERS.put( + INTERVAL_DAY_TO_SECOND, + new ParserBuilder(INTERVAL_DAY_TO_SECOND).unit(TimeUnit.DAY) .separator(SPACE) .unit(TimeUnit.HOUR, MAX_HOUR) .separator(COLON) @@ -450,32 +474,39 @@ public static TemporalAmount negate(TemporalAmount interval) { .separator(COLON) .unit(TimeUnit.SECOND, MAX_SECOND) .optional() - .separator(DOT).unit(TimeUnit.MILLISECOND, MAX_MILLI) - .build()); - - PARSERS.put(INTERVAL_HOUR_TO_MINUTE, new ParserBuilder(INTERVAL_HOUR_TO_MINUTE) - .unit(TimeUnit.HOUR) - .separator(COLON) - .unit(TimeUnit.MINUTE, MAX_MINUTE) - .build()); - - PARSERS.put(INTERVAL_HOUR_TO_SECOND, new ParserBuilder(INTERVAL_HOUR_TO_SECOND) - .unit(TimeUnit.HOUR) + .separator(DOT) + .unit(TimeUnit.MILLISECOND, MAX_MILLI) + .build() + ); + + PARSERS.put( + INTERVAL_HOUR_TO_MINUTE, + new ParserBuilder(INTERVAL_HOUR_TO_MINUTE).unit(TimeUnit.HOUR).separator(COLON).unit(TimeUnit.MINUTE, MAX_MINUTE).build() + ); + + PARSERS.put( + INTERVAL_HOUR_TO_SECOND, + new ParserBuilder(INTERVAL_HOUR_TO_SECOND).unit(TimeUnit.HOUR) .separator(COLON) .unit(TimeUnit.MINUTE, MAX_MINUTE) .separator(COLON) .unit(TimeUnit.SECOND, MAX_SECOND) .optional() - .separator(DOT).unit(TimeUnit.MILLISECOND, MAX_MILLI) - .build()); - - PARSERS.put(INTERVAL_MINUTE_TO_SECOND, new ParserBuilder(INTERVAL_MINUTE_TO_SECOND) - .unit(TimeUnit.MINUTE) + .separator(DOT) + .unit(TimeUnit.MILLISECOND, MAX_MILLI) + .build() + ); + + PARSERS.put( + INTERVAL_MINUTE_TO_SECOND, + new ParserBuilder(INTERVAL_MINUTE_TO_SECOND).unit(TimeUnit.MINUTE) .separator(COLON) .unit(TimeUnit.SECOND, MAX_SECOND) .optional() - .separator(DOT).unit(TimeUnit.MILLISECOND, MAX_MILLI) - .build()); + .separator(DOT) + .unit(TimeUnit.MILLISECOND, MAX_MILLI) + .build() + ); } public static TemporalAmount parseInterval(Source source, String value, DataType intervalType) { diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/conditional/ArbitraryConditionalFunction.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/conditional/ArbitraryConditionalFunction.java index 418dd188d7799..94ccb2c4e231e 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/conditional/ArbitraryConditionalFunction.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/conditional/ArbitraryConditionalFunction.java @@ -45,7 +45,7 @@ public ScriptTemplate asScript() { templates.add(asScript(ex)); } - StringJoiner template = new StringJoiner(",", "{sql}." + operation.scriptMethodName() +"([", "])"); + StringJoiner template = new StringJoiner(",", "{sql}." + operation.scriptMethodName() + "([", "])"); ParamsBuilder params = paramsBuilder(); for (ScriptTemplate scriptTemplate : templates) { diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/conditional/Case.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/conditional/Case.java index d9cc3db87286e..3671fcd3f8859 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/conditional/Case.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/conditional/Case.java @@ -90,26 +90,41 @@ protected TypeResolution resolveType() { for (IfConditional conditional : conditions) { if (conditional.condition().dataType() != DataTypes.BOOLEAN) { - return new TypeResolution(format(null, "condition of [{}] must be [boolean], found value [{}] type [{}]", - conditional.sourceText(), - Expressions.name(conditional.condition()), - conditional.condition().dataType().typeName())); + return new TypeResolution( + format( + null, + "condition of [{}] must be [boolean], found value [{}] type [{}]", + conditional.sourceText(), + Expressions.name(conditional.condition()), + conditional.condition().dataType().typeName() + ) + ); } if (SqlDataTypes.areCompatible(expectedResultDataType, conditional.dataType()) == false) { - return new TypeResolution(format(null, "result of [{}] must be [{}], found value [{}] type [{}]", - conditional.sourceText(), - expectedResultDataType.typeName(), - Expressions.name(conditional.result()), - conditional.dataType().typeName())); + return new TypeResolution( + format( + null, + "result of [{}] must be [{}], found value [{}] type [{}]", + conditional.sourceText(), + expectedResultDataType.typeName(), + Expressions.name(conditional.result()), + conditional.dataType().typeName() + ) + ); } } if (SqlDataTypes.areCompatible(expectedResultDataType, elseResult.dataType()) == false) { - return new TypeResolution(format(null, "ELSE clause of [{}] must be [{}], found value [{}] type [{}]", - elseResult.sourceText(), - expectedResultDataType.typeName(), - Expressions.name(elseResult), - elseResult.dataType().typeName())); + return new TypeResolution( + format( + null, + "ELSE clause of [{}] must be [{}], found value [{}] type [{}]", + elseResult.sourceText(), + expectedResultDataType.typeName(), + Expressions.name(elseResult), + elseResult.dataType().typeName() + ) + ); } return TypeResolution.TYPE_RESOLVED; diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/conditional/ConditionalFunction.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/conditional/ConditionalFunction.java index c87eb3805c595..980dc32142a72 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/conditional/ConditionalFunction.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/conditional/ConditionalFunction.java @@ -62,12 +62,17 @@ protected TypeResolution resolveType() { } } else { if (SqlDataTypes.areCompatible(dt, child.dataType()) == false) { - return new TypeResolution(format(null, "{} argument of [{}] must be [{}], found value [{}] type [{}]", - ordinal(i + 1), - sourceText(), - dt.typeName(), - Expressions.name(child), - child.dataType().typeName())); + return new TypeResolution( + format( + null, + "{} argument of [{}] must be [{}], found value [{}] type [{}]", + ordinal(i + 1), + sourceText(), + dt.typeName(), + Expressions.name(child), + child.dataType().typeName() + ) + ); } } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/conditional/ConditionalProcessor.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/conditional/ConditionalProcessor.java index 1972229e4667d..a4e82b739eb1e 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/conditional/ConditionalProcessor.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/conditional/ConditionalProcessor.java @@ -26,7 +26,6 @@ public enum ConditionalOperation implements Function, Object> GREATEST(Conditionals::greatest, Conditionals::greatestInput), LEAST(Conditionals::least, Conditionals::leastInput); - String scriptMethodName() { return name().toLowerCase(Locale.ROOT); } @@ -34,8 +33,7 @@ String scriptMethodName() { private final Function, Object> process; private final BiFunction, Object, Object> inputProcess; - ConditionalOperation(Function, Object> process, - BiFunction, Object, Object> inputProcess) { + ConditionalOperation(Function, Object> process, BiFunction, Object, Object> inputProcess) { this.process = process; this.inputProcess = inputProcess; } @@ -91,8 +89,7 @@ public boolean equals(Object o) { } ConditionalProcessor that = (ConditionalProcessor) o; - return Objects.equals(processors, that.processors) && - operation == that.operation; + return Objects.equals(processors, that.processors) && operation == that.operation; } @Override diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/conditional/IfConditional.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/conditional/IfConditional.java index 096a66275e429..adda6ca10438c 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/conditional/IfConditional.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/conditional/IfConditional.java @@ -75,8 +75,7 @@ public int hashCode() { public boolean equals(Object o) { if (super.equals(o)) { IfConditional that = (IfConditional) o; - return Objects.equals(condition, that.condition) - && Objects.equals(result, that.result); + return Objects.equals(condition, that.condition) && Objects.equals(result, that.result); } return false; } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/conditional/Iif.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/conditional/Iif.java index 44326e3329931..4fac420767ec5 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/conditional/Iif.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/conditional/Iif.java @@ -49,22 +49,23 @@ protected TypeResolution resolveType() { return TypeResolution.TYPE_RESOLVED; } - TypeResolution conditionTypeResolution = isBoolean( - conditions().get(0).condition(), - sourceText(), - FIRST - ); + TypeResolution conditionTypeResolution = isBoolean(conditions().get(0).condition(), sourceText(), FIRST); if (conditionTypeResolution.unresolved()) { return conditionTypeResolution; } DataType resultDataType = conditions().get(0).dataType(); if (SqlDataTypes.areCompatible(resultDataType, elseResult().dataType()) == false) { - return new TypeResolution(format(null, "third argument of [{}] must be [{}], found value [{}] type [{}]", - sourceText(), - resultDataType.typeName(), - Expressions.name(elseResult()), - elseResult().dataType().typeName())); + return new TypeResolution( + format( + null, + "third argument of [{}] must be [{}], found value [{}] type [{}]", + sourceText(), + resultDataType.typeName(), + Expressions.name(elseResult()), + elseResult().dataType().typeName() + ) + ); } return TypeResolution.TYPE_RESOLVED; } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/conditional/NullIf.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/conditional/NullIf.java index bca6852c87a38..821c533d629dd 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/conditional/NullIf.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/conditional/NullIf.java @@ -79,7 +79,6 @@ public ScriptTemplate asScript() { @Override protected Pipe makePipe() { - return new NullIfPipe(source(), this, - Expressions.pipe(children().get(0)), Expressions.pipe(children().get(1))); + return new NullIfPipe(source(), this, Expressions.pipe(children().get(0)), Expressions.pipe(children().get(1))); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/conditional/NullIfProcessor.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/conditional/NullIfProcessor.java index eb53fc2984826..9ba9b0faccada 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/conditional/NullIfProcessor.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/conditional/NullIfProcessor.java @@ -22,7 +22,6 @@ public class NullIfProcessor implements Processor { private final Processor leftProcessor; private final Processor rightProcessor; - public NullIfProcessor(Processor leftProcessor, Processor rightProcessor) { this.leftProcessor = leftProcessor; this.rightProcessor = rightProcessor; @@ -67,8 +66,7 @@ public boolean equals(Object o) { return false; } NullIfProcessor that = (NullIfProcessor) o; - return Objects.equals(leftProcessor, that.leftProcessor) && - Objects.equals(rightProcessor, that.rightProcessor); + return Objects.equals(leftProcessor, that.leftProcessor) && Objects.equals(rightProcessor, that.rightProcessor); } @Override diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/operator/arithmetic/DateTimeArithmeticOperation.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/operator/arithmetic/DateTimeArithmeticOperation.java index c115e70749b1b..1bdc02f778e8b 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/operator/arithmetic/DateTimeArithmeticOperation.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/operator/arithmetic/DateTimeArithmeticOperation.java @@ -60,7 +60,7 @@ protected TypeResolution resolveWithIntervals() { DataType r = right().dataType(); if ((SqlDataTypes.isDateOrTimeBased(r) || SqlDataTypes.isInterval(r) || DataTypes.isNull(r)) == false - || (SqlDataTypes.isDateOrTimeBased(l) || SqlDataTypes.isInterval(l) || DataTypes.isNull(l)) == false) { + || (SqlDataTypes.isDateOrTimeBased(l) || SqlDataTypes.isInterval(l) || DataTypes.isNull(l)) == false) { return new TypeResolution(format(null, "[{}] has arguments with incompatible types [{}] and [{}]", symbol(), l, r)); } return TypeResolution.TYPE_RESOLVED; diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/operator/arithmetic/SqlBinaryArithmeticOperation.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/operator/arithmetic/SqlBinaryArithmeticOperation.java index 35c1412cd54d9..780ed998ff94a 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/operator/arithmetic/SqlBinaryArithmeticOperation.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/operator/arithmetic/SqlBinaryArithmeticOperation.java @@ -24,7 +24,6 @@ import java.time.temporal.Temporal; import java.util.function.BiFunction; - public enum SqlBinaryArithmeticOperation implements BinaryArithmeticOperation { ADD((Object l, Object r) -> { @@ -50,8 +49,11 @@ public enum SqlBinaryArithmeticOperation implements BinaryArithmeticOperation { return IntervalArithmetics.add((Temporal) r, ((IntervalDayTime) l).interval()); } - throw new QlIllegalArgumentException("Cannot compute [+] between [{}] and [{}]", l.getClass().getSimpleName(), - r.getClass().getSimpleName()); + throw new QlIllegalArgumentException( + "Cannot compute [+] between [{}] and [{}]", + l.getClass().getSimpleName(), + r.getClass().getSimpleName() + ); }, "+"), SUB((Object l, Object r) -> { if (l instanceof Number) { @@ -69,12 +71,15 @@ public enum SqlBinaryArithmeticOperation implements BinaryArithmeticOperation { if ((l instanceof ZonedDateTime || l instanceof OffsetTime) && r instanceof IntervalDayTime) { return IntervalArithmetics.sub((Temporal) l, ((IntervalDayTime) r).interval()); } - if ((r instanceof ZonedDateTime || r instanceof OffsetTime) && l instanceof Interval) { + if ((r instanceof ZonedDateTime || r instanceof OffsetTime) && l instanceof Interval) { throw new QlIllegalArgumentException("Cannot subtract a date from an interval; do you mean the reverse?"); } - throw new QlIllegalArgumentException("Cannot compute [-] between [{}] and [{}]", l.getClass().getSimpleName(), - r.getClass().getSimpleName()); + throw new QlIllegalArgumentException( + "Cannot compute [-] between [{}] and [{}]", + l.getClass().getSimpleName(), + r.getClass().getSimpleName() + ); }, "-"), MUL((Object l, Object r) -> { if (l instanceof Number && r instanceof Number) { @@ -93,8 +98,11 @@ public enum SqlBinaryArithmeticOperation implements BinaryArithmeticOperation { return ((IntervalDayTime) l).mul(((Number) r).longValue()); } - throw new QlIllegalArgumentException("Cannot compute [*] between [{}] and [{}]", l.getClass().getSimpleName(), - r.getClass().getSimpleName()); + throw new QlIllegalArgumentException( + "Cannot compute [*] between [{}] and [{}]", + l.getClass().getSimpleName(), + r.getClass().getSimpleName() + ); }, "*"), DIV(Arithmetics::div, "/"), MOD(Arithmetics::mod, "%"); diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/operator/arithmetic/Sub.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/operator/arithmetic/Sub.java index d700cd23c2e74..1bba6cb700963 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/operator/arithmetic/Sub.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/operator/arithmetic/Sub.java @@ -40,8 +40,15 @@ protected TypeResolution resolveWithIntervals() { return resolution; } if ((SqlDataTypes.isDateOrTimeBased(right().dataType())) && SqlDataTypes.isInterval(left().dataType())) { - return new TypeResolution(format(null, "Cannot subtract a {}[{}] from an interval[{}]; do you mean the reverse?", - right().dataType().typeName(), right().source().text(), left().source().text())); + return new TypeResolution( + format( + null, + "Cannot subtract a {}[{}] from an interval[{}]; do you mean the reverse?", + right().dataType().typeName(), + right().source().text(), + left().source().text() + ) + ); } return TypeResolution.TYPE_RESOLVED; } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/optimizer/Optimizer.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/optimizer/Optimizer.java index 29fbaaca0c9d9..5fd8c9564440a 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/optimizer/Optimizer.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/optimizer/Optimizer.java @@ -109,7 +109,6 @@ import static org.elasticsearch.xpack.ql.optimizer.OptimizerRules.PushDownAndCombineFilters; import static org.elasticsearch.xpack.ql.util.CollectionUtils.combine; - public class Optimizer extends RuleExecutor { public ExecutionInfo debugOptimize(LogicalPlan verified) { @@ -122,72 +121,73 @@ public LogicalPlan optimize(LogicalPlan verified) { @Override protected Iterable.Batch> batches() { - Batch substitutions = new Batch("Substitutions", Limiter.ONCE, - new RewritePivot(), - new ReplaceRegexMatch(), - new ReplaceAggregatesWithLiterals() - ); - - Batch refs = new Batch("Replace References", Limiter.ONCE, - new ReplaceReferenceAttributeWithSource() - ); - - Batch operators = new Batch("Operator Optimization", - // combining - new CombineProjections(), - // folding - new ReplaceFoldableAttributes(), - new FoldNull(), - new ReplaceAggregationsInLocalRelations(), - new ConstantFolding(), - new SimplifyConditional(), - new SimplifyCase(), - // boolean - new BooleanSimplification(), - new LiteralsOnTheRight(), - new BinaryComparisonSimplification(), - // needs to occur before BinaryComparison combinations (see class) - new PropagateEquals(), - new PropagateNullable(), - new CombineBinaryComparisons(), - new CombineDisjunctionsToIn(), - new SimplifyComparisonsArithmetics(SqlDataTypes::areCompatible), - // prune/elimination - new PruneDuplicatesInGroupBy(), - new PruneFilters(), - new PruneOrderByForImplicitGrouping(), - new PruneLiteralsInOrderBy(), - new PruneOrderByNestedFields(), - new PruneCast(), - // order by alignment of the aggs - new SortAggregateOnOrderBy(), - // ReplaceAggregationsInLocalRelations, ConstantFolding and PruneFilters must all be applied before this: - new PushDownAndCombineFilters() + Batch substitutions = new Batch( + "Substitutions", + Limiter.ONCE, + new RewritePivot(), + new ReplaceRegexMatch(), + new ReplaceAggregatesWithLiterals() + ); + + Batch refs = new Batch("Replace References", Limiter.ONCE, new ReplaceReferenceAttributeWithSource()); + + Batch operators = new Batch( + "Operator Optimization", + // combining + new CombineProjections(), + // folding + new ReplaceFoldableAttributes(), + new FoldNull(), + new ReplaceAggregationsInLocalRelations(), + new ConstantFolding(), + new SimplifyConditional(), + new SimplifyCase(), + // boolean + new BooleanSimplification(), + new LiteralsOnTheRight(), + new BinaryComparisonSimplification(), + // needs to occur before BinaryComparison combinations (see class) + new PropagateEquals(), + new PropagateNullable(), + new CombineBinaryComparisons(), + new CombineDisjunctionsToIn(), + new SimplifyComparisonsArithmetics(SqlDataTypes::areCompatible), + // prune/elimination + new PruneDuplicatesInGroupBy(), + new PruneFilters(), + new PruneOrderByForImplicitGrouping(), + new PruneLiteralsInOrderBy(), + new PruneOrderByNestedFields(), + new PruneCast(), + // order by alignment of the aggs + new SortAggregateOnOrderBy(), + // ReplaceAggregationsInLocalRelations, ConstantFolding and PruneFilters must all be applied before this: + new PushDownAndCombineFilters() + ); + + Batch aggregate = new Batch( + "Aggregation Rewrite", + new ReplaceMinMaxWithTopHits(), + new ReplaceAggsWithMatrixStats(), + new ReplaceAggsWithExtendedStats(), + new ReplaceAggsWithStats(), + new ReplaceSumWithStats(), + new PromoteStatsToExtendedStats(), + new ReplaceAggsWithPercentiles(), + new ReplaceAggsWithPercentileRanks() + ); + + Batch local = new Batch( + "Skip Elasticsearch", + new SkipQueryOnLimitZero(), + new SkipQueryForLiteralAggregations(), + new PushProjectionsIntoLocalRelation(), + // must run after `PushProjectionsIntoLocalRelation` because it removes the distinction between implicit + // and explicit groupings + new PruneLiteralsInGroupBy() ); - Batch aggregate = new Batch("Aggregation Rewrite", - new ReplaceMinMaxWithTopHits(), - new ReplaceAggsWithMatrixStats(), - new ReplaceAggsWithExtendedStats(), - new ReplaceAggsWithStats(), - new ReplaceSumWithStats(), - new PromoteStatsToExtendedStats(), - new ReplaceAggsWithPercentiles(), - new ReplaceAggsWithPercentileRanks() - ); - - Batch local = new Batch("Skip Elasticsearch", - new SkipQueryOnLimitZero(), - new SkipQueryForLiteralAggregations(), - new PushProjectionsIntoLocalRelation(), - // must run after `PushProjectionsIntoLocalRelation` because it removes the distinction between implicit - // and explicit groupings - new PruneLiteralsInGroupBy() - ); - - Batch label = new Batch("Set as Optimized", Limiter.ONCE, - CleanAliases.INSTANCE, - new SetAsOptimized()); + Batch label = new Batch("Set as Optimized", Limiter.ONCE, CleanAliases.INSTANCE, new SetAsOptimized()); return Arrays.asList(substitutions, refs, operators, aggregate, local, label); } @@ -205,8 +205,12 @@ protected LogicalPlan rule(Pivot plan) { } // fallback - should not happen else { - UnresolvedAttribute attr = new UnresolvedAttribute(namedExpression.source(), namedExpression.name(), null, - "Unexpected alias"); + UnresolvedAttribute attr = new UnresolvedAttribute( + namedExpression.source(), + namedExpression.name(), + null, + "Unexpected alias" + ); return new Pivot(plan.source(), plan.child(), plan.column(), singletonList(attr), plan.aggregates()); } } @@ -364,8 +368,7 @@ protected LogicalPlan rule(Project project) { // projection has no nested field references, remove any nested orders if (nestedTopFields.isEmpty()) { orders.removeAll(nestedOrders.values()); - } - else { + } else { // remove orders that are not ancestors of the nested projections for (Entry entry : nestedOrders.entrySet()) { String parent = entry.getKey(); @@ -462,7 +465,7 @@ protected LogicalPlan rule(OrderBy ob) { // Check if the groupings (a, y) match the orderings (b, x) through the aggregates' aliases (x, y) // e.g. SELECT a AS x, b AS y ... GROUP BY a, y ORDER BY b, x if ((equalsAsAttribute(child, group) - && (equalsAsAttribute(alias, fieldToOrder) || equalsAsAttribute(child, fieldToOrder))) + && (equalsAsAttribute(alias, fieldToOrder) || equalsAsAttribute(child, fieldToOrder))) || (equalsAsAttribute(alias, group) && (equalsAsAttribute(alias, fieldToOrder) || equalsAsAttribute(child, fieldToOrder)))) { isMatching.set(Boolean.TRUE); @@ -559,7 +562,7 @@ protected LogicalPlan rule(UnaryPlan plan) { // for example an alias defined in the lower list might be referred in the upper - without replacing it the alias becomes invalid private List combineProjections(List upper, List lower) { - //TODO: this need rewriting when moving functions of NamedExpression + // TODO: this need rewriting when moving functions of NamedExpression // collect aliases in the lower list AttributeMap.Builder aliasesBuilder = AttributeMap.builder(); @@ -582,7 +585,6 @@ private List combineProjections(List } } - // replace attributes of foldable expressions with the foldable trees // SELECT 5 a, 3 + 2 b ... WHERE a < 10 ORDER BY b @@ -648,11 +650,11 @@ protected LogicalPlan rule(LogicalPlan plan) { private boolean canPropagateFoldable(LogicalPlan p) { return p instanceof Project - || p instanceof Filter - || p instanceof SubQueryAlias - || p instanceof Aggregate - || p instanceof Limit - || p instanceof OrderBy; + || p instanceof Filter + || p instanceof SubQueryAlias + || p instanceof Aggregate + || p instanceof Limit + || p instanceof OrderBy; } } @@ -681,8 +683,8 @@ protected Expression rule(Expression e) { } } else if (e instanceof Alias == false - && e.nullable() == Nullability.TRUE - && Expressions.anyMatch(e.children(), Expressions::isNull)) { + && e.nullable() == Nullability.TRUE + && Expressions.anyMatch(e.children(), Expressions::isNull)) { return Literal.of(e, null); } return e; @@ -1089,16 +1091,24 @@ static class ReplaceAggsWithPercentiles extends OptimizerBasicRule { public LogicalPlan apply(LogicalPlan p) { Map> percentsPerAggKey = new LinkedHashMap<>(); - p.forEachExpressionUp(Percentile.class, per -> - percentsPerAggKey.computeIfAbsent(new PercentileKey(per), v -> new LinkedHashSet<>()).add(per.percent()) + p.forEachExpressionUp( + Percentile.class, + per -> percentsPerAggKey.computeIfAbsent(new PercentileKey(per), v -> new LinkedHashSet<>()).add(per.percent()) ); // create a Percentile agg for each agg key Map percentilesPerAggKey = new LinkedHashMap<>(); - percentsPerAggKey.forEach((aggKey, percents) -> percentilesPerAggKey.put( - aggKey, - new Percentiles(percents.iterator().next().source(), aggKey.field(), new ArrayList<>(percents), - aggKey.percentilesConfig()))); + percentsPerAggKey.forEach( + (aggKey, percents) -> percentilesPerAggKey.put( + aggKey, + new Percentiles( + percents.iterator().next().source(), + aggKey.field(), + new ArrayList<>(percents), + aggKey.percentilesConfig() + ) + ) + ); return p.transformExpressionsUp(Percentile.class, per -> { PercentileKey a = new PercentileKey(per); @@ -1114,16 +1124,24 @@ static class ReplaceAggsWithPercentileRanks extends OptimizerBasicRule { public LogicalPlan apply(LogicalPlan p) { final Map> valuesPerAggKey = new LinkedHashMap<>(); - p.forEachExpressionUp(PercentileRank.class, per -> - valuesPerAggKey.computeIfAbsent(new PercentileKey(per), v -> new LinkedHashSet<>()).add(per.value()) + p.forEachExpressionUp( + PercentileRank.class, + per -> valuesPerAggKey.computeIfAbsent(new PercentileKey(per), v -> new LinkedHashSet<>()).add(per.value()) ); // create a PercentileRank agg for each agg key Map ranksPerAggKey = new LinkedHashMap<>(); - valuesPerAggKey.forEach((aggKey, values) -> ranksPerAggKey.put( - aggKey, - new PercentileRanks(values.iterator().next().source(), aggKey.field(), new ArrayList<>(values), - aggKey.percentilesConfig()))); + valuesPerAggKey.forEach( + (aggKey, values) -> ranksPerAggKey.put( + aggKey, + new PercentileRanks( + values.iterator().next().source(), + aggKey.field(), + new ArrayList<>(values), + aggKey.percentilesConfig() + ) + ) + ); return p.transformExpressionsUp(PercentileRank.class, per -> { PercentileRanks ranks = ranksPerAggKey.get(new PercentileKey(per)); diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/CommandBuilder.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/CommandBuilder.java index 44c8792691f0a..b5a2a787029a8 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/CommandBuilder.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/CommandBuilder.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.sql.parser; import org.antlr.v4.runtime.Token; -import org.elasticsearch.core.Booleans; import org.elasticsearch.common.Strings; +import org.elasticsearch.core.Booleans; import org.elasticsearch.xpack.ql.expression.Literal; import org.elasticsearch.xpack.ql.index.IndexResolver; import org.elasticsearch.xpack.ql.index.IndexResolver.IndexType; @@ -69,8 +69,7 @@ public Command visitDebug(DebugContext ctx) { if (ctx.type != null) { if (ctx.type.getType() == SqlBaseLexer.ANALYZED) { type = Debug.Type.ANALYZED; - } - else { + } else { type = Debug.Type.OPTIMIZED; } } @@ -80,7 +79,6 @@ public Command visitDebug(DebugContext ctx) { return new Debug(source, plan(ctx.statement()), type, format); } - @Override public Command visitExplain(ExplainContext ctx) { Source source = source(ctx); @@ -133,8 +131,14 @@ public Object visitShowFunctions(ShowFunctionsContext ctx) { public Object visitShowTables(ShowTablesContext ctx) { TableIdentifier ti = visitTableIdentifier(ctx.tableIdent); String index = ti != null ? ti.qualifiedIndex() : null; - return new ShowTables(source(ctx), visitLikePattern(ctx.clusterLike), visitString(ctx.cluster), index, - visitLikePattern(ctx.tableLike), ctx.FROZEN() != null); + return new ShowTables( + source(ctx), + visitLikePattern(ctx.clusterLike), + visitString(ctx.cluster), + index, + visitLikePattern(ctx.tableLike), + ctx.FROZEN() != null + ); } @Override @@ -190,8 +194,13 @@ public SysTables visitSysTables(SysTablesContext ctx) { public Object visitSysColumns(SysColumnsContext ctx) { TableIdentifier ti = visitTableIdentifier(ctx.tableIdent); String index = ti != null ? ti.qualifiedIndex() : null; - return new SysColumns(source(ctx), string(ctx.cluster), index, visitLikePattern(ctx.tableLike), - visitLikePattern(ctx.columnPattern)); + return new SysColumns( + source(ctx), + string(ctx.cluster), + index, + visitLikePattern(ctx.tableLike), + visitLikePattern(ctx.columnPattern) + ); } @Override diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/ExpressionBuilder.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/ExpressionBuilder.java index 39eb429ef7d41..fbbfe16d92ede 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/ExpressionBuilder.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/ExpressionBuilder.java @@ -177,8 +177,12 @@ public Expression visitSelectExpression(SelectExpressionContext ctx) { @Override public Expression visitStar(StarContext ctx) { - return new UnresolvedStar(source(ctx), ctx.qualifiedName() != null ? - new UnresolvedAttribute(source(ctx.qualifiedName()), visitQualifiedName(ctx.qualifiedName())) : null); + return new UnresolvedStar( + source(ctx), + ctx.qualifiedName() != null + ? new UnresolvedAttribute(source(ctx.qualifiedName()), visitQualifiedName(ctx.qualifiedName())) + : null + ); } @Override @@ -290,23 +294,36 @@ public LikePattern visitPattern(PatternContext ctx) { escape = escapeString.charAt(0); // these chars already have a meaning if (escape == '%' || escape == '_') { - throw new ParsingException(source(escapeCtx.escape), - "Char [{}] cannot be used for escaping as it's one of the wildcard chars [%_]", escape); + throw new ParsingException( + source(escapeCtx.escape), + "Char [{}] cannot be used for escaping as it's one of the wildcard chars [%_]", + escape + ); } // lastly validate that escape chars (if present) are followed by special chars for (int i = 0; i < pattern.length(); i++) { char current = pattern.charAt(i); if (current == escape) { if (i + 1 == pattern.length()) { - throw new ParsingException(source(ctx.value), - "Pattern [{}] is invalid as escape char [{}] at position {} does not escape anything", pattern, escape, - i); + throw new ParsingException( + source(ctx.value), + "Pattern [{}] is invalid as escape char [{}] at position {} does not escape anything", + pattern, + escape, + i + ); } char next = pattern.charAt(i + 1); if (next != '%' && next != '_') { - throw new ParsingException(source(ctx.value), - "Pattern [{}] is invalid as escape char [{}] at position {} can only escape " - + "wildcard chars [%_]; found [{}]", pattern, escape, i, next); + throw new ParsingException( + source(ctx.value), + "Pattern [{}] is invalid as escape char [{}] at position {} can only escape " + + "wildcard chars [%_]; found [{}]", + pattern, + escape, + i, + next + ); } } } @@ -316,7 +333,6 @@ public LikePattern visitPattern(PatternContext ctx) { return new LikePattern(pattern, escape); } - // // Arithmetic // @@ -371,19 +387,27 @@ public Object visitStringQuery(StringQueryContext ctx) { @Override public Object visitMatchQuery(MatchQueryContext ctx) { - return new MatchQueryPredicate(source(ctx), new UnresolvedAttribute(source(ctx.singleField), - visitQualifiedName(ctx.singleField)), string(ctx.queryString), getQueryOptions(ctx.matchQueryOptions())); + return new MatchQueryPredicate( + source(ctx), + new UnresolvedAttribute(source(ctx.singleField), visitQualifiedName(ctx.singleField)), + string(ctx.queryString), + getQueryOptions(ctx.matchQueryOptions()) + ); } @Override public Object visitMultiMatchQuery(MultiMatchQueryContext ctx) { - return new MultiMatchQueryPredicate(source(ctx), string(ctx.multiFields), string(ctx.queryString), - getQueryOptions(ctx.matchQueryOptions())); + return new MultiMatchQueryPredicate( + source(ctx), + string(ctx.multiFields), + string(ctx.queryString), + getQueryOptions(ctx.matchQueryOptions()) + ); } private String getQueryOptions(MatchQueryOptionsContext optionsCtx) { StringJoiner sj = new StringJoiner(";"); - for (StringContext sc: optionsCtx.string()) { + for (StringContext sc : optionsCtx.string()) { sj.add(string(sc)); } return sj.toString(); @@ -391,9 +415,12 @@ private String getQueryOptions(MatchQueryOptionsContext optionsCtx) { @Override public Order visitOrderBy(OrderByContext ctx) { - return new Order(source(ctx), expression(ctx.expression()), - ctx.DESC() != null ? Order.OrderDirection.DESC : Order.OrderDirection.ASC, - ctx.NULLS() != null ? (ctx.FIRST() != null ? NullsPosition.FIRST : NullsPosition.LAST) : null); + return new Order( + source(ctx), + expression(ctx.expression()), + ctx.DESC() != null ? Order.OrderDirection.DESC : Order.OrderDirection.ASC, + ctx.NULLS() != null ? (ctx.FIRST() != null ? NullsPosition.FIRST : NullsPosition.LAST) : null + ); } @Override @@ -434,8 +461,12 @@ public Object visitCastOperatorExpression(SqlBaseParser.CastOperatorExpressionCo public Function visitExtractExpression(ExtractExpressionContext ctx) { ExtractTemplateContext template = ctx.extractTemplate(); String fieldString = visitIdentifier(template.field); - return new UnresolvedFunction(source(template), fieldString, - SqlFunctionResolution.EXTRACT, singletonList(expression(template.valueExpression()))); + return new UnresolvedFunction( + source(template), + fieldString, + SqlFunctionResolution.EXTRACT, + singletonList(expression(template.valueExpression())) + ); } @Override @@ -474,8 +505,13 @@ public Object visitCase(SqlBaseParser.CaseContext ctx) { List expressions = new ArrayList<>(ctx.whenClause().size()); for (SqlBaseParser.WhenClauseContext when : ctx.whenClause()) { if (ctx.operand != null) { - expressions.add(new IfConditional(source(when), - new Equals(source(when), expression(ctx.operand), expression(when.condition), zoneId), expression(when.result))); + expressions.add( + new IfConditional( + source(when), + new Equals(source(when), expression(ctx.operand), expression(when.condition), zoneId), + expression(when.result) + ) + ); } else { expressions.add(new IfConditional(source(when), expression(when.condition), expression(when.result))); } @@ -493,7 +529,6 @@ public Expression visitParenthesizedExpression(ParenthesizedExpressionContext ct return expression(ctx.expression()); } - // // Logical constructs // @@ -519,12 +554,10 @@ public Object visitLogicalBinary(LogicalBinaryContext ctx) { throw new ParsingException(source, "Don't know how to parse {}", ctx); } - // // Literal // - @Override public Expression visitNullLiteral(NullLiteralContext ctx) { return new Literal(source(ctx), null, DataTypes.NULL); @@ -539,15 +572,23 @@ public Literal visitInterval(IntervalContext interval) { // only YEAR TO MONTH or DAY TO HOUR/MINUTE/SECOND are valid declaration if (trailing != null) { if (leading == TimeUnit.YEAR && trailing != TimeUnit.MONTH) { - throw new ParsingException(source(interval.trailing), - "Invalid interval declaration; YEAR trailing unit required to be MONTH, received {}", trailing); + throw new ParsingException( + source(interval.trailing), + "Invalid interval declaration; YEAR trailing unit required to be MONTH, received {}", + trailing + ); } else { if (trailing.ordinal() <= leading.ordinal()) { EnumSet range = EnumSet.range(leading, TimeUnit.SECOND); range.remove(leading); - throw new ParsingException(source(interval.trailing), - "Invalid interval declaration; trailing unit [{}] needs to be smaller than leading unit[{}], " - + "expected one of {}", trailing, leading, range); + throw new ParsingException( + source(interval.trailing), + "Invalid interval declaration; trailing unit [{}] needs to be smaller than leading unit[{}], " + + "expected one of {}", + trailing, + leading, + range + ); } } } @@ -574,7 +615,6 @@ public Literal visitInterval(IntervalContext interval) { } } - // negation inside the interval negative ^= interval.sign != null && interval.sign.getType() == SqlBaseParser.MINUS; @@ -582,17 +622,21 @@ public Literal visitInterval(IntervalContext interval) { if (interval.valueNumeric != null) { if (trailing != null) { - throw new ParsingException(source(interval.trailing), - "Invalid interval declaration; trailing unit [{}] specified but the value is with numeric (single unit), " - + "use the string notation instead", trailing); + throw new ParsingException( + source(interval.trailing), + "Invalid interval declaration; trailing unit [{}] specified but the value is with numeric (single unit), " + + "use the string notation instead", + trailing + ); } value = of(interval.valueNumeric, leading); } else { value = of(interval.valuePattern, negative, intervalType); } - Interval timeInterval = value instanceof Period ? new IntervalYearMonth((Period) value, - intervalType) : new IntervalDayTime((Duration) value, intervalType); + Interval timeInterval = value instanceof Period + ? new IntervalYearMonth((Period) value, intervalType) + : new IntervalDayTime((Duration) value, intervalType); return new Literal(source(interval), timeInterval, timeInterval.dataType()); } @@ -655,9 +699,9 @@ public Expression visitBooleanLiteral(BooleanLiteralContext ctx) { boolean value; try { value = Booleans.parseBoolean(ctx.getText().toLowerCase(Locale.ROOT), false); - } catch(IllegalArgumentException iae) { + } catch (IllegalArgumentException iae) { throw new ParsingException(source(ctx), iae.getMessage()); - } + } return new Literal(source(ctx), Boolean.valueOf(value), DataTypes.BOOLEAN); } @@ -718,8 +762,13 @@ public Literal visitParamLiteral(ParamLiteralContext ctx) { try { sourceType = DataTypes.fromJava(param.value); } catch (QlIllegalArgumentException ex) { - throw new ParsingException(ex, source, "Unexpected actual parameter type [{}] for type [{}]", param.value.getClass().getName(), - param.type); + throw new ParsingException( + ex, + source, + "Unexpected actual parameter type [{}] for type [{}]", + param.value.getClass().getName(), + param.type + ); } if (sourceType == dataType) { // no conversion is required if the value is already have correct type @@ -728,8 +777,13 @@ public Literal visitParamLiteral(ParamLiteralContext ctx) { // otherwise we need to make sure that xcontent-serialized value is converted to the correct type try { if (canConvert(sourceType, dataType) == false) { - throw new ParsingException(source, "Cannot cast value [{}] of type [{}] to parameter type [{}]", param.value, sourceType, - dataType); + throw new ParsingException( + source, + "Cannot cast value [{}] of type [{}] to parameter type [{}]", + param.value, + sourceType, + dataType + ); } return new Literal(source, converterFor(sourceType, dataType).convert(param.value), dataType); } catch (QlIllegalArgumentException ex) { @@ -778,7 +832,7 @@ public Literal visitDateEscapedLiteral(DateEscapedLiteralContext ctx) { // parse yyyy-MM-dd (time optional but is set to 00:00:00.000 because of the conversion to DATE try { return new Literal(source, asDateOnly(string), SqlDataTypes.DATE); - } catch(DateTimeParseException ex) { + } catch (DateTimeParseException ex) { throw new ParsingException(source, "Invalid date received; {}", ex.getMessage()); } } @@ -829,8 +883,13 @@ public Literal visitGuidEscapedLiteral(GuidEscapedLiteralContext ctx) { int[] separatorPos = { 8, 13, 18, 23 }; for (int pos : separatorPos) { if (lowerCase.charAt(pos) != '-') { - throw new ParsingException(source, "{}expected group separator at offset [{}], found [{}]", - errorPrefix, pos, string.charAt(pos)); + throw new ParsingException( + source, + "{}expected group separator at offset [{}], found [{}]", + errorPrefix, + pos, + string.charAt(pos) + ); } } @@ -907,7 +966,7 @@ private static Tuple minusAwareSource(SqlBaseParser.NumberConte } } } - // Intervals and SysTypes can only have a single "-" as parentheses are not allowed there + // Intervals and SysTypes can only have a single "-" as parentheses are not allowed there } else if (parentCtx instanceof IntervalContext) { IntervalContext ic = (IntervalContext) parentCtx; if (ic.sign != null && ic.sign.getType() == SqlBaseParser.MINUS) { diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/LogicalPlanBuilder.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/LogicalPlanBuilder.java index 7d1be389807a9..f63a0a284f40c 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/LogicalPlanBuilder.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/LogicalPlanBuilder.java @@ -119,8 +119,10 @@ public LogicalPlan visitQueryNoWith(QueryNoWithContext ctx) { Token limit = limitClause.limit; if (limit != null && limitClause.INTEGER_VALUE() != null) { if (plan instanceof Limit) { - throw new ParsingException(source(limitClause), - "TOP and LIMIT are not allowed in the same query - use one or the other"); + throw new ParsingException( + source(limitClause), + "TOP and LIMIT are not allowed in the same query - use one or the other" + ); } else { plan = limit(plan, source(limitClause), limit); } @@ -160,8 +162,7 @@ public LogicalPlan visitQuerySpecification(QuerySpecificationContext ctx) { List groupBy = expressions(groupingElement); ParserRuleContext endSource = groupingElement.isEmpty() ? groupByCtx : groupingElement.get(groupingElement.size() - 1); query = new Aggregate(source(ctx.GROUP(), endSource), query, groupBy, selectTarget); - } - else if (selectTarget.isEmpty() == false) { + } else if (selectTarget.isEmpty() == false) { query = new Project(source(ctx.selectItems()), query, selectTarget); } @@ -187,9 +188,7 @@ else if (selectTarget.isEmpty() == false) { public LogicalPlan visitFromClause(FromClauseContext ctx) { // if there are multiple FROM clauses, convert each pair in a inner join List plans = plans(ctx.relation()); - LogicalPlan plan = plans.stream() - .reduce((left, right) -> new Join(source(ctx), left, right, Join.JoinType.IMPLICIT, null)) - .get(); + LogicalPlan plan = plans.stream().reduce((left, right) -> new Join(source(ctx), left, right, Join.JoinType.IMPLICIT, null)).get(); // PIVOT if (ctx.pivotClause() != null) { @@ -197,8 +196,11 @@ public LogicalPlan visitFromClause(FromClauseContext ctx) { UnresolvedAttribute column = new UnresolvedAttribute(source(pivotClause.column), visitQualifiedName(pivotClause.column)); List values = namedValues(pivotClause.aggs); if (values.size() > 1) { - throw new ParsingException(source(pivotClause.aggs), "PIVOT currently supports only one aggregation, found [{}]", - values.size()); + throw new ParsingException( + source(pivotClause.aggs), + "PIVOT currently supports only one aggregation, found [{}]", + values.size() + ); } plan = new Pivot(source(pivotClause), plan, column, namedValues(pivotClause.vals), namedValues(pivotClause.aggs)); } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/SqlBaseBaseListener.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/SqlBaseBaseListener.java index a0e87bd707c70..2c5662dda01f2 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/SqlBaseBaseListener.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/SqlBaseBaseListener.java @@ -11,1277 +11,1699 @@ * of the available methods. */ class SqlBaseBaseListener implements SqlBaseListener { - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterSingleStatement(SqlBaseParser.SingleStatementContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitSingleStatement(SqlBaseParser.SingleStatementContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterSingleExpression(SqlBaseParser.SingleExpressionContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitSingleExpression(SqlBaseParser.SingleExpressionContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterStatementDefault(SqlBaseParser.StatementDefaultContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitStatementDefault(SqlBaseParser.StatementDefaultContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterExplain(SqlBaseParser.ExplainContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitExplain(SqlBaseParser.ExplainContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterDebug(SqlBaseParser.DebugContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitDebug(SqlBaseParser.DebugContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterShowTables(SqlBaseParser.ShowTablesContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitShowTables(SqlBaseParser.ShowTablesContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterShowColumns(SqlBaseParser.ShowColumnsContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitShowColumns(SqlBaseParser.ShowColumnsContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterShowFunctions(SqlBaseParser.ShowFunctionsContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitShowFunctions(SqlBaseParser.ShowFunctionsContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterShowSchemas(SqlBaseParser.ShowSchemasContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitShowSchemas(SqlBaseParser.ShowSchemasContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterShowCatalogs(SqlBaseParser.ShowCatalogsContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitShowCatalogs(SqlBaseParser.ShowCatalogsContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterSysTables(SqlBaseParser.SysTablesContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitSysTables(SqlBaseParser.SysTablesContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterSysColumns(SqlBaseParser.SysColumnsContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitSysColumns(SqlBaseParser.SysColumnsContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterSysTypes(SqlBaseParser.SysTypesContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitSysTypes(SqlBaseParser.SysTypesContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterQuery(SqlBaseParser.QueryContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitQuery(SqlBaseParser.QueryContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterQueryNoWith(SqlBaseParser.QueryNoWithContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitQueryNoWith(SqlBaseParser.QueryNoWithContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterLimitClause(SqlBaseParser.LimitClauseContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitLimitClause(SqlBaseParser.LimitClauseContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterQueryPrimaryDefault(SqlBaseParser.QueryPrimaryDefaultContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitQueryPrimaryDefault(SqlBaseParser.QueryPrimaryDefaultContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterSubquery(SqlBaseParser.SubqueryContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitSubquery(SqlBaseParser.SubqueryContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterOrderBy(SqlBaseParser.OrderByContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitOrderBy(SqlBaseParser.OrderByContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterQuerySpecification(SqlBaseParser.QuerySpecificationContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitQuerySpecification(SqlBaseParser.QuerySpecificationContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterFromClause(SqlBaseParser.FromClauseContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitFromClause(SqlBaseParser.FromClauseContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterGroupBy(SqlBaseParser.GroupByContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitGroupBy(SqlBaseParser.GroupByContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterSingleGroupingSet(SqlBaseParser.SingleGroupingSetContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitSingleGroupingSet(SqlBaseParser.SingleGroupingSetContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterGroupingExpressions(SqlBaseParser.GroupingExpressionsContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitGroupingExpressions(SqlBaseParser.GroupingExpressionsContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterNamedQuery(SqlBaseParser.NamedQueryContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitNamedQuery(SqlBaseParser.NamedQueryContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterTopClause(SqlBaseParser.TopClauseContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitTopClause(SqlBaseParser.TopClauseContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterSetQuantifier(SqlBaseParser.SetQuantifierContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitSetQuantifier(SqlBaseParser.SetQuantifierContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterSelectItems(SqlBaseParser.SelectItemsContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitSelectItems(SqlBaseParser.SelectItemsContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterSelectExpression(SqlBaseParser.SelectExpressionContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitSelectExpression(SqlBaseParser.SelectExpressionContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterRelation(SqlBaseParser.RelationContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitRelation(SqlBaseParser.RelationContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterJoinRelation(SqlBaseParser.JoinRelationContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitJoinRelation(SqlBaseParser.JoinRelationContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterJoinType(SqlBaseParser.JoinTypeContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitJoinType(SqlBaseParser.JoinTypeContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterJoinCriteria(SqlBaseParser.JoinCriteriaContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitJoinCriteria(SqlBaseParser.JoinCriteriaContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterTableName(SqlBaseParser.TableNameContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitTableName(SqlBaseParser.TableNameContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterAliasedQuery(SqlBaseParser.AliasedQueryContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitAliasedQuery(SqlBaseParser.AliasedQueryContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterAliasedRelation(SqlBaseParser.AliasedRelationContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitAliasedRelation(SqlBaseParser.AliasedRelationContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterPivotClause(SqlBaseParser.PivotClauseContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitPivotClause(SqlBaseParser.PivotClauseContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterPivotArgs(SqlBaseParser.PivotArgsContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitPivotArgs(SqlBaseParser.PivotArgsContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterNamedValueExpression(SqlBaseParser.NamedValueExpressionContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitNamedValueExpression(SqlBaseParser.NamedValueExpressionContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterExpression(SqlBaseParser.ExpressionContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitExpression(SqlBaseParser.ExpressionContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterLogicalNot(SqlBaseParser.LogicalNotContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitLogicalNot(SqlBaseParser.LogicalNotContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterStringQuery(SqlBaseParser.StringQueryContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitStringQuery(SqlBaseParser.StringQueryContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterBooleanDefault(SqlBaseParser.BooleanDefaultContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitBooleanDefault(SqlBaseParser.BooleanDefaultContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterExists(SqlBaseParser.ExistsContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitExists(SqlBaseParser.ExistsContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterMultiMatchQuery(SqlBaseParser.MultiMatchQueryContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitMultiMatchQuery(SqlBaseParser.MultiMatchQueryContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterMatchQuery(SqlBaseParser.MatchQueryContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitMatchQuery(SqlBaseParser.MatchQueryContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterLogicalBinary(SqlBaseParser.LogicalBinaryContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitLogicalBinary(SqlBaseParser.LogicalBinaryContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterMatchQueryOptions(SqlBaseParser.MatchQueryOptionsContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitMatchQueryOptions(SqlBaseParser.MatchQueryOptionsContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterPredicated(SqlBaseParser.PredicatedContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitPredicated(SqlBaseParser.PredicatedContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterPredicate(SqlBaseParser.PredicateContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitPredicate(SqlBaseParser.PredicateContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterLikePattern(SqlBaseParser.LikePatternContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitLikePattern(SqlBaseParser.LikePatternContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterPattern(SqlBaseParser.PatternContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitPattern(SqlBaseParser.PatternContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterPatternEscape(SqlBaseParser.PatternEscapeContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitPatternEscape(SqlBaseParser.PatternEscapeContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterValueExpressionDefault(SqlBaseParser.ValueExpressionDefaultContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitValueExpressionDefault(SqlBaseParser.ValueExpressionDefaultContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterComparison(SqlBaseParser.ComparisonContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitComparison(SqlBaseParser.ComparisonContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterArithmeticBinary(SqlBaseParser.ArithmeticBinaryContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitArithmeticBinary(SqlBaseParser.ArithmeticBinaryContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterArithmeticUnary(SqlBaseParser.ArithmeticUnaryContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitArithmeticUnary(SqlBaseParser.ArithmeticUnaryContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterDereference(SqlBaseParser.DereferenceContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitDereference(SqlBaseParser.DereferenceContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterCast(SqlBaseParser.CastContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitCast(SqlBaseParser.CastContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterConstantDefault(SqlBaseParser.ConstantDefaultContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitConstantDefault(SqlBaseParser.ConstantDefaultContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterExtract(SqlBaseParser.ExtractContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitExtract(SqlBaseParser.ExtractContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterParenthesizedExpression(SqlBaseParser.ParenthesizedExpressionContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitParenthesizedExpression(SqlBaseParser.ParenthesizedExpressionContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterStar(SqlBaseParser.StarContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitStar(SqlBaseParser.StarContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterCastOperatorExpression(SqlBaseParser.CastOperatorExpressionContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitCastOperatorExpression(SqlBaseParser.CastOperatorExpressionContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterFunction(SqlBaseParser.FunctionContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitFunction(SqlBaseParser.FunctionContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterCurrentDateTimeFunction(SqlBaseParser.CurrentDateTimeFunctionContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitCurrentDateTimeFunction(SqlBaseParser.CurrentDateTimeFunctionContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterSubqueryExpression(SqlBaseParser.SubqueryExpressionContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitSubqueryExpression(SqlBaseParser.SubqueryExpressionContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterCase(SqlBaseParser.CaseContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitCase(SqlBaseParser.CaseContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterBuiltinDateTimeFunction(SqlBaseParser.BuiltinDateTimeFunctionContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitBuiltinDateTimeFunction(SqlBaseParser.BuiltinDateTimeFunctionContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterCastExpression(SqlBaseParser.CastExpressionContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitCastExpression(SqlBaseParser.CastExpressionContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterCastTemplate(SqlBaseParser.CastTemplateContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitCastTemplate(SqlBaseParser.CastTemplateContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterConvertTemplate(SqlBaseParser.ConvertTemplateContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitConvertTemplate(SqlBaseParser.ConvertTemplateContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterExtractExpression(SqlBaseParser.ExtractExpressionContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitExtractExpression(SqlBaseParser.ExtractExpressionContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterExtractTemplate(SqlBaseParser.ExtractTemplateContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitExtractTemplate(SqlBaseParser.ExtractTemplateContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterFunctionExpression(SqlBaseParser.FunctionExpressionContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitFunctionExpression(SqlBaseParser.FunctionExpressionContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterFunctionTemplate(SqlBaseParser.FunctionTemplateContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitFunctionTemplate(SqlBaseParser.FunctionTemplateContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterFunctionName(SqlBaseParser.FunctionNameContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitFunctionName(SqlBaseParser.FunctionNameContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterNullLiteral(SqlBaseParser.NullLiteralContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitNullLiteral(SqlBaseParser.NullLiteralContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterIntervalLiteral(SqlBaseParser.IntervalLiteralContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitIntervalLiteral(SqlBaseParser.IntervalLiteralContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterNumericLiteral(SqlBaseParser.NumericLiteralContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitNumericLiteral(SqlBaseParser.NumericLiteralContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterBooleanLiteral(SqlBaseParser.BooleanLiteralContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitBooleanLiteral(SqlBaseParser.BooleanLiteralContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterStringLiteral(SqlBaseParser.StringLiteralContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitStringLiteral(SqlBaseParser.StringLiteralContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterParamLiteral(SqlBaseParser.ParamLiteralContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitParamLiteral(SqlBaseParser.ParamLiteralContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterDateEscapedLiteral(SqlBaseParser.DateEscapedLiteralContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitDateEscapedLiteral(SqlBaseParser.DateEscapedLiteralContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterTimeEscapedLiteral(SqlBaseParser.TimeEscapedLiteralContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitTimeEscapedLiteral(SqlBaseParser.TimeEscapedLiteralContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterTimestampEscapedLiteral(SqlBaseParser.TimestampEscapedLiteralContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitTimestampEscapedLiteral(SqlBaseParser.TimestampEscapedLiteralContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterGuidEscapedLiteral(SqlBaseParser.GuidEscapedLiteralContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitGuidEscapedLiteral(SqlBaseParser.GuidEscapedLiteralContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterComparisonOperator(SqlBaseParser.ComparisonOperatorContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitComparisonOperator(SqlBaseParser.ComparisonOperatorContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterBooleanValue(SqlBaseParser.BooleanValueContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitBooleanValue(SqlBaseParser.BooleanValueContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterInterval(SqlBaseParser.IntervalContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitInterval(SqlBaseParser.IntervalContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterIntervalField(SqlBaseParser.IntervalFieldContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitIntervalField(SqlBaseParser.IntervalFieldContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterPrimitiveDataType(SqlBaseParser.PrimitiveDataTypeContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitPrimitiveDataType(SqlBaseParser.PrimitiveDataTypeContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterQualifiedName(SqlBaseParser.QualifiedNameContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitQualifiedName(SqlBaseParser.QualifiedNameContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterIdentifier(SqlBaseParser.IdentifierContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitIdentifier(SqlBaseParser.IdentifierContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterTableIdentifier(SqlBaseParser.TableIdentifierContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitTableIdentifier(SqlBaseParser.TableIdentifierContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterQuotedIdentifier(SqlBaseParser.QuotedIdentifierContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitQuotedIdentifier(SqlBaseParser.QuotedIdentifierContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterBackQuotedIdentifier(SqlBaseParser.BackQuotedIdentifierContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitBackQuotedIdentifier(SqlBaseParser.BackQuotedIdentifierContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterUnquotedIdentifier(SqlBaseParser.UnquotedIdentifierContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitUnquotedIdentifier(SqlBaseParser.UnquotedIdentifierContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterDigitIdentifier(SqlBaseParser.DigitIdentifierContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitDigitIdentifier(SqlBaseParser.DigitIdentifierContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterDecimalLiteral(SqlBaseParser.DecimalLiteralContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitDecimalLiteral(SqlBaseParser.DecimalLiteralContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterIntegerLiteral(SqlBaseParser.IntegerLiteralContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitIntegerLiteral(SqlBaseParser.IntegerLiteralContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterString(SqlBaseParser.StringContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitString(SqlBaseParser.StringContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterWhenClause(SqlBaseParser.WhenClauseContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitWhenClause(SqlBaseParser.WhenClauseContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterNonReserved(SqlBaseParser.NonReservedContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitNonReserved(SqlBaseParser.NonReservedContext ctx) { } - - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void enterEveryRule(ParserRuleContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void exitEveryRule(ParserRuleContext ctx) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void visitTerminal(TerminalNode node) { } - /** - * {@inheritDoc} - * - *

    The default implementation does nothing.

    - */ - @Override public void visitErrorNode(ErrorNode node) { } + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterSingleStatement(SqlBaseParser.SingleStatementContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitSingleStatement(SqlBaseParser.SingleStatementContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterSingleExpression(SqlBaseParser.SingleExpressionContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitSingleExpression(SqlBaseParser.SingleExpressionContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterStatementDefault(SqlBaseParser.StatementDefaultContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitStatementDefault(SqlBaseParser.StatementDefaultContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterExplain(SqlBaseParser.ExplainContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitExplain(SqlBaseParser.ExplainContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterDebug(SqlBaseParser.DebugContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitDebug(SqlBaseParser.DebugContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterShowTables(SqlBaseParser.ShowTablesContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitShowTables(SqlBaseParser.ShowTablesContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterShowColumns(SqlBaseParser.ShowColumnsContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitShowColumns(SqlBaseParser.ShowColumnsContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterShowFunctions(SqlBaseParser.ShowFunctionsContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitShowFunctions(SqlBaseParser.ShowFunctionsContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterShowSchemas(SqlBaseParser.ShowSchemasContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitShowSchemas(SqlBaseParser.ShowSchemasContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterShowCatalogs(SqlBaseParser.ShowCatalogsContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitShowCatalogs(SqlBaseParser.ShowCatalogsContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterSysTables(SqlBaseParser.SysTablesContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitSysTables(SqlBaseParser.SysTablesContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterSysColumns(SqlBaseParser.SysColumnsContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitSysColumns(SqlBaseParser.SysColumnsContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterSysTypes(SqlBaseParser.SysTypesContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitSysTypes(SqlBaseParser.SysTypesContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterQuery(SqlBaseParser.QueryContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitQuery(SqlBaseParser.QueryContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterQueryNoWith(SqlBaseParser.QueryNoWithContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitQueryNoWith(SqlBaseParser.QueryNoWithContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterLimitClause(SqlBaseParser.LimitClauseContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitLimitClause(SqlBaseParser.LimitClauseContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterQueryPrimaryDefault(SqlBaseParser.QueryPrimaryDefaultContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitQueryPrimaryDefault(SqlBaseParser.QueryPrimaryDefaultContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterSubquery(SqlBaseParser.SubqueryContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitSubquery(SqlBaseParser.SubqueryContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterOrderBy(SqlBaseParser.OrderByContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitOrderBy(SqlBaseParser.OrderByContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterQuerySpecification(SqlBaseParser.QuerySpecificationContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitQuerySpecification(SqlBaseParser.QuerySpecificationContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterFromClause(SqlBaseParser.FromClauseContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitFromClause(SqlBaseParser.FromClauseContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterGroupBy(SqlBaseParser.GroupByContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitGroupBy(SqlBaseParser.GroupByContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterSingleGroupingSet(SqlBaseParser.SingleGroupingSetContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitSingleGroupingSet(SqlBaseParser.SingleGroupingSetContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterGroupingExpressions(SqlBaseParser.GroupingExpressionsContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitGroupingExpressions(SqlBaseParser.GroupingExpressionsContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterNamedQuery(SqlBaseParser.NamedQueryContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitNamedQuery(SqlBaseParser.NamedQueryContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterTopClause(SqlBaseParser.TopClauseContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitTopClause(SqlBaseParser.TopClauseContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterSetQuantifier(SqlBaseParser.SetQuantifierContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitSetQuantifier(SqlBaseParser.SetQuantifierContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterSelectItems(SqlBaseParser.SelectItemsContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitSelectItems(SqlBaseParser.SelectItemsContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterSelectExpression(SqlBaseParser.SelectExpressionContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitSelectExpression(SqlBaseParser.SelectExpressionContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterRelation(SqlBaseParser.RelationContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitRelation(SqlBaseParser.RelationContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterJoinRelation(SqlBaseParser.JoinRelationContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitJoinRelation(SqlBaseParser.JoinRelationContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterJoinType(SqlBaseParser.JoinTypeContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitJoinType(SqlBaseParser.JoinTypeContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterJoinCriteria(SqlBaseParser.JoinCriteriaContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitJoinCriteria(SqlBaseParser.JoinCriteriaContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterTableName(SqlBaseParser.TableNameContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitTableName(SqlBaseParser.TableNameContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterAliasedQuery(SqlBaseParser.AliasedQueryContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitAliasedQuery(SqlBaseParser.AliasedQueryContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterAliasedRelation(SqlBaseParser.AliasedRelationContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitAliasedRelation(SqlBaseParser.AliasedRelationContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterPivotClause(SqlBaseParser.PivotClauseContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitPivotClause(SqlBaseParser.PivotClauseContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterPivotArgs(SqlBaseParser.PivotArgsContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitPivotArgs(SqlBaseParser.PivotArgsContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterNamedValueExpression(SqlBaseParser.NamedValueExpressionContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitNamedValueExpression(SqlBaseParser.NamedValueExpressionContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterExpression(SqlBaseParser.ExpressionContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitExpression(SqlBaseParser.ExpressionContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterLogicalNot(SqlBaseParser.LogicalNotContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitLogicalNot(SqlBaseParser.LogicalNotContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterStringQuery(SqlBaseParser.StringQueryContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitStringQuery(SqlBaseParser.StringQueryContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterBooleanDefault(SqlBaseParser.BooleanDefaultContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitBooleanDefault(SqlBaseParser.BooleanDefaultContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterExists(SqlBaseParser.ExistsContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitExists(SqlBaseParser.ExistsContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterMultiMatchQuery(SqlBaseParser.MultiMatchQueryContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitMultiMatchQuery(SqlBaseParser.MultiMatchQueryContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterMatchQuery(SqlBaseParser.MatchQueryContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitMatchQuery(SqlBaseParser.MatchQueryContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterLogicalBinary(SqlBaseParser.LogicalBinaryContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitLogicalBinary(SqlBaseParser.LogicalBinaryContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterMatchQueryOptions(SqlBaseParser.MatchQueryOptionsContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitMatchQueryOptions(SqlBaseParser.MatchQueryOptionsContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterPredicated(SqlBaseParser.PredicatedContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitPredicated(SqlBaseParser.PredicatedContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterPredicate(SqlBaseParser.PredicateContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitPredicate(SqlBaseParser.PredicateContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterLikePattern(SqlBaseParser.LikePatternContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitLikePattern(SqlBaseParser.LikePatternContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterPattern(SqlBaseParser.PatternContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitPattern(SqlBaseParser.PatternContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterPatternEscape(SqlBaseParser.PatternEscapeContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitPatternEscape(SqlBaseParser.PatternEscapeContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterValueExpressionDefault(SqlBaseParser.ValueExpressionDefaultContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitValueExpressionDefault(SqlBaseParser.ValueExpressionDefaultContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterComparison(SqlBaseParser.ComparisonContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitComparison(SqlBaseParser.ComparisonContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterArithmeticBinary(SqlBaseParser.ArithmeticBinaryContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitArithmeticBinary(SqlBaseParser.ArithmeticBinaryContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterArithmeticUnary(SqlBaseParser.ArithmeticUnaryContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitArithmeticUnary(SqlBaseParser.ArithmeticUnaryContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterDereference(SqlBaseParser.DereferenceContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitDereference(SqlBaseParser.DereferenceContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterCast(SqlBaseParser.CastContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitCast(SqlBaseParser.CastContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterConstantDefault(SqlBaseParser.ConstantDefaultContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitConstantDefault(SqlBaseParser.ConstantDefaultContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterExtract(SqlBaseParser.ExtractContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitExtract(SqlBaseParser.ExtractContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterParenthesizedExpression(SqlBaseParser.ParenthesizedExpressionContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitParenthesizedExpression(SqlBaseParser.ParenthesizedExpressionContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterStar(SqlBaseParser.StarContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitStar(SqlBaseParser.StarContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterCastOperatorExpression(SqlBaseParser.CastOperatorExpressionContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitCastOperatorExpression(SqlBaseParser.CastOperatorExpressionContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterFunction(SqlBaseParser.FunctionContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitFunction(SqlBaseParser.FunctionContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterCurrentDateTimeFunction(SqlBaseParser.CurrentDateTimeFunctionContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitCurrentDateTimeFunction(SqlBaseParser.CurrentDateTimeFunctionContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterSubqueryExpression(SqlBaseParser.SubqueryExpressionContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitSubqueryExpression(SqlBaseParser.SubqueryExpressionContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterCase(SqlBaseParser.CaseContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitCase(SqlBaseParser.CaseContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterBuiltinDateTimeFunction(SqlBaseParser.BuiltinDateTimeFunctionContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitBuiltinDateTimeFunction(SqlBaseParser.BuiltinDateTimeFunctionContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterCastExpression(SqlBaseParser.CastExpressionContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitCastExpression(SqlBaseParser.CastExpressionContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterCastTemplate(SqlBaseParser.CastTemplateContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitCastTemplate(SqlBaseParser.CastTemplateContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterConvertTemplate(SqlBaseParser.ConvertTemplateContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitConvertTemplate(SqlBaseParser.ConvertTemplateContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterExtractExpression(SqlBaseParser.ExtractExpressionContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitExtractExpression(SqlBaseParser.ExtractExpressionContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterExtractTemplate(SqlBaseParser.ExtractTemplateContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitExtractTemplate(SqlBaseParser.ExtractTemplateContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterFunctionExpression(SqlBaseParser.FunctionExpressionContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitFunctionExpression(SqlBaseParser.FunctionExpressionContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterFunctionTemplate(SqlBaseParser.FunctionTemplateContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitFunctionTemplate(SqlBaseParser.FunctionTemplateContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterFunctionName(SqlBaseParser.FunctionNameContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitFunctionName(SqlBaseParser.FunctionNameContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterNullLiteral(SqlBaseParser.NullLiteralContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitNullLiteral(SqlBaseParser.NullLiteralContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterIntervalLiteral(SqlBaseParser.IntervalLiteralContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitIntervalLiteral(SqlBaseParser.IntervalLiteralContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterNumericLiteral(SqlBaseParser.NumericLiteralContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitNumericLiteral(SqlBaseParser.NumericLiteralContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterBooleanLiteral(SqlBaseParser.BooleanLiteralContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitBooleanLiteral(SqlBaseParser.BooleanLiteralContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterStringLiteral(SqlBaseParser.StringLiteralContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitStringLiteral(SqlBaseParser.StringLiteralContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterParamLiteral(SqlBaseParser.ParamLiteralContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitParamLiteral(SqlBaseParser.ParamLiteralContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterDateEscapedLiteral(SqlBaseParser.DateEscapedLiteralContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitDateEscapedLiteral(SqlBaseParser.DateEscapedLiteralContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterTimeEscapedLiteral(SqlBaseParser.TimeEscapedLiteralContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitTimeEscapedLiteral(SqlBaseParser.TimeEscapedLiteralContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterTimestampEscapedLiteral(SqlBaseParser.TimestampEscapedLiteralContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitTimestampEscapedLiteral(SqlBaseParser.TimestampEscapedLiteralContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterGuidEscapedLiteral(SqlBaseParser.GuidEscapedLiteralContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitGuidEscapedLiteral(SqlBaseParser.GuidEscapedLiteralContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterComparisonOperator(SqlBaseParser.ComparisonOperatorContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitComparisonOperator(SqlBaseParser.ComparisonOperatorContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterBooleanValue(SqlBaseParser.BooleanValueContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitBooleanValue(SqlBaseParser.BooleanValueContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterInterval(SqlBaseParser.IntervalContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitInterval(SqlBaseParser.IntervalContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterIntervalField(SqlBaseParser.IntervalFieldContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitIntervalField(SqlBaseParser.IntervalFieldContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterPrimitiveDataType(SqlBaseParser.PrimitiveDataTypeContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitPrimitiveDataType(SqlBaseParser.PrimitiveDataTypeContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterQualifiedName(SqlBaseParser.QualifiedNameContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitQualifiedName(SqlBaseParser.QualifiedNameContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterIdentifier(SqlBaseParser.IdentifierContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitIdentifier(SqlBaseParser.IdentifierContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterTableIdentifier(SqlBaseParser.TableIdentifierContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitTableIdentifier(SqlBaseParser.TableIdentifierContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterQuotedIdentifier(SqlBaseParser.QuotedIdentifierContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitQuotedIdentifier(SqlBaseParser.QuotedIdentifierContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterBackQuotedIdentifier(SqlBaseParser.BackQuotedIdentifierContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitBackQuotedIdentifier(SqlBaseParser.BackQuotedIdentifierContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterUnquotedIdentifier(SqlBaseParser.UnquotedIdentifierContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitUnquotedIdentifier(SqlBaseParser.UnquotedIdentifierContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterDigitIdentifier(SqlBaseParser.DigitIdentifierContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitDigitIdentifier(SqlBaseParser.DigitIdentifierContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterDecimalLiteral(SqlBaseParser.DecimalLiteralContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitDecimalLiteral(SqlBaseParser.DecimalLiteralContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterIntegerLiteral(SqlBaseParser.IntegerLiteralContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitIntegerLiteral(SqlBaseParser.IntegerLiteralContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterString(SqlBaseParser.StringContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitString(SqlBaseParser.StringContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterWhenClause(SqlBaseParser.WhenClauseContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitWhenClause(SqlBaseParser.WhenClauseContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterNonReserved(SqlBaseParser.NonReservedContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitNonReserved(SqlBaseParser.NonReservedContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void enterEveryRule(ParserRuleContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void exitEveryRule(ParserRuleContext ctx) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void visitTerminal(TerminalNode node) {} + + /** + * {@inheritDoc} + * + *

    The default implementation does nothing.

    + */ + @Override + public void visitErrorNode(ErrorNode node) {} } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/SqlBaseBaseVisitor.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/SqlBaseBaseVisitor.java index 720ce36cfd173..eaa1d81ebcf87 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/SqlBaseBaseVisitor.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/SqlBaseBaseVisitor.java @@ -1,5 +1,6 @@ // ANTLR GENERATED CODE: DO NOT EDIT package org.elasticsearch.xpack.sql.parser; + import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor; /** @@ -11,732 +12,1147 @@ * operations with no return type. */ class SqlBaseBaseVisitor extends AbstractParseTreeVisitor implements SqlBaseVisitor { - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitSingleStatement(SqlBaseParser.SingleStatementContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitSingleExpression(SqlBaseParser.SingleExpressionContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitStatementDefault(SqlBaseParser.StatementDefaultContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitExplain(SqlBaseParser.ExplainContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitDebug(SqlBaseParser.DebugContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitShowTables(SqlBaseParser.ShowTablesContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitShowColumns(SqlBaseParser.ShowColumnsContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitShowFunctions(SqlBaseParser.ShowFunctionsContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitShowSchemas(SqlBaseParser.ShowSchemasContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitShowCatalogs(SqlBaseParser.ShowCatalogsContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitSysTables(SqlBaseParser.SysTablesContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitSysColumns(SqlBaseParser.SysColumnsContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitSysTypes(SqlBaseParser.SysTypesContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitQuery(SqlBaseParser.QueryContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitQueryNoWith(SqlBaseParser.QueryNoWithContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitLimitClause(SqlBaseParser.LimitClauseContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitQueryPrimaryDefault(SqlBaseParser.QueryPrimaryDefaultContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitSubquery(SqlBaseParser.SubqueryContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitOrderBy(SqlBaseParser.OrderByContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitQuerySpecification(SqlBaseParser.QuerySpecificationContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitFromClause(SqlBaseParser.FromClauseContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitGroupBy(SqlBaseParser.GroupByContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitSingleGroupingSet(SqlBaseParser.SingleGroupingSetContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitGroupingExpressions(SqlBaseParser.GroupingExpressionsContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitNamedQuery(SqlBaseParser.NamedQueryContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitTopClause(SqlBaseParser.TopClauseContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitSetQuantifier(SqlBaseParser.SetQuantifierContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitSelectItems(SqlBaseParser.SelectItemsContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitSelectExpression(SqlBaseParser.SelectExpressionContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitRelation(SqlBaseParser.RelationContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitJoinRelation(SqlBaseParser.JoinRelationContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitJoinType(SqlBaseParser.JoinTypeContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitJoinCriteria(SqlBaseParser.JoinCriteriaContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitTableName(SqlBaseParser.TableNameContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitAliasedQuery(SqlBaseParser.AliasedQueryContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitAliasedRelation(SqlBaseParser.AliasedRelationContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitPivotClause(SqlBaseParser.PivotClauseContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitPivotArgs(SqlBaseParser.PivotArgsContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitNamedValueExpression(SqlBaseParser.NamedValueExpressionContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitExpression(SqlBaseParser.ExpressionContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitLogicalNot(SqlBaseParser.LogicalNotContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitStringQuery(SqlBaseParser.StringQueryContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitBooleanDefault(SqlBaseParser.BooleanDefaultContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitExists(SqlBaseParser.ExistsContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitMultiMatchQuery(SqlBaseParser.MultiMatchQueryContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitMatchQuery(SqlBaseParser.MatchQueryContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitLogicalBinary(SqlBaseParser.LogicalBinaryContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitMatchQueryOptions(SqlBaseParser.MatchQueryOptionsContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitPredicated(SqlBaseParser.PredicatedContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitPredicate(SqlBaseParser.PredicateContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitLikePattern(SqlBaseParser.LikePatternContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitPattern(SqlBaseParser.PatternContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitPatternEscape(SqlBaseParser.PatternEscapeContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitValueExpressionDefault(SqlBaseParser.ValueExpressionDefaultContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitComparison(SqlBaseParser.ComparisonContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitArithmeticBinary(SqlBaseParser.ArithmeticBinaryContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitArithmeticUnary(SqlBaseParser.ArithmeticUnaryContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitDereference(SqlBaseParser.DereferenceContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitCast(SqlBaseParser.CastContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitConstantDefault(SqlBaseParser.ConstantDefaultContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitExtract(SqlBaseParser.ExtractContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitParenthesizedExpression(SqlBaseParser.ParenthesizedExpressionContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitStar(SqlBaseParser.StarContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitCastOperatorExpression(SqlBaseParser.CastOperatorExpressionContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitFunction(SqlBaseParser.FunctionContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitCurrentDateTimeFunction(SqlBaseParser.CurrentDateTimeFunctionContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitSubqueryExpression(SqlBaseParser.SubqueryExpressionContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitCase(SqlBaseParser.CaseContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitBuiltinDateTimeFunction(SqlBaseParser.BuiltinDateTimeFunctionContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitCastExpression(SqlBaseParser.CastExpressionContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitCastTemplate(SqlBaseParser.CastTemplateContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitConvertTemplate(SqlBaseParser.ConvertTemplateContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitExtractExpression(SqlBaseParser.ExtractExpressionContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitExtractTemplate(SqlBaseParser.ExtractTemplateContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitFunctionExpression(SqlBaseParser.FunctionExpressionContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitFunctionTemplate(SqlBaseParser.FunctionTemplateContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitFunctionName(SqlBaseParser.FunctionNameContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitNullLiteral(SqlBaseParser.NullLiteralContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitIntervalLiteral(SqlBaseParser.IntervalLiteralContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitNumericLiteral(SqlBaseParser.NumericLiteralContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitBooleanLiteral(SqlBaseParser.BooleanLiteralContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitStringLiteral(SqlBaseParser.StringLiteralContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitParamLiteral(SqlBaseParser.ParamLiteralContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitDateEscapedLiteral(SqlBaseParser.DateEscapedLiteralContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitTimeEscapedLiteral(SqlBaseParser.TimeEscapedLiteralContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitTimestampEscapedLiteral(SqlBaseParser.TimestampEscapedLiteralContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitGuidEscapedLiteral(SqlBaseParser.GuidEscapedLiteralContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitComparisonOperator(SqlBaseParser.ComparisonOperatorContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitBooleanValue(SqlBaseParser.BooleanValueContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitInterval(SqlBaseParser.IntervalContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitIntervalField(SqlBaseParser.IntervalFieldContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitPrimitiveDataType(SqlBaseParser.PrimitiveDataTypeContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitQualifiedName(SqlBaseParser.QualifiedNameContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitIdentifier(SqlBaseParser.IdentifierContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitTableIdentifier(SqlBaseParser.TableIdentifierContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitQuotedIdentifier(SqlBaseParser.QuotedIdentifierContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitBackQuotedIdentifier(SqlBaseParser.BackQuotedIdentifierContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitUnquotedIdentifier(SqlBaseParser.UnquotedIdentifierContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitDigitIdentifier(SqlBaseParser.DigitIdentifierContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitDecimalLiteral(SqlBaseParser.DecimalLiteralContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitIntegerLiteral(SqlBaseParser.IntegerLiteralContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitString(SqlBaseParser.StringContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitWhenClause(SqlBaseParser.WhenClauseContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

    The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

    - */ - @Override public T visitNonReserved(SqlBaseParser.NonReservedContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitSingleStatement(SqlBaseParser.SingleStatementContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitSingleExpression(SqlBaseParser.SingleExpressionContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitStatementDefault(SqlBaseParser.StatementDefaultContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitExplain(SqlBaseParser.ExplainContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitDebug(SqlBaseParser.DebugContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitShowTables(SqlBaseParser.ShowTablesContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitShowColumns(SqlBaseParser.ShowColumnsContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitShowFunctions(SqlBaseParser.ShowFunctionsContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitShowSchemas(SqlBaseParser.ShowSchemasContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitShowCatalogs(SqlBaseParser.ShowCatalogsContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitSysTables(SqlBaseParser.SysTablesContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitSysColumns(SqlBaseParser.SysColumnsContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitSysTypes(SqlBaseParser.SysTypesContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitQuery(SqlBaseParser.QueryContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitQueryNoWith(SqlBaseParser.QueryNoWithContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitLimitClause(SqlBaseParser.LimitClauseContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitQueryPrimaryDefault(SqlBaseParser.QueryPrimaryDefaultContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitSubquery(SqlBaseParser.SubqueryContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitOrderBy(SqlBaseParser.OrderByContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitQuerySpecification(SqlBaseParser.QuerySpecificationContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitFromClause(SqlBaseParser.FromClauseContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitGroupBy(SqlBaseParser.GroupByContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitSingleGroupingSet(SqlBaseParser.SingleGroupingSetContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitGroupingExpressions(SqlBaseParser.GroupingExpressionsContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitNamedQuery(SqlBaseParser.NamedQueryContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitTopClause(SqlBaseParser.TopClauseContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitSetQuantifier(SqlBaseParser.SetQuantifierContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitSelectItems(SqlBaseParser.SelectItemsContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitSelectExpression(SqlBaseParser.SelectExpressionContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitRelation(SqlBaseParser.RelationContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitJoinRelation(SqlBaseParser.JoinRelationContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitJoinType(SqlBaseParser.JoinTypeContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitJoinCriteria(SqlBaseParser.JoinCriteriaContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitTableName(SqlBaseParser.TableNameContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitAliasedQuery(SqlBaseParser.AliasedQueryContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitAliasedRelation(SqlBaseParser.AliasedRelationContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitPivotClause(SqlBaseParser.PivotClauseContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitPivotArgs(SqlBaseParser.PivotArgsContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitNamedValueExpression(SqlBaseParser.NamedValueExpressionContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitExpression(SqlBaseParser.ExpressionContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitLogicalNot(SqlBaseParser.LogicalNotContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitStringQuery(SqlBaseParser.StringQueryContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitBooleanDefault(SqlBaseParser.BooleanDefaultContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitExists(SqlBaseParser.ExistsContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitMultiMatchQuery(SqlBaseParser.MultiMatchQueryContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitMatchQuery(SqlBaseParser.MatchQueryContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitLogicalBinary(SqlBaseParser.LogicalBinaryContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitMatchQueryOptions(SqlBaseParser.MatchQueryOptionsContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitPredicated(SqlBaseParser.PredicatedContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitPredicate(SqlBaseParser.PredicateContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitLikePattern(SqlBaseParser.LikePatternContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitPattern(SqlBaseParser.PatternContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitPatternEscape(SqlBaseParser.PatternEscapeContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitValueExpressionDefault(SqlBaseParser.ValueExpressionDefaultContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitComparison(SqlBaseParser.ComparisonContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitArithmeticBinary(SqlBaseParser.ArithmeticBinaryContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitArithmeticUnary(SqlBaseParser.ArithmeticUnaryContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitDereference(SqlBaseParser.DereferenceContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitCast(SqlBaseParser.CastContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitConstantDefault(SqlBaseParser.ConstantDefaultContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitExtract(SqlBaseParser.ExtractContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitParenthesizedExpression(SqlBaseParser.ParenthesizedExpressionContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitStar(SqlBaseParser.StarContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitCastOperatorExpression(SqlBaseParser.CastOperatorExpressionContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitFunction(SqlBaseParser.FunctionContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitCurrentDateTimeFunction(SqlBaseParser.CurrentDateTimeFunctionContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitSubqueryExpression(SqlBaseParser.SubqueryExpressionContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitCase(SqlBaseParser.CaseContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitBuiltinDateTimeFunction(SqlBaseParser.BuiltinDateTimeFunctionContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitCastExpression(SqlBaseParser.CastExpressionContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitCastTemplate(SqlBaseParser.CastTemplateContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitConvertTemplate(SqlBaseParser.ConvertTemplateContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitExtractExpression(SqlBaseParser.ExtractExpressionContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitExtractTemplate(SqlBaseParser.ExtractTemplateContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitFunctionExpression(SqlBaseParser.FunctionExpressionContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitFunctionTemplate(SqlBaseParser.FunctionTemplateContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitFunctionName(SqlBaseParser.FunctionNameContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitNullLiteral(SqlBaseParser.NullLiteralContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitIntervalLiteral(SqlBaseParser.IntervalLiteralContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitNumericLiteral(SqlBaseParser.NumericLiteralContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitBooleanLiteral(SqlBaseParser.BooleanLiteralContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitStringLiteral(SqlBaseParser.StringLiteralContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitParamLiteral(SqlBaseParser.ParamLiteralContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitDateEscapedLiteral(SqlBaseParser.DateEscapedLiteralContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitTimeEscapedLiteral(SqlBaseParser.TimeEscapedLiteralContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitTimestampEscapedLiteral(SqlBaseParser.TimestampEscapedLiteralContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitGuidEscapedLiteral(SqlBaseParser.GuidEscapedLiteralContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitComparisonOperator(SqlBaseParser.ComparisonOperatorContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitBooleanValue(SqlBaseParser.BooleanValueContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitInterval(SqlBaseParser.IntervalContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitIntervalField(SqlBaseParser.IntervalFieldContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitPrimitiveDataType(SqlBaseParser.PrimitiveDataTypeContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitQualifiedName(SqlBaseParser.QualifiedNameContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitIdentifier(SqlBaseParser.IdentifierContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitTableIdentifier(SqlBaseParser.TableIdentifierContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitQuotedIdentifier(SqlBaseParser.QuotedIdentifierContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitBackQuotedIdentifier(SqlBaseParser.BackQuotedIdentifierContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitUnquotedIdentifier(SqlBaseParser.UnquotedIdentifierContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitDigitIdentifier(SqlBaseParser.DigitIdentifierContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitDecimalLiteral(SqlBaseParser.DecimalLiteralContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitIntegerLiteral(SqlBaseParser.IntegerLiteralContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitString(SqlBaseParser.StringContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitWhenClause(SqlBaseParser.WhenClauseContext ctx) { + return visitChildren(ctx); + } + + /** + * {@inheritDoc} + * + *

    The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

    + */ + @Override + public T visitNonReserved(SqlBaseParser.NonReservedContext ctx) { + return visitChildren(ctx); + } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/SqlBaseLexer.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/SqlBaseLexer.java index 21e57bf267115..9be257085a9ac 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/SqlBaseLexer.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/SqlBaseLexer.java @@ -1,595 +1,943 @@ // ANTLR GENERATED CODE: DO NOT EDIT package org.elasticsearch.xpack.sql.parser; -import org.antlr.v4.runtime.Lexer; -import org.antlr.v4.runtime.CharStream; -import org.antlr.v4.runtime.Token; -import org.antlr.v4.runtime.TokenStream; + import org.antlr.v4.runtime.*; +import org.antlr.v4.runtime.CharStream; +import org.antlr.v4.runtime.Lexer; import org.antlr.v4.runtime.atn.*; import org.antlr.v4.runtime.dfa.DFA; import org.antlr.v4.runtime.misc.*; -@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"}) +@SuppressWarnings({ "all", "warnings", "unchecked", "unused", "cast" }) class SqlBaseLexer extends Lexer { - static { RuntimeMetaData.checkVersion("4.9.2", RuntimeMetaData.VERSION); } + static { + RuntimeMetaData.checkVersion("4.9.2", RuntimeMetaData.VERSION); + } - protected static final DFA[] _decisionToDFA; - protected static final PredictionContextCache _sharedContextCache = - new PredictionContextCache(); - public static final int - T__0=1, T__1=2, T__2=3, T__3=4, ALL=5, ANALYZE=6, ANALYZED=7, AND=8, ANY=9, - AS=10, ASC=11, BETWEEN=12, BY=13, CASE=14, CAST=15, CATALOG=16, CATALOGS=17, - COLUMNS=18, CONVERT=19, CURRENT_DATE=20, CURRENT_TIME=21, CURRENT_TIMESTAMP=22, - DAY=23, DAYS=24, DEBUG=25, DESC=26, DESCRIBE=27, DISTINCT=28, ELSE=29, - END=30, ESCAPE=31, EXECUTABLE=32, EXISTS=33, EXPLAIN=34, EXTRACT=35, FALSE=36, - FIRST=37, FOR=38, FORMAT=39, FROM=40, FROZEN=41, FULL=42, FUNCTIONS=43, - GRAPHVIZ=44, GROUP=45, HAVING=46, HOUR=47, HOURS=48, IN=49, INCLUDE=50, - INNER=51, INTERVAL=52, IS=53, JOIN=54, LAST=55, LEFT=56, LIKE=57, LIMIT=58, - MAPPED=59, MATCH=60, MINUTE=61, MINUTES=62, MONTH=63, MONTHS=64, NATURAL=65, - NOT=66, NULL=67, NULLS=68, ON=69, OPTIMIZED=70, OR=71, ORDER=72, OUTER=73, - PARSED=74, PHYSICAL=75, PIVOT=76, PLAN=77, RIGHT=78, RLIKE=79, QUERY=80, - SCHEMAS=81, SECOND=82, SECONDS=83, SELECT=84, SHOW=85, SYS=86, TABLE=87, - TABLES=88, TEXT=89, THEN=90, TRUE=91, TO=92, TOP=93, TYPE=94, TYPES=95, - USING=96, VERIFY=97, WHEN=98, WHERE=99, WITH=100, YEAR=101, YEARS=102, - ESCAPE_ESC=103, FUNCTION_ESC=104, LIMIT_ESC=105, DATE_ESC=106, TIME_ESC=107, - TIMESTAMP_ESC=108, GUID_ESC=109, ESC_START=110, ESC_END=111, EQ=112, NULLEQ=113, - NEQ=114, LT=115, LTE=116, GT=117, GTE=118, PLUS=119, MINUS=120, ASTERISK=121, - SLASH=122, PERCENT=123, CAST_OP=124, DOT=125, PARAM=126, STRING=127, INTEGER_VALUE=128, - DECIMAL_VALUE=129, IDENTIFIER=130, DIGIT_IDENTIFIER=131, TABLE_IDENTIFIER=132, - QUOTED_IDENTIFIER=133, BACKQUOTED_IDENTIFIER=134, SIMPLE_COMMENT=135, - BRACKETED_COMMENT=136, WS=137, UNRECOGNIZED=138; - public static String[] channelNames = { - "DEFAULT_TOKEN_CHANNEL", "HIDDEN" - }; + protected static final DFA[] _decisionToDFA; + protected static final PredictionContextCache _sharedContextCache = new PredictionContextCache(); + public static final int T__0 = 1, T__1 = 2, T__2 = 3, T__3 = 4, ALL = 5, ANALYZE = 6, ANALYZED = 7, AND = 8, ANY = 9, AS = 10, ASC = 11, + BETWEEN = 12, BY = 13, CASE = 14, CAST = 15, CATALOG = 16, CATALOGS = 17, COLUMNS = 18, CONVERT = 19, CURRENT_DATE = 20, + CURRENT_TIME = 21, CURRENT_TIMESTAMP = 22, DAY = 23, DAYS = 24, DEBUG = 25, DESC = 26, DESCRIBE = 27, DISTINCT = 28, ELSE = 29, + END = 30, ESCAPE = 31, EXECUTABLE = 32, EXISTS = 33, EXPLAIN = 34, EXTRACT = 35, FALSE = 36, FIRST = 37, FOR = 38, FORMAT = 39, + FROM = 40, FROZEN = 41, FULL = 42, FUNCTIONS = 43, GRAPHVIZ = 44, GROUP = 45, HAVING = 46, HOUR = 47, HOURS = 48, IN = 49, INCLUDE = + 50, INNER = 51, INTERVAL = 52, IS = 53, JOIN = 54, LAST = 55, LEFT = 56, LIKE = 57, LIMIT = 58, MAPPED = 59, MATCH = 60, + MINUTE = 61, MINUTES = 62, MONTH = 63, MONTHS = 64, NATURAL = 65, NOT = 66, NULL = 67, NULLS = 68, ON = 69, OPTIMIZED = 70, OR = 71, + ORDER = 72, OUTER = 73, PARSED = 74, PHYSICAL = 75, PIVOT = 76, PLAN = 77, RIGHT = 78, RLIKE = 79, QUERY = 80, SCHEMAS = 81, + SECOND = 82, SECONDS = 83, SELECT = 84, SHOW = 85, SYS = 86, TABLE = 87, TABLES = 88, TEXT = 89, THEN = 90, TRUE = 91, TO = 92, + TOP = 93, TYPE = 94, TYPES = 95, USING = 96, VERIFY = 97, WHEN = 98, WHERE = 99, WITH = 100, YEAR = 101, YEARS = 102, ESCAPE_ESC = + 103, FUNCTION_ESC = 104, LIMIT_ESC = 105, DATE_ESC = 106, TIME_ESC = 107, TIMESTAMP_ESC = 108, GUID_ESC = 109, ESC_START = 110, + ESC_END = 111, EQ = 112, NULLEQ = 113, NEQ = 114, LT = 115, LTE = 116, GT = 117, GTE = 118, PLUS = 119, MINUS = 120, ASTERISK = 121, + SLASH = 122, PERCENT = 123, CAST_OP = 124, DOT = 125, PARAM = 126, STRING = 127, INTEGER_VALUE = 128, DECIMAL_VALUE = 129, + IDENTIFIER = 130, DIGIT_IDENTIFIER = 131, TABLE_IDENTIFIER = 132, QUOTED_IDENTIFIER = 133, BACKQUOTED_IDENTIFIER = 134, + SIMPLE_COMMENT = 135, BRACKETED_COMMENT = 136, WS = 137, UNRECOGNIZED = 138; + public static String[] channelNames = { "DEFAULT_TOKEN_CHANNEL", "HIDDEN" }; - public static String[] modeNames = { - "DEFAULT_MODE" - }; + public static String[] modeNames = { "DEFAULT_MODE" }; - private static String[] makeRuleNames() { - return new String[] { - "T__0", "T__1", "T__2", "T__3", "ALL", "ANALYZE", "ANALYZED", "AND", - "ANY", "AS", "ASC", "BETWEEN", "BY", "CASE", "CAST", "CATALOG", "CATALOGS", - "COLUMNS", "CONVERT", "CURRENT_DATE", "CURRENT_TIME", "CURRENT_TIMESTAMP", - "DAY", "DAYS", "DEBUG", "DESC", "DESCRIBE", "DISTINCT", "ELSE", "END", - "ESCAPE", "EXECUTABLE", "EXISTS", "EXPLAIN", "EXTRACT", "FALSE", "FIRST", - "FOR", "FORMAT", "FROM", "FROZEN", "FULL", "FUNCTIONS", "GRAPHVIZ", "GROUP", - "HAVING", "HOUR", "HOURS", "IN", "INCLUDE", "INNER", "INTERVAL", "IS", - "JOIN", "LAST", "LEFT", "LIKE", "LIMIT", "MAPPED", "MATCH", "MINUTE", - "MINUTES", "MONTH", "MONTHS", "NATURAL", "NOT", "NULL", "NULLS", "ON", - "OPTIMIZED", "OR", "ORDER", "OUTER", "PARSED", "PHYSICAL", "PIVOT", "PLAN", - "RIGHT", "RLIKE", "QUERY", "SCHEMAS", "SECOND", "SECONDS", "SELECT", - "SHOW", "SYS", "TABLE", "TABLES", "TEXT", "THEN", "TRUE", "TO", "TOP", - "TYPE", "TYPES", "USING", "VERIFY", "WHEN", "WHERE", "WITH", "YEAR", - "YEARS", "ESCAPE_ESC", "FUNCTION_ESC", "LIMIT_ESC", "DATE_ESC", "TIME_ESC", - "TIMESTAMP_ESC", "GUID_ESC", "ESC_START", "ESC_END", "EQ", "NULLEQ", - "NEQ", "LT", "LTE", "GT", "GTE", "PLUS", "MINUS", "ASTERISK", "SLASH", - "PERCENT", "CAST_OP", "DOT", "PARAM", "STRING", "INTEGER_VALUE", "DECIMAL_VALUE", - "IDENTIFIER", "DIGIT_IDENTIFIER", "TABLE_IDENTIFIER", "QUOTED_IDENTIFIER", - "BACKQUOTED_IDENTIFIER", "EXPONENT", "DIGIT", "LETTER", "SIMPLE_COMMENT", - "BRACKETED_COMMENT", "WS", "UNRECOGNIZED" - }; - } - public static final String[] ruleNames = makeRuleNames(); + private static String[] makeRuleNames() { + return new String[] { + "T__0", + "T__1", + "T__2", + "T__3", + "ALL", + "ANALYZE", + "ANALYZED", + "AND", + "ANY", + "AS", + "ASC", + "BETWEEN", + "BY", + "CASE", + "CAST", + "CATALOG", + "CATALOGS", + "COLUMNS", + "CONVERT", + "CURRENT_DATE", + "CURRENT_TIME", + "CURRENT_TIMESTAMP", + "DAY", + "DAYS", + "DEBUG", + "DESC", + "DESCRIBE", + "DISTINCT", + "ELSE", + "END", + "ESCAPE", + "EXECUTABLE", + "EXISTS", + "EXPLAIN", + "EXTRACT", + "FALSE", + "FIRST", + "FOR", + "FORMAT", + "FROM", + "FROZEN", + "FULL", + "FUNCTIONS", + "GRAPHVIZ", + "GROUP", + "HAVING", + "HOUR", + "HOURS", + "IN", + "INCLUDE", + "INNER", + "INTERVAL", + "IS", + "JOIN", + "LAST", + "LEFT", + "LIKE", + "LIMIT", + "MAPPED", + "MATCH", + "MINUTE", + "MINUTES", + "MONTH", + "MONTHS", + "NATURAL", + "NOT", + "NULL", + "NULLS", + "ON", + "OPTIMIZED", + "OR", + "ORDER", + "OUTER", + "PARSED", + "PHYSICAL", + "PIVOT", + "PLAN", + "RIGHT", + "RLIKE", + "QUERY", + "SCHEMAS", + "SECOND", + "SECONDS", + "SELECT", + "SHOW", + "SYS", + "TABLE", + "TABLES", + "TEXT", + "THEN", + "TRUE", + "TO", + "TOP", + "TYPE", + "TYPES", + "USING", + "VERIFY", + "WHEN", + "WHERE", + "WITH", + "YEAR", + "YEARS", + "ESCAPE_ESC", + "FUNCTION_ESC", + "LIMIT_ESC", + "DATE_ESC", + "TIME_ESC", + "TIMESTAMP_ESC", + "GUID_ESC", + "ESC_START", + "ESC_END", + "EQ", + "NULLEQ", + "NEQ", + "LT", + "LTE", + "GT", + "GTE", + "PLUS", + "MINUS", + "ASTERISK", + "SLASH", + "PERCENT", + "CAST_OP", + "DOT", + "PARAM", + "STRING", + "INTEGER_VALUE", + "DECIMAL_VALUE", + "IDENTIFIER", + "DIGIT_IDENTIFIER", + "TABLE_IDENTIFIER", + "QUOTED_IDENTIFIER", + "BACKQUOTED_IDENTIFIER", + "EXPONENT", + "DIGIT", + "LETTER", + "SIMPLE_COMMENT", + "BRACKETED_COMMENT", + "WS", + "UNRECOGNIZED" }; + } + + public static final String[] ruleNames = makeRuleNames(); - private static String[] makeLiteralNames() { - return new String[] { - null, "'('", "')'", "','", "':'", "'ALL'", "'ANALYZE'", "'ANALYZED'", - "'AND'", "'ANY'", "'AS'", "'ASC'", "'BETWEEN'", "'BY'", "'CASE'", "'CAST'", - "'CATALOG'", "'CATALOGS'", "'COLUMNS'", "'CONVERT'", "'CURRENT_DATE'", - "'CURRENT_TIME'", "'CURRENT_TIMESTAMP'", "'DAY'", "'DAYS'", "'DEBUG'", - "'DESC'", "'DESCRIBE'", "'DISTINCT'", "'ELSE'", "'END'", "'ESCAPE'", - "'EXECUTABLE'", "'EXISTS'", "'EXPLAIN'", "'EXTRACT'", "'FALSE'", "'FIRST'", - "'FOR'", "'FORMAT'", "'FROM'", "'FROZEN'", "'FULL'", "'FUNCTIONS'", "'GRAPHVIZ'", - "'GROUP'", "'HAVING'", "'HOUR'", "'HOURS'", "'IN'", "'INCLUDE'", "'INNER'", - "'INTERVAL'", "'IS'", "'JOIN'", "'LAST'", "'LEFT'", "'LIKE'", "'LIMIT'", - "'MAPPED'", "'MATCH'", "'MINUTE'", "'MINUTES'", "'MONTH'", "'MONTHS'", - "'NATURAL'", "'NOT'", "'NULL'", "'NULLS'", "'ON'", "'OPTIMIZED'", "'OR'", - "'ORDER'", "'OUTER'", "'PARSED'", "'PHYSICAL'", "'PIVOT'", "'PLAN'", - "'RIGHT'", "'RLIKE'", "'QUERY'", "'SCHEMAS'", "'SECOND'", "'SECONDS'", - "'SELECT'", "'SHOW'", "'SYS'", "'TABLE'", "'TABLES'", "'TEXT'", "'THEN'", - "'TRUE'", "'TO'", "'TOP'", "'TYPE'", "'TYPES'", "'USING'", "'VERIFY'", - "'WHEN'", "'WHERE'", "'WITH'", "'YEAR'", "'YEARS'", null, null, null, - null, null, null, null, null, "'}'", "'='", "'<=>'", null, "'<'", "'<='", - "'>'", "'>='", "'+'", "'-'", "'*'", "'/'", "'%'", "'::'", "'.'", "'?'" - }; - } - private static final String[] _LITERAL_NAMES = makeLiteralNames(); - private static String[] makeSymbolicNames() { - return new String[] { - null, null, null, null, null, "ALL", "ANALYZE", "ANALYZED", "AND", "ANY", - "AS", "ASC", "BETWEEN", "BY", "CASE", "CAST", "CATALOG", "CATALOGS", - "COLUMNS", "CONVERT", "CURRENT_DATE", "CURRENT_TIME", "CURRENT_TIMESTAMP", - "DAY", "DAYS", "DEBUG", "DESC", "DESCRIBE", "DISTINCT", "ELSE", "END", - "ESCAPE", "EXECUTABLE", "EXISTS", "EXPLAIN", "EXTRACT", "FALSE", "FIRST", - "FOR", "FORMAT", "FROM", "FROZEN", "FULL", "FUNCTIONS", "GRAPHVIZ", "GROUP", - "HAVING", "HOUR", "HOURS", "IN", "INCLUDE", "INNER", "INTERVAL", "IS", - "JOIN", "LAST", "LEFT", "LIKE", "LIMIT", "MAPPED", "MATCH", "MINUTE", - "MINUTES", "MONTH", "MONTHS", "NATURAL", "NOT", "NULL", "NULLS", "ON", - "OPTIMIZED", "OR", "ORDER", "OUTER", "PARSED", "PHYSICAL", "PIVOT", "PLAN", - "RIGHT", "RLIKE", "QUERY", "SCHEMAS", "SECOND", "SECONDS", "SELECT", - "SHOW", "SYS", "TABLE", "TABLES", "TEXT", "THEN", "TRUE", "TO", "TOP", - "TYPE", "TYPES", "USING", "VERIFY", "WHEN", "WHERE", "WITH", "YEAR", - "YEARS", "ESCAPE_ESC", "FUNCTION_ESC", "LIMIT_ESC", "DATE_ESC", "TIME_ESC", - "TIMESTAMP_ESC", "GUID_ESC", "ESC_START", "ESC_END", "EQ", "NULLEQ", - "NEQ", "LT", "LTE", "GT", "GTE", "PLUS", "MINUS", "ASTERISK", "SLASH", - "PERCENT", "CAST_OP", "DOT", "PARAM", "STRING", "INTEGER_VALUE", "DECIMAL_VALUE", - "IDENTIFIER", "DIGIT_IDENTIFIER", "TABLE_IDENTIFIER", "QUOTED_IDENTIFIER", - "BACKQUOTED_IDENTIFIER", "SIMPLE_COMMENT", "BRACKETED_COMMENT", "WS", - "UNRECOGNIZED" - }; - } - private static final String[] _SYMBOLIC_NAMES = makeSymbolicNames(); - public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); + private static String[] makeLiteralNames() { + return new String[] { + null, + "'('", + "')'", + "','", + "':'", + "'ALL'", + "'ANALYZE'", + "'ANALYZED'", + "'AND'", + "'ANY'", + "'AS'", + "'ASC'", + "'BETWEEN'", + "'BY'", + "'CASE'", + "'CAST'", + "'CATALOG'", + "'CATALOGS'", + "'COLUMNS'", + "'CONVERT'", + "'CURRENT_DATE'", + "'CURRENT_TIME'", + "'CURRENT_TIMESTAMP'", + "'DAY'", + "'DAYS'", + "'DEBUG'", + "'DESC'", + "'DESCRIBE'", + "'DISTINCT'", + "'ELSE'", + "'END'", + "'ESCAPE'", + "'EXECUTABLE'", + "'EXISTS'", + "'EXPLAIN'", + "'EXTRACT'", + "'FALSE'", + "'FIRST'", + "'FOR'", + "'FORMAT'", + "'FROM'", + "'FROZEN'", + "'FULL'", + "'FUNCTIONS'", + "'GRAPHVIZ'", + "'GROUP'", + "'HAVING'", + "'HOUR'", + "'HOURS'", + "'IN'", + "'INCLUDE'", + "'INNER'", + "'INTERVAL'", + "'IS'", + "'JOIN'", + "'LAST'", + "'LEFT'", + "'LIKE'", + "'LIMIT'", + "'MAPPED'", + "'MATCH'", + "'MINUTE'", + "'MINUTES'", + "'MONTH'", + "'MONTHS'", + "'NATURAL'", + "'NOT'", + "'NULL'", + "'NULLS'", + "'ON'", + "'OPTIMIZED'", + "'OR'", + "'ORDER'", + "'OUTER'", + "'PARSED'", + "'PHYSICAL'", + "'PIVOT'", + "'PLAN'", + "'RIGHT'", + "'RLIKE'", + "'QUERY'", + "'SCHEMAS'", + "'SECOND'", + "'SECONDS'", + "'SELECT'", + "'SHOW'", + "'SYS'", + "'TABLE'", + "'TABLES'", + "'TEXT'", + "'THEN'", + "'TRUE'", + "'TO'", + "'TOP'", + "'TYPE'", + "'TYPES'", + "'USING'", + "'VERIFY'", + "'WHEN'", + "'WHERE'", + "'WITH'", + "'YEAR'", + "'YEARS'", + null, + null, + null, + null, + null, + null, + null, + null, + "'}'", + "'='", + "'<=>'", + null, + "'<'", + "'<='", + "'>'", + "'>='", + "'+'", + "'-'", + "'*'", + "'/'", + "'%'", + "'::'", + "'.'", + "'?'" }; + } - /** - * @deprecated Use {@link #VOCABULARY} instead. - */ - @Deprecated - public static final String[] tokenNames; - static { - tokenNames = new String[_SYMBOLIC_NAMES.length]; - for (int i = 0; i < tokenNames.length; i++) { - tokenNames[i] = VOCABULARY.getLiteralName(i); - if (tokenNames[i] == null) { - tokenNames[i] = VOCABULARY.getSymbolicName(i); - } + private static final String[] _LITERAL_NAMES = makeLiteralNames(); - if (tokenNames[i] == null) { - tokenNames[i] = ""; - } + private static String[] makeSymbolicNames() { + return new String[] { + null, + null, + null, + null, + null, + "ALL", + "ANALYZE", + "ANALYZED", + "AND", + "ANY", + "AS", + "ASC", + "BETWEEN", + "BY", + "CASE", + "CAST", + "CATALOG", + "CATALOGS", + "COLUMNS", + "CONVERT", + "CURRENT_DATE", + "CURRENT_TIME", + "CURRENT_TIMESTAMP", + "DAY", + "DAYS", + "DEBUG", + "DESC", + "DESCRIBE", + "DISTINCT", + "ELSE", + "END", + "ESCAPE", + "EXECUTABLE", + "EXISTS", + "EXPLAIN", + "EXTRACT", + "FALSE", + "FIRST", + "FOR", + "FORMAT", + "FROM", + "FROZEN", + "FULL", + "FUNCTIONS", + "GRAPHVIZ", + "GROUP", + "HAVING", + "HOUR", + "HOURS", + "IN", + "INCLUDE", + "INNER", + "INTERVAL", + "IS", + "JOIN", + "LAST", + "LEFT", + "LIKE", + "LIMIT", + "MAPPED", + "MATCH", + "MINUTE", + "MINUTES", + "MONTH", + "MONTHS", + "NATURAL", + "NOT", + "NULL", + "NULLS", + "ON", + "OPTIMIZED", + "OR", + "ORDER", + "OUTER", + "PARSED", + "PHYSICAL", + "PIVOT", + "PLAN", + "RIGHT", + "RLIKE", + "QUERY", + "SCHEMAS", + "SECOND", + "SECONDS", + "SELECT", + "SHOW", + "SYS", + "TABLE", + "TABLES", + "TEXT", + "THEN", + "TRUE", + "TO", + "TOP", + "TYPE", + "TYPES", + "USING", + "VERIFY", + "WHEN", + "WHERE", + "WITH", + "YEAR", + "YEARS", + "ESCAPE_ESC", + "FUNCTION_ESC", + "LIMIT_ESC", + "DATE_ESC", + "TIME_ESC", + "TIMESTAMP_ESC", + "GUID_ESC", + "ESC_START", + "ESC_END", + "EQ", + "NULLEQ", + "NEQ", + "LT", + "LTE", + "GT", + "GTE", + "PLUS", + "MINUS", + "ASTERISK", + "SLASH", + "PERCENT", + "CAST_OP", + "DOT", + "PARAM", + "STRING", + "INTEGER_VALUE", + "DECIMAL_VALUE", + "IDENTIFIER", + "DIGIT_IDENTIFIER", + "TABLE_IDENTIFIER", + "QUOTED_IDENTIFIER", + "BACKQUOTED_IDENTIFIER", + "SIMPLE_COMMENT", + "BRACKETED_COMMENT", + "WS", + "UNRECOGNIZED" }; } - } - @Override - @Deprecated - public String[] getTokenNames() { - return tokenNames; - } + private static final String[] _SYMBOLIC_NAMES = makeSymbolicNames(); + public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); - @Override + /** + * @deprecated Use {@link #VOCABULARY} instead. + */ + @Deprecated + public static final String[] tokenNames; + static { + tokenNames = new String[_SYMBOLIC_NAMES.length]; + for (int i = 0; i < tokenNames.length; i++) { + tokenNames[i] = VOCABULARY.getLiteralName(i); + if (tokenNames[i] == null) { + tokenNames[i] = VOCABULARY.getSymbolicName(i); + } - public Vocabulary getVocabulary() { - return VOCABULARY; - } + if (tokenNames[i] == null) { + tokenNames[i] = ""; + } + } + } + @Override + @Deprecated + public String[] getTokenNames() { + return tokenNames; + } - public SqlBaseLexer(CharStream input) { - super(input); - _interp = new LexerATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache); - } + @Override - @Override - public String getGrammarFileName() { return "SqlBase.g4"; } + public Vocabulary getVocabulary() { + return VOCABULARY; + } + + public SqlBaseLexer(CharStream input) { + super(input); + _interp = new LexerATNSimulator(this, _ATN, _decisionToDFA, _sharedContextCache); + } + + @Override + public String getGrammarFileName() { + return "SqlBase.g4"; + } - @Override - public String[] getRuleNames() { return ruleNames; } + @Override + public String[] getRuleNames() { + return ruleNames; + } - @Override - public String getSerializedATN() { return _serializedATN; } + @Override + public String getSerializedATN() { + return _serializedATN; + } - @Override - public String[] getChannelNames() { return channelNames; } + @Override + public String[] getChannelNames() { + return channelNames; + } - @Override - public String[] getModeNames() { return modeNames; } + @Override + public String[] getModeNames() { + return modeNames; + } - @Override - public ATN getATN() { return _ATN; } + @Override + public ATN getATN() { + return _ATN; + } - public static final String _serializedATN = - "\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\2\u008c\u0489\b\1\4"+ - "\2\t\2\4\3\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b\t\b\4\t\t\t\4\n\t\n"+ - "\4\13\t\13\4\f\t\f\4\r\t\r\4\16\t\16\4\17\t\17\4\20\t\20\4\21\t\21\4\22"+ - "\t\22\4\23\t\23\4\24\t\24\4\25\t\25\4\26\t\26\4\27\t\27\4\30\t\30\4\31"+ - "\t\31\4\32\t\32\4\33\t\33\4\34\t\34\4\35\t\35\4\36\t\36\4\37\t\37\4 \t"+ - " \4!\t!\4\"\t\"\4#\t#\4$\t$\4%\t%\4&\t&\4\'\t\'\4(\t(\4)\t)\4*\t*\4+\t"+ - "+\4,\t,\4-\t-\4.\t.\4/\t/\4\60\t\60\4\61\t\61\4\62\t\62\4\63\t\63\4\64"+ - "\t\64\4\65\t\65\4\66\t\66\4\67\t\67\48\t8\49\t9\4:\t:\4;\t;\4<\t<\4=\t"+ - "=\4>\t>\4?\t?\4@\t@\4A\tA\4B\tB\4C\tC\4D\tD\4E\tE\4F\tF\4G\tG\4H\tH\4"+ - "I\tI\4J\tJ\4K\tK\4L\tL\4M\tM\4N\tN\4O\tO\4P\tP\4Q\tQ\4R\tR\4S\tS\4T\t"+ - "T\4U\tU\4V\tV\4W\tW\4X\tX\4Y\tY\4Z\tZ\4[\t[\4\\\t\\\4]\t]\4^\t^\4_\t_"+ - "\4`\t`\4a\ta\4b\tb\4c\tc\4d\td\4e\te\4f\tf\4g\tg\4h\th\4i\ti\4j\tj\4k"+ - "\tk\4l\tl\4m\tm\4n\tn\4o\to\4p\tp\4q\tq\4r\tr\4s\ts\4t\tt\4u\tu\4v\tv"+ - "\4w\tw\4x\tx\4y\ty\4z\tz\4{\t{\4|\t|\4}\t}\4~\t~\4\177\t\177\4\u0080\t"+ - "\u0080\4\u0081\t\u0081\4\u0082\t\u0082\4\u0083\t\u0083\4\u0084\t\u0084"+ - "\4\u0085\t\u0085\4\u0086\t\u0086\4\u0087\t\u0087\4\u0088\t\u0088\4\u0089"+ - "\t\u0089\4\u008a\t\u008a\4\u008b\t\u008b\4\u008c\t\u008c\4\u008d\t\u008d"+ - "\4\u008e\t\u008e\3\2\3\2\3\3\3\3\3\4\3\4\3\5\3\5\3\6\3\6\3\6\3\6\3\7\3"+ - "\7\3\7\3\7\3\7\3\7\3\7\3\7\3\b\3\b\3\b\3\b\3\b\3\b\3\b\3\b\3\b\3\t\3\t"+ - "\3\t\3\t\3\n\3\n\3\n\3\n\3\13\3\13\3\13\3\f\3\f\3\f\3\f\3\r\3\r\3\r\3"+ - "\r\3\r\3\r\3\r\3\r\3\16\3\16\3\16\3\17\3\17\3\17\3\17\3\17\3\20\3\20\3"+ - "\20\3\20\3\20\3\21\3\21\3\21\3\21\3\21\3\21\3\21\3\21\3\22\3\22\3\22\3"+ - "\22\3\22\3\22\3\22\3\22\3\22\3\23\3\23\3\23\3\23\3\23\3\23\3\23\3\23\3"+ - "\24\3\24\3\24\3\24\3\24\3\24\3\24\3\24\3\25\3\25\3\25\3\25\3\25\3\25\3"+ - "\25\3\25\3\25\3\25\3\25\3\25\3\25\3\26\3\26\3\26\3\26\3\26\3\26\3\26\3"+ - "\26\3\26\3\26\3\26\3\26\3\26\3\27\3\27\3\27\3\27\3\27\3\27\3\27\3\27\3"+ - "\27\3\27\3\27\3\27\3\27\3\27\3\27\3\27\3\27\3\27\3\30\3\30\3\30\3\30\3"+ - "\31\3\31\3\31\3\31\3\31\3\32\3\32\3\32\3\32\3\32\3\32\3\33\3\33\3\33\3"+ - "\33\3\33\3\34\3\34\3\34\3\34\3\34\3\34\3\34\3\34\3\34\3\35\3\35\3\35\3"+ - "\35\3\35\3\35\3\35\3\35\3\35\3\36\3\36\3\36\3\36\3\36\3\37\3\37\3\37\3"+ - "\37\3 \3 \3 \3 \3 \3 \3 \3!\3!\3!\3!\3!\3!\3!\3!\3!\3!\3!\3\"\3\"\3\""+ - "\3\"\3\"\3\"\3\"\3#\3#\3#\3#\3#\3#\3#\3#\3$\3$\3$\3$\3$\3$\3$\3$\3%\3"+ - "%\3%\3%\3%\3%\3&\3&\3&\3&\3&\3&\3\'\3\'\3\'\3\'\3(\3(\3(\3(\3(\3(\3(\3"+ - ")\3)\3)\3)\3)\3*\3*\3*\3*\3*\3*\3*\3+\3+\3+\3+\3+\3,\3,\3,\3,\3,\3,\3"+ - ",\3,\3,\3,\3-\3-\3-\3-\3-\3-\3-\3-\3-\3.\3.\3.\3.\3.\3.\3/\3/\3/\3/\3"+ - "/\3/\3/\3\60\3\60\3\60\3\60\3\60\3\61\3\61\3\61\3\61\3\61\3\61\3\62\3"+ - "\62\3\62\3\63\3\63\3\63\3\63\3\63\3\63\3\63\3\63\3\64\3\64\3\64\3\64\3"+ - "\64\3\64\3\65\3\65\3\65\3\65\3\65\3\65\3\65\3\65\3\65\3\66\3\66\3\66\3"+ - "\67\3\67\3\67\3\67\3\67\38\38\38\38\38\39\39\39\39\39\3:\3:\3:\3:\3:\3"+ - ";\3;\3;\3;\3;\3;\3<\3<\3<\3<\3<\3<\3<\3=\3=\3=\3=\3=\3=\3>\3>\3>\3>\3"+ - ">\3>\3>\3?\3?\3?\3?\3?\3?\3?\3?\3@\3@\3@\3@\3@\3@\3A\3A\3A\3A\3A\3A\3"+ - "A\3B\3B\3B\3B\3B\3B\3B\3B\3C\3C\3C\3C\3D\3D\3D\3D\3D\3E\3E\3E\3E\3E\3"+ - "E\3F\3F\3F\3G\3G\3G\3G\3G\3G\3G\3G\3G\3G\3H\3H\3H\3I\3I\3I\3I\3I\3I\3"+ - "J\3J\3J\3J\3J\3J\3K\3K\3K\3K\3K\3K\3K\3L\3L\3L\3L\3L\3L\3L\3L\3L\3M\3"+ - "M\3M\3M\3M\3M\3N\3N\3N\3N\3N\3O\3O\3O\3O\3O\3O\3P\3P\3P\3P\3P\3P\3Q\3"+ - "Q\3Q\3Q\3Q\3Q\3R\3R\3R\3R\3R\3R\3R\3R\3S\3S\3S\3S\3S\3S\3S\3T\3T\3T\3"+ - "T\3T\3T\3T\3T\3U\3U\3U\3U\3U\3U\3U\3V\3V\3V\3V\3V\3W\3W\3W\3W\3X\3X\3"+ - "X\3X\3X\3X\3Y\3Y\3Y\3Y\3Y\3Y\3Y\3Z\3Z\3Z\3Z\3Z\3[\3[\3[\3[\3[\3\\\3\\"+ - "\3\\\3\\\3\\\3]\3]\3]\3^\3^\3^\3^\3_\3_\3_\3_\3_\3`\3`\3`\3`\3`\3`\3a"+ - "\3a\3a\3a\3a\3a\3b\3b\3b\3b\3b\3b\3b\3c\3c\3c\3c\3c\3d\3d\3d\3d\3d\3d"+ - "\3e\3e\3e\3e\3e\3f\3f\3f\3f\3f\3g\3g\3g\3g\3g\3g\3h\3h\3h\3h\3h\3h\3h"+ - "\3h\3i\3i\3i\3i\3j\3j\3j\3j\3j\3j\3j\3k\3k\3k\3l\3l\3l\3m\3m\3m\3m\3n"+ - "\3n\3n\3n\3n\3n\3o\3o\7o\u03b9\no\fo\16o\u03bc\13o\3p\3p\3q\3q\3r\3r\3"+ - "r\3r\3s\3s\3s\3s\5s\u03ca\ns\3t\3t\3u\3u\3u\3v\3v\3w\3w\3w\3x\3x\3y\3"+ - "y\3z\3z\3{\3{\3|\3|\3}\3}\3}\3~\3~\3\177\3\177\3\u0080\3\u0080\3\u0080"+ - "\3\u0080\7\u0080\u03eb\n\u0080\f\u0080\16\u0080\u03ee\13\u0080\3\u0080"+ - "\3\u0080\3\u0081\6\u0081\u03f3\n\u0081\r\u0081\16\u0081\u03f4\3\u0082"+ - "\6\u0082\u03f8\n\u0082\r\u0082\16\u0082\u03f9\3\u0082\3\u0082\7\u0082"+ - "\u03fe\n\u0082\f\u0082\16\u0082\u0401\13\u0082\3\u0082\3\u0082\6\u0082"+ - "\u0405\n\u0082\r\u0082\16\u0082\u0406\3\u0082\6\u0082\u040a\n\u0082\r"+ - "\u0082\16\u0082\u040b\3\u0082\3\u0082\7\u0082\u0410\n\u0082\f\u0082\16"+ - "\u0082\u0413\13\u0082\5\u0082\u0415\n\u0082\3\u0082\3\u0082\3\u0082\3"+ - "\u0082\6\u0082\u041b\n\u0082\r\u0082\16\u0082\u041c\3\u0082\3\u0082\5"+ - "\u0082\u0421\n\u0082\3\u0083\3\u0083\5\u0083\u0425\n\u0083\3\u0083\3\u0083"+ - "\3\u0083\7\u0083\u042a\n\u0083\f\u0083\16\u0083\u042d\13\u0083\3\u0084"+ - "\3\u0084\3\u0084\3\u0084\6\u0084\u0433\n\u0084\r\u0084\16\u0084\u0434"+ - "\3\u0085\3\u0085\3\u0085\6\u0085\u043a\n\u0085\r\u0085\16\u0085\u043b"+ - "\3\u0086\3\u0086\3\u0086\3\u0086\7\u0086\u0442\n\u0086\f\u0086\16\u0086"+ - "\u0445\13\u0086\3\u0086\3\u0086\3\u0087\3\u0087\3\u0087\3\u0087\7\u0087"+ - "\u044d\n\u0087\f\u0087\16\u0087\u0450\13\u0087\3\u0087\3\u0087\3\u0088"+ - "\3\u0088\5\u0088\u0456\n\u0088\3\u0088\6\u0088\u0459\n\u0088\r\u0088\16"+ - "\u0088\u045a\3\u0089\3\u0089\3\u008a\3\u008a\3\u008b\3\u008b\3\u008b\3"+ - "\u008b\7\u008b\u0465\n\u008b\f\u008b\16\u008b\u0468\13\u008b\3\u008b\5"+ - "\u008b\u046b\n\u008b\3\u008b\5\u008b\u046e\n\u008b\3\u008b\3\u008b\3\u008c"+ - "\3\u008c\3\u008c\3\u008c\3\u008c\7\u008c\u0477\n\u008c\f\u008c\16\u008c"+ - "\u047a\13\u008c\3\u008c\3\u008c\3\u008c\3\u008c\3\u008c\3\u008d\6\u008d"+ - "\u0482\n\u008d\r\u008d\16\u008d\u0483\3\u008d\3\u008d\3\u008e\3\u008e"+ - "\3\u0478\2\u008f\3\3\5\4\7\5\t\6\13\7\r\b\17\t\21\n\23\13\25\f\27\r\31"+ - "\16\33\17\35\20\37\21!\22#\23%\24\'\25)\26+\27-\30/\31\61\32\63\33\65"+ - "\34\67\359\36;\37= ?!A\"C#E$G%I&K\'M(O)Q*S+U,W-Y.[/]\60_\61a\62c\63e\64"+ - "g\65i\66k\67m8o9q:s;u{?}@\177A\u0081B\u0083C\u0085D\u0087E\u0089"+ - "F\u008bG\u008dH\u008fI\u0091J\u0093K\u0095L\u0097M\u0099N\u009bO\u009d"+ - "P\u009fQ\u00a1R\u00a3S\u00a5T\u00a7U\u00a9V\u00abW\u00adX\u00afY\u00b1"+ - "Z\u00b3[\u00b5\\\u00b7]\u00b9^\u00bb_\u00bd`\u00bfa\u00c1b\u00c3c\u00c5"+ - "d\u00c7e\u00c9f\u00cbg\u00cdh\u00cfi\u00d1j\u00d3k\u00d5l\u00d7m\u00d9"+ - "n\u00dbo\u00ddp\u00dfq\u00e1r\u00e3s\u00e5t\u00e7u\u00e9v\u00ebw\u00ed"+ - "x\u00efy\u00f1z\u00f3{\u00f5|\u00f7}\u00f9~\u00fb\177\u00fd\u0080\u00ff"+ - "\u0081\u0101\u0082\u0103\u0083\u0105\u0084\u0107\u0085\u0109\u0086\u010b"+ - "\u0087\u010d\u0088\u010f\2\u0111\2\u0113\2\u0115\u0089\u0117\u008a\u0119"+ - "\u008b\u011b\u008c\3\2\13\3\2))\4\2BBaa\3\2$$\3\2bb\4\2--//\3\2\62;\3"+ - "\2C\\\4\2\f\f\17\17\5\2\13\f\17\17\"\"\2\u04aa\2\3\3\2\2\2\2\5\3\2\2\2"+ - "\2\7\3\2\2\2\2\t\3\2\2\2\2\13\3\2\2\2\2\r\3\2\2\2\2\17\3\2\2\2\2\21\3"+ - "\2\2\2\2\23\3\2\2\2\2\25\3\2\2\2\2\27\3\2\2\2\2\31\3\2\2\2\2\33\3\2\2"+ - "\2\2\35\3\2\2\2\2\37\3\2\2\2\2!\3\2\2\2\2#\3\2\2\2\2%\3\2\2\2\2\'\3\2"+ - "\2\2\2)\3\2\2\2\2+\3\2\2\2\2-\3\2\2\2\2/\3\2\2\2\2\61\3\2\2\2\2\63\3\2"+ - "\2\2\2\65\3\2\2\2\2\67\3\2\2\2\29\3\2\2\2\2;\3\2\2\2\2=\3\2\2\2\2?\3\2"+ - "\2\2\2A\3\2\2\2\2C\3\2\2\2\2E\3\2\2\2\2G\3\2\2\2\2I\3\2\2\2\2K\3\2\2\2"+ - "\2M\3\2\2\2\2O\3\2\2\2\2Q\3\2\2\2\2S\3\2\2\2\2U\3\2\2\2\2W\3\2\2\2\2Y"+ - "\3\2\2\2\2[\3\2\2\2\2]\3\2\2\2\2_\3\2\2\2\2a\3\2\2\2\2c\3\2\2\2\2e\3\2"+ - "\2\2\2g\3\2\2\2\2i\3\2\2\2\2k\3\2\2\2\2m\3\2\2\2\2o\3\2\2\2\2q\3\2\2\2"+ - "\2s\3\2\2\2\2u\3\2\2\2\2w\3\2\2\2\2y\3\2\2\2\2{\3\2\2\2\2}\3\2\2\2\2\177"+ - "\3\2\2\2\2\u0081\3\2\2\2\2\u0083\3\2\2\2\2\u0085\3\2\2\2\2\u0087\3\2\2"+ - "\2\2\u0089\3\2\2\2\2\u008b\3\2\2\2\2\u008d\3\2\2\2\2\u008f\3\2\2\2\2\u0091"+ - "\3\2\2\2\2\u0093\3\2\2\2\2\u0095\3\2\2\2\2\u0097\3\2\2\2\2\u0099\3\2\2"+ - "\2\2\u009b\3\2\2\2\2\u009d\3\2\2\2\2\u009f\3\2\2\2\2\u00a1\3\2\2\2\2\u00a3"+ - "\3\2\2\2\2\u00a5\3\2\2\2\2\u00a7\3\2\2\2\2\u00a9\3\2\2\2\2\u00ab\3\2\2"+ - "\2\2\u00ad\3\2\2\2\2\u00af\3\2\2\2\2\u00b1\3\2\2\2\2\u00b3\3\2\2\2\2\u00b5"+ - "\3\2\2\2\2\u00b7\3\2\2\2\2\u00b9\3\2\2\2\2\u00bb\3\2\2\2\2\u00bd\3\2\2"+ - "\2\2\u00bf\3\2\2\2\2\u00c1\3\2\2\2\2\u00c3\3\2\2\2\2\u00c5\3\2\2\2\2\u00c7"+ - "\3\2\2\2\2\u00c9\3\2\2\2\2\u00cb\3\2\2\2\2\u00cd\3\2\2\2\2\u00cf\3\2\2"+ - "\2\2\u00d1\3\2\2\2\2\u00d3\3\2\2\2\2\u00d5\3\2\2\2\2\u00d7\3\2\2\2\2\u00d9"+ - "\3\2\2\2\2\u00db\3\2\2\2\2\u00dd\3\2\2\2\2\u00df\3\2\2\2\2\u00e1\3\2\2"+ - "\2\2\u00e3\3\2\2\2\2\u00e5\3\2\2\2\2\u00e7\3\2\2\2\2\u00e9\3\2\2\2\2\u00eb"+ - "\3\2\2\2\2\u00ed\3\2\2\2\2\u00ef\3\2\2\2\2\u00f1\3\2\2\2\2\u00f3\3\2\2"+ - "\2\2\u00f5\3\2\2\2\2\u00f7\3\2\2\2\2\u00f9\3\2\2\2\2\u00fb\3\2\2\2\2\u00fd"+ - "\3\2\2\2\2\u00ff\3\2\2\2\2\u0101\3\2\2\2\2\u0103\3\2\2\2\2\u0105\3\2\2"+ - "\2\2\u0107\3\2\2\2\2\u0109\3\2\2\2\2\u010b\3\2\2\2\2\u010d\3\2\2\2\2\u0115"+ - "\3\2\2\2\2\u0117\3\2\2\2\2\u0119\3\2\2\2\2\u011b\3\2\2\2\3\u011d\3\2\2"+ - "\2\5\u011f\3\2\2\2\7\u0121\3\2\2\2\t\u0123\3\2\2\2\13\u0125\3\2\2\2\r"+ - "\u0129\3\2\2\2\17\u0131\3\2\2\2\21\u013a\3\2\2\2\23\u013e\3\2\2\2\25\u0142"+ - "\3\2\2\2\27\u0145\3\2\2\2\31\u0149\3\2\2\2\33\u0151\3\2\2\2\35\u0154\3"+ - "\2\2\2\37\u0159\3\2\2\2!\u015e\3\2\2\2#\u0166\3\2\2\2%\u016f\3\2\2\2\'"+ - "\u0177\3\2\2\2)\u017f\3\2\2\2+\u018c\3\2\2\2-\u0199\3\2\2\2/\u01ab\3\2"+ - "\2\2\61\u01af\3\2\2\2\63\u01b4\3\2\2\2\65\u01ba\3\2\2\2\67\u01bf\3\2\2"+ - "\29\u01c8\3\2\2\2;\u01d1\3\2\2\2=\u01d6\3\2\2\2?\u01da\3\2\2\2A\u01e1"+ - "\3\2\2\2C\u01ec\3\2\2\2E\u01f3\3\2\2\2G\u01fb\3\2\2\2I\u0203\3\2\2\2K"+ - "\u0209\3\2\2\2M\u020f\3\2\2\2O\u0213\3\2\2\2Q\u021a\3\2\2\2S\u021f\3\2"+ - "\2\2U\u0226\3\2\2\2W\u022b\3\2\2\2Y\u0235\3\2\2\2[\u023e\3\2\2\2]\u0244"+ - "\3\2\2\2_\u024b\3\2\2\2a\u0250\3\2\2\2c\u0256\3\2\2\2e\u0259\3\2\2\2g"+ - "\u0261\3\2\2\2i\u0267\3\2\2\2k\u0270\3\2\2\2m\u0273\3\2\2\2o\u0278\3\2"+ - "\2\2q\u027d\3\2\2\2s\u0282\3\2\2\2u\u0287\3\2\2\2w\u028d\3\2\2\2y\u0294"+ - "\3\2\2\2{\u029a\3\2\2\2}\u02a1\3\2\2\2\177\u02a9\3\2\2\2\u0081\u02af\3"+ - "\2\2\2\u0083\u02b6\3\2\2\2\u0085\u02be\3\2\2\2\u0087\u02c2\3\2\2\2\u0089"+ - "\u02c7\3\2\2\2\u008b\u02cd\3\2\2\2\u008d\u02d0\3\2\2\2\u008f\u02da\3\2"+ - "\2\2\u0091\u02dd\3\2\2\2\u0093\u02e3\3\2\2\2\u0095\u02e9\3\2\2\2\u0097"+ - "\u02f0\3\2\2\2\u0099\u02f9\3\2\2\2\u009b\u02ff\3\2\2\2\u009d\u0304\3\2"+ - "\2\2\u009f\u030a\3\2\2\2\u00a1\u0310\3\2\2\2\u00a3\u0316\3\2\2\2\u00a5"+ - "\u031e\3\2\2\2\u00a7\u0325\3\2\2\2\u00a9\u032d\3\2\2\2\u00ab\u0334\3\2"+ - "\2\2\u00ad\u0339\3\2\2\2\u00af\u033d\3\2\2\2\u00b1\u0343\3\2\2\2\u00b3"+ - "\u034a\3\2\2\2\u00b5\u034f\3\2\2\2\u00b7\u0354\3\2\2\2\u00b9\u0359\3\2"+ - "\2\2\u00bb\u035c\3\2\2\2\u00bd\u0360\3\2\2\2\u00bf\u0365\3\2\2\2\u00c1"+ - "\u036b\3\2\2\2\u00c3\u0371\3\2\2\2\u00c5\u0378\3\2\2\2\u00c7\u037d\3\2"+ - "\2\2\u00c9\u0383\3\2\2\2\u00cb\u0388\3\2\2\2\u00cd\u038d\3\2\2\2\u00cf"+ - "\u0393\3\2\2\2\u00d1\u039b\3\2\2\2\u00d3\u039f\3\2\2\2\u00d5\u03a6\3\2"+ - "\2\2\u00d7\u03a9\3\2\2\2\u00d9\u03ac\3\2\2\2\u00db\u03b0\3\2\2\2\u00dd"+ - "\u03b6\3\2\2\2\u00df\u03bd\3\2\2\2\u00e1\u03bf\3\2\2\2\u00e3\u03c1\3\2"+ - "\2\2\u00e5\u03c9\3\2\2\2\u00e7\u03cb\3\2\2\2\u00e9\u03cd\3\2\2\2\u00eb"+ - "\u03d0\3\2\2\2\u00ed\u03d2\3\2\2\2\u00ef\u03d5\3\2\2\2\u00f1\u03d7\3\2"+ - "\2\2\u00f3\u03d9\3\2\2\2\u00f5\u03db\3\2\2\2\u00f7\u03dd\3\2\2\2\u00f9"+ - "\u03df\3\2\2\2\u00fb\u03e2\3\2\2\2\u00fd\u03e4\3\2\2\2\u00ff\u03e6\3\2"+ - "\2\2\u0101\u03f2\3\2\2\2\u0103\u0420\3\2\2\2\u0105\u0424\3\2\2\2\u0107"+ - "\u042e\3\2\2\2\u0109\u0439\3\2\2\2\u010b\u043d\3\2\2\2\u010d\u0448\3\2"+ - "\2\2\u010f\u0453\3\2\2\2\u0111\u045c\3\2\2\2\u0113\u045e\3\2\2\2\u0115"+ - "\u0460\3\2\2\2\u0117\u0471\3\2\2\2\u0119\u0481\3\2\2\2\u011b\u0487\3\2"+ - "\2\2\u011d\u011e\7*\2\2\u011e\4\3\2\2\2\u011f\u0120\7+\2\2\u0120\6\3\2"+ - "\2\2\u0121\u0122\7.\2\2\u0122\b\3\2\2\2\u0123\u0124\7<\2\2\u0124\n\3\2"+ - "\2\2\u0125\u0126\7C\2\2\u0126\u0127\7N\2\2\u0127\u0128\7N\2\2\u0128\f"+ - "\3\2\2\2\u0129\u012a\7C\2\2\u012a\u012b\7P\2\2\u012b\u012c\7C\2\2\u012c"+ - "\u012d\7N\2\2\u012d\u012e\7[\2\2\u012e\u012f\7\\\2\2\u012f\u0130\7G\2"+ - "\2\u0130\16\3\2\2\2\u0131\u0132\7C\2\2\u0132\u0133\7P\2\2\u0133\u0134"+ - "\7C\2\2\u0134\u0135\7N\2\2\u0135\u0136\7[\2\2\u0136\u0137\7\\\2\2\u0137"+ - "\u0138\7G\2\2\u0138\u0139\7F\2\2\u0139\20\3\2\2\2\u013a\u013b\7C\2\2\u013b"+ - "\u013c\7P\2\2\u013c\u013d\7F\2\2\u013d\22\3\2\2\2\u013e\u013f\7C\2\2\u013f"+ - "\u0140\7P\2\2\u0140\u0141\7[\2\2\u0141\24\3\2\2\2\u0142\u0143\7C\2\2\u0143"+ - "\u0144\7U\2\2\u0144\26\3\2\2\2\u0145\u0146\7C\2\2\u0146\u0147\7U\2\2\u0147"+ - "\u0148\7E\2\2\u0148\30\3\2\2\2\u0149\u014a\7D\2\2\u014a\u014b\7G\2\2\u014b"+ - "\u014c\7V\2\2\u014c\u014d\7Y\2\2\u014d\u014e\7G\2\2\u014e\u014f\7G\2\2"+ - "\u014f\u0150\7P\2\2\u0150\32\3\2\2\2\u0151\u0152\7D\2\2\u0152\u0153\7"+ - "[\2\2\u0153\34\3\2\2\2\u0154\u0155\7E\2\2\u0155\u0156\7C\2\2\u0156\u0157"+ - "\7U\2\2\u0157\u0158\7G\2\2\u0158\36\3\2\2\2\u0159\u015a\7E\2\2\u015a\u015b"+ - "\7C\2\2\u015b\u015c\7U\2\2\u015c\u015d\7V\2\2\u015d \3\2\2\2\u015e\u015f"+ - "\7E\2\2\u015f\u0160\7C\2\2\u0160\u0161\7V\2\2\u0161\u0162\7C\2\2\u0162"+ - "\u0163\7N\2\2\u0163\u0164\7Q\2\2\u0164\u0165\7I\2\2\u0165\"\3\2\2\2\u0166"+ - "\u0167\7E\2\2\u0167\u0168\7C\2\2\u0168\u0169\7V\2\2\u0169\u016a\7C\2\2"+ - "\u016a\u016b\7N\2\2\u016b\u016c\7Q\2\2\u016c\u016d\7I\2\2\u016d\u016e"+ - "\7U\2\2\u016e$\3\2\2\2\u016f\u0170\7E\2\2\u0170\u0171\7Q\2\2\u0171\u0172"+ - "\7N\2\2\u0172\u0173\7W\2\2\u0173\u0174\7O\2\2\u0174\u0175\7P\2\2\u0175"+ - "\u0176\7U\2\2\u0176&\3\2\2\2\u0177\u0178\7E\2\2\u0178\u0179\7Q\2\2\u0179"+ - "\u017a\7P\2\2\u017a\u017b\7X\2\2\u017b\u017c\7G\2\2\u017c\u017d\7T\2\2"+ - "\u017d\u017e\7V\2\2\u017e(\3\2\2\2\u017f\u0180\7E\2\2\u0180\u0181\7W\2"+ - "\2\u0181\u0182\7T\2\2\u0182\u0183\7T\2\2\u0183\u0184\7G\2\2\u0184\u0185"+ - "\7P\2\2\u0185\u0186\7V\2\2\u0186\u0187\7a\2\2\u0187\u0188\7F\2\2\u0188"+ - "\u0189\7C\2\2\u0189\u018a\7V\2\2\u018a\u018b\7G\2\2\u018b*\3\2\2\2\u018c"+ - "\u018d\7E\2\2\u018d\u018e\7W\2\2\u018e\u018f\7T\2\2\u018f\u0190\7T\2\2"+ - "\u0190\u0191\7G\2\2\u0191\u0192\7P\2\2\u0192\u0193\7V\2\2\u0193\u0194"+ - "\7a\2\2\u0194\u0195\7V\2\2\u0195\u0196\7K\2\2\u0196\u0197\7O\2\2\u0197"+ - "\u0198\7G\2\2\u0198,\3\2\2\2\u0199\u019a\7E\2\2\u019a\u019b\7W\2\2\u019b"+ - "\u019c\7T\2\2\u019c\u019d\7T\2\2\u019d\u019e\7G\2\2\u019e\u019f\7P\2\2"+ - "\u019f\u01a0\7V\2\2\u01a0\u01a1\7a\2\2\u01a1\u01a2\7V\2\2\u01a2\u01a3"+ - "\7K\2\2\u01a3\u01a4\7O\2\2\u01a4\u01a5\7G\2\2\u01a5\u01a6\7U\2\2\u01a6"+ - "\u01a7\7V\2\2\u01a7\u01a8\7C\2\2\u01a8\u01a9\7O\2\2\u01a9\u01aa\7R\2\2"+ - "\u01aa.\3\2\2\2\u01ab\u01ac\7F\2\2\u01ac\u01ad\7C\2\2\u01ad\u01ae\7[\2"+ - "\2\u01ae\60\3\2\2\2\u01af\u01b0\7F\2\2\u01b0\u01b1\7C\2\2\u01b1\u01b2"+ - "\7[\2\2\u01b2\u01b3\7U\2\2\u01b3\62\3\2\2\2\u01b4\u01b5\7F\2\2\u01b5\u01b6"+ - "\7G\2\2\u01b6\u01b7\7D\2\2\u01b7\u01b8\7W\2\2\u01b8\u01b9\7I\2\2\u01b9"+ - "\64\3\2\2\2\u01ba\u01bb\7F\2\2\u01bb\u01bc\7G\2\2\u01bc\u01bd\7U\2\2\u01bd"+ - "\u01be\7E\2\2\u01be\66\3\2\2\2\u01bf\u01c0\7F\2\2\u01c0\u01c1\7G\2\2\u01c1"+ - "\u01c2\7U\2\2\u01c2\u01c3\7E\2\2\u01c3\u01c4\7T\2\2\u01c4\u01c5\7K\2\2"+ - "\u01c5\u01c6\7D\2\2\u01c6\u01c7\7G\2\2\u01c78\3\2\2\2\u01c8\u01c9\7F\2"+ - "\2\u01c9\u01ca\7K\2\2\u01ca\u01cb\7U\2\2\u01cb\u01cc\7V\2\2\u01cc\u01cd"+ - "\7K\2\2\u01cd\u01ce\7P\2\2\u01ce\u01cf\7E\2\2\u01cf\u01d0\7V\2\2\u01d0"+ - ":\3\2\2\2\u01d1\u01d2\7G\2\2\u01d2\u01d3\7N\2\2\u01d3\u01d4\7U\2\2\u01d4"+ - "\u01d5\7G\2\2\u01d5<\3\2\2\2\u01d6\u01d7\7G\2\2\u01d7\u01d8\7P\2\2\u01d8"+ - "\u01d9\7F\2\2\u01d9>\3\2\2\2\u01da\u01db\7G\2\2\u01db\u01dc\7U\2\2\u01dc"+ - "\u01dd\7E\2\2\u01dd\u01de\7C\2\2\u01de\u01df\7R\2\2\u01df\u01e0\7G\2\2"+ - "\u01e0@\3\2\2\2\u01e1\u01e2\7G\2\2\u01e2\u01e3\7Z\2\2\u01e3\u01e4\7G\2"+ - "\2\u01e4\u01e5\7E\2\2\u01e5\u01e6\7W\2\2\u01e6\u01e7\7V\2\2\u01e7\u01e8"+ - "\7C\2\2\u01e8\u01e9\7D\2\2\u01e9\u01ea\7N\2\2\u01ea\u01eb\7G\2\2\u01eb"+ - "B\3\2\2\2\u01ec\u01ed\7G\2\2\u01ed\u01ee\7Z\2\2\u01ee\u01ef\7K\2\2\u01ef"+ - "\u01f0\7U\2\2\u01f0\u01f1\7V\2\2\u01f1\u01f2\7U\2\2\u01f2D\3\2\2\2\u01f3"+ - "\u01f4\7G\2\2\u01f4\u01f5\7Z\2\2\u01f5\u01f6\7R\2\2\u01f6\u01f7\7N\2\2"+ - "\u01f7\u01f8\7C\2\2\u01f8\u01f9\7K\2\2\u01f9\u01fa\7P\2\2\u01faF\3\2\2"+ - "\2\u01fb\u01fc\7G\2\2\u01fc\u01fd\7Z\2\2\u01fd\u01fe\7V\2\2\u01fe\u01ff"+ - "\7T\2\2\u01ff\u0200\7C\2\2\u0200\u0201\7E\2\2\u0201\u0202\7V\2\2\u0202"+ - "H\3\2\2\2\u0203\u0204\7H\2\2\u0204\u0205\7C\2\2\u0205\u0206\7N\2\2\u0206"+ - "\u0207\7U\2\2\u0207\u0208\7G\2\2\u0208J\3\2\2\2\u0209\u020a\7H\2\2\u020a"+ - "\u020b\7K\2\2\u020b\u020c\7T\2\2\u020c\u020d\7U\2\2\u020d\u020e\7V\2\2"+ - "\u020eL\3\2\2\2\u020f\u0210\7H\2\2\u0210\u0211\7Q\2\2\u0211\u0212\7T\2"+ - "\2\u0212N\3\2\2\2\u0213\u0214\7H\2\2\u0214\u0215\7Q\2\2\u0215\u0216\7"+ - "T\2\2\u0216\u0217\7O\2\2\u0217\u0218\7C\2\2\u0218\u0219\7V\2\2\u0219P"+ - "\3\2\2\2\u021a\u021b\7H\2\2\u021b\u021c\7T\2\2\u021c\u021d\7Q\2\2\u021d"+ - "\u021e\7O\2\2\u021eR\3\2\2\2\u021f\u0220\7H\2\2\u0220\u0221\7T\2\2\u0221"+ - "\u0222\7Q\2\2\u0222\u0223\7\\\2\2\u0223\u0224\7G\2\2\u0224\u0225\7P\2"+ - "\2\u0225T\3\2\2\2\u0226\u0227\7H\2\2\u0227\u0228\7W\2\2\u0228\u0229\7"+ - "N\2\2\u0229\u022a\7N\2\2\u022aV\3\2\2\2\u022b\u022c\7H\2\2\u022c\u022d"+ - "\7W\2\2\u022d\u022e\7P\2\2\u022e\u022f\7E\2\2\u022f\u0230\7V\2\2\u0230"+ - "\u0231\7K\2\2\u0231\u0232\7Q\2\2\u0232\u0233\7P\2\2\u0233\u0234\7U\2\2"+ - "\u0234X\3\2\2\2\u0235\u0236\7I\2\2\u0236\u0237\7T\2\2\u0237\u0238\7C\2"+ - "\2\u0238\u0239\7R\2\2\u0239\u023a\7J\2\2\u023a\u023b\7X\2\2\u023b\u023c"+ - "\7K\2\2\u023c\u023d\7\\\2\2\u023dZ\3\2\2\2\u023e\u023f\7I\2\2\u023f\u0240"+ - "\7T\2\2\u0240\u0241\7Q\2\2\u0241\u0242\7W\2\2\u0242\u0243\7R\2\2\u0243"+ - "\\\3\2\2\2\u0244\u0245\7J\2\2\u0245\u0246\7C\2\2\u0246\u0247\7X\2\2\u0247"+ - "\u0248\7K\2\2\u0248\u0249\7P\2\2\u0249\u024a\7I\2\2\u024a^\3\2\2\2\u024b"+ - "\u024c\7J\2\2\u024c\u024d\7Q\2\2\u024d\u024e\7W\2\2\u024e\u024f\7T\2\2"+ - "\u024f`\3\2\2\2\u0250\u0251\7J\2\2\u0251\u0252\7Q\2\2\u0252\u0253\7W\2"+ - "\2\u0253\u0254\7T\2\2\u0254\u0255\7U\2\2\u0255b\3\2\2\2\u0256\u0257\7"+ - "K\2\2\u0257\u0258\7P\2\2\u0258d\3\2\2\2\u0259\u025a\7K\2\2\u025a\u025b"+ - "\7P\2\2\u025b\u025c\7E\2\2\u025c\u025d\7N\2\2\u025d\u025e\7W\2\2\u025e"+ - "\u025f\7F\2\2\u025f\u0260\7G\2\2\u0260f\3\2\2\2\u0261\u0262\7K\2\2\u0262"+ - "\u0263\7P\2\2\u0263\u0264\7P\2\2\u0264\u0265\7G\2\2\u0265\u0266\7T\2\2"+ - "\u0266h\3\2\2\2\u0267\u0268\7K\2\2\u0268\u0269\7P\2\2\u0269\u026a\7V\2"+ - "\2\u026a\u026b\7G\2\2\u026b\u026c\7T\2\2\u026c\u026d\7X\2\2\u026d\u026e"+ - "\7C\2\2\u026e\u026f\7N\2\2\u026fj\3\2\2\2\u0270\u0271\7K\2\2\u0271\u0272"+ - "\7U\2\2\u0272l\3\2\2\2\u0273\u0274\7L\2\2\u0274\u0275\7Q\2\2\u0275\u0276"+ - "\7K\2\2\u0276\u0277\7P\2\2\u0277n\3\2\2\2\u0278\u0279\7N\2\2\u0279\u027a"+ - "\7C\2\2\u027a\u027b\7U\2\2\u027b\u027c\7V\2\2\u027cp\3\2\2\2\u027d\u027e"+ - "\7N\2\2\u027e\u027f\7G\2\2\u027f\u0280\7H\2\2\u0280\u0281\7V\2\2\u0281"+ - "r\3\2\2\2\u0282\u0283\7N\2\2\u0283\u0284\7K\2\2\u0284\u0285\7M\2\2\u0285"+ - "\u0286\7G\2\2\u0286t\3\2\2\2\u0287\u0288\7N\2\2\u0288\u0289\7K\2\2\u0289"+ - "\u028a\7O\2\2\u028a\u028b\7K\2\2\u028b\u028c\7V\2\2\u028cv\3\2\2\2\u028d"+ - "\u028e\7O\2\2\u028e\u028f\7C\2\2\u028f\u0290\7R\2\2\u0290\u0291\7R\2\2"+ - "\u0291\u0292\7G\2\2\u0292\u0293\7F\2\2\u0293x\3\2\2\2\u0294\u0295\7O\2"+ - "\2\u0295\u0296\7C\2\2\u0296\u0297\7V\2\2\u0297\u0298\7E\2\2\u0298\u0299"+ - "\7J\2\2\u0299z\3\2\2\2\u029a\u029b\7O\2\2\u029b\u029c\7K\2\2\u029c\u029d"+ - "\7P\2\2\u029d\u029e\7W\2\2\u029e\u029f\7V\2\2\u029f\u02a0\7G\2\2\u02a0"+ - "|\3\2\2\2\u02a1\u02a2\7O\2\2\u02a2\u02a3\7K\2\2\u02a3\u02a4\7P\2\2\u02a4"+ - "\u02a5\7W\2\2\u02a5\u02a6\7V\2\2\u02a6\u02a7\7G\2\2\u02a7\u02a8\7U\2\2"+ - "\u02a8~\3\2\2\2\u02a9\u02aa\7O\2\2\u02aa\u02ab\7Q\2\2\u02ab\u02ac\7P\2"+ - "\2\u02ac\u02ad\7V\2\2\u02ad\u02ae\7J\2\2\u02ae\u0080\3\2\2\2\u02af\u02b0"+ - "\7O\2\2\u02b0\u02b1\7Q\2\2\u02b1\u02b2\7P\2\2\u02b2\u02b3\7V\2\2\u02b3"+ - "\u02b4\7J\2\2\u02b4\u02b5\7U\2\2\u02b5\u0082\3\2\2\2\u02b6\u02b7\7P\2"+ - "\2\u02b7\u02b8\7C\2\2\u02b8\u02b9\7V\2\2\u02b9\u02ba\7W\2\2\u02ba\u02bb"+ - "\7T\2\2\u02bb\u02bc\7C\2\2\u02bc\u02bd\7N\2\2\u02bd\u0084\3\2\2\2\u02be"+ - "\u02bf\7P\2\2\u02bf\u02c0\7Q\2\2\u02c0\u02c1\7V\2\2\u02c1\u0086\3\2\2"+ - "\2\u02c2\u02c3\7P\2\2\u02c3\u02c4\7W\2\2\u02c4\u02c5\7N\2\2\u02c5\u02c6"+ - "\7N\2\2\u02c6\u0088\3\2\2\2\u02c7\u02c8\7P\2\2\u02c8\u02c9\7W\2\2\u02c9"+ - "\u02ca\7N\2\2\u02ca\u02cb\7N\2\2\u02cb\u02cc\7U\2\2\u02cc\u008a\3\2\2"+ - "\2\u02cd\u02ce\7Q\2\2\u02ce\u02cf\7P\2\2\u02cf\u008c\3\2\2\2\u02d0\u02d1"+ - "\7Q\2\2\u02d1\u02d2\7R\2\2\u02d2\u02d3\7V\2\2\u02d3\u02d4\7K\2\2\u02d4"+ - "\u02d5\7O\2\2\u02d5\u02d6\7K\2\2\u02d6\u02d7\7\\\2\2\u02d7\u02d8\7G\2"+ - "\2\u02d8\u02d9\7F\2\2\u02d9\u008e\3\2\2\2\u02da\u02db\7Q\2\2\u02db\u02dc"+ - "\7T\2\2\u02dc\u0090\3\2\2\2\u02dd\u02de\7Q\2\2\u02de\u02df\7T\2\2\u02df"+ - "\u02e0\7F\2\2\u02e0\u02e1\7G\2\2\u02e1\u02e2\7T\2\2\u02e2\u0092\3\2\2"+ - "\2\u02e3\u02e4\7Q\2\2\u02e4\u02e5\7W\2\2\u02e5\u02e6\7V\2\2\u02e6\u02e7"+ - "\7G\2\2\u02e7\u02e8\7T\2\2\u02e8\u0094\3\2\2\2\u02e9\u02ea\7R\2\2\u02ea"+ - "\u02eb\7C\2\2\u02eb\u02ec\7T\2\2\u02ec\u02ed\7U\2\2\u02ed\u02ee\7G\2\2"+ - "\u02ee\u02ef\7F\2\2\u02ef\u0096\3\2\2\2\u02f0\u02f1\7R\2\2\u02f1\u02f2"+ - "\7J\2\2\u02f2\u02f3\7[\2\2\u02f3\u02f4\7U\2\2\u02f4\u02f5\7K\2\2\u02f5"+ - "\u02f6\7E\2\2\u02f6\u02f7\7C\2\2\u02f7\u02f8\7N\2\2\u02f8\u0098\3\2\2"+ - "\2\u02f9\u02fa\7R\2\2\u02fa\u02fb\7K\2\2\u02fb\u02fc\7X\2\2\u02fc\u02fd"+ - "\7Q\2\2\u02fd\u02fe\7V\2\2\u02fe\u009a\3\2\2\2\u02ff\u0300\7R\2\2\u0300"+ - "\u0301\7N\2\2\u0301\u0302\7C\2\2\u0302\u0303\7P\2\2\u0303\u009c\3\2\2"+ - "\2\u0304\u0305\7T\2\2\u0305\u0306\7K\2\2\u0306\u0307\7I\2\2\u0307\u0308"+ - "\7J\2\2\u0308\u0309\7V\2\2\u0309\u009e\3\2\2\2\u030a\u030b\7T\2\2\u030b"+ - "\u030c\7N\2\2\u030c\u030d\7K\2\2\u030d\u030e\7M\2\2\u030e\u030f\7G\2\2"+ - "\u030f\u00a0\3\2\2\2\u0310\u0311\7S\2\2\u0311\u0312\7W\2\2\u0312\u0313"+ - "\7G\2\2\u0313\u0314\7T\2\2\u0314\u0315\7[\2\2\u0315\u00a2\3\2\2\2\u0316"+ - "\u0317\7U\2\2\u0317\u0318\7E\2\2\u0318\u0319\7J\2\2\u0319\u031a\7G\2\2"+ - "\u031a\u031b\7O\2\2\u031b\u031c\7C\2\2\u031c\u031d\7U\2\2\u031d\u00a4"+ - "\3\2\2\2\u031e\u031f\7U\2\2\u031f\u0320\7G\2\2\u0320\u0321\7E\2\2\u0321"+ - "\u0322\7Q\2\2\u0322\u0323\7P\2\2\u0323\u0324\7F\2\2\u0324\u00a6\3\2\2"+ - "\2\u0325\u0326\7U\2\2\u0326\u0327\7G\2\2\u0327\u0328\7E\2\2\u0328\u0329"+ - "\7Q\2\2\u0329\u032a\7P\2\2\u032a\u032b\7F\2\2\u032b\u032c\7U\2\2\u032c"+ - "\u00a8\3\2\2\2\u032d\u032e\7U\2\2\u032e\u032f\7G\2\2\u032f\u0330\7N\2"+ - "\2\u0330\u0331\7G\2\2\u0331\u0332\7E\2\2\u0332\u0333\7V\2\2\u0333\u00aa"+ - "\3\2\2\2\u0334\u0335\7U\2\2\u0335\u0336\7J\2\2\u0336\u0337\7Q\2\2\u0337"+ - "\u0338\7Y\2\2\u0338\u00ac\3\2\2\2\u0339\u033a\7U\2\2\u033a\u033b\7[\2"+ - "\2\u033b\u033c\7U\2\2\u033c\u00ae\3\2\2\2\u033d\u033e\7V\2\2\u033e\u033f"+ - "\7C\2\2\u033f\u0340\7D\2\2\u0340\u0341\7N\2\2\u0341\u0342\7G\2\2\u0342"+ - "\u00b0\3\2\2\2\u0343\u0344\7V\2\2\u0344\u0345\7C\2\2\u0345\u0346\7D\2"+ - "\2\u0346\u0347\7N\2\2\u0347\u0348\7G\2\2\u0348\u0349\7U\2\2\u0349\u00b2"+ - "\3\2\2\2\u034a\u034b\7V\2\2\u034b\u034c\7G\2\2\u034c\u034d\7Z\2\2\u034d"+ - "\u034e\7V\2\2\u034e\u00b4\3\2\2\2\u034f\u0350\7V\2\2\u0350\u0351\7J\2"+ - "\2\u0351\u0352\7G\2\2\u0352\u0353\7P\2\2\u0353\u00b6\3\2\2\2\u0354\u0355"+ - "\7V\2\2\u0355\u0356\7T\2\2\u0356\u0357\7W\2\2\u0357\u0358\7G\2\2\u0358"+ - "\u00b8\3\2\2\2\u0359\u035a\7V\2\2\u035a\u035b\7Q\2\2\u035b\u00ba\3\2\2"+ - "\2\u035c\u035d\7V\2\2\u035d\u035e\7Q\2\2\u035e\u035f\7R\2\2\u035f\u00bc"+ - "\3\2\2\2\u0360\u0361\7V\2\2\u0361\u0362\7[\2\2\u0362\u0363\7R\2\2\u0363"+ - "\u0364\7G\2\2\u0364\u00be\3\2\2\2\u0365\u0366\7V\2\2\u0366\u0367\7[\2"+ - "\2\u0367\u0368\7R\2\2\u0368\u0369\7G\2\2\u0369\u036a\7U\2\2\u036a\u00c0"+ - "\3\2\2\2\u036b\u036c\7W\2\2\u036c\u036d\7U\2\2\u036d\u036e\7K\2\2\u036e"+ - "\u036f\7P\2\2\u036f\u0370\7I\2\2\u0370\u00c2\3\2\2\2\u0371\u0372\7X\2"+ - "\2\u0372\u0373\7G\2\2\u0373\u0374\7T\2\2\u0374\u0375\7K\2\2\u0375\u0376"+ - "\7H\2\2\u0376\u0377\7[\2\2\u0377\u00c4\3\2\2\2\u0378\u0379\7Y\2\2\u0379"+ - "\u037a\7J\2\2\u037a\u037b\7G\2\2\u037b\u037c\7P\2\2\u037c\u00c6\3\2\2"+ - "\2\u037d\u037e\7Y\2\2\u037e\u037f\7J\2\2\u037f\u0380\7G\2\2\u0380\u0381"+ - "\7T\2\2\u0381\u0382\7G\2\2\u0382\u00c8\3\2\2\2\u0383\u0384\7Y\2\2\u0384"+ - "\u0385\7K\2\2\u0385\u0386\7V\2\2\u0386\u0387\7J\2\2\u0387\u00ca\3\2\2"+ - "\2\u0388\u0389\7[\2\2\u0389\u038a\7G\2\2\u038a\u038b\7C\2\2\u038b\u038c"+ - "\7T\2\2\u038c\u00cc\3\2\2\2\u038d\u038e\7[\2\2\u038e\u038f\7G\2\2\u038f"+ - "\u0390\7C\2\2\u0390\u0391\7T\2\2\u0391\u0392\7U\2\2\u0392\u00ce\3\2\2"+ - "\2\u0393\u0394\5\u00ddo\2\u0394\u0395\7G\2\2\u0395\u0396\7U\2\2\u0396"+ - "\u0397\7E\2\2\u0397\u0398\7C\2\2\u0398\u0399\7R\2\2\u0399\u039a\7G\2\2"+ - "\u039a\u00d0\3\2\2\2\u039b\u039c\5\u00ddo\2\u039c\u039d\7H\2\2\u039d\u039e"+ - "\7P\2\2\u039e\u00d2\3\2\2\2\u039f\u03a0\5\u00ddo\2\u03a0\u03a1\7N\2\2"+ - "\u03a1\u03a2\7K\2\2\u03a2\u03a3\7O\2\2\u03a3\u03a4\7K\2\2\u03a4\u03a5"+ - "\7V\2\2\u03a5\u00d4\3\2\2\2\u03a6\u03a7\5\u00ddo\2\u03a7\u03a8\7F\2\2"+ - "\u03a8\u00d6\3\2\2\2\u03a9\u03aa\5\u00ddo\2\u03aa\u03ab\7V\2\2\u03ab\u00d8"+ - "\3\2\2\2\u03ac\u03ad\5\u00ddo\2\u03ad\u03ae\7V\2\2\u03ae\u03af\7U\2\2"+ - "\u03af\u00da\3\2\2\2\u03b0\u03b1\5\u00ddo\2\u03b1\u03b2\7I\2\2\u03b2\u03b3"+ - "\7W\2\2\u03b3\u03b4\7K\2\2\u03b4\u03b5\7F\2\2\u03b5\u00dc\3\2\2\2\u03b6"+ - "\u03ba\7}\2\2\u03b7\u03b9\5\u0119\u008d\2\u03b8\u03b7\3\2\2\2\u03b9\u03bc"+ - "\3\2\2\2\u03ba\u03b8\3\2\2\2\u03ba\u03bb\3\2\2\2\u03bb\u00de\3\2\2\2\u03bc"+ - "\u03ba\3\2\2\2\u03bd\u03be\7\177\2\2\u03be\u00e0\3\2\2\2\u03bf\u03c0\7"+ - "?\2\2\u03c0\u00e2\3\2\2\2\u03c1\u03c2\7>\2\2\u03c2\u03c3\7?\2\2\u03c3"+ - "\u03c4\7@\2\2\u03c4\u00e4\3\2\2\2\u03c5\u03c6\7>\2\2\u03c6\u03ca\7@\2"+ - "\2\u03c7\u03c8\7#\2\2\u03c8\u03ca\7?\2\2\u03c9\u03c5\3\2\2\2\u03c9\u03c7"+ - "\3\2\2\2\u03ca\u00e6\3\2\2\2\u03cb\u03cc\7>\2\2\u03cc\u00e8\3\2\2\2\u03cd"+ - "\u03ce\7>\2\2\u03ce\u03cf\7?\2\2\u03cf\u00ea\3\2\2\2\u03d0\u03d1\7@\2"+ - "\2\u03d1\u00ec\3\2\2\2\u03d2\u03d3\7@\2\2\u03d3\u03d4\7?\2\2\u03d4\u00ee"+ - "\3\2\2\2\u03d5\u03d6\7-\2\2\u03d6\u00f0\3\2\2\2\u03d7\u03d8\7/\2\2\u03d8"+ - "\u00f2\3\2\2\2\u03d9\u03da\7,\2\2\u03da\u00f4\3\2\2\2\u03db\u03dc\7\61"+ - "\2\2\u03dc\u00f6\3\2\2\2\u03dd\u03de\7\'\2\2\u03de\u00f8\3\2\2\2\u03df"+ - "\u03e0\7<\2\2\u03e0\u03e1\7<\2\2\u03e1\u00fa\3\2\2\2\u03e2\u03e3\7\60"+ - "\2\2\u03e3\u00fc\3\2\2\2\u03e4\u03e5\7A\2\2\u03e5\u00fe\3\2\2\2\u03e6"+ - "\u03ec\7)\2\2\u03e7\u03eb\n\2\2\2\u03e8\u03e9\7)\2\2\u03e9\u03eb\7)\2"+ - "\2\u03ea\u03e7\3\2\2\2\u03ea\u03e8\3\2\2\2\u03eb\u03ee\3\2\2\2\u03ec\u03ea"+ - "\3\2\2\2\u03ec\u03ed\3\2\2\2\u03ed\u03ef\3\2\2\2\u03ee\u03ec\3\2\2\2\u03ef"+ - "\u03f0\7)\2\2\u03f0\u0100\3\2\2\2\u03f1\u03f3\5\u0111\u0089\2\u03f2\u03f1"+ - "\3\2\2\2\u03f3\u03f4\3\2\2\2\u03f4\u03f2\3\2\2\2\u03f4\u03f5\3\2\2\2\u03f5"+ - "\u0102\3\2\2\2\u03f6\u03f8\5\u0111\u0089\2\u03f7\u03f6\3\2\2\2\u03f8\u03f9"+ - "\3\2\2\2\u03f9\u03f7\3\2\2\2\u03f9\u03fa\3\2\2\2\u03fa\u03fb\3\2\2\2\u03fb"+ - "\u03ff\5\u00fb~\2\u03fc\u03fe\5\u0111\u0089\2\u03fd\u03fc\3\2\2\2\u03fe"+ - "\u0401\3\2\2\2\u03ff\u03fd\3\2\2\2\u03ff\u0400\3\2\2\2\u0400\u0421\3\2"+ - "\2\2\u0401\u03ff\3\2\2\2\u0402\u0404\5\u00fb~\2\u0403\u0405\5\u0111\u0089"+ - "\2\u0404\u0403\3\2\2\2\u0405\u0406\3\2\2\2\u0406\u0404\3\2\2\2\u0406\u0407"+ - "\3\2\2\2\u0407\u0421\3\2\2\2\u0408\u040a\5\u0111\u0089\2\u0409\u0408\3"+ - "\2\2\2\u040a\u040b\3\2\2\2\u040b\u0409\3\2\2\2\u040b\u040c\3\2\2\2\u040c"+ - "\u0414\3\2\2\2\u040d\u0411\5\u00fb~\2\u040e\u0410\5\u0111\u0089\2\u040f"+ - "\u040e\3\2\2\2\u0410\u0413\3\2\2\2\u0411\u040f\3\2\2\2\u0411\u0412\3\2"+ - "\2\2\u0412\u0415\3\2\2\2\u0413\u0411\3\2\2\2\u0414\u040d\3\2\2\2\u0414"+ - "\u0415\3\2\2\2\u0415\u0416\3\2\2\2\u0416\u0417\5\u010f\u0088\2\u0417\u0421"+ - "\3\2\2\2\u0418\u041a\5\u00fb~\2\u0419\u041b\5\u0111\u0089\2\u041a\u0419"+ - "\3\2\2\2\u041b\u041c\3\2\2\2\u041c\u041a\3\2\2\2\u041c\u041d\3\2\2\2\u041d"+ - "\u041e\3\2\2\2\u041e\u041f\5\u010f\u0088\2\u041f\u0421\3\2\2\2\u0420\u03f7"+ - "\3\2\2\2\u0420\u0402\3\2\2\2\u0420\u0409\3\2\2\2\u0420\u0418\3\2\2\2\u0421"+ - "\u0104\3\2\2\2\u0422\u0425\5\u0113\u008a\2\u0423\u0425\7a\2\2\u0424\u0422"+ - "\3\2\2\2\u0424\u0423\3\2\2\2\u0425\u042b\3\2\2\2\u0426\u042a\5\u0113\u008a"+ - "\2\u0427\u042a\5\u0111\u0089\2\u0428\u042a\t\3\2\2\u0429\u0426\3\2\2\2"+ - "\u0429\u0427\3\2\2\2\u0429\u0428\3\2\2\2\u042a\u042d\3\2\2\2\u042b\u0429"+ - "\3\2\2\2\u042b\u042c\3\2\2\2\u042c\u0106\3\2\2\2\u042d\u042b\3\2\2\2\u042e"+ - "\u0432\5\u0111\u0089\2\u042f\u0433\5\u0113\u008a\2\u0430\u0433\5\u0111"+ - "\u0089\2\u0431\u0433\t\3\2\2\u0432\u042f\3\2\2\2\u0432\u0430\3\2\2\2\u0432"+ - "\u0431\3\2\2\2\u0433\u0434\3\2\2\2\u0434\u0432\3\2\2\2\u0434\u0435\3\2"+ - "\2\2\u0435\u0108\3\2\2\2\u0436\u043a\5\u0113\u008a\2\u0437\u043a\5\u0111"+ - "\u0089\2\u0438\u043a\7a\2\2\u0439\u0436\3\2\2\2\u0439\u0437\3\2\2\2\u0439"+ - "\u0438\3\2\2\2\u043a\u043b\3\2\2\2\u043b\u0439\3\2\2\2\u043b\u043c\3\2"+ - "\2\2\u043c\u010a\3\2\2\2\u043d\u0443\7$\2\2\u043e\u0442\n\4\2\2\u043f"+ - "\u0440\7$\2\2\u0440\u0442\7$\2\2\u0441\u043e\3\2\2\2\u0441\u043f\3\2\2"+ - "\2\u0442\u0445\3\2\2\2\u0443\u0441\3\2\2\2\u0443\u0444\3\2\2\2\u0444\u0446"+ - "\3\2\2\2\u0445\u0443\3\2\2\2\u0446\u0447\7$\2\2\u0447\u010c\3\2\2\2\u0448"+ - "\u044e\7b\2\2\u0449\u044d\n\5\2\2\u044a\u044b\7b\2\2\u044b\u044d\7b\2"+ - "\2\u044c\u0449\3\2\2\2\u044c\u044a\3\2\2\2\u044d\u0450\3\2\2\2\u044e\u044c"+ - "\3\2\2\2\u044e\u044f\3\2\2\2\u044f\u0451\3\2\2\2\u0450\u044e\3\2\2\2\u0451"+ - "\u0452\7b\2\2\u0452\u010e\3\2\2\2\u0453\u0455\7G\2\2\u0454\u0456\t\6\2"+ - "\2\u0455\u0454\3\2\2\2\u0455\u0456\3\2\2\2\u0456\u0458\3\2\2\2\u0457\u0459"+ - "\5\u0111\u0089\2\u0458\u0457\3\2\2\2\u0459\u045a\3\2\2\2\u045a\u0458\3"+ - "\2\2\2\u045a\u045b\3\2\2\2\u045b\u0110\3\2\2\2\u045c\u045d\t\7\2\2\u045d"+ - "\u0112\3\2\2\2\u045e\u045f\t\b\2\2\u045f\u0114\3\2\2\2\u0460\u0461\7/"+ - "\2\2\u0461\u0462\7/\2\2\u0462\u0466\3\2\2\2\u0463\u0465\n\t\2\2\u0464"+ - "\u0463\3\2\2\2\u0465\u0468\3\2\2\2\u0466\u0464\3\2\2\2\u0466\u0467\3\2"+ - "\2\2\u0467\u046a\3\2\2\2\u0468\u0466\3\2\2\2\u0469\u046b\7\17\2\2\u046a"+ - "\u0469\3\2\2\2\u046a\u046b\3\2\2\2\u046b\u046d\3\2\2\2\u046c\u046e\7\f"+ - "\2\2\u046d\u046c\3\2\2\2\u046d\u046e\3\2\2\2\u046e\u046f\3\2\2\2\u046f"+ - "\u0470\b\u008b\2\2\u0470\u0116\3\2\2\2\u0471\u0472\7\61\2\2\u0472\u0473"+ - "\7,\2\2\u0473\u0478\3\2\2\2\u0474\u0477\5\u0117\u008c\2\u0475\u0477\13"+ - "\2\2\2\u0476\u0474\3\2\2\2\u0476\u0475\3\2\2\2\u0477\u047a\3\2\2\2\u0478"+ - "\u0479\3\2\2\2\u0478\u0476\3\2\2\2\u0479\u047b\3\2\2\2\u047a\u0478\3\2"+ - "\2\2\u047b\u047c\7,\2\2\u047c\u047d\7\61\2\2\u047d\u047e\3\2\2\2\u047e"+ - "\u047f\b\u008c\2\2\u047f\u0118\3\2\2\2\u0480\u0482\t\n\2\2\u0481\u0480"+ - "\3\2\2\2\u0482\u0483\3\2\2\2\u0483\u0481\3\2\2\2\u0483\u0484\3\2\2\2\u0484"+ - "\u0485\3\2\2\2\u0485\u0486\b\u008d\2\2\u0486\u011a\3\2\2\2\u0487\u0488"+ - "\13\2\2\2\u0488\u011c\3\2\2\2#\2\u03ba\u03c9\u03ea\u03ec\u03f4\u03f9\u03ff"+ - "\u0406\u040b\u0411\u0414\u041c\u0420\u0424\u0429\u042b\u0432\u0434\u0439"+ - "\u043b\u0441\u0443\u044c\u044e\u0455\u045a\u0466\u046a\u046d\u0476\u0478"+ - "\u0483\3\2\3\2"; - public static final ATN _ATN = - new ATNDeserializer().deserialize(_serializedATN.toCharArray()); - static { - _decisionToDFA = new DFA[_ATN.getNumberOfDecisions()]; - for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) { - _decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i); + public static final String _serializedATN = "\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\2\u008c\u0489\b\1\4" + + "\2\t\2\4\3\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b\t\b\4\t\t\t\4\n\t\n" + + "\4\13\t\13\4\f\t\f\4\r\t\r\4\16\t\16\4\17\t\17\4\20\t\20\4\21\t\21\4\22" + + "\t\22\4\23\t\23\4\24\t\24\4\25\t\25\4\26\t\26\4\27\t\27\4\30\t\30\4\31" + + "\t\31\4\32\t\32\4\33\t\33\4\34\t\34\4\35\t\35\4\36\t\36\4\37\t\37\4 \t" + + " \4!\t!\4\"\t\"\4#\t#\4$\t$\4%\t%\4&\t&\4\'\t\'\4(\t(\4)\t)\4*\t*\4+\t" + + "+\4,\t,\4-\t-\4.\t.\4/\t/\4\60\t\60\4\61\t\61\4\62\t\62\4\63\t\63\4\64" + + "\t\64\4\65\t\65\4\66\t\66\4\67\t\67\48\t8\49\t9\4:\t:\4;\t;\4<\t<\4=\t" + + "=\4>\t>\4?\t?\4@\t@\4A\tA\4B\tB\4C\tC\4D\tD\4E\tE\4F\tF\4G\tG\4H\tH\4" + + "I\tI\4J\tJ\4K\tK\4L\tL\4M\tM\4N\tN\4O\tO\4P\tP\4Q\tQ\4R\tR\4S\tS\4T\t" + + "T\4U\tU\4V\tV\4W\tW\4X\tX\4Y\tY\4Z\tZ\4[\t[\4\\\t\\\4]\t]\4^\t^\4_\t_" + + "\4`\t`\4a\ta\4b\tb\4c\tc\4d\td\4e\te\4f\tf\4g\tg\4h\th\4i\ti\4j\tj\4k" + + "\tk\4l\tl\4m\tm\4n\tn\4o\to\4p\tp\4q\tq\4r\tr\4s\ts\4t\tt\4u\tu\4v\tv" + + "\4w\tw\4x\tx\4y\ty\4z\tz\4{\t{\4|\t|\4}\t}\4~\t~\4\177\t\177\4\u0080\t" + + "\u0080\4\u0081\t\u0081\4\u0082\t\u0082\4\u0083\t\u0083\4\u0084\t\u0084" + + "\4\u0085\t\u0085\4\u0086\t\u0086\4\u0087\t\u0087\4\u0088\t\u0088\4\u0089" + + "\t\u0089\4\u008a\t\u008a\4\u008b\t\u008b\4\u008c\t\u008c\4\u008d\t\u008d" + + "\4\u008e\t\u008e\3\2\3\2\3\3\3\3\3\4\3\4\3\5\3\5\3\6\3\6\3\6\3\6\3\7\3" + + "\7\3\7\3\7\3\7\3\7\3\7\3\7\3\b\3\b\3\b\3\b\3\b\3\b\3\b\3\b\3\b\3\t\3\t" + + "\3\t\3\t\3\n\3\n\3\n\3\n\3\13\3\13\3\13\3\f\3\f\3\f\3\f\3\r\3\r\3\r\3" + + "\r\3\r\3\r\3\r\3\r\3\16\3\16\3\16\3\17\3\17\3\17\3\17\3\17\3\20\3\20\3" + + "\20\3\20\3\20\3\21\3\21\3\21\3\21\3\21\3\21\3\21\3\21\3\22\3\22\3\22\3" + + "\22\3\22\3\22\3\22\3\22\3\22\3\23\3\23\3\23\3\23\3\23\3\23\3\23\3\23\3" + + "\24\3\24\3\24\3\24\3\24\3\24\3\24\3\24\3\25\3\25\3\25\3\25\3\25\3\25\3" + + "\25\3\25\3\25\3\25\3\25\3\25\3\25\3\26\3\26\3\26\3\26\3\26\3\26\3\26\3" + + "\26\3\26\3\26\3\26\3\26\3\26\3\27\3\27\3\27\3\27\3\27\3\27\3\27\3\27\3" + + "\27\3\27\3\27\3\27\3\27\3\27\3\27\3\27\3\27\3\27\3\30\3\30\3\30\3\30\3" + + "\31\3\31\3\31\3\31\3\31\3\32\3\32\3\32\3\32\3\32\3\32\3\33\3\33\3\33\3" + + "\33\3\33\3\34\3\34\3\34\3\34\3\34\3\34\3\34\3\34\3\34\3\35\3\35\3\35\3" + + "\35\3\35\3\35\3\35\3\35\3\35\3\36\3\36\3\36\3\36\3\36\3\37\3\37\3\37\3" + + "\37\3 \3 \3 \3 \3 \3 \3 \3!\3!\3!\3!\3!\3!\3!\3!\3!\3!\3!\3\"\3\"\3\"" + + "\3\"\3\"\3\"\3\"\3#\3#\3#\3#\3#\3#\3#\3#\3$\3$\3$\3$\3$\3$\3$\3$\3%\3" + + "%\3%\3%\3%\3%\3&\3&\3&\3&\3&\3&\3\'\3\'\3\'\3\'\3(\3(\3(\3(\3(\3(\3(\3" + + ")\3)\3)\3)\3)\3*\3*\3*\3*\3*\3*\3*\3+\3+\3+\3+\3+\3,\3,\3,\3,\3,\3,\3" + + ",\3,\3,\3,\3-\3-\3-\3-\3-\3-\3-\3-\3-\3.\3.\3.\3.\3.\3.\3/\3/\3/\3/\3" + + "/\3/\3/\3\60\3\60\3\60\3\60\3\60\3\61\3\61\3\61\3\61\3\61\3\61\3\62\3" + + "\62\3\62\3\63\3\63\3\63\3\63\3\63\3\63\3\63\3\63\3\64\3\64\3\64\3\64\3" + + "\64\3\64\3\65\3\65\3\65\3\65\3\65\3\65\3\65\3\65\3\65\3\66\3\66\3\66\3" + + "\67\3\67\3\67\3\67\3\67\38\38\38\38\38\39\39\39\39\39\3:\3:\3:\3:\3:\3" + + ";\3;\3;\3;\3;\3;\3<\3<\3<\3<\3<\3<\3<\3=\3=\3=\3=\3=\3=\3>\3>\3>\3>\3" + + ">\3>\3>\3?\3?\3?\3?\3?\3?\3?\3?\3@\3@\3@\3@\3@\3@\3A\3A\3A\3A\3A\3A\3" + + "A\3B\3B\3B\3B\3B\3B\3B\3B\3C\3C\3C\3C\3D\3D\3D\3D\3D\3E\3E\3E\3E\3E\3" + + "E\3F\3F\3F\3G\3G\3G\3G\3G\3G\3G\3G\3G\3G\3H\3H\3H\3I\3I\3I\3I\3I\3I\3" + + "J\3J\3J\3J\3J\3J\3K\3K\3K\3K\3K\3K\3K\3L\3L\3L\3L\3L\3L\3L\3L\3L\3M\3" + + "M\3M\3M\3M\3M\3N\3N\3N\3N\3N\3O\3O\3O\3O\3O\3O\3P\3P\3P\3P\3P\3P\3Q\3" + + "Q\3Q\3Q\3Q\3Q\3R\3R\3R\3R\3R\3R\3R\3R\3S\3S\3S\3S\3S\3S\3S\3T\3T\3T\3" + + "T\3T\3T\3T\3T\3U\3U\3U\3U\3U\3U\3U\3V\3V\3V\3V\3V\3W\3W\3W\3W\3X\3X\3" + + "X\3X\3X\3X\3Y\3Y\3Y\3Y\3Y\3Y\3Y\3Z\3Z\3Z\3Z\3Z\3[\3[\3[\3[\3[\3\\\3\\" + + "\3\\\3\\\3\\\3]\3]\3]\3^\3^\3^\3^\3_\3_\3_\3_\3_\3`\3`\3`\3`\3`\3`\3a" + + "\3a\3a\3a\3a\3a\3b\3b\3b\3b\3b\3b\3b\3c\3c\3c\3c\3c\3d\3d\3d\3d\3d\3d" + + "\3e\3e\3e\3e\3e\3f\3f\3f\3f\3f\3g\3g\3g\3g\3g\3g\3h\3h\3h\3h\3h\3h\3h" + + "\3h\3i\3i\3i\3i\3j\3j\3j\3j\3j\3j\3j\3k\3k\3k\3l\3l\3l\3m\3m\3m\3m\3n" + + "\3n\3n\3n\3n\3n\3o\3o\7o\u03b9\no\fo\16o\u03bc\13o\3p\3p\3q\3q\3r\3r\3" + + "r\3r\3s\3s\3s\3s\5s\u03ca\ns\3t\3t\3u\3u\3u\3v\3v\3w\3w\3w\3x\3x\3y\3" + + "y\3z\3z\3{\3{\3|\3|\3}\3}\3}\3~\3~\3\177\3\177\3\u0080\3\u0080\3\u0080" + + "\3\u0080\7\u0080\u03eb\n\u0080\f\u0080\16\u0080\u03ee\13\u0080\3\u0080" + + "\3\u0080\3\u0081\6\u0081\u03f3\n\u0081\r\u0081\16\u0081\u03f4\3\u0082" + + "\6\u0082\u03f8\n\u0082\r\u0082\16\u0082\u03f9\3\u0082\3\u0082\7\u0082" + + "\u03fe\n\u0082\f\u0082\16\u0082\u0401\13\u0082\3\u0082\3\u0082\6\u0082" + + "\u0405\n\u0082\r\u0082\16\u0082\u0406\3\u0082\6\u0082\u040a\n\u0082\r" + + "\u0082\16\u0082\u040b\3\u0082\3\u0082\7\u0082\u0410\n\u0082\f\u0082\16" + + "\u0082\u0413\13\u0082\5\u0082\u0415\n\u0082\3\u0082\3\u0082\3\u0082\3" + + "\u0082\6\u0082\u041b\n\u0082\r\u0082\16\u0082\u041c\3\u0082\3\u0082\5" + + "\u0082\u0421\n\u0082\3\u0083\3\u0083\5\u0083\u0425\n\u0083\3\u0083\3\u0083" + + "\3\u0083\7\u0083\u042a\n\u0083\f\u0083\16\u0083\u042d\13\u0083\3\u0084" + + "\3\u0084\3\u0084\3\u0084\6\u0084\u0433\n\u0084\r\u0084\16\u0084\u0434" + + "\3\u0085\3\u0085\3\u0085\6\u0085\u043a\n\u0085\r\u0085\16\u0085\u043b" + + "\3\u0086\3\u0086\3\u0086\3\u0086\7\u0086\u0442\n\u0086\f\u0086\16\u0086" + + "\u0445\13\u0086\3\u0086\3\u0086\3\u0087\3\u0087\3\u0087\3\u0087\7\u0087" + + "\u044d\n\u0087\f\u0087\16\u0087\u0450\13\u0087\3\u0087\3\u0087\3\u0088" + + "\3\u0088\5\u0088\u0456\n\u0088\3\u0088\6\u0088\u0459\n\u0088\r\u0088\16" + + "\u0088\u045a\3\u0089\3\u0089\3\u008a\3\u008a\3\u008b\3\u008b\3\u008b\3" + + "\u008b\7\u008b\u0465\n\u008b\f\u008b\16\u008b\u0468\13\u008b\3\u008b\5" + + "\u008b\u046b\n\u008b\3\u008b\5\u008b\u046e\n\u008b\3\u008b\3\u008b\3\u008c" + + "\3\u008c\3\u008c\3\u008c\3\u008c\7\u008c\u0477\n\u008c\f\u008c\16\u008c" + + "\u047a\13\u008c\3\u008c\3\u008c\3\u008c\3\u008c\3\u008c\3\u008d\6\u008d" + + "\u0482\n\u008d\r\u008d\16\u008d\u0483\3\u008d\3\u008d\3\u008e\3\u008e" + + "\3\u0478\2\u008f\3\3\5\4\7\5\t\6\13\7\r\b\17\t\21\n\23\13\25\f\27\r\31" + + "\16\33\17\35\20\37\21!\22#\23%\24\'\25)\26+\27-\30/\31\61\32\63\33\65" + + "\34\67\359\36;\37= ?!A\"C#E$G%I&K\'M(O)Q*S+U,W-Y.[/]\60_\61a\62c\63e\64" + + "g\65i\66k\67m8o9q:s;u{?}@\177A\u0081B\u0083C\u0085D\u0087E\u0089" + + "F\u008bG\u008dH\u008fI\u0091J\u0093K\u0095L\u0097M\u0099N\u009bO\u009d" + + "P\u009fQ\u00a1R\u00a3S\u00a5T\u00a7U\u00a9V\u00abW\u00adX\u00afY\u00b1" + + "Z\u00b3[\u00b5\\\u00b7]\u00b9^\u00bb_\u00bd`\u00bfa\u00c1b\u00c3c\u00c5" + + "d\u00c7e\u00c9f\u00cbg\u00cdh\u00cfi\u00d1j\u00d3k\u00d5l\u00d7m\u00d9" + + "n\u00dbo\u00ddp\u00dfq\u00e1r\u00e3s\u00e5t\u00e7u\u00e9v\u00ebw\u00ed" + + "x\u00efy\u00f1z\u00f3{\u00f5|\u00f7}\u00f9~\u00fb\177\u00fd\u0080\u00ff" + + "\u0081\u0101\u0082\u0103\u0083\u0105\u0084\u0107\u0085\u0109\u0086\u010b" + + "\u0087\u010d\u0088\u010f\2\u0111\2\u0113\2\u0115\u0089\u0117\u008a\u0119" + + "\u008b\u011b\u008c\3\2\13\3\2))\4\2BBaa\3\2$$\3\2bb\4\2--//\3\2\62;\3" + + "\2C\\\4\2\f\f\17\17\5\2\13\f\17\17\"\"\2\u04aa\2\3\3\2\2\2\2\5\3\2\2\2" + + "\2\7\3\2\2\2\2\t\3\2\2\2\2\13\3\2\2\2\2\r\3\2\2\2\2\17\3\2\2\2\2\21\3" + + "\2\2\2\2\23\3\2\2\2\2\25\3\2\2\2\2\27\3\2\2\2\2\31\3\2\2\2\2\33\3\2\2" + + "\2\2\35\3\2\2\2\2\37\3\2\2\2\2!\3\2\2\2\2#\3\2\2\2\2%\3\2\2\2\2\'\3\2" + + "\2\2\2)\3\2\2\2\2+\3\2\2\2\2-\3\2\2\2\2/\3\2\2\2\2\61\3\2\2\2\2\63\3\2" + + "\2\2\2\65\3\2\2\2\2\67\3\2\2\2\29\3\2\2\2\2;\3\2\2\2\2=\3\2\2\2\2?\3\2" + + "\2\2\2A\3\2\2\2\2C\3\2\2\2\2E\3\2\2\2\2G\3\2\2\2\2I\3\2\2\2\2K\3\2\2\2" + + "\2M\3\2\2\2\2O\3\2\2\2\2Q\3\2\2\2\2S\3\2\2\2\2U\3\2\2\2\2W\3\2\2\2\2Y" + + "\3\2\2\2\2[\3\2\2\2\2]\3\2\2\2\2_\3\2\2\2\2a\3\2\2\2\2c\3\2\2\2\2e\3\2" + + "\2\2\2g\3\2\2\2\2i\3\2\2\2\2k\3\2\2\2\2m\3\2\2\2\2o\3\2\2\2\2q\3\2\2\2" + + "\2s\3\2\2\2\2u\3\2\2\2\2w\3\2\2\2\2y\3\2\2\2\2{\3\2\2\2\2}\3\2\2\2\2\177" + + "\3\2\2\2\2\u0081\3\2\2\2\2\u0083\3\2\2\2\2\u0085\3\2\2\2\2\u0087\3\2\2" + + "\2\2\u0089\3\2\2\2\2\u008b\3\2\2\2\2\u008d\3\2\2\2\2\u008f\3\2\2\2\2\u0091" + + "\3\2\2\2\2\u0093\3\2\2\2\2\u0095\3\2\2\2\2\u0097\3\2\2\2\2\u0099\3\2\2" + + "\2\2\u009b\3\2\2\2\2\u009d\3\2\2\2\2\u009f\3\2\2\2\2\u00a1\3\2\2\2\2\u00a3" + + "\3\2\2\2\2\u00a5\3\2\2\2\2\u00a7\3\2\2\2\2\u00a9\3\2\2\2\2\u00ab\3\2\2" + + "\2\2\u00ad\3\2\2\2\2\u00af\3\2\2\2\2\u00b1\3\2\2\2\2\u00b3\3\2\2\2\2\u00b5" + + "\3\2\2\2\2\u00b7\3\2\2\2\2\u00b9\3\2\2\2\2\u00bb\3\2\2\2\2\u00bd\3\2\2" + + "\2\2\u00bf\3\2\2\2\2\u00c1\3\2\2\2\2\u00c3\3\2\2\2\2\u00c5\3\2\2\2\2\u00c7" + + "\3\2\2\2\2\u00c9\3\2\2\2\2\u00cb\3\2\2\2\2\u00cd\3\2\2\2\2\u00cf\3\2\2" + + "\2\2\u00d1\3\2\2\2\2\u00d3\3\2\2\2\2\u00d5\3\2\2\2\2\u00d7\3\2\2\2\2\u00d9" + + "\3\2\2\2\2\u00db\3\2\2\2\2\u00dd\3\2\2\2\2\u00df\3\2\2\2\2\u00e1\3\2\2" + + "\2\2\u00e3\3\2\2\2\2\u00e5\3\2\2\2\2\u00e7\3\2\2\2\2\u00e9\3\2\2\2\2\u00eb" + + "\3\2\2\2\2\u00ed\3\2\2\2\2\u00ef\3\2\2\2\2\u00f1\3\2\2\2\2\u00f3\3\2\2" + + "\2\2\u00f5\3\2\2\2\2\u00f7\3\2\2\2\2\u00f9\3\2\2\2\2\u00fb\3\2\2\2\2\u00fd" + + "\3\2\2\2\2\u00ff\3\2\2\2\2\u0101\3\2\2\2\2\u0103\3\2\2\2\2\u0105\3\2\2" + + "\2\2\u0107\3\2\2\2\2\u0109\3\2\2\2\2\u010b\3\2\2\2\2\u010d\3\2\2\2\2\u0115" + + "\3\2\2\2\2\u0117\3\2\2\2\2\u0119\3\2\2\2\2\u011b\3\2\2\2\3\u011d\3\2\2" + + "\2\5\u011f\3\2\2\2\7\u0121\3\2\2\2\t\u0123\3\2\2\2\13\u0125\3\2\2\2\r" + + "\u0129\3\2\2\2\17\u0131\3\2\2\2\21\u013a\3\2\2\2\23\u013e\3\2\2\2\25\u0142" + + "\3\2\2\2\27\u0145\3\2\2\2\31\u0149\3\2\2\2\33\u0151\3\2\2\2\35\u0154\3" + + "\2\2\2\37\u0159\3\2\2\2!\u015e\3\2\2\2#\u0166\3\2\2\2%\u016f\3\2\2\2\'" + + "\u0177\3\2\2\2)\u017f\3\2\2\2+\u018c\3\2\2\2-\u0199\3\2\2\2/\u01ab\3\2" + + "\2\2\61\u01af\3\2\2\2\63\u01b4\3\2\2\2\65\u01ba\3\2\2\2\67\u01bf\3\2\2" + + "\29\u01c8\3\2\2\2;\u01d1\3\2\2\2=\u01d6\3\2\2\2?\u01da\3\2\2\2A\u01e1" + + "\3\2\2\2C\u01ec\3\2\2\2E\u01f3\3\2\2\2G\u01fb\3\2\2\2I\u0203\3\2\2\2K" + + "\u0209\3\2\2\2M\u020f\3\2\2\2O\u0213\3\2\2\2Q\u021a\3\2\2\2S\u021f\3\2" + + "\2\2U\u0226\3\2\2\2W\u022b\3\2\2\2Y\u0235\3\2\2\2[\u023e\3\2\2\2]\u0244" + + "\3\2\2\2_\u024b\3\2\2\2a\u0250\3\2\2\2c\u0256\3\2\2\2e\u0259\3\2\2\2g" + + "\u0261\3\2\2\2i\u0267\3\2\2\2k\u0270\3\2\2\2m\u0273\3\2\2\2o\u0278\3\2" + + "\2\2q\u027d\3\2\2\2s\u0282\3\2\2\2u\u0287\3\2\2\2w\u028d\3\2\2\2y\u0294" + + "\3\2\2\2{\u029a\3\2\2\2}\u02a1\3\2\2\2\177\u02a9\3\2\2\2\u0081\u02af\3" + + "\2\2\2\u0083\u02b6\3\2\2\2\u0085\u02be\3\2\2\2\u0087\u02c2\3\2\2\2\u0089" + + "\u02c7\3\2\2\2\u008b\u02cd\3\2\2\2\u008d\u02d0\3\2\2\2\u008f\u02da\3\2" + + "\2\2\u0091\u02dd\3\2\2\2\u0093\u02e3\3\2\2\2\u0095\u02e9\3\2\2\2\u0097" + + "\u02f0\3\2\2\2\u0099\u02f9\3\2\2\2\u009b\u02ff\3\2\2\2\u009d\u0304\3\2" + + "\2\2\u009f\u030a\3\2\2\2\u00a1\u0310\3\2\2\2\u00a3\u0316\3\2\2\2\u00a5" + + "\u031e\3\2\2\2\u00a7\u0325\3\2\2\2\u00a9\u032d\3\2\2\2\u00ab\u0334\3\2" + + "\2\2\u00ad\u0339\3\2\2\2\u00af\u033d\3\2\2\2\u00b1\u0343\3\2\2\2\u00b3" + + "\u034a\3\2\2\2\u00b5\u034f\3\2\2\2\u00b7\u0354\3\2\2\2\u00b9\u0359\3\2" + + "\2\2\u00bb\u035c\3\2\2\2\u00bd\u0360\3\2\2\2\u00bf\u0365\3\2\2\2\u00c1" + + "\u036b\3\2\2\2\u00c3\u0371\3\2\2\2\u00c5\u0378\3\2\2\2\u00c7\u037d\3\2" + + "\2\2\u00c9\u0383\3\2\2\2\u00cb\u0388\3\2\2\2\u00cd\u038d\3\2\2\2\u00cf" + + "\u0393\3\2\2\2\u00d1\u039b\3\2\2\2\u00d3\u039f\3\2\2\2\u00d5\u03a6\3\2" + + "\2\2\u00d7\u03a9\3\2\2\2\u00d9\u03ac\3\2\2\2\u00db\u03b0\3\2\2\2\u00dd" + + "\u03b6\3\2\2\2\u00df\u03bd\3\2\2\2\u00e1\u03bf\3\2\2\2\u00e3\u03c1\3\2" + + "\2\2\u00e5\u03c9\3\2\2\2\u00e7\u03cb\3\2\2\2\u00e9\u03cd\3\2\2\2\u00eb" + + "\u03d0\3\2\2\2\u00ed\u03d2\3\2\2\2\u00ef\u03d5\3\2\2\2\u00f1\u03d7\3\2" + + "\2\2\u00f3\u03d9\3\2\2\2\u00f5\u03db\3\2\2\2\u00f7\u03dd\3\2\2\2\u00f9" + + "\u03df\3\2\2\2\u00fb\u03e2\3\2\2\2\u00fd\u03e4\3\2\2\2\u00ff\u03e6\3\2" + + "\2\2\u0101\u03f2\3\2\2\2\u0103\u0420\3\2\2\2\u0105\u0424\3\2\2\2\u0107" + + "\u042e\3\2\2\2\u0109\u0439\3\2\2\2\u010b\u043d\3\2\2\2\u010d\u0448\3\2" + + "\2\2\u010f\u0453\3\2\2\2\u0111\u045c\3\2\2\2\u0113\u045e\3\2\2\2\u0115" + + "\u0460\3\2\2\2\u0117\u0471\3\2\2\2\u0119\u0481\3\2\2\2\u011b\u0487\3\2" + + "\2\2\u011d\u011e\7*\2\2\u011e\4\3\2\2\2\u011f\u0120\7+\2\2\u0120\6\3\2" + + "\2\2\u0121\u0122\7.\2\2\u0122\b\3\2\2\2\u0123\u0124\7<\2\2\u0124\n\3\2" + + "\2\2\u0125\u0126\7C\2\2\u0126\u0127\7N\2\2\u0127\u0128\7N\2\2\u0128\f" + + "\3\2\2\2\u0129\u012a\7C\2\2\u012a\u012b\7P\2\2\u012b\u012c\7C\2\2\u012c" + + "\u012d\7N\2\2\u012d\u012e\7[\2\2\u012e\u012f\7\\\2\2\u012f\u0130\7G\2" + + "\2\u0130\16\3\2\2\2\u0131\u0132\7C\2\2\u0132\u0133\7P\2\2\u0133\u0134" + + "\7C\2\2\u0134\u0135\7N\2\2\u0135\u0136\7[\2\2\u0136\u0137\7\\\2\2\u0137" + + "\u0138\7G\2\2\u0138\u0139\7F\2\2\u0139\20\3\2\2\2\u013a\u013b\7C\2\2\u013b" + + "\u013c\7P\2\2\u013c\u013d\7F\2\2\u013d\22\3\2\2\2\u013e\u013f\7C\2\2\u013f" + + "\u0140\7P\2\2\u0140\u0141\7[\2\2\u0141\24\3\2\2\2\u0142\u0143\7C\2\2\u0143" + + "\u0144\7U\2\2\u0144\26\3\2\2\2\u0145\u0146\7C\2\2\u0146\u0147\7U\2\2\u0147" + + "\u0148\7E\2\2\u0148\30\3\2\2\2\u0149\u014a\7D\2\2\u014a\u014b\7G\2\2\u014b" + + "\u014c\7V\2\2\u014c\u014d\7Y\2\2\u014d\u014e\7G\2\2\u014e\u014f\7G\2\2" + + "\u014f\u0150\7P\2\2\u0150\32\3\2\2\2\u0151\u0152\7D\2\2\u0152\u0153\7" + + "[\2\2\u0153\34\3\2\2\2\u0154\u0155\7E\2\2\u0155\u0156\7C\2\2\u0156\u0157" + + "\7U\2\2\u0157\u0158\7G\2\2\u0158\36\3\2\2\2\u0159\u015a\7E\2\2\u015a\u015b" + + "\7C\2\2\u015b\u015c\7U\2\2\u015c\u015d\7V\2\2\u015d \3\2\2\2\u015e\u015f" + + "\7E\2\2\u015f\u0160\7C\2\2\u0160\u0161\7V\2\2\u0161\u0162\7C\2\2\u0162" + + "\u0163\7N\2\2\u0163\u0164\7Q\2\2\u0164\u0165\7I\2\2\u0165\"\3\2\2\2\u0166" + + "\u0167\7E\2\2\u0167\u0168\7C\2\2\u0168\u0169\7V\2\2\u0169\u016a\7C\2\2" + + "\u016a\u016b\7N\2\2\u016b\u016c\7Q\2\2\u016c\u016d\7I\2\2\u016d\u016e" + + "\7U\2\2\u016e$\3\2\2\2\u016f\u0170\7E\2\2\u0170\u0171\7Q\2\2\u0171\u0172" + + "\7N\2\2\u0172\u0173\7W\2\2\u0173\u0174\7O\2\2\u0174\u0175\7P\2\2\u0175" + + "\u0176\7U\2\2\u0176&\3\2\2\2\u0177\u0178\7E\2\2\u0178\u0179\7Q\2\2\u0179" + + "\u017a\7P\2\2\u017a\u017b\7X\2\2\u017b\u017c\7G\2\2\u017c\u017d\7T\2\2" + + "\u017d\u017e\7V\2\2\u017e(\3\2\2\2\u017f\u0180\7E\2\2\u0180\u0181\7W\2" + + "\2\u0181\u0182\7T\2\2\u0182\u0183\7T\2\2\u0183\u0184\7G\2\2\u0184\u0185" + + "\7P\2\2\u0185\u0186\7V\2\2\u0186\u0187\7a\2\2\u0187\u0188\7F\2\2\u0188" + + "\u0189\7C\2\2\u0189\u018a\7V\2\2\u018a\u018b\7G\2\2\u018b*\3\2\2\2\u018c" + + "\u018d\7E\2\2\u018d\u018e\7W\2\2\u018e\u018f\7T\2\2\u018f\u0190\7T\2\2" + + "\u0190\u0191\7G\2\2\u0191\u0192\7P\2\2\u0192\u0193\7V\2\2\u0193\u0194" + + "\7a\2\2\u0194\u0195\7V\2\2\u0195\u0196\7K\2\2\u0196\u0197\7O\2\2\u0197" + + "\u0198\7G\2\2\u0198,\3\2\2\2\u0199\u019a\7E\2\2\u019a\u019b\7W\2\2\u019b" + + "\u019c\7T\2\2\u019c\u019d\7T\2\2\u019d\u019e\7G\2\2\u019e\u019f\7P\2\2" + + "\u019f\u01a0\7V\2\2\u01a0\u01a1\7a\2\2\u01a1\u01a2\7V\2\2\u01a2\u01a3" + + "\7K\2\2\u01a3\u01a4\7O\2\2\u01a4\u01a5\7G\2\2\u01a5\u01a6\7U\2\2\u01a6" + + "\u01a7\7V\2\2\u01a7\u01a8\7C\2\2\u01a8\u01a9\7O\2\2\u01a9\u01aa\7R\2\2" + + "\u01aa.\3\2\2\2\u01ab\u01ac\7F\2\2\u01ac\u01ad\7C\2\2\u01ad\u01ae\7[\2" + + "\2\u01ae\60\3\2\2\2\u01af\u01b0\7F\2\2\u01b0\u01b1\7C\2\2\u01b1\u01b2" + + "\7[\2\2\u01b2\u01b3\7U\2\2\u01b3\62\3\2\2\2\u01b4\u01b5\7F\2\2\u01b5\u01b6" + + "\7G\2\2\u01b6\u01b7\7D\2\2\u01b7\u01b8\7W\2\2\u01b8\u01b9\7I\2\2\u01b9" + + "\64\3\2\2\2\u01ba\u01bb\7F\2\2\u01bb\u01bc\7G\2\2\u01bc\u01bd\7U\2\2\u01bd" + + "\u01be\7E\2\2\u01be\66\3\2\2\2\u01bf\u01c0\7F\2\2\u01c0\u01c1\7G\2\2\u01c1" + + "\u01c2\7U\2\2\u01c2\u01c3\7E\2\2\u01c3\u01c4\7T\2\2\u01c4\u01c5\7K\2\2" + + "\u01c5\u01c6\7D\2\2\u01c6\u01c7\7G\2\2\u01c78\3\2\2\2\u01c8\u01c9\7F\2" + + "\2\u01c9\u01ca\7K\2\2\u01ca\u01cb\7U\2\2\u01cb\u01cc\7V\2\2\u01cc\u01cd" + + "\7K\2\2\u01cd\u01ce\7P\2\2\u01ce\u01cf\7E\2\2\u01cf\u01d0\7V\2\2\u01d0" + + ":\3\2\2\2\u01d1\u01d2\7G\2\2\u01d2\u01d3\7N\2\2\u01d3\u01d4\7U\2\2\u01d4" + + "\u01d5\7G\2\2\u01d5<\3\2\2\2\u01d6\u01d7\7G\2\2\u01d7\u01d8\7P\2\2\u01d8" + + "\u01d9\7F\2\2\u01d9>\3\2\2\2\u01da\u01db\7G\2\2\u01db\u01dc\7U\2\2\u01dc" + + "\u01dd\7E\2\2\u01dd\u01de\7C\2\2\u01de\u01df\7R\2\2\u01df\u01e0\7G\2\2" + + "\u01e0@\3\2\2\2\u01e1\u01e2\7G\2\2\u01e2\u01e3\7Z\2\2\u01e3\u01e4\7G\2" + + "\2\u01e4\u01e5\7E\2\2\u01e5\u01e6\7W\2\2\u01e6\u01e7\7V\2\2\u01e7\u01e8" + + "\7C\2\2\u01e8\u01e9\7D\2\2\u01e9\u01ea\7N\2\2\u01ea\u01eb\7G\2\2\u01eb" + + "B\3\2\2\2\u01ec\u01ed\7G\2\2\u01ed\u01ee\7Z\2\2\u01ee\u01ef\7K\2\2\u01ef" + + "\u01f0\7U\2\2\u01f0\u01f1\7V\2\2\u01f1\u01f2\7U\2\2\u01f2D\3\2\2\2\u01f3" + + "\u01f4\7G\2\2\u01f4\u01f5\7Z\2\2\u01f5\u01f6\7R\2\2\u01f6\u01f7\7N\2\2" + + "\u01f7\u01f8\7C\2\2\u01f8\u01f9\7K\2\2\u01f9\u01fa\7P\2\2\u01faF\3\2\2" + + "\2\u01fb\u01fc\7G\2\2\u01fc\u01fd\7Z\2\2\u01fd\u01fe\7V\2\2\u01fe\u01ff" + + "\7T\2\2\u01ff\u0200\7C\2\2\u0200\u0201\7E\2\2\u0201\u0202\7V\2\2\u0202" + + "H\3\2\2\2\u0203\u0204\7H\2\2\u0204\u0205\7C\2\2\u0205\u0206\7N\2\2\u0206" + + "\u0207\7U\2\2\u0207\u0208\7G\2\2\u0208J\3\2\2\2\u0209\u020a\7H\2\2\u020a" + + "\u020b\7K\2\2\u020b\u020c\7T\2\2\u020c\u020d\7U\2\2\u020d\u020e\7V\2\2" + + "\u020eL\3\2\2\2\u020f\u0210\7H\2\2\u0210\u0211\7Q\2\2\u0211\u0212\7T\2" + + "\2\u0212N\3\2\2\2\u0213\u0214\7H\2\2\u0214\u0215\7Q\2\2\u0215\u0216\7" + + "T\2\2\u0216\u0217\7O\2\2\u0217\u0218\7C\2\2\u0218\u0219\7V\2\2\u0219P" + + "\3\2\2\2\u021a\u021b\7H\2\2\u021b\u021c\7T\2\2\u021c\u021d\7Q\2\2\u021d" + + "\u021e\7O\2\2\u021eR\3\2\2\2\u021f\u0220\7H\2\2\u0220\u0221\7T\2\2\u0221" + + "\u0222\7Q\2\2\u0222\u0223\7\\\2\2\u0223\u0224\7G\2\2\u0224\u0225\7P\2" + + "\2\u0225T\3\2\2\2\u0226\u0227\7H\2\2\u0227\u0228\7W\2\2\u0228\u0229\7" + + "N\2\2\u0229\u022a\7N\2\2\u022aV\3\2\2\2\u022b\u022c\7H\2\2\u022c\u022d" + + "\7W\2\2\u022d\u022e\7P\2\2\u022e\u022f\7E\2\2\u022f\u0230\7V\2\2\u0230" + + "\u0231\7K\2\2\u0231\u0232\7Q\2\2\u0232\u0233\7P\2\2\u0233\u0234\7U\2\2" + + "\u0234X\3\2\2\2\u0235\u0236\7I\2\2\u0236\u0237\7T\2\2\u0237\u0238\7C\2" + + "\2\u0238\u0239\7R\2\2\u0239\u023a\7J\2\2\u023a\u023b\7X\2\2\u023b\u023c" + + "\7K\2\2\u023c\u023d\7\\\2\2\u023dZ\3\2\2\2\u023e\u023f\7I\2\2\u023f\u0240" + + "\7T\2\2\u0240\u0241\7Q\2\2\u0241\u0242\7W\2\2\u0242\u0243\7R\2\2\u0243" + + "\\\3\2\2\2\u0244\u0245\7J\2\2\u0245\u0246\7C\2\2\u0246\u0247\7X\2\2\u0247" + + "\u0248\7K\2\2\u0248\u0249\7P\2\2\u0249\u024a\7I\2\2\u024a^\3\2\2\2\u024b" + + "\u024c\7J\2\2\u024c\u024d\7Q\2\2\u024d\u024e\7W\2\2\u024e\u024f\7T\2\2" + + "\u024f`\3\2\2\2\u0250\u0251\7J\2\2\u0251\u0252\7Q\2\2\u0252\u0253\7W\2" + + "\2\u0253\u0254\7T\2\2\u0254\u0255\7U\2\2\u0255b\3\2\2\2\u0256\u0257\7" + + "K\2\2\u0257\u0258\7P\2\2\u0258d\3\2\2\2\u0259\u025a\7K\2\2\u025a\u025b" + + "\7P\2\2\u025b\u025c\7E\2\2\u025c\u025d\7N\2\2\u025d\u025e\7W\2\2\u025e" + + "\u025f\7F\2\2\u025f\u0260\7G\2\2\u0260f\3\2\2\2\u0261\u0262\7K\2\2\u0262" + + "\u0263\7P\2\2\u0263\u0264\7P\2\2\u0264\u0265\7G\2\2\u0265\u0266\7T\2\2" + + "\u0266h\3\2\2\2\u0267\u0268\7K\2\2\u0268\u0269\7P\2\2\u0269\u026a\7V\2" + + "\2\u026a\u026b\7G\2\2\u026b\u026c\7T\2\2\u026c\u026d\7X\2\2\u026d\u026e" + + "\7C\2\2\u026e\u026f\7N\2\2\u026fj\3\2\2\2\u0270\u0271\7K\2\2\u0271\u0272" + + "\7U\2\2\u0272l\3\2\2\2\u0273\u0274\7L\2\2\u0274\u0275\7Q\2\2\u0275\u0276" + + "\7K\2\2\u0276\u0277\7P\2\2\u0277n\3\2\2\2\u0278\u0279\7N\2\2\u0279\u027a" + + "\7C\2\2\u027a\u027b\7U\2\2\u027b\u027c\7V\2\2\u027cp\3\2\2\2\u027d\u027e" + + "\7N\2\2\u027e\u027f\7G\2\2\u027f\u0280\7H\2\2\u0280\u0281\7V\2\2\u0281" + + "r\3\2\2\2\u0282\u0283\7N\2\2\u0283\u0284\7K\2\2\u0284\u0285\7M\2\2\u0285" + + "\u0286\7G\2\2\u0286t\3\2\2\2\u0287\u0288\7N\2\2\u0288\u0289\7K\2\2\u0289" + + "\u028a\7O\2\2\u028a\u028b\7K\2\2\u028b\u028c\7V\2\2\u028cv\3\2\2\2\u028d" + + "\u028e\7O\2\2\u028e\u028f\7C\2\2\u028f\u0290\7R\2\2\u0290\u0291\7R\2\2" + + "\u0291\u0292\7G\2\2\u0292\u0293\7F\2\2\u0293x\3\2\2\2\u0294\u0295\7O\2" + + "\2\u0295\u0296\7C\2\2\u0296\u0297\7V\2\2\u0297\u0298\7E\2\2\u0298\u0299" + + "\7J\2\2\u0299z\3\2\2\2\u029a\u029b\7O\2\2\u029b\u029c\7K\2\2\u029c\u029d" + + "\7P\2\2\u029d\u029e\7W\2\2\u029e\u029f\7V\2\2\u029f\u02a0\7G\2\2\u02a0" + + "|\3\2\2\2\u02a1\u02a2\7O\2\2\u02a2\u02a3\7K\2\2\u02a3\u02a4\7P\2\2\u02a4" + + "\u02a5\7W\2\2\u02a5\u02a6\7V\2\2\u02a6\u02a7\7G\2\2\u02a7\u02a8\7U\2\2" + + "\u02a8~\3\2\2\2\u02a9\u02aa\7O\2\2\u02aa\u02ab\7Q\2\2\u02ab\u02ac\7P\2" + + "\2\u02ac\u02ad\7V\2\2\u02ad\u02ae\7J\2\2\u02ae\u0080\3\2\2\2\u02af\u02b0" + + "\7O\2\2\u02b0\u02b1\7Q\2\2\u02b1\u02b2\7P\2\2\u02b2\u02b3\7V\2\2\u02b3" + + "\u02b4\7J\2\2\u02b4\u02b5\7U\2\2\u02b5\u0082\3\2\2\2\u02b6\u02b7\7P\2" + + "\2\u02b7\u02b8\7C\2\2\u02b8\u02b9\7V\2\2\u02b9\u02ba\7W\2\2\u02ba\u02bb" + + "\7T\2\2\u02bb\u02bc\7C\2\2\u02bc\u02bd\7N\2\2\u02bd\u0084\3\2\2\2\u02be" + + "\u02bf\7P\2\2\u02bf\u02c0\7Q\2\2\u02c0\u02c1\7V\2\2\u02c1\u0086\3\2\2" + + "\2\u02c2\u02c3\7P\2\2\u02c3\u02c4\7W\2\2\u02c4\u02c5\7N\2\2\u02c5\u02c6" + + "\7N\2\2\u02c6\u0088\3\2\2\2\u02c7\u02c8\7P\2\2\u02c8\u02c9\7W\2\2\u02c9" + + "\u02ca\7N\2\2\u02ca\u02cb\7N\2\2\u02cb\u02cc\7U\2\2\u02cc\u008a\3\2\2" + + "\2\u02cd\u02ce\7Q\2\2\u02ce\u02cf\7P\2\2\u02cf\u008c\3\2\2\2\u02d0\u02d1" + + "\7Q\2\2\u02d1\u02d2\7R\2\2\u02d2\u02d3\7V\2\2\u02d3\u02d4\7K\2\2\u02d4" + + "\u02d5\7O\2\2\u02d5\u02d6\7K\2\2\u02d6\u02d7\7\\\2\2\u02d7\u02d8\7G\2" + + "\2\u02d8\u02d9\7F\2\2\u02d9\u008e\3\2\2\2\u02da\u02db\7Q\2\2\u02db\u02dc" + + "\7T\2\2\u02dc\u0090\3\2\2\2\u02dd\u02de\7Q\2\2\u02de\u02df\7T\2\2\u02df" + + "\u02e0\7F\2\2\u02e0\u02e1\7G\2\2\u02e1\u02e2\7T\2\2\u02e2\u0092\3\2\2" + + "\2\u02e3\u02e4\7Q\2\2\u02e4\u02e5\7W\2\2\u02e5\u02e6\7V\2\2\u02e6\u02e7" + + "\7G\2\2\u02e7\u02e8\7T\2\2\u02e8\u0094\3\2\2\2\u02e9\u02ea\7R\2\2\u02ea" + + "\u02eb\7C\2\2\u02eb\u02ec\7T\2\2\u02ec\u02ed\7U\2\2\u02ed\u02ee\7G\2\2" + + "\u02ee\u02ef\7F\2\2\u02ef\u0096\3\2\2\2\u02f0\u02f1\7R\2\2\u02f1\u02f2" + + "\7J\2\2\u02f2\u02f3\7[\2\2\u02f3\u02f4\7U\2\2\u02f4\u02f5\7K\2\2\u02f5" + + "\u02f6\7E\2\2\u02f6\u02f7\7C\2\2\u02f7\u02f8\7N\2\2\u02f8\u0098\3\2\2" + + "\2\u02f9\u02fa\7R\2\2\u02fa\u02fb\7K\2\2\u02fb\u02fc\7X\2\2\u02fc\u02fd" + + "\7Q\2\2\u02fd\u02fe\7V\2\2\u02fe\u009a\3\2\2\2\u02ff\u0300\7R\2\2\u0300" + + "\u0301\7N\2\2\u0301\u0302\7C\2\2\u0302\u0303\7P\2\2\u0303\u009c\3\2\2" + + "\2\u0304\u0305\7T\2\2\u0305\u0306\7K\2\2\u0306\u0307\7I\2\2\u0307\u0308" + + "\7J\2\2\u0308\u0309\7V\2\2\u0309\u009e\3\2\2\2\u030a\u030b\7T\2\2\u030b" + + "\u030c\7N\2\2\u030c\u030d\7K\2\2\u030d\u030e\7M\2\2\u030e\u030f\7G\2\2" + + "\u030f\u00a0\3\2\2\2\u0310\u0311\7S\2\2\u0311\u0312\7W\2\2\u0312\u0313" + + "\7G\2\2\u0313\u0314\7T\2\2\u0314\u0315\7[\2\2\u0315\u00a2\3\2\2\2\u0316" + + "\u0317\7U\2\2\u0317\u0318\7E\2\2\u0318\u0319\7J\2\2\u0319\u031a\7G\2\2" + + "\u031a\u031b\7O\2\2\u031b\u031c\7C\2\2\u031c\u031d\7U\2\2\u031d\u00a4" + + "\3\2\2\2\u031e\u031f\7U\2\2\u031f\u0320\7G\2\2\u0320\u0321\7E\2\2\u0321" + + "\u0322\7Q\2\2\u0322\u0323\7P\2\2\u0323\u0324\7F\2\2\u0324\u00a6\3\2\2" + + "\2\u0325\u0326\7U\2\2\u0326\u0327\7G\2\2\u0327\u0328\7E\2\2\u0328\u0329" + + "\7Q\2\2\u0329\u032a\7P\2\2\u032a\u032b\7F\2\2\u032b\u032c\7U\2\2\u032c" + + "\u00a8\3\2\2\2\u032d\u032e\7U\2\2\u032e\u032f\7G\2\2\u032f\u0330\7N\2" + + "\2\u0330\u0331\7G\2\2\u0331\u0332\7E\2\2\u0332\u0333\7V\2\2\u0333\u00aa" + + "\3\2\2\2\u0334\u0335\7U\2\2\u0335\u0336\7J\2\2\u0336\u0337\7Q\2\2\u0337" + + "\u0338\7Y\2\2\u0338\u00ac\3\2\2\2\u0339\u033a\7U\2\2\u033a\u033b\7[\2" + + "\2\u033b\u033c\7U\2\2\u033c\u00ae\3\2\2\2\u033d\u033e\7V\2\2\u033e\u033f" + + "\7C\2\2\u033f\u0340\7D\2\2\u0340\u0341\7N\2\2\u0341\u0342\7G\2\2\u0342" + + "\u00b0\3\2\2\2\u0343\u0344\7V\2\2\u0344\u0345\7C\2\2\u0345\u0346\7D\2" + + "\2\u0346\u0347\7N\2\2\u0347\u0348\7G\2\2\u0348\u0349\7U\2\2\u0349\u00b2" + + "\3\2\2\2\u034a\u034b\7V\2\2\u034b\u034c\7G\2\2\u034c\u034d\7Z\2\2\u034d" + + "\u034e\7V\2\2\u034e\u00b4\3\2\2\2\u034f\u0350\7V\2\2\u0350\u0351\7J\2" + + "\2\u0351\u0352\7G\2\2\u0352\u0353\7P\2\2\u0353\u00b6\3\2\2\2\u0354\u0355" + + "\7V\2\2\u0355\u0356\7T\2\2\u0356\u0357\7W\2\2\u0357\u0358\7G\2\2\u0358" + + "\u00b8\3\2\2\2\u0359\u035a\7V\2\2\u035a\u035b\7Q\2\2\u035b\u00ba\3\2\2" + + "\2\u035c\u035d\7V\2\2\u035d\u035e\7Q\2\2\u035e\u035f\7R\2\2\u035f\u00bc" + + "\3\2\2\2\u0360\u0361\7V\2\2\u0361\u0362\7[\2\2\u0362\u0363\7R\2\2\u0363" + + "\u0364\7G\2\2\u0364\u00be\3\2\2\2\u0365\u0366\7V\2\2\u0366\u0367\7[\2" + + "\2\u0367\u0368\7R\2\2\u0368\u0369\7G\2\2\u0369\u036a\7U\2\2\u036a\u00c0" + + "\3\2\2\2\u036b\u036c\7W\2\2\u036c\u036d\7U\2\2\u036d\u036e\7K\2\2\u036e" + + "\u036f\7P\2\2\u036f\u0370\7I\2\2\u0370\u00c2\3\2\2\2\u0371\u0372\7X\2" + + "\2\u0372\u0373\7G\2\2\u0373\u0374\7T\2\2\u0374\u0375\7K\2\2\u0375\u0376" + + "\7H\2\2\u0376\u0377\7[\2\2\u0377\u00c4\3\2\2\2\u0378\u0379\7Y\2\2\u0379" + + "\u037a\7J\2\2\u037a\u037b\7G\2\2\u037b\u037c\7P\2\2\u037c\u00c6\3\2\2" + + "\2\u037d\u037e\7Y\2\2\u037e\u037f\7J\2\2\u037f\u0380\7G\2\2\u0380\u0381" + + "\7T\2\2\u0381\u0382\7G\2\2\u0382\u00c8\3\2\2\2\u0383\u0384\7Y\2\2\u0384" + + "\u0385\7K\2\2\u0385\u0386\7V\2\2\u0386\u0387\7J\2\2\u0387\u00ca\3\2\2" + + "\2\u0388\u0389\7[\2\2\u0389\u038a\7G\2\2\u038a\u038b\7C\2\2\u038b\u038c" + + "\7T\2\2\u038c\u00cc\3\2\2\2\u038d\u038e\7[\2\2\u038e\u038f\7G\2\2\u038f" + + "\u0390\7C\2\2\u0390\u0391\7T\2\2\u0391\u0392\7U\2\2\u0392\u00ce\3\2\2" + + "\2\u0393\u0394\5\u00ddo\2\u0394\u0395\7G\2\2\u0395\u0396\7U\2\2\u0396" + + "\u0397\7E\2\2\u0397\u0398\7C\2\2\u0398\u0399\7R\2\2\u0399\u039a\7G\2\2" + + "\u039a\u00d0\3\2\2\2\u039b\u039c\5\u00ddo\2\u039c\u039d\7H\2\2\u039d\u039e" + + "\7P\2\2\u039e\u00d2\3\2\2\2\u039f\u03a0\5\u00ddo\2\u03a0\u03a1\7N\2\2" + + "\u03a1\u03a2\7K\2\2\u03a2\u03a3\7O\2\2\u03a3\u03a4\7K\2\2\u03a4\u03a5" + + "\7V\2\2\u03a5\u00d4\3\2\2\2\u03a6\u03a7\5\u00ddo\2\u03a7\u03a8\7F\2\2" + + "\u03a8\u00d6\3\2\2\2\u03a9\u03aa\5\u00ddo\2\u03aa\u03ab\7V\2\2\u03ab\u00d8" + + "\3\2\2\2\u03ac\u03ad\5\u00ddo\2\u03ad\u03ae\7V\2\2\u03ae\u03af\7U\2\2" + + "\u03af\u00da\3\2\2\2\u03b0\u03b1\5\u00ddo\2\u03b1\u03b2\7I\2\2\u03b2\u03b3" + + "\7W\2\2\u03b3\u03b4\7K\2\2\u03b4\u03b5\7F\2\2\u03b5\u00dc\3\2\2\2\u03b6" + + "\u03ba\7}\2\2\u03b7\u03b9\5\u0119\u008d\2\u03b8\u03b7\3\2\2\2\u03b9\u03bc" + + "\3\2\2\2\u03ba\u03b8\3\2\2\2\u03ba\u03bb\3\2\2\2\u03bb\u00de\3\2\2\2\u03bc" + + "\u03ba\3\2\2\2\u03bd\u03be\7\177\2\2\u03be\u00e0\3\2\2\2\u03bf\u03c0\7" + + "?\2\2\u03c0\u00e2\3\2\2\2\u03c1\u03c2\7>\2\2\u03c2\u03c3\7?\2\2\u03c3" + + "\u03c4\7@\2\2\u03c4\u00e4\3\2\2\2\u03c5\u03c6\7>\2\2\u03c6\u03ca\7@\2" + + "\2\u03c7\u03c8\7#\2\2\u03c8\u03ca\7?\2\2\u03c9\u03c5\3\2\2\2\u03c9\u03c7" + + "\3\2\2\2\u03ca\u00e6\3\2\2\2\u03cb\u03cc\7>\2\2\u03cc\u00e8\3\2\2\2\u03cd" + + "\u03ce\7>\2\2\u03ce\u03cf\7?\2\2\u03cf\u00ea\3\2\2\2\u03d0\u03d1\7@\2" + + "\2\u03d1\u00ec\3\2\2\2\u03d2\u03d3\7@\2\2\u03d3\u03d4\7?\2\2\u03d4\u00ee" + + "\3\2\2\2\u03d5\u03d6\7-\2\2\u03d6\u00f0\3\2\2\2\u03d7\u03d8\7/\2\2\u03d8" + + "\u00f2\3\2\2\2\u03d9\u03da\7,\2\2\u03da\u00f4\3\2\2\2\u03db\u03dc\7\61" + + "\2\2\u03dc\u00f6\3\2\2\2\u03dd\u03de\7\'\2\2\u03de\u00f8\3\2\2\2\u03df" + + "\u03e0\7<\2\2\u03e0\u03e1\7<\2\2\u03e1\u00fa\3\2\2\2\u03e2\u03e3\7\60" + + "\2\2\u03e3\u00fc\3\2\2\2\u03e4\u03e5\7A\2\2\u03e5\u00fe\3\2\2\2\u03e6" + + "\u03ec\7)\2\2\u03e7\u03eb\n\2\2\2\u03e8\u03e9\7)\2\2\u03e9\u03eb\7)\2" + + "\2\u03ea\u03e7\3\2\2\2\u03ea\u03e8\3\2\2\2\u03eb\u03ee\3\2\2\2\u03ec\u03ea" + + "\3\2\2\2\u03ec\u03ed\3\2\2\2\u03ed\u03ef\3\2\2\2\u03ee\u03ec\3\2\2\2\u03ef" + + "\u03f0\7)\2\2\u03f0\u0100\3\2\2\2\u03f1\u03f3\5\u0111\u0089\2\u03f2\u03f1" + + "\3\2\2\2\u03f3\u03f4\3\2\2\2\u03f4\u03f2\3\2\2\2\u03f4\u03f5\3\2\2\2\u03f5" + + "\u0102\3\2\2\2\u03f6\u03f8\5\u0111\u0089\2\u03f7\u03f6\3\2\2\2\u03f8\u03f9" + + "\3\2\2\2\u03f9\u03f7\3\2\2\2\u03f9\u03fa\3\2\2\2\u03fa\u03fb\3\2\2\2\u03fb" + + "\u03ff\5\u00fb~\2\u03fc\u03fe\5\u0111\u0089\2\u03fd\u03fc\3\2\2\2\u03fe" + + "\u0401\3\2\2\2\u03ff\u03fd\3\2\2\2\u03ff\u0400\3\2\2\2\u0400\u0421\3\2" + + "\2\2\u0401\u03ff\3\2\2\2\u0402\u0404\5\u00fb~\2\u0403\u0405\5\u0111\u0089" + + "\2\u0404\u0403\3\2\2\2\u0405\u0406\3\2\2\2\u0406\u0404\3\2\2\2\u0406\u0407" + + "\3\2\2\2\u0407\u0421\3\2\2\2\u0408\u040a\5\u0111\u0089\2\u0409\u0408\3" + + "\2\2\2\u040a\u040b\3\2\2\2\u040b\u0409\3\2\2\2\u040b\u040c\3\2\2\2\u040c" + + "\u0414\3\2\2\2\u040d\u0411\5\u00fb~\2\u040e\u0410\5\u0111\u0089\2\u040f" + + "\u040e\3\2\2\2\u0410\u0413\3\2\2\2\u0411\u040f\3\2\2\2\u0411\u0412\3\2" + + "\2\2\u0412\u0415\3\2\2\2\u0413\u0411\3\2\2\2\u0414\u040d\3\2\2\2\u0414" + + "\u0415\3\2\2\2\u0415\u0416\3\2\2\2\u0416\u0417\5\u010f\u0088\2\u0417\u0421" + + "\3\2\2\2\u0418\u041a\5\u00fb~\2\u0419\u041b\5\u0111\u0089\2\u041a\u0419" + + "\3\2\2\2\u041b\u041c\3\2\2\2\u041c\u041a\3\2\2\2\u041c\u041d\3\2\2\2\u041d" + + "\u041e\3\2\2\2\u041e\u041f\5\u010f\u0088\2\u041f\u0421\3\2\2\2\u0420\u03f7" + + "\3\2\2\2\u0420\u0402\3\2\2\2\u0420\u0409\3\2\2\2\u0420\u0418\3\2\2\2\u0421" + + "\u0104\3\2\2\2\u0422\u0425\5\u0113\u008a\2\u0423\u0425\7a\2\2\u0424\u0422" + + "\3\2\2\2\u0424\u0423\3\2\2\2\u0425\u042b\3\2\2\2\u0426\u042a\5\u0113\u008a" + + "\2\u0427\u042a\5\u0111\u0089\2\u0428\u042a\t\3\2\2\u0429\u0426\3\2\2\2" + + "\u0429\u0427\3\2\2\2\u0429\u0428\3\2\2\2\u042a\u042d\3\2\2\2\u042b\u0429" + + "\3\2\2\2\u042b\u042c\3\2\2\2\u042c\u0106\3\2\2\2\u042d\u042b\3\2\2\2\u042e" + + "\u0432\5\u0111\u0089\2\u042f\u0433\5\u0113\u008a\2\u0430\u0433\5\u0111" + + "\u0089\2\u0431\u0433\t\3\2\2\u0432\u042f\3\2\2\2\u0432\u0430\3\2\2\2\u0432" + + "\u0431\3\2\2\2\u0433\u0434\3\2\2\2\u0434\u0432\3\2\2\2\u0434\u0435\3\2" + + "\2\2\u0435\u0108\3\2\2\2\u0436\u043a\5\u0113\u008a\2\u0437\u043a\5\u0111" + + "\u0089\2\u0438\u043a\7a\2\2\u0439\u0436\3\2\2\2\u0439\u0437\3\2\2\2\u0439" + + "\u0438\3\2\2\2\u043a\u043b\3\2\2\2\u043b\u0439\3\2\2\2\u043b\u043c\3\2" + + "\2\2\u043c\u010a\3\2\2\2\u043d\u0443\7$\2\2\u043e\u0442\n\4\2\2\u043f" + + "\u0440\7$\2\2\u0440\u0442\7$\2\2\u0441\u043e\3\2\2\2\u0441\u043f\3\2\2" + + "\2\u0442\u0445\3\2\2\2\u0443\u0441\3\2\2\2\u0443\u0444\3\2\2\2\u0444\u0446" + + "\3\2\2\2\u0445\u0443\3\2\2\2\u0446\u0447\7$\2\2\u0447\u010c\3\2\2\2\u0448" + + "\u044e\7b\2\2\u0449\u044d\n\5\2\2\u044a\u044b\7b\2\2\u044b\u044d\7b\2" + + "\2\u044c\u0449\3\2\2\2\u044c\u044a\3\2\2\2\u044d\u0450\3\2\2\2\u044e\u044c" + + "\3\2\2\2\u044e\u044f\3\2\2\2\u044f\u0451\3\2\2\2\u0450\u044e\3\2\2\2\u0451" + + "\u0452\7b\2\2\u0452\u010e\3\2\2\2\u0453\u0455\7G\2\2\u0454\u0456\t\6\2" + + "\2\u0455\u0454\3\2\2\2\u0455\u0456\3\2\2\2\u0456\u0458\3\2\2\2\u0457\u0459" + + "\5\u0111\u0089\2\u0458\u0457\3\2\2\2\u0459\u045a\3\2\2\2\u045a\u0458\3" + + "\2\2\2\u045a\u045b\3\2\2\2\u045b\u0110\3\2\2\2\u045c\u045d\t\7\2\2\u045d" + + "\u0112\3\2\2\2\u045e\u045f\t\b\2\2\u045f\u0114\3\2\2\2\u0460\u0461\7/" + + "\2\2\u0461\u0462\7/\2\2\u0462\u0466\3\2\2\2\u0463\u0465\n\t\2\2\u0464" + + "\u0463\3\2\2\2\u0465\u0468\3\2\2\2\u0466\u0464\3\2\2\2\u0466\u0467\3\2" + + "\2\2\u0467\u046a\3\2\2\2\u0468\u0466\3\2\2\2\u0469\u046b\7\17\2\2\u046a" + + "\u0469\3\2\2\2\u046a\u046b\3\2\2\2\u046b\u046d\3\2\2\2\u046c\u046e\7\f" + + "\2\2\u046d\u046c\3\2\2\2\u046d\u046e\3\2\2\2\u046e\u046f\3\2\2\2\u046f" + + "\u0470\b\u008b\2\2\u0470\u0116\3\2\2\2\u0471\u0472\7\61\2\2\u0472\u0473" + + "\7,\2\2\u0473\u0478\3\2\2\2\u0474\u0477\5\u0117\u008c\2\u0475\u0477\13" + + "\2\2\2\u0476\u0474\3\2\2\2\u0476\u0475\3\2\2\2\u0477\u047a\3\2\2\2\u0478" + + "\u0479\3\2\2\2\u0478\u0476\3\2\2\2\u0479\u047b\3\2\2\2\u047a\u0478\3\2" + + "\2\2\u047b\u047c\7,\2\2\u047c\u047d\7\61\2\2\u047d\u047e\3\2\2\2\u047e" + + "\u047f\b\u008c\2\2\u047f\u0118\3\2\2\2\u0480\u0482\t\n\2\2\u0481\u0480" + + "\3\2\2\2\u0482\u0483\3\2\2\2\u0483\u0481\3\2\2\2\u0483\u0484\3\2\2\2\u0484" + + "\u0485\3\2\2\2\u0485\u0486\b\u008d\2\2\u0486\u011a\3\2\2\2\u0487\u0488" + + "\13\2\2\2\u0488\u011c\3\2\2\2#\2\u03ba\u03c9\u03ea\u03ec\u03f4\u03f9\u03ff" + + "\u0406\u040b\u0411\u0414\u041c\u0420\u0424\u0429\u042b\u0432\u0434\u0439" + + "\u043b\u0441\u0443\u044c\u044e\u0455\u045a\u0466\u046a\u046d\u0476\u0478" + + "\u0483\3\2\3\2"; + public static final ATN _ATN = new ATNDeserializer().deserialize(_serializedATN.toCharArray()); + static { + _decisionToDFA = new DFA[_ATN.getNumberOfDecisions()]; + for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) { + _decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i); + } } - } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/SqlBaseListener.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/SqlBaseListener.java index d943be197a2c2..9cd5816e72677 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/SqlBaseListener.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/SqlBaseListener.java @@ -1,5 +1,6 @@ // ANTLR GENERATED CODE: DO NOT EDIT package org.elasticsearch.xpack.sql.parser; + import org.antlr.v4.runtime.tree.ParseTreeListener; /** @@ -7,1158 +8,1365 @@ * {@link SqlBaseParser}. */ interface SqlBaseListener extends ParseTreeListener { - /** - * Enter a parse tree produced by {@link SqlBaseParser#singleStatement}. - * @param ctx the parse tree - */ - void enterSingleStatement(SqlBaseParser.SingleStatementContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#singleStatement}. - * @param ctx the parse tree - */ - void exitSingleStatement(SqlBaseParser.SingleStatementContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#singleExpression}. - * @param ctx the parse tree - */ - void enterSingleExpression(SqlBaseParser.SingleExpressionContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#singleExpression}. - * @param ctx the parse tree - */ - void exitSingleExpression(SqlBaseParser.SingleExpressionContext ctx); - /** - * Enter a parse tree produced by the {@code statementDefault} - * labeled alternative in {@link SqlBaseParser#statement}. - * @param ctx the parse tree - */ - void enterStatementDefault(SqlBaseParser.StatementDefaultContext ctx); - /** - * Exit a parse tree produced by the {@code statementDefault} - * labeled alternative in {@link SqlBaseParser#statement}. - * @param ctx the parse tree - */ - void exitStatementDefault(SqlBaseParser.StatementDefaultContext ctx); - /** - * Enter a parse tree produced by the {@code explain} - * labeled alternative in {@link SqlBaseParser#statement}. - * @param ctx the parse tree - */ - void enterExplain(SqlBaseParser.ExplainContext ctx); - /** - * Exit a parse tree produced by the {@code explain} - * labeled alternative in {@link SqlBaseParser#statement}. - * @param ctx the parse tree - */ - void exitExplain(SqlBaseParser.ExplainContext ctx); - /** - * Enter a parse tree produced by the {@code debug} - * labeled alternative in {@link SqlBaseParser#statement}. - * @param ctx the parse tree - */ - void enterDebug(SqlBaseParser.DebugContext ctx); - /** - * Exit a parse tree produced by the {@code debug} - * labeled alternative in {@link SqlBaseParser#statement}. - * @param ctx the parse tree - */ - void exitDebug(SqlBaseParser.DebugContext ctx); - /** - * Enter a parse tree produced by the {@code showTables} - * labeled alternative in {@link SqlBaseParser#statement}. - * @param ctx the parse tree - */ - void enterShowTables(SqlBaseParser.ShowTablesContext ctx); - /** - * Exit a parse tree produced by the {@code showTables} - * labeled alternative in {@link SqlBaseParser#statement}. - * @param ctx the parse tree - */ - void exitShowTables(SqlBaseParser.ShowTablesContext ctx); - /** - * Enter a parse tree produced by the {@code showColumns} - * labeled alternative in {@link SqlBaseParser#statement}. - * @param ctx the parse tree - */ - void enterShowColumns(SqlBaseParser.ShowColumnsContext ctx); - /** - * Exit a parse tree produced by the {@code showColumns} - * labeled alternative in {@link SqlBaseParser#statement}. - * @param ctx the parse tree - */ - void exitShowColumns(SqlBaseParser.ShowColumnsContext ctx); - /** - * Enter a parse tree produced by the {@code showFunctions} - * labeled alternative in {@link SqlBaseParser#statement}. - * @param ctx the parse tree - */ - void enterShowFunctions(SqlBaseParser.ShowFunctionsContext ctx); - /** - * Exit a parse tree produced by the {@code showFunctions} - * labeled alternative in {@link SqlBaseParser#statement}. - * @param ctx the parse tree - */ - void exitShowFunctions(SqlBaseParser.ShowFunctionsContext ctx); - /** - * Enter a parse tree produced by the {@code showSchemas} - * labeled alternative in {@link SqlBaseParser#statement}. - * @param ctx the parse tree - */ - void enterShowSchemas(SqlBaseParser.ShowSchemasContext ctx); - /** - * Exit a parse tree produced by the {@code showSchemas} - * labeled alternative in {@link SqlBaseParser#statement}. - * @param ctx the parse tree - */ - void exitShowSchemas(SqlBaseParser.ShowSchemasContext ctx); - /** - * Enter a parse tree produced by the {@code showCatalogs} - * labeled alternative in {@link SqlBaseParser#statement}. - * @param ctx the parse tree - */ - void enterShowCatalogs(SqlBaseParser.ShowCatalogsContext ctx); - /** - * Exit a parse tree produced by the {@code showCatalogs} - * labeled alternative in {@link SqlBaseParser#statement}. - * @param ctx the parse tree - */ - void exitShowCatalogs(SqlBaseParser.ShowCatalogsContext ctx); - /** - * Enter a parse tree produced by the {@code sysTables} - * labeled alternative in {@link SqlBaseParser#statement}. - * @param ctx the parse tree - */ - void enterSysTables(SqlBaseParser.SysTablesContext ctx); - /** - * Exit a parse tree produced by the {@code sysTables} - * labeled alternative in {@link SqlBaseParser#statement}. - * @param ctx the parse tree - */ - void exitSysTables(SqlBaseParser.SysTablesContext ctx); - /** - * Enter a parse tree produced by the {@code sysColumns} - * labeled alternative in {@link SqlBaseParser#statement}. - * @param ctx the parse tree - */ - void enterSysColumns(SqlBaseParser.SysColumnsContext ctx); - /** - * Exit a parse tree produced by the {@code sysColumns} - * labeled alternative in {@link SqlBaseParser#statement}. - * @param ctx the parse tree - */ - void exitSysColumns(SqlBaseParser.SysColumnsContext ctx); - /** - * Enter a parse tree produced by the {@code sysTypes} - * labeled alternative in {@link SqlBaseParser#statement}. - * @param ctx the parse tree - */ - void enterSysTypes(SqlBaseParser.SysTypesContext ctx); - /** - * Exit a parse tree produced by the {@code sysTypes} - * labeled alternative in {@link SqlBaseParser#statement}. - * @param ctx the parse tree - */ - void exitSysTypes(SqlBaseParser.SysTypesContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#query}. - * @param ctx the parse tree - */ - void enterQuery(SqlBaseParser.QueryContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#query}. - * @param ctx the parse tree - */ - void exitQuery(SqlBaseParser.QueryContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#queryNoWith}. - * @param ctx the parse tree - */ - void enterQueryNoWith(SqlBaseParser.QueryNoWithContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#queryNoWith}. - * @param ctx the parse tree - */ - void exitQueryNoWith(SqlBaseParser.QueryNoWithContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#limitClause}. - * @param ctx the parse tree - */ - void enterLimitClause(SqlBaseParser.LimitClauseContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#limitClause}. - * @param ctx the parse tree - */ - void exitLimitClause(SqlBaseParser.LimitClauseContext ctx); - /** - * Enter a parse tree produced by the {@code queryPrimaryDefault} - * labeled alternative in {@link SqlBaseParser#queryTerm}. - * @param ctx the parse tree - */ - void enterQueryPrimaryDefault(SqlBaseParser.QueryPrimaryDefaultContext ctx); - /** - * Exit a parse tree produced by the {@code queryPrimaryDefault} - * labeled alternative in {@link SqlBaseParser#queryTerm}. - * @param ctx the parse tree - */ - void exitQueryPrimaryDefault(SqlBaseParser.QueryPrimaryDefaultContext ctx); - /** - * Enter a parse tree produced by the {@code subquery} - * labeled alternative in {@link SqlBaseParser#queryTerm}. - * @param ctx the parse tree - */ - void enterSubquery(SqlBaseParser.SubqueryContext ctx); - /** - * Exit a parse tree produced by the {@code subquery} - * labeled alternative in {@link SqlBaseParser#queryTerm}. - * @param ctx the parse tree - */ - void exitSubquery(SqlBaseParser.SubqueryContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#orderBy}. - * @param ctx the parse tree - */ - void enterOrderBy(SqlBaseParser.OrderByContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#orderBy}. - * @param ctx the parse tree - */ - void exitOrderBy(SqlBaseParser.OrderByContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#querySpecification}. - * @param ctx the parse tree - */ - void enterQuerySpecification(SqlBaseParser.QuerySpecificationContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#querySpecification}. - * @param ctx the parse tree - */ - void exitQuerySpecification(SqlBaseParser.QuerySpecificationContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#fromClause}. - * @param ctx the parse tree - */ - void enterFromClause(SqlBaseParser.FromClauseContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#fromClause}. - * @param ctx the parse tree - */ - void exitFromClause(SqlBaseParser.FromClauseContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#groupBy}. - * @param ctx the parse tree - */ - void enterGroupBy(SqlBaseParser.GroupByContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#groupBy}. - * @param ctx the parse tree - */ - void exitGroupBy(SqlBaseParser.GroupByContext ctx); - /** - * Enter a parse tree produced by the {@code singleGroupingSet} - * labeled alternative in {@link SqlBaseParser#groupingElement}. - * @param ctx the parse tree - */ - void enterSingleGroupingSet(SqlBaseParser.SingleGroupingSetContext ctx); - /** - * Exit a parse tree produced by the {@code singleGroupingSet} - * labeled alternative in {@link SqlBaseParser#groupingElement}. - * @param ctx the parse tree - */ - void exitSingleGroupingSet(SqlBaseParser.SingleGroupingSetContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#groupingExpressions}. - * @param ctx the parse tree - */ - void enterGroupingExpressions(SqlBaseParser.GroupingExpressionsContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#groupingExpressions}. - * @param ctx the parse tree - */ - void exitGroupingExpressions(SqlBaseParser.GroupingExpressionsContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#namedQuery}. - * @param ctx the parse tree - */ - void enterNamedQuery(SqlBaseParser.NamedQueryContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#namedQuery}. - * @param ctx the parse tree - */ - void exitNamedQuery(SqlBaseParser.NamedQueryContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#topClause}. - * @param ctx the parse tree - */ - void enterTopClause(SqlBaseParser.TopClauseContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#topClause}. - * @param ctx the parse tree - */ - void exitTopClause(SqlBaseParser.TopClauseContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#setQuantifier}. - * @param ctx the parse tree - */ - void enterSetQuantifier(SqlBaseParser.SetQuantifierContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#setQuantifier}. - * @param ctx the parse tree - */ - void exitSetQuantifier(SqlBaseParser.SetQuantifierContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#selectItems}. - * @param ctx the parse tree - */ - void enterSelectItems(SqlBaseParser.SelectItemsContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#selectItems}. - * @param ctx the parse tree - */ - void exitSelectItems(SqlBaseParser.SelectItemsContext ctx); - /** - * Enter a parse tree produced by the {@code selectExpression} - * labeled alternative in {@link SqlBaseParser#selectItem}. - * @param ctx the parse tree - */ - void enterSelectExpression(SqlBaseParser.SelectExpressionContext ctx); - /** - * Exit a parse tree produced by the {@code selectExpression} - * labeled alternative in {@link SqlBaseParser#selectItem}. - * @param ctx the parse tree - */ - void exitSelectExpression(SqlBaseParser.SelectExpressionContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#relation}. - * @param ctx the parse tree - */ - void enterRelation(SqlBaseParser.RelationContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#relation}. - * @param ctx the parse tree - */ - void exitRelation(SqlBaseParser.RelationContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#joinRelation}. - * @param ctx the parse tree - */ - void enterJoinRelation(SqlBaseParser.JoinRelationContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#joinRelation}. - * @param ctx the parse tree - */ - void exitJoinRelation(SqlBaseParser.JoinRelationContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#joinType}. - * @param ctx the parse tree - */ - void enterJoinType(SqlBaseParser.JoinTypeContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#joinType}. - * @param ctx the parse tree - */ - void exitJoinType(SqlBaseParser.JoinTypeContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#joinCriteria}. - * @param ctx the parse tree - */ - void enterJoinCriteria(SqlBaseParser.JoinCriteriaContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#joinCriteria}. - * @param ctx the parse tree - */ - void exitJoinCriteria(SqlBaseParser.JoinCriteriaContext ctx); - /** - * Enter a parse tree produced by the {@code tableName} - * labeled alternative in {@link SqlBaseParser#relationPrimary}. - * @param ctx the parse tree - */ - void enterTableName(SqlBaseParser.TableNameContext ctx); - /** - * Exit a parse tree produced by the {@code tableName} - * labeled alternative in {@link SqlBaseParser#relationPrimary}. - * @param ctx the parse tree - */ - void exitTableName(SqlBaseParser.TableNameContext ctx); - /** - * Enter a parse tree produced by the {@code aliasedQuery} - * labeled alternative in {@link SqlBaseParser#relationPrimary}. - * @param ctx the parse tree - */ - void enterAliasedQuery(SqlBaseParser.AliasedQueryContext ctx); - /** - * Exit a parse tree produced by the {@code aliasedQuery} - * labeled alternative in {@link SqlBaseParser#relationPrimary}. - * @param ctx the parse tree - */ - void exitAliasedQuery(SqlBaseParser.AliasedQueryContext ctx); - /** - * Enter a parse tree produced by the {@code aliasedRelation} - * labeled alternative in {@link SqlBaseParser#relationPrimary}. - * @param ctx the parse tree - */ - void enterAliasedRelation(SqlBaseParser.AliasedRelationContext ctx); - /** - * Exit a parse tree produced by the {@code aliasedRelation} - * labeled alternative in {@link SqlBaseParser#relationPrimary}. - * @param ctx the parse tree - */ - void exitAliasedRelation(SqlBaseParser.AliasedRelationContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#pivotClause}. - * @param ctx the parse tree - */ - void enterPivotClause(SqlBaseParser.PivotClauseContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#pivotClause}. - * @param ctx the parse tree - */ - void exitPivotClause(SqlBaseParser.PivotClauseContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#pivotArgs}. - * @param ctx the parse tree - */ - void enterPivotArgs(SqlBaseParser.PivotArgsContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#pivotArgs}. - * @param ctx the parse tree - */ - void exitPivotArgs(SqlBaseParser.PivotArgsContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#namedValueExpression}. - * @param ctx the parse tree - */ - void enterNamedValueExpression(SqlBaseParser.NamedValueExpressionContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#namedValueExpression}. - * @param ctx the parse tree - */ - void exitNamedValueExpression(SqlBaseParser.NamedValueExpressionContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#expression}. - * @param ctx the parse tree - */ - void enterExpression(SqlBaseParser.ExpressionContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#expression}. - * @param ctx the parse tree - */ - void exitExpression(SqlBaseParser.ExpressionContext ctx); - /** - * Enter a parse tree produced by the {@code logicalNot} - * labeled alternative in {@link SqlBaseParser#booleanExpression}. - * @param ctx the parse tree - */ - void enterLogicalNot(SqlBaseParser.LogicalNotContext ctx); - /** - * Exit a parse tree produced by the {@code logicalNot} - * labeled alternative in {@link SqlBaseParser#booleanExpression}. - * @param ctx the parse tree - */ - void exitLogicalNot(SqlBaseParser.LogicalNotContext ctx); - /** - * Enter a parse tree produced by the {@code stringQuery} - * labeled alternative in {@link SqlBaseParser#booleanExpression}. - * @param ctx the parse tree - */ - void enterStringQuery(SqlBaseParser.StringQueryContext ctx); - /** - * Exit a parse tree produced by the {@code stringQuery} - * labeled alternative in {@link SqlBaseParser#booleanExpression}. - * @param ctx the parse tree - */ - void exitStringQuery(SqlBaseParser.StringQueryContext ctx); - /** - * Enter a parse tree produced by the {@code booleanDefault} - * labeled alternative in {@link SqlBaseParser#booleanExpression}. - * @param ctx the parse tree - */ - void enterBooleanDefault(SqlBaseParser.BooleanDefaultContext ctx); - /** - * Exit a parse tree produced by the {@code booleanDefault} - * labeled alternative in {@link SqlBaseParser#booleanExpression}. - * @param ctx the parse tree - */ - void exitBooleanDefault(SqlBaseParser.BooleanDefaultContext ctx); - /** - * Enter a parse tree produced by the {@code exists} - * labeled alternative in {@link SqlBaseParser#booleanExpression}. - * @param ctx the parse tree - */ - void enterExists(SqlBaseParser.ExistsContext ctx); - /** - * Exit a parse tree produced by the {@code exists} - * labeled alternative in {@link SqlBaseParser#booleanExpression}. - * @param ctx the parse tree - */ - void exitExists(SqlBaseParser.ExistsContext ctx); - /** - * Enter a parse tree produced by the {@code multiMatchQuery} - * labeled alternative in {@link SqlBaseParser#booleanExpression}. - * @param ctx the parse tree - */ - void enterMultiMatchQuery(SqlBaseParser.MultiMatchQueryContext ctx); - /** - * Exit a parse tree produced by the {@code multiMatchQuery} - * labeled alternative in {@link SqlBaseParser#booleanExpression}. - * @param ctx the parse tree - */ - void exitMultiMatchQuery(SqlBaseParser.MultiMatchQueryContext ctx); - /** - * Enter a parse tree produced by the {@code matchQuery} - * labeled alternative in {@link SqlBaseParser#booleanExpression}. - * @param ctx the parse tree - */ - void enterMatchQuery(SqlBaseParser.MatchQueryContext ctx); - /** - * Exit a parse tree produced by the {@code matchQuery} - * labeled alternative in {@link SqlBaseParser#booleanExpression}. - * @param ctx the parse tree - */ - void exitMatchQuery(SqlBaseParser.MatchQueryContext ctx); - /** - * Enter a parse tree produced by the {@code logicalBinary} - * labeled alternative in {@link SqlBaseParser#booleanExpression}. - * @param ctx the parse tree - */ - void enterLogicalBinary(SqlBaseParser.LogicalBinaryContext ctx); - /** - * Exit a parse tree produced by the {@code logicalBinary} - * labeled alternative in {@link SqlBaseParser#booleanExpression}. - * @param ctx the parse tree - */ - void exitLogicalBinary(SqlBaseParser.LogicalBinaryContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#matchQueryOptions}. - * @param ctx the parse tree - */ - void enterMatchQueryOptions(SqlBaseParser.MatchQueryOptionsContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#matchQueryOptions}. - * @param ctx the parse tree - */ - void exitMatchQueryOptions(SqlBaseParser.MatchQueryOptionsContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#predicated}. - * @param ctx the parse tree - */ - void enterPredicated(SqlBaseParser.PredicatedContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#predicated}. - * @param ctx the parse tree - */ - void exitPredicated(SqlBaseParser.PredicatedContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#predicate}. - * @param ctx the parse tree - */ - void enterPredicate(SqlBaseParser.PredicateContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#predicate}. - * @param ctx the parse tree - */ - void exitPredicate(SqlBaseParser.PredicateContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#likePattern}. - * @param ctx the parse tree - */ - void enterLikePattern(SqlBaseParser.LikePatternContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#likePattern}. - * @param ctx the parse tree - */ - void exitLikePattern(SqlBaseParser.LikePatternContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#pattern}. - * @param ctx the parse tree - */ - void enterPattern(SqlBaseParser.PatternContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#pattern}. - * @param ctx the parse tree - */ - void exitPattern(SqlBaseParser.PatternContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#patternEscape}. - * @param ctx the parse tree - */ - void enterPatternEscape(SqlBaseParser.PatternEscapeContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#patternEscape}. - * @param ctx the parse tree - */ - void exitPatternEscape(SqlBaseParser.PatternEscapeContext ctx); - /** - * Enter a parse tree produced by the {@code valueExpressionDefault} - * labeled alternative in {@link SqlBaseParser#valueExpression}. - * @param ctx the parse tree - */ - void enterValueExpressionDefault(SqlBaseParser.ValueExpressionDefaultContext ctx); - /** - * Exit a parse tree produced by the {@code valueExpressionDefault} - * labeled alternative in {@link SqlBaseParser#valueExpression}. - * @param ctx the parse tree - */ - void exitValueExpressionDefault(SqlBaseParser.ValueExpressionDefaultContext ctx); - /** - * Enter a parse tree produced by the {@code comparison} - * labeled alternative in {@link SqlBaseParser#valueExpression}. - * @param ctx the parse tree - */ - void enterComparison(SqlBaseParser.ComparisonContext ctx); - /** - * Exit a parse tree produced by the {@code comparison} - * labeled alternative in {@link SqlBaseParser#valueExpression}. - * @param ctx the parse tree - */ - void exitComparison(SqlBaseParser.ComparisonContext ctx); - /** - * Enter a parse tree produced by the {@code arithmeticBinary} - * labeled alternative in {@link SqlBaseParser#valueExpression}. - * @param ctx the parse tree - */ - void enterArithmeticBinary(SqlBaseParser.ArithmeticBinaryContext ctx); - /** - * Exit a parse tree produced by the {@code arithmeticBinary} - * labeled alternative in {@link SqlBaseParser#valueExpression}. - * @param ctx the parse tree - */ - void exitArithmeticBinary(SqlBaseParser.ArithmeticBinaryContext ctx); - /** - * Enter a parse tree produced by the {@code arithmeticUnary} - * labeled alternative in {@link SqlBaseParser#valueExpression}. - * @param ctx the parse tree - */ - void enterArithmeticUnary(SqlBaseParser.ArithmeticUnaryContext ctx); - /** - * Exit a parse tree produced by the {@code arithmeticUnary} - * labeled alternative in {@link SqlBaseParser#valueExpression}. - * @param ctx the parse tree - */ - void exitArithmeticUnary(SqlBaseParser.ArithmeticUnaryContext ctx); - /** - * Enter a parse tree produced by the {@code dereference} - * labeled alternative in {@link SqlBaseParser#primaryExpression}. - * @param ctx the parse tree - */ - void enterDereference(SqlBaseParser.DereferenceContext ctx); - /** - * Exit a parse tree produced by the {@code dereference} - * labeled alternative in {@link SqlBaseParser#primaryExpression}. - * @param ctx the parse tree - */ - void exitDereference(SqlBaseParser.DereferenceContext ctx); - /** - * Enter a parse tree produced by the {@code cast} - * labeled alternative in {@link SqlBaseParser#primaryExpression}. - * @param ctx the parse tree - */ - void enterCast(SqlBaseParser.CastContext ctx); - /** - * Exit a parse tree produced by the {@code cast} - * labeled alternative in {@link SqlBaseParser#primaryExpression}. - * @param ctx the parse tree - */ - void exitCast(SqlBaseParser.CastContext ctx); - /** - * Enter a parse tree produced by the {@code constantDefault} - * labeled alternative in {@link SqlBaseParser#primaryExpression}. - * @param ctx the parse tree - */ - void enterConstantDefault(SqlBaseParser.ConstantDefaultContext ctx); - /** - * Exit a parse tree produced by the {@code constantDefault} - * labeled alternative in {@link SqlBaseParser#primaryExpression}. - * @param ctx the parse tree - */ - void exitConstantDefault(SqlBaseParser.ConstantDefaultContext ctx); - /** - * Enter a parse tree produced by the {@code extract} - * labeled alternative in {@link SqlBaseParser#primaryExpression}. - * @param ctx the parse tree - */ - void enterExtract(SqlBaseParser.ExtractContext ctx); - /** - * Exit a parse tree produced by the {@code extract} - * labeled alternative in {@link SqlBaseParser#primaryExpression}. - * @param ctx the parse tree - */ - void exitExtract(SqlBaseParser.ExtractContext ctx); - /** - * Enter a parse tree produced by the {@code parenthesizedExpression} - * labeled alternative in {@link SqlBaseParser#primaryExpression}. - * @param ctx the parse tree - */ - void enterParenthesizedExpression(SqlBaseParser.ParenthesizedExpressionContext ctx); - /** - * Exit a parse tree produced by the {@code parenthesizedExpression} - * labeled alternative in {@link SqlBaseParser#primaryExpression}. - * @param ctx the parse tree - */ - void exitParenthesizedExpression(SqlBaseParser.ParenthesizedExpressionContext ctx); - /** - * Enter a parse tree produced by the {@code star} - * labeled alternative in {@link SqlBaseParser#primaryExpression}. - * @param ctx the parse tree - */ - void enterStar(SqlBaseParser.StarContext ctx); - /** - * Exit a parse tree produced by the {@code star} - * labeled alternative in {@link SqlBaseParser#primaryExpression}. - * @param ctx the parse tree - */ - void exitStar(SqlBaseParser.StarContext ctx); - /** - * Enter a parse tree produced by the {@code castOperatorExpression} - * labeled alternative in {@link SqlBaseParser#primaryExpression}. - * @param ctx the parse tree - */ - void enterCastOperatorExpression(SqlBaseParser.CastOperatorExpressionContext ctx); - /** - * Exit a parse tree produced by the {@code castOperatorExpression} - * labeled alternative in {@link SqlBaseParser#primaryExpression}. - * @param ctx the parse tree - */ - void exitCastOperatorExpression(SqlBaseParser.CastOperatorExpressionContext ctx); - /** - * Enter a parse tree produced by the {@code function} - * labeled alternative in {@link SqlBaseParser#primaryExpression}. - * @param ctx the parse tree - */ - void enterFunction(SqlBaseParser.FunctionContext ctx); - /** - * Exit a parse tree produced by the {@code function} - * labeled alternative in {@link SqlBaseParser#primaryExpression}. - * @param ctx the parse tree - */ - void exitFunction(SqlBaseParser.FunctionContext ctx); - /** - * Enter a parse tree produced by the {@code currentDateTimeFunction} - * labeled alternative in {@link SqlBaseParser#primaryExpression}. - * @param ctx the parse tree - */ - void enterCurrentDateTimeFunction(SqlBaseParser.CurrentDateTimeFunctionContext ctx); - /** - * Exit a parse tree produced by the {@code currentDateTimeFunction} - * labeled alternative in {@link SqlBaseParser#primaryExpression}. - * @param ctx the parse tree - */ - void exitCurrentDateTimeFunction(SqlBaseParser.CurrentDateTimeFunctionContext ctx); - /** - * Enter a parse tree produced by the {@code subqueryExpression} - * labeled alternative in {@link SqlBaseParser#primaryExpression}. - * @param ctx the parse tree - */ - void enterSubqueryExpression(SqlBaseParser.SubqueryExpressionContext ctx); - /** - * Exit a parse tree produced by the {@code subqueryExpression} - * labeled alternative in {@link SqlBaseParser#primaryExpression}. - * @param ctx the parse tree - */ - void exitSubqueryExpression(SqlBaseParser.SubqueryExpressionContext ctx); - /** - * Enter a parse tree produced by the {@code case} - * labeled alternative in {@link SqlBaseParser#primaryExpression}. - * @param ctx the parse tree - */ - void enterCase(SqlBaseParser.CaseContext ctx); - /** - * Exit a parse tree produced by the {@code case} - * labeled alternative in {@link SqlBaseParser#primaryExpression}. - * @param ctx the parse tree - */ - void exitCase(SqlBaseParser.CaseContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#builtinDateTimeFunction}. - * @param ctx the parse tree - */ - void enterBuiltinDateTimeFunction(SqlBaseParser.BuiltinDateTimeFunctionContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#builtinDateTimeFunction}. - * @param ctx the parse tree - */ - void exitBuiltinDateTimeFunction(SqlBaseParser.BuiltinDateTimeFunctionContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#castExpression}. - * @param ctx the parse tree - */ - void enterCastExpression(SqlBaseParser.CastExpressionContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#castExpression}. - * @param ctx the parse tree - */ - void exitCastExpression(SqlBaseParser.CastExpressionContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#castTemplate}. - * @param ctx the parse tree - */ - void enterCastTemplate(SqlBaseParser.CastTemplateContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#castTemplate}. - * @param ctx the parse tree - */ - void exitCastTemplate(SqlBaseParser.CastTemplateContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#convertTemplate}. - * @param ctx the parse tree - */ - void enterConvertTemplate(SqlBaseParser.ConvertTemplateContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#convertTemplate}. - * @param ctx the parse tree - */ - void exitConvertTemplate(SqlBaseParser.ConvertTemplateContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#extractExpression}. - * @param ctx the parse tree - */ - void enterExtractExpression(SqlBaseParser.ExtractExpressionContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#extractExpression}. - * @param ctx the parse tree - */ - void exitExtractExpression(SqlBaseParser.ExtractExpressionContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#extractTemplate}. - * @param ctx the parse tree - */ - void enterExtractTemplate(SqlBaseParser.ExtractTemplateContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#extractTemplate}. - * @param ctx the parse tree - */ - void exitExtractTemplate(SqlBaseParser.ExtractTemplateContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#functionExpression}. - * @param ctx the parse tree - */ - void enterFunctionExpression(SqlBaseParser.FunctionExpressionContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#functionExpression}. - * @param ctx the parse tree - */ - void exitFunctionExpression(SqlBaseParser.FunctionExpressionContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#functionTemplate}. - * @param ctx the parse tree - */ - void enterFunctionTemplate(SqlBaseParser.FunctionTemplateContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#functionTemplate}. - * @param ctx the parse tree - */ - void exitFunctionTemplate(SqlBaseParser.FunctionTemplateContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#functionName}. - * @param ctx the parse tree - */ - void enterFunctionName(SqlBaseParser.FunctionNameContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#functionName}. - * @param ctx the parse tree - */ - void exitFunctionName(SqlBaseParser.FunctionNameContext ctx); - /** - * Enter a parse tree produced by the {@code nullLiteral} - * labeled alternative in {@link SqlBaseParser#constant}. - * @param ctx the parse tree - */ - void enterNullLiteral(SqlBaseParser.NullLiteralContext ctx); - /** - * Exit a parse tree produced by the {@code nullLiteral} - * labeled alternative in {@link SqlBaseParser#constant}. - * @param ctx the parse tree - */ - void exitNullLiteral(SqlBaseParser.NullLiteralContext ctx); - /** - * Enter a parse tree produced by the {@code intervalLiteral} - * labeled alternative in {@link SqlBaseParser#constant}. - * @param ctx the parse tree - */ - void enterIntervalLiteral(SqlBaseParser.IntervalLiteralContext ctx); - /** - * Exit a parse tree produced by the {@code intervalLiteral} - * labeled alternative in {@link SqlBaseParser#constant}. - * @param ctx the parse tree - */ - void exitIntervalLiteral(SqlBaseParser.IntervalLiteralContext ctx); - /** - * Enter a parse tree produced by the {@code numericLiteral} - * labeled alternative in {@link SqlBaseParser#constant}. - * @param ctx the parse tree - */ - void enterNumericLiteral(SqlBaseParser.NumericLiteralContext ctx); - /** - * Exit a parse tree produced by the {@code numericLiteral} - * labeled alternative in {@link SqlBaseParser#constant}. - * @param ctx the parse tree - */ - void exitNumericLiteral(SqlBaseParser.NumericLiteralContext ctx); - /** - * Enter a parse tree produced by the {@code booleanLiteral} - * labeled alternative in {@link SqlBaseParser#constant}. - * @param ctx the parse tree - */ - void enterBooleanLiteral(SqlBaseParser.BooleanLiteralContext ctx); - /** - * Exit a parse tree produced by the {@code booleanLiteral} - * labeled alternative in {@link SqlBaseParser#constant}. - * @param ctx the parse tree - */ - void exitBooleanLiteral(SqlBaseParser.BooleanLiteralContext ctx); - /** - * Enter a parse tree produced by the {@code stringLiteral} - * labeled alternative in {@link SqlBaseParser#constant}. - * @param ctx the parse tree - */ - void enterStringLiteral(SqlBaseParser.StringLiteralContext ctx); - /** - * Exit a parse tree produced by the {@code stringLiteral} - * labeled alternative in {@link SqlBaseParser#constant}. - * @param ctx the parse tree - */ - void exitStringLiteral(SqlBaseParser.StringLiteralContext ctx); - /** - * Enter a parse tree produced by the {@code paramLiteral} - * labeled alternative in {@link SqlBaseParser#constant}. - * @param ctx the parse tree - */ - void enterParamLiteral(SqlBaseParser.ParamLiteralContext ctx); - /** - * Exit a parse tree produced by the {@code paramLiteral} - * labeled alternative in {@link SqlBaseParser#constant}. - * @param ctx the parse tree - */ - void exitParamLiteral(SqlBaseParser.ParamLiteralContext ctx); - /** - * Enter a parse tree produced by the {@code dateEscapedLiteral} - * labeled alternative in {@link SqlBaseParser#constant}. - * @param ctx the parse tree - */ - void enterDateEscapedLiteral(SqlBaseParser.DateEscapedLiteralContext ctx); - /** - * Exit a parse tree produced by the {@code dateEscapedLiteral} - * labeled alternative in {@link SqlBaseParser#constant}. - * @param ctx the parse tree - */ - void exitDateEscapedLiteral(SqlBaseParser.DateEscapedLiteralContext ctx); - /** - * Enter a parse tree produced by the {@code timeEscapedLiteral} - * labeled alternative in {@link SqlBaseParser#constant}. - * @param ctx the parse tree - */ - void enterTimeEscapedLiteral(SqlBaseParser.TimeEscapedLiteralContext ctx); - /** - * Exit a parse tree produced by the {@code timeEscapedLiteral} - * labeled alternative in {@link SqlBaseParser#constant}. - * @param ctx the parse tree - */ - void exitTimeEscapedLiteral(SqlBaseParser.TimeEscapedLiteralContext ctx); - /** - * Enter a parse tree produced by the {@code timestampEscapedLiteral} - * labeled alternative in {@link SqlBaseParser#constant}. - * @param ctx the parse tree - */ - void enterTimestampEscapedLiteral(SqlBaseParser.TimestampEscapedLiteralContext ctx); - /** - * Exit a parse tree produced by the {@code timestampEscapedLiteral} - * labeled alternative in {@link SqlBaseParser#constant}. - * @param ctx the parse tree - */ - void exitTimestampEscapedLiteral(SqlBaseParser.TimestampEscapedLiteralContext ctx); - /** - * Enter a parse tree produced by the {@code guidEscapedLiteral} - * labeled alternative in {@link SqlBaseParser#constant}. - * @param ctx the parse tree - */ - void enterGuidEscapedLiteral(SqlBaseParser.GuidEscapedLiteralContext ctx); - /** - * Exit a parse tree produced by the {@code guidEscapedLiteral} - * labeled alternative in {@link SqlBaseParser#constant}. - * @param ctx the parse tree - */ - void exitGuidEscapedLiteral(SqlBaseParser.GuidEscapedLiteralContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#comparisonOperator}. - * @param ctx the parse tree - */ - void enterComparisonOperator(SqlBaseParser.ComparisonOperatorContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#comparisonOperator}. - * @param ctx the parse tree - */ - void exitComparisonOperator(SqlBaseParser.ComparisonOperatorContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#booleanValue}. - * @param ctx the parse tree - */ - void enterBooleanValue(SqlBaseParser.BooleanValueContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#booleanValue}. - * @param ctx the parse tree - */ - void exitBooleanValue(SqlBaseParser.BooleanValueContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#interval}. - * @param ctx the parse tree - */ - void enterInterval(SqlBaseParser.IntervalContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#interval}. - * @param ctx the parse tree - */ - void exitInterval(SqlBaseParser.IntervalContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#intervalField}. - * @param ctx the parse tree - */ - void enterIntervalField(SqlBaseParser.IntervalFieldContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#intervalField}. - * @param ctx the parse tree - */ - void exitIntervalField(SqlBaseParser.IntervalFieldContext ctx); - /** - * Enter a parse tree produced by the {@code primitiveDataType} - * labeled alternative in {@link SqlBaseParser#dataType}. - * @param ctx the parse tree - */ - void enterPrimitiveDataType(SqlBaseParser.PrimitiveDataTypeContext ctx); - /** - * Exit a parse tree produced by the {@code primitiveDataType} - * labeled alternative in {@link SqlBaseParser#dataType}. - * @param ctx the parse tree - */ - void exitPrimitiveDataType(SqlBaseParser.PrimitiveDataTypeContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#qualifiedName}. - * @param ctx the parse tree - */ - void enterQualifiedName(SqlBaseParser.QualifiedNameContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#qualifiedName}. - * @param ctx the parse tree - */ - void exitQualifiedName(SqlBaseParser.QualifiedNameContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#identifier}. - * @param ctx the parse tree - */ - void enterIdentifier(SqlBaseParser.IdentifierContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#identifier}. - * @param ctx the parse tree - */ - void exitIdentifier(SqlBaseParser.IdentifierContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#tableIdentifier}. - * @param ctx the parse tree - */ - void enterTableIdentifier(SqlBaseParser.TableIdentifierContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#tableIdentifier}. - * @param ctx the parse tree - */ - void exitTableIdentifier(SqlBaseParser.TableIdentifierContext ctx); - /** - * Enter a parse tree produced by the {@code quotedIdentifier} - * labeled alternative in {@link SqlBaseParser#quoteIdentifier}. - * @param ctx the parse tree - */ - void enterQuotedIdentifier(SqlBaseParser.QuotedIdentifierContext ctx); - /** - * Exit a parse tree produced by the {@code quotedIdentifier} - * labeled alternative in {@link SqlBaseParser#quoteIdentifier}. - * @param ctx the parse tree - */ - void exitQuotedIdentifier(SqlBaseParser.QuotedIdentifierContext ctx); - /** - * Enter a parse tree produced by the {@code backQuotedIdentifier} - * labeled alternative in {@link SqlBaseParser#quoteIdentifier}. - * @param ctx the parse tree - */ - void enterBackQuotedIdentifier(SqlBaseParser.BackQuotedIdentifierContext ctx); - /** - * Exit a parse tree produced by the {@code backQuotedIdentifier} - * labeled alternative in {@link SqlBaseParser#quoteIdentifier}. - * @param ctx the parse tree - */ - void exitBackQuotedIdentifier(SqlBaseParser.BackQuotedIdentifierContext ctx); - /** - * Enter a parse tree produced by the {@code unquotedIdentifier} - * labeled alternative in {@link SqlBaseParser#unquoteIdentifier}. - * @param ctx the parse tree - */ - void enterUnquotedIdentifier(SqlBaseParser.UnquotedIdentifierContext ctx); - /** - * Exit a parse tree produced by the {@code unquotedIdentifier} - * labeled alternative in {@link SqlBaseParser#unquoteIdentifier}. - * @param ctx the parse tree - */ - void exitUnquotedIdentifier(SqlBaseParser.UnquotedIdentifierContext ctx); - /** - * Enter a parse tree produced by the {@code digitIdentifier} - * labeled alternative in {@link SqlBaseParser#unquoteIdentifier}. - * @param ctx the parse tree - */ - void enterDigitIdentifier(SqlBaseParser.DigitIdentifierContext ctx); - /** - * Exit a parse tree produced by the {@code digitIdentifier} - * labeled alternative in {@link SqlBaseParser#unquoteIdentifier}. - * @param ctx the parse tree - */ - void exitDigitIdentifier(SqlBaseParser.DigitIdentifierContext ctx); - /** - * Enter a parse tree produced by the {@code decimalLiteral} - * labeled alternative in {@link SqlBaseParser#number}. - * @param ctx the parse tree - */ - void enterDecimalLiteral(SqlBaseParser.DecimalLiteralContext ctx); - /** - * Exit a parse tree produced by the {@code decimalLiteral} - * labeled alternative in {@link SqlBaseParser#number}. - * @param ctx the parse tree - */ - void exitDecimalLiteral(SqlBaseParser.DecimalLiteralContext ctx); - /** - * Enter a parse tree produced by the {@code integerLiteral} - * labeled alternative in {@link SqlBaseParser#number}. - * @param ctx the parse tree - */ - void enterIntegerLiteral(SqlBaseParser.IntegerLiteralContext ctx); - /** - * Exit a parse tree produced by the {@code integerLiteral} - * labeled alternative in {@link SqlBaseParser#number}. - * @param ctx the parse tree - */ - void exitIntegerLiteral(SqlBaseParser.IntegerLiteralContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#string}. - * @param ctx the parse tree - */ - void enterString(SqlBaseParser.StringContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#string}. - * @param ctx the parse tree - */ - void exitString(SqlBaseParser.StringContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#whenClause}. - * @param ctx the parse tree - */ - void enterWhenClause(SqlBaseParser.WhenClauseContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#whenClause}. - * @param ctx the parse tree - */ - void exitWhenClause(SqlBaseParser.WhenClauseContext ctx); - /** - * Enter a parse tree produced by {@link SqlBaseParser#nonReserved}. - * @param ctx the parse tree - */ - void enterNonReserved(SqlBaseParser.NonReservedContext ctx); - /** - * Exit a parse tree produced by {@link SqlBaseParser#nonReserved}. - * @param ctx the parse tree - */ - void exitNonReserved(SqlBaseParser.NonReservedContext ctx); + /** + * Enter a parse tree produced by {@link SqlBaseParser#singleStatement}. + * @param ctx the parse tree + */ + void enterSingleStatement(SqlBaseParser.SingleStatementContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#singleStatement}. + * @param ctx the parse tree + */ + void exitSingleStatement(SqlBaseParser.SingleStatementContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#singleExpression}. + * @param ctx the parse tree + */ + void enterSingleExpression(SqlBaseParser.SingleExpressionContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#singleExpression}. + * @param ctx the parse tree + */ + void exitSingleExpression(SqlBaseParser.SingleExpressionContext ctx); + + /** + * Enter a parse tree produced by the {@code statementDefault} + * labeled alternative in {@link SqlBaseParser#statement}. + * @param ctx the parse tree + */ + void enterStatementDefault(SqlBaseParser.StatementDefaultContext ctx); + + /** + * Exit a parse tree produced by the {@code statementDefault} + * labeled alternative in {@link SqlBaseParser#statement}. + * @param ctx the parse tree + */ + void exitStatementDefault(SqlBaseParser.StatementDefaultContext ctx); + + /** + * Enter a parse tree produced by the {@code explain} + * labeled alternative in {@link SqlBaseParser#statement}. + * @param ctx the parse tree + */ + void enterExplain(SqlBaseParser.ExplainContext ctx); + + /** + * Exit a parse tree produced by the {@code explain} + * labeled alternative in {@link SqlBaseParser#statement}. + * @param ctx the parse tree + */ + void exitExplain(SqlBaseParser.ExplainContext ctx); + + /** + * Enter a parse tree produced by the {@code debug} + * labeled alternative in {@link SqlBaseParser#statement}. + * @param ctx the parse tree + */ + void enterDebug(SqlBaseParser.DebugContext ctx); + + /** + * Exit a parse tree produced by the {@code debug} + * labeled alternative in {@link SqlBaseParser#statement}. + * @param ctx the parse tree + */ + void exitDebug(SqlBaseParser.DebugContext ctx); + + /** + * Enter a parse tree produced by the {@code showTables} + * labeled alternative in {@link SqlBaseParser#statement}. + * @param ctx the parse tree + */ + void enterShowTables(SqlBaseParser.ShowTablesContext ctx); + + /** + * Exit a parse tree produced by the {@code showTables} + * labeled alternative in {@link SqlBaseParser#statement}. + * @param ctx the parse tree + */ + void exitShowTables(SqlBaseParser.ShowTablesContext ctx); + + /** + * Enter a parse tree produced by the {@code showColumns} + * labeled alternative in {@link SqlBaseParser#statement}. + * @param ctx the parse tree + */ + void enterShowColumns(SqlBaseParser.ShowColumnsContext ctx); + + /** + * Exit a parse tree produced by the {@code showColumns} + * labeled alternative in {@link SqlBaseParser#statement}. + * @param ctx the parse tree + */ + void exitShowColumns(SqlBaseParser.ShowColumnsContext ctx); + + /** + * Enter a parse tree produced by the {@code showFunctions} + * labeled alternative in {@link SqlBaseParser#statement}. + * @param ctx the parse tree + */ + void enterShowFunctions(SqlBaseParser.ShowFunctionsContext ctx); + + /** + * Exit a parse tree produced by the {@code showFunctions} + * labeled alternative in {@link SqlBaseParser#statement}. + * @param ctx the parse tree + */ + void exitShowFunctions(SqlBaseParser.ShowFunctionsContext ctx); + + /** + * Enter a parse tree produced by the {@code showSchemas} + * labeled alternative in {@link SqlBaseParser#statement}. + * @param ctx the parse tree + */ + void enterShowSchemas(SqlBaseParser.ShowSchemasContext ctx); + + /** + * Exit a parse tree produced by the {@code showSchemas} + * labeled alternative in {@link SqlBaseParser#statement}. + * @param ctx the parse tree + */ + void exitShowSchemas(SqlBaseParser.ShowSchemasContext ctx); + + /** + * Enter a parse tree produced by the {@code showCatalogs} + * labeled alternative in {@link SqlBaseParser#statement}. + * @param ctx the parse tree + */ + void enterShowCatalogs(SqlBaseParser.ShowCatalogsContext ctx); + + /** + * Exit a parse tree produced by the {@code showCatalogs} + * labeled alternative in {@link SqlBaseParser#statement}. + * @param ctx the parse tree + */ + void exitShowCatalogs(SqlBaseParser.ShowCatalogsContext ctx); + + /** + * Enter a parse tree produced by the {@code sysTables} + * labeled alternative in {@link SqlBaseParser#statement}. + * @param ctx the parse tree + */ + void enterSysTables(SqlBaseParser.SysTablesContext ctx); + + /** + * Exit a parse tree produced by the {@code sysTables} + * labeled alternative in {@link SqlBaseParser#statement}. + * @param ctx the parse tree + */ + void exitSysTables(SqlBaseParser.SysTablesContext ctx); + + /** + * Enter a parse tree produced by the {@code sysColumns} + * labeled alternative in {@link SqlBaseParser#statement}. + * @param ctx the parse tree + */ + void enterSysColumns(SqlBaseParser.SysColumnsContext ctx); + + /** + * Exit a parse tree produced by the {@code sysColumns} + * labeled alternative in {@link SqlBaseParser#statement}. + * @param ctx the parse tree + */ + void exitSysColumns(SqlBaseParser.SysColumnsContext ctx); + + /** + * Enter a parse tree produced by the {@code sysTypes} + * labeled alternative in {@link SqlBaseParser#statement}. + * @param ctx the parse tree + */ + void enterSysTypes(SqlBaseParser.SysTypesContext ctx); + + /** + * Exit a parse tree produced by the {@code sysTypes} + * labeled alternative in {@link SqlBaseParser#statement}. + * @param ctx the parse tree + */ + void exitSysTypes(SqlBaseParser.SysTypesContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#query}. + * @param ctx the parse tree + */ + void enterQuery(SqlBaseParser.QueryContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#query}. + * @param ctx the parse tree + */ + void exitQuery(SqlBaseParser.QueryContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#queryNoWith}. + * @param ctx the parse tree + */ + void enterQueryNoWith(SqlBaseParser.QueryNoWithContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#queryNoWith}. + * @param ctx the parse tree + */ + void exitQueryNoWith(SqlBaseParser.QueryNoWithContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#limitClause}. + * @param ctx the parse tree + */ + void enterLimitClause(SqlBaseParser.LimitClauseContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#limitClause}. + * @param ctx the parse tree + */ + void exitLimitClause(SqlBaseParser.LimitClauseContext ctx); + + /** + * Enter a parse tree produced by the {@code queryPrimaryDefault} + * labeled alternative in {@link SqlBaseParser#queryTerm}. + * @param ctx the parse tree + */ + void enterQueryPrimaryDefault(SqlBaseParser.QueryPrimaryDefaultContext ctx); + + /** + * Exit a parse tree produced by the {@code queryPrimaryDefault} + * labeled alternative in {@link SqlBaseParser#queryTerm}. + * @param ctx the parse tree + */ + void exitQueryPrimaryDefault(SqlBaseParser.QueryPrimaryDefaultContext ctx); + + /** + * Enter a parse tree produced by the {@code subquery} + * labeled alternative in {@link SqlBaseParser#queryTerm}. + * @param ctx the parse tree + */ + void enterSubquery(SqlBaseParser.SubqueryContext ctx); + + /** + * Exit a parse tree produced by the {@code subquery} + * labeled alternative in {@link SqlBaseParser#queryTerm}. + * @param ctx the parse tree + */ + void exitSubquery(SqlBaseParser.SubqueryContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#orderBy}. + * @param ctx the parse tree + */ + void enterOrderBy(SqlBaseParser.OrderByContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#orderBy}. + * @param ctx the parse tree + */ + void exitOrderBy(SqlBaseParser.OrderByContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#querySpecification}. + * @param ctx the parse tree + */ + void enterQuerySpecification(SqlBaseParser.QuerySpecificationContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#querySpecification}. + * @param ctx the parse tree + */ + void exitQuerySpecification(SqlBaseParser.QuerySpecificationContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#fromClause}. + * @param ctx the parse tree + */ + void enterFromClause(SqlBaseParser.FromClauseContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#fromClause}. + * @param ctx the parse tree + */ + void exitFromClause(SqlBaseParser.FromClauseContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#groupBy}. + * @param ctx the parse tree + */ + void enterGroupBy(SqlBaseParser.GroupByContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#groupBy}. + * @param ctx the parse tree + */ + void exitGroupBy(SqlBaseParser.GroupByContext ctx); + + /** + * Enter a parse tree produced by the {@code singleGroupingSet} + * labeled alternative in {@link SqlBaseParser#groupingElement}. + * @param ctx the parse tree + */ + void enterSingleGroupingSet(SqlBaseParser.SingleGroupingSetContext ctx); + + /** + * Exit a parse tree produced by the {@code singleGroupingSet} + * labeled alternative in {@link SqlBaseParser#groupingElement}. + * @param ctx the parse tree + */ + void exitSingleGroupingSet(SqlBaseParser.SingleGroupingSetContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#groupingExpressions}. + * @param ctx the parse tree + */ + void enterGroupingExpressions(SqlBaseParser.GroupingExpressionsContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#groupingExpressions}. + * @param ctx the parse tree + */ + void exitGroupingExpressions(SqlBaseParser.GroupingExpressionsContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#namedQuery}. + * @param ctx the parse tree + */ + void enterNamedQuery(SqlBaseParser.NamedQueryContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#namedQuery}. + * @param ctx the parse tree + */ + void exitNamedQuery(SqlBaseParser.NamedQueryContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#topClause}. + * @param ctx the parse tree + */ + void enterTopClause(SqlBaseParser.TopClauseContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#topClause}. + * @param ctx the parse tree + */ + void exitTopClause(SqlBaseParser.TopClauseContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#setQuantifier}. + * @param ctx the parse tree + */ + void enterSetQuantifier(SqlBaseParser.SetQuantifierContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#setQuantifier}. + * @param ctx the parse tree + */ + void exitSetQuantifier(SqlBaseParser.SetQuantifierContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#selectItems}. + * @param ctx the parse tree + */ + void enterSelectItems(SqlBaseParser.SelectItemsContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#selectItems}. + * @param ctx the parse tree + */ + void exitSelectItems(SqlBaseParser.SelectItemsContext ctx); + + /** + * Enter a parse tree produced by the {@code selectExpression} + * labeled alternative in {@link SqlBaseParser#selectItem}. + * @param ctx the parse tree + */ + void enterSelectExpression(SqlBaseParser.SelectExpressionContext ctx); + + /** + * Exit a parse tree produced by the {@code selectExpression} + * labeled alternative in {@link SqlBaseParser#selectItem}. + * @param ctx the parse tree + */ + void exitSelectExpression(SqlBaseParser.SelectExpressionContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#relation}. + * @param ctx the parse tree + */ + void enterRelation(SqlBaseParser.RelationContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#relation}. + * @param ctx the parse tree + */ + void exitRelation(SqlBaseParser.RelationContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#joinRelation}. + * @param ctx the parse tree + */ + void enterJoinRelation(SqlBaseParser.JoinRelationContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#joinRelation}. + * @param ctx the parse tree + */ + void exitJoinRelation(SqlBaseParser.JoinRelationContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#joinType}. + * @param ctx the parse tree + */ + void enterJoinType(SqlBaseParser.JoinTypeContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#joinType}. + * @param ctx the parse tree + */ + void exitJoinType(SqlBaseParser.JoinTypeContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#joinCriteria}. + * @param ctx the parse tree + */ + void enterJoinCriteria(SqlBaseParser.JoinCriteriaContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#joinCriteria}. + * @param ctx the parse tree + */ + void exitJoinCriteria(SqlBaseParser.JoinCriteriaContext ctx); + + /** + * Enter a parse tree produced by the {@code tableName} + * labeled alternative in {@link SqlBaseParser#relationPrimary}. + * @param ctx the parse tree + */ + void enterTableName(SqlBaseParser.TableNameContext ctx); + + /** + * Exit a parse tree produced by the {@code tableName} + * labeled alternative in {@link SqlBaseParser#relationPrimary}. + * @param ctx the parse tree + */ + void exitTableName(SqlBaseParser.TableNameContext ctx); + + /** + * Enter a parse tree produced by the {@code aliasedQuery} + * labeled alternative in {@link SqlBaseParser#relationPrimary}. + * @param ctx the parse tree + */ + void enterAliasedQuery(SqlBaseParser.AliasedQueryContext ctx); + + /** + * Exit a parse tree produced by the {@code aliasedQuery} + * labeled alternative in {@link SqlBaseParser#relationPrimary}. + * @param ctx the parse tree + */ + void exitAliasedQuery(SqlBaseParser.AliasedQueryContext ctx); + + /** + * Enter a parse tree produced by the {@code aliasedRelation} + * labeled alternative in {@link SqlBaseParser#relationPrimary}. + * @param ctx the parse tree + */ + void enterAliasedRelation(SqlBaseParser.AliasedRelationContext ctx); + + /** + * Exit a parse tree produced by the {@code aliasedRelation} + * labeled alternative in {@link SqlBaseParser#relationPrimary}. + * @param ctx the parse tree + */ + void exitAliasedRelation(SqlBaseParser.AliasedRelationContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#pivotClause}. + * @param ctx the parse tree + */ + void enterPivotClause(SqlBaseParser.PivotClauseContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#pivotClause}. + * @param ctx the parse tree + */ + void exitPivotClause(SqlBaseParser.PivotClauseContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#pivotArgs}. + * @param ctx the parse tree + */ + void enterPivotArgs(SqlBaseParser.PivotArgsContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#pivotArgs}. + * @param ctx the parse tree + */ + void exitPivotArgs(SqlBaseParser.PivotArgsContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#namedValueExpression}. + * @param ctx the parse tree + */ + void enterNamedValueExpression(SqlBaseParser.NamedValueExpressionContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#namedValueExpression}. + * @param ctx the parse tree + */ + void exitNamedValueExpression(SqlBaseParser.NamedValueExpressionContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#expression}. + * @param ctx the parse tree + */ + void enterExpression(SqlBaseParser.ExpressionContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#expression}. + * @param ctx the parse tree + */ + void exitExpression(SqlBaseParser.ExpressionContext ctx); + + /** + * Enter a parse tree produced by the {@code logicalNot} + * labeled alternative in {@link SqlBaseParser#booleanExpression}. + * @param ctx the parse tree + */ + void enterLogicalNot(SqlBaseParser.LogicalNotContext ctx); + + /** + * Exit a parse tree produced by the {@code logicalNot} + * labeled alternative in {@link SqlBaseParser#booleanExpression}. + * @param ctx the parse tree + */ + void exitLogicalNot(SqlBaseParser.LogicalNotContext ctx); + + /** + * Enter a parse tree produced by the {@code stringQuery} + * labeled alternative in {@link SqlBaseParser#booleanExpression}. + * @param ctx the parse tree + */ + void enterStringQuery(SqlBaseParser.StringQueryContext ctx); + + /** + * Exit a parse tree produced by the {@code stringQuery} + * labeled alternative in {@link SqlBaseParser#booleanExpression}. + * @param ctx the parse tree + */ + void exitStringQuery(SqlBaseParser.StringQueryContext ctx); + + /** + * Enter a parse tree produced by the {@code booleanDefault} + * labeled alternative in {@link SqlBaseParser#booleanExpression}. + * @param ctx the parse tree + */ + void enterBooleanDefault(SqlBaseParser.BooleanDefaultContext ctx); + + /** + * Exit a parse tree produced by the {@code booleanDefault} + * labeled alternative in {@link SqlBaseParser#booleanExpression}. + * @param ctx the parse tree + */ + void exitBooleanDefault(SqlBaseParser.BooleanDefaultContext ctx); + + /** + * Enter a parse tree produced by the {@code exists} + * labeled alternative in {@link SqlBaseParser#booleanExpression}. + * @param ctx the parse tree + */ + void enterExists(SqlBaseParser.ExistsContext ctx); + + /** + * Exit a parse tree produced by the {@code exists} + * labeled alternative in {@link SqlBaseParser#booleanExpression}. + * @param ctx the parse tree + */ + void exitExists(SqlBaseParser.ExistsContext ctx); + + /** + * Enter a parse tree produced by the {@code multiMatchQuery} + * labeled alternative in {@link SqlBaseParser#booleanExpression}. + * @param ctx the parse tree + */ + void enterMultiMatchQuery(SqlBaseParser.MultiMatchQueryContext ctx); + + /** + * Exit a parse tree produced by the {@code multiMatchQuery} + * labeled alternative in {@link SqlBaseParser#booleanExpression}. + * @param ctx the parse tree + */ + void exitMultiMatchQuery(SqlBaseParser.MultiMatchQueryContext ctx); + + /** + * Enter a parse tree produced by the {@code matchQuery} + * labeled alternative in {@link SqlBaseParser#booleanExpression}. + * @param ctx the parse tree + */ + void enterMatchQuery(SqlBaseParser.MatchQueryContext ctx); + + /** + * Exit a parse tree produced by the {@code matchQuery} + * labeled alternative in {@link SqlBaseParser#booleanExpression}. + * @param ctx the parse tree + */ + void exitMatchQuery(SqlBaseParser.MatchQueryContext ctx); + + /** + * Enter a parse tree produced by the {@code logicalBinary} + * labeled alternative in {@link SqlBaseParser#booleanExpression}. + * @param ctx the parse tree + */ + void enterLogicalBinary(SqlBaseParser.LogicalBinaryContext ctx); + + /** + * Exit a parse tree produced by the {@code logicalBinary} + * labeled alternative in {@link SqlBaseParser#booleanExpression}. + * @param ctx the parse tree + */ + void exitLogicalBinary(SqlBaseParser.LogicalBinaryContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#matchQueryOptions}. + * @param ctx the parse tree + */ + void enterMatchQueryOptions(SqlBaseParser.MatchQueryOptionsContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#matchQueryOptions}. + * @param ctx the parse tree + */ + void exitMatchQueryOptions(SqlBaseParser.MatchQueryOptionsContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#predicated}. + * @param ctx the parse tree + */ + void enterPredicated(SqlBaseParser.PredicatedContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#predicated}. + * @param ctx the parse tree + */ + void exitPredicated(SqlBaseParser.PredicatedContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#predicate}. + * @param ctx the parse tree + */ + void enterPredicate(SqlBaseParser.PredicateContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#predicate}. + * @param ctx the parse tree + */ + void exitPredicate(SqlBaseParser.PredicateContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#likePattern}. + * @param ctx the parse tree + */ + void enterLikePattern(SqlBaseParser.LikePatternContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#likePattern}. + * @param ctx the parse tree + */ + void exitLikePattern(SqlBaseParser.LikePatternContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#pattern}. + * @param ctx the parse tree + */ + void enterPattern(SqlBaseParser.PatternContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#pattern}. + * @param ctx the parse tree + */ + void exitPattern(SqlBaseParser.PatternContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#patternEscape}. + * @param ctx the parse tree + */ + void enterPatternEscape(SqlBaseParser.PatternEscapeContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#patternEscape}. + * @param ctx the parse tree + */ + void exitPatternEscape(SqlBaseParser.PatternEscapeContext ctx); + + /** + * Enter a parse tree produced by the {@code valueExpressionDefault} + * labeled alternative in {@link SqlBaseParser#valueExpression}. + * @param ctx the parse tree + */ + void enterValueExpressionDefault(SqlBaseParser.ValueExpressionDefaultContext ctx); + + /** + * Exit a parse tree produced by the {@code valueExpressionDefault} + * labeled alternative in {@link SqlBaseParser#valueExpression}. + * @param ctx the parse tree + */ + void exitValueExpressionDefault(SqlBaseParser.ValueExpressionDefaultContext ctx); + + /** + * Enter a parse tree produced by the {@code comparison} + * labeled alternative in {@link SqlBaseParser#valueExpression}. + * @param ctx the parse tree + */ + void enterComparison(SqlBaseParser.ComparisonContext ctx); + + /** + * Exit a parse tree produced by the {@code comparison} + * labeled alternative in {@link SqlBaseParser#valueExpression}. + * @param ctx the parse tree + */ + void exitComparison(SqlBaseParser.ComparisonContext ctx); + + /** + * Enter a parse tree produced by the {@code arithmeticBinary} + * labeled alternative in {@link SqlBaseParser#valueExpression}. + * @param ctx the parse tree + */ + void enterArithmeticBinary(SqlBaseParser.ArithmeticBinaryContext ctx); + + /** + * Exit a parse tree produced by the {@code arithmeticBinary} + * labeled alternative in {@link SqlBaseParser#valueExpression}. + * @param ctx the parse tree + */ + void exitArithmeticBinary(SqlBaseParser.ArithmeticBinaryContext ctx); + + /** + * Enter a parse tree produced by the {@code arithmeticUnary} + * labeled alternative in {@link SqlBaseParser#valueExpression}. + * @param ctx the parse tree + */ + void enterArithmeticUnary(SqlBaseParser.ArithmeticUnaryContext ctx); + + /** + * Exit a parse tree produced by the {@code arithmeticUnary} + * labeled alternative in {@link SqlBaseParser#valueExpression}. + * @param ctx the parse tree + */ + void exitArithmeticUnary(SqlBaseParser.ArithmeticUnaryContext ctx); + + /** + * Enter a parse tree produced by the {@code dereference} + * labeled alternative in {@link SqlBaseParser#primaryExpression}. + * @param ctx the parse tree + */ + void enterDereference(SqlBaseParser.DereferenceContext ctx); + + /** + * Exit a parse tree produced by the {@code dereference} + * labeled alternative in {@link SqlBaseParser#primaryExpression}. + * @param ctx the parse tree + */ + void exitDereference(SqlBaseParser.DereferenceContext ctx); + + /** + * Enter a parse tree produced by the {@code cast} + * labeled alternative in {@link SqlBaseParser#primaryExpression}. + * @param ctx the parse tree + */ + void enterCast(SqlBaseParser.CastContext ctx); + + /** + * Exit a parse tree produced by the {@code cast} + * labeled alternative in {@link SqlBaseParser#primaryExpression}. + * @param ctx the parse tree + */ + void exitCast(SqlBaseParser.CastContext ctx); + + /** + * Enter a parse tree produced by the {@code constantDefault} + * labeled alternative in {@link SqlBaseParser#primaryExpression}. + * @param ctx the parse tree + */ + void enterConstantDefault(SqlBaseParser.ConstantDefaultContext ctx); + + /** + * Exit a parse tree produced by the {@code constantDefault} + * labeled alternative in {@link SqlBaseParser#primaryExpression}. + * @param ctx the parse tree + */ + void exitConstantDefault(SqlBaseParser.ConstantDefaultContext ctx); + + /** + * Enter a parse tree produced by the {@code extract} + * labeled alternative in {@link SqlBaseParser#primaryExpression}. + * @param ctx the parse tree + */ + void enterExtract(SqlBaseParser.ExtractContext ctx); + + /** + * Exit a parse tree produced by the {@code extract} + * labeled alternative in {@link SqlBaseParser#primaryExpression}. + * @param ctx the parse tree + */ + void exitExtract(SqlBaseParser.ExtractContext ctx); + + /** + * Enter a parse tree produced by the {@code parenthesizedExpression} + * labeled alternative in {@link SqlBaseParser#primaryExpression}. + * @param ctx the parse tree + */ + void enterParenthesizedExpression(SqlBaseParser.ParenthesizedExpressionContext ctx); + + /** + * Exit a parse tree produced by the {@code parenthesizedExpression} + * labeled alternative in {@link SqlBaseParser#primaryExpression}. + * @param ctx the parse tree + */ + void exitParenthesizedExpression(SqlBaseParser.ParenthesizedExpressionContext ctx); + + /** + * Enter a parse tree produced by the {@code star} + * labeled alternative in {@link SqlBaseParser#primaryExpression}. + * @param ctx the parse tree + */ + void enterStar(SqlBaseParser.StarContext ctx); + + /** + * Exit a parse tree produced by the {@code star} + * labeled alternative in {@link SqlBaseParser#primaryExpression}. + * @param ctx the parse tree + */ + void exitStar(SqlBaseParser.StarContext ctx); + + /** + * Enter a parse tree produced by the {@code castOperatorExpression} + * labeled alternative in {@link SqlBaseParser#primaryExpression}. + * @param ctx the parse tree + */ + void enterCastOperatorExpression(SqlBaseParser.CastOperatorExpressionContext ctx); + + /** + * Exit a parse tree produced by the {@code castOperatorExpression} + * labeled alternative in {@link SqlBaseParser#primaryExpression}. + * @param ctx the parse tree + */ + void exitCastOperatorExpression(SqlBaseParser.CastOperatorExpressionContext ctx); + + /** + * Enter a parse tree produced by the {@code function} + * labeled alternative in {@link SqlBaseParser#primaryExpression}. + * @param ctx the parse tree + */ + void enterFunction(SqlBaseParser.FunctionContext ctx); + + /** + * Exit a parse tree produced by the {@code function} + * labeled alternative in {@link SqlBaseParser#primaryExpression}. + * @param ctx the parse tree + */ + void exitFunction(SqlBaseParser.FunctionContext ctx); + + /** + * Enter a parse tree produced by the {@code currentDateTimeFunction} + * labeled alternative in {@link SqlBaseParser#primaryExpression}. + * @param ctx the parse tree + */ + void enterCurrentDateTimeFunction(SqlBaseParser.CurrentDateTimeFunctionContext ctx); + + /** + * Exit a parse tree produced by the {@code currentDateTimeFunction} + * labeled alternative in {@link SqlBaseParser#primaryExpression}. + * @param ctx the parse tree + */ + void exitCurrentDateTimeFunction(SqlBaseParser.CurrentDateTimeFunctionContext ctx); + + /** + * Enter a parse tree produced by the {@code subqueryExpression} + * labeled alternative in {@link SqlBaseParser#primaryExpression}. + * @param ctx the parse tree + */ + void enterSubqueryExpression(SqlBaseParser.SubqueryExpressionContext ctx); + + /** + * Exit a parse tree produced by the {@code subqueryExpression} + * labeled alternative in {@link SqlBaseParser#primaryExpression}. + * @param ctx the parse tree + */ + void exitSubqueryExpression(SqlBaseParser.SubqueryExpressionContext ctx); + + /** + * Enter a parse tree produced by the {@code case} + * labeled alternative in {@link SqlBaseParser#primaryExpression}. + * @param ctx the parse tree + */ + void enterCase(SqlBaseParser.CaseContext ctx); + + /** + * Exit a parse tree produced by the {@code case} + * labeled alternative in {@link SqlBaseParser#primaryExpression}. + * @param ctx the parse tree + */ + void exitCase(SqlBaseParser.CaseContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#builtinDateTimeFunction}. + * @param ctx the parse tree + */ + void enterBuiltinDateTimeFunction(SqlBaseParser.BuiltinDateTimeFunctionContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#builtinDateTimeFunction}. + * @param ctx the parse tree + */ + void exitBuiltinDateTimeFunction(SqlBaseParser.BuiltinDateTimeFunctionContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#castExpression}. + * @param ctx the parse tree + */ + void enterCastExpression(SqlBaseParser.CastExpressionContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#castExpression}. + * @param ctx the parse tree + */ + void exitCastExpression(SqlBaseParser.CastExpressionContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#castTemplate}. + * @param ctx the parse tree + */ + void enterCastTemplate(SqlBaseParser.CastTemplateContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#castTemplate}. + * @param ctx the parse tree + */ + void exitCastTemplate(SqlBaseParser.CastTemplateContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#convertTemplate}. + * @param ctx the parse tree + */ + void enterConvertTemplate(SqlBaseParser.ConvertTemplateContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#convertTemplate}. + * @param ctx the parse tree + */ + void exitConvertTemplate(SqlBaseParser.ConvertTemplateContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#extractExpression}. + * @param ctx the parse tree + */ + void enterExtractExpression(SqlBaseParser.ExtractExpressionContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#extractExpression}. + * @param ctx the parse tree + */ + void exitExtractExpression(SqlBaseParser.ExtractExpressionContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#extractTemplate}. + * @param ctx the parse tree + */ + void enterExtractTemplate(SqlBaseParser.ExtractTemplateContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#extractTemplate}. + * @param ctx the parse tree + */ + void exitExtractTemplate(SqlBaseParser.ExtractTemplateContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#functionExpression}. + * @param ctx the parse tree + */ + void enterFunctionExpression(SqlBaseParser.FunctionExpressionContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#functionExpression}. + * @param ctx the parse tree + */ + void exitFunctionExpression(SqlBaseParser.FunctionExpressionContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#functionTemplate}. + * @param ctx the parse tree + */ + void enterFunctionTemplate(SqlBaseParser.FunctionTemplateContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#functionTemplate}. + * @param ctx the parse tree + */ + void exitFunctionTemplate(SqlBaseParser.FunctionTemplateContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#functionName}. + * @param ctx the parse tree + */ + void enterFunctionName(SqlBaseParser.FunctionNameContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#functionName}. + * @param ctx the parse tree + */ + void exitFunctionName(SqlBaseParser.FunctionNameContext ctx); + + /** + * Enter a parse tree produced by the {@code nullLiteral} + * labeled alternative in {@link SqlBaseParser#constant}. + * @param ctx the parse tree + */ + void enterNullLiteral(SqlBaseParser.NullLiteralContext ctx); + + /** + * Exit a parse tree produced by the {@code nullLiteral} + * labeled alternative in {@link SqlBaseParser#constant}. + * @param ctx the parse tree + */ + void exitNullLiteral(SqlBaseParser.NullLiteralContext ctx); + + /** + * Enter a parse tree produced by the {@code intervalLiteral} + * labeled alternative in {@link SqlBaseParser#constant}. + * @param ctx the parse tree + */ + void enterIntervalLiteral(SqlBaseParser.IntervalLiteralContext ctx); + + /** + * Exit a parse tree produced by the {@code intervalLiteral} + * labeled alternative in {@link SqlBaseParser#constant}. + * @param ctx the parse tree + */ + void exitIntervalLiteral(SqlBaseParser.IntervalLiteralContext ctx); + + /** + * Enter a parse tree produced by the {@code numericLiteral} + * labeled alternative in {@link SqlBaseParser#constant}. + * @param ctx the parse tree + */ + void enterNumericLiteral(SqlBaseParser.NumericLiteralContext ctx); + + /** + * Exit a parse tree produced by the {@code numericLiteral} + * labeled alternative in {@link SqlBaseParser#constant}. + * @param ctx the parse tree + */ + void exitNumericLiteral(SqlBaseParser.NumericLiteralContext ctx); + + /** + * Enter a parse tree produced by the {@code booleanLiteral} + * labeled alternative in {@link SqlBaseParser#constant}. + * @param ctx the parse tree + */ + void enterBooleanLiteral(SqlBaseParser.BooleanLiteralContext ctx); + + /** + * Exit a parse tree produced by the {@code booleanLiteral} + * labeled alternative in {@link SqlBaseParser#constant}. + * @param ctx the parse tree + */ + void exitBooleanLiteral(SqlBaseParser.BooleanLiteralContext ctx); + + /** + * Enter a parse tree produced by the {@code stringLiteral} + * labeled alternative in {@link SqlBaseParser#constant}. + * @param ctx the parse tree + */ + void enterStringLiteral(SqlBaseParser.StringLiteralContext ctx); + + /** + * Exit a parse tree produced by the {@code stringLiteral} + * labeled alternative in {@link SqlBaseParser#constant}. + * @param ctx the parse tree + */ + void exitStringLiteral(SqlBaseParser.StringLiteralContext ctx); + + /** + * Enter a parse tree produced by the {@code paramLiteral} + * labeled alternative in {@link SqlBaseParser#constant}. + * @param ctx the parse tree + */ + void enterParamLiteral(SqlBaseParser.ParamLiteralContext ctx); + + /** + * Exit a parse tree produced by the {@code paramLiteral} + * labeled alternative in {@link SqlBaseParser#constant}. + * @param ctx the parse tree + */ + void exitParamLiteral(SqlBaseParser.ParamLiteralContext ctx); + + /** + * Enter a parse tree produced by the {@code dateEscapedLiteral} + * labeled alternative in {@link SqlBaseParser#constant}. + * @param ctx the parse tree + */ + void enterDateEscapedLiteral(SqlBaseParser.DateEscapedLiteralContext ctx); + + /** + * Exit a parse tree produced by the {@code dateEscapedLiteral} + * labeled alternative in {@link SqlBaseParser#constant}. + * @param ctx the parse tree + */ + void exitDateEscapedLiteral(SqlBaseParser.DateEscapedLiteralContext ctx); + + /** + * Enter a parse tree produced by the {@code timeEscapedLiteral} + * labeled alternative in {@link SqlBaseParser#constant}. + * @param ctx the parse tree + */ + void enterTimeEscapedLiteral(SqlBaseParser.TimeEscapedLiteralContext ctx); + + /** + * Exit a parse tree produced by the {@code timeEscapedLiteral} + * labeled alternative in {@link SqlBaseParser#constant}. + * @param ctx the parse tree + */ + void exitTimeEscapedLiteral(SqlBaseParser.TimeEscapedLiteralContext ctx); + + /** + * Enter a parse tree produced by the {@code timestampEscapedLiteral} + * labeled alternative in {@link SqlBaseParser#constant}. + * @param ctx the parse tree + */ + void enterTimestampEscapedLiteral(SqlBaseParser.TimestampEscapedLiteralContext ctx); + + /** + * Exit a parse tree produced by the {@code timestampEscapedLiteral} + * labeled alternative in {@link SqlBaseParser#constant}. + * @param ctx the parse tree + */ + void exitTimestampEscapedLiteral(SqlBaseParser.TimestampEscapedLiteralContext ctx); + + /** + * Enter a parse tree produced by the {@code guidEscapedLiteral} + * labeled alternative in {@link SqlBaseParser#constant}. + * @param ctx the parse tree + */ + void enterGuidEscapedLiteral(SqlBaseParser.GuidEscapedLiteralContext ctx); + + /** + * Exit a parse tree produced by the {@code guidEscapedLiteral} + * labeled alternative in {@link SqlBaseParser#constant}. + * @param ctx the parse tree + */ + void exitGuidEscapedLiteral(SqlBaseParser.GuidEscapedLiteralContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#comparisonOperator}. + * @param ctx the parse tree + */ + void enterComparisonOperator(SqlBaseParser.ComparisonOperatorContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#comparisonOperator}. + * @param ctx the parse tree + */ + void exitComparisonOperator(SqlBaseParser.ComparisonOperatorContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#booleanValue}. + * @param ctx the parse tree + */ + void enterBooleanValue(SqlBaseParser.BooleanValueContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#booleanValue}. + * @param ctx the parse tree + */ + void exitBooleanValue(SqlBaseParser.BooleanValueContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#interval}. + * @param ctx the parse tree + */ + void enterInterval(SqlBaseParser.IntervalContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#interval}. + * @param ctx the parse tree + */ + void exitInterval(SqlBaseParser.IntervalContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#intervalField}. + * @param ctx the parse tree + */ + void enterIntervalField(SqlBaseParser.IntervalFieldContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#intervalField}. + * @param ctx the parse tree + */ + void exitIntervalField(SqlBaseParser.IntervalFieldContext ctx); + + /** + * Enter a parse tree produced by the {@code primitiveDataType} + * labeled alternative in {@link SqlBaseParser#dataType}. + * @param ctx the parse tree + */ + void enterPrimitiveDataType(SqlBaseParser.PrimitiveDataTypeContext ctx); + + /** + * Exit a parse tree produced by the {@code primitiveDataType} + * labeled alternative in {@link SqlBaseParser#dataType}. + * @param ctx the parse tree + */ + void exitPrimitiveDataType(SqlBaseParser.PrimitiveDataTypeContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#qualifiedName}. + * @param ctx the parse tree + */ + void enterQualifiedName(SqlBaseParser.QualifiedNameContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#qualifiedName}. + * @param ctx the parse tree + */ + void exitQualifiedName(SqlBaseParser.QualifiedNameContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#identifier}. + * @param ctx the parse tree + */ + void enterIdentifier(SqlBaseParser.IdentifierContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#identifier}. + * @param ctx the parse tree + */ + void exitIdentifier(SqlBaseParser.IdentifierContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#tableIdentifier}. + * @param ctx the parse tree + */ + void enterTableIdentifier(SqlBaseParser.TableIdentifierContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#tableIdentifier}. + * @param ctx the parse tree + */ + void exitTableIdentifier(SqlBaseParser.TableIdentifierContext ctx); + + /** + * Enter a parse tree produced by the {@code quotedIdentifier} + * labeled alternative in {@link SqlBaseParser#quoteIdentifier}. + * @param ctx the parse tree + */ + void enterQuotedIdentifier(SqlBaseParser.QuotedIdentifierContext ctx); + + /** + * Exit a parse tree produced by the {@code quotedIdentifier} + * labeled alternative in {@link SqlBaseParser#quoteIdentifier}. + * @param ctx the parse tree + */ + void exitQuotedIdentifier(SqlBaseParser.QuotedIdentifierContext ctx); + + /** + * Enter a parse tree produced by the {@code backQuotedIdentifier} + * labeled alternative in {@link SqlBaseParser#quoteIdentifier}. + * @param ctx the parse tree + */ + void enterBackQuotedIdentifier(SqlBaseParser.BackQuotedIdentifierContext ctx); + + /** + * Exit a parse tree produced by the {@code backQuotedIdentifier} + * labeled alternative in {@link SqlBaseParser#quoteIdentifier}. + * @param ctx the parse tree + */ + void exitBackQuotedIdentifier(SqlBaseParser.BackQuotedIdentifierContext ctx); + + /** + * Enter a parse tree produced by the {@code unquotedIdentifier} + * labeled alternative in {@link SqlBaseParser#unquoteIdentifier}. + * @param ctx the parse tree + */ + void enterUnquotedIdentifier(SqlBaseParser.UnquotedIdentifierContext ctx); + + /** + * Exit a parse tree produced by the {@code unquotedIdentifier} + * labeled alternative in {@link SqlBaseParser#unquoteIdentifier}. + * @param ctx the parse tree + */ + void exitUnquotedIdentifier(SqlBaseParser.UnquotedIdentifierContext ctx); + + /** + * Enter a parse tree produced by the {@code digitIdentifier} + * labeled alternative in {@link SqlBaseParser#unquoteIdentifier}. + * @param ctx the parse tree + */ + void enterDigitIdentifier(SqlBaseParser.DigitIdentifierContext ctx); + + /** + * Exit a parse tree produced by the {@code digitIdentifier} + * labeled alternative in {@link SqlBaseParser#unquoteIdentifier}. + * @param ctx the parse tree + */ + void exitDigitIdentifier(SqlBaseParser.DigitIdentifierContext ctx); + + /** + * Enter a parse tree produced by the {@code decimalLiteral} + * labeled alternative in {@link SqlBaseParser#number}. + * @param ctx the parse tree + */ + void enterDecimalLiteral(SqlBaseParser.DecimalLiteralContext ctx); + + /** + * Exit a parse tree produced by the {@code decimalLiteral} + * labeled alternative in {@link SqlBaseParser#number}. + * @param ctx the parse tree + */ + void exitDecimalLiteral(SqlBaseParser.DecimalLiteralContext ctx); + + /** + * Enter a parse tree produced by the {@code integerLiteral} + * labeled alternative in {@link SqlBaseParser#number}. + * @param ctx the parse tree + */ + void enterIntegerLiteral(SqlBaseParser.IntegerLiteralContext ctx); + + /** + * Exit a parse tree produced by the {@code integerLiteral} + * labeled alternative in {@link SqlBaseParser#number}. + * @param ctx the parse tree + */ + void exitIntegerLiteral(SqlBaseParser.IntegerLiteralContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#string}. + * @param ctx the parse tree + */ + void enterString(SqlBaseParser.StringContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#string}. + * @param ctx the parse tree + */ + void exitString(SqlBaseParser.StringContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#whenClause}. + * @param ctx the parse tree + */ + void enterWhenClause(SqlBaseParser.WhenClauseContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#whenClause}. + * @param ctx the parse tree + */ + void exitWhenClause(SqlBaseParser.WhenClauseContext ctx); + + /** + * Enter a parse tree produced by {@link SqlBaseParser#nonReserved}. + * @param ctx the parse tree + */ + void enterNonReserved(SqlBaseParser.NonReservedContext ctx); + + /** + * Exit a parse tree produced by {@link SqlBaseParser#nonReserved}. + * @param ctx the parse tree + */ + void exitNonReserved(SqlBaseParser.NonReservedContext ctx); } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/SqlBaseParser.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/SqlBaseParser.java index 04f0b7a987d76..dd4fbcdd0e7ab 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/SqlBaseParser.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/SqlBaseParser.java @@ -1,7578 +1,9351 @@ // ANTLR GENERATED CODE: DO NOT EDIT package org.elasticsearch.xpack.sql.parser; + +import org.antlr.v4.runtime.*; import org.antlr.v4.runtime.atn.*; import org.antlr.v4.runtime.dfa.DFA; -import org.antlr.v4.runtime.*; import org.antlr.v4.runtime.misc.*; import org.antlr.v4.runtime.tree.*; + import java.util.List; -import java.util.Iterator; -import java.util.ArrayList; -@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"}) +@SuppressWarnings({ "all", "warnings", "unchecked", "unused", "cast" }) class SqlBaseParser extends Parser { - static { RuntimeMetaData.checkVersion("4.9.2", RuntimeMetaData.VERSION); } - - protected static final DFA[] _decisionToDFA; - protected static final PredictionContextCache _sharedContextCache = - new PredictionContextCache(); - public static final int - T__0=1, T__1=2, T__2=3, T__3=4, ALL=5, ANALYZE=6, ANALYZED=7, AND=8, ANY=9, - AS=10, ASC=11, BETWEEN=12, BY=13, CASE=14, CAST=15, CATALOG=16, CATALOGS=17, - COLUMNS=18, CONVERT=19, CURRENT_DATE=20, CURRENT_TIME=21, CURRENT_TIMESTAMP=22, - DAY=23, DAYS=24, DEBUG=25, DESC=26, DESCRIBE=27, DISTINCT=28, ELSE=29, - END=30, ESCAPE=31, EXECUTABLE=32, EXISTS=33, EXPLAIN=34, EXTRACT=35, FALSE=36, - FIRST=37, FOR=38, FORMAT=39, FROM=40, FROZEN=41, FULL=42, FUNCTIONS=43, - GRAPHVIZ=44, GROUP=45, HAVING=46, HOUR=47, HOURS=48, IN=49, INCLUDE=50, - INNER=51, INTERVAL=52, IS=53, JOIN=54, LAST=55, LEFT=56, LIKE=57, LIMIT=58, - MAPPED=59, MATCH=60, MINUTE=61, MINUTES=62, MONTH=63, MONTHS=64, NATURAL=65, - NOT=66, NULL=67, NULLS=68, ON=69, OPTIMIZED=70, OR=71, ORDER=72, OUTER=73, - PARSED=74, PHYSICAL=75, PIVOT=76, PLAN=77, RIGHT=78, RLIKE=79, QUERY=80, - SCHEMAS=81, SECOND=82, SECONDS=83, SELECT=84, SHOW=85, SYS=86, TABLE=87, - TABLES=88, TEXT=89, THEN=90, TRUE=91, TO=92, TOP=93, TYPE=94, TYPES=95, - USING=96, VERIFY=97, WHEN=98, WHERE=99, WITH=100, YEAR=101, YEARS=102, - ESCAPE_ESC=103, FUNCTION_ESC=104, LIMIT_ESC=105, DATE_ESC=106, TIME_ESC=107, - TIMESTAMP_ESC=108, GUID_ESC=109, ESC_START=110, ESC_END=111, EQ=112, NULLEQ=113, - NEQ=114, LT=115, LTE=116, GT=117, GTE=118, PLUS=119, MINUS=120, ASTERISK=121, - SLASH=122, PERCENT=123, CAST_OP=124, DOT=125, PARAM=126, STRING=127, INTEGER_VALUE=128, - DECIMAL_VALUE=129, IDENTIFIER=130, DIGIT_IDENTIFIER=131, TABLE_IDENTIFIER=132, - QUOTED_IDENTIFIER=133, BACKQUOTED_IDENTIFIER=134, SIMPLE_COMMENT=135, - BRACKETED_COMMENT=136, WS=137, UNRECOGNIZED=138, DELIMITER=139; - public static final int - RULE_singleStatement = 0, RULE_singleExpression = 1, RULE_statement = 2, - RULE_query = 3, RULE_queryNoWith = 4, RULE_limitClause = 5, RULE_queryTerm = 6, - RULE_orderBy = 7, RULE_querySpecification = 8, RULE_fromClause = 9, RULE_groupBy = 10, - RULE_groupingElement = 11, RULE_groupingExpressions = 12, RULE_namedQuery = 13, - RULE_topClause = 14, RULE_setQuantifier = 15, RULE_selectItems = 16, RULE_selectItem = 17, - RULE_relation = 18, RULE_joinRelation = 19, RULE_joinType = 20, RULE_joinCriteria = 21, - RULE_relationPrimary = 22, RULE_pivotClause = 23, RULE_pivotArgs = 24, - RULE_namedValueExpression = 25, RULE_expression = 26, RULE_booleanExpression = 27, - RULE_matchQueryOptions = 28, RULE_predicated = 29, RULE_predicate = 30, - RULE_likePattern = 31, RULE_pattern = 32, RULE_patternEscape = 33, RULE_valueExpression = 34, - RULE_primaryExpression = 35, RULE_builtinDateTimeFunction = 36, RULE_castExpression = 37, - RULE_castTemplate = 38, RULE_convertTemplate = 39, RULE_extractExpression = 40, - RULE_extractTemplate = 41, RULE_functionExpression = 42, RULE_functionTemplate = 43, - RULE_functionName = 44, RULE_constant = 45, RULE_comparisonOperator = 46, - RULE_booleanValue = 47, RULE_interval = 48, RULE_intervalField = 49, RULE_dataType = 50, - RULE_qualifiedName = 51, RULE_identifier = 52, RULE_tableIdentifier = 53, - RULE_quoteIdentifier = 54, RULE_unquoteIdentifier = 55, RULE_number = 56, - RULE_string = 57, RULE_whenClause = 58, RULE_nonReserved = 59; - private static String[] makeRuleNames() { - return new String[] { - "singleStatement", "singleExpression", "statement", "query", "queryNoWith", - "limitClause", "queryTerm", "orderBy", "querySpecification", "fromClause", - "groupBy", "groupingElement", "groupingExpressions", "namedQuery", "topClause", - "setQuantifier", "selectItems", "selectItem", "relation", "joinRelation", - "joinType", "joinCriteria", "relationPrimary", "pivotClause", "pivotArgs", - "namedValueExpression", "expression", "booleanExpression", "matchQueryOptions", - "predicated", "predicate", "likePattern", "pattern", "patternEscape", - "valueExpression", "primaryExpression", "builtinDateTimeFunction", "castExpression", - "castTemplate", "convertTemplate", "extractExpression", "extractTemplate", - "functionExpression", "functionTemplate", "functionName", "constant", - "comparisonOperator", "booleanValue", "interval", "intervalField", "dataType", - "qualifiedName", "identifier", "tableIdentifier", "quoteIdentifier", - "unquoteIdentifier", "number", "string", "whenClause", "nonReserved" - }; - } - public static final String[] ruleNames = makeRuleNames(); - - private static String[] makeLiteralNames() { - return new String[] { - null, "'('", "')'", "','", "':'", "'ALL'", "'ANALYZE'", "'ANALYZED'", - "'AND'", "'ANY'", "'AS'", "'ASC'", "'BETWEEN'", "'BY'", "'CASE'", "'CAST'", - "'CATALOG'", "'CATALOGS'", "'COLUMNS'", "'CONVERT'", "'CURRENT_DATE'", - "'CURRENT_TIME'", "'CURRENT_TIMESTAMP'", "'DAY'", "'DAYS'", "'DEBUG'", - "'DESC'", "'DESCRIBE'", "'DISTINCT'", "'ELSE'", "'END'", "'ESCAPE'", - "'EXECUTABLE'", "'EXISTS'", "'EXPLAIN'", "'EXTRACT'", "'FALSE'", "'FIRST'", - "'FOR'", "'FORMAT'", "'FROM'", "'FROZEN'", "'FULL'", "'FUNCTIONS'", "'GRAPHVIZ'", - "'GROUP'", "'HAVING'", "'HOUR'", "'HOURS'", "'IN'", "'INCLUDE'", "'INNER'", - "'INTERVAL'", "'IS'", "'JOIN'", "'LAST'", "'LEFT'", "'LIKE'", "'LIMIT'", - "'MAPPED'", "'MATCH'", "'MINUTE'", "'MINUTES'", "'MONTH'", "'MONTHS'", - "'NATURAL'", "'NOT'", "'NULL'", "'NULLS'", "'ON'", "'OPTIMIZED'", "'OR'", - "'ORDER'", "'OUTER'", "'PARSED'", "'PHYSICAL'", "'PIVOT'", "'PLAN'", - "'RIGHT'", "'RLIKE'", "'QUERY'", "'SCHEMAS'", "'SECOND'", "'SECONDS'", - "'SELECT'", "'SHOW'", "'SYS'", "'TABLE'", "'TABLES'", "'TEXT'", "'THEN'", - "'TRUE'", "'TO'", "'TOP'", "'TYPE'", "'TYPES'", "'USING'", "'VERIFY'", - "'WHEN'", "'WHERE'", "'WITH'", "'YEAR'", "'YEARS'", null, null, null, - null, null, null, null, null, "'}'", "'='", "'<=>'", null, "'<'", "'<='", - "'>'", "'>='", "'+'", "'-'", "'*'", "'/'", "'%'", "'::'", "'.'", "'?'" - }; - } - private static final String[] _LITERAL_NAMES = makeLiteralNames(); - private static String[] makeSymbolicNames() { - return new String[] { - null, null, null, null, null, "ALL", "ANALYZE", "ANALYZED", "AND", "ANY", - "AS", "ASC", "BETWEEN", "BY", "CASE", "CAST", "CATALOG", "CATALOGS", - "COLUMNS", "CONVERT", "CURRENT_DATE", "CURRENT_TIME", "CURRENT_TIMESTAMP", - "DAY", "DAYS", "DEBUG", "DESC", "DESCRIBE", "DISTINCT", "ELSE", "END", - "ESCAPE", "EXECUTABLE", "EXISTS", "EXPLAIN", "EXTRACT", "FALSE", "FIRST", - "FOR", "FORMAT", "FROM", "FROZEN", "FULL", "FUNCTIONS", "GRAPHVIZ", "GROUP", - "HAVING", "HOUR", "HOURS", "IN", "INCLUDE", "INNER", "INTERVAL", "IS", - "JOIN", "LAST", "LEFT", "LIKE", "LIMIT", "MAPPED", "MATCH", "MINUTE", - "MINUTES", "MONTH", "MONTHS", "NATURAL", "NOT", "NULL", "NULLS", "ON", - "OPTIMIZED", "OR", "ORDER", "OUTER", "PARSED", "PHYSICAL", "PIVOT", "PLAN", - "RIGHT", "RLIKE", "QUERY", "SCHEMAS", "SECOND", "SECONDS", "SELECT", - "SHOW", "SYS", "TABLE", "TABLES", "TEXT", "THEN", "TRUE", "TO", "TOP", - "TYPE", "TYPES", "USING", "VERIFY", "WHEN", "WHERE", "WITH", "YEAR", - "YEARS", "ESCAPE_ESC", "FUNCTION_ESC", "LIMIT_ESC", "DATE_ESC", "TIME_ESC", - "TIMESTAMP_ESC", "GUID_ESC", "ESC_START", "ESC_END", "EQ", "NULLEQ", - "NEQ", "LT", "LTE", "GT", "GTE", "PLUS", "MINUS", "ASTERISK", "SLASH", - "PERCENT", "CAST_OP", "DOT", "PARAM", "STRING", "INTEGER_VALUE", "DECIMAL_VALUE", - "IDENTIFIER", "DIGIT_IDENTIFIER", "TABLE_IDENTIFIER", "QUOTED_IDENTIFIER", - "BACKQUOTED_IDENTIFIER", "SIMPLE_COMMENT", "BRACKETED_COMMENT", "WS", - "UNRECOGNIZED", "DELIMITER" - }; - } - private static final String[] _SYMBOLIC_NAMES = makeSymbolicNames(); - public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); - - /** - * @deprecated Use {@link #VOCABULARY} instead. - */ - @Deprecated - public static final String[] tokenNames; - static { - tokenNames = new String[_SYMBOLIC_NAMES.length]; - for (int i = 0; i < tokenNames.length; i++) { - tokenNames[i] = VOCABULARY.getLiteralName(i); - if (tokenNames[i] == null) { - tokenNames[i] = VOCABULARY.getSymbolicName(i); - } - - if (tokenNames[i] == null) { - tokenNames[i] = ""; - } - } - } - - @Override - @Deprecated - public String[] getTokenNames() { - return tokenNames; - } - - @Override - - public Vocabulary getVocabulary() { - return VOCABULARY; - } - - @Override - public String getGrammarFileName() { return "SqlBase.g4"; } - - @Override - public String[] getRuleNames() { return ruleNames; } - - @Override - public String getSerializedATN() { return _serializedATN; } - - @Override - public ATN getATN() { return _ATN; } - - public SqlBaseParser(TokenStream input) { - super(input); - _interp = new ParserATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache); - } - - public static class SingleStatementContext extends ParserRuleContext { - public StatementContext statement() { - return getRuleContext(StatementContext.class,0); - } - public TerminalNode EOF() { return getToken(SqlBaseParser.EOF, 0); } - public SingleStatementContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_singleStatement; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterSingleStatement(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitSingleStatement(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitSingleStatement(this); - else return visitor.visitChildren(this); - } - } - - public final SingleStatementContext singleStatement() throws RecognitionException { - SingleStatementContext _localctx = new SingleStatementContext(_ctx, getState()); - enterRule(_localctx, 0, RULE_singleStatement); - try { - enterOuterAlt(_localctx, 1); - { - setState(120); - statement(); - setState(121); - match(EOF); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class SingleExpressionContext extends ParserRuleContext { - public ExpressionContext expression() { - return getRuleContext(ExpressionContext.class,0); - } - public TerminalNode EOF() { return getToken(SqlBaseParser.EOF, 0); } - public SingleExpressionContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_singleExpression; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterSingleExpression(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitSingleExpression(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitSingleExpression(this); - else return visitor.visitChildren(this); - } - } - - public final SingleExpressionContext singleExpression() throws RecognitionException { - SingleExpressionContext _localctx = new SingleExpressionContext(_ctx, getState()); - enterRule(_localctx, 2, RULE_singleExpression); - try { - enterOuterAlt(_localctx, 1); - { - setState(123); - expression(); - setState(124); - match(EOF); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class StatementContext extends ParserRuleContext { - public StatementContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_statement; } - - public StatementContext() { } - public void copyFrom(StatementContext ctx) { - super.copyFrom(ctx); - } - } - public static class ExplainContext extends StatementContext { - public Token type; - public Token format; - public BooleanValueContext verify; - public TerminalNode EXPLAIN() { return getToken(SqlBaseParser.EXPLAIN, 0); } - public StatementContext statement() { - return getRuleContext(StatementContext.class,0); - } - public List PLAN() { return getTokens(SqlBaseParser.PLAN); } - public TerminalNode PLAN(int i) { - return getToken(SqlBaseParser.PLAN, i); - } - public List FORMAT() { return getTokens(SqlBaseParser.FORMAT); } - public TerminalNode FORMAT(int i) { - return getToken(SqlBaseParser.FORMAT, i); - } - public List VERIFY() { return getTokens(SqlBaseParser.VERIFY); } - public TerminalNode VERIFY(int i) { - return getToken(SqlBaseParser.VERIFY, i); - } - public List booleanValue() { - return getRuleContexts(BooleanValueContext.class); - } - public BooleanValueContext booleanValue(int i) { - return getRuleContext(BooleanValueContext.class,i); - } - public List PARSED() { return getTokens(SqlBaseParser.PARSED); } - public TerminalNode PARSED(int i) { - return getToken(SqlBaseParser.PARSED, i); - } - public List ANALYZED() { return getTokens(SqlBaseParser.ANALYZED); } - public TerminalNode ANALYZED(int i) { - return getToken(SqlBaseParser.ANALYZED, i); - } - public List OPTIMIZED() { return getTokens(SqlBaseParser.OPTIMIZED); } - public TerminalNode OPTIMIZED(int i) { - return getToken(SqlBaseParser.OPTIMIZED, i); - } - public List MAPPED() { return getTokens(SqlBaseParser.MAPPED); } - public TerminalNode MAPPED(int i) { - return getToken(SqlBaseParser.MAPPED, i); - } - public List EXECUTABLE() { return getTokens(SqlBaseParser.EXECUTABLE); } - public TerminalNode EXECUTABLE(int i) { - return getToken(SqlBaseParser.EXECUTABLE, i); - } - public List ALL() { return getTokens(SqlBaseParser.ALL); } - public TerminalNode ALL(int i) { - return getToken(SqlBaseParser.ALL, i); - } - public List TEXT() { return getTokens(SqlBaseParser.TEXT); } - public TerminalNode TEXT(int i) { - return getToken(SqlBaseParser.TEXT, i); - } - public List GRAPHVIZ() { return getTokens(SqlBaseParser.GRAPHVIZ); } - public TerminalNode GRAPHVIZ(int i) { - return getToken(SqlBaseParser.GRAPHVIZ, i); - } - public ExplainContext(StatementContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterExplain(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitExplain(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitExplain(this); - else return visitor.visitChildren(this); - } - } - public static class SysColumnsContext extends StatementContext { - public StringContext cluster; - public LikePatternContext tableLike; - public TableIdentifierContext tableIdent; - public LikePatternContext columnPattern; - public TerminalNode SYS() { return getToken(SqlBaseParser.SYS, 0); } - public TerminalNode COLUMNS() { return getToken(SqlBaseParser.COLUMNS, 0); } - public TerminalNode CATALOG() { return getToken(SqlBaseParser.CATALOG, 0); } - public TerminalNode TABLE() { return getToken(SqlBaseParser.TABLE, 0); } - public StringContext string() { - return getRuleContext(StringContext.class,0); - } - public List likePattern() { - return getRuleContexts(LikePatternContext.class); - } - public LikePatternContext likePattern(int i) { - return getRuleContext(LikePatternContext.class,i); - } - public TableIdentifierContext tableIdentifier() { - return getRuleContext(TableIdentifierContext.class,0); - } - public SysColumnsContext(StatementContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterSysColumns(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitSysColumns(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitSysColumns(this); - else return visitor.visitChildren(this); - } - } - public static class SysTypesContext extends StatementContext { - public NumberContext type; - public TerminalNode SYS() { return getToken(SqlBaseParser.SYS, 0); } - public TerminalNode TYPES() { return getToken(SqlBaseParser.TYPES, 0); } - public NumberContext number() { - return getRuleContext(NumberContext.class,0); - } - public TerminalNode PLUS() { return getToken(SqlBaseParser.PLUS, 0); } - public TerminalNode MINUS() { return getToken(SqlBaseParser.MINUS, 0); } - public SysTypesContext(StatementContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterSysTypes(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitSysTypes(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitSysTypes(this); - else return visitor.visitChildren(this); - } - } - public static class DebugContext extends StatementContext { - public Token type; - public Token format; - public TerminalNode DEBUG() { return getToken(SqlBaseParser.DEBUG, 0); } - public StatementContext statement() { - return getRuleContext(StatementContext.class,0); - } - public List PLAN() { return getTokens(SqlBaseParser.PLAN); } - public TerminalNode PLAN(int i) { - return getToken(SqlBaseParser.PLAN, i); - } - public List FORMAT() { return getTokens(SqlBaseParser.FORMAT); } - public TerminalNode FORMAT(int i) { - return getToken(SqlBaseParser.FORMAT, i); - } - public List ANALYZED() { return getTokens(SqlBaseParser.ANALYZED); } - public TerminalNode ANALYZED(int i) { - return getToken(SqlBaseParser.ANALYZED, i); - } - public List OPTIMIZED() { return getTokens(SqlBaseParser.OPTIMIZED); } - public TerminalNode OPTIMIZED(int i) { - return getToken(SqlBaseParser.OPTIMIZED, i); - } - public List TEXT() { return getTokens(SqlBaseParser.TEXT); } - public TerminalNode TEXT(int i) { - return getToken(SqlBaseParser.TEXT, i); - } - public List GRAPHVIZ() { return getTokens(SqlBaseParser.GRAPHVIZ); } - public TerminalNode GRAPHVIZ(int i) { - return getToken(SqlBaseParser.GRAPHVIZ, i); - } - public DebugContext(StatementContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterDebug(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitDebug(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitDebug(this); - else return visitor.visitChildren(this); - } - } - public static class ShowCatalogsContext extends StatementContext { - public TerminalNode SHOW() { return getToken(SqlBaseParser.SHOW, 0); } - public TerminalNode CATALOGS() { return getToken(SqlBaseParser.CATALOGS, 0); } - public ShowCatalogsContext(StatementContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterShowCatalogs(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitShowCatalogs(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitShowCatalogs(this); - else return visitor.visitChildren(this); - } - } - public static class StatementDefaultContext extends StatementContext { - public QueryContext query() { - return getRuleContext(QueryContext.class,0); - } - public StatementDefaultContext(StatementContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterStatementDefault(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitStatementDefault(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitStatementDefault(this); - else return visitor.visitChildren(this); - } - } - public static class SysTablesContext extends StatementContext { - public LikePatternContext clusterLike; - public LikePatternContext tableLike; - public TableIdentifierContext tableIdent; - public TerminalNode SYS() { return getToken(SqlBaseParser.SYS, 0); } - public TerminalNode TABLES() { return getToken(SqlBaseParser.TABLES, 0); } - public TerminalNode CATALOG() { return getToken(SqlBaseParser.CATALOG, 0); } - public TerminalNode TYPE() { return getToken(SqlBaseParser.TYPE, 0); } - public List string() { - return getRuleContexts(StringContext.class); - } - public StringContext string(int i) { - return getRuleContext(StringContext.class,i); - } - public List likePattern() { - return getRuleContexts(LikePatternContext.class); - } - public LikePatternContext likePattern(int i) { - return getRuleContext(LikePatternContext.class,i); - } - public TableIdentifierContext tableIdentifier() { - return getRuleContext(TableIdentifierContext.class,0); - } - public SysTablesContext(StatementContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterSysTables(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitSysTables(this); + static { + RuntimeMetaData.checkVersion("4.9.2", RuntimeMetaData.VERSION); + } + + protected static final DFA[] _decisionToDFA; + protected static final PredictionContextCache _sharedContextCache = new PredictionContextCache(); + public static final int T__0 = 1, T__1 = 2, T__2 = 3, T__3 = 4, ALL = 5, ANALYZE = 6, ANALYZED = 7, AND = 8, ANY = 9, AS = 10, ASC = 11, + BETWEEN = 12, BY = 13, CASE = 14, CAST = 15, CATALOG = 16, CATALOGS = 17, COLUMNS = 18, CONVERT = 19, CURRENT_DATE = 20, + CURRENT_TIME = 21, CURRENT_TIMESTAMP = 22, DAY = 23, DAYS = 24, DEBUG = 25, DESC = 26, DESCRIBE = 27, DISTINCT = 28, ELSE = 29, + END = 30, ESCAPE = 31, EXECUTABLE = 32, EXISTS = 33, EXPLAIN = 34, EXTRACT = 35, FALSE = 36, FIRST = 37, FOR = 38, FORMAT = 39, + FROM = 40, FROZEN = 41, FULL = 42, FUNCTIONS = 43, GRAPHVIZ = 44, GROUP = 45, HAVING = 46, HOUR = 47, HOURS = 48, IN = 49, INCLUDE = + 50, INNER = 51, INTERVAL = 52, IS = 53, JOIN = 54, LAST = 55, LEFT = 56, LIKE = 57, LIMIT = 58, MAPPED = 59, MATCH = 60, + MINUTE = 61, MINUTES = 62, MONTH = 63, MONTHS = 64, NATURAL = 65, NOT = 66, NULL = 67, NULLS = 68, ON = 69, OPTIMIZED = 70, OR = 71, + ORDER = 72, OUTER = 73, PARSED = 74, PHYSICAL = 75, PIVOT = 76, PLAN = 77, RIGHT = 78, RLIKE = 79, QUERY = 80, SCHEMAS = 81, + SECOND = 82, SECONDS = 83, SELECT = 84, SHOW = 85, SYS = 86, TABLE = 87, TABLES = 88, TEXT = 89, THEN = 90, TRUE = 91, TO = 92, + TOP = 93, TYPE = 94, TYPES = 95, USING = 96, VERIFY = 97, WHEN = 98, WHERE = 99, WITH = 100, YEAR = 101, YEARS = 102, ESCAPE_ESC = + 103, FUNCTION_ESC = 104, LIMIT_ESC = 105, DATE_ESC = 106, TIME_ESC = 107, TIMESTAMP_ESC = 108, GUID_ESC = 109, ESC_START = 110, + ESC_END = 111, EQ = 112, NULLEQ = 113, NEQ = 114, LT = 115, LTE = 116, GT = 117, GTE = 118, PLUS = 119, MINUS = 120, ASTERISK = 121, + SLASH = 122, PERCENT = 123, CAST_OP = 124, DOT = 125, PARAM = 126, STRING = 127, INTEGER_VALUE = 128, DECIMAL_VALUE = 129, + IDENTIFIER = 130, DIGIT_IDENTIFIER = 131, TABLE_IDENTIFIER = 132, QUOTED_IDENTIFIER = 133, BACKQUOTED_IDENTIFIER = 134, + SIMPLE_COMMENT = 135, BRACKETED_COMMENT = 136, WS = 137, UNRECOGNIZED = 138, DELIMITER = 139; + public static final int RULE_singleStatement = 0, RULE_singleExpression = 1, RULE_statement = 2, RULE_query = 3, RULE_queryNoWith = 4, + RULE_limitClause = 5, RULE_queryTerm = 6, RULE_orderBy = 7, RULE_querySpecification = 8, RULE_fromClause = 9, RULE_groupBy = 10, + RULE_groupingElement = 11, RULE_groupingExpressions = 12, RULE_namedQuery = 13, RULE_topClause = 14, RULE_setQuantifier = 15, + RULE_selectItems = 16, RULE_selectItem = 17, RULE_relation = 18, RULE_joinRelation = 19, RULE_joinType = 20, RULE_joinCriteria = 21, + RULE_relationPrimary = 22, RULE_pivotClause = 23, RULE_pivotArgs = 24, RULE_namedValueExpression = 25, RULE_expression = 26, + RULE_booleanExpression = 27, RULE_matchQueryOptions = 28, RULE_predicated = 29, RULE_predicate = 30, RULE_likePattern = 31, + RULE_pattern = 32, RULE_patternEscape = 33, RULE_valueExpression = 34, RULE_primaryExpression = 35, RULE_builtinDateTimeFunction = + 36, RULE_castExpression = 37, RULE_castTemplate = 38, RULE_convertTemplate = 39, RULE_extractExpression = 40, + RULE_extractTemplate = 41, RULE_functionExpression = 42, RULE_functionTemplate = 43, RULE_functionName = 44, RULE_constant = 45, + RULE_comparisonOperator = 46, RULE_booleanValue = 47, RULE_interval = 48, RULE_intervalField = 49, RULE_dataType = 50, + RULE_qualifiedName = 51, RULE_identifier = 52, RULE_tableIdentifier = 53, RULE_quoteIdentifier = 54, RULE_unquoteIdentifier = 55, + RULE_number = 56, RULE_string = 57, RULE_whenClause = 58, RULE_nonReserved = 59; + + private static String[] makeRuleNames() { + return new String[] { + "singleStatement", + "singleExpression", + "statement", + "query", + "queryNoWith", + "limitClause", + "queryTerm", + "orderBy", + "querySpecification", + "fromClause", + "groupBy", + "groupingElement", + "groupingExpressions", + "namedQuery", + "topClause", + "setQuantifier", + "selectItems", + "selectItem", + "relation", + "joinRelation", + "joinType", + "joinCriteria", + "relationPrimary", + "pivotClause", + "pivotArgs", + "namedValueExpression", + "expression", + "booleanExpression", + "matchQueryOptions", + "predicated", + "predicate", + "likePattern", + "pattern", + "patternEscape", + "valueExpression", + "primaryExpression", + "builtinDateTimeFunction", + "castExpression", + "castTemplate", + "convertTemplate", + "extractExpression", + "extractTemplate", + "functionExpression", + "functionTemplate", + "functionName", + "constant", + "comparisonOperator", + "booleanValue", + "interval", + "intervalField", + "dataType", + "qualifiedName", + "identifier", + "tableIdentifier", + "quoteIdentifier", + "unquoteIdentifier", + "number", + "string", + "whenClause", + "nonReserved" }; + } + + public static final String[] ruleNames = makeRuleNames(); + + private static String[] makeLiteralNames() { + return new String[] { + null, + "'('", + "')'", + "','", + "':'", + "'ALL'", + "'ANALYZE'", + "'ANALYZED'", + "'AND'", + "'ANY'", + "'AS'", + "'ASC'", + "'BETWEEN'", + "'BY'", + "'CASE'", + "'CAST'", + "'CATALOG'", + "'CATALOGS'", + "'COLUMNS'", + "'CONVERT'", + "'CURRENT_DATE'", + "'CURRENT_TIME'", + "'CURRENT_TIMESTAMP'", + "'DAY'", + "'DAYS'", + "'DEBUG'", + "'DESC'", + "'DESCRIBE'", + "'DISTINCT'", + "'ELSE'", + "'END'", + "'ESCAPE'", + "'EXECUTABLE'", + "'EXISTS'", + "'EXPLAIN'", + "'EXTRACT'", + "'FALSE'", + "'FIRST'", + "'FOR'", + "'FORMAT'", + "'FROM'", + "'FROZEN'", + "'FULL'", + "'FUNCTIONS'", + "'GRAPHVIZ'", + "'GROUP'", + "'HAVING'", + "'HOUR'", + "'HOURS'", + "'IN'", + "'INCLUDE'", + "'INNER'", + "'INTERVAL'", + "'IS'", + "'JOIN'", + "'LAST'", + "'LEFT'", + "'LIKE'", + "'LIMIT'", + "'MAPPED'", + "'MATCH'", + "'MINUTE'", + "'MINUTES'", + "'MONTH'", + "'MONTHS'", + "'NATURAL'", + "'NOT'", + "'NULL'", + "'NULLS'", + "'ON'", + "'OPTIMIZED'", + "'OR'", + "'ORDER'", + "'OUTER'", + "'PARSED'", + "'PHYSICAL'", + "'PIVOT'", + "'PLAN'", + "'RIGHT'", + "'RLIKE'", + "'QUERY'", + "'SCHEMAS'", + "'SECOND'", + "'SECONDS'", + "'SELECT'", + "'SHOW'", + "'SYS'", + "'TABLE'", + "'TABLES'", + "'TEXT'", + "'THEN'", + "'TRUE'", + "'TO'", + "'TOP'", + "'TYPE'", + "'TYPES'", + "'USING'", + "'VERIFY'", + "'WHEN'", + "'WHERE'", + "'WITH'", + "'YEAR'", + "'YEARS'", + null, + null, + null, + null, + null, + null, + null, + null, + "'}'", + "'='", + "'<=>'", + null, + "'<'", + "'<='", + "'>'", + "'>='", + "'+'", + "'-'", + "'*'", + "'/'", + "'%'", + "'::'", + "'.'", + "'?'" }; + } + + private static final String[] _LITERAL_NAMES = makeLiteralNames(); + + private static String[] makeSymbolicNames() { + return new String[] { + null, + null, + null, + null, + null, + "ALL", + "ANALYZE", + "ANALYZED", + "AND", + "ANY", + "AS", + "ASC", + "BETWEEN", + "BY", + "CASE", + "CAST", + "CATALOG", + "CATALOGS", + "COLUMNS", + "CONVERT", + "CURRENT_DATE", + "CURRENT_TIME", + "CURRENT_TIMESTAMP", + "DAY", + "DAYS", + "DEBUG", + "DESC", + "DESCRIBE", + "DISTINCT", + "ELSE", + "END", + "ESCAPE", + "EXECUTABLE", + "EXISTS", + "EXPLAIN", + "EXTRACT", + "FALSE", + "FIRST", + "FOR", + "FORMAT", + "FROM", + "FROZEN", + "FULL", + "FUNCTIONS", + "GRAPHVIZ", + "GROUP", + "HAVING", + "HOUR", + "HOURS", + "IN", + "INCLUDE", + "INNER", + "INTERVAL", + "IS", + "JOIN", + "LAST", + "LEFT", + "LIKE", + "LIMIT", + "MAPPED", + "MATCH", + "MINUTE", + "MINUTES", + "MONTH", + "MONTHS", + "NATURAL", + "NOT", + "NULL", + "NULLS", + "ON", + "OPTIMIZED", + "OR", + "ORDER", + "OUTER", + "PARSED", + "PHYSICAL", + "PIVOT", + "PLAN", + "RIGHT", + "RLIKE", + "QUERY", + "SCHEMAS", + "SECOND", + "SECONDS", + "SELECT", + "SHOW", + "SYS", + "TABLE", + "TABLES", + "TEXT", + "THEN", + "TRUE", + "TO", + "TOP", + "TYPE", + "TYPES", + "USING", + "VERIFY", + "WHEN", + "WHERE", + "WITH", + "YEAR", + "YEARS", + "ESCAPE_ESC", + "FUNCTION_ESC", + "LIMIT_ESC", + "DATE_ESC", + "TIME_ESC", + "TIMESTAMP_ESC", + "GUID_ESC", + "ESC_START", + "ESC_END", + "EQ", + "NULLEQ", + "NEQ", + "LT", + "LTE", + "GT", + "GTE", + "PLUS", + "MINUS", + "ASTERISK", + "SLASH", + "PERCENT", + "CAST_OP", + "DOT", + "PARAM", + "STRING", + "INTEGER_VALUE", + "DECIMAL_VALUE", + "IDENTIFIER", + "DIGIT_IDENTIFIER", + "TABLE_IDENTIFIER", + "QUOTED_IDENTIFIER", + "BACKQUOTED_IDENTIFIER", + "SIMPLE_COMMENT", + "BRACKETED_COMMENT", + "WS", + "UNRECOGNIZED", + "DELIMITER" }; + } + + private static final String[] _SYMBOLIC_NAMES = makeSymbolicNames(); + public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); + + /** + * @deprecated Use {@link #VOCABULARY} instead. + */ + @Deprecated + public static final String[] tokenNames; + static { + tokenNames = new String[_SYMBOLIC_NAMES.length]; + for (int i = 0; i < tokenNames.length; i++) { + tokenNames[i] = VOCABULARY.getLiteralName(i); + if (tokenNames[i] == null) { + tokenNames[i] = VOCABULARY.getSymbolicName(i); + } + + if (tokenNames[i] == null) { + tokenNames[i] = ""; + } + } } + @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitSysTables(this); - else return visitor.visitChildren(this); - } - } - public static class ShowFunctionsContext extends StatementContext { - public TerminalNode SHOW() { return getToken(SqlBaseParser.SHOW, 0); } - public TerminalNode FUNCTIONS() { return getToken(SqlBaseParser.FUNCTIONS, 0); } - public LikePatternContext likePattern() { - return getRuleContext(LikePatternContext.class,0); - } - public ShowFunctionsContext(StatementContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterShowFunctions(this); + @Deprecated + public String[] getTokenNames() { + return tokenNames; } + @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitShowFunctions(this); + + public Vocabulary getVocabulary() { + return VOCABULARY; } + @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitShowFunctions(this); - else return visitor.visitChildren(this); - } - } - public static class ShowTablesContext extends StatementContext { - public LikePatternContext clusterLike; - public StringContext cluster; - public LikePatternContext tableLike; - public TableIdentifierContext tableIdent; - public TerminalNode SHOW() { return getToken(SqlBaseParser.SHOW, 0); } - public TerminalNode TABLES() { return getToken(SqlBaseParser.TABLES, 0); } - public TerminalNode CATALOG() { return getToken(SqlBaseParser.CATALOG, 0); } - public TerminalNode INCLUDE() { return getToken(SqlBaseParser.INCLUDE, 0); } - public TerminalNode FROZEN() { return getToken(SqlBaseParser.FROZEN, 0); } - public List likePattern() { - return getRuleContexts(LikePatternContext.class); - } - public LikePatternContext likePattern(int i) { - return getRuleContext(LikePatternContext.class,i); - } - public TableIdentifierContext tableIdentifier() { - return getRuleContext(TableIdentifierContext.class,0); - } - public StringContext string() { - return getRuleContext(StringContext.class,0); - } - public ShowTablesContext(StatementContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterShowTables(this); + public String getGrammarFileName() { + return "SqlBase.g4"; } + @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitShowTables(this); + public String[] getRuleNames() { + return ruleNames; } + @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitShowTables(this); - else return visitor.visitChildren(this); - } - } - public static class ShowSchemasContext extends StatementContext { - public TerminalNode SHOW() { return getToken(SqlBaseParser.SHOW, 0); } - public TerminalNode SCHEMAS() { return getToken(SqlBaseParser.SCHEMAS, 0); } - public ShowSchemasContext(StatementContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterShowSchemas(this); + public String getSerializedATN() { + return _serializedATN; } + @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitShowSchemas(this); + public ATN getATN() { + return _ATN; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitShowSchemas(this); - else return visitor.visitChildren(this); - } - } - public static class ShowColumnsContext extends StatementContext { - public StringContext cluster; - public LikePatternContext tableLike; - public TableIdentifierContext tableIdent; - public TerminalNode SHOW() { return getToken(SqlBaseParser.SHOW, 0); } - public TerminalNode COLUMNS() { return getToken(SqlBaseParser.COLUMNS, 0); } - public TerminalNode FROM() { return getToken(SqlBaseParser.FROM, 0); } - public TerminalNode IN() { return getToken(SqlBaseParser.IN, 0); } - public TerminalNode CATALOG() { return getToken(SqlBaseParser.CATALOG, 0); } - public TerminalNode INCLUDE() { return getToken(SqlBaseParser.INCLUDE, 0); } - public TerminalNode FROZEN() { return getToken(SqlBaseParser.FROZEN, 0); } - public LikePatternContext likePattern() { - return getRuleContext(LikePatternContext.class,0); - } - public TableIdentifierContext tableIdentifier() { - return getRuleContext(TableIdentifierContext.class,0); - } - public StringContext string() { - return getRuleContext(StringContext.class,0); - } - public TerminalNode DESCRIBE() { return getToken(SqlBaseParser.DESCRIBE, 0); } - public TerminalNode DESC() { return getToken(SqlBaseParser.DESC, 0); } - public ShowColumnsContext(StatementContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterShowColumns(this); + + public SqlBaseParser(TokenStream input) { + super(input); + _interp = new ParserATNSimulator(this, _ATN, _decisionToDFA, _sharedContextCache); } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitShowColumns(this); + + public static class SingleStatementContext extends ParserRuleContext { + public StatementContext statement() { + return getRuleContext(StatementContext.class, 0); + } + + public TerminalNode EOF() { + return getToken(SqlBaseParser.EOF, 0); + } + + public SingleStatementContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_singleStatement; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterSingleStatement(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitSingleStatement(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitSingleStatement(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitShowColumns(this); - else return visitor.visitChildren(this); - } - } - - public final StatementContext statement() throws RecognitionException { - StatementContext _localctx = new StatementContext(_ctx, getState()); - enterRule(_localctx, 4, RULE_statement); - int _la; - try { - setState(256); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,26,_ctx) ) { - case 1: - _localctx = new StatementDefaultContext(_localctx); - enterOuterAlt(_localctx, 1); - { - setState(126); - query(); - } - break; - case 2: - _localctx = new ExplainContext(_localctx); - enterOuterAlt(_localctx, 2); - { - setState(127); - match(EXPLAIN); - setState(141); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,2,_ctx) ) { - case 1: - { - setState(128); - match(T__0); - setState(137); - _errHandler.sync(this); - _la = _input.LA(1); - while (((((_la - 39)) & ~0x3f) == 0 && ((1L << (_la - 39)) & ((1L << (FORMAT - 39)) | (1L << (PLAN - 39)) | (1L << (VERIFY - 39)))) != 0)) { - { - setState(135); - _errHandler.sync(this); - switch (_input.LA(1)) { - case PLAN: - { - setState(129); - match(PLAN); - setState(130); - ((ExplainContext)_localctx).type = _input.LT(1); - _la = _input.LA(1); - if ( !((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << ALL) | (1L << ANALYZED) | (1L << EXECUTABLE) | (1L << MAPPED))) != 0) || _la==OPTIMIZED || _la==PARSED) ) { - ((ExplainContext)_localctx).type = (Token)_errHandler.recoverInline(this); - } - else { - if ( _input.LA(1)==Token.EOF ) matchedEOF = true; - _errHandler.reportMatch(this); - consume(); - } - } - break; - case FORMAT: - { - setState(131); - match(FORMAT); - setState(132); - ((ExplainContext)_localctx).format = _input.LT(1); - _la = _input.LA(1); - if ( !(_la==GRAPHVIZ || _la==TEXT) ) { - ((ExplainContext)_localctx).format = (Token)_errHandler.recoverInline(this); - } - else { - if ( _input.LA(1)==Token.EOF ) matchedEOF = true; - _errHandler.reportMatch(this); - consume(); - } - } - break; - case VERIFY: - { - setState(133); - match(VERIFY); - setState(134); - ((ExplainContext)_localctx).verify = booleanValue(); - } - break; - default: - throw new NoViableAltException(this); - } - } - setState(139); - _errHandler.sync(this); - _la = _input.LA(1); - } - setState(140); - match(T__1); - } - break; - } - setState(143); - statement(); - } - break; - case 3: - _localctx = new DebugContext(_localctx); - enterOuterAlt(_localctx, 3); - { - setState(144); - match(DEBUG); - setState(156); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,5,_ctx) ) { - case 1: - { - setState(145); - match(T__0); - setState(152); - _errHandler.sync(this); - _la = _input.LA(1); - while (_la==FORMAT || _la==PLAN) { - { - setState(150); - _errHandler.sync(this); - switch (_input.LA(1)) { - case PLAN: - { - setState(146); - match(PLAN); - setState(147); - ((DebugContext)_localctx).type = _input.LT(1); - _la = _input.LA(1); - if ( !(_la==ANALYZED || _la==OPTIMIZED) ) { - ((DebugContext)_localctx).type = (Token)_errHandler.recoverInline(this); - } - else { - if ( _input.LA(1)==Token.EOF ) matchedEOF = true; - _errHandler.reportMatch(this); - consume(); - } - } - break; - case FORMAT: - { - setState(148); - match(FORMAT); - setState(149); - ((DebugContext)_localctx).format = _input.LT(1); - _la = _input.LA(1); - if ( !(_la==GRAPHVIZ || _la==TEXT) ) { - ((DebugContext)_localctx).format = (Token)_errHandler.recoverInline(this); - } - else { - if ( _input.LA(1)==Token.EOF ) matchedEOF = true; - _errHandler.reportMatch(this); - consume(); - } - } - break; - default: - throw new NoViableAltException(this); - } - } - setState(154); - _errHandler.sync(this); - _la = _input.LA(1); - } - setState(155); - match(T__1); - } - break; - } - setState(158); - statement(); - } - break; - case 4: - _localctx = new ShowTablesContext(_localctx); - enterOuterAlt(_localctx, 4); - { - setState(159); - match(SHOW); - setState(160); - match(TABLES); - setState(166); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==CATALOG) { - { - setState(161); - match(CATALOG); - setState(164); - _errHandler.sync(this); - switch (_input.LA(1)) { - case LIKE: - { - setState(162); - ((ShowTablesContext)_localctx).clusterLike = likePattern(); - } - break; - case PARAM: - case STRING: - { - setState(163); - ((ShowTablesContext)_localctx).cluster = string(); - } - break; - default: - throw new NoViableAltException(this); - } - } - } - - setState(170); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==INCLUDE) { - { - setState(168); - match(INCLUDE); - setState(169); - match(FROZEN); - } - } - - setState(174); - _errHandler.sync(this); - switch (_input.LA(1)) { - case LIKE: - { - setState(172); - ((ShowTablesContext)_localctx).tableLike = likePattern(); - } - break; - case ANALYZE: - case ANALYZED: - case CATALOGS: - case COLUMNS: - case CURRENT_DATE: - case CURRENT_TIME: - case CURRENT_TIMESTAMP: - case DAY: - case DEBUG: - case EXECUTABLE: - case EXPLAIN: - case FIRST: - case FORMAT: - case FULL: - case FUNCTIONS: - case GRAPHVIZ: - case HOUR: - case INTERVAL: - case LAST: - case LIMIT: - case MAPPED: - case MINUTE: - case MONTH: - case OPTIMIZED: - case PARSED: - case PHYSICAL: - case PIVOT: - case PLAN: - case RLIKE: - case QUERY: - case SCHEMAS: - case SECOND: - case SHOW: - case SYS: - case TABLES: - case TEXT: - case TOP: - case TYPE: - case TYPES: - case VERIFY: - case YEAR: - case IDENTIFIER: - case DIGIT_IDENTIFIER: - case TABLE_IDENTIFIER: - case QUOTED_IDENTIFIER: - case BACKQUOTED_IDENTIFIER: - { - setState(173); - ((ShowTablesContext)_localctx).tableIdent = tableIdentifier(); - } - break; - case EOF: - break; - default: - break; - } - } - break; - case 5: - _localctx = new ShowColumnsContext(_localctx); - enterOuterAlt(_localctx, 5); - { - setState(176); - match(SHOW); - setState(177); - match(COLUMNS); - setState(180); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==CATALOG) { - { - setState(178); - match(CATALOG); - setState(179); - ((ShowColumnsContext)_localctx).cluster = string(); - } - } - - setState(184); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==INCLUDE) { - { - setState(182); - match(INCLUDE); - setState(183); - match(FROZEN); - } - } - - setState(186); - _la = _input.LA(1); - if ( !(_la==FROM || _la==IN) ) { - _errHandler.recoverInline(this); - } - else { - if ( _input.LA(1)==Token.EOF ) matchedEOF = true; - _errHandler.reportMatch(this); - consume(); - } - setState(189); - _errHandler.sync(this); - switch (_input.LA(1)) { - case LIKE: - { - setState(187); - ((ShowColumnsContext)_localctx).tableLike = likePattern(); - } - break; - case ANALYZE: - case ANALYZED: - case CATALOGS: - case COLUMNS: - case CURRENT_DATE: - case CURRENT_TIME: - case CURRENT_TIMESTAMP: - case DAY: - case DEBUG: - case EXECUTABLE: - case EXPLAIN: - case FIRST: - case FORMAT: - case FULL: - case FUNCTIONS: - case GRAPHVIZ: - case HOUR: - case INTERVAL: - case LAST: - case LIMIT: - case MAPPED: - case MINUTE: - case MONTH: - case OPTIMIZED: - case PARSED: - case PHYSICAL: - case PIVOT: - case PLAN: - case RLIKE: - case QUERY: - case SCHEMAS: - case SECOND: - case SHOW: - case SYS: - case TABLES: - case TEXT: - case TOP: - case TYPE: - case TYPES: - case VERIFY: - case YEAR: - case IDENTIFIER: - case DIGIT_IDENTIFIER: - case TABLE_IDENTIFIER: - case QUOTED_IDENTIFIER: - case BACKQUOTED_IDENTIFIER: - { - setState(188); - ((ShowColumnsContext)_localctx).tableIdent = tableIdentifier(); - } - break; - default: - throw new NoViableAltException(this); - } - } - break; - case 6: - _localctx = new ShowColumnsContext(_localctx); - enterOuterAlt(_localctx, 6); - { - setState(191); - _la = _input.LA(1); - if ( !(_la==DESC || _la==DESCRIBE) ) { - _errHandler.recoverInline(this); - } - else { - if ( _input.LA(1)==Token.EOF ) matchedEOF = true; - _errHandler.reportMatch(this); - consume(); - } - setState(194); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==CATALOG) { - { - setState(192); - match(CATALOG); - setState(193); - ((ShowColumnsContext)_localctx).cluster = string(); - } - } - - setState(198); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==INCLUDE) { - { - setState(196); - match(INCLUDE); - setState(197); - match(FROZEN); - } - } - - setState(202); - _errHandler.sync(this); - switch (_input.LA(1)) { - case LIKE: - { - setState(200); - ((ShowColumnsContext)_localctx).tableLike = likePattern(); - } - break; - case ANALYZE: - case ANALYZED: - case CATALOGS: - case COLUMNS: - case CURRENT_DATE: - case CURRENT_TIME: - case CURRENT_TIMESTAMP: - case DAY: - case DEBUG: - case EXECUTABLE: - case EXPLAIN: - case FIRST: - case FORMAT: - case FULL: - case FUNCTIONS: - case GRAPHVIZ: - case HOUR: - case INTERVAL: - case LAST: - case LIMIT: - case MAPPED: - case MINUTE: - case MONTH: - case OPTIMIZED: - case PARSED: - case PHYSICAL: - case PIVOT: - case PLAN: - case RLIKE: - case QUERY: - case SCHEMAS: - case SECOND: - case SHOW: - case SYS: - case TABLES: - case TEXT: - case TOP: - case TYPE: - case TYPES: - case VERIFY: - case YEAR: - case IDENTIFIER: - case DIGIT_IDENTIFIER: - case TABLE_IDENTIFIER: - case QUOTED_IDENTIFIER: - case BACKQUOTED_IDENTIFIER: - { - setState(201); - ((ShowColumnsContext)_localctx).tableIdent = tableIdentifier(); - } - break; - default: - throw new NoViableAltException(this); - } - } - break; - case 7: - _localctx = new ShowFunctionsContext(_localctx); - enterOuterAlt(_localctx, 7); - { - setState(204); - match(SHOW); - setState(205); - match(FUNCTIONS); - setState(207); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==LIKE) { - { - setState(206); - likePattern(); - } - } - - } - break; - case 8: - _localctx = new ShowSchemasContext(_localctx); - enterOuterAlt(_localctx, 8); - { - setState(209); - match(SHOW); - setState(210); - match(SCHEMAS); - } - break; - case 9: - _localctx = new ShowCatalogsContext(_localctx); - enterOuterAlt(_localctx, 9); - { - setState(211); - match(SHOW); - setState(212); - match(CATALOGS); - } - break; - case 10: - _localctx = new SysTablesContext(_localctx); - enterOuterAlt(_localctx, 10); - { - setState(213); - match(SYS); - setState(214); - match(TABLES); - setState(217); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==CATALOG) { - { - setState(215); - match(CATALOG); - setState(216); - ((SysTablesContext)_localctx).clusterLike = likePattern(); - } - } - - setState(221); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,18,_ctx) ) { - case 1: - { - setState(219); - ((SysTablesContext)_localctx).tableLike = likePattern(); - } - break; - case 2: - { - setState(220); - ((SysTablesContext)_localctx).tableIdent = tableIdentifier(); - } - break; - } - setState(232); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==TYPE) { - { - setState(223); - match(TYPE); - setState(224); - string(); - setState(229); - _errHandler.sync(this); - _la = _input.LA(1); - while (_la==T__2) { - { - { - setState(225); - match(T__2); - setState(226); - string(); - } - } - setState(231); - _errHandler.sync(this); - _la = _input.LA(1); - } - } - } - - } - break; - case 11: - _localctx = new SysColumnsContext(_localctx); - enterOuterAlt(_localctx, 11); - { - setState(234); - match(SYS); - setState(235); - match(COLUMNS); - setState(238); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==CATALOG) { - { - setState(236); - match(CATALOG); - setState(237); - ((SysColumnsContext)_localctx).cluster = string(); - } - } - - setState(243); - _errHandler.sync(this); - switch (_input.LA(1)) { - case TABLE: - { - setState(240); - match(TABLE); - setState(241); - ((SysColumnsContext)_localctx).tableLike = likePattern(); - } - break; - case ANALYZE: - case ANALYZED: - case CATALOGS: - case COLUMNS: - case CURRENT_DATE: - case CURRENT_TIME: - case CURRENT_TIMESTAMP: - case DAY: - case DEBUG: - case EXECUTABLE: - case EXPLAIN: - case FIRST: - case FORMAT: - case FULL: - case FUNCTIONS: - case GRAPHVIZ: - case HOUR: - case INTERVAL: - case LAST: - case LIMIT: - case MAPPED: - case MINUTE: - case MONTH: - case OPTIMIZED: - case PARSED: - case PHYSICAL: - case PIVOT: - case PLAN: - case RLIKE: - case QUERY: - case SCHEMAS: - case SECOND: - case SHOW: - case SYS: - case TABLES: - case TEXT: - case TOP: - case TYPE: - case TYPES: - case VERIFY: - case YEAR: - case IDENTIFIER: - case DIGIT_IDENTIFIER: - case TABLE_IDENTIFIER: - case QUOTED_IDENTIFIER: - case BACKQUOTED_IDENTIFIER: - { - setState(242); - ((SysColumnsContext)_localctx).tableIdent = tableIdentifier(); - } - break; - case EOF: - case LIKE: - break; - default: - break; - } - setState(246); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==LIKE) { - { - setState(245); - ((SysColumnsContext)_localctx).columnPattern = likePattern(); - } - } - - } - break; - case 12: - _localctx = new SysTypesContext(_localctx); - enterOuterAlt(_localctx, 12); - { - setState(248); - match(SYS); - setState(249); - match(TYPES); - setState(254); - _errHandler.sync(this); - _la = _input.LA(1); - if (((((_la - 119)) & ~0x3f) == 0 && ((1L << (_la - 119)) & ((1L << (PLUS - 119)) | (1L << (MINUS - 119)) | (1L << (INTEGER_VALUE - 119)) | (1L << (DECIMAL_VALUE - 119)))) != 0)) { - { - setState(251); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==PLUS || _la==MINUS) { + + public final SingleStatementContext singleStatement() throws RecognitionException { + SingleStatementContext _localctx = new SingleStatementContext(_ctx, getState()); + enterRule(_localctx, 0, RULE_singleStatement); + try { + enterOuterAlt(_localctx, 1); { - setState(250); - _la = _input.LA(1); - if ( !(_la==PLUS || _la==MINUS) ) { - _errHandler.recoverInline(this); + setState(120); + statement(); + setState(121); + match(EOF); } - else { - if ( _input.LA(1)==Token.EOF ) matchedEOF = true; - _errHandler.reportMatch(this); - consume(); - } - } - } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; + } - setState(253); - ((SysTypesContext)_localctx).type = number(); - } + public static class SingleExpressionContext extends ParserRuleContext { + public ExpressionContext expression() { + return getRuleContext(ExpressionContext.class, 0); } + public TerminalNode EOF() { + return getToken(SqlBaseParser.EOF, 0); } - break; - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - public static class QueryContext extends ParserRuleContext { - public QueryNoWithContext queryNoWith() { - return getRuleContext(QueryNoWithContext.class,0); - } - public TerminalNode WITH() { return getToken(SqlBaseParser.WITH, 0); } - public List namedQuery() { - return getRuleContexts(NamedQueryContext.class); - } - public NamedQueryContext namedQuery(int i) { - return getRuleContext(NamedQueryContext.class,i); - } - public QueryContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_query; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterQuery(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitQuery(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitQuery(this); - else return visitor.visitChildren(this); - } - } - - public final QueryContext query() throws RecognitionException { - QueryContext _localctx = new QueryContext(_ctx, getState()); - enterRule(_localctx, 6, RULE_query); - int _la; - try { - enterOuterAlt(_localctx, 1); - { - setState(267); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==WITH) { - { - setState(258); - match(WITH); - setState(259); - namedQuery(); - setState(264); - _errHandler.sync(this); - _la = _input.LA(1); - while (_la==T__2) { - { - { - setState(260); - match(T__2); - setState(261); - namedQuery(); - } - } - setState(266); - _errHandler.sync(this); - _la = _input.LA(1); - } - } - } - - setState(269); - queryNoWith(); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class QueryNoWithContext extends ParserRuleContext { - public QueryTermContext queryTerm() { - return getRuleContext(QueryTermContext.class,0); - } - public TerminalNode ORDER() { return getToken(SqlBaseParser.ORDER, 0); } - public TerminalNode BY() { return getToken(SqlBaseParser.BY, 0); } - public List orderBy() { - return getRuleContexts(OrderByContext.class); - } - public OrderByContext orderBy(int i) { - return getRuleContext(OrderByContext.class,i); - } - public LimitClauseContext limitClause() { - return getRuleContext(LimitClauseContext.class,0); - } - public QueryNoWithContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_queryNoWith; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterQueryNoWith(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitQueryNoWith(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitQueryNoWith(this); - else return visitor.visitChildren(this); - } - } - - public final QueryNoWithContext queryNoWith() throws RecognitionException { - QueryNoWithContext _localctx = new QueryNoWithContext(_ctx, getState()); - enterRule(_localctx, 8, RULE_queryNoWith); - int _la; - try { - enterOuterAlt(_localctx, 1); - { - setState(271); - queryTerm(); - setState(282); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==ORDER) { - { - setState(272); - match(ORDER); - setState(273); - match(BY); - setState(274); - orderBy(); - setState(279); - _errHandler.sync(this); - _la = _input.LA(1); - while (_la==T__2) { - { - { - setState(275); - match(T__2); - setState(276); - orderBy(); - } - } - setState(281); - _errHandler.sync(this); - _la = _input.LA(1); - } - } - } - - setState(285); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==LIMIT || _la==LIMIT_ESC) { - { - setState(284); - limitClause(); - } - } - - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class LimitClauseContext extends ParserRuleContext { - public Token limit; - public TerminalNode LIMIT() { return getToken(SqlBaseParser.LIMIT, 0); } - public TerminalNode INTEGER_VALUE() { return getToken(SqlBaseParser.INTEGER_VALUE, 0); } - public TerminalNode ALL() { return getToken(SqlBaseParser.ALL, 0); } - public TerminalNode LIMIT_ESC() { return getToken(SqlBaseParser.LIMIT_ESC, 0); } - public TerminalNode ESC_END() { return getToken(SqlBaseParser.ESC_END, 0); } - public LimitClauseContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_limitClause; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterLimitClause(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitLimitClause(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitLimitClause(this); - else return visitor.visitChildren(this); - } - } - - public final LimitClauseContext limitClause() throws RecognitionException { - LimitClauseContext _localctx = new LimitClauseContext(_ctx, getState()); - enterRule(_localctx, 10, RULE_limitClause); - int _la; - try { - setState(292); - _errHandler.sync(this); - switch (_input.LA(1)) { - case LIMIT: - enterOuterAlt(_localctx, 1); - { - setState(287); - match(LIMIT); - setState(288); - ((LimitClauseContext)_localctx).limit = _input.LT(1); - _la = _input.LA(1); - if ( !(_la==ALL || _la==INTEGER_VALUE) ) { - ((LimitClauseContext)_localctx).limit = (Token)_errHandler.recoverInline(this); - } - else { - if ( _input.LA(1)==Token.EOF ) matchedEOF = true; - _errHandler.reportMatch(this); - consume(); - } - } - break; - case LIMIT_ESC: - enterOuterAlt(_localctx, 2); - { - setState(289); - match(LIMIT_ESC); - setState(290); - ((LimitClauseContext)_localctx).limit = _input.LT(1); - _la = _input.LA(1); - if ( !(_la==ALL || _la==INTEGER_VALUE) ) { - ((LimitClauseContext)_localctx).limit = (Token)_errHandler.recoverInline(this); - } - else { - if ( _input.LA(1)==Token.EOF ) matchedEOF = true; - _errHandler.reportMatch(this); - consume(); - } - setState(291); - match(ESC_END); - } - break; - default: - throw new NoViableAltException(this); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class QueryTermContext extends ParserRuleContext { - public QueryTermContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_queryTerm; } - - public QueryTermContext() { } - public void copyFrom(QueryTermContext ctx) { - super.copyFrom(ctx); - } - } - public static class SubqueryContext extends QueryTermContext { - public QueryNoWithContext queryNoWith() { - return getRuleContext(QueryNoWithContext.class,0); - } - public SubqueryContext(QueryTermContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterSubquery(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitSubquery(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitSubquery(this); - else return visitor.visitChildren(this); - } - } - public static class QueryPrimaryDefaultContext extends QueryTermContext { - public QuerySpecificationContext querySpecification() { - return getRuleContext(QuerySpecificationContext.class,0); - } - public QueryPrimaryDefaultContext(QueryTermContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterQueryPrimaryDefault(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitQueryPrimaryDefault(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitQueryPrimaryDefault(this); - else return visitor.visitChildren(this); - } - } - - public final QueryTermContext queryTerm() throws RecognitionException { - QueryTermContext _localctx = new QueryTermContext(_ctx, getState()); - enterRule(_localctx, 12, RULE_queryTerm); - try { - setState(299); - _errHandler.sync(this); - switch (_input.LA(1)) { - case SELECT: - _localctx = new QueryPrimaryDefaultContext(_localctx); - enterOuterAlt(_localctx, 1); - { - setState(294); - querySpecification(); - } - break; - case T__0: - _localctx = new SubqueryContext(_localctx); - enterOuterAlt(_localctx, 2); - { - setState(295); - match(T__0); - setState(296); - queryNoWith(); - setState(297); - match(T__1); - } - break; - default: - throw new NoViableAltException(this); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class OrderByContext extends ParserRuleContext { - public Token ordering; - public Token nullOrdering; - public ExpressionContext expression() { - return getRuleContext(ExpressionContext.class,0); - } - public TerminalNode NULLS() { return getToken(SqlBaseParser.NULLS, 0); } - public TerminalNode ASC() { return getToken(SqlBaseParser.ASC, 0); } - public TerminalNode DESC() { return getToken(SqlBaseParser.DESC, 0); } - public TerminalNode FIRST() { return getToken(SqlBaseParser.FIRST, 0); } - public TerminalNode LAST() { return getToken(SqlBaseParser.LAST, 0); } - public OrderByContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_orderBy; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterOrderBy(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitOrderBy(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitOrderBy(this); - else return visitor.visitChildren(this); - } - } - - public final OrderByContext orderBy() throws RecognitionException { - OrderByContext _localctx = new OrderByContext(_ctx, getState()); - enterRule(_localctx, 14, RULE_orderBy); - int _la; - try { - enterOuterAlt(_localctx, 1); - { - setState(301); - expression(); - setState(303); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==ASC || _la==DESC) { - { - setState(302); - ((OrderByContext)_localctx).ordering = _input.LT(1); - _la = _input.LA(1); - if ( !(_la==ASC || _la==DESC) ) { - ((OrderByContext)_localctx).ordering = (Token)_errHandler.recoverInline(this); - } - else { - if ( _input.LA(1)==Token.EOF ) matchedEOF = true; - _errHandler.reportMatch(this); - consume(); - } - } - } - - setState(307); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==NULLS) { - { - setState(305); - match(NULLS); - setState(306); - ((OrderByContext)_localctx).nullOrdering = _input.LT(1); - _la = _input.LA(1); - if ( !(_la==FIRST || _la==LAST) ) { - ((OrderByContext)_localctx).nullOrdering = (Token)_errHandler.recoverInline(this); - } - else { - if ( _input.LA(1)==Token.EOF ) matchedEOF = true; - _errHandler.reportMatch(this); - consume(); - } - } - } - - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class QuerySpecificationContext extends ParserRuleContext { - public BooleanExpressionContext where; - public BooleanExpressionContext having; - public TerminalNode SELECT() { return getToken(SqlBaseParser.SELECT, 0); } - public SelectItemsContext selectItems() { - return getRuleContext(SelectItemsContext.class,0); - } - public TopClauseContext topClause() { - return getRuleContext(TopClauseContext.class,0); - } - public SetQuantifierContext setQuantifier() { - return getRuleContext(SetQuantifierContext.class,0); - } - public FromClauseContext fromClause() { - return getRuleContext(FromClauseContext.class,0); - } - public TerminalNode WHERE() { return getToken(SqlBaseParser.WHERE, 0); } - public TerminalNode GROUP() { return getToken(SqlBaseParser.GROUP, 0); } - public TerminalNode BY() { return getToken(SqlBaseParser.BY, 0); } - public GroupByContext groupBy() { - return getRuleContext(GroupByContext.class,0); - } - public TerminalNode HAVING() { return getToken(SqlBaseParser.HAVING, 0); } - public List booleanExpression() { - return getRuleContexts(BooleanExpressionContext.class); - } - public BooleanExpressionContext booleanExpression(int i) { - return getRuleContext(BooleanExpressionContext.class,i); - } - public QuerySpecificationContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_querySpecification; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterQuerySpecification(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitQuerySpecification(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitQuerySpecification(this); - else return visitor.visitChildren(this); - } - } - - public final QuerySpecificationContext querySpecification() throws RecognitionException { - QuerySpecificationContext _localctx = new QuerySpecificationContext(_ctx, getState()); - enterRule(_localctx, 16, RULE_querySpecification); - int _la; - try { - enterOuterAlt(_localctx, 1); - { - setState(309); - match(SELECT); - setState(311); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,36,_ctx) ) { - case 1: - { - setState(310); - topClause(); - } - break; - } - setState(314); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==ALL || _la==DISTINCT) { - { - setState(313); - setQuantifier(); - } - } - - setState(316); - selectItems(); - setState(318); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==FROM) { - { - setState(317); - fromClause(); - } - } - - setState(322); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==WHERE) { - { - setState(320); - match(WHERE); - setState(321); - ((QuerySpecificationContext)_localctx).where = booleanExpression(0); - } - } - - setState(327); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==GROUP) { - { - setState(324); - match(GROUP); - setState(325); - match(BY); - setState(326); - groupBy(); - } - } - - setState(331); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==HAVING) { - { - setState(329); - match(HAVING); - setState(330); - ((QuerySpecificationContext)_localctx).having = booleanExpression(0); - } - } - - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class FromClauseContext extends ParserRuleContext { - public TerminalNode FROM() { return getToken(SqlBaseParser.FROM, 0); } - public List relation() { - return getRuleContexts(RelationContext.class); - } - public RelationContext relation(int i) { - return getRuleContext(RelationContext.class,i); - } - public PivotClauseContext pivotClause() { - return getRuleContext(PivotClauseContext.class,0); - } - public FromClauseContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_fromClause; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterFromClause(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitFromClause(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitFromClause(this); - else return visitor.visitChildren(this); - } - } - - public final FromClauseContext fromClause() throws RecognitionException { - FromClauseContext _localctx = new FromClauseContext(_ctx, getState()); - enterRule(_localctx, 18, RULE_fromClause); - int _la; - try { - enterOuterAlt(_localctx, 1); - { - setState(333); - match(FROM); - setState(334); - relation(); - setState(339); - _errHandler.sync(this); - _la = _input.LA(1); - while (_la==T__2) { - { - { - setState(335); - match(T__2); - setState(336); - relation(); - } - } - setState(341); - _errHandler.sync(this); - _la = _input.LA(1); - } - setState(343); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==PIVOT) { - { - setState(342); - pivotClause(); - } - } - - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class GroupByContext extends ParserRuleContext { - public List groupingElement() { - return getRuleContexts(GroupingElementContext.class); - } - public GroupingElementContext groupingElement(int i) { - return getRuleContext(GroupingElementContext.class,i); - } - public SetQuantifierContext setQuantifier() { - return getRuleContext(SetQuantifierContext.class,0); - } - public GroupByContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_groupBy; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterGroupBy(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitGroupBy(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitGroupBy(this); - else return visitor.visitChildren(this); - } - } - - public final GroupByContext groupBy() throws RecognitionException { - GroupByContext _localctx = new GroupByContext(_ctx, getState()); - enterRule(_localctx, 20, RULE_groupBy); - int _la; - try { - enterOuterAlt(_localctx, 1); - { - setState(346); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==ALL || _la==DISTINCT) { - { - setState(345); - setQuantifier(); - } - } - - setState(348); - groupingElement(); - setState(353); - _errHandler.sync(this); - _la = _input.LA(1); - while (_la==T__2) { - { - { - setState(349); - match(T__2); - setState(350); - groupingElement(); - } - } - setState(355); - _errHandler.sync(this); - _la = _input.LA(1); - } - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class GroupingElementContext extends ParserRuleContext { - public GroupingElementContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_groupingElement; } - - public GroupingElementContext() { } - public void copyFrom(GroupingElementContext ctx) { - super.copyFrom(ctx); - } - } - public static class SingleGroupingSetContext extends GroupingElementContext { - public GroupingExpressionsContext groupingExpressions() { - return getRuleContext(GroupingExpressionsContext.class,0); - } - public SingleGroupingSetContext(GroupingElementContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterSingleGroupingSet(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitSingleGroupingSet(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitSingleGroupingSet(this); - else return visitor.visitChildren(this); - } - } + public SingleExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } - public final GroupingElementContext groupingElement() throws RecognitionException { - GroupingElementContext _localctx = new GroupingElementContext(_ctx, getState()); - enterRule(_localctx, 22, RULE_groupingElement); - try { - _localctx = new SingleGroupingSetContext(_localctx); - enterOuterAlt(_localctx, 1); - { - setState(356); - groupingExpressions(); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } + @Override + public int getRuleIndex() { + return RULE_singleExpression; + } - public static class GroupingExpressionsContext extends ParserRuleContext { - public List expression() { - return getRuleContexts(ExpressionContext.class); - } - public ExpressionContext expression(int i) { - return getRuleContext(ExpressionContext.class,i); - } - public GroupingExpressionsContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_groupingExpressions; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterGroupingExpressions(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitGroupingExpressions(this); + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterSingleExpression(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitSingleExpression(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitSingleExpression(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitGroupingExpressions(this); - else return visitor.visitChildren(this); - } - } - - public final GroupingExpressionsContext groupingExpressions() throws RecognitionException { - GroupingExpressionsContext _localctx = new GroupingExpressionsContext(_ctx, getState()); - enterRule(_localctx, 24, RULE_groupingExpressions); - int _la; - try { - setState(371); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,48,_ctx) ) { - case 1: - enterOuterAlt(_localctx, 1); - { - setState(358); - match(T__0); - setState(367); - _errHandler.sync(this); - _la = _input.LA(1); - if ((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__0) | (1L << ANALYZE) | (1L << ANALYZED) | (1L << CASE) | (1L << CAST) | (1L << CATALOGS) | (1L << COLUMNS) | (1L << CONVERT) | (1L << CURRENT_DATE) | (1L << CURRENT_TIME) | (1L << CURRENT_TIMESTAMP) | (1L << DAY) | (1L << DEBUG) | (1L << EXECUTABLE) | (1L << EXISTS) | (1L << EXPLAIN) | (1L << EXTRACT) | (1L << FALSE) | (1L << FIRST) | (1L << FORMAT) | (1L << FULL) | (1L << FUNCTIONS) | (1L << GRAPHVIZ) | (1L << HOUR) | (1L << INTERVAL) | (1L << LAST) | (1L << LEFT) | (1L << LIMIT) | (1L << MAPPED) | (1L << MATCH) | (1L << MINUTE) | (1L << MONTH))) != 0) || ((((_la - 66)) & ~0x3f) == 0 && ((1L << (_la - 66)) & ((1L << (NOT - 66)) | (1L << (NULL - 66)) | (1L << (OPTIMIZED - 66)) | (1L << (PARSED - 66)) | (1L << (PHYSICAL - 66)) | (1L << (PIVOT - 66)) | (1L << (PLAN - 66)) | (1L << (RIGHT - 66)) | (1L << (RLIKE - 66)) | (1L << (QUERY - 66)) | (1L << (SCHEMAS - 66)) | (1L << (SECOND - 66)) | (1L << (SHOW - 66)) | (1L << (SYS - 66)) | (1L << (TABLES - 66)) | (1L << (TEXT - 66)) | (1L << (TRUE - 66)) | (1L << (TOP - 66)) | (1L << (TYPE - 66)) | (1L << (TYPES - 66)) | (1L << (VERIFY - 66)) | (1L << (YEAR - 66)) | (1L << (FUNCTION_ESC - 66)) | (1L << (DATE_ESC - 66)) | (1L << (TIME_ESC - 66)) | (1L << (TIMESTAMP_ESC - 66)) | (1L << (GUID_ESC - 66)) | (1L << (PLUS - 66)) | (1L << (MINUS - 66)) | (1L << (ASTERISK - 66)) | (1L << (PARAM - 66)) | (1L << (STRING - 66)) | (1L << (INTEGER_VALUE - 66)) | (1L << (DECIMAL_VALUE - 66)))) != 0) || ((((_la - 130)) & ~0x3f) == 0 && ((1L << (_la - 130)) & ((1L << (IDENTIFIER - 130)) | (1L << (DIGIT_IDENTIFIER - 130)) | (1L << (QUOTED_IDENTIFIER - 130)) | (1L << (BACKQUOTED_IDENTIFIER - 130)))) != 0)) { - { - setState(359); - expression(); - setState(364); - _errHandler.sync(this); - _la = _input.LA(1); - while (_la==T__2) { - { + + public final SingleExpressionContext singleExpression() throws RecognitionException { + SingleExpressionContext _localctx = new SingleExpressionContext(_ctx, getState()); + enterRule(_localctx, 2, RULE_singleExpression); + try { + enterOuterAlt(_localctx, 1); { - setState(360); - match(T__2); - setState(361); - expression(); - } + setState(123); + expression(); + setState(124); + match(EOF); } - setState(366); - _errHandler.sync(this); - _la = _input.LA(1); - } - } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); } + return _localctx; + } - setState(369); - match(T__1); + public static class StatementContext extends ParserRuleContext { + public StatementContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); } - break; - case 2: - enterOuterAlt(_localctx, 2); - { - setState(370); - expression(); + + @Override + public int getRuleIndex() { + return RULE_statement; + } + + public StatementContext() {} + + public void copyFrom(StatementContext ctx) { + super.copyFrom(ctx); } - break; - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); } - return _localctx; - } - public static class NamedQueryContext extends ParserRuleContext { - public IdentifierContext name; - public TerminalNode AS() { return getToken(SqlBaseParser.AS, 0); } - public QueryNoWithContext queryNoWith() { - return getRuleContext(QueryNoWithContext.class,0); + public static class ExplainContext extends StatementContext { + public Token type; + public Token format; + public BooleanValueContext verify; + + public TerminalNode EXPLAIN() { + return getToken(SqlBaseParser.EXPLAIN, 0); + } + + public StatementContext statement() { + return getRuleContext(StatementContext.class, 0); + } + + public List PLAN() { + return getTokens(SqlBaseParser.PLAN); + } + + public TerminalNode PLAN(int i) { + return getToken(SqlBaseParser.PLAN, i); + } + + public List FORMAT() { + return getTokens(SqlBaseParser.FORMAT); + } + + public TerminalNode FORMAT(int i) { + return getToken(SqlBaseParser.FORMAT, i); + } + + public List VERIFY() { + return getTokens(SqlBaseParser.VERIFY); + } + + public TerminalNode VERIFY(int i) { + return getToken(SqlBaseParser.VERIFY, i); + } + + public List booleanValue() { + return getRuleContexts(BooleanValueContext.class); + } + + public BooleanValueContext booleanValue(int i) { + return getRuleContext(BooleanValueContext.class, i); + } + + public List PARSED() { + return getTokens(SqlBaseParser.PARSED); + } + + public TerminalNode PARSED(int i) { + return getToken(SqlBaseParser.PARSED, i); + } + + public List ANALYZED() { + return getTokens(SqlBaseParser.ANALYZED); + } + + public TerminalNode ANALYZED(int i) { + return getToken(SqlBaseParser.ANALYZED, i); + } + + public List OPTIMIZED() { + return getTokens(SqlBaseParser.OPTIMIZED); + } + + public TerminalNode OPTIMIZED(int i) { + return getToken(SqlBaseParser.OPTIMIZED, i); + } + + public List MAPPED() { + return getTokens(SqlBaseParser.MAPPED); + } + + public TerminalNode MAPPED(int i) { + return getToken(SqlBaseParser.MAPPED, i); + } + + public List EXECUTABLE() { + return getTokens(SqlBaseParser.EXECUTABLE); + } + + public TerminalNode EXECUTABLE(int i) { + return getToken(SqlBaseParser.EXECUTABLE, i); + } + + public List ALL() { + return getTokens(SqlBaseParser.ALL); + } + + public TerminalNode ALL(int i) { + return getToken(SqlBaseParser.ALL, i); + } + + public List TEXT() { + return getTokens(SqlBaseParser.TEXT); + } + + public TerminalNode TEXT(int i) { + return getToken(SqlBaseParser.TEXT, i); + } + + public List GRAPHVIZ() { + return getTokens(SqlBaseParser.GRAPHVIZ); + } + + public TerminalNode GRAPHVIZ(int i) { + return getToken(SqlBaseParser.GRAPHVIZ, i); + } + + public ExplainContext(StatementContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterExplain(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitExplain(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitExplain(this); + else return visitor.visitChildren(this); + } } - public IdentifierContext identifier() { - return getRuleContext(IdentifierContext.class,0); - } - public NamedQueryContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_namedQuery; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterNamedQuery(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitNamedQuery(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitNamedQuery(this); - else return visitor.visitChildren(this); - } - } - - public final NamedQueryContext namedQuery() throws RecognitionException { - NamedQueryContext _localctx = new NamedQueryContext(_ctx, getState()); - enterRule(_localctx, 26, RULE_namedQuery); - try { - enterOuterAlt(_localctx, 1); - { - setState(373); - ((NamedQueryContext)_localctx).name = identifier(); - setState(374); - match(AS); - setState(375); - match(T__0); - setState(376); - queryNoWith(); - setState(377); - match(T__1); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class TopClauseContext extends ParserRuleContext { - public Token top; - public TerminalNode TOP() { return getToken(SqlBaseParser.TOP, 0); } - public TerminalNode INTEGER_VALUE() { return getToken(SqlBaseParser.INTEGER_VALUE, 0); } - public TopClauseContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_topClause; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterTopClause(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitTopClause(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitTopClause(this); - else return visitor.visitChildren(this); - } - } - - public final TopClauseContext topClause() throws RecognitionException { - TopClauseContext _localctx = new TopClauseContext(_ctx, getState()); - enterRule(_localctx, 28, RULE_topClause); - try { - enterOuterAlt(_localctx, 1); - { - setState(379); - match(TOP); - setState(380); - ((TopClauseContext)_localctx).top = match(INTEGER_VALUE); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class SetQuantifierContext extends ParserRuleContext { - public TerminalNode DISTINCT() { return getToken(SqlBaseParser.DISTINCT, 0); } - public TerminalNode ALL() { return getToken(SqlBaseParser.ALL, 0); } - public SetQuantifierContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_setQuantifier; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterSetQuantifier(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitSetQuantifier(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitSetQuantifier(this); - else return visitor.visitChildren(this); - } - } - - public final SetQuantifierContext setQuantifier() throws RecognitionException { - SetQuantifierContext _localctx = new SetQuantifierContext(_ctx, getState()); - enterRule(_localctx, 30, RULE_setQuantifier); - int _la; - try { - enterOuterAlt(_localctx, 1); - { - setState(382); - _la = _input.LA(1); - if ( !(_la==ALL || _la==DISTINCT) ) { - _errHandler.recoverInline(this); - } - else { - if ( _input.LA(1)==Token.EOF ) matchedEOF = true; - _errHandler.reportMatch(this); - consume(); - } - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class SelectItemsContext extends ParserRuleContext { - public List selectItem() { - return getRuleContexts(SelectItemContext.class); - } - public SelectItemContext selectItem(int i) { - return getRuleContext(SelectItemContext.class,i); - } - public SelectItemsContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_selectItems; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterSelectItems(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitSelectItems(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitSelectItems(this); - else return visitor.visitChildren(this); - } - } - - public final SelectItemsContext selectItems() throws RecognitionException { - SelectItemsContext _localctx = new SelectItemsContext(_ctx, getState()); - enterRule(_localctx, 32, RULE_selectItems); - int _la; - try { - enterOuterAlt(_localctx, 1); - { - setState(384); - selectItem(); - setState(389); - _errHandler.sync(this); - _la = _input.LA(1); - while (_la==T__2) { - { - { - setState(385); - match(T__2); - setState(386); - selectItem(); - } - } - setState(391); - _errHandler.sync(this); - _la = _input.LA(1); - } - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class SelectItemContext extends ParserRuleContext { - public SelectItemContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_selectItem; } - - public SelectItemContext() { } - public void copyFrom(SelectItemContext ctx) { - super.copyFrom(ctx); - } - } - public static class SelectExpressionContext extends SelectItemContext { - public ExpressionContext expression() { - return getRuleContext(ExpressionContext.class,0); - } - public IdentifierContext identifier() { - return getRuleContext(IdentifierContext.class,0); - } - public TerminalNode AS() { return getToken(SqlBaseParser.AS, 0); } - public SelectExpressionContext(SelectItemContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterSelectExpression(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitSelectExpression(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitSelectExpression(this); - else return visitor.visitChildren(this); - } - } - - public final SelectItemContext selectItem() throws RecognitionException { - SelectItemContext _localctx = new SelectItemContext(_ctx, getState()); - enterRule(_localctx, 34, RULE_selectItem); - int _la; - try { - _localctx = new SelectExpressionContext(_localctx); - enterOuterAlt(_localctx, 1); - { - setState(392); - expression(); - setState(397); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,51,_ctx) ) { - case 1: - { - setState(394); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==AS) { - { - setState(393); - match(AS); - } - } - - setState(396); - identifier(); - } - break; - } - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class RelationContext extends ParserRuleContext { - public RelationPrimaryContext relationPrimary() { - return getRuleContext(RelationPrimaryContext.class,0); - } - public List joinRelation() { - return getRuleContexts(JoinRelationContext.class); - } - public JoinRelationContext joinRelation(int i) { - return getRuleContext(JoinRelationContext.class,i); - } - public RelationContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_relation; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterRelation(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitRelation(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitRelation(this); - else return visitor.visitChildren(this); - } - } - - public final RelationContext relation() throws RecognitionException { - RelationContext _localctx = new RelationContext(_ctx, getState()); - enterRule(_localctx, 36, RULE_relation); - int _la; - try { - enterOuterAlt(_localctx, 1); - { - setState(399); - relationPrimary(); - setState(403); - _errHandler.sync(this); - _la = _input.LA(1); - while (((((_la - 42)) & ~0x3f) == 0 && ((1L << (_la - 42)) & ((1L << (FULL - 42)) | (1L << (INNER - 42)) | (1L << (JOIN - 42)) | (1L << (LEFT - 42)) | (1L << (NATURAL - 42)) | (1L << (RIGHT - 42)))) != 0)) { - { - { - setState(400); - joinRelation(); - } - } - setState(405); - _errHandler.sync(this); - _la = _input.LA(1); - } - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class JoinRelationContext extends ParserRuleContext { - public RelationPrimaryContext right; - public TerminalNode JOIN() { return getToken(SqlBaseParser.JOIN, 0); } - public RelationPrimaryContext relationPrimary() { - return getRuleContext(RelationPrimaryContext.class,0); - } - public JoinTypeContext joinType() { - return getRuleContext(JoinTypeContext.class,0); - } - public JoinCriteriaContext joinCriteria() { - return getRuleContext(JoinCriteriaContext.class,0); - } - public TerminalNode NATURAL() { return getToken(SqlBaseParser.NATURAL, 0); } - public JoinRelationContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_joinRelation; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterJoinRelation(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitJoinRelation(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitJoinRelation(this); - else return visitor.visitChildren(this); - } - } - - public final JoinRelationContext joinRelation() throws RecognitionException { - JoinRelationContext _localctx = new JoinRelationContext(_ctx, getState()); - enterRule(_localctx, 38, RULE_joinRelation); - int _la; - try { - setState(417); - _errHandler.sync(this); - switch (_input.LA(1)) { - case FULL: - case INNER: - case JOIN: - case LEFT: - case RIGHT: - enterOuterAlt(_localctx, 1); - { - { - setState(406); - joinType(); - } - setState(407); - match(JOIN); - setState(408); - ((JoinRelationContext)_localctx).right = relationPrimary(); - setState(410); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==ON || _la==USING) { - { - setState(409); - joinCriteria(); - } - } - - } - break; - case NATURAL: - enterOuterAlt(_localctx, 2); - { - setState(412); - match(NATURAL); - setState(413); - joinType(); - setState(414); - match(JOIN); - setState(415); - ((JoinRelationContext)_localctx).right = relationPrimary(); - } - break; - default: - throw new NoViableAltException(this); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class JoinTypeContext extends ParserRuleContext { - public TerminalNode INNER() { return getToken(SqlBaseParser.INNER, 0); } - public TerminalNode LEFT() { return getToken(SqlBaseParser.LEFT, 0); } - public TerminalNode OUTER() { return getToken(SqlBaseParser.OUTER, 0); } - public TerminalNode RIGHT() { return getToken(SqlBaseParser.RIGHT, 0); } - public TerminalNode FULL() { return getToken(SqlBaseParser.FULL, 0); } - public JoinTypeContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_joinType; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterJoinType(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitJoinType(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitJoinType(this); - else return visitor.visitChildren(this); - } - } - - public final JoinTypeContext joinType() throws RecognitionException { - JoinTypeContext _localctx = new JoinTypeContext(_ctx, getState()); - enterRule(_localctx, 40, RULE_joinType); - int _la; - try { - setState(434); - _errHandler.sync(this); - switch (_input.LA(1)) { - case INNER: - case JOIN: - enterOuterAlt(_localctx, 1); - { - setState(420); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==INNER) { - { - setState(419); - match(INNER); - } - } - - } - break; - case LEFT: - enterOuterAlt(_localctx, 2); - { - setState(422); - match(LEFT); - setState(424); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==OUTER) { - { - setState(423); - match(OUTER); - } - } - - } - break; - case RIGHT: - enterOuterAlt(_localctx, 3); - { - setState(426); - match(RIGHT); - setState(428); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==OUTER) { - { - setState(427); - match(OUTER); - } - } - - } - break; - case FULL: - enterOuterAlt(_localctx, 4); - { - setState(430); - match(FULL); - setState(432); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==OUTER) { - { - setState(431); - match(OUTER); - } - } - - } - break; - default: - throw new NoViableAltException(this); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class JoinCriteriaContext extends ParserRuleContext { - public TerminalNode ON() { return getToken(SqlBaseParser.ON, 0); } - public BooleanExpressionContext booleanExpression() { - return getRuleContext(BooleanExpressionContext.class,0); - } - public TerminalNode USING() { return getToken(SqlBaseParser.USING, 0); } - public List identifier() { - return getRuleContexts(IdentifierContext.class); - } - public IdentifierContext identifier(int i) { - return getRuleContext(IdentifierContext.class,i); - } - public JoinCriteriaContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_joinCriteria; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterJoinCriteria(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitJoinCriteria(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitJoinCriteria(this); - else return visitor.visitChildren(this); - } - } - - public final JoinCriteriaContext joinCriteria() throws RecognitionException { - JoinCriteriaContext _localctx = new JoinCriteriaContext(_ctx, getState()); - enterRule(_localctx, 42, RULE_joinCriteria); - int _la; - try { - setState(450); - _errHandler.sync(this); - switch (_input.LA(1)) { - case ON: - enterOuterAlt(_localctx, 1); - { - setState(436); - match(ON); - setState(437); - booleanExpression(0); - } - break; - case USING: - enterOuterAlt(_localctx, 2); - { - setState(438); - match(USING); - setState(439); - match(T__0); - setState(440); - identifier(); - setState(445); - _errHandler.sync(this); - _la = _input.LA(1); - while (_la==T__2) { - { - { - setState(441); - match(T__2); - setState(442); - identifier(); - } - } - setState(447); - _errHandler.sync(this); - _la = _input.LA(1); - } - setState(448); - match(T__1); - } - break; - default: - throw new NoViableAltException(this); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class RelationPrimaryContext extends ParserRuleContext { - public RelationPrimaryContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_relationPrimary; } - - public RelationPrimaryContext() { } - public void copyFrom(RelationPrimaryContext ctx) { - super.copyFrom(ctx); - } - } - public static class AliasedRelationContext extends RelationPrimaryContext { - public RelationContext relation() { - return getRuleContext(RelationContext.class,0); - } - public QualifiedNameContext qualifiedName() { - return getRuleContext(QualifiedNameContext.class,0); - } - public TerminalNode AS() { return getToken(SqlBaseParser.AS, 0); } - public AliasedRelationContext(RelationPrimaryContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterAliasedRelation(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitAliasedRelation(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitAliasedRelation(this); - else return visitor.visitChildren(this); + + public static class SysColumnsContext extends StatementContext { + public StringContext cluster; + public LikePatternContext tableLike; + public TableIdentifierContext tableIdent; + public LikePatternContext columnPattern; + + public TerminalNode SYS() { + return getToken(SqlBaseParser.SYS, 0); + } + + public TerminalNode COLUMNS() { + return getToken(SqlBaseParser.COLUMNS, 0); + } + + public TerminalNode CATALOG() { + return getToken(SqlBaseParser.CATALOG, 0); + } + + public TerminalNode TABLE() { + return getToken(SqlBaseParser.TABLE, 0); + } + + public StringContext string() { + return getRuleContext(StringContext.class, 0); + } + + public List likePattern() { + return getRuleContexts(LikePatternContext.class); + } + + public LikePatternContext likePattern(int i) { + return getRuleContext(LikePatternContext.class, i); + } + + public TableIdentifierContext tableIdentifier() { + return getRuleContext(TableIdentifierContext.class, 0); + } + + public SysColumnsContext(StatementContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterSysColumns(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitSysColumns(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitSysColumns(this); + else return visitor.visitChildren(this); + } } - } - public static class AliasedQueryContext extends RelationPrimaryContext { - public QueryNoWithContext queryNoWith() { - return getRuleContext(QueryNoWithContext.class,0); + + public static class SysTypesContext extends StatementContext { + public NumberContext type; + + public TerminalNode SYS() { + return getToken(SqlBaseParser.SYS, 0); + } + + public TerminalNode TYPES() { + return getToken(SqlBaseParser.TYPES, 0); + } + + public NumberContext number() { + return getRuleContext(NumberContext.class, 0); + } + + public TerminalNode PLUS() { + return getToken(SqlBaseParser.PLUS, 0); + } + + public TerminalNode MINUS() { + return getToken(SqlBaseParser.MINUS, 0); + } + + public SysTypesContext(StatementContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterSysTypes(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitSysTypes(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitSysTypes(this); + else return visitor.visitChildren(this); + } } - public QualifiedNameContext qualifiedName() { - return getRuleContext(QualifiedNameContext.class,0); + + public static class DebugContext extends StatementContext { + public Token type; + public Token format; + + public TerminalNode DEBUG() { + return getToken(SqlBaseParser.DEBUG, 0); + } + + public StatementContext statement() { + return getRuleContext(StatementContext.class, 0); + } + + public List PLAN() { + return getTokens(SqlBaseParser.PLAN); + } + + public TerminalNode PLAN(int i) { + return getToken(SqlBaseParser.PLAN, i); + } + + public List FORMAT() { + return getTokens(SqlBaseParser.FORMAT); + } + + public TerminalNode FORMAT(int i) { + return getToken(SqlBaseParser.FORMAT, i); + } + + public List ANALYZED() { + return getTokens(SqlBaseParser.ANALYZED); + } + + public TerminalNode ANALYZED(int i) { + return getToken(SqlBaseParser.ANALYZED, i); + } + + public List OPTIMIZED() { + return getTokens(SqlBaseParser.OPTIMIZED); + } + + public TerminalNode OPTIMIZED(int i) { + return getToken(SqlBaseParser.OPTIMIZED, i); + } + + public List TEXT() { + return getTokens(SqlBaseParser.TEXT); + } + + public TerminalNode TEXT(int i) { + return getToken(SqlBaseParser.TEXT, i); + } + + public List GRAPHVIZ() { + return getTokens(SqlBaseParser.GRAPHVIZ); + } + + public TerminalNode GRAPHVIZ(int i) { + return getToken(SqlBaseParser.GRAPHVIZ, i); + } + + public DebugContext(StatementContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterDebug(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitDebug(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitDebug(this); + else return visitor.visitChildren(this); + } } - public TerminalNode AS() { return getToken(SqlBaseParser.AS, 0); } - public AliasedQueryContext(RelationPrimaryContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterAliasedQuery(this); + + public static class ShowCatalogsContext extends StatementContext { + public TerminalNode SHOW() { + return getToken(SqlBaseParser.SHOW, 0); + } + + public TerminalNode CATALOGS() { + return getToken(SqlBaseParser.CATALOGS, 0); + } + + public ShowCatalogsContext(StatementContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterShowCatalogs(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitShowCatalogs(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitShowCatalogs(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitAliasedQuery(this); + + public static class StatementDefaultContext extends StatementContext { + public QueryContext query() { + return getRuleContext(QueryContext.class, 0); + } + + public StatementDefaultContext(StatementContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterStatementDefault(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitStatementDefault(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitStatementDefault(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitAliasedQuery(this); - else return visitor.visitChildren(this); + + public static class SysTablesContext extends StatementContext { + public LikePatternContext clusterLike; + public LikePatternContext tableLike; + public TableIdentifierContext tableIdent; + + public TerminalNode SYS() { + return getToken(SqlBaseParser.SYS, 0); + } + + public TerminalNode TABLES() { + return getToken(SqlBaseParser.TABLES, 0); + } + + public TerminalNode CATALOG() { + return getToken(SqlBaseParser.CATALOG, 0); + } + + public TerminalNode TYPE() { + return getToken(SqlBaseParser.TYPE, 0); + } + + public List string() { + return getRuleContexts(StringContext.class); + } + + public StringContext string(int i) { + return getRuleContext(StringContext.class, i); + } + + public List likePattern() { + return getRuleContexts(LikePatternContext.class); + } + + public LikePatternContext likePattern(int i) { + return getRuleContext(LikePatternContext.class, i); + } + + public TableIdentifierContext tableIdentifier() { + return getRuleContext(TableIdentifierContext.class, 0); + } + + public SysTablesContext(StatementContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterSysTables(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitSysTables(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitSysTables(this); + else return visitor.visitChildren(this); + } } - } - public static class TableNameContext extends RelationPrimaryContext { - public TableIdentifierContext tableIdentifier() { - return getRuleContext(TableIdentifierContext.class,0); + + public static class ShowFunctionsContext extends StatementContext { + public TerminalNode SHOW() { + return getToken(SqlBaseParser.SHOW, 0); + } + + public TerminalNode FUNCTIONS() { + return getToken(SqlBaseParser.FUNCTIONS, 0); + } + + public LikePatternContext likePattern() { + return getRuleContext(LikePatternContext.class, 0); + } + + public ShowFunctionsContext(StatementContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterShowFunctions(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitShowFunctions(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitShowFunctions(this); + else return visitor.visitChildren(this); + } } - public TerminalNode FROZEN() { return getToken(SqlBaseParser.FROZEN, 0); } - public QualifiedNameContext qualifiedName() { - return getRuleContext(QualifiedNameContext.class,0); + + public static class ShowTablesContext extends StatementContext { + public LikePatternContext clusterLike; + public StringContext cluster; + public LikePatternContext tableLike; + public TableIdentifierContext tableIdent; + + public TerminalNode SHOW() { + return getToken(SqlBaseParser.SHOW, 0); + } + + public TerminalNode TABLES() { + return getToken(SqlBaseParser.TABLES, 0); + } + + public TerminalNode CATALOG() { + return getToken(SqlBaseParser.CATALOG, 0); + } + + public TerminalNode INCLUDE() { + return getToken(SqlBaseParser.INCLUDE, 0); + } + + public TerminalNode FROZEN() { + return getToken(SqlBaseParser.FROZEN, 0); + } + + public List likePattern() { + return getRuleContexts(LikePatternContext.class); + } + + public LikePatternContext likePattern(int i) { + return getRuleContext(LikePatternContext.class, i); + } + + public TableIdentifierContext tableIdentifier() { + return getRuleContext(TableIdentifierContext.class, 0); + } + + public StringContext string() { + return getRuleContext(StringContext.class, 0); + } + + public ShowTablesContext(StatementContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterShowTables(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitShowTables(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitShowTables(this); + else return visitor.visitChildren(this); + } } - public TerminalNode AS() { return getToken(SqlBaseParser.AS, 0); } - public TableNameContext(RelationPrimaryContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterTableName(this); + + public static class ShowSchemasContext extends StatementContext { + public TerminalNode SHOW() { + return getToken(SqlBaseParser.SHOW, 0); + } + + public TerminalNode SCHEMAS() { + return getToken(SqlBaseParser.SCHEMAS, 0); + } + + public ShowSchemasContext(StatementContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterShowSchemas(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitShowSchemas(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitShowSchemas(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitTableName(this); + + public static class ShowColumnsContext extends StatementContext { + public StringContext cluster; + public LikePatternContext tableLike; + public TableIdentifierContext tableIdent; + + public TerminalNode SHOW() { + return getToken(SqlBaseParser.SHOW, 0); + } + + public TerminalNode COLUMNS() { + return getToken(SqlBaseParser.COLUMNS, 0); + } + + public TerminalNode FROM() { + return getToken(SqlBaseParser.FROM, 0); + } + + public TerminalNode IN() { + return getToken(SqlBaseParser.IN, 0); + } + + public TerminalNode CATALOG() { + return getToken(SqlBaseParser.CATALOG, 0); + } + + public TerminalNode INCLUDE() { + return getToken(SqlBaseParser.INCLUDE, 0); + } + + public TerminalNode FROZEN() { + return getToken(SqlBaseParser.FROZEN, 0); + } + + public LikePatternContext likePattern() { + return getRuleContext(LikePatternContext.class, 0); + } + + public TableIdentifierContext tableIdentifier() { + return getRuleContext(TableIdentifierContext.class, 0); + } + + public StringContext string() { + return getRuleContext(StringContext.class, 0); + } + + public TerminalNode DESCRIBE() { + return getToken(SqlBaseParser.DESCRIBE, 0); + } + + public TerminalNode DESC() { + return getToken(SqlBaseParser.DESC, 0); + } + + public ShowColumnsContext(StatementContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterShowColumns(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitShowColumns(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitShowColumns(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitTableName(this); - else return visitor.visitChildren(this); - } - } - - public final RelationPrimaryContext relationPrimary() throws RecognitionException { - RelationPrimaryContext _localctx = new RelationPrimaryContext(_ctx, getState()); - enterRule(_localctx, 44, RULE_relationPrimary); - int _la; - try { - setState(480); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,69,_ctx) ) { - case 1: - _localctx = new TableNameContext(_localctx); - enterOuterAlt(_localctx, 1); - { - setState(453); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==FROZEN) { - { - setState(452); - match(FROZEN); - } - } - - setState(455); - tableIdentifier(); - setState(460); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,64,_ctx) ) { - case 1: - { - setState(457); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==AS) { - { - setState(456); - match(AS); - } - } - - setState(459); - qualifiedName(); - } - break; - } - } - break; - case 2: - _localctx = new AliasedQueryContext(_localctx); - enterOuterAlt(_localctx, 2); - { - setState(462); - match(T__0); - setState(463); - queryNoWith(); - setState(464); - match(T__1); - setState(469); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,66,_ctx) ) { - case 1: - { - setState(466); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==AS) { - { - setState(465); - match(AS); - } - } - - setState(468); - qualifiedName(); - } - break; - } - } - break; - case 3: - _localctx = new AliasedRelationContext(_localctx); - enterOuterAlt(_localctx, 3); - { - setState(471); - match(T__0); - setState(472); - relation(); - setState(473); - match(T__1); - setState(478); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,68,_ctx) ) { - case 1: - { - setState(475); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==AS) { - { - setState(474); - match(AS); + + public final StatementContext statement() throws RecognitionException { + StatementContext _localctx = new StatementContext(_ctx, getState()); + enterRule(_localctx, 4, RULE_statement); + int _la; + try { + setState(256); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 26, _ctx)) { + case 1: + _localctx = new StatementDefaultContext(_localctx); + enterOuterAlt(_localctx, 1); { + setState(126); + query(); + } + break; + case 2: + _localctx = new ExplainContext(_localctx); + enterOuterAlt(_localctx, 2); { + setState(127); + match(EXPLAIN); + setState(141); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 2, _ctx)) { + case 1: { + setState(128); + match(T__0); + setState(137); + _errHandler.sync(this); + _la = _input.LA(1); + while (((((_la - 39)) & ~0x3f) == 0 + && ((1L << (_la - 39)) & ((1L << (FORMAT - 39)) | (1L << (PLAN - 39)) | (1L << (VERIFY - 39)))) != 0)) { + { + setState(135); + _errHandler.sync(this); + switch (_input.LA(1)) { + case PLAN: { + setState(129); + match(PLAN); + setState(130); + ((ExplainContext) _localctx).type = _input.LT(1); + _la = _input.LA(1); + if (!((((_la) & ~0x3f) == 0 + && ((1L << _la) & ((1L << ALL) | (1L << ANALYZED) | (1L << EXECUTABLE) | (1L + << MAPPED))) != 0) || _la == OPTIMIZED || _la == PARSED)) { + ((ExplainContext) _localctx).type = (Token) _errHandler.recoverInline(this); + } else { + if (_input.LA(1) == Token.EOF) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + } + break; + case FORMAT: { + setState(131); + match(FORMAT); + setState(132); + ((ExplainContext) _localctx).format = _input.LT(1); + _la = _input.LA(1); + if (!(_la == GRAPHVIZ || _la == TEXT)) { + ((ExplainContext) _localctx).format = (Token) _errHandler.recoverInline(this); + } else { + if (_input.LA(1) == Token.EOF) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + } + break; + case VERIFY: { + setState(133); + match(VERIFY); + setState(134); + ((ExplainContext) _localctx).verify = booleanValue(); + } + break; + default: + throw new NoViableAltException(this); + } + } + setState(139); + _errHandler.sync(this); + _la = _input.LA(1); + } + setState(140); + match(T__1); + } + break; + } + setState(143); + statement(); + } + break; + case 3: + _localctx = new DebugContext(_localctx); + enterOuterAlt(_localctx, 3); { + setState(144); + match(DEBUG); + setState(156); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 5, _ctx)) { + case 1: { + setState(145); + match(T__0); + setState(152); + _errHandler.sync(this); + _la = _input.LA(1); + while (_la == FORMAT || _la == PLAN) { + { + setState(150); + _errHandler.sync(this); + switch (_input.LA(1)) { + case PLAN: { + setState(146); + match(PLAN); + setState(147); + ((DebugContext) _localctx).type = _input.LT(1); + _la = _input.LA(1); + if (!(_la == ANALYZED || _la == OPTIMIZED)) { + ((DebugContext) _localctx).type = (Token) _errHandler.recoverInline(this); + } else { + if (_input.LA(1) == Token.EOF) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + } + break; + case FORMAT: { + setState(148); + match(FORMAT); + setState(149); + ((DebugContext) _localctx).format = _input.LT(1); + _la = _input.LA(1); + if (!(_la == GRAPHVIZ || _la == TEXT)) { + ((DebugContext) _localctx).format = (Token) _errHandler.recoverInline(this); + } else { + if (_input.LA(1) == Token.EOF) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + } + break; + default: + throw new NoViableAltException(this); + } + } + setState(154); + _errHandler.sync(this); + _la = _input.LA(1); + } + setState(155); + match(T__1); + } + break; + } + setState(158); + statement(); + } + break; + case 4: + _localctx = new ShowTablesContext(_localctx); + enterOuterAlt(_localctx, 4); { + setState(159); + match(SHOW); + setState(160); + match(TABLES); + setState(166); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == CATALOG) { + { + setState(161); + match(CATALOG); + setState(164); + _errHandler.sync(this); + switch (_input.LA(1)) { + case LIKE: { + setState(162); + ((ShowTablesContext) _localctx).clusterLike = likePattern(); + } + break; + case PARAM: + case STRING: { + setState(163); + ((ShowTablesContext) _localctx).cluster = string(); + } + break; + default: + throw new NoViableAltException(this); + } + } + } + + setState(170); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == INCLUDE) { + { + setState(168); + match(INCLUDE); + setState(169); + match(FROZEN); + } + } + + setState(174); + _errHandler.sync(this); + switch (_input.LA(1)) { + case LIKE: { + setState(172); + ((ShowTablesContext) _localctx).tableLike = likePattern(); + } + break; + case ANALYZE: + case ANALYZED: + case CATALOGS: + case COLUMNS: + case CURRENT_DATE: + case CURRENT_TIME: + case CURRENT_TIMESTAMP: + case DAY: + case DEBUG: + case EXECUTABLE: + case EXPLAIN: + case FIRST: + case FORMAT: + case FULL: + case FUNCTIONS: + case GRAPHVIZ: + case HOUR: + case INTERVAL: + case LAST: + case LIMIT: + case MAPPED: + case MINUTE: + case MONTH: + case OPTIMIZED: + case PARSED: + case PHYSICAL: + case PIVOT: + case PLAN: + case RLIKE: + case QUERY: + case SCHEMAS: + case SECOND: + case SHOW: + case SYS: + case TABLES: + case TEXT: + case TOP: + case TYPE: + case TYPES: + case VERIFY: + case YEAR: + case IDENTIFIER: + case DIGIT_IDENTIFIER: + case TABLE_IDENTIFIER: + case QUOTED_IDENTIFIER: + case BACKQUOTED_IDENTIFIER: { + setState(173); + ((ShowTablesContext) _localctx).tableIdent = tableIdentifier(); + } + break; + case EOF: + break; + default: + break; + } + } + break; + case 5: + _localctx = new ShowColumnsContext(_localctx); + enterOuterAlt(_localctx, 5); { + setState(176); + match(SHOW); + setState(177); + match(COLUMNS); + setState(180); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == CATALOG) { + { + setState(178); + match(CATALOG); + setState(179); + ((ShowColumnsContext) _localctx).cluster = string(); + } + } + + setState(184); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == INCLUDE) { + { + setState(182); + match(INCLUDE); + setState(183); + match(FROZEN); + } + } + + setState(186); + _la = _input.LA(1); + if (!(_la == FROM || _la == IN)) { + _errHandler.recoverInline(this); + } else { + if (_input.LA(1) == Token.EOF) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + setState(189); + _errHandler.sync(this); + switch (_input.LA(1)) { + case LIKE: { + setState(187); + ((ShowColumnsContext) _localctx).tableLike = likePattern(); + } + break; + case ANALYZE: + case ANALYZED: + case CATALOGS: + case COLUMNS: + case CURRENT_DATE: + case CURRENT_TIME: + case CURRENT_TIMESTAMP: + case DAY: + case DEBUG: + case EXECUTABLE: + case EXPLAIN: + case FIRST: + case FORMAT: + case FULL: + case FUNCTIONS: + case GRAPHVIZ: + case HOUR: + case INTERVAL: + case LAST: + case LIMIT: + case MAPPED: + case MINUTE: + case MONTH: + case OPTIMIZED: + case PARSED: + case PHYSICAL: + case PIVOT: + case PLAN: + case RLIKE: + case QUERY: + case SCHEMAS: + case SECOND: + case SHOW: + case SYS: + case TABLES: + case TEXT: + case TOP: + case TYPE: + case TYPES: + case VERIFY: + case YEAR: + case IDENTIFIER: + case DIGIT_IDENTIFIER: + case TABLE_IDENTIFIER: + case QUOTED_IDENTIFIER: + case BACKQUOTED_IDENTIFIER: { + setState(188); + ((ShowColumnsContext) _localctx).tableIdent = tableIdentifier(); + } + break; + default: + throw new NoViableAltException(this); + } + } + break; + case 6: + _localctx = new ShowColumnsContext(_localctx); + enterOuterAlt(_localctx, 6); { + setState(191); + _la = _input.LA(1); + if (!(_la == DESC || _la == DESCRIBE)) { + _errHandler.recoverInline(this); + } else { + if (_input.LA(1) == Token.EOF) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + setState(194); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == CATALOG) { + { + setState(192); + match(CATALOG); + setState(193); + ((ShowColumnsContext) _localctx).cluster = string(); + } + } + + setState(198); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == INCLUDE) { + { + setState(196); + match(INCLUDE); + setState(197); + match(FROZEN); + } + } + + setState(202); + _errHandler.sync(this); + switch (_input.LA(1)) { + case LIKE: { + setState(200); + ((ShowColumnsContext) _localctx).tableLike = likePattern(); + } + break; + case ANALYZE: + case ANALYZED: + case CATALOGS: + case COLUMNS: + case CURRENT_DATE: + case CURRENT_TIME: + case CURRENT_TIMESTAMP: + case DAY: + case DEBUG: + case EXECUTABLE: + case EXPLAIN: + case FIRST: + case FORMAT: + case FULL: + case FUNCTIONS: + case GRAPHVIZ: + case HOUR: + case INTERVAL: + case LAST: + case LIMIT: + case MAPPED: + case MINUTE: + case MONTH: + case OPTIMIZED: + case PARSED: + case PHYSICAL: + case PIVOT: + case PLAN: + case RLIKE: + case QUERY: + case SCHEMAS: + case SECOND: + case SHOW: + case SYS: + case TABLES: + case TEXT: + case TOP: + case TYPE: + case TYPES: + case VERIFY: + case YEAR: + case IDENTIFIER: + case DIGIT_IDENTIFIER: + case TABLE_IDENTIFIER: + case QUOTED_IDENTIFIER: + case BACKQUOTED_IDENTIFIER: { + setState(201); + ((ShowColumnsContext) _localctx).tableIdent = tableIdentifier(); + } + break; + default: + throw new NoViableAltException(this); + } + } + break; + case 7: + _localctx = new ShowFunctionsContext(_localctx); + enterOuterAlt(_localctx, 7); { + setState(204); + match(SHOW); + setState(205); + match(FUNCTIONS); + setState(207); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == LIKE) { + { + setState(206); + likePattern(); + } + } + + } + break; + case 8: + _localctx = new ShowSchemasContext(_localctx); + enterOuterAlt(_localctx, 8); { + setState(209); + match(SHOW); + setState(210); + match(SCHEMAS); + } + break; + case 9: + _localctx = new ShowCatalogsContext(_localctx); + enterOuterAlt(_localctx, 9); { + setState(211); + match(SHOW); + setState(212); + match(CATALOGS); + } + break; + case 10: + _localctx = new SysTablesContext(_localctx); + enterOuterAlt(_localctx, 10); { + setState(213); + match(SYS); + setState(214); + match(TABLES); + setState(217); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == CATALOG) { + { + setState(215); + match(CATALOG); + setState(216); + ((SysTablesContext) _localctx).clusterLike = likePattern(); + } + } + + setState(221); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 18, _ctx)) { + case 1: { + setState(219); + ((SysTablesContext) _localctx).tableLike = likePattern(); + } + break; + case 2: { + setState(220); + ((SysTablesContext) _localctx).tableIdent = tableIdentifier(); + } + break; + } + setState(232); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == TYPE) { + { + setState(223); + match(TYPE); + setState(224); + string(); + setState(229); + _errHandler.sync(this); + _la = _input.LA(1); + while (_la == T__2) { + { + { + setState(225); + match(T__2); + setState(226); + string(); + } + } + setState(231); + _errHandler.sync(this); + _la = _input.LA(1); + } + } + } + + } + break; + case 11: + _localctx = new SysColumnsContext(_localctx); + enterOuterAlt(_localctx, 11); { + setState(234); + match(SYS); + setState(235); + match(COLUMNS); + setState(238); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == CATALOG) { + { + setState(236); + match(CATALOG); + setState(237); + ((SysColumnsContext) _localctx).cluster = string(); + } + } + + setState(243); + _errHandler.sync(this); + switch (_input.LA(1)) { + case TABLE: { + setState(240); + match(TABLE); + setState(241); + ((SysColumnsContext) _localctx).tableLike = likePattern(); + } + break; + case ANALYZE: + case ANALYZED: + case CATALOGS: + case COLUMNS: + case CURRENT_DATE: + case CURRENT_TIME: + case CURRENT_TIMESTAMP: + case DAY: + case DEBUG: + case EXECUTABLE: + case EXPLAIN: + case FIRST: + case FORMAT: + case FULL: + case FUNCTIONS: + case GRAPHVIZ: + case HOUR: + case INTERVAL: + case LAST: + case LIMIT: + case MAPPED: + case MINUTE: + case MONTH: + case OPTIMIZED: + case PARSED: + case PHYSICAL: + case PIVOT: + case PLAN: + case RLIKE: + case QUERY: + case SCHEMAS: + case SECOND: + case SHOW: + case SYS: + case TABLES: + case TEXT: + case TOP: + case TYPE: + case TYPES: + case VERIFY: + case YEAR: + case IDENTIFIER: + case DIGIT_IDENTIFIER: + case TABLE_IDENTIFIER: + case QUOTED_IDENTIFIER: + case BACKQUOTED_IDENTIFIER: { + setState(242); + ((SysColumnsContext) _localctx).tableIdent = tableIdentifier(); + } + break; + case EOF: + case LIKE: + break; + default: + break; + } + setState(246); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == LIKE) { + { + setState(245); + ((SysColumnsContext) _localctx).columnPattern = likePattern(); + } + } + + } + break; + case 12: + _localctx = new SysTypesContext(_localctx); + enterOuterAlt(_localctx, 12); { + setState(248); + match(SYS); + setState(249); + match(TYPES); + setState(254); + _errHandler.sync(this); + _la = _input.LA(1); + if (((((_la - 119)) & ~0x3f) == 0 + && ((1L << (_la - 119)) & ((1L << (PLUS - 119)) | (1L << (MINUS - 119)) | (1L << (INTEGER_VALUE - 119)) | (1L + << (DECIMAL_VALUE - 119)))) != 0)) { + { + setState(251); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == PLUS || _la == MINUS) { + { + setState(250); + _la = _input.LA(1); + if (!(_la == PLUS || _la == MINUS)) { + _errHandler.recoverInline(this); + } else { + if (_input.LA(1) == Token.EOF) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + } + } + + setState(253); + ((SysTypesContext) _localctx).type = number(); + } + } + + } + break; } - } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; + } + + public static class QueryContext extends ParserRuleContext { + public QueryNoWithContext queryNoWith() { + return getRuleContext(QueryNoWithContext.class, 0); + } + + public TerminalNode WITH() { + return getToken(SqlBaseParser.WITH, 0); + } + + public List namedQuery() { + return getRuleContexts(NamedQueryContext.class); + } + + public NamedQueryContext namedQuery(int i) { + return getRuleContext(NamedQueryContext.class, i); + } + + public QueryContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_query; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterQuery(this); + } - setState(477); - qualifiedName(); - } - break; + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitQuery(this); } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitQuery(this); + else return visitor.visitChildren(this); } - break; - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); } - finally { - exitRule(); - } - return _localctx; - } - public static class PivotClauseContext extends ParserRuleContext { - public PivotArgsContext aggs; - public QualifiedNameContext column; - public PivotArgsContext vals; - public TerminalNode PIVOT() { return getToken(SqlBaseParser.PIVOT, 0); } - public TerminalNode FOR() { return getToken(SqlBaseParser.FOR, 0); } - public TerminalNode IN() { return getToken(SqlBaseParser.IN, 0); } - public List pivotArgs() { - return getRuleContexts(PivotArgsContext.class); - } - public PivotArgsContext pivotArgs(int i) { - return getRuleContext(PivotArgsContext.class,i); - } - public QualifiedNameContext qualifiedName() { - return getRuleContext(QualifiedNameContext.class,0); - } - public PivotClauseContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_pivotClause; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterPivotClause(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitPivotClause(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitPivotClause(this); - else return visitor.visitChildren(this); - } - } - - public final PivotClauseContext pivotClause() throws RecognitionException { - PivotClauseContext _localctx = new PivotClauseContext(_ctx, getState()); - enterRule(_localctx, 46, RULE_pivotClause); - try { - enterOuterAlt(_localctx, 1); - { - setState(482); - match(PIVOT); - setState(483); - match(T__0); - setState(484); - ((PivotClauseContext)_localctx).aggs = pivotArgs(); - setState(485); - match(FOR); - setState(486); - ((PivotClauseContext)_localctx).column = qualifiedName(); - setState(487); - match(IN); - setState(488); - match(T__0); - setState(489); - ((PivotClauseContext)_localctx).vals = pivotArgs(); - setState(490); - match(T__1); - setState(491); - match(T__1); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class PivotArgsContext extends ParserRuleContext { - public List namedValueExpression() { - return getRuleContexts(NamedValueExpressionContext.class); - } - public NamedValueExpressionContext namedValueExpression(int i) { - return getRuleContext(NamedValueExpressionContext.class,i); - } - public PivotArgsContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_pivotArgs; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterPivotArgs(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitPivotArgs(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitPivotArgs(this); - else return visitor.visitChildren(this); - } - } - - public final PivotArgsContext pivotArgs() throws RecognitionException { - PivotArgsContext _localctx = new PivotArgsContext(_ctx, getState()); - enterRule(_localctx, 48, RULE_pivotArgs); - int _la; - try { - enterOuterAlt(_localctx, 1); - { - setState(493); - namedValueExpression(); - setState(498); - _errHandler.sync(this); - _la = _input.LA(1); - while (_la==T__2) { - { - { - setState(494); - match(T__2); - setState(495); - namedValueExpression(); - } - } - setState(500); - _errHandler.sync(this); - _la = _input.LA(1); - } - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class NamedValueExpressionContext extends ParserRuleContext { - public ValueExpressionContext valueExpression() { - return getRuleContext(ValueExpressionContext.class,0); - } - public IdentifierContext identifier() { - return getRuleContext(IdentifierContext.class,0); - } - public TerminalNode AS() { return getToken(SqlBaseParser.AS, 0); } - public NamedValueExpressionContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_namedValueExpression; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterNamedValueExpression(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitNamedValueExpression(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitNamedValueExpression(this); - else return visitor.visitChildren(this); - } - } - - public final NamedValueExpressionContext namedValueExpression() throws RecognitionException { - NamedValueExpressionContext _localctx = new NamedValueExpressionContext(_ctx, getState()); - enterRule(_localctx, 50, RULE_namedValueExpression); - int _la; - try { - enterOuterAlt(_localctx, 1); - { - setState(501); - valueExpression(0); - setState(506); - _errHandler.sync(this); - _la = _input.LA(1); - if ((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << ANALYZE) | (1L << ANALYZED) | (1L << AS) | (1L << CATALOGS) | (1L << COLUMNS) | (1L << CURRENT_DATE) | (1L << CURRENT_TIME) | (1L << CURRENT_TIMESTAMP) | (1L << DAY) | (1L << DEBUG) | (1L << EXECUTABLE) | (1L << EXPLAIN) | (1L << FIRST) | (1L << FORMAT) | (1L << FULL) | (1L << FUNCTIONS) | (1L << GRAPHVIZ) | (1L << HOUR) | (1L << INTERVAL) | (1L << LAST) | (1L << LIMIT) | (1L << MAPPED) | (1L << MINUTE) | (1L << MONTH))) != 0) || ((((_la - 70)) & ~0x3f) == 0 && ((1L << (_la - 70)) & ((1L << (OPTIMIZED - 70)) | (1L << (PARSED - 70)) | (1L << (PHYSICAL - 70)) | (1L << (PIVOT - 70)) | (1L << (PLAN - 70)) | (1L << (RLIKE - 70)) | (1L << (QUERY - 70)) | (1L << (SCHEMAS - 70)) | (1L << (SECOND - 70)) | (1L << (SHOW - 70)) | (1L << (SYS - 70)) | (1L << (TABLES - 70)) | (1L << (TEXT - 70)) | (1L << (TOP - 70)) | (1L << (TYPE - 70)) | (1L << (TYPES - 70)) | (1L << (VERIFY - 70)) | (1L << (YEAR - 70)) | (1L << (IDENTIFIER - 70)) | (1L << (DIGIT_IDENTIFIER - 70)) | (1L << (QUOTED_IDENTIFIER - 70)))) != 0) || _la==BACKQUOTED_IDENTIFIER) { - { - setState(503); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==AS) { - { - setState(502); - match(AS); - } - } - - setState(505); - identifier(); - } - } - - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class ExpressionContext extends ParserRuleContext { - public BooleanExpressionContext booleanExpression() { - return getRuleContext(BooleanExpressionContext.class,0); - } - public ExpressionContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_expression; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterExpression(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitExpression(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitExpression(this); - else return visitor.visitChildren(this); - } - } - - public final ExpressionContext expression() throws RecognitionException { - ExpressionContext _localctx = new ExpressionContext(_ctx, getState()); - enterRule(_localctx, 52, RULE_expression); - try { - enterOuterAlt(_localctx, 1); - { - setState(508); - booleanExpression(0); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class BooleanExpressionContext extends ParserRuleContext { - public BooleanExpressionContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_booleanExpression; } - - public BooleanExpressionContext() { } - public void copyFrom(BooleanExpressionContext ctx) { - super.copyFrom(ctx); - } - } - public static class LogicalNotContext extends BooleanExpressionContext { - public TerminalNode NOT() { return getToken(SqlBaseParser.NOT, 0); } - public BooleanExpressionContext booleanExpression() { - return getRuleContext(BooleanExpressionContext.class,0); - } - public LogicalNotContext(BooleanExpressionContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterLogicalNot(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitLogicalNot(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitLogicalNot(this); - else return visitor.visitChildren(this); - } - } - public static class StringQueryContext extends BooleanExpressionContext { - public StringContext queryString; - public TerminalNode QUERY() { return getToken(SqlBaseParser.QUERY, 0); } - public MatchQueryOptionsContext matchQueryOptions() { - return getRuleContext(MatchQueryOptionsContext.class,0); - } - public StringContext string() { - return getRuleContext(StringContext.class,0); - } - public StringQueryContext(BooleanExpressionContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterStringQuery(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitStringQuery(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitStringQuery(this); - else return visitor.visitChildren(this); - } - } - public static class BooleanDefaultContext extends BooleanExpressionContext { - public PredicatedContext predicated() { - return getRuleContext(PredicatedContext.class,0); - } - public BooleanDefaultContext(BooleanExpressionContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterBooleanDefault(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitBooleanDefault(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitBooleanDefault(this); - else return visitor.visitChildren(this); - } - } - public static class ExistsContext extends BooleanExpressionContext { - public TerminalNode EXISTS() { return getToken(SqlBaseParser.EXISTS, 0); } - public QueryContext query() { - return getRuleContext(QueryContext.class,0); - } - public ExistsContext(BooleanExpressionContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterExists(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitExists(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitExists(this); - else return visitor.visitChildren(this); + public final QueryContext query() throws RecognitionException { + QueryContext _localctx = new QueryContext(_ctx, getState()); + enterRule(_localctx, 6, RULE_query); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(267); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == WITH) { + { + setState(258); + match(WITH); + setState(259); + namedQuery(); + setState(264); + _errHandler.sync(this); + _la = _input.LA(1); + while (_la == T__2) { + { + { + setState(260); + match(T__2); + setState(261); + namedQuery(); + } + } + setState(266); + _errHandler.sync(this); + _la = _input.LA(1); + } + } + } + + setState(269); + queryNoWith(); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - } - public static class MultiMatchQueryContext extends BooleanExpressionContext { - public StringContext multiFields; - public StringContext queryString; - public TerminalNode MATCH() { return getToken(SqlBaseParser.MATCH, 0); } - public MatchQueryOptionsContext matchQueryOptions() { - return getRuleContext(MatchQueryOptionsContext.class,0); + + public static class QueryNoWithContext extends ParserRuleContext { + public QueryTermContext queryTerm() { + return getRuleContext(QueryTermContext.class, 0); + } + + public TerminalNode ORDER() { + return getToken(SqlBaseParser.ORDER, 0); + } + + public TerminalNode BY() { + return getToken(SqlBaseParser.BY, 0); + } + + public List orderBy() { + return getRuleContexts(OrderByContext.class); + } + + public OrderByContext orderBy(int i) { + return getRuleContext(OrderByContext.class, i); + } + + public LimitClauseContext limitClause() { + return getRuleContext(LimitClauseContext.class, 0); + } + + public QueryNoWithContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_queryNoWith; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterQueryNoWith(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitQueryNoWith(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitQueryNoWith(this); + else return visitor.visitChildren(this); + } } - public List string() { - return getRuleContexts(StringContext.class); + + public final QueryNoWithContext queryNoWith() throws RecognitionException { + QueryNoWithContext _localctx = new QueryNoWithContext(_ctx, getState()); + enterRule(_localctx, 8, RULE_queryNoWith); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(271); + queryTerm(); + setState(282); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == ORDER) { + { + setState(272); + match(ORDER); + setState(273); + match(BY); + setState(274); + orderBy(); + setState(279); + _errHandler.sync(this); + _la = _input.LA(1); + while (_la == T__2) { + { + { + setState(275); + match(T__2); + setState(276); + orderBy(); + } + } + setState(281); + _errHandler.sync(this); + _la = _input.LA(1); + } + } + } + + setState(285); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == LIMIT || _la == LIMIT_ESC) { + { + setState(284); + limitClause(); + } + } + + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - public StringContext string(int i) { - return getRuleContext(StringContext.class,i); + + public static class LimitClauseContext extends ParserRuleContext { + public Token limit; + + public TerminalNode LIMIT() { + return getToken(SqlBaseParser.LIMIT, 0); + } + + public TerminalNode INTEGER_VALUE() { + return getToken(SqlBaseParser.INTEGER_VALUE, 0); + } + + public TerminalNode ALL() { + return getToken(SqlBaseParser.ALL, 0); + } + + public TerminalNode LIMIT_ESC() { + return getToken(SqlBaseParser.LIMIT_ESC, 0); + } + + public TerminalNode ESC_END() { + return getToken(SqlBaseParser.ESC_END, 0); + } + + public LimitClauseContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_limitClause; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterLimitClause(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitLimitClause(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitLimitClause(this); + else return visitor.visitChildren(this); + } } - public MultiMatchQueryContext(BooleanExpressionContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterMultiMatchQuery(this); + + public final LimitClauseContext limitClause() throws RecognitionException { + LimitClauseContext _localctx = new LimitClauseContext(_ctx, getState()); + enterRule(_localctx, 10, RULE_limitClause); + int _la; + try { + setState(292); + _errHandler.sync(this); + switch (_input.LA(1)) { + case LIMIT: + enterOuterAlt(_localctx, 1); { + setState(287); + match(LIMIT); + setState(288); + ((LimitClauseContext) _localctx).limit = _input.LT(1); + _la = _input.LA(1); + if (!(_la == ALL || _la == INTEGER_VALUE)) { + ((LimitClauseContext) _localctx).limit = (Token) _errHandler.recoverInline(this); + } else { + if (_input.LA(1) == Token.EOF) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + } + break; + case LIMIT_ESC: + enterOuterAlt(_localctx, 2); { + setState(289); + match(LIMIT_ESC); + setState(290); + ((LimitClauseContext) _localctx).limit = _input.LT(1); + _la = _input.LA(1); + if (!(_la == ALL || _la == INTEGER_VALUE)) { + ((LimitClauseContext) _localctx).limit = (Token) _errHandler.recoverInline(this); + } else { + if (_input.LA(1) == Token.EOF) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + setState(291); + match(ESC_END); + } + break; + default: + throw new NoViableAltException(this); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitMultiMatchQuery(this); + + public static class QueryTermContext extends ParserRuleContext { + public QueryTermContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_queryTerm; + } + + public QueryTermContext() {} + + public void copyFrom(QueryTermContext ctx) { + super.copyFrom(ctx); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitMultiMatchQuery(this); - else return visitor.visitChildren(this); + + public static class SubqueryContext extends QueryTermContext { + public QueryNoWithContext queryNoWith() { + return getRuleContext(QueryNoWithContext.class, 0); + } + + public SubqueryContext(QueryTermContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterSubquery(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitSubquery(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitSubquery(this); + else return visitor.visitChildren(this); + } } - } - public static class MatchQueryContext extends BooleanExpressionContext { - public QualifiedNameContext singleField; - public StringContext queryString; - public TerminalNode MATCH() { return getToken(SqlBaseParser.MATCH, 0); } - public MatchQueryOptionsContext matchQueryOptions() { - return getRuleContext(MatchQueryOptionsContext.class,0); + + public static class QueryPrimaryDefaultContext extends QueryTermContext { + public QuerySpecificationContext querySpecification() { + return getRuleContext(QuerySpecificationContext.class, 0); + } + + public QueryPrimaryDefaultContext(QueryTermContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterQueryPrimaryDefault(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitQueryPrimaryDefault(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitQueryPrimaryDefault(this); + else return visitor.visitChildren(this); + } } - public QualifiedNameContext qualifiedName() { - return getRuleContext(QualifiedNameContext.class,0); + + public final QueryTermContext queryTerm() throws RecognitionException { + QueryTermContext _localctx = new QueryTermContext(_ctx, getState()); + enterRule(_localctx, 12, RULE_queryTerm); + try { + setState(299); + _errHandler.sync(this); + switch (_input.LA(1)) { + case SELECT: + _localctx = new QueryPrimaryDefaultContext(_localctx); + enterOuterAlt(_localctx, 1); { + setState(294); + querySpecification(); + } + break; + case T__0: + _localctx = new SubqueryContext(_localctx); + enterOuterAlt(_localctx, 2); { + setState(295); + match(T__0); + setState(296); + queryNoWith(); + setState(297); + match(T__1); + } + break; + default: + throw new NoViableAltException(this); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - public StringContext string() { - return getRuleContext(StringContext.class,0); + + public static class OrderByContext extends ParserRuleContext { + public Token ordering; + public Token nullOrdering; + + public ExpressionContext expression() { + return getRuleContext(ExpressionContext.class, 0); + } + + public TerminalNode NULLS() { + return getToken(SqlBaseParser.NULLS, 0); + } + + public TerminalNode ASC() { + return getToken(SqlBaseParser.ASC, 0); + } + + public TerminalNode DESC() { + return getToken(SqlBaseParser.DESC, 0); + } + + public TerminalNode FIRST() { + return getToken(SqlBaseParser.FIRST, 0); + } + + public TerminalNode LAST() { + return getToken(SqlBaseParser.LAST, 0); + } + + public OrderByContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_orderBy; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterOrderBy(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitOrderBy(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitOrderBy(this); + else return visitor.visitChildren(this); + } } - public MatchQueryContext(BooleanExpressionContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterMatchQuery(this); + + public final OrderByContext orderBy() throws RecognitionException { + OrderByContext _localctx = new OrderByContext(_ctx, getState()); + enterRule(_localctx, 14, RULE_orderBy); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(301); + expression(); + setState(303); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == ASC || _la == DESC) { + { + setState(302); + ((OrderByContext) _localctx).ordering = _input.LT(1); + _la = _input.LA(1); + if (!(_la == ASC || _la == DESC)) { + ((OrderByContext) _localctx).ordering = (Token) _errHandler.recoverInline(this); + } else { + if (_input.LA(1) == Token.EOF) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + } + } + + setState(307); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == NULLS) { + { + setState(305); + match(NULLS); + setState(306); + ((OrderByContext) _localctx).nullOrdering = _input.LT(1); + _la = _input.LA(1); + if (!(_la == FIRST || _la == LAST)) { + ((OrderByContext) _localctx).nullOrdering = (Token) _errHandler.recoverInline(this); + } else { + if (_input.LA(1) == Token.EOF) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + } + } + + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitMatchQuery(this); + + public static class QuerySpecificationContext extends ParserRuleContext { + public BooleanExpressionContext where; + public BooleanExpressionContext having; + + public TerminalNode SELECT() { + return getToken(SqlBaseParser.SELECT, 0); + } + + public SelectItemsContext selectItems() { + return getRuleContext(SelectItemsContext.class, 0); + } + + public TopClauseContext topClause() { + return getRuleContext(TopClauseContext.class, 0); + } + + public SetQuantifierContext setQuantifier() { + return getRuleContext(SetQuantifierContext.class, 0); + } + + public FromClauseContext fromClause() { + return getRuleContext(FromClauseContext.class, 0); + } + + public TerminalNode WHERE() { + return getToken(SqlBaseParser.WHERE, 0); + } + + public TerminalNode GROUP() { + return getToken(SqlBaseParser.GROUP, 0); + } + + public TerminalNode BY() { + return getToken(SqlBaseParser.BY, 0); + } + + public GroupByContext groupBy() { + return getRuleContext(GroupByContext.class, 0); + } + + public TerminalNode HAVING() { + return getToken(SqlBaseParser.HAVING, 0); + } + + public List booleanExpression() { + return getRuleContexts(BooleanExpressionContext.class); + } + + public BooleanExpressionContext booleanExpression(int i) { + return getRuleContext(BooleanExpressionContext.class, i); + } + + public QuerySpecificationContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_querySpecification; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterQuerySpecification(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitQuerySpecification(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitQuerySpecification(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitMatchQuery(this); - else return visitor.visitChildren(this); - } - } - public static class LogicalBinaryContext extends BooleanExpressionContext { - public BooleanExpressionContext left; - public Token operator; - public BooleanExpressionContext right; - public List booleanExpression() { - return getRuleContexts(BooleanExpressionContext.class); - } - public BooleanExpressionContext booleanExpression(int i) { - return getRuleContext(BooleanExpressionContext.class,i); - } - public TerminalNode AND() { return getToken(SqlBaseParser.AND, 0); } - public TerminalNode OR() { return getToken(SqlBaseParser.OR, 0); } - public LogicalBinaryContext(BooleanExpressionContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterLogicalBinary(this); + + public final QuerySpecificationContext querySpecification() throws RecognitionException { + QuerySpecificationContext _localctx = new QuerySpecificationContext(_ctx, getState()); + enterRule(_localctx, 16, RULE_querySpecification); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(309); + match(SELECT); + setState(311); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 36, _ctx)) { + case 1: { + setState(310); + topClause(); + } + break; + } + setState(314); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == ALL || _la == DISTINCT) { + { + setState(313); + setQuantifier(); + } + } + + setState(316); + selectItems(); + setState(318); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == FROM) { + { + setState(317); + fromClause(); + } + } + + setState(322); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == WHERE) { + { + setState(320); + match(WHERE); + setState(321); + ((QuerySpecificationContext) _localctx).where = booleanExpression(0); + } + } + + setState(327); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == GROUP) { + { + setState(324); + match(GROUP); + setState(325); + match(BY); + setState(326); + groupBy(); + } + } + + setState(331); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == HAVING) { + { + setState(329); + match(HAVING); + setState(330); + ((QuerySpecificationContext) _localctx).having = booleanExpression(0); + } + } + + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitLogicalBinary(this); + + public static class FromClauseContext extends ParserRuleContext { + public TerminalNode FROM() { + return getToken(SqlBaseParser.FROM, 0); + } + + public List relation() { + return getRuleContexts(RelationContext.class); + } + + public RelationContext relation(int i) { + return getRuleContext(RelationContext.class, i); + } + + public PivotClauseContext pivotClause() { + return getRuleContext(PivotClauseContext.class, 0); + } + + public FromClauseContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_fromClause; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterFromClause(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitFromClause(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitFromClause(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitLogicalBinary(this); - else return visitor.visitChildren(this); - } - } - - public final BooleanExpressionContext booleanExpression() throws RecognitionException { - return booleanExpression(0); - } - - private BooleanExpressionContext booleanExpression(int _p) throws RecognitionException { - ParserRuleContext _parentctx = _ctx; - int _parentState = getState(); - BooleanExpressionContext _localctx = new BooleanExpressionContext(_ctx, _parentState); - BooleanExpressionContext _prevctx = _localctx; - int _startState = 54; - enterRecursionRule(_localctx, 54, RULE_booleanExpression, _p); - try { - int _alt; - enterOuterAlt(_localctx, 1); - { - setState(541); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,73,_ctx) ) { - case 1: - { - _localctx = new LogicalNotContext(_localctx); - _ctx = _localctx; - _prevctx = _localctx; - - setState(511); - match(NOT); - setState(512); - booleanExpression(8); - } - break; - case 2: - { - _localctx = new ExistsContext(_localctx); - _ctx = _localctx; - _prevctx = _localctx; - setState(513); - match(EXISTS); - setState(514); - match(T__0); - setState(515); - query(); - setState(516); - match(T__1); - } - break; - case 3: - { - _localctx = new StringQueryContext(_localctx); - _ctx = _localctx; - _prevctx = _localctx; - setState(518); - match(QUERY); - setState(519); - match(T__0); - setState(520); - ((StringQueryContext)_localctx).queryString = string(); - setState(521); - matchQueryOptions(); - setState(522); - match(T__1); - } - break; - case 4: - { - _localctx = new MatchQueryContext(_localctx); - _ctx = _localctx; - _prevctx = _localctx; - setState(524); - match(MATCH); - setState(525); - match(T__0); - setState(526); - ((MatchQueryContext)_localctx).singleField = qualifiedName(); - setState(527); - match(T__2); - setState(528); - ((MatchQueryContext)_localctx).queryString = string(); - setState(529); - matchQueryOptions(); - setState(530); - match(T__1); - } - break; - case 5: - { - _localctx = new MultiMatchQueryContext(_localctx); - _ctx = _localctx; - _prevctx = _localctx; - setState(532); - match(MATCH); - setState(533); - match(T__0); - setState(534); - ((MultiMatchQueryContext)_localctx).multiFields = string(); - setState(535); - match(T__2); - setState(536); - ((MultiMatchQueryContext)_localctx).queryString = string(); - setState(537); - matchQueryOptions(); - setState(538); - match(T__1); - } - break; - case 6: - { - _localctx = new BooleanDefaultContext(_localctx); - _ctx = _localctx; - _prevctx = _localctx; - setState(540); - predicated(); - } - break; - } - _ctx.stop = _input.LT(-1); - setState(551); - _errHandler.sync(this); - _alt = getInterpreter().adaptivePredict(_input,75,_ctx); - while ( _alt!=2 && _alt!=org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER ) { - if ( _alt==1 ) { - if ( _parseListeners!=null ) triggerExitRuleEvent(); - _prevctx = _localctx; - { - setState(549); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,74,_ctx) ) { - case 1: + + public final FromClauseContext fromClause() throws RecognitionException { + FromClauseContext _localctx = new FromClauseContext(_ctx, getState()); + enterRule(_localctx, 18, RULE_fromClause); + int _la; + try { + enterOuterAlt(_localctx, 1); { - _localctx = new LogicalBinaryContext(new BooleanExpressionContext(_parentctx, _parentState)); - ((LogicalBinaryContext)_localctx).left = _prevctx; - pushNewRecursionContext(_localctx, _startState, RULE_booleanExpression); - setState(543); - if (!(precpred(_ctx, 2))) throw new FailedPredicateException(this, "precpred(_ctx, 2)"); - setState(544); - ((LogicalBinaryContext)_localctx).operator = match(AND); - setState(545); - ((LogicalBinaryContext)_localctx).right = booleanExpression(3); + setState(333); + match(FROM); + setState(334); + relation(); + setState(339); + _errHandler.sync(this); + _la = _input.LA(1); + while (_la == T__2) { + { + { + setState(335); + match(T__2); + setState(336); + relation(); + } + } + setState(341); + _errHandler.sync(this); + _la = _input.LA(1); + } + setState(343); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == PIVOT) { + { + setState(342); + pivotClause(); + } + } + } - break; - case 2: + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; + } + + public static class GroupByContext extends ParserRuleContext { + public List groupingElement() { + return getRuleContexts(GroupingElementContext.class); + } + + public GroupingElementContext groupingElement(int i) { + return getRuleContext(GroupingElementContext.class, i); + } + + public SetQuantifierContext setQuantifier() { + return getRuleContext(SetQuantifierContext.class, 0); + } + + public GroupByContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_groupBy; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterGroupBy(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitGroupBy(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitGroupBy(this); + else return visitor.visitChildren(this); + } + } + + public final GroupByContext groupBy() throws RecognitionException { + GroupByContext _localctx = new GroupByContext(_ctx, getState()); + enterRule(_localctx, 20, RULE_groupBy); + int _la; + try { + enterOuterAlt(_localctx, 1); { - _localctx = new LogicalBinaryContext(new BooleanExpressionContext(_parentctx, _parentState)); - ((LogicalBinaryContext)_localctx).left = _prevctx; - pushNewRecursionContext(_localctx, _startState, RULE_booleanExpression); - setState(546); - if (!(precpred(_ctx, 1))) throw new FailedPredicateException(this, "precpred(_ctx, 1)"); - setState(547); - ((LogicalBinaryContext)_localctx).operator = match(OR); - setState(548); - ((LogicalBinaryContext)_localctx).right = booleanExpression(2); + setState(346); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == ALL || _la == DISTINCT) { + { + setState(345); + setQuantifier(); + } + } + + setState(348); + groupingElement(); + setState(353); + _errHandler.sync(this); + _la = _input.LA(1); + while (_la == T__2) { + { + { + setState(349); + match(T__2); + setState(350); + groupingElement(); + } + } + setState(355); + _errHandler.sync(this); + _la = _input.LA(1); + } } - break; - } - } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); } - setState(553); - _errHandler.sync(this); - _alt = getInterpreter().adaptivePredict(_input,75,_ctx); - } - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); + return _localctx; } - finally { - unrollRecursionContexts(_parentctx); - } - return _localctx; - } - public static class MatchQueryOptionsContext extends ParserRuleContext { - public List string() { - return getRuleContexts(StringContext.class); - } - public StringContext string(int i) { - return getRuleContext(StringContext.class,i); - } - public MatchQueryOptionsContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_matchQueryOptions; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterMatchQueryOptions(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitMatchQueryOptions(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitMatchQueryOptions(this); - else return visitor.visitChildren(this); - } - } - - public final MatchQueryOptionsContext matchQueryOptions() throws RecognitionException { - MatchQueryOptionsContext _localctx = new MatchQueryOptionsContext(_ctx, getState()); - enterRule(_localctx, 56, RULE_matchQueryOptions); - int _la; - try { - enterOuterAlt(_localctx, 1); - { - setState(558); - _errHandler.sync(this); - _la = _input.LA(1); - while (_la==T__2) { - { - { - setState(554); - match(T__2); - setState(555); - string(); - } - } - setState(560); - _errHandler.sync(this); - _la = _input.LA(1); - } - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class PredicatedContext extends ParserRuleContext { - public ValueExpressionContext valueExpression() { - return getRuleContext(ValueExpressionContext.class,0); - } - public PredicateContext predicate() { - return getRuleContext(PredicateContext.class,0); - } - public PredicatedContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_predicated; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterPredicated(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitPredicated(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitPredicated(this); - else return visitor.visitChildren(this); - } - } - - public final PredicatedContext predicated() throws RecognitionException { - PredicatedContext _localctx = new PredicatedContext(_ctx, getState()); - enterRule(_localctx, 58, RULE_predicated); - try { - enterOuterAlt(_localctx, 1); - { - setState(561); - valueExpression(0); - setState(563); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,77,_ctx) ) { - case 1: - { - setState(562); - predicate(); - } - break; - } - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class PredicateContext extends ParserRuleContext { - public Token kind; - public ValueExpressionContext lower; - public ValueExpressionContext upper; - public StringContext regex; - public TerminalNode AND() { return getToken(SqlBaseParser.AND, 0); } - public TerminalNode BETWEEN() { return getToken(SqlBaseParser.BETWEEN, 0); } - public List valueExpression() { - return getRuleContexts(ValueExpressionContext.class); - } - public ValueExpressionContext valueExpression(int i) { - return getRuleContext(ValueExpressionContext.class,i); - } - public TerminalNode NOT() { return getToken(SqlBaseParser.NOT, 0); } - public TerminalNode IN() { return getToken(SqlBaseParser.IN, 0); } - public QueryContext query() { - return getRuleContext(QueryContext.class,0); - } - public PatternContext pattern() { - return getRuleContext(PatternContext.class,0); - } - public TerminalNode LIKE() { return getToken(SqlBaseParser.LIKE, 0); } - public TerminalNode RLIKE() { return getToken(SqlBaseParser.RLIKE, 0); } - public StringContext string() { - return getRuleContext(StringContext.class,0); - } - public TerminalNode IS() { return getToken(SqlBaseParser.IS, 0); } - public TerminalNode NULL() { return getToken(SqlBaseParser.NULL, 0); } - public PredicateContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_predicate; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterPredicate(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitPredicate(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitPredicate(this); - else return visitor.visitChildren(this); - } - } - - public final PredicateContext predicate() throws RecognitionException { - PredicateContext _localctx = new PredicateContext(_ctx, getState()); - enterRule(_localctx, 60, RULE_predicate); - int _la; - try { - setState(611); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,85,_ctx) ) { - case 1: - enterOuterAlt(_localctx, 1); - { - setState(566); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==NOT) { - { - setState(565); - match(NOT); - } - } - - setState(568); - ((PredicateContext)_localctx).kind = match(BETWEEN); - setState(569); - ((PredicateContext)_localctx).lower = valueExpression(0); - setState(570); - match(AND); - setState(571); - ((PredicateContext)_localctx).upper = valueExpression(0); - } - break; - case 2: - enterOuterAlt(_localctx, 2); - { - setState(574); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==NOT) { - { - setState(573); - match(NOT); - } - } - - setState(576); - ((PredicateContext)_localctx).kind = match(IN); - setState(577); - match(T__0); - setState(578); - valueExpression(0); - setState(583); - _errHandler.sync(this); - _la = _input.LA(1); - while (_la==T__2) { - { - { - setState(579); - match(T__2); - setState(580); - valueExpression(0); - } - } - setState(585); - _errHandler.sync(this); - _la = _input.LA(1); - } - setState(586); - match(T__1); - } - break; - case 3: - enterOuterAlt(_localctx, 3); - { - setState(589); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==NOT) { - { - setState(588); - match(NOT); - } - } - - setState(591); - ((PredicateContext)_localctx).kind = match(IN); - setState(592); - match(T__0); - setState(593); - query(); - setState(594); - match(T__1); - } - break; - case 4: - enterOuterAlt(_localctx, 4); - { - setState(597); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==NOT) { - { - setState(596); - match(NOT); - } - } - - setState(599); - ((PredicateContext)_localctx).kind = match(LIKE); - setState(600); - pattern(); - } - break; - case 5: - enterOuterAlt(_localctx, 5); - { - setState(602); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==NOT) { - { - setState(601); - match(NOT); - } - } - - setState(604); - ((PredicateContext)_localctx).kind = match(RLIKE); - setState(605); - ((PredicateContext)_localctx).regex = string(); - } - break; - case 6: - enterOuterAlt(_localctx, 6); - { - setState(606); - match(IS); - setState(608); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==NOT) { - { - setState(607); - match(NOT); - } - } - - setState(610); - ((PredicateContext)_localctx).kind = match(NULL); - } - break; - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class LikePatternContext extends ParserRuleContext { - public TerminalNode LIKE() { return getToken(SqlBaseParser.LIKE, 0); } - public PatternContext pattern() { - return getRuleContext(PatternContext.class,0); - } - public LikePatternContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_likePattern; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterLikePattern(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitLikePattern(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitLikePattern(this); - else return visitor.visitChildren(this); - } - } - - public final LikePatternContext likePattern() throws RecognitionException { - LikePatternContext _localctx = new LikePatternContext(_ctx, getState()); - enterRule(_localctx, 62, RULE_likePattern); - try { - enterOuterAlt(_localctx, 1); - { - setState(613); - match(LIKE); - setState(614); - pattern(); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class PatternContext extends ParserRuleContext { - public StringContext value; - public StringContext string() { - return getRuleContext(StringContext.class,0); - } - public PatternEscapeContext patternEscape() { - return getRuleContext(PatternEscapeContext.class,0); - } - public PatternContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_pattern; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterPattern(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitPattern(this); + public static class GroupingElementContext extends ParserRuleContext { + public GroupingElementContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_groupingElement; + } + + public GroupingElementContext() {} + + public void copyFrom(GroupingElementContext ctx) { + super.copyFrom(ctx); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitPattern(this); - else return visitor.visitChildren(this); - } - } - - public final PatternContext pattern() throws RecognitionException { - PatternContext _localctx = new PatternContext(_ctx, getState()); - enterRule(_localctx, 64, RULE_pattern); - try { - enterOuterAlt(_localctx, 1); - { - setState(616); - ((PatternContext)_localctx).value = string(); - setState(618); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,86,_ctx) ) { - case 1: - { - setState(617); - patternEscape(); - } - break; - } - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class PatternEscapeContext extends ParserRuleContext { - public StringContext escape; - public TerminalNode ESCAPE() { return getToken(SqlBaseParser.ESCAPE, 0); } - public StringContext string() { - return getRuleContext(StringContext.class,0); - } - public TerminalNode ESCAPE_ESC() { return getToken(SqlBaseParser.ESCAPE_ESC, 0); } - public TerminalNode ESC_END() { return getToken(SqlBaseParser.ESC_END, 0); } - public PatternEscapeContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_patternEscape; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterPatternEscape(this); + + public static class SingleGroupingSetContext extends GroupingElementContext { + public GroupingExpressionsContext groupingExpressions() { + return getRuleContext(GroupingExpressionsContext.class, 0); + } + + public SingleGroupingSetContext(GroupingElementContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterSingleGroupingSet(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitSingleGroupingSet(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitSingleGroupingSet(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitPatternEscape(this); + + public final GroupingElementContext groupingElement() throws RecognitionException { + GroupingElementContext _localctx = new GroupingElementContext(_ctx, getState()); + enterRule(_localctx, 22, RULE_groupingElement); + try { + _localctx = new SingleGroupingSetContext(_localctx); + enterOuterAlt(_localctx, 1); + { + setState(356); + groupingExpressions(); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitPatternEscape(this); - else return visitor.visitChildren(this); - } - } - - public final PatternEscapeContext patternEscape() throws RecognitionException { - PatternEscapeContext _localctx = new PatternEscapeContext(_ctx, getState()); - enterRule(_localctx, 66, RULE_patternEscape); - try { - setState(626); - _errHandler.sync(this); - switch (_input.LA(1)) { - case ESCAPE: - enterOuterAlt(_localctx, 1); - { - setState(620); - match(ESCAPE); - setState(621); - ((PatternEscapeContext)_localctx).escape = string(); - } - break; - case ESCAPE_ESC: - enterOuterAlt(_localctx, 2); - { - setState(622); - match(ESCAPE_ESC); - setState(623); - ((PatternEscapeContext)_localctx).escape = string(); - setState(624); - match(ESC_END); - } - break; - default: - throw new NoViableAltException(this); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class ValueExpressionContext extends ParserRuleContext { - public ValueExpressionContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_valueExpression; } - - public ValueExpressionContext() { } - public void copyFrom(ValueExpressionContext ctx) { - super.copyFrom(ctx); - } - } - public static class ValueExpressionDefaultContext extends ValueExpressionContext { - public PrimaryExpressionContext primaryExpression() { - return getRuleContext(PrimaryExpressionContext.class,0); - } - public ValueExpressionDefaultContext(ValueExpressionContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterValueExpressionDefault(this); + + public static class GroupingExpressionsContext extends ParserRuleContext { + public List expression() { + return getRuleContexts(ExpressionContext.class); + } + + public ExpressionContext expression(int i) { + return getRuleContext(ExpressionContext.class, i); + } + + public GroupingExpressionsContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_groupingExpressions; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterGroupingExpressions(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitGroupingExpressions(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitGroupingExpressions(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitValueExpressionDefault(this); + + public final GroupingExpressionsContext groupingExpressions() throws RecognitionException { + GroupingExpressionsContext _localctx = new GroupingExpressionsContext(_ctx, getState()); + enterRule(_localctx, 24, RULE_groupingExpressions); + int _la; + try { + setState(371); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 48, _ctx)) { + case 1: + enterOuterAlt(_localctx, 1); { + setState(358); + match(T__0); + setState(367); + _errHandler.sync(this); + _la = _input.LA(1); + if ((((_la) & ~0x3f) == 0 + && ((1L << _la) & ((1L << T__0) | (1L << ANALYZE) | (1L << ANALYZED) | (1L << CASE) | (1L << CAST) | (1L + << CATALOGS) | (1L << COLUMNS) | (1L << CONVERT) | (1L << CURRENT_DATE) | (1L << CURRENT_TIME) | (1L + << CURRENT_TIMESTAMP) | (1L << DAY) | (1L << DEBUG) | (1L << EXECUTABLE) | (1L << EXISTS) | (1L << EXPLAIN) + | (1L << EXTRACT) | (1L << FALSE) | (1L << FIRST) | (1L << FORMAT) | (1L << FULL) | (1L << FUNCTIONS) | (1L + << GRAPHVIZ) | (1L << HOUR) | (1L << INTERVAL) | (1L << LAST) | (1L << LEFT) | (1L << LIMIT) | (1L + << MAPPED) | (1L << MATCH) | (1L << MINUTE) | (1L << MONTH))) != 0) + || ((((_la - 66)) & ~0x3f) == 0 + && ((1L << (_la - 66)) & ((1L << (NOT - 66)) | (1L << (NULL - 66)) | (1L << (OPTIMIZED - 66)) | (1L << (PARSED + - 66)) | (1L << (PHYSICAL - 66)) | (1L << (PIVOT - 66)) | (1L << (PLAN - 66)) | (1L << (RIGHT - 66)) | (1L + << (RLIKE - 66)) | (1L << (QUERY - 66)) | (1L << (SCHEMAS - 66)) | (1L << (SECOND - 66)) | (1L << (SHOW + - 66)) | (1L << (SYS - 66)) | (1L << (TABLES - 66)) | (1L << (TEXT - 66)) | (1L << (TRUE - 66)) + | (1L << (TOP - 66)) | (1L << (TYPE - 66)) | (1L << (TYPES - 66)) | (1L << (VERIFY - 66)) | (1L << (YEAR + - 66)) | (1L << (FUNCTION_ESC - 66)) | (1L << (DATE_ESC - 66)) | (1L << (TIME_ESC - 66)) | (1L + << (TIMESTAMP_ESC - 66)) | (1L << (GUID_ESC - 66)) | (1L << (PLUS - 66)) | (1L << (MINUS - 66)) + | (1L << (ASTERISK - 66)) | (1L << (PARAM - 66)) | (1L << (STRING - 66)) | (1L << (INTEGER_VALUE - 66)) + | (1L << (DECIMAL_VALUE - 66)))) != 0) + || ((((_la - 130)) & ~0x3f) == 0 + && ((1L << (_la - 130)) & ((1L << (IDENTIFIER - 130)) | (1L << (DIGIT_IDENTIFIER - 130)) | (1L + << (QUOTED_IDENTIFIER - 130)) | (1L << (BACKQUOTED_IDENTIFIER - 130)))) != 0)) { + { + setState(359); + expression(); + setState(364); + _errHandler.sync(this); + _la = _input.LA(1); + while (_la == T__2) { + { + { + setState(360); + match(T__2); + setState(361); + expression(); + } + } + setState(366); + _errHandler.sync(this); + _la = _input.LA(1); + } + } + } + + setState(369); + match(T__1); + } + break; + case 2: + enterOuterAlt(_localctx, 2); { + setState(370); + expression(); + } + break; + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitValueExpressionDefault(this); - else return visitor.visitChildren(this); + + public static class NamedQueryContext extends ParserRuleContext { + public IdentifierContext name; + + public TerminalNode AS() { + return getToken(SqlBaseParser.AS, 0); + } + + public QueryNoWithContext queryNoWith() { + return getRuleContext(QueryNoWithContext.class, 0); + } + + public IdentifierContext identifier() { + return getRuleContext(IdentifierContext.class, 0); + } + + public NamedQueryContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_namedQuery; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterNamedQuery(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitNamedQuery(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitNamedQuery(this); + else return visitor.visitChildren(this); + } } - } - public static class ComparisonContext extends ValueExpressionContext { - public ValueExpressionContext left; - public ValueExpressionContext right; - public ComparisonOperatorContext comparisonOperator() { - return getRuleContext(ComparisonOperatorContext.class,0); + + public final NamedQueryContext namedQuery() throws RecognitionException { + NamedQueryContext _localctx = new NamedQueryContext(_ctx, getState()); + enterRule(_localctx, 26, RULE_namedQuery); + try { + enterOuterAlt(_localctx, 1); + { + setState(373); + ((NamedQueryContext) _localctx).name = identifier(); + setState(374); + match(AS); + setState(375); + match(T__0); + setState(376); + queryNoWith(); + setState(377); + match(T__1); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - public List valueExpression() { - return getRuleContexts(ValueExpressionContext.class); + + public static class TopClauseContext extends ParserRuleContext { + public Token top; + + public TerminalNode TOP() { + return getToken(SqlBaseParser.TOP, 0); + } + + public TerminalNode INTEGER_VALUE() { + return getToken(SqlBaseParser.INTEGER_VALUE, 0); + } + + public TopClauseContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_topClause; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterTopClause(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitTopClause(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitTopClause(this); + else return visitor.visitChildren(this); + } } - public ValueExpressionContext valueExpression(int i) { - return getRuleContext(ValueExpressionContext.class,i); + + public final TopClauseContext topClause() throws RecognitionException { + TopClauseContext _localctx = new TopClauseContext(_ctx, getState()); + enterRule(_localctx, 28, RULE_topClause); + try { + enterOuterAlt(_localctx, 1); + { + setState(379); + match(TOP); + setState(380); + ((TopClauseContext) _localctx).top = match(INTEGER_VALUE); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - public ComparisonContext(ValueExpressionContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterComparison(this); + + public static class SetQuantifierContext extends ParserRuleContext { + public TerminalNode DISTINCT() { + return getToken(SqlBaseParser.DISTINCT, 0); + } + + public TerminalNode ALL() { + return getToken(SqlBaseParser.ALL, 0); + } + + public SetQuantifierContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_setQuantifier; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterSetQuantifier(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitSetQuantifier(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitSetQuantifier(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitComparison(this); + + public final SetQuantifierContext setQuantifier() throws RecognitionException { + SetQuantifierContext _localctx = new SetQuantifierContext(_ctx, getState()); + enterRule(_localctx, 30, RULE_setQuantifier); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(382); + _la = _input.LA(1); + if (!(_la == ALL || _la == DISTINCT)) { + _errHandler.recoverInline(this); + } else { + if (_input.LA(1) == Token.EOF) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitComparison(this); - else return visitor.visitChildren(this); - } - } - public static class ArithmeticBinaryContext extends ValueExpressionContext { - public ValueExpressionContext left; - public Token operator; - public ValueExpressionContext right; - public List valueExpression() { - return getRuleContexts(ValueExpressionContext.class); - } - public ValueExpressionContext valueExpression(int i) { - return getRuleContext(ValueExpressionContext.class,i); - } - public TerminalNode ASTERISK() { return getToken(SqlBaseParser.ASTERISK, 0); } - public TerminalNode SLASH() { return getToken(SqlBaseParser.SLASH, 0); } - public TerminalNode PERCENT() { return getToken(SqlBaseParser.PERCENT, 0); } - public TerminalNode PLUS() { return getToken(SqlBaseParser.PLUS, 0); } - public TerminalNode MINUS() { return getToken(SqlBaseParser.MINUS, 0); } - public ArithmeticBinaryContext(ValueExpressionContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterArithmeticBinary(this); + + public static class SelectItemsContext extends ParserRuleContext { + public List selectItem() { + return getRuleContexts(SelectItemContext.class); + } + + public SelectItemContext selectItem(int i) { + return getRuleContext(SelectItemContext.class, i); + } + + public SelectItemsContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_selectItems; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterSelectItems(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitSelectItems(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitSelectItems(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitArithmeticBinary(this); + + public final SelectItemsContext selectItems() throws RecognitionException { + SelectItemsContext _localctx = new SelectItemsContext(_ctx, getState()); + enterRule(_localctx, 32, RULE_selectItems); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(384); + selectItem(); + setState(389); + _errHandler.sync(this); + _la = _input.LA(1); + while (_la == T__2) { + { + { + setState(385); + match(T__2); + setState(386); + selectItem(); + } + } + setState(391); + _errHandler.sync(this); + _la = _input.LA(1); + } + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitArithmeticBinary(this); - else return visitor.visitChildren(this); - } - } - public static class ArithmeticUnaryContext extends ValueExpressionContext { - public Token operator; - public ValueExpressionContext valueExpression() { - return getRuleContext(ValueExpressionContext.class,0); - } - public TerminalNode MINUS() { return getToken(SqlBaseParser.MINUS, 0); } - public TerminalNode PLUS() { return getToken(SqlBaseParser.PLUS, 0); } - public ArithmeticUnaryContext(ValueExpressionContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterArithmeticUnary(this); + + public static class SelectItemContext extends ParserRuleContext { + public SelectItemContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_selectItem; + } + + public SelectItemContext() {} + + public void copyFrom(SelectItemContext ctx) { + super.copyFrom(ctx); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitArithmeticUnary(this); + + public static class SelectExpressionContext extends SelectItemContext { + public ExpressionContext expression() { + return getRuleContext(ExpressionContext.class, 0); + } + + public IdentifierContext identifier() { + return getRuleContext(IdentifierContext.class, 0); + } + + public TerminalNode AS() { + return getToken(SqlBaseParser.AS, 0); + } + + public SelectExpressionContext(SelectItemContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterSelectExpression(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitSelectExpression(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitSelectExpression(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitArithmeticUnary(this); - else return visitor.visitChildren(this); - } - } - - public final ValueExpressionContext valueExpression() throws RecognitionException { - return valueExpression(0); - } - - private ValueExpressionContext valueExpression(int _p) throws RecognitionException { - ParserRuleContext _parentctx = _ctx; - int _parentState = getState(); - ValueExpressionContext _localctx = new ValueExpressionContext(_ctx, _parentState); - ValueExpressionContext _prevctx = _localctx; - int _startState = 68; - enterRecursionRule(_localctx, 68, RULE_valueExpression, _p); - int _la; - try { - int _alt; - enterOuterAlt(_localctx, 1); - { - setState(632); - _errHandler.sync(this); - switch (_input.LA(1)) { - case T__0: - case ANALYZE: - case ANALYZED: - case CASE: - case CAST: - case CATALOGS: - case COLUMNS: - case CONVERT: - case CURRENT_DATE: - case CURRENT_TIME: - case CURRENT_TIMESTAMP: - case DAY: - case DEBUG: - case EXECUTABLE: - case EXPLAIN: - case EXTRACT: - case FALSE: - case FIRST: - case FORMAT: - case FULL: - case FUNCTIONS: - case GRAPHVIZ: - case HOUR: - case INTERVAL: - case LAST: - case LEFT: - case LIMIT: - case MAPPED: - case MINUTE: - case MONTH: - case NULL: - case OPTIMIZED: - case PARSED: - case PHYSICAL: - case PIVOT: - case PLAN: - case RIGHT: - case RLIKE: - case QUERY: - case SCHEMAS: - case SECOND: - case SHOW: - case SYS: - case TABLES: - case TEXT: - case TRUE: - case TOP: - case TYPE: - case TYPES: - case VERIFY: - case YEAR: - case FUNCTION_ESC: - case DATE_ESC: - case TIME_ESC: - case TIMESTAMP_ESC: - case GUID_ESC: - case ASTERISK: - case PARAM: - case STRING: - case INTEGER_VALUE: - case DECIMAL_VALUE: - case IDENTIFIER: - case DIGIT_IDENTIFIER: - case QUOTED_IDENTIFIER: - case BACKQUOTED_IDENTIFIER: - { - _localctx = new ValueExpressionDefaultContext(_localctx); - _ctx = _localctx; - _prevctx = _localctx; - - setState(629); - primaryExpression(0); - } - break; - case PLUS: - case MINUS: - { - _localctx = new ArithmeticUnaryContext(_localctx); - _ctx = _localctx; - _prevctx = _localctx; - setState(630); - ((ArithmeticUnaryContext)_localctx).operator = _input.LT(1); - _la = _input.LA(1); - if ( !(_la==PLUS || _la==MINUS) ) { - ((ArithmeticUnaryContext)_localctx).operator = (Token)_errHandler.recoverInline(this); - } - else { - if ( _input.LA(1)==Token.EOF ) matchedEOF = true; - _errHandler.reportMatch(this); - consume(); - } - setState(631); - valueExpression(4); - } - break; - default: - throw new NoViableAltException(this); - } - _ctx.stop = _input.LT(-1); - setState(646); - _errHandler.sync(this); - _alt = getInterpreter().adaptivePredict(_input,90,_ctx); - while ( _alt!=2 && _alt!=org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER ) { - if ( _alt==1 ) { - if ( _parseListeners!=null ) triggerExitRuleEvent(); - _prevctx = _localctx; - { - setState(644); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,89,_ctx) ) { - case 1: - { - _localctx = new ArithmeticBinaryContext(new ValueExpressionContext(_parentctx, _parentState)); - ((ArithmeticBinaryContext)_localctx).left = _prevctx; - pushNewRecursionContext(_localctx, _startState, RULE_valueExpression); - setState(634); - if (!(precpred(_ctx, 3))) throw new FailedPredicateException(this, "precpred(_ctx, 3)"); - setState(635); - ((ArithmeticBinaryContext)_localctx).operator = _input.LT(1); - _la = _input.LA(1); - if ( !(((((_la - 121)) & ~0x3f) == 0 && ((1L << (_la - 121)) & ((1L << (ASTERISK - 121)) | (1L << (SLASH - 121)) | (1L << (PERCENT - 121)))) != 0)) ) { - ((ArithmeticBinaryContext)_localctx).operator = (Token)_errHandler.recoverInline(this); - } - else { - if ( _input.LA(1)==Token.EOF ) matchedEOF = true; - _errHandler.reportMatch(this); - consume(); - } - setState(636); - ((ArithmeticBinaryContext)_localctx).right = valueExpression(4); - } - break; - case 2: + + public final SelectItemContext selectItem() throws RecognitionException { + SelectItemContext _localctx = new SelectItemContext(_ctx, getState()); + enterRule(_localctx, 34, RULE_selectItem); + int _la; + try { + _localctx = new SelectExpressionContext(_localctx); + enterOuterAlt(_localctx, 1); { - _localctx = new ArithmeticBinaryContext(new ValueExpressionContext(_parentctx, _parentState)); - ((ArithmeticBinaryContext)_localctx).left = _prevctx; - pushNewRecursionContext(_localctx, _startState, RULE_valueExpression); - setState(637); - if (!(precpred(_ctx, 2))) throw new FailedPredicateException(this, "precpred(_ctx, 2)"); - setState(638); - ((ArithmeticBinaryContext)_localctx).operator = _input.LT(1); - _la = _input.LA(1); - if ( !(_la==PLUS || _la==MINUS) ) { - ((ArithmeticBinaryContext)_localctx).operator = (Token)_errHandler.recoverInline(this); - } - else { - if ( _input.LA(1)==Token.EOF ) matchedEOF = true; - _errHandler.reportMatch(this); - consume(); + setState(392); + expression(); + setState(397); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 51, _ctx)) { + case 1: { + setState(394); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == AS) { + { + setState(393); + match(AS); + } + } + + setState(396); + identifier(); + } + break; + } } - setState(639); - ((ArithmeticBinaryContext)_localctx).right = valueExpression(3); - } - break; - case 3: + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; + } + + public static class RelationContext extends ParserRuleContext { + public RelationPrimaryContext relationPrimary() { + return getRuleContext(RelationPrimaryContext.class, 0); + } + + public List joinRelation() { + return getRuleContexts(JoinRelationContext.class); + } + + public JoinRelationContext joinRelation(int i) { + return getRuleContext(JoinRelationContext.class, i); + } + + public RelationContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_relation; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterRelation(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitRelation(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitRelation(this); + else return visitor.visitChildren(this); + } + } + + public final RelationContext relation() throws RecognitionException { + RelationContext _localctx = new RelationContext(_ctx, getState()); + enterRule(_localctx, 36, RULE_relation); + int _la; + try { + enterOuterAlt(_localctx, 1); { - _localctx = new ComparisonContext(new ValueExpressionContext(_parentctx, _parentState)); - ((ComparisonContext)_localctx).left = _prevctx; - pushNewRecursionContext(_localctx, _startState, RULE_valueExpression); - setState(640); - if (!(precpred(_ctx, 1))) throw new FailedPredicateException(this, "precpred(_ctx, 1)"); - setState(641); - comparisonOperator(); - setState(642); - ((ComparisonContext)_localctx).right = valueExpression(2); + setState(399); + relationPrimary(); + setState(403); + _errHandler.sync(this); + _la = _input.LA(1); + while (((((_la - 42)) & ~0x3f) == 0 + && ((1L << (_la - 42)) & ((1L << (FULL - 42)) | (1L << (INNER - 42)) | (1L << (JOIN - 42)) | (1L << (LEFT - 42)) | (1L + << (NATURAL - 42)) | (1L << (RIGHT - 42)))) != 0)) { + { + { + setState(400); + joinRelation(); + } + } + setState(405); + _errHandler.sync(this); + _la = _input.LA(1); + } } - break; - } - } - } - setState(648); - _errHandler.sync(this); - _alt = getInterpreter().adaptivePredict(_input,90,_ctx); - } - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - unrollRecursionContexts(_parentctx); - } - return _localctx; - } - - public static class PrimaryExpressionContext extends ParserRuleContext { - public PrimaryExpressionContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_primaryExpression; } - - public PrimaryExpressionContext() { } - public void copyFrom(PrimaryExpressionContext ctx) { - super.copyFrom(ctx); - } - } - public static class DereferenceContext extends PrimaryExpressionContext { - public QualifiedNameContext qualifiedName() { - return getRuleContext(QualifiedNameContext.class,0); - } - public DereferenceContext(PrimaryExpressionContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterDereference(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitDereference(this); + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitDereference(this); - else return visitor.visitChildren(this); + + public static class JoinRelationContext extends ParserRuleContext { + public RelationPrimaryContext right; + + public TerminalNode JOIN() { + return getToken(SqlBaseParser.JOIN, 0); + } + + public RelationPrimaryContext relationPrimary() { + return getRuleContext(RelationPrimaryContext.class, 0); + } + + public JoinTypeContext joinType() { + return getRuleContext(JoinTypeContext.class, 0); + } + + public JoinCriteriaContext joinCriteria() { + return getRuleContext(JoinCriteriaContext.class, 0); + } + + public TerminalNode NATURAL() { + return getToken(SqlBaseParser.NATURAL, 0); + } + + public JoinRelationContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_joinRelation; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterJoinRelation(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitJoinRelation(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitJoinRelation(this); + else return visitor.visitChildren(this); + } } - } - public static class CastContext extends PrimaryExpressionContext { - public CastExpressionContext castExpression() { - return getRuleContext(CastExpressionContext.class,0); + + public final JoinRelationContext joinRelation() throws RecognitionException { + JoinRelationContext _localctx = new JoinRelationContext(_ctx, getState()); + enterRule(_localctx, 38, RULE_joinRelation); + int _la; + try { + setState(417); + _errHandler.sync(this); + switch (_input.LA(1)) { + case FULL: + case INNER: + case JOIN: + case LEFT: + case RIGHT: + enterOuterAlt(_localctx, 1); { + { + setState(406); + joinType(); + } + setState(407); + match(JOIN); + setState(408); + ((JoinRelationContext) _localctx).right = relationPrimary(); + setState(410); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == ON || _la == USING) { + { + setState(409); + joinCriteria(); + } + } + + } + break; + case NATURAL: + enterOuterAlt(_localctx, 2); { + setState(412); + match(NATURAL); + setState(413); + joinType(); + setState(414); + match(JOIN); + setState(415); + ((JoinRelationContext) _localctx).right = relationPrimary(); + } + break; + default: + throw new NoViableAltException(this); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - public CastContext(PrimaryExpressionContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterCast(this); + + public static class JoinTypeContext extends ParserRuleContext { + public TerminalNode INNER() { + return getToken(SqlBaseParser.INNER, 0); + } + + public TerminalNode LEFT() { + return getToken(SqlBaseParser.LEFT, 0); + } + + public TerminalNode OUTER() { + return getToken(SqlBaseParser.OUTER, 0); + } + + public TerminalNode RIGHT() { + return getToken(SqlBaseParser.RIGHT, 0); + } + + public TerminalNode FULL() { + return getToken(SqlBaseParser.FULL, 0); + } + + public JoinTypeContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_joinType; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterJoinType(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitJoinType(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitJoinType(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitCast(this); + + public final JoinTypeContext joinType() throws RecognitionException { + JoinTypeContext _localctx = new JoinTypeContext(_ctx, getState()); + enterRule(_localctx, 40, RULE_joinType); + int _la; + try { + setState(434); + _errHandler.sync(this); + switch (_input.LA(1)) { + case INNER: + case JOIN: + enterOuterAlt(_localctx, 1); { + setState(420); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == INNER) { + { + setState(419); + match(INNER); + } + } + + } + break; + case LEFT: + enterOuterAlt(_localctx, 2); { + setState(422); + match(LEFT); + setState(424); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == OUTER) { + { + setState(423); + match(OUTER); + } + } + + } + break; + case RIGHT: + enterOuterAlt(_localctx, 3); { + setState(426); + match(RIGHT); + setState(428); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == OUTER) { + { + setState(427); + match(OUTER); + } + } + + } + break; + case FULL: + enterOuterAlt(_localctx, 4); { + setState(430); + match(FULL); + setState(432); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == OUTER) { + { + setState(431); + match(OUTER); + } + } + + } + break; + default: + throw new NoViableAltException(this); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitCast(this); - else return visitor.visitChildren(this); + + public static class JoinCriteriaContext extends ParserRuleContext { + public TerminalNode ON() { + return getToken(SqlBaseParser.ON, 0); + } + + public BooleanExpressionContext booleanExpression() { + return getRuleContext(BooleanExpressionContext.class, 0); + } + + public TerminalNode USING() { + return getToken(SqlBaseParser.USING, 0); + } + + public List identifier() { + return getRuleContexts(IdentifierContext.class); + } + + public IdentifierContext identifier(int i) { + return getRuleContext(IdentifierContext.class, i); + } + + public JoinCriteriaContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_joinCriteria; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterJoinCriteria(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitJoinCriteria(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitJoinCriteria(this); + else return visitor.visitChildren(this); + } } - } - public static class ConstantDefaultContext extends PrimaryExpressionContext { - public ConstantContext constant() { - return getRuleContext(ConstantContext.class,0); + + public final JoinCriteriaContext joinCriteria() throws RecognitionException { + JoinCriteriaContext _localctx = new JoinCriteriaContext(_ctx, getState()); + enterRule(_localctx, 42, RULE_joinCriteria); + int _la; + try { + setState(450); + _errHandler.sync(this); + switch (_input.LA(1)) { + case ON: + enterOuterAlt(_localctx, 1); { + setState(436); + match(ON); + setState(437); + booleanExpression(0); + } + break; + case USING: + enterOuterAlt(_localctx, 2); { + setState(438); + match(USING); + setState(439); + match(T__0); + setState(440); + identifier(); + setState(445); + _errHandler.sync(this); + _la = _input.LA(1); + while (_la == T__2) { + { + { + setState(441); + match(T__2); + setState(442); + identifier(); + } + } + setState(447); + _errHandler.sync(this); + _la = _input.LA(1); + } + setState(448); + match(T__1); + } + break; + default: + throw new NoViableAltException(this); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - public ConstantDefaultContext(PrimaryExpressionContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterConstantDefault(this); + + public static class RelationPrimaryContext extends ParserRuleContext { + public RelationPrimaryContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_relationPrimary; + } + + public RelationPrimaryContext() {} + + public void copyFrom(RelationPrimaryContext ctx) { + super.copyFrom(ctx); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitConstantDefault(this); + + public static class AliasedRelationContext extends RelationPrimaryContext { + public RelationContext relation() { + return getRuleContext(RelationContext.class, 0); + } + + public QualifiedNameContext qualifiedName() { + return getRuleContext(QualifiedNameContext.class, 0); + } + + public TerminalNode AS() { + return getToken(SqlBaseParser.AS, 0); + } + + public AliasedRelationContext(RelationPrimaryContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterAliasedRelation(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitAliasedRelation(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitAliasedRelation(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitConstantDefault(this); - else return visitor.visitChildren(this); + + public static class AliasedQueryContext extends RelationPrimaryContext { + public QueryNoWithContext queryNoWith() { + return getRuleContext(QueryNoWithContext.class, 0); + } + + public QualifiedNameContext qualifiedName() { + return getRuleContext(QualifiedNameContext.class, 0); + } + + public TerminalNode AS() { + return getToken(SqlBaseParser.AS, 0); + } + + public AliasedQueryContext(RelationPrimaryContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterAliasedQuery(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitAliasedQuery(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitAliasedQuery(this); + else return visitor.visitChildren(this); + } } - } - public static class ExtractContext extends PrimaryExpressionContext { - public ExtractExpressionContext extractExpression() { - return getRuleContext(ExtractExpressionContext.class,0); + + public static class TableNameContext extends RelationPrimaryContext { + public TableIdentifierContext tableIdentifier() { + return getRuleContext(TableIdentifierContext.class, 0); + } + + public TerminalNode FROZEN() { + return getToken(SqlBaseParser.FROZEN, 0); + } + + public QualifiedNameContext qualifiedName() { + return getRuleContext(QualifiedNameContext.class, 0); + } + + public TerminalNode AS() { + return getToken(SqlBaseParser.AS, 0); + } + + public TableNameContext(RelationPrimaryContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterTableName(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitTableName(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitTableName(this); + else return visitor.visitChildren(this); + } } - public ExtractContext(PrimaryExpressionContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterExtract(this); + + public final RelationPrimaryContext relationPrimary() throws RecognitionException { + RelationPrimaryContext _localctx = new RelationPrimaryContext(_ctx, getState()); + enterRule(_localctx, 44, RULE_relationPrimary); + int _la; + try { + setState(480); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 69, _ctx)) { + case 1: + _localctx = new TableNameContext(_localctx); + enterOuterAlt(_localctx, 1); { + setState(453); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == FROZEN) { + { + setState(452); + match(FROZEN); + } + } + + setState(455); + tableIdentifier(); + setState(460); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 64, _ctx)) { + case 1: { + setState(457); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == AS) { + { + setState(456); + match(AS); + } + } + + setState(459); + qualifiedName(); + } + break; + } + } + break; + case 2: + _localctx = new AliasedQueryContext(_localctx); + enterOuterAlt(_localctx, 2); { + setState(462); + match(T__0); + setState(463); + queryNoWith(); + setState(464); + match(T__1); + setState(469); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 66, _ctx)) { + case 1: { + setState(466); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == AS) { + { + setState(465); + match(AS); + } + } + + setState(468); + qualifiedName(); + } + break; + } + } + break; + case 3: + _localctx = new AliasedRelationContext(_localctx); + enterOuterAlt(_localctx, 3); { + setState(471); + match(T__0); + setState(472); + relation(); + setState(473); + match(T__1); + setState(478); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 68, _ctx)) { + case 1: { + setState(475); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == AS) { + { + setState(474); + match(AS); + } + } + + setState(477); + qualifiedName(); + } + break; + } + } + break; + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitExtract(this); + + public static class PivotClauseContext extends ParserRuleContext { + public PivotArgsContext aggs; + public QualifiedNameContext column; + public PivotArgsContext vals; + + public TerminalNode PIVOT() { + return getToken(SqlBaseParser.PIVOT, 0); + } + + public TerminalNode FOR() { + return getToken(SqlBaseParser.FOR, 0); + } + + public TerminalNode IN() { + return getToken(SqlBaseParser.IN, 0); + } + + public List pivotArgs() { + return getRuleContexts(PivotArgsContext.class); + } + + public PivotArgsContext pivotArgs(int i) { + return getRuleContext(PivotArgsContext.class, i); + } + + public QualifiedNameContext qualifiedName() { + return getRuleContext(QualifiedNameContext.class, 0); + } + + public PivotClauseContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_pivotClause; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterPivotClause(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitPivotClause(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitPivotClause(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitExtract(this); - else return visitor.visitChildren(this); + + public final PivotClauseContext pivotClause() throws RecognitionException { + PivotClauseContext _localctx = new PivotClauseContext(_ctx, getState()); + enterRule(_localctx, 46, RULE_pivotClause); + try { + enterOuterAlt(_localctx, 1); + { + setState(482); + match(PIVOT); + setState(483); + match(T__0); + setState(484); + ((PivotClauseContext) _localctx).aggs = pivotArgs(); + setState(485); + match(FOR); + setState(486); + ((PivotClauseContext) _localctx).column = qualifiedName(); + setState(487); + match(IN); + setState(488); + match(T__0); + setState(489); + ((PivotClauseContext) _localctx).vals = pivotArgs(); + setState(490); + match(T__1); + setState(491); + match(T__1); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - } - public static class ParenthesizedExpressionContext extends PrimaryExpressionContext { - public ExpressionContext expression() { - return getRuleContext(ExpressionContext.class,0); + + public static class PivotArgsContext extends ParserRuleContext { + public List namedValueExpression() { + return getRuleContexts(NamedValueExpressionContext.class); + } + + public NamedValueExpressionContext namedValueExpression(int i) { + return getRuleContext(NamedValueExpressionContext.class, i); + } + + public PivotArgsContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_pivotArgs; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterPivotArgs(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitPivotArgs(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitPivotArgs(this); + else return visitor.visitChildren(this); + } } - public ParenthesizedExpressionContext(PrimaryExpressionContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterParenthesizedExpression(this); + + public final PivotArgsContext pivotArgs() throws RecognitionException { + PivotArgsContext _localctx = new PivotArgsContext(_ctx, getState()); + enterRule(_localctx, 48, RULE_pivotArgs); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(493); + namedValueExpression(); + setState(498); + _errHandler.sync(this); + _la = _input.LA(1); + while (_la == T__2) { + { + { + setState(494); + match(T__2); + setState(495); + namedValueExpression(); + } + } + setState(500); + _errHandler.sync(this); + _la = _input.LA(1); + } + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitParenthesizedExpression(this); + + public static class NamedValueExpressionContext extends ParserRuleContext { + public ValueExpressionContext valueExpression() { + return getRuleContext(ValueExpressionContext.class, 0); + } + + public IdentifierContext identifier() { + return getRuleContext(IdentifierContext.class, 0); + } + + public TerminalNode AS() { + return getToken(SqlBaseParser.AS, 0); + } + + public NamedValueExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_namedValueExpression; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterNamedValueExpression(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitNamedValueExpression(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitNamedValueExpression(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitParenthesizedExpression(this); - else return visitor.visitChildren(this); - } - } - public static class StarContext extends PrimaryExpressionContext { - public TerminalNode ASTERISK() { return getToken(SqlBaseParser.ASTERISK, 0); } - public QualifiedNameContext qualifiedName() { - return getRuleContext(QualifiedNameContext.class,0); - } - public TerminalNode DOT() { return getToken(SqlBaseParser.DOT, 0); } - public StarContext(PrimaryExpressionContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterStar(this); + + public final NamedValueExpressionContext namedValueExpression() throws RecognitionException { + NamedValueExpressionContext _localctx = new NamedValueExpressionContext(_ctx, getState()); + enterRule(_localctx, 50, RULE_namedValueExpression); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(501); + valueExpression(0); + setState(506); + _errHandler.sync(this); + _la = _input.LA(1); + if ((((_la) & ~0x3f) == 0 + && ((1L << _la) & ((1L << ANALYZE) | (1L << ANALYZED) | (1L << AS) | (1L << CATALOGS) | (1L << COLUMNS) | (1L + << CURRENT_DATE) | (1L << CURRENT_TIME) | (1L << CURRENT_TIMESTAMP) | (1L << DAY) | (1L << DEBUG) | (1L + << EXECUTABLE) | (1L << EXPLAIN) | (1L << FIRST) | (1L << FORMAT) | (1L << FULL) | (1L << FUNCTIONS) | (1L + << GRAPHVIZ) | (1L << HOUR) | (1L << INTERVAL) | (1L << LAST) | (1L << LIMIT) | (1L << MAPPED) | (1L + << MINUTE) | (1L << MONTH))) != 0) + || ((((_la - 70)) & ~0x3f) == 0 + && ((1L << (_la - 70)) & ((1L << (OPTIMIZED - 70)) | (1L << (PARSED - 70)) | (1L << (PHYSICAL - 70)) | (1L << (PIVOT + - 70)) | (1L << (PLAN - 70)) | (1L << (RLIKE - 70)) | (1L << (QUERY - 70)) | (1L << (SCHEMAS - 70)) | (1L + << (SECOND - 70)) | (1L << (SHOW - 70)) | (1L << (SYS - 70)) | (1L << (TABLES - 70)) | (1L << (TEXT - 70)) + | (1L << (TOP - 70)) | (1L << (TYPE - 70)) | (1L << (TYPES - 70)) | (1L << (VERIFY - 70)) | (1L << (YEAR - 70)) + | (1L << (IDENTIFIER - 70)) | (1L << (DIGIT_IDENTIFIER - 70)) | (1L << (QUOTED_IDENTIFIER - 70)))) != 0) + || _la == BACKQUOTED_IDENTIFIER) { + { + setState(503); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == AS) { + { + setState(502); + match(AS); + } + } + + setState(505); + identifier(); + } + } + + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitStar(this); + + public static class ExpressionContext extends ParserRuleContext { + public BooleanExpressionContext booleanExpression() { + return getRuleContext(BooleanExpressionContext.class, 0); + } + + public ExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_expression; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterExpression(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitExpression(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitExpression(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitStar(this); - else return visitor.visitChildren(this); + + public final ExpressionContext expression() throws RecognitionException { + ExpressionContext _localctx = new ExpressionContext(_ctx, getState()); + enterRule(_localctx, 52, RULE_expression); + try { + enterOuterAlt(_localctx, 1); + { + setState(508); + booleanExpression(0); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - } - public static class CastOperatorExpressionContext extends PrimaryExpressionContext { - public PrimaryExpressionContext primaryExpression() { - return getRuleContext(PrimaryExpressionContext.class,0); + + public static class BooleanExpressionContext extends ParserRuleContext { + public BooleanExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_booleanExpression; + } + + public BooleanExpressionContext() {} + + public void copyFrom(BooleanExpressionContext ctx) { + super.copyFrom(ctx); + } } - public TerminalNode CAST_OP() { return getToken(SqlBaseParser.CAST_OP, 0); } - public DataTypeContext dataType() { - return getRuleContext(DataTypeContext.class,0); + + public static class LogicalNotContext extends BooleanExpressionContext { + public TerminalNode NOT() { + return getToken(SqlBaseParser.NOT, 0); + } + + public BooleanExpressionContext booleanExpression() { + return getRuleContext(BooleanExpressionContext.class, 0); + } + + public LogicalNotContext(BooleanExpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterLogicalNot(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitLogicalNot(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitLogicalNot(this); + else return visitor.visitChildren(this); + } } - public CastOperatorExpressionContext(PrimaryExpressionContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterCastOperatorExpression(this); + + public static class StringQueryContext extends BooleanExpressionContext { + public StringContext queryString; + + public TerminalNode QUERY() { + return getToken(SqlBaseParser.QUERY, 0); + } + + public MatchQueryOptionsContext matchQueryOptions() { + return getRuleContext(MatchQueryOptionsContext.class, 0); + } + + public StringContext string() { + return getRuleContext(StringContext.class, 0); + } + + public StringQueryContext(BooleanExpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterStringQuery(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitStringQuery(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitStringQuery(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitCastOperatorExpression(this); + + public static class BooleanDefaultContext extends BooleanExpressionContext { + public PredicatedContext predicated() { + return getRuleContext(PredicatedContext.class, 0); + } + + public BooleanDefaultContext(BooleanExpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterBooleanDefault(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitBooleanDefault(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitBooleanDefault(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitCastOperatorExpression(this); - else return visitor.visitChildren(this); + + public static class ExistsContext extends BooleanExpressionContext { + public TerminalNode EXISTS() { + return getToken(SqlBaseParser.EXISTS, 0); + } + + public QueryContext query() { + return getRuleContext(QueryContext.class, 0); + } + + public ExistsContext(BooleanExpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterExists(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitExists(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitExists(this); + else return visitor.visitChildren(this); + } } - } - public static class FunctionContext extends PrimaryExpressionContext { - public FunctionExpressionContext functionExpression() { - return getRuleContext(FunctionExpressionContext.class,0); + + public static class MultiMatchQueryContext extends BooleanExpressionContext { + public StringContext multiFields; + public StringContext queryString; + + public TerminalNode MATCH() { + return getToken(SqlBaseParser.MATCH, 0); + } + + public MatchQueryOptionsContext matchQueryOptions() { + return getRuleContext(MatchQueryOptionsContext.class, 0); + } + + public List string() { + return getRuleContexts(StringContext.class); + } + + public StringContext string(int i) { + return getRuleContext(StringContext.class, i); + } + + public MultiMatchQueryContext(BooleanExpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterMultiMatchQuery(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitMultiMatchQuery(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitMultiMatchQuery(this); + else return visitor.visitChildren(this); + } } - public FunctionContext(PrimaryExpressionContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterFunction(this); + + public static class MatchQueryContext extends BooleanExpressionContext { + public QualifiedNameContext singleField; + public StringContext queryString; + + public TerminalNode MATCH() { + return getToken(SqlBaseParser.MATCH, 0); + } + + public MatchQueryOptionsContext matchQueryOptions() { + return getRuleContext(MatchQueryOptionsContext.class, 0); + } + + public QualifiedNameContext qualifiedName() { + return getRuleContext(QualifiedNameContext.class, 0); + } + + public StringContext string() { + return getRuleContext(StringContext.class, 0); + } + + public MatchQueryContext(BooleanExpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterMatchQuery(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitMatchQuery(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitMatchQuery(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitFunction(this); + + public static class LogicalBinaryContext extends BooleanExpressionContext { + public BooleanExpressionContext left; + public Token operator; + public BooleanExpressionContext right; + + public List booleanExpression() { + return getRuleContexts(BooleanExpressionContext.class); + } + + public BooleanExpressionContext booleanExpression(int i) { + return getRuleContext(BooleanExpressionContext.class, i); + } + + public TerminalNode AND() { + return getToken(SqlBaseParser.AND, 0); + } + + public TerminalNode OR() { + return getToken(SqlBaseParser.OR, 0); + } + + public LogicalBinaryContext(BooleanExpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterLogicalBinary(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitLogicalBinary(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitLogicalBinary(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitFunction(this); - else return visitor.visitChildren(this); + + public final BooleanExpressionContext booleanExpression() throws RecognitionException { + return booleanExpression(0); } - } - public static class CurrentDateTimeFunctionContext extends PrimaryExpressionContext { - public BuiltinDateTimeFunctionContext builtinDateTimeFunction() { - return getRuleContext(BuiltinDateTimeFunctionContext.class,0); + + private BooleanExpressionContext booleanExpression(int _p) throws RecognitionException { + ParserRuleContext _parentctx = _ctx; + int _parentState = getState(); + BooleanExpressionContext _localctx = new BooleanExpressionContext(_ctx, _parentState); + BooleanExpressionContext _prevctx = _localctx; + int _startState = 54; + enterRecursionRule(_localctx, 54, RULE_booleanExpression, _p); + try { + int _alt; + enterOuterAlt(_localctx, 1); + { + setState(541); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 73, _ctx)) { + case 1: { + _localctx = new LogicalNotContext(_localctx); + _ctx = _localctx; + _prevctx = _localctx; + + setState(511); + match(NOT); + setState(512); + booleanExpression(8); + } + break; + case 2: { + _localctx = new ExistsContext(_localctx); + _ctx = _localctx; + _prevctx = _localctx; + setState(513); + match(EXISTS); + setState(514); + match(T__0); + setState(515); + query(); + setState(516); + match(T__1); + } + break; + case 3: { + _localctx = new StringQueryContext(_localctx); + _ctx = _localctx; + _prevctx = _localctx; + setState(518); + match(QUERY); + setState(519); + match(T__0); + setState(520); + ((StringQueryContext) _localctx).queryString = string(); + setState(521); + matchQueryOptions(); + setState(522); + match(T__1); + } + break; + case 4: { + _localctx = new MatchQueryContext(_localctx); + _ctx = _localctx; + _prevctx = _localctx; + setState(524); + match(MATCH); + setState(525); + match(T__0); + setState(526); + ((MatchQueryContext) _localctx).singleField = qualifiedName(); + setState(527); + match(T__2); + setState(528); + ((MatchQueryContext) _localctx).queryString = string(); + setState(529); + matchQueryOptions(); + setState(530); + match(T__1); + } + break; + case 5: { + _localctx = new MultiMatchQueryContext(_localctx); + _ctx = _localctx; + _prevctx = _localctx; + setState(532); + match(MATCH); + setState(533); + match(T__0); + setState(534); + ((MultiMatchQueryContext) _localctx).multiFields = string(); + setState(535); + match(T__2); + setState(536); + ((MultiMatchQueryContext) _localctx).queryString = string(); + setState(537); + matchQueryOptions(); + setState(538); + match(T__1); + } + break; + case 6: { + _localctx = new BooleanDefaultContext(_localctx); + _ctx = _localctx; + _prevctx = _localctx; + setState(540); + predicated(); + } + break; + } + _ctx.stop = _input.LT(-1); + setState(551); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input, 75, _ctx); + while (_alt != 2 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER) { + if (_alt == 1) { + if (_parseListeners != null) triggerExitRuleEvent(); + _prevctx = _localctx; + { + setState(549); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 74, _ctx)) { + case 1: { + _localctx = new LogicalBinaryContext(new BooleanExpressionContext(_parentctx, _parentState)); + ((LogicalBinaryContext) _localctx).left = _prevctx; + pushNewRecursionContext(_localctx, _startState, RULE_booleanExpression); + setState(543); + if (!(precpred(_ctx, 2))) throw new FailedPredicateException(this, "precpred(_ctx, 2)"); + setState(544); + ((LogicalBinaryContext) _localctx).operator = match(AND); + setState(545); + ((LogicalBinaryContext) _localctx).right = booleanExpression(3); + } + break; + case 2: { + _localctx = new LogicalBinaryContext(new BooleanExpressionContext(_parentctx, _parentState)); + ((LogicalBinaryContext) _localctx).left = _prevctx; + pushNewRecursionContext(_localctx, _startState, RULE_booleanExpression); + setState(546); + if (!(precpred(_ctx, 1))) throw new FailedPredicateException(this, "precpred(_ctx, 1)"); + setState(547); + ((LogicalBinaryContext) _localctx).operator = match(OR); + setState(548); + ((LogicalBinaryContext) _localctx).right = booleanExpression(2); + } + break; + } + } + } + setState(553); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input, 75, _ctx); + } + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + unrollRecursionContexts(_parentctx); + } + return _localctx; } - public CurrentDateTimeFunctionContext(PrimaryExpressionContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterCurrentDateTimeFunction(this); + + public static class MatchQueryOptionsContext extends ParserRuleContext { + public List string() { + return getRuleContexts(StringContext.class); + } + + public StringContext string(int i) { + return getRuleContext(StringContext.class, i); + } + + public MatchQueryOptionsContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_matchQueryOptions; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterMatchQueryOptions(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitMatchQueryOptions(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitMatchQueryOptions(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitCurrentDateTimeFunction(this); + + public final MatchQueryOptionsContext matchQueryOptions() throws RecognitionException { + MatchQueryOptionsContext _localctx = new MatchQueryOptionsContext(_ctx, getState()); + enterRule(_localctx, 56, RULE_matchQueryOptions); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(558); + _errHandler.sync(this); + _la = _input.LA(1); + while (_la == T__2) { + { + { + setState(554); + match(T__2); + setState(555); + string(); + } + } + setState(560); + _errHandler.sync(this); + _la = _input.LA(1); + } + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitCurrentDateTimeFunction(this); - else return visitor.visitChildren(this); + + public static class PredicatedContext extends ParserRuleContext { + public ValueExpressionContext valueExpression() { + return getRuleContext(ValueExpressionContext.class, 0); + } + + public PredicateContext predicate() { + return getRuleContext(PredicateContext.class, 0); + } + + public PredicatedContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_predicated; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterPredicated(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitPredicated(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitPredicated(this); + else return visitor.visitChildren(this); + } } - } - public static class SubqueryExpressionContext extends PrimaryExpressionContext { - public QueryContext query() { - return getRuleContext(QueryContext.class,0); + + public final PredicatedContext predicated() throws RecognitionException { + PredicatedContext _localctx = new PredicatedContext(_ctx, getState()); + enterRule(_localctx, 58, RULE_predicated); + try { + enterOuterAlt(_localctx, 1); + { + setState(561); + valueExpression(0); + setState(563); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 77, _ctx)) { + case 1: { + setState(562); + predicate(); + } + break; + } + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - public SubqueryExpressionContext(PrimaryExpressionContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterSubqueryExpression(this); + + public static class PredicateContext extends ParserRuleContext { + public Token kind; + public ValueExpressionContext lower; + public ValueExpressionContext upper; + public StringContext regex; + + public TerminalNode AND() { + return getToken(SqlBaseParser.AND, 0); + } + + public TerminalNode BETWEEN() { + return getToken(SqlBaseParser.BETWEEN, 0); + } + + public List valueExpression() { + return getRuleContexts(ValueExpressionContext.class); + } + + public ValueExpressionContext valueExpression(int i) { + return getRuleContext(ValueExpressionContext.class, i); + } + + public TerminalNode NOT() { + return getToken(SqlBaseParser.NOT, 0); + } + + public TerminalNode IN() { + return getToken(SqlBaseParser.IN, 0); + } + + public QueryContext query() { + return getRuleContext(QueryContext.class, 0); + } + + public PatternContext pattern() { + return getRuleContext(PatternContext.class, 0); + } + + public TerminalNode LIKE() { + return getToken(SqlBaseParser.LIKE, 0); + } + + public TerminalNode RLIKE() { + return getToken(SqlBaseParser.RLIKE, 0); + } + + public StringContext string() { + return getRuleContext(StringContext.class, 0); + } + + public TerminalNode IS() { + return getToken(SqlBaseParser.IS, 0); + } + + public TerminalNode NULL() { + return getToken(SqlBaseParser.NULL, 0); + } + + public PredicateContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_predicate; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterPredicate(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitPredicate(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitPredicate(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitSubqueryExpression(this); + + public final PredicateContext predicate() throws RecognitionException { + PredicateContext _localctx = new PredicateContext(_ctx, getState()); + enterRule(_localctx, 60, RULE_predicate); + int _la; + try { + setState(611); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 85, _ctx)) { + case 1: + enterOuterAlt(_localctx, 1); { + setState(566); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == NOT) { + { + setState(565); + match(NOT); + } + } + + setState(568); + ((PredicateContext) _localctx).kind = match(BETWEEN); + setState(569); + ((PredicateContext) _localctx).lower = valueExpression(0); + setState(570); + match(AND); + setState(571); + ((PredicateContext) _localctx).upper = valueExpression(0); + } + break; + case 2: + enterOuterAlt(_localctx, 2); { + setState(574); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == NOT) { + { + setState(573); + match(NOT); + } + } + + setState(576); + ((PredicateContext) _localctx).kind = match(IN); + setState(577); + match(T__0); + setState(578); + valueExpression(0); + setState(583); + _errHandler.sync(this); + _la = _input.LA(1); + while (_la == T__2) { + { + { + setState(579); + match(T__2); + setState(580); + valueExpression(0); + } + } + setState(585); + _errHandler.sync(this); + _la = _input.LA(1); + } + setState(586); + match(T__1); + } + break; + case 3: + enterOuterAlt(_localctx, 3); { + setState(589); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == NOT) { + { + setState(588); + match(NOT); + } + } + + setState(591); + ((PredicateContext) _localctx).kind = match(IN); + setState(592); + match(T__0); + setState(593); + query(); + setState(594); + match(T__1); + } + break; + case 4: + enterOuterAlt(_localctx, 4); { + setState(597); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == NOT) { + { + setState(596); + match(NOT); + } + } + + setState(599); + ((PredicateContext) _localctx).kind = match(LIKE); + setState(600); + pattern(); + } + break; + case 5: + enterOuterAlt(_localctx, 5); { + setState(602); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == NOT) { + { + setState(601); + match(NOT); + } + } + + setState(604); + ((PredicateContext) _localctx).kind = match(RLIKE); + setState(605); + ((PredicateContext) _localctx).regex = string(); + } + break; + case 6: + enterOuterAlt(_localctx, 6); { + setState(606); + match(IS); + setState(608); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == NOT) { + { + setState(607); + match(NOT); + } + } + + setState(610); + ((PredicateContext) _localctx).kind = match(NULL); + } + break; + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitSubqueryExpression(this); - else return visitor.visitChildren(this); - } - } - public static class CaseContext extends PrimaryExpressionContext { - public BooleanExpressionContext operand; - public BooleanExpressionContext elseClause; - public TerminalNode CASE() { return getToken(SqlBaseParser.CASE, 0); } - public TerminalNode END() { return getToken(SqlBaseParser.END, 0); } - public List whenClause() { - return getRuleContexts(WhenClauseContext.class); - } - public WhenClauseContext whenClause(int i) { - return getRuleContext(WhenClauseContext.class,i); - } - public TerminalNode ELSE() { return getToken(SqlBaseParser.ELSE, 0); } - public List booleanExpression() { - return getRuleContexts(BooleanExpressionContext.class); - } - public BooleanExpressionContext booleanExpression(int i) { - return getRuleContext(BooleanExpressionContext.class,i); - } - public CaseContext(PrimaryExpressionContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterCase(this); + + public static class LikePatternContext extends ParserRuleContext { + public TerminalNode LIKE() { + return getToken(SqlBaseParser.LIKE, 0); + } + + public PatternContext pattern() { + return getRuleContext(PatternContext.class, 0); + } + + public LikePatternContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_likePattern; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterLikePattern(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitLikePattern(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitLikePattern(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitCase(this); + + public final LikePatternContext likePattern() throws RecognitionException { + LikePatternContext _localctx = new LikePatternContext(_ctx, getState()); + enterRule(_localctx, 62, RULE_likePattern); + try { + enterOuterAlt(_localctx, 1); + { + setState(613); + match(LIKE); + setState(614); + pattern(); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitCase(this); - else return visitor.visitChildren(this); - } - } - - public final PrimaryExpressionContext primaryExpression() throws RecognitionException { - return primaryExpression(0); - } - - private PrimaryExpressionContext primaryExpression(int _p) throws RecognitionException { - ParserRuleContext _parentctx = _ctx; - int _parentState = getState(); - PrimaryExpressionContext _localctx = new PrimaryExpressionContext(_ctx, _parentState); - PrimaryExpressionContext _prevctx = _localctx; - int _startState = 70; - enterRecursionRule(_localctx, 70, RULE_primaryExpression, _p); - int _la; - try { - int _alt; - enterOuterAlt(_localctx, 1); - { - setState(685); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,95,_ctx) ) { - case 1: - { - _localctx = new CastContext(_localctx); - _ctx = _localctx; - _prevctx = _localctx; - - setState(650); - castExpression(); - } - break; - case 2: - { - _localctx = new ExtractContext(_localctx); - _ctx = _localctx; - _prevctx = _localctx; - setState(651); - extractExpression(); - } - break; - case 3: - { - _localctx = new CurrentDateTimeFunctionContext(_localctx); - _ctx = _localctx; - _prevctx = _localctx; - setState(652); - builtinDateTimeFunction(); - } - break; - case 4: - { - _localctx = new ConstantDefaultContext(_localctx); - _ctx = _localctx; - _prevctx = _localctx; - setState(653); - constant(); - } - break; - case 5: - { - _localctx = new StarContext(_localctx); - _ctx = _localctx; - _prevctx = _localctx; - setState(657); - _errHandler.sync(this); - _la = _input.LA(1); - if ((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << ANALYZE) | (1L << ANALYZED) | (1L << CATALOGS) | (1L << COLUMNS) | (1L << CURRENT_DATE) | (1L << CURRENT_TIME) | (1L << CURRENT_TIMESTAMP) | (1L << DAY) | (1L << DEBUG) | (1L << EXECUTABLE) | (1L << EXPLAIN) | (1L << FIRST) | (1L << FORMAT) | (1L << FULL) | (1L << FUNCTIONS) | (1L << GRAPHVIZ) | (1L << HOUR) | (1L << INTERVAL) | (1L << LAST) | (1L << LIMIT) | (1L << MAPPED) | (1L << MINUTE) | (1L << MONTH))) != 0) || ((((_la - 70)) & ~0x3f) == 0 && ((1L << (_la - 70)) & ((1L << (OPTIMIZED - 70)) | (1L << (PARSED - 70)) | (1L << (PHYSICAL - 70)) | (1L << (PIVOT - 70)) | (1L << (PLAN - 70)) | (1L << (RLIKE - 70)) | (1L << (QUERY - 70)) | (1L << (SCHEMAS - 70)) | (1L << (SECOND - 70)) | (1L << (SHOW - 70)) | (1L << (SYS - 70)) | (1L << (TABLES - 70)) | (1L << (TEXT - 70)) | (1L << (TOP - 70)) | (1L << (TYPE - 70)) | (1L << (TYPES - 70)) | (1L << (VERIFY - 70)) | (1L << (YEAR - 70)) | (1L << (IDENTIFIER - 70)) | (1L << (DIGIT_IDENTIFIER - 70)) | (1L << (QUOTED_IDENTIFIER - 70)))) != 0) || _la==BACKQUOTED_IDENTIFIER) { - { - setState(654); - qualifiedName(); - setState(655); - match(DOT); - } - } - - setState(659); - match(ASTERISK); - } - break; - case 6: - { - _localctx = new FunctionContext(_localctx); - _ctx = _localctx; - _prevctx = _localctx; - setState(660); - functionExpression(); - } - break; - case 7: - { - _localctx = new SubqueryExpressionContext(_localctx); - _ctx = _localctx; - _prevctx = _localctx; - setState(661); - match(T__0); - setState(662); - query(); - setState(663); - match(T__1); - } - break; - case 8: - { - _localctx = new DereferenceContext(_localctx); - _ctx = _localctx; - _prevctx = _localctx; - setState(665); - qualifiedName(); - } - break; - case 9: - { - _localctx = new ParenthesizedExpressionContext(_localctx); - _ctx = _localctx; - _prevctx = _localctx; - setState(666); - match(T__0); - setState(667); - expression(); - setState(668); - match(T__1); - } - break; - case 10: - { - _localctx = new CaseContext(_localctx); - _ctx = _localctx; - _prevctx = _localctx; - setState(670); - match(CASE); - setState(672); - _errHandler.sync(this); - _la = _input.LA(1); - if ((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__0) | (1L << ANALYZE) | (1L << ANALYZED) | (1L << CASE) | (1L << CAST) | (1L << CATALOGS) | (1L << COLUMNS) | (1L << CONVERT) | (1L << CURRENT_DATE) | (1L << CURRENT_TIME) | (1L << CURRENT_TIMESTAMP) | (1L << DAY) | (1L << DEBUG) | (1L << EXECUTABLE) | (1L << EXISTS) | (1L << EXPLAIN) | (1L << EXTRACT) | (1L << FALSE) | (1L << FIRST) | (1L << FORMAT) | (1L << FULL) | (1L << FUNCTIONS) | (1L << GRAPHVIZ) | (1L << HOUR) | (1L << INTERVAL) | (1L << LAST) | (1L << LEFT) | (1L << LIMIT) | (1L << MAPPED) | (1L << MATCH) | (1L << MINUTE) | (1L << MONTH))) != 0) || ((((_la - 66)) & ~0x3f) == 0 && ((1L << (_la - 66)) & ((1L << (NOT - 66)) | (1L << (NULL - 66)) | (1L << (OPTIMIZED - 66)) | (1L << (PARSED - 66)) | (1L << (PHYSICAL - 66)) | (1L << (PIVOT - 66)) | (1L << (PLAN - 66)) | (1L << (RIGHT - 66)) | (1L << (RLIKE - 66)) | (1L << (QUERY - 66)) | (1L << (SCHEMAS - 66)) | (1L << (SECOND - 66)) | (1L << (SHOW - 66)) | (1L << (SYS - 66)) | (1L << (TABLES - 66)) | (1L << (TEXT - 66)) | (1L << (TRUE - 66)) | (1L << (TOP - 66)) | (1L << (TYPE - 66)) | (1L << (TYPES - 66)) | (1L << (VERIFY - 66)) | (1L << (YEAR - 66)) | (1L << (FUNCTION_ESC - 66)) | (1L << (DATE_ESC - 66)) | (1L << (TIME_ESC - 66)) | (1L << (TIMESTAMP_ESC - 66)) | (1L << (GUID_ESC - 66)) | (1L << (PLUS - 66)) | (1L << (MINUS - 66)) | (1L << (ASTERISK - 66)) | (1L << (PARAM - 66)) | (1L << (STRING - 66)) | (1L << (INTEGER_VALUE - 66)) | (1L << (DECIMAL_VALUE - 66)))) != 0) || ((((_la - 130)) & ~0x3f) == 0 && ((1L << (_la - 130)) & ((1L << (IDENTIFIER - 130)) | (1L << (DIGIT_IDENTIFIER - 130)) | (1L << (QUOTED_IDENTIFIER - 130)) | (1L << (BACKQUOTED_IDENTIFIER - 130)))) != 0)) { - { - setState(671); - ((CaseContext)_localctx).operand = booleanExpression(0); - } - } - - setState(675); - _errHandler.sync(this); - _la = _input.LA(1); - do { - { - { - setState(674); - whenClause(); - } - } - setState(677); - _errHandler.sync(this); - _la = _input.LA(1); - } while ( _la==WHEN ); - setState(681); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==ELSE) { - { - setState(679); - match(ELSE); - setState(680); - ((CaseContext)_localctx).elseClause = booleanExpression(0); - } - } - - setState(683); - match(END); - } - break; - } - _ctx.stop = _input.LT(-1); - setState(692); - _errHandler.sync(this); - _alt = getInterpreter().adaptivePredict(_input,96,_ctx); - while ( _alt!=2 && _alt!=org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER ) { - if ( _alt==1 ) { - if ( _parseListeners!=null ) triggerExitRuleEvent(); - _prevctx = _localctx; - { - { - _localctx = new CastOperatorExpressionContext(new PrimaryExpressionContext(_parentctx, _parentState)); - pushNewRecursionContext(_localctx, _startState, RULE_primaryExpression); - setState(687); - if (!(precpred(_ctx, 10))) throw new FailedPredicateException(this, "precpred(_ctx, 10)"); - setState(688); - match(CAST_OP); - setState(689); - dataType(); - } - } - } - setState(694); - _errHandler.sync(this); - _alt = getInterpreter().adaptivePredict(_input,96,_ctx); - } - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - unrollRecursionContexts(_parentctx); - } - return _localctx; - } - - public static class BuiltinDateTimeFunctionContext extends ParserRuleContext { - public Token name; - public TerminalNode CURRENT_TIMESTAMP() { return getToken(SqlBaseParser.CURRENT_TIMESTAMP, 0); } - public TerminalNode CURRENT_DATE() { return getToken(SqlBaseParser.CURRENT_DATE, 0); } - public TerminalNode CURRENT_TIME() { return getToken(SqlBaseParser.CURRENT_TIME, 0); } - public BuiltinDateTimeFunctionContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_builtinDateTimeFunction; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterBuiltinDateTimeFunction(this); + + public static class PatternContext extends ParserRuleContext { + public StringContext value; + + public StringContext string() { + return getRuleContext(StringContext.class, 0); + } + + public PatternEscapeContext patternEscape() { + return getRuleContext(PatternEscapeContext.class, 0); + } + + public PatternContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_pattern; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterPattern(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitPattern(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitPattern(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitBuiltinDateTimeFunction(this); + + public final PatternContext pattern() throws RecognitionException { + PatternContext _localctx = new PatternContext(_ctx, getState()); + enterRule(_localctx, 64, RULE_pattern); + try { + enterOuterAlt(_localctx, 1); + { + setState(616); + ((PatternContext) _localctx).value = string(); + setState(618); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 86, _ctx)) { + case 1: { + setState(617); + patternEscape(); + } + break; + } + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitBuiltinDateTimeFunction(this); - else return visitor.visitChildren(this); - } - } - - public final BuiltinDateTimeFunctionContext builtinDateTimeFunction() throws RecognitionException { - BuiltinDateTimeFunctionContext _localctx = new BuiltinDateTimeFunctionContext(_ctx, getState()); - enterRule(_localctx, 72, RULE_builtinDateTimeFunction); - try { - setState(698); - _errHandler.sync(this); - switch (_input.LA(1)) { - case CURRENT_TIMESTAMP: - enterOuterAlt(_localctx, 1); - { - setState(695); - ((BuiltinDateTimeFunctionContext)_localctx).name = match(CURRENT_TIMESTAMP); - } - break; - case CURRENT_DATE: - enterOuterAlt(_localctx, 2); - { - setState(696); - ((BuiltinDateTimeFunctionContext)_localctx).name = match(CURRENT_DATE); - } - break; - case CURRENT_TIME: - enterOuterAlt(_localctx, 3); - { - setState(697); - ((BuiltinDateTimeFunctionContext)_localctx).name = match(CURRENT_TIME); - } - break; - default: - throw new NoViableAltException(this); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class CastExpressionContext extends ParserRuleContext { - public CastTemplateContext castTemplate() { - return getRuleContext(CastTemplateContext.class,0); - } - public TerminalNode FUNCTION_ESC() { return getToken(SqlBaseParser.FUNCTION_ESC, 0); } - public TerminalNode ESC_END() { return getToken(SqlBaseParser.ESC_END, 0); } - public ConvertTemplateContext convertTemplate() { - return getRuleContext(ConvertTemplateContext.class,0); - } - public CastExpressionContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_castExpression; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterCastExpression(this); + + public static class PatternEscapeContext extends ParserRuleContext { + public StringContext escape; + + public TerminalNode ESCAPE() { + return getToken(SqlBaseParser.ESCAPE, 0); + } + + public StringContext string() { + return getRuleContext(StringContext.class, 0); + } + + public TerminalNode ESCAPE_ESC() { + return getToken(SqlBaseParser.ESCAPE_ESC, 0); + } + + public TerminalNode ESC_END() { + return getToken(SqlBaseParser.ESC_END, 0); + } + + public PatternEscapeContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_patternEscape; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterPatternEscape(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitPatternEscape(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitPatternEscape(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitCastExpression(this); + + public final PatternEscapeContext patternEscape() throws RecognitionException { + PatternEscapeContext _localctx = new PatternEscapeContext(_ctx, getState()); + enterRule(_localctx, 66, RULE_patternEscape); + try { + setState(626); + _errHandler.sync(this); + switch (_input.LA(1)) { + case ESCAPE: + enterOuterAlt(_localctx, 1); { + setState(620); + match(ESCAPE); + setState(621); + ((PatternEscapeContext) _localctx).escape = string(); + } + break; + case ESCAPE_ESC: + enterOuterAlt(_localctx, 2); { + setState(622); + match(ESCAPE_ESC); + setState(623); + ((PatternEscapeContext) _localctx).escape = string(); + setState(624); + match(ESC_END); + } + break; + default: + throw new NoViableAltException(this); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitCastExpression(this); - else return visitor.visitChildren(this); - } - } - - public final CastExpressionContext castExpression() throws RecognitionException { - CastExpressionContext _localctx = new CastExpressionContext(_ctx, getState()); - enterRule(_localctx, 74, RULE_castExpression); - try { - setState(710); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,98,_ctx) ) { - case 1: - enterOuterAlt(_localctx, 1); - { - setState(700); - castTemplate(); - } - break; - case 2: - enterOuterAlt(_localctx, 2); - { - setState(701); - match(FUNCTION_ESC); - setState(702); - castTemplate(); - setState(703); - match(ESC_END); - } - break; - case 3: - enterOuterAlt(_localctx, 3); - { - setState(705); - convertTemplate(); - } - break; - case 4: - enterOuterAlt(_localctx, 4); - { - setState(706); - match(FUNCTION_ESC); - setState(707); - convertTemplate(); - setState(708); - match(ESC_END); - } - break; - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class CastTemplateContext extends ParserRuleContext { - public TerminalNode CAST() { return getToken(SqlBaseParser.CAST, 0); } - public ExpressionContext expression() { - return getRuleContext(ExpressionContext.class,0); - } - public TerminalNode AS() { return getToken(SqlBaseParser.AS, 0); } - public DataTypeContext dataType() { - return getRuleContext(DataTypeContext.class,0); - } - public CastTemplateContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_castTemplate; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterCastTemplate(this); + + public static class ValueExpressionContext extends ParserRuleContext { + public ValueExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_valueExpression; + } + + public ValueExpressionContext() {} + + public void copyFrom(ValueExpressionContext ctx) { + super.copyFrom(ctx); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitCastTemplate(this); + + public static class ValueExpressionDefaultContext extends ValueExpressionContext { + public PrimaryExpressionContext primaryExpression() { + return getRuleContext(PrimaryExpressionContext.class, 0); + } + + public ValueExpressionDefaultContext(ValueExpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterValueExpressionDefault(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitValueExpressionDefault(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitValueExpressionDefault(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitCastTemplate(this); - else return visitor.visitChildren(this); - } - } - - public final CastTemplateContext castTemplate() throws RecognitionException { - CastTemplateContext _localctx = new CastTemplateContext(_ctx, getState()); - enterRule(_localctx, 76, RULE_castTemplate); - try { - enterOuterAlt(_localctx, 1); - { - setState(712); - match(CAST); - setState(713); - match(T__0); - setState(714); - expression(); - setState(715); - match(AS); - setState(716); - dataType(); - setState(717); - match(T__1); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class ConvertTemplateContext extends ParserRuleContext { - public TerminalNode CONVERT() { return getToken(SqlBaseParser.CONVERT, 0); } - public ExpressionContext expression() { - return getRuleContext(ExpressionContext.class,0); - } - public DataTypeContext dataType() { - return getRuleContext(DataTypeContext.class,0); - } - public ConvertTemplateContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_convertTemplate; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterConvertTemplate(this); + + public static class ComparisonContext extends ValueExpressionContext { + public ValueExpressionContext left; + public ValueExpressionContext right; + + public ComparisonOperatorContext comparisonOperator() { + return getRuleContext(ComparisonOperatorContext.class, 0); + } + + public List valueExpression() { + return getRuleContexts(ValueExpressionContext.class); + } + + public ValueExpressionContext valueExpression(int i) { + return getRuleContext(ValueExpressionContext.class, i); + } + + public ComparisonContext(ValueExpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterComparison(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitComparison(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitComparison(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitConvertTemplate(this); + + public static class ArithmeticBinaryContext extends ValueExpressionContext { + public ValueExpressionContext left; + public Token operator; + public ValueExpressionContext right; + + public List valueExpression() { + return getRuleContexts(ValueExpressionContext.class); + } + + public ValueExpressionContext valueExpression(int i) { + return getRuleContext(ValueExpressionContext.class, i); + } + + public TerminalNode ASTERISK() { + return getToken(SqlBaseParser.ASTERISK, 0); + } + + public TerminalNode SLASH() { + return getToken(SqlBaseParser.SLASH, 0); + } + + public TerminalNode PERCENT() { + return getToken(SqlBaseParser.PERCENT, 0); + } + + public TerminalNode PLUS() { + return getToken(SqlBaseParser.PLUS, 0); + } + + public TerminalNode MINUS() { + return getToken(SqlBaseParser.MINUS, 0); + } + + public ArithmeticBinaryContext(ValueExpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterArithmeticBinary(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitArithmeticBinary(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitArithmeticBinary(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitConvertTemplate(this); - else return visitor.visitChildren(this); - } - } - - public final ConvertTemplateContext convertTemplate() throws RecognitionException { - ConvertTemplateContext _localctx = new ConvertTemplateContext(_ctx, getState()); - enterRule(_localctx, 78, RULE_convertTemplate); - try { - enterOuterAlt(_localctx, 1); - { - setState(719); - match(CONVERT); - setState(720); - match(T__0); - setState(721); - expression(); - setState(722); - match(T__2); - setState(723); - dataType(); - setState(724); - match(T__1); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class ExtractExpressionContext extends ParserRuleContext { - public ExtractTemplateContext extractTemplate() { - return getRuleContext(ExtractTemplateContext.class,0); - } - public TerminalNode FUNCTION_ESC() { return getToken(SqlBaseParser.FUNCTION_ESC, 0); } - public TerminalNode ESC_END() { return getToken(SqlBaseParser.ESC_END, 0); } - public ExtractExpressionContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_extractExpression; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterExtractExpression(this); + + public static class ArithmeticUnaryContext extends ValueExpressionContext { + public Token operator; + + public ValueExpressionContext valueExpression() { + return getRuleContext(ValueExpressionContext.class, 0); + } + + public TerminalNode MINUS() { + return getToken(SqlBaseParser.MINUS, 0); + } + + public TerminalNode PLUS() { + return getToken(SqlBaseParser.PLUS, 0); + } + + public ArithmeticUnaryContext(ValueExpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterArithmeticUnary(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitArithmeticUnary(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitArithmeticUnary(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitExtractExpression(this); + + public final ValueExpressionContext valueExpression() throws RecognitionException { + return valueExpression(0); } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitExtractExpression(this); - else return visitor.visitChildren(this); - } - } - - public final ExtractExpressionContext extractExpression() throws RecognitionException { - ExtractExpressionContext _localctx = new ExtractExpressionContext(_ctx, getState()); - enterRule(_localctx, 80, RULE_extractExpression); - try { - setState(731); - _errHandler.sync(this); - switch (_input.LA(1)) { - case EXTRACT: - enterOuterAlt(_localctx, 1); - { - setState(726); - extractTemplate(); - } - break; - case FUNCTION_ESC: - enterOuterAlt(_localctx, 2); - { - setState(727); - match(FUNCTION_ESC); - setState(728); - extractTemplate(); - setState(729); - match(ESC_END); - } - break; - default: - throw new NoViableAltException(this); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class ExtractTemplateContext extends ParserRuleContext { - public IdentifierContext field; - public TerminalNode EXTRACT() { return getToken(SqlBaseParser.EXTRACT, 0); } - public TerminalNode FROM() { return getToken(SqlBaseParser.FROM, 0); } - public ValueExpressionContext valueExpression() { - return getRuleContext(ValueExpressionContext.class,0); - } - public IdentifierContext identifier() { - return getRuleContext(IdentifierContext.class,0); - } - public ExtractTemplateContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_extractTemplate; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterExtractTemplate(this); + + private ValueExpressionContext valueExpression(int _p) throws RecognitionException { + ParserRuleContext _parentctx = _ctx; + int _parentState = getState(); + ValueExpressionContext _localctx = new ValueExpressionContext(_ctx, _parentState); + ValueExpressionContext _prevctx = _localctx; + int _startState = 68; + enterRecursionRule(_localctx, 68, RULE_valueExpression, _p); + int _la; + try { + int _alt; + enterOuterAlt(_localctx, 1); + { + setState(632); + _errHandler.sync(this); + switch (_input.LA(1)) { + case T__0: + case ANALYZE: + case ANALYZED: + case CASE: + case CAST: + case CATALOGS: + case COLUMNS: + case CONVERT: + case CURRENT_DATE: + case CURRENT_TIME: + case CURRENT_TIMESTAMP: + case DAY: + case DEBUG: + case EXECUTABLE: + case EXPLAIN: + case EXTRACT: + case FALSE: + case FIRST: + case FORMAT: + case FULL: + case FUNCTIONS: + case GRAPHVIZ: + case HOUR: + case INTERVAL: + case LAST: + case LEFT: + case LIMIT: + case MAPPED: + case MINUTE: + case MONTH: + case NULL: + case OPTIMIZED: + case PARSED: + case PHYSICAL: + case PIVOT: + case PLAN: + case RIGHT: + case RLIKE: + case QUERY: + case SCHEMAS: + case SECOND: + case SHOW: + case SYS: + case TABLES: + case TEXT: + case TRUE: + case TOP: + case TYPE: + case TYPES: + case VERIFY: + case YEAR: + case FUNCTION_ESC: + case DATE_ESC: + case TIME_ESC: + case TIMESTAMP_ESC: + case GUID_ESC: + case ASTERISK: + case PARAM: + case STRING: + case INTEGER_VALUE: + case DECIMAL_VALUE: + case IDENTIFIER: + case DIGIT_IDENTIFIER: + case QUOTED_IDENTIFIER: + case BACKQUOTED_IDENTIFIER: { + _localctx = new ValueExpressionDefaultContext(_localctx); + _ctx = _localctx; + _prevctx = _localctx; + + setState(629); + primaryExpression(0); + } + break; + case PLUS: + case MINUS: { + _localctx = new ArithmeticUnaryContext(_localctx); + _ctx = _localctx; + _prevctx = _localctx; + setState(630); + ((ArithmeticUnaryContext) _localctx).operator = _input.LT(1); + _la = _input.LA(1); + if (!(_la == PLUS || _la == MINUS)) { + ((ArithmeticUnaryContext) _localctx).operator = (Token) _errHandler.recoverInline(this); + } else { + if (_input.LA(1) == Token.EOF) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + setState(631); + valueExpression(4); + } + break; + default: + throw new NoViableAltException(this); + } + _ctx.stop = _input.LT(-1); + setState(646); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input, 90, _ctx); + while (_alt != 2 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER) { + if (_alt == 1) { + if (_parseListeners != null) triggerExitRuleEvent(); + _prevctx = _localctx; + { + setState(644); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 89, _ctx)) { + case 1: { + _localctx = new ArithmeticBinaryContext(new ValueExpressionContext(_parentctx, _parentState)); + ((ArithmeticBinaryContext) _localctx).left = _prevctx; + pushNewRecursionContext(_localctx, _startState, RULE_valueExpression); + setState(634); + if (!(precpred(_ctx, 3))) throw new FailedPredicateException(this, "precpred(_ctx, 3)"); + setState(635); + ((ArithmeticBinaryContext) _localctx).operator = _input.LT(1); + _la = _input.LA(1); + if (!(((((_la - 121)) & ~0x3f) == 0 + && ((1L << (_la - 121)) & ((1L << (ASTERISK - 121)) | (1L << (SLASH - 121)) | (1L << (PERCENT + - 121)))) != 0))) { + ((ArithmeticBinaryContext) _localctx).operator = (Token) _errHandler.recoverInline(this); + } else { + if (_input.LA(1) == Token.EOF) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + setState(636); + ((ArithmeticBinaryContext) _localctx).right = valueExpression(4); + } + break; + case 2: { + _localctx = new ArithmeticBinaryContext(new ValueExpressionContext(_parentctx, _parentState)); + ((ArithmeticBinaryContext) _localctx).left = _prevctx; + pushNewRecursionContext(_localctx, _startState, RULE_valueExpression); + setState(637); + if (!(precpred(_ctx, 2))) throw new FailedPredicateException(this, "precpred(_ctx, 2)"); + setState(638); + ((ArithmeticBinaryContext) _localctx).operator = _input.LT(1); + _la = _input.LA(1); + if (!(_la == PLUS || _la == MINUS)) { + ((ArithmeticBinaryContext) _localctx).operator = (Token) _errHandler.recoverInline(this); + } else { + if (_input.LA(1) == Token.EOF) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + setState(639); + ((ArithmeticBinaryContext) _localctx).right = valueExpression(3); + } + break; + case 3: { + _localctx = new ComparisonContext(new ValueExpressionContext(_parentctx, _parentState)); + ((ComparisonContext) _localctx).left = _prevctx; + pushNewRecursionContext(_localctx, _startState, RULE_valueExpression); + setState(640); + if (!(precpred(_ctx, 1))) throw new FailedPredicateException(this, "precpred(_ctx, 1)"); + setState(641); + comparisonOperator(); + setState(642); + ((ComparisonContext) _localctx).right = valueExpression(2); + } + break; + } + } + } + setState(648); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input, 90, _ctx); + } + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + unrollRecursionContexts(_parentctx); + } + return _localctx; } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitExtractTemplate(this); + + public static class PrimaryExpressionContext extends ParserRuleContext { + public PrimaryExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_primaryExpression; + } + + public PrimaryExpressionContext() {} + + public void copyFrom(PrimaryExpressionContext ctx) { + super.copyFrom(ctx); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitExtractTemplate(this); - else return visitor.visitChildren(this); - } - } - - public final ExtractTemplateContext extractTemplate() throws RecognitionException { - ExtractTemplateContext _localctx = new ExtractTemplateContext(_ctx, getState()); - enterRule(_localctx, 82, RULE_extractTemplate); - try { - enterOuterAlt(_localctx, 1); - { - setState(733); - match(EXTRACT); - setState(734); - match(T__0); - setState(735); - ((ExtractTemplateContext)_localctx).field = identifier(); - setState(736); - match(FROM); - setState(737); - valueExpression(0); - setState(738); - match(T__1); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class FunctionExpressionContext extends ParserRuleContext { - public FunctionTemplateContext functionTemplate() { - return getRuleContext(FunctionTemplateContext.class,0); - } - public TerminalNode FUNCTION_ESC() { return getToken(SqlBaseParser.FUNCTION_ESC, 0); } - public TerminalNode ESC_END() { return getToken(SqlBaseParser.ESC_END, 0); } - public FunctionExpressionContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_functionExpression; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterFunctionExpression(this); + + public static class DereferenceContext extends PrimaryExpressionContext { + public QualifiedNameContext qualifiedName() { + return getRuleContext(QualifiedNameContext.class, 0); + } + + public DereferenceContext(PrimaryExpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterDereference(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitDereference(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitDereference(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitFunctionExpression(this); + + public static class CastContext extends PrimaryExpressionContext { + public CastExpressionContext castExpression() { + return getRuleContext(CastExpressionContext.class, 0); + } + + public CastContext(PrimaryExpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterCast(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitCast(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitCast(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitFunctionExpression(this); - else return visitor.visitChildren(this); - } - } - - public final FunctionExpressionContext functionExpression() throws RecognitionException { - FunctionExpressionContext _localctx = new FunctionExpressionContext(_ctx, getState()); - enterRule(_localctx, 84, RULE_functionExpression); - try { - setState(745); - _errHandler.sync(this); - switch (_input.LA(1)) { - case ANALYZE: - case ANALYZED: - case CATALOGS: - case COLUMNS: - case CURRENT_DATE: - case CURRENT_TIME: - case CURRENT_TIMESTAMP: - case DAY: - case DEBUG: - case EXECUTABLE: - case EXPLAIN: - case FIRST: - case FORMAT: - case FULL: - case FUNCTIONS: - case GRAPHVIZ: - case HOUR: - case INTERVAL: - case LAST: - case LEFT: - case LIMIT: - case MAPPED: - case MINUTE: - case MONTH: - case OPTIMIZED: - case PARSED: - case PHYSICAL: - case PIVOT: - case PLAN: - case RIGHT: - case RLIKE: - case QUERY: - case SCHEMAS: - case SECOND: - case SHOW: - case SYS: - case TABLES: - case TEXT: - case TOP: - case TYPE: - case TYPES: - case VERIFY: - case YEAR: - case IDENTIFIER: - case DIGIT_IDENTIFIER: - case QUOTED_IDENTIFIER: - case BACKQUOTED_IDENTIFIER: - enterOuterAlt(_localctx, 1); - { - setState(740); - functionTemplate(); - } - break; - case FUNCTION_ESC: - enterOuterAlt(_localctx, 2); - { - setState(741); - match(FUNCTION_ESC); - setState(742); - functionTemplate(); - setState(743); - match(ESC_END); - } - break; - default: - throw new NoViableAltException(this); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class FunctionTemplateContext extends ParserRuleContext { - public FunctionNameContext functionName() { - return getRuleContext(FunctionNameContext.class,0); - } - public List expression() { - return getRuleContexts(ExpressionContext.class); - } - public ExpressionContext expression(int i) { - return getRuleContext(ExpressionContext.class,i); - } - public SetQuantifierContext setQuantifier() { - return getRuleContext(SetQuantifierContext.class,0); - } - public FunctionTemplateContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_functionTemplate; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterFunctionTemplate(this); + + public static class ConstantDefaultContext extends PrimaryExpressionContext { + public ConstantContext constant() { + return getRuleContext(ConstantContext.class, 0); + } + + public ConstantDefaultContext(PrimaryExpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterConstantDefault(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitConstantDefault(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitConstantDefault(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitFunctionTemplate(this); + + public static class ExtractContext extends PrimaryExpressionContext { + public ExtractExpressionContext extractExpression() { + return getRuleContext(ExtractExpressionContext.class, 0); + } + + public ExtractContext(PrimaryExpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterExtract(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitExtract(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitExtract(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitFunctionTemplate(this); - else return visitor.visitChildren(this); - } - } - - public final FunctionTemplateContext functionTemplate() throws RecognitionException { - FunctionTemplateContext _localctx = new FunctionTemplateContext(_ctx, getState()); - enterRule(_localctx, 86, RULE_functionTemplate); - int _la; - try { - enterOuterAlt(_localctx, 1); - { - setState(747); - functionName(); - setState(748); - match(T__0); - setState(760); - _errHandler.sync(this); - _la = _input.LA(1); - if ((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__0) | (1L << ALL) | (1L << ANALYZE) | (1L << ANALYZED) | (1L << CASE) | (1L << CAST) | (1L << CATALOGS) | (1L << COLUMNS) | (1L << CONVERT) | (1L << CURRENT_DATE) | (1L << CURRENT_TIME) | (1L << CURRENT_TIMESTAMP) | (1L << DAY) | (1L << DEBUG) | (1L << DISTINCT) | (1L << EXECUTABLE) | (1L << EXISTS) | (1L << EXPLAIN) | (1L << EXTRACT) | (1L << FALSE) | (1L << FIRST) | (1L << FORMAT) | (1L << FULL) | (1L << FUNCTIONS) | (1L << GRAPHVIZ) | (1L << HOUR) | (1L << INTERVAL) | (1L << LAST) | (1L << LEFT) | (1L << LIMIT) | (1L << MAPPED) | (1L << MATCH) | (1L << MINUTE) | (1L << MONTH))) != 0) || ((((_la - 66)) & ~0x3f) == 0 && ((1L << (_la - 66)) & ((1L << (NOT - 66)) | (1L << (NULL - 66)) | (1L << (OPTIMIZED - 66)) | (1L << (PARSED - 66)) | (1L << (PHYSICAL - 66)) | (1L << (PIVOT - 66)) | (1L << (PLAN - 66)) | (1L << (RIGHT - 66)) | (1L << (RLIKE - 66)) | (1L << (QUERY - 66)) | (1L << (SCHEMAS - 66)) | (1L << (SECOND - 66)) | (1L << (SHOW - 66)) | (1L << (SYS - 66)) | (1L << (TABLES - 66)) | (1L << (TEXT - 66)) | (1L << (TRUE - 66)) | (1L << (TOP - 66)) | (1L << (TYPE - 66)) | (1L << (TYPES - 66)) | (1L << (VERIFY - 66)) | (1L << (YEAR - 66)) | (1L << (FUNCTION_ESC - 66)) | (1L << (DATE_ESC - 66)) | (1L << (TIME_ESC - 66)) | (1L << (TIMESTAMP_ESC - 66)) | (1L << (GUID_ESC - 66)) | (1L << (PLUS - 66)) | (1L << (MINUS - 66)) | (1L << (ASTERISK - 66)) | (1L << (PARAM - 66)) | (1L << (STRING - 66)) | (1L << (INTEGER_VALUE - 66)) | (1L << (DECIMAL_VALUE - 66)))) != 0) || ((((_la - 130)) & ~0x3f) == 0 && ((1L << (_la - 130)) & ((1L << (IDENTIFIER - 130)) | (1L << (DIGIT_IDENTIFIER - 130)) | (1L << (QUOTED_IDENTIFIER - 130)) | (1L << (BACKQUOTED_IDENTIFIER - 130)))) != 0)) { - { - setState(750); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==ALL || _la==DISTINCT) { - { - setState(749); - setQuantifier(); - } - } - - setState(752); - expression(); - setState(757); - _errHandler.sync(this); - _la = _input.LA(1); - while (_la==T__2) { - { - { - setState(753); - match(T__2); - setState(754); - expression(); - } - } - setState(759); - _errHandler.sync(this); - _la = _input.LA(1); - } - } - } - - setState(762); - match(T__1); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class FunctionNameContext extends ParserRuleContext { - public TerminalNode LEFT() { return getToken(SqlBaseParser.LEFT, 0); } - public TerminalNode RIGHT() { return getToken(SqlBaseParser.RIGHT, 0); } - public IdentifierContext identifier() { - return getRuleContext(IdentifierContext.class,0); - } - public FunctionNameContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_functionName; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterFunctionName(this); + + public static class ParenthesizedExpressionContext extends PrimaryExpressionContext { + public ExpressionContext expression() { + return getRuleContext(ExpressionContext.class, 0); + } + + public ParenthesizedExpressionContext(PrimaryExpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterParenthesizedExpression(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitParenthesizedExpression(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitParenthesizedExpression(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitFunctionName(this); + + public static class StarContext extends PrimaryExpressionContext { + public TerminalNode ASTERISK() { + return getToken(SqlBaseParser.ASTERISK, 0); + } + + public QualifiedNameContext qualifiedName() { + return getRuleContext(QualifiedNameContext.class, 0); + } + + public TerminalNode DOT() { + return getToken(SqlBaseParser.DOT, 0); + } + + public StarContext(PrimaryExpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterStar(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitStar(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitStar(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitFunctionName(this); - else return visitor.visitChildren(this); - } - } - - public final FunctionNameContext functionName() throws RecognitionException { - FunctionNameContext _localctx = new FunctionNameContext(_ctx, getState()); - enterRule(_localctx, 88, RULE_functionName); - try { - setState(767); - _errHandler.sync(this); - switch (_input.LA(1)) { - case LEFT: - enterOuterAlt(_localctx, 1); - { - setState(764); - match(LEFT); - } - break; - case RIGHT: - enterOuterAlt(_localctx, 2); - { - setState(765); - match(RIGHT); - } - break; - case ANALYZE: - case ANALYZED: - case CATALOGS: - case COLUMNS: - case CURRENT_DATE: - case CURRENT_TIME: - case CURRENT_TIMESTAMP: - case DAY: - case DEBUG: - case EXECUTABLE: - case EXPLAIN: - case FIRST: - case FORMAT: - case FULL: - case FUNCTIONS: - case GRAPHVIZ: - case HOUR: - case INTERVAL: - case LAST: - case LIMIT: - case MAPPED: - case MINUTE: - case MONTH: - case OPTIMIZED: - case PARSED: - case PHYSICAL: - case PIVOT: - case PLAN: - case RLIKE: - case QUERY: - case SCHEMAS: - case SECOND: - case SHOW: - case SYS: - case TABLES: - case TEXT: - case TOP: - case TYPE: - case TYPES: - case VERIFY: - case YEAR: - case IDENTIFIER: - case DIGIT_IDENTIFIER: - case QUOTED_IDENTIFIER: - case BACKQUOTED_IDENTIFIER: - enterOuterAlt(_localctx, 3); - { - setState(766); - identifier(); - } - break; - default: - throw new NoViableAltException(this); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class ConstantContext extends ParserRuleContext { - public ConstantContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_constant; } - - public ConstantContext() { } - public void copyFrom(ConstantContext ctx) { - super.copyFrom(ctx); - } - } - public static class NullLiteralContext extends ConstantContext { - public TerminalNode NULL() { return getToken(SqlBaseParser.NULL, 0); } - public NullLiteralContext(ConstantContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterNullLiteral(this); + + public static class CastOperatorExpressionContext extends PrimaryExpressionContext { + public PrimaryExpressionContext primaryExpression() { + return getRuleContext(PrimaryExpressionContext.class, 0); + } + + public TerminalNode CAST_OP() { + return getToken(SqlBaseParser.CAST_OP, 0); + } + + public DataTypeContext dataType() { + return getRuleContext(DataTypeContext.class, 0); + } + + public CastOperatorExpressionContext(PrimaryExpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterCastOperatorExpression(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitCastOperatorExpression(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitCastOperatorExpression(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitNullLiteral(this); + + public static class FunctionContext extends PrimaryExpressionContext { + public FunctionExpressionContext functionExpression() { + return getRuleContext(FunctionExpressionContext.class, 0); + } + + public FunctionContext(PrimaryExpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterFunction(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitFunction(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitFunction(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitNullLiteral(this); - else return visitor.visitChildren(this); - } - } - public static class TimestampEscapedLiteralContext extends ConstantContext { - public TerminalNode TIMESTAMP_ESC() { return getToken(SqlBaseParser.TIMESTAMP_ESC, 0); } - public StringContext string() { - return getRuleContext(StringContext.class,0); - } - public TerminalNode ESC_END() { return getToken(SqlBaseParser.ESC_END, 0); } - public TimestampEscapedLiteralContext(ConstantContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterTimestampEscapedLiteral(this); + + public static class CurrentDateTimeFunctionContext extends PrimaryExpressionContext { + public BuiltinDateTimeFunctionContext builtinDateTimeFunction() { + return getRuleContext(BuiltinDateTimeFunctionContext.class, 0); + } + + public CurrentDateTimeFunctionContext(PrimaryExpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterCurrentDateTimeFunction(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitCurrentDateTimeFunction(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitCurrentDateTimeFunction(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitTimestampEscapedLiteral(this); + + public static class SubqueryExpressionContext extends PrimaryExpressionContext { + public QueryContext query() { + return getRuleContext(QueryContext.class, 0); + } + + public SubqueryExpressionContext(PrimaryExpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterSubqueryExpression(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitSubqueryExpression(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitSubqueryExpression(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitTimestampEscapedLiteral(this); - else return visitor.visitChildren(this); + + public static class CaseContext extends PrimaryExpressionContext { + public BooleanExpressionContext operand; + public BooleanExpressionContext elseClause; + + public TerminalNode CASE() { + return getToken(SqlBaseParser.CASE, 0); + } + + public TerminalNode END() { + return getToken(SqlBaseParser.END, 0); + } + + public List whenClause() { + return getRuleContexts(WhenClauseContext.class); + } + + public WhenClauseContext whenClause(int i) { + return getRuleContext(WhenClauseContext.class, i); + } + + public TerminalNode ELSE() { + return getToken(SqlBaseParser.ELSE, 0); + } + + public List booleanExpression() { + return getRuleContexts(BooleanExpressionContext.class); + } + + public BooleanExpressionContext booleanExpression(int i) { + return getRuleContext(BooleanExpressionContext.class, i); + } + + public CaseContext(PrimaryExpressionContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterCase(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitCase(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitCase(this); + else return visitor.visitChildren(this); + } } - } - public static class StringLiteralContext extends ConstantContext { - public List STRING() { return getTokens(SqlBaseParser.STRING); } - public TerminalNode STRING(int i) { - return getToken(SqlBaseParser.STRING, i); + + public final PrimaryExpressionContext primaryExpression() throws RecognitionException { + return primaryExpression(0); } - public StringLiteralContext(ConstantContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterStringLiteral(this); + + private PrimaryExpressionContext primaryExpression(int _p) throws RecognitionException { + ParserRuleContext _parentctx = _ctx; + int _parentState = getState(); + PrimaryExpressionContext _localctx = new PrimaryExpressionContext(_ctx, _parentState); + PrimaryExpressionContext _prevctx = _localctx; + int _startState = 70; + enterRecursionRule(_localctx, 70, RULE_primaryExpression, _p); + int _la; + try { + int _alt; + enterOuterAlt(_localctx, 1); + { + setState(685); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 95, _ctx)) { + case 1: { + _localctx = new CastContext(_localctx); + _ctx = _localctx; + _prevctx = _localctx; + + setState(650); + castExpression(); + } + break; + case 2: { + _localctx = new ExtractContext(_localctx); + _ctx = _localctx; + _prevctx = _localctx; + setState(651); + extractExpression(); + } + break; + case 3: { + _localctx = new CurrentDateTimeFunctionContext(_localctx); + _ctx = _localctx; + _prevctx = _localctx; + setState(652); + builtinDateTimeFunction(); + } + break; + case 4: { + _localctx = new ConstantDefaultContext(_localctx); + _ctx = _localctx; + _prevctx = _localctx; + setState(653); + constant(); + } + break; + case 5: { + _localctx = new StarContext(_localctx); + _ctx = _localctx; + _prevctx = _localctx; + setState(657); + _errHandler.sync(this); + _la = _input.LA(1); + if ((((_la) & ~0x3f) == 0 + && ((1L << _la) & ((1L << ANALYZE) | (1L << ANALYZED) | (1L << CATALOGS) | (1L << COLUMNS) | (1L + << CURRENT_DATE) | (1L << CURRENT_TIME) | (1L << CURRENT_TIMESTAMP) | (1L << DAY) | (1L << DEBUG) | (1L + << EXECUTABLE) | (1L << EXPLAIN) | (1L << FIRST) | (1L << FORMAT) | (1L << FULL) | (1L << FUNCTIONS) + | (1L << GRAPHVIZ) | (1L << HOUR) | (1L << INTERVAL) | (1L << LAST) | (1L << LIMIT) | (1L << MAPPED) | (1L + << MINUTE) | (1L << MONTH))) != 0) + || ((((_la - 70)) & ~0x3f) == 0 + && ((1L << (_la - 70)) & ((1L << (OPTIMIZED - 70)) | (1L << (PARSED - 70)) | (1L << (PHYSICAL - 70)) | (1L + << (PIVOT - 70)) | (1L << (PLAN - 70)) | (1L << (RLIKE - 70)) | (1L << (QUERY - 70)) | (1L << (SCHEMAS + - 70)) | (1L << (SECOND - 70)) | (1L << (SHOW - 70)) | (1L << (SYS - 70)) | (1L << (TABLES - 70)) + | (1L << (TEXT - 70)) | (1L << (TOP - 70)) | (1L << (TYPE - 70)) | (1L << (TYPES - 70)) | (1L << (VERIFY + - 70)) | (1L << (YEAR - 70)) | (1L << (IDENTIFIER - 70)) | (1L << (DIGIT_IDENTIFIER - 70)) | (1L + << (QUOTED_IDENTIFIER - 70)))) != 0) + || _la == BACKQUOTED_IDENTIFIER) { + { + setState(654); + qualifiedName(); + setState(655); + match(DOT); + } + } + + setState(659); + match(ASTERISK); + } + break; + case 6: { + _localctx = new FunctionContext(_localctx); + _ctx = _localctx; + _prevctx = _localctx; + setState(660); + functionExpression(); + } + break; + case 7: { + _localctx = new SubqueryExpressionContext(_localctx); + _ctx = _localctx; + _prevctx = _localctx; + setState(661); + match(T__0); + setState(662); + query(); + setState(663); + match(T__1); + } + break; + case 8: { + _localctx = new DereferenceContext(_localctx); + _ctx = _localctx; + _prevctx = _localctx; + setState(665); + qualifiedName(); + } + break; + case 9: { + _localctx = new ParenthesizedExpressionContext(_localctx); + _ctx = _localctx; + _prevctx = _localctx; + setState(666); + match(T__0); + setState(667); + expression(); + setState(668); + match(T__1); + } + break; + case 10: { + _localctx = new CaseContext(_localctx); + _ctx = _localctx; + _prevctx = _localctx; + setState(670); + match(CASE); + setState(672); + _errHandler.sync(this); + _la = _input.LA(1); + if ((((_la) & ~0x3f) == 0 + && ((1L << _la) & ((1L << T__0) | (1L << ANALYZE) | (1L << ANALYZED) | (1L << CASE) | (1L << CAST) | (1L + << CATALOGS) | (1L << COLUMNS) | (1L << CONVERT) | (1L << CURRENT_DATE) | (1L << CURRENT_TIME) | (1L + << CURRENT_TIMESTAMP) | (1L << DAY) | (1L << DEBUG) | (1L << EXECUTABLE) | (1L << EXISTS) | (1L + << EXPLAIN) | (1L << EXTRACT) | (1L << FALSE) | (1L << FIRST) | (1L << FORMAT) | (1L << FULL) | (1L + << FUNCTIONS) | (1L << GRAPHVIZ) | (1L << HOUR) | (1L << INTERVAL) | (1L << LAST) | (1L << LEFT) + | (1L << LIMIT) | (1L << MAPPED) | (1L << MATCH) | (1L << MINUTE) | (1L << MONTH))) != 0) + || ((((_la - 66)) & ~0x3f) == 0 + && ((1L << (_la - 66)) & ((1L << (NOT - 66)) | (1L << (NULL - 66)) | (1L << (OPTIMIZED - 66)) | (1L + << (PARSED - 66)) | (1L << (PHYSICAL - 66)) | (1L << (PIVOT - 66)) | (1L << (PLAN - 66)) | (1L << (RIGHT + - 66)) | (1L << (RLIKE - 66)) | (1L << (QUERY - 66)) | (1L << (SCHEMAS - 66)) | (1L << (SECOND + - 66)) | (1L << (SHOW - 66)) | (1L << (SYS - 66)) | (1L << (TABLES - 66)) | (1L << (TEXT - 66)) + | (1L << (TRUE - 66)) | (1L << (TOP - 66)) | (1L << (TYPE - 66)) | (1L << (TYPES - 66)) | (1L << (VERIFY + - 66)) | (1L << (YEAR - 66)) | (1L << (FUNCTION_ESC - 66)) | (1L << (DATE_ESC - 66)) | (1L + << (TIME_ESC - 66)) | (1L << (TIMESTAMP_ESC - 66)) | (1L << (GUID_ESC - 66)) | (1L << (PLUS + - 66)) | (1L << (MINUS - 66)) | (1L << (ASTERISK - 66)) | (1L << (PARAM - 66)) | (1L + << (STRING - 66)) | (1L << (INTEGER_VALUE - 66)) | (1L << (DECIMAL_VALUE - 66)))) != 0) + || ((((_la - 130)) & ~0x3f) == 0 + && ((1L << (_la - 130)) & ((1L << (IDENTIFIER - 130)) | (1L << (DIGIT_IDENTIFIER - 130)) | (1L + << (QUOTED_IDENTIFIER - 130)) | (1L << (BACKQUOTED_IDENTIFIER - 130)))) != 0)) { + { + setState(671); + ((CaseContext) _localctx).operand = booleanExpression(0); + } + } + + setState(675); + _errHandler.sync(this); + _la = _input.LA(1); + do { + { + { + setState(674); + whenClause(); + } + } + setState(677); + _errHandler.sync(this); + _la = _input.LA(1); + } while (_la == WHEN); + setState(681); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == ELSE) { + { + setState(679); + match(ELSE); + setState(680); + ((CaseContext) _localctx).elseClause = booleanExpression(0); + } + } + + setState(683); + match(END); + } + break; + } + _ctx.stop = _input.LT(-1); + setState(692); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input, 96, _ctx); + while (_alt != 2 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER) { + if (_alt == 1) { + if (_parseListeners != null) triggerExitRuleEvent(); + _prevctx = _localctx; + { + { + _localctx = new CastOperatorExpressionContext(new PrimaryExpressionContext(_parentctx, _parentState)); + pushNewRecursionContext(_localctx, _startState, RULE_primaryExpression); + setState(687); + if (!(precpred(_ctx, 10))) throw new FailedPredicateException(this, "precpred(_ctx, 10)"); + setState(688); + match(CAST_OP); + setState(689); + dataType(); + } + } + } + setState(694); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input, 96, _ctx); + } + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + unrollRecursionContexts(_parentctx); + } + return _localctx; } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitStringLiteral(this); + + public static class BuiltinDateTimeFunctionContext extends ParserRuleContext { + public Token name; + + public TerminalNode CURRENT_TIMESTAMP() { + return getToken(SqlBaseParser.CURRENT_TIMESTAMP, 0); + } + + public TerminalNode CURRENT_DATE() { + return getToken(SqlBaseParser.CURRENT_DATE, 0); + } + + public TerminalNode CURRENT_TIME() { + return getToken(SqlBaseParser.CURRENT_TIME, 0); + } + + public BuiltinDateTimeFunctionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_builtinDateTimeFunction; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterBuiltinDateTimeFunction(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitBuiltinDateTimeFunction(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitBuiltinDateTimeFunction(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitStringLiteral(this); - else return visitor.visitChildren(this); - } - } - public static class ParamLiteralContext extends ConstantContext { - public TerminalNode PARAM() { return getToken(SqlBaseParser.PARAM, 0); } - public ParamLiteralContext(ConstantContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterParamLiteral(this); + + public final BuiltinDateTimeFunctionContext builtinDateTimeFunction() throws RecognitionException { + BuiltinDateTimeFunctionContext _localctx = new BuiltinDateTimeFunctionContext(_ctx, getState()); + enterRule(_localctx, 72, RULE_builtinDateTimeFunction); + try { + setState(698); + _errHandler.sync(this); + switch (_input.LA(1)) { + case CURRENT_TIMESTAMP: + enterOuterAlt(_localctx, 1); { + setState(695); + ((BuiltinDateTimeFunctionContext) _localctx).name = match(CURRENT_TIMESTAMP); + } + break; + case CURRENT_DATE: + enterOuterAlt(_localctx, 2); { + setState(696); + ((BuiltinDateTimeFunctionContext) _localctx).name = match(CURRENT_DATE); + } + break; + case CURRENT_TIME: + enterOuterAlt(_localctx, 3); { + setState(697); + ((BuiltinDateTimeFunctionContext) _localctx).name = match(CURRENT_TIME); + } + break; + default: + throw new NoViableAltException(this); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitParamLiteral(this); + + public static class CastExpressionContext extends ParserRuleContext { + public CastTemplateContext castTemplate() { + return getRuleContext(CastTemplateContext.class, 0); + } + + public TerminalNode FUNCTION_ESC() { + return getToken(SqlBaseParser.FUNCTION_ESC, 0); + } + + public TerminalNode ESC_END() { + return getToken(SqlBaseParser.ESC_END, 0); + } + + public ConvertTemplateContext convertTemplate() { + return getRuleContext(ConvertTemplateContext.class, 0); + } + + public CastExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_castExpression; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterCastExpression(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitCastExpression(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitCastExpression(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitParamLiteral(this); - else return visitor.visitChildren(this); - } - } - public static class TimeEscapedLiteralContext extends ConstantContext { - public TerminalNode TIME_ESC() { return getToken(SqlBaseParser.TIME_ESC, 0); } - public StringContext string() { - return getRuleContext(StringContext.class,0); - } - public TerminalNode ESC_END() { return getToken(SqlBaseParser.ESC_END, 0); } - public TimeEscapedLiteralContext(ConstantContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterTimeEscapedLiteral(this); + + public final CastExpressionContext castExpression() throws RecognitionException { + CastExpressionContext _localctx = new CastExpressionContext(_ctx, getState()); + enterRule(_localctx, 74, RULE_castExpression); + try { + setState(710); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 98, _ctx)) { + case 1: + enterOuterAlt(_localctx, 1); { + setState(700); + castTemplate(); + } + break; + case 2: + enterOuterAlt(_localctx, 2); { + setState(701); + match(FUNCTION_ESC); + setState(702); + castTemplate(); + setState(703); + match(ESC_END); + } + break; + case 3: + enterOuterAlt(_localctx, 3); { + setState(705); + convertTemplate(); + } + break; + case 4: + enterOuterAlt(_localctx, 4); { + setState(706); + match(FUNCTION_ESC); + setState(707); + convertTemplate(); + setState(708); + match(ESC_END); + } + break; + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitTimeEscapedLiteral(this); + + public static class CastTemplateContext extends ParserRuleContext { + public TerminalNode CAST() { + return getToken(SqlBaseParser.CAST, 0); + } + + public ExpressionContext expression() { + return getRuleContext(ExpressionContext.class, 0); + } + + public TerminalNode AS() { + return getToken(SqlBaseParser.AS, 0); + } + + public DataTypeContext dataType() { + return getRuleContext(DataTypeContext.class, 0); + } + + public CastTemplateContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_castTemplate; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterCastTemplate(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitCastTemplate(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitCastTemplate(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitTimeEscapedLiteral(this); - else return visitor.visitChildren(this); - } - } - public static class DateEscapedLiteralContext extends ConstantContext { - public TerminalNode DATE_ESC() { return getToken(SqlBaseParser.DATE_ESC, 0); } - public StringContext string() { - return getRuleContext(StringContext.class,0); - } - public TerminalNode ESC_END() { return getToken(SqlBaseParser.ESC_END, 0); } - public DateEscapedLiteralContext(ConstantContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterDateEscapedLiteral(this); + + public final CastTemplateContext castTemplate() throws RecognitionException { + CastTemplateContext _localctx = new CastTemplateContext(_ctx, getState()); + enterRule(_localctx, 76, RULE_castTemplate); + try { + enterOuterAlt(_localctx, 1); + { + setState(712); + match(CAST); + setState(713); + match(T__0); + setState(714); + expression(); + setState(715); + match(AS); + setState(716); + dataType(); + setState(717); + match(T__1); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitDateEscapedLiteral(this); + + public static class ConvertTemplateContext extends ParserRuleContext { + public TerminalNode CONVERT() { + return getToken(SqlBaseParser.CONVERT, 0); + } + + public ExpressionContext expression() { + return getRuleContext(ExpressionContext.class, 0); + } + + public DataTypeContext dataType() { + return getRuleContext(DataTypeContext.class, 0); + } + + public ConvertTemplateContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_convertTemplate; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterConvertTemplate(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitConvertTemplate(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitConvertTemplate(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitDateEscapedLiteral(this); - else return visitor.visitChildren(this); + + public final ConvertTemplateContext convertTemplate() throws RecognitionException { + ConvertTemplateContext _localctx = new ConvertTemplateContext(_ctx, getState()); + enterRule(_localctx, 78, RULE_convertTemplate); + try { + enterOuterAlt(_localctx, 1); + { + setState(719); + match(CONVERT); + setState(720); + match(T__0); + setState(721); + expression(); + setState(722); + match(T__2); + setState(723); + dataType(); + setState(724); + match(T__1); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - } - public static class IntervalLiteralContext extends ConstantContext { - public IntervalContext interval() { - return getRuleContext(IntervalContext.class,0); + + public static class ExtractExpressionContext extends ParserRuleContext { + public ExtractTemplateContext extractTemplate() { + return getRuleContext(ExtractTemplateContext.class, 0); + } + + public TerminalNode FUNCTION_ESC() { + return getToken(SqlBaseParser.FUNCTION_ESC, 0); + } + + public TerminalNode ESC_END() { + return getToken(SqlBaseParser.ESC_END, 0); + } + + public ExtractExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_extractExpression; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterExtractExpression(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitExtractExpression(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitExtractExpression(this); + else return visitor.visitChildren(this); + } } - public IntervalLiteralContext(ConstantContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterIntervalLiteral(this); + + public final ExtractExpressionContext extractExpression() throws RecognitionException { + ExtractExpressionContext _localctx = new ExtractExpressionContext(_ctx, getState()); + enterRule(_localctx, 80, RULE_extractExpression); + try { + setState(731); + _errHandler.sync(this); + switch (_input.LA(1)) { + case EXTRACT: + enterOuterAlt(_localctx, 1); { + setState(726); + extractTemplate(); + } + break; + case FUNCTION_ESC: + enterOuterAlt(_localctx, 2); { + setState(727); + match(FUNCTION_ESC); + setState(728); + extractTemplate(); + setState(729); + match(ESC_END); + } + break; + default: + throw new NoViableAltException(this); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitIntervalLiteral(this); + + public static class ExtractTemplateContext extends ParserRuleContext { + public IdentifierContext field; + + public TerminalNode EXTRACT() { + return getToken(SqlBaseParser.EXTRACT, 0); + } + + public TerminalNode FROM() { + return getToken(SqlBaseParser.FROM, 0); + } + + public ValueExpressionContext valueExpression() { + return getRuleContext(ValueExpressionContext.class, 0); + } + + public IdentifierContext identifier() { + return getRuleContext(IdentifierContext.class, 0); + } + + public ExtractTemplateContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_extractTemplate; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterExtractTemplate(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitExtractTemplate(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitExtractTemplate(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitIntervalLiteral(this); - else return visitor.visitChildren(this); + + public final ExtractTemplateContext extractTemplate() throws RecognitionException { + ExtractTemplateContext _localctx = new ExtractTemplateContext(_ctx, getState()); + enterRule(_localctx, 82, RULE_extractTemplate); + try { + enterOuterAlt(_localctx, 1); + { + setState(733); + match(EXTRACT); + setState(734); + match(T__0); + setState(735); + ((ExtractTemplateContext) _localctx).field = identifier(); + setState(736); + match(FROM); + setState(737); + valueExpression(0); + setState(738); + match(T__1); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - } - public static class NumericLiteralContext extends ConstantContext { - public NumberContext number() { - return getRuleContext(NumberContext.class,0); + + public static class FunctionExpressionContext extends ParserRuleContext { + public FunctionTemplateContext functionTemplate() { + return getRuleContext(FunctionTemplateContext.class, 0); + } + + public TerminalNode FUNCTION_ESC() { + return getToken(SqlBaseParser.FUNCTION_ESC, 0); + } + + public TerminalNode ESC_END() { + return getToken(SqlBaseParser.ESC_END, 0); + } + + public FunctionExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_functionExpression; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterFunctionExpression(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitFunctionExpression(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitFunctionExpression(this); + else return visitor.visitChildren(this); + } } - public NumericLiteralContext(ConstantContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterNumericLiteral(this); + + public final FunctionExpressionContext functionExpression() throws RecognitionException { + FunctionExpressionContext _localctx = new FunctionExpressionContext(_ctx, getState()); + enterRule(_localctx, 84, RULE_functionExpression); + try { + setState(745); + _errHandler.sync(this); + switch (_input.LA(1)) { + case ANALYZE: + case ANALYZED: + case CATALOGS: + case COLUMNS: + case CURRENT_DATE: + case CURRENT_TIME: + case CURRENT_TIMESTAMP: + case DAY: + case DEBUG: + case EXECUTABLE: + case EXPLAIN: + case FIRST: + case FORMAT: + case FULL: + case FUNCTIONS: + case GRAPHVIZ: + case HOUR: + case INTERVAL: + case LAST: + case LEFT: + case LIMIT: + case MAPPED: + case MINUTE: + case MONTH: + case OPTIMIZED: + case PARSED: + case PHYSICAL: + case PIVOT: + case PLAN: + case RIGHT: + case RLIKE: + case QUERY: + case SCHEMAS: + case SECOND: + case SHOW: + case SYS: + case TABLES: + case TEXT: + case TOP: + case TYPE: + case TYPES: + case VERIFY: + case YEAR: + case IDENTIFIER: + case DIGIT_IDENTIFIER: + case QUOTED_IDENTIFIER: + case BACKQUOTED_IDENTIFIER: + enterOuterAlt(_localctx, 1); { + setState(740); + functionTemplate(); + } + break; + case FUNCTION_ESC: + enterOuterAlt(_localctx, 2); { + setState(741); + match(FUNCTION_ESC); + setState(742); + functionTemplate(); + setState(743); + match(ESC_END); + } + break; + default: + throw new NoViableAltException(this); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitNumericLiteral(this); + + public static class FunctionTemplateContext extends ParserRuleContext { + public FunctionNameContext functionName() { + return getRuleContext(FunctionNameContext.class, 0); + } + + public List expression() { + return getRuleContexts(ExpressionContext.class); + } + + public ExpressionContext expression(int i) { + return getRuleContext(ExpressionContext.class, i); + } + + public SetQuantifierContext setQuantifier() { + return getRuleContext(SetQuantifierContext.class, 0); + } + + public FunctionTemplateContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_functionTemplate; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterFunctionTemplate(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitFunctionTemplate(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitFunctionTemplate(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitNumericLiteral(this); - else return visitor.visitChildren(this); + + public final FunctionTemplateContext functionTemplate() throws RecognitionException { + FunctionTemplateContext _localctx = new FunctionTemplateContext(_ctx, getState()); + enterRule(_localctx, 86, RULE_functionTemplate); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(747); + functionName(); + setState(748); + match(T__0); + setState(760); + _errHandler.sync(this); + _la = _input.LA(1); + if ((((_la) & ~0x3f) == 0 + && ((1L << _la) & ((1L << T__0) | (1L << ALL) | (1L << ANALYZE) | (1L << ANALYZED) | (1L << CASE) | (1L << CAST) | (1L + << CATALOGS) | (1L << COLUMNS) | (1L << CONVERT) | (1L << CURRENT_DATE) | (1L << CURRENT_TIME) | (1L + << CURRENT_TIMESTAMP) | (1L << DAY) | (1L << DEBUG) | (1L << DISTINCT) | (1L << EXECUTABLE) | (1L << EXISTS) + | (1L << EXPLAIN) | (1L << EXTRACT) | (1L << FALSE) | (1L << FIRST) | (1L << FORMAT) | (1L << FULL) | (1L + << FUNCTIONS) | (1L << GRAPHVIZ) | (1L << HOUR) | (1L << INTERVAL) | (1L << LAST) | (1L << LEFT) | (1L << LIMIT) + | (1L << MAPPED) | (1L << MATCH) | (1L << MINUTE) | (1L << MONTH))) != 0) + || ((((_la - 66)) & ~0x3f) == 0 + && ((1L << (_la - 66)) & ((1L << (NOT - 66)) | (1L << (NULL - 66)) | (1L << (OPTIMIZED - 66)) | (1L << (PARSED + - 66)) | (1L << (PHYSICAL - 66)) | (1L << (PIVOT - 66)) | (1L << (PLAN - 66)) | (1L << (RIGHT - 66)) | (1L + << (RLIKE - 66)) | (1L << (QUERY - 66)) | (1L << (SCHEMAS - 66)) | (1L << (SECOND - 66)) | (1L << (SHOW + - 66)) | (1L << (SYS - 66)) | (1L << (TABLES - 66)) | (1L << (TEXT - 66)) | (1L << (TRUE - 66)) | (1L + << (TOP - 66)) | (1L << (TYPE - 66)) | (1L << (TYPES - 66)) | (1L << (VERIFY - 66)) | (1L << (YEAR + - 66)) | (1L << (FUNCTION_ESC - 66)) | (1L << (DATE_ESC - 66)) | (1L << (TIME_ESC - 66)) | (1L + << (TIMESTAMP_ESC - 66)) | (1L << (GUID_ESC - 66)) | (1L << (PLUS - 66)) | (1L << (MINUS + - 66)) | (1L << (ASTERISK - 66)) | (1L << (PARAM - 66)) | (1L << (STRING - 66)) | (1L + << (INTEGER_VALUE - 66)) | (1L << (DECIMAL_VALUE - 66)))) != 0) + || ((((_la - 130)) & ~0x3f) == 0 + && ((1L << (_la - 130)) & ((1L << (IDENTIFIER - 130)) | (1L << (DIGIT_IDENTIFIER - 130)) | (1L << (QUOTED_IDENTIFIER + - 130)) | (1L << (BACKQUOTED_IDENTIFIER - 130)))) != 0)) { + { + setState(750); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == ALL || _la == DISTINCT) { + { + setState(749); + setQuantifier(); + } + } + + setState(752); + expression(); + setState(757); + _errHandler.sync(this); + _la = _input.LA(1); + while (_la == T__2) { + { + { + setState(753); + match(T__2); + setState(754); + expression(); + } + } + setState(759); + _errHandler.sync(this); + _la = _input.LA(1); + } + } + } + + setState(762); + match(T__1); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - } - public static class BooleanLiteralContext extends ConstantContext { - public BooleanValueContext booleanValue() { - return getRuleContext(BooleanValueContext.class,0); + + public static class FunctionNameContext extends ParserRuleContext { + public TerminalNode LEFT() { + return getToken(SqlBaseParser.LEFT, 0); + } + + public TerminalNode RIGHT() { + return getToken(SqlBaseParser.RIGHT, 0); + } + + public IdentifierContext identifier() { + return getRuleContext(IdentifierContext.class, 0); + } + + public FunctionNameContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_functionName; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterFunctionName(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitFunctionName(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitFunctionName(this); + else return visitor.visitChildren(this); + } } - public BooleanLiteralContext(ConstantContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterBooleanLiteral(this); + + public final FunctionNameContext functionName() throws RecognitionException { + FunctionNameContext _localctx = new FunctionNameContext(_ctx, getState()); + enterRule(_localctx, 88, RULE_functionName); + try { + setState(767); + _errHandler.sync(this); + switch (_input.LA(1)) { + case LEFT: + enterOuterAlt(_localctx, 1); { + setState(764); + match(LEFT); + } + break; + case RIGHT: + enterOuterAlt(_localctx, 2); { + setState(765); + match(RIGHT); + } + break; + case ANALYZE: + case ANALYZED: + case CATALOGS: + case COLUMNS: + case CURRENT_DATE: + case CURRENT_TIME: + case CURRENT_TIMESTAMP: + case DAY: + case DEBUG: + case EXECUTABLE: + case EXPLAIN: + case FIRST: + case FORMAT: + case FULL: + case FUNCTIONS: + case GRAPHVIZ: + case HOUR: + case INTERVAL: + case LAST: + case LIMIT: + case MAPPED: + case MINUTE: + case MONTH: + case OPTIMIZED: + case PARSED: + case PHYSICAL: + case PIVOT: + case PLAN: + case RLIKE: + case QUERY: + case SCHEMAS: + case SECOND: + case SHOW: + case SYS: + case TABLES: + case TEXT: + case TOP: + case TYPE: + case TYPES: + case VERIFY: + case YEAR: + case IDENTIFIER: + case DIGIT_IDENTIFIER: + case QUOTED_IDENTIFIER: + case BACKQUOTED_IDENTIFIER: + enterOuterAlt(_localctx, 3); { + setState(766); + identifier(); + } + break; + default: + throw new NoViableAltException(this); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitBooleanLiteral(this); + + public static class ConstantContext extends ParserRuleContext { + public ConstantContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_constant; + } + + public ConstantContext() {} + + public void copyFrom(ConstantContext ctx) { + super.copyFrom(ctx); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitBooleanLiteral(this); - else return visitor.visitChildren(this); - } - } - public static class GuidEscapedLiteralContext extends ConstantContext { - public TerminalNode GUID_ESC() { return getToken(SqlBaseParser.GUID_ESC, 0); } - public StringContext string() { - return getRuleContext(StringContext.class,0); - } - public TerminalNode ESC_END() { return getToken(SqlBaseParser.ESC_END, 0); } - public GuidEscapedLiteralContext(ConstantContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterGuidEscapedLiteral(this); + + public static class NullLiteralContext extends ConstantContext { + public TerminalNode NULL() { + return getToken(SqlBaseParser.NULL, 0); + } + + public NullLiteralContext(ConstantContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterNullLiteral(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitNullLiteral(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitNullLiteral(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitGuidEscapedLiteral(this); + + public static class TimestampEscapedLiteralContext extends ConstantContext { + public TerminalNode TIMESTAMP_ESC() { + return getToken(SqlBaseParser.TIMESTAMP_ESC, 0); + } + + public StringContext string() { + return getRuleContext(StringContext.class, 0); + } + + public TerminalNode ESC_END() { + return getToken(SqlBaseParser.ESC_END, 0); + } + + public TimestampEscapedLiteralContext(ConstantContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterTimestampEscapedLiteral(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitTimestampEscapedLiteral(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitTimestampEscapedLiteral(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitGuidEscapedLiteral(this); - else return visitor.visitChildren(this); - } - } - - public final ConstantContext constant() throws RecognitionException { - ConstantContext _localctx = new ConstantContext(_ctx, getState()); - enterRule(_localctx, 90, RULE_constant); - try { - int _alt; - setState(795); - _errHandler.sync(this); - switch (_input.LA(1)) { - case NULL: - _localctx = new NullLiteralContext(_localctx); - enterOuterAlt(_localctx, 1); - { - setState(769); - match(NULL); - } - break; - case INTERVAL: - _localctx = new IntervalLiteralContext(_localctx); - enterOuterAlt(_localctx, 2); - { - setState(770); - interval(); - } - break; - case INTEGER_VALUE: - case DECIMAL_VALUE: - _localctx = new NumericLiteralContext(_localctx); - enterOuterAlt(_localctx, 3); - { - setState(771); - number(); - } - break; - case FALSE: - case TRUE: - _localctx = new BooleanLiteralContext(_localctx); - enterOuterAlt(_localctx, 4); - { - setState(772); - booleanValue(); - } - break; - case STRING: - _localctx = new StringLiteralContext(_localctx); - enterOuterAlt(_localctx, 5); - { - setState(774); - _errHandler.sync(this); - _alt = 1; - do { - switch (_alt) { - case 1: - { - { - setState(773); - match(STRING); - } - } - break; - default: - throw new NoViableAltException(this); - } - setState(776); - _errHandler.sync(this); - _alt = getInterpreter().adaptivePredict(_input,105,_ctx); - } while ( _alt!=2 && _alt!=org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER ); - } - break; - case PARAM: - _localctx = new ParamLiteralContext(_localctx); - enterOuterAlt(_localctx, 6); - { - setState(778); - match(PARAM); - } - break; - case DATE_ESC: - _localctx = new DateEscapedLiteralContext(_localctx); - enterOuterAlt(_localctx, 7); - { - setState(779); - match(DATE_ESC); - setState(780); - string(); - setState(781); - match(ESC_END); - } - break; - case TIME_ESC: - _localctx = new TimeEscapedLiteralContext(_localctx); - enterOuterAlt(_localctx, 8); - { - setState(783); - match(TIME_ESC); - setState(784); - string(); - setState(785); - match(ESC_END); - } - break; - case TIMESTAMP_ESC: - _localctx = new TimestampEscapedLiteralContext(_localctx); - enterOuterAlt(_localctx, 9); - { - setState(787); - match(TIMESTAMP_ESC); - setState(788); - string(); - setState(789); - match(ESC_END); - } - break; - case GUID_ESC: - _localctx = new GuidEscapedLiteralContext(_localctx); - enterOuterAlt(_localctx, 10); - { - setState(791); - match(GUID_ESC); - setState(792); - string(); - setState(793); - match(ESC_END); - } - break; - default: - throw new NoViableAltException(this); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class ComparisonOperatorContext extends ParserRuleContext { - public TerminalNode EQ() { return getToken(SqlBaseParser.EQ, 0); } - public TerminalNode NULLEQ() { return getToken(SqlBaseParser.NULLEQ, 0); } - public TerminalNode NEQ() { return getToken(SqlBaseParser.NEQ, 0); } - public TerminalNode LT() { return getToken(SqlBaseParser.LT, 0); } - public TerminalNode LTE() { return getToken(SqlBaseParser.LTE, 0); } - public TerminalNode GT() { return getToken(SqlBaseParser.GT, 0); } - public TerminalNode GTE() { return getToken(SqlBaseParser.GTE, 0); } - public ComparisonOperatorContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_comparisonOperator; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterComparisonOperator(this); + + public static class StringLiteralContext extends ConstantContext { + public List STRING() { + return getTokens(SqlBaseParser.STRING); + } + + public TerminalNode STRING(int i) { + return getToken(SqlBaseParser.STRING, i); + } + + public StringLiteralContext(ConstantContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterStringLiteral(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitStringLiteral(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitStringLiteral(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitComparisonOperator(this); + + public static class ParamLiteralContext extends ConstantContext { + public TerminalNode PARAM() { + return getToken(SqlBaseParser.PARAM, 0); + } + + public ParamLiteralContext(ConstantContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterParamLiteral(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitParamLiteral(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitParamLiteral(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitComparisonOperator(this); - else return visitor.visitChildren(this); - } - } - - public final ComparisonOperatorContext comparisonOperator() throws RecognitionException { - ComparisonOperatorContext _localctx = new ComparisonOperatorContext(_ctx, getState()); - enterRule(_localctx, 92, RULE_comparisonOperator); - int _la; - try { - enterOuterAlt(_localctx, 1); - { - setState(797); - _la = _input.LA(1); - if ( !(((((_la - 112)) & ~0x3f) == 0 && ((1L << (_la - 112)) & ((1L << (EQ - 112)) | (1L << (NULLEQ - 112)) | (1L << (NEQ - 112)) | (1L << (LT - 112)) | (1L << (LTE - 112)) | (1L << (GT - 112)) | (1L << (GTE - 112)))) != 0)) ) { - _errHandler.recoverInline(this); - } - else { - if ( _input.LA(1)==Token.EOF ) matchedEOF = true; - _errHandler.reportMatch(this); - consume(); - } - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class BooleanValueContext extends ParserRuleContext { - public TerminalNode TRUE() { return getToken(SqlBaseParser.TRUE, 0); } - public TerminalNode FALSE() { return getToken(SqlBaseParser.FALSE, 0); } - public BooleanValueContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_booleanValue; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterBooleanValue(this); + + public static class TimeEscapedLiteralContext extends ConstantContext { + public TerminalNode TIME_ESC() { + return getToken(SqlBaseParser.TIME_ESC, 0); + } + + public StringContext string() { + return getRuleContext(StringContext.class, 0); + } + + public TerminalNode ESC_END() { + return getToken(SqlBaseParser.ESC_END, 0); + } + + public TimeEscapedLiteralContext(ConstantContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterTimeEscapedLiteral(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitTimeEscapedLiteral(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitTimeEscapedLiteral(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitBooleanValue(this); + + public static class DateEscapedLiteralContext extends ConstantContext { + public TerminalNode DATE_ESC() { + return getToken(SqlBaseParser.DATE_ESC, 0); + } + + public StringContext string() { + return getRuleContext(StringContext.class, 0); + } + + public TerminalNode ESC_END() { + return getToken(SqlBaseParser.ESC_END, 0); + } + + public DateEscapedLiteralContext(ConstantContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterDateEscapedLiteral(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitDateEscapedLiteral(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitDateEscapedLiteral(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitBooleanValue(this); - else return visitor.visitChildren(this); - } - } - - public final BooleanValueContext booleanValue() throws RecognitionException { - BooleanValueContext _localctx = new BooleanValueContext(_ctx, getState()); - enterRule(_localctx, 94, RULE_booleanValue); - int _la; - try { - enterOuterAlt(_localctx, 1); - { - setState(799); - _la = _input.LA(1); - if ( !(_la==FALSE || _la==TRUE) ) { - _errHandler.recoverInline(this); - } - else { - if ( _input.LA(1)==Token.EOF ) matchedEOF = true; - _errHandler.reportMatch(this); - consume(); - } - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class IntervalContext extends ParserRuleContext { - public Token sign; - public NumberContext valueNumeric; - public StringContext valuePattern; - public IntervalFieldContext leading; - public IntervalFieldContext trailing; - public TerminalNode INTERVAL() { return getToken(SqlBaseParser.INTERVAL, 0); } - public List intervalField() { - return getRuleContexts(IntervalFieldContext.class); - } - public IntervalFieldContext intervalField(int i) { - return getRuleContext(IntervalFieldContext.class,i); - } - public NumberContext number() { - return getRuleContext(NumberContext.class,0); - } - public StringContext string() { - return getRuleContext(StringContext.class,0); - } - public TerminalNode TO() { return getToken(SqlBaseParser.TO, 0); } - public TerminalNode PLUS() { return getToken(SqlBaseParser.PLUS, 0); } - public TerminalNode MINUS() { return getToken(SqlBaseParser.MINUS, 0); } - public IntervalContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_interval; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterInterval(this); + + public static class IntervalLiteralContext extends ConstantContext { + public IntervalContext interval() { + return getRuleContext(IntervalContext.class, 0); + } + + public IntervalLiteralContext(ConstantContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterIntervalLiteral(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitIntervalLiteral(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitIntervalLiteral(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitInterval(this); + + public static class NumericLiteralContext extends ConstantContext { + public NumberContext number() { + return getRuleContext(NumberContext.class, 0); + } + + public NumericLiteralContext(ConstantContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterNumericLiteral(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitNumericLiteral(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitNumericLiteral(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitInterval(this); - else return visitor.visitChildren(this); - } - } - - public final IntervalContext interval() throws RecognitionException { - IntervalContext _localctx = new IntervalContext(_ctx, getState()); - enterRule(_localctx, 96, RULE_interval); - int _la; - try { - enterOuterAlt(_localctx, 1); - { - setState(801); - match(INTERVAL); - setState(803); - _errHandler.sync(this); - _la = _input.LA(1); - if (_la==PLUS || _la==MINUS) { - { - setState(802); - ((IntervalContext)_localctx).sign = _input.LT(1); - _la = _input.LA(1); - if ( !(_la==PLUS || _la==MINUS) ) { - ((IntervalContext)_localctx).sign = (Token)_errHandler.recoverInline(this); - } - else { - if ( _input.LA(1)==Token.EOF ) matchedEOF = true; - _errHandler.reportMatch(this); - consume(); - } - } - } - - setState(807); - _errHandler.sync(this); - switch (_input.LA(1)) { - case INTEGER_VALUE: - case DECIMAL_VALUE: - { - setState(805); - ((IntervalContext)_localctx).valueNumeric = number(); - } - break; - case PARAM: - case STRING: - { - setState(806); - ((IntervalContext)_localctx).valuePattern = string(); - } - break; - default: - throw new NoViableAltException(this); - } - setState(809); - ((IntervalContext)_localctx).leading = intervalField(); - setState(812); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,109,_ctx) ) { - case 1: - { - setState(810); - match(TO); - setState(811); - ((IntervalContext)_localctx).trailing = intervalField(); - } - break; - } - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class IntervalFieldContext extends ParserRuleContext { - public TerminalNode YEAR() { return getToken(SqlBaseParser.YEAR, 0); } - public TerminalNode YEARS() { return getToken(SqlBaseParser.YEARS, 0); } - public TerminalNode MONTH() { return getToken(SqlBaseParser.MONTH, 0); } - public TerminalNode MONTHS() { return getToken(SqlBaseParser.MONTHS, 0); } - public TerminalNode DAY() { return getToken(SqlBaseParser.DAY, 0); } - public TerminalNode DAYS() { return getToken(SqlBaseParser.DAYS, 0); } - public TerminalNode HOUR() { return getToken(SqlBaseParser.HOUR, 0); } - public TerminalNode HOURS() { return getToken(SqlBaseParser.HOURS, 0); } - public TerminalNode MINUTE() { return getToken(SqlBaseParser.MINUTE, 0); } - public TerminalNode MINUTES() { return getToken(SqlBaseParser.MINUTES, 0); } - public TerminalNode SECOND() { return getToken(SqlBaseParser.SECOND, 0); } - public TerminalNode SECONDS() { return getToken(SqlBaseParser.SECONDS, 0); } - public IntervalFieldContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_intervalField; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterIntervalField(this); + + public static class BooleanLiteralContext extends ConstantContext { + public BooleanValueContext booleanValue() { + return getRuleContext(BooleanValueContext.class, 0); + } + + public BooleanLiteralContext(ConstantContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterBooleanLiteral(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitBooleanLiteral(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitBooleanLiteral(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitIntervalField(this); + + public static class GuidEscapedLiteralContext extends ConstantContext { + public TerminalNode GUID_ESC() { + return getToken(SqlBaseParser.GUID_ESC, 0); + } + + public StringContext string() { + return getRuleContext(StringContext.class, 0); + } + + public TerminalNode ESC_END() { + return getToken(SqlBaseParser.ESC_END, 0); + } + + public GuidEscapedLiteralContext(ConstantContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterGuidEscapedLiteral(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitGuidEscapedLiteral(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitGuidEscapedLiteral(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitIntervalField(this); - else return visitor.visitChildren(this); - } - } - - public final IntervalFieldContext intervalField() throws RecognitionException { - IntervalFieldContext _localctx = new IntervalFieldContext(_ctx, getState()); - enterRule(_localctx, 98, RULE_intervalField); - int _la; - try { - enterOuterAlt(_localctx, 1); - { - setState(814); - _la = _input.LA(1); - if ( !((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << DAY) | (1L << DAYS) | (1L << HOUR) | (1L << HOURS) | (1L << MINUTE) | (1L << MINUTES) | (1L << MONTH))) != 0) || ((((_la - 64)) & ~0x3f) == 0 && ((1L << (_la - 64)) & ((1L << (MONTHS - 64)) | (1L << (SECOND - 64)) | (1L << (SECONDS - 64)) | (1L << (YEAR - 64)) | (1L << (YEARS - 64)))) != 0)) ) { - _errHandler.recoverInline(this); - } - else { - if ( _input.LA(1)==Token.EOF ) matchedEOF = true; - _errHandler.reportMatch(this); - consume(); - } - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class DataTypeContext extends ParserRuleContext { - public DataTypeContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_dataType; } - - public DataTypeContext() { } - public void copyFrom(DataTypeContext ctx) { - super.copyFrom(ctx); - } - } - public static class PrimitiveDataTypeContext extends DataTypeContext { - public IdentifierContext identifier() { - return getRuleContext(IdentifierContext.class,0); - } - public PrimitiveDataTypeContext(DataTypeContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterPrimitiveDataType(this); + + public final ConstantContext constant() throws RecognitionException { + ConstantContext _localctx = new ConstantContext(_ctx, getState()); + enterRule(_localctx, 90, RULE_constant); + try { + int _alt; + setState(795); + _errHandler.sync(this); + switch (_input.LA(1)) { + case NULL: + _localctx = new NullLiteralContext(_localctx); + enterOuterAlt(_localctx, 1); { + setState(769); + match(NULL); + } + break; + case INTERVAL: + _localctx = new IntervalLiteralContext(_localctx); + enterOuterAlt(_localctx, 2); { + setState(770); + interval(); + } + break; + case INTEGER_VALUE: + case DECIMAL_VALUE: + _localctx = new NumericLiteralContext(_localctx); + enterOuterAlt(_localctx, 3); { + setState(771); + number(); + } + break; + case FALSE: + case TRUE: + _localctx = new BooleanLiteralContext(_localctx); + enterOuterAlt(_localctx, 4); { + setState(772); + booleanValue(); + } + break; + case STRING: + _localctx = new StringLiteralContext(_localctx); + enterOuterAlt(_localctx, 5); { + setState(774); + _errHandler.sync(this); + _alt = 1; + do { + switch (_alt) { + case 1: { + { + setState(773); + match(STRING); + } + } + break; + default: + throw new NoViableAltException(this); + } + setState(776); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input, 105, _ctx); + } while (_alt != 2 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER); + } + break; + case PARAM: + _localctx = new ParamLiteralContext(_localctx); + enterOuterAlt(_localctx, 6); { + setState(778); + match(PARAM); + } + break; + case DATE_ESC: + _localctx = new DateEscapedLiteralContext(_localctx); + enterOuterAlt(_localctx, 7); { + setState(779); + match(DATE_ESC); + setState(780); + string(); + setState(781); + match(ESC_END); + } + break; + case TIME_ESC: + _localctx = new TimeEscapedLiteralContext(_localctx); + enterOuterAlt(_localctx, 8); { + setState(783); + match(TIME_ESC); + setState(784); + string(); + setState(785); + match(ESC_END); + } + break; + case TIMESTAMP_ESC: + _localctx = new TimestampEscapedLiteralContext(_localctx); + enterOuterAlt(_localctx, 9); { + setState(787); + match(TIMESTAMP_ESC); + setState(788); + string(); + setState(789); + match(ESC_END); + } + break; + case GUID_ESC: + _localctx = new GuidEscapedLiteralContext(_localctx); + enterOuterAlt(_localctx, 10); { + setState(791); + match(GUID_ESC); + setState(792); + string(); + setState(793); + match(ESC_END); + } + break; + default: + throw new NoViableAltException(this); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitPrimitiveDataType(this); + + public static class ComparisonOperatorContext extends ParserRuleContext { + public TerminalNode EQ() { + return getToken(SqlBaseParser.EQ, 0); + } + + public TerminalNode NULLEQ() { + return getToken(SqlBaseParser.NULLEQ, 0); + } + + public TerminalNode NEQ() { + return getToken(SqlBaseParser.NEQ, 0); + } + + public TerminalNode LT() { + return getToken(SqlBaseParser.LT, 0); + } + + public TerminalNode LTE() { + return getToken(SqlBaseParser.LTE, 0); + } + + public TerminalNode GT() { + return getToken(SqlBaseParser.GT, 0); + } + + public TerminalNode GTE() { + return getToken(SqlBaseParser.GTE, 0); + } + + public ComparisonOperatorContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_comparisonOperator; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterComparisonOperator(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitComparisonOperator(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitComparisonOperator(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitPrimitiveDataType(this); - else return visitor.visitChildren(this); + + public final ComparisonOperatorContext comparisonOperator() throws RecognitionException { + ComparisonOperatorContext _localctx = new ComparisonOperatorContext(_ctx, getState()); + enterRule(_localctx, 92, RULE_comparisonOperator); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(797); + _la = _input.LA(1); + if (!(((((_la - 112)) & ~0x3f) == 0 + && ((1L << (_la - 112)) & ((1L << (EQ - 112)) | (1L << (NULLEQ - 112)) | (1L << (NEQ - 112)) | (1L << (LT - 112)) | (1L + << (LTE - 112)) | (1L << (GT - 112)) | (1L << (GTE - 112)))) != 0))) { + _errHandler.recoverInline(this); + } else { + if (_input.LA(1) == Token.EOF) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - } - public final DataTypeContext dataType() throws RecognitionException { - DataTypeContext _localctx = new DataTypeContext(_ctx, getState()); - enterRule(_localctx, 100, RULE_dataType); - try { - _localctx = new PrimitiveDataTypeContext(_localctx); - enterOuterAlt(_localctx, 1); - { - setState(816); - identifier(); - } + public static class BooleanValueContext extends ParserRuleContext { + public TerminalNode TRUE() { + return getToken(SqlBaseParser.TRUE, 0); + } + + public TerminalNode FALSE() { + return getToken(SqlBaseParser.FALSE, 0); + } + + public BooleanValueContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_booleanValue; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterBooleanValue(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitBooleanValue(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitBooleanValue(this); + else return visitor.visitChildren(this); + } } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); + + public final BooleanValueContext booleanValue() throws RecognitionException { + BooleanValueContext _localctx = new BooleanValueContext(_ctx, getState()); + enterRule(_localctx, 94, RULE_booleanValue); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(799); + _la = _input.LA(1); + if (!(_la == FALSE || _la == TRUE)) { + _errHandler.recoverInline(this); + } else { + if (_input.LA(1) == Token.EOF) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - finally { - exitRule(); + + public static class IntervalContext extends ParserRuleContext { + public Token sign; + public NumberContext valueNumeric; + public StringContext valuePattern; + public IntervalFieldContext leading; + public IntervalFieldContext trailing; + + public TerminalNode INTERVAL() { + return getToken(SqlBaseParser.INTERVAL, 0); + } + + public List intervalField() { + return getRuleContexts(IntervalFieldContext.class); + } + + public IntervalFieldContext intervalField(int i) { + return getRuleContext(IntervalFieldContext.class, i); + } + + public NumberContext number() { + return getRuleContext(NumberContext.class, 0); + } + + public StringContext string() { + return getRuleContext(StringContext.class, 0); + } + + public TerminalNode TO() { + return getToken(SqlBaseParser.TO, 0); + } + + public TerminalNode PLUS() { + return getToken(SqlBaseParser.PLUS, 0); + } + + public TerminalNode MINUS() { + return getToken(SqlBaseParser.MINUS, 0); + } + + public IntervalContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_interval; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterInterval(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitInterval(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitInterval(this); + else return visitor.visitChildren(this); + } } - return _localctx; - } - public static class QualifiedNameContext extends ParserRuleContext { - public List identifier() { - return getRuleContexts(IdentifierContext.class); + public final IntervalContext interval() throws RecognitionException { + IntervalContext _localctx = new IntervalContext(_ctx, getState()); + enterRule(_localctx, 96, RULE_interval); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(801); + match(INTERVAL); + setState(803); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la == PLUS || _la == MINUS) { + { + setState(802); + ((IntervalContext) _localctx).sign = _input.LT(1); + _la = _input.LA(1); + if (!(_la == PLUS || _la == MINUS)) { + ((IntervalContext) _localctx).sign = (Token) _errHandler.recoverInline(this); + } else { + if (_input.LA(1) == Token.EOF) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + } + } + + setState(807); + _errHandler.sync(this); + switch (_input.LA(1)) { + case INTEGER_VALUE: + case DECIMAL_VALUE: { + setState(805); + ((IntervalContext) _localctx).valueNumeric = number(); + } + break; + case PARAM: + case STRING: { + setState(806); + ((IntervalContext) _localctx).valuePattern = string(); + } + break; + default: + throw new NoViableAltException(this); + } + setState(809); + ((IntervalContext) _localctx).leading = intervalField(); + setState(812); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 109, _ctx)) { + case 1: { + setState(810); + match(TO); + setState(811); + ((IntervalContext) _localctx).trailing = intervalField(); + } + break; + } + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - public IdentifierContext identifier(int i) { - return getRuleContext(IdentifierContext.class,i); + + public static class IntervalFieldContext extends ParserRuleContext { + public TerminalNode YEAR() { + return getToken(SqlBaseParser.YEAR, 0); + } + + public TerminalNode YEARS() { + return getToken(SqlBaseParser.YEARS, 0); + } + + public TerminalNode MONTH() { + return getToken(SqlBaseParser.MONTH, 0); + } + + public TerminalNode MONTHS() { + return getToken(SqlBaseParser.MONTHS, 0); + } + + public TerminalNode DAY() { + return getToken(SqlBaseParser.DAY, 0); + } + + public TerminalNode DAYS() { + return getToken(SqlBaseParser.DAYS, 0); + } + + public TerminalNode HOUR() { + return getToken(SqlBaseParser.HOUR, 0); + } + + public TerminalNode HOURS() { + return getToken(SqlBaseParser.HOURS, 0); + } + + public TerminalNode MINUTE() { + return getToken(SqlBaseParser.MINUTE, 0); + } + + public TerminalNode MINUTES() { + return getToken(SqlBaseParser.MINUTES, 0); + } + + public TerminalNode SECOND() { + return getToken(SqlBaseParser.SECOND, 0); + } + + public TerminalNode SECONDS() { + return getToken(SqlBaseParser.SECONDS, 0); + } + + public IntervalFieldContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_intervalField; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterIntervalField(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitIntervalField(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitIntervalField(this); + else return visitor.visitChildren(this); + } } - public List DOT() { return getTokens(SqlBaseParser.DOT); } - public TerminalNode DOT(int i) { - return getToken(SqlBaseParser.DOT, i); + + public final IntervalFieldContext intervalField() throws RecognitionException { + IntervalFieldContext _localctx = new IntervalFieldContext(_ctx, getState()); + enterRule(_localctx, 98, RULE_intervalField); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(814); + _la = _input.LA(1); + if (!((((_la) & ~0x3f) == 0 + && ((1L << _la) & ((1L << DAY) | (1L << DAYS) | (1L << HOUR) | (1L << HOURS) | (1L << MINUTE) | (1L << MINUTES) | (1L + << MONTH))) != 0) + || ((((_la - 64)) & ~0x3f) == 0 + && ((1L << (_la - 64)) & ((1L << (MONTHS - 64)) | (1L << (SECOND - 64)) | (1L << (SECONDS - 64)) | (1L << (YEAR + - 64)) | (1L << (YEARS - 64)))) != 0))) { + _errHandler.recoverInline(this); + } else { + if (_input.LA(1) == Token.EOF) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - public QualifiedNameContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); + + public static class DataTypeContext extends ParserRuleContext { + public DataTypeContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_dataType; + } + + public DataTypeContext() {} + + public void copyFrom(DataTypeContext ctx) { + super.copyFrom(ctx); + } } - @Override public int getRuleIndex() { return RULE_qualifiedName; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterQualifiedName(this); + + public static class PrimitiveDataTypeContext extends DataTypeContext { + public IdentifierContext identifier() { + return getRuleContext(IdentifierContext.class, 0); + } + + public PrimitiveDataTypeContext(DataTypeContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterPrimitiveDataType(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitPrimitiveDataType(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitPrimitiveDataType(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitQualifiedName(this); + + public final DataTypeContext dataType() throws RecognitionException { + DataTypeContext _localctx = new DataTypeContext(_ctx, getState()); + enterRule(_localctx, 100, RULE_dataType); + try { + _localctx = new PrimitiveDataTypeContext(_localctx); + enterOuterAlt(_localctx, 1); + { + setState(816); + identifier(); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitQualifiedName(this); - else return visitor.visitChildren(this); - } - } - - public final QualifiedNameContext qualifiedName() throws RecognitionException { - QualifiedNameContext _localctx = new QualifiedNameContext(_ctx, getState()); - enterRule(_localctx, 102, RULE_qualifiedName); - try { - int _alt; - enterOuterAlt(_localctx, 1); - { - setState(823); - _errHandler.sync(this); - _alt = getInterpreter().adaptivePredict(_input,110,_ctx); - while ( _alt!=2 && _alt!=org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER ) { - if ( _alt==1 ) { - { - { - setState(818); - identifier(); - setState(819); - match(DOT); - } - } - } - setState(825); - _errHandler.sync(this); - _alt = getInterpreter().adaptivePredict(_input,110,_ctx); - } - setState(826); - identifier(); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class IdentifierContext extends ParserRuleContext { - public QuoteIdentifierContext quoteIdentifier() { - return getRuleContext(QuoteIdentifierContext.class,0); - } - public UnquoteIdentifierContext unquoteIdentifier() { - return getRuleContext(UnquoteIdentifierContext.class,0); - } - public IdentifierContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_identifier; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterIdentifier(this); + + public static class QualifiedNameContext extends ParserRuleContext { + public List identifier() { + return getRuleContexts(IdentifierContext.class); + } + + public IdentifierContext identifier(int i) { + return getRuleContext(IdentifierContext.class, i); + } + + public List DOT() { + return getTokens(SqlBaseParser.DOT); + } + + public TerminalNode DOT(int i) { + return getToken(SqlBaseParser.DOT, i); + } + + public QualifiedNameContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_qualifiedName; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterQualifiedName(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitQualifiedName(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitQualifiedName(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitIdentifier(this); + + public final QualifiedNameContext qualifiedName() throws RecognitionException { + QualifiedNameContext _localctx = new QualifiedNameContext(_ctx, getState()); + enterRule(_localctx, 102, RULE_qualifiedName); + try { + int _alt; + enterOuterAlt(_localctx, 1); + { + setState(823); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input, 110, _ctx); + while (_alt != 2 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER) { + if (_alt == 1) { + { + { + setState(818); + identifier(); + setState(819); + match(DOT); + } + } + } + setState(825); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input, 110, _ctx); + } + setState(826); + identifier(); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitIdentifier(this); - else return visitor.visitChildren(this); - } - } - - public final IdentifierContext identifier() throws RecognitionException { - IdentifierContext _localctx = new IdentifierContext(_ctx, getState()); - enterRule(_localctx, 104, RULE_identifier); - try { - setState(830); - _errHandler.sync(this); - switch (_input.LA(1)) { - case QUOTED_IDENTIFIER: - case BACKQUOTED_IDENTIFIER: - enterOuterAlt(_localctx, 1); - { - setState(828); - quoteIdentifier(); - } - break; - case ANALYZE: - case ANALYZED: - case CATALOGS: - case COLUMNS: - case CURRENT_DATE: - case CURRENT_TIME: - case CURRENT_TIMESTAMP: - case DAY: - case DEBUG: - case EXECUTABLE: - case EXPLAIN: - case FIRST: - case FORMAT: - case FULL: - case FUNCTIONS: - case GRAPHVIZ: - case HOUR: - case INTERVAL: - case LAST: - case LIMIT: - case MAPPED: - case MINUTE: - case MONTH: - case OPTIMIZED: - case PARSED: - case PHYSICAL: - case PIVOT: - case PLAN: - case RLIKE: - case QUERY: - case SCHEMAS: - case SECOND: - case SHOW: - case SYS: - case TABLES: - case TEXT: - case TOP: - case TYPE: - case TYPES: - case VERIFY: - case YEAR: - case IDENTIFIER: - case DIGIT_IDENTIFIER: - enterOuterAlt(_localctx, 2); - { - setState(829); - unquoteIdentifier(); - } - break; - default: - throw new NoViableAltException(this); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class TableIdentifierContext extends ParserRuleContext { - public IdentifierContext catalog; - public IdentifierContext name; - public TerminalNode TABLE_IDENTIFIER() { return getToken(SqlBaseParser.TABLE_IDENTIFIER, 0); } - public List identifier() { - return getRuleContexts(IdentifierContext.class); - } - public IdentifierContext identifier(int i) { - return getRuleContext(IdentifierContext.class,i); - } - public TableIdentifierContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_tableIdentifier; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterTableIdentifier(this); + + public static class IdentifierContext extends ParserRuleContext { + public QuoteIdentifierContext quoteIdentifier() { + return getRuleContext(QuoteIdentifierContext.class, 0); + } + + public UnquoteIdentifierContext unquoteIdentifier() { + return getRuleContext(UnquoteIdentifierContext.class, 0); + } + + public IdentifierContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_identifier; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterIdentifier(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitIdentifier(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitIdentifier(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitTableIdentifier(this); + + public final IdentifierContext identifier() throws RecognitionException { + IdentifierContext _localctx = new IdentifierContext(_ctx, getState()); + enterRule(_localctx, 104, RULE_identifier); + try { + setState(830); + _errHandler.sync(this); + switch (_input.LA(1)) { + case QUOTED_IDENTIFIER: + case BACKQUOTED_IDENTIFIER: + enterOuterAlt(_localctx, 1); { + setState(828); + quoteIdentifier(); + } + break; + case ANALYZE: + case ANALYZED: + case CATALOGS: + case COLUMNS: + case CURRENT_DATE: + case CURRENT_TIME: + case CURRENT_TIMESTAMP: + case DAY: + case DEBUG: + case EXECUTABLE: + case EXPLAIN: + case FIRST: + case FORMAT: + case FULL: + case FUNCTIONS: + case GRAPHVIZ: + case HOUR: + case INTERVAL: + case LAST: + case LIMIT: + case MAPPED: + case MINUTE: + case MONTH: + case OPTIMIZED: + case PARSED: + case PHYSICAL: + case PIVOT: + case PLAN: + case RLIKE: + case QUERY: + case SCHEMAS: + case SECOND: + case SHOW: + case SYS: + case TABLES: + case TEXT: + case TOP: + case TYPE: + case TYPES: + case VERIFY: + case YEAR: + case IDENTIFIER: + case DIGIT_IDENTIFIER: + enterOuterAlt(_localctx, 2); { + setState(829); + unquoteIdentifier(); + } + break; + default: + throw new NoViableAltException(this); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitTableIdentifier(this); - else return visitor.visitChildren(this); - } - } - - public final TableIdentifierContext tableIdentifier() throws RecognitionException { - TableIdentifierContext _localctx = new TableIdentifierContext(_ctx, getState()); - enterRule(_localctx, 106, RULE_tableIdentifier); - int _la; - try { - setState(844); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,114,_ctx) ) { - case 1: - enterOuterAlt(_localctx, 1); - { - setState(835); - _errHandler.sync(this); - _la = _input.LA(1); - if ((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << ANALYZE) | (1L << ANALYZED) | (1L << CATALOGS) | (1L << COLUMNS) | (1L << CURRENT_DATE) | (1L << CURRENT_TIME) | (1L << CURRENT_TIMESTAMP) | (1L << DAY) | (1L << DEBUG) | (1L << EXECUTABLE) | (1L << EXPLAIN) | (1L << FIRST) | (1L << FORMAT) | (1L << FULL) | (1L << FUNCTIONS) | (1L << GRAPHVIZ) | (1L << HOUR) | (1L << INTERVAL) | (1L << LAST) | (1L << LIMIT) | (1L << MAPPED) | (1L << MINUTE) | (1L << MONTH))) != 0) || ((((_la - 70)) & ~0x3f) == 0 && ((1L << (_la - 70)) & ((1L << (OPTIMIZED - 70)) | (1L << (PARSED - 70)) | (1L << (PHYSICAL - 70)) | (1L << (PIVOT - 70)) | (1L << (PLAN - 70)) | (1L << (RLIKE - 70)) | (1L << (QUERY - 70)) | (1L << (SCHEMAS - 70)) | (1L << (SECOND - 70)) | (1L << (SHOW - 70)) | (1L << (SYS - 70)) | (1L << (TABLES - 70)) | (1L << (TEXT - 70)) | (1L << (TOP - 70)) | (1L << (TYPE - 70)) | (1L << (TYPES - 70)) | (1L << (VERIFY - 70)) | (1L << (YEAR - 70)) | (1L << (IDENTIFIER - 70)) | (1L << (DIGIT_IDENTIFIER - 70)) | (1L << (QUOTED_IDENTIFIER - 70)))) != 0) || _la==BACKQUOTED_IDENTIFIER) { - { - setState(832); - ((TableIdentifierContext)_localctx).catalog = identifier(); - setState(833); - match(T__3); - } - } - - setState(837); - match(TABLE_IDENTIFIER); - } - break; - case 2: - enterOuterAlt(_localctx, 2); - { - setState(841); - _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,113,_ctx) ) { - case 1: - { - setState(838); - ((TableIdentifierContext)_localctx).catalog = identifier(); - setState(839); - match(T__3); - } - break; - } - setState(843); - ((TableIdentifierContext)_localctx).name = identifier(); - } - break; - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class QuoteIdentifierContext extends ParserRuleContext { - public QuoteIdentifierContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_quoteIdentifier; } - - public QuoteIdentifierContext() { } - public void copyFrom(QuoteIdentifierContext ctx) { - super.copyFrom(ctx); - } - } - public static class BackQuotedIdentifierContext extends QuoteIdentifierContext { - public TerminalNode BACKQUOTED_IDENTIFIER() { return getToken(SqlBaseParser.BACKQUOTED_IDENTIFIER, 0); } - public BackQuotedIdentifierContext(QuoteIdentifierContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterBackQuotedIdentifier(this); + + public static class TableIdentifierContext extends ParserRuleContext { + public IdentifierContext catalog; + public IdentifierContext name; + + public TerminalNode TABLE_IDENTIFIER() { + return getToken(SqlBaseParser.TABLE_IDENTIFIER, 0); + } + + public List identifier() { + return getRuleContexts(IdentifierContext.class); + } + + public IdentifierContext identifier(int i) { + return getRuleContext(IdentifierContext.class, i); + } + + public TableIdentifierContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_tableIdentifier; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterTableIdentifier(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitTableIdentifier(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitTableIdentifier(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitBackQuotedIdentifier(this); + + public final TableIdentifierContext tableIdentifier() throws RecognitionException { + TableIdentifierContext _localctx = new TableIdentifierContext(_ctx, getState()); + enterRule(_localctx, 106, RULE_tableIdentifier); + int _la; + try { + setState(844); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 114, _ctx)) { + case 1: + enterOuterAlt(_localctx, 1); { + setState(835); + _errHandler.sync(this); + _la = _input.LA(1); + if ((((_la) & ~0x3f) == 0 + && ((1L << _la) & ((1L << ANALYZE) | (1L << ANALYZED) | (1L << CATALOGS) | (1L << COLUMNS) | (1L << CURRENT_DATE) + | (1L << CURRENT_TIME) | (1L << CURRENT_TIMESTAMP) | (1L << DAY) | (1L << DEBUG) | (1L << EXECUTABLE) | (1L + << EXPLAIN) | (1L << FIRST) | (1L << FORMAT) | (1L << FULL) | (1L << FUNCTIONS) | (1L << GRAPHVIZ) | (1L + << HOUR) | (1L << INTERVAL) | (1L << LAST) | (1L << LIMIT) | (1L << MAPPED) | (1L << MINUTE) | (1L + << MONTH))) != 0) + || ((((_la - 70)) & ~0x3f) == 0 + && ((1L << (_la - 70)) & ((1L << (OPTIMIZED - 70)) | (1L << (PARSED - 70)) | (1L << (PHYSICAL - 70)) | (1L + << (PIVOT - 70)) | (1L << (PLAN - 70)) | (1L << (RLIKE - 70)) | (1L << (QUERY - 70)) | (1L << (SCHEMAS + - 70)) | (1L << (SECOND - 70)) | (1L << (SHOW - 70)) | (1L << (SYS - 70)) | (1L << (TABLES - 70)) | (1L + << (TEXT - 70)) | (1L << (TOP - 70)) | (1L << (TYPE - 70)) | (1L << (TYPES - 70)) | (1L << (VERIFY + - 70)) | (1L << (YEAR - 70)) | (1L << (IDENTIFIER - 70)) | (1L << (DIGIT_IDENTIFIER - 70)) | (1L + << (QUOTED_IDENTIFIER - 70)))) != 0) + || _la == BACKQUOTED_IDENTIFIER) { + { + setState(832); + ((TableIdentifierContext) _localctx).catalog = identifier(); + setState(833); + match(T__3); + } + } + + setState(837); + match(TABLE_IDENTIFIER); + } + break; + case 2: + enterOuterAlt(_localctx, 2); { + setState(841); + _errHandler.sync(this); + switch (getInterpreter().adaptivePredict(_input, 113, _ctx)) { + case 1: { + setState(838); + ((TableIdentifierContext) _localctx).catalog = identifier(); + setState(839); + match(T__3); + } + break; + } + setState(843); + ((TableIdentifierContext) _localctx).name = identifier(); + } + break; + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitBackQuotedIdentifier(this); - else return visitor.visitChildren(this); - } - } - public static class QuotedIdentifierContext extends QuoteIdentifierContext { - public TerminalNode QUOTED_IDENTIFIER() { return getToken(SqlBaseParser.QUOTED_IDENTIFIER, 0); } - public QuotedIdentifierContext(QuoteIdentifierContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterQuotedIdentifier(this); + + public static class QuoteIdentifierContext extends ParserRuleContext { + public QuoteIdentifierContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_quoteIdentifier; + } + + public QuoteIdentifierContext() {} + + public void copyFrom(QuoteIdentifierContext ctx) { + super.copyFrom(ctx); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitQuotedIdentifier(this); + + public static class BackQuotedIdentifierContext extends QuoteIdentifierContext { + public TerminalNode BACKQUOTED_IDENTIFIER() { + return getToken(SqlBaseParser.BACKQUOTED_IDENTIFIER, 0); + } + + public BackQuotedIdentifierContext(QuoteIdentifierContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterBackQuotedIdentifier(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitBackQuotedIdentifier(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitBackQuotedIdentifier(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitQuotedIdentifier(this); - else return visitor.visitChildren(this); - } - } - - public final QuoteIdentifierContext quoteIdentifier() throws RecognitionException { - QuoteIdentifierContext _localctx = new QuoteIdentifierContext(_ctx, getState()); - enterRule(_localctx, 108, RULE_quoteIdentifier); - try { - setState(848); - _errHandler.sync(this); - switch (_input.LA(1)) { - case QUOTED_IDENTIFIER: - _localctx = new QuotedIdentifierContext(_localctx); - enterOuterAlt(_localctx, 1); - { - setState(846); - match(QUOTED_IDENTIFIER); - } - break; - case BACKQUOTED_IDENTIFIER: - _localctx = new BackQuotedIdentifierContext(_localctx); - enterOuterAlt(_localctx, 2); - { - setState(847); - match(BACKQUOTED_IDENTIFIER); - } - break; - default: - throw new NoViableAltException(this); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class UnquoteIdentifierContext extends ParserRuleContext { - public UnquoteIdentifierContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_unquoteIdentifier; } - - public UnquoteIdentifierContext() { } - public void copyFrom(UnquoteIdentifierContext ctx) { - super.copyFrom(ctx); - } - } - public static class DigitIdentifierContext extends UnquoteIdentifierContext { - public TerminalNode DIGIT_IDENTIFIER() { return getToken(SqlBaseParser.DIGIT_IDENTIFIER, 0); } - public DigitIdentifierContext(UnquoteIdentifierContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterDigitIdentifier(this); + + public static class QuotedIdentifierContext extends QuoteIdentifierContext { + public TerminalNode QUOTED_IDENTIFIER() { + return getToken(SqlBaseParser.QUOTED_IDENTIFIER, 0); + } + + public QuotedIdentifierContext(QuoteIdentifierContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterQuotedIdentifier(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitQuotedIdentifier(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitQuotedIdentifier(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitDigitIdentifier(this); + + public final QuoteIdentifierContext quoteIdentifier() throws RecognitionException { + QuoteIdentifierContext _localctx = new QuoteIdentifierContext(_ctx, getState()); + enterRule(_localctx, 108, RULE_quoteIdentifier); + try { + setState(848); + _errHandler.sync(this); + switch (_input.LA(1)) { + case QUOTED_IDENTIFIER: + _localctx = new QuotedIdentifierContext(_localctx); + enterOuterAlt(_localctx, 1); { + setState(846); + match(QUOTED_IDENTIFIER); + } + break; + case BACKQUOTED_IDENTIFIER: + _localctx = new BackQuotedIdentifierContext(_localctx); + enterOuterAlt(_localctx, 2); { + setState(847); + match(BACKQUOTED_IDENTIFIER); + } + break; + default: + throw new NoViableAltException(this); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitDigitIdentifier(this); - else return visitor.visitChildren(this); + + public static class UnquoteIdentifierContext extends ParserRuleContext { + public UnquoteIdentifierContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_unquoteIdentifier; + } + + public UnquoteIdentifierContext() {} + + public void copyFrom(UnquoteIdentifierContext ctx) { + super.copyFrom(ctx); + } } - } - public static class UnquotedIdentifierContext extends UnquoteIdentifierContext { - public TerminalNode IDENTIFIER() { return getToken(SqlBaseParser.IDENTIFIER, 0); } - public NonReservedContext nonReserved() { - return getRuleContext(NonReservedContext.class,0); + + public static class DigitIdentifierContext extends UnquoteIdentifierContext { + public TerminalNode DIGIT_IDENTIFIER() { + return getToken(SqlBaseParser.DIGIT_IDENTIFIER, 0); + } + + public DigitIdentifierContext(UnquoteIdentifierContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterDigitIdentifier(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitDigitIdentifier(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitDigitIdentifier(this); + else return visitor.visitChildren(this); + } } - public UnquotedIdentifierContext(UnquoteIdentifierContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterUnquotedIdentifier(this); + + public static class UnquotedIdentifierContext extends UnquoteIdentifierContext { + public TerminalNode IDENTIFIER() { + return getToken(SqlBaseParser.IDENTIFIER, 0); + } + + public NonReservedContext nonReserved() { + return getRuleContext(NonReservedContext.class, 0); + } + + public UnquotedIdentifierContext(UnquoteIdentifierContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterUnquotedIdentifier(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitUnquotedIdentifier(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitUnquotedIdentifier(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitUnquotedIdentifier(this); + + public final UnquoteIdentifierContext unquoteIdentifier() throws RecognitionException { + UnquoteIdentifierContext _localctx = new UnquoteIdentifierContext(_ctx, getState()); + enterRule(_localctx, 110, RULE_unquoteIdentifier); + try { + setState(853); + _errHandler.sync(this); + switch (_input.LA(1)) { + case IDENTIFIER: + _localctx = new UnquotedIdentifierContext(_localctx); + enterOuterAlt(_localctx, 1); { + setState(850); + match(IDENTIFIER); + } + break; + case ANALYZE: + case ANALYZED: + case CATALOGS: + case COLUMNS: + case CURRENT_DATE: + case CURRENT_TIME: + case CURRENT_TIMESTAMP: + case DAY: + case DEBUG: + case EXECUTABLE: + case EXPLAIN: + case FIRST: + case FORMAT: + case FULL: + case FUNCTIONS: + case GRAPHVIZ: + case HOUR: + case INTERVAL: + case LAST: + case LIMIT: + case MAPPED: + case MINUTE: + case MONTH: + case OPTIMIZED: + case PARSED: + case PHYSICAL: + case PIVOT: + case PLAN: + case RLIKE: + case QUERY: + case SCHEMAS: + case SECOND: + case SHOW: + case SYS: + case TABLES: + case TEXT: + case TOP: + case TYPE: + case TYPES: + case VERIFY: + case YEAR: + _localctx = new UnquotedIdentifierContext(_localctx); + enterOuterAlt(_localctx, 2); { + setState(851); + nonReserved(); + } + break; + case DIGIT_IDENTIFIER: + _localctx = new DigitIdentifierContext(_localctx); + enterOuterAlt(_localctx, 3); { + setState(852); + match(DIGIT_IDENTIFIER); + } + break; + default: + throw new NoViableAltException(this); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitUnquotedIdentifier(this); - else return visitor.visitChildren(this); - } - } - - public final UnquoteIdentifierContext unquoteIdentifier() throws RecognitionException { - UnquoteIdentifierContext _localctx = new UnquoteIdentifierContext(_ctx, getState()); - enterRule(_localctx, 110, RULE_unquoteIdentifier); - try { - setState(853); - _errHandler.sync(this); - switch (_input.LA(1)) { - case IDENTIFIER: - _localctx = new UnquotedIdentifierContext(_localctx); - enterOuterAlt(_localctx, 1); - { - setState(850); - match(IDENTIFIER); - } - break; - case ANALYZE: - case ANALYZED: - case CATALOGS: - case COLUMNS: - case CURRENT_DATE: - case CURRENT_TIME: - case CURRENT_TIMESTAMP: - case DAY: - case DEBUG: - case EXECUTABLE: - case EXPLAIN: - case FIRST: - case FORMAT: - case FULL: - case FUNCTIONS: - case GRAPHVIZ: - case HOUR: - case INTERVAL: - case LAST: - case LIMIT: - case MAPPED: - case MINUTE: - case MONTH: - case OPTIMIZED: - case PARSED: - case PHYSICAL: - case PIVOT: - case PLAN: - case RLIKE: - case QUERY: - case SCHEMAS: - case SECOND: - case SHOW: - case SYS: - case TABLES: - case TEXT: - case TOP: - case TYPE: - case TYPES: - case VERIFY: - case YEAR: - _localctx = new UnquotedIdentifierContext(_localctx); - enterOuterAlt(_localctx, 2); - { - setState(851); - nonReserved(); - } - break; - case DIGIT_IDENTIFIER: - _localctx = new DigitIdentifierContext(_localctx); - enterOuterAlt(_localctx, 3); - { - setState(852); - match(DIGIT_IDENTIFIER); - } - break; - default: - throw new NoViableAltException(this); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class NumberContext extends ParserRuleContext { - public NumberContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_number; } - - public NumberContext() { } - public void copyFrom(NumberContext ctx) { - super.copyFrom(ctx); - } - } - public static class DecimalLiteralContext extends NumberContext { - public TerminalNode DECIMAL_VALUE() { return getToken(SqlBaseParser.DECIMAL_VALUE, 0); } - public DecimalLiteralContext(NumberContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterDecimalLiteral(this); + + public static class NumberContext extends ParserRuleContext { + public NumberContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_number; + } + + public NumberContext() {} + + public void copyFrom(NumberContext ctx) { + super.copyFrom(ctx); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitDecimalLiteral(this); + + public static class DecimalLiteralContext extends NumberContext { + public TerminalNode DECIMAL_VALUE() { + return getToken(SqlBaseParser.DECIMAL_VALUE, 0); + } + + public DecimalLiteralContext(NumberContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterDecimalLiteral(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitDecimalLiteral(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitDecimalLiteral(this); + else return visitor.visitChildren(this); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitDecimalLiteral(this); - else return visitor.visitChildren(this); - } - } - public static class IntegerLiteralContext extends NumberContext { - public TerminalNode INTEGER_VALUE() { return getToken(SqlBaseParser.INTEGER_VALUE, 0); } - public IntegerLiteralContext(NumberContext ctx) { copyFrom(ctx); } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterIntegerLiteral(this); + + public static class IntegerLiteralContext extends NumberContext { + public TerminalNode INTEGER_VALUE() { + return getToken(SqlBaseParser.INTEGER_VALUE, 0); + } + + public IntegerLiteralContext(NumberContext ctx) { + copyFrom(ctx); + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterIntegerLiteral(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitIntegerLiteral(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitIntegerLiteral(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitIntegerLiteral(this); + + public final NumberContext number() throws RecognitionException { + NumberContext _localctx = new NumberContext(_ctx, getState()); + enterRule(_localctx, 112, RULE_number); + try { + setState(857); + _errHandler.sync(this); + switch (_input.LA(1)) { + case DECIMAL_VALUE: + _localctx = new DecimalLiteralContext(_localctx); + enterOuterAlt(_localctx, 1); { + setState(855); + match(DECIMAL_VALUE); + } + break; + case INTEGER_VALUE: + _localctx = new IntegerLiteralContext(_localctx); + enterOuterAlt(_localctx, 2); { + setState(856); + match(INTEGER_VALUE); + } + break; + default: + throw new NoViableAltException(this); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitIntegerLiteral(this); - else return visitor.visitChildren(this); - } - } - - public final NumberContext number() throws RecognitionException { - NumberContext _localctx = new NumberContext(_ctx, getState()); - enterRule(_localctx, 112, RULE_number); - try { - setState(857); - _errHandler.sync(this); - switch (_input.LA(1)) { - case DECIMAL_VALUE: - _localctx = new DecimalLiteralContext(_localctx); - enterOuterAlt(_localctx, 1); - { - setState(855); - match(DECIMAL_VALUE); - } - break; - case INTEGER_VALUE: - _localctx = new IntegerLiteralContext(_localctx); - enterOuterAlt(_localctx, 2); - { - setState(856); - match(INTEGER_VALUE); - } - break; - default: - throw new NoViableAltException(this); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class StringContext extends ParserRuleContext { - public TerminalNode PARAM() { return getToken(SqlBaseParser.PARAM, 0); } - public TerminalNode STRING() { return getToken(SqlBaseParser.STRING, 0); } - public StringContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_string; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterString(this); + + public static class StringContext extends ParserRuleContext { + public TerminalNode PARAM() { + return getToken(SqlBaseParser.PARAM, 0); + } + + public TerminalNode STRING() { + return getToken(SqlBaseParser.STRING, 0); + } + + public StringContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_string; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterString(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitString(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitString(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitString(this); + + public final StringContext string() throws RecognitionException { + StringContext _localctx = new StringContext(_ctx, getState()); + enterRule(_localctx, 114, RULE_string); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(859); + _la = _input.LA(1); + if (!(_la == PARAM || _la == STRING)) { + _errHandler.recoverInline(this); + } else { + if (_input.LA(1) == Token.EOF) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitString(this); - else return visitor.visitChildren(this); - } - } - - public final StringContext string() throws RecognitionException { - StringContext _localctx = new StringContext(_ctx, getState()); - enterRule(_localctx, 114, RULE_string); - int _la; - try { - enterOuterAlt(_localctx, 1); - { - setState(859); - _la = _input.LA(1); - if ( !(_la==PARAM || _la==STRING) ) { - _errHandler.recoverInline(this); - } - else { - if ( _input.LA(1)==Token.EOF ) matchedEOF = true; - _errHandler.reportMatch(this); - consume(); - } - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class WhenClauseContext extends ParserRuleContext { - public ExpressionContext condition; - public ExpressionContext result; - public TerminalNode WHEN() { return getToken(SqlBaseParser.WHEN, 0); } - public TerminalNode THEN() { return getToken(SqlBaseParser.THEN, 0); } - public List expression() { - return getRuleContexts(ExpressionContext.class); - } - public ExpressionContext expression(int i) { - return getRuleContext(ExpressionContext.class,i); - } - public WhenClauseContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_whenClause; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterWhenClause(this); + + public static class WhenClauseContext extends ParserRuleContext { + public ExpressionContext condition; + public ExpressionContext result; + + public TerminalNode WHEN() { + return getToken(SqlBaseParser.WHEN, 0); + } + + public TerminalNode THEN() { + return getToken(SqlBaseParser.THEN, 0); + } + + public List expression() { + return getRuleContexts(ExpressionContext.class); + } + + public ExpressionContext expression(int i) { + return getRuleContext(ExpressionContext.class, i); + } + + public WhenClauseContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_whenClause; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterWhenClause(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitWhenClause(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitWhenClause(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitWhenClause(this); + + public final WhenClauseContext whenClause() throws RecognitionException { + WhenClauseContext _localctx = new WhenClauseContext(_ctx, getState()); + enterRule(_localctx, 116, RULE_whenClause); + try { + enterOuterAlt(_localctx, 1); + { + setState(861); + match(WHEN); + setState(862); + ((WhenClauseContext) _localctx).condition = expression(); + setState(863); + match(THEN); + setState(864); + ((WhenClauseContext) _localctx).result = expression(); + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitWhenClause(this); - else return visitor.visitChildren(this); - } - } - - public final WhenClauseContext whenClause() throws RecognitionException { - WhenClauseContext _localctx = new WhenClauseContext(_ctx, getState()); - enterRule(_localctx, 116, RULE_whenClause); - try { - enterOuterAlt(_localctx, 1); - { - setState(861); - match(WHEN); - setState(862); - ((WhenClauseContext)_localctx).condition = expression(); - setState(863); - match(THEN); - setState(864); - ((WhenClauseContext)_localctx).result = expression(); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static class NonReservedContext extends ParserRuleContext { - public TerminalNode ANALYZE() { return getToken(SqlBaseParser.ANALYZE, 0); } - public TerminalNode ANALYZED() { return getToken(SqlBaseParser.ANALYZED, 0); } - public TerminalNode CATALOGS() { return getToken(SqlBaseParser.CATALOGS, 0); } - public TerminalNode COLUMNS() { return getToken(SqlBaseParser.COLUMNS, 0); } - public TerminalNode CURRENT_DATE() { return getToken(SqlBaseParser.CURRENT_DATE, 0); } - public TerminalNode CURRENT_TIME() { return getToken(SqlBaseParser.CURRENT_TIME, 0); } - public TerminalNode CURRENT_TIMESTAMP() { return getToken(SqlBaseParser.CURRENT_TIMESTAMP, 0); } - public TerminalNode DAY() { return getToken(SqlBaseParser.DAY, 0); } - public TerminalNode DEBUG() { return getToken(SqlBaseParser.DEBUG, 0); } - public TerminalNode EXECUTABLE() { return getToken(SqlBaseParser.EXECUTABLE, 0); } - public TerminalNode EXPLAIN() { return getToken(SqlBaseParser.EXPLAIN, 0); } - public TerminalNode FIRST() { return getToken(SqlBaseParser.FIRST, 0); } - public TerminalNode FORMAT() { return getToken(SqlBaseParser.FORMAT, 0); } - public TerminalNode FULL() { return getToken(SqlBaseParser.FULL, 0); } - public TerminalNode FUNCTIONS() { return getToken(SqlBaseParser.FUNCTIONS, 0); } - public TerminalNode GRAPHVIZ() { return getToken(SqlBaseParser.GRAPHVIZ, 0); } - public TerminalNode HOUR() { return getToken(SqlBaseParser.HOUR, 0); } - public TerminalNode INTERVAL() { return getToken(SqlBaseParser.INTERVAL, 0); } - public TerminalNode LAST() { return getToken(SqlBaseParser.LAST, 0); } - public TerminalNode LIMIT() { return getToken(SqlBaseParser.LIMIT, 0); } - public TerminalNode MAPPED() { return getToken(SqlBaseParser.MAPPED, 0); } - public TerminalNode MINUTE() { return getToken(SqlBaseParser.MINUTE, 0); } - public TerminalNode MONTH() { return getToken(SqlBaseParser.MONTH, 0); } - public TerminalNode OPTIMIZED() { return getToken(SqlBaseParser.OPTIMIZED, 0); } - public TerminalNode PARSED() { return getToken(SqlBaseParser.PARSED, 0); } - public TerminalNode PHYSICAL() { return getToken(SqlBaseParser.PHYSICAL, 0); } - public TerminalNode PIVOT() { return getToken(SqlBaseParser.PIVOT, 0); } - public TerminalNode PLAN() { return getToken(SqlBaseParser.PLAN, 0); } - public TerminalNode QUERY() { return getToken(SqlBaseParser.QUERY, 0); } - public TerminalNode RLIKE() { return getToken(SqlBaseParser.RLIKE, 0); } - public TerminalNode SCHEMAS() { return getToken(SqlBaseParser.SCHEMAS, 0); } - public TerminalNode SECOND() { return getToken(SqlBaseParser.SECOND, 0); } - public TerminalNode SHOW() { return getToken(SqlBaseParser.SHOW, 0); } - public TerminalNode SYS() { return getToken(SqlBaseParser.SYS, 0); } - public TerminalNode TABLES() { return getToken(SqlBaseParser.TABLES, 0); } - public TerminalNode TEXT() { return getToken(SqlBaseParser.TEXT, 0); } - public TerminalNode TOP() { return getToken(SqlBaseParser.TOP, 0); } - public TerminalNode TYPE() { return getToken(SqlBaseParser.TYPE, 0); } - public TerminalNode TYPES() { return getToken(SqlBaseParser.TYPES, 0); } - public TerminalNode VERIFY() { return getToken(SqlBaseParser.VERIFY, 0); } - public TerminalNode YEAR() { return getToken(SqlBaseParser.YEAR, 0); } - public NonReservedContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_nonReserved; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).enterNonReserved(this); + + public static class NonReservedContext extends ParserRuleContext { + public TerminalNode ANALYZE() { + return getToken(SqlBaseParser.ANALYZE, 0); + } + + public TerminalNode ANALYZED() { + return getToken(SqlBaseParser.ANALYZED, 0); + } + + public TerminalNode CATALOGS() { + return getToken(SqlBaseParser.CATALOGS, 0); + } + + public TerminalNode COLUMNS() { + return getToken(SqlBaseParser.COLUMNS, 0); + } + + public TerminalNode CURRENT_DATE() { + return getToken(SqlBaseParser.CURRENT_DATE, 0); + } + + public TerminalNode CURRENT_TIME() { + return getToken(SqlBaseParser.CURRENT_TIME, 0); + } + + public TerminalNode CURRENT_TIMESTAMP() { + return getToken(SqlBaseParser.CURRENT_TIMESTAMP, 0); + } + + public TerminalNode DAY() { + return getToken(SqlBaseParser.DAY, 0); + } + + public TerminalNode DEBUG() { + return getToken(SqlBaseParser.DEBUG, 0); + } + + public TerminalNode EXECUTABLE() { + return getToken(SqlBaseParser.EXECUTABLE, 0); + } + + public TerminalNode EXPLAIN() { + return getToken(SqlBaseParser.EXPLAIN, 0); + } + + public TerminalNode FIRST() { + return getToken(SqlBaseParser.FIRST, 0); + } + + public TerminalNode FORMAT() { + return getToken(SqlBaseParser.FORMAT, 0); + } + + public TerminalNode FULL() { + return getToken(SqlBaseParser.FULL, 0); + } + + public TerminalNode FUNCTIONS() { + return getToken(SqlBaseParser.FUNCTIONS, 0); + } + + public TerminalNode GRAPHVIZ() { + return getToken(SqlBaseParser.GRAPHVIZ, 0); + } + + public TerminalNode HOUR() { + return getToken(SqlBaseParser.HOUR, 0); + } + + public TerminalNode INTERVAL() { + return getToken(SqlBaseParser.INTERVAL, 0); + } + + public TerminalNode LAST() { + return getToken(SqlBaseParser.LAST, 0); + } + + public TerminalNode LIMIT() { + return getToken(SqlBaseParser.LIMIT, 0); + } + + public TerminalNode MAPPED() { + return getToken(SqlBaseParser.MAPPED, 0); + } + + public TerminalNode MINUTE() { + return getToken(SqlBaseParser.MINUTE, 0); + } + + public TerminalNode MONTH() { + return getToken(SqlBaseParser.MONTH, 0); + } + + public TerminalNode OPTIMIZED() { + return getToken(SqlBaseParser.OPTIMIZED, 0); + } + + public TerminalNode PARSED() { + return getToken(SqlBaseParser.PARSED, 0); + } + + public TerminalNode PHYSICAL() { + return getToken(SqlBaseParser.PHYSICAL, 0); + } + + public TerminalNode PIVOT() { + return getToken(SqlBaseParser.PIVOT, 0); + } + + public TerminalNode PLAN() { + return getToken(SqlBaseParser.PLAN, 0); + } + + public TerminalNode QUERY() { + return getToken(SqlBaseParser.QUERY, 0); + } + + public TerminalNode RLIKE() { + return getToken(SqlBaseParser.RLIKE, 0); + } + + public TerminalNode SCHEMAS() { + return getToken(SqlBaseParser.SCHEMAS, 0); + } + + public TerminalNode SECOND() { + return getToken(SqlBaseParser.SECOND, 0); + } + + public TerminalNode SHOW() { + return getToken(SqlBaseParser.SHOW, 0); + } + + public TerminalNode SYS() { + return getToken(SqlBaseParser.SYS, 0); + } + + public TerminalNode TABLES() { + return getToken(SqlBaseParser.TABLES, 0); + } + + public TerminalNode TEXT() { + return getToken(SqlBaseParser.TEXT, 0); + } + + public TerminalNode TOP() { + return getToken(SqlBaseParser.TOP, 0); + } + + public TerminalNode TYPE() { + return getToken(SqlBaseParser.TYPE, 0); + } + + public TerminalNode TYPES() { + return getToken(SqlBaseParser.TYPES, 0); + } + + public TerminalNode VERIFY() { + return getToken(SqlBaseParser.VERIFY, 0); + } + + public TerminalNode YEAR() { + return getToken(SqlBaseParser.YEAR, 0); + } + + public NonReservedContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + + @Override + public int getRuleIndex() { + return RULE_nonReserved; + } + + @Override + public void enterRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).enterNonReserved(this); + } + + @Override + public void exitRule(ParseTreeListener listener) { + if (listener instanceof SqlBaseListener) ((SqlBaseListener) listener).exitNonReserved(this); + } + + @Override + public T accept(ParseTreeVisitor visitor) { + if (visitor instanceof SqlBaseVisitor) return ((SqlBaseVisitor) visitor).visitNonReserved(this); + else return visitor.visitChildren(this); + } } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SqlBaseListener ) ((SqlBaseListener)listener).exitNonReserved(this); + + public final NonReservedContext nonReserved() throws RecognitionException { + NonReservedContext _localctx = new NonReservedContext(_ctx, getState()); + enterRule(_localctx, 118, RULE_nonReserved); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(866); + _la = _input.LA(1); + if (!((((_la) & ~0x3f) == 0 + && ((1L << _la) & ((1L << ANALYZE) | (1L << ANALYZED) | (1L << CATALOGS) | (1L << COLUMNS) | (1L << CURRENT_DATE) | (1L + << CURRENT_TIME) | (1L << CURRENT_TIMESTAMP) | (1L << DAY) | (1L << DEBUG) | (1L << EXECUTABLE) | (1L << EXPLAIN) + | (1L << FIRST) | (1L << FORMAT) | (1L << FULL) | (1L << FUNCTIONS) | (1L << GRAPHVIZ) | (1L << HOUR) | (1L + << INTERVAL) | (1L << LAST) | (1L << LIMIT) | (1L << MAPPED) | (1L << MINUTE) | (1L << MONTH))) != 0) + || ((((_la - 70)) & ~0x3f) == 0 + && ((1L << (_la - 70)) & ((1L << (OPTIMIZED - 70)) | (1L << (PARSED - 70)) | (1L << (PHYSICAL - 70)) | (1L << (PIVOT + - 70)) | (1L << (PLAN - 70)) | (1L << (RLIKE - 70)) | (1L << (QUERY - 70)) | (1L << (SCHEMAS - 70)) | (1L + << (SECOND - 70)) | (1L << (SHOW - 70)) | (1L << (SYS - 70)) | (1L << (TABLES - 70)) | (1L << (TEXT - 70)) + | (1L << (TOP - 70)) | (1L << (TYPE - 70)) | (1L << (TYPES - 70)) | (1L << (VERIFY - 70)) | (1L << (YEAR + - 70)))) != 0))) { + _errHandler.recoverInline(this); + } else { + if (_input.LA(1) == Token.EOF) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + } + } catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } finally { + exitRule(); + } + return _localctx; + } + + public boolean sempred(RuleContext _localctx, int ruleIndex, int predIndex) { + switch (ruleIndex) { + case 27: + return booleanExpression_sempred((BooleanExpressionContext) _localctx, predIndex); + case 34: + return valueExpression_sempred((ValueExpressionContext) _localctx, predIndex); + case 35: + return primaryExpression_sempred((PrimaryExpressionContext) _localctx, predIndex); + } + return true; + } + + private boolean booleanExpression_sempred(BooleanExpressionContext _localctx, int predIndex) { + switch (predIndex) { + case 0: + return precpred(_ctx, 2); + case 1: + return precpred(_ctx, 1); + } + return true; + } + + private boolean valueExpression_sempred(ValueExpressionContext _localctx, int predIndex) { + switch (predIndex) { + case 2: + return precpred(_ctx, 3); + case 3: + return precpred(_ctx, 2); + case 4: + return precpred(_ctx, 1); + } + return true; + } + + private boolean primaryExpression_sempred(PrimaryExpressionContext _localctx, int predIndex) { + switch (predIndex) { + case 5: + return precpred(_ctx, 10); + } + return true; + } + + public static final String _serializedATN = "\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\3\u008d\u0367\4\2\t" + + "\2\4\3\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b\t\b\4\t\t\t\4\n\t\n\4\13" + + "\t\13\4\f\t\f\4\r\t\r\4\16\t\16\4\17\t\17\4\20\t\20\4\21\t\21\4\22\t\22" + + "\4\23\t\23\4\24\t\24\4\25\t\25\4\26\t\26\4\27\t\27\4\30\t\30\4\31\t\31" + + "\4\32\t\32\4\33\t\33\4\34\t\34\4\35\t\35\4\36\t\36\4\37\t\37\4 \t \4!" + + "\t!\4\"\t\"\4#\t#\4$\t$\4%\t%\4&\t&\4\'\t\'\4(\t(\4)\t)\4*\t*\4+\t+\4" + + ",\t,\4-\t-\4.\t.\4/\t/\4\60\t\60\4\61\t\61\4\62\t\62\4\63\t\63\4\64\t" + + "\64\4\65\t\65\4\66\t\66\4\67\t\67\48\t8\49\t9\4:\t:\4;\t;\4<\t<\4=\t=" + + "\3\2\3\2\3\2\3\3\3\3\3\3\3\4\3\4\3\4\3\4\3\4\3\4\3\4\3\4\3\4\7\4\u008a" + + "\n\4\f\4\16\4\u008d\13\4\3\4\5\4\u0090\n\4\3\4\3\4\3\4\3\4\3\4\3\4\3\4" + + "\7\4\u0099\n\4\f\4\16\4\u009c\13\4\3\4\5\4\u009f\n\4\3\4\3\4\3\4\3\4\3" + + "\4\3\4\5\4\u00a7\n\4\5\4\u00a9\n\4\3\4\3\4\5\4\u00ad\n\4\3\4\3\4\5\4\u00b1" + + "\n\4\3\4\3\4\3\4\3\4\5\4\u00b7\n\4\3\4\3\4\5\4\u00bb\n\4\3\4\3\4\3\4\5" + + "\4\u00c0\n\4\3\4\3\4\3\4\5\4\u00c5\n\4\3\4\3\4\5\4\u00c9\n\4\3\4\3\4\5" + + "\4\u00cd\n\4\3\4\3\4\3\4\5\4\u00d2\n\4\3\4\3\4\3\4\3\4\3\4\3\4\3\4\3\4" + + "\5\4\u00dc\n\4\3\4\3\4\5\4\u00e0\n\4\3\4\3\4\3\4\3\4\7\4\u00e6\n\4\f\4" + + "\16\4\u00e9\13\4\5\4\u00eb\n\4\3\4\3\4\3\4\3\4\5\4\u00f1\n\4\3\4\3\4\3" + + "\4\5\4\u00f6\n\4\3\4\5\4\u00f9\n\4\3\4\3\4\3\4\5\4\u00fe\n\4\3\4\5\4\u0101" + + "\n\4\5\4\u0103\n\4\3\5\3\5\3\5\3\5\7\5\u0109\n\5\f\5\16\5\u010c\13\5\5" + + "\5\u010e\n\5\3\5\3\5\3\6\3\6\3\6\3\6\3\6\3\6\7\6\u0118\n\6\f\6\16\6\u011b" + + "\13\6\5\6\u011d\n\6\3\6\5\6\u0120\n\6\3\7\3\7\3\7\3\7\3\7\5\7\u0127\n" + + "\7\3\b\3\b\3\b\3\b\3\b\5\b\u012e\n\b\3\t\3\t\5\t\u0132\n\t\3\t\3\t\5\t" + + "\u0136\n\t\3\n\3\n\5\n\u013a\n\n\3\n\5\n\u013d\n\n\3\n\3\n\5\n\u0141\n" + + "\n\3\n\3\n\5\n\u0145\n\n\3\n\3\n\3\n\5\n\u014a\n\n\3\n\3\n\5\n\u014e\n" + + "\n\3\13\3\13\3\13\3\13\7\13\u0154\n\13\f\13\16\13\u0157\13\13\3\13\5\13" + + "\u015a\n\13\3\f\5\f\u015d\n\f\3\f\3\f\3\f\7\f\u0162\n\f\f\f\16\f\u0165" + + "\13\f\3\r\3\r\3\16\3\16\3\16\3\16\7\16\u016d\n\16\f\16\16\16\u0170\13" + + "\16\5\16\u0172\n\16\3\16\3\16\5\16\u0176\n\16\3\17\3\17\3\17\3\17\3\17" + + "\3\17\3\20\3\20\3\20\3\21\3\21\3\22\3\22\3\22\7\22\u0186\n\22\f\22\16" + + "\22\u0189\13\22\3\23\3\23\5\23\u018d\n\23\3\23\5\23\u0190\n\23\3\24\3" + + "\24\7\24\u0194\n\24\f\24\16\24\u0197\13\24\3\25\3\25\3\25\3\25\5\25\u019d" + + "\n\25\3\25\3\25\3\25\3\25\3\25\5\25\u01a4\n\25\3\26\5\26\u01a7\n\26\3" + + "\26\3\26\5\26\u01ab\n\26\3\26\3\26\5\26\u01af\n\26\3\26\3\26\5\26\u01b3" + + "\n\26\5\26\u01b5\n\26\3\27\3\27\3\27\3\27\3\27\3\27\3\27\7\27\u01be\n" + + "\27\f\27\16\27\u01c1\13\27\3\27\3\27\5\27\u01c5\n\27\3\30\5\30\u01c8\n" + + "\30\3\30\3\30\5\30\u01cc\n\30\3\30\5\30\u01cf\n\30\3\30\3\30\3\30\3\30" + + "\5\30\u01d5\n\30\3\30\5\30\u01d8\n\30\3\30\3\30\3\30\3\30\5\30\u01de\n" + + "\30\3\30\5\30\u01e1\n\30\5\30\u01e3\n\30\3\31\3\31\3\31\3\31\3\31\3\31" + + "\3\31\3\31\3\31\3\31\3\31\3\32\3\32\3\32\7\32\u01f3\n\32\f\32\16\32\u01f6" + + "\13\32\3\33\3\33\5\33\u01fa\n\33\3\33\5\33\u01fd\n\33\3\34\3\34\3\35\3" + + "\35\3\35\3\35\3\35\3\35\3\35\3\35\3\35\3\35\3\35\3\35\3\35\3\35\3\35\3" + + "\35\3\35\3\35\3\35\3\35\3\35\3\35\3\35\3\35\3\35\3\35\3\35\3\35\3\35\3" + + "\35\3\35\5\35\u0220\n\35\3\35\3\35\3\35\3\35\3\35\3\35\7\35\u0228\n\35" + + "\f\35\16\35\u022b\13\35\3\36\3\36\7\36\u022f\n\36\f\36\16\36\u0232\13" + + "\36\3\37\3\37\5\37\u0236\n\37\3 \5 \u0239\n \3 \3 \3 \3 \3 \3 \5 \u0241" + + "\n \3 \3 \3 \3 \3 \7 \u0248\n \f \16 \u024b\13 \3 \3 \3 \5 \u0250\n \3" + + " \3 \3 \3 \3 \3 \5 \u0258\n \3 \3 \3 \5 \u025d\n \3 \3 \3 \3 \5 \u0263" + + "\n \3 \5 \u0266\n \3!\3!\3!\3\"\3\"\5\"\u026d\n\"\3#\3#\3#\3#\3#\3#\5" + + "#\u0275\n#\3$\3$\3$\3$\5$\u027b\n$\3$\3$\3$\3$\3$\3$\3$\3$\3$\3$\7$\u0287" + + "\n$\f$\16$\u028a\13$\3%\3%\3%\3%\3%\3%\3%\3%\5%\u0294\n%\3%\3%\3%\3%\3" + + "%\3%\3%\3%\3%\3%\3%\3%\3%\5%\u02a3\n%\3%\6%\u02a6\n%\r%\16%\u02a7\3%\3" + + "%\5%\u02ac\n%\3%\3%\5%\u02b0\n%\3%\3%\3%\7%\u02b5\n%\f%\16%\u02b8\13%" + + "\3&\3&\3&\5&\u02bd\n&\3\'\3\'\3\'\3\'\3\'\3\'\3\'\3\'\3\'\3\'\5\'\u02c9" + + "\n\'\3(\3(\3(\3(\3(\3(\3(\3)\3)\3)\3)\3)\3)\3)\3*\3*\3*\3*\3*\5*\u02de" + + "\n*\3+\3+\3+\3+\3+\3+\3+\3,\3,\3,\3,\3,\5,\u02ec\n,\3-\3-\3-\5-\u02f1" + + "\n-\3-\3-\3-\7-\u02f6\n-\f-\16-\u02f9\13-\5-\u02fb\n-\3-\3-\3.\3.\3.\5" + + ".\u0302\n.\3/\3/\3/\3/\3/\6/\u0309\n/\r/\16/\u030a\3/\3/\3/\3/\3/\3/\3" + + "/\3/\3/\3/\3/\3/\3/\3/\3/\3/\3/\5/\u031e\n/\3\60\3\60\3\61\3\61\3\62\3" + + "\62\5\62\u0326\n\62\3\62\3\62\5\62\u032a\n\62\3\62\3\62\3\62\5\62\u032f" + + "\n\62\3\63\3\63\3\64\3\64\3\65\3\65\3\65\7\65\u0338\n\65\f\65\16\65\u033b" + + "\13\65\3\65\3\65\3\66\3\66\5\66\u0341\n\66\3\67\3\67\3\67\5\67\u0346\n" + + "\67\3\67\3\67\3\67\3\67\5\67\u034c\n\67\3\67\5\67\u034f\n\67\38\38\58" + + "\u0353\n8\39\39\39\59\u0358\n9\3:\3:\5:\u035c\n:\3;\3;\3<\3<\3<\3<\3<" + + "\3=\3=\3=\2\58FH>\2\4\6\b\n\f\16\20\22\24\26\30\32\34\36 \"$&(*,.\60\62" + + "\64\668:<>@BDFHJLNPRTVXZ\\^`bdfhjlnprtvx\2\22\b\2\7\7\t\t\"\"==HHLL\4" + + "\2..[[\4\2\t\tHH\4\2**\63\63\3\2\34\35\3\2yz\4\2\7\7\u0082\u0082\4\2\r" + + "\r\34\34\4\2\'\'99\4\2\7\7\36\36\3\2{}\3\2rx\4\2&&]]\7\2\31\32\61\62?" + + "BTUgh\3\2\u0080\u0081\31\2\b\t\23\24\26\31\33\33\"\"$$\'\')),.\61\61\66" + + "\6699<=??AAHHLOQTWXZ[_accgg\2\u03cf\2z\3\2\2\2\4}\3\2\2\2\6\u0102\3\2" + + "\2\2\b\u010d\3\2\2\2\n\u0111\3\2\2\2\f\u0126\3\2\2\2\16\u012d\3\2\2\2" + + "\20\u012f\3\2\2\2\22\u0137\3\2\2\2\24\u014f\3\2\2\2\26\u015c\3\2\2\2\30" + + "\u0166\3\2\2\2\32\u0175\3\2\2\2\34\u0177\3\2\2\2\36\u017d\3\2\2\2 \u0180" + + "\3\2\2\2\"\u0182\3\2\2\2$\u018a\3\2\2\2&\u0191\3\2\2\2(\u01a3\3\2\2\2" + + "*\u01b4\3\2\2\2,\u01c4\3\2\2\2.\u01e2\3\2\2\2\60\u01e4\3\2\2\2\62\u01ef" + + "\3\2\2\2\64\u01f7\3\2\2\2\66\u01fe\3\2\2\28\u021f\3\2\2\2:\u0230\3\2\2" + + "\2<\u0233\3\2\2\2>\u0265\3\2\2\2@\u0267\3\2\2\2B\u026a\3\2\2\2D\u0274" + + "\3\2\2\2F\u027a\3\2\2\2H\u02af\3\2\2\2J\u02bc\3\2\2\2L\u02c8\3\2\2\2N" + + "\u02ca\3\2\2\2P\u02d1\3\2\2\2R\u02dd\3\2\2\2T\u02df\3\2\2\2V\u02eb\3\2" + + "\2\2X\u02ed\3\2\2\2Z\u0301\3\2\2\2\\\u031d\3\2\2\2^\u031f\3\2\2\2`\u0321" + + "\3\2\2\2b\u0323\3\2\2\2d\u0330\3\2\2\2f\u0332\3\2\2\2h\u0339\3\2\2\2j" + + "\u0340\3\2\2\2l\u034e\3\2\2\2n\u0352\3\2\2\2p\u0357\3\2\2\2r\u035b\3\2" + + "\2\2t\u035d\3\2\2\2v\u035f\3\2\2\2x\u0364\3\2\2\2z{\5\6\4\2{|\7\2\2\3" + + "|\3\3\2\2\2}~\5\66\34\2~\177\7\2\2\3\177\5\3\2\2\2\u0080\u0103\5\b\5\2" + + "\u0081\u008f\7$\2\2\u0082\u008b\7\3\2\2\u0083\u0084\7O\2\2\u0084\u008a" + + "\t\2\2\2\u0085\u0086\7)\2\2\u0086\u008a\t\3\2\2\u0087\u0088\7c\2\2\u0088" + + "\u008a\5`\61\2\u0089\u0083\3\2\2\2\u0089\u0085\3\2\2\2\u0089\u0087\3\2" + + "\2\2\u008a\u008d\3\2\2\2\u008b\u0089\3\2\2\2\u008b\u008c\3\2\2\2\u008c" + + "\u008e\3\2\2\2\u008d\u008b\3\2\2\2\u008e\u0090\7\4\2\2\u008f\u0082\3\2" + + "\2\2\u008f\u0090\3\2\2\2\u0090\u0091\3\2\2\2\u0091\u0103\5\6\4\2\u0092" + + "\u009e\7\33\2\2\u0093\u009a\7\3\2\2\u0094\u0095\7O\2\2\u0095\u0099\t\4" + + "\2\2\u0096\u0097\7)\2\2\u0097\u0099\t\3\2\2\u0098\u0094\3\2\2\2\u0098" + + "\u0096\3\2\2\2\u0099\u009c\3\2\2\2\u009a\u0098\3\2\2\2\u009a\u009b\3\2" + + "\2\2\u009b\u009d\3\2\2\2\u009c\u009a\3\2\2\2\u009d\u009f\7\4\2\2\u009e" + + "\u0093\3\2\2\2\u009e\u009f\3\2\2\2\u009f\u00a0\3\2\2\2\u00a0\u0103\5\6" + + "\4\2\u00a1\u00a2\7W\2\2\u00a2\u00a8\7Z\2\2\u00a3\u00a6\7\22\2\2\u00a4" + + "\u00a7\5@!\2\u00a5\u00a7\5t;\2\u00a6\u00a4\3\2\2\2\u00a6\u00a5\3\2\2\2" + + "\u00a7\u00a9\3\2\2\2\u00a8\u00a3\3\2\2\2\u00a8\u00a9\3\2\2\2\u00a9\u00ac" + + "\3\2\2\2\u00aa\u00ab\7\64\2\2\u00ab\u00ad\7+\2\2\u00ac\u00aa\3\2\2\2\u00ac" + + "\u00ad\3\2\2\2\u00ad\u00b0\3\2\2\2\u00ae\u00b1\5@!\2\u00af\u00b1\5l\67" + + "\2\u00b0\u00ae\3\2\2\2\u00b0\u00af\3\2\2\2\u00b0\u00b1\3\2\2\2\u00b1\u0103" + + "\3\2\2\2\u00b2\u00b3\7W\2\2\u00b3\u00b6\7\24\2\2\u00b4\u00b5\7\22\2\2" + + "\u00b5\u00b7\5t;\2\u00b6\u00b4\3\2\2\2\u00b6\u00b7\3\2\2\2\u00b7\u00ba" + + "\3\2\2\2\u00b8\u00b9\7\64\2\2\u00b9\u00bb\7+\2\2\u00ba\u00b8\3\2\2\2\u00ba" + + "\u00bb\3\2\2\2\u00bb\u00bc\3\2\2\2\u00bc\u00bf\t\5\2\2\u00bd\u00c0\5@" + + "!\2\u00be\u00c0\5l\67\2\u00bf\u00bd\3\2\2\2\u00bf\u00be\3\2\2\2\u00c0" + + "\u0103\3\2\2\2\u00c1\u00c4\t\6\2\2\u00c2\u00c3\7\22\2\2\u00c3\u00c5\5" + + "t;\2\u00c4\u00c2\3\2\2\2\u00c4\u00c5\3\2\2\2\u00c5\u00c8\3\2\2\2\u00c6" + + "\u00c7\7\64\2\2\u00c7\u00c9\7+\2\2\u00c8\u00c6\3\2\2\2\u00c8\u00c9\3\2" + + "\2\2\u00c9\u00cc\3\2\2\2\u00ca\u00cd\5@!\2\u00cb\u00cd\5l\67\2\u00cc\u00ca" + + "\3\2\2\2\u00cc\u00cb\3\2\2\2\u00cd\u0103\3\2\2\2\u00ce\u00cf\7W\2\2\u00cf" + + "\u00d1\7-\2\2\u00d0\u00d2\5@!\2\u00d1\u00d0\3\2\2\2\u00d1\u00d2\3\2\2" + + "\2\u00d2\u0103\3\2\2\2\u00d3\u00d4\7W\2\2\u00d4\u0103\7S\2\2\u00d5\u00d6" + + "\7W\2\2\u00d6\u0103\7\23\2\2\u00d7\u00d8\7X\2\2\u00d8\u00db\7Z\2\2\u00d9" + + "\u00da\7\22\2\2\u00da\u00dc\5@!\2\u00db\u00d9\3\2\2\2\u00db\u00dc\3\2" + + "\2\2\u00dc\u00df\3\2\2\2\u00dd\u00e0\5@!\2\u00de\u00e0\5l\67\2\u00df\u00dd" + + "\3\2\2\2\u00df\u00de\3\2\2\2\u00df\u00e0\3\2\2\2\u00e0\u00ea\3\2\2\2\u00e1" + + "\u00e2\7`\2\2\u00e2\u00e7\5t;\2\u00e3\u00e4\7\5\2\2\u00e4\u00e6\5t;\2" + + "\u00e5\u00e3\3\2\2\2\u00e6\u00e9\3\2\2\2\u00e7\u00e5\3\2\2\2\u00e7\u00e8" + + "\3\2\2\2\u00e8\u00eb\3\2\2\2\u00e9\u00e7\3\2\2\2\u00ea\u00e1\3\2\2\2\u00ea" + + "\u00eb\3\2\2\2\u00eb\u0103\3\2\2\2\u00ec\u00ed\7X\2\2\u00ed\u00f0\7\24" + + "\2\2\u00ee\u00ef\7\22\2\2\u00ef\u00f1\5t;\2\u00f0\u00ee\3\2\2\2\u00f0" + + "\u00f1\3\2\2\2\u00f1\u00f5\3\2\2\2\u00f2\u00f3\7Y\2\2\u00f3\u00f6\5@!" + + "\2\u00f4\u00f6\5l\67\2\u00f5\u00f2\3\2\2\2\u00f5\u00f4\3\2\2\2\u00f5\u00f6" + + "\3\2\2\2\u00f6\u00f8\3\2\2\2\u00f7\u00f9\5@!\2\u00f8\u00f7\3\2\2\2\u00f8" + + "\u00f9\3\2\2\2\u00f9\u0103\3\2\2\2\u00fa\u00fb\7X\2\2\u00fb\u0100\7a\2" + + "\2\u00fc\u00fe\t\7\2\2\u00fd\u00fc\3\2\2\2\u00fd\u00fe\3\2\2\2\u00fe\u00ff" + + "\3\2\2\2\u00ff\u0101\5r:\2\u0100\u00fd\3\2\2\2\u0100\u0101\3\2\2\2\u0101" + + "\u0103\3\2\2\2\u0102\u0080\3\2\2\2\u0102\u0081\3\2\2\2\u0102\u0092\3\2" + + "\2\2\u0102\u00a1\3\2\2\2\u0102\u00b2\3\2\2\2\u0102\u00c1\3\2\2\2\u0102" + + "\u00ce\3\2\2\2\u0102\u00d3\3\2\2\2\u0102\u00d5\3\2\2\2\u0102\u00d7\3\2" + + "\2\2\u0102\u00ec\3\2\2\2\u0102\u00fa\3\2\2\2\u0103\7\3\2\2\2\u0104\u0105" + + "\7f\2\2\u0105\u010a\5\34\17\2\u0106\u0107\7\5\2\2\u0107\u0109\5\34\17" + + "\2\u0108\u0106\3\2\2\2\u0109\u010c\3\2\2\2\u010a\u0108\3\2\2\2\u010a\u010b" + + "\3\2\2\2\u010b\u010e\3\2\2\2\u010c\u010a\3\2\2\2\u010d\u0104\3\2\2\2\u010d" + + "\u010e\3\2\2\2\u010e\u010f\3\2\2\2\u010f\u0110\5\n\6\2\u0110\t\3\2\2\2" + + "\u0111\u011c\5\16\b\2\u0112\u0113\7J\2\2\u0113\u0114\7\17\2\2\u0114\u0119" + + "\5\20\t\2\u0115\u0116\7\5\2\2\u0116\u0118\5\20\t\2\u0117\u0115\3\2\2\2" + + "\u0118\u011b\3\2\2\2\u0119\u0117\3\2\2\2\u0119\u011a\3\2\2\2\u011a\u011d" + + "\3\2\2\2\u011b\u0119\3\2\2\2\u011c\u0112\3\2\2\2\u011c\u011d\3\2\2\2\u011d" + + "\u011f\3\2\2\2\u011e\u0120\5\f\7\2\u011f\u011e\3\2\2\2\u011f\u0120\3\2" + + "\2\2\u0120\13\3\2\2\2\u0121\u0122\7<\2\2\u0122\u0127\t\b\2\2\u0123\u0124" + + "\7k\2\2\u0124\u0125\t\b\2\2\u0125\u0127\7q\2\2\u0126\u0121\3\2\2\2\u0126" + + "\u0123\3\2\2\2\u0127\r\3\2\2\2\u0128\u012e\5\22\n\2\u0129\u012a\7\3\2" + + "\2\u012a\u012b\5\n\6\2\u012b\u012c\7\4\2\2\u012c\u012e\3\2\2\2\u012d\u0128" + + "\3\2\2\2\u012d\u0129\3\2\2\2\u012e\17\3\2\2\2\u012f\u0131\5\66\34\2\u0130" + + "\u0132\t\t\2\2\u0131\u0130\3\2\2\2\u0131\u0132\3\2\2\2\u0132\u0135\3\2" + + "\2\2\u0133\u0134\7F\2\2\u0134\u0136\t\n\2\2\u0135\u0133\3\2\2\2\u0135" + + "\u0136\3\2\2\2\u0136\21\3\2\2\2\u0137\u0139\7V\2\2\u0138\u013a\5\36\20" + + "\2\u0139\u0138\3\2\2\2\u0139\u013a\3\2\2\2\u013a\u013c\3\2\2\2\u013b\u013d" + + "\5 \21\2\u013c\u013b\3\2\2\2\u013c\u013d\3\2\2\2\u013d\u013e\3\2\2\2\u013e" + + "\u0140\5\"\22\2\u013f\u0141\5\24\13\2\u0140\u013f\3\2\2\2\u0140\u0141" + + "\3\2\2\2\u0141\u0144\3\2\2\2\u0142\u0143\7e\2\2\u0143\u0145\58\35\2\u0144" + + "\u0142\3\2\2\2\u0144\u0145\3\2\2\2\u0145\u0149\3\2\2\2\u0146\u0147\7/" + + "\2\2\u0147\u0148\7\17\2\2\u0148\u014a\5\26\f\2\u0149\u0146\3\2\2\2\u0149" + + "\u014a\3\2\2\2\u014a\u014d\3\2\2\2\u014b\u014c\7\60\2\2\u014c\u014e\5" + + "8\35\2\u014d\u014b\3\2\2\2\u014d\u014e\3\2\2\2\u014e\23\3\2\2\2\u014f" + + "\u0150\7*\2\2\u0150\u0155\5&\24\2\u0151\u0152\7\5\2\2\u0152\u0154\5&\24" + + "\2\u0153\u0151\3\2\2\2\u0154\u0157\3\2\2\2\u0155\u0153\3\2\2\2\u0155\u0156" + + "\3\2\2\2\u0156\u0159\3\2\2\2\u0157\u0155\3\2\2\2\u0158\u015a\5\60\31\2" + + "\u0159\u0158\3\2\2\2\u0159\u015a\3\2\2\2\u015a\25\3\2\2\2\u015b\u015d" + + "\5 \21\2\u015c\u015b\3\2\2\2\u015c\u015d\3\2\2\2\u015d\u015e\3\2\2\2\u015e" + + "\u0163\5\30\r\2\u015f\u0160\7\5\2\2\u0160\u0162\5\30\r\2\u0161\u015f\3" + + "\2\2\2\u0162\u0165\3\2\2\2\u0163\u0161\3\2\2\2\u0163\u0164\3\2\2\2\u0164" + + "\27\3\2\2\2\u0165\u0163\3\2\2\2\u0166\u0167\5\32\16\2\u0167\31\3\2\2\2" + + "\u0168\u0171\7\3\2\2\u0169\u016e\5\66\34\2\u016a\u016b\7\5\2\2\u016b\u016d" + + "\5\66\34\2\u016c\u016a\3\2\2\2\u016d\u0170\3\2\2\2\u016e\u016c\3\2\2\2" + + "\u016e\u016f\3\2\2\2\u016f\u0172\3\2\2\2\u0170\u016e\3\2\2\2\u0171\u0169" + + "\3\2\2\2\u0171\u0172\3\2\2\2\u0172\u0173\3\2\2\2\u0173\u0176\7\4\2\2\u0174" + + "\u0176\5\66\34\2\u0175\u0168\3\2\2\2\u0175\u0174\3\2\2\2\u0176\33\3\2" + + "\2\2\u0177\u0178\5j\66\2\u0178\u0179\7\f\2\2\u0179\u017a\7\3\2\2\u017a" + + "\u017b\5\n\6\2\u017b\u017c\7\4\2\2\u017c\35\3\2\2\2\u017d\u017e\7_\2\2" + + "\u017e\u017f\7\u0082\2\2\u017f\37\3\2\2\2\u0180\u0181\t\13\2\2\u0181!" + + "\3\2\2\2\u0182\u0187\5$\23\2\u0183\u0184\7\5\2\2\u0184\u0186\5$\23\2\u0185" + + "\u0183\3\2\2\2\u0186\u0189\3\2\2\2\u0187\u0185\3\2\2\2\u0187\u0188\3\2" + + "\2\2\u0188#\3\2\2\2\u0189\u0187\3\2\2\2\u018a\u018f\5\66\34\2\u018b\u018d" + + "\7\f\2\2\u018c\u018b\3\2\2\2\u018c\u018d\3\2\2\2\u018d\u018e\3\2\2\2\u018e" + + "\u0190\5j\66\2\u018f\u018c\3\2\2\2\u018f\u0190\3\2\2\2\u0190%\3\2\2\2" + + "\u0191\u0195\5.\30\2\u0192\u0194\5(\25\2\u0193\u0192\3\2\2\2\u0194\u0197" + + "\3\2\2\2\u0195\u0193\3\2\2\2\u0195\u0196\3\2\2\2\u0196\'\3\2\2\2\u0197" + + "\u0195\3\2\2\2\u0198\u0199\5*\26\2\u0199\u019a\78\2\2\u019a\u019c\5.\30" + + "\2\u019b\u019d\5,\27\2\u019c\u019b\3\2\2\2\u019c\u019d\3\2\2\2\u019d\u01a4" + + "\3\2\2\2\u019e\u019f\7C\2\2\u019f\u01a0\5*\26\2\u01a0\u01a1\78\2\2\u01a1" + + "\u01a2\5.\30\2\u01a2\u01a4\3\2\2\2\u01a3\u0198\3\2\2\2\u01a3\u019e\3\2" + + "\2\2\u01a4)\3\2\2\2\u01a5\u01a7\7\65\2\2\u01a6\u01a5\3\2\2\2\u01a6\u01a7" + + "\3\2\2\2\u01a7\u01b5\3\2\2\2\u01a8\u01aa\7:\2\2\u01a9\u01ab\7K\2\2\u01aa" + + "\u01a9\3\2\2\2\u01aa\u01ab\3\2\2\2\u01ab\u01b5\3\2\2\2\u01ac\u01ae\7P" + + "\2\2\u01ad\u01af\7K\2\2\u01ae\u01ad\3\2\2\2\u01ae\u01af\3\2\2\2\u01af" + + "\u01b5\3\2\2\2\u01b0\u01b2\7,\2\2\u01b1\u01b3\7K\2\2\u01b2\u01b1\3\2\2" + + "\2\u01b2\u01b3\3\2\2\2\u01b3\u01b5\3\2\2\2\u01b4\u01a6\3\2\2\2\u01b4\u01a8" + + "\3\2\2\2\u01b4\u01ac\3\2\2\2\u01b4\u01b0\3\2\2\2\u01b5+\3\2\2\2\u01b6" + + "\u01b7\7G\2\2\u01b7\u01c5\58\35\2\u01b8\u01b9\7b\2\2\u01b9\u01ba\7\3\2" + + "\2\u01ba\u01bf\5j\66\2\u01bb\u01bc\7\5\2\2\u01bc\u01be\5j\66\2\u01bd\u01bb" + + "\3\2\2\2\u01be\u01c1\3\2\2\2\u01bf\u01bd\3\2\2\2\u01bf\u01c0\3\2\2\2\u01c0" + + "\u01c2\3\2\2\2\u01c1\u01bf\3\2\2\2\u01c2\u01c3\7\4\2\2\u01c3\u01c5\3\2" + + "\2\2\u01c4\u01b6\3\2\2\2\u01c4\u01b8\3\2\2\2\u01c5-\3\2\2\2\u01c6\u01c8" + + "\7+\2\2\u01c7\u01c6\3\2\2\2\u01c7\u01c8\3\2\2\2\u01c8\u01c9\3\2\2\2\u01c9" + + "\u01ce\5l\67\2\u01ca\u01cc\7\f\2\2\u01cb\u01ca\3\2\2\2\u01cb\u01cc\3\2" + + "\2\2\u01cc\u01cd\3\2\2\2\u01cd\u01cf\5h\65\2\u01ce\u01cb\3\2\2\2\u01ce" + + "\u01cf\3\2\2\2\u01cf\u01e3\3\2\2\2\u01d0\u01d1\7\3\2\2\u01d1\u01d2\5\n" + + "\6\2\u01d2\u01d7\7\4\2\2\u01d3\u01d5\7\f\2\2\u01d4\u01d3\3\2\2\2\u01d4" + + "\u01d5\3\2\2\2\u01d5\u01d6\3\2\2\2\u01d6\u01d8\5h\65\2\u01d7\u01d4\3\2" + + "\2\2\u01d7\u01d8\3\2\2\2\u01d8\u01e3\3\2\2\2\u01d9\u01da\7\3\2\2\u01da" + + "\u01db\5&\24\2\u01db\u01e0\7\4\2\2\u01dc\u01de\7\f\2\2\u01dd\u01dc\3\2" + + "\2\2\u01dd\u01de\3\2\2\2\u01de\u01df\3\2\2\2\u01df\u01e1\5h\65\2\u01e0" + + "\u01dd\3\2\2\2\u01e0\u01e1\3\2\2\2\u01e1\u01e3\3\2\2\2\u01e2\u01c7\3\2" + + "\2\2\u01e2\u01d0\3\2\2\2\u01e2\u01d9\3\2\2\2\u01e3/\3\2\2\2\u01e4\u01e5" + + "\7N\2\2\u01e5\u01e6\7\3\2\2\u01e6\u01e7\5\62\32\2\u01e7\u01e8\7(\2\2\u01e8" + + "\u01e9\5h\65\2\u01e9\u01ea\7\63\2\2\u01ea\u01eb\7\3\2\2\u01eb\u01ec\5" + + "\62\32\2\u01ec\u01ed\7\4\2\2\u01ed\u01ee\7\4\2\2\u01ee\61\3\2\2\2\u01ef" + + "\u01f4\5\64\33\2\u01f0\u01f1\7\5\2\2\u01f1\u01f3\5\64\33\2\u01f2\u01f0" + + "\3\2\2\2\u01f3\u01f6\3\2\2\2\u01f4\u01f2\3\2\2\2\u01f4\u01f5\3\2\2\2\u01f5" + + "\63\3\2\2\2\u01f6\u01f4\3\2\2\2\u01f7\u01fc\5F$\2\u01f8\u01fa\7\f\2\2" + + "\u01f9\u01f8\3\2\2\2\u01f9\u01fa\3\2\2\2\u01fa\u01fb\3\2\2\2\u01fb\u01fd" + + "\5j\66\2\u01fc\u01f9\3\2\2\2\u01fc\u01fd\3\2\2\2\u01fd\65\3\2\2\2\u01fe" + + "\u01ff\58\35\2\u01ff\67\3\2\2\2\u0200\u0201\b\35\1\2\u0201\u0202\7D\2" + + "\2\u0202\u0220\58\35\n\u0203\u0204\7#\2\2\u0204\u0205\7\3\2\2\u0205\u0206" + + "\5\b\5\2\u0206\u0207\7\4\2\2\u0207\u0220\3\2\2\2\u0208\u0209\7R\2\2\u0209" + + "\u020a\7\3\2\2\u020a\u020b\5t;\2\u020b\u020c\5:\36\2\u020c\u020d\7\4\2" + + "\2\u020d\u0220\3\2\2\2\u020e\u020f\7>\2\2\u020f\u0210\7\3\2\2\u0210\u0211" + + "\5h\65\2\u0211\u0212\7\5\2\2\u0212\u0213\5t;\2\u0213\u0214\5:\36\2\u0214" + + "\u0215\7\4\2\2\u0215\u0220\3\2\2\2\u0216\u0217\7>\2\2\u0217\u0218\7\3" + + "\2\2\u0218\u0219\5t;\2\u0219\u021a\7\5\2\2\u021a\u021b\5t;\2\u021b\u021c" + + "\5:\36\2\u021c\u021d\7\4\2\2\u021d\u0220\3\2\2\2\u021e\u0220\5<\37\2\u021f" + + "\u0200\3\2\2\2\u021f\u0203\3\2\2\2\u021f\u0208\3\2\2\2\u021f\u020e\3\2" + + "\2\2\u021f\u0216\3\2\2\2\u021f\u021e\3\2\2\2\u0220\u0229\3\2\2\2\u0221" + + "\u0222\f\4\2\2\u0222\u0223\7\n\2\2\u0223\u0228\58\35\5\u0224\u0225\f\3" + + "\2\2\u0225\u0226\7I\2\2\u0226\u0228\58\35\4\u0227\u0221\3\2\2\2\u0227" + + "\u0224\3\2\2\2\u0228\u022b\3\2\2\2\u0229\u0227\3\2\2\2\u0229\u022a\3\2" + + "\2\2\u022a9\3\2\2\2\u022b\u0229\3\2\2\2\u022c\u022d\7\5\2\2\u022d\u022f" + + "\5t;\2\u022e\u022c\3\2\2\2\u022f\u0232\3\2\2\2\u0230\u022e\3\2\2\2\u0230" + + "\u0231\3\2\2\2\u0231;\3\2\2\2\u0232\u0230\3\2\2\2\u0233\u0235\5F$\2\u0234" + + "\u0236\5> \2\u0235\u0234\3\2\2\2\u0235\u0236\3\2\2\2\u0236=\3\2\2\2\u0237" + + "\u0239\7D\2\2\u0238\u0237\3\2\2\2\u0238\u0239\3\2\2\2\u0239\u023a\3\2" + + "\2\2\u023a\u023b\7\16\2\2\u023b\u023c\5F$\2\u023c\u023d\7\n\2\2\u023d" + + "\u023e\5F$\2\u023e\u0266\3\2\2\2\u023f\u0241\7D\2\2\u0240\u023f\3\2\2" + + "\2\u0240\u0241\3\2\2\2\u0241\u0242\3\2\2\2\u0242\u0243\7\63\2\2\u0243" + + "\u0244\7\3\2\2\u0244\u0249\5F$\2\u0245\u0246\7\5\2\2\u0246\u0248\5F$\2" + + "\u0247\u0245\3\2\2\2\u0248\u024b\3\2\2\2\u0249\u0247\3\2\2\2\u0249\u024a" + + "\3\2\2\2\u024a\u024c\3\2\2\2\u024b\u0249\3\2\2\2\u024c\u024d\7\4\2\2\u024d" + + "\u0266\3\2\2\2\u024e\u0250\7D\2\2\u024f\u024e\3\2\2\2\u024f\u0250\3\2" + + "\2\2\u0250\u0251\3\2\2\2\u0251\u0252\7\63\2\2\u0252\u0253\7\3\2\2\u0253" + + "\u0254\5\b\5\2\u0254\u0255\7\4\2\2\u0255\u0266\3\2\2\2\u0256\u0258\7D" + + "\2\2\u0257\u0256\3\2\2\2\u0257\u0258\3\2\2\2\u0258\u0259\3\2\2\2\u0259" + + "\u025a\7;\2\2\u025a\u0266\5B\"\2\u025b\u025d\7D\2\2\u025c\u025b\3\2\2" + + "\2\u025c\u025d\3\2\2\2\u025d\u025e\3\2\2\2\u025e\u025f\7Q\2\2\u025f\u0266" + + "\5t;\2\u0260\u0262\7\67\2\2\u0261\u0263\7D\2\2\u0262\u0261\3\2\2\2\u0262" + + "\u0263\3\2\2\2\u0263\u0264\3\2\2\2\u0264\u0266\7E\2\2\u0265\u0238\3\2" + + "\2\2\u0265\u0240\3\2\2\2\u0265\u024f\3\2\2\2\u0265\u0257\3\2\2\2\u0265" + + "\u025c\3\2\2\2\u0265\u0260\3\2\2\2\u0266?\3\2\2\2\u0267\u0268\7;\2\2\u0268" + + "\u0269\5B\"\2\u0269A\3\2\2\2\u026a\u026c\5t;\2\u026b\u026d\5D#\2\u026c" + + "\u026b\3\2\2\2\u026c\u026d\3\2\2\2\u026dC\3\2\2\2\u026e\u026f\7!\2\2\u026f" + + "\u0275\5t;\2\u0270\u0271\7i\2\2\u0271\u0272\5t;\2\u0272\u0273\7q\2\2\u0273" + + "\u0275\3\2\2\2\u0274\u026e\3\2\2\2\u0274\u0270\3\2\2\2\u0275E\3\2\2\2" + + "\u0276\u0277\b$\1\2\u0277\u027b\5H%\2\u0278\u0279\t\7\2\2\u0279\u027b" + + "\5F$\6\u027a\u0276\3\2\2\2\u027a\u0278\3\2\2\2\u027b\u0288\3\2\2\2\u027c" + + "\u027d\f\5\2\2\u027d\u027e\t\f\2\2\u027e\u0287\5F$\6\u027f\u0280\f\4\2" + + "\2\u0280\u0281\t\7\2\2\u0281\u0287\5F$\5\u0282\u0283\f\3\2\2\u0283\u0284" + + "\5^\60\2\u0284\u0285\5F$\4\u0285\u0287\3\2\2\2\u0286\u027c\3\2\2\2\u0286" + + "\u027f\3\2\2\2\u0286\u0282\3\2\2\2\u0287\u028a\3\2\2\2\u0288\u0286\3\2" + + "\2\2\u0288\u0289\3\2\2\2\u0289G\3\2\2\2\u028a\u0288\3\2\2\2\u028b\u028c" + + "\b%\1\2\u028c\u02b0\5L\'\2\u028d\u02b0\5R*\2\u028e\u02b0\5J&\2\u028f\u02b0" + + "\5\\/\2\u0290\u0291\5h\65\2\u0291\u0292\7\177\2\2\u0292\u0294\3\2\2\2" + + "\u0293\u0290\3\2\2\2\u0293\u0294\3\2\2\2\u0294\u0295\3\2\2\2\u0295\u02b0" + + "\7{\2\2\u0296\u02b0\5V,\2\u0297\u0298\7\3\2\2\u0298\u0299\5\b\5\2\u0299" + + "\u029a\7\4\2\2\u029a\u02b0\3\2\2\2\u029b\u02b0\5h\65\2\u029c\u029d\7\3" + + "\2\2\u029d\u029e\5\66\34\2\u029e\u029f\7\4\2\2\u029f\u02b0\3\2\2\2\u02a0" + + "\u02a2\7\20\2\2\u02a1\u02a3\58\35\2\u02a2\u02a1\3\2\2\2\u02a2\u02a3\3" + + "\2\2\2\u02a3\u02a5\3\2\2\2\u02a4\u02a6\5v<\2\u02a5\u02a4\3\2\2\2\u02a6" + + "\u02a7\3\2\2\2\u02a7\u02a5\3\2\2\2\u02a7\u02a8\3\2\2\2\u02a8\u02ab\3\2" + + "\2\2\u02a9\u02aa\7\37\2\2\u02aa\u02ac\58\35\2\u02ab\u02a9\3\2\2\2\u02ab" + + "\u02ac\3\2\2\2\u02ac\u02ad\3\2\2\2\u02ad\u02ae\7 \2\2\u02ae\u02b0\3\2" + + "\2\2\u02af\u028b\3\2\2\2\u02af\u028d\3\2\2\2\u02af\u028e\3\2\2\2\u02af" + + "\u028f\3\2\2\2\u02af\u0293\3\2\2\2\u02af\u0296\3\2\2\2\u02af\u0297\3\2" + + "\2\2\u02af\u029b\3\2\2\2\u02af\u029c\3\2\2\2\u02af\u02a0\3\2\2\2\u02b0" + + "\u02b6\3\2\2\2\u02b1\u02b2\f\f\2\2\u02b2\u02b3\7~\2\2\u02b3\u02b5\5f\64" + + "\2\u02b4\u02b1\3\2\2\2\u02b5\u02b8\3\2\2\2\u02b6\u02b4\3\2\2\2\u02b6\u02b7" + + "\3\2\2\2\u02b7I\3\2\2\2\u02b8\u02b6\3\2\2\2\u02b9\u02bd\7\30\2\2\u02ba" + + "\u02bd\7\26\2\2\u02bb\u02bd\7\27\2\2\u02bc\u02b9\3\2\2\2\u02bc\u02ba\3" + + "\2\2\2\u02bc\u02bb\3\2\2\2\u02bdK\3\2\2\2\u02be\u02c9\5N(\2\u02bf\u02c0" + + "\7j\2\2\u02c0\u02c1\5N(\2\u02c1\u02c2\7q\2\2\u02c2\u02c9\3\2\2\2\u02c3" + + "\u02c9\5P)\2\u02c4\u02c5\7j\2\2\u02c5\u02c6\5P)\2\u02c6\u02c7\7q\2\2\u02c7" + + "\u02c9\3\2\2\2\u02c8\u02be\3\2\2\2\u02c8\u02bf\3\2\2\2\u02c8\u02c3\3\2" + + "\2\2\u02c8\u02c4\3\2\2\2\u02c9M\3\2\2\2\u02ca\u02cb\7\21\2\2\u02cb\u02cc" + + "\7\3\2\2\u02cc\u02cd\5\66\34\2\u02cd\u02ce\7\f\2\2\u02ce\u02cf\5f\64\2" + + "\u02cf\u02d0\7\4\2\2\u02d0O\3\2\2\2\u02d1\u02d2\7\25\2\2\u02d2\u02d3\7" + + "\3\2\2\u02d3\u02d4\5\66\34\2\u02d4\u02d5\7\5\2\2\u02d5\u02d6\5f\64\2\u02d6" + + "\u02d7\7\4\2\2\u02d7Q\3\2\2\2\u02d8\u02de\5T+\2\u02d9\u02da\7j\2\2\u02da" + + "\u02db\5T+\2\u02db\u02dc\7q\2\2\u02dc\u02de\3\2\2\2\u02dd\u02d8\3\2\2" + + "\2\u02dd\u02d9\3\2\2\2\u02deS\3\2\2\2\u02df\u02e0\7%\2\2\u02e0\u02e1\7" + + "\3\2\2\u02e1\u02e2\5j\66\2\u02e2\u02e3\7*\2\2\u02e3\u02e4\5F$\2\u02e4" + + "\u02e5\7\4\2\2\u02e5U\3\2\2\2\u02e6\u02ec\5X-\2\u02e7\u02e8\7j\2\2\u02e8" + + "\u02e9\5X-\2\u02e9\u02ea\7q\2\2\u02ea\u02ec\3\2\2\2\u02eb\u02e6\3\2\2" + + "\2\u02eb\u02e7\3\2\2\2\u02ecW\3\2\2\2\u02ed\u02ee\5Z.\2\u02ee\u02fa\7" + + "\3\2\2\u02ef\u02f1\5 \21\2\u02f0\u02ef\3\2\2\2\u02f0\u02f1\3\2\2\2\u02f1" + + "\u02f2\3\2\2\2\u02f2\u02f7\5\66\34\2\u02f3\u02f4\7\5\2\2\u02f4\u02f6\5" + + "\66\34\2\u02f5\u02f3\3\2\2\2\u02f6\u02f9\3\2\2\2\u02f7\u02f5\3\2\2\2\u02f7" + + "\u02f8\3\2\2\2\u02f8\u02fb\3\2\2\2\u02f9\u02f7\3\2\2\2\u02fa\u02f0\3\2" + + "\2\2\u02fa\u02fb\3\2\2\2\u02fb\u02fc\3\2\2\2\u02fc\u02fd\7\4\2\2\u02fd" + + "Y\3\2\2\2\u02fe\u0302\7:\2\2\u02ff\u0302\7P\2\2\u0300\u0302\5j\66\2\u0301" + + "\u02fe\3\2\2\2\u0301\u02ff\3\2\2\2\u0301\u0300\3\2\2\2\u0302[\3\2\2\2" + + "\u0303\u031e\7E\2\2\u0304\u031e\5b\62\2\u0305\u031e\5r:\2\u0306\u031e" + + "\5`\61\2\u0307\u0309\7\u0081\2\2\u0308\u0307\3\2\2\2\u0309\u030a\3\2\2" + + "\2\u030a\u0308\3\2\2\2\u030a\u030b\3\2\2\2\u030b\u031e\3\2\2\2\u030c\u031e" + + "\7\u0080\2\2\u030d\u030e\7l\2\2\u030e\u030f\5t;\2\u030f\u0310\7q\2\2\u0310" + + "\u031e\3\2\2\2\u0311\u0312\7m\2\2\u0312\u0313\5t;\2\u0313\u0314\7q\2\2" + + "\u0314\u031e\3\2\2\2\u0315\u0316\7n\2\2\u0316\u0317\5t;\2\u0317\u0318" + + "\7q\2\2\u0318\u031e\3\2\2\2\u0319\u031a\7o\2\2\u031a\u031b\5t;\2\u031b" + + "\u031c\7q\2\2\u031c\u031e\3\2\2\2\u031d\u0303\3\2\2\2\u031d\u0304\3\2" + + "\2\2\u031d\u0305\3\2\2\2\u031d\u0306\3\2\2\2\u031d\u0308\3\2\2\2\u031d" + + "\u030c\3\2\2\2\u031d\u030d\3\2\2\2\u031d\u0311\3\2\2\2\u031d\u0315\3\2" + + "\2\2\u031d\u0319\3\2\2\2\u031e]\3\2\2\2\u031f\u0320\t\r\2\2\u0320_\3\2" + + "\2\2\u0321\u0322\t\16\2\2\u0322a\3\2\2\2\u0323\u0325\7\66\2\2\u0324\u0326" + + "\t\7\2\2\u0325\u0324\3\2\2\2\u0325\u0326\3\2\2\2\u0326\u0329\3\2\2\2\u0327" + + "\u032a\5r:\2\u0328\u032a\5t;\2\u0329\u0327\3\2\2\2\u0329\u0328\3\2\2\2" + + "\u032a\u032b\3\2\2\2\u032b\u032e\5d\63\2\u032c\u032d\7^\2\2\u032d\u032f" + + "\5d\63\2\u032e\u032c\3\2\2\2\u032e\u032f\3\2\2\2\u032fc\3\2\2\2\u0330" + + "\u0331\t\17\2\2\u0331e\3\2\2\2\u0332\u0333\5j\66\2\u0333g\3\2\2\2\u0334" + + "\u0335\5j\66\2\u0335\u0336\7\177\2\2\u0336\u0338\3\2\2\2\u0337\u0334\3" + + "\2\2\2\u0338\u033b\3\2\2\2\u0339\u0337\3\2\2\2\u0339\u033a\3\2\2\2\u033a" + + "\u033c\3\2\2\2\u033b\u0339\3\2\2\2\u033c\u033d\5j\66\2\u033di\3\2\2\2" + + "\u033e\u0341\5n8\2\u033f\u0341\5p9\2\u0340\u033e\3\2\2\2\u0340\u033f\3" + + "\2\2\2\u0341k\3\2\2\2\u0342\u0343\5j\66\2\u0343\u0344\7\6\2\2\u0344\u0346" + + "\3\2\2\2\u0345\u0342\3\2\2\2\u0345\u0346\3\2\2\2\u0346\u0347\3\2\2\2\u0347" + + "\u034f\7\u0086\2\2\u0348\u0349\5j\66\2\u0349\u034a\7\6\2\2\u034a\u034c" + + "\3\2\2\2\u034b\u0348\3\2\2\2\u034b\u034c\3\2\2\2\u034c\u034d\3\2\2\2\u034d" + + "\u034f\5j\66\2\u034e\u0345\3\2\2\2\u034e\u034b\3\2\2\2\u034fm\3\2\2\2" + + "\u0350\u0353\7\u0087\2\2\u0351\u0353\7\u0088\2\2\u0352\u0350\3\2\2\2\u0352" + + "\u0351\3\2\2\2\u0353o\3\2\2\2\u0354\u0358\7\u0084\2\2\u0355\u0358\5x=" + + "\2\u0356\u0358\7\u0085\2\2\u0357\u0354\3\2\2\2\u0357\u0355\3\2\2\2\u0357" + + "\u0356\3\2\2\2\u0358q\3\2\2\2\u0359\u035c\7\u0083\2\2\u035a\u035c\7\u0082" + + "\2\2\u035b\u0359\3\2\2\2\u035b\u035a\3\2\2\2\u035cs\3\2\2\2\u035d\u035e" + + "\t\20\2\2\u035eu\3\2\2\2\u035f\u0360\7d\2\2\u0360\u0361\5\66\34\2\u0361" + + "\u0362\7\\\2\2\u0362\u0363\5\66\34\2\u0363w\3\2\2\2\u0364\u0365\t\21\2" + + "\2\u0365y\3\2\2\2x\u0089\u008b\u008f\u0098\u009a\u009e\u00a6\u00a8\u00ac" + + "\u00b0\u00b6\u00ba\u00bf\u00c4\u00c8\u00cc\u00d1\u00db\u00df\u00e7\u00ea" + + "\u00f0\u00f5\u00f8\u00fd\u0100\u0102\u010a\u010d\u0119\u011c\u011f\u0126" + + "\u012d\u0131\u0135\u0139\u013c\u0140\u0144\u0149\u014d\u0155\u0159\u015c" + + "\u0163\u016e\u0171\u0175\u0187\u018c\u018f\u0195\u019c\u01a3\u01a6\u01aa" + + "\u01ae\u01b2\u01b4\u01bf\u01c4\u01c7\u01cb\u01ce\u01d4\u01d7\u01dd\u01e0" + + "\u01e2\u01f4\u01f9\u01fc\u021f\u0227\u0229\u0230\u0235\u0238\u0240\u0249" + + "\u024f\u0257\u025c\u0262\u0265\u026c\u0274\u027a\u0286\u0288\u0293\u02a2" + + "\u02a7\u02ab\u02af\u02b6\u02bc\u02c8\u02dd\u02eb\u02f0\u02f7\u02fa\u0301" + + "\u030a\u031d\u0325\u0329\u032e\u0339\u0340\u0345\u034b\u034e\u0352\u0357" + + "\u035b"; + public static final ATN _ATN = new ATNDeserializer().deserialize(_serializedATN.toCharArray()); + static { + _decisionToDFA = new DFA[_ATN.getNumberOfDecisions()]; + for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) { + _decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i); + } } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SqlBaseVisitor ) return ((SqlBaseVisitor)visitor).visitNonReserved(this); - else return visitor.visitChildren(this); - } - } - - public final NonReservedContext nonReserved() throws RecognitionException { - NonReservedContext _localctx = new NonReservedContext(_ctx, getState()); - enterRule(_localctx, 118, RULE_nonReserved); - int _la; - try { - enterOuterAlt(_localctx, 1); - { - setState(866); - _la = _input.LA(1); - if ( !((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << ANALYZE) | (1L << ANALYZED) | (1L << CATALOGS) | (1L << COLUMNS) | (1L << CURRENT_DATE) | (1L << CURRENT_TIME) | (1L << CURRENT_TIMESTAMP) | (1L << DAY) | (1L << DEBUG) | (1L << EXECUTABLE) | (1L << EXPLAIN) | (1L << FIRST) | (1L << FORMAT) | (1L << FULL) | (1L << FUNCTIONS) | (1L << GRAPHVIZ) | (1L << HOUR) | (1L << INTERVAL) | (1L << LAST) | (1L << LIMIT) | (1L << MAPPED) | (1L << MINUTE) | (1L << MONTH))) != 0) || ((((_la - 70)) & ~0x3f) == 0 && ((1L << (_la - 70)) & ((1L << (OPTIMIZED - 70)) | (1L << (PARSED - 70)) | (1L << (PHYSICAL - 70)) | (1L << (PIVOT - 70)) | (1L << (PLAN - 70)) | (1L << (RLIKE - 70)) | (1L << (QUERY - 70)) | (1L << (SCHEMAS - 70)) | (1L << (SECOND - 70)) | (1L << (SHOW - 70)) | (1L << (SYS - 70)) | (1L << (TABLES - 70)) | (1L << (TEXT - 70)) | (1L << (TOP - 70)) | (1L << (TYPE - 70)) | (1L << (TYPES - 70)) | (1L << (VERIFY - 70)) | (1L << (YEAR - 70)))) != 0)) ) { - _errHandler.recoverInline(this); - } - else { - if ( _input.LA(1)==Token.EOF ) matchedEOF = true; - _errHandler.reportMatch(this); - consume(); - } - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public boolean sempred(RuleContext _localctx, int ruleIndex, int predIndex) { - switch (ruleIndex) { - case 27: - return booleanExpression_sempred((BooleanExpressionContext)_localctx, predIndex); - case 34: - return valueExpression_sempred((ValueExpressionContext)_localctx, predIndex); - case 35: - return primaryExpression_sempred((PrimaryExpressionContext)_localctx, predIndex); - } - return true; - } - private boolean booleanExpression_sempred(BooleanExpressionContext _localctx, int predIndex) { - switch (predIndex) { - case 0: - return precpred(_ctx, 2); - case 1: - return precpred(_ctx, 1); - } - return true; - } - private boolean valueExpression_sempred(ValueExpressionContext _localctx, int predIndex) { - switch (predIndex) { - case 2: - return precpred(_ctx, 3); - case 3: - return precpred(_ctx, 2); - case 4: - return precpred(_ctx, 1); - } - return true; - } - private boolean primaryExpression_sempred(PrimaryExpressionContext _localctx, int predIndex) { - switch (predIndex) { - case 5: - return precpred(_ctx, 10); - } - return true; - } - - public static final String _serializedATN = - "\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\3\u008d\u0367\4\2\t"+ - "\2\4\3\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b\t\b\4\t\t\t\4\n\t\n\4\13"+ - "\t\13\4\f\t\f\4\r\t\r\4\16\t\16\4\17\t\17\4\20\t\20\4\21\t\21\4\22\t\22"+ - "\4\23\t\23\4\24\t\24\4\25\t\25\4\26\t\26\4\27\t\27\4\30\t\30\4\31\t\31"+ - "\4\32\t\32\4\33\t\33\4\34\t\34\4\35\t\35\4\36\t\36\4\37\t\37\4 \t \4!"+ - "\t!\4\"\t\"\4#\t#\4$\t$\4%\t%\4&\t&\4\'\t\'\4(\t(\4)\t)\4*\t*\4+\t+\4"+ - ",\t,\4-\t-\4.\t.\4/\t/\4\60\t\60\4\61\t\61\4\62\t\62\4\63\t\63\4\64\t"+ - "\64\4\65\t\65\4\66\t\66\4\67\t\67\48\t8\49\t9\4:\t:\4;\t;\4<\t<\4=\t="+ - "\3\2\3\2\3\2\3\3\3\3\3\3\3\4\3\4\3\4\3\4\3\4\3\4\3\4\3\4\3\4\7\4\u008a"+ - "\n\4\f\4\16\4\u008d\13\4\3\4\5\4\u0090\n\4\3\4\3\4\3\4\3\4\3\4\3\4\3\4"+ - "\7\4\u0099\n\4\f\4\16\4\u009c\13\4\3\4\5\4\u009f\n\4\3\4\3\4\3\4\3\4\3"+ - "\4\3\4\5\4\u00a7\n\4\5\4\u00a9\n\4\3\4\3\4\5\4\u00ad\n\4\3\4\3\4\5\4\u00b1"+ - "\n\4\3\4\3\4\3\4\3\4\5\4\u00b7\n\4\3\4\3\4\5\4\u00bb\n\4\3\4\3\4\3\4\5"+ - "\4\u00c0\n\4\3\4\3\4\3\4\5\4\u00c5\n\4\3\4\3\4\5\4\u00c9\n\4\3\4\3\4\5"+ - "\4\u00cd\n\4\3\4\3\4\3\4\5\4\u00d2\n\4\3\4\3\4\3\4\3\4\3\4\3\4\3\4\3\4"+ - "\5\4\u00dc\n\4\3\4\3\4\5\4\u00e0\n\4\3\4\3\4\3\4\3\4\7\4\u00e6\n\4\f\4"+ - "\16\4\u00e9\13\4\5\4\u00eb\n\4\3\4\3\4\3\4\3\4\5\4\u00f1\n\4\3\4\3\4\3"+ - "\4\5\4\u00f6\n\4\3\4\5\4\u00f9\n\4\3\4\3\4\3\4\5\4\u00fe\n\4\3\4\5\4\u0101"+ - "\n\4\5\4\u0103\n\4\3\5\3\5\3\5\3\5\7\5\u0109\n\5\f\5\16\5\u010c\13\5\5"+ - "\5\u010e\n\5\3\5\3\5\3\6\3\6\3\6\3\6\3\6\3\6\7\6\u0118\n\6\f\6\16\6\u011b"+ - "\13\6\5\6\u011d\n\6\3\6\5\6\u0120\n\6\3\7\3\7\3\7\3\7\3\7\5\7\u0127\n"+ - "\7\3\b\3\b\3\b\3\b\3\b\5\b\u012e\n\b\3\t\3\t\5\t\u0132\n\t\3\t\3\t\5\t"+ - "\u0136\n\t\3\n\3\n\5\n\u013a\n\n\3\n\5\n\u013d\n\n\3\n\3\n\5\n\u0141\n"+ - "\n\3\n\3\n\5\n\u0145\n\n\3\n\3\n\3\n\5\n\u014a\n\n\3\n\3\n\5\n\u014e\n"+ - "\n\3\13\3\13\3\13\3\13\7\13\u0154\n\13\f\13\16\13\u0157\13\13\3\13\5\13"+ - "\u015a\n\13\3\f\5\f\u015d\n\f\3\f\3\f\3\f\7\f\u0162\n\f\f\f\16\f\u0165"+ - "\13\f\3\r\3\r\3\16\3\16\3\16\3\16\7\16\u016d\n\16\f\16\16\16\u0170\13"+ - "\16\5\16\u0172\n\16\3\16\3\16\5\16\u0176\n\16\3\17\3\17\3\17\3\17\3\17"+ - "\3\17\3\20\3\20\3\20\3\21\3\21\3\22\3\22\3\22\7\22\u0186\n\22\f\22\16"+ - "\22\u0189\13\22\3\23\3\23\5\23\u018d\n\23\3\23\5\23\u0190\n\23\3\24\3"+ - "\24\7\24\u0194\n\24\f\24\16\24\u0197\13\24\3\25\3\25\3\25\3\25\5\25\u019d"+ - "\n\25\3\25\3\25\3\25\3\25\3\25\5\25\u01a4\n\25\3\26\5\26\u01a7\n\26\3"+ - "\26\3\26\5\26\u01ab\n\26\3\26\3\26\5\26\u01af\n\26\3\26\3\26\5\26\u01b3"+ - "\n\26\5\26\u01b5\n\26\3\27\3\27\3\27\3\27\3\27\3\27\3\27\7\27\u01be\n"+ - "\27\f\27\16\27\u01c1\13\27\3\27\3\27\5\27\u01c5\n\27\3\30\5\30\u01c8\n"+ - "\30\3\30\3\30\5\30\u01cc\n\30\3\30\5\30\u01cf\n\30\3\30\3\30\3\30\3\30"+ - "\5\30\u01d5\n\30\3\30\5\30\u01d8\n\30\3\30\3\30\3\30\3\30\5\30\u01de\n"+ - "\30\3\30\5\30\u01e1\n\30\5\30\u01e3\n\30\3\31\3\31\3\31\3\31\3\31\3\31"+ - "\3\31\3\31\3\31\3\31\3\31\3\32\3\32\3\32\7\32\u01f3\n\32\f\32\16\32\u01f6"+ - "\13\32\3\33\3\33\5\33\u01fa\n\33\3\33\5\33\u01fd\n\33\3\34\3\34\3\35\3"+ - "\35\3\35\3\35\3\35\3\35\3\35\3\35\3\35\3\35\3\35\3\35\3\35\3\35\3\35\3"+ - "\35\3\35\3\35\3\35\3\35\3\35\3\35\3\35\3\35\3\35\3\35\3\35\3\35\3\35\3"+ - "\35\3\35\5\35\u0220\n\35\3\35\3\35\3\35\3\35\3\35\3\35\7\35\u0228\n\35"+ - "\f\35\16\35\u022b\13\35\3\36\3\36\7\36\u022f\n\36\f\36\16\36\u0232\13"+ - "\36\3\37\3\37\5\37\u0236\n\37\3 \5 \u0239\n \3 \3 \3 \3 \3 \3 \5 \u0241"+ - "\n \3 \3 \3 \3 \3 \7 \u0248\n \f \16 \u024b\13 \3 \3 \3 \5 \u0250\n \3"+ - " \3 \3 \3 \3 \3 \5 \u0258\n \3 \3 \3 \5 \u025d\n \3 \3 \3 \3 \5 \u0263"+ - "\n \3 \5 \u0266\n \3!\3!\3!\3\"\3\"\5\"\u026d\n\"\3#\3#\3#\3#\3#\3#\5"+ - "#\u0275\n#\3$\3$\3$\3$\5$\u027b\n$\3$\3$\3$\3$\3$\3$\3$\3$\3$\3$\7$\u0287"+ - "\n$\f$\16$\u028a\13$\3%\3%\3%\3%\3%\3%\3%\3%\5%\u0294\n%\3%\3%\3%\3%\3"+ - "%\3%\3%\3%\3%\3%\3%\3%\3%\5%\u02a3\n%\3%\6%\u02a6\n%\r%\16%\u02a7\3%\3"+ - "%\5%\u02ac\n%\3%\3%\5%\u02b0\n%\3%\3%\3%\7%\u02b5\n%\f%\16%\u02b8\13%"+ - "\3&\3&\3&\5&\u02bd\n&\3\'\3\'\3\'\3\'\3\'\3\'\3\'\3\'\3\'\3\'\5\'\u02c9"+ - "\n\'\3(\3(\3(\3(\3(\3(\3(\3)\3)\3)\3)\3)\3)\3)\3*\3*\3*\3*\3*\5*\u02de"+ - "\n*\3+\3+\3+\3+\3+\3+\3+\3,\3,\3,\3,\3,\5,\u02ec\n,\3-\3-\3-\5-\u02f1"+ - "\n-\3-\3-\3-\7-\u02f6\n-\f-\16-\u02f9\13-\5-\u02fb\n-\3-\3-\3.\3.\3.\5"+ - ".\u0302\n.\3/\3/\3/\3/\3/\6/\u0309\n/\r/\16/\u030a\3/\3/\3/\3/\3/\3/\3"+ - "/\3/\3/\3/\3/\3/\3/\3/\3/\3/\3/\5/\u031e\n/\3\60\3\60\3\61\3\61\3\62\3"+ - "\62\5\62\u0326\n\62\3\62\3\62\5\62\u032a\n\62\3\62\3\62\3\62\5\62\u032f"+ - "\n\62\3\63\3\63\3\64\3\64\3\65\3\65\3\65\7\65\u0338\n\65\f\65\16\65\u033b"+ - "\13\65\3\65\3\65\3\66\3\66\5\66\u0341\n\66\3\67\3\67\3\67\5\67\u0346\n"+ - "\67\3\67\3\67\3\67\3\67\5\67\u034c\n\67\3\67\5\67\u034f\n\67\38\38\58"+ - "\u0353\n8\39\39\39\59\u0358\n9\3:\3:\5:\u035c\n:\3;\3;\3<\3<\3<\3<\3<"+ - "\3=\3=\3=\2\58FH>\2\4\6\b\n\f\16\20\22\24\26\30\32\34\36 \"$&(*,.\60\62"+ - "\64\668:<>@BDFHJLNPRTVXZ\\^`bdfhjlnprtvx\2\22\b\2\7\7\t\t\"\"==HHLL\4"+ - "\2..[[\4\2\t\tHH\4\2**\63\63\3\2\34\35\3\2yz\4\2\7\7\u0082\u0082\4\2\r"+ - "\r\34\34\4\2\'\'99\4\2\7\7\36\36\3\2{}\3\2rx\4\2&&]]\7\2\31\32\61\62?"+ - "BTUgh\3\2\u0080\u0081\31\2\b\t\23\24\26\31\33\33\"\"$$\'\')),.\61\61\66"+ - "\6699<=??AAHHLOQTWXZ[_accgg\2\u03cf\2z\3\2\2\2\4}\3\2\2\2\6\u0102\3\2"+ - "\2\2\b\u010d\3\2\2\2\n\u0111\3\2\2\2\f\u0126\3\2\2\2\16\u012d\3\2\2\2"+ - "\20\u012f\3\2\2\2\22\u0137\3\2\2\2\24\u014f\3\2\2\2\26\u015c\3\2\2\2\30"+ - "\u0166\3\2\2\2\32\u0175\3\2\2\2\34\u0177\3\2\2\2\36\u017d\3\2\2\2 \u0180"+ - "\3\2\2\2\"\u0182\3\2\2\2$\u018a\3\2\2\2&\u0191\3\2\2\2(\u01a3\3\2\2\2"+ - "*\u01b4\3\2\2\2,\u01c4\3\2\2\2.\u01e2\3\2\2\2\60\u01e4\3\2\2\2\62\u01ef"+ - "\3\2\2\2\64\u01f7\3\2\2\2\66\u01fe\3\2\2\28\u021f\3\2\2\2:\u0230\3\2\2"+ - "\2<\u0233\3\2\2\2>\u0265\3\2\2\2@\u0267\3\2\2\2B\u026a\3\2\2\2D\u0274"+ - "\3\2\2\2F\u027a\3\2\2\2H\u02af\3\2\2\2J\u02bc\3\2\2\2L\u02c8\3\2\2\2N"+ - "\u02ca\3\2\2\2P\u02d1\3\2\2\2R\u02dd\3\2\2\2T\u02df\3\2\2\2V\u02eb\3\2"+ - "\2\2X\u02ed\3\2\2\2Z\u0301\3\2\2\2\\\u031d\3\2\2\2^\u031f\3\2\2\2`\u0321"+ - "\3\2\2\2b\u0323\3\2\2\2d\u0330\3\2\2\2f\u0332\3\2\2\2h\u0339\3\2\2\2j"+ - "\u0340\3\2\2\2l\u034e\3\2\2\2n\u0352\3\2\2\2p\u0357\3\2\2\2r\u035b\3\2"+ - "\2\2t\u035d\3\2\2\2v\u035f\3\2\2\2x\u0364\3\2\2\2z{\5\6\4\2{|\7\2\2\3"+ - "|\3\3\2\2\2}~\5\66\34\2~\177\7\2\2\3\177\5\3\2\2\2\u0080\u0103\5\b\5\2"+ - "\u0081\u008f\7$\2\2\u0082\u008b\7\3\2\2\u0083\u0084\7O\2\2\u0084\u008a"+ - "\t\2\2\2\u0085\u0086\7)\2\2\u0086\u008a\t\3\2\2\u0087\u0088\7c\2\2\u0088"+ - "\u008a\5`\61\2\u0089\u0083\3\2\2\2\u0089\u0085\3\2\2\2\u0089\u0087\3\2"+ - "\2\2\u008a\u008d\3\2\2\2\u008b\u0089\3\2\2\2\u008b\u008c\3\2\2\2\u008c"+ - "\u008e\3\2\2\2\u008d\u008b\3\2\2\2\u008e\u0090\7\4\2\2\u008f\u0082\3\2"+ - "\2\2\u008f\u0090\3\2\2\2\u0090\u0091\3\2\2\2\u0091\u0103\5\6\4\2\u0092"+ - "\u009e\7\33\2\2\u0093\u009a\7\3\2\2\u0094\u0095\7O\2\2\u0095\u0099\t\4"+ - "\2\2\u0096\u0097\7)\2\2\u0097\u0099\t\3\2\2\u0098\u0094\3\2\2\2\u0098"+ - "\u0096\3\2\2\2\u0099\u009c\3\2\2\2\u009a\u0098\3\2\2\2\u009a\u009b\3\2"+ - "\2\2\u009b\u009d\3\2\2\2\u009c\u009a\3\2\2\2\u009d\u009f\7\4\2\2\u009e"+ - "\u0093\3\2\2\2\u009e\u009f\3\2\2\2\u009f\u00a0\3\2\2\2\u00a0\u0103\5\6"+ - "\4\2\u00a1\u00a2\7W\2\2\u00a2\u00a8\7Z\2\2\u00a3\u00a6\7\22\2\2\u00a4"+ - "\u00a7\5@!\2\u00a5\u00a7\5t;\2\u00a6\u00a4\3\2\2\2\u00a6\u00a5\3\2\2\2"+ - "\u00a7\u00a9\3\2\2\2\u00a8\u00a3\3\2\2\2\u00a8\u00a9\3\2\2\2\u00a9\u00ac"+ - "\3\2\2\2\u00aa\u00ab\7\64\2\2\u00ab\u00ad\7+\2\2\u00ac\u00aa\3\2\2\2\u00ac"+ - "\u00ad\3\2\2\2\u00ad\u00b0\3\2\2\2\u00ae\u00b1\5@!\2\u00af\u00b1\5l\67"+ - "\2\u00b0\u00ae\3\2\2\2\u00b0\u00af\3\2\2\2\u00b0\u00b1\3\2\2\2\u00b1\u0103"+ - "\3\2\2\2\u00b2\u00b3\7W\2\2\u00b3\u00b6\7\24\2\2\u00b4\u00b5\7\22\2\2"+ - "\u00b5\u00b7\5t;\2\u00b6\u00b4\3\2\2\2\u00b6\u00b7\3\2\2\2\u00b7\u00ba"+ - "\3\2\2\2\u00b8\u00b9\7\64\2\2\u00b9\u00bb\7+\2\2\u00ba\u00b8\3\2\2\2\u00ba"+ - "\u00bb\3\2\2\2\u00bb\u00bc\3\2\2\2\u00bc\u00bf\t\5\2\2\u00bd\u00c0\5@"+ - "!\2\u00be\u00c0\5l\67\2\u00bf\u00bd\3\2\2\2\u00bf\u00be\3\2\2\2\u00c0"+ - "\u0103\3\2\2\2\u00c1\u00c4\t\6\2\2\u00c2\u00c3\7\22\2\2\u00c3\u00c5\5"+ - "t;\2\u00c4\u00c2\3\2\2\2\u00c4\u00c5\3\2\2\2\u00c5\u00c8\3\2\2\2\u00c6"+ - "\u00c7\7\64\2\2\u00c7\u00c9\7+\2\2\u00c8\u00c6\3\2\2\2\u00c8\u00c9\3\2"+ - "\2\2\u00c9\u00cc\3\2\2\2\u00ca\u00cd\5@!\2\u00cb\u00cd\5l\67\2\u00cc\u00ca"+ - "\3\2\2\2\u00cc\u00cb\3\2\2\2\u00cd\u0103\3\2\2\2\u00ce\u00cf\7W\2\2\u00cf"+ - "\u00d1\7-\2\2\u00d0\u00d2\5@!\2\u00d1\u00d0\3\2\2\2\u00d1\u00d2\3\2\2"+ - "\2\u00d2\u0103\3\2\2\2\u00d3\u00d4\7W\2\2\u00d4\u0103\7S\2\2\u00d5\u00d6"+ - "\7W\2\2\u00d6\u0103\7\23\2\2\u00d7\u00d8\7X\2\2\u00d8\u00db\7Z\2\2\u00d9"+ - "\u00da\7\22\2\2\u00da\u00dc\5@!\2\u00db\u00d9\3\2\2\2\u00db\u00dc\3\2"+ - "\2\2\u00dc\u00df\3\2\2\2\u00dd\u00e0\5@!\2\u00de\u00e0\5l\67\2\u00df\u00dd"+ - "\3\2\2\2\u00df\u00de\3\2\2\2\u00df\u00e0\3\2\2\2\u00e0\u00ea\3\2\2\2\u00e1"+ - "\u00e2\7`\2\2\u00e2\u00e7\5t;\2\u00e3\u00e4\7\5\2\2\u00e4\u00e6\5t;\2"+ - "\u00e5\u00e3\3\2\2\2\u00e6\u00e9\3\2\2\2\u00e7\u00e5\3\2\2\2\u00e7\u00e8"+ - "\3\2\2\2\u00e8\u00eb\3\2\2\2\u00e9\u00e7\3\2\2\2\u00ea\u00e1\3\2\2\2\u00ea"+ - "\u00eb\3\2\2\2\u00eb\u0103\3\2\2\2\u00ec\u00ed\7X\2\2\u00ed\u00f0\7\24"+ - "\2\2\u00ee\u00ef\7\22\2\2\u00ef\u00f1\5t;\2\u00f0\u00ee\3\2\2\2\u00f0"+ - "\u00f1\3\2\2\2\u00f1\u00f5\3\2\2\2\u00f2\u00f3\7Y\2\2\u00f3\u00f6\5@!"+ - "\2\u00f4\u00f6\5l\67\2\u00f5\u00f2\3\2\2\2\u00f5\u00f4\3\2\2\2\u00f5\u00f6"+ - "\3\2\2\2\u00f6\u00f8\3\2\2\2\u00f7\u00f9\5@!\2\u00f8\u00f7\3\2\2\2\u00f8"+ - "\u00f9\3\2\2\2\u00f9\u0103\3\2\2\2\u00fa\u00fb\7X\2\2\u00fb\u0100\7a\2"+ - "\2\u00fc\u00fe\t\7\2\2\u00fd\u00fc\3\2\2\2\u00fd\u00fe\3\2\2\2\u00fe\u00ff"+ - "\3\2\2\2\u00ff\u0101\5r:\2\u0100\u00fd\3\2\2\2\u0100\u0101\3\2\2\2\u0101"+ - "\u0103\3\2\2\2\u0102\u0080\3\2\2\2\u0102\u0081\3\2\2\2\u0102\u0092\3\2"+ - "\2\2\u0102\u00a1\3\2\2\2\u0102\u00b2\3\2\2\2\u0102\u00c1\3\2\2\2\u0102"+ - "\u00ce\3\2\2\2\u0102\u00d3\3\2\2\2\u0102\u00d5\3\2\2\2\u0102\u00d7\3\2"+ - "\2\2\u0102\u00ec\3\2\2\2\u0102\u00fa\3\2\2\2\u0103\7\3\2\2\2\u0104\u0105"+ - "\7f\2\2\u0105\u010a\5\34\17\2\u0106\u0107\7\5\2\2\u0107\u0109\5\34\17"+ - "\2\u0108\u0106\3\2\2\2\u0109\u010c\3\2\2\2\u010a\u0108\3\2\2\2\u010a\u010b"+ - "\3\2\2\2\u010b\u010e\3\2\2\2\u010c\u010a\3\2\2\2\u010d\u0104\3\2\2\2\u010d"+ - "\u010e\3\2\2\2\u010e\u010f\3\2\2\2\u010f\u0110\5\n\6\2\u0110\t\3\2\2\2"+ - "\u0111\u011c\5\16\b\2\u0112\u0113\7J\2\2\u0113\u0114\7\17\2\2\u0114\u0119"+ - "\5\20\t\2\u0115\u0116\7\5\2\2\u0116\u0118\5\20\t\2\u0117\u0115\3\2\2\2"+ - "\u0118\u011b\3\2\2\2\u0119\u0117\3\2\2\2\u0119\u011a\3\2\2\2\u011a\u011d"+ - "\3\2\2\2\u011b\u0119\3\2\2\2\u011c\u0112\3\2\2\2\u011c\u011d\3\2\2\2\u011d"+ - "\u011f\3\2\2\2\u011e\u0120\5\f\7\2\u011f\u011e\3\2\2\2\u011f\u0120\3\2"+ - "\2\2\u0120\13\3\2\2\2\u0121\u0122\7<\2\2\u0122\u0127\t\b\2\2\u0123\u0124"+ - "\7k\2\2\u0124\u0125\t\b\2\2\u0125\u0127\7q\2\2\u0126\u0121\3\2\2\2\u0126"+ - "\u0123\3\2\2\2\u0127\r\3\2\2\2\u0128\u012e\5\22\n\2\u0129\u012a\7\3\2"+ - "\2\u012a\u012b\5\n\6\2\u012b\u012c\7\4\2\2\u012c\u012e\3\2\2\2\u012d\u0128"+ - "\3\2\2\2\u012d\u0129\3\2\2\2\u012e\17\3\2\2\2\u012f\u0131\5\66\34\2\u0130"+ - "\u0132\t\t\2\2\u0131\u0130\3\2\2\2\u0131\u0132\3\2\2\2\u0132\u0135\3\2"+ - "\2\2\u0133\u0134\7F\2\2\u0134\u0136\t\n\2\2\u0135\u0133\3\2\2\2\u0135"+ - "\u0136\3\2\2\2\u0136\21\3\2\2\2\u0137\u0139\7V\2\2\u0138\u013a\5\36\20"+ - "\2\u0139\u0138\3\2\2\2\u0139\u013a\3\2\2\2\u013a\u013c\3\2\2\2\u013b\u013d"+ - "\5 \21\2\u013c\u013b\3\2\2\2\u013c\u013d\3\2\2\2\u013d\u013e\3\2\2\2\u013e"+ - "\u0140\5\"\22\2\u013f\u0141\5\24\13\2\u0140\u013f\3\2\2\2\u0140\u0141"+ - "\3\2\2\2\u0141\u0144\3\2\2\2\u0142\u0143\7e\2\2\u0143\u0145\58\35\2\u0144"+ - "\u0142\3\2\2\2\u0144\u0145\3\2\2\2\u0145\u0149\3\2\2\2\u0146\u0147\7/"+ - "\2\2\u0147\u0148\7\17\2\2\u0148\u014a\5\26\f\2\u0149\u0146\3\2\2\2\u0149"+ - "\u014a\3\2\2\2\u014a\u014d\3\2\2\2\u014b\u014c\7\60\2\2\u014c\u014e\5"+ - "8\35\2\u014d\u014b\3\2\2\2\u014d\u014e\3\2\2\2\u014e\23\3\2\2\2\u014f"+ - "\u0150\7*\2\2\u0150\u0155\5&\24\2\u0151\u0152\7\5\2\2\u0152\u0154\5&\24"+ - "\2\u0153\u0151\3\2\2\2\u0154\u0157\3\2\2\2\u0155\u0153\3\2\2\2\u0155\u0156"+ - "\3\2\2\2\u0156\u0159\3\2\2\2\u0157\u0155\3\2\2\2\u0158\u015a\5\60\31\2"+ - "\u0159\u0158\3\2\2\2\u0159\u015a\3\2\2\2\u015a\25\3\2\2\2\u015b\u015d"+ - "\5 \21\2\u015c\u015b\3\2\2\2\u015c\u015d\3\2\2\2\u015d\u015e\3\2\2\2\u015e"+ - "\u0163\5\30\r\2\u015f\u0160\7\5\2\2\u0160\u0162\5\30\r\2\u0161\u015f\3"+ - "\2\2\2\u0162\u0165\3\2\2\2\u0163\u0161\3\2\2\2\u0163\u0164\3\2\2\2\u0164"+ - "\27\3\2\2\2\u0165\u0163\3\2\2\2\u0166\u0167\5\32\16\2\u0167\31\3\2\2\2"+ - "\u0168\u0171\7\3\2\2\u0169\u016e\5\66\34\2\u016a\u016b\7\5\2\2\u016b\u016d"+ - "\5\66\34\2\u016c\u016a\3\2\2\2\u016d\u0170\3\2\2\2\u016e\u016c\3\2\2\2"+ - "\u016e\u016f\3\2\2\2\u016f\u0172\3\2\2\2\u0170\u016e\3\2\2\2\u0171\u0169"+ - "\3\2\2\2\u0171\u0172\3\2\2\2\u0172\u0173\3\2\2\2\u0173\u0176\7\4\2\2\u0174"+ - "\u0176\5\66\34\2\u0175\u0168\3\2\2\2\u0175\u0174\3\2\2\2\u0176\33\3\2"+ - "\2\2\u0177\u0178\5j\66\2\u0178\u0179\7\f\2\2\u0179\u017a\7\3\2\2\u017a"+ - "\u017b\5\n\6\2\u017b\u017c\7\4\2\2\u017c\35\3\2\2\2\u017d\u017e\7_\2\2"+ - "\u017e\u017f\7\u0082\2\2\u017f\37\3\2\2\2\u0180\u0181\t\13\2\2\u0181!"+ - "\3\2\2\2\u0182\u0187\5$\23\2\u0183\u0184\7\5\2\2\u0184\u0186\5$\23\2\u0185"+ - "\u0183\3\2\2\2\u0186\u0189\3\2\2\2\u0187\u0185\3\2\2\2\u0187\u0188\3\2"+ - "\2\2\u0188#\3\2\2\2\u0189\u0187\3\2\2\2\u018a\u018f\5\66\34\2\u018b\u018d"+ - "\7\f\2\2\u018c\u018b\3\2\2\2\u018c\u018d\3\2\2\2\u018d\u018e\3\2\2\2\u018e"+ - "\u0190\5j\66\2\u018f\u018c\3\2\2\2\u018f\u0190\3\2\2\2\u0190%\3\2\2\2"+ - "\u0191\u0195\5.\30\2\u0192\u0194\5(\25\2\u0193\u0192\3\2\2\2\u0194\u0197"+ - "\3\2\2\2\u0195\u0193\3\2\2\2\u0195\u0196\3\2\2\2\u0196\'\3\2\2\2\u0197"+ - "\u0195\3\2\2\2\u0198\u0199\5*\26\2\u0199\u019a\78\2\2\u019a\u019c\5.\30"+ - "\2\u019b\u019d\5,\27\2\u019c\u019b\3\2\2\2\u019c\u019d\3\2\2\2\u019d\u01a4"+ - "\3\2\2\2\u019e\u019f\7C\2\2\u019f\u01a0\5*\26\2\u01a0\u01a1\78\2\2\u01a1"+ - "\u01a2\5.\30\2\u01a2\u01a4\3\2\2\2\u01a3\u0198\3\2\2\2\u01a3\u019e\3\2"+ - "\2\2\u01a4)\3\2\2\2\u01a5\u01a7\7\65\2\2\u01a6\u01a5\3\2\2\2\u01a6\u01a7"+ - "\3\2\2\2\u01a7\u01b5\3\2\2\2\u01a8\u01aa\7:\2\2\u01a9\u01ab\7K\2\2\u01aa"+ - "\u01a9\3\2\2\2\u01aa\u01ab\3\2\2\2\u01ab\u01b5\3\2\2\2\u01ac\u01ae\7P"+ - "\2\2\u01ad\u01af\7K\2\2\u01ae\u01ad\3\2\2\2\u01ae\u01af\3\2\2\2\u01af"+ - "\u01b5\3\2\2\2\u01b0\u01b2\7,\2\2\u01b1\u01b3\7K\2\2\u01b2\u01b1\3\2\2"+ - "\2\u01b2\u01b3\3\2\2\2\u01b3\u01b5\3\2\2\2\u01b4\u01a6\3\2\2\2\u01b4\u01a8"+ - "\3\2\2\2\u01b4\u01ac\3\2\2\2\u01b4\u01b0\3\2\2\2\u01b5+\3\2\2\2\u01b6"+ - "\u01b7\7G\2\2\u01b7\u01c5\58\35\2\u01b8\u01b9\7b\2\2\u01b9\u01ba\7\3\2"+ - "\2\u01ba\u01bf\5j\66\2\u01bb\u01bc\7\5\2\2\u01bc\u01be\5j\66\2\u01bd\u01bb"+ - "\3\2\2\2\u01be\u01c1\3\2\2\2\u01bf\u01bd\3\2\2\2\u01bf\u01c0\3\2\2\2\u01c0"+ - "\u01c2\3\2\2\2\u01c1\u01bf\3\2\2\2\u01c2\u01c3\7\4\2\2\u01c3\u01c5\3\2"+ - "\2\2\u01c4\u01b6\3\2\2\2\u01c4\u01b8\3\2\2\2\u01c5-\3\2\2\2\u01c6\u01c8"+ - "\7+\2\2\u01c7\u01c6\3\2\2\2\u01c7\u01c8\3\2\2\2\u01c8\u01c9\3\2\2\2\u01c9"+ - "\u01ce\5l\67\2\u01ca\u01cc\7\f\2\2\u01cb\u01ca\3\2\2\2\u01cb\u01cc\3\2"+ - "\2\2\u01cc\u01cd\3\2\2\2\u01cd\u01cf\5h\65\2\u01ce\u01cb\3\2\2\2\u01ce"+ - "\u01cf\3\2\2\2\u01cf\u01e3\3\2\2\2\u01d0\u01d1\7\3\2\2\u01d1\u01d2\5\n"+ - "\6\2\u01d2\u01d7\7\4\2\2\u01d3\u01d5\7\f\2\2\u01d4\u01d3\3\2\2\2\u01d4"+ - "\u01d5\3\2\2\2\u01d5\u01d6\3\2\2\2\u01d6\u01d8\5h\65\2\u01d7\u01d4\3\2"+ - "\2\2\u01d7\u01d8\3\2\2\2\u01d8\u01e3\3\2\2\2\u01d9\u01da\7\3\2\2\u01da"+ - "\u01db\5&\24\2\u01db\u01e0\7\4\2\2\u01dc\u01de\7\f\2\2\u01dd\u01dc\3\2"+ - "\2\2\u01dd\u01de\3\2\2\2\u01de\u01df\3\2\2\2\u01df\u01e1\5h\65\2\u01e0"+ - "\u01dd\3\2\2\2\u01e0\u01e1\3\2\2\2\u01e1\u01e3\3\2\2\2\u01e2\u01c7\3\2"+ - "\2\2\u01e2\u01d0\3\2\2\2\u01e2\u01d9\3\2\2\2\u01e3/\3\2\2\2\u01e4\u01e5"+ - "\7N\2\2\u01e5\u01e6\7\3\2\2\u01e6\u01e7\5\62\32\2\u01e7\u01e8\7(\2\2\u01e8"+ - "\u01e9\5h\65\2\u01e9\u01ea\7\63\2\2\u01ea\u01eb\7\3\2\2\u01eb\u01ec\5"+ - "\62\32\2\u01ec\u01ed\7\4\2\2\u01ed\u01ee\7\4\2\2\u01ee\61\3\2\2\2\u01ef"+ - "\u01f4\5\64\33\2\u01f0\u01f1\7\5\2\2\u01f1\u01f3\5\64\33\2\u01f2\u01f0"+ - "\3\2\2\2\u01f3\u01f6\3\2\2\2\u01f4\u01f2\3\2\2\2\u01f4\u01f5\3\2\2\2\u01f5"+ - "\63\3\2\2\2\u01f6\u01f4\3\2\2\2\u01f7\u01fc\5F$\2\u01f8\u01fa\7\f\2\2"+ - "\u01f9\u01f8\3\2\2\2\u01f9\u01fa\3\2\2\2\u01fa\u01fb\3\2\2\2\u01fb\u01fd"+ - "\5j\66\2\u01fc\u01f9\3\2\2\2\u01fc\u01fd\3\2\2\2\u01fd\65\3\2\2\2\u01fe"+ - "\u01ff\58\35\2\u01ff\67\3\2\2\2\u0200\u0201\b\35\1\2\u0201\u0202\7D\2"+ - "\2\u0202\u0220\58\35\n\u0203\u0204\7#\2\2\u0204\u0205\7\3\2\2\u0205\u0206"+ - "\5\b\5\2\u0206\u0207\7\4\2\2\u0207\u0220\3\2\2\2\u0208\u0209\7R\2\2\u0209"+ - "\u020a\7\3\2\2\u020a\u020b\5t;\2\u020b\u020c\5:\36\2\u020c\u020d\7\4\2"+ - "\2\u020d\u0220\3\2\2\2\u020e\u020f\7>\2\2\u020f\u0210\7\3\2\2\u0210\u0211"+ - "\5h\65\2\u0211\u0212\7\5\2\2\u0212\u0213\5t;\2\u0213\u0214\5:\36\2\u0214"+ - "\u0215\7\4\2\2\u0215\u0220\3\2\2\2\u0216\u0217\7>\2\2\u0217\u0218\7\3"+ - "\2\2\u0218\u0219\5t;\2\u0219\u021a\7\5\2\2\u021a\u021b\5t;\2\u021b\u021c"+ - "\5:\36\2\u021c\u021d\7\4\2\2\u021d\u0220\3\2\2\2\u021e\u0220\5<\37\2\u021f"+ - "\u0200\3\2\2\2\u021f\u0203\3\2\2\2\u021f\u0208\3\2\2\2\u021f\u020e\3\2"+ - "\2\2\u021f\u0216\3\2\2\2\u021f\u021e\3\2\2\2\u0220\u0229\3\2\2\2\u0221"+ - "\u0222\f\4\2\2\u0222\u0223\7\n\2\2\u0223\u0228\58\35\5\u0224\u0225\f\3"+ - "\2\2\u0225\u0226\7I\2\2\u0226\u0228\58\35\4\u0227\u0221\3\2\2\2\u0227"+ - "\u0224\3\2\2\2\u0228\u022b\3\2\2\2\u0229\u0227\3\2\2\2\u0229\u022a\3\2"+ - "\2\2\u022a9\3\2\2\2\u022b\u0229\3\2\2\2\u022c\u022d\7\5\2\2\u022d\u022f"+ - "\5t;\2\u022e\u022c\3\2\2\2\u022f\u0232\3\2\2\2\u0230\u022e\3\2\2\2\u0230"+ - "\u0231\3\2\2\2\u0231;\3\2\2\2\u0232\u0230\3\2\2\2\u0233\u0235\5F$\2\u0234"+ - "\u0236\5> \2\u0235\u0234\3\2\2\2\u0235\u0236\3\2\2\2\u0236=\3\2\2\2\u0237"+ - "\u0239\7D\2\2\u0238\u0237\3\2\2\2\u0238\u0239\3\2\2\2\u0239\u023a\3\2"+ - "\2\2\u023a\u023b\7\16\2\2\u023b\u023c\5F$\2\u023c\u023d\7\n\2\2\u023d"+ - "\u023e\5F$\2\u023e\u0266\3\2\2\2\u023f\u0241\7D\2\2\u0240\u023f\3\2\2"+ - "\2\u0240\u0241\3\2\2\2\u0241\u0242\3\2\2\2\u0242\u0243\7\63\2\2\u0243"+ - "\u0244\7\3\2\2\u0244\u0249\5F$\2\u0245\u0246\7\5\2\2\u0246\u0248\5F$\2"+ - "\u0247\u0245\3\2\2\2\u0248\u024b\3\2\2\2\u0249\u0247\3\2\2\2\u0249\u024a"+ - "\3\2\2\2\u024a\u024c\3\2\2\2\u024b\u0249\3\2\2\2\u024c\u024d\7\4\2\2\u024d"+ - "\u0266\3\2\2\2\u024e\u0250\7D\2\2\u024f\u024e\3\2\2\2\u024f\u0250\3\2"+ - "\2\2\u0250\u0251\3\2\2\2\u0251\u0252\7\63\2\2\u0252\u0253\7\3\2\2\u0253"+ - "\u0254\5\b\5\2\u0254\u0255\7\4\2\2\u0255\u0266\3\2\2\2\u0256\u0258\7D"+ - "\2\2\u0257\u0256\3\2\2\2\u0257\u0258\3\2\2\2\u0258\u0259\3\2\2\2\u0259"+ - "\u025a\7;\2\2\u025a\u0266\5B\"\2\u025b\u025d\7D\2\2\u025c\u025b\3\2\2"+ - "\2\u025c\u025d\3\2\2\2\u025d\u025e\3\2\2\2\u025e\u025f\7Q\2\2\u025f\u0266"+ - "\5t;\2\u0260\u0262\7\67\2\2\u0261\u0263\7D\2\2\u0262\u0261\3\2\2\2\u0262"+ - "\u0263\3\2\2\2\u0263\u0264\3\2\2\2\u0264\u0266\7E\2\2\u0265\u0238\3\2"+ - "\2\2\u0265\u0240\3\2\2\2\u0265\u024f\3\2\2\2\u0265\u0257\3\2\2\2\u0265"+ - "\u025c\3\2\2\2\u0265\u0260\3\2\2\2\u0266?\3\2\2\2\u0267\u0268\7;\2\2\u0268"+ - "\u0269\5B\"\2\u0269A\3\2\2\2\u026a\u026c\5t;\2\u026b\u026d\5D#\2\u026c"+ - "\u026b\3\2\2\2\u026c\u026d\3\2\2\2\u026dC\3\2\2\2\u026e\u026f\7!\2\2\u026f"+ - "\u0275\5t;\2\u0270\u0271\7i\2\2\u0271\u0272\5t;\2\u0272\u0273\7q\2\2\u0273"+ - "\u0275\3\2\2\2\u0274\u026e\3\2\2\2\u0274\u0270\3\2\2\2\u0275E\3\2\2\2"+ - "\u0276\u0277\b$\1\2\u0277\u027b\5H%\2\u0278\u0279\t\7\2\2\u0279\u027b"+ - "\5F$\6\u027a\u0276\3\2\2\2\u027a\u0278\3\2\2\2\u027b\u0288\3\2\2\2\u027c"+ - "\u027d\f\5\2\2\u027d\u027e\t\f\2\2\u027e\u0287\5F$\6\u027f\u0280\f\4\2"+ - "\2\u0280\u0281\t\7\2\2\u0281\u0287\5F$\5\u0282\u0283\f\3\2\2\u0283\u0284"+ - "\5^\60\2\u0284\u0285\5F$\4\u0285\u0287\3\2\2\2\u0286\u027c\3\2\2\2\u0286"+ - "\u027f\3\2\2\2\u0286\u0282\3\2\2\2\u0287\u028a\3\2\2\2\u0288\u0286\3\2"+ - "\2\2\u0288\u0289\3\2\2\2\u0289G\3\2\2\2\u028a\u0288\3\2\2\2\u028b\u028c"+ - "\b%\1\2\u028c\u02b0\5L\'\2\u028d\u02b0\5R*\2\u028e\u02b0\5J&\2\u028f\u02b0"+ - "\5\\/\2\u0290\u0291\5h\65\2\u0291\u0292\7\177\2\2\u0292\u0294\3\2\2\2"+ - "\u0293\u0290\3\2\2\2\u0293\u0294\3\2\2\2\u0294\u0295\3\2\2\2\u0295\u02b0"+ - "\7{\2\2\u0296\u02b0\5V,\2\u0297\u0298\7\3\2\2\u0298\u0299\5\b\5\2\u0299"+ - "\u029a\7\4\2\2\u029a\u02b0\3\2\2\2\u029b\u02b0\5h\65\2\u029c\u029d\7\3"+ - "\2\2\u029d\u029e\5\66\34\2\u029e\u029f\7\4\2\2\u029f\u02b0\3\2\2\2\u02a0"+ - "\u02a2\7\20\2\2\u02a1\u02a3\58\35\2\u02a2\u02a1\3\2\2\2\u02a2\u02a3\3"+ - "\2\2\2\u02a3\u02a5\3\2\2\2\u02a4\u02a6\5v<\2\u02a5\u02a4\3\2\2\2\u02a6"+ - "\u02a7\3\2\2\2\u02a7\u02a5\3\2\2\2\u02a7\u02a8\3\2\2\2\u02a8\u02ab\3\2"+ - "\2\2\u02a9\u02aa\7\37\2\2\u02aa\u02ac\58\35\2\u02ab\u02a9\3\2\2\2\u02ab"+ - "\u02ac\3\2\2\2\u02ac\u02ad\3\2\2\2\u02ad\u02ae\7 \2\2\u02ae\u02b0\3\2"+ - "\2\2\u02af\u028b\3\2\2\2\u02af\u028d\3\2\2\2\u02af\u028e\3\2\2\2\u02af"+ - "\u028f\3\2\2\2\u02af\u0293\3\2\2\2\u02af\u0296\3\2\2\2\u02af\u0297\3\2"+ - "\2\2\u02af\u029b\3\2\2\2\u02af\u029c\3\2\2\2\u02af\u02a0\3\2\2\2\u02b0"+ - "\u02b6\3\2\2\2\u02b1\u02b2\f\f\2\2\u02b2\u02b3\7~\2\2\u02b3\u02b5\5f\64"+ - "\2\u02b4\u02b1\3\2\2\2\u02b5\u02b8\3\2\2\2\u02b6\u02b4\3\2\2\2\u02b6\u02b7"+ - "\3\2\2\2\u02b7I\3\2\2\2\u02b8\u02b6\3\2\2\2\u02b9\u02bd\7\30\2\2\u02ba"+ - "\u02bd\7\26\2\2\u02bb\u02bd\7\27\2\2\u02bc\u02b9\3\2\2\2\u02bc\u02ba\3"+ - "\2\2\2\u02bc\u02bb\3\2\2\2\u02bdK\3\2\2\2\u02be\u02c9\5N(\2\u02bf\u02c0"+ - "\7j\2\2\u02c0\u02c1\5N(\2\u02c1\u02c2\7q\2\2\u02c2\u02c9\3\2\2\2\u02c3"+ - "\u02c9\5P)\2\u02c4\u02c5\7j\2\2\u02c5\u02c6\5P)\2\u02c6\u02c7\7q\2\2\u02c7"+ - "\u02c9\3\2\2\2\u02c8\u02be\3\2\2\2\u02c8\u02bf\3\2\2\2\u02c8\u02c3\3\2"+ - "\2\2\u02c8\u02c4\3\2\2\2\u02c9M\3\2\2\2\u02ca\u02cb\7\21\2\2\u02cb\u02cc"+ - "\7\3\2\2\u02cc\u02cd\5\66\34\2\u02cd\u02ce\7\f\2\2\u02ce\u02cf\5f\64\2"+ - "\u02cf\u02d0\7\4\2\2\u02d0O\3\2\2\2\u02d1\u02d2\7\25\2\2\u02d2\u02d3\7"+ - "\3\2\2\u02d3\u02d4\5\66\34\2\u02d4\u02d5\7\5\2\2\u02d5\u02d6\5f\64\2\u02d6"+ - "\u02d7\7\4\2\2\u02d7Q\3\2\2\2\u02d8\u02de\5T+\2\u02d9\u02da\7j\2\2\u02da"+ - "\u02db\5T+\2\u02db\u02dc\7q\2\2\u02dc\u02de\3\2\2\2\u02dd\u02d8\3\2\2"+ - "\2\u02dd\u02d9\3\2\2\2\u02deS\3\2\2\2\u02df\u02e0\7%\2\2\u02e0\u02e1\7"+ - "\3\2\2\u02e1\u02e2\5j\66\2\u02e2\u02e3\7*\2\2\u02e3\u02e4\5F$\2\u02e4"+ - "\u02e5\7\4\2\2\u02e5U\3\2\2\2\u02e6\u02ec\5X-\2\u02e7\u02e8\7j\2\2\u02e8"+ - "\u02e9\5X-\2\u02e9\u02ea\7q\2\2\u02ea\u02ec\3\2\2\2\u02eb\u02e6\3\2\2"+ - "\2\u02eb\u02e7\3\2\2\2\u02ecW\3\2\2\2\u02ed\u02ee\5Z.\2\u02ee\u02fa\7"+ - "\3\2\2\u02ef\u02f1\5 \21\2\u02f0\u02ef\3\2\2\2\u02f0\u02f1\3\2\2\2\u02f1"+ - "\u02f2\3\2\2\2\u02f2\u02f7\5\66\34\2\u02f3\u02f4\7\5\2\2\u02f4\u02f6\5"+ - "\66\34\2\u02f5\u02f3\3\2\2\2\u02f6\u02f9\3\2\2\2\u02f7\u02f5\3\2\2\2\u02f7"+ - "\u02f8\3\2\2\2\u02f8\u02fb\3\2\2\2\u02f9\u02f7\3\2\2\2\u02fa\u02f0\3\2"+ - "\2\2\u02fa\u02fb\3\2\2\2\u02fb\u02fc\3\2\2\2\u02fc\u02fd\7\4\2\2\u02fd"+ - "Y\3\2\2\2\u02fe\u0302\7:\2\2\u02ff\u0302\7P\2\2\u0300\u0302\5j\66\2\u0301"+ - "\u02fe\3\2\2\2\u0301\u02ff\3\2\2\2\u0301\u0300\3\2\2\2\u0302[\3\2\2\2"+ - "\u0303\u031e\7E\2\2\u0304\u031e\5b\62\2\u0305\u031e\5r:\2\u0306\u031e"+ - "\5`\61\2\u0307\u0309\7\u0081\2\2\u0308\u0307\3\2\2\2\u0309\u030a\3\2\2"+ - "\2\u030a\u0308\3\2\2\2\u030a\u030b\3\2\2\2\u030b\u031e\3\2\2\2\u030c\u031e"+ - "\7\u0080\2\2\u030d\u030e\7l\2\2\u030e\u030f\5t;\2\u030f\u0310\7q\2\2\u0310"+ - "\u031e\3\2\2\2\u0311\u0312\7m\2\2\u0312\u0313\5t;\2\u0313\u0314\7q\2\2"+ - "\u0314\u031e\3\2\2\2\u0315\u0316\7n\2\2\u0316\u0317\5t;\2\u0317\u0318"+ - "\7q\2\2\u0318\u031e\3\2\2\2\u0319\u031a\7o\2\2\u031a\u031b\5t;\2\u031b"+ - "\u031c\7q\2\2\u031c\u031e\3\2\2\2\u031d\u0303\3\2\2\2\u031d\u0304\3\2"+ - "\2\2\u031d\u0305\3\2\2\2\u031d\u0306\3\2\2\2\u031d\u0308\3\2\2\2\u031d"+ - "\u030c\3\2\2\2\u031d\u030d\3\2\2\2\u031d\u0311\3\2\2\2\u031d\u0315\3\2"+ - "\2\2\u031d\u0319\3\2\2\2\u031e]\3\2\2\2\u031f\u0320\t\r\2\2\u0320_\3\2"+ - "\2\2\u0321\u0322\t\16\2\2\u0322a\3\2\2\2\u0323\u0325\7\66\2\2\u0324\u0326"+ - "\t\7\2\2\u0325\u0324\3\2\2\2\u0325\u0326\3\2\2\2\u0326\u0329\3\2\2\2\u0327"+ - "\u032a\5r:\2\u0328\u032a\5t;\2\u0329\u0327\3\2\2\2\u0329\u0328\3\2\2\2"+ - "\u032a\u032b\3\2\2\2\u032b\u032e\5d\63\2\u032c\u032d\7^\2\2\u032d\u032f"+ - "\5d\63\2\u032e\u032c\3\2\2\2\u032e\u032f\3\2\2\2\u032fc\3\2\2\2\u0330"+ - "\u0331\t\17\2\2\u0331e\3\2\2\2\u0332\u0333\5j\66\2\u0333g\3\2\2\2\u0334"+ - "\u0335\5j\66\2\u0335\u0336\7\177\2\2\u0336\u0338\3\2\2\2\u0337\u0334\3"+ - "\2\2\2\u0338\u033b\3\2\2\2\u0339\u0337\3\2\2\2\u0339\u033a\3\2\2\2\u033a"+ - "\u033c\3\2\2\2\u033b\u0339\3\2\2\2\u033c\u033d\5j\66\2\u033di\3\2\2\2"+ - "\u033e\u0341\5n8\2\u033f\u0341\5p9\2\u0340\u033e\3\2\2\2\u0340\u033f\3"+ - "\2\2\2\u0341k\3\2\2\2\u0342\u0343\5j\66\2\u0343\u0344\7\6\2\2\u0344\u0346"+ - "\3\2\2\2\u0345\u0342\3\2\2\2\u0345\u0346\3\2\2\2\u0346\u0347\3\2\2\2\u0347"+ - "\u034f\7\u0086\2\2\u0348\u0349\5j\66\2\u0349\u034a\7\6\2\2\u034a\u034c"+ - "\3\2\2\2\u034b\u0348\3\2\2\2\u034b\u034c\3\2\2\2\u034c\u034d\3\2\2\2\u034d"+ - "\u034f\5j\66\2\u034e\u0345\3\2\2\2\u034e\u034b\3\2\2\2\u034fm\3\2\2\2"+ - "\u0350\u0353\7\u0087\2\2\u0351\u0353\7\u0088\2\2\u0352\u0350\3\2\2\2\u0352"+ - "\u0351\3\2\2\2\u0353o\3\2\2\2\u0354\u0358\7\u0084\2\2\u0355\u0358\5x="+ - "\2\u0356\u0358\7\u0085\2\2\u0357\u0354\3\2\2\2\u0357\u0355\3\2\2\2\u0357"+ - "\u0356\3\2\2\2\u0358q\3\2\2\2\u0359\u035c\7\u0083\2\2\u035a\u035c\7\u0082"+ - "\2\2\u035b\u0359\3\2\2\2\u035b\u035a\3\2\2\2\u035cs\3\2\2\2\u035d\u035e"+ - "\t\20\2\2\u035eu\3\2\2\2\u035f\u0360\7d\2\2\u0360\u0361\5\66\34\2\u0361"+ - "\u0362\7\\\2\2\u0362\u0363\5\66\34\2\u0363w\3\2\2\2\u0364\u0365\t\21\2"+ - "\2\u0365y\3\2\2\2x\u0089\u008b\u008f\u0098\u009a\u009e\u00a6\u00a8\u00ac"+ - "\u00b0\u00b6\u00ba\u00bf\u00c4\u00c8\u00cc\u00d1\u00db\u00df\u00e7\u00ea"+ - "\u00f0\u00f5\u00f8\u00fd\u0100\u0102\u010a\u010d\u0119\u011c\u011f\u0126"+ - "\u012d\u0131\u0135\u0139\u013c\u0140\u0144\u0149\u014d\u0155\u0159\u015c"+ - "\u0163\u016e\u0171\u0175\u0187\u018c\u018f\u0195\u019c\u01a3\u01a6\u01aa"+ - "\u01ae\u01b2\u01b4\u01bf\u01c4\u01c7\u01cb\u01ce\u01d4\u01d7\u01dd\u01e0"+ - "\u01e2\u01f4\u01f9\u01fc\u021f\u0227\u0229\u0230\u0235\u0238\u0240\u0249"+ - "\u024f\u0257\u025c\u0262\u0265\u026c\u0274\u027a\u0286\u0288\u0293\u02a2"+ - "\u02a7\u02ab\u02af\u02b6\u02bc\u02c8\u02dd\u02eb\u02f0\u02f7\u02fa\u0301"+ - "\u030a\u031d\u0325\u0329\u032e\u0339\u0340\u0345\u034b\u034e\u0352\u0357"+ - "\u035b"; - public static final ATN _ATN = - new ATNDeserializer().deserialize(_serializedATN.toCharArray()); - static { - _decisionToDFA = new DFA[_ATN.getNumberOfDecisions()]; - for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) { - _decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i); - } - } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/SqlBaseVisitor.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/SqlBaseVisitor.java index cdf80b11da7ac..cfb14a8e39d13 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/SqlBaseVisitor.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/SqlBaseVisitor.java @@ -1,5 +1,6 @@ // ANTLR GENERATED CODE: DO NOT EDIT package org.elasticsearch.xpack.sql.parser; + import org.antlr.v4.runtime.tree.ParseTreeVisitor; /** @@ -10,685 +11,788 @@ * operations with no return type. */ interface SqlBaseVisitor extends ParseTreeVisitor { - /** - * Visit a parse tree produced by {@link SqlBaseParser#singleStatement}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitSingleStatement(SqlBaseParser.SingleStatementContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#singleExpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitSingleExpression(SqlBaseParser.SingleExpressionContext ctx); - /** - * Visit a parse tree produced by the {@code statementDefault} - * labeled alternative in {@link SqlBaseParser#statement}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitStatementDefault(SqlBaseParser.StatementDefaultContext ctx); - /** - * Visit a parse tree produced by the {@code explain} - * labeled alternative in {@link SqlBaseParser#statement}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitExplain(SqlBaseParser.ExplainContext ctx); - /** - * Visit a parse tree produced by the {@code debug} - * labeled alternative in {@link SqlBaseParser#statement}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitDebug(SqlBaseParser.DebugContext ctx); - /** - * Visit a parse tree produced by the {@code showTables} - * labeled alternative in {@link SqlBaseParser#statement}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitShowTables(SqlBaseParser.ShowTablesContext ctx); - /** - * Visit a parse tree produced by the {@code showColumns} - * labeled alternative in {@link SqlBaseParser#statement}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitShowColumns(SqlBaseParser.ShowColumnsContext ctx); - /** - * Visit a parse tree produced by the {@code showFunctions} - * labeled alternative in {@link SqlBaseParser#statement}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitShowFunctions(SqlBaseParser.ShowFunctionsContext ctx); - /** - * Visit a parse tree produced by the {@code showSchemas} - * labeled alternative in {@link SqlBaseParser#statement}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitShowSchemas(SqlBaseParser.ShowSchemasContext ctx); - /** - * Visit a parse tree produced by the {@code showCatalogs} - * labeled alternative in {@link SqlBaseParser#statement}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitShowCatalogs(SqlBaseParser.ShowCatalogsContext ctx); - /** - * Visit a parse tree produced by the {@code sysTables} - * labeled alternative in {@link SqlBaseParser#statement}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitSysTables(SqlBaseParser.SysTablesContext ctx); - /** - * Visit a parse tree produced by the {@code sysColumns} - * labeled alternative in {@link SqlBaseParser#statement}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitSysColumns(SqlBaseParser.SysColumnsContext ctx); - /** - * Visit a parse tree produced by the {@code sysTypes} - * labeled alternative in {@link SqlBaseParser#statement}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitSysTypes(SqlBaseParser.SysTypesContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#query}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitQuery(SqlBaseParser.QueryContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#queryNoWith}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitQueryNoWith(SqlBaseParser.QueryNoWithContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#limitClause}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitLimitClause(SqlBaseParser.LimitClauseContext ctx); - /** - * Visit a parse tree produced by the {@code queryPrimaryDefault} - * labeled alternative in {@link SqlBaseParser#queryTerm}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitQueryPrimaryDefault(SqlBaseParser.QueryPrimaryDefaultContext ctx); - /** - * Visit a parse tree produced by the {@code subquery} - * labeled alternative in {@link SqlBaseParser#queryTerm}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitSubquery(SqlBaseParser.SubqueryContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#orderBy}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitOrderBy(SqlBaseParser.OrderByContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#querySpecification}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitQuerySpecification(SqlBaseParser.QuerySpecificationContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#fromClause}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitFromClause(SqlBaseParser.FromClauseContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#groupBy}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitGroupBy(SqlBaseParser.GroupByContext ctx); - /** - * Visit a parse tree produced by the {@code singleGroupingSet} - * labeled alternative in {@link SqlBaseParser#groupingElement}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitSingleGroupingSet(SqlBaseParser.SingleGroupingSetContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#groupingExpressions}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitGroupingExpressions(SqlBaseParser.GroupingExpressionsContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#namedQuery}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitNamedQuery(SqlBaseParser.NamedQueryContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#topClause}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitTopClause(SqlBaseParser.TopClauseContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#setQuantifier}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitSetQuantifier(SqlBaseParser.SetQuantifierContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#selectItems}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitSelectItems(SqlBaseParser.SelectItemsContext ctx); - /** - * Visit a parse tree produced by the {@code selectExpression} - * labeled alternative in {@link SqlBaseParser#selectItem}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitSelectExpression(SqlBaseParser.SelectExpressionContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#relation}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitRelation(SqlBaseParser.RelationContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#joinRelation}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitJoinRelation(SqlBaseParser.JoinRelationContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#joinType}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitJoinType(SqlBaseParser.JoinTypeContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#joinCriteria}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitJoinCriteria(SqlBaseParser.JoinCriteriaContext ctx); - /** - * Visit a parse tree produced by the {@code tableName} - * labeled alternative in {@link SqlBaseParser#relationPrimary}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitTableName(SqlBaseParser.TableNameContext ctx); - /** - * Visit a parse tree produced by the {@code aliasedQuery} - * labeled alternative in {@link SqlBaseParser#relationPrimary}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitAliasedQuery(SqlBaseParser.AliasedQueryContext ctx); - /** - * Visit a parse tree produced by the {@code aliasedRelation} - * labeled alternative in {@link SqlBaseParser#relationPrimary}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitAliasedRelation(SqlBaseParser.AliasedRelationContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#pivotClause}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitPivotClause(SqlBaseParser.PivotClauseContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#pivotArgs}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitPivotArgs(SqlBaseParser.PivotArgsContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#namedValueExpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitNamedValueExpression(SqlBaseParser.NamedValueExpressionContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#expression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitExpression(SqlBaseParser.ExpressionContext ctx); - /** - * Visit a parse tree produced by the {@code logicalNot} - * labeled alternative in {@link SqlBaseParser#booleanExpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitLogicalNot(SqlBaseParser.LogicalNotContext ctx); - /** - * Visit a parse tree produced by the {@code stringQuery} - * labeled alternative in {@link SqlBaseParser#booleanExpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitStringQuery(SqlBaseParser.StringQueryContext ctx); - /** - * Visit a parse tree produced by the {@code booleanDefault} - * labeled alternative in {@link SqlBaseParser#booleanExpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitBooleanDefault(SqlBaseParser.BooleanDefaultContext ctx); - /** - * Visit a parse tree produced by the {@code exists} - * labeled alternative in {@link SqlBaseParser#booleanExpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitExists(SqlBaseParser.ExistsContext ctx); - /** - * Visit a parse tree produced by the {@code multiMatchQuery} - * labeled alternative in {@link SqlBaseParser#booleanExpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitMultiMatchQuery(SqlBaseParser.MultiMatchQueryContext ctx); - /** - * Visit a parse tree produced by the {@code matchQuery} - * labeled alternative in {@link SqlBaseParser#booleanExpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitMatchQuery(SqlBaseParser.MatchQueryContext ctx); - /** - * Visit a parse tree produced by the {@code logicalBinary} - * labeled alternative in {@link SqlBaseParser#booleanExpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitLogicalBinary(SqlBaseParser.LogicalBinaryContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#matchQueryOptions}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitMatchQueryOptions(SqlBaseParser.MatchQueryOptionsContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#predicated}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitPredicated(SqlBaseParser.PredicatedContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#predicate}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitPredicate(SqlBaseParser.PredicateContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#likePattern}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitLikePattern(SqlBaseParser.LikePatternContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#pattern}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitPattern(SqlBaseParser.PatternContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#patternEscape}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitPatternEscape(SqlBaseParser.PatternEscapeContext ctx); - /** - * Visit a parse tree produced by the {@code valueExpressionDefault} - * labeled alternative in {@link SqlBaseParser#valueExpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitValueExpressionDefault(SqlBaseParser.ValueExpressionDefaultContext ctx); - /** - * Visit a parse tree produced by the {@code comparison} - * labeled alternative in {@link SqlBaseParser#valueExpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitComparison(SqlBaseParser.ComparisonContext ctx); - /** - * Visit a parse tree produced by the {@code arithmeticBinary} - * labeled alternative in {@link SqlBaseParser#valueExpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitArithmeticBinary(SqlBaseParser.ArithmeticBinaryContext ctx); - /** - * Visit a parse tree produced by the {@code arithmeticUnary} - * labeled alternative in {@link SqlBaseParser#valueExpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitArithmeticUnary(SqlBaseParser.ArithmeticUnaryContext ctx); - /** - * Visit a parse tree produced by the {@code dereference} - * labeled alternative in {@link SqlBaseParser#primaryExpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitDereference(SqlBaseParser.DereferenceContext ctx); - /** - * Visit a parse tree produced by the {@code cast} - * labeled alternative in {@link SqlBaseParser#primaryExpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitCast(SqlBaseParser.CastContext ctx); - /** - * Visit a parse tree produced by the {@code constantDefault} - * labeled alternative in {@link SqlBaseParser#primaryExpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitConstantDefault(SqlBaseParser.ConstantDefaultContext ctx); - /** - * Visit a parse tree produced by the {@code extract} - * labeled alternative in {@link SqlBaseParser#primaryExpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitExtract(SqlBaseParser.ExtractContext ctx); - /** - * Visit a parse tree produced by the {@code parenthesizedExpression} - * labeled alternative in {@link SqlBaseParser#primaryExpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitParenthesizedExpression(SqlBaseParser.ParenthesizedExpressionContext ctx); - /** - * Visit a parse tree produced by the {@code star} - * labeled alternative in {@link SqlBaseParser#primaryExpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitStar(SqlBaseParser.StarContext ctx); - /** - * Visit a parse tree produced by the {@code castOperatorExpression} - * labeled alternative in {@link SqlBaseParser#primaryExpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitCastOperatorExpression(SqlBaseParser.CastOperatorExpressionContext ctx); - /** - * Visit a parse tree produced by the {@code function} - * labeled alternative in {@link SqlBaseParser#primaryExpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitFunction(SqlBaseParser.FunctionContext ctx); - /** - * Visit a parse tree produced by the {@code currentDateTimeFunction} - * labeled alternative in {@link SqlBaseParser#primaryExpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitCurrentDateTimeFunction(SqlBaseParser.CurrentDateTimeFunctionContext ctx); - /** - * Visit a parse tree produced by the {@code subqueryExpression} - * labeled alternative in {@link SqlBaseParser#primaryExpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitSubqueryExpression(SqlBaseParser.SubqueryExpressionContext ctx); - /** - * Visit a parse tree produced by the {@code case} - * labeled alternative in {@link SqlBaseParser#primaryExpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitCase(SqlBaseParser.CaseContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#builtinDateTimeFunction}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitBuiltinDateTimeFunction(SqlBaseParser.BuiltinDateTimeFunctionContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#castExpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitCastExpression(SqlBaseParser.CastExpressionContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#castTemplate}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitCastTemplate(SqlBaseParser.CastTemplateContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#convertTemplate}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitConvertTemplate(SqlBaseParser.ConvertTemplateContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#extractExpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitExtractExpression(SqlBaseParser.ExtractExpressionContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#extractTemplate}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitExtractTemplate(SqlBaseParser.ExtractTemplateContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#functionExpression}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitFunctionExpression(SqlBaseParser.FunctionExpressionContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#functionTemplate}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitFunctionTemplate(SqlBaseParser.FunctionTemplateContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#functionName}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitFunctionName(SqlBaseParser.FunctionNameContext ctx); - /** - * Visit a parse tree produced by the {@code nullLiteral} - * labeled alternative in {@link SqlBaseParser#constant}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitNullLiteral(SqlBaseParser.NullLiteralContext ctx); - /** - * Visit a parse tree produced by the {@code intervalLiteral} - * labeled alternative in {@link SqlBaseParser#constant}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitIntervalLiteral(SqlBaseParser.IntervalLiteralContext ctx); - /** - * Visit a parse tree produced by the {@code numericLiteral} - * labeled alternative in {@link SqlBaseParser#constant}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitNumericLiteral(SqlBaseParser.NumericLiteralContext ctx); - /** - * Visit a parse tree produced by the {@code booleanLiteral} - * labeled alternative in {@link SqlBaseParser#constant}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitBooleanLiteral(SqlBaseParser.BooleanLiteralContext ctx); - /** - * Visit a parse tree produced by the {@code stringLiteral} - * labeled alternative in {@link SqlBaseParser#constant}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitStringLiteral(SqlBaseParser.StringLiteralContext ctx); - /** - * Visit a parse tree produced by the {@code paramLiteral} - * labeled alternative in {@link SqlBaseParser#constant}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitParamLiteral(SqlBaseParser.ParamLiteralContext ctx); - /** - * Visit a parse tree produced by the {@code dateEscapedLiteral} - * labeled alternative in {@link SqlBaseParser#constant}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitDateEscapedLiteral(SqlBaseParser.DateEscapedLiteralContext ctx); - /** - * Visit a parse tree produced by the {@code timeEscapedLiteral} - * labeled alternative in {@link SqlBaseParser#constant}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitTimeEscapedLiteral(SqlBaseParser.TimeEscapedLiteralContext ctx); - /** - * Visit a parse tree produced by the {@code timestampEscapedLiteral} - * labeled alternative in {@link SqlBaseParser#constant}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitTimestampEscapedLiteral(SqlBaseParser.TimestampEscapedLiteralContext ctx); - /** - * Visit a parse tree produced by the {@code guidEscapedLiteral} - * labeled alternative in {@link SqlBaseParser#constant}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitGuidEscapedLiteral(SqlBaseParser.GuidEscapedLiteralContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#comparisonOperator}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitComparisonOperator(SqlBaseParser.ComparisonOperatorContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#booleanValue}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitBooleanValue(SqlBaseParser.BooleanValueContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#interval}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitInterval(SqlBaseParser.IntervalContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#intervalField}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitIntervalField(SqlBaseParser.IntervalFieldContext ctx); - /** - * Visit a parse tree produced by the {@code primitiveDataType} - * labeled alternative in {@link SqlBaseParser#dataType}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitPrimitiveDataType(SqlBaseParser.PrimitiveDataTypeContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#qualifiedName}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitQualifiedName(SqlBaseParser.QualifiedNameContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#identifier}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitIdentifier(SqlBaseParser.IdentifierContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#tableIdentifier}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitTableIdentifier(SqlBaseParser.TableIdentifierContext ctx); - /** - * Visit a parse tree produced by the {@code quotedIdentifier} - * labeled alternative in {@link SqlBaseParser#quoteIdentifier}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitQuotedIdentifier(SqlBaseParser.QuotedIdentifierContext ctx); - /** - * Visit a parse tree produced by the {@code backQuotedIdentifier} - * labeled alternative in {@link SqlBaseParser#quoteIdentifier}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitBackQuotedIdentifier(SqlBaseParser.BackQuotedIdentifierContext ctx); - /** - * Visit a parse tree produced by the {@code unquotedIdentifier} - * labeled alternative in {@link SqlBaseParser#unquoteIdentifier}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitUnquotedIdentifier(SqlBaseParser.UnquotedIdentifierContext ctx); - /** - * Visit a parse tree produced by the {@code digitIdentifier} - * labeled alternative in {@link SqlBaseParser#unquoteIdentifier}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitDigitIdentifier(SqlBaseParser.DigitIdentifierContext ctx); - /** - * Visit a parse tree produced by the {@code decimalLiteral} - * labeled alternative in {@link SqlBaseParser#number}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitDecimalLiteral(SqlBaseParser.DecimalLiteralContext ctx); - /** - * Visit a parse tree produced by the {@code integerLiteral} - * labeled alternative in {@link SqlBaseParser#number}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitIntegerLiteral(SqlBaseParser.IntegerLiteralContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#string}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitString(SqlBaseParser.StringContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#whenClause}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitWhenClause(SqlBaseParser.WhenClauseContext ctx); - /** - * Visit a parse tree produced by {@link SqlBaseParser#nonReserved}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitNonReserved(SqlBaseParser.NonReservedContext ctx); + /** + * Visit a parse tree produced by {@link SqlBaseParser#singleStatement}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitSingleStatement(SqlBaseParser.SingleStatementContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#singleExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitSingleExpression(SqlBaseParser.SingleExpressionContext ctx); + + /** + * Visit a parse tree produced by the {@code statementDefault} + * labeled alternative in {@link SqlBaseParser#statement}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitStatementDefault(SqlBaseParser.StatementDefaultContext ctx); + + /** + * Visit a parse tree produced by the {@code explain} + * labeled alternative in {@link SqlBaseParser#statement}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitExplain(SqlBaseParser.ExplainContext ctx); + + /** + * Visit a parse tree produced by the {@code debug} + * labeled alternative in {@link SqlBaseParser#statement}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitDebug(SqlBaseParser.DebugContext ctx); + + /** + * Visit a parse tree produced by the {@code showTables} + * labeled alternative in {@link SqlBaseParser#statement}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitShowTables(SqlBaseParser.ShowTablesContext ctx); + + /** + * Visit a parse tree produced by the {@code showColumns} + * labeled alternative in {@link SqlBaseParser#statement}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitShowColumns(SqlBaseParser.ShowColumnsContext ctx); + + /** + * Visit a parse tree produced by the {@code showFunctions} + * labeled alternative in {@link SqlBaseParser#statement}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitShowFunctions(SqlBaseParser.ShowFunctionsContext ctx); + + /** + * Visit a parse tree produced by the {@code showSchemas} + * labeled alternative in {@link SqlBaseParser#statement}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitShowSchemas(SqlBaseParser.ShowSchemasContext ctx); + + /** + * Visit a parse tree produced by the {@code showCatalogs} + * labeled alternative in {@link SqlBaseParser#statement}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitShowCatalogs(SqlBaseParser.ShowCatalogsContext ctx); + + /** + * Visit a parse tree produced by the {@code sysTables} + * labeled alternative in {@link SqlBaseParser#statement}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitSysTables(SqlBaseParser.SysTablesContext ctx); + + /** + * Visit a parse tree produced by the {@code sysColumns} + * labeled alternative in {@link SqlBaseParser#statement}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitSysColumns(SqlBaseParser.SysColumnsContext ctx); + + /** + * Visit a parse tree produced by the {@code sysTypes} + * labeled alternative in {@link SqlBaseParser#statement}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitSysTypes(SqlBaseParser.SysTypesContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#query}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitQuery(SqlBaseParser.QueryContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#queryNoWith}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitQueryNoWith(SqlBaseParser.QueryNoWithContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#limitClause}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitLimitClause(SqlBaseParser.LimitClauseContext ctx); + + /** + * Visit a parse tree produced by the {@code queryPrimaryDefault} + * labeled alternative in {@link SqlBaseParser#queryTerm}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitQueryPrimaryDefault(SqlBaseParser.QueryPrimaryDefaultContext ctx); + + /** + * Visit a parse tree produced by the {@code subquery} + * labeled alternative in {@link SqlBaseParser#queryTerm}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitSubquery(SqlBaseParser.SubqueryContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#orderBy}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitOrderBy(SqlBaseParser.OrderByContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#querySpecification}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitQuerySpecification(SqlBaseParser.QuerySpecificationContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#fromClause}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitFromClause(SqlBaseParser.FromClauseContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#groupBy}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitGroupBy(SqlBaseParser.GroupByContext ctx); + + /** + * Visit a parse tree produced by the {@code singleGroupingSet} + * labeled alternative in {@link SqlBaseParser#groupingElement}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitSingleGroupingSet(SqlBaseParser.SingleGroupingSetContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#groupingExpressions}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitGroupingExpressions(SqlBaseParser.GroupingExpressionsContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#namedQuery}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitNamedQuery(SqlBaseParser.NamedQueryContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#topClause}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitTopClause(SqlBaseParser.TopClauseContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#setQuantifier}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitSetQuantifier(SqlBaseParser.SetQuantifierContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#selectItems}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitSelectItems(SqlBaseParser.SelectItemsContext ctx); + + /** + * Visit a parse tree produced by the {@code selectExpression} + * labeled alternative in {@link SqlBaseParser#selectItem}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitSelectExpression(SqlBaseParser.SelectExpressionContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#relation}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitRelation(SqlBaseParser.RelationContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#joinRelation}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitJoinRelation(SqlBaseParser.JoinRelationContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#joinType}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitJoinType(SqlBaseParser.JoinTypeContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#joinCriteria}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitJoinCriteria(SqlBaseParser.JoinCriteriaContext ctx); + + /** + * Visit a parse tree produced by the {@code tableName} + * labeled alternative in {@link SqlBaseParser#relationPrimary}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitTableName(SqlBaseParser.TableNameContext ctx); + + /** + * Visit a parse tree produced by the {@code aliasedQuery} + * labeled alternative in {@link SqlBaseParser#relationPrimary}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitAliasedQuery(SqlBaseParser.AliasedQueryContext ctx); + + /** + * Visit a parse tree produced by the {@code aliasedRelation} + * labeled alternative in {@link SqlBaseParser#relationPrimary}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitAliasedRelation(SqlBaseParser.AliasedRelationContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#pivotClause}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitPivotClause(SqlBaseParser.PivotClauseContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#pivotArgs}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitPivotArgs(SqlBaseParser.PivotArgsContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#namedValueExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitNamedValueExpression(SqlBaseParser.NamedValueExpressionContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#expression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitExpression(SqlBaseParser.ExpressionContext ctx); + + /** + * Visit a parse tree produced by the {@code logicalNot} + * labeled alternative in {@link SqlBaseParser#booleanExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitLogicalNot(SqlBaseParser.LogicalNotContext ctx); + + /** + * Visit a parse tree produced by the {@code stringQuery} + * labeled alternative in {@link SqlBaseParser#booleanExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitStringQuery(SqlBaseParser.StringQueryContext ctx); + + /** + * Visit a parse tree produced by the {@code booleanDefault} + * labeled alternative in {@link SqlBaseParser#booleanExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitBooleanDefault(SqlBaseParser.BooleanDefaultContext ctx); + + /** + * Visit a parse tree produced by the {@code exists} + * labeled alternative in {@link SqlBaseParser#booleanExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitExists(SqlBaseParser.ExistsContext ctx); + + /** + * Visit a parse tree produced by the {@code multiMatchQuery} + * labeled alternative in {@link SqlBaseParser#booleanExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitMultiMatchQuery(SqlBaseParser.MultiMatchQueryContext ctx); + + /** + * Visit a parse tree produced by the {@code matchQuery} + * labeled alternative in {@link SqlBaseParser#booleanExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitMatchQuery(SqlBaseParser.MatchQueryContext ctx); + + /** + * Visit a parse tree produced by the {@code logicalBinary} + * labeled alternative in {@link SqlBaseParser#booleanExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitLogicalBinary(SqlBaseParser.LogicalBinaryContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#matchQueryOptions}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitMatchQueryOptions(SqlBaseParser.MatchQueryOptionsContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#predicated}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitPredicated(SqlBaseParser.PredicatedContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#predicate}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitPredicate(SqlBaseParser.PredicateContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#likePattern}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitLikePattern(SqlBaseParser.LikePatternContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#pattern}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitPattern(SqlBaseParser.PatternContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#patternEscape}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitPatternEscape(SqlBaseParser.PatternEscapeContext ctx); + + /** + * Visit a parse tree produced by the {@code valueExpressionDefault} + * labeled alternative in {@link SqlBaseParser#valueExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitValueExpressionDefault(SqlBaseParser.ValueExpressionDefaultContext ctx); + + /** + * Visit a parse tree produced by the {@code comparison} + * labeled alternative in {@link SqlBaseParser#valueExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitComparison(SqlBaseParser.ComparisonContext ctx); + + /** + * Visit a parse tree produced by the {@code arithmeticBinary} + * labeled alternative in {@link SqlBaseParser#valueExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitArithmeticBinary(SqlBaseParser.ArithmeticBinaryContext ctx); + + /** + * Visit a parse tree produced by the {@code arithmeticUnary} + * labeled alternative in {@link SqlBaseParser#valueExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitArithmeticUnary(SqlBaseParser.ArithmeticUnaryContext ctx); + + /** + * Visit a parse tree produced by the {@code dereference} + * labeled alternative in {@link SqlBaseParser#primaryExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitDereference(SqlBaseParser.DereferenceContext ctx); + + /** + * Visit a parse tree produced by the {@code cast} + * labeled alternative in {@link SqlBaseParser#primaryExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitCast(SqlBaseParser.CastContext ctx); + + /** + * Visit a parse tree produced by the {@code constantDefault} + * labeled alternative in {@link SqlBaseParser#primaryExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitConstantDefault(SqlBaseParser.ConstantDefaultContext ctx); + + /** + * Visit a parse tree produced by the {@code extract} + * labeled alternative in {@link SqlBaseParser#primaryExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitExtract(SqlBaseParser.ExtractContext ctx); + + /** + * Visit a parse tree produced by the {@code parenthesizedExpression} + * labeled alternative in {@link SqlBaseParser#primaryExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitParenthesizedExpression(SqlBaseParser.ParenthesizedExpressionContext ctx); + + /** + * Visit a parse tree produced by the {@code star} + * labeled alternative in {@link SqlBaseParser#primaryExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitStar(SqlBaseParser.StarContext ctx); + + /** + * Visit a parse tree produced by the {@code castOperatorExpression} + * labeled alternative in {@link SqlBaseParser#primaryExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitCastOperatorExpression(SqlBaseParser.CastOperatorExpressionContext ctx); + + /** + * Visit a parse tree produced by the {@code function} + * labeled alternative in {@link SqlBaseParser#primaryExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitFunction(SqlBaseParser.FunctionContext ctx); + + /** + * Visit a parse tree produced by the {@code currentDateTimeFunction} + * labeled alternative in {@link SqlBaseParser#primaryExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitCurrentDateTimeFunction(SqlBaseParser.CurrentDateTimeFunctionContext ctx); + + /** + * Visit a parse tree produced by the {@code subqueryExpression} + * labeled alternative in {@link SqlBaseParser#primaryExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitSubqueryExpression(SqlBaseParser.SubqueryExpressionContext ctx); + + /** + * Visit a parse tree produced by the {@code case} + * labeled alternative in {@link SqlBaseParser#primaryExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitCase(SqlBaseParser.CaseContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#builtinDateTimeFunction}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitBuiltinDateTimeFunction(SqlBaseParser.BuiltinDateTimeFunctionContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#castExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitCastExpression(SqlBaseParser.CastExpressionContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#castTemplate}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitCastTemplate(SqlBaseParser.CastTemplateContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#convertTemplate}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitConvertTemplate(SqlBaseParser.ConvertTemplateContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#extractExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitExtractExpression(SqlBaseParser.ExtractExpressionContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#extractTemplate}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitExtractTemplate(SqlBaseParser.ExtractTemplateContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#functionExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitFunctionExpression(SqlBaseParser.FunctionExpressionContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#functionTemplate}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitFunctionTemplate(SqlBaseParser.FunctionTemplateContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#functionName}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitFunctionName(SqlBaseParser.FunctionNameContext ctx); + + /** + * Visit a parse tree produced by the {@code nullLiteral} + * labeled alternative in {@link SqlBaseParser#constant}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitNullLiteral(SqlBaseParser.NullLiteralContext ctx); + + /** + * Visit a parse tree produced by the {@code intervalLiteral} + * labeled alternative in {@link SqlBaseParser#constant}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitIntervalLiteral(SqlBaseParser.IntervalLiteralContext ctx); + + /** + * Visit a parse tree produced by the {@code numericLiteral} + * labeled alternative in {@link SqlBaseParser#constant}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitNumericLiteral(SqlBaseParser.NumericLiteralContext ctx); + + /** + * Visit a parse tree produced by the {@code booleanLiteral} + * labeled alternative in {@link SqlBaseParser#constant}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitBooleanLiteral(SqlBaseParser.BooleanLiteralContext ctx); + + /** + * Visit a parse tree produced by the {@code stringLiteral} + * labeled alternative in {@link SqlBaseParser#constant}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitStringLiteral(SqlBaseParser.StringLiteralContext ctx); + + /** + * Visit a parse tree produced by the {@code paramLiteral} + * labeled alternative in {@link SqlBaseParser#constant}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitParamLiteral(SqlBaseParser.ParamLiteralContext ctx); + + /** + * Visit a parse tree produced by the {@code dateEscapedLiteral} + * labeled alternative in {@link SqlBaseParser#constant}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitDateEscapedLiteral(SqlBaseParser.DateEscapedLiteralContext ctx); + + /** + * Visit a parse tree produced by the {@code timeEscapedLiteral} + * labeled alternative in {@link SqlBaseParser#constant}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitTimeEscapedLiteral(SqlBaseParser.TimeEscapedLiteralContext ctx); + + /** + * Visit a parse tree produced by the {@code timestampEscapedLiteral} + * labeled alternative in {@link SqlBaseParser#constant}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitTimestampEscapedLiteral(SqlBaseParser.TimestampEscapedLiteralContext ctx); + + /** + * Visit a parse tree produced by the {@code guidEscapedLiteral} + * labeled alternative in {@link SqlBaseParser#constant}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitGuidEscapedLiteral(SqlBaseParser.GuidEscapedLiteralContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#comparisonOperator}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitComparisonOperator(SqlBaseParser.ComparisonOperatorContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#booleanValue}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitBooleanValue(SqlBaseParser.BooleanValueContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#interval}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitInterval(SqlBaseParser.IntervalContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#intervalField}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitIntervalField(SqlBaseParser.IntervalFieldContext ctx); + + /** + * Visit a parse tree produced by the {@code primitiveDataType} + * labeled alternative in {@link SqlBaseParser#dataType}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitPrimitiveDataType(SqlBaseParser.PrimitiveDataTypeContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#qualifiedName}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitQualifiedName(SqlBaseParser.QualifiedNameContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#identifier}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitIdentifier(SqlBaseParser.IdentifierContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#tableIdentifier}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitTableIdentifier(SqlBaseParser.TableIdentifierContext ctx); + + /** + * Visit a parse tree produced by the {@code quotedIdentifier} + * labeled alternative in {@link SqlBaseParser#quoteIdentifier}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitQuotedIdentifier(SqlBaseParser.QuotedIdentifierContext ctx); + + /** + * Visit a parse tree produced by the {@code backQuotedIdentifier} + * labeled alternative in {@link SqlBaseParser#quoteIdentifier}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitBackQuotedIdentifier(SqlBaseParser.BackQuotedIdentifierContext ctx); + + /** + * Visit a parse tree produced by the {@code unquotedIdentifier} + * labeled alternative in {@link SqlBaseParser#unquoteIdentifier}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitUnquotedIdentifier(SqlBaseParser.UnquotedIdentifierContext ctx); + + /** + * Visit a parse tree produced by the {@code digitIdentifier} + * labeled alternative in {@link SqlBaseParser#unquoteIdentifier}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitDigitIdentifier(SqlBaseParser.DigitIdentifierContext ctx); + + /** + * Visit a parse tree produced by the {@code decimalLiteral} + * labeled alternative in {@link SqlBaseParser#number}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitDecimalLiteral(SqlBaseParser.DecimalLiteralContext ctx); + + /** + * Visit a parse tree produced by the {@code integerLiteral} + * labeled alternative in {@link SqlBaseParser#number}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitIntegerLiteral(SqlBaseParser.IntegerLiteralContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#string}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitString(SqlBaseParser.StringContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#whenClause}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitWhenClause(SqlBaseParser.WhenClauseContext ctx); + + /** + * Visit a parse tree produced by {@link SqlBaseParser#nonReserved}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitNonReserved(SqlBaseParser.NonReservedContext ctx); } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/SqlParser.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/SqlParser.java index ac6ae1a02a323..8ce6bb37872da 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/SqlParser.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/parser/SqlParser.java @@ -96,11 +96,13 @@ public Expression createExpression(String expression, List p return invokeParser(expression, params, UTC, SqlBaseParser::singleExpression, AstBuilder::expression); } - private T invokeParser(String sql, - List params, - ZoneId zoneId, - Function parseFunction, - BiFunction visitor) { + private T invokeParser( + String sql, + List params, + ZoneId zoneId, + Function parseFunction, + BiFunction visitor + ) { try { SqlBaseLexer lexer = new SqlBaseLexer(new CaseChangingCharStream(CharStreams.fromString(sql), true)); @@ -127,9 +129,7 @@ private T invokeParser(String sql, for (Token t : tokenStream.getTokens()) { String symbolicName = SqlBaseLexer.VOCABULARY.getSymbolicName(t.getType()); String literalName = SqlBaseLexer.VOCABULARY.getLiteralName(t.getType()); - log.info(format(Locale.ROOT, " %-15s '%s'", - symbolicName == null ? literalName : symbolicName, - t.getText())); + log.info(format(Locale.ROOT, " %-15s '%s'", symbolicName == null ? literalName : symbolicName, t.getText())); } } @@ -141,8 +141,10 @@ private T invokeParser(String sql, return visitor.apply(new AstBuilder(paramTokens, zoneId), tree); } catch (StackOverflowError e) { - throw new ParsingException("SQL statement is too large, " + - "causing stack overflow when generating the parsing tree: [{}]", sql); + throw new ParsingException( + "SQL statement is too large, " + "causing stack overflow when generating the parsing tree: [{}]", + sql + ); } } @@ -155,12 +157,24 @@ private static void debug(SqlBaseParser parser) { parser.addErrorListener(new DiagnosticErrorListener(false) { @Override - public void reportAttemptingFullContext(Parser recognizer, DFA dfa, - int startIndex, int stopIndex, BitSet conflictingAlts, ATNConfigSet configs) {} + public void reportAttemptingFullContext( + Parser recognizer, + DFA dfa, + int startIndex, + int stopIndex, + BitSet conflictingAlts, + ATNConfigSet configs + ) {} @Override - public void reportContextSensitivity(Parser recognizer, DFA dfa, - int startIndex, int stopIndex, int prediction, ATNConfigSet configs) {} + public void reportContextSensitivity( + Parser recognizer, + DFA dfa, + int startIndex, + int stopIndex, + int prediction, + ATNConfigSet configs + ) {} }); } @@ -175,20 +189,22 @@ private class PostProcessor extends SqlBaseBaseListener { public void exitBackQuotedIdentifier(SqlBaseParser.BackQuotedIdentifierContext context) { Token token = context.BACKQUOTED_IDENTIFIER().getSymbol(); throw new ParsingException( - "backquoted identifiers not supported; please use double quotes instead", - null, - token.getLine(), - token.getCharPositionInLine()); + "backquoted identifiers not supported; please use double quotes instead", + null, + token.getLine(), + token.getCharPositionInLine() + ); } @Override public void exitDigitIdentifier(SqlBaseParser.DigitIdentifierContext context) { Token token = context.DIGIT_IDENTIFIER().getSymbol(); throw new ParsingException( - "identifiers must not start with a digit; please use double quotes", - null, - token.getLine(), - token.getCharPositionInLine()); + "identifiers must not start with a digit; please use double quotes", + null, + token.getLine(), + token.getCharPositionInLine() + ); } @Override @@ -197,12 +213,16 @@ public void exitQuotedIdentifier(SqlBaseParser.QuotedIdentifierContext context) context.getParent().removeLastChild(); Token token = (Token) context.getChild(0).getPayload(); - context.getParent().addChild(new CommonToken( - new Pair<>(token.getTokenSource(), token.getInputStream()), - SqlBaseLexer.IDENTIFIER, - token.getChannel(), - token.getStartIndex() + 1, - token.getStopIndex() - 1)); + context.getParent() + .addChild( + new CommonToken( + new Pair<>(token.getTokenSource(), token.getInputStream()), + SqlBaseLexer.IDENTIFIER, + token.getChannel(), + token.getStartIndex() + 1, + token.getStopIndex() - 1 + ) + ); } @Override @@ -217,19 +237,29 @@ public void exitNonReserved(SqlBaseParser.NonReservedContext context) { context.getParent().removeLastChild(); Token token = (Token) context.getChild(0).getPayload(); - context.getParent().addChild(new CommonToken( - new Pair<>(token.getTokenSource(), token.getInputStream()), - SqlBaseLexer.IDENTIFIER, - token.getChannel(), - token.getStartIndex(), - token.getStopIndex())); + context.getParent() + .addChild( + new CommonToken( + new Pair<>(token.getTokenSource(), token.getInputStream()), + SqlBaseLexer.IDENTIFIER, + token.getChannel(), + token.getStartIndex(), + token.getStopIndex() + ) + ); } } private static final BaseErrorListener ERROR_LISTENER = new BaseErrorListener() { @Override - public void syntaxError(Recognizer recognizer, Object offendingSymbol, int line, - int charPositionInLine, String message, RecognitionException e) { + public void syntaxError( + Recognizer recognizer, + Object offendingSymbol, + int line, + int charPositionInLine, + String message, + RecognitionException e + ) { throw new ParsingException(message, e, line, charPositionInLine); } }; diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/Join.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/Join.java index d696dd2e6ad87..5efcf8119c6fa 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/Join.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/Join.java @@ -77,9 +77,7 @@ public List output() { } private static List makeNullable(List output) { - return output.stream() - .map(a -> a.withNullability(Nullability.TRUE)) - .collect(toList()); + return output.stream().map(a -> a.withNullability(Nullability.TRUE)).collect(toList()); } @Override @@ -97,10 +95,10 @@ public boolean resolved() { // - the children are resolved // - there are no conflicts in output // - the condition (if present) is resolved to a boolean - return childrenResolved() && - duplicatesResolved() && - expressionsResolved() && - (condition == null || DataTypes.BOOLEAN == condition.dataType()); + return childrenResolved() + && duplicatesResolved() + && expressionsResolved() + && (condition == null || DataTypes.BOOLEAN == condition.dataType()); } @Override @@ -120,8 +118,8 @@ public boolean equals(Object obj) { Join other = (Join) obj; return Objects.equals(type, other.type) - && Objects.equals(condition, other.condition) - && Objects.equals(left(), other.left()) - && Objects.equals(right(), other.right()); + && Objects.equals(condition, other.condition) + && Objects.equals(left(), other.left()) + && Objects.equals(right(), other.right()); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/Pivot.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/Pivot.java index b10d3197bbf44..547c1a0c8e3fa 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/Pivot.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/Pivot.java @@ -45,8 +45,14 @@ public Pivot(Source source, LogicalPlan child, Expression column, List values, List aggregates, - List grouping) { + public Pivot( + Source source, + LogicalPlan child, + Expression column, + List values, + List aggregates, + List grouping + ) { super(source, child); this.column = column; this.values = values; @@ -57,11 +63,13 @@ public Pivot(Source source, LogicalPlan child, Expression column, List(new AttributeSet(Expressions.onlyPrimitiveFieldAttributes(child().output())) + grouping = new ArrayList<>( + new AttributeSet(Expressions.onlyPrimitiveFieldAttributes(child().output())) // make sure to have the column as the last entry (helps with translation) so substract it .subtract(columnSet) .subtract(Expressions.references(aggregates)) - .combine(columnSet)); + .combine(columnSet) + ); } this.grouping = grouping; @@ -120,7 +128,7 @@ private AttributeSet valuesOutput() { name = ((Function) a.child()).functionName(); } } - //FIXME: the value attributes are reused and thus will clash - new ids need to be created + // FIXME: the value attributes are reused and thus will clash - new ids need to be created for (NamedExpression value : values) { out.add(value.toAttribute().withName(value.name() + "_" + name).withDataType(agg.dataType())); } @@ -157,9 +165,7 @@ public AttributeMap valuesToLiterals() { @Override public List output() { if (output == null) { - output = new ArrayList<>(groupingSet() - .subtract(Expressions.references(singletonList(column))) - .combine(valuesOutput())); + output = new ArrayList<>(groupingSet().subtract(Expressions.references(singletonList(column))).combine(valuesOutput())); } return output; @@ -195,8 +201,8 @@ public boolean equals(Object obj) { Pivot other = (Pivot) obj; return Objects.equals(column, other.column) - && Objects.equals(values, other.values) - && Objects.equals(aggregates, other.aggregates) - && Objects.equals(child(), other.child()); + && Objects.equals(values, other.values) + && Objects.equals(aggregates, other.aggregates) + && Objects.equals(child(), other.child()); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/SubQueryAlias.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/SubQueryAlias.java index 7492686ac7b18..787eca7a0ae24 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/SubQueryAlias.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/SubQueryAlias.java @@ -44,10 +44,7 @@ public String alias() { @Override public List output() { if (output == null) { - output = alias == null ? child().output() : - child().output().stream() - .map(e -> e.withQualifier(alias)) - .collect(toList()); + output = alias == null ? child().output() : child().output().stream().map(e -> e.withQualifier(alias)).collect(toList()); } return output; } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/command/Debug.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/command/Debug.java index 7964110ebd5ea..c15705732d84a 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/command/Debug.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/command/Debug.java @@ -35,11 +35,13 @@ public class Debug extends Command { public enum Type { - ANALYZED, OPTIMIZED; + ANALYZED, + OPTIMIZED; } public enum Format { - TEXT, GRAPHVIZ + TEXT, + GRAPHVIZ } private final LogicalPlan plan; @@ -82,8 +84,11 @@ public void execute(SqlSession session, ActionListener listener) { session.debugAnalyzedPlan(plan, wrap(i -> handleInfo(i, listener), listener::onFailure)); break; case OPTIMIZED: - session.analyzedPlan(plan, true, - wrap(analyzedPlan -> handleInfo(session.optimizer().debugOptimize(analyzedPlan), listener), listener::onFailure)); + session.analyzedPlan( + plan, + true, + wrap(analyzedPlan -> handleInfo(session.optimizer().debugOptimize(analyzedPlan), listener), listener::onFailure) + ); break; default: break; diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/command/Explain.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/command/Explain.java index 3675bcb84cba0..1fe9e121bcc17 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/command/Explain.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/command/Explain.java @@ -35,7 +35,12 @@ public class Explain extends Command { public enum Type { - PARSED, ANALYZED, OPTIMIZED, MAPPED, EXECUTABLE, ALL; + PARSED, + ANALYZED, + OPTIMIZED, + MAPPED, + EXECUTABLE, + ALL; public String printableName() { return Strings.capitalize(name().toLowerCase(Locale.ROOT)); @@ -43,7 +48,8 @@ public String printableName() { } public enum Format { - TEXT, GRAPHVIZ + TEXT, + GRAPHVIZ } private final LogicalPlan plan; @@ -123,8 +129,11 @@ public void execute(SqlSession session, ActionListener listener) { } // Type.All - listener.onResponse(Page.last( - Rows.singleton(output(), printPlans(format, plan, analyzedPlan, optimizedPlan, mappedPlan, executablePlan)))); + listener.onResponse( + Page.last( + Rows.singleton(output(), printPlans(format, plan, analyzedPlan, optimizedPlan, mappedPlan, executablePlan)) + ) + ); }, listener::onFailure)); } @@ -153,8 +162,14 @@ public void execute(SqlSession session, ActionListener listener) { return; } - listener.onResponse(Page.last(Rows.singleton(output(), - printPlans(format, plan, analyzedPlan, optimizedPlan, mappedPlan, executablePlan)))); + listener.onResponse( + Page.last( + Rows.singleton( + output(), + printPlans(format, plan, analyzedPlan, optimizedPlan, mappedPlan, executablePlan) + ) + ) + ); return; } // mapped failed @@ -163,15 +178,15 @@ public void execute(SqlSession session, ActionListener listener) { return; } - listener.onResponse(Page - .last(Rows.singleton(output(), printPlans(format, plan, analyzedPlan, optimizedPlan, mappedPlan, null)))); + listener.onResponse( + Page.last(Rows.singleton(output(), printPlans(format, plan, analyzedPlan, optimizedPlan, mappedPlan, null))) + ); }, listener::onFailure)); // cannot continue } else { if (type != Type.ALL) { listener.onResponse(Page.last(Rows.singleton(output(), formatPlan(format, analyzedPlan)))); - } - else { + } else { listener.onResponse(Page.last(Rows.singleton(output(), printPlans(format, plan, analyzedPlan, null, null, null)))); } } @@ -179,8 +194,14 @@ public void execute(SqlSession session, ActionListener listener) { }, listener::onFailure)); } - private static String printPlans(Format format, LogicalPlan parsed, LogicalPlan analyzedPlan, LogicalPlan optimizedPlan, - PhysicalPlan mappedPlan, PhysicalPlan executionPlan) { + private static String printPlans( + Format format, + LogicalPlan parsed, + LogicalPlan analyzedPlan, + LogicalPlan optimizedPlan, + PhysicalPlan mappedPlan, + PhysicalPlan executionPlan + ) { if (format == Format.TEXT) { StringBuilder sb = new StringBuilder(); sb.append("Parsed\n"); @@ -237,8 +258,8 @@ public boolean equals(Object obj) { } Explain o = (Explain) obj; return Objects.equals(verify, o.verify) - && Objects.equals(format, o.format) - && Objects.equals(type, o.type) - && Objects.equals(plan, o.plan); + && Objects.equals(format, o.format) + && Objects.equals(type, o.type) + && Objects.equals(plan, o.plan); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/command/ShowColumns.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/command/ShowColumns.java index 62ae88480c109..9833c747a1933 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/command/ShowColumns.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/command/ShowColumns.java @@ -63,9 +63,11 @@ protected NodeInfo info() { @Override public List output() { - return asList(new FieldAttribute(source(), "column", new KeywordEsField("column")), - new FieldAttribute(source(), "type", new KeywordEsField("type")), - new FieldAttribute(source(), "mapping", new KeywordEsField("mapping"))); + return asList( + new FieldAttribute(source(), "column", new KeywordEsField("column")), + new FieldAttribute(source(), "type", new KeywordEsField("type")), + new FieldAttribute(source(), "mapping", new KeywordEsField("mapping")) + ); } @Override @@ -76,16 +78,14 @@ public void execute(SqlSession session, ActionListener listener) { idx = hasText(cat) && cat.equals(cluster) == false ? buildRemoteIndexName(cat, idx) : idx; boolean withFrozen = includeFrozen || session.configuration().includeFrozen(); - session.indexResolver().resolveAsMergedMapping(idx, withFrozen, emptyMap(), ActionListener.wrap( - indexResult -> { - List> rows = emptyList(); - if (indexResult.isValid()) { - rows = new ArrayList<>(); - fillInRows(indexResult.get().mapping(), null, rows); - } - listener.onResponse(of(session, rows)); - }, - listener::onFailure)); + session.indexResolver().resolveAsMergedMapping(idx, withFrozen, emptyMap(), ActionListener.wrap(indexResult -> { + List> rows = emptyList(); + if (indexResult.isValid()) { + rows = new ArrayList<>(); + fillInRows(indexResult.get().mapping(), null, rows); + } + listener.onResponse(of(session, rows)); + }, listener::onFailure)); } private void fillInRows(Map mapping, String prefix, List> rows) { @@ -120,8 +120,6 @@ public boolean equals(Object obj) { } ShowColumns other = (ShowColumns) obj; - return Objects.equals(index, other.index) - && Objects.equals(pattern, other.pattern) - && includeFrozen == other.includeFrozen; + return Objects.equals(index, other.index) && Objects.equals(pattern, other.pattern) && includeFrozen == other.includeFrozen; } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/command/ShowFunctions.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/command/ShowFunctions.java index 9f0e4413e2aaf..a89577d3b635a 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/command/ShowFunctions.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/command/ShowFunctions.java @@ -46,17 +46,19 @@ public LikePattern pattern() { @Override public List output() { - return asList(new FieldAttribute(source(), "name", new KeywordEsField("name")), - new FieldAttribute(source(), "type", new KeywordEsField("type"))); + return asList( + new FieldAttribute(source(), "name", new KeywordEsField("name")), + new FieldAttribute(source(), "type", new KeywordEsField("type")) + ); } @Override public void execute(SqlSession session, ActionListener listener) { FunctionRegistry registry = session.functionRegistry(); Collection functions = registry.listFunctions(pattern != null ? pattern.asJavaRegex() : null); - listener.onResponse(of(session, functions.stream() - .map(f -> asList(f.name(), SqlFunctionTypeRegistry.INSTANCE.type(f.clazz()))) - .collect(toList()))); + listener.onResponse( + of(session, functions.stream().map(f -> asList(f.name(), SqlFunctionTypeRegistry.INSTANCE.type(f.clazz()))).collect(toList())) + ); } @Override diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/command/ShowTables.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/command/ShowTables.java index ad3193f98b02b..8435892830d2d 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/command/ShowTables.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/command/ShowTables.java @@ -31,8 +31,7 @@ public class ShowTables extends Command { private final LikePattern pattern; private final boolean includeFrozen; - public ShowTables(Source source, LikePattern catalogPattern, String catalog, String index, LikePattern pattern, - boolean includeFrozen) { + public ShowTables(Source source, LikePattern catalogPattern, String catalog, String index, LikePattern pattern, boolean includeFrozen) { super(source); this.catalogPattern = catalogPattern; this.catalog = catalog; @@ -70,9 +69,12 @@ public final void execute(SqlSession session, ActionListener listener) { // to avoid redundancy, indicate whether frozen fields are required by specifying the type EnumSet indexType = withFrozen ? IndexType.VALID_INCLUDE_FROZEN : IndexType.VALID_REGULAR; session.indexResolver().resolveNames(cat, idx, regex, indexType, ActionListener.wrap(result -> { - listener.onResponse(of(session, result.stream() - .map(t -> asList(t.cluster(), t.name(), t.type().toSql(), t.type().toNative())) - .collect(toList()))); + listener.onResponse( + of( + session, + result.stream().map(t -> asList(t.cluster(), t.name(), t.type().toSql(), t.type().toNative())).collect(toList()) + ) + ); }, listener::onFailure)); } @@ -92,8 +94,6 @@ public boolean equals(Object obj) { } ShowTables other = (ShowTables) obj; - return Objects.equals(index, other.index) - && Objects.equals(pattern, other.pattern) - && includeFrozen == other.includeFrozen; + return Objects.equals(index, other.index) && Objects.equals(pattern, other.pattern) && includeFrozen == other.includeFrozen; } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/command/sys/SysColumns.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/command/sys/SysColumns.java index b1c4847cf8e8a..80cc9fb86280d 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/command/sys/SysColumns.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/command/sys/SysColumns.java @@ -35,14 +35,14 @@ import static java.util.Collections.emptyMap; import static org.elasticsearch.transport.RemoteClusterAware.REMOTE_CLUSTER_INDEX_SEPARATOR; import static org.elasticsearch.transport.RemoteClusterAware.buildRemoteIndexName; -import static org.elasticsearch.xpack.ql.util.RemoteClusterUtils.isQualified; -import static org.elasticsearch.xpack.ql.util.RemoteClusterUtils.splitQualifiedIndex; import static org.elasticsearch.xpack.ql.type.DataTypes.BINARY; import static org.elasticsearch.xpack.ql.type.DataTypes.INTEGER; import static org.elasticsearch.xpack.ql.type.DataTypes.NESTED; import static org.elasticsearch.xpack.ql.type.DataTypes.SHORT; import static org.elasticsearch.xpack.ql.type.DataTypes.isPrimitive; import static org.elasticsearch.xpack.ql.type.DataTypes.isString; +import static org.elasticsearch.xpack.ql.util.RemoteClusterUtils.isQualified; +import static org.elasticsearch.xpack.ql.util.RemoteClusterUtils.splitQualifiedIndex; import static org.elasticsearch.xpack.sql.type.SqlDataTypes.displaySize; import static org.elasticsearch.xpack.sql.type.SqlDataTypes.metaSqlDataType; import static org.elasticsearch.xpack.sql.type.SqlDataTypes.metaSqlDateTimeSub; @@ -83,32 +83,33 @@ private List output(boolean odbcCompatible) { // ODBC expects some fields as SHORT while JDBC as Integer // which causes conversion issues and CCE DataType clientBasedType = odbcCompatible ? SHORT : INTEGER; - return asList(keyword("TABLE_CAT"), - keyword("TABLE_SCHEM"), - keyword("TABLE_NAME"), - keyword("COLUMN_NAME"), - field("DATA_TYPE", clientBasedType), - keyword("TYPE_NAME"), - field("COLUMN_SIZE", INTEGER), - field("BUFFER_LENGTH", INTEGER), - field("DECIMAL_DIGITS", clientBasedType), - field("NUM_PREC_RADIX", clientBasedType), - field("NULLABLE", clientBasedType), - keyword("REMARKS"), - keyword("COLUMN_DEF"), - field("SQL_DATA_TYPE", clientBasedType), - field("SQL_DATETIME_SUB", clientBasedType), - field("CHAR_OCTET_LENGTH", INTEGER), - field("ORDINAL_POSITION", INTEGER), - keyword("IS_NULLABLE"), - // JDBC specific - keyword("SCOPE_CATALOG"), - keyword("SCOPE_SCHEMA"), - keyword("SCOPE_TABLE"), - field("SOURCE_DATA_TYPE", SHORT), - keyword("IS_AUTOINCREMENT"), - keyword("IS_GENERATEDCOLUMN") - ); + return asList( + keyword("TABLE_CAT"), + keyword("TABLE_SCHEM"), + keyword("TABLE_NAME"), + keyword("COLUMN_NAME"), + field("DATA_TYPE", clientBasedType), + keyword("TYPE_NAME"), + field("COLUMN_SIZE", INTEGER), + field("BUFFER_LENGTH", INTEGER), + field("DECIMAL_DIGITS", clientBasedType), + field("NUM_PREC_RADIX", clientBasedType), + field("NULLABLE", clientBasedType), + keyword("REMARKS"), + keyword("COLUMN_DEF"), + field("SQL_DATA_TYPE", clientBasedType), + field("SQL_DATETIME_SUB", clientBasedType), + field("CHAR_OCTET_LENGTH", INTEGER), + field("ORDINAL_POSITION", INTEGER), + keyword("IS_NULLABLE"), + // JDBC specific + keyword("SCOPE_CATALOG"), + keyword("SCOPE_SCHEMA"), + keyword("SCOPE_TABLE"), + field("SOURCE_DATA_TYPE", SHORT), + keyword("IS_AUTOINCREMENT"), + keyword("IS_GENERATEDCOLUMN") + ); } @Override @@ -118,8 +119,9 @@ public void execute(SqlSession session, ActionListener listener) { String cluster = session.indexResolver().clusterName(); // save original index name (as the pattern can contain special chars) - String indexName = index != null ? index : - (pattern != null ? StringUtils.likeToUnescaped(pattern.pattern(), pattern.escape()) : ""); + String indexName = index != null + ? index + : (pattern != null ? StringUtils.likeToUnescaped(pattern.pattern(), pattern.escape()) : ""); String idx = index != null ? index : (pattern != null ? pattern.asIndexNameWildcard() : "*"); String regex = pattern != null ? pattern.asJavaRegex() : null; @@ -128,8 +130,14 @@ public void execute(SqlSession session, ActionListener listener) { // disallow double catalog specification, like: SYS COLUMNS CATALOG 'catA' TABLE LIKE 'catB:index_expression' if (isQualified(idx)) { - throw new IllegalArgumentException("illegal character [" + REMOTE_CLUSTER_INDEX_SEPARATOR + "] (the catalog delimiter) " + - "found in the table expression [" + idx + "]"); + throw new IllegalArgumentException( + "illegal character [" + + REMOTE_CLUSTER_INDEX_SEPARATOR + + "] (the catalog delimiter) " + + "found in the table expression [" + + idx + + "]" + ); } String indexPattern = idx; @@ -147,37 +155,51 @@ public void execute(SqlSession session, ActionListener listener) { // special case for '%' (translated to *) if ("*".equals(idx)) { - session.indexResolver().resolveAsSeparateMappings(indexPattern, regex, includeFrozen, emptyMap(), - ActionListener.wrap(esIndices -> { + session.indexResolver() + .resolveAsSeparateMappings(indexPattern, regex, includeFrozen, emptyMap(), ActionListener.wrap(esIndices -> { List> rows = new ArrayList<>(); for (EsIndex esIndex : esIndices) { fillInRows(tableCat, esIndex.name(), esIndex.mapping(), null, rows, columnMatcher, mode); } - listener.onResponse(ListCursor.of(Rows.schema(output), rows, session.configuration().pageSize())); - }, listener::onFailure)); + listener.onResponse(ListCursor.of(Rows.schema(output), rows, session.configuration().pageSize())); + }, listener::onFailure)); } // otherwise use a merged mapping else { - session.indexResolver().resolveAsMergedMapping(indexPattern, includeFrozen, emptyMap(), - ActionListener.wrap(r -> { - List> rows = new ArrayList<>(); - // populate the data only when a target is found - if (r.isValid()) { - EsIndex esIndex = r.get(); - fillInRows(tableCat, indexName, esIndex.mapping(), null, rows, columnMatcher, mode); - } + session.indexResolver().resolveAsMergedMapping(indexPattern, includeFrozen, emptyMap(), ActionListener.wrap(r -> { + List> rows = new ArrayList<>(); + // populate the data only when a target is found + if (r.isValid()) { + EsIndex esIndex = r.get(); + fillInRows(tableCat, indexName, esIndex.mapping(), null, rows, columnMatcher, mode); + } listener.onResponse(ListCursor.of(Rows.schema(output), rows, session.configuration().pageSize())); }, listener::onFailure)); } } - static void fillInRows(String clusterName, String indexName, Map mapping, String prefix, List> rows, - Pattern columnMatcher, Mode mode) { + static void fillInRows( + String clusterName, + String indexName, + Map mapping, + String prefix, + List> rows, + Pattern columnMatcher, + Mode mode + ) { fillInRows(clusterName, splitQualifiedIndex(indexName).v2(), mapping, prefix, rows, columnMatcher, Counter.newCounter(), mode); } - private static void fillInRows(String clusterName, String indexName, Map mapping, String prefix, List> rows, - Pattern columnMatcher, Counter position, Mode mode) { + private static void fillInRows( + String clusterName, + String indexName, + Map mapping, + String prefix, + List> rows, + Pattern columnMatcher, + Counter position, + Mode mode + ) { boolean isOdbcClient = mode == Mode.ODBC; for (Map.Entry entry : mapping.entrySet()) { position.addAndGet(1); // JDBC is 1-based so we start with 1 here @@ -190,7 +212,9 @@ private static void fillInRows(String clusterName, String indexName, Map info() { @Override public List output() { - return asList(keyword("TABLE_CAT"), - keyword("TABLE_SCHEM"), - keyword("TABLE_NAME"), - keyword("TABLE_TYPE"), - keyword("REMARKS"), - keyword("TYPE_CAT"), - keyword("TYPE_SCHEM"), - keyword("TYPE_NAME"), - keyword("SELF_REFERENCING_COL_NAME"), - keyword("REF_GENERATION") - ); + return asList( + keyword("TABLE_CAT"), + keyword("TABLE_SCHEM"), + keyword("TABLE_NAME"), + keyword("TABLE_TYPE"), + keyword("REMARKS"), + keyword("TYPE_CAT"), + keyword("TYPE_SCHEM"), + keyword("TYPE_NAME"), + keyword("SELF_REFERENCING_COL_NAME"), + keyword("REF_GENERATION") + ); } @Override @@ -100,8 +101,10 @@ public final void execute(SqlSession session, ActionListener listener) { if (types == null) { // empty string for catalog if (clusterPattern != null && clusterPattern.pattern().isEmpty() - // empty string for table like and no index specified - && pattern != null && pattern.pattern().isEmpty() && index == null) { + // empty string for table like and no index specified + && pattern != null + && pattern.pattern().isEmpty() + && index == null) { List> values = new ArrayList<>(); // send only the types, everything else is made of empty strings // NB: since the types are sent in SQL, frozen doesn't have to be taken into account since @@ -119,7 +122,6 @@ public final void execute(SqlSession session, ActionListener listener) { } } - // no enumeration pattern found, list actual tables String cRegex = clusterPattern != null ? clusterPattern.asIndexNameWildcard() : null; String idx = hasText(index) ? index : (pattern != null ? pattern.asIndexNameWildcard() : "*"); @@ -136,24 +138,30 @@ public final void execute(SqlSession session, ActionListener listener) { } } - session.indexResolver().resolveNames(cRegex, idx, regex, tableTypes, ActionListener.wrap(result -> listener.onResponse( - of(session, result.stream() - // sort by type, then by cluster and name - .sorted(Comparator. comparing(i -> i.type().toSql()) - .thenComparing(IndexInfo::cluster) - .thenComparing(IndexInfo::name)) - .map(t -> asList(t.cluster(), - null, - t.name(), - t.type().toSql(), - EMPTY, - null, - null, - null, - null, - null)) - .collect(toList()))) - , listener::onFailure)); + session.indexResolver() + .resolveNames( + cRegex, + idx, + regex, + tableTypes, + ActionListener.wrap( + result -> listener.onResponse( + of( + session, + result.stream() + // sort by type, then by cluster and name + .sorted( + Comparator.comparing(i -> i.type().toSql()) + .thenComparing(IndexInfo::cluster) + .thenComparing(IndexInfo::name) + ) + .map(t -> asList(t.cluster(), null, t.name(), t.type().toSql(), EMPTY, null, null, null, null, null)) + .collect(toList()) + ) + ), + listener::onFailure + ) + ); } @Override @@ -173,8 +181,8 @@ public boolean equals(Object obj) { SysTables other = (SysTables) obj; return Objects.equals(clusterPattern, other.clusterPattern) - && Objects.equals(index, other.index) - && Objects.equals(pattern, other.pattern) - && Objects.equals(types, other.types); + && Objects.equals(index, other.index) + && Objects.equals(pattern, other.pattern) + && Objects.equals(types, other.types); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/command/sys/SysTypes.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/command/sys/SysTypes.java index e99bff4f9e73f..1adf275767aaa 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/command/sys/SysTypes.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/command/sys/SysTypes.java @@ -57,27 +57,28 @@ protected NodeInfo info() { @Override public List output() { - return asList(keyword("TYPE_NAME"), - field("DATA_TYPE", INTEGER), - field("PRECISION",INTEGER), - keyword("LITERAL_PREFIX"), - keyword("LITERAL_SUFFIX"), - keyword("CREATE_PARAMS"), - field("NULLABLE", SHORT), - field("CASE_SENSITIVE", BOOLEAN), - field("SEARCHABLE", SHORT), - field("UNSIGNED_ATTRIBUTE", BOOLEAN), - field("FIXED_PREC_SCALE", BOOLEAN), - field("AUTO_INCREMENT", BOOLEAN), - keyword("LOCAL_TYPE_NAME"), - field("MINIMUM_SCALE", SHORT), - field("MAXIMUM_SCALE", SHORT), - field("SQL_DATA_TYPE", INTEGER), - field("SQL_DATETIME_SUB", INTEGER), - field("NUM_PREC_RADIX", INTEGER), - // ODBC - field("INTERVAL_PRECISION", INTEGER) - ); + return asList( + keyword("TYPE_NAME"), + field("DATA_TYPE", INTEGER), + field("PRECISION", INTEGER), + keyword("LITERAL_PREFIX"), + keyword("LITERAL_SUFFIX"), + keyword("CREATE_PARAMS"), + field("NULLABLE", SHORT), + field("CASE_SENSITIVE", BOOLEAN), + field("SEARCHABLE", SHORT), + field("UNSIGNED_ATTRIBUTE", BOOLEAN), + field("FIXED_PREC_SCALE", BOOLEAN), + field("AUTO_INCREMENT", BOOLEAN), + keyword("LOCAL_TYPE_NAME"), + field("MINIMUM_SCALE", SHORT), + field("MAXIMUM_SCALE", SHORT), + field("SQL_DATA_TYPE", INTEGER), + field("SQL_DATETIME_SUB", INTEGER), + field("NUM_PREC_RADIX", INTEGER), + // ODBC + field("INTERVAL_PRECISION", INTEGER) + ); } @Override @@ -87,35 +88,42 @@ public final void execute(SqlSession session, ActionListener listener) { values = values.filter(t -> type.equals(sqlType(t).getVendorTypeNumber())); } List> rows = values - // sort by SQL int type (that's what the JDBC/ODBC specs want) followed by name - .sorted(Comparator.comparing((DataType t) -> sqlType(t).getVendorTypeNumber()) - .thenComparing((DataType t) -> sqlType(t).getName())) - .map(t -> asList(t.toString(), - sqlType(t).getVendorTypeNumber(), precision(t), - "'", - "'", - null, - // don't be specific on nullable - DatabaseMetaData.typeNullableUnknown, - // all strings are case-sensitive - isString(t), - // everything is searchable, - DatabaseMetaData.typeSearchable, - // only numerics are signed - isSigned(t) == false, - //no fixed precision scale SQL_FALSE - Boolean.FALSE, - // not auto-incremented - Boolean.FALSE, - null, - metaSqlMinimumScale(t), metaSqlMaximumScale(t), - // SQL_DATA_TYPE - ODBC wants this to be not null - metaSqlDataType(t), metaSqlDateTimeSub(t), - // Radix - metaSqlRadix(t), - null - )) - .collect(toList()); + // sort by SQL int type (that's what the JDBC/ODBC specs want) followed by name + .sorted( + Comparator.comparing((DataType t) -> sqlType(t).getVendorTypeNumber()).thenComparing((DataType t) -> sqlType(t).getName()) + ) + .map( + t -> asList( + t.toString(), + sqlType(t).getVendorTypeNumber(), + precision(t), + "'", + "'", + null, + // don't be specific on nullable + DatabaseMetaData.typeNullableUnknown, + // all strings are case-sensitive + isString(t), + // everything is searchable, + DatabaseMetaData.typeSearchable, + // only numerics are signed + isSigned(t) == false, + // no fixed precision scale SQL_FALSE + Boolean.FALSE, + // not auto-incremented + Boolean.FALSE, + null, + metaSqlMinimumScale(t), + metaSqlMaximumScale(t), + // SQL_DATA_TYPE - ODBC wants this to be not null + metaSqlDataType(t), + metaSqlDateTimeSub(t), + // Radix + metaSqlRadix(t), + null + ) + ) + .collect(toList()); listener.onResponse(of(session, rows)); } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/physical/AggregateExec.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/physical/AggregateExec.java index c1cf2e2ade36f..94814901a29e9 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/physical/AggregateExec.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/physical/AggregateExec.java @@ -21,8 +21,12 @@ public class AggregateExec extends UnaryExec implements Unexecutable { private final List groupings; private final List aggregates; - public AggregateExec(Source source, PhysicalPlan child, - List groupings, List aggregates) { + public AggregateExec( + Source source, + PhysicalPlan child, + List groupings, + List aggregates + ) { super(source, child); this.groupings = groupings; this.aggregates = aggregates; @@ -68,7 +72,7 @@ public boolean equals(Object obj) { AggregateExec other = (AggregateExec) obj; return Objects.equals(groupings, other.groupings) - && Objects.equals(aggregates, other.aggregates) - && Objects.equals(child(), other.child()); + && Objects.equals(aggregates, other.aggregates) + && Objects.equals(child(), other.child()); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/physical/BinaryExec.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/physical/BinaryExec.java index d86fbf4171d66..476a4d5ad1a6a 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/physical/BinaryExec.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/physical/BinaryExec.java @@ -26,6 +26,7 @@ protected BinaryExec(Source source, PhysicalPlan left, PhysicalPlan right) { public final BinaryExec replaceChildren(List newChildren) { return replaceChildren(newChildren.get(0), newChildren.get(1)); } + protected abstract BinaryExec replaceChildren(PhysicalPlan newLeft, PhysicalPlan newRight); public PhysicalPlan left() { @@ -52,7 +53,6 @@ public boolean equals(Object obj) { } BinaryExec other = (BinaryExec) obj; - return Objects.equals(left, other.left) - && Objects.equals(right, other.right); + return Objects.equals(left, other.left) && Objects.equals(right, other.right); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/physical/EsQueryExec.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/physical/EsQueryExec.java index 2fb83b76442a8..aa7635f29c74a 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/physical/EsQueryExec.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/physical/EsQueryExec.java @@ -77,8 +77,8 @@ public boolean equals(Object obj) { EsQueryExec other = (EsQueryExec) obj; return Objects.equals(index, other.index) - && Objects.equals(queryContainer, other.queryContainer) - && Objects.equals(output, other.output); + && Objects.equals(queryContainer, other.queryContainer) + && Objects.equals(output, other.output); } @Override diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/physical/FilterExec.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/physical/FilterExec.java index 0e715f8586da9..76201730aa4a5 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/physical/FilterExec.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/physical/FilterExec.java @@ -70,7 +70,6 @@ public boolean equals(Object obj) { } FilterExec other = (FilterExec) obj; - return Objects.equals(condition, other.condition) - && Objects.equals(child(), other.child()); + return Objects.equals(condition, other.condition) && Objects.equals(child(), other.child()); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/physical/LimitExec.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/physical/LimitExec.java index 3d6c085f38d5b..549a1bbc4bd03 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/physical/LimitExec.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/physical/LimitExec.java @@ -51,7 +51,6 @@ public boolean equals(Object obj) { } LimitExec other = (LimitExec) obj; - return Objects.equals(limit, other.limit) - && Objects.equals(child(), other.child()); + return Objects.equals(limit, other.limit) && Objects.equals(child(), other.child()); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/physical/OrderExec.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/physical/OrderExec.java index 41b0421659b0a..3ad4453ae4c1f 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/physical/OrderExec.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/physical/OrderExec.java @@ -52,7 +52,6 @@ public boolean equals(Object obj) { OrderExec other = (OrderExec) obj; - return Objects.equals(order, other.order) - && Objects.equals(child(), other.child()); + return Objects.equals(order, other.order) && Objects.equals(child(), other.child()); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/physical/PivotExec.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/physical/PivotExec.java index c1059fbbd46b9..867ba0772ca3a 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/physical/PivotExec.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/physical/PivotExec.java @@ -58,7 +58,6 @@ public boolean equals(Object obj) { PivotExec other = (PivotExec) obj; - return Objects.equals(pivot, other.pivot) - && Objects.equals(child(), other.child()); + return Objects.equals(pivot, other.pivot) && Objects.equals(child(), other.child()); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/physical/ProjectExec.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/physical/ProjectExec.java index d540d82323d7c..8f6e5728216cc 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/physical/ProjectExec.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/physical/ProjectExec.java @@ -59,7 +59,6 @@ public boolean equals(Object obj) { ProjectExec other = (ProjectExec) obj; - return Objects.equals(projections, other.projections) - && Objects.equals(child(), other.child()); + return Objects.equals(projections, other.projections) && Objects.equals(child(), other.child()); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/physical/Unexecutable.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/physical/Unexecutable.java index 40f031ba1d0a2..0767c7c663d2c 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/physical/Unexecutable.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/physical/Unexecutable.java @@ -12,7 +12,6 @@ import org.elasticsearch.xpack.sql.session.Executable; import org.elasticsearch.xpack.sql.session.Session; - // this is mainly a marker interface to validate a plan before being executed public interface Unexecutable extends Executable { diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/planner/Mapper.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/planner/Mapper.java index 9144bcae6d745..ea82b7e2631de 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/planner/Mapper.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/planner/Mapper.java @@ -46,10 +46,7 @@ public PhysicalPlan map(LogicalPlan plan) { @Override protected Iterable.Batch> batches() { - Batch conversion = new Batch("Mapping", - new JoinMapper(), - new SimpleExecMapper() - ); + Batch conversion = new Batch("Mapping", new JoinMapper(), new SimpleExecMapper()); return Arrays.asList(conversion); } @@ -127,12 +124,11 @@ protected PhysicalPlan map(Join j) { } private PhysicalPlan join(Join join) { - //TODO: pick up on nested/parent-child docs + // TODO: pick up on nested/parent-child docs // 2. Hash? // 3. Cartesian // 3. Fallback to nested loop - throw new UnsupportedOperationException("Don't know how to handle join " + join.nodeString()); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/planner/QueryFolder.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/planner/QueryFolder.java index 01face7be8e22..cb3dd18f3267b 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/planner/QueryFolder.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/planner/QueryFolder.java @@ -115,23 +115,19 @@ PhysicalPlan fold(PhysicalPlan plan) { @Override protected Iterable.Batch> batches() { - Batch rollup = new Batch("Fold queries", - new FoldPivot(), - new FoldAggregate(), - new FoldProject(), - new FoldFilter(), - new FoldOrderBy(), - new FoldLimit() - ); + Batch rollup = new Batch( + "Fold queries", + new FoldPivot(), + new FoldAggregate(), + new FoldProject(), + new FoldFilter(), + new FoldOrderBy(), + new FoldLimit() + ); - Batch local = new Batch("Local queries", - new LocalLimit(), - new PropagateEmptyLocal() - ); + Batch local = new Batch("Local queries", new LocalLimit(), new PropagateEmptyLocal()); - Batch finish = new Batch("Finish query", Limiter.ONCE, - new PlanOutputToQueryRef() - ); + Batch finish = new Batch("Finish query", Limiter.ONCE, new PlanOutputToQueryRef()); return Arrays.asList(rollup, local, finish); } @@ -162,15 +158,19 @@ protected PhysicalPlan rule(ProjectExec project) { } } - QueryContainer clone = new QueryContainer(queryC.query(), queryC.aggs(), queryC.fields(), - aliases.build(), - queryC.pseudoFunctions(), - processors.build(), - queryC.sort(), - queryC.limit(), - queryC.shouldTrackHits(), - queryC.shouldIncludeFrozen(), - queryC.minPageSize()); + QueryContainer clone = new QueryContainer( + queryC.query(), + queryC.aggs(), + queryC.fields(), + aliases.build(), + queryC.pseudoFunctions(), + processors.build(), + queryC.sort(), + queryC.limit(), + queryC.shouldTrackHits(), + queryC.shouldIncludeFrozen(), + queryC.minPageSize() + ); return new EsQueryExec(exec.source(), exec.index(), project.output(), clone); } return project; @@ -193,15 +193,19 @@ protected PhysicalPlan rule(FilterExec plan) { } Aggs aggs = addPipelineAggs(qContainer, qt, plan); - qContainer = new QueryContainer(query, aggs, qContainer.fields(), - qContainer.aliases(), - qContainer.pseudoFunctions(), - qContainer.scalarFunctions(), - qContainer.sort(), - qContainer.limit(), - qContainer.shouldTrackHits(), - qContainer.shouldIncludeFrozen(), - qContainer.minPageSize()); + qContainer = new QueryContainer( + query, + aggs, + qContainer.fields(), + qContainer.aliases(), + qContainer.pseudoFunctions(), + qContainer.scalarFunctions(), + qContainer.sort(), + qContainer.limit(), + qContainer.shouldTrackHits(), + qContainer.shouldIncludeFrozen(), + qContainer.minPageSize() + ); return exec.with(qContainer); } @@ -214,8 +218,7 @@ private Aggs addPipelineAggs(QueryContainer qContainer, QueryTranslation qt, Fil if (filter == null) { return qContainer.aggs(); - } - else { + } else { aggs = aggs.addAgg(filter); } @@ -331,8 +334,8 @@ else if (exp instanceof GroupingFunction) { Object value = h.interval().value(); // interval of exactly 1 year or 1 month - if (value instanceof IntervalYearMonth && - (((IntervalYearMonth) value).interval().equals(Period.ofYears(1)) + if (value instanceof IntervalYearMonth + && (((IntervalYearMonth) value).interval().equals(Period.ofYears(1)) || ((IntervalYearMonth) value).interval().equals(Period.ofMonths(1)))) { Period yearMonth = ((IntervalYearMonth) value).interval(); String calendarInterval = yearMonth.equals(Period.ofYears(1)) ? YEAR_INTERVAL : MONTH_INTERVAL; @@ -347,15 +350,15 @@ else if (exp instanceof GroupingFunction) { } // interval of exactly 1 day else if (value instanceof IntervalDayTime - && ((IntervalDayTime) value).interval().equals(Duration.ofDays(1))) { - // When the histogram is `INTERVAL '1' DAY` the interval used in - // the ES date_histogram will be a calendar_interval with value "1d" - if (field instanceof FieldAttribute) { - key = new GroupByDateHistogram(aggId, QueryTranslator.nameOf(field), DAY_INTERVAL, h.zoneId()); - } else if (field instanceof Function) { - key = new GroupByDateHistogram(aggId, ((Function) field).asScript(), DAY_INTERVAL, h.zoneId()); + && ((IntervalDayTime) value).interval().equals(Duration.ofDays(1))) { + // When the histogram is `INTERVAL '1' DAY` the interval used in + // the ES date_histogram will be a calendar_interval with value "1d" + if (field instanceof FieldAttribute) { + key = new GroupByDateHistogram(aggId, QueryTranslator.nameOf(field), DAY_INTERVAL, h.zoneId()); + } else if (field instanceof Function) { + key = new GroupByDateHistogram(aggId, ((Function) field).asScript(), DAY_INTERVAL, h.zoneId()); + } } - } // All other intervals will be fixed_intervals expressed in ms. else { long intervalAsMillis = Intervals.inMillis(h.interval()); @@ -377,8 +380,10 @@ else if (value instanceof IntervalDayTime // numeric histogram else { if (field instanceof FieldAttribute || field instanceof Function) { - Double interval = (Double) SqlDataTypeConverter.convert(Foldables.valueOf(h.interval()), - DataTypes.DOUBLE); + Double interval = (Double) SqlDataTypeConverter.convert( + Foldables.valueOf(h.interval()), + DataTypes.DOUBLE + ); if (field instanceof FieldAttribute) { key = new GroupByNumericHistogram(aggId, QueryTranslator.nameOf(field), interval); } else { @@ -437,7 +442,6 @@ static EsQueryExec fold(AggregateExec a, EsQueryExec exec) { queryC = queryC.withAliases(aliases.build()); } - // build the group aggregation // NB: any reference in grouping is already "optimized" by its source so there's no need to look for aliases GroupingContext groupingContext = groupBy(a.groupings()); @@ -455,19 +459,19 @@ static EsQueryExec fold(AggregateExec a, EsQueryExec exec) { // unwrap alias (since we support aliases declared inside SELECTs to be used by the GROUP BY) // An alias can point to : // - field - // SELECT emp_no AS e ... GROUP BY e + // SELECT emp_no AS e ... GROUP BY e // - a function - // SELECT YEAR(hire_date) ... GROUP BY YEAR(hire_date) + // SELECT YEAR(hire_date) ... GROUP BY YEAR(hire_date) // - an agg function over the grouped field - // SELECT COUNT(*), AVG(salary) ... GROUP BY salary; + // SELECT COUNT(*), AVG(salary) ... GROUP BY salary; // - a scalar function, which can be applied on a column or aggregate and can require one or multiple inputs - // SELECT SIN(emp_no) ... GROUP BY emp_no - // SELECT CAST(YEAR(hire_date)) ... GROUP BY YEAR(hire_date) - // SELECT CAST(AVG(salary)) ... GROUP BY salary - // SELECT AVG(salary) + SIN(MIN(salary)) ... GROUP BY salary + // SELECT SIN(emp_no) ... GROUP BY emp_no + // SELECT CAST(YEAR(hire_date)) ... GROUP BY YEAR(hire_date) + // SELECT CAST(AVG(salary)) ... GROUP BY salary + // SELECT AVG(salary) + SIN(MIN(salary)) ... GROUP BY salary Expression target = ne; @@ -519,8 +523,10 @@ else if (target instanceof Function) { // (otherwise it is the opposite of grouping) // normally this case should be caught by the Verifier if (exp instanceof ScalarFunction) { - throw new FoldingException(exp, - "Scalar function " + exp.toString() + " can be used only if included already in grouping"); + throw new FoldingException( + exp, + "Scalar function " + exp.toString() + " can be used only if included already in grouping" + ); } } @@ -539,15 +545,23 @@ else if (target instanceof Function) { action = ((UnaryPipe) p).action(); isDateBased = true; } - return new AggPathInput(exp.source(), exp, new GroupByRef(matchingGroup.id(), null, isDateBased), - action); + return new AggPathInput( + exp.source(), + exp, + new GroupByRef(matchingGroup.id(), null, isDateBased), + action + ); } } // or found an aggregate expression (which has to work on an attribute used for grouping) // (can happen when dealing with a root group) if (Functions.isAggregate(exp)) { - Tuple withFunction = addAggFunction(matchingGroup, (AggregateFunction) exp, - compoundAggMap, qC.get()); + Tuple withFunction = addAggFunction( + matchingGroup, + (AggregateFunction) exp, + compoundAggMap, + qC.get() + ); qC.set(withFunction.v1()); return withFunction.v2(); } @@ -564,7 +578,7 @@ else if (target instanceof Function) { } // apply the same logic above (for function inputs) to non-scalar functions with small variations: - // instead of adding things as input, add them as full blown column + // instead of adding things as input, add them as full blown column else { GroupByKey matchingGroup = null; if (groupingContext != null) { @@ -587,8 +601,11 @@ else if (target.foldable()) { // fallback to regular agg functions else { // the only thing left is agg function - Check.isTrue(Functions.isAggregate(target), "Expected aggregate function inside alias; got [{}]", - target.nodeString()); + Check.isTrue( + Functions.isAggregate(target), + "Expected aggregate function inside alias; got [{}]", + target.nodeString() + ); AggregateFunction af = (AggregateFunction) target; Tuple withAgg = addAggFunction(matchingGroup, af, compoundAggMap, queryC); // make sure to add the inner id (to handle compound aggs) @@ -606,8 +623,7 @@ else if (target.foldable()) { matchingGroup = groupingContext.groupFor(target); Check.notNull(matchingGroup, "Cannot find group [{}]", Expressions.name(ne)); - queryC = queryC.addColumn( - new GroupByRef(matchingGroup.id(), null, isDateBased(ne.dataType())), id); + queryC = queryC.addColumn(new GroupByRef(matchingGroup.id(), null, isDateBased(ne.dataType())), id); } // fallback else { @@ -626,8 +642,12 @@ else if (target.foldable()) { return new EsQueryExec(exec.source(), exec.index(), a.output(), queryC); } - private static Tuple addAggFunction(GroupByKey groupingAgg, AggregateFunction f, - Map compoundAggMap, QueryContainer queryC) { + private static Tuple addAggFunction( + GroupByKey groupingAgg, + AggregateFunction f, + Map compoundAggMap, + QueryContainer queryC + ) { String functionId = Expressions.id(f); // handle count as a special case agg @@ -648,7 +668,7 @@ private static Tuple addAggFunction(GroupByKey gro Map pseudoFunctions = new LinkedHashMap<>(queryC.pseudoFunctions()); pseudoFunctions.put(functionId, groupingAgg); return new Tuple<>(queryC.withPseudoFunctions(pseudoFunctions), new AggPathInput(f, ref)); - // COUNT() + // COUNT() } else if (c.distinct() == false) { LeafAgg leafAgg = toAgg(functionId, f); AggPathInput a = new AggPathInput(f, new MetricAggRef(leafAgg.id(), "doc_count", "_count", null)); @@ -676,12 +696,16 @@ private static Tuple addAggFunction(GroupByKey gro // FIXME: concern leak - hack around MatrixAgg which is not // generalized (afaik) - aggInput = new AggPathInput(f, - new MetricAggRef(cAggPath, ia.innerName(), - ia.innerKey() != null ? QueryTranslator.nameOf(ia.innerKey()) : null, - ia.dataType())); - } - else { + aggInput = new AggPathInput( + f, + new MetricAggRef( + cAggPath, + ia.innerName(), + ia.innerKey() != null ? QueryTranslator.nameOf(ia.innerKey()) : null, + ia.dataType() + ) + ); + } else { LeafAgg leafAgg = toAgg(functionId, f); if (f instanceof TopHits) { aggInput = new AggPathInput(f, new TopHitsAggRef(leafAgg.id(), f.dataType())); @@ -729,8 +753,10 @@ protected PhysicalPlan rule(OrderExec plan) { // field if (orderExpression instanceof FieldAttribute) { - qContainer = qContainer.prependSort(lookup, - new AttributeSort((FieldAttribute) orderExpression, direction, missing)); + qContainer = qContainer.prependSort( + lookup, + new AttributeSort((FieldAttribute) orderExpression, direction, missing) + ); } // scalar functions typically require script ordering else if (orderExpression instanceof ScalarFunction) { @@ -748,8 +774,10 @@ else if (orderExpression instanceof Score) { } // agg function else if (orderExpression instanceof AggregateFunction) { - qContainer = qContainer.prependSort(lookup, - new AggregateSort((AggregateFunction) orderExpression, direction, missing)); + qContainer = qContainer.prependSort( + lookup, + new AggregateSort((AggregateFunction) orderExpression, direction, missing) + ); } // unknown else { @@ -763,7 +791,6 @@ else if (orderExpression instanceof AggregateFunction) { } } - private static class FoldLimit extends FoldingRule { @Override @@ -798,7 +825,6 @@ protected PhysicalPlan rule(EsQueryExec exec) { } } - private static class FoldPivot extends FoldingRule { @Override @@ -806,9 +832,10 @@ protected PhysicalPlan rule(PivotExec plan) { if (plan.child() instanceof EsQueryExec) { EsQueryExec exec = (EsQueryExec) plan.child(); Pivot p = plan.pivot(); - EsQueryExec fold = FoldAggregate - .fold(new AggregateExec(plan.source(), exec, - new ArrayList<>(p.groupingSet()), combine(p.groupingSet(), p.aggregates())), exec); + EsQueryExec fold = FoldAggregate.fold( + new AggregateExec(plan.source(), exec, new ArrayList<>(p.groupingSet()), combine(p.groupingSet(), p.aggregates())), + exec + ); // replace the aggregate extractors with pivot specific extractors // these require a reference to the pivoting column in order to compare the value @@ -824,13 +851,20 @@ protected PhysicalPlan rule(PivotExec plan) { for (int i = startingIndex; i < fields.size(); i++) { Tuple tuple = fields.remove(i); for (Map.Entry entry : values.entrySet()) { - fields.add(new Tuple<>( - new PivotColumnRef(groupTuple.v1(), tuple.v1(), entry.getValue().value()), Expressions.id(entry.getKey()))); + fields.add( + new Tuple<>( + new PivotColumnRef(groupTuple.v1(), tuple.v1(), entry.getValue().value()), + Expressions.id(entry.getKey()) + ) + ); } i += values.size(); } - return fold.with(new QueryContainer(query.query(), query.aggs(), + return fold.with( + new QueryContainer( + query.query(), + query.aggs(), fields, query.aliases(), query.pseudoFunctions(), @@ -839,7 +873,9 @@ protected PhysicalPlan rule(PivotExec plan) { query.limit(), query.shouldTrackHits(), query.shouldIncludeFrozen(), - values.size())); + values.size() + ) + ); } return plan; } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/planner/QueryTranslator.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/planner/QueryTranslator.java index 8cf7e0ae7ca53..2d17f356bddc0 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/planner/QueryTranslator.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/planner/QueryTranslator.java @@ -94,36 +94,36 @@ final class QueryTranslator { private QueryTranslator() {} private static final List> QUERY_TRANSLATORS = Arrays.asList( - new BinaryComparisons(), - new InComparisons(), - new Ranges(), - new BinaryLogic(), - new Nots(), - new IsNullTranslator(), - new IsNotNullTranslator(), - new Likes(), - new StringQueries(), - new Matches(), - new MultiMatches(), - new Scalars() - ); + new BinaryComparisons(), + new InComparisons(), + new Ranges(), + new BinaryLogic(), + new Nots(), + new IsNullTranslator(), + new IsNotNullTranslator(), + new Likes(), + new StringQueries(), + new Matches(), + new MultiMatches(), + new Scalars() + ); private static final List> AGG_TRANSLATORS = Arrays.asList( - new Maxes(), - new Mins(), - new Avgs(), - new Sums(), - new StatsAggs(), - new ExtendedStatsAggs(), - new MatrixStatsAggs(), - new PercentilesAggs(), - new PercentileRanksAggs(), - new CountAggs(), - new DateTimes(), - new Firsts(), - new Lasts(), - new MADs() - ); + new Maxes(), + new Mins(), + new Avgs(), + new Sums(), + new StatsAggs(), + new ExtendedStatsAggs(), + new MatrixStatsAggs(), + new PercentilesAggs(), + new PercentileRanksAggs(), + new CountAggs(), + new DateTimes(), + new Firsts(), + new Lasts(), + new MADs() + ); static class QueryTranslation { final Query query; @@ -183,11 +183,9 @@ static QueryTranslation and(Source source, QueryTranslation left, QueryTranslati if (left.aggFilter == null) { aggFilter = right.aggFilter; - } - else if (right.aggFilter == null) { + } else if (right.aggFilter == null) { aggFilter = left.aggFilter; - } - else { + } else { aggFilter = new AndAggFilter(left.aggFilter, right.aggFilter); } @@ -212,11 +210,9 @@ static QueryTranslation or(Source source, QueryTranslation left, QueryTranslatio if (left.aggFilter == null) { aggFilter = right.aggFilter; - } - else if (right.aggFilter == null) { + } else if (right.aggFilter == null) { aggFilter = left.aggFilter; - } - else { + } else { aggFilter = new OrAggFilter(left.aggFilter, right.aggFilter); } @@ -229,8 +225,7 @@ static String nameOf(Expression e) { } if (e instanceof NamedExpression) { return ((NamedExpression) e).name(); - } - else { + } else { return e.sourceText(); } } @@ -248,8 +243,11 @@ static String field(AggregateFunction af, Expression arg) { } return field.name(); } - throw new SqlIllegalArgumentException("Does not know how to convert argument {} for function {}", arg.nodeString(), - af.nodeString()); + throw new SqlIllegalArgumentException( + "Does not know how to convert argument {} for function {}", + arg.nodeString(), + af.nodeString() + ); } private static boolean isFieldOrLiteral(Expression e) { @@ -308,8 +306,11 @@ protected QueryTranslation asQuery(MultiMatchQueryPredicate q, boolean onAggs, T static class BinaryLogic extends SqlExpressionTranslator { @Override - protected QueryTranslation asQuery(org.elasticsearch.xpack.ql.expression.predicate.logical.BinaryLogic e, boolean onAggs, - TranslatorHandler handler) { + protected QueryTranslation asQuery( + org.elasticsearch.xpack.ql.expression.predicate.logical.BinaryLogic e, + boolean onAggs, + TranslatorHandler handler + ) { if (e instanceof And) { return and(e.source(), toQuery(e.left(), onAggs), toQuery(e.right(), onAggs)); } @@ -399,7 +400,7 @@ private static Query translateQuery(BinaryComparison bc, TranslatorHandler handl // Possible geo optimization if (bc.left() instanceof StDistance && value instanceof Number) { - if (bc instanceof LessThan || bc instanceof LessThanOrEqual) { + if (bc instanceof LessThan || bc instanceof LessThanOrEqual) { // Special case for ST_Distance translatable into geo_distance query StDistance stDistance = (StDistance) bc.left(); if (stDistance.left() instanceof FieldAttribute && stDistance.right().foldable()) { @@ -408,8 +409,13 @@ private static Query translateQuery(BinaryComparison bc, TranslatorHandler handl Geometry geometry = ((GeoShape) geoShape).toGeometry(); if (geometry instanceof Point) { String field = nameOf(stDistance.left()); - Query query = new GeoDistanceQuery(source, field, ((Number) value).doubleValue(), - ((Point) geometry).getY(), ((Point) geometry).getX()); + Query query = new GeoDistanceQuery( + source, + field, + ((Number) value).doubleValue(), + ((Point) geometry).getY(), + ((Point) geometry).getX() + ); return ExpressionTranslator.wrapIfNested(query, stDistance.left()); } } @@ -435,8 +441,7 @@ protected QueryTranslation asQuery(In in, boolean onAggs, TranslatorHandler hand // if (onAggs) { aggFilter = new AggFilter(id(in.value()), in.asScript()); - } - else { + } else { query = org.elasticsearch.xpack.ql.planner.ExpressionTranslators.InComparisons.doTranslate(in, handler); } return new QueryTranslation(query, aggFilter); @@ -482,7 +487,6 @@ protected QueryTranslation asQuery(ScalarFunction f, boolean onAggs, TranslatorH } } - abstract static class SqlExpressionTranslator { private final Class typeToken = ReflectionUtils.detectSuperTypeForRuleLike(getClass()); diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/RestSqlClearCursorAction.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/RestSqlClearCursorAction.java index 2130aa052958c..d1e23bef499f6 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/RestSqlClearCursorAction.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/RestSqlClearCursorAction.java @@ -8,10 +8,10 @@ package org.elasticsearch.xpack.sql.plugin; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestToXContentListener; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.sql.action.SqlClearCursorAction; import org.elasticsearch.xpack.sql.action.SqlClearCursorRequest; import org.elasticsearch.xpack.sql.proto.Protocol; @@ -29,8 +29,7 @@ public List routes() { } @Override - protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) - throws IOException { + protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException { SqlClearCursorRequest sqlRequest; try (XContentParser parser = request.contentParser()) { sqlRequest = SqlClearCursorRequest.fromXContent(parser); diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/RestSqlQueryAction.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/RestSqlQueryAction.java index de089729bcc97..2303304acf7e7 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/RestSqlQueryAction.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/RestSqlQueryAction.java @@ -8,12 +8,12 @@ package org.elasticsearch.xpack.sql.plugin; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.MediaType; -import org.elasticsearch.xcontent.MediaTypeRegistry; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestCancellableNodeClient; +import org.elasticsearch.xcontent.MediaType; +import org.elasticsearch.xcontent.MediaTypeRegistry; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.sql.action.SqlQueryAction; import org.elasticsearch.xpack.sql.action.SqlQueryRequest; import org.elasticsearch.xpack.sql.proto.Protocol; @@ -31,9 +31,7 @@ public class RestSqlQueryAction extends BaseRestHandler { @Override public List routes() { - return List.of( - new Route(GET, Protocol.SQL_QUERY_REST_ENDPOINT), - new Route(POST, Protocol.SQL_QUERY_REST_ENDPOINT)); + return List.of(new Route(GET, Protocol.SQL_QUERY_REST_ENDPOINT), new Route(POST, Protocol.SQL_QUERY_REST_ENDPOINT)); } public MediaTypeRegistry validAcceptMediaTypes() { @@ -41,8 +39,7 @@ public MediaTypeRegistry validAcceptMediaTypes() { } @Override - protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) - throws IOException { + protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException { SqlQueryRequest sqlRequest; try (XContentParser parser = request.contentOrSourceParamParser()) { sqlRequest = SqlQueryRequest.fromXContent(parser); diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/RestSqlTranslateAction.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/RestSqlTranslateAction.java index c6c7f9dae1621..56fe46708fcaa 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/RestSqlTranslateAction.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/RestSqlTranslateAction.java @@ -7,10 +7,10 @@ package org.elasticsearch.xpack.sql.plugin; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestToXContentListener; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.sql.action.SqlTranslateAction; import org.elasticsearch.xpack.sql.action.SqlTranslateRequest; import org.elasticsearch.xpack.sql.proto.Protocol; @@ -28,14 +28,11 @@ public class RestSqlTranslateAction extends BaseRestHandler { @Override public List routes() { - return List.of( - new Route(GET, Protocol.SQL_TRANSLATE_REST_ENDPOINT), - new Route(POST, Protocol.SQL_TRANSLATE_REST_ENDPOINT)); + return List.of(new Route(GET, Protocol.SQL_TRANSLATE_REST_ENDPOINT), new Route(POST, Protocol.SQL_TRANSLATE_REST_ENDPOINT)); } @Override - protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) - throws IOException { + protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException { SqlTranslateRequest sqlRequest; try (XContentParser parser = request.contentOrSourceParamParser()) { sqlRequest = SqlTranslateRequest.fromXContent(parser); @@ -49,4 +46,3 @@ public String getName() { return "xpack_sql_translate_action"; } } - diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/SqlMediaTypeParser.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/SqlMediaTypeParser.java index a748948f10e77..dfb40d1aff05d 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/SqlMediaTypeParser.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/SqlMediaTypeParser.java @@ -7,11 +7,11 @@ package org.elasticsearch.xpack.sql.plugin; +import org.elasticsearch.rest.RestRequest; import org.elasticsearch.xcontent.MediaType; import org.elasticsearch.xcontent.MediaTypeRegistry; import org.elasticsearch.xcontent.ParsedMediaType; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.rest.RestRequest; import org.elasticsearch.xpack.sql.action.SqlQueryRequest; import org.elasticsearch.xpack.sql.proto.Mode; @@ -20,9 +20,9 @@ import static org.elasticsearch.xpack.sql.proto.Protocol.URL_PARAM_FORMAT; public class SqlMediaTypeParser { - public static final MediaTypeRegistry MEDIA_TYPE_REGISTRY = new MediaTypeRegistry<>() - .register(XContentType.values()) - .register(TextFormat.values()); + public static final MediaTypeRegistry MEDIA_TYPE_REGISTRY = new MediaTypeRegistry<>().register( + XContentType.values() + ).register(TextFormat.values()); /* * Since we support {@link TextFormat} and @@ -66,16 +66,22 @@ private static MediaType mediaTypeFromParams(RestRequest request) { private static MediaType validateColumnarRequest(boolean requestIsColumnar, MediaType fromMediaType, RestRequest request) { if (requestIsColumnar && fromMediaType instanceof TextFormat) { - throw new IllegalArgumentException("Invalid use of [columnar] argument: cannot be used in combination with " - + "txt, csv or tsv formats"); + throw new IllegalArgumentException( + "Invalid use of [columnar] argument: cannot be used in combination with " + "txt, csv or tsv formats" + ); } return checkNonNullMediaType(fromMediaType, request); } private static MediaType checkNonNullMediaType(MediaType mediaType, RestRequest request) { if (mediaType == null) { - String msg = String.format(Locale.ROOT, "Invalid request content type: Accept=[%s], Content-Type=[%s], format=[%s]", - request.header("Accept"), request.header("Content-Type"), request.param("format")); + String msg = String.format( + Locale.ROOT, + "Invalid request content type: Accept=[%s], Content-Type=[%s], format=[%s]", + request.header("Accept"), + request.header("Content-Type"), + request.param("format") + ); throw new IllegalArgumentException(msg); } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/SqlPlugin.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/SqlPlugin.java index 25cdc29ac82b5..a069c89b6df9a 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/SqlPlugin.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/SqlPlugin.java @@ -17,7 +17,6 @@ import org.elasticsearch.common.settings.IndexScopedSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsFilter; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.env.Environment; import org.elasticsearch.env.NodeEnvironment; import org.elasticsearch.license.License; @@ -32,6 +31,7 @@ import org.elasticsearch.script.ScriptService; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.watcher.ResourceWatcherService; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.XPackPlugin; import org.elasticsearch.xpack.core.action.XPackInfoFeatureAction; import org.elasticsearch.xpack.core.action.XPackUsageFeatureAction; @@ -55,42 +55,48 @@ public class SqlPlugin extends Plugin implements ActionPlugin { private final LicensedFeature.Momentary JDBC_FEATURE = LicensedFeature.momentary("sql", "jdbc", License.OperationMode.PLATINUM); private final LicensedFeature.Momentary ODBC_FEATURE = LicensedFeature.momentary("sql", "odbc", License.OperationMode.PLATINUM); - private final SqlLicenseChecker sqlLicenseChecker = new SqlLicenseChecker( - (mode) -> { - XPackLicenseState licenseState = getLicenseState(); - switch (mode) { - case JDBC: - if (JDBC_FEATURE.check(licenseState) == false) { - throw LicenseUtils.newComplianceException("jdbc"); - } - break; - case ODBC: - if (ODBC_FEATURE.check(licenseState) == false) { - throw LicenseUtils.newComplianceException("odbc"); - } - break; - case PLAIN: - case CLI: - break; - default: - throw new IllegalArgumentException("Unknown SQL mode " + mode); - } + private final SqlLicenseChecker sqlLicenseChecker = new SqlLicenseChecker((mode) -> { + XPackLicenseState licenseState = getLicenseState(); + switch (mode) { + case JDBC: + if (JDBC_FEATURE.check(licenseState) == false) { + throw LicenseUtils.newComplianceException("jdbc"); + } + break; + case ODBC: + if (ODBC_FEATURE.check(licenseState) == false) { + throw LicenseUtils.newComplianceException("odbc"); + } + break; + case PLAIN: + case CLI: + break; + default: + throw new IllegalArgumentException("Unknown SQL mode " + mode); } - ); + }); - public SqlPlugin(Settings settings) { - } + public SqlPlugin(Settings settings) {} // overridable by tests - protected XPackLicenseState getLicenseState() { return XPackPlugin.getSharedLicenseState(); } + protected XPackLicenseState getLicenseState() { + return XPackPlugin.getSharedLicenseState(); + } @Override - public Collection createComponents(Client client, ClusterService clusterService, ThreadPool threadPool, - ResourceWatcherService resourceWatcherService, ScriptService scriptService, - NamedXContentRegistry xContentRegistry, Environment environment, - NodeEnvironment nodeEnvironment, NamedWriteableRegistry namedWriteableRegistry, - IndexNameExpressionResolver expressionResolver, - Supplier repositoriesServiceSupplier) { + public Collection createComponents( + Client client, + ClusterService clusterService, + ThreadPool threadPool, + ResourceWatcherService resourceWatcherService, + ScriptService scriptService, + NamedXContentRegistry xContentRegistry, + Environment environment, + NodeEnvironment nodeEnvironment, + NamedWriteableRegistry namedWriteableRegistry, + IndexNameExpressionResolver expressionResolver, + Supplier repositoriesServiceSupplier + ) { return createComponents(client, environment.settings(), clusterService, namedWriteableRegistry); } @@ -98,27 +104,42 @@ public Collection createComponents(Client client, ClusterService cluster /** * Create components used by the sql plugin. */ - Collection createComponents(Client client, Settings settings, ClusterService clusterService, - NamedWriteableRegistry namedWriteableRegistry) { + Collection createComponents( + Client client, + Settings settings, + ClusterService clusterService, + NamedWriteableRegistry namedWriteableRegistry + ) { RemoteClusterResolver remoteClusterResolver = new RemoteClusterResolver(settings, clusterService.getClusterSettings()); - IndexResolver indexResolver = new IndexResolver(client, clusterService.getClusterName().value(), SqlDataTypeRegistry.INSTANCE, - remoteClusterResolver::remoteClusters); + IndexResolver indexResolver = new IndexResolver( + client, + clusterService.getClusterName().value(), + SqlDataTypeRegistry.INSTANCE, + remoteClusterResolver::remoteClusters + ); return Arrays.asList(sqlLicenseChecker, indexResolver, new PlanExecutor(client, indexResolver, namedWriteableRegistry)); } @Override - public List getRestHandlers(Settings settings, RestController restController, - ClusterSettings clusterSettings, IndexScopedSettings indexScopedSettings, - SettingsFilter settingsFilter, IndexNameExpressionResolver indexNameExpressionResolver, - Supplier nodesInCluster) { + public List getRestHandlers( + Settings settings, + RestController restController, + ClusterSettings clusterSettings, + IndexScopedSettings indexScopedSettings, + SettingsFilter settingsFilter, + IndexNameExpressionResolver indexNameExpressionResolver, + Supplier nodesInCluster + ) { - return Arrays.asList(new RestSqlQueryAction(), - new RestSqlTranslateAction(), - new RestSqlClearCursorAction(), - new RestSqlStatsAction(), - new RestSqlAsyncGetResultsAction(), - new RestSqlAsyncGetStatusAction(), - new RestSqlAsyncDeleteResultsAction()); + return Arrays.asList( + new RestSqlQueryAction(), + new RestSqlTranslateAction(), + new RestSqlClearCursorAction(), + new RestSqlStatsAction(), + new RestSqlAsyncGetResultsAction(), + new RestSqlAsyncGetStatusAction(), + new RestSqlAsyncDeleteResultsAction() + ); } @Override @@ -126,13 +147,15 @@ public List getRestHandlers(Settings settings, RestController restC var usageAction = new ActionHandler<>(XPackUsageFeatureAction.SQL, SqlUsageTransportAction.class); var infoAction = new ActionHandler<>(XPackInfoFeatureAction.SQL, SqlInfoTransportAction.class); - return Arrays.asList(new ActionHandler<>(SqlQueryAction.INSTANCE, TransportSqlQueryAction.class), - new ActionHandler<>(SqlTranslateAction.INSTANCE, TransportSqlTranslateAction.class), - new ActionHandler<>(SqlClearCursorAction.INSTANCE, TransportSqlClearCursorAction.class), - new ActionHandler<>(SqlStatsAction.INSTANCE, TransportSqlStatsAction.class), - new ActionHandler<>(SqlAsyncGetResultsAction.INSTANCE, TransportSqlAsyncGetResultsAction.class), - new ActionHandler<>(SqlAsyncGetStatusAction.INSTANCE, TransportSqlAsyncGetStatusAction.class), - usageAction, - infoAction); + return Arrays.asList( + new ActionHandler<>(SqlQueryAction.INSTANCE, TransportSqlQueryAction.class), + new ActionHandler<>(SqlTranslateAction.INSTANCE, TransportSqlTranslateAction.class), + new ActionHandler<>(SqlClearCursorAction.INSTANCE, TransportSqlClearCursorAction.class), + new ActionHandler<>(SqlStatsAction.INSTANCE, TransportSqlStatsAction.class), + new ActionHandler<>(SqlAsyncGetResultsAction.INSTANCE, TransportSqlAsyncGetResultsAction.class), + new ActionHandler<>(SqlAsyncGetStatusAction.INSTANCE, TransportSqlAsyncGetStatusAction.class), + usageAction, + infoAction + ); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/SqlResponseListener.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/SqlResponseListener.java index 4ac4019d74efc..1434ee1b68d75 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/SqlResponseListener.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/SqlResponseListener.java @@ -7,15 +7,15 @@ package org.elasticsearch.xpack.sql.plugin; -import org.elasticsearch.xcontent.MediaType; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestChannel; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestResponseListener; +import org.elasticsearch.xcontent.MediaType; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.sql.action.SqlQueryRequest; import org.elasticsearch.xpack.sql.action.SqlQueryResponse; @@ -35,7 +35,6 @@ class SqlResponseListener extends RestResponseListener { private final MediaType mediaType; private final RestRequest request; - SqlResponseListener(RestChannel channel, RestRequest request, SqlQueryRequest sqlRequest) { super(channel); this.request = request; @@ -49,8 +48,11 @@ class SqlResponseListener extends RestResponseListener { * parameter should only be checked for CSV, not always. */ if (mediaType != TextFormat.CSV && request.hasParam(URL_PARAM_DELIMITER)) { - String message = String.format(Locale.ROOT, "request [%s] contains unrecognized parameter: [" + URL_PARAM_DELIMITER + "]", - request.path()); + String message = String.format( + Locale.ROOT, + "request [%s] contains unrecognized parameter: [" + URL_PARAM_DELIMITER + "]", + request.path() + ); throw new IllegalArgumentException(message); } } @@ -75,8 +77,7 @@ public RestResponse buildResponse(SqlQueryResponse response) throws Exception { TextFormat type = (TextFormat) mediaType; final String data = type.format(request, response); - restResponse = new BytesRestResponse(RestStatus.OK, type.contentType(request), - data.getBytes(StandardCharsets.UTF_8)); + restResponse = new BytesRestResponse(RestStatus.OK, type.contentType(request), data.getBytes(StandardCharsets.UTF_8)); if (response.hasCursor()) { restResponse.addHeader(HEADER_NAME_CURSOR, response.cursor()); diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/TextFormat.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/TextFormat.java index 913efd6b04125..121c9d90a7e8d 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/TextFormat.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/TextFormat.java @@ -8,8 +8,8 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.core.Tuple; -import org.elasticsearch.xcontent.MediaType; import org.elasticsearch.rest.RestRequest; +import org.elasticsearch.xcontent.MediaType; import org.elasticsearch.xpack.ql.util.StringUtils; import org.elasticsearch.xpack.sql.SqlIllegalArgumentException; import org.elasticsearch.xpack.sql.action.BasicFormatter; @@ -107,10 +107,12 @@ protected String eol() { @Override public Set headerValues() { return Set.of( - new HeaderValue(CONTENT_TYPE_TXT, - Map.of("header", "present|absent")), - new HeaderValue(VENDOR_CONTENT_TYPE_TXT, - Map.of("header", "present|absent", COMPATIBLE_WITH_PARAMETER_NAME, VERSION_PATTERN))); + new HeaderValue(CONTENT_TYPE_TXT, Map.of("header", "present|absent")), + new HeaderValue( + VENDOR_CONTENT_TYPE_TXT, + Map.of("header", "present|absent", COMPATIBLE_WITH_PARAMETER_NAME, VERSION_PATTERN) + ) + ); } }, @@ -132,7 +134,7 @@ protected Character delimiter() { @Override protected String eol() { - //CRLF + // CRLF return "\r\n"; } @@ -148,8 +150,11 @@ String contentType() { @Override String contentType(RestRequest request) { - return contentType() + "; charset=utf-8; " + - URL_PARAM_HEADER + "=" + (hasHeader(request) ? PARAM_HEADER_PRESENT : PARAM_HEADER_ABSENT); + return contentType() + + "; charset=utf-8; " + + URL_PARAM_HEADER + + "=" + + (hasHeader(request) ? PARAM_HEADER_PRESENT : PARAM_HEADER_ABSENT); } @Override @@ -160,8 +165,9 @@ protected Character delimiter(RestRequest request) { } delimiterParam = URLDecoder.decode(delimiterParam, StandardCharsets.UTF_8); if (delimiterParam.length() != 1) { - throw new IllegalArgumentException("invalid " + - (delimiterParam.length() > 0 ? "multi-character" : "empty") + " delimiter [" + delimiterParam + "]"); + throw new IllegalArgumentException( + "invalid " + (delimiterParam.length() > 0 ? "multi-character" : "empty") + " delimiter [" + delimiterParam + "]" + ); } Character delimiter = delimiterParam.charAt(0); switch (delimiter) { @@ -170,8 +176,9 @@ protected Character delimiter(RestRequest request) { case '\r': throw new IllegalArgumentException("illegal reserved character specified as delimiter [" + delimiter + "]"); case '\t': - throw new IllegalArgumentException("illegal delimiter [TAB] specified as delimiter for the [csv] format; " + - "choose the [tsv] format instead"); + throw new IllegalArgumentException( + "illegal delimiter [TAB] specified as delimiter for the [csv] format; " + "choose the [tsv] format instead" + ); } return delimiter; } @@ -231,14 +238,16 @@ boolean hasHeader(RestRequest request) { @Override public Set headerValues() { return Set.of( - new HeaderValue(CONTENT_TYPE_CSV, - Map.of("header", "present|absent","delimiter", ".+")),// more detailed parsing is in TextFormat.CSV#delimiter - new HeaderValue(VENDOR_CONTENT_TYPE_CSV, - Map.of("header", "present|absent","delimiter", ".+", COMPATIBLE_WITH_PARAMETER_NAME, VERSION_PATTERN))); + new HeaderValue(CONTENT_TYPE_CSV, Map.of("header", "present|absent", "delimiter", ".+")),// more detailed parsing is in + // TextFormat.CSV#delimiter + new HeaderValue( + VENDOR_CONTENT_TYPE_CSV, + Map.of("header", "present|absent", "delimiter", ".+", COMPATIBLE_WITH_PARAMETER_NAME, VERSION_PATTERN) + ) + ); } }, - TSV() { @Override protected Character delimiter() { @@ -273,10 +282,10 @@ String maybeEscape(String value, Character __) { for (int i = 0; i < value.length(); i++) { char c = value.charAt(i); switch (c) { - case '\n' : + case '\n': sb.append("\\n"); break; - case '\t' : + case '\t': sb.append("\\t"); break; default: @@ -291,8 +300,11 @@ String maybeEscape(String value, Character __) { public Set headerValues() { return Set.of( new HeaderValue(CONTENT_TYPE_TSV, Map.of("header", "present|absent")), - new HeaderValue(VENDOR_CONTENT_TYPE_TSV, - Map.of("header", "present|absent", COMPATIBLE_WITH_PARAMETER_NAME, VERSION_PATTERN))); + new HeaderValue( + VENDOR_CONTENT_TYPE_TSV, + Map.of("header", "present|absent", COMPATIBLE_WITH_PARAMETER_NAME, VERSION_PATTERN) + ) + ); } }; @@ -318,8 +330,12 @@ String format(RestRequest request, SqlQueryResponse response) { } for (List row : response.rows()) { - row(sb, row, f -> f instanceof ZonedDateTime ? DateUtils.toString((ZonedDateTime) f) : Objects.toString(f, StringUtils.EMPTY), - delimiter(request)); + row( + sb, + row, + f -> f instanceof ZonedDateTime ? DateUtils.toString((ZonedDateTime) f) : Objects.toString(f, StringUtils.EMPTY), + delimiter(request) + ); } return sb.toString(); diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/TextFormatterCursor.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/TextFormatterCursor.java index f3dfaae95509a..03755612db6e2 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/TextFormatterCursor.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/TextFormatterCursor.java @@ -19,6 +19,7 @@ import java.util.Objects; import static org.elasticsearch.action.ActionListener.wrap; + /** * The cursor that wraps all necessary information for textual representation of the result table */ @@ -51,11 +52,10 @@ public BasicFormatter getFormatter() { @Override public void nextPage(SqlConfiguration cfg, Client client, NamedWriteableRegistry registry, ActionListener listener) { // keep wrapping the text formatter - delegate.nextPage(cfg, client, registry, - wrap(p -> { - Cursor next = p.next(); - listener.onResponse(next == Cursor.EMPTY ? p : new Page(p.rowSet(), new TextFormatterCursor(next, formatter))); - }, listener::onFailure)); + delegate.nextPage(cfg, client, registry, wrap(p -> { + Cursor next = p.next(); + listener.onResponse(next == Cursor.EMPTY ? p : new Page(p.rowSet(), new TextFormatterCursor(next, formatter))); + }, listener::onFailure)); } @Override @@ -77,8 +77,7 @@ public boolean equals(Object o) { return false; } TextFormatterCursor that = (TextFormatterCursor) o; - return Objects.equals(delegate, that.delegate) && - Objects.equals(formatter, that.formatter); + return Objects.equals(delegate, that.delegate) && Objects.equals(formatter, that.formatter); } @Override diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/TransportSqlAsyncGetResultsAction.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/TransportSqlAsyncGetResultsAction.java index d647298a8f888..568f5bcdc92fe 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/TransportSqlAsyncGetResultsAction.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/TransportSqlAsyncGetResultsAction.java @@ -22,15 +22,26 @@ public class TransportSqlAsyncGetResultsAction extends AbstractTransportQlAsyncGetResultsAction { @Inject - public TransportSqlAsyncGetResultsAction(TransportService transportService, - ActionFilters actionFilters, - ClusterService clusterService, - NamedWriteableRegistry registry, - Client client, - ThreadPool threadPool, - BigArrays bigArrays) { - super(SqlAsyncGetResultsAction.NAME, transportService, actionFilters, clusterService, registry, client, threadPool, bigArrays, - SqlQueryTask.class); + public TransportSqlAsyncGetResultsAction( + TransportService transportService, + ActionFilters actionFilters, + ClusterService clusterService, + NamedWriteableRegistry registry, + Client client, + ThreadPool threadPool, + BigArrays bigArrays + ) { + super( + SqlAsyncGetResultsAction.NAME, + transportService, + actionFilters, + clusterService, + registry, + client, + threadPool, + bigArrays, + SqlQueryTask.class + ); } @Override diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/TransportSqlAsyncGetStatusAction.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/TransportSqlAsyncGetStatusAction.java index fa9c8fbc5cd6b..591adadf30722 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/TransportSqlAsyncGetStatusAction.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/TransportSqlAsyncGetStatusAction.java @@ -19,18 +19,28 @@ import org.elasticsearch.xpack.sql.action.SqlQueryResponse; import org.elasticsearch.xpack.sql.action.SqlQueryTask; - public class TransportSqlAsyncGetStatusAction extends AbstractTransportQlAsyncGetStatusAction { @Inject - public TransportSqlAsyncGetStatusAction(TransportService transportService, - ActionFilters actionFilters, - ClusterService clusterService, - NamedWriteableRegistry registry, - Client client, - ThreadPool threadPool, - BigArrays bigArrays) { - super(SqlAsyncGetStatusAction.NAME, transportService, actionFilters, clusterService, registry, client, threadPool, bigArrays, - SqlQueryTask.class); + public TransportSqlAsyncGetStatusAction( + TransportService transportService, + ActionFilters actionFilters, + ClusterService clusterService, + NamedWriteableRegistry registry, + Client client, + ThreadPool threadPool, + BigArrays bigArrays + ) { + super( + SqlAsyncGetStatusAction.NAME, + transportService, + actionFilters, + clusterService, + registry, + client, + threadPool, + bigArrays, + SqlQueryTask.class + ); } @Override diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/TransportSqlClearCursorAction.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/TransportSqlClearCursorAction.java index 3b4247e29f6cf..8260735653954 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/TransportSqlClearCursorAction.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/TransportSqlClearCursorAction.java @@ -25,8 +25,12 @@ public class TransportSqlClearCursorAction extends HandledTransportAction listener) { + public static void operation( + PlanExecutor planExecutor, + SqlClearCursorRequest request, + ActionListener listener + ) { Cursor cursor = Cursors.decodeFromStringWithZone(request.getCursor()).v1(); planExecutor.cleanCursor( cursor, @@ -47,4 +54,3 @@ public static void operation(PlanExecutor planExecutor, SqlClearCursorRequest re ); } } - diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/TransportSqlQueryAction.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/TransportSqlQueryAction.java index 6c3f26c119756..6d0c4a849e1f1 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/TransportSqlQueryAction.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/TransportSqlQueryAction.java @@ -62,7 +62,8 @@ import static org.elasticsearch.xpack.sql.proto.Mode.CLI; public class TransportSqlQueryAction extends HandledTransportAction - implements AsyncTaskManagementService.AsyncOperation { + implements + AsyncTaskManagementService.AsyncOperation { private static final Logger log = LogManager.getLogger(TransportSqlQueryAction.class); private final SecurityContext securityContext; @@ -73,29 +74,52 @@ public class TransportSqlQueryAction extends HandledTransportAction asyncTaskManagementService; @Inject - public TransportSqlQueryAction(Settings settings, ClusterService clusterService, TransportService transportService, - ThreadPool threadPool, ActionFilters actionFilters, PlanExecutor planExecutor, - SqlLicenseChecker sqlLicenseChecker, BigArrays bigArrays) { + public TransportSqlQueryAction( + Settings settings, + ClusterService clusterService, + TransportService transportService, + ThreadPool threadPool, + ActionFilters actionFilters, + PlanExecutor planExecutor, + SqlLicenseChecker sqlLicenseChecker, + BigArrays bigArrays + ) { super(SqlQueryAction.NAME, transportService, actionFilters, SqlQueryRequest::new); - this.securityContext = XPackSettings.SECURITY_ENABLED.get(settings) ? - new SecurityContext(settings, threadPool.getThreadContext()) : null; + this.securityContext = XPackSettings.SECURITY_ENABLED.get(settings) + ? new SecurityContext(settings, threadPool.getThreadContext()) + : null; this.clusterService = clusterService; this.planExecutor = planExecutor; this.sqlLicenseChecker = sqlLicenseChecker; this.transportService = transportService; - asyncTaskManagementService = new AsyncTaskManagementService<>(XPackPlugin.ASYNC_RESULTS_INDEX, planExecutor.client(), - ASYNC_SEARCH_ORIGIN, planExecutor.writeableRegistry(), taskManager, SqlQueryAction.INSTANCE.name(), this, SqlQueryTask.class, - clusterService, threadPool, bigArrays); + asyncTaskManagementService = new AsyncTaskManagementService<>( + XPackPlugin.ASYNC_RESULTS_INDEX, + planExecutor.client(), + ASYNC_SEARCH_ORIGIN, + planExecutor.writeableRegistry(), + taskManager, + SqlQueryAction.INSTANCE.name(), + this, + SqlQueryTask.class, + clusterService, + threadPool, + bigArrays + ); } @Override protected void doExecute(Task task, SqlQueryRequest request, ActionListener listener) { sqlLicenseChecker.checkIfSqlAllowed(request.mode()); if (request.waitForCompletionTimeout() != null && request.waitForCompletionTimeout().getMillis() >= 0) { - asyncTaskManagementService.asyncExecute(request, request.waitForCompletionTimeout(), request.keepAlive(), - request.keepOnCompletion(), listener); + asyncTaskManagementService.asyncExecute( + request, + request.waitForCompletionTimeout(), + request.keepAlive(), + request.keepOnCompletion(), + listener + ); } else { operation(planExecutor, (SqlQueryTask) task, request, listener, username(securityContext), transportService, clusterService); } @@ -104,28 +128,61 @@ protected void doExecute(Task task, SqlQueryRequest request, ActionListener listener, String username, TransportService transportService, - ClusterService clusterService) { + public static void operation( + PlanExecutor planExecutor, + SqlQueryTask task, + SqlQueryRequest request, + ActionListener listener, + String username, + TransportService transportService, + ClusterService clusterService + ) { // The configuration is always created however when dealing with the next page, only the timeouts are relevant // the rest having default values (since the query is already created) - SqlConfiguration cfg = new SqlConfiguration(request.zoneId(), request.catalog(), request.fetchSize(), request.requestTimeout(), - request.pageTimeout(), request.filter(), request.runtimeMappings(), request.mode(), request.clientId(), request.version(), - username, clusterName(clusterService), request.fieldMultiValueLeniency(), request.indexIncludeFrozen(), - new TaskId(clusterService.localNode().getId(), task.getId()), task); + SqlConfiguration cfg = new SqlConfiguration( + request.zoneId(), + request.catalog(), + request.fetchSize(), + request.requestTimeout(), + request.pageTimeout(), + request.filter(), + request.runtimeMappings(), + request.mode(), + request.clientId(), + request.version(), + username, + clusterName(clusterService), + request.fieldMultiValueLeniency(), + request.indexIncludeFrozen(), + new TaskId(clusterService.localNode().getId(), task.getId()), + task + ); if (Strings.hasText(request.cursor()) == false) { - executeRequestWithRetryAttempt(clusterService, listener::onFailure, - onFailure -> planExecutor.sql(cfg, request.query(), request.params(), - wrap(p -> listener.onResponse(createResponseWithSchema(request, p, task)), onFailure)), - node -> transportService.sendRequest(node, SqlQueryAction.NAME, request, - new ActionListenerResponseHandler<>(listener, SqlQueryResponse::new, ThreadPool.Names.SAME)), - log); + executeRequestWithRetryAttempt( + clusterService, + listener::onFailure, + onFailure -> planExecutor.sql( + cfg, + request.query(), + request.params(), + wrap(p -> listener.onResponse(createResponseWithSchema(request, p, task)), onFailure) + ), + node -> transportService.sendRequest( + node, + SqlQueryAction.NAME, + request, + new ActionListenerResponseHandler<>(listener, SqlQueryResponse::new, ThreadPool.Names.SAME) + ), + log + ); } else { Tuple decoded = Cursors.decodeFromStringWithZone(request.cursor()); - planExecutor.nextPage(cfg, decoded.v1(), - wrap(p -> listener.onResponse(createResponse(request, decoded.v2(), null, p, task)), - listener::onFailure)); + planExecutor.nextPage( + cfg, + decoded.v1(), + wrap(p -> listener.onResponse(createResponse(request, decoded.v2(), null, p, task)), listener::onFailure) + ); } } @@ -148,8 +205,13 @@ private static SqlQueryResponse createResponseWithSchema(SqlQueryRequest request return createResponse(request, request.zoneId(), columns, page, task); } - private static SqlQueryResponse createResponse(SqlQueryRequest request, ZoneId zoneId, List header, Page page, - SqlQueryTask task) { + private static SqlQueryResponse createResponse( + SqlQueryRequest request, + ZoneId zoneId, + List header, + Page page, + SqlQueryTask task + ) { List> rows = new ArrayList<>(); page.rowSet().forEachRow(rowView -> { List row = new ArrayList<>(rowView.columnCount()); @@ -159,14 +221,15 @@ private static SqlQueryResponse createResponse(SqlQueryRequest request, ZoneId z AsyncExecutionId executionId = task.getExecutionId(); return new SqlQueryResponse( - Cursors.encodeToString(page.next(), zoneId), - request.mode(), - request.version(), - request.columnar(), - header, - rows, - executionId == null ? null : executionId.getEncoded(), - false, false + Cursors.encodeToString(page.next(), zoneId), + request.mode(), + request.version(), + request.columnar(), + header, + rows, + executionId == null ? null : executionId.getEncoded(), + false, + false ); } @@ -192,10 +255,30 @@ private static Object value(Object r, Mode mode) { } @Override - public SqlQueryTask createTask(SqlQueryRequest request, long id, String type, String action, TaskId parentTaskId, - Map headers, Map originHeaders, AsyncExecutionId asyncExecutionId) { - return new SqlQueryTask(id, type, action, request.getDescription(), parentTaskId, headers, originHeaders, asyncExecutionId, - request.keepAlive(), request.mode(), request.version(), request.columnar()); + public SqlQueryTask createTask( + SqlQueryRequest request, + long id, + String type, + String action, + TaskId parentTaskId, + Map headers, + Map originHeaders, + AsyncExecutionId asyncExecutionId + ) { + return new SqlQueryTask( + id, + type, + action, + request.getDescription(), + parentTaskId, + headers, + originHeaders, + asyncExecutionId, + request.keepAlive(), + request.mode(), + request.version(), + request.columnar() + ); } @Override diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/TransportSqlStatsAction.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/TransportSqlStatsAction.java index 38626825bf5f4..00302f0390cac 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/TransportSqlStatsAction.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/TransportSqlStatsAction.java @@ -24,24 +24,43 @@ /** * Performs the stats operation. */ -public class TransportSqlStatsAction extends TransportNodesAction { +public class TransportSqlStatsAction extends TransportNodesAction< + SqlStatsRequest, + SqlStatsResponse, + SqlStatsRequest.NodeStatsRequest, + SqlStatsResponse.NodeStatsResponse> { // the plan executor holds the metrics private final PlanExecutor planExecutor; @Inject - public TransportSqlStatsAction(TransportService transportService, ClusterService clusterService, - ThreadPool threadPool, ActionFilters actionFilters, PlanExecutor planExecutor) { - super(SqlStatsAction.NAME, threadPool, clusterService, transportService, actionFilters, - SqlStatsRequest::new, SqlStatsRequest.NodeStatsRequest::new, ThreadPool.Names.MANAGEMENT, - SqlStatsResponse.NodeStatsResponse.class); + public TransportSqlStatsAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + PlanExecutor planExecutor + ) { + super( + SqlStatsAction.NAME, + threadPool, + clusterService, + transportService, + actionFilters, + SqlStatsRequest::new, + SqlStatsRequest.NodeStatsRequest::new, + ThreadPool.Names.MANAGEMENT, + SqlStatsResponse.NodeStatsResponse.class + ); this.planExecutor = planExecutor; } @Override - protected SqlStatsResponse newResponse(SqlStatsRequest request, List nodes, - List failures) { + protected SqlStatsResponse newResponse( + SqlStatsRequest request, + List nodes, + List failures + ) { return new SqlStatsResponse(clusterService.getClusterName(), nodes, failures); } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/TransportSqlTranslateAction.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/TransportSqlTranslateAction.java index 6b0e835b5cf65..7f69d850d9fcd 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/TransportSqlTranslateAction.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/TransportSqlTranslateAction.java @@ -37,13 +37,20 @@ public class TransportSqlTranslateAction extends HandledTransportAction listener) { sqlLicenseChecker.checkIfSqlAllowed(request.mode()); - SqlConfiguration cfg = new SqlConfiguration(request.zoneId(), request.catalog(), request.fetchSize(), - request.requestTimeout(), request.pageTimeout(), request.filter(), request.runtimeMappings(), - request.mode(), request.clientId(), request.version(), - username(securityContext), clusterName(clusterService), Protocol.FIELD_MULTI_VALUE_LENIENCY, - Protocol.INDEX_INCLUDE_FROZEN, null, null); + SqlConfiguration cfg = new SqlConfiguration( + request.zoneId(), + request.catalog(), + request.fetchSize(), + request.requestTimeout(), + request.pageTimeout(), + request.filter(), + request.runtimeMappings(), + request.mode(), + request.clientId(), + request.version(), + username(securityContext), + clusterName(clusterService), + Protocol.FIELD_MULTI_VALUE_LENIENCY, + Protocol.INDEX_INCLUDE_FROZEN, + null, + null + ); - planExecutor.searchSource(cfg, request.query(), request.params(), ActionListener.wrap( - searchSourceBuilder -> listener.onResponse(new SqlTranslateResponse(searchSourceBuilder)), listener::onFailure)); + planExecutor.searchSource( + cfg, + request.query(), + request.params(), + ActionListener.wrap( + searchSourceBuilder -> listener.onResponse(new SqlTranslateResponse(searchSourceBuilder)), + listener::onFailure + ) + ); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/Agg.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/Agg.java index 71d24dcd660ab..4df7e4298676f 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/Agg.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/Agg.java @@ -49,8 +49,7 @@ public boolean equals(Object obj) { } Agg other = (Agg) obj; - return Objects.equals(id, other.id) - && Objects.equals(source, other.source); + return Objects.equals(id, other.id) && Objects.equals(source, other.source); } @Override diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/AggFilter.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/AggFilter.java index c78bbcea4bc7e..56851bbac59f5 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/AggFilter.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/AggFilter.java @@ -58,8 +58,7 @@ public boolean equals(Object obj) { } AggFilter other = (AggFilter) obj; - return Objects.equals(name(), other.name()) - && Objects.equals(scriptTemplate(), other.scriptTemplate()); + return Objects.equals(name(), other.name()) && Objects.equals(scriptTemplate(), other.scriptTemplate()); } @Override diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/AggSource.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/AggSource.java index 32a75f30322df..629d7bdf5bd9a 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/AggSource.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/AggSource.java @@ -33,8 +33,7 @@ public static AggSource of(ScriptTemplate script) { ValuesSourceAggregationBuilder with(ValuesSourceAggregationBuilder aggBuilder) { if (fieldName != null) { return aggBuilder.field(fieldName); - } - else { + } else { return aggBuilder.script(script.toPainless()); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/Aggs.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/Aggs.java index a47b3bbbbc16e..0b82d37f46940 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/Aggs.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/Aggs.java @@ -172,8 +172,8 @@ public boolean equals(Object obj) { Aggs other = (Aggs) obj; return Objects.equals(groups, other.groups) - && Objects.equals(simpleAggs, other.simpleAggs) - && Objects.equals(pipelineAggs, other.pipelineAggs); + && Objects.equals(simpleAggs, other.simpleAggs) + && Objects.equals(pipelineAggs, other.pipelineAggs); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/FilterExistsAgg.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/FilterExistsAgg.java index e1801de82953f..d26d4cd1e44d2 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/FilterExistsAgg.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/FilterExistsAgg.java @@ -41,9 +41,10 @@ AggregationBuilder toBuilder() { } private static ScriptTemplate wrapWithIsNotNull(ScriptTemplate script) { - return new ScriptTemplate(formatTemplate( - format(Locale.ROOT, "{ql}.isNotNull(%s)", script.template())), - script.params(), - DataTypes.BOOLEAN); + return new ScriptTemplate( + formatTemplate(format(Locale.ROOT, "{ql}.isNotNull(%s)", script.template())), + script.params(), + DataTypes.BOOLEAN + ); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/GroupByDateHistogram.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/GroupByDateHistogram.java index ac8f4744f42bd..43fcdae78a011 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/GroupByDateHistogram.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/GroupByDateHistogram.java @@ -10,8 +10,8 @@ import org.elasticsearch.search.aggregations.bucket.composite.DateHistogramValuesSourceBuilder; import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; import org.elasticsearch.xpack.ql.expression.gen.script.ScriptTemplate; -import org.elasticsearch.xpack.ql.querydsl.container.Sort.Missing; import org.elasticsearch.xpack.ql.querydsl.container.Sort.Direction; +import org.elasticsearch.xpack.ql.querydsl.container.Sort.Missing; import org.elasticsearch.xpack.sql.SqlIllegalArgumentException; import java.time.ZoneId; @@ -42,8 +42,15 @@ public GroupByDateHistogram(String id, ScriptTemplate script, String calendarInt this(id, AggSource.of(script), null, null, -1L, calendarInterval, zoneId); } - private GroupByDateHistogram(String id, AggSource source, Direction direction, Missing missing, long fixedInterval, - String calendarInterval, ZoneId zoneId) { + private GroupByDateHistogram( + String id, + AggSource source, + Direction direction, + Missing missing, + long fixedInterval, + String calendarInterval, + ZoneId zoneId + ) { super(id, source, direction, missing); if (fixedInterval <= 0 && (calendarInterval == null || calendarInterval.isBlank())) { throw new SqlIllegalArgumentException("Either fixed interval or calendar interval needs to be specified"); @@ -61,8 +68,9 @@ public long fixedInterval() { @Override protected CompositeValuesSourceBuilder createSourceBuilder() { DateHistogramValuesSourceBuilder builder = new DateHistogramValuesSourceBuilder(id()).timeZone(zoneId); - return calendarInterval != null ? builder.calendarInterval(new DateHistogramInterval(calendarInterval)) - : builder.fixedInterval(new DateHistogramInterval(fixedInterval + "ms")); + return calendarInterval != null + ? builder.calendarInterval(new DateHistogramInterval(calendarInterval)) + : builder.fixedInterval(new DateHistogramInterval(fixedInterval + "ms")); } @Override @@ -80,8 +88,8 @@ public boolean equals(Object obj) { if (super.equals(obj)) { GroupByDateHistogram other = (GroupByDateHistogram) obj; return Objects.equals(fixedInterval, other.fixedInterval) - && Objects.equals(calendarInterval, other.calendarInterval) - && Objects.equals(zoneId, other.zoneId); + && Objects.equals(calendarInterval, other.calendarInterval) + && Objects.equals(zoneId, other.zoneId); } return false; } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/GroupByKey.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/GroupByKey.java index ada3e94bfbdb4..3754082a7fcd9 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/GroupByKey.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/GroupByKey.java @@ -67,8 +67,7 @@ public final CompositeValuesSourceBuilder asValueSource() { else { builder.field(source().fieldName()); } - builder.order(direction.asOrder()) - .missingBucket(true); + builder.order(direction.asOrder()).missingBucket(true); if (missing.aggregationOrder() != null) { builder.missingOrder(missing.aggregationOrder()); diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/GroupByNumericHistogram.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/GroupByNumericHistogram.java index 911912dca04d4..fabceaf30c3e3 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/GroupByNumericHistogram.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/GroupByNumericHistogram.java @@ -36,8 +36,7 @@ private GroupByNumericHistogram(String id, AggSource aggSource, Direction direct @Override protected CompositeValuesSourceBuilder createSourceBuilder() { - return new HistogramValuesSourceBuilder(id()) - .interval(interval); + return new HistogramValuesSourceBuilder(id()).interval(interval); } @Override diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/PercentileRanksAgg.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/PercentileRanksAgg.java index 56c4b0490fce9..447ba52307b19 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/PercentileRanksAgg.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/PercentileRanksAgg.java @@ -27,7 +27,6 @@ public PercentileRanksAgg(String id, AggSource source, List values, Perc @Override Function> builder() { - return s -> percentileRanks(s, values.stream().mapToDouble(Double::doubleValue).toArray()) - .percentilesConfig(percentilesConfig); + return s -> percentileRanks(s, values.stream().mapToDouble(Double::doubleValue).toArray()).percentilesConfig(percentilesConfig); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/PercentilesAgg.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/PercentilesAgg.java index 3c57d87ae21fe..02ee779f9630a 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/PercentilesAgg.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/PercentilesAgg.java @@ -27,8 +27,7 @@ public PercentilesAgg(String id, AggSource source, List percents, Percen @Override Function> builder() { - return s -> percentiles(s) - .percentiles(percents.stream().mapToDouble(Double::doubleValue).toArray()) + return s -> percentiles(s).percentiles(percents.stream().mapToDouble(Double::doubleValue).toArray()) .percentilesConfig(percentilesConfig); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/TopHitsAgg.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/TopHitsAgg.java index 9f70badec6958..6de81fac8f650 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/TopHitsAgg.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/agg/TopHitsAgg.java @@ -49,7 +49,7 @@ public TopHitsAgg( AggregationBuilder toBuilder() { // Sort missing values (NULLs) as last to get the first/last non-null value List> sortBuilderList = new ArrayList<>(2); - if (sortSource!= null) { + if (sortSource != null) { if (sortSource.fieldName() != null) { sortBuilderList.add( new FieldSortBuilder(sortSource.fieldName()).order(sortOrder) @@ -110,9 +110,9 @@ public boolean equals(Object o) { return false; } TopHitsAgg that = (TopHitsAgg) o; - return Objects.equals(sortSource, that.sortSource) && - sortOrder==that.sortOrder && - Objects.equals(fieldDataType, that.fieldDataType) && - Objects.equals(sortFieldDataType, that.sortFieldDataType); + return Objects.equals(sortSource, that.sortSource) + && sortOrder == that.sortOrder + && Objects.equals(fieldDataType, that.fieldDataType) + && Objects.equals(sortFieldDataType, that.sortFieldDataType); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/container/AggregateSort.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/container/AggregateSort.java index 9297764a1f3e5..5575206bc3e03 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/container/AggregateSort.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/container/AggregateSort.java @@ -42,7 +42,7 @@ public boolean equals(Object obj) { AggregateSort other = (AggregateSort) obj; return Objects.equals(direction(), other.direction()) - && Objects.equals(missing(), other.missing()) - && Objects.equals(agg, other.agg); + && Objects.equals(missing(), other.missing()) + && Objects.equals(agg, other.agg); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/container/GroupByRef.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/container/GroupByRef.java index ca86345946dc2..3ce1d6c5cc694 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/container/GroupByRef.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/container/GroupByRef.java @@ -14,7 +14,8 @@ public class GroupByRef extends AggRef { public enum Property { - VALUE, COUNT; + VALUE, + COUNT; } private final String key; diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/container/GroupingFunctionSort.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/container/GroupingFunctionSort.java index 80151d5986a75..12b95b33a66da 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/container/GroupingFunctionSort.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/container/GroupingFunctionSort.java @@ -32,7 +32,6 @@ public boolean equals(Object obj) { } GroupingFunctionSort other = (GroupingFunctionSort) obj; - return Objects.equals(direction(), other.direction()) - && Objects.equals(missing(), other.missing()); + return Objects.equals(direction(), other.direction()) && Objects.equals(missing(), other.missing()); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/container/QueryContainer.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/container/QueryContainer.java index c22a292c668ee..7fc356620d426 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/container/QueryContainer.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/container/QueryContainer.java @@ -6,8 +6,8 @@ */ package org.elasticsearch.xpack.sql.querydsl.container; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.Tuple; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; @@ -92,22 +92,23 @@ public class QueryContainer { // associate Attributes with aliased FieldAttributes (since they map directly to ES fields) private Map fieldAlias; - public QueryContainer() { this(null, null, null, null, null, null, null, -1, false, false, -1); } - public QueryContainer(Query query, - Aggs aggs, - List> fields, - AttributeMap aliases, - Map pseudoFunctions, - AttributeMap scalarFunctions, - Map sort, - int limit, - boolean trackHits, - boolean includeFrozen, - int minPageSize) { + public QueryContainer( + Query query, + Aggs aggs, + List> fields, + AttributeMap aliases, + Map pseudoFunctions, + AttributeMap scalarFunctions, + Map sort, + int limit, + boolean trackHits, + boolean includeFrozen, + int minPageSize + ) { this.query = query; this.aggs = aggs == null ? Aggs.EMPTY : aggs; this.fields = fields == null || fields.isEmpty() ? emptyList() : fields; @@ -269,13 +270,35 @@ public int minPageSize() { // public QueryContainer with(Query q) { - return new QueryContainer(q, aggs, fields, aliases, pseudoFunctions, scalarFunctions, sort, limit, trackHits, includeFrozen, - minPageSize); + return new QueryContainer( + q, + aggs, + fields, + aliases, + pseudoFunctions, + scalarFunctions, + sort, + limit, + trackHits, + includeFrozen, + minPageSize + ); } public QueryContainer withAliases(AttributeMap a) { - return new QueryContainer(query, aggs, fields, a, pseudoFunctions, scalarFunctions, sort, limit, trackHits, includeFrozen, - minPageSize); + return new QueryContainer( + query, + aggs, + fields, + a, + pseudoFunctions, + scalarFunctions, + sort, + limit, + trackHits, + includeFrozen, + minPageSize + ); } public QueryContainer withPseudoFunctions(Map p) { @@ -283,23 +306,61 @@ public QueryContainer withPseudoFunctions(Map p) { } public QueryContainer with(Aggs a) { - return new QueryContainer(query, a, fields, aliases, pseudoFunctions, scalarFunctions, sort, limit, trackHits, includeFrozen, - minPageSize); + return new QueryContainer( + query, + a, + fields, + aliases, + pseudoFunctions, + scalarFunctions, + sort, + limit, + trackHits, + includeFrozen, + minPageSize + ); } public QueryContainer withLimit(int l) { - return l == limit ? this : new QueryContainer(query, aggs, fields, aliases, pseudoFunctions, scalarFunctions, sort, l, trackHits, - includeFrozen, minPageSize); + return l == limit + ? this + : new QueryContainer( + query, + aggs, + fields, + aliases, + pseudoFunctions, + scalarFunctions, + sort, + l, + trackHits, + includeFrozen, + minPageSize + ); } public QueryContainer withTrackHits() { - return trackHits ? this : new QueryContainer(query, aggs, fields, aliases, pseudoFunctions, scalarFunctions, sort, limit, true, - includeFrozen, minPageSize); + return trackHits + ? this + : new QueryContainer( + query, + aggs, + fields, + aliases, + pseudoFunctions, + scalarFunctions, + sort, + limit, + true, + includeFrozen, + minPageSize + ); } public QueryContainer withFrozen() { - return includeFrozen ? this : new QueryContainer(query, aggs, fields, aliases, pseudoFunctions, scalarFunctions, sort, limit, - trackHits, true, minPageSize); + return includeFrozen + ? this + : new QueryContainer(query, aggs, fields, aliases, pseudoFunctions, scalarFunctions, sort, limit, trackHits, true, minPageSize); } public QueryContainer withScalarProcessors(AttributeMap procs) { @@ -316,8 +377,19 @@ public QueryContainer prependSort(String expressionId, Sort sortable) { for (Map.Entry entry : this.sort.entrySet()) { newSort.putIfAbsent(entry.getKey(), entry.getValue()); } - return new QueryContainer(query, aggs, fields, aliases, pseudoFunctions, scalarFunctions, newSort, limit, trackHits, includeFrozen, - minPageSize); + return new QueryContainer( + query, + aggs, + fields, + aliases, + pseudoFunctions, + scalarFunctions, + newSort, + limit, + trackHits, + includeFrozen, + minPageSize + ); } private String aliasName(Attribute attr) { @@ -342,26 +414,52 @@ private FieldExtraction topHitFieldRef(FieldAttribute fieldAttr) { private Tuple nestedHitFieldRef(FieldAttribute attr) { String name = aliasName(attr); - Query q = rewriteToContainNestedField(query, attr.source(), - attr.nestedParent().name(), name, - SqlDataTypes.format(attr.field().getDataType()), - SqlDataTypes.isFromDocValuesOnly(attr.field().getDataType())); + Query q = rewriteToContainNestedField( + query, + attr.source(), + attr.nestedParent().name(), + name, + SqlDataTypes.format(attr.field().getDataType()), + SqlDataTypes.isFromDocValuesOnly(attr.field().getDataType()) + ); SearchHitFieldRef nestedFieldRef = new SearchHitFieldRef(name, attr.field().getDataType(), attr.nestedParent().name()); return new Tuple<>( - new QueryContainer(q, aggs, fields, aliases, pseudoFunctions, scalarFunctions, sort, limit, trackHits, includeFrozen, - minPageSize), - nestedFieldRef); - } - - static Query rewriteToContainNestedField(@Nullable Query query, Source source, String path, String name, String format, - boolean hasDocValues) { + new QueryContainer( + q, + aggs, + fields, + aliases, + pseudoFunctions, + scalarFunctions, + sort, + limit, + trackHits, + includeFrozen, + minPageSize + ), + nestedFieldRef + ); + } + + static Query rewriteToContainNestedField( + @Nullable Query query, + Source source, + String path, + String name, + String format, + boolean hasDocValues + ) { if (query == null) { /* There is no query so we must add the nested query * ourselves to fetch the field. */ - return new NestedQuery(source, path, singletonMap(name, new AbstractMap.SimpleImmutableEntry<>(hasDocValues, format)), - new MatchAll(source)); + return new NestedQuery( + source, + path, + singletonMap(name, new AbstractMap.SimpleImmutableEntry<>(hasDocValues, format)), + new MatchAll(source) + ); } if (query.containsNestedField(path, name)) { // The query already has the nested field. Nothing to do. @@ -377,8 +475,12 @@ static Query rewriteToContainNestedField(@Nullable Query query, Source source, S } /* There is no nested query with a matching path so we must * add the nested query ourselves just to fetch the field. */ - NestedQuery nested = new NestedQuery(source, path, - singletonMap(name, new AbstractMap.SimpleImmutableEntry<>(hasDocValues, format)), new MatchAll(source)); + NestedQuery nested = new NestedQuery( + source, + path, + singletonMap(name, new AbstractMap.SimpleImmutableEntry<>(hasDocValues, format)), + new MatchAll(source) + ); return new BoolQuery(source, true, query, nested); } @@ -412,8 +514,7 @@ public FieldExtraction resolve(Attribute attribute) { // update proc (if needed) if (qContainer.scalarFunctions().size() != scalarFunctions.size()) { - qContainer = qContainer.withScalarProcessors( - AttributeMap.builder(qContainer.scalarFunctions).put(attr, proc).build()); + qContainer = qContainer.withScalarProcessors(AttributeMap.builder(qContainer.scalarFunctions).put(attr, proc).build()); } return new Tuple<>(qContainer, new ComputedRef(proc)); @@ -458,9 +559,19 @@ private Tuple asFieldExtraction(Attribute attr) } public QueryContainer addColumn(FieldExtraction ref, String id) { - return new QueryContainer(query, aggs, combine(fields, new Tuple<>(ref, id)), aliases, pseudoFunctions, - scalarFunctions, - sort, limit, trackHits, includeFrozen, minPageSize); + return new QueryContainer( + query, + aggs, + combine(fields, new Tuple<>(ref, id)), + aliases, + pseudoFunctions, + scalarFunctions, + sort, + limit, + trackHits, + includeFrozen, + minPageSize + ); } public AttributeMap scalarFunctions() { @@ -508,13 +619,13 @@ public boolean equals(Object obj) { QueryContainer other = (QueryContainer) obj; return Objects.equals(query, other.query) - && Objects.equals(aggs, other.aggs) - && Objects.equals(fields, other.fields) - && Objects.equals(aliases, other.aliases) - && Objects.equals(sort, other.sort) - && Objects.equals(limit, other.limit) - && Objects.equals(trackHits, other.trackHits) - && Objects.equals(includeFrozen, other.includeFrozen); + && Objects.equals(aggs, other.aggs) + && Objects.equals(fields, other.fields) + && Objects.equals(aliases, other.aliases) + && Objects.equals(sort, other.sort) + && Objects.equals(limit, other.limit) + && Objects.equals(trackHits, other.trackHits) + && Objects.equals(includeFrozen, other.includeFrozen); } @Override diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/container/ScoreSort.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/container/ScoreSort.java index 329d466590485..47d9f013a7ce4 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/container/ScoreSort.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/container/ScoreSort.java @@ -32,7 +32,6 @@ public boolean equals(Object obj) { } ScriptSort other = (ScriptSort) obj; - return Objects.equals(direction(), other.direction()) - && Objects.equals(missing(), other.missing()); + return Objects.equals(direction(), other.direction()) && Objects.equals(missing(), other.missing()); } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/session/Cursors.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/session/Cursors.java index c801a6ccc61bb..ccd687d1550ab 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/session/Cursors.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/session/Cursors.java @@ -7,9 +7,9 @@ package org.elasticsearch.xpack.sql.session; import org.elasticsearch.Version; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.io.stream.NamedWriteable; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; +import org.elasticsearch.core.Tuple; import org.elasticsearch.xpack.ql.util.StringUtils; import org.elasticsearch.xpack.sql.SqlIllegalArgumentException; import org.elasticsearch.xpack.sql.common.io.SqlStreamInput; diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/session/ListCursor.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/session/ListCursor.java index f78d787877437..a6aaec11b7e0f 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/session/ListCursor.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/session/ListCursor.java @@ -74,12 +74,10 @@ public static Page of(Schema schema, List> data, int pageSize) { // why this method is not exposed private static Page of(Schema schema, List> data, int pageSize, int columnCount) { List> nextData = data.size() > pageSize ? data.subList(pageSize, data.size()) : emptyList(); - Cursor next = nextData.isEmpty() - ? Cursor.EMPTY - : new ListCursor(nextData, pageSize, columnCount); - List> currData = data.isEmpty() || pageSize == 0 - ? emptyList() - : data.size() == pageSize ? data : data.subList(0, Math.min(pageSize, data.size())); + Cursor next = nextData.isEmpty() ? Cursor.EMPTY : new ListCursor(nextData, pageSize, columnCount); + List> currData = data.isEmpty() || pageSize == 0 ? emptyList() + : data.size() == pageSize ? data + : data.subList(0, Math.min(pageSize, data.size())); return new Page(new ListRowSet(schema, currData, columnCount), next); } @@ -110,8 +108,8 @@ public boolean equals(Object obj) { ListCursor other = (ListCursor) obj; return Objects.equals(pageSize, other.pageSize) - && Objects.equals(columnCount, other.columnCount) - && Objects.equals(data, other.data); + && Objects.equals(columnCount, other.columnCount) + && Objects.equals(data, other.data); } @Override diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/session/ListRowSet.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/session/ListRowSet.java index 66508e8fbc5d7..a599f3dd392fe 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/session/ListRowSet.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/session/ListRowSet.java @@ -27,7 +27,6 @@ public class ListRowSet extends AbstractRowSet implements SchemaRowSet { this.list = list; } - @Override protected boolean doHasCurrent() { return pos < size(); diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/session/SqlSession.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/session/SqlSession.java index bac11119b83dc..be39a954e9a1a 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/session/SqlSession.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/session/SqlSession.java @@ -51,13 +51,17 @@ public class SqlSession implements Session { private final SqlConfiguration configuration; - public SqlSession(SqlConfiguration configuration, Client client, FunctionRegistry functionRegistry, - IndexResolver indexResolver, - PreAnalyzer preAnalyzer, - Verifier verifier, - Optimizer optimizer, - Planner planner, - PlanExecutor planExecutor) { + public SqlSession( + SqlConfiguration configuration, + Client client, + FunctionRegistry functionRegistry, + IndexResolver indexResolver, + PreAnalyzer preAnalyzer, + Verifier verifier, + Optimizer optimizer, + Planner planner, + PlanExecutor planExecutor + ) { this.client = configuration.taskId() != null ? new ParentTaskAssigningClient(client, configuration.taskId()) : client; this.functionRegistry = functionRegistry; @@ -145,12 +149,17 @@ private void preAnalyze(LogicalPlan parsed, Function act String cluster = table.cluster(); cluster = hasText(cluster) ? cluster : configuration.catalog(); - String indexPattern = hasText(cluster) && cluster.equals(configuration.clusterName()) == false ? - buildRemoteIndexName(cluster, table.index()) : table.index(); + String indexPattern = hasText(cluster) && cluster.equals(configuration.clusterName()) == false + ? buildRemoteIndexName(cluster, table.index()) + : table.index(); boolean includeFrozen = configuration.includeFrozen() || tableInfo.isFrozen(); - indexResolver.resolveAsMergedMapping(indexPattern, includeFrozen, - configuration.runtimeMappings(), wrap(indexResult -> listener.onResponse(action.apply(indexResult)), listener::onFailure)); + indexResolver.resolveAsMergedMapping( + indexPattern, + includeFrozen, + configuration.runtimeMappings(), + wrap(indexResult -> listener.onResponse(action.apply(indexResult)), listener::onFailure) + ); } else { try { // occurs when dealing with local relations (SELECT 5+2) diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/stats/Metrics.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/stats/Metrics.java index ae774fedcce20..be87898ce02b0 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/stats/Metrics.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/stats/Metrics.java @@ -24,7 +24,9 @@ */ public class Metrics { private enum OperationType { - FAILED, PAGING, TOTAL; + FAILED, + PAGING, + TOTAL; @Override public String toString() { @@ -47,7 +49,7 @@ public Metrics() { for (QueryMetric metric : QueryMetric.values()) { Map metricsMap = new LinkedHashMap<>(OperationType.values().length); for (OperationType type : OperationType.values()) { - metricsMap.put(type, new CounterMetric()); + metricsMap.put(type, new CounterMetric()); } qMap.put(metric, Collections.unmodifiableMap(metricsMap)); @@ -56,7 +58,7 @@ public Metrics() { Map fMap = new LinkedHashMap<>(FeatureMetric.values().length); for (FeatureMetric featureMetric : FeatureMetric.values()) { - fMap.put(featureMetric, new CounterMetric()); + fMap.put(featureMetric, new CounterMetric()); } featuresMetrics = Collections.unmodifiableMap(fMap); diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/stats/QueryMetric.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/stats/QueryMetric.java index 58122092c2047..f0428be3664c7 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/stats/QueryMetric.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/stats/QueryMetric.java @@ -15,7 +15,13 @@ import static org.elasticsearch.xpack.sql.proto.RequestInfo.ODBC_CLIENT_IDS; public enum QueryMetric { - CANVAS, CLI, JDBC, ODBC, ODBC32, ODBC64, REST; + CANVAS, + CLI, + JDBC, + ODBC, + ODBC32, + ODBC64, + REST; public static QueryMetric fromString(String metric) { try { diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/type/SqlDataTypeConverter.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/type/SqlDataTypeConverter.java index 57f454fa9f20a..00c67c0e06a76 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/type/SqlDataTypeConverter.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/type/SqlDataTypeConverter.java @@ -55,7 +55,6 @@ import static org.elasticsearch.xpack.sql.type.SqlDataTypes.isInterval; import static org.elasticsearch.xpack.sql.type.SqlDataTypes.isYearMonthInterval; - public final class SqlDataTypeConverter { private SqlDataTypeConverter() {} diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/type/SqlDataTypes.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/type/SqlDataTypes.java index 01c29ca505970..b58cbcc07ce78 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/type/SqlDataTypes.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/type/SqlDataTypes.java @@ -139,7 +139,9 @@ public class SqlDataTypes { ODBC_TO_ES.put("SQL_INTERVAL_MINUTE_TO_SECOND", INTERVAL_MINUTE_TO_SECOND); } - private static final Collection TYPES = Stream.concat(DataTypes.types().stream(), Stream.of( + private static final Collection TYPES = Stream.concat( + DataTypes.types().stream(), + Stream.of( DATE, TIME, INTERVAL_YEAR, @@ -157,12 +159,11 @@ public class SqlDataTypes { INTERVAL_MINUTE_TO_SECOND, GEO_SHAPE, GEO_POINT, - SHAPE)) - .sorted(Comparator.comparing(DataType::typeName)) - .collect(toUnmodifiableList()); + SHAPE + ) + ).sorted(Comparator.comparing(DataType::typeName)).collect(toUnmodifiableList()); - private static final Map NAME_TO_TYPE = TYPES.stream() - .collect(toUnmodifiableMap(DataType::typeName, t -> t)); + private static final Map NAME_TO_TYPE = TYPES.stream().collect(toUnmodifiableMap(DataType::typeName, t -> t)); private static final Map ES_TO_TYPE; @@ -244,10 +245,16 @@ public static boolean isYearMonthInterval(DataType dataType) { } public static boolean isDayTimeInterval(DataType dataType) { - return dataType == INTERVAL_DAY || dataType == INTERVAL_HOUR || dataType == INTERVAL_MINUTE || dataType == INTERVAL_SECOND - || dataType == INTERVAL_DAY_TO_HOUR || dataType == INTERVAL_DAY_TO_MINUTE || dataType == INTERVAL_DAY_TO_SECOND - || dataType == INTERVAL_HOUR_TO_MINUTE || dataType == INTERVAL_HOUR_TO_SECOND - || dataType == INTERVAL_MINUTE_TO_SECOND; + return dataType == INTERVAL_DAY + || dataType == INTERVAL_HOUR + || dataType == INTERVAL_MINUTE + || dataType == INTERVAL_SECOND + || dataType == INTERVAL_DAY_TO_HOUR + || dataType == INTERVAL_DAY_TO_MINUTE + || dataType == INTERVAL_DAY_TO_SECOND + || dataType == INTERVAL_HOUR_TO_MINUTE + || dataType == INTERVAL_HOUR_TO_SECOND + || dataType == INTERVAL_MINUTE_TO_SECOND; } public static boolean isDateBased(DataType type) { @@ -276,11 +283,11 @@ public static String format(DataType type) { public static boolean isFromDocValuesOnly(DataType dataType) { return dataType == KEYWORD // because of ignore_above. Extracting this from _source wouldn't make sense - || dataType == DATE // because of date formats - || dataType == DATETIME - || dataType == SCALED_FLOAT // because of scaling_factor - || dataType == GEO_POINT - || dataType == SHAPE; + || dataType == DATE // because of date formats + || dataType == DATETIME + || dataType == SCALED_FLOAT // because of scaling_factor + || dataType == GEO_POINT + || dataType == SHAPE; } public static boolean areCompatible(DataType left, DataType right) { @@ -288,11 +295,12 @@ public static boolean areCompatible(DataType left, DataType right) { return true; } else { return (left == NULL || right == NULL) - || (DataTypes.isString(left) && DataTypes.isString(right)) - || (left.isNumeric() && right.isNumeric()) - || (isDateBased(left) && isDateBased(right)) - || (isInterval(left) && isDateBased(right)) || (isDateBased(left) && isInterval(right)) - || (isInterval(left) && isInterval(right) && Intervals.compatibleInterval(left, right) != null); + || (DataTypes.isString(left) && DataTypes.isString(right)) + || (left.isNumeric() && right.isNumeric()) + || (isDateBased(left) && isDateBased(right)) + || (isInterval(left) && isDateBased(right)) + || (isDateBased(left) && isInterval(right)) + || (isInterval(left) && isInterval(right) && Intervals.compatibleInterval(left, right) != null); } } @@ -615,7 +623,7 @@ public static int displaySize(DataType dataType) { return dataType.size(); } if (dataType == GEO_POINT) { - //2 doubles + len("POINT( )") + // 2 doubles + len("POINT( )") return 25 * 2 + 8; } if (dataType == SHAPE) { @@ -687,15 +695,15 @@ private static Short metaSqlSameScale(DataType t) { // https://docs.microsoft.com/en-us/sql/odbc/reference/syntax/sqlgettypeinfo-function public static Integer metaSqlRadix(DataType t) { - // RADIX - Determines how numbers returned by COLUMN_SIZE and DECIMAL_DIGITS should be interpreted. + // RADIX - Determines how numbers returned by COLUMN_SIZE and DECIMAL_DIGITS should be interpreted. // 10 means they represent the number of decimal digits allowed for the column. // 2 means they represent the number of bits allowed for the column. // null means radix is not applicable for the given type. return t.isInteger() ? Integer.valueOf(10) : (t.isRational() ? Integer.valueOf(2) : null); } - //https://docs.microsoft.com/en-us/sql/odbc/reference/syntax/sqlgettypeinfo-function#comments - //https://docs.microsoft.com/en-us/sql/odbc/reference/appendixes/column-size + // https://docs.microsoft.com/en-us/sql/odbc/reference/syntax/sqlgettypeinfo-function#comments + // https://docs.microsoft.com/en-us/sql/odbc/reference/appendixes/column-size public static Integer precision(DataType t) { if (t.isNumeric()) { return defaultPrecision(t); diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/util/Check.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/util/Check.java index 15aa49224d1fd..38f13dbe974f6 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/util/Check.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/util/Check.java @@ -40,13 +40,15 @@ public static void notNull(Object object, String message, Object... values) { public static void isFixedNumberAndInRange(Object object, String objectName, Long from, Long to) { if ((object instanceof Number) == false || object instanceof Float || object instanceof Double) { - throw new SqlIllegalArgumentException("A fixed point number is required for [{}]; received [{}]", objectName, - object.getClass().getTypeName()); + throw new SqlIllegalArgumentException( + "A fixed point number is required for [{}]; received [{}]", + objectName, + object.getClass().getTypeName() + ); } Long longValue = ((Number) object).longValue(); if (longValue < from || longValue > to) { - throw new SqlIllegalArgumentException("[{}] out of the allowed range [{}, {}], received [{}]", objectName, from, to, - longValue); + throw new SqlIllegalArgumentException("[{}] out of the allowed range [{}, {}], received [{}]", objectName, from, to, longValue); } } } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/util/DateUtils.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/util/DateUtils.java index 5be8ccededdf1..d9b2b32dd23e6 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/util/DateUtils.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/util/DateUtils.java @@ -37,25 +37,17 @@ public final class DateUtils { public static final LocalDate EPOCH = LocalDate.of(1970, 1, 1); public static final long DAY_IN_MILLIS = 60 * 60 * 24 * 1000L; - private static final DateTimeFormatter ISO_LOCAL_TIME_OPTIONAL_TZ = new DateTimeFormatterBuilder() - .append(ISO_LOCAL_TIME) - .optionalStart() - .appendZoneOrOffsetId() - .toFormatter().withZone(UTC); - private static final DateTimeFormatter ISO_LOCAL_DATE_OPTIONAL_TIME_FORMATTER_WHITESPACE = new DateTimeFormatterBuilder() - .append(ISO_LOCAL_DATE) - .optionalStart() - .appendLiteral(' ') - .append(ISO_LOCAL_TIME_OPTIONAL_TZ) - .optionalEnd() - .toFormatter().withZone(UTC); - private static final DateTimeFormatter ISO_LOCAL_DATE_OPTIONAL_TIME_FORMATTER_T_LITERAL = new DateTimeFormatterBuilder() - .append(ISO_LOCAL_DATE) - .optionalStart() - .appendLiteral('T') - .append(ISO_LOCAL_TIME_OPTIONAL_TZ) - .optionalEnd() - .toFormatter().withZone(UTC); + private static final DateTimeFormatter ISO_LOCAL_TIME_OPTIONAL_TZ = new DateTimeFormatterBuilder().append(ISO_LOCAL_TIME) + .optionalStart() + .appendZoneOrOffsetId() + .toFormatter() + .withZone(UTC); + private static final DateTimeFormatter ISO_LOCAL_DATE_OPTIONAL_TIME_FORMATTER_WHITESPACE = new DateTimeFormatterBuilder().append( + ISO_LOCAL_DATE + ).optionalStart().appendLiteral(' ').append(ISO_LOCAL_TIME_OPTIONAL_TZ).optionalEnd().toFormatter().withZone(UTC); + private static final DateTimeFormatter ISO_LOCAL_DATE_OPTIONAL_TIME_FORMATTER_T_LITERAL = new DateTimeFormatterBuilder().append( + ISO_LOCAL_DATE + ).optionalStart().appendLiteral('T').append(ISO_LOCAL_TIME_OPTIONAL_TZ).optionalEnd().toFormatter().withZone(UTC); private static final DateFormatter UTC_DATE_TIME_FORMATTER = DateFormatter.forPattern("strict_date_optional_time").withZone(UTC); private static final int DEFAULT_PRECISION_FOR_CURRENT_FUNCTIONS = 3; @@ -152,7 +144,7 @@ public static String toTimeString(OffsetTime time) { } public static long minDayInterval(long l) { - if (l < DAY_IN_MILLIS ) { + if (l < DAY_IN_MILLIS) { return DAY_IN_MILLIS; } return l - (l % DAY_IN_MILLIS); @@ -170,8 +162,11 @@ public static int getNanoPrecision(Expression precisionExpression, int nano) { } if (precision < 0 || precision > 9) { - throw new ParsingException(precisionExpression.source(), "precision needs to be between [0-9], received [{}]", - precisionExpression.sourceText()); + throw new ParsingException( + precisionExpression.source(), + "precision needs to be between [0-9], received [{}]", + precisionExpression.sourceText() + ); } // remove the remainder @@ -204,8 +199,8 @@ public static ZonedDateTime atTimeZone(ZonedDateTime zdt, ZoneId zoneId) { public static TemporalAccessor atTimeZone(TemporalAccessor ta, ZoneId zoneId) { if (ta instanceof LocalDateTime) { return atTimeZone((LocalDateTime) ta, zoneId); - } else if (ta instanceof ZonedDateTime){ - return atTimeZone((ZonedDateTime)ta, zoneId); + } else if (ta instanceof ZonedDateTime) { + return atTimeZone((ZonedDateTime) ta, zoneId); } else if (ta instanceof OffsetTime) { return atTimeZone((OffsetTime) ta, zoneId); } else if (ta instanceof LocalTime) { @@ -224,8 +219,8 @@ private static int timeSeparatorIdx(String timestampStr) { } // Find the second `-` date separator and move 3 places past the dayOfYear to find the time separator // e.g. 2020-06-01T10:20:30.... - // ^ - // +3 = ^ + // ^ + // +3 = ^ return timestampStr.indexOf('-', separatorIdx + 1) + 3; } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/SqlInfoTransportActionTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/SqlInfoTransportActionTests.java index 97505810b72ad..37e4acea1452b 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/SqlInfoTransportActionTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/SqlInfoTransportActionTests.java @@ -16,12 +16,12 @@ import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.ObjectPath; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.tasks.Task; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.ObjectPath; import org.elasticsearch.xpack.core.action.XPackUsageFeatureResponse; import org.elasticsearch.xpack.core.sql.SqlFeatureSetUsage; import org.elasticsearch.xpack.core.watcher.common.stats.Counters; @@ -58,15 +58,17 @@ public void init() throws Exception { public void testAvailable() { SqlInfoTransportAction featureSet = new SqlInfoTransportAction( - mock(TransportService.class), mock(ActionFilters.class), licenseState); + mock(TransportService.class), + mock(ActionFilters.class), + licenseState + ); assertThat(featureSet.available(), is(true)); } @SuppressWarnings("unchecked") public void testUsageStats() throws Exception { doAnswer(mock -> { - ActionListener listener = - (ActionListener) mock.getArguments()[2]; + ActionListener listener = (ActionListener) mock.getArguments()[2]; List nodes = new ArrayList<>(); DiscoveryNode first = new DiscoveryNode("first", buildNewFakeTransportAddress(), Version.CURRENT); @@ -93,8 +95,15 @@ public void testUsageStats() throws Exception { when(mockNode.getId()).thenReturn("mocknode"); when(clusterService.localNode()).thenReturn(mockNode); - var usageAction = new SqlUsageTransportAction(mock(TransportService.class), clusterService, null, - mock(ActionFilters.class), null, licenseState, client); + var usageAction = new SqlUsageTransportAction( + mock(TransportService.class), + clusterService, + null, + mock(ActionFilters.class), + null, + licenseState, + client + ); PlainActionFuture future = new PlainActionFuture<>(); usageAction.masterOperation(mock(Task.class), null, null, future); SqlFeatureSetUsage sqlUsage = (SqlFeatureSetUsage) future.get().getUsage(); diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/SqlTestUtils.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/SqlTestUtils.java index 938ae01e3ef96..31a6f2bee88ff 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/SqlTestUtils.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/SqlTestUtils.java @@ -38,20 +38,34 @@ import static org.elasticsearch.test.ESTestCase.randomNonNegativeLong; import static org.elasticsearch.test.ESTestCase.randomZone; - public final class SqlTestUtils { private SqlTestUtils() {} - public static final SqlConfiguration TEST_CFG = new SqlConfiguration(DateUtils.UTC, null, Protocol.FETCH_SIZE, - Protocol.REQUEST_TIMEOUT, Protocol.PAGE_TIMEOUT, null, null, Mode.PLAIN, - null, null, null, null, false, false, - null, null); + public static final SqlConfiguration TEST_CFG = new SqlConfiguration( + DateUtils.UTC, + null, + Protocol.FETCH_SIZE, + Protocol.REQUEST_TIMEOUT, + Protocol.PAGE_TIMEOUT, + null, + null, + Mode.PLAIN, + null, + null, + null, + null, + false, + false, + null, + null + ); public static SqlConfiguration randomConfiguration(ZoneId providedZoneId, SqlVersion sqlVersion) { Mode mode = randomFrom(Mode.values()); long taskId = randomNonNegativeLong(); - return new SqlConfiguration(providedZoneId != null ? providedZoneId : randomZone(), + return new SqlConfiguration( + providedZoneId != null ? providedZoneId : randomZone(), null, randomIntBetween(0, 1000), new TimeValue(randomNonNegativeLong()), @@ -66,7 +80,8 @@ public static SqlConfiguration randomConfiguration(ZoneId providedZoneId, SqlVer false, randomBoolean(), new TaskId(randomAlphaOfLength(10), taskId), - randomTask(taskId, mode, sqlVersion)); + randomTask(taskId, mode, sqlVersion) + ); } public static SqlConfiguration randomConfiguration() { @@ -82,9 +97,20 @@ public static SqlConfiguration randomConfiguration(SqlVersion version) { } public static SqlQueryTask randomTask(long taskId, Mode mode, SqlVersion sqlVersion) { - return new SqlQueryTask(taskId, "transport", SqlQueryAction.NAME, "", null, emptyMap(), emptyMap(), - new AsyncExecutionId("", new TaskId(randomAlphaOfLength(10), 1)), TimeValue.timeValueDays(5), mode, sqlVersion, - randomBoolean()); + return new SqlQueryTask( + taskId, + "transport", + SqlQueryAction.NAME, + "", + null, + emptyMap(), + emptyMap(), + new AsyncExecutionId("", new TaskId(randomAlphaOfLength(10), 1)), + TimeValue.timeValueDays(5), + mode, + sqlVersion, + randomBoolean() + ); } public static String randomWhitespaces() { diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/action/AbstractSqlIntegTestCase.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/action/AbstractSqlIntegTestCase.java index a833ee2a0ee1f..63d3f68b7045d 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/action/AbstractSqlIntegTestCase.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/action/AbstractSqlIntegTestCase.java @@ -36,4 +36,3 @@ protected Collection> nodePlugins() { return Collections.singletonList(LocalStateSQLXPackPlugin.class); } } - diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/action/BasicFormatterTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/action/BasicFormatterTests.java index 5a98373afc6a5..3351c3df495c5 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/action/BasicFormatterTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/action/BasicFormatterTests.java @@ -6,13 +6,13 @@ */ package org.elasticsearch.xpack.sql.action; -import java.util.Arrays; - import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.sql.action.BasicFormatter.FormatOption; import org.elasticsearch.xpack.sql.proto.ColumnInfo; import org.elasticsearch.xpack.sql.proto.Mode; +import java.util.Arrays; + import static org.elasticsearch.xpack.sql.action.BasicFormatter.FormatOption.CLI; import static org.elasticsearch.xpack.sql.proto.SqlVersion.DATE_NANOS_SUPPORT_VERSION; import static org.hamcrest.Matchers.arrayWithSize; @@ -51,14 +51,26 @@ public class BasicFormatterTests extends ESTestCase { public void testFormatWithHeader() { String[] result = formatter.formatWithHeader(firstResponse.columns(), firstResponse.rows()).split("\n"); assertThat(result, arrayWithSize(4)); - assertEquals(" foo | bar |15charwidename!| null_field1 |superduperwidename!!!| baz |" - + " date | null_field2 ", result[0]); - assertEquals("---------------+----------------------+---------------+---------------+---------------------+---------------+" - + "------------------------+---------------", result[1]); - assertEquals("15charwidedata!|1 |6.888 |null |12 |rabbit |" - + "1953-09-02T00:00:00.000Z|null ", result[2]); - assertEquals("dog |1.7976931348623157E308|123124.888 |null |9912 |goat |" - + "2000-03-15T21:34:37.443Z|null ", result[3]); + assertEquals( + " foo | bar |15charwidename!| null_field1 |superduperwidename!!!| baz |" + + " date | null_field2 ", + result[0] + ); + assertEquals( + "---------------+----------------------+---------------+---------------+---------------------+---------------+" + + "------------------------+---------------", + result[1] + ); + assertEquals( + "15charwidedata!|1 |6.888 |null |12 |rabbit |" + + "1953-09-02T00:00:00.000Z|null ", + result[2] + ); + assertEquals( + "dog |1.7976931348623157E308|123124.888 |null |9912 |goat |" + + "2000-03-15T21:34:37.443Z|null ", + result[3] + ); } /** @@ -67,23 +79,32 @@ public void testFormatWithHeader() { */ public void testFormatWithoutHeader() { String[] result = formatter.formatWithoutHeader( - Arrays.asList( - Arrays.asList("ohnotruncateddata", 4, 1, null, 77, "wombat", "1955-01-21T01:02:03.342Z", null), - Arrays.asList("dog", 2, 123124.888, null, 9912, "goat", "2231-12-31T23:59:59.999Z", null))).split("\n"); + Arrays.asList( + Arrays.asList("ohnotruncateddata", 4, 1, null, 77, "wombat", "1955-01-21T01:02:03.342Z", null), + Arrays.asList("dog", 2, 123124.888, null, 9912, "goat", "2231-12-31T23:59:59.999Z", null) + ) + ).split("\n"); assertThat(result, arrayWithSize(2)); - assertEquals("ohnotruncatedd~|4 |1 |null |77 |wombat |" - + "1955-01-21T01:02:03.342Z|null ", result[0]); - assertEquals("dog |2 |123124.888 |null |9912 |goat |" - + "2231-12-31T23:59:59.999Z|null ", result[1]); + assertEquals( + "ohnotruncatedd~|4 |1 |null |77 |wombat |" + + "1955-01-21T01:02:03.342Z|null ", + result[0] + ); + assertEquals( + "dog |2 |123124.888 |null |9912 |goat |" + + "2231-12-31T23:59:59.999Z|null ", + result[1] + ); } /** * Ensure that our estimates are perfect in at least some cases. */ public void testEstimateSize() { - assertEquals(formatter.formatWithHeader(firstResponse.columns(), firstResponse.rows()).length(), - formatter.estimateSize(firstResponse.rows().size() + 2)); - assertEquals(formatter.formatWithoutHeader(firstResponse.rows()).length(), - formatter.estimateSize(firstResponse.rows().size())); + assertEquals( + formatter.formatWithHeader(firstResponse.columns(), firstResponse.rows()).length(), + formatter.estimateSize(firstResponse.rows().size() + 2) + ); + assertEquals(formatter.formatWithoutHeader(firstResponse.rows()).length(), formatter.estimateSize(firstResponse.rows().size())); } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/analysis/CancellationTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/analysis/CancellationTests.java index 0a9b7e6d66bf2..7b239afac3851 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/analysis/CancellationTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/analysis/CancellationTests.java @@ -67,18 +67,18 @@ public void testCancellationBeforeFieldCaps() throws InterruptedException { CountDownLatch countDownLatch = new CountDownLatch(1); SqlQueryRequest request = new SqlQueryRequestBuilder(client, SqlQueryAction.INSTANCE).query("SELECT foo FROM bar").request(); TransportSqlQueryAction.operation(planExecutor, task, request, new ActionListener<>() { - @Override - public void onResponse(SqlQueryResponse sqlSearchResponse) { - fail("Shouldn't be here"); - countDownLatch.countDown(); - } - - @Override - public void onFailure(Exception e) { - assertThat(e, instanceOf(TaskCancelledException.class)); - countDownLatch.countDown(); - } - }, "", mock(TransportService.class), mockClusterService); + @Override + public void onResponse(SqlQueryResponse sqlSearchResponse) { + fail("Shouldn't be here"); + countDownLatch.countDown(); + } + + @Override + public void onFailure(Exception e) { + assertThat(e, instanceOf(TaskCancelledException.class)); + countDownLatch.countDown(); + } + }, "", mock(TransportService.class), mockClusterService); countDownLatch.await(); verify(client, times(1)).settings(); verify(client, times(1)).threadPool(); @@ -86,12 +86,19 @@ public void onFailure(Exception e) { } private Map> fields(String[] indices) { - FieldCapabilities fooField = - new FieldCapabilities("foo", "integer", false, true, true, indices, null, null, emptyMap()); - FieldCapabilities categoryField = - new FieldCapabilities("event.category", "keyword", false, true, true, indices, null, null, emptyMap()); - FieldCapabilities timestampField = - new FieldCapabilities("@timestamp", "date", false, true, true, indices, null, null, emptyMap()); + FieldCapabilities fooField = new FieldCapabilities("foo", "integer", false, true, true, indices, null, null, emptyMap()); + FieldCapabilities categoryField = new FieldCapabilities( + "event.category", + "keyword", + false, + true, + true, + indices, + null, + null, + emptyMap() + ); + FieldCapabilities timestampField = new FieldCapabilities("@timestamp", "date", false, true, true, indices, null, null, emptyMap()); Map> fields = new HashMap<>(); fields.put(fooField.getName(), singletonMap(fooField.getName(), fooField)); fields.put(categoryField.getName(), singletonMap(categoryField.getName(), categoryField)); @@ -105,7 +112,7 @@ public void testCancellationBeforeSearch() throws InterruptedException { SqlQueryTask task = randomTask(); ClusterService mockClusterService = mockClusterService(); - String[] indices = new String[]{"endgame"}; + String[] indices = new String[] { "endgame" }; FieldCapabilitiesResponse fieldCapabilitiesResponse = mock(FieldCapabilitiesResponse.class); when(fieldCapabilitiesResponse.getIndices()).thenReturn(indices); @@ -118,12 +125,11 @@ public void testCancellationBeforeSearch() throws InterruptedException { return null; }).when(client).fieldCaps(any(), any()); - IndexResolver indexResolver = indexResolver(client); PlanExecutor planExecutor = new PlanExecutor(client, indexResolver, new NamedWriteableRegistry(Collections.emptyList())); CountDownLatch countDownLatch = new CountDownLatch(1); - SqlQueryRequest request = new SqlQueryRequestBuilder(client, SqlQueryAction.INSTANCE) - .query("SELECT foo FROM " + indices[0]).request(); + SqlQueryRequest request = new SqlQueryRequestBuilder(client, SqlQueryAction.INSTANCE).query("SELECT foo FROM " + indices[0]) + .request(); TransportSqlQueryAction.operation(planExecutor, task, request, new ActionListener<>() { @Override public void onResponse(SqlQueryResponse sqlSearchResponse) { @@ -151,7 +157,7 @@ public void testCancellationDuringSearch() throws InterruptedException { String nodeId = randomAlphaOfLength(10); ClusterService mockClusterService = mockClusterService(nodeId); - String[] indices = new String[]{"endgame"}; + String[] indices = new String[] { "endgame" }; // Emulation of field capabilities FieldCapabilitiesResponse fieldCapabilitiesResponse = mock(FieldCapabilitiesResponse.class); @@ -182,8 +188,8 @@ public void testCancellationDuringSearch() throws InterruptedException { IndexResolver indexResolver = indexResolver(client); PlanExecutor planExecutor = new PlanExecutor(client, indexResolver, new NamedWriteableRegistry(Collections.emptyList())); - SqlQueryRequest request = new SqlQueryRequestBuilder(client, SqlQueryAction.INSTANCE) - .query("SELECT foo FROM " + indices[0]).request(); + SqlQueryRequest request = new SqlQueryRequestBuilder(client, SqlQueryAction.INSTANCE).query("SELECT foo FROM " + indices[0]) + .request(); CountDownLatch countDownLatch = new CountDownLatch(1); TransportSqlQueryAction.operation(planExecutor, task, request, new ActionListener<>() { @Override diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/analysis/analyzer/FieldAttributeTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/analysis/analyzer/FieldAttributeTests.java index 1f101b8f93d08..f278d0d8d5373 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/analysis/analyzer/FieldAttributeTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/analysis/analyzer/FieldAttributeTests.java @@ -135,11 +135,12 @@ public void testAmbiguousExactKeyword() { assertThat(attr.name(), is("some.ambiguous")); assertThat(attr.dataType(), is(TEXT)); assertFalse(attr.getExactInfo().hasExact()); - assertThat(attr.getExactInfo().errorMsg(), - is("Multiple exact keyword candidates available for [ambiguous]; specify which one to use")); + assertThat( + attr.getExactInfo().errorMsg(), + is("Multiple exact keyword candidates available for [ambiguous]; specify which one to use") + ); QlIllegalArgumentException e = expectThrows(QlIllegalArgumentException.class, () -> attr.exactAttribute()); - assertThat(e.getMessage(), - is("Multiple exact keyword candidates available for [ambiguous]; specify which one to use")); + assertThat(e.getMessage(), is("Multiple exact keyword candidates available for [ambiguous]; specify which one to use")); } public void testNormalizedKeyword() { @@ -155,13 +156,14 @@ public void testDottedFieldPath() { } public void testDottedFieldPathDeeper() { - assertThat(error("some.dotted"), - is("Found 1 problem\nline 1:8: Cannot use field [some.dotted] type [object] only its subfields")); + assertThat(error("some.dotted"), is("Found 1 problem\nline 1:8: Cannot use field [some.dotted] type [object] only its subfields")); } public void testDottedFieldPathTypo() { - assertThat(error("some.dotted.fild"), - is("Found 1 problem\nline 1:8: Unknown column [some.dotted.fild], did you mean [some.dotted.field]?")); + assertThat( + error("some.dotted.fild"), + is("Found 1 problem\nline 1:8: Unknown column [some.dotted.fild], did you mean [some.dotted.field]?") + ); } public void testStarExpansionExcludesObjectAndUnsupportedTypes() { @@ -184,15 +186,17 @@ public void testFieldAmbiguity() { VerificationException ex = expectThrows(VerificationException.class, () -> plan("SELECT test.bar FROM test")); assertEquals( - "Found 1 problem\nline 1:8: Reference [test.bar] is ambiguous (to disambiguate use quotes or qualifiers); " - + "matches any of [line 1:22 [\"test\".\"bar\"], line 1:22 [\"test\".\"test.bar\"]]", - ex.getMessage()); + "Found 1 problem\nline 1:8: Reference [test.bar] is ambiguous (to disambiguate use quotes or qualifiers); " + + "matches any of [line 1:22 [\"test\".\"bar\"], line 1:22 [\"test\".\"test.bar\"]]", + ex.getMessage() + ); ex = expectThrows(VerificationException.class, () -> plan("SELECT test.test FROM test")); assertEquals( - "Found 1 problem\nline 1:8: Reference [test.test] is ambiguous (to disambiguate use quotes or qualifiers); " - + "matches any of [line 1:23 [\"test\".\"test\"], line 1:23 [\"test\".\"test.test\"]]", - ex.getMessage()); + "Found 1 problem\nline 1:8: Reference [test.test] is ambiguous (to disambiguate use quotes or qualifiers); " + + "matches any of [line 1:23 [\"test\".\"test\"], line 1:23 [\"test\".\"test.test\"]]", + ex.getMessage() + ); LogicalPlan plan = plan("SELECT test.test FROM test AS x"); assertThat(plan, instanceOf(Project.class)); @@ -248,36 +252,48 @@ public void testGroupByAmbiguity() { getIndexResult = IndexResolution.valid(index); analyzer = new Analyzer(SqlTestUtils.TEST_CFG, functionRegistry, getIndexResult, verifier); - VerificationException ex = expectThrows(VerificationException.class, - () -> plan("SELECT gender AS g, sum(salary) AS g FROM test GROUP BY g")); + VerificationException ex = expectThrows( + VerificationException.class, + () -> plan("SELECT gender AS g, sum(salary) AS g FROM test GROUP BY g") + ); assertEquals( - "Found 1 problem\nline 1:57: Reference [g] is ambiguous (to disambiguate use quotes or qualifiers); " + - "matches any of [line 1:8 [g], line 1:21 [g]]", - ex.getMessage()); - - ex = expectThrows(VerificationException.class, - () -> plan("SELECT gender AS g, max(salary) AS g, min(salary) AS g FROM test GROUP BY g")); + "Found 1 problem\nline 1:57: Reference [g] is ambiguous (to disambiguate use quotes or qualifiers); " + + "matches any of [line 1:8 [g], line 1:21 [g]]", + ex.getMessage() + ); + + ex = expectThrows( + VerificationException.class, + () -> plan("SELECT gender AS g, max(salary) AS g, min(salary) AS g FROM test GROUP BY g") + ); assertEquals( - "Found 1 problem\nline 1:75: Reference [g] is ambiguous (to disambiguate use quotes or qualifiers); " + - "matches any of [line 1:8 [g], line 1:21 [g], line 1:39 [g]]", - ex.getMessage()); - - ex = expectThrows(VerificationException.class, - () -> plan("SELECT gender AS g, last_name AS g, sum(salary) AS s FROM test GROUP BY g")); + "Found 1 problem\nline 1:75: Reference [g] is ambiguous (to disambiguate use quotes or qualifiers); " + + "matches any of [line 1:8 [g], line 1:21 [g], line 1:39 [g]]", + ex.getMessage() + ); + + ex = expectThrows( + VerificationException.class, + () -> plan("SELECT gender AS g, last_name AS g, sum(salary) AS s FROM test GROUP BY g") + ); assertEquals( - "Found 1 problem\nline 1:73: Reference [g] is ambiguous (to disambiguate use quotes or qualifiers); " + - "matches any of [line 1:8 [g], line 1:21 [g]]", - ex.getMessage()); - - ex = expectThrows(VerificationException.class, - () -> plan("SELECT gender AS g, last_name AS g, min(salary) AS m, max(salary) as m FROM test GROUP BY g, m")); + "Found 1 problem\nline 1:73: Reference [g] is ambiguous (to disambiguate use quotes or qualifiers); " + + "matches any of [line 1:8 [g], line 1:21 [g]]", + ex.getMessage() + ); + + ex = expectThrows( + VerificationException.class, + () -> plan("SELECT gender AS g, last_name AS g, min(salary) AS m, max(salary) as m FROM test GROUP BY g, m") + ); assertEquals( - "Found 2 problems\n" + - "line 1:91: Reference [g] is ambiguous (to disambiguate use quotes or qualifiers); " - + "matches any of [line 1:8 [g], line 1:21 [g]]\n" + - "line 1:94: Reference [m] is ambiguous (to disambiguate use quotes or qualifiers); " + "Found 2 problems\n" + + "line 1:91: Reference [g] is ambiguous (to disambiguate use quotes or qualifiers); " + + "matches any of [line 1:8 [g], line 1:21 [g]]\n" + + "line 1:94: Reference [m] is ambiguous (to disambiguate use quotes or qualifiers); " + "matches any of [line 1:37 [m], line 1:55 [m]]", - ex.getMessage()); + ex.getMessage() + ); } public void testFunctionOverNonExistingFieldAsArgumentAndSameAlias() throws Exception { @@ -286,8 +302,10 @@ public void testFunctionOverNonExistingFieldAsArgumentAndSameAlias() throws Exce getIndexResult = IndexResolution.valid(index); analyzer = new Analyzer(SqlTestUtils.TEST_CFG, functionRegistry, getIndexResult, verifier); - VerificationException ex = expectThrows(VerificationException.class, () -> - plan("SELECT sum(missing) AS missing FROM test WHERE missing = 0")); + VerificationException ex = expectThrows( + VerificationException.class, + () -> plan("SELECT sum(missing) AS missing FROM test WHERE missing = 0") + ); assertEquals("Found 1 problem\nline 1:12: Unknown column [missing]", ex.getMessage()); } @@ -297,8 +315,10 @@ public void testFunctionWithExpressionOverNonExistingFieldAsArgumentAndSameAlias getIndexResult = IndexResolution.valid(index); analyzer = new Analyzer(SqlTestUtils.TEST_CFG, functionRegistry, getIndexResult, verifier); - VerificationException ex = expectThrows(VerificationException.class, () -> - plan("SELECT LENGTH(CONCAT(missing, 'x')) + 1 AS missing FROM test WHERE missing = 0")); + VerificationException ex = expectThrows( + VerificationException.class, + () -> plan("SELECT LENGTH(CONCAT(missing, 'x')) + 1 AS missing FROM test WHERE missing = 0") + ); assertEquals("Found 1 problem\nline 1:22: Unknown column [missing]", ex.getMessage()); } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/analysis/analyzer/VerifierErrorMessagesTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/analysis/analyzer/VerifierErrorMessagesTests.java index 3df08bf42761b..b10063a9fc4fa 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/analysis/analyzer/VerifierErrorMessagesTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/analysis/analyzer/VerifierErrorMessagesTests.java @@ -26,6 +26,7 @@ import org.elasticsearch.xpack.sql.expression.predicate.conditional.NullIf; import org.elasticsearch.xpack.sql.parser.SqlParser; import org.elasticsearch.xpack.sql.stats.Metrics; + import java.util.Arrays; import java.util.HashMap; import java.util.LinkedHashMap; @@ -82,8 +83,10 @@ private IndexResolution incompatible() { Map incompatible = loadMapping("mapping-basic-incompatible.json"); assertNotEquals(basicMapping, incompatible); - IndexResolution resolution = IndexResolverTests.merge(new EsIndex("basic", basicMapping), - new EsIndex("incompatible", incompatible)); + IndexResolution resolution = IndexResolverTests.merge( + new EsIndex("basic", basicMapping), + new EsIndex("incompatible", incompatible) + ); assertTrue(resolution.isValid()); return resolution; } @@ -107,8 +110,10 @@ public void testNonBooleanFilter() { testData.put("DATETIME", List.of("date", "date + INTERVAL 1 DAY", "NOW()")); for (String typeName : testData.keySet()) { for (String exp : testData.get(typeName)) { - assertEquals("1:26: Condition expression needs to be boolean, found [" + typeName + "]", - error("SELECT * FROM test WHERE " + exp)); + assertEquals( + "1:26: Condition expression needs to be boolean, found [" + typeName + "]", + error("SELECT * FROM test WHERE " + exp) + ); } } } @@ -153,23 +158,32 @@ public void testFieldAliasTypeWithoutHierarchy() { } public void testMultipleColumnsWithWildcard1() { - assertEquals("1:14: Unknown column [a]\n" + - "line 1:17: Unknown column [b]\n" + - "line 1:22: Unknown column [c]\n" + - "line 1:25: Unknown column [tex], did you mean [text]?", error("SELECT bool, a, b.*, c, tex.* FROM test")); + assertEquals( + "1:14: Unknown column [a]\n" + + "line 1:17: Unknown column [b]\n" + + "line 1:22: Unknown column [c]\n" + + "line 1:25: Unknown column [tex], did you mean [text]?", + error("SELECT bool, a, b.*, c, tex.* FROM test") + ); } public void testMultipleColumnsWithWildcard2() { - assertEquals("1:8: Unknown column [tex], did you mean [text]?\n" + - "line 1:21: Unknown column [a]\n" + - "line 1:24: Unknown column [dat], did you mean [date]?\n" + - "line 1:31: Unknown column [c]", error("SELECT tex.*, bool, a, dat.*, c FROM test")); + assertEquals( + "1:8: Unknown column [tex], did you mean [text]?\n" + + "line 1:21: Unknown column [a]\n" + + "line 1:24: Unknown column [dat], did you mean [date]?\n" + + "line 1:31: Unknown column [c]", + error("SELECT tex.*, bool, a, dat.*, c FROM test") + ); } public void testMultipleColumnsWithWildcard3() { - assertEquals("1:8: Unknown column [ate], did you mean [date]?\n" + - "line 1:21: Unknown column [keyw], did you mean [keyword]?\n" + - "line 1:29: Unknown column [da], did you mean [date]?" , error("SELECT ate.*, bool, keyw.*, da FROM test")); + assertEquals( + "1:8: Unknown column [ate], did you mean [date]?\n" + + "line 1:21: Unknown column [keyw], did you mean [keyword]?\n" + + "line 1:29: Unknown column [da], did you mean [date]?", + error("SELECT ate.*, bool, keyw.*, da FROM test") + ); } public void testMisspelledColumn() { @@ -197,8 +211,10 @@ public void testMissingColumnInGroupBy() { } public void testInvalidOrdinalInOrderBy() { - assertEquals("1:56: Invalid ordinal [3] specified in [ORDER BY 2, 3] (valid range is [1, 2])", - error("SELECT bool, MIN(int) FROM test GROUP BY 1 ORDER BY 2, 3")); + assertEquals( + "1:56: Invalid ordinal [3] specified in [ORDER BY 2, 3] (valid range is [1, 2])", + error("SELECT bool, MIN(int) FROM test GROUP BY 1 ORDER BY 2, 3") + ); } public void testFilterOnUnknownColumn() { @@ -222,8 +238,10 @@ public void testMissingExtractSimilar() { } public void testMissingExtractSimilarMany() { - assertEquals("1:8: Unknown datetime field [DOP], did you mean any of [DOM, DOW, DOY, IDOW]?", - error("SELECT EXTRACT(DOP FROM date) FROM test")); + assertEquals( + "1:8: Unknown datetime field [DOP], did you mean any of [DOM, DOW, DOY, IDOW]?", + error("SELECT EXTRACT(DOP FROM date) FROM test") + ); } public void testExtractNonDateTime() { @@ -240,20 +258,31 @@ public void testDateTruncValidArgs() { } public void testDateTruncInvalidArgs() { - assertEquals("1:8: first argument of [DATE_TRUNC(int, date)] must be [string], found value [int] type [integer]", - error("SELECT DATE_TRUNC(int, date) FROM test")); - assertEquals("1:8: second argument of [DATE_TRUNC(keyword, keyword)] must be [date, datetime or an interval data type]," + - " found value [keyword] type [keyword]", error("SELECT DATE_TRUNC(keyword, keyword) FROM test")); - assertEquals("1:8: first argument of [DATE_TRUNC('invalid', keyword)] must be one of [MILLENNIUM, CENTURY, DECADE, " + "" + - "YEAR, QUARTER, MONTH, WEEK, DAY, HOUR, MINUTE, SECOND, MILLISECOND, MICROSECOND, NANOSECOND] " + - "or their aliases; found value ['invalid']", - error("SELECT DATE_TRUNC('invalid', keyword) FROM test")); - assertEquals("1:8: Unknown value ['millenioum'] for first argument of [DATE_TRUNC('millenioum', keyword)]; " + - "did you mean [millennium, millennia]?", - error("SELECT DATE_TRUNC('millenioum', keyword) FROM test")); - assertEquals("1:8: Unknown value ['yyyz'] for first argument of [DATE_TRUNC('yyyz', keyword)]; " + - "did you mean [yyyy, yy]?", - error("SELECT DATE_TRUNC('yyyz', keyword) FROM test")); + assertEquals( + "1:8: first argument of [DATE_TRUNC(int, date)] must be [string], found value [int] type [integer]", + error("SELECT DATE_TRUNC(int, date) FROM test") + ); + assertEquals( + "1:8: second argument of [DATE_TRUNC(keyword, keyword)] must be [date, datetime or an interval data type]," + + " found value [keyword] type [keyword]", + error("SELECT DATE_TRUNC(keyword, keyword) FROM test") + ); + assertEquals( + "1:8: first argument of [DATE_TRUNC('invalid', keyword)] must be one of [MILLENNIUM, CENTURY, DECADE, " + + "" + + "YEAR, QUARTER, MONTH, WEEK, DAY, HOUR, MINUTE, SECOND, MILLISECOND, MICROSECOND, NANOSECOND] " + + "or their aliases; found value ['invalid']", + error("SELECT DATE_TRUNC('invalid', keyword) FROM test") + ); + assertEquals( + "1:8: Unknown value ['millenioum'] for first argument of [DATE_TRUNC('millenioum', keyword)]; " + + "did you mean [millennium, millennia]?", + error("SELECT DATE_TRUNC('millenioum', keyword) FROM test") + ); + assertEquals( + "1:8: Unknown value ['yyyz'] for first argument of [DATE_TRUNC('yyyz', keyword)]; " + "did you mean [yyyy, yy]?", + error("SELECT DATE_TRUNC('yyyz', keyword) FROM test") + ); } public void testDateAddValidArgs() { @@ -266,22 +295,33 @@ public void testDateAddValidArgs() { } public void testDateAddInvalidArgs() { - assertEquals("1:8: first argument of [DATE_ADD(int, int, date)] must be [string], found value [int] type [integer]", - error("SELECT DATE_ADD(int, int, date) FROM test")); - assertEquals("1:8: second argument of [DATE_ADD(keyword, 1.2, date)] must be [integer], found value [1.2] " + - "type [double]", error("SELECT DATE_ADD(keyword, 1.2, date) FROM test")); - assertEquals("1:8: third argument of [DATE_ADD(keyword, int, keyword)] must be [date or datetime], found value [keyword] " + - "type [keyword]", error("SELECT DATE_ADD(keyword, int, keyword) FROM test")); - assertEquals("1:8: first argument of [DATE_ADD('invalid', int, date)] must be one of [YEAR, QUARTER, MONTH, DAYOFYEAR, " + - "DAY, WEEK, WEEKDAY, HOUR, MINUTE, SECOND, MILLISECOND, MICROSECOND, NANOSECOND] " + - "or their aliases; found value ['invalid']", - error("SELECT DATE_ADD('invalid', int, date) FROM test")); - assertEquals("1:8: Unknown value ['sacinds'] for first argument of [DATE_ADD('sacinds', int, date)]; " + - "did you mean [seconds, second]?", - error("SELECT DATE_ADD('sacinds', int, date) FROM test")); - assertEquals("1:8: Unknown value ['dz'] for first argument of [DATE_ADD('dz', int, date)]; " + - "did you mean [dd, dw, dy, d]?", - error("SELECT DATE_ADD('dz', int, date) FROM test")); + assertEquals( + "1:8: first argument of [DATE_ADD(int, int, date)] must be [string], found value [int] type [integer]", + error("SELECT DATE_ADD(int, int, date) FROM test") + ); + assertEquals( + "1:8: second argument of [DATE_ADD(keyword, 1.2, date)] must be [integer], found value [1.2] " + "type [double]", + error("SELECT DATE_ADD(keyword, 1.2, date) FROM test") + ); + assertEquals( + "1:8: third argument of [DATE_ADD(keyword, int, keyword)] must be [date or datetime], found value [keyword] " + + "type [keyword]", + error("SELECT DATE_ADD(keyword, int, keyword) FROM test") + ); + assertEquals( + "1:8: first argument of [DATE_ADD('invalid', int, date)] must be one of [YEAR, QUARTER, MONTH, DAYOFYEAR, " + + "DAY, WEEK, WEEKDAY, HOUR, MINUTE, SECOND, MILLISECOND, MICROSECOND, NANOSECOND] " + + "or their aliases; found value ['invalid']", + error("SELECT DATE_ADD('invalid', int, date) FROM test") + ); + assertEquals( + "1:8: Unknown value ['sacinds'] for first argument of [DATE_ADD('sacinds', int, date)]; " + "did you mean [seconds, second]?", + error("SELECT DATE_ADD('sacinds', int, date) FROM test") + ); + assertEquals( + "1:8: Unknown value ['dz'] for first argument of [DATE_ADD('dz', int, date)]; " + "did you mean [dd, dw, dy, d]?", + error("SELECT DATE_ADD('dz', int, date) FROM test") + ); } public void testDateDiffValidArgs() { @@ -294,39 +334,59 @@ public void testDateDiffValidArgs() { } public void testDateDiffInvalidArgs() { - assertEquals("1:8: first argument of [DATE_DIFF(int, date, date)] must be [string], found value [int] type [integer]", - error("SELECT DATE_DIFF(int, date, date) FROM test")); - assertEquals("1:8: second argument of [DATE_DIFF(keyword, keyword, date)] must be [date or datetime], found value [keyword] " + - "type [keyword]", error("SELECT DATE_DIFF(keyword, keyword, date) FROM test")); - assertEquals("1:8: third argument of [DATE_DIFF(keyword, date, keyword)] must be [date or datetime], found value [keyword] " + - "type [keyword]", error("SELECT DATE_DIFF(keyword, date, keyword) FROM test")); - assertEquals("1:8: first argument of [DATE_DIFF('invalid', int, date)] must be one of [YEAR, QUARTER, MONTH, DAYOFYEAR, " + - "DAY, WEEK, WEEKDAY, HOUR, MINUTE, SECOND, MILLISECOND, MICROSECOND, NANOSECOND] " + - "or their aliases; found value ['invalid']", - error("SELECT DATE_DIFF('invalid', int, date) FROM test")); - assertEquals("1:8: Unknown value ['sacinds'] for first argument of [DATE_DIFF('sacinds', int, date)]; " + - "did you mean [seconds, second]?", - error("SELECT DATE_DIFF('sacinds', int, date) FROM test")); - assertEquals("1:8: Unknown value ['dz'] for first argument of [DATE_DIFF('dz', int, date)]; " + - "did you mean [dd, dw, dy, d]?", - error("SELECT DATE_DIFF('dz', int, date) FROM test")); + assertEquals( + "1:8: first argument of [DATE_DIFF(int, date, date)] must be [string], found value [int] type [integer]", + error("SELECT DATE_DIFF(int, date, date) FROM test") + ); + assertEquals( + "1:8: second argument of [DATE_DIFF(keyword, keyword, date)] must be [date or datetime], found value [keyword] " + + "type [keyword]", + error("SELECT DATE_DIFF(keyword, keyword, date) FROM test") + ); + assertEquals( + "1:8: third argument of [DATE_DIFF(keyword, date, keyword)] must be [date or datetime], found value [keyword] " + + "type [keyword]", + error("SELECT DATE_DIFF(keyword, date, keyword) FROM test") + ); + assertEquals( + "1:8: first argument of [DATE_DIFF('invalid', int, date)] must be one of [YEAR, QUARTER, MONTH, DAYOFYEAR, " + + "DAY, WEEK, WEEKDAY, HOUR, MINUTE, SECOND, MILLISECOND, MICROSECOND, NANOSECOND] " + + "or their aliases; found value ['invalid']", + error("SELECT DATE_DIFF('invalid', int, date) FROM test") + ); + assertEquals( + "1:8: Unknown value ['sacinds'] for first argument of [DATE_DIFF('sacinds', int, date)]; " + "did you mean [seconds, second]?", + error("SELECT DATE_DIFF('sacinds', int, date) FROM test") + ); + assertEquals( + "1:8: Unknown value ['dz'] for first argument of [DATE_DIFF('dz', int, date)]; " + "did you mean [dd, dw, dy, d]?", + error("SELECT DATE_DIFF('dz', int, date) FROM test") + ); } public void testDatePartInvalidArgs() { - assertEquals("1:8: first argument of [DATE_PART(int, date)] must be [string], found value [int] type [integer]", - error("SELECT DATE_PART(int, date) FROM test")); - assertEquals("1:8: second argument of [DATE_PART(keyword, keyword)] must be [date or datetime], found value [keyword] " + - "type [keyword]", error("SELECT DATE_PART(keyword, keyword) FROM test")); - assertEquals("1:8: first argument of [DATE_PART('invalid', keyword)] must be one of [YEAR, QUARTER, MONTH, DAYOFYEAR, " + - "DAY, WEEK, WEEKDAY, HOUR, MINUTE, SECOND, MILLISECOND, MICROSECOND, NANOSECOND, TZOFFSET] " + - "or their aliases; found value ['invalid']", - error("SELECT DATE_PART('invalid', keyword) FROM test")); - assertEquals("1:8: Unknown value ['tzofset'] for first argument of [DATE_PART('tzofset', keyword)]; " + - "did you mean [tzoffset]?", - error("SELECT DATE_PART('tzofset', keyword) FROM test")); - assertEquals("1:8: Unknown value ['dz'] for first argument of [DATE_PART('dz', keyword)]; " + - "did you mean [dd, tz, dw, dy, d]?", - error("SELECT DATE_PART('dz', keyword) FROM test")); + assertEquals( + "1:8: first argument of [DATE_PART(int, date)] must be [string], found value [int] type [integer]", + error("SELECT DATE_PART(int, date) FROM test") + ); + assertEquals( + "1:8: second argument of [DATE_PART(keyword, keyword)] must be [date or datetime], found value [keyword] " + "type [keyword]", + error("SELECT DATE_PART(keyword, keyword) FROM test") + ); + assertEquals( + "1:8: first argument of [DATE_PART('invalid', keyword)] must be one of [YEAR, QUARTER, MONTH, DAYOFYEAR, " + + "DAY, WEEK, WEEKDAY, HOUR, MINUTE, SECOND, MILLISECOND, MICROSECOND, NANOSECOND, TZOFFSET] " + + "or their aliases; found value ['invalid']", + error("SELECT DATE_PART('invalid', keyword) FROM test") + ); + assertEquals( + "1:8: Unknown value ['tzofset'] for first argument of [DATE_PART('tzofset', keyword)]; " + "did you mean [tzoffset]?", + error("SELECT DATE_PART('tzofset', keyword) FROM test") + ); + assertEquals( + "1:8: Unknown value ['dz'] for first argument of [DATE_PART('dz', keyword)]; " + "did you mean [dd, tz, dw, dy, d]?", + error("SELECT DATE_PART('dz', keyword) FROM test") + ); } public void testDatePartValidArgs() { @@ -396,14 +456,18 @@ public void testValidDateTimeFunctionsOnTime() { } public void testInvalidDateTimeFunctionsOnTime() { - assertEquals("1:8: argument of [DAY_OF_YEAR(CAST(date AS TIME))] must be [date or datetime], " + - "found value [CAST(date AS TIME)] type [time]", - error("SELECT DAY_OF_YEAR(CAST(date AS TIME)) FROM test")); + assertEquals( + "1:8: argument of [DAY_OF_YEAR(CAST(date AS TIME))] must be [date or datetime], " + + "found value [CAST(date AS TIME)] type [time]", + error("SELECT DAY_OF_YEAR(CAST(date AS TIME)) FROM test") + ); } public void testGroupByOnTimeNotAllowed() { - assertEquals("1:36: Function [CAST(date AS TIME)] with data type [time] cannot be used for grouping", - error("SELECT count(*) FROM test GROUP BY CAST(date AS TIME)")); + assertEquals( + "1:36: Function [CAST(date AS TIME)] with data type [time] cannot be used for grouping", + error("SELECT count(*) FROM test GROUP BY CAST(date AS TIME)") + ); } public void testGroupByOnTimeWrappedWithScalar() { @@ -411,46 +475,55 @@ public void testGroupByOnTimeWrappedWithScalar() { } public void testHistogramOnTimeNotAllowed() { - assertEquals("1:8: first argument of [HISTOGRAM] must be [date, datetime or numeric], " + - "found value [CAST(date AS TIME)] type [time]", - error("SELECT HISTOGRAM(CAST(date AS TIME), INTERVAL 1 MONTH), COUNT(*) FROM test GROUP BY 1")); + assertEquals( + "1:8: first argument of [HISTOGRAM] must be [date, datetime or numeric], " + "found value [CAST(date AS TIME)] type [time]", + error("SELECT HISTOGRAM(CAST(date AS TIME), INTERVAL 1 MONTH), COUNT(*) FROM test GROUP BY 1") + ); } public void testSubtractFromInterval() { - assertEquals("1:8: Cannot subtract a datetime[CAST('2000-01-01' AS DATETIME)] " + - "from an interval[INTERVAL 1 MONTH]; do you mean the reverse?", - error("SELECT INTERVAL 1 MONTH - CAST('2000-01-01' AS DATETIME)")); + assertEquals( + "1:8: Cannot subtract a datetime[CAST('2000-01-01' AS DATETIME)] " + + "from an interval[INTERVAL 1 MONTH]; do you mean the reverse?", + error("SELECT INTERVAL 1 MONTH - CAST('2000-01-01' AS DATETIME)") + ); - assertEquals("1:8: Cannot subtract a time[CAST('12:23:56.789' AS TIME)] " + - "from an interval[INTERVAL 1 MONTH]; do you mean the reverse?", - error("SELECT INTERVAL 1 MONTH - CAST('12:23:56.789' AS TIME)")); + assertEquals( + "1:8: Cannot subtract a time[CAST('12:23:56.789' AS TIME)] " + "from an interval[INTERVAL 1 MONTH]; do you mean the reverse?", + error("SELECT INTERVAL 1 MONTH - CAST('12:23:56.789' AS TIME)") + ); } public void testAddIntervalAndNumberNotAllowed() { - assertEquals("1:8: [+] has arguments with incompatible types [INTERVAL_DAY] and [INTEGER]", - error("SELECT INTERVAL 1 DAY + 100")); - assertEquals("1:8: [+] has arguments with incompatible types [INTEGER] and [INTERVAL_DAY]", - error("SELECT 100 + INTERVAL 1 DAY")); + assertEquals("1:8: [+] has arguments with incompatible types [INTERVAL_DAY] and [INTEGER]", error("SELECT INTERVAL 1 DAY + 100")); + assertEquals("1:8: [+] has arguments with incompatible types [INTEGER] and [INTERVAL_DAY]", error("SELECT 100 + INTERVAL 1 DAY")); } public void testSubtractIntervalAndNumberNotAllowed() { - assertEquals("1:8: [-] has arguments with incompatible types [INTERVAL_MINUTE] and [DOUBLE]", - error("SELECT INTERVAL 10 MINUTE - 100.0")); - assertEquals("1:8: [-] has arguments with incompatible types [DOUBLE] and [INTERVAL_MINUTE]", - error("SELECT 100.0 - INTERVAL 10 MINUTE")); + assertEquals( + "1:8: [-] has arguments with incompatible types [INTERVAL_MINUTE] and [DOUBLE]", + error("SELECT INTERVAL 10 MINUTE - 100.0") + ); + assertEquals( + "1:8: [-] has arguments with incompatible types [DOUBLE] and [INTERVAL_MINUTE]", + error("SELECT 100.0 - INTERVAL 10 MINUTE") + ); } public void testMultiplyIntervalWithDecimalNotAllowed() { - assertEquals("1:8: [*] has arguments with incompatible types [INTERVAL_MONTH] and [DOUBLE]", - error("SELECT INTERVAL 1 MONTH * 1.234")); - assertEquals("1:8: [*] has arguments with incompatible types [DOUBLE] and [INTERVAL_MONTH]", - error("SELECT 1.234 * INTERVAL 1 MONTH")); + assertEquals( + "1:8: [*] has arguments with incompatible types [INTERVAL_MONTH] and [DOUBLE]", + error("SELECT INTERVAL 1 MONTH * 1.234") + ); + assertEquals( + "1:8: [*] has arguments with incompatible types [DOUBLE] and [INTERVAL_MONTH]", + error("SELECT 1.234 * INTERVAL 1 MONTH") + ); } public void testMultipleColumns() { // We get only one message back because the messages are grouped by the node that caused the issue - assertEquals("1:43: Unknown column [xxx]", - error("SELECT xxx FROM test GROUP BY DAY_oF_YEAR(xxx)")); + assertEquals("1:43: Unknown column [xxx]", error("SELECT xxx FROM test GROUP BY DAY_oF_YEAR(xxx)")); } // GROUP BY @@ -463,38 +536,46 @@ public void testGroupBySelectWithAliasOrderOnActualField() { } public void testGroupBySelectNonGrouped() { - assertEquals("1:8: Cannot use non-grouped column [text], expected [int]", - error("SELECT text, int FROM test GROUP BY int")); + assertEquals("1:8: Cannot use non-grouped column [text], expected [int]", error("SELECT text, int FROM test GROUP BY int")); } public void testGroupByFunctionSelectFieldFromGroupByFunction() { - assertEquals("1:8: Cannot use non-grouped column [int], expected [ABS(int)]", - error("SELECT int FROM test GROUP BY ABS(int)")); + assertEquals("1:8: Cannot use non-grouped column [int], expected [ABS(int)]", error("SELECT int FROM test GROUP BY ABS(int)")); } public void testGroupByOrderByNonGrouped() { - assertEquals("1:50: Cannot order by non-grouped column [bool], expected [text]", - error("SELECT MAX(int) FROM test GROUP BY text ORDER BY bool")); + assertEquals( + "1:50: Cannot order by non-grouped column [bool], expected [text]", + error("SELECT MAX(int) FROM test GROUP BY text ORDER BY bool") + ); } public void testGroupByOrderByNonGrouped_WithHaving() { - assertEquals("1:71: Cannot order by non-grouped column [bool], expected [text]", - error("SELECT MAX(int) FROM test GROUP BY text HAVING MAX(int) > 10 ORDER BY bool")); + assertEquals( + "1:71: Cannot order by non-grouped column [bool], expected [text]", + error("SELECT MAX(int) FROM test GROUP BY text HAVING MAX(int) > 10 ORDER BY bool") + ); } public void testGroupByOrdinalPointingToAggregate() { - assertEquals("1:42: Ordinal [2] in [GROUP BY 2] refers to an invalid argument, aggregate function [MIN(int)]", - error("SELECT bool, MIN(int) FROM test GROUP BY 2")); + assertEquals( + "1:42: Ordinal [2] in [GROUP BY 2] refers to an invalid argument, aggregate function [MIN(int)]", + error("SELECT bool, MIN(int) FROM test GROUP BY 2") + ); } public void testGroupByInvalidOrdinal() { - assertEquals("1:42: Invalid ordinal [3] specified in [GROUP BY 3] (valid range is [1, 2])", - error("SELECT bool, MIN(int) FROM test GROUP BY 3")); + assertEquals( + "1:42: Invalid ordinal [3] specified in [GROUP BY 3] (valid range is [1, 2])", + error("SELECT bool, MIN(int) FROM test GROUP BY 3") + ); } public void testGroupByNegativeOrdinal() { - assertEquals("1:42: Invalid ordinal [-1] specified in [GROUP BY -1] (valid range is [1, 2])", - error("SELECT bool, MIN(int) FROM test GROUP BY -1")); + assertEquals( + "1:42: Invalid ordinal [-1] specified in [GROUP BY -1] (valid range is [1, 2])", + error("SELECT bool, MIN(int) FROM test GROUP BY -1") + ); } public void testGroupByOrderByAliasedInSelectAllowed() { @@ -503,33 +584,44 @@ public void testGroupByOrderByAliasedInSelectAllowed() { } public void testGroupByOrderByScalarOverNonGrouped() { - assertEquals("1:50: Cannot order by non-grouped column [YEAR(date)], expected [text] or an aggregate function", - error("SELECT MAX(int) FROM test GROUP BY text ORDER BY YEAR(date)")); + assertEquals( + "1:50: Cannot order by non-grouped column [YEAR(date)], expected [text] or an aggregate function", + error("SELECT MAX(int) FROM test GROUP BY text ORDER BY YEAR(date)") + ); } public void testGroupByOrderByFieldFromGroupByFunction() { - assertEquals("1:54: Cannot order by non-grouped column [int], expected [ABS(int)]", - error("SELECT ABS(int) FROM test GROUP BY ABS(int) ORDER BY int")); - assertEquals("1:91: Cannot order by non-grouped column [c], expected [b] or an aggregate function", - error("SELECT b, abs, 2 as c FROM (SELECT bool as b, ABS(int) abs FROM test) GROUP BY b ORDER BY c")); + assertEquals( + "1:54: Cannot order by non-grouped column [int], expected [ABS(int)]", + error("SELECT ABS(int) FROM test GROUP BY ABS(int) ORDER BY int") + ); + assertEquals( + "1:91: Cannot order by non-grouped column [c], expected [b] or an aggregate function", + error("SELECT b, abs, 2 as c FROM (SELECT bool as b, ABS(int) abs FROM test) GROUP BY b ORDER BY c") + ); } public void testGroupByOrderByScalarOverNonGrouped_WithHaving() { - assertEquals("1:71: Cannot order by non-grouped column [YEAR(date)], expected [text] or an aggregate function", - error("SELECT MAX(int) FROM test GROUP BY text HAVING MAX(int) > 10 ORDER BY YEAR(date)")); + assertEquals( + "1:71: Cannot order by non-grouped column [YEAR(date)], expected [text] or an aggregate function", + error("SELECT MAX(int) FROM test GROUP BY text HAVING MAX(int) > 10 ORDER BY YEAR(date)") + ); } public void testGroupByHavingNonGrouped() { - assertEquals("1:48: Cannot use HAVING filter on non-aggregate [int]; use WHERE instead", - error("SELECT AVG(int) FROM test GROUP BY bool HAVING int > 10")); + assertEquals( + "1:48: Cannot use HAVING filter on non-aggregate [int]; use WHERE instead", + error("SELECT AVG(int) FROM test GROUP BY bool HAVING int > 10") + ); accept("SELECT AVG(int) FROM test GROUP BY bool HAVING AVG(int) > 2"); } public void testGroupByAggregate() { - assertEquals("1:36: Cannot use an aggregate [AVG] for grouping", - error("SELECT AVG(int) FROM test GROUP BY AVG(int)")); - assertEquals("1:65: Cannot use an aggregate [AVG] for grouping", - error("SELECT ROUND(AVG(int),2), AVG(int), COUNT(*) FROM test GROUP BY AVG(int) ORDER BY AVG(int)")); + assertEquals("1:36: Cannot use an aggregate [AVG] for grouping", error("SELECT AVG(int) FROM test GROUP BY AVG(int)")); + assertEquals( + "1:65: Cannot use an aggregate [AVG] for grouping", + error("SELECT ROUND(AVG(int),2), AVG(int), COUNT(*) FROM test GROUP BY AVG(int) ORDER BY AVG(int)") + ); } public void testStarOnNested() { @@ -537,78 +629,109 @@ public void testStarOnNested() { } public void testGroupByOnInexact() { - assertEquals("1:36: Field [text] of data type [text] cannot be used for grouping; " + - "No keyword/multi-field defined exact matches for [text]; define one or use MATCH/QUERY instead", - error("SELECT COUNT(*) FROM test GROUP BY text")); + assertEquals( + "1:36: Field [text] of data type [text] cannot be used for grouping; " + + "No keyword/multi-field defined exact matches for [text]; define one or use MATCH/QUERY instead", + error("SELECT COUNT(*) FROM test GROUP BY text") + ); } public void testGroupByOnNested() { - assertEquals("1:38: Grouping isn't (yet) compatible with nested fields [dep.dep_id]", - error("SELECT dep.dep_id FROM test GROUP BY dep.dep_id")); - assertEquals("1:8: Grouping isn't (yet) compatible with nested fields [dep.dep_id]", - error("SELECT dep.dep_id AS a FROM test GROUP BY a")); - assertEquals("1:8: Grouping isn't (yet) compatible with nested fields [dep.dep_id]", - error("SELECT dep.dep_id AS a FROM test GROUP BY 1")); - assertEquals("1:8: Grouping isn't (yet) compatible with nested fields [dep.dep_id, dep.start_date]", - error("SELECT dep.dep_id AS a, dep.start_date AS b FROM test GROUP BY 1, 2")); - assertEquals("1:8: Grouping isn't (yet) compatible with nested fields [dep.dep_id, dep.start_date]", - error("SELECT dep.dep_id AS a, dep.start_date AS b FROM test GROUP BY a, b")); + assertEquals( + "1:38: Grouping isn't (yet) compatible with nested fields [dep.dep_id]", + error("SELECT dep.dep_id FROM test GROUP BY dep.dep_id") + ); + assertEquals( + "1:8: Grouping isn't (yet) compatible with nested fields [dep.dep_id]", + error("SELECT dep.dep_id AS a FROM test GROUP BY a") + ); + assertEquals( + "1:8: Grouping isn't (yet) compatible with nested fields [dep.dep_id]", + error("SELECT dep.dep_id AS a FROM test GROUP BY 1") + ); + assertEquals( + "1:8: Grouping isn't (yet) compatible with nested fields [dep.dep_id, dep.start_date]", + error("SELECT dep.dep_id AS a, dep.start_date AS b FROM test GROUP BY 1, 2") + ); + assertEquals( + "1:8: Grouping isn't (yet) compatible with nested fields [dep.dep_id, dep.start_date]", + error("SELECT dep.dep_id AS a, dep.start_date AS b FROM test GROUP BY a, b") + ); } public void testHavingOnNested() { - assertEquals("1:51: HAVING isn't (yet) compatible with nested fields [dep.start_date]", - error("SELECT int FROM test GROUP BY int HAVING AVG(YEAR(dep.start_date)) > 1980")); - assertEquals("1:22: HAVING isn't (yet) compatible with nested fields [dep.start_date]", - error("SELECT int, AVG(YEAR(dep.start_date)) AS average FROM test GROUP BY int HAVING average > 1980")); - assertEquals("1:22: HAVING isn't (yet) compatible with nested fields [dep.start_date, dep.end_date]", - error("SELECT int, AVG(YEAR(dep.start_date)) AS a, MAX(MONTH(dep.end_date)) AS b " + - "FROM test GROUP BY int " + - "HAVING a > 1980 AND b < 10")); + assertEquals( + "1:51: HAVING isn't (yet) compatible with nested fields [dep.start_date]", + error("SELECT int FROM test GROUP BY int HAVING AVG(YEAR(dep.start_date)) > 1980") + ); + assertEquals( + "1:22: HAVING isn't (yet) compatible with nested fields [dep.start_date]", + error("SELECT int, AVG(YEAR(dep.start_date)) AS average FROM test GROUP BY int HAVING average > 1980") + ); + assertEquals( + "1:22: HAVING isn't (yet) compatible with nested fields [dep.start_date, dep.end_date]", + error( + "SELECT int, AVG(YEAR(dep.start_date)) AS a, MAX(MONTH(dep.end_date)) AS b " + + "FROM test GROUP BY int " + + "HAVING a > 1980 AND b < 10" + ) + ); } public void testWhereOnNested() { - assertEquals("1:33: WHERE isn't (yet) compatible with scalar functions on nested fields [dep.start_date]", - error("SELECT int FROM test WHERE YEAR(dep.start_date) + 10 > 0")); - assertEquals("1:13: WHERE isn't (yet) compatible with scalar functions on nested fields [dep.start_date]", - error("SELECT YEAR(dep.start_date) + 10 AS a FROM test WHERE int > 10 AND (int < 3 OR NOT (a > 5))")); + assertEquals( + "1:33: WHERE isn't (yet) compatible with scalar functions on nested fields [dep.start_date]", + error("SELECT int FROM test WHERE YEAR(dep.start_date) + 10 > 0") + ); + assertEquals( + "1:13: WHERE isn't (yet) compatible with scalar functions on nested fields [dep.start_date]", + error("SELECT YEAR(dep.start_date) + 10 AS a FROM test WHERE int > 10 AND (int < 3 OR NOT (a > 5))") + ); accept("SELECT int FROM test WHERE dep.start_date > '2020-01-30'::date AND (int > 10 OR dep.end_date IS NULL)"); - accept("SELECT int FROM test WHERE dep.start_date > '2020-01-30'::date AND (int > 10 OR dep.end_date IS NULL) " + - "OR NOT(dep.start_date >= '2020-01-01')"); + accept( + "SELECT int FROM test WHERE dep.start_date > '2020-01-30'::date AND (int > 10 OR dep.end_date IS NULL) " + + "OR NOT(dep.start_date >= '2020-01-01')" + ); String operator = randomFrom("<", "<="); - assertEquals("1:46: WHERE isn't (yet) compatible with scalar functions on nested fields [dep.location]", - error("SELECT geo_shape FROM test " + - "WHERE ST_Distance(dep.location, ST_WKTToSQL('point (10 20)')) " + operator + " 25")); + assertEquals( + "1:46: WHERE isn't (yet) compatible with scalar functions on nested fields [dep.location]", + error("SELECT geo_shape FROM test " + "WHERE ST_Distance(dep.location, ST_WKTToSQL('point (10 20)')) " + operator + " 25") + ); } public void testOrderByOnNested() { - assertEquals("1:36: ORDER BY isn't (yet) compatible with scalar functions on nested fields [dep.start_date]", - error("SELECT int FROM test ORDER BY YEAR(dep.start_date) + 10")); - assertEquals("1:13: ORDER BY isn't (yet) compatible with scalar functions on nested fields [dep.start_date]", - error("SELECT YEAR(dep.start_date) + 10 FROM test ORDER BY 1")); - assertEquals("1:13: ORDER BY isn't (yet) compatible with scalar functions on nested fields " + - "[dep.start_date, dep.end_date]", - error("SELECT YEAR(dep.start_date) + 10 AS a, MONTH(dep.end_date) - 10 as b FROM test ORDER BY 1, 2")); + assertEquals( + "1:36: ORDER BY isn't (yet) compatible with scalar functions on nested fields [dep.start_date]", + error("SELECT int FROM test ORDER BY YEAR(dep.start_date) + 10") + ); + assertEquals( + "1:13: ORDER BY isn't (yet) compatible with scalar functions on nested fields [dep.start_date]", + error("SELECT YEAR(dep.start_date) + 10 FROM test ORDER BY 1") + ); + assertEquals( + "1:13: ORDER BY isn't (yet) compatible with scalar functions on nested fields " + "[dep.start_date, dep.end_date]", + error("SELECT YEAR(dep.start_date) + 10 AS a, MONTH(dep.end_date) - 10 as b FROM test ORDER BY 1, 2") + ); accept("SELECT int FROM test ORDER BY dep.start_date, dep.end_date"); } public void testGroupByScalarFunctionWithAggOnTarget() { - assertEquals("1:31: Cannot use an aggregate [AVG] for grouping", - error("SELECT int FROM test GROUP BY AVG(int) + 2")); + assertEquals("1:31: Cannot use an aggregate [AVG] for grouping", error("SELECT int FROM test GROUP BY AVG(int) + 2")); } public void testUnsupportedType() { - assertEquals("1:8: Cannot use field [unsupported] with unsupported type [ip_range]", - error("SELECT unsupported FROM test")); + assertEquals("1:8: Cannot use field [unsupported] with unsupported type [ip_range]", error("SELECT unsupported FROM test")); } public void testUnsupportedStarExpansion() { - assertEquals("1:8: Cannot use field [unsupported] with unsupported type [ip_range]", - error("SELECT unsupported.* FROM test")); + assertEquals("1:8: Cannot use field [unsupported] with unsupported type [ip_range]", error("SELECT unsupported.* FROM test")); } public void testUnsupportedTypeInFilter() { - assertEquals("1:26: Cannot use field [unsupported] with unsupported type [ip_range]", - error("SELECT * FROM test WHERE unsupported > 1")); + assertEquals( + "1:26: Cannot use field [unsupported] with unsupported type [ip_range]", + error("SELECT * FROM test WHERE unsupported > 1") + ); } public void testValidRootFieldWithUnsupportedChildren() { @@ -616,41 +739,54 @@ public void testValidRootFieldWithUnsupportedChildren() { } public void testUnsupportedTypeInHierarchy() { - assertEquals("1:8: Cannot use field [x.y.z.w] with unsupported type [foobar] in hierarchy (field [y])", - error("SELECT x.y.z.w FROM test")); - assertEquals("1:8: Cannot use field [x.y.z.v] with unsupported type [foobar] in hierarchy (field [y])", - error("SELECT x.y.z.v FROM test")); - assertEquals("1:8: Cannot use field [x.y.z] with unsupported type [foobar] in hierarchy (field [y])", - error("SELECT x.y.z.* FROM test")); + assertEquals( + "1:8: Cannot use field [x.y.z.w] with unsupported type [foobar] in hierarchy (field [y])", + error("SELECT x.y.z.w FROM test") + ); + assertEquals( + "1:8: Cannot use field [x.y.z.v] with unsupported type [foobar] in hierarchy (field [y])", + error("SELECT x.y.z.v FROM test") + ); + assertEquals( + "1:8: Cannot use field [x.y.z] with unsupported type [foobar] in hierarchy (field [y])", + error("SELECT x.y.z.* FROM test") + ); assertEquals("1:8: Cannot use field [x.y] with unsupported type [foobar]", error("SELECT x.y FROM test")); } public void testTermEqualityOnInexact() { - assertEquals("1:26: [text = 'value'] cannot operate on first argument field of data type [text]: " + - "No keyword/multi-field defined exact matches for [text]; define one or use MATCH/QUERY instead", - error("SELECT * FROM test WHERE text = 'value'")); + assertEquals( + "1:26: [text = 'value'] cannot operate on first argument field of data type [text]: " + + "No keyword/multi-field defined exact matches for [text]; define one or use MATCH/QUERY instead", + error("SELECT * FROM test WHERE text = 'value'") + ); } public void testTermEqualityOnAmbiguous() { - assertEquals("1:26: [some.ambiguous = 'value'] cannot operate on first argument field of data type [text]: " + - "Multiple exact keyword candidates available for [ambiguous]; specify which one to use", - error("SELECT * FROM test WHERE some.ambiguous = 'value'")); + assertEquals( + "1:26: [some.ambiguous = 'value'] cannot operate on first argument field of data type [text]: " + + "Multiple exact keyword candidates available for [ambiguous]; specify which one to use", + error("SELECT * FROM test WHERE some.ambiguous = 'value'") + ); } public void testUnsupportedTypeInFunction() { - assertEquals("1:12: Cannot use field [unsupported] with unsupported type [ip_range]", - error("SELECT ABS(unsupported) FROM test")); + assertEquals("1:12: Cannot use field [unsupported] with unsupported type [ip_range]", error("SELECT ABS(unsupported) FROM test")); } public void testUnsupportedTypeInOrder() { - assertEquals("1:29: Cannot use field [unsupported] with unsupported type [ip_range]", - error("SELECT * FROM test ORDER BY unsupported")); + assertEquals( + "1:29: Cannot use field [unsupported] with unsupported type [ip_range]", + error("SELECT * FROM test ORDER BY unsupported") + ); } public void testInexactFieldInOrder() { - assertEquals("1:29: ORDER BY cannot be applied to field of data type [text]: " + - "No keyword/multi-field defined exact matches for [text]; define one or use MATCH/QUERY instead", - error("SELECT * FROM test ORDER BY text")); + assertEquals( + "1:29: ORDER BY cannot be applied to field of data type [text]: " + + "No keyword/multi-field defined exact matches for [text]; define one or use MATCH/QUERY instead", + error("SELECT * FROM test ORDER BY text") + ); } public void testGroupByOrderByAggregate() { @@ -666,13 +802,17 @@ public void testGroupByOrderByAggAndGroupedColumn() { } public void testGroupByOrderByNonAggAndNonGroupedColumn() { - assertEquals("1:44: Cannot order by non-grouped column [bool], expected [int]", - error("SELECT int FROM test GROUP BY int ORDER BY bool")); + assertEquals( + "1:44: Cannot order by non-grouped column [bool], expected [int]", + error("SELECT int FROM test GROUP BY int ORDER BY bool") + ); } public void testGroupByOrderByScore() { - assertEquals("1:44: Cannot order by non-grouped column [SCORE()], expected [int] or an aggregate function", - error("SELECT int FROM test GROUP BY int ORDER BY SCORE()")); + assertEquals( + "1:44: Cannot order by non-grouped column [SCORE()], expected [int] or an aggregate function", + error("SELECT int FROM test GROUP BY int ORDER BY SCORE()") + ); } public void testGroupByWithRepeatedAliases() { @@ -681,162 +821,231 @@ public void testGroupByWithRepeatedAliases() { } public void testHavingOnColumn() { - assertEquals("1:42: Cannot use HAVING filter on non-aggregate [int]; use WHERE instead", - error("SELECT int FROM test GROUP BY int HAVING int > 2")); + assertEquals( + "1:42: Cannot use HAVING filter on non-aggregate [int]; use WHERE instead", + error("SELECT int FROM test GROUP BY int HAVING int > 2") + ); } public void testHavingOnScalar() { - assertEquals("1:42: Cannot use HAVING filter on non-aggregate [int]; use WHERE instead", - error("SELECT int FROM test GROUP BY int HAVING 2 < ABS(int)")); + assertEquals( + "1:42: Cannot use HAVING filter on non-aggregate [int]; use WHERE instead", + error("SELECT int FROM test GROUP BY int HAVING 2 < ABS(int)") + ); } public void testInWithIncompatibleDataTypes() { - assertEquals("1:8: 1st argument of ['2000-02-02T00:00:00Z'::date IN ('02:02:02Z'::time)] must be [date], " + - "found value ['02:02:02Z'::time] type [time]", - error("SELECT '2000-02-02T00:00:00Z'::date IN ('02:02:02Z'::time)")); + assertEquals( + "1:8: 1st argument of ['2000-02-02T00:00:00Z'::date IN ('02:02:02Z'::time)] must be [date], " + + "found value ['02:02:02Z'::time] type [time]", + error("SELECT '2000-02-02T00:00:00Z'::date IN ('02:02:02Z'::time)") + ); } public void testInWithFieldInListOfValues() { - assertEquals("1:26: Comparisons against fields are not (currently) supported; offender [int] in [int IN (1, int)]", - error("SELECT * FROM test WHERE int IN (1, int)")); + assertEquals( + "1:26: Comparisons against fields are not (currently) supported; offender [int] in [int IN (1, int)]", + error("SELECT * FROM test WHERE int IN (1, int)") + ); } public void testInOnFieldTextWithNoKeyword() { - assertEquals("1:26: [IN] cannot operate on field of data type [text]: " + - "No keyword/multi-field defined exact matches for [text]; define one or use MATCH/QUERY instead", - error("SELECT * FROM test WHERE text IN ('foo', 'bar')")); + assertEquals( + "1:26: [IN] cannot operate on field of data type [text]: " + + "No keyword/multi-field defined exact matches for [text]; define one or use MATCH/QUERY instead", + error("SELECT * FROM test WHERE text IN ('foo', 'bar')") + ); } public void testNotSupportedAggregateOnDate() { - assertEquals("1:8: argument of [AVG(date)] must be [numeric], found value [date] type [datetime]", - error("SELECT AVG(date) FROM test")); + assertEquals( + "1:8: argument of [AVG(date)] must be [numeric], found value [date] type [datetime]", + error("SELECT AVG(date) FROM test") + ); } public void testInvalidTypeForStringFunction_WithOneArgString() { - assertEquals("1:8: argument of [LENGTH(1)] must be [string], found value [1] type [integer]", - error("SELECT LENGTH(1)")); + assertEquals("1:8: argument of [LENGTH(1)] must be [string], found value [1] type [integer]", error("SELECT LENGTH(1)")); } public void testInvalidTypeForStringFunction_WithOneArgNumeric() { String functionName = randomFrom(Arrays.asList(Char.class, Space.class)).getSimpleName().toUpperCase(Locale.ROOT); - assertEquals("1:8: argument of [" + functionName + "('foo')] must be [integer], found value ['foo'] type [keyword]", - error("SELECT " + functionName + "('foo')")); - assertEquals("1:8: argument of [" + functionName + "(1.2)] must be [integer], found value [1.2] type [double]", - error("SELECT " + functionName + "(1.2)")); + assertEquals( + "1:8: argument of [" + functionName + "('foo')] must be [integer], found value ['foo'] type [keyword]", + error("SELECT " + functionName + "('foo')") + ); + assertEquals( + "1:8: argument of [" + functionName + "(1.2)] must be [integer], found value [1.2] type [double]", + error("SELECT " + functionName + "(1.2)") + ); } public void testInvalidTypeForNestedStringFunctions_WithOneArg() { - assertEquals("1:15: argument of [SPACE('foo')] must be [integer], found value ['foo'] type [keyword]", - error("SELECT LENGTH(SPACE('foo'))")); - assertEquals("1:15: argument of [SPACE(1.2)] must be [integer], found value [1.2] type [double]", - error("SELECT LENGTH(SPACE(1.2))")); + assertEquals( + "1:15: argument of [SPACE('foo')] must be [integer], found value ['foo'] type [keyword]", + error("SELECT LENGTH(SPACE('foo'))") + ); + assertEquals( + "1:15: argument of [SPACE(1.2)] must be [integer], found value [1.2] type [double]", + error("SELECT LENGTH(SPACE(1.2))") + ); } public void testInvalidTypeForNumericFunction_WithOneArg() { - assertEquals("1:8: argument of [COS('foo')] must be [numeric], found value ['foo'] type [keyword]", - error("SELECT COS('foo')")); + assertEquals("1:8: argument of [COS('foo')] must be [numeric], found value ['foo'] type [keyword]", error("SELECT COS('foo')")); } public void testInvalidTypeForBooleanFunction_WithOneArg() { - assertEquals("1:8: argument of [NOT 'foo'] must be [boolean], found value ['foo'] type [keyword]", - error("SELECT NOT 'foo'")); + assertEquals("1:8: argument of [NOT 'foo'] must be [boolean], found value ['foo'] type [keyword]", error("SELECT NOT 'foo'")); } public void testInvalidTypeForStringFunction_WithTwoArgs() { - assertEquals("1:8: first argument of [CONCAT(1, 'bar')] must be [string], found value [1] type [integer]", - error("SELECT CONCAT(1, 'bar')")); - assertEquals("1:8: second argument of [CONCAT('foo', 2)] must be [string], found value [2] type [integer]", - error("SELECT CONCAT('foo', 2)")); + assertEquals( + "1:8: first argument of [CONCAT(1, 'bar')] must be [string], found value [1] type [integer]", + error("SELECT CONCAT(1, 'bar')") + ); + assertEquals( + "1:8: second argument of [CONCAT('foo', 2)] must be [string], found value [2] type [integer]", + error("SELECT CONCAT('foo', 2)") + ); } public void testInvalidTypeForNumericFunction_WithTwoArgs() { String functionName = randomFrom(Arrays.asList(Round.class, Truncate.class)).getSimpleName().toUpperCase(Locale.ROOT); - assertEquals("1:8: first argument of [" + functionName + "('foo', 2)] must be [numeric], found value ['foo'] type [keyword]", - error("SELECT " + functionName + "('foo', 2)")); - assertEquals("1:8: second argument of [" + functionName + "(1.2, 'bar')] must be [integer], found value ['bar'] type [keyword]", - error("SELECT " + functionName + "(1.2, 'bar')")); - assertEquals("1:8: second argument of [" + functionName + "(1.2, 3.4)] must be [integer], found value [3.4] type [double]", - error("SELECT " + functionName + "(1.2, 3.4)")); + assertEquals( + "1:8: first argument of [" + functionName + "('foo', 2)] must be [numeric], found value ['foo'] type [keyword]", + error("SELECT " + functionName + "('foo', 2)") + ); + assertEquals( + "1:8: second argument of [" + functionName + "(1.2, 'bar')] must be [integer], found value ['bar'] type [keyword]", + error("SELECT " + functionName + "(1.2, 'bar')") + ); + assertEquals( + "1:8: second argument of [" + functionName + "(1.2, 3.4)] must be [integer], found value [3.4] type [double]", + error("SELECT " + functionName + "(1.2, 3.4)") + ); } public void testInvalidTypeForBooleanFuntion_WithTwoArgs() { - assertEquals("1:8: first argument of [1 OR true] must be [boolean], found value [1] type [integer]", - error("SELECT 1 OR true")); - assertEquals("1:8: second argument of [true OR 2] must be [boolean], found value [2] type [integer]", - error("SELECT true OR 2")); + assertEquals("1:8: first argument of [1 OR true] must be [boolean], found value [1] type [integer]", error("SELECT 1 OR true")); + assertEquals("1:8: second argument of [true OR 2] must be [boolean], found value [2] type [integer]", error("SELECT true OR 2")); } public void testInvalidTypeForReplace() { - assertEquals("1:8: first argument of [REPLACE(1, 'foo', 'bar')] must be [string], found value [1] type [integer]", - error("SELECT REPLACE(1, 'foo', 'bar')")); - assertEquals("1:8: [REPLACE(text, 'foo', 'bar')] cannot operate on first argument field of data type [text]: " + - "No keyword/multi-field defined exact matches for [text]; define one or use MATCH/QUERY instead", - error("SELECT REPLACE(text, 'foo', 'bar') FROM test")); + assertEquals( + "1:8: first argument of [REPLACE(1, 'foo', 'bar')] must be [string], found value [1] type [integer]", + error("SELECT REPLACE(1, 'foo', 'bar')") + ); + assertEquals( + "1:8: [REPLACE(text, 'foo', 'bar')] cannot operate on first argument field of data type [text]: " + + "No keyword/multi-field defined exact matches for [text]; define one or use MATCH/QUERY instead", + error("SELECT REPLACE(text, 'foo', 'bar') FROM test") + ); - assertEquals("1:8: second argument of [REPLACE('foo', 2, 'bar')] must be [string], found value [2] type [integer]", - error("SELECT REPLACE('foo', 2, 'bar')")); - assertEquals("1:8: [REPLACE('foo', text, 'bar')] cannot operate on second argument field of data type [text]: " + - "No keyword/multi-field defined exact matches for [text]; define one or use MATCH/QUERY instead", - error("SELECT REPLACE('foo', text, 'bar') FROM test")); + assertEquals( + "1:8: second argument of [REPLACE('foo', 2, 'bar')] must be [string], found value [2] type [integer]", + error("SELECT REPLACE('foo', 2, 'bar')") + ); + assertEquals( + "1:8: [REPLACE('foo', text, 'bar')] cannot operate on second argument field of data type [text]: " + + "No keyword/multi-field defined exact matches for [text]; define one or use MATCH/QUERY instead", + error("SELECT REPLACE('foo', text, 'bar') FROM test") + ); - assertEquals("1:8: third argument of [REPLACE('foo', 'bar', 3)] must be [string], found value [3] type [integer]", - error("SELECT REPLACE('foo', 'bar', 3)")); - assertEquals("1:8: [REPLACE('foo', 'bar', text)] cannot operate on third argument field of data type [text]: " + - "No keyword/multi-field defined exact matches for [text]; define one or use MATCH/QUERY instead", - error("SELECT REPLACE('foo', 'bar', text) FROM test")); + assertEquals( + "1:8: third argument of [REPLACE('foo', 'bar', 3)] must be [string], found value [3] type [integer]", + error("SELECT REPLACE('foo', 'bar', 3)") + ); + assertEquals( + "1:8: [REPLACE('foo', 'bar', text)] cannot operate on third argument field of data type [text]: " + + "No keyword/multi-field defined exact matches for [text]; define one or use MATCH/QUERY instead", + error("SELECT REPLACE('foo', 'bar', text) FROM test") + ); } public void testInvalidTypeForSubString() { - assertEquals("1:8: first argument of [SUBSTRING(1, 2, 3)] must be [string], found value [1] type [integer]", - error("SELECT SUBSTRING(1, 2, 3)")); - assertEquals("1:8: [SUBSTRING(text, 2, 3)] cannot operate on first argument field of data type [text]: " + - "No keyword/multi-field defined exact matches for [text]; define one or use MATCH/QUERY instead", - error("SELECT SUBSTRING(text, 2, 3) FROM test")); + assertEquals( + "1:8: first argument of [SUBSTRING(1, 2, 3)] must be [string], found value [1] type [integer]", + error("SELECT SUBSTRING(1, 2, 3)") + ); + assertEquals( + "1:8: [SUBSTRING(text, 2, 3)] cannot operate on first argument field of data type [text]: " + + "No keyword/multi-field defined exact matches for [text]; define one or use MATCH/QUERY instead", + error("SELECT SUBSTRING(text, 2, 3) FROM test") + ); - assertEquals("1:8: second argument of [SUBSTRING('foo', 'bar', 3)] must be [integer], found value ['bar'] type [keyword]", - error("SELECT SUBSTRING('foo', 'bar', 3)")); - assertEquals("1:8: second argument of [SUBSTRING('foo', 1.2, 3)] must be [integer], found value [1.2] type [double]", - error("SELECT SUBSTRING('foo', 1.2, 3)")); + assertEquals( + "1:8: second argument of [SUBSTRING('foo', 'bar', 3)] must be [integer], found value ['bar'] type [keyword]", + error("SELECT SUBSTRING('foo', 'bar', 3)") + ); + assertEquals( + "1:8: second argument of [SUBSTRING('foo', 1.2, 3)] must be [integer], found value [1.2] type [double]", + error("SELECT SUBSTRING('foo', 1.2, 3)") + ); - assertEquals("1:8: third argument of [SUBSTRING('foo', 2, 'bar')] must be [integer], found value ['bar'] type [keyword]", - error("SELECT SUBSTRING('foo', 2, 'bar')")); - assertEquals("1:8: third argument of [SUBSTRING('foo', 2, 3.4)] must be [integer], found value [3.4] type [double]", - error("SELECT SUBSTRING('foo', 2, 3.4)")); + assertEquals( + "1:8: third argument of [SUBSTRING('foo', 2, 'bar')] must be [integer], found value ['bar'] type [keyword]", + error("SELECT SUBSTRING('foo', 2, 'bar')") + ); + assertEquals( + "1:8: third argument of [SUBSTRING('foo', 2, 3.4)] must be [integer], found value [3.4] type [double]", + error("SELECT SUBSTRING('foo', 2, 3.4)") + ); } public void testInvalidTypeForFunction_WithFourArgs() { - assertEquals("1:8: first argument of [INSERT(1, 1, 2, 'new')] must be [string], found value [1] type [integer]", - error("SELECT INSERT(1, 1, 2, 'new')")); - assertEquals("1:8: second argument of [INSERT('text', 'foo', 2, 'new')] must be [numeric], found value ['foo'] type [keyword]", - error("SELECT INSERT('text', 'foo', 2, 'new')")); - assertEquals("1:8: third argument of [INSERT('text', 1, 'bar', 'new')] must be [numeric], found value ['bar'] type [keyword]", - error("SELECT INSERT('text', 1, 'bar', 'new')")); - assertEquals("1:8: fourth argument of [INSERT('text', 1, 2, 3)] must be [string], found value [3] type [integer]", - error("SELECT INSERT('text', 1, 2, 3)")); + assertEquals( + "1:8: first argument of [INSERT(1, 1, 2, 'new')] must be [string], found value [1] type [integer]", + error("SELECT INSERT(1, 1, 2, 'new')") + ); + assertEquals( + "1:8: second argument of [INSERT('text', 'foo', 2, 'new')] must be [numeric], found value ['foo'] type [keyword]", + error("SELECT INSERT('text', 'foo', 2, 'new')") + ); + assertEquals( + "1:8: third argument of [INSERT('text', 1, 'bar', 'new')] must be [numeric], found value ['bar'] type [keyword]", + error("SELECT INSERT('text', 1, 'bar', 'new')") + ); + assertEquals( + "1:8: fourth argument of [INSERT('text', 1, 2, 3)] must be [string], found value [3] type [integer]", + error("SELECT INSERT('text', 1, 2, 3)") + ); } public void testInvalidTypeForLikeMatch() { - assertEquals("1:26: [text LIKE 'foo'] cannot operate on field of data type [text]: " + - "No keyword/multi-field defined exact matches for [text]; define one or use MATCH/QUERY instead", - error("SELECT * FROM test WHERE text LIKE 'foo'")); + assertEquals( + "1:26: [text LIKE 'foo'] cannot operate on field of data type [text]: " + + "No keyword/multi-field defined exact matches for [text]; define one or use MATCH/QUERY instead", + error("SELECT * FROM test WHERE text LIKE 'foo'") + ); } public void testInvalidTypeForRLikeMatch() { - assertEquals("1:26: [text RLIKE 'foo'] cannot operate on field of data type [text]: " + - "No keyword/multi-field defined exact matches for [text]; define one or use MATCH/QUERY instead", - error("SELECT * FROM test WHERE text RLIKE 'foo'")); + assertEquals( + "1:26: [text RLIKE 'foo'] cannot operate on field of data type [text]: " + + "No keyword/multi-field defined exact matches for [text]; define one or use MATCH/QUERY instead", + error("SELECT * FROM test WHERE text RLIKE 'foo'") + ); } public void testMatchAndQueryFunctionsNotAllowedInSelect() { - assertEquals("1:8: Cannot use MATCH() or QUERY() full-text search functions in the SELECT clause", - error("SELECT MATCH(text, 'foo') FROM test")); - assertEquals("1:8: Cannot use MATCH() or QUERY() full-text search functions in the SELECT clause", - error("SELECT MATCH(text, 'foo') AS fullTextSearch FROM test")); - assertEquals("1:38: Cannot use MATCH() or QUERY() full-text search functions in the SELECT clause", - error("SELECT int > 10 AND (bool = false OR QUERY('foo*')) AS fullTextSearch FROM test")); - assertEquals("1:8: Cannot use MATCH() or QUERY() full-text search functions in the SELECT clause\n" + - "line 1:28: Cannot use MATCH() or QUERY() full-text search functions in the SELECT clause", - error("SELECT MATCH(text, 'foo'), MATCH(text, 'bar') FROM test")); + assertEquals( + "1:8: Cannot use MATCH() or QUERY() full-text search functions in the SELECT clause", + error("SELECT MATCH(text, 'foo') FROM test") + ); + assertEquals( + "1:8: Cannot use MATCH() or QUERY() full-text search functions in the SELECT clause", + error("SELECT MATCH(text, 'foo') AS fullTextSearch FROM test") + ); + assertEquals( + "1:38: Cannot use MATCH() or QUERY() full-text search functions in the SELECT clause", + error("SELECT int > 10 AND (bool = false OR QUERY('foo*')) AS fullTextSearch FROM test") + ); + assertEquals( + "1:8: Cannot use MATCH() or QUERY() full-text search functions in the SELECT clause\n" + + "line 1:28: Cannot use MATCH() or QUERY() full-text search functions in the SELECT clause", + error("SELECT MATCH(text, 'foo'), MATCH(text, 'bar') FROM test") + ); accept("SELECT * FROM test WHERE MATCH(text, 'foo')"); } @@ -850,86 +1059,111 @@ public void testWildcardInIncompatibleMappings() { public void testMismatchedFieldInIncompatibleMappings() { assertEquals( - "1:8: Cannot use field [emp_no] due to ambiguities being mapped as [2] incompatible types: " - + "[integer] in [basic], [long] in [incompatible]", - incompatibleError("SELECT emp_no FROM \"*\"")); + "1:8: Cannot use field [emp_no] due to ambiguities being mapped as [2] incompatible types: " + + "[integer] in [basic], [long] in [incompatible]", + incompatibleError("SELECT emp_no FROM \"*\"") + ); } public void testMismatchedFieldStarInIncompatibleMappings() { assertEquals( - "1:8: Cannot use field [emp_no] due to ambiguities being mapped as [2] incompatible types: " - + "[integer] in [basic], [long] in [incompatible]", - incompatibleError("SELECT emp_no.* FROM \"*\"")); + "1:8: Cannot use field [emp_no] due to ambiguities being mapped as [2] incompatible types: " + + "[integer] in [basic], [long] in [incompatible]", + incompatibleError("SELECT emp_no.* FROM \"*\"") + ); } public void testMismatchedFieldFilterInIncompatibleMappings() { assertEquals( - "1:33: Cannot use field [emp_no] due to ambiguities being mapped as [2] incompatible types: " - + "[integer] in [basic], [long] in [incompatible]", - incompatibleError("SELECT languages FROM \"*\" WHERE emp_no > 1")); + "1:33: Cannot use field [emp_no] due to ambiguities being mapped as [2] incompatible types: " + + "[integer] in [basic], [long] in [incompatible]", + incompatibleError("SELECT languages FROM \"*\" WHERE emp_no > 1") + ); } public void testMismatchedFieldScalarInIncompatibleMappings() { assertEquals( - "1:45: Cannot use field [emp_no] due to ambiguities being mapped as [2] incompatible types: " - + "[integer] in [basic], [long] in [incompatible]", - incompatibleError("SELECT languages FROM \"*\" ORDER BY SIGN(ABS(emp_no))")); + "1:45: Cannot use field [emp_no] due to ambiguities being mapped as [2] incompatible types: " + + "[integer] in [basic], [long] in [incompatible]", + incompatibleError("SELECT languages FROM \"*\" ORDER BY SIGN(ABS(emp_no))") + ); } public void testConditionalWithDifferentDataTypes() { @SuppressWarnings("unchecked") String function = randomFrom(IfNull.class, NullIf.class).getSimpleName(); - assertEquals("1:17: 2nd argument of [" + function + "(3, '4')] must be [integer], found value ['4'] type [keyword]", - error("SELECT 1 = 1 OR " + function + "(3, '4') > 1")); + assertEquals( + "1:17: 2nd argument of [" + function + "(3, '4')] must be [integer], found value ['4'] type [keyword]", + error("SELECT 1 = 1 OR " + function + "(3, '4') > 1") + ); @SuppressWarnings("unchecked") String arbirtraryArgsFunction = randomFrom(Coalesce.class, Greatest.class, Least.class).getSimpleName(); - assertEquals("1:17: 3rd argument of [" + arbirtraryArgsFunction + "(null, 3, '4')] must be [integer], " + - "found value ['4'] type [keyword]", - error("SELECT 1 = 1 OR " + arbirtraryArgsFunction + "(null, 3, '4') > 1")); + assertEquals( + "1:17: 3rd argument of [" + arbirtraryArgsFunction + "(null, 3, '4')] must be [integer], " + "found value ['4'] type [keyword]", + error("SELECT 1 = 1 OR " + arbirtraryArgsFunction + "(null, 3, '4') > 1") + ); } public void testCaseWithNonBooleanConditionExpression() { - assertEquals("1:8: condition of [WHEN abs(int) THEN 'foo'] must be [boolean], found value [abs(int)] type [integer]", - error("SELECT CASE WHEN int = 1 THEN 'one' WHEN abs(int) THEN 'foo' END FROM test")); + assertEquals( + "1:8: condition of [WHEN abs(int) THEN 'foo'] must be [boolean], found value [abs(int)] type [integer]", + error("SELECT CASE WHEN int = 1 THEN 'one' WHEN abs(int) THEN 'foo' END FROM test") + ); } public void testCaseWithDifferentResultDataTypes() { - assertEquals("1:8: result of [WHEN int > 10 THEN 10] must be [keyword], found value [10] type [integer]", - error("SELECT CASE WHEN int > 20 THEN 'foo' WHEN int > 10 THEN 10 ELSE 'bar' END FROM test")); + assertEquals( + "1:8: result of [WHEN int > 10 THEN 10] must be [keyword], found value [10] type [integer]", + error("SELECT CASE WHEN int > 20 THEN 'foo' WHEN int > 10 THEN 10 ELSE 'bar' END FROM test") + ); } public void testCaseWithDifferentResultAndDefaultValueDataTypes() { - assertEquals("1:8: ELSE clause of [date] must be [keyword], found value [date] type [datetime]", - error("SELECT CASE WHEN int > 20 THEN 'foo' ELSE date END FROM test")); + assertEquals( + "1:8: ELSE clause of [date] must be [keyword], found value [date] type [datetime]", + error("SELECT CASE WHEN int > 20 THEN 'foo' ELSE date END FROM test") + ); } public void testCaseWithDifferentResultAndDefaultValueDataTypesAndNullTypesSkipped() { - assertEquals("1:8: ELSE clause of [date] must be [keyword], found value [date] type [datetime]", - error("SELECT CASE WHEN int > 20 THEN null WHEN int > 10 THEN null WHEN int > 5 THEN 'foo' ELSE date END FROM test")); + assertEquals( + "1:8: ELSE clause of [date] must be [keyword], found value [date] type [datetime]", + error("SELECT CASE WHEN int > 20 THEN null WHEN int > 10 THEN null WHEN int > 5 THEN 'foo' ELSE date END FROM test") + ); } public void testIifWithNonBooleanConditionExpression() { - assertEquals("1:8: first argument of [IIF(int, 'one', 'zero')] must be [boolean], found value [int] type [integer]", - error("SELECT IIF(int, 'one', 'zero') FROM test")); + assertEquals( + "1:8: first argument of [IIF(int, 'one', 'zero')] must be [boolean], found value [int] type [integer]", + error("SELECT IIF(int, 'one', 'zero') FROM test") + ); } public void testIifWithDifferentResultAndDefaultValueDataTypes() { - assertEquals("1:8: third argument of [IIF(int > 20, 'foo', date)] must be [keyword], found value [date] type [datetime]", - error("SELECT IIF(int > 20, 'foo', date) FROM test")); + assertEquals( + "1:8: third argument of [IIF(int > 20, 'foo', date)] must be [keyword], found value [date] type [datetime]", + error("SELECT IIF(int > 20, 'foo', date) FROM test") + ); } public void testAggsInWhere() { - assertEquals("1:33: Cannot use WHERE filtering on aggregate function [MAX(int)], use HAVING instead", - error("SELECT MAX(int) FROM test WHERE MAX(int) > 10 GROUP BY bool")); + assertEquals( + "1:33: Cannot use WHERE filtering on aggregate function [MAX(int)], use HAVING instead", + error("SELECT MAX(int) FROM test WHERE MAX(int) > 10 GROUP BY bool") + ); } public void testHavingInAggs() { - assertEquals("1:29: [int] field must appear in the GROUP BY clause or in an aggregate function", - error("SELECT int FROM test HAVING MAX(int) = 0")); + assertEquals( + "1:29: [int] field must appear in the GROUP BY clause or in an aggregate function", + error("SELECT int FROM test HAVING MAX(int) = 0") + ); - assertEquals("1:35: [int] field must appear in the GROUP BY clause or in an aggregate function", - error("SELECT int FROM test HAVING int = count(1)")); + assertEquals( + "1:35: [int] field must appear in the GROUP BY clause or in an aggregate function", + error("SELECT int FROM test HAVING int = count(1)") + ); } public void testHavingAsWhere() { @@ -942,34 +1176,40 @@ public void testHavingAsWhere() { } public void testHistogramInFilter() { - assertEquals("1:63: Cannot filter on grouping function [HISTOGRAM(date, INTERVAL 1 MONTH)], use its argument instead", - error("SELECT HISTOGRAM(date, INTERVAL 1 MONTH) AS h FROM test WHERE " - + "HISTOGRAM(date, INTERVAL 1 MONTH) > CAST('2000-01-01' AS DATETIME) GROUP BY h")); + assertEquals( + "1:63: Cannot filter on grouping function [HISTOGRAM(date, INTERVAL 1 MONTH)], use its argument instead", + error( + "SELECT HISTOGRAM(date, INTERVAL 1 MONTH) AS h FROM test WHERE " + + "HISTOGRAM(date, INTERVAL 1 MONTH) > CAST('2000-01-01' AS DATETIME) GROUP BY h" + ) + ); } // related https://github.com/elastic/elasticsearch/issues/36853 public void testHistogramInHaving() { - assertEquals("1:75: Cannot filter on grouping function [h], use its argument instead", - error("SELECT HISTOGRAM(date, INTERVAL 1 MONTH) AS h FROM test GROUP BY h HAVING " - + "h > CAST('2000-01-01' AS DATETIME)")); + assertEquals( + "1:75: Cannot filter on grouping function [h], use its argument instead", + error("SELECT HISTOGRAM(date, INTERVAL 1 MONTH) AS h FROM test GROUP BY h HAVING " + "h > CAST('2000-01-01' AS DATETIME)") + ); } public void testGroupByScalarOnTopOfGrouping() { assertEquals( - "1:14: Cannot combine [HISTOGRAM(date, INTERVAL 1 MONTH)] grouping function inside " - + "GROUP BY, found [MONTH(HISTOGRAM(date, INTERVAL 1 MONTH))]; consider moving the expression inside the histogram", - error("SELECT MONTH(HISTOGRAM(date, INTERVAL 1 MONTH)) AS h FROM test GROUP BY h")); + "1:14: Cannot combine [HISTOGRAM(date, INTERVAL 1 MONTH)] grouping function inside " + + "GROUP BY, found [MONTH(HISTOGRAM(date, INTERVAL 1 MONTH))]; consider moving the expression inside the histogram", + error("SELECT MONTH(HISTOGRAM(date, INTERVAL 1 MONTH)) AS h FROM test GROUP BY h") + ); } public void testAggsInHistogram() { - assertEquals("1:37: Cannot use an aggregate [MAX] for grouping", - error("SELECT MAX(date) FROM test GROUP BY MAX(int)")); + assertEquals("1:37: Cannot use an aggregate [MAX] for grouping", error("SELECT MAX(date) FROM test GROUP BY MAX(int)")); } public void testGroupingsInHistogram() { assertEquals( - "1:47: Cannot embed grouping functions within each other, found [HISTOGRAM(int, 1)] in [HISTOGRAM(HISTOGRAM(int, 1), 1)]", - error("SELECT MAX(date) FROM test GROUP BY HISTOGRAM(HISTOGRAM(int, 1), 1)")); + "1:47: Cannot embed grouping functions within each other, found [HISTOGRAM(int, 1)] in [HISTOGRAM(HISTOGRAM(int, 1), 1)]", + error("SELECT MAX(date) FROM test GROUP BY HISTOGRAM(HISTOGRAM(int, 1), 1)") + ); } public void testCastInHistogram() { @@ -977,179 +1217,250 @@ public void testCastInHistogram() { } public void testHistogramNotInGrouping() { - assertEquals("1:8: [HISTOGRAM(date, INTERVAL 1 MONTH)] needs to be part of the grouping", - error("SELECT HISTOGRAM(date, INTERVAL 1 MONTH) AS h FROM test")); + assertEquals( + "1:8: [HISTOGRAM(date, INTERVAL 1 MONTH)] needs to be part of the grouping", + error("SELECT HISTOGRAM(date, INTERVAL 1 MONTH) AS h FROM test") + ); } public void testHistogramNotInGroupingWithCount() { - assertEquals("1:8: [HISTOGRAM(date, INTERVAL 1 MONTH)] needs to be part of the grouping", - error("SELECT HISTOGRAM(date, INTERVAL 1 MONTH) AS h, COUNT(*) FROM test")); + assertEquals( + "1:8: [HISTOGRAM(date, INTERVAL 1 MONTH)] needs to be part of the grouping", + error("SELECT HISTOGRAM(date, INTERVAL 1 MONTH) AS h, COUNT(*) FROM test") + ); } public void testHistogramNotInGroupingWithMaxFirst() { - assertEquals("1:19: [HISTOGRAM(date, INTERVAL 1 MONTH)] needs to be part of the grouping", - error("SELECT MAX(date), HISTOGRAM(date, INTERVAL 1 MONTH) AS h FROM test")); + assertEquals( + "1:19: [HISTOGRAM(date, INTERVAL 1 MONTH)] needs to be part of the grouping", + error("SELECT MAX(date), HISTOGRAM(date, INTERVAL 1 MONTH) AS h FROM test") + ); } public void testHistogramWithoutAliasNotInGrouping() { - assertEquals("1:8: [HISTOGRAM(date, INTERVAL 1 MONTH)] needs to be part of the grouping", - error("SELECT HISTOGRAM(date, INTERVAL 1 MONTH) FROM test")); + assertEquals( + "1:8: [HISTOGRAM(date, INTERVAL 1 MONTH)] needs to be part of the grouping", + error("SELECT HISTOGRAM(date, INTERVAL 1 MONTH) FROM test") + ); } public void testTwoHistogramsNotInGrouping() { - assertEquals("1:48: [HISTOGRAM(date, INTERVAL 1 DAY)] needs to be part of the grouping", - error("SELECT HISTOGRAM(date, INTERVAL 1 MONTH) AS h, HISTOGRAM(date, INTERVAL 1 DAY) FROM test GROUP BY h")); + assertEquals( + "1:48: [HISTOGRAM(date, INTERVAL 1 DAY)] needs to be part of the grouping", + error("SELECT HISTOGRAM(date, INTERVAL 1 MONTH) AS h, HISTOGRAM(date, INTERVAL 1 DAY) FROM test GROUP BY h") + ); } public void testHistogramNotInGrouping_WithGroupByField() { - assertEquals("1:8: [HISTOGRAM(date, INTERVAL 1 MONTH)] needs to be part of the grouping", - error("SELECT HISTOGRAM(date, INTERVAL 1 MONTH) FROM test GROUP BY date")); + assertEquals( + "1:8: [HISTOGRAM(date, INTERVAL 1 MONTH)] needs to be part of the grouping", + error("SELECT HISTOGRAM(date, INTERVAL 1 MONTH) FROM test GROUP BY date") + ); } public void testScalarOfHistogramNotInGrouping() { - assertEquals("1:14: [HISTOGRAM(date, INTERVAL 1 MONTH)] needs to be part of the grouping", - error("SELECT MONTH(HISTOGRAM(date, INTERVAL 1 MONTH)) FROM test")); + assertEquals( + "1:14: [HISTOGRAM(date, INTERVAL 1 MONTH)] needs to be part of the grouping", + error("SELECT MONTH(HISTOGRAM(date, INTERVAL 1 MONTH)) FROM test") + ); } public void testErrorMessageForPercentileWithSecondArgBasedOnAField() { - assertEquals("1:8: second argument of [PERCENTILE(int, ABS(int))] must be a constant, received [ABS(int)]", - error("SELECT PERCENTILE(int, ABS(int)) FROM test")); + assertEquals( + "1:8: second argument of [PERCENTILE(int, ABS(int))] must be a constant, received [ABS(int)]", + error("SELECT PERCENTILE(int, ABS(int)) FROM test") + ); } public void testErrorMessageForPercentileWithWrongMethodType() { - assertEquals("1:8: third argument of [PERCENTILE(int, 50, 2)] must be [string], found value [2] type [integer]", - error("SELECT PERCENTILE(int, 50, 2) FROM test")); + assertEquals( + "1:8: third argument of [PERCENTILE(int, 50, 2)] must be [string], found value [2] type [integer]", + error("SELECT PERCENTILE(int, 50, 2) FROM test") + ); } public void testErrorMessageForPercentileWithNullMethodType() { - assertEquals("1:8: third argument of [PERCENTILE(int, 50, null)] must be one of [tdigest, hdr], received [null]", - error("SELECT PERCENTILE(int, 50, null) FROM test")); + assertEquals( + "1:8: third argument of [PERCENTILE(int, 50, null)] must be one of [tdigest, hdr], received [null]", + error("SELECT PERCENTILE(int, 50, null) FROM test") + ); } public void testErrorMessageForPercentileWithHDRRequiresInt() { - assertEquals("1:8: fourth argument of [PERCENTILE(int, 50, 'hdr', 2.2)] must be [integer], found value [2.2] type [double]", - error("SELECT PERCENTILE(int, 50, 'hdr', 2.2) FROM test")); + assertEquals( + "1:8: fourth argument of [PERCENTILE(int, 50, 'hdr', 2.2)] must be [integer], found value [2.2] type [double]", + error("SELECT PERCENTILE(int, 50, 'hdr', 2.2) FROM test") + ); } public void testErrorMessageForPercentileWithWrongMethod() { - assertEquals("1:8: third argument of [PERCENTILE(int, 50, 'notExistingMethod', 5)] must be " + - "one of [tdigest, hdr], received [notExistingMethod]", - error("SELECT PERCENTILE(int, 50, 'notExistingMethod', 5) FROM test")); + assertEquals( + "1:8: third argument of [PERCENTILE(int, 50, 'notExistingMethod', 5)] must be " + + "one of [tdigest, hdr], received [notExistingMethod]", + error("SELECT PERCENTILE(int, 50, 'notExistingMethod', 5) FROM test") + ); } public void testErrorMessageForPercentileWithWrongMethodParameterType() { - assertEquals("1:8: fourth argument of [PERCENTILE(int, 50, 'tdigest', '5')] must be [numeric], found value ['5'] type [keyword]", - error("SELECT PERCENTILE(int, 50, 'tdigest', '5') FROM test")); + assertEquals( + "1:8: fourth argument of [PERCENTILE(int, 50, 'tdigest', '5')] must be [numeric], found value ['5'] type [keyword]", + error("SELECT PERCENTILE(int, 50, 'tdigest', '5') FROM test") + ); } public void testErrorMessageForPercentileRankWithSecondArgBasedOnAField() { - assertEquals("1:8: second argument of [PERCENTILE_RANK(int, ABS(int))] must be a constant, received [ABS(int)]", - error("SELECT PERCENTILE_RANK(int, ABS(int)) FROM test")); + assertEquals( + "1:8: second argument of [PERCENTILE_RANK(int, ABS(int))] must be a constant, received [ABS(int)]", + error("SELECT PERCENTILE_RANK(int, ABS(int)) FROM test") + ); } public void testErrorMessageForPercentileRankWithWrongMethodType() { - assertEquals("1:8: third argument of [PERCENTILE_RANK(int, 50, 2)] must be [string], found value [2] type [integer]", - error("SELECT PERCENTILE_RANK(int, 50, 2) FROM test")); + assertEquals( + "1:8: third argument of [PERCENTILE_RANK(int, 50, 2)] must be [string], found value [2] type [integer]", + error("SELECT PERCENTILE_RANK(int, 50, 2) FROM test") + ); } public void testErrorMessageForPercentileRankWithNullMethodType() { - assertEquals("1:8: third argument of [PERCENTILE_RANK(int, 50, null)] must be one of [tdigest, hdr], received [null]", - error("SELECT PERCENTILE_RANK(int, 50, null) FROM test")); + assertEquals( + "1:8: third argument of [PERCENTILE_RANK(int, 50, null)] must be one of [tdigest, hdr], received [null]", + error("SELECT PERCENTILE_RANK(int, 50, null) FROM test") + ); } public void testErrorMessageForPercentileRankWithHDRRequiresInt() { - assertEquals("1:8: fourth argument of [PERCENTILE_RANK(int, 50, 'hdr', 2.2)] must be [integer], found value [2.2] type [double]", - error("SELECT PERCENTILE_RANK(int, 50, 'hdr', 2.2) FROM test")); + assertEquals( + "1:8: fourth argument of [PERCENTILE_RANK(int, 50, 'hdr', 2.2)] must be [integer], found value [2.2] type [double]", + error("SELECT PERCENTILE_RANK(int, 50, 'hdr', 2.2) FROM test") + ); } public void testErrorMessageForPercentileRankWithWrongMethod() { - assertEquals("1:8: third argument of [PERCENTILE_RANK(int, 50, 'notExistingMethod', 5)] must be " + - "one of [tdigest, hdr], received [notExistingMethod]", - error("SELECT PERCENTILE_RANK(int, 50, 'notExistingMethod', 5) FROM test")); + assertEquals( + "1:8: third argument of [PERCENTILE_RANK(int, 50, 'notExistingMethod', 5)] must be " + + "one of [tdigest, hdr], received [notExistingMethod]", + error("SELECT PERCENTILE_RANK(int, 50, 'notExistingMethod', 5) FROM test") + ); } public void testErrorMessageForPercentileRankWithWrongMethodParameterType() { - assertEquals("1:8: fourth argument of [PERCENTILE_RANK(int, 50, 'tdigest', '5')] must be [numeric], " + - "found value ['5'] type [keyword]", - error("SELECT PERCENTILE_RANK(int, 50, 'tdigest', '5') FROM test")); + assertEquals( + "1:8: fourth argument of [PERCENTILE_RANK(int, 50, 'tdigest', '5')] must be [numeric], " + "found value ['5'] type [keyword]", + error("SELECT PERCENTILE_RANK(int, 50, 'tdigest', '5') FROM test") + ); } public void testTopHitsFirstArgConstant() { String topHitsFunction = randomTopHitsFunction(); - assertEquals("1:8: first argument of [" + topHitsFunction + "('foo', int)] must be a table column, found constant ['foo']", - error("SELECT " + topHitsFunction + "('foo', int) FROM test")); + assertEquals( + "1:8: first argument of [" + topHitsFunction + "('foo', int)] must be a table column, found constant ['foo']", + error("SELECT " + topHitsFunction + "('foo', int) FROM test") + ); } public void testTopHitsSecondArgConstant() { String topHitsFunction = randomTopHitsFunction(); - assertEquals("1:8: second argument of [" + topHitsFunction + "(int, 10)] must be a table column, found constant [10]", - error("SELECT " + topHitsFunction + "(int, 10) FROM test")); + assertEquals( + "1:8: second argument of [" + topHitsFunction + "(int, 10)] must be a table column, found constant [10]", + error("SELECT " + topHitsFunction + "(int, 10) FROM test") + ); } public void testTopHitsFirstArgTextWithNoKeyword() { String topHitsFunction = randomTopHitsFunction(); - assertEquals("1:8: [" + topHitsFunction + "(text)] cannot operate on first argument field of data type [text]: " + - "No keyword/multi-field defined exact matches for [text]; define one or use MATCH/QUERY instead", - error("SELECT " + topHitsFunction + "(text) FROM test")); + assertEquals( + "1:8: [" + + topHitsFunction + + "(text)] cannot operate on first argument field of data type [text]: " + + "No keyword/multi-field defined exact matches for [text]; define one or use MATCH/QUERY instead", + error("SELECT " + topHitsFunction + "(text) FROM test") + ); } public void testTopHitsSecondArgTextWithNoKeyword() { String topHitsFunction = randomTopHitsFunction(); - assertEquals("1:8: [" + topHitsFunction + "(keyword, text)] cannot operate on second argument field of data type [text]: " + - "No keyword/multi-field defined exact matches for [text]; define one or use MATCH/QUERY instead", - error("SELECT " + topHitsFunction + "(keyword, text) FROM test")); + assertEquals( + "1:8: [" + + topHitsFunction + + "(keyword, text)] cannot operate on second argument field of data type [text]: " + + "No keyword/multi-field defined exact matches for [text]; define one or use MATCH/QUERY instead", + error("SELECT " + topHitsFunction + "(keyword, text) FROM test") + ); } public void testTopHitsByHavingUnsupported() { String topHitsFunction = randomTopHitsFunction(); int column = 31 + topHitsFunction.length(); - assertEquals("1:" + column + ": filtering is unsupported for function [" + topHitsFunction + "(int)]", - error("SELECT " + topHitsFunction + "(int) FROM test HAVING " + topHitsFunction + "(int) > 10")); + assertEquals( + "1:" + column + ": filtering is unsupported for function [" + topHitsFunction + "(int)]", + error("SELECT " + topHitsFunction + "(int) FROM test HAVING " + topHitsFunction + "(int) > 10") + ); } public void testTopHitsGroupByHavingUnsupported() { String topHitsFunction = randomTopHitsFunction(); int column = 45 + topHitsFunction.length(); - assertEquals("1:" + column + ": filtering is unsupported for function [" + topHitsFunction + "(int)]", - error("SELECT " + topHitsFunction + "(int) FROM test GROUP BY text HAVING " + topHitsFunction + "(int) > 10")); + assertEquals( + "1:" + column + ": filtering is unsupported for function [" + topHitsFunction + "(int)]", + error("SELECT " + topHitsFunction + "(int) FROM test GROUP BY text HAVING " + topHitsFunction + "(int) > 10") + ); } public void testTopHitsHavingWithSubqueryUnsupported() { String filter = randomFrom("WHERE", "HAVING"); int column = 99 + filter.length(); - assertEquals("1:" + column + ": filtering is unsupported for functions [FIRST(int), LAST(int)]", - error("SELECT * FROM (SELECT * FROM (SELECT * FROM (SELECT FIRST(int) AS f, LAST(int) AS l FROM test))) " + - filter + " f > 10 or l < 10")); + assertEquals( + "1:" + column + ": filtering is unsupported for functions [FIRST(int), LAST(int)]", + error( + "SELECT * FROM (SELECT * FROM (SELECT * FROM (SELECT FIRST(int) AS f, LAST(int) AS l FROM test))) " + + filter + + " f > 10 or l < 10" + ) + ); } public void testTopHitsGroupByHavingWithSubqueryUnsupported() { String filter = randomFrom("WHERE", "HAVING"); int column = 113 + filter.length(); - assertEquals("1:" + column + ": filtering is unsupported for functions [FIRST(int), LAST(int)]", - error("SELECT * FROM (SELECT * FROM (SELECT * FROM (SELECT FIRST(int) AS f, LAST(int) AS l FROM test GROUP BY bool))) " + - filter + " f > 10 or l < 10")); + assertEquals( + "1:" + column + ": filtering is unsupported for functions [FIRST(int), LAST(int)]", + error( + "SELECT * FROM (SELECT * FROM (SELECT * FROM (SELECT FIRST(int) AS f, LAST(int) AS l FROM test GROUP BY bool))) " + + filter + + " f > 10 or l < 10" + ) + ); } public void testMinOnInexactUnsupported() { - assertEquals("1:8: [MIN(text)] cannot operate on field of data type [text]: " + - "No keyword/multi-field defined exact matches for [text]; define one or use MATCH/QUERY instead", - error("SELECT MIN(text) FROM test")); + assertEquals( + "1:8: [MIN(text)] cannot operate on field of data type [text]: " + + "No keyword/multi-field defined exact matches for [text]; define one or use MATCH/QUERY instead", + error("SELECT MIN(text) FROM test") + ); } public void testMaxOnInexactUnsupported() { - assertEquals("1:8: [MAX(text)] cannot operate on field of data type [text]: " + - "No keyword/multi-field defined exact matches for [text]; define one or use MATCH/QUERY instead", - error("SELECT MAX(text) FROM test")); + assertEquals( + "1:8: [MAX(text)] cannot operate on field of data type [text]: " + + "No keyword/multi-field defined exact matches for [text]; define one or use MATCH/QUERY instead", + error("SELECT MAX(text) FROM test") + ); } public void testMinOnKeywordGroupByHavingUnsupported() { - assertEquals("1:52: HAVING filter is unsupported for function [MIN(keyword)]", - error("SELECT MIN(keyword) FROM test GROUP BY text HAVING MIN(keyword) > 10")); + assertEquals( + "1:52: HAVING filter is unsupported for function [MIN(keyword)]", + error("SELECT MIN(keyword) FROM test GROUP BY text HAVING MIN(keyword) > 10") + ); } public void testMaxOnKeywordGroupByHavingUnsupported() { - assertEquals("1:52: HAVING filter is unsupported for function [MAX(keyword)]", - error("SELECT MAX(keyword) FROM test GROUP BY text HAVING MAX(keyword) > 10")); + assertEquals( + "1:52: HAVING filter is unsupported for function [MAX(keyword)]", + error("SELECT MAX(keyword) FROM test GROUP BY text HAVING MAX(keyword) > 10") + ); } public void testProjectAliasInFilter() { @@ -1173,23 +1484,27 @@ public void testProjectUnresolvedAliasWithSameNameInOrderBy() { } public void testGeoShapeInWhereClause() { - assertEquals("1:53: geo shapes cannot be used for filtering", - error("SELECT ST_AsWKT(geo_shape) FROM test WHERE ST_AsWKT(geo_shape) = 'point (10 20)'")); + assertEquals( + "1:53: geo shapes cannot be used for filtering", + error("SELECT ST_AsWKT(geo_shape) FROM test WHERE ST_AsWKT(geo_shape) = 'point (10 20)'") + ); // We get only one message back because the messages are grouped by the node that caused the issue - assertEquals("1:50: geo shapes cannot be used for filtering", - error("SELECT MAX(ST_X(geo_shape)) FROM test WHERE ST_Y(geo_shape) > 10 " + - "GROUP BY ST_GEOMETRYTYPE(geo_shape) ORDER BY ST_ASWKT(geo_shape)")); + assertEquals( + "1:50: geo shapes cannot be used for filtering", + error( + "SELECT MAX(ST_X(geo_shape)) FROM test WHERE ST_Y(geo_shape) > 10 " + + "GROUP BY ST_GEOMETRYTYPE(geo_shape) ORDER BY ST_ASWKT(geo_shape)" + ) + ); } public void testGeoShapeInGroupBy() { - assertEquals("1:48: geo shapes cannot be used in grouping", - error("SELECT ST_X(geo_shape) FROM test GROUP BY ST_X(geo_shape)")); + assertEquals("1:48: geo shapes cannot be used in grouping", error("SELECT ST_X(geo_shape) FROM test GROUP BY ST_X(geo_shape)")); } public void testGeoShapeInOrderBy() { - assertEquals("1:48: geo shapes cannot be used for sorting", - error("SELECT ST_X(geo_shape) FROM test ORDER BY ST_Z(geo_shape)")); + assertEquals("1:48: geo shapes cannot be used for sorting", error("SELECT ST_X(geo_shape) FROM test ORDER BY ST_Z(geo_shape)")); } public void testGeoShapeInSelect() { @@ -1200,70 +1515,98 @@ public void testGeoShapeInSelect() { // Pivot verifications // public void testPivotNonExactColumn() { - assertEquals("1:72: Field [text] of data type [text] cannot be used for grouping;" + assertEquals( + "1:72: Field [text] of data type [text] cannot be used for grouping;" + " No keyword/multi-field defined exact matches for [text]; define one or use MATCH/QUERY instead", - error("SELECT * FROM (SELECT int, text, keyword FROM test) " + "PIVOT(AVG(int) FOR text IN ('bla'))")); + error("SELECT * FROM (SELECT int, text, keyword FROM test) " + "PIVOT(AVG(int) FOR text IN ('bla'))") + ); } public void testPivotColumnUsedInsteadOfAgg() { - assertEquals("1:59: No aggregate function found in PIVOT at [int]", - error("SELECT * FROM (SELECT int, keyword, bool FROM test) " + "PIVOT(int FOR keyword IN ('bla'))")); + assertEquals( + "1:59: No aggregate function found in PIVOT at [int]", + error("SELECT * FROM (SELECT int, keyword, bool FROM test) " + "PIVOT(int FOR keyword IN ('bla'))") + ); } public void testPivotScalarUsedInsteadOfAgg() { - assertEquals("1:59: No aggregate function found in PIVOT at [ROUND(int)]", - error("SELECT * FROM (SELECT int, keyword, bool FROM test) " + "PIVOT(ROUND(int) FOR keyword IN ('bla'))")); + assertEquals( + "1:59: No aggregate function found in PIVOT at [ROUND(int)]", + error("SELECT * FROM (SELECT int, keyword, bool FROM test) " + "PIVOT(ROUND(int) FOR keyword IN ('bla'))") + ); } public void testPivotScalarUsedAlongSideAgg() { - assertEquals("1:59: Non-aggregate function found in PIVOT at [AVG(int) + ROUND(int)]", - error("SELECT * FROM (SELECT int, keyword, bool FROM test) " + "PIVOT(AVG(int) + ROUND(int) FOR keyword IN ('bla'))")); + assertEquals( + "1:59: Non-aggregate function found in PIVOT at [AVG(int) + ROUND(int)]", + error("SELECT * FROM (SELECT int, keyword, bool FROM test) " + "PIVOT(AVG(int) + ROUND(int) FOR keyword IN ('bla'))") + ); } public void testPivotValueNotFoldable() { - assertEquals("1:91: Non-literal [bool] found inside PIVOT values", - error("SELECT * FROM (SELECT int, keyword, bool FROM test) " + "PIVOT(AVG(int) FOR keyword IN ('bla', bool))")); + assertEquals( + "1:91: Non-literal [bool] found inside PIVOT values", + error("SELECT * FROM (SELECT int, keyword, bool FROM test) " + "PIVOT(AVG(int) FOR keyword IN ('bla', bool))") + ); } public void testPivotWithFunctionInput() { - assertEquals("1:37: No functions allowed (yet); encountered [YEAR(date)]", - error("SELECT * FROM (SELECT int, keyword, YEAR(date) FROM test) " + "PIVOT(AVG(int) FOR keyword IN ('bla'))")); + assertEquals( + "1:37: No functions allowed (yet); encountered [YEAR(date)]", + error("SELECT * FROM (SELECT int, keyword, YEAR(date) FROM test) " + "PIVOT(AVG(int) FOR keyword IN ('bla'))") + ); } public void testPivotWithFoldableFunctionInValues() { - assertEquals("1:85: Non-literal [UCASE('bla')] found inside PIVOT values", - error("SELECT * FROM (SELECT int, keyword, bool FROM test) " + "PIVOT(AVG(int) FOR keyword IN ( UCASE('bla') ))")); + assertEquals( + "1:85: Non-literal [UCASE('bla')] found inside PIVOT values", + error("SELECT * FROM (SELECT int, keyword, bool FROM test) " + "PIVOT(AVG(int) FOR keyword IN ( UCASE('bla') ))") + ); } public void testPivotWithNull() { - assertEquals("1:85: Null not allowed as a PIVOT value", - error("SELECT * FROM (SELECT int, keyword, bool FROM test) " + "PIVOT(AVG(int) FOR keyword IN ( null ))")); + assertEquals( + "1:85: Null not allowed as a PIVOT value", + error("SELECT * FROM (SELECT int, keyword, bool FROM test) " + "PIVOT(AVG(int) FOR keyword IN ( null ))") + ); } public void testPivotValuesHaveDifferentTypeThanColumn() { - assertEquals("1:81: Literal ['bla'] of type [keyword] does not match type [boolean] of PIVOT column [bool]", - error("SELECT * FROM (SELECT int, keyword, bool FROM test) " + "PIVOT(AVG(int) FOR bool IN ('bla'))")); + assertEquals( + "1:81: Literal ['bla'] of type [keyword] does not match type [boolean] of PIVOT column [bool]", + error("SELECT * FROM (SELECT int, keyword, bool FROM test) " + "PIVOT(AVG(int) FOR bool IN ('bla'))") + ); } public void testPivotValuesWithMultipleDifferencesThanColumn() { - assertEquals("1:81: Literal ['bla'] of type [keyword] does not match type [boolean] of PIVOT column [bool]", - error("SELECT * FROM (SELECT int, keyword, bool FROM test) " + "PIVOT(AVG(int) FOR bool IN ('bla', true))")); + assertEquals( + "1:81: Literal ['bla'] of type [keyword] does not match type [boolean] of PIVOT column [bool]", + error("SELECT * FROM (SELECT int, keyword, bool FROM test) " + "PIVOT(AVG(int) FOR bool IN ('bla', true))") + ); } public void testErrorMessageForMatrixStatsWithScalars() { - assertEquals("1:17: [KURTOSIS()] cannot be used on top of operators or scalars", - error("SELECT KURTOSIS(ABS(int * 10.123)) FROM test")); - assertEquals("1:17: [SKEWNESS()] cannot be used on top of operators or scalars", - error("SELECT SKEWNESS(ABS(int * 10.123)) FROM test")); + assertEquals( + "1:17: [KURTOSIS()] cannot be used on top of operators or scalars", + error("SELECT KURTOSIS(ABS(int * 10.123)) FROM test") + ); + assertEquals( + "1:17: [SKEWNESS()] cannot be used on top of operators or scalars", + error("SELECT SKEWNESS(ABS(int * 10.123)) FROM test") + ); } public void testCastOnInexact() { // inexact with underlying keyword - assertEquals("1:36: [some.string] of data type [text] cannot be used for [CAST()] inside the WHERE clause", - error("SELECT * FROM test WHERE NOT (CAST(some.string AS string) = 'foo') OR true")); + assertEquals( + "1:36: [some.string] of data type [text] cannot be used for [CAST()] inside the WHERE clause", + error("SELECT * FROM test WHERE NOT (CAST(some.string AS string) = 'foo') OR true") + ); // inexact without underlying keyword (text only) - assertEquals("1:36: [text] of data type [text] cannot be used for [CAST()] inside the WHERE clause", - error("SELECT * FROM test WHERE NOT (CAST(text AS string) = 'foo') OR true")); + assertEquals( + "1:36: [text] of data type [text] cannot be used for [CAST()] inside the WHERE clause", + error("SELECT * FROM test WHERE NOT (CAST(text AS string) = 'foo') OR true") + ); } public void testBinaryFieldWithDocValues() { @@ -1274,14 +1617,22 @@ public void testBinaryFieldWithDocValues() { } public void testBinaryFieldWithNoDocValues() { - assertEquals("1:31: Binary field [binary] cannot be used for filtering unless it has the doc_values setting enabled", - error("SELECT binary FROM test WHERE binary IS NOT NULL")); - assertEquals("1:34: Binary field [binary] cannot be used in aggregations unless it has the doc_values setting enabled", - error("SELECT binary FROM test GROUP BY binary")); - assertEquals("1:45: Binary field [binary] cannot be used for filtering unless it has the doc_values setting enabled", - error("SELECT count(binary) FROM test HAVING count(binary) > 1")); - assertEquals("1:34: Binary field [binary] cannot be used for ordering unless it has the doc_values setting enabled", - error("SELECT binary FROM test ORDER BY binary")); + assertEquals( + "1:31: Binary field [binary] cannot be used for filtering unless it has the doc_values setting enabled", + error("SELECT binary FROM test WHERE binary IS NOT NULL") + ); + assertEquals( + "1:34: Binary field [binary] cannot be used in aggregations unless it has the doc_values setting enabled", + error("SELECT binary FROM test GROUP BY binary") + ); + assertEquals( + "1:45: Binary field [binary] cannot be used for filtering unless it has the doc_values setting enabled", + error("SELECT count(binary) FROM test HAVING count(binary) > 1") + ); + assertEquals( + "1:34: Binary field [binary] cannot be used for ordering unless it has the doc_values setting enabled", + error("SELECT binary FROM test ORDER BY binary") + ); } public void testDistinctIsNotSupported() { @@ -1293,24 +1644,29 @@ public void testExistsIsNotSupported() { } public void testScoreCannotBeUsedInExpressions() { - assertEquals("1:12: [SCORE()] cannot be used in expressions, does not support further processing", - error("SELECT SIN(SCORE()) FROM test")); + assertEquals( + "1:12: [SCORE()] cannot be used in expressions, does not support further processing", + error("SELECT SIN(SCORE()) FROM test") + ); } public void testScoreIsNotInHaving() { - assertEquals("1:54: HAVING filter is unsupported for function [SCORE()]\n" + - "line 1:54: [SCORE()] cannot be used in expressions, does not support further processing", - error("SELECT bool, AVG(int) FROM test GROUP BY bool HAVING SCORE() > 0.5")); + assertEquals( + "1:54: HAVING filter is unsupported for function [SCORE()]\n" + + "line 1:54: [SCORE()] cannot be used in expressions, does not support further processing", + error("SELECT bool, AVG(int) FROM test GROUP BY bool HAVING SCORE() > 0.5") + ); } public void testScoreCannotBeUsedForGrouping() { - assertEquals("1:42: Cannot use [SCORE()] for grouping", - error("SELECT bool, AVG(int) FROM test GROUP BY SCORE()")); + assertEquals("1:42: Cannot use [SCORE()] for grouping", error("SELECT bool, AVG(int) FROM test GROUP BY SCORE()")); } public void testScoreCannotBeAnAggregateFunction() { - assertEquals("1:14: Cannot use non-grouped column [SCORE()], expected [bool]", - error("SELECT bool, SCORE() FROM test GROUP BY bool")); + assertEquals( + "1:14: Cannot use non-grouped column [SCORE()], expected [bool]", + error("SELECT bool, SCORE() FROM test GROUP BY bool") + ); } public void testScalarFunctionInAggregateAndGrouping() { @@ -1327,20 +1683,22 @@ public void testLiteralAsAggregate() { } public void testShapeInWhereClause() { - assertEquals("1:49: shapes cannot be used for filtering", - error("SELECT ST_AsWKT(shape) FROM test WHERE ST_AsWKT(shape) = 'point (10 20)'")); - assertEquals("1:46: shapes cannot be used for filtering", - error("SELECT MAX(ST_X(shape)) FROM test WHERE ST_Y(shape) > 10 GROUP BY ST_GEOMETRYTYPE(shape) ORDER BY ST_ASWKT(shape)")); + assertEquals( + "1:49: shapes cannot be used for filtering", + error("SELECT ST_AsWKT(shape) FROM test WHERE ST_AsWKT(shape) = 'point (10 20)'") + ); + assertEquals( + "1:46: shapes cannot be used for filtering", + error("SELECT MAX(ST_X(shape)) FROM test WHERE ST_Y(shape) > 10 GROUP BY ST_GEOMETRYTYPE(shape) ORDER BY ST_ASWKT(shape)") + ); } public void testShapeInGroupBy() { - assertEquals("1:44: shapes cannot be used in grouping", - error("SELECT ST_X(shape) FROM test GROUP BY ST_X(shape)")); + assertEquals("1:44: shapes cannot be used in grouping", error("SELECT ST_X(shape) FROM test GROUP BY ST_X(shape)")); } public void testShapeInOrderBy() { - assertEquals("1:44: shapes cannot be used for sorting", - error("SELECT ST_X(shape) FROM test ORDER BY ST_Z(shape)")); + assertEquals("1:44: shapes cannot be used for sorting", error("SELECT ST_X(shape) FROM test ORDER BY ST_Z(shape)")); } public void testShapeInSelect() { diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/analysis/index/IndexResolverTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/analysis/index/IndexResolverTests.java index 8eb425bb72007..50edd14dd8278 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/analysis/index/IndexResolverTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/analysis/index/IndexResolverTests.java @@ -6,14 +6,6 @@ */ package org.elasticsearch.xpack.sql.analysis.index; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.stream.Stream; - import org.elasticsearch.action.fieldcaps.FieldCapabilities; import org.elasticsearch.action.fieldcaps.FieldCapabilitiesResponse; import org.elasticsearch.test.ESTestCase; @@ -26,6 +18,14 @@ import org.elasticsearch.xpack.ql.type.KeywordEsField; import org.elasticsearch.xpack.sql.type.SqlDataTypeRegistry; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.stream.Stream; + import static java.util.Collections.singletonMap; import static org.elasticsearch.common.logging.LoggerMessageFormat.format; import static org.elasticsearch.xpack.ql.type.DataTypes.INTEGER; @@ -80,8 +80,9 @@ public void testMergeIncompatibleTypes() throws Exception { assertEquals(InvalidMappedField.class, esField.getClass()); assertEquals( - "mapped as [2] incompatible types: [text] in [incompatible], [keyword] in [basic]", - ((InvalidMappedField) esField).errorMessage()); + "mapped as [2] incompatible types: [text] in [incompatible], [keyword] in [basic]", + ((InvalidMappedField) esField).errorMessage() + ); } public void testMergeIncompatibleCapabilities() throws Exception { @@ -162,8 +163,10 @@ public void testFlattenedHiddenSubfield() throws Exception { assertEquals(UNSUPPORTED, esIndex.mapping().get("some_field").getProperties().get("_keyed").getDataType()); assertEquals(OBJECT, esIndex.mapping().get("nested_field").getDataType()); assertEquals(UNSUPPORTED, esIndex.mapping().get("nested_field").getProperties().get("sub_field").getDataType()); - assertEquals(UNSUPPORTED, - esIndex.mapping().get("nested_field").getProperties().get("sub_field").getProperties().get("_keyed").getDataType()); + assertEquals( + UNSUPPORTED, + esIndex.mapping().get("nested_field").getProperties().get("sub_field").getProperties().get("_keyed").getDataType() + ); assertEquals(KEYWORD, esIndex.mapping().get("text").getDataType()); assertEquals(OBJECT, esIndex.mapping().get("another_field").getDataType()); assertEquals(KEYWORD, esIndex.mapping().get("another_field").getProperties().get("_keyed").getDataType()); @@ -188,10 +191,14 @@ public void testPropagateUnsupportedTypeToSubFields() throws Exception { assertEquals(TEXT, esIndex.mapping().get("a").getDataType()); assertEquals(UNSUPPORTED, esIndex.mapping().get("a").getProperties().get("b").getDataType()); assertEquals(UNSUPPORTED, esIndex.mapping().get("a").getProperties().get("b").getProperties().get("c").getDataType()); - assertEquals(UNSUPPORTED, esIndex.mapping().get("a").getProperties().get("b").getProperties().get("c") - .getProperties().get("d").getDataType()); - assertEquals(UNSUPPORTED, esIndex.mapping().get("a").getProperties().get("b").getProperties().get("c") - .getProperties().get("e").getDataType()); + assertEquals( + UNSUPPORTED, + esIndex.mapping().get("a").getProperties().get("b").getProperties().get("c").getProperties().get("d").getDataType() + ); + assertEquals( + UNSUPPORTED, + esIndex.mapping().get("a").getProperties().get("b").getProperties().get("c").getProperties().get("e").getDataType() + ); } public void testRandomMappingFieldTypeMappedAsUnsupported() throws Exception { @@ -219,11 +226,15 @@ public void testRandomMappingFieldTypeMappedAsUnsupported() throws Exception { assertEquals(UNSUPPORTED, esIndex.mapping().get("some_field").getDataType()); assertEquals(OBJECT, esIndex.mapping().get("nested_field").getDataType()); assertEquals(UNSUPPORTED, esIndex.mapping().get("nested_field").getProperties().get("sub_field1").getDataType()); - assertEquals(UNSUPPORTED, - esIndex.mapping().get("nested_field").getProperties().get("sub_field1").getProperties().get("bar").getDataType()); + assertEquals( + UNSUPPORTED, + esIndex.mapping().get("nested_field").getProperties().get("sub_field1").getProperties().get("bar").getDataType() + ); assertEquals(UNSUPPORTED, esIndex.mapping().get("nested_field").getProperties().get("sub_field2").getDataType()); - assertEquals(UNSUPPORTED, - esIndex.mapping().get("nested_field").getProperties().get("sub_field2").getProperties().get("bar").getDataType()); + assertEquals( + UNSUPPORTED, + esIndex.mapping().get("nested_field").getProperties().get("sub_field2").getProperties().get("bar").getDataType() + ); assertEquals(KEYWORD, esIndex.mapping().get("text").getDataType()); assertEquals(OBJECT, esIndex.mapping().get("another_field").getDataType()); assertEquals(UNSUPPORTED, esIndex.mapping().get("another_field").getProperties().get("_foo").getDataType()); @@ -247,15 +258,26 @@ public void testMergeIncompatibleCapabilitiesOfObjectFields() throws Exception { addFieldCaps(fieldCaps, fieldName + ".keyword", "keyword", true, true); Map multi = new HashMap<>(); - multi.put("long", new FieldCapabilities(fieldName, "long", false, true, true, new String[] { "one-index" }, null, null, + multi.put( + "long", + new FieldCapabilities(fieldName, "long", false, true, true, new String[] { "one-index" }, null, null, Collections.emptyMap()) + ); + multi.put( + "text", + new FieldCapabilities( + fieldName, + "text", + false, + true, + false, + new String[] { "another-index" }, + null, + null, Collections.emptyMap() - )); - multi.put("text", new FieldCapabilities(fieldName, "text", false, true, false, new String[] { "another-index" }, null, null, - Collections.emptyMap() - )); + ) + ); fieldCaps.put(fieldName, multi); - String wildcard = "*"; IndexResolution resolution = mergedMappings(wildcard, new String[] { "one-index" }, fieldCaps); @@ -270,8 +292,10 @@ public void testMergeIncompatibleCapabilitiesOfObjectFields() throws Exception { props = esField.getProperties(); } assertEquals(InvalidMappedField.class, esField.getClass()); - assertEquals("mapped as [2] incompatible types: [text] in [another-index], [long] in [one-index]", - ((InvalidMappedField) esField).errorMessage()); + assertEquals( + "mapped as [2] incompatible types: [text] in [another-index], [long] in [one-index]", + ((InvalidMappedField) esField).errorMessage() + ); } public void testSeparateSameMappingDifferentIndices() throws Exception { @@ -321,9 +345,13 @@ public void testMultipleCompatibleIndicesWithDifferentFields() { } public void testIndexWithNoMapping() { - Map> versionFC = singletonMap("_version", - singletonMap("_index", new FieldCapabilities("_version", "_version", true, false, false, - null, null, null, Collections.emptyMap()))); + Map> versionFC = singletonMap( + "_version", + singletonMap( + "_index", + new FieldCapabilities("_version", "_version", true, false, false, null, null, null, Collections.emptyMap()) + ) + ); assertTrue(mergedMappings("*", new String[] { "empty" }, versionFC).isValid()); } @@ -332,8 +360,7 @@ public static IndexResolution merge(EsIndex... indices) { } public static List separate(EsIndex... indices) { - return separateMappings(null, Stream.of(indices).map(EsIndex::name).toArray(String[]::new), - fromMappings(indices)); + return separateMappings(null, Stream.of(indices).map(EsIndex::name).toArray(String[]::new), fromMappings(indices)); } public static Map> fromMappings(EsIndex... indices) { @@ -356,7 +383,7 @@ public static Map> fromMappings(EsIndex.. UpdateableFieldCapabilities fieldCaps = (UpdateableFieldCapabilities) caps.get(field.getDataType().esType()); fieldCaps.indices.add(index.name()); } - //TODO: what about nonAgg/SearchIndices? + // TODO: what about nonAgg/SearchIndices? } } @@ -370,10 +397,15 @@ private static void addFieldCaps(String parent, EsField field, String indexName, map = new HashMap<>(); merged.put(fieldName, map); } - FieldCapabilities caps = map.computeIfAbsent(field.getDataType().esType(), - esType -> new UpdateableFieldCapabilities(fieldName, esType, - isSearchable(field.getDataType()), - isAggregatable(field.getDataType()))); + FieldCapabilities caps = map.computeIfAbsent( + field.getDataType().esType(), + esType -> new UpdateableFieldCapabilities( + fieldName, + esType, + isSearchable(field.getDataType()), + isAggregatable(field.getDataType()) + ) + ); if (field.isAggregatable() == false) { ((UpdateableFieldCapabilities) caps).nonAggregatableIndices.add(indexName); @@ -429,35 +461,54 @@ private static void assertEqualsMaps(Map left, Map right) { } } - private void addFieldCaps(Map> fieldCaps, - String name, - String type, - boolean isSearchable, - boolean isAggregatable) { + private void addFieldCaps( + Map> fieldCaps, + String name, + String type, + boolean isSearchable, + boolean isAggregatable + ) { addFieldCaps(fieldCaps, name, type, false, isSearchable, isAggregatable); } - private void addFieldCaps(Map> fieldCaps, - String name, - String type, - boolean isMetadataField, - boolean isSearchable, - boolean isAggregatable) { + private void addFieldCaps( + Map> fieldCaps, + String name, + String type, + boolean isMetadataField, + boolean isSearchable, + boolean isAggregatable + ) { Map cap = new HashMap<>(); - cap.put(type, new FieldCapabilities(name, type, isMetadataField, - isSearchable, isAggregatable, null, null, null, Collections.emptyMap())); + cap.put( + type, + new FieldCapabilities(name, type, isMetadataField, isSearchable, isAggregatable, null, null, null, Collections.emptyMap()) + ); fieldCaps.put(name, cap); } - private static IndexResolution mergedMappings(String indexPattern, String[] indexNames, - Map> fieldCaps) { - return IndexResolver.mergedMappings(SqlDataTypeRegistry.INSTANCE, indexPattern, - new FieldCapabilitiesResponse(indexNames, fieldCaps)); + private static IndexResolution mergedMappings( + String indexPattern, + String[] indexNames, + Map> fieldCaps + ) { + return IndexResolver.mergedMappings( + SqlDataTypeRegistry.INSTANCE, + indexPattern, + new FieldCapabilitiesResponse(indexNames, fieldCaps) + ); } - private static List separateMappings(String javaRegex, String[] indexNames, - Map> fieldCaps) { - return IndexResolver.separateMappings(SqlDataTypeRegistry.INSTANCE, javaRegex, - new FieldCapabilitiesResponse(indexNames, fieldCaps), null); + private static List separateMappings( + String javaRegex, + String[] indexNames, + Map> fieldCaps + ) { + return IndexResolver.separateMappings( + SqlDataTypeRegistry.INSTANCE, + javaRegex, + new FieldCapabilitiesResponse(indexNames, fieldCaps), + null + ); } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/CompositeAggregationCursorTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/CompositeAggregationCursorTests.java index 8330e2d8e32fa..4815a7feec358 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/CompositeAggregationCursorTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/CompositeAggregationCursorTests.java @@ -29,8 +29,14 @@ public static CompositeAggCursor randomCompositeCursor() { extractors.add(randomBucketExtractor(id)); } - return new CompositeAggCursor(new byte[randomInt(256)], extractors, randomBitSet(extractorsSize), - randomIntBetween(10, 1024), randomBoolean(), randomAlphaOfLength(5)); + return new CompositeAggCursor( + new byte[randomInt(256)], + extractors, + randomBitSet(extractorsSize), + randomIntBetween(10, 1024), + randomBoolean(), + randomAlphaOfLength(5) + ); } static BucketExtractor randomBucketExtractor(ZoneId zoneId) { @@ -43,11 +49,14 @@ static BucketExtractor randomBucketExtractor(ZoneId zoneId) { @Override protected CompositeAggCursor mutateInstance(CompositeAggCursor instance) throws IOException { - return new CompositeAggCursor(instance.next(), instance.extractors(), - randomValueOtherThan(instance.mask(), () -> randomBitSet(instance.extractors().size())), - randomValueOtherThan(instance.limit(), () -> randomIntBetween(1, 512)), - instance.includeFrozen() == false, - instance.indices()); + return new CompositeAggCursor( + instance.next(), + instance.extractors(), + randomValueOtherThan(instance.mask(), () -> randomBitSet(instance.extractors().size())), + randomValueOtherThan(instance.limit(), () -> randomIntBetween(1, 512)), + instance.includeFrozen() == false, + instance.indices() + ); } @Override diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/QuerierTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/QuerierTests.java index a7b4f19d068f1..44016669595cf 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/QuerierTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/QuerierTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.sql.execution.search; import org.elasticsearch.action.ActionListener; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.io.stream.NamedWriteable; +import org.elasticsearch.core.Tuple; import org.elasticsearch.search.aggregations.MultiBucketConsumerService; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.ql.type.Schema; @@ -41,7 +41,7 @@ public void testAggSortingAscending() { List> results = queue.asList(); assertEquals(10, results.size()); - for (int i = 0; i < 10; i ++) { + for (int i = 0; i < 10; i++) { assertEquals(i, results.get(i).get(0)); } } @@ -56,7 +56,7 @@ public void testAggSortingDescending() { List> results = queue.asList(); assertEquals(10, results.size()); - for (int i = 0; i < 10; i ++) { + for (int i = 0; i < 10; i++) { assertEquals(50 - i, results.get(i).get(0)); } } @@ -99,13 +99,13 @@ public void testAggSorting_TwoFields_One_Presorted() { } } - @SuppressWarnings({"rawtypes", "unchecked"}) + @SuppressWarnings({ "rawtypes", "unchecked" }) public void testAggSorting_FourFields() { - List comparators = Arrays. asList( - Comparator.naturalOrder(), - Comparator.naturalOrder(), - Comparator.reverseOrder(), - Comparator.naturalOrder() + List comparators = Arrays.asList( + Comparator.naturalOrder(), + Comparator.naturalOrder(), + Comparator.reverseOrder(), + Comparator.naturalOrder() ); List> tuples = new ArrayList<>(4); tuples.add(new Tuple<>(0, null)); @@ -227,8 +227,7 @@ protected boolean doNext() { } @Override - protected void doReset() { - } + protected void doReset() {} @Override public Schema schema() { @@ -239,10 +238,13 @@ public Schema schema() { public int size() { return dataSize; // irrelevant } - }; + } + ; - Cursor.Page page = new Cursor.Page(new TestResultRowSet(List.of(randomHitExtractor(0)), new BitSet(), dataSize), - Cursor.EMPTY); + Cursor.Page page = new Cursor.Page( + new TestResultRowSet(List.of(randomHitExtractor(0)), new BitSet(), dataSize), + Cursor.EMPTY + ); AtomicInteger responses = new AtomicInteger(); AtomicInteger failures = new AtomicInteger(); diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/ScrollCursorTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/ScrollCursorTests.java index d8e012bb873ff..f4e19175134fe 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/ScrollCursorTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/ScrollCursorTests.java @@ -28,8 +28,12 @@ public static ScrollCursor randomScrollCursor() { for (int i = 0; i < extractorsSize; i++) { extractors.add(randomHitExtractor(0)); } - return new ScrollCursor(randomAlphaOfLength(5), extractors, CompositeAggregationCursorTests.randomBitSet(extractorsSize), - randomIntBetween(10, 1024)); + return new ScrollCursor( + randomAlphaOfLength(5), + extractors, + CompositeAggregationCursorTests.randomBitSet(extractorsSize), + randomIntBetween(10, 1024) + ); } static HitExtractor randomHitExtractor(int depth) { @@ -43,9 +47,12 @@ static HitExtractor randomHitExtractor(int depth) { @Override protected ScrollCursor mutateInstance(ScrollCursor instance) throws IOException { - return new ScrollCursor(instance.scrollId(), instance.extractors(), - randomValueOtherThan(instance.mask(), () -> CompositeAggregationCursorTests.randomBitSet(instance.extractors().size())), - randomValueOtherThan(instance.limit(), () -> randomIntBetween(1, 1024))); + return new ScrollCursor( + instance.scrollId(), + instance.extractors(), + randomValueOtherThan(instance.mask(), () -> CompositeAggregationCursorTests.randomBitSet(instance.extractors().size())), + randomValueOtherThan(instance.limit(), () -> randomIntBetween(1, 1024)) + ); } @Override diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/SourceGeneratorTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/SourceGeneratorTests.java index 8d838d68d6d63..5e8fffd440bff 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/SourceGeneratorTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/SourceGeneratorTests.java @@ -62,8 +62,10 @@ public void testQueryFilter() { QueryContainer container = new QueryContainer().with(new MatchQuery(Source.EMPTY, "foo", "bar")); QueryBuilder filter = matchQuery("bar", "baz"); SearchSourceBuilder sourceBuilder = SourceGenerator.sourceBuilder(container, filter, randomIntBetween(1, 10)); - assertEquals(boolQuery().must(matchQuery("foo", "bar").operator(Operator.OR)).filter(matchQuery("bar", "baz")), - sourceBuilder.query()); + assertEquals( + boolQuery().must(matchQuery("foo", "bar").operator(Operator.OR)).filter(matchQuery("bar", "baz")), + sourceBuilder.query() + ); } public void testLimit() { @@ -91,8 +93,7 @@ public void testSelectScoreForcesTrackingScore() { } public void testSortScoreSpecified() { - QueryContainer container = new QueryContainer() - .prependSort("id", new ScoreSort(Direction.DESC, null)); + QueryContainer container = new QueryContainer().prependSort("id", new ScoreSort(Direction.DESC, null)); SearchSourceBuilder sourceBuilder = SourceGenerator.sourceBuilder(container, null, randomIntBetween(1, 10)); assertEquals(singletonList(scoreSort()), sourceBuilder.sorts()); } @@ -100,15 +101,17 @@ public void testSortScoreSpecified() { public void testSortFieldSpecified() { FieldSortBuilder sortField = fieldSort("test").unmappedType("keyword"); - QueryContainer container = new QueryContainer() - .prependSort("id", new AttributeSort(new FieldAttribute(Source.EMPTY, "test", new KeywordEsField("test")), - Direction.ASC, Missing.LAST)); + QueryContainer container = new QueryContainer().prependSort( + "id", + new AttributeSort(new FieldAttribute(Source.EMPTY, "test", new KeywordEsField("test")), Direction.ASC, Missing.LAST) + ); SearchSourceBuilder sourceBuilder = SourceGenerator.sourceBuilder(container, null, randomIntBetween(1, 10)); assertEquals(singletonList(sortField.order(SortOrder.ASC).missing("_last")), sourceBuilder.sorts()); - container = new QueryContainer() - .prependSort("id", new AttributeSort(new FieldAttribute(Source.EMPTY, "test", new KeywordEsField("test")), - Direction.DESC, Missing.FIRST)); + container = new QueryContainer().prependSort( + "id", + new AttributeSort(new FieldAttribute(Source.EMPTY, "test", new KeywordEsField("test")), Direction.DESC, Missing.FIRST) + ); sourceBuilder = SourceGenerator.sourceBuilder(container, null, randomIntBetween(1, 10)); assertEquals(singletonList(sortField.order(SortOrder.DESC).missing("_first")), sourceBuilder.sorts()); } @@ -119,16 +122,21 @@ public void testNoSort() { } public void testTrackHits() { - SearchSourceBuilder sourceBuilder = SourceGenerator.sourceBuilder(new QueryContainer().withTrackHits(), null, - randomIntBetween(1, 10)); - assertEquals("Should have tracked hits", Integer.valueOf(SearchContext.TRACK_TOTAL_HITS_ACCURATE), - sourceBuilder.trackTotalHitsUpTo()); + SearchSourceBuilder sourceBuilder = SourceGenerator.sourceBuilder( + new QueryContainer().withTrackHits(), + null, + randomIntBetween(1, 10) + ); + assertEquals( + "Should have tracked hits", + Integer.valueOf(SearchContext.TRACK_TOTAL_HITS_ACCURATE), + sourceBuilder.trackTotalHitsUpTo() + ); } public void testNoSortIfAgg() { - QueryContainer container = new QueryContainer() - .addGroups(singletonList(new GroupByValue("group_id", "group_column"))) - .addAgg("group_id", new AvgAgg("agg_id", AggSource.of("avg_column"))); + QueryContainer container = new QueryContainer().addGroups(singletonList(new GroupByValue("group_id", "group_column"))) + .addAgg("group_id", new AvgAgg("agg_id", AggSource.of("avg_column"))); SearchSourceBuilder sourceBuilder = SourceGenerator.sourceBuilder(container, null, randomIntBetween(1, 10)); assertNull(sourceBuilder.sorts()); } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/SqlSourceBuilderTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/SqlSourceBuilderTests.java index 1f5922c4d1f35..8a467771e04f4 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/SqlSourceBuilderTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/SqlSourceBuilderTests.java @@ -46,8 +46,8 @@ public void testSqlSourceBuilder() { assertThat(fetchFields.get(1).format, equalTo("test")); Map scriptFields = source.scriptFields() - .stream() - .collect(Collectors.toMap(SearchSourceBuilder.ScriptField::fieldName, SearchSourceBuilder.ScriptField::script)); + .stream() + .collect(Collectors.toMap(SearchSourceBuilder.ScriptField::fieldName, SearchSourceBuilder.ScriptField::script)); assertThat(scriptFields.get("baz").getIdOrCode(), equalTo("eggplant")); assertThat(scriptFields.get("baz2").getIdOrCode(), equalTo("potato")); } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/CompositeKeyExtractorTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/CompositeKeyExtractorTests.java index ba4153e8527ae..88773071f465a 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/CompositeKeyExtractorTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/CompositeKeyExtractorTests.java @@ -55,13 +55,13 @@ protected CompositeKeyExtractor mutateInstance(CompositeKeyExtractor instance) { instance.key() + "mutated", randomValueOtherThan(instance.property(), () -> randomFrom(Property.values())), randomValueOtherThan(instance.zoneId(), ESTestCase::randomZone), - instance.isDateTimeBased() == false); + instance.isDateTimeBased() == false + ); } public void testExtractBucketCount() { Bucket bucket = new TestBucket(emptyMap(), randomLong(), new Aggregations(emptyList())); - CompositeKeyExtractor extractor = new CompositeKeyExtractor(randomAlphaOfLength(16), Property.COUNT, - randomZone(), false); + CompositeKeyExtractor extractor = new CompositeKeyExtractor(randomAlphaOfLength(16), Property.COUNT, randomZone(), false); assertEquals(bucket.getDocCount(), extractor.extract(bucket)); } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/ComputingExtractorTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/ComputingExtractorTests.java index 1b35d50be0f99..ef77edc07e3c8 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/ComputingExtractorTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/ComputingExtractorTests.java @@ -66,16 +66,17 @@ protected Reader instanceReader() { @Override protected ComputingExtractor mutateInstance(ComputingExtractor instance) throws IOException { return new ComputingExtractor( - randomValueOtherThan(instance.processor(), () -> randomProcessor()), - randomValueOtherThan(instance.hitName(), () -> randomAlphaOfLength(10)) - ); + randomValueOtherThan(instance.processor(), () -> randomProcessor()), + randomValueOtherThan(instance.hitName(), () -> randomAlphaOfLength(10)) + ); } public void testGet() { String fieldName = randomAlphaOfLength(5); ChainingProcessor extractor = new ChainingProcessor( - new HitExtractorProcessor(new FieldHitExtractor(fieldName, DOUBLE, UTC, false)), - new MathProcessor(MathOperation.LOG)); + new HitExtractorProcessor(new FieldHitExtractor(fieldName, DOUBLE, UTC, false)), + new MathProcessor(MathOperation.LOG) + ); int times = between(1, 1000); for (int i = 0; i < times; i++) { diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/FieldHitExtractorTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/FieldHitExtractorTests.java index a2478b52e1cd2..7b4b91e25add3 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/FieldHitExtractorTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/FieldHitExtractorTests.java @@ -16,6 +16,7 @@ import org.elasticsearch.xpack.sql.proto.StringUtils; import org.elasticsearch.xpack.sql.type.SqlDataTypes; import org.elasticsearch.xpack.sql.util.DateUtils; + import java.math.BigDecimal; import java.math.BigInteger; import java.time.ZoneId; @@ -148,7 +149,7 @@ public void testExtractSourcePath() { SearchHit hit = new SearchHit(1, null, null, singletonMap("a.b.c", field), null); assertThat(fe.extract(hit), is(value)); } - + public void testMultiValuedSource() { FieldHitExtractor fe = getFieldHitExtractor("a"); Object value = randomValue(); @@ -157,7 +158,7 @@ public void testMultiValuedSource() { QlIllegalArgumentException ex = expectThrows(QlIllegalArgumentException.class, () -> fe.extract(hit)); assertThat(ex.getMessage(), is("Arrays (returned by [a]) are not supported")); } - + public void testMultiValuedSourceAllowed() { FieldHitExtractor fe = new FieldHitExtractor("a", null, UTC, true); Object valueA = randomValue(); @@ -179,11 +180,11 @@ public void testGeoShapeExtraction() { assertEquals(new GeoShape(1, 2), fe.extract(hit)); } - + public void testMultipleGeoShapeExtraction() { String fieldName = randomAlphaOfLength(5); FieldHitExtractor fe = new FieldHitExtractor(fieldName, randomBoolean() ? GEO_SHAPE : SHAPE, UTC, false); - + Map map1 = new HashMap<>(2); map1.put("coordinates", asList(1d, 2d)); map1.put("type", "Point"); @@ -195,10 +196,14 @@ public void testMultipleGeoShapeExtraction() { QlIllegalArgumentException ex = expectThrows(QlIllegalArgumentException.class, () -> fe.extract(hit)); assertThat(ex.getMessage(), is("Arrays (returned by [" + fieldName + "]) are not supported")); - + FieldHitExtractor lenientFe = new FieldHitExtractor(fieldName, randomBoolean() ? GEO_SHAPE : SHAPE, UTC, true); - assertEquals(new GeoShape(3, 4), lenientFe.extract(new SearchHit(1, null, null, singletonMap(fieldName, - new DocumentField(fieldName, singletonList(map2))), null))); + assertEquals( + new GeoShape(3, 4), + lenientFe.extract( + new SearchHit(1, null, null, singletonMap(fieldName, new DocumentField(fieldName, singletonList(map2))), null) + ) + ); } private FieldHitExtractor getFieldHitExtractor(String fieldName) { diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/MetricAggExtractorTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/MetricAggExtractorTests.java index 2fab5d965ca23..e0539e2bfcff6 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/MetricAggExtractorTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/MetricAggExtractorTests.java @@ -30,13 +30,23 @@ public class MetricAggExtractorTests extends AbstractSqlWireSerializingTestCase { public static MetricAggExtractor randomMetricAggExtractor() { - return new MetricAggExtractor(randomAlphaOfLength(16), randomAlphaOfLength(16), randomAlphaOfLength(16), - randomZone(), randomFrom(SqlDataTypes.types())); + return new MetricAggExtractor( + randomAlphaOfLength(16), + randomAlphaOfLength(16), + randomAlphaOfLength(16), + randomZone(), + randomFrom(SqlDataTypes.types()) + ); } public static MetricAggExtractor randomMetricAggExtractor(ZoneId zoneId) { - return new MetricAggExtractor(randomAlphaOfLength(16), randomAlphaOfLength(16), randomAlphaOfLength(16), zoneId, - randomFrom(SqlDataTypes.types())); + return new MetricAggExtractor( + randomAlphaOfLength(16), + randomAlphaOfLength(16), + randomAlphaOfLength(16), + zoneId, + randomFrom(SqlDataTypes.types()) + ); } @Override @@ -60,7 +70,9 @@ protected MetricAggExtractor mutateInstance(MetricAggExtractor instance) throws instance.name() + "mutated", instance.property() + "mutated", instance.innerKey() + "mutated", - randomValueOtherThan(instance.zoneId(), ESTestCase::randomZone), randomFrom(SqlDataTypes.types())); + randomValueOtherThan(instance.zoneId(), ESTestCase::randomZone), + randomFrom(SqlDataTypes.types()) + ); } public void testNoAggs() { @@ -86,14 +98,17 @@ public void testSingleValuePropertyDate() { double value = randomDouble(); Aggregation agg = new TestSingleValueAggregation(extractor.name(), singletonList(extractor.property()), value); Bucket bucket = new TestBucket(emptyMap(), 0, new Aggregations(singletonList(agg))); - assertEquals(DateUtils.asDateTimeWithMillis((long) value , zoneId), extractor.extract(bucket)); + assertEquals(DateUtils.asDateTimeWithMillis((long) value, zoneId), extractor.extract(bucket)); } public void testSingleValueInnerKey() { MetricAggExtractor extractor = new MetricAggExtractor("field", "property", "innerKey", null); double innerValue = randomDouble(); - Aggregation agg = new TestSingleValueAggregation(extractor.name(), singletonList(extractor.property()), - singletonMap(extractor.innerKey(), innerValue)); + Aggregation agg = new TestSingleValueAggregation( + extractor.name(), + singletonList(extractor.property()), + singletonMap(extractor.innerKey(), innerValue) + ); Bucket bucket = new TestBucket(emptyMap(), 0, new Aggregations(singletonList(agg))); assertEquals(innerValue, extractor.extract(bucket)); } @@ -103,10 +118,13 @@ public void testSingleValueInnerKeyDate() { MetricAggExtractor extractor = new MetricAggExtractor("field", "property", "innerKey", zoneId, DATE); double innerValue = randomDouble(); - Aggregation agg = new TestSingleValueAggregation(extractor.name(), singletonList(extractor.property()), - singletonMap(extractor.innerKey(), innerValue)); + Aggregation agg = new TestSingleValueAggregation( + extractor.name(), + singletonList(extractor.property()), + singletonMap(extractor.innerKey(), innerValue) + ); Bucket bucket = new TestBucket(emptyMap(), 0, new Aggregations(singletonList(agg))); - assertEquals(DateUtils.asDateTimeWithMillis((long) innerValue , zoneId), extractor.extract(bucket)); + assertEquals(DateUtils.asDateTimeWithMillis((long) innerValue, zoneId), extractor.extract(bucket)); } public void testMultiValueProperty() { @@ -125,7 +143,7 @@ public void testMultiValuePropertyDate() { double value = randomDouble(); Aggregation agg = new TestMultiValueAggregation(extractor.name(), singletonMap(extractor.property(), value)); Bucket bucket = new TestBucket(emptyMap(), 0, new Aggregations(singletonList(agg))); - assertEquals(DateUtils.asDateTimeWithMillis((long) value , zoneId), extractor.extract(bucket)); + assertEquals(DateUtils.asDateTimeWithMillis((long) value, zoneId), extractor.extract(bucket)); } public static ZoneId extractZoneId(BucketExtractor extractor) { diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/TestBucket.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/TestBucket.java index 68c6ad83df3e3..2233ce94c0985 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/TestBucket.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/TestBucket.java @@ -6,9 +6,9 @@ */ package org.elasticsearch.xpack.sql.execution.search.extractor; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.search.aggregations.Aggregations; import org.elasticsearch.search.aggregations.bucket.composite.CompositeAggregation.Bucket; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.Map; diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/TestMultiValueAggregation.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/TestMultiValueAggregation.java index 94a0b92696e1a..2d806e3e6f114 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/TestMultiValueAggregation.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/TestMultiValueAggregation.java @@ -7,9 +7,9 @@ package org.elasticsearch.xpack.sql.execution.search.extractor; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.metrics.InternalNumericMetricsAggregation; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.List; diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/TestSingleValueAggregation.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/TestSingleValueAggregation.java index 3e0c9648f5660..ea63c797cac7b 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/TestSingleValueAggregation.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/TestSingleValueAggregation.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.sql.execution.search.extractor; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.search.aggregations.InternalAggregation; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.List; diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/TopHitsAggExtractorTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/TopHitsAggExtractorTests.java index 820a1b1df4755..2efe12629f739 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/TopHitsAggExtractorTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/TopHitsAggExtractorTests.java @@ -22,6 +22,7 @@ import org.elasticsearch.xpack.sql.proto.StringUtils; import org.elasticsearch.xpack.sql.type.SqlDataTypes; import org.elasticsearch.xpack.sql.util.DateUtils; + import java.time.ZoneId; import java.util.Collections; @@ -55,8 +56,9 @@ protected ZoneId instanceZoneId(TopHitsAggExtractor instance) { protected TopHitsAggExtractor mutateInstance(TopHitsAggExtractor instance) { return new TopHitsAggExtractor( instance.name() + "mutated", - randomValueOtherThan(instance.fieldDataType(), () -> randomFrom(SqlDataTypes.types())), - randomValueOtherThan(instance.zoneId(), ESTestCase::randomZone)); + randomValueOtherThan(instance.fieldDataType(), () -> randomFrom(SqlDataTypes.types())), + randomValueOtherThan(instance.zoneId(), ESTestCase::randomZone) + ); } public void testNoAggs() { @@ -89,16 +91,30 @@ public void testExtractDateValue() { TopHitsAggExtractor extractor = new TopHitsAggExtractor("topHitsAgg", DataTypes.DATETIME, zoneId); long value = 123456789L; - Aggregation agg = new InternalTopHits(extractor.name(), 0, 1, null, - searchHitsOf(StringUtils.toString(DateUtils.asDateTimeWithMillis(value, zoneId))), null); + Aggregation agg = new InternalTopHits( + extractor.name(), + 0, + 1, + null, + searchHitsOf(StringUtils.toString(DateUtils.asDateTimeWithMillis(value, zoneId))), + null + ); Bucket bucket = new TestBucket(emptyMap(), 0, new Aggregations(singletonList(agg))); assertEquals(DateUtils.asDateTimeWithMillis(value, zoneId), extractor.extract(bucket)); } private SearchHits searchHitsOf(Object value) { TotalHits totalHits = new TotalHits(10, TotalHits.Relation.EQUAL_TO); - return new SearchHits(new SearchHit[] {new SearchHit(1, "docId", null, - Collections.singletonMap("topHitsAgg", new DocumentField("field", Collections.singletonList(value))))}, - totalHits, 0.0f); + return new SearchHits( + new SearchHit[] { + new SearchHit( + 1, + "docId", + null, + Collections.singletonMap("topHitsAgg", new DocumentField("field", Collections.singletonList(value))) + ) }, + totalHits, + 0.0f + ); } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/ProcessorTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/ProcessorTests.java index 7c06a07c5a983..c8e76b0ac36a4 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/ProcessorTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/ProcessorTests.java @@ -32,10 +32,11 @@ public static void init() throws Exception { } public void testProcessorRegistration() throws Exception { - LinkedHashSet registered = Processors.getNamedWriteables().stream() - .filter(e -> Processor.class == e.categoryClass) - .map(e -> e.name) - .collect(toCollection(LinkedHashSet::new)); + LinkedHashSet registered = Processors.getNamedWriteables() + .stream() + .filter(e -> Processor.class == e.categoryClass) + .map(e -> e.name) + .collect(toCollection(LinkedHashSet::new)); // discover available processors int missing = processors.size() - registered.size(); diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/SqlFunctionRegistryTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/SqlFunctionRegistryTests.java index 07bc693191b0b..5f809b8c6d24e 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/SqlFunctionRegistryTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/SqlFunctionRegistryTests.java @@ -38,13 +38,11 @@ public void testNoArgFunction() { FunctionDefinition def = r.resolveFunction(ur.name()); // Distinct isn't supported - ParsingException e = expectThrows(ParsingException.class, () -> - uf(DISTINCT).buildResolved(randomConfiguration(), def)); + ParsingException e = expectThrows(ParsingException.class, () -> uf(DISTINCT).buildResolved(randomConfiguration(), def)); assertThat(e.getMessage(), endsWith("Cannot use DISTINCT on non-SQL function DUMMY_FUNCTION()")); // Any children aren't supported - e = expectThrows(ParsingException.class, () -> - uf(DEFAULT, mock(Expression.class)).buildResolved(randomConfiguration(), def)); + e = expectThrows(ParsingException.class, () -> uf(DEFAULT, mock(Expression.class)).buildResolved(randomConfiguration(), def)); assertThat(e.getMessage(), endsWith("expects no arguments")); } @@ -54,18 +52,21 @@ public void testUnaryFunction() { FunctionDefinition def = r.resolveFunction(ur.name()); // Distinct isn't supported - ParsingException e = expectThrows(ParsingException.class, () -> - uf(DISTINCT, mock(Expression.class)).buildResolved(randomConfiguration(), def)); + ParsingException e = expectThrows( + ParsingException.class, + () -> uf(DISTINCT, mock(Expression.class)).buildResolved(randomConfiguration(), def) + ); assertThat(e.getMessage(), endsWith("Cannot use DISTINCT on non-SQL function DUMMY_FUNCTION()")); // No children aren't supported - e = expectThrows(ParsingException.class, () -> - uf(DEFAULT).buildResolved(randomConfiguration(), def)); + e = expectThrows(ParsingException.class, () -> uf(DEFAULT).buildResolved(randomConfiguration(), def)); assertThat(e.getMessage(), endsWith("expects exactly one argument")); // Multiple children aren't supported - e = expectThrows(ParsingException.class, () -> - uf(DEFAULT, mock(Expression.class), mock(Expression.class)).buildResolved(randomConfiguration(), def)); + e = expectThrows( + ParsingException.class, + () -> uf(DEFAULT, mock(Expression.class), mock(Expression.class)).buildResolved(randomConfiguration(), def) + ); assertThat(e.getMessage(), endsWith("expects exactly one argument")); } @@ -85,13 +86,14 @@ public void testUnaryDistinctAwareFunction() { assertFalse(((SqlFunctionDefinition) def).extractViable()); // No children aren't supported - ParsingException e = expectThrows(ParsingException.class, () -> - uf(DEFAULT).buildResolved(randomConfiguration(), def)); + ParsingException e = expectThrows(ParsingException.class, () -> uf(DEFAULT).buildResolved(randomConfiguration(), def)); assertThat(e.getMessage(), endsWith("expects exactly one argument")); // Multiple children aren't supported - e = expectThrows(ParsingException.class, () -> - uf(DEFAULT, mock(Expression.class), mock(Expression.class)).buildResolved(randomConfiguration(), def)); + e = expectThrows( + ParsingException.class, + () -> uf(DEFAULT, mock(Expression.class), mock(Expression.class)).buildResolved(randomConfiguration(), def) + ); assertThat(e.getMessage(), endsWith("expects exactly one argument")); } @@ -112,19 +114,15 @@ public void testDateTimeFunction() { assertTrue(((SqlFunctionDefinition) def).extractViable()); // Distinct isn't supported - ParsingException e = expectThrows(ParsingException.class, () -> - uf(DISTINCT, exprMock).buildResolved(providedConfiguration, def)); + ParsingException e = expectThrows(ParsingException.class, () -> uf(DISTINCT, exprMock).buildResolved(providedConfiguration, def)); assertThat(e.getMessage(), endsWith("does not support DISTINCT yet it was specified")); // No children aren't supported - e = expectThrows(ParsingException.class, () -> - uf(DEFAULT).buildResolved(randomConfiguration(), def)); + e = expectThrows(ParsingException.class, () -> uf(DEFAULT).buildResolved(randomConfiguration(), def)); assertThat(e.getMessage(), endsWith("expects exactly one argument")); // Multiple children aren't supported - e = expectThrows(ParsingException.class, () -> - uf(DEFAULT, exprMock, exprMock).buildResolved(randomConfiguration(), def)); + e = expectThrows(ParsingException.class, () -> uf(DEFAULT, exprMock, exprMock).buildResolved(randomConfiguration(), def)); assertThat(e.getMessage(), endsWith("expects exactly one argument")); } } - diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/DatabaseFunctionTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/DatabaseFunctionTests.java index d8b14bd545126..84db6a231c02a 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/DatabaseFunctionTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/DatabaseFunctionTests.java @@ -30,16 +30,25 @@ public void testDatabaseFunctionOutput() { String clusterName = randomAlphaOfLengthBetween(1, 15); SqlParser parser = new SqlParser(); EsIndex test = new EsIndex("test", SqlTypesTests.loadMapping("mapping-basic.json", true)); - SqlConfiguration sqlConfig = new SqlConfiguration(DateUtils.UTC, null, Protocol.FETCH_SIZE, Protocol.REQUEST_TIMEOUT, - Protocol.PAGE_TIMEOUT, null, null, - randomFrom(Mode.values()), randomAlphaOfLength(10), - null, null, clusterName, randomBoolean(), randomBoolean(), null, null); - Analyzer analyzer = new Analyzer( - sqlConfig, - new SqlFunctionRegistry(), - IndexResolution.valid(test), - new Verifier(new Metrics()) + SqlConfiguration sqlConfig = new SqlConfiguration( + DateUtils.UTC, + null, + Protocol.FETCH_SIZE, + Protocol.REQUEST_TIMEOUT, + Protocol.PAGE_TIMEOUT, + null, + null, + randomFrom(Mode.values()), + randomAlphaOfLength(10), + null, + null, + clusterName, + randomBoolean(), + randomBoolean(), + null, + null ); + Analyzer analyzer = new Analyzer(sqlConfig, new SqlFunctionRegistry(), IndexResolution.valid(test), new Verifier(new Metrics())); Project result = (Project) analyzer.analyze(parser.createStatement("SELECT DATABASE()"), true); NamedExpression ne = result.projections().get(0); diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/UserFunctionTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/UserFunctionTests.java index 90789bd37f57e..67090f21a209f 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/UserFunctionTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/UserFunctionTests.java @@ -29,17 +29,25 @@ public class UserFunctionTests extends ESTestCase { public void testNoUsernameFunctionOutput() { SqlParser parser = new SqlParser(); EsIndex test = new EsIndex("test", SqlTypesTests.loadMapping("mapping-basic.json", true)); - SqlConfiguration sqlConfig = new SqlConfiguration(DateUtils.UTC, null, Protocol.FETCH_SIZE, Protocol.REQUEST_TIMEOUT, - Protocol.PAGE_TIMEOUT, null, null, - randomFrom(Mode.values()), randomAlphaOfLength(10), - null, null, randomAlphaOfLengthBetween(1, 15), - randomBoolean(), randomBoolean(), null, null); - Analyzer analyzer = new Analyzer( - sqlConfig, - new SqlFunctionRegistry(), - IndexResolution.valid(test), - new Verifier(new Metrics()) + SqlConfiguration sqlConfig = new SqlConfiguration( + DateUtils.UTC, + null, + Protocol.FETCH_SIZE, + Protocol.REQUEST_TIMEOUT, + Protocol.PAGE_TIMEOUT, + null, + null, + randomFrom(Mode.values()), + randomAlphaOfLength(10), + null, + null, + randomAlphaOfLengthBetween(1, 15), + randomBoolean(), + randomBoolean(), + null, + null ); + Analyzer analyzer = new Analyzer(sqlConfig, new SqlFunctionRegistry(), IndexResolution.valid(test), new Verifier(new Metrics())); Project result = (Project) analyzer.analyze(parser.createStatement("SELECT USER()"), true); NamedExpression ne = result.projections().get(0); diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/CurrentDateTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/CurrentDateTests.java index 0a099d1aa739e..ff25695c68d66 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/CurrentDateTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/CurrentDateTests.java @@ -35,16 +35,16 @@ protected CurrentDate copy(CurrentDate instance) { @Override protected CurrentDate mutate(CurrentDate instance) { ZonedDateTime now = instance.configuration().now(); - ZoneId mutatedZoneId = randomValueOtherThanMany(o -> Objects.equals(now.getOffset(), o.getRules().getOffset(now.toInstant())), - () -> randomZone()); + ZoneId mutatedZoneId = randomValueOtherThanMany( + o -> Objects.equals(now.getOffset(), o.getRules().getOffset(now.toInstant())), + () -> randomZone() + ); return new CurrentDate(instance.source(), SqlTestUtils.randomConfiguration(mutatedZoneId)); } @Override - public void testTransform() { - } + public void testTransform() {} @Override - public void testReplaceChildren() { - } + public void testReplaceChildren() {} } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/CurrentDateTimeTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/CurrentDateTimeTests.java index 996f8c9ba6faa..090794b869416 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/CurrentDateTimeTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/CurrentDateTimeTests.java @@ -49,18 +49,18 @@ protected CurrentDateTime copy(CurrentDateTime instance) { @Override protected CurrentDateTime mutate(CurrentDateTime instance) { ZonedDateTime now = instance.configuration().now(); - ZoneId mutatedZoneId = randomValueOtherThanMany(o -> Objects.equals(now.getOffset(), o.getRules().getOffset(now.toInstant())), - ESTestCase::randomZone); + ZoneId mutatedZoneId = randomValueOtherThanMany( + o -> Objects.equals(now.getOffset(), o.getRules().getOffset(now.toInstant())), + ESTestCase::randomZone + ); return new CurrentDateTime(instance.source(), literal(randomInt(9)), SqlTestUtils.randomConfiguration(mutatedZoneId)); } @Override - public void testTransform() { - } + public void testTransform() {} @Override - public void testReplaceChildren() { - } + public void testReplaceChildren() {} public void testNanoPrecision() { ZonedDateTime zdt = ZonedDateTime.parse("2018-01-23T12:34:45.123456789Z"); @@ -89,16 +89,18 @@ public void testDefaultPrecision() { public void testInvalidPrecision() { SqlParser parser = new SqlParser(); - IndexResolution indexResolution = IndexResolution.valid(new EsIndex("test", - SqlTypesTests.loadMapping("mapping-multi-field-with-nested.json"))); + IndexResolution indexResolution = IndexResolution.valid( + new EsIndex("test", SqlTypesTests.loadMapping("mapping-multi-field-with-nested.json")) + ); Analyzer analyzer = new Analyzer(SqlTestUtils.TEST_CFG, new SqlFunctionRegistry(), indexResolution, new Verifier(new Metrics())); - ParsingException e = expectThrows(ParsingException.class, () -> - analyzer.analyze(parser.createStatement("SELECT CURRENT_TIMESTAMP(100000000000000)"), true)); + ParsingException e = expectThrows( + ParsingException.class, + () -> analyzer.analyze(parser.createStatement("SELECT CURRENT_TIMESTAMP(100000000000000)"), true) + ); assertEquals("line 1:27: invalid precision; [100000000000000] out of [integer] range", e.getMessage()); - e = expectThrows(ParsingException.class, () -> - analyzer.analyze(parser.createStatement("SELECT CURRENT_TIMESTAMP(100)"), true)); + e = expectThrows(ParsingException.class, () -> analyzer.analyze(parser.createStatement("SELECT CURRENT_TIMESTAMP(100)"), true)); assertEquals("line 1:27: precision needs to be between [0-9], received [100]", e.getMessage()); } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/CurrentTimeTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/CurrentTimeTests.java index 24dbe8db22f70..4c845728f1c2b 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/CurrentTimeTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/CurrentTimeTests.java @@ -50,18 +50,18 @@ protected CurrentTime copy(CurrentTime instance) { @Override protected CurrentTime mutate(CurrentTime instance) { ZonedDateTime now = instance.configuration().now(); - ZoneId mutatedZoneId = randomValueOtherThanMany(o -> Objects.equals(now.getOffset(), o.getRules().getOffset(now.toInstant())), - ESTestCase::randomZone); + ZoneId mutatedZoneId = randomValueOtherThanMany( + o -> Objects.equals(now.getOffset(), o.getRules().getOffset(now.toInstant())), + ESTestCase::randomZone + ); return new CurrentTime(instance.source(), literal(randomInt(9)), SqlTestUtils.randomConfiguration(mutatedZoneId)); } @Override - public void testTransform() { - } + public void testTransform() {} @Override - public void testReplaceChildren() { - } + public void testReplaceChildren() {} public void testNanoPrecision() { OffsetTime ot = OffsetTime.parse("12:34:45.123456789Z"); @@ -90,16 +90,18 @@ public void testDefaultPrecision() { public void testInvalidPrecision() { SqlParser parser = new SqlParser(); - IndexResolution indexResolution = IndexResolution.valid(new EsIndex("test", - SqlTypesTests.loadMapping("mapping-multi-field-with-nested.json"))); + IndexResolution indexResolution = IndexResolution.valid( + new EsIndex("test", SqlTypesTests.loadMapping("mapping-multi-field-with-nested.json")) + ); Analyzer analyzer = new Analyzer(SqlTestUtils.TEST_CFG, new SqlFunctionRegistry(), indexResolution, new Verifier(new Metrics())); - ParsingException e = expectThrows(ParsingException.class, () -> - analyzer.analyze(parser.createStatement("SELECT CURRENT_TIME(100000000000000)"), true)); + ParsingException e = expectThrows( + ParsingException.class, + () -> analyzer.analyze(parser.createStatement("SELECT CURRENT_TIME(100000000000000)"), true) + ); assertEquals("line 1:22: invalid precision; [100000000000000] out of [integer] range", e.getMessage()); - e = expectThrows(ParsingException.class, () -> - analyzer.analyze(parser.createStatement("SELECT CURRENT_TIME(100)"), true)); + e = expectThrows(ParsingException.class, () -> analyzer.analyze(parser.createStatement("SELECT CURRENT_TIME(100)"), true)); assertEquals("line 1:22: precision needs to be between [0-9], received [100]", e.getMessage()); } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateAddPipeTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateAddPipeTests.java index 8553d454c087d..9b0dfca616be4 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateAddPipeTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateAddPipeTests.java @@ -39,13 +39,8 @@ private Expression randomDateAddPipeExpression() { } public static DateAddPipe randomDateAddPipe() { - return (DateAddPipe) new DateAdd( - randomSource(), - randomStringLiteral(), - randomIntLiteral(), - randomDatetimeLiteral(), - randomZone()) - .makePipe(); + return (DateAddPipe) new DateAdd(randomSource(), randomStringLiteral(), randomIntLiteral(), randomDatetimeLiteral(), randomZone()) + .makePipe(); } @Override @@ -55,26 +50,13 @@ public void testTransform() { DateAddPipe b1 = randomInstance(); Expression newExpression = randomValueOtherThan(b1.expression(), this::randomDateAddPipeExpression); - DateAddPipe newB = new DateAddPipe( - b1.source(), - newExpression, - b1.first(), - b1.second(), - b1.third(), - b1.zoneId()); + DateAddPipe newB = new DateAddPipe(b1.source(), newExpression, b1.first(), b1.second(), b1.third(), b1.zoneId()); assertEquals(newB, b1.transformPropertiesOnly(Expression.class, v -> Objects.equals(v, b1.expression()) ? newExpression : v)); DateAddPipe b2 = randomInstance(); Source newLoc = randomValueOtherThan(b2.source(), SourceTests::randomSource); - newB = new DateAddPipe( - newLoc, - b2.expression(), - b2.first(), - b2.second(), - b2.third(), - b2.zoneId()); - assertEquals(newB, - b2.transformPropertiesOnly(Source.class, v -> Objects.equals(v, b2.source()) ? newLoc : v)); + newB = new DateAddPipe(newLoc, b2.expression(), b2.first(), b2.second(), b2.third(), b2.zoneId()); + assertEquals(newB, b2.transformPropertiesOnly(Source.class, v -> Objects.equals(v, b2.source()) ? newLoc : v)); } @Override @@ -84,7 +66,7 @@ public void testReplaceChildren() { Pipe newSecond = pipe(((Expression) randomValueOtherThan(b.second(), FunctionTestUtils::randomIntLiteral))); Pipe newThird = pipe(((Expression) randomValueOtherThan(b.third(), FunctionTestUtils::randomDatetimeLiteral))); ZoneId newZoneId = randomValueOtherThan(b.zoneId(), ESTestCase::randomZone); - DateAddPipe newB = new DateAddPipe( b.source(), b.expression(), b.first(), b.second(), b.third(), newZoneId); + DateAddPipe newB = new DateAddPipe(b.source(), b.expression(), b.first(), b.second(), b.third(), newZoneId); ThreeArgsDateTimePipe transformed = newB.replaceChildren(newFirst, b.second(), b.third()); assertEquals(transformed.source(), b.source()); @@ -116,26 +98,46 @@ public void testReplaceChildren() { @Override protected DateAddPipe mutate(DateAddPipe instance) { List> randoms = new ArrayList<>(); - randoms.add(f -> new DateAddPipe(f.source(), f.expression(), + randoms.add( + f -> new DateAddPipe( + f.source(), + f.expression(), pipe(((Expression) randomValueOtherThan(f.first(), FunctionTestUtils::randomStringLiteral))), f.second(), f.third(), - randomValueOtherThan(f.zoneId(), ESTestCase::randomZone))); - randoms.add(f -> new DateAddPipe(f.source(), f.expression(), + randomValueOtherThan(f.zoneId(), ESTestCase::randomZone) + ) + ); + randoms.add( + f -> new DateAddPipe( + f.source(), + f.expression(), f.first(), pipe(((Expression) randomValueOtherThan(f.second(), FunctionTestUtils::randomIntLiteral))), f.third(), - randomValueOtherThan(f.zoneId(), ESTestCase::randomZone))); - randoms.add(f -> new DateAddPipe(f.source(), f.expression(), + randomValueOtherThan(f.zoneId(), ESTestCase::randomZone) + ) + ); + randoms.add( + f -> new DateAddPipe( + f.source(), + f.expression(), f.first(), f.second(), pipe(((Expression) randomValueOtherThan(f.third(), FunctionTestUtils::randomDatetimeLiteral))), - randomValueOtherThan(f.zoneId(), ESTestCase::randomZone))); - randoms.add(f -> new DateAddPipe(f.source(), f.expression(), + randomValueOtherThan(f.zoneId(), ESTestCase::randomZone) + ) + ); + randoms.add( + f -> new DateAddPipe( + f.source(), + f.expression(), pipe(((Expression) randomValueOtherThan(f.first(), FunctionTestUtils::randomStringLiteral))), pipe(((Expression) randomValueOtherThan(f.second(), FunctionTestUtils::randomIntLiteral))), pipe(((Expression) randomValueOtherThan(f.third(), FunctionTestUtils::randomDatetimeLiteral))), - randomValueOtherThan(f.zoneId(), ESTestCase::randomZone))); + randomValueOtherThan(f.zoneId(), ESTestCase::randomZone) + ) + ); return randomFrom(randoms).apply(instance); } @@ -148,6 +150,7 @@ protected DateAddPipe copy(DateAddPipe instance) { instance.first(), instance.second(), instance.third(), - instance.zoneId()); + instance.zoneId() + ); } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateAddProcessorTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateAddProcessorTests.java index 8dc40dc8dbb86..98cfda78b0fff 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateAddProcessorTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateAddProcessorTests.java @@ -32,7 +32,8 @@ public static DateAddProcessor randomDateAddProcessor() { new ConstantProcessor(randomRealisticUnicodeOfLengthBetween(0, 128)), new ConstantProcessor(randomInt()), new ConstantProcessor(DateTimeTestUtils.nowWithMillisResolution()), - randomZone()); + randomZone() + ); } @Override @@ -56,144 +57,231 @@ protected DateAddProcessor mutateInstance(DateAddProcessor instance) { new ConstantProcessor(ESTestCase.randomRealisticUnicodeOfLength(128)), new ConstantProcessor(randomValueOtherThan((Integer) instance.second().process(null), ESTestCase::randomInt)), new ConstantProcessor(DateTimeTestUtils.nowWithMillisResolution()), - randomValueOtherThan(instance.zoneId(), ESTestCase::randomZone)); + randomValueOtherThan(instance.zoneId(), ESTestCase::randomZone) + ); } public void testInvalidInputs() { - SqlIllegalArgumentException siae = expectThrows(SqlIllegalArgumentException.class, - () -> new DateAdd(Source.EMPTY, - l(5), l(10), randomDatetimeLiteral(), randomZone()).makePipe().asProcessor().process(null)); + SqlIllegalArgumentException siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new DateAdd(Source.EMPTY, l(5), l(10), randomDatetimeLiteral(), randomZone()).makePipe().asProcessor().process(null) + ); assertEquals("A string is required; received [5]", siae.getMessage()); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new DateAdd(Source.EMPTY, - l("days"), l("foo"), randomDatetimeLiteral(), randomZone()).makePipe().asProcessor().process(null)); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new DateAdd(Source.EMPTY, l("days"), l("foo"), randomDatetimeLiteral(), randomZone()).makePipe() + .asProcessor() + .process(null) + ); assertEquals("A number is required; received [foo]", siae.getMessage()); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new DateAdd(Source.EMPTY, - l("days"), l(10), l("foo"), randomZone()).makePipe().asProcessor().process(null)); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new DateAdd(Source.EMPTY, l("days"), l(10), l("foo"), randomZone()).makePipe().asProcessor().process(null) + ); assertEquals("A date/datetime is required; received [foo]", siae.getMessage()); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new DateAdd(Source.EMPTY, - l("invalid"), l(10), randomDatetimeLiteral(), randomZone()).makePipe().asProcessor().process(null)); - assertEquals("A value of [YEAR, QUARTER, MONTH, DAYOFYEAR, DAY, WEEK, WEEKDAY, HOUR, MINUTE, " + - "SECOND, MILLISECOND, MICROSECOND, NANOSECOND] or their aliases is required; received [invalid]", - siae.getMessage()); - - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new DateAdd(Source.EMPTY, - l("quertar"), l(10), randomDatetimeLiteral(), randomZone()).makePipe().asProcessor().process(null)); - assertEquals("Received value [quertar] is not valid date part to add; did you mean [quarter, quarters]?", - siae.getMessage()); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new DateAdd(Source.EMPTY, l("invalid"), l(10), randomDatetimeLiteral(), randomZone()).makePipe() + .asProcessor() + .process(null) + ); + assertEquals( + "A value of [YEAR, QUARTER, MONTH, DAYOFYEAR, DAY, WEEK, WEEKDAY, HOUR, MINUTE, " + + "SECOND, MILLISECOND, MICROSECOND, NANOSECOND] or their aliases is required; received [invalid]", + siae.getMessage() + ); + + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new DateAdd(Source.EMPTY, l("quertar"), l(10), randomDatetimeLiteral(), randomZone()).makePipe() + .asProcessor() + .process(null) + ); + assertEquals("Received value [quertar] is not valid date part to add; did you mean [quarter, quarters]?", siae.getMessage()); } public void testWithNulls() { - assertNull(new DateAdd(Source.EMPTY, - NULL, randomIntLiteral(), randomDatetimeLiteral(), randomZone()).makePipe().asProcessor().process(null)); - assertNull(new DateAdd(Source.EMPTY, - l("days"), NULL, randomDatetimeLiteral(), randomZone()).makePipe().asProcessor().process(null)); - assertNull(new DateAdd(Source.EMPTY, - l("days"), randomIntLiteral(), NULL, randomZone()).makePipe().asProcessor().process(null)); - assertNull(new DateAdd(Source.EMPTY, - NULL, NULL, NULL, randomZone()).makePipe().asProcessor().process(null)); + assertNull( + new DateAdd(Source.EMPTY, NULL, randomIntLiteral(), randomDatetimeLiteral(), randomZone()).makePipe() + .asProcessor() + .process(null) + ); + assertNull( + new DateAdd(Source.EMPTY, l("days"), NULL, randomDatetimeLiteral(), randomZone()).makePipe().asProcessor().process(null) + ); + assertNull(new DateAdd(Source.EMPTY, l("days"), randomIntLiteral(), NULL, randomZone()).makePipe().asProcessor().process(null)); + assertNull(new DateAdd(Source.EMPTY, NULL, NULL, NULL, randomZone()).makePipe().asProcessor().process(null)); } public void testAddition() { ZoneId zoneId = ZoneId.of("Etc/GMT-10"); Literal dateTime = l(dateTime(2019, 9, 3, 18, 10, 37, 123456789)); - assertEquals("2029-09-04T04:10:37.123456789+10:00", - toString((ZonedDateTime) new DateAdd(Source.EMPTY, l("years"), l(10), dateTime, zoneId) - .makePipe().asProcessor().process(null))); - assertEquals("2009-09-04T04:10:37.123456789+10:00", - toString((ZonedDateTime) new DateAdd(Source.EMPTY, l("years"), l(-10), dateTime, zoneId) - .makePipe().asProcessor().process(null))); - - assertEquals("2022-03-04T04:10:37.123456789+10:00", - toString((ZonedDateTime) new DateAdd(Source.EMPTY, l("quarters"), l(10), dateTime, zoneId) - .makePipe().asProcessor().process(null))); - assertEquals("2017-03-04T04:10:37.123456789+10:00", - toString((ZonedDateTime) new DateAdd(Source.EMPTY, l("quarters"), l(-10), dateTime, zoneId) - .makePipe().asProcessor().process(null))); - - assertEquals("2021-05-04T04:10:37.123456789+10:00", - toString((ZonedDateTime) new DateAdd(Source.EMPTY, l("month"), l(20), dateTime, zoneId) - .makePipe().asProcessor().process(null))); - assertEquals("2018-01-04T04:10:37.123456789+10:00", - toString((ZonedDateTime) new DateAdd(Source.EMPTY, l("month"), l(-20), dateTime, zoneId) - .makePipe().asProcessor().process(null))); - - assertEquals("2020-05-01T04:10:37.123456789+10:00", - toString((ZonedDateTime) new DateAdd(Source.EMPTY, l("day"), l(240), dateTime, zoneId) - .makePipe().asProcessor().process(null))); - assertEquals("2019-05-07T04:10:37.123456789+10:00", - toString((ZonedDateTime) new DateAdd(Source.EMPTY, l("day"), l(-120), dateTime, zoneId) - .makePipe().asProcessor().process(null))); - - assertEquals("2020-12-25T04:10:37.123456789+10:00", - toString((ZonedDateTime) new DateAdd(Source.EMPTY, l("dayofyear"), l(478), dateTime, zoneId) - .makePipe().asProcessor().process(null))); - assertEquals("2018-05-14T04:10:37.123456789+10:00", - toString((ZonedDateTime) new DateAdd(Source.EMPTY, l("dayofyear"), l(-478), dateTime, zoneId) - .makePipe().asProcessor().process(null))); - - assertEquals("2021-12-22T04:10:37.123456789+10:00", - toString((ZonedDateTime) new DateAdd(Source.EMPTY, l("weeks"), l(120), dateTime, zoneId) - .makePipe().asProcessor().process(null))); - assertEquals("2017-05-17T04:10:37.123456789+10:00", - toString((ZonedDateTime) new DateAdd(Source.EMPTY, l("weeks"), l(-120), dateTime, zoneId) - .makePipe().asProcessor().process(null))); - - assertEquals("2053-06-22T04:10:37.123456789+10:00", - toString((ZonedDateTime) new DateAdd(Source.EMPTY, l("weekday"), l(12345), dateTime, zoneId) - .makePipe().asProcessor().process(null))); - assertEquals("1985-11-16T04:10:37.123456789+10:00", - toString((ZonedDateTime) new DateAdd(Source.EMPTY, l("weekday"), l(-12345), dateTime, zoneId) - .makePipe().asProcessor().process(null))); - - assertEquals("2020-07-05T05:10:37.123456789+10:00", - toString((ZonedDateTime) new DateAdd(Source.EMPTY, l("hours"), l(7321), dateTime, zoneId) - .makePipe().asProcessor().process(null))); - assertEquals("2018-11-03T03:10:37.123456789+10:00", - toString((ZonedDateTime) new DateAdd(Source.EMPTY, l("hours"), l(-7321), dateTime, zoneId) - .makePipe().asProcessor().process(null))); - - assertEquals("2021-07-21T01:04:37.123456789+10:00", - toString((ZonedDateTime) new DateAdd(Source.EMPTY, l("minute"), l(987654), dateTime, zoneId) - .makePipe().asProcessor().process(null))); - assertEquals("2017-10-18T07:16:37.123456789+10:00", - toString((ZonedDateTime) new DateAdd(Source.EMPTY, l("minute"), l(-987654), dateTime, zoneId) - .makePipe().asProcessor().process(null))); - - assertEquals("2020-02-01T11:51:31.123456789+10:00", - toString((ZonedDateTime) new DateAdd(Source.EMPTY, l("seconds"), l(12987654), dateTime, zoneId) - .makePipe().asProcessor().process(null))); - assertEquals("2019-04-06T20:29:43.123456789+10:00", - toString((ZonedDateTime) new DateAdd(Source.EMPTY, l("seconds"), l(-12987654), dateTime, zoneId) - .makePipe().asProcessor().process(null))); - - assertEquals("2019-09-19T04:56:42.555456789+10:00", - toString((ZonedDateTime) new DateAdd(Source.EMPTY, l("ms"), l(1298765432), dateTime, zoneId) - .makePipe().asProcessor().process(null))); - assertEquals("2019-08-20T03:24:31.691456789+10:00", - toString((ZonedDateTime) new DateAdd(Source.EMPTY, l("ms"), l(-1298765432), dateTime, zoneId) - .makePipe().asProcessor().process(null))); - - assertEquals("2019-09-04T04:12:41.111110789+10:00", - toString((ZonedDateTime) new DateAdd(Source.EMPTY, l("mcs"), l(123987654), dateTime, zoneId) - .makePipe().asProcessor().process(null))); - assertEquals("2019-09-04T04:08:33.135802789+10:00", - toString((ZonedDateTime) new DateAdd(Source.EMPTY, l("mcs"), l(-123987654), dateTime, zoneId) - .makePipe().asProcessor().process(null))); - - assertEquals("2019-09-04T04:10:37.935855554+10:00", - toString((ZonedDateTime) new DateAdd(Source.EMPTY, l("nanoseconds"), l(812398765), dateTime, zoneId) - .makePipe().asProcessor().process(null))); - assertEquals("2019-09-04T04:10:36.311058024+10:00", - toString((ZonedDateTime) new DateAdd(Source.EMPTY, l("nanoseconds"), l(-812398765), dateTime, zoneId) - .makePipe().asProcessor().process(null))); + assertEquals( + "2029-09-04T04:10:37.123456789+10:00", + toString((ZonedDateTime) new DateAdd(Source.EMPTY, l("years"), l(10), dateTime, zoneId).makePipe().asProcessor().process(null)) + ); + assertEquals( + "2009-09-04T04:10:37.123456789+10:00", + toString((ZonedDateTime) new DateAdd(Source.EMPTY, l("years"), l(-10), dateTime, zoneId).makePipe().asProcessor().process(null)) + ); + + assertEquals( + "2022-03-04T04:10:37.123456789+10:00", + toString( + (ZonedDateTime) new DateAdd(Source.EMPTY, l("quarters"), l(10), dateTime, zoneId).makePipe().asProcessor().process(null) + ) + ); + assertEquals( + "2017-03-04T04:10:37.123456789+10:00", + toString( + (ZonedDateTime) new DateAdd(Source.EMPTY, l("quarters"), l(-10), dateTime, zoneId).makePipe().asProcessor().process(null) + ) + ); + + assertEquals( + "2021-05-04T04:10:37.123456789+10:00", + toString((ZonedDateTime) new DateAdd(Source.EMPTY, l("month"), l(20), dateTime, zoneId).makePipe().asProcessor().process(null)) + ); + assertEquals( + "2018-01-04T04:10:37.123456789+10:00", + toString((ZonedDateTime) new DateAdd(Source.EMPTY, l("month"), l(-20), dateTime, zoneId).makePipe().asProcessor().process(null)) + ); + + assertEquals( + "2020-05-01T04:10:37.123456789+10:00", + toString((ZonedDateTime) new DateAdd(Source.EMPTY, l("day"), l(240), dateTime, zoneId).makePipe().asProcessor().process(null)) + ); + assertEquals( + "2019-05-07T04:10:37.123456789+10:00", + toString((ZonedDateTime) new DateAdd(Source.EMPTY, l("day"), l(-120), dateTime, zoneId).makePipe().asProcessor().process(null)) + ); + + assertEquals( + "2020-12-25T04:10:37.123456789+10:00", + toString( + (ZonedDateTime) new DateAdd(Source.EMPTY, l("dayofyear"), l(478), dateTime, zoneId).makePipe().asProcessor().process(null) + ) + ); + assertEquals( + "2018-05-14T04:10:37.123456789+10:00", + toString( + (ZonedDateTime) new DateAdd(Source.EMPTY, l("dayofyear"), l(-478), dateTime, zoneId).makePipe().asProcessor().process(null) + ) + ); + + assertEquals( + "2021-12-22T04:10:37.123456789+10:00", + toString((ZonedDateTime) new DateAdd(Source.EMPTY, l("weeks"), l(120), dateTime, zoneId).makePipe().asProcessor().process(null)) + ); + assertEquals( + "2017-05-17T04:10:37.123456789+10:00", + toString( + (ZonedDateTime) new DateAdd(Source.EMPTY, l("weeks"), l(-120), dateTime, zoneId).makePipe().asProcessor().process(null) + ) + ); + + assertEquals( + "2053-06-22T04:10:37.123456789+10:00", + toString( + (ZonedDateTime) new DateAdd(Source.EMPTY, l("weekday"), l(12345), dateTime, zoneId).makePipe().asProcessor().process(null) + ) + ); + assertEquals( + "1985-11-16T04:10:37.123456789+10:00", + toString( + (ZonedDateTime) new DateAdd(Source.EMPTY, l("weekday"), l(-12345), dateTime, zoneId).makePipe().asProcessor().process(null) + ) + ); + + assertEquals( + "2020-07-05T05:10:37.123456789+10:00", + toString( + (ZonedDateTime) new DateAdd(Source.EMPTY, l("hours"), l(7321), dateTime, zoneId).makePipe().asProcessor().process(null) + ) + ); + assertEquals( + "2018-11-03T03:10:37.123456789+10:00", + toString( + (ZonedDateTime) new DateAdd(Source.EMPTY, l("hours"), l(-7321), dateTime, zoneId).makePipe().asProcessor().process(null) + ) + ); + + assertEquals( + "2021-07-21T01:04:37.123456789+10:00", + toString( + (ZonedDateTime) new DateAdd(Source.EMPTY, l("minute"), l(987654), dateTime, zoneId).makePipe().asProcessor().process(null) + ) + ); + assertEquals( + "2017-10-18T07:16:37.123456789+10:00", + toString( + (ZonedDateTime) new DateAdd(Source.EMPTY, l("minute"), l(-987654), dateTime, zoneId).makePipe().asProcessor().process(null) + ) + ); + + assertEquals( + "2020-02-01T11:51:31.123456789+10:00", + toString( + (ZonedDateTime) new DateAdd(Source.EMPTY, l("seconds"), l(12987654), dateTime, zoneId).makePipe() + .asProcessor() + .process(null) + ) + ); + assertEquals( + "2019-04-06T20:29:43.123456789+10:00", + toString( + (ZonedDateTime) new DateAdd(Source.EMPTY, l("seconds"), l(-12987654), dateTime, zoneId).makePipe() + .asProcessor() + .process(null) + ) + ); + + assertEquals( + "2019-09-19T04:56:42.555456789+10:00", + toString( + (ZonedDateTime) new DateAdd(Source.EMPTY, l("ms"), l(1298765432), dateTime, zoneId).makePipe().asProcessor().process(null) + ) + ); + assertEquals( + "2019-08-20T03:24:31.691456789+10:00", + toString( + (ZonedDateTime) new DateAdd(Source.EMPTY, l("ms"), l(-1298765432), dateTime, zoneId).makePipe().asProcessor().process(null) + ) + ); + + assertEquals( + "2019-09-04T04:12:41.111110789+10:00", + toString( + (ZonedDateTime) new DateAdd(Source.EMPTY, l("mcs"), l(123987654), dateTime, zoneId).makePipe().asProcessor().process(null) + ) + ); + assertEquals( + "2019-09-04T04:08:33.135802789+10:00", + toString( + (ZonedDateTime) new DateAdd(Source.EMPTY, l("mcs"), l(-123987654), dateTime, zoneId).makePipe().asProcessor().process(null) + ) + ); + + assertEquals( + "2019-09-04T04:10:37.935855554+10:00", + toString( + (ZonedDateTime) new DateAdd(Source.EMPTY, l("nanoseconds"), l(812398765), dateTime, zoneId).makePipe() + .asProcessor() + .process(null) + ) + ); + assertEquals( + "2019-09-04T04:10:36.311058024+10:00", + toString( + (ZonedDateTime) new DateAdd(Source.EMPTY, l("nanoseconds"), l(-812398765), dateTime, zoneId).makePipe() + .asProcessor() + .process(null) + ) + ); } private String toString(ZonedDateTime dateTime) { diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateDiffPipeTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateDiffPipeTests.java index 3760c8ec4288e..995e70a2920b6 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateDiffPipeTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateDiffPipeTests.java @@ -39,12 +39,12 @@ private Expression randomDateDiffPipeExpression() { public static DateDiffPipe randomDateDiffPipe() { return (DateDiffPipe) new DateDiff( - randomSource(), - randomStringLiteral(), - randomDatetimeLiteral(), - randomDatetimeLiteral(), - randomZone()) - .makePipe(); + randomSource(), + randomStringLiteral(), + randomDatetimeLiteral(), + randomDatetimeLiteral(), + randomZone() + ).makePipe(); } @Override @@ -54,26 +54,13 @@ public void testTransform() { DateDiffPipe b1 = randomInstance(); Expression newExpression = randomValueOtherThan(b1.expression(), this::randomDateDiffPipeExpression); - DateDiffPipe newB = new DateDiffPipe( - b1.source(), - newExpression, - b1.first(), - b1.second(), - b1.third(), - b1.zoneId()); + DateDiffPipe newB = new DateDiffPipe(b1.source(), newExpression, b1.first(), b1.second(), b1.third(), b1.zoneId()); assertEquals(newB, b1.transformPropertiesOnly(Expression.class, v -> Objects.equals(v, b1.expression()) ? newExpression : v)); DateDiffPipe b2 = randomInstance(); Source newLoc = randomValueOtherThan(b2.source(), SourceTests::randomSource); - newB = new DateDiffPipe( - newLoc, - b2.expression(), - b2.first(), - b2.second(), - b2.third(), - b2.zoneId()); - assertEquals(newB, - b2.transformPropertiesOnly(Source.class, v -> Objects.equals(v, b2.source()) ? newLoc : v)); + newB = new DateDiffPipe(newLoc, b2.expression(), b2.first(), b2.second(), b2.third(), b2.zoneId()); + assertEquals(newB, b2.transformPropertiesOnly(Source.class, v -> Objects.equals(v, b2.source()) ? newLoc : v)); } @Override @@ -117,26 +104,46 @@ public void testReplaceChildren() { @Override protected DateDiffPipe mutate(DateDiffPipe instance) { List> randoms = new ArrayList<>(); - randoms.add(f -> new DateDiffPipe(f.source(), f.expression(), + randoms.add( + f -> new DateDiffPipe( + f.source(), + f.expression(), pipe(((Expression) randomValueOtherThan(f.first(), FunctionTestUtils::randomStringLiteral))), f.second(), f.third(), - randomValueOtherThan(f.zoneId(), ESTestCase::randomZone))); - randoms.add(f -> new DateDiffPipe(f.source(), f.expression(), + randomValueOtherThan(f.zoneId(), ESTestCase::randomZone) + ) + ); + randoms.add( + f -> new DateDiffPipe( + f.source(), + f.expression(), f.first(), pipe(((Expression) randomValueOtherThan(f.second(), FunctionTestUtils::randomDatetimeLiteral))), f.third(), - randomValueOtherThan(f.zoneId(), ESTestCase::randomZone))); - randoms.add(f -> new DateDiffPipe(f.source(), f.expression(), + randomValueOtherThan(f.zoneId(), ESTestCase::randomZone) + ) + ); + randoms.add( + f -> new DateDiffPipe( + f.source(), + f.expression(), f.first(), f.second(), pipe(((Expression) randomValueOtherThan(f.third(), FunctionTestUtils::randomDatetimeLiteral))), - randomValueOtherThan(f.zoneId(), ESTestCase::randomZone))); - randoms.add(f -> new DateDiffPipe(f.source(), f.expression(), + randomValueOtherThan(f.zoneId(), ESTestCase::randomZone) + ) + ); + randoms.add( + f -> new DateDiffPipe( + f.source(), + f.expression(), pipe(((Expression) randomValueOtherThan(f.first(), FunctionTestUtils::randomStringLiteral))), pipe(((Expression) randomValueOtherThan(f.second(), FunctionTestUtils::randomDatetimeLiteral))), pipe(((Expression) randomValueOtherThan(f.third(), FunctionTestUtils::randomDatetimeLiteral))), - randomValueOtherThan(f.zoneId(), ESTestCase::randomZone))); + randomValueOtherThan(f.zoneId(), ESTestCase::randomZone) + ) + ); return randomFrom(randoms).apply(instance); } @@ -149,6 +156,7 @@ protected DateDiffPipe copy(DateDiffPipe instance) { instance.first(), instance.second(), instance.third(), - instance.zoneId()); + instance.zoneId() + ); } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateDiffProcessorTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateDiffProcessorTests.java index c5ddf8caf2bdb..c15d00250b237 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateDiffProcessorTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateDiffProcessorTests.java @@ -30,7 +30,8 @@ public static DateDiffProcessor randomDateDiffProcessor() { new ConstantProcessor(randomRealisticUnicodeOfLengthBetween(0, 128)), new ConstantProcessor(DateTimeTestUtils.nowWithMillisResolution()), new ConstantProcessor(DateTimeTestUtils.nowWithMillisResolution()), - randomZone()); + randomZone() + ); } @Override @@ -54,48 +55,69 @@ protected DateDiffProcessor mutateInstance(DateDiffProcessor instance) { new ConstantProcessor(ESTestCase.randomRealisticUnicodeOfLength(128)), new ConstantProcessor(DateTimeTestUtils.nowWithMillisResolution()), new ConstantProcessor(DateTimeTestUtils.nowWithMillisResolution()), - randomValueOtherThan(instance.zoneId(), ESTestCase::randomZone)); + randomValueOtherThan(instance.zoneId(), ESTestCase::randomZone) + ); } public void testInvalidInputs() { - SqlIllegalArgumentException siae = expectThrows(SqlIllegalArgumentException.class, - () -> new DateDiff(Source.EMPTY, l(5), - randomDatetimeLiteral(), randomDatetimeLiteral(), randomZone()).makePipe().asProcessor().process(null)); + SqlIllegalArgumentException siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new DateDiff(Source.EMPTY, l(5), randomDatetimeLiteral(), randomDatetimeLiteral(), randomZone()).makePipe() + .asProcessor() + .process(null) + ); assertEquals("A string is required; received [5]", siae.getMessage()); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new DateDiff(Source.EMPTY, - l("days"), l("foo"), randomDatetimeLiteral(), randomZone()).makePipe().asProcessor().process(null)); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new DateDiff(Source.EMPTY, l("days"), l("foo"), randomDatetimeLiteral(), randomZone()).makePipe() + .asProcessor() + .process(null) + ); assertEquals("A date/datetime is required; received [foo]", siae.getMessage()); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new DateDiff(Source.EMPTY, - l("days"), randomDatetimeLiteral(), l("foo"), randomZone()).makePipe().asProcessor().process(null)); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new DateDiff(Source.EMPTY, l("days"), randomDatetimeLiteral(), l("foo"), randomZone()).makePipe() + .asProcessor() + .process(null) + ); assertEquals("A date/datetime is required; received [foo]", siae.getMessage()); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new DateDiff(Source.EMPTY, l("invalid"), - randomDatetimeLiteral(), randomDatetimeLiteral(), randomZone()).makePipe().asProcessor().process(null)); - assertEquals("A value of [YEAR, QUARTER, MONTH, DAYOFYEAR, DAY, WEEK, WEEKDAY, HOUR, MINUTE, " + - "SECOND, MILLISECOND, MICROSECOND, NANOSECOND] or their aliases is required; received [invalid]", - siae.getMessage()); - - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new DateDiff(Source.EMPTY, l("quertar"), - randomDatetimeLiteral(), randomDatetimeLiteral(), randomZone()).makePipe().asProcessor().process(null)); - assertEquals("Received value [quertar] is not valid date part to add; did you mean [quarter, quarters]?", - siae.getMessage()); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new DateDiff(Source.EMPTY, l("invalid"), randomDatetimeLiteral(), randomDatetimeLiteral(), randomZone()).makePipe() + .asProcessor() + .process(null) + ); + assertEquals( + "A value of [YEAR, QUARTER, MONTH, DAYOFYEAR, DAY, WEEK, WEEKDAY, HOUR, MINUTE, " + + "SECOND, MILLISECOND, MICROSECOND, NANOSECOND] or their aliases is required; received [invalid]", + siae.getMessage() + ); + + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new DateDiff(Source.EMPTY, l("quertar"), randomDatetimeLiteral(), randomDatetimeLiteral(), randomZone()).makePipe() + .asProcessor() + .process(null) + ); + assertEquals("Received value [quertar] is not valid date part to add; did you mean [quarter, quarters]?", siae.getMessage()); } public void testWithNulls() { - assertNull(new DateDiff(Source.EMPTY, - NULL, randomDatetimeLiteral(), randomDatetimeLiteral(), randomZone()).makePipe().asProcessor().process(null)); - assertNull(new DateDiff(Source.EMPTY, - l("days"), NULL, randomDatetimeLiteral(), randomZone()).makePipe().asProcessor().process(null)); - assertNull(new DateDiff(Source.EMPTY, - l("days"), randomDatetimeLiteral(), NULL, randomZone()).makePipe().asProcessor().process(null)); - assertNull(new DateDiff(Source.EMPTY, - NULL, NULL, NULL, randomZone()).makePipe().asProcessor().process(null)); + assertNull( + new DateDiff(Source.EMPTY, NULL, randomDatetimeLiteral(), randomDatetimeLiteral(), randomZone()).makePipe() + .asProcessor() + .process(null) + ); + assertNull( + new DateDiff(Source.EMPTY, l("days"), NULL, randomDatetimeLiteral(), randomZone()).makePipe().asProcessor().process(null) + ); + assertNull( + new DateDiff(Source.EMPTY, l("days"), randomDatetimeLiteral(), NULL, randomZone()).makePipe().asProcessor().process(null) + ); + assertNull(new DateDiff(Source.EMPTY, NULL, NULL, NULL, randomZone()).makePipe().asProcessor().process(null)); } public void testDiff() { @@ -104,131 +126,79 @@ public void testDiff() { Literal dt1 = l(dateTime(2019, 12, 31, 20, 22, 33, 987654321, ZoneId.of("Etc/GMT+5"))); Literal dt2 = l(dateTime(2022, 1, 1, 4, 33, 22, 123456789, ZoneId.of("Etc/GMT-5"))); - assertEquals(1, new DateDiff(Source.EMPTY, l("years"), dt1, dt2, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(-1, new DateDiff(Source.EMPTY, l("year"), dt2, dt1, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(2, new DateDiff(Source.EMPTY, l("yyyy"), dt1, dt2, zoneId) - .makePipe().asProcessor().process(null)); - assertEquals(-2, new DateDiff(Source.EMPTY, l("yy"), dt2, dt1, zoneId) - .makePipe().asProcessor().process(null)); - - assertEquals(7, new DateDiff(Source.EMPTY, l("quarter"), dt1, dt2, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(-7, new DateDiff(Source.EMPTY, l("qq"), dt2, dt1, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(8, new DateDiff(Source.EMPTY, l("quarter"), dt1, dt2, zoneId) - .makePipe().asProcessor().process(null)); - assertEquals(-8, new DateDiff(Source.EMPTY, l("qq"), dt2, dt1, zoneId) - .makePipe().asProcessor().process(null)); - - assertEquals(23, new DateDiff(Source.EMPTY, l("month"), dt1, dt2, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(-23, new DateDiff(Source.EMPTY, l("months"), dt2, dt1, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(24, new DateDiff(Source.EMPTY, l("mm"), dt1, dt2, zoneId) - .makePipe().asProcessor().process(null)); - assertEquals(-24, new DateDiff(Source.EMPTY, l("m"), dt2, dt1, zoneId) - .makePipe().asProcessor().process(null)); - - assertEquals(730, new DateDiff(Source.EMPTY, l("dayofyear"), dt1, dt2, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(-730, new DateDiff(Source.EMPTY, l("dy"), dt2, dt1, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(730, new DateDiff(Source.EMPTY, l("y"), dt1, dt2, zoneId) - .makePipe().asProcessor().process(null)); - assertEquals(-730, new DateDiff(Source.EMPTY, l("y"), dt2, dt1, zoneId) - .makePipe().asProcessor().process(null)); - - assertEquals(730, new DateDiff(Source.EMPTY, l("day"), dt1, dt2, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(-730, new DateDiff(Source.EMPTY, l("days"), dt2, dt1, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(730, new DateDiff(Source.EMPTY, l("dd"), dt1, dt2, zoneId) - .makePipe().asProcessor().process(null)); - assertEquals(-730, new DateDiff(Source.EMPTY, l("dd"), dt2, dt1, zoneId) - .makePipe().asProcessor().process(null)); - - assertEquals(104, new DateDiff(Source.EMPTY, l("week"), dt1, dt2, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(-104, new DateDiff(Source.EMPTY, l("weeks"), dt2, dt1, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(104, new DateDiff(Source.EMPTY, l("wk"), dt1, dt2, zoneId) - .makePipe().asProcessor().process(null)); - assertEquals(-104, new DateDiff(Source.EMPTY, l("ww"), dt2, dt1, zoneId) - .makePipe().asProcessor().process(null)); - - assertEquals(730, new DateDiff(Source.EMPTY, l("weekday"), dt1, dt2, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(-730, new DateDiff(Source.EMPTY, l("weekdays"), dt2, dt1, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(730, new DateDiff(Source.EMPTY, l("dw"), dt1, dt2, zoneId) - .makePipe().asProcessor().process(null)); - assertEquals(-730, new DateDiff(Source.EMPTY, l("dw"), dt2, dt1, zoneId) - .makePipe().asProcessor().process(null)); - - assertEquals(17542, new DateDiff(Source.EMPTY, l("hour"), dt1, dt2, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(-17542, new DateDiff(Source.EMPTY, l("hours"), dt2, dt1, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(17542, new DateDiff(Source.EMPTY, l("hh"), dt1, dt2, zoneId) - .makePipe().asProcessor().process(null)); - assertEquals(-17542, new DateDiff(Source.EMPTY, l("hh"), dt2, dt1, zoneId) - .makePipe().asProcessor().process(null)); - - assertEquals(1052531, new DateDiff(Source.EMPTY, l("minute"), dt1, dt2, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(-1052531, new DateDiff(Source.EMPTY, l("minutes"), dt2, dt1, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(1052531, new DateDiff(Source.EMPTY, l("mi"), dt1, dt2, zoneId) - .makePipe().asProcessor().process(null)); - assertEquals(-1052531, new DateDiff(Source.EMPTY, l("n"), dt2, dt1, zoneId) - .makePipe().asProcessor().process(null)); + assertEquals(1, new DateDiff(Source.EMPTY, l("years"), dt1, dt2, UTC).makePipe().asProcessor().process(null)); + assertEquals(-1, new DateDiff(Source.EMPTY, l("year"), dt2, dt1, UTC).makePipe().asProcessor().process(null)); + assertEquals(2, new DateDiff(Source.EMPTY, l("yyyy"), dt1, dt2, zoneId).makePipe().asProcessor().process(null)); + assertEquals(-2, new DateDiff(Source.EMPTY, l("yy"), dt2, dt1, zoneId).makePipe().asProcessor().process(null)); + + assertEquals(7, new DateDiff(Source.EMPTY, l("quarter"), dt1, dt2, UTC).makePipe().asProcessor().process(null)); + assertEquals(-7, new DateDiff(Source.EMPTY, l("qq"), dt2, dt1, UTC).makePipe().asProcessor().process(null)); + assertEquals(8, new DateDiff(Source.EMPTY, l("quarter"), dt1, dt2, zoneId).makePipe().asProcessor().process(null)); + assertEquals(-8, new DateDiff(Source.EMPTY, l("qq"), dt2, dt1, zoneId).makePipe().asProcessor().process(null)); + + assertEquals(23, new DateDiff(Source.EMPTY, l("month"), dt1, dt2, UTC).makePipe().asProcessor().process(null)); + assertEquals(-23, new DateDiff(Source.EMPTY, l("months"), dt2, dt1, UTC).makePipe().asProcessor().process(null)); + assertEquals(24, new DateDiff(Source.EMPTY, l("mm"), dt1, dt2, zoneId).makePipe().asProcessor().process(null)); + assertEquals(-24, new DateDiff(Source.EMPTY, l("m"), dt2, dt1, zoneId).makePipe().asProcessor().process(null)); + + assertEquals(730, new DateDiff(Source.EMPTY, l("dayofyear"), dt1, dt2, UTC).makePipe().asProcessor().process(null)); + assertEquals(-730, new DateDiff(Source.EMPTY, l("dy"), dt2, dt1, UTC).makePipe().asProcessor().process(null)); + assertEquals(730, new DateDiff(Source.EMPTY, l("y"), dt1, dt2, zoneId).makePipe().asProcessor().process(null)); + assertEquals(-730, new DateDiff(Source.EMPTY, l("y"), dt2, dt1, zoneId).makePipe().asProcessor().process(null)); + + assertEquals(730, new DateDiff(Source.EMPTY, l("day"), dt1, dt2, UTC).makePipe().asProcessor().process(null)); + assertEquals(-730, new DateDiff(Source.EMPTY, l("days"), dt2, dt1, UTC).makePipe().asProcessor().process(null)); + assertEquals(730, new DateDiff(Source.EMPTY, l("dd"), dt1, dt2, zoneId).makePipe().asProcessor().process(null)); + assertEquals(-730, new DateDiff(Source.EMPTY, l("dd"), dt2, dt1, zoneId).makePipe().asProcessor().process(null)); + + assertEquals(104, new DateDiff(Source.EMPTY, l("week"), dt1, dt2, UTC).makePipe().asProcessor().process(null)); + assertEquals(-104, new DateDiff(Source.EMPTY, l("weeks"), dt2, dt1, UTC).makePipe().asProcessor().process(null)); + assertEquals(104, new DateDiff(Source.EMPTY, l("wk"), dt1, dt2, zoneId).makePipe().asProcessor().process(null)); + assertEquals(-104, new DateDiff(Source.EMPTY, l("ww"), dt2, dt1, zoneId).makePipe().asProcessor().process(null)); + + assertEquals(730, new DateDiff(Source.EMPTY, l("weekday"), dt1, dt2, UTC).makePipe().asProcessor().process(null)); + assertEquals(-730, new DateDiff(Source.EMPTY, l("weekdays"), dt2, dt1, UTC).makePipe().asProcessor().process(null)); + assertEquals(730, new DateDiff(Source.EMPTY, l("dw"), dt1, dt2, zoneId).makePipe().asProcessor().process(null)); + assertEquals(-730, new DateDiff(Source.EMPTY, l("dw"), dt2, dt1, zoneId).makePipe().asProcessor().process(null)); + + assertEquals(17542, new DateDiff(Source.EMPTY, l("hour"), dt1, dt2, UTC).makePipe().asProcessor().process(null)); + assertEquals(-17542, new DateDiff(Source.EMPTY, l("hours"), dt2, dt1, UTC).makePipe().asProcessor().process(null)); + assertEquals(17542, new DateDiff(Source.EMPTY, l("hh"), dt1, dt2, zoneId).makePipe().asProcessor().process(null)); + assertEquals(-17542, new DateDiff(Source.EMPTY, l("hh"), dt2, dt1, zoneId).makePipe().asProcessor().process(null)); + + assertEquals(1052531, new DateDiff(Source.EMPTY, l("minute"), dt1, dt2, UTC).makePipe().asProcessor().process(null)); + assertEquals(-1052531, new DateDiff(Source.EMPTY, l("minutes"), dt2, dt1, UTC).makePipe().asProcessor().process(null)); + assertEquals(1052531, new DateDiff(Source.EMPTY, l("mi"), dt1, dt2, zoneId).makePipe().asProcessor().process(null)); + assertEquals(-1052531, new DateDiff(Source.EMPTY, l("n"), dt2, dt1, zoneId).makePipe().asProcessor().process(null)); dt1 = l(dateTime(2020, 12, 31, 20, 22, 33, 123456789, ZoneId.of("Etc/GMT+5"))); dt2 = l(dateTime(2021, 1, 1, 10, 33, 22, 987654321, ZoneId.of("Etc/GMT-5"))); - assertEquals(15049, new DateDiff(Source.EMPTY, l("second"), dt1, dt2, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(-15049, new DateDiff(Source.EMPTY, l("seconds"), dt2, dt1, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(15049, new DateDiff(Source.EMPTY, l("ss"), dt1, dt2, zoneId) - .makePipe().asProcessor().process(null)); - assertEquals(-15049, new DateDiff(Source.EMPTY, l("s"), dt2, dt1, zoneId) - .makePipe().asProcessor().process(null)); - - assertEquals(15049864, new DateDiff(Source.EMPTY, l("millisecond"), dt1, dt2, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(-15049864, new DateDiff(Source.EMPTY, l("milliseconds"), dt2, dt1, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(15049864, new DateDiff(Source.EMPTY, l("ms"), dt1, dt2, zoneId) - .makePipe().asProcessor().process(null)); - assertEquals(-15049864, new DateDiff(Source.EMPTY, l("ms"), dt2, dt1, zoneId) - .makePipe().asProcessor().process(null)); + assertEquals(15049, new DateDiff(Source.EMPTY, l("second"), dt1, dt2, UTC).makePipe().asProcessor().process(null)); + assertEquals(-15049, new DateDiff(Source.EMPTY, l("seconds"), dt2, dt1, UTC).makePipe().asProcessor().process(null)); + assertEquals(15049, new DateDiff(Source.EMPTY, l("ss"), dt1, dt2, zoneId).makePipe().asProcessor().process(null)); + assertEquals(-15049, new DateDiff(Source.EMPTY, l("s"), dt2, dt1, zoneId).makePipe().asProcessor().process(null)); + + assertEquals(15049864, new DateDiff(Source.EMPTY, l("millisecond"), dt1, dt2, UTC).makePipe().asProcessor().process(null)); + assertEquals(-15049864, new DateDiff(Source.EMPTY, l("milliseconds"), dt2, dt1, UTC).makePipe().asProcessor().process(null)); + assertEquals(15049864, new DateDiff(Source.EMPTY, l("ms"), dt1, dt2, zoneId).makePipe().asProcessor().process(null)); + assertEquals(-15049864, new DateDiff(Source.EMPTY, l("ms"), dt2, dt1, zoneId).makePipe().asProcessor().process(null)); dt1 = l(dateTime(2020, 12, 31, 20, 22, 33, 123456789, ZoneId.of("Etc/GMT+5"))); dt2 = l(dateTime(2021, 1, 1, 6, 33, 22, 987654321, ZoneId.of("Etc/GMT-5"))); - assertEquals(649864198, new DateDiff(Source.EMPTY, l("microsecond"), dt1, dt2, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(-649864198, new DateDiff(Source.EMPTY, l("microseconds"), dt2, dt1, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(649864198, new DateDiff(Source.EMPTY, l("mcs"), dt1, dt2, zoneId) - .makePipe().asProcessor().process(null)); - assertEquals(-649864198, new DateDiff(Source.EMPTY, l("mcs"), dt2, dt1, zoneId) - .makePipe().asProcessor().process(null)); + assertEquals(649864198, new DateDiff(Source.EMPTY, l("microsecond"), dt1, dt2, UTC).makePipe().asProcessor().process(null)); + assertEquals(-649864198, new DateDiff(Source.EMPTY, l("microseconds"), dt2, dt1, UTC).makePipe().asProcessor().process(null)); + assertEquals(649864198, new DateDiff(Source.EMPTY, l("mcs"), dt1, dt2, zoneId).makePipe().asProcessor().process(null)); + assertEquals(-649864198, new DateDiff(Source.EMPTY, l("mcs"), dt2, dt1, zoneId).makePipe().asProcessor().process(null)); dt1 = l(dateTime(2020, 12, 31, 20, 33, 22, 123456789, ZoneId.of("Etc/GMT+5"))); dt2 = l(dateTime(2021, 1, 1, 6, 33, 23, 987654321, ZoneId.of("Etc/GMT-5"))); - assertEquals(1864197532, new DateDiff(Source.EMPTY, l("nanosecond"), dt1, dt2, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(-1864197532, new DateDiff(Source.EMPTY, l("nanoseconds"), dt2, dt1, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(1864197532, new DateDiff(Source.EMPTY, l("ns"), dt1, dt2, zoneId) - .makePipe().asProcessor().process(null)); - assertEquals(-1864197532, new DateDiff(Source.EMPTY, l("ns"), dt2, dt1, zoneId) - .makePipe().asProcessor().process(null)); + assertEquals(1864197532, new DateDiff(Source.EMPTY, l("nanosecond"), dt1, dt2, UTC).makePipe().asProcessor().process(null)); + assertEquals(-1864197532, new DateDiff(Source.EMPTY, l("nanoseconds"), dt2, dt1, UTC).makePipe().asProcessor().process(null)); + assertEquals(1864197532, new DateDiff(Source.EMPTY, l("ns"), dt1, dt2, zoneId).makePipe().asProcessor().process(null)); + assertEquals(-1864197532, new DateDiff(Source.EMPTY, l("ns"), dt2, dt1, zoneId).makePipe().asProcessor().process(null)); } public void testDiffEdgeCases() { @@ -237,153 +207,97 @@ public void testDiffEdgeCases() { Literal dt1 = l(dateTime(2010, 12, 31, 18, 0, 0, 0)); Literal dt2 = l(dateTime(2019, 1, 1, 18, 0, 0, 0)); - assertEquals(9, new DateDiff(Source.EMPTY, l("years"), dt1, dt2, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(-9, new DateDiff(Source.EMPTY, l("year"), dt2, dt1, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(8, new DateDiff(Source.EMPTY, l("yyyy"), dt1, dt2, zoneId) - .makePipe().asProcessor().process(null)); - assertEquals(-8, new DateDiff(Source.EMPTY, l("yy"), dt2, dt1, zoneId) - .makePipe().asProcessor().process(null)); - - assertEquals(33, new DateDiff(Source.EMPTY, l("quarter"), dt1, dt2, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(-33, new DateDiff(Source.EMPTY, l("qq"), dt2, dt1, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(32, new DateDiff(Source.EMPTY, l("quarter"), dt1, dt2, zoneId) - .makePipe().asProcessor().process(null)); - assertEquals(-32, new DateDiff(Source.EMPTY, l("qq"), dt2, dt1, zoneId) - .makePipe().asProcessor().process(null)); - - assertEquals(97, new DateDiff(Source.EMPTY, l("month"), dt1, dt2, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(-97, new DateDiff(Source.EMPTY, l("months"), dt2, dt1, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(96, new DateDiff(Source.EMPTY, l("mm"), dt1, dt2, zoneId) - .makePipe().asProcessor().process(null)); - assertEquals(-96, new DateDiff(Source.EMPTY, l("m"), dt2, dt1, zoneId) - .makePipe().asProcessor().process(null)); + assertEquals(9, new DateDiff(Source.EMPTY, l("years"), dt1, dt2, UTC).makePipe().asProcessor().process(null)); + assertEquals(-9, new DateDiff(Source.EMPTY, l("year"), dt2, dt1, UTC).makePipe().asProcessor().process(null)); + assertEquals(8, new DateDiff(Source.EMPTY, l("yyyy"), dt1, dt2, zoneId).makePipe().asProcessor().process(null)); + assertEquals(-8, new DateDiff(Source.EMPTY, l("yy"), dt2, dt1, zoneId).makePipe().asProcessor().process(null)); + + assertEquals(33, new DateDiff(Source.EMPTY, l("quarter"), dt1, dt2, UTC).makePipe().asProcessor().process(null)); + assertEquals(-33, new DateDiff(Source.EMPTY, l("qq"), dt2, dt1, UTC).makePipe().asProcessor().process(null)); + assertEquals(32, new DateDiff(Source.EMPTY, l("quarter"), dt1, dt2, zoneId).makePipe().asProcessor().process(null)); + assertEquals(-32, new DateDiff(Source.EMPTY, l("qq"), dt2, dt1, zoneId).makePipe().asProcessor().process(null)); + + assertEquals(97, new DateDiff(Source.EMPTY, l("month"), dt1, dt2, UTC).makePipe().asProcessor().process(null)); + assertEquals(-97, new DateDiff(Source.EMPTY, l("months"), dt2, dt1, UTC).makePipe().asProcessor().process(null)); + assertEquals(96, new DateDiff(Source.EMPTY, l("mm"), dt1, dt2, zoneId).makePipe().asProcessor().process(null)); + assertEquals(-96, new DateDiff(Source.EMPTY, l("m"), dt2, dt1, zoneId).makePipe().asProcessor().process(null)); dt1 = l(dateTime(1976, 9, 9, 0, 0, 0, 0)); dt2 = l(dateTime(1983, 5, 22, 0, 0, 0, 0)); - assertEquals(350, new DateDiff(Source.EMPTY, l("week"), dt1, dt2, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(-350, new DateDiff(Source.EMPTY, l("weeks"), dt2, dt1, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(350, new DateDiff(Source.EMPTY, l("wk"), dt1, dt2, zoneId) - .makePipe().asProcessor().process(null)); - assertEquals(-350, new DateDiff(Source.EMPTY, l("ww"), dt2, dt1, zoneId) - .makePipe().asProcessor().process(null)); + assertEquals(350, new DateDiff(Source.EMPTY, l("week"), dt1, dt2, UTC).makePipe().asProcessor().process(null)); + assertEquals(-350, new DateDiff(Source.EMPTY, l("weeks"), dt2, dt1, UTC).makePipe().asProcessor().process(null)); + assertEquals(350, new DateDiff(Source.EMPTY, l("wk"), dt1, dt2, zoneId).makePipe().asProcessor().process(null)); + assertEquals(-350, new DateDiff(Source.EMPTY, l("ww"), dt2, dt1, zoneId).makePipe().asProcessor().process(null)); dt1 = l(dateTime(1988, 1, 2, 0, 0, 0, 0)); dt2 = l(dateTime(1987, 12, 29, 0, 0, 0, 0)); - assertEquals(0, new DateDiff(Source.EMPTY, l("week"), dt1, dt2, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(0, new DateDiff(Source.EMPTY, l("weeks"), dt2, dt1, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(0, new DateDiff(Source.EMPTY, l("wk"), dt1, dt2, zoneId) - .makePipe().asProcessor().process(null)); - assertEquals(0, new DateDiff(Source.EMPTY, l("ww"), dt2, dt1, zoneId) - .makePipe().asProcessor().process(null)); + assertEquals(0, new DateDiff(Source.EMPTY, l("week"), dt1, dt2, UTC).makePipe().asProcessor().process(null)); + assertEquals(0, new DateDiff(Source.EMPTY, l("weeks"), dt2, dt1, UTC).makePipe().asProcessor().process(null)); + assertEquals(0, new DateDiff(Source.EMPTY, l("wk"), dt1, dt2, zoneId).makePipe().asProcessor().process(null)); + assertEquals(0, new DateDiff(Source.EMPTY, l("ww"), dt2, dt1, zoneId).makePipe().asProcessor().process(null)); dt1 = l(dateTime(1988, 1, 5, 0, 0, 0, 0)); dt2 = l(dateTime(1996, 5, 13, 0, 0, 0, 0)); - assertEquals(436, new DateDiff(Source.EMPTY, l("week"), dt1, dt2, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(-436, new DateDiff(Source.EMPTY, l("weeks"), dt2, dt1, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(436, new DateDiff(Source.EMPTY, l("wk"), dt1, dt2, zoneId) - .makePipe().asProcessor().process(null)); - assertEquals(-436, new DateDiff(Source.EMPTY, l("ww"), dt2, dt1, zoneId) - .makePipe().asProcessor().process(null)); + assertEquals(436, new DateDiff(Source.EMPTY, l("week"), dt1, dt2, UTC).makePipe().asProcessor().process(null)); + assertEquals(-436, new DateDiff(Source.EMPTY, l("weeks"), dt2, dt1, UTC).makePipe().asProcessor().process(null)); + assertEquals(436, new DateDiff(Source.EMPTY, l("wk"), dt1, dt2, zoneId).makePipe().asProcessor().process(null)); + assertEquals(-436, new DateDiff(Source.EMPTY, l("ww"), dt2, dt1, zoneId).makePipe().asProcessor().process(null)); dt1 = l(dateTime(1999, 8, 20, 0, 0, 0, 0)); dt2 = l(dateTime(1974, 3, 17, 0, 0, 0, 0)); - assertEquals(-1326, new DateDiff(Source.EMPTY, l("week"), dt1, dt2, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(1326, new DateDiff(Source.EMPTY, l("weeks"), dt2, dt1, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(-1326, new DateDiff(Source.EMPTY, l("wk"), dt1, dt2, zoneId) - .makePipe().asProcessor().process(null)); - assertEquals(1326, new DateDiff(Source.EMPTY, l("ww"), dt2, dt1, zoneId) - .makePipe().asProcessor().process(null)); + assertEquals(-1326, new DateDiff(Source.EMPTY, l("week"), dt1, dt2, UTC).makePipe().asProcessor().process(null)); + assertEquals(1326, new DateDiff(Source.EMPTY, l("weeks"), dt2, dt1, UTC).makePipe().asProcessor().process(null)); + assertEquals(-1326, new DateDiff(Source.EMPTY, l("wk"), dt1, dt2, zoneId).makePipe().asProcessor().process(null)); + assertEquals(1326, new DateDiff(Source.EMPTY, l("ww"), dt2, dt1, zoneId).makePipe().asProcessor().process(null)); dt1 = l(dateTime(1997, 2, 2, 0, 0, 0, 0)); dt2 = l(dateTime(1997, 9, 19, 0, 0, 0, 0)); - assertEquals(32, new DateDiff(Source.EMPTY, l("week"), dt1, dt2, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(-32, new DateDiff(Source.EMPTY, l("weeks"), dt2, dt1, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(32, new DateDiff(Source.EMPTY, l("wk"), dt1, dt2, zoneId) - .makePipe().asProcessor().process(null)); - assertEquals(-32, new DateDiff(Source.EMPTY, l("ww"), dt2, dt1, zoneId) - .makePipe().asProcessor().process(null)); + assertEquals(32, new DateDiff(Source.EMPTY, l("week"), dt1, dt2, UTC).makePipe().asProcessor().process(null)); + assertEquals(-32, new DateDiff(Source.EMPTY, l("weeks"), dt2, dt1, UTC).makePipe().asProcessor().process(null)); + assertEquals(32, new DateDiff(Source.EMPTY, l("wk"), dt1, dt2, zoneId).makePipe().asProcessor().process(null)); + assertEquals(-32, new DateDiff(Source.EMPTY, l("ww"), dt2, dt1, zoneId).makePipe().asProcessor().process(null)); dt1 = l(dateTime(1980, 11, 7, 0, 0, 0, 0)); dt2 = l(dateTime(1979, 4, 1, 0, 0, 0, 0)); - assertEquals(-83, new DateDiff(Source.EMPTY, l("week"), dt1, dt2, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(83, new DateDiff(Source.EMPTY, l("weeks"), dt2, dt1, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(-83, new DateDiff(Source.EMPTY, l("wk"), dt1, dt2, zoneId) - .makePipe().asProcessor().process(null)); - assertEquals(83, new DateDiff(Source.EMPTY, l("ww"), dt2, dt1, zoneId) - .makePipe().asProcessor().process(null)); + assertEquals(-83, new DateDiff(Source.EMPTY, l("week"), dt1, dt2, UTC).makePipe().asProcessor().process(null)); + assertEquals(83, new DateDiff(Source.EMPTY, l("weeks"), dt2, dt1, UTC).makePipe().asProcessor().process(null)); + assertEquals(-83, new DateDiff(Source.EMPTY, l("wk"), dt1, dt2, zoneId).makePipe().asProcessor().process(null)); + assertEquals(83, new DateDiff(Source.EMPTY, l("ww"), dt2, dt1, zoneId).makePipe().asProcessor().process(null)); dt1 = l(dateTime(1997, 9, 19, 0, 0, 0, 0)); dt2 = l(dateTime(2004, 8, 2, 7, 59, 23, 0)); - assertEquals(60223, new DateDiff(Source.EMPTY, l("hour"), dt1, dt2, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(-60223, new DateDiff(Source.EMPTY, l("hours"), dt2, dt1, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(60223, new DateDiff(Source.EMPTY, l("hh"), dt1, dt2, zoneId) - .makePipe().asProcessor().process(null)); - assertEquals(-60223, new DateDiff(Source.EMPTY, l("hh"), dt2, dt1, zoneId) - .makePipe().asProcessor().process(null)); + assertEquals(60223, new DateDiff(Source.EMPTY, l("hour"), dt1, dt2, UTC).makePipe().asProcessor().process(null)); + assertEquals(-60223, new DateDiff(Source.EMPTY, l("hours"), dt2, dt1, UTC).makePipe().asProcessor().process(null)); + assertEquals(60223, new DateDiff(Source.EMPTY, l("hh"), dt1, dt2, zoneId).makePipe().asProcessor().process(null)); + assertEquals(-60223, new DateDiff(Source.EMPTY, l("hh"), dt2, dt1, zoneId).makePipe().asProcessor().process(null)); dt1 = l(dateTime(1997, 9, 19, 0, 0, 0, 0)); dt2 = l(dateTime(2004, 8, 2, 7, 59, 59, 999999999)); - assertEquals(60223, new DateDiff(Source.EMPTY, l("hour"), dt1, dt2, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(-60223, new DateDiff(Source.EMPTY, l("hours"), dt2, dt1, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(60223, new DateDiff(Source.EMPTY, l("hh"), dt1, dt2, zoneId) - .makePipe().asProcessor().process(null)); - assertEquals(-60223, new DateDiff(Source.EMPTY, l("hh"), dt2, dt1, zoneId) - .makePipe().asProcessor().process(null)); + assertEquals(60223, new DateDiff(Source.EMPTY, l("hour"), dt1, dt2, UTC).makePipe().asProcessor().process(null)); + assertEquals(-60223, new DateDiff(Source.EMPTY, l("hours"), dt2, dt1, UTC).makePipe().asProcessor().process(null)); + assertEquals(60223, new DateDiff(Source.EMPTY, l("hh"), dt1, dt2, zoneId).makePipe().asProcessor().process(null)); + assertEquals(-60223, new DateDiff(Source.EMPTY, l("hh"), dt2, dt1, zoneId).makePipe().asProcessor().process(null)); dt1 = l(dateTime(2002, 4, 27, 0, 0, 0, 0)); dt2 = l(dateTime(2004, 7, 28, 12, 34, 28, 0)); - assertEquals(1185874, new DateDiff(Source.EMPTY, l("minute"), dt1, dt2, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(-1185874, new DateDiff(Source.EMPTY, l("minutes"), dt2, dt1, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(1185874, new DateDiff(Source.EMPTY, l("mi"), dt1, dt2, zoneId) - .makePipe().asProcessor().process(null)); - assertEquals(-1185874, new DateDiff(Source.EMPTY, l("n"), dt2, dt1, zoneId) - .makePipe().asProcessor().process(null)); + assertEquals(1185874, new DateDiff(Source.EMPTY, l("minute"), dt1, dt2, UTC).makePipe().asProcessor().process(null)); + assertEquals(-1185874, new DateDiff(Source.EMPTY, l("minutes"), dt2, dt1, UTC).makePipe().asProcessor().process(null)); + assertEquals(1185874, new DateDiff(Source.EMPTY, l("mi"), dt1, dt2, zoneId).makePipe().asProcessor().process(null)); + assertEquals(-1185874, new DateDiff(Source.EMPTY, l("n"), dt2, dt1, zoneId).makePipe().asProcessor().process(null)); dt1 = l(dateTime(1995, 9, 3, 0, 0, 0, 0)); dt2 = l(dateTime(2004, 7, 26, 12, 30, 34, 0)); - assertEquals(4679310, new DateDiff(Source.EMPTY, l("minute"), dt1, dt2, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(-4679310, new DateDiff(Source.EMPTY, l("minutes"), dt2, dt1, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(4679310, new DateDiff(Source.EMPTY, l("mi"), dt1, dt2, zoneId) - .makePipe().asProcessor().process(null)); - assertEquals(-4679310, new DateDiff(Source.EMPTY, l("n"), dt2, dt1, zoneId) - .makePipe().asProcessor().process(null)); + assertEquals(4679310, new DateDiff(Source.EMPTY, l("minute"), dt1, dt2, UTC).makePipe().asProcessor().process(null)); + assertEquals(-4679310, new DateDiff(Source.EMPTY, l("minutes"), dt2, dt1, UTC).makePipe().asProcessor().process(null)); + assertEquals(4679310, new DateDiff(Source.EMPTY, l("mi"), dt1, dt2, zoneId).makePipe().asProcessor().process(null)); + assertEquals(-4679310, new DateDiff(Source.EMPTY, l("n"), dt2, dt1, zoneId).makePipe().asProcessor().process(null)); dt1 = l(dateTime(1997, 5, 30, 0, 0, 0, 0)); dt2 = l(dateTime(2004, 7, 28, 23, 30, 59, 999999999)); - assertEquals(3768450, new DateDiff(Source.EMPTY, l("minute"), dt1, dt2, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(-3768450, new DateDiff(Source.EMPTY, l("minutes"), dt2, dt1, UTC) - .makePipe().asProcessor().process(null)); - assertEquals(3768450, new DateDiff(Source.EMPTY, l("mi"), dt1, dt2, zoneId) - .makePipe().asProcessor().process(null)); - assertEquals(-3768450, new DateDiff(Source.EMPTY, l("n"), dt2, dt1, zoneId) - .makePipe().asProcessor().process(null)); + assertEquals(3768450, new DateDiff(Source.EMPTY, l("minute"), dt1, dt2, UTC).makePipe().asProcessor().process(null)); + assertEquals(-3768450, new DateDiff(Source.EMPTY, l("minutes"), dt2, dt1, UTC).makePipe().asProcessor().process(null)); + assertEquals(3768450, new DateDiff(Source.EMPTY, l("mi"), dt1, dt2, zoneId).makePipe().asProcessor().process(null)); + assertEquals(-3768450, new DateDiff(Source.EMPTY, l("n"), dt2, dt1, zoneId).makePipe().asProcessor().process(null)); } public void testOverflow() { @@ -391,70 +305,114 @@ public void testOverflow() { Literal dt1 = l(dateTime(-99992022, 12, 31, 20, 22, 33, 123456789, ZoneId.of("Etc/GMT-5"))); Literal dt2 = l(dateTime(99992022, 4, 18, 8, 33, 22, 987654321, ZoneId.of("Etc/GMT+5"))); - SqlIllegalArgumentException siae = expectThrows(SqlIllegalArgumentException.class, - () -> new DateDiff(Source.EMPTY, l("month"), dt1, dt2, zoneId).makePipe().asProcessor().process(null)); - assertEquals("The DATE_DIFF function resulted in an overflow; the number of units separating two date/datetime " + - "instances is too large. Try to use DATE_DIFF with a less precise unit.", - siae.getMessage()); - - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new DateDiff(Source.EMPTY, l("dayofyear"), dt1, dt2, zoneId).makePipe().asProcessor().process(null)); - assertEquals("The DATE_DIFF function resulted in an overflow; the number of units separating two date/datetime " + - "instances is too large. Try to use DATE_DIFF with a less precise unit.", - siae.getMessage()); - - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new DateDiff(Source.EMPTY, l("day"), dt1, dt2, zoneId).makePipe().asProcessor().process(null)); - assertEquals("The DATE_DIFF function resulted in an overflow; the number of units separating two date/datetime " + - "instances is too large. Try to use DATE_DIFF with a less precise unit.", - siae.getMessage()); - - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new DateDiff(Source.EMPTY, l("week"), dt1, dt2, zoneId).makePipe().asProcessor().process(null)); - assertEquals("The DATE_DIFF function resulted in an overflow; the number of units separating two date/datetime " + - "instances is too large. Try to use DATE_DIFF with a less precise unit.", - siae.getMessage()); - - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new DateDiff(Source.EMPTY, l("weekday"), dt1, dt2, zoneId).makePipe().asProcessor().process(null)); - assertEquals("The DATE_DIFF function resulted in an overflow; the number of units separating two date/datetime " + - "instances is too large. Try to use DATE_DIFF with a less precise unit.", - siae.getMessage()); - - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new DateDiff(Source.EMPTY, l("hours"), dt1, dt2, zoneId).makePipe().asProcessor().process(null)); - assertEquals("The DATE_DIFF function resulted in an overflow; the number of units separating two date/datetime " + - "instances is too large. Try to use DATE_DIFF with a less precise unit.", - siae.getMessage()); - - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new DateDiff(Source.EMPTY, l("minute"), dt1, dt2, zoneId).makePipe().asProcessor().process(null)); - assertEquals("The DATE_DIFF function resulted in an overflow; the number of units separating two date/datetime " + - "instances is too large. Try to use DATE_DIFF with a less precise unit.", - siae.getMessage()); - - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new DateDiff(Source.EMPTY, l("second"), dt1, dt2, zoneId).makePipe().asProcessor().process(null)); - assertEquals("The DATE_DIFF function resulted in an overflow; the number of units separating two date/datetime " + - "instances is too large. Try to use DATE_DIFF with a less precise unit.", - siae.getMessage()); - - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new DateDiff(Source.EMPTY, l("milliseconds"), dt2, dt1, zoneId).makePipe().asProcessor().process(null)); - assertEquals("The DATE_DIFF function resulted in an overflow; the number of units separating two date/datetime " + - "instances is too large. Try to use DATE_DIFF with a less precise unit.", - siae.getMessage()); - - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new DateDiff(Source.EMPTY, l("mcs"), dt1, dt2, zoneId).makePipe().asProcessor().process(null)); - assertEquals("The DATE_DIFF function resulted in an overflow; the number of units separating two date/datetime " + - "instances is too large. Try to use DATE_DIFF with a less precise unit.", - siae.getMessage()); - - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new DateDiff(Source.EMPTY, l("nanoseconds"), dt2, dt1, zoneId).makePipe().asProcessor().process(null)); - assertEquals("The DATE_DIFF function resulted in an overflow; the number of units separating two date/datetime " + - "instances is too large. Try to use DATE_DIFF with a less precise unit.", - siae.getMessage()); + SqlIllegalArgumentException siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new DateDiff(Source.EMPTY, l("month"), dt1, dt2, zoneId).makePipe().asProcessor().process(null) + ); + assertEquals( + "The DATE_DIFF function resulted in an overflow; the number of units separating two date/datetime " + + "instances is too large. Try to use DATE_DIFF with a less precise unit.", + siae.getMessage() + ); + + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new DateDiff(Source.EMPTY, l("dayofyear"), dt1, dt2, zoneId).makePipe().asProcessor().process(null) + ); + assertEquals( + "The DATE_DIFF function resulted in an overflow; the number of units separating two date/datetime " + + "instances is too large. Try to use DATE_DIFF with a less precise unit.", + siae.getMessage() + ); + + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new DateDiff(Source.EMPTY, l("day"), dt1, dt2, zoneId).makePipe().asProcessor().process(null) + ); + assertEquals( + "The DATE_DIFF function resulted in an overflow; the number of units separating two date/datetime " + + "instances is too large. Try to use DATE_DIFF with a less precise unit.", + siae.getMessage() + ); + + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new DateDiff(Source.EMPTY, l("week"), dt1, dt2, zoneId).makePipe().asProcessor().process(null) + ); + assertEquals( + "The DATE_DIFF function resulted in an overflow; the number of units separating two date/datetime " + + "instances is too large. Try to use DATE_DIFF with a less precise unit.", + siae.getMessage() + ); + + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new DateDiff(Source.EMPTY, l("weekday"), dt1, dt2, zoneId).makePipe().asProcessor().process(null) + ); + assertEquals( + "The DATE_DIFF function resulted in an overflow; the number of units separating two date/datetime " + + "instances is too large. Try to use DATE_DIFF with a less precise unit.", + siae.getMessage() + ); + + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new DateDiff(Source.EMPTY, l("hours"), dt1, dt2, zoneId).makePipe().asProcessor().process(null) + ); + assertEquals( + "The DATE_DIFF function resulted in an overflow; the number of units separating two date/datetime " + + "instances is too large. Try to use DATE_DIFF with a less precise unit.", + siae.getMessage() + ); + + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new DateDiff(Source.EMPTY, l("minute"), dt1, dt2, zoneId).makePipe().asProcessor().process(null) + ); + assertEquals( + "The DATE_DIFF function resulted in an overflow; the number of units separating two date/datetime " + + "instances is too large. Try to use DATE_DIFF with a less precise unit.", + siae.getMessage() + ); + + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new DateDiff(Source.EMPTY, l("second"), dt1, dt2, zoneId).makePipe().asProcessor().process(null) + ); + assertEquals( + "The DATE_DIFF function resulted in an overflow; the number of units separating two date/datetime " + + "instances is too large. Try to use DATE_DIFF with a less precise unit.", + siae.getMessage() + ); + + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new DateDiff(Source.EMPTY, l("milliseconds"), dt2, dt1, zoneId).makePipe().asProcessor().process(null) + ); + assertEquals( + "The DATE_DIFF function resulted in an overflow; the number of units separating two date/datetime " + + "instances is too large. Try to use DATE_DIFF with a less precise unit.", + siae.getMessage() + ); + + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new DateDiff(Source.EMPTY, l("mcs"), dt1, dt2, zoneId).makePipe().asProcessor().process(null) + ); + assertEquals( + "The DATE_DIFF function resulted in an overflow; the number of units separating two date/datetime " + + "instances is too large. Try to use DATE_DIFF with a less precise unit.", + siae.getMessage() + ); + + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new DateDiff(Source.EMPTY, l("nanoseconds"), dt2, dt1, zoneId).makePipe().asProcessor().process(null) + ); + assertEquals( + "The DATE_DIFF function resulted in an overflow; the number of units separating two date/datetime " + + "instances is too large. Try to use DATE_DIFF with a less precise unit.", + siae.getMessage() + ); } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DatePartPipeTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DatePartPipeTests.java index 7ebff6164e663..5c4f695585dc4 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DatePartPipeTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DatePartPipeTests.java @@ -39,12 +39,7 @@ private Expression randomDatePartPipeExpression() { } public static DatePartPipe randomDatePartPipe() { - return (DatePartPipe) new DatePart( - randomSource(), - randomStringLiteral(), - randomDatetimeLiteral(), - randomZone()) - .makePipe(); + return (DatePartPipe) new DatePart(randomSource(), randomStringLiteral(), randomDatetimeLiteral(), randomZone()).makePipe(); } @Override @@ -54,24 +49,13 @@ public void testTransform() { DatePartPipe b1 = randomInstance(); Expression newExpression = randomValueOtherThan(b1.expression(), this::randomDatePartPipeExpression); - DatePartPipe newB = new DatePartPipe( - b1.source(), - newExpression, - b1.left(), - b1.right(), - b1.zoneId()); + DatePartPipe newB = new DatePartPipe(b1.source(), newExpression, b1.left(), b1.right(), b1.zoneId()); assertEquals(newB, b1.transformPropertiesOnly(Expression.class, v -> Objects.equals(v, b1.expression()) ? newExpression : v)); DatePartPipe b2 = randomInstance(); Source newLoc = randomValueOtherThan(b2.source(), SourceTests::randomSource); - newB = new DatePartPipe( - newLoc, - b2.expression(), - b2.left(), - b2.right(), - b2.zoneId()); - assertEquals(newB, - b2.transformPropertiesOnly(Source.class, v -> Objects.equals(v, b2.source()) ? newLoc : v)); + newB = new DatePartPipe(newLoc, b2.expression(), b2.left(), b2.right(), b2.zoneId()); + assertEquals(newB, b2.transformPropertiesOnly(Source.class, v -> Objects.equals(v, b2.source()) ? newLoc : v)); } @Override @@ -80,7 +64,7 @@ public void testReplaceChildren() { Pipe newLeft = pipe(((Expression) randomValueOtherThan(b.left(), FunctionTestUtils::randomStringLiteral))); Pipe newRight = pipe(((Expression) randomValueOtherThan(b.right(), FunctionTestUtils::randomDatetimeLiteral))); ZoneId newZoneId = randomValueOtherThan(b.zoneId(), ESTestCase::randomZone); - DatePartPipe newB = new DatePartPipe( b.source(), b.expression(), b.left(), b.right(), newZoneId); + DatePartPipe newB = new DatePartPipe(b.source(), b.expression(), b.left(), b.right(), newZoneId); BinaryPipe transformed = newB.replaceChildren(newLeft, b.right()); assertEquals(transformed.left(), newLeft); @@ -104,29 +88,39 @@ public void testReplaceChildren() { @Override protected DatePartPipe mutate(DatePartPipe instance) { List> randoms = new ArrayList<>(); - randoms.add(f -> new DatePartPipe(f.source(), f.expression(), - pipe(((Expression) randomValueOtherThan(f.left(), FunctionTestUtils::randomStringLiteral))), - f.right(), - randomValueOtherThan(f.zoneId(), ESTestCase::randomZone))); - randoms.add(f -> new DatePartPipe(f.source(), f.expression(), - f.left(), - pipe(((Expression) randomValueOtherThan(f.right(), FunctionTestUtils::randomDatetimeLiteral))), - randomValueOtherThan(f.zoneId(), ESTestCase::randomZone))); - randoms.add(f -> new DatePartPipe(f.source(), f.expression(), - pipe(((Expression) randomValueOtherThan(f.left(), FunctionTestUtils::randomStringLiteral))), - pipe(((Expression) randomValueOtherThan(f.right(), FunctionTestUtils::randomDatetimeLiteral))), - randomValueOtherThan(f.zoneId(), ESTestCase::randomZone))); + randoms.add( + f -> new DatePartPipe( + f.source(), + f.expression(), + pipe(((Expression) randomValueOtherThan(f.left(), FunctionTestUtils::randomStringLiteral))), + f.right(), + randomValueOtherThan(f.zoneId(), ESTestCase::randomZone) + ) + ); + randoms.add( + f -> new DatePartPipe( + f.source(), + f.expression(), + f.left(), + pipe(((Expression) randomValueOtherThan(f.right(), FunctionTestUtils::randomDatetimeLiteral))), + randomValueOtherThan(f.zoneId(), ESTestCase::randomZone) + ) + ); + randoms.add( + f -> new DatePartPipe( + f.source(), + f.expression(), + pipe(((Expression) randomValueOtherThan(f.left(), FunctionTestUtils::randomStringLiteral))), + pipe(((Expression) randomValueOtherThan(f.right(), FunctionTestUtils::randomDatetimeLiteral))), + randomValueOtherThan(f.zoneId(), ESTestCase::randomZone) + ) + ); return randomFrom(randoms).apply(instance); } @Override protected DatePartPipe copy(DatePartPipe instance) { - return new DatePartPipe( - instance.source(), - instance.expression(), - instance.left(), - instance.right(), - instance.zoneId()); + return new DatePartPipe(instance.source(), instance.expression(), instance.left(), instance.right(), instance.zoneId()); } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DatePartProcessorTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DatePartProcessorTests.java index c042182673e9a..436c49c589af2 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DatePartProcessorTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DatePartProcessorTests.java @@ -28,7 +28,8 @@ public static DatePartProcessor randomDatePartProcessor() { return new DatePartProcessor( new ConstantProcessor(randomRealisticUnicodeOfLengthBetween(0, 128)), new ConstantProcessor(DateTimeTestUtils.nowWithMillisResolution()), - randomZone()); + randomZone() + ); } @Override @@ -51,28 +52,38 @@ protected DatePartProcessor mutateInstance(DatePartProcessor instance) { return new DatePartProcessor( new ConstantProcessor(ESTestCase.randomRealisticUnicodeOfLength(128)), new ConstantProcessor(DateTimeTestUtils.nowWithMillisResolution()), - randomValueOtherThan(instance.zoneId(), ESTestCase::randomZone)); + randomValueOtherThan(instance.zoneId(), ESTestCase::randomZone) + ); } public void testInvalidInputs() { - SqlIllegalArgumentException siae = expectThrows(SqlIllegalArgumentException.class, - () -> new DatePart(Source.EMPTY, l(5), randomDatetimeLiteral(), randomZone()).makePipe().asProcessor().process(null)); + SqlIllegalArgumentException siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new DatePart(Source.EMPTY, l(5), randomDatetimeLiteral(), randomZone()).makePipe().asProcessor().process(null) + ); assertEquals("A string is required; received [5]", siae.getMessage()); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new DatePart(Source.EMPTY, l("days"), l("foo"), randomZone()).makePipe().asProcessor().process(null)); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new DatePart(Source.EMPTY, l("days"), l("foo"), randomZone()).makePipe().asProcessor().process(null) + ); assertEquals("A date/datetime is required; received [foo]", siae.getMessage()); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new DatePart(Source.EMPTY, l("invalid"), randomDatetimeLiteral(), randomZone()).makePipe().asProcessor().process(null)); - assertEquals("A value of [YEAR, QUARTER, MONTH, DAYOFYEAR, DAY, WEEK, WEEKDAY, HOUR, MINUTE, SECOND, MILLISECOND, " + - "MICROSECOND, NANOSECOND, TZOFFSET] or their aliases is required; received [invalid]", - siae.getMessage()); - - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new DatePart(Source.EMPTY, l("dayfyear"), randomDatetimeLiteral(), randomZone()).makePipe().asProcessor().process(null)); - assertEquals("Received value [dayfyear] is not valid date part for extraction; did you mean [dayofyear, year]?", - siae.getMessage()); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new DatePart(Source.EMPTY, l("invalid"), randomDatetimeLiteral(), randomZone()).makePipe().asProcessor().process(null) + ); + assertEquals( + "A value of [YEAR, QUARTER, MONTH, DAYOFYEAR, DAY, WEEK, WEEKDAY, HOUR, MINUTE, SECOND, MILLISECOND, " + + "MICROSECOND, NANOSECOND, TZOFFSET] or their aliases is required; received [invalid]", + siae.getMessage() + ); + + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new DatePart(Source.EMPTY, l("dayfyear"), randomDatetimeLiteral(), randomZone()).makePipe().asProcessor().process(null) + ); + assertEquals("Received value [dayfyear] is not valid date part for extraction; did you mean [dayofyear, year]?", siae.getMessage()); } public void testWithNulls() { diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeFormatPipeTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeFormatPipeTests.java index e275eb43b4831..bd75e2af3ffb3 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeFormatPipeTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeFormatPipeTests.java @@ -32,10 +32,8 @@ public class DateTimeFormatPipeTests extends AbstractNodeTestCase functions = new ArrayList<>(); - functions.add(new DateTimeFormat(randomSource(), randomDatetimeLiteral(), randomStringLiteral(), randomZone()) - .makePipe()); - functions.add(new Format(randomSource(), randomDatetimeLiteral(), randomStringLiteral(), randomZone()) - .makePipe()); + functions.add(new DateTimeFormat(randomSource(), randomDatetimeLiteral(), randomStringLiteral(), randomZone()).makePipe()); + functions.add(new Format(randomSource(), randomDatetimeLiteral(), randomStringLiteral(), randomZone()).makePipe()); return (DateTimeFormatPipe) randomFrom(functions); } @@ -150,11 +148,13 @@ protected DateTimeFormatPipe mutate(DateTimeFormatPipe instance) { @Override protected DateTimeFormatPipe copy(DateTimeFormatPipe instance) { - return new DateTimeFormatPipe(instance.source(), + return new DateTimeFormatPipe( + instance.source(), instance.expression(), instance.left(), instance.right(), instance.zoneId(), - instance.formatter()); + instance.formatter() + ); } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeFormatProcessorTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeFormatProcessorTests.java index 68851c4bf41db..d84ff1395e2fc 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeFormatProcessorTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeFormatProcessorTests.java @@ -131,9 +131,7 @@ public void testFormatInvalidInputs() { siae = expectThrows( SqlIllegalArgumentException.class, - () -> new Format(Source.EMPTY, l(time(18, 10, 37, 123000000)), l("MM/dd"), randomZone()).makePipe() - .asProcessor() - .process(null) + () -> new Format(Source.EMPTY, l(time(18, 10, 37, 123000000)), l("MM/dd"), randomZone()).makePipe().asProcessor().process(null) ); assertEquals( "Invalid pattern [MM/dd] is received for formatting date/time [18:10:37.123Z]; Unsupported field: MonthOfYear", @@ -187,16 +185,12 @@ public void testFormatting() { .process(null) ); - zoneId = ZoneId.of("Etc/GMT-10"); dateTime = l(dateTime(2019, 9, 3, 18, 10, 37, 123456789)); assertEquals("AD : 3", new Format(Source.EMPTY, dateTime, l("G : Q"), zoneId).makePipe().asProcessor().process(null)); assertEquals("AD", new Format(Source.EMPTY, dateTime, l("g"), zoneId).makePipe().asProcessor().process(null)); - assertEquals( - "2019-09-04", - new Format(Source.EMPTY, dateTime, l("YYYY-MM-dd"), zoneId).makePipe().asProcessor().process(null) - ); + assertEquals("2019-09-04", new Format(Source.EMPTY, dateTime, l("YYYY-MM-dd"), zoneId).makePipe().asProcessor().process(null)); assertEquals( "2019-09-04 Wed", new Format(Source.EMPTY, dateTime, l("YYYY-MM-dd ddd"), zoneId).makePipe().asProcessor().process(null) @@ -232,10 +226,7 @@ public void testFormatting() { zoneId = ZoneId.of("America/Sao_Paulo"); assertEquals("-0300", new Format(Source.EMPTY, dateTime, l("Z"), zoneId).makePipe().asProcessor().process(null)); assertEquals("-03", new Format(Source.EMPTY, dateTime, l("z"), zoneId).makePipe().asProcessor().process(null)); - assertEquals( - "America/Sao_Paulo", - new Format(Source.EMPTY, dateTime, l("VV"), zoneId).makePipe().asProcessor().process(null) - ); + assertEquals("America/Sao_Paulo", new Format(Source.EMPTY, dateTime, l("VV"), zoneId).makePipe().asProcessor().process(null)); assertEquals( "07:11:22.1234", @@ -246,51 +237,43 @@ public void testFormatting() { assertEquals( "10:11", - new Format(Source.EMPTY, l(time(10, 11, 22, 123456789), TIME), l("H:m"), ZoneOffset.UTC).makePipe() - .asProcessor() - .process(null) + new Format(Source.EMPTY, l(time(10, 11, 22, 123456789), TIME), l("H:m"), ZoneOffset.UTC).makePipe().asProcessor().process(null) ); assertEquals( "21:9", - new Format(Source.EMPTY, l(time(21, 11, 22, 123456789), TIME), l("H:h"), ZoneOffset.UTC).makePipe() - .asProcessor() - .process(null) + new Format(Source.EMPTY, l(time(21, 11, 22, 123456789), TIME), l("H:h"), ZoneOffset.UTC).makePipe().asProcessor().process(null) ); assertEquals( "2-02", - new Format(Source.EMPTY, l(time(21, 11, 2, 123456789), TIME), l("s-ss"), ZoneOffset.UTC).makePipe() - .asProcessor() - .process(null) + new Format(Source.EMPTY, l(time(21, 11, 2, 123456789), TIME), l("s-ss"), ZoneOffset.UTC).makePipe().asProcessor().process(null) ); - assertEquals("9-09-Sep-September", - new Format(Source.EMPTY, dateTime, l("M-MM-MMM-MMMM"), zoneId).makePipe() - .asProcessor() - .process(null)); + assertEquals( + "9-09-Sep-September", + new Format(Source.EMPTY, dateTime, l("M-MM-MMM-MMMM"), zoneId).makePipe().asProcessor().process(null) + ); - assertEquals("arr: 3:10 PM", - new Format(Source.EMPTY, dateTime, l("'arr:' h:m t"), zoneId).makePipe() - .asProcessor() - .process(null)) - ; - assertEquals("-03/-0300/-03:00", - new Format(Source.EMPTY, dateTime, l("z/zz/zzz"), zoneId).makePipe() - .asProcessor() - .process(null)); + assertEquals("arr: 3:10 PM", new Format(Source.EMPTY, dateTime, l("'arr:' h:m t"), zoneId).makePipe().asProcessor().process(null)); + assertEquals("-03/-0300/-03:00", new Format(Source.EMPTY, dateTime, l("z/zz/zzz"), zoneId).makePipe().asProcessor().process(null)); assertEquals("3", new Format(Source.EMPTY, dateTime, l("d"), zoneId).makePipe().asProcessor().process(null)); - assertEquals("2001-01-2001-02001", - new Format(Source.EMPTY, l(dateTime(2001, 9, 3, 18, 10, 37, 123456789)), - l("y-yy-yyyy-yyyyy"), zoneId).makePipe().asProcessor().process(null)); - - assertEquals("%9-\"09-\\Sep-September", - new Format(Source.EMPTY, dateTime, l("%M-\"MM-\\MMM-MMMM"), zoneId).makePipe() + assertEquals( + "2001-01-2001-02001", + new Format(Source.EMPTY, l(dateTime(2001, 9, 3, 18, 10, 37, 123456789)), l("y-yy-yyyy-yyyyy"), zoneId).makePipe() .asProcessor() - .process(null)); + .process(null) + ); + + assertEquals( + "%9-\"09-\\Sep-September", + new Format(Source.EMPTY, dateTime, l("%M-\"MM-\\MMM-MMMM"), zoneId).makePipe().asProcessor().process(null) + ); - assertEquals("45-0045", + assertEquals( + "45-0045", new Format(Source.EMPTY, l(dateTime(45, 9, 3, 18, 10, 37, 123456789)), l("y-yyyy"), zoneId).makePipe() .asProcessor() - .process(null)); + .process(null) + ); } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeParsePipeTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeParsePipeTests.java index 100ae10b96085..9b357314aa9a3 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeParsePipeTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeParsePipeTests.java @@ -27,29 +27,13 @@ import static org.elasticsearch.xpack.ql.tree.SourceTests.randomSource; import static org.elasticsearch.xpack.sql.expression.function.scalar.datetime.DateTimeParseProcessor.Parser; - public class DateTimeParsePipeTests extends AbstractNodeTestCase { public static DateTimeParsePipe randomDateTimeParsePipe() { List functions = new ArrayList<>(); - functions.add(new DateTimeParse( - randomSource(), - randomStringLiteral(), - randomStringLiteral(), - randomZone() - ).makePipe()); - functions.add(new TimeParse( - randomSource(), - randomStringLiteral(), - randomStringLiteral(), - randomZone() - ).makePipe()); - functions.add(new DateParse( - randomSource(), - randomStringLiteral(), - randomStringLiteral(), - randomZone() - ).makePipe()); + functions.add(new DateTimeParse(randomSource(), randomStringLiteral(), randomStringLiteral(), randomZone()).makePipe()); + functions.add(new TimeParse(randomSource(), randomStringLiteral(), randomStringLiteral(), randomZone()).makePipe()); + functions.add(new DateParse(randomSource(), randomStringLiteral(), randomStringLiteral(), randomZone()).makePipe()); return (DateTimeParsePipe) randomFrom(functions); } @@ -69,13 +53,7 @@ public void testTransform() { DateTimeParsePipe b1 = randomInstance(); Expression newExpression = randomValueOtherThan(b1.expression(), this::randomDateTimeParsePipeExpression); - DateTimeParsePipe newB = new DateTimeParsePipe( - b1.source(), - newExpression, - b1.left(), - b1.right(), - b1.zoneId(), - b1.parser()); + DateTimeParsePipe newB = new DateTimeParsePipe(b1.source(), newExpression, b1.left(), b1.right(), b1.zoneId(), b1.parser()); assertEquals(newB, b1.transformPropertiesOnly(Expression.class, v -> Objects.equals(v, b1.expression()) ? newExpression : v)); DateTimeParsePipe b2 = randomInstance(); @@ -99,13 +77,7 @@ public void testReplaceChildren() { DateTimeParsePipe b = randomInstance(); Pipe newLeft = pipe(((Expression) randomValueOtherThan(b.left(), FunctionTestUtils::randomDatetimeLiteral))); Pipe newRight = pipe(((Expression) randomValueOtherThan(b.right(), FunctionTestUtils::randomStringLiteral))); - DateTimeParsePipe newB = new DateTimeParsePipe( - b.source(), - b.expression(), - b.left(), - b.right(), - b.zoneId(), - b.parser()); + DateTimeParsePipe newB = new DateTimeParsePipe(b.source(), b.expression(), b.left(), b.right(), b.zoneId(), b.parser()); BinaryPipe transformed = newB.replaceChildren(newLeft, b.right()); assertEquals(transformed.left(), newLeft); @@ -186,11 +158,12 @@ protected DateTimeParsePipe mutate(DateTimeParsePipe instance) { @Override protected DateTimeParsePipe copy(DateTimeParsePipe instance) { return new DateTimeParsePipe( - instance.source(), - instance.expression(), - instance.left(), - instance.right(), - instance.zoneId(), - instance.parser()); + instance.source(), + instance.expression(), + instance.left(), + instance.right(), + instance.zoneId(), + instance.parser() + ); } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeParseProcessorTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeParseProcessorTests.java index b3bb25012c64d..ac93ee69fa8a0 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeParseProcessorTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeParseProcessorTests.java @@ -21,9 +21,9 @@ import static org.elasticsearch.xpack.ql.expression.Literal.NULL; import static org.elasticsearch.xpack.ql.expression.function.scalar.FunctionTestUtils.l; import static org.elasticsearch.xpack.ql.expression.function.scalar.FunctionTestUtils.randomStringLiteral; +import static org.elasticsearch.xpack.sql.expression.function.scalar.datetime.DateTimeTestUtils.date; import static org.elasticsearch.xpack.sql.expression.function.scalar.datetime.DateTimeTestUtils.dateTime; import static org.elasticsearch.xpack.sql.expression.function.scalar.datetime.DateTimeTestUtils.time; -import static org.elasticsearch.xpack.sql.expression.function.scalar.datetime.DateTimeTestUtils.date; public class DateTimeParseProcessorTests extends AbstractSqlWireSerializingTestCase { @@ -79,10 +79,7 @@ public void testDateTimeInvalidInputs() { SqlIllegalArgumentException.class, () -> new DateTimeParse(Source.EMPTY, l("2020-04-07"), l("invalid"), randomZone()).makePipe().asProcessor().process(null) ); - assertEquals( - "Invalid datetime string [2020-04-07] or pattern [invalid] is received; Unknown pattern letter: i", - siae.getMessage() - ); + assertEquals("Invalid datetime string [2020-04-07] or pattern [invalid] is received; Unknown pattern letter: i", siae.getMessage()); siae = expectThrows( SqlIllegalArgumentException.class, @@ -103,8 +100,10 @@ public void testDateTimeInvalidInputs() { ); siae = expectThrows( - SqlIllegalArgumentException.class, () -> new DateTimeParse( - Source.EMPTY, l("10:20:30.123456789"), l("HH:mm:ss.SSSSSSSSS"), randomZone()).makePipe().asProcessor().process(null) + SqlIllegalArgumentException.class, + () -> new DateTimeParse(Source.EMPTY, l("10:20:30.123456789"), l("HH:mm:ss.SSSSSSSSS"), randomZone()).makePipe() + .asProcessor() + .process(null) ); assertEquals( "Invalid datetime string [10:20:30.123456789] or pattern [HH:mm:ss.SSSSSSSSS] is received; " @@ -130,18 +129,15 @@ public void testTimeInvalidInputs() { SqlIllegalArgumentException.class, () -> new TimeParse(Source.EMPTY, l("11:04:07"), l("invalid"), randomZone()).makePipe().asProcessor().process(null) ); - assertEquals( - "Invalid time string [11:04:07] or pattern [invalid] is received; Unknown pattern letter: i", - siae.getMessage() - ); + assertEquals("Invalid time string [11:04:07] or pattern [invalid] is received; Unknown pattern letter: i", siae.getMessage()); siae = expectThrows( SqlIllegalArgumentException.class, () -> new TimeParse(Source.EMPTY, l("11:04:07"), l("HH:mm"), randomZone()).makePipe().asProcessor().process(null) ); assertEquals( - "Invalid time string [11:04:07] or pattern [HH:mm] is received; " + - "Text '11:04:07' could not be parsed, unparsed text found at index 5", + "Invalid time string [11:04:07] or pattern [HH:mm] is received; " + + "Text '11:04:07' could not be parsed, unparsed text found at index 5", siae.getMessage() ); @@ -172,19 +168,16 @@ public void testDateInvalidInputs() { SqlIllegalArgumentException.class, () -> new DateParse(Source.EMPTY, l("07/05/2020"), l("invalid"), randomZone()).makePipe().asProcessor().process(null) ); - assertEquals( - "Invalid date string [07/05/2020] or pattern [invalid] is received; Unknown pattern letter: i", - siae.getMessage() - ); + assertEquals("Invalid date string [07/05/2020] or pattern [invalid] is received; Unknown pattern letter: i", siae.getMessage()); siae = expectThrows( - SqlIllegalArgumentException.class, - () -> new DateParse(Source.EMPTY, l("07/05/2020"), l("dd/MM"), randomZone()).makePipe().asProcessor().process(null) + SqlIllegalArgumentException.class, + () -> new DateParse(Source.EMPTY, l("07/05/2020"), l("dd/MM"), randomZone()).makePipe().asProcessor().process(null) ); assertEquals( - "Invalid date string [07/05/2020] or pattern [dd/MM] is received; " + - "Text '07/05/2020' could not be parsed, unparsed text found at index 5", - siae.getMessage() + "Invalid date string [07/05/2020] or pattern [dd/MM] is received; " + + "Text '07/05/2020' could not be parsed, unparsed text found at index 5", + siae.getMessage() ); siae = expectThrows( @@ -198,8 +191,7 @@ public void testDateInvalidInputs() { siae = expectThrows( SqlIllegalArgumentException.class, - () -> new DateParse(Source.EMPTY, l("05/2020 11:04:07"), l("MM/uuuu HH:mm:ss"), randomZone()) - .makePipe() + () -> new DateParse(Source.EMPTY, l("05/2020 11:04:07"), l("MM/uuuu HH:mm:ss"), randomZone()).makePipe() .asProcessor() .process(null) ); @@ -232,8 +224,7 @@ public void testParsing() { ZoneId zoneId = ZoneId.of("America/Sao_Paulo"); assertEquals( dateTime(2020, 4, 7, 10, 20, 30, 123000000, zoneId), - new DateTimeParse(Source.EMPTY, l("07/04/2020 10:20:30.123"), l("dd/MM/uuuu HH:mm:ss.SSS"), zoneId) - .makePipe() + new DateTimeParse(Source.EMPTY, l("07/04/2020 10:20:30.123"), l("dd/MM/uuuu HH:mm:ss.SSS"), zoneId).makePipe() .asProcessor() .process(null) ); @@ -254,9 +245,7 @@ public void testParsing() { // TimeParse assertEquals( time(10, 20, 30, 123000000, zoneId), - new TimeParse(Source.EMPTY, l("10:20:30.123"), l("HH:mm:ss.SSS"), zoneId).makePipe() - .asProcessor() - .process(null) + new TimeParse(Source.EMPTY, l("10:20:30.123"), l("HH:mm:ss.SSS"), zoneId).makePipe().asProcessor().process(null) ); assertEquals( time(10, 20, 30, 123456789, ZoneOffset.of("+05:30"), zoneId), @@ -267,15 +256,11 @@ public void testParsing() { // DateParse assertEquals( date(2020, 4, 7, zoneId), - new DateParse(Source.EMPTY, l("07/04/2020"), l("dd/MM/uuuu"), zoneId).makePipe() - .asProcessor() - .process(null) + new DateParse(Source.EMPTY, l("07/04/2020"), l("dd/MM/uuuu"), zoneId).makePipe().asProcessor().process(null) ); assertEquals( - date(2020, 4, 7, zoneId), - new DateParse(Source.EMPTY, l("07/04/2020 12:12:00"), l("dd/MM/uuuu HH:mm:ss"), zoneId).makePipe() - .asProcessor() - .process(null) + date(2020, 4, 7, zoneId), + new DateParse(Source.EMPTY, l("07/04/2020 12:12:00"), l("dd/MM/uuuu HH:mm:ss"), zoneId).makePipe().asProcessor().process(null) ); assertEquals( time(10, 20, 30, 123456789, ZoneOffset.of("+05:30"), zoneId), diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeProcessorTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeProcessorTests.java index 8b4ffa573097a..f6c2277ecae3c 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeProcessorTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeProcessorTests.java @@ -113,9 +113,7 @@ public void testApply_withTimezoneOtherThanUTC() { public void testFailOnTime() { DateTimeProcessor proc = new DateTimeProcessor(DateTimeExtractor.YEAR, UTC); - SqlIllegalArgumentException e = expectThrows(SqlIllegalArgumentException.class, () -> { - proc.process(OffsetTime.now(UTC)); - }); + SqlIllegalArgumentException e = expectThrows(SqlIllegalArgumentException.class, () -> { proc.process(OffsetTime.now(UTC)); }); assertThat(e.getMessage(), startsWith("A [date], a [time] or a [datetime] is required; received ")); } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeTestUtils.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeTestUtils.java index 9c29e38a5c72a..2c15291619a6a 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeTestUtils.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeTestUtils.java @@ -11,10 +11,10 @@ import java.time.Clock; import java.time.Duration; -import java.time.OffsetTime; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; +import java.time.OffsetTime; import java.time.ZoneId; import java.time.ZoneOffset; import java.time.ZonedDateTime; @@ -31,8 +31,7 @@ public static ZonedDateTime dateTime(int year, int month, int day, int hour, int return dateTime(year, month, day, hour, minute, seconds, nanos, DateUtils.UTC); } - public static ZonedDateTime dateTime(int year, int month, int day, int hour, int minute, int seconds, int nanos, - ZoneId zoneId) { + public static ZonedDateTime dateTime(int year, int month, int day, int hour, int minute, int seconds, int nanos, ZoneId zoneId) { return ZonedDateTime.of(year, month, day, hour, minute, seconds, nanos, zoneId); } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeToCharProcessorTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeToCharProcessorTests.java index 35fe39738b0bb..3ada55fa21c17 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeToCharProcessorTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeToCharProcessorTests.java @@ -65,7 +65,7 @@ public static Iterable parameters() throws Exception { continue; } String[] cols = line.split(quote(DELIMITER)); - params.add(new Object[]{testFile, lineNumber, cols[0], cols[1], cols[2], cols[3], cols[4]}); + params.add(new Object[] { testFile, lineNumber, cols[0], cols[1], cols[2], cols[3], cols[4] }); } return params; } @@ -91,8 +91,14 @@ public static Iterable parameters() throws Exception { * '[[formatString]]'). */ public DateTimeToCharProcessorTests( - String testFile, int lineNumber, String secondsAndFractionsSinceEpoch, String zone, - String formatString, String posgresTimestamp, String expectedResult) { + String testFile, + int lineNumber, + String secondsAndFractionsSinceEpoch, + String zone, + String formatString, + String posgresTimestamp, + String expectedResult + ) { this.testFile = testFile; this.lineNumber = lineNumber; @@ -106,11 +112,9 @@ public DateTimeToCharProcessorTests( public void test() { ZoneId zoneId = ZoneId.of(zone); ZonedDateTime timestamp = dateTimeWithFractions(secondsAndFractionsSinceEpoch); - String actualResult = - (String) new ToChar(EMPTY, l(timestamp, DATETIME), l(formatString, KEYWORD), zoneId) - .makePipe() - .asProcessor() - .process(null); + String actualResult = (String) new ToChar(EMPTY, l(timestamp, DATETIME), l(formatString, KEYWORD), zoneId).makePipe() + .asProcessor() + .process(null); List expectedResultSplitted = asList(expectedResult.split(quote(PATTERN_DELIMITER))); List resultSplitted = asList(actualResult.split(quote(PATTERN_DELIMITER))); List formatStringSplitted = asList(formatString.split(PATTERN_DELIMITER)); @@ -121,22 +125,34 @@ public void test() { String expectedPart = expectedResultSplitted.get(i); String actualPart = resultSplitted.get(i); assertEquals( - String.format(Locale.ROOT, - "\n" + - "Line number: %s (in %s)\n" + - "zone: %s\n" + - "timestamp (as epoch): %s\n" + - "timestamp (java, UTC): %s\n" + - "timestamp (postgres, to_timestamp): %s\n" + - "timestamp (java with zone): %s\n" + - "format string: %s\n" + - "expected (postgres to_char result): %s\n" + - "actual (ES to_char result): %s\n" + - " FAILED (sub)pattern: %s,", - lineNumber, testFile, - zone, secondsAndFractionsSinceEpoch, timestamp, posgresTimestamp, timestamp.withZoneSameInstant(zoneId), - formatString, expectedResult, actualResult, patternMaybeWithIndex), - expectedPart, actualPart); + String.format( + Locale.ROOT, + "\n" + + "Line number: %s (in %s)\n" + + "zone: %s\n" + + "timestamp (as epoch): %s\n" + + "timestamp (java, UTC): %s\n" + + "timestamp (postgres, to_timestamp): %s\n" + + "timestamp (java with zone): %s\n" + + "format string: %s\n" + + "expected (postgres to_char result): %s\n" + + "actual (ES to_char result): %s\n" + + " FAILED (sub)pattern: %s,", + lineNumber, + testFile, + zone, + secondsAndFractionsSinceEpoch, + timestamp, + posgresTimestamp, + timestamp.withZoneSameInstant(zoneId), + formatString, + expectedResult, + actualResult, + patternMaybeWithIndex + ), + expectedPart, + actualPart + ); } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTruncPipeTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTruncPipeTests.java index c3855a108e6eb..c3fe5340d4df5 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTruncPipeTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTruncPipeTests.java @@ -39,12 +39,7 @@ private Expression randomDateTruncPipeExpression() { } public static DateTruncPipe randomDateTruncPipe() { - return (DateTruncPipe) new DateTrunc( - randomSource(), - randomStringLiteral(), - randomDatetimeLiteral(), - randomZone()) - .makePipe(); + return (DateTruncPipe) new DateTrunc(randomSource(), randomStringLiteral(), randomDatetimeLiteral(), randomZone()).makePipe(); } @Override @@ -54,24 +49,13 @@ public void testTransform() { DateTruncPipe b1 = randomInstance(); Expression newExpression = randomValueOtherThan(b1.expression(), this::randomDateTruncPipeExpression); - DateTruncPipe newB = new DateTruncPipe( - b1.source(), - newExpression, - b1.left(), - b1.right(), - b1.zoneId()); + DateTruncPipe newB = new DateTruncPipe(b1.source(), newExpression, b1.left(), b1.right(), b1.zoneId()); assertEquals(newB, b1.transformPropertiesOnly(Expression.class, v -> Objects.equals(v, b1.expression()) ? newExpression : v)); DateTruncPipe b2 = randomInstance(); Source newLoc = randomValueOtherThan(b2.source(), SourceTests::randomSource); - newB = new DateTruncPipe( - newLoc, - b2.expression(), - b2.left(), - b2.right(), - b2.zoneId()); - assertEquals(newB, - b2.transformPropertiesOnly(Source.class, v -> Objects.equals(v, b2.source()) ? newLoc : v)); + newB = new DateTruncPipe(newLoc, b2.expression(), b2.left(), b2.right(), b2.zoneId()); + assertEquals(newB, b2.transformPropertiesOnly(Source.class, v -> Objects.equals(v, b2.source()) ? newLoc : v)); } @Override @@ -80,7 +64,7 @@ public void testReplaceChildren() { Pipe newLeft = pipe(((Expression) randomValueOtherThan(b.left(), FunctionTestUtils::randomStringLiteral))); Pipe newRight = pipe(((Expression) randomValueOtherThan(b.right(), FunctionTestUtils::randomDatetimeLiteral))); ZoneId newZoneId = randomValueOtherThan(b.zoneId(), ESTestCase::randomZone); - DateTruncPipe newB = new DateTruncPipe( b.source(), b.expression(), b.left(), b.right(), newZoneId); + DateTruncPipe newB = new DateTruncPipe(b.source(), b.expression(), b.left(), b.right(), newZoneId); BinaryPipe transformed = newB.replaceChildren(newLeft, b.right()); assertEquals(transformed.left(), newLeft); @@ -104,29 +88,39 @@ public void testReplaceChildren() { @Override protected DateTruncPipe mutate(DateTruncPipe instance) { List> randoms = new ArrayList<>(); - randoms.add(f -> new DateTruncPipe(f.source(), f.expression(), - pipe(((Expression) randomValueOtherThan(f.left(), FunctionTestUtils::randomStringLiteral))), - f.right(), - randomValueOtherThan(f.zoneId(), ESTestCase::randomZone))); - randoms.add(f -> new DateTruncPipe(f.source(), f.expression(), - f.left(), - pipe(((Expression) randomValueOtherThan(f.right(), FunctionTestUtils::randomDatetimeLiteral))), - randomValueOtherThan(f.zoneId(), ESTestCase::randomZone))); - randoms.add(f -> new DateTruncPipe(f.source(), f.expression(), - pipe(((Expression) randomValueOtherThan(f.left(), FunctionTestUtils::randomStringLiteral))), - pipe(((Expression) randomValueOtherThan(f.right(), FunctionTestUtils::randomDatetimeLiteral))), - randomValueOtherThan(f.zoneId(), ESTestCase::randomZone))); + randoms.add( + f -> new DateTruncPipe( + f.source(), + f.expression(), + pipe(((Expression) randomValueOtherThan(f.left(), FunctionTestUtils::randomStringLiteral))), + f.right(), + randomValueOtherThan(f.zoneId(), ESTestCase::randomZone) + ) + ); + randoms.add( + f -> new DateTruncPipe( + f.source(), + f.expression(), + f.left(), + pipe(((Expression) randomValueOtherThan(f.right(), FunctionTestUtils::randomDatetimeLiteral))), + randomValueOtherThan(f.zoneId(), ESTestCase::randomZone) + ) + ); + randoms.add( + f -> new DateTruncPipe( + f.source(), + f.expression(), + pipe(((Expression) randomValueOtherThan(f.left(), FunctionTestUtils::randomStringLiteral))), + pipe(((Expression) randomValueOtherThan(f.right(), FunctionTestUtils::randomDatetimeLiteral))), + randomValueOtherThan(f.zoneId(), ESTestCase::randomZone) + ) + ); return randomFrom(randoms).apply(instance); } @Override protected DateTruncPipe copy(DateTruncPipe instance) { - return new DateTruncPipe( - instance.source(), - instance.expression(), - instance.left(), - instance.right(), - instance.zoneId()); + return new DateTruncPipe(instance.source(), instance.expression(), instance.left(), instance.right(), instance.zoneId()); } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTruncProcessorTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTruncProcessorTests.java index 4dd942747c69a..3ddddb4392d3f 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTruncProcessorTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTruncProcessorTests.java @@ -42,7 +42,8 @@ public static DateTruncProcessor randomDateTruncProcessor() { return new DateTruncProcessor( new ConstantProcessor(randomRealisticUnicodeOfLengthBetween(0, 128)), new ConstantProcessor(DateTimeTestUtils.nowWithMillisResolution()), - randomZone()); + randomZone() + ); } @Override @@ -65,7 +66,8 @@ protected DateTruncProcessor mutateInstance(DateTruncProcessor instance) { return new DateTruncProcessor( new ConstantProcessor(ESTestCase.randomRealisticUnicodeOfLength(128)), new ConstantProcessor(DateTimeTestUtils.nowWithMillisResolution()), - randomValueOtherThan(instance.zoneId(), ESTestCase::randomZone)); + randomValueOtherThan(instance.zoneId(), ESTestCase::randomZone) + ); } public void testInvalidInputs() { @@ -74,34 +76,45 @@ public void testInvalidInputs() { TemporalAmount duration = Duration.ofDays(42).plusHours(12).plusMinutes(23).plusSeconds(12).plusNanos(143000000); Literal dayToSecond = intervalLiteral(duration, INTERVAL_DAY_TO_SECOND); - SqlIllegalArgumentException siae = expectThrows(SqlIllegalArgumentException.class, - () -> new DateTrunc(Source.EMPTY, l(5), randomDatetimeLiteral(), randomZone()).makePipe().asProcessor().process(null)); + SqlIllegalArgumentException siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new DateTrunc(Source.EMPTY, l(5), randomDatetimeLiteral(), randomZone()).makePipe().asProcessor().process(null) + ); assertEquals("A string is required; received [5]", siae.getMessage()); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new DateTrunc(Source.EMPTY, l("days"), l("foo"), randomZone()).makePipe().asProcessor().process(null)); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new DateTrunc(Source.EMPTY, l("days"), l("foo"), randomZone()).makePipe().asProcessor().process(null) + ); assertEquals("A date/datetime/interval is required; received [foo]", siae.getMessage()); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new DateTrunc(Source.EMPTY, l("invalid"), randomDatetimeLiteral(), randomZone()).makePipe().asProcessor().process(null)); - assertEquals("A value of [MILLENNIUM, CENTURY, DECADE, YEAR, QUARTER, MONTH, WEEK, DAY, HOUR, MINUTE, " + - "SECOND, MILLISECOND, MICROSECOND, NANOSECOND] or their aliases is required; received [invalid]", - siae.getMessage()); - - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new DateTrunc(Source.EMPTY, l("dacede"), randomDatetimeLiteral(), randomZone()).makePipe().asProcessor().process(null)); - assertEquals("Received value [dacede] is not valid date part for truncation; did you mean [decade, decades]?", - siae.getMessage()); - - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new DateTrunc(Source.EMPTY, l("weeks"), yearToMonth, null).makePipe().asProcessor().process(null)); - assertEquals("Truncating intervals is not supported for weeks units", - siae.getMessage()); - - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new DateTrunc(Source.EMPTY, l("week"), dayToSecond, null).makePipe().asProcessor().process(null)); - assertEquals("Truncating intervals is not supported for week units", - siae.getMessage()); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new DateTrunc(Source.EMPTY, l("invalid"), randomDatetimeLiteral(), randomZone()).makePipe().asProcessor().process(null) + ); + assertEquals( + "A value of [MILLENNIUM, CENTURY, DECADE, YEAR, QUARTER, MONTH, WEEK, DAY, HOUR, MINUTE, " + + "SECOND, MILLISECOND, MICROSECOND, NANOSECOND] or their aliases is required; received [invalid]", + siae.getMessage() + ); + + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new DateTrunc(Source.EMPTY, l("dacede"), randomDatetimeLiteral(), randomZone()).makePipe().asProcessor().process(null) + ); + assertEquals("Received value [dacede] is not valid date part for truncation; did you mean [decade, decades]?", siae.getMessage()); + + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new DateTrunc(Source.EMPTY, l("weeks"), yearToMonth, null).makePipe().asProcessor().process(null) + ); + assertEquals("Truncating intervals is not supported for weeks units", siae.getMessage()); + + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new DateTrunc(Source.EMPTY, l("week"), dayToSecond, null).makePipe().asProcessor().process(null) + ); + assertEquals("Truncating intervals is not supported for week units", siae.getMessage()); } public void testWithNulls() { @@ -118,189 +131,298 @@ public void testTruncation() { TemporalAmount duration = Duration.ofDays(105).plusHours(2).plusMinutes(45).plusSeconds(55).plusNanos(123456789); Literal dayToSecond = intervalLiteral(duration, INTERVAL_DAY_TO_SECOND); - assertEquals("2000-01-01T00:00:00.000+10:00", - DateUtils.toString((ZonedDateTime) new DateTrunc(Source.EMPTY, l("millennia"), dateTime, zoneId) - .makePipe().asProcessor().process(null))); - assertEquals("2000-01-01T00:00:00.000+10:00", - DateUtils.toString((ZonedDateTime) new DateTrunc(Source.EMPTY, l("CENTURY"), dateTime, zoneId) - .makePipe().asProcessor().process(null))); - assertEquals("2010-01-01T00:00:00.000+10:00", - DateUtils.toString((ZonedDateTime) new DateTrunc(Source.EMPTY, l("decades"), dateTime, zoneId) - .makePipe().asProcessor().process(null))); - assertEquals("2019-01-01T00:00:00.000+10:00", - DateUtils.toString((ZonedDateTime) new DateTrunc(Source.EMPTY, l("years"), dateTime, zoneId) - .makePipe().asProcessor().process(null))); - assertEquals("2019-07-01T00:00:00.000+10:00", - toString((ZonedDateTime) new DateTrunc(Source.EMPTY, l("quarters"), dateTime, zoneId) - .makePipe().asProcessor().process(null))); - assertEquals("2019-09-01T00:00:00.000+10:00", - toString((ZonedDateTime) new DateTrunc(Source.EMPTY, l("month"), dateTime, zoneId) - .makePipe().asProcessor().process(null))); - assertEquals("2019-09-02T00:00:00.000+10:00", - toString((ZonedDateTime) new DateTrunc(Source.EMPTY, l("weeks"), dateTime, zoneId) - .makePipe().asProcessor().process(null))); - assertEquals("2019-09-04T00:00:00.000+10:00", - toString((ZonedDateTime) new DateTrunc(Source.EMPTY, l("days"), dateTime, zoneId) - .makePipe().asProcessor().process(null))); - assertEquals("2019-09-04T04:00:00.000+10:00", - toString((ZonedDateTime) new DateTrunc(Source.EMPTY, l("hh"), dateTime, zoneId) - .makePipe().asProcessor().process(null))); - assertEquals("2019-09-04T04:10:00.000+10:00", - toString((ZonedDateTime) new DateTrunc(Source.EMPTY, l("mi"), dateTime, zoneId) - .makePipe().asProcessor().process(null))); - assertEquals("2019-09-04T04:10:37.000+10:00", - toString((ZonedDateTime) new DateTrunc(Source.EMPTY, l("second"), dateTime, zoneId) - .makePipe().asProcessor().process(null))); - assertEquals("2019-09-04T04:10:37.123+10:00", - toString((ZonedDateTime) new DateTrunc(Source.EMPTY, l("ms"), dateTime, zoneId) - .makePipe().asProcessor().process(null))); - assertEquals("2019-09-04T04:10:37.123456+10:00", - toString((ZonedDateTime) new DateTrunc(Source.EMPTY, l("mcs"), dateTime, zoneId) - .makePipe().asProcessor().process(null))); - assertEquals("2019-09-04T04:10:37.123456789+10:00", - toString((ZonedDateTime) new DateTrunc(Source.EMPTY, l("nanoseconds"), dateTime, zoneId) - .makePipe().asProcessor().process(null))); - - assertEquals("+2000-0", - toString((IntervalYearMonth) new DateTrunc(Source.EMPTY, l("millennia"), yearToMonth, null) - .makePipe().asProcessor().process(null))); - assertEquals("+2000-0", - toString((IntervalYearMonth) new DateTrunc(Source.EMPTY, l("CENTURY"), yearToMonth, null) - .makePipe().asProcessor().process(null))); - assertEquals("+2010-0", - toString((IntervalYearMonth) new DateTrunc(Source.EMPTY, l("decades"), yearToMonth, null) - .makePipe().asProcessor().process(null))); - assertEquals("+2019-0", - toString((IntervalYearMonth) new DateTrunc(Source.EMPTY, l("years"), yearToMonth, null) - .makePipe().asProcessor().process(null))); - assertEquals("+2019-9", - toString((IntervalYearMonth) new DateTrunc(Source.EMPTY, l("quarters"), yearToMonth, null) - .makePipe().asProcessor().process(null))); - assertEquals("+2019-10", - toString((IntervalYearMonth) new DateTrunc(Source.EMPTY, l("month"), yearToMonth, null) - .makePipe().asProcessor().process(null))); - assertEquals("+2019-10", - toString((IntervalYearMonth) new DateTrunc(Source.EMPTY, l("days"), yearToMonth, null) - .makePipe().asProcessor().process(null))); - assertEquals("+2019-10", - toString((IntervalYearMonth) new DateTrunc(Source.EMPTY, l("hh"), yearToMonth, null) - .makePipe().asProcessor().process(null))); - assertEquals("+2019-10", - toString((IntervalYearMonth) new DateTrunc(Source.EMPTY, l("mi"), yearToMonth, null) - .makePipe().asProcessor().process(null))); - assertEquals("+2019-10", - toString((IntervalYearMonth) new DateTrunc(Source.EMPTY, l("second"), yearToMonth, null) - .makePipe().asProcessor().process(null))); - assertEquals("+2019-10", - toString((IntervalYearMonth) new DateTrunc(Source.EMPTY, l("ms"), yearToMonth, null) - .makePipe().asProcessor().process(null))); - assertEquals("+2019-10", - toString((IntervalYearMonth) new DateTrunc(Source.EMPTY, l("mcs"), yearToMonth, null) - .makePipe().asProcessor().process(null))); - assertEquals("+2019-10", - toString((IntervalYearMonth) new DateTrunc(Source.EMPTY, l("nanoseconds"), yearToMonth, null) - .makePipe().asProcessor().process(null))); - - assertEquals("+0 00:00:00", - toString((IntervalDayTime) new DateTrunc(Source.EMPTY, l("millennia"), dayToSecond, null) - .makePipe().asProcessor().process(null))); - assertEquals("+0 00:00:00", - toString((IntervalDayTime) new DateTrunc(Source.EMPTY, l("CENTURY"), dayToSecond, null) - .makePipe().asProcessor().process(null))); - assertEquals("+0 00:00:00", - toString((IntervalDayTime) new DateTrunc(Source.EMPTY, l("decades"), dayToSecond, null) - .makePipe().asProcessor().process(null))); - assertEquals("+0 00:00:00", - toString((IntervalDayTime) new DateTrunc(Source.EMPTY, l("years"), dayToSecond, null) - .makePipe().asProcessor().process(null))); - assertEquals("+0 00:00:00", - toString((IntervalDayTime) new DateTrunc(Source.EMPTY, l("quarters"), dayToSecond, null) - .makePipe().asProcessor().process(null))); - assertEquals("+0 00:00:00", - toString((IntervalDayTime) new DateTrunc(Source.EMPTY, l("month"), dayToSecond, null) - .makePipe().asProcessor().process(null))); - assertEquals("+105 00:00:00", - toString((IntervalDayTime) new DateTrunc(Source.EMPTY, l("days"), dayToSecond, null) - .makePipe().asProcessor().process(null))); - assertEquals("+105 02:00:00", - toString((IntervalDayTime) new DateTrunc(Source.EMPTY, l("hh"), dayToSecond, null) - .makePipe().asProcessor().process(null))); - assertEquals("+105 02:45:00", - toString((IntervalDayTime) new DateTrunc(Source.EMPTY, l("mi"), dayToSecond, null) - .makePipe().asProcessor().process(null))); - assertEquals("+105 02:45:55", - toString((IntervalDayTime) new DateTrunc(Source.EMPTY, l("second"), dayToSecond, null) - .makePipe().asProcessor().process(null))); - assertEquals("+105 02:45:55.123", - toString((IntervalDayTime) new DateTrunc(Source.EMPTY, l("ms"), dayToSecond, null) - .makePipe().asProcessor().process(null))); - assertEquals("+105 02:45:55.123", - toString((IntervalDayTime) new DateTrunc(Source.EMPTY, l("microseconds"), dayToSecond, null) - .makePipe().asProcessor().process(null))); - assertEquals("+105 02:45:55.123", - toString((IntervalDayTime) new DateTrunc(Source.EMPTY, l("nanoseconds"), dayToSecond, null) - .makePipe().asProcessor().process(null))); + assertEquals( + "2000-01-01T00:00:00.000+10:00", + DateUtils.toString( + (ZonedDateTime) new DateTrunc(Source.EMPTY, l("millennia"), dateTime, zoneId).makePipe().asProcessor().process(null) + ) + ); + assertEquals( + "2000-01-01T00:00:00.000+10:00", + DateUtils.toString( + (ZonedDateTime) new DateTrunc(Source.EMPTY, l("CENTURY"), dateTime, zoneId).makePipe().asProcessor().process(null) + ) + ); + assertEquals( + "2010-01-01T00:00:00.000+10:00", + DateUtils.toString( + (ZonedDateTime) new DateTrunc(Source.EMPTY, l("decades"), dateTime, zoneId).makePipe().asProcessor().process(null) + ) + ); + assertEquals( + "2019-01-01T00:00:00.000+10:00", + DateUtils.toString( + (ZonedDateTime) new DateTrunc(Source.EMPTY, l("years"), dateTime, zoneId).makePipe().asProcessor().process(null) + ) + ); + assertEquals( + "2019-07-01T00:00:00.000+10:00", + toString((ZonedDateTime) new DateTrunc(Source.EMPTY, l("quarters"), dateTime, zoneId).makePipe().asProcessor().process(null)) + ); + assertEquals( + "2019-09-01T00:00:00.000+10:00", + toString((ZonedDateTime) new DateTrunc(Source.EMPTY, l("month"), dateTime, zoneId).makePipe().asProcessor().process(null)) + ); + assertEquals( + "2019-09-02T00:00:00.000+10:00", + toString((ZonedDateTime) new DateTrunc(Source.EMPTY, l("weeks"), dateTime, zoneId).makePipe().asProcessor().process(null)) + ); + assertEquals( + "2019-09-04T00:00:00.000+10:00", + toString((ZonedDateTime) new DateTrunc(Source.EMPTY, l("days"), dateTime, zoneId).makePipe().asProcessor().process(null)) + ); + assertEquals( + "2019-09-04T04:00:00.000+10:00", + toString((ZonedDateTime) new DateTrunc(Source.EMPTY, l("hh"), dateTime, zoneId).makePipe().asProcessor().process(null)) + ); + assertEquals( + "2019-09-04T04:10:00.000+10:00", + toString((ZonedDateTime) new DateTrunc(Source.EMPTY, l("mi"), dateTime, zoneId).makePipe().asProcessor().process(null)) + ); + assertEquals( + "2019-09-04T04:10:37.000+10:00", + toString((ZonedDateTime) new DateTrunc(Source.EMPTY, l("second"), dateTime, zoneId).makePipe().asProcessor().process(null)) + ); + assertEquals( + "2019-09-04T04:10:37.123+10:00", + toString((ZonedDateTime) new DateTrunc(Source.EMPTY, l("ms"), dateTime, zoneId).makePipe().asProcessor().process(null)) + ); + assertEquals( + "2019-09-04T04:10:37.123456+10:00", + toString((ZonedDateTime) new DateTrunc(Source.EMPTY, l("mcs"), dateTime, zoneId).makePipe().asProcessor().process(null)) + ); + assertEquals( + "2019-09-04T04:10:37.123456789+10:00", + toString((ZonedDateTime) new DateTrunc(Source.EMPTY, l("nanoseconds"), dateTime, zoneId).makePipe().asProcessor().process(null)) + ); + + assertEquals( + "+2000-0", + toString( + (IntervalYearMonth) new DateTrunc(Source.EMPTY, l("millennia"), yearToMonth, null).makePipe().asProcessor().process(null) + ) + ); + assertEquals( + "+2000-0", + toString( + (IntervalYearMonth) new DateTrunc(Source.EMPTY, l("CENTURY"), yearToMonth, null).makePipe().asProcessor().process(null) + ) + ); + assertEquals( + "+2010-0", + toString( + (IntervalYearMonth) new DateTrunc(Source.EMPTY, l("decades"), yearToMonth, null).makePipe().asProcessor().process(null) + ) + ); + assertEquals( + "+2019-0", + toString((IntervalYearMonth) new DateTrunc(Source.EMPTY, l("years"), yearToMonth, null).makePipe().asProcessor().process(null)) + ); + assertEquals( + "+2019-9", + toString( + (IntervalYearMonth) new DateTrunc(Source.EMPTY, l("quarters"), yearToMonth, null).makePipe().asProcessor().process(null) + ) + ); + assertEquals( + "+2019-10", + toString((IntervalYearMonth) new DateTrunc(Source.EMPTY, l("month"), yearToMonth, null).makePipe().asProcessor().process(null)) + ); + assertEquals( + "+2019-10", + toString((IntervalYearMonth) new DateTrunc(Source.EMPTY, l("days"), yearToMonth, null).makePipe().asProcessor().process(null)) + ); + assertEquals( + "+2019-10", + toString((IntervalYearMonth) new DateTrunc(Source.EMPTY, l("hh"), yearToMonth, null).makePipe().asProcessor().process(null)) + ); + assertEquals( + "+2019-10", + toString((IntervalYearMonth) new DateTrunc(Source.EMPTY, l("mi"), yearToMonth, null).makePipe().asProcessor().process(null)) + ); + assertEquals( + "+2019-10", + toString((IntervalYearMonth) new DateTrunc(Source.EMPTY, l("second"), yearToMonth, null).makePipe().asProcessor().process(null)) + ); + assertEquals( + "+2019-10", + toString((IntervalYearMonth) new DateTrunc(Source.EMPTY, l("ms"), yearToMonth, null).makePipe().asProcessor().process(null)) + ); + assertEquals( + "+2019-10", + toString((IntervalYearMonth) new DateTrunc(Source.EMPTY, l("mcs"), yearToMonth, null).makePipe().asProcessor().process(null)) + ); + assertEquals( + "+2019-10", + toString( + (IntervalYearMonth) new DateTrunc(Source.EMPTY, l("nanoseconds"), yearToMonth, null).makePipe().asProcessor().process(null) + ) + ); + + assertEquals( + "+0 00:00:00", + toString( + (IntervalDayTime) new DateTrunc(Source.EMPTY, l("millennia"), dayToSecond, null).makePipe().asProcessor().process(null) + ) + ); + assertEquals( + "+0 00:00:00", + toString((IntervalDayTime) new DateTrunc(Source.EMPTY, l("CENTURY"), dayToSecond, null).makePipe().asProcessor().process(null)) + ); + assertEquals( + "+0 00:00:00", + toString((IntervalDayTime) new DateTrunc(Source.EMPTY, l("decades"), dayToSecond, null).makePipe().asProcessor().process(null)) + ); + assertEquals( + "+0 00:00:00", + toString((IntervalDayTime) new DateTrunc(Source.EMPTY, l("years"), dayToSecond, null).makePipe().asProcessor().process(null)) + ); + assertEquals( + "+0 00:00:00", + toString((IntervalDayTime) new DateTrunc(Source.EMPTY, l("quarters"), dayToSecond, null).makePipe().asProcessor().process(null)) + ); + assertEquals( + "+0 00:00:00", + toString((IntervalDayTime) new DateTrunc(Source.EMPTY, l("month"), dayToSecond, null).makePipe().asProcessor().process(null)) + ); + assertEquals( + "+105 00:00:00", + toString((IntervalDayTime) new DateTrunc(Source.EMPTY, l("days"), dayToSecond, null).makePipe().asProcessor().process(null)) + ); + assertEquals( + "+105 02:00:00", + toString((IntervalDayTime) new DateTrunc(Source.EMPTY, l("hh"), dayToSecond, null).makePipe().asProcessor().process(null)) + ); + assertEquals( + "+105 02:45:00", + toString((IntervalDayTime) new DateTrunc(Source.EMPTY, l("mi"), dayToSecond, null).makePipe().asProcessor().process(null)) + ); + assertEquals( + "+105 02:45:55", + toString((IntervalDayTime) new DateTrunc(Source.EMPTY, l("second"), dayToSecond, null).makePipe().asProcessor().process(null)) + ); + assertEquals( + "+105 02:45:55.123", + toString((IntervalDayTime) new DateTrunc(Source.EMPTY, l("ms"), dayToSecond, null).makePipe().asProcessor().process(null)) + ); + assertEquals( + "+105 02:45:55.123", + toString( + (IntervalDayTime) new DateTrunc(Source.EMPTY, l("microseconds"), dayToSecond, null).makePipe().asProcessor().process(null) + ) + ); + assertEquals( + "+105 02:45:55.123", + toString( + (IntervalDayTime) new DateTrunc(Source.EMPTY, l("nanoseconds"), dayToSecond, null).makePipe().asProcessor().process(null) + ) + ); } public void testTruncationEdgeCases() { ZoneId zoneId = ZoneId.of("Etc/GMT-10"); Literal dateTime = l(dateTime(-11412, 9, 3, 18, 10, 37, 123456789)); - assertEquals("-11000-01-01T00:00:00.000+10:00", - DateUtils.toString((ZonedDateTime) new DateTrunc(Source.EMPTY, l("millennia"), dateTime, zoneId) - .makePipe().asProcessor().process(null))); + assertEquals( + "-11000-01-01T00:00:00.000+10:00", + DateUtils.toString( + (ZonedDateTime) new DateTrunc(Source.EMPTY, l("millennia"), dateTime, zoneId).makePipe().asProcessor().process(null) + ) + ); dateTime = l(dateTime(-12999, 9, 3, 18, 10, 37, 123456789)); - assertEquals("-12900-01-01T00:00:00.000+10:00", - DateUtils.toString((ZonedDateTime) new DateTrunc(Source.EMPTY, l("centuries"), dateTime, zoneId) - .makePipe().asProcessor().process(null))); + assertEquals( + "-12900-01-01T00:00:00.000+10:00", + DateUtils.toString( + (ZonedDateTime) new DateTrunc(Source.EMPTY, l("centuries"), dateTime, zoneId).makePipe().asProcessor().process(null) + ) + ); dateTime = l(dateTime(-32999, 9, 3, 18, 10, 37, 123456789)); - assertEquals("-32990-01-01T00:00:00.000+10:00", - DateUtils.toString((ZonedDateTime) new DateTrunc(Source.EMPTY, l("decades"), dateTime, zoneId) - .makePipe().asProcessor().process(null))); + assertEquals( + "-32990-01-01T00:00:00.000+10:00", + DateUtils.toString( + (ZonedDateTime) new DateTrunc(Source.EMPTY, l("decades"), dateTime, zoneId).makePipe().asProcessor().process(null) + ) + ); dateTime = l(dateTime(-1234, 9, 3, 18, 10, 37, 123456789)); - assertEquals("-1234-08-29T00:00:00.000+10:00", - DateUtils.toString((ZonedDateTime) new DateTrunc(Source.EMPTY, l("week"), dateTime, zoneId) - .makePipe().asProcessor().process(null))); + assertEquals( + "-1234-08-29T00:00:00.000+10:00", + DateUtils.toString( + (ZonedDateTime) new DateTrunc(Source.EMPTY, l("week"), dateTime, zoneId).makePipe().asProcessor().process(null) + ) + ); Literal yearToMonth = intervalLiteral(Period.ofYears(-12523).minusMonths(10), INTERVAL_YEAR_TO_MONTH); - assertEquals("-12000-0", toString((IntervalYearMonth) new DateTrunc(Source.EMPTY, l("millennia"), yearToMonth, null) - .makePipe().asProcessor().process(null))); + assertEquals( + "-12000-0", + toString( + (IntervalYearMonth) new DateTrunc(Source.EMPTY, l("millennia"), yearToMonth, null).makePipe().asProcessor().process(null) + ) + ); yearToMonth = intervalLiteral(Period.ofYears(-32543).minusMonths(10), INTERVAL_YEAR_TO_MONTH); - assertEquals("-32500-0", toString((IntervalYearMonth) new DateTrunc(Source.EMPTY, l("centuries"), yearToMonth, null) - .makePipe().asProcessor().process(null))); + assertEquals( + "-32500-0", + toString( + (IntervalYearMonth) new DateTrunc(Source.EMPTY, l("centuries"), yearToMonth, null).makePipe().asProcessor().process(null) + ) + ); yearToMonth = intervalLiteral(Period.ofYears(-24321).minusMonths(10), INTERVAL_YEAR_TO_MONTH); - assertEquals("-24320-0", toString((IntervalYearMonth) new DateTrunc(Source.EMPTY, l("decades"), yearToMonth, null) - .makePipe().asProcessor().process(null))); - - Literal dayToSecond = intervalLiteral(Duration.ofDays(-435).minusHours(23).minusMinutes(45).minusSeconds(55).minusNanos(123000000), - INTERVAL_DAY_TO_SECOND); - assertEquals("-435 00:00:00", toString((IntervalDayTime) new DateTrunc(Source.EMPTY, l("days"), dayToSecond, null) - .makePipe().asProcessor().process(null))); - - dayToSecond = intervalLiteral(Duration.ofDays(-4231).minusHours(23).minusMinutes(45).minusSeconds(55).minusNanos(234000000), - INTERVAL_DAY_TO_SECOND); - assertEquals("-4231 23:00:00", toString((IntervalDayTime) new DateTrunc(Source.EMPTY, l("hh"), dayToSecond, null) - .makePipe().asProcessor().process(null))); - - dayToSecond = intervalLiteral(Duration.ofDays(-124).minusHours(0).minusMinutes(59).minusSeconds(11).minusNanos(564000000), - INTERVAL_DAY_TO_SECOND); - assertEquals("-124 00:59:00", toString((IntervalDayTime) new DateTrunc(Source.EMPTY, l("mi"), dayToSecond, null) - .makePipe().asProcessor().process(null))); - - dayToSecond = intervalLiteral(Duration.ofDays(-534).minusHours(23).minusMinutes(59).minusSeconds(59).minusNanos(245000000), - INTERVAL_DAY_TO_SECOND); - assertEquals("-534 23:59:59", toString((IntervalDayTime) new DateTrunc(Source.EMPTY, l("seconds"), dayToSecond, null) - .makePipe().asProcessor().process(null))); - - dayToSecond = intervalLiteral(Duration.ofDays(-127).minusHours(17).minusMinutes(59).minusSeconds(59).minusNanos(987654321), - INTERVAL_DAY_TO_SECOND); - assertEquals("-127 17:59:59.987", toString((IntervalDayTime) new DateTrunc(Source.EMPTY, l("ms"), dayToSecond, null) - .makePipe().asProcessor().process(null))); + assertEquals( + "-24320-0", + toString( + (IntervalYearMonth) new DateTrunc(Source.EMPTY, l("decades"), yearToMonth, null).makePipe().asProcessor().process(null) + ) + ); + + Literal dayToSecond = intervalLiteral( + Duration.ofDays(-435).minusHours(23).minusMinutes(45).minusSeconds(55).minusNanos(123000000), + INTERVAL_DAY_TO_SECOND + ); + assertEquals( + "-435 00:00:00", + toString((IntervalDayTime) new DateTrunc(Source.EMPTY, l("days"), dayToSecond, null).makePipe().asProcessor().process(null)) + ); + + dayToSecond = intervalLiteral( + Duration.ofDays(-4231).minusHours(23).minusMinutes(45).minusSeconds(55).minusNanos(234000000), + INTERVAL_DAY_TO_SECOND + ); + assertEquals( + "-4231 23:00:00", + toString((IntervalDayTime) new DateTrunc(Source.EMPTY, l("hh"), dayToSecond, null).makePipe().asProcessor().process(null)) + ); + + dayToSecond = intervalLiteral( + Duration.ofDays(-124).minusHours(0).minusMinutes(59).minusSeconds(11).minusNanos(564000000), + INTERVAL_DAY_TO_SECOND + ); + assertEquals( + "-124 00:59:00", + toString((IntervalDayTime) new DateTrunc(Source.EMPTY, l("mi"), dayToSecond, null).makePipe().asProcessor().process(null)) + ); + + dayToSecond = intervalLiteral( + Duration.ofDays(-534).minusHours(23).minusMinutes(59).minusSeconds(59).minusNanos(245000000), + INTERVAL_DAY_TO_SECOND + ); + assertEquals( + "-534 23:59:59", + toString((IntervalDayTime) new DateTrunc(Source.EMPTY, l("seconds"), dayToSecond, null).makePipe().asProcessor().process(null)) + ); + + dayToSecond = intervalLiteral( + Duration.ofDays(-127).minusHours(17).minusMinutes(59).minusSeconds(59).minusNanos(987654321), + INTERVAL_DAY_TO_SECOND + ); + assertEquals( + "-127 17:59:59.987", + toString((IntervalDayTime) new DateTrunc(Source.EMPTY, l("ms"), dayToSecond, null).makePipe().asProcessor().process(null)) + ); } + private String toString(IntervalYearMonth intervalYearMonth) { return StringUtils.toString(intervalYearMonth); } @@ -308,12 +430,14 @@ private String toString(IntervalYearMonth intervalYearMonth) { private String toString(IntervalDayTime intervalDayTime) { return StringUtils.toString(intervalDayTime); } + private String toString(ZonedDateTime dateTime) { return ISO_DATETIME_WITH_NANOS.format(dateTime); } private static Literal intervalLiteral(TemporalAmount value, DataType intervalType) { - Object interval = value instanceof Period ? new IntervalYearMonth((Period) value, intervalType) + Object interval = value instanceof Period + ? new IntervalYearMonth((Period) value, intervalType) : new IntervalDayTime((Duration) value, intervalType); return new Literal(EMPTY, interval, SqlDataTypes.fromJava(interval)); } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/NamedDateTimeProcessorTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/NamedDateTimeProcessorTests.java index b2f99992e6006..5e5452dbfbcdb 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/NamedDateTimeProcessorTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/NamedDateTimeProcessorTests.java @@ -6,9 +6,9 @@ */ package org.elasticsearch.xpack.sql.expression.function.scalar.datetime; -import org.elasticsearch.jdk.JavaVersion; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.Writeable.Reader; +import org.elasticsearch.jdk.JavaVersion; import org.elasticsearch.xpack.sql.AbstractSqlWireSerializingTestCase; import org.elasticsearch.xpack.sql.expression.function.scalar.datetime.NamedDateTimeProcessor.NameExtractor; import org.junit.Assume; @@ -75,7 +75,7 @@ public void testValidDayNamesWithNonUTCTimeZone() { public void testValidMonthNamesInUTC() { assumeJava9PlusAndCompatLocaleProviderSetting(); - NamedDateTimeProcessor proc = new NamedDateTimeProcessor(NameExtractor.MONTH_NAME, UTC); + NamedDateTimeProcessor proc = new NamedDateTimeProcessor(NameExtractor.MONTH_NAME, UTC); assertEquals("January", proc.process(dateTime(0))); assertEquals("September", proc.process(dateTime(-64165813612338L))); assertEquals("April", proc.process(dateTime(64164233612338L))); @@ -115,7 +115,7 @@ private void assumeJava9PlusAndCompatLocaleProviderSetting() { String beforeJava9CompatibleLocale = System.getProperty("java.locale.providers"); // and COMPAT setting needs to be first on the list boolean isBeforeJava9Compatible = beforeJava9CompatibleLocale != null - && Strings.tokenizeToStringArray(beforeJava9CompatibleLocale, ",")[0].equals("COMPAT"); + && Strings.tokenizeToStringArray(beforeJava9CompatibleLocale, ",")[0].equals("COMPAT"); Assume.assumeTrue(isBeforeJava9Compatible); } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/NonIsoDateTimeProcessorTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/NonIsoDateTimeProcessorTests.java index 280159896e404..953a0709467b6 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/NonIsoDateTimeProcessorTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/NonIsoDateTimeProcessorTests.java @@ -47,15 +47,15 @@ public void testNonISOWeekOfYearInUTC() { NonIsoDateTimeProcessor proc = new NonIsoDateTimeProcessor(NonIsoDateTimeExtractor.WEEK_OF_YEAR, UTC); // 1 Jan 1988 is Friday - under Sunday,1 rule it is the first week of the year (under ISO rule it would be 53 of the previous year // hence the 5th Jan 1988 Tuesday is the second week of a year - assertEquals(2, proc.process(dateTime(568372930000L))); //1988-01-05T09:22:10Z[UTC] - assertEquals(6, proc.process(dateTime(981278530000L))); //2001-02-04T09:22:10Z[UTC] - assertEquals(7, proc.process(dateTime(224241730000L))); //1977-02-08T09:22:10Z[UTC] + assertEquals(2, proc.process(dateTime(568372930000L))); // 1988-01-05T09:22:10Z[UTC] + assertEquals(6, proc.process(dateTime(981278530000L))); // 2001-02-04T09:22:10Z[UTC] + assertEquals(7, proc.process(dateTime(224241730000L))); // 1977-02-08T09:22:10Z[UTC] - assertEquals(12, proc.process(dateTime(132744130000L))); //1974-03-17T09:22:10Z[UTC] - assertEquals(17, proc.process(dateTime(230376130000L))); //1977-04-20T09:22:10Z[UTC] - assertEquals(17, proc.process(dateTime(766833730000L))); //1994-04-20T09:22:10Z[UTC] - assertEquals(29, proc.process(dateTime(79780930000L))); //1972-07-12T09:22:10Z[UTC] - assertEquals(33, proc.process(dateTime(902913730000L))); //1998-08-12T09:22:10Z[UTC] + assertEquals(12, proc.process(dateTime(132744130000L))); // 1974-03-17T09:22:10Z[UTC] + assertEquals(17, proc.process(dateTime(230376130000L))); // 1977-04-20T09:22:10Z[UTC] + assertEquals(17, proc.process(dateTime(766833730000L))); // 1994-04-20T09:22:10Z[UTC] + assertEquals(29, proc.process(dateTime(79780930000L))); // 1972-07-12T09:22:10Z[UTC] + assertEquals(33, proc.process(dateTime(902913730000L))); // 1998-08-12T09:22:10Z[UTC] // Tested against MS-SQL Server and H2 assertEquals(2, proc.process(dateTime(1988, 1, 5, 0, 0, 0, 0))); @@ -117,15 +117,15 @@ public void testNonISOWeekOfYearInNonUTCTimeZone() { public void testNonISODayOfWeekInUTC() { NonIsoDateTimeProcessor proc = new NonIsoDateTimeProcessor(NonIsoDateTimeExtractor.DAY_OF_WEEK, UTC); - assertEquals(3, proc.process(dateTime(568372930000L))); //1988-01-05T09:22:10Z[UTC] - assertEquals(1, proc.process(dateTime(981278530000L))); //2001-02-04T09:22:10Z[UTC] - assertEquals(3, proc.process(dateTime(224241730000L))); //1977-02-08T09:22:10Z[UTC] - - assertEquals(1, proc.process(dateTime(132744130000L))); //1974-03-17T09:22:10Z[UTC] - assertEquals(4, proc.process(dateTime(230376130000L))); //1977-04-20T09:22:10Z[UTC] - assertEquals(4, proc.process(dateTime(766833730000L))); //1994-04-20T09:22:10Z[UTC] - assertEquals(7, proc.process(dateTime(333451330000L))); //1980-07-26T09:22:10Z[UTC] - assertEquals(6, proc.process(dateTime(874660930000L))); //1997-09-19T09:22:10Z[UTC] + assertEquals(3, proc.process(dateTime(568372930000L))); // 1988-01-05T09:22:10Z[UTC] + assertEquals(1, proc.process(dateTime(981278530000L))); // 2001-02-04T09:22:10Z[UTC] + assertEquals(3, proc.process(dateTime(224241730000L))); // 1977-02-08T09:22:10Z[UTC] + + assertEquals(1, proc.process(dateTime(132744130000L))); // 1974-03-17T09:22:10Z[UTC] + assertEquals(4, proc.process(dateTime(230376130000L))); // 1977-04-20T09:22:10Z[UTC] + assertEquals(4, proc.process(dateTime(766833730000L))); // 1994-04-20T09:22:10Z[UTC] + assertEquals(7, proc.process(dateTime(333451330000L))); // 1980-07-26T09:22:10Z[UTC] + assertEquals(6, proc.process(dateTime(874660930000L))); // 1997-09-19T09:22:10Z[UTC] } public void testNonISODayOfWeekInNonUTCTimeZone() { diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/TimeProcessorTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/TimeProcessorTests.java index bbd8bdbbf9b96..76d271f47a356 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/TimeProcessorTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/TimeProcessorTests.java @@ -77,7 +77,8 @@ public void testApply_withTimeZoneOtherThanUTC() { proc = new TimeProcessor(DateTimeExtractor.HOUR_OF_DAY, zoneId); assertEquals(10, proc.process(time(0L))); - assertEquals(20, proc.process(time(10, 20, 30, 123456789)));; + assertEquals(20, proc.process(time(10, 20, 30, 123456789))); + ; assertEquals(4, proc.process(time(18, 20, 30, 123456789))); } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/ToCharTestScript.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/ToCharTestScript.java index 3c60c1330c13a..6d6f8b3dcc22b 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/ToCharTestScript.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/ToCharTestScript.java @@ -9,9 +9,9 @@ import com.carrotsearch.randomizedtesting.generators.RandomNumbers; -import org.elasticsearch.core.SuppressForbidden; -import org.elasticsearch.core.PathUtils; import org.elasticsearch.common.util.set.Sets; +import org.elasticsearch.core.PathUtils; +import org.elasticsearch.core.SuppressForbidden; import java.math.BigDecimal; import java.net.URI; @@ -64,8 +64,9 @@ private class TestRecord { private static final List FILL_MODIFIERS = asList("FM", "fm", ""); private static final List ORDINAL_SUFFIX_MODIFIERS = asList("TH", "th", ""); // timezones that are valid both in Java and in Postgres - public static final List TIMEZONES_TO_TEST = - readAllLinesWithoutComment(ToCharTestScript.class.getResource("tochar-test-timezones.txt")); + public static final List TIMEZONES_TO_TEST = readAllLinesWithoutComment( + ToCharTestScript.class.getResource("tochar-test-timezones.txt") + ); private final List testRecords = new ArrayList<>(); private final List testEpochSeconds = new ArrayList<>(); @@ -97,21 +98,27 @@ private void generateTestTimestamps() { private void patternsOneByOne() { for (String pattern : MATCHABLE_PATTERNS) { - testRecords.add(new TestRecord( - randomFromCollection(testEpochSeconds), - NOT_FULLY_MATCHABLE_PATTERNS.contains(pattern) ? - pattern : - String.join(PATTERN_DELIMITER, pattern, FILL_MODIFIERS.get(0) + pattern + ORDINAL_SUFFIX_MODIFIERS.get(0)))); + testRecords.add( + new TestRecord( + randomFromCollection(testEpochSeconds), + NOT_FULLY_MATCHABLE_PATTERNS.contains(pattern) + ? pattern + : String.join(PATTERN_DELIMITER, pattern, FILL_MODIFIERS.get(0) + pattern + ORDINAL_SUFFIX_MODIFIERS.get(0)) + ) + ); } } private void allPatternsTogether() { for (BigDecimal es : testEpochSeconds) { - testRecords.add(new TestRecord( - es, - IntStream.range(0, MATCHABLE_PATTERNS.size()) - .mapToObj(idx -> idx + ":" + patternWithRandomModifiers(MATCHABLE_PATTERNS.get(idx))) - .collect(Collectors.joining(PATTERN_DELIMITER)))); + testRecords.add( + new TestRecord( + es, + IntStream.range(0, MATCHABLE_PATTERNS.size()) + .mapToObj(idx -> idx + ":" + patternWithRandomModifiers(MATCHABLE_PATTERNS.get(idx))) + .collect(Collectors.joining(PATTERN_DELIMITER)) + ) + ); } } @@ -127,9 +134,7 @@ private void postgreSQLPatternParsingBehaviour() { private void monthsAsRomanNumbers() { for (int i = 1; i <= 12; i++) { - testRecords.add(new TestRecord( - new BigDecimal(dateTime(0).withMonth(i).toEpochSecond()), - random.nextBoolean() ? "RM" : "rm")); + testRecords.add(new TestRecord(new BigDecimal(dateTime(0).withMonth(i).toEpochSecond()), random.nextBoolean() ? "RM" : "rm")); } } @@ -140,20 +145,19 @@ private void randomizedPatternStrings() { final int lengthOfRandomizedPattern = 50; final int pctChanceOfRandomCharacter = 80; for (int i = 0; i < randomizedPatternCount; i++) { - String patternWithLiterals = IntStream.rangeClosed(1, lengthOfRandomizedPattern) - .mapToObj(idx -> { - if (random.nextInt(100) < pctChanceOfRandomCharacter) { - return randomCharacters.substring(random.nextInt(randomCharacters.length())).substring(0, 1); - } else { - return (randomFromCollection(FILL_MODIFIERS) + randomFromCollection(PATTERNS) - + randomFromCollection(ORDINAL_SUFFIX_MODIFIERS)); - }}) - .collect(Collectors.joining()); + String patternWithLiterals = IntStream.rangeClosed(1, lengthOfRandomizedPattern).mapToObj(idx -> { + if (random.nextInt(100) < pctChanceOfRandomCharacter) { + return randomCharacters.substring(random.nextInt(randomCharacters.length())).substring(0, 1); + } else { + return (randomFromCollection(FILL_MODIFIERS) + randomFromCollection(PATTERNS) + randomFromCollection( + ORDINAL_SUFFIX_MODIFIERS + )); + } + }).collect(Collectors.joining()); // clean up the random string from the unsupported modifiers for (String unsupportedPatternModifier : Sets.union(UNSUPPORTED_PATTERN_MODIFIERS, NOT_FULLY_MATCHABLE_PATTERNS)) { - patternWithLiterals = patternWithLiterals - .replace(unsupportedPatternModifier, "") + patternWithLiterals = patternWithLiterals.replace(unsupportedPatternModifier, "") .replace(unsupportedPatternModifier.toLowerCase(Locale.ROOT), ""); } testRecords.add(new TestRecord(randomFromCollection(testEpochSeconds), patternWithLiterals)); @@ -161,8 +165,9 @@ private void randomizedPatternStrings() { } private BigDecimal randomSecondsWithFractions(int minYear, int maxYear) { - BigDecimal seconds = - new BigDecimal(RandomNumbers.randomLongBetween(random, (minYear - 1970) * SECONDS_IN_YEAR, (maxYear - 1970) * SECONDS_IN_YEAR)); + BigDecimal seconds = new BigDecimal( + RandomNumbers.randomLongBetween(random, (minYear - 1970) * SECONDS_IN_YEAR, (maxYear - 1970) * SECONDS_IN_YEAR) + ); BigDecimal fractions = new BigDecimal(RandomNumbers.randomIntBetween(random, 0, 999_999)).movePointLeft(6); return seconds.add(fractions); } @@ -207,25 +212,29 @@ static List readAllLinesWithoutComment(URL url) { * and update the test CSV file. */ private String unitTestExporterScript() { - String header = - "\n\\echo #" + - "\n\\echo # DO NOT EDIT manually, was generated using " + ToCharTestScript.class.getName() + - "\n\\echo #\n\n"; + String header = "\n\\echo #" + + "\n\\echo # DO NOT EDIT manually, was generated using " + + ToCharTestScript.class.getName() + + "\n\\echo #\n\n"; String testCases = testRecords.stream().map(tc -> { long seconds = tc.secondsAndFractionsSinceEpoch.longValue(); BigDecimal fractions = tc.secondsAndFractionsSinceEpoch.remainder(BigDecimal.ONE).movePointRight(6); - return String.format(Locale.ROOT, + return String.format( + Locale.ROOT, "SET TIME ZONE '%6$s';\n" + "\\copy (SELECT %1$s as epoch_seconds_and_microsends, '%5$s' as zone_id, '%4$s' as format_string, " + "(TO_TIMESTAMP(%2$d) + INTERVAL '%3$d microseconds') as to_timestamp_result, " + "TO_CHAR((TO_TIMESTAMP(%2$d) + INTERVAL '%3$d microseconds'), '%4$s') as to_char_result) to stdout " - + "with DELIMITER as '" + DELIMITER + "' NULL as '' csv \n", + + "with DELIMITER as '" + + DELIMITER + + "' NULL as '' csv \n", tc.secondsAndFractionsSinceEpoch.toPlainString(), seconds, fractions.intValue(), tc.formatString, tc.zoneId, - adjustZoneIdToPostgres(tc.zoneId)); + adjustZoneIdToPostgres(tc.zoneId) + ); }).collect(Collectors.joining("\n")); return header + testCases; } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/GeoProcessorTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/GeoProcessorTests.java index 5ce6cf0ab7f5f..ddd3fc268f6b5 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/GeoProcessorTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/GeoProcessorTests.java @@ -44,18 +44,25 @@ public void testApplyGeometryType() throws Exception { assertEquals("POINT", new GeoProcessor(GeoOperation.GEOMETRY_TYPE).process(new GeoShape("POINT (10 20)"))); assertEquals("MULTIPOINT", new GeoProcessor(GeoOperation.GEOMETRY_TYPE).process(new GeoShape("multipoint (2.0 1.0)"))); assertEquals("LINESTRING", new GeoProcessor(GeoOperation.GEOMETRY_TYPE).process(new GeoShape("LINESTRING (3.0 1.0, 4.0 2.0)"))); - assertEquals("POLYGON", new GeoProcessor(GeoOperation.GEOMETRY_TYPE).process( - new GeoShape("polygon ((3.0 1.0, 4.0 2.0, 4.0 3.0, 3.0 1.0))"))); - assertEquals("MULTILINESTRING", new GeoProcessor(GeoOperation.GEOMETRY_TYPE).process( - new GeoShape("multilinestring ((3.0 1.0, 4.0 2.0), (2.0 1.0, 5.0 6.0))"))); - assertEquals("MULTIPOLYGON", new GeoProcessor(GeoOperation.GEOMETRY_TYPE).process( - new GeoShape("multipolygon (((3.0 1.0, 4.0 2.0, 4.0 3.0, 3.0 1.0)))"))); + assertEquals( + "POLYGON", + new GeoProcessor(GeoOperation.GEOMETRY_TYPE).process(new GeoShape("polygon ((3.0 1.0, 4.0 2.0, 4.0 3.0, 3.0 1.0))")) + ); + assertEquals( + "MULTILINESTRING", + new GeoProcessor(GeoOperation.GEOMETRY_TYPE).process(new GeoShape("multilinestring ((3.0 1.0, 4.0 2.0), (2.0 1.0, 5.0 6.0))")) + ); + assertEquals( + "MULTIPOLYGON", + new GeoProcessor(GeoOperation.GEOMETRY_TYPE).process(new GeoShape("multipolygon (((3.0 1.0, 4.0 2.0, 4.0 3.0, 3.0 1.0)))")) + ); assertEquals("ENVELOPE", new GeoProcessor(GeoOperation.GEOMETRY_TYPE).process(new GeoShape("bbox (10.0, 20.0, 40.0, 30.0)"))); - assertEquals("GEOMETRYCOLLECTION", new GeoProcessor(GeoOperation.GEOMETRY_TYPE).process( - new GeoShape("geometrycollection (point (20.0 10.0),point (1.0 2.0))"))); + assertEquals( + "GEOMETRYCOLLECTION", + new GeoProcessor(GeoOperation.GEOMETRY_TYPE).process(new GeoShape("geometrycollection (point (20.0 10.0),point (1.0 2.0))")) + ); } - public void testApplyGetXYZ() throws Exception { assertEquals(10.0, new GeoProcessor(GeoOperation.X).process(new GeoShape(10, 20))); assertEquals(20.0, new GeoProcessor(GeoOperation.Y).process(new GeoShape(10, 20))); @@ -69,26 +76,36 @@ public void testApplyGetXYZ() throws Exception { assertEquals(1.0, new GeoProcessor(GeoOperation.Y).process(new GeoShape("multipoint (2.0 1.0)"))); assertEquals(3.0, new GeoProcessor(GeoOperation.X).process(new GeoShape("LINESTRING (3.0 1.0, 4.0 2.0)"))); assertEquals(1.0, new GeoProcessor(GeoOperation.Y).process(new GeoShape("LINESTRING (3.0 1.0, 4.0 2.0)"))); - assertEquals(3.0, new GeoProcessor(GeoOperation.X).process( - new GeoShape("multilinestring ((3.0 1.0, 4.0 2.0), (2.0 1.0, 5.0 6.0))"))); - assertEquals(1.0, new GeoProcessor(GeoOperation.Y).process( - new GeoShape("multilinestring ((3.0 1.0, 4.0 2.0), (2.0 1.0, 5.0 6.0))"))); - // minX minX, maxX, maxY, minY + assertEquals( + 3.0, + new GeoProcessor(GeoOperation.X).process(new GeoShape("multilinestring ((3.0 1.0, 4.0 2.0), (2.0 1.0, 5.0 6.0))")) + ); + assertEquals( + 1.0, + new GeoProcessor(GeoOperation.Y).process(new GeoShape("multilinestring ((3.0 1.0, 4.0 2.0), (2.0 1.0, 5.0 6.0))")) + ); + // minX minX, maxX, maxY, minY assertEquals(10.0, new GeoProcessor(GeoOperation.X).process(new GeoShape("bbox (10.0, 20.0, 40.0, 30.0)"))); - // minY minX, maxX, maxY, minY + // minY minX, maxX, maxY, minY assertEquals(30.0, new GeoProcessor(GeoOperation.Y).process(new GeoShape("bbox (10.0, 20.0, 40.0, 30.0)"))); - assertEquals(20.0, new GeoProcessor(GeoOperation.X).process( - new GeoShape("geometrycollection (point (20.0 10.0),point (1.0 2.0))"))); - assertEquals(10.0, new GeoProcessor(GeoOperation.Y).process( - new GeoShape("geometrycollection (point (20.0 10.0),point (1.0 2.0))"))); + assertEquals( + 20.0, + new GeoProcessor(GeoOperation.X).process(new GeoShape("geometrycollection (point (20.0 10.0),point (1.0 2.0))")) + ); + assertEquals( + 10.0, + new GeoProcessor(GeoOperation.Y).process(new GeoShape("geometrycollection (point (20.0 10.0),point (1.0 2.0))")) + ); } public void testApplyGetXYZToPolygons() throws Exception { assertEquals(3.0, new GeoProcessor(GeoOperation.X).process(new GeoShape("polygon ((3.0 1.0, 4.0 2.0, 4.0 3.0, 3.0 1.0))"))); assertEquals(1.0, new GeoProcessor(GeoOperation.Y).process(new GeoShape("polygon ((3.0 1.0, 4.0 2.0, 4.0 3.0, 3.0 1.0))"))); assertNull(new GeoProcessor(GeoOperation.Z).process(new GeoShape("polygon ((3.0 1.0, 4.0 2.0, 4.0 3.0, 3.0 1.0))"))); - assertEquals(5.0, new GeoProcessor(GeoOperation.Z).process( - new GeoShape("polygon ((3.0 1.0 5.0, 4.0 2.0 6.0, 4.0 3.0 7.0, 3.0 1.0 5.0))"))); + assertEquals( + 5.0, + new GeoProcessor(GeoOperation.Z).process(new GeoShape("polygon ((3.0 1.0 5.0, 4.0 2.0 6.0, 4.0 3.0 7.0, 3.0 1.0 5.0))")) + ); assertEquals(3.0, new GeoProcessor(GeoOperation.X).process(new GeoShape("multipolygon (((3.0 1.0, 4.0 2.0, 4.0 3.0, 3.0 1.0)))"))); assertEquals(1.0, new GeoProcessor(GeoOperation.Y).process(new GeoShape("multipolygon (((3.0 1.0, 4.0 2.0, 4.0 3.0, 3.0 1.0)))"))); } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StDistanceProcessorTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StDistanceProcessorTests.java index 2751e3536880f..2ff801e244969 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StDistanceProcessorTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StDistanceProcessorTests.java @@ -58,12 +58,16 @@ public void testNullHandling() { } public void testTypeCheck() { - SqlIllegalArgumentException siae = expectThrows(SqlIllegalArgumentException.class, - () -> new StDistance(EMPTY, l("foo"), l(new GeoShape(1, 2))).makePipe().asProcessor().process(null)); + SqlIllegalArgumentException siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new StDistance(EMPTY, l("foo"), l(new GeoShape(1, 2))).makePipe().asProcessor().process(null) + ); assertEquals("A geo_point or geo_shape with type point is required; received [foo]", siae.getMessage()); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new StDistance(EMPTY, l(new GeoShape(1, 2)), l("bar")).makePipe().asProcessor().process(null)); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new StDistance(EMPTY, l(new GeoShape(1, 2)), l("bar")).makePipe().asProcessor().process(null) + ); assertEquals("A geo_point or geo_shape with type point is required; received [bar]", siae.getMessage()); } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StWkttosqlProcessorTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StWkttosqlProcessorTests.java index 4b075add25a00..5eb87ae736e88 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StWkttosqlProcessorTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/geo/StWkttosqlProcessorTests.java @@ -37,7 +37,6 @@ public void testTypeCheck() { siae = expectThrows(QlIllegalArgumentException.class, () -> procPoint.process("point (foo bar)")); assertEquals("Cannot parse [point (foo bar)] as a geo_shape or shape value", siae.getMessage()); - siae = expectThrows(QlIllegalArgumentException.class, () -> procPoint.process("point (10 10")); assertEquals("Cannot parse [point (10 10] as a geo_shape or shape value", siae.getMessage()); } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/math/BinaryMathProcessorTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/math/BinaryMathProcessorTests.java index 6749710a07c28..40aa46c4e90c0 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/math/BinaryMathProcessorTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/math/BinaryMathProcessorTests.java @@ -21,9 +21,10 @@ public class BinaryMathProcessorTests extends AbstractWireSerializingTestCase { public static BinaryMathProcessor randomProcessor() { return new BinaryMathProcessor( - new ConstantProcessor(randomLong()), - new ConstantProcessor(randomLong()), - randomFrom(BinaryMathProcessor.BinaryMathOperation.values())); + new ConstantProcessor(randomLong()), + new ConstantProcessor(randomLong()), + randomFrom(BinaryMathProcessor.BinaryMathOperation.values()) + ); } @Override @@ -65,20 +66,25 @@ public void testRoundWithValidInput() { public void testRoundFunctionWithEdgeCasesInputs() { assertNull(new Round(EMPTY, l(null), l(3)).makePipe().asProcessor().process(null)); assertEquals(-0.0, new Round(EMPTY, l(0), l(0)).makePipe().asProcessor().process(null)); - assertEquals((double) Long.MAX_VALUE, new Round(EMPTY, l(Long.MAX_VALUE), l(0)) - .makePipe().asProcessor().process(null)); + assertEquals((double) Long.MAX_VALUE, new Round(EMPTY, l(Long.MAX_VALUE), l(0)).makePipe().asProcessor().process(null)); assertEquals(0.0, new Round(EMPTY, l(123.456), l(Integer.MAX_VALUE)).makePipe().asProcessor().process(null)); } public void testRoundInputValidation() { - SqlIllegalArgumentException siae = expectThrows(SqlIllegalArgumentException.class, - () -> new Round(EMPTY, l(5), l("foobarbar")).makePipe().asProcessor().process(null)); + SqlIllegalArgumentException siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Round(EMPTY, l(5), l("foobarbar")).makePipe().asProcessor().process(null) + ); assertEquals("A number is required; received [foobarbar]", siae.getMessage()); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new Round(EMPTY, l("bla"), l(0)).makePipe().asProcessor().process(null)); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Round(EMPTY, l("bla"), l(0)).makePipe().asProcessor().process(null) + ); assertEquals("A number is required; received [bla]", siae.getMessage()); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new Round(EMPTY, l(123.34), l(0.1)).makePipe().asProcessor().process(null)); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Round(EMPTY, l(123.34), l(0.1)).makePipe().asProcessor().process(null) + ); assertEquals("An integer number is required; received [0.1] as second parameter", siae.getMessage()); } @@ -96,21 +102,25 @@ public void testTruncateWithValidInput() { public void testTruncateFunctionWithEdgeCasesInputs() { assertNull(new Truncate(EMPTY, l(null), l(3)).makePipe().asProcessor().process(null)); assertEquals(0.0, new Truncate(EMPTY, l(0), l(0)).makePipe().asProcessor().process(null)); - assertEquals((double) Long.MAX_VALUE, new Truncate(EMPTY, l(Long.MAX_VALUE), l(0)) - .makePipe().asProcessor().process(null)); - assertEquals(Double.NaN, new Truncate(EMPTY, l(123.456), l(Integer.MAX_VALUE)) - .makePipe().asProcessor().process(null)); + assertEquals((double) Long.MAX_VALUE, new Truncate(EMPTY, l(Long.MAX_VALUE), l(0)).makePipe().asProcessor().process(null)); + assertEquals(Double.NaN, new Truncate(EMPTY, l(123.456), l(Integer.MAX_VALUE)).makePipe().asProcessor().process(null)); } public void testTruncateInputValidation() { - SqlIllegalArgumentException siae = expectThrows(SqlIllegalArgumentException.class, - () -> new Truncate(EMPTY, l(5), l("foobarbar")).makePipe().asProcessor().process(null)); + SqlIllegalArgumentException siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Truncate(EMPTY, l(5), l("foobarbar")).makePipe().asProcessor().process(null) + ); assertEquals("A number is required; received [foobarbar]", siae.getMessage()); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new Truncate(EMPTY, l("bla"), l(0)).makePipe().asProcessor().process(null)); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Truncate(EMPTY, l("bla"), l(0)).makePipe().asProcessor().process(null) + ); assertEquals("A number is required; received [bla]", siae.getMessage()); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new Truncate(EMPTY, l(123.34), l(0.1)).makePipe().asProcessor().process(null)); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Truncate(EMPTY, l(123.34), l(0.1)).makePipe().asProcessor().process(null) + ); assertEquals("An integer number is required; received [0.1] as second parameter", siae.getMessage()); } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/BinaryStringNumericPipeTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/BinaryStringNumericPipeTests.java index 5b48c59bf0f71..b3092f1020c90 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/BinaryStringNumericPipeTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/BinaryStringNumericPipeTests.java @@ -24,8 +24,7 @@ import static org.elasticsearch.xpack.ql.expression.function.scalar.FunctionTestUtils.randomStringLiteral; import static org.elasticsearch.xpack.ql.tree.SourceTests.randomSource; -public class BinaryStringNumericPipeTests - extends AbstractNodeTestCase { +public class BinaryStringNumericPipeTests extends AbstractNodeTestCase { @Override protected BinaryStringNumericPipe randomInstance() { @@ -56,35 +55,21 @@ public void testTransform() { BinaryStringNumericPipe b1 = randomInstance(); Expression newExpression = randomValueOtherThan(b1.expression(), () -> randomBinaryStringNumericExpression()); - BinaryStringNumericPipe newB = new BinaryStringNumericPipe( - b1.source(), - newExpression, - b1.left(), - b1.right(), - b1.operation()); + BinaryStringNumericPipe newB = new BinaryStringNumericPipe(b1.source(), newExpression, b1.left(), b1.right(), b1.operation()); assertEquals(newB, b1.transformPropertiesOnly(Expression.class, v -> Objects.equals(v, b1.expression()) ? newExpression : v)); BinaryStringNumericPipe b2 = randomInstance(); BinaryStringNumericOperation newOp = randomValueOtherThan(b2.operation(), () -> randomBinaryStringNumericOperation()); - newB = new BinaryStringNumericPipe( - b2.source(), - b2.expression(), - b2.left(), - b2.right(), - newOp); - assertEquals(newB, - b2.transformPropertiesOnly(BinaryStringNumericOperation.class, v -> Objects.equals(v, b2.operation()) ? newOp : v)); + newB = new BinaryStringNumericPipe(b2.source(), b2.expression(), b2.left(), b2.right(), newOp); + assertEquals( + newB, + b2.transformPropertiesOnly(BinaryStringNumericOperation.class, v -> Objects.equals(v, b2.operation()) ? newOp : v) + ); BinaryStringNumericPipe b3 = randomInstance(); Source newLoc = randomValueOtherThan(b3.source(), () -> randomSource()); - newB = new BinaryStringNumericPipe( - newLoc, - b3.expression(), - b3.left(), - b3.right(), - b3.operation()); - assertEquals(newB, - b3.transformPropertiesOnly(Source.class, v -> Objects.equals(v, b3.source()) ? newLoc : v)); + newB = new BinaryStringNumericPipe(newLoc, b3.expression(), b3.left(), b3.right(), b3.operation()); + assertEquals(newB, b3.transformPropertiesOnly(Source.class, v -> Objects.equals(v, b3.source()) ? newLoc : v)); } @Override @@ -92,8 +77,7 @@ public void testReplaceChildren() { BinaryStringNumericPipe b = randomInstance(); Pipe newLeft = pipe(((Expression) randomValueOtherThan(b.left(), () -> randomStringLiteral()))); Pipe newRight = pipe(((Expression) randomValueOtherThan(b.right(), () -> randomIntLiteral()))); - BinaryStringNumericPipe newB = - new BinaryStringNumericPipe(b.source(), b.expression(), b.left(), b.right(), b.operation()); + BinaryStringNumericPipe newB = new BinaryStringNumericPipe(b.source(), b.expression(), b.left(), b.right(), b.operation()); BinaryPipe transformed = newB.replaceChildren(newLeft, b.right()); assertEquals(transformed.left(), newLeft); @@ -117,31 +101,45 @@ public void testReplaceChildren() { @Override protected BinaryStringNumericPipe mutate(BinaryStringNumericPipe instance) { List> randoms = new ArrayList<>(); - randoms.add(f -> new BinaryStringNumericPipe(f.source(), + randoms.add( + f -> new BinaryStringNumericPipe( + f.source(), f.expression(), pipe(((Expression) randomValueOtherThan(f.left(), () -> randomStringLiteral()))), f.right(), - f.operation())); - randoms.add(f -> new BinaryStringNumericPipe(f.source(), + f.operation() + ) + ); + randoms.add( + f -> new BinaryStringNumericPipe( + f.source(), f.expression(), f.left(), pipe(((Expression) randomValueOtherThan(f.right(), () -> randomIntLiteral()))), - f.operation())); - randoms.add(f -> new BinaryStringNumericPipe(f.source(), + f.operation() + ) + ); + randoms.add( + f -> new BinaryStringNumericPipe( + f.source(), f.expression(), pipe(((Expression) randomValueOtherThan(f.left(), () -> randomStringLiteral()))), pipe(((Expression) randomValueOtherThan(f.right(), () -> randomIntLiteral()))), - f.operation())); + f.operation() + ) + ); return randomFrom(randoms).apply(instance); } @Override protected BinaryStringNumericPipe copy(BinaryStringNumericPipe instance) { - return new BinaryStringNumericPipe(instance.source(), - instance.expression(), - instance.left(), - instance.right(), - instance.operation()); + return new BinaryStringNumericPipe( + instance.source(), + instance.expression(), + instance.left(), + instance.right(), + instance.operation() + ); } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/BinaryStringNumericProcessorTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/BinaryStringNumericProcessorTests.java index 726bfa1b8fc3c..eb8d365c8f17b 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/BinaryStringNumericProcessorTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/BinaryStringNumericProcessorTests.java @@ -23,9 +23,10 @@ public class BinaryStringNumericProcessorTests extends AbstractWireSerializingTe @Override protected BinaryStringNumericProcessor createTestInstance() { return new BinaryStringNumericProcessor( - new ConstantProcessor(randomRealisticUnicodeOfLengthBetween(1, 128)), - new ConstantProcessor(randomInt(256)), - randomFrom(BinaryStringNumericOperation.values())); + new ConstantProcessor(randomRealisticUnicodeOfLengthBetween(1, 128)), + new ConstantProcessor(randomInt(256)), + randomFrom(BinaryStringNumericOperation.values()) + ); } @Override @@ -56,24 +57,34 @@ public void testLeftFunctionWithEdgeCases() { } public void testLeftFunctionInputValidation() { - SqlIllegalArgumentException siae = expectThrows(SqlIllegalArgumentException.class, - () -> new Left(EMPTY, l(5), l(3)).makePipe().asProcessor().process(null)); + SqlIllegalArgumentException siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Left(EMPTY, l(5), l(3)).makePipe().asProcessor().process(null) + ); assertEquals("A string/char is required; received [5]", siae.getMessage()); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new Left(EMPTY, l("foo bar"), l("baz")).makePipe().asProcessor().process(null)); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Left(EMPTY, l("foo bar"), l("baz")).makePipe().asProcessor().process(null) + ); assertEquals("A fixed point number is required for [count]; received [java.lang.String]", siae.getMessage()); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new Left(EMPTY, l("foo"), l((long)Integer.MIN_VALUE - 1)).makePipe().asProcessor().process(null)); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Left(EMPTY, l("foo"), l((long) Integer.MIN_VALUE - 1)).makePipe().asProcessor().process(null) + ); assertEquals("[count] out of the allowed range [-2147483648, 2147483647], received [-2147483649]", siae.getMessage()); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new Left(EMPTY, l("foo"), l((long)Integer.MAX_VALUE + 1)).makePipe().asProcessor().process(null)); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Left(EMPTY, l("foo"), l((long) Integer.MAX_VALUE + 1)).makePipe().asProcessor().process(null) + ); assertEquals("[count] out of the allowed range [-2147483648, 2147483647], received [2147483648]", siae.getMessage()); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new Left(EMPTY, l("foo"), l(1.0)).makePipe().asProcessor().process(null)); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Left(EMPTY, l("foo"), l(1.0)).makePipe().asProcessor().process(null) + ); assertEquals("A fixed point number is required for [count]; received [java.lang.Double]", siae.getMessage()); } @@ -95,24 +106,34 @@ public void testRightFunctionWithEdgeCases() { } public void testRightFunctionInputValidation() { - SqlIllegalArgumentException siae = expectThrows(SqlIllegalArgumentException.class, - () -> new Right(EMPTY, l(5), l(3)).makePipe().asProcessor().process(null)); + SqlIllegalArgumentException siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Right(EMPTY, l(5), l(3)).makePipe().asProcessor().process(null) + ); assertEquals("A string/char is required; received [5]", siae.getMessage()); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new Right(EMPTY, l("foo bar"), l("baz")).makePipe().asProcessor().process(null)); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Right(EMPTY, l("foo bar"), l("baz")).makePipe().asProcessor().process(null) + ); assertEquals("A fixed point number is required for [count]; received [java.lang.String]", siae.getMessage()); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new Right(EMPTY, l("foo"), l((long)Integer.MIN_VALUE - 1)).makePipe().asProcessor().process(null)); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Right(EMPTY, l("foo"), l((long) Integer.MIN_VALUE - 1)).makePipe().asProcessor().process(null) + ); assertEquals("[count] out of the allowed range [-2147483648, 2147483647], received [-2147483649]", siae.getMessage()); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new Right(EMPTY, l("foo"), l((long)Integer.MAX_VALUE + 1)).makePipe().asProcessor().process(null)); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Right(EMPTY, l("foo"), l((long) Integer.MAX_VALUE + 1)).makePipe().asProcessor().process(null) + ); assertEquals("[count] out of the allowed range [-2147483648, 2147483647], received [2147483648]", siae.getMessage()); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new Right(EMPTY, l("foo"), l(1.0)).makePipe().asProcessor().process(null)); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Right(EMPTY, l("foo"), l(1.0)).makePipe().asProcessor().process(null) + ); assertEquals("A fixed point number is required for [count]; received [java.lang.Double]", siae.getMessage()); } @@ -132,24 +153,34 @@ public void testRepeatFunctionWithEdgeCases() { } public void testRepeatFunctionInputsValidation() { - SqlIllegalArgumentException siae = expectThrows(SqlIllegalArgumentException.class, - () -> new Repeat(EMPTY, l(5), l(3)).makePipe().asProcessor().process(null)); + SqlIllegalArgumentException siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Repeat(EMPTY, l(5), l(3)).makePipe().asProcessor().process(null) + ); assertEquals("A string/char is required; received [5]", siae.getMessage()); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new Repeat(EMPTY, l("foo bar"), l("baz")).makePipe().asProcessor().process(null)); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Repeat(EMPTY, l("foo bar"), l("baz")).makePipe().asProcessor().process(null) + ); assertEquals("A fixed point number is required for [count]; received [java.lang.String]", siae.getMessage()); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new Repeat(EMPTY, l("foo"), l((long)Integer.MIN_VALUE - 1)).makePipe().asProcessor().process(null)); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Repeat(EMPTY, l("foo"), l((long) Integer.MIN_VALUE - 1)).makePipe().asProcessor().process(null) + ); assertEquals("[count] out of the allowed range [-2147483648, 2147483647], received [-2147483649]", siae.getMessage()); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new Repeat(EMPTY, l("foo"), l((long)Integer.MAX_VALUE + 1)).makePipe().asProcessor().process(null)); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Repeat(EMPTY, l("foo"), l((long) Integer.MAX_VALUE + 1)).makePipe().asProcessor().process(null) + ); assertEquals("[count] out of the allowed range [-2147483648, 2147483647], received [2147483648]", siae.getMessage()); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new Repeat(EMPTY, l("foo"), l(1.0)).makePipe().asProcessor().process(null)); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Repeat(EMPTY, l("foo"), l(1.0)).makePipe().asProcessor().process(null) + ); assertEquals("A fixed point number is required for [count]; received [java.lang.Double]", siae.getMessage()); } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/BinaryStringStringPipeTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/BinaryStringStringPipeTests.java index 5c2f413ed3dc0..f38a41854bcac 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/BinaryStringStringPipeTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/BinaryStringStringPipeTests.java @@ -22,8 +22,7 @@ import static org.elasticsearch.xpack.ql.expression.function.scalar.FunctionTestUtils.randomStringLiteral; import static org.elasticsearch.xpack.ql.tree.SourceTests.randomSource; -public class BinaryStringStringPipeTests - extends AbstractNodeTestCase { +public class BinaryStringStringPipeTests extends AbstractNodeTestCase { @Override protected BinaryStringStringPipe randomInstance() { @@ -36,11 +35,7 @@ private Expression randomBinaryStringStringExpression() { public static BinaryStringStringPipe randomBinaryStringStringPipe() { List functions = new ArrayList<>(); - functions.add(new Position( - randomSource(), - randomStringLiteral(), - randomStringLiteral() - ).makePipe()); + functions.add(new Position(randomSource(), randomStringLiteral(), randomStringLiteral()).makePipe()); // if we decide to add DIFFERENCE(string,string) in the future, here we'd add it as well return (BinaryStringStringPipe) randomFrom(functions); } @@ -51,24 +46,13 @@ public void testTransform() { // skipping the children (the two parameters of the binary function) which are tested separately BinaryStringStringPipe b1 = randomInstance(); Expression newExpression = randomValueOtherThan(b1.expression(), () -> randomBinaryStringStringExpression()); - BinaryStringStringPipe newB = new BinaryStringStringPipe( - b1.source(), - newExpression, - b1.left(), - b1.right(), - b1.operation()); + BinaryStringStringPipe newB = new BinaryStringStringPipe(b1.source(), newExpression, b1.left(), b1.right(), b1.operation()); assertEquals(newB, b1.transformPropertiesOnly(Expression.class, v -> Objects.equals(v, b1.expression()) ? newExpression : v)); BinaryStringStringPipe b2 = randomInstance(); Source newLoc = randomValueOtherThan(b2.source(), () -> randomSource()); - newB = new BinaryStringStringPipe( - newLoc, - b2.expression(), - b2.left(), - b2.right(), - b2.operation()); - assertEquals(newB, - b2.transformPropertiesOnly(Source.class, v -> Objects.equals(v, b2.source()) ? newLoc : v)); + newB = new BinaryStringStringPipe(newLoc, b2.expression(), b2.left(), b2.right(), b2.operation()); + assertEquals(newB, b2.transformPropertiesOnly(Source.class, v -> Objects.equals(v, b2.source()) ? newLoc : v)); } @Override @@ -76,8 +60,7 @@ public void testReplaceChildren() { BinaryStringStringPipe b = randomInstance(); Pipe newLeft = pipe(((Expression) randomValueOtherThan(b.left(), () -> randomStringLiteral()))); Pipe newRight = pipe(((Expression) randomValueOtherThan(b.right(), () -> randomStringLiteral()))); - BinaryStringStringPipe newB = - new BinaryStringStringPipe(b.source(), b.expression(), b.left(), b.right(), b.operation()); + BinaryStringStringPipe newB = new BinaryStringStringPipe(b.source(), b.expression(), b.left(), b.right(), b.operation()); BinaryPipe transformed = newB.replaceChildren(newLeft, b.right()); assertEquals(transformed.left(), newLeft); @@ -101,31 +84,45 @@ public void testReplaceChildren() { @Override protected BinaryStringStringPipe mutate(BinaryStringStringPipe instance) { List> randoms = new ArrayList<>(); - randoms.add(f -> new BinaryStringStringPipe(f.source(), + randoms.add( + f -> new BinaryStringStringPipe( + f.source(), f.expression(), pipe(((Expression) randomValueOtherThan(f.left(), () -> randomStringLiteral()))), f.right(), - f.operation())); - randoms.add(f -> new BinaryStringStringPipe(f.source(), + f.operation() + ) + ); + randoms.add( + f -> new BinaryStringStringPipe( + f.source(), f.expression(), f.left(), pipe(((Expression) randomValueOtherThan(f.right(), () -> randomStringLiteral()))), - f.operation())); - randoms.add(f -> new BinaryStringStringPipe(f.source(), + f.operation() + ) + ); + randoms.add( + f -> new BinaryStringStringPipe( + f.source(), f.expression(), pipe(((Expression) randomValueOtherThan(f.left(), () -> randomStringLiteral()))), pipe(((Expression) randomValueOtherThan(f.right(), () -> randomStringLiteral()))), - f.operation())); + f.operation() + ) + ); return randomFrom(randoms).apply(instance); } @Override protected BinaryStringStringPipe copy(BinaryStringStringPipe instance) { - return new BinaryStringStringPipe(instance.source(), - instance.expression(), - instance.left(), - instance.right(), - instance.operation()); + return new BinaryStringStringPipe( + instance.source(), + instance.expression(), + instance.left(), + instance.right(), + instance.operation() + ); } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/BinaryStringStringProcessorTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/BinaryStringStringProcessorTests.java index c9bc7b32b1bbc..f03fccbc336f0 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/BinaryStringStringProcessorTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/BinaryStringStringProcessorTests.java @@ -23,9 +23,10 @@ public class BinaryStringStringProcessorTests extends AbstractWireSerializingTes @Override protected BinaryStringStringProcessor createTestInstance() { return new BinaryStringStringProcessor( - new ConstantProcessor(randomRealisticUnicodeOfLengthBetween(1, 128)), - new ConstantProcessor(randomRealisticUnicodeOfLengthBetween(1, 128)), - randomFrom(BinaryStringStringOperation.values())); + new ConstantProcessor(randomRealisticUnicodeOfLengthBetween(1, 128)), + new ConstantProcessor(randomRealisticUnicodeOfLengthBetween(1, 128)), + randomFrom(BinaryStringStringOperation.values()) + ); } @Override @@ -54,11 +55,15 @@ public void testPositionFunctionWithEdgeCases() { } public void testPositionFunctionInputsValidation() { - SqlIllegalArgumentException siae = expectThrows(SqlIllegalArgumentException.class, - () -> new Position(EMPTY, l(5), l("foo")).makePipe().asProcessor().process(null)); + SqlIllegalArgumentException siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Position(EMPTY, l(5), l("foo")).makePipe().asProcessor().process(null) + ); assertEquals("A string/char is required; received [5]", siae.getMessage()); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new Position(EMPTY, l("foo bar"), l(3)).makePipe().asProcessor().process(null)); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Position(EMPTY, l("foo bar"), l(3)).makePipe().asProcessor().process(null) + ); assertEquals("A string/char is required; received [3]", siae.getMessage()); } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/ConcatFunctionPipeTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/ConcatFunctionPipeTests.java index acd8907e9c63d..7d1b4790e24cd 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/ConcatFunctionPipeTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/ConcatFunctionPipeTests.java @@ -34,11 +34,7 @@ private Expression randomConcatFunctionExpression() { } public static ConcatFunctionPipe randomConcatFunctionPipe() { - return (ConcatFunctionPipe) new Concat( - randomSource(), - randomStringLiteral(), - randomStringLiteral()) - .makePipe(); + return (ConcatFunctionPipe) new Concat(randomSource(), randomStringLiteral(), randomStringLiteral()).makePipe(); } @Override @@ -48,22 +44,13 @@ public void testTransform() { ConcatFunctionPipe b1 = randomInstance(); Expression newExpression = randomValueOtherThan(b1.expression(), () -> randomConcatFunctionExpression()); - ConcatFunctionPipe newB = new ConcatFunctionPipe( - b1.source(), - newExpression, - b1.left(), - b1.right()); + ConcatFunctionPipe newB = new ConcatFunctionPipe(b1.source(), newExpression, b1.left(), b1.right()); assertEquals(newB, b1.transformPropertiesOnly(Expression.class, v -> Objects.equals(v, b1.expression()) ? newExpression : v)); ConcatFunctionPipe b2 = randomInstance(); Source newLoc = randomValueOtherThan(b2.source(), () -> randomSource()); - newB = new ConcatFunctionPipe( - newLoc, - b2.expression(), - b2.left(), - b2.right()); - assertEquals(newB, - b2.transformPropertiesOnly(Source.class, v -> Objects.equals(v, b2.source()) ? newLoc : v)); + newB = new ConcatFunctionPipe(newLoc, b2.expression(), b2.left(), b2.right()); + assertEquals(newB, b2.transformPropertiesOnly(Source.class, v -> Objects.equals(v, b2.source()) ? newLoc : v)); } @Override @@ -71,8 +58,7 @@ public void testReplaceChildren() { ConcatFunctionPipe b = randomInstance(); Pipe newLeft = randomValueOtherThan(b.left(), () -> pipe(randomStringLiteral())); Pipe newRight = randomValueOtherThan(b.right(), () -> pipe(randomStringLiteral())); - ConcatFunctionPipe newB = - new ConcatFunctionPipe(b.source(), b.expression(), b.left(), b.right()); + ConcatFunctionPipe newB = new ConcatFunctionPipe(b.source(), b.expression(), b.left(), b.right()); BinaryPipe transformed = newB.replaceChildren(newLeft, b.right()); assertEquals(transformed.left(), newLeft); @@ -96,27 +82,36 @@ public void testReplaceChildren() { @Override protected ConcatFunctionPipe mutate(ConcatFunctionPipe instance) { List> randoms = new ArrayList<>(); - randoms.add(f -> new ConcatFunctionPipe(f.source(), + randoms.add( + f -> new ConcatFunctionPipe( + f.source(), f.expression(), randomValueOtherThan(f.left(), () -> pipe(randomStringLiteral())), - f.right())); - randoms.add(f -> new ConcatFunctionPipe(f.source(), + f.right() + ) + ); + randoms.add( + f -> new ConcatFunctionPipe( + f.source(), f.expression(), f.left(), - randomValueOtherThan(f.right(), () -> pipe(randomStringLiteral())))); - randoms.add(f -> new ConcatFunctionPipe(f.source(), + randomValueOtherThan(f.right(), () -> pipe(randomStringLiteral())) + ) + ); + randoms.add( + f -> new ConcatFunctionPipe( + f.source(), f.expression(), randomValueOtherThan(f.left(), () -> pipe(randomStringLiteral())), - randomValueOtherThan(f.right(), () -> pipe(randomStringLiteral())))); + randomValueOtherThan(f.right(), () -> pipe(randomStringLiteral())) + ) + ); return randomFrom(randoms).apply(instance); } @Override protected ConcatFunctionPipe copy(ConcatFunctionPipe instance) { - return new ConcatFunctionPipe(instance.source(), - instance.expression(), - instance.left(), - instance.right()); + return new ConcatFunctionPipe(instance.source(), instance.expression(), instance.left(), instance.right()); } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/ConcatProcessorTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/ConcatProcessorTests.java index ece07a14473b3..b7d524e9c8e84 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/ConcatProcessorTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/ConcatProcessorTests.java @@ -22,8 +22,9 @@ public class ConcatProcessorTests extends AbstractWireSerializingTestCase new Concat(EMPTY, l(5), l("foo")).makePipe().asProcessor().process(null)); + SqlIllegalArgumentException siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Concat(EMPTY, l(5), l("foo")).makePipe().asProcessor().process(null) + ); assertEquals("A string/char is required; received [5]", siae.getMessage()); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new Concat(EMPTY, l("foo bar"), l(3)).makePipe().asProcessor().process(null)); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Concat(EMPTY, l("foo bar"), l(3)).makePipe().asProcessor().process(null) + ); assertEquals("A string/char is required; received [3]", siae.getMessage()); } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/InsertFunctionPipeTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/InsertFunctionPipeTests.java index 97bc06e7414b3..85288ef2ebd2a 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/InsertFunctionPipeTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/InsertFunctionPipeTests.java @@ -36,12 +36,13 @@ private Expression randomInsertFunctionExpression() { } public static InsertFunctionPipe randomInsertFunctionPipe() { - return (InsertFunctionPipe) (new Insert(randomSource(), - randomStringLiteral(), - randomIntLiteral(), - randomIntLiteral(), - randomStringLiteral()) - .makePipe()); + return (InsertFunctionPipe) (new Insert( + randomSource(), + randomStringLiteral(), + randomIntLiteral(), + randomIntLiteral(), + randomStringLiteral() + ).makePipe()); } @Override @@ -50,26 +51,13 @@ public void testTransform() { // skipping the children (the two parameters of the binary function) which are tested separately InsertFunctionPipe b1 = randomInstance(); Expression newExpression = randomValueOtherThan(b1.expression(), () -> randomInsertFunctionExpression()); - InsertFunctionPipe newB = new InsertFunctionPipe( - b1.source(), - newExpression, - b1.input(), - b1.start(), - b1.length(), - b1.replacement()); + InsertFunctionPipe newB = new InsertFunctionPipe(b1.source(), newExpression, b1.input(), b1.start(), b1.length(), b1.replacement()); assertEquals(newB, b1.transformPropertiesOnly(Expression.class, v -> Objects.equals(v, b1.expression()) ? newExpression : v)); InsertFunctionPipe b2 = randomInstance(); Source newLoc = randomValueOtherThan(b2.source(), () -> randomSource()); - newB = new InsertFunctionPipe( - newLoc, - b2.expression(), - b2.input(), - b2.start(), - b2.length(), - b2.replacement()); - assertEquals(newB, - b2.transformPropertiesOnly(Source.class, v -> Objects.equals(v, b2.source()) ? newLoc : v)); + newB = new InsertFunctionPipe(newLoc, b2.expression(), b2.input(), b2.start(), b2.length(), b2.replacement()); + assertEquals(newB, b2.transformPropertiesOnly(Source.class, v -> Objects.equals(v, b2.source()) ? newLoc : v)); } @Override @@ -79,18 +67,18 @@ public void testReplaceChildren() { Pipe newStart = randomValueOtherThan(b.start(), () -> pipe(randomIntLiteral())); Pipe newLength = randomValueOtherThan(b.length(), () -> pipe(randomIntLiteral())); Pipe newR = randomValueOtherThan(b.replacement(), () -> pipe(randomStringLiteral())); - InsertFunctionPipe newB = - new InsertFunctionPipe(b.source(), b.expression(), b.input(), b.start(), b.length(), b.replacement()); + InsertFunctionPipe newB = new InsertFunctionPipe(b.source(), b.expression(), b.input(), b.start(), b.length(), b.replacement()); InsertFunctionPipe transformed = null; // generate all the combinations of possible children modifications and test all of them - for(int i = 1; i < 5; i++) { - for(BitSet comb : new Combinations(4, i)) { + for (int i = 1; i < 5; i++) { + for (BitSet comb : new Combinations(4, i)) { transformed = (InsertFunctionPipe) newB.replaceChildren( - comb.get(0) ? newInput : b.input(), - comb.get(1) ? newStart : b.start(), - comb.get(2) ? newLength : b.length(), - comb.get(3) ? newR : b.replacement()); + comb.get(0) ? newInput : b.input(), + comb.get(1) ? newStart : b.start(), + comb.get(2) ? newLength : b.length(), + comb.get(3) ? newR : b.replacement() + ); assertEquals(transformed.input(), comb.get(0) ? newInput : b.input()); assertEquals(transformed.start(), comb.get(1) ? newStart : b.start()); assertEquals(transformed.length(), comb.get(2) ? newLength : b.length()); @@ -105,15 +93,18 @@ public void testReplaceChildren() { protected InsertFunctionPipe mutate(InsertFunctionPipe instance) { List> randoms = new ArrayList<>(); - for(int i = 1; i < 5; i++) { - for(BitSet comb : new Combinations(4, i)) { - randoms.add(f -> new InsertFunctionPipe( + for (int i = 1; i < 5; i++) { + for (BitSet comb : new Combinations(4, i)) { + randoms.add( + f -> new InsertFunctionPipe( f.source(), f.expression(), comb.get(0) ? randomValueOtherThan(f.input(), () -> pipe(randomStringLiteral())) : f.input(), comb.get(1) ? randomValueOtherThan(f.start(), () -> pipe(randomIntLiteral())) : f.start(), - comb.get(2) ? randomValueOtherThan(f.length(), () -> pipe(randomIntLiteral())): f.length(), - comb.get(3) ? randomValueOtherThan(f.replacement(), () -> pipe(randomStringLiteral())) : f.replacement())); + comb.get(2) ? randomValueOtherThan(f.length(), () -> pipe(randomIntLiteral())) : f.length(), + comb.get(3) ? randomValueOtherThan(f.replacement(), () -> pipe(randomStringLiteral())) : f.replacement() + ) + ); } } @@ -122,11 +113,13 @@ protected InsertFunctionPipe mutate(InsertFunctionPipe instance) { @Override protected InsertFunctionPipe copy(InsertFunctionPipe instance) { - return new InsertFunctionPipe(instance.source(), - instance.expression(), - instance.input(), - instance.start(), - instance.length(), - instance.replacement()); + return new InsertFunctionPipe( + instance.source(), + instance.expression(), + instance.input(), + instance.start(), + instance.length(), + instance.replacement() + ); } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/InsertProcessorTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/InsertProcessorTests.java index 16695d65426cf..df3c801efa640 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/InsertProcessorTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/InsertProcessorTests.java @@ -22,10 +22,11 @@ public class InsertProcessorTests extends AbstractWireSerializingTestCase new Insert(EMPTY, l(5), l(1), l(3), l("baz")).makePipe().asProcessor().process(null)); + SqlIllegalArgumentException siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Insert(EMPTY, l(5), l(1), l(3), l("baz")).makePipe().asProcessor().process(null) + ); assertEquals("A string/char is required; received [5]", siae.getMessage()); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new Insert(EMPTY, l("foobar"), l(1), l(3), l(66)).makePipe().asProcessor().process(null)); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Insert(EMPTY, l("foobar"), l(1), l(3), l(66)).makePipe().asProcessor().process(null) + ); assertEquals("A string/char is required; received [66]", siae.getMessage()); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new Insert(EMPTY, l("foobar"), l("c"), l(3), l("baz")).makePipe().asProcessor().process(null)); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Insert(EMPTY, l("foobar"), l("c"), l(3), l("baz")).makePipe().asProcessor().process(null) + ); assertEquals("A fixed point number is required for [start]; received [java.lang.String]", siae.getMessage()); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new Insert(EMPTY, l("foobar"), l(1), l('z'), l("baz")).makePipe().asProcessor().process(null)); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Insert(EMPTY, l("foobar"), l(1), l('z'), l("baz")).makePipe().asProcessor().process(null) + ); assertEquals("A fixed point number is required for [length]; received [java.lang.Character]", siae.getMessage()); - assertEquals("baroobar", new Insert(EMPTY, l("foobar"), l(Integer.MIN_VALUE + 1), l(1), - l("bar")).makePipe().asProcessor().process(null)); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new Insert(EMPTY, l("foobarbar"), l(Integer.MIN_VALUE), l(1), l("bar")).makePipe().asProcessor().process(null)); + assertEquals( + "baroobar", + new Insert(EMPTY, l("foobar"), l(Integer.MIN_VALUE + 1), l(1), l("bar")).makePipe().asProcessor().process(null) + ); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Insert(EMPTY, l("foobarbar"), l(Integer.MIN_VALUE), l(1), l("bar")).makePipe().asProcessor().process(null) + ); assertEquals("[start] out of the allowed range [-2147483647, 2147483647], received [-2147483648]", siae.getMessage()); - assertEquals("foobar", new Insert(EMPTY, l("foobar"), l(Integer.MAX_VALUE), l(1), - l("bar")).makePipe().asProcessor().process(null)); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new Insert(EMPTY, l("foobar"), l((long) Integer.MAX_VALUE + 1), l(1), l("bar")).makePipe().asProcessor().process(null)); + assertEquals("foobar", new Insert(EMPTY, l("foobar"), l(Integer.MAX_VALUE), l(1), l("bar")).makePipe().asProcessor().process(null)); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Insert(EMPTY, l("foobar"), l((long) Integer.MAX_VALUE + 1), l(1), l("bar")).makePipe().asProcessor().process(null) + ); assertEquals("[start] out of the allowed range [-2147483647, 2147483647], received [2147483648]", siae.getMessage()); assertEquals("barfoobar", new Insert(EMPTY, l("foobar"), l(1), l(0), l("bar")).makePipe().asProcessor().process(null)); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new Insert(EMPTY, l("foobar"), l(1), l(-1), l("bar")).makePipe().asProcessor().process(null)); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Insert(EMPTY, l("foobar"), l(1), l(-1), l("bar")).makePipe().asProcessor().process(null) + ); assertEquals("[length] out of the allowed range [0, 2147483647], received [-1]", siae.getMessage()); assertEquals("bar", new Insert(EMPTY, l("foobar"), l(1), l(Integer.MAX_VALUE), l("bar")).makePipe().asProcessor().process(null)); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new Insert(EMPTY, l("foobar"), l(1), l((long) Integer.MAX_VALUE + 1), l("bar")).makePipe().asProcessor().process(null)); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Insert(EMPTY, l("foobar"), l(1), l((long) Integer.MAX_VALUE + 1), l("bar")).makePipe().asProcessor().process(null) + ); assertEquals("[length] out of the allowed range [0, 2147483647], received [2147483648]", siae.getMessage()); } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/LocateFunctionPipeTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/LocateFunctionPipeTests.java index 41e72e4666288..f76ab6790b1e8 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/LocateFunctionPipeTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/LocateFunctionPipeTests.java @@ -36,11 +36,12 @@ private Expression randomLocateFunctionExpression() { } public static LocateFunctionPipe randomLocateFunctionPipe() { - return (LocateFunctionPipe) (new Locate(randomSource(), - randomStringLiteral(), - randomStringLiteral(), - randomFrom(true, false) ? randomIntLiteral() : null) - .makePipe()); + return (LocateFunctionPipe) (new Locate( + randomSource(), + randomStringLiteral(), + randomStringLiteral(), + randomFrom(true, false) ? randomIntLiteral() : null + ).makePipe()); } @Override @@ -49,26 +50,15 @@ public void testTransform() { // skipping the children (the two parameters of the binary function) which are tested separately LocateFunctionPipe b1 = randomInstance(); Expression newExpression = randomValueOtherThan(b1.expression(), () -> randomLocateFunctionExpression()); - LocateFunctionPipe newB = new LocateFunctionPipe( - b1.source(), - newExpression, - b1.pattern(), - b1.input(), - b1.start()); + LocateFunctionPipe newB = new LocateFunctionPipe(b1.source(), newExpression, b1.pattern(), b1.input(), b1.start()); assertEquals(newB, b1.transformPropertiesOnly(Expression.class, v -> Objects.equals(v, b1.expression()) ? newExpression : v)); LocateFunctionPipe b2 = randomInstance(); Source newLoc = randomValueOtherThan(b2.source(), () -> randomSource()); - newB = new LocateFunctionPipe( - newLoc, - b2.expression(), - b2.pattern(), - b2.input(), - b2.start()); - - assertEquals(newB, - b2.transformPropertiesOnly(Source.class, v -> Objects.equals(v, b2.source()) ? newLoc : v)); + newB = new LocateFunctionPipe(newLoc, b2.expression(), b2.pattern(), b2.input(), b2.start()); + + assertEquals(newB, b2.transformPropertiesOnly(Source.class, v -> Objects.equals(v, b2.source()) ? newLoc : v)); } @Override @@ -82,13 +72,14 @@ public void testReplaceChildren() { LocateFunctionPipe transformed = null; // generate all the combinations of possible children modifications and test all of them - for(int i = 1; i < 4; i++) { - for(BitSet comb : new Combinations(3, i)) { + for (int i = 1; i < 4; i++) { + for (BitSet comb : new Combinations(3, i)) { Pipe tempNewStart = b.start() == null ? b.start() : (comb.get(2) ? newStart : b.start()); transformed = (LocateFunctionPipe) newB.replaceChildren( - comb.get(0) ? newPattern : b.pattern(), - comb.get(1) ? newInput : b.input(), - tempNewStart); + comb.get(0) ? newPattern : b.pattern(), + comb.get(1) ? newInput : b.input(), + tempNewStart + ); assertEquals(transformed.pattern(), comb.get(0) ? newPattern : b.pattern()); assertEquals(transformed.input(), comb.get(1) ? newInput : b.input()); @@ -103,23 +94,31 @@ public void testReplaceChildren() { protected LocateFunctionPipe mutate(LocateFunctionPipe instance) { List> randoms = new ArrayList<>(); if (instance.start() == null) { - for(int i = 1; i < 3; i++) { - for(BitSet comb : new Combinations(2, i)) { - randoms.add(f -> new LocateFunctionPipe(f.source(), + for (int i = 1; i < 3; i++) { + for (BitSet comb : new Combinations(2, i)) { + randoms.add( + f -> new LocateFunctionPipe( + f.source(), f.expression(), comb.get(0) ? randomValueOtherThan(f.pattern(), () -> pipe(randomStringLiteral())) : f.pattern(), comb.get(1) ? randomValueOtherThan(f.input(), () -> pipe(randomStringLiteral())) : f.input(), - null)); + null + ) + ); } } } else { - for(int i = 1; i < 4; i++) { - for(BitSet comb : new Combinations(3, i)) { - randoms.add(f -> new LocateFunctionPipe(f.source(), + for (int i = 1; i < 4; i++) { + for (BitSet comb : new Combinations(3, i)) { + randoms.add( + f -> new LocateFunctionPipe( + f.source(), f.expression(), comb.get(0) ? randomValueOtherThan(f.pattern(), () -> pipe(randomStringLiteral())) : f.pattern(), comb.get(1) ? randomValueOtherThan(f.input(), () -> pipe(randomStringLiteral())) : f.input(), - comb.get(2) ? randomValueOtherThan(f.start(), () -> pipe(randomIntLiteral())) : f.start())); + comb.get(2) ? randomValueOtherThan(f.start(), () -> pipe(randomIntLiteral())) : f.start() + ) + ); } } } @@ -129,10 +128,6 @@ protected LocateFunctionPipe mutate(LocateFunctionPipe instance) { @Override protected LocateFunctionPipe copy(LocateFunctionPipe instance) { - return new LocateFunctionPipe(instance.source(), - instance.expression(), - instance.pattern(), - instance.input(), - instance.start()); + return new LocateFunctionPipe(instance.source(), instance.expression(), instance.pattern(), instance.input(), instance.start()); } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/LocateProcessorTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/LocateProcessorTests.java index 64ca8b69f6b79..01223940bd281 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/LocateProcessorTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/LocateProcessorTests.java @@ -26,9 +26,10 @@ protected LocateFunctionProcessor createTestInstance() { // values for it. Integer start = frequently() ? randomInt() : null; return new LocateFunctionProcessor( - new ConstantProcessor(randomRealisticUnicodeOfLengthBetween(0, 128)), - new ConstantProcessor(randomRealisticUnicodeOfLengthBetween(0, 128)), - new ConstantProcessor(start)); + new ConstantProcessor(randomRealisticUnicodeOfLengthBetween(0, 128)), + new ConstantProcessor(randomRealisticUnicodeOfLengthBetween(0, 128)), + new ConstantProcessor(start) + ); } @Override @@ -63,26 +64,36 @@ public void testLocateFunctionWithEdgeCasesInputs() { } public void testLocateFunctionValidatingInputs() { - SqlIllegalArgumentException siae = expectThrows(SqlIllegalArgumentException.class, - () -> new Locate(EMPTY, l(5), l("foobarbar"), l(3)).makePipe().asProcessor().process(null)); + SqlIllegalArgumentException siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Locate(EMPTY, l(5), l("foobarbar"), l(3)).makePipe().asProcessor().process(null) + ); assertEquals("A string/char is required; received [5]", siae.getMessage()); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new Locate(EMPTY, l("foo"), l(1), l(3)).makePipe().asProcessor().process(null)); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Locate(EMPTY, l("foo"), l(1), l(3)).makePipe().asProcessor().process(null) + ); assertEquals("A string/char is required; received [1]", siae.getMessage()); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new Locate(EMPTY, l("foobarbar"), l("bar"), l('c')).makePipe().asProcessor().process(null)); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Locate(EMPTY, l("foobarbar"), l("bar"), l('c')).makePipe().asProcessor().process(null) + ); assertEquals("A fixed point number is required for [start]; received [java.lang.Character]", siae.getMessage()); assertEquals(4, new Locate(EMPTY, l("bar"), l("foobarbar"), l(Integer.MIN_VALUE + 1)).makePipe().asProcessor().process(null)); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new Locate(EMPTY, l("bar"), l("foobarbar"), l(Integer.MIN_VALUE)).makePipe().asProcessor().process(null)); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Locate(EMPTY, l("bar"), l("foobarbar"), l(Integer.MIN_VALUE)).makePipe().asProcessor().process(null) + ); assertEquals("[start] out of the allowed range [-2147483647, 2147483647], received [-2147483648]", siae.getMessage()); assertEquals(0, new Locate(EMPTY, l("bar"), l("foobarbar"), l(Integer.MAX_VALUE)).makePipe().asProcessor().process(null)); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new Locate(EMPTY, l("bar"), l("foobarbar"), l((long) Integer.MAX_VALUE + 1)).makePipe().asProcessor().process(null)); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Locate(EMPTY, l("bar"), l("foobarbar"), l((long) Integer.MAX_VALUE + 1)).makePipe().asProcessor().process(null) + ); assertEquals("[start] out of the allowed range [-2147483647, 2147483647], received [2147483648]", siae.getMessage()); } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/ReplaceFunctionPipeTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/ReplaceFunctionPipeTests.java index 50aa31fb6a615..65270e1bad0a7 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/ReplaceFunctionPipeTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/ReplaceFunctionPipeTests.java @@ -35,11 +35,8 @@ private Expression randomReplaceFunctionExpression() { } public static ReplaceFunctionPipe randomReplaceFunctionPipe() { - return (ReplaceFunctionPipe) (new Replace(randomSource(), - randomStringLiteral(), - randomStringLiteral(), - randomStringLiteral()) - .makePipe()); + return (ReplaceFunctionPipe) (new Replace(randomSource(), randomStringLiteral(), randomStringLiteral(), randomStringLiteral()) + .makePipe()); } @Override @@ -49,24 +46,13 @@ public void testTransform() { ReplaceFunctionPipe b1 = randomInstance(); Expression newExpression = randomValueOtherThan(b1.expression(), () -> randomReplaceFunctionExpression()); - ReplaceFunctionPipe newB = new ReplaceFunctionPipe( - b1.source(), - newExpression, - b1.input(), - b1.pattern(), - b1.replacement()); + ReplaceFunctionPipe newB = new ReplaceFunctionPipe(b1.source(), newExpression, b1.input(), b1.pattern(), b1.replacement()); assertEquals(newB, b1.transformPropertiesOnly(Expression.class, v -> Objects.equals(v, b1.expression()) ? newExpression : v)); ReplaceFunctionPipe b2 = randomInstance(); Source newLoc = randomValueOtherThan(b2.source(), () -> randomSource()); - newB = new ReplaceFunctionPipe( - newLoc, - b2.expression(), - b2.input(), - b2.pattern(), - b2.replacement()); - assertEquals(newB, - b2.transformPropertiesOnly(Source.class, v -> Objects.equals(v, b2.source()) ? newLoc : v)); + newB = new ReplaceFunctionPipe(newLoc, b2.expression(), b2.input(), b2.pattern(), b2.replacement()); + assertEquals(newB, b2.transformPropertiesOnly(Source.class, v -> Objects.equals(v, b2.source()) ? newLoc : v)); } @Override @@ -79,12 +65,13 @@ public void testReplaceChildren() { ReplaceFunctionPipe transformed = null; // generate all the combinations of possible children modifications and test all of them - for(int i = 1; i < 4; i++) { - for(BitSet comb : new Combinations(3, i)) { + for (int i = 1; i < 4; i++) { + for (BitSet comb : new Combinations(3, i)) { transformed = (ReplaceFunctionPipe) newB.replaceChildren( - comb.get(0) ? newInput : b.input(), - comb.get(1) ? newPattern : b.pattern(), - comb.get(2) ? newR : b.replacement()); + comb.get(0) ? newInput : b.input(), + comb.get(1) ? newPattern : b.pattern(), + comb.get(2) ? newR : b.replacement() + ); assertEquals(transformed.input(), comb.get(0) ? newInput : b.input()); assertEquals(transformed.pattern(), comb.get(1) ? newPattern : b.pattern()); @@ -99,13 +86,17 @@ public void testReplaceChildren() { protected ReplaceFunctionPipe mutate(ReplaceFunctionPipe instance) { List> randoms = new ArrayList<>(); - for(int i = 1; i < 4; i++) { - for(BitSet comb : new Combinations(3, i)) { - randoms.add(f -> new ReplaceFunctionPipe(f.source(), + for (int i = 1; i < 4; i++) { + for (BitSet comb : new Combinations(3, i)) { + randoms.add( + f -> new ReplaceFunctionPipe( + f.source(), f.expression(), comb.get(0) ? randomValueOtherThan(f.input(), () -> pipe(randomStringLiteral())) : f.input(), comb.get(1) ? randomValueOtherThan(f.pattern(), () -> pipe(randomStringLiteral())) : f.pattern(), - comb.get(2) ? randomValueOtherThan(f.replacement(), () -> pipe(randomStringLiteral())) : f.replacement())); + comb.get(2) ? randomValueOtherThan(f.replacement(), () -> pipe(randomStringLiteral())) : f.replacement() + ) + ); } } @@ -114,10 +105,12 @@ protected ReplaceFunctionPipe mutate(ReplaceFunctionPipe instance) { @Override protected ReplaceFunctionPipe copy(ReplaceFunctionPipe instance) { - return new ReplaceFunctionPipe(instance.source(), - instance.expression(), - instance.input(), - instance.pattern(), - instance.replacement()); + return new ReplaceFunctionPipe( + instance.source(), + instance.expression(), + instance.input(), + instance.pattern(), + instance.replacement() + ); } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/ReplaceProcessorTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/ReplaceProcessorTests.java index a7040b34ddb01..0961587a3e63f 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/ReplaceProcessorTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/ReplaceProcessorTests.java @@ -22,9 +22,10 @@ public class ReplaceProcessorTests extends AbstractWireSerializingTestCase new Replace(EMPTY, l(5), l("bar"), l("baz")).makePipe().asProcessor().process(null)); + SqlIllegalArgumentException siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Replace(EMPTY, l(5), l("bar"), l("baz")).makePipe().asProcessor().process(null) + ); assertEquals("A string/char is required; received [5]", siae.getMessage()); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new Replace(EMPTY, l("foobarbar"), l(4), l("baz")).makePipe().asProcessor().process(null)); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Replace(EMPTY, l("foobarbar"), l(4), l("baz")).makePipe().asProcessor().process(null) + ); assertEquals("A string/char is required; received [4]", siae.getMessage()); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new Replace(EMPTY, l("foobarbar"), l("bar"), l(3)).makePipe().asProcessor().process(null)); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Replace(EMPTY, l("foobarbar"), l("bar"), l(3)).makePipe().asProcessor().process(null) + ); assertEquals("A string/char is required; received [3]", siae.getMessage()); } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/StringFunctionProcessorTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/StringFunctionProcessorTests.java index d72c4f8b4590e..af839ff7e78d2 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/StringFunctionProcessorTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/StringFunctionProcessorTests.java @@ -180,7 +180,7 @@ public void testTrim() { assertEquals("", proc.process(withRandomWhitespaces(" \t \r\n \n ", true, true))); assertEquals("foo bar", proc.process(withRandomWhitespaces("foo bar", true, false))); assertEquals("foo bar", proc.process(withRandomWhitespaces("foo bar", false, true))); - assertEquals("foo bar", proc.process(withRandomWhitespaces("foo bar",true, true))); + assertEquals("foo bar", proc.process(withRandomWhitespaces("foo bar", true, true))); assertEquals("foo \t \r\n \n bar", proc.process(withRandomWhitespaces("foo \t \r\n \n bar", true, true))); assertEquals("f", proc.process('f')); diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/SubstringFunctionPipeTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/SubstringFunctionPipeTests.java index 1d46873215a25..b47bda65112cd 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/SubstringFunctionPipeTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/SubstringFunctionPipeTests.java @@ -24,8 +24,7 @@ import static org.elasticsearch.xpack.ql.expression.function.scalar.FunctionTestUtils.randomStringLiteral; import static org.elasticsearch.xpack.ql.tree.SourceTests.randomSource; -public class SubstringFunctionPipeTests - extends AbstractNodeTestCase { +public class SubstringFunctionPipeTests extends AbstractNodeTestCase { @Override protected SubstringFunctionPipe randomInstance() { @@ -37,11 +36,8 @@ private Expression randomSubstringFunctionExpression() { } public static SubstringFunctionPipe randomSubstringFunctionPipe() { - return (SubstringFunctionPipe) (new Substring(randomSource(), - randomStringLiteral(), - randomIntLiteral(), - randomIntLiteral()) - .makePipe()); + return (SubstringFunctionPipe) (new Substring(randomSource(), randomStringLiteral(), randomIntLiteral(), randomIntLiteral()) + .makePipe()); } @Override @@ -50,24 +46,13 @@ public void testTransform() { // skipping the children (the two parameters of the binary function) which are tested separately SubstringFunctionPipe b1 = randomInstance(); Expression newExpression = randomValueOtherThan(b1.expression(), () -> randomSubstringFunctionExpression()); - SubstringFunctionPipe newB = new SubstringFunctionPipe( - b1.source(), - newExpression, - b1.input(), - b1.start(), - b1.length()); + SubstringFunctionPipe newB = new SubstringFunctionPipe(b1.source(), newExpression, b1.input(), b1.start(), b1.length()); assertEquals(newB, b1.transformPropertiesOnly(Expression.class, v -> Objects.equals(v, b1.expression()) ? newExpression : v)); SubstringFunctionPipe b2 = randomInstance(); Source newLoc = randomValueOtherThan(b2.source(), () -> randomSource()); - newB = new SubstringFunctionPipe( - newLoc, - b2.expression(), - b2.input(), - b2.start(), - b2.length()); - assertEquals(newB, - b2.transformPropertiesOnly(Source.class, v -> Objects.equals(v, b2.source()) ? newLoc : v)); + newB = new SubstringFunctionPipe(newLoc, b2.expression(), b2.input(), b2.start(), b2.length()); + assertEquals(newB, b2.transformPropertiesOnly(Source.class, v -> Objects.equals(v, b2.source()) ? newLoc : v)); } @Override @@ -80,12 +65,13 @@ public void testReplaceChildren() { SubstringFunctionPipe transformed = null; // generate all the combinations of possible children modifications and test all of them - for(int i = 1; i < 4; i++) { - for(BitSet comb : new Combinations(3, i)) { + for (int i = 1; i < 4; i++) { + for (BitSet comb : new Combinations(3, i)) { transformed = newB.replaceChildren( - comb.get(0) ? newInput : b.input(), - comb.get(1) ? newStart : b.start(), - comb.get(2) ? newLength : b.length()); + comb.get(0) ? newInput : b.input(), + comb.get(1) ? newStart : b.start(), + comb.get(2) ? newLength : b.length() + ); assertEquals(transformed.input(), comb.get(0) ? newInput : b.input()); assertEquals(transformed.start(), comb.get(1) ? newStart : b.start()); assertEquals(transformed.length(), comb.get(2) ? newLength : b.length()); @@ -99,14 +85,17 @@ public void testReplaceChildren() { protected SubstringFunctionPipe mutate(SubstringFunctionPipe instance) { List> randoms = new ArrayList<>(); - for(int i = 1; i < 4; i++) { - for(BitSet comb : new Combinations(3, i)) { - randoms.add(f -> new SubstringFunctionPipe( + for (int i = 1; i < 4; i++) { + for (BitSet comb : new Combinations(3, i)) { + randoms.add( + f -> new SubstringFunctionPipe( f.source(), f.expression(), comb.get(0) ? randomValueOtherThan(f.input(), () -> pipe(randomStringLiteral())) : f.input(), comb.get(1) ? randomValueOtherThan(f.start(), () -> pipe(randomIntLiteral())) : f.start(), - comb.get(2) ? randomValueOtherThan(f.length(), () -> pipe(randomIntLiteral())): f.length())); + comb.get(2) ? randomValueOtherThan(f.length(), () -> pipe(randomIntLiteral())) : f.length() + ) + ); } } @@ -115,10 +104,6 @@ protected SubstringFunctionPipe mutate(SubstringFunctionPipe instance) { @Override protected SubstringFunctionPipe copy(SubstringFunctionPipe instance) { - return new SubstringFunctionPipe(instance.source(), - instance.expression(), - instance.input(), - instance.start(), - instance.length()); + return new SubstringFunctionPipe(instance.source(), instance.expression(), instance.input(), instance.start(), instance.length()); } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/SubstringProcessorTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/SubstringProcessorTests.java index 630236d34af25..b3d3f2ce6a5ff 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/SubstringProcessorTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/string/SubstringProcessorTests.java @@ -22,9 +22,10 @@ public class SubstringProcessorTests extends AbstractWireSerializingTestCase new Substring(EMPTY, l(5), l(1), l(3)).makePipe().asProcessor().process(null)); + SqlIllegalArgumentException siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Substring(EMPTY, l(5), l(1), l(3)).makePipe().asProcessor().process(null) + ); assertEquals("A string/char is required; received [5]", siae.getMessage()); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new Substring(EMPTY, l("foobarbar"), l(1), l("baz")).makePipe().asProcessor().process(null)); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Substring(EMPTY, l("foobarbar"), l(1), l("baz")).makePipe().asProcessor().process(null) + ); assertEquals("A fixed point number is required for [length]; received [java.lang.String]", siae.getMessage()); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new Substring(EMPTY, l("foobarbar"), l("bar"), l(3)).makePipe().asProcessor().process(null)); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Substring(EMPTY, l("foobarbar"), l("bar"), l(3)).makePipe().asProcessor().process(null) + ); assertEquals("A fixed point number is required for [start]; received [java.lang.String]", siae.getMessage()); assertEquals("f", new Substring(EMPTY, l("foobarbar"), l(Integer.MIN_VALUE + 1), l(1)).makePipe().asProcessor().process(null)); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new Substring(EMPTY, l("foobarbar"), l(Integer.MIN_VALUE), l(1)).makePipe().asProcessor().process(null)); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Substring(EMPTY, l("foobarbar"), l(Integer.MIN_VALUE), l(1)).makePipe().asProcessor().process(null) + ); assertEquals("[start] out of the allowed range [-2147483647, 2147483647], received [-2147483648]", siae.getMessage()); assertEquals("", new Substring(EMPTY, l("foobarbar"), l(Integer.MAX_VALUE), l(1)).makePipe().asProcessor().process(null)); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new Substring(EMPTY, l("foobarbar"), l((long) Integer.MAX_VALUE + 1), l(1)).makePipe().asProcessor().process(null)); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Substring(EMPTY, l("foobarbar"), l((long) Integer.MAX_VALUE + 1), l(1)).makePipe().asProcessor().process(null) + ); assertEquals("[start] out of the allowed range [-2147483647, 2147483647], received [2147483648]", siae.getMessage()); assertEquals("", new Substring(EMPTY, l("foobarbar"), l(1), l(0)).makePipe().asProcessor().process(null)); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new Substring(EMPTY, l("foobarbar"), l(1), l(-1)).makePipe().asProcessor().process(null)); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Substring(EMPTY, l("foobarbar"), l(1), l(-1)).makePipe().asProcessor().process(null) + ); assertEquals("[length] out of the allowed range [0, 2147483647], received [-1]", siae.getMessage()); assertEquals("foobarbar", new Substring(EMPTY, l("foobarbar"), l(1), l(Integer.MAX_VALUE)).makePipe().asProcessor().process(null)); - siae = expectThrows(SqlIllegalArgumentException.class, - () -> new Substring(EMPTY, l("foobarbar"), l(1), l((long) Integer.MAX_VALUE + 1)).makePipe().asProcessor().process(null)); + siae = expectThrows( + SqlIllegalArgumentException.class, + () -> new Substring(EMPTY, l("foobarbar"), l(1), l((long) Integer.MAX_VALUE + 1)).makePipe().asProcessor().process(null) + ); assertEquals("[length] out of the allowed range [0, 2147483647], received [2147483648]", siae.getMessage()); } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/literal/interval/IntervalsTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/literal/interval/IntervalsTests.java index 93552efd9f345..96dffcfbab190 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/literal/interval/IntervalsTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/literal/interval/IntervalsTests.java @@ -135,8 +135,13 @@ public void testDayToSecond() throws Exception { String value = format(Locale.ROOT, "%s%d %d:%d:%d%s", sign, randomDay, randomHour, randomMinute, randomSecond, millisString); TemporalAmount amount = parseInterval(EMPTY, value, INTERVAL_DAY_TO_SECOND); - assertEquals(maybeNegate(sign, Duration.ofDays(randomDay).plusHours(randomHour).plusMinutes(randomMinute) - .plusSeconds(randomSecond).plusMillis(randomMilli)), amount); + assertEquals( + maybeNegate( + sign, + Duration.ofDays(randomDay).plusHours(randomHour).plusMinutes(randomMinute).plusSeconds(randomSecond).plusMillis(randomMilli) + ), + amount + ); } public void testHourToMinute() throws Exception { @@ -158,8 +163,10 @@ public void testHourToSecond() throws Exception { String value = format(Locale.ROOT, "%s%d:%d:%d%s", sign, randomHour, randomMinute, randomSecond, millisString); TemporalAmount amount = parseInterval(EMPTY, value, INTERVAL_HOUR_TO_SECOND); - assertEquals(maybeNegate(sign, - Duration.ofHours(randomHour).plusMinutes(randomMinute).plusSeconds(randomSecond).plusMillis(randomMilli)), amount); + assertEquals( + maybeNegate(sign, Duration.ofHours(randomHour).plusMinutes(randomMinute).plusSeconds(randomSecond).plusMillis(randomMilli)), + amount + ); } public void testMinuteToSecond() throws Exception { @@ -175,15 +182,20 @@ public void testMinuteToSecond() throws Exception { assertEquals(maybeNegate(sign, Duration.ofMinutes(randomMinute).plusSeconds(randomSecond).plusMillis(randomMilli)), amount); } - // validation public void testYearToMonthTooBig() throws Exception { int randomYear = randomNonNegativeInt(); int randomTooBig = randomIntBetween(12, 9999); String value = format(Locale.ROOT, "%s%d-%d", sign, randomYear, randomTooBig); ParsingException pe = expectThrows(ParsingException.class, () -> parseInterval(EMPTY, value, INTERVAL_YEAR_TO_MONTH)); - assertEquals("line -1:0: Invalid [INTERVAL YEAR TO MONTH] value [" + value + "]: [MONTH] unit has illegal value [" + randomTooBig - + "], expected a positive number up to [11]", pe.getMessage()); + assertEquals( + "line -1:0: Invalid [INTERVAL YEAR TO MONTH] value [" + + value + + "]: [MONTH] unit has illegal value [" + + randomTooBig + + "], expected a positive number up to [11]", + pe.getMessage() + ); } public void testMillisTooBig() throws Exception { @@ -191,8 +203,14 @@ public void testMillisTooBig() throws Exception { int millisTooLarge = 1234; String value = format(Locale.ROOT, "%s%d.%d", sign, randomSeconds, millisTooLarge); ParsingException pe = expectThrows(ParsingException.class, () -> parseInterval(EMPTY, value, INTERVAL_SECOND)); - assertEquals("line -1:0: Invalid [INTERVAL SECOND] value [" + value + "]: [MILLISECOND] unit has illegal value [" + millisTooLarge - + "], expected a positive number up to [999]", pe.getMessage()); + assertEquals( + "line -1:0: Invalid [INTERVAL SECOND] value [" + + value + + "]: [MILLISECOND] unit has illegal value [" + + millisTooLarge + + "], expected a positive number up to [999]", + pe.getMessage() + ); } public void testDayToMinuteTooBig() throws Exception { @@ -201,22 +219,32 @@ public void testDayToMinuteTooBig() throws Exception { int randomMinute = randomInt(59); String value = format(Locale.ROOT, "%s%d %d:%d", sign, randomDay, randomHour, randomMinute); ParsingException pe = expectThrows(ParsingException.class, () -> parseInterval(EMPTY, value, INTERVAL_DAY_TO_MINUTE)); - assertEquals("line -1:0: Invalid [INTERVAL DAY TO MINUTE] value [" + value + "]: [HOUR] unit has illegal value [" + randomHour - + "], expected a positive number up to [23]", pe.getMessage()); + assertEquals( + "line -1:0: Invalid [INTERVAL DAY TO MINUTE] value [" + + value + + "]: [HOUR] unit has illegal value [" + + randomHour + + "], expected a positive number up to [23]", + pe.getMessage() + ); } public void testIncompleteYearToMonthInterval() throws Exception { String value = "123-"; ParsingException pe = expectThrows(ParsingException.class, () -> parseInterval(EMPTY, value, INTERVAL_YEAR_TO_MONTH)); - assertEquals("line -1:0: Invalid [INTERVAL YEAR TO MONTH] value [123-]: incorrect format, expecting [numeric]-[numeric]", - pe.getMessage()); + assertEquals( + "line -1:0: Invalid [INTERVAL YEAR TO MONTH] value [123-]: incorrect format, expecting [numeric]-[numeric]", + pe.getMessage() + ); } public void testIncompleteDayToHourInterval() throws Exception { String value = "123 23:"; ParsingException pe = expectThrows(ParsingException.class, () -> parseInterval(EMPTY, value, INTERVAL_DAY_TO_HOUR)); - assertEquals("line -1:0: Invalid [INTERVAL DAY TO HOUR] value [123 23:]: unexpected trailing characters found [:]", - pe.getMessage()); + assertEquals( + "line -1:0: Invalid [INTERVAL DAY TO HOUR] value [123 23:]: unexpected trailing characters found [:]", + pe.getMessage() + ); } public void testExtraCharLeading() throws Exception { diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/parser/ParameterTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/parser/ParameterTests.java index ced9dce2dac67..505dfc252d194 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/parser/ParameterTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/parser/ParameterTests.java @@ -29,10 +29,10 @@ public class ParameterTests extends ESTestCase { public void testSingleParameter() { - Expression expression = new SqlParser().createExpression("a = \n?", - Collections.singletonList( - new SqlTypedParamValue(KEYWORD.typeName(), "foo") - )); + Expression expression = new SqlParser().createExpression( + "a = \n?", + Collections.singletonList(new SqlTypedParamValue(KEYWORD.typeName(), "foo")) + ); logger.info(expression); assertThat(expression, instanceOf(Equals.class)); Expression right = ((Equals) expression).right(); @@ -44,10 +44,15 @@ public void testSingleParameter() { } public void testMultipleParameters() { - Expression expression = new SqlParser().createExpression("(? + ? * ?) - ?", Arrays.asList( - new SqlTypedParamValue(LONG.typeName(), 1L), new SqlTypedParamValue(LONG.typeName(), 2L), - new SqlTypedParamValue(LONG.typeName(), 3L), new SqlTypedParamValue(LONG.typeName(), 4L) - )); + Expression expression = new SqlParser().createExpression( + "(? + ? * ?) - ?", + Arrays.asList( + new SqlTypedParamValue(LONG.typeName(), 1L), + new SqlTypedParamValue(LONG.typeName(), 2L), + new SqlTypedParamValue(LONG.typeName(), 3L), + new SqlTypedParamValue(LONG.typeName(), 4L) + ) + ); assertThat(expression, instanceOf(Sub.class)); Sub sub = (Sub) expression; assertThat(((Literal) sub.right()).value(), equalTo(4L)); @@ -61,11 +66,17 @@ public void testMultipleParameters() { } public void testNotEnoughParameters() { - ParsingException ex = expectThrows(ParsingException.class, - () -> new SqlParser().createExpression("(? + ? * ?) - ?", Arrays.asList( - new SqlTypedParamValue(LONG.typeName(), 1L), new SqlTypedParamValue(LONG.typeName(), 2L), - new SqlTypedParamValue(LONG.typeName(), 3L) - ))); + ParsingException ex = expectThrows( + ParsingException.class, + () -> new SqlParser().createExpression( + "(? + ? * ?) - ?", + Arrays.asList( + new SqlTypedParamValue(LONG.typeName(), 1L), + new SqlTypedParamValue(LONG.typeName(), 2L), + new SqlTypedParamValue(LONG.typeName(), 3L) + ) + ) + ); assertThat(ex.getMessage(), containsString("Not enough actual parameters")); } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/parser/QuotingTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/parser/QuotingTests.java index 52355e6474a89..df1e9ab5b391b 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/parser/QuotingTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/parser/QuotingTests.java @@ -71,8 +71,7 @@ public void testQuotedAttribute() { public void testBackQuotedAttribute() { String quote = "`"; String name = "@timestamp"; - ParsingException ex = expectThrows(ParsingException.class, () -> - new SqlParser().createExpression(quote + name + quote)); + ParsingException ex = expectThrows(ParsingException.class, () -> new SqlParser().createExpression(quote + name + quote)); assertThat(ex.getMessage(), equalTo("line 1:1: backquoted identifiers not supported; please use double quotes instead")); } @@ -88,13 +87,14 @@ public void testQuotedAttributeAndQualifier() { assertThat(ua.qualifier(), is(nullValue())); } - public void testBackQuotedAttributeAndQualifier() { String quote = "`"; String qualifier = "table"; String name = "@timestamp"; - ParsingException ex = expectThrows(ParsingException.class, () -> - new SqlParser().createExpression(quote + qualifier + quote + "." + quote + name + quote)); + ParsingException ex = expectThrows( + ParsingException.class, + () -> new SqlParser().createExpression(quote + qualifier + quote + "." + quote + name + quote) + ); assertThat(ex.getMessage(), equalTo("line 1:1: backquoted identifiers not supported; please use double quotes instead")); } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/predicate/conditional/CaseTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/predicate/conditional/CaseTests.java index 41ccd5fd7e55d..6cab6591fcee3 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/predicate/conditional/CaseTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/predicate/conditional/CaseTests.java @@ -41,9 +41,13 @@ public static Case randomCase() { int noConditionals = randomIntBetween(1, 5); List expressions = new ArrayList<>(noConditionals + 1); for (int i = 0; i < noConditionals; i++) { - expressions.add(new IfConditional( - randomSource(), new Equals(randomSource(), randomStringLiteral(), randomStringLiteral(), randomZone()), - randomIntLiteral())); + expressions.add( + new IfConditional( + randomSource(), + new Equals(randomSource(), randomStringLiteral(), randomStringLiteral(), randomZone()), + randomIntLiteral() + ) + ); } // default else @@ -72,8 +76,10 @@ public void testTransform() { Case c = randomCase(); Source newSource = randomValueOtherThan(c.source(), SourceTests::randomSource); - assertEquals(new Case(c.source(), c.children()), - c.transformPropertiesOnly(Object.class, p -> Objects.equals(p, c.source()) ? newSource: p)); + assertEquals( + new Case(c.source(), c.children()), + c.transformPropertiesOnly(Object.class, p -> Objects.equals(p, c.source()) ? newSource : p) + ); } @Override @@ -88,34 +94,36 @@ public void testDataTypes() { // CASE WHEN 1 = 1 THEN NULL // ELSE 'default' // END - Case c = new Case(EMPTY, Arrays.asList( - new IfConditional(EMPTY, equalsOf(literal(1), literal(1)), Literal.NULL), literal("default"))); + Case c = new Case( + EMPTY, + Arrays.asList(new IfConditional(EMPTY, equalsOf(literal(1), literal(1)), Literal.NULL), literal("default")) + ); assertEquals(KEYWORD, c.dataType()); // CASE WHEN 1 = 1 THEN 'foo' // ELSE NULL // END - c = new Case(EMPTY, Arrays.asList( - new IfConditional(EMPTY, equalsOf(literal(1), literal(1)), literal("foo")), - Literal.NULL)); + c = new Case(EMPTY, Arrays.asList(new IfConditional(EMPTY, equalsOf(literal(1), literal(1)), literal("foo")), Literal.NULL)); assertEquals(KEYWORD, c.dataType()); // CASE WHEN 1 = 1 THEN NULL // ELSE NULL // END - c = new Case(EMPTY, Arrays.asList( - new IfConditional(EMPTY, equalsOf(literal(1), literal(1)), Literal.NULL), - Literal.NULL)); + c = new Case(EMPTY, Arrays.asList(new IfConditional(EMPTY, equalsOf(literal(1), literal(1)), Literal.NULL), Literal.NULL)); assertEquals(NULL, c.dataType()); // CASE WHEN 1 = 1 THEN NULL - // WHEN 2 = 2 THEN 'foo' + // WHEN 2 = 2 THEN 'foo' // ELSE NULL // END - c = new Case(EMPTY, Arrays.asList( + c = new Case( + EMPTY, + Arrays.asList( new IfConditional(EMPTY, equalsOf(literal(1), literal(1)), Literal.NULL), new IfConditional(EMPTY, equalsOf(literal(2), literal(2)), literal("foo")), - Literal.NULL)); + Literal.NULL + ) + ); assertEquals(KEYWORD, c.dataType()); } @@ -135,9 +143,13 @@ private List mutateChildren(Case c) { int rndIdx = randomInt(c.conditions().size()); for (int i = 0; i < c.conditions().size(); i++) { if (i == rndIdx) { - expressions.add(new IfConditional(randomValueOtherThan(c.conditions().get(i).source(), SourceTests::randomSource), - new Equals(randomSource(), randomStringLiteral(), randomStringLiteral(), randomZone()), - randomValueOtherThan(c.conditions().get(i).condition(), FunctionTestUtils::randomStringLiteral))); + expressions.add( + new IfConditional( + randomValueOtherThan(c.conditions().get(i).source(), SourceTests::randomSource), + new Equals(randomSource(), randomStringLiteral(), randomStringLiteral(), randomZone()), + randomValueOtherThan(c.conditions().get(i).condition(), FunctionTestUtils::randomStringLiteral) + ) + ); } else { expressions.add(c.conditions().get(i)); } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/predicate/conditional/IifTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/predicate/conditional/IifTests.java index c9d5cb39ec795..64a5aa4ca971d 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/predicate/conditional/IifTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/predicate/conditional/IifTests.java @@ -35,8 +35,12 @@ public class IifTests extends AbstractNodeTestCase { public static Iif randomIif() { - return new Iif(randomSource(), new Equals(randomSource(), randomStringLiteral(), randomStringLiteral(), randomZone()), - randomIntLiteral(), randomIntLiteral()); + return new Iif( + randomSource(), + new Equals(randomSource(), randomStringLiteral(), randomStringLiteral(), randomZone()), + randomIntLiteral(), + randomIntLiteral() + ); } @Override @@ -53,8 +57,12 @@ protected Iif mutate(Iif instance) { @Override protected Iif copy(Iif instance) { - return new Iif(instance.source(), instance.conditions().get(0).condition(), instance.conditions().get(0).result(), - instance.elseResult()); + return new Iif( + instance.source(), + instance.conditions().get(0).condition(), + instance.conditions().get(0).result(), + instance.elseResult() + ); } @Override @@ -62,8 +70,10 @@ public void testTransform() { Iif iif = randomIif(); Source newSource = randomValueOtherThan(iif.source(), SourceTests::randomSource); - assertEquals(new Iif(iif.source(), iif.conditions().get(0).condition(), iif.conditions().get(0).result(), iif.elseResult()), - iif.transformPropertiesOnly(Object.class, p -> Objects.equals(p, iif.source()) ? newSource: p)); + assertEquals( + new Iif(iif.source(), iif.conditions().get(0).condition(), iif.conditions().get(0).result(), iif.elseResult()), + iif.transformPropertiesOnly(Object.class, p -> Objects.equals(p, iif.source()) ? newSource : p) + ); } @Override @@ -71,9 +81,10 @@ public void testReplaceChildren() { Iif iif = randomIif(); List newChildren = mutateChildren(iif); - assertEquals(new Iif(iif.source(), newChildren.get(0), newChildren.get(1), newChildren.get(2)), - iif.replaceChildren(Arrays.asList(new IfConditional(iif.source(), newChildren.get(0), newChildren.get(1)), - newChildren.get(2)))); + assertEquals( + new Iif(iif.source(), newChildren.get(0), newChildren.get(1), newChildren.get(2)), + iif.replaceChildren(Arrays.asList(new IfConditional(iif.source(), newChildren.get(0), newChildren.get(1)), newChildren.get(2))) + ); } public void testConditionFolded() { @@ -86,10 +97,14 @@ public void testConditionFolded() { private List mutateChildren(Iif iif) { List expressions = new ArrayList<>(3); Equals eq = (Equals) iif.conditions().get(0).condition(); - expressions.add(new Equals(randomSource(), - randomValueOtherThan(eq.left(), FunctionTestUtils::randomStringLiteral), - randomValueOtherThan(eq.right(), FunctionTestUtils::randomStringLiteral), - randomValueOtherThan(eq.zoneId(), ESTestCase::randomZone))); + expressions.add( + new Equals( + randomSource(), + randomValueOtherThan(eq.left(), FunctionTestUtils::randomStringLiteral), + randomValueOtherThan(eq.right(), FunctionTestUtils::randomStringLiteral), + randomValueOtherThan(eq.zoneId(), ESTestCase::randomZone) + ) + ); expressions.add(randomValueOtherThan(iif.conditions().get(0).result(), FunctionTestUtils::randomIntLiteral)); expressions.add(randomValueOtherThan(iif.elseResult(), FunctionTestUtils::randomIntLiteral)); return expressions; diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/predicate/operator/arithmetic/SqlBinaryArithmeticTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/predicate/operator/arithmetic/SqlBinaryArithmeticTests.java index 25256daa0a4ab..dd7ae3677e10c 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/predicate/operator/arithmetic/SqlBinaryArithmeticTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/predicate/operator/arithmetic/SqlBinaryArithmeticTests.java @@ -295,8 +295,9 @@ private static Literal L(Object value) { } private static Literal interval(TemporalAmount value, DataType intervalType) { - Object i = value instanceof Period ? new IntervalYearMonth((Period) value, intervalType) - : new IntervalDayTime((Duration) value, intervalType); + Object i = value instanceof Period + ? new IntervalYearMonth((Period) value, intervalType) + : new IntervalDayTime((Duration) value, intervalType); return new Literal(EMPTY, i, SqlDataTypes.fromJava(i)); } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/optimizer/OptimizerRunTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/optimizer/OptimizerRunTests.java index 481c0e80d2653..c56a2eaeae34d 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/optimizer/OptimizerRunTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/optimizer/OptimizerRunTests.java @@ -145,7 +145,7 @@ public void testSimplifyComparisonArithmeticSkippedOnIntegerArithmeticalOverflow public void testSimplifyComparisonArithmeticSkippedOnFloatingPointArithmeticalOverflow() { assertNotSimplified("float / 10 " + randomBinaryComparison() + " " + Float.MAX_VALUE); - assertNotSimplified("float / " + Float.MAX_VALUE +" " + randomBinaryComparison() + " 10"); + assertNotSimplified("float / " + Float.MAX_VALUE + " " + randomBinaryComparison() + " 10"); assertNotSimplified("float / 10 " + randomBinaryComparison() + " " + Double.MAX_VALUE); assertNotSimplified("float / " + Double.MAX_VALUE + " " + randomBinaryComparison() + " 10"); // note: the "reversed" test (i.e.: MAX_VALUE / float < literal) would require a floating literal, which is skipped for other @@ -190,26 +190,38 @@ public void testSimplifyComparisonArithmeticSkippedOnFloats() { for (Tuple nr : List.of(new Tuple<>(.4, 1), new Tuple<>(1, .4))) { assertNotSimplified(field + " + " + nr.v1() + " " + randomBinaryComparison() + " " + nr.v2()); assertNotSimplified(field + " - " + nr.v1() + " " + randomBinaryComparison() + " " + nr.v2()); - assertNotSimplified(nr.v1()+ " + " + field + " " + randomBinaryComparison() + " " + nr.v2()); - assertNotSimplified(nr.v1()+ " - " + field + " " + randomBinaryComparison() + " " + nr.v2()); + assertNotSimplified(nr.v1() + " + " + field + " " + randomBinaryComparison() + " " + nr.v2()); + assertNotSimplified(nr.v1() + " - " + field + " " + randomBinaryComparison() + " " + nr.v2()); } } } public void testSimplifyComparisonArithmeticWithDateTime() { - doTestSimplifyComparisonArithmetics("date - INTERVAL 1 MONTH > '2010-01-01T01:01:01'::DATETIME", "date", ">", - ZonedDateTime.parse("2010-02-01T01:01:01Z")); + doTestSimplifyComparisonArithmetics( + "date - INTERVAL 1 MONTH > '2010-01-01T01:01:01'::DATETIME", + "date", + ">", + ZonedDateTime.parse("2010-02-01T01:01:01Z") + ); } public void testSimplifyComparisonArithmeticWithDate() { - doTestSimplifyComparisonArithmetics("date + INTERVAL 1 YEAR <= '2011-01-01T00:00:00'::DATE", "date", "<=", - ZonedDateTime.parse("2010-01-01T00:00:00Z")); + doTestSimplifyComparisonArithmetics( + "date + INTERVAL 1 YEAR <= '2011-01-01T00:00:00'::DATE", + "date", + "<=", + ZonedDateTime.parse("2010-01-01T00:00:00Z") + ); } public void testSimplifyComparisonArithmeticWithDateAndMultiplication() { // the multiplication should be folded, but check - doTestSimplifyComparisonArithmetics("date + 2 * INTERVAL 1 YEAR <= '2012-01-01T00:00:00'::DATE", "date", "<=", - ZonedDateTime.parse("2010-01-01T00:00:00Z")); + doTestSimplifyComparisonArithmetics( + "date + 2 * INTERVAL 1 YEAR <= '2012-01-01T00:00:00'::DATE", + "date", + "<=", + ZonedDateTime.parse("2010-01-01T00:00:00Z") + ); } private void doTestSimplifyComparisonArithmetics(String expression, String fieldName, String compSymbol, Object bound) { @@ -252,9 +264,10 @@ private LogicalPlan planWithArithmeticCondition(String condition) { } private static void assertSemanticMatching(Expression fieldAttributeExp, Expression unresolvedAttributeExp) { - Expression unresolvedUpdated = unresolvedAttributeExp - .transformUp(LITERALS_ON_THE_RIGHT.expressionToken(), LITERALS_ON_THE_RIGHT::rule) - .transformUp(x -> x.foldable() ? new Literal(x.source(), x.fold(), x.dataType()) : x); + Expression unresolvedUpdated = unresolvedAttributeExp.transformUp( + LITERALS_ON_THE_RIGHT.expressionToken(), + LITERALS_ON_THE_RIGHT::rule + ).transformUp(x -> x.foldable() ? new Literal(x.source(), x.fold(), x.dataType()) : x); List resolvedFields = fieldAttributeExp.collectFirstChildren(x -> x instanceof FieldAttribute); for (Expression field : resolvedFields) { diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/optimizer/OptimizerTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/optimizer/OptimizerTests.java index 3b52989a786c2..74759812bbd7c 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/optimizer/OptimizerTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/optimizer/OptimizerTests.java @@ -55,8 +55,8 @@ import org.elasticsearch.xpack.ql.type.EsField; import org.elasticsearch.xpack.ql.util.CollectionUtils; import org.elasticsearch.xpack.ql.util.StringUtils; -import org.elasticsearch.xpack.sql.analysis.analyzer.Analyzer.ReplaceSubQueryAliases; import org.elasticsearch.xpack.sql.analysis.analyzer.Analyzer.PruneSubQueryAliases; +import org.elasticsearch.xpack.sql.analysis.analyzer.Analyzer.ReplaceSubQueryAliases; import org.elasticsearch.xpack.sql.expression.function.aggregate.Avg; import org.elasticsearch.xpack.sql.expression.function.aggregate.ExtendedStats; import org.elasticsearch.xpack.sql.expression.function.aggregate.First; @@ -275,20 +275,25 @@ public void testConstantFoldingDatetime() { assertEquals(19, foldFunction(new DayOfMonth(EMPTY, cast, UTC))); assertEquals(19, foldFunction(new DayOfYear(EMPTY, cast, UTC))); assertEquals(3, foldFunction(new IsoWeekOfYear(EMPTY, cast, UTC))); - assertNull(foldFunction( - new IsoWeekOfYear(EMPTY, new Literal(EMPTY, null, DataTypes.NULL), UTC))); + assertNull(foldFunction(new IsoWeekOfYear(EMPTY, new Literal(EMPTY, null, DataTypes.NULL), UTC))); } public void testConstantFoldingIn() { - In in = new In(EMPTY, ONE, - asList(ONE, TWO, ONE, THREE, new Sub(EMPTY, THREE, ONE), ONE, FOUR, new Abs(EMPTY, new Sub(EMPTY, TWO, FIVE)))); - Literal result= (Literal) new ConstantFolding().rule(in); + In in = new In( + EMPTY, + ONE, + asList(ONE, TWO, ONE, THREE, new Sub(EMPTY, THREE, ONE), ONE, FOUR, new Abs(EMPTY, new Sub(EMPTY, TWO, FIVE))) + ); + Literal result = (Literal) new ConstantFolding().rule(in); assertEquals(true, result.value()); } public void testConstantFoldingIn_LeftValueNotFoldable() { - In in = new In(EMPTY, getFieldAttribute(), - asList(ONE, TWO, ONE, THREE, new Sub(EMPTY, THREE, ONE), ONE, FOUR, new Abs(EMPTY, new Sub(EMPTY, TWO, FIVE)))); + In in = new In( + EMPTY, + getFieldAttribute(), + asList(ONE, TWO, ONE, THREE, new Sub(EMPTY, THREE, ONE), ONE, FOUR, new Abs(EMPTY, new Sub(EMPTY, TWO, FIVE))) + ); Alias as = new Alias(in.source(), in.sourceText(), in); Project p = new Project(EMPTY, FROM(), Collections.singletonList(as)); p = (Project) new ConstantFolding().apply(p); @@ -300,13 +305,13 @@ public void testConstantFoldingIn_LeftValueNotFoldable() { public void testConstantFoldingIn_RightValueIsNull() { In in = new In(EMPTY, getFieldAttribute(), asList(NULL, NULL)); - Literal result= (Literal) new ConstantFolding().rule(in); + Literal result = (Literal) new ConstantFolding().rule(in); assertNull(result.value()); } public void testConstantFoldingIn_LeftValueIsNull() { In in = new In(EMPTY, NULL, asList(ONE, TWO, THREE)); - Literal result= (Literal) new ConstantFolding().rule(in); + Literal result = (Literal) new ConstantFolding().rule(in); assertNull(result.value()); } @@ -349,8 +354,10 @@ public void testNullFoldingIsNullWithCast() { final IsNull isNullOpt = (IsNull) foldNull.rule(isNull); assertEquals(isNull, isNullOpt); - QlIllegalArgumentException sqlIAE = expectThrows(QlIllegalArgumentException.class, - () -> isNullOpt.asPipe().asProcessor().process(null)); + QlIllegalArgumentException sqlIAE = expectThrows( + QlIllegalArgumentException.class, + () -> isNullOpt.asPipe().asProcessor().process(null) + ); assertEquals("cannot cast [foo] to [date]: Text 'foo' could not be parsed at index 0", sqlIAE.getMessage()); isNull = new IsNull(EMPTY, new Cast(EMPTY, NULL, randomFrom(DataTypes.types()))); @@ -375,8 +382,10 @@ public void testNullFoldingIsNotNullWithCast() { final IsNotNull isNotNullOpt = (IsNotNull) foldNull.rule(isNotNull); assertEquals(isNotNull, isNotNullOpt); - QlIllegalArgumentException sqlIAE = expectThrows(QlIllegalArgumentException.class, - () -> isNotNullOpt.asPipe().asProcessor().process(null)); + QlIllegalArgumentException sqlIAE = expectThrows( + QlIllegalArgumentException.class, + () -> isNotNullOpt.asPipe().asProcessor().process(null) + ); assertEquals("cannot cast [foo] to [date]: Text 'foo' could not be parsed at index 0", sqlIAE.getMessage()); isNotNull = new IsNotNull(EMPTY, new Cast(EMPTY, NULL, randomFrom(DataTypes.types()))); @@ -430,8 +439,7 @@ public void testNullFoldingDoesNotApplyOnLogicalExpressions() { public void testNullFoldingDoesNotApplyOnConditionals() throws Exception { FoldNull rule = new FoldNull(); - Class clazz = - (Class) randomFrom(IfNull.class, NullIf.class); + Class clazz = (Class) randomFrom(IfNull.class, NullIf.class); Constructor ctor = clazz.getConstructor(Source.class, Expression.class, Expression.class); ConditionalFunction conditionalFunction = ctor.newInstance(EMPTY, NULL, ONE); assertEquals(conditionalFunction, rule.rule(conditionalFunction)); @@ -445,8 +453,11 @@ public void testNullFoldingDoesNotApplyOnConditionals() throws Exception { public void testNullFoldingDoesNotApplyOnArbitraryConditionals() throws Exception { FoldNull rule = new FoldNull(); - Class clazz = - (Class) randomFrom(Coalesce.class, Greatest.class, Least.class); + Class clazz = (Class) randomFrom( + Coalesce.class, + Greatest.class, + Least.class + ); Constructor ctor = clazz.getConstructor(Source.class, List.class); ArbitraryConditionalFunction conditionalFunction = ctor.newInstance(EMPTY, asList(NULL, ONE, TWO)); assertEquals(conditionalFunction, rule.rule(conditionalFunction)); @@ -470,10 +481,12 @@ public void testSimplifyCoalesceSameExpression() { } public void testSimplifyCoalesceRandomNullsWithValue() { - Expression e = new SimplifyConditional().rule(new Coalesce(EMPTY, - CollectionUtils.combine( - CollectionUtils.combine(randomListOfNulls(), TRUE, FALSE, TRUE), - randomListOfNulls()))); + Expression e = new SimplifyConditional().rule( + new Coalesce( + EMPTY, + CollectionUtils.combine(CollectionUtils.combine(randomListOfNulls(), TRUE, FALSE, TRUE), randomListOfNulls()) + ) + ); assertEquals(1, e.children().size()); assertEquals(TRUE, e.children().get(0)); assertEquals(BOOLEAN, e.dataType()); @@ -484,9 +497,7 @@ private List randomListOfNulls() { } public void testSimplifyCoalesceFirstLiteral() { - Expression e = new SimplifyConditional() - .rule(new Coalesce(EMPTY, - asList(NULL, TRUE, FALSE, new Abs(EMPTY, getFieldAttribute())))); + Expression e = new SimplifyConditional().rule(new Coalesce(EMPTY, asList(NULL, TRUE, FALSE, new Abs(EMPTY, getFieldAttribute())))); assertEquals(Coalesce.class, e.getClass()); assertEquals(1, e.children().size()); assertEquals(TRUE, e.children().get(0)); @@ -537,8 +548,9 @@ public void testSimplifyGreatestRandomNulls() { } public void testSimplifyGreatestRandomNullsWithValue() { - Expression e = new SimplifyConditional().rule(new Greatest(EMPTY, - CollectionUtils.combine(CollectionUtils.combine(randomListOfNulls(), ONE, TWO, ONE), randomListOfNulls()))); + Expression e = new SimplifyConditional().rule( + new Greatest(EMPTY, CollectionUtils.combine(CollectionUtils.combine(randomListOfNulls(), ONE, TWO, ONE), randomListOfNulls())) + ); assertEquals(Greatest.class, e.getClass()); assertEquals(2, e.children().size()); assertEquals(ONE, e.children().get(0)); @@ -559,8 +571,9 @@ public void testSimplifyLeastRandomNulls() { } public void testSimplifyLeastRandomNullsWithValue() { - Expression e = new SimplifyConditional().rule(new Least(EMPTY, - CollectionUtils.combine(CollectionUtils.combine(randomListOfNulls(), ONE, TWO, ONE), randomListOfNulls()))); + Expression e = new SimplifyConditional().rule( + new Least(EMPTY, CollectionUtils.combine(CollectionUtils.combine(randomListOfNulls(), ONE, TWO, ONE), randomListOfNulls())) + ); assertEquals(Least.class, e.getClass()); assertEquals(2, e.children().size()); assertEquals(ONE, e.children().get(0)); @@ -663,24 +676,29 @@ public void testFoldNullDateTrunc() { public void testSimplifyCaseConditionsFoldWhenFalse() { // CASE WHEN a = 1 THEN 'foo1' - // WHEN 1 = 2 THEN 'bar1' - // WHEN 2 = 1 THEN 'bar2' - // WHEN a > 1 THEN 'foo2' + // WHEN 1 = 2 THEN 'bar1' + // WHEN 2 = 1 THEN 'bar2' + // WHEN a > 1 THEN 'foo2' // ELSE 'default' // END // // ==> // // CASE WHEN a = 1 THEN 'foo1' - // WHEN a > 1 THEN 'foo2' + // WHEN a > 1 THEN 'foo2' // ELSE 'default' // END - Case c = new Case(EMPTY, asList( + Case c = new Case( + EMPTY, + asList( new IfConditional(EMPTY, equalsOf(getFieldAttribute(), ONE), literal("foo1")), new IfConditional(EMPTY, equalsOf(ONE, TWO), literal("bar1")), new IfConditional(EMPTY, equalsOf(TWO, ONE), literal("bar2")), - new IfConditional(EMPTY, greaterThanOf(getFieldAttribute(), ONE), literal("foo2")), literal("default"))); + new IfConditional(EMPTY, greaterThanOf(getFieldAttribute(), ONE), literal("foo2")), + literal("default") + ) + ); assertFalse(c.foldable()); Expression e = new SimplifyCase().rule(c); assertEquals(Case.class, e.getClass()); @@ -694,7 +712,7 @@ public void testSimplifyCaseConditionsFoldWhenFalse() { public void testSimplifyCaseConditionsFoldCompletely_FoldableElse() { // CASE WHEN 1 = 2 THEN 'foo1' - // WHEN 1 = 1 THEN 'foo2' + // WHEN 1 = 1 THEN 'foo2' // ELSE 'default' // END // @@ -702,9 +720,14 @@ public void testSimplifyCaseConditionsFoldCompletely_FoldableElse() { // // 'foo2' - Case c = new Case(EMPTY, asList( + Case c = new Case( + EMPTY, + asList( new IfConditional(EMPTY, equalsOf(ONE, TWO), literal("foo1")), - new IfConditional(EMPTY, equalsOf(ONE, ONE), literal("foo2")), literal("default"))); + new IfConditional(EMPTY, equalsOf(ONE, ONE), literal("foo2")), + literal("default") + ) + ); assertFalse(c.foldable()); SimplifyCase rule = new SimplifyCase(); @@ -727,9 +750,7 @@ public void testSimplifyCaseConditionsFoldCompletely_NonFoldableElse() { // // myField (non-foldable) - Case c = new Case(EMPTY, asList( - new IfConditional(EMPTY, equalsOf(ONE, TWO), literal("foo1")), - getFieldAttribute("myField"))); + Case c = new Case(EMPTY, asList(new IfConditional(EMPTY, equalsOf(ONE, TWO), literal("foo1")), getFieldAttribute("myField"))); assertFalse(c.foldable()); SimplifyCase rule = new SimplifyCase(); @@ -827,7 +848,7 @@ public void testBoolSimplifyNotIsNullAndNotIsNotNull() { // Conjunction - // a != NULL AND a > 1 AND a < 5 AND a == 10 -> (a != NULL AND a == 10) AND 1 <= a < 5 + // a != NULL AND a > 1 AND a < 5 AND a == 10 -> (a != NULL AND a == 10) AND 1 <= a < 5 public void testCombineUnbalancedComparisonsMixedWithEqualsIntoRange() { FieldAttribute fa = getFieldAttribute(); IsNotNull isn = new IsNotNull(EMPTY, fa); @@ -853,13 +874,16 @@ public void testCombineUnbalancedComparisonsMixedWithEqualsIntoRange() { public void testTranslateMinToFirst() { Min min1 = new Min(EMPTY, new FieldAttribute(EMPTY, "str", new EsField("str", KEYWORD, emptyMap(), true))); - Min min2 = new Min(EMPTY, getFieldAttribute()); + Min min2 = new Min(EMPTY, getFieldAttribute()); - OrderBy plan = new OrderBy(EMPTY, new Aggregate(EMPTY, FROM(), emptyList(), - asList(a("min1", min1), a("min2", min2))), + OrderBy plan = new OrderBy( + EMPTY, + new Aggregate(EMPTY, FROM(), emptyList(), asList(a("min1", min1), a("min2", min2))), asList( new Order(EMPTY, min1, OrderDirection.ASC, Order.NullsPosition.LAST), - new Order(EMPTY, min2, OrderDirection.ASC, Order.NullsPosition.LAST))); + new Order(EMPTY, min2, OrderDirection.ASC, Order.NullsPosition.LAST) + ) + ); LogicalPlan result = new ReplaceMinMaxWithTopHits().apply(plan); assertTrue(result instanceof OrderBy); List order = ((OrderBy) result).order(); @@ -879,12 +903,16 @@ public void testTranslateMinToFirst() { public void testTranslateMaxToLast() { Max max1 = new Max(EMPTY, new FieldAttribute(EMPTY, "str", new EsField("str", KEYWORD, emptyMap(), true))); - Max max2 = new Max(EMPTY, getFieldAttribute()); + Max max2 = new Max(EMPTY, getFieldAttribute()); - OrderBy plan = new OrderBy(EMPTY, new Aggregate(EMPTY, FROM(), emptyList(), asList(a("max1", max1), a("max2", max2))), + OrderBy plan = new OrderBy( + EMPTY, + new Aggregate(EMPTY, FROM(), emptyList(), asList(a("max1", max1), a("max2", max2))), asList( new Order(EMPTY, max1, OrderDirection.ASC, Order.NullsPosition.LAST), - new Order(EMPTY, max2, OrderDirection.ASC, Order.NullsPosition.LAST))); + new Order(EMPTY, max2, OrderDirection.ASC, Order.NullsPosition.LAST) + ) + ); LogicalPlan result = new ReplaceMinMaxWithTopHits().apply(plan); assertTrue(result instanceof OrderBy); List order = ((OrderBy) result).order(); @@ -903,16 +931,17 @@ public void testTranslateMaxToLast() { public void testSortAggregateOnOrderByWithTwoFields() { FieldAttribute firstField = new FieldAttribute(EMPTY, "first_field", new EsField("first_field", BYTE, emptyMap(), true)); - FieldAttribute secondField = new FieldAttribute(EMPTY, "second_field", - new EsField("second_field", BYTE, emptyMap(), true)); + FieldAttribute secondField = new FieldAttribute(EMPTY, "second_field", new EsField("second_field", BYTE, emptyMap(), true)); Alias firstAlias = new Alias(EMPTY, "first_alias", firstField); Alias secondAlias = new Alias(EMPTY, "second_alias", secondField); Order firstOrderBy = new Order(EMPTY, firstField, OrderDirection.ASC, Order.NullsPosition.LAST); Order secondOrderBy = new Order(EMPTY, secondField, OrderDirection.ASC, Order.NullsPosition.LAST); - OrderBy orderByPlan = new OrderBy(EMPTY, - new Aggregate(EMPTY, FROM(), asList(secondField, firstField), asList(secondAlias, firstAlias)), - asList(firstOrderBy, secondOrderBy)); + OrderBy orderByPlan = new OrderBy( + EMPTY, + new Aggregate(EMPTY, FROM(), asList(secondField, firstField), asList(secondAlias, firstAlias)), + asList(firstOrderBy, secondOrderBy) + ); LogicalPlan result = new SortAggregateOnOrderBy().apply(orderByPlan); assertTrue(result instanceof OrderBy); @@ -935,16 +964,17 @@ public void testSortAggregateOnOrderByWithTwoFields() { public void testSortAggregateOnOrderByOnlyAliases() { FieldAttribute firstField = new FieldAttribute(EMPTY, "first_field", new EsField("first_field", BYTE, emptyMap(), true)); - FieldAttribute secondField = new FieldAttribute(EMPTY, "second_field", - new EsField("second_field", BYTE, emptyMap(), true)); + FieldAttribute secondField = new FieldAttribute(EMPTY, "second_field", new EsField("second_field", BYTE, emptyMap(), true)); Alias firstAlias = new Alias(EMPTY, "first_alias", firstField); Alias secondAlias = new Alias(EMPTY, "second_alias", secondField); Order firstOrderBy = new Order(EMPTY, firstAlias, OrderDirection.ASC, Order.NullsPosition.LAST); Order secondOrderBy = new Order(EMPTY, secondAlias, OrderDirection.ASC, Order.NullsPosition.LAST); - OrderBy orderByPlan = new OrderBy(EMPTY, - new Aggregate(EMPTY, FROM(), asList(secondAlias, firstAlias), asList(secondAlias, firstAlias)), - asList(firstOrderBy, secondOrderBy)); + OrderBy orderByPlan = new OrderBy( + EMPTY, + new Aggregate(EMPTY, FROM(), asList(secondAlias, firstAlias), asList(secondAlias, firstAlias)), + asList(firstOrderBy, secondOrderBy) + ); LogicalPlan result = new SortAggregateOnOrderBy().apply(orderByPlan); assertTrue(result instanceof OrderBy); @@ -1008,15 +1038,18 @@ public void testAggregatesPromoteToStats_WithFullTextPredicatesConditions() { List aggregates; boolean isSimpleStats = randomBoolean(); if (isSimpleStats) { - aggregates = asList(new Avg(EMPTY, aggField), new Sum(EMPTY, aggField), new Min(EMPTY, aggField), - new Max(EMPTY, aggField)); + aggregates = asList(new Avg(EMPTY, aggField), new Sum(EMPTY, aggField), new Min(EMPTY, aggField), new Max(EMPTY, aggField)); } else { aggregates = asList(new StddevPop(EMPTY, aggField), new SumOfSquares(EMPTY, aggField), new VarPop(EMPTY, aggField)); } AggregateFunction firstAggregate = randomFrom(aggregates); AggregateFunction secondAggregate = randomValueOtherThan(firstAggregate, () -> randomFrom(aggregates)); - Aggregate aggregatePlan = new Aggregate(EMPTY, filter, singletonList(matchField), - asList(new Alias(EMPTY, "first", firstAggregate), new Alias(EMPTY, "second", secondAggregate))); + Aggregate aggregatePlan = new Aggregate( + EMPTY, + filter, + singletonList(matchField), + asList(new Alias(EMPTY, "first", firstAggregate), new Alias(EMPTY, "second", secondAggregate)) + ); LogicalPlan result; if (isSimpleStats) { result = new ReplaceAggsWithStats().apply(aggregatePlan); @@ -1064,8 +1097,11 @@ public void testReplaceAttributesWithTarget() { Alias bAlias = new Alias(EMPTY, "bAlias", b); Project p = new Project(EMPTY, FROM(), asList(aAlias, bAlias)); - Filter f = new Filter(EMPTY, p, new And(EMPTY, greaterThanOf(aAlias.toAttribute(), L(1)), - greaterThanOf(bAlias.toAttribute(), L(2)))); + Filter f = new Filter( + EMPTY, + p, + new And(EMPTY, greaterThanOf(aAlias.toAttribute(), L(1)), greaterThanOf(bAlias.toAttribute(), L(2))) + ); ReplaceReferenceAttributeWithSource rule = new ReplaceReferenceAttributeWithSource(); Expression condition = f.condition(); @@ -1141,9 +1177,11 @@ public void testSumIsNotReplacedWithStats() { public void testPushProjectionsIntoLocalRelations() { // SELECT TRUE as a - Project plan = new Project(EMPTY, + Project plan = new Project( + EMPTY, new LocalRelation(EMPTY, new SingletonExecutable(emptyList())), - singletonList(new Alias(EMPTY, "a", TRUE))); + singletonList(new Alias(EMPTY, "a", TRUE)) + ); LogicalPlan optimized = new Optimizer.PushProjectionsIntoLocalRelation().apply(plan); @@ -1152,7 +1190,8 @@ public void testPushProjectionsIntoLocalRelations() { } public void testSkipQueryForOnlyLiteralAggregations() { - Aggregate plan = new Aggregate(EMPTY, + Aggregate plan = new Aggregate( + EMPTY, new EsRelation(EMPTY, new EsIndex("table", emptyMap()), false), emptyList(), singletonList(new Alias(EMPTY, "a", TRUE)) @@ -1168,18 +1207,15 @@ public void testSkipQueryForOnlyLiteralAggregations() { public void testDoNotSkipQueryOnEsRelationWithFilter() { // SELECT TRUE as a FROM table WHERE col IS NULL - Project plan = new Project(EMPTY, - new Filter(EMPTY, - new EsRelation(EMPTY, new EsIndex("table", emptyMap()), false), - new IsNull(EMPTY, getFieldAttribute("col"))), - singletonList(new Alias(EMPTY, "a", TRUE))); + Project plan = new Project( + EMPTY, + new Filter(EMPTY, new EsRelation(EMPTY, new EsIndex("table", emptyMap()), false), new IsNull(EMPTY, getFieldAttribute("col"))), + singletonList(new Alias(EMPTY, "a", TRUE)) + ); LogicalPlan optimized = new Optimizer.SkipQueryForLiteralAggregations().apply(plan); - optimized.forEachDown(LeafPlan.class, l -> { - assertEquals(EsRelation.class, l.getClass()); - }); + optimized.forEachDown(LeafPlan.class, l -> { assertEquals(EsRelation.class, l.getClass()); }); } - } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/parser/EscapedFunctionsTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/parser/EscapedFunctionsTests.java index f5927a30aba6d..502623a3b8d2c 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/parser/EscapedFunctionsTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/parser/EscapedFunctionsTests.java @@ -42,8 +42,11 @@ public class EscapedFunctionsTests extends ESTestCase { private final SqlParser parser = new SqlParser(); private String buildExpression(String escape, String pattern, Object value) { - return format(Locale.ROOT, "{" + randomWhitespaces() + escape + " " + randomWhitespaces() + - pattern + randomWhitespaces() + "}", value); + return format( + Locale.ROOT, + "{" + randomWhitespaces() + escape + " " + randomWhitespaces() + pattern + randomWhitespaces() + "}", + value + ); } private Literal dateLiteral(String date) { @@ -93,9 +96,9 @@ private String buildTime() { private String buildSecsFractionalAndTimezone() { String str = ""; if (randomBoolean()) { - str = ":55" + randomFrom("", ".1", ".12", ".123", ".1234", ".12345", ".123456", - ".1234567", ".12345678", ".123456789") + - randomFrom("", "Z", "Etc/GMT-5", "-05:30", "+04:20"); + str = ":55" + + randomFrom("", ".1", ".12", ".123", ".1234", ".12345", ".123456", ".1234567", ".12345678", ".123456789") + + randomFrom("", "Z", "Etc/GMT-5", "-05:30", "+04:20"); } return str; } @@ -116,15 +119,17 @@ private Limit limit(int limit) { } private LikePattern likeEscape(String like, String character) { - Expression exp = parser.createExpression(format(Locale.ROOT, "exp LIKE '%s' ", like) + - buildExpression("escape", "'%s'", character)); + Expression exp = parser.createExpression( + format(Locale.ROOT, "exp LIKE '%s' ", like) + buildExpression("escape", "'%s'", character) + ); assertThat(exp, instanceOf(Like.class)); return ((Like) exp).pattern(); } private Function function(String name) { Expression exp = parser.createExpression( - format(Locale.ROOT, "{" + randomWhitespaces() + "fn" + randomWhitespaces() + "%s" + randomWhitespaces() + "}", name)); + format(Locale.ROOT, "{" + randomWhitespaces() + "fn" + randomWhitespaces() + "%s" + randomWhitespaces() + "}", name) + ); assertThat(exp, instanceOf(Function.class)); return (Function) exp; } @@ -133,6 +138,7 @@ private void assertFunction(String name, String result) { String escapedName = name.replace("(", "\\(").replace(")", "\\)").replace("{", "\\{").replace("}", "\\}"); assertThat(result, matchesPattern("\\{\\s*fn\\s*" + escapedName + "\\s*}")); } + public void testFunctionNoArg() { Function f = function("SCORE()"); assertFunction("SCORE()", f.sourceText()); @@ -198,9 +204,14 @@ public void testFunctionWithFunctionWithArg() { public void testFunctionWithFunctionWithArgAndParams() { String e = "POWER(?, {fn POWER({fn ABS(?)}, {fN ABS(?)})})"; - Function f = (Function) parser.createExpression(e, - asList(new SqlTypedParamValue(LONG.typeName(), 1), new SqlTypedParamValue(LONG.typeName(), 1), - new SqlTypedParamValue(LONG.typeName(), 1))); + Function f = (Function) parser.createExpression( + e, + asList( + new SqlTypedParamValue(LONG.typeName(), 1), + new SqlTypedParamValue(LONG.typeName(), 1), + new SqlTypedParamValue(LONG.typeName(), 1) + ) + ); assertEquals(e, f.sourceText()); assertEquals(2, f.arguments().size()); @@ -229,9 +240,11 @@ public void testDateLiteral() { public void testDateLiteralValidation() { ParsingException ex = expectThrows(ParsingException.class, () -> dateLiteral("2012-13-01")); - assertEquals("line 1:2: Invalid date received; Text '2012-13-01' could not be parsed: " + - "Invalid value for MonthOfYear (valid values 1 - 12): 13", - ex.getMessage()); + assertEquals( + "line 1:2: Invalid date received; Text '2012-13-01' could not be parsed: " + + "Invalid value for MonthOfYear (valid values 1 - 12): 13", + ex.getMessage() + ); } public void testTimeLiteral() { @@ -241,9 +254,11 @@ public void testTimeLiteral() { public void testTimeLiteralValidation() { ParsingException ex = expectThrows(ParsingException.class, () -> timeLiteral("10:10:65")); - assertEquals("line 1:2: Invalid time received; Text '10:10:65' could not be parsed: " + - "Invalid value for SecondOfMinute (valid values 0 - 59): 65", - ex.getMessage()); + assertEquals( + "line 1:2: Invalid time received; Text '10:10:65' could not be parsed: " + + "Invalid value for SecondOfMinute (valid values 0 - 59): 65", + ex.getMessage() + ); } public void testTimestampLiteral() { @@ -255,20 +270,31 @@ public void testTimestampLiteral() { public void testTimestampLiteralValidation() { String date = buildDate(); - ParsingException ex = expectThrows(ParsingException.class, () -> timestampLiteral(date+ "_AB 10:01:02.3456")); + ParsingException ex = expectThrows(ParsingException.class, () -> timestampLiteral(date + "_AB 10:01:02.3456")); assertEquals( - "line 1:2: Invalid timestamp received; Text '" + date + "_AB 10:01:02.3456' could not be parsed, " + - "unparsed text found at index " + date.length(), - ex.getMessage()); + "line 1:2: Invalid timestamp received; Text '" + + date + + "_AB 10:01:02.3456' could not be parsed, " + + "unparsed text found at index " + + date.length(), + ex.getMessage() + ); ex = expectThrows(ParsingException.class, () -> timestampLiteral("20120101_AB 10:01:02.3456")); assertEquals( - "line 1:2: Invalid timestamp received; Text '20120101_AB 10:01:02.3456' could not be parsed at index 0", - ex.getMessage()); + "line 1:2: Invalid timestamp received; Text '20120101_AB 10:01:02.3456' could not be parsed at index 0", + ex.getMessage() + ); ex = expectThrows(ParsingException.class, () -> timestampLiteral(date)); - assertThat(ex.getMessage(), startsWith( - "line 1:2: Invalid timestamp received; Text '" + date + "' could not be parsed: " + - "Unable to obtain ZonedDateTime from TemporalAccessor")); + assertThat( + ex.getMessage(), + startsWith( + "line 1:2: Invalid timestamp received; Text '" + + date + + "' could not be parsed: " + + "Unable to obtain ZonedDateTime from TemporalAccessor" + ) + ); } public void testGUID() { diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/parser/ExpressionTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/parser/ExpressionTests.java index f166106093564..ba1107b254e30 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/parser/ExpressionTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/parser/ExpressionTests.java @@ -163,10 +163,19 @@ public void testStringInterval() { int randomSecond = randomInt(59); int randomMilli = randomInt(999); - String value = format(Locale.ROOT, "INTERVAL '%d %d:%d:%d.%03d' DAY TO SECOND", randomDay, randomHour, randomMinute, randomSecond, - randomMilli); - assertEquals(Duration.ofDays(randomDay).plusHours(randomHour).plusMinutes(randomMinute).plusSeconds(randomSecond) - .plusMillis(randomMilli), intervalOf(value)); + String value = format( + Locale.ROOT, + "INTERVAL '%d %d:%d:%d.%03d' DAY TO SECOND", + randomDay, + randomHour, + randomMinute, + randomSecond, + randomMilli + ); + assertEquals( + Duration.ofDays(randomDay).plusHours(randomHour).plusMinutes(randomMinute).plusSeconds(randomSecond).plusMillis(randomMilli), + intervalOf(value) + ); } public void testNegativeStringInterval() { @@ -176,10 +185,24 @@ public void testNegativeStringInterval() { int randomSecond = randomInt(59); int randomMilli = randomInt(999); - String value = format(Locale.ROOT, "INTERVAL -'%d %d:%d:%d.%03d' DAY TO SECOND", randomDay, randomHour, randomMinute, randomSecond, - randomMilli); - assertEquals(Duration.ofDays(randomDay).plusHours(randomHour).plusMinutes(randomMinute).plusSeconds(randomSecond) - .plusMillis(randomMilli).negated(), intervalOf(value)); + String value = format( + Locale.ROOT, + "INTERVAL -'%d %d:%d:%d.%03d' DAY TO SECOND", + randomDay, + randomHour, + randomMinute, + randomSecond, + randomMilli + ); + assertEquals( + Duration.ofDays(randomDay) + .plusHours(randomHour) + .plusMinutes(randomMinute) + .plusSeconds(randomSecond) + .plusMillis(randomMilli) + .negated(), + intervalOf(value) + ); } private TemporalAmount intervalOf(String query) { @@ -253,7 +276,7 @@ public void testNegativeLiteral() { expr = parser.createExpression(sb.toString()); assertEquals(Literal.class, expr.getClass()); assertEquals(sb.toString(), expr.sourceText()); - assertEquals(numberOfMinuses % 2 == 0 ? value : - value, expr.fold()); + assertEquals(numberOfMinuses % 2 == 0 ? value : -value, expr.fold()); } public void testComplexArithmetic() { @@ -505,10 +528,8 @@ public void testSourceFunction() { public void testCaseWithoutOperand() { Expression expr = parser.createExpression( - "CASE WHEN a = 1 THEN 'one'" + - " WHEN a > 2 THEN 'a few'" + - " WHEN a > 10 THEN 'many' " + - "END"); + "CASE WHEN a = 1 THEN 'one'" + " WHEN a > 2 THEN 'a few'" + " WHEN a > 10 THEN 'many' " + "END" + ); assertEquals(Case.class, expr.getClass()); Case c = (Case) expr; @@ -519,11 +540,7 @@ public void testCaseWithoutOperand() { assertEquals("one", ifc.result().toString()); assertEquals(Literal.NULL, c.elseResult()); - expr = parser.createExpression( - "CASE WHEN a = 1 THEN 'one'" + - " WHEN a <= 2 THEN 'a few'" + - "ELSE 'many' " + - "END"); + expr = parser.createExpression("CASE WHEN a = 1 THEN 'one'" + " WHEN a <= 2 THEN 'a few'" + "ELSE 'many' " + "END"); assertEquals(Case.class, expr.getClass()); c = (Case) expr; @@ -535,10 +552,8 @@ public void testCaseWithoutOperand() { public void testCaseWithOperand() { Expression expr = parser.createExpression( - "CASE a WHEN 1 THEN 'one'" + - " WHEN 2 THEN 'two'" + - " WHEN 3 THEN 'three' " + - "END"); + "CASE a WHEN 1 THEN 'one'" + " WHEN 2 THEN 'two'" + " WHEN 3 THEN 'three' " + "END" + ); assertEquals(Case.class, expr.getClass()); Case c = (Case) expr; @@ -549,11 +564,7 @@ public void testCaseWithOperand() { assertEquals("one", ifc.result().toString()); assertEquals(Literal.NULL, c.elseResult()); - expr = parser.createExpression( - "CASE a WHEN 1 THEN 'one'" + - " WHEN 2 THEN 'two'" + - "ELSE 'many' " + - "END"); + expr = parser.createExpression("CASE a WHEN 1 THEN 'one'" + " WHEN 2 THEN 'two'" + "ELSE 'many' " + "END"); assertEquals(Case.class, expr.getClass()); c = (Case) expr; assertEquals(2, c.conditions().size()); @@ -563,9 +574,10 @@ public void testCaseWithOperand() { } public void testLikePatternWithNullParameterNotAllowed() { - ParsingException e = expectThrows(ParsingException.class, - () -> parser.createExpression("a LIKE ?", - Collections.singletonList(new SqlTypedParamValue(KEYWORD.typeName(), null)))); + ParsingException e = expectThrows( + ParsingException.class, + () -> parser.createExpression("a LIKE ?", Collections.singletonList(new SqlTypedParamValue(KEYWORD.typeName(), null))) + ); assertEquals("line 1:9: Pattern must not be [null]", e.getMessage()); } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/parser/LikeEscapingParsingTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/parser/LikeEscapingParsingTests.java index 8e42662c525be..259abd65b3d26 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/parser/LikeEscapingParsingTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/parser/LikeEscapingParsingTests.java @@ -29,8 +29,7 @@ private static LikePattern patternOfLike(Expression exp) { } private String error(String pattern) { - ParsingException ex = expectThrows(ParsingException.class, - () -> parser.createExpression(format(null, "exp LIKE {}", pattern))); + ParsingException ex = expectThrows(ParsingException.class, () -> parser.createExpression(format(null, "exp LIKE {}", pattern))); return ex.getMessage(); } @@ -58,26 +57,38 @@ public void testNoEscaping() { } public void testEscapingLastChar() { - assertThat(error("'string|' ESCAPE '|'"), - is("line 1:11: Pattern [string|] is invalid as escape char [|] at position 6 does not escape anything")); + assertThat( + error("'string|' ESCAPE '|'"), + is("line 1:11: Pattern [string|] is invalid as escape char [|] at position 6 does not escape anything") + ); } public void testEscapingWrongChar() { - assertThat(error("'|string' ESCAPE '|'"), - is("line 1:11: Pattern [|string] is invalid as escape char [|] at position 0 can only escape " - + "wildcard chars [%_]; found [s]")); + assertThat( + error("'|string' ESCAPE '|'"), + is( + "line 1:11: Pattern [|string] is invalid as escape char [|] at position 0 can only escape " + + "wildcard chars [%_]; found [s]" + ) + ); } public void testEscapingTheEscapeCharacter() { - assertThat(error("'||string' ESCAPE '|'"), - is("line 1:11: Pattern [||string] is invalid as escape char [|] at position 0 can only escape wildcard chars [%_]; found [|]")); + assertThat( + error("'||string' ESCAPE '|'"), + is("line 1:11: Pattern [||string] is invalid as escape char [|] at position 0 can only escape wildcard chars [%_]; found [|]") + ); } public void testEscapingWildcards() { - assertThat(error("'string' ESCAPE '%'"), - is("line 1:27: Char [%] cannot be used for escaping as it's one of the wildcard chars [%_]")); - assertThat(error("'string' ESCAPE '_'"), - is("line 1:27: Char [_] cannot be used for escaping as it's one of the wildcard chars [%_]")); + assertThat( + error("'string' ESCAPE '%'"), + is("line 1:27: Char [%] cannot be used for escaping as it's one of the wildcard chars [%_]") + ); + assertThat( + error("'string' ESCAPE '_'"), + is("line 1:27: Char [_] cannot be used for escaping as it's one of the wildcard chars [%_]") + ); } public void testCanUseStarWithoutEscaping() { diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/parser/ParamLiteralTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/parser/ParamLiteralTests.java index 1ca1420ce2c3c..937656ad822d2 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/parser/ParamLiteralTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/parser/ParamLiteralTests.java @@ -34,7 +34,8 @@ private LogicalPlan parse(String sql, SqlTypedParamValue... parameters) { } public void testMultipleParamLiteralsWithUnresolvedAliases() { - LogicalPlan logicalPlan = parse("SELECT ?, ? FROM test", + LogicalPlan logicalPlan = parse( + "SELECT ?, ? FROM test", new SqlTypedParamValue("integer", 100), new SqlTypedParamValue("integer", 200) ); @@ -45,7 +46,8 @@ public void testMultipleParamLiteralsWithUnresolvedAliases() { } public void testMultipleParamLiteralsWithUnresolvedAliasesAndWhereClause() { - LogicalPlan logicalPlan = parse("SELECT ?, ?, (?) FROM test WHERE 1 < ?", + LogicalPlan logicalPlan = parse( + "SELECT ?, ?, (?) FROM test WHERE 1 < ?", new SqlTypedParamValue("integer", 100), new SqlTypedParamValue("integer", 100), new SqlTypedParamValue("integer", 200), @@ -63,11 +65,12 @@ public void testMultipleParamLiteralsWithUnresolvedAliasesAndWhereClause() { LessThan condition = (LessThan) filter.condition(); assertThat(condition.left(), instanceOf(Literal.class)); assertThat(condition.right(), instanceOf(Literal.class)); - assertThat(((Literal)condition.right()).value(), equalTo(300)); + assertThat(((Literal) condition.right()).value(), equalTo(300)); } public void testParamLiteralsWithUnresolvedAliasesAndMixedTypes() { - LogicalPlan logicalPlan = parse("SELECT ?, ? FROM test", + LogicalPlan logicalPlan = parse( + "SELECT ?, ? FROM test", new SqlTypedParamValue("integer", 100), new SqlTypedParamValue("text", "100") ); @@ -78,15 +81,18 @@ public void testParamLiteralsWithUnresolvedAliasesAndMixedTypes() { } public void testParamLiteralsWithResolvedAndUnresolvedAliases() { - LogicalPlan logicalPlan = parse("SELECT ?, ? as x, ? FROM test", + LogicalPlan logicalPlan = parse( + "SELECT ?, ? as x, ? FROM test", new SqlTypedParamValue("integer", 100), new SqlTypedParamValue("integer", 200), new SqlTypedParamValue("integer", 300) ); List projections = ((Project) logicalPlan.children().get(0)).projections(); assertThat(projections.get(0).toString(), startsWith("100 AS ?")); - assertThat(projections.get(1).toString(), startsWith("200 AS x#"));; - assertThat(projections.get(2).toString(), startsWith("300 AS ?"));; + assertThat(projections.get(1).toString(), startsWith("200 AS x#")); + ; + assertThat(projections.get(2).toString(), startsWith("300 AS ?")); + ; } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/parser/SqlParserTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/parser/SqlParserTests.java index 9719851fd1ec6..245fca9f612ce 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/parser/SqlParserTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/parser/SqlParserTests.java @@ -79,8 +79,10 @@ public void testSelectScore() { } public void testSelectCast() { - UnresolvedAlias f = singleProjection(project(parseStatement("SELECT CAST(POWER(languages, 2) AS DOUBLE) FROM foo")), - UnresolvedAlias.class); + UnresolvedAlias f = singleProjection( + project(parseStatement("SELECT CAST(POWER(languages, 2) AS DOUBLE) FROM foo")), + UnresolvedAlias.class + ); assertEquals("CAST(POWER(languages, 2) AS DOUBLE)", f.sourceText()); } @@ -90,8 +92,10 @@ public void testSelectCastOperator() { } public void testSelectCastWithSQLOperator() { - UnresolvedAlias f = singleProjection(project(parseStatement("SELECT CONVERT(POWER(languages, 2), SQL_DOUBLE) FROM foo")), - UnresolvedAlias.class); + UnresolvedAlias f = singleProjection( + project(parseStatement("SELECT CONVERT(POWER(languages, 2), SQL_DOUBLE) FROM foo")), + UnresolvedAlias.class + ); assertEquals("CONVERT(POWER(languages, 2), SQL_DOUBLE)", f.sourceText()); } @@ -143,21 +147,56 @@ public void testUseBothTopAndLimitInvalid() { ParsingException e = expectThrows(ParsingException.class, () -> parseStatement("SELECT TOP 10 * FROM test LIMIT 20")); assertEquals("line 1:28: TOP and LIMIT are not allowed in the same query - use one or the other", e.getMessage()); - e = expectThrows(ParsingException.class, - () -> parseStatement("SELECT TOP 30 a, count(*) cnt FROM test WHERE b = 20 GROUP BY a HAVING cnt > 10 LIMIT 40")); + e = expectThrows( + ParsingException.class, + () -> parseStatement("SELECT TOP 30 a, count(*) cnt FROM test WHERE b = 20 GROUP BY a HAVING cnt > 10 LIMIT 40") + ); assertEquals("line 1:82: TOP and LIMIT are not allowed in the same query - use one or the other", e.getMessage()); - e = expectThrows(ParsingException.class, - () -> parseStatement("SELECT TOP 30 * FROM test ORDER BY a LIMIT 40")); + e = expectThrows(ParsingException.class, () -> parseStatement("SELECT TOP 30 * FROM test ORDER BY a LIMIT 40")); assertEquals("line 1:39: TOP and LIMIT are not allowed in the same query - use one or the other", e.getMessage()); } public void testsSelectNonReservedKeywords() { String[] reserved = new String[] { - "ANALYZE", "ANALYZED", "CATALOGS", "COLUMNS", "CURRENT", "DAY", "DEBUG", "EXECUTABLE", "EXPLAIN", - "FIRST", "FORMAT", "FULL", "FUNCTIONS", "GRAPHVIZ", "HOUR", "INTERVAL", "LAST", "LIMIT", - "MAPPED", "MINUTE", "MONTH", "OPTIMIZED", "PARSED", "PHYSICAL", "PLAN", "QUERY", "RLIKE", - "SCHEMAS", "SECOND", "SHOW", "SYS", "TABLES", "TEXT", "TOP", "TYPE", "TYPES", "VERIFY", "YEAR"}; + "ANALYZE", + "ANALYZED", + "CATALOGS", + "COLUMNS", + "CURRENT", + "DAY", + "DEBUG", + "EXECUTABLE", + "EXPLAIN", + "FIRST", + "FORMAT", + "FULL", + "FUNCTIONS", + "GRAPHVIZ", + "HOUR", + "INTERVAL", + "LAST", + "LIMIT", + "MAPPED", + "MINUTE", + "MONTH", + "OPTIMIZED", + "PARSED", + "PHYSICAL", + "PLAN", + "QUERY", + "RLIKE", + "SCHEMAS", + "SECOND", + "SHOW", + "SYS", + "TABLES", + "TEXT", + "TOP", + "TYPE", + "TYPES", + "VERIFY", + "YEAR" }; StringJoiner sj = new StringJoiner(","); for (String s : reserved) { sj.add(s); @@ -198,10 +237,11 @@ public void testOrderByScore() { public void testOrderByTwo() { Order.OrderDirection dir0 = randomFrom(Order.OrderDirection.values()); Order.OrderDirection dir1 = randomFrom(Order.OrderDirection.values()); - OrderBy ob = orderBy(parseStatement( - " SELECT *" - + " FROM foo" - + " ORDER BY bar" + stringForDirection(dir0) + ", baz" + stringForDirection(dir1))); + OrderBy ob = orderBy( + parseStatement( + " SELECT *" + " FROM foo" + " ORDER BY bar" + stringForDirection(dir0) + ", baz" + stringForDirection(dir1) + ) + ); assertThat(ob.order(), hasSize(2)); Order o = ob.order().get(0); assertEquals(dir0, o.direction()); @@ -216,9 +256,9 @@ public void testOrderByTwo() { } public void testStringQuery() { - LogicalPlan plan = - parseStatement("SELECT * FROM FOO WHERE " + - "QUERY('foo', 'default_field=last_name;lenient=true', 'fuzzy_rewrite=scoring_boolean')"); + LogicalPlan plan = parseStatement( + "SELECT * FROM FOO WHERE " + "QUERY('foo', 'default_field=last_name;lenient=true', 'fuzzy_rewrite=scoring_boolean')" + ); StringQueryPredicate sqp = (StringQueryPredicate) ((Filter) plan.children().get(0).children().get(0)).condition(); assertEquals("foo", sqp.query()); @@ -229,8 +269,9 @@ public void testStringQuery() { } public void testMatchQuery() { - LogicalPlan plan = parseStatement("SELECT * FROM FOO WHERE " + - "MATCH(first_name, 'foo', 'operator=AND;lenient=true', 'fuzzy_rewrite=scoring_boolean')"); + LogicalPlan plan = parseStatement( + "SELECT * FROM FOO WHERE " + "MATCH(first_name, 'foo', 'operator=AND;lenient=true', 'fuzzy_rewrite=scoring_boolean')" + ); MatchQueryPredicate mqp = (MatchQueryPredicate) ((Filter) plan.children().get(0).children().get(0)).condition(); assertEquals("foo", mqp.query()); @@ -242,8 +283,10 @@ public void testMatchQuery() { } public void testMultiMatchQuery() { - LogicalPlan plan = parseStatement("SELECT * FROM FOO WHERE " + - "MATCH('first_name,last_name', 'foo', 'operator=AND;type=best_fields', 'fuzzy_rewrite=scoring_boolean')"); + LogicalPlan plan = parseStatement( + "SELECT * FROM FOO WHERE " + + "MATCH('first_name,last_name', 'foo', 'operator=AND;type=best_fields', 'fuzzy_rewrite=scoring_boolean')" + ); MultiMatchQueryPredicate mmqp = (MultiMatchQueryPredicate) ((Filter) plan.children().get(0).children().get(0)).condition(); assertEquals("foo", mmqp.query()); @@ -261,24 +304,31 @@ public void testLimitToPreventStackOverflowFromLargeBinaryBooleanExpression() { new SqlParser().createExpression(join(" OR ", nCopies(1000, "a = b"))); // 10000 elements cause stack overflow - ParsingException e = expectThrows(ParsingException.class, () -> - new SqlParser().createExpression(join(" OR ", nCopies(10000, "a = b")))); - assertThat(e.getMessage(), - startsWith("line -1:0: SQL statement is too large, causing stack overflow when generating the parsing tree: [")); + ParsingException e = expectThrows( + ParsingException.class, + () -> new SqlParser().createExpression(join(" OR ", nCopies(10000, "a = b"))) + ); + assertThat( + e.getMessage(), + startsWith("line -1:0: SQL statement is too large, causing stack overflow when generating the parsing tree: [") + ); } public void testLimitToPreventStackOverflowFromLargeUnaryArithmeticExpression() { // Create expression in the form of abs(abs(abs ... (i) ...) // 200 elements is ok - new SqlParser().createExpression( - join("", nCopies(200, "abs(")).concat("i").concat(join("", nCopies(200, ")")))); + new SqlParser().createExpression(join("", nCopies(200, "abs(")).concat("i").concat(join("", nCopies(200, ")")))); // 5000 elements cause stack overflow - ParsingException e = expectThrows(ParsingException.class, () -> new SqlParser().createExpression( - join("", nCopies(5000, "abs(")).concat("i").concat(join("", nCopies(5000, ")"))))); - assertThat(e.getMessage(), - startsWith("line -1:0: SQL statement is too large, causing stack overflow when generating the parsing tree: [")); + ParsingException e = expectThrows( + ParsingException.class, + () -> new SqlParser().createExpression(join("", nCopies(5000, "abs(")).concat("i").concat(join("", nCopies(5000, ")")))) + ); + assertThat( + e.getMessage(), + startsWith("line -1:0: SQL statement is too large, causing stack overflow when generating the parsing tree: [") + ); } public void testLimitToPreventStackOverflowFromLargeBinaryArithmeticExpression() { @@ -288,28 +338,30 @@ public void testLimitToPreventStackOverflowFromLargeBinaryArithmeticExpression() new SqlParser().createExpression(join(" + ", nCopies(1000, "a"))); // 10000 elements cause stack overflow - ParsingException e = expectThrows(ParsingException.class, () -> - new SqlParser().createExpression(join(" + ", nCopies(10000, "a")))); - assertThat(e.getMessage(), - startsWith("line -1:0: SQL statement is too large, causing stack overflow when generating the parsing tree: [")); + ParsingException e = expectThrows(ParsingException.class, () -> new SqlParser().createExpression(join(" + ", nCopies(10000, "a")))); + assertThat( + e.getMessage(), + startsWith("line -1:0: SQL statement is too large, causing stack overflow when generating the parsing tree: [") + ); } public void testLimitToPreventStackOverflowFromLargeSubselectTree() { // Test with queries in the form of `SELECT * FROM (SELECT * FROM (... t) ...) // 200 elements is ok - new SqlParser().createStatement( - join(" (", nCopies(200, "SELECT * FROM")) - .concat("t") - .concat(join("", nCopies(199, ")")))); + new SqlParser().createStatement(join(" (", nCopies(200, "SELECT * FROM")).concat("t").concat(join("", nCopies(199, ")")))); // 1000 elements cause stack overflow - ParsingException e = expectThrows(ParsingException.class, () -> new SqlParser().createStatement( - join(" (", nCopies(1000, "SELECT * FROM")) - .concat("t") - .concat(join("", nCopies(999, ")"))))); - assertThat(e.getMessage(), - startsWith("line -1:0: SQL statement is too large, causing stack overflow when generating the parsing tree: [")); + ParsingException e = expectThrows( + ParsingException.class, + () -> new SqlParser().createStatement( + join(" (", nCopies(1000, "SELECT * FROM")).concat("t").concat(join("", nCopies(999, ")"))) + ) + ); + assertThat( + e.getMessage(), + startsWith("line -1:0: SQL statement is too large, causing stack overflow when generating the parsing tree: [") + ); } private LogicalPlan parseStatement(String sql) { @@ -337,9 +389,7 @@ private void projectRecur(LogicalPlan plan, List sync) { * Find the one and only {@code ORDER BY} in a plan. */ private OrderBy orderBy(LogicalPlan plan) { - List l = plan.children().stream() - .filter(c -> c instanceof OrderBy) - .collect(toList()); + List l = plan.children().stream().filter(c -> c instanceof OrderBy).collect(toList()); assertThat("expected only one ORDER BY", l, hasSize(1)); return (OrderBy) l.get(0); } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plan/logical/UnresolvedRelationTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plan/logical/UnresolvedRelationTests.java index e3c4663ad6155..d43b0fad69ea1 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plan/logical/UnresolvedRelationTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plan/logical/UnresolvedRelationTests.java @@ -26,28 +26,37 @@ public void testEqualsAndHashCode() { String unresolvedMessage = randomAlphaOfLength(5); UnresolvedRelation relation = new UnresolvedRelation(source, table, alias, randomBoolean(), unresolvedMessage); List> mutators = new ArrayList<>(); - mutators.add(r -> new UnresolvedRelation( - r.source(), - new TableIdentifier(r.source(), r.table().cluster(), r.table().index() + "m"), - r.alias(), - r.frozen(), - r.unresolvedMessage())); - mutators.add(r -> new UnresolvedRelation( - r.source(), - r.table(), - randomValueOtherThanMany( - a -> Objects.equals(a, r.alias()), - () -> randomBoolean() ? null : randomAlphaOfLength(5)), - r.frozen(), - r.unresolvedMessage())); - mutators.add(r -> new UnresolvedRelation( - r.source(), - r.table(), - r.alias(), - r.frozen(), - randomValueOtherThan(r.unresolvedMessage(), () -> randomAlphaOfLength(5)))); - checkEqualsAndHashCode(relation, + mutators.add( + r -> new UnresolvedRelation( + r.source(), + new TableIdentifier(r.source(), r.table().cluster(), r.table().index() + "m"), + r.alias(), + r.frozen(), + r.unresolvedMessage() + ) + ); + mutators.add( + r -> new UnresolvedRelation( + r.source(), + r.table(), + randomValueOtherThanMany(a -> Objects.equals(a, r.alias()), () -> randomBoolean() ? null : randomAlphaOfLength(5)), + r.frozen(), + r.unresolvedMessage() + ) + ); + mutators.add( + r -> new UnresolvedRelation( + r.source(), + r.table(), + r.alias(), + r.frozen(), + randomValueOtherThan(r.unresolvedMessage(), () -> randomAlphaOfLength(5)) + ) + ); + checkEqualsAndHashCode( + relation, r -> new UnresolvedRelation(r.source(), r.table(), r.alias(), r.frozen(), r.unresolvedMessage()), - r -> randomFrom(mutators).apply(r)); + r -> randomFrom(mutators).apply(r) + ); } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plan/logical/command/sys/SysColumnsTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plan/logical/command/sys/SysColumnsTests.java index 3377e13a5dd71..205245224fc02 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plan/logical/command/sys/SysColumnsTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plan/logical/command/sys/SysColumnsTests.java @@ -6,15 +6,6 @@ */ package org.elasticsearch.xpack.sql.plan.logical.command.sys; -import java.sql.Types; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.Set; -import java.util.function.Consumer; - import org.elasticsearch.Version; import org.elasticsearch.action.ActionListener; import org.elasticsearch.core.Tuple; @@ -45,6 +36,7 @@ import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Set; import java.util.function.Consumer; import static java.util.Collections.emptyList; @@ -190,7 +182,7 @@ public void testSysColumnsNoArg() { } public void testSysColumnsWithCatalogWildcard() { - executeCommand("SYS COLUMNS CATALOG '" + CLUSTER_NAME + "' TABLE LIKE 'test' LIKE '%'", emptyList(), r -> { + executeCommand("SYS COLUMNS CATALOG '" + CLUSTER_NAME + "' TABLE LIKE 'test' LIKE '%'", emptyList(), r -> { assertEquals(FIELD_COUNT1, r.size()); assertEquals(CLUSTER_NAME, r.column(0)); assertEquals("test", r.column(2)); @@ -239,12 +231,27 @@ public void testSysColumnsPaginationInOdbcMode() { } private int executeCommandInOdbcModeAndCountRows(String sql) { - final SqlConfiguration config = new SqlConfiguration(DateUtils.UTC, null, randomIntBetween(1, 15), Protocol.REQUEST_TIMEOUT, - Protocol.PAGE_TIMEOUT, null, null, Mode.ODBC, null, SqlVersion.fromId(Version.CURRENT.id), null, null, - false, false, null, null); + final SqlConfiguration config = new SqlConfiguration( + DateUtils.UTC, + null, + randomIntBetween(1, 15), + Protocol.REQUEST_TIMEOUT, + Protocol.PAGE_TIMEOUT, + null, + null, + Mode.ODBC, + null, + SqlVersion.fromId(Version.CURRENT.id), + null, + null, + false, + false, + null, + null + ); Tuple tuple = sql(sql, emptyList(), config, MAPPING1); - int[] rowCount = {0}; + int[] rowCount = { 0 }; tuple.v1().execute(tuple.v2(), new ActionListener<>() { @Override public void onResponse(Cursor.Page page) { @@ -263,24 +270,52 @@ public void onFailure(Exception e) { return rowCount[0]; } - private void executeCommand(String sql, List params, Mode mode, Consumer consumer, - Map mapping) { - final SqlConfiguration config = new SqlConfiguration(DateUtils.UTC, null, Protocol.FETCH_SIZE, Protocol.REQUEST_TIMEOUT, - Protocol.PAGE_TIMEOUT, null, null, mode, null, SqlVersion.fromId(Version.CURRENT.id), null, null, - false, false, null, null); + private void executeCommand( + String sql, + List params, + Mode mode, + Consumer consumer, + Map mapping + ) { + final SqlConfiguration config = new SqlConfiguration( + DateUtils.UTC, + null, + Protocol.FETCH_SIZE, + Protocol.REQUEST_TIMEOUT, + Protocol.PAGE_TIMEOUT, + null, + null, + mode, + null, + SqlVersion.fromId(Version.CURRENT.id), + null, + null, + false, + false, + null, + null + ); Tuple tuple = sql(sql, params, config, mapping); tuple.v1().execute(tuple.v2(), wrap(p -> consumer.accept((SchemaRowSet) p.rowSet()), ex -> fail(ex.getMessage()))); } - private void executeCommand(String sql, List params, - Consumer consumer, Map mapping) { + private void executeCommand( + String sql, + List params, + Consumer consumer, + Map mapping + ) { executeCommand(sql, params, Mode.PLAIN, consumer, mapping); } @SuppressWarnings({ "unchecked" }) - private Tuple sql(String sql, List params, SqlConfiguration config, - Map mapping) { + private Tuple sql( + String sql, + List params, + SqlConfiguration config, + Map mapping + ) { EsIndex test = new EsIndex("test", mapping); Analyzer analyzer = new Analyzer(config, new FunctionRegistry(), IndexResolution.valid(test), new Verifier(new Metrics())); Command cmd = (Command) analyzer.analyze(parser.createStatement(sql, params, UTC), true); @@ -306,7 +341,7 @@ private static void checkOdbcShortTypes(SchemaRowSet r) { // https://github.com/elastic/elasticsearch/issues/35376 // cols that need to be of short type: DATA_TYPE, DECIMAL_DIGITS, NUM_PREC_RADIX, NULLABLE, SQL_DATA_TYPE, SQL_DATETIME_SUB List cols = Arrays.asList(4, 8, 9, 10, 13, 14); - for (Integer i: cols) { + for (Integer i : cols) { assertEquals("short", r.schema().get(i).type().name().toLowerCase(Locale.ROOT)); } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plan/logical/command/sys/SysTablesTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plan/logical/command/sys/SysTablesTests.java index 891e33829be6f..9fa52599ccf15 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plan/logical/command/sys/SysTablesTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plan/logical/command/sys/SysTablesTests.java @@ -59,9 +59,24 @@ public class SysTablesTests extends ESTestCase { private final IndexInfo alias = new IndexInfo(CLUSTER_NAME, "alias", IndexType.ALIAS); private final IndexInfo frozen = new IndexInfo(CLUSTER_NAME, "frozen", IndexType.FROZEN_INDEX); - private final SqlConfiguration FROZEN_CFG = new SqlConfiguration(DateUtils.UTC, null, Protocol.FETCH_SIZE, Protocol.REQUEST_TIMEOUT, - Protocol.PAGE_TIMEOUT, null, null, Mode.PLAIN, null, null, null, null, - false, true, null, null); + private final SqlConfiguration FROZEN_CFG = new SqlConfiguration( + DateUtils.UTC, + null, + Protocol.FETCH_SIZE, + Protocol.REQUEST_TIMEOUT, + Protocol.PAGE_TIMEOUT, + null, + null, + Mode.PLAIN, + null, + null, + null, + null, + false, + true, + null, + null + ); // // catalog enumeration @@ -100,7 +115,6 @@ public void testSysTablesCatalogNoTypes() throws Exception { }, index); } - // // table types enumeration // @@ -138,9 +152,7 @@ public void testSysTablesTypesEnumeration() throws Exception { // when a type is specified, apply filtering public void testSysTablesTypesEnumerationAllCatalogsAndSpecifiedView() throws Exception { - executeCommand("SYS TABLES CATALOG LIKE '%' LIKE '' TYPE 'VIEW'", r -> { - assertEquals(0, r.size()); - }, new IndexInfo[0]); + executeCommand("SYS TABLES CATALOG LIKE '%' LIKE '' TYPE 'VIEW'", r -> { assertEquals(0, r.size()); }, new IndexInfo[0]); } public void testSysTablesDifferentCatalog() throws Exception { @@ -328,20 +340,21 @@ public void testSysTablesWithCatalogOnlyAliases() throws Exception { } public void testSysTablesWithInvalidType() throws Exception { - executeCommand("SYS TABLES LIKE 'test' TYPE 'QUE HORA ES'", r -> { - assertEquals(0, r.size()); - }, new IndexInfo[0]); + executeCommand("SYS TABLES LIKE 'test' TYPE 'QUE HORA ES'", r -> { assertEquals(0, r.size()); }, new IndexInfo[0]); } - private SqlTypedParamValue param(Object value) { return new SqlTypedParamValue(DataTypes.fromJava(value).typeName(), value); } private Tuple sql(String sql, List params, SqlConfiguration cfg) { EsIndex test = new EsIndex("test", mapping); - Analyzer analyzer = new Analyzer(SqlTestUtils.TEST_CFG, new FunctionRegistry(), IndexResolution.valid(test), - new Verifier(new Metrics())); + Analyzer analyzer = new Analyzer( + SqlTestUtils.TEST_CFG, + new FunctionRegistry(), + IndexResolution.valid(test), + new Verifier(new Metrics()) + ); Command cmd = (Command) analyzer.analyze(parser.createStatement(sql, params, cfg.zoneId()), true); IndexResolver resolver = mock(IndexResolver.class); @@ -360,13 +373,18 @@ private void executeCommand(String sql, Consumer consumer, SqlConf } private void executeCommand(String sql, List params, Consumer consumer, IndexInfo... infos) - throws Exception { + throws Exception { executeCommand(sql, params, consumer, SqlTestUtils.TEST_CFG, infos); } @SuppressWarnings({ "unchecked", "rawtypes" }) - private void executeCommand(String sql, List params, Consumer consumer, SqlConfiguration cfg, - IndexInfo... infos) throws Exception { + private void executeCommand( + String sql, + List params, + Consumer consumer, + SqlConfiguration cfg, + IndexInfo... infos + ) throws Exception { Tuple tuple = sql(sql, params, cfg); IndexResolver resolver = tuple.v2().indexResolver(); diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plan/logical/command/sys/SysTypesTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plan/logical/command/sys/SysTypesTests.java index a814ad44ee0a1..48122baf707d9 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plan/logical/command/sys/SysTypesTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plan/logical/command/sys/SysTypesTests.java @@ -39,9 +39,24 @@ public class SysTypesTests extends ESTestCase { private final SqlParser parser = new SqlParser(); private Tuple sql(String sql, Mode mode, SqlVersion version) { - SqlConfiguration configuration = new SqlConfiguration(DateUtils.UTC, null, Protocol.FETCH_SIZE, - Protocol.REQUEST_TIMEOUT, Protocol.PAGE_TIMEOUT, null, null, mode, null, version, null, - null, false, false, null, null); + SqlConfiguration configuration = new SqlConfiguration( + DateUtils.UTC, + null, + Protocol.FETCH_SIZE, + Protocol.REQUEST_TIMEOUT, + Protocol.PAGE_TIMEOUT, + null, + null, + mode, + null, + version, + null, + null, + false, + false, + null, + null + ); EsIndex test = new EsIndex("test", SqlTypesTests.loadMapping("mapping-multi-field-with-nested.json", true)); Analyzer analyzer = new Analyzer(configuration, new FunctionRegistry(), IndexResolution.valid(test), null); Command cmd = (Command) analyzer.analyze(parser.createStatement(sql), false); @@ -50,6 +65,7 @@ private Tuple sql(String sql, Mode mode, SqlVersion version SqlSession session = new SqlSession(configuration, null, null, resolver, null, null, null, null, null); return new Tuple<>(cmd, session); } + private Tuple sql(String sql) { return sql(sql, randomFrom(Mode.values()), randomBoolean() ? null : SqlVersion.fromId(Version.CURRENT.id)); } @@ -57,12 +73,44 @@ private Tuple sql(String sql) { public void testSysTypes() { Tuple cmd = sql("SYS TYPES"); - List names = asList("BYTE", "LONG", "BINARY", "NULL", "INTEGER", "SHORT", "HALF_FLOAT", - "FLOAT", "DOUBLE", "SCALED_FLOAT", "IP", "KEYWORD", "TEXT", "BOOLEAN", "DATE", "TIME", "DATETIME", - "INTERVAL_YEAR", "INTERVAL_MONTH", "INTERVAL_DAY", "INTERVAL_HOUR", "INTERVAL_MINUTE", "INTERVAL_SECOND", - "INTERVAL_YEAR_TO_MONTH", "INTERVAL_DAY_TO_HOUR", "INTERVAL_DAY_TO_MINUTE", "INTERVAL_DAY_TO_SECOND", - "INTERVAL_HOUR_TO_MINUTE", "INTERVAL_HOUR_TO_SECOND", "INTERVAL_MINUTE_TO_SECOND", - "GEO_POINT", "GEO_SHAPE", "SHAPE", "UNSUPPORTED", "NESTED", "OBJECT"); + List names = asList( + "BYTE", + "LONG", + "BINARY", + "NULL", + "INTEGER", + "SHORT", + "HALF_FLOAT", + "FLOAT", + "DOUBLE", + "SCALED_FLOAT", + "IP", + "KEYWORD", + "TEXT", + "BOOLEAN", + "DATE", + "TIME", + "DATETIME", + "INTERVAL_YEAR", + "INTERVAL_MONTH", + "INTERVAL_DAY", + "INTERVAL_HOUR", + "INTERVAL_MINUTE", + "INTERVAL_SECOND", + "INTERVAL_YEAR_TO_MONTH", + "INTERVAL_DAY_TO_HOUR", + "INTERVAL_DAY_TO_MINUTE", + "INTERVAL_DAY_TO_SECOND", + "INTERVAL_HOUR_TO_MINUTE", + "INTERVAL_HOUR_TO_SECOND", + "INTERVAL_MINUTE_TO_SECOND", + "GEO_POINT", + "GEO_SHAPE", + "SHAPE", + "UNSUPPORTED", + "NESTED", + "OBJECT" + ); cmd.v1().execute(cmd.v2(), wrap(p -> { SchemaRowSet r = (SchemaRowSet) p.rowSet(); diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/planner/QueryFolderTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/planner/QueryFolderTests.java index 65bd468f633ae..21fedeac77c28 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/planner/QueryFolderTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/planner/QueryFolderTests.java @@ -290,8 +290,10 @@ public void testFoldingBooleanOrNull_WhereClause() { PhysicalPlan p = plan("SELECT keyword FROM test WHERE int > 10 OR null OR false"); assertEquals(EsQueryExec.class, p.getClass()); EsQueryExec ee = (EsQueryExec) p; - assertEquals("{\"range\":{\"int\":{\"from\":10,\"to\":null,\"include_lower\":false,\"include_upper\":false,\"boost\":1.0}}}", - ee.queryContainer().query().asBuilder().toString().replaceAll("\\s+", "")); + assertEquals( + "{\"range\":{\"int\":{\"from\":10,\"to\":null,\"include_lower\":false,\"include_upper\":false,\"boost\":1.0}}}", + ee.queryContainer().query().asBuilder().toString().replaceAll("\\s+", "") + ); assertEquals(1, ee.output().size()); assertThat(ee.output().get(0).toString(), startsWith("test.keyword{f}#")); } @@ -300,10 +302,18 @@ public void testFoldingBooleanOrNull_HavingClause() { PhysicalPlan p = plan("SELECT keyword, max(int) FROM test GROUP BY keyword HAVING max(int) > 10 OR null"); assertEquals(EsQueryExec.class, p.getClass()); EsQueryExec ee = (EsQueryExec) p; - assertTrue(ee.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", "").contains( - "\"script\":{\"source\":\"InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(" + - "InternalQlScriptUtils.nullSafeCastNumeric(params.a0,params.v0),params.v1))\"," + - "\"lang\":\"painless\",\"params\":{\"v0\":\"INTEGER\",\"v1\":10}},")); + assertTrue( + ee.queryContainer() + .aggs() + .asAggBuilder() + .toString() + .replaceAll("\\s+", "") + .contains( + "\"script\":{\"source\":\"InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(" + + "InternalQlScriptUtils.nullSafeCastNumeric(params.a0,params.v0),params.v1))\"," + + "\"lang\":\"painless\",\"params\":{\"v0\":\"INTEGER\",\"v1\":10}}," + ) + ); assertEquals(2, ee.output().size()); assertThat(ee.output().get(0).toString(), startsWith("test.keyword{f}#")); assertThat(ee.output().get(1).toString(), startsWith("max(int){r}")); @@ -373,11 +383,15 @@ public void testGroupKeyTypes_Boolean() { PhysicalPlan p = plan("SELECT count(*), int > 10 AS a FROM test GROUP BY a"); assertEquals(EsQueryExec.class, p.getClass()); EsQueryExec ee = (EsQueryExec) p; - assertThat(ee.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""), - endsWith("{\"script\":{" + - "\"source\":\"InternalQlScriptUtils.gt(InternalQlScriptUtils.docValue(doc,params.v0),params.v1)\"," + - "\"lang\":\"painless\",\"params\":{\"v0\":\"int\",\"v1\":10}},\"missing_bucket\":true," + - "\"value_type\":\"boolean\",\"order\":\"asc\"}}}]}}}")); + assertThat( + ee.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""), + endsWith( + "{\"script\":{" + + "\"source\":\"InternalQlScriptUtils.gt(InternalQlScriptUtils.docValue(doc,params.v0),params.v1)\"," + + "\"lang\":\"painless\",\"params\":{\"v0\":\"int\",\"v1\":10}},\"missing_bucket\":true," + + "\"value_type\":\"boolean\",\"order\":\"asc\"}}}]}}}" + ) + ); assertEquals(2, ee.output().size()); assertThat(ee.output().get(0).toString(), startsWith("count(*){r}")); assertThat(ee.output().get(1).toString(), startsWith("a{r}")); @@ -387,11 +401,15 @@ public void testGroupKeyTypes_Integer() { PhysicalPlan p = plan("SELECT count(*), int + 10 AS a FROM test GROUP BY a"); assertEquals(EsQueryExec.class, p.getClass()); EsQueryExec ee = (EsQueryExec) p; - assertThat(ee.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""), - endsWith("{\"script\":{" + - "\"source\":\"InternalSqlScriptUtils.add(InternalQlScriptUtils.docValue(doc,params.v0),params.v1)\"," + - "\"lang\":\"painless\",\"params\":{\"v0\":\"int\",\"v1\":10}},\"missing_bucket\":true," + - "\"value_type\":\"long\",\"order\":\"asc\"}}}]}}}")); + assertThat( + ee.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""), + endsWith( + "{\"script\":{" + + "\"source\":\"InternalSqlScriptUtils.add(InternalQlScriptUtils.docValue(doc,params.v0),params.v1)\"," + + "\"lang\":\"painless\",\"params\":{\"v0\":\"int\",\"v1\":10}},\"missing_bucket\":true," + + "\"value_type\":\"long\",\"order\":\"asc\"}}}]}}}" + ) + ); assertEquals(2, ee.output().size()); assertThat(ee.output().get(0).toString(), startsWith("count(*){r}")); assertThat(ee.output().get(1).toString(), startsWith("a{r}")); @@ -401,11 +419,15 @@ public void testGroupKeyTypes_Rational() { PhysicalPlan p = plan("SELECT count(*), sin(int) AS a FROM test GROUP BY a"); assertEquals(EsQueryExec.class, p.getClass()); EsQueryExec ee = (EsQueryExec) p; - assertThat(ee.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""), - endsWith("{\"script\":{" + - "\"source\":\"InternalSqlScriptUtils.sin(InternalQlScriptUtils.docValue(doc,params.v0))\"," + - "\"lang\":\"painless\",\"params\":{\"v0\":\"int\"}},\"missing_bucket\":true," + - "\"value_type\":\"double\",\"order\":\"asc\"}}}]}}}")); + assertThat( + ee.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""), + endsWith( + "{\"script\":{" + + "\"source\":\"InternalSqlScriptUtils.sin(InternalQlScriptUtils.docValue(doc,params.v0))\"," + + "\"lang\":\"painless\",\"params\":{\"v0\":\"int\"}},\"missing_bucket\":true," + + "\"value_type\":\"double\",\"order\":\"asc\"}}}]}}}" + ) + ); assertEquals(2, ee.output().size()); assertThat(ee.output().get(0).toString(), startsWith("count(*){r}")); assertThat(ee.output().get(1).toString(), startsWith("a{r}")); @@ -415,11 +437,15 @@ public void testGroupKeyTypes_String() { PhysicalPlan p = plan("SELECT count(*), LCASE(keyword) AS a FROM test GROUP BY a"); assertEquals(EsQueryExec.class, p.getClass()); EsQueryExec ee = (EsQueryExec) p; - assertThat(ee.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""), - endsWith("{\"script\":{" + - "\"source\":\"InternalSqlScriptUtils.lcase(InternalQlScriptUtils.docValue(doc,params.v0))\"," + - "\"lang\":\"painless\",\"params\":{\"v0\":\"keyword\"}},\"missing_bucket\":true," + - "\"value_type\":\"string\",\"order\":\"asc\"}}}]}}}")); + assertThat( + ee.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""), + endsWith( + "{\"script\":{" + + "\"source\":\"InternalSqlScriptUtils.lcase(InternalQlScriptUtils.docValue(doc,params.v0))\"," + + "\"lang\":\"painless\",\"params\":{\"v0\":\"keyword\"}},\"missing_bucket\":true," + + "\"value_type\":\"string\",\"order\":\"asc\"}}}]}}}" + ) + ); assertEquals(2, ee.output().size()); assertThat(ee.output().get(0).toString(), startsWith("count(*){r}#")); assertThat(ee.output().get(1).toString(), startsWith("a{r}")); @@ -429,11 +455,15 @@ public void testGroupKeyTypes_IP() { PhysicalPlan p = plan("SELECT count(*), CAST(keyword AS IP) AS a FROM test GROUP BY a"); assertEquals(EsQueryExec.class, p.getClass()); EsQueryExec ee = (EsQueryExec) p; - assertThat(ee.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""), - endsWith("{\"script\":{\"source\":\"InternalSqlScriptUtils.cast(" + - "InternalQlScriptUtils.docValue(doc,params.v0),params.v1)\"," + - "\"lang\":\"painless\",\"params\":{\"v0\":\"keyword\",\"v1\":\"IP\"}}," + - "\"missing_bucket\":true,\"value_type\":\"ip\",\"order\":\"asc\"}}}]}}}")); + assertThat( + ee.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""), + endsWith( + "{\"script\":{\"source\":\"InternalSqlScriptUtils.cast(" + + "InternalQlScriptUtils.docValue(doc,params.v0),params.v1)\"," + + "\"lang\":\"painless\",\"params\":{\"v0\":\"keyword\",\"v1\":\"IP\"}}," + + "\"missing_bucket\":true,\"value_type\":\"ip\",\"order\":\"asc\"}}}]}}}" + ) + ); assertEquals(2, ee.output().size()); assertThat(ee.output().get(0).toString(), startsWith("count(*){r}#")); assertThat(ee.output().get(1).toString(), startsWith("a{r}")); @@ -443,12 +473,16 @@ public void testGroupKeyTypes_DateTime() { PhysicalPlan p = plan("SELECT count(*), date + INTERVAL '1-2' YEAR TO MONTH AS a FROM test GROUP BY a"); assertEquals(EsQueryExec.class, p.getClass()); EsQueryExec ee = (EsQueryExec) p; - assertThat(ee.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""), - endsWith("{\"script\":{" + - "\"source\":\"InternalSqlScriptUtils.add(InternalQlScriptUtils.docValue(doc,params.v0)," + - "InternalSqlScriptUtils.intervalYearMonth(params.v1,params.v2))\",\"lang\":\"painless\",\"params\":{" + - "\"v0\":\"date\",\"v1\":\"P1Y2M\",\"v2\":\"INTERVAL_YEAR_TO_MONTH\"}},\"missing_bucket\":true," + - "\"value_type\":\"long\",\"order\":\"asc\"}}}]}}}")); + assertThat( + ee.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""), + endsWith( + "{\"script\":{" + + "\"source\":\"InternalSqlScriptUtils.add(InternalQlScriptUtils.docValue(doc,params.v0)," + + "InternalSqlScriptUtils.intervalYearMonth(params.v1,params.v2))\",\"lang\":\"painless\",\"params\":{" + + "\"v0\":\"date\",\"v1\":\"P1Y2M\",\"v2\":\"INTERVAL_YEAR_TO_MONTH\"}},\"missing_bucket\":true," + + "\"value_type\":\"long\",\"order\":\"asc\"}}}]}}}" + ) + ); assertEquals(2, ee.output().size()); assertThat(ee.output().get(0).toString(), startsWith("count(*){r}#")); assertThat(ee.output().get(1).toString(), startsWith("a{r}")); @@ -460,16 +494,20 @@ public void testSelectLiteralWithGroupBy() { EsQueryExec ee = (EsQueryExec) p; assertEquals(2, ee.output().size()); assertEquals(asList("1", "MAX(int)"), Expressions.names(ee.output())); - assertThat(ee.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""), - containsString("\"max\":{\"field\":\"int\"")); + assertThat( + ee.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""), + containsString("\"max\":{\"field\":\"int\"") + ); p = plan("SELECT 1, count(*) FROM test GROUP BY int"); assertEquals(EsQueryExec.class, p.getClass()); ee = (EsQueryExec) p; assertEquals(2, ee.output().size()); assertEquals(asList("1", "count(*)"), Expressions.names(ee.output())); - assertThat(ee.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""), - containsString("\"terms\":{\"field\":\"int\"")); + assertThat( + ee.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""), + containsString("\"terms\":{\"field\":\"int\"") + ); } public void testConcatIsNotFoldedForNull() { @@ -516,19 +554,29 @@ public void testFoldingOfPivot() { public void testPivotHasSameQueryAsGroupBy() { final Map aggFnsWithMultipleArguments = Map.of( - "PERCENTILE", "PERCENTILE(int, 0)", - "PERCENTILE_RANK", "PERCENTILE_RANK(int, 0)" + "PERCENTILE", + "PERCENTILE(int, 0)", + "PERCENTILE_RANK", + "PERCENTILE_RANK(int, 0)" ); List aggregations = new SqlFunctionRegistry().listFunctions() - .stream() - .filter(def -> AggregateFunction.class.isAssignableFrom(def.clazz())) - .map(def -> aggFnsWithMultipleArguments.getOrDefault(def.name(), def.name() + "(int)")) - .collect(toList()); + .stream() + .filter(def -> AggregateFunction.class.isAssignableFrom(def.clazz())) + .map(def -> aggFnsWithMultipleArguments.getOrDefault(def.name(), def.name() + "(int)")) + .collect(toList()); for (String aggregationStr : aggregations) { - PhysicalPlan pivotPlan = plan("SELECT * FROM (SELECT some.dotted.field, bool, keyword, int FROM test) " + - "PIVOT(" + aggregationStr + " FOR keyword IN ('A', 'B'))"); - PhysicalPlan groupByPlan = plan("SELECT some.dotted.field, bool, keyword, " + aggregationStr + " " + - "FROM test WHERE keyword IN ('A', 'B') GROUP BY some.dotted.field, bool, keyword"); + PhysicalPlan pivotPlan = plan( + "SELECT * FROM (SELECT some.dotted.field, bool, keyword, int FROM test) " + + "PIVOT(" + + aggregationStr + + " FOR keyword IN ('A', 'B'))" + ); + PhysicalPlan groupByPlan = plan( + "SELECT some.dotted.field, bool, keyword, " + + aggregationStr + + " " + + "FROM test WHERE keyword IN ('A', 'B') GROUP BY some.dotted.field, bool, keyword" + ); assertEquals(EsQueryExec.class, pivotPlan.getClass()); assertEquals(EsQueryExec.class, groupByPlan.getClass()); QueryContainer pivotQueryContainer = ((EsQueryExec) pivotPlan).queryContainer(); @@ -593,9 +641,7 @@ public void testFoldGroupByHistogramWithNullsOrdering() { Tuple.tuple("DESC NULLS FIRST", MissingOrder.FIRST), Tuple.tuple("DESC NULLS LAST", MissingOrder.LAST) )) { - PhysicalPlan p = plan( - "SELECT HISTOGRAM(int, 100) h FROM test GROUP BY h ORDER BY h " + orderDirectiveWithExpectedMissing.v1() - ); + PhysicalPlan p = plan("SELECT HISTOGRAM(int, 100) h FROM test GROUP BY h ORDER BY h " + orderDirectiveWithExpectedMissing.v1()); assertEquals(EsQueryExec.class, p.getClass()); EsQueryExec ee = (EsQueryExec) p; diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/planner/QueryTranslatorSpecTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/planner/QueryTranslatorSpecTests.java index c514ee61943f1..a528280414c09 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/planner/QueryTranslatorSpecTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/planner/QueryTranslatorSpecTests.java @@ -8,6 +8,7 @@ package org.elasticsearch.xpack.sql.planner; import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; + import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.ql.TestUtils; import org.elasticsearch.xpack.ql.index.EsIndex; @@ -61,7 +62,7 @@ PhysicalPlan optimizeAndPlan(String sql) { } PhysicalPlan optimizeAndPlan(LogicalPlan plan) { - return planner.plan(optimizer.optimize(plan),true); + return planner.plan(optimizer.optimize(plan), true); } } @@ -95,7 +96,7 @@ public static Iterable parameters() throws Exception { public void test() { assumeFalse("Test is ignored", name.endsWith("-Ignore")); - + PhysicalPlan p = testContext.optimizeAndPlan(query); assertEquals(EsQueryExec.class, p.getClass()); EsQueryExec eqe = (EsQueryExec) p; diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/planner/QueryTranslatorTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/planner/QueryTranslatorTests.java index 6987d2a5277e9..3553d862831c3 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/planner/QueryTranslatorTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/planner/QueryTranslatorTests.java @@ -148,7 +148,7 @@ private PhysicalPlan optimizeAndPlan(String sql) { } private PhysicalPlan optimizeAndPlan(LogicalPlan plan) { - return planner.plan(optimizer.optimize(plan),true); + return planner.plan(optimizer.optimize(plan), true); } private LogicalPlan parameterizedSql(String sql, SqlTypedParamValue... params) { @@ -221,6 +221,7 @@ public void testAliasAndGroupByResolution() { assertEquals("c", (agg.get(0)).name()); assertEquals("COUNT", ((Count) ((Alias) agg.get(0)).child()).functionName()); } + public void testLiteralWithGroupBy() { LogicalPlan p = plan("SELECT 1 as t, 2 FROM test GROUP BY int"); assertTrue(p instanceof Aggregate); @@ -247,21 +248,21 @@ public void testComparisonAgainstColumns() { } public void testMathFunctionHavingClause() { - MathOperation operation = - (MathOperation) randomFrom(Stream.of(MathOperation.values()).filter(o -> o != PI && o != E).toArray()); + MathOperation operation = (MathOperation) randomFrom(Stream.of(MathOperation.values()).filter(o -> o != PI && o != E).toArray()); - LogicalPlan p = plan("SELECT keyword, max(int) FROM test GROUP BY keyword HAVING " + - operation.name() + "(max(int)) > 10"); + LogicalPlan p = plan("SELECT keyword, max(int) FROM test GROUP BY keyword HAVING " + operation.name() + "(max(int)) > 10"); assertTrue(p instanceof Filter); Expression condition = ((Filter) p).condition(); assertFalse(condition.foldable()); QueryTranslation translation = translateWithAggs(condition); assertNull(translation.query); AggFilter aggFilter = translation.aggFilter; - assertEquals("InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(InternalSqlScriptUtils." + - operation.name().toLowerCase(Locale.ROOT) + - "(InternalQlScriptUtils.nullSafeCastNumeric(params.a0,params.v0)),params.v1))", - aggFilter.scriptTemplate().toString()); + assertEquals( + "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(InternalSqlScriptUtils." + + operation.name().toLowerCase(Locale.ROOT) + + "(InternalQlScriptUtils.nullSafeCastNumeric(params.a0,params.v0)),params.v1))", + aggFilter.scriptTemplate().toString() + ); assertThat(aggFilter.scriptTemplate().params().toString(), startsWith("[{a=max(int)")); assertThat(aggFilter.scriptTemplate().params().toString(), endsWith(", {v=10}]")); } @@ -281,9 +282,13 @@ public void testHavingWithColumnImplicitGrouping() { EsQueryExec eqe = (EsQueryExec) p; assertTrue("Should be tracking hits", eqe.queryContainer().shouldTrackHits()); assertEquals(1, eqe.output().size()); - assertThat(eqe.queryContainer().toString().replaceAll("\\s+", ""), containsString( + assertThat( + eqe.queryContainer().toString().replaceAll("\\s+", ""), + containsString( "\"script\":{\"source\":\"InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(params.a0,params.v0))\"," - + "\"lang\":\"painless\",\"params\":{\"v0\":0}}")); + + "\"lang\":\"painless\",\"params\":{\"v0\":0}}" + ) + ); } public void testScriptsInsideAggregateFunctions() { @@ -297,23 +302,23 @@ public void testScriptsInsideAggregateFunctions() { PhysicalPlan p = optimizeAndPlan("SELECT " + aggFunction + " FROM test"); if (fd.clazz() == Count.class) { assertESQuery( - p, - containsString( - ":{\"script\":{\"source\":\"InternalQlScriptUtils.isNotNull(InternalSqlScriptUtils.add(" - + "InternalSqlScriptUtils.abs(InternalSqlScriptUtils.div(InternalSqlScriptUtils.mul(" - + "InternalQlScriptUtils.docValue(doc,params.v0),params.v1),params.v2)),params.v3))\"," - + "\"lang\":\"painless\",\"params\":{\"v0\":\"int\",\"v1\":10,\"v2\":3,\"v3\":1}}" - ) + p, + containsString( + ":{\"script\":{\"source\":\"InternalQlScriptUtils.isNotNull(InternalSqlScriptUtils.add(" + + "InternalSqlScriptUtils.abs(InternalSqlScriptUtils.div(InternalSqlScriptUtils.mul(" + + "InternalQlScriptUtils.docValue(doc,params.v0),params.v1),params.v2)),params.v3))\"," + + "\"lang\":\"painless\",\"params\":{\"v0\":\"int\",\"v1\":10,\"v2\":3,\"v3\":1}}" + ) ); } else { assertESQuery( - p, - containsString( - ":{\"script\":{\"source\":\"InternalSqlScriptUtils.add(InternalSqlScriptUtils.abs(" - + "InternalSqlScriptUtils.div(InternalSqlScriptUtils.mul(InternalQlScriptUtils.docValue(" - + "doc,params.v0),params.v1),params.v2)),params.v3)\",\"lang\":\"painless\",\"params\":{" - + "\"v0\":\"int\",\"v1\":10,\"v2\":3,\"v3\":1}}" - ) + p, + containsString( + ":{\"script\":{\"source\":\"InternalSqlScriptUtils.add(InternalSqlScriptUtils.abs(" + + "InternalSqlScriptUtils.div(InternalSqlScriptUtils.mul(InternalQlScriptUtils.docValue(" + + "doc,params.v0),params.v1),params.v2)),params.v3)\",\"lang\":\"painless\",\"params\":{" + + "\"v0\":\"int\",\"v1\":10,\"v2\":3,\"v3\":1}}" + ) ); } } @@ -322,10 +327,9 @@ public void testScriptsInsideAggregateFunctions() { public void testScriptsInsideAggregateFunctionsWithHaving() { for (FunctionDefinition fd : defaultTestContext.sqlFunctionRegistry.listFunctions()) { - if (AggregateFunction.class.isAssignableFrom(fd.clazz()) - && (MatrixStatsEnclosed.class.isAssignableFrom(fd.clazz()) == false) - // First/Last don't support having: https://github.com/elastic/elasticsearch/issues/37938 - && (TopHits.class.isAssignableFrom(fd.clazz()) == false)) { + if (AggregateFunction.class.isAssignableFrom(fd.clazz()) && (MatrixStatsEnclosed.class.isAssignableFrom(fd.clazz()) == false) + // First/Last don't support having: https://github.com/elastic/elasticsearch/issues/37938 + && (TopHits.class.isAssignableFrom(fd.clazz()) == false)) { String aggFunction = fd.name() + "(ABS((int * 10) / 3) + 1"; if (fd.clazz() == Percentile.class || fd.clazz() == PercentileRank.class) { aggFunction += ", 50"; @@ -350,8 +354,8 @@ public void testScriptsInsideAggregateFunctionsWithHaving() { String param1st = typeName.length() == 0 ? "params.a0" : safeCast; String param2nd = typeName.length() == 0 ? "params.v0" : "params.v1"; assertEquals( - "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(" + param1st + "," + param2nd + "))", - aggFilter.scriptTemplate().toString() + "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(" + param1st + "," + param2nd + "))", + aggFilter.scriptTemplate().toString() ); String params = "[{a=" + aggFunction + "}," + (typeName.length() == 0 ? "" : " {v=" + typeName + "},") + " {v=20}]"; assertEquals(params, aggFilter.scriptTemplate().params().toString()); @@ -360,10 +364,14 @@ public void testScriptsInsideAggregateFunctionsWithHaving() { } public void testFoldingWithParamsWithoutIndex() { - PhysicalPlan p = optimizeAndPlan(parameterizedSql("SELECT ?, ?, ? FROM test", + PhysicalPlan p = optimizeAndPlan( + parameterizedSql( + "SELECT ?, ?, ? FROM test", new SqlTypedParamValue("integer", 100), new SqlTypedParamValue("integer", 100), - new SqlTypedParamValue("integer", 200))); + new SqlTypedParamValue("integer", 200) + ) + ); assertThat(p.output(), everyItem(instanceOf(ReferenceAttribute.class))); assertThat(p.output().get(0).toString(), startsWith("?{r}#")); assertThat(p.output().get(1).toString(), startsWith("?{r}#")); @@ -372,8 +380,7 @@ public void testFoldingWithParamsWithoutIndex() { } public void testSameAliasForParamAndField() { - PhysicalPlan p = optimizeAndPlan(parameterizedSql("SELECT ?, int as \"?\" FROM test", - new SqlTypedParamValue("integer", 100))); + PhysicalPlan p = optimizeAndPlan(parameterizedSql("SELECT ?, int as \"?\" FROM test", new SqlTypedParamValue("integer", 100))); assertThat(p.output(), everyItem(instanceOf(ReferenceAttribute.class))); assertThat(p.output().get(0).toString(), startsWith("?{r}#")); assertThat(p.output().get(1).toString(), startsWith("?{r}#")); @@ -388,9 +395,9 @@ public void testSameAliasOnSameField() { } public void testFoldingWithMixedParamsWithoutAlias() { - PhysicalPlan p = optimizeAndPlan(parameterizedSql("SELECT ?, ? FROM test", - new SqlTypedParamValue("integer", 100), - new SqlTypedParamValue("text", "200"))); + PhysicalPlan p = optimizeAndPlan( + parameterizedSql("SELECT ?, ? FROM test", new SqlTypedParamValue("integer", 100), new SqlTypedParamValue("text", "200")) + ); assertThat(p.output(), everyItem(instanceOf(ReferenceAttribute.class))); assertThat(p.output().get(0).toString(), startsWith("?{r}#")); assertThat(p.output().get(1).toString(), startsWith("?{r}#")); @@ -500,78 +507,68 @@ public void testDateRangeCast() { public void testDateRangeWithCurrentTimestamp() { Integer nanoPrecision = randomPrecision(); testDateRangeWithCurrentFunctions( - functionWithPrecision("CURRENT_TIMESTAMP", nanoPrecision), - DATE_FORMAT, - nanoPrecision, - TEST_CFG.now() + functionWithPrecision("CURRENT_TIMESTAMP", nanoPrecision), + DATE_FORMAT, + nanoPrecision, + TEST_CFG.now() ); testDateRangeWithCurrentFunctionsAndRangeOptimization( - functionWithPrecision("CURRENT_TIMESTAMP", nanoPrecision), - DATE_FORMAT, - nanoPrecision, - TEST_CFG.now().minusDays(1L).minusSeconds(1L), - TEST_CFG.now().plusDays(1L).plusSeconds(1L) + functionWithPrecision("CURRENT_TIMESTAMP", nanoPrecision), + DATE_FORMAT, + nanoPrecision, + TEST_CFG.now().minusDays(1L).minusSeconds(1L), + TEST_CFG.now().plusDays(1L).plusSeconds(1L) ); } public void testDateRangeWithCurrentDate() { testDateRangeWithCurrentFunctions("CURRENT_DATE()", DATE_FORMAT, null, DateUtils.asDateOnly(TEST_CFG.now())); testDateRangeWithCurrentFunctionsAndRangeOptimization( - "CURRENT_DATE()", - DATE_FORMAT, - null, - DateUtils.asDateOnly(TEST_CFG.now().minusDays(1L)).minusSeconds(1), - DateUtils.asDateOnly(TEST_CFG.now().plusDays(1L)).plusSeconds(1) + "CURRENT_DATE()", + DATE_FORMAT, + null, + DateUtils.asDateOnly(TEST_CFG.now().minusDays(1L)).minusSeconds(1), + DateUtils.asDateOnly(TEST_CFG.now().plusDays(1L)).plusSeconds(1) ); } public void testDateRangeWithToday() { testDateRangeWithCurrentFunctions("TODAY()", DATE_FORMAT, null, DateUtils.asDateOnly(TEST_CFG.now())); testDateRangeWithCurrentFunctionsAndRangeOptimization( - "TODAY()", - DATE_FORMAT, - null, - DateUtils.asDateOnly(TEST_CFG.now().minusDays(1L)).minusSeconds(1), - DateUtils.asDateOnly(TEST_CFG.now().plusDays(1L)).plusSeconds(1) + "TODAY()", + DATE_FORMAT, + null, + DateUtils.asDateOnly(TEST_CFG.now().minusDays(1L)).minusSeconds(1), + DateUtils.asDateOnly(TEST_CFG.now().plusDays(1L)).plusSeconds(1) ); } public void testDateRangeWithNow() { Integer nanoPrecision = randomPrecision(); - testDateRangeWithCurrentFunctions( - functionWithPrecision("NOW", nanoPrecision), - DATE_FORMAT, - nanoPrecision, - TEST_CFG.now() - ); + testDateRangeWithCurrentFunctions(functionWithPrecision("NOW", nanoPrecision), DATE_FORMAT, nanoPrecision, TEST_CFG.now()); testDateRangeWithCurrentFunctionsAndRangeOptimization( - functionWithPrecision("NOW", nanoPrecision), - DATE_FORMAT, - nanoPrecision, - TEST_CFG.now().minusDays(1L).minusSeconds(1L), - TEST_CFG.now().plusDays(1L).plusSeconds(1L) + functionWithPrecision("NOW", nanoPrecision), + DATE_FORMAT, + nanoPrecision, + TEST_CFG.now().minusDays(1L).minusSeconds(1L), + TEST_CFG.now().plusDays(1L).plusSeconds(1L) ); } public void testDateRangeWithCurrentTime() { Integer nanoPrecision = randomPrecision(); - testDateRangeWithCurrentFunctions( - functionWithPrecision("CURRENT_TIME", nanoPrecision), - TIME_FORMAT, - nanoPrecision, - TEST_CFG.now() - ); + testDateRangeWithCurrentFunctions(functionWithPrecision("CURRENT_TIME", nanoPrecision), TIME_FORMAT, nanoPrecision, TEST_CFG.now()); testDateRangeWithCurrentFunctionsAndRangeOptimization( - functionWithPrecision("CURRENT_TIME", nanoPrecision), - TIME_FORMAT, - nanoPrecision, - TEST_CFG.now().minusDays(1L).minusSeconds(1L), - TEST_CFG.now().plusDays(1L).plusSeconds(1L) + functionWithPrecision("CURRENT_TIME", nanoPrecision), + TIME_FORMAT, + nanoPrecision, + TEST_CFG.now().minusDays(1L).minusSeconds(1L), + TEST_CFG.now().plusDays(1L).plusSeconds(1L) ); } private Integer randomPrecision() { - return randomFrom(new Integer[] {null, 1, 2, 3, 4, 5, 6, 7, 8, 9}); + return randomFrom(new Integer[] { null, 1, 2, 3, 4, 5, 6, 7, 8, 9 }); } private String functionWithPrecision(String function, Integer precision) { @@ -601,12 +598,18 @@ private void testDateRangeWithCurrentFunctions(String function, String pattern, assertEquals("date", rq.field()); if (operator.contains("<") || operator.equals("=") || operator.equals("!=")) { - assertEquals(DateFormatter.forPattern(pattern).format(now.withNano(DateUtils.getNanoPrecision( - nanoPrecision == null ? null : literal(nanoPrecision), now.getNano()))), rq.upper()); + assertEquals( + DateFormatter.forPattern(pattern) + .format(now.withNano(DateUtils.getNanoPrecision(nanoPrecision == null ? null : literal(nanoPrecision), now.getNano()))), + rq.upper() + ); } if (operator.contains(">") || operator.equals("=") || operator.equals("!=")) { - assertEquals(DateFormatter.forPattern(pattern).format(now.withNano(DateUtils.getNanoPrecision( - nanoPrecision == null ? null : literal(nanoPrecision), now.getNano()))), rq.lower()); + assertEquals( + DateFormatter.forPattern(pattern) + .format(now.withNano(DateUtils.getNanoPrecision(nanoPrecision == null ? null : literal(nanoPrecision), now.getNano()))), + rq.lower() + ); } assertEquals(operator.equals("=") || operator.equals("!=") || operator.equals("<="), rq.includeUpper()); @@ -615,19 +618,29 @@ private void testDateRangeWithCurrentFunctions(String function, String pattern, } private void testDateRangeWithCurrentFunctionsAndRangeOptimization( - String function, - String pattern, - Integer nanoPrecision, - ZonedDateTime lowerValue, - ZonedDateTime upperValue + String function, + String pattern, + Integer nanoPrecision, + ZonedDateTime lowerValue, + ZonedDateTime upperValue ) { String lowerOperator = randomFrom("<", "<="); String upperOperator = randomFrom(">", ">="); // use both date-only interval (1 DAY) and time-only interval (1 second) to cover CURRENT_TIMESTAMP and TODAY scenarios String interval = "(INTERVAL 1 DAY + INTERVAL 1 SECOND)"; - PhysicalPlan p = optimizeAndPlan("SELECT some.string FROM test WHERE date" + lowerOperator + function + " + " + interval - + " AND date " + upperOperator + function + " - " + interval); + PhysicalPlan p = optimizeAndPlan( + "SELECT some.string FROM test WHERE date" + + lowerOperator + + function + + " + " + + interval + + " AND date " + + upperOperator + + function + + " - " + + interval + ); assertEquals(EsQueryExec.class, p.getClass()); EsQueryExec eqe = (EsQueryExec) p; assertEquals(1, eqe.output().size()); @@ -641,12 +654,24 @@ private void testDateRangeWithCurrentFunctionsAndRangeOptimization( RangeQuery rq = (RangeQuery) query; assertEquals("date", rq.field()); - assertEquals(DateFormatter.forPattern(pattern) - .format(upperValue.withNano(DateUtils.getNanoPrecision( - nanoPrecision == null ? null : literal(nanoPrecision), upperValue.getNano()))), rq.upper()); - assertEquals(DateFormatter.forPattern(pattern) - .format(lowerValue.withNano(DateUtils.getNanoPrecision( - nanoPrecision == null ? null : literal(nanoPrecision), lowerValue.getNano()))), rq.lower()); + assertEquals( + DateFormatter.forPattern(pattern) + .format( + upperValue.withNano( + DateUtils.getNanoPrecision(nanoPrecision == null ? null : literal(nanoPrecision), upperValue.getNano()) + ) + ), + rq.upper() + ); + assertEquals( + DateFormatter.forPattern(pattern) + .format( + lowerValue.withNano( + DateUtils.getNanoPrecision(nanoPrecision == null ? null : literal(nanoPrecision), lowerValue.getNano()) + ) + ), + rq.lower() + ); assertEquals(lowerOperator.equals("<="), rq.includeUpper()); assertEquals(upperOperator.equals(">="), rq.includeLower()); @@ -698,7 +723,7 @@ public void testDateRangeWithESDateMath() { public void testInExpressionWhereClauseDatetime() { ZoneId zoneId = randomZone(); - String[] dates = {"2002-02-02T02:02:02.222Z", "2003-03-03T03:03:03.333Z"}; + String[] dates = { "2002-02-02T02:02:02.222Z", "2003-03-03T03:03:03.333Z" }; LogicalPlan p = plan("SELECT * FROM test WHERE date IN ('" + dates[0] + "'::datetime, '" + dates[1] + "'::datetime)", zoneId); assertTrue(p instanceof Project); p = ((Project) p).child(); @@ -712,10 +737,12 @@ public void testInExpressionWhereClauseDatetime() { assertFalse(bq.isAnd()); assertTrue(bq.left() instanceof RangeQuery); assertTrue(bq.right() instanceof RangeQuery); - List> tuples = asList(new Tuple<>(dates[0], (RangeQuery)bq.left()), - new Tuple<>(dates[1], (RangeQuery) bq.right())); + List> tuples = asList( + new Tuple<>(dates[0], (RangeQuery) bq.left()), + new Tuple<>(dates[1], (RangeQuery) bq.right()) + ); - for (Tuple t: tuples) { + for (Tuple t : tuples) { String date = t.v1(); RangeQuery rq = t.v2(); @@ -732,9 +759,12 @@ public void testInExpressionWhereClauseDatetime() { public void testChronoFieldBasedDateTimeFunctionsWithMathIntervalAndGroupBy() { DateTimeExtractor randomFunction = randomValueOtherThan(DateTimeExtractor.YEAR, () -> randomFrom(DateTimeExtractor.values())); PhysicalPlan p = optimizeAndPlan( - "SELECT " - + randomFunction.name() - + "(date + INTERVAL 1 YEAR) FROM test GROUP BY " + randomFunction.name() + "(date + INTERVAL 1 YEAR)"); + "SELECT " + + randomFunction.name() + + "(date + INTERVAL 1 YEAR) FROM test GROUP BY " + + randomFunction.name() + + "(date + INTERVAL 1 YEAR)" + ); assertESQuery( p, containsString( @@ -751,20 +781,24 @@ public void testChronoFieldBasedDateTimeFunctionsWithMathIntervalAndGroupBy() { } public void testDateTimeFunctionsWithMathIntervalAndGroupBy() { - String[] functions = new String[] {"DAY_NAME", "MONTH_NAME", "DAY_OF_WEEK", "WEEK_OF_YEAR", "QUARTER"}; - String[] scriptMethods = new String[] {"dayName", "monthName", "dayOfWeek", "weekOfYear", "quarter"}; + String[] functions = new String[] { "DAY_NAME", "MONTH_NAME", "DAY_OF_WEEK", "WEEK_OF_YEAR", "QUARTER" }; + String[] scriptMethods = new String[] { "dayName", "monthName", "dayOfWeek", "weekOfYear", "quarter" }; int pos = randomIntBetween(0, functions.length - 1); PhysicalPlan p = optimizeAndPlan( - "SELECT " - + functions[pos] - + "(date + INTERVAL 1 YEAR) FROM test GROUP BY " + functions[pos] + "(date + INTERVAL 1 YEAR)"); + "SELECT " + functions[pos] + "(date + INTERVAL 1 YEAR) FROM test GROUP BY " + functions[pos] + "(date + INTERVAL 1 YEAR)" + ); assertEquals(EsQueryExec.class, p.getClass()); EsQueryExec eqe = (EsQueryExec) p; - assertThat(eqe.queryContainer().toString().replaceAll("\\s+", ""), containsString( - "{\"terms\":{\"script\":{\"source\":\"InternalSqlScriptUtils." + scriptMethods[pos] - + "(InternalSqlScriptUtils.add(InternalQlScriptUtils.docValue(doc,params.v0)," - + "InternalSqlScriptUtils.intervalYearMonth(params.v1,params.v2)),params.v3)\",\"lang\":\"painless\"," - + "\"params\":{\"v0\":\"date\",\"v1\":\"P1Y\",\"v2\":\"INTERVAL_YEAR\",\"v3\":\"Z\"}},\"missing_bucket\":true,")); + assertThat( + eqe.queryContainer().toString().replaceAll("\\s+", ""), + containsString( + "{\"terms\":{\"script\":{\"source\":\"InternalSqlScriptUtils." + + scriptMethods[pos] + + "(InternalSqlScriptUtils.add(InternalQlScriptUtils.docValue(doc,params.v0)," + + "InternalSqlScriptUtils.intervalYearMonth(params.v1,params.v2)),params.v3)\",\"lang\":\"painless\"," + + "\"params\":{\"v0\":\"date\",\"v1\":\"P1Y\",\"v2\":\"INTERVAL_YEAR\",\"v3\":\"Z\"}},\"missing_bucket\":true," + ) + ); } // Like/RLike/StartsWith @@ -795,8 +829,20 @@ public void testDifferentLikeAndNotLikePatterns() { } public void testRLikePatterns() { - String[] patterns = new String[] {"(...)+", "abab(ab)?", "(ab){1,2}", "(ab){3}", "aabb|bbaa", "a+b+|b+a+", "aa(cc|bb)", - "a{4,6}b{4,6}", ".{3}.{3}", "aaa*bbb*", "a+.+", "a.c.e", "[^abc\\-]"}; + String[] patterns = new String[] { + "(...)+", + "abab(ab)?", + "(ab){1,2}", + "(ab){3}", + "aabb|bbaa", + "a+b+|b+a+", + "aa(cc|bb)", + "a{4,6}b{4,6}", + ".{3}.{3}", + "aaa*bbb*", + "a+.+", + "a.c.e", + "[^abc\\-]" }; for (int i = 0; i < 5; i++) { assertDifferentRLikeAndNotRLikePatterns(randomFrom(patterns), randomFrom(patterns)); } @@ -853,8 +899,10 @@ public void testStartsWithUsesPrefixQuery() { } public void testStartsWithUsesPrefixQueryAndScript() { - LogicalPlan p = plan("SELECT keyword FROM test WHERE STARTS_WITH(keyword, 'x') AND STARTS_WITH(keyword, 'xy') " - + "AND STARTS_WITH(LCASE(keyword), 'xyz')"); + LogicalPlan p = plan( + "SELECT keyword FROM test WHERE STARTS_WITH(keyword, 'x') AND STARTS_WITH(keyword, 'xy') " + + "AND STARTS_WITH(LCASE(keyword), 'xyz')" + ); assertTrue(p instanceof Project); assertTrue(p.children().get(0) instanceof Filter); @@ -880,10 +928,12 @@ public void testStartsWithUsesPrefixQueryAndScript() { assertEquals("x", pql.query()); ScriptQuery sq = (ScriptQuery) bq.right(); - assertEquals("InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.startsWith(" + assertEquals( + "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.startsWith(" + "InternalSqlScriptUtils.lcase(InternalQlScriptUtils.docValue(doc,params.v0)), " + "params.v1, params.v2))", - sq.script().toString()); + sq.script().toString() + ); assertEquals("[{v=keyword}, {v=xyz}, {v=false}]", sq.script().params().toString()); } @@ -893,9 +943,13 @@ public void testTrimWhereClause() { String trimFunctionName = trimFunction.getSimpleName().toUpperCase(Locale.ROOT); LogicalPlan p = plan("SELECT " + trimFunctionName + "(keyword) trimmed FROM test WHERE " + trimFunctionName + "(keyword) = 'foo'"); - assertESQuery(p, - containsString("InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.eq(InternalSqlScriptUtils." + - trimFunctionName.toLowerCase(Locale.ROOT) + "(InternalQlScriptUtils.docValue(doc,params.v0)),params.v1))"), + assertESQuery( + p, + containsString( + "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.eq(InternalSqlScriptUtils." + + trimFunctionName.toLowerCase(Locale.ROOT) + + "(InternalQlScriptUtils.docValue(doc,params.v0)),params.v1))" + ), containsString("\"params\":{\"v0\":\"keyword\",\"v1\":\"foo\"}") ); } @@ -904,8 +958,9 @@ public void testTrimWhereClause() { public void testTrimGroupBy() { Class trimFunction = randomFrom(Trim.class, LTrim.class, RTrim.class); String trimFunctionName = trimFunction.getSimpleName().toUpperCase(Locale.ROOT); - LogicalPlan p = plan("SELECT " + trimFunctionName + "(keyword) trimmed, count(*) FROM test GROUP BY " + - trimFunctionName + "(keyword)"); + LogicalPlan p = plan( + "SELECT " + trimFunctionName + "(keyword) trimmed, count(*) FROM test GROUP BY " + trimFunctionName + "(keyword)" + ); assertEquals(Aggregate.class, p.getClass()); Aggregate agg = (Aggregate) p; @@ -913,7 +968,7 @@ public void testTrimGroupBy() { assertEquals(2, agg.aggregates().size()); assertEquals(trimFunction, agg.groupings().get(0).getClass()); assertEquals(trimFunction, ((Alias) agg.aggregates().get(0)).child().getClass()); - assertEquals(Count.class,((Alias) agg.aggregates().get(1)).child().getClass()); + assertEquals(Count.class, ((Alias) agg.aggregates().get(1)).child().getClass()); UnaryStringFunction trim = (UnaryStringFunction) agg.groupings().get(0); assertEquals(1, trim.children().size()); @@ -921,7 +976,7 @@ public void testTrimGroupBy() { GroupingContext groupingContext = QueryFolder.FoldAggregate.groupBy(agg.groupings()); assertNotNull(groupingContext); assertESQuery( - p, + p, containsString( "InternalSqlScriptUtils." + trimFunctionName.toLowerCase(Locale.ROOT) + "(InternalQlScriptUtils.docValue(doc,params.v0))" ), @@ -977,8 +1032,7 @@ public void testGroupByHistogramWithDate() { } public void testGroupByHistogramWithDateAndSmallInterval() { - PhysicalPlan p = optimizeAndPlan("SELECT MAX(int) FROM test GROUP BY " + - "HISTOGRAM(CAST(date AS DATE), INTERVAL 5 MINUTES)"); + PhysicalPlan p = optimizeAndPlan("SELECT MAX(int) FROM test GROUP BY " + "HISTOGRAM(CAST(date AS DATE), INTERVAL 5 MINUTES)"); assertEquals(EsQueryExec.class, p.getClass()); EsQueryExec eqe = (EsQueryExec) p; assertEquals(1, eqe.queryContainer().aggs().groups().size()); @@ -988,8 +1042,9 @@ public void testGroupByHistogramWithDateAndSmallInterval() { public void testGroupByHistogramWithDateTruncateIntervalToDayMultiples() { { - PhysicalPlan p = optimizeAndPlan("SELECT MAX(int) FROM test GROUP BY " + - "HISTOGRAM(CAST(date AS DATE), INTERVAL '2 3:04' DAY TO MINUTE)"); + PhysicalPlan p = optimizeAndPlan( + "SELECT MAX(int) FROM test GROUP BY " + "HISTOGRAM(CAST(date AS DATE), INTERVAL '2 3:04' DAY TO MINUTE)" + ); assertEquals(EsQueryExec.class, p.getClass()); EsQueryExec eqe = (EsQueryExec) p; assertEquals(1, eqe.queryContainer().aggs().groups().size()); @@ -997,8 +1052,9 @@ public void testGroupByHistogramWithDateTruncateIntervalToDayMultiples() { assertEquals(172800000L, ((GroupByDateHistogram) eqe.queryContainer().aggs().groups().get(0)).fixedInterval()); } { - PhysicalPlan p = optimizeAndPlan("SELECT MAX(int) FROM test GROUP BY " + - "HISTOGRAM(CAST(date AS DATE), INTERVAL 4409 MINUTES)"); + PhysicalPlan p = optimizeAndPlan( + "SELECT MAX(int) FROM test GROUP BY " + "HISTOGRAM(CAST(date AS DATE), INTERVAL 4409 MINUTES)" + ); assertEquals(EsQueryExec.class, p.getClass()); EsQueryExec eqe = (EsQueryExec) p; assertEquals(1, eqe.queryContainer().aggs().groups().size()); @@ -1010,9 +1066,11 @@ public void testGroupByHistogramWithDateTruncateIntervalToDayMultiples() { // Count /////////// public void testAllCountVariantsWithHavingGenerateCorrectAggregations() { - PhysicalPlan p = optimizeAndPlan("SELECT AVG(int), COUNT(keyword) ln, COUNT(distinct keyword) dln, COUNT(some.dotted.field) fn," + PhysicalPlan p = optimizeAndPlan( + "SELECT AVG(int), COUNT(keyword) ln, COUNT(distinct keyword) dln, COUNT(some.dotted.field) fn," + "COUNT(distinct some.dotted.field) dfn, COUNT(*) ccc FROM test GROUP BY bool " - + "HAVING dln > 3 AND ln > 32 AND dfn > 1 AND fn > 2 AND ccc > 5 AND AVG(int) > 50000"); + + "HAVING dln > 3 AND ln > 32 AND dfn > 1 AND fn > 2 AND ccc > 5 AND AVG(int) > 50000" + ); assertEquals(EsQueryExec.class, p.getClass()); EsQueryExec ee = (EsQueryExec) p; assertEquals(6, ee.output().size()); @@ -1048,41 +1106,58 @@ public void testAllCountVariantsWithHavingGenerateCorrectAggregations() { CardinalityAggregationBuilder cardinalityDottedField = (CardinalityAggregationBuilder) subAggs.toArray()[4]; assertEquals("some.dotted.field", cardinalityDottedField.field()); - assertThat(ee.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""), - endsWith("{\"buckets_path\":{" - + "\"a0\":\"" + cardinalityKeyword.getName() + "\"," - + "\"a1\":\"" + existsKeyword.getName() + "._count\"," - + "\"a2\":\"" + cardinalityDottedField.getName() + "\"," - + "\"a3\":\"" + existsDottedField.getName() + "._count\"," - + "\"a4\":\"_count\"," - + "\"a5\":\"" + avgInt.getName() + "\"}," - + "\"script\":{\"source\":\"" - + "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.and(" - + "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.and(" - + "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.and(" - + "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.and(" - + "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.and(" - + "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(params.a0,params.v0))," - + "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(params.a1,params.v1))))," - + "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(params.a2,params.v2))))," - + "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(params.a3,params.v3))))," - + "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(params.a4,params.v4))))," - + "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(params.a5,params.v5))))\"," - + "\"lang\":\"painless\",\"params\":{\"v0\":3,\"v1\":32,\"v2\":1,\"v3\":2,\"v4\":5,\"v5\":50000}}," - + "\"gap_policy\":\"skip\"}}}}}")); + assertThat( + ee.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""), + endsWith( + "{\"buckets_path\":{" + + "\"a0\":\"" + + cardinalityKeyword.getName() + + "\"," + + "\"a1\":\"" + + existsKeyword.getName() + + "._count\"," + + "\"a2\":\"" + + cardinalityDottedField.getName() + + "\"," + + "\"a3\":\"" + + existsDottedField.getName() + + "._count\"," + + "\"a4\":\"_count\"," + + "\"a5\":\"" + + avgInt.getName() + + "\"}," + + "\"script\":{\"source\":\"" + + "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.and(" + + "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.and(" + + "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.and(" + + "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.and(" + + "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.and(" + + "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(params.a0,params.v0))," + + "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(params.a1,params.v1))))," + + "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(params.a2,params.v2))))," + + "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(params.a3,params.v3))))," + + "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(params.a4,params.v4))))," + + "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(params.a5,params.v5))))\"," + + "\"lang\":\"painless\",\"params\":{\"v0\":3,\"v1\":32,\"v2\":1,\"v3\":2,\"v4\":5,\"v5\":50000}}," + + "\"gap_policy\":\"skip\"}}}}}" + ) + ); } - // Stats/Extended Stats /////////////////////// public void testExtendedStatsAggsStddevAndVar() { - final Map metricToAgg = Map.of( - "STDDEV_POP", "std_deviation", - "STDDEV_SAMP", "std_deviation_sampling", - "VAR_POP", "variance", - "VAR_SAMP", "variance_sampling" + final Map metricToAgg = Map.of( + "STDDEV_POP", + "std_deviation", + "STDDEV_SAMP", + "std_deviation_sampling", + "VAR_POP", + "variance", + "VAR_SAMP", + "variance_sampling" ); - for (String funcName: metricToAgg.keySet()) { + for (String funcName : metricToAgg.keySet()) { PhysicalPlan p = optimizeAndPlan("SELECT " + funcName + "(int) FROM test"); assertEquals(EsQueryExec.class, p.getClass()); EsQueryExec eqe = (EsQueryExec) p; @@ -1096,10 +1171,7 @@ public void testExtendedStatsAggsStddevAndVar() { assertEquals(((MetricAggRef) fe).property(), metricToAgg.get(funcName)); String aggName = eqe.queryContainer().aggs().asAggBuilder().getSubAggregations().iterator().next().getName(); - assertESQuery( - p, - endsWith("\"aggregations\":{\"" + aggName + "\":{\"extended_stats\":{\"field\":\"int\",\"sigma\":2.0}}}}}}") - ); + assertESQuery(p, endsWith("\"aggregations\":{\"" + aggName + "\":{\"extended_stats\":{\"field\":\"int\",\"sigma\":2.0}}}}}}")); } } @@ -1110,31 +1182,32 @@ public void testScriptsInsideAggregateFunctionsExtendedStats() { String aggFunction = fd.name() + "(ABS((int * 10) / 3) + 1)"; PhysicalPlan p = optimizeAndPlan("SELECT " + aggFunction + " FROM test"); assertESQuery( - p, - containsString( - "{\"extended_stats\":{\"script\":{\"source\":\"InternalSqlScriptUtils.add(InternalSqlScriptUtils.abs(" - + "InternalSqlScriptUtils.div(InternalSqlScriptUtils.mul(InternalQlScriptUtils.docValue(" - + "doc,params.v0),params.v1),params.v2)),params.v3)\",\"lang\":\"painless\",\"params\":{" - + "\"v0\":\"int\",\"v1\":10,\"v2\":3,\"v3\":1}}" - ) + p, + containsString( + "{\"extended_stats\":{\"script\":{\"source\":\"InternalSqlScriptUtils.add(InternalSqlScriptUtils.abs(" + + "InternalSqlScriptUtils.div(InternalSqlScriptUtils.mul(InternalQlScriptUtils.docValue(" + + "doc,params.v0),params.v1),params.v2)),params.v3)\",\"lang\":\"painless\",\"params\":{" + + "\"v0\":\"int\",\"v1\":10,\"v2\":3,\"v3\":1}}" + ) ); } } } - @SuppressWarnings({"rawtypes"}) + @SuppressWarnings({ "rawtypes" }) public void testPercentileMethodParametersSameAsDefault() { BiConsumer> test = (fnName, pctOrValFn) -> { final int fieldCount = 5; final String sql = ("SELECT " + - // 0-3: these all should fold into the same aggregation - " PERCENTILE(int, 50, 'tdigest', 79.8 + 20.2), " + - " PERCENTILE(int, 40 + 10, 'tdigest', null), " + - " PERCENTILE(int, 50, 'tdigest'), " + - " PERCENTILE(int, 50), " + - // 4: this has a different method parameter - // just to make sure we don't fold everything to default - " PERCENTILE(int, 50, 'tdigest', 22) " + // 0-3: these all should fold into the same aggregation + " PERCENTILE(int, 50, 'tdigest', 79.8 + 20.2), " + + " PERCENTILE(int, 40 + 10, 'tdigest', null), " + + " PERCENTILE(int, 50, 'tdigest'), " + + " PERCENTILE(int, 50), " + + + // 4: this has a different method parameter + // just to make sure we don't fold everything to default + " PERCENTILE(int, 50, 'tdigest', 22) " + "FROM test").replaceAll("PERCENTILE", fnName); List aggs = percentilesAggsByField(optimizeAndPlan(sql), fieldCount); @@ -1151,46 +1224,43 @@ public void testPercentileMethodParametersSameAsDefault() { assertArrayEquals(new double[] { 50 }, pctOrValFn.apply(aggs.get(4)), 0); }; - test.accept("PERCENTILE", p -> ((PercentilesAggregationBuilder)p).percentiles()); - test.accept("PERCENTILE_RANK", p -> ((PercentileRanksAggregationBuilder)p).values()); + test.accept("PERCENTILE", p -> ((PercentilesAggregationBuilder) p).percentiles()); + test.accept("PERCENTILE_RANK", p -> ((PercentileRanksAggregationBuilder) p).values()); } - @SuppressWarnings({"rawtypes"}) + @SuppressWarnings({ "rawtypes" }) public void testPercentileOptimization() { BiConsumer> test = (fnName, pctOrValFn) -> { final int fieldCount = 5; final String sql = ("SELECT " + - // 0-1: fold into the same aggregation - " PERCENTILE(int, 50, 'tdigest'), " + - " PERCENTILE(int, 60, 'tdigest'), " + + // 0-1: fold into the same aggregation + " PERCENTILE(int, 50, 'tdigest'), " + " PERCENTILE(int, 60, 'tdigest'), " + - // 2-3: fold into one aggregation - " PERCENTILE(int, 50, 'hdr'), " + - " PERCENTILE(int, 60, 'hdr', 3), " + + // 2-3: fold into one aggregation + " PERCENTILE(int, 50, 'hdr'), " + " PERCENTILE(int, 60, 'hdr', 3), " + - // 4: folds into a separate aggregation - " PERCENTILE(int, 60, 'hdr', 4)" + - "FROM test").replaceAll("PERCENTILE", fnName); + // 4: folds into a separate aggregation + " PERCENTILE(int, 60, 'hdr', 4)" + "FROM test").replaceAll("PERCENTILE", fnName); List aggs = percentilesAggsByField(optimizeAndPlan(sql), fieldCount); // 0-1 assertEquals(aggs.get(0), aggs.get(1)); assertEquals(new PercentilesConfig.TDigest(), aggs.get(0).percentilesConfig()); - assertArrayEquals(new double[]{50, 60}, pctOrValFn.apply(aggs.get(0)), 0); + assertArrayEquals(new double[] { 50, 60 }, pctOrValFn.apply(aggs.get(0)), 0); // 2-3 assertEquals(aggs.get(2), aggs.get(3)); assertEquals(new PercentilesConfig.Hdr(), aggs.get(2).percentilesConfig()); - assertArrayEquals(new double[]{50, 60}, pctOrValFn.apply(aggs.get(2)), 0); + assertArrayEquals(new double[] { 50, 60 }, pctOrValFn.apply(aggs.get(2)), 0); // 4 assertEquals(new PercentilesConfig.Hdr(4), aggs.get(4).percentilesConfig()); - assertArrayEquals(new double[]{60}, pctOrValFn.apply(aggs.get(4)), 0); + assertArrayEquals(new double[] { 60 }, pctOrValFn.apply(aggs.get(4)), 0); }; - test.accept("PERCENTILE", p -> ((PercentilesAggregationBuilder)p).percentiles()); - test.accept("PERCENTILE_RANK", p -> ((PercentileRanksAggregationBuilder)p).values()); + test.accept("PERCENTILE", p -> ((PercentilesAggregationBuilder) p).percentiles()); + test.accept("PERCENTILE_RANK", p -> ((PercentileRanksAggregationBuilder) p).values()); } // Tests the workaround for the SUM(all zeros) = NULL issue raised in https://github.com/elastic/elasticsearch/issues/45251 and @@ -1200,7 +1270,8 @@ public void testReplaceSumWithStats() { List testCases = asList( "SELECT keyword, SUM(int) FROM test GROUP BY keyword", "SELECT SUM(int) FROM test", - "SELECT * FROM (SELECT some.string, keyword, int FROM test) PIVOT (SUM(int) FOR keyword IN ('a', 'b'))"); + "SELECT * FROM (SELECT some.string, keyword, int FROM test) PIVOT (SUM(int) FOR keyword IN ('a', 'b'))" + ); for (String testCase : testCases) { PhysicalPlan physicalPlan = optimizeAndPlan(testCase); assertEquals(EsQueryExec.class, physicalPlan.getClass()); @@ -1209,7 +1280,7 @@ public void testReplaceSumWithStats() { } } - @SuppressWarnings({"rawtypes"}) + @SuppressWarnings({ "rawtypes" }) private static List percentilesAggsByField(PhysicalPlan p, int fieldCount) { assertEquals(EsQueryExec.class, p.getClass()); EsQueryExec ee = (EsQueryExec) p; @@ -1218,15 +1289,15 @@ private static List percentilesAggsByFiel assertEquals(ReferenceAttribute.class, ee.output().get(0).getClass()); assertEquals(fieldCount, ee.queryContainer().fields().size()); assertThat(fieldCount, greaterThanOrEqualTo(ee.queryContainer().aggs().asAggBuilder().getSubAggregations().size())); - Map aggsByName = - aggregationBuilder.getSubAggregations().stream().collect(Collectors.toMap(AggregationBuilder::getName, ab -> ab)); + Map aggsByName = aggregationBuilder.getSubAggregations() + .stream() + .collect(Collectors.toMap(AggregationBuilder::getName, ab -> ab)); return IntStream.range(0, fieldCount).mapToObj(i -> { String percentileAggName = ((MetricAggRef) ee.queryContainer().fields().get(i).v1()).name(); return (AbstractPercentilesAggregationBuilder) aggsByName.get(percentileAggName); }).collect(Collectors.toList()); } - // Boolean Conditions ///////////////////// public void testAddMissingEqualsToBoolField() { @@ -1248,10 +1319,13 @@ public void testAddMissingEqualsToBoolField() { } public void testAddMissingEqualsToNestedBoolField() { - LogicalPlan p = plan("SELECT bool FROM test " + - "WHERE int > 1 and (bool or int < 2) or (int = 3 and bool) or (int = 4 and bool = false) or bool"); - LogicalPlan expectedPlan = plan("SELECT bool FROM test " + - "WHERE int > 1 and (bool = true or int < 2) or (int = 3 and bool = true) or (int = 4 and bool = false) or bool = true"); + LogicalPlan p = plan( + "SELECT bool FROM test " + "WHERE int > 1 and (bool or int < 2) or (int = 3 and bool) or (int = 4 and bool = false) or bool" + ); + LogicalPlan expectedPlan = plan( + "SELECT bool FROM test " + + "WHERE int > 1 and (bool = true or int < 2) or (int = 3 and bool = true) or (int = 4 and bool = false) or bool = true" + ); assertTrue(p instanceof Project); p = ((Project) p).child(); @@ -1262,15 +1336,18 @@ public void testAddMissingEqualsToNestedBoolField() { List expectedFields = expectedCondition.collect(x -> x instanceof FieldAttribute); Set expectedBools = expectedFields.stream() - .filter(x -> ((FieldAttribute) x).name().equals("bool")).collect(Collectors.toSet()); + .filter(x -> ((FieldAttribute) x).name().equals("bool")) + .collect(Collectors.toSet()); assertEquals(1, expectedBools.size()); Set expectedInts = expectedFields.stream() - .filter(x -> ((FieldAttribute) x).name().equals("int")).collect(Collectors.toSet()); + .filter(x -> ((FieldAttribute) x).name().equals("int")) + .collect(Collectors.toSet()); assertEquals(1, expectedInts.size()); - condition = condition - .transformDown(FieldAttribute.class, x -> x.name().equals("bool") ? (FieldAttribute) expectedBools.toArray()[0] : x) - .transformDown(FieldAttribute.class, x -> x.name().equals("int") ? (FieldAttribute) expectedInts.toArray()[0] : x); + condition = condition.transformDown( + FieldAttribute.class, + x -> x.name().equals("bool") ? (FieldAttribute) expectedBools.toArray()[0] : x + ).transformDown(FieldAttribute.class, x -> x.name().equals("int") ? (FieldAttribute) expectedInts.toArray()[0] : x); assertEquals(expectedCondition, condition); } @@ -1279,11 +1356,8 @@ public void testAddMissingEqualsToNestedBoolField() { ///////////////////// public void testMultiLevelSubqueryWithoutRelation1() { PhysicalPlan p = optimizeAndPlan( - "SELECT int FROM (" + - " SELECT int FROM (" + - " SELECT 1 AS int" + - " ) AS subq1" + - ") AS subq2"); + "SELECT int FROM (" + " SELECT int FROM (" + " SELECT 1 AS int" + " ) AS subq1" + ") AS subq2" + ); assertThat(p, instanceOf(LocalExec.class)); LocalExec le = (LocalExec) p; assertThat(le.executable(), instanceOf(SingletonExecutable.class)); @@ -1293,17 +1367,18 @@ public void testMultiLevelSubqueryWithoutRelation1() { public void testMultiLevelSubqueryWithoutRelation2() { PhysicalPlan p = optimizeAndPlan( - "SELECT i, string FROM (" + - " SELECT * FROM (" + - " SELECT int as i, str AS string FROM (" + - " SELECT * FROM (" + - " SELECT int, s AS str FROM (" + - " SELECT 1 AS int, 'foo' AS s" + - " ) AS subq1" + - " )" + - " ) AS subq2" + - " ) AS subq3" + - ")"); + "SELECT i, string FROM (" + + " SELECT * FROM (" + + " SELECT int as i, str AS string FROM (" + + " SELECT * FROM (" + + " SELECT int, s AS str FROM (" + + " SELECT 1 AS int, 'foo' AS s" + + " ) AS subq1" + + " )" + + " ) AS subq2" + + " ) AS subq3" + + ")" + ); assertThat(p, instanceOf(LocalExec.class)); LocalExec le = (LocalExec) p; assertThat(le.executable(), instanceOf(SingletonExecutable.class)); diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/planner/VerifierTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/planner/VerifierTests.java index 8c04f57fb4fac..fdb87f9ab1c92 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/planner/VerifierTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/planner/VerifierTests.java @@ -27,12 +27,7 @@ public class VerifierTests extends ESTestCase { private final IndexResolution indexResolution = IndexResolution.valid( new EsIndex("test", loadMapping("mapping-multi-field-with-nested.json")) ); - private final Analyzer analyzer = new Analyzer( - TEST_CFG, - new SqlFunctionRegistry(), - indexResolution, - new Verifier(new Metrics()) - ); + private final Analyzer analyzer = new Analyzer(TEST_CFG, new SqlFunctionRegistry(), indexResolution, new Verifier(new Metrics())); private final Planner planner = new Planner(); private PhysicalPlan verify(String sql) { @@ -40,10 +35,7 @@ private PhysicalPlan verify(String sql) { } private String error(String sql) { - PlanningException e = expectThrows( - PlanningException.class, - () -> verify(sql) - ); + PlanningException e = expectThrows(PlanningException.class, () -> verify(sql)); String message = e.getMessage(); assertTrue(message.startsWith("Found ")); String pattern = "\nline "; @@ -59,27 +51,23 @@ private String innerLimitMsg(int line, int column) { } public void testSubselectWithOrderByOnTopOfOrderByAndLimit() { + assertEquals(innerLimitMsg(1, 50), error("SELECT * FROM (SELECT * FROM test ORDER BY 1 ASC LIMIT 10) ORDER BY 2")); + assertEquals(innerLimitMsg(1, 50), error("SELECT * FROM (SELECT * FROM (SELECT * FROM test LIMIT 10) ORDER BY 1) ORDER BY 2")); assertEquals( - innerLimitMsg(1, 50), - error("SELECT * FROM (SELECT * FROM test ORDER BY 1 ASC LIMIT 10) ORDER BY 2") - ); - assertEquals( - innerLimitMsg(1, 50), - error("SELECT * FROM (SELECT * FROM (SELECT * FROM test LIMIT 10) ORDER BY 1) ORDER BY 2") - ); - assertEquals( - innerLimitMsg(1, 66), - error("SELECT * FROM (SELECT * FROM (SELECT * FROM test ORDER BY 1 ASC) LIMIT 5) ORDER BY 1 DESC") + innerLimitMsg(1, 66), + error("SELECT * FROM (SELECT * FROM (SELECT * FROM test ORDER BY 1 ASC) LIMIT 5) ORDER BY 1 DESC") ); assertEquals( - innerLimitMsg(1, 142), - error("SELECT * FROM (" + - "SELECT * FROM (" + - "SELECT * FROM (" + - "SELECT * FROM test ORDER BY int DESC" + - ") ORDER BY int ASC NULLS LAST) " + - "ORDER BY int DESC NULLS LAST LIMIT 12) " + - "ORDER BY int DESC NULLS FIRST") + innerLimitMsg(1, 142), + error( + "SELECT * FROM (" + + "SELECT * FROM (" + + "SELECT * FROM (" + + "SELECT * FROM test ORDER BY int DESC" + + ") ORDER BY int ASC NULLS LAST) " + + "ORDER BY int DESC NULLS LAST LIMIT 12) " + + "ORDER BY int DESC NULLS FIRST" + ) ); assertEquals( innerLimitMsg(1, 50), @@ -90,9 +78,7 @@ public void testSubselectWithOrderByOnTopOfOrderByAndLimit() { public void testSubselectWithOrderByOnTopOfGroupByOrderByAndLimit() { assertEquals( innerLimitMsg(1, 86), - error( - "SELECT * FROM (SELECT max(int) AS max, bool FROM test GROUP BY bool ORDER BY max ASC LIMIT 10) ORDER BY max DESC" - ) + error("SELECT * FROM (SELECT max(int) AS max, bool FROM test GROUP BY bool ORDER BY max ASC LIMIT 10) ORDER BY max DESC") ); assertEquals( innerLimitMsg(1, 102), @@ -105,38 +91,36 @@ public void testSubselectWithOrderByOnTopOfGroupByOrderByAndLimit() { ) ); assertEquals( - innerLimitMsg(1, 176), - error("SELECT * FROM (" + - "SELECT * FROM (" + - "SELECT * FROM (" + - "SELECT max(int) AS max, bool FROM test GROUP BY bool ORDER BY max DESC" + - ") ORDER BY max ASC NULLS LAST) " + - "ORDER BY max DESC NULLS LAST LIMIT 12) " + - "ORDER BY max DESC NULLS FIRST") + innerLimitMsg(1, 176), + error( + "SELECT * FROM (" + + "SELECT * FROM (" + + "SELECT * FROM (" + + "SELECT max(int) AS max, bool FROM test GROUP BY bool ORDER BY max DESC" + + ") ORDER BY max ASC NULLS LAST) " + + "ORDER BY max DESC NULLS LAST LIMIT 12) " + + "ORDER BY max DESC NULLS FIRST" + ) ); } public void testInnerLimitWithWhere() { - assertEquals(innerLimitMsg(1, 35), - error("SELECT * FROM (SELECT * FROM test LIMIT 10) WHERE int = 1")); - assertEquals(innerLimitMsg(1, 50), - error("SELECT * FROM (SELECT * FROM (SELECT * FROM test LIMIT 10)) WHERE int = 1")); - assertEquals(innerLimitMsg(1, 51), - error("SELECT * FROM (SELECT * FROM (SELECT * FROM test) LIMIT 10) WHERE int = 1")); + assertEquals(innerLimitMsg(1, 35), error("SELECT * FROM (SELECT * FROM test LIMIT 10) WHERE int = 1")); + assertEquals(innerLimitMsg(1, 50), error("SELECT * FROM (SELECT * FROM (SELECT * FROM test LIMIT 10)) WHERE int = 1")); + assertEquals(innerLimitMsg(1, 51), error("SELECT * FROM (SELECT * FROM (SELECT * FROM test) LIMIT 10) WHERE int = 1")); } public void testInnerLimitWithGroupBy() { - assertEquals(innerLimitMsg(1, 37), - error("SELECT int FROM (SELECT * FROM test LIMIT 10) GROUP BY int")); - assertEquals(innerLimitMsg(1, 52), - error("SELECT int FROM (SELECT * FROM (SELECT * FROM test LIMIT 10)) GROUP BY int")); - assertEquals(innerLimitMsg(1, 53), - error("SELECT int FROM (SELECT * FROM (SELECT * FROM test) LIMIT 10) GROUP BY int")); + assertEquals(innerLimitMsg(1, 37), error("SELECT int FROM (SELECT * FROM test LIMIT 10) GROUP BY int")); + assertEquals(innerLimitMsg(1, 52), error("SELECT int FROM (SELECT * FROM (SELECT * FROM test LIMIT 10)) GROUP BY int")); + assertEquals(innerLimitMsg(1, 53), error("SELECT int FROM (SELECT * FROM (SELECT * FROM test) LIMIT 10) GROUP BY int")); } public void testInnerLimitWithPivot() { - assertEquals(innerLimitMsg(1, 52), - error("SELECT * FROM (SELECT int, bool, keyword FROM test LIMIT 10) PIVOT (AVG(int) FOR bool IN (true, false))")); + assertEquals( + innerLimitMsg(1, 52), + error("SELECT * FROM (SELECT int, bool, keyword FROM test LIMIT 10) PIVOT (AVG(int) FOR bool IN (true, false))") + ); } public void testTopWithOrderBySucceeds() { @@ -145,11 +129,11 @@ public void testTopWithOrderBySucceeds() { } public void testInnerTop() { - assertEquals(innerLimitMsg(1, 23), - error("SELECT * FROM (SELECT TOP 10 * FROM test) WHERE int = 1")); - assertEquals(innerLimitMsg(1, 23), - error("SELECT * FROM (SELECT TOP 10 * FROM test) ORDER BY int")); - assertEquals(innerLimitMsg(1, 23), - error("SELECT * FROM (SELECT TOP 10 int, bool, keyword FROM test) PIVOT (AVG(int) FOR bool IN (true, false))")); + assertEquals(innerLimitMsg(1, 23), error("SELECT * FROM (SELECT TOP 10 * FROM test) WHERE int = 1")); + assertEquals(innerLimitMsg(1, 23), error("SELECT * FROM (SELECT TOP 10 * FROM test) ORDER BY int")); + assertEquals( + innerLimitMsg(1, 23), + error("SELECT * FROM (SELECT TOP 10 int, bool, keyword FROM test) PIVOT (AVG(int) FOR bool IN (true, false))") + ); } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plugin/CursorTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plugin/CursorTests.java index 6d89e05efa445..712e6e803f6e2 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plugin/CursorTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plugin/CursorTests.java @@ -79,27 +79,27 @@ private static SqlQueryResponse createRandomSqlResponse() { @SuppressWarnings("unchecked") static Cursor randomNonEmptyCursor() { - Supplier cursorSupplier = randomFrom( - () -> ScrollCursorTests.randomScrollCursor(), - () -> { - SqlQueryResponse response = createRandomSqlResponse(); - if (response.columns() != null && response.rows() != null) { - return new TextFormatterCursor(ScrollCursorTests.randomScrollCursor(), - new BasicFormatter(response.columns(), response.rows(), BasicFormatter.FormatOption.CLI)); - } else { - return ScrollCursorTests.randomScrollCursor(); - } - }, - () -> { - SqlQueryResponse response = createRandomSqlResponse(); - if (response.columns() != null && response.rows() != null) { - return new TextFormatterCursor(ScrollCursorTests.randomScrollCursor(), - new BasicFormatter(response.columns(), response.rows(), BasicFormatter.FormatOption.TEXT)); - } else { - return ScrollCursorTests.randomScrollCursor(); - } - } - ); + Supplier cursorSupplier = randomFrom(() -> ScrollCursorTests.randomScrollCursor(), () -> { + SqlQueryResponse response = createRandomSqlResponse(); + if (response.columns() != null && response.rows() != null) { + return new TextFormatterCursor( + ScrollCursorTests.randomScrollCursor(), + new BasicFormatter(response.columns(), response.rows(), BasicFormatter.FormatOption.CLI) + ); + } else { + return ScrollCursorTests.randomScrollCursor(); + } + }, () -> { + SqlQueryResponse response = createRandomSqlResponse(); + if (response.columns() != null && response.rows() != null) { + return new TextFormatterCursor( + ScrollCursorTests.randomScrollCursor(), + new BasicFormatter(response.columns(), response.rows(), BasicFormatter.FormatOption.TEXT) + ); + } else { + return ScrollCursorTests.randomScrollCursor(); + } + }); return cursorSupplier.get(); } @@ -110,11 +110,15 @@ public void testVersionHandling() { Version nextMinorVersion = Version.fromId(Version.CURRENT.id + 10000); String encodedWithWrongVersion = CursorsTestUtil.encodeToString(cursor, nextMinorVersion, randomZone()); - SqlIllegalArgumentException exception = expectThrows(SqlIllegalArgumentException.class, - () -> decodeFromString(encodedWithWrongVersion)); + SqlIllegalArgumentException exception = expectThrows( + SqlIllegalArgumentException.class, + () -> decodeFromString(encodedWithWrongVersion) + ); - assertEquals(LoggerMessageFormat.format("Unsupported cursor version [{}], expected [{}]", nextMinorVersion, Version.CURRENT), - exception.getMessage()); + assertEquals( + LoggerMessageFormat.format("Unsupported cursor version [{}], expected [{}]", nextMinorVersion, Version.CURRENT), + exception.getMessage() + ); } public static Cursor decodeFromString(String base64) { diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plugin/SqlMediaTypeParserTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plugin/SqlMediaTypeParserTests.java index 16fc47b37e830..43bbe3b46328d 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plugin/SqlMediaTypeParserTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plugin/SqlMediaTypeParserTests.java @@ -8,11 +8,11 @@ package org.elasticsearch.xpack.sql.plugin; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.MediaType; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.rest.FakeRestRequest; +import org.elasticsearch.xcontent.MediaType; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.sql.action.SqlQueryRequest; import org.elasticsearch.xpack.sql.proto.Mode; import org.elasticsearch.xpack.sql.proto.RequestInfo; @@ -43,62 +43,93 @@ public void testCsvDetection() { } public void testTsvDetection() { - MediaType text = getResponseMediaType(reqWithAccept("text/tab-separated-values"), - createTestInstance(false, Mode.PLAIN, false)); + MediaType text = getResponseMediaType(reqWithAccept("text/tab-separated-values"), createTestInstance(false, Mode.PLAIN, false)); assertThat(text, is(TSV)); } public void testMediaTypeDetectionWithParameters() { - assertThat(getResponseMediaType(reqWithAccept("text/plain; charset=utf-8"), - createTestInstance(false, Mode.PLAIN, false)), is(PLAIN_TEXT)); - assertThat(getResponseMediaType(reqWithAccept("text/plain; header=present"), - createTestInstance(false, Mode.PLAIN, false)), is(PLAIN_TEXT)); - assertThat(getResponseMediaType(reqWithAccept("text/plain; charset=utf-8; header=present"), - createTestInstance(false, Mode.PLAIN, false)), is(PLAIN_TEXT)); - - assertThat(getResponseMediaType(reqWithAccept("text/csv; charset=utf-8"), - createTestInstance(false, Mode.PLAIN, false)), is(CSV)); - assertThat(getResponseMediaType(reqWithAccept("text/csv; header=present"), - createTestInstance(false, Mode.PLAIN, false)), is(CSV)); - assertThat(getResponseMediaType(reqWithAccept("text/csv; charset=utf-8; header=present"), - createTestInstance(false, Mode.PLAIN, false)), is(CSV)); - - assertThat(getResponseMediaType(reqWithAccept("text/tab-separated-values; charset=utf-8"), - createTestInstance(false, Mode.PLAIN, false)), is(TSV)); - assertThat(getResponseMediaType(reqWithAccept("text/tab-separated-values; header=present"), - createTestInstance(false, Mode.PLAIN, false)), is(TSV)); - assertThat(getResponseMediaType(reqWithAccept("text/tab-separated-values; charset=utf-8; header=present"), - createTestInstance(false, Mode.PLAIN, false)), is(TSV)); + assertThat( + getResponseMediaType(reqWithAccept("text/plain; charset=utf-8"), createTestInstance(false, Mode.PLAIN, false)), + is(PLAIN_TEXT) + ); + assertThat( + getResponseMediaType(reqWithAccept("text/plain; header=present"), createTestInstance(false, Mode.PLAIN, false)), + is(PLAIN_TEXT) + ); + assertThat( + getResponseMediaType(reqWithAccept("text/plain; charset=utf-8; header=present"), createTestInstance(false, Mode.PLAIN, false)), + is(PLAIN_TEXT) + ); + + assertThat(getResponseMediaType(reqWithAccept("text/csv; charset=utf-8"), createTestInstance(false, Mode.PLAIN, false)), is(CSV)); + assertThat(getResponseMediaType(reqWithAccept("text/csv; header=present"), createTestInstance(false, Mode.PLAIN, false)), is(CSV)); + assertThat( + getResponseMediaType(reqWithAccept("text/csv; charset=utf-8; header=present"), createTestInstance(false, Mode.PLAIN, false)), + is(CSV) + ); + + assertThat( + getResponseMediaType(reqWithAccept("text/tab-separated-values; charset=utf-8"), createTestInstance(false, Mode.PLAIN, false)), + is(TSV) + ); + assertThat( + getResponseMediaType(reqWithAccept("text/tab-separated-values; header=present"), createTestInstance(false, Mode.PLAIN, false)), + is(TSV) + ); + assertThat( + getResponseMediaType( + reqWithAccept("text/tab-separated-values; charset=utf-8; header=present"), + createTestInstance(false, Mode.PLAIN, false) + ), + is(TSV) + ); } public void testInvalidFormat() { - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> getResponseMediaType(reqWithAccept("text/garbage"), createTestInstance(false, Mode.PLAIN, false))); - assertEquals(e.getMessage(), - "Invalid request content type: Accept=[text/garbage], Content-Type=[application/json], format=[null]"); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> getResponseMediaType(reqWithAccept("text/garbage"), createTestInstance(false, Mode.PLAIN, false)) + ); + assertEquals(e.getMessage(), "Invalid request content type: Accept=[text/garbage], Content-Type=[application/json], format=[null]"); } public void testNoFormat() { - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> getResponseMediaType(new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).build(), - createTestInstance(false, Mode.PLAIN, false))); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> getResponseMediaType( + new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).build(), + createTestInstance(false, Mode.PLAIN, false) + ) + ); assertEquals(e.getMessage(), "Invalid request content type: Accept=[null], Content-Type=[null], format=[null]"); } private static RestRequest reqWithAccept(String acceptHeader) { - return new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY) - .withHeaders(Map.of("Content-Type", Collections.singletonList("application/json"), - "Accept", Collections.singletonList(acceptHeader))) - .build(); + return new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withHeaders( + Map.of("Content-Type", Collections.singletonList("application/json"), "Accept", Collections.singletonList(acceptHeader)) + ).build(); } protected SqlQueryRequest createTestInstance(boolean binaryCommunication, Mode mode, boolean columnar) { - return new SqlQueryRequest(randomAlphaOfLength(10), Collections.emptyList(), null, null, - randomZone(), randomAlphaOfLength(9), between(1, Integer.MAX_VALUE), TimeValue.parseTimeValue(randomTimeValue(), null, "test"), - TimeValue.parseTimeValue(randomTimeValue(), null, "test"), columnar, randomAlphaOfLength(10), + return new SqlQueryRequest( + randomAlphaOfLength(10), + Collections.emptyList(), + null, + null, + randomZone(), + randomAlphaOfLength(9), + between(1, Integer.MAX_VALUE), + TimeValue.parseTimeValue(randomTimeValue(), null, "test"), + TimeValue.parseTimeValue(randomTimeValue(), null, "test"), + columnar, + randomAlphaOfLength(10), new RequestInfo(mode, randomFrom(randomFrom(CLIENT_IDS), randomAlphaOfLengthBetween(10, 20))), - randomBoolean(), randomBoolean(), TimeValue.parseTimeValue(randomTimeValue(), null, "test"), - randomBoolean(), TimeValue.parseTimeValue(randomTimeValue(), null, "test")).binaryCommunication(binaryCommunication); + randomBoolean(), + randomBoolean(), + TimeValue.parseTimeValue(randomTimeValue(), null, "test"), + randomBoolean(), + TimeValue.parseTimeValue(randomTimeValue(), null, "test") + ).binaryCommunication(binaryCommunication); } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plugin/SqlPluginTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plugin/SqlPluginTests.java index 894132d1aee60..98f42103c07de 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plugin/SqlPluginTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plugin/SqlPluginTests.java @@ -33,17 +33,30 @@ public void testSqlDisabledIsNoOp() { SqlPlugin plugin = new SqlPlugin(settings); ClusterService clusterService = mock(ClusterService.class); when(clusterService.getClusterName()).thenReturn(new ClusterName(randomAlphaOfLength(10))); - when(clusterService.getClusterSettings()).thenReturn(new ClusterSettings(Settings.EMPTY, - ClusterSettings.BUILT_IN_CLUSTER_SETTINGS)); - assertThat(plugin.createComponents(mock(Client.class), Settings.EMPTY, clusterService, - new NamedWriteableRegistry(Cursors.getNamedWriteables())), - hasSize(3)); + when(clusterService.getClusterSettings()).thenReturn( + new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS) + ); + assertThat( + plugin.createComponents( + mock(Client.class), + Settings.EMPTY, + clusterService, + new NamedWriteableRegistry(Cursors.getNamedWriteables()) + ), + hasSize(3) + ); assertThat(plugin.getActions(), hasSize(8)); assertThat( - plugin.getRestHandlers(Settings.EMPTY, mock(RestController.class), + plugin.getRestHandlers( + Settings.EMPTY, + mock(RestController.class), new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), - IndexScopedSettings.DEFAULT_SCOPED_SETTINGS, new SettingsFilter(Collections.emptyList()), - mock(IndexNameExpressionResolver.class), () -> mock(DiscoveryNodes.class)), - hasSize(7)); + IndexScopedSettings.DEFAULT_SCOPED_SETTINGS, + new SettingsFilter(Collections.emptyList()), + mock(IndexNameExpressionResolver.class), + () -> mock(DiscoveryNodes.class) + ), + hasSize(7) + ); } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plugin/TextFormatTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plugin/TextFormatTests.java index 6ab6474570b71..895a5b758ffad 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plugin/TextFormatTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plugin/TextFormatTests.java @@ -6,20 +6,20 @@ */ package org.elasticsearch.xpack.sql.plugin; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Set; -import java.util.stream.Collectors; - -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.rest.FakeRestRequest; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.sql.action.SqlQueryResponse; import org.elasticsearch.xpack.sql.proto.ColumnInfo; import org.elasticsearch.xpack.sql.proto.Mode; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + import static java.util.Arrays.asList; import static java.util.Collections.emptyList; import static java.util.Collections.singletonList; @@ -71,17 +71,12 @@ public void testTsvFormatWithEmptyData() { public void testCsvFormatWithRegularData() { String text = CSV.format(req(), regularData()); - assertEquals("string,number\r\n" + - "Along The River Bank,708\r\n" + - "Mind Train,280\r\n", - text); + assertEquals("string,number\r\n" + "Along The River Bank,708\r\n" + "Mind Train,280\r\n", text); } public void testCsvFormatNoHeaderWithRegularData() { String text = CSV.format(reqWithParam("header", "absent"), regularData()); - assertEquals("Along The River Bank,708\r\n" + - "Mind Train,280\r\n", - text); + assertEquals("Along The River Bank,708\r\n" + "Mind Train,280\r\n", text); } public void testCsvFormatWithCustomDelimiterRegularData() { @@ -104,42 +99,32 @@ public void testCsvFormatWithCustomDelimiterRegularData() { public void testTsvFormatWithRegularData() { String text = TSV.format(req(), regularData()); - assertEquals("string\tnumber\n" + - "Along The River Bank\t708\n" + - "Mind Train\t280\n", - text); + assertEquals("string\tnumber\n" + "Along The River Bank\t708\n" + "Mind Train\t280\n", text); } public void testCsvFormatWithEscapedData() { String text = CSV.format(req(), escapedData()); - assertEquals("first,\"\"\"special\"\"\"\r\n" + - "normal,\"\"\"quo\"\"ted\"\",\n\"\r\n" + - "commas,\"a,b,c,\n,d,e,\t\n\"\r\n" - , text); + assertEquals("first,\"\"\"special\"\"\"\r\n" + "normal,\"\"\"quo\"\"ted\"\",\n\"\r\n" + "commas,\"a,b,c,\n,d,e,\t\n\"\r\n", text); } public void testCsvFormatWithCustomDelimiterEscapedData() { String text = CSV.format(reqWithParam("delimiter", "\\"), escapedData()); - assertEquals("first\\\"\"\"special\"\"\"\r\n" + - "normal\\\"\"\"quo\"\"ted\"\",\n\"\r\n" + - "commas\\\"a,b,c,\n,d,e,\t\n\"\r\n" - , text); + assertEquals( + "first\\\"\"\"special\"\"\"\r\n" + "normal\\\"\"\"quo\"\"ted\"\",\n\"\r\n" + "commas\\\"a,b,c,\n,d,e,\t\n\"\r\n", + text + ); } public void testTsvFormatWithEscapedData() { String text = TSV.format(req(), escapedData()); - assertEquals("first\t\"special\"\n" + - "normal\t\"quo\"ted\",\\n\n" + - "commas\ta,b,c,\\n,d,e,\\t\\n\n" - , text); + assertEquals("first\t\"special\"\n" + "normal\t\"quo\"ted\",\\n\n" + "commas\ta,b,c,\\n,d,e,\\t\\n\n", text); } public void testInvalidCsvDelims() { List invalid = Arrays.asList("\"", "\r", "\n", "\t", "", "ab"); - for (String c: invalid) { - Exception e = expectThrows(IllegalArgumentException.class, - () -> CSV.format(reqWithParam("delimiter", c), emptyData())); + for (String c : invalid) { + Exception e = expectThrows(IllegalArgumentException.class, () -> CSV.format(reqWithParam("delimiter", c), emptyData())); String msg; if (c.length() == 1) { msg = c.equals("\t") @@ -152,7 +137,6 @@ public void testInvalidCsvDelims() { } } - private static SqlQueryResponse emptyData() { return new SqlQueryResponse( null, diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/querydsl/container/QueryContainerTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/querydsl/container/QueryContainerTests.java index 3df852a91d6ea..6d15fa2784643 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/querydsl/container/QueryContainerTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/querydsl/container/QueryContainerTests.java @@ -37,40 +37,48 @@ public class QueryContainerTests extends ESTestCase { private boolean hasDocValues = randomBoolean(); public void testRewriteToContainNestedFieldNoQuery() { - Query expected = new NestedQuery(source, path, singletonMap(name, new SimpleImmutableEntry<>(hasDocValues, format)), - new MatchAll(source)); + Query expected = new NestedQuery( + source, + path, + singletonMap(name, new SimpleImmutableEntry<>(hasDocValues, format)), + new MatchAll(source) + ); assertEquals(expected, QueryContainer.rewriteToContainNestedField(null, source, path, name, format, hasDocValues)); } public void testRewriteToContainsNestedFieldWhenContainsNestedField() { ZoneId zoneId = randomZone(); - Query original = new BoolQuery(source, true, - new NestedQuery(source, path, singletonMap(name, new SimpleImmutableEntry<>(hasDocValues, format)), - new MatchAll(source)), - new RangeQuery(source, randomAlphaOfLength(5), 0, randomBoolean(), 100, randomBoolean(), zoneId)); + Query original = new BoolQuery( + source, + true, + new NestedQuery(source, path, singletonMap(name, new SimpleImmutableEntry<>(hasDocValues, format)), new MatchAll(source)), + new RangeQuery(source, randomAlphaOfLength(5), 0, randomBoolean(), 100, randomBoolean(), zoneId) + ); assertSame(original, QueryContainer.rewriteToContainNestedField(original, source, path, name, format, randomBoolean())); } public void testRewriteToContainsNestedFieldWhenCanAddNestedField() { ZoneId zoneId = randomZone(); Query buddy = new RangeQuery(source, randomAlphaOfLength(5), 0, randomBoolean(), 100, randomBoolean(), zoneId); - Query original = new BoolQuery(source, true, - new NestedQuery(source, path, emptyMap(), new MatchAll(source)), - buddy); - Query expected = new BoolQuery(source, true, - new NestedQuery(source, path, singletonMap(name, new SimpleImmutableEntry<>(hasDocValues, format)), - new MatchAll(source)), - buddy); + Query original = new BoolQuery(source, true, new NestedQuery(source, path, emptyMap(), new MatchAll(source)), buddy); + Query expected = new BoolQuery( + source, + true, + new NestedQuery(source, path, singletonMap(name, new SimpleImmutableEntry<>(hasDocValues, format)), new MatchAll(source)), + buddy + ); assertEquals(expected, QueryContainer.rewriteToContainNestedField(original, source, path, name, format, hasDocValues)); } public void testRewriteToContainsNestedFieldWhenDoesNotContainNestedFieldAndCantAdd() { ZoneId zoneId = randomZone(); Query original = new RangeQuery(source, randomAlphaOfLength(5), 0, randomBoolean(), 100, randomBoolean(), zoneId); - Query expected = new BoolQuery(source, true, + Query expected = new BoolQuery( + source, + true, original, - new NestedQuery(source, path, singletonMap(name, new SimpleImmutableEntry<>(hasDocValues, format)), - new MatchAll(source))); + new NestedQuery(source, path, singletonMap(name, new SimpleImmutableEntry<>(hasDocValues, format)), new MatchAll(source)) + ); assertEquals(expected, QueryContainer.rewriteToContainNestedField(original, source, path, name, format, hasDocValues)); } @@ -84,8 +92,7 @@ public void testColumnMaskShouldDuplicateSameAttributes() { Attribute fourth = new FieldAttribute(Source.EMPTY, "fourth", esField); Alias firstAliased = new Alias(Source.EMPTY, "firstAliased", first); - QueryContainer queryContainer = new QueryContainer() - .withAliases(new AttributeMap<>(firstAliased.toAttribute(), first)) + QueryContainer queryContainer = new QueryContainer().withAliases(new AttributeMap<>(firstAliased.toAttribute(), first)) .addColumn(third) .addColumn(first) .addColumn(fourth) @@ -94,13 +101,7 @@ public void testColumnMaskShouldDuplicateSameAttributes() { .addColumn(first) .addColumn(fourth); - BitSet result = queryContainer.columnMask(Arrays.asList( - first, - first, - second, - third, - firstAliased.toAttribute() - )); + BitSet result = queryContainer.columnMask(Arrays.asList(first, first, second, third, firstAliased.toAttribute())); BitSet expected = new BitSet(); expected.set(0, true); @@ -111,7 +112,6 @@ public void testColumnMaskShouldDuplicateSameAttributes() { expected.set(5, true); expected.set(6, false); - assertEquals(expected, result); } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/session/ListCursorTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/session/ListCursorTests.java index 43e0d6476e561..b4d4c0201c182 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/session/ListCursorTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/session/ListCursorTests.java @@ -32,9 +32,7 @@ public static ListCursor randomPagingListCursor() { @Override protected ListCursor mutateInstance(ListCursor instance) throws IOException { - return new ListCursor(instance.data(), - randomValueOtherThan(instance.pageSize(), () -> between(1, 20)), - instance.columnCount()); + return new ListCursor(instance.data(), randomValueOtherThan(instance.pageSize(), () -> between(1, 20)), instance.columnCount()); } @Override diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/stats/VerifierMetricsTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/stats/VerifierMetricsTests.java index 263fe373e2e38..da6c96a84684e 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/stats/VerifierMetricsTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/stats/VerifierMetricsTests.java @@ -33,8 +33,14 @@ public class VerifierMetricsTests extends ESTestCase { private SqlParser parser = new SqlParser(); - private String[] commands = {"SHOW FUNCTIONS", "SHOW COLUMNS FROM library", "SHOW SCHEMAS", - "SHOW TABLES", "SYS COLUMNS LIKE '%name'", "SYS TABLES", "SYS TYPES"}; + private String[] commands = { + "SHOW FUNCTIONS", + "SHOW COLUMNS FROM library", + "SHOW SCHEMAS", + "SHOW TABLES", + "SYS COLUMNS LIKE '%name'", + "SYS TABLES", + "SYS TYPES" }; public void testWhereQuery() { Counters c = sql("SELECT emp_no FROM test WHERE languages > 2"); @@ -92,8 +98,17 @@ public void testOrderByQuery() { } public void testCommand() { - Counters c = sql(randomFrom("SHOW FUNCTIONS", "SHOW COLUMNS FROM library", "SHOW SCHEMAS", - "SHOW TABLES", "SYS COLUMNS LIKE '%name'", "SYS TABLES", "SYS TYPES")); + Counters c = sql( + randomFrom( + "SHOW FUNCTIONS", + "SHOW COLUMNS FROM library", + "SHOW SCHEMAS", + "SHOW TABLES", + "SYS COLUMNS LIKE '%name'", + "SYS TABLES", + "SYS TYPES" + ) + ); assertEquals(0, where(c)); assertEquals(0, limit(c)); assertEquals(0, groupby(c)); @@ -148,8 +163,9 @@ public void testWhereLimitGroupByHavingQuery() { } public void testWhereLimitGroupByHavingOrderByQuery() { - Counters c = sql("SELECT languages FROM test WHERE languages > 2 GROUP BY languages HAVING MAX(languages) > 3" - + " ORDER BY languages LIMIT 5"); + Counters c = sql( + "SELECT languages FROM test WHERE languages > 2 GROUP BY languages HAVING MAX(languages) > 3" + " ORDER BY languages LIMIT 5" + ); assertEquals(1L, where(c)); assertEquals(1L, limit(c)); assertEquals(1L, groupby(c)); @@ -163,8 +179,10 @@ public void testTwoQueriesExecuted() { Metrics metrics = new Metrics(); Verifier verifier = new Verifier(metrics); sqlWithVerifier("SELECT languages FROM test WHERE languages > 2 GROUP BY languages LIMIT 5", verifier); - sqlWithVerifier("SELECT languages FROM test WHERE languages > 2 GROUP BY languages HAVING MAX(languages) > 3 " - + "ORDER BY languages LIMIT 5", verifier); + sqlWithVerifier( + "SELECT languages FROM test WHERE languages > 2 GROUP BY languages HAVING MAX(languages) > 3 " + "ORDER BY languages LIMIT 5", + verifier + ); Counters c = metrics.stats(); assertEquals(2L, where(c)); diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/tree/SqlNodeSubclassTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/tree/SqlNodeSubclassTests.java index 568484f8cc9e3..bf647c6b91ccc 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/tree/SqlNodeSubclassTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/tree/SqlNodeSubclassTests.java @@ -56,9 +56,17 @@ */ public class SqlNodeSubclassTests> extends NodeSubclassTests { - private static final List> CLASSES_WITH_MIN_TWO_CHILDREN = asList(Percentile.class, Percentiles.class, PercentileRanks.class, - Iif.class, IfConditional.class, IfNull.class, In.class, InPipe.class, - org.elasticsearch.xpack.ql.expression.predicate.operator.comparison.In.class); + private static final List> CLASSES_WITH_MIN_TWO_CHILDREN = asList( + Percentile.class, + Percentiles.class, + PercentileRanks.class, + Iif.class, + IfConditional.class, + IfNull.class, + In.class, + InPipe.class, + org.elasticsearch.xpack.ql.expression.predicate.operator.comparison.In.class + ); public SqlNodeSubclassTests(Class subclass) { super(subclass); diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/type/SqlDataTypeConverterTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/type/SqlDataTypeConverterTests.java index f0ebaa8967f79..ae4acf2ffeb6e 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/type/SqlDataTypeConverterTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/type/SqlDataTypeConverterTests.java @@ -7,10 +7,6 @@ package org.elasticsearch.xpack.sql.type; -import java.time.OffsetTime; -import java.time.ZoneOffset; -import java.time.ZonedDateTime; - import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.ql.QlIllegalArgumentException; import org.elasticsearch.xpack.ql.expression.Literal; @@ -20,6 +16,10 @@ import org.elasticsearch.xpack.ql.type.DataType; import org.elasticsearch.xpack.sql.util.DateUtils; +import java.time.OffsetTime; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; + import static java.util.stream.Collectors.toList; import static org.elasticsearch.xpack.ql.type.DataTypes.BOOLEAN; import static org.elasticsearch.xpack.ql.type.DataTypes.BYTE; @@ -326,8 +326,7 @@ public void testConversionToDateTime() { Converter back = converterFor(KEYWORD, DATETIME); assertEquals(dt, back.convert(forward.convert(dt))); Exception e = expectThrows(QlIllegalArgumentException.class, () -> conversion.convert("0xff")); - assertEquals("cannot cast [0xff] to [datetime]: Text '0xff' could not be parsed at index 0", - e.getMessage()); + assertEquals("cannot cast [0xff] to [datetime]: Text '0xff' could not be parsed at index 0", e.getMessage()); } } @@ -730,9 +729,7 @@ public void testIpToString() { } private DataType randomInterval() { - return randomFrom(SqlDataTypes.types().stream() - .filter(SqlDataTypes::isInterval) - .collect(toList())); + return randomFrom(SqlDataTypes.types().stream().filter(SqlDataTypes::isInterval).collect(toList())); } static ZonedDateTime dateTime(long millisSinceEpoch) { diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/type/SqlDataTypesTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/type/SqlDataTypesTests.java index c670e5a1abd75..bcb03d666753d 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/type/SqlDataTypesTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/type/SqlDataTypesTests.java @@ -88,22 +88,23 @@ public void testMetaRadix() { assertEquals(Integer.valueOf(2), metaSqlRadix(FLOAT)); } - // type checks public void testIsInterval() { - for (DataType dataType : asList(INTERVAL_YEAR, - INTERVAL_MONTH, - INTERVAL_DAY, - INTERVAL_HOUR, - INTERVAL_MINUTE, - INTERVAL_SECOND, - INTERVAL_YEAR_TO_MONTH, - INTERVAL_DAY_TO_HOUR, - INTERVAL_DAY_TO_MINUTE, - INTERVAL_DAY_TO_SECOND, - INTERVAL_HOUR_TO_MINUTE, - INTERVAL_HOUR_TO_SECOND, - INTERVAL_MINUTE_TO_SECOND)) { + for (DataType dataType : asList( + INTERVAL_YEAR, + INTERVAL_MONTH, + INTERVAL_DAY, + INTERVAL_HOUR, + INTERVAL_MINUTE, + INTERVAL_SECOND, + INTERVAL_YEAR_TO_MONTH, + INTERVAL_DAY_TO_HOUR, + INTERVAL_DAY_TO_MINUTE, + INTERVAL_DAY_TO_SECOND, + INTERVAL_HOUR_TO_MINUTE, + INTERVAL_HOUR_TO_SECOND, + INTERVAL_MINUTE_TO_SECOND + )) { assertTrue(dataType + " is not an interval", isInterval(dataType)); } } @@ -141,7 +142,8 @@ public void testIncompatibleInterval() { } public void testIntervalCompabitilityWithDateTimes() { - for (DataType intervalType : asList(INTERVAL_YEAR, + for (DataType intervalType : asList( + INTERVAL_YEAR, INTERVAL_MONTH, INTERVAL_DAY, INTERVAL_HOUR, @@ -153,8 +155,9 @@ public void testIntervalCompabitilityWithDateTimes() { INTERVAL_DAY_TO_SECOND, INTERVAL_HOUR_TO_MINUTE, INTERVAL_HOUR_TO_SECOND, - INTERVAL_MINUTE_TO_SECOND)) { - for (DataType dateTimeType: asList(DATE, DATETIME)) { + INTERVAL_MINUTE_TO_SECOND + )) { + for (DataType dateTimeType : asList(DATE, DATETIME)) { assertTrue(areCompatible(intervalType, dateTimeType)); assertTrue(areCompatible(dateTimeType, intervalType)); } @@ -162,28 +165,51 @@ public void testIntervalCompabitilityWithDateTimes() { } public void testEsToDataType() { - List types = new ArrayList<>(Arrays.asList("null", "boolean", "bool", - "byte", "tinyint", - "short", "smallint", + List types = new ArrayList<>( + Arrays.asList( + "null", + "boolean", + "bool", + "byte", + "tinyint", + "short", + "smallint", "integer", - "long", "bigint", - "double", "real", - "half_float", "scaled_float", "float", - "decimal", "numeric", - "keyword", "text", "varchar", - "date", "datetime", "timestamp", - "binary", "varbinary", + "long", + "bigint", + "double", + "real", + "half_float", + "scaled_float", + "float", + "decimal", + "numeric", + "keyword", + "text", + "varchar", + "date", + "datetime", + "timestamp", + "binary", + "varbinary", "ip", - "interval_year", "interval_month", "interval_year_to_month", - "interval_day", "interval_hour", "interval_minute", "interval_second", - "interval_day_to_hour", "interval_day_to_minute", "interval_day_to_second", - "interval_hour_to_minute", "interval_hour_to_second", - "interval_minute_to_second")); - - types.addAll(SqlDataTypes.types().stream() - .filter(DataTypes::isPrimitive) - .map(DataType::typeName) - .collect(toList())); + "interval_year", + "interval_month", + "interval_year_to_month", + "interval_day", + "interval_hour", + "interval_minute", + "interval_second", + "interval_day_to_hour", + "interval_day_to_minute", + "interval_day_to_second", + "interval_hour_to_minute", + "interval_hour_to_second", + "interval_minute_to_second" + ) + ); + + types.addAll(SqlDataTypes.types().stream().filter(DataTypes::isPrimitive).map(DataType::typeName).collect(toList())); String type = randomFrom(types.toArray(new String[0])); DataType dataType = SqlDataTypes.fromSqlOrEsType(type); assertNotNull("cound not find " + type, dataType); diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/types/SqlTypesTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/types/SqlTypesTests.java index acb21ccdab9f1..8e05925c54a47 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/types/SqlTypesTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/types/SqlTypesTests.java @@ -23,7 +23,6 @@ public class SqlTypesTests extends ESTestCase { - public void testGeoField() { Map mapping = loadMapping("mapping-geo.json"); assertThat(mapping.size(), is(2)); @@ -33,7 +32,6 @@ public void testGeoField() { assertThat(gs.getDataType().typeName(), is("geo_shape")); } - public void testTextField() { Map mapping = loadMapping("mapping-text.json"); assertThat(mapping.size(), is(1)); diff --git a/x-pack/plugin/src/test/java/org/elasticsearch/xpack/test/rest/AbstractXPackRestTest.java b/x-pack/plugin/src/test/java/org/elasticsearch/xpack/test/rest/AbstractXPackRestTest.java index f7433e2414ec1..85342782ff930 100644 --- a/x-pack/plugin/src/test/java/org/elasticsearch/xpack/test/rest/AbstractXPackRestTest.java +++ b/x-pack/plugin/src/test/java/org/elasticsearch/xpack/test/rest/AbstractXPackRestTest.java @@ -42,8 +42,10 @@ // TODO: Remove this timeout increase once this test suite is broken up @TimeoutSuite(millis = 60 * TimeUnits.MINUTE) public class AbstractXPackRestTest extends ESClientYamlSuiteTestCase { - private static final String BASIC_AUTH_VALUE = - basicAuthHeaderValue("x_pack_rest_user", SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING); + private static final String BASIC_AUTH_VALUE = basicAuthHeaderValue( + "x_pack_rest_user", + SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING + ); public AbstractXPackRestTest(ClientYamlTestCandidate testCandidate) { super(testCandidate); @@ -56,12 +58,9 @@ public static Iterable parameters() throws Exception { @Override protected Settings restClientSettings() { - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", BASIC_AUTH_VALUE) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", BASIC_AUTH_VALUE).build(); } - @Before public void setupForTests() throws Exception { waitForTemplates(); @@ -79,9 +78,13 @@ private void waitForTemplates() { ); for (String template : templates) { - awaitCallApi("indices.exists_index_template", singletonMap("name", template), emptyList(), + awaitCallApi( + "indices.exists_index_template", + singletonMap("name", template), + emptyList(), response -> true, - () -> "Exception when waiting for [" + template + "] template to be created"); + () -> "Exception when waiting for [" + template + "] template to be created" + ); } } } @@ -99,8 +102,8 @@ public void cleanup() throws Exception { // This waits for pending tasks to complete, so must go last (otherwise // it could be waiting for pending tasks while monitoring is still running). waitForPendingTasks(adminClient(), task -> { - // Don't check rollup jobs because we clear them in the superclass. - return task.contains(RollupJob.NAME); + // Don't check rollup jobs because we clear them in the superclass. + return task.contains(RollupJob.NAME); }); } } @@ -117,11 +120,13 @@ private void clearMlState() throws Exception { /** * Executes an API call using the admin context, waiting for it to succeed. */ - private void awaitCallApi(String apiName, - Map params, - List> bodies, - CheckedFunction success, - Supplier error) { + private void awaitCallApi( + String apiName, + Map params, + List> bodies, + CheckedFunction success, + Supplier error + ) { try { final AtomicReference response = new AtomicReference<>(); assertBusy(() -> { @@ -136,10 +141,12 @@ private void awaitCallApi(String apiName, } } - private ClientYamlTestResponse callApi(String apiName, - Map params, - List> bodies, - Map headers) throws IOException { + private ClientYamlTestResponse callApi( + String apiName, + Map params, + List> bodies, + Map headers + ) throws IOException { return getAdminExecutionContext().callApi(apiName, params, bodies, headers); } diff --git a/x-pack/plugin/src/yamlRestTest/java/org/elasticsearch/xpack/test/rest/XPackRestIT.java b/x-pack/plugin/src/yamlRestTest/java/org/elasticsearch/xpack/test/rest/XPackRestIT.java index b5b41e045dd02..3e060d2875e8a 100644 --- a/x-pack/plugin/src/yamlRestTest/java/org/elasticsearch/xpack/test/rest/XPackRestIT.java +++ b/x-pack/plugin/src/yamlRestTest/java/org/elasticsearch/xpack/test/rest/XPackRestIT.java @@ -8,6 +8,7 @@ package org.elasticsearch.xpack.test.rest; import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; + import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate; public class XPackRestIT extends AbstractXPackRestTest { @@ -21,4 +22,3 @@ public static Iterable parameters() throws Exception { return createParameters(); } } - diff --git a/x-pack/plugin/stack/qa/rest/src/yamlRestTest/java/org/elasticsearch/xpack/stack/StackYamlIT.java b/x-pack/plugin/stack/qa/rest/src/yamlRestTest/java/org/elasticsearch/xpack/stack/StackYamlIT.java index 894f42faa20a4..313361d330818 100644 --- a/x-pack/plugin/stack/qa/rest/src/yamlRestTest/java/org/elasticsearch/xpack/stack/StackYamlIT.java +++ b/x-pack/plugin/stack/qa/rest/src/yamlRestTest/java/org/elasticsearch/xpack/stack/StackYamlIT.java @@ -10,6 +10,7 @@ import com.carrotsearch.randomizedtesting.annotations.Name; import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; import com.carrotsearch.randomizedtesting.annotations.TimeoutSuite; + import org.apache.lucene.util.TimeUnits; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; diff --git a/x-pack/plugin/text-structure/qa/text-structure-with-security/src/yamlRestTest/java/org/elasticsearch/smoketest/TextStructureWithSecurityIT.java b/x-pack/plugin/text-structure/qa/text-structure-with-security/src/yamlRestTest/java/org/elasticsearch/smoketest/TextStructureWithSecurityIT.java index b6892ff884d5b..cfbd7cbc7d0f6 100644 --- a/x-pack/plugin/text-structure/qa/text-structure-with-security/src/yamlRestTest/java/org/elasticsearch/smoketest/TextStructureWithSecurityIT.java +++ b/x-pack/plugin/text-structure/qa/text-structure-with-security/src/yamlRestTest/java/org/elasticsearch/smoketest/TextStructureWithSecurityIT.java @@ -7,6 +7,7 @@ package org.elasticsearch.smoketest; import com.carrotsearch.randomizedtesting.annotations.Name; + import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; diff --git a/x-pack/plugin/transform/qa/multi-cluster-tests-with-security/src/test/java/org/elasticsearch/multi_cluster/MultiClusterYamlTestSuiteIT.java b/x-pack/plugin/transform/qa/multi-cluster-tests-with-security/src/test/java/org/elasticsearch/multi_cluster/MultiClusterYamlTestSuiteIT.java index c08e331f90997..f2488719a9e95 100644 --- a/x-pack/plugin/transform/qa/multi-cluster-tests-with-security/src/test/java/org/elasticsearch/multi_cluster/MultiClusterYamlTestSuiteIT.java +++ b/x-pack/plugin/transform/qa/multi-cluster-tests-with-security/src/test/java/org/elasticsearch/multi_cluster/MultiClusterYamlTestSuiteIT.java @@ -41,9 +41,6 @@ public static Iterable parameters() throws Exception { @Override protected Settings restClientSettings() { String token = basicAuthHeaderValue(USER, new SecureString(PASS.toCharArray())); - return Settings.builder() - .put(super.restClientSettings()) - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(super.restClientSettings()).put(ThreadContext.PREFIX + ".Authorization", token).build(); } } diff --git a/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/LatestIT.java b/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/LatestIT.java index 5dafeef6bb32e..43458d25ffef1 100644 --- a/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/LatestIT.java +++ b/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/LatestIT.java @@ -64,54 +64,53 @@ private static final String getDateStringForRow(int row) { private static final String STARS = "stars"; private static final String COMMENT = "comment"; - private static final Map row( - String userId, String businessId, int count, int stars, String timestamp, String comment) { - return new HashMap<>() {{ - if (userId != null) { - put(USER_ID, userId); + private static final Map row(String userId, String businessId, int count, int stars, String timestamp, String comment) { + return new HashMap<>() { + { + if (userId != null) { + put(USER_ID, userId); + } + put(BUSINESS_ID, businessId); + put(COUNT, count); + put(STARS, stars); + put(TIMESTAMP, timestamp); + put(COMMENT, comment); + put("regular_object", singletonMap("foo", 42)); + put("nested_object", singletonMap("bar", 43)); } - put(BUSINESS_ID, businessId); - put(COUNT, count); - put(STARS, stars); - put(TIMESTAMP, timestamp); - put(COMMENT, comment); - put("regular_object", singletonMap("foo", 42)); - put("nested_object", singletonMap("bar", 43)); - }}; + }; } - private static final Object[] EXPECTED_DEST_INDEX_ROWS = - new Object[] { - row("user_0", "business_37", 87, 2, "2017-04-04T12:30:00Z", "Great stuff, deserves 2 stars"), - row("user_1", "business_38", 88, 3, "2017-04-05T12:30:00Z", "Great stuff, deserves 3 stars"), - row("user_2", "business_39", 89, 4, "2017-04-06T12:30:00Z", "Great stuff, deserves 4 stars"), - row("user_3", "business_40", 90, 0, "2017-04-07T12:30:00Z", "Great stuff, deserves 0 stars"), - row("user_4", "business_41", 91, 1, "2017-04-08T12:30:00Z", "Great stuff, deserves 1 stars"), - row("user_5", "business_42", 92, 2, "2017-04-09T12:30:00Z", "Great stuff, deserves 2 stars"), - row("user_6", "business_43", 93, 3, "2017-04-10T12:30:00Z", "Great stuff, deserves 3 stars"), - row("user_7", "business_44", 94, 4, "2017-04-11T12:30:00Z", "Great stuff, deserves 4 stars"), - row("user_8", "business_45", 95, 0, "2017-04-12T12:30:00Z", "Great stuff, deserves 0 stars"), - row("user_9", "business_46", 96, 1, "2017-04-13T12:30:00Z", "Great stuff, deserves 1 stars"), - row("user_10", "business_47", 97, 2, "2017-04-14T12:30:00Z", "Great stuff, deserves 2 stars"), - row("user_11", "business_48", 98, 3, "2017-04-15T12:30:00Z", "Great stuff, deserves 3 stars"), - row("user_12", "business_49", 99, 4, "2017-04-16T12:30:00Z", "Great stuff, deserves 4 stars"), - row("user_13", "business_21", 71, 1, "2017-03-16T12:30:00Z", "Great stuff, deserves 1 stars"), - row("user_14", "business_22", 72, 2, "2017-03-17T12:30:00Z", "Great stuff, deserves 2 stars"), - row("user_15", "business_23", 73, 3, "2017-03-18T12:30:00Z", "Great stuff, deserves 3 stars"), - row("user_16", "business_24", 74, 4, "2017-03-19T12:30:00Z", "Great stuff, deserves 4 stars"), - row("user_17", "business_25", 75, 0, "2017-03-20T12:30:00Z", "Great stuff, deserves 0 stars"), - row("user_18", "business_26", 76, 1, "2017-03-21T12:30:00Z", "Great stuff, deserves 1 stars"), - row("user_19", "business_27", 77, 2, "2017-03-22T12:30:00Z", "Great stuff, deserves 2 stars"), - row("user_20", "business_28", 78, 3, "2017-03-23T12:30:00Z", "Great stuff, deserves 3 stars"), - row("user_21", "business_29", 79, 4, "2017-03-24T12:30:00Z", "Great stuff, deserves 4 stars"), - row("user_22", "business_30", 80, 0, "2017-03-25T12:30:00Z", "Great stuff, deserves 0 stars"), - row("user_23", "business_31", 81, 1, "2017-03-26T12:30:00Z", "Great stuff, deserves 1 stars"), - row("user_24", "business_32", 82, 2, "2017-03-27T12:30:00Z", "Great stuff, deserves 2 stars"), - row("user_25", "business_33", 83, 3, "2017-03-28T12:30:00Z", "Great stuff, deserves 3 stars"), - row("user_26", "business_34", 84, 4, "2017-04-01T12:30:00Z", "Great stuff, deserves 4 stars"), - row("user_27", "business_35", 85, 0, "2017-04-02T12:30:00Z", "Great stuff, deserves 0 stars"), - row(null, "business_36", 86, 1, "2017-04-03T12:30:00Z", "Great stuff, deserves 1 stars") - }; + private static final Object[] EXPECTED_DEST_INDEX_ROWS = new Object[] { + row("user_0", "business_37", 87, 2, "2017-04-04T12:30:00Z", "Great stuff, deserves 2 stars"), + row("user_1", "business_38", 88, 3, "2017-04-05T12:30:00Z", "Great stuff, deserves 3 stars"), + row("user_2", "business_39", 89, 4, "2017-04-06T12:30:00Z", "Great stuff, deserves 4 stars"), + row("user_3", "business_40", 90, 0, "2017-04-07T12:30:00Z", "Great stuff, deserves 0 stars"), + row("user_4", "business_41", 91, 1, "2017-04-08T12:30:00Z", "Great stuff, deserves 1 stars"), + row("user_5", "business_42", 92, 2, "2017-04-09T12:30:00Z", "Great stuff, deserves 2 stars"), + row("user_6", "business_43", 93, 3, "2017-04-10T12:30:00Z", "Great stuff, deserves 3 stars"), + row("user_7", "business_44", 94, 4, "2017-04-11T12:30:00Z", "Great stuff, deserves 4 stars"), + row("user_8", "business_45", 95, 0, "2017-04-12T12:30:00Z", "Great stuff, deserves 0 stars"), + row("user_9", "business_46", 96, 1, "2017-04-13T12:30:00Z", "Great stuff, deserves 1 stars"), + row("user_10", "business_47", 97, 2, "2017-04-14T12:30:00Z", "Great stuff, deserves 2 stars"), + row("user_11", "business_48", 98, 3, "2017-04-15T12:30:00Z", "Great stuff, deserves 3 stars"), + row("user_12", "business_49", 99, 4, "2017-04-16T12:30:00Z", "Great stuff, deserves 4 stars"), + row("user_13", "business_21", 71, 1, "2017-03-16T12:30:00Z", "Great stuff, deserves 1 stars"), + row("user_14", "business_22", 72, 2, "2017-03-17T12:30:00Z", "Great stuff, deserves 2 stars"), + row("user_15", "business_23", 73, 3, "2017-03-18T12:30:00Z", "Great stuff, deserves 3 stars"), + row("user_16", "business_24", 74, 4, "2017-03-19T12:30:00Z", "Great stuff, deserves 4 stars"), + row("user_17", "business_25", 75, 0, "2017-03-20T12:30:00Z", "Great stuff, deserves 0 stars"), + row("user_18", "business_26", 76, 1, "2017-03-21T12:30:00Z", "Great stuff, deserves 1 stars"), + row("user_19", "business_27", 77, 2, "2017-03-22T12:30:00Z", "Great stuff, deserves 2 stars"), + row("user_20", "business_28", 78, 3, "2017-03-23T12:30:00Z", "Great stuff, deserves 3 stars"), + row("user_21", "business_29", 79, 4, "2017-03-24T12:30:00Z", "Great stuff, deserves 4 stars"), + row("user_22", "business_30", 80, 0, "2017-03-25T12:30:00Z", "Great stuff, deserves 0 stars"), + row("user_23", "business_31", 81, 1, "2017-03-26T12:30:00Z", "Great stuff, deserves 1 stars"), + row("user_24", "business_32", 82, 2, "2017-03-27T12:30:00Z", "Great stuff, deserves 2 stars"), + row("user_25", "business_33", 83, 3, "2017-03-28T12:30:00Z", "Great stuff, deserves 3 stars"), + row("user_26", "business_34", 84, 4, "2017-04-01T12:30:00Z", "Great stuff, deserves 4 stars"), + row("user_27", "business_35", 85, 0, "2017-04-02T12:30:00Z", "Great stuff, deserves 0 stars"), + row(null, "business_36", 86, 1, "2017-04-03T12:30:00Z", "Great stuff, deserves 1 stars") }; @After public void cleanTransforms() throws IOException { @@ -122,14 +121,12 @@ public void testLatest() throws Exception { createReviewsIndex(SOURCE_INDEX_NAME, 100, NUM_USERS, LatestIT::getUserIdForRow, LatestIT::getDateStringForRow); String destIndexName = "reviews-latest"; - TransformConfig transformConfig = - createTransformConfigBuilder(TRANSFORM_NAME, destIndexName, QueryBuilders.matchAllQuery(), SOURCE_INDEX_NAME) - .setLatestConfig( - LatestConfig.builder() - .setUniqueKey(USER_ID) - .setSort(TIMESTAMP) - .build()) - .build(); + TransformConfig transformConfig = createTransformConfigBuilder( + TRANSFORM_NAME, + destIndexName, + QueryBuilders.matchAllQuery(), + SOURCE_INDEX_NAME + ).setLatestConfig(LatestConfig.builder().setUniqueKey(USER_ID).setSort(TIMESTAMP).build()).build(); assertTrue(putTransform(transformConfig, RequestOptions.DEFAULT).isAcknowledged()); assertTrue(startTransform(transformConfig.getId(), RequestOptions.DEFAULT).isAcknowledged()); waitUntilCheckpoint(transformConfig.getId(), 1L); @@ -138,30 +135,31 @@ public void testLatest() throws Exception { try (RestHighLevelClient restClient = new TestRestHighLevelClient()) { restClient.indices().refresh(new RefreshRequest(destIndexName), RequestOptions.DEFAULT); // Verify destination index mappings - GetMappingsResponse destIndexMapping = - restClient.indices().getMapping(new GetMappingsRequest().indices(destIndexName), RequestOptions.DEFAULT); + GetMappingsResponse destIndexMapping = restClient.indices() + .getMapping(new GetMappingsRequest().indices(destIndexName), RequestOptions.DEFAULT); assertThat(destIndexMapping.mappings().get(destIndexName).sourceAsMap(), allOf(hasKey("_meta"), hasKey("properties"))); // Verify destination index contents - SearchResponse searchResponse = - restClient.search(new SearchRequest(destIndexName).source(new SearchSourceBuilder().size(1000)), RequestOptions.DEFAULT); + SearchResponse searchResponse = restClient.search( + new SearchRequest(destIndexName).source(new SearchSourceBuilder().size(1000)), + RequestOptions.DEFAULT + ); assertThat(searchResponse.getHits().getTotalHits().value, is(equalTo(Long.valueOf(NUM_USERS + 1)))); assertThat( Stream.of(searchResponse.getHits().getHits()).map(SearchHit::getSourceAsMap).collect(toList()), - containsInAnyOrder(EXPECTED_DEST_INDEX_ROWS)); + containsInAnyOrder(EXPECTED_DEST_INDEX_ROWS) + ); } } public void testLatestPreview() throws Exception { createReviewsIndex(SOURCE_INDEX_NAME, 100, NUM_USERS, LatestIT::getUserIdForRow, LatestIT::getDateStringForRow); - TransformConfig transformConfig = - createTransformConfigBuilder(TRANSFORM_NAME, "dummy", QueryBuilders.matchAllQuery(), SOURCE_INDEX_NAME) - .setLatestConfig( - LatestConfig.builder() - .setUniqueKey(USER_ID) - .setSort(TIMESTAMP) - .build()) - .build(); + TransformConfig transformConfig = createTransformConfigBuilder( + TRANSFORM_NAME, + "dummy", + QueryBuilders.matchAllQuery(), + SOURCE_INDEX_NAME + ).setLatestConfig(LatestConfig.builder().setUniqueKey(USER_ID).setSort(TIMESTAMP).build()).build(); PreviewTransformResponse previewResponse = previewTransform(transformConfig, RequestOptions.DEFAULT); // Verify preview mappings diff --git a/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TestFeatureResetIT.java b/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TestFeatureResetIT.java index 9ff33b1630580..a83b3813f7856 100644 --- a/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TestFeatureResetIT.java +++ b/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TestFeatureResetIT.java @@ -66,20 +66,21 @@ public void testTransformFeatureReset() throws Exception { .addAggregator(AggregationBuilders.avg("review_score").field("stars")) .addAggregator(AggregationBuilders.max("timestamp").field("timestamp")); - TransformConfig config = - createTransformConfigBuilder(transformId, "reviews-by-user-business-day", QueryBuilders.matchAllQuery(), indexName) - .setPivotConfig(createPivotConfig(groups, aggs)) - .build(); + TransformConfig config = createTransformConfigBuilder( + transformId, + "reviews-by-user-business-day", + QueryBuilders.matchAllQuery(), + indexName + ).setPivotConfig(createPivotConfig(groups, aggs)).build(); assertTrue(putTransform(config, RequestOptions.DEFAULT).isAcknowledged()); assertTrue(startTransform(config.getId(), RequestOptions.DEFAULT).isAcknowledged()); transformId = "continuous-transform-feature-reset"; - config = - createTransformConfigBuilder(transformId, "reviews-by-user-business-day-cont", QueryBuilders.matchAllQuery(), indexName) - .setPivotConfig(createPivotConfig(groups, aggs)) - .setSyncConfig(TimeSyncConfig.builder().setField("timestamp").setDelay(TimeValue.timeValueSeconds(1)).build()) - .build(); + config = createTransformConfigBuilder(transformId, "reviews-by-user-business-day-cont", QueryBuilders.matchAllQuery(), indexName) + .setPivotConfig(createPivotConfig(groups, aggs)) + .setSyncConfig(TimeSyncConfig.builder().setField("timestamp").setDelay(TimeValue.timeValueSeconds(1)).build()) + .build(); assertTrue(putTransform(config, RequestOptions.DEFAULT).isAcknowledged()); assertTrue(startTransform(config.getId(), RequestOptions.DEFAULT).isAcknowledged()); @@ -87,21 +88,18 @@ public void testTransformFeatureReset() throws Exception { highLevelClient.features().resetFeatures(new ResetFeaturesRequest(), RequestOptions.DEFAULT); Response response = adminClient().performRequest(new Request("GET", "/_cluster/state?metric=metadata")); - Map metadata = (Map)ESRestTestCase.entityAsMap(response).get("metadata"); + Map metadata = (Map) ESRestTestCase.entityAsMap(response).get("metadata"); assertThat(metadata, is(not(nullValue()))); // after a successful reset we completely remove the transform metadata - Map transformMetadata = (Map)metadata.get("transform"); + Map transformMetadata = (Map) metadata.get("transform"); assertThat(transformMetadata, is(nullValue())); // assert transforms are gone assertThat(getTransform("_all").getCount(), equalTo(0L)); // assert transform indices are gone - assertThat( - ESRestTestCase.entityAsMap(adminClient().performRequest(new Request("GET", ".transform-*"))), - is(anEmptyMap()) - ); + assertThat(ESRestTestCase.entityAsMap(adminClient().performRequest(new Request("GET", ".transform-*"))), is(anEmptyMap())); } } diff --git a/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformIT.java b/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformIT.java index 936b47eed307c..6140edbb02c68 100644 --- a/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformIT.java +++ b/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformIT.java @@ -28,13 +28,13 @@ import org.elasticsearch.client.transform.transforms.pivot.TermsGroupSource; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.AggregatorFactories; import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; import org.elasticsearch.search.builder.SearchSourceBuilder; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; import org.junit.After; import org.junit.Before; @@ -97,10 +97,12 @@ public void testTransformCrud() throws Exception { .addAggregator(AggregationBuilders.avg("review_score").field("stars")) .addAggregator(AggregationBuilders.max("timestamp").field("timestamp")); - TransformConfig config = - createTransformConfigBuilder(transformId, "reviews-by-user-business-day", QueryBuilders.matchAllQuery(), indexName) - .setPivotConfig(createPivotConfig(groups, aggs)) - .build(); + TransformConfig config = createTransformConfigBuilder( + transformId, + "reviews-by-user-business-day", + QueryBuilders.matchAllQuery(), + indexName + ).setPivotConfig(createPivotConfig(groups, aggs)).build(); assertTrue(putTransform(config, RequestOptions.DEFAULT).isAcknowledged()); assertTrue(startTransform(config.getId(), RequestOptions.DEFAULT).isAcknowledged()); @@ -108,9 +110,9 @@ public void testTransformCrud() throws Exception { waitUntilCheckpoint(config.getId(), 1L); stopTransform(config.getId()); - assertBusy(() -> { - assertEquals(TransformStats.State.STOPPED, getTransformStats(config.getId()).getTransformsStats().get(0).getState()); - }); + assertBusy( + () -> { assertEquals(TransformStats.State.STOPPED, getTransformStats(config.getId()).getTransformsStats().get(0).getState()); } + ); TransformConfig storedConfig = getTransform(config.getId()).getTransformConfigurations().get(0); assertThat(storedConfig.getVersion(), equalTo(Version.CURRENT)); @@ -133,12 +135,15 @@ public void testContinuousTransformCrud() throws Exception { .addAggregator(AggregationBuilders.avg("review_score").field("stars")) .addAggregator(AggregationBuilders.max("timestamp").field("timestamp")); - TransformConfig config = - createTransformConfigBuilder(transformId, "reviews-by-user-business-day", QueryBuilders.matchAllQuery(), indexName) - .setPivotConfig(createPivotConfig(groups, aggs)) - .setSyncConfig(TimeSyncConfig.builder().setField("timestamp").setDelay(TimeValue.timeValueSeconds(1)).build()) - .setSettings(SettingsConfig.builder().setAlignCheckpoints(false).build()) - .build(); + TransformConfig config = createTransformConfigBuilder( + transformId, + "reviews-by-user-business-day", + QueryBuilders.matchAllQuery(), + indexName + ).setPivotConfig(createPivotConfig(groups, aggs)) + .setSyncConfig(TimeSyncConfig.builder().setField("timestamp").setDelay(TimeValue.timeValueSeconds(1)).build()) + .setSettings(SettingsConfig.builder().setAlignCheckpoints(false).build()) + .build(); assertTrue(putTransform(config, RequestOptions.DEFAULT).isAcknowledged()); assertTrue(startTransform(config.getId(), RequestOptions.DEFAULT).isAcknowledged()); @@ -182,11 +187,9 @@ public void testContinuousTransformUpdate() throws Exception { String id = "transform-to-update"; String dest = "reviews-by-user-business-day-to-update"; - TransformConfig config = - createTransformConfigBuilder(id, dest, QueryBuilders.matchAllQuery(), indexName) - .setPivotConfig(createPivotConfig(groups, aggs)) - .setSyncConfig(TimeSyncConfig.builder().setField("timestamp").setDelay(TimeValue.timeValueSeconds(1)).build()) - .build(); + TransformConfig config = createTransformConfigBuilder(id, dest, QueryBuilders.matchAllQuery(), indexName).setPivotConfig( + createPivotConfig(groups, aggs) + ).setSyncConfig(TimeSyncConfig.builder().setField("timestamp").setDelay(TimeValue.timeValueSeconds(1)).build()).build(); assertTrue(putTransform(config, RequestOptions.DEFAULT).isAcknowledged()); assertTrue(startTransform(config.getId(), RequestOptions.DEFAULT).isAcknowledged()); @@ -268,11 +271,14 @@ public void testStopWaitForCheckpoint() throws Exception { .addAggregator(AggregationBuilders.avg("review_score").field("stars")) .addAggregator(AggregationBuilders.max("timestamp").field("timestamp")); - TransformConfig config = - createTransformConfigBuilder(transformId, "reviews-by-user-business-day", QueryBuilders.matchAllQuery(), indexName) - .setPivotConfig(createPivotConfig(groups, aggs)) - .setSyncConfig(TimeSyncConfig.builder().setField("timestamp").setDelay(TimeValue.timeValueSeconds(1)).build()) - .build(); + TransformConfig config = createTransformConfigBuilder( + transformId, + "reviews-by-user-business-day", + QueryBuilders.matchAllQuery(), + indexName + ).setPivotConfig(createPivotConfig(groups, aggs)) + .setSyncConfig(TimeSyncConfig.builder().setField("timestamp").setDelay(TimeValue.timeValueSeconds(1)).build()) + .build(); assertTrue(putTransform(config, RequestOptions.DEFAULT).isAcknowledged()); @@ -335,13 +341,16 @@ public void testContinuousTransformRethrottle() throws Exception { .addAggregator(AggregationBuilders.avg("review_score").field("stars")) .addAggregator(AggregationBuilders.max("timestamp").field("timestamp")); - TransformConfig config = - createTransformConfigBuilder(transformId, "reviews-by-user-business-day", QueryBuilders.matchAllQuery(), indexName) - .setPivotConfig(createPivotConfig(groups, aggs)) - .setSyncConfig(TimeSyncConfig.builder().setField("timestamp").setDelay(TimeValue.timeValueSeconds(1)).build()) - // set requests per second and page size low enough to fail the test if update does not succeed, - .setSettings(SettingsConfig.builder().setRequestsPerSecond(1F).setMaxPageSearchSize(10).setAlignCheckpoints(false).build()) - .build(); + TransformConfig config = createTransformConfigBuilder( + transformId, + "reviews-by-user-business-day", + QueryBuilders.matchAllQuery(), + indexName + ).setPivotConfig(createPivotConfig(groups, aggs)) + .setSyncConfig(TimeSyncConfig.builder().setField("timestamp").setDelay(TimeValue.timeValueSeconds(1)).build()) + // set requests per second and page size low enough to fail the test if update does not succeed, + .setSettings(SettingsConfig.builder().setRequestsPerSecond(1F).setMaxPageSearchSize(10).setAlignCheckpoints(false).build()) + .build(); assertTrue(putTransform(config, RequestOptions.DEFAULT).isAcknowledged()); assertTrue(startTransform(config.getId(), RequestOptions.DEFAULT).isAcknowledged()); diff --git a/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformIntegTestCase.java b/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformIntegTestCase.java index 830039ce91b2a..3039691ccfd36 100644 --- a/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformIntegTestCase.java +++ b/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformIntegTestCase.java @@ -46,15 +46,9 @@ import org.elasticsearch.client.transform.transforms.pivot.SingleGroupSource; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.DeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.query.MatchAllQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.rest.RestStatus; @@ -65,6 +59,12 @@ import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.test.rest.ESRestTestCase; +import org.elasticsearch.xcontent.DeprecationHandler; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; import java.io.IOException; import java.nio.charset.StandardCharsets; @@ -283,14 +283,9 @@ protected AggregationConfig createAggConfig(AggregatorFactories.Builder aggregat return new AggregationConfig(aggregations); } - protected PivotConfig createPivotConfig( - Map groups, - AggregatorFactories.Builder aggregations - ) throws Exception { - return PivotConfig.builder() - .setGroups(createGroupConfig(groups)) - .setAggregationConfig(createAggConfig(aggregations)) - .build(); + protected PivotConfig createPivotConfig(Map groups, AggregatorFactories.Builder aggregations) + throws Exception { + return PivotConfig.builder().setGroups(createGroupConfig(groups)).setAggregationConfig(createAggConfig(aggregations)).build(); } protected TransformConfig.Builder createTransformConfigBuilder( @@ -320,11 +315,13 @@ protected void updateConfig(String id, TransformConfigUpdate update) throws Exce } } - protected void createReviewsIndex(String indexName, - int numDocs, - int numUsers, - Function userIdProvider, - Function dateStringProvider) throws Exception { + protected void createReviewsIndex( + String indexName, + int numDocs, + int numUsers, + Function userIdProvider, + Function dateStringProvider + ) throws Exception { assert numUsers > 0; try (RestHighLevelClient restClient = new TestRestHighLevelClient()) { @@ -380,14 +377,9 @@ protected void createReviewsIndex(String indexName, StringBuilder sourceBuilder = new StringBuilder().append("{"); if (user != null) { - sourceBuilder - .append("\"user_id\":\"") - .append("user_") - .append(user) - .append("\","); + sourceBuilder.append("\"user_id\":\"").append("user_").append(user).append("\","); } - sourceBuilder - .append("\"count\":") + sourceBuilder.append("\"count\":") .append(i) .append(",\"business_id\":\"") .append("business_") diff --git a/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformUsingSearchRuntimeFieldsIT.java b/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformUsingSearchRuntimeFieldsIT.java index b67ed06972f0e..cff4dd112a483 100644 --- a/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformUsingSearchRuntimeFieldsIT.java +++ b/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformUsingSearchRuntimeFieldsIT.java @@ -69,24 +69,39 @@ private static final String getDateStringForRow(int row) { } private static Map createRuntimeMappings() { - return new HashMap<>() {{ - put("user-upper", new HashMap<>() {{ - put("type", "keyword"); - put("script", singletonMap("source", "if (params._source.user_id != null) {emit(params._source.user_id.toUpperCase())}")); - }}); - put("stars", new HashMap<>() {{ - put("type", "long"); - }}); - put("stars-x2", new HashMap<>() {{ - put("type", "long"); - put("script", singletonMap("source", "if (params._source.stars != null) {emit(2 * params._source.stars)}")); - }}); - put("timestamp-5m", new HashMap<>() {{ - put("type", "date"); - put("script", singletonMap( - "source", "emit(doc['timestamp'].value.toInstant().minus(5, ChronoUnit.MINUTES).toEpochMilli())")); - }}); - }}; + return new HashMap<>() { + { + put("user-upper", new HashMap<>() { + { + put("type", "keyword"); + put( + "script", + singletonMap("source", "if (params._source.user_id != null) {emit(params._source.user_id.toUpperCase())}") + ); + } + }); + put("stars", new HashMap<>() { + { + put("type", "long"); + } + }); + put("stars-x2", new HashMap<>() { + { + put("type", "long"); + put("script", singletonMap("source", "if (params._source.stars != null) {emit(2 * params._source.stars)}")); + } + }); + put("timestamp-5m", new HashMap<>() { + { + put("type", "date"); + put( + "script", + singletonMap("source", "emit(doc['timestamp'].value.toInstant().minus(5, ChronoUnit.MINUTES).toEpochMilli())") + ); + } + }); + } + }; } @Before @@ -96,7 +111,8 @@ public void createReviewsIndex() throws Exception { 100, NUM_USERS, TransformUsingSearchRuntimeFieldsIT::getUserIdForRow, - TransformUsingSearchRuntimeFieldsIT::getDateStringForRow); + TransformUsingSearchRuntimeFieldsIT::getDateStringForRow + ); } @After @@ -117,20 +133,18 @@ public void testPivotTransform() throws Exception { .addAggregator(AggregationBuilders.max("review_score_rt_max").field("stars-x2")) .addAggregator(AggregationBuilders.max("timestamp").field("timestamp")) .addAggregator(AggregationBuilders.max("timestamp_rt").field("timestamp-5m")); - TransformConfig config = - createTransformConfigBuilder(transformId, destIndexName, QueryBuilders.matchAllQuery(), "dummy") - .setSource(SourceConfig.builder() - .setIndex(REVIEWS_INDEX_NAME) - .setQuery(QueryBuilders.matchAllQuery()) - .setRuntimeMappings(runtimeMappings) - .build()) - .setPivotConfig(createPivotConfig(groups, aggs)) - .build(); + TransformConfig config = createTransformConfigBuilder(transformId, destIndexName, QueryBuilders.matchAllQuery(), "dummy").setSource( + SourceConfig.builder() + .setIndex(REVIEWS_INDEX_NAME) + .setQuery(QueryBuilders.matchAllQuery()) + .setRuntimeMappings(runtimeMappings) + .build() + ).setPivotConfig(createPivotConfig(groups, aggs)).build(); PreviewTransformResponse previewResponse = previewTransform(config, RequestOptions.DEFAULT); // Verify preview mappings - Map expectedMappingProperties = - new HashMap<>() {{ + Map expectedMappingProperties = new HashMap<>() { + { put("by-user", singletonMap("type", "keyword")); put("review_score", singletonMap("type", "double")); put("review_score_max", singletonMap("type", "long")); @@ -138,20 +152,20 @@ public void testPivotTransform() throws Exception { put("review_score_rt_max", singletonMap("type", "long")); put("timestamp", singletonMap("type", "date")); put("timestamp_rt", singletonMap("type", "date")); - }}; + } + }; assertThat(previewResponse.getMappings(), allOf(hasKey("_meta"), hasEntry("properties", expectedMappingProperties))); // Verify preview contents assertThat(previewResponse.getDocs(), hasSize(NUM_USERS)); - previewResponse.getDocs().forEach( - doc -> { - assertThat((String) doc.get("by-user"), isUpperCase()); - assertThat(doc.get("review_score_rt_avg"), is(equalTo(2 * (double) doc.get("review_score")))); - assertThat(doc.get("review_score_rt_max"), is(equalTo(2 * (int) doc.get("review_score_max")))); - assertThat( - Instant.parse((String) doc.get("timestamp_rt")), - is(equalTo(Instant.parse((String) doc.get("timestamp")).minus(5, ChronoUnit.MINUTES)))); - } - ); + previewResponse.getDocs().forEach(doc -> { + assertThat((String) doc.get("by-user"), isUpperCase()); + assertThat(doc.get("review_score_rt_avg"), is(equalTo(2 * (double) doc.get("review_score")))); + assertThat(doc.get("review_score_rt_max"), is(equalTo(2 * (int) doc.get("review_score_max")))); + assertThat( + Instant.parse((String) doc.get("timestamp_rt")), + is(equalTo(Instant.parse((String) doc.get("timestamp")).minus(5, ChronoUnit.MINUTES))) + ); + }); assertTrue(putTransform(config, RequestOptions.DEFAULT).isAcknowledged()); assertTrue(startTransform(config.getId(), RequestOptions.DEFAULT).isAcknowledged()); @@ -159,36 +173,46 @@ public void testPivotTransform() throws Exception { waitUntilCheckpoint(config.getId(), 1L); stopTransform(config.getId()); - assertBusy(() -> { - assertEquals(TransformStats.State.STOPPED, getTransformStats(config.getId()).getTransformsStats().get(0).getState()); - }); + assertBusy( + () -> { assertEquals(TransformStats.State.STOPPED, getTransformStats(config.getId()).getTransformsStats().get(0).getState()); } + ); try (RestHighLevelClient restClient = new TestRestHighLevelClient()) { restClient.indices().refresh(new RefreshRequest(destIndexName), RequestOptions.DEFAULT); // Verify destination index mappings - GetMappingsResponse destIndexMapping = - restClient.indices().getMapping(new GetMappingsRequest().indices(destIndexName), RequestOptions.DEFAULT); + GetMappingsResponse destIndexMapping = restClient.indices() + .getMapping(new GetMappingsRequest().indices(destIndexName), RequestOptions.DEFAULT); assertThat(destIndexMapping.mappings().get(destIndexName).sourceAsMap(), allOf(hasKey("_meta"), hasKey("properties"))); // Verify destination index contents - SearchResponse searchResponse = - restClient.search(new SearchRequest(destIndexName).source(new SearchSourceBuilder().size(1000)), RequestOptions.DEFAULT); + SearchResponse searchResponse = restClient.search( + new SearchRequest(destIndexName).source(new SearchSourceBuilder().size(1000)), + RequestOptions.DEFAULT + ); assertThat(searchResponse.getHits().getTotalHits().value, is(equalTo(Long.valueOf(NUM_USERS)))); assertThat( Stream.of(searchResponse.getHits().getHits()).map(SearchHit::getSourceAsMap).collect(toList()), - is(equalTo(previewResponse.getDocs()))); + is(equalTo(previewResponse.getDocs())) + ); } } public void testPivotTransform_BadRuntimeFieldScript() throws Exception { String destIndexName = "reviews-by-user-pivot"; String transformId = "transform-with-st-rt-fields-pivot"; - Map runtimeMappings = new HashMap<>() {{ - put("user-upper", new HashMap<>() {{ - put("type", "keyword"); - // Method name used in the script is misspelled, i.e.: "toUperCase" instead of "toUpperCase" - put("script", singletonMap("source", "if (params._source.user_id != null) {emit(params._source.user_id.toUperCase())}")); - }}); - }}; + Map runtimeMappings = new HashMap<>() { + { + put("user-upper", new HashMap<>() { + { + put("type", "keyword"); + // Method name used in the script is misspelled, i.e.: "toUperCase" instead of "toUpperCase" + put( + "script", + singletonMap("source", "if (params._source.user_id != null) {emit(params._source.user_id.toUperCase())}") + ); + } + }); + } + }; Map groups = singletonMap("by-user", TermsGroupSource.builder().setField("user-upper").build()); AggregatorFactories.Builder aggs = AggregatorFactories.builder() @@ -196,25 +220,25 @@ public void testPivotTransform_BadRuntimeFieldScript() throws Exception { .addAggregator(AggregationBuilders.avg("review_score_rt").field("stars-x2")) .addAggregator(AggregationBuilders.max("timestamp").field("timestamp")) .addAggregator(AggregationBuilders.max("timestamp_rt").field("timestamp-5m")); - TransformConfig config = - createTransformConfigBuilder(transformId, destIndexName, QueryBuilders.matchAllQuery(), "dummy") - .setSource(SourceConfig.builder() - .setIndex(REVIEWS_INDEX_NAME) - .setQuery(QueryBuilders.matchAllQuery()) - .setRuntimeMappings(runtimeMappings) - .build()) - .setPivotConfig(createPivotConfig(groups, aggs)) - .build(); + TransformConfig config = createTransformConfigBuilder(transformId, destIndexName, QueryBuilders.matchAllQuery(), "dummy").setSource( + SourceConfig.builder() + .setIndex(REVIEWS_INDEX_NAME) + .setQuery(QueryBuilders.matchAllQuery()) + .setRuntimeMappings(runtimeMappings) + .build() + ).setPivotConfig(createPivotConfig(groups, aggs)).build(); Exception e = expectThrows(Exception.class, () -> previewTransform(config, RequestOptions.DEFAULT)); assertThat( ExceptionsHelper.stackTrace(e), - allOf(containsString("script_exception"), containsString("dynamic method [java.lang.String, toUperCase/0] not found"))); + allOf(containsString("script_exception"), containsString("dynamic method [java.lang.String, toUperCase/0] not found")) + ); e = expectThrows(Exception.class, () -> putTransform(config, RequestOptions.DEFAULT)); assertThat( ExceptionsHelper.stackTrace(e), - allOf(containsString("script_exception"), containsString("dynamic method [java.lang.String, toUperCase/0] not found"))); + allOf(containsString("script_exception"), containsString("dynamic method [java.lang.String, toUperCase/0] not found")) + ); } public void testLatestTransform() throws Exception { @@ -222,41 +246,36 @@ public void testLatestTransform() throws Exception { String transformId = "transform-with-st-rt-fields-latest"; Map runtimeMappings = createRuntimeMappings(); - SourceConfig sourceConfig = - SourceConfig.builder() - .setIndex(REVIEWS_INDEX_NAME) - .setQuery(QueryBuilders.matchAllQuery()) - .setRuntimeMappings(runtimeMappings) - .build(); - TransformConfig configWithOrdinaryFields = - createTransformConfigBuilder(transformId, destIndexName, QueryBuilders.matchAllQuery(), "dummy") - .setSource(sourceConfig) - .setLatestConfig(LatestConfig.builder() - .setUniqueKey("user_id") - .setSort("timestamp") - .build()) - .build(); + SourceConfig sourceConfig = SourceConfig.builder() + .setIndex(REVIEWS_INDEX_NAME) + .setQuery(QueryBuilders.matchAllQuery()) + .setRuntimeMappings(runtimeMappings) + .build(); + TransformConfig configWithOrdinaryFields = createTransformConfigBuilder( + transformId, + destIndexName, + QueryBuilders.matchAllQuery(), + "dummy" + ).setSource(sourceConfig).setLatestConfig(LatestConfig.builder().setUniqueKey("user_id").setSort("timestamp").build()).build(); PreviewTransformResponse previewWithOrdinaryFields = previewTransform(configWithOrdinaryFields, RequestOptions.DEFAULT); // Verify preview mappings assertThat(previewWithOrdinaryFields.getMappings(), allOf(hasKey("_meta"), hasKey("properties"))); // Verify preview contents assertThat("Got preview: " + previewWithOrdinaryFields, previewWithOrdinaryFields.getDocs(), hasSize(NUM_USERS)); - previewWithOrdinaryFields.getDocs().forEach( - doc -> { - assertThat(doc, hasKey("user_id")); - assertThat(doc, not(hasKey("user-upper"))); - } - ); + previewWithOrdinaryFields.getDocs().forEach(doc -> { + assertThat(doc, hasKey("user_id")); + assertThat(doc, not(hasKey("user-upper"))); + }); - TransformConfig configWithRuntimeFields = - createTransformConfigBuilder(transformId, destIndexName, QueryBuilders.matchAllQuery(), "dummy") - .setSource(sourceConfig) - .setLatestConfig(LatestConfig.builder() - .setUniqueKey("user-upper") - .setSort("timestamp-5m") - .build()) - .build(); + TransformConfig configWithRuntimeFields = createTransformConfigBuilder( + transformId, + destIndexName, + QueryBuilders.matchAllQuery(), + "dummy" + ).setSource(sourceConfig) + .setLatestConfig(LatestConfig.builder().setUniqueKey("user-upper").setSort("timestamp-5m").build()) + .build(); PreviewTransformResponse previewWithRuntimeFields = previewTransform(configWithRuntimeFields, RequestOptions.DEFAULT); assertThat(previewWithRuntimeFields.getDocs(), is(equalTo(previewWithOrdinaryFields.getDocs()))); @@ -267,63 +286,75 @@ public void testLatestTransform() throws Exception { waitUntilCheckpoint(configWithRuntimeFields.getId(), 1L); stopTransform(configWithRuntimeFields.getId()); - assertBusy(() -> { - assertEquals( - TransformStats.State.STOPPED, - getTransformStats(configWithRuntimeFields.getId()).getTransformsStats().get(0).getState()); - }); + assertBusy( + () -> { + assertEquals( + TransformStats.State.STOPPED, + getTransformStats(configWithRuntimeFields.getId()).getTransformsStats().get(0).getState() + ); + } + ); try (RestHighLevelClient restClient = new TestRestHighLevelClient()) { restClient.indices().refresh(new RefreshRequest(destIndexName), RequestOptions.DEFAULT); // Verify destination index mappings - GetMappingsResponse destIndexMapping = - restClient.indices().getMapping(new GetMappingsRequest().indices(destIndexName), RequestOptions.DEFAULT); + GetMappingsResponse destIndexMapping = restClient.indices() + .getMapping(new GetMappingsRequest().indices(destIndexName), RequestOptions.DEFAULT); assertThat(destIndexMapping.mappings().get(destIndexName).sourceAsMap(), allOf(hasKey("_meta"), hasKey("properties"))); // Verify destination index contents - SearchResponse searchResponse = - restClient.search(new SearchRequest(destIndexName).source(new SearchSourceBuilder().size(1000)), RequestOptions.DEFAULT); + SearchResponse searchResponse = restClient.search( + new SearchRequest(destIndexName).source(new SearchSourceBuilder().size(1000)), + RequestOptions.DEFAULT + ); assertThat(searchResponse.getHits().getTotalHits().value, is(equalTo(Long.valueOf(NUM_USERS)))); assertThat( Stream.of(searchResponse.getHits().getHits()).map(SearchHit::getSourceAsMap).collect(toList()), - is(equalTo(previewWithOrdinaryFields.getDocs()))); + is(equalTo(previewWithOrdinaryFields.getDocs())) + ); } } public void testLatestTransform_BadRuntimeFieldScript() throws Exception { String destIndexName = "reviews-by-user-latest"; String transformId = "transform-with-st-rt-fields-latest"; - Map runtimeMappings = new HashMap<>() {{ - put("user-upper", new HashMap<>() {{ - put("type", "keyword"); - // Method name used in the script is misspelled, i.e.: "toUperCase" instead of "toUpperCase" - put("script", singletonMap("source", "if (params._source.user_id != null) {emit(params._source.user_id.toUperCase())}")); - }}); - }}; - - SourceConfig sourceConfig = - SourceConfig.builder() - .setIndex(REVIEWS_INDEX_NAME) - .setQuery(QueryBuilders.matchAllQuery()) - .setRuntimeMappings(runtimeMappings) - .build(); - TransformConfig configWithRuntimeFields = - createTransformConfigBuilder(transformId, destIndexName, QueryBuilders.matchAllQuery(), "dummy") - .setSource(sourceConfig) - .setLatestConfig(LatestConfig.builder() - .setUniqueKey("user-upper") - .setSort("timestamp") - .build()) - .build(); + Map runtimeMappings = new HashMap<>() { + { + put("user-upper", new HashMap<>() { + { + put("type", "keyword"); + // Method name used in the script is misspelled, i.e.: "toUperCase" instead of "toUpperCase" + put( + "script", + singletonMap("source", "if (params._source.user_id != null) {emit(params._source.user_id.toUperCase())}") + ); + } + }); + } + }; + + SourceConfig sourceConfig = SourceConfig.builder() + .setIndex(REVIEWS_INDEX_NAME) + .setQuery(QueryBuilders.matchAllQuery()) + .setRuntimeMappings(runtimeMappings) + .build(); + TransformConfig configWithRuntimeFields = createTransformConfigBuilder( + transformId, + destIndexName, + QueryBuilders.matchAllQuery(), + "dummy" + ).setSource(sourceConfig).setLatestConfig(LatestConfig.builder().setUniqueKey("user-upper").setSort("timestamp").build()).build(); Exception e = expectThrows(Exception.class, () -> previewTransform(configWithRuntimeFields, RequestOptions.DEFAULT)); assertThat( ExceptionsHelper.stackTrace(e), - allOf(containsString("script_exception"), containsString("dynamic method [java.lang.String, toUperCase/0] not found"))); + allOf(containsString("script_exception"), containsString("dynamic method [java.lang.String, toUperCase/0] not found")) + ); e = expectThrows(Exception.class, () -> putTransform(configWithRuntimeFields, RequestOptions.DEFAULT)); assertThat( ExceptionsHelper.stackTrace(e), - allOf(containsString("script_exception"), containsString("dynamic method [java.lang.String, toUperCase/0] not found"))); + allOf(containsString("script_exception"), containsString("dynamic method [java.lang.String, toUperCase/0] not found")) + ); } private static IsUpperCaseMatcher isUpperCase() { diff --git a/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/continuous/ContinuousTestCase.java b/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/continuous/ContinuousTestCase.java index ad7a0aa41ea50..f33069af35b90 100644 --- a/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/continuous/ContinuousTestCase.java +++ b/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/continuous/ContinuousTestCase.java @@ -17,12 +17,12 @@ import org.elasticsearch.client.transform.transforms.TransformConfig; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.core.TimeValue; import org.elasticsearch.search.SearchModule; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.AggregatorFactories; import org.elasticsearch.test.rest.ESRestTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; import java.io.IOException; import java.nio.charset.StandardCharsets; diff --git a/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/continuous/DateHistogramGroupByOtherTimeFieldIT.java b/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/continuous/DateHistogramGroupByOtherTimeFieldIT.java index cf71db1962d10..457f5fbb7a70a 100644 --- a/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/continuous/DateHistogramGroupByOtherTimeFieldIT.java +++ b/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/continuous/DateHistogramGroupByOtherTimeFieldIT.java @@ -2,7 +2,6 @@ import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; -import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.client.transform.transforms.DestConfig; import org.elasticsearch.client.transform.transforms.SettingsConfig; import org.elasticsearch.client.transform.transforms.SourceConfig; diff --git a/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/continuous/LatestContinuousIT.java b/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/continuous/LatestContinuousIT.java index 281e56a244566..059062e9f0eb2 100644 --- a/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/continuous/LatestContinuousIT.java +++ b/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/continuous/LatestContinuousIT.java @@ -40,13 +40,16 @@ public class LatestContinuousIT extends ContinuousTestCase { private static final String NAME = "continuous-latest-test"; - private static final Map RUNTIME_MAPPINGS = - new HashMap() {{ - put("event-upper-at-search", new HashMap() {{ - put("type", "keyword"); - put("script", singletonMap("source", "if (params._source.event != null) {emit(params._source.event.toUpperCase())}")); - }}); - }}; + private static final Map RUNTIME_MAPPINGS = new HashMap() { + { + put("event-upper-at-search", new HashMap() { + { + put("type", "keyword"); + put("script", singletonMap("source", "if (params._source.event != null) {emit(params._source.event.toUpperCase())}")); + } + }); + } + }; private static final String MISSING_BUCKET_KEY = "~~NULL~~"; // ensure that this key is last after sorting private final String eventField; @@ -59,20 +62,10 @@ public LatestContinuousIT() { @Override public TransformConfig createConfig() { - TransformConfig.Builder transformConfigBuilder = - new TransformConfig.Builder() - .setId(NAME) - .setSource( - SourceConfig.builder() - .setIndex(CONTINUOUS_EVENTS_SOURCE_INDEX) - .setRuntimeMappings(RUNTIME_MAPPINGS) - .build()) - .setDest(new DestConfig(NAME, INGEST_PIPELINE)) - .setLatestConfig( - LatestConfig.builder() - .setUniqueKey(eventField) - .setSort(timestampField) - .build()); + TransformConfig.Builder transformConfigBuilder = new TransformConfig.Builder().setId(NAME) + .setSource(SourceConfig.builder().setIndex(CONTINUOUS_EVENTS_SOURCE_INDEX).setRuntimeMappings(RUNTIME_MAPPINGS).build()) + .setDest(new DestConfig(NAME, INGEST_PIPELINE)) + .setLatestConfig(LatestConfig.builder().setUniqueKey(eventField).setSort(timestampField).build()); addCommonBuilderParameters(transformConfigBuilder); return transformConfigBuilder.build(); } @@ -84,30 +77,27 @@ public String getName() { @Override public void testIteration(int iteration, Set modifiedEvents) throws IOException { - SearchRequest searchRequestSource = - new SearchRequest(CONTINUOUS_EVENTS_SOURCE_INDEX) - .allowPartialSearchResults(false) - .source( - new SearchSourceBuilder() - // runtime mappings are needed in case "event-upper-at-search" is selected as the event field in test constructor - .runtimeMappings(RUNTIME_MAPPINGS) - .size(0) - .aggregation( - new TermsAggregationBuilder("by_event") - .size(1000) - .field(eventField) - .missing(MISSING_BUCKET_KEY) - .order(BucketOrder.key(true)) - .subAggregation(AggregationBuilders.max("max_timestamp").field(timestampField)))); + SearchRequest searchRequestSource = new SearchRequest(CONTINUOUS_EVENTS_SOURCE_INDEX).allowPartialSearchResults(false) + .source( + new SearchSourceBuilder() + // runtime mappings are needed in case "event-upper-at-search" is selected as the event field in test constructor + .runtimeMappings(RUNTIME_MAPPINGS) + .size(0) + .aggregation( + new TermsAggregationBuilder("by_event").size(1000) + .field(eventField) + .missing(MISSING_BUCKET_KEY) + .order(BucketOrder.key(true)) + .subAggregation(AggregationBuilders.max("max_timestamp").field(timestampField)) + ) + ); SearchResponse searchResponseSource = search(searchRequestSource); - SearchRequest searchRequestDest = - new SearchRequest(NAME) - .allowPartialSearchResults(false) - // In destination index we don't have access to runtime fields from source index, let's use what we have i.e.: event.keyword - // and assume the sorting order will be the same (it is true as the runtime field applies "toUpperCase()" which preserves - // sorting order) - .source(new SearchSourceBuilder().size(1000).sort("event.keyword")); + SearchRequest searchRequestDest = new SearchRequest(NAME).allowPartialSearchResults(false) + // In destination index we don't have access to runtime fields from source index, let's use what we have i.e.: event.keyword + // and assume the sorting order will be the same (it is true as the runtime field applies "toUpperCase()" which preserves + // sorting order) + .source(new SearchSourceBuilder().size(1000).sort("event.keyword")); SearchResponse searchResponseDest = search(searchRequestDest); List buckets = ((Terms) searchResponseSource.getAggregations().get("by_event")).getBuckets(); @@ -116,7 +106,10 @@ public void testIteration(int iteration, Set modifiedEvents) throws IOEx assertThat( new ParameterizedMessage( "Number of buckets did not match, source: {}, expected: {}, iteration: {}", - searchResponseDest.getHits().getTotalHits().value, Long.valueOf(buckets.size()), iteration).getFormattedMessage(), + searchResponseDest.getHits().getTotalHits().value, + Long.valueOf(buckets.size()), + iteration + ).getFormattedMessage(), searchResponseDest.getHits().getTotalHits().value, is(equalTo(Long.valueOf(buckets.size()))) ); @@ -140,7 +133,10 @@ public void testIteration(int iteration, Set modifiedEvents) throws IOEx assertThat( new ParameterizedMessage( "Buckets did not match, source: {}, expected: {}, iteration: {}", - source, bucket.getKey(), iteration).getFormattedMessage(), + source, + bucket.getKey(), + iteration + ).getFormattedMessage(), transformBucketKey, is(equalTo(bucket.getKey())) ); @@ -150,7 +146,10 @@ public void testIteration(int iteration, Set modifiedEvents) throws IOEx assertThat( new ParameterizedMessage( "Timestamps did not match, source: {}, expected: {}, iteration: {}", - source, maxTimestampValueAsString, iteration).getFormattedMessage(), + source, + maxTimestampValueAsString, + iteration + ).getFormattedMessage(), timestampFieldValue.substring(0, timestampFieldValue.lastIndexOf('.') + 3), is(equalTo(maxTimestampValueAsString.substring(0, maxTimestampValueAsString.lastIndexOf('.') + 3))) ); diff --git a/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/continuous/TransformContinuousIT.java b/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/continuous/TransformContinuousIT.java index 580ff5b26e3f0..65ce62f35dd22 100644 --- a/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/continuous/TransformContinuousIT.java +++ b/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/continuous/TransformContinuousIT.java @@ -36,14 +36,14 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.time.DateFormatter; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.common.xcontent.support.XContentMapValues; import org.elasticsearch.core.TimeValue; import org.elasticsearch.core.Tuple; import org.elasticsearch.search.SearchModule; import org.elasticsearch.test.rest.ESRestTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; import org.junit.After; import org.junit.Before; @@ -54,7 +54,6 @@ import java.text.DecimalFormatSymbols; import java.time.Instant; import java.time.ZoneId; -import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Base64; import java.util.Collections; @@ -194,8 +193,7 @@ public void testContinousEvents() throws Exception { for (int i = 0; i < 100; i++) { dates.add( // create a random date between 1/1/2001 and 1/1/2006 - formatTimestmap(dateType) - .format(Instant.ofEpochMilli(randomLongBetween(978307200000L, 1136073600000L))) + formatTimestmap(dateType).format(Instant.ofEpochMilli(randomLongBetween(978307200000L, 1136073600000L))) ); } @@ -249,17 +247,17 @@ public void testContinousEvents() throws Exception { } // simulate a different timestamp that is off from the timestamp used for sync, so it can fall into the previous bucket - String metricDateString = formatTimestmap(dateType) - .format(runDate.minusSeconds(randomIntBetween(0, 2)).plusNanos(randomIntBetween(0, 999999))); + String metricDateString = formatTimestmap(dateType).format( + runDate.minusSeconds(randomIntBetween(0, 2)).plusNanos(randomIntBetween(0, 999999)) + ); source.append("\"metric-timestamp\":\"").append(metricDateString).append("\","); final Instant timestamp = runDate.plusNanos(randomIntBetween(0, 999999)); - String dateString = formatTimestmap(dateType) - .format(timestamp); + String dateString = formatTimestmap(dateType).format(timestamp); source.append("\"timestamp\":\"").append(dateString).append("\","); // for data streams - //dynamic field results in a date type + // dynamic field results in a date type source.append("\"@timestamp\":\"").append(formatTimestmap("date").format(timestamp)).append("\","); source.append("\"run\":").append(run); source.append("}"); @@ -304,7 +302,7 @@ public void testContinousEvents() throws Exception { } private DateFormatter formatTimestmap(String dateType) { - if(dateType == "date_nanos"){ + if (dateType == "date_nanos") { return DateFormatter.forPattern("strict_date_optional_time_nanos").withZone(ZoneId.of("UTC")); } else { return DateFormatter.forPattern("strict_date_optional_time").withZone(ZoneId.of("UTC")); diff --git a/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformAuditorIT.java b/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformAuditorIT.java index e6c4ff1d3a684..acbd8cbd74726 100644 --- a/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformAuditorIT.java +++ b/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformAuditorIT.java @@ -30,8 +30,10 @@ public class TransformAuditorIT extends TransformRestTestCase { private static final String TEST_USER_NAME = "transform_admin_plus_data"; private static final String DATA_ACCESS_ROLE = "test_data_access"; - private static final String BASIC_AUTH_VALUE_TRANSFORM_ADMIN_WITH_SOME_DATA_ACCESS = - basicAuthHeaderValue(TEST_USER_NAME, TEST_PASSWORD_SECURE_STRING); + private static final String BASIC_AUTH_VALUE_TRANSFORM_ADMIN_WITH_SOME_DATA_ACCESS = basicAuthHeaderValue( + TEST_USER_NAME, + TEST_PASSWORD_SECURE_STRING + ); private static boolean indicesCreated = false; @@ -78,10 +80,10 @@ public void testAuditorWritesAudits() throws Exception { assertBusy(() -> { refreshIndex(TransformInternalIndexConstants.AUDIT_INDEX); Map response = entityAsMap(client().performRequest(request)); - List hitList = ((List) ((Map)response.get("hits")).get("hits")); + List hitList = ((List) ((Map) response.get("hits")).get("hits")); assertThat(hitList, is(not(empty()))); Map hitRsp = (Map) hitList.get(0); - Map source = (Map)hitRsp.get("_source"); + Map source = (Map) hitRsp.get("_source"); assertThat(source.get("transform_id"), equalTo(transformId)); assertThat(source.get("level"), equalTo("info")); assertThat(source.get("message"), is(notNullValue())); @@ -93,22 +95,27 @@ public void testAuditorWritesAudits() throws Exception { public void testAliasCreatedforBWCIndexes() throws Exception { Settings.Builder settings = Settings.builder() - .put(IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), 1) - .put(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 0); + .put(IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), 1) + .put(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 0); // These indices should only exist if created in previous versions, ignore the deprecation warning for this test - RequestOptions options = expectWarnings("index name [" + TransformInternalIndexConstants.AUDIT_INDEX_DEPRECATED + "] starts " + - "with a dot '.', in the next major version, index names starting with a dot are reserved for hidden indices " + - "and system indices"); + RequestOptions options = expectWarnings( + "index name [" + + TransformInternalIndexConstants.AUDIT_INDEX_DEPRECATED + + "] starts " + + "with a dot '.', in the next major version, index names starting with a dot are reserved for hidden indices " + + "and system indices" + ); Request request = new Request("PUT", "/" + TransformInternalIndexConstants.AUDIT_INDEX_DEPRECATED); String entity = "{\"settings\": " + Strings.toString(settings.build()) + "}"; request.setJsonEntity(entity); request.setOptions(options); client().performRequest(request); - assertBusy(() -> - assertTrue(aliasExists(TransformInternalIndexConstants.AUDIT_INDEX_DEPRECATED, - TransformInternalIndexConstants.AUDIT_INDEX_READ_ALIAS)) + assertBusy( + () -> assertTrue( + aliasExists(TransformInternalIndexConstants.AUDIT_INDEX_DEPRECATED, TransformInternalIndexConstants.AUDIT_INDEX_READ_ALIAS) + ) ); } } diff --git a/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformConfigurationIndexIT.java b/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformConfigurationIndexIT.java index 9d45b5f394823..b2a78b1840d99 100644 --- a/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformConfigurationIndexIT.java +++ b/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformConfigurationIndexIT.java @@ -14,8 +14,8 @@ import org.elasticsearch.client.Response; import org.elasticsearch.client.ResponseException; import org.elasticsearch.common.Strings; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.support.XContentMapValues; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.transform.TransformField; import org.elasticsearch.xpack.core.transform.transforms.TransformConfig; import org.elasticsearch.xpack.core.transform.transforms.persistence.TransformInternalIndexConstants; @@ -35,9 +35,12 @@ public class TransformConfigurationIndexIT extends TransformRestTestCase { */ public void testDeleteConfigurationLeftOver() throws IOException { String fakeTransformName = randomAlphaOfLengthBetween(5, 20); - final RequestOptions expectWarningOptions = expectWarnings("this request accesses system indices: [" + - TransformInternalIndexConstants.LATEST_INDEX_NAME + "], but in a future major version, direct access to system indices will " + - "be prevented by default"); + final RequestOptions expectWarningOptions = expectWarnings( + "this request accesses system indices: [" + + TransformInternalIndexConstants.LATEST_INDEX_NAME + + "], but in a future major version, direct access to system indices will " + + "be prevented by default" + ); try (XContentBuilder builder = jsonBuilder()) { builder.startObject(); @@ -46,8 +49,10 @@ public void testDeleteConfigurationLeftOver() throws IOException { } builder.endObject(); final StringEntity entity = new StringEntity(Strings.toString(builder), ContentType.APPLICATION_JSON); - Request req = new Request("PUT", - TransformInternalIndexConstants.LATEST_INDEX_NAME + "/_doc/" + TransformConfig.documentId(fakeTransformName)); + Request req = new Request( + "PUT", + TransformInternalIndexConstants.LATEST_INDEX_NAME + "/_doc/" + TransformConfig.documentId(fakeTransformName) + ); req.setOptions(expectWarningOptions); req.setEntity(entity); client().performRequest(req); @@ -61,9 +66,9 @@ public void testDeleteConfigurationLeftOver() throws IOException { Request deleteRequest = new Request("DELETE", getTransformEndpoint() + fakeTransformName); Response deleteResponse = client().performRequest(deleteRequest); assertOK(deleteResponse); - assertTrue((boolean)XContentMapValues.extractValue("acknowledged", entityAsMap(deleteResponse))); + assertTrue((boolean) XContentMapValues.extractValue("acknowledged", entityAsMap(deleteResponse))); // delete again, should fail - expectThrows(ResponseException.class,() -> client().performRequest(deleteRequest)); + expectThrows(ResponseException.class, () -> client().performRequest(deleteRequest)); } } diff --git a/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformGetAndGetStatsIT.java b/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformGetAndGetStatsIT.java index 35e1850822f02..1112c80801ecc 100644 --- a/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformGetAndGetStatsIT.java +++ b/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformGetAndGetStatsIT.java @@ -28,13 +28,13 @@ import org.elasticsearch.client.transform.transforms.pivot.PivotConfig; import org.elasticsearch.client.transform.transforms.pivot.TermsGroupSource; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.common.xcontent.support.XContentMapValues; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchModule; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.AggregatorFactories; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.transform.TransformField; import org.junit.After; import org.junit.Before; diff --git a/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformMetadataIT.java b/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformMetadataIT.java index 4ce1bab5c620e..1da5a5c20bf8f 100644 --- a/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformMetadataIT.java +++ b/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformMetadataIT.java @@ -46,16 +46,22 @@ public void testMetadata() throws Exception { Response mappingResponse = client().performRequest(new Request("GET", "pivot_reviews/_mapping")); Map mappingAsMap = entityAsMap(mappingResponse); - assertEquals(Version.CURRENT.toString(), - XContentMapValues.extractValue("pivot_reviews.mappings._meta._transform.version.created", mappingAsMap)); - assertTrue((Long) XContentMapValues.extractValue("pivot_reviews.mappings._meta._transform.creation_date_in_millis", - mappingAsMap) < System.currentTimeMillis()); - assertTrue((Long) XContentMapValues.extractValue("pivot_reviews.mappings._meta._transform.creation_date_in_millis", - mappingAsMap) > testStarted); - assertEquals("test_meta", - XContentMapValues.extractValue("pivot_reviews.mappings._meta._transform.transform", mappingAsMap)); - assertEquals("transform", - XContentMapValues.extractValue("pivot_reviews.mappings._meta.created_by", mappingAsMap)); + assertEquals( + Version.CURRENT.toString(), + XContentMapValues.extractValue("pivot_reviews.mappings._meta._transform.version.created", mappingAsMap) + ); + assertTrue( + (Long) XContentMapValues.extractValue("pivot_reviews.mappings._meta._transform.creation_date_in_millis", mappingAsMap) < System + .currentTimeMillis() + ); + assertTrue( + (Long) XContentMapValues.extractValue( + "pivot_reviews.mappings._meta._transform.creation_date_in_millis", + mappingAsMap + ) > testStarted + ); + assertEquals("test_meta", XContentMapValues.extractValue("pivot_reviews.mappings._meta._transform.transform", mappingAsMap)); + assertEquals("transform", XContentMapValues.extractValue("pivot_reviews.mappings._meta.created_by", mappingAsMap)); } } diff --git a/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformPivotRestIT.java b/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformPivotRestIT.java index 53a9f6ae738dd..9485a6b851489 100644 --- a/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformPivotRestIT.java +++ b/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformPivotRestIT.java @@ -13,8 +13,8 @@ import org.elasticsearch.client.Response; import org.elasticsearch.client.ResponseException; import org.elasticsearch.common.Strings; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.support.XContentMapValues; +import org.elasticsearch.xcontent.XContentBuilder; import org.junit.Before; import java.io.IOException; @@ -1133,13 +1133,7 @@ public void testPivotWithGeoBoundsAgg() throws Exception { BASIC_AUTH_VALUE_TRANSFORM_ADMIN_WITH_SOME_DATA_ACCESS ); - String config = "{" - + " \"source\": {\"index\":\"" - + indexName - + "\"}," - + " \"dest\": {\"index\":\"" - + transformIndex - + "\"},"; + String config = "{" + " \"source\": {\"index\":\"" + indexName + "\"}," + " \"dest\": {\"index\":\"" + transformIndex + "\"},"; config += " \"pivot\": {" + " \"group_by\": {" @@ -1454,7 +1448,9 @@ public void testPivotWithTopMetrics() throws Exception { Map searchResult = getAsMap(transformIndex + "/_search?q=reviewer:user_4"); assertEquals(1, XContentMapValues.extractValue("hits.total.value", searchResult)); - String actual = (String) ((List) XContentMapValues.extractValue("hits.hits._source.top_business.business_id", searchResult)).get(0); + String actual = (String) ((List) XContentMapValues.extractValue("hits.hits._source.top_business.business_id", searchResult)).get( + 0 + ); assertEquals("business_9", actual); searchResult = getAsMap(transformIndex + "/_search?q=reviewer:user_1"); @@ -1813,11 +1809,10 @@ public void testExportAndImport() throws Exception { createPivotReviewsTransform(transformId, transformIndex, null, null, BASIC_AUTH_VALUE_TRANSFORM_ADMIN_WITH_SOME_DATA_ACCESS); - Response response = adminClient().performRequest(new Request("GET", - getTransformEndpoint() + transformId + "?exclude_generated=true")); - Map storedConfig = ((List>) XContentMapValues.extractValue( - "transforms", - entityAsMap(response))) + Response response = adminClient().performRequest( + new Request("GET", getTransformEndpoint() + transformId + "?exclude_generated=true") + ); + Map storedConfig = ((List>) XContentMapValues.extractValue("transforms", entityAsMap(response))) .get(0); storedConfig.remove("id"); try (XContentBuilder builder = jsonBuilder()) { @@ -1827,11 +1822,10 @@ public void testExportAndImport() throws Exception { adminClient().performRequest(putTransform); } - response = adminClient().performRequest(new Request("GET", - getTransformEndpoint() + transformId + "-import" + "?exclude_generated=true")); - Map importConfig = ((List>) XContentMapValues.extractValue( - "transforms", - entityAsMap(response))) + response = adminClient().performRequest( + new Request("GET", getTransformEndpoint() + transformId + "-import" + "?exclude_generated=true") + ); + Map importConfig = ((List>) XContentMapValues.extractValue("transforms", entityAsMap(response))) .get(0); importConfig.remove("id"); assertThat(storedConfig, equalTo(importConfig)); diff --git a/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformPivotRestSpecialCasesIT.java b/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformPivotRestSpecialCasesIT.java index abd3acde38082..0f2a1eda23310 100644 --- a/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformPivotRestSpecialCasesIT.java +++ b/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformPivotRestSpecialCasesIT.java @@ -11,9 +11,9 @@ import org.apache.http.entity.StringEntity; import org.elasticsearch.client.Request; import org.elasticsearch.common.Strings; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.support.XContentMapValues; import org.elasticsearch.rest.action.admin.indices.RestPutIndexTemplateAction; +import org.elasticsearch.xcontent.XContentBuilder; import org.junit.Before; import java.io.IOException; diff --git a/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformProgressIT.java b/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformProgressIT.java index 7f3b1fbc85cbb..854d45d5e2517 100644 --- a/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformProgressIT.java +++ b/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformProgressIT.java @@ -22,14 +22,14 @@ import org.elasticsearch.client.indices.CreateIndexResponse; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.AggregatorFactories; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.test.rest.ESRestTestCase; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.transform.transforms.DestConfig; import org.elasticsearch.xpack.core.transform.transforms.SettingsConfig; import org.elasticsearch.xpack.core.transform.transforms.SourceConfig; diff --git a/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformRestTestCase.java b/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformRestTestCase.java index a5b2a4b5badbf..cb154c67965bc 100644 --- a/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformRestTestCase.java +++ b/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformRestTestCase.java @@ -18,9 +18,9 @@ import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.support.XContentMapValues; import org.elasticsearch.test.rest.ESRestTestCase; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.transform.TransformField; import org.elasticsearch.xpack.core.transform.transforms.persistence.TransformInternalIndexConstants; import org.junit.After; diff --git a/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformRobustnessIT.java b/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformRobustnessIT.java index 2032a5d7ee813..93bd26973771e 100644 --- a/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformRobustnessIT.java +++ b/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformRobustnessIT.java @@ -119,9 +119,14 @@ private int getNumberOfTransformTasks() throws IOException { private void beEvilAndDeleteTheTransformIndex() throws IOException { final Request deleteRequest = new Request("DELETE", TransformInternalIndexConstants.LATEST_INDEX_NAME); - deleteRequest.setOptions(expectWarnings("this request accesses system indices: [" + - TransformInternalIndexConstants.LATEST_INDEX_NAME + "], but in a future major version, direct access to system indices will " + - "be prevented by default")); + deleteRequest.setOptions( + expectWarnings( + "this request accesses system indices: [" + + TransformInternalIndexConstants.LATEST_INDEX_NAME + + "], but in a future major version, direct access to system indices will " + + "be prevented by default" + ) + ); adminClient().performRequest(deleteRequest); } } diff --git a/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformTaskFailedStateIT.java b/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformTaskFailedStateIT.java index adf24fd8ed6e7..8da3f271aa6c1 100644 --- a/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformTaskFailedStateIT.java +++ b/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformTaskFailedStateIT.java @@ -12,9 +12,9 @@ import org.elasticsearch.client.Request; import org.elasticsearch.client.ResponseException; import org.elasticsearch.common.Strings; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.support.XContentMapValues; import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.transform.transforms.TransformStats; import org.junit.After; import org.junit.Before; diff --git a/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformUsageIT.java b/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformUsageIT.java index 51c794901c93d..40c9ac686d027 100644 --- a/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformUsageIT.java +++ b/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformUsageIT.java @@ -69,9 +69,14 @@ public void testUsage() throws Exception { + ":" + TransformStoredDoc.NAME ); - statsExistsRequest.setOptions(expectWarnings("this request accesses system indices: [" + - TransformInternalIndexConstants.LATEST_INDEX_NAME + "], but in a future major version, direct access to system indices will " + - "be prevented by default")); + statsExistsRequest.setOptions( + expectWarnings( + "this request accesses system indices: [" + + TransformInternalIndexConstants.LATEST_INDEX_NAME + + "], but in a future major version, direct access to system indices will " + + "be prevented by default" + ) + ); // Verify that we have one stat document assertBusy(() -> { Map hasStatsMap = entityAsMap(client().performRequest(statsExistsRequest)); diff --git a/x-pack/plugin/transform/src/internalClusterTest/java/org/elasticsearch/xpack/transform/TransformSingleNodeTestCase.java b/x-pack/plugin/transform/src/internalClusterTest/java/org/elasticsearch/xpack/transform/TransformSingleNodeTestCase.java index e14a31449dd85..603b37d3e41f3 100644 --- a/x-pack/plugin/transform/src/internalClusterTest/java/org/elasticsearch/xpack/transform/TransformSingleNodeTestCase.java +++ b/x-pack/plugin/transform/src/internalClusterTest/java/org/elasticsearch/xpack/transform/TransformSingleNodeTestCase.java @@ -11,11 +11,11 @@ import org.elasticsearch.action.LatchedActionListener; import org.elasticsearch.action.admin.cluster.snapshots.features.ResetFeatureStateAction; import org.elasticsearch.action.admin.cluster.snapshots.features.ResetFeatureStateRequest; -import org.elasticsearch.core.CheckedConsumer; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.reindex.ReindexPlugin; +import org.elasticsearch.core.CheckedConsumer; import org.elasticsearch.node.NodeRoleSettings; import org.elasticsearch.plugins.Plugin; +import org.elasticsearch.reindex.ReindexPlugin; import org.elasticsearch.test.ESSingleNodeTestCase; import org.junit.After; @@ -43,8 +43,12 @@ public void cleanup() { client().execute(ResetFeatureStateAction.INSTANCE, new ResetFeatureStateRequest()).actionGet(); } - protected void assertAsync(Consumer> function, T expected, CheckedConsumer onAnswer, - Consumer onException) throws InterruptedException { + protected void assertAsync( + Consumer> function, + T expected, + CheckedConsumer onAnswer, + Consumer onException + ) throws InterruptedException { CountDownLatch latch = new CountDownLatch(1); diff --git a/x-pack/plugin/transform/src/internalClusterTest/java/org/elasticsearch/xpack/transform/integration/TransformInternalIndexIT.java b/x-pack/plugin/transform/src/internalClusterTest/java/org/elasticsearch/xpack/transform/integration/TransformInternalIndexIT.java index 1876f7507cf86..b9e21f4249740 100644 --- a/x-pack/plugin/transform/src/internalClusterTest/java/org/elasticsearch/xpack/transform/integration/TransformInternalIndexIT.java +++ b/x-pack/plugin/transform/src/internalClusterTest/java/org/elasticsearch/xpack/transform/integration/TransformInternalIndexIT.java @@ -33,7 +33,6 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; - public class TransformInternalIndexIT extends TransformSingleNodeTestCase { private static final String CURRENT_INDEX = TransformInternalIndexConstants.LATEST_INDEX_NAME; @@ -58,7 +57,8 @@ public void testUpdateDeletesOldTransformConfig() throws Exception { TransformInternalIndex.addTransformsConfigMappings(builder); builder.endObject(); builder.endObject(); - client().admin().indices() + client().admin() + .indices() .create(new CreateIndexRequest(OLD_INDEX).mapping(builder).origin(ClientHelper.TRANSFORM_ORIGIN)) .actionGet(); } @@ -66,8 +66,12 @@ public void testUpdateDeletesOldTransformConfig() throws Exception { createSourceIndex(transformIndex); String transformId = "transform-update-deletes-old-transform-config"; String config = "{\"dest\": {\"index\":\"bar\"}," - + " \"source\": {\"index\":\"" + transformIndex + "\", \"query\": {\"match_all\":{}}}," - + " \"id\": \""+transformId+"\"," + + " \"source\": {\"index\":\"" + + transformIndex + + "\", \"query\": {\"match_all\":{}}}," + + " \"id\": \"" + + transformId + + "\"," + " \"doc_type\": \"data_frame_transform_config\"," + " \"pivot\": {" + " \"group_by\": {" @@ -82,8 +86,7 @@ public void testUpdateDeletesOldTransformConfig() throws Exception { + " } } } }," + "\"frequency\":\"1s\"" + "}"; - IndexRequest indexRequest = new IndexRequest(OLD_INDEX) - .id(TransformConfig.documentId(transformId)) + IndexRequest indexRequest = new IndexRequest(OLD_INDEX).id(TransformConfig.documentId(transformId)) .source(config, XContentType.JSON) .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); IndexResponse indexResponse = client().index(indexRequest).actionGet(); @@ -95,9 +98,14 @@ public void testUpdateDeletesOldTransformConfig() throws Exception { UpdateTransformAction.Request updateTransformActionRequest = new UpdateTransformAction.Request( new TransformConfigUpdate(null, null, null, null, "updated", null, null, null), - transformId, false, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT); - UpdateTransformAction.Response updateTransformActionResponse = - client().execute(UpdateTransformAction.INSTANCE, updateTransformActionRequest).actionGet(); + transformId, + false, + AcknowledgedRequest.DEFAULT_ACK_TIMEOUT + ); + UpdateTransformAction.Response updateTransformActionResponse = client().execute( + UpdateTransformAction.INSTANCE, + updateTransformActionRequest + ).actionGet(); assertThat(updateTransformActionResponse.getConfig().getId(), equalTo(transformId)); assertThat(updateTransformActionResponse.getConfig().getDescription(), equalTo("updated")); diff --git a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/TransformClusterStateListener.java b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/TransformClusterStateListener.java index 3c57274646e4a..ee24f629f8da0 100644 --- a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/TransformClusterStateListener.java +++ b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/TransformClusterStateListener.java @@ -49,19 +49,17 @@ public void clusterChanged(ClusterChangedEvent event) { // The atomic flag prevents multiple simultaneous attempts to run alias creation // if there is a flurry of cluster state updates in quick succession if (event.localNodeMaster() && isIndexCreationInProgress.compareAndSet(false, true)) { - createAuditAliasForDataFrameBWC(event.state(), client, ActionListener.wrap( - r -> { - isIndexCreationInProgress.set(false); - if (r) { - logger.info("Created alias for deprecated data frame notifications index"); - } else { - logger.debug("Skipped creating alias for deprecated data frame notifications index"); - } - }, - e -> { - isIndexCreationInProgress.set(false); - logger.error("Error creating alias for deprecated data frame notifications index", e); - })); + createAuditAliasForDataFrameBWC(event.state(), client, ActionListener.wrap(r -> { + isIndexCreationInProgress.set(false); + if (r) { + logger.info("Created alias for deprecated data frame notifications index"); + } else { + logger.debug("Skipped creating alias for deprecated data frame notifications index"); + } + }, e -> { + isIndexCreationInProgress.set(false); + logger.error("Error creating alias for deprecated data frame notifications index", e); + })); } } @@ -74,22 +72,34 @@ private static void createAuditAliasForDataFrameBWC(ClusterState state, Client c } Metadata metadata = state.metadata(); - if (state.getMetadata().getIndicesLookup().get(TransformInternalIndexConstants.AUDIT_INDEX_DEPRECATED).getIndices().stream() - .anyMatch(name -> metadata.index(name).getAliases().containsKey(TransformInternalIndexConstants.AUDIT_INDEX_READ_ALIAS))) { + if (state.getMetadata() + .getIndicesLookup() + .get(TransformInternalIndexConstants.AUDIT_INDEX_DEPRECATED) + .getIndices() + .stream() + .anyMatch(name -> metadata.index(name).getAliases().containsKey(TransformInternalIndexConstants.AUDIT_INDEX_READ_ALIAS))) { finalListener.onResponse(false); return; } - final IndicesAliasesRequest request = client.admin().indices().prepareAliases() - .addAliasAction(IndicesAliasesRequest.AliasActions.add() + final IndicesAliasesRequest request = client.admin() + .indices() + .prepareAliases() + .addAliasAction( + IndicesAliasesRequest.AliasActions.add() .index(TransformInternalIndexConstants.AUDIT_INDEX_DEPRECATED) .alias(TransformInternalIndexConstants.AUDIT_INDEX_READ_ALIAS) - .isHidden(true)) - .request(); - - executeAsyncWithOrigin(client.threadPool().getThreadContext(), TRANSFORM_ORIGIN, request, - ActionListener.wrap(r -> finalListener.onResponse(r.isAcknowledged()), finalListener::onFailure), - client.admin().indices()::aliases); + .isHidden(true) + ) + .request(); + + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + TRANSFORM_ORIGIN, + request, + ActionListener.wrap(r -> finalListener.onResponse(r.isAcknowledged()), finalListener::onFailure), + client.admin().indices()::aliases + ); } } diff --git a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/TransformUsageTransportAction.java b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/TransformUsageTransportAction.java index 35863427d864d..2f6ab6629ea49 100644 --- a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/TransformUsageTransportAction.java +++ b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/TransformUsageTransportAction.java @@ -18,7 +18,6 @@ import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.persistent.PersistentTasksCustomMetadata; @@ -30,6 +29,7 @@ import org.elasticsearch.tasks.Task; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xpack.core.ClientHelper; import org.elasticsearch.xpack.core.action.XPackUsageFeatureAction; import org.elasticsearch.xpack.core.action.XPackUsageFeatureResponse; @@ -64,12 +64,10 @@ public class TransformUsageTransportAction extends XPackUsageFeatureTransportAct * Each feature corresponds to a field in {@link TransformConfig}. * If the field exists in the config then we assume the feature is used. */ - private static final String[] FEATURES = - Stream.concat( - Stream.of(TransformConfig.Function.values()).map(TransformConfig.Function::getParseField), - Stream.of(TransformField.RETENTION_POLICY, TransformField.SYNC)) - .map(ParseField::getPreferredName) - .toArray(String[]::new); + private static final String[] FEATURES = Stream.concat( + Stream.of(TransformConfig.Function.values()).map(TransformConfig.Function::getParseField), + Stream.of(TransformField.RETENTION_POLICY, TransformField.SYNC) + ).map(ParseField::getPreferredName).toArray(String[]::new); private final Client client; diff --git a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/action/TransformPrivilegeChecker.java b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/action/TransformPrivilegeChecker.java index 230c0030bedd8..53e49f7cea8aa 100644 --- a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/action/TransformPrivilegeChecker.java +++ b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/action/TransformPrivilegeChecker.java @@ -49,8 +49,13 @@ static void checkPrivileges( listener::onFailure ); - HasPrivilegesRequest hasPrivilegesRequest = - buildPrivilegesRequest(config, indexNameExpressionResolver, clusterState, username, checkDestIndexPrivileges); + HasPrivilegesRequest hasPrivilegesRequest = buildPrivilegesRequest( + config, + indexNameExpressionResolver, + clusterState, + username, + checkDestIndexPrivileges + ); client.execute(HasPrivilegesAction.INSTANCE, hasPrivilegesRequest, hasPrivilegesResponseListener); } @@ -72,8 +77,11 @@ private static HasPrivilegesRequest buildPrivilegesRequest( if (checkDestIndexPrivileges) { final String destIndex = config.getDestination().getIndex(); - final String[] concreteDest = - indexNameExpressionResolver.concreteIndexNames(clusterState, IndicesOptions.lenientExpandOpen(), destIndex); + final String[] concreteDest = indexNameExpressionResolver.concreteIndexNames( + clusterState, + IndicesOptions.lenientExpandOpen(), + destIndex + ); List destPrivileges = new ArrayList<>(3); destPrivileges.add("read"); diff --git a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/action/TransportDeleteTransformAction.java b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/action/TransportDeleteTransformAction.java index 693e408be3ea1..f8fea7b18f58a 100644 --- a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/action/TransportDeleteTransformAction.java +++ b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/action/TransportDeleteTransformAction.java @@ -75,8 +75,16 @@ protected TransportDeleteTransformAction( TransformServices transformServices, Client client ) { - super(name, transportService, clusterService, threadPool, actionFilters, Request::new, indexNameExpressionResolver, - ThreadPool.Names.SAME); + super( + name, + transportService, + clusterService, + threadPool, + actionFilters, + Request::new, + indexNameExpressionResolver, + ThreadPool.Names.SAME + ); this.transformConfigManager = transformServices.getConfigManager(); this.auditor = transformServices.getAuditor(); this.client = client; diff --git a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/action/TransportGetTransformAction.java b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/action/TransportGetTransformAction.java index 8975da66d6a04..9edcb09cfc496 100644 --- a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/action/TransportGetTransformAction.java +++ b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/action/TransportGetTransformAction.java @@ -13,16 +13,16 @@ import org.elasticsearch.client.Client; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.inject.Inject; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.tasks.Task; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ClientHelper; import org.elasticsearch.xpack.core.action.AbstractTransportGetResourcesAction; import org.elasticsearch.xpack.core.transform.TransformField; @@ -36,7 +36,6 @@ import static org.elasticsearch.xpack.core.transform.TransformField.INDEX_DOC_TYPE; - public class TransportGetTransformAction extends AbstractTransportGetResourcesAction { private final ClusterService clusterService; @@ -68,10 +67,7 @@ protected TransportGetTransformAction( protected void doExecute(Task task, Request request, ActionListener listener) { final ClusterState state = clusterService.state(); TransformNodes.warnIfNoTransformNodes(state); - searchResources(request, ActionListener.wrap( - r -> listener.onResponse(new Response(r.results(), r.count())), - listener::onFailure - )); + searchResources(request, ActionListener.wrap(r -> listener.onResponse(new Response(r.results(), r.count())), listener::onFailure)); } @Override @@ -81,7 +77,8 @@ protected ParseField getResultsField() { @Override protected String[] getIndices() { - return new String[] { TransformInternalIndexConstants.INDEX_NAME_PATTERN, + return new String[] { + TransformInternalIndexConstants.INDEX_NAME_PATTERN, TransformInternalIndexConstants.INDEX_NAME_PATTERN_DEPRECATED }; } @@ -92,8 +89,7 @@ protected TransformConfig parse(XContentParser parser) { @Override protected ResourceNotFoundException notFoundException(String resourceId) { - return new ResourceNotFoundException( - TransformMessages.getMessage(TransformMessages.REST_UNKNOWN_TRANSFORM, resourceId)); + return new ResourceNotFoundException(TransformMessages.getMessage(TransformMessages.REST_UNKNOWN_TRANSFORM, resourceId)); } @Override diff --git a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/action/TransportGetTransformStatsAction.java b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/action/TransportGetTransformStatsAction.java index 77fc51a17d2a7..ef5be29650f5c 100644 --- a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/action/TransportGetTransformStatsAction.java +++ b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/action/TransportGetTransformStatsAction.java @@ -19,10 +19,10 @@ import org.elasticsearch.client.Client; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.Nullable; import org.elasticsearch.index.IndexNotFoundException; import org.elasticsearch.persistent.PersistentTasksCustomMetadata; import org.elasticsearch.persistent.PersistentTasksCustomMetadata.Assignment; @@ -149,14 +149,25 @@ protected void doExecute(Task task, Request request, ActionListener fi ActionListener.wrap(hitsAndIds -> { boolean hasAnyTransformNode = TransformNodes.hasAnyTransformNode(clusterState.getNodes()); boolean requiresRemote = hitsAndIds.v2().v2().stream().anyMatch(config -> config.getSource().requiresRemoteCluster()); - if (hasAnyTransformNode && TransformNodes.redirectToAnotherNodeIfNeeded( - clusterState, nodeSettings, requiresRemote, transportService, actionName, request, Response::new, finalListener)) { + if (hasAnyTransformNode + && TransformNodes.redirectToAnotherNodeIfNeeded( + clusterState, + nodeSettings, + requiresRemote, + transportService, + actionName, + request, + Response::new, + finalListener + )) { return; } request.setExpandedIds(hitsAndIds.v2().v1()); - final TransformNodeAssignments transformNodeAssignments = - TransformNodes.transformTaskNodes(hitsAndIds.v2().v1(), clusterState); + final TransformNodeAssignments transformNodeAssignments = TransformNodes.transformTaskNodes( + hitsAndIds.v2().v1(), + clusterState + ); ActionListener doExecuteListener = ActionListener.wrap(response -> { PersistentTasksCustomMetadata tasksInProgress = clusterState.getMetadata().custom(PersistentTasksCustomMetadata.TYPE); @@ -189,16 +200,14 @@ protected void doExecute(Task task, Request request, ActionListener fi } else { doExecuteListener.onResponse(new Response(Collections.emptyList(), 0L)); } - }, - e -> { - // If the index to search, or the individual config is not there, just return empty - if (e instanceof ResourceNotFoundException) { - finalListener.onResponse(new Response(Collections.emptyList(), 0L)); - } else { - finalListener.onFailure(e); - } + }, e -> { + // If the index to search, or the individual config is not there, just return empty + if (e instanceof ResourceNotFoundException) { + finalListener.onResponse(new Response(Collections.emptyList(), 0L)); + } else { + finalListener.onFailure(e); } - ) + }) ); } diff --git a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/action/TransportPreviewTransformAction.java b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/action/TransportPreviewTransformAction.java index d5dc6012a479a..96f5ee810d20d 100644 --- a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/action/TransportPreviewTransformAction.java +++ b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/action/TransportPreviewTransformAction.java @@ -25,11 +25,7 @@ import org.elasticsearch.common.logging.DeprecationLogger; import org.elasticsearch.common.logging.HeaderWarning; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.common.xcontent.support.XContentMapValues; import org.elasticsearch.ingest.IngestService; import org.elasticsearch.license.License; @@ -38,6 +34,10 @@ import org.elasticsearch.tasks.Task; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.ClientHelper; import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.xpack.core.common.validation.SourceDestValidator; @@ -133,7 +133,8 @@ protected TransportPreviewTransformAction( transportService.getRemoteClusterService(), DiscoveryNode.isRemoteClusterClient(settings) /* transforms are BASIC so always allowed, no need to check license */ - ? new RemoteClusterLicenseChecker(client, mode -> true) : null, + ? new RemoteClusterLicenseChecker(client, mode -> true) + : null, ingestService, clusterService.getNodeName(), License.OperationMode.BASIC.description() @@ -149,7 +150,15 @@ protected void doExecute(Task task, Request request, ActionListener li if (clusterState.nodes().getMinNodeVersion().onOrAfter(Version.V_7_13_0)) { boolean requiresRemote = request.getConfig().getSource().requiresRemoteCluster(); if (TransformNodes.redirectToAnotherNodeIfNeeded( - clusterState, nodeSettings, requiresRemote, transportService, actionName, request, Response::new, listener)) { + clusterState, + nodeSettings, + requiresRemote, + transportService, + actionName, + request, + Response::new, + listener + )) { return; } } @@ -158,43 +167,35 @@ protected void doExecute(Task task, Request request, ActionListener li final Function function = FunctionFactory.create(config); // <4> Validate transform query - ActionListener validateConfigListener = ActionListener.wrap( - validateConfigResponse -> { - getPreview( - config.getId(), // note: @link{PreviewTransformAction} sets an id, so this is never null - function, - config.getSource(), - config.getDestination().getPipeline(), - config.getDestination().getIndex(), - config.getSyncConfig(), - listener - ); - }, - listener::onFailure - ); + ActionListener validateConfigListener = ActionListener.wrap(validateConfigResponse -> { + getPreview( + config.getId(), // note: @link{PreviewTransformAction} sets an id, so this is never null + function, + config.getSource(), + config.getDestination().getPipeline(), + config.getDestination().getIndex(), + config.getSyncConfig(), + listener + ); + }, listener::onFailure); // <3> Validate transform function config ActionListener validateSourceDestListener = ActionListener.wrap( - validateSourceDestResponse -> { - function.validateConfig(validateConfigListener); - }, + validateSourceDestResponse -> { function.validateConfig(validateConfigListener); }, listener::onFailure ); // <2> Validate source and destination indices - ActionListener checkPrivilegesListener = ActionListener.wrap( - aVoid -> { - sourceDestValidator.validate( - clusterState, - config.getSource().getIndex(), - config.getDestination().getIndex(), - config.getDestination().getPipeline(), - SourceDestValidations.getValidationsForPreview(config.getAdditionalSourceDestValidations()), - validateSourceDestListener - ); - }, - listener::onFailure - ); + ActionListener checkPrivilegesListener = ActionListener.wrap(aVoid -> { + sourceDestValidator.validate( + clusterState, + config.getSource().getIndex(), + config.getDestination().getIndex(), + config.getDestination().getPipeline(), + SourceDestValidations.getValidationsForPreview(config.getAdditionalSourceDestValidations()), + validateSourceDestListener + ); + }, listener::onFailure); // <1> Early check to verify that the user can create the destination index and can read from the source if (XPackSettings.SECURITY_ENABLED.get(nodeSettings)) { @@ -247,54 +248,48 @@ private void getPreview( listener.onResponse(new Response(docs, generatedDestIndexSettings)); }, listener::onFailure); - ActionListener>> previewListener = ActionListener.wrap( - docs -> { - if (pipeline == null) { - TransformDestIndexSettings generatedDestIndexSettings = TransformIndex.createTransformDestIndexSettings( - mappings.get(), - transformId, - Clock.systemUTC() - ); - List warnings = TransformConfigLinter.getWarnings(function, source, syncConfig); - warnings.forEach(warning -> HeaderWarning.addWarning(DeprecationLogger.CRITICAL, warning)); - listener.onResponse(new Response(docs, generatedDestIndexSettings)); - } else { - List> results = docs.stream().map(doc -> { - Map src = new HashMap<>(); - String id = (String) doc.get(TransformField.DOCUMENT_ID_FIELD); - src.put("_source", doc); - src.put("_id", id); - src.put("_index", dest); - return src; - }).collect(Collectors.toList()); + ActionListener>> previewListener = ActionListener.wrap(docs -> { + if (pipeline == null) { + TransformDestIndexSettings generatedDestIndexSettings = TransformIndex.createTransformDestIndexSettings( + mappings.get(), + transformId, + Clock.systemUTC() + ); + List warnings = TransformConfigLinter.getWarnings(function, source, syncConfig); + warnings.forEach(warning -> HeaderWarning.addWarning(DeprecationLogger.CRITICAL, warning)); + listener.onResponse(new Response(docs, generatedDestIndexSettings)); + } else { + List> results = docs.stream().map(doc -> { + Map src = new HashMap<>(); + String id = (String) doc.get(TransformField.DOCUMENT_ID_FIELD); + src.put("_source", doc); + src.put("_id", id); + src.put("_index", dest); + return src; + }).collect(Collectors.toList()); - try (XContentBuilder builder = jsonBuilder()) { - builder.startObject(); - builder.field("docs", results); - builder.endObject(); - var pipelineRequest = new SimulatePipelineRequest(BytesReference.bytes(builder), XContentType.JSON); - pipelineRequest.setId(pipeline); - client.execute(SimulatePipelineAction.INSTANCE, pipelineRequest, pipelineResponseActionListener); - } + try (XContentBuilder builder = jsonBuilder()) { + builder.startObject(); + builder.field("docs", results); + builder.endObject(); + var pipelineRequest = new SimulatePipelineRequest(BytesReference.bytes(builder), XContentType.JSON); + pipelineRequest.setId(pipeline); + client.execute(SimulatePipelineAction.INSTANCE, pipelineRequest, pipelineResponseActionListener); } - }, - listener::onFailure - ); + } + }, listener::onFailure); - ActionListener> deduceMappingsListener = ActionListener.wrap( - deducedMappings -> { - mappings.set(deducedMappings); - function.preview( - client, - ClientHelper.filterSecurityHeaders(threadPool.getThreadContext().getHeaders()), - source, - deducedMappings, - NUMBER_OF_PREVIEW_BUCKETS, - previewListener - ); - }, - listener::onFailure - ); + ActionListener> deduceMappingsListener = ActionListener.wrap(deducedMappings -> { + mappings.set(deducedMappings); + function.preview( + client, + ClientHelper.filterSecurityHeaders(threadPool.getThreadContext().getHeaders()), + source, + deducedMappings, + NUMBER_OF_PREVIEW_BUCKETS, + previewListener + ); + }, listener::onFailure); function.deduceMappings(client, source, deduceMappingsListener); } diff --git a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/action/TransportPutTransformAction.java b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/action/TransportPutTransformAction.java index 726acafb0fac4..1cd5eed7f94a7 100644 --- a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/action/TransportPutTransformAction.java +++ b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/action/TransportPutTransformAction.java @@ -135,28 +135,31 @@ protected void masterOperation(Task task, Request request, ClusterState clusterS // <3> Create the transform ActionListener validateTransformListener = ActionListener.wrap( - validationResponse -> { - putTransform(request, listener); - }, + validationResponse -> { putTransform(request, listener); }, listener::onFailure ); // <2> Validate source and destination indices - ActionListener checkPrivilegesListener = ActionListener.wrap( - aVoid -> { - client.execute( - ValidateTransformAction.INSTANCE, - new ValidateTransformAction.Request(config, request.isDeferValidation(), request.timeout()), - validateTransformListener - ); - }, - listener::onFailure - ); + ActionListener checkPrivilegesListener = ActionListener.wrap(aVoid -> { + client.execute( + ValidateTransformAction.INSTANCE, + new ValidateTransformAction.Request(config, request.isDeferValidation(), request.timeout()), + validateTransformListener + ); + }, listener::onFailure); // <1> Early check to verify that the user can create the destination index and can read from the source if (XPackSettings.SECURITY_ENABLED.get(settings) && request.isDeferValidation() == false) { TransformPrivilegeChecker.checkPrivileges( - "create", securityContext, indexNameExpressionResolver, clusterState, client, config, true, checkPrivilegesListener); + "create", + securityContext, + indexNameExpressionResolver, + clusterState, + client, + config, + true, + checkPrivilegesListener + ); } else { // No security enabled, just move on checkPrivilegesListener.onResponse(null); } diff --git a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/action/TransportSetTransformResetModeAction.java b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/action/TransportSetTransformResetModeAction.java index 0316ed9921555..45450b04070d7 100644 --- a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/action/TransportSetTransformResetModeAction.java +++ b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/action/TransportSetTransformResetModeAction.java @@ -19,12 +19,16 @@ import org.elasticsearch.xpack.core.transform.TransformMetadata; import org.elasticsearch.xpack.core.transform.action.SetResetModeAction; - public class TransportSetTransformResetModeAction extends AbstractTransportSetResetModeAction { @Inject - public TransportSetTransformResetModeAction(TransportService transportService, ThreadPool threadPool, ClusterService clusterService, - ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver) { + public TransportSetTransformResetModeAction( + TransportService transportService, + ThreadPool threadPool, + ClusterService clusterService, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver + ) { super(SetResetModeAction.NAME, transportService, threadPool, clusterService, actionFilters, indexNameExpressionResolver); } @@ -43,16 +47,11 @@ protected ClusterState setState(ClusterState oldState, SetResetModeActionRequest ClusterState.Builder newState = ClusterState.builder(oldState); if (request.shouldDeleteMetadata()) { assert request.isEnabled() == false; // SetResetModeActionRequest should have enforced this - newState.metadata(Metadata.builder(oldState.getMetadata()) - .removeCustom(TransformMetadata.TYPE) - .build()); + newState.metadata(Metadata.builder(oldState.getMetadata()).removeCustom(TransformMetadata.TYPE).build()); } else { - TransformMetadata.Builder builder = TransformMetadata.Builder - .from(oldState.metadata().custom(TransformMetadata.TYPE)) + TransformMetadata.Builder builder = TransformMetadata.Builder.from(oldState.metadata().custom(TransformMetadata.TYPE)) .isResetMode(request.isEnabled()); - newState.metadata(Metadata.builder(oldState.getMetadata()) - .putCustom(TransformMetadata.TYPE, builder.build()) - .build()); + newState.metadata(Metadata.builder(oldState.getMetadata()).putCustom(TransformMetadata.TYPE, builder.build()).build()); } return newState.build(); } diff --git a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/action/TransportStartTransformAction.java b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/action/TransportStartTransformAction.java index 6c0a54f148220..67e2f6cdff0a3 100644 --- a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/action/TransportStartTransformAction.java +++ b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/action/TransportStartTransformAction.java @@ -229,7 +229,9 @@ protected void masterOperation( listener.onFailure( new ElasticsearchStatusException( TransformMessages.getMessage( - TransformMessages.TRANSFORM_CONFIGURATION_INVALID, request.getId(), validationException.getMessage() + TransformMessages.TRANSFORM_CONFIGURATION_INVALID, + request.getId(), + validationException.getMessage() ), RestStatus.BAD_REQUEST ) @@ -238,7 +240,10 @@ protected void masterOperation( } transformTaskParamsHolder.set( new TransformTaskParams( - config.getId(), config.getVersion(), config.getFrequency(), config.getSource().requiresRemoteCluster() + config.getId(), + config.getVersion(), + config.getFrequency(), + config.getSource().requiresRemoteCluster() ) ); transformConfigHolder.set(config); @@ -253,9 +258,11 @@ protected void masterOperation( transformConfigManager.getTransformConfiguration(request.getId(), getTransformListener); } - private void createDestinationIndex(final TransformConfig config, - final Map mappings, - final ActionListener listener) { + private void createDestinationIndex( + final TransformConfig config, + final Map mappings, + final ActionListener listener + ) { TransformDestIndexSettings generatedDestIndexSettings = TransformIndex.createTransformDestIndexSettings( mappings, diff --git a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/action/TransportStopTransformAction.java b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/action/TransportStopTransformAction.java index eb95e7feff2c1..471396d50fbba 100644 --- a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/action/TransportStopTransformAction.java +++ b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/action/TransportStopTransformAction.java @@ -167,8 +167,10 @@ protected void doExecute(Task task, Request request, ActionListener li ActionListener.wrap(hitsAndIds -> { validateTaskState(state, hitsAndIds.v2().v1(), request.isForce()); request.setExpandedIds(new HashSet<>(hitsAndIds.v2().v1())); - final TransformNodeAssignments transformNodeAssignments = - TransformNodes.transformTaskNodes(hitsAndIds.v2().v1(), state); + final TransformNodeAssignments transformNodeAssignments = TransformNodes.transformTaskNodes( + hitsAndIds.v2().v1(), + state + ); final ActionListener doExecuteListener; if (transformNodeAssignments.getWaitingForAssignment().size() > 0) { @@ -302,24 +304,19 @@ private ActionListener waitForStopListener(Request request, ActionList })), listener::onFailure ); - return ActionListener.wrap( - response -> { - // If there were failures attempting to stop the tasks, we don't know if they will actually stop. - // It is better to respond to the user now than allow for the persistent task waiting to timeout - if (response.getTaskFailures().isEmpty() == false || response.getNodeFailures().isEmpty() == false) { - RestStatus status = firstNotOKStatus(response.getTaskFailures(), response.getNodeFailures()); - listener.onFailure(buildException(response.getTaskFailures(), response.getNodeFailures(), status)); - return; - } - // Wait until the persistent task is stopped - // Switch over to Generic threadpool so we don't block the network thread - threadPool.generic() - .execute( - () -> waitForTransformStopped(request.getExpandedIds(), request.getTimeout(), request.isForce(), onStopListener) - ); - }, - listener::onFailure - ); + return ActionListener.wrap(response -> { + // If there were failures attempting to stop the tasks, we don't know if they will actually stop. + // It is better to respond to the user now than allow for the persistent task waiting to timeout + if (response.getTaskFailures().isEmpty() == false || response.getNodeFailures().isEmpty() == false) { + RestStatus status = firstNotOKStatus(response.getTaskFailures(), response.getNodeFailures()); + listener.onFailure(buildException(response.getTaskFailures(), response.getNodeFailures(), status)); + return; + } + // Wait until the persistent task is stopped + // Switch over to Generic threadpool so we don't block the network thread + threadPool.generic() + .execute(() -> waitForTransformStopped(request.getExpandedIds(), request.getTimeout(), request.isForce(), onStopListener)); + }, listener::onFailure); } static ElasticsearchStatusException buildException( diff --git a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/action/TransportValidateTransformAction.java b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/action/TransportValidateTransformAction.java index bc9190c0eed24..0703917e77b4d 100644 --- a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/action/TransportValidateTransformAction.java +++ b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/action/TransportValidateTransformAction.java @@ -64,7 +64,8 @@ public TransportValidateTransformAction( indexNameExpressionResolver, clusterService, settings, - ingestService); + ingestService + ); } protected TransportValidateTransformAction( @@ -87,7 +88,8 @@ protected TransportValidateTransformAction( transportService.getRemoteClusterService(), DiscoveryNode.isRemoteClusterClient(settings) /* transforms are BASIC so always allowed, no need to check license */ - ? new RemoteClusterLicenseChecker(client, mode -> true) : null, + ? new RemoteClusterLicenseChecker(client, mode -> true) + : null, ingestService, clusterService.getNodeName(), License.OperationMode.BASIC.description() @@ -101,7 +103,15 @@ protected void doExecute(Task task, Request request, ActionListener li TransformNodes.throwIfNoTransformNodes(clusterState); boolean requiresRemote = request.getConfig().getSource().requiresRemoteCluster(); if (TransformNodes.redirectToAnotherNodeIfNeeded( - clusterState, nodeSettings, requiresRemote, transportService, actionName, request, Response::new, listener)) { + clusterState, + nodeSettings, + requiresRemote, + transportService, + actionName, + request, + Response::new, + listener + )) { return; } } @@ -127,45 +137,33 @@ protected void doExecute(Task task, Request request, ActionListener li // <5> Final listener ActionListener> deduceMappingsListener = ActionListener.wrap( - deducedMappings -> { - listener.onResponse(new Response(deducedMappings)); - }, + deducedMappings -> { listener.onResponse(new Response(deducedMappings)); }, deduceTargetMappingsException -> listener.onFailure( - new RuntimeException( - TransformMessages.REST_PUT_TRANSFORM_FAILED_TO_DEDUCE_DEST_MAPPINGS, - deduceTargetMappingsException) + new RuntimeException(TransformMessages.REST_PUT_TRANSFORM_FAILED_TO_DEDUCE_DEST_MAPPINGS, deduceTargetMappingsException) ) ); // <4> Deduce destination index mappings - ActionListener validateQueryListener = ActionListener.wrap( - validateQueryResponse -> { - if (request.isDeferValidation()) { - deduceMappingsListener.onResponse(null); - } else { - function.deduceMappings(client, config.getSource(), deduceMappingsListener); - } - }, - listener::onFailure - ); + ActionListener validateQueryListener = ActionListener.wrap(validateQueryResponse -> { + if (request.isDeferValidation()) { + deduceMappingsListener.onResponse(null); + } else { + function.deduceMappings(client, config.getSource(), deduceMappingsListener); + } + }, listener::onFailure); // <3> Validate transform query - ActionListener validateConfigListener = ActionListener.wrap( - validateConfigResponse -> { - if (request.isDeferValidation()) { - validateQueryListener.onResponse(true); - } else { - function.validateQuery(client, config.getSource(), validateQueryListener); - } - }, - listener::onFailure - ); + ActionListener validateConfigListener = ActionListener.wrap(validateConfigResponse -> { + if (request.isDeferValidation()) { + validateQueryListener.onResponse(true); + } else { + function.validateQuery(client, config.getSource(), validateQueryListener); + } + }, listener::onFailure); // <2> Validate transform function config ActionListener validateSourceDestListener = ActionListener.wrap( - validateSourceDestResponse -> { - function.validateConfig(validateConfigListener); - }, + validateSourceDestResponse -> { function.validateConfig(validateConfigListener); }, listener::onFailure ); diff --git a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/checkpoint/DefaultCheckpointProvider.java b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/checkpoint/DefaultCheckpointProvider.java index 4893d59c4ccb3..341b8e9a363b5 100644 --- a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/checkpoint/DefaultCheckpointProvider.java +++ b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/checkpoint/DefaultCheckpointProvider.java @@ -85,20 +85,16 @@ public void createNextCheckpoint(final TransformCheckpoint lastCheckpoint, final final long timestamp = clock.millis(); final long checkpoint = TransformCheckpoint.isNullOrEmpty(lastCheckpoint) ? 1 : lastCheckpoint.getCheckpoint() + 1; - getIndexCheckpoints( - ActionListener.wrap( - checkpointsByIndex -> { - reportSourceIndexChanges( - TransformCheckpoint.isNullOrEmpty(lastCheckpoint) - ? Collections.emptySet() - : lastCheckpoint.getIndicesCheckpoints().keySet(), - checkpointsByIndex.keySet() - ); + getIndexCheckpoints(ActionListener.wrap(checkpointsByIndex -> { + reportSourceIndexChanges( + TransformCheckpoint.isNullOrEmpty(lastCheckpoint) + ? Collections.emptySet() + : lastCheckpoint.getIndicesCheckpoints().keySet(), + checkpointsByIndex.keySet() + ); - listener.onResponse(new TransformCheckpoint(transformConfig.getId(), timestamp, checkpoint, checkpointsByIndex, 0L)); - }, - listener::onFailure) - ); + listener.onResponse(new TransformCheckpoint(transformConfig.getId(), timestamp, checkpoint, checkpointsByIndex, 0L)); + }, listener::onFailure)); } protected void getIndexCheckpoints(ActionListener> listener) { @@ -177,15 +173,20 @@ private static void getCheckpointsFromOneCluster( logger.warn( new ParameterizedMessage( "Source has [{}] failed shards, shard failure [{}]", - response.getFailedShards(), i).getFormattedMessage(), - response.getShardFailures()[i]); + response.getFailedShards(), + i + ).getFormattedMessage(), + response.getShardFailures()[i] + ); } listener.onFailure( new CheckpointException( "Source has [{}] failed shards, first shard failure: {}", response.getShardFailures()[0], response.getFailedShards(), - response.getShardFailures()[0].toString())); + response.getShardFailures()[0].toString() + ) + ); return; } listener.onResponse(extractIndexCheckPoints(response.getShards(), userIndices, prefix)); diff --git a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/checkpoint/TimeBasedCheckpointProvider.java b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/checkpoint/TimeBasedCheckpointProvider.java index f1737b22d15bd..4ebb72a220412 100644 --- a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/checkpoint/TimeBasedCheckpointProvider.java +++ b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/checkpoint/TimeBasedCheckpointProvider.java @@ -27,10 +27,10 @@ import org.elasticsearch.xpack.transform.persistence.TransformConfigManager; import java.time.Clock; +import java.util.Map; import java.util.Objects; import java.util.Optional; import java.util.function.Function; -import java.util.Map; import static java.util.function.Function.identity; @@ -60,21 +60,17 @@ public void sourceHasChanged(TransformCheckpoint lastCheckpoint, ActionListener< final long timestamp = clock.millis(); final long timeUpperBound = alignTimestamp.apply(timestamp - timeSyncConfig.getDelay().millis()); - BoolQueryBuilder queryBuilder = new BoolQueryBuilder() - .filter(transformConfig.getSource().getQueryConfig().getQuery()) + BoolQueryBuilder queryBuilder = new BoolQueryBuilder().filter(transformConfig.getSource().getQueryConfig().getQuery()) .filter( - new RangeQueryBuilder(timeSyncConfig.getField()) - .gte(lastCheckpoint.getTimeUpperBound()) + new RangeQueryBuilder(timeSyncConfig.getField()).gte(lastCheckpoint.getTimeUpperBound()) .lt(timeUpperBound) .format("epoch_millis") ); - SearchSourceBuilder sourceBuilder = new SearchSourceBuilder() - .size(0) + SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().size(0) // we only want to know if there is at least 1 new document .trackTotalHitsUpTo(1) .query(queryBuilder); - SearchRequest searchRequest = new SearchRequest(transformConfig.getSource().getIndex()) - .allowPartialSearchResults(false) + SearchRequest searchRequest = new SearchRequest(transformConfig.getSource().getIndex()).allowPartialSearchResults(false) .indicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN) .source(sourceBuilder); @@ -86,10 +82,7 @@ public void sourceHasChanged(TransformCheckpoint lastCheckpoint, ActionListener< client, SearchAction.INSTANCE, searchRequest, - ActionListener.wrap( - r -> listener.onResponse(r.getHits().getTotalHits().value > 0L), - listener::onFailure - ) + ActionListener.wrap(r -> listener.onResponse(r.getHits().getTotalHits().value > 0L), listener::onFailure) ); } @@ -133,12 +126,12 @@ private static Function createAlignTimestampFunction(TransformConfig if (groups == null || groups.isEmpty()) { return identity(); } - Optional dateHistogramGroupSource = - groups.values().stream() - .filter(DateHistogramGroupSource.class::isInstance) - .map(DateHistogramGroupSource.class::cast) - .filter(group -> Objects.equals(group.getField(), transformConfig.getSyncConfig().getField())) - .findFirst(); + Optional dateHistogramGroupSource = groups.values() + .stream() + .filter(DateHistogramGroupSource.class::isInstance) + .map(DateHistogramGroupSource.class::cast) + .filter(group -> Objects.equals(group.getField(), transformConfig.getSyncConfig().getField())) + .findFirst(); if (dateHistogramGroupSource.isEmpty()) { return identity(); } diff --git a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/checkpoint/TransformCheckpointService.java b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/checkpoint/TransformCheckpointService.java index 4eebaf4fbde7b..014c400ebb805 100644 --- a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/checkpoint/TransformCheckpointService.java +++ b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/checkpoint/TransformCheckpointService.java @@ -66,7 +66,13 @@ public CheckpointProvider getCheckpointProvider(final Client client, final Trans } return new DefaultCheckpointProvider( - clock, client, remoteClusterResolver, transformConfigManager, transformAuditor, transformConfig); + clock, + client, + remoteClusterResolver, + transformConfigManager, + transformAuditor, + transformConfig + ); } /** diff --git a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/notifications/TransformAuditor.java b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/notifications/TransformAuditor.java index eddbca9ada8fc..408281f2a5dc8 100644 --- a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/notifications/TransformAuditor.java +++ b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/notifications/TransformAuditor.java @@ -39,9 +39,7 @@ public TransformAuditor(Client client, String nodeName, ClusterService clusterSe TransformInternalIndexConstants.AUDIT_INDEX, () -> { try { - return new PutComposableIndexTemplateAction.Request( - TransformInternalIndexConstants.AUDIT_INDEX - ).indexTemplate( + return new PutComposableIndexTemplateAction.Request(TransformInternalIndexConstants.AUDIT_INDEX).indexTemplate( new ComposableIndexTemplate.Builder().template(TransformInternalIndex.getAuditIndexTemplate()) .version((long) Version.CURRENT.id) .indexPatterns(Collections.singletonList(TransformInternalIndexConstants.AUDIT_INDEX_PREFIX + "*")) diff --git a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/persistence/TransformIndex.java b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/persistence/TransformIndex.java index c512b31b2ebc7..3f2adb880e469 100644 --- a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/persistence/TransformIndex.java +++ b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/persistence/TransformIndex.java @@ -139,7 +139,6 @@ private static Settings createSettings() { * @param mappings A Map of the form {"fieldName": "fieldType"} */ static Map createMappingsFromStringMap(Map mappings) { - return mappings.entrySet().stream() - .collect(toMap(e -> e.getKey(), e -> singletonMap("type", e.getValue()))); + return mappings.entrySet().stream().collect(toMap(e -> e.getKey(), e -> singletonMap("type", e.getValue()))); } } diff --git a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/persistence/TransformInternalIndex.java b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/persistence/TransformInternalIndex.java index f00b0c08d1d56..9e05dfc3b33f0 100644 --- a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/persistence/TransformInternalIndex.java +++ b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/persistence/TransformInternalIndex.java @@ -120,28 +120,27 @@ private static XContentBuilder auditMappings() throws IOException { builder.startObject(SINGLE_MAPPING_NAME); addMetaInformation(builder); builder.field(DYNAMIC, "false"); - builder - .startObject(PROPERTIES) - .startObject(TRANSFORM_ID) - .field(TYPE, KEYWORD) - .endObject() - .startObject(AbstractAuditMessage.LEVEL.getPreferredName()) - .field(TYPE, KEYWORD) - .endObject() - .startObject(AbstractAuditMessage.MESSAGE.getPreferredName()) - .field(TYPE, TEXT) - .startObject(FIELDS) - .startObject(RAW) - .field(TYPE, KEYWORD) - .field(IGNORE_ABOVE, 1024) - .endObject() - .endObject() + builder.startObject(PROPERTIES) + .startObject(TRANSFORM_ID) + .field(TYPE, KEYWORD) + .endObject() + .startObject(AbstractAuditMessage.LEVEL.getPreferredName()) + .field(TYPE, KEYWORD) + .endObject() + .startObject(AbstractAuditMessage.MESSAGE.getPreferredName()) + .field(TYPE, TEXT) + .startObject(FIELDS) + .startObject(RAW) + .field(TYPE, KEYWORD) + .field(IGNORE_ABOVE, 1024) + .endObject() + .endObject() .endObject() .startObject(AbstractAuditMessage.TIMESTAMP.getPreferredName()) - .field(TYPE, DATE) + .field(TYPE, DATE) .endObject() .startObject(AbstractAuditMessage.NODE_NAME.getPreferredName()) - .field(TYPE, KEYWORD) + .field(TYPE, KEYWORD) .endObject() .endObject() .endObject() @@ -191,163 +190,160 @@ public static XContentBuilder mappings(XContentBuilder builder) throws IOExcepti } private static XContentBuilder addTransformStoredDocMappings(XContentBuilder builder) throws IOException { - return builder - .startObject(TransformStoredDoc.STATE_FIELD.getPreferredName()) - .startObject(PROPERTIES) - .startObject(TransformState.TASK_STATE.getPreferredName()) - .field(TYPE, KEYWORD) - .endObject() - .startObject(TransformState.INDEXER_STATE.getPreferredName()) - .field(TYPE, KEYWORD) - .endObject() - .startObject(TransformState.SHOULD_STOP_AT_NEXT_CHECKPOINT.getPreferredName()) - .field(TYPE, BOOLEAN) - .endObject() - .startObject(TransformState.CURRENT_POSITION.getPreferredName()) - .field(ENABLED, false) - .endObject() - .startObject(TransformState.CHECKPOINT.getPreferredName()) - .field(TYPE, LONG) - .endObject() - .startObject(TransformState.REASON.getPreferredName()) - .field(TYPE, KEYWORD) - .endObject() - .startObject(TransformState.PROGRESS.getPreferredName()) - .startObject(PROPERTIES) - .startObject(TransformProgress.TOTAL_DOCS.getPreferredName()) - .field(TYPE, LONG) - .endObject() - .startObject(TransformProgress.DOCS_REMAINING.getPreferredName()) - .field(TYPE, LONG) - .endObject() - .startObject(TransformProgress.PERCENT_COMPLETE) - .field(TYPE, FLOAT) - .endObject() - .startObject(TransformProgress.DOCS_INDEXED.getPreferredName()) - .field(TYPE, LONG) - .endObject() - .startObject(TransformProgress.DOCS_PROCESSED.getPreferredName()) - .field(TYPE, LONG) - .endObject() - .endObject() - .endObject() - .endObject() + return builder.startObject(TransformStoredDoc.STATE_FIELD.getPreferredName()) + .startObject(PROPERTIES) + .startObject(TransformState.TASK_STATE.getPreferredName()) + .field(TYPE, KEYWORD) + .endObject() + .startObject(TransformState.INDEXER_STATE.getPreferredName()) + .field(TYPE, KEYWORD) + .endObject() + .startObject(TransformState.SHOULD_STOP_AT_NEXT_CHECKPOINT.getPreferredName()) + .field(TYPE, BOOLEAN) + .endObject() + .startObject(TransformState.CURRENT_POSITION.getPreferredName()) + .field(ENABLED, false) + .endObject() + .startObject(TransformState.CHECKPOINT.getPreferredName()) + .field(TYPE, LONG) + .endObject() + .startObject(TransformState.REASON.getPreferredName()) + .field(TYPE, KEYWORD) + .endObject() + .startObject(TransformState.PROGRESS.getPreferredName()) + .startObject(PROPERTIES) + .startObject(TransformProgress.TOTAL_DOCS.getPreferredName()) + .field(TYPE, LONG) + .endObject() + .startObject(TransformProgress.DOCS_REMAINING.getPreferredName()) + .field(TYPE, LONG) + .endObject() + .startObject(TransformProgress.PERCENT_COMPLETE) + .field(TYPE, FLOAT) + .endObject() + .startObject(TransformProgress.DOCS_INDEXED.getPreferredName()) + .field(TYPE, LONG) + .endObject() + .startObject(TransformProgress.DOCS_PROCESSED.getPreferredName()) + .field(TYPE, LONG) + .endObject() + .endObject() + .endObject() + .endObject() .endObject() .startObject(TransformField.STATS_FIELD.getPreferredName()) - .startObject(PROPERTIES) - .startObject(TransformIndexerStats.NUM_PAGES.getPreferredName()) - .field(TYPE, LONG) - .endObject() - .startObject(TransformIndexerStats.NUM_INPUT_DOCUMENTS.getPreferredName()) - .field(TYPE, LONG) - .endObject() - .startObject(TransformIndexerStats.NUM_OUTPUT_DOCUMENTS.getPreferredName()) - .field(TYPE, LONG) - .endObject() - .startObject(TransformIndexerStats.NUM_DELETED_DOCUMENTS.getPreferredName()) - .field(TYPE, LONG) - .endObject() - .startObject(TransformIndexerStats.NUM_INVOCATIONS.getPreferredName()) - .field(TYPE, LONG) - .endObject() - .startObject(TransformIndexerStats.INDEX_TIME_IN_MS.getPreferredName()) - .field(TYPE, LONG) - .endObject() - .startObject(TransformIndexerStats.SEARCH_TIME_IN_MS.getPreferredName()) - .field(TYPE, LONG) - .endObject() - .startObject(TransformIndexerStats.PROCESSING_TIME_IN_MS.getPreferredName()) - .field(TYPE, LONG) - .endObject() - .startObject(TransformIndexerStats.DELETE_TIME_IN_MS.getPreferredName()) - .field(TYPE, LONG) - .endObject() - .startObject(TransformIndexerStats.INDEX_TOTAL.getPreferredName()) - .field(TYPE, LONG) - .endObject() - .startObject(TransformIndexerStats.SEARCH_TOTAL.getPreferredName()) - .field(TYPE, LONG) - .endObject() - .startObject(TransformIndexerStats.PROCESSING_TOTAL.getPreferredName()) - .field(TYPE, LONG) - .endObject() - .startObject(TransformIndexerStats.SEARCH_FAILURES.getPreferredName()) - .field(TYPE, LONG) - .endObject() - .startObject(TransformIndexerStats.INDEX_FAILURES.getPreferredName()) - .field(TYPE, LONG) - .endObject() - .startObject(TransformIndexerStats.EXPONENTIAL_AVG_CHECKPOINT_DURATION_MS.getPreferredName()) - .field(TYPE, DOUBLE) - .endObject() - .startObject(TransformIndexerStats.EXPONENTIAL_AVG_DOCUMENTS_INDEXED.getPreferredName()) - .field(TYPE, DOUBLE) - .endObject() - .startObject(TransformIndexerStats.EXPONENTIAL_AVG_DOCUMENTS_PROCESSED.getPreferredName()) - .field(TYPE, DOUBLE) - .endObject() - .endObject() + .startObject(PROPERTIES) + .startObject(TransformIndexerStats.NUM_PAGES.getPreferredName()) + .field(TYPE, LONG) + .endObject() + .startObject(TransformIndexerStats.NUM_INPUT_DOCUMENTS.getPreferredName()) + .field(TYPE, LONG) + .endObject() + .startObject(TransformIndexerStats.NUM_OUTPUT_DOCUMENTS.getPreferredName()) + .field(TYPE, LONG) + .endObject() + .startObject(TransformIndexerStats.NUM_DELETED_DOCUMENTS.getPreferredName()) + .field(TYPE, LONG) + .endObject() + .startObject(TransformIndexerStats.NUM_INVOCATIONS.getPreferredName()) + .field(TYPE, LONG) + .endObject() + .startObject(TransformIndexerStats.INDEX_TIME_IN_MS.getPreferredName()) + .field(TYPE, LONG) + .endObject() + .startObject(TransformIndexerStats.SEARCH_TIME_IN_MS.getPreferredName()) + .field(TYPE, LONG) + .endObject() + .startObject(TransformIndexerStats.PROCESSING_TIME_IN_MS.getPreferredName()) + .field(TYPE, LONG) + .endObject() + .startObject(TransformIndexerStats.DELETE_TIME_IN_MS.getPreferredName()) + .field(TYPE, LONG) + .endObject() + .startObject(TransformIndexerStats.INDEX_TOTAL.getPreferredName()) + .field(TYPE, LONG) + .endObject() + .startObject(TransformIndexerStats.SEARCH_TOTAL.getPreferredName()) + .field(TYPE, LONG) + .endObject() + .startObject(TransformIndexerStats.PROCESSING_TOTAL.getPreferredName()) + .field(TYPE, LONG) + .endObject() + .startObject(TransformIndexerStats.SEARCH_FAILURES.getPreferredName()) + .field(TYPE, LONG) + .endObject() + .startObject(TransformIndexerStats.INDEX_FAILURES.getPreferredName()) + .field(TYPE, LONG) + .endObject() + .startObject(TransformIndexerStats.EXPONENTIAL_AVG_CHECKPOINT_DURATION_MS.getPreferredName()) + .field(TYPE, DOUBLE) + .endObject() + .startObject(TransformIndexerStats.EXPONENTIAL_AVG_DOCUMENTS_INDEXED.getPreferredName()) + .field(TYPE, DOUBLE) + .endObject() + .startObject(TransformIndexerStats.EXPONENTIAL_AVG_DOCUMENTS_PROCESSED.getPreferredName()) + .field(TYPE, DOUBLE) + .endObject() + .endObject() .endObject(); } public static XContentBuilder addTransformsConfigMappings(XContentBuilder builder) throws IOException { - return builder - .startObject(TransformField.ID.getPreferredName()) - .field(TYPE, KEYWORD) + return builder.startObject(TransformField.ID.getPreferredName()) + .field(TYPE, KEYWORD) .endObject() .startObject(TransformField.SOURCE.getPreferredName()) - .startObject(PROPERTIES) - .startObject(SourceConfig.INDEX.getPreferredName()) - .field(TYPE, KEYWORD) - .endObject() - .startObject(SourceConfig.QUERY.getPreferredName()) - .field(ENABLED, false) - .endObject() - .endObject() + .startObject(PROPERTIES) + .startObject(SourceConfig.INDEX.getPreferredName()) + .field(TYPE, KEYWORD) + .endObject() + .startObject(SourceConfig.QUERY.getPreferredName()) + .field(ENABLED, false) + .endObject() + .endObject() .endObject() .startObject(TransformField.DESTINATION.getPreferredName()) - .startObject(PROPERTIES) - .startObject(DestConfig.INDEX.getPreferredName()) - .field(TYPE, KEYWORD) - .endObject() - .endObject() + .startObject(PROPERTIES) + .startObject(DestConfig.INDEX.getPreferredName()) + .field(TYPE, KEYWORD) + .endObject() + .endObject() .endObject() .startObject(TransformField.DESCRIPTION.getPreferredName()) - .field(TYPE, TEXT) + .field(TYPE, TEXT) .endObject() .startObject(TransformField.VERSION.getPreferredName()) - .field(TYPE, KEYWORD) + .field(TYPE, KEYWORD) .endObject() .startObject(TransformField.CREATE_TIME.getPreferredName()) - .field(TYPE, DATE) + .field(TYPE, DATE) .endObject() .startObject(TransformConfig.Function.PIVOT.getParseField().getPreferredName()) - .field(TYPE, FLATTENED) + .field(TYPE, FLATTENED) .endObject() .startObject(TransformConfig.Function.LATEST.getParseField().getPreferredName()) - .field(TYPE, FLATTENED) + .field(TYPE, FLATTENED) .endObject() .startObject(TransformField.METADATA.getPreferredName()) - .field(TYPE, FLATTENED) + .field(TYPE, FLATTENED) .endObject() .startObject(TransformField.RETENTION_POLICY.getPreferredName()) - .field(TYPE, FLATTENED) + .field(TYPE, FLATTENED) .endObject() .startObject(TransformField.SYNC.getPreferredName()) - .field(TYPE, FLATTENED) + .field(TYPE, FLATTENED) .endObject(); } private static XContentBuilder addTransformCheckpointMappings(XContentBuilder builder) throws IOException { - return builder - .startObject(TransformField.TIMESTAMP_MILLIS.getPreferredName()) - .field(TYPE, DATE) + return builder.startObject(TransformField.TIMESTAMP_MILLIS.getPreferredName()) + .field(TYPE, DATE) .endObject() .startObject(TransformField.TIME_UPPER_BOUND_MILLIS.getPreferredName()) - .field(TYPE, DATE) + .field(TYPE, DATE) .endObject() .startObject(TransformCheckpoint.CHECKPOINT.getPreferredName()) - .field(TYPE, LONG) + .field(TYPE, LONG) .endObject(); } diff --git a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/rest/action/RestCatTransformAction.java b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/rest/action/RestCatTransformAction.java index 7e41288a8af54..f8b0cf734ea18 100644 --- a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/rest/action/RestCatTransformAction.java +++ b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/rest/action/RestCatTransformAction.java @@ -41,10 +41,7 @@ public class RestCatTransformAction extends AbstractCatAction { @Override public List routes() { - return List.of( - new Route(GET, "_cat/transforms"), - new Route(GET, "_cat/transforms/{" + TransformField.TRANSFORM_ID + "}") - ); + return List.of(new Route(GET, "_cat/transforms"), new Route(GET, "_cat/transforms/{" + TransformField.TRANSFORM_ID + "}")); } @Override diff --git a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/rest/action/RestGetTransformAction.java b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/rest/action/RestGetTransformAction.java index 57e80c57243ef..05875dc9b996f 100644 --- a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/rest/action/RestGetTransformAction.java +++ b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/rest/action/RestGetTransformAction.java @@ -29,7 +29,8 @@ public class RestGetTransformAction extends BaseRestHandler { public List routes() { return List.of( new Route(GET, TransformField.REST_BASE_PATH_TRANSFORMS), - new Route(GET, TransformField.REST_BASE_PATH_TRANSFORMS_BY_ID)); + new Route(GET, TransformField.REST_BASE_PATH_TRANSFORMS_BY_ID) + ); } @Override @@ -41,8 +42,11 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient request.setAllowNoResources(restRequest.paramAsBoolean(ALLOW_NO_MATCH.getPreferredName(), true)); if (restRequest.hasParam(PageParams.FROM.getPreferredName()) || restRequest.hasParam(PageParams.SIZE.getPreferredName())) { request.setPageParams( - new PageParams(restRequest.paramAsInt(PageParams.FROM.getPreferredName(), PageParams.DEFAULT_FROM), - restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE))); + new PageParams( + restRequest.paramAsInt(PageParams.FROM.getPreferredName(), PageParams.DEFAULT_FROM), + restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE) + ) + ); } return channel -> client.execute(GetTransformAction.INSTANCE, request, new RestToXContentListener<>(channel)); } diff --git a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/rest/action/RestGetTransformStatsAction.java b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/rest/action/RestGetTransformStatsAction.java index a927a6e5ab4d1..a3acd5ff7ef80 100644 --- a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/rest/action/RestGetTransformStatsAction.java +++ b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/rest/action/RestGetTransformStatsAction.java @@ -26,7 +26,8 @@ public class RestGetTransformStatsAction extends BaseRestHandler { public List routes() { return List.of( new Route(GET, TransformField.REST_BASE_PATH_TRANSFORMS + "_stats"), - new Route(GET, TransformField.REST_BASE_PATH_TRANSFORMS_BY_ID + "_stats")); + new Route(GET, TransformField.REST_BASE_PATH_TRANSFORMS_BY_ID + "_stats") + ); } @Override @@ -36,11 +37,13 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient request.setAllowNoMatch(restRequest.paramAsBoolean(ALLOW_NO_MATCH.getPreferredName(), true)); if (restRequest.hasParam(PageParams.FROM.getPreferredName()) || restRequest.hasParam(PageParams.SIZE.getPreferredName())) { request.setPageParams( - new PageParams(restRequest.paramAsInt(PageParams.FROM.getPreferredName(), PageParams.DEFAULT_FROM), - restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE))); + new PageParams( + restRequest.paramAsInt(PageParams.FROM.getPreferredName(), PageParams.DEFAULT_FROM), + restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE) + ) + ); } - return channel -> client.execute(GetTransformStatsAction.INSTANCE, request, - new RestToXContentListener<>(channel)); + return channel -> client.execute(GetTransformStatsAction.INSTANCE, request, new RestToXContentListener<>(channel)); } @Override diff --git a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/rest/action/RestStopTransformAction.java b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/rest/action/RestStopTransformAction.java index 71db86c0db472..f48989a5e2562 100644 --- a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/rest/action/RestStopTransformAction.java +++ b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/rest/action/RestStopTransformAction.java @@ -28,23 +28,22 @@ public List routes() { @Override protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) { String id = restRequest.param(TransformField.ID.getPreferredName()); - TimeValue timeout = restRequest.paramAsTime(TransformField.TIMEOUT.getPreferredName(), - StopTransformAction.DEFAULT_TIMEOUT); + TimeValue timeout = restRequest.paramAsTime(TransformField.TIMEOUT.getPreferredName(), StopTransformAction.DEFAULT_TIMEOUT); boolean waitForCompletion = restRequest.paramAsBoolean(TransformField.WAIT_FOR_COMPLETION.getPreferredName(), false); boolean force = restRequest.paramAsBoolean(TransformField.FORCE.getPreferredName(), false); boolean allowNoMatch = restRequest.paramAsBoolean(TransformField.ALLOW_NO_MATCH.getPreferredName(), false); boolean waitForCheckpoint = restRequest.paramAsBoolean(TransformField.WAIT_FOR_CHECKPOINT.getPreferredName(), false); - - StopTransformAction.Request request = new StopTransformAction.Request(id, + StopTransformAction.Request request = new StopTransformAction.Request( + id, waitForCompletion, force, timeout, allowNoMatch, - waitForCheckpoint); + waitForCheckpoint + ); - return channel -> client.execute(StopTransformAction.INSTANCE, request, - new RestToXContentListener<>(channel)); + return channel -> client.execute(StopTransformAction.INSTANCE, request, new RestToXContentListener<>(channel)); } @Override diff --git a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/transforms/IDGenerator.java b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/transforms/IDGenerator.java index a7fae2596f644..5dd12d9f57a68 100644 --- a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/transforms/IDGenerator.java +++ b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/transforms/IDGenerator.java @@ -29,8 +29,7 @@ public final class IDGenerator { private final TreeMap objectsForIDGeneration = new TreeMap<>(); - public IDGenerator() { - } + public IDGenerator() {} /** * Add a value to the generator @@ -94,7 +93,7 @@ private static byte[] getBytes(Object value) { } else if (value instanceof Integer) { return Numbers.intToBytes((Integer) value); } else if (value instanceof Boolean) { - return new byte[] { (Boolean)value ? (byte)1 : (byte)0 }; + return new byte[] { (Boolean) value ? (byte) 1 : (byte) 0 }; } throw new IllegalArgumentException("Value of type [" + value.getClass() + "] is not supported"); diff --git a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/transforms/TransformContext.java b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/transforms/TransformContext.java index f5383e264e8a5..1be96aa36154f 100644 --- a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/transforms/TransformContext.java +++ b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/transforms/TransformContext.java @@ -133,16 +133,10 @@ void shutdown() { } void markAsFailed(String failureMessage) { - taskListener.fail( - failureMessage, - ActionListener.wrap( - r -> { - // Successfully marked as failed, reset counter so that task can be restarted - failureCount.set(0); - }, - e -> {} - ) - ); + taskListener.fail(failureMessage, ActionListener.wrap(r -> { + // Successfully marked as failed, reset counter so that task can be restarted + failureCount.set(0); + }, e -> {})); } } diff --git a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/transforms/TransformNodes.java b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/transforms/TransformNodes.java index ff44565818fbc..eb76798ccc8c1 100644 --- a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/transforms/TransformNodes.java +++ b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/transforms/TransformNodes.java @@ -11,8 +11,8 @@ import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.ActionListenerResponseHandler; import org.elasticsearch.cluster.ClusterState; -import org.elasticsearch.cluster.node.DiscoveryNodeRole; import org.elasticsearch.cluster.node.DiscoveryNode; +import org.elasticsearch.cluster.node.DiscoveryNodeRole; import org.elasticsearch.cluster.node.DiscoveryNodes; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.Writeable; @@ -200,7 +200,8 @@ public static (listener, reader)); + new ActionListenerResponseHandler<>(listener, reader) + ); } else { Map explain = new TreeMap<>(); for (DiscoveryNode node : nodes) { @@ -210,7 +211,9 @@ public static e.getKey() + ":" + e.getValue()).collect(Collectors.joining("|")))); + explain.entrySet().stream().map(e -> e.getKey() + ":" + e.getValue()).collect(Collectors.joining("|")) + ) + ); } return true; } @@ -235,15 +238,19 @@ static Optional selectAnyNodeThatCanRunThisTransform(DiscoveryNod .findAny(); } - public static boolean nodeCanRunThisTransform(DiscoveryNode node, - Version minRequiredVersion, - boolean requiresRemote, - Map explain) { + public static boolean nodeCanRunThisTransform( + DiscoveryNode node, + Version minRequiredVersion, + boolean requiresRemote, + Map explain + ) { // version of the transform run on a node that has at least the same version if (node.getVersion().onOrAfter(minRequiredVersion) == false) { if (explain != null) { explain.put( - node.getId(), "node has version: " + node.getVersion() + " but transform requires at least " + minRequiredVersion); + node.getId(), + "node has version: " + node.getVersion() + " but transform requires at least " + minRequiredVersion + ); } return false; } diff --git a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/transforms/TransformPersistentTasksExecutor.java b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/transforms/TransformPersistentTasksExecutor.java index b0d0b1d914b1d..37c497fba3580 100644 --- a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/transforms/TransformPersistentTasksExecutor.java +++ b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/transforms/TransformPersistentTasksExecutor.java @@ -319,11 +319,7 @@ protected void nodeOperation(AllocatedPersistentTask task, @Nullable TransformTa ); // <1> Check the latest internal index (IMPORTANT: according to _this_ node, which might be newer than master) is installed - TransformInternalIndex.createLatestVersionedIndexIfRequired( - clusterService, - buildTask.getParentTaskClient(), - templateCheckListener - ); + TransformInternalIndex.createLatestVersionedIndexIfRequired(clusterService, buildTask.getParentTaskClient(), templateCheckListener); } private static IndexerState currentIndexerState(TransformState previousState) { diff --git a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/transforms/common/DocumentConversionUtils.java b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/transforms/common/DocumentConversionUtils.java index f5496f3e9e589..c3345251e47b7 100644 --- a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/transforms/common/DocumentConversionUtils.java +++ b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/transforms/common/DocumentConversionUtils.java @@ -33,10 +33,12 @@ public class DocumentConversionUtils { * @param destinationPipeline Optional destination pipeline * @return A valid {@link IndexRequest} */ - public static IndexRequest convertDocumentToIndexRequest(String docId, - Map document, - String destinationIndex, - String destinationPipeline) { + public static IndexRequest convertDocumentToIndexRequest( + String docId, + Map document, + String destinationIndex, + String destinationPipeline + ) { if (docId == null) { throw new RuntimeException("Expected a document id but got null."); } @@ -53,7 +55,8 @@ public static IndexRequest convertDocumentToIndexRequest(String docId, * @return A new {@link Map} but with all keys that start with "_" removed */ public static Map removeInternalFields(Map document) { - return document.entrySet().stream() + return document.entrySet() + .stream() .filter(not(e -> e.getKey() != null && e.getKey().startsWith("_"))) // Workaround for handling null keys properly. For details see https://bugs.openjdk.java.net/browse/JDK-8148463 .collect(HashMap::new, (m, e) -> m.put(e.getKey(), e.getValue()), HashMap::putAll); @@ -68,16 +71,13 @@ public static Map removeInternalFields(Map document) { public static Map extractFieldMappings(FieldCapabilitiesResponse response) { Map extractedTypes = new HashMap<>(); - response.get() - .forEach( - (fieldName, capabilitiesMap) -> { - // TODO: overwrites types, requires resolve if types are mixed - capabilitiesMap.forEach((name, capability) -> { - logger.trace(() -> new ParameterizedMessage("Extracted type for [{}] : [{}]", fieldName, capability.getType())); - extractedTypes.put(fieldName, capability.getType()); - }); - } - ); + response.get().forEach((fieldName, capabilitiesMap) -> { + // TODO: overwrites types, requires resolve if types are mixed + capabilitiesMap.forEach((name, capability) -> { + logger.trace(() -> new ParameterizedMessage("Extracted type for [{}] : [{}]", fieldName, capability.getType())); + extractedTypes.put(fieldName, capability.getType()); + }); + }); return extractedTypes; } diff --git a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/transforms/pivot/AggregationResultUtils.java b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/transforms/pivot/AggregationResultUtils.java index 390845573d0cf..9f33cd8643937 100644 --- a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/transforms/pivot/AggregationResultUtils.java +++ b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/transforms/pivot/AggregationResultUtils.java @@ -10,7 +10,6 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.Numbers; import org.elasticsearch.common.geo.GeoPoint; - import org.elasticsearch.geometry.Rectangle; import org.elasticsearch.index.mapper.DateFieldMapper; import org.elasticsearch.search.aggregations.Aggregation; @@ -417,10 +416,7 @@ public Object value(Aggregation agg, Map fieldTypeMap, String lo // If the two geo_points are equal, it is a point if (aggregation.topLeft().equals(aggregation.bottomRight())) { geoShape.put(FIELD_TYPE, POINT); - geoShape.put( - FIELD_COORDINATES, - Arrays.asList(aggregation.topLeft().getLon(), aggregation.bottomRight().getLat()) - ); + geoShape.put(FIELD_COORDINATES, Arrays.asList(aggregation.topLeft().getLon(), aggregation.bottomRight().getLat())); // If only the lat or the lon of the two geo_points are equal, than we know it should be a line } else if (Double.compare(aggregation.topLeft().getLat(), aggregation.bottomRight().getLat()) == 0 || Double.compare(aggregation.topLeft().getLon(), aggregation.bottomRight().getLon()) == 0) { diff --git a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/transforms/pivot/Pivot.java b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/transforms/pivot/Pivot.java index 2abef9402e61c..238b0e4eb0cbc 100644 --- a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/transforms/pivot/Pivot.java +++ b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/transforms/pivot/Pivot.java @@ -16,15 +16,15 @@ import org.elasticsearch.common.ValidationException; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.aggregations.AggregationBuilder; import org.elasticsearch.search.aggregations.bucket.composite.CompositeAggregation; import org.elasticsearch.search.aggregations.bucket.composite.CompositeAggregationBuilder; import org.elasticsearch.search.builder.SearchSourceBuilder; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.transform.TransformMessages; import org.elasticsearch.xpack.core.transform.transforms.SettingsConfig; import org.elasticsearch.xpack.core.transform.transforms.SourceConfig; diff --git a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/transforms/pivot/SchemaUtil.java b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/transforms/pivot/SchemaUtil.java index 741d94b35a8a2..74c9a33f92ba8 100644 --- a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/transforms/pivot/SchemaUtil.java +++ b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/transforms/pivot/SchemaUtil.java @@ -108,23 +108,19 @@ public static void deduceMappings( // collects the target mapping types used for grouping Map fieldTypesForGrouping = new HashMap<>(); - config.getGroupConfig() - .getGroups() - .forEach( - (destinationFieldName, group) -> { - // skip any fields that use scripts as there will be no source mapping - if (group.getScriptConfig() != null) { - return; - } + config.getGroupConfig().getGroups().forEach((destinationFieldName, group) -> { + // skip any fields that use scripts as there will be no source mapping + if (group.getScriptConfig() != null) { + return; + } - // We will always need the field name for the grouping to create the mapping - fieldNamesForGrouping.put(destinationFieldName, group.getField()); - // Sometimes the group config will supply a desired mapping as well - if (group.getMappingType() != null) { - fieldTypesForGrouping.put(destinationFieldName, group.getMappingType()); - } - } - ); + // We will always need the field name for the grouping to create the mapping + fieldNamesForGrouping.put(destinationFieldName, group.getField()); + // Sometimes the group config will supply a desired mapping as well + if (group.getMappingType() != null) { + fieldTypesForGrouping.put(destinationFieldName, group.getMappingType()); + } + }); for (AggregationBuilder agg : config.getAggregationConfig().getAggregatorFactories()) { Tuple, Map> inputAndOutputTypes = TransformAggregations.getAggregationInputAndOutputTypes( @@ -156,7 +152,8 @@ public static void deduceMappings( aggregationTypes, fieldNamesForGrouping, fieldTypesForGrouping, - sourceMappings) + sourceMappings + ) ), listener::onFailure ) @@ -254,27 +251,25 @@ private static Map resolveMappings( /* * Very "magic" helper method to extract the source mappings */ - static void getSourceFieldMappings(Client client, - String[] index, - String[] fields, - Map runtimeMappings, - ActionListener> listener) { + static void getSourceFieldMappings( + Client client, + String[] index, + String[] fields, + Map runtimeMappings, + ActionListener> listener + ) { if (index == null || index.length == 0 || fields == null || fields.length == 0) { listener.onResponse(Collections.emptyMap()); return; } - FieldCapabilitiesRequest fieldCapabilitiesRequest = - new FieldCapabilitiesRequest() - .indices(index) - .fields(fields) - .runtimeFields(runtimeMappings) - .indicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN); + FieldCapabilitiesRequest fieldCapabilitiesRequest = new FieldCapabilitiesRequest().indices(index) + .fields(fields) + .runtimeFields(runtimeMappings) + .indicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN); client.execute( FieldCapabilitiesAction.INSTANCE, fieldCapabilitiesRequest, - ActionListener.wrap( - response -> listener.onResponse(extractFieldMappings(response)), - listener::onFailure) + ActionListener.wrap(response -> listener.onResponse(extractFieldMappings(response)), listener::onFailure) ); } diff --git a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/transforms/pivot/TransformAggregations.java b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/transforms/pivot/TransformAggregations.java index ccb350850fc84..1d8287d0e6c9c 100644 --- a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/transforms/pivot/TransformAggregations.java +++ b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/transforms/pivot/TransformAggregations.java @@ -224,20 +224,12 @@ public static Tuple, Map> getAggregationInpu outputFieldNames.get() .stream() .collect( - Collectors.toMap( - outputField -> agg.getName() + "." + outputField, - outputField -> outputField, - (v1, v2) -> v1 - ) + Collectors.toMap(outputField -> agg.getName() + "." + outputField, outputField -> outputField, (v1, v2) -> v1) ), outputFieldNames.get() .stream() .collect( - Collectors.toMap( - outputField -> agg.getName() + "." + outputField, - outputField -> agg.getType(), - (v1, v2) -> v1 - ) + Collectors.toMap(outputField -> agg.getName() + "." + outputField, outputField -> agg.getType(), (v1, v2) -> v1) ) ); } diff --git a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/utils/SourceDestValidations.java b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/utils/SourceDestValidations.java index f4d69012f3334..ad439930ad1dd 100644 --- a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/utils/SourceDestValidations.java +++ b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/utils/SourceDestValidations.java @@ -31,7 +31,10 @@ private SourceDestValidations() {} new SourceDestValidator.RemoteSourceEnabledAndRemoteLicenseValidation("transform"); private static final List PREVIEW_VALIDATIONS = Arrays.asList( - SOURCE_MISSING_VALIDATION, REMOTE_SOURCE_VALIDATION, DESTINATION_PIPELINE_MISSING_VALIDATION); + SOURCE_MISSING_VALIDATION, + REMOTE_SOURCE_VALIDATION, + DESTINATION_PIPELINE_MISSING_VALIDATION + ); private static final List ALL_VALIDATIONS = Arrays.asList( SOURCE_MISSING_VALIDATION, @@ -53,9 +56,11 @@ public static List getValidationsForPreview(List getValidations(boolean isDeferValidation, - List primaryValidations, - List additionalValidations) { + private static List getValidations( + boolean isDeferValidation, + List primaryValidations, + List additionalValidations + ) { if (isDeferValidation) { return SourceDestValidations.NON_DEFERABLE_VALIDATIONS; } diff --git a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/TransformInfoTransportActionTests.java b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/TransformInfoTransportActionTests.java index fddb7c5a8c568..5c6f875415384 100644 --- a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/TransformInfoTransportActionTests.java +++ b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/TransformInfoTransportActionTests.java @@ -29,18 +29,12 @@ public class TransformInfoTransportActionTests extends ESTestCase { public void testAvailable() { - TransformInfoTransportAction featureSet = new TransformInfoTransportAction( - mock(TransportService.class), - mock(ActionFilters.class) - ); + TransformInfoTransportAction featureSet = new TransformInfoTransportAction(mock(TransportService.class), mock(ActionFilters.class)); assertThat(featureSet.available(), is(true)); } public void testEnabledDefault() { - TransformInfoTransportAction featureSet = new TransformInfoTransportAction( - mock(TransportService.class), - mock(ActionFilters.class) - ); + TransformInfoTransportAction featureSet = new TransformInfoTransportAction(mock(TransportService.class), mock(ActionFilters.class)); assertTrue(featureSet.enabled()); } diff --git a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/TransformMetadataTests.java b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/TransformMetadataTests.java index b63925bd02db0..e4a52630542ec 100644 --- a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/TransformMetadataTests.java +++ b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/TransformMetadataTests.java @@ -8,8 +8,8 @@ package org.elasticsearch.xpack.transform; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.transform.TransformMetadata; public class TransformMetadataTests extends AbstractSerializingTestCase { diff --git a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/action/TransformPrivilegeCheckerTests.java b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/action/TransformPrivilegeCheckerTests.java index 8ce2d1332cf98..3e66ab5995a43 100644 --- a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/action/TransformPrivilegeCheckerTests.java +++ b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/action/TransformPrivilegeCheckerTests.java @@ -45,19 +45,16 @@ public class TransformPrivilegeCheckerTests extends ESTestCase { private static final String TRANSFORM_ID = "some-id"; private static final String SOURCE_INDEX_NAME = "some-source-index"; private static final String DEST_INDEX_NAME = "some-dest-index"; - private static final TransformConfig TRANSFORM_CONFIG = - new TransformConfig.Builder() - .setId(TRANSFORM_ID) - .setSource(new SourceConfig(SOURCE_INDEX_NAME)) - .setDest(new DestConfig(DEST_INDEX_NAME, null)) - .build(); + private static final TransformConfig TRANSFORM_CONFIG = new TransformConfig.Builder().setId(TRANSFORM_ID) + .setSource(new SourceConfig(SOURCE_INDEX_NAME)) + .setDest(new DestConfig(DEST_INDEX_NAME, null)) + .build(); - private final SecurityContext securityContext = - new SecurityContext(Settings.EMPTY, null) { - public User getUser() { - return new User(USER_NAME); - } - }; + private final SecurityContext securityContext = new SecurityContext(Settings.EMPTY, null) { + public User getUser() { + return new User(USER_NAME); + } + }; private final IndexNameExpressionResolver indexNameExpressionResolver = TestIndexNameExpressionResolver.newInstance(); private MyMockClient client; @@ -83,19 +80,17 @@ public void testCheckPrivileges_NoCheckDestIndexPrivileges() { client, TRANSFORM_CONFIG, false, - ActionListener.wrap( - aVoid -> { - HasPrivilegesRequest request = client.lastHasPrivilegesRequest; - assertThat(request.username(), is(equalTo(USER_NAME))); - assertThat(request.applicationPrivileges(), is(emptyArray())); - assertThat(request.clusterPrivileges(), is(emptyArray())); - assertThat(request.indexPrivileges(), is(arrayWithSize(1))); - RoleDescriptor.IndicesPrivileges sourceIndicesPrivileges = request.indexPrivileges()[0]; - assertThat(sourceIndicesPrivileges.getIndices(), is(arrayContaining(SOURCE_INDEX_NAME))); - assertThat(sourceIndicesPrivileges.getPrivileges(), is(arrayContaining("read", "view_index_metadata"))); - }, - e -> fail(e.getMessage()) - )); + ActionListener.wrap(aVoid -> { + HasPrivilegesRequest request = client.lastHasPrivilegesRequest; + assertThat(request.username(), is(equalTo(USER_NAME))); + assertThat(request.applicationPrivileges(), is(emptyArray())); + assertThat(request.clusterPrivileges(), is(emptyArray())); + assertThat(request.indexPrivileges(), is(arrayWithSize(1))); + RoleDescriptor.IndicesPrivileges sourceIndicesPrivileges = request.indexPrivileges()[0]; + assertThat(sourceIndicesPrivileges.getIndices(), is(arrayContaining(SOURCE_INDEX_NAME))); + assertThat(sourceIndicesPrivileges.getPrivileges(), is(arrayContaining("read", "view_index_metadata"))); + }, e -> fail(e.getMessage())) + ); } public void testCheckPrivileges_CheckDestIndexPrivileges_DestIndexDoesNotExist() { @@ -107,33 +102,29 @@ public void testCheckPrivileges_CheckDestIndexPrivileges_DestIndexDoesNotExist() client, TRANSFORM_CONFIG, true, - ActionListener.wrap( - aVoid -> { - HasPrivilegesRequest request = client.lastHasPrivilegesRequest; - assertThat(request.username(), is(equalTo(USER_NAME))); - assertThat(request.applicationPrivileges(), is(emptyArray())); - assertThat(request.clusterPrivileges(), is(emptyArray())); - assertThat(request.indexPrivileges(), is(arrayWithSize(2))); - RoleDescriptor.IndicesPrivileges sourceIndicesPrivileges = request.indexPrivileges()[0]; - assertThat(sourceIndicesPrivileges.getIndices(), is(arrayContaining(SOURCE_INDEX_NAME))); - assertThat(sourceIndicesPrivileges.getPrivileges(), is(arrayContaining("read", "view_index_metadata"))); - RoleDescriptor.IndicesPrivileges destIndicesPrivileges = request.indexPrivileges()[1]; - assertThat(destIndicesPrivileges.getIndices(), is(arrayContaining(DEST_INDEX_NAME))); - assertThat(destIndicesPrivileges.getPrivileges(), is(arrayContaining("read", "index", "create_index"))); - }, - e -> fail(e.getMessage()) - )); + ActionListener.wrap(aVoid -> { + HasPrivilegesRequest request = client.lastHasPrivilegesRequest; + assertThat(request.username(), is(equalTo(USER_NAME))); + assertThat(request.applicationPrivileges(), is(emptyArray())); + assertThat(request.clusterPrivileges(), is(emptyArray())); + assertThat(request.indexPrivileges(), is(arrayWithSize(2))); + RoleDescriptor.IndicesPrivileges sourceIndicesPrivileges = request.indexPrivileges()[0]; + assertThat(sourceIndicesPrivileges.getIndices(), is(arrayContaining(SOURCE_INDEX_NAME))); + assertThat(sourceIndicesPrivileges.getPrivileges(), is(arrayContaining("read", "view_index_metadata"))); + RoleDescriptor.IndicesPrivileges destIndicesPrivileges = request.indexPrivileges()[1]; + assertThat(destIndicesPrivileges.getIndices(), is(arrayContaining(DEST_INDEX_NAME))); + assertThat(destIndicesPrivileges.getPrivileges(), is(arrayContaining("read", "index", "create_index"))); + }, e -> fail(e.getMessage())) + ); } public void testCheckPrivileges_CheckDestIndexPrivileges_DestIndexExists() { - ClusterState clusterState = - ClusterState.builder(ClusterName.DEFAULT) - .metadata(Metadata.builder() - .put(IndexMetadata.builder(DEST_INDEX_NAME) - .settings(settings(Version.CURRENT)) - .numberOfShards(1) - .numberOfReplicas(0))) - .build(); + ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT) + .metadata( + Metadata.builder() + .put(IndexMetadata.builder(DEST_INDEX_NAME).settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(0)) + ) + .build(); TransformPrivilegeChecker.checkPrivileges( OPERATION_NAME, securityContext, @@ -142,22 +133,20 @@ public void testCheckPrivileges_CheckDestIndexPrivileges_DestIndexExists() { client, TRANSFORM_CONFIG, true, - ActionListener.wrap( - aVoid -> { - HasPrivilegesRequest request = client.lastHasPrivilegesRequest; - assertThat(request.username(), is(equalTo(USER_NAME))); - assertThat(request.applicationPrivileges(), is(emptyArray())); - assertThat(request.clusterPrivileges(), is(emptyArray())); - assertThat(request.indexPrivileges(), is(arrayWithSize(2))); - RoleDescriptor.IndicesPrivileges sourceIndicesPrivileges = request.indexPrivileges()[0]; - assertThat(sourceIndicesPrivileges.getIndices(), is(arrayContaining(SOURCE_INDEX_NAME))); - assertThat(sourceIndicesPrivileges.getPrivileges(), is(arrayContaining("read", "view_index_metadata"))); - RoleDescriptor.IndicesPrivileges destIndicesPrivileges = request.indexPrivileges()[1]; - assertThat(destIndicesPrivileges.getIndices(), is(arrayContaining(DEST_INDEX_NAME))); - assertThat(destIndicesPrivileges.getPrivileges(), is(arrayContaining("read", "index"))); - }, - e -> fail(e.getMessage()) - )); + ActionListener.wrap(aVoid -> { + HasPrivilegesRequest request = client.lastHasPrivilegesRequest; + assertThat(request.username(), is(equalTo(USER_NAME))); + assertThat(request.applicationPrivileges(), is(emptyArray())); + assertThat(request.clusterPrivileges(), is(emptyArray())); + assertThat(request.indexPrivileges(), is(arrayWithSize(2))); + RoleDescriptor.IndicesPrivileges sourceIndicesPrivileges = request.indexPrivileges()[0]; + assertThat(sourceIndicesPrivileges.getIndices(), is(arrayContaining(SOURCE_INDEX_NAME))); + assertThat(sourceIndicesPrivileges.getPrivileges(), is(arrayContaining("read", "view_index_metadata"))); + RoleDescriptor.IndicesPrivileges destIndicesPrivileges = request.indexPrivileges()[1]; + assertThat(destIndicesPrivileges.getIndices(), is(arrayContaining(DEST_INDEX_NAME))); + assertThat(destIndicesPrivileges.getPrivileges(), is(arrayContaining("read", "index"))); + }, e -> fail(e.getMessage())) + ); } private static class MyMockClient extends NoOpClient { diff --git a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/checkpoint/DefaultCheckpointProviderTests.java b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/checkpoint/DefaultCheckpointProviderTests.java index 8fc075782b1dc..5d903821ee9a5 100644 --- a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/checkpoint/DefaultCheckpointProviderTests.java +++ b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/checkpoint/DefaultCheckpointProviderTests.java @@ -10,6 +10,7 @@ import org.apache.logging.log4j.Level; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.apache.lucene.util.SetOnce; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.LatchedActionListener; import org.elasticsearch.action.admin.indices.get.GetIndexAction; @@ -17,9 +18,6 @@ import org.elasticsearch.action.admin.indices.stats.IndicesStatsAction; import org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse; import org.elasticsearch.action.support.DefaultShardOperationFailedException; -import org.apache.lucene.util.SetOnce; -import org.elasticsearch.action.ActionListener; -import org.elasticsearch.action.LatchedActionListener; import org.elasticsearch.client.Client; import org.elasticsearch.common.logging.Loggers; import org.elasticsearch.common.settings.ClusterSettings; @@ -48,14 +46,14 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.Matchers.startsWith; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.doReturn; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.nullValue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -204,14 +202,14 @@ public void testReportSourceIndexChangesAddDeleteMany() throws Exception { public void testHandlingShardFailures() throws Exception { String transformId = getTestName(); String indexName = "some-index"; - TransformConfig transformConfig = - new TransformConfig.Builder(TransformConfigTests.randomTransformConfig(transformId)) - .setSource(new SourceConfig(indexName)) - .build(); + TransformConfig transformConfig = new TransformConfig.Builder(TransformConfigTests.randomTransformConfig(transformId)).setSource( + new SourceConfig(indexName) + ).build(); RemoteClusterResolver remoteClusterResolver = mock(RemoteClusterResolver.class); - doReturn(new RemoteClusterResolver.ResolvedIndices(Collections.emptyMap(), Collections.singletonList(indexName))) - .when(remoteClusterResolver).resolve(transformConfig.getSource().getIndex()); + doReturn(new RemoteClusterResolver.ResolvedIndices(Collections.emptyMap(), Collections.singletonList(indexName))).when( + remoteClusterResolver + ).resolve(transformConfig.getSource().getIndex()); GetIndexResponse getIndexResponse = new GetIndexResponse(new String[] { indexName }, null, null, null, null, null); doAnswer(withResponse(getIndexResponse)).when(client).execute(eq(GetIndexAction.INSTANCE), any(), any()); @@ -220,8 +218,8 @@ public void testHandlingShardFailures() throws Exception { doReturn(7).when(indicesStatsResponse).getFailedShards(); doReturn( new DefaultShardOperationFailedException[] { - new DefaultShardOperationFailedException(indexName, 3, new Exception("something's wrong")) - }).when(indicesStatsResponse).getShardFailures(); + new DefaultShardOperationFailedException(indexName, 3, new Exception("something's wrong")) } + ).when(indicesStatsResponse).getShardFailures(); doAnswer(withResponse(indicesStatsResponse)).when(client).execute(eq(IndicesStatsAction.INSTANCE), any(), any()); DefaultCheckpointProvider provider = new DefaultCheckpointProvider( @@ -243,7 +241,9 @@ public void testHandlingShardFailures() throws Exception { e.getMessage(), startsWith( "Source has [7] failed shards, first shard failure: [some-index][3] failed, " - + "reason [java.lang.Exception: something's wrong")) + + "reason [java.lang.Exception: something's wrong" + ) + ) ), latch ) diff --git a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/checkpoint/TimeBasedCheckpointProviderTests.java b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/checkpoint/TimeBasedCheckpointProviderTests.java index 5e80ce69bcf1a..ccdaacd200118 100644 --- a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/checkpoint/TimeBasedCheckpointProviderTests.java +++ b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/checkpoint/TimeBasedCheckpointProviderTests.java @@ -97,7 +97,8 @@ public void testSourceHasChanged_NotChanged() throws InterruptedException { TIMESTAMP_FIELD, TimeValue.timeValueMinutes(10), TimeValue.ZERO, - tuple(0L, 123000000L)); + tuple(0L, 123000000L) + ); } public void testSourceHasChanged_Changed() throws InterruptedException { @@ -148,17 +149,23 @@ public void testSourceHasChanged_WithDelay() throws InterruptedException { ); } - private void testSourceHasChanged(long totalHits, - boolean expectedHasChangedValue, - TransformCheckpoint lastCheckpoint, - String dateHistogramField, - TimeValue dateHistogramInterval, - TimeValue delay, - Tuple expectedRangeQueryBounds) throws InterruptedException { + private void testSourceHasChanged( + long totalHits, + boolean expectedHasChangedValue, + TransformCheckpoint lastCheckpoint, + String dateHistogramField, + TimeValue dateHistogramInterval, + TimeValue delay, + Tuple expectedRangeQueryBounds + ) throws InterruptedException { doAnswer(withResponse(newSearchResponse(totalHits))).when(client).execute(eq(SearchAction.INSTANCE), any(), any()); String transformId = getTestName(); - TransformConfig transformConfig = - newTransformConfigWithDateHistogram(transformId, dateHistogramField, dateHistogramInterval, delay); + TransformConfig transformConfig = newTransformConfigWithDateHistogram( + transformId, + dateHistogramField, + dateHistogramInterval, + delay + ); TimeBasedCheckpointProvider provider = newCheckpointProvider(transformConfig); SetOnce hasChangedHolder = new SetOnce<>(); @@ -190,7 +197,8 @@ public void testCreateNextCheckpoint_NoDelay() throws InterruptedException { TimeValue.timeValueMinutes(10), TimeValue.ZERO, new TransformCheckpoint(transformId, 100000000L, 7, emptyMap(), 120000000L), - new TransformCheckpoint(transformId, 123456789L, 8, emptyMap(), 123000000L)); + new TransformCheckpoint(transformId, 123456789L, 8, emptyMap(), 123000000L) + ); } public void testCreateNextCheckpoint_SmallDelay() throws InterruptedException { @@ -201,7 +209,8 @@ public void testCreateNextCheckpoint_SmallDelay() throws InterruptedException { TimeValue.timeValueMinutes(10), TimeValue.timeValueMinutes(5), new TransformCheckpoint(transformId, 100000000L, 7, emptyMap(), 120000000L), - new TransformCheckpoint(transformId, 123456789L, 8, emptyMap(), 123000000L)); + new TransformCheckpoint(transformId, 123456789L, 8, emptyMap(), 123000000L) + ); } public void testCreateNextCheckpoint_BigDelay() throws InterruptedException { @@ -212,31 +221,38 @@ public void testCreateNextCheckpoint_BigDelay() throws InterruptedException { TimeValue.timeValueMinutes(10), TimeValue.timeValueMinutes(10), new TransformCheckpoint(transformId, 100000000L, 7, emptyMap(), 120000000L), - new TransformCheckpoint(transformId, 123456789L, 8, emptyMap(), 122400000L)); + new TransformCheckpoint(transformId, 123456789L, 8, emptyMap(), 122400000L) + ); } - private void testCreateNextCheckpoint(String transformId, - String dateHistogramField, - TimeValue dateHistogramInterval, - TimeValue delay, - TransformCheckpoint lastCheckpoint, - TransformCheckpoint expectedNextCheckpoint) throws InterruptedException { - GetIndexResponse getIndexResponse = - new GetIndexResponse( - new String[] { "some-index" }, - ImmutableOpenMap.of(), - ImmutableOpenMap.of(), - ImmutableOpenMap.of(), - ImmutableOpenMap.of(), - ImmutableOpenMap.of()); + private void testCreateNextCheckpoint( + String transformId, + String dateHistogramField, + TimeValue dateHistogramInterval, + TimeValue delay, + TransformCheckpoint lastCheckpoint, + TransformCheckpoint expectedNextCheckpoint + ) throws InterruptedException { + GetIndexResponse getIndexResponse = new GetIndexResponse( + new String[] { "some-index" }, + ImmutableOpenMap.of(), + ImmutableOpenMap.of(), + ImmutableOpenMap.of(), + ImmutableOpenMap.of(), + ImmutableOpenMap.of() + ); doAnswer(withResponse(getIndexResponse)).when(client).execute(eq(GetIndexAction.INSTANCE), any(), any()); IndicesStatsResponse indicesStatsResponse = mock(IndicesStatsResponse.class); when(indicesStatsResponse.getShards()).thenReturn(new ShardStats[0]); when(indicesStatsResponse.getFailedShards()).thenReturn(0); doAnswer(withResponse(indicesStatsResponse)).when(client).execute(eq(IndicesStatsAction.INSTANCE), any(), any()); - TransformConfig transformConfig = - newTransformConfigWithDateHistogram(transformId, dateHistogramField, dateHistogramInterval, delay); + TransformConfig transformConfig = newTransformConfigWithDateHistogram( + transformId, + dateHistogramField, + dateHistogramInterval, + delay + ); TimeBasedCheckpointProvider provider = newCheckpointProvider(transformConfig); SetOnce checkpointHolder = new SetOnce<>(); @@ -262,10 +278,12 @@ private TimeBasedCheckpointProvider newCheckpointProvider(TransformConfig transf ); } - private static TransformConfig newTransformConfigWithDateHistogram(String transformId, - String dateHistogramField, - TimeValue dateHistogramInterval, - TimeValue delay) { + private static TransformConfig newTransformConfigWithDateHistogram( + String transformId, + String dateHistogramField, + TimeValue dateHistogramInterval, + TimeValue delay + ) { DateHistogramGroupSource dateHistogramGroupSource = new DateHistogramGroupSource( dateHistogramField, null, @@ -273,22 +291,19 @@ private static TransformConfig newTransformConfigWithDateHistogram(String transf new DateHistogramGroupSource.FixedInterval(new DateHistogramInterval(dateHistogramInterval.getStringRep())), null ); - Supplier singleGroupSourceSupplier = - new Supplier<>() { - int groupCount = 0; - @Override - public SingleGroupSource get() { - return ++groupCount == 1 - ? dateHistogramGroupSource - : GroupConfigTests.randomSingleGroupSource(Version.CURRENT); - } - }; - PivotConfig pivotConfigWithDateHistogramSource = - new PivotConfig( - GroupConfigTests.randomGroupConfig(singleGroupSourceSupplier), - AggregationConfigTests.randomAggregationConfig(), - null // deprecated - ); + Supplier singleGroupSourceSupplier = new Supplier<>() { + int groupCount = 0; + + @Override + public SingleGroupSource get() { + return ++groupCount == 1 ? dateHistogramGroupSource : GroupConfigTests.randomSingleGroupSource(Version.CURRENT); + } + }; + PivotConfig pivotConfigWithDateHistogramSource = new PivotConfig( + GroupConfigTests.randomGroupConfig(singleGroupSourceSupplier), + AggregationConfigTests.randomAggregationConfig(), + null // deprecated + ); SettingsConfig.Builder settingsConfigBuilder = new SettingsConfig.Builder(); if (randomBoolean()) { settingsConfigBuilder.setAlignCheckpoints( @@ -296,15 +311,14 @@ public SingleGroupSource get() { // Set align_checkpoints setting explicitly to "true". ? true // Set align_checkpoints setting explicitly to "null". This will be interpreted as "true". - : null); + : null + ); } else { // Leave align_checkpoints setting unset. This will be interpreted as "true". } - return new TransformConfig.Builder(TransformConfigTests.randomTransformConfig(transformId)) - .setSettings(settingsConfigBuilder.build()) - .setPivotConfig(pivotConfigWithDateHistogramSource) - .setSyncConfig(new TimeSyncConfig(TIMESTAMP_FIELD, delay)) - .build(); + return new TransformConfig.Builder(TransformConfigTests.randomTransformConfig(transformId)).setSettings( + settingsConfigBuilder.build() + ).setPivotConfig(pivotConfigWithDateHistogramSource).setSyncConfig(new TimeSyncConfig(TIMESTAMP_FIELD, delay)).build(); } private static SearchResponse newSearchResponse(long totalHits) { diff --git a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/notifications/MockTransformAuditor.java b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/notifications/MockTransformAuditor.java index 9ca0a4471a6ff..d47fbabafeb01 100644 --- a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/notifications/MockTransformAuditor.java +++ b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/notifications/MockTransformAuditor.java @@ -156,7 +156,6 @@ public void assertMatched() { } } - private void audit(Level level, String resourceId, String message) { for (AuditExpectation expectation : expectations) { expectation.match(level, resourceId, message); diff --git a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/persistence/SeqNoPrimaryTermAndIndexTests.java b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/persistence/SeqNoPrimaryTermAndIndexTests.java index 852065b87be90..d76b6b67368f9 100644 --- a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/persistence/SeqNoPrimaryTermAndIndexTests.java +++ b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/persistence/SeqNoPrimaryTermAndIndexTests.java @@ -43,14 +43,18 @@ public void testFromIndexResponse() { long seqNo = randomLongBetween(-2, 10_000); long primaryTerm = randomLongBetween(-2, 10_000); String index = randomAlphaOfLength(10); - IndexResponse indexResponse = new IndexResponse(new ShardId(index, randomAlphaOfLength(10), 1), + IndexResponse indexResponse = new IndexResponse( + new ShardId(index, randomAlphaOfLength(10), 1), "asdf", seqNo, primaryTerm, 1, - randomBoolean()); + randomBoolean() + ); - assertThat(SeqNoPrimaryTermAndIndex.fromIndexResponse(indexResponse), - equalTo(new SeqNoPrimaryTermAndIndex(seqNo, primaryTerm, index))); + assertThat( + SeqNoPrimaryTermAndIndex.fromIndexResponse(indexResponse), + equalTo(new SeqNoPrimaryTermAndIndex(seqNo, primaryTerm, index)) + ); } } diff --git a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/persistence/TransformIndexTests.java b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/persistence/TransformIndexTests.java index 69de785175ea9..cf4aa12e6bcaf 100644 --- a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/persistence/TransformIndexTests.java +++ b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/persistence/TransformIndexTests.java @@ -11,9 +11,9 @@ import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; import org.elasticsearch.client.Client; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.transform.transforms.TransformConfigTests; import org.mockito.ArgumentCaptor; @@ -80,40 +80,43 @@ public void testCreateMappingsFromStringMap() { TransformIndex.createMappingsFromStringMap(singletonMap("a", "long")), is(equalTo(singletonMap("a", singletonMap("type", "long")))) ); - assertThat( - TransformIndex.createMappingsFromStringMap(new HashMap<>() {{ + assertThat(TransformIndex.createMappingsFromStringMap(new HashMap<>() { + { put("a", "long"); put("b", "keyword"); - }}), - is(equalTo(new HashMap<>() {{ + } + }), is(equalTo(new HashMap<>() { + { put("a", singletonMap("type", "long")); put("b", singletonMap("type", "keyword")); - }})) - ); - assertThat( - TransformIndex.createMappingsFromStringMap(new HashMap<>() {{ + } + }))); + assertThat(TransformIndex.createMappingsFromStringMap(new HashMap<>() { + { put("a", "long"); put("a.b", "keyword"); - }}), - is(equalTo(new HashMap<>() {{ + } + }), is(equalTo(new HashMap<>() { + { put("a", singletonMap("type", "long")); put("a.b", singletonMap("type", "keyword")); - }})) - ); - assertThat( - TransformIndex.createMappingsFromStringMap(new HashMap<>() {{ + } + }))); + assertThat(TransformIndex.createMappingsFromStringMap(new HashMap<>() { + { put("a", "long"); put("a.b", "text"); put("a.b.c", "keyword"); - }}), - is(equalTo(new HashMap<>() {{ + } + }), is(equalTo(new HashMap<>() { + { put("a", singletonMap("type", "long")); put("a.b", singletonMap("type", "text")); put("a.b.c", singletonMap("type", "keyword")); - }})) - ); - assertThat( - TransformIndex.createMappingsFromStringMap(new HashMap<>() {{ + } + }))); + assertThat(TransformIndex.createMappingsFromStringMap(new HashMap<>() { + { put("a", "object"); put("a.b", "long"); put("c", "nested"); @@ -122,8 +125,9 @@ public void testCreateMappingsFromStringMap() { put("f.g", "object"); put("f.g.h", "text"); put("f.g.h.i", "text"); - }}), - is(equalTo(new HashMap<>() {{ + } + }), is(equalTo(new HashMap<>() { + { put("a", singletonMap("type", "object")); put("a.b", singletonMap("type", "long")); put("c", singletonMap("type", "nested")); @@ -132,7 +136,7 @@ public void testCreateMappingsFromStringMap() { put("f.g", singletonMap("type", "object")); put("f.g.h", singletonMap("type", "text")); put("f.g.h.i", singletonMap("type", "text")); - }})) - ); + } + }))); } } diff --git a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/rest/action/RestDeleteTransformActionTests.java b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/rest/action/RestDeleteTransformActionTests.java index 167724c93cd48..0d72aaedaf091 100644 --- a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/rest/action/RestDeleteTransformActionTests.java +++ b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/rest/action/RestDeleteTransformActionTests.java @@ -9,12 +9,12 @@ import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.common.bytes.BytesArray; +import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.rest.FakeRestRequest; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.test.rest.FakeRestRequest; import static org.hamcrest.Matchers.equalTo; import static org.mockito.Mockito.mock; @@ -29,12 +29,14 @@ public void testBodyRejection() throws Exception { builder.field("id", "my_id"); } builder.endObject(); - final FakeRestRequest request = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY) - .withContent(new BytesArray(builder.toString()), XContentType.JSON) - .build(); + final FakeRestRequest request = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withContent( + new BytesArray(builder.toString()), + XContentType.JSON + ).build(); IllegalArgumentException e = expectThrows( - IllegalArgumentException.class, - () -> handler.prepareRequest(request, mock(NodeClient.class))); + IllegalArgumentException.class, + () -> handler.prepareRequest(request, mock(NodeClient.class)) + ); assertThat(e.getMessage(), equalTo("delete transform requests can not have a request body")); } } diff --git a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/ClientTransformIndexerTests.java b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/ClientTransformIndexerTests.java index 9c5070b801897..604a997cca1d9 100644 --- a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/ClientTransformIndexerTests.java +++ b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/ClientTransformIndexerTests.java @@ -105,11 +105,14 @@ public void testDoSearchGivenNoIndices() { ClientTransformIndexer indexer = createTestIndexer(); SearchRequest searchRequest = new SearchRequest(new String[0]); Tuple namedSearchRequest = new Tuple<>("test", searchRequest); - indexer.doSearch(namedSearchRequest, ActionListener.wrap( - // A search of zero indices should return null rather than attempt to search all indices - ESTestCase::assertNull, - e -> fail(e.getMessage()) - )); + indexer.doSearch( + namedSearchRequest, + ActionListener.wrap( + // A search of zero indices should return null rather than attempt to search all indices + ESTestCase::assertNull, + e -> fail(e.getMessage()) + ) + ); } public void testPitInjection() throws InterruptedException { diff --git a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/TransformIndexerTests.java b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/TransformIndexerTests.java index e0aa1ba0e4c1a..bbe9438286ce6 100644 --- a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/TransformIndexerTests.java +++ b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/TransformIndexerTests.java @@ -248,11 +248,7 @@ protected IterationResult doProcess(SearchResponse sea --numberOfLoops; // pretend that we processed 10k documents for each call getStats().incrementNumDocuments(10_000); - return new IterationResult<>( - Stream.of(new IndexRequest()), - new TransformIndexerPosition(null, null), - numberOfLoops == 0 - ); + return new IterationResult<>(Stream.of(new IndexRequest()), new TransformIndexerPosition(null, null), numberOfLoops == 0); } @Override diff --git a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/TransformNodesTests.java b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/TransformNodesTests.java index 0b5622542efef..529a078f39e5a 100644 --- a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/TransformNodesTests.java +++ b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/TransformNodesTests.java @@ -17,10 +17,10 @@ import org.elasticsearch.cluster.node.DiscoveryNodes; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.persistent.PersistentTaskParams; import org.elasticsearch.persistent.PersistentTasksCustomMetadata; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.transform.TransformField; import org.elasticsearch.xpack.core.transform.transforms.TransformTaskParams; @@ -221,22 +221,20 @@ public void testSelectAnyNodeThatCanRunThisTransform() { assertThat(TransformNodes.selectAnyNodeThatCanRunThisTransform(nodes, true), isEmpty()); assertThat(TransformNodes.selectAnyNodeThatCanRunThisTransform(nodes, false), isEmpty()); - nodes = - DiscoveryNodes.builder() - .add(newDiscoveryNode("node-1", Version.V_7_12_0, TRANSFORM_ROLE, REMOTE_CLUSTER_CLIENT_ROLE)) - .add(newDiscoveryNode("node-2", Version.V_7_13_0, TRANSFORM_ROLE)) - .add(newDiscoveryNode("node-3", Version.V_7_13_0, REMOTE_CLUSTER_CLIENT_ROLE)) - .build(); + nodes = DiscoveryNodes.builder() + .add(newDiscoveryNode("node-1", Version.V_7_12_0, TRANSFORM_ROLE, REMOTE_CLUSTER_CLIENT_ROLE)) + .add(newDiscoveryNode("node-2", Version.V_7_13_0, TRANSFORM_ROLE)) + .add(newDiscoveryNode("node-3", Version.V_7_13_0, REMOTE_CLUSTER_CLIENT_ROLE)) + .build(); assertThat(TransformNodes.selectAnyNodeThatCanRunThisTransform(nodes, true), isEmpty()); assertThat(TransformNodes.selectAnyNodeThatCanRunThisTransform(nodes, false).get().getId(), is(equalTo("node-2"))); - nodes = - DiscoveryNodes.builder() - .add(newDiscoveryNode("node-1", Version.V_7_12_0, TRANSFORM_ROLE, REMOTE_CLUSTER_CLIENT_ROLE)) - .add(newDiscoveryNode("node-2", Version.V_7_13_0, TRANSFORM_ROLE)) - .add(newDiscoveryNode("node-3", Version.V_7_13_0, REMOTE_CLUSTER_CLIENT_ROLE)) - .add(newDiscoveryNode("node-4", Version.V_7_13_0, TRANSFORM_ROLE, REMOTE_CLUSTER_CLIENT_ROLE)) - .build(); + nodes = DiscoveryNodes.builder() + .add(newDiscoveryNode("node-1", Version.V_7_12_0, TRANSFORM_ROLE, REMOTE_CLUSTER_CLIENT_ROLE)) + .add(newDiscoveryNode("node-2", Version.V_7_13_0, TRANSFORM_ROLE)) + .add(newDiscoveryNode("node-3", Version.V_7_13_0, REMOTE_CLUSTER_CLIENT_ROLE)) + .add(newDiscoveryNode("node-4", Version.V_7_13_0, TRANSFORM_ROLE, REMOTE_CLUSTER_CLIENT_ROLE)) + .build(); assertThat(TransformNodes.selectAnyNodeThatCanRunThisTransform(nodes, true).get().getId(), is(equalTo("node-4"))); assertThat(TransformNodes.selectAnyNodeThatCanRunThisTransform(nodes, false).get().getId(), is(oneOf("node-2", "node-4"))); } @@ -248,40 +246,31 @@ public void testHasAnyTransformNode() { expectThrows(ElasticsearchStatusException.class, () -> TransformNodes.throwIfNoTransformNodes(newClusterState(nodes))); } { - DiscoveryNodes nodes = - DiscoveryNodes.builder() - .add(newDiscoveryNode("node-1", Version.V_7_12_0)) - .add(newDiscoveryNode("node-2", Version.V_7_13_0)) - .add(newDiscoveryNode("node-3", Version.V_7_13_0)) - .build(); + DiscoveryNodes nodes = DiscoveryNodes.builder() + .add(newDiscoveryNode("node-1", Version.V_7_12_0)) + .add(newDiscoveryNode("node-2", Version.V_7_13_0)) + .add(newDiscoveryNode("node-3", Version.V_7_13_0)) + .build(); assertThat(TransformNodes.hasAnyTransformNode(nodes), is(false)); expectThrows(ElasticsearchStatusException.class, () -> TransformNodes.throwIfNoTransformNodes(newClusterState(nodes))); } { - DiscoveryNodes nodes = - DiscoveryNodes.builder() - .add(newDiscoveryNode("node-1", Version.V_7_12_0)) - .add(newDiscoveryNode("node-2", Version.V_7_13_0, TRANSFORM_ROLE)) - .add(newDiscoveryNode("node-3", Version.V_7_13_0, REMOTE_CLUSTER_CLIENT_ROLE)) - .add(newDiscoveryNode("node-4", Version.V_7_13_0)) - .build(); + DiscoveryNodes nodes = DiscoveryNodes.builder() + .add(newDiscoveryNode("node-1", Version.V_7_12_0)) + .add(newDiscoveryNode("node-2", Version.V_7_13_0, TRANSFORM_ROLE)) + .add(newDiscoveryNode("node-3", Version.V_7_13_0, REMOTE_CLUSTER_CLIENT_ROLE)) + .add(newDiscoveryNode("node-4", Version.V_7_13_0)) + .build(); assertThat(TransformNodes.hasAnyTransformNode(nodes), is(true)); TransformNodes.throwIfNoTransformNodes(newClusterState(nodes)); } } private static ClusterState newClusterState(DiscoveryNodes nodes) { - return ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)) - .nodes(nodes) - .build(); + return ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).nodes(nodes).build(); } private static DiscoveryNode newDiscoveryNode(String id, Version version, DiscoveryNodeRole... roles) { - return new DiscoveryNode( - id, - buildNewFakeTransportAddress(), - emptyMap(), - new HashSet<>(Arrays.asList(roles)), - version); + return new DiscoveryNode(id, buildNewFakeTransportAddress(), emptyMap(), new HashSet<>(Arrays.asList(roles)), version); } } diff --git a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/TransformPersistentTasksExecutorTests.java b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/TransformPersistentTasksExecutorTests.java index 1cd2b91ed9368..9a85e5c32d52e 100644 --- a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/TransformPersistentTasksExecutorTests.java +++ b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/TransformPersistentTasksExecutorTests.java @@ -62,18 +62,18 @@ public void testNodeVersionAssignment() { TransformPersistentTasksExecutor executor = buildTaskExecutor(); assertThat( - executor.getAssignment(new TransformTaskParams("new-task-id", Version.CURRENT, null, true), - cs.nodes().getAllNodes(), cs).getExecutorNode(), + executor.getAssignment(new TransformTaskParams("new-task-id", Version.CURRENT, null, true), cs.nodes().getAllNodes(), cs) + .getExecutorNode(), equalTo("current-data-node-with-1-tasks") ); assertThat( - executor.getAssignment(new TransformTaskParams("new-task-id", Version.CURRENT, null, false), - cs.nodes().getAllNodes(), cs).getExecutorNode(), + executor.getAssignment(new TransformTaskParams("new-task-id", Version.CURRENT, null, false), cs.nodes().getAllNodes(), cs) + .getExecutorNode(), equalTo("current-data-node-with-0-tasks-transform-remote-disabled") ); assertThat( - executor.getAssignment(new TransformTaskParams("new-old-task-id", Version.V_7_7_0, null, true), - cs.nodes().getAllNodes(), cs).getExecutorNode(), + executor.getAssignment(new TransformTaskParams("new-old-task-id", Version.V_7_7_0, null, true), cs.nodes().getAllNodes(), cs) + .getExecutorNode(), equalTo("past-data-node-1") ); } @@ -84,8 +84,11 @@ public void testNodeAssignmentProblems() { ClusterState cs = buildClusterState(nodes); TransformPersistentTasksExecutor executor = buildTaskExecutor(); - Assignment assignment = executor.getAssignment(new TransformTaskParams("new-task-id", Version.CURRENT, null, false), - cs.nodes().getAllNodes(), cs); + Assignment assignment = executor.getAssignment( + new TransformTaskParams("new-task-id", Version.CURRENT, null, false), + cs.nodes().getAllNodes(), + cs + ); assertNull(assignment.getExecutorNode()); assertThat( assignment.getExplanation(), @@ -97,8 +100,11 @@ public void testNodeAssignmentProblems() { cs = buildClusterState(nodes); executor = buildTaskExecutor(); - assignment = executor.getAssignment(new TransformTaskParams("new-task-id", Version.CURRENT, null, false), - cs.nodes().getAllNodes(), cs); + assignment = executor.getAssignment( + new TransformTaskParams("new-task-id", Version.CURRENT, null, false), + cs.nodes().getAllNodes(), + cs + ); assertNotNull(assignment.getExecutorNode()); assertThat(assignment.getExecutorNode(), equalTo("dedicated-transform-node")); @@ -107,8 +113,11 @@ public void testNodeAssignmentProblems() { cs = buildClusterState(nodes); executor = buildTaskExecutor(); - assignment = executor.getAssignment(new TransformTaskParams("new-task-id", Version.V_8_0_0, null, false), - cs.nodes().getAllNodes(), cs); + assignment = executor.getAssignment( + new TransformTaskParams("new-task-id", Version.V_8_0_0, null, false), + cs.nodes().getAllNodes(), + cs + ); assertNull(assignment.getExecutorNode()); assertThat( assignment.getExplanation(), @@ -121,8 +130,11 @@ public void testNodeAssignmentProblems() { ) ); - assignment = executor.getAssignment(new TransformTaskParams("new-task-id", Version.V_7_5_0, null, false), - cs.nodes().getAllNodes(), cs); + assignment = executor.getAssignment( + new TransformTaskParams("new-task-id", Version.V_7_5_0, null, false), + cs.nodes().getAllNodes(), + cs + ); assertNotNull(assignment.getExecutorNode()); assertThat(assignment.getExecutorNode(), equalTo("past-data-node-1")); @@ -131,8 +143,11 @@ public void testNodeAssignmentProblems() { cs = buildClusterState(nodes); executor = buildTaskExecutor(); - assignment = executor.getAssignment(new TransformTaskParams("new-task-id", Version.V_7_5_0, null, true), - cs.nodes().getAllNodes(), cs); + assignment = executor.getAssignment( + new TransformTaskParams("new-task-id", Version.V_7_5_0, null, true), + cs.nodes().getAllNodes(), + cs + ); assertNull(assignment.getExecutorNode()); assertThat( assignment.getExplanation(), @@ -144,8 +159,11 @@ public void testNodeAssignmentProblems() { ) ); - assignment = executor.getAssignment(new TransformTaskParams("new-task-id", Version.CURRENT, null, false), - cs.nodes().getAllNodes(), cs); + assignment = executor.getAssignment( + new TransformTaskParams("new-task-id", Version.CURRENT, null, false), + cs.nodes().getAllNodes(), + cs + ); assertNotNull(assignment.getExecutorNode()); assertThat(assignment.getExecutorNode(), equalTo("current-data-node-with-0-tasks-transform-remote-disabled")); @@ -154,8 +172,11 @@ public void testNodeAssignmentProblems() { cs = buildClusterState(nodes); executor = buildTaskExecutor(); - assignment = executor.getAssignment(new TransformTaskParams("new-task-id", Version.V_7_5_0, null, true), - cs.nodes().getAllNodes(), cs); + assignment = executor.getAssignment( + new TransformTaskParams("new-task-id", Version.V_7_5_0, null, true), + cs.nodes().getAllNodes(), + cs + ); assertNull(assignment.getExecutorNode()); assertThat( assignment.getExplanation(), @@ -173,8 +194,11 @@ public void testNodeAssignmentProblems() { cs = buildClusterState(nodes); executor = buildTaskExecutor(); - assignment = executor.getAssignment(new TransformTaskParams("new-task-id", Version.V_7_5_0, null, true), - cs.nodes().getAllNodes(), cs); + assignment = executor.getAssignment( + new TransformTaskParams("new-task-id", Version.V_7_5_0, null, true), + cs.nodes().getAllNodes(), + cs + ); assertNotNull(assignment.getExecutorNode()); assertThat(assignment.getExecutorNode(), equalTo("past-data-node-1")); } diff --git a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/common/DocumentConversionUtilsTests.java b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/common/DocumentConversionUtilsTests.java index eb583f2f3ce81..dbdfee697c9c0 100644 --- a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/common/DocumentConversionUtilsTests.java +++ b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/common/DocumentConversionUtilsTests.java @@ -57,10 +57,10 @@ public class DocumentConversionUtilsTests extends ESTestCase { ); public void testConvertDocumentToIndexRequest_MissingId() { - Exception e = - expectThrows( - Exception.class, - () -> DocumentConversionUtils.convertDocumentToIndexRequest(null, Collections.emptyMap(), INDEX, PIPELINE)); + Exception e = expectThrows( + Exception.class, + () -> DocumentConversionUtils.convertDocumentToIndexRequest(null, Collections.emptyMap(), INDEX, PIPELINE) + ); assertThat(e.getMessage(), is(equalTo("Expected a document id but got null."))); } @@ -93,30 +93,39 @@ public void testRemoveInternalFields() { } public void testExtractFieldMappings() { - FieldCapabilitiesResponse response = - new FieldCapabilitiesResponse( - new String[] { "some-index" }, - new HashMap<>() {{ - put("field-1", new HashMap<>() {{ + FieldCapabilitiesResponse response = new FieldCapabilitiesResponse(new String[] { "some-index" }, new HashMap<>() { + { + put("field-1", new HashMap<>() { + { put("keyword", createFieldCapabilities("field-1", "keyword")); - }}); - put("field-2", new HashMap<>() {{ + } + }); + put("field-2", new HashMap<>() { + { put("long", createFieldCapabilities("field-2", "long")); put("keyword", createFieldCapabilities("field-2", "keyword")); - }}); - }}); + } + }); + } + }); assertThat( DocumentConversionUtils.extractFieldMappings(response), - allOf( - hasEntry("field-1", "keyword"), - hasEntry(is(equalTo("field-2")), is(oneOf("long", "keyword"))) - ) + allOf(hasEntry("field-1", "keyword"), hasEntry(is(equalTo("field-2")), is(oneOf("long", "keyword")))) ); } private static FieldCapabilities createFieldCapabilities(String name, String type) { return new FieldCapabilities( - name, type, false, true, true, Strings.EMPTY_ARRAY, Strings.EMPTY_ARRAY, Strings.EMPTY_ARRAY, Collections.emptyMap()); + name, + type, + false, + true, + true, + Strings.EMPTY_ARRAY, + Strings.EMPTY_ARRAY, + Strings.EMPTY_ARRAY, + Collections.emptyMap() + ); } } diff --git a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/latest/LatestChangeCollectorTests.java b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/latest/LatestChangeCollectorTests.java index 5c86325816a79..69b1d5a96412e 100644 --- a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/latest/LatestChangeCollectorTests.java +++ b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/latest/LatestChangeCollectorTests.java @@ -40,7 +40,7 @@ public void testBuildFilterQuery() { ); } - @AwaitsFix(bugUrl="https://github.com/elastic/elasticsearch/issues/77329") + @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/77329") public void testGetIndicesToQuery() { LatestChangeCollector changeCollector = new LatestChangeCollector("timestamp"); diff --git a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/latest/LatestTests.java b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/latest/LatestTests.java index 9c693bdcf96b5..0104e27fd0a22 100644 --- a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/latest/LatestTests.java +++ b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/latest/LatestTests.java @@ -23,10 +23,7 @@ public class LatestTests extends ESTestCase { public void testValidateConfig() { LatestConfig latestConfig = LatestConfigTests.randomLatestConfig(); Function latest = new Latest(latestConfig); - latest.validateConfig( - ActionListener.wrap( - isValid -> assertThat(isValid, is(true)), - e -> fail(e.getMessage()))); + latest.validateConfig(ActionListener.wrap(isValid -> assertThat(isValid, is(true)), e -> fail(e.getMessage()))); } public void testGetPerformanceCriticalFields() { diff --git a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/pivot/AggregationResultUtilsTests.java b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/pivot/AggregationResultUtilsTests.java index 790ce63d45f35..339b368371c73 100644 --- a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/pivot/AggregationResultUtilsTests.java +++ b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/pivot/AggregationResultUtilsTests.java @@ -9,14 +9,6 @@ import org.elasticsearch.common.geo.GeoPoint; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.xcontent.ContextParser; -import org.elasticsearch.xcontent.DeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.script.Script; import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.AggregationBuilder; @@ -64,6 +56,14 @@ import org.elasticsearch.search.aggregations.pipeline.ParsedStatsBucket; import org.elasticsearch.search.aggregations.pipeline.StatsBucketPipelineAggregationBuilder; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.ContextParser; +import org.elasticsearch.xcontent.DeprecationHandler; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.transform.TransformField; import org.elasticsearch.xpack.core.transform.transforms.TransformIndexerStats; import org.elasticsearch.xpack.core.transform.transforms.TransformProgress; diff --git a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/pivot/AggregationSchemaAndResultTests.java b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/pivot/AggregationSchemaAndResultTests.java index 4e9b2d5f69c3c..c8ec775408763 100644 --- a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/pivot/AggregationSchemaAndResultTests.java +++ b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/pivot/AggregationSchemaAndResultTests.java @@ -88,10 +88,7 @@ protected void fieldCaps.put( field, - Collections.singletonMap( - type, - new FieldCapabilities(field, type, false, true, true, null, null, null, emptyMap()) - ) + Collections.singletonMap(type, new FieldCapabilities(field, type, false, true, true, null, null, null, emptyMap())) ); } diff --git a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/pivot/SchemaUtilTests.java b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/pivot/SchemaUtilTests.java index d406393b8b15a..5798863268fdd 100644 --- a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/pivot/SchemaUtilTests.java +++ b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/pivot/SchemaUtilTests.java @@ -107,8 +107,13 @@ public void testGetSourceFieldMappings() throws InterruptedException { // fields is empty this.>assertAsync( - listener -> - SchemaUtil.getSourceFieldMappings(client, new String[] { "index-1", "index-2" }, new String[] {}, emptyMap(), listener), + listener -> SchemaUtil.getSourceFieldMappings( + client, + new String[] { "index-1", "index-2" }, + new String[] {}, + emptyMap(), + listener + ), mappings -> { assertNotNull(mappings); assertTrue(mappings.isEmpty()); @@ -126,8 +131,13 @@ public void testGetSourceFieldMappings() throws InterruptedException { // indices is empty this.>assertAsync( - listener -> - SchemaUtil.getSourceFieldMappings(client, new String[] {}, new String[] { "field-1", "field-2" }, emptyMap(), listener), + listener -> SchemaUtil.getSourceFieldMappings( + client, + new String[] {}, + new String[] { "field-1", "field-2" }, + emptyMap(), + listener + ), mappings -> { assertNotNull(mappings); assertTrue(mappings.isEmpty()); @@ -154,10 +164,12 @@ public void testGetSourceFieldMappings() throws InterruptedException { } public void testGetSourceFieldMappingsWithRuntimeMappings() throws InterruptedException { - Map runtimeMappings = new HashMap<>() {{ - put("field-2", singletonMap("type", "keyword")); - put("field-3", singletonMap("type", "boolean")); - }}; + Map runtimeMappings = new HashMap<>() { + { + put("field-2", singletonMap("type", "keyword")); + put("field-3", singletonMap("type", "boolean")); + } + }; try (Client client = new FieldCapsMockClient(getTestName())) { this.>assertAsync( listener -> SchemaUtil.getSourceFieldMappings( @@ -171,7 +183,8 @@ public void testGetSourceFieldMappingsWithRuntimeMappings() throws InterruptedEx assertThat(mappings, is(aMapWithSize(3))); assertThat( mappings, - allOf(hasEntry("field-1", "long"), hasEntry("field-2", "keyword"), hasEntry("field-3", "boolean"))); + allOf(hasEntry("field-1", "long"), hasEntry("field-2", "keyword"), hasEntry("field-3", "boolean")) + ); } ); } @@ -197,7 +210,7 @@ protected void } for (Map.Entry runtimeField : fieldCapsRequest.runtimeFields().entrySet()) { String field = runtimeField.getKey(); - String type = (String)((Map) runtimeField.getValue()).get("type"); + String type = (String) ((Map) runtimeField.getValue()).get("type"); responseMap.put(field, singletonMap(field, createFieldCapabilities(field, type))); } diff --git a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/pivot/TransformAggregationsTests.java b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/pivot/TransformAggregationsTests.java index 7137718c55ea6..609c2cce4d758 100644 --- a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/pivot/TransformAggregationsTests.java +++ b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/pivot/TransformAggregationsTests.java @@ -7,9 +7,9 @@ package org.elasticsearch.xpack.transform.transforms.pivot; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.Tuple; import org.elasticsearch.index.query.TermQueryBuilder; import org.elasticsearch.search.SearchModule; import org.elasticsearch.search.aggregations.AggregationBuilder; diff --git a/x-pack/plugin/vector-tile/qa/multi-cluster/src/test/java/org/elasticsearch/vectortile/VectorTileCCSIT.java b/x-pack/plugin/vector-tile/qa/multi-cluster/src/test/java/org/elasticsearch/vectortile/VectorTileCCSIT.java index 554d00db7a61a..df35d47257a28 100644 --- a/x-pack/plugin/vector-tile/qa/multi-cluster/src/test/java/org/elasticsearch/vectortile/VectorTileCCSIT.java +++ b/x-pack/plugin/vector-tile/qa/multi-cluster/src/test/java/org/elasticsearch/vectortile/VectorTileCCSIT.java @@ -30,9 +30,7 @@ public class VectorTileCCSIT extends ESRestTestCase { @Override protected Settings restClientSettings() { final String token = basicAuthHeaderValue("admin", new SecureString("admin-password".toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } private int createIndex(RestClient client, String indexName) throws IOException { @@ -41,13 +39,7 @@ private int createIndex(RestClient client, String indexName) throws IOException assertThat(response.getStatusLine().getStatusCode(), Matchers.equalTo(HttpStatus.SC_OK)); final Request mappingRequest = new Request(HttpPut.METHOD_NAME, indexName + "/_mapping"); mappingRequest.setJsonEntity( - "{\n" - + " \"properties\": {\n" - + " \"location\": {\n" - + " \"type\": \"geo_shape\"\n" - + " }\n" - + " }\n" - + "}" + "{\n" + " \"properties\": {\n" + " \"location\": {\n" + " \"type\": \"geo_shape\"\n" + " }\n" + " }\n" + "}" ); response = client.performRequest(mappingRequest); assertThat(response.getStatusLine().getStatusCode(), Matchers.equalTo(HttpStatus.SC_OK)); @@ -113,8 +105,11 @@ private RestClient buildRemoteClusterClient() throws IOException { private RestClient buildClient(final String url) throws IOException { final int portSeparator = url.lastIndexOf(':'); - final HttpHost httpHost = new HttpHost(url.substring(0, portSeparator), - Integer.parseInt(url.substring(portSeparator + 1)), getProtocol()); - return buildClient(restAdminSettings(), new HttpHost[]{httpHost}); + final HttpHost httpHost = new HttpHost( + url.substring(0, portSeparator), + Integer.parseInt(url.substring(portSeparator + 1)), + getProtocol() + ); + return buildClient(restAdminSettings(), new HttpHost[] { httpHost }); } } diff --git a/x-pack/plugin/vectors/src/internalClusterTest/java/org/elasticsearch/xpack/vectors/mapper/SparseVectorFieldMapperTests.java b/x-pack/plugin/vectors/src/internalClusterTest/java/org/elasticsearch/xpack/vectors/mapper/SparseVectorFieldMapperTests.java index 56879f09e7720..d24e52593d993 100644 --- a/x-pack/plugin/vectors/src/internalClusterTest/java/org/elasticsearch/xpack/vectors/mapper/SparseVectorFieldMapperTests.java +++ b/x-pack/plugin/vectors/src/internalClusterTest/java/org/elasticsearch/xpack/vectors/mapper/SparseVectorFieldMapperTests.java @@ -5,7 +5,6 @@ * 2.0. */ - package org.elasticsearch.xpack.vectors.mapper; import org.elasticsearch.Version; @@ -13,8 +12,6 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.compress.CompressedXContent; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.IndexService; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.MappedFieldType; @@ -25,6 +22,8 @@ import org.elasticsearch.plugins.Plugin; import org.elasticsearch.test.ESSingleNodeTestCase; import org.elasticsearch.test.VersionUtils; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.LocalStateCompositeXPackPlugin; import org.elasticsearch.xpack.vectors.DenseVectorPlugin; @@ -48,72 +47,82 @@ protected boolean forbidPrivateIndexSettings() { public void testValueFetcherIsNotSupported() { SparseVectorFieldMapper.Builder builder = new SparseVectorFieldMapper.Builder("field"); MappedFieldType fieldMapper = builder.build(MapperBuilderContext.ROOT).fieldType(); - UnsupportedOperationException exc = expectThrows(UnsupportedOperationException.class, - () -> fieldMapper.valueFetcher(null, null)); + UnsupportedOperationException exc = expectThrows(UnsupportedOperationException.class, () -> fieldMapper.valueFetcher(null, null)); assertEquals(SparseVectorFieldMapper.ERROR_MESSAGE_7X, exc.getMessage()); } public void testSparseVectorWith8xIndex() throws Exception { Version version = VersionUtils.randomVersionBetween(random(), Version.V_8_0_0, Version.CURRENT); - Settings settings = Settings.builder() - .put(IndexMetadata.SETTING_VERSION_CREATED, version) - .build(); + Settings settings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, version).build(); IndexService indexService = createIndex("index", settings); MapperService mapperService = indexService.mapperService(); - BytesReference mapping = BytesReference.bytes(XContentFactory.jsonBuilder() - .startObject() + BytesReference mapping = BytesReference.bytes( + XContentFactory.jsonBuilder() + .startObject() .startObject("_doc") - .startObject("properties") - .startObject("my-vector").field("type", "sparse_vector") - .endObject() - .endObject() + .startObject("properties") + .startObject("my-vector") + .field("type", "sparse_vector") + .endObject() + .endObject() .endObject() - .endObject()); + .endObject() + ); - MapperParsingException e = expectThrows(MapperParsingException.class, () -> - mapperService.parseMapping(MapperService.SINGLE_MAPPING_NAME, new CompressedXContent(mapping))); + MapperParsingException e = expectThrows( + MapperParsingException.class, + () -> mapperService.parseMapping(MapperService.SINGLE_MAPPING_NAME, new CompressedXContent(mapping)) + ); assertThat(e.getMessage(), containsString(SparseVectorFieldMapper.ERROR_MESSAGE)); } public void testSparseVectorWith7xIndex() throws Exception { Version version = VersionUtils.randomPreviousCompatibleVersion(random(), Version.V_8_0_0); - Settings settings = Settings.builder() - .put(IndexMetadata.SETTING_VERSION_CREATED, version) - .build(); + Settings settings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, version).build(); IndexService indexService = createIndex("index", settings); MapperService mapperService = indexService.mapperService(); - BytesReference mapping = BytesReference.bytes(XContentFactory.jsonBuilder() - .startObject() + BytesReference mapping = BytesReference.bytes( + XContentFactory.jsonBuilder() + .startObject() .startObject("_doc") - .startObject("properties") - .startObject("my-vector").field("type", "sparse_vector") - .endObject() - .endObject() + .startObject("properties") + .startObject("my-vector") + .field("type", "sparse_vector") + .endObject() + .endObject() .endObject() - .endObject()); + .endObject() + ); - DocumentMapper mapper = mapperService.merge(MapperService.SINGLE_MAPPING_NAME, new CompressedXContent(mapping), - MapperService.MergeReason.MAPPING_UPDATE); + DocumentMapper mapper = mapperService.merge( + MapperService.SINGLE_MAPPING_NAME, + new CompressedXContent(mapping), + MapperService.MergeReason.MAPPING_UPDATE + ); assertWarnings(SparseVectorFieldMapper.ERROR_MESSAGE_7X); // Check that new vectors cannot be indexed. - int[] indexedDims = {65535, 50, 2}; - float[] indexedValues = {0.5f, 1800f, -34567.11f}; - BytesReference source = BytesReference.bytes(XContentFactory.jsonBuilder() + int[] indexedDims = { 65535, 50, 2 }; + float[] indexedValues = { 0.5f, 1800f, -34567.11f }; + BytesReference source = BytesReference.bytes( + XContentFactory.jsonBuilder() .startObject() - .startObject("my-vector") - .field(Integer.toString(indexedDims[0]), indexedValues[0]) - .field(Integer.toString(indexedDims[1]), indexedValues[1]) - .field(Integer.toString(indexedDims[2]), indexedValues[2]) - .endObject() - .endObject()); - - MapperParsingException indexException = expectThrows(MapperParsingException.class, () -> - mapper.parse(new SourceToParse("index", "id", source, XContentType.JSON))); + .startObject("my-vector") + .field(Integer.toString(indexedDims[0]), indexedValues[0]) + .field(Integer.toString(indexedDims[1]), indexedValues[1]) + .field(Integer.toString(indexedDims[2]), indexedValues[2]) + .endObject() + .endObject() + ); + + MapperParsingException indexException = expectThrows( + MapperParsingException.class, + () -> mapper.parse(new SourceToParse("index", "id", source, XContentType.JSON)) + ); assertThat(indexException.getCause().getMessage(), containsString(SparseVectorFieldMapper.ERROR_MESSAGE)); } } diff --git a/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/DenseVectorPlugin.java b/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/DenseVectorPlugin.java index 201c06a76fda4..c0dda2dc5fa54 100644 --- a/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/DenseVectorPlugin.java +++ b/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/DenseVectorPlugin.java @@ -33,7 +33,7 @@ public class DenseVectorPlugin extends Plugin implements ActionPlugin, MapperPlugin, SearchPlugin { - public DenseVectorPlugin() { } + public DenseVectorPlugin() {} @Override public Map getMappers() { @@ -59,9 +59,14 @@ public List getRestHandlers( @Override public List> getQueries() { // This query is only meant to be used internally, and not passed to the _search endpoint - return List.of(new QuerySpec<>(KnnVectorQueryBuilder.NAME, KnnVectorQueryBuilder::new, - parser -> { - throw new IllegalArgumentException("[knn] queries cannot be provided directly, use the [_knn_search] endpoint instead"); - })); + return List.of( + new QuerySpec<>( + KnnVectorQueryBuilder.NAME, + KnnVectorQueryBuilder::new, + parser -> { + throw new IllegalArgumentException("[knn] queries cannot be provided directly, use the [_knn_search] endpoint instead"); + } + ) + ); } } diff --git a/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/action/KnnSearchRequestBuilder.java b/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/action/KnnSearchRequestBuilder.java index b69d2f3f2ed3b..a23f31a401376 100644 --- a/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/action/KnnSearchRequestBuilder.java +++ b/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/action/KnnSearchRequestBuilder.java @@ -41,25 +41,38 @@ class KnnSearchRequestBuilder { static { PARSER = new ObjectParser<>("knn-search"); - PARSER.declareField(KnnSearchRequestBuilder::knnSearch, KnnSearch::parse, - KNN_SECTION_FIELD, ObjectParser.ValueType.OBJECT); - PARSER.declareField((p, request, c) -> request.fetchSource(FetchSourceContext.fromXContent(p)), - SearchSourceBuilder._SOURCE_FIELD, ObjectParser.ValueType.OBJECT_ARRAY_BOOLEAN_OR_STRING); - PARSER.declareFieldArray(KnnSearchRequestBuilder::fields, (p, c) -> FieldAndFormat.fromXContent(p), - SearchSourceBuilder.FETCH_FIELDS_FIELD, ObjectParser.ValueType.OBJECT_ARRAY); - PARSER.declareFieldArray(KnnSearchRequestBuilder::docValueFields, (p, c) -> FieldAndFormat.fromXContent(p), - SearchSourceBuilder.DOCVALUE_FIELDS_FIELD, ObjectParser.ValueType.OBJECT_ARRAY); - PARSER.declareField((p, request, c) -> request.storedFields( - StoredFieldsContext.fromXContent(SearchSourceBuilder.STORED_FIELDS_FIELD.getPreferredName(), p)), - SearchSourceBuilder.STORED_FIELDS_FIELD, ObjectParser.ValueType.STRING_ARRAY); + PARSER.declareField(KnnSearchRequestBuilder::knnSearch, KnnSearch::parse, KNN_SECTION_FIELD, ObjectParser.ValueType.OBJECT); + PARSER.declareField( + (p, request, c) -> request.fetchSource(FetchSourceContext.fromXContent(p)), + SearchSourceBuilder._SOURCE_FIELD, + ObjectParser.ValueType.OBJECT_ARRAY_BOOLEAN_OR_STRING + ); + PARSER.declareFieldArray( + KnnSearchRequestBuilder::fields, + (p, c) -> FieldAndFormat.fromXContent(p), + SearchSourceBuilder.FETCH_FIELDS_FIELD, + ObjectParser.ValueType.OBJECT_ARRAY + ); + PARSER.declareFieldArray( + KnnSearchRequestBuilder::docValueFields, + (p, c) -> FieldAndFormat.fromXContent(p), + SearchSourceBuilder.DOCVALUE_FIELDS_FIELD, + ObjectParser.ValueType.OBJECT_ARRAY + ); + PARSER.declareField( + (p, request, c) -> request.storedFields( + StoredFieldsContext.fromXContent(SearchSourceBuilder.STORED_FIELDS_FIELD.getPreferredName(), p) + ), + SearchSourceBuilder.STORED_FIELDS_FIELD, + ObjectParser.ValueType.STRING_ARRAY + ); } /** * Parses a {@link RestRequest} representing a kNN search into a request builder. */ static KnnSearchRequestBuilder parseRestRequest(RestRequest restRequest) throws IOException { - KnnSearchRequestBuilder builder = new KnnSearchRequestBuilder( - Strings.splitStringByCommaToArray(restRequest.param("index"))); + KnnSearchRequestBuilder builder = new KnnSearchRequestBuilder(Strings.splitStringByCommaToArray(restRequest.param("index"))); builder.routing(restRequest.param("routing")); if (restRequest.hasContentOrSourceParam()) { @@ -215,8 +228,9 @@ void build(SearchSourceBuilder builder) { throw new IllegalArgumentException("[" + K_FIELD.getPreferredName() + "] must be greater than 0"); } if (numCands < k) { - throw new IllegalArgumentException("[" + NUM_CANDS_FIELD.getPreferredName() + "] cannot be less than " + - "[" + K_FIELD.getPreferredName() + "]"); + throw new IllegalArgumentException( + "[" + NUM_CANDS_FIELD.getPreferredName() + "] cannot be less than " + "[" + K_FIELD.getPreferredName() + "]" + ); } if (numCands > NUM_CANDS_LIMIT) { throw new IllegalArgumentException("[" + NUM_CANDS_FIELD.getPreferredName() + "] cannot exceed [" + NUM_CANDS_LIMIT + "]"); @@ -231,8 +245,10 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; KnnSearch that = (KnnSearch) o; - return k == that.k && numCands == that.numCands - && Objects.equals(field, that.field) && Arrays.equals(queryVector, that.queryVector); + return k == that.k + && numCands == that.numCands + && Objects.equals(field, that.field) + && Arrays.equals(queryVector, that.queryVector); } @Override diff --git a/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/action/RestKnnSearchAction.java b/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/action/RestKnnSearchAction.java index befe73d225865..01857cbf40002 100644 --- a/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/action/RestKnnSearchAction.java +++ b/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/action/RestKnnSearchAction.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.xpack.vectors.action; - import org.elasticsearch.action.search.SearchRequestBuilder; import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.rest.BaseRestHandler; @@ -30,9 +29,7 @@ public RestKnnSearchAction() {} @Override public List routes() { - return List.of( - new Route(GET, "{index}/_knn_search"), - new Route(POST, "{index}/_knn_search")); + return List.of(new Route(GET, "{index}/_knn_search"), new Route(POST, "{index}/_knn_search")); } @Override diff --git a/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/mapper/DenseVectorFieldMapper.java b/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/mapper/DenseVectorFieldMapper.java index 43cf03f704bde..70e533831f432 100644 --- a/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/mapper/DenseVectorFieldMapper.java +++ b/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/mapper/DenseVectorFieldMapper.java @@ -5,7 +5,6 @@ * 2.0. */ - package org.elasticsearch.xpack.vectors.mapper; import org.apache.lucene.codecs.KnnVectorsFormat; @@ -59,7 +58,7 @@ public class DenseVectorFieldMapper extends FieldMapper implements PerFieldKnnVectorsFormatFieldMapper { public static final String CONTENT_TYPE = "dense_vector"; - public static short MAX_DIMS_COUNT = 2048; //maximum allowed number of dimensions + public static short MAX_DIMS_COUNT = 2048; // maximum allowed number of dimensions private static final byte INT_BYTES = 4; private static DenseVectorFieldMapper toType(FieldMapper in) { @@ -67,23 +66,44 @@ private static DenseVectorFieldMapper toType(FieldMapper in) { } public static class Builder extends FieldMapper.Builder { - private final Parameter dims - = new Parameter<>("dims", false, () -> null, (n, c, o) -> XContentMapValues.nodeIntegerValue(o), m -> toType(m).dims) - .addValidator(dims -> { - if (dims == null) { - throw new MapperParsingException("Missing required parameter [dims] for field [" + name + "]"); - } - if ((dims > MAX_DIMS_COUNT) || (dims < 1)) { - throw new MapperParsingException("The number of dimensions for field [" + name + - "] should be in the range [1, " + MAX_DIMS_COUNT + "] but was [" + dims + "]"); - } - }); + private final Parameter dims = new Parameter<>( + "dims", + false, + () -> null, + (n, c, o) -> XContentMapValues.nodeIntegerValue(o), + m -> toType(m).dims + ).addValidator(dims -> { + if (dims == null) { + throw new MapperParsingException("Missing required parameter [dims] for field [" + name + "]"); + } + if ((dims > MAX_DIMS_COUNT) || (dims < 1)) { + throw new MapperParsingException( + "The number of dimensions for field [" + + name + + "] should be in the range [1, " + + MAX_DIMS_COUNT + + "] but was [" + + dims + + "]" + ); + } + }); private final Parameter indexed = Parameter.indexParam(m -> toType(m).indexed, false); private final Parameter similarity = Parameter.enumParam( - "similarity", false, m -> toType(m).similarity, null, VectorSimilarity.class); - private final Parameter indexOptions = new Parameter<>("index_options", false, () -> null, - (n, c, o) -> o == null ? null : parseIndexOptions(n, o), m -> toType(m).indexOptions); + "similarity", + false, + m -> toType(m).similarity, + null, + VectorSimilarity.class + ); + private final Parameter indexOptions = new Parameter<>( + "index_options", + false, + () -> null, + (n, c, o) -> o == null ? null : parseIndexOptions(n, o), + m -> toType(m).indexOptions + ); private final Parameter> meta = Parameter.metaParam(); final Version indexVersionCreated; @@ -108,15 +128,22 @@ protected List> getParameters() { public DenseVectorFieldMapper build(MapperBuilderContext context) { return new DenseVectorFieldMapper( name, - new DenseVectorFieldType(context.buildFullName(name), indexVersionCreated, - dims.getValue(), indexed.getValue(), similarity.getValue(), meta.getValue()), + new DenseVectorFieldType( + context.buildFullName(name), + indexVersionCreated, + dims.getValue(), + indexed.getValue(), + similarity.getValue(), + meta.getValue() + ), dims.getValue(), indexed.getValue(), similarity.getValue(), indexOptions.getValue(), indexVersionCreated, multiFieldsBuilder.build(this, context), - copyTo.build()); + copyTo.build() + ); } } @@ -126,6 +153,7 @@ enum VectorSimilarity { dot_product(VectorSimilarityFunction.DOT_PRODUCT); public final VectorSimilarityFunction function; + VectorSimilarity(VectorSimilarityFunction function) { this.function = function; } @@ -133,6 +161,7 @@ enum VectorSimilarity { private abstract static class IndexOptions implements ToXContent { final String type; + IndexOptions(String type) { this.type = type; } @@ -192,8 +221,10 @@ public String toString() { } } - public static final TypeParser PARSER - = new TypeParser((n, c) -> new Builder(n, c.indexVersionCreated()), notInMultiFields(CONTENT_TYPE)); + public static final TypeParser PARSER = new TypeParser( + (n, c) -> new Builder(n, c.indexVersionCreated()), + notInMultiFields(CONTENT_TYPE) + ); public static final class DenseVectorFieldType extends SimpleMappedFieldType { private final int dims; @@ -201,8 +232,14 @@ public static final class DenseVectorFieldType extends SimpleMappedFieldType { private final VectorSimilarity similarity; private final Version indexVersionCreated; - public DenseVectorFieldType(String name, Version indexVersionCreated, int dims,boolean indexed, - VectorSimilarity similarity, Map meta) { + public DenseVectorFieldType( + String name, + Version indexVersionCreated, + int dims, + boolean indexed, + VectorSimilarity similarity, + Map meta + ) { super(name, indexed, false, indexed == false, TextSearchInfo.NONE, meta); this.dims = dims; this.indexed = indexed; @@ -231,7 +268,8 @@ protected Object parseSourceValue(Object value) { @Override public DocValueFormat docValueFormat(String format, ZoneId timeZone) { throw new IllegalArgumentException( - "Field [" + name() + "] of type [" + typeName() + "] doesn't support docvalue_fields or aggregations"); + "Field [" + name() + "] of type [" + typeName() + "] doesn't support docvalue_fields or aggregations" + ); } @Override @@ -256,19 +294,20 @@ public Query existsQuery(SearchExecutionContext context) { @Override public Query termQuery(Object value, SearchExecutionContext context) { - throw new IllegalArgumentException( - "Field [" + name() + "] of type [" + typeName() + "] doesn't support queries"); + throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support queries"); } public KnnVectorQuery createKnnQuery(float[] queryVector, int numCands) { if (isSearchable() == false) { - throw new IllegalArgumentException("[" + KnnVectorQueryBuilder.NAME + "] " + - "queries are not supported if [index] is disabled"); + throw new IllegalArgumentException( + "[" + KnnVectorQueryBuilder.NAME + "] " + "queries are not supported if [index] is disabled" + ); } if (queryVector.length != dims) { - throw new IllegalArgumentException("the query vector has a different dimension [" + queryVector.length + "] " - + "than the index vectors [" + dims + "]"); + throw new IllegalArgumentException( + "the query vector has a different dimension [" + queryVector.length + "] " + "than the index vectors [" + dims + "]" + ); } if (similarity == VectorSimilarity.dot_product) { @@ -284,8 +323,12 @@ public KnnVectorQuery createKnnQuery(float[] queryVector, int numCands) { private void checkVectorMagnitude(float[] vector, double squaredMagnitude) { if (Math.abs(squaredMagnitude - 1.0f) > 1e-4) { // Include the first five elements of the invalid vector in the error message - StringBuilder sb = new StringBuilder("The [" + VectorSimilarity.dot_product.name() + "] similarity can " + - "only be used with unit-length vectors. Preview of invalid vector: "); + StringBuilder sb = new StringBuilder( + "The [" + + VectorSimilarity.dot_product.name() + + "] similarity can " + + "only be used with unit-length vectors. Preview of invalid vector: " + ); sb.append("["); for (int i = 0; i < Math.min(5, vector.length); i++) { if (i > 0) { @@ -309,9 +352,17 @@ private void checkVectorMagnitude(float[] vector, double squaredMagnitude) { private final IndexOptions indexOptions; private final Version indexCreatedVersion; - private DenseVectorFieldMapper(String simpleName, MappedFieldType mappedFieldType, int dims, boolean indexed, - VectorSimilarity similarity, IndexOptions indexOptions, - Version indexCreatedVersion, MultiFields multiFields, CopyTo copyTo) { + private DenseVectorFieldMapper( + String simpleName, + MappedFieldType mappedFieldType, + int dims, + boolean indexed, + VectorSimilarity similarity, + IndexOptions indexOptions, + Version indexCreatedVersion, + MultiFields multiFields, + CopyTo copyTo + ) { super(simpleName, mappedFieldType, multiFields, copyTo); this.dims = dims; this.indexed = indexed; @@ -333,13 +384,16 @@ public boolean parsesArrayValue() { @Override public void parse(DocumentParserContext context) throws IOException { if (context.doc().getByKey(fieldType().name()) != null) { - throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + - "] doesn't not support indexing multiple values for the same field in the same document"); + throw new IllegalArgumentException( + "Field [" + + name() + + "] of type [" + + typeName() + + "] doesn't not support indexing multiple values for the same field in the same document" + ); } - Field field = fieldType().indexed - ? parseKnnVector(context) - : parseBinaryDocValuesVector(context); + Field field = fieldType().indexed ? parseKnnVector(context) : parseBinaryDocValuesVector(context); context.doc().addWithKey(fieldType().name(), field); } @@ -391,17 +445,37 @@ private Field parseBinaryDocValuesVector(DocumentParserContext context) throws I private void checkDimensionExceeded(int index, DocumentParserContext context) { if (index >= dims) { - throw new IllegalArgumentException("The [" + typeName() + "] field [" + name() + - "] in doc [" + context.sourceToParse().id() + "] has more dimensions " + - "than defined in the mapping [" + dims + "]"); + throw new IllegalArgumentException( + "The [" + + typeName() + + "] field [" + + name() + + "] in doc [" + + context.sourceToParse().id() + + "] has more dimensions " + + "than defined in the mapping [" + + dims + + "]" + ); } } private void checkDimensionMatches(int index, DocumentParserContext context) { if (index != dims) { - throw new IllegalArgumentException("The [" + typeName() + "] field [" + name() + - "] in doc [" + context.sourceToParse().id() + "] has a different number of dimensions " + - "[" + index + "] than defined in the mapping [" + dims + "]"); + throw new IllegalArgumentException( + "The [" + + typeName() + + "] field [" + + name() + + "] in doc [" + + context.sourceToParse().id() + + "] has a different number of dimensions " + + "[" + + index + + "] than defined in the mapping [" + + dims + + "]" + ); } } diff --git a/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/mapper/SparseVectorFieldMapper.java b/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/mapper/SparseVectorFieldMapper.java index c99d25f1668cb..5382c99120741 100644 --- a/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/mapper/SparseVectorFieldMapper.java +++ b/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/mapper/SparseVectorFieldMapper.java @@ -5,7 +5,6 @@ * 2.0. */ - package org.elasticsearch.xpack.vectors.mapper; import org.apache.lucene.search.Query; @@ -37,8 +36,8 @@ public class SparseVectorFieldMapper extends FieldMapper { private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(SparseVectorFieldMapper.class); static final String ERROR_MESSAGE = "The [sparse_vector] field type is no longer supported."; - static final String ERROR_MESSAGE_7X = "The [sparse_vector] field type is no longer supported. Old 7.x indices are allowed to " + - "contain [sparse_vector] fields, but they cannot be indexed or searched."; + static final String ERROR_MESSAGE_7X = "The [sparse_vector] field type is no longer supported. Old 7.x indices are allowed to " + + "contain [sparse_vector] fields, but they cannot be indexed or searched."; public static final String CONTENT_TYPE = "sparse_vector"; public static class Builder extends FieldMapper.Builder { @@ -57,8 +56,11 @@ protected List> getParameters() { @Override public SparseVectorFieldMapper build(MapperBuilderContext context) { return new SparseVectorFieldMapper( - name, new SparseVectorFieldType(context.buildFullName(name), meta.getValue()), - multiFieldsBuilder.build(this, context), copyTo.build()); + name, + new SparseVectorFieldType(context.buildFullName(name), meta.getValue()), + multiFieldsBuilder.build(this, context), + copyTo.build() + ); } } @@ -103,9 +105,7 @@ public Query termQuery(Object value, SearchExecutionContext context) { } } - - private SparseVectorFieldMapper(String simpleName, MappedFieldType mappedFieldType, - MultiFields multiFields, CopyTo copyTo) { + private SparseVectorFieldMapper(String simpleName, MappedFieldType mappedFieldType, MultiFields multiFields, CopyTo copyTo) { super(simpleName, mappedFieldType, multiFields, copyTo); } diff --git a/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/mapper/VectorEncoderDecoder.java b/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/mapper/VectorEncoderDecoder.java index fde298f7881f0..9b53685dc0ccd 100644 --- a/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/mapper/VectorEncoderDecoder.java +++ b/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/mapper/VectorEncoderDecoder.java @@ -5,7 +5,6 @@ * 2.0. */ - package org.elasticsearch.xpack.vectors.mapper; import org.apache.lucene.util.BytesRef; @@ -14,16 +13,13 @@ import java.nio.ByteBuffer; - public final class VectorEncoderDecoder { public static final byte INT_BYTES = 4; - private VectorEncoderDecoder() { } + private VectorEncoderDecoder() {} public static int denseVectorLength(Version indexVersion, BytesRef vectorBR) { - return indexVersion.onOrAfter(Version.V_7_5_0) - ? (vectorBR.length - INT_BYTES) / INT_BYTES - : vectorBR.length / INT_BYTES; + return indexVersion.onOrAfter(Version.V_7_5_0) ? (vectorBR.length - INT_BYTES) / INT_BYTES : vectorBR.length / INT_BYTES; } /** diff --git a/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/query/BinaryDenseVectorScriptDocValues.java b/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/query/BinaryDenseVectorScriptDocValues.java index 4616af49f5c0c..6df3ed449bd16 100644 --- a/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/query/BinaryDenseVectorScriptDocValues.java +++ b/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/query/BinaryDenseVectorScriptDocValues.java @@ -5,7 +5,6 @@ * 2.0. */ - package org.elasticsearch.xpack.vectors.query; import org.apache.lucene.index.BinaryDocValues; @@ -39,7 +38,6 @@ public void setNextDocId(int docId) throws IOException { } } - @Override public float[] getVectorValue() { VectorEncoderDecoder.decodeDenseVector(value, vector); diff --git a/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/query/DenseVectorScriptDocValues.java b/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/query/DenseVectorScriptDocValues.java index daf4a174c19e4..6ebce8541d308 100644 --- a/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/query/DenseVectorScriptDocValues.java +++ b/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/query/DenseVectorScriptDocValues.java @@ -5,7 +5,6 @@ * 2.0. */ - package org.elasticsearch.xpack.vectors.query; import org.apache.lucene.util.BytesRef; @@ -35,13 +34,16 @@ public int dims() { public abstract float getMagnitude(); public abstract double dotProduct(float[] queryVector); + public abstract double l1Norm(float[] queryVector); + public abstract double l2Norm(float[] queryVector); @Override public BytesRef get(int index) { - throw new UnsupportedOperationException("accessing a vector field's value through 'get' or 'value' is not supported!" + - "Use 'vectorValue' or 'magnitude' instead!'"); + throw new UnsupportedOperationException( + "accessing a vector field's value through 'get' or 'value' is not supported!" + "Use 'vectorValue' or 'magnitude' instead!'" + ); } public static DenseVectorScriptDocValues empty(int dims) { diff --git a/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/query/DocValuesWhitelistExtension.java b/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/query/DocValuesWhitelistExtension.java index cd00c7e3766ac..c53d1379dc252 100644 --- a/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/query/DocValuesWhitelistExtension.java +++ b/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/query/DocValuesWhitelistExtension.java @@ -5,10 +5,8 @@ * 2.0. */ - package org.elasticsearch.xpack.vectors.query; - import org.elasticsearch.painless.spi.PainlessExtension; import org.elasticsearch.painless.spi.Whitelist; import org.elasticsearch.painless.spi.WhitelistLoader; @@ -21,8 +19,7 @@ public class DocValuesWhitelistExtension implements PainlessExtension { - private static final Whitelist WHITELIST = - WhitelistLoader.loadFromResourceFiles(DocValuesWhitelistExtension.class, "whitelist.txt"); + private static final Whitelist WHITELIST = WhitelistLoader.loadFromResourceFiles(DocValuesWhitelistExtension.class, "whitelist.txt"); @Override public Map, List> getContextWhitelists() { diff --git a/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/query/KnnDenseVectorScriptDocValues.java b/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/query/KnnDenseVectorScriptDocValues.java index d7dbb055b5039..03afcbf0dd685 100644 --- a/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/query/KnnDenseVectorScriptDocValues.java +++ b/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/query/KnnDenseVectorScriptDocValues.java @@ -5,7 +5,6 @@ * 2.0. */ - package org.elasticsearch.xpack.vectors.query; import org.apache.lucene.index.VectorValues; diff --git a/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/query/KnnVectorQueryBuilder.java b/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/query/KnnVectorQueryBuilder.java index cfc1fd9db265a..4dbe2ec47dcbc 100644 --- a/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/query/KnnVectorQueryBuilder.java +++ b/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/query/KnnVectorQueryBuilder.java @@ -70,10 +70,7 @@ protected void doWriteTo(StreamOutput out) throws IOException { @Override protected void doXContent(XContentBuilder builder, Params params) throws IOException { - builder.startObject(NAME) - .field("field", fieldName) - .field("vector", queryVector) - .field("num_candidates", numCands); + builder.startObject(NAME).field("field", fieldName).field("vector", queryVector).field("num_candidates", numCands); builder.endObject(); } @@ -90,8 +87,9 @@ protected Query doToQuery(SearchExecutionContext context) { } if (fieldType instanceof DenseVectorFieldType == false) { - throw new IllegalArgumentException("[" + NAME + "] queries are only supported on [" - + DenseVectorFieldMapper.CONTENT_TYPE + "] fields"); + throw new IllegalArgumentException( + "[" + NAME + "] queries are only supported on [" + DenseVectorFieldMapper.CONTENT_TYPE + "] fields" + ); } if (context.getNestedParent(fieldType.name()) != null) { @@ -109,8 +107,6 @@ protected int doHashCode() { @Override protected boolean doEquals(KnnVectorQueryBuilder other) { - return Objects.equals(fieldName, other.fieldName) && - Arrays.equals(queryVector, other.queryVector) && - numCands == other.numCands; + return Objects.equals(fieldName, other.fieldName) && Arrays.equals(queryVector, other.queryVector) && numCands == other.numCands; } } diff --git a/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/query/ScoreScriptUtils.java b/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/query/ScoreScriptUtils.java index 82d7cd5362503..511985b62a58e 100644 --- a/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/query/ScoreScriptUtils.java +++ b/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/query/ScoreScriptUtils.java @@ -5,7 +5,6 @@ * 2.0. */ - package org.elasticsearch.xpack.vectors.query; import org.elasticsearch.ExceptionsHelper; @@ -21,9 +20,7 @@ public static class DenseVectorFunction { final float[] queryVector; final DenseVectorScriptDocValues docValues; - public DenseVectorFunction(ScoreScript scoreScript, - List queryVector, - String field) { + public DenseVectorFunction(ScoreScript scoreScript, List queryVector, String field) { this(scoreScript, queryVector, field, false); } @@ -34,16 +31,18 @@ public DenseVectorFunction(ScoreScript scoreScript, * @param queryVector The query vector. * @param normalizeQuery Whether the provided query should be normalized to unit length. */ - public DenseVectorFunction(ScoreScript scoreScript, - List queryVector, - String field, - boolean normalizeQuery) { + public DenseVectorFunction(ScoreScript scoreScript, List queryVector, String field, boolean normalizeQuery) { this.scoreScript = scoreScript; this.docValues = (DenseVectorScriptDocValues) scoreScript.getDoc().get(field); - if (docValues.dims() != queryVector.size()){ - throw new IllegalArgumentException("The query vector has a different number of dimensions [" + - queryVector.size() + "] than the document vectors [" + docValues.dims() + "]."); + if (docValues.dims() != queryVector.size()) { + throw new IllegalArgumentException( + "The query vector has a different number of dimensions [" + + queryVector.size() + + "] than the document vectors [" + + docValues.dims() + + "]." + ); } this.queryVector = new float[queryVector.size()]; diff --git a/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/query/VectorDVLeafFieldData.java b/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/query/VectorDVLeafFieldData.java index b7015d0f47ba2..1aca1f12f7379 100644 --- a/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/query/VectorDVLeafFieldData.java +++ b/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/query/VectorDVLeafFieldData.java @@ -5,7 +5,6 @@ * 2.0. */ - package org.elasticsearch.xpack.vectors.query; import org.apache.lucene.index.BinaryDocValues; diff --git a/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/query/VectorIndexFieldData.java b/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/query/VectorIndexFieldData.java index 10f9b96254129..7a788c948068d 100644 --- a/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/query/VectorIndexFieldData.java +++ b/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/query/VectorIndexFieldData.java @@ -5,14 +5,13 @@ * 2.0. */ - package org.elasticsearch.xpack.vectors.query; import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.search.SortField; import org.elasticsearch.Version; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.util.BigArrays; +import org.elasticsearch.core.Nullable; import org.elasticsearch.index.fielddata.IndexFieldData; import org.elasticsearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested; import org.elasticsearch.index.fielddata.IndexFieldDataCache; @@ -24,7 +23,6 @@ import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.xpack.vectors.mapper.DenseVectorFieldMapper; - public class VectorIndexFieldData implements IndexFieldData { protected final String fieldName; @@ -53,13 +51,22 @@ public ValuesSourceType getValuesSourceType() { @Override public SortField sortField(@Nullable Object missingValue, MultiValueMode sortMode, Nested nested, boolean reverse) { - throw new IllegalArgumentException("Field [" + fieldName + "] of type [" + - DenseVectorFieldMapper.CONTENT_TYPE + "] doesn't support sort"); + throw new IllegalArgumentException( + "Field [" + fieldName + "] of type [" + DenseVectorFieldMapper.CONTENT_TYPE + "] doesn't support sort" + ); } @Override - public BucketedSort newBucketedSort(BigArrays bigArrays, Object missingValue, MultiValueMode sortMode, Nested nested, - SortOrder sortOrder, DocValueFormat format, int bucketSize, BucketedSort.ExtraData extra) { + public BucketedSort newBucketedSort( + BigArrays bigArrays, + Object missingValue, + MultiValueMode sortMode, + Nested nested, + SortOrder sortOrder, + DocValueFormat format, + int bucketSize, + BucketedSort.ExtraData extra + ) { throw new IllegalArgumentException("only supported on numeric fields"); } diff --git a/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/action/KnnSearchRequestBuilderTests.java b/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/action/KnnSearchRequestBuilderTests.java index 183393a9b2970..10508c9078335 100644 --- a/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/action/KnnSearchRequestBuilderTests.java +++ b/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/action/KnnSearchRequestBuilderTests.java @@ -53,7 +53,8 @@ public void testBuildSearchRequest() throws IOException { () -> null, Collections::emptyList, () -> null, - () -> null); + () -> null + ); XContentBuilder builder = createRequestBody(knnSearch, searchSource); // Convert the REST request to a search request and check the components @@ -81,10 +82,10 @@ public void testParseSourceString() throws IOException { KnnSearch knnSearch = randomKnnSearch(); builder.startObject() .startObject(KnnSearchRequestBuilder.KNN_SECTION_FIELD.getPreferredName()) - .field(KnnSearch.FIELD_FIELD.getPreferredName(), knnSearch.field) - .field(KnnSearch.K_FIELD.getPreferredName(), knnSearch.k) - .field(KnnSearch.NUM_CANDS_FIELD.getPreferredName(), knnSearch.numCands) - .field(KnnSearch.QUERY_VECTOR_FIELD.getPreferredName(), knnSearch.queryVector) + .field(KnnSearch.FIELD_FIELD.getPreferredName(), knnSearch.field) + .field(KnnSearch.K_FIELD.getPreferredName(), knnSearch.k) + .field(KnnSearch.NUM_CANDS_FIELD.getPreferredName(), knnSearch.numCands) + .field(KnnSearch.QUERY_VECTOR_FIELD.getPreferredName(), knnSearch.queryVector) .endObject(); builder.field(SearchSourceBuilder._SOURCE_FIELD.getPreferredName(), "some-field"); @@ -96,7 +97,7 @@ public void testParseSourceString() throws IOException { FetchSourceContext fetchSource = searchRequest.source().fetchSource(); assertTrue(fetchSource.fetchSource()); - assertArrayEquals(new String[]{"some-field"}, fetchSource.includes()); + assertArrayEquals(new String[] { "some-field" }, fetchSource.includes()); } public void testParseSourceArray() throws IOException { @@ -107,10 +108,10 @@ public void testParseSourceArray() throws IOException { KnnSearch knnSearch = randomKnnSearch(); builder.startObject() .startObject(KnnSearchRequestBuilder.KNN_SECTION_FIELD.getPreferredName()) - .field(KnnSearch.FIELD_FIELD.getPreferredName(), knnSearch.field) - .field(KnnSearch.K_FIELD.getPreferredName(), knnSearch.k) - .field(KnnSearch.NUM_CANDS_FIELD.getPreferredName(), knnSearch.numCands) - .field(KnnSearch.QUERY_VECTOR_FIELD.getPreferredName(), knnSearch.queryVector) + .field(KnnSearch.FIELD_FIELD.getPreferredName(), knnSearch.field) + .field(KnnSearch.K_FIELD.getPreferredName(), knnSearch.k) + .field(KnnSearch.NUM_CANDS_FIELD.getPreferredName(), knnSearch.numCands) + .field(KnnSearch.QUERY_VECTOR_FIELD.getPreferredName(), knnSearch.queryVector) .endObject(); builder.array(SearchSourceBuilder._SOURCE_FIELD.getPreferredName(), "field1", "field2", "field3"); @@ -122,12 +123,13 @@ public void testParseSourceArray() throws IOException { FetchSourceContext fetchSource = searchRequest.source().fetchSource(); assertTrue(fetchSource.fetchSource()); - assertArrayEquals(new String[]{"field1", "field2", "field3"}, fetchSource.includes()); + assertArrayEquals(new String[] { "field1", "field2", "field3" }, fetchSource.includes()); } public void testMissingKnnSection() throws IOException { XContentType xContentType = randomFrom(XContentType.values()); - XContentBuilder builder = XContentBuilder.builder(xContentType.xContent()).startObject() + XContentBuilder builder = XContentBuilder.builder(xContentType.xContent()) + .startObject() .array(SearchSourceBuilder.FETCH_FIELDS_FIELD.getPreferredName(), "field1", "field2") .endObject(); @@ -137,13 +139,14 @@ public void testMissingKnnSection() throws IOException { public void testNumCandsLessThanK() throws IOException { XContentType xContentType = randomFrom(XContentType.values()); - XContentBuilder builder = XContentBuilder.builder(xContentType.xContent()).startObject() - .startObject(KnnSearchRequestBuilder.KNN_SECTION_FIELD.getPreferredName()) - .field(KnnSearch.FIELD_FIELD.getPreferredName(), "field") - .field(KnnSearch.K_FIELD.getPreferredName(), 100) - .field(KnnSearch.NUM_CANDS_FIELD.getPreferredName(), 80) - .field(KnnSearch.QUERY_VECTOR_FIELD.getPreferredName(), new float[]{1.0f, 2.0f, 3.0f}) - .endObject() + XContentBuilder builder = XContentBuilder.builder(xContentType.xContent()) + .startObject() + .startObject(KnnSearchRequestBuilder.KNN_SECTION_FIELD.getPreferredName()) + .field(KnnSearch.FIELD_FIELD.getPreferredName(), "field") + .field(KnnSearch.K_FIELD.getPreferredName(), 100) + .field(KnnSearch.NUM_CANDS_FIELD.getPreferredName(), 80) + .field(KnnSearch.QUERY_VECTOR_FIELD.getPreferredName(), new float[] { 1.0f, 2.0f, 3.0f }) + .endObject() .endObject(); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> buildSearchRequest(builder)); @@ -152,13 +155,14 @@ public void testNumCandsLessThanK() throws IOException { public void testNumCandsExceedsLimit() throws IOException { XContentType xContentType = randomFrom(XContentType.values()); - XContentBuilder builder = XContentBuilder.builder(xContentType.xContent()).startObject() - .startObject(KnnSearchRequestBuilder.KNN_SECTION_FIELD.getPreferredName()) - .field(KnnSearch.FIELD_FIELD.getPreferredName(), "field") - .field(KnnSearch.K_FIELD.getPreferredName(), 100) - .field(KnnSearch.NUM_CANDS_FIELD.getPreferredName(), 10002) - .field(KnnSearch.QUERY_VECTOR_FIELD.getPreferredName(), new float[]{1.0f, 2.0f, 3.0f}) - .endObject() + XContentBuilder builder = XContentBuilder.builder(xContentType.xContent()) + .startObject() + .startObject(KnnSearchRequestBuilder.KNN_SECTION_FIELD.getPreferredName()) + .field(KnnSearch.FIELD_FIELD.getPreferredName(), "field") + .field(KnnSearch.K_FIELD.getPreferredName(), 100) + .field(KnnSearch.NUM_CANDS_FIELD.getPreferredName(), 10002) + .field(KnnSearch.QUERY_VECTOR_FIELD.getPreferredName(), new float[] { 1.0f, 2.0f, 3.0f }) + .endObject() .endObject(); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> buildSearchRequest(builder)); @@ -167,13 +171,14 @@ public void testNumCandsExceedsLimit() throws IOException { public void testInvalidK() throws IOException { XContentType xContentType = randomFrom(XContentType.values()); - XContentBuilder builder = XContentBuilder.builder(xContentType.xContent()).startObject() - .startObject(KnnSearchRequestBuilder.KNN_SECTION_FIELD.getPreferredName()) - .field(KnnSearch.FIELD_FIELD.getPreferredName(), "field") - .field(KnnSearch.K_FIELD.getPreferredName(), 0) - .field(KnnSearch.NUM_CANDS_FIELD.getPreferredName(), 10) - .field(KnnSearch.QUERY_VECTOR_FIELD.getPreferredName(), new float[]{1.0f, 2.0f, 3.0f}) - .endObject() + XContentBuilder builder = XContentBuilder.builder(xContentType.xContent()) + .startObject() + .startObject(KnnSearchRequestBuilder.KNN_SECTION_FIELD.getPreferredName()) + .field(KnnSearch.FIELD_FIELD.getPreferredName(), "field") + .field(KnnSearch.K_FIELD.getPreferredName(), 0) + .field(KnnSearch.NUM_CANDS_FIELD.getPreferredName(), 10) + .field(KnnSearch.QUERY_VECTOR_FIELD.getPreferredName(), new float[] { 1.0f, 2.0f, 3.0f }) + .endObject() .endObject(); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> buildSearchRequest(builder)); @@ -187,11 +192,11 @@ private SearchRequestBuilder buildSearchRequest(XContentBuilder builder) throws private SearchRequestBuilder buildSearchRequest(XContentBuilder builder, Map params) throws IOException { KnnSearchRequestBuilder knnRequestBuilder = KnnSearchRequestBuilder.parseRestRequest( - new FakeRestRequest.Builder(xContentRegistry()) - .withMethod(RestRequest.Method.POST) + new FakeRestRequest.Builder(xContentRegistry()).withMethod(RestRequest.Method.POST) .withParams(params) .withContent(BytesReference.bytes(builder), builder.contentType()) - .build()); + .build() + ); SearchRequestBuilder searchRequestBuilder = new SearchRequestBuilder(null, SearchAction.INSTANCE); knnRequestBuilder.build(searchRequestBuilder); return searchRequestBuilder; @@ -216,10 +221,10 @@ private XContentBuilder createRequestBody(KnnSearch knnSearch, SearchSourceBuild builder.startObject(); builder.startObject(KnnSearchRequestBuilder.KNN_SECTION_FIELD.getPreferredName()) - .field(KnnSearch.FIELD_FIELD.getPreferredName(), knnSearch.field) - .field(KnnSearch.K_FIELD.getPreferredName(), knnSearch.k) - .field(KnnSearch.NUM_CANDS_FIELD.getPreferredName(), knnSearch.numCands) - .field(KnnSearch.QUERY_VECTOR_FIELD.getPreferredName(), knnSearch.queryVector) + .field(KnnSearch.FIELD_FIELD.getPreferredName(), knnSearch.field) + .field(KnnSearch.K_FIELD.getPreferredName(), knnSearch.k) + .field(KnnSearch.NUM_CANDS_FIELD.getPreferredName(), knnSearch.numCands) + .field(KnnSearch.QUERY_VECTOR_FIELD.getPreferredName(), knnSearch.queryVector) .endObject(); if (searchSource.fetchSource() != null) { diff --git a/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/mapper/DenseVectorFieldMapperTests.java b/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/mapper/DenseVectorFieldMapperTests.java index ba22ae4d0b4a2..384ef234de81b 100644 --- a/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/mapper/DenseVectorFieldMapperTests.java +++ b/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/mapper/DenseVectorFieldMapperTests.java @@ -21,15 +21,15 @@ import org.elasticsearch.Version; import org.elasticsearch.index.codec.CodecService; import org.elasticsearch.index.codec.PerFieldMapperCodec; -import org.elasticsearch.index.mapper.MapperService; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.LuceneDocument; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.MapperParsingException; +import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.mapper.MapperTestCase; import org.elasticsearch.index.mapper.ParsedDocument; import org.elasticsearch.plugins.Plugin; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.vectors.DenseVectorPlugin; import org.elasticsearch.xpack.vectors.mapper.DenseVectorFieldMapper.DenseVectorFieldType; import org.elasticsearch.xpack.vectors.mapper.DenseVectorFieldMapper.VectorSimilarity; @@ -68,9 +68,9 @@ protected void minimalMapping(XContentBuilder b) throws IOException { b.field("index", true).field("similarity", "dot_product"); if (indexOptionsSet) { b.startObject("index_options"); - b.field("type", "hnsw"); - b.field("m", 5); - b.field("ef_construction", 50); + b.field("type", "hnsw"); + b.field("m", 5); + b.field("ef_construction", 50); b.endObject(); } } @@ -83,40 +83,36 @@ protected Object getSampleValueForDocument() { @Override protected void registerParameters(ParameterChecker checker) throws IOException { - checker.registerConflictCheck("dims", + checker.registerConflictCheck( + "dims", fieldMapping(b -> b.field("type", "dense_vector").field("dims", 4)), - fieldMapping(b -> b.field("type", "dense_vector").field("dims", 5))); - checker.registerConflictCheck("similarity", - fieldMapping(b -> b.field("type", "dense_vector") - .field("dims", 4) - .field("index", true) - .field("similarity", "dot_product")), - fieldMapping(b -> b.field("type", "dense_vector") - .field("dims", 4) - .field("index", true) - .field("similarity", "l2_norm"))); - checker.registerConflictCheck("index", - fieldMapping(b -> b.field("type", "dense_vector") - .field("dims", 4) - .field("index", true) - .field("similarity", "dot_product")), - fieldMapping(b -> b.field("type", "dense_vector") - .field("dims", 4) - .field("index", false))); - checker.registerConflictCheck("index_options", - fieldMapping(b -> b.field("type", "dense_vector") - .field("dims", 4) - .field("index", true) - .field("similarity", "dot_product")), - fieldMapping(b -> b.field("type", "dense_vector") - .field("dims", 4) - .field("index", true) - .field("similarity", "dot_product") - .startObject("index_options") - .field("type" , "hnsw") + fieldMapping(b -> b.field("type", "dense_vector").field("dims", 5)) + ); + checker.registerConflictCheck( + "similarity", + fieldMapping(b -> b.field("type", "dense_vector").field("dims", 4).field("index", true).field("similarity", "dot_product")), + fieldMapping(b -> b.field("type", "dense_vector").field("dims", 4).field("index", true).field("similarity", "l2_norm")) + ); + checker.registerConflictCheck( + "index", + fieldMapping(b -> b.field("type", "dense_vector").field("dims", 4).field("index", true).field("similarity", "dot_product")), + fieldMapping(b -> b.field("type", "dense_vector").field("dims", 4).field("index", false)) + ); + checker.registerConflictCheck( + "index_options", + fieldMapping(b -> b.field("type", "dense_vector").field("dims", 4).field("index", true).field("similarity", "dot_product")), + fieldMapping( + b -> b.field("type", "dense_vector") + .field("dims", 4) + .field("index", true) + .field("similarity", "dot_product") + .startObject("index_options") + .field("type", "hnsw") .field("m", 5) .field("ef_construction", 80) - .endObject())); + .endObject() + ) + ); } @Override @@ -150,20 +146,31 @@ public void testDims() { b.field("type", "dense_vector"); b.field("dims", 0); }))); - assertThat(e.getMessage(), equalTo("Failed to parse mapping: " + - "The number of dimensions for field [field] should be in the range [1, 2048] but was [0]")); + assertThat( + e.getMessage(), + equalTo( + "Failed to parse mapping: " + "The number of dimensions for field [field] should be in the range [1, 2048] but was [0]" + ) + ); } { Exception e = expectThrows(MapperParsingException.class, () -> createMapperService(fieldMapping(b -> { b.field("type", "dense_vector"); b.field("dims", 3000); }))); - assertThat(e.getMessage(), equalTo("Failed to parse mapping: " + - "The number of dimensions for field [field] should be in the range [1, 2048] but was [3000]")); + assertThat( + e.getMessage(), + equalTo( + "Failed to parse mapping: " + + "The number of dimensions for field [field] should be in the range [1, 2048] but was [3000]" + ) + ); } { - Exception e = expectThrows(MapperParsingException.class, - () -> createMapperService(fieldMapping(b -> b.field("type", "dense_vector")))); + Exception e = expectThrows( + MapperParsingException.class, + () -> createMapperService(fieldMapping(b -> b.field("type", "dense_vector"))) + ); assertThat(e.getMessage(), equalTo("Failed to parse mapping: Missing required parameter [dims] for field [field]")); } } @@ -172,9 +179,9 @@ public void testDefaults() throws Exception { DocumentMapper mapper = createDocumentMapper(fieldMapping(b -> b.field("type", "dense_vector").field("dims", 3))); - float[] validVector = {-12.1f, 100.7f, -4}; + float[] validVector = { -12.1f, 100.7f, -4 }; double dotProduct = 0.0f; - for (float value: validVector) { + for (float value : validVector) { dotProduct += value * value; } float expectedMagnitude = (float) Math.sqrt(dotProduct); @@ -188,23 +195,16 @@ public void testDefaults() throws Exception { float[] decodedValues = decodeDenseVector(Version.CURRENT, vectorBR); float decodedMagnitude = VectorEncoderDecoder.decodeMagnitude(Version.CURRENT, vectorBR); assertEquals(expectedMagnitude, decodedMagnitude, 0.001f); - assertArrayEquals( - "Decoded dense vector values is not equal to the indexed one.", - validVector, - decodedValues, - 0.001f - ); + assertArrayEquals("Decoded dense vector values is not equal to the indexed one.", validVector, decodedValues, 0.001f); } public void testIndexedVector() throws Exception { VectorSimilarity similarity = RandomPicks.randomFrom(random(), VectorSimilarity.values()); - DocumentMapper mapper = createDocumentMapper(fieldMapping(b -> b - .field("type", "dense_vector") - .field("dims", 3) - .field("index", true) - .field("similarity", similarity.name()))); + DocumentMapper mapper = createDocumentMapper( + fieldMapping(b -> b.field("type", "dense_vector").field("dims", 3).field("index", true).field("similarity", similarity.name())) + ); - float[] vector = {-0.5f, 0.5f, 0.7071f}; + float[] vector = { -0.5f, 0.5f, 0.7071f }; ParsedDocument doc1 = mapper.parse(source(b -> b.array("field", vector))); IndexableField[] fields = doc1.rootDoc().getFields("field"); @@ -212,105 +212,127 @@ public void testIndexedVector() throws Exception { assertThat(fields[0], instanceOf(KnnVectorField.class)); KnnVectorField vectorField = (KnnVectorField) fields[0]; - assertArrayEquals( - "Parsed vector is not equal to original.", - vector, - vectorField.vectorValue(), - 0.001f); + assertArrayEquals("Parsed vector is not equal to original.", vector, vectorField.vectorValue(), 0.001f); assertEquals(similarity.function, vectorField.fieldType().vectorSimilarityFunction()); } public void testDotProductWithInvalidNorm() throws Exception { - DocumentMapper mapper = createDocumentMapper(fieldMapping(b -> b - .field("type", "dense_vector") - .field("dims", 3) - .field("index", true) - .field("similarity", VectorSimilarity.dot_product))); - float[] vector = {-12.1f, 2.7f, -4}; + DocumentMapper mapper = createDocumentMapper( + fieldMapping( + b -> b.field("type", "dense_vector").field("dims", 3).field("index", true).field("similarity", VectorSimilarity.dot_product) + ) + ); + float[] vector = { -12.1f, 2.7f, -4 }; MapperParsingException e = expectThrows(MapperParsingException.class, () -> mapper.parse(source(b -> b.array("field", vector)))); assertNotNull(e.getCause()); - assertThat(e.getCause().getMessage(), containsString("The [dot_product] similarity can only be used with unit-length vectors. " + - "Preview of invalid vector: [-12.1, 2.7, -4.0]")); - - DocumentMapper mapperWithLargerDim = createDocumentMapper(fieldMapping(b -> b - .field("type", "dense_vector") - .field("dims", 6) - .field("index", true) - .field("similarity", VectorSimilarity.dot_product))); - float[] largerVector = {-12.1f, 2.7f, -4, 1.05f, 10.0f, 29.9f}; + assertThat( + e.getCause().getMessage(), + containsString( + "The [dot_product] similarity can only be used with unit-length vectors. " + "Preview of invalid vector: [-12.1, 2.7, -4.0]" + ) + ); + + DocumentMapper mapperWithLargerDim = createDocumentMapper( + fieldMapping( + b -> b.field("type", "dense_vector").field("dims", 6).field("index", true).field("similarity", VectorSimilarity.dot_product) + ) + ); + float[] largerVector = { -12.1f, 2.7f, -4, 1.05f, 10.0f, 29.9f }; e = expectThrows(MapperParsingException.class, () -> mapperWithLargerDim.parse(source(b -> b.array("field", largerVector)))); assertNotNull(e.getCause()); - assertThat(e.getCause().getMessage(), containsString("The [dot_product] similarity can only be used with unit-length vectors. " + - "Preview of invalid vector: [-12.1, 2.7, -4.0, 1.05, 10.0, ...]")); + assertThat( + e.getCause().getMessage(), + containsString( + "The [dot_product] similarity can only be used with unit-length vectors. " + + "Preview of invalid vector: [-12.1, 2.7, -4.0, 1.05, 10.0, ...]" + ) + ); } public void testInvalidParameters() { - MapperParsingException e = expectThrows(MapperParsingException.class, - () -> createDocumentMapper(fieldMapping(b -> b - .field("type", "dense_vector") - .field("dims", 3) - .field("index", true)))); + MapperParsingException e = expectThrows( + MapperParsingException.class, + () -> createDocumentMapper(fieldMapping(b -> b.field("type", "dense_vector").field("dims", 3).field("index", true))) + ); assertThat(e.getMessage(), containsString("Field [index] requires field [similarity] to be configured")); - e = expectThrows(MapperParsingException.class, - () -> createDocumentMapper(fieldMapping(b -> b - .field("type", "dense_vector") - .field("dims", 3) - .field("similarity", "l2_norm")))); + e = expectThrows( + MapperParsingException.class, + () -> createDocumentMapper(fieldMapping(b -> b.field("type", "dense_vector").field("dims", 3).field("similarity", "l2_norm"))) + ); assertThat(e.getMessage(), containsString("Field [similarity] requires field [index] to be configured")); - e = expectThrows(MapperParsingException.class, - () -> createDocumentMapper(fieldMapping(b -> b - .field("type", "dense_vector") - .field("dims", 3) - .startObject("index_options") - .field("type", "hnsw") - .field("m", 5) - .field("ef_construction", 100) - .endObject()))); + e = expectThrows( + MapperParsingException.class, + () -> createDocumentMapper( + fieldMapping( + b -> b.field("type", "dense_vector") + .field("dims", 3) + .startObject("index_options") + .field("type", "hnsw") + .field("m", 5) + .field("ef_construction", 100) + .endObject() + ) + ) + ); assertThat(e.getMessage(), containsString("Field [index_options] requires field [index] to be configured")); - e = expectThrows(MapperParsingException.class, - () -> createDocumentMapper(fieldMapping(b -> b - .field("type", "dense_vector") - .field("dims", 3) - .field("similarity", "l2_norm") - .field("index", true) - .startObject("index_options") - .endObject()))); + e = expectThrows( + MapperParsingException.class, + () -> createDocumentMapper( + fieldMapping( + b -> b.field("type", "dense_vector") + .field("dims", 3) + .field("similarity", "l2_norm") + .field("index", true) + .startObject("index_options") + .endObject() + ) + ) + ); assertThat(e.getMessage(), containsString("[index_options] requires field [type] to be configured")); - e = expectThrows(MapperParsingException.class, - () -> createDocumentMapper(fieldMapping(b -> b - .field("type", "dense_vector") - .field("dims", 3) - .field("similarity", "l2_norm") - .field("index", true) - .startObject("index_options") - .field("type", "hnsw") - .field("ef_construction", 100) - .endObject()))); + e = expectThrows( + MapperParsingException.class, + () -> createDocumentMapper( + fieldMapping( + b -> b.field("type", "dense_vector") + .field("dims", 3) + .field("similarity", "l2_norm") + .field("index", true) + .startObject("index_options") + .field("type", "hnsw") + .field("ef_construction", 100) + .endObject() + ) + ) + ); assertThat(e.getMessage(), containsString("[index_options] of type [hnsw] requires field [m] to be configured")); - e = expectThrows(MapperParsingException.class, - () -> createDocumentMapper(fieldMapping(b -> b - .field("type", "dense_vector") - .field("dims", 3) - .field("similarity", "l2_norm") - .field("index", true) - .startObject("index_options") - .field("type", "hnsw") - .field("m", 5) - .endObject()))); + e = expectThrows( + MapperParsingException.class, + () -> createDocumentMapper( + fieldMapping( + b -> b.field("type", "dense_vector") + .field("dims", 3) + .field("similarity", "l2_norm") + .field("index", true) + .startObject("index_options") + .field("type", "hnsw") + .field("m", 5) + .endObject() + ) + ) + ); assertThat(e.getMessage(), containsString("[index_options] of type [hnsw] requires field [ef_construction] to be configured")); } public void testAddDocumentsToIndexBefore_V_7_5_0() throws Exception { Version indexVersion = Version.V_7_4_0; - DocumentMapper mapper - = createDocumentMapper(indexVersion, fieldMapping(b -> b.field("type", "dense_vector").field("dims", 3))); + DocumentMapper mapper = createDocumentMapper(indexVersion, fieldMapping(b -> b.field("type", "dense_vector").field("dims", 3))); - float[] validVector = {-12.1f, 100.7f, -4}; + float[] validVector = { -12.1f, 100.7f, -4 }; ParsedDocument doc1 = mapper.parse(source(b -> b.array("field", validVector))); IndexableField[] fields = doc1.rootDoc().getFields("field"); assertEquals(1, fields.length); @@ -318,12 +340,7 @@ public void testAddDocumentsToIndexBefore_V_7_5_0() throws Exception { // assert that after decoding the indexed value is equal to expected BytesRef vectorBR = fields[0].binaryValue(); float[] decodedValues = decodeDenseVector(indexVersion, vectorBR); - assertArrayEquals( - "Decoded dense vector values is not equal to the indexed one.", - validVector, - decodedValues, - 0.001f - ); + assertArrayEquals("Decoded dense vector values is not equal to the indexed one.", validVector, decodedValues, 0.001f); } private static float[] decodeDenseVector(Version indexVersion, BytesRef encodedVector) { @@ -353,16 +370,22 @@ public void testDocumentsWithIncorrectDims() throws Exception { // test that error is thrown when a document has number of dims more than defined in the mapping float[] invalidVector = new float[dims + 1]; - MapperParsingException e = expectThrows(MapperParsingException.class, - () -> mapper.parse(source(b -> b.array("field", invalidVector)))); + MapperParsingException e = expectThrows( + MapperParsingException.class, + () -> mapper.parse(source(b -> b.array("field", invalidVector))) + ); assertThat(e.getCause().getMessage(), containsString("has more dimensions than defined in the mapping [3]")); // test that error is thrown when a document has number of dims less than defined in the mapping float[] invalidVector2 = new float[dims - 1]; - MapperParsingException e2 = expectThrows(MapperParsingException.class, - () -> mapper.parse(source(b -> b.array("field", invalidVector2)))); - assertThat(e2.getCause().getMessage(), - containsString("has a different number of dimensions [2] than defined in the mapping [3]")); + MapperParsingException e2 = expectThrows( + MapperParsingException.class, + () -> mapper.parse(source(b -> b.array("field", invalidVector2))) + ); + assertThat( + e2.getCause().getMessage(), + containsString("has a different number of dimensions [2] than defined in the mapping [3]") + ); } } @@ -398,9 +421,9 @@ public void testKnnVectorsFormat() throws IOException { b.field("index", true); b.field("similarity", "dot_product"); b.startObject("index_options"); - b.field("type", "hnsw"); - b.field("m", m); - b.field("ef_construction", efConstruction); + b.field("type", "hnsw"); + b.field("m", m); + b.field("ef_construction", efConstruction); b.endObject(); })); CodecService codecService = new CodecService(mapperService); @@ -408,8 +431,11 @@ public void testKnnVectorsFormat() throws IOException { assertThat(codec, instanceOf(PerFieldMapperCodec.class)); KnnVectorsFormat knnVectorsFormat = ((PerFieldMapperCodec) codec).getKnnVectorsFormatForField("field"); assertThat(knnVectorsFormat, instanceOf(Lucene90HnswVectorsFormat.class)); - String expectedString = "Lucene90HnswVectorsFormat(name = Lucene90HnswVectorsFormat, maxConn = " + m + - ", beamWidth=" + efConstruction + ")"; + String expectedString = "Lucene90HnswVectorsFormat(name = Lucene90HnswVectorsFormat, maxConn = " + + m + + ", beamWidth=" + + efConstruction + + ")"; assertEquals(expectedString, knnVectorsFormat.toString()); } } diff --git a/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/mapper/DenseVectorFieldTypeTests.java b/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/mapper/DenseVectorFieldTypeTests.java index 8a6e8c4800a6f..37e5bcbf9c1d1 100644 --- a/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/mapper/DenseVectorFieldTypeTests.java +++ b/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/mapper/DenseVectorFieldTypeTests.java @@ -46,9 +46,7 @@ public void testIsAggregatable() { public void testFielddataBuilder() { DenseVectorFieldType ft = createFieldType(); - assertNotNull(ft.fielddataBuilder("index", () -> { - throw new UnsupportedOperationException(); - })); + assertNotNull(ft.fielddataBuilder("index", () -> { throw new UnsupportedOperationException(); })); } public void testDocValueFormat() { @@ -63,16 +61,29 @@ public void testFetchSourceValue() throws IOException { } public void testCreateKnnQuery() { - DenseVectorFieldType unindexedField = new DenseVectorFieldType("f", Version.CURRENT, - 3, false, VectorSimilarity.cosine, Collections.emptyMap()); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> unindexedField.createKnnQuery( - new float[]{0.3f, 0.1f, 1.0f}, 10)); + DenseVectorFieldType unindexedField = new DenseVectorFieldType( + "f", + Version.CURRENT, + 3, + false, + VectorSimilarity.cosine, + Collections.emptyMap() + ); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> unindexedField.createKnnQuery(new float[] { 0.3f, 0.1f, 1.0f }, 10) + ); assertThat(e.getMessage(), containsString("[knn] queries are not supported if [index] is disabled")); - DenseVectorFieldType dotProductField = new DenseVectorFieldType("f", Version.CURRENT, - 3, true, VectorSimilarity.dot_product, Collections.emptyMap()); - e = expectThrows(IllegalArgumentException.class, () -> dotProductField.createKnnQuery( - new float[]{0.3f, 0.1f, 1.0f}, 10)); + DenseVectorFieldType dotProductField = new DenseVectorFieldType( + "f", + Version.CURRENT, + 3, + true, + VectorSimilarity.dot_product, + Collections.emptyMap() + ); + e = expectThrows(IllegalArgumentException.class, () -> dotProductField.createKnnQuery(new float[] { 0.3f, 0.1f, 1.0f }, 10)); assertThat(e.getMessage(), containsString("The [dot_product] similarity can only be used with unit-length vectors.")); } } diff --git a/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/mapper/SparseVectorFieldTypeTests.java b/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/mapper/SparseVectorFieldTypeTests.java index 6fed982c89089..c5f4f96a89690 100644 --- a/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/mapper/SparseVectorFieldTypeTests.java +++ b/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/mapper/SparseVectorFieldTypeTests.java @@ -5,7 +5,6 @@ * 2.0. */ - package org.elasticsearch.xpack.vectors.mapper; import org.elasticsearch.index.mapper.FieldTypeTestCase; diff --git a/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/query/BinaryDenseVectorScriptDocValuesTests.java b/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/query/BinaryDenseVectorScriptDocValuesTests.java index edf0285259320..6541ccbd01c4e 100644 --- a/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/query/BinaryDenseVectorScriptDocValuesTests.java +++ b/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/query/BinaryDenseVectorScriptDocValuesTests.java @@ -23,7 +23,7 @@ public class BinaryDenseVectorScriptDocValuesTests extends ESTestCase { public void testGetVectorValueAndGetMagnitude() throws IOException { int dims = 3; - float[][] vectors = {{ 1, 1, 1 }, { 1, 1, 2 }, { 1, 1, 3 } }; + float[][] vectors = { { 1, 1, 1 }, { 1, 1, 2 }, { 1, 1, 3 } }; float[] expectedMagnitudes = { 1.7320f, 2.4495f, 3.3166f }; for (Version indexVersion : Arrays.asList(Version.V_7_4_0, Version.CURRENT)) { @@ -39,7 +39,7 @@ public void testGetVectorValueAndGetMagnitude() throws IOException { public void testMissingValues() throws IOException { int dims = 3; - float[][] vectors = {{ 1, 1, 1 }, { 1, 1, 2 }, { 1, 1, 3 } }; + float[][] vectors = { { 1, 1, 1 }, { 1, 1, 2 }, { 1, 1, 3 } }; BinaryDocValues docValues = wrap(vectors, Version.CURRENT); DenseVectorScriptDocValues scriptDocValues = new BinaryDenseVectorScriptDocValues(docValues, Version.CURRENT, dims); @@ -53,7 +53,7 @@ public void testMissingValues() throws IOException { public void testGetFunctionIsNotAccessible() throws IOException { int dims = 3; - float[][] vectors = {{ 1, 1, 1 }, { 1, 1, 2 }, { 1, 1, 3 } }; + float[][] vectors = { { 1, 1, 1 }, { 1, 1, 2 }, { 1, 1, 3 } }; BinaryDocValues docValues = wrap(vectors, Version.CURRENT); DenseVectorScriptDocValues scriptDocValues = new BinaryDenseVectorScriptDocValues(docValues, Version.CURRENT, dims); @@ -64,21 +64,23 @@ public void testGetFunctionIsNotAccessible() throws IOException { public void testSimilarityFunctions() throws IOException { int dims = 5; - float[] docVector = new float[] {230.0f, 300.33f, -34.8988f, 15.555f, -200.0f}; - float[] queryVector = new float[] {0.5f, 111.3f, -13.0f, 14.8f, -156.0f}; + float[] docVector = new float[] { 230.0f, 300.33f, -34.8988f, 15.555f, -200.0f }; + float[] queryVector = new float[] { 0.5f, 111.3f, -13.0f, 14.8f, -156.0f }; for (Version indexVersion : Arrays.asList(Version.V_7_4_0, Version.CURRENT)) { - BinaryDocValues docValues = wrap(new float[][]{docVector}, indexVersion); + BinaryDocValues docValues = wrap(new float[][] { docVector }, indexVersion); DenseVectorScriptDocValues scriptDocValues = new BinaryDenseVectorScriptDocValues(docValues, Version.CURRENT, dims); scriptDocValues.setNextDocId(0); - assertEquals("dotProduct result is not equal to the expected value!", - 65425.624, scriptDocValues.dotProduct(queryVector), 0.001); - assertEquals("l1norm result is not equal to the expected value!", 485.184, - scriptDocValues.l1Norm(queryVector), 0.001); - assertEquals("l2norm result is not equal to the expected value!", 301.361, - scriptDocValues.l2Norm(queryVector), 0.001); + assertEquals( + "dotProduct result is not equal to the expected value!", + 65425.624, + scriptDocValues.dotProduct(queryVector), + 0.001 + ); + assertEquals("l1norm result is not equal to the expected value!", 485.184, scriptDocValues.l1Norm(queryVector), 0.001); + assertEquals("l2norm result is not equal to the expected value!", 301.361, scriptDocValues.l2Norm(queryVector), 0.001); } } @@ -86,6 +88,7 @@ static BinaryDocValues wrap(float[][] vectors, Version indexVersion) { return new BinaryDocValues() { int idx = -1; int maxIdx = vectors.length; + @Override public BytesRef binaryValue() { if (idx >= maxIdx) { diff --git a/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/query/DenseVectorFunctionTests.java b/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/query/DenseVectorFunctionTests.java index 0b3557a9e70dd..1cd89e4993c7e 100644 --- a/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/query/DenseVectorFunctionTests.java +++ b/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/query/DenseVectorFunctionTests.java @@ -30,12 +30,12 @@ public class DenseVectorFunctionTests extends ESTestCase { public void testVectorFunctions() { String field = "vector"; int dims = 5; - float[] docVector = new float[] {230.0f, 300.33f, -34.8988f, 15.555f, -200.0f}; + float[] docVector = new float[] { 230.0f, 300.33f, -34.8988f, 15.555f, -200.0f }; List queryVector = Arrays.asList(0.5f, 111.3f, -13.0f, 14.8f, -156.0f); List invalidQueryVector = Arrays.asList(0.5, 111.3); for (Version indexVersion : Arrays.asList(Version.V_7_4_0, Version.CURRENT)) { - BinaryDocValues docValues = BinaryDenseVectorScriptDocValuesTests.wrap(new float[][]{docVector}, indexVersion); + BinaryDocValues docValues = BinaryDenseVectorScriptDocValuesTests.wrap(new float[][] { docVector }, indexVersion); DenseVectorScriptDocValues scriptDocValues = new BinaryDenseVectorScriptDocValues(docValues, indexVersion, dims); ScoreScript scoreScript = mock(ScoreScript.class); @@ -43,8 +43,7 @@ public void testVectorFunctions() { // Test cosine similarity explicitly, as it must perform special logic on top of the doc values CosineSimilarity function = new CosineSimilarity(scoreScript, queryVector, field); - assertEquals("cosineSimilarity result is not equal to the expected value!", - 0.790, function.cosineSimilarity(), 0.001); + assertEquals("cosineSimilarity result is not equal to the expected value!", 0.790, function.cosineSimilarity(), 0.001); // Check each function rejects query vectors with the wrong dimension assertDimensionMismatch(() -> new DotProduct(scoreScript, invalidQueryVector, field)); diff --git a/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/query/KnnDenseVectorScriptDocValuesTests.java b/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/query/KnnDenseVectorScriptDocValuesTests.java index 507f24886767f..319a98a619bf6 100644 --- a/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/query/KnnDenseVectorScriptDocValuesTests.java +++ b/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/query/KnnDenseVectorScriptDocValuesTests.java @@ -19,7 +19,7 @@ public class KnnDenseVectorScriptDocValuesTests extends ESTestCase { public void testGetVectorValueAndGetMagnitude() throws IOException { int dims = 3; - float[][] vectors = {{ 1, 1, 1 }, { 1, 1, 2 }, { 1, 1, 3 } }; + float[][] vectors = { { 1, 1, 1 }, { 1, 1, 2 }, { 1, 1, 3 } }; float[] expectedMagnitudes = { 1.7320f, 2.4495f, 3.3166f }; DenseVectorScriptDocValues scriptDocValues = new KnnDenseVectorScriptDocValues(wrap(vectors), dims); @@ -32,7 +32,7 @@ public void testGetVectorValueAndGetMagnitude() throws IOException { public void testMissingValues() throws IOException { int dims = 3; - float[][] vectors = {{ 1, 1, 1 }, { 1, 1, 2 }, { 1, 1, 3 } }; + float[][] vectors = { { 1, 1, 1 }, { 1, 1, 2 }, { 1, 1, 3 } }; DenseVectorScriptDocValues scriptDocValues = new KnnDenseVectorScriptDocValues(wrap(vectors), dims); scriptDocValues.setNextDocId(3); @@ -45,7 +45,7 @@ public void testMissingValues() throws IOException { public void testGetFunctionIsNotAccessible() throws IOException { int dims = 3; - float[][] vectors = {{ 1, 1, 1 }, { 1, 1, 2 }, { 1, 1, 3 } }; + float[][] vectors = { { 1, 1, 1 }, { 1, 1, 2 }, { 1, 1, 3 } }; DenseVectorScriptDocValues scriptDocValues = new KnnDenseVectorScriptDocValues(wrap(vectors), dims); scriptDocValues.setNextDocId(0); @@ -55,18 +55,15 @@ public void testGetFunctionIsNotAccessible() throws IOException { public void testSimilarityFunctions() throws IOException { int dims = 5; - float[] docVector = new float[] {230.0f, 300.33f, -34.8988f, 15.555f, -200.0f}; - float[] queryVector = new float[] {0.5f, 111.3f, -13.0f, 14.8f, -156.0f}; + float[] docVector = new float[] { 230.0f, 300.33f, -34.8988f, 15.555f, -200.0f }; + float[] queryVector = new float[] { 0.5f, 111.3f, -13.0f, 14.8f, -156.0f }; - DenseVectorScriptDocValues scriptDocValues = new KnnDenseVectorScriptDocValues(wrap(new float[][]{docVector}), dims); + DenseVectorScriptDocValues scriptDocValues = new KnnDenseVectorScriptDocValues(wrap(new float[][] { docVector }), dims); scriptDocValues.setNextDocId(0); - assertEquals("dotProduct result is not equal to the expected value!", - 65425.624, scriptDocValues.dotProduct(queryVector), 0.001); - assertEquals("l1norm result is not equal to the expected value!", 485.184, - scriptDocValues.l1Norm(queryVector), 0.001); - assertEquals("l2norm result is not equal to the expected value!", 301.361, - scriptDocValues.l2Norm(queryVector), 0.001); + assertEquals("dotProduct result is not equal to the expected value!", 65425.624, scriptDocValues.dotProduct(queryVector), 0.001); + assertEquals("l1norm result is not equal to the expected value!", 485.184, scriptDocValues.l1Norm(queryVector), 0.001); + assertEquals("l2norm result is not equal to the expected value!", 301.361, scriptDocValues.l2Norm(queryVector), 0.001); } private static VectorValues wrap(float[][] vectors) { diff --git a/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/query/KnnSearchActionTests.java b/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/query/KnnSearchActionTests.java index 0a8235204ce93..121621096c69b 100644 --- a/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/query/KnnSearchActionTests.java +++ b/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/query/KnnSearchActionTests.java @@ -31,29 +31,24 @@ protected Collection> getPlugins() { } public void testTotalHits() throws IOException { - Settings indexSettings = Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .build(); - XContentBuilder builder = XContentFactory.jsonBuilder().startObject().startObject("properties") - .startObject("vector") - .field("type", "dense_vector") - .field("dims", VECTOR_DIMENSION) - .field("index", true) - .field("similarity", "l2_norm") - .endObject() - .endObject().endObject(); + Settings indexSettings = Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).build(); + XContentBuilder builder = XContentFactory.jsonBuilder() + .startObject() + .startObject("properties") + .startObject("vector") + .field("type", "dense_vector") + .field("dims", VECTOR_DIMENSION) + .field("index", true) + .field("similarity", "l2_norm") + .endObject() + .endObject() + .endObject(); createIndex("index1", indexSettings, builder); createIndex("index2", indexSettings, builder); for (int doc = 0; doc < 10; doc++) { - client().prepareIndex("index1") - .setId(String.valueOf(doc)) - .setSource("vector", randomVector()) - .get(); - client().prepareIndex("index2") - .setId(String.valueOf(doc)) - .setSource("vector", randomVector()) - .get(); + client().prepareIndex("index1").setId(String.valueOf(doc)).setSource("vector", randomVector()).get(); + client().prepareIndex("index2").setId(String.valueOf(doc)).setSource("vector", randomVector()).get(); } client().admin().indices().prepareForceMerge("index1", "index2").setMaxNumSegments(1).get(); diff --git a/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/query/KnnVectorQueryBuilderTests.java b/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/query/KnnVectorQueryBuilderTests.java index 138d22d0e25e9..66713cee92a29 100644 --- a/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/query/KnnVectorQueryBuilderTests.java +++ b/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/query/KnnVectorQueryBuilderTests.java @@ -40,20 +40,26 @@ protected Collection> getPlugins() { @Override protected void initializeAdditionalMappings(MapperService mapperService) throws IOException { - XContentBuilder builder = XContentFactory.jsonBuilder().startObject().startObject("properties") - .startObject(VECTOR_FIELD) - .field("type", "dense_vector") - .field("dims", VECTOR_DIMENSION) - .field("index", true) - .field("similarity", "l2_norm") - .endObject() - .startObject(VECTOR_ALIAS_FIELD) - .field("type", "alias") - .field("path", VECTOR_FIELD) - .endObject() - .endObject().endObject(); - mapperService.merge(MapperService.SINGLE_MAPPING_NAME, - new CompressedXContent(Strings.toString(builder)), MapperService.MergeReason.MAPPING_UPDATE); + XContentBuilder builder = XContentFactory.jsonBuilder() + .startObject() + .startObject("properties") + .startObject(VECTOR_FIELD) + .field("type", "dense_vector") + .field("dims", VECTOR_DIMENSION) + .field("index", true) + .field("similarity", "l2_norm") + .endObject() + .startObject(VECTOR_ALIAS_FIELD) + .field("type", "alias") + .field("path", VECTOR_FIELD) + .endObject() + .endObject() + .endObject(); + mapperService.merge( + MapperService.SINGLE_MAPPING_NAME, + new CompressedXContent(Strings.toString(builder)), + MapperService.MergeReason.MAPPING_UPDATE + ); } @Override @@ -74,48 +80,48 @@ protected void doAssertLuceneQuery(KnnVectorQueryBuilder queryBuilder, Query que KnnVectorQuery knnVectorQuery = (KnnVectorQuery) query; // The field should always be resolved to the concrete field - assertThat(knnVectorQuery, equalTo(new KnnVectorQuery(VECTOR_FIELD, - queryBuilder.queryVector(), - queryBuilder.numCands()))); + assertThat(knnVectorQuery, equalTo(new KnnVectorQuery(VECTOR_FIELD, queryBuilder.queryVector(), queryBuilder.numCands()))); } - public void testWrongDimension() { + public void testWrongDimension() { SearchExecutionContext context = createSearchExecutionContext(); - KnnVectorQueryBuilder query = new KnnVectorQueryBuilder(VECTOR_FIELD, new float[] {1.0f, 2.0f}, 10); + KnnVectorQueryBuilder query = new KnnVectorQueryBuilder(VECTOR_FIELD, new float[] { 1.0f, 2.0f }, 10); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> query.doToQuery(context)); assertThat(e.getMessage(), containsString("the query vector has a different dimension [2] than the index vectors [3]")); } - public void testNonexistentField() { + public void testNonexistentField() { SearchExecutionContext context = createSearchExecutionContext(); - KnnVectorQueryBuilder query = new KnnVectorQueryBuilder("nonexistent", - new float[]{1.0f, 1.0f, 1.0f}, 10); + KnnVectorQueryBuilder query = new KnnVectorQueryBuilder("nonexistent", new float[] { 1.0f, 1.0f, 1.0f }, 10); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> query.doToQuery(context)); assertThat(e.getMessage(), containsString("field [nonexistent] does not exist in the mapping")); } - public void testWrongFieldType() { + public void testWrongFieldType() { SearchExecutionContext context = createSearchExecutionContext(); - KnnVectorQueryBuilder query = new KnnVectorQueryBuilder(AbstractBuilderTestCase.KEYWORD_FIELD_NAME, - new float[]{1.0f, 1.0f, 1.0f}, 10); + KnnVectorQueryBuilder query = new KnnVectorQueryBuilder( + AbstractBuilderTestCase.KEYWORD_FIELD_NAME, + new float[] { 1.0f, 1.0f, 1.0f }, + 10 + ); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> query.doToQuery(context)); assertThat(e.getMessage(), containsString("[knn] queries are only supported on [dense_vector] fields")); } @Override public void testValidOutput() { - KnnVectorQueryBuilder query = new KnnVectorQueryBuilder(VECTOR_FIELD, new float[] {1.0f, 2.0f, 3.0f}, 10); - String expected = "{\n" + - " \"knn\" : {\n" + - " \"field\" : \"vector\",\n" + - " \"vector\" : [\n" + - " 1.0,\n" + - " 2.0,\n" + - " 3.0\n" + - " ],\n" + - " \"num_candidates\" : 10\n" + - " }\n" + - "}"; + KnnVectorQueryBuilder query = new KnnVectorQueryBuilder(VECTOR_FIELD, new float[] { 1.0f, 2.0f, 3.0f }, 10); + String expected = "{\n" + + " \"knn\" : {\n" + + " \"field\" : \"vector\",\n" + + " \"vector\" : [\n" + + " 1.0,\n" + + " 2.0,\n" + + " 3.0\n" + + " ],\n" + + " \"num_candidates\" : 10\n" + + " }\n" + + "}"; assertEquals(expected, query.toString()); } diff --git a/x-pack/plugin/watcher/qa/rest/src/javaRestTest/java/org/elasticsearch/smoketest/SmokeTestWatcherTestSuiteIT.java b/x-pack/plugin/watcher/qa/rest/src/javaRestTest/java/org/elasticsearch/smoketest/SmokeTestWatcherTestSuiteIT.java index eaca29a6db1a0..e05979034d94f 100644 --- a/x-pack/plugin/watcher/qa/rest/src/javaRestTest/java/org/elasticsearch/smoketest/SmokeTestWatcherTestSuiteIT.java +++ b/x-pack/plugin/watcher/qa/rest/src/javaRestTest/java/org/elasticsearch/smoketest/SmokeTestWatcherTestSuiteIT.java @@ -13,17 +13,17 @@ import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.test.rest.yaml.ObjectPath; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.rest.yaml.ObjectPath; import org.elasticsearch.xpack.watcher.WatcherRestTestCase; import java.io.IOException; import java.util.Map; import java.util.concurrent.atomic.AtomicReference; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.rest.action.search.RestSearchAction.TOTAL_HITS_AS_INT_PARAM; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.Matchers.greaterThanOrEqualTo; import static org.hamcrest.Matchers.hasEntry; import static org.hamcrest.Matchers.is; @@ -70,18 +70,37 @@ public void testMonitorClusterHealth() throws Exception { // trigger builder.startObject("trigger").startObject("schedule").field("interval", "1s").endObject().endObject(); // input - builder.startObject("input").startObject("http").startObject("request").field("host", host).field("port", port) + builder.startObject("input") + .startObject("http") + .startObject("request") + .field("host", host) + .field("port", port) .field("path", "/_cluster/health") .field("scheme", "http") - .startObject("auth").startObject("basic") - .field("username", TEST_ADMIN_USERNAME).field("password", TEST_ADMIN_PASSWORD) - .endObject().endObject() - .endObject().endObject().endObject(); + .startObject("auth") + .startObject("basic") + .field("username", TEST_ADMIN_USERNAME) + .field("password", TEST_ADMIN_PASSWORD) + .endObject() + .endObject() + .endObject() + .endObject() + .endObject(); // condition - builder.startObject("condition").startObject("compare").startObject("ctx.payload.number_of_data_nodes").field("lt", 10) - .endObject().endObject().endObject(); + builder.startObject("condition") + .startObject("compare") + .startObject("ctx.payload.number_of_data_nodes") + .field("lt", 10) + .endObject() + .endObject() + .endObject(); // actions - builder.startObject("actions").startObject("log").startObject("logging").field("text", "executed").endObject().endObject() + builder.startObject("actions") + .startObject("log") + .startObject("logging") + .field("text", "executed") + .endObject() + .endObject() .endObject(); builder.endObject(); @@ -188,7 +207,7 @@ private ObjectPath getWatchHistoryEntry(String watchId) throws Exception { logger.info("Found [{}] hits in watcher history", totalHits); assertThat(totalHits, is(greaterThanOrEqualTo(1))); String foundWatchId = objectPath.evaluate("hits.hits.0._source.watch_id"); - logger.info("Watch hit 0 has id [{}] (expecting [{}])", foundWatchId, watchId); + logger.info("Watch hit 0 has id [{}] (expecting [{}])", foundWatchId, watchId); assertThat("watch_id for hit 0 in watcher history", foundWatchId, is(watchId)); objectPathReference.set(objectPath); } catch (ResponseException e) { diff --git a/x-pack/plugin/watcher/qa/with-monitoring/src/javaRestTest/java/org/elasticsearch/smoketest/MonitoringWithWatcherRestIT.java b/x-pack/plugin/watcher/qa/with-monitoring/src/javaRestTest/java/org/elasticsearch/smoketest/MonitoringWithWatcherRestIT.java index e2aa12023cfd1..27914d7154eac 100644 --- a/x-pack/plugin/watcher/qa/with-monitoring/src/javaRestTest/java/org/elasticsearch/smoketest/MonitoringWithWatcherRestIT.java +++ b/x-pack/plugin/watcher/qa/with-monitoring/src/javaRestTest/java/org/elasticsearch/smoketest/MonitoringWithWatcherRestIT.java @@ -30,30 +30,35 @@ public class MonitoringWithWatcherRestIT extends ESRestTestCase { "kibana_version_mismatch", "logstash_version_mismatch", "xpack_license_expiration", - "elasticsearch_nodes", - }; + "elasticsearch_nodes", }; @After public void cleanExporters() throws Exception { Request cleanupSettingsRequest = new Request("PUT", "/_cluster/settings"); - cleanupSettingsRequest.setJsonEntity(Strings.toString(jsonBuilder().startObject() - .startObject("persistent") - .nullField("xpack.monitoring.exporters.*") - .endObject().endObject())); + cleanupSettingsRequest.setJsonEntity( + Strings.toString( + jsonBuilder().startObject().startObject("persistent").nullField("xpack.monitoring.exporters.*").endObject().endObject() + ) + ); adminClient().performRequest(cleanupSettingsRequest); deleteAllWatcherData(); } - @AwaitsFix( bugUrl = "https://github.com/elastic/elasticsearch/issues/59132" ) + @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/59132") public void testThatLocalExporterAddsWatches() throws Exception { String watchId = createMonitoringWatch(); Request request = new Request("PUT", "/_cluster/settings"); - request.setJsonEntity(Strings.toString(jsonBuilder().startObject() - .startObject("persistent") + request.setJsonEntity( + Strings.toString( + jsonBuilder().startObject() + .startObject("persistent") .field("xpack.monitoring.exporters.my_local_exporter.type", "local") .field("xpack.monitoring.exporters.my_local_exporter.cluster_alerts.management.enabled", true) - .endObject().endObject())); + .endObject() + .endObject() + ) + ); adminClient().performRequest(request); assertTotalWatchCount(WATCH_IDS.length); @@ -83,9 +88,9 @@ private String createMonitoringWatch() throws Exception { String clusterUUID = getClusterUUID(); String watchId = clusterUUID + "_kibana_version_mismatch"; Request request = new Request("PUT", "/_watcher/watch/" + watchId); - String watch = "{\"trigger\":{\"schedule\":{\"interval\":\"1000m\"}},\"input\":{\"simple\":{}}," + - "\"condition\":{\"always\":{}}," + - "\"actions\":{\"logme\":{\"logging\":{\"level\":\"info\",\"text\":\"foo\"}}}}"; + String watch = "{\"trigger\":{\"schedule\":{\"interval\":\"1000m\"}},\"input\":{\"simple\":{}}," + + "\"condition\":{\"always\":{}}," + + "\"actions\":{\"logme\":{\"logging\":{\"level\":\"info\",\"text\":\"foo\"}}}}"; request.setJsonEntity(watch); client().performRequest(request); return watchId; diff --git a/x-pack/plugin/watcher/qa/with-security/src/javaRestTest/java/org/elasticsearch/smoketest/SmokeTestWatcherWithSecurityIT.java b/x-pack/plugin/watcher/qa/with-security/src/javaRestTest/java/org/elasticsearch/smoketest/SmokeTestWatcherWithSecurityIT.java index 510b8df341505..8f5d3d8242d44 100644 --- a/x-pack/plugin/watcher/qa/with-security/src/javaRestTest/java/org/elasticsearch/smoketest/SmokeTestWatcherWithSecurityIT.java +++ b/x-pack/plugin/watcher/qa/with-security/src/javaRestTest/java/org/elasticsearch/smoketest/SmokeTestWatcherWithSecurityIT.java @@ -14,8 +14,8 @@ import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.test.rest.yaml.ObjectPath; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.watcher.WatcherRestTestCase; import org.junit.Before; @@ -23,8 +23,8 @@ import java.util.Map; import java.util.concurrent.atomic.AtomicReference; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.rest.action.search.RestSearchAction.TOTAL_HITS_AS_INT_PARAM; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.Matchers.greaterThanOrEqualTo; import static org.hamcrest.Matchers.hasEntry; import static org.hamcrest.Matchers.is; @@ -47,7 +47,7 @@ public void beforeTest() throws Exception { createAllowedDoc.addParameter("refresh", "true"); adminClient().performRequest(createAllowedDoc); - // create one document in this index, so we can test that the index cannot be accessed + // create one document in this index, so we can test that the index cannot be accessed Request createNotAllowedDoc = new Request("PUT", "/index_not_allowed_to_read/_doc/1"); createNotAllowedDoc.setJsonEntity("{\"foo\":\"bar\"}"); adminClient().performRequest(createNotAllowedDoc); @@ -56,32 +56,48 @@ public void beforeTest() throws Exception { @Override protected Settings restClientSettings() { String token = basicAuthHeaderValue("watcher_manager", new SecureString("x-pack-test-password".toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } @Override protected Settings restAdminSettings() { String token = basicAuthHeaderValue(TEST_ADMIN_USERNAME, new SecureString(TEST_ADMIN_PASSWORD.toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } - public void testSearchInputHasPermissions() throws Exception { try (XContentBuilder builder = jsonBuilder()) { builder.startObject(); builder.startObject("trigger").startObject("schedule").field("interval", "1s").endObject().endObject(); - builder.startObject("input").startObject("search").startObject("request") - .startArray("indices").value("my_test_index").endArray() - .startObject("body").startObject("query").startObject("match_all").endObject().endObject().endObject() - .endObject().endObject().endObject(); - builder.startObject("condition").startObject("compare").startObject("ctx.payload.hits.total").field("gte", 1) - .endObject().endObject().endObject(); - builder.startObject("actions").startObject("logging").startObject("logging") - .field("text", "successfully ran " + watchId + "to test for search input").endObject().endObject().endObject(); + builder.startObject("input") + .startObject("search") + .startObject("request") + .startArray("indices") + .value("my_test_index") + .endArray() + .startObject("body") + .startObject("query") + .startObject("match_all") + .endObject() + .endObject() + .endObject() + .endObject() + .endObject() + .endObject(); + builder.startObject("condition") + .startObject("compare") + .startObject("ctx.payload.hits.total") + .field("gte", 1) + .endObject() + .endObject() + .endObject(); + builder.startObject("actions") + .startObject("logging") + .startObject("logging") + .field("text", "successfully ran " + watchId + "to test for search input") + .endObject() + .endObject() + .endObject(); builder.endObject(); indexWatch(watchId, builder); @@ -98,14 +114,35 @@ public void testSearchInputWithInsufficientPrivileges() throws Exception { try (XContentBuilder builder = jsonBuilder()) { builder.startObject(); builder.startObject("trigger").startObject("schedule").field("interval", "4s").endObject().endObject(); - builder.startObject("input").startObject("search").startObject("request") - .startArray("indices").value(indexName).endArray() - .startObject("body").startObject("query").startObject("match_all").endObject().endObject().endObject() - .endObject().endObject().endObject(); - builder.startObject("condition").startObject("compare").startObject("ctx.payload.hits.total").field("gte", 1) - .endObject().endObject().endObject(); - builder.startObject("actions").startObject("logging").startObject("logging") - .field("text", "this should never be logged").endObject().endObject().endObject(); + builder.startObject("input") + .startObject("search") + .startObject("request") + .startArray("indices") + .value(indexName) + .endArray() + .startObject("body") + .startObject("query") + .startObject("match_all") + .endObject() + .endObject() + .endObject() + .endObject() + .endObject() + .endObject(); + builder.startObject("condition") + .startObject("compare") + .startObject("ctx.payload.hits.total") + .field("gte", 1) + .endObject() + .endObject() + .endObject(); + builder.startObject("actions") + .startObject("logging") + .startObject("logging") + .field("text", "this should never be logged") + .endObject() + .endObject() + .endObject(); builder.endObject(); indexWatch(watchId, builder); @@ -122,14 +159,29 @@ public void testSearchTransformHasPermissions() throws Exception { builder.startObject(); builder.startObject("trigger").startObject("schedule").field("interval", "1s").endObject().endObject(); builder.startObject("input").startObject("simple").field("foo", "bar").endObject().endObject(); - builder.startObject("transform").startObject("search").startObject("request") - .startArray("indices").value("my_test_index").endArray() - .startObject("body").startObject("query").startObject("match_all").endObject().endObject().endObject() - .endObject().endObject().endObject(); - builder.startObject("actions").startObject("index").startObject("index") - .field("index", "my_test_index") - .field("doc_id", "my-id") - .endObject().endObject().endObject(); + builder.startObject("transform") + .startObject("search") + .startObject("request") + .startArray("indices") + .value("my_test_index") + .endArray() + .startObject("body") + .startObject("query") + .startObject("match_all") + .endObject() + .endObject() + .endObject() + .endObject() + .endObject() + .endObject(); + builder.startObject("actions") + .startObject("index") + .startObject("index") + .field("index", "my_test_index") + .field("doc_id", "my-id") + .endObject() + .endObject() + .endObject(); builder.endObject(); indexWatch(watchId, builder); @@ -150,16 +202,36 @@ public void testSearchTransformInsufficientPermissions() throws Exception { builder.startObject(); builder.startObject("trigger").startObject("schedule").field("interval", "1s").endObject().endObject(); builder.startObject("input").startObject("simple").field("foo", "bar").endObject().endObject(); - builder.startObject("transform").startObject("search").startObject("request") - .startArray("indices").value("index_not_allowed_to_read").endArray() - .startObject("body").startObject("query").startObject("match_all").endObject().endObject().endObject() - .endObject().endObject().endObject(); - builder.startObject("condition").startObject("compare").startObject("ctx.payload.hits.total").field("gte", 1) - .endObject().endObject().endObject(); - builder.startObject("actions").startObject("index").startObject("index") - .field("index", "my_test_index") - .field("doc_id", "some-id") - .endObject().endObject().endObject(); + builder.startObject("transform") + .startObject("search") + .startObject("request") + .startArray("indices") + .value("index_not_allowed_to_read") + .endArray() + .startObject("body") + .startObject("query") + .startObject("match_all") + .endObject() + .endObject() + .endObject() + .endObject() + .endObject() + .endObject(); + builder.startObject("condition") + .startObject("compare") + .startObject("ctx.payload.hits.total") + .field("gte", 1) + .endObject() + .endObject() + .endObject(); + builder.startObject("actions") + .startObject("index") + .startObject("index") + .field("index", "my_test_index") + .field("doc_id", "some-id") + .endObject() + .endObject() + .endObject(); builder.endObject(); indexWatch(watchId, builder); @@ -176,10 +248,14 @@ public void testIndexActionHasPermissions() throws Exception { builder.startObject(); builder.startObject("trigger").startObject("schedule").field("interval", "1s").endObject().endObject(); builder.startObject("input").startObject("simple").field("spam", "eggs").endObject().endObject(); - builder.startObject("actions").startObject("index").startObject("index") - .field("index", "my_test_index") - .field("doc_id", "my-id") - .endObject().endObject().endObject(); + builder.startObject("actions") + .startObject("index") + .startObject("index") + .field("index", "my_test_index") + .field("doc_id", "my-id") + .endObject() + .endObject() + .endObject(); builder.endObject(); indexWatch(watchId, builder); @@ -199,10 +275,14 @@ public void testIndexActionInsufficientPrivileges() throws Exception { builder.startObject(); builder.startObject("trigger").startObject("schedule").field("interval", "1s").endObject().endObject(); builder.startObject("input").startObject("simple").field("spam", "eggs").endObject().endObject(); - builder.startObject("actions").startObject("index").startObject("index") - .field("index", "index_not_allowed_to_read") - .field("doc_id", "my-id") - .endObject().endObject().endObject(); + builder.startObject("actions") + .startObject("index") + .startObject("index") + .field("index", "index_not_allowed_to_read") + .field("doc_id", "my-id") + .endObject() + .endObject() + .endObject(); builder.endObject(); indexWatch(watchId, builder); @@ -239,15 +319,30 @@ private ObjectPath getWatchHistoryEntry(String watchId, String state) throws Exc try (XContentBuilder builder = jsonBuilder()) { builder.startObject(); builder.startObject("query").startObject("bool").startArray("must"); - builder.startObject().startObject("term").startObject("watch_id").field("value", watchId).endObject().endObject() + builder.startObject() + .startObject("term") + .startObject("watch_id") + .field("value", watchId) + .endObject() + .endObject() .endObject(); if (Strings.isNullOrEmpty(state) == false) { - builder.startObject().startObject("term").startObject("state").field("value", state).endObject().endObject() + builder.startObject() + .startObject("term") + .startObject("state") + .field("value", state) + .endObject() + .endObject() .endObject(); } builder.endArray().endObject().endObject(); - builder.startArray("sort").startObject().startObject("trigger_event.triggered_time").field("order", "desc").endObject() - .endObject().endArray(); + builder.startArray("sort") + .startObject() + .startObject("trigger_event.triggered_time") + .field("order", "desc") + .endObject() + .endObject() + .endArray(); builder.endObject(); Request searchRequest = new Request("POST", "/.watcher-history-*/_search"); diff --git a/x-pack/plugin/watcher/qa/with-security/src/yamlRestTest/java/org/elasticsearch/smoketest/SmokeTestWatcherWithSecurityClientYamlTestSuiteIT.java b/x-pack/plugin/watcher/qa/with-security/src/yamlRestTest/java/org/elasticsearch/smoketest/SmokeTestWatcherWithSecurityClientYamlTestSuiteIT.java index 8f07ee64a63f8..78e2f8ad8cd7f 100644 --- a/x-pack/plugin/watcher/qa/with-security/src/yamlRestTest/java/org/elasticsearch/smoketest/SmokeTestWatcherWithSecurityClientYamlTestSuiteIT.java +++ b/x-pack/plugin/watcher/qa/with-security/src/yamlRestTest/java/org/elasticsearch/smoketest/SmokeTestWatcherWithSecurityClientYamlTestSuiteIT.java @@ -8,6 +8,7 @@ import com.carrotsearch.randomizedtesting.annotations.Name; import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; + import org.elasticsearch.client.Request; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; @@ -41,16 +42,12 @@ public void beforeTest() throws Exception { @Override protected Settings restClientSettings() { String token = basicAuthHeaderValue("watcher_manager", new SecureString("x-pack-test-password".toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } @Override protected Settings restAdminSettings() { String token = basicAuthHeaderValue(TEST_ADMIN_USERNAME, new SecureString(TEST_ADMIN_PASSWORD.toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } } diff --git a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/WatcherConcreteIndexTests.java b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/WatcherConcreteIndexTests.java index 3d54120c03cd3..8df698d8150de 100644 --- a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/WatcherConcreteIndexTests.java +++ b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/WatcherConcreteIndexTests.java @@ -35,12 +35,12 @@ public void testCanUseAnyConcreteIndexName() throws Exception { ensureGreen(newWatcherIndexName); startWatcher(); - PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client(), "mywatch").setSource(watchBuilder() - .trigger(schedule(interval("3s"))) - .input(noneInput()) - .condition(InternalAlwaysCondition.INSTANCE) - .addAction("indexer", indexAction(watchResultsIndex))) - .get(); + PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client(), "mywatch").setSource( + watchBuilder().trigger(schedule(interval("3s"))) + .input(noneInput()) + .condition(InternalAlwaysCondition.INSTANCE) + .addAction("indexer", indexAction(watchResultsIndex)) + ).get(); assertTrue(putWatchResponse.isCreated()); refresh(); diff --git a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/actions/ActionErrorIntegrationTests.java b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/actions/ActionErrorIntegrationTests.java index f8ce41a537610..5f1fad6d89096 100644 --- a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/actions/ActionErrorIntegrationTests.java +++ b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/actions/ActionErrorIntegrationTests.java @@ -35,14 +35,14 @@ public void testErrorInAction() throws Exception { createIndex("foo"); client().admin().indices().prepareUpdateSettings("foo").setSettings(Settings.builder().put("index.blocks.write", true)).get(); - PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client(), "_id").setSource(watchBuilder() - .trigger(schedule(interval("10m"))) + PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client(), "_id").setSource( + watchBuilder().trigger(schedule(interval("10m"))) - // adding an action that throws an error and is associated with a 60 minute throttle period - // with such a period, on successful execution we other executions of the watch will be - // throttled within the hour... but on failed execution there should be no throttling - .addAction("_action", TimeValue.timeValueMinutes(60), IndexAction.builder("foo"))) - .get(); + // adding an action that throws an error and is associated with a 60 minute throttle period + // with such a period, on successful execution we other executions of the watch will be + // throttled within the hour... but on failed execution there should be no throttling + .addAction("_action", TimeValue.timeValueMinutes(60), IndexAction.builder("foo")) + ).get(); assertThat(putWatchResponse.isCreated(), is(true)); @@ -52,9 +52,11 @@ public void testErrorInAction() throws Exception { // there should be a single history record with a failure status for the action: assertBusy(() -> { - long count = watchRecordCount(QueryBuilders.boolQuery() + long count = watchRecordCount( + QueryBuilders.boolQuery() .must(termsQuery("result.actions.id", "_action")) - .must(termsQuery("result.actions.status", "failure"))); + .must(termsQuery("result.actions.status", "failure")) + ); assertThat(count, is(1L)); }); @@ -69,9 +71,11 @@ public void testErrorInAction() throws Exception { // there should be a single history record with a failure status for the action: assertBusy(() -> { - long count = watchRecordCount(QueryBuilders.boolQuery() + long count = watchRecordCount( + QueryBuilders.boolQuery() .must(termsQuery("result.actions.id", "_action")) - .must(termsQuery("result.actions.status", "failure"))); + .must(termsQuery("result.actions.status", "failure")) + ); assertThat(count, is(2L)); }); diff --git a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/actions/TimeThrottleIntegrationTests.java b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/actions/TimeThrottleIntegrationTests.java index 42365b38958e2..14b1e3376ebab 100644 --- a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/actions/TimeThrottleIntegrationTests.java +++ b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/actions/TimeThrottleIntegrationTests.java @@ -8,12 +8,12 @@ import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.ObjectPath; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.protocol.xpack.watcher.PutWatchResponse; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.sort.SortBuilders; import org.elasticsearch.search.sort.SortOrder; +import org.elasticsearch.xcontent.ObjectPath; import org.elasticsearch.xpack.core.watcher.history.HistoryStoreField; import org.elasticsearch.xpack.core.watcher.transport.actions.put.PutWatchRequestBuilder; import org.elasticsearch.xpack.watcher.test.AbstractWatcherIntegrationTestCase; @@ -34,14 +34,14 @@ public class TimeThrottleIntegrationTests extends AbstractWatcherIntegrationTest public void testTimeThrottle() throws Exception { String id = randomAlphaOfLength(20); - PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client()) - .setId(id) - .setSource(watchBuilder() - .trigger(schedule(interval("5s"))) - .input(simpleInput()) - .addAction("my-logging-action", loggingAction("foo")) - .defaultThrottlePeriod(TimeValue.timeValueSeconds(30))) - .get(); + PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client()).setId(id) + .setSource( + watchBuilder().trigger(schedule(interval("5s"))) + .input(simpleInput()) + .addAction("my-logging-action", loggingAction("foo")) + .defaultThrottlePeriod(TimeValue.timeValueSeconds(30)) + ) + .get(); assertThat(putWatchResponse.isCreated(), is(true)); timeWarp().trigger(id); @@ -60,13 +60,13 @@ public void testTimeThrottle() throws Exception { public void testTimeThrottleDefaults() throws Exception { String id = randomAlphaOfLength(30); - PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client()) - .setId(id) - .setSource(watchBuilder() - .trigger(schedule(interval("1s"))) - .input(simpleInput()) - .addAction("my-logging-action", indexAction("my_watcher_index"))) - .get(); + PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client()).setId(id) + .setSource( + watchBuilder().trigger(schedule(interval("1s"))) + .input(simpleInput()) + .addAction("my-logging-action", indexAction("my_watcher_index")) + ) + .get(); assertThat(putWatchResponse.isCreated(), is(true)); timeWarp().trigger(id); @@ -99,11 +99,10 @@ private Map assertLatestHistoryEntry(String id) { refresh(HistoryStoreField.DATA_STREAM + "*"); SearchResponse searchResponse = client().prepareSearch(HistoryStoreField.DATA_STREAM + "*") - .setSize(1) - .setSource(new SearchSourceBuilder().query(QueryBuilders.boolQuery() - .must(termQuery("watch_id", id)))) - .addSort(SortBuilders.fieldSort("result.execution_time").order(SortOrder.DESC)) - .get(); + .setSize(1) + .setSource(new SearchSourceBuilder().query(QueryBuilders.boolQuery().must(termQuery("watch_id", id)))) + .addSort(SortBuilders.fieldSort("result.execution_time").order(SortOrder.DESC)) + .get(); Map map = searchResponse.getHits().getHits()[0].getSourceAsMap(); String actionId = ObjectPath.eval("result.actions.0.id", map); @@ -113,9 +112,9 @@ private Map assertLatestHistoryEntry(String id) { private void assertTotalHistoryEntries(String id, long expectedCount) { SearchResponse searchResponse = client().prepareSearch(HistoryStoreField.DATA_STREAM + "*") - .setSize(0) - .setSource(new SearchSourceBuilder().query(QueryBuilders.boolQuery().must(termQuery("watch_id", id)))) - .get(); + .setSize(0) + .setSource(new SearchSourceBuilder().query(QueryBuilders.boolQuery().must(termQuery("watch_id", id)))) + .get(); assertThat(searchResponse.getHits().getTotalHits().value, is(oneOf(expectedCount, expectedCount + 1))); } diff --git a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/actions/email/EmailAttachmentTests.java b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/actions/email/EmailAttachmentTests.java index d129010f9c5ae..f3e9212991812 100644 --- a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/actions/email/EmailAttachmentTests.java +++ b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/actions/email/EmailAttachmentTests.java @@ -11,11 +11,11 @@ import org.elasticsearch.common.io.Streams; import org.elasticsearch.common.settings.MockSecureSettings; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.test.http.MockResponse; import org.elasticsearch.test.http.MockWebServer; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.watcher.client.WatchSourceBuilder; import org.elasticsearch.xpack.core.watcher.history.HistoryStoreField; import org.elasticsearch.xpack.core.watcher.transport.actions.put.PutWatchRequestBuilder; @@ -32,10 +32,6 @@ import org.elasticsearch.xpack.watcher.trigger.schedule.IntervalSchedule; import org.junit.After; -import javax.mail.BodyPart; -import javax.mail.Multipart; -import javax.mail.Part; -import javax.mail.internet.MimeMessage; import java.io.InputStream; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; @@ -44,8 +40,13 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; +import javax.mail.BodyPart; +import javax.mail.Multipart; +import javax.mail.Part; +import javax.mail.internet.MimeMessage; + import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.xpack.watcher.actions.ActionBuilders.emailAction; import static org.elasticsearch.xpack.watcher.client.WatchSourceBuilders.watchBuilder; import static org.elasticsearch.xpack.watcher.input.InputBuilders.noneInput; @@ -63,7 +64,8 @@ public class EmailAttachmentTests extends AbstractWatcherIntegrationTestCase { private MockWebServer webServer = new MockWebServer(); private MockResponse mockResponse = new MockResponse().setResponseCode(200) - .addHeader("Content-Type", "application/foo").setBody("This is the content"); + .addHeader("Content-Type", "application/foo") + .setBody("This is the content"); private EmailServer server; @Override @@ -86,19 +88,18 @@ protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { final MockSecureSettings secureSettings = new MockSecureSettings(); secureSettings.setString("xpack.notification.email.account.test.smtp.secure_password", EmailServer.PASSWORD); return Settings.builder() - .put(super.nodeSettings(nodeOrdinal, otherSettings)) - .put("xpack.notification.email.account.test.smtp.auth", true) - .put("xpack.notification.email.account.test.smtp.user", EmailServer.USERNAME) - .put("xpack.notification.email.account.test.smtp.port", server.port()) - .put("xpack.notification.email.account.test.smtp.host", "localhost") - .setSecureSettings(secureSettings) - .build(); + .put(super.nodeSettings(nodeOrdinal, otherSettings)) + .put("xpack.notification.email.account.test.smtp.auth", true) + .put("xpack.notification.email.account.test.smtp.user", EmailServer.USERNAME) + .put("xpack.notification.email.account.test.smtp.port", server.port()) + .put("xpack.notification.email.account.test.smtp.host", "localhost") + .setSecureSettings(secureSettings) + .build(); } public List getAttachments(MimeMessage message) throws Exception { Object content = message.getContent(); - if (content instanceof String) - return null; + if (content instanceof String) return null; if (content instanceof Multipart) { Multipart multipart = (Multipart) content; @@ -161,9 +162,12 @@ public void testThatEmailAttachmentsAreSent() throws Exception { attachments.add(dataAttachment); HttpRequestTemplate requestTemplate = HttpRequestTemplate.builder("localhost", webServer.getPort()) - .path("/").scheme(Scheme.HTTP).build(); + .path("/") + .scheme(Scheme.HTTP) + .build(); HttpRequestAttachment httpRequestAttachment = HttpRequestAttachment.builder("other-id") - .httpRequestTemplate(requestTemplate).build(); + .httpRequestTemplate(requestTemplate) + .build(); attachments.add(httpRequestAttachment); EmailAttachments emailAttachments = new EmailAttachments(attachments); @@ -173,24 +177,24 @@ public void testThatEmailAttachmentsAreSent() throws Exception { tmpBuilder.endObject(); EmailTemplate.Builder emailBuilder = EmailTemplate.builder().from("from@example.org").to("to@example.org").subject("Subject"); - WatchSourceBuilder watchSourceBuilder = watchBuilder() - .trigger(schedule(interval(5, IntervalSchedule.Interval.Unit.SECONDS))) - .input(noneInput()) - .condition(InternalAlwaysCondition.INSTANCE) - .addAction("_email", emailAction(emailBuilder).setAuthentication(EmailServer.USERNAME, EmailServer.PASSWORD.toCharArray()) - .setAttachments(emailAttachments)); - - new PutWatchRequestBuilder(client()) - .setId("_test_id") - .setSource(watchSourceBuilder) - .get(); + WatchSourceBuilder watchSourceBuilder = watchBuilder().trigger(schedule(interval(5, IntervalSchedule.Interval.Unit.SECONDS))) + .input(noneInput()) + .condition(InternalAlwaysCondition.INSTANCE) + .addAction( + "_email", + emailAction(emailBuilder).setAuthentication(EmailServer.USERNAME, EmailServer.PASSWORD.toCharArray()) + .setAttachments(emailAttachments) + ); + + new PutWatchRequestBuilder(client()).setId("_test_id").setSource(watchSourceBuilder).get(); timeWarp().trigger("_test_id"); refresh(); SearchResponse searchResponse = client().prepareSearch(HistoryStoreField.DATA_STREAM + "*") - .setQuery(QueryBuilders.termQuery("watch_id", "_test_id")) - .execute().actionGet(); + .setQuery(QueryBuilders.termQuery("watch_id", "_test_id")) + .execute() + .actionGet(); assertHitCount(searchResponse, 1); if (latch.await(5, TimeUnit.SECONDS) == false) { diff --git a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/actions/throttler/ActionThrottleTests.java b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/actions/throttler/ActionThrottleTests.java index 9084b7d2d339f..564b438f2e190 100644 --- a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/actions/throttler/ActionThrottleTests.java +++ b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/actions/throttler/ActionThrottleTests.java @@ -55,20 +55,20 @@ public class ActionThrottleTests extends AbstractWatcherIntegrationTestCase { public void testSingleActionAckThrottle() throws Exception { - WatchSourceBuilder watchSourceBuilder = watchBuilder() - .trigger(schedule(interval("60m"))); + WatchSourceBuilder watchSourceBuilder = watchBuilder().trigger(schedule(interval("60m"))); AvailableAction availableAction = randomFrom(AvailableAction.values()); Action.Builder action = availableAction.action(); watchSourceBuilder.addAction("test_id", action); - new PutWatchRequestBuilder(client()) - .setId("_id").setSource(watchSourceBuilder.buildAsBytes(XContentType.JSON), XContentType.JSON).get(); + new PutWatchRequestBuilder(client()).setId("_id") + .setSource(watchSourceBuilder.buildAsBytes(XContentType.JSON), XContentType.JSON) + .get(); refresh(Watch.INDEX); ExecuteWatchRequestBuilder executeWatchRequestBuilder = new ExecuteWatchRequestBuilder(client()).setId("_id") - .setRecordExecution(true) - .setActionMode("test_id", ActionExecutionMode.SIMULATE); + .setRecordExecution(true) + .setActionMode("test_id", ActionExecutionMode.SIMULATE); Map responseMap = executeWatchRequestBuilder.get().getRecordSource().getAsMap(); String status = ObjectPath.eval("result.actions.0.status", responseMap); @@ -82,8 +82,8 @@ public void testSingleActionAckThrottle() throws Exception { } executeWatchRequestBuilder = new ExecuteWatchRequestBuilder(client()).setId("_id") - .setRecordExecution(true) - .setActionMode("test_id", ActionExecutionMode.SIMULATE); + .setRecordExecution(true) + .setActionMode("test_id", ActionExecutionMode.SIMULATE); responseMap = executeWatchRequestBuilder.get().getRecordSource().getAsMap(); status = ObjectPath.eval("result.actions.0.status", responseMap); if (ack) { @@ -94,11 +94,10 @@ public void testSingleActionAckThrottle() throws Exception { } public void testRandomMultiActionAckThrottle() throws Exception { - WatchSourceBuilder watchSourceBuilder = watchBuilder() - .trigger(schedule(interval("60m"))); + WatchSourceBuilder watchSourceBuilder = watchBuilder().trigger(schedule(interval("60m"))); Set ackingActions = new HashSet<>(); - for (int i = 0; i < scaledRandomIntBetween(5,10); ++i) { + for (int i = 0; i < scaledRandomIntBetween(5, 10); ++i) { AvailableAction availableAction = randomFrom(AvailableAction.values()); Action.Builder action = availableAction.action(); watchSourceBuilder.addAction("test_id" + i, action); @@ -107,12 +106,13 @@ public void testRandomMultiActionAckThrottle() throws Exception { } } - new PutWatchRequestBuilder(client()) - .setId("_id").setSource(watchSourceBuilder.buildAsBytes(XContentType.JSON), XContentType.JSON).get(); + new PutWatchRequestBuilder(client()).setId("_id") + .setSource(watchSourceBuilder.buildAsBytes(XContentType.JSON), XContentType.JSON) + .get(); refresh(Watch.INDEX); executeWatch("_id"); - for (String actionId : ackingActions) { + for (String actionId : ackingActions) { new AckWatchRequestBuilder(client(), "_id").setActionIds(actionId).get(); } @@ -131,22 +131,31 @@ public void testRandomMultiActionAckThrottle() throws Exception { private Map executeWatch(String id) { return new ExecuteWatchRequestBuilder(client()).setId(id) - .setRecordExecution(true) - .setActionMode("_all", ActionExecutionMode.SIMULATE).get().getRecordSource().getAsMap(); + .setRecordExecution(true) + .setActionMode("_all", ActionExecutionMode.SIMULATE) + .get() + .getRecordSource() + .getAsMap(); } public void testDifferentThrottlePeriods() throws Exception { timeWarp().clock().setTime(ZonedDateTime.now(ZoneOffset.UTC)); - WatchSourceBuilder watchSourceBuilder = watchBuilder() - .trigger(schedule(interval("60m"))); - - watchSourceBuilder.addAction("ten_sec_throttle", new TimeValue(10, TimeUnit.SECONDS), - randomFrom(AvailableAction.values()).action()); - watchSourceBuilder.addAction("fifteen_sec_throttle", new TimeValue(15, TimeUnit.SECONDS), - randomFrom(AvailableAction.values()).action()); - - new PutWatchRequestBuilder(client()) - .setId("_id").setSource(watchSourceBuilder.buildAsBytes(XContentType.JSON), XContentType.JSON).get(); + WatchSourceBuilder watchSourceBuilder = watchBuilder().trigger(schedule(interval("60m"))); + + watchSourceBuilder.addAction( + "ten_sec_throttle", + new TimeValue(10, TimeUnit.SECONDS), + randomFrom(AvailableAction.values()).action() + ); + watchSourceBuilder.addAction( + "fifteen_sec_throttle", + new TimeValue(15, TimeUnit.SECONDS), + randomFrom(AvailableAction.values()).action() + ); + + new PutWatchRequestBuilder(client()).setId("_id") + .setSource(watchSourceBuilder.buildAsBytes(XContentType.JSON), XContentType.JSON) + .get(); refresh(Watch.INDEX); timeWarp().clock().fastForwardSeconds(1); @@ -177,24 +186,28 @@ public void testDifferentThrottlePeriods() throws Exception { } public void testDefaultThrottlePeriod() throws Exception { - WatchSourceBuilder watchSourceBuilder = watchBuilder() - .trigger(schedule(interval("60m"))); + WatchSourceBuilder watchSourceBuilder = watchBuilder().trigger(schedule(interval("60m"))); AvailableAction availableAction = randomFrom(AvailableAction.values()); watchSourceBuilder.addAction("default_global_throttle", availableAction.action()); - new PutWatchRequestBuilder(client()) - .setId("_id").setSource(watchSourceBuilder.buildAsBytes(XContentType.JSON), XContentType.JSON).get(); + new PutWatchRequestBuilder(client()).setId("_id") + .setSource(watchSourceBuilder.buildAsBytes(XContentType.JSON), XContentType.JSON) + .get(); refresh(Watch.INDEX); timeWarp().clock().setTime(ZonedDateTime.now(ZoneOffset.UTC)); ExecuteWatchResponse executeWatchResponse = new ExecuteWatchRequestBuilder(client()).setId("_id") - .setTriggerEvent(new ManualTriggerEvent("execute_id", - new ScheduleTriggerEvent(ZonedDateTime.now(ZoneOffset.UTC), ZonedDateTime.now(ZoneOffset.UTC)))) - .setActionMode("default_global_throttle", ActionExecutionMode.SIMULATE) - .setRecordExecution(true) - .get(); + .setTriggerEvent( + new ManualTriggerEvent( + "execute_id", + new ScheduleTriggerEvent(ZonedDateTime.now(ZoneOffset.UTC), ZonedDateTime.now(ZoneOffset.UTC)) + ) + ) + .setActionMode("default_global_throttle", ActionExecutionMode.SIMULATE) + .setRecordExecution(true) + .get(); String status = ObjectPath.eval("result.actions.0.status", executeWatchResponse.getRecordSource().getAsMap()); assertThat(status, equalTo("simulated")); @@ -202,11 +215,15 @@ public void testDefaultThrottlePeriod() throws Exception { timeWarp().clock().fastForwardSeconds(1); executeWatchResponse = new ExecuteWatchRequestBuilder(client()).setId("_id") - .setTriggerEvent(new ManualTriggerEvent("execute_id", - new ScheduleTriggerEvent(ZonedDateTime.now(ZoneOffset.UTC), ZonedDateTime.now(ZoneOffset.UTC)))) - .setActionMode("default_global_throttle", ActionExecutionMode.SIMULATE) - .setRecordExecution(true) - .get(); + .setTriggerEvent( + new ManualTriggerEvent( + "execute_id", + new ScheduleTriggerEvent(ZonedDateTime.now(ZoneOffset.UTC), ZonedDateTime.now(ZoneOffset.UTC)) + ) + ) + .setActionMode("default_global_throttle", ActionExecutionMode.SIMULATE) + .setRecordExecution(true) + .get(); status = ObjectPath.eval("result.actions.0.status", executeWatchResponse.getRecordSource().getAsMap()); assertThat(status, equalTo("throttled")); @@ -215,11 +232,15 @@ public void testDefaultThrottlePeriod() throws Exception { assertBusy(() -> { try { ExecuteWatchResponse executeWatchResponse1 = new ExecuteWatchRequestBuilder(client()).setId("_id") - .setTriggerEvent(new ManualTriggerEvent("execute_id", - new ScheduleTriggerEvent(ZonedDateTime.now(ZoneOffset.UTC), ZonedDateTime.now(ZoneOffset.UTC)))) - .setActionMode("default_global_throttle", ActionExecutionMode.SIMULATE) - .setRecordExecution(true) - .get(); + .setTriggerEvent( + new ManualTriggerEvent( + "execute_id", + new ScheduleTriggerEvent(ZonedDateTime.now(ZoneOffset.UTC), ZonedDateTime.now(ZoneOffset.UTC)) + ) + ) + .setActionMode("default_global_throttle", ActionExecutionMode.SIMULATE) + .setRecordExecution(true) + .get(); String currentStatus = ObjectPath.eval("result.actions.0.status", executeWatchResponse1.getRecordSource().getAsMap()); assertThat(currentStatus, equalTo("simulated")); } catch (IOException ioe) { @@ -229,36 +250,44 @@ public void testDefaultThrottlePeriod() throws Exception { } public void testWatchThrottlePeriod() throws Exception { - WatchSourceBuilder watchSourceBuilder = watchBuilder() - .trigger(schedule(interval("60m"))) - .defaultThrottlePeriod(new TimeValue(20, TimeUnit.SECONDS)); + WatchSourceBuilder watchSourceBuilder = watchBuilder().trigger(schedule(interval("60m"))) + .defaultThrottlePeriod(new TimeValue(20, TimeUnit.SECONDS)); AvailableAction availableAction = randomFrom(AvailableAction.values()); watchSourceBuilder.addAction("default_global_throttle", availableAction.action()); - new PutWatchRequestBuilder(client()) - .setId("_id").setSource(watchSourceBuilder.buildAsBytes(XContentType.JSON), XContentType.JSON).get(); + new PutWatchRequestBuilder(client()).setId("_id") + .setSource(watchSourceBuilder.buildAsBytes(XContentType.JSON), XContentType.JSON) + .get(); refresh(Watch.INDEX); timeWarp().clock().setTime(ZonedDateTime.now(ZoneOffset.UTC)); ExecuteWatchResponse executeWatchResponse = new ExecuteWatchRequestBuilder(client()).setId("_id") - .setTriggerEvent(new ManualTriggerEvent("execute_id", - new ScheduleTriggerEvent(ZonedDateTime.now(ZoneOffset.UTC), ZonedDateTime.now(ZoneOffset.UTC)))) - .setActionMode("default_global_throttle", ActionExecutionMode.SIMULATE) - .setRecordExecution(true) - .get(); + .setTriggerEvent( + new ManualTriggerEvent( + "execute_id", + new ScheduleTriggerEvent(ZonedDateTime.now(ZoneOffset.UTC), ZonedDateTime.now(ZoneOffset.UTC)) + ) + ) + .setActionMode("default_global_throttle", ActionExecutionMode.SIMULATE) + .setRecordExecution(true) + .get(); String status = ObjectPath.eval("result.actions.0.status", executeWatchResponse.getRecordSource().getAsMap()); assertThat(status, equalTo("simulated")); timeWarp().clock().fastForwardSeconds(1); executeWatchResponse = new ExecuteWatchRequestBuilder(client()).setId("_id") - .setTriggerEvent(new ManualTriggerEvent("execute_id", - new ScheduleTriggerEvent(ZonedDateTime.now(ZoneOffset.UTC), ZonedDateTime.now(ZoneOffset.UTC)))) - .setActionMode("default_global_throttle", ActionExecutionMode.SIMULATE) - .setRecordExecution(true) - .get(); + .setTriggerEvent( + new ManualTriggerEvent( + "execute_id", + new ScheduleTriggerEvent(ZonedDateTime.now(ZoneOffset.UTC), ZonedDateTime.now(ZoneOffset.UTC)) + ) + ) + .setActionMode("default_global_throttle", ActionExecutionMode.SIMULATE) + .setRecordExecution(true) + .get(); status = ObjectPath.eval("result.actions.0.status", executeWatchResponse.getRecordSource().getAsMap()); assertThat(status, equalTo("throttled")); @@ -266,13 +295,17 @@ public void testWatchThrottlePeriod() throws Exception { assertBusy(() -> { try { - //Since the default throttle period is 5 seconds but we have overridden the period in the watch this should trigger + // Since the default throttle period is 5 seconds but we have overridden the period in the watch this should trigger ExecuteWatchResponse executeWatchResponse1 = new ExecuteWatchRequestBuilder(client()).setId("_id") - .setTriggerEvent(new ManualTriggerEvent("execute_id", - new ScheduleTriggerEvent(ZonedDateTime.now(ZoneOffset.UTC), ZonedDateTime.now(ZoneOffset.UTC)))) - .setActionMode("default_global_throttle", ActionExecutionMode.SIMULATE) - .setRecordExecution(true) - .get(); + .setTriggerEvent( + new ManualTriggerEvent( + "execute_id", + new ScheduleTriggerEvent(ZonedDateTime.now(ZoneOffset.UTC), ZonedDateTime.now(ZoneOffset.UTC)) + ) + ) + .setActionMode("default_global_throttle", ActionExecutionMode.SIMULATE) + .setRecordExecution(true) + .get(); String status1 = ObjectPath.eval("result.actions.0.status", executeWatchResponse1.getRecordSource().getAsMap()); assertThat(status1, equalTo("simulated")); } catch (IOException ioe) { @@ -283,32 +316,37 @@ public void testWatchThrottlePeriod() throws Exception { public void testFailingActionDoesGetThrottled() throws Exception { // create a mapping with a wrong @timestamp field, so that the index action of the watch below will fail - String mapping = Strings.toString(XContentFactory.jsonBuilder() + String mapping = Strings.toString( + XContentFactory.jsonBuilder() .startObject() .startObject("properties") .startObject("@timestamp") .field("type", "integer") .endObject() .endObject() - .endObject()); + .endObject() + ); client().admin().indices().prepareCreate("foo").setMapping(mapping).get(); TimeValue throttlePeriod = new TimeValue(60, TimeUnit.MINUTES); - new PutWatchRequestBuilder(client(), "_id").setSource(watchBuilder() - .trigger(new ScheduleTrigger(new IntervalSchedule( - new IntervalSchedule.Interval(60, IntervalSchedule.Interval.Unit.MINUTES)))) + new PutWatchRequestBuilder(client(), "_id").setSource( + watchBuilder().trigger( + new ScheduleTrigger(new IntervalSchedule(new IntervalSchedule.Interval(60, IntervalSchedule.Interval.Unit.MINUTES))) + ) .defaultThrottlePeriod(throttlePeriod) .addAction("logging", loggingAction("test out")) - .addAction("failing_hook", indexAction("foo").setExecutionTimeField("@timestamp"))) - .get(); + .addAction("failing_hook", indexAction("foo").setExecutionTimeField("@timestamp")) + ).get(); refresh(Watch.INDEX); { Map responseMap = new ExecuteWatchRequestBuilder(client()).setId("_id") - .setRecordExecution(true) - .get().getRecordSource().getAsMap(); + .setRecordExecution(true) + .get() + .getRecordSource() + .getAsMap(); String state = ObjectPath.eval("state", responseMap); @@ -329,8 +367,10 @@ public void testFailingActionDoesGetThrottled() throws Exception { { Map responseMap = new ExecuteWatchRequestBuilder(client()).setId("_id") - .setRecordExecution(true) - .get().getRecordSource().getAsMap(); + .setRecordExecution(true) + .get() + .getRecordSource() + .getAsMap(); String state = ObjectPath.eval("state", responseMap); String firstId = ObjectPath.eval("result.actions.0.id", responseMap); @@ -369,8 +409,8 @@ public String type() { @Override public Action.Builder action() throws Exception { HttpRequestTemplate.Builder requestBuilder = HttpRequestTemplate.builder("localhost", 1234) - .path("/") - .method(HttpMethod.GET); + .path("/") + .method(HttpMethod.GET); return WebhookAction.builder(requestBuilder.build()); } diff --git a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/actions/webhook/WebhookHttpsIntegrationTests.java b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/actions/webhook/WebhookHttpsIntegrationTests.java index 71404e69b933a..f60b23eff9d70 100644 --- a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/actions/webhook/WebhookHttpsIntegrationTests.java +++ b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/actions/webhook/WebhookHttpsIntegrationTests.java @@ -7,11 +7,12 @@ package org.elasticsearch.xpack.watcher.actions.webhook; import com.sun.net.httpserver.HttpsServer; + import org.elasticsearch.action.search.SearchResponse; -import org.elasticsearch.jdk.JavaVersion; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.env.Environment; import org.elasticsearch.index.query.QueryBuilders; +import org.elasticsearch.jdk.JavaVersion; import org.elasticsearch.test.http.MockResponse; import org.elasticsearch.test.http.MockWebServer; import org.elasticsearch.xpack.core.XPackSettings; @@ -81,18 +82,17 @@ public void stopWebservice() throws Exception { public void testHttps() throws Exception { webServer.enqueue(new MockResponse().setResponseCode(200).setBody("body")); HttpRequestTemplate.Builder builder = HttpRequestTemplate.builder("localhost", webServer.getPort()) - .scheme(Scheme.HTTPS) - .path(new TextTemplate("/test/_id")) - .body(new TextTemplate("{key=value}")) - .method(HttpMethod.POST); - - new PutWatchRequestBuilder(client(), "_id") - .setSource(watchBuilder() - .trigger(schedule(interval("5s"))) - .input(simpleInput("key", "value")) - .condition(InternalAlwaysCondition.INSTANCE) - .addAction("_id", ActionBuilders.webhookAction(builder))) - .get(); + .scheme(Scheme.HTTPS) + .path(new TextTemplate("/test/_id")) + .body(new TextTemplate("{key=value}")) + .method(HttpMethod.POST); + + new PutWatchRequestBuilder(client(), "_id").setSource( + watchBuilder().trigger(schedule(interval("5s"))) + .input(simpleInput("key", "value")) + .condition(InternalAlwaysCondition.INSTANCE) + .addAction("_id", ActionBuilders.webhookAction(builder)) + ).get(); timeWarp().trigger("_id"); refresh(); @@ -102,8 +102,9 @@ public void testHttps() throws Exception { assertThat(webServer.requests().get(0).getUri().getPath(), equalTo("/test/_id")); assertThat(webServer.requests().get(0).getBody(), equalTo("{key=value}")); - SearchResponse response = - searchWatchRecords(b -> b.setQuery(QueryBuilders.termQuery(WatchRecord.STATE.getPreferredName(), "executed"))); + SearchResponse response = searchWatchRecords( + b -> b.setQuery(QueryBuilders.termQuery(WatchRecord.STATE.getPreferredName(), "executed")) + ); assertNoFailures(response); XContentSource source = xContentSource(response.getHits().getAt(0).getSourceRef()); @@ -119,19 +120,18 @@ public void testHttps() throws Exception { public void testHttpsAndBasicAuth() throws Exception { webServer.enqueue(new MockResponse().setResponseCode(200).setBody("body")); HttpRequestTemplate.Builder builder = HttpRequestTemplate.builder("localhost", webServer.getPort()) - .scheme(Scheme.HTTPS) - .auth(new BasicAuth("_username", "_password".toCharArray())) - .path(new TextTemplate("/test/_id")) - .body(new TextTemplate("{key=value}")) - .method(HttpMethod.POST); - - new PutWatchRequestBuilder(client(), "_id") - .setSource(watchBuilder() - .trigger(schedule(interval("5s"))) - .input(simpleInput("key", "value")) - .condition(InternalAlwaysCondition.INSTANCE) - .addAction("_id", ActionBuilders.webhookAction(builder))) - .get(); + .scheme(Scheme.HTTPS) + .auth(new BasicAuth("_username", "_password".toCharArray())) + .path(new TextTemplate("/test/_id")) + .body(new TextTemplate("{key=value}")) + .method(HttpMethod.POST); + + new PutWatchRequestBuilder(client(), "_id").setSource( + watchBuilder().trigger(schedule(interval("5s"))) + .input(simpleInput("key", "value")) + .condition(InternalAlwaysCondition.INSTANCE) + .addAction("_id", ActionBuilders.webhookAction(builder)) + ).get(); timeWarp().trigger("_id"); refresh(); @@ -151,9 +151,9 @@ private static List getProtocols() { if (JavaVersion.current().compareTo(JavaVersion.parse("12")) < 0) { return List.of("TLSv1.2"); } else { - JavaVersion full = - AccessController.doPrivileged( - (PrivilegedAction) () -> JavaVersion.parse(System.getProperty("java.version"))); + JavaVersion full = AccessController.doPrivileged( + (PrivilegedAction) () -> JavaVersion.parse(System.getProperty("java.version")) + ); if (full.compareTo(JavaVersion.parse("12.0.1")) < 0) { return List.of("TLSv1.2"); } diff --git a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/actions/webhook/WebhookIntegrationTests.java b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/actions/webhook/WebhookIntegrationTests.java index 20120d41cd886..a51d602feee50 100644 --- a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/actions/webhook/WebhookIntegrationTests.java +++ b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/actions/webhook/WebhookIntegrationTests.java @@ -73,28 +73,29 @@ public void stopWebservice() throws Exception { public void testWebhook() throws Exception { webServer.enqueue(new MockResponse().setResponseCode(200).setBody("body")); HttpRequestTemplate.Builder builder = HttpRequestTemplate.builder("localhost", webServer.getPort()) - .path(new TextTemplate("/test/_id")) - .putParam("param1", new TextTemplate("value1")) - .putParam("watch_id", new TextTemplate("_id")) - .body(new TextTemplate("_body")) - .auth(new BasicAuth("user", "pass".toCharArray())) - .method(HttpMethod.POST); - - new PutWatchRequestBuilder(client(), "_id") - .setSource(watchBuilder() - .trigger(schedule(interval("5s"))) - .input(simpleInput("key", "value")) - .condition(InternalAlwaysCondition.INSTANCE) - .addAction("_id", ActionBuilders.webhookAction(builder))) - .get(); + .path(new TextTemplate("/test/_id")) + .putParam("param1", new TextTemplate("value1")) + .putParam("watch_id", new TextTemplate("_id")) + .body(new TextTemplate("_body")) + .auth(new BasicAuth("user", "pass".toCharArray())) + .method(HttpMethod.POST); + + new PutWatchRequestBuilder(client(), "_id").setSource( + watchBuilder().trigger(schedule(interval("5s"))) + .input(simpleInput("key", "value")) + .condition(InternalAlwaysCondition.INSTANCE) + .addAction("_id", ActionBuilders.webhookAction(builder)) + ).get(); timeWarp().trigger("_id"); refresh(); assertWatchWithMinimumPerformedActionsCount("_id", 1, false); assertThat(webServer.requests(), hasSize(1)); - assertThat(webServer.requests().get(0).getUri().getQuery(), - anyOf(equalTo("watch_id=_id¶m1=value1"), equalTo("param1=value1&watch_id=_id"))); + assertThat( + webServer.requests().get(0).getUri().getQuery(), + anyOf(equalTo("watch_id=_id¶m1=value1"), equalTo("param1=value1&watch_id=_id")) + ); assertThat(webServer.requests().get(0).getBody(), is("_body")); @@ -113,20 +114,19 @@ public void testWebhook() throws Exception { public void testWebhookWithBasicAuth() throws Exception { webServer.enqueue(new MockResponse().setResponseCode(200).setBody("body")); HttpRequestTemplate.Builder builder = HttpRequestTemplate.builder("localhost", webServer.getPort()) - .auth(new BasicAuth("_username", "_password".toCharArray())) - .path(new TextTemplate("/test/_id")) - .putParam("param1", new TextTemplate("value1")) - .putParam("watch_id", new TextTemplate("_id")) - .body(new TextTemplate("_body")) - .method(HttpMethod.POST); - - new PutWatchRequestBuilder(client(), "_id") - .setSource(watchBuilder() - .trigger(schedule(interval("5s"))) - .input(simpleInput("key", "value")) - .condition(InternalAlwaysCondition.INSTANCE) - .addAction("_id", ActionBuilders.webhookAction(builder))) - .get(); + .auth(new BasicAuth("_username", "_password".toCharArray())) + .path(new TextTemplate("/test/_id")) + .putParam("param1", new TextTemplate("value1")) + .putParam("watch_id", new TextTemplate("_id")) + .body(new TextTemplate("_body")) + .method(HttpMethod.POST); + + new PutWatchRequestBuilder(client(), "_id").setSource( + watchBuilder().trigger(schedule(interval("5s"))) + .input(simpleInput("key", "value")) + .condition(InternalAlwaysCondition.INSTANCE) + .addAction("_id", ActionBuilders.webhookAction(builder)) + ).get(); timeWarp().trigger("_id"); refresh(); @@ -134,8 +134,10 @@ public void testWebhookWithBasicAuth() throws Exception { assertWatchWithMinimumPerformedActionsCount("_id", 1, false); assertThat(webServer.requests(), hasSize(1)); - assertThat(webServer.requests().get(0).getUri().getQuery(), - anyOf(equalTo("watch_id=_id¶m1=value1"), equalTo("param1=value1&watch_id=_id"))); + assertThat( + webServer.requests().get(0).getUri().getQuery(), + anyOf(equalTo("watch_id=_id¶m1=value1"), equalTo("param1=value1&watch_id=_id")) + ); assertThat(webServer.requests().get(0).getBody(), is("_body")); assertThat(webServer.requests().get(0).getHeader("Authorization"), is(("Basic X3VzZXJuYW1lOl9wYXNzd29yZA=="))); } @@ -148,18 +150,17 @@ public void testWebhookWithTimebasedIndex() throws Exception { String host = publishAddress.address().getHostString(); HttpRequestTemplate.Builder builder = HttpRequestTemplate.builder(host, publishAddress.getPort()) - .path(new TextTemplate("/%3Clogstash-%7Bnow%2Fd%7D%3E/_doc/1")) - .body(new TextTemplate("{\"foo\":\"bar\"}")) - .putHeader("Content-Type", new TextTemplate("application/json")) - .method(HttpMethod.PUT); - - new PutWatchRequestBuilder(client(), "_id") - .setSource(watchBuilder() - .trigger(schedule(interval("5s"))) - .input(simpleInput("key", "value")) - .condition(InternalAlwaysCondition.INSTANCE) - .addAction("_id", ActionBuilders.webhookAction(builder))) - .get(); + .path(new TextTemplate("/%3Clogstash-%7Bnow%2Fd%7D%3E/_doc/1")) + .body(new TextTemplate("{\"foo\":\"bar\"}")) + .putHeader("Content-Type", new TextTemplate("application/json")) + .method(HttpMethod.PUT); + + new PutWatchRequestBuilder(client(), "_id").setSource( + watchBuilder().trigger(schedule(interval("5s"))) + .input(simpleInput("key", "value")) + .condition(InternalAlwaysCondition.INSTANCE) + .addAction("_id", ActionBuilders.webhookAction(builder)) + ).get(); new ExecuteWatchRequestBuilder(client(), "_id").get(); diff --git a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/condition/ArrayCompareConditionSearchTests.java b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/condition/ArrayCompareConditionSearchTests.java index 1aeada2bd155a..2ec8a00fced0a 100644 --- a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/condition/ArrayCompareConditionSearchTests.java +++ b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/condition/ArrayCompareConditionSearchTests.java @@ -7,9 +7,9 @@ package org.elasticsearch.xpack.watcher.condition; import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.xpack.core.watcher.condition.Condition; import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext; import org.elasticsearch.xpack.core.watcher.watch.Payload; @@ -31,8 +31,7 @@ public class ArrayCompareConditionSearchTests extends AbstractWatcherIntegration public void testExecuteWithAggs() throws Exception { String index = "test-index"; - client().admin().indices().prepareCreate(index) - .get(); + client().admin().indices().prepareCreate(index).get(); ArrayCompareCondition.Op op = randomFrom(ArrayCompareCondition.Op.values()); ArrayCompareCondition.Quantifier quantifier = randomFrom(ArrayCompareCondition.Quantifier.values()); @@ -46,11 +45,17 @@ public void testExecuteWithAggs() throws Exception { refresh(); SearchResponse response = client().prepareSearch(index) - .addAggregation(AggregationBuilders.terms("top_tweeters").field("user.screen_name.keyword").size(3)).get(); - - - ArrayCompareCondition condition = new ArrayCompareCondition("ctx.payload.aggregations.top_tweeters.buckets" , "doc_count", op, - numberOfDocumentsWatchingFor, quantifier, Clock.systemUTC()); + .addAggregation(AggregationBuilders.terms("top_tweeters").field("user.screen_name.keyword").size(3)) + .get(); + + ArrayCompareCondition condition = new ArrayCompareCondition( + "ctx.payload.aggregations.top_tweeters.buckets", + "doc_count", + op, + numberOfDocumentsWatchingFor, + quantifier, + Clock.systemUTC() + ); WatchExecutionContext ctx = mockExecutionContext("_name", new Payload.XContent(response, ToXContent.EMPTY_PARAMS)); Condition.Result result = condition.execute(ctx); @@ -67,14 +72,17 @@ public void testExecuteWithAggs() throws Exception { Map fightsForTheUsers = new HashMap<>(); fightsForTheUsers.put("doc_count", numberOfDocuments); fightsForTheUsers.put("key", "fights_for_the_users"); - assertThat(resolvedValues, hasEntry("ctx.payload.aggregations.top_tweeters.buckets", - (Object) Arrays.asList(elastic, fightsForTheUsers))); + assertThat( + resolvedValues, + hasEntry("ctx.payload.aggregations.top_tweeters.buckets", (Object) Arrays.asList(elastic, fightsForTheUsers)) + ); client().prepareIndex(index).setSource(source("fights_for_the_users", "you know, for the users", numberOfDocuments)).get(); refresh(); response = client().prepareSearch(index) - .addAggregation(AggregationBuilders.terms("top_tweeters").field("user.screen_name.keyword").size(3)).get(); + .addAggregation(AggregationBuilders.terms("top_tweeters").field("user.screen_name.keyword").size(3)) + .get(); ctx = mockExecutionContext("_name", new Payload.XContent(response, ToXContent.EMPTY_PARAMS)); result = condition.execute(ctx); @@ -86,16 +94,18 @@ public void testExecuteWithAggs() throws Exception { assertThat(resolvedValues, notNullValue()); assertThat(resolvedValues.size(), is(1)); fightsForTheUsers.put("doc_count", numberOfDocumentsWatchingFor); - assertThat(resolvedValues, hasEntry("ctx.payload.aggregations.top_tweeters.buckets", - (Object) Arrays.asList(fightsForTheUsers, elastic))); + assertThat( + resolvedValues, + hasEntry("ctx.payload.aggregations.top_tweeters.buckets", (Object) Arrays.asList(fightsForTheUsers, elastic)) + ); } private XContentBuilder source(String screenName, String tweet, int i) throws IOException { return jsonBuilder().startObject() - .startObject("user") - .field("screen_name", screenName) - .endObject() - .field("tweet", tweet + " " + i) - .endObject(); + .startObject("user") + .field("screen_name", screenName) + .endObject() + .field("tweet", tweet + " " + i) + .endObject(); } } diff --git a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/condition/CompareConditionSearchTests.java b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/condition/CompareConditionSearchTests.java index e7f32e10b9831..9ab58fbc17b16 100644 --- a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/condition/CompareConditionSearchTests.java +++ b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/condition/CompareConditionSearchTests.java @@ -9,7 +9,6 @@ import org.apache.lucene.search.TotalHits; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.ShardSearchFailure; -import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; @@ -18,6 +17,7 @@ import org.elasticsearch.search.aggregations.BucketOrder; import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; import org.elasticsearch.search.internal.InternalSearchResponse; +import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext; import org.elasticsearch.xpack.core.watcher.watch.Payload; import org.elasticsearch.xpack.watcher.test.AbstractWatcherIntegrationTestCase; @@ -41,12 +41,20 @@ public void testExecuteWithAggs() throws Exception { refresh(); SearchResponse response = client().prepareSearch("my-index") - .addAggregation(AggregationBuilders.dateHistogram("rate").field("@timestamp") - .fixedInterval(DateHistogramInterval.HOUR).order(BucketOrder.count(false))) - .get(); + .addAggregation( + AggregationBuilders.dateHistogram("rate") + .field("@timestamp") + .fixedInterval(DateHistogramInterval.HOUR) + .order(BucketOrder.count(false)) + ) + .get(); - CompareCondition condition = new CompareCondition("ctx.payload.aggregations.rate.buckets.0.doc_count", CompareCondition.Op.GTE, 5, - Clock.systemUTC()); + CompareCondition condition = new CompareCondition( + "ctx.payload.aggregations.rate.buckets.0.doc_count", + CompareCondition.Op.GTE, + 5, + Clock.systemUTC() + ); WatchExecutionContext ctx = mockExecutionContext("_name", new Payload.XContent(response, ToXContent.EMPTY_PARAMS)); CompareCondition.Result result = condition.execute(ctx); assertThat(result.met(), is(false)); @@ -59,9 +67,13 @@ public void testExecuteWithAggs() throws Exception { refresh(); response = client().prepareSearch("my-index") - .addAggregation(AggregationBuilders.dateHistogram("rate") - .field("@timestamp").fixedInterval(DateHistogramInterval.HOUR).order(BucketOrder.count(false))) - .get(); + .addAggregation( + AggregationBuilders.dateHistogram("rate") + .field("@timestamp") + .fixedInterval(DateHistogramInterval.HOUR) + .order(BucketOrder.count(false)) + ) + .get(); ctx = mockExecutionContext("_name", new Payload.XContent(response, ToXContent.EMPTY_PARAMS)); result = condition.execute(ctx); @@ -73,17 +85,30 @@ public void testExecuteWithAggs() throws Exception { } public void testExecuteAccessHits() throws Exception { - CompareCondition condition = new CompareCondition("ctx.payload.hits.hits.0._score", CompareCondition.Op.EQ, 1, - Clock.systemUTC()); + CompareCondition condition = new CompareCondition("ctx.payload.hits.hits.0._score", CompareCondition.Op.EQ, 1, Clock.systemUTC()); SearchHit hit = new SearchHit(0, "1", null, null); hit.score(1f); hit.shard(new SearchShardTarget("a", new ShardId("a", "indexUUID", 0), null)); InternalSearchResponse internalSearchResponse = new InternalSearchResponse( - new SearchHits(new SearchHit[]{hit}, new TotalHits(1L, TotalHits.Relation.EQUAL_TO), 1f), - null, null, null, false, false, 1); - SearchResponse response = new SearchResponse(internalSearchResponse, "", 3, 3, 0, - 500L, ShardSearchFailure.EMPTY_ARRAY, SearchResponse.Clusters.EMPTY); + new SearchHits(new SearchHit[] { hit }, new TotalHits(1L, TotalHits.Relation.EQUAL_TO), 1f), + null, + null, + null, + false, + false, + 1 + ); + SearchResponse response = new SearchResponse( + internalSearchResponse, + "", + 3, + 3, + 0, + 500L, + ShardSearchFailure.EMPTY_ARRAY, + SearchResponse.Clusters.EMPTY + ); WatchExecutionContext ctx = mockExecutionContext("_watch_name", new Payload.XContent(response, ToXContent.EMPTY_PARAMS)); assertThat(condition.execute(ctx).met(), is(true)); diff --git a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/execution/ExecuteWatchQueuedStatsTests.java b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/execution/ExecuteWatchQueuedStatsTests.java index d753d89014fff..900d412901237 100644 --- a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/execution/ExecuteWatchQueuedStatsTests.java +++ b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/execution/ExecuteWatchQueuedStatsTests.java @@ -63,17 +63,13 @@ protected boolean timeWarped() { */ public void testQueuedStats() throws ExecutionException, InterruptedException { final Client client = client(); - new PutWatchRequestBuilder(client, "id") - .setActive(true) - .setSource( - new WatchSourceBuilder() - .input(simpleInput("payload", "yes")) - .trigger(schedule(interval("1s"))) - .addAction( - "action", - TimeValue.timeValueSeconds(1), - IndexAction.builder("test_index").setDocId("id"))) - .get(); + new PutWatchRequestBuilder(client, "id").setActive(true) + .setSource( + new WatchSourceBuilder().input(simpleInput("payload", "yes")) + .trigger(schedule(interval("1s"))) + .addAction("action", TimeValue.timeValueSeconds(1), IndexAction.builder("test_index").setDocId("id")) + ) + .get(); final int numberOfIterations = 128 - scaledRandomIntBetween(0, 128); @@ -89,9 +85,12 @@ public void testQueuedStats() throws ExecutionException, InterruptedException { for (int i = 0; i < numberOfIterations; i++) { final ExecuteWatchRequest request = new ExecuteWatchRequest("id"); try { - request.setTriggerEvent(new ManualTriggerEvent( + request.setTriggerEvent( + new ManualTriggerEvent( "id-" + i, - new ScheduleTriggerEvent(ZonedDateTime.now(ZoneOffset.UTC), ZonedDateTime.now(ZoneOffset.UTC)))); + new ScheduleTriggerEvent(ZonedDateTime.now(ZoneOffset.UTC), ZonedDateTime.now(ZoneOffset.UTC)) + ) + ); } catch (final IOException e) { fail(e.toString()); } diff --git a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/history/HistoryActionConditionTests.java b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/history/HistoryActionConditionTests.java index c612bb8ecc5a6..fcb3ce3ede158 100644 --- a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/history/HistoryActionConditionTests.java +++ b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/history/HistoryActionConditionTests.java @@ -54,8 +54,11 @@ public class HistoryActionConditionTests extends AbstractWatcherIntegrationTestC private final ExecutableCondition scriptConditionPasses = mockScriptCondition("return true;"); private final ExecutableCondition compareConditionPasses = new CompareCondition("ctx.payload.key", CompareCondition.Op.GTE, 15); - private final ExecutableCondition conditionPasses = randomFrom(InternalAlwaysCondition.INSTANCE, - scriptConditionPasses, compareConditionPasses); + private final ExecutableCondition conditionPasses = randomFrom( + InternalAlwaysCondition.INSTANCE, + scriptConditionPasses, + compareConditionPasses + ); private final ExecutableCondition scriptConditionFails = mockScriptCondition("return false;"); private final ExecutableCondition compareConditionFails = new CompareCondition("ctx.payload.key", CompareCondition.Op.LT, 15); @@ -76,9 +79,10 @@ protected Map, Object>> pluginScripts() { scripts.put("return true;", vars -> true); scripts.put("return false;", vars -> false); - scripts.put("throw new IllegalStateException('failed');", vars -> { - throw new IllegalStateException("[expected] failed hard"); - }); + scripts.put( + "throw new IllegalStateException('failed');", + vars -> { throw new IllegalStateException("[expected] failed hard"); } + ); return scripts; } @@ -93,8 +97,11 @@ public void testActionConditionWithHardFailures() throws Exception { final String id = "testActionConditionWithHardFailures"; final ExecutableCondition scriptConditionFailsHard = mockScriptCondition("throw new IllegalStateException('failed');"); - final List actionConditionsWithFailure = - Arrays.asList(scriptConditionFailsHard, conditionPasses, InternalAlwaysCondition.INSTANCE); + final List actionConditionsWithFailure = Arrays.asList( + scriptConditionFailsHard, + conditionPasses, + InternalAlwaysCondition.INSTANCE + ); Collections.shuffle(actionConditionsWithFailure, random()); @@ -114,9 +121,9 @@ public void testActionConditionWithHardFailures() throws Exception { final List actions = getActionsFromHit(hit.getSourceAsMap()); for (int i = 0; i < actionConditionsWithFailure.size(); ++i) { - final Map action = (Map)actions.get(i); - final Map condition = (Map)action.get("condition"); - final Map logging = (Map)action.get("logging"); + final Map action = (Map) actions.get(i); + final Map condition = (Map) action.get("condition"); + final Map logging = (Map) action.get("logging"); assertThat(action.get("id"), is("action" + i)); @@ -140,10 +147,9 @@ public void testActionConditionWithHardFailures() throws Exception { public void testActionConditionWithFailures() throws Exception { final String id = "testActionConditionWithFailures"; final ExecutableCondition[] actionConditionsWithFailure = new ExecutableCondition[] { - conditionFails, - conditionPasses, - InternalAlwaysCondition.INSTANCE - }; + conditionFails, + conditionPasses, + InternalAlwaysCondition.INSTANCE }; Collections.shuffle(Arrays.asList(actionConditionsWithFailure), random()); final int failedIndex = Arrays.asList(actionConditionsWithFailure).indexOf(conditionFails); @@ -159,9 +165,9 @@ public void testActionConditionWithFailures() throws Exception { final List actions = getActionsFromHit(hit.getSourceAsMap()); for (int i = 0; i < actionConditionsWithFailure.length; ++i) { - final Map action = (Map)actions.get(i); - final Map condition = (Map)action.get("condition"); - final Map logging = (Map)action.get("logging"); + final Map action = (Map) actions.get(i); + final Map condition = (Map) action.get("condition"); + final Map logging = (Map) action.get("logging"); assertThat(action.get("id"), is("action" + i)); assertThat(condition.get("type"), is(actionConditionsWithFailure[i].type())); @@ -181,11 +187,11 @@ public void testActionConditionWithFailures() throws Exception { } @SuppressWarnings("unchecked") - @AwaitsFix( bugUrl = "https://github.com/elastic/elasticsearch/issues/65064") + @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/65064") public void testActionCondition() throws Exception { final String id = "testActionCondition"; final List actionConditions = new ArrayList<>(); - //actionConditions.add(conditionPasses); + // actionConditions.add(conditionPasses); actionConditions.add(InternalAlwaysCondition.INSTANCE); /* @@ -210,9 +216,9 @@ public void testActionCondition() throws Exception { final List actions = getActionsFromHit(hit.getSourceAsMap()); for (int i = 0; i < actionConditions.size(); ++i) { - final Map action = (Map)actions.get(i); - final Map condition = (Map)action.get("condition"); - final Map logging = (Map)action.get("logging"); + final Map action = (Map) actions.get(i); + final Map condition = (Map) action.get("condition"); + final Map logging = (Map) action.get("logging"); assertThat(action.get("id"), is("action" + i)); assertThat(action.get("status"), is("success")); @@ -231,9 +237,9 @@ public void testActionCondition() throws Exception { */ @SuppressWarnings("unchecked") private List getActionsFromHit(final Map source) { - final Map result = (Map)source.get("result"); + final Map result = (Map) source.get("result"); - return (List)result.get("actions"); + return (List) result.get("actions"); } /** @@ -246,10 +252,9 @@ private List getActionsFromHit(final Map source) { * @param actionConditions The conditions to add to the Watch */ private void putAndTriggerWatch(final String id, final Input input, final Condition... actionConditions) throws Exception { - WatchSourceBuilder source = watchBuilder() - .trigger(schedule(interval("5s"))) - .input(input) - .condition(InternalAlwaysCondition.INSTANCE); + WatchSourceBuilder source = watchBuilder().trigger(schedule(interval("5s"))) + .input(input) + .condition(InternalAlwaysCondition.INSTANCE); for (int i = 0; i < actionConditions.length; ++i) { source.addAction("action" + i, actionConditions[i], loggingAction(Integer.toString(i))); diff --git a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/history/HistoryTemplateEmailMappingsTests.java b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/history/HistoryTemplateEmailMappingsTests.java index 71796a98f5fa2..6324b9fd9dc58 100644 --- a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/history/HistoryTemplateEmailMappingsTests.java +++ b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/history/HistoryTemplateEmailMappingsTests.java @@ -56,31 +56,36 @@ protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { final MockSecureSettings secureSettings = new MockSecureSettings(); secureSettings.setString("xpack.notification.email.account.test.smtp.secure_password", EmailServer.PASSWORD); return Settings.builder() - .put(super.nodeSettings(nodeOrdinal, otherSettings)) - - // email - .put("xpack.notification.email.account.test.smtp.auth", true) - .put("xpack.notification.email.account.test.smtp.user", EmailServer.USERNAME) - .put("xpack.notification.email.account.test.smtp.port", server.port()) - .put("xpack.notification.email.account.test.smtp.host", "localhost") - .setSecureSettings(secureSettings) - .build(); + .put(super.nodeSettings(nodeOrdinal, otherSettings)) + + // email + .put("xpack.notification.email.account.test.smtp.auth", true) + .put("xpack.notification.email.account.test.smtp.user", EmailServer.USERNAME) + .put("xpack.notification.email.account.test.smtp.port", server.port()) + .put("xpack.notification.email.account.test.smtp.host", "localhost") + .setSecureSettings(secureSettings) + .build(); } public void testEmailFields() throws Exception { - PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client(), "_id").setSource(watchBuilder() - .trigger(schedule(interval("5s"))) + PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client(), "_id").setSource( + watchBuilder().trigger(schedule(interval("5s"))) .input(simpleInput()) .condition(InternalAlwaysCondition.INSTANCE) - .addAction("_email", emailAction(EmailTemplate.builder() - .from("from@example.com") - .to("to1@example.com", "to2@example.com") - .cc("cc1@example.com", "cc2@example.com") - .bcc("bcc1@example.com", "bcc2@example.com") - .replyTo("rt1@example.com", "rt2@example.com") - .subject("_subject") - .textBody("_body")))) - .get(); + .addAction( + "_email", + emailAction( + EmailTemplate.builder() + .from("from@example.com") + .to("to1@example.com", "to2@example.com") + .cc("cc1@example.com", "cc2@example.com") + .bcc("bcc1@example.com", "bcc2@example.com") + .replyTo("rt1@example.com", "rt2@example.com") + .subject("_subject") + .textBody("_body") + ) + ) + ).get(); assertThat(putWatchResponse.isCreated(), is(true)); timeWarp().trigger("_id"); @@ -90,13 +95,15 @@ public void testEmailFields() throws Exception { // the action should fail as no email server is available assertWatchWithMinimumActionsCount("_id", ExecutionState.EXECUTED, 1); - SearchResponse response = client().prepareSearch(HistoryStoreField.DATA_STREAM + "*").setSource(searchSource() - .aggregation(terms("from").field("result.actions.email.message.from")) - .aggregation(terms("to").field("result.actions.email.message.to")) - .aggregation(terms("cc").field("result.actions.email.message.cc")) - .aggregation(terms("bcc").field("result.actions.email.message.bcc")) - .aggregation(terms("reply_to").field("result.actions.email.message.reply_to"))) - .get(); + SearchResponse response = client().prepareSearch(HistoryStoreField.DATA_STREAM + "*") + .setSource( + searchSource().aggregation(terms("from").field("result.actions.email.message.from")) + .aggregation(terms("to").field("result.actions.email.message.to")) + .aggregation(terms("cc").field("result.actions.email.message.cc")) + .aggregation(terms("bcc").field("result.actions.email.message.bcc")) + .aggregation(terms("reply_to").field("result.actions.email.message.reply_to")) + ) + .get(); assertThat(response, notNullValue()); assertThat(response.getHits().getTotalHits().value, greaterThanOrEqualTo(1L)); diff --git a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/history/HistoryTemplateHttpMappingsTests.java b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/history/HistoryTemplateHttpMappingsTests.java index 245cf27574e45..809e607b423d4 100644 --- a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/history/HistoryTemplateHttpMappingsTests.java +++ b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/history/HistoryTemplateHttpMappingsTests.java @@ -10,13 +10,13 @@ import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.cluster.metadata.MappingMetadata; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.ObjectPath; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.protocol.xpack.watcher.PutWatchResponse; import org.elasticsearch.search.aggregations.Aggregations; import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.test.http.MockResponse; import org.elasticsearch.test.http.MockWebServer; +import org.elasticsearch.xcontent.ObjectPath; import org.elasticsearch.xpack.core.watcher.execution.ExecutionState; import org.elasticsearch.xpack.core.watcher.history.HistoryStoreField; import org.elasticsearch.xpack.core.watcher.transport.actions.execute.ExecuteWatchRequestBuilder; @@ -67,15 +67,20 @@ public void cleanup() throws Exception { } public void testHttpFields() throws Exception { - PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client(), "_id").setSource(watchBuilder() - .trigger(schedule(interval("5s"))) + PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client(), "_id").setSource( + watchBuilder().trigger(schedule(interval("5s"))) .input(httpInput(HttpRequestTemplate.builder("localhost", webServer.getPort()).path("/input/path"))) .condition(InternalAlwaysCondition.INSTANCE) - .addAction("_webhook", webhookAction(HttpRequestTemplate.builder("localhost", webServer.getPort()) - .path("/webhook/path") - .method(HttpMethod.POST) - .body("_body")))) - .get(); + .addAction( + "_webhook", + webhookAction( + HttpRequestTemplate.builder("localhost", webServer.getPort()) + .path("/webhook/path") + .method(HttpMethod.POST) + .body("_body") + ) + ) + ).get(); // one for the input, one for the webhook webServer.enqueue(new MockResponse().setResponseCode(200).setBody("{}")); @@ -89,11 +94,13 @@ public void testHttpFields() throws Exception { // the action should fail as no email server is available assertWatchWithMinimumActionsCount("_id", ExecutionState.EXECUTED, 1); - SearchResponse response = client().prepareSearch(HistoryStoreField.DATA_STREAM + "*").setSource(searchSource() - .aggregation(terms("input_result_path").field("result.input.http.request.path")) - .aggregation(terms("input_result_host").field("result.input.http.request.host")) - .aggregation(terms("webhook_path").field("result.actions.webhook.request.path"))) - .get(); + SearchResponse response = client().prepareSearch(HistoryStoreField.DATA_STREAM + "*") + .setSource( + searchSource().aggregation(terms("input_result_path").field("result.input.http.request.path")) + .aggregation(terms("input_result_host").field("result.input.http.request.host")) + .aggregation(terms("webhook_path").field("result.actions.webhook.request.path")) + ) + .get(); assertThat(response, notNullValue()); assertThat(response.getHits().getTotalHits().value, is(oneOf(1L, 2L))); @@ -131,18 +138,27 @@ public void testExceptionMapping() { webServer.enqueue(new MockResponse().setBeforeReplyDelay(TimeValue.timeValueSeconds(5))); } - PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client(), id).setSource(watchBuilder() - .trigger(schedule(interval("1h"))) - .input(httpInput(HttpRequestTemplate.builder("localhost", webServer.getPort()) - .path("/") - .readTimeout(abortAtInput ? TimeValue.timeValueMillis(10) : TimeValue.timeValueSeconds(10)))) + PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client(), id).setSource( + watchBuilder().trigger(schedule(interval("1h"))) + .input( + httpInput( + HttpRequestTemplate.builder("localhost", webServer.getPort()) + .path("/") + .readTimeout(abortAtInput ? TimeValue.timeValueMillis(10) : TimeValue.timeValueSeconds(10)) + ) + ) .condition(InternalAlwaysCondition.INSTANCE) - .addAction("_webhook", webhookAction(HttpRequestTemplate.builder("localhost", webServer.getPort()) - .readTimeout(TimeValue.timeValueMillis(10)) - .path("/webhook/path") - .method(HttpMethod.POST) - .body("_body")))) - .get(); + .addAction( + "_webhook", + webhookAction( + HttpRequestTemplate.builder("localhost", webServer.getPort()) + .readTimeout(TimeValue.timeValueMillis(10)) + .path("/webhook/path") + .method(HttpMethod.POST) + .body("_body") + ) + ) + ).get(); assertThat(putWatchResponse.isCreated(), is(true)); new ExecuteWatchRequestBuilder(client(), id).setRecordExecution(true).get(); @@ -150,8 +166,8 @@ public void testExceptionMapping() { // ensure watcher history index has been written with this id flushAndRefresh(HistoryStoreField.INDEX_PREFIX + "*"); SearchResponse searchResponse = client().prepareSearch(HistoryStoreField.INDEX_PREFIX + "*") - .setQuery(QueryBuilders.termQuery("watch_id", id)) - .get(); + .setQuery(QueryBuilders.termQuery("watch_id", id)) + .get(); assertHitCount(searchResponse, 1L); // ensure that enabled is set to false diff --git a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/history/HistoryTemplateIndexActionMappingsTests.java b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/history/HistoryTemplateIndexActionMappingsTests.java index e18fe6064e6e8..7b122d2507853 100644 --- a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/history/HistoryTemplateIndexActionMappingsTests.java +++ b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/history/HistoryTemplateIndexActionMappingsTests.java @@ -34,10 +34,9 @@ public class HistoryTemplateIndexActionMappingsTests extends AbstractWatcherInte public void testIndexActionFields() throws Exception { String index = "the-index"; - PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client(), "_id").setSource(watchBuilder() - .trigger(schedule(interval("5m"))) - .addAction("index", indexAction(index))) - .get(); + PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client(), "_id").setSource( + watchBuilder().trigger(schedule(interval("5m"))).addAction("index", indexAction(index)) + ).get(); assertThat(putWatchResponse.isCreated(), is(true)); timeWarp().trigger("_id"); @@ -49,9 +48,9 @@ public void testIndexActionFields() throws Exception { flush(); refresh(); - SearchResponse response = client().prepareSearch(HistoryStoreField.DATA_STREAM + "*").setSource(searchSource() - .aggregation(terms("index_action_indices").field("result.actions.index.response.index"))) - .get(); + SearchResponse response = client().prepareSearch(HistoryStoreField.DATA_STREAM + "*") + .setSource(searchSource().aggregation(terms("index_action_indices").field("result.actions.index.response.index"))) + .get(); assertThat(response, notNullValue()); assertThat(response.getHits().getTotalHits().value, is(oneOf(1L, 2L))); diff --git a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/history/HistoryTemplateSearchInputMappingsTests.java b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/history/HistoryTemplateSearchInputMappingsTests.java index 3ee5289f7a62d..7cd5bce4372ec 100644 --- a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/history/HistoryTemplateSearchInputMappingsTests.java +++ b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/history/HistoryTemplateSearchInputMappingsTests.java @@ -44,15 +44,17 @@ public void testHttpFields() throws Exception { refresh(); WatcherSearchTemplateRequest request = new WatcherSearchTemplateRequest( - new String[]{index}, SearchType.QUERY_THEN_FETCH, - WatcherSearchTemplateRequest.DEFAULT_INDICES_OPTIONS, new BytesArray("{}") + new String[] { index }, + SearchType.QUERY_THEN_FETCH, + WatcherSearchTemplateRequest.DEFAULT_INDICES_OPTIONS, + new BytesArray("{}") ); - PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client(), "_id").setSource(watchBuilder() - .trigger(schedule(interval("5s"))) + PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client(), "_id").setSource( + watchBuilder().trigger(schedule(interval("5s"))) .input(searchInput(request)) .condition(InternalAlwaysCondition.INSTANCE) - .addAction("logger", loggingAction("indexed"))) - .get(); + .addAction("logger", loggingAction("indexed")) + ).get(); assertThat(putWatchResponse.isCreated(), is(true)); timeWarp().trigger("_id"); @@ -62,11 +64,13 @@ WatcherSearchTemplateRequest.DEFAULT_INDICES_OPTIONS, new BytesArray("{}") // the action should fail as no email server is available assertWatchWithMinimumActionsCount("_id", ExecutionState.EXECUTED, 1); - SearchResponse response = client().prepareSearch(HistoryStoreField.DATA_STREAM + "*").setSource(searchSource() - .aggregation(terms("input_search_type").field("result.input.search.request.search_type")) - .aggregation(terms("input_indices").field("result.input.search.request.indices")) - .aggregation(terms("input_body").field("result.input.search.request.body"))) - .get(); + SearchResponse response = client().prepareSearch(HistoryStoreField.DATA_STREAM + "*") + .setSource( + searchSource().aggregation(terms("input_search_type").field("result.input.search.request.search_type")) + .aggregation(terms("input_indices").field("result.input.search.request.indices")) + .aggregation(terms("input_body").field("result.input.search.request.body")) + ) + .get(); assertThat(response, notNullValue()); assertThat(response.getHits().getTotalHits().value, is(oneOf(1L, 2L))); diff --git a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/history/HistoryTemplateTimeMappingsTests.java b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/history/HistoryTemplateTimeMappingsTests.java index 4f52fe03b6b1a..95551a73339be 100644 --- a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/history/HistoryTemplateTimeMappingsTests.java +++ b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/history/HistoryTemplateTimeMappingsTests.java @@ -7,6 +7,7 @@ package org.elasticsearch.xpack.watcher.history; import com.carrotsearch.hppc.cursors.ObjectObjectCursor; + import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse; import org.elasticsearch.cluster.metadata.MappingMetadata; @@ -34,12 +35,12 @@ public class HistoryTemplateTimeMappingsTests extends AbstractWatcherIntegrationTestCase { public void testTimeFields() throws Exception { - PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client(), "_id").setSource(watchBuilder() - .trigger(schedule(interval("5s"))) + PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client(), "_id").setSource( + watchBuilder().trigger(schedule(interval("5s"))) .input(simpleInput()) .condition(InternalAlwaysCondition.INSTANCE) - .addAction("_logging", loggingAction("foobar"))) - .get(); + .addAction("_logging", loggingAction("foobar")) + ).get(); assertThat(putWatchResponse.isCreated(), is(true)); timeWarp().trigger("_id"); @@ -60,8 +61,10 @@ public void testTimeFields() throws Exception { logger.info("checking index [{}] with metadata:\n[{}]", metadatas.key, metadata.source().toString()); assertThat(extractValue("properties.trigger_event.properties.type.type", source), is((Object) "keyword")); assertThat(extractValue("properties.trigger_event.properties.triggered_time.type", source), is((Object) "date")); - assertThat(extractValue("properties.trigger_event.properties.schedule.properties.scheduled_time.type", source), - is((Object) "date")); + assertThat( + extractValue("properties.trigger_event.properties.schedule.properties.scheduled_time.type", source), + is((Object) "date") + ); assertThat(extractValue("properties.result.properties.execution_time.type", source), is((Object) "date")); } catch (ElasticsearchParseException e) { throw new RuntimeException(e); diff --git a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/history/HistoryTemplateTransformMappingsTests.java b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/history/HistoryTemplateTransformMappingsTests.java index 200fb03d2f1e7..aec701a9f3d95 100644 --- a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/history/HistoryTemplateTransformMappingsTests.java +++ b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/history/HistoryTemplateTransformMappingsTests.java @@ -16,9 +16,9 @@ import java.util.Objects; import java.util.Optional; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.xpack.watcher.actions.ActionBuilders.loggingAction; import static org.elasticsearch.xpack.watcher.client.WatchSourceBuilders.watchBuilder; import static org.elasticsearch.xpack.watcher.input.InputBuilders.simpleInput; @@ -30,59 +30,82 @@ public class HistoryTemplateTransformMappingsTests extends AbstractWatcherIntegrationTestCase { public void testTransformFields() throws Exception { - assertAcked(client().admin().indices().prepareCreate("idx").setMapping( - jsonBuilder().startObject() + assertAcked( + client().admin() + .indices() + .prepareCreate("idx") + .setMapping( + jsonBuilder().startObject() .startObject("properties") .startObject("foo") .field("type", "object") .field("enabled", false) .endObject() .endObject() - .endObject())); + .endObject() + ) + ); - client().prepareBulk().setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .add(client().prepareIndex().setIndex("idx").setId("1") - .setSource(jsonBuilder().startObject().field("name", "first").field("foo", "bar").endObject())) - .add(client().prepareIndex().setIndex("idx").setId("2") - .setSource(jsonBuilder().startObject().field("name", "second") - .startObject("foo").field("what", "ever").endObject().endObject())) - .get(); + client().prepareBulk() + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + .add( + client().prepareIndex() + .setIndex("idx") + .setId("1") + .setSource(jsonBuilder().startObject().field("name", "first").field("foo", "bar").endObject()) + ) + .add( + client().prepareIndex() + .setIndex("idx") + .setId("2") + .setSource( + jsonBuilder().startObject().field("name", "second").startObject("foo").field("what", "ever").endObject().endObject() + ) + ) + .get(); - new PutWatchRequestBuilder(client(), "_first").setSource(watchBuilder() - .trigger(schedule(interval("5s"))) + new PutWatchRequestBuilder(client(), "_first").setSource( + watchBuilder().trigger(schedule(interval("5s"))) .input(simpleInput()) .transform(searchTransform(templateRequest(searchSource().query(QueryBuilders.termQuery("name", "first")), "idx"))) - .addAction("logger", - searchTransform(templateRequest(searchSource().query(QueryBuilders.termQuery("name", "first")), "idx")), - loggingAction("indexed"))) - .get(); + .addAction( + "logger", + searchTransform(templateRequest(searchSource().query(QueryBuilders.termQuery("name", "first")), "idx")), + loggingAction("indexed") + ) + ).get(); // execute another watch which with a transform that should conflict with the previous watch. Since the // mapping for the transform construct is disabled, there should be no problems. - new PutWatchRequestBuilder(client(), "_second").setSource(watchBuilder() - .trigger(schedule(interval("5s"))) + new PutWatchRequestBuilder(client(), "_second").setSource( + watchBuilder().trigger(schedule(interval("5s"))) .input(simpleInput()) .transform(searchTransform(templateRequest(searchSource().query(QueryBuilders.termQuery("name", "second")), "idx"))) - .addAction("logger", - searchTransform(templateRequest(searchSource().query(QueryBuilders.termQuery("name", "second")), "idx")), - loggingAction("indexed"))) - .get(); + .addAction( + "logger", + searchTransform(templateRequest(searchSource().query(QueryBuilders.termQuery("name", "second")), "idx")), + loggingAction("indexed") + ) + ).get(); new ExecuteWatchRequestBuilder(client(), "_first").setRecordExecution(true).get(); new ExecuteWatchRequestBuilder(client(), "_second").setRecordExecution(true).get(); assertBusy(() -> { - GetFieldMappingsResponse response = client().admin().indices() - .prepareGetFieldMappings(".watcher-history*") - .setFields("result.actions.transform.payload") - .includeDefaults(true) - .get(); + GetFieldMappingsResponse response = client().admin() + .indices() + .prepareGetFieldMappings(".watcher-history*") + .setFields("result.actions.transform.payload") + .includeDefaults(true) + .get(); // time might have rolled over to a new day, thus we need to check that this field exists only in one of the history indices - Optional mapping = response.mappings().values().stream() - .map(map -> map.get("result.actions.transform.payload")) - .filter(Objects::nonNull) - .findFirst(); + Optional mapping = response.mappings() + .values() + .stream() + .map(map -> map.get("result.actions.transform.payload")) + .filter(Objects::nonNull) + .findFirst(); assertTrue(mapping.isEmpty()); }); diff --git a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/input/chain/ChainIntegrationTests.java b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/input/chain/ChainIntegrationTests.java index 5e7e93bdfb68e..1f0f38ac5be5e 100644 --- a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/input/chain/ChainIntegrationTests.java +++ b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/input/chain/ChainIntegrationTests.java @@ -10,10 +10,10 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.CollectionUtils; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.IndexNotFoundException; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.transport.netty4.Netty4Plugin; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.watcher.transport.actions.put.PutWatchRequestBuilder; import org.elasticsearch.xpack.watcher.common.http.HttpRequestTemplate; import org.elasticsearch.xpack.watcher.input.http.HttpInput; @@ -23,8 +23,8 @@ import java.util.Collection; import static org.elasticsearch.action.support.WriteRequest.RefreshPolicy.IMMEDIATE; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.xpack.watcher.actions.ActionBuilders.indexAction; import static org.elasticsearch.xpack.watcher.client.WatchSourceBuilders.watchBuilder; import static org.elasticsearch.xpack.watcher.input.InputBuilders.chainInput; @@ -44,9 +44,7 @@ protected boolean addMockHttpTransport() { @Override protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { - return Settings.builder() - .put(super.nodeSettings(nodeOrdinal, otherSettings)) - .build(); + return Settings.builder().put(super.nodeSettings(nodeOrdinal, otherSettings)).build(); } @Override @@ -60,20 +58,20 @@ public void testChainedInputsAreWorking() throws Exception { client().prepareIndex().setIndex(index).setId("id").setSource("{}", XContentType.JSON).setRefreshPolicy(IMMEDIATE).get(); InetSocketAddress address = internalCluster().httpAddresses()[0]; - HttpInput.Builder httpInputBuilder = httpInput(HttpRequestTemplate.builder(address.getHostString(), address.getPort()) + HttpInput.Builder httpInputBuilder = httpInput( + HttpRequestTemplate.builder(address.getHostString(), address.getPort()) .path("/" + index + "/_search") - .body(Strings.toString(jsonBuilder().startObject().field("size", 1).endObject()))); + .body(Strings.toString(jsonBuilder().startObject().field("size", 1).endObject())) + ); - ChainInput.Builder chainedInputBuilder = chainInput() - .add("first", simpleInput("url", "/" + index + "/_search")) - .add("second", httpInputBuilder); + ChainInput.Builder chainedInputBuilder = chainInput().add("first", simpleInput("url", "/" + index + "/_search")) + .add("second", httpInputBuilder); - new PutWatchRequestBuilder(client(), "_name") - .setSource(watchBuilder() - .trigger(schedule(interval(5, SECONDS))) - .input(chainedInputBuilder) - .addAction("indexAction", indexAction("my-index"))) - .get(); + new PutWatchRequestBuilder(client(), "_name").setSource( + watchBuilder().trigger(schedule(interval(5, SECONDS))) + .input(chainedInputBuilder) + .addAction("indexAction", indexAction("my-index")) + ).get(); timeWarp().trigger("_name"); refresh(); @@ -88,7 +86,7 @@ public void assertWatchExecuted() { assertHitCount(searchResponse, 1); assertThat(searchResponse.getHits().getAt(0).getSourceAsString(), containsString("the-most-awesome-index-ever")); } catch (IndexNotFoundException e) { - fail("Index not found: ["+ e.getIndex() + "]"); + fail("Index not found: [" + e.getIndex() + "]"); } } } diff --git a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/notification/email/EmailSecretsIntegrationTests.java b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/notification/email/EmailSecretsIntegrationTests.java index b0e76ffd2fdb1..6d262e1e4d423 100644 --- a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/notification/email/EmailSecretsIntegrationTests.java +++ b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/notification/email/EmailSecretsIntegrationTests.java @@ -69,11 +69,11 @@ protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { } } Settings.Builder builder = Settings.builder() - .put(super.nodeSettings(nodeOrdinal, otherSettings)) - .put("xpack.notification.email.account.test.smtp.auth", true) - .put("xpack.notification.email.account.test.smtp.port", server.port()) - .put("xpack.notification.email.account.test.smtp.host", "localhost") - .put("xpack.watcher.encrypt_sensitive_data", encryptSensitiveData); + .put(super.nodeSettings(nodeOrdinal, otherSettings)) + .put("xpack.notification.email.account.test.smtp.auth", true) + .put("xpack.notification.email.account.test.smtp.port", server.port()) + .put("xpack.notification.email.account.test.smtp.host", "localhost") + .put("xpack.watcher.encrypt_sensitive_data", encryptSensitiveData); if (encryptSensitiveData) { MockSecureSettings secureSettings = new MockSecureSettings(); secureSettings.setFile(WatcherField.ENCRYPTION_KEY_SETTING.getKey(), encryptionKey); @@ -83,18 +83,16 @@ protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { } public void testEmail() throws Exception { - new PutWatchRequestBuilder(client(), "_id") - .setSource(watchBuilder() - .trigger(schedule(cron("0 0 0 1 * ? 2020"))) - .input(simpleInput()) - .condition(InternalAlwaysCondition.INSTANCE) - .addAction("_email", ActionBuilders.emailAction( - EmailTemplate.builder() - .from("from@example.org") - .to("to@example.org") - .subject("_subject")) - .setAuthentication(EmailServer.USERNAME, EmailServer.PASSWORD.toCharArray()))) - .get(); + new PutWatchRequestBuilder(client(), "_id").setSource( + watchBuilder().trigger(schedule(cron("0 0 0 1 * ? 2020"))) + .input(simpleInput()) + .condition(InternalAlwaysCondition.INSTANCE) + .addAction( + "_email", + ActionBuilders.emailAction(EmailTemplate.builder().from("from@example.org").to("to@example.org").subject("_subject")) + .setAuthentication(EmailServer.USERNAME, EmailServer.PASSWORD.toCharArray()) + ) + ).get(); // verifying the email password is stored encrypted in the index GetResponse response = client().prepareGet().setIndex(Watch.INDEX).setId("_id").get(); @@ -138,11 +136,10 @@ public void testEmail() throws Exception { }); TriggerEvent triggerEvent = new ScheduleTriggerEvent(ZonedDateTime.now(ZoneOffset.UTC), ZonedDateTime.now(ZoneOffset.UTC)); - ExecuteWatchResponse executeResponse = new ExecuteWatchRequestBuilder(client(), "_id") - .setRecordExecution(false) - .setTriggerEvent(triggerEvent) - .setActionMode("_all", ActionExecutionMode.FORCE_EXECUTE) - .get(); + ExecuteWatchResponse executeResponse = new ExecuteWatchRequestBuilder(client(), "_id").setRecordExecution(false) + .setTriggerEvent(triggerEvent) + .setActionMode("_all", ActionExecutionMode.FORCE_EXECUTE) + .get(); assertThat(executeResponse, notNullValue()); contentSource = executeResponse.getRecordSource(); diff --git a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/test/AbstractWatcherIntegrationTestCase.java b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/test/AbstractWatcherIntegrationTestCase.java index 1d26b488c0dcc..ae22d9f6dac98 100644 --- a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/test/AbstractWatcherIntegrationTestCase.java +++ b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/test/AbstractWatcherIntegrationTestCase.java @@ -22,12 +22,12 @@ import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; import org.elasticsearch.cluster.metadata.MappingMetadata; import org.elasticsearch.cluster.routing.IndexRoutingTable; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.settings.ClusterSettings; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.support.XContentMapValues; +import org.elasticsearch.core.TimeValue; +import org.elasticsearch.core.Tuple; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.license.LicenseService; import org.elasticsearch.license.XPackLicenseState; @@ -112,16 +112,16 @@ public abstract class AbstractWatcherIntegrationTestCase extends ESIntegTestCase @Override protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { return Settings.builder() - .put(super.nodeSettings(nodeOrdinal, otherSettings)) - .put(XPackSettings.SECURITY_ENABLED.getKey(), false) - .put(LicenseService.SELF_GENERATED_LICENSE_TYPE.getKey(), "trial") - // we do this by default in core, but for watcher this isn't needed and only adds noise. - .put("index.store.mock.check_index_on_close", false) - // watcher settings that should work despite randomization - .put("xpack.watcher.execution.scroll.size", randomIntBetween(1, 100)) - .put("xpack.watcher.watch.scroll.size", randomIntBetween(1, 100)) - .put("indices.lifecycle.history_index_enabled", false) - .build(); + .put(super.nodeSettings(nodeOrdinal, otherSettings)) + .put(XPackSettings.SECURITY_ENABLED.getKey(), false) + .put(LicenseService.SELF_GENERATED_LICENSE_TYPE.getKey(), "trial") + // we do this by default in core, but for watcher this isn't needed and only adds noise. + .put("index.store.mock.check_index_on_close", false) + // watcher settings that should work despite randomization + .put("xpack.watcher.execution.scroll.size", randomIntBetween(1, 100)) + .put("xpack.watcher.watch.scroll.size", randomIntBetween(1, 100)) + .put("indices.lifecycle.history_index_enabled", false) + .build(); } @Override @@ -177,8 +177,10 @@ protected boolean timeWarped() { @Before public void _setup() throws Exception { if (timeWarped()) { - timeWarp = new TimeWarp(internalCluster().getInstances(ScheduleTriggerEngineMock.class), - (ClockMock)getInstanceFromMaster(ClockHolder.class).clock); + timeWarp = new TimeWarp( + internalCluster().getInstances(ScheduleTriggerEngineMock.class), + (ClockMock) getInstanceFromMaster(ClockHolder.class).clock + ); } if (internalCluster().size() > 0) { @@ -207,11 +209,12 @@ public void _cleanup() throws Exception { // Otherwise ESIntegTestCase test cluster's wipe cluster logic that deletes all indices may fail, // because it attempts to remove the write index of an existing data stream. waitNoPendingTasksOnAll(); - String[] dataStreamsToDelete = {HistoryStoreField.DATA_STREAM}; + String[] dataStreamsToDelete = { HistoryStoreField.DATA_STREAM }; client().execute(DeleteDataStreamAction.INSTANCE, new DeleteDataStreamAction.Request(dataStreamsToDelete)); GetDataStreamAction.Request getDataStreamRequest = new GetDataStreamAction.Request(dataStreamsToDelete); - assertBusy(()-> assertFutureThrows(client().execute(GetDataStreamAction.INSTANCE, getDataStreamRequest), - ResourceNotFoundException.class)); + assertBusy( + () -> assertFutureThrows(client().execute(GetDataStreamAction.INSTANCE, getDataStreamRequest), ResourceNotFoundException.class) + ); } /** @@ -231,14 +234,16 @@ private void createWatcherIndicesOrAliases() throws Exception { if (randomBoolean()) { // Create an index to get the template String tempIndex = ".watches" + randomAlphaOfLength(5).toLowerCase(Locale.ROOT); - CreateIndexResponse response = client().admin().indices().prepareCreate(tempIndex) - .setCause("Index to test aliases with .watches index") - .addAlias(new Alias(Watch.INDEX)) - .get(); + CreateIndexResponse response = client().admin() + .indices() + .prepareCreate(tempIndex) + .setCause("Index to test aliases with .watches index") + .addAlias(new Alias(Watch.INDEX)) + .get(); assertAcked(response); // Now replace it with a randomly named index - watchIndexName = randomAlphaOfLengthBetween(5,10).toLowerCase(Locale.ROOT); + watchIndexName = randomAlphaOfLengthBetween(5, 10).toLowerCase(Locale.ROOT); replaceWatcherIndexWithRandomlyNamedIndex(Watch.INDEX, watchIndexName); logger.info("set alias for .watches index to [{}]", watchIndexName); @@ -254,15 +259,19 @@ private void createWatcherIndicesOrAliases() throws Exception { // alias for .triggered-watches, ensuring the index template is set appropriately if (randomBoolean()) { String tempIndex = ".triggered_watches-alias-index"; - CreateIndexResponse response = client().admin().indices().prepareCreate(tempIndex) - .setCause("Index to test aliases with .triggered-watches index") - .addAlias(new Alias(TriggeredWatchStoreField.INDEX_NAME)) - .get(); + CreateIndexResponse response = client().admin() + .indices() + .prepareCreate(tempIndex) + .setCause("Index to test aliases with .triggered-watches index") + .addAlias(new Alias(TriggeredWatchStoreField.INDEX_NAME)) + .get(); assertAcked(response); // Now replace it with a randomly-named index - triggeredWatchIndexName = randomValueOtherThan(watchIndexName, - () -> randomAlphaOfLengthBetween(5,10).toLowerCase(Locale.ROOT)); + triggeredWatchIndexName = randomValueOtherThan( + watchIndexName, + () -> randomAlphaOfLengthBetween(5, 10).toLowerCase(Locale.ROOT) + ); replaceWatcherIndexWithRandomlyNamedIndex(TriggeredWatchStoreField.INDEX_NAME, triggeredWatchIndexName); logger.info("set alias for .triggered-watches index to [{}]", triggeredWatchIndexName); } else { @@ -283,7 +292,9 @@ public void replaceWatcherIndexWithRandomlyNamedIndex(String originalIndexOrAlia newSettings.remove("index.creation_date"); newSettings.remove("index.version.created"); - CreateIndexResponse createIndexResponse = client().admin().indices().prepareCreate(to) + CreateIndexResponse createIndexResponse = client().admin() + .indices() + .prepareCreate(to) .setMapping(mapping.sourceAsMap()) .setSettings(newSettings) .get(); @@ -349,21 +360,29 @@ protected void assertValue(XContentSource source, String path, Matcher matche assertThat(source.getValue(path), (Matcher) matcher); } - protected void assertWatchWithMinimumPerformedActionsCount(final String watchName, - final long minimumExpectedWatchActionsWithActionPerformed) throws Exception { + protected void assertWatchWithMinimumPerformedActionsCount( + final String watchName, + final long minimumExpectedWatchActionsWithActionPerformed + ) throws Exception { assertWatchWithMinimumPerformedActionsCount(watchName, minimumExpectedWatchActionsWithActionPerformed, true); } // TODO remove this shitty method... the `assertConditionMet` is bogus - protected void assertWatchWithMinimumPerformedActionsCount(final String watchName, - final long minimumExpectedWatchActionsWithActionPerformed, - final boolean assertConditionMet) throws Exception { + protected void assertWatchWithMinimumPerformedActionsCount( + final String watchName, + final long minimumExpectedWatchActionsWithActionPerformed, + final boolean assertConditionMet + ) throws Exception { final AtomicReference lastResponse = new AtomicReference<>(); try { assertBusy(() -> { ClusterState state = client().admin().cluster().prepareState().get().getState(); - String[] watchHistoryIndices = indexNameExpressionResolver().concreteIndexNames(state, - IndicesOptions.lenientExpandOpen(), true, HistoryStoreField.DATA_STREAM + "*"); + String[] watchHistoryIndices = indexNameExpressionResolver().concreteIndexNames( + state, + IndicesOptions.lenientExpandOpen(), + true, + HistoryStoreField.DATA_STREAM + "*" + ); assertThat(watchHistoryIndices, not(emptyArray())); for (String index : watchHistoryIndices) { IndexRoutingTable routingTable = state.getRoutingTable().index(index); @@ -373,16 +392,23 @@ protected void assertWatchWithMinimumPerformedActionsCount(final String watchNam refresh(); SearchResponse searchResponse = client().prepareSearch(HistoryStoreField.DATA_STREAM + "*") - .setIndicesOptions(IndicesOptions.lenientExpandOpen()) - .setQuery(boolQuery().must(matchQuery("watch_id", watchName)).must(matchQuery("state", - ExecutionState.EXECUTED.id()))) - .get(); + .setIndicesOptions(IndicesOptions.lenientExpandOpen()) + .setQuery(boolQuery().must(matchQuery("watch_id", watchName)).must(matchQuery("state", ExecutionState.EXECUTED.id()))) + .get(); lastResponse.set(searchResponse); - assertThat("could not find executed watch record for watch " + watchName, searchResponse.getHits().getTotalHits().value, - greaterThanOrEqualTo(minimumExpectedWatchActionsWithActionPerformed)); + assertThat( + "could not find executed watch record for watch " + watchName, + searchResponse.getHits().getTotalHits().value, + greaterThanOrEqualTo(minimumExpectedWatchActionsWithActionPerformed) + ); if (assertConditionMet) { - assertThat((Integer) XContentMapValues.extractValue("result.input.payload.hits.total", - searchResponse.getHits().getAt(0).getSourceAsMap()), greaterThanOrEqualTo(1)); + assertThat( + (Integer) XContentMapValues.extractValue( + "result.input.payload.hits.total", + searchResponse.getHits().getAt(0).getSourceAsMap() + ), + greaterThanOrEqualTo(1) + ); } }); } catch (AssertionError error) { @@ -397,8 +423,7 @@ protected void assertWatchWithMinimumPerformedActionsCount(final String watchNam } protected SearchResponse searchWatchRecords(Consumer requestBuilderCallback) { - SearchRequestBuilder builder = - client().prepareSearch(HistoryStoreField.DATA_STREAM + "*"); + SearchRequestBuilder builder = client().prepareSearch(HistoryStoreField.DATA_STREAM + "*"); requestBuilderCallback.accept(builder); return builder.get(); } @@ -406,22 +431,26 @@ protected SearchResponse searchWatchRecords(Consumer reque protected long findNumberOfPerformedActions(String watchName) { refresh(); SearchResponse searchResponse = client().prepareSearch(HistoryStoreField.DATA_STREAM + "*") - .setIndicesOptions(IndicesOptions.lenientExpandOpen()) - .setQuery(boolQuery().must(matchQuery("watch_id", watchName)).must(matchQuery("state", ExecutionState.EXECUTED.id()))) - .get(); + .setIndicesOptions(IndicesOptions.lenientExpandOpen()) + .setQuery(boolQuery().must(matchQuery("watch_id", watchName)).must(matchQuery("state", ExecutionState.EXECUTED.id()))) + .get(); return searchResponse.getHits().getTotalHits().value; } - protected void assertWatchWithNoActionNeeded(final String watchName, - final long expectedWatchActionsWithNoActionNeeded) throws Exception { + protected void assertWatchWithNoActionNeeded(final String watchName, final long expectedWatchActionsWithNoActionNeeded) + throws Exception { final AtomicReference lastResponse = new AtomicReference<>(); try { assertBusy(() -> { // The watch_history index gets created in the background when the first watch is triggered // so we to check first is this index is created and shards are started ClusterState state = client().admin().cluster().prepareState().get().getState(); - String[] watchHistoryIndices = indexNameExpressionResolver().concreteIndexNames(state, - IndicesOptions.lenientExpandOpen(), true, HistoryStoreField.DATA_STREAM + "*"); + String[] watchHistoryIndices = indexNameExpressionResolver().concreteIndexNames( + state, + IndicesOptions.lenientExpandOpen(), + true, + HistoryStoreField.DATA_STREAM + "*" + ); assertThat(watchHistoryIndices, not(emptyArray())); for (String index : watchHistoryIndices) { IndexRoutingTable routingTable = state.getRoutingTable().index(index); @@ -430,10 +459,12 @@ protected void assertWatchWithNoActionNeeded(final String watchName, } refresh(); SearchResponse searchResponse = client().prepareSearch(HistoryStoreField.DATA_STREAM + "*") - .setIndicesOptions(IndicesOptions.lenientExpandOpen()) - .setQuery(boolQuery().must(matchQuery("watch_id", watchName)).must(matchQuery("state", - ExecutionState.EXECUTION_NOT_NEEDED.id()))) - .get(); + .setIndicesOptions(IndicesOptions.lenientExpandOpen()) + .setQuery( + boolQuery().must(matchQuery("watch_id", watchName)) + .must(matchQuery("state", ExecutionState.EXECUTION_NOT_NEEDED.id())) + ) + .get(); lastResponse.set(searchResponse); assertThat(searchResponse.getHits().getTotalHits().value, greaterThanOrEqualTo(expectedWatchActionsWithNoActionNeeded)); }); @@ -448,12 +479,16 @@ protected void assertWatchWithNoActionNeeded(final String watchName, } } - protected void assertWatchWithMinimumActionsCount(final String watchName, final ExecutionState recordState, - final long recordCount) throws Exception { + protected void assertWatchWithMinimumActionsCount(final String watchName, final ExecutionState recordState, final long recordCount) + throws Exception { assertBusy(() -> { ClusterState state = client().admin().cluster().prepareState().get().getState(); - String[] watchHistoryIndices = indexNameExpressionResolver().concreteIndexNames(state, IndicesOptions.lenientExpandOpen(), - true, HistoryStoreField.DATA_STREAM + "*"); + String[] watchHistoryIndices = indexNameExpressionResolver().concreteIndexNames( + state, + IndicesOptions.lenientExpandOpen(), + true, + HistoryStoreField.DATA_STREAM + "*" + ); assertThat(watchHistoryIndices, not(emptyArray())); for (String index : watchHistoryIndices) { IndexRoutingTable routingTable = state.getRoutingTable().index(index); @@ -463,19 +498,24 @@ protected void assertWatchWithMinimumActionsCount(final String watchName, final refresh(); SearchResponse searchResponse = client().prepareSearch(HistoryStoreField.DATA_STREAM + "*") - .setIndicesOptions(IndicesOptions.lenientExpandOpen()) - .setQuery(boolQuery().must(matchQuery("watch_id", watchName)).must(matchQuery("state", recordState.id()))) - .get(); - assertThat("could not find executed watch record", searchResponse.getHits().getTotalHits().value, - greaterThanOrEqualTo(recordCount)); + .setIndicesOptions(IndicesOptions.lenientExpandOpen()) + .setQuery(boolQuery().must(matchQuery("watch_id", watchName)).must(matchQuery("state", recordState.id()))) + .get(); + assertThat( + "could not find executed watch record", + searchResponse.getHits().getTotalHits().value, + greaterThanOrEqualTo(recordCount) + ); }); } private void ensureWatcherTemplatesAdded() throws Exception { // Verify that the index templates exist: assertBusy(() -> { - GetComposableIndexTemplateAction.Response response = client().execute(GetComposableIndexTemplateAction.INSTANCE, - new GetComposableIndexTemplateAction.Request(HISTORY_TEMPLATE_NAME)).get(); + GetComposableIndexTemplateAction.Response response = client().execute( + GetComposableIndexTemplateAction.INSTANCE, + new GetComposableIndexTemplateAction.Request(HISTORY_TEMPLATE_NAME) + ).get(); assertThat("[" + HISTORY_TEMPLATE_NAME + "] is missing", response.indexTemplates().size(), equalTo(1)); }); } @@ -484,9 +524,10 @@ protected void startWatcher() throws Exception { assertBusy(() -> { WatcherStatsResponse watcherStatsResponse = new WatcherStatsRequestBuilder(client()).get(); assertThat(watcherStatsResponse.hasFailures(), is(false)); - List> currentStatesFromStatsRequest = watcherStatsResponse.getNodes().stream() - .map(response -> Tuple.tuple(response.getNode().getName(), response.getWatcherState())) - .collect(Collectors.toList()); + List> currentStatesFromStatsRequest = watcherStatsResponse.getNodes() + .stream() + .map(response -> Tuple.tuple(response.getNode().getName(), response.getWatcherState())) + .collect(Collectors.toList()); List states = currentStatesFromStatsRequest.stream().map(Tuple::v2).collect(Collectors.toList()); logger.info("waiting to start watcher, current states {}", currentStatesFromStatsRequest); @@ -528,13 +569,17 @@ protected void stopWatcher() throws Exception { assertBusy(() -> { WatcherStatsResponse watcherStatsResponse = new WatcherStatsRequestBuilder(client()).get(); assertThat(watcherStatsResponse.hasFailures(), is(false)); - List> currentStatesFromStatsRequest = watcherStatsResponse.getNodes().stream() - .map(response -> Tuple.tuple(response.getNode().getName() + " (" + response.getThreadPoolQueueSize() + ")", - response.getWatcherState())).collect(Collectors.toList()); + List> currentStatesFromStatsRequest = watcherStatsResponse.getNodes() + .stream() + .map( + response -> Tuple.tuple( + response.getNode().getName() + " (" + response.getThreadPoolQueueSize() + ")", + response.getWatcherState() + ) + ) + .collect(Collectors.toList()); List states = currentStatesFromStatsRequest.stream().map(Tuple::v2).collect(Collectors.toList()); - - logger.info("waiting to stop watcher, current states {}", currentStatesFromStatsRequest); boolean isAllStateStarted = states.stream().allMatch(w -> w == WatcherState.STARTED); @@ -565,8 +610,12 @@ protected void stopWatcher() throws Exception { public static class NoopEmailService extends EmailService { public NoopEmailService() { - super(Settings.EMPTY, null, mock(SSLService.class), - new ClusterSettings(Settings.EMPTY, new HashSet<>(EmailService.getSettings()))); + super( + Settings.EMPTY, + null, + mock(SSLService.class), + new ClusterSettings(Settings.EMPTY, new HashSet<>(EmailService.getSettings())) + ); } @Override @@ -596,9 +645,7 @@ public ClockMock clock() { public void trigger(String watchId, int times, TimeValue timeValue) throws Exception { assertBusy(() -> { - long triggeredCount = schedulers.stream() - .filter(scheduler -> scheduler.trigger(watchId, times, timeValue)) - .count(); + long triggeredCount = schedulers.stream().filter(scheduler -> scheduler.trigger(watchId, times, timeValue)).count(); String msg = String.format(Locale.ROOT, "watch was triggered on [%d] schedulers, expected [1]", triggeredCount); if (triggeredCount > 1) { logger.warn(msg); @@ -635,19 +682,19 @@ public void removeAndEnsureHealthy(InternalTestCluster cluster) { @Override public synchronized void applyToNode(String node, InternalTestCluster cluster) { if (frozen) { - ((ClockMock)cluster.getInstance(ClockHolder.class, node).clock).freeze(); + ((ClockMock) cluster.getInstance(ClockHolder.class, node).clock).freeze(); } } @Override public void removeFromNode(String node, InternalTestCluster cluster) { - ((ClockMock)cluster.getInstance(ClockHolder.class, node).clock).unfreeze(); + ((ClockMock) cluster.getInstance(ClockHolder.class, node).clock).unfreeze(); } @Override public synchronized void startDisrupting() { frozen = true; - for (String node: cluster.getNodeNames()) { + for (String node : cluster.getNodeNames()) { applyToNode(node, cluster); } } @@ -655,14 +702,13 @@ public synchronized void startDisrupting() { @Override public void stopDisrupting() { frozen = false; - for (String node: cluster.getNodeNames()) { + for (String node : cluster.getNodeNames()) { removeFromNode(node, cluster); } } @Override - public void testClusterClosed() { - } + public void testClusterClosed() {} @Override public TimeValue expectedTimeToHeal() { diff --git a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/test/integration/BasicWatcherTests.java b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/test/integration/BasicWatcherTests.java index 442daef8b102c..7a86741d171d7 100644 --- a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/test/integration/BasicWatcherTests.java +++ b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/test/integration/BasicWatcherTests.java @@ -12,8 +12,6 @@ import org.elasticsearch.action.search.SearchType; import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.query.TermQueryBuilder; import org.elasticsearch.protocol.xpack.watcher.DeleteWatchResponse; import org.elasticsearch.protocol.xpack.watcher.PutWatchResponse; @@ -22,6 +20,8 @@ import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.searchafter.SearchAfterBuilder; import org.elasticsearch.search.sort.FieldSortBuilder; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.watcher.client.WatchSourceBuilder; import org.elasticsearch.xpack.core.watcher.support.xcontent.XContentSource; import org.elasticsearch.xpack.core.watcher.transport.actions.QueryWatchesAction; @@ -45,13 +45,13 @@ import java.util.List; import java.util.Map; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery; import static org.elasticsearch.index.query.QueryBuilders.matchQuery; import static org.elasticsearch.index.query.QueryBuilders.termQuery; import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.xpack.watcher.actions.ActionBuilders.indexAction; import static org.elasticsearch.xpack.watcher.actions.ActionBuilders.loggingAction; import static org.elasticsearch.xpack.watcher.client.WatchSourceBuilders.watchBuilder; @@ -79,13 +79,13 @@ public void testIndexWatch() throws Exception { refresh(); WatcherSearchTemplateRequest request = templateRequest(searchSource().query(termQuery("field", "foo")), "idx"); new PutWatchRequestBuilder(client()).setId("_name") - .setSource(watchBuilder() - .trigger(schedule(interval(5, IntervalSchedule.Interval.Unit.SECONDS))) - .input(searchInput(request)) - .condition(new CompareCondition("ctx.payload.hits.total", CompareCondition.Op.EQ, 1L)) - .addAction("_logger", loggingAction("_logging") - .setCategory("_category"))) - .get(); + .setSource( + watchBuilder().trigger(schedule(interval(5, IntervalSchedule.Interval.Unit.SECONDS))) + .input(searchInput(request)) + .condition(new CompareCondition("ctx.payload.hits.total", CompareCondition.Op.EQ, 1L)) + .addAction("_logger", loggingAction("_logging").setCategory("_category")) + ) + .get(); timeWarp().trigger("_name"); assertWatchWithMinimumPerformedActionsCount("_name", 1); @@ -98,11 +98,12 @@ public void testIndexWatch() throws Exception { public void testIndexWatchRegisterWatchBeforeTargetIndex() throws Exception { WatcherSearchTemplateRequest searchRequest = templateRequest(searchSource().query(termQuery("field", "value")), "idx"); new PutWatchRequestBuilder(client()).setId("_name") - .setSource(watchBuilder() - .trigger(schedule(interval(5, IntervalSchedule.Interval.Unit.SECONDS))) - .input(searchInput(searchRequest)) - .condition(new CompareCondition("ctx.payload.hits.total", CompareCondition.Op.EQ, 1L))) - .get(); + .setSource( + watchBuilder().trigger(schedule(interval(5, IntervalSchedule.Interval.Unit.SECONDS))) + .input(searchInput(searchRequest)) + .condition(new CompareCondition("ctx.payload.hits.total", CompareCondition.Op.EQ, 1L)) + ) + .get(); timeWarp().trigger("_name"); // The watch's condition won't meet because there is no data that matches with the query assertWatchWithNoActionNeeded("_name", 1); @@ -121,11 +122,12 @@ public void testIndexWatchRegisterWatchBeforeTargetIndex() throws Exception { public void testDeleteWatch() throws Exception { WatcherSearchTemplateRequest searchRequest = templateRequest(searchSource().query(matchAllQuery()), "idx"); PutWatchResponse indexResponse = new PutWatchRequestBuilder(client()).setId("_name") - .setSource(watchBuilder() - .trigger(schedule(cron("0/1 * * * * ? 2020"))) - .input(searchInput(searchRequest)) - .condition(new CompareCondition("ctx.payload.hits.total", CompareCondition.Op.EQ, 1L))) - .get(); + .setSource( + watchBuilder().trigger(schedule(cron("0/1 * * * * ? 2020"))) + .input(searchInput(searchRequest)) + .condition(new CompareCondition("ctx.payload.hits.total", CompareCondition.Op.EQ, 1L)) + ) + .get(); assertThat(indexResponse.isCreated(), is(true)); DeleteWatchResponse deleteWatchResponse = new DeleteWatchRequestBuilder(client()).setId("_name").get(); assertThat(deleteWatchResponse, notNullValue()); @@ -157,16 +159,14 @@ public void testMalformedWatch() throws Exception { watchSource.endObject(); try { new PutWatchRequestBuilder(client()).setId("_name") - .setSource(BytesReference.bytes(watchSource), watchSource.contentType()) - .get(); + .setSource(BytesReference.bytes(watchSource), watchSource.contentType()) + .get(); fail(); } catch (ElasticsearchParseException e) { // In watch store we fail parsing if an watch contains undefined fields. } try { - client().prepareIndex().setIndex(Watch.INDEX).setId("_name") - .setSource(watchSource) - .get(); + client().prepareIndex().setIndex(Watch.INDEX).setId("_name").setSource(watchSource).get(); fail(); } catch (Exception e) { // The watch index template the mapping is defined as strict @@ -177,22 +177,21 @@ public void testModifyWatches() throws Exception { createIndex("idx"); WatcherSearchTemplateRequest searchRequest = templateRequest(searchSource().query(matchAllQuery()), "idx"); - WatchSourceBuilder source = watchBuilder() - .trigger(schedule(interval("5s"))) - .input(searchInput(searchRequest)) - .addAction("_id", indexAction("idx")); + WatchSourceBuilder source = watchBuilder().trigger(schedule(interval("5s"))) + .input(searchInput(searchRequest)) + .addAction("_id", indexAction("idx")); new PutWatchRequestBuilder(client()).setId("_name") - .setSource(source.condition(new CompareCondition("ctx.payload.hits.total", CompareCondition.Op.EQ, 1L))) - .get(); + .setSource(source.condition(new CompareCondition("ctx.payload.hits.total", CompareCondition.Op.EQ, 1L))) + .get(); timeWarp().clock().fastForwardSeconds(5); timeWarp().trigger("_name"); assertWatchWithMinimumPerformedActionsCount("_name", 0, false); new PutWatchRequestBuilder(client()).setId("_name") - .setSource(source.condition(new CompareCondition("ctx.payload.hits.total", CompareCondition.Op.EQ, 0L))) - .get(); + .setSource(source.condition(new CompareCondition("ctx.payload.hits.total", CompareCondition.Op.EQ, 0L))) + .get(); timeWarp().clock().fastForwardSeconds(5); timeWarp().trigger("_name"); @@ -200,10 +199,11 @@ public void testModifyWatches() throws Exception { assertWatchWithMinimumPerformedActionsCount("_name", 1, false); new PutWatchRequestBuilder(client()).setId("_name") - .setSource(source - .trigger(schedule(Schedules.cron("0/1 * * * * ? 2020"))) - .condition(new CompareCondition("ctx.payload.hits.total", CompareCondition.Op.EQ, 0L))) - .get(); + .setSource( + source.trigger(schedule(Schedules.cron("0/1 * * * * ? 2020"))) + .condition(new CompareCondition("ctx.payload.hits.total", CompareCondition.Op.EQ, 0L)) + ) + .get(); timeWarp().clock().fastForwardSeconds(5); timeWarp().trigger("_name"); @@ -221,19 +221,33 @@ public void testConditionSearchWithSource() throws Exception { public void testConditionSearchWithIndexedTemplate() throws Exception { SearchSourceBuilder searchSourceBuilder = searchSource().query(matchQuery("level", "a")); - assertAcked(client().admin().cluster().preparePutStoredScript() + assertAcked( + client().admin() + .cluster() + .preparePutStoredScript() .setId("my-template") - .setContent(BytesReference.bytes( - jsonBuilder().startObject().startObject("script") - .field("lang", "mustache") - .field("source").value(searchSourceBuilder) - .endObject().endObject()), - XContentType.JSON) - .get()); + .setContent( + BytesReference.bytes( + jsonBuilder().startObject() + .startObject("script") + .field("lang", "mustache") + .field("source") + .value(searchSourceBuilder) + .endObject() + .endObject() + ), + XContentType.JSON + ) + .get() + ); Script template = new Script(ScriptType.STORED, null, "my-template", Collections.emptyMap()); - WatcherSearchTemplateRequest searchRequest = new WatcherSearchTemplateRequest(new String[]{"events"}, - SearchType.DEFAULT, WatcherSearchTemplateRequest.DEFAULT_INDICES_OPTIONS, template); + WatcherSearchTemplateRequest searchRequest = new WatcherSearchTemplateRequest( + new String[] { "events" }, + SearchType.DEFAULT, + WatcherSearchTemplateRequest.DEFAULT_INDICES_OPTIONS, + template + ); testConditionSearch(searchRequest); } @@ -244,18 +258,20 @@ public void testInputFiltering() throws Exception { refresh(); WatcherSearchTemplateRequest request = templateRequest(searchSource().query(termQuery("field", "foovalue")), "idx"); new PutWatchRequestBuilder(client()).setId("_name1") - .setSource(watchBuilder() - .trigger(schedule(interval(5, IntervalSchedule.Interval.Unit.SECONDS))) - .input(searchInput(request).extractKeys("hits.total.value")) - .condition(new CompareCondition("ctx.payload.hits.total", CompareCondition.Op.EQ, 1L))) - .get(); + .setSource( + watchBuilder().trigger(schedule(interval(5, IntervalSchedule.Interval.Unit.SECONDS))) + .input(searchInput(request).extractKeys("hits.total.value")) + .condition(new CompareCondition("ctx.payload.hits.total", CompareCondition.Op.EQ, 1L)) + ) + .get(); // in this watcher the condition will fail, because max_score isn't extracted, only total: new PutWatchRequestBuilder(client()).setId("_name2") - .setSource(watchBuilder() - .trigger(schedule(interval(5, IntervalSchedule.Interval.Unit.SECONDS))) - .input(searchInput(request).extractKeys("hits.total.value")) - .condition(new CompareCondition("ctx.payload.hits.max_score", CompareCondition.Op.GTE, 0L))) - .get(); + .setSource( + watchBuilder().trigger(schedule(interval(5, IntervalSchedule.Interval.Unit.SECONDS))) + .input(searchInput(request).extractKeys("hits.total.value")) + .condition(new CompareCondition("ctx.payload.hits.max_score", CompareCondition.Op.GTE, 0L)) + ) + .get(); timeWarp().trigger("_name1"); assertWatchWithMinimumPerformedActionsCount("_name1", 1); @@ -273,12 +289,13 @@ public void testInputFiltering() throws Exception { public void testPutWatchWithNegativeSchedule() throws Exception { try { new PutWatchRequestBuilder(client()).setId("_name") - .setSource(watchBuilder() - .trigger(schedule(interval(-5, IntervalSchedule.Interval.Unit.SECONDS))) - .input(simpleInput("key", "value")) - .condition(InternalAlwaysCondition.INSTANCE) - .addAction("_logger", loggingAction("executed!"))) - .get(); + .setSource( + watchBuilder().trigger(schedule(interval(-5, IntervalSchedule.Interval.Unit.SECONDS))) + .input(simpleInput("key", "value")) + .condition(InternalAlwaysCondition.INSTANCE) + .addAction("_logger", loggingAction("executed!")) + ) + .get(); fail("put watch should have failed"); } catch (IllegalArgumentException e) { assertThat(e.getMessage(), equalTo("interval can't be lower than 1000 ms, but [-5s] was specified")); @@ -286,12 +303,13 @@ public void testPutWatchWithNegativeSchedule() throws Exception { try { new PutWatchRequestBuilder(client()).setId("_name") - .setSource(watchBuilder() - .trigger(schedule(hourly().minutes(-10).build())) - .input(simpleInput("key", "value")) - .condition(InternalAlwaysCondition.INSTANCE) - .addAction("_logger", loggingAction("executed!"))) - .get(); + .setSource( + watchBuilder().trigger(schedule(hourly().minutes(-10).build())) + .input(simpleInput("key", "value")) + .condition(InternalAlwaysCondition.INSTANCE) + .addAction("_logger", loggingAction("executed!")) + ) + .get(); fail("put watch should have failed"); } catch (IllegalArgumentException e) { assertThat(e.getMessage(), equalTo("invalid hourly minute [-10]. minute must be between 0 and 59 incl.")); @@ -299,44 +317,53 @@ public void testPutWatchWithNegativeSchedule() throws Exception { try { new PutWatchRequestBuilder(client()).setId("_name") - .setSource(watchBuilder() - .trigger(schedule(daily().atRoundHour(-10).build())) - .input(simpleInput("key", "value")) - .condition(InternalAlwaysCondition.INSTANCE) - .addAction("_logger", loggingAction("executed!"))) - .get(); + .setSource( + watchBuilder().trigger(schedule(daily().atRoundHour(-10).build())) + .input(simpleInput("key", "value")) + .condition(InternalAlwaysCondition.INSTANCE) + .addAction("_logger", loggingAction("executed!")) + ) + .get(); fail("put watch should have failed"); } catch (IllegalArgumentException e) { - assertThat(e.getMessage(), - equalTo("invalid time [0-10:00]. invalid time hour value [-10]. time hours must be between 0 and 23 incl.")); + assertThat( + e.getMessage(), + equalTo("invalid time [0-10:00]. invalid time hour value [-10]. time hours must be between 0 and 23 incl.") + ); } try { new PutWatchRequestBuilder(client()).setId("_name") - .setSource(watchBuilder() - .trigger(schedule(weekly().time(WeekTimes.builder().atRoundHour(-10).build()).build())) - .input(simpleInput("key", "value")) - .condition(InternalAlwaysCondition.INSTANCE) - .addAction("_logger", loggingAction("executed!"))) - .get(); + .setSource( + watchBuilder().trigger(schedule(weekly().time(WeekTimes.builder().atRoundHour(-10).build()).build())) + .input(simpleInput("key", "value")) + .condition(InternalAlwaysCondition.INSTANCE) + .addAction("_logger", loggingAction("executed!")) + ) + .get(); fail("put watch should have failed"); } catch (IllegalArgumentException e) { - assertThat(e.getMessage(), - equalTo("invalid time [0-10:00]. invalid time hour value [-10]. time hours must be between 0 and 23 incl.")); + assertThat( + e.getMessage(), + equalTo("invalid time [0-10:00]. invalid time hour value [-10]. time hours must be between 0 and 23 incl.") + ); } try { new PutWatchRequestBuilder(client()).setId("_name") - .setSource(watchBuilder() - .trigger(schedule(monthly().time(MonthTimes.builder().atRoundHour(-10).build()).build())) - .input(simpleInput("key", "value")) - .condition(InternalAlwaysCondition.INSTANCE) - .addAction("_logger", loggingAction("executed!"))) - .get(); + .setSource( + watchBuilder().trigger(schedule(monthly().time(MonthTimes.builder().atRoundHour(-10).build()).build())) + .input(simpleInput("key", "value")) + .condition(InternalAlwaysCondition.INSTANCE) + .addAction("_logger", loggingAction("executed!")) + ) + .get(); fail("put watch should have failed"); } catch (IllegalArgumentException e) { - assertThat(e.getMessage(), - equalTo("invalid time [0-10:00]. invalid time hour value [-10]. time hours must be between 0 and 23 incl.")); + assertThat( + e.getMessage(), + equalTo("invalid time [0-10:00]. invalid time hour value [-10]. time hours must be between 0 and 23 incl.") + ); } } @@ -348,37 +375,30 @@ private void testConditionSearch(WatcherSearchTemplateRequest request) throws Ex assertAcked(prepareCreate("events").setMapping("level", "type=text")); new PutWatchRequestBuilder(client()).setId(watchName) - .setSource(watchBuilder() - .trigger(schedule(interval("5s"))) - .input(searchInput(request)) - .condition(new CompareCondition("ctx.payload.hits.total", CompareCondition.Op.GTE, 3L))) - .get(); + .setSource( + watchBuilder().trigger(schedule(interval("5s"))) + .input(searchInput(request)) + .condition(new CompareCondition("ctx.payload.hits.total", CompareCondition.Op.GTE, 3L)) + ) + .get(); logger.info("created watch [{}] at [{}]", watchName, ZonedDateTime.now(Clock.systemUTC())); - client().prepareIndex("events") - .setSource("level", "a") - .get(); - client().prepareIndex("events") - .setSource("level", "a") - .get(); + client().prepareIndex("events").setSource("level", "a").get(); + client().prepareIndex("events").setSource("level", "a").get(); refresh(); timeWarp().clock().fastForwardSeconds(1); timeWarp().trigger(watchName); assertWatchWithNoActionNeeded(watchName, 1); - client().prepareIndex("events") - .setSource("level", "b") - .get(); + client().prepareIndex("events").setSource("level", "b").get(); refresh(); timeWarp().clock().fastForwardSeconds(1); timeWarp().trigger(watchName); assertWatchWithNoActionNeeded(watchName, 2); - client().prepareIndex("events") - .setSource("level", "a") - .get(); + client().prepareIndex("events").setSource("level", "a").get(); refresh(); timeWarp().clock().fastForwardSeconds(1); timeWarp().trigger(watchName); @@ -389,17 +409,23 @@ public void testQueryWatches() { int numWatches = 6; for (int i = 0; i < numWatches; i++) { PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client()).setId("" + i) - .setSource(watchBuilder() - .trigger(schedule(interval(1, IntervalSchedule.Interval.Unit.DAYS))) - .addAction("_logger", loggingAction("log me")) - .metadata(Map.of("key1", i, "key2", numWatches - i))) + .setSource( + watchBuilder().trigger(schedule(interval(1, IntervalSchedule.Interval.Unit.DAYS))) + .addAction("_logger", loggingAction("log me")) + .metadata(Map.of("key1", i, "key2", numWatches - i)) + ) .get(); assertThat(putWatchResponse.isCreated(), is(true)); } refresh(); - QueryWatchesAction.Request request = - new QueryWatchesAction.Request(0, 2, null, List.of(new FieldSortBuilder("metadata.key1")), null); + QueryWatchesAction.Request request = new QueryWatchesAction.Request( + 0, + 2, + null, + List.of(new FieldSortBuilder("metadata.key1")), + null + ); QueryWatchesAction.Response response = client().execute(QueryWatchesAction.INSTANCE, request).actionGet(); assertThat(response.getWatchTotalCount(), equalTo((long) numWatches)); assertThat(response.getWatches().size(), equalTo(2)); @@ -464,41 +490,62 @@ public void testQueryWatchesSearchAfter() { int numWatches = 6; for (int i = 0; i < numWatches; i++) { PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client()).setId("" + i) - .setSource(watchBuilder() - .trigger(schedule(interval(1, IntervalSchedule.Interval.Unit.DAYS))) - .addAction("_logger", loggingAction("log me")) - .metadata(Map.of("_id", i))) + .setSource( + watchBuilder().trigger(schedule(interval(1, IntervalSchedule.Interval.Unit.DAYS))) + .addAction("_logger", loggingAction("log me")) + .metadata(Map.of("_id", i)) + ) .get(); assertThat(putWatchResponse.isCreated(), is(true)); } refresh(); - QueryWatchesAction.Request request = - new QueryWatchesAction.Request(0, 2, null, List.of(new FieldSortBuilder("metadata._id")), null); + QueryWatchesAction.Request request = new QueryWatchesAction.Request( + 0, + 2, + null, + List.of(new FieldSortBuilder("metadata._id")), + null + ); QueryWatchesAction.Response response = client().execute(QueryWatchesAction.INSTANCE, request).actionGet(); assertThat(response.getWatchTotalCount(), equalTo((long) numWatches)); assertThat(response.getWatches().size(), equalTo(2)); assertThat(response.getWatches().get(0).getId(), equalTo("0")); assertThat(response.getWatches().get(1).getId(), equalTo("1")); - request = new QueryWatchesAction.Request(0, 2, null, List.of(new FieldSortBuilder("metadata._id")), - new SearchAfterBuilder().setSortValues(new Object[]{"1"})); + request = new QueryWatchesAction.Request( + 0, + 2, + null, + List.of(new FieldSortBuilder("metadata._id")), + new SearchAfterBuilder().setSortValues(new Object[] { "1" }) + ); response = client().execute(QueryWatchesAction.INSTANCE, request).actionGet(); assertThat(response.getWatchTotalCount(), equalTo((long) numWatches)); assertThat(response.getWatches().size(), equalTo(2)); assertThat(response.getWatches().get(0).getId(), equalTo("2")); assertThat(response.getWatches().get(1).getId(), equalTo("3")); - request = new QueryWatchesAction.Request(0, 2, null, List.of(new FieldSortBuilder("metadata._id")), - new SearchAfterBuilder().setSortValues(new Object[]{"3"})); + request = new QueryWatchesAction.Request( + 0, + 2, + null, + List.of(new FieldSortBuilder("metadata._id")), + new SearchAfterBuilder().setSortValues(new Object[] { "3" }) + ); response = client().execute(QueryWatchesAction.INSTANCE, request).actionGet(); assertThat(response.getWatchTotalCount(), equalTo((long) numWatches)); assertThat(response.getWatches().size(), equalTo(2)); assertThat(response.getWatches().get(0).getId(), equalTo("4")); assertThat(response.getWatches().get(1).getId(), equalTo("5")); - request = new QueryWatchesAction.Request(0, 2, null, List.of(new FieldSortBuilder("metadata._id")), - new SearchAfterBuilder().setSortValues(new Object[]{"5"})); + request = new QueryWatchesAction.Request( + 0, + 2, + null, + List.of(new FieldSortBuilder("metadata._id")), + new SearchAfterBuilder().setSortValues(new Object[] { "5" }) + ); response = client().execute(QueryWatchesAction.INSTANCE, request).actionGet(); assertThat(response.getWatchTotalCount(), equalTo((long) numWatches)); assertThat(response.getWatches().size(), equalTo(0)); diff --git a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/test/integration/BootStrapTests.java b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/test/integration/BootStrapTests.java index a8e16d3e0fd3d..9d4dc9efcfc28 100644 --- a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/test/integration/BootStrapTests.java +++ b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/test/integration/BootStrapTests.java @@ -41,12 +41,12 @@ import java.util.concurrent.TimeUnit; import static org.elasticsearch.action.support.WriteRequest.RefreshPolicy.IMMEDIATE; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.index.query.QueryBuilders.termQuery; import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.xpack.watcher.actions.ActionBuilders.indexAction; import static org.elasticsearch.xpack.watcher.client.WatchSourceBuilders.watchBuilder; import static org.elasticsearch.xpack.watcher.input.InputBuilders.searchInput; @@ -65,80 +65,101 @@ protected boolean timeWarped() { } public void testLoadMalformedWatchRecord() throws Exception { - client().prepareIndex().setIndex(Watch.INDEX).setId("_id") - .setSource(jsonBuilder().startObject() - .startObject(WatchField.TRIGGER.getPreferredName()) - .startObject("schedule") - .field("cron", "0/5 * * * * ? 2050") - .endObject() - .endObject() - .startObject(WatchField.ACTIONS.getPreferredName()) - .endObject() - .endObject()) - .get(); + client().prepareIndex() + .setIndex(Watch.INDEX) + .setId("_id") + .setSource( + jsonBuilder().startObject() + .startObject(WatchField.TRIGGER.getPreferredName()) + .startObject("schedule") + .field("cron", "0/5 * * * * ? 2050") + .endObject() + .endObject() + .startObject(WatchField.ACTIONS.getPreferredName()) + .endObject() + .endObject() + ) + .get(); // valid watch record: ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC); Wid wid = new Wid("_id", now); ScheduleTriggerEvent event = new ScheduleTriggerEvent("_id", now, now); ExecutableCondition condition = InternalAlwaysCondition.INSTANCE; - client().prepareIndex().setIndex(HistoryStoreField.DATA_STREAM).setId(wid.value()) - .setOpType(DocWriteRequest.OpType.CREATE) - .setSource(jsonBuilder().startObject() - .field("@timestamp", ZonedDateTime.now()) - .startObject(WatchRecord.TRIGGER_EVENT.getPreferredName()) - .field(event.type(), event) - .endObject() - .startObject(WatchField.CONDITION.getPreferredName()) - .field(condition.type(), condition) - .endObject() - .startObject(WatchField.INPUT.getPreferredName()) - .startObject("none").endObject() - .endObject() - .endObject()) - .setWaitForActiveShards(ActiveShardCount.ALL) - .setRefreshPolicy(IMMEDIATE) - .get(); + client().prepareIndex() + .setIndex(HistoryStoreField.DATA_STREAM) + .setId(wid.value()) + .setOpType(DocWriteRequest.OpType.CREATE) + .setSource( + jsonBuilder().startObject() + .field("@timestamp", ZonedDateTime.now()) + .startObject(WatchRecord.TRIGGER_EVENT.getPreferredName()) + .field(event.type(), event) + .endObject() + .startObject(WatchField.CONDITION.getPreferredName()) + .field(condition.type(), condition) + .endObject() + .startObject(WatchField.INPUT.getPreferredName()) + .startObject("none") + .endObject() + .endObject() + .endObject() + ) + .setWaitForActiveShards(ActiveShardCount.ALL) + .setRefreshPolicy(IMMEDIATE) + .get(); // unknown condition: wid = new Wid("_id", now); - client().prepareIndex().setIndex(HistoryStoreField.DATA_STREAM).setId(wid.value()) - .setOpType(DocWriteRequest.OpType.CREATE) - .setSource(jsonBuilder().startObject() - .field("@timestamp", ZonedDateTime.now()) - .startObject(WatchRecord.TRIGGER_EVENT.getPreferredName()) - .field(event.type(), event) - .endObject() - .startObject(WatchField.CONDITION.getPreferredName()) - .startObject("unknown").endObject() - .endObject() - .startObject(WatchField.INPUT.getPreferredName()) - .startObject("none").endObject() - .endObject() - .endObject()) - .setWaitForActiveShards(ActiveShardCount.ALL) - .setRefreshPolicy(IMMEDIATE) - .get(); + client().prepareIndex() + .setIndex(HistoryStoreField.DATA_STREAM) + .setId(wid.value()) + .setOpType(DocWriteRequest.OpType.CREATE) + .setSource( + jsonBuilder().startObject() + .field("@timestamp", ZonedDateTime.now()) + .startObject(WatchRecord.TRIGGER_EVENT.getPreferredName()) + .field(event.type(), event) + .endObject() + .startObject(WatchField.CONDITION.getPreferredName()) + .startObject("unknown") + .endObject() + .endObject() + .startObject(WatchField.INPUT.getPreferredName()) + .startObject("none") + .endObject() + .endObject() + .endObject() + ) + .setWaitForActiveShards(ActiveShardCount.ALL) + .setRefreshPolicy(IMMEDIATE) + .get(); // unknown trigger: wid = new Wid("_id", now); - client().prepareIndex().setIndex(HistoryStoreField.DATA_STREAM).setId(wid.value()) - .setOpType(DocWriteRequest.OpType.CREATE) - .setSource(jsonBuilder().startObject() - .field("@timestamp", ZonedDateTime.now()) - .startObject(WatchRecord.TRIGGER_EVENT.getPreferredName()) - .startObject("unknown").endObject() - .endObject() - .startObject(WatchField.CONDITION.getPreferredName()) - .field(condition.type(), condition) - .endObject() - .startObject(WatchField.INPUT.getPreferredName()) - .startObject("none").endObject() - .endObject() - .endObject()) - .setWaitForActiveShards(ActiveShardCount.ALL) - .setRefreshPolicy(IMMEDIATE) - .get(); + client().prepareIndex() + .setIndex(HistoryStoreField.DATA_STREAM) + .setId(wid.value()) + .setOpType(DocWriteRequest.OpType.CREATE) + .setSource( + jsonBuilder().startObject() + .field("@timestamp", ZonedDateTime.now()) + .startObject(WatchRecord.TRIGGER_EVENT.getPreferredName()) + .startObject("unknown") + .endObject() + .endObject() + .startObject(WatchField.CONDITION.getPreferredName()) + .field(condition.type(), condition) + .endObject() + .startObject(WatchField.INPUT.getPreferredName()) + .startObject("none") + .endObject() + .endObject() + .endObject() + ) + .setWaitForActiveShards(ActiveShardCount.ALL) + .setRefreshPolicy(IMMEDIATE) + .get(); stopWatcher(); startWatcher(); @@ -153,20 +174,23 @@ public void testLoadExistingWatchesUponStartup() throws Exception { stopWatcher(); int numWatches = scaledRandomIntBetween(16, 128); - WatcherSearchTemplateRequest request = - templateRequest(searchSource().query(termQuery("field", "value")), "my-index"); + WatcherSearchTemplateRequest request = templateRequest(searchSource().query(termQuery("field", "value")), "my-index"); BulkRequestBuilder bulkRequestBuilder = client().prepareBulk(); for (int i = 0; i < numWatches; i++) { bulkRequestBuilder.add( - client().prepareIndex().setIndex(Watch.INDEX).setId("_id" + i) - .setSource(watchBuilder() - .trigger(schedule(cron("0 0/5 * * * ? 2050"))) - .input(searchInput(request)) - .condition(new CompareCondition("ctx.payload.hits.total.value", CompareCondition.Op.EQ, 1L)) - .buildAsBytes(XContentType.JSON), XContentType.JSON - ) - .setWaitForActiveShards(ActiveShardCount.ALL)); + client().prepareIndex() + .setIndex(Watch.INDEX) + .setId("_id" + i) + .setSource( + watchBuilder().trigger(schedule(cron("0 0/5 * * * ? 2050"))) + .input(searchInput(request)) + .condition(new CompareCondition("ctx.payload.hits.total.value", CompareCondition.Op.EQ, 1L)) + .buildAsBytes(XContentType.JSON), + XContentType.JSON + ) + .setWaitForActiveShards(ActiveShardCount.ALL) + ); } bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).get(); assertHitCount(client().prepareSearch(Watch.INDEX).setSize(0).get(), numWatches); @@ -181,9 +205,12 @@ public void testLoadExistingWatchesUponStartup() throws Exception { public void testMixedTriggeredWatchLoading() throws Exception { createIndex("output"); - client().prepareIndex().setIndex("my-index").setId("bar") - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .setSource("field", "value").get(); + client().prepareIndex() + .setIndex("my-index") + .setId("bar") + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + .setSource("field", "value") + .get(); WatcherStatsResponse response = new WatcherStatsRequestBuilder(client()).get(); assertThat(response.getWatchesCount(), equalTo(0L)); @@ -194,13 +221,15 @@ public void testMixedTriggeredWatchLoading() throws Exception { int numWatches = 8; for (int i = 0; i < numWatches; i++) { String watchId = "_id" + i; - new PutWatchRequestBuilder(client()).setId(watchId).setSource(watchBuilder() - .trigger(schedule(cron("0/5 * * * * ? 2050"))) - .input(searchInput(request)) - .condition(InternalAlwaysCondition.INSTANCE) - .addAction("_id", indexAction("output")) - .defaultThrottlePeriod(TimeValue.timeValueMillis(0)) - ).get(); + new PutWatchRequestBuilder(client()).setId(watchId) + .setSource( + watchBuilder().trigger(schedule(cron("0/5 * * * * ? 2050"))) + .input(searchInput(request)) + .condition(InternalAlwaysCondition.INSTANCE) + .addAction("_id", indexAction("output")) + .defaultThrottlePeriod(TimeValue.timeValueMillis(0)) + ) + .get(); } stopWatcher(); @@ -215,11 +244,12 @@ public void testMixedTriggeredWatchLoading() throws Exception { Wid wid = new Wid(watchId, now); TriggeredWatch triggeredWatch = new TriggeredWatch(wid, event); bulkRequestBuilder.add( - client().prepareIndex() - .setIndex(TriggeredWatchStoreField.INDEX_NAME) - .setId(triggeredWatch.id().value()) - .setSource(jsonBuilder().value(triggeredWatch)) - .request()); + client().prepareIndex() + .setIndex(TriggeredWatchStoreField.INDEX_NAME) + .setId(triggeredWatch.id().value()) + .setSource(jsonBuilder().value(triggeredWatch)) + .request() + ); } bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).get(); @@ -230,22 +260,27 @@ public void testMixedTriggeredWatchLoading() throws Exception { public void testTriggeredWatchLoading() throws Exception { createIndex("output"); - client().prepareIndex().setIndex("my-index").setId("bar") - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .setSource("field", "value").get(); + client().prepareIndex() + .setIndex("my-index") + .setId("bar") + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + .setSource("field", "value") + .get(); WatcherStatsResponse response = new WatcherStatsRequestBuilder(client()).get(); assertThat(response.getWatchesCount(), equalTo(0L)); String watchId = "_id"; WatcherSearchTemplateRequest request = templateRequest(searchSource().query(termQuery("field", "value")), "my-index"); - new PutWatchRequestBuilder(client()).setId(watchId).setSource(watchBuilder() - .trigger(schedule(cron("0/5 * * * * ? 2050"))) - .input(searchInput(request)) - .condition(InternalAlwaysCondition.INSTANCE) - .addAction("_id", indexAction("output")) - .defaultThrottlePeriod(TimeValue.timeValueMillis(0)) - ).get(); + new PutWatchRequestBuilder(client()).setId(watchId) + .setSource( + watchBuilder().trigger(schedule(cron("0/5 * * * * ? 2050"))) + .input(searchInput(request)) + .condition(InternalAlwaysCondition.INSTANCE) + .addAction("_id", indexAction("output")) + .defaultThrottlePeriod(TimeValue.timeValueMillis(0)) + ) + .get(); stopWatcher(); @@ -257,8 +292,10 @@ public void testTriggeredWatchLoading() throws Exception { ScheduleTriggerEvent event = new ScheduleTriggerEvent(watchId, now, now); Wid wid = new Wid(watchId, now); TriggeredWatch triggeredWatch = new TriggeredWatch(wid, event); - bulkRequestBuilder.add(client() - .prepareIndex().setIndex(TriggeredWatchStoreField.INDEX_NAME).setId(triggeredWatch.id().value()) + bulkRequestBuilder.add( + client().prepareIndex() + .setIndex(TriggeredWatchStoreField.INDEX_NAME) + .setId(triggeredWatch.id().value()) .setSource(jsonBuilder().value(triggeredWatch)) .setWaitForActiveShards(ActiveShardCount.ALL) ); @@ -270,8 +307,8 @@ public void testTriggeredWatchLoading() throws Exception { assertSingleExecutionAndCompleteWatchHistory(1, numRecords); } - private void assertSingleExecutionAndCompleteWatchHistory(final long numberOfWatches, - final int expectedWatchHistoryCount) throws Exception { + private void assertSingleExecutionAndCompleteWatchHistory(final long numberOfWatches, final int expectedWatchHistoryCount) + throws Exception { assertBusy(() -> { // We need to wait until all the records are processed from the internal execution queue, only then we can assert // that numRecords watch records have been processed as part of starting up. @@ -288,10 +325,14 @@ private void assertSingleExecutionAndCompleteWatchHistory(final long numberOfWat SearchResponse historySearchResponse = client().prepareSearch(HistoryStoreField.INDEX_PREFIX + "*").setSize(10000).get(); assertHitCount(historySearchResponse, expectedWatchHistoryCount); long notExecutedCount = Arrays.stream(historySearchResponse.getHits().getHits()) - .filter(hit -> hit.getSourceAsMap().get("state").equals(ExecutionState.NOT_EXECUTED_ALREADY_QUEUED.id())) - .count(); - logger.info("Watches not executed: [{}]: expected watch history count [{}] - [{}] successful watch exections", - notExecutedCount, expectedWatchHistoryCount, successfulWatchExecutions); + .filter(hit -> hit.getSourceAsMap().get("state").equals(ExecutionState.NOT_EXECUTED_ALREADY_QUEUED.id())) + .count(); + logger.info( + "Watches not executed: [{}]: expected watch history count [{}] - [{}] successful watch exections", + notExecutedCount, + expectedWatchHistoryCount, + successfulWatchExecutions + ); assertThat(notExecutedCount, is(expectedWatchHistoryCount - successfulWatchExecutions)); }, 20, TimeUnit.SECONDS); } @@ -316,7 +357,7 @@ public void testWatchRecordSavedTwice() throws Exception { assertAcked(client().admin().indices().prepareCreate(Watch.INDEX)); } LocalDateTime localDateTime = LocalDateTime.of(2015, 11, 5, 0, 0, 0, 0); - ZonedDateTime triggeredTime = ZonedDateTime.of(localDateTime,ZoneOffset.UTC); + ZonedDateTime triggeredTime = ZonedDateTime.of(localDateTime, ZoneOffset.UTC); logger.info("Stopping watcher"); stopWatcher(); @@ -329,14 +370,20 @@ public void testWatchRecordSavedTwice() throws Exception { Wid wid = new Wid(watchId, triggeredTime); TriggeredWatch triggeredWatch = new TriggeredWatch(wid, event); bulkRequestBuilder.add( - client().prepareIndex().setIndex(TriggeredWatchStoreField.INDEX_NAME) - .setId(triggeredWatch.id().value()).setSource(jsonBuilder().value(triggeredWatch)) + client().prepareIndex() + .setIndex(TriggeredWatchStoreField.INDEX_NAME) + .setId(triggeredWatch.id().value()) + .setSource(jsonBuilder().value(triggeredWatch)) ); String id = internalCluster().getInstance(ClusterService.class).localNode().getId(); WatchRecord watchRecord = new WatchRecord.MessageWatchRecord(wid, event, ExecutionState.EXECUTED, "executed", id); - bulkRequestBuilder.add(client().prepareIndex().setIndex(HistoryStoreField.DATA_STREAM).setId(watchRecord.id().value()) - .setOpType(DocWriteRequest.OpType.CREATE).setSource(jsonBuilder().value(watchRecord)) + bulkRequestBuilder.add( + client().prepareIndex() + .setIndex(HistoryStoreField.DATA_STREAM) + .setId(watchRecord.id().value()) + .setOpType(DocWriteRequest.OpType.CREATE) + .setSource(jsonBuilder().value(watchRecord)) ); } assertNoFailures(bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).get()); @@ -357,8 +404,7 @@ public void testWatchRecordSavedTwice() throws Exception { SearchResponse searchResponse = client().prepareSearch(HistoryStoreField.DATA_STREAM).setSize(numRecords).get(); assertThat(searchResponse.getHits().getTotalHits().value, Matchers.equalTo((long) numRecords)); for (int i = 0; i < numRecords; i++) { - assertThat(searchResponse.getHits().getAt(i).getSourceAsMap().get("state"), - is(ExecutionState.EXECUTED.id())); + assertThat(searchResponse.getHits().getAt(i).getSourceAsMap().get("state"), is(ExecutionState.EXECUTED.id())); } }); } diff --git a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/test/integration/ExecutionVarsIntegrationTests.java b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/test/integration/ExecutionVarsIntegrationTests.java index 9bade2476fcd6..7e1ba6701c500 100644 --- a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/test/integration/ExecutionVarsIntegrationTests.java +++ b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/test/integration/ExecutionVarsIntegrationTests.java @@ -7,10 +7,10 @@ package org.elasticsearch.xpack.watcher.test.integration; import org.elasticsearch.action.search.SearchResponse; -import org.elasticsearch.xcontent.ObjectPath; import org.elasticsearch.common.xcontent.support.XContentMapValues; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.protocol.xpack.watcher.PutWatchResponse; +import org.elasticsearch.xcontent.ObjectPath; import org.elasticsearch.xpack.core.watcher.support.xcontent.XContentSource; import org.elasticsearch.xpack.core.watcher.transport.actions.execute.ExecuteWatchRequestBuilder; import org.elasticsearch.xpack.core.watcher.transport.actions.execute.ExecuteWatchResponse; @@ -72,9 +72,9 @@ protected Map, Object>> pluginScripts() { }); // Transforms the value of a1, equivalent to: - // ctx.vars.a1_transform_value = ctx.vars.watch_transform_value + 10; - // ctx.payload.a1_transformed_value = ctx.vars.a1_transform_value; - // return ctx.payload; + // ctx.vars.a1_transform_value = ctx.vars.watch_transform_value + 10; + // ctx.payload.a1_transformed_value = ctx.vars.a1_transform_value; + // return ctx.payload; scripts.put("transform a1", vars -> { Map ctxVars = (Map) XContentMapValues.extractValue("ctx.vars", vars); Map ctxPayload = (Map) XContentMapValues.extractValue("ctx.payload", vars); @@ -89,9 +89,9 @@ protected Map, Object>> pluginScripts() { }); // Transforms the value of a2, equivalent to: - // ctx.vars.a2_transform_value = ctx.vars.watch_transform_value + 20; - // ctx.payload.a2_transformed_value = ctx.vars.a2_transform_value; - // return ctx.payload; + // ctx.vars.a2_transform_value = ctx.vars.watch_transform_value + 20; + // ctx.payload.a2_transformed_value = ctx.vars.a2_transform_value; + // return ctx.payload; scripts.put("transform a2", vars -> { Map ctxVars = (Map) XContentMapValues.extractValue("ctx.vars", vars); Map ctxPayload = (Map) XContentMapValues.extractValue("ctx.payload", vars); @@ -111,22 +111,22 @@ protected Map, Object>> pluginScripts() { @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/67908") public void testVars() throws Exception { - PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client()).setId(watchId).setSource(watchBuilder() - .trigger(schedule(cron("0/1 * * * * ?"))) - .input(simpleInput("value", 5)) - .condition(new ScriptCondition( - mockScript("ctx.vars.condition_value = ctx.payload.value + 5; return ctx.vars.condition_value > 5;"))) - .transform( - scriptTransform(mockScript("ctx.vars.watch_transform_value = ctx.vars.condition_value + 5; return ctx.payload;"))) - .addAction( - "a1", - scriptTransform(mockScript("transform a1")), - loggingAction("_text")) - .addAction( - "a2", - scriptTransform(mockScript("transform a2")), - loggingAction("_text"))) - .get(); + PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client()).setId(watchId) + .setSource( + watchBuilder().trigger(schedule(cron("0/1 * * * * ?"))) + .input(simpleInput("value", 5)) + .condition( + new ScriptCondition( + mockScript("ctx.vars.condition_value = ctx.payload.value + 5; return ctx.vars.condition_value > 5;") + ) + ) + .transform( + scriptTransform(mockScript("ctx.vars.watch_transform_value = ctx.vars.condition_value + 5; return ctx.payload;")) + ) + .addAction("a1", scriptTransform(mockScript("transform a1")), loggingAction("_text")) + .addAction("a2", scriptTransform(mockScript("transform a2")), loggingAction("_text")) + ) + .get(); assertThat(putWatchResponse.isCreated(), is(true)); @@ -173,31 +173,28 @@ public void testVars() throws Exception { } public void testVarsManual() throws Exception { - PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client()).setId(watchId).setSource(watchBuilder() - .trigger(schedule(cron("0/1 * * * * ? 2020"))) - .input(simpleInput("value", 5)) - .condition(new ScriptCondition( - mockScript("ctx.vars.condition_value = ctx.payload.value + 5; return ctx.vars.condition_value > 5;"))) - .transform( - scriptTransform(mockScript("ctx.vars.watch_transform_value = ctx.vars.condition_value + 5; return ctx.payload;"))) - .addAction( - "a1", - scriptTransform(mockScript("transform a1")), - loggingAction("_text")) - .addAction( - "a2", - scriptTransform(mockScript("transform a2")), - loggingAction("_text"))) - .get(); + PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client()).setId(watchId) + .setSource( + watchBuilder().trigger(schedule(cron("0/1 * * * * ? 2020"))) + .input(simpleInput("value", 5)) + .condition( + new ScriptCondition( + mockScript("ctx.vars.condition_value = ctx.payload.value + 5; return ctx.vars.condition_value > 5;") + ) + ) + .transform( + scriptTransform(mockScript("ctx.vars.watch_transform_value = ctx.vars.condition_value + 5; return ctx.payload;")) + ) + .addAction("a1", scriptTransform(mockScript("transform a1")), loggingAction("_text")) + .addAction("a2", scriptTransform(mockScript("transform a2")), loggingAction("_text")) + ) + .get(); assertThat(putWatchResponse.isCreated(), is(true)); boolean debug = randomBoolean(); - ExecuteWatchResponse executeWatchResponse = new ExecuteWatchRequestBuilder(client()) - .setId(watchId) - .setDebug(debug) - .get(); + ExecuteWatchResponse executeWatchResponse = new ExecuteWatchRequestBuilder(client()).setId(watchId).setDebug(debug).get(); assertThat(executeWatchResponse.getRecordId(), notNullValue()); XContentSource source = executeWatchResponse.getRecordSource(); diff --git a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/test/integration/HistoryIntegrationTests.java b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/test/integration/HistoryIntegrationTests.java index b77dc1d0e945e..d9eb2574e29ac 100644 --- a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/test/integration/HistoryIntegrationTests.java +++ b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/test/integration/HistoryIntegrationTests.java @@ -8,12 +8,12 @@ import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse; import org.elasticsearch.action.search.SearchResponse; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.protocol.xpack.watcher.PutWatchResponse; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.sort.SortBuilders; import org.elasticsearch.search.sort.SortOrder; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.watcher.actions.ActionStatus; import org.elasticsearch.xpack.core.watcher.client.WatchSourceBuilder; import org.elasticsearch.xpack.core.watcher.input.Input; @@ -29,11 +29,11 @@ import java.util.Locale; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.index.mapper.MapperService.SINGLE_MAPPING_NAME; import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery; import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.xpack.watcher.actions.ActionBuilders.loggingAction; import static org.elasticsearch.xpack.watcher.client.WatchSourceBuilders.watchBuilder; import static org.elasticsearch.xpack.watcher.input.InputBuilders.chainInput; @@ -49,17 +49,21 @@ public class HistoryIntegrationTests extends AbstractWatcherIntegrationTestCase // issue: https://github.com/elastic/x-plugins/issues/2338 public void testThatHistoryIsWrittenWithChainedInput() throws Exception { - XContentBuilder xContentBuilder = jsonBuilder().startObject().startObject("inner").field("date", "2015-06-06").endObject() - .endObject(); + XContentBuilder xContentBuilder = jsonBuilder().startObject() + .startObject("inner") + .field("date", "2015-06-06") + .endObject() + .endObject(); index("foo", "1", xContentBuilder); refresh(); - WatchSourceBuilder builder = watchBuilder() - .trigger(schedule(interval("10s"))) - .addAction("logging", loggingAction("foo")); + WatchSourceBuilder builder = watchBuilder().trigger(schedule(interval("10s"))).addAction("logging", loggingAction("foo")); - builder.input(chainInput().add("first", searchInput( - templateRequest(searchSource().sort(SortBuilders.fieldSort("inner.date").order(SortOrder.DESC)), "foo"))) + builder.input( + chainInput().add( + "first", + searchInput(templateRequest(searchSource().sort(SortBuilders.fieldSort("inner.date").order(SortOrder.DESC)), "foo")) + ) ); PutWatchResponse response = new PutWatchRequestBuilder(client()).setId("test_watch").setSource(builder).get(); @@ -74,14 +78,14 @@ public void testThatHistoryIsWrittenWithChainedInput() throws Exception { // See https://github.com/elastic/x-plugins/issues/2913 public void testFailedInputResultWithDotsInFieldNameGetsStored() throws Exception { - WatcherSearchTemplateRequest request = templateRequest(searchSource() - .query(matchAllQuery()) - .sort("trigger_event.triggered_time", SortOrder.DESC) - .size(1), "non-existing-index"); + WatcherSearchTemplateRequest request = templateRequest( + searchSource().query(matchAllQuery()).sort("trigger_event.triggered_time", SortOrder.DESC).size(1), + "non-existing-index" + ); // The result of the search input will be a failure, because a missing index does not exist when // the query is executed - @SuppressWarnings({"rawtypes"}) + @SuppressWarnings({ "rawtypes" }) Input.Builder input = searchInput(request); // wrapping this randomly into a chained input to test this as well boolean useChained = randomBoolean(); @@ -90,11 +94,12 @@ public void testFailedInputResultWithDotsInFieldNameGetsStored() throws Exceptio } new PutWatchRequestBuilder(client()).setId("test_watch") - .setSource(watchBuilder() - .trigger(schedule(interval(5, IntervalSchedule.Interval.Unit.HOURS))) - .input(input) - .addAction("_logger", loggingAction("#### randomLogging"))) - .get(); + .setSource( + watchBuilder().trigger(schedule(interval(5, IntervalSchedule.Interval.Unit.HOURS))) + .input(input) + .addAction("_logger", loggingAction("#### randomLogging")) + ) + .get(); new ExecuteWatchRequestBuilder(client()).setId("test_watch").setRecordExecution(true).get(); @@ -104,17 +109,16 @@ public void testFailedInputResultWithDotsInFieldNameGetsStored() throws Exceptio // as fields with dots are allowed in 5.0 again, the mapping must be checked in addition GetMappingsResponse response = client().admin().indices().prepareGetMappings(".watcher-history*").get(); - XContentSource source = new XContentSource( - response.getMappings().valuesIt().next().source().uncompressed(), XContentType.JSON); + XContentSource source = new XContentSource(response.getMappings().valuesIt().next().source().uncompressed(), XContentType.JSON); // lets make sure the body fields are disabled if (useChained) { - String chainedPath = SINGLE_MAPPING_NAME + - ".properties.result.properties.input.properties.chain.properties.chained.properties.search" + - ".properties.request.properties.body.enabled"; + String chainedPath = SINGLE_MAPPING_NAME + + ".properties.result.properties.input.properties.chain.properties.chained.properties.search" + + ".properties.request.properties.body.enabled"; assertThat(source.getValue(chainedPath), is(false)); } else { - String path = SINGLE_MAPPING_NAME + - ".properties.result.properties.input.properties.search.properties.request.properties.body.enabled"; + String path = SINGLE_MAPPING_NAME + + ".properties.result.properties.input.properties.search.properties.request.properties.body.enabled"; assertThat(source.getValue(path), is(false)); } } @@ -131,11 +135,12 @@ public void testPayloadInputWithDotsInFieldNameWorks() throws Exception { } new PutWatchRequestBuilder(client()).setId("test_watch") - .setSource(watchBuilder() - .trigger(schedule(interval(5, IntervalSchedule.Interval.Unit.HOURS))) - .input(input) - .addAction("_logger", loggingAction("#### randomLogging"))) - .get(); + .setSource( + watchBuilder().trigger(schedule(interval(5, IntervalSchedule.Interval.Unit.HOURS))) + .input(input) + .addAction("_logger", loggingAction("#### randomLogging")) + ) + .get(); new ExecuteWatchRequestBuilder(client()).setId("test_watch").setRecordExecution(true).get(); @@ -145,13 +150,12 @@ public void testPayloadInputWithDotsInFieldNameWorks() throws Exception { // as fields with dots are allowed in 5.0 again, the mapping must be checked in addition GetMappingsResponse response = client().admin().indices().prepareGetMappings(".watcher-history*").get(); - XContentSource source = new XContentSource( - response.getMappings().valuesIt().next().source().uncompressed(), XContentType.JSON); + XContentSource source = new XContentSource(response.getMappings().valuesIt().next().source().uncompressed(), XContentType.JSON); // lets make sure the body fields are disabled if (useChained) { - String path = SINGLE_MAPPING_NAME + - ".properties.result.properties.input.properties.chain.properties.chained.properties.payload.enabled"; + String path = SINGLE_MAPPING_NAME + + ".properties.result.properties.input.properties.chain.properties.chained.properties.payload.enabled"; assertThat(source.getValue(path), is(false)); } else { String path = SINGLE_MAPPING_NAME + ".properties.result.properties.input.properties.payload.enabled"; @@ -161,11 +165,12 @@ public void testPayloadInputWithDotsInFieldNameWorks() throws Exception { public void testThatHistoryContainsStatus() throws Exception { new PutWatchRequestBuilder(client()).setId("test_watch") - .setSource(watchBuilder() - .trigger(schedule(interval(5, IntervalSchedule.Interval.Unit.HOURS))) - .input(simpleInput("foo", "bar")) - .addAction("_logger", loggingAction("#### randomLogging"))) - .get(); + .setSource( + watchBuilder().trigger(schedule(interval(5, IntervalSchedule.Interval.Unit.HOURS))) + .input(simpleInput("foo", "bar")) + .addAction("_logger", loggingAction("#### randomLogging")) + ) + .get(); new ExecuteWatchRequestBuilder(client()).setId("test_watch").setRecordExecution(true).get(); @@ -202,12 +207,13 @@ public void testThatHistoryContainsStatus() throws Exception { // also ensure that the status field is disabled in the watch history GetMappingsResponse response = client().admin().indices().prepareGetMappings(".watcher-history*").get(); - XContentSource mappingSource = - new XContentSource(response.getMappings().valuesIt().next().source().uncompressed(), XContentType.JSON); + XContentSource mappingSource = new XContentSource( + response.getMappings().valuesIt().next().source().uncompressed(), + XContentType.JSON + ); assertThat(mappingSource.getValue(SINGLE_MAPPING_NAME + ".properties.status.enabled"), is(false)); assertThat(mappingSource.getValue(SINGLE_MAPPING_NAME + ".properties.status.properties.status"), is(nullValue())); assertThat(mappingSource.getValue(SINGLE_MAPPING_NAME + ".properties.status.properties.status.properties.active"), is(nullValue())); } - } diff --git a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/test/integration/HttpSecretsIntegrationTests.java b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/test/integration/HttpSecretsIntegrationTests.java index 212b9c7e6d202..872f2b52ce123 100644 --- a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/test/integration/HttpSecretsIntegrationTests.java +++ b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/test/integration/HttpSecretsIntegrationTests.java @@ -82,24 +82,29 @@ protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { MockSecureSettings secureSettings = new MockSecureSettings(); secureSettings.setFile(WatcherField.ENCRYPTION_KEY_SETTING.getKey(), encryptionKey); return Settings.builder() - .put(super.nodeSettings(nodeOrdinal, otherSettings)) - .put("xpack.watcher.encrypt_sensitive_data", encryptSensitiveData) - .setSecureSettings(secureSettings) - .build(); + .put(super.nodeSettings(nodeOrdinal, otherSettings)) + .put("xpack.watcher.encrypt_sensitive_data", encryptSensitiveData) + .setSecureSettings(secureSettings) + .build(); } return super.nodeSettings(nodeOrdinal, otherSettings); } public void testHttpInput() throws Exception { new PutWatchRequestBuilder(client()).setId("_id") - .setSource(watchBuilder() - .trigger(schedule(cron("0 0 0 1 * ? 2020"))) - .input(httpInput(HttpRequestTemplate.builder(webServer.getHostName(), webServer.getPort()) + .setSource( + watchBuilder().trigger(schedule(cron("0 0 0 1 * ? 2020"))) + .input( + httpInput( + HttpRequestTemplate.builder(webServer.getHostName(), webServer.getPort()) .path("/") - .auth(new BasicAuth(USERNAME, PASSWORD.toCharArray())))) - .condition(InternalAlwaysCondition.INSTANCE) - .addAction("_logging", loggingAction("executed"))) - .get(); + .auth(new BasicAuth(USERNAME, PASSWORD.toCharArray())) + ) + ) + .condition(InternalAlwaysCondition.INSTANCE) + .addAction("_logging", loggingAction("executed")) + ) + .get(); // verifying the basic auth password is stored encrypted in the index when security // is enabled, and when it's not enabled, it's stored in plain text @@ -140,15 +145,17 @@ public void testHttpInput() throws Exception { // now lets execute the watch manually - webServer.enqueue(new MockResponse().setResponseCode(200).setBody( - BytesReference.bytes(jsonBuilder().startObject().field("key", "value").endObject()).utf8ToString())); + webServer.enqueue( + new MockResponse().setResponseCode(200) + .setBody(BytesReference.bytes(jsonBuilder().startObject().field("key", "value").endObject()).utf8ToString()) + ); TriggerEvent triggerEvent = new ScheduleTriggerEvent(ZonedDateTime.now(ZoneOffset.UTC), ZonedDateTime.now(ZoneOffset.UTC)); ExecuteWatchResponse executeResponse = new ExecuteWatchRequestBuilder(client()).setId("_id") - .setRecordExecution(false) - .setTriggerEvent(triggerEvent) - .setActionMode("_all", ActionExecutionMode.FORCE_EXECUTE) - .get(); + .setRecordExecution(false) + .setTriggerEvent(triggerEvent) + .setActionMode("_all", ActionExecutionMode.FORCE_EXECUTE) + .get(); assertThat(executeResponse, notNullValue()); contentSource = executeResponse.getRecordSource(); value = contentSource.getValue("result.input.http.status_code"); @@ -156,28 +163,34 @@ public void testHttpInput() throws Exception { assertThat(value, is((Object) 200)); assertThat(webServer.requests(), hasSize(1)); - assertThat(webServer.requests().get(0).getHeader("Authorization"), - is(headerValue(USERNAME, PASSWORD.toCharArray()))); + assertThat(webServer.requests().get(0).getHeader("Authorization"), is(headerValue(USERNAME, PASSWORD.toCharArray()))); // now trigger the by the scheduler and make sure that the password is also correctly transmitted - webServer.enqueue(new MockResponse().setResponseCode(200).setBody( - BytesReference.bytes(jsonBuilder().startObject().field("key", "value").endObject()).utf8ToString())); + webServer.enqueue( + new MockResponse().setResponseCode(200) + .setBody(BytesReference.bytes(jsonBuilder().startObject().field("key", "value").endObject()).utf8ToString()) + ); timeWarp().trigger("_id"); assertThat(webServer.requests(), hasSize(2)); - assertThat(webServer.requests().get(1).getHeader("Authorization"), - is(headerValue(USERNAME, PASSWORD.toCharArray()))); + assertThat(webServer.requests().get(1).getHeader("Authorization"), is(headerValue(USERNAME, PASSWORD.toCharArray()))); } public void testWebhookAction() throws Exception { new PutWatchRequestBuilder(client()).setId("_id") - .setSource(watchBuilder() - .trigger(schedule(cron("0 0 0 1 * ? 2020"))) - .input(simpleInput()) - .condition(InternalAlwaysCondition.INSTANCE) - .addAction("_webhook", webhookAction(HttpRequestTemplate.builder(webServer.getHostName(), webServer.getPort()) + .setSource( + watchBuilder().trigger(schedule(cron("0 0 0 1 * ? 2020"))) + .input(simpleInput()) + .condition(InternalAlwaysCondition.INSTANCE) + .addAction( + "_webhook", + webhookAction( + HttpRequestTemplate.builder(webServer.getHostName(), webServer.getPort()) .path("/") - .auth(new BasicAuth(USERNAME, PASSWORD.toCharArray()))))) - .get(); + .auth(new BasicAuth(USERNAME, PASSWORD.toCharArray())) + ) + ) + ) + .get(); // verifying the basic auth password is stored encrypted in the index when security // is enabled, when it's not enabled, the password should be stored in plain text @@ -219,15 +232,17 @@ public void testWebhookAction() throws Exception { // now lets execute the watch manually - webServer.enqueue(new MockResponse().setResponseCode(200).setBody( - BytesReference.bytes(jsonBuilder().startObject().field("key", "value").endObject()).utf8ToString())); + webServer.enqueue( + new MockResponse().setResponseCode(200) + .setBody(BytesReference.bytes(jsonBuilder().startObject().field("key", "value").endObject()).utf8ToString()) + ); TriggerEvent triggerEvent = new ScheduleTriggerEvent(ZonedDateTime.now(ZoneOffset.UTC), ZonedDateTime.now(ZoneOffset.UTC)); ExecuteWatchResponse executeResponse = new ExecuteWatchRequestBuilder(client()).setId("_id") - .setRecordExecution(false) - .setActionMode("_all", ActionExecutionMode.FORCE_EXECUTE) - .setTriggerEvent(triggerEvent) - .get(); + .setRecordExecution(false) + .setActionMode("_all", ActionExecutionMode.FORCE_EXECUTE) + .setTriggerEvent(triggerEvent) + .get(); assertThat(executeResponse, notNullValue()); contentSource = executeResponse.getRecordSource(); @@ -252,8 +267,7 @@ public void testWebhookAction() throws Exception { } assertThat(webServer.requests(), hasSize(1)); - assertThat(webServer.requests().get(0).getHeader("Authorization"), - is(headerValue(USERNAME, PASSWORD.toCharArray()))); + assertThat(webServer.requests().get(0).getHeader("Authorization"), is(headerValue(USERNAME, PASSWORD.toCharArray()))); } private String headerValue(String username, char[] password) { diff --git a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/test/integration/RejectedExecutionTests.java b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/test/integration/RejectedExecutionTests.java index e32f171a47247..e35ad66cd7d08 100644 --- a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/test/integration/RejectedExecutionTests.java +++ b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/test/integration/RejectedExecutionTests.java @@ -30,7 +30,7 @@ public class RejectedExecutionTests extends AbstractWatcherIntegrationTestCase { @Override protected boolean timeWarped() { - //need to use the real scheduler + // need to use the real scheduler return false; } @@ -40,12 +40,12 @@ public void testHistoryOnRejection() throws Exception { refresh(); WatcherSearchTemplateRequest request = templateRequest(searchSource().query(termQuery("field", "a")), "idx"); new PutWatchRequestBuilder(client()).setId(randomAlphaOfLength(5)) - .setSource(watchBuilder() - .trigger(schedule(interval(1, IntervalSchedule.Interval.Unit.SECONDS))) - .input(searchInput(request)) - .condition(new CompareCondition("ctx.payload.hits.total", CompareCondition.Op.EQ, 1L)) - .addAction("_logger", loggingAction("_logging") - .setCategory("_category"))) + .setSource( + watchBuilder().trigger(schedule(interval(1, IntervalSchedule.Interval.Unit.SECONDS))) + .input(searchInput(request)) + .condition(new CompareCondition("ctx.payload.hits.total", CompareCondition.Op.EQ, 1L)) + .addAction("_logger", loggingAction("_logging").setCategory("_category")) + ) .get(); assertBusy(() -> { @@ -69,5 +69,4 @@ protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { .build(); } - } diff --git a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/test/integration/SingleNodeTests.java b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/test/integration/SingleNodeTests.java index 0eb8c6e31c62b..69d2531135f6c 100644 --- a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/test/integration/SingleNodeTests.java +++ b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/test/integration/SingleNodeTests.java @@ -40,7 +40,7 @@ protected boolean timeWarped() { // this is the standard setup when starting watcher in a regular cluster // the index does not exist, a watch gets added // the watch should be executed properly, despite the index being created and the cluster state listener being reloaded - @AwaitsFix(bugUrl="https://github.com/elastic/elasticsearch/issues/54096") + @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/54096") public void testThatLoadingWithNonExistingIndexWorks() throws Exception { stopWatcher(); ClusterStateResponse clusterStateResponse = client().admin().cluster().prepareState().get(); @@ -52,10 +52,11 @@ public void testThatLoadingWithNonExistingIndexWorks() throws Exception { String watchId = randomAlphaOfLength(20); // now we start with an empty set up, store a watch and expected it to be executed PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client()).setId(watchId) - .setSource(watchBuilder() - .trigger(schedule(interval(1, IntervalSchedule.Interval.Unit.SECONDS))) - .input(simpleInput()) - .addAction("_logger", loggingAction("logging of watch _name"))) + .setSource( + watchBuilder().trigger(schedule(interval(1, IntervalSchedule.Interval.Unit.SECONDS))) + .input(simpleInput()) + .addAction("_logger", loggingAction("logging of watch _name")) + ) .get(); assertThat(putWatchResponse.isCreated(), is(true)); diff --git a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/test/integration/WatchAckTests.java b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/test/integration/WatchAckTests.java index d2d00748af61c..af8ada52b8057 100644 --- a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/test/integration/WatchAckTests.java +++ b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/test/integration/WatchAckTests.java @@ -13,8 +13,8 @@ import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.support.WriteRequest; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.protocol.xpack.watcher.PutWatchResponse; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.watcher.actions.ActionStatus; import org.elasticsearch.xpack.core.watcher.execution.ExecutionState; import org.elasticsearch.xpack.core.watcher.history.HistoryStoreField; @@ -54,25 +54,27 @@ public class WatchAckTests extends AbstractWatcherIntegrationTestCase { @Before public void indexTestDocument() { - IndexResponse eventIndexResponse = client().prepareIndex().setIndex("events").setId(id) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - .setSource("level", "error") - .get(); + IndexResponse eventIndexResponse = client().prepareIndex() + .setIndex("events") + .setId(id) + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + .setSource("level", "error") + .get(); assertEquals(DocWriteResponse.Result.CREATED, eventIndexResponse.getResult()); } public void testAckSingleAction() throws Exception { - PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client()) - .setId("_id") - .setSource(watchBuilder() - .trigger(schedule(cron("0/5 * * * * ? *"))) - .input(searchInput(templateRequest(searchSource(), "events"))) - .condition(new CompareCondition("ctx.payload.hits.total", CompareCondition.Op.GT, 0L)) - .transform(searchTransform(templateRequest(searchSource(), "events"))) - .addAction("_a1", indexAction("actions1")) - .addAction("_a2", indexAction("actions2")) - .defaultThrottlePeriod(new TimeValue(0, TimeUnit.SECONDS))) - .get(); + PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client()).setId("_id") + .setSource( + watchBuilder().trigger(schedule(cron("0/5 * * * * ? *"))) + .input(searchInput(templateRequest(searchSource(), "events"))) + .condition(new CompareCondition("ctx.payload.hits.total", CompareCondition.Op.GT, 0L)) + .transform(searchTransform(templateRequest(searchSource(), "events"))) + .addAction("_a1", indexAction("actions1")) + .addAction("_a2", indexAction("actions2")) + .defaultThrottlePeriod(new TimeValue(0, TimeUnit.SECONDS)) + ) + .get(); assertThat(putWatchResponse.isCreated(), is(true)); assertThat(new WatcherStatsRequestBuilder(client()).get().getWatchesCount(), is(1L)); @@ -110,30 +112,42 @@ public void testAckSingleAction() throws Exception { GetWatchResponse getWatchResponse = new GetWatchRequestBuilder(client()).setId("_id").get(); assertThat(getWatchResponse.isFound(), is(true)); - Watch parsedWatch = watchParser().parse(getWatchResponse.getId(), true, getWatchResponse.getSource().getBytes(), - XContentType.JSON, getWatchResponse.getSeqNo(), getWatchResponse.getPrimaryTerm()); - assertThat(parsedWatch.status().actionStatus("_a1").ackStatus().state(), - is(ActionStatus.AckStatus.State.AWAITS_SUCCESSFUL_EXECUTION)); - assertThat(parsedWatch.status().actionStatus("_a2").ackStatus().state(), - is(ActionStatus.AckStatus.State.AWAITS_SUCCESSFUL_EXECUTION)); - - long throttledCount = docCount(HistoryStoreField.DATA_STREAM + "*", - matchQuery(WatchRecord.STATE.getPreferredName(), ExecutionState.ACKNOWLEDGED.id())); + Watch parsedWatch = watchParser().parse( + getWatchResponse.getId(), + true, + getWatchResponse.getSource().getBytes(), + XContentType.JSON, + getWatchResponse.getSeqNo(), + getWatchResponse.getPrimaryTerm() + ); + assertThat( + parsedWatch.status().actionStatus("_a1").ackStatus().state(), + is(ActionStatus.AckStatus.State.AWAITS_SUCCESSFUL_EXECUTION) + ); + assertThat( + parsedWatch.status().actionStatus("_a2").ackStatus().state(), + is(ActionStatus.AckStatus.State.AWAITS_SUCCESSFUL_EXECUTION) + ); + + long throttledCount = docCount( + HistoryStoreField.DATA_STREAM + "*", + matchQuery(WatchRecord.STATE.getPreferredName(), ExecutionState.ACKNOWLEDGED.id()) + ); assertThat(throttledCount, greaterThan(0L)); } public void testAckAllActions() throws Exception { - PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client()) - .setId("_id") - .setSource(watchBuilder() - .trigger(schedule(cron("0/5 * * * * ? *"))) - .input(searchInput(templateRequest(searchSource(), "events"))) - .condition(new CompareCondition("ctx.payload.hits.total", CompareCondition.Op.GT, 0L)) - .transform(searchTransform(templateRequest(searchSource(), "events"))) - .addAction("_a1", indexAction("actions1")) - .addAction("_a2", indexAction("actions2")) - .defaultThrottlePeriod(new TimeValue(0, TimeUnit.SECONDS))) - .get(); + PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client()).setId("_id") + .setSource( + watchBuilder().trigger(schedule(cron("0/5 * * * * ? *"))) + .input(searchInput(templateRequest(searchSource(), "events"))) + .condition(new CompareCondition("ctx.payload.hits.total", CompareCondition.Op.GT, 0L)) + .transform(searchTransform(templateRequest(searchSource(), "events"))) + .addAction("_a1", indexAction("actions1")) + .addAction("_a2", indexAction("actions2")) + .defaultThrottlePeriod(new TimeValue(0, TimeUnit.SECONDS)) + ) + .get(); assertThat(putWatchResponse.isCreated(), is(true)); assertThat(new WatcherStatsRequestBuilder(client()).get().getWatchesCount(), is(1L)); @@ -179,28 +193,40 @@ public void testAckAllActions() throws Exception { GetWatchResponse getWatchResponse = new GetWatchRequestBuilder(client()).setId("_id").get(); assertThat(getWatchResponse.isFound(), is(true)); - Watch parsedWatch = watchParser().parse(getWatchResponse.getId(), true, - getWatchResponse.getSource().getBytes(), XContentType.JSON, getWatchResponse.getSeqNo(), getWatchResponse.getPrimaryTerm()); - assertThat(parsedWatch.status().actionStatus("_a1").ackStatus().state(), - is(ActionStatus.AckStatus.State.AWAITS_SUCCESSFUL_EXECUTION)); - assertThat(parsedWatch.status().actionStatus("_a2").ackStatus().state(), - is(ActionStatus.AckStatus.State.AWAITS_SUCCESSFUL_EXECUTION)); - - long throttledCount = docCount(HistoryStoreField.DATA_STREAM + "*", - matchQuery(WatchRecord.STATE.getPreferredName(), ExecutionState.ACKNOWLEDGED.id())); + Watch parsedWatch = watchParser().parse( + getWatchResponse.getId(), + true, + getWatchResponse.getSource().getBytes(), + XContentType.JSON, + getWatchResponse.getSeqNo(), + getWatchResponse.getPrimaryTerm() + ); + assertThat( + parsedWatch.status().actionStatus("_a1").ackStatus().state(), + is(ActionStatus.AckStatus.State.AWAITS_SUCCESSFUL_EXECUTION) + ); + assertThat( + parsedWatch.status().actionStatus("_a2").ackStatus().state(), + is(ActionStatus.AckStatus.State.AWAITS_SUCCESSFUL_EXECUTION) + ); + + long throttledCount = docCount( + HistoryStoreField.DATA_STREAM + "*", + matchQuery(WatchRecord.STATE.getPreferredName(), ExecutionState.ACKNOWLEDGED.id()) + ); assertThat(throttledCount, greaterThan(0L)); } public void testAckWithRestart() throws Exception { - PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client()) - .setId("_name") - .setSource(watchBuilder() - .trigger(schedule(cron("0/5 * * * * ? *"))) - .input(searchInput(templateRequest(searchSource(), "events"))) - .condition(new CompareCondition("ctx.payload.hits.total", CompareCondition.Op.GT, 0L)) - .transform(searchTransform(templateRequest(searchSource(), "events"))) - .addAction("_id", indexAction("actions"))) - .get(); + PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client()).setId("_name") + .setSource( + watchBuilder().trigger(schedule(cron("0/5 * * * * ? *"))) + .input(searchInput(templateRequest(searchSource(), "events"))) + .condition(new CompareCondition("ctx.payload.hits.total", CompareCondition.Op.GT, 0L)) + .transform(searchTransform(templateRequest(searchSource(), "events"))) + .addAction("_id", indexAction("actions")) + ) + .get(); assertThat(putWatchResponse.isCreated(), is(true)); assertThat(new WatcherStatsRequestBuilder(client()).get().getWatchesCount(), is(1L)); @@ -211,8 +237,7 @@ public void testAckWithRestart() throws Exception { assertThat(ackResponse.getStatus().actionStatus("_id").ackStatus().state(), is(ActionStatus.AckStatus.State.ACKED)); refresh("actions"); - long countAfterAck = client().prepareSearch("actions").setQuery(matchAllQuery()).get() - .getHits().getTotalHits().value; + long countAfterAck = client().prepareSearch("actions").setQuery(matchAllQuery()).get().getHits().getTotalHits().value; assertThat(countAfterAck, greaterThanOrEqualTo(1L)); restartWatcherRandomly(); @@ -222,10 +247,18 @@ public void testAckWithRestart() throws Exception { refresh(); GetResponse getResponse = client().get(new GetRequest(Watch.INDEX, "_name")).actionGet(); - Watch indexedWatch = watchParser().parse("_name", true, getResponse.getSourceAsBytesRef(), XContentType.JSON, - getResponse.getSeqNo(), getResponse.getPrimaryTerm()); - assertThat(watchResponse.getStatus().actionStatus("_id").ackStatus().state(), - equalTo(indexedWatch.status().actionStatus("_id").ackStatus().state())); + Watch indexedWatch = watchParser().parse( + "_name", + true, + getResponse.getSourceAsBytesRef(), + XContentType.JSON, + getResponse.getSeqNo(), + getResponse.getPrimaryTerm() + ); + assertThat( + watchResponse.getStatus().actionStatus("_id").ackStatus().state(), + equalTo(indexedWatch.status().actionStatus("_id").ackStatus().state()) + ); timeWarp().trigger("_name", 4, TimeValue.timeValueSeconds(5)); refresh("actions"); diff --git a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/test/integration/WatchMetadataTests.java b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/test/integration/WatchMetadataTests.java index 7810050a99b3b..715d354584d88 100644 --- a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/test/integration/WatchMetadataTests.java +++ b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/test/integration/WatchMetadataTests.java @@ -52,19 +52,20 @@ public void testWatchMetadata() throws Exception { metadata.put("baz", metaList); new PutWatchRequestBuilder(client()).setId("_name") - .setSource(watchBuilder() - .trigger(schedule(cron("0/5 * * * * ? *"))) - .input(noneInput()) - .condition(new CompareCondition("ctx.payload.hits.total.value", CompareCondition.Op.EQ, 1L)) - .metadata(metadata)) - .get(); + .setSource( + watchBuilder().trigger(schedule(cron("0/5 * * * * ? *"))) + .input(noneInput()) + .condition(new CompareCondition("ctx.payload.hits.total.value", CompareCondition.Op.EQ, 1L)) + .metadata(metadata) + ) + .get(); timeWarp().trigger("_name"); refresh(); SearchResponse searchResponse = client().prepareSearch(HistoryStoreField.DATA_STREAM + "*") - .setQuery(termQuery("metadata.foo", "bar")) - .get(); + .setQuery(termQuery("metadata.foo", "bar")) + .get(); assertThat(searchResponse.getHits().getTotalHits().value, greaterThan(0L)); } @@ -73,23 +74,24 @@ public void testWatchMetadataAvailableAtExecution() throws Exception { metadata.put("foo", "bar"); metadata.put("logtext", "This is a test"); - LoggingAction.Builder loggingAction = loggingAction(new TextTemplate("_logging")) - .setLevel(LoggingLevel.DEBUG) - .setCategory("test"); + LoggingAction.Builder loggingAction = loggingAction(new TextTemplate("_logging")).setLevel(LoggingLevel.DEBUG).setCategory("test"); new PutWatchRequestBuilder(client()).setId("_name") - .setSource(watchBuilder() - .trigger(schedule(cron("0 0 0 1 1 ? 2050"))) - .input(noneInput()) - .condition(InternalAlwaysCondition.INSTANCE) - .addAction("testLogger", loggingAction) - .defaultThrottlePeriod(TimeValue.timeValueSeconds(0)) - .metadata(metadata)) - .get(); + .setSource( + watchBuilder().trigger(schedule(cron("0 0 0 1 1 ? 2050"))) + .input(noneInput()) + .condition(InternalAlwaysCondition.INSTANCE) + .addAction("testLogger", loggingAction) + .defaultThrottlePeriod(TimeValue.timeValueSeconds(0)) + .metadata(metadata) + ) + .get(); TriggerEvent triggerEvent = new ScheduleTriggerEvent(ZonedDateTime.now(ZoneOffset.UTC), ZonedDateTime.now(ZoneOffset.UTC)); ExecuteWatchResponse executeWatchResponse = new ExecuteWatchRequestBuilder(client()).setId("_name") - .setTriggerEvent(triggerEvent).setActionMode("_all", ActionExecutionMode.SIMULATE).get(); + .setTriggerEvent(triggerEvent) + .setActionMode("_all", ActionExecutionMode.SIMULATE) + .get(); Map result = executeWatchResponse.getRecordSource().getAsMap(); logger.info("result=\n{}", result); diff --git a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/transform/TransformIntegrationTests.java b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/transform/TransformIntegrationTests.java index d64fa0e495339..6641d23036f9c 100644 --- a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/transform/TransformIntegrationTests.java +++ b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/transform/TransformIntegrationTests.java @@ -10,13 +10,13 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.collect.MapBuilder; import org.elasticsearch.common.util.CollectionUtils; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.common.xcontent.support.XContentMapValues; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.protocol.xpack.watcher.PutWatchResponse; import org.elasticsearch.script.MockScriptPlugin; import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptType; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.watcher.transport.actions.execute.ExecuteWatchRequestBuilder; import org.elasticsearch.xpack.core.watcher.transport.actions.put.PutWatchRequestBuilder; import org.elasticsearch.xpack.watcher.support.search.WatcherSearchTemplateRequest; @@ -107,30 +107,39 @@ public void testScriptTransform() throws Exception { script = mockScript("['key3' : ctx.payload.key1 + ctx.payload.key2]"); } else { logger.info("testing script transform with an indexed script"); - assertAcked(client().admin().cluster().preparePutStoredScript() + assertAcked( + client().admin() + .cluster() + .preparePutStoredScript() .setId("my-script") - .setContent(new BytesArray("{\"script\" : {\"lang\": \"" + MockScriptPlugin.NAME + "\", " + - "\"source\": \"['key3' : ctx.payload.key1 + ctx.payload.key2]\"}"), XContentType.JSON) - .get()); + .setContent( + new BytesArray( + "{\"script\" : {\"lang\": \"" + + MockScriptPlugin.NAME + + "\", " + + "\"source\": \"['key3' : ctx.payload.key1 + ctx.payload.key2]\"}" + ), + XContentType.JSON + ) + .get() + ); script = new Script(ScriptType.STORED, null, "my-script", Collections.emptyMap()); } // put a watch that has watch level transform: - PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client(), "_id1") - .setSource(watchBuilder() - .trigger(schedule(interval("5s"))) - .input(simpleInput(MapBuilder.newMapBuilder().put("key1", 10).put("key2", 10))) - .transform(scriptTransform(script)) - .addAction("_id", indexAction("output1"))) - .get(); + PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client(), "_id1").setSource( + watchBuilder().trigger(schedule(interval("5s"))) + .input(simpleInput(MapBuilder.newMapBuilder().put("key1", 10).put("key2", 10))) + .transform(scriptTransform(script)) + .addAction("_id", indexAction("output1")) + ).get(); assertThat(putWatchResponse.isCreated(), is(true)); // put a watch that has a action level transform: - putWatchResponse = new PutWatchRequestBuilder(client(), "_id2") - .setSource(watchBuilder() - .trigger(schedule(interval("5s"))) - .input(simpleInput(MapBuilder.newMapBuilder().put("key1", 10).put("key2", 10))) - .addAction("_id", scriptTransform(script), indexAction("output2"))) - .get(); + putWatchResponse = new PutWatchRequestBuilder(client(), "_id2").setSource( + watchBuilder().trigger(schedule(interval("5s"))) + .input(simpleInput(MapBuilder.newMapBuilder().put("key1", 10).put("key2", 10))) + .addAction("_id", scriptTransform(script), indexAction("output2")) + ).get(); assertThat(putWatchResponse.isCreated(), is(true)); executeWatch("_id1"); @@ -164,20 +173,18 @@ public void testSearchTransform() throws Exception { WatcherSearchTemplateRequest inputRequest = templateRequest(searchSource().query(matchAllQuery()), "my-condition-index"); WatcherSearchTemplateRequest transformRequest = templateRequest(searchSource().query(matchAllQuery()), "my-payload-index"); - PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client(), "_id1") - .setSource(watchBuilder() - .trigger(schedule(interval("5s"))) - .input(searchInput(inputRequest)) - .transform(searchTransform(transformRequest)) - .addAction("_id", indexAction("output1")) - ).get(); + PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client(), "_id1").setSource( + watchBuilder().trigger(schedule(interval("5s"))) + .input(searchInput(inputRequest)) + .transform(searchTransform(transformRequest)) + .addAction("_id", indexAction("output1")) + ).get(); assertThat(putWatchResponse.isCreated(), is(true)); - putWatchResponse = new PutWatchRequestBuilder(client(), "_id2") - .setSource(watchBuilder() - .trigger(schedule(interval("5s"))) - .input(searchInput(inputRequest)) - .addAction("_id", searchTransform(transformRequest), indexAction("output2")) - ).get(); + putWatchResponse = new PutWatchRequestBuilder(client(), "_id2").setSource( + watchBuilder().trigger(schedule(interval("5s"))) + .input(searchInput(inputRequest)) + .addAction("_id", searchTransform(transformRequest), indexAction("output2")) + ).get(); assertThat(putWatchResponse.isCreated(), is(true)); executeWatch("_id1"); @@ -204,22 +211,19 @@ public void testChainTransform() throws Exception { Script script2 = mockScript("['key4' : ctx.payload.key3 + 10]"); // put a watch that has watch level transform: - PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client(), "_id1") - .setSource(watchBuilder() - .trigger(schedule(interval("5s"))) - .input(simpleInput(MapBuilder.newMapBuilder().put("key1", 10).put("key2", 10))) - .transform(chainTransform(scriptTransform(script1), scriptTransform(script2))) - .addAction("_id", indexAction("output1"))) - .get(); + PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client(), "_id1").setSource( + watchBuilder().trigger(schedule(interval("5s"))) + .input(simpleInput(MapBuilder.newMapBuilder().put("key1", 10).put("key2", 10))) + .transform(chainTransform(scriptTransform(script1), scriptTransform(script2))) + .addAction("_id", indexAction("output1")) + ).get(); assertThat(putWatchResponse.isCreated(), is(true)); // put a watch that has a action level transform: - putWatchResponse = new PutWatchRequestBuilder(client(), "_id2") - .setSource(watchBuilder() - .trigger(schedule(interval("5s"))) - .input(simpleInput(MapBuilder.newMapBuilder().put("key1", 10).put("key2", 10))) - .addAction("_id", chainTransform(scriptTransform(script1), scriptTransform(script2)), - indexAction("output2"))) - .get(); + putWatchResponse = new PutWatchRequestBuilder(client(), "_id2").setSource( + watchBuilder().trigger(schedule(interval("5s"))) + .input(simpleInput(MapBuilder.newMapBuilder().put("key1", 10).put("key2", 10))) + .addAction("_id", chainTransform(scriptTransform(script1), scriptTransform(script2)), indexAction("output2")) + ).get(); assertThat(putWatchResponse.isCreated(), is(true)); executeWatch("_id1"); @@ -244,8 +248,6 @@ public void testChainTransform() throws Exception { } private void executeWatch(String watchId) { - new ExecuteWatchRequestBuilder(client(), watchId) - .setRecordExecution(true) - .get(); + new ExecuteWatchRequestBuilder(client(), watchId).setRecordExecution(true).get(); } } diff --git a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/transport/action/activate/ActivateWatchTests.java b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/transport/action/activate/ActivateWatchTests.java index f9a25ce1f63da..ec03215435fae 100644 --- a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/transport/action/activate/ActivateWatchTests.java +++ b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/transport/action/activate/ActivateWatchTests.java @@ -11,12 +11,12 @@ import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.BytesStreamOutput; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.set.Sets; +import org.elasticsearch.core.TimeValue; +import org.elasticsearch.protocol.xpack.watcher.PutWatchResponse; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.protocol.xpack.watcher.PutWatchResponse; import org.elasticsearch.xpack.core.watcher.execution.ExecutionState; import org.elasticsearch.xpack.core.watcher.support.xcontent.XContentSource; import org.elasticsearch.xpack.core.watcher.transport.actions.activate.ActivateWatchRequestBuilder; @@ -51,14 +51,14 @@ protected boolean timeWarped() { } public void testDeactivateAndActivate() throws Exception { - PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client()) - .setId("_id") - .setSource(watchBuilder() - .trigger(schedule(interval("1s"))) - .input(simpleInput("foo", "bar")) - .addAction("_a1", indexAction("actions")) - .defaultThrottlePeriod(new TimeValue(0, TimeUnit.SECONDS))) - .get(); + PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client()).setId("_id") + .setSource( + watchBuilder().trigger(schedule(interval("1s"))) + .input(simpleInput("foo", "bar")) + .addAction("_a1", indexAction("actions")) + .defaultThrottlePeriod(new TimeValue(0, TimeUnit.SECONDS)) + ) + .get(); assertThat(putWatchResponse.isCreated(), is(true)); @@ -107,14 +107,14 @@ public void testDeactivateAndActivate() throws Exception { } public void testLoadWatchWithoutAState() throws Exception { - PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client()) - .setId("_id") - .setSource(watchBuilder() - .trigger(schedule(cron("0 0 0 1 1 ? 2050"))) // some time in 2050 - .input(simpleInput("foo", "bar")) - .addAction("_a1", indexAction("actions")) - .defaultThrottlePeriod(new TimeValue(0, TimeUnit.SECONDS))) - .get(); + PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client()).setId("_id") + .setSource( + watchBuilder().trigger(schedule(cron("0 0 0 1 1 ? 2050"))) // some time in 2050 + .input(simpleInput("foo", "bar")) + .addAction("_a1", indexAction("actions")) + .defaultThrottlePeriod(new TimeValue(0, TimeUnit.SECONDS)) + ) + .get(); assertThat(putWatchResponse.isCreated(), is(true)); @@ -126,25 +126,28 @@ public void testLoadWatchWithoutAState() throws Exception { XContentSource source = new XContentSource(getResponse.getSourceAsBytesRef(), XContentType.JSON); Set filters = Sets.newHashSet( - "trigger.**", - "input.**", - "condition.**", - "throttle_period.**", - "transform.**", - "actions.**", - "metadata.**", - "status.version", - "status.last_checked", - "status.last_met_condition", - "status.actions.**"); + "trigger.**", + "input.**", + "condition.**", + "throttle_period.**", + "transform.**", + "actions.**", + "metadata.**", + "status.version", + "status.last_checked", + "status.last_met_condition", + "status.actions.**" + ); XContentBuilder builder = new XContentBuilder(XContentType.JSON, new BytesStreamOutput(), filters); source.toXContent(builder, ToXContent.EMPTY_PARAMS); // now that we filtered out the watch status state, lets put it back in - IndexResponse indexResponse = client().prepareIndex().setIndex(".watches").setId("_id") - .setSource(BytesReference.bytes(builder), XContentType.JSON) - .get(); + IndexResponse indexResponse = client().prepareIndex() + .setIndex(".watches") + .setId("_id") + .setSource(BytesReference.bytes(builder), XContentType.JSON) + .get(); assertThat(indexResponse.getId(), is("_id")); // now, let's restart diff --git a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/transport/action/delete/DeleteWatchTests.java b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/transport/action/delete/DeleteWatchTests.java index 0295a0f734932..19cdecb4edcfd 100644 --- a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/transport/action/delete/DeleteWatchTests.java +++ b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/transport/action/delete/DeleteWatchTests.java @@ -9,11 +9,11 @@ import org.elasticsearch.action.ActionFuture; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.ObjectPath; import org.elasticsearch.protocol.xpack.watcher.DeleteWatchResponse; import org.elasticsearch.protocol.xpack.watcher.PutWatchResponse; import org.elasticsearch.test.http.MockResponse; import org.elasticsearch.test.http.MockWebServer; +import org.elasticsearch.xcontent.ObjectPath; import org.elasticsearch.xpack.core.watcher.history.HistoryStoreField; import org.elasticsearch.xpack.core.watcher.transport.actions.delete.DeleteWatchRequestBuilder; import org.elasticsearch.xpack.core.watcher.transport.actions.execute.ExecuteWatchRequestBuilder; @@ -56,15 +56,14 @@ public void testWatchDeletionDuringExecutionWorks() throws Exception { server.start(); HttpRequestTemplate template = HttpRequestTemplate.builder(server.getHostName(), server.getPort()).path("/").build(); - PutWatchResponse responseFuture = new PutWatchRequestBuilder(client(), "_name").setSource(watchBuilder() - .trigger(schedule(interval("6h"))) - .input(httpInput(template)) - .addAction("_action1", loggingAction("anything"))) - .get(); + PutWatchResponse responseFuture = new PutWatchRequestBuilder(client(), "_name").setSource( + watchBuilder().trigger(schedule(interval("6h"))).input(httpInput(template)).addAction("_action1", loggingAction("anything")) + ).get(); assertThat(responseFuture.isCreated(), is(true)); - ActionFuture executeWatchFuture = - new ExecuteWatchRequestBuilder(client(), "_name").setRecordExecution(true).execute(); + ActionFuture executeWatchFuture = new ExecuteWatchRequestBuilder(client(), "_name").setRecordExecution( + true + ).execute(); // without this sleep the delete operation might overtake the watch execution sleep(1000); diff --git a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/transport/action/execute/ExecuteWatchTests.java b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/transport/action/execute/ExecuteWatchTests.java index 1efb93b619cd2..9fc4f14884e95 100644 --- a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/transport/action/execute/ExecuteWatchTests.java +++ b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/transport/action/execute/ExecuteWatchTests.java @@ -33,14 +33,14 @@ public class ExecuteWatchTests extends AbstractWatcherIntegrationTestCase { public void testExecuteAllDefaults() throws Exception { - PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client()) - .setId("_id") - .setSource(watchBuilder() - .trigger(schedule(cron("0/5 * * * * ? 2099"))) - .input(simpleInput("foo", "bar")) - .condition(InternalAlwaysCondition.INSTANCE) - .addAction("log", loggingAction("_text"))) - .get(); + PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client()).setId("_id") + .setSource( + watchBuilder().trigger(schedule(cron("0/5 * * * * ? 2099"))) + .input(simpleInput("foo", "bar")) + .condition(InternalAlwaysCondition.INSTANCE) + .addAction("log", loggingAction("_text")) + ) + .get(); assertThat(putWatchResponse.isCreated(), is(true)); @@ -73,15 +73,15 @@ public void testExecuteAllDefaults() throws Exception { } public void testExecuteActionMode() throws Exception { - PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client()) - .setId("_id") - .setSource(watchBuilder() - .trigger(schedule(interval("1s"))) // run every second so we can ack it - .input(simpleInput("foo", "bar")) - .defaultThrottlePeriod(TimeValue.timeValueMillis(0)) - .condition(InternalAlwaysCondition.INSTANCE) - .addAction("log", loggingAction("_text"))) - .get(); + PutWatchResponse putWatchResponse = new PutWatchRequestBuilder(client()).setId("_id") + .setSource( + watchBuilder().trigger(schedule(interval("1s"))) // run every second so we can ack it + .input(simpleInput("foo", "bar")) + .defaultThrottlePeriod(TimeValue.timeValueMillis(0)) + .condition(InternalAlwaysCondition.INSTANCE) + .addAction("log", loggingAction("_text")) + ) + .get(); assertThat(putWatchResponse.isCreated(), is(true)); @@ -109,12 +109,7 @@ public void testExecuteActionMode() throws Exception { // lets wait for the watch to be ackable timeWarp().trigger("_id"); - String[] actionIds = randomFrom( - new String[] { "_all" }, - new String[] { "log" }, - new String[] { "foo", "_all" }, - null - ); + String[] actionIds = randomFrom(new String[] { "_all" }, new String[] { "log" }, new String[] { "foo", "_all" }, null); AckWatchRequestBuilder ackWatchRequestBuilder = new AckWatchRequestBuilder(client(), "_id"); if (actionIds != null) { ackWatchRequestBuilder.setActionIds(actionIds); @@ -128,9 +123,10 @@ public void testExecuteActionMode() throws Exception { assertThat(actionStatus.ackStatus().state(), is(ActionStatus.AckStatus.State.ACKED)); } - ExecuteWatchResponse response = new ExecuteWatchRequestBuilder(client(), "_id") - .setActionMode(randomBoolean() ? "log" : "_all", mode) - .get(); + ExecuteWatchResponse response = new ExecuteWatchRequestBuilder(client(), "_id").setActionMode( + randomBoolean() ? "log" : "_all", + mode + ).get(); assertThat(response, notNullValue()); assertThat(response.getRecordId(), notNullValue()); Wid wid = new Wid(response.getRecordId()); diff --git a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/transport/action/get/GetWatchTests.java b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/transport/action/get/GetWatchTests.java index f82880e036ef8..d3b589572d25e 100644 --- a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/transport/action/get/GetWatchTests.java +++ b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/transport/action/get/GetWatchTests.java @@ -34,12 +34,12 @@ public class GetWatchTests extends AbstractWatcherIntegrationTestCase { public void testGet() throws Exception { - PutWatchResponse putResponse = new PutWatchRequestBuilder(client(), "_name").setSource(watchBuilder() - .trigger(schedule(interval("5m"))) + PutWatchResponse putResponse = new PutWatchRequestBuilder(client(), "_name").setSource( + watchBuilder().trigger(schedule(interval("5m"))) .input(simpleInput()) .condition(InternalAlwaysCondition.INSTANCE) - .addAction("_action1", loggingAction("{{ctx.watch_id}}"))) - .get(); + .addAction("_action1", loggingAction("{{ctx.watch_id}}")) + ).get(); assertThat(putResponse, notNullValue()); assertThat(putResponse.isCreated(), is(true)); diff --git a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/watch/WatchStatusIntegrationTests.java b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/watch/WatchStatusIntegrationTests.java index abbd0578a2783..b372dab5d88b2 100644 --- a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/watch/WatchStatusIntegrationTests.java +++ b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/watch/WatchStatusIntegrationTests.java @@ -35,13 +35,12 @@ public class WatchStatusIntegrationTests extends AbstractWatcherIntegrationTestCase { public void testThatStatusGetsUpdated() throws Exception { - new PutWatchRequestBuilder(client(), "_name") - .setSource(watchBuilder() - .trigger(schedule(interval(5, SECONDS))) - .input(simpleInput()) - .condition(NeverCondition.INSTANCE) - .addAction("_logger", loggingAction("logged text"))) - .get(); + new PutWatchRequestBuilder(client(), "_name").setSource( + watchBuilder().trigger(schedule(interval(5, SECONDS))) + .input(simpleInput()) + .condition(NeverCondition.INSTANCE) + .addAction("_logger", loggingAction("logged text")) + ).get(); timeWarp().trigger("_name"); GetWatchResponse getWatchResponse = new GetWatchRequestBuilder(client(), "_name").get(); @@ -63,10 +62,10 @@ public void testThatStatusGetsUpdated() throws Exception { } private Matcher isMillisResolution() { - return new FeatureMatcher(equalTo(true), "has millisecond precision", "precission") { + return new FeatureMatcher(equalTo(true), "has millisecond precision", "precission") { @Override protected Boolean featureValueOf(ZonedDateTime actual) { - //if date has millisecond precision its nanosecond field will be rounded to millis (equal millis * 10^6) + // if date has millisecond precision its nanosecond field will be rounded to millis (equal millis * 10^6) return actual.getNano() == actual.get(ChronoField.MILLI_OF_SECOND) * 1000_000; } }; diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/EncryptSensitiveDataBootstrapCheck.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/EncryptSensitiveDataBootstrapCheck.java index 82f820b244e19..555787f577efe 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/EncryptSensitiveDataBootstrapCheck.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/EncryptSensitiveDataBootstrapCheck.java @@ -19,22 +19,26 @@ final class EncryptSensitiveDataBootstrapCheck implements BootstrapCheck { @Override public BootstrapCheckResult check(BootstrapContext context) { if (Watcher.ENCRYPT_SENSITIVE_DATA_SETTING.get(context.settings()) - && WatcherField.ENCRYPTION_KEY_SETTING.exists(context.settings()) == false) { + && WatcherField.ENCRYPTION_KEY_SETTING.exists(context.settings()) == false) { final Path systemKeyPath = XPackPlugin.resolveConfigFile(context.environment(), "system_key").toAbsolutePath(); final String message; if (Files.exists(systemKeyPath)) { - message = "Encryption of sensitive data requires the key to be placed in the secure setting store. Run " + - "'bin/elasticsearch-keystore add-file " + WatcherField.ENCRYPTION_KEY_SETTING.getKey() + " " + - systemKeyPath + - "' to import the file.\nAfter importing, the system_key file should be removed from the " + - "filesystem.\nRepeat this on every node in the cluster."; + message = "Encryption of sensitive data requires the key to be placed in the secure setting store. Run " + + "'bin/elasticsearch-keystore add-file " + + WatcherField.ENCRYPTION_KEY_SETTING.getKey() + + " " + + systemKeyPath + + "' to import the file.\nAfter importing, the system_key file should be removed from the " + + "filesystem.\nRepeat this on every node in the cluster."; } else { - message = "Encryption of sensitive data requires a key to be placed in the secure setting store. First run the " + - "bin/elasticsearch-syskeygen tool to generate a key file.\nThen run 'bin/elasticsearch-keystore add-file " + - WatcherField.ENCRYPTION_KEY_SETTING.getKey() + " " + - systemKeyPath + "' to import the key into" + - " the secure setting store. Finally, remove the system_key file from the filesystem.\n" + - "Repeat this on every node in the cluster"; + message = "Encryption of sensitive data requires a key to be placed in the secure setting store. First run the " + + "bin/elasticsearch-syskeygen tool to generate a key file.\nThen run 'bin/elasticsearch-keystore add-file " + + WatcherField.ENCRYPTION_KEY_SETTING.getKey() + + " " + + systemKeyPath + + "' to import the key into" + + " the secure setting store. Finally, remove the system_key file from the filesystem.\n" + + "Repeat this on every node in the cluster"; } return BootstrapCheckResult.failure(message); } else { diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/Watcher.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/Watcher.java index f682fa9e0bb1c..0b770f857cc8f 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/Watcher.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/Watcher.java @@ -34,10 +34,8 @@ import org.elasticsearch.common.settings.SettingsFilter; import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.EsExecutors; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.env.Environment; import org.elasticsearch.env.NodeEnvironment; @@ -58,6 +56,8 @@ import org.elasticsearch.threadpool.FixedExecutorBuilder; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.watcher.ResourceWatcherService; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.XPackPlugin; import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.xpack.core.action.XPackInfoFeatureAction; @@ -204,35 +204,64 @@ import static java.util.Collections.emptyList; import static org.elasticsearch.common.settings.Setting.Property.NodeScope; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.index.mapper.MapperService.SINGLE_MAPPING_NAME; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.xpack.core.ClientHelper.WATCHER_ORIGIN; public class Watcher extends Plugin implements SystemIndexPlugin, ScriptPlugin, ReloadablePlugin { // This setting is only here for backward compatibility reasons as 6.x indices made use of it. It can be removed in 8.x. @Deprecated - public static final Setting INDEX_WATCHER_TEMPLATE_VERSION_SETTING = - new Setting<>("index.xpack.watcher.template.version", "", Function.identity(), Setting.Property.IndexScope); - public static final Setting ENCRYPT_SENSITIVE_DATA_SETTING = - Setting.boolSetting("xpack.watcher.encrypt_sensitive_data", false, Setting.Property.NodeScope); - public static final Setting MAX_STOP_TIMEOUT_SETTING = - Setting.timeSetting("xpack.watcher.stop.timeout", TimeValue.timeValueSeconds(30), Setting.Property.NodeScope); - public static final Setting USE_ILM_INDEX_MANAGEMENT = - Setting.boolSetting("xpack.watcher.use_ilm_index_management", true, NodeScope); - private static final Setting SETTING_BULK_ACTIONS = - Setting.intSetting("xpack.watcher.bulk.actions", 1, 1, 10000, NodeScope); - private static final Setting SETTING_BULK_CONCURRENT_REQUESTS = - Setting.intSetting("xpack.watcher.bulk.concurrent_requests", 0, 0, 20, NodeScope); - private static final Setting SETTING_BULK_FLUSH_INTERVAL = - Setting.timeSetting("xpack.watcher.bulk.flush_interval", TimeValue.timeValueSeconds(1), NodeScope); - private static final Setting SETTING_BULK_SIZE = - Setting.byteSizeSetting("xpack.watcher.bulk.size", new ByteSizeValue(1, ByteSizeUnit.MB), - new ByteSizeValue(1, ByteSizeUnit.MB), new ByteSizeValue(10, ByteSizeUnit.MB), NodeScope); - - public static final ScriptContext SCRIPT_TEMPLATE_CONTEXT - = new ScriptContext<>("xpack_template", TemplateScript.Factory.class, - 200, TimeValue.timeValueMillis(0), false, true); + public static final Setting INDEX_WATCHER_TEMPLATE_VERSION_SETTING = new Setting<>( + "index.xpack.watcher.template.version", + "", + Function.identity(), + Setting.Property.IndexScope + ); + public static final Setting ENCRYPT_SENSITIVE_DATA_SETTING = Setting.boolSetting( + "xpack.watcher.encrypt_sensitive_data", + false, + Setting.Property.NodeScope + ); + public static final Setting MAX_STOP_TIMEOUT_SETTING = Setting.timeSetting( + "xpack.watcher.stop.timeout", + TimeValue.timeValueSeconds(30), + Setting.Property.NodeScope + ); + public static final Setting USE_ILM_INDEX_MANAGEMENT = Setting.boolSetting( + "xpack.watcher.use_ilm_index_management", + true, + NodeScope + ); + private static final Setting SETTING_BULK_ACTIONS = Setting.intSetting("xpack.watcher.bulk.actions", 1, 1, 10000, NodeScope); + private static final Setting SETTING_BULK_CONCURRENT_REQUESTS = Setting.intSetting( + "xpack.watcher.bulk.concurrent_requests", + 0, + 0, + 20, + NodeScope + ); + private static final Setting SETTING_BULK_FLUSH_INTERVAL = Setting.timeSetting( + "xpack.watcher.bulk.flush_interval", + TimeValue.timeValueSeconds(1), + NodeScope + ); + private static final Setting SETTING_BULK_SIZE = Setting.byteSizeSetting( + "xpack.watcher.bulk.size", + new ByteSizeValue(1, ByteSizeUnit.MB), + new ByteSizeValue(1, ByteSizeUnit.MB), + new ByteSizeValue(10, ByteSizeUnit.MB), + NodeScope + ); + + public static final ScriptContext SCRIPT_TEMPLATE_CONTEXT = new ScriptContext<>( + "xpack_template", + TemplateScript.Factory.class, + 200, + TimeValue.timeValueMillis(0), + false, + true + ); private static final Logger logger = LogManager.getLogger(Watcher.class); private WatcherIndexingListener listener; @@ -249,17 +278,32 @@ public Watcher(final Settings settings) { } // overridable by tests - protected SSLService getSslService() { return XPackPlugin.getSharedSslService(); } - protected XPackLicenseState getLicenseState() { return XPackPlugin.getSharedLicenseState(); } - protected Clock getClock() { return Clock.systemUTC(); } + protected SSLService getSslService() { + return XPackPlugin.getSharedSslService(); + } + + protected XPackLicenseState getLicenseState() { + return XPackPlugin.getSharedLicenseState(); + } + + protected Clock getClock() { + return Clock.systemUTC(); + } @Override - public Collection createComponents(Client client, ClusterService clusterService, ThreadPool threadPool, - ResourceWatcherService resourceWatcherService, ScriptService scriptService, - NamedXContentRegistry xContentRegistry, Environment environment, - NodeEnvironment nodeEnvironment, NamedWriteableRegistry namedWriteableRegistry, - IndexNameExpressionResolver expressionResolver, - Supplier repositoriesServiceSupplier) { + public Collection createComponents( + Client client, + ClusterService clusterService, + ThreadPool threadPool, + ResourceWatcherService resourceWatcherService, + ScriptService scriptService, + NamedXContentRegistry xContentRegistry, + Environment environment, + NodeEnvironment nodeEnvironment, + NamedWriteableRegistry namedWriteableRegistry, + IndexNameExpressionResolver expressionResolver, + Supplier repositoriesServiceSupplier + ) { if (enabled == false) { return Collections.emptyList(); } @@ -275,8 +319,13 @@ public Collection createComponents(Client client, ClusterService cluster throw new UncheckedIOException(e); } - WatcherIndexTemplateRegistry templateRegistry = new WatcherIndexTemplateRegistry(environment.settings(), - clusterService, threadPool, client, xContentRegistry); + WatcherIndexTemplateRegistry templateRegistry = new WatcherIndexTemplateRegistry( + environment.settings(), + clusterService, + threadPool, + client, + xContentRegistry + ); templateRegistry.initialize(); final SSLService sslService = getSslService(); @@ -298,23 +347,37 @@ public Collection createComponents(Client client, ClusterService cluster Map> emailAttachmentParsers = new HashMap<>(); emailAttachmentParsers.put(HttpEmailAttachementParser.TYPE, new HttpEmailAttachementParser(httpClient, templateEngine)); emailAttachmentParsers.put(DataAttachmentParser.TYPE, new DataAttachmentParser()); - emailAttachmentParsers.put(ReportingAttachmentParser.TYPE, - new ReportingAttachmentParser(settings, httpClient, templateEngine, clusterService.getClusterSettings())); + emailAttachmentParsers.put( + ReportingAttachmentParser.TYPE, + new ReportingAttachmentParser(settings, httpClient, templateEngine, clusterService.getClusterSettings()) + ); EmailAttachmentsParser emailAttachmentsParser = new EmailAttachmentsParser(emailAttachmentParsers); // conditions final ConditionRegistry conditionRegistry = new ConditionRegistry( - Map.of( - InternalAlwaysCondition.TYPE, (c, id, p) -> InternalAlwaysCondition.parse(id, p), - NeverCondition.TYPE, (c, id, p) -> NeverCondition.parse(id, p), - ArrayCompareCondition.TYPE, ArrayCompareCondition::parse, - CompareCondition.TYPE, CompareCondition::parse, - ScriptCondition.TYPE, (c, id, p) -> ScriptCondition.parse(scriptService, id, p)), - getClock()); - final TransformRegistry transformRegistry = new TransformRegistry(Map.of( - ScriptTransform.TYPE, new ScriptTransformFactory(scriptService), - SearchTransform.TYPE, new SearchTransformFactory(settings, client, xContentRegistry, scriptService))); + Map.of( + InternalAlwaysCondition.TYPE, + (c, id, p) -> InternalAlwaysCondition.parse(id, p), + NeverCondition.TYPE, + (c, id, p) -> NeverCondition.parse(id, p), + ArrayCompareCondition.TYPE, + ArrayCompareCondition::parse, + CompareCondition.TYPE, + CompareCondition::parse, + ScriptCondition.TYPE, + (c, id, p) -> ScriptCondition.parse(scriptService, id, p) + ), + getClock() + ); + final TransformRegistry transformRegistry = new TransformRegistry( + Map.of( + ScriptTransform.TYPE, + new ScriptTransformFactory(scriptService), + SearchTransform.TYPE, + new SearchTransformFactory(settings, client, xContentRegistry, scriptService) + ) + ); // actions final Map actionFactoryMap = new HashMap<>(); @@ -325,8 +388,13 @@ ScriptTransform.TYPE, new ScriptTransformFactory(scriptService), actionFactoryMap.put(JiraAction.TYPE, new JiraActionFactory(templateEngine, jiraService)); actionFactoryMap.put(SlackAction.TYPE, new SlackActionFactory(templateEngine, slackService)); actionFactoryMap.put(PagerDutyAction.TYPE, new PagerDutyActionFactory(templateEngine, pagerDutyService)); - final ActionRegistry registry = new ActionRegistry(actionFactoryMap, conditionRegistry, transformRegistry, getClock(), - getLicenseState()); + final ActionRegistry registry = new ActionRegistry( + actionFactoryMap, + conditionRegistry, + transformRegistry, + getClock(), + getLicenseState() + ); // inputs final Map> inputFactories = new HashMap<>(); @@ -340,8 +408,7 @@ ScriptTransform.TYPE, new ScriptTransformFactory(scriptService), bulkProcessor = BulkProcessor.builder(new OriginSettingClient(client, WATCHER_ORIGIN)::bulk, new BulkProcessor.Listener() { @Override - public void beforeBulk(long executionId, BulkRequest request) { - } + public void beforeBulk(long executionId, BulkRequest request) {} @Override public void afterBulk(long executionId, BulkRequest request, BulkResponse response) { @@ -356,13 +423,19 @@ public void afterBulk(long executionId, BulkRequest request, BulkResponse respon .collect(Collectors.toMap(BulkItemResponse::getId, BulkItemResponse::getFailureMessage)); if (triggeredFailures.isEmpty() == false) { String failure = triggeredFailures.values().stream().collect(Collectors.joining(", ")); - logger.error("triggered watches could not be deleted {}, failure [{}]", - triggeredFailures.keySet(), Strings.substring(failure, 0, 2000)); + logger.error( + "triggered watches could not be deleted {}, failure [{}]", + triggeredFailures.keySet(), + Strings.substring(failure, 0, 2000) + ); } if (historyFailures.isEmpty() == false) { String failure = historyFailures.values().stream().collect(Collectors.joining(", ")); - logger.error("watch history could not be written {}, failure [{}]", - historyFailures.keySet(), Strings.substring(failure, 0, 2000)); + logger.error( + "watch history could not be written {}, failure [{}]", + historyFailures.keySet(), + Strings.substring(failure, 0, 2000) + ); } Map overwrittenIds = Arrays.stream(response.getItems()) @@ -372,8 +445,11 @@ public void afterBulk(long executionId, BulkRequest request, BulkResponse respon .collect(Collectors.toMap(BulkItemResponse::getId, BulkItemResponse::getFailureMessage)); if (overwrittenIds.isEmpty() == false) { String failure = overwrittenIds.values().stream().collect(Collectors.joining(", ")); - logger.info("overwrote watch history entries {}, possible second execution of a triggered watch, failure [{}]", - overwrittenIds.keySet(), Strings.substring(failure, 0, 2000)); + logger.info( + "overwrote watch history entries {}, possible second execution of a triggered watch, failure [{}]", + overwrittenIds.keySet(), + Strings.substring(failure, 0, 2000) + ); } } } @@ -413,30 +489,58 @@ public void afterBulk(long executionId, BulkRequest request, Throwable failure) final TriggeredWatch.Parser triggeredWatchParser = new TriggeredWatch.Parser(triggerService); final TriggeredWatchStore triggeredWatchStore = new TriggeredWatchStore(settings, client, triggeredWatchParser, bulkProcessor); - final WatcherSearchTemplateService watcherSearchTemplateService = - new WatcherSearchTemplateService(scriptService, xContentRegistry); + final WatcherSearchTemplateService watcherSearchTemplateService = new WatcherSearchTemplateService(scriptService, xContentRegistry); final WatchExecutor watchExecutor = getWatchExecutor(threadPool); final WatchParser watchParser = new WatchParser(triggerService, registry, inputRegistry, cryptoService, getClock()); - final ExecutionService executionService = new ExecutionService(settings, historyStore, triggeredWatchStore, watchExecutor, - getClock(), watchParser, clusterService, client, threadPool.generic()); + final ExecutionService executionService = new ExecutionService( + settings, + historyStore, + triggeredWatchStore, + watchExecutor, + getClock(), + watchParser, + clusterService, + client, + threadPool.generic() + ); final Consumer> triggerEngineListener = getTriggerEngineListener(executionService); triggerService.register(triggerEngineListener); - WatcherService watcherService = new WatcherService(settings, triggerService, triggeredWatchStore, executionService, - watchParser, client); + WatcherService watcherService = new WatcherService( + settings, + triggerService, + triggeredWatchStore, + executionService, + watchParser, + client + ); - final WatcherLifeCycleService watcherLifeCycleService = - new WatcherLifeCycleService(clusterService, watcherService); + final WatcherLifeCycleService watcherLifeCycleService = new WatcherLifeCycleService(clusterService, watcherService); listener = new WatcherIndexingListener(watchParser, getClock(), triggerService, watcherLifeCycleService.getState()); clusterService.addListener(listener); // note: clock is needed here until actions can be constructed directly instead of by guice - return Arrays.asList(new ClockHolder(getClock()), registry, inputRegistry, historyStore, triggerService, triggeredWatchParser, - watcherLifeCycleService, executionService, triggerEngineListener, watcherService, watchParser, - configuredTriggerEngine, triggeredWatchStore, watcherSearchTemplateService, slackService, pagerDutyService); + return Arrays.asList( + new ClockHolder(getClock()), + registry, + inputRegistry, + historyStore, + triggerService, + triggeredWatchParser, + watcherLifeCycleService, + executionService, + triggerEngineListener, + watcherService, + watchParser, + configuredTriggerEngine, + triggeredWatchStore, + watcherSearchTemplateService, + slackService, + pagerDutyService + ); } protected TriggerEngine getTriggerEngine(Clock clock, ScheduleRegistry scheduleRegistry) { @@ -499,14 +603,14 @@ public List> getSettings() { @Override public List> getExecutorBuilders(final Settings settings) { if (enabled) { - final FixedExecutorBuilder builder = - new FixedExecutorBuilder( - settings, - InternalWatchExecutor.THREAD_POOL_NAME, - getWatcherThreadPoolSize(settings), - 1000, - "xpack.watcher.thread_pool", - false); + final FixedExecutorBuilder builder = new FixedExecutorBuilder( + settings, + InternalWatchExecutor.THREAD_POOL_NAME, + getWatcherThreadPoolSize(settings), + 1000, + "xpack.watcher.thread_pool", + false + ); return Collections.singletonList(builder); } return Collections.emptyList(); @@ -552,38 +656,47 @@ static int getWatcherThreadPoolSize(final boolean isDataNode, final int allocate if (false == enabled) { return Arrays.asList(usageAction, infoAction); } - return Arrays.asList(new ActionHandler<>(PutWatchAction.INSTANCE, TransportPutWatchAction.class), - new ActionHandler<>(DeleteWatchAction.INSTANCE, TransportDeleteWatchAction.class), - new ActionHandler<>(GetWatchAction.INSTANCE, TransportGetWatchAction.class), - new ActionHandler<>(WatcherStatsAction.INSTANCE, TransportWatcherStatsAction.class), - new ActionHandler<>(AckWatchAction.INSTANCE, TransportAckWatchAction.class), - new ActionHandler<>(ActivateWatchAction.INSTANCE, TransportActivateWatchAction.class), - new ActionHandler<>(WatcherServiceAction.INSTANCE, TransportWatcherServiceAction.class), - new ActionHandler<>(ExecuteWatchAction.INSTANCE, TransportExecuteWatchAction.class), - new ActionHandler<>(QueryWatchesAction.INSTANCE, TransportQueryWatchesAction.class), - usageAction, - infoAction); + return Arrays.asList( + new ActionHandler<>(PutWatchAction.INSTANCE, TransportPutWatchAction.class), + new ActionHandler<>(DeleteWatchAction.INSTANCE, TransportDeleteWatchAction.class), + new ActionHandler<>(GetWatchAction.INSTANCE, TransportGetWatchAction.class), + new ActionHandler<>(WatcherStatsAction.INSTANCE, TransportWatcherStatsAction.class), + new ActionHandler<>(AckWatchAction.INSTANCE, TransportAckWatchAction.class), + new ActionHandler<>(ActivateWatchAction.INSTANCE, TransportActivateWatchAction.class), + new ActionHandler<>(WatcherServiceAction.INSTANCE, TransportWatcherServiceAction.class), + new ActionHandler<>(ExecuteWatchAction.INSTANCE, TransportExecuteWatchAction.class), + new ActionHandler<>(QueryWatchesAction.INSTANCE, TransportQueryWatchesAction.class), + usageAction, + infoAction + ); } @Override - public List getRestHandlers(Settings settings, RestController restController, ClusterSettings clusterSettings, - IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter, IndexNameExpressionResolver indexNameExpressionResolver, - Supplier nodesInCluster) { + public List getRestHandlers( + Settings settings, + RestController restController, + ClusterSettings clusterSettings, + IndexScopedSettings indexScopedSettings, + SettingsFilter settingsFilter, + IndexNameExpressionResolver indexNameExpressionResolver, + Supplier nodesInCluster + ) { if (false == enabled) { return emptyList(); } return Arrays.asList( - new RestPutWatchAction(), - new RestDeleteWatchAction(), - new RestWatcherStatsAction(), - new RestGetWatchAction(), - new RestWatchServiceAction(), - new RestWatchServiceAction.StopRestHandler(), - new RestAckWatchAction(), - new RestActivateWatchAction(), - new DeactivateRestHandler(), - new RestExecuteWatchAction(), - new RestQueryWatchesAction()); + new RestPutWatchAction(), + new RestDeleteWatchAction(), + new RestWatcherStatsAction(), + new RestGetWatchAction(), + new RestWatchServiceAction(), + new RestWatchServiceAction.StopRestHandler(), + new RestAckWatchAction(), + new RestActivateWatchAction(), + new DeactivateRestHandler(), + new RestExecuteWatchAction(), + new RestQueryWatchesAction() + ); } @Override diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/WatcherIndexingListener.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/WatcherIndexingListener.java index 7daac42b9eec6..f2fc6a510b710 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/WatcherIndexingListener.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/WatcherIndexingListener.java @@ -22,10 +22,10 @@ import org.elasticsearch.cluster.routing.ShardRouting; import org.elasticsearch.common.Strings; import org.elasticsearch.common.util.set.Sets; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.engine.Engine; import org.elasticsearch.index.shard.IndexingOperationListener; import org.elasticsearch.index.shard.ShardId; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.watcher.WatcherState; import org.elasticsearch.xpack.core.watcher.watch.Watch; import org.elasticsearch.xpack.watcher.trigger.TriggerService; @@ -115,19 +115,30 @@ public void postIndex(ShardId shardId, Engine.Index operation, Engine.IndexResul ZonedDateTime now = Instant.ofEpochMilli(clock.millis()).atZone(ZoneOffset.UTC); try { - Watch watch = parser.parseWithSecrets(operation.id(), true, operation.source(), now, XContentType.JSON, - operation.getIfSeqNo(), operation.getIfPrimaryTerm()); + Watch watch = parser.parseWithSecrets( + operation.id(), + true, + operation.source(), + now, + XContentType.JSON, + operation.getIfSeqNo(), + operation.getIfPrimaryTerm() + ); ShardAllocationConfiguration shardAllocationConfiguration = configuration.localShards.get(shardId); if (shardAllocationConfiguration == null) { - logger.debug("no distributed watch execution info found for watch [{}] on shard [{}], got configuration for {}", - watch.id(), shardId, configuration.localShards.keySet()); + logger.debug( + "no distributed watch execution info found for watch [{}] on shard [{}], got configuration for {}", + watch.id(), + shardId, + configuration.localShards.keySet() + ); return; } boolean shouldBeTriggered = shardAllocationConfiguration.shouldBeTriggered(watch.id()); WatcherState currentState = watcherState.get(); if (shouldBeTriggered && EnumSet.of(WatcherState.STOPPING, WatcherState.STOPPED).contains(currentState) == false) { - if (watch.status().state().isActive() ) { + if (watch.status().state().isActive()) { logger.debug("adding watch [{}] to trigger service", watch.id()); triggerService.add(watch); } else { @@ -196,8 +207,8 @@ private boolean isWatchDocument(String index) { public void clusterChanged(ClusterChangedEvent event) { // if there is no master node configured in the current state, this node should not try to trigger anything, but consider itself // inactive. the same applies, if there is a cluster block that does not allow writes - if (Strings.isNullOrEmpty(event.state().nodes().getMasterNodeId()) || - event.state().getBlocks().hasGlobalBlockWithLevel(ClusterBlockLevel.WRITE)) { + if (Strings.isNullOrEmpty(event.state().nodes().getMasterNodeId()) + || event.state().getBlocks().hasGlobalBlockWithLevel(ClusterBlockLevel.WRITE)) { configuration = INACTIVE; return; } @@ -240,8 +251,7 @@ private void checkWatchIndexHasChanged(IndexMetadata metadata, ClusterChangedEve * @param localShardRouting List of local shards of that index * @param event The cluster changed event containing the new cluster state */ - private void reloadConfiguration(String watchIndex, List localShardRouting, - ClusterChangedEvent event) { + private void reloadConfiguration(String watchIndex, List localShardRouting, ClusterChangedEvent event) { // changed alias means to always read a new configuration boolean isAliasChanged = watchIndex.equals(configuration.index) == false; if (isAliasChanged || hasShardAllocationIdChanged(watchIndex, event.state())) { @@ -269,10 +279,12 @@ private boolean hasShardAllocationIdChanged(String watchIndex, ClusterState stat // check for different shard ids String localNodeId = state.nodes().getLocalNodeId(); - Set clusterStateLocalShardIds = state.getRoutingNodes().node(localNodeId) - .shardsWithState(watchIndex, STARTED, RELOCATING).stream() - .map(ShardRouting::shardId) - .collect(Collectors.toSet()); + Set clusterStateLocalShardIds = state.getRoutingNodes() + .node(localNodeId) + .shardsWithState(watchIndex, STARTED, RELOCATING) + .stream() + .map(ShardRouting::shardId) + .collect(Collectors.toSet()); Set configuredLocalShardIds = new HashSet<>(configuration.localShards.keySet()); Set differenceSet = Sets.difference(clusterStateLocalShardIds, configuredLocalShardIds); if (differenceSet.isEmpty() == false) { @@ -280,9 +292,12 @@ private boolean hasShardAllocationIdChanged(String watchIndex, ClusterState stat } Map> shards = allStartedRelocatedShards.stream() - .collect(Collectors.groupingBy(ShardRouting::shardId, - Collectors.mapping(sr -> sr.allocationId().getId(), - Collectors.toCollection(ArrayList::new)))); + .collect( + Collectors.groupingBy( + ShardRouting::shardId, + Collectors.mapping(sr -> sr.allocationId().getId(), Collectors.toCollection(ArrayList::new)) + ) + ); // sort the collection, so we have a stable order shards.values().forEach(Collections::sort); @@ -322,11 +337,12 @@ Map getLocalShardAllocationIds(List allocationIds = routingTable.shard(shardId.getId()).getActiveShards() - .stream() - .map(ShardRouting::allocationId) - .map(AllocationId::getId) - .collect(Collectors.toList()); + List allocationIds = routingTable.shard(shardId.getId()) + .getActiveShards() + .stream() + .map(ShardRouting::allocationId) + .map(AllocationId::getId) + .collect(Collectors.toList()); // sort the list so it is stable Collections.sort(allocationIds); diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/WatcherInfoTransportAction.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/WatcherInfoTransportAction.java index 370a7511233eb..e93081359ca92 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/WatcherInfoTransportAction.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/WatcherInfoTransportAction.java @@ -23,8 +23,12 @@ public class WatcherInfoTransportAction extends XPackInfoFeatureTransportAction private final XPackLicenseState licenseState; @Inject - public WatcherInfoTransportAction(TransportService transportService, ActionFilters actionFilters, - Settings settings, XPackLicenseState licenseState) { + public WatcherInfoTransportAction( + TransportService transportService, + ActionFilters actionFilters, + Settings settings, + XPackLicenseState licenseState + ) { super(XPackInfoFeatureAction.WATCHER.name(), transportService, actionFilters); this.enabled = XPackSettings.WATCHER_ENABLED.get(settings); this.licenseState = licenseState; diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/WatcherLifeCycleService.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/WatcherLifeCycleService.java index 3b14a93ff50df..db9e69e69d45d 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/WatcherLifeCycleService.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/WatcherLifeCycleService.java @@ -99,8 +99,7 @@ public void clusterChanged(ClusterChangedEvent event) { boolean isWatcherStoppedManually = isWatcherStoppedManually(event.state()); boolean isStoppedOrStopping = stopStates.contains(this.state.get()); // if this is not a data node, we need to start it ourselves possibly - if (event.state().nodes().getLocalNode().canContainData() == false && - isWatcherStoppedManually == false && isStoppedOrStopping) { + if (event.state().nodes().getLocalNode().canContainData() == false && isWatcherStoppedManually == false && isStoppedOrStopping) { this.state.set(WatcherState.STARTING); watcherService.start(event.state(), () -> this.state.set(WatcherState.STARTED)); return; @@ -111,9 +110,9 @@ public void clusterChanged(ClusterChangedEvent event) { clearAllocationIds(); boolean stopping = this.state.compareAndSet(WatcherState.STARTED, WatcherState.STOPPING); if (stopping) { - //waiting to set state to stopped until after all currently running watches are finished + // waiting to set state to stopped until after all currently running watches are finished watcherService.stop("watcher manually marked to shutdown by cluster state update", () -> { - //only transition from stopping -> stopped (which may not be the case if restarted quickly) + // only transition from stopping -> stopped (which may not be the case if restarted quickly) boolean stopped = state.compareAndSet(WatcherState.STOPPING, WatcherState.STOPPED); if (stopped) { logger.info("watcher has stopped"); @@ -205,7 +204,7 @@ List shardRoutings() { return previousShardRoutings.get(); } - public Supplier getState(){ + public Supplier getState() { return () -> state.get(); } } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/WatcherPainlessExtension.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/WatcherPainlessExtension.java index c77faa4594c01..31e8d323fb951 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/WatcherPainlessExtension.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/WatcherPainlessExtension.java @@ -20,8 +20,10 @@ public class WatcherPainlessExtension implements PainlessExtension { - private static final Whitelist WHITELIST = - WhitelistLoader.loadFromResourceFiles(WatcherPainlessExtension.class, "painless_whitelist.txt"); + private static final Whitelist WHITELIST = WhitelistLoader.loadFromResourceFiles( + WatcherPainlessExtension.class, + "painless_whitelist.txt" + ); @Override public Map, List> getContextWhitelists() { diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/WatcherService.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/WatcherService.java index bc7bb6e94ae31..3bca1615afb43 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/WatcherService.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/WatcherService.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.xpack.watcher; - import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.message.ParameterizedMessage; @@ -26,14 +25,14 @@ import org.elasticsearch.cluster.routing.RoutingNode; import org.elasticsearch.cluster.routing.ShardRouting; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.AbstractRunnable; import org.elasticsearch.common.util.concurrent.EsExecutors; -import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.sort.SortBuilders; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.ClientHelper; import org.elasticsearch.xpack.core.upgrade.UpgradeField; import org.elasticsearch.xpack.core.watcher.WatcherState; @@ -85,8 +84,15 @@ public class WatcherService { private final AtomicLong processedClusterStateVersion = new AtomicLong(0); private final ExecutorService executor; - WatcherService(Settings settings, TriggerService triggerService, TriggeredWatchStore triggeredWatchStore, - ExecutionService executionService, WatchParser parser, Client client, ExecutorService executor) { + WatcherService( + Settings settings, + TriggerService triggerService, + TriggeredWatchStore triggeredWatchStore, + ExecutionService executionService, + WatchParser parser, + Client client, + ExecutorService executor + ) { this.triggerService = triggerService; this.triggeredWatchStore = triggeredWatchStore; this.executionService = executionService; @@ -98,11 +104,30 @@ public class WatcherService { this.executor = executor; } - WatcherService(Settings settings, TriggerService triggerService, TriggeredWatchStore triggeredWatchStore, - ExecutionService executionService, WatchParser parser, Client client) { - this(settings, triggerService, triggeredWatchStore, executionService, parser, client, - EsExecutors.newFixed(LIFECYCLE_THREADPOOL_NAME, 1, 1000, daemonThreadFactory(settings, LIFECYCLE_THREADPOOL_NAME), - client.threadPool().getThreadContext(), false)); + WatcherService( + Settings settings, + TriggerService triggerService, + TriggeredWatchStore triggeredWatchStore, + ExecutionService executionService, + WatchParser parser, + Client client + ) { + this( + settings, + triggerService, + triggeredWatchStore, + executionService, + parser, + client, + EsExecutors.newFixed( + LIFECYCLE_THREADPOOL_NAME, + 1, + 1000, + daemonThreadFactory(settings, LIFECYCLE_THREADPOOL_NAME), + client.threadPool().getThreadContext(), + false + ) + ); } /** @@ -119,15 +144,20 @@ public boolean validate(ClusterState state) { } IndexMetadata watcherIndexMetadata = WatchStoreUtils.getConcreteIndex(Watch.INDEX, state.metadata()); - IndexMetadata triggeredWatchesIndexMetadata = WatchStoreUtils.getConcreteIndex(TriggeredWatchStoreField.INDEX_NAME, - state.metadata()); - boolean isIndexInternalFormatWatchIndex = watcherIndexMetadata == null || - UpgradeField.checkInternalIndexFormat(watcherIndexMetadata); - boolean isIndexInternalFormatTriggeredWatchIndex = triggeredWatchesIndexMetadata == null || - UpgradeField.checkInternalIndexFormat(triggeredWatchesIndexMetadata); + IndexMetadata triggeredWatchesIndexMetadata = WatchStoreUtils.getConcreteIndex( + TriggeredWatchStoreField.INDEX_NAME, + state.metadata() + ); + boolean isIndexInternalFormatWatchIndex = watcherIndexMetadata == null + || UpgradeField.checkInternalIndexFormat(watcherIndexMetadata); + boolean isIndexInternalFormatTriggeredWatchIndex = triggeredWatchesIndexMetadata == null + || UpgradeField.checkInternalIndexFormat(triggeredWatchesIndexMetadata); if (isIndexInternalFormatTriggeredWatchIndex == false || isIndexInternalFormatWatchIndex == false) { - logger.warn("not starting watcher, upgrade API run required: .watches[{}], .triggered_watches[{}]", - isIndexInternalFormatWatchIndex, isIndexInternalFormatTriggeredWatchIndex); + logger.warn( + "not starting watcher, upgrade API run required: .watches[{}], .triggered_watches[{}]", + isIndexInternalFormatWatchIndex, + isIndexInternalFormatTriggeredWatchIndex + ); return false; } @@ -137,8 +167,9 @@ public boolean validate(ClusterState state) { return false; } - return watcherIndexMetadata == null || (watcherIndexMetadata.getState() == IndexMetadata.State.OPEN && - state.routingTable().index(watcherIndexMetadata.getIndex()).allPrimaryShardsActive()); + return watcherIndexMetadata == null + || (watcherIndexMetadata.getState() == IndexMetadata.State.OPEN + && state.routingTable().index(watcherIndexMetadata.getIndex()).allPrimaryShardsActive()); } catch (IllegalStateException e) { logger.debug("error validating to start watcher", e); return false; @@ -195,8 +226,7 @@ void reload(ClusterState state, String reason) { int cancelledTaskCount = executionService.clearExecutionsAndQueue(() -> {}); logger.info("reloading watcher, reason [{}], cancelled [{}] queued tasks", reason, cancelledTaskCount); - executor.execute(wrapWatcherService(() -> reloadInner(state, reason, false), - e -> logger.error("error reloading watcher", e))); + executor.execute(wrapWatcherService(() -> reloadInner(state, reason, false), e -> logger.error("error reloading watcher", e))); } /** @@ -209,11 +239,10 @@ public void start(ClusterState state, Runnable postWatchesLoadedCallback) { executionService.unPause(); processedClusterStateVersion.set(state.getVersion()); executor.execute(wrapWatcherService(() -> { - if (reloadInner(state, "starting", true)) { - postWatchesLoadedCallback.run(); - } - }, - e -> logger.error("error starting watcher", e))); + if (reloadInner(state, "starting", true)) { + postWatchesLoadedCallback.run(); + } + }, e -> logger.error("error starting watcher", e))); } /** @@ -227,8 +256,11 @@ public void start(ClusterState state, Runnable postWatchesLoadedCallback) { private synchronized boolean reloadInner(ClusterState state, String reason, boolean loadTriggeredWatches) { // exit early if another thread has come in between if (processedClusterStateVersion.get() != state.getVersion()) { - logger.debug("watch service has not been reloaded for state [{}], another reload for state [{}] in progress", - state.getVersion(), processedClusterStateVersion.get()); + logger.debug( + "watch service has not been reloaded for state [{}], another reload for state [{}] in progress", + state.getVersion(), + processedClusterStateVersion.get() + ); return false; } @@ -251,8 +283,11 @@ private synchronized boolean reloadInner(ClusterState state, String reason, bool logger.debug("watch service has been reloaded, reason [{}]", reason); return true; } else { - logger.debug("watch service has not been reloaded for state [{}], another reload for state [{}] in progress", - state.getVersion(), processedClusterStateVersion.get()); + logger.debug( + "watch service has not been reloaded for state [{}], another reload for state [{}] in progress", + state.getVersion(), + processedClusterStateVersion.get() + ); return false; } } @@ -281,7 +316,9 @@ private Collection loadWatches(ClusterState clusterState) { SearchResponse response = null; List watches = new ArrayList<>(); try { - RefreshResponse refreshResponse = client.admin().indices().refresh(new RefreshRequest(INDEX)) + RefreshResponse refreshResponse = client.admin() + .indices() + .refresh(new RefreshRequest(INDEX)) .actionGet(TimeValue.timeValueSeconds(5)); if (refreshResponse.getSuccessfulShards() < indexMetadata.getNumberOfShards()) { throw illegalState("not all required shards have been refreshed"); @@ -299,13 +336,9 @@ private Collection loadWatches(ClusterState clusterState) { // find out all allocation ids List watchIndexShardRoutings = clusterState.getRoutingTable().allShards(watchIndexName); - SearchRequest searchRequest = new SearchRequest(INDEX) - .scroll(scrollTimeout) + SearchRequest searchRequest = new SearchRequest(INDEX).scroll(scrollTimeout) .preference(Preference.ONLY_LOCAL.toString()) - .source(new SearchSourceBuilder() - .size(scrollSize) - .sort(SortBuilders.fieldSort("_doc")) - .seqNoAndPrimaryTerm(true)); + .source(new SearchSourceBuilder().size(scrollSize).sort(SortBuilders.fieldSort("_doc")).seqNoAndPrimaryTerm(true)); response = client.search(searchRequest).actionGet(defaultSearchTimeout); if (response.getTotalShards() != response.getSuccessfulShards()) { @@ -320,8 +353,10 @@ private Collection loadWatches(ClusterState clusterState) { for (ShardRouting localShardRouting : localShards) { List sortedAllocationIds = watchIndexShardRoutings.stream() .filter(sr -> localShardRouting.getId() == sr.getId()) - .map(ShardRouting::allocationId).filter(Objects::nonNull) - .map(AllocationId::getId).filter(Objects::nonNull) + .map(ShardRouting::allocationId) + .filter(Objects::nonNull) + .map(AllocationId::getId) + .filter(Objects::nonNull) .sorted() .collect(Collectors.toList()); @@ -353,8 +388,13 @@ private Collection loadWatches(ClusterState clusterState) { watches.add(watch); } } catch (Exception e) { - logger.error((org.apache.logging.log4j.util.Supplier) - () -> new ParameterizedMessage("couldn't load watch [{}], ignoring it...", id), e); + logger.error( + (org.apache.logging.log4j.util.Supplier) () -> new ParameterizedMessage( + "couldn't load watch [{}], ignoring it...", + id + ), + e + ); } } SearchScrollRequest request = new SearchScrollRequest(response.getScrollId()); diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/WatcherUsageTransportAction.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/WatcherUsageTransportAction.java index 78a8b2b8e78db..df1ff1f4fb3c9 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/WatcherUsageTransportAction.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/WatcherUsageTransportAction.java @@ -44,22 +44,38 @@ public class WatcherUsageTransportAction extends XPackUsageFeatureTransportActio private final Client client; @Inject - public WatcherUsageTransportAction(TransportService transportService, ClusterService clusterService, ThreadPool threadPool, - ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver, - Settings settings, XPackLicenseState licenseState, Client client) { - super(XPackUsageFeatureAction.WATCHER.name(), transportService, clusterService, threadPool, actionFilters, - indexNameExpressionResolver); + public WatcherUsageTransportAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + IndexNameExpressionResolver indexNameExpressionResolver, + Settings settings, + XPackLicenseState licenseState, + Client client + ) { + super( + XPackUsageFeatureAction.WATCHER.name(), + transportService, + clusterService, + threadPool, + actionFilters, + indexNameExpressionResolver + ); this.enabled = XPackSettings.WATCHER_ENABLED.get(settings); this.licenseState = licenseState; this.client = client; } @Override - protected void masterOperation(Task task, XPackUsageRequest request, ClusterState state, - ActionListener listener) { + protected void masterOperation( + Task task, + XPackUsageRequest request, + ClusterState state, + ActionListener listener + ) { if (enabled) { - try (ThreadContext.StoredContext ignore = - client.threadPool().getThreadContext().stashWithOrigin(WATCHER_ORIGIN)) { + try (ThreadContext.StoredContext ignore = client.threadPool().getThreadContext().stashWithOrigin(WATCHER_ORIGIN)) { WatcherStatsRequest statsRequest = new WatcherStatsRequest(); statsRequest.includeStats(true); statsRequest.setParentTask(clusterService.localNode().getId(), task.getId()); @@ -71,13 +87,19 @@ protected void masterOperation(Task task, XPackUsageRequest request, ClusterStat .collect(Collectors.toList()); Counters mergedCounters = Counters.merge(countersPerNode); WatcherFeatureSetUsage usage = new WatcherFeatureSetUsage( - WatcherField.WATCHER_FEATURE.checkWithoutTracking(licenseState), true, mergedCounters.toNestedMap()); + WatcherField.WATCHER_FEATURE.checkWithoutTracking(licenseState), + true, + mergedCounters.toNestedMap() + ); listener.onResponse(new XPackUsageFeatureResponse(usage)); }, listener::onFailure)); } } else { WatcherFeatureSetUsage usage = new WatcherFeatureSetUsage( - WatcherField.WATCHER_FEATURE.checkWithoutTracking(licenseState), false, Collections.emptyMap()); + WatcherField.WATCHER_FEATURE.checkWithoutTracking(licenseState), + false, + Collections.emptyMap() + ); listener.onResponse(new XPackUsageFeatureResponse(usage)); } } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/ActionBuilders.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/ActionBuilders.java index 837f318ce1e43..b09230d0d2118 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/ActionBuilders.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/ActionBuilders.java @@ -23,8 +23,7 @@ public final class ActionBuilders { - private ActionBuilders() { - } + private ActionBuilders() {} public static EmailAction.Builder emailAction(EmailTemplate.Builder email) { return emailAction(email.build()); diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/email/EmailAction.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/email/EmailAction.java index 72a55f2ff6b92..245139a729783 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/email/EmailAction.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/email/EmailAction.java @@ -33,14 +33,25 @@ public class EmailAction implements Action { public static final String TYPE = "email"; private final EmailTemplate email; - @Nullable private final String account; - @Nullable private final Authentication auth; - @Nullable private final Profile profile; - @Nullable private final DataAttachment dataAttachment; - @Nullable private final EmailAttachments emailAttachments; - - public EmailAction(EmailTemplate email, @Nullable String account, @Nullable Authentication auth, @Nullable Profile profile, - @Nullable DataAttachment dataAttachment, @Nullable EmailAttachments emailAttachments) { + @Nullable + private final String account; + @Nullable + private final Authentication auth; + @Nullable + private final Profile profile; + @Nullable + private final DataAttachment dataAttachment; + @Nullable + private final EmailAttachments emailAttachments; + + public EmailAction( + EmailTemplate email, + @Nullable String account, + @Nullable Authentication auth, + @Nullable Profile profile, + @Nullable DataAttachment dataAttachment, + @Nullable EmailAttachments emailAttachments + ) { this.email = email; this.account = account; this.auth = auth; @@ -85,12 +96,12 @@ public boolean equals(Object o) { EmailAction action = (EmailAction) o; - return Objects.equals(email, action.email) && - Objects.equals(account, action.account) && - Objects.equals(auth, action.auth) && - Objects.equals(profile, action.profile) && - Objects.equals(emailAttachments, action.emailAttachments) && - Objects.equals(dataAttachment, action.dataAttachment); + return Objects.equals(email, action.email) + && Objects.equals(account, action.account) + && Objects.equals(auth, action.auth) + && Objects.equals(profile, action.profile) + && Objects.equals(emailAttachments, action.emailAttachments) + && Objects.equals(dataAttachment, action.dataAttachment); } @Override @@ -125,8 +136,8 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws return builder.endObject(); } - public static EmailAction parse(String watchId, String actionId, XContentParser parser, - EmailAttachmentsParser emailAttachmentsParser) throws IOException { + public static EmailAction parse(String watchId, String actionId, XContentParser parser, EmailAttachmentsParser emailAttachmentsParser) + throws IOException { EmailTemplate.Parser emailParser = new EmailTemplate.Parser(); String account = null; String user = null; @@ -144,8 +155,14 @@ public static EmailAction parse(String watchId, String actionId, XContentParser try { dataAttachment = DataAttachment.parse(parser); } catch (IOException ioe) { - throw new ElasticsearchParseException("could not parse [{}] action [{}/{}]. failed to parse data attachment field " + - "[{}]", ioe, TYPE, watchId, actionId, currentFieldName); + throw new ElasticsearchParseException( + "could not parse [{}] action [{}/{}]. failed to parse data attachment field " + "[{}]", + ioe, + TYPE, + watchId, + actionId, + currentFieldName + ); } } else if (Field.ATTACHMENTS.match(currentFieldName, parser.getDeprecationHandler())) { attachments = emailAttachmentsParser.parse(parser); @@ -164,12 +181,22 @@ public static EmailAction parse(String watchId, String actionId, XContentParser throw new ElasticsearchParseException("could not parse [{}] action [{}/{}]", TYPE, watchId, actionId, iae); } } else { - throw new ElasticsearchParseException("could not parse [{}] action [{}/{}]. unexpected string field [{}]", TYPE, - watchId, actionId, currentFieldName); + throw new ElasticsearchParseException( + "could not parse [{}] action [{}/{}]. unexpected string field [{}]", + TYPE, + watchId, + actionId, + currentFieldName + ); } } else { - throw new ElasticsearchParseException("could not parse [{}] action [{}/{}]. unexpected token [{}]", TYPE, watchId, - actionId, token); + throw new ElasticsearchParseException( + "could not parse [{}] action [{}/{}]. unexpected token [{}]", + TYPE, + watchId, + actionId, + token + ); } } } @@ -214,9 +241,9 @@ public Email email() { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { return builder.startObject(type) - .field(Field.ACCOUNT.getPreferredName(), account) - .field(Field.MESSAGE.getPreferredName(), email, params) - .endObject(); + .field(Field.ACCOUNT.getPreferredName(), account) + .field(Field.MESSAGE.getPreferredName(), email, params) + .endObject(); } } @@ -235,9 +262,7 @@ public Email email() { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder.startObject(type) - .field(Field.MESSAGE.getPreferredName(), email, params) - .endObject(); + return builder.startObject(type).field(Field.MESSAGE.getPreferredName(), email, params).endObject(); } } } @@ -245,11 +270,16 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws public static class Builder implements Action.Builder { final EmailTemplate email; - @Nullable String account; - @Nullable Authentication auth; - @Nullable Profile profile; - @Nullable DataAttachment dataAttachment; - @Nullable EmailAttachments attachments; + @Nullable + String account; + @Nullable + Authentication auth; + @Nullable + Profile profile; + @Nullable + DataAttachment dataAttachment; + @Nullable + EmailAttachments attachments; private Builder(EmailTemplate email) { this.email = email; diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/email/EmailActionFactory.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/email/EmailActionFactory.java index 6c1200eaa6e5a..3763de73df0fc 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/email/EmailActionFactory.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/email/EmailActionFactory.java @@ -24,8 +24,12 @@ public class EmailActionFactory extends ActionFactory { private final HtmlSanitizer htmlSanitizer; private final EmailAttachmentsParser emailAttachmentsParser; - public EmailActionFactory(Settings settings, EmailService emailService, TextTemplateEngine templateEngine, - EmailAttachmentsParser emailAttachmentsParser) { + public EmailActionFactory( + Settings settings, + EmailService emailService, + TextTemplateEngine templateEngine, + EmailAttachmentsParser emailAttachmentsParser + ) { super(LogManager.getLogger(ExecutableEmailAction.class)); this.emailService = emailService; this.templateEngine = templateEngine; @@ -35,8 +39,14 @@ public EmailActionFactory(Settings settings, EmailService emailService, TextTemp @Override public ExecutableEmailAction parseExecutable(String watchId, String actionId, XContentParser parser) throws IOException { - return new ExecutableEmailAction(EmailAction.parse(watchId, actionId, parser, emailAttachmentsParser), - actionLogger, emailService, templateEngine, htmlSanitizer, emailAttachmentsParser.getParsers()); + return new ExecutableEmailAction( + EmailAction.parse(watchId, actionId, parser, emailAttachmentsParser), + actionLogger, + emailService, + templateEngine, + htmlSanitizer, + emailAttachmentsParser.getParsers() + ); } } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/email/ExecutableEmailAction.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/email/ExecutableEmailAction.java index 7b00229337ecd..c0710287cf526 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/email/ExecutableEmailAction.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/email/ExecutableEmailAction.java @@ -66,14 +66,17 @@ public Action.Result execute(String actionId, WatchExecutionContext ctx, Payload if (action.getAttachments() != null && action.getAttachments().getAttachments().size() > 0) { for (EmailAttachment emailAttachment : action.getAttachments().getAttachments()) { @SuppressWarnings("unchecked") - EmailAttachmentParser parser = - (EmailAttachmentParser) emailAttachmentParsers.get(emailAttachment.type()); + EmailAttachmentParser parser = (EmailAttachmentParser) emailAttachmentParsers.get( + emailAttachment.type() + ); try { Attachment attachment = parser.toAttachment(ctx, payload, emailAttachment); attachments.put(attachment.id(), attachment); } catch (ElasticsearchException | IOException e) { logger().error( - (Supplier) () -> new ParameterizedMessage("failed to execute action [{}/{}]", ctx.watch().id(), actionId), e); + (Supplier) () -> new ParameterizedMessage("failed to execute action [{}/{}]", ctx.watch().id(), actionId), + e + ); return new EmailAction.Result.FailureWithException(action.type(), e); } } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/index/ExecutableIndexAction.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/index/ExecutableIndexAction.java index 7e247c432af5f..4f0fd0ec37039 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/index/ExecutableIndexAction.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/index/ExecutableIndexAction.java @@ -16,9 +16,9 @@ import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.client.Client; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.xpack.core.ClientHelper; import org.elasticsearch.xpack.core.watcher.actions.Action; import org.elasticsearch.xpack.core.watcher.actions.Action.Result.Status; @@ -48,8 +48,13 @@ public class ExecutableIndexAction extends ExecutableAction { private final TimeValue indexDefaultTimeout; private final TimeValue bulkDefaultTimeout; - public ExecutableIndexAction(IndexAction action, Logger logger, Client client, - TimeValue indexDefaultTimeout, TimeValue bulkDefaultTimeout) { + public ExecutableIndexAction( + IndexAction action, + Logger logger, + Client client, + TimeValue indexDefaultTimeout, + TimeValue bulkDefaultTimeout + ) { super(action, logger); this.client = client; this.indexDefaultTimeout = action.timeout != null ? action.timeout : indexDefaultTimeout; @@ -71,8 +76,12 @@ public Action.Result execute(String actionId, WatchExecutionContext ctx, Payload if (doc instanceof Map) { data = (Map) doc; } else { - throw illegalState("could not execute action [{}] of watch [{}]. failed to index payload data." + - "[_data] field must either hold a Map or an List/Array of Maps", actionId, ctx.watch().id()); + throw illegalState( + "could not execute action [{}] of watch [{}]. failed to index payload data." + + "[_data] field must either hold a Map or an List/Array of Maps", + actionId, + ctx.watch().id() + ); } } @@ -85,7 +94,7 @@ public Action.Result execute(String actionId, WatchExecutionContext ctx, Payload } indexRequest.index(getField(actionId, ctx.id().watchId(), "index", data, INDEX_FIELD, action.index)); - indexRequest.id(getField(actionId, ctx.id().watchId(), "id",data, ID_FIELD, action.docId)); + indexRequest.id(getField(actionId, ctx.id().watchId(), "id", data, ID_FIELD, action.docId)); if (action.opType != null) { indexRequest.opType(action.opType); } @@ -97,13 +106,21 @@ public Action.Result execute(String actionId, WatchExecutionContext ctx, Payload } if (ctx.simulateAction(actionId)) { - return new IndexAction.Simulated(indexRequest.index(), indexRequest.id(), - action.refreshPolicy, new XContentSource(indexRequest.source(), XContentType.JSON)); + return new IndexAction.Simulated( + indexRequest.index(), + indexRequest.id(), + action.refreshPolicy, + new XContentSource(indexRequest.source(), XContentType.JSON) + ); } ClientHelper.assertNoAuthorizationHeader(ctx.watch().status().getHeaders()); - IndexResponse response = ClientHelper.executeWithHeaders(ctx.watch().status().getHeaders(), ClientHelper.WATCHER_ORIGIN, client, - () -> client.index(indexRequest).actionGet(indexDefaultTimeout)); + IndexResponse response = ClientHelper.executeWithHeaders( + ctx.watch().status().getHeaders(), + ClientHelper.WATCHER_ORIGIN, + client, + () -> client.index(indexRequest).actionGet(indexDefaultTimeout) + ); try (XContentBuilder builder = jsonBuilder()) { indexResponseToXContent(builder, response); bytesReference = BytesReference.bytes(builder); @@ -123,8 +140,12 @@ Action.Result indexBulk(Iterable list, String actionId, WatchExecutionContext for (Object item : list) { if ((item instanceof Map) == false) { - throw illegalState("could not execute action [{}] of watch [{}]. failed to index payload data. " + - "[_data] field must either hold a Map or an List/Array of Maps", actionId, ctx.watch().id()); + throw illegalState( + "could not execute action [{}] of watch [{}]. failed to index payload data. " + + "[_data] field must either hold a Map or an List/Array of Maps", + actionId, + ctx.watch().id() + ); } @SuppressWarnings("unchecked") @@ -135,7 +156,7 @@ Action.Result indexBulk(Iterable list, String actionId, WatchExecutionContext IndexRequest indexRequest = new IndexRequest(); indexRequest.index(getField(actionId, ctx.id().watchId(), "index", doc, INDEX_FIELD, action.index)); - indexRequest.id(getField(actionId, ctx.id().watchId(), "id",doc, ID_FIELD, action.docId)); + indexRequest.id(getField(actionId, ctx.id().watchId(), "id", doc, ID_FIELD, action.docId)); if (action.opType != null) { indexRequest.opType(action.opType); } @@ -157,14 +178,22 @@ Action.Result indexBulk(Iterable list, String actionId, WatchExecutionContext } builder.endArray(); - return new IndexAction.Simulated("", "", - action.refreshPolicy, new XContentSource(BytesReference.bytes(builder), XContentType.JSON)); + return new IndexAction.Simulated( + "", + "", + action.refreshPolicy, + new XContentSource(BytesReference.bytes(builder), XContentType.JSON) + ); } } ClientHelper.assertNoAuthorizationHeader(ctx.watch().status().getHeaders()); - BulkResponse bulkResponse = ClientHelper.executeWithHeaders(ctx.watch().status().getHeaders(), ClientHelper.WATCHER_ORIGIN, client, - () -> client.bulk(bulkRequest).actionGet(bulkDefaultTimeout)); + BulkResponse bulkResponse = ClientHelper.executeWithHeaders( + ctx.watch().status().getHeaders(), + ClientHelper.WATCHER_ORIGIN, + client, + () -> client.bulk(bulkRequest).actionGet(bulkDefaultTimeout) + ); try (XContentBuilder jsonBuilder = jsonBuilder().startArray()) { for (BulkItemResponse item : bulkResponse) { itemResponseToXContent(jsonBuilder, item); @@ -178,8 +207,10 @@ Action.Result indexBulk(Iterable list, String actionId, WatchExecutionContext } else if (failures == bulkResponse.getItems().length) { return new IndexAction.Result(Status.FAILURE, new XContentSource(BytesReference.bytes(jsonBuilder), XContentType.JSON)); } else { - return new IndexAction.Result(Status.PARTIAL_FAILURE, - new XContentSource(BytesReference.bytes(jsonBuilder), XContentType.JSON)); + return new IndexAction.Result( + Status.PARTIAL_FAILURE, + new XContentSource(BytesReference.bytes(jsonBuilder), XContentType.JSON) + ); } } } @@ -199,9 +230,15 @@ private String getField(String actionId, String watchId, String name, Map mutableMap(Map data) { private static void itemResponseToXContent(XContentBuilder builder, BulkItemResponse item) throws IOException { if (item.isFailed()) { builder.startObject() - .field("failed", item.isFailed()) - .field("message", item.getFailureMessage()) - .field("id", item.getId()) - .field("index", item.getIndex()) - .endObject(); + .field("failed", item.isFailed()) + .field("message", item.getFailureMessage()) + .field("id", item.getId()) + .field("index", item.getIndex()) + .endObject(); } else { indexResponseToXContent(builder, item.getResponse()); } @@ -236,13 +273,11 @@ private static void itemResponseToXContent(XContentBuilder builder, BulkItemResp static void indexResponseToXContent(XContentBuilder builder, IndexResponse response) throws IOException { builder.startObject() - .field("created", response.getResult() == DocWriteResponse.Result.CREATED) - .field("result", response.getResult().getLowercase()) - .field("id", response.getId()) - .field("version", response.getVersion()) - .field("index", response.getIndex()) - .endObject(); + .field("created", response.getResult() == DocWriteResponse.Result.CREATED) + .field("result", response.getResult().getLowercase()) + .field("id", response.getId()) + .field("version", response.getVersion()) + .field("index", response.getIndex()) + .endObject(); } } - - diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/index/IndexAction.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/index/IndexAction.java index 19a8dd8f91f10..c3cabcc19eb85 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/index/IndexAction.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/index/IndexAction.java @@ -9,10 +9,10 @@ import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.action.DocWriteRequest; import org.elasticsearch.action.support.WriteRequest.RefreshPolicy; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.time.DateUtils; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.watcher.actions.Action; @@ -30,17 +30,30 @@ public class IndexAction implements Action { public static final String TYPE = "index"; - @Nullable final String index; - @Nullable final String docId; - @Nullable final DocWriteRequest.OpType opType; - @Nullable final String executionTimeField; - @Nullable final TimeValue timeout; - @Nullable final ZoneId dynamicNameTimeZone; - @Nullable final RefreshPolicy refreshPolicy; - - public IndexAction(@Nullable String index, @Nullable String docId, @Nullable DocWriteRequest.OpType opType, - @Nullable String executionTimeField, @Nullable TimeValue timeout, @Nullable ZoneId dynamicNameTimeZone, - @Nullable RefreshPolicy refreshPolicy) { + @Nullable + final String index; + @Nullable + final String docId; + @Nullable + final DocWriteRequest.OpType opType; + @Nullable + final String executionTimeField; + @Nullable + final TimeValue timeout; + @Nullable + final ZoneId dynamicNameTimeZone; + @Nullable + final RefreshPolicy refreshPolicy; + + public IndexAction( + @Nullable String index, + @Nullable String docId, + @Nullable DocWriteRequest.OpType opType, + @Nullable String executionTimeField, + @Nullable TimeValue timeout, + @Nullable ZoneId dynamicNameTimeZone, + @Nullable RefreshPolicy refreshPolicy + ) { this.index = index; this.docId = docId; this.opType = opType; @@ -87,12 +100,12 @@ public boolean equals(Object o) { IndexAction that = (IndexAction) o; return Objects.equals(index, that.index) - && Objects.equals(docId, that.docId) - && Objects.equals(opType, that.opType) - && Objects.equals(executionTimeField, that.executionTimeField) - && Objects.equals(timeout, that.timeout) - && Objects.equals(dynamicNameTimeZone, that.dynamicNameTimeZone) - && Objects.equals(refreshPolicy, that.refreshPolicy); + && Objects.equals(docId, that.docId) + && Objects.equals(opType, that.opType) + && Objects.equals(executionTimeField, that.executionTimeField) + && Objects.equals(timeout, that.timeout) + && Objects.equals(dynamicNameTimeZone, that.dynamicNameTimeZone) + && Objects.equals(refreshPolicy, that.refreshPolicy); } @Override @@ -121,7 +134,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws if (dynamicNameTimeZone != null) { builder.field(Field.DYNAMIC_NAME_TIMEZONE.getPreferredName(), dynamicNameTimeZone.toString()); } - if (refreshPolicy!= null) { + if (refreshPolicy != null) { builder.field(Field.REFRESH.getPreferredName(), refreshPolicy.getValue()); } return builder.endObject(); @@ -145,15 +158,26 @@ public static IndexAction parse(String watchId, String actionId, XContentParser try { index = parser.text(); } catch (ElasticsearchParseException pe) { - throw new ElasticsearchParseException("could not parse [{}] action [{}/{}]. failed to parse index name value for " + - "field [{}]", pe, TYPE, watchId, actionId, currentFieldName); + throw new ElasticsearchParseException( + "could not parse [{}] action [{}/{}]. failed to parse index name value for " + "field [{}]", + pe, + TYPE, + watchId, + actionId, + currentFieldName + ); } } else if (token == XContentParser.Token.VALUE_NUMBER) { if (Field.TIMEOUT.match(currentFieldName, parser.getDeprecationHandler())) { timeout = timeValueMillis(parser.longValue()); } else { - throw new ElasticsearchParseException("could not parse [{}] action [{}/{}]. unexpected number field [{}]", TYPE, - watchId, actionId, currentFieldName); + throw new ElasticsearchParseException( + "could not parse [{}] action [{}/{}]. unexpected number field [{}]", + TYPE, + watchId, + actionId, + currentFieldName + ); } } else if (token == XContentParser.Token.VALUE_STRING) { if (Field.DOC_ID.match(currentFieldName, parser.getDeprecationHandler())) { @@ -162,12 +186,22 @@ public static IndexAction parse(String watchId, String actionId, XContentParser try { opType = DocWriteRequest.OpType.fromString(parser.text()); if (List.of(DocWriteRequest.OpType.CREATE, DocWriteRequest.OpType.INDEX).contains(opType) == false) { - throw new ElasticsearchParseException("could not parse [{}] action [{}/{}]. op_type value for field [{}] " + - "must be [index] or [create]", TYPE, watchId, actionId, currentFieldName); + throw new ElasticsearchParseException( + "could not parse [{}] action [{}/{}]. op_type value for field [{}] " + "must be [index] or [create]", + TYPE, + watchId, + actionId, + currentFieldName + ); } } catch (IllegalArgumentException e) { - throw new ElasticsearchParseException("could not parse [{}] action [{}/{}]. failed to parse op_type value for " + - "field [{}]", TYPE, watchId, actionId, currentFieldName); + throw new ElasticsearchParseException( + "could not parse [{}] action [{}/{}]. failed to parse op_type value for " + "field [{}]", + TYPE, + watchId, + actionId, + currentFieldName + ); } } else if (Field.EXECUTION_TIME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) { executionTimeField = parser.text(); @@ -178,18 +212,33 @@ public static IndexAction parse(String watchId, String actionId, XContentParser if (token == XContentParser.Token.VALUE_STRING) { dynamicNameTimeZone = DateUtils.of(parser.text()); } else { - throw new ElasticsearchParseException("could not parse [{}] action for watch [{}]. failed to parse [{}]. must be " + - "a string value (e.g. 'UTC' or '+01:00').", TYPE, watchId, currentFieldName); + throw new ElasticsearchParseException( + "could not parse [{}] action for watch [{}]. failed to parse [{}]. must be " + + "a string value (e.g. 'UTC' or '+01:00').", + TYPE, + watchId, + currentFieldName + ); } } else if (Field.REFRESH.match(currentFieldName, parser.getDeprecationHandler())) { refreshPolicy = RefreshPolicy.parse(parser.text()); } else { - throw new ElasticsearchParseException("could not parse [{}] action [{}/{}]. unexpected string field [{}]", TYPE, - watchId, actionId, currentFieldName); + throw new ElasticsearchParseException( + "could not parse [{}] action [{}/{}]. unexpected string field [{}]", + TYPE, + watchId, + actionId, + currentFieldName + ); } } else { - throw new ElasticsearchParseException("could not parse [{}] action [{}/{}]. unexpected token [{}]", TYPE, watchId, - actionId, token); + throw new ElasticsearchParseException( + "could not parse [{}] action [{}/{}]. unexpected token [{}]", + TYPE, + watchId, + actionId, + token + ); } } @@ -215,21 +264,20 @@ public XContentSource response() { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder.startObject(type) - .field(Field.RESPONSE.getPreferredName(), response, params) - .endObject(); + return builder.startObject(type).field(Field.RESPONSE.getPreferredName(), response, params).endObject(); } } static class Simulated extends Action.Result { private final String index; - @Nullable private final String docId; - @Nullable private final RefreshPolicy refreshPolicy; + @Nullable + private final String docId; + @Nullable + private final RefreshPolicy refreshPolicy; private final XContentSource source; - protected Simulated(String index, @Nullable String docId, @Nullable RefreshPolicy refreshPolicy, - XContentSource source) { + protected Simulated(String index, @Nullable String docId, @Nullable RefreshPolicy refreshPolicy, XContentSource source) { super(TYPE, Status.SIMULATED); this.index = index; this.docId = docId; @@ -251,9 +299,7 @@ public XContentSource source() { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.startObject(type) - .startObject(Field.REQUEST.getPreferredName()) - .field(Field.INDEX.getPreferredName(), index); + builder.startObject(type).startObject(Field.REQUEST.getPreferredName()).field(Field.INDEX.getPreferredName(), index); if (docId != null) { builder.field(Field.DOC_ID.getPreferredName(), docId); @@ -263,9 +309,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.field(Field.REFRESH.getPreferredName(), refreshPolicy.getValue()); } - return builder.field(Field.SOURCE.getPreferredName(), source, params) - .endObject() - .endObject(); + return builder.field(Field.SOURCE.getPreferredName(), source, params).endObject().endObject(); } } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/index/IndexActionFactory.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/index/IndexActionFactory.java index 9880f19ad1f9a..12291dc999c89 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/index/IndexActionFactory.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/index/IndexActionFactory.java @@ -30,7 +30,12 @@ public IndexActionFactory(Settings settings, Client client) { @Override public ExecutableIndexAction parseExecutable(String watchId, String actionId, XContentParser parser) throws IOException { - return new ExecutableIndexAction(IndexAction.parse(watchId, actionId, parser), actionLogger, client, - indexDefaultTimeout, bulkDefaultTimeout); + return new ExecutableIndexAction( + IndexAction.parse(watchId, actionId, parser), + actionLogger, + client, + indexDefaultTimeout, + bulkDefaultTimeout + ); } } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/jira/JiraAction.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/jira/JiraAction.java index 448cd46c69671..27541b8f38a30 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/jira/JiraAction.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/jira/JiraAction.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.xpack.watcher.actions.jira; - import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ParseField; @@ -25,8 +24,10 @@ public class JiraAction implements Action { public static final String TYPE = "jira"; - @Nullable final String account; - @Nullable final HttpProxy proxy; + @Nullable + final String account; + @Nullable + final HttpProxy proxy; final Map fields; public JiraAction(@Nullable String account, Map fields, HttpProxy proxy) { @@ -50,9 +51,7 @@ public boolean equals(Object o) { if (o == null || getClass() != o.getClass()) return false; JiraAction that = (JiraAction) o; - return Objects.equals(account, that.account) && - Objects.equals(fields, that.fields) && - Objects.equals(proxy, that.proxy); + return Objects.equals(account, that.account) && Objects.equals(fields, that.fields) && Objects.equals(proxy, that.proxy); } @Override @@ -87,8 +86,14 @@ public static JiraAction parse(String watchId, String actionId, XContentParser p if (token == XContentParser.Token.VALUE_STRING) { account = parser.text(); } else { - throw new ElasticsearchParseException("failed to parse [{}] action [{}/{}]. expected [{}] to be of type string, but " + - "found [{}] instead", TYPE, watchId, actionId, Field.ACCOUNT.getPreferredName(), token); + throw new ElasticsearchParseException( + "failed to parse [{}] action [{}/{}]. expected [{}] to be of type string, but " + "found [{}] instead", + TYPE, + watchId, + actionId, + Field.ACCOUNT.getPreferredName(), + token + ); } } else if (Field.PROXY.match(currentFieldName, parser.getDeprecationHandler())) { proxy = HttpProxy.parse(parser); @@ -96,12 +101,24 @@ public static JiraAction parse(String watchId, String actionId, XContentParser p try { fields = parser.map(); } catch (Exception e) { - throw new ElasticsearchParseException("failed to parse [{}] action [{}/{}]. failed to parse [{}] field", e, TYPE, - watchId, actionId, Field.FIELDS.getPreferredName()); + throw new ElasticsearchParseException( + "failed to parse [{}] action [{}/{}]. failed to parse [{}] field", + e, + TYPE, + watchId, + actionId, + Field.FIELDS.getPreferredName() + ); } } else { - throw new ElasticsearchParseException("failed to parse [{}] action [{}/{}]. unexpected token [{}/{}]", TYPE, watchId, - actionId, token, currentFieldName); + throw new ElasticsearchParseException( + "failed to parse [{}] action [{}/{}]. unexpected token [{}/{}]", + TYPE, + watchId, + actionId, + token, + currentFieldName + ); } } if (fields == null) { @@ -144,9 +161,7 @@ public Map getFields() { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder.startObject(type) - .field(Field.FIELDS.getPreferredName(), fields) - .endObject(); + return builder.startObject(type).field(Field.FIELDS.getPreferredName(), fields).endObject(); } } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/logging/ExecutableLoggingAction.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/logging/ExecutableLoggingAction.java index 973cbd11a32ff..18c0aff075937 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/logging/ExecutableLoggingAction.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/logging/ExecutableLoggingAction.java @@ -6,8 +6,8 @@ */ package org.elasticsearch.xpack.watcher.actions.logging; -import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.elasticsearch.xpack.core.watcher.actions.Action; import org.elasticsearch.xpack.core.watcher.actions.ExecutableAction; import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext; @@ -40,7 +40,7 @@ Logger textLogger() { } @Override - public Action.Result execute(String actionId, WatchExecutionContext ctx, Payload payload) throws Exception { + public Action.Result execute(String actionId, WatchExecutionContext ctx, Payload payload) throws Exception { Map model = Variables.createCtxParamsMap(ctx, payload); String loggedText = templateEngine.render(action.text, model); diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/logging/LoggingAction.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/logging/LoggingAction.java index 71ab5d74357ff..5780dd0ee45c0 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/logging/LoggingAction.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/logging/LoggingAction.java @@ -23,8 +23,10 @@ public class LoggingAction implements Action { public static final String TYPE = "logging"; final TextTemplate text; - @Nullable final LoggingLevel level; - @Nullable final String category; + @Nullable + final LoggingLevel level; + @Nullable + final String category; public LoggingAction(TextTemplate text, @Nullable LoggingLevel level, @Nullable String category) { this.text = text; @@ -82,8 +84,14 @@ public static LoggingAction parse(String watchId, String actionId, XContentParse try { text = TextTemplate.parse(parser); } catch (ElasticsearchParseException pe) { - throw new ElasticsearchParseException("failed to parse [{}] action [{}/{}]. failed to parse [{}] field", pe, TYPE, - watchId, actionId, Field.TEXT.getPreferredName()); + throw new ElasticsearchParseException( + "failed to parse [{}] action [{}/{}]. failed to parse [{}] field", + pe, + TYPE, + watchId, + actionId, + Field.TEXT.getPreferredName() + ); } } else if (token == XContentParser.Token.VALUE_STRING) { if (Field.CATEGORY.match(currentFieldName, parser.getDeprecationHandler())) { @@ -92,22 +100,42 @@ public static LoggingAction parse(String watchId, String actionId, XContentParse try { level = LoggingLevel.valueOf(parser.text().toUpperCase(Locale.ROOT)); } catch (IllegalArgumentException iae) { - throw new ElasticsearchParseException("failed to parse [{}] action [{}/{}]. unknown logging level [{}]", TYPE, - watchId, actionId, parser.text()); + throw new ElasticsearchParseException( + "failed to parse [{}] action [{}/{}]. unknown logging level [{}]", + TYPE, + watchId, + actionId, + parser.text() + ); } } else { - throw new ElasticsearchParseException("failed to parse [{}] action [{}/{}]. unexpected string field [{}]", TYPE, - watchId, actionId, currentFieldName); + throw new ElasticsearchParseException( + "failed to parse [{}] action [{}/{}]. unexpected string field [{}]", + TYPE, + watchId, + actionId, + currentFieldName + ); } } else { - throw new ElasticsearchParseException("failed to parse [{}] action [{}/{}]. unexpected token [{}]", TYPE, watchId, - actionId, token); + throw new ElasticsearchParseException( + "failed to parse [{}] action [{}/{}]. unexpected token [{}]", + TYPE, + watchId, + actionId, + token + ); } } if (text == null) { - throw new ElasticsearchParseException("failed to parse [{}] action [{}/{}]. missing required [{}] field", TYPE, watchId, - actionId, Field.TEXT.getPreferredName()); + throw new ElasticsearchParseException( + "failed to parse [{}] action [{}/{}]. missing required [{}] field", + TYPE, + watchId, + actionId, + Field.TEXT.getPreferredName() + ); } return new LoggingAction(text, level, category); @@ -134,9 +162,7 @@ public String loggedText() { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder.startObject(type) - .field(Field.LOGGED_TEXT.getPreferredName(), loggedText) - .endObject(); + return builder.startObject(type).field(Field.LOGGED_TEXT.getPreferredName(), loggedText).endObject(); } } @@ -155,9 +181,7 @@ public String loggedText() { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder.startObject(type) - .field(Field.LOGGED_TEXT.getPreferredName(), loggedText) - .endObject(); + return builder.startObject(type).field(Field.LOGGED_TEXT.getPreferredName(), loggedText).endObject(); } } } @@ -166,7 +190,8 @@ public static class Builder implements Action.Builder { final TextTemplate text; LoggingLevel level; - @Nullable String category; + @Nullable + String category; private Builder(TextTemplate text) { this.text = text; diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/pagerduty/ExecutablePagerDutyAction.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/pagerduty/ExecutablePagerDutyAction.java index 6d06655df6768..f33173275c939 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/pagerduty/ExecutablePagerDutyAction.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/pagerduty/ExecutablePagerDutyAction.java @@ -25,8 +25,12 @@ public class ExecutablePagerDutyAction extends ExecutableAction private final TextTemplateEngine templateEngine; private final PagerDutyService pagerDutyService; - public ExecutablePagerDutyAction(PagerDutyAction action, Logger logger, PagerDutyService pagerDutyService, - TextTemplateEngine templateEngine) { + public ExecutablePagerDutyAction( + PagerDutyAction action, + Logger logger, + PagerDutyService pagerDutyService, + TextTemplateEngine templateEngine + ) { super(action, logger); this.pagerDutyService = pagerDutyService; this.templateEngine = templateEngine; diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/pagerduty/PagerDutyAction.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/pagerduty/PagerDutyAction.java index 43b9cda5e386d..6c220298af548 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/pagerduty/PagerDutyAction.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/pagerduty/PagerDutyAction.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.xpack.watcher.actions.pagerduty; - import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -108,9 +107,7 @@ public IncidentEvent event() { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder.startObject(type) - .field(XField.EVENT.getPreferredName(), event, params) - .endObject(); + return builder.startObject(type).field(XField.EVENT.getPreferredName(), event, params).endObject(); } } } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/slack/SlackAction.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/slack/SlackAction.java index 7b9fd56461ae8..54a5bced32d2d 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/slack/SlackAction.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/slack/SlackAction.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.xpack.watcher.actions.slack; - import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ParseField; @@ -25,8 +24,10 @@ public class SlackAction implements Action { public static final String TYPE = "slack"; final SlackMessage.Template message; - @Nullable final String account; - @Nullable final HttpProxy proxy; + @Nullable + final String account; + @Nullable + final HttpProxy proxy; public SlackAction(@Nullable String account, SlackMessage.Template message, HttpProxy proxy) { this.account = account; @@ -46,9 +47,7 @@ public boolean equals(Object o) { SlackAction that = (SlackAction) o; - return Objects.equals(account, that.account) && - Objects.equals(message, that.message) && - Objects.equals(proxy, that.proxy); + return Objects.equals(account, that.account) && Objects.equals(message, that.message) && Objects.equals(proxy, that.proxy); } @Override @@ -83,8 +82,14 @@ public static SlackAction parse(String watchId, String actionId, XContentParser if (token == XContentParser.Token.VALUE_STRING) { account = parser.text(); } else { - throw new ElasticsearchParseException("failed to parse [{}] action [{}/{}]. expected [{}] to be of type string, but " + - "found [{}] instead", TYPE, watchId, actionId, Field.ACCOUNT.getPreferredName(), token); + throw new ElasticsearchParseException( + "failed to parse [{}] action [{}/{}]. expected [{}] to be of type string, but " + "found [{}] instead", + TYPE, + watchId, + actionId, + Field.ACCOUNT.getPreferredName(), + token + ); } } else if (Field.PROXY.match(currentFieldName, parser.getDeprecationHandler())) { proxy = HttpProxy.parse(parser); @@ -92,18 +97,34 @@ public static SlackAction parse(String watchId, String actionId, XContentParser try { message = SlackMessage.Template.parse(parser); } catch (Exception e) { - throw new ElasticsearchParseException("failed to parse [{}] action [{}/{}]. failed to parse [{}] field", e, TYPE, - watchId, actionId, Field.MESSAGE.getPreferredName()); + throw new ElasticsearchParseException( + "failed to parse [{}] action [{}/{}]. failed to parse [{}] field", + e, + TYPE, + watchId, + actionId, + Field.MESSAGE.getPreferredName() + ); } } else { - throw new ElasticsearchParseException("failed to parse [{}] action [{}/{}]. unexpected token [{}]", TYPE, watchId, - actionId, token); + throw new ElasticsearchParseException( + "failed to parse [{}] action [{}/{}]. unexpected token [{}]", + TYPE, + watchId, + actionId, + token + ); } } if (message == null) { - throw new ElasticsearchParseException("failed to parse [{}] action [{}/{}]. missing required [{}] field", TYPE, watchId, - actionId, Field.MESSAGE.getPreferredName()); + throw new ElasticsearchParseException( + "failed to parse [{}] action [{}/{}]. missing required [{}] field", + TYPE, + watchId, + actionId, + Field.MESSAGE.getPreferredName() + ); } return new SlackAction(account, message, proxy); @@ -165,9 +186,7 @@ public SlackMessage getMessage() { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder.startObject(type) - .field(Field.MESSAGE.getPreferredName(), message, params) - .endObject(); + return builder.startObject(type).field(Field.MESSAGE.getPreferredName(), message, params).endObject(); } } } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/webhook/WebhookAction.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/webhook/WebhookAction.java index 325ee30e6b14c..fd8ef3cf2e31c 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/webhook/WebhookAction.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/webhook/WebhookAction.java @@ -61,8 +61,13 @@ public static WebhookAction parse(String watchId, String actionId, XContentParse HttpRequestTemplate request = HttpRequestTemplate.Parser.parse(parser); return new WebhookAction(request); } catch (ElasticsearchParseException pe) { - throw new ElasticsearchParseException("could not parse [{}] action [{}/{}]. failed parsing http request template", pe, TYPE, - watchId, actionId); + throw new ElasticsearchParseException( + "could not parse [{}] action [{}/{}]. failed parsing http request template", + pe, + TYPE, + watchId, + actionId + ); } } @@ -94,9 +99,9 @@ public HttpRequest request() { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { return builder.startObject(type) - .field(Field.REQUEST.getPreferredName(), request, params) - .field(Field.RESPONSE.getPreferredName(), response, params) - .endObject(); + .field(Field.REQUEST.getPreferredName(), request, params) + .field(Field.RESPONSE.getPreferredName(), response, params) + .endObject(); } } @@ -127,9 +132,9 @@ public HttpRequest request() { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { super.toXContent(builder, params); return builder.startObject(type) - .field(Field.REQUEST.getPreferredName(), request, params) - .field(Field.RESPONSE.getPreferredName(), response, params) - .endObject(); + .field(Field.REQUEST.getPreferredName(), request, params) + .field(Field.RESPONSE.getPreferredName(), response, params) + .endObject(); } } @@ -148,9 +153,7 @@ public HttpRequest request() { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder.startObject(type) - .field(Field.REQUEST.getPreferredName(), request, params) - .endObject(); + return builder.startObject(type).field(Field.REQUEST.getPreferredName(), request, params).endObject(); } } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/webhook/WebhookActionFactory.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/webhook/WebhookActionFactory.java index 8932aa33ecb38..5cd648a148cef 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/webhook/WebhookActionFactory.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/webhook/WebhookActionFactory.java @@ -27,8 +27,7 @@ public WebhookActionFactory(HttpClient httpClient, TextTemplateEngine templateEn @Override public ExecutableWebhookAction parseExecutable(String watchId, String actionId, XContentParser parser) throws IOException { - return new ExecutableWebhookAction(WebhookAction.parse(watchId, actionId, parser), - actionLogger, httpClient, templateEngine); + return new ExecutableWebhookAction(WebhookAction.parse(watchId, actionId, parser), actionLogger, httpClient, templateEngine); } } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/client/WatchSourceBuilders.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/client/WatchSourceBuilders.java index fb48f87ba7df5..2d5bb7fcbb49f 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/client/WatchSourceBuilders.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/client/WatchSourceBuilders.java @@ -10,8 +10,7 @@ public final class WatchSourceBuilders { - private WatchSourceBuilders() { - } + private WatchSourceBuilders() {} public static WatchSourceBuilder watchBuilder() { return new WatchSourceBuilder(); diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/http/HttpClient.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/http/HttpClient.java index 62dd39d6e6187..cbeac315e6d10 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/http/HttpClient.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/http/HttpClient.java @@ -47,20 +47,19 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.Strings; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.regex.Regex; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.ssl.SslConfiguration; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.core.Tuple; +import org.elasticsearch.core.internal.io.Streams; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.core.internal.io.Streams; import org.elasticsearch.xpack.core.common.socket.SocketAccess; import org.elasticsearch.xpack.core.ssl.SSLService; import org.elasticsearch.xpack.core.watcher.crypto.CryptoService; -import javax.net.ssl.HostnameVerifier; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.IOException; @@ -78,6 +77,8 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; +import javax.net.ssl.HostnameVerifier; + public class HttpClient implements Closeable { private static final String SETTINGS_SSL_PREFIX = "xpack.http.ssl."; @@ -137,14 +138,19 @@ private CloseableHttpClient createHttpClient() { clientBuilder.setMaxConnTotal(MAX_CONNECTIONS); clientBuilder.setRedirectStrategy(new DefaultRedirectStrategy() { @Override - public boolean isRedirected(org.apache.http.HttpRequest request, org.apache.http.HttpResponse response, - HttpContext context) throws ProtocolException { + public boolean isRedirected(org.apache.http.HttpRequest request, org.apache.http.HttpResponse response, HttpContext context) + throws ProtocolException { boolean isRedirected = super.isRedirected(request, response, context); if (isRedirected) { String host = response.getHeaders("Location")[0].getValue(); if (isWhitelisted(host) == false) { - throw new ElasticsearchException("host [" + host + "] is not whitelisted in setting [" + - HttpSettings.HOSTS_WHITELIST.getKey() + "], will not redirect"); + throw new ElasticsearchException( + "host [" + + host + + "] is not whitelisted in setting [" + + HttpSettings.HOSTS_WHITELIST.getKey() + + "], will not redirect" + ); } } @@ -154,8 +160,11 @@ public boolean isRedirected(org.apache.http.HttpRequest request, org.apache.http clientBuilder.addInterceptorFirst((HttpRequestInterceptor) (request, context) -> { if (request instanceof HttpRequestWrapper == false) { - throw new ElasticsearchException("unable to check request [{}/{}] for white listing", request, - request.getClass().getName()); + throw new ElasticsearchException( + "unable to check request [{}/{}] for white listing", + request, + request.getClass().getName() + ); } HttpRequestWrapper wrapper = ((HttpRequestWrapper) request); @@ -167,8 +176,9 @@ public boolean isRedirected(org.apache.http.HttpRequest request, org.apache.http } if (isWhitelisted(host) == false) { - throw new ElasticsearchException("host [" + host + "] is not whitelisted in setting [" + - HttpSettings.HOSTS_WHITELIST.getKey() + "], will not connect"); + throw new ElasticsearchException( + "host [" + host + "] is not whitelisted in setting [" + HttpSettings.HOSTS_WHITELIST.getKey() + "], will not connect" + ); } }); @@ -224,8 +234,10 @@ public HttpResponse execute(HttpRequest request) throws IOException { // auth if (request.auth() != null) { CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); - Credentials credentials = new UsernamePasswordCredentials(request.auth().username, - new String(request.auth().password.text(cryptoService))); + Credentials credentials = new UsernamePasswordCredentials( + request.auth().username, + new String(request.auth().password.text(cryptoService)) + ); credentialsProvider.setCredentials(new AuthScope(request.host, request.port), credentials); localContext.setCredentialsProvider(credentialsProvider); @@ -267,7 +279,7 @@ public HttpResponse execute(HttpRequest request) throws IOException { responseHeaders.put(header.getName(), values); } else { - responseHeaders.put(header.getName(), new String[]{header.getValue()}); + responseHeaders.put(header.getName(), new String[] { header.getValue() }); } } @@ -313,14 +325,20 @@ static void setProxy(RequestConfig.Builder config, HttpRequest request, HttpProx */ private HttpProxy getProxyFromSettings(Settings settings) { String proxyHost = HttpSettings.PROXY_HOST.get(settings); - Scheme proxyScheme = HttpSettings.PROXY_SCHEME.exists(settings) ? - Scheme.parse(HttpSettings.PROXY_SCHEME.get(settings)) : Scheme.HTTP; + Scheme proxyScheme = HttpSettings.PROXY_SCHEME.exists(settings) + ? Scheme.parse(HttpSettings.PROXY_SCHEME.get(settings)) + : Scheme.HTTP; int proxyPort = HttpSettings.PROXY_PORT.get(settings); if (proxyPort != 0 && Strings.hasText(proxyHost)) { logger.info("Using default proxy for http input and slack/pagerduty/webhook actions [{}:{}]", proxyHost, proxyPort); } else if (proxyPort != 0 ^ Strings.hasText(proxyHost)) { - throw new IllegalArgumentException("HTTP proxy requires both settings: [" + HttpSettings.PROXY_HOST.getKey() + "] and [" + - HttpSettings.PROXY_PORT.getKey() + "]"); + throw new IllegalArgumentException( + "HTTP proxy requires both settings: [" + + HttpSettings.PROXY_HOST.getKey() + + "] and [" + + HttpSettings.PROXY_PORT.getKey() + + "]" + ); } if (proxyPort > 0 && Strings.hasText(proxyHost)) { @@ -359,8 +377,7 @@ static Tuple createURI(HttpRequest request) { } } - final URI uri = new URIBuilder() - .setScheme(request.scheme().scheme()) + final URI uri = new URIBuilder().setScheme(request.scheme().scheme()) .setHost(request.host) .setPort(request.port) .setPathSegments(unescapedPathParts) @@ -402,6 +419,7 @@ private boolean isWhitelisted(String host) { } private static final CharacterRunAutomaton MATCH_ALL_AUTOMATON = new CharacterRunAutomaton(Regex.simpleMatchToAutomaton("*")); + // visible for testing static CharacterRunAutomaton createAutomaton(List whiteListedHosts) { if (whiteListedHosts.isEmpty()) { diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/http/HttpContentType.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/http/HttpContentType.java index e646f352a57c3..77666cd4d820f 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/http/HttpContentType.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/http/HttpContentType.java @@ -48,9 +48,12 @@ public String id() { public static HttpContentType resolve(String id) { switch (id.toLowerCase(Locale.ROOT)) { - case "json" : return JSON; - case "yaml": return YAML; - case "text": return TEXT; + case "json": + return JSON; + case "yaml": + return YAML; + case "text": + return TEXT; default: throw illegalArgument("unknown http content type [{}]", id); } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/http/HttpProxy.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/http/HttpProxy.java index ddfd497dbc976..0a72b77376b65 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/http/HttpProxy.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/http/HttpProxy.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.watcher.common.http; import org.elasticsearch.ElasticsearchParseException; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentFragment; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -78,7 +78,6 @@ public int hashCode() { return Objects.hash(host, port, scheme); } - public static HttpProxy parse(XContentParser parser) throws IOException { XContentParser.Token token; String currentFieldName = null; diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/http/HttpRequest.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/http/HttpRequest.java index e3738aa5b9850..0626d12907499 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/http/HttpRequest.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/http/HttpRequest.java @@ -7,16 +7,16 @@ package org.elasticsearch.xpack.watcher.common.http; import org.elasticsearch.ElasticsearchParseException; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.rest.RestUtils; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.rest.RestUtils; import org.elasticsearch.xpack.core.watcher.support.WatcherDateTimeUtils; import org.elasticsearch.xpack.core.watcher.support.WatcherUtils; import org.elasticsearch.xpack.core.watcher.support.xcontent.WatcherParams; @@ -46,19 +46,35 @@ public class HttpRequest implements ToXContentObject { final int port; final Scheme scheme; final HttpMethod method; - @Nullable final String path; + @Nullable + final String path; final Map params; final Map headers; - @Nullable final BasicAuth auth; - @Nullable final String body; - @Nullable final TimeValue connectionTimeout; - @Nullable final TimeValue readTimeout; - @Nullable final HttpProxy proxy; - - public HttpRequest(String host, int port, @Nullable Scheme scheme, @Nullable HttpMethod method, @Nullable String path, - @Nullable Map params, @Nullable Map headers, - @Nullable BasicAuth auth, @Nullable String body, @Nullable TimeValue connectionTimeout, - @Nullable TimeValue readTimeout, @Nullable HttpProxy proxy) { + @Nullable + final BasicAuth auth; + @Nullable + final String body; + @Nullable + final TimeValue connectionTimeout; + @Nullable + final TimeValue readTimeout; + @Nullable + final HttpProxy proxy; + + public HttpRequest( + String host, + int port, + @Nullable Scheme scheme, + @Nullable HttpMethod method, + @Nullable String path, + @Nullable Map params, + @Nullable Map headers, + @Nullable BasicAuth auth, + @Nullable String body, + @Nullable TimeValue connectionTimeout, + @Nullable TimeValue readTimeout, + @Nullable HttpProxy proxy + ) { this.host = host; this.port = port; this.scheme = scheme != null ? scheme : Scheme.HTTP; @@ -162,20 +178,24 @@ public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params toX } } if (auth != null) { - builder.startObject(Field.AUTH.getPreferredName()) - .field(BasicAuth.TYPE, auth, toXContentParams) - .endObject(); + builder.startObject(Field.AUTH.getPreferredName()).field(BasicAuth.TYPE, auth, toXContentParams).endObject(); } if (body != null) { builder.field(Field.BODY.getPreferredName(), body); } if (connectionTimeout != null) { - builder.humanReadableField(HttpRequest.Field.CONNECTION_TIMEOUT.getPreferredName(), - HttpRequest.Field.CONNECTION_TIMEOUT_HUMAN.getPreferredName(), connectionTimeout); + builder.humanReadableField( + HttpRequest.Field.CONNECTION_TIMEOUT.getPreferredName(), + HttpRequest.Field.CONNECTION_TIMEOUT_HUMAN.getPreferredName(), + connectionTimeout + ); } if (readTimeout != null) { - builder.humanReadableField(HttpRequest.Field.READ_TIMEOUT.getPreferredName(), - HttpRequest.Field.READ_TIMEOUT_HUMAN.getPreferredName(), readTimeout); + builder.humanReadableField( + HttpRequest.Field.READ_TIMEOUT.getPreferredName(), + HttpRequest.Field.READ_TIMEOUT_HUMAN.getPreferredName(), + readTimeout + ); } if (proxy != null) { proxy.toXContent(builder, toXContentParams); @@ -229,9 +249,12 @@ public String toString() { sb.append("port=[").append(port).append("], "); sb.append("path=[").append(path).append("], "); if (headers.isEmpty() == false) { - sb.append(sanitizeHeaders(headers).entrySet().stream() - .map(header -> header.getKey() + ": " + header.getValue()) - .collect(Collectors.joining(", ", "headers=[", "], "))); + sb.append( + sanitizeHeaders(headers).entrySet() + .stream() + .map(header -> header.getKey() + ": " + header.getValue()) + .collect(Collectors.joining(", ", "headers=[", "], ")) + ); } if (auth != null) { sb.append("auth=[").append(BasicAuth.TYPE).append("], "); @@ -274,11 +297,15 @@ public static HttpRequest parse(XContentParser parser) throws IOException { } else if (HttpRequest.Field.CONNECTION_TIMEOUT_HUMAN.match(currentFieldName, parser.getDeprecationHandler())) { // Users and 2.x specify the timeout this way try { - builder.connectionTimeout(WatcherDateTimeUtils.parseTimeValue(parser, - HttpRequest.Field.CONNECTION_TIMEOUT.toString())); + builder.connectionTimeout( + WatcherDateTimeUtils.parseTimeValue(parser, HttpRequest.Field.CONNECTION_TIMEOUT.toString()) + ); } catch (ElasticsearchParseException pe) { - throw new ElasticsearchParseException("could not parse http request template. invalid time value for [{}] field", - pe, currentFieldName); + throw new ElasticsearchParseException( + "could not parse http request template. invalid time value for [{}] field", + pe, + currentFieldName + ); } } else if (HttpRequest.Field.READ_TIMEOUT.match(currentFieldName, parser.getDeprecationHandler())) { builder.readTimeout(TimeValue.timeValueMillis(parser.longValue())); @@ -287,11 +314,14 @@ public static HttpRequest parse(XContentParser parser) throws IOException { try { builder.readTimeout(WatcherDateTimeUtils.parseTimeValue(parser, HttpRequest.Field.READ_TIMEOUT.toString())); } catch (ElasticsearchParseException pe) { - throw new ElasticsearchParseException("could not parse http request template. invalid time value for [{}] field", - pe, currentFieldName); + throw new ElasticsearchParseException( + "could not parse http request template. invalid time value for [{}] field", + pe, + currentFieldName + ); } } else if (token == XContentParser.Token.START_OBJECT) { - @SuppressWarnings({"unchecked", "rawtypes"}) + @SuppressWarnings({ "unchecked", "rawtypes" }) final Map headers = (Map) WatcherUtils.flattenModel(parser.map()); if (Field.HEADERS.match(currentFieldName, parser.getDeprecationHandler())) { builder.setHeaders(headers); @@ -300,8 +330,10 @@ public static HttpRequest parse(XContentParser parser) throws IOException { } else if (Field.BODY.match(currentFieldName, parser.getDeprecationHandler())) { builder.body(parser.text()); } else { - throw new ElasticsearchParseException("could not parse http request. unexpected object field [{}]", - currentFieldName); + throw new ElasticsearchParseException( + "could not parse http request. unexpected object field [{}]", + currentFieldName + ); } } else if (token == XContentParser.Token.VALUE_STRING) { if (Field.SCHEME.match(currentFieldName, parser.getDeprecationHandler())) { @@ -317,15 +349,19 @@ public static HttpRequest parse(XContentParser parser) throws IOException { } else if (Field.URL.match(currentFieldName, parser.getDeprecationHandler())) { builder.fromUrl(parser.text()); } else { - throw new ElasticsearchParseException("could not parse http request. unexpected string field [{}]", - currentFieldName); + throw new ElasticsearchParseException( + "could not parse http request. unexpected string field [{}]", + currentFieldName + ); } } else if (token == XContentParser.Token.VALUE_NUMBER) { if (Field.PORT.match(currentFieldName, parser.getDeprecationHandler())) { builder.port = parser.intValue(); } else { - throw new ElasticsearchParseException("could not parse http request. unexpected numeric field [{}]", - currentFieldName); + throw new ElasticsearchParseException( + "could not parse http request. unexpected numeric field [{}]", + currentFieldName + ); } } else { throw new ElasticsearchParseException("could not parse http request. unexpected token [{}]", token); @@ -333,13 +369,17 @@ public static HttpRequest parse(XContentParser parser) throws IOException { } if (builder.host == null) { - throw new ElasticsearchParseException("could not parse http request. missing required [{}] field", - Field.HOST.getPreferredName()); + throw new ElasticsearchParseException( + "could not parse http request. missing required [{}] field", + Field.HOST.getPreferredName() + ); } if (builder.port < 0) { - throw new ElasticsearchParseException("could not parse http request. missing required [{}] field", - Field.PORT.getPreferredName()); + throw new ElasticsearchParseException( + "could not parse http request. missing required [{}] field", + Field.PORT.getPreferredName() + ); } return builder.build(); @@ -366,8 +406,7 @@ private Builder(String host, int port) { this.port = port; } - private Builder() { - } + private Builder() {} public Builder scheme(Scheme scheme) { this.scheme = scheme; @@ -446,8 +485,20 @@ public Builder proxy(HttpProxy proxy) { } public HttpRequest build() { - HttpRequest request = new HttpRequest(host, port, scheme, method, path, unmodifiableMap(params), - unmodifiableMap(headers), auth, body, connectionTimeout, readTimeout, proxy); + HttpRequest request = new HttpRequest( + host, + port, + scheme, + method, + path, + unmodifiableMap(params), + unmodifiableMap(headers), + auth, + body, + connectionTimeout, + readTimeout, + proxy + ); params = null; headers = null; return request; @@ -508,12 +559,18 @@ public interface Field { * @return A bytearrayinputstream that contains the serialized request * @throws IOException if an IOException is triggered in the underlying toXContent method */ - public static InputStream filterToXContent(HttpRequest request, XContentType xContentType, Params params, - String excludeField) throws IOException { - try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); - XContentBuilder filteredBuilder = new XContentBuilder(xContentType.xContent(), bos, - Collections.emptySet(), Collections.singleton(excludeField), - xContentType.toParsedMediaType())) { + public static InputStream filterToXContent(HttpRequest request, XContentType xContentType, Params params, String excludeField) + throws IOException { + try ( + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + XContentBuilder filteredBuilder = new XContentBuilder( + xContentType.xContent(), + bos, + Collections.emptySet(), + Collections.singleton(excludeField), + xContentType.toParsedMediaType() + ) + ) { request.toXContent(filteredBuilder, params); filteredBuilder.flush(); return new ByteArrayInputStream(bos.toByteArray()); diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/http/HttpRequestTemplate.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/http/HttpRequestTemplate.java index c82f49f345219..840ad54447182 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/http/HttpRequestTemplate.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/http/HttpRequestTemplate.java @@ -7,17 +7,18 @@ package org.elasticsearch.xpack.watcher.common.http; import io.netty.handler.codec.http.HttpHeaders; + import org.elasticsearch.ElasticsearchParseException; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.collect.MapBuilder; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.rest.RestUtils; +import org.elasticsearch.script.ScriptType; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.rest.RestUtils; -import org.elasticsearch.script.ScriptType; import org.elasticsearch.xpack.core.watcher.support.WatcherDateTimeUtils; import org.elasticsearch.xpack.watcher.common.text.TextTemplate; import org.elasticsearch.xpack.watcher.common.text.TextTemplateEngine; @@ -43,17 +44,30 @@ public class HttpRequestTemplate implements ToXContentObject { private final Map headers; private final BasicAuth auth; private final TextTemplate body; - @Nullable private final TimeValue connectionTimeout; - @Nullable private final TimeValue readTimeout; - @Nullable private final HttpProxy proxy; - - public HttpRequestTemplate(String host, int port, @Nullable Scheme scheme, @Nullable HttpMethod method, @Nullable TextTemplate path, - Map params, Map headers, BasicAuth auth, - TextTemplate body, @Nullable TimeValue connectionTimeout, @Nullable TimeValue readTimeout, - @Nullable HttpProxy proxy) { + @Nullable + private final TimeValue connectionTimeout; + @Nullable + private final TimeValue readTimeout; + @Nullable + private final HttpProxy proxy; + + public HttpRequestTemplate( + String host, + int port, + @Nullable Scheme scheme, + @Nullable HttpMethod method, + @Nullable TextTemplate path, + Map params, + Map headers, + BasicAuth auth, + TextTemplate body, + @Nullable TimeValue connectionTimeout, + @Nullable TimeValue readTimeout, + @Nullable HttpProxy proxy + ) { this.host = host; this.port = port; - this.scheme = scheme != null ? scheme :Scheme.HTTP; + this.scheme = scheme != null ? scheme : Scheme.HTTP; this.method = method != null ? method : HttpMethod.GET; this.path = path; this.params = params != null ? params : emptyMap(); @@ -183,20 +197,24 @@ public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params par builder.endObject(); } if (auth != null) { - builder.startObject(HttpRequest.Field.AUTH.getPreferredName()) - .field(BasicAuth.TYPE, auth, params) - .endObject(); + builder.startObject(HttpRequest.Field.AUTH.getPreferredName()).field(BasicAuth.TYPE, auth, params).endObject(); } if (body != null) { builder.field(HttpRequest.Field.BODY.getPreferredName(), body, params); } if (connectionTimeout != null) { - builder.humanReadableField(HttpRequest.Field.CONNECTION_TIMEOUT.getPreferredName(), - HttpRequest.Field.CONNECTION_TIMEOUT_HUMAN.getPreferredName(), connectionTimeout); + builder.humanReadableField( + HttpRequest.Field.CONNECTION_TIMEOUT.getPreferredName(), + HttpRequest.Field.CONNECTION_TIMEOUT_HUMAN.getPreferredName(), + connectionTimeout + ); } if (readTimeout != null) { - builder.humanReadableField(HttpRequest.Field.READ_TIMEOUT.getPreferredName(), - HttpRequest.Field.READ_TIMEOUT_HUMAN.getPreferredName(), readTimeout); + builder.humanReadableField( + HttpRequest.Field.READ_TIMEOUT.getPreferredName(), + HttpRequest.Field.READ_TIMEOUT_HUMAN.getPreferredName(), + readTimeout + ); } if (proxy != null) { proxy.toXContent(builder, params); @@ -276,11 +294,15 @@ public static HttpRequestTemplate parse(XContentParser parser) throws IOExceptio } else if (HttpRequest.Field.CONNECTION_TIMEOUT_HUMAN.match(currentFieldName, parser.getDeprecationHandler())) { // Users and 2.x specify the timeout this way try { - builder.connectionTimeout(WatcherDateTimeUtils.parseTimeValue(parser, - HttpRequest.Field.CONNECTION_TIMEOUT.toString())); + builder.connectionTimeout( + WatcherDateTimeUtils.parseTimeValue(parser, HttpRequest.Field.CONNECTION_TIMEOUT.toString()) + ); } catch (ElasticsearchParseException pe) { - throw new ElasticsearchParseException("could not parse http request template. invalid time value for [{}] field", - pe, currentFieldName); + throw new ElasticsearchParseException( + "could not parse http request template. invalid time value for [{}] field", + pe, + currentFieldName + ); } } else if (HttpRequest.Field.READ_TIMEOUT.match(currentFieldName, parser.getDeprecationHandler())) { builder.readTimeout(TimeValue.timeValueMillis(parser.longValue())); @@ -289,15 +311,20 @@ public static HttpRequestTemplate parse(XContentParser parser) throws IOExceptio try { builder.readTimeout(WatcherDateTimeUtils.parseTimeValue(parser, HttpRequest.Field.READ_TIMEOUT.toString())); } catch (ElasticsearchParseException pe) { - throw new ElasticsearchParseException("could not parse http request template. invalid time value for [{}] field", - pe, currentFieldName); + throw new ElasticsearchParseException( + "could not parse http request template. invalid time value for [{}] field", + pe, + currentFieldName + ); } } else if (token == XContentParser.Token.START_OBJECT) { if (HttpRequest.Field.AUTH.match(currentFieldName, parser.getDeprecationHandler())) { builder.auth(BasicAuth.parse(parser)); } else { - throw new ElasticsearchParseException("could not parse http request template. unexpected object field [{}]", - currentFieldName); + throw new ElasticsearchParseException( + "could not parse http request template. unexpected object field [{}]", + currentFieldName + ); } } else if (token == XContentParser.Token.VALUE_STRING) { if (HttpRequest.Field.SCHEME.match(currentFieldName, parser.getDeprecationHandler())) { @@ -307,29 +334,40 @@ public static HttpRequestTemplate parse(XContentParser parser) throws IOExceptio } else if (HttpRequest.Field.HOST.match(currentFieldName, parser.getDeprecationHandler())) { builder.host = parser.text(); } else { - throw new ElasticsearchParseException("could not parse http request template. unexpected string field [{}]", - currentFieldName); + throw new ElasticsearchParseException( + "could not parse http request template. unexpected string field [{}]", + currentFieldName + ); } } else if (token == XContentParser.Token.VALUE_NUMBER) { if (HttpRequest.Field.PORT.match(currentFieldName, parser.getDeprecationHandler())) { builder.port = parser.intValue(); } else { - throw new ElasticsearchParseException("could not parse http request template. unexpected numeric field [{}]", - currentFieldName); + throw new ElasticsearchParseException( + "could not parse http request template. unexpected numeric field [{}]", + currentFieldName + ); } } else { - throw new ElasticsearchParseException("could not parse http request template. unexpected token [{}] for field [{}]", - token, currentFieldName); + throw new ElasticsearchParseException( + "could not parse http request template. unexpected token [{}] for field [{}]", + token, + currentFieldName + ); } } if (builder.host == null) { - throw new ElasticsearchParseException("could not parse http request template. missing required [{}] string field", - HttpRequest.Field.HOST.getPreferredName()); + throw new ElasticsearchParseException( + "could not parse http request template. missing required [{}] string field", + HttpRequest.Field.HOST.getPreferredName() + ); } if (builder.port <= 0) { - throw new ElasticsearchParseException("could not parse http request template. wrong port for [{}]", - HttpRequest.Field.PORT.getPreferredName()); + throw new ElasticsearchParseException( + "could not parse http request template. wrong port for [{}]", + HttpRequest.Field.PORT.getPreferredName() + ); } return builder.build(); @@ -339,8 +377,11 @@ private static TextTemplate parseFieldTemplate(String field, XContentParser pars try { return TextTemplate.parse(parser); } catch (ElasticsearchParseException pe) { - throw new ElasticsearchParseException("could not parse http request template. could not parse value for [{}] field", pe, - field); + throw new ElasticsearchParseException( + "could not parse http request template. could not parse value for [{}] field", + pe, + field + ); } } @@ -375,8 +416,7 @@ public static class Builder { private TimeValue readTimeout; private HttpProxy proxy; - private Builder() { - } + private Builder() {} private Builder(String url) { fromUrl(url); @@ -460,8 +500,20 @@ public Builder proxy(HttpProxy proxy) { } public HttpRequestTemplate build() { - return new HttpRequestTemplate(host, port, scheme, method, path, Map.copyOf(params), - Map.copyOf(headers), auth, body, connectionTimeout, readTimeout, proxy); + return new HttpRequestTemplate( + host, + port, + scheme, + method, + path, + Map.copyOf(params), + Map.copyOf(headers), + auth, + body, + connectionTimeout, + readTimeout, + proxy + ); } public Builder fromUrl(String supposedUrl) { diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/http/HttpResponse.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/http/HttpResponse.java index 1a91fe85ac844..cc397ea71540d 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/http/HttpResponse.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/http/HttpResponse.java @@ -7,11 +7,12 @@ package org.elasticsearch.xpack.watcher.common.http; import io.netty.handler.codec.http.HttpHeaders; + import org.elasticsearch.ElasticsearchParseException; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -64,8 +65,8 @@ public HttpResponse(int status, @Nullable BytesReference body, Map e.getKey().toLowerCase(Locale.ROOT), Map.Entry::getValue)); + .stream() + .collect(Collectors.toUnmodifiableMap(e -> e.getKey().toLowerCase(Locale.ROOT), Map.Entry::getValue)); } public int status() { @@ -86,8 +87,8 @@ public BytesReference body() { */ public Map> headers() { return headers.entrySet() - .stream() - .collect(Collectors.toUnmodifiableMap(e -> e.getKey().toLowerCase(Locale.ROOT), e -> Arrays.asList(e.getValue()))); + .stream() + .collect(Collectors.toUnmodifiableMap(e -> e.getKey().toLowerCase(Locale.ROOT), e -> Arrays.asList(e.getValue()))); } public String[] header(String header) { @@ -110,7 +111,7 @@ public XContentType xContentType() { try { return XContentType.fromMediaType(values[0]); } catch (IllegalArgumentException e) { - //HttpInputTests - content-type being unrecognized_content_type + // HttpInputTests - content-type being unrecognized_content_type return null; } } @@ -207,17 +208,22 @@ public static HttpResponse parse(XContentParser parser) throws IOException { while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { headerName = parser.currentName(); - } else if (headerName == null){ - throw new ElasticsearchParseException("could not parse http response. expected a header name but found [{}] " + - "instead", token); + } else if (headerName == null) { + throw new ElasticsearchParseException( + "could not parse http response. expected a header name but found [{}] " + "instead", + token + ); } else if (token.isValue()) { headers.put(headerName, new String[] { String.valueOf(parser.objectText()) }); } else if (token == XContentParser.Token.START_ARRAY) { List values = new ArrayList<>(); while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { if (token.isValue() == false) { - throw new ElasticsearchParseException("could not parse http response. expected a header value for header " + - "[{}] but found [{}] instead", headerName, token); + throw new ElasticsearchParseException( + "could not parse http response. expected a header value for header " + "[{}] but found [{}] instead", + headerName, + token + ); } else { values.add(String.valueOf(parser.objectText())); } @@ -231,8 +237,10 @@ public static HttpResponse parse(XContentParser parser) throws IOException { } if (status < 0) { - throw new ElasticsearchParseException("could not parse http response. missing required numeric [{}] field holding the " + - "response's http status code", Field.STATUS.getPreferredName()); + throw new ElasticsearchParseException( + "could not parse http response. missing required numeric [{}] field holding the " + "response's http status code", + Field.STATUS.getPreferredName() + ); } return new HttpResponse(status, body, unmodifiableMap(headers)); } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/http/HttpSettings.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/http/HttpSettings.java index de8787a1c4e42..a9a52336ef292 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/http/HttpSettings.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/http/HttpSettings.java @@ -26,14 +26,22 @@ public class HttpSettings { private static final TimeValue DEFAULT_READ_TIMEOUT = TimeValue.timeValueSeconds(10); private static final TimeValue DEFAULT_CONNECTION_TIMEOUT = DEFAULT_READ_TIMEOUT; - static final Setting READ_TIMEOUT = Setting.timeSetting("xpack.http.default_read_timeout", - DEFAULT_READ_TIMEOUT, Property.NodeScope); - static final Setting CONNECTION_TIMEOUT = Setting.timeSetting("xpack.http.default_connection_timeout", - DEFAULT_CONNECTION_TIMEOUT, Property.NodeScope); - static final Setting TCP_KEEPALIVE = Setting.boolSetting("xpack.http.tcp.keep_alive", - true, Property.NodeScope); - static final Setting CONNECTION_POOL_TTL = Setting.timeSetting("xpack.http.connection_pool_ttl", - TimeValue.MINUS_ONE, Property.NodeScope); + static final Setting READ_TIMEOUT = Setting.timeSetting( + "xpack.http.default_read_timeout", + DEFAULT_READ_TIMEOUT, + Property.NodeScope + ); + static final Setting CONNECTION_TIMEOUT = Setting.timeSetting( + "xpack.http.default_connection_timeout", + DEFAULT_CONNECTION_TIMEOUT, + Property.NodeScope + ); + static final Setting TCP_KEEPALIVE = Setting.boolSetting("xpack.http.tcp.keep_alive", true, Property.NodeScope); + static final Setting CONNECTION_POOL_TTL = Setting.timeSetting( + "xpack.http.connection_pool_ttl", + TimeValue.MINUS_ONE, + Property.NodeScope + ); private static final String PROXY_HOST_KEY = "xpack.http.proxy.host"; private static final String PROXY_PORT_KEY = "xpack.http.proxy.port"; @@ -43,14 +51,21 @@ public class HttpSettings { static final Setting PROXY_HOST = Setting.simpleString(PROXY_HOST_KEY, Property.NodeScope); static final Setting PROXY_SCHEME = Setting.simpleString(PROXY_SCHEME_KEY, Scheme::parse, Property.NodeScope); static final Setting PROXY_PORT = Setting.intSetting(PROXY_PORT_KEY, 0, 0, 0xFFFF, Property.NodeScope); - static final Setting> HOSTS_WHITELIST = Setting.listSetting("xpack.http.whitelist", Collections.singletonList("*"), - Function.identity(), Property.NodeScope, Property.Dynamic); + static final Setting> HOSTS_WHITELIST = Setting.listSetting( + "xpack.http.whitelist", + Collections.singletonList("*"), + Function.identity(), + Property.NodeScope, + Property.Dynamic + ); - static final Setting MAX_HTTP_RESPONSE_SIZE = Setting.byteSizeSetting("xpack.http.max_response_size", - new ByteSizeValue(10, ByteSizeUnit.MB), // default - new ByteSizeValue(1, ByteSizeUnit.BYTES), // min - new ByteSizeValue(50, ByteSizeUnit.MB), // max - Property.NodeScope); + static final Setting MAX_HTTP_RESPONSE_SIZE = Setting.byteSizeSetting( + "xpack.http.max_response_size", + new ByteSizeValue(10, ByteSizeUnit.MB), // default + new ByteSizeValue(1, ByteSizeUnit.BYTES), // min + new ByteSizeValue(50, ByteSizeUnit.MB), // max + Property.NodeScope + ); private static final SSLConfigurationSettings SSL = SSLConfigurationSettings.withPrefix(SSL_KEY_PREFIX, true); @@ -69,6 +84,5 @@ public static List> getSettings() { return settings; } - private HttpSettings() { - } + private HttpSettings() {} } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/text/TextTemplate.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/text/TextTemplate.java index a747104731827..ad5ef9f443b21 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/text/TextTemplate.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/text/TextTemplate.java @@ -7,12 +7,12 @@ package org.elasticsearch.xpack.watcher.common.text; import org.elasticsearch.core.Nullable; +import org.elasticsearch.script.Script; +import org.elasticsearch.script.ScriptType; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.script.Script; -import org.elasticsearch.script.ScriptType; import java.io.IOException; import java.util.HashMap; @@ -38,8 +38,7 @@ public TextTemplate(String template) { this.mayRequireCompilation = template.contains("{{"); } - public TextTemplate(String template, @Nullable XContentType contentType, ScriptType type, - @Nullable Map params) { + public TextTemplate(String template, @Nullable XContentType contentType, ScriptType type, @Nullable Map params) { Map options = null; if (type == ScriptType.INLINE) { options = new HashMap<>(); @@ -94,11 +93,11 @@ public XContentType getContentType() { } public ScriptType getType() { - return script != null ? script.getType(): ScriptType.INLINE; + return script != null ? script.getType() : ScriptType.INLINE; } public Map getParams() { - return script != null ? script.getParams(): null; + return script != null ? script.getParams() : null; } @Override @@ -107,8 +106,7 @@ public boolean equals(Object o) { if (o == null || getClass() != o.getClass()) return false; TextTemplate template1 = (TextTemplate) o; - return Objects.equals(script, template1.script) && - Objects.equals(inlineTemplate, template1.inlineTemplate); + return Objects.equals(script, template1.script) && Objects.equals(inlineTemplate, template1.inlineTemplate); } @Override @@ -135,4 +133,3 @@ public static TextTemplate parse(XContentParser parser) throws IOException { } } } - diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/text/TextTemplateEngine.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/text/TextTemplateEngine.java index 03a1d14b28d54..1d50a3980555e 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/text/TextTemplateEngine.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/text/TextTemplateEngine.java @@ -6,11 +6,11 @@ */ package org.elasticsearch.xpack.watcher.common.text; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptService; import org.elasticsearch.script.ScriptType; import org.elasticsearch.script.TemplateScript; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.watcher.Watcher; import java.util.HashMap; @@ -53,16 +53,21 @@ public String render(TextTemplate textTemplate, Map model) { options.put(Script.CONTENT_TYPE_OPTION, mediaType); } - Script script = new Script(textTemplate.getType(), - textTemplate.getType() == ScriptType.STORED ? null : "mustache", template, options, mergedModel); + Script script = new Script( + textTemplate.getType(), + textTemplate.getType() == ScriptType.STORED ? null : "mustache", + template, + options, + mergedModel + ); TemplateScript.Factory compiledTemplate = service.compile(script, Watcher.SCRIPT_TEMPLATE_CONTEXT); return compiledTemplate.newInstance(mergedModel).execute(); } private String trimContentType(TextTemplate textTemplate) { String template = textTemplate.getTemplate(); - if (template.startsWith("__") == false){ - return template; //Doesn't even start with __ so can't have a content type + if (template.startsWith("__") == false) { + return template; // Doesn't even start with __ so can't have a content type } // There must be a __ model, Map resolvedV } resolvedValues.put(arrayPath, resolvedArray); - return new Result(resolvedValues, TYPE, quantifier.eval(resolvedValue, - configuredValue, op)); + return new Result(resolvedValues, TYPE, quantifier.eval(resolvedValue, configuredValue, op)); } @Override @@ -177,11 +226,11 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; ArrayCompareCondition that = (ArrayCompareCondition) o; - return Objects.equals(getArrayPath(), that.getArrayPath()) && - Objects.equals(getPath(), that.getPath()) && - Objects.equals(getOp(), that.getOp()) && - Objects.equals(getValue(), that.getValue()) && - Objects.equals(getQuantifier(), that.getQuantifier()); + return Objects.equals(getArrayPath(), that.getArrayPath()) + && Objects.equals(getPath(), that.getPath()) + && Objects.equals(getOp(), that.getOp()) + && Objects.equals(getValue(), that.getValue()) + && Objects.equals(getQuantifier(), that.getQuantifier()); } @Override @@ -192,14 +241,14 @@ public int hashCode() { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { return builder.startObject() - .startObject(arrayPath) - .field("path", path) - .startObject(op.id()) - .field("value", value) - .field("quantifier", quantifier.id()) - .endObject() - .endObject() - .endObject(); + .startObject(arrayPath) + .field("path", path) + .startObject(op.id()) + .field("value", value) + .field("quantifier", quantifier.id()) + .endObject() + .endObject() + .endObject(); } public enum Op { diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/CompareCondition.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/CompareCondition.java index 05e561e1078df..1698d92cab7f7 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/CompareCondition.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/CompareCondition.java @@ -7,10 +7,10 @@ package org.elasticsearch.xpack.watcher.condition; import org.elasticsearch.ElasticsearchParseException; +import org.elasticsearch.xcontent.ObjectPath; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentUtils; -import org.elasticsearch.xcontent.ObjectPath; import java.io.IOException; import java.time.Clock; @@ -18,7 +18,6 @@ import java.util.Map; import java.util.Objects; - public final class CompareCondition extends AbstractCompareCondition { public static final String TYPE = "compare"; private final String path; @@ -50,8 +49,12 @@ public Object getValue() { public static CompareCondition parse(Clock clock, String watchId, XContentParser parser) throws IOException { if (parser.currentToken() != XContentParser.Token.START_OBJECT) { - throw new ElasticsearchParseException("could not parse [{}] condition for watch [{}]. expected an object but found [{}] " + - "instead", TYPE, watchId, parser.currentToken()); + throw new ElasticsearchParseException( + "could not parse [{}] condition for watch [{}]. expected an object but found [{}] " + "instead", + TYPE, + watchId, + parser.currentToken() + ); } String path = null; Object value = null; @@ -62,35 +65,64 @@ public static CompareCondition parse(Clock clock, String watchId, XContentParser if (token == XContentParser.Token.FIELD_NAME) { path = parser.currentName(); } else if (path == null) { - throw new ElasticsearchParseException("could not parse [{}] condition for watch [{}]. expected a field indicating the " + - "compared path, but found [{}] instead", TYPE, watchId, token); + throw new ElasticsearchParseException( + "could not parse [{}] condition for watch [{}]. expected a field indicating the " + + "compared path, but found [{}] instead", + TYPE, + watchId, + token + ); } else if (token == XContentParser.Token.START_OBJECT) { token = parser.nextToken(); if (token != XContentParser.Token.FIELD_NAME) { - throw new ElasticsearchParseException("could not parse [{}] condition for watch [{}]. expected a field indicating the" + - " comparison operator, but found [{}] instead", TYPE, watchId, token); + throw new ElasticsearchParseException( + "could not parse [{}] condition for watch [{}]. expected a field indicating the" + + " comparison operator, but found [{}] instead", + TYPE, + watchId, + token + ); } try { op = Op.resolve(parser.currentName()); } catch (IllegalArgumentException iae) { - throw new ElasticsearchParseException("could not parse [{}] condition for watch [{}]. unknown comparison operator " + - "[{}]", TYPE, watchId, parser.currentName()); + throw new ElasticsearchParseException( + "could not parse [{}] condition for watch [{}]. unknown comparison operator " + "[{}]", + TYPE, + watchId, + parser.currentName() + ); } token = parser.nextToken(); if (op.supportsStructures() == false && token.isValue() == false && token != XContentParser.Token.VALUE_NULL) { - throw new ElasticsearchParseException("could not parse [{}] condition for watch [{}]. compared value for [{}] with " + - "operation [{}] must either be a numeric, string, boolean or null value, but found [{}] instead", TYPE, - watchId, path, op.name().toLowerCase(Locale.ROOT), token); + throw new ElasticsearchParseException( + "could not parse [{}] condition for watch [{}]. compared value for [{}] with " + + "operation [{}] must either be a numeric, string, boolean or null value, but found [{}] instead", + TYPE, + watchId, + path, + op.name().toLowerCase(Locale.ROOT), + token + ); } value = XContentUtils.readValue(parser, token); token = parser.nextToken(); if (token != XContentParser.Token.END_OBJECT) { - throw new ElasticsearchParseException("could not parse [{}] condition for watch [{}]. expected end of path object, " + - "but found [{}] instead", TYPE, watchId, token); + throw new ElasticsearchParseException( + "could not parse [{}] condition for watch [{}]. expected end of path object, " + "but found [{}] instead", + TYPE, + watchId, + token + ); } } else { - throw new ElasticsearchParseException("could not parse [{}] condition for watch [{}]. expected an object for field [{}] " + - "but found [{}] instead", TYPE, watchId, path, token); + throw new ElasticsearchParseException( + "could not parse [{}] condition for watch [{}]. expected an object for field [{}] " + "but found [{}] instead", + TYPE, + watchId, + path, + token + ); } } return new CompareCondition(path, op, value, clock); @@ -125,11 +157,7 @@ public int hashCode() { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder.startObject() - .startObject(path) - .field(op.id(), value) - .endObject() - .endObject(); + return builder.startObject().startObject(path).field(op.id(), value).endObject().endObject(); } public enum Op { diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/InternalAlwaysCondition.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/InternalAlwaysCondition.java index 2a89ab896e1e2..e3da11b6c616a 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/InternalAlwaysCondition.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/InternalAlwaysCondition.java @@ -19,17 +19,25 @@ public final class InternalAlwaysCondition extends AlwaysCondition implements Ex public static final Result RESULT_INSTANCE = new Result(null, TYPE, true); public static final InternalAlwaysCondition INSTANCE = new InternalAlwaysCondition(); - private InternalAlwaysCondition() { } + private InternalAlwaysCondition() {} public static InternalAlwaysCondition parse(String watchId, XContentParser parser) throws IOException { if (parser.currentToken() != XContentParser.Token.START_OBJECT) { - throw new ElasticsearchParseException("unable to parse [{}] condition for watch [{}]. expected an empty object but found [{}]", - TYPE, watchId, parser.currentName()); + throw new ElasticsearchParseException( + "unable to parse [{}] condition for watch [{}]. expected an empty object but found [{}]", + TYPE, + watchId, + parser.currentName() + ); } XContentParser.Token token = parser.nextToken(); if (token != XContentParser.Token.END_OBJECT) { - throw new ElasticsearchParseException("unable to parse [{}] condition for watch [{}]. expected an empty object but found [{}]", - TYPE, watchId, parser.currentName()); + throw new ElasticsearchParseException( + "unable to parse [{}] condition for watch [{}]. expected an empty object but found [{}]", + TYPE, + watchId, + parser.currentName() + ); } return INSTANCE; } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/LenientCompare.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/LenientCompare.java index 6476e5bcfe44f..bf33c78f31c0e 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/LenientCompare.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/LenientCompare.java @@ -45,8 +45,9 @@ public static Integer compare(Object v1, Object v2) { return null; } } - return ((Number) v1).doubleValue() > ((Number) v2).doubleValue() ? 1 : - ((Number) v1).doubleValue() < ((Number) v2).doubleValue() ? -1 : 0; + return ((Number) v1).doubleValue() > ((Number) v2).doubleValue() ? 1 + : ((Number) v1).doubleValue() < ((Number) v2).doubleValue() ? -1 + : 0; } // special case for strings. If v1 is not a string, we'll convert it to a string diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/NeverCondition.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/NeverCondition.java index 68fa914b7618e..a478fa2d63c1c 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/NeverCondition.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/NeverCondition.java @@ -20,17 +20,25 @@ public final class NeverCondition implements ExecutableCondition { public static final Result RESULT_INSTANCE = new Result(null, TYPE, false); public static final NeverCondition INSTANCE = new NeverCondition(); - private NeverCondition() { } + private NeverCondition() {} public static NeverCondition parse(String watchId, XContentParser parser) throws IOException { if (parser.currentToken() != XContentParser.Token.START_OBJECT) { - throw new ElasticsearchParseException("could not parse [{}] condition for watch [{}]. expected an empty object but found [{}]", - TYPE, watchId, parser.currentName()); + throw new ElasticsearchParseException( + "could not parse [{}] condition for watch [{}]. expected an empty object but found [{}]", + TYPE, + watchId, + parser.currentName() + ); } XContentParser.Token token = parser.nextToken(); if (token != XContentParser.Token.END_OBJECT) { - throw new ElasticsearchParseException("could not parse [{}] condition for watch [{}]. expected an empty object but found [{}]", - TYPE, watchId, parser.currentName()); + throw new ElasticsearchParseException( + "could not parse [{}] condition for watch [{}]. expected an empty object but found [{}]", + TYPE, + watchId, + parser.currentName() + ); } return INSTANCE; } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/ScriptCondition.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/ScriptCondition.java index 364ca197c410c..badc6c1e1ed7c 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/ScriptCondition.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/ScriptCondition.java @@ -7,10 +7,10 @@ package org.elasticsearch.xpack.watcher.condition; import org.elasticsearch.ElasticsearchParseException; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptService; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.watcher.condition.ExecutableCondition; import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext; @@ -46,8 +46,12 @@ public static ScriptCondition parse(ScriptService scriptService, String watchId, Script script = Script.parse(parser); return new ScriptCondition(script, scriptService); } catch (ElasticsearchParseException pe) { - throw new ElasticsearchParseException("could not parse [{}] condition for watch [{}]. failed to parse script", pe, TYPE, - watchId); + throw new ElasticsearchParseException( + "could not parse [{}] condition for watch [{}]. failed to parse script", + pe, + TYPE, + watchId + ); } } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/WatcherConditionScript.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/WatcherConditionScript.java index 04d4b97a8508c..724ba58c7e996 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/WatcherConditionScript.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/condition/WatcherConditionScript.java @@ -43,6 +43,12 @@ public interface Factory { WatcherConditionScript newInstance(Map params, WatchExecutionContext watcherContext); } - public static ScriptContext CONTEXT = new ScriptContext<>("watcher_condition", Factory.class, - 200, TimeValue.timeValueMillis(0), false, true); + public static ScriptContext CONTEXT = new ScriptContext<>( + "watcher_condition", + Factory.class, + 200, + TimeValue.timeValueMillis(0), + false, + true + ); } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/execution/AsyncTriggerEventConsumer.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/execution/AsyncTriggerEventConsumer.java index f8a8fe1702866..1dc43802423ae 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/execution/AsyncTriggerEventConsumer.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/execution/AsyncTriggerEventConsumer.java @@ -30,11 +30,12 @@ public void accept(Iterable events) { executionService.processEventsAsync(events); } catch (Exception e) { logger.error( - (Supplier) () -> new ParameterizedMessage( - "failed to process triggered events [{}]", - (Object) stream(events.spliterator(), false).toArray(size -> - new TriggerEvent[size])), - e); + (Supplier) () -> new ParameterizedMessage( + "failed to process triggered events [{}]", + (Object) stream(events.spliterator(), false).toArray(size -> new TriggerEvent[size]) + ), + e + ); } } } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/execution/CurrentExecutions.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/execution/CurrentExecutions.java index ff6ab0c0d4fba..b017b23fc6b41 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/execution/CurrentExecutions.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/execution/CurrentExecutions.java @@ -43,8 +43,11 @@ public boolean put(String id, ExecutionService.WatchExecution execution) { try { if (seal.get() != null) { // We shouldn't get here, because, ExecutionService#started should have been set to false - throw illegalState("could not register execution [{}]. current executions are sealed and forbid registrations of " + - "additional executions.", id); + throw illegalState( + "could not register execution [{}]. current executions are sealed and forbid registrations of " + + "additional executions.", + id + ); } return currentExecutions.putIfAbsent(id, execution) != null; } finally { @@ -89,7 +92,7 @@ void sealAndAwaitEmpty(TimeValue maxStopTimeout, Runnable stoppedListener) { } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { - //fully stop Watcher after all executions are finished + // fully stop Watcher after all executions are finished stoppedListener.run(); lock.unlock(); } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/execution/ExecutionService.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/execution/ExecutionService.java index f0a1e193cec3f..52072f967ae4d 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/execution/ExecutionService.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/execution/ExecutionService.java @@ -24,21 +24,21 @@ import org.elasticsearch.client.Client; import org.elasticsearch.cluster.routing.Preference; import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.metrics.MeanMetric; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException; import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.common.util.iterable.Iterables; +import org.elasticsearch.core.TimeValue; +import org.elasticsearch.core.Tuple; +import org.elasticsearch.index.engine.DocumentMissingException; +import org.elasticsearch.index.engine.VersionConflictEngineException; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.index.engine.DocumentMissingException; -import org.elasticsearch.index.engine.VersionConflictEngineException; import org.elasticsearch.xpack.core.watcher.WatcherState; import org.elasticsearch.xpack.core.watcher.actions.ActionWrapper; import org.elasticsearch.xpack.core.watcher.actions.ActionWrapperResult; @@ -86,9 +86,11 @@ public class ExecutionService { - public static final Setting DEFAULT_THROTTLE_PERIOD_SETTING = - Setting.positiveTimeSetting("xpack.watcher.execution.default_throttle_period", - TimeValue.timeValueSeconds(5), Setting.Property.NodeScope); + public static final Setting DEFAULT_THROTTLE_PERIOD_SETTING = Setting.positiveTimeSetting( + "xpack.watcher.execution.default_throttle_period", + TimeValue.timeValueSeconds(5), + Setting.Property.NodeScope + ); private static final Logger logger = LogManager.getLogger(ExecutionService.class); @@ -111,9 +113,17 @@ public class ExecutionService { private AtomicReference currentExecutions = new AtomicReference<>(); private final AtomicBoolean paused = new AtomicBoolean(false); - public ExecutionService(Settings settings, HistoryStore historyStore, TriggeredWatchStore triggeredWatchStore, WatchExecutor executor, - Clock clock, WatchParser parser, ClusterService clusterService, Client client, - ExecutorService genericExecutor) { + public ExecutionService( + Settings settings, + HistoryStore historyStore, + TriggeredWatchStore triggeredWatchStore, + WatchExecutor executor, + Clock clock, + WatchParser parser, + ClusterService clusterService, + Client client, + ExecutorService genericExecutor + ) { this.historyStore = historyStore; this.triggeredWatchStore = triggeredWatchStore; this.executor = executor; @@ -218,16 +228,17 @@ void processEventsAsync(Iterable events) throws Exception { } Tuple, List> watchesAndContext = createTriggeredWatchesAndContext(events); List triggeredWatches = watchesAndContext.v1(); - triggeredWatchStore.putAll(triggeredWatches, ActionListener.wrap( - response -> executeTriggeredWatches(response, watchesAndContext), - e -> { + triggeredWatchStore.putAll( + triggeredWatches, + ActionListener.wrap(response -> executeTriggeredWatches(response, watchesAndContext), e -> { Throwable cause = ExceptionsHelper.unwrapCause(e); if (cause instanceof EsRejectedExecutionException) { logger.debug("failed to store watch records due to filled up watcher threadpool"); } else { logger.warn("failed to store watch records", e); } - })); + }) + ); } void processEventsSync(Iterable events) throws IOException { @@ -273,8 +284,10 @@ private Tuple, List> createTrigg * @param response The bulk response containing the response of indexing triggered watches * @param watchesAndContext The triggered watches and context objects needed for execution */ - private void executeTriggeredWatches(final BulkResponse response, - final Tuple, List> watchesAndContext) { + private void executeTriggeredWatches( + final BulkResponse response, + final Tuple, List> watchesAndContext + ) { for (int i = 0; i < response.getItems().length; i++) { BulkItemResponse itemResponse = response.getItems()[i]; if (itemResponse.isFailed()) { @@ -289,7 +302,7 @@ public WatchRecord execute(WatchExecutionContext ctx) { ctx.setNodeId(clusterService.localNode().getId()); WatchRecord record = null; final String watchId = ctx.id().watchId(); - //pull this to a local reference since the class reference can be swapped, and need to ensure same object is used for put/remove + // pull this to a local reference since the class reference can be swapped, and need to ensure same object is used for put/remove final CurrentExecutions currentExecutions = this.currentExecutions.get(); try { boolean executionAlreadyExists = currentExecutions.put(watchId, new WatchExecution(ctx, Thread.currentThread())); @@ -303,8 +316,15 @@ record = ctx.abortBeforeExecution(ExecutionState.NOT_EXECUTED_ALREADY_QUEUED, "W if (resp.isExists() == false) { throw new ResourceNotFoundException("watch [{}] does not exist", watchId); } - return parser.parseWithSecrets(watchId, true, resp.getSourceAsBytesRef(), ctx.executionTime(), XContentType.JSON, - resp.getSeqNo(), resp.getPrimaryTerm()); + return parser.parseWithSecrets( + watchId, + true, + resp.getSourceAsBytesRef(), + ctx.executionTime(), + XContentType.JSON, + resp.getSeqNo(), + resp.getPrimaryTerm() + ); }); } catch (ResourceNotFoundException e) { String message = "unable to find watch for record [" + ctx.id() + "]"; @@ -363,12 +383,10 @@ public void updateWatchStatus(Watch watch) throws IOException { // at the moment we store the status together with the watch, // so we just need to update the watch itself // we do not want to update the status.state field, as it might have been deactivated in-between - Map parameters = Map.of( - Watch.INCLUDE_STATUS_KEY, "true", - WatchStatus.INCLUDE_STATE, "false"); + Map parameters = Map.of(Watch.INCLUDE_STATUS_KEY, "true", WatchStatus.INCLUDE_STATE, "false"); ToXContent.MapParams params = new ToXContent.MapParams(parameters); - XContentBuilder source = JsonXContent.contentBuilder(). - startObject() + XContentBuilder source = JsonXContent.contentBuilder() + .startObject() .field(WatchField.STATUS.getPreferredName(), watch.status(), params) .endObject(); @@ -419,10 +437,10 @@ private void executeAsync(WatchExecutionContext ctx, final TriggeredWatch trigge try { executor.execute(new WatchExecutionTask(ctx, () -> execute(ctx))); } catch (EsRejectedExecutionException e) { - //Using the generic pool here since this can happen from a write thread and we don't want to block a write - //thread to kick off these additional write/delete requests. - //Intentionally not using the HistoryStore or TriggerWatchStore to avoid re-using the same synchronous - //BulkProcessor which can cause a deadlock see #41390 + // Using the generic pool here since this can happen from a write thread and we don't want to block a write + // thread to kick off these additional write/delete requests. + // Intentionally not using the HistoryStore or TriggerWatchStore to avoid re-using the same synchronous + // BulkProcessor which can cause a deadlock see #41390 genericExecutor.execute(new WatchExecutionTask(ctx, () -> { String message = "failed to run triggered watch [" + triggeredWatch.id() + "] due to thread pool capacity"; logger.warn(message); @@ -430,18 +448,24 @@ private void executeAsync(WatchExecutionContext ctx, final TriggeredWatch trigge try { forcePutHistory(record); } catch (Exception exc) { - logger.error((Supplier) () -> - new ParameterizedMessage( + logger.error( + (Supplier) () -> new ParameterizedMessage( "Error storing watch history record for watch [{}] after thread pool rejection", - triggeredWatch.id()), exc); + triggeredWatch.id() + ), + exc + ); } try { deleteTrigger(triggeredWatch.id()); } catch (Exception exc) { - logger.error((Supplier) () -> - new ParameterizedMessage( + logger.error( + (Supplier) () -> new ParameterizedMessage( "Error deleting entry from .triggered_watches for watch [{}] after thread pool rejection", - triggeredWatch.id()), exc); + triggeredWatch.id() + ), + exc + ); } })); } @@ -453,22 +477,27 @@ private void executeAsync(WatchExecutionContext ctx, final TriggeredWatch trigge */ private void forcePutHistory(WatchRecord watchRecord) { try { - try (XContentBuilder builder = XContentFactory.jsonBuilder(); - ThreadContext.StoredContext ignore = client.threadPool().getThreadContext().stashWithOrigin(WATCHER_ORIGIN)) { + try ( + XContentBuilder builder = XContentFactory.jsonBuilder(); + ThreadContext.StoredContext ignore = client.threadPool().getThreadContext().stashWithOrigin(WATCHER_ORIGIN) + ) { watchRecord.toXContent(builder, WatcherParams.HIDE_SECRETS); - IndexRequest request = new IndexRequest(HistoryStoreField.DATA_STREAM) - .id(watchRecord.id().value()) + IndexRequest request = new IndexRequest(HistoryStoreField.DATA_STREAM).id(watchRecord.id().value()) .source(builder) .opType(IndexRequest.OpType.CREATE); client.index(request).get(30, TimeUnit.SECONDS); logger.debug("indexed watch history record [{}]", watchRecord.id().value()); } catch (VersionConflictEngineException vcee) { - watchRecord = new WatchRecord.MessageWatchRecord(watchRecord, ExecutionState.EXECUTED_MULTIPLE_TIMES, - "watch record [{ " + watchRecord.id() + " }] has been stored before, previous state [" + watchRecord.state() + "]"); - try (XContentBuilder xContentBuilder = XContentFactory.jsonBuilder(); - ThreadContext.StoredContext ignore = client.threadPool().getThreadContext().stashWithOrigin(WATCHER_ORIGIN)) { - IndexRequest request = new IndexRequest(HistoryStoreField.DATA_STREAM) - .id(watchRecord.id().value()) + watchRecord = new WatchRecord.MessageWatchRecord( + watchRecord, + ExecutionState.EXECUTED_MULTIPLE_TIMES, + "watch record [{ " + watchRecord.id() + " }] has been stored before, previous state [" + watchRecord.state() + "]" + ); + try ( + XContentBuilder xContentBuilder = XContentFactory.jsonBuilder(); + ThreadContext.StoredContext ignore = client.threadPool().getThreadContext().stashWithOrigin(WATCHER_ORIGIN) + ) { + IndexRequest request = new IndexRequest(HistoryStoreField.DATA_STREAM).id(watchRecord.id().value()) .source(xContentBuilder.value(watchRecord)); client.index(request).get(30, TimeUnit.SECONDS); } @@ -549,16 +578,29 @@ public void executeTriggeredWatches(Collection triggeredWatches) for (TriggeredWatch triggeredWatch : triggeredWatches) { GetResponse response = getWatch(triggeredWatch.id().watchId()); if (response.isExists() == false) { - String message = "unable to find watch for record [" + triggeredWatch.id().watchId() + "]/[" + triggeredWatch.id() + - "], perhaps it has been deleted, ignoring..."; - WatchRecord record = new WatchRecord.MessageWatchRecord(triggeredWatch.id(), triggeredWatch.triggerEvent(), - ExecutionState.NOT_EXECUTED_WATCH_MISSING, message, clusterService.localNode().getId()); + String message = "unable to find watch for record [" + + triggeredWatch.id().watchId() + + "]/[" + + triggeredWatch.id() + + "], perhaps it has been deleted, ignoring..."; + WatchRecord record = new WatchRecord.MessageWatchRecord( + triggeredWatch.id(), + triggeredWatch.triggerEvent(), + ExecutionState.NOT_EXECUTED_WATCH_MISSING, + message, + clusterService.localNode().getId() + ); historyStore.forcePut(record); triggeredWatchStore.delete(triggeredWatch.id()); } else { ZonedDateTime now = clock.instant().atZone(ZoneOffset.UTC); - TriggeredExecutionContext ctx = new TriggeredExecutionContext(triggeredWatch.id().watchId(), now, - triggeredWatch.triggerEvent(), defaultThrottlePeriod, true); + TriggeredExecutionContext ctx = new TriggeredExecutionContext( + triggeredWatch.id().watchId(), + now, + triggeredWatch.triggerEvent(), + defaultThrottlePeriod, + true + ); executeAsync(ctx, triggeredWatch); counter++; } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/execution/ManualExecutionContext.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/execution/ManualExecutionContext.java index 47cba5fc96921..2a9e20877e21f 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/execution/ManualExecutionContext.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/execution/ManualExecutionContext.java @@ -30,9 +30,17 @@ public class ManualExecutionContext extends WatchExecutionContext { private final boolean recordExecution; private final boolean knownWatch; - ManualExecutionContext(Watch watch, boolean knownWatch, ZonedDateTime executionTime, ManualTriggerEvent triggerEvent, - TimeValue defaultThrottlePeriod, Input.Result inputResult, Condition.Result conditionResult, - Map actionModes, boolean recordExecution) throws Exception { + ManualExecutionContext( + Watch watch, + boolean knownWatch, + ZonedDateTime executionTime, + ManualTriggerEvent triggerEvent, + TimeValue defaultThrottlePeriod, + Input.Result inputResult, + Condition.Result conditionResult, + Map actionModes, + boolean recordExecution + ) throws Exception { super(watch.id(), executionTime, triggerEvent, defaultThrottlePeriod); @@ -54,13 +62,15 @@ public class ManualExecutionContext extends WatchExecutionContext { boolean throttleAll = allMode == ActionExecutionMode.SKIP; for (ActionWrapper action : watch.actions()) { if (throttleAll) { - onActionResult(new ActionWrapperResult(action.id(), - new Action.Result.Throttled(action.action().type(), "manually skipped"))); + onActionResult( + new ActionWrapperResult(action.id(), new Action.Result.Throttled(action.action().type(), "manually skipped")) + ); } else { ActionExecutionMode mode = actionModes.get(action.id()); if (mode == ActionExecutionMode.SKIP) { - onActionResult(new ActionWrapperResult(action.id(), - new Action.Result.Throttled(action.action().type(), "manually skipped"))); + onActionResult( + new ActionWrapperResult(action.id(), new Action.Result.Throttled(action.action().type(), "manually skipped")) + ); } } } @@ -167,8 +177,17 @@ public ManualExecutionContext build() throws Exception { if (executionTime == null) { executionTime = ZonedDateTime.now(ZoneOffset.UTC); } - ManualExecutionContext context = new ManualExecutionContext(watch, knownWatch, executionTime, triggerEvent, - defaultThrottlePeriod, inputResult, conditionResult, unmodifiableMap(actionModes), recordExecution); + ManualExecutionContext context = new ManualExecutionContext( + watch, + knownWatch, + executionTime, + triggerEvent, + defaultThrottlePeriod, + inputResult, + conditionResult, + unmodifiableMap(actionModes), + recordExecution + ); actionModes = null; return context; } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/execution/SyncTriggerEventConsumer.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/execution/SyncTriggerEventConsumer.java index a109e81ce1319..9d9bbd26699d1 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/execution/SyncTriggerEventConsumer.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/execution/SyncTriggerEventConsumer.java @@ -6,8 +6,8 @@ */ package org.elasticsearch.xpack.watcher.execution; -import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.message.ParameterizedMessage; import org.apache.logging.log4j.util.Supplier; import org.elasticsearch.xpack.core.watcher.trigger.TriggerEvent; @@ -31,11 +31,12 @@ public void accept(Iterable events) { executionService.processEventsSync(events); } catch (Exception e) { logger.error( - (Supplier) () -> new ParameterizedMessage( - "failed to process triggered events [{}]", - (Object) stream(events.spliterator(), false).toArray(size -> - new TriggerEvent[size])), - e); + (Supplier) () -> new ParameterizedMessage( + "failed to process triggered events [{}]", + (Object) stream(events.spliterator(), false).toArray(size -> new TriggerEvent[size]) + ), + e + ); } } } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/execution/TriggeredExecutionContext.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/execution/TriggeredExecutionContext.java index 1c41a51ab3130..0bdd2c2b7816c 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/execution/TriggeredExecutionContext.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/execution/TriggeredExecutionContext.java @@ -16,13 +16,22 @@ public class TriggeredExecutionContext extends WatchExecutionContext { private final boolean overrideOnConflict; - public TriggeredExecutionContext(String watchId, ZonedDateTime executionTime, TriggerEvent triggerEvent, - TimeValue defaultThrottlePeriod) { + public TriggeredExecutionContext( + String watchId, + ZonedDateTime executionTime, + TriggerEvent triggerEvent, + TimeValue defaultThrottlePeriod + ) { this(watchId, executionTime, triggerEvent, defaultThrottlePeriod, false); } - TriggeredExecutionContext(String watchId, ZonedDateTime executionTime, TriggerEvent triggerEvent, TimeValue defaultThrottlePeriod, - boolean overrideOnConflict) { + TriggeredExecutionContext( + String watchId, + ZonedDateTime executionTime, + TriggerEvent triggerEvent, + TimeValue defaultThrottlePeriod, + boolean overrideOnConflict + ) { super(watchId, executionTime, triggerEvent, defaultThrottlePeriod); this.overrideOnConflict = overrideOnConflict; } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/execution/TriggeredWatch.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/execution/TriggeredWatch.java index 5128896d5646a..b547cb26cc3b8 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/execution/TriggeredWatch.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/execution/TriggeredWatch.java @@ -7,13 +7,13 @@ package org.elasticsearch.xpack.watcher.execution; import org.elasticsearch.ElasticsearchException; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.watcher.execution.Wid; import org.elasticsearch.xpack.core.watcher.trigger.TriggerEvent; @@ -57,8 +57,9 @@ public Parser(TriggerService triggerService) { public TriggeredWatch parse(String id, long version, BytesReference source) { // EMPTY is safe here because we never use namedObject - try (XContentParser parser = XContentHelper - .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, source)) { + try ( + XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, source) + ) { return parse(id, version, parser); } catch (IOException e) { throw new ElasticsearchException("unable to parse watch record", e); @@ -87,7 +88,7 @@ public TriggeredWatch parse(String id, long version, XContentParser parser) thro } TriggeredWatch record = new TriggeredWatch(wid, triggerEvent); - assert record.triggerEvent() != null : "watch record [" + id +"] is missing trigger"; + assert record.triggerEvent() != null : "watch record [" + id + "] is missing trigger"; return record; } } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/execution/TriggeredWatchStore.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/execution/TriggeredWatchStore.java index 4290138d07e69..2376a8f51a71a 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/execution/TriggeredWatchStore.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/execution/TriggeredWatchStore.java @@ -27,13 +27,13 @@ import org.elasticsearch.cluster.routing.Preference; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.index.IndexNotFoundException; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.sort.SortBuilders; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xpack.core.ClientHelper; import org.elasticsearch.xpack.core.watcher.execution.TriggeredWatchStoreField; import org.elasticsearch.xpack.core.watcher.execution.Wid; @@ -75,7 +75,7 @@ public TriggeredWatchStore(Settings settings, Client client, TriggeredWatch.Pars public void putAll(final List triggeredWatches, final ActionListener listener) throws IOException { if (triggeredWatches.isEmpty()) { - listener.onResponse(new BulkResponse(new BulkItemResponse[]{}, 0)); + listener.onResponse(new BulkResponse(new BulkItemResponse[] {}, 0)); return; } @@ -149,13 +149,9 @@ public Collection findTriggeredWatches(Collection watches Set ids = watches.stream().map(Watch::id).collect(Collectors.toSet()); Collection triggeredWatches = new ArrayList<>(ids.size()); - SearchRequest searchRequest = new SearchRequest(TriggeredWatchStoreField.INDEX_NAME) - .scroll(scrollTimeout) + SearchRequest searchRequest = new SearchRequest(TriggeredWatchStoreField.INDEX_NAME).scroll(scrollTimeout) .preference(Preference.LOCAL.toString()) - .source(new SearchSourceBuilder() - .size(scrollSize) - .sort(SortBuilders.fieldSort("_doc")) - .version(true)); + .source(new SearchSourceBuilder().size(scrollSize).sort(SortBuilders.fieldSort("_doc")).version(true)); SearchResponse response = null; try { @@ -186,7 +182,8 @@ public Collection findTriggeredWatches(Collection watches public static boolean validate(ClusterState state) { IndexMetadata indexMetadata = WatchStoreUtils.getConcreteIndex(TriggeredWatchStoreField.INDEX_NAME, state.metadata()); - return indexMetadata == null || (indexMetadata.getState() == IndexMetadata.State.OPEN && - state.routingTable().index(indexMetadata.getIndex()).allPrimaryShardsActive()); + return indexMetadata == null + || (indexMetadata.getState() == IndexMetadata.State.OPEN + && state.routingTable().index(indexMetadata.getIndex()).allPrimaryShardsActive()); } } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/history/HistoryStore.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/history/HistoryStore.java index f99e81e51bdbe..f72027ed4eb31 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/history/HistoryStore.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/history/HistoryStore.java @@ -57,12 +57,12 @@ public void put(WatchRecord watchRecord) throws Exception { * Any existing watchRecord will be overwritten. */ public void forcePut(WatchRecord watchRecord) { - try (XContentBuilder builder = XContentFactory.jsonBuilder()) { - watchRecord.toXContent(builder, WatcherParams.HIDE_SECRETS); + try (XContentBuilder builder = XContentFactory.jsonBuilder()) { + watchRecord.toXContent(builder, WatcherParams.HIDE_SECRETS); - IndexRequest request = new IndexRequest(HistoryStoreField.DATA_STREAM).id(watchRecord.id().value()).source(builder); - request.opType(DocWriteRequest.OpType.CREATE); - bulkProcessor.add(request); + IndexRequest request = new IndexRequest(HistoryStoreField.DATA_STREAM).id(watchRecord.id().value()).source(builder); + request.opType(DocWriteRequest.OpType.CREATE); + bulkProcessor.add(request); } catch (IOException ioe) { final WatchRecord wr = watchRecord; logger.error((Supplier) () -> new ParameterizedMessage("failed to persist watch record [{}]", wr), ioe); @@ -78,8 +78,9 @@ public void forcePut(WatchRecord watchRecord) { */ public static boolean validate(ClusterState state) { IndexMetadata indexMetadata = WatchStoreUtils.getConcreteIndex(HistoryStoreField.DATA_STREAM, state.metadata()); - return indexMetadata == null || (indexMetadata.getState() == IndexMetadata.State.OPEN && - state.routingTable().index(indexMetadata.getIndex()).allPrimaryShardsActive()); + return indexMetadata == null + || (indexMetadata.getState() == IndexMetadata.State.OPEN + && state.routingTable().index(indexMetadata.getIndex()).allPrimaryShardsActive()); } public void flush() { diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/InputBuilders.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/InputBuilders.java index c211dbc5b094c..50e0b869c01d7 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/InputBuilders.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/InputBuilders.java @@ -21,8 +21,7 @@ public final class InputBuilders { - private InputBuilders() { - } + private InputBuilders() {} public static NoneInput.Builder noneInput() { return NoneInput.builder(); diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/InputRegistry.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/InputRegistry.java index 4b84de5303db2..0f022019911c4 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/InputRegistry.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/InputRegistry.java @@ -37,8 +37,11 @@ public InputRegistry(Map> factories) { String type = null; if (parser.currentToken() != XContentParser.Token.START_OBJECT) { - throw new ElasticsearchParseException("could not parse input for watch [{}]. expected an object representing the input, but " + - "found [{}] instead", watchId, parser.currentToken()); + throw new ElasticsearchParseException( + "could not parse input for watch [{}]. expected an object representing the input, but " + "found [{}] instead", + watchId, + parser.currentToken() + ); } XContentParser.Token token; @@ -47,8 +50,11 @@ public InputRegistry(Map> factories) { if (token == XContentParser.Token.FIELD_NAME) { type = parser.currentName(); } else if (type == null) { - throw new ElasticsearchParseException("could not parse input for watch [{}]. expected field indicating the input type, " + - "but found [{}] instead", watchId, token); + throw new ElasticsearchParseException( + "could not parse input for watch [{}]. expected field indicating the input type, " + "but found [{}] instead", + watchId, + token + ); } else if (token == XContentParser.Token.START_OBJECT) { InputFactory factory = factories.get(type); if (factory == null) { @@ -56,14 +62,21 @@ public InputRegistry(Map> factories) { } input = factory.parseExecutable(watchId, parser); } else { - throw new ElasticsearchParseException("could not parse input for watch [{}]. expected an object representing input [{}], " + - "but found [{}] instead", watchId, type, token); + throw new ElasticsearchParseException( + "could not parse input for watch [{}]. expected an object representing input [{}], " + "but found [{}] instead", + watchId, + type, + token + ); } } if (input == null) { - throw new ElasticsearchParseException("could not parse input for watch [{}]. expected field indicating the input type, but " + - "found an empty object instead", watchId, token); + throw new ElasticsearchParseException( + "could not parse input for watch [{}]. expected field indicating the input type, but " + "found an empty object instead", + watchId, + token + ); } return input; diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/chain/ChainInput.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/chain/ChainInput.java index 678aa2ce4e1c2..c9bde8b83889d 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/chain/ChainInput.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/chain/ChainInput.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.watcher.input.chain; import org.elasticsearch.ElasticsearchParseException; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.core.Tuple; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.watcher.input.Input; @@ -78,8 +78,8 @@ public static ChainInput parse(String watchId, XContentParser parser, InputRegis return new ChainInput(inputs); } - private static Input parseSingleInput(String watchId, String name, XContentParser parser, - InputRegistry inputRegistry) throws IOException { + private static Input parseSingleInput(String watchId, String name, XContentParser parser, InputRegistry inputRegistry) + throws IOException { if (parser.nextToken() != XContentParser.Token.START_OBJECT) { throw new ElasticsearchParseException("Expected starting JSON object after [{}] in watch [{}]", name, watchId); } @@ -88,8 +88,12 @@ private static Input parseSingleInput(String watchId, String name, XContentParse // expecting closing of two json object to start the next element in the array if (parser.currentToken() != XContentParser.Token.END_OBJECT || parser.nextToken() != XContentParser.Token.END_OBJECT) { - throw new ElasticsearchParseException("Expected closing JSON object after parsing input [{}] named [{}] in watch [{}]", - input.type(), name, watchId); + throw new ElasticsearchParseException( + "Expected closing JSON object after parsing input [{}] named [{}] in watch [{}]", + input.type(), + name, + watchId + ); } return input; diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/chain/ChainInputFactory.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/chain/ChainInputFactory.java index ae8ce24e77364..9ad8a205e6cc1 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/chain/ChainInputFactory.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/chain/ChainInputFactory.java @@ -40,8 +40,8 @@ public ExecutableChainInput createExecutable(ChainInput input) { List>> executableInputs = new ArrayList<>(); for (Tuple tuple : input.getInputs()) { @SuppressWarnings("unchecked") - ExecutableInput executableInput = - ((InputFactory) inputRegistry.factories().get(tuple.v2().type())).createExecutable(tuple.v2()); + ExecutableInput executableInput = ((InputFactory) inputRegistry.factories().get(tuple.v2().type())) + .createExecutable(tuple.v2()); executableInputs.add(new Tuple<>(tuple.v1(), executableInput)); } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/chain/ExecutableChainInput.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/chain/ExecutableChainInput.java index 64b78f6081abf..a2554eb40c52d 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/chain/ExecutableChainInput.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/chain/ExecutableChainInput.java @@ -6,8 +6,8 @@ */ package org.elasticsearch.xpack.watcher.input.chain; -import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.elasticsearch.core.Tuple; import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext; import org.elasticsearch.xpack.core.watcher.input.ExecutableInput; @@ -21,7 +21,7 @@ import static org.elasticsearch.xpack.watcher.input.chain.ChainInput.TYPE; -public class ExecutableChainInput extends ExecutableInput { +public class ExecutableChainInput extends ExecutableInput { private static final Logger logger = LogManager.getLogger(ExecutableChainInput.class); private List>> inputs; diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/http/ExecutableHttpInput.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/http/ExecutableHttpInput.java index c33e3f497b4f9..5a6a9ae97f298 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/http/ExecutableHttpInput.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/http/ExecutableHttpInput.java @@ -6,13 +6,12 @@ */ package org.elasticsearch.xpack.watcher.input.http; - -import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext; @@ -72,21 +71,28 @@ HttpInput.Result doExecute(WatchExecutionContext ctx, HttpRequest request) throw final XContentType contentType; XContentType responseContentType = response.xContentType(); if (input.getExpectedResponseXContentType() == null) { - //Attempt to auto detect content type, if not set in response + // Attempt to auto detect content type, if not set in response contentType = responseContentType != null ? responseContentType : XContentHelper.xContentType(response.body()); } else { contentType = input.getExpectedResponseXContentType().contentType(); if (responseContentType != contentType) { - logger.warn("[{}] [{}] input expected content type [{}] but read [{}] from headers, using expected one", type(), ctx.id(), - input.getExpectedResponseXContentType(), responseContentType); + logger.warn( + "[{}] [{}] input expected content type [{}] but read [{}] from headers, using expected one", + type(), + ctx.id(), + input.getExpectedResponseXContentType(), + responseContentType + ); } } if (contentType != null) { // EMPTY is safe here because we never use namedObject - try (InputStream stream = response.body().streamInput(); - XContentParser parser = contentType.xContent() - .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) { + try ( + InputStream stream = response.body().streamInput(); + XContentParser parser = contentType.xContent() + .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream) + ) { if (input.getExtractKeys() != null) { payloadMap.putAll(XContentFilterKeysUtils.filterMapOrdered(input.getExtractKeys(), parser)); } else { @@ -99,8 +105,13 @@ HttpInput.Result doExecute(WatchExecutionContext ctx, HttpRequest request) throw } } } catch (Exception e) { - throw new ElasticsearchParseException("could not parse response body [{}] it does not appear to be [{}]", type(), ctx.id(), - response.body().utf8ToString(), contentType.queryParameter()); + throw new ElasticsearchParseException( + "could not parse response body [{}] it does not appear to be [{}]", + type(), + ctx.id(), + response.body().utf8ToString(), + contentType.queryParameter() + ); } } else { payloadMap.put("_value", response.body().utf8ToString()); diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/http/HttpInput.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/http/HttpInput.java index 0ee9f20802853..81c767e2f1198 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/http/HttpInput.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/http/HttpInput.java @@ -30,11 +30,16 @@ public class HttpInput implements Input { public static final String TYPE = "http"; private final HttpRequestTemplate request; - @Nullable private final HttpContentType expectedResponseXContentType; - @Nullable private final Set extractKeys; - - public HttpInput(HttpRequestTemplate request, @Nullable HttpContentType expectedResponseXContentType, - @Nullable Set extractKeys) { + @Nullable + private final HttpContentType expectedResponseXContentType; + @Nullable + private final Set extractKeys; + + public HttpInput( + HttpRequestTemplate request, + @Nullable HttpContentType expectedResponseXContentType, + @Nullable Set extractKeys + ) { this.request = request; this.expectedResponseXContentType = expectedResponseXContentType; this.extractKeys = extractKeys; @@ -85,8 +90,12 @@ public static HttpInput parse(String watchId, XContentParser parser) throws IOEx try { request = HttpRequestTemplate.Parser.parse(parser); } catch (ElasticsearchParseException pe) { - throw new ElasticsearchParseException("could not parse [{}] input for watch [{}]. failed to parse http request " + - "template", pe, TYPE, watchId); + throw new ElasticsearchParseException( + "could not parse [{}] input for watch [{}]. failed to parse http request " + "template", + pe, + TYPE, + watchId + ); } } else if (token == XContentParser.Token.START_ARRAY) { if (Field.EXTRACT.getPreferredName().equals(currentFieldName)) { @@ -95,39 +104,69 @@ public static HttpInput parse(String watchId, XContentParser parser) throws IOEx if (token == XContentParser.Token.VALUE_STRING) { extract.add(parser.text()); } else { - throw new ElasticsearchParseException("could not parse [{}] input for watch [{}]. expected a string value as " + - "an [{}] item but found [{}] instead", TYPE, watchId, currentFieldName, token); + throw new ElasticsearchParseException( + "could not parse [{}] input for watch [{}]. expected a string value as " + + "an [{}] item but found [{}] instead", + TYPE, + watchId, + currentFieldName, + token + ); } } } else { - throw new ElasticsearchParseException("could not parse [{}] input for watch [{}]. unexpected array field [{}]", TYPE, - watchId, currentFieldName); + throw new ElasticsearchParseException( + "could not parse [{}] input for watch [{}]. unexpected array field [{}]", + TYPE, + watchId, + currentFieldName + ); } } else if (token == XContentParser.Token.VALUE_STRING) { if (Field.RESPONSE_CONTENT_TYPE.match(currentFieldName, parser.getDeprecationHandler())) { expectedResponseBodyType = HttpContentType.resolve(parser.text()); if (expectedResponseBodyType == null) { - throw new ElasticsearchParseException("could not parse [{}] input for watch [{}]. unknown content type [{}]", - TYPE, watchId, parser.text()); + throw new ElasticsearchParseException( + "could not parse [{}] input for watch [{}]. unknown content type [{}]", + TYPE, + watchId, + parser.text() + ); } } else { - throw new ElasticsearchParseException("could not parse [{}] input for watch [{}]. unexpected string field [{}]", - TYPE, watchId, currentFieldName); + throw new ElasticsearchParseException( + "could not parse [{}] input for watch [{}]. unexpected string field [{}]", + TYPE, + watchId, + currentFieldName + ); } } else { - throw new ElasticsearchParseException("could not parse [{}] input for watch [{}]. unexpected token [{}]", TYPE, watchId, - token); + throw new ElasticsearchParseException( + "could not parse [{}] input for watch [{}]. unexpected token [{}]", + TYPE, + watchId, + token + ); } } if (request == null) { - throw new ElasticsearchParseException("could not parse [{}] input for watch [{}]. missing require [{}] field", TYPE, watchId, - Field.REQUEST.getPreferredName()); + throw new ElasticsearchParseException( + "could not parse [{}] input for watch [{}]. missing require [{}] field", + TYPE, + watchId, + Field.REQUEST.getPreferredName() + ); } - if (expectedResponseBodyType == HttpContentType.TEXT && extract != null ) { - throw new ElasticsearchParseException("could not parse [{}] input for watch [{}]. key extraction is not supported for content" + - " type [{}]", TYPE, watchId, expectedResponseBodyType); + if (expectedResponseBodyType == HttpContentType.TEXT && extract != null) { + throw new ElasticsearchParseException( + "could not parse [{}] input for watch [{}]. key extraction is not supported for content" + " type [{}]", + TYPE, + watchId, + expectedResponseBodyType + ); } return new HttpInput(request, expectedResponseBodyType, extract); @@ -139,7 +178,8 @@ public static Builder builder(HttpRequestTemplate httpRequest) { public static class Result extends Input.Result { - @Nullable private final HttpRequest request; + @Nullable + private final HttpRequest request; final int statusCode; public Result(HttpRequest request, int statusCode, Payload payload) { diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/search/ExecutableSearchInput.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/search/ExecutableSearchInput.java index 7156496933fce..611212a8b1ccf 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/search/ExecutableSearchInput.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/search/ExecutableSearchInput.java @@ -6,22 +6,22 @@ */ package org.elasticsearch.xpack.watcher.input.search; -import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchType; import org.elasticsearch.client.Client; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.script.Script; import org.elasticsearch.search.SearchHit; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.ClientHelper; import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext; import org.elasticsearch.xpack.core.watcher.input.ExecutableInput; @@ -49,8 +49,12 @@ public class ExecutableSearchInput extends ExecutableInput client.search(searchRequest).actionGet(timeout)); + final SearchResponse response = ClientHelper.executeWithHeaders( + ctx.watch().status().getHeaders(), + ClientHelper.WATCHER_ORIGIN, + client, + () -> client.search(searchRequest).actionGet(timeout) + ); if (logger.isDebugEnabled()) { logger.debug("[{}] found [{}] hits", ctx.id(), response.getHits().getTotalHits().value); @@ -98,8 +106,14 @@ SearchInput.Result doExecute(WatchExecutionContext ctx, WatcherSearchTemplateReq if (input.getExtractKeys() != null) { BytesReference bytes = XContentHelper.toXContent(response, XContentType.SMILE, params, false); // EMPTY is safe here because we never use namedObject - try (XContentParser parser = XContentHelper - .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, bytes, XContentType.SMILE)) { + try ( + XContentParser parser = XContentHelper.createParser( + NamedXContentRegistry.EMPTY, + LoggingDeprecationHandler.INSTANCE, + bytes, + XContentType.SMILE + ) + ) { Map filteredKeys = XContentFilterKeysUtils.filterMapOrdered(input.getExtractKeys(), parser); payload = new Payload.Simple(filteredKeys); } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/search/SearchInput.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/search/SearchInput.java index 99657ca5b391d..e9492fe5ec876 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/search/SearchInput.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/search/SearchInput.java @@ -7,10 +7,10 @@ package org.elasticsearch.xpack.watcher.input.search; import org.elasticsearch.ElasticsearchParseException; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.time.DateUtils; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.watcher.input.Input; @@ -34,12 +34,19 @@ public class SearchInput implements Input { public static final String TYPE = "search"; private final WatcherSearchTemplateRequest request; - @Nullable private final Set extractKeys; - @Nullable private final TimeValue timeout; - @Nullable private final ZoneId dynamicNameTimeZone; - - public SearchInput(WatcherSearchTemplateRequest request, @Nullable Set extractKeys, - @Nullable TimeValue timeout, @Nullable ZoneId dynamicNameTimeZone) { + @Nullable + private final Set extractKeys; + @Nullable + private final TimeValue timeout; + @Nullable + private final ZoneId dynamicNameTimeZone; + + public SearchInput( + WatcherSearchTemplateRequest request, + @Nullable Set extractKeys, + @Nullable TimeValue timeout, + @Nullable ZoneId dynamicNameTimeZone + ) { this.request = request; this.extractKeys = extractKeys; this.timeout = timeout; @@ -125,8 +132,13 @@ public static SearchInput parse(String watchId, XContentParser parser) throws IO try { request = WatcherSearchTemplateRequest.fromXContent(parser, ExecutableSearchInput.DEFAULT_SEARCH_TYPE); } catch (ElasticsearchParseException srpe) { - throw new ElasticsearchParseException("could not parse [{}] input for watch [{}]. failed to parse [{}]", srpe, TYPE, - watchId, currentFieldName); + throw new ElasticsearchParseException( + "could not parse [{}] input for watch [{}]. failed to parse [{}]", + srpe, + TYPE, + watchId, + currentFieldName + ); } } else if (token == XContentParser.Token.START_ARRAY) { if (Field.EXTRACT.match(currentFieldName, parser.getDeprecationHandler())) { @@ -135,13 +147,23 @@ public static SearchInput parse(String watchId, XContentParser parser) throws IO if (token == XContentParser.Token.VALUE_STRING) { extract.add(parser.text()); } else { - throw new ElasticsearchParseException("could not parse [{}] input for watch [{}]. expected a string value in " + - "[{}] array, but found [{}] instead", TYPE, watchId, currentFieldName, token); + throw new ElasticsearchParseException( + "could not parse [{}] input for watch [{}]. expected a string value in " + + "[{}] array, but found [{}] instead", + TYPE, + watchId, + currentFieldName, + token + ); } } } else { - throw new ElasticsearchParseException("could not parse [{}] input for watch [{}]. unexpected array field [{}]", TYPE, - watchId, currentFieldName); + throw new ElasticsearchParseException( + "could not parse [{}] input for watch [{}]. unexpected array field [{}]", + TYPE, + watchId, + currentFieldName + ); } } else if (Field.TIMEOUT.match(currentFieldName, parser.getDeprecationHandler())) { timeout = timeValueMillis(parser.longValue()); @@ -152,18 +174,31 @@ public static SearchInput parse(String watchId, XContentParser parser) throws IO if (token == XContentParser.Token.VALUE_STRING) { dynamicNameTimeZone = DateUtils.of(parser.text()); } else { - throw new ElasticsearchParseException("could not parse [{}] input for watch [{}]. failed to parse [{}]. must be a " + - "string value (e.g. 'UTC' or '+01:00').", TYPE, watchId, currentFieldName); + throw new ElasticsearchParseException( + "could not parse [{}] input for watch [{}]. failed to parse [{}]. must be a " + + "string value (e.g. 'UTC' or '+01:00').", + TYPE, + watchId, + currentFieldName + ); } } else { - throw new ElasticsearchParseException("could not parse [{}] input for watch [{}]. unexpected token [{}]", TYPE, watchId, - token); + throw new ElasticsearchParseException( + "could not parse [{}] input for watch [{}]. unexpected token [{}]", + TYPE, + watchId, + token + ); } } if (request == null) { - throw new ElasticsearchParseException("could not parse [{}] input for watch [{}]. missing required [{}] field", TYPE, - watchId, Field.REQUEST.getPreferredName()); + throw new ElasticsearchParseException( + "could not parse [{}] input for watch [{}]. missing required [{}] field", + TYPE, + watchId, + Field.REQUEST.getPreferredName() + ); } return new SearchInput(request, extract, timeout, dynamicNameTimeZone); } @@ -174,7 +209,8 @@ public static Builder builder(WatcherSearchTemplateRequest request) { public static class Result extends Input.Result { - @Nullable private final WatcherSearchTemplateRequest request; + @Nullable + private final WatcherSearchTemplateRequest request; public Result(WatcherSearchTemplateRequest request, Payload payload) { super(TYPE, payload); diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/search/SearchInputFactory.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/search/SearchInputFactory.java index 5fc84df91621a..ae89533b5f60c 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/search/SearchInputFactory.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/search/SearchInputFactory.java @@ -9,9 +9,9 @@ import org.elasticsearch.client.Client; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.script.ScriptService; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.script.ScriptService; import org.elasticsearch.xpack.watcher.input.InputFactory; import org.elasticsearch.xpack.watcher.support.search.WatcherSearchTemplateService; @@ -23,8 +23,7 @@ public class SearchInputFactory extends InputFactory { // using the new updated cluster settings private volatile SecureSettings cachedSecureSettings; - public NotificationService(String type, Settings settings, ClusterSettings clusterSettings, List> pluginDynamicSettings, - List> pluginSecureSettings) { + public NotificationService( + String type, + Settings settings, + ClusterSettings clusterSettings, + List> pluginDynamicSettings, + List> pluginSecureSettings + ) { this(type, settings, pluginSecureSettings); // register a grand updater for the whole group, as settings are usable together clusterSettings.addSettingsUpdateConsumer(this::clusterSettingsConsumer, pluginDynamicSettings); @@ -113,8 +118,14 @@ public Account getAccount(String name) { } LazyInitializable theAccount = accounts.getOrDefault(name, defaultAccount); if (theAccount == null && name == null) { - throw new IllegalArgumentException("no accounts of type [" + type + "] configured. " + - "Please set up an account using the [xpack.notification." + type +"] settings"); + throw new IllegalArgumentException( + "no accounts of type [" + + type + + "] configured. " + + "Please set up an account using the [xpack.notification." + + type + + "] settings" + ); } if (theAccount == null) { throw new IllegalArgumentException("no account found for name: [" + name + "]"); @@ -134,20 +145,23 @@ private Set getAccountNames(Settings settings) { return settings.get("xpack.notification." + type + ".default_account"); } - private Map> createAccounts(Settings settings, Set accountNames, - BiFunction accountFactory) { + private Map> createAccounts( + Settings settings, + Set accountNames, + BiFunction accountFactory + ) { final Map> accounts = new HashMap<>(); for (final String accountName : accountNames) { final Settings accountSettings = settings.getAsSettings(getNotificationsAccountPrefix() + accountName); - accounts.put(accountName, new LazyInitializable<>(() -> { - return accountFactory.apply(accountName, accountSettings); - })); + accounts.put(accountName, new LazyInitializable<>(() -> { return accountFactory.apply(accountName, accountSettings); })); } return Collections.unmodifiableMap(accounts); } - private @Nullable LazyInitializable findDefaultAccountOrNull(Settings settings, - Map> accounts) { + private @Nullable LazyInitializable findDefaultAccountOrNull( + Settings settings, + Map> accounts + ) { final String defaultAccountName = getDefaultAccountName(settings); if (defaultAccountName == null) { if (accounts.isEmpty()) { @@ -177,7 +191,7 @@ private Map> createAccount * @return A copy of the {@code SecureSettings} of the passed in {@code Settings} argument. */ private static SecureSettings extractSecureSettings(Settings source, List> securePluginSettings) - throws GeneralSecurityException { + throws GeneralSecurityException { // get the secure settings out final SecureSettings sourceSecureSettings = Settings.builder().put(source, true).getSecureSettings(); // filter and cache them... @@ -186,8 +200,10 @@ private static SecureSettings extractSecureSettings(Settings source, List secureSetting : securePluginSettings) { if (secureSetting.match(settingKey)) { - cache.put(settingKey, - new Tuple<>(sourceSecureSettings.getString(settingKey), sourceSecureSettings.getSHA256Digest(settingKey))); + cache.put( + settingKey, + new Tuple<>(sourceSecureSettings.getString(settingKey), sourceSecureSettings.getSHA256Digest(settingKey)) + ); } } } @@ -220,8 +236,7 @@ public byte[] getSHA256Digest(String setting) { } @Override - public void close() throws IOException { - } + public void close() throws IOException {} }; } } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/Account.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/Account.java index fdfbda6ca7b00..eff93d3847b7c 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/Account.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/Account.java @@ -8,15 +8,24 @@ import org.apache.logging.log4j.Logger; import org.elasticsearch.SpecialPermission; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.settings.SecureSetting; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsException; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xpack.core.watcher.crypto.CryptoService; +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.security.PrivilegedActionException; +import java.security.PrivilegedExceptionAction; +import java.util.Objects; +import java.util.Properties; +import java.util.Set; +import java.util.stream.Collectors; + import javax.activation.CommandMap; import javax.activation.MailcapCommandMap; import javax.mail.MessagingException; @@ -26,14 +35,6 @@ import javax.mail.internet.MimeMessage; import javax.net.SocketFactory; import javax.net.ssl.SSLSocketFactory; -import java.security.AccessController; -import java.security.PrivilegedAction; -import java.security.PrivilegedActionException; -import java.security.PrivilegedExceptionAction; -import java.util.Objects; -import java.util.Properties; -import java.util.Set; -import java.util.stream.Collectors; import static org.elasticsearch.xpack.core.watcher.WatcherField.EMAIL_NOTIFICATION_SSL_PREFIX; @@ -64,10 +65,10 @@ public class Account { public static void init() {} static final Settings DEFAULT_SMTP_TIMEOUT_SETTINGS = Settings.builder() - .put("connection_timeout", TimeValue.timeValueMinutes(2)) - .put("write_timeout", TimeValue.timeValueMinutes(2)) - .put("timeout", TimeValue.timeValueMinutes(2)) - .build(); + .put("connection_timeout", TimeValue.timeValueMinutes(2)) + .put("write_timeout", TimeValue.timeValueMinutes(2)) + .put("timeout", TimeValue.timeValueMinutes(2)) + .build(); private final Config config; private final CryptoService cryptoService; @@ -138,8 +139,9 @@ public Email send(Email email, Authentication auth, Profile profile) throws Mess // unprivileged code such as scripts do not have SpecialPermission sm.checkPermission(new SpecialPermission()); } - contextClassLoader = AccessController.doPrivileged((PrivilegedAction) () -> - Thread.currentThread().getContextClassLoader()); + contextClassLoader = AccessController.doPrivileged( + (PrivilegedAction) () -> Thread.currentThread().getContextClassLoader() + ); // if we cannot get the context class loader, changing does not make sense, as we run into the danger of not being able to // change it back if (contextClassLoader != null) { @@ -202,14 +204,19 @@ static class Config { throw new SettingsException(msg); } if (sslSocketFactory != null) { - String sslKeys = smtp.properties.keySet().stream() + String sslKeys = smtp.properties.keySet() + .stream() .map(String::valueOf) .filter(key -> key.startsWith("mail.smtp.ssl.")) .collect(Collectors.joining(",")); if (sslKeys.isEmpty() == false) { - logger.warn("The SMTP SSL settings [{}] that are configured for Account [{}]" + - " will be ignored due to the notification SSL settings in [{}]", - sslKeys, name, EMAIL_NOTIFICATION_SSL_PREFIX); + logger.warn( + "The SMTP SSL settings [{}] that are configured for Account [{}]" + + " will be ignored due to the notification SSL settings in [{}]", + sslKeys, + name, + EMAIL_NOTIFICATION_SSL_PREFIX + ); } smtp.setSocketFactory(sslSocketFactory); } @@ -233,7 +240,7 @@ static class Smtp { port = settings.getAsInt("port", settings.getAsInt("localport", settings.getAsInt("local_port", 25))); user = settings.get("user", settings.get("from", null)); password = getSecureSetting(settings, SECURE_PASSWORD_SETTING); - //password = passStr != null ? passStr.toCharArray() : null; + // password = passStr != null ? passStr.toCharArray() : null; properties = loadSmtpProperties(settings); } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/Attachment.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/Attachment.java index 6c5f421edace0..bfee2512c3a01 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/Attachment.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/Attachment.java @@ -7,20 +7,14 @@ package org.elasticsearch.xpack.watcher.notification.email; import org.elasticsearch.ElasticsearchException; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.inject.Provider; +import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.watcher.notification.email.support.BodyPartSource; -import javax.activation.DataHandler; -import javax.activation.DataSource; -import javax.activation.FileDataSource; -import javax.mail.MessagingException; -import javax.mail.internet.MimeBodyPart; -import javax.mail.util.ByteArrayDataSource; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -28,6 +22,13 @@ import java.util.Collections; import java.util.Set; +import javax.activation.DataHandler; +import javax.activation.DataSource; +import javax.activation.FileDataSource; +import javax.mail.MessagingException; +import javax.mail.internet.MimeBodyPart; +import javax.mail.util.ByteArrayDataSource; + import static javax.mail.Part.ATTACHMENT; import static javax.mail.Part.INLINE; @@ -73,11 +74,11 @@ public Set getWarnings() { @Override public final XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { return builder.startObject() - .field("type", type()) - .field("id", id) - .field("name", name) - .field("content_type", contentType) - .endObject(); + .field("type", type()) + .field("id", id) + .field("name", name) + .field("content_type", contentType) + .endObject(); } protected abstract void writeTo(MimeBodyPart part) throws MessagingException; @@ -232,10 +233,14 @@ protected XContent(String id, String name, ToXContent content, XContentType type static String mimeType(XContentType type) { switch (type) { - case JSON: return "application/json"; - case YAML: return "application/yaml"; - case SMILE: return "application/smile"; - case CBOR: return "application/cbor"; + case JSON: + return "application/json"; + case YAML: + return "application/yaml"; + case SMILE: + return "application/smile"; + case CBOR: + return "application/cbor"; default: throw new IllegalArgumentException("unsupported xcontent attachment type [" + type.name() + "]"); } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/Authentication.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/Authentication.java index c805279d42bb7..665ec2e3200c9 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/Authentication.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/Authentication.java @@ -33,8 +33,7 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Authentication that = (Authentication) o; - return Objects.equals(user, that.user) && - Objects.equals(password, that.password); + return Objects.equals(user, that.user) && Objects.equals(password, that.password); } @Override diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/DataAttachment.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/DataAttachment.java index 9e709e0888f42..0059547d9f19b 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/DataAttachment.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/DataAttachment.java @@ -64,8 +64,10 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws public static DataAttachment resolve(String format) { switch (format.toLowerCase(Locale.ROOT)) { - case "yaml": return YAML; - case "json": return JSON; + case "yaml": + return YAML; + case "json": + return JSON; default: throw illegalArgument("unknown data attachment format [{}]", format); } @@ -80,8 +82,10 @@ public static DataAttachment parse(XContentParser parser) throws IOException { return parser.booleanValue() ? DEFAULT : null; } if (token != XContentParser.Token.START_OBJECT) { - throw new ElasticsearchParseException("could not parse data attachment. expected either a boolean value or an object but " + - "found [{}] instead", token); + throw new ElasticsearchParseException( + "could not parse data attachment. expected either a boolean value or an object but " + "found [{}] instead", + token + ); } DataAttachment dataAttachment = DEFAULT; @@ -91,14 +95,20 @@ public static DataAttachment parse(XContentParser parser) throws IOException { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (currentFieldName == null) { - throw new ElasticsearchParseException("could not parse data attachment. expected [{}] field but found [{}] instead", - Field.FORMAT.getPreferredName(), token); + throw new ElasticsearchParseException( + "could not parse data attachment. expected [{}] field but found [{}] instead", + Field.FORMAT.getPreferredName(), + token + ); } else if (Field.FORMAT.match(currentFieldName, parser.getDeprecationHandler())) { if (token == XContentParser.Token.VALUE_STRING) { dataAttachment = resolve(parser.text()); } else { - throw new ElasticsearchParseException("could not parse data attachment. expected string value for [{}] field but " + - "found [{}] instead", currentFieldName, token); + throw new ElasticsearchParseException( + "could not parse data attachment. expected string value for [{}] field but " + "found [{}] instead", + currentFieldName, + token + ); } } else { throw new ElasticsearchParseException("could not parse data attachment. unexpected field [{}]", currentFieldName); diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/Email.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/Email.java index 7b84e2c06052f..a6287e5753800 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/Email.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/Email.java @@ -7,19 +7,15 @@ package org.elasticsearch.xpack.watcher.notification.email; import org.elasticsearch.ElasticsearchParseException; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.time.DateFormatter; import org.elasticsearch.common.time.DateFormatters; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentFragment; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import javax.mail.MessagingException; -import javax.mail.internet.AddressException; -import javax.mail.internet.InternetAddress; -import javax.mail.internet.MimeMessage; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets; @@ -33,6 +29,11 @@ import java.util.Locale; import java.util.Map; +import javax.mail.MessagingException; +import javax.mail.internet.AddressException; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeMessage; + import static java.util.Collections.unmodifiableMap; public class Email implements ToXContentObject { @@ -50,9 +51,20 @@ public class Email implements ToXContentObject { final String htmlBody; final Map attachments; - public Email(String id, Address from, AddressList replyTo, Priority priority, ZonedDateTime sentDate, - AddressList to, AddressList cc, AddressList bcc, String subject, String textBody, String htmlBody, - Map attachments) { + public Email( + String id, + Address from, + AddressList replyTo, + Priority priority, + ZonedDateTime sentDate, + AddressList to, + AddressList cc, + AddressList bcc, + String subject, + String textBody, + String htmlBody, + Map attachments + ) { this.id = id; this.from = from; @@ -174,57 +186,60 @@ public static Builder builder() { return new Builder(); } - public static Email parse(XContentParser parser) throws IOException{ + public static Email parse(XContentParser parser) throws IOException { Builder email = new Builder(); String currentFieldName = null; XContentParser.Token token; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); - } else if ((token.isValue() || token == XContentParser.Token.START_OBJECT || token == XContentParser.Token.START_ARRAY) && - currentFieldName != null) { - if (Field.ID.match(currentFieldName, parser.getDeprecationHandler())) { - email.id(parser.text()); - } else if (Field.FROM.match(currentFieldName, parser.getDeprecationHandler())) { - email.from(Address.parse(currentFieldName, token, parser)); - } else if (Field.REPLY_TO.match(currentFieldName, parser.getDeprecationHandler())) { - email.replyTo(AddressList.parse(currentFieldName, token, parser)); - } else if (Field.TO.match(currentFieldName, parser.getDeprecationHandler())) { - email.to(AddressList.parse(currentFieldName, token, parser)); - } else if (Field.CC.match(currentFieldName, parser.getDeprecationHandler())) { - email.cc(AddressList.parse(currentFieldName, token, parser)); - } else if (Field.BCC.match(currentFieldName, parser.getDeprecationHandler())) { - email.bcc(AddressList.parse(currentFieldName, token, parser)); - } else if (Field.PRIORITY.match(currentFieldName, parser.getDeprecationHandler())) { - email.priority(Email.Priority.resolve(parser.text())); - } else if (Field.SENT_DATE.match(currentFieldName, parser.getDeprecationHandler())) { - email.sentDate(DateFormatters.from(DATE_TIME_FORMATTER.parse(parser.text()))); - } else if (Field.SUBJECT.match(currentFieldName, parser.getDeprecationHandler())) { - email.subject(parser.text()); - } else if (Field.BODY.match(currentFieldName, parser.getDeprecationHandler())) { - String bodyField = currentFieldName; - if (parser.currentToken() == XContentParser.Token.VALUE_STRING) { - email.textBody(parser.text()); - } else if (parser.currentToken() == XContentParser.Token.START_OBJECT) { - while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { - if (token == XContentParser.Token.FIELD_NAME) { - currentFieldName = parser.currentName(); - } else if (currentFieldName == null) { - throw new ElasticsearchParseException("could not parse email. empty [{}] field", bodyField); - } else if (Email.Field.BODY_TEXT.match(currentFieldName, parser.getDeprecationHandler())) { - email.textBody(parser.text()); - } else if (Email.Field.BODY_HTML.match(currentFieldName, parser.getDeprecationHandler())) { - email.htmlBody(parser.text()); - } else { - throw new ElasticsearchParseException("could not parse email. unexpected field [{}.{}] field", bodyField, - currentFieldName); + } else if ((token.isValue() || token == XContentParser.Token.START_OBJECT || token == XContentParser.Token.START_ARRAY) + && currentFieldName != null) { + if (Field.ID.match(currentFieldName, parser.getDeprecationHandler())) { + email.id(parser.text()); + } else if (Field.FROM.match(currentFieldName, parser.getDeprecationHandler())) { + email.from(Address.parse(currentFieldName, token, parser)); + } else if (Field.REPLY_TO.match(currentFieldName, parser.getDeprecationHandler())) { + email.replyTo(AddressList.parse(currentFieldName, token, parser)); + } else if (Field.TO.match(currentFieldName, parser.getDeprecationHandler())) { + email.to(AddressList.parse(currentFieldName, token, parser)); + } else if (Field.CC.match(currentFieldName, parser.getDeprecationHandler())) { + email.cc(AddressList.parse(currentFieldName, token, parser)); + } else if (Field.BCC.match(currentFieldName, parser.getDeprecationHandler())) { + email.bcc(AddressList.parse(currentFieldName, token, parser)); + } else if (Field.PRIORITY.match(currentFieldName, parser.getDeprecationHandler())) { + email.priority(Email.Priority.resolve(parser.text())); + } else if (Field.SENT_DATE.match(currentFieldName, parser.getDeprecationHandler())) { + email.sentDate(DateFormatters.from(DATE_TIME_FORMATTER.parse(parser.text()))); + } else if (Field.SUBJECT.match(currentFieldName, parser.getDeprecationHandler())) { + email.subject(parser.text()); + } else if (Field.BODY.match(currentFieldName, parser.getDeprecationHandler())) { + String bodyField = currentFieldName; + if (parser.currentToken() == XContentParser.Token.VALUE_STRING) { + email.textBody(parser.text()); + } else if (parser.currentToken() == XContentParser.Token.START_OBJECT) { + while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { + if (token == XContentParser.Token.FIELD_NAME) { + currentFieldName = parser.currentName(); + } else if (currentFieldName == null) { + throw new ElasticsearchParseException("could not parse email. empty [{}] field", bodyField); + } else if (Email.Field.BODY_TEXT.match(currentFieldName, parser.getDeprecationHandler())) { + email.textBody(parser.text()); + } else if (Email.Field.BODY_HTML.match(currentFieldName, parser.getDeprecationHandler())) { + email.htmlBody(parser.text()); + } else { + throw new ElasticsearchParseException( + "could not parse email. unexpected field [{}.{}] field", + bodyField, + currentFieldName + ); + } } } + } else { + throw new ElasticsearchParseException("could not parse email. unexpected field [{}]", currentFieldName); } - } else { - throw new ElasticsearchParseException("could not parse email. unexpected field [{}]", currentFieldName); } - } } return email.build(); } @@ -244,8 +259,7 @@ public static class Builder { private String htmlBody; private Map attachments = new HashMap<>(); - private Builder() { - } + private Builder() {} public Builder copyFrom(Email email) { id = email.id; @@ -356,8 +370,20 @@ public Builder attach(Attachment attachment) { */ public Email build() { assert id != null : "email id should not be null"; - Email email = new Email(id, from, replyTo, priority, sentDate, to, cc, bcc, subject, textBody, htmlBody, - unmodifiableMap(attachments)); + Email email = new Email( + id, + from, + replyTo, + priority, + sentDate, + to, + cc, + bcc, + subject, + textBody, + htmlBody, + unmodifiableMap(attachments) + ); attachments = null; return email; } @@ -401,11 +427,16 @@ public static Priority resolve(String name, Priority defaultPriority) { return defaultPriority; } switch (name.toLowerCase(Locale.ROOT)) { - case "highest": return HIGHEST; - case "high": return HIGH; - case "normal": return NORMAL; - case "low": return LOW; - case "lowest": return LOWEST; + case "highest": + return HIGHEST; + case "high": + return HIGH; + case "normal": + return NORMAL; + case "low": + return LOW; + case "lowest": + return LOWEST; default: return defaultPriority; } @@ -457,8 +488,9 @@ public static Address parse(String field, XContentParser.Token token, XContentPa } else if (ADDRESS_NAME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) { name = parser.text(); } else { - throw new ElasticsearchParseException("could not parse [" + field + "] object as address. unknown address " + - "field [" + currentFieldName + "]"); + throw new ElasticsearchParseException( + "could not parse [" + field + "] object as address. unknown address " + "field [" + currentFieldName + "]" + ); } } } @@ -473,8 +505,11 @@ public static Address parse(String field, XContentParser.Token token, XContentPa } } - throw new ElasticsearchParseException("could not parse [{}] as address. address must either be a string (RFC822 encoded) or " + - "an object specifying the address [name] and [email]", field); + throw new ElasticsearchParseException( + "could not parse [{}] as address. address must either be a string (RFC822 encoded) or " + + "an object specifying the address [name] and [email]", + field + ); } public static Address parse(Settings settings, String name) { @@ -559,8 +594,15 @@ public static Email.AddressList parse(String field, XContentParser.Token token, try { return parse(parser.text()); } catch (AddressException ae) { - throw new ElasticsearchParseException("could not parse field [" + field + "] with value [" + text + "] as address " + - "list. address(es) must be RFC822 encoded", ae); + throw new ElasticsearchParseException( + "could not parse field [" + + field + + "] with value [" + + text + + "] as address " + + "list. address(es) must be RFC822 encoded", + ae + ); } } if (token == XContentParser.Token.START_ARRAY) { @@ -570,8 +612,12 @@ public static Email.AddressList parse(String field, XContentParser.Token token, } return new Email.AddressList(addresses); } - throw new ElasticsearchParseException("could not parse [" + field + "] as address list. field must either be a string " + - "(comma-separated list of RFC822 encoded addresses) or an array of objects representing addresses"); + throw new ElasticsearchParseException( + "could not parse [" + + field + + "] as address list. field must either be a string " + + "(comma-separated list of RFC822 encoded addresses) or an array of objects representing addresses" + ); } @Override diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/EmailService.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/EmailService.java index 6c8c0d09dc3c5..cb30b89458495 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/EmailService.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/EmailService.java @@ -8,7 +8,6 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.settings.ClusterSettings; import org.elasticsearch.common.settings.SecureSetting; import org.elasticsearch.common.settings.SecureString; @@ -16,18 +15,20 @@ import org.elasticsearch.common.settings.Setting.Property; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.ssl.SslConfiguration; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xpack.core.ssl.SSLConfigurationSettings; import org.elasticsearch.xpack.core.ssl.SSLService; import org.elasticsearch.xpack.core.watcher.crypto.CryptoService; import org.elasticsearch.xpack.watcher.notification.NotificationService; -import javax.mail.MessagingException; -import javax.net.ssl.SSLSocketFactory; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import javax.mail.MessagingException; +import javax.net.ssl.SSLSocketFactory; + import static org.elasticsearch.xpack.core.watcher.WatcherField.EMAIL_NOTIFICATION_SSL_PREFIX; /** @@ -35,77 +36,114 @@ */ public class EmailService extends NotificationService { - private static final Setting SETTING_DEFAULT_ACCOUNT = - Setting.simpleString("xpack.notification.email.default_account", Property.Dynamic, Property.NodeScope); + private static final Setting SETTING_DEFAULT_ACCOUNT = Setting.simpleString( + "xpack.notification.email.default_account", + Property.Dynamic, + Property.NodeScope + ); - private static final Setting.AffixSetting SETTING_PROFILE = - Setting.affixKeySetting("xpack.notification.email.account.", "profile", - (key) -> Setting.simpleString(key, Property.Dynamic, Property.NodeScope)); + private static final Setting.AffixSetting SETTING_PROFILE = Setting.affixKeySetting( + "xpack.notification.email.account.", + "profile", + (key) -> Setting.simpleString(key, Property.Dynamic, Property.NodeScope) + ); - private static final Setting.AffixSetting SETTING_EMAIL_DEFAULTS = - Setting.affixKeySetting("xpack.notification.email.account.", "email_defaults", - (key) -> Setting.groupSetting(key + ".", Property.Dynamic, Property.NodeScope)); + private static final Setting.AffixSetting SETTING_EMAIL_DEFAULTS = Setting.affixKeySetting( + "xpack.notification.email.account.", + "email_defaults", + (key) -> Setting.groupSetting(key + ".", Property.Dynamic, Property.NodeScope) + ); // settings that can be configured as smtp properties - private static final Setting.AffixSetting SETTING_SMTP_AUTH = - Setting.affixKeySetting("xpack.notification.email.account.", "smtp.auth", - (key) -> Setting.boolSetting(key, false, Property.Dynamic, Property.NodeScope)); - - private static final Setting.AffixSetting SETTING_SMTP_STARTTLS_ENABLE = - Setting.affixKeySetting("xpack.notification.email.account.", "smtp.starttls.enable", - (key) -> Setting.boolSetting(key, false, Property.Dynamic, Property.NodeScope)); - - private static final Setting.AffixSetting SETTING_SMTP_STARTTLS_REQUIRED = - Setting.affixKeySetting("xpack.notification.email.account.", "smtp.starttls.required", - (key) -> Setting.boolSetting(key, false, Property.Dynamic, Property.NodeScope)); - - private static final Setting.AffixSetting SETTING_SMTP_HOST = - Setting.affixKeySetting("xpack.notification.email.account.", "smtp.host", - (key) -> Setting.simpleString(key, Property.Dynamic, Property.NodeScope)); - - private static final Setting.AffixSetting SETTING_SMTP_PORT = - Setting.affixKeySetting("xpack.notification.email.account.", "smtp.port", - (key) -> Setting.intSetting(key, 587, Property.Dynamic, Property.NodeScope)); - - private static final Setting.AffixSetting SETTING_SMTP_USER = - Setting.affixKeySetting("xpack.notification.email.account.", "smtp.user", - (key) -> Setting.simpleString(key, Property.Dynamic, Property.NodeScope)); - - private static final Setting.AffixSetting SETTING_SECURE_PASSWORD = - Setting.affixKeySetting("xpack.notification.email.account.", "smtp.secure_password", - (key) -> SecureSetting.secureString(key, null)); - - private static final Setting.AffixSetting SETTING_SMTP_TIMEOUT = - Setting.affixKeySetting("xpack.notification.email.account.", "smtp.timeout", - (key) -> Setting.timeSetting(key, TimeValue.timeValueMinutes(2), Property.Dynamic, Property.NodeScope)); - - private static final Setting.AffixSetting SETTING_SMTP_CONNECTION_TIMEOUT = - Setting.affixKeySetting("xpack.notification.email.account.", "smtp.connection_timeout", - (key) -> Setting.timeSetting(key, TimeValue.timeValueMinutes(2), Property.Dynamic, Property.NodeScope)); - - private static final Setting.AffixSetting SETTING_SMTP_WRITE_TIMEOUT = - Setting.affixKeySetting("xpack.notification.email.account.", "smtp.write_timeout", - (key) -> Setting.timeSetting(key, TimeValue.timeValueMinutes(2), Property.Dynamic, Property.NodeScope)); - - private static final Setting.AffixSetting SETTING_SMTP_LOCAL_ADDRESS = - Setting.affixKeySetting("xpack.notification.email.account.", "smtp.local_address", - (key) -> Setting.simpleString(key, Property.Dynamic, Property.NodeScope)); - - private static final Setting.AffixSetting SETTING_SMTP_SSL_TRUST_ADDRESS = - Setting.affixKeySetting("xpack.notification.email.account.", "smtp.ssl.trust", - (key) -> Setting.simpleString(key, Property.Dynamic, Property.NodeScope)); - - private static final Setting.AffixSetting SETTING_SMTP_LOCAL_PORT = - Setting.affixKeySetting("xpack.notification.email.account.", "smtp.local_port", - (key) -> Setting.intSetting(key, 25, Property.Dynamic, Property.NodeScope)); - - private static final Setting.AffixSetting SETTING_SMTP_SEND_PARTIAL = - Setting.affixKeySetting("xpack.notification.email.account.", "smtp.send_partial", - (key) -> Setting.boolSetting(key, false, Property.Dynamic, Property.NodeScope)); - - private static final Setting.AffixSetting SETTING_SMTP_WAIT_ON_QUIT = - Setting.affixKeySetting("xpack.notification.email.account.", "smtp.wait_on_quit", - (key) -> Setting.boolSetting(key, true, Property.Dynamic, Property.NodeScope)); + private static final Setting.AffixSetting SETTING_SMTP_AUTH = Setting.affixKeySetting( + "xpack.notification.email.account.", + "smtp.auth", + (key) -> Setting.boolSetting(key, false, Property.Dynamic, Property.NodeScope) + ); + + private static final Setting.AffixSetting SETTING_SMTP_STARTTLS_ENABLE = Setting.affixKeySetting( + "xpack.notification.email.account.", + "smtp.starttls.enable", + (key) -> Setting.boolSetting(key, false, Property.Dynamic, Property.NodeScope) + ); + + private static final Setting.AffixSetting SETTING_SMTP_STARTTLS_REQUIRED = Setting.affixKeySetting( + "xpack.notification.email.account.", + "smtp.starttls.required", + (key) -> Setting.boolSetting(key, false, Property.Dynamic, Property.NodeScope) + ); + + private static final Setting.AffixSetting SETTING_SMTP_HOST = Setting.affixKeySetting( + "xpack.notification.email.account.", + "smtp.host", + (key) -> Setting.simpleString(key, Property.Dynamic, Property.NodeScope) + ); + + private static final Setting.AffixSetting SETTING_SMTP_PORT = Setting.affixKeySetting( + "xpack.notification.email.account.", + "smtp.port", + (key) -> Setting.intSetting(key, 587, Property.Dynamic, Property.NodeScope) + ); + + private static final Setting.AffixSetting SETTING_SMTP_USER = Setting.affixKeySetting( + "xpack.notification.email.account.", + "smtp.user", + (key) -> Setting.simpleString(key, Property.Dynamic, Property.NodeScope) + ); + + private static final Setting.AffixSetting SETTING_SECURE_PASSWORD = Setting.affixKeySetting( + "xpack.notification.email.account.", + "smtp.secure_password", + (key) -> SecureSetting.secureString(key, null) + ); + + private static final Setting.AffixSetting SETTING_SMTP_TIMEOUT = Setting.affixKeySetting( + "xpack.notification.email.account.", + "smtp.timeout", + (key) -> Setting.timeSetting(key, TimeValue.timeValueMinutes(2), Property.Dynamic, Property.NodeScope) + ); + + private static final Setting.AffixSetting SETTING_SMTP_CONNECTION_TIMEOUT = Setting.affixKeySetting( + "xpack.notification.email.account.", + "smtp.connection_timeout", + (key) -> Setting.timeSetting(key, TimeValue.timeValueMinutes(2), Property.Dynamic, Property.NodeScope) + ); + + private static final Setting.AffixSetting SETTING_SMTP_WRITE_TIMEOUT = Setting.affixKeySetting( + "xpack.notification.email.account.", + "smtp.write_timeout", + (key) -> Setting.timeSetting(key, TimeValue.timeValueMinutes(2), Property.Dynamic, Property.NodeScope) + ); + + private static final Setting.AffixSetting SETTING_SMTP_LOCAL_ADDRESS = Setting.affixKeySetting( + "xpack.notification.email.account.", + "smtp.local_address", + (key) -> Setting.simpleString(key, Property.Dynamic, Property.NodeScope) + ); + + private static final Setting.AffixSetting SETTING_SMTP_SSL_TRUST_ADDRESS = Setting.affixKeySetting( + "xpack.notification.email.account.", + "smtp.ssl.trust", + (key) -> Setting.simpleString(key, Property.Dynamic, Property.NodeScope) + ); + + private static final Setting.AffixSetting SETTING_SMTP_LOCAL_PORT = Setting.affixKeySetting( + "xpack.notification.email.account.", + "smtp.local_port", + (key) -> Setting.intSetting(key, 25, Property.Dynamic, Property.NodeScope) + ); + + private static final Setting.AffixSetting SETTING_SMTP_SEND_PARTIAL = Setting.affixKeySetting( + "xpack.notification.email.account.", + "smtp.send_partial", + (key) -> Setting.boolSetting(key, false, Property.Dynamic, Property.NodeScope) + ); + + private static final Setting.AffixSetting SETTING_SMTP_WAIT_ON_QUIT = Setting.affixKeySetting( + "xpack.notification.email.account.", + "smtp.wait_on_quit", + (key) -> Setting.boolSetting(key, true, Property.Dynamic, Property.NodeScope) + ); private static final SSLConfigurationSettings SSL_SETTINGS = SSLConfigurationSettings.withPrefix(EMAIL_NOTIFICATION_SSL_PREFIX, true); @@ -158,8 +196,9 @@ private SSLSocketFactory getSmtpSslSocketFactory() { public EmailSent send(Email email, Authentication auth, Profile profile, String accountName) throws MessagingException { Account account = getAccount(accountName); if (account == null) { - throw new IllegalArgumentException("failed to send email with subject [" + email.subject() + "] via account [" + accountName - + "]. account does not exist"); + throw new IllegalArgumentException( + "failed to send email with subject [" + email.subject() + "] via account [" + accountName + "]. account does not exist" + ); } return send(email, auth, profile, account); } @@ -169,8 +208,10 @@ private EmailSent send(Email email, Authentication auth, Profile profile, Accoun try { email = account.send(email, auth, profile); } catch (MessagingException me) { - throw new MessagingException("failed to send email with subject [" + email.subject() + "] via account [" + account.name() + - "]", me); + throw new MessagingException( + "failed to send email with subject [" + email.subject() + "] via account [" + account.name() + "]", + me + ); } return new EmailSent(account.name(), email); } @@ -195,10 +236,25 @@ public Email email() { } private static List> getDynamicSettings() { - return Arrays.asList(SETTING_DEFAULT_ACCOUNT, SETTING_PROFILE, SETTING_EMAIL_DEFAULTS, SETTING_SMTP_AUTH, SETTING_SMTP_HOST, - SETTING_SMTP_PORT, SETTING_SMTP_STARTTLS_ENABLE, SETTING_SMTP_USER, SETTING_SMTP_STARTTLS_REQUIRED, - SETTING_SMTP_TIMEOUT, SETTING_SMTP_CONNECTION_TIMEOUT, SETTING_SMTP_WRITE_TIMEOUT, SETTING_SMTP_LOCAL_ADDRESS, - SETTING_SMTP_LOCAL_PORT, SETTING_SMTP_SEND_PARTIAL, SETTING_SMTP_WAIT_ON_QUIT, SETTING_SMTP_SSL_TRUST_ADDRESS); + return Arrays.asList( + SETTING_DEFAULT_ACCOUNT, + SETTING_PROFILE, + SETTING_EMAIL_DEFAULTS, + SETTING_SMTP_AUTH, + SETTING_SMTP_HOST, + SETTING_SMTP_PORT, + SETTING_SMTP_STARTTLS_ENABLE, + SETTING_SMTP_USER, + SETTING_SMTP_STARTTLS_REQUIRED, + SETTING_SMTP_TIMEOUT, + SETTING_SMTP_CONNECTION_TIMEOUT, + SETTING_SMTP_WRITE_TIMEOUT, + SETTING_SMTP_LOCAL_ADDRESS, + SETTING_SMTP_LOCAL_PORT, + SETTING_SMTP_SEND_PARTIAL, + SETTING_SMTP_WAIT_ON_QUIT, + SETTING_SMTP_SSL_TRUST_ADDRESS + ); } private static List> getSecureSettings() { diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/EmailTemplate.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/EmailTemplate.java index addae863a6fcd..183f1fa0041d8 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/EmailTemplate.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/EmailTemplate.java @@ -14,7 +14,6 @@ import org.elasticsearch.xpack.watcher.common.text.TextTemplate; import org.elasticsearch.xpack.watcher.common.text.TextTemplateEngine; -import javax.mail.internet.AddressException; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; @@ -24,6 +23,8 @@ import java.util.Objects; import java.util.Set; +import javax.mail.internet.AddressException; + public class EmailTemplate implements ToXContentObject { final TextTemplate from; @@ -36,9 +37,17 @@ public class EmailTemplate implements ToXContentObject { final TextTemplate textBody; final TextTemplate htmlBody; - public EmailTemplate(TextTemplate from, TextTemplate[] replyTo, TextTemplate priority, TextTemplate[] to, - TextTemplate[] cc, TextTemplate[] bcc, TextTemplate subject, TextTemplate textBody, - TextTemplate htmlBody) { + public EmailTemplate( + TextTemplate from, + TextTemplate[] replyTo, + TextTemplate priority, + TextTemplate[] to, + TextTemplate[] cc, + TextTemplate[] bcc, + TextTemplate subject, + TextTemplate textBody, + TextTemplate htmlBody + ) { this.from = from; this.replyTo = replyTo; this.priority = priority; @@ -86,8 +95,12 @@ public TextTemplate htmlBody() { return htmlBody; } - public Email.Builder render(TextTemplateEngine engine, Map model, HtmlSanitizer htmlSanitizer, - Map attachments) throws AddressException { + public Email.Builder render( + TextTemplateEngine engine, + Map model, + HtmlSanitizer htmlSanitizer, + Map attachments + ) throws AddressException { Email.Builder builder = Email.builder(); if (from != null) { builder.from(engine.render(from, model)); @@ -125,12 +138,11 @@ public Email.Builder render(TextTemplateEngine engine, Map model String htmlWarnings = ""; String textWarnings = ""; - if(warnings.isEmpty() == false){ + if (warnings.isEmpty() == false) { StringBuilder textWarningBuilder = new StringBuilder(); StringBuilder htmlWarningBuilder = new StringBuilder(); - warnings.forEach(w -> - { - if(Strings.isNullOrEmpty(w) == false) { + warnings.forEach(w -> { + if (Strings.isNullOrEmpty(w) == false) { textWarningBuilder.append(w).append("\n"); htmlWarningBuilder.append(w).append("
    "); } @@ -150,15 +162,15 @@ public Email.Builder render(TextTemplateEngine engine, Map model builder.htmlBody(renderedHtml); } - if(htmlBody == null && textBody == null && Strings.isNullOrEmpty(textWarnings) == false){ + if (htmlBody == null && textBody == null && Strings.isNullOrEmpty(textWarnings) == false) { builder.textBody(textWarnings); } return builder; } - private static Email.AddressList templatesToAddressList(TextTemplateEngine engine, TextTemplate[] templates, - Map model) throws AddressException { + private static Email.AddressList templatesToAddressList(TextTemplateEngine engine, TextTemplate[] templates, Map model) + throws AddressException { List addresses = new ArrayList<>(templates.length); for (TextTemplate template : templates) { Email.AddressList.parse(engine.render(template, model)).forEach(addresses::add); @@ -171,15 +183,15 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; EmailTemplate that = (EmailTemplate) o; - return Objects.equals(from, that.from) && - Arrays.equals(replyTo, that.replyTo) && - Objects.equals(priority, that.priority) && - Arrays.equals(to, that.to) && - Arrays.equals(cc, that.cc) && - Arrays.equals(bcc, that.bcc) && - Objects.equals(subject, that.subject) && - Objects.equals(textBody, that.textBody) && - Objects.equals(htmlBody, that.htmlBody); + return Objects.equals(from, that.from) + && Arrays.equals(replyTo, that.replyTo) + && Objects.equals(priority, that.priority) + && Arrays.equals(to, that.to) + && Arrays.equals(cc, that.cc) + && Arrays.equals(bcc, that.bcc) + && Objects.equals(subject, that.subject) + && Objects.equals(textBody, that.textBody) + && Objects.equals(htmlBody, that.htmlBody); } @Override @@ -261,8 +273,7 @@ public static class Builder { private TextTemplate textBody; private TextTemplate htmlBody; - private Builder() { - } + private Builder() {} public Builder from(String from) { return from(new TextTemplate(from)); @@ -438,8 +449,11 @@ public boolean handle(String fieldName, XContentParser parser) throws IOExceptio } else if (Email.Field.BODY_HTML.match(currentFieldName, parser.getDeprecationHandler())) { builder.htmlBody(TextTemplate.parse(parser)); } else { - throw new ElasticsearchParseException("could not parse email template. unknown field [{}.{}] field", - fieldName, currentFieldName); + throw new ElasticsearchParseException( + "could not parse email template. unknown field [{}.{}] field", + fieldName, + currentFieldName + ); } } } @@ -453,7 +467,7 @@ public boolean handle(String fieldName, XContentParser parser) throws IOExceptio * If this is a text template not using mustache * @param emails The list of email addresses to parse */ - static void validateEmailAddresses(TextTemplate ... emails) { + static void validateEmailAddresses(TextTemplate... emails) { for (TextTemplate emailTemplate : emails) { // no mustache, do validation if (emailTemplate.mayRequireCompilation() == false) { diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/HtmlSanitizer.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/HtmlSanitizer.java index 5a6456b84a7b3..42761dcf3c2b9 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/HtmlSanitizer.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/HtmlSanitizer.java @@ -6,10 +6,10 @@ */ package org.elasticsearch.xpack.watcher.notification.email; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Setting.Property; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.SuppressForbidden; import org.owasp.html.CssSchema; import org.owasp.html.ElementPolicy; import org.owasp.html.HtmlPolicyBuilder; @@ -29,36 +29,63 @@ public class HtmlSanitizer { static final String[] FORMATTING_TAGS = new String[] { - "b", "i", "s", "u", "o", "sup", "sub", "ins", "del", "strong", - "strike", "tt", "code", "big", "small", "br", "span", "em", "hr" - }; - static final String[] BLOCK_TAGS = new String[] { - "p", "div", "h1", "h2", "h3", "h4", "h5", "h6", "ul", "ol", "li", "blockquote" - }; - static final String[] TABLE_TAGS = new String[] { - "table", "th", "tr", "td", "caption", "col", "colgroup", "thead", "tbody", "tfoot" - }; + "b", + "i", + "s", + "u", + "o", + "sup", + "sub", + "ins", + "del", + "strong", + "strike", + "tt", + "code", + "big", + "small", + "br", + "span", + "em", + "hr" }; + static final String[] BLOCK_TAGS = new String[] { "p", "div", "h1", "h2", "h3", "h4", "h5", "h6", "ul", "ol", "li", "blockquote" }; + static final String[] TABLE_TAGS = new String[] { "table", "th", "tr", "td", "caption", "col", "colgroup", "thead", "tbody", "tfoot" }; static final List DEFAULT_ALLOWED = Arrays.asList( - "body", "head", "_tables", "_links", "_blocks", "_formatting", "img:embedded" + "body", + "head", + "_tables", + "_links", + "_blocks", + "_formatting", + "img:embedded" ); - private static Setting SETTING_SANITIZATION_ENABLED = - Setting.boolSetting("xpack.notification.email.html.sanitization.enabled", true, Property.NodeScope); + private static Setting SETTING_SANITIZATION_ENABLED = Setting.boolSetting( + "xpack.notification.email.html.sanitization.enabled", + true, + Property.NodeScope + ); - private static Setting> SETTING_SANITIZATION_ALLOW = - Setting.listSetting("xpack.notification.email.html.sanitization.allow", DEFAULT_ALLOWED, Function.identity(), - Property.NodeScope); + private static Setting> SETTING_SANITIZATION_ALLOW = Setting.listSetting( + "xpack.notification.email.html.sanitization.allow", + DEFAULT_ALLOWED, + Function.identity(), + Property.NodeScope + ); - private static Setting> SETTING_SANITIZATION_DISALLOW = - Setting.listSetting("xpack.notification.email.html.sanitization.disallow", Collections.emptyList(), Function.identity(), - Property.NodeScope); + private static Setting> SETTING_SANITIZATION_DISALLOW = Setting.listSetting( + "xpack.notification.email.html.sanitization.disallow", + Collections.emptyList(), + Function.identity(), + Property.NodeScope + ); private static final MethodHandle sanitizeHandle; static { try { MethodHandles.Lookup methodLookup = MethodHandles.publicLookup(); MethodType sanitizeSignature = MethodType.methodType(String.class, String.class); sanitizeHandle = methodLookup.findVirtual(PolicyFactory.class, "sanitize", sanitizeSignature); - } catch (NoSuchMethodException|IllegalAccessException e) { + } catch (NoSuchMethodException | IllegalAccessException e) { throw new RuntimeException("Missing guava on runtime classpath", e); } } @@ -91,25 +118,29 @@ public String sanitize(String html) { return sanitizer.apply(html); } - @SuppressForbidden( reason = "PolicyFactory uses guava Function") + @SuppressForbidden(reason = "PolicyFactory uses guava Function") static PolicyFactory createCommonPolicy(List allow, List disallow) { HtmlPolicyBuilder policyBuilder = new HtmlPolicyBuilder(); if (allow.stream().anyMatch("_all"::equals)) { - return policyBuilder - .allowElements(TABLE_TAGS) - .allowAttributes("span").onElements("col") - .allowElements(BLOCK_TAGS) - .allowElements(FORMATTING_TAGS) - .allowWithoutAttributes("span") - .allowStyling(CssSchema.DEFAULT) - .allowStandardUrlProtocols().allowElements("a") - .allowAttributes("href").onElements("a").requireRelNofollowOnLinks() - .allowElements("img") - .allowAttributes("src").onElements("img") - .allowStandardUrlProtocols() - .allowUrlProtocols("cid") - .toFactory(); + return policyBuilder.allowElements(TABLE_TAGS) + .allowAttributes("span") + .onElements("col") + .allowElements(BLOCK_TAGS) + .allowElements(FORMATTING_TAGS) + .allowWithoutAttributes("span") + .allowStyling(CssSchema.DEFAULT) + .allowStandardUrlProtocols() + .allowElements("a") + .allowAttributes("href") + .onElements("a") + .requireRelNofollowOnLinks() + .allowElements("img") + .allowAttributes("src") + .onElements("img") + .allowStandardUrlProtocols() + .allowUrlProtocols("cid") + .toFactory(); } EnumSet images = EnumSet.noneOf(Images.class); @@ -125,16 +156,16 @@ static PolicyFactory createCommonPolicy(List allow, List disallo break; case "_links": policyBuilder.allowElements("a") - .allowAttributes("href").onElements("a") - .allowStandardUrlProtocols() - .requireRelNofollowOnLinks(); + .allowAttributes("href") + .onElements("a") + .allowStandardUrlProtocols() + .requireRelNofollowOnLinks(); break; case "_blocks": policyBuilder.allowElements(BLOCK_TAGS); break; case "_formatting": - policyBuilder.allowElements(FORMATTING_TAGS) - .allowWithoutAttributes("span"); + policyBuilder.allowElements(FORMATTING_TAGS).allowWithoutAttributes("span"); break; case "_styles": policyBuilder.allowStyling(CssSchema.DEFAULT); @@ -194,8 +225,6 @@ static PolicyFactory createCommonPolicy(List allow, List disallo return policyBuilder.toFactory(); } - - /** * An {@code img} tag policy that only accept {@code cid:} values in its {@code src} attribute. * If such value is found, the content id is verified against the available attachements of the diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/Profile.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/Profile.java index 3987b3eace71a..5a6445d1624d1 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/Profile.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/Profile.java @@ -38,8 +38,10 @@ public String textBody(MimeMessage msg) throws IOException, MessagingException { } } if (related == null) { - throw new IllegalStateException("could not extract body text from mime message using [standard] profile. could not find " + - "part content type with [multipart/related]"); + throw new IllegalStateException( + "could not extract body text from mime message using [standard] profile. could not find " + + "part content type with [multipart/related]" + ); } MimeMultipart alternative = null; @@ -51,8 +53,10 @@ public String textBody(MimeMessage msg) throws IOException, MessagingException { } } if (alternative == null) { - throw new IllegalStateException("could not extract body text from mime message using [standard] profile. could not find " + - "part content type with [multipart/alternative]"); + throw new IllegalStateException( + "could not extract body text from mime message using [standard] profile. could not find " + + "part content type with [multipart/alternative]" + ); } for (int i = 0; i < alternative.getCount(); i++) { @@ -145,7 +149,7 @@ public MimeMessage toMimeMessage(Email email, Session session) throws MessagingE static final String MESSAGE_ID_HEADER = "Message-ID"; - public abstract MimeMessage toMimeMessage(Email email, Session session) throws MessagingException ; + public abstract MimeMessage toMimeMessage(Email email, Session session) throws MessagingException; public abstract String textBody(MimeMessage msg) throws IOException, MessagingException; @@ -163,10 +167,14 @@ public static Profile resolve(String name, Profile defaultProfile) { } switch (name.toLowerCase(Locale.ROOT)) { case "std": - case "standard": return STANDARD; - case "outlook": return OUTLOOK; - case "gmail": return GMAIL; - case "mac": return MAC; + case "standard": + return STANDARD; + case "outlook": + return OUTLOOK; + case "gmail": + return GMAIL; + case "mac": + return MAC; default: return defaultProfile; } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/attachment/DataAttachment.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/attachment/DataAttachment.java index 36f61071a31ce..79bac46774c6b 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/attachment/DataAttachment.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/attachment/DataAttachment.java @@ -69,7 +69,6 @@ public static Builder builder(String id) { return new Builder(id); } - public static class Builder { private String id; diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/attachment/DataAttachmentParser.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/attachment/DataAttachmentParser.java index 54c3af37a18d5..951bc1ebbdc42 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/attachment/DataAttachmentParser.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/attachment/DataAttachmentParser.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.watcher.notification.email.attachment; import org.elasticsearch.ElasticsearchParseException; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext; import org.elasticsearch.xpack.core.watcher.watch.Payload; @@ -36,7 +36,7 @@ public String type() { @Override public DataAttachment parse(String id, XContentParser parser) throws IOException { org.elasticsearch.xpack.watcher.notification.email.DataAttachment dataAttachment = - org.elasticsearch.xpack.watcher.notification.email.DataAttachment.YAML; + org.elasticsearch.xpack.watcher.notification.email.DataAttachment.YAML; String currentFieldName = null; XContentParser.Token token; @@ -47,8 +47,11 @@ public DataAttachment parse(String id, XContentParser parser) throws IOException if (token == XContentParser.Token.VALUE_STRING) { dataAttachment = resolve(parser.text()); } else { - throw new ElasticsearchParseException("could not parse data attachment. expected string value for [{}] field but " + - "found [{}] instead", currentFieldName, token); + throw new ElasticsearchParseException( + "could not parse data attachment. expected string value for [{}] field but " + "found [{}] instead", + currentFieldName, + token + ); } } } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/attachment/EmailAttachments.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/attachment/EmailAttachments.java index ffaecfe785e06..21c5ea162cd62 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/attachment/EmailAttachments.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/attachment/EmailAttachments.java @@ -18,7 +18,8 @@ public class EmailAttachments implements ToXContentFragment { public static final EmailAttachments EMPTY_ATTACHMENTS = new EmailAttachments( - Collections.emptyList()); + Collections.emptyList() + ); public interface Fields { ParseField ATTACHMENTS = new ParseField("attachments"); diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/attachment/HttpEmailAttachementParser.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/attachment/HttpEmailAttachementParser.java index f9642cc57a1b6..7227dd6a2a2fa 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/attachment/HttpEmailAttachementParser.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/attachment/HttpEmailAttachementParser.java @@ -8,9 +8,9 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchParseException; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext; import org.elasticsearch.xpack.core.watcher.watch.Payload; @@ -78,8 +78,7 @@ public HttpRequestAttachment parse(String id, XContentParser parser) throws IOEx } @Override - public Attachment toAttachment(WatchExecutionContext context, Payload payload, - HttpRequestAttachment attachment) throws IOException { + public Attachment toAttachment(WatchExecutionContext context, Payload payload, HttpRequestAttachment attachment) throws IOException { Map model = Variables.createCtxParamsMap(context, payload); HttpRequest httpRequest = attachment.getRequestTemplate().render(templateEngine, model); @@ -89,19 +88,35 @@ public Attachment toAttachment(WatchExecutionContext context, Payload payload, if (response.hasContent()) { String contentType = attachment.getContentType(); String attachmentContentType = Strings.hasLength(contentType) ? contentType : response.contentType(); - return new Attachment.Bytes(attachment.id(), BytesReference.toBytes(response.body()), attachmentContentType, - attachment.inline()); + return new Attachment.Bytes( + attachment.id(), + BytesReference.toBytes(response.body()), + attachmentContentType, + attachment.inline() + ); } else { - throw new ElasticsearchException("Watch[{}] attachment[{}] HTTP empty response body host[{}], port[{}], " + - "method[{}], path[{}], status[{}]", - context.watch().id(), attachment.id(), httpRequest.host(), httpRequest.port(), httpRequest.method(), - httpRequest.path(), response.status()); + throw new ElasticsearchException( + "Watch[{}] attachment[{}] HTTP empty response body host[{}], port[{}], " + "method[{}], path[{}], status[{}]", + context.watch().id(), + attachment.id(), + httpRequest.host(), + httpRequest.port(), + httpRequest.method(), + httpRequest.path(), + response.status() + ); } } else { - throw new ElasticsearchException("Watch[{}] attachment[{}] HTTP error status host[{}], port[{}], " + - "method[{}], path[{}], status[{}]", - context.watch().id(), attachment.id(), httpRequest.host(), httpRequest.port(), httpRequest.method(), - httpRequest.path(), response.status()); + throw new ElasticsearchException( + "Watch[{}] attachment[{}] HTTP error status host[{}], port[{}], " + "method[{}], path[{}], status[{}]", + context.watch().id(), + attachment.id(), + httpRequest.host(), + httpRequest.port(), + httpRequest.method(), + httpRequest.path(), + response.status() + ); } } } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/attachment/HttpRequestAttachment.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/attachment/HttpRequestAttachment.java index f42672ea7d3df..69ca256a630b6 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/attachment/HttpRequestAttachment.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/attachment/HttpRequestAttachment.java @@ -6,8 +6,8 @@ */ package org.elasticsearch.xpack.watcher.notification.email.attachment; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.Strings; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.watcher.common.http.HttpRequestTemplate; @@ -49,8 +49,8 @@ public boolean inline() { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(id) - .startObject(HttpEmailAttachementParser.TYPE) - .field(HttpEmailAttachementParser.Fields.REQUEST.getPreferredName(), requestTemplate, params); + .startObject(HttpEmailAttachementParser.TYPE) + .field(HttpEmailAttachementParser.Fields.REQUEST.getPreferredName(), requestTemplate, params); if (Strings.hasLength(contentType)) { builder.field(HttpEmailAttachementParser.Fields.CONTENT_TYPE.getPreferredName(), contentType); } @@ -75,8 +75,10 @@ public boolean equals(Object o) { if (o == null || getClass() != o.getClass()) return false; HttpRequestAttachment otherDataAttachment = (HttpRequestAttachment) o; - return Objects.equals(id, otherDataAttachment.id) && Objects.equals(requestTemplate, otherDataAttachment.requestTemplate) - && Objects.equals(contentType, otherDataAttachment.contentType) && Objects.equals(inline, otherDataAttachment.inline); + return Objects.equals(id, otherDataAttachment.id) + && Objects.equals(requestTemplate, otherDataAttachment.requestTemplate) + && Objects.equals(contentType, otherDataAttachment.contentType) + && Objects.equals(inline, otherDataAttachment.inline); } @Override diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/attachment/ReportingAttachment.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/attachment/ReportingAttachment.java index 921ef3d82909d..8347abaa1d16e 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/attachment/ReportingAttachment.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/attachment/ReportingAttachment.java @@ -7,11 +7,11 @@ package org.elasticsearch.xpack.watcher.notification.email.attachment; import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xpack.watcher.common.http.HttpProxy; import org.elasticsearch.xpack.watcher.common.http.BasicAuth; +import org.elasticsearch.xpack.watcher.common.http.HttpProxy; import java.io.IOException; import java.util.Objects; @@ -33,8 +33,15 @@ public class ReportingAttachment implements EmailAttachmentParser.EmailAttachmen private final Integer retries; private final HttpProxy proxy; - ReportingAttachment(String id, String url, boolean inline, @Nullable TimeValue interval, @Nullable Integer retries, - @Nullable BasicAuth auth, @Nullable HttpProxy proxy) { + ReportingAttachment( + String id, + String url, + boolean inline, + @Nullable TimeValue interval, + @Nullable Integer retries, + @Nullable BasicAuth auth, + @Nullable HttpProxy proxy + ) { this.id = id; this.url = url; this.retries = retries; @@ -84,8 +91,7 @@ public HttpProxy proxy() { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.startObject(id).startObject(ReportingAttachmentParser.TYPE) - .field(URL.getPreferredName(), url); + builder.startObject(id).startObject(ReportingAttachmentParser.TYPE).field(URL.getPreferredName(), url); if (retries != null) { builder.field(RETRIES.getPreferredName(), retries); @@ -118,10 +124,13 @@ public boolean equals(Object o) { if (o == null || getClass() != o.getClass()) return false; ReportingAttachment otherAttachment = (ReportingAttachment) o; - return Objects.equals(id, otherAttachment.id) && Objects.equals(url, otherAttachment.url) && - Objects.equals(interval, otherAttachment.interval) && Objects.equals(inline, otherAttachment.inline) && - Objects.equals(retries, otherAttachment.retries) && Objects.equals(auth, otherAttachment.auth) && - Objects.equals(proxy, otherAttachment.proxy); + return Objects.equals(id, otherAttachment.id) + && Objects.equals(url, otherAttachment.url) + && Objects.equals(interval, otherAttachment.interval) + && Objects.equals(inline, otherAttachment.inline) + && Objects.equals(retries, otherAttachment.retries) + && Objects.equals(auth, otherAttachment.auth) + && Objects.equals(proxy, otherAttachment.proxy); } @Override diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/attachment/ReportingAttachmentParser.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/attachment/ReportingAttachmentParser.java index 108f9b4b3868a..511ccc0223cbb 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/attachment/ReportingAttachmentParser.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/attachment/ReportingAttachmentParser.java @@ -10,17 +10,17 @@ import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.message.ParameterizedMessage; import org.elasticsearch.ElasticsearchException; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.logging.LoggerMessageFormat; import org.elasticsearch.common.settings.ClusterSettings; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ObjectParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext; @@ -54,21 +54,37 @@ public class ReportingAttachmentParser implements EmailAttachmentParser INTERVAL_SETTING = - Setting.timeSetting("xpack.notification.reporting.interval", TimeValue.timeValueSeconds(15), Setting.Property.NodeScope); - static final Setting RETRIES_SETTING = - Setting.intSetting("xpack.notification.reporting.retries", 40, 0, Setting.Property.NodeScope); + static final Setting INTERVAL_SETTING = Setting.timeSetting( + "xpack.notification.reporting.interval", + TimeValue.timeValueSeconds(15), + Setting.Property.NodeScope + ); + static final Setting RETRIES_SETTING = Setting.intSetting( + "xpack.notification.reporting.retries", + 40, + 0, + Setting.Property.NodeScope + ); - static final Setting REPORT_WARNING_ENABLED_SETTING = - Setting.boolSetting("xpack.notification.reporting.warning.enabled", true, Setting.Property.NodeScope, Setting.Property.Dynamic); + static final Setting REPORT_WARNING_ENABLED_SETTING = Setting.boolSetting( + "xpack.notification.reporting.warning.enabled", + true, + Setting.Property.NodeScope, + Setting.Property.Dynamic + ); - static final Setting.AffixSetting REPORT_WARNING_TEXT = - Setting.affixKeySetting("xpack.notification.reporting.warning.", "text", - key -> Setting.simpleString(key, Setting.Property.NodeScope, Setting.Property.Dynamic)); + static final Setting.AffixSetting REPORT_WARNING_TEXT = Setting.affixKeySetting( + "xpack.notification.reporting.warning.", + "text", + key -> Setting.simpleString(key, Setting.Property.NodeScope, Setting.Property.Dynamic) + ); private static final ObjectParser PARSER = new ObjectParser<>("reporting_attachment"); - private static final ObjectParser PAYLOAD_PARSER = - new ObjectParser<>("reporting_attachment_kibana_payload", true, null); + private static final ObjectParser PAYLOAD_PARSER = new ObjectParser<>( + "reporting_attachment_kibana_payload", + true, + null + ); static final Map WARNINGS = Map.of( "kbn-csv-contains-formulas", @@ -99,6 +115,7 @@ public static List> getSettings() { allSettings.addAll(getStaticSettings()); return allSettings; } + private final Logger logger; private final TimeValue interval; private final int retries; @@ -107,8 +124,12 @@ public static List> getSettings() { private boolean warningEnabled = REPORT_WARNING_ENABLED_SETTING.getDefault(Settings.EMPTY); private final Map customWarnings = new ConcurrentHashMap<>(1); - public ReportingAttachmentParser(Settings settings, HttpClient httpClient, TextTemplateEngine templateEngine, - ClusterSettings clusterSettings) { + public ReportingAttachmentParser( + Settings settings, + HttpClient httpClient, + TextTemplateEngine templateEngine, + ClusterSettings clusterSettings + ) { this.interval = INTERVAL_SETTING.get(settings); this.retries = RETRIES_SETTING.get(settings); this.httpClient = httpClient; @@ -128,9 +149,13 @@ void addWarningText(String name, String value) { void warningValidator(String name, String value) { if (WARNINGS.keySet().contains(name) == false) { - throw new IllegalArgumentException(new ParameterizedMessage( - "Warning [{}] is not supported. Only the following warnings are supported [{}]", - name, String.join(", ", WARNINGS.keySet())).getFormattedMessage()); + throw new IllegalArgumentException( + new ParameterizedMessage( + "Warning [{}] is not supported. Only the following warnings are supported [{}]", + name, + String.join(", ", WARNINGS.keySet()) + ).getFormattedMessage() + ); } } @@ -153,27 +178,27 @@ public Attachment toAttachment(WatchExecutionContext context, Payload payload, R String initialUrl = templateEngine.render(new TextTemplate(attachment.url()), model); HttpRequestTemplate requestTemplate = HttpRequestTemplate.builder(initialUrl) - .connectionTimeout(TimeValue.timeValueSeconds(15)) - .readTimeout(TimeValue.timeValueSeconds(15)) - .method(HttpMethod.POST) - .auth(attachment.auth()) - .proxy(attachment.proxy()) - .putHeader("kbn-xsrf", new TextTemplate("reporting")) - .build(); + .connectionTimeout(TimeValue.timeValueSeconds(15)) + .readTimeout(TimeValue.timeValueSeconds(15)) + .method(HttpMethod.POST) + .auth(attachment.auth()) + .proxy(attachment.proxy()) + .putHeader("kbn-xsrf", new TextTemplate("reporting")) + .build(); HttpRequest request = requestTemplate.render(templateEngine, model); HttpResponse reportGenerationResponse = requestReportGeneration(context.watch().id(), attachment.id(), request); String path = extractIdFromJson(context.watch().id(), attachment.id(), reportGenerationResponse.body()); HttpRequestTemplate pollingRequestTemplate = HttpRequestTemplate.builder(request.host(), request.port()) - .connectionTimeout(TimeValue.timeValueSeconds(10)) - .readTimeout(TimeValue.timeValueSeconds(10)) - .auth(attachment.auth()) - .path(path) - .scheme(request.scheme()) - .proxy(attachment.proxy()) - .putHeader("kbn-xsrf", new TextTemplate("reporting")) - .build(); + .connectionTimeout(TimeValue.timeValueSeconds(10)) + .readTimeout(TimeValue.timeValueSeconds(10)) + .auth(attachment.auth()) + .path(path) + .scheme(request.scheme()) + .proxy(attachment.proxy()) + .putHeader("kbn-xsrf", new TextTemplate("reporting")) + .build(); HttpRequest pollingRequest = pollingRequestTemplate.render(templateEngine, model); int maxRetries = attachment.retries() != null ? attachment.retries() : this.retries; @@ -188,13 +213,26 @@ public Attachment toAttachment(WatchExecutionContext context, Payload payload, R if (response.status() == 503) { // requires us to interval another run, no action to take, except logging - logger.trace("Watch[{}] reporting[{}] pdf is not ready, polling in [{}] again", context.watch().id(), attachment.id(), - TimeValue.timeValueMillis(sleepMillis)); + logger.trace( + "Watch[{}] reporting[{}] pdf is not ready, polling in [{}] again", + context.watch().id(), + attachment.id(), + TimeValue.timeValueMillis(sleepMillis) + ); } else if (response.status() >= 400) { String body = response.body() != null ? response.body().utf8ToString() : null; - throw new ElasticsearchException("Watch[{}] reporting[{}] Error when polling pdf from host[{}], port[{}], " + - "method[{}], path[{}], status[{}], body[{}]", context.watch().id(), attachment.id(), request.host(), - request.port(), request.method(), request.path(), response.status(), body); + throw new ElasticsearchException( + "Watch[{}] reporting[{}] Error when polling pdf from host[{}], port[{}], " + + "method[{}], path[{}], status[{}], body[{}]", + context.watch().id(), + attachment.id(), + request.host(), + request.port(), + request.method(), + request.path(), + response.status(), + body + ); } else if (response.status() == 200) { Set warnings = new HashSet<>(1); if (warningEnabled) { @@ -212,19 +250,38 @@ public Attachment toAttachment(WatchExecutionContext context, Payload payload, R } }); } - return new Attachment.Bytes(attachment.id(), attachment.id(), BytesReference.toBytes(response.body()), - response.contentType(), attachment.inline(), warnings); + return new Attachment.Bytes( + attachment.id(), + attachment.id(), + BytesReference.toBytes(response.body()), + response.contentType(), + attachment.inline(), + warnings + ); } else { String body = response.body() != null ? response.body().utf8ToString() : null; - String message = LoggerMessageFormat.format("", "Watch[{}] reporting[{}] Unexpected status code host[{}], port[{}], " + - "method[{}], path[{}], status[{}], body[{}]", context.watch().id(), attachment.id(), request.host(), - request.port(), request.method(), request.path(), response.status(), body); + String message = LoggerMessageFormat.format( + "", + "Watch[{}] reporting[{}] Unexpected status code host[{}], port[{}], " + "method[{}], path[{}], status[{}], body[{}]", + context.watch().id(), + attachment.id(), + request.host(), + request.port(), + request.method(), + request.path(), + response.status(), + body + ); throw new IllegalStateException(message); } } - throw new ElasticsearchException("Watch[{}] reporting[{}]: Aborting due to maximum number of retries hit [{}]", - context.watch().id(), attachment.id(), maxRetries); + throw new ElasticsearchException( + "Watch[{}] reporting[{}]: Aborting due to maximum number of retries hit [{}]", + context.watch().id(), + attachment.id(), + maxRetries + ); } private void sleep(long sleepMillis, WatchExecutionContext context, ReportingAttachment attachment) { @@ -232,8 +289,11 @@ private void sleep(long sleepMillis, WatchExecutionContext context, ReportingAtt Thread.sleep(sleepMillis); } catch (InterruptedException e) { Thread.currentThread().interrupt(); - throw new ElasticsearchException("Watch[{}] reporting[{}] thread was interrupted, while waiting for polling. Aborting.", - context.watch().id(), attachment.id()); + throw new ElasticsearchException( + "Watch[{}] reporting[{}] thread was interrupted, while waiting for polling. Aborting.", + context.watch().id(), + attachment.id() + ); } } @@ -244,8 +304,13 @@ private long getSleepMillis(WatchExecutionContext context, ReportingAttachment a long sleepMillis; if (attachment.interval() == null) { sleepMillis = interval.millis(); - logger.trace("Watch[{}] reporting[{}] invalid interval configuration [{}], using configured default [{}]", context.watch().id(), - attachment.id(), attachment.interval(), this.interval); + logger.trace( + "Watch[{}] reporting[{}] invalid interval configuration [{}], using configured default [{}]", + context.watch().id(), + attachment.id(), + attachment.interval(), + this.interval + ); } else { sleepMillis = attachment.interval().millis(); } @@ -258,9 +323,17 @@ private long getSleepMillis(WatchExecutionContext context, ReportingAttachment a private HttpResponse requestReportGeneration(String watchId, String attachmentId, HttpRequest request) throws IOException { HttpResponse response = httpClient.execute(request); if (response.status() != 200) { - throw new ElasticsearchException("Watch[{}] reporting[{}] Error response when trying to trigger reporting generation " + - "host[{}], port[{}] method[{}], path[{}], response[{}]", watchId, attachmentId, request.host(), - request.port(), request.method(), request.path(), response); + throw new ElasticsearchException( + "Watch[{}] reporting[{}] Error response when trying to trigger reporting generation " + + "host[{}], port[{}] method[{}], path[{}], response[{}]", + watchId, + attachmentId, + request.host(), + request.port(), + request.method(), + request.path(), + response + ); } return response; @@ -271,15 +344,24 @@ private HttpResponse requestReportGeneration(String watchId, String attachmentId */ private String extractIdFromJson(String watchId, String attachmentId, BytesReference body) throws IOException { // EMPTY is safe here becaus we never call namedObject - try (InputStream stream = body.streamInput(); - XContentParser parser = JsonXContent.jsonXContent - .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) { + try ( + InputStream stream = body.streamInput(); + XContentParser parser = JsonXContent.jsonXContent.createParser( + NamedXContentRegistry.EMPTY, + LoggingDeprecationHandler.INSTANCE, + stream + ) + ) { KibanaReportingPayload payload = new KibanaReportingPayload(); PAYLOAD_PARSER.parse(parser, payload, null); String path = payload.getPath(); if (Strings.isEmpty(path)) { - throw new ElasticsearchException("Watch[{}] reporting[{}] field path found in JSON payload, payload was {}", - watchId, attachmentId, body.utf8ToString()); + throw new ElasticsearchException( + "Watch[{}] reporting[{}] field path found in JSON payload, payload was {}", + watchId, + attachmentId, + body.utf8ToString() + ); } return path; } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/support/BodyPartSource.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/support/BodyPartSource.java index 39875e5b95d50..dc494b5594fdc 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/support/BodyPartSource.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/support/BodyPartSource.java @@ -9,11 +9,12 @@ import org.elasticsearch.SpecialPermission; import org.elasticsearch.xcontent.ToXContentObject; +import java.security.AccessController; +import java.security.PrivilegedAction; + import javax.activation.FileTypeMap; import javax.mail.MessagingException; import javax.mail.internet.MimeBodyPart; -import java.security.AccessController; -import java.security.PrivilegedAction; public abstract class BodyPartSource implements ToXContentObject { @@ -23,8 +24,7 @@ public abstract class BodyPartSource implements ToXContentObject { if (sm != null) { sm.checkPermission(new SpecialPermission()); } - fileTypeMap = AccessController.doPrivileged( - (PrivilegedAction)() -> FileTypeMap.getDefaultFileTypeMap()); + fileTypeMap = AccessController.doPrivileged((PrivilegedAction) () -> FileTypeMap.getDefaultFileTypeMap()); } protected final String id; diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/jira/JiraAccount.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/jira/JiraAccount.java index 99291df845460..2ba4506c9a2a4 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/jira/JiraAccount.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/jira/JiraAccount.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.xpack.watcher.notification.jira; -import org.elasticsearch.core.Booleans; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.SecureSetting; import org.elasticsearch.common.settings.SecureString; @@ -14,18 +13,19 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsException; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.core.Booleans; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.xpack.watcher.common.http.BasicAuth; import org.elasticsearch.xpack.watcher.common.http.HttpClient; import org.elasticsearch.xpack.watcher.common.http.HttpMethod; import org.elasticsearch.xpack.watcher.common.http.HttpProxy; import org.elasticsearch.xpack.watcher.common.http.HttpRequest; import org.elasticsearch.xpack.watcher.common.http.HttpResponse; import org.elasticsearch.xpack.watcher.common.http.Scheme; -import org.elasticsearch.xpack.watcher.common.http.BasicAuth; import java.io.IOException; import java.io.InputStream; @@ -72,7 +72,9 @@ public JiraAccount(String name, Settings settings, HttpClient httpClient) { this.url = uri; } catch (URISyntaxException | IllegalArgumentException e) { throw new SettingsException( - "invalid jira [" + name + "] account settings. invalid [" + SECURE_URL_SETTING.getKey() + "] setting", e); + "invalid jira [" + name + "] account settings. invalid [" + SECURE_URL_SETTING.getKey() + "] setting", + e + ); } this.user = getSetting(name, settings, SECURE_USER_SETTING); this.password = getSetting(name, settings, SECURE_PASSWORD_SETTING); @@ -80,9 +82,11 @@ public JiraAccount(String name, Settings settings, HttpClient httpClient) { builder.startObject(); settings.getAsSettings(ISSUE_DEFAULTS_SETTING).toXContent(builder, ToXContent.EMPTY_PARAMS); builder.endObject(); - try (InputStream stream = BytesReference.bytes(builder).streamInput(); - XContentParser parser = XContentType.JSON.xContent() - .createParser(new NamedXContentRegistry(Collections.emptyList()), LoggingDeprecationHandler.INSTANCE, stream)) { + try ( + InputStream stream = BytesReference.bytes(builder).streamInput(); + XContentParser parser = XContentType.JSON.xContent() + .createParser(new NamedXContentRegistry(Collections.emptyList()), LoggingDeprecationHandler.INSTANCE, stream) + ) { this.issueDefaults = Collections.unmodifiableMap(parser.map()); } } catch (IOException ex) { @@ -108,13 +112,13 @@ public Map getDefaults() { public JiraIssue createIssue(final Map fields, final HttpProxy proxy) throws IOException { HttpRequest request = HttpRequest.builder(url.getHost(), url.getPort()) - .scheme(Scheme.parse(url.getScheme())) - .method(HttpMethod.POST) - .path(url.getPath().isEmpty() || url.getPath().equals("/") ? DEFAULT_PATH : url.getPath()) - .jsonBody((builder, params) -> builder.field("fields", fields)) - .auth(new BasicAuth(user, password.toCharArray())) - .proxy(proxy) - .build(); + .scheme(Scheme.parse(url.getScheme())) + .method(HttpMethod.POST) + .path(url.getPath().isEmpty() || url.getPath().equals("/") ? DEFAULT_PATH : url.getPath()) + .jsonBody((builder, params) -> builder.field("fields", fields)) + .auth(new BasicAuth(user, password.toCharArray())) + .proxy(proxy) + .build(); HttpResponse response = httpClient.execute(request); return JiraIssue.responded(name, fields, request, response); diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/jira/JiraIssue.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/jira/JiraIssue.java index 8c57a0e1ef33c..148e19f895120 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/jira/JiraIssue.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/jira/JiraIssue.java @@ -8,17 +8,17 @@ import org.apache.http.HttpStatus; import org.elasticsearch.ElasticsearchParseException; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; +import org.elasticsearch.xpack.watcher.actions.jira.JiraAction; import org.elasticsearch.xpack.watcher.common.http.HttpRequest; import org.elasticsearch.xpack.watcher.common.http.HttpResponse; -import org.elasticsearch.xpack.watcher.actions.jira.JiraAction; import java.io.IOException; import java.io.InputStream; @@ -29,11 +29,15 @@ public class JiraIssue implements ToXContentObject { - @Nullable final String account; + @Nullable + final String account; private final Map fields; - @Nullable private final HttpRequest request; - @Nullable private final HttpResponse response; - @Nullable private final String failureReason; + @Nullable + private final HttpRequest request; + @Nullable + private final HttpResponse response; + @Nullable + private final String failureReason; public static JiraIssue responded(String account, Map fields, HttpRequest request, HttpResponse response) { return new JiraIssue(account, fields, request, response, resolveFailureReason(response)); @@ -77,11 +81,11 @@ public boolean equals(Object o) { if (o == null || getClass() != o.getClass()) return false; JiraIssue issue = (JiraIssue) o; - return Objects.equals(account, issue.account) && - Objects.equals(fields, issue.fields) && - Objects.equals(request, issue.request) && - Objects.equals(response, issue.response) && - Objects.equals(failureReason, issue.failureReason); + return Objects.equals(account, issue.account) + && Objects.equals(fields, issue.fields) + && Objects.equals(request, issue.request) + && Objects.equals(response, issue.response) + && Objects.equals(failureReason, issue.failureReason); } @Override @@ -152,16 +156,23 @@ static String resolveFailureReason(HttpResponse response) { if (response.hasContent()) { final List errors = new ArrayList<>(); // EMPTY is safe here because we never call namedObject - try (InputStream stream = response.body().streamInput(); - XContentParser parser = JsonXContent.jsonXContent - .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) { + try ( + InputStream stream = response.body().streamInput(); + XContentParser parser = JsonXContent.jsonXContent.createParser( + NamedXContentRegistry.EMPTY, + LoggingDeprecationHandler.INSTANCE, + stream + ) + ) { XContentParser.Token token = parser.currentToken(); if (token == null) { token = parser.nextToken(); } if (token != XContentParser.Token.START_OBJECT) { - throw new ElasticsearchParseException("failed to parse jira project. expected an object, but found [{}] instead", - token); + throw new ElasticsearchParseException( + "failed to parse jira project. expected an object, but found [{}] instead", + token + ); } String currentFieldName = null; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/jira/JiraService.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/jira/JiraService.java index 6a246d9055d79..d0aaeca17c7af 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/jira/JiraService.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/jira/JiraService.java @@ -26,28 +26,41 @@ */ public class JiraService extends NotificationService { - private static final Setting SETTING_DEFAULT_ACCOUNT = - Setting.simpleString("xpack.notification.jira.default_account", Property.Dynamic, Property.NodeScope); + private static final Setting SETTING_DEFAULT_ACCOUNT = Setting.simpleString( + "xpack.notification.jira.default_account", + Property.Dynamic, + Property.NodeScope + ); - private static final Setting.AffixSetting SETTING_ALLOW_HTTP = - Setting.affixKeySetting("xpack.notification.jira.account.", "allow_http", - (key) -> Setting.boolSetting(key, false, Property.Dynamic, Property.NodeScope)); + private static final Setting.AffixSetting SETTING_ALLOW_HTTP = Setting.affixKeySetting( + "xpack.notification.jira.account.", + "allow_http", + (key) -> Setting.boolSetting(key, false, Property.Dynamic, Property.NodeScope) + ); - private static final Setting.AffixSetting SETTING_SECURE_USER = - Setting.affixKeySetting("xpack.notification.jira.account.", "secure_user", - (key) -> SecureSetting.secureString(key, null)); + private static final Setting.AffixSetting SETTING_SECURE_USER = Setting.affixKeySetting( + "xpack.notification.jira.account.", + "secure_user", + (key) -> SecureSetting.secureString(key, null) + ); - private static final Setting.AffixSetting SETTING_SECURE_URL = - Setting.affixKeySetting("xpack.notification.jira.account.", "secure_url", - (key) -> SecureSetting.secureString(key, null)); + private static final Setting.AffixSetting SETTING_SECURE_URL = Setting.affixKeySetting( + "xpack.notification.jira.account.", + "secure_url", + (key) -> SecureSetting.secureString(key, null) + ); - private static final Setting.AffixSetting SETTING_SECURE_PASSWORD = - Setting.affixKeySetting("xpack.notification.jira.account.", "secure_password", - (key) -> SecureSetting.secureString(key, null)); + private static final Setting.AffixSetting SETTING_SECURE_PASSWORD = Setting.affixKeySetting( + "xpack.notification.jira.account.", + "secure_password", + (key) -> SecureSetting.secureString(key, null) + ); - private static final Setting.AffixSetting SETTING_DEFAULTS = - Setting.affixKeySetting("xpack.notification.jira.account.", "issue_defaults", - (key) -> Setting.groupSetting(key + ".", Property.Dynamic, Property.NodeScope)); + private static final Setting.AffixSetting SETTING_DEFAULTS = Setting.affixKeySetting( + "xpack.notification.jira.account.", + "issue_defaults", + (key) -> Setting.groupSetting(key + ".", Property.Dynamic, Property.NodeScope) + ); private final HttpClient httpClient; diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/pagerduty/IncidentEvent.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/pagerduty/IncidentEvent.java index 755e14317f441..be008212a3793 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/pagerduty/IncidentEvent.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/pagerduty/IncidentEvent.java @@ -7,9 +7,9 @@ package org.elasticsearch.xpack.watcher.notification.pagerduty; import org.elasticsearch.ElasticsearchParseException; +import org.elasticsearch.common.Strings; import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.common.Strings; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -43,22 +43,37 @@ public class IncidentEvent implements ToXContentObject { static final String ACCEPT_HEADER = "application/vnd.pagerduty+json;version=2"; final String description; - @Nullable final HttpProxy proxy; - @Nullable final String incidentKey; - @Nullable final String client; - @Nullable final String clientUrl; - @Nullable final String account; + @Nullable + final HttpProxy proxy; + @Nullable + final String incidentKey; + @Nullable + final String client; + @Nullable + final String clientUrl; + @Nullable + final String account; final String eventType; final boolean attachPayload; - @Nullable final IncidentEventContext[] contexts; - - public IncidentEvent(String description, @Nullable String eventType, @Nullable String incidentKey, @Nullable String client, - @Nullable String clientUrl, @Nullable String account, boolean attachPayload, - @Nullable IncidentEventContext[] contexts, @Nullable HttpProxy proxy) { + @Nullable + final IncidentEventContext[] contexts; + + public IncidentEvent( + String description, + @Nullable String eventType, + @Nullable String incidentKey, + @Nullable String client, + @Nullable String clientUrl, + @Nullable String account, + boolean attachPayload, + @Nullable IncidentEventContext[] contexts, + @Nullable HttpProxy proxy + ) { this.description = description; if (description == null) { - throw new IllegalStateException("could not create pagerduty event. missing required [" + - Fields.DESCRIPTION.getPreferredName() + "] setting"); + throw new IllegalStateException( + "could not create pagerduty event. missing required [" + Fields.DESCRIPTION.getPreferredName() + "] setting" + ); } this.incidentKey = incidentKey; this.client = client; @@ -76,15 +91,15 @@ public boolean equals(Object o) { if (o == null || getClass() != o.getClass()) return false; IncidentEvent template = (IncidentEvent) o; - return Objects.equals(description, template.description) && - Objects.equals(incidentKey, template.incidentKey) && - Objects.equals(client, template.client) && - Objects.equals(clientUrl, template.clientUrl) && - Objects.equals(attachPayload, template.attachPayload) && - Objects.equals(eventType, template.eventType) && - Objects.equals(account, template.account) && - Objects.equals(proxy, template.proxy) && - Arrays.equals(contexts, template.contexts); + return Objects.equals(description, template.description) + && Objects.equals(incidentKey, template.incidentKey) + && Objects.equals(client, template.client) + && Objects.equals(clientUrl, template.clientUrl) + && Objects.equals(attachPayload, template.attachPayload) + && Objects.equals(eventType, template.eventType) + && Objects.equals(account, template.account) + && Objects.equals(proxy, template.proxy) + && Arrays.equals(contexts, template.contexts); } @Override @@ -96,17 +111,17 @@ public int hashCode() { HttpRequest createRequest(final String serviceKey, final Payload payload, final String watchId) throws IOException { return HttpRequest.builder(HOST, -1) - .method(HttpMethod.POST) - .scheme(Scheme.HTTPS) - .path(PATH) - .proxy(proxy) - .setHeader("Accept", ACCEPT_HEADER) - .jsonBody((b, p) -> buildAPIXContent(b, p, serviceKey, payload, watchId)) - .build(); + .method(HttpMethod.POST) + .scheme(Scheme.HTTPS) + .path(PATH) + .proxy(proxy) + .setHeader("Accept", ACCEPT_HEADER) + .jsonBody((b, p) -> buildAPIXContent(b, p, serviceKey, payload, watchId)) + .build(); } - XContentBuilder buildAPIXContent(XContentBuilder builder, Params params, String serviceKey, - Payload payload, String watchId) throws IOException { + XContentBuilder buildAPIXContent(XContentBuilder builder, Params params, String serviceKey, Payload payload, String watchId) + throws IOException { builder.field(Fields.ROUTING_KEY.getPreferredName(), serviceKey); builder.field(Fields.EVENT_ACTION.getPreferredName(), eventType); if (incidentKey != null) { @@ -152,8 +167,8 @@ XContentBuilder buildAPIXContent(XContentBuilder builder, Params params, String /** * Turns the V1 API contexts into 2 distinct lists, images and links. The V2 API has separated these out into 2 top level fields. */ - private void toXContentV2Contexts(XContentBuilder builder, ToXContent.Params params, - IncidentEventContext[] contexts) throws IOException { + private void toXContentV2Contexts(XContentBuilder builder, ToXContent.Params params, IncidentEventContext[] contexts) + throws IOException { // contexts can be either links or images, and the v2 api needs them separate Map> groups = Arrays.stream(contexts) .collect(Collectors.groupingBy(iec -> iec.type)); @@ -198,6 +213,7 @@ public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params par } return builder.endObject(); } + public static Template.Builder templateBuilder(String description) { return templateBuilder(new TextTemplate(description)); } @@ -218,9 +234,17 @@ public static class Template implements ToXContentObject { final IncidentEventContext.Template[] contexts; final HttpProxy proxy; - public Template(TextTemplate description, TextTemplate eventType, TextTemplate incidentKey, TextTemplate client, - TextTemplate clientUrl, String account, Boolean attachPayload, IncidentEventContext.Template[] contexts, - HttpProxy proxy) { + public Template( + TextTemplate description, + TextTemplate eventType, + TextTemplate incidentKey, + TextTemplate client, + TextTemplate clientUrl, + String account, + Boolean attachPayload, + IncidentEventContext.Template[] contexts, + HttpProxy proxy + ) { this.description = description; this.eventType = eventType; this.incidentKey = incidentKey; @@ -238,15 +262,15 @@ public boolean equals(Object o) { if (o == null || getClass() != o.getClass()) return false; Template template = (Template) o; - return Objects.equals(description, template.description) && - Objects.equals(incidentKey, template.incidentKey) && - Objects.equals(client, template.client) && - Objects.equals(clientUrl, template.clientUrl) && - Objects.equals(eventType, template.eventType) && - Objects.equals(attachPayload, template.attachPayload) && - Objects.equals(account, template.account) && - Objects.equals(proxy, template.proxy) && - Arrays.equals(contexts, template.contexts); + return Objects.equals(description, template.description) + && Objects.equals(incidentKey, template.incidentKey) + && Objects.equals(client, template.client) + && Objects.equals(clientUrl, template.clientUrl) + && Objects.equals(eventType, template.eventType) + && Objects.equals(attachPayload, template.attachPayload) + && Objects.equals(account, template.account) + && Objects.equals(proxy, template.proxy) + && Arrays.equals(contexts, template.contexts); } @Override @@ -256,11 +280,17 @@ public int hashCode() { return result; } - public IncidentEvent render(String watchId, String actionId, TextTemplateEngine engine, Map model, - IncidentEventDefaults defaults) { + public IncidentEvent render( + String watchId, + String actionId, + TextTemplateEngine engine, + Map model, + IncidentEventDefaults defaults + ) { String description = this.description != null ? engine.render(this.description, model) : defaults.description; - String incidentKey = this.incidentKey != null ? engine.render(this.incidentKey, model) : - defaults.incidentKey != null ? defaults.incidentKey : watchId; + String incidentKey = this.incidentKey != null ? engine.render(this.incidentKey, model) + : defaults.incidentKey != null ? defaults.incidentKey + : watchId; String client = this.client != null ? engine.render(this.client, model) : defaults.client; String clientUrl = this.clientUrl != null ? engine.render(this.clientUrl, model) : defaults.clientUrl; String eventType = this.eventType != null ? engine.render(this.eventType, model) : defaults.eventType; @@ -330,36 +360,46 @@ public static Template parse(String watchId, String actionId, XContentParser par try { incidentKey = TextTemplate.parse(parser); } catch (ElasticsearchParseException e) { - throw new ElasticsearchParseException("could not parse pager duty event template. failed to parse field [{}]", - Fields.INCIDENT_KEY.getPreferredName()); + throw new ElasticsearchParseException( + "could not parse pager duty event template. failed to parse field [{}]", + Fields.INCIDENT_KEY.getPreferredName() + ); } } else if (Fields.DESCRIPTION.match(currentFieldName, parser.getDeprecationHandler())) { try { description = TextTemplate.parse(parser); } catch (ElasticsearchParseException e) { - throw new ElasticsearchParseException("could not parse pager duty event template. failed to parse field [{}]", - Fields.DESCRIPTION.getPreferredName()); + throw new ElasticsearchParseException( + "could not parse pager duty event template. failed to parse field [{}]", + Fields.DESCRIPTION.getPreferredName() + ); } } else if (Fields.CLIENT.match(currentFieldName, parser.getDeprecationHandler())) { try { client = TextTemplate.parse(parser); } catch (ElasticsearchParseException e) { - throw new ElasticsearchParseException("could not parse pager duty event template. failed to parse field [{}]", - Fields.CLIENT.getPreferredName()); + throw new ElasticsearchParseException( + "could not parse pager duty event template. failed to parse field [{}]", + Fields.CLIENT.getPreferredName() + ); } } else if (Fields.CLIENT_URL.match(currentFieldName, parser.getDeprecationHandler())) { try { clientUrl = TextTemplate.parse(parser); } catch (ElasticsearchParseException e) { - throw new ElasticsearchParseException("could not parse pager duty event template. failed to parse field [{}]", - Fields.CLIENT_URL.getPreferredName()); + throw new ElasticsearchParseException( + "could not parse pager duty event template. failed to parse field [{}]", + Fields.CLIENT_URL.getPreferredName() + ); } } else if (Fields.ACCOUNT.match(currentFieldName, parser.getDeprecationHandler())) { try { account = parser.text(); } catch (ElasticsearchParseException e) { - throw new ElasticsearchParseException("could not parse pager duty event template. failed to parse field [{}]", - Fields.CLIENT_URL.getPreferredName()); + throw new ElasticsearchParseException( + "could not parse pager duty event template. failed to parse field [{}]", + Fields.CLIENT_URL.getPreferredName() + ); } } else if (Fields.PROXY.match(currentFieldName, parser.getDeprecationHandler())) { proxy = HttpProxy.parse(parser); @@ -367,34 +407,44 @@ public static Template parse(String watchId, String actionId, XContentParser par try { eventType = TextTemplate.parse(parser); } catch (ElasticsearchParseException e) { - throw new ElasticsearchParseException("could not parse pager duty event template. failed to parse field [{}]", - Fields.EVENT_TYPE.getPreferredName()); + throw new ElasticsearchParseException( + "could not parse pager duty event template. failed to parse field [{}]", + Fields.EVENT_TYPE.getPreferredName() + ); } } else if (Fields.ATTACH_PAYLOAD.match(currentFieldName, parser.getDeprecationHandler())) { if (token == XContentParser.Token.VALUE_BOOLEAN) { attachPayload = parser.booleanValue(); } else { - throw new ElasticsearchParseException("could not parse pager duty event template. failed to parse field [{}], " + - "expected a boolean value but found [{}] instead", Fields.ATTACH_PAYLOAD.getPreferredName(), token); + throw new ElasticsearchParseException( + "could not parse pager duty event template. failed to parse field [{}], " + + "expected a boolean value but found [{}] instead", + Fields.ATTACH_PAYLOAD.getPreferredName(), + token + ); } } else if (Fields.CONTEXTS.match(currentFieldName, parser.getDeprecationHandler()) - || Fields.CONTEXT_DEPRECATED.match(currentFieldName, parser.getDeprecationHandler())) { - if (token == XContentParser.Token.START_ARRAY) { - List list = new ArrayList<>(); - while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { - try { - list.add(IncidentEventContext.Template.parse(parser)); - } catch (ElasticsearchParseException e) { - throw new ElasticsearchParseException("could not parse pager duty event template. failed to parse field " + - "[{}]", parser.currentName()); + || Fields.CONTEXT_DEPRECATED.match(currentFieldName, parser.getDeprecationHandler())) { + if (token == XContentParser.Token.START_ARRAY) { + List list = new ArrayList<>(); + while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { + try { + list.add(IncidentEventContext.Template.parse(parser)); + } catch (ElasticsearchParseException e) { + throw new ElasticsearchParseException( + "could not parse pager duty event template. failed to parse field " + "[{}]", + parser.currentName() + ); + } } + contexts = list.toArray(new IncidentEventContext.Template[list.size()]); } - contexts = list.toArray(new IncidentEventContext.Template[list.size()]); + } else { + throw new ElasticsearchParseException( + "could not parse pager duty event template. unexpected field [{}]", + currentFieldName + ); } - } else { - throw new ElasticsearchParseException("could not parse pager duty event template. unexpected field [{}]", - currentFieldName); - } } return new Template(description, eventType, incidentKey, client, clientUrl, account, attachPayload, contexts, proxy); } @@ -436,7 +486,7 @@ public Builder setEventType(TextTemplate eventType) { } public Builder setAccount(String account) { - this.account= account; + this.account = account; return this; } @@ -456,8 +506,9 @@ public Builder addContext(IncidentEventContext.Template context) { } public Template build() { - IncidentEventContext.Template[] contexts = this.contexts.isEmpty() ? null : - this.contexts.toArray(new IncidentEventContext.Template[this.contexts.size()]); + IncidentEventContext.Template[] contexts = this.contexts.isEmpty() + ? null + : this.contexts.toArray(new IncidentEventContext.Template[this.contexts.size()]); return new Template(description, eventType, incidentKey, client, clientUrl, account, attachPayload, contexts, proxy); } } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/pagerduty/IncidentEventContext.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/pagerduty/IncidentEventContext.java index 9177bd408862e..d3faee9e49bcb 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/pagerduty/IncidentEventContext.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/pagerduty/IncidentEventContext.java @@ -7,9 +7,9 @@ package org.elasticsearch.xpack.watcher.notification.pagerduty; import org.elasticsearch.ElasticsearchParseException; +import org.elasticsearch.common.Strings; import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.common.Strings; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -24,7 +24,8 @@ public class IncidentEventContext implements ToXContentObject { enum Type { - LINK, IMAGE + LINK, + IMAGE } final Type type; @@ -58,11 +59,11 @@ public boolean equals(Object o) { IncidentEventContext that = (IncidentEventContext) o; - return Objects.equals(type, that.type) && - Objects.equals(href, that.href) && - Objects.equals(text, that.text) && - Objects.equals(src, that.src) && - Objects.equals(alt, that.alt); + return Objects.equals(type, that.type) + && Objects.equals(href, that.href) + && Objects.equals(text, that.text) + && Objects.equals(src, that.src) + && Objects.equals(alt, that.alt); } @Override @@ -133,45 +134,59 @@ public static IncidentEventContext parse(XContentParser parser) throws IOExcepti return createAndValidateTemplate(type, href, src, alt, text); } - private static IncidentEventContext createAndValidateTemplate(Type type, String href, String src, String alt, - String text) { + private static IncidentEventContext createAndValidateTemplate(Type type, String href, String src, String alt, String text) { if (type == null) { - throw new ElasticsearchParseException("could not parse trigger incident event context. missing required field [{}]", - XField.TYPE.getPreferredName()); + throw new ElasticsearchParseException( + "could not parse trigger incident event context. missing required field [{}]", + XField.TYPE.getPreferredName() + ); } switch (type) { case LINK: if (href == null) { - throw new ElasticsearchParseException("could not parse trigger incident event context. missing required field " + - "[{}] for [{}] context", XField.HREF.getPreferredName(), Type.LINK.name().toLowerCase(Locale.ROOT)); + throw new ElasticsearchParseException( + "could not parse trigger incident event context. missing required field " + "[{}] for [{}] context", + XField.HREF.getPreferredName(), + Type.LINK.name().toLowerCase(Locale.ROOT) + ); } if (src != null) { - throw new ElasticsearchParseException("could not parse trigger incident event context. unexpected field [{}] for " + - "[{}] context", XField.SRC.getPreferredName(), Type.LINK.name().toLowerCase(Locale.ROOT)); + throw new ElasticsearchParseException( + "could not parse trigger incident event context. unexpected field [{}] for " + "[{}] context", + XField.SRC.getPreferredName(), + Type.LINK.name().toLowerCase(Locale.ROOT) + ); } if (alt != null) { - throw new ElasticsearchParseException("could not parse trigger incident event context. unexpected field [{}] for " + - "[{}] context", XField.ALT.getPreferredName(), Type.LINK.name().toLowerCase(Locale.ROOT)); + throw new ElasticsearchParseException( + "could not parse trigger incident event context. unexpected field [{}] for " + "[{}] context", + XField.ALT.getPreferredName(), + Type.LINK.name().toLowerCase(Locale.ROOT) + ); } return link(href, text); case IMAGE: if (src == null) { - throw new ElasticsearchParseException("could not parse trigger incident event context. missing required field " + - "[{}] for [{}] context", XField.SRC.getPreferredName(), Type.IMAGE.name().toLowerCase(Locale.ROOT)); + throw new ElasticsearchParseException( + "could not parse trigger incident event context. missing required field " + "[{}] for [{}] context", + XField.SRC.getPreferredName(), + Type.IMAGE.name().toLowerCase(Locale.ROOT) + ); } if (text != null) { - throw new ElasticsearchParseException("could not parse trigger incident event context. unexpected field [{}] for " + - "[{}] context", XField.TEXT.getPreferredName(), Type.IMAGE.name().toLowerCase(Locale.ROOT)); + throw new ElasticsearchParseException( + "could not parse trigger incident event context. unexpected field [{}] for " + "[{}] context", + XField.TEXT.getPreferredName(), + Type.IMAGE.name().toLowerCase(Locale.ROOT) + ); } return image(src, href, alt); default: - throw new ElasticsearchParseException("could not parse trigger incident event context. unknown context type [{}]", - type); + throw new ElasticsearchParseException("could not parse trigger incident event context. unknown context type [{}]", type); } } - public static class Template implements ToXContentObject { final Type type; @@ -182,16 +197,18 @@ public static class Template implements ToXContentObject { public static Template link(TextTemplate href, @Nullable TextTemplate text) { if (href == null) { - throw new IllegalStateException("could not create link context for pager duty trigger incident event. missing required " + - "[href] setting"); + throw new IllegalStateException( + "could not create link context for pager duty trigger incident event. missing required " + "[href] setting" + ); } return new Template(Type.LINK, href, text, null, null); } public static Template image(TextTemplate src, @Nullable TextTemplate href, @Nullable TextTemplate alt) { if (src == null) { - throw new IllegalStateException("could not create link context for pager duty trigger incident event. missing required " + - "[src] setting"); + throw new IllegalStateException( + "could not create link context for pager duty trigger incident event. missing required " + "[src] setting" + ); } return new Template(Type.IMAGE, href, null, src, alt); } @@ -210,11 +227,11 @@ public boolean equals(Object o) { if (o == null || getClass() != o.getClass()) return false; Template that = (Template) o; - return Objects.equals(type, that.type) && - Objects.equals(href, that.href) && - Objects.equals(text, that.text) && - Objects.equals(src, that.src) && - Objects.equals(alt, that.alt); + return Objects.equals(type, that.type) + && Objects.equals(href, that.href) + && Objects.equals(text, that.text) + && Objects.equals(src, that.src) + && Objects.equals(alt, that.alt); } @Override @@ -309,41 +326,65 @@ public static Template parse(XContentParser parser) throws IOException { return createAndValidateTemplate(type, href, src, alt, text); } - private static Template createAndValidateTemplate(Type type, TextTemplate href, TextTemplate src, TextTemplate alt, - TextTemplate text) { + private static Template createAndValidateTemplate( + Type type, + TextTemplate href, + TextTemplate src, + TextTemplate alt, + TextTemplate text + ) { if (type == null) { - throw new ElasticsearchParseException("could not parse trigger incident event context. missing required field [{}]", - XField.TYPE.getPreferredName()); + throw new ElasticsearchParseException( + "could not parse trigger incident event context. missing required field [{}]", + XField.TYPE.getPreferredName() + ); } switch (type) { case LINK: if (href == null) { - throw new ElasticsearchParseException("could not parse trigger incident event context. missing required field " + - "[{}] for [{}] context", XField.HREF.getPreferredName(), Type.LINK.name().toLowerCase(Locale.ROOT)); + throw new ElasticsearchParseException( + "could not parse trigger incident event context. missing required field " + "[{}] for [{}] context", + XField.HREF.getPreferredName(), + Type.LINK.name().toLowerCase(Locale.ROOT) + ); } if (src != null) { - throw new ElasticsearchParseException("could not parse trigger incident event context. unexpected field [{}] for " + - "[{}] context", XField.SRC.getPreferredName(), Type.LINK.name().toLowerCase(Locale.ROOT)); + throw new ElasticsearchParseException( + "could not parse trigger incident event context. unexpected field [{}] for " + "[{}] context", + XField.SRC.getPreferredName(), + Type.LINK.name().toLowerCase(Locale.ROOT) + ); } if (alt != null) { - throw new ElasticsearchParseException("could not parse trigger incident event context. unexpected field [{}] for " + - "[{}] context", XField.ALT.getPreferredName(), Type.LINK.name().toLowerCase(Locale.ROOT)); + throw new ElasticsearchParseException( + "could not parse trigger incident event context. unexpected field [{}] for " + "[{}] context", + XField.ALT.getPreferredName(), + Type.LINK.name().toLowerCase(Locale.ROOT) + ); } return link(href, text); case IMAGE: if (src == null) { - throw new ElasticsearchParseException("could not parse trigger incident event context. missing required field " + - "[{}] for [{}] context", XField.SRC.getPreferredName(), Type.IMAGE.name().toLowerCase(Locale.ROOT)); + throw new ElasticsearchParseException( + "could not parse trigger incident event context. missing required field " + "[{}] for [{}] context", + XField.SRC.getPreferredName(), + Type.IMAGE.name().toLowerCase(Locale.ROOT) + ); } if (text != null) { - throw new ElasticsearchParseException("could not parse trigger incident event context. unexpected field [{}] for " + - "[{}] context", XField.TEXT.getPreferredName(), Type.IMAGE.name().toLowerCase(Locale.ROOT)); + throw new ElasticsearchParseException( + "could not parse trigger incident event context. unexpected field [{}] for " + "[{}] context", + XField.TEXT.getPreferredName(), + Type.IMAGE.name().toLowerCase(Locale.ROOT) + ); } return image(src, href, alt); default: - throw new ElasticsearchParseException("could not parse trigger incident event context. unknown context type [{}]", - type); + throw new ElasticsearchParseException( + "could not parse trigger incident event context. unknown context type [{}]", + type + ); } } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/pagerduty/IncidentEventDefaults.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/pagerduty/IncidentEventDefaults.java index b29815c6f1edf..e7d639671bc4f 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/pagerduty/IncidentEventDefaults.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/pagerduty/IncidentEventDefaults.java @@ -55,7 +55,7 @@ public int hashCode() { @Override public boolean equals(Object obj) { - if (obj == null || getClass() != obj.getClass()){ + if (obj == null || getClass() != obj.getClass()) { return false; } final LinkDefaults other = (LinkDefaults) obj; @@ -82,7 +82,7 @@ public int hashCode() { @Override public boolean equals(Object obj) { - if (obj == null || getClass() != obj.getClass()){ + if (obj == null || getClass() != obj.getClass()) { return false; } final ImageDefaults other = (ImageDefaults) obj; @@ -91,5 +91,4 @@ public boolean equals(Object obj) { } } - } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/pagerduty/PagerDutyAccount.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/pagerduty/PagerDutyAccount.java index a90f0797793ab..6214841348432 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/pagerduty/PagerDutyAccount.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/pagerduty/PagerDutyAccount.java @@ -54,7 +54,8 @@ private static String getServiceKey(String name, Settings accountSettings) { SecureString secureString = SECURE_SERVICE_API_KEY_SETTING.get(accountSettings); if (secureString == null || secureString.length() < 1) { throw new SettingsException( - "invalid pagerduty account [" + name + "]. missing required [" + SECURE_SERVICE_API_KEY_SETTING.getKey() + "] setting"); + "invalid pagerduty account [" + name + "]. missing required [" + SECURE_SERVICE_API_KEY_SETTING.getKey() + "] setting" + ); } return secureString.toString(); } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/pagerduty/PagerDutyService.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/pagerduty/PagerDutyService.java index a704dfef80bba..1976018ddb9c1 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/pagerduty/PagerDutyService.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/pagerduty/PagerDutyService.java @@ -24,16 +24,23 @@ */ public class PagerDutyService extends NotificationService { - private static final Setting SETTING_DEFAULT_ACCOUNT = - Setting.simpleString("xpack.notification.pagerduty.default_account", Property.Dynamic, Property.NodeScope); + private static final Setting SETTING_DEFAULT_ACCOUNT = Setting.simpleString( + "xpack.notification.pagerduty.default_account", + Property.Dynamic, + Property.NodeScope + ); - private static final Setting.AffixSetting SETTING_SECURE_SERVICE_API_KEY = - Setting.affixKeySetting("xpack.notification.pagerduty.account.", "secure_service_api_key", - (key) -> SecureSetting.secureString(key, null)); + private static final Setting.AffixSetting SETTING_SECURE_SERVICE_API_KEY = Setting.affixKeySetting( + "xpack.notification.pagerduty.account.", + "secure_service_api_key", + (key) -> SecureSetting.secureString(key, null) + ); - private static final Setting.AffixSetting SETTING_DEFAULTS = - Setting.affixKeySetting("xpack.notification.pagerduty.account.", "event_defaults", - (key) -> Setting.groupSetting(key + ".", Property.Dynamic, Property.NodeScope)); + private static final Setting.AffixSetting SETTING_DEFAULTS = Setting.affixKeySetting( + "xpack.notification.pagerduty.account.", + "event_defaults", + (key) -> Setting.groupSetting(key + ".", Property.Dynamic, Property.NodeScope) + ); private final HttpClient httpClient; diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/pagerduty/SentEvent.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/pagerduty/SentEvent.java index 09b25caa73b91..5fbffe34795af 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/pagerduty/SentEvent.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/pagerduty/SentEvent.java @@ -7,10 +7,10 @@ package org.elasticsearch.xpack.watcher.notification.pagerduty; import org.elasticsearch.ElasticsearchParseException; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -29,9 +29,12 @@ public class SentEvent implements ToXContentObject { final IncidentEvent event; - @Nullable final HttpRequest request; - @Nullable final HttpResponse response; - @Nullable final String failureReason; + @Nullable + final HttpRequest request; + @Nullable + final HttpResponse response; + @Nullable + final String failureReason; public static SentEvent responded(IncidentEvent event, HttpRequest request, HttpResponse response) { String failureReason = resolveFailureReason(response); @@ -67,9 +70,9 @@ public boolean equals(Object o) { if (o == null || getClass() != o.getClass()) return false; SentEvent sentEvent = (SentEvent) o; - return Objects.equals(event, sentEvent.event) && - Objects.equals(request, sentEvent.request) && - Objects.equals(failureReason, sentEvent.failureReason); + return Objects.equals(event, sentEvent.event) + && Objects.equals(request, sentEvent.request) + && Objects.equals(failureReason, sentEvent.failureReason); } @Override @@ -88,8 +91,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws // as this makes debugging pagerduty services much harder, this should be changed to only filter for // body.service_key - however the body is currently just a string, making filtering much harder if (WatcherParams.hideSecrets(params)) { - try (InputStream is = HttpRequest.filterToXContent(request, builder.contentType(), - params, "body")) { + try (InputStream is = HttpRequest.filterToXContent(request, builder.contentType(), params, "body")) { builder.rawField(XField.REQUEST.getPreferredName(), is, builder.contentType()); } } else { @@ -116,10 +118,12 @@ private static String resolveFailureReason(HttpResponse response) { // lets first try to parse the error response in the body // based on https://developer.pagerduty.com/documentation/rest/errors - try (InputStream stream = response.body().streamInput(); - XContentParser parser = JsonXContent.jsonXContent - // EMPTY is safe here because we never call namedObject - .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) { + try ( + InputStream stream = response.body().streamInput(); + XContentParser parser = JsonXContent.jsonXContent + // EMPTY is safe here because we never call namedObject + .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream) + ) { parser.nextToken(); String message = null; @@ -139,8 +143,10 @@ private static String resolveFailureReason(HttpResponse response) { errors.add(parser.text()); } } else { - throw new ElasticsearchParseException("could not parse pagerduty event response. unexpected field [{}]", - currentFieldName); + throw new ElasticsearchParseException( + "could not parse pagerduty event response. unexpected field [{}]", + currentFieldName + ); } } @@ -162,12 +168,18 @@ private static String resolveFailureReason(HttpResponse response) { } switch (status) { - case 400: return "Bad Request"; - case 401: return "Unauthorized. The account service api key is invalid."; - case 403: return "Forbidden. The account doesn't have permission to send this trigger."; - case 404: return "The account used invalid HipChat APIs"; - case 408: return "Request Timeout. The request took too long to process."; - case 500: return "PagerDuty Server Error. Internal error occurred while processing request."; + case 400: + return "Bad Request"; + case 401: + return "Unauthorized. The account service api key is invalid."; + case 403: + return "Forbidden. The account doesn't have permission to send this trigger."; + case 404: + return "The account used invalid HipChat APIs"; + case 408: + return "Request Timeout. The request took too long to process."; + case 500: + return "PagerDuty Server Error. Internal error occurred while processing request."; default: return "Unknown Error"; } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/slack/SentMessages.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/slack/SentMessages.java index b6e130b64b35b..94d17844f06da 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/slack/SentMessages.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/slack/SentMessages.java @@ -74,9 +74,12 @@ public static class SentMessage implements ToXContentObject { final String to; final SlackMessage message; - @Nullable final HttpRequest request; - @Nullable final HttpResponse response; - @Nullable final Exception exception; + @Nullable + final HttpRequest request; + @Nullable + final HttpResponse response; + @Nullable + final Exception exception; public static SentMessage responded(String to, SlackMessage message, HttpRequest request, HttpResponse response) { return new SentMessage(to, message, request, response, null); @@ -119,8 +122,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws if (WatcherParams.hideSecrets(params)) { // this writes out the request to the byte array output stream with the correct excludes // for slack - try (InputStream is = HttpRequest.filterToXContent(request, builder.contentType(), - params, "path")) { + try (InputStream is = HttpRequest.filterToXContent(request, builder.contentType(), params, "path")) { builder.rawField(REQUEST.getPreferredName(), is, builder.contentType()); } } else { diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/slack/SlackAccount.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/slack/SlackAccount.java index 4b4ac36450525..f20340fcfce02 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/slack/SlackAccount.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/slack/SlackAccount.java @@ -72,43 +72,43 @@ public SentMessages send(final SlackMessage message, HttpProxy proxy) { public SentMessages.SentMessage send(final String to, final SlackMessage message, final HttpProxy proxy) { HttpRequest request = HttpRequest.builder(url.getHost(), url.getPort()) - .path(url.getPath()) - .method(HttpMethod.POST) - .proxy(proxy) - .scheme(Scheme.parse(url.getScheme())) - .jsonBody(new ToXContent() { - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - if (to != null) { - builder.field("channel", to); - } - if (message.getFrom() != null) { - builder.field("username", message.getFrom()); - } - String icon = message.getIcon(); - if (icon != null) { - if (icon.startsWith("http")) { - builder.field("icon_url", icon); - } else { - builder.field("icon_emoji", icon); - } + .path(url.getPath()) + .method(HttpMethod.POST) + .proxy(proxy) + .scheme(Scheme.parse(url.getScheme())) + .jsonBody(new ToXContent() { + @Override + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + if (to != null) { + builder.field("channel", to); + } + if (message.getFrom() != null) { + builder.field("username", message.getFrom()); + } + String icon = message.getIcon(); + if (icon != null) { + if (icon.startsWith("http")) { + builder.field("icon_url", icon); + } else { + builder.field("icon_emoji", icon); } - if (message.getText() != null) { - builder.field("text", message.getText()); + } + if (message.getText() != null) { + builder.field("text", message.getText()); + } + Attachment[] attachments = message.getAttachments(); + if (attachments != null && attachments.length > 0) { + builder.startArray("attachments"); + for (Attachment attachment : attachments) { + attachment.toXContent(builder, params); } - Attachment[] attachments = message.getAttachments(); - if (attachments != null && attachments.length > 0) { - builder.startArray("attachments"); - for (Attachment attachment : attachments) { - attachment.toXContent(builder, params); - } - builder.endArray(); + builder.endArray(); - } - return builder; } - }) - .build(); + return builder; + } + }) + .build(); try { HttpResponse response = httpClient.execute(request); @@ -123,13 +123,16 @@ static URI url(String name, Settings settings) { SecureString secureStringUrl = SECURE_URL_SETTING.get(settings); if (secureStringUrl == null || secureStringUrl.length() < 1) { throw new SettingsException( - "invalid slack [" + name + "] account settings. missing required [" + SECURE_URL_SETTING.getKey() + "] setting"); + "invalid slack [" + name + "] account settings. missing required [" + SECURE_URL_SETTING.getKey() + "] setting" + ); } try { return new URI(secureStringUrl.toString()); } catch (URISyntaxException e) { throw new SettingsException( - "invalid slack [" + name + "] account settings. invalid [" + SECURE_URL_SETTING.getKey() + "] setting", e); + "invalid slack [" + name + "] account settings. invalid [" + SECURE_URL_SETTING.getKey() + "] setting", + e + ); } } } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/slack/SlackService.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/slack/SlackService.java index d718fb8268ebb..33df0ad473b25 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/slack/SlackService.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/slack/SlackService.java @@ -26,15 +26,23 @@ */ public class SlackService extends NotificationService { - private static final Setting SETTING_DEFAULT_ACCOUNT = - Setting.simpleString("xpack.notification.slack.default_account", Property.Dynamic, Property.NodeScope); + private static final Setting SETTING_DEFAULT_ACCOUNT = Setting.simpleString( + "xpack.notification.slack.default_account", + Property.Dynamic, + Property.NodeScope + ); - private static final Setting.AffixSetting SETTING_URL_SECURE = - Setting.affixKeySetting("xpack.notification.slack.account.", "secure_url", (key) -> SecureSetting.secureString(key, null)); + private static final Setting.AffixSetting SETTING_URL_SECURE = Setting.affixKeySetting( + "xpack.notification.slack.account.", + "secure_url", + (key) -> SecureSetting.secureString(key, null) + ); - private static final Setting.AffixSetting SETTING_DEFAULTS = - Setting.affixKeySetting("xpack.notification.slack.account.", "message_defaults", - (key) -> Setting.groupSetting(key + ".", Property.Dynamic, Property.NodeScope)); + private static final Setting.AffixSetting SETTING_DEFAULTS = Setting.affixKeySetting( + "xpack.notification.slack.account.", + "message_defaults", + (key) -> Setting.groupSetting(key + ".", Property.Dynamic, Property.NodeScope) + ); private static final Logger logger = LogManager.getLogger(SlackService.class); diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/slack/message/Action.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/slack/message/Action.java index c892df76a1269..6e545c82a9297 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/slack/message/Action.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/slack/message/Action.java @@ -6,9 +6,9 @@ */ package org.elasticsearch.xpack.watcher.notification.slack.message; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ObjectParser; import org.elasticsearch.xcontent.ObjectParser.ValueType; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.watcher.common.text.TextTemplate; @@ -41,8 +41,7 @@ public class Action implements MessageElement { private String text; private String url; - public Action() { - } + public Action() {} public Action(String style, String name, String type, String text, String url) { this.style = style; @@ -59,8 +58,11 @@ public boolean equals(Object o) { Action template = (Action) o; - return Objects.equals(style, template.style) && Objects.equals(type, template.type) && Objects.equals(url, template.url) - && Objects.equals(text, template.text) && Objects.equals(name, template.name); + return Objects.equals(style, template.style) + && Objects.equals(type, template.type) + && Objects.equals(url, template.url) + && Objects.equals(text, template.text) + && Objects.equals(name, template.name); } @Override @@ -71,12 +73,12 @@ public int hashCode() { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { return builder.startObject() - .field(NAME.getPreferredName(), name) - .field(STYLE.getPreferredName(), style) - .field(TYPE.getPreferredName(), type) - .field(TEXT.getPreferredName(), text) - .field(URL.getPreferredName(), url) - .endObject(); + .field(NAME.getPreferredName(), name) + .field(STYLE.getPreferredName(), style) + .field(TYPE.getPreferredName(), type) + .field(TEXT.getPreferredName(), text) + .field(URL.getPreferredName(), url) + .endObject(); } static class Template implements ToXContent { @@ -103,8 +105,11 @@ public boolean equals(Object o) { Template template = (Template) o; - return Objects.equals(style, template.style) && Objects.equals(type, template.type) && Objects.equals(url, template.url) - && Objects.equals(text, template.text) && Objects.equals(name, template.name); + return Objects.equals(style, template.style) + && Objects.equals(type, template.type) + && Objects.equals(url, template.url) + && Objects.equals(text, template.text) + && Objects.equals(name, template.name); } @Override @@ -115,12 +120,12 @@ public int hashCode() { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { return builder.startObject() - .field(NAME.getPreferredName(), name) - .field(STYLE.getPreferredName(), style) - .field(TYPE.getPreferredName(), type) - .field(TEXT.getPreferredName(), text) - .field(URL.getPreferredName(), url) - .endObject(); + .field(NAME.getPreferredName(), name) + .field(STYLE.getPreferredName(), style) + .field(TYPE.getPreferredName(), type) + .field(TEXT.getPreferredName(), text) + .field(URL.getPreferredName(), url) + .endObject(); } public TextTemplate getType() { diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/slack/message/Attachment.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/slack/message/Attachment.java index d161b0c9c7d97..1242627c3e946 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/slack/message/Attachment.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/slack/message/Attachment.java @@ -38,9 +38,22 @@ public class Attachment implements MessageElement { final String[] markdownSupportedFields; final List actions; - public Attachment(String fallback, String color, String pretext, String authorName, String authorLink, - String authorIcon, String title, String titleLink, String text, Field[] fields, - String imageUrl, String thumbUrl, String[] markdownSupportedFields, List actions) { + public Attachment( + String fallback, + String color, + String pretext, + String authorName, + String authorLink, + String authorIcon, + String title, + String titleLink, + String text, + Field[] fields, + String imageUrl, + String thumbUrl, + String[] markdownSupportedFields, + List actions + ) { this.fallback = fallback; this.color = color; @@ -64,19 +77,40 @@ public boolean equals(Object o) { if (o == null || getClass() != o.getClass()) return false; Attachment that = (Attachment) o; - return Objects.equals(fallback, that.fallback) && Objects.equals(color, that.color) && - Objects.equals(pretext, that.pretext) && Objects.equals(authorName, that.authorName) && - Objects.equals(authorLink, that.authorLink) && Objects.equals(authorIcon, that.authorIcon) && - Objects.equals(title, that.title) && Objects.equals(titleLink, that.titleLink) && - Objects.equals(text, that.text) && Objects.equals(imageUrl, that.imageUrl) && - Objects.equals(thumbUrl, that.thumbUrl) && Objects.equals(actions, that.actions) && - Arrays.equals(markdownSupportedFields, that.markdownSupportedFields) && Arrays.equals(fields, that.fields); + return Objects.equals(fallback, that.fallback) + && Objects.equals(color, that.color) + && Objects.equals(pretext, that.pretext) + && Objects.equals(authorName, that.authorName) + && Objects.equals(authorLink, that.authorLink) + && Objects.equals(authorIcon, that.authorIcon) + && Objects.equals(title, that.title) + && Objects.equals(titleLink, that.titleLink) + && Objects.equals(text, that.text) + && Objects.equals(imageUrl, that.imageUrl) + && Objects.equals(thumbUrl, that.thumbUrl) + && Objects.equals(actions, that.actions) + && Arrays.equals(markdownSupportedFields, that.markdownSupportedFields) + && Arrays.equals(fields, that.fields); } @Override public int hashCode() { - return Objects.hash(fallback, color, pretext, authorName, authorLink, authorIcon, title, titleLink, text, fields, imageUrl, - thumbUrl, markdownSupportedFields, actions); + return Objects.hash( + fallback, + color, + pretext, + authorName, + authorLink, + authorIcon, + title, + titleLink, + text, + fields, + imageUrl, + thumbUrl, + markdownSupportedFields, + actions + ); } /** @@ -162,10 +196,22 @@ static class Template implements ToXContentObject { final TextTemplate[] markdownSupportedFields; final List actions; - Template(TextTemplate fallback, TextTemplate color, TextTemplate pretext, TextTemplate authorName, - TextTemplate authorLink, TextTemplate authorIcon, TextTemplate title, TextTemplate titleLink, - TextTemplate text, Field.Template[] fields, TextTemplate imageUrl, TextTemplate thumbUrl, - TextTemplate[] markdownSupportedFields, List actions) { + Template( + TextTemplate fallback, + TextTemplate color, + TextTemplate pretext, + TextTemplate authorName, + TextTemplate authorLink, + TextTemplate authorIcon, + TextTemplate title, + TextTemplate titleLink, + TextTemplate text, + Field.Template[] fields, + TextTemplate imageUrl, + TextTemplate thumbUrl, + TextTemplate[] markdownSupportedFields, + List actions + ) { this.fallback = fallback; this.color = color; @@ -216,8 +262,22 @@ public Attachment render(TextTemplateEngine engine, Map model, S } } - return new Attachment(fallback, color, pretext, authorName, authorLink, authorIcon, title, titleLink, text, fields, imageUrl, - thumbUrl, markdownFields, actions); + return new Attachment( + fallback, + color, + pretext, + authorName, + authorLink, + authorIcon, + title, + titleLink, + text, + fields, + imageUrl, + thumbUrl, + markdownFields, + actions + ); } @Override @@ -227,20 +287,40 @@ public boolean equals(Object o) { Template template = (Template) o; - return Objects.equals(fallback, template.fallback) && Objects.equals(color, template.color) && - Objects.equals(pretext, template.pretext) && Objects.equals(authorName, template.authorName) && - Objects.equals(authorLink, template.authorLink) && Objects.equals(authorIcon, template.authorIcon) && - Objects.equals(title, template.title) && Objects.equals(titleLink, template.titleLink) && - Objects.equals(text, template.text) && Objects.equals(imageUrl, template.imageUrl) && - Objects.equals(thumbUrl, template.thumbUrl) && Objects.equals(actions, template.actions) && - Arrays.equals(fields, template.fields) && - Arrays.equals(markdownSupportedFields, template.markdownSupportedFields); + return Objects.equals(fallback, template.fallback) + && Objects.equals(color, template.color) + && Objects.equals(pretext, template.pretext) + && Objects.equals(authorName, template.authorName) + && Objects.equals(authorLink, template.authorLink) + && Objects.equals(authorIcon, template.authorIcon) + && Objects.equals(title, template.title) + && Objects.equals(titleLink, template.titleLink) + && Objects.equals(text, template.text) + && Objects.equals(imageUrl, template.imageUrl) + && Objects.equals(thumbUrl, template.thumbUrl) + && Objects.equals(actions, template.actions) + && Arrays.equals(fields, template.fields) + && Arrays.equals(markdownSupportedFields, template.markdownSupportedFields); } @Override public int hashCode() { - return Objects.hash(fallback, color, pretext, authorName, authorLink, authorIcon, title, titleLink, text, fields, imageUrl, - thumbUrl, markdownSupportedFields, actions); + return Objects.hash( + fallback, + color, + pretext, + authorName, + authorLink, + authorIcon, + title, + titleLink, + text, + fields, + imageUrl, + thumbUrl, + markdownSupportedFields, + actions + ); } @Override @@ -330,64 +410,91 @@ public static Template parse(XContentParser parser) throws IOException { try { fallback = TextTemplate.parse(parser); } catch (ElasticsearchParseException pe) { - throw new ElasticsearchParseException("could not parse message attachment. failed to parse [{}] field", pe, - XField.FALLBACK); + throw new ElasticsearchParseException( + "could not parse message attachment. failed to parse [{}] field", + pe, + XField.FALLBACK + ); } } else if (XField.COLOR.match(currentFieldName, parser.getDeprecationHandler())) { try { color = TextTemplate.parse(parser); } catch (ElasticsearchParseException pe) { - throw new ElasticsearchParseException("could not parse message attachment. failed to parse [{}] field", pe, - XField.COLOR); + throw new ElasticsearchParseException( + "could not parse message attachment. failed to parse [{}] field", + pe, + XField.COLOR + ); } } else if (XField.PRETEXT.match(currentFieldName, parser.getDeprecationHandler())) { try { pretext = TextTemplate.parse(parser); } catch (ElasticsearchParseException pe) { - throw new ElasticsearchParseException("could not parse message attachment. failed to parse [{}] field", pe, - XField.PRETEXT); + throw new ElasticsearchParseException( + "could not parse message attachment. failed to parse [{}] field", + pe, + XField.PRETEXT + ); } } else if (XField.AUTHOR_NAME.match(currentFieldName, parser.getDeprecationHandler())) { try { authorName = TextTemplate.parse(parser); } catch (ElasticsearchParseException pe) { - throw new ElasticsearchParseException("could not parse message attachment. failed to parse [{}] field", pe, - XField.AUTHOR_NAME); + throw new ElasticsearchParseException( + "could not parse message attachment. failed to parse [{}] field", + pe, + XField.AUTHOR_NAME + ); } } else if (XField.AUTHOR_LINK.match(currentFieldName, parser.getDeprecationHandler())) { try { authorLink = TextTemplate.parse(parser); } catch (ElasticsearchParseException pe) { - throw new ElasticsearchParseException("could not parse message attachment. failed to parse [{}] field", pe, - XField.AUTHOR_LINK); + throw new ElasticsearchParseException( + "could not parse message attachment. failed to parse [{}] field", + pe, + XField.AUTHOR_LINK + ); } } else if (XField.AUTHOR_ICON.match(currentFieldName, parser.getDeprecationHandler())) { try { authorIcon = TextTemplate.parse(parser); } catch (ElasticsearchParseException pe) { - throw new ElasticsearchParseException("could not parse message attachment. failed to parse [{}] field", pe, - XField.AUTHOR_ICON); + throw new ElasticsearchParseException( + "could not parse message attachment. failed to parse [{}] field", + pe, + XField.AUTHOR_ICON + ); } } else if (XField.TITLE.match(currentFieldName, parser.getDeprecationHandler())) { try { title = TextTemplate.parse(parser); } catch (ElasticsearchParseException pe) { - throw new ElasticsearchParseException("could not parse message attachment. failed to parse [{}] field", pe, - XField.TITLE); + throw new ElasticsearchParseException( + "could not parse message attachment. failed to parse [{}] field", + pe, + XField.TITLE + ); } } else if (XField.TITLE_LINK.match(currentFieldName, parser.getDeprecationHandler())) { try { titleLink = TextTemplate.parse(parser); } catch (ElasticsearchParseException pe) { - throw new ElasticsearchParseException("could not parse message attachment. failed to parse [{}] field", pe, - XField.TITLE_LINK); + throw new ElasticsearchParseException( + "could not parse message attachment. failed to parse [{}] field", + pe, + XField.TITLE_LINK + ); } } else if (XField.TEXT.match(currentFieldName, parser.getDeprecationHandler())) { try { text = TextTemplate.parse(parser); } catch (ElasticsearchParseException pe) { - throw new ElasticsearchParseException("could not parse message attachment. failed to parse [{}] field", pe, - XField.TEXT); + throw new ElasticsearchParseException( + "could not parse message attachment. failed to parse [{}] field", + pe, + XField.TEXT + ); } } else if (XField.FIELDS.match(currentFieldName, parser.getDeprecationHandler())) { if (token == XContentParser.Token.START_ARRAY) { @@ -396,32 +503,44 @@ public static Template parse(XContentParser parser) throws IOException { try { list.add(Field.Template.parse(parser)); } catch (ElasticsearchParseException pe) { - throw new ElasticsearchParseException("could not parse message attachment. failed to parse [{}] field", - pe, XField.FIELDS); + throw new ElasticsearchParseException( + "could not parse message attachment. failed to parse [{}] field", + pe, + XField.FIELDS + ); } } fields = list.toArray(new Field.Template[list.size()]); } else { try { - fields = new Field.Template[]{Field.Template.parse(parser)}; + fields = new Field.Template[] { Field.Template.parse(parser) }; } catch (ElasticsearchParseException pe) { - throw new ElasticsearchParseException("could not parse message attachment. failed to parse [{}] field", pe, - XField.FIELDS); + throw new ElasticsearchParseException( + "could not parse message attachment. failed to parse [{}] field", + pe, + XField.FIELDS + ); } } } else if (XField.IMAGE_URL.match(currentFieldName, parser.getDeprecationHandler())) { try { imageUrl = TextTemplate.parse(parser); } catch (ElasticsearchParseException pe) { - throw new ElasticsearchParseException("could not parse message attachment. failed to parse [{}] field", pe, - XField.IMAGE_URL); + throw new ElasticsearchParseException( + "could not parse message attachment. failed to parse [{}] field", + pe, + XField.IMAGE_URL + ); } } else if (XField.THUMB_URL.match(currentFieldName, parser.getDeprecationHandler())) { try { thumbUrl = TextTemplate.parse(parser); } catch (ElasticsearchParseException pe) { - throw new ElasticsearchParseException("could not parse message attachment. failed to parse [{}] field", pe, - XField.THUMB_URL); + throw new ElasticsearchParseException( + "could not parse message attachment. failed to parse [{}] field", + pe, + XField.THUMB_URL + ); } } else if (XField.MARKDOWN_IN.match(currentFieldName, parser.getDeprecationHandler())) { if (token == XContentParser.Token.START_ARRAY) { @@ -430,17 +549,23 @@ public static Template parse(XContentParser parser) throws IOException { try { list.add(new TextTemplate(parser.text())); } catch (ElasticsearchParseException pe) { - throw new ElasticsearchParseException("could not parse message attachment. failed to parse [{}] field", - pe, XField.MARKDOWN_IN); + throw new ElasticsearchParseException( + "could not parse message attachment. failed to parse [{}] field", + pe, + XField.MARKDOWN_IN + ); } } markdownFields = list.toArray(new TextTemplate[list.size()]); } else { try { - markdownFields = new TextTemplate[]{new TextTemplate(parser.text())}; + markdownFields = new TextTemplate[] { new TextTemplate(parser.text()) }; } catch (ElasticsearchParseException pe) { - throw new ElasticsearchParseException("could not parse message attachment. failed to parse [{}] field", pe, - XField.MARKDOWN_IN); + throw new ElasticsearchParseException( + "could not parse message attachment. failed to parse [{}] field", + pe, + XField.MARKDOWN_IN + ); } } } else if (XField.ACTIONS.match(currentFieldName, parser.getDeprecationHandler())) { @@ -448,31 +573,55 @@ public static Template parse(XContentParser parser) throws IOException { actions.add(Action.ACTION_PARSER.parse(parser, null)); } } else { - throw new ElasticsearchParseException("could not parse message attachment field. unexpected field [{}]", - currentFieldName); + throw new ElasticsearchParseException( + "could not parse message attachment field. unexpected field [{}]", + currentFieldName + ); } } if (authorName == null) { if (authorLink != null) { - throw new ElasticsearchParseException("could not parse message attachment field. found field [{}], but no [{}] is " + - "defined", XField.AUTHOR_LINK, XField.AUTHOR_NAME); + throw new ElasticsearchParseException( + "could not parse message attachment field. found field [{}], but no [{}] is " + "defined", + XField.AUTHOR_LINK, + XField.AUTHOR_NAME + ); } if (authorIcon != null) { - throw new ElasticsearchParseException("could not parse message attachment field. found field [{}], but no [{}] is " + - "defined", XField.AUTHOR_ICON, XField.AUTHOR_NAME); + throw new ElasticsearchParseException( + "could not parse message attachment field. found field [{}], but no [{}] is " + "defined", + XField.AUTHOR_ICON, + XField.AUTHOR_NAME + ); } } if (title == null) { if (titleLink != null) { - throw new ElasticsearchParseException("could not parse message attachment field. found field [{}], but no [{}] is " + - "defined", XField.TITLE_LINK, XField.TITLE); + throw new ElasticsearchParseException( + "could not parse message attachment field. found field [{}], but no [{}] is " + "defined", + XField.TITLE_LINK, + XField.TITLE + ); } } - return new Template(fallback, color, pretext, authorName, authorLink, authorIcon, title, titleLink, text, fields, imageUrl, - thumbUrl, markdownFields, actions); + return new Template( + fallback, + color, + pretext, + authorName, + authorLink, + authorIcon, + title, + titleLink, + text, + fields, + imageUrl, + thumbUrl, + markdownFields, + actions + ); } - public static Builder builder() { return new Builder(); } @@ -494,8 +643,7 @@ public static class Builder { private List markdownFields = new ArrayList<>(); private List actions = new ArrayList<>(); - private Builder() { - } + private Builder() {} public Builder setFallback(TextTemplate fallback) { this.fallback = fallback; @@ -617,10 +765,25 @@ public Builder addAction(Action.Template action) { public Template build() { Field.Template[] fields = this.fields.isEmpty() ? null : this.fields.toArray(new Field.Template[this.fields.size()]); - TextTemplate[] markdownFields = - this.markdownFields.isEmpty() ? null : this.markdownFields.toArray(new TextTemplate[this.markdownFields.size()]); - return new Template(fallback, color, pretext, authorName, authorLink, authorIcon, title, titleLink, text, fields, imageUrl, - thumbUrl, markdownFields, actions); + TextTemplate[] markdownFields = this.markdownFields.isEmpty() + ? null + : this.markdownFields.toArray(new TextTemplate[this.markdownFields.size()]); + return new Template( + fallback, + color, + pretext, + authorName, + authorLink, + authorIcon, + title, + titleLink, + text, + fields, + imageUrl, + thumbUrl, + markdownFields, + actions + ); } } } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/slack/message/DynamicAttachments.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/slack/message/DynamicAttachments.java index ddd802bfa4d8a..0ccb550a9682c 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/slack/message/DynamicAttachments.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/slack/message/DynamicAttachments.java @@ -7,10 +7,10 @@ package org.elasticsearch.xpack.watcher.notification.slack.message; import org.elasticsearch.ElasticsearchParseException; +import org.elasticsearch.xcontent.ObjectPath; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.ObjectPath; import org.elasticsearch.xpack.watcher.common.text.TextTemplateEngine; import java.io.IOException; @@ -32,14 +32,26 @@ public DynamicAttachments(String listPath, Attachment.Template attachment) { public List render(TextTemplateEngine engine, Map model, SlackMessageDefaults.AttachmentDefaults defaults) { Object value = ObjectPath.eval(listPath, model); if ((value instanceof Iterable) == false) { - throw new IllegalArgumentException("dynamic attachment could not be resolved. expected context [" + listPath + "] to be a " + - "list, but found [" + value + "] instead"); + throw new IllegalArgumentException( + "dynamic attachment could not be resolved. expected context [" + + listPath + + "] to be a " + + "list, but found [" + + value + + "] instead" + ); } List attachments = new ArrayList<>(); for (Object obj : (Iterable) value) { if ((obj instanceof Map) == false) { - throw new IllegalArgumentException("dynamic attachment could not be resolved. expected [" + listPath + "] list to contain" + - " key/value pairs, but found [" + obj + "] instead"); + throw new IllegalArgumentException( + "dynamic attachment could not be resolved. expected [" + + listPath + + "] list to contain" + + " key/value pairs, but found [" + + obj + + "] instead" + ); } Map attachmentModel = (Map) obj; attachments.add(attachment.render(engine, attachmentModel, defaults)); @@ -50,9 +62,9 @@ public List render(TextTemplateEngine engine, Map mo @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { return builder.startObject() - .field(XField.LIST_PATH.getPreferredName(), listPath) - .field(XField.TEMPLATE.getPreferredName(), attachment, params) - .endObject(); + .field(XField.LIST_PATH.getPreferredName(), listPath) + .field(XField.TEMPLATE.getPreferredName(), attachment, params) + .endObject(); } public static DynamicAttachments parse(XContentParser parser) throws IOException { @@ -68,27 +80,37 @@ public static DynamicAttachments parse(XContentParser parser) throws IOException if (token == XContentParser.Token.VALUE_STRING) { listPath = parser.text(); } else { - throw new ElasticsearchParseException("could not parse dynamic attachments. expected a string value for [{}] field, " + - "but found [{}]", XField.LIST_PATH.getPreferredName(), token); + throw new ElasticsearchParseException( + "could not parse dynamic attachments. expected a string value for [{}] field, " + "but found [{}]", + XField.LIST_PATH.getPreferredName(), + token + ); } } else if (XField.TEMPLATE.match(currentFieldName, parser.getDeprecationHandler())) { try { template = Attachment.Template.parse(parser); } catch (ElasticsearchParseException pe) { - throw new ElasticsearchParseException("could not parse dynamic attachments. failed to parse [{}] field", pe, - XField.TEMPLATE.getPreferredName()); + throw new ElasticsearchParseException( + "could not parse dynamic attachments. failed to parse [{}] field", + pe, + XField.TEMPLATE.getPreferredName() + ); } } else { throw new ElasticsearchParseException("could not parse dynamic attachments. unexpected field [{}]", currentFieldName); } } if (listPath == null) { - throw new ElasticsearchParseException("could not parse dynamic attachments. missing required field [{}]", - XField.LIST_PATH.getPreferredName()); + throw new ElasticsearchParseException( + "could not parse dynamic attachments. missing required field [{}]", + XField.LIST_PATH.getPreferredName() + ); } if (template == null) { - throw new ElasticsearchParseException("could not parse dynamic attachments. missing required field [{}]", - XField.TEMPLATE.getPreferredName()); + throw new ElasticsearchParseException( + "could not parse dynamic attachments. missing required field [{}]", + XField.TEMPLATE.getPreferredName() + ); } return new DynamicAttachments(listPath, template); } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/slack/message/Field.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/slack/message/Field.java index 4d3bde01207d5..d6317afd2e53d 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/slack/message/Field.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/slack/message/Field.java @@ -50,10 +50,10 @@ public int hashCode() { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { return builder.startObject() - .field(XField.TITLE.getPreferredName(), title) - .field(XField.VALUE.getPreferredName(), value) - .field(XField.SHORT.getPreferredName(), isShort) - .endObject(); + .field(XField.TITLE.getPreferredName(), title) + .field(XField.VALUE.getPreferredName(), value) + .field(XField.SHORT.getPreferredName(), isShort) + .endObject(); } static class Template implements ToXContentObject { @@ -68,8 +68,11 @@ static class Template implements ToXContentObject { this.isShort = isShort; } - public Field render(TextTemplateEngine engine, Map model, - SlackMessageDefaults.AttachmentDefaults.FieldDefaults defaults) { + public Field render( + TextTemplateEngine engine, + Map model, + SlackMessageDefaults.AttachmentDefaults.FieldDefaults defaults + ) { String title = this.title != null ? engine.render(this.title, model) : defaults.title; String value = this.value != null ? engine.render(this.value, model) : defaults.value; Boolean isShort = this.isShort != null ? this.isShort : defaults.isShort; @@ -99,10 +102,10 @@ public int hashCode() { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { return builder.startObject() - .field(XField.TITLE.getPreferredName(), title) - .field(XField.VALUE.getPreferredName(), value) - .field(XField.SHORT.getPreferredName(), isShort) - .endObject(); + .field(XField.TITLE.getPreferredName(), title) + .field(XField.VALUE.getPreferredName(), value) + .field(XField.SHORT.getPreferredName(), isShort) + .endObject(); } public static Template parse(XContentParser parser) throws IOException { @@ -120,36 +123,51 @@ public static Template parse(XContentParser parser) throws IOException { try { title = TextTemplate.parse(parser); } catch (ElasticsearchParseException pe) { - throw new ElasticsearchParseException("could not parse message attachment field. failed to parse [{}] field", pe, - XField.TITLE); + throw new ElasticsearchParseException( + "could not parse message attachment field. failed to parse [{}] field", + pe, + XField.TITLE + ); } } else if (XField.VALUE.match(currentFieldName, parser.getDeprecationHandler())) { try { value = TextTemplate.parse(parser); } catch (ElasticsearchParseException pe) { - throw new ElasticsearchParseException("could not parse message attachment field. failed to parse [{}] field", pe, - XField.VALUE); + throw new ElasticsearchParseException( + "could not parse message attachment field. failed to parse [{}] field", + pe, + XField.VALUE + ); } } else if (XField.SHORT.match(currentFieldName, parser.getDeprecationHandler())) { if (token == XContentParser.Token.VALUE_BOOLEAN) { isShort = parser.booleanValue(); } else { - throw new ElasticsearchParseException("could not parse message attachment field. expected a boolean value for " + - "[{}] field, but found [{}]", XField.SHORT, token); + throw new ElasticsearchParseException( + "could not parse message attachment field. expected a boolean value for " + "[{}] field, but found [{}]", + XField.SHORT, + token + ); } } else { - throw new ElasticsearchParseException("could not parse message attachment field. unexpected field [{}]", - currentFieldName); + throw new ElasticsearchParseException( + "could not parse message attachment field. unexpected field [{}]", + currentFieldName + ); } } if (title == null) { - throw new ElasticsearchParseException("could not parse message attachment field. missing required [{}] field", - XField.TITLE); + throw new ElasticsearchParseException( + "could not parse message attachment field. missing required [{}] field", + XField.TITLE + ); } if (value == null) { - throw new ElasticsearchParseException("could not parse message attachment field. missing required [{}] field", - XField.VALUE); + throw new ElasticsearchParseException( + "could not parse message attachment field. missing required [{}] field", + XField.VALUE + ); } return new Template(title, value, isShort); } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/slack/message/SlackMessage.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/slack/message/SlackMessage.java index 0cef2dccc1be1..bb75290c6f0d2 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/slack/message/SlackMessage.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/slack/message/SlackMessage.java @@ -7,13 +7,13 @@ package org.elasticsearch.xpack.watcher.notification.slack.message; import org.elasticsearch.ElasticsearchParseException; +import org.elasticsearch.core.Nullable; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.watcher.common.text.TextTemplate; import org.elasticsearch.xpack.watcher.common.text.TextTemplateEngine; -import org.elasticsearch.core.Nullable; import java.io.IOException; import java.util.ArrayList; @@ -32,7 +32,7 @@ public class SlackMessage implements MessageElement { final Attachment[] attachments; public SlackMessage(String from, String[] to, String icon, @Nullable String text, @Nullable Attachment[] attachments) { - if(text == null && attachments == null) { + if (text == null && attachments == null) { throw new IllegalArgumentException("Both text and attachments cannot be null."); } @@ -127,8 +127,14 @@ public static class Template implements ToXContentObject { final Attachment.Template[] attachments; final DynamicAttachments dynamicAttachments; - public Template(TextTemplate from, TextTemplate[] to, TextTemplate text, TextTemplate icon, Attachment.Template[] attachments, - DynamicAttachments dynamicAttachments) { + public Template( + TextTemplate from, + TextTemplate[] to, + TextTemplate text, + TextTemplate icon, + Attachment.Template[] attachments, + DynamicAttachments dynamicAttachments + ) { this.from = from; this.to = to; this.text = text; @@ -168,12 +174,12 @@ public boolean equals(Object o) { Template template = (Template) o; - return Objects.equals(from, template.from) && - Objects.equals(text, template.text) && - Objects.equals(icon, template.icon) && - Objects.equals(dynamicAttachments, template.dynamicAttachments) && - Arrays.equals(to, template.to) && - Arrays.equals(attachments, template.attachments); + return Objects.equals(from, template.from) + && Objects.equals(text, template.text) + && Objects.equals(icon, template.icon) + && Objects.equals(dynamicAttachments, template.dynamicAttachments) + && Arrays.equals(to, template.to) + && Arrays.equals(attachments, template.attachments); } @Override @@ -181,10 +187,14 @@ public int hashCode() { return Objects.hash(from, to, text, icon, attachments, dynamicAttachments); } - public SlackMessage render(String watchId, String actionId, TextTemplateEngine engine, Map model, - SlackMessageDefaults defaults) { - String from = this.from != null ? engine.render(this.from, model) : - defaults.from != null ? defaults.from : watchId; + public SlackMessage render( + String watchId, + String actionId, + TextTemplateEngine engine, + Map model, + SlackMessageDefaults defaults + ) { + String from = this.from != null ? engine.render(this.from, model) : defaults.from != null ? defaults.from : watchId; String[] to = defaults.to; if (this.to != null) { to = new String[this.to.length]; @@ -257,8 +267,11 @@ public static Template parse(XContentParser parser) throws IOException { try { builder.setFrom(TextTemplate.parse(parser)); } catch (ElasticsearchParseException pe) { - throw new ElasticsearchParseException("could not parse slack message. failed to parse [{}] field", pe, - XField.FROM.getPreferredName()); + throw new ElasticsearchParseException( + "could not parse slack message. failed to parse [{}] field", + pe, + XField.FROM.getPreferredName() + ); } } else if (XField.TO.match(currentFieldName, parser.getDeprecationHandler())) { if (token == XContentParser.Token.START_ARRAY) { @@ -266,31 +279,43 @@ public static Template parse(XContentParser parser) throws IOException { try { builder.addTo(TextTemplate.parse(parser)); } catch (ElasticsearchParseException pe) { - throw new ElasticsearchParseException("could not parse slack message. failed to parse [{}] field.", pe, - XField.TO.getPreferredName()); + throw new ElasticsearchParseException( + "could not parse slack message. failed to parse [{}] field.", + pe, + XField.TO.getPreferredName() + ); } } } else { try { builder.addTo(TextTemplate.parse(parser)); } catch (ElasticsearchParseException pe) { - throw new ElasticsearchParseException("could not parse slack message. failed to parse [{}] field", pe, - XField.TO.getPreferredName()); + throw new ElasticsearchParseException( + "could not parse slack message. failed to parse [{}] field", + pe, + XField.TO.getPreferredName() + ); } } } else if (XField.TEXT.match(currentFieldName, parser.getDeprecationHandler())) { try { builder.setText(TextTemplate.parse(parser)); } catch (ElasticsearchParseException pe) { - throw new ElasticsearchParseException("could not parse slack message. failed to parse [{}] field", pe, - XField.TEXT.getPreferredName()); + throw new ElasticsearchParseException( + "could not parse slack message. failed to parse [{}] field", + pe, + XField.TEXT.getPreferredName() + ); } } else if (XField.ICON.match(currentFieldName, parser.getDeprecationHandler())) { try { builder.setIcon(TextTemplate.parse(parser)); } catch (ElasticsearchParseException pe) { - throw new ElasticsearchParseException("could not parse slack message. failed to parse [{}] field.", pe, - XField.ICON.getPreferredName()); + throw new ElasticsearchParseException( + "could not parse slack message. failed to parse [{}] field.", + pe, + XField.ICON.getPreferredName() + ); } } else if (XField.ATTACHMENTS.match(currentFieldName, parser.getDeprecationHandler())) { if (token == XContentParser.Token.START_ARRAY) { @@ -298,24 +323,33 @@ public static Template parse(XContentParser parser) throws IOException { try { builder.addAttachments(Attachment.Template.parse(parser)); } catch (ElasticsearchParseException pe) { - throw new ElasticsearchParseException("could not parse slack message. failed to parse [{}] field.", pe, - XField.ATTACHMENTS.getPreferredName()); + throw new ElasticsearchParseException( + "could not parse slack message. failed to parse [{}] field.", + pe, + XField.ATTACHMENTS.getPreferredName() + ); } } } else { try { builder.addAttachments(Attachment.Template.parse(parser)); } catch (ElasticsearchParseException pe) { - throw new ElasticsearchParseException("could not parse slack message. failed to parse [{}] field.", pe, - XField.ATTACHMENTS.getPreferredName()); + throw new ElasticsearchParseException( + "could not parse slack message. failed to parse [{}] field.", + pe, + XField.ATTACHMENTS.getPreferredName() + ); } } } else if (XField.DYNAMIC_ATTACHMENTS.match(currentFieldName, parser.getDeprecationHandler())) { try { builder.setDynamicAttachments(DynamicAttachments.parse(parser)); } catch (ElasticsearchParseException pe) { - throw new ElasticsearchParseException("could not parse slack message. failed to parse [{}] field.", pe, - XField.ICON.getPreferredName()); + throw new ElasticsearchParseException( + "could not parse slack message. failed to parse [{}] field.", + pe, + XField.ICON.getPreferredName() + ); } } else { throw new ElasticsearchParseException("could not parse slack message. unknown field [{}].", currentFieldName); @@ -338,8 +372,7 @@ public static class Builder { final List attachments = new ArrayList<>(); DynamicAttachments dynamicAttachments; - private Builder() { - } + private Builder() {} public Builder setFrom(TextTemplate from) { this.from = from; @@ -399,8 +432,9 @@ public Builder setDynamicAttachments(DynamicAttachments dynamicAttachments) { public Template build() { TextTemplate[] to = this.to.isEmpty() ? null : this.to.toArray(new TextTemplate[this.to.size()]); - Attachment.Template[] attachments = this.attachments.isEmpty() ? null : - this.attachments.toArray(new Attachment.Template[this.attachments.size()]); + Attachment.Template[] attachments = this.attachments.isEmpty() + ? null + : this.attachments.toArray(new Attachment.Template[this.attachments.size()]); return new Template(from, to, text, icon, attachments, dynamicAttachments); } } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/slack/message/SlackMessageDefaults.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/slack/message/SlackMessageDefaults.java index 59545d057f82c..5fbc4a2dd341f 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/slack/message/SlackMessageDefaults.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/slack/message/SlackMessageDefaults.java @@ -108,19 +108,38 @@ public boolean equals(Object o) { AttachmentDefaults that = (AttachmentDefaults) o; - return Objects.equals(fallback, that.fallback) && Objects.equals(color, that.color) && - Objects.equals(pretext, that.pretext) && Objects.equals(authorName, that.authorName) && - Objects.equals(authorLink, that.authorLink) && Objects.equals(authorIcon, that.authorIcon) && - Objects.equals(title, that.title) && Objects.equals(titleLink, that.titleLink) && - Objects.equals(text, that.text) && Objects.equals(imageUrl, that.imageUrl) && - Objects.equals(thumbUrl, that.thumbUrl) && Objects.equals(field, that.field) && - Objects.equals(markdownSupportedFields, that.markdownSupportedFields); + return Objects.equals(fallback, that.fallback) + && Objects.equals(color, that.color) + && Objects.equals(pretext, that.pretext) + && Objects.equals(authorName, that.authorName) + && Objects.equals(authorLink, that.authorLink) + && Objects.equals(authorIcon, that.authorIcon) + && Objects.equals(title, that.title) + && Objects.equals(titleLink, that.titleLink) + && Objects.equals(text, that.text) + && Objects.equals(imageUrl, that.imageUrl) + && Objects.equals(thumbUrl, that.thumbUrl) + && Objects.equals(field, that.field) + && Objects.equals(markdownSupportedFields, that.markdownSupportedFields); } @Override public int hashCode() { - return Objects.hash(fallback, color, pretext, authorName, authorLink, authorIcon, title, titleLink, text, imageUrl, - thumbUrl, field, markdownSupportedFields); + return Objects.hash( + fallback, + color, + pretext, + authorName, + authorLink, + authorIcon, + title, + titleLink, + text, + imageUrl, + thumbUrl, + field, + markdownSupportedFields + ); } static class FieldDefaults { diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/rest/action/RestAckWatchAction.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/rest/action/RestAckWatchAction.java index 4d4e62d03f824..9a6e6723198d3 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/rest/action/RestAckWatchAction.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/rest/action/RestAckWatchAction.java @@ -9,13 +9,13 @@ import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.core.RestApiVersion; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.watcher.support.xcontent.WatcherParams; import org.elasticsearch.xpack.core.watcher.transport.actions.ack.AckWatchAction; import org.elasticsearch.xpack.core.watcher.transport.actions.ack.AckWatchRequest; @@ -35,14 +35,14 @@ public class RestAckWatchAction extends BaseRestHandler { @Override public List routes() { return List.of( - Route.builder(POST, "/_watcher/watch/{id}/_ack") - .replaces(POST, "/_xpack/watcher/watch/{id}/_ack", RestApiVersion.V_7).build(), - Route.builder(PUT, "/_watcher/watch/{id}/_ack") - .replaces(PUT, "/_xpack/watcher/watch/{id}/_ack", RestApiVersion.V_7).build(), + Route.builder(POST, "/_watcher/watch/{id}/_ack").replaces(POST, "/_xpack/watcher/watch/{id}/_ack", RestApiVersion.V_7).build(), + Route.builder(PUT, "/_watcher/watch/{id}/_ack").replaces(PUT, "/_xpack/watcher/watch/{id}/_ack", RestApiVersion.V_7).build(), Route.builder(POST, "/_watcher/watch/{id}/_ack/{actions}") - .replaces(POST, "/_xpack/watcher/watch/{id}/_ack/{actions}", RestApiVersion.V_7).build(), + .replaces(POST, "/_xpack/watcher/watch/{id}/_ack/{actions}", RestApiVersion.V_7) + .build(), Route.builder(PUT, "/_watcher/watch/{id}/_ack/{actions}") - .replaces(PUT, "/_xpack/watcher/watch/{id}/_ack/{actions}", RestApiVersion.V_7).build() + .replaces(PUT, "/_xpack/watcher/watch/{id}/_ack/{actions}", RestApiVersion.V_7) + .build() ); } @@ -61,9 +61,12 @@ public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client return channel -> client.execute(AckWatchAction.INSTANCE, ackWatchRequest, new RestBuilderListener(channel) { @Override public RestResponse buildResponse(AckWatchResponse response, XContentBuilder builder) throws Exception { - return new BytesRestResponse(RestStatus.OK, builder.startObject() + return new BytesRestResponse( + RestStatus.OK, + builder.startObject() .field(WatchField.STATUS.getPreferredName(), response.getStatus(), WatcherParams.HIDE_SECRETS) - .endObject()); + .endObject() + ); } }); diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/rest/action/RestActivateWatchAction.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/rest/action/RestActivateWatchAction.java index 5e5297da6c0dc..532de92e7205e 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/rest/action/RestActivateWatchAction.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/rest/action/RestActivateWatchAction.java @@ -9,13 +9,13 @@ import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.core.RestApiVersion; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.watcher.support.xcontent.WatcherParams; import org.elasticsearch.xpack.core.watcher.transport.actions.activate.ActivateWatchAction; import org.elasticsearch.xpack.core.watcher.transport.actions.activate.ActivateWatchRequest; @@ -36,9 +36,11 @@ public class RestActivateWatchAction extends BaseRestHandler { public List routes() { return List.of( Route.builder(POST, "/_watcher/watch/{id}/_activate") - .replaces(POST, "/_xpack/watcher/watch/{id}/_activate", RestApiVersion.V_7).build(), + .replaces(POST, "/_xpack/watcher/watch/{id}/_activate", RestApiVersion.V_7) + .build(), Route.builder(PUT, "/_watcher/watch/{id}/_activate") - .replaces(PUT, "/_xpack/watcher/watch/{id}/_activate", RestApiVersion.V_7).build() + .replaces(PUT, "/_xpack/watcher/watch/{id}/_activate", RestApiVersion.V_7) + .build() ); } @@ -50,16 +52,21 @@ public String getName() { @Override public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) { String watchId = request.param("id"); - return channel -> - client.execute(ActivateWatchAction.INSTANCE, new ActivateWatchRequest(watchId, true), - new RestBuilderListener(channel) { - @Override - public RestResponse buildResponse(ActivateWatchResponse response, XContentBuilder builder) throws Exception { - return new BytesRestResponse(RestStatus.OK, builder.startObject() - .field(WatchField.STATUS.getPreferredName(), response.getStatus(), WatcherParams.HIDE_SECRETS) - .endObject()); - } - }); + return channel -> client.execute( + ActivateWatchAction.INSTANCE, + new ActivateWatchRequest(watchId, true), + new RestBuilderListener(channel) { + @Override + public RestResponse buildResponse(ActivateWatchResponse response, XContentBuilder builder) throws Exception { + return new BytesRestResponse( + RestStatus.OK, + builder.startObject() + .field(WatchField.STATUS.getPreferredName(), response.getStatus(), WatcherParams.HIDE_SECRETS) + .endObject() + ); + } + } + ); } public static class DeactivateRestHandler extends BaseRestHandler { @@ -68,9 +75,11 @@ public static class DeactivateRestHandler extends BaseRestHandler { public List routes() { return List.of( Route.builder(POST, "/_watcher/watch/{id}/_deactivate") - .replaces(POST, "/_xpack/watcher/watch/{id}/_deactivate", RestApiVersion.V_7).build(), + .replaces(POST, "/_xpack/watcher/watch/{id}/_deactivate", RestApiVersion.V_7) + .build(), Route.builder(PUT, "/_watcher/watch/{id}/_deactivate") - .replaces(PUT, "/_xpack/watcher/watch/{id}/_deactivate", RestApiVersion.V_7).build() + .replaces(PUT, "/_xpack/watcher/watch/{id}/_deactivate", RestApiVersion.V_7) + .build() ); } @@ -82,16 +91,21 @@ public String getName() { @Override public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) { String watchId = request.param("id"); - return channel -> - client.execute(ActivateWatchAction.INSTANCE, new ActivateWatchRequest(watchId, false), - new RestBuilderListener(channel) { - @Override - public RestResponse buildResponse(ActivateWatchResponse response, XContentBuilder builder) throws Exception { - return new BytesRestResponse(RestStatus.OK, builder.startObject() - .field(WatchField.STATUS.getPreferredName(), response.getStatus(), WatcherParams.HIDE_SECRETS) - .endObject()); - } - }); + return channel -> client.execute( + ActivateWatchAction.INSTANCE, + new ActivateWatchRequest(watchId, false), + new RestBuilderListener(channel) { + @Override + public RestResponse buildResponse(ActivateWatchResponse response, XContentBuilder builder) throws Exception { + return new BytesRestResponse( + RestStatus.OK, + builder.startObject() + .field(WatchField.STATUS.getPreferredName(), response.getStatus(), WatcherParams.HIDE_SECRETS) + .endObject() + ); + } + } + ); } } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/rest/action/RestDeleteWatchAction.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/rest/action/RestDeleteWatchAction.java index e7c93d9e415c1..1f22aba61cf11 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/rest/action/RestDeleteWatchAction.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/rest/action/RestDeleteWatchAction.java @@ -9,7 +9,6 @@ import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.core.RestApiVersion; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.protocol.xpack.watcher.DeleteWatchRequest; import org.elasticsearch.protocol.xpack.watcher.DeleteWatchResponse; import org.elasticsearch.rest.BaseRestHandler; @@ -18,6 +17,7 @@ import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.watcher.transport.actions.delete.DeleteWatchAction; import java.util.List; @@ -31,8 +31,7 @@ public class RestDeleteWatchAction extends BaseRestHandler { @Override public List routes() { return List.of( - Route.builder(DELETE, "/_watcher/watch/{id}") - .replaces(DELETE, "/_xpack/watcher/watch/{id}", RestApiVersion.V_7).build() + Route.builder(DELETE, "/_watcher/watch/{id}").replaces(DELETE, "/_xpack/watcher/watch/{id}", RestApiVersion.V_7).build() ); } @@ -48,10 +47,10 @@ protected RestChannelConsumer prepareRequest(final RestRequest request, NodeClie @Override public RestResponse buildResponse(DeleteWatchResponse response, XContentBuilder builder) throws Exception { builder.startObject() - .field("_id", response.getId()) - .field("_version", response.getVersion()) - .field("found", response.isFound()) - .endObject(); + .field("_id", response.getId()) + .field("_version", response.getVersion()) + .field("found", response.isFound()) + .endObject(); RestStatus status = response.isFound() ? OK : NOT_FOUND; return new BytesRestResponse(status, builder); } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/rest/action/RestExecuteWatchAction.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/rest/action/RestExecuteWatchAction.java index 1013a1d65c1ee..d48d4e953620d 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/rest/action/RestExecuteWatchAction.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/rest/action/RestExecuteWatchAction.java @@ -9,19 +9,19 @@ import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.common.util.set.Sets; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; +import org.elasticsearch.rest.RestRequestFilter; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; -import org.elasticsearch.rest.RestRequestFilter; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.watcher.execution.ActionExecutionMode; import org.elasticsearch.xpack.core.watcher.support.xcontent.WatcherParams; import org.elasticsearch.xpack.core.watcher.transport.actions.execute.ExecuteWatchAction; @@ -43,23 +43,29 @@ public class RestExecuteWatchAction extends BaseRestHandler implements RestRequestFilter { - private static final List RESERVED_FIELD_NAMES = asList(WatchField.TRIGGER.getPreferredName(), - WatchField.INPUT.getPreferredName(), WatchField.CONDITION.getPreferredName(), - WatchField.ACTIONS.getPreferredName(), WatchField.TRANSFORM.getPreferredName(), - WatchField.THROTTLE_PERIOD.getPreferredName(), WatchField.THROTTLE_PERIOD_HUMAN.getPreferredName(), - WatchField.METADATA.getPreferredName(), WatchField.STATUS.getPreferredName()); + private static final List RESERVED_FIELD_NAMES = asList( + WatchField.TRIGGER.getPreferredName(), + WatchField.INPUT.getPreferredName(), + WatchField.CONDITION.getPreferredName(), + WatchField.ACTIONS.getPreferredName(), + WatchField.TRANSFORM.getPreferredName(), + WatchField.THROTTLE_PERIOD.getPreferredName(), + WatchField.THROTTLE_PERIOD_HUMAN.getPreferredName(), + WatchField.METADATA.getPreferredName(), + WatchField.STATUS.getPreferredName() + ); @Override public List routes() { return List.of( Route.builder(POST, "/_watcher/watch/{id}/_execute") - .replaces(POST, "/_xpack/watcher/watch/{id}/_execute", RestApiVersion.V_7).build(), + .replaces(POST, "/_xpack/watcher/watch/{id}/_execute", RestApiVersion.V_7) + .build(), Route.builder(PUT, "/_watcher/watch/{id}/_execute") - .replaces(PUT, "/_xpack/watcher/watch/{id}/_execute", RestApiVersion.V_7).build(), - Route.builder(POST, "/_watcher/watch/_execute") - .replaces(POST, "/_xpack/watcher/watch/_execute", RestApiVersion.V_7).build(), - Route.builder(PUT, "/_watcher/watch/_execute") - .replaces(PUT, "/_xpack/watcher/watch/_execute", RestApiVersion.V_7).build() + .replaces(PUT, "/_xpack/watcher/watch/{id}/_execute", RestApiVersion.V_7) + .build(), + Route.builder(POST, "/_watcher/watch/_execute").replaces(POST, "/_xpack/watcher/watch/_execute", RestApiVersion.V_7).build(), + Route.builder(PUT, "/_watcher/watch/_execute").replaces(PUT, "/_xpack/watcher/watch/_execute", RestApiVersion.V_7).build() ); } @@ -84,7 +90,7 @@ public RestResponse buildResponse(ExecuteWatchResponse response, XContentBuilder }); } - //This tightly binds the REST API to the java API. pkg private for testing + // This tightly binds the REST API to the java API. pkg private for testing static ExecuteWatchRequest parseRequest(RestRequest request, NodeClient client) throws IOException { ExecuteWatchRequestBuilder builder = new ExecuteWatchRequestBuilder(client); builder.setId(request.param("id")); @@ -111,8 +117,10 @@ static ExecuteWatchRequest parseRequest(RestRequest request, NodeClient client) } else if (RECORD_EXECUTION.match(currentFieldName, parser.getDeprecationHandler())) { builder.setRecordExecution(parser.booleanValue()); } else { - throw new ElasticsearchParseException("could not parse watch execution request. unexpected boolean field [{}]", - currentFieldName); + throw new ElasticsearchParseException( + "could not parse watch execution request. unexpected boolean field [{}]", + currentFieldName + ); } } else if (token == XContentParser.Token.START_OBJECT) { if (Field.ALTERNATIVE_INPUT.match(currentFieldName, parser.getDeprecationHandler())) { @@ -137,17 +145,22 @@ static ExecuteWatchRequest parseRequest(RestRequest request, NodeClient client) } } else { throw new ElasticsearchParseException( - "could not parse watch execution request. unexpected array field [{}]", - currentFieldName); + "could not parse watch execution request. unexpected array field [{}]", + currentFieldName + ); } } } else { if (RESERVED_FIELD_NAMES.contains(currentFieldName)) { - throw new ElasticsearchParseException("please wrap watch including field [{}] inside a \"watch\" field", - currentFieldName); + throw new ElasticsearchParseException( + "please wrap watch including field [{}] inside a \"watch\" field", + currentFieldName + ); } else { - throw new ElasticsearchParseException("could not parse watch execution request. unexpected object field [{}]", - currentFieldName); + throw new ElasticsearchParseException( + "could not parse watch execution request. unexpected object field [{}]", + currentFieldName + ); } } } else { @@ -160,10 +173,13 @@ static ExecuteWatchRequest parseRequest(RestRequest request, NodeClient client) } private static final Set FILTERED_FIELDS = Collections.unmodifiableSet( - Sets.newHashSet("watch.input.http.request.auth.basic.password", - "watch.input.chain.inputs.*.http.request.auth.basic.password", - "watch.actions.*.email.attachments.*.reporting.auth.basic.password", - "watch.actions.*.webhook.auth.basic.password")); + Sets.newHashSet( + "watch.input.http.request.auth.basic.password", + "watch.input.chain.inputs.*.http.request.auth.basic.password", + "watch.actions.*.email.attachments.*.reporting.auth.basic.password", + "watch.actions.*.webhook.auth.basic.password" + ) + ); @Override public Set getFilteredFields() { diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/rest/action/RestGetWatchAction.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/rest/action/RestGetWatchAction.java index ccbf7f7acc720..d652af879ac42 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/rest/action/RestGetWatchAction.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/rest/action/RestGetWatchAction.java @@ -9,13 +9,13 @@ import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.core.RestApiVersion; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.watcher.transport.actions.get.GetWatchAction; import org.elasticsearch.xpack.core.watcher.transport.actions.get.GetWatchRequest; import org.elasticsearch.xpack.core.watcher.transport.actions.get.GetWatchResponse; @@ -30,10 +30,7 @@ public class RestGetWatchAction extends BaseRestHandler { @Override public List routes() { - return List.of( - Route.builder(GET, "/_watcher/watch/{id}") - .replaces(GET, "/_xpack/watcher/watch/{id}", RestApiVersion.V_7).build() - ); + return List.of(Route.builder(GET, "/_watcher/watch/{id}").replaces(GET, "/_xpack/watcher/watch/{id}", RestApiVersion.V_7).build()); } @Override diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/rest/action/RestPutWatchAction.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/rest/action/RestPutWatchAction.java index 05b1ea2af5b07..9aa4a08c2dc9b 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/rest/action/RestPutWatchAction.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/rest/action/RestPutWatchAction.java @@ -8,19 +8,19 @@ package org.elasticsearch.xpack.watcher.rest.action; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.common.lucene.uid.Versions; import org.elasticsearch.common.util.set.Sets; -import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest; import org.elasticsearch.protocol.xpack.watcher.PutWatchResponse; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestRequest; +import org.elasticsearch.rest.RestRequestFilter; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestBuilderListener; -import org.elasticsearch.rest.RestRequestFilter; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.watcher.transport.actions.put.PutWatchAction; import java.util.Collections; @@ -37,10 +37,8 @@ public class RestPutWatchAction extends BaseRestHandler implements RestRequestFi @Override public List routes() { return List.of( - Route.builder(POST, "/_watcher/watch/{id}") - .replaces(POST, "/_xpack/watcher/watch/{id}", RestApiVersion.V_7).build(), - Route.builder(PUT, "/_watcher/watch/{id}") - .replaces(PUT, "/_xpack/watcher/watch/{id}", RestApiVersion.V_7).build() + Route.builder(POST, "/_watcher/watch/{id}").replaces(POST, "/_xpack/watcher/watch/{id}", RestApiVersion.V_7).build(), + Route.builder(PUT, "/_watcher/watch/{id}").replaces(PUT, "/_xpack/watcher/watch/{id}", RestApiVersion.V_7).build() ); } @@ -51,8 +49,7 @@ public String getName() { @Override protected RestChannelConsumer prepareRequest(final RestRequest request, NodeClient client) { - PutWatchRequest putWatchRequest = - new PutWatchRequest(request.param("id"), request.content(), request.getXContentType()); + PutWatchRequest putWatchRequest = new PutWatchRequest(request.param("id"), request.content(), request.getXContentType()); putWatchRequest.setVersion(request.paramAsLong("version", Versions.MATCH_ANY)); putWatchRequest.setIfSeqNo(request.paramAsLong("if_seq_no", putWatchRequest.getIfSeqNo())); putWatchRequest.setIfPrimaryTerm(request.paramAsLong("if_primary_term", putWatchRequest.getIfPrimaryTerm())); @@ -68,8 +65,13 @@ public RestResponse buildResponse(PutWatchResponse response, XContentBuilder bui } private static final Set FILTERED_FIELDS = Collections.unmodifiableSet( - Sets.newHashSet("input.http.request.auth.basic.password", "input.chain.inputs.*.http.request.auth.basic.password", - "actions.*.email.attachments.*.reporting.auth.basic.password", "actions.*.webhook.auth.basic.password")); + Sets.newHashSet( + "input.http.request.auth.basic.password", + "input.chain.inputs.*.http.request.auth.basic.password", + "actions.*.email.attachments.*.reporting.auth.basic.password", + "actions.*.webhook.auth.basic.password" + ) + ); @Override public Set getFilteredFields() { diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/rest/action/RestQueryWatchesAction.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/rest/action/RestQueryWatchesAction.java index 312e946682f6f..a8b28e15e208b 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/rest/action/RestQueryWatchesAction.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/rest/action/RestQueryWatchesAction.java @@ -22,10 +22,7 @@ public class RestQueryWatchesAction extends BaseRestHandler { @Override public List routes() { - return List.of( - new Route(GET, "/_watcher/_query/watches"), - new Route(POST, "/_watcher/_query/watches") - ); + return List.of(new Route(GET, "/_watcher/_query/watches"), new Route(POST, "/_watcher/_query/watches")); } @Override diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/rest/action/RestWatchServiceAction.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/rest/action/RestWatchServiceAction.java index 737a4a2238902..968a8084daf53 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/rest/action/RestWatchServiceAction.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/rest/action/RestWatchServiceAction.java @@ -23,10 +23,7 @@ public class RestWatchServiceAction extends BaseRestHandler { @Override public List routes() { - return List.of( - Route.builder(POST, "/_watcher/_start") - .replaces(POST, "/_xpack/watcher/_start", RestApiVersion.V_7).build() - ); + return List.of(Route.builder(POST, "/_watcher/_start").replaces(POST, "/_xpack/watcher/_start", RestApiVersion.V_7).build()); } @Override @@ -36,18 +33,18 @@ public String getName() { @Override public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) { - return channel -> - client.execute(WatcherServiceAction.INSTANCE, new WatcherServiceRequest().start(), new RestToXContentListener<>(channel)); + return channel -> client.execute( + WatcherServiceAction.INSTANCE, + new WatcherServiceRequest().start(), + new RestToXContentListener<>(channel) + ); } public static class StopRestHandler extends BaseRestHandler { @Override public List routes() { - return List.of( - Route.builder(POST, "/_watcher/_stop") - .replaces(POST, "/_xpack/watcher/_stop", RestApiVersion.V_7).build() - ); + return List.of(Route.builder(POST, "/_watcher/_stop").replaces(POST, "/_xpack/watcher/_stop", RestApiVersion.V_7).build()); } @Override diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/rest/action/RestWatcherStatsAction.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/rest/action/RestWatcherStatsAction.java index 6f18d2b98304f..42dfc958f97a8 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/rest/action/RestWatcherStatsAction.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/rest/action/RestWatcherStatsAction.java @@ -8,10 +8,10 @@ package org.elasticsearch.xpack.watcher.rest.action; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.common.Strings; import org.elasticsearch.common.logging.DeprecationCategory; import org.elasticsearch.common.logging.DeprecationLogger; +import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.action.RestActions; @@ -30,10 +30,8 @@ public class RestWatcherStatsAction extends BaseRestHandler { @Override public List routes() { return List.of( - Route.builder(GET, "/_watcher/stats") - .replaces(GET, "/_xpack/watcher/stats", RestApiVersion.V_7).build(), - Route.builder(GET, "/_watcher/stats/{metric}") - .replaces(GET, "/_xpack/watcher/stats/{metric}", RestApiVersion.V_7).build() + Route.builder(GET, "/_watcher/stats").replaces(GET, "/_xpack/watcher/stats", RestApiVersion.V_7).build(), + Route.builder(GET, "/_watcher/stats/{metric}").replaces(GET, "/_xpack/watcher/stats/{metric}", RestApiVersion.V_7).build() ); } @@ -56,11 +54,13 @@ protected RestChannelConsumer prepareRequest(final RestRequest restRequest, Node } if (metrics.contains("pending_watches")) { - deprecationLogger.critical(DeprecationCategory.API, "pending_watches", - "The pending_watches parameter is deprecated, use queued_watches instead"); + deprecationLogger.critical( + DeprecationCategory.API, + "pending_watches", + "The pending_watches parameter is deprecated, use queued_watches instead" + ); } - return channel -> client.execute(WatcherStatsAction.INSTANCE, request, new RestActions.NodesResponseRestListener<>(channel)); } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/support/Strings.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/support/Strings.java index 727657a545dd2..15cb993cd4882 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/support/Strings.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/support/Strings.java @@ -9,8 +9,7 @@ import java.util.Objects; public class Strings { - private Strings() { - } + private Strings() {} public static String join(String delimiter, int... values) { Objects.requireNonNull(delimiter); diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/support/WatcherIndexTemplateRegistry.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/support/WatcherIndexTemplateRegistry.java index e9e3936c6ca76..e3e29cd5ccb82 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/support/WatcherIndexTemplateRegistry.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/support/WatcherIndexTemplateRegistry.java @@ -10,8 +10,8 @@ import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.template.IndexTemplateConfig; import org.elasticsearch.xpack.core.template.IndexTemplateRegistry; import org.elasticsearch.xpack.core.template.LifecyclePolicyConfig; @@ -30,24 +30,34 @@ public class WatcherIndexTemplateRegistry extends IndexTemplateRegistry { WatcherIndexTemplateRegistryField.HISTORY_TEMPLATE_NAME, "/watch-history.json", WatcherIndexTemplateRegistryField.INDEX_TEMPLATE_VERSION, - WATCHER_TEMPLATE_VERSION_VARIABLE); + WATCHER_TEMPLATE_VERSION_VARIABLE + ); public static final IndexTemplateConfig TEMPLATE_CONFIG_WATCH_HISTORY_NO_ILM = new IndexTemplateConfig( WatcherIndexTemplateRegistryField.HISTORY_TEMPLATE_NAME_NO_ILM, "/watch-history-no-ilm.json", WatcherIndexTemplateRegistryField.INDEX_TEMPLATE_VERSION, - WATCHER_TEMPLATE_VERSION_VARIABLE); + WATCHER_TEMPLATE_VERSION_VARIABLE + ); - public static final LifecyclePolicyConfig POLICY_WATCH_HISTORY = new LifecyclePolicyConfig("watch-history-ilm-policy", - "/watch-history-ilm-policy.json"); + public static final LifecyclePolicyConfig POLICY_WATCH_HISTORY = new LifecyclePolicyConfig( + "watch-history-ilm-policy", + "/watch-history-ilm-policy.json" + ); private final List templatesToUse; - public WatcherIndexTemplateRegistry(Settings nodeSettings, ClusterService clusterService, ThreadPool threadPool, Client client, - NamedXContentRegistry xContentRegistry) { + public WatcherIndexTemplateRegistry( + Settings nodeSettings, + ClusterService clusterService, + ThreadPool threadPool, + Client client, + NamedXContentRegistry xContentRegistry + ) { super(nodeSettings, clusterService, threadPool, client, xContentRegistry); boolean ilmManagementEnabled = Watcher.USE_ILM_INDEX_MANAGEMENT.get(nodeSettings); - templatesToUse = Collections.singletonList(ilmManagementEnabled ? TEMPLATE_CONFIG_WATCH_HISTORY : - TEMPLATE_CONFIG_WATCH_HISTORY_NO_ILM); + templatesToUse = Collections.singletonList( + ilmManagementEnabled ? TEMPLATE_CONFIG_WATCH_HISTORY : TEMPLATE_CONFIG_WATCH_HISTORY_NO_ILM + ); } @Override @@ -72,13 +82,14 @@ protected String getOrigin() { } public static boolean validate(ClusterState state) { - return state.getMetadata().templatesV2().containsKey(WatcherIndexTemplateRegistryField.HISTORY_TEMPLATE_NAME) || - state.getMetadata().templatesV2().containsKey(WatcherIndexTemplateRegistryField.HISTORY_TEMPLATE_NAME_NO_ILM) || + return state.getMetadata().templatesV2().containsKey(WatcherIndexTemplateRegistryField.HISTORY_TEMPLATE_NAME) + || state.getMetadata().templatesV2().containsKey(WatcherIndexTemplateRegistryField.HISTORY_TEMPLATE_NAME_NO_ILM) + || // Template versions 12 or 13 are also ok to have (no breaking changes). At some point these will be upgraded to version 14. - state.getMetadata().templatesV2().containsKey(".watch-history-12") || - state.getMetadata().templatesV2().containsKey(".watch-history-no-ilm-12") || - state.getMetadata().templatesV2().containsKey(".watch-history-13") || - state.getMetadata().templatesV2().containsKey(".watch-history-no-ilm-13"); + state.getMetadata().templatesV2().containsKey(".watch-history-12") + || state.getMetadata().templatesV2().containsKey(".watch-history-no-ilm-12") + || state.getMetadata().templatesV2().containsKey(".watch-history-13") + || state.getMetadata().templatesV2().containsKey(".watch-history-no-ilm-13"); } @Override diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/support/XContentFilterKeysUtils.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/support/XContentFilterKeysUtils.java index ca8a446a85c72..5d90115c33d22 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/support/XContentFilterKeysUtils.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/support/XContentFilterKeysUtils.java @@ -21,8 +21,7 @@ public final class XContentFilterKeysUtils { - private XContentFilterKeysUtils() { - } + private XContentFilterKeysUtils() {} public static Map filterMapOrdered(Set keys, XContentParser parser) throws IOException { try { diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/support/search/WatcherSearchTemplateRequest.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/support/search/WatcherSearchTemplateRequest.java index 0dfcbe7bc022d..77dc361f4c5ab 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/support/search/WatcherSearchTemplateRequest.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/support/search/WatcherSearchTemplateRequest.java @@ -9,17 +9,17 @@ import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.action.search.SearchType; import org.elasticsearch.action.support.IndicesOptions; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.script.Script; +import org.elasticsearch.script.ScriptType; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.script.Script; -import org.elasticsearch.script.ScriptType; import java.io.IOException; import java.io.InputStream; @@ -43,8 +43,12 @@ public class WatcherSearchTemplateRequest implements ToXContentObject { private final BytesReference searchSource; private boolean restTotalHitsAsInt = true; - public WatcherSearchTemplateRequest(String[] indices, SearchType searchType, IndicesOptions indicesOptions, - BytesReference searchSource) { + public WatcherSearchTemplateRequest( + String[] indices, + SearchType searchType, + IndicesOptions indicesOptions, + BytesReference searchSource + ) { this.indices = indices; this.searchType = searchType; this.indicesOptions = indicesOptions; @@ -54,8 +58,7 @@ public WatcherSearchTemplateRequest(String[] indices, SearchType searchType, Ind this.searchSource = BytesArray.EMPTY; } - public WatcherSearchTemplateRequest(String[] indices, SearchType searchType, IndicesOptions indicesOptions, - Script template) { + public WatcherSearchTemplateRequest(String[] indices, SearchType searchType, IndicesOptions indicesOptions, Script template) { this.indices = indices; this.searchType = searchType; this.indicesOptions = indicesOptions; @@ -72,8 +75,13 @@ public WatcherSearchTemplateRequest(WatcherSearchTemplateRequest original, Bytes this.restTotalHitsAsInt = original.restTotalHitsAsInt; } - private WatcherSearchTemplateRequest(String[] indices, SearchType searchType, IndicesOptions indicesOptions, - BytesReference searchSource, Script template) { + private WatcherSearchTemplateRequest( + String[] indices, + SearchType searchType, + IndicesOptions indicesOptions, + BytesReference searchSource, + Script template + ) { this.indices = indices; this.searchType = searchType; this.indicesOptions = indicesOptions; @@ -151,7 +159,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws return builder.endObject(); } - /** * Reads a new watcher search request instance for the specified parser. */ @@ -174,13 +181,19 @@ public static WatcherSearchTemplateRequest fromXContent(XContentParser parser, S if (token == XContentParser.Token.VALUE_STRING) { indices.add(parser.textOrNull()); } else { - throw new ElasticsearchParseException("could not read search request. expected string values in [" + - currentFieldName + "] field, but instead found [" + token + "]"); + throw new ElasticsearchParseException( + "could not read search request. expected string values in [" + + currentFieldName + + "] field, but instead found [" + + token + + "]" + ); } } } else { - throw new ElasticsearchParseException("could not read search request. unexpected array field [" + - currentFieldName + "]"); + throw new ElasticsearchParseException( + "could not read search request. unexpected array field [" + currentFieldName + "]" + ); } } else if (token == XContentParser.Token.START_OBJECT) { if (BODY_FIELD.match(currentFieldName, parser.getDeprecationHandler())) { @@ -193,8 +206,9 @@ public static WatcherSearchTemplateRequest fromXContent(XContentParser parser, S } else if (TEMPLATE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) { template = Script.parse(parser, Script.DEFAULT_TEMPLATE_LANG); } else { - throw new ElasticsearchParseException("could not read search request. unexpected object field [" + - currentFieldName + "]"); + throw new ElasticsearchParseException( + "could not read search request. unexpected object field [" + currentFieldName + "]" + ); } } else if (token == XContentParser.Token.VALUE_STRING) { if (INDICES_FIELD.match(currentFieldName, parser.getDeprecationHandler())) { @@ -205,15 +219,17 @@ public static WatcherSearchTemplateRequest fromXContent(XContentParser parser, S } else if (REST_TOTAL_HITS_AS_INT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) { totalHitsAsInt = parser.booleanValue(); } else { - throw new ElasticsearchParseException("could not read search request. unexpected string field [" + - currentFieldName + "]"); + throw new ElasticsearchParseException( + "could not read search request. unexpected string field [" + currentFieldName + "]" + ); } } else if (token == XContentParser.Token.VALUE_BOOLEAN) { if (REST_TOTAL_HITS_AS_INT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) { totalHitsAsInt = parser.booleanValue(); } else { - throw new ElasticsearchParseException("could not read search request. unexpected boolean field [" + - currentFieldName + "]"); + throw new ElasticsearchParseException( + "could not read search request. unexpected boolean field [" + currentFieldName + "]" + ); } } else { throw new ElasticsearchParseException("could not read search request. unexpected token [" + token + "]"); @@ -224,8 +240,13 @@ public static WatcherSearchTemplateRequest fromXContent(XContentParser parser, S searchSource = BytesArray.EMPTY; } - WatcherSearchTemplateRequest request = new WatcherSearchTemplateRequest(indices.toArray(new String[0]), - searchType, indicesOptions, searchSource, template); + WatcherSearchTemplateRequest request = new WatcherSearchTemplateRequest( + indices.toArray(new String[0]), + searchType, + indicesOptions, + searchSource, + template + ); request.setRestTotalHitsAsInt(totalHitsAsInt); return request; } @@ -236,12 +257,12 @@ public boolean equals(Object o) { if (o == null || getClass() != o.getClass()) return false; WatcherSearchTemplateRequest other = (WatcherSearchTemplateRequest) o; - return Arrays.equals(indices, other.indices) && - Objects.equals(searchType, other.searchType) && - Objects.equals(indicesOptions, other.indicesOptions) && - Objects.equals(searchSource, other.searchSource) && - Objects.equals(template, other.template) && - Objects.equals(restTotalHitsAsInt, other.restTotalHitsAsInt); + return Arrays.equals(indices, other.indices) + && Objects.equals(searchType, other.searchType) + && Objects.equals(indicesOptions, other.indicesOptions) + && Objects.equals(searchSource, other.searchSource) + && Objects.equals(template, other.template) + && Objects.equals(restTotalHitsAsInt, other.restTotalHitsAsInt); } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/support/search/WatcherSearchTemplateService.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/support/search/WatcherSearchTemplateService.java index 8b41499b4eb4a..7fb53e07b05be 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/support/search/WatcherSearchTemplateService.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/support/search/WatcherSearchTemplateService.java @@ -9,15 +9,15 @@ import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptService; import org.elasticsearch.script.ScriptType; import org.elasticsearch.script.TemplateScript; import org.elasticsearch.search.builder.SearchSourceBuilder; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext; import org.elasticsearch.xpack.core.watcher.watch.Payload; import org.elasticsearch.xpack.watcher.Watcher; @@ -50,8 +50,13 @@ public String renderTemplate(Script source, WatchExecutionContext ctx, Payload p watcherContextParams.putAll(source.getParams()); } // Templates are always of lang mustache: - Script template = new Script(source.getType(), source.getType() == ScriptType.STORED ? null : "mustache", - source.getIdOrCode(), source.getOptions(), watcherContextParams); + Script template = new Script( + source.getType(), + source.getType() == ScriptType.STORED ? null : "mustache", + source.getIdOrCode(), + source.getOptions(), + watcherContextParams + ); TemplateScript.Factory compiledTemplate = scriptService.compile(template, Watcher.SCRIPT_TEMPLATE_CONTEXT); return compiledTemplate.newInstance(template.getParams()).execute(); } @@ -63,9 +68,11 @@ public SearchRequest toSearchRequest(WatcherSearchTemplateRequest request) throw SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource(); BytesReference source = request.getSearchSource(); if (source != null && source.length() > 0) { - try (InputStream stream = source.streamInput(); - XContentParser parser = XContentFactory.xContent(XContentHelper.xContentType(source)) - .createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, stream)) { + try ( + InputStream stream = source.streamInput(); + XContentParser parser = XContentFactory.xContent(XContentHelper.xContentType(source)) + .createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, stream) + ) { sourceBuilder.parseXContent(parser); searchRequest.source(sourceBuilder); } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transform/TransformBuilders.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transform/TransformBuilders.java index 8971c2f8a77fa..d6e07751c87e0 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transform/TransformBuilders.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transform/TransformBuilders.java @@ -18,8 +18,7 @@ public final class TransformBuilders { - private TransformBuilders() { - } + private TransformBuilders() {} public static SearchTransform.Builder searchTransform(WatcherSearchTemplateRequest request) { return SearchTransform.builder(request); diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transform/script/ScriptTransform.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transform/script/ScriptTransform.java index 4c714bd18a48d..a788e3d137873 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transform/script/ScriptTransform.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transform/script/ScriptTransform.java @@ -7,9 +7,9 @@ package org.elasticsearch.xpack.watcher.transform.script; import org.elasticsearch.ElasticsearchParseException; +import org.elasticsearch.script.Script; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.script.Script; import org.elasticsearch.xpack.core.watcher.transform.Transform; import org.elasticsearch.xpack.core.watcher.watch.Payload; @@ -59,8 +59,12 @@ public static ScriptTransform parse(String watchId, XContentParser parser) throw Script script = Script.parse(parser); return new ScriptTransform(script); } catch (ElasticsearchParseException pe) { - throw new ElasticsearchParseException("could not parse [{}] transform for watch [{}]. failed to parse script", pe, TYPE, - watchId); + throw new ElasticsearchParseException( + "could not parse [{}] transform for watch [{}]. failed to parse script", + pe, + TYPE, + watchId + ); } } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transform/script/ScriptTransformFactory.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transform/script/ScriptTransformFactory.java index 40d9d34ef26f2..cbd63517d5f76 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transform/script/ScriptTransformFactory.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transform/script/ScriptTransformFactory.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.watcher.transform.script; import org.apache.logging.log4j.LogManager; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.script.ScriptService; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.watcher.transform.TransformFactory; import java.io.IOException; diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transform/script/WatcherTransformScript.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transform/script/WatcherTransformScript.java index 3a48e25635f1d..93e3eefb4aa26 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transform/script/WatcherTransformScript.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transform/script/WatcherTransformScript.java @@ -44,6 +44,12 @@ public interface Factory { WatcherTransformScript newInstance(Map params, WatchExecutionContext watcherContext, Payload payload); } - public static ScriptContext CONTEXT = new ScriptContext<>("watcher_transform", Factory.class, - 200, TimeValue.timeValueMillis(0), false, true); + public static ScriptContext CONTEXT = new ScriptContext<>( + "watcher_transform", + Factory.class, + 200, + TimeValue.timeValueMillis(0), + false, + true + ); } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transform/search/ExecutableSearchTransform.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transform/search/ExecutableSearchTransform.java index ed732c8c05de0..8b8f8f18993c9 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transform/search/ExecutableSearchTransform.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transform/search/ExecutableSearchTransform.java @@ -35,8 +35,13 @@ public class ExecutableSearchTransform extends ExecutableTransform client.search(searchRequest).actionGet(timeout)); + SearchResponse resp = ClientHelper.executeWithHeaders( + ctx.watch().status().getHeaders(), + ClientHelper.WATCHER_ORIGIN, + client, + () -> client.search(searchRequest).actionGet(timeout) + ); final Params params; if (request.isRestTotalHitsAsint()) { params = new MapParams(Collections.singletonMap("rest_total_hits_as_int", "true")); diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transform/search/SearchTransform.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transform/search/SearchTransform.java index d4c3716d7dd82..3a3ed04cd8d8e 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transform/search/SearchTransform.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transform/search/SearchTransform.java @@ -7,10 +7,10 @@ package org.elasticsearch.xpack.watcher.transform.search; import org.elasticsearch.ElasticsearchParseException; -import org.elasticsearch.core.Nullable; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.time.DateUtils; +import org.elasticsearch.core.Nullable; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.watcher.support.WatcherDateTimeUtils; @@ -29,8 +29,10 @@ public class SearchTransform implements Transform { public static final String TYPE = "search"; private final WatcherSearchTemplateRequest request; - @Nullable private final TimeValue timeout; - @Nullable private final ZoneId dynamicNameTimeZone; + @Nullable + private final TimeValue timeout; + @Nullable + private final ZoneId dynamicNameTimeZone; public SearchTransform(WatcherSearchTemplateRequest request, @Nullable TimeValue timeout, @Nullable ZoneId dynamicNameTimeZone) { this.request = request; @@ -107,8 +109,13 @@ public static SearchTransform parse(String watchId, XContentParser parser) throw try { request = WatcherSearchTemplateRequest.fromXContent(parser, ExecutableSearchTransform.DEFAULT_SEARCH_TYPE); } catch (ElasticsearchParseException srpe) { - throw new ElasticsearchParseException("could not parse [{}] transform for watch [{}]. failed to parse [{}]", srpe, - TYPE, watchId, currentFieldName); + throw new ElasticsearchParseException( + "could not parse [{}] transform for watch [{}]. failed to parse [{}]", + srpe, + TYPE, + watchId, + currentFieldName + ); } } else if (Field.TIMEOUT.match(currentFieldName, parser.getDeprecationHandler())) { timeout = timeValueMillis(parser.longValue()); @@ -119,18 +126,31 @@ public static SearchTransform parse(String watchId, XContentParser parser) throw if (token == XContentParser.Token.VALUE_STRING) { dynamicNameTimeZone = DateUtils.of(parser.text()); } else { - throw new ElasticsearchParseException("could not parse [{}] transform for watch [{}]. failed to parse [{}]. must be a" + - " string value (e.g. 'UTC' or '+01:00').", TYPE, watchId, currentFieldName); + throw new ElasticsearchParseException( + "could not parse [{}] transform for watch [{}]. failed to parse [{}]. must be a" + + " string value (e.g. 'UTC' or '+01:00').", + TYPE, + watchId, + currentFieldName + ); } } else { - throw new ElasticsearchParseException("could not parse [{}] transform for watch [{}]. unexpected field [{}]", TYPE, - watchId, currentFieldName); + throw new ElasticsearchParseException( + "could not parse [{}] transform for watch [{}]. unexpected field [{}]", + TYPE, + watchId, + currentFieldName + ); } } if (request == null) { - throw new ElasticsearchParseException("could not parse [{}] transform for watch [{}]. missing required [{}] field", TYPE, - watchId, Field.REQUEST.getPreferredName()); + throw new ElasticsearchParseException( + "could not parse [{}] transform for watch [{}]. missing required [{}] field", + TYPE, + watchId, + Field.REQUEST.getPreferredName() + ); } return new SearchTransform(request, timeout, dynamicNameTimeZone); } @@ -141,7 +161,8 @@ public static Builder builder(WatcherSearchTemplateRequest request) { public static class Result extends Transform.Result { - @Nullable private final WatcherSearchTemplateRequest request; + @Nullable + private final WatcherSearchTemplateRequest request; public Result(WatcherSearchTemplateRequest request, Payload payload) { super(TYPE, payload); diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transform/search/SearchTransformFactory.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transform/search/SearchTransformFactory.java index b7ae97a535ea6..f5e845ca1c684 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transform/search/SearchTransformFactory.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transform/search/SearchTransformFactory.java @@ -10,9 +10,9 @@ import org.elasticsearch.client.Client; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.script.ScriptService; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.script.ScriptService; import org.elasticsearch.xpack.core.watcher.transform.TransformFactory; import org.elasticsearch.xpack.watcher.support.search.WatcherSearchTemplateService; @@ -43,6 +43,6 @@ public SearchTransform parseTransform(String watchId, XContentParser parser) thr @Override public ExecutableSearchTransform createExecutable(SearchTransform transform) { - return new ExecutableSearchTransform(transform, transformLogger, client, searchTemplateService, defaultTimeout); + return new ExecutableSearchTransform(transform, transformLogger, client, searchTemplateService, defaultTimeout); } } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/TransportAckWatchAction.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/TransportAckWatchAction.java index 82a4e8a389e88..aa441db98c909 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/TransportAckWatchAction.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/TransportAckWatchAction.java @@ -18,12 +18,12 @@ import org.elasticsearch.client.Client; import org.elasticsearch.cluster.routing.Preference; import org.elasticsearch.common.inject.Inject; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.watcher.actions.ActionWrapper; import org.elasticsearch.xpack.core.watcher.transport.actions.ack.AckWatchAction; import org.elasticsearch.xpack.core.watcher.transport.actions.ack.AckWatchRequest; @@ -52,9 +52,14 @@ public class TransportAckWatchAction extends WatcherTransportAction { - boolean isWatchRunning = response.getNodes().stream() + boolean isWatchRunning = response.getNodes() + .stream() .anyMatch(node -> node.getSnapshots().stream().anyMatch(snapshot -> snapshot.watchId().equals(request.getWatchId()))); if (isWatchRunning) { - listener.onFailure(new ElasticsearchStatusException("watch[{}] is running currently, cannot ack until finished", - RestStatus.CONFLICT, request.getWatchId())); + listener.onFailure( + new ElasticsearchStatusException( + "watch[{}] is running currently, cannot ack until finished", + RestStatus.CONFLICT, + request.getWatchId() + ) + ); } else { - GetRequest getRequest = new GetRequest(Watch.INDEX, request.getWatchId()) - .preference(Preference.LOCAL.type()).realtime(true); + GetRequest getRequest = new GetRequest(Watch.INDEX, request.getWatchId()).preference(Preference.LOCAL.type()) + .realtime(true); - executeAsyncWithOrigin(client.threadPool().getThreadContext(), WATCHER_ORIGIN, getRequest, + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + WATCHER_ORIGIN, + getRequest, ActionListener.wrap(getResponse -> { if (getResponse.isExists() == false) { listener.onFailure(new ResourceNotFoundException("Watch with id [{}] does not exist", request.getWatchId())); } else { ZonedDateTime now = clock.instant().atZone(ZoneOffset.UTC); - Watch watch = parser.parseWithSecrets(request.getWatchId(), true, getResponse.getSourceAsBytesRef(), - now, XContentType.JSON, getResponse.getSeqNo(), getResponse.getPrimaryTerm()); + Watch watch = parser.parseWithSecrets( + request.getWatchId(), + true, + getResponse.getSourceAsBytesRef(), + now, + XContentType.JSON, + getResponse.getSeqNo(), + getResponse.getPrimaryTerm() + ); watch.status().version(getResponse.getVersion()); String[] actionIds = request.getActionIds(); if (actionIds == null || actionIds.length == 0) { - actionIds = new String[]{WatchField.ALL_ACTIONS_ID}; + actionIds = new String[] { WatchField.ALL_ACTIONS_ID }; } // exit early in case nothing changes @@ -103,9 +124,7 @@ protected void doExecute(AckWatchRequest request, ActionListener actionIdsAsList = Arrays.asList(actionIds); boolean updateAll = actionIdsAsList.contains("_all"); @@ -120,12 +139,20 @@ protected void doExecute(AckWatchRequest request, ActionListenerwrap( (updateResponse) -> listener.onResponse(new AckWatchResponse(watch.status())), - listener::onFailure), client::update); + listener::onFailure + ), + client::update + ); } - }, listener::onFailure), client::get); + }, listener::onFailure), + client::get + ); } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/TransportActivateWatchAction.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/TransportActivateWatchAction.java index 8f41a6ba67622..973f5cccf4692 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/TransportActivateWatchAction.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/TransportActivateWatchAction.java @@ -17,10 +17,10 @@ import org.elasticsearch.client.Client; import org.elasticsearch.cluster.routing.Preference; import org.elasticsearch.common.inject.Inject; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.watcher.transport.actions.activate.ActivateWatchAction; import org.elasticsearch.xpack.core.watcher.transport.actions.activate.ActivateWatchRequest; import org.elasticsearch.xpack.core.watcher.transport.actions.activate.ActivateWatchResponse; @@ -50,8 +50,14 @@ public class TransportActivateWatchAction extends WatcherTransportActionwrap(updateResponse -> { - GetRequest getRequest = new GetRequest(Watch.INDEX, request.getWatchId()) - .preference(Preference.LOCAL.type()).realtime(true); + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + WATCHER_ORIGIN, + updateRequest, + ActionListener.wrap(updateResponse -> { + GetRequest getRequest = new GetRequest(Watch.INDEX, request.getWatchId()).preference(Preference.LOCAL.type()) + .realtime(true); - executeAsyncWithOrigin(client.threadPool().getThreadContext(), WATCHER_ORIGIN, getRequest, + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + WATCHER_ORIGIN, + getRequest, ActionListener.wrap(getResponse -> { if (getResponse.isExists()) { - Watch watch = parser.parseWithSecrets(request.getWatchId(), true, getResponse.getSourceAsBytesRef(), now, - XContentType.JSON, getResponse.getSeqNo(), getResponse.getPrimaryTerm()); + Watch watch = parser.parseWithSecrets( + request.getWatchId(), + true, + getResponse.getSourceAsBytesRef(), + now, + XContentType.JSON, + getResponse.getSeqNo(), + getResponse.getPrimaryTerm() + ); watch.status().version(getResponse.getVersion()); listener.onResponse(new ActivateWatchResponse(watch.status())); } else { - listener.onFailure(new ResourceNotFoundException("Watch with id [{}] does not exist", - request.getWatchId())); + listener.onFailure( + new ResourceNotFoundException("Watch with id [{}] does not exist", request.getWatchId()) + ); } - }, listener::onFailure), client::get); - }, listener::onFailure), client::update); + }, listener::onFailure), + client::get + ); + }, listener::onFailure), + client::update + ); } catch (IOException e) { listener.onFailure(e); } @@ -97,9 +121,9 @@ protected void doExecute(ActivateWatchRequest request, ActionListener listener) { DeleteRequest deleteRequest = new DeleteRequest(Watch.INDEX, request.getId()); deleteRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - executeAsyncWithOrigin(client.threadPool().getThreadContext(), WATCHER_ORIGIN, deleteRequest, - ActionListener.wrap(deleteResponse -> { - boolean deleted = deleteResponse.getResult() == DocWriteResponse.Result.DELETED; - DeleteWatchResponse response = new DeleteWatchResponse(deleteResponse.getId(), deleteResponse.getVersion(), deleted); - listener.onResponse(response); - }, listener::onFailure), client::delete); + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + WATCHER_ORIGIN, + deleteRequest, + ActionListener.wrap(deleteResponse -> { + boolean deleted = deleteResponse.getResult() == DocWriteResponse.Result.DELETED; + DeleteWatchResponse response = new DeleteWatchResponse(deleteResponse.getId(), deleteResponse.getVersion(), deleted); + listener.onResponse(response); + }, listener::onFailure), + client::delete + ); } } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/TransportExecuteWatchAction.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/TransportExecuteWatchAction.java index 0479718965699..bb8f7461e9727 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/TransportExecuteWatchAction.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/TransportExecuteWatchAction.java @@ -17,13 +17,13 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.util.concurrent.AbstractRunnable; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.seqno.SequenceNumbers; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.ClientHelper; import org.elasticsearch.xpack.core.XPackField; import org.elasticsearch.xpack.core.watcher.execution.ActionExecutionMode; @@ -67,10 +67,17 @@ public class TransportExecuteWatchAction extends WatcherTransportAction listener) { if (request.getId() != null) { - GetRequest getRequest = new GetRequest(Watch.INDEX, request.getId()) - .preference(Preference.LOCAL.type()).realtime(true); - - executeAsyncWithOrigin(client.threadPool().getThreadContext(), WATCHER_ORIGIN, getRequest, - ActionListener.wrap(response -> { - if (response.isExists()) { - Watch watch = watchParser.parse(request.getId(), true, response.getSourceAsBytesRef(), - request.getXContentType(), response.getSeqNo(), response.getPrimaryTerm()); - watch.status().version(response.getVersion()); - executeWatch(request, listener, watch, true); - } else { - listener.onFailure(new ResourceNotFoundException("Watch with id [{}] does not exist", request.getId())); - } - }, listener::onFailure), client::get); + GetRequest getRequest = new GetRequest(Watch.INDEX, request.getId()).preference(Preference.LOCAL.type()).realtime(true); + + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + WATCHER_ORIGIN, + getRequest, + ActionListener.wrap(response -> { + if (response.isExists()) { + Watch watch = watchParser.parse( + request.getId(), + true, + response.getSourceAsBytesRef(), + request.getXContentType(), + response.getSeqNo(), + response.getPrimaryTerm() + ); + watch.status().version(response.getVersion()); + executeWatch(request, listener, watch, true); + } else { + listener.onFailure(new ResourceNotFoundException("Watch with id [{}] does not exist", request.getId())); + } + }, listener::onFailure), + client::get + ); } else if (request.getWatchSource() != null) { try { assert request.isRecordExecution() == false; - Watch watch = watchParser.parse(ExecuteWatchRequest.INLINE_WATCH_ID, true, request.getWatchSource(), - request.getXContentType(), SequenceNumbers.UNASSIGNED_SEQ_NO, SequenceNumbers.UNASSIGNED_PRIMARY_TERM); + Watch watch = watchParser.parse( + ExecuteWatchRequest.INLINE_WATCH_ID, + true, + request.getWatchSource(), + request.getXContentType(), + SequenceNumbers.UNASSIGNED_SEQ_NO, + SequenceNumbers.UNASSIGNED_PRIMARY_TERM + ); executeWatch(request, listener, watch, false); } catch (IOException e) { logger.error(new ParameterizedMessage("failed to parse [{}]", request.getId()), e); @@ -113,10 +136,11 @@ protected void doExecute(ExecuteWatchRequest request, ActionListener listener, - final Watch watch, - final boolean knownWatch) { + final ExecuteWatchRequest request, + final ActionListener listener, + final Watch watch, + final boolean knownWatch + ) { try { /* * Ensure that the headers from the incoming request are used instead those of the stored watch otherwise the watch would run @@ -128,9 +152,11 @@ private void executeWatch( final TriggerEvent triggerEvent = triggerService.simulateEvent(triggerType, watch.id(), request.getTriggerData()); final ManualExecutionContext.Builder ctxBuilder = ManualExecutionContext.builder( - watch, - knownWatch, - new ManualTriggerEvent(triggerEvent.jobName(), triggerEvent), executionService.defaultThrottlePeriod()); + watch, + knownWatch, + new ManualTriggerEvent(triggerEvent.jobName(), triggerEvent), + executionService.defaultThrottlePeriod() + ); final ZonedDateTime executionTime = clock.instant().atZone(ZoneOffset.UTC); ctxBuilder.executionTime(executionTime); @@ -168,6 +194,5 @@ protected void doRun() throws Exception { listener.onFailure(e); } - } } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/TransportGetWatchAction.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/TransportGetWatchAction.java index 84b2c05d89b48..8ea05582c56f2 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/TransportGetWatchAction.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/TransportGetWatchAction.java @@ -14,11 +14,11 @@ import org.elasticsearch.cluster.routing.Preference; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.inject.Inject; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.IndexNotFoundException; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.watcher.support.xcontent.WatcherParams; import org.elasticsearch.xpack.core.watcher.support.xcontent.XContentSource; import org.elasticsearch.xpack.core.watcher.transport.actions.get.GetWatchAction; @@ -43,8 +43,14 @@ public class TransportGetWatchAction extends WatcherTransportAction listener) { - GetRequest getRequest = new GetRequest(Watch.INDEX, request.getId()) - .preference(Preference.LOCAL.type()).realtime(true); + GetRequest getRequest = new GetRequest(Watch.INDEX, request.getId()).preference(Preference.LOCAL.type()).realtime(true); - executeAsyncWithOrigin(client.threadPool().getThreadContext(), WATCHER_ORIGIN, getRequest, - ActionListener.wrap(getResponse -> { - if (getResponse.isExists()) { - try (XContentBuilder builder = jsonBuilder()) { - // When we return the watch via the Get Watch REST API, we want to return the watch as was specified in - // the put api, we don't include the status in the watch source itself, but as a separate top level field, - // so that it indicates the status is managed by watcher itself. - ZonedDateTime now = clock.instant().atZone(ZoneOffset.UTC); - Watch watch = parser.parseWithSecrets(request.getId(), true, getResponse.getSourceAsBytesRef(), now, - XContentType.JSON, getResponse.getSeqNo(), getResponse.getPrimaryTerm()); - watch.toXContent(builder, WatcherParams.builder() - .hideSecrets(true) - .includeStatus(false) - .build()); - watch.status().version(getResponse.getVersion()); - listener.onResponse(new GetWatchResponse(watch.id(), getResponse.getVersion(), - watch.getSourceSeqNo(), watch.getSourcePrimaryTerm(), - watch.status(), new XContentSource(BytesReference.bytes(builder), XContentType.JSON))); - } - } else { - listener.onResponse(new GetWatchResponse(request.getId())); - } - }, e -> { - // special case. This API should not care if the index is missing or not, - // it should respond with the watch not being found - if (e instanceof IndexNotFoundException) { - listener.onResponse(new GetWatchResponse(request.getId())); - } else { - listener.onFailure(e); + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + WATCHER_ORIGIN, + getRequest, + ActionListener.wrap(getResponse -> { + if (getResponse.isExists()) { + try (XContentBuilder builder = jsonBuilder()) { + // When we return the watch via the Get Watch REST API, we want to return the watch as was specified in + // the put api, we don't include the status in the watch source itself, but as a separate top level field, + // so that it indicates the status is managed by watcher itself. + ZonedDateTime now = clock.instant().atZone(ZoneOffset.UTC); + Watch watch = parser.parseWithSecrets( + request.getId(), + true, + getResponse.getSourceAsBytesRef(), + now, + XContentType.JSON, + getResponse.getSeqNo(), + getResponse.getPrimaryTerm() + ); + watch.toXContent(builder, WatcherParams.builder().hideSecrets(true).includeStatus(false).build()); + watch.status().version(getResponse.getVersion()); + listener.onResponse( + new GetWatchResponse( + watch.id(), + getResponse.getVersion(), + watch.getSourceSeqNo(), + watch.getSourcePrimaryTerm(), + watch.status(), + new XContentSource(BytesReference.bytes(builder), XContentType.JSON) + ) + ); } - }), client::get); + } else { + listener.onResponse(new GetWatchResponse(request.getId())); + } + }, e -> { + // special case. This API should not care if the index is missing or not, + // it should respond with the watch not being found + if (e instanceof IndexNotFoundException) { + listener.onResponse(new GetWatchResponse(request.getId())); + } else { + listener.onFailure(e); + } + }), + client::get + ); } } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/TransportPutWatchAction.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/TransportPutWatchAction.java index eea897a1352f4..59fb025e7884e 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/TransportPutWatchAction.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/TransportPutWatchAction.java @@ -16,14 +16,14 @@ import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.client.Client; import org.elasticsearch.common.inject.Inject; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.index.seqno.SequenceNumbers; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest; import org.elasticsearch.protocol.xpack.watcher.PutWatchResponse; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ClientHelper; import org.elasticsearch.xpack.core.watcher.support.xcontent.WatcherParams; import org.elasticsearch.xpack.core.watcher.transport.actions.put.PutWatchAction; @@ -61,12 +61,22 @@ public class TransportPutWatchAction extends WatcherTransportAction 0 || request.getIfSeqNo() != SequenceNumbers.UNASSIGNED_SEQ_NO; - Watch watch = parser.parseWithSecrets(request.getId(), false, request.getSource(), now, request.xContentType(), - isUpdate, request.getIfSeqNo(), request.getIfPrimaryTerm()); + Watch watch = parser.parseWithSecrets( + request.getId(), + false, + request.getSource(), + now, + request.xContentType(), + isUpdate, + request.getIfSeqNo(), + request.getIfPrimaryTerm() + ); watch.setState(request.isActive(), now); @@ -102,24 +120,46 @@ protected void doExecute(PutWatchRequest request, ActionListenerwrap(response -> { - boolean created = response.getResult() == DocWriteResponse.Result.CREATED; - listener.onResponse(new PutWatchResponse(response.getId(), response.getVersion(), - response.getSeqNo(), response.getPrimaryTerm(), created)); - }, listener::onFailure), - client::update); + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + WATCHER_ORIGIN, + updateRequest, + ActionListener.wrap(response -> { + boolean created = response.getResult() == DocWriteResponse.Result.CREATED; + listener.onResponse( + new PutWatchResponse( + response.getId(), + response.getVersion(), + response.getSeqNo(), + response.getPrimaryTerm(), + created + ) + ); + }, listener::onFailure), + client::update + ); } else { IndexRequest indexRequest = new IndexRequest(Watch.INDEX).id(request.getId()); indexRequest.source(builder); indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - executeAsyncWithOrigin(client.threadPool().getThreadContext(), WATCHER_ORIGIN, indexRequest, + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + WATCHER_ORIGIN, + indexRequest, ActionListener.wrap(response -> { boolean created = response.getResult() == DocWriteResponse.Result.CREATED; - listener.onResponse(new PutWatchResponse(response.getId(), response.getVersion(), - response.getSeqNo(), response.getPrimaryTerm(), created)); + listener.onResponse( + new PutWatchResponse( + response.getId(), + response.getVersion(), + response.getSeqNo(), + response.getPrimaryTerm(), + created + ) + ); }, listener::onFailure), - client::index); + client::index + ); } } } catch (Exception e) { diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/TransportQueryWatchesAction.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/TransportQueryWatchesAction.java index f122712e6fd34..ff5bad26ba2bc 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/TransportQueryWatchesAction.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/TransportQueryWatchesAction.java @@ -14,12 +14,12 @@ import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.client.Client; import org.elasticsearch.common.inject.Inject; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.sort.FieldSortBuilder; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.watcher.support.xcontent.WatcherParams; import org.elasticsearch.xpack.core.watcher.support.xcontent.XContentSource; import org.elasticsearch.xpack.core.watcher.transport.actions.QueryWatchesAction; @@ -47,8 +47,14 @@ public class TransportQueryWatchesAction extends WatcherTransportAction listener) { SearchRequest searchRequest = createSearchRequest(request); - executeAsyncWithOrigin(client.threadPool().getThreadContext(), WATCHER_ORIGIN, searchRequest, - ActionListener.wrap(r -> transformResponse(r, listener), listener::onFailure), client::search); + executeAsyncWithOrigin( + client.threadPool().getThreadContext(), + WATCHER_ORIGIN, + searchRequest, + ActionListener.wrap(r -> transformResponse(r, listener), listener::onFailure), + client::search + ); } SearchRequest createSearchRequest(QueryWatchesAction.Request request) { @@ -87,7 +98,6 @@ SearchRequest createSearchRequest(QueryWatchesAction.Request request) { return searchRequest; } - void transformResponse(SearchResponse searchResponse, ActionListener listener) { assert searchResponse.getHits().getTotalHits().relation == TotalHits.Relation.EQUAL_TO; List items = Arrays.stream(searchResponse.getHits().getHits()) @@ -99,14 +109,23 @@ void transformResponse(SearchResponse searchResponse, ActionListener listener) { + protected void masterOperation( + Task task, + WatcherServiceRequest request, + ClusterState state, + ActionListener listener + ) { final boolean manuallyStopped = request.getCommand() == WatcherServiceRequest.Command.STOP; final String source = manuallyStopped ? "update_watcher_manually_stopped" : "update_watcher_manually_started"; // TODO: make WatcherServiceRequest a real AckedRequest so that we have both a configurable timeout and master node timeout like - // we do elsewhere + // we do elsewhere clusterService.submitStateUpdateTask(source, new AckedClusterStateUpdateTask(new AckedRequest() { @Override public TimeValue ackTimeout() { @@ -75,16 +91,17 @@ public ClusterState execute(ClusterState clusterState) { return clusterState; } else { ClusterState.Builder builder = new ClusterState.Builder(clusterState); - builder.metadata(Metadata.builder(clusterState.getMetadata()) - .putCustom(WatcherMetadata.TYPE, newWatcherMetadata)); + builder.metadata(Metadata.builder(clusterState.getMetadata()).putCustom(WatcherMetadata.TYPE, newWatcherMetadata)); return builder.build(); } } @Override public void onFailure(String source, Exception e) { - logger.error(new ParameterizedMessage("could not update watcher stopped status to [{}], source [{}]", - manuallyStopped, source), e); + logger.error( + new ParameterizedMessage("could not update watcher stopped status to [{}], source [{}]", manuallyStopped, source), + e + ); listener.onFailure(e); } }); diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/TransportWatcherStatsAction.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/TransportWatcherStatsAction.java index 6497af8c1397a..f5d58928a12ea 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/TransportWatcherStatsAction.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/TransportWatcherStatsAction.java @@ -32,27 +32,48 @@ /** * Performs the stats operation. */ -public class TransportWatcherStatsAction extends TransportNodesAction { +public class TransportWatcherStatsAction extends TransportNodesAction< + WatcherStatsRequest, + WatcherStatsResponse, + WatcherStatsRequest.Node, + WatcherStatsResponse.Node> { private final ExecutionService executionService; private final TriggerService triggerService; private final WatcherLifeCycleService lifeCycleService; @Inject - public TransportWatcherStatsAction(TransportService transportService, ClusterService clusterService, - ThreadPool threadPool, ActionFilters actionFilters, WatcherLifeCycleService lifeCycleService, - ExecutionService executionService, TriggerService triggerService) { - super(WatcherStatsAction.NAME, threadPool, clusterService, transportService, actionFilters, - WatcherStatsRequest::new, WatcherStatsRequest.Node::new, ThreadPool.Names.MANAGEMENT, WatcherStatsResponse.Node.class); + public TransportWatcherStatsAction( + TransportService transportService, + ClusterService clusterService, + ThreadPool threadPool, + ActionFilters actionFilters, + WatcherLifeCycleService lifeCycleService, + ExecutionService executionService, + TriggerService triggerService + ) { + super( + WatcherStatsAction.NAME, + threadPool, + clusterService, + transportService, + actionFilters, + WatcherStatsRequest::new, + WatcherStatsRequest.Node::new, + ThreadPool.Names.MANAGEMENT, + WatcherStatsResponse.Node.class + ); this.lifeCycleService = lifeCycleService; this.executionService = executionService; this.triggerService = triggerService; } @Override - protected WatcherStatsResponse newResponse(WatcherStatsRequest request, List nodes, - List failures) { + protected WatcherStatsResponse newResponse( + WatcherStatsRequest request, + List nodes, + List failures + ) { return new WatcherStatsResponse(clusterService.getClusterName(), getWatcherMetadata(), nodes, failures); } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/WatcherTransportAction.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/WatcherTransportAction.java index ac200b2b61068..8967f90d13541 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/WatcherTransportAction.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/WatcherTransportAction.java @@ -20,13 +20,19 @@ import org.elasticsearch.xpack.core.XPackField; import org.elasticsearch.xpack.core.watcher.WatcherField; -abstract class WatcherTransportAction - extends HandledTransportAction { +abstract class WatcherTransportAction extends HandledTransportAction< + Request, + Response> { protected final XPackLicenseState licenseState; - WatcherTransportAction(String actionName, TransportService transportService, ActionFilters actionFilters, - XPackLicenseState licenseState, Writeable.Reader request) { + WatcherTransportAction( + String actionName, + TransportService transportService, + ActionFilters actionFilters, + XPackLicenseState licenseState, + Writeable.Reader request + ) { super(actionName, transportService, actionFilters, request); this.licenseState = licenseState; } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/TriggerBuilders.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/TriggerBuilders.java index f1ba18802b5cb..ae340183d3a09 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/TriggerBuilders.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/TriggerBuilders.java @@ -11,8 +11,7 @@ public final class TriggerBuilders { - private TriggerBuilders() { - } + private TriggerBuilders() {} public static ScheduleTrigger.Builder schedule(Schedule schedule) { return ScheduleTrigger.builder(schedule); diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/TriggerService.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/TriggerService.java index eb0fd4b69828e..7df8b6eb8d6c1 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/TriggerService.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/TriggerService.java @@ -182,20 +182,31 @@ public Trigger parseTrigger(String jobName, XContentParser parser) throws IOExce assert token == XContentParser.Token.START_OBJECT; token = parser.nextToken(); if (token != XContentParser.Token.FIELD_NAME) { - throw new ElasticsearchParseException("could not parse trigger for [{}]. expected trigger type string field, but found [{}]", - jobName, token); + throw new ElasticsearchParseException( + "could not parse trigger for [{}]. expected trigger type string field, but found [{}]", + jobName, + token + ); } String type = parser.currentName(); token = parser.nextToken(); if (token != XContentParser.Token.START_OBJECT) { - throw new ElasticsearchParseException("could not parse trigger [{}] for [{}]. expected trigger an object as the trigger body," + - " but found [{}]", type, jobName, token); + throw new ElasticsearchParseException( + "could not parse trigger [{}] for [{}]. expected trigger an object as the trigger body," + " but found [{}]", + type, + jobName, + token + ); } Trigger trigger = parseTrigger(jobName, type, parser); token = parser.nextToken(); if (token != XContentParser.Token.END_OBJECT) { - throw new ElasticsearchParseException("could not parse trigger [{}] for [{}]. expected [END_OBJECT] token, but found [{}]", - type, jobName, token); + throw new ElasticsearchParseException( + "could not parse trigger [{}] for [{}]. expected [END_OBJECT] token, but found [{}]", + type, + jobName, + token + ); } return trigger; } @@ -213,20 +224,33 @@ public TriggerEvent parseTriggerEvent(String watchId, String context, XContentPa assert token == XContentParser.Token.START_OBJECT; token = parser.nextToken(); if (token != XContentParser.Token.FIELD_NAME) { - throw new ElasticsearchParseException("could not parse trigger event for [{}] for watch [{}]. expected trigger type string " + - "field, but found [{}]", context, watchId, token); + throw new ElasticsearchParseException( + "could not parse trigger event for [{}] for watch [{}]. expected trigger type string " + "field, but found [{}]", + context, + watchId, + token + ); } String type = parser.currentName(); token = parser.nextToken(); if (token != XContentParser.Token.START_OBJECT) { - throw new ElasticsearchParseException("could not parse trigger event for [{}] for watch [{}]. expected trigger an object as " + - "the trigger body, but found [{}]", context, watchId, token); + throw new ElasticsearchParseException( + "could not parse trigger event for [{}] for watch [{}]. expected trigger an object as " + + "the trigger body, but found [{}]", + context, + watchId, + token + ); } TriggerEvent trigger = parseTriggerEvent(watchId, context, type, parser); token = parser.nextToken(); if (token != XContentParser.Token.END_OBJECT) { - throw new ElasticsearchParseException("could not parse trigger [{}] for [{}]. expected [END_OBJECT] token, but found [{}]", - type, context, token); + throw new ElasticsearchParseException( + "could not parse trigger [{}] for [{}]. expected [END_OBJECT] token, but found [{}]", + type, + context, + token + ); } return trigger; } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/TriggerWatchStats.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/TriggerWatchStats.java index ce32f54c42976..9028c6c6bd9db 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/TriggerWatchStats.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/TriggerWatchStats.java @@ -20,8 +20,15 @@ public class TriggerWatchStats { public final String transformType; public final ActionStats[] actions; - private TriggerWatchStats(boolean metadata, String triggerType, String scheduleType, String inputType, - String conditionType, String transformType, ActionStats[] actions) { + private TriggerWatchStats( + boolean metadata, + String triggerType, + String scheduleType, + String inputType, + String conditionType, + String transformType, + ActionStats[] actions + ) { this.metadata = metadata; this.triggerType = triggerType; this.scheduleType = scheduleType; @@ -64,7 +71,6 @@ public static TriggerWatchStats create(Watch watch) { actionStats[i++] = new ActionStats(type, transform, condition); } - return new TriggerWatchStats(metadata, triggerType, scheduleTriggerType, inputType, - conditionType, transformType, actionStats); + return new TriggerWatchStats(metadata, triggerType, scheduleTriggerType, inputType, conditionType, transformType, actionStats); } } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/manual/ManualTrigger.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/manual/ManualTrigger.java index f9b0275e467b9..5ee6de567e9fc 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/manual/ManualTrigger.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/manual/ManualTrigger.java @@ -25,15 +25,25 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws return builder.startObject().endObject(); } - static ManualTrigger parse(XContentParser parser) throws IOException{ - if (parser.currentToken() != XContentParser.Token.START_OBJECT){ - throw new ElasticsearchParseException("unable to parse [" + ManualTriggerEngine.TYPE + - "] trigger. expected a start object token, found [" + parser.currentToken() + "]"); + static ManualTrigger parse(XContentParser parser) throws IOException { + if (parser.currentToken() != XContentParser.Token.START_OBJECT) { + throw new ElasticsearchParseException( + "unable to parse [" + + ManualTriggerEngine.TYPE + + "] trigger. expected a start object token, found [" + + parser.currentToken() + + "]" + ); } XContentParser.Token token = parser.nextToken(); if (token != XContentParser.Token.END_OBJECT) { - throw new ElasticsearchParseException("unable to parse [" + ManualTriggerEngine.TYPE + - "] trigger. expected an empty object, but found an object with [" + token + "]"); + throw new ElasticsearchParseException( + "unable to parse [" + + ManualTriggerEngine.TYPE + + "] trigger. expected an empty object, but found an object with [" + + token + + "]" + ); } return new ManualTrigger(); } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/manual/ManualTriggerEngine.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/manual/ManualTriggerEngine.java index c2d7328f82ec4..bdf549f9cfa87 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/manual/ManualTriggerEngine.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/manual/ManualTriggerEngine.java @@ -34,24 +34,19 @@ public String type() { * from the given list of jobs */ @Override - public void start(Collection jobs) { - } + public void start(Collection jobs) {} @Override - public void stop() { - } + public void stop() {} @Override - public void register(Consumer> consumer) { - } + public void register(Consumer> consumer) {} @Override - public void add(Watch job) { - } + public void add(Watch job) {} @Override - public void pauseExecution() { - } + public void pauseExecution() {} @Override public boolean remove(String jobId) { @@ -80,8 +75,8 @@ public ManualTrigger parseTrigger(String context, XContentParser parser) throws } @Override - public ManualTriggerEvent parseTriggerEvent(TriggerService service, String watchId, String context, XContentParser parser) throws - IOException { + public ManualTriggerEvent parseTriggerEvent(TriggerService service, String watchId, String context, XContentParser parser) + throws IOException { return ManualTriggerEvent.parse(service, watchId, context, parser); } } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/manual/ManualTriggerEvent.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/manual/ManualTriggerEvent.java index 76440d46517e5..9b1dbbec0a762 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/manual/ManualTriggerEvent.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/manual/ManualTriggerEvent.java @@ -42,8 +42,8 @@ public void recordDataXContent(XContentBuilder builder, Params params) throws IO builder.endObject(); } - public static ManualTriggerEvent parse(TriggerService triggerService, String watchId, String context, XContentParser parser) throws - IOException { + public static ManualTriggerEvent parse(TriggerService triggerService, String watchId, String context, XContentParser parser) + throws IOException { TriggerEvent parsedTriggerEvent = triggerService.parseTriggerEvent(watchId, context, parser); return new ManualTriggerEvent(context, parsedTriggerEvent); } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/CronSchedule.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/CronSchedule.java index 45974a4461705..ce49cd7283ab4 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/CronSchedule.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/CronSchedule.java @@ -56,8 +56,9 @@ public CronSchedule parse(XContentParser parser) throws IOException { crons.add(parser.text()); break; default: - throw new ElasticsearchParseException("could not parse [cron] schedule. expected a string value in the cron " + - "array but found [" + token + "]"); + throw new ElasticsearchParseException( + "could not parse [cron] schedule. expected a string value in the cron " + "array but found [" + token + "]" + ); } } if (crons.isEmpty()) { @@ -70,8 +71,12 @@ public CronSchedule parse(XContentParser parser) throws IOException { } } else { - throw new ElasticsearchParseException("could not parse [cron] schedule. expected either a cron string value or an array " + - "of cron string values, but found [" + token + "]"); + throw new ElasticsearchParseException( + "could not parse [cron] schedule. expected either a cron string value or an array " + + "of cron string values, but found [" + + token + + "]" + ); } } } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/DailySchedule.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/DailySchedule.java index 1c746a43779d2..359f98fb516af 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/DailySchedule.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/DailySchedule.java @@ -94,16 +94,26 @@ public DailySchedule parse(XContentParser parser) throws IOException { try { times.add(DayTimes.parse(parser, token)); } catch (ElasticsearchParseException pe) { - throw new ElasticsearchParseException("could not parse [{}] schedule. invalid time value for field [{}] - [{}]", - pe, TYPE, currentFieldName, token); + throw new ElasticsearchParseException( + "could not parse [{}] schedule. invalid time value for field [{}] - [{}]", + pe, + TYPE, + currentFieldName, + token + ); } } else { while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { try { times.add(DayTimes.parse(parser, token)); } catch (ElasticsearchParseException pe) { - throw new ElasticsearchParseException("could not parse [{}] schedule. invalid time value for field [{}] -" + - " [{}]", pe, TYPE, currentFieldName, token); + throw new ElasticsearchParseException( + "could not parse [{}] schedule. invalid time value for field [{}] -" + " [{}]", + pe, + TYPE, + currentFieldName, + token + ); } } } @@ -120,8 +130,7 @@ public static class Builder { private Set times = new HashSet<>(); - private Builder() { - } + private Builder() {} public Builder at(int hour, int minute) { times.add(new DayTimes(hour, minute)); diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/HourlySchedule.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/HourlySchedule.java index 57e490d5cfae6..f0a6f682d8e48 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/HourlySchedule.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/HourlySchedule.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.watcher.trigger.schedule; import org.elasticsearch.ElasticsearchParseException; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.util.CollectionUtils; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.watcher.trigger.schedule.support.DayTimes; @@ -106,22 +106,34 @@ public HourlySchedule parse(XContentParser parser) throws IOException { try { minutes.add(DayTimes.parseMinuteValue(parser, token)); } catch (ElasticsearchParseException pe) { - throw new ElasticsearchParseException("could not parse [{}] schedule. invalid value for [{}]", pe, TYPE, - currentFieldName); + throw new ElasticsearchParseException( + "could not parse [{}] schedule. invalid value for [{}]", + pe, + TYPE, + currentFieldName + ); } } else if (token == XContentParser.Token.START_ARRAY) { while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { try { minutes.add(DayTimes.parseMinuteValue(parser, token)); } catch (ElasticsearchParseException pe) { - throw new ElasticsearchParseException("could not parse [{}] schedule. invalid value for [{}]", pe, TYPE, - currentFieldName); + throw new ElasticsearchParseException( + "could not parse [{}] schedule. invalid value for [{}]", + pe, + TYPE, + currentFieldName + ); } } } else { - throw new ElasticsearchParseException("could not parse [{}] schedule. invalid value for [{}]. " + - "expected either string/value or an array of string/number values, but found [{}]", TYPE, currentFieldName, - token); + throw new ElasticsearchParseException( + "could not parse [{}] schedule. invalid value for [{}]. " + + "expected either string/value or an array of string/number values, but found [{}]", + TYPE, + currentFieldName, + token + ); } } else { throw new ElasticsearchParseException("could not parse [{}] schedule. unexpected field [{}]", TYPE, currentFieldName); @@ -137,8 +149,7 @@ public static class Builder { private Set minutes = new HashSet<>(); - private Builder() { - } + private Builder() {} public Builder minutes(int... minutes) { for (int minute : minutes) { diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/IntervalSchedule.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/IntervalSchedule.java index 86cce192e9d7e..65a602ad3fbdb 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/IntervalSchedule.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/IntervalSchedule.java @@ -97,8 +97,12 @@ public IntervalSchedule parse(XContentParser parser) throws IOException { } catch (Exception e) { throw new ElasticsearchParseException("could not parse schedule: {}", e, e.getMessage()); } - throw new ElasticsearchParseException("could not parse [{}] schedule. expected either a numeric value " + - "(millis) or a string value representing time value (e.g. '5s'), but found [{}]", TYPE, token); + throw new ElasticsearchParseException( + "could not parse [{}] schedule. expected either a numeric value " + + "(millis) or a string value representing time value (e.g. '5s'), but found [{}]", + TYPE, + token + ); } } @@ -137,8 +141,12 @@ public long parse(String value) { try { return Long.parseLong(num); } catch (NumberFormatException nfe) { - throw new ElasticsearchParseException("could not parse [{}] schedule. could not parse [{}] as a [{}] duration", - TYPE, num, name().toLowerCase(Locale.ROOT)); + throw new ElasticsearchParseException( + "could not parse [{}] schedule. could not parse [{}] as a [{}] duration", + TYPE, + num, + name().toLowerCase(Locale.ROOT) + ); } } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/MonthlySchedule.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/MonthlySchedule.java index 6715146e558f7..1c9c021ec79ec 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/MonthlySchedule.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/MonthlySchedule.java @@ -96,8 +96,12 @@ public MonthlySchedule parse(XContentParser parser) throws IOException { } return times.isEmpty() ? new MonthlySchedule() : new MonthlySchedule(times.toArray(new MonthTimes[times.size()])); } - throw new ElasticsearchParseException("could not parse [{}] schedule. expected either an object or an array " + - "of objects representing month times, but found [{}] instead", TYPE, parser.currentToken()); + throw new ElasticsearchParseException( + "could not parse [{}] schedule. expected either an object or an array " + + "of objects representing month times, but found [{}] instead", + TYPE, + parser.currentToken() + ); } } @@ -105,8 +109,7 @@ public static class Builder { private final Set times = new HashSet<>(); - private Builder() { - } + private Builder() {} public Builder time(MonthTimes time) { times.add(time); diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/ScheduleRegistry.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/ScheduleRegistry.java index 6689127fd33ec..7dde5e5faacee 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/ScheduleRegistry.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/ScheduleRegistry.java @@ -35,8 +35,10 @@ public Schedule parse(String context, XContentParser parser) throws IOException } else if (type != null) { schedule = parse(context, type, parser); } else { - throw new ElasticsearchParseException("could not parse schedule. expected a schedule type field, but found [{}] instead", - token); + throw new ElasticsearchParseException( + "could not parse schedule. expected a schedule type field, but found [{}] instead", + token + ); } } if (schedule == null) { diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/ScheduleTriggerEngine.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/ScheduleTriggerEngine.java index 9d4000152989f..4bf64925b014f 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/ScheduleTriggerEngine.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/ScheduleTriggerEngine.java @@ -47,7 +47,6 @@ public void register(Consumer> consumer) { consumers.add(consumer); } - @Override public ScheduleTriggerEvent simulateEvent(String jobId, @Nullable Map data, TriggerService service) { ZonedDateTime now = clock.instant().atZone(ZoneOffset.UTC); @@ -77,8 +76,8 @@ public ScheduleTrigger parseTrigger(String context, XContentParser parser) throw } @Override - public ScheduleTriggerEvent parseTriggerEvent(TriggerService service, String watchId, String context, XContentParser parser) throws - IOException { + public ScheduleTriggerEvent parseTriggerEvent(TriggerService service, String watchId, String context, XContentParser parser) + throws IOException { return ScheduleTriggerEvent.parse(parser, watchId, context, clock); } } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/ScheduleTriggerEvent.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/ScheduleTriggerEvent.java index b7e20fca27aa3..ae343a5766c68 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/ScheduleTriggerEvent.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/ScheduleTriggerEvent.java @@ -69,20 +69,36 @@ public static ScheduleTriggerEvent parse(XContentParser parser, String watchId, try { triggeredTime = WatcherDateTimeUtils.parseDateMath(currentFieldName, parser, ZoneOffset.UTC, clock); } catch (ElasticsearchParseException pe) { - //Failed to parse as a date try datemath parsing - throw new ElasticsearchParseException("could not parse [{}] trigger event for [{}] for watch [{}]. failed to parse " + - "date field [{}]", pe, ScheduleTriggerEngine.TYPE, context, watchId, currentFieldName); + // Failed to parse as a date try datemath parsing + throw new ElasticsearchParseException( + "could not parse [{}] trigger event for [{}] for watch [{}]. failed to parse " + "date field [{}]", + pe, + ScheduleTriggerEngine.TYPE, + context, + watchId, + currentFieldName + ); } - } else if (Field.SCHEDULED_TIME.match(currentFieldName, parser.getDeprecationHandler())) { + } else if (Field.SCHEDULED_TIME.match(currentFieldName, parser.getDeprecationHandler())) { try { scheduledTime = WatcherDateTimeUtils.parseDateMath(currentFieldName, parser, ZoneOffset.UTC, clock); } catch (ElasticsearchParseException pe) { - throw new ElasticsearchParseException("could not parse [{}] trigger event for [{}] for watch [{}]. failed to parse " + - "date field [{}]", pe, ScheduleTriggerEngine.TYPE, context, watchId, currentFieldName); + throw new ElasticsearchParseException( + "could not parse [{}] trigger event for [{}] for watch [{}]. failed to parse " + "date field [{}]", + pe, + ScheduleTriggerEngine.TYPE, + context, + watchId, + currentFieldName + ); } - }else { - throw new ElasticsearchParseException("could not parse trigger event for [{}] for watch [{}]. unexpected token [{}]", - context, watchId, token); + } else { + throw new ElasticsearchParseException( + "could not parse trigger event for [{}] for watch [{}]. unexpected token [{}]", + context, + watchId, + token + ); } } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/Schedules.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/Schedules.java index 6fe7fd5eb1bc7..83bcb10e5eb33 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/Schedules.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/Schedules.java @@ -11,8 +11,7 @@ */ public class Schedules { - private Schedules() { - } + private Schedules() {} /** * Creates an interval schedule. The provided string can have the following format: diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/WeeklySchedule.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/WeeklySchedule.java index 47bafdf31d672..32c79d20fdab5 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/WeeklySchedule.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/WeeklySchedule.java @@ -96,8 +96,12 @@ public WeeklySchedule parse(XContentParser parser) throws IOException { } return times.isEmpty() ? new WeeklySchedule() : new WeeklySchedule(times.toArray(new WeekTimes[times.size()])); } - throw new ElasticsearchParseException("could not parse [{}] schedule. expected either an object or an array " + - "of objects representing weekly times, but found [{}] instead", TYPE, parser.currentToken()); + throw new ElasticsearchParseException( + "could not parse [{}] schedule. expected either an object or an array " + + "of objects representing weekly times, but found [{}] instead", + TYPE, + parser.currentToken() + ); } } @@ -120,5 +124,4 @@ public WeeklySchedule build() { } - } diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/YearlySchedule.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/YearlySchedule.java index 672e4e751142e..25f135daa5411 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/YearlySchedule.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/YearlySchedule.java @@ -96,8 +96,12 @@ public YearlySchedule parse(XContentParser parser) throws IOException { } return times.isEmpty() ? new YearlySchedule() : new YearlySchedule(times.toArray(new YearTimes[times.size()])); } - throw new ElasticsearchParseException("could not parse [{}] schedule. expected either an object or an array " + - "of objects representing year times, but found [{}] instead", TYPE, parser.currentToken()); + throw new ElasticsearchParseException( + "could not parse [{}] schedule. expected either an object or an array " + + "of objects representing year times, but found [{}] instead", + TYPE, + parser.currentToken() + ); } } @@ -105,8 +109,7 @@ public static class Builder { private final Set times = new HashSet<>(); - private Builder() { - } + private Builder() {} public Builder time(YearTimes time) { times.add(time); diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/engine/TickerScheduleTriggerEngine.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/engine/TickerScheduleTriggerEngine.java index 095c797d991ab..f6b0ca3e7caf0 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/engine/TickerScheduleTriggerEngine.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/engine/TickerScheduleTriggerEngine.java @@ -38,8 +38,11 @@ public class TickerScheduleTriggerEngine extends ScheduleTriggerEngine { - public static final Setting TICKER_INTERVAL_SETTING = - positiveTimeSetting("xpack.watcher.trigger.schedule.ticker.tick_interval", TimeValue.timeValueMillis(500), Property.NodeScope); + public static final Setting TICKER_INTERVAL_SETTING = positiveTimeSetting( + "xpack.watcher.trigger.schedule.ticker.tick_interval", + TimeValue.timeValueMillis(500), + Property.NodeScope + ); private static final Logger logger = LogManager.getLogger(TickerScheduleTriggerEngine.class); @@ -113,8 +116,7 @@ void checkJobs() { if (scheduledTime > 0) { ZonedDateTime triggeredDateTime = utcDateTimeAtEpochMillis(triggeredTime); ZonedDateTime scheduledDateTime = utcDateTimeAtEpochMillis(scheduledTime); - logger.debug("triggered job [{}] at [{}] (scheduled time was [{}])", schedule.name, - triggeredDateTime, scheduledDateTime); + logger.debug("triggered job [{}] at [{}] (scheduled time was [{}])", schedule.name, triggeredDateTime, scheduledDateTime); events.add(new ScheduleTriggerEvent(schedule.name, triggeredDateTime, scheduledDateTime)); if (events.size() >= 1000) { notifyListeners(events); diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/support/DayOfWeek.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/support/DayOfWeek.java index 891eb176c9c3b..af69eb1385786 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/support/DayOfWeek.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/support/DayOfWeek.java @@ -40,13 +40,20 @@ public static String cronPart(EnumSet days) { public static DayOfWeek resolve(int day) { switch (day) { - case 1: return SUNDAY; - case 2: return MONDAY; - case 3: return TUESDAY; - case 4: return WEDNESDAY; - case 5: return THURSDAY; - case 6: return FRIDAY; - case 7: return SATURDAY; + case 1: + return SUNDAY; + case 2: + return MONDAY; + case 3: + return TUESDAY; + case 4: + return WEDNESDAY; + case 5: + return THURSDAY; + case 6: + return FRIDAY; + case 7: + return SATURDAY; default: throw new ElasticsearchParseException("unknown day of week number [{}]", day); } @@ -56,31 +63,37 @@ public static DayOfWeek resolve(String day) { switch (day.toLowerCase(Locale.ROOT)) { case "1": case "sun": - case "sunday": return SUNDAY; + case "sunday": + return SUNDAY; case "2": case "mon": - case "monday": return MONDAY; + case "monday": + return MONDAY; case "3": case "tue": - case "tuesday": return TUESDAY; + case "tuesday": + return TUESDAY; case "4": case "wed": - case "wednesday": return WEDNESDAY; + case "wednesday": + return WEDNESDAY; case "5": case "thu": - case "thursday": return THURSDAY; + case "thursday": + return THURSDAY; case "6": case "fri": - case "friday": return FRIDAY; + case "friday": + return FRIDAY; case "7": case "sat": - case "saturday": return SATURDAY; + case "saturday": + return SATURDAY; default: throw new ElasticsearchParseException("unknown day of week [{}]", day); } } - @Override public String toString() { return cronKey; diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/support/DayTimes.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/support/DayTimes.java index 2bc86a8ae1064..f73cafcbfb1fc 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/support/DayTimes.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/support/DayTimes.java @@ -103,14 +103,20 @@ public static DayTimes parse(String time) throws ElasticsearchParseException { public void validate() { for (int i = 0; i < hour.length; i++) { if (validHour(hour[i]) == false) { - throw illegalArgument("invalid time [{}]. invalid time hour value [{}]. time hours must be between 0 and 23 incl.", - this, hour[i]); + throw illegalArgument( + "invalid time [{}]. invalid time hour value [{}]. time hours must be between 0 and 23 incl.", + this, + hour[i] + ); } } for (int i = 0; i < minute.length; i++) { if (validMinute(minute[i]) == false) { - throw illegalArgument("invalid time [{}]. invalid time minute value [{}]. time minutes must be between 0 and 59 incl.", - this, minute[i]); + throw illegalArgument( + "invalid time [{}]. invalid time minute value [{}]. time minutes must be between 0 and 59 incl.", + this, + minute[i] + ); } } } @@ -134,10 +140,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws if (time != null) { return builder.value(time); } - return builder.startObject() - .array(HOUR_FIELD.getPreferredName(), hour) - .array(MINUTE_FIELD.getPreferredName(), minute) - .endObject(); + return builder.startObject().array(HOUR_FIELD.getPreferredName(), hour).array(MINUTE_FIELD.getPreferredName(), minute).endObject(); } @Override @@ -184,7 +187,7 @@ public int hashCode() { return result; } - public static DayTimes parse(XContentParser parser, XContentParser.Token token) throws IOException, ElasticsearchParseException { + public static DayTimes parse(XContentParser parser, XContentParser.Token token) throws IOException, ElasticsearchParseException { if (token == XContentParser.Token.VALUE_STRING) { return DayTimes.parse(parser.text()); } @@ -205,8 +208,10 @@ public static DayTimes parse(XContentParser parser, XContentParser.Token token) hours.add(parseHourValue(parser, token)); } } else { - throw new ElasticsearchParseException("invalid time hour value. expected string/number value or an array of " + - "string/number values, but found [{}]", token); + throw new ElasticsearchParseException( + "invalid time hour value. expected string/number value or an array of " + "string/number values, but found [{}]", + token + ); } } else if (MINUTE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) { if (token.isValue()) { @@ -216,8 +221,10 @@ public static DayTimes parse(XContentParser parser, XContentParser.Token token) minutes.add(parseMinuteValue(parser, token)); } } else { - throw new ElasticsearchParseException("invalid time minute value. expected string/number value or an array of " + - "string/number values, but found [{}]", token); + throw new ElasticsearchParseException( + "invalid time minute value. expected string/number value or an array of " + "string/number values, but found [{}]", + token + ); } } } @@ -235,8 +242,10 @@ public static int parseHourValue(XContentParser parser, XContentParser.Token tok case VALUE_NUMBER: int hour = parser.intValue(); if (DayTimes.validHour(hour) == false) { - throw new ElasticsearchParseException("invalid time hour value [{}] (possible values may be between 0 and 23 incl.)", - hour); + throw new ElasticsearchParseException( + "invalid time hour value [{}] (possible values may be between 0 and 23 incl.)", + hour + ); } return hour; @@ -263,8 +272,10 @@ public static int parseMinuteValue(XContentParser parser, XContentParser.Token t case VALUE_NUMBER: int minute = parser.intValue(); if (DayTimes.validMinute(minute) == false) { - throw new ElasticsearchParseException("invalid time minute value [{}] (possible values may be between 0 and 59 incl.)", - minute); + throw new ElasticsearchParseException( + "invalid time minute value [{}] (possible values may be between 0 and 59 incl.)", + minute + ); } return minute; @@ -273,8 +284,10 @@ public static int parseMinuteValue(XContentParser parser, XContentParser.Token t try { minute = Integer.valueOf(value); if (DayTimes.validMinute(minute) == false) { - throw new ElasticsearchParseException("invalid time minute value [{}] (possible values may be between 0 and 59 " + - "incl.)", minute); + throw new ElasticsearchParseException( + "invalid time minute value [{}] (possible values may be between 0 and 59 " + "incl.)", + minute + ); } return minute; } catch (NumberFormatException nfe) { diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/support/Month.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/support/Month.java index ed6bcb1cc0cd7..a65d5ee4166e0 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/support/Month.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/support/Month.java @@ -45,18 +45,30 @@ public static String cronPart(EnumSet days) { public static Month resolve(int month) { switch (month) { - case 1: return JANUARY; - case 2: return FEBRUARY; - case 3: return MARCH; - case 4: return APRIL; - case 5: return MAY; - case 6: return JUNE; - case 7: return JULY; - case 8: return AUGUST; - case 9: return SEPTEMBER; - case 10: return OCTOBER; - case 11: return NOVEMBER; - case 12: return DECEMBER; + case 1: + return JANUARY; + case 2: + return FEBRUARY; + case 3: + return MARCH; + case 4: + return APRIL; + case 5: + return MAY; + case 6: + return JUNE; + case 7: + return JULY; + case 8: + return AUGUST; + case 9: + return SEPTEMBER; + case 10: + return OCTOBER; + case 11: + return NOVEMBER; + case 12: + return DECEMBER; default: throw new ElasticsearchParseException("unknown month number [{}]", month); } @@ -67,46 +79,57 @@ public static Month resolve(String day) { case "1": case "jan": case "first": - case "january": return JANUARY; + case "january": + return JANUARY; case "2": case "feb": - case "february": return FEBRUARY; + case "february": + return FEBRUARY; case "3": case "mar": - case "march": return MARCH; + case "march": + return MARCH; case "4": case "apr": - case "april": return APRIL; + case "april": + return APRIL; case "5": - case "may": return MAY; + case "may": + return MAY; case "6": case "jun": - case "june": return JUNE; + case "june": + return JUNE; case "7": case "jul": - case "july": return JULY; + case "july": + return JULY; case "8": case "aug": - case "august": return AUGUST; + case "august": + return AUGUST; case "9": case "sep": - case "september": return SEPTEMBER; + case "september": + return SEPTEMBER; case "10": case "oct": - case "october": return OCTOBER; + case "october": + return OCTOBER; case "11": case "nov": - case "november": return NOVEMBER; + case "november": + return NOVEMBER; case "12": case "dec": case "last": - case "december": return DECEMBER; + case "december": + return DECEMBER; default: throw new ElasticsearchParseException("unknown month [{}]", day); } } - @Override public String toString() { return cronKey; diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/support/MonthTimes.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/support/MonthTimes.java index 02ec4bf2e4047..c497410c0b8cf 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/support/MonthTimes.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/support/MonthTimes.java @@ -46,7 +46,7 @@ public MonthTimes(int[] days, DayTimes[] times) { void validate() { for (int day : days) { - if (day < 1 || day > 32) { //32 represents the last day of the month + if (day < 1 || day > 32) { // 32 represents the last day of the month throw illegalArgument("invalid month day [{}]", day); } } @@ -96,16 +96,11 @@ public int hashCode() { @Override public String toString() { - return String.format( - Locale.ROOT, - "days [%s], times [%s]", - join(",", days), - Strings.arrayToCommaDelimitedString(times) - ); + return String.format(Locale.ROOT, "days [%s], times [%s]", join(",", days), Strings.arrayToCommaDelimitedString(times)); } public boolean contains(int day, DayTimes dayTimes) { - if (Arrays.binarySearch(days, day) == -1) { //days are already sorted + if (Arrays.binarySearch(days, day) == -1) { // days are already sorted return false; } for (DayTimes dayTimes1 : this.times()) { @@ -161,8 +156,12 @@ public static MonthTimes parse(XContentParser parser, XContentParser.Token token daysSet.add(parseDayValue(parser, token)); } } else { - throw new ElasticsearchParseException("invalid month day value for [{}] field. expected string/number value or an " + - "array of string/number values, but found [{}]", currentFieldName, token); + throw new ElasticsearchParseException( + "invalid month day value for [{}] field. expected string/number value or an " + + "array of string/number values, but found [{}]", + currentFieldName, + token + ); } } else if (TIME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) { if (token != XContentParser.Token.START_ARRAY) { @@ -213,8 +212,7 @@ public static class Builder { private final Set days = new HashSet<>(); private final Set times = new HashSet<>(); - private Builder() { - } + private Builder() {} public Builder on(int... days) { Arrays.stream(days).forEach(this.days::add); diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/support/WeekTimes.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/support/WeekTimes.java index fa7cf3ea367f0..f480614254880 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/support/WeekTimes.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/support/WeekTimes.java @@ -117,8 +117,12 @@ public static WeekTimes parse(XContentParser parser, XContentParser.Token token) daysSet.add(parseDayValue(parser, token)); } } else { - throw new ElasticsearchParseException("invalid week day value for [{}] field. expected string/number value or an " + - "array of string/number values, but found [{}]", currentFieldName, token); + throw new ElasticsearchParseException( + "invalid week day value for [{}] field. expected string/number value or an " + + "array of string/number values, but found [{}]", + currentFieldName, + token + ); } } else if (TIME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) { if (token != XContentParser.Token.START_ARRAY) { @@ -158,8 +162,7 @@ public static class Builder { private final Set days = new HashSet<>(); private final Set times = new HashSet<>(); - private Builder() { - } + private Builder() {} public Builder on(DayOfWeek... days) { Collections.addAll(this.days, days); diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/support/YearTimes.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/support/YearTimes.java index 6008889b4a9f0..1925418cae974 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/support/YearTimes.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/support/YearTimes.java @@ -48,7 +48,7 @@ public YearTimes(EnumSet months, int[] days, DayTimes[] times) { void validate() { for (int day : days) { - if (day < 1 || day > 32) { //32 represents the last day of the month + if (day < 1 || day > 32) { // 32 represents the last day of the month throw illegalArgument("invalid month day [{}]", day); } } @@ -90,9 +90,8 @@ public boolean equals(Object o) { YearTimes that = (YearTimes) o; - return Arrays.equals(days, that.days) - && months.equals(that.months) - // order doesn't matter + return Arrays.equals(days, that.days) && months.equals(that.months) + // order doesn't matter && newHashSet(times).equals(newHashSet(that.times)); } @@ -107,11 +106,11 @@ public int hashCode() { @Override public String toString() { return String.format( - Locale.ROOT, - "months [%s], days [%s], times [%s]", - Strings.collectionToCommaDelimitedString(months), - join(",", days), - Strings.arrayToCommaDelimitedString(times) + Locale.ROOT, + "months [%s], days [%s], times [%s]", + Strings.collectionToCommaDelimitedString(months), + join(",", days), + Strings.arrayToCommaDelimitedString(times) ); } @@ -151,8 +150,12 @@ public static YearTimes parse(XContentParser parser, XContentParser.Token token) monthsSet.add(parseMonthValue(parser, token)); } } else { - throw new ElasticsearchParseException("invalid year month value for [{}] field. expected string/number value or an " + - "array of string/number values, but found [{}]", currentFieldName, token); + throw new ElasticsearchParseException( + "invalid year month value for [{}] field. expected string/number value or an " + + "array of string/number values, but found [{}]", + currentFieldName, + token + ); } } else if (DAY_FIELD.match(currentFieldName, parser.getDeprecationHandler())) { if (token.isValue()) { @@ -162,8 +165,12 @@ public static YearTimes parse(XContentParser parser, XContentParser.Token token) daysSet.add(MonthTimes.parseDayValue(parser, token)); } } else { - throw new ElasticsearchParseException("invalid year day value for [{}] field. expected string/number value or an " + - "array of string/number values, but found [{}]", currentFieldName, token); + throw new ElasticsearchParseException( + "invalid year day value for [{}] field. expected string/number value or an " + + "array of string/number values, but found [{}]", + currentFieldName, + token + ); } } else if (TIME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) { if (token != XContentParser.Token.START_ARRAY) { @@ -205,8 +212,7 @@ public static class Builder { private final Set days = new HashSet<>(); private final Set times = new HashSet<>(); - private Builder() { - } + private Builder() {} public Builder in(Month... months) { Collections.addAll(this.months, months); diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/tool/CronEvalTool.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/tool/CronEvalTool.java index a0809142f4b25..de1596412daff 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/tool/CronEvalTool.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/tool/CronEvalTool.java @@ -8,6 +8,7 @@ import joptsimple.OptionSet; import joptsimple.OptionSpec; + import org.elasticsearch.cli.ExitCodes; import org.elasticsearch.cli.LoggingAwareCommand; import org.elasticsearch.cli.Terminal; diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/watch/WatchParser.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/watch/WatchParser.java index 44074861f8e67..054645c4197a8 100644 --- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/watch/WatchParser.java +++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/watch/WatchParser.java @@ -9,10 +9,10 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.ElasticsearchParseException; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; @@ -62,8 +62,13 @@ public class WatchParser { private final ExecutableCondition defaultCondition; private final List defaultActions; - public WatchParser(TriggerService triggerService, ActionRegistry actionRegistry, InputRegistry inputRegistry, - @Nullable CryptoService cryptoService, Clock clock) { + public WatchParser( + TriggerService triggerService, + ActionRegistry actionRegistry, + InputRegistry inputRegistry, + @Nullable CryptoService cryptoService, + Clock clock + ) { this.triggerService = triggerService; this.actionRegistry = actionRegistry; this.inputRegistry = inputRegistry; @@ -74,16 +79,28 @@ public WatchParser(TriggerService triggerService, ActionRegistry actionRegistry, this.defaultActions = Collections.emptyList(); } - public Watch parse(String name, boolean includeStatus, BytesReference source, XContentType xContentType, - long sourceSeqNo, long sourcePrimaryTerm) throws IOException { + public Watch parse( + String name, + boolean includeStatus, + BytesReference source, + XContentType xContentType, + long sourceSeqNo, + long sourcePrimaryTerm + ) throws IOException { - ZonedDateTime now = clock.instant().atZone(ZoneOffset.UTC); - return parse(name, includeStatus, false, source, now, xContentType, false, - sourceSeqNo, sourcePrimaryTerm); + ZonedDateTime now = clock.instant().atZone(ZoneOffset.UTC); + return parse(name, includeStatus, false, source, now, xContentType, false, sourceSeqNo, sourcePrimaryTerm); } - public Watch parse(String name, boolean includeStatus, BytesReference source, ZonedDateTime now, - XContentType xContentType, long sourceSeqNo, long sourcePrimaryTerm) throws IOException { + public Watch parse( + String name, + boolean includeStatus, + BytesReference source, + ZonedDateTime now, + XContentType xContentType, + long sourceSeqNo, + long sourcePrimaryTerm + ) throws IOException { return parse(name, includeStatus, false, source, now, xContentType, false, sourceSeqNo, sourcePrimaryTerm); } @@ -98,29 +115,55 @@ public Watch parse(String name, boolean includeStatus, BytesReference source, Zo * of the watch in the system will be use secrets for sensitive data. * */ - public Watch parseWithSecrets(String id, boolean includeStatus, BytesReference source, ZonedDateTime now, - XContentType xContentType, boolean allowRedactedPasswords, long sourceSeqNo, long sourcePrimaryTerm - ) throws IOException { + public Watch parseWithSecrets( + String id, + boolean includeStatus, + BytesReference source, + ZonedDateTime now, + XContentType xContentType, + boolean allowRedactedPasswords, + long sourceSeqNo, + long sourcePrimaryTerm + ) throws IOException { return parse(id, includeStatus, true, source, now, xContentType, allowRedactedPasswords, sourceSeqNo, sourcePrimaryTerm); } - - public Watch parseWithSecrets(String id, boolean includeStatus, BytesReference source, ZonedDateTime now, - XContentType xContentType, long sourceSeqNo, long sourcePrimaryTerm) throws IOException { + public Watch parseWithSecrets( + String id, + boolean includeStatus, + BytesReference source, + ZonedDateTime now, + XContentType xContentType, + long sourceSeqNo, + long sourcePrimaryTerm + ) throws IOException { return parse(id, includeStatus, true, source, now, xContentType, false, sourceSeqNo, sourcePrimaryTerm); } - private Watch parse(String id, boolean includeStatus, boolean withSecrets, BytesReference source, ZonedDateTime now, - XContentType xContentType, boolean allowRedactedPasswords, long sourceSeqNo, long sourcePrimaryTerm) - throws IOException { + private Watch parse( + String id, + boolean includeStatus, + boolean withSecrets, + BytesReference source, + ZonedDateTime now, + XContentType xContentType, + boolean allowRedactedPasswords, + long sourceSeqNo, + long sourcePrimaryTerm + ) throws IOException { if (logger.isTraceEnabled()) { logger.trace("parsing watch [{}] ", source.utf8ToString()); } // EMPTY is safe here because we never use namedObject - try (InputStream stream = source.streamInput(); - WatcherXContentParser parser = new WatcherXContentParser(xContentType.xContent().createParser(NamedXContentRegistry.EMPTY, - LoggingDeprecationHandler.INSTANCE, stream), - now, withSecrets ? cryptoService : null, allowRedactedPasswords)) { + try ( + InputStream stream = source.streamInput(); + WatcherXContentParser parser = new WatcherXContentParser( + xContentType.xContent().createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream), + now, + withSecrets ? cryptoService : null, + allowRedactedPasswords + ) + ) { parser.nextToken(); return parse(id, includeStatus, parser, sourceSeqNo, sourcePrimaryTerm); } catch (IOException ioe) { @@ -142,7 +185,7 @@ public Watch parse(String id, boolean includeStatus, WatcherXContentParser parse String currentFieldName = null; XContentParser.Token token; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { - if (token == null ) { + if (token == null) { throw new ElasticsearchParseException("could not parse watch [{}]. null token", id); } else if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); @@ -163,8 +206,12 @@ public Watch parse(String id, boolean includeStatus, WatcherXContentParser parse try { throttlePeriod = WatcherDateTimeUtils.parseTimeValue(parser, WatchField.THROTTLE_PERIOD_HUMAN.toString()); } catch (ElasticsearchParseException pe) { - throw new ElasticsearchParseException("could not parse watch [{}]. failed to parse time value for field [{}]", - pe, id, currentFieldName); + throw new ElasticsearchParseException( + "could not parse watch [{}]. failed to parse time value for field [{}]", + pe, + id, + currentFieldName + ); } } else if (WatchField.ACTIONS.match(currentFieldName, parser.getDeprecationHandler())) { actions = actionRegistry.parseActions(id, parser); @@ -181,16 +228,22 @@ public Watch parse(String id, boolean includeStatus, WatcherXContentParser parse } } if (trigger == null) { - throw new ElasticsearchParseException("could not parse watch [{}]. missing required field [{}]", id, - WatchField.TRIGGER.getPreferredName()); + throw new ElasticsearchParseException( + "could not parse watch [{}]. missing required field [{}]", + id, + WatchField.TRIGGER.getPreferredName() + ); } if (status != null) { // verify the status is valid (that every action indeed has a status) for (ActionWrapper action : actions) { if (status.actionStatus(action.id()) == null) { - throw new ElasticsearchParseException("could not parse watch [{}]. watch status in invalid state. action [{}] " + - "status is missing", id, action.id()); + throw new ElasticsearchParseException( + "could not parse watch [{}]. watch status in invalid state. action [{}] " + "status is missing", + id, + action.id() + ); } } } else { @@ -202,8 +255,18 @@ public Watch parse(String id, boolean includeStatus, WatcherXContentParser parse status = new WatchStatus(parser.getParseDateTime(), unmodifiableMap(actionsStatuses)); } - return new Watch( - id, trigger, input, condition, transform, throttlePeriod, actions, metatdata, status, sourceSeqNo, sourcePrimaryTerm); + id, + trigger, + input, + condition, + transform, + throttlePeriod, + actions, + metatdata, + status, + sourceSeqNo, + sourcePrimaryTerm + ); } } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/EncryptSensitiveDataBootstrapCheckTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/EncryptSensitiveDataBootstrapCheckTests.java index 0f16ae0634a2d..8a77c5c12958c 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/EncryptSensitiveDataBootstrapCheckTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/EncryptSensitiveDataBootstrapCheckTests.java @@ -29,9 +29,9 @@ public void testKeyInKeystore() { MockSecureSettings secureSettings = new MockSecureSettings(); secureSettings.setFile(WatcherField.ENCRYPTION_KEY_SETTING.getKey(), CryptoServiceTests.generateKey()); Settings settings = Settings.builder() - .put(Watcher.ENCRYPT_SENSITIVE_DATA_SETTING.getKey(), true) - .setSecureSettings(secureSettings) - .build(); + .put(Watcher.ENCRYPT_SENSITIVE_DATA_SETTING.getKey(), true) + .setSecureSettings(secureSettings) + .build(); assertFalse(CHECK.check(createTestContext(settings, null)).isFailure()); } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/WatcherIndexingListenerTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/WatcherIndexingListenerTests.java index adac84b01a070..6182838eabd71 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/WatcherIndexingListenerTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/WatcherIndexingListenerTests.java @@ -220,12 +220,15 @@ public void testPostIndexCheckParsingException() throws Exception { when(operation.id()).thenReturn(id); when(operation.source()).thenReturn(BytesArray.EMPTY); when(shardId.getIndexName()).thenReturn(Watch.INDEX); - when(parser.parseWithSecrets(anyObject(), eq(true), anyObject(), anyObject(), anyObject(), anyLong(), anyLong())) - .thenThrow(new IOException("self thrown")); + when(parser.parseWithSecrets(anyObject(), eq(true), anyObject(), anyObject(), anyObject(), anyLong(), anyLong())).thenThrow( + new IOException("self thrown") + ); when(result.getResultType()).thenReturn(Engine.Result.Type.SUCCESS); - ElasticsearchParseException exc = expectThrows(ElasticsearchParseException.class, - () -> listener.postIndex(shardId, operation, result)); + ElasticsearchParseException exc = expectThrows( + ElasticsearchParseException.class, + () -> listener.postIndex(shardId, operation, result) + ); assertThat(exc.getMessage(), containsString("Could not parse watch")); assertThat(exc.getMessage(), containsString(id)); } @@ -324,27 +327,22 @@ public void testClusterChangedWatchAliasChanged() throws Exception { ClusterState currentClusterState = mockClusterState(newActiveWatchIndex); when(currentClusterState.routingTable()).thenReturn(routingTable); - DiscoveryNodes nodes = DiscoveryNodes.builder().add(newNode("node_1")) - .localNodeId("node_1").build(); + DiscoveryNodes nodes = DiscoveryNodes.builder().add(newNode("node_1")).localNodeId("node_1").build(); when(currentClusterState.getNodes()).thenReturn(nodes); RoutingNodes routingNodes = mock(RoutingNodes.class); RoutingNode routingNode = mock(RoutingNode.class); boolean emptyShards = randomBoolean(); if (emptyShards) { - when(routingNode.shardsWithState(eq(newActiveWatchIndex), any())) - .thenReturn(Collections.emptyList()); + when(routingNode.shardsWithState(eq(newActiveWatchIndex), any())).thenReturn(Collections.emptyList()); } else { Index index = new Index(newActiveWatchIndex, "uuid"); ShardId shardId = new ShardId(index, 0); - ShardRouting shardRouting = TestShardRouting.newShardRouting(shardId, "node_1", true, - STARTED); + ShardRouting shardRouting = TestShardRouting.newShardRouting(shardId, "node_1", true, STARTED); List routing = Collections.singletonList(shardRouting); - when(routingNode.shardsWithState(eq(newActiveWatchIndex), eq(STARTED), eq(RELOCATING))) - .thenReturn(routing); + when(routingNode.shardsWithState(eq(newActiveWatchIndex), eq(STARTED), eq(RELOCATING))).thenReturn(routing); when(routingTable.allShards(eq(newActiveWatchIndex))).thenReturn(routing); - IndexRoutingTable indexRoutingTable = IndexRoutingTable.builder(index) - .addShard(shardRouting).build(); + IndexRoutingTable indexRoutingTable = IndexRoutingTable.builder(index).addShard(shardRouting).build(); when(routingTable.index(newActiveWatchIndex)).thenReturn(indexRoutingTable); } @@ -354,15 +352,13 @@ public void testClusterChangedWatchAliasChanged() throws Exception { ClusterState previousClusterState = mockClusterState(randomAlphaOfLength(8)); when(previousClusterState.routingTable()).thenReturn(routingTable); - ClusterChangedEvent event = new ClusterChangedEvent("something", currentClusterState, - previousClusterState); + ClusterChangedEvent event = new ClusterChangedEvent("something", currentClusterState, previousClusterState); listener.clusterChanged(event); if (emptyShards) { assertThat(listener.getConfiguration(), is(INACTIVE)); } else { - assertThat(listener.getConfiguration().isIndexAndActive(newActiveWatchIndex), - is(true)); + assertThat(listener.getConfiguration().isIndexAndActive(newActiveWatchIndex), is(true)); } } @@ -370,22 +366,19 @@ public void testClusterChangedNoRoutingChanges() throws Exception { Index index = new Index(Watch.INDEX, "foo"); IndexRoutingTable watchRoutingTable = IndexRoutingTable.builder(index).build(); ClusterState previousState = ClusterState.builder(new ClusterName("my-cluster")) - .nodes(new DiscoveryNodes.Builder().masterNodeId("node_1").localNodeId("node_1") - .add(newNode("node_1"))) - .routingTable(RoutingTable.builder().add(watchRoutingTable).build()) - .build(); + .nodes(new DiscoveryNodes.Builder().masterNodeId("node_1").localNodeId("node_1").add(newNode("node_1"))) + .routingTable(RoutingTable.builder().add(watchRoutingTable).build()) + .build(); ClusterState currentState = ClusterState.builder(new ClusterName("my-cluster")) - .nodes(new DiscoveryNodes.Builder().masterNodeId("node_1").localNodeId("node_1") - .add(newNode("node_1")).add(newNode("node_2"))) - .routingTable(RoutingTable.builder().add(watchRoutingTable).build()) - .build(); + .nodes(new DiscoveryNodes.Builder().masterNodeId("node_1").localNodeId("node_1").add(newNode("node_1")).add(newNode("node_2"))) + .routingTable(RoutingTable.builder().add(watchRoutingTable).build()) + .build(); Configuration configuration = listener.getConfiguration(); assertThat(configuration.isIndexAndActive(Watch.INDEX), is(true)); - ClusterChangedEvent event = new ClusterChangedEvent("something", currentState, - previousState); + ClusterChangedEvent event = new ClusterChangedEvent("something", currentState, previousState); listener.clusterChanged(event); assertThat(listener.getConfiguration(), is(configuration)); @@ -398,13 +391,19 @@ public void testCheckAllocationIdsOnShardStarted() throws Exception { Index index = new Index(Watch.INDEX, "foo"); ShardId shardId = new ShardId(index, 0); ShardRoutingState randomState = randomFrom(STARTED, RELOCATING); - ShardRouting shardRouting = TestShardRouting.newShardRouting(shardId, "current", randomState == RELOCATING ? "other" : null, true, - randomState); - IndexRoutingTable indexRoutingTable = IndexRoutingTable.builder(index) - .addShard(shardRouting).build(); - - Map allocationIds = - listener.getLocalShardAllocationIds(asList(shardRouting), indexRoutingTable); + ShardRouting shardRouting = TestShardRouting.newShardRouting( + shardId, + "current", + randomState == RELOCATING ? "other" : null, + true, + randomState + ); + IndexRoutingTable indexRoutingTable = IndexRoutingTable.builder(index).addShard(shardRouting).build(); + + Map allocationIds = listener.getLocalShardAllocationIds( + asList(shardRouting), + indexRoutingTable + ); assertThat(allocationIds.size(), is(1)); assertThat(allocationIds.get(shardId).index, is(0)); @@ -414,13 +413,13 @@ public void testCheckAllocationIdsOnShardStarted() throws Exception { public void testCheckAllocationIdsWithoutShards() throws Exception { Index index = new Index(Watch.INDEX, "foo"); ShardId shardId = new ShardId(index, 0); - ShardRouting shardRouting = TestShardRouting.newShardRouting(shardId, "other", true, - STARTED); - IndexRoutingTable indexRoutingTable = IndexRoutingTable.builder(index) - .addShard(shardRouting).build(); + ShardRouting shardRouting = TestShardRouting.newShardRouting(shardId, "other", true, STARTED); + IndexRoutingTable indexRoutingTable = IndexRoutingTable.builder(index).addShard(shardRouting).build(); - Map allocationIds = - listener.getLocalShardAllocationIds(Collections.emptyList(), indexRoutingTable); + Map allocationIds = listener.getLocalShardAllocationIds( + Collections.emptyList(), + indexRoutingTable + ); assertThat(allocationIds.size(), is(0)); } @@ -435,14 +434,13 @@ public void testCheckAllocationIdsWithSeveralShards() { localShards.add(TestShardRouting.newShardRouting(secondShardId, "node1", true, STARTED)); IndexRoutingTable indexRoutingTable = IndexRoutingTable.builder(index) - .addShard(localShards.get(0)) - .addShard(localShards.get(1)) - .addShard(TestShardRouting.newShardRouting(firstShardId, "node2", true, STARTED)) - .addShard(TestShardRouting.newShardRouting(secondShardId, "node2", true, STARTED)) - .build(); - - Map allocationIds = - listener.getLocalShardAllocationIds(localShards, indexRoutingTable); + .addShard(localShards.get(0)) + .addShard(localShards.get(1)) + .addShard(TestShardRouting.newShardRouting(firstShardId, "node2", true, STARTED)) + .addShard(TestShardRouting.newShardRouting(secondShardId, "node2", true, STARTED)) + .build(); + + Map allocationIds = listener.getLocalShardAllocationIds(localShards, indexRoutingTable); assertThat(allocationIds.size(), is(2)); } @@ -455,15 +453,13 @@ public void testShardConfigurationShouldBeTriggeredExactlyOnce() throws Exceptio logger.info("Testing [{}] documents with [{}] shards", numberOfDocuments, numberOfShards); for (int currentShardId = 0; currentShardId < numberOfShards; currentShardId++) { - ShardAllocationConfiguration sac = new ShardAllocationConfiguration(currentShardId, - numberOfShards, Collections.emptyList()); + ShardAllocationConfiguration sac = new ShardAllocationConfiguration(currentShardId, numberOfShards, Collections.emptyList()); for (int i = 0; i < numberOfDocuments; i++) { boolean shouldBeTriggered = sac.shouldBeTriggered("watch_" + i); boolean hasAlreadyBeenTriggered = bitSet.get(i); if (shouldBeTriggered) { - String message = String.format(Locale.ROOT, "Watch [%s] has already been " + - "triggered", i); + String message = String.format(Locale.ROOT, "Watch [%s] has already been " + "triggered", i); assertThat(message, hasAlreadyBeenTriggered, is(false)); bitSet.set(i); } @@ -481,36 +477,47 @@ public void testOnNonDataNodes() { ShardRouting shardRouting = TestShardRouting.newShardRouting(shardId, "node2", true, STARTED); IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index).addShard(shardRouting); - DiscoveryNode node1 = new DiscoveryNode("node_1", ESTestCase.buildNewFakeTransportAddress(), - Collections.emptyMap(), new HashSet<>(Collections.singletonList( - randomFrom(DiscoveryNodeRole.INGEST_ROLE, DiscoveryNodeRole.MASTER_ROLE))), - Version.CURRENT); - - DiscoveryNode node2 = new DiscoveryNode("node_2", ESTestCase.buildNewFakeTransportAddress(), Collections.emptyMap(), - new HashSet<>(Collections.singletonList(DiscoveryNodeRole.DATA_ROLE)), Version.CURRENT); - - DiscoveryNode node3 = new DiscoveryNode("node_3", ESTestCase.buildNewFakeTransportAddress(), Collections.emptyMap(), - new HashSet<>(Collections.singletonList(DiscoveryNodeRole.DATA_ROLE)), Version.CURRENT); - - IndexMetadata.Builder indexMetadataBuilder = createIndexBuilder(Watch.INDEX, 1 ,0); + DiscoveryNode node1 = new DiscoveryNode( + "node_1", + ESTestCase.buildNewFakeTransportAddress(), + Collections.emptyMap(), + new HashSet<>(Collections.singletonList(randomFrom(DiscoveryNodeRole.INGEST_ROLE, DiscoveryNodeRole.MASTER_ROLE))), + Version.CURRENT + ); + + DiscoveryNode node2 = new DiscoveryNode( + "node_2", + ESTestCase.buildNewFakeTransportAddress(), + Collections.emptyMap(), + new HashSet<>(Collections.singletonList(DiscoveryNodeRole.DATA_ROLE)), + Version.CURRENT + ); + + DiscoveryNode node3 = new DiscoveryNode( + "node_3", + ESTestCase.buildNewFakeTransportAddress(), + Collections.emptyMap(), + new HashSet<>(Collections.singletonList(DiscoveryNodeRole.DATA_ROLE)), + Version.CURRENT + ); + + IndexMetadata.Builder indexMetadataBuilder = createIndexBuilder(Watch.INDEX, 1, 0); ClusterState previousState = ClusterState.builder(new ClusterName("my-cluster")) - .metadata(Metadata.builder().put(indexMetadataBuilder)) - .nodes(new DiscoveryNodes.Builder().masterNodeId("node_1").localNodeId("node_1").add(node1).add(node2).add(node3)) - .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) - .build(); + .metadata(Metadata.builder().put(indexMetadataBuilder)) + .nodes(new DiscoveryNodes.Builder().masterNodeId("node_1").localNodeId("node_1").add(node1).add(node2).add(node3)) + .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) + .build(); IndexMetadata.Builder newIndexMetadataBuilder = createIndexBuilder(Watch.INDEX, 1, 1); ShardRouting replicaShardRouting = TestShardRouting.newShardRouting(shardId, "node3", false, STARTED); - IndexRoutingTable.Builder newRoutingTable = IndexRoutingTable.builder(index) - .addShard(shardRouting) - .addShard(replicaShardRouting); + IndexRoutingTable.Builder newRoutingTable = IndexRoutingTable.builder(index).addShard(shardRouting).addShard(replicaShardRouting); ClusterState currentState = ClusterState.builder(new ClusterName("my-cluster")) - .metadata(Metadata.builder().put(newIndexMetadataBuilder)) - .nodes(new DiscoveryNodes.Builder().masterNodeId("node_1").localNodeId("node_1").add(node1).add(node2).add(node3)) - .routingTable(RoutingTable.builder().add(newRoutingTable).build()) - .build(); + .metadata(Metadata.builder().put(newIndexMetadataBuilder)) + .nodes(new DiscoveryNodes.Builder().masterNodeId("node_1").localNodeId("node_1").add(node1).add(node2).add(node3)) + .routingTable(RoutingTable.builder().add(newRoutingTable).build()) + .build(); ClusterChangedEvent event = new ClusterChangedEvent("something", currentState, previousState); listener.clusterChanged(event); @@ -527,14 +534,14 @@ public void testListenerWorksIfOtherIndicesChange() throws Exception { IndexMetadata.Builder indexMetadataBuilder = createIndexBuilder("random-index", 2, 1); IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index) - .addShard(TestShardRouting.newShardRouting(firstShardId, "node_1", true, STARTED)) - .addShard(TestShardRouting.newShardRouting(firstShardId, "node_2", false, STARTED)); + .addShard(TestShardRouting.newShardRouting(firstShardId, "node_1", true, STARTED)) + .addShard(TestShardRouting.newShardRouting(firstShardId, "node_2", false, STARTED)); ClusterState previousState = ClusterState.builder(new ClusterName("my-cluster")) - .metadata(Metadata.builder().put(indexMetadataBuilder)) - .nodes(new DiscoveryNodes.Builder().masterNodeId("node_1").localNodeId("node_1").add(node1).add(node2)) - .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) - .build(); + .metadata(Metadata.builder().put(indexMetadataBuilder)) + .nodes(new DiscoveryNodes.Builder().masterNodeId("node_1").localNodeId("node_1").add(node1).add(node2)) + .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) + .build(); IndexMetadata.Builder currentMetadataBuilder = createIndexBuilder(Watch.INDEX, 2, 1); @@ -544,14 +551,14 @@ public void testListenerWorksIfOtherIndicesChange() throws Exception { ShardId watchShardId = new ShardId(otherIndex, 0); IndexRoutingTable.Builder currentRoutingTable = IndexRoutingTable.builder(otherIndex) - .addShard(TestShardRouting.newShardRouting(watchShardId, "node_1", true, STARTED)) - .addShard(TestShardRouting.newShardRouting(watchShardId, "node_2", false, STARTED)); + .addShard(TestShardRouting.newShardRouting(watchShardId, "node_1", true, STARTED)) + .addShard(TestShardRouting.newShardRouting(watchShardId, "node_2", false, STARTED)); ClusterState currentState = ClusterState.builder(new ClusterName("my-cluster")) - .metadata(Metadata.builder().put(currentMetadataBuilder)) - .nodes(new DiscoveryNodes.Builder().masterNodeId("node_1").localNodeId("node_1").add(node1).add(node2)) - .routingTable(RoutingTable.builder().add(currentRoutingTable).build()) - .build(); + .metadata(Metadata.builder().put(currentMetadataBuilder)) + .nodes(new DiscoveryNodes.Builder().masterNodeId("node_1").localNodeId("node_1").add(node1).add(node2)) + .routingTable(RoutingTable.builder().add(currentRoutingTable).build()) + .build(); listener.setConfiguration(INACTIVE); ClusterChangedEvent event = new ClusterChangedEvent("something", currentState, previousState); @@ -587,22 +594,20 @@ public void testThatShardConfigurationIsNotReloadedNonAffectedShardsChange() { ShardRouting secondShardRoutingPrimary = TestShardRouting.newShardRouting(secondShardId, "node_3", true, STARTED); ShardRouting secondShardRoutingReplica = TestShardRouting.newShardRouting(secondShardId, "node_4", false, STARTED); IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index) - .addShard(firstShardRoutingPrimary) - .addShard(firstShardRoutingReplica) - .addShard(secondShardRoutingPrimary) - .addShard(secondShardRoutingReplica); + .addShard(firstShardRoutingPrimary) + .addShard(firstShardRoutingReplica) + .addShard(secondShardRoutingPrimary) + .addShard(secondShardRoutingReplica); ClusterState previousState = ClusterState.builder(new ClusterName("my-cluster")) - .metadata(Metadata.builder().put(indexMetadataBuilder)) - .nodes(new DiscoveryNodes.Builder().masterNodeId("node_1").localNodeId(localNode) - .add(node1).add(node2).add(node3).add(node4)) - .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) - .build(); + .metadata(Metadata.builder().put(indexMetadataBuilder)) + .nodes(new DiscoveryNodes.Builder().masterNodeId("node_1").localNodeId(localNode).add(node1).add(node2).add(node3).add(node4)) + .routingTable(RoutingTable.builder().add(indexRoutingTable).build()) + .build(); ClusterState emptyState = ClusterState.builder(new ClusterName("my-cluster")) - .nodes(new DiscoveryNodes.Builder().masterNodeId("node_1").localNodeId(localNode) - .add(node1).add(node2).add(node3).add(node4)) - .build(); + .nodes(new DiscoveryNodes.Builder().masterNodeId("node_1").localNodeId(localNode).add(node1).add(node2).add(node3).add(node4)) + .build(); ClusterChangedEvent event = new ClusterChangedEvent("something", previousState, emptyState); listener.clusterChanged(event); @@ -613,16 +618,15 @@ public void testThatShardConfigurationIsNotReloadedNonAffectedShardsChange() { IndexMetadata.Builder newIndexMetadataBuilder = createIndexBuilder(Watch.INDEX, 2, 1); IndexRoutingTable.Builder newRoutingTable = IndexRoutingTable.builder(index) - .addShard(firstShardRoutingPrimary) - .addShard(firstShardRoutingReplica) - .addShard(secondShardRoutingPrimary); + .addShard(firstShardRoutingPrimary) + .addShard(firstShardRoutingReplica) + .addShard(secondShardRoutingPrimary); ClusterState currentState = ClusterState.builder(new ClusterName("my-cluster")) - .metadata(Metadata.builder().put(newIndexMetadataBuilder)) - .nodes(new DiscoveryNodes.Builder().masterNodeId("node_1").localNodeId(localNode) - .add(node1).add(node2).add(node3).add(node4)) - .routingTable(RoutingTable.builder().add(newRoutingTable).build()) - .build(); + .metadata(Metadata.builder().put(newIndexMetadataBuilder)) + .nodes(new DiscoveryNodes.Builder().masterNodeId("node_1").localNodeId(localNode).add(node1).add(node2).add(node3).add(node4)) + .routingTable(RoutingTable.builder().add(newRoutingTable).build()) + .build(); ClusterChangedEvent nodeGoneEvent = new ClusterChangedEvent("something", currentState, previousState); listener.clusterChanged(nodeGoneEvent); @@ -644,11 +648,10 @@ public void testWithAliasPointingToTwoIndicesSetsWatcherInactive() { // regular cluster state with correct single alias pointing to watches index ClusterState previousState = ClusterState.builder(new ClusterName("my-cluster")) - .metadata(Metadata.builder().put(createIndexBuilder("foo", 1, 0) - .putAlias(AliasMetadata.builder(Watch.INDEX)))) - .nodes(new DiscoveryNodes.Builder().masterNodeId("node_1").localNodeId("node_1").add(node1)) - .routingTable(RoutingTable.builder().add(fooIndexRoutingTable).build()) - .build(); + .metadata(Metadata.builder().put(createIndexBuilder("foo", 1, 0).putAlias(AliasMetadata.builder(Watch.INDEX)))) + .nodes(new DiscoveryNodes.Builder().masterNodeId("node_1").localNodeId("node_1").add(node1)) + .routingTable(RoutingTable.builder().add(fooIndexRoutingTable).build()) + .build(); // index bar pointing to .watches Index barIndex = new Index("bar", "someuuid2"); @@ -659,15 +662,14 @@ public void testWithAliasPointingToTwoIndicesSetsWatcherInactive() { // cluster state with two indices pointing to the .watches index ClusterState currentState = ClusterState.builder(new ClusterName("my-cluster")) - .metadata(Metadata.builder().put(createIndexBuilder("foo", 1, 0) - .putAlias(AliasMetadata.builder(Watch.INDEX))) - .put(barIndexMetadata)) - .nodes(new DiscoveryNodes.Builder().masterNodeId("node_1").localNodeId("node_1") - .add(node1)) - .routingTable(RoutingTable.builder() - .add(IndexRoutingTable.builder(fooIndex).addShard(fooShardRouting)) - .add(barIndexRoutingTable).build()) - .build(); + .metadata( + Metadata.builder().put(createIndexBuilder("foo", 1, 0).putAlias(AliasMetadata.builder(Watch.INDEX))).put(barIndexMetadata) + ) + .nodes(new DiscoveryNodes.Builder().masterNodeId("node_1").localNodeId("node_1").add(node1)) + .routingTable( + RoutingTable.builder().add(IndexRoutingTable.builder(fooIndex).addShard(fooShardRouting)).add(barIndexRoutingTable).build() + ) + .build(); ClusterChangedEvent nodeGoneEvent = new ClusterChangedEvent("something", currentState, previousState); listener.clusterChanged(nodeGoneEvent); @@ -742,18 +744,23 @@ private ClusterState mockClusterState(String watchIndex) { return clusterState; } - private IndexMetadata.Builder createIndexBuilder(String name, int numberOfShards, - int numberOfReplicas) { + private IndexMetadata.Builder createIndexBuilder(String name, int numberOfShards, int numberOfReplicas) { return IndexMetadata.builder(name) - .settings(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, numberOfShards) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, numberOfReplicas) - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - ); + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, numberOfShards) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, numberOfReplicas) + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + ); } private static DiscoveryNode newNode(String nodeId) { - return new DiscoveryNode(nodeId, ESTestCase.buildNewFakeTransportAddress(), Collections.emptyMap(), - DiscoveryNodeRole.roles(), Version.CURRENT); + return new DiscoveryNode( + nodeId, + ESTestCase.buildNewFakeTransportAddress(), + Collections.emptyMap(), + DiscoveryNodeRole.roles(), + Version.CURRENT + ); } } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/WatcherInfoTransportActionTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/WatcherInfoTransportActionTests.java index 94b187f143cce..cde25ca7c4f1c 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/WatcherInfoTransportActionTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/WatcherInfoTransportActionTests.java @@ -17,14 +17,14 @@ import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.ObjectPath; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.license.MockLicenseState; import org.elasticsearch.tasks.Task; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.ObjectPath; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.XPackFeatureSet; import org.elasticsearch.xpack.core.action.XPackUsageFeatureResponse; import org.elasticsearch.xpack.core.watcher.WatcherFeatureSetUsage; @@ -68,7 +68,11 @@ public void init() throws Exception { public void testAvailable() { WatcherInfoTransportAction featureSet = new WatcherInfoTransportAction( - mock(TransportService.class), mock(ActionFilters.class), Settings.EMPTY, licenseState); + mock(TransportService.class), + mock(ActionFilters.class), + Settings.EMPTY, + licenseState + ); boolean available = randomBoolean(); when(licenseState.isAllowed(WatcherField.WATCHER_FEATURE)).thenReturn(available); assertThat(featureSet.available(), is(available)); @@ -85,15 +89,18 @@ public void testEnabled() { settings.put("xpack.watcher.enabled", enabled); } WatcherInfoTransportAction featureSet = new WatcherInfoTransportAction( - mock(TransportService.class), mock(ActionFilters.class), settings.build(), licenseState); + mock(TransportService.class), + mock(ActionFilters.class), + settings.build(), + licenseState + ); assertThat(featureSet.enabled(), is(enabled)); } public void testUsageStats() throws Exception { doAnswer(mock -> { @SuppressWarnings("unchecked") - ActionListener listener = - (ActionListener) mock.getArguments()[2]; + ActionListener listener = (ActionListener) mock.getArguments()[2]; List nodes = new ArrayList<>(); DiscoveryNode first = new DiscoveryNode("first", buildNewFakeTransportAddress(), Version.CURRENT); @@ -112,8 +119,9 @@ public void testUsageStats() throws Exception { secondNode.setStats(secondCounters); nodes.add(secondNode); - listener.onResponse(new WatcherStatsResponse(new ClusterName("whatever"), new WatcherMetadata(false), - nodes, Collections.emptyList())); + listener.onResponse( + new WatcherStatsResponse(new ClusterName("whatever"), new WatcherMetadata(false), nodes, Collections.emptyList()) + ); return null; }).when(client).execute(eq(WatcherStatsAction.INSTANCE), any(), any()); ClusterService clusterService = mock(ClusterService.class); @@ -121,8 +129,16 @@ public void testUsageStats() throws Exception { when(mockNode.getId()).thenReturn("mocknode"); when(clusterService.localNode()).thenReturn(mockNode); - var usageAction = new WatcherUsageTransportAction(mock(TransportService.class), clusterService, null, - mock(ActionFilters.class), null, Settings.EMPTY, licenseState, client); + var usageAction = new WatcherUsageTransportAction( + mock(TransportService.class), + clusterService, + null, + mock(ActionFilters.class), + null, + Settings.EMPTY, + licenseState, + client + ); PlainActionFuture future = new PlainActionFuture<>(); usageAction.masterOperation(mock(Task.class), null, null, future); WatcherFeatureSetUsage watcherUsage = (WatcherFeatureSetUsage) future.get().getUsage(); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/WatcherLifeCycleServiceTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/WatcherLifeCycleServiceTests.java index a1649c1fa23de..cefc42f8e5ef1 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/WatcherLifeCycleServiceTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/WatcherLifeCycleServiceTests.java @@ -91,9 +91,7 @@ public void testNoRestartWithoutAllocationIdsConfigured() { IndexRoutingTable watchRoutingTable = IndexRoutingTable.builder(new Index(Watch.INDEX, "foo")).build(); ClusterState clusterState = ClusterState.builder(new ClusterName("my-cluster")) - .metadata(Metadata.builder() - .put(IndexTemplateMetadata.builder(HISTORY_TEMPLATE_NAME).patterns(randomIndexPatterns())) - .build()) + .metadata(Metadata.builder().put(IndexTemplateMetadata.builder(HISTORY_TEMPLATE_NAME).patterns(randomIndexPatterns())).build()) .nodes(new DiscoveryNodes.Builder().masterNodeId("node_1").localNodeId("node_1").add(newNode("node_1"))) .routingTable(RoutingTable.builder().add(watchRoutingTable).build()) .build(); @@ -111,7 +109,8 @@ public void testStartWithStateNotRecoveredBlock() { DiscoveryNodes.Builder nodes = new DiscoveryNodes.Builder().masterNodeId("id1").localNodeId("id1"); ClusterState clusterState = ClusterState.builder(new ClusterName("my-cluster")) .blocks(ClusterBlocks.builder().addGlobalBlock(GatewayService.STATE_NOT_RECOVERED_BLOCK)) - .nodes(nodes).build(); + .nodes(nodes) + .build(); lifeCycleService.clusterChanged(new ClusterChangedEvent("any", clusterState, clusterState)); verifyZeroInteractions(watcherService); } @@ -121,9 +120,7 @@ public void testShutdown() { ClusterState clusterState = ClusterState.builder(new ClusterName("my-cluster")) .nodes(new DiscoveryNodes.Builder().masterNodeId("node_1").localNodeId("node_1").add(newNode("node_1"))) .routingTable(RoutingTable.builder().add(watchRoutingTable).build()) - .metadata(Metadata.builder() - .put(IndexTemplateMetadata.builder(HISTORY_TEMPLATE_NAME).patterns(randomIndexPatterns())) - .build()) + .metadata(Metadata.builder().put(IndexTemplateMetadata.builder(HISTORY_TEMPLATE_NAME).patterns(randomIndexPatterns())).build()) .build(); when(watcherService.validate(clusterState)).thenReturn(true); @@ -140,11 +137,11 @@ public void testShutdown() { public void testManualStartStop() { Index index = new Index(Watch.INDEX, "uuid"); IndexRoutingTable.Builder indexRoutingTableBuilder = IndexRoutingTable.builder(index); - indexRoutingTableBuilder.addShard( - TestShardRouting.newShardRouting(Watch.INDEX, 0, "node_1", true, ShardRoutingState.STARTED)); - IndexMetadata.Builder indexMetadataBuilder = IndexMetadata.builder(Watch.INDEX).settings(settings(Version.CURRENT) - .put(IndexMetadata.INDEX_FORMAT_SETTING.getKey(), 6)) // the internal index format, required - .numberOfShards(1).numberOfReplicas(0); + indexRoutingTableBuilder.addShard(TestShardRouting.newShardRouting(Watch.INDEX, 0, "node_1", true, ShardRoutingState.STARTED)); + IndexMetadata.Builder indexMetadataBuilder = IndexMetadata.builder(Watch.INDEX) + .settings(settings(Version.CURRENT).put(IndexMetadata.INDEX_FORMAT_SETTING.getKey(), 6)) // the internal index format, required + .numberOfShards(1) + .numberOfReplicas(0); Metadata.Builder metadataBuilder = Metadata.builder() .put(indexMetadataBuilder) .put(IndexTemplateMetadata.builder(HISTORY_TEMPLATE_NAME).patterns(randomIndexPatterns())); @@ -170,8 +167,7 @@ public void testManualStartStop() { lifeCycleService.clusterChanged(new ClusterChangedEvent("foo", stoppedClusterState, clusterState)); ArgumentCaptor captor = ArgumentCaptor.forClass(Runnable.class); - verify(watcherService, times(1)) - .stop(eq("watcher manually marked to shutdown by cluster state update"), captor.capture()); + verify(watcherService, times(1)).stop(eq("watcher manually marked to shutdown by cluster state update"), captor.capture()); assertEquals(WatcherState.STOPPING, lifeCycleService.getState().get()); captor.getValue().run(); assertEquals(WatcherState.STOPPED, lifeCycleService.getState().get()); @@ -191,21 +187,27 @@ public void testManualStartStop() { public void testNoLocalShards() { Index watchIndex = new Index(Watch.INDEX, "foo"); ShardId shardId = new ShardId(watchIndex, 0); - DiscoveryNodes nodes = new DiscoveryNodes.Builder().masterNodeId("node_1").localNodeId("node_1") - .add(newNode("node_1")).add(newNode("node_2")) + DiscoveryNodes nodes = new DiscoveryNodes.Builder().masterNodeId("node_1") + .localNodeId("node_1") + .add(newNode("node_1")) + .add(newNode("node_2")) .build(); IndexMetadata indexMetadata = IndexMetadata.builder(Watch.INDEX) - .settings(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(IndexMetadata.INDEX_FORMAT_SETTING.getKey(), 6) - ).build(); + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetadata.INDEX_FORMAT_SETTING.getKey(), 6) + ) + .build(); IndexRoutingTable watchRoutingTable = IndexRoutingTable.builder(watchIndex) - .addShard(randomBoolean() ? - TestShardRouting.newShardRouting(shardId, "node_1", true, STARTED) : - TestShardRouting.newShardRouting(shardId, "node_1", "node_2", true, RELOCATING)) + .addShard( + randomBoolean() + ? TestShardRouting.newShardRouting(shardId, "node_1", true, STARTED) + : TestShardRouting.newShardRouting(shardId, "node_1", "node_2", true, RELOCATING) + ) .build(); ClusterState clusterStateWithLocalShards = ClusterState.builder(new ClusterName("my-cluster")) .nodes(nodes) @@ -215,9 +217,11 @@ public void testNoLocalShards() { // shard moved over to node 2 IndexRoutingTable watchRoutingTableNode2 = IndexRoutingTable.builder(watchIndex) - .addShard(randomBoolean() ? - TestShardRouting.newShardRouting(shardId, "node_2", true, STARTED) : - TestShardRouting.newShardRouting(shardId, "node_2", "node_1", true, RELOCATING)) + .addShard( + randomBoolean() + ? TestShardRouting.newShardRouting(shardId, "node_2", true, STARTED) + : TestShardRouting.newShardRouting(shardId, "node_2", "node_1", true, RELOCATING) + ) .build(); ClusterState clusterStateWithoutLocalShards = ClusterState.builder(new ClusterName("my-cluster")) .nodes(nodes) @@ -249,7 +253,8 @@ public void testReplicaWasAddedOrRemoved() { Index watchIndex = new Index(Watch.INDEX, "foo"); ShardId shardId = new ShardId(watchIndex, 0); ShardId secondShardId = new ShardId(watchIndex, 1); - DiscoveryNodes discoveryNodes = new DiscoveryNodes.Builder().masterNodeId("node_1").localNodeId("node_1") + DiscoveryNodes discoveryNodes = new DiscoveryNodes.Builder().masterNodeId("node_1") + .localNodeId("node_1") .add(newNode("node_1")) .add(newNode("node_2")) .build(); @@ -263,12 +268,14 @@ public void testReplicaWasAddedOrRemoved() { .build(); IndexMetadata indexMetadata = IndexMetadata.builder(Watch.INDEX) - .settings(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(IndexMetadata.INDEX_FORMAT_SETTING.getKey(), 6) - ).build(); + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetadata.INDEX_FORMAT_SETTING.getKey(), 6) + ) + .build(); ClusterState stateWithPrimaryShard = ClusterState.builder(new ClusterName("my-cluster")) .nodes(discoveryNodes) @@ -326,20 +333,36 @@ public void testNonDataNode() { ShardRouting shardRouting = TestShardRouting.newShardRouting(shardId, "node2", true, STARTED); IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index).addShard(shardRouting); - DiscoveryNode node1 = new DiscoveryNode("node_1", ESTestCase.buildNewFakeTransportAddress(), Collections.emptyMap(), - new HashSet<>(asList(randomFrom(DiscoveryNodeRole.INGEST_ROLE, DiscoveryNodeRole.MASTER_ROLE))), Version.CURRENT); - - DiscoveryNode node2 = new DiscoveryNode("node_2", ESTestCase.buildNewFakeTransportAddress(), Collections.emptyMap(), - new HashSet<>(asList(DiscoveryNodeRole.DATA_ROLE)), Version.CURRENT); - - DiscoveryNode node3 = new DiscoveryNode("node_3", ESTestCase.buildNewFakeTransportAddress(), Collections.emptyMap(), - new HashSet<>(asList(DiscoveryNodeRole.DATA_ROLE)), Version.CURRENT); + DiscoveryNode node1 = new DiscoveryNode( + "node_1", + ESTestCase.buildNewFakeTransportAddress(), + Collections.emptyMap(), + new HashSet<>(asList(randomFrom(DiscoveryNodeRole.INGEST_ROLE, DiscoveryNodeRole.MASTER_ROLE))), + Version.CURRENT + ); + + DiscoveryNode node2 = new DiscoveryNode( + "node_2", + ESTestCase.buildNewFakeTransportAddress(), + Collections.emptyMap(), + new HashSet<>(asList(DiscoveryNodeRole.DATA_ROLE)), + Version.CURRENT + ); + + DiscoveryNode node3 = new DiscoveryNode( + "node_3", + ESTestCase.buildNewFakeTransportAddress(), + Collections.emptyMap(), + new HashSet<>(asList(DiscoveryNodeRole.DATA_ROLE)), + Version.CURRENT + ); IndexMetadata.Builder indexMetadataBuilder = IndexMetadata.builder(Watch.INDEX) - .settings(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) ); ClusterState previousState = ClusterState.builder(new ClusterName("my-cluster")) @@ -349,10 +372,11 @@ public void testNonDataNode() { .build(); IndexMetadata.Builder newIndexMetadataBuilder = IndexMetadata.builder(Watch.INDEX) - .settings(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) ); ShardRouting replicaShardRouting = TestShardRouting.newShardRouting(shardId, "node3", false, STARTED); @@ -372,15 +396,17 @@ public void testThatMissingWatcherIndexMetadataOnlyResetsOnce() { Index watchIndex = new Index(Watch.INDEX, "foo"); ShardId shardId = new ShardId(watchIndex, 0); IndexRoutingTable watchRoutingTable = IndexRoutingTable.builder(watchIndex) - .addShard(TestShardRouting.newShardRouting(shardId, "node_1", true, STARTED)).build(); + .addShard(TestShardRouting.newShardRouting(shardId, "node_1", true, STARTED)) + .build(); DiscoveryNodes nodes = new DiscoveryNodes.Builder().masterNodeId("node_1").localNodeId("node_1").add(newNode("node_1")).build(); IndexMetadata.Builder newIndexMetadataBuilder = IndexMetadata.builder(Watch.INDEX) - .settings(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(IndexMetadata.INDEX_FORMAT_SETTING.getKey(), 6) + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetadata.INDEX_FORMAT_SETTING.getKey(), 6) ); ClusterState clusterStateWithWatcherIndex = ClusterState.builder(new ClusterName("my-cluster")) @@ -389,9 +415,7 @@ public void testThatMissingWatcherIndexMetadataOnlyResetsOnce() { .metadata(Metadata.builder().put(newIndexMetadataBuilder)) .build(); - ClusterState clusterStateWithoutWatcherIndex = ClusterState.builder(new ClusterName("my-cluster")) - .nodes(nodes) - .build(); + ClusterState clusterStateWithoutWatcherIndex = ClusterState.builder(new ClusterName("my-cluster")).nodes(nodes).build(); when(watcherService.validate(eq(clusterStateWithWatcherIndex))).thenReturn(true); when(watcherService.validate(eq(clusterStateWithoutWatcherIndex))).thenReturn(false); @@ -410,10 +434,7 @@ public void testThatMissingWatcherIndexMetadataOnlyResetsOnce() { } public void testWatcherServiceDoesNotStartIfIndexTemplatesAreMissing() throws Exception { - DiscoveryNodes nodes = new DiscoveryNodes.Builder() - .masterNodeId("node_1").localNodeId("node_1") - .add(newNode("node_1")) - .build(); + DiscoveryNodes nodes = new DiscoveryNodes.Builder().masterNodeId("node_1").localNodeId("node_1").add(newNode("node_1")).build(); Metadata.Builder metadataBuilder = Metadata.builder(); boolean isHistoryTemplateAdded = randomBoolean(); @@ -430,10 +451,7 @@ public void testWatcherServiceDoesNotStartIfIndexTemplatesAreMissing() throws Ex public void testWatcherStopsWhenMasterNodeIsMissing() { startWatcher(); - DiscoveryNodes nodes = new DiscoveryNodes.Builder() - .localNodeId("node_1") - .add(newNode("node_1")) - .build(); + DiscoveryNodes nodes = new DiscoveryNodes.Builder().localNodeId("node_1").add(newNode("node_1")).build(); ClusterState state = ClusterState.builder(new ClusterName("my-cluster")).nodes(nodes).build(); lifeCycleService.clusterChanged(new ClusterChangedEvent("any", state, state)); verify(watcherService, times(1)).pauseExecution(eq("no master node")); @@ -442,11 +460,7 @@ public void testWatcherStopsWhenMasterNodeIsMissing() { public void testWatcherStopsOnClusterLevelBlock() { startWatcher(); - DiscoveryNodes nodes = new DiscoveryNodes.Builder() - .localNodeId("node_1") - .masterNodeId("node_1") - .add(newNode("node_1")) - .build(); + DiscoveryNodes nodes = new DiscoveryNodes.Builder().localNodeId("node_1").masterNodeId("node_1").add(newNode("node_1")).build(); ClusterBlocks clusterBlocks = ClusterBlocks.builder().addGlobalBlock(NoMasterBlockService.NO_MASTER_BLOCK_WRITES).build(); ClusterState state = ClusterState.builder(new ClusterName("my-cluster")).nodes(nodes).blocks(clusterBlocks).build(); lifeCycleService.clusterChanged(new ClusterChangedEvent("any", state, state)); @@ -454,19 +468,30 @@ public void testWatcherStopsOnClusterLevelBlock() { } public void testMasterOnlyNodeCanStart() { - List roles = - Collections.singletonList(randomFrom(DiscoveryNodeRole.MASTER_ROLE, DiscoveryNodeRole.INGEST_ROLE)); + List roles = Collections.singletonList(randomFrom(DiscoveryNodeRole.MASTER_ROLE, DiscoveryNodeRole.INGEST_ROLE)); ClusterState state = ClusterState.builder(new ClusterName("my-cluster")) - .nodes(new DiscoveryNodes.Builder().masterNodeId("node_1").localNodeId("node_1") - .add(new DiscoveryNode("node_1", ESTestCase.buildNewFakeTransportAddress(), Collections.emptyMap(), - new HashSet<>(roles), Version.CURRENT))).build(); + .nodes( + new DiscoveryNodes.Builder().masterNodeId("node_1") + .localNodeId("node_1") + .add( + new DiscoveryNode( + "node_1", + ESTestCase.buildNewFakeTransportAddress(), + Collections.emptyMap(), + new HashSet<>(roles), + Version.CURRENT + ) + ) + ) + .build(); lifeCycleService.clusterChanged(new ClusterChangedEvent("test", state, state)); assertThat(lifeCycleService.getState().get(), is(WatcherState.STARTED)); } public void testDataNodeWithoutDataCanStart() { - Metadata metadata = Metadata.builder().put(IndexTemplateMetadata.builder(HISTORY_TEMPLATE_NAME).patterns(randomIndexPatterns())) + Metadata metadata = Metadata.builder() + .put(IndexTemplateMetadata.builder(HISTORY_TEMPLATE_NAME).patterns(randomIndexPatterns())) .build(); ClusterState state = ClusterState.builder(new ClusterName("my-cluster")) .nodes(new DiscoveryNodes.Builder().masterNodeId("node_1").localNodeId("node_1").add(newNode("node_1"))) @@ -485,7 +510,8 @@ public void testWatcherReloadsOnNodeOutageWithWatcherShard() { ShardId shardId = new ShardId(watchIndex, 0); String localNodeId = randomFrom("node_1", "node_2"); String outageNodeId = localNodeId.equals("node_1") ? "node_2" : "node_1"; - DiscoveryNodes previousDiscoveryNodes = new DiscoveryNodes.Builder().masterNodeId(localNodeId).localNodeId(localNodeId) + DiscoveryNodes previousDiscoveryNodes = new DiscoveryNodes.Builder().masterNodeId(localNodeId) + .localNodeId(localNodeId) .add(newNode(localNodeId)) .add(newNode(outageNodeId)) .build(); @@ -498,12 +524,14 @@ public void testWatcherReloadsOnNodeOutageWithWatcherShard() { .build(); IndexMetadata indexMetadata = IndexMetadata.builder(Watch.INDEX) - .settings(Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) - .put(IndexMetadata.INDEX_FORMAT_SETTING.getKey(), 6) - ).build(); + .settings( + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .put(IndexMetadata.INDEX_FORMAT_SETTING.getKey(), 6) + ) + .build(); ClusterState previousState = ClusterState.builder(new ClusterName("my-cluster")) .nodes(previousDiscoveryNodes) @@ -512,11 +540,10 @@ public void testWatcherReloadsOnNodeOutageWithWatcherShard() { .build(); ShardRouting nowPrimaryShardRouting = replicaShardRouting.moveActiveReplicaToPrimary(); - IndexRoutingTable currentWatchRoutingTable = IndexRoutingTable.builder(watchIndex) - .addShard(nowPrimaryShardRouting) - .build(); + IndexRoutingTable currentWatchRoutingTable = IndexRoutingTable.builder(watchIndex).addShard(nowPrimaryShardRouting).build(); - DiscoveryNodes currentDiscoveryNodes = new DiscoveryNodes.Builder().masterNodeId(localNodeId).localNodeId(localNodeId) + DiscoveryNodes currentDiscoveryNodes = new DiscoveryNodes.Builder().masterNodeId(localNodeId) + .localNodeId(localNodeId) .add(newNode(localNodeId)) .build(); @@ -540,23 +567,22 @@ public void testWatcherReloadsOnNodeOutageWithWatcherShard() { private void startWatcher() { Index index = new Index(Watch.INDEX, "uuid"); IndexRoutingTable.Builder indexRoutingTableBuilder = IndexRoutingTable.builder(index); - indexRoutingTableBuilder.addShard( - TestShardRouting.newShardRouting(Watch.INDEX, 0, "node_1", true, ShardRoutingState.STARTED)); - IndexMetadata.Builder indexMetadataBuilder = IndexMetadata.builder(Watch.INDEX).settings(settings(Version.CURRENT) - .put(IndexMetadata.INDEX_FORMAT_SETTING.getKey(), 6)) // the internal index format, required - .numberOfShards(1).numberOfReplicas(0); - Metadata metadata = Metadata.builder().put(IndexTemplateMetadata.builder(HISTORY_TEMPLATE_NAME).patterns(randomIndexPatterns())) + indexRoutingTableBuilder.addShard(TestShardRouting.newShardRouting(Watch.INDEX, 0, "node_1", true, ShardRoutingState.STARTED)); + IndexMetadata.Builder indexMetadataBuilder = IndexMetadata.builder(Watch.INDEX) + .settings(settings(Version.CURRENT).put(IndexMetadata.INDEX_FORMAT_SETTING.getKey(), 6)) // the internal index format, required + .numberOfShards(1) + .numberOfReplicas(0); + Metadata metadata = Metadata.builder() + .put(IndexTemplateMetadata.builder(HISTORY_TEMPLATE_NAME).patterns(randomIndexPatterns())) .put(indexMetadataBuilder) .build(); ClusterState state = ClusterState.builder(new ClusterName("my-cluster")) - .nodes(new DiscoveryNodes.Builder().masterNodeId("node_1").localNodeId("node_1") - .add(newNode("node_1"))) + .nodes(new DiscoveryNodes.Builder().masterNodeId("node_1").localNodeId("node_1").add(newNode("node_1"))) .routingTable(RoutingTable.builder().add(indexRoutingTableBuilder.build()).build()) .metadata(metadata) .build(); ClusterState emptyState = ClusterState.builder(new ClusterName("my-cluster")) - .nodes(new DiscoveryNodes.Builder().masterNodeId("node_1").localNodeId("node_1") - .add(newNode("node_1"))) + .nodes(new DiscoveryNodes.Builder().masterNodeId("node_1").localNodeId("node_1").add(newNode("node_1"))) .metadata(metadata) .build(); @@ -572,9 +598,7 @@ private void startWatcher() { } private List randomIndexPatterns() { - return IntStream.range(0, between(1, 10)) - .mapToObj(n -> randomAlphaOfLengthBetween(1, 100)) - .collect(Collectors.toList()); + return IntStream.range(0, between(1, 10)).mapToObj(n -> randomAlphaOfLengthBetween(1, 100)).collect(Collectors.toList()); } private static DiscoveryNode newNode(String nodeName) { @@ -582,7 +606,12 @@ private static DiscoveryNode newNode(String nodeName) { } private static DiscoveryNode newNode(String nodeName, Version version) { - return new DiscoveryNode(nodeName, ESTestCase.buildNewFakeTransportAddress(), Collections.emptyMap(), - DiscoveryNodeRole.roles(), version); + return new DiscoveryNode( + nodeName, + ESTestCase.buildNewFakeTransportAddress(), + Collections.emptyMap(), + DiscoveryNodeRole.roles(), + version + ); } } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/WatcherMetadataSerializationTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/WatcherMetadataSerializationTests.java index 41a5c7e28d42d..b140227a5c155 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/WatcherMetadataSerializationTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/WatcherMetadataSerializationTests.java @@ -11,12 +11,12 @@ import org.elasticsearch.cluster.metadata.RepositoriesMetadata; import org.elasticsearch.cluster.metadata.RepositoryMetadata; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.XPackClientPlugin; import org.elasticsearch.xpack.core.watcher.WatcherMetadata; @@ -59,8 +59,9 @@ public void testWatcherMetadataParsingDoesNotSwallowOtherMetadata() throws Excep } // serialize metadata XContentBuilder builder = XContentFactory.jsonBuilder(); - ToXContent.Params params = new ToXContent.MapParams(Collections.singletonMap(Metadata.CONTEXT_MODE_PARAM, - Metadata.CONTEXT_MODE_GATEWAY)); + ToXContent.Params params = new ToXContent.MapParams( + Collections.singletonMap(Metadata.CONTEXT_MODE_PARAM, Metadata.CONTEXT_MODE_GATEWAY) + ); builder.startObject(); builder = metadataBuilder.build().toXContent(builder, params); builder.endObject(); @@ -74,7 +75,7 @@ public void testWatcherMetadataParsingDoesNotSwallowOtherMetadata() throws Excep private static WatcherMetadata getWatcherMetadataFromXContent(XContentParser parser) throws Exception { parser.nextToken(); // consume null parser.nextToken(); // consume "watcher" - WatcherMetadata watcherMetadataFromXContent = (WatcherMetadata)WatcherMetadata.fromXContent(parser); + WatcherMetadata watcherMetadataFromXContent = (WatcherMetadata) WatcherMetadata.fromXContent(parser); parser.nextToken(); // consume endObject assertThat(parser.nextToken(), nullValue()); return watcherMetadataFromXContent; @@ -82,10 +83,12 @@ private static WatcherMetadata getWatcherMetadataFromXContent(XContentParser par @Override protected NamedXContentRegistry xContentRegistry() { - return new NamedXContentRegistry(Stream.concat( + return new NamedXContentRegistry( + Stream.concat( new XPackClientPlugin(Settings.builder().put("path.home", createTempDir()).build()).getNamedXContent().stream(), ClusterModule.getNamedXWriteables().stream() - ).collect(Collectors.toList())); + ).collect(Collectors.toList()) + ); } } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/WatcherPluginTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/WatcherPluginTests.java index c220b06dcc184..f55799ea741ec 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/WatcherPluginTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/WatcherPluginTests.java @@ -33,10 +33,7 @@ public class WatcherPluginTests extends ESTestCase { public void testWatcherDisabledTests() throws Exception { - Settings settings = Settings.builder() - .put("xpack.watcher.enabled", false) - .put("path.home", createTempDir()) - .build(); + Settings settings = Settings.builder().put("xpack.watcher.enabled", false).put("path.home", createTempDir()).build(); Watcher watcher = new Watcher(settings); List> executorBuilders = watcher.getExecutorBuilders(settings); @@ -46,10 +43,27 @@ public void testWatcherDisabledTests() throws Exception { // ensure index module is not called, even if watches index is tried IndexSettings indexSettings = IndexSettingsModule.newIndexSettings(Watch.INDEX, settings); - AnalysisRegistry registry = new AnalysisRegistry(TestEnvironment.newEnvironment(settings), emptyMap(), emptyMap(), emptyMap(), - emptyMap(), emptyMap(), emptyMap(), emptyMap(), emptyMap(), emptyMap()); - IndexModule indexModule = new IndexModule(indexSettings, registry, new InternalEngineFactory(), Collections.emptyMap(), - () -> true, TestIndexNameExpressionResolver.newInstance(), Collections.emptyMap()); + AnalysisRegistry registry = new AnalysisRegistry( + TestEnvironment.newEnvironment(settings), + emptyMap(), + emptyMap(), + emptyMap(), + emptyMap(), + emptyMap(), + emptyMap(), + emptyMap(), + emptyMap(), + emptyMap() + ); + IndexModule indexModule = new IndexModule( + indexSettings, + registry, + new InternalEngineFactory(), + Collections.emptyMap(), + () -> true, + TestIndexNameExpressionResolver.newInstance(), + Collections.emptyMap() + ); // this will trip an assertion if the watcher indexing operation listener is null (which it is) but we try to add it watcher.onIndexModule(indexModule); @@ -77,10 +91,7 @@ public void testThreadPoolSize() { } public void testReload() { - Settings settings = Settings.builder() - .put("xpack.watcher.enabled", true) - .put("path.home", createTempDir()) - .build(); + Settings settings = Settings.builder().put("xpack.watcher.enabled", true).put("path.home", createTempDir()).build(); NotificationService mockService = mock(NotificationService.class); Watcher watcher = new TestWatcher(settings, mockService); @@ -89,10 +100,7 @@ public void testReload() { } public void testReloadDisabled() { - Settings settings = Settings.builder() - .put("xpack.watcher.enabled", false) - .put("path.home", createTempDir()) - .build(); + Settings settings = Settings.builder().put("xpack.watcher.enabled", false).put("path.home", createTempDir()).build(); NotificationService mockService = mock(NotificationService.class); Watcher watcher = new TestWatcher(settings, mockService); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/WatcherServiceTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/WatcherServiceTests.java index 4699622c2bcb9..e3364f1cd8cb8 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/WatcherServiceTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/WatcherServiceTests.java @@ -38,7 +38,6 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.EsExecutors; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.Index; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.search.SearchHit; @@ -46,6 +45,7 @@ import org.elasticsearch.search.SearchShardTarget; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.watcher.input.ExecutableInput; import org.elasticsearch.xpack.core.watcher.trigger.Trigger; import org.elasticsearch.xpack.core.watcher.watch.Watch; @@ -94,55 +94,64 @@ public void testValidateStartWithClosedIndex() { ExecutionService executionService = mock(ExecutionService.class); WatchParser parser = mock(WatchParser.class); - WatcherService service = new WatcherService(Settings.EMPTY, triggerService, triggeredWatchStore, - executionService, parser, client, EsExecutors.DIRECT_EXECUTOR_SERVICE) { + WatcherService service = new WatcherService( + Settings.EMPTY, + triggerService, + triggeredWatchStore, + executionService, + parser, + client, + EsExecutors.DIRECT_EXECUTOR_SERVICE + ) { @Override - void stopExecutor() { - } + void stopExecutor() {} }; ClusterState.Builder csBuilder = new ClusterState.Builder(new ClusterName("_name")); Metadata.Builder metadataBuilder = Metadata.builder(); - Settings indexSettings = settings(Version.CURRENT) - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) - .build(); + Settings indexSettings = settings(Version.CURRENT).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) + .build(); metadataBuilder.put(IndexMetadata.builder(Watch.INDEX).state(IndexMetadata.State.CLOSE).settings(indexSettings)); csBuilder.metadata(metadataBuilder); assertThat(service.validate(csBuilder.build()), is(false)); } - @SuppressWarnings({"unchecked", "rawtypes"}) + @SuppressWarnings({ "unchecked", "rawtypes" }) public void testLoadOnlyActiveWatches() throws Exception { TriggerService triggerService = mock(TriggerService.class); TriggeredWatchStore triggeredWatchStore = mock(TriggeredWatchStore.class); ExecutionService executionService = mock(ExecutionService.class); WatchParser parser = mock(WatchParser.class); - WatcherService service = new WatcherService(Settings.EMPTY, triggerService, triggeredWatchStore, - executionService, parser, client, EsExecutors.DIRECT_EXECUTOR_SERVICE) { + WatcherService service = new WatcherService( + Settings.EMPTY, + triggerService, + triggeredWatchStore, + executionService, + parser, + client, + EsExecutors.DIRECT_EXECUTOR_SERVICE + ) { @Override - void stopExecutor() { - } + void stopExecutor() {} }; - // cluster state setup, with one node, one shard ClusterState.Builder csBuilder = new ClusterState.Builder(new ClusterName("_name")); Metadata.Builder metadataBuilder = Metadata.builder(); - Settings indexSettings = settings(Version.CURRENT) - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) - .build(); + Settings indexSettings = settings(Version.CURRENT).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) + .build(); metadataBuilder.put(IndexMetadata.builder(Watch.INDEX).settings(indexSettings)); csBuilder.metadata(metadataBuilder); Index watchIndex = new Index(Watch.INDEX, "uuid"); ShardId shardId = new ShardId(watchIndex, 0); - IndexShardRoutingTable indexShardRoutingTable = new IndexShardRoutingTable.Builder(shardId) - .addShard(TestShardRouting.newShardRouting(shardId, "node", true, ShardRoutingState.STARTED)) - .build(); + IndexShardRoutingTable indexShardRoutingTable = new IndexShardRoutingTable.Builder(shardId).addShard( + TestShardRouting.newShardRouting(shardId, "node", true, ShardRoutingState.STARTED) + ).build(); IndexRoutingTable indexRoutingTable = IndexRoutingTable.builder(watchIndex).addIndexShard(indexShardRoutingTable).build(); RoutingTable routingTable = RoutingTable.builder().add(indexRoutingTable).build(); @@ -151,11 +160,11 @@ void stopExecutor() { csBuilder.nodes(new DiscoveryNodes.Builder().masterNodeId("node").localNodeId("node").add(newNode())); ClusterState clusterState = csBuilder.build(); - // response setup, successful refresh response RefreshResponse refreshResponse = mock(RefreshResponse.class); - when(refreshResponse.getSuccessfulShards()) - .thenReturn(clusterState.getMetadata().getIndices().get(Watch.INDEX).getNumberOfShards()); + when(refreshResponse.getSuccessfulShards()).thenReturn( + clusterState.getMetadata().getIndices().get(Watch.INDEX).getNumberOfShards() + ); doAnswer(invocation -> { ActionListener listener = (ActionListener) invocation.getArguments()[2]; listener.onResponse(refreshResponse); @@ -164,8 +173,16 @@ void stopExecutor() { // empty scroll response, no further scrolling needed SearchResponseSections scrollSearchSections = new SearchResponseSections(SearchHits.empty(), null, null, false, false, null, 1); - SearchResponse scrollSearchResponse = new SearchResponse(scrollSearchSections, "scrollId", 1, 1, 0, 10, - ShardSearchFailure.EMPTY_ARRAY, SearchResponse.Clusters.EMPTY); + SearchResponse scrollSearchResponse = new SearchResponse( + scrollSearchSections, + "scrollId", + 1, + 1, + 0, + 10, + ShardSearchFailure.EMPTY_ARRAY, + SearchResponse.Clusters.EMPTY + ); doAnswer(invocation -> { ActionListener listener = (ActionListener) invocation.getArguments()[2]; listener.onResponse(scrollSearchResponse); @@ -196,8 +213,16 @@ void stopExecutor() { } SearchHits searchHits = new SearchHits(hits, new TotalHits(count, TotalHits.Relation.EQUAL_TO), 1.0f); SearchResponseSections sections = new SearchResponseSections(searchHits, null, null, false, false, null, 1); - SearchResponse searchResponse = new SearchResponse(sections, "scrollId", 1, 1, 0, 10, ShardSearchFailure.EMPTY_ARRAY, - SearchResponse.Clusters.EMPTY); + SearchResponse searchResponse = new SearchResponse( + sections, + "scrollId", + 1, + 1, + 0, + 10, + ShardSearchFailure.EMPTY_ARRAY, + SearchResponse.Clusters.EMPTY + ); doAnswer(invocation -> { ActionListener listener = (ActionListener) invocation.getArguments()[2]; listener.onResponse(searchResponse); @@ -219,7 +244,7 @@ void stopExecutor() { assertThat(watches, hasSize(activeWatchCount)); } - @SuppressWarnings({"unchecked", "rawtypes"}) + @SuppressWarnings({ "unchecked", "rawtypes" }) public void testPausingWatcherServiceAlsoPausesTriggerService() { String engineType = "foo"; TriggerEngine triggerEngine = mock(TriggerEngine.class); @@ -240,11 +265,17 @@ public void testPausingWatcherServiceAlsoPausesTriggerService() { triggerService.add(watch); assertThat(triggerService.count(), is(1L)); - WatcherService service = new WatcherService(Settings.EMPTY, triggerService, mock(TriggeredWatchStore.class), - mock(ExecutionService.class), mock(WatchParser.class), client, EsExecutors.DIRECT_EXECUTOR_SERVICE) { + WatcherService service = new WatcherService( + Settings.EMPTY, + triggerService, + mock(TriggeredWatchStore.class), + mock(ExecutionService.class), + mock(WatchParser.class), + client, + EsExecutors.DIRECT_EXECUTOR_SERVICE + ) { @Override - void stopExecutor() { - } + void stopExecutor() {} }; service.pauseExecution("pausing"); @@ -257,11 +288,17 @@ void stopExecutor() { public void testReloadingWatcherDoesNotPauseExecutionService() { ExecutionService executionService = mock(ExecutionService.class); TriggerService triggerService = mock(TriggerService.class); - WatcherService service = new WatcherService(Settings.EMPTY, triggerService, mock(TriggeredWatchStore.class), - executionService, mock(WatchParser.class), client, EsExecutors.DIRECT_EXECUTOR_SERVICE) { + WatcherService service = new WatcherService( + Settings.EMPTY, + triggerService, + mock(TriggeredWatchStore.class), + executionService, + mock(WatchParser.class), + client, + EsExecutors.DIRECT_EXECUTOR_SERVICE + ) { @Override - void stopExecutor() { - } + void stopExecutor() {} }; ClusterState.Builder csBuilder = new ClusterState.Builder(new ClusterName("_name")); @@ -274,11 +311,15 @@ void stopExecutor() { } private static DiscoveryNode newNode() { - return new DiscoveryNode("node", ESTestCase.buildNewFakeTransportAddress(), Collections.emptyMap(), - DiscoveryNodeRole.roles(), Version.CURRENT); + return new DiscoveryNode( + "node", + ESTestCase.buildNewFakeTransportAddress(), + Collections.emptyMap(), + DiscoveryNodeRole.roles(), + Version.CURRENT + ); } - @SuppressWarnings("unchecked") private static ActionListener anyActionListener() { return any(ActionListener.class); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/ActionWrapperTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/ActionWrapperTests.java index a28a1de39e29b..c91525d786cff 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/ActionWrapperTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/ActionWrapperTests.java @@ -7,11 +7,11 @@ package org.elasticsearch.xpack.watcher.actions; import org.elasticsearch.common.Strings; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.watcher.actions.Action; import org.elasticsearch.xpack.core.watcher.actions.ActionStatus; import org.elasticsearch.xpack.core.watcher.actions.ActionStatus.AckStatus.State; @@ -82,18 +82,25 @@ public void testOtherActionsAreNotAffectedOnActionConditionReset() throws Except public void testThatMultipleResultsCanBeReturned() throws Exception { final LoggingAction loggingAction = new LoggingAction(new TextTemplate("{{key}}"), null, null); - final ExecutableAction executableAction = - new ExecutableLoggingAction(loggingAction, logger, new MockTextTemplateEngine()); - ActionWrapper wrapper = new ActionWrapper("_action", null, InternalAlwaysCondition.INSTANCE, null, executableAction, - "ctx.payload.my_path", null); + final ExecutableAction executableAction = new ExecutableLoggingAction( + loggingAction, + logger, + new MockTextTemplateEngine() + ); + ActionWrapper wrapper = new ActionWrapper( + "_action", + null, + InternalAlwaysCondition.INSTANCE, + null, + executableAction, + "ctx.payload.my_path", + null + ); WatchExecutionContext ctx = mockExecutionContent(watch); - Payload.Simple payload = new Payload.Simple(Map.of("my_path", - List.of( - Map.of("key", "first"), - Map.of("key", "second"), - Map.of("key", "third") - ))); + Payload.Simple payload = new Payload.Simple( + Map.of("my_path", List.of(Map.of("key", "first"), Map.of("key", "second"), Map.of("key", "third"))) + ); when(ctx.payload()).thenReturn(payload); ActionWrapperResult result = wrapper.execute(ctx); @@ -112,8 +119,15 @@ public void testThatMultipleResultsCanBeReturned() throws Exception { } public void testThatSpecifiedPathIsNotCollection() { - ActionWrapper wrapper = new ActionWrapper("_action", null, InternalAlwaysCondition.INSTANCE, null, executableAction, - "ctx.payload.my_path", null); + ActionWrapper wrapper = new ActionWrapper( + "_action", + null, + InternalAlwaysCondition.INSTANCE, + null, + executableAction, + "ctx.payload.my_path", + null + ); WatchExecutionContext ctx = mockExecutionContent(watch); Payload.Simple payload = new Payload.Simple(Map.of("my_path", "not a map")); when(ctx.payload()).thenReturn(payload); @@ -123,13 +137,22 @@ public void testThatSpecifiedPathIsNotCollection() { assertThat(result.action().status(), is(Action.Result.Status.FAILURE)); assertThat(result.action(), instanceOf(Action.Result.FailureWithException.class)); Action.Result.FailureWithException failureWithException = (Action.Result.FailureWithException) result.action(); - assertThat(failureWithException.getException().getMessage(), - is("specified foreach object was not a an array/collection: [ctx.payload.my_path]")); + assertThat( + failureWithException.getException().getMessage(), + is("specified foreach object was not a an array/collection: [ctx.payload.my_path]") + ); } public void testEmptyCollection() { - ActionWrapper wrapper = new ActionWrapper("_action", null, InternalAlwaysCondition.INSTANCE, null, executableAction, - "ctx.payload.my_path", null); + ActionWrapper wrapper = new ActionWrapper( + "_action", + null, + InternalAlwaysCondition.INSTANCE, + null, + executableAction, + "ctx.payload.my_path", + null + ); WatchExecutionContext ctx = mockExecutionContent(watch); Payload.Simple payload = new Payload.Simple(Map.of("my_path", Collections.emptyList())); when(ctx.payload()).thenReturn(payload); @@ -139,23 +162,29 @@ public void testEmptyCollection() { assertThat(result.action().status(), is(Action.Result.Status.FAILURE)); assertThat(result.action(), instanceOf(Action.Result.FailureWithException.class)); Action.Result.FailureWithException failureWithException = (Action.Result.FailureWithException) result.action(); - assertThat(failureWithException.getException().getMessage(), - is("foreach object [ctx.payload.my_path] was an empty list, could not run any action")); + assertThat( + failureWithException.getException().getMessage(), + is("foreach object [ctx.payload.my_path] was an empty list, could not run any action") + ); } public void testPartialFailure() throws Exception { - ActionWrapper wrapper = new ActionWrapper("_action", null, InternalAlwaysCondition.INSTANCE, null, executableAction, - "ctx.payload.my_path", null); + ActionWrapper wrapper = new ActionWrapper( + "_action", + null, + InternalAlwaysCondition.INSTANCE, + null, + executableAction, + "ctx.payload.my_path", + null + ); WatchExecutionContext ctx = mockExecutionContent(watch); - Payload.Simple payload = new Payload.Simple(Map.of("my_path", - List.of( - Map.of("key", "first"), - Map.of("key", "second") - ))); + Payload.Simple payload = new Payload.Simple(Map.of("my_path", List.of(Map.of("key", "first"), Map.of("key", "second")))); when(ctx.payload()).thenReturn(payload); when(executableAction.logger()).thenReturn(logger); - final Action.Result firstResult = new LoggingAction.Result.Success("log_message");; + final Action.Result firstResult = new LoggingAction.Result.Success("log_message"); + ; final Payload firstPayload = new Payload.Simple(Map.of("key", "first")); when(executableAction.execute(eq("_action"), eq(ctx), eq(firstPayload))).thenReturn(firstResult); @@ -168,12 +197,20 @@ public void testPartialFailure() throws Exception { } public void testDefaultLimitOfNumberOfActionsExecuted() throws Exception { - ActionWrapper wrapper = new ActionWrapper("_action", null, InternalAlwaysCondition.INSTANCE, null, executableAction, - "ctx.payload.my_path", null); + ActionWrapper wrapper = new ActionWrapper( + "_action", + null, + InternalAlwaysCondition.INSTANCE, + null, + executableAction, + "ctx.payload.my_path", + null + ); WatchExecutionContext ctx = mockExecutionContent(watch); List> itemsPayload = new ArrayList<>(); for (int i = 0; i < 101; i++) { - final Action.Result actionResult = new LoggingAction.Result.Success("log_message " + i);; + final Action.Result actionResult = new LoggingAction.Result.Success("log_message " + i); + ; final Payload singleItemPayload = new Payload.Simple(Map.of("key", String.valueOf(i))); itemsPayload.add(Map.of("key", String.valueOf(i))); when(executableAction.execute(eq("_action"), eq(ctx), eq(singleItemPayload))).thenReturn(actionResult); @@ -205,12 +242,20 @@ public void testDefaultLimitOfNumberOfActionsExecuted() throws Exception { public void testConfiguredLimitOfNumberOfActionsExecuted() throws Exception { int randomMaxIterations = randomIntBetween(1, 1000); - ActionWrapper wrapper = new ActionWrapper("_action", null, InternalAlwaysCondition.INSTANCE, null, executableAction, - "ctx.payload.my_path", randomMaxIterations); + ActionWrapper wrapper = new ActionWrapper( + "_action", + null, + InternalAlwaysCondition.INSTANCE, + null, + executableAction, + "ctx.payload.my_path", + randomMaxIterations + ); WatchExecutionContext ctx = mockExecutionContent(watch); List> itemsPayload = new ArrayList<>(); for (int i = 0; i < randomMaxIterations + 1; i++) { - final Action.Result actionResult = new LoggingAction.Result.Success("log_message " + i);; + final Action.Result actionResult = new LoggingAction.Result.Success("log_message " + i); + ; final Payload singleItemPayload = new Payload.Simple(Map.of("key", String.valueOf(i))); itemsPayload.add(Map.of("key", String.valueOf(i))); when(executableAction.execute(eq("_action"), eq(ctx), eq(singleItemPayload))).thenReturn(actionResult); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/email/EmailActionTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/email/EmailActionTests.java index 9a7477be83ce7..bd98169d230cd 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/email/EmailActionTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/email/EmailActionTests.java @@ -7,6 +7,7 @@ package org.elasticsearch.xpack.watcher.actions.email; import io.netty.handler.codec.http.HttpHeaders; + import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; @@ -14,12 +15,12 @@ import org.elasticsearch.common.io.Streams; import org.elasticsearch.common.settings.ClusterSettings; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.ssl.SSLService; import org.elasticsearch.xpack.core.watcher.actions.Action; import org.elasticsearch.xpack.core.watcher.common.secret.Secret; @@ -88,8 +89,10 @@ public class EmailActionTests extends ESTestCase { @Before public void addEmailAttachmentParsers() { Map> emailAttachmentParsers = new HashMap<>(); - emailAttachmentParsers.put(HttpEmailAttachementParser.TYPE, new HttpEmailAttachementParser(httpClient, - new MockTextTemplateEngine())); + emailAttachmentParsers.put( + HttpEmailAttachementParser.TYPE, + new HttpEmailAttachementParser(httpClient, new MockTextTemplateEngine()) + ); emailAttachmentParsers.put(DataAttachmentParser.TYPE, new DataAttachmentParser()); emailAttachmentParser = new EmailAttachmentsParser(emailAttachmentParsers); } @@ -125,8 +128,14 @@ public void testExecute() throws Exception { EmailAttachments emailAttachments = randomEmailAttachments(); EmailAction action = new EmailAction(email, account, auth, profile, dataAttachment, emailAttachments); - ExecutableEmailAction executable = new ExecutableEmailAction(action, logger, service, engine, htmlSanitizer, - emailAttachmentParser.getParsers()); + ExecutableEmailAction executable = new ExecutableEmailAction( + action, + logger, + service, + engine, + htmlSanitizer, + emailAttachmentParser.getParsers() + ); Map data = new HashMap<>(); Payload payload = new Payload.Simple(data); @@ -136,12 +145,11 @@ public void testExecute() throws Exception { ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC); Wid wid = new Wid("watch1", now); - WatchExecutionContext ctx = mockExecutionContextBuilder("watch1") - .wid(wid) - .payload(payload) - .time("watch1", now) - .metadata(metadata) - .buildMock(); + WatchExecutionContext ctx = mockExecutionContextBuilder("watch1").wid(wid) + .payload(payload) + .time("watch1", now) + .metadata(metadata) + .buildMock(); Map triggerModel = new HashMap<>(); triggerModel.put("triggered_time", now); @@ -196,21 +204,23 @@ public void testParser() throws Exception { Email.Priority priority = randomFrom(Email.Priority.values()); Email.Address[] to = rarely() ? null : Email.AddressList.parse(randomBoolean() ? "to@domain" : "to1@domain,to2@domain").toArray(); Email.Address[] cc = rarely() ? null : Email.AddressList.parse(randomBoolean() ? "cc@domain" : "cc1@domain,cc2@domain").toArray(); - Email.Address[] bcc = rarely() ? null : Email.AddressList.parse( - randomBoolean() ? "bcc@domain" : "bcc1@domain,bcc2@domain").toArray(); - Email.Address[] replyTo = rarely() ? null : Email.AddressList.parse( - randomBoolean() ? "reply@domain" : "reply1@domain,reply2@domain").toArray(); + Email.Address[] bcc = rarely() + ? null + : Email.AddressList.parse(randomBoolean() ? "bcc@domain" : "bcc1@domain,bcc2@domain").toArray(); + Email.Address[] replyTo = rarely() + ? null + : Email.AddressList.parse(randomBoolean() ? "reply@domain" : "reply1@domain,reply2@domain").toArray(); TextTemplate subject = randomBoolean() ? new TextTemplate("_subject") : null; TextTemplate textBody = randomBoolean() ? new TextTemplate("_text_body") : null; TextTemplate htmlBody = randomBoolean() ? new TextTemplate("_text_html") : null; org.elasticsearch.xpack.watcher.notification.email.DataAttachment dataAttachment = randomDataAttachment(); XContentBuilder builder = jsonBuilder().startObject() - .field("account", "_account") - .field("profile", profile.name()) - .field("user", "_user") - .field("password", "_passwd") - .field("from", "from@domain") - .field("priority", priority.name()); + .field("account", "_account") + .field("profile", profile.name()) + .field("user", "_user") + .field("password", "_passwd") + .field("from", "from@domain") + .field("priority", priority.name()); if (dataAttachment != null) { builder.field("attach_data", (ToXContentObject) dataAttachment); } else if (randomBoolean()) { @@ -293,7 +303,7 @@ public void testParser() throws Exception { parser.nextToken(); ExecutableEmailAction executable = new EmailActionFactory(Settings.EMPTY, emailService, engine, emailAttachmentParser) - .parseExecutable(randomAlphaOfLength(8), randomAlphaOfLength(3), parser); + .parseExecutable(randomAlphaOfLength(8), randomAlphaOfLength(3), parser); assertThat(executable, notNullValue()); assertThat(executable.action().getAccount(), is("_account")); @@ -373,8 +383,14 @@ public void testParserSelfGenerated() throws Exception { EmailAttachments emailAttachments = randomEmailAttachments(); EmailAction action = new EmailAction(email, account, auth, profile, dataAttachment, emailAttachments); - ExecutableEmailAction executable = new ExecutableEmailAction(action, logger, service, engine, htmlSanitizer, - emailAttachmentParser.getParsers()); + ExecutableEmailAction executable = new ExecutableEmailAction( + action, + logger, + service, + engine, + htmlSanitizer, + emailAttachmentParser.getParsers() + ); boolean hideSecrets = randomBoolean(); ToXContent.Params params = WatcherParams.builder().hideSecrets(hideSecrets).build(); @@ -386,8 +402,11 @@ public void testParserSelfGenerated() throws Exception { XContentParser parser = createParser(JsonXContent.jsonXContent, bytes); parser.nextToken(); - ExecutableEmailAction parsed = new EmailActionFactory(Settings.EMPTY, service, engine, emailAttachmentParser) - .parseExecutable(randomAlphaOfLength(4), randomAlphaOfLength(10), parser); + ExecutableEmailAction parsed = new EmailActionFactory(Settings.EMPTY, service, engine, emailAttachmentParser).parseExecutable( + randomAlphaOfLength(4), + randomAlphaOfLength(10), + parser + ); if (hideSecrets == false) { assertThat(parsed, equalTo(executable)); @@ -417,8 +436,11 @@ public void testParserInvalid() throws Exception { XContentParser parser = createParser(builder); parser.nextToken(); try { - new EmailActionFactory(Settings.EMPTY, emailService, engine, emailAttachmentsParser) - .parseExecutable(randomAlphaOfLength(3), randomAlphaOfLength(7), parser); + new EmailActionFactory(Settings.EMPTY, emailService, engine, emailAttachmentsParser).parseExecutable( + randomAlphaOfLength(3), + randomAlphaOfLength(7), + parser + ); } catch (ElasticsearchParseException e) { assertThat(e.getMessage(), containsString("unexpected string field [unknown_field]")); } @@ -429,33 +451,36 @@ public void testRequestAttachmentGetsAppendedToEmailAttachments() throws Excepti // setup mock response Map headers = new HashMap<>(1); - headers.put(HttpHeaders.Names.CONTENT_TYPE, new String[]{"plain/text"}); + headers.put(HttpHeaders.Names.CONTENT_TYPE, new String[] { "plain/text" }); String content = "My wonderful text"; HttpResponse mockResponse = new HttpResponse(200, content, headers); when(httpClient.execute(any(HttpRequest.class))).thenReturn(mockResponse); XContentBuilder builder = jsonBuilder().startObject() - .startObject("attachments") - // http attachment - .startObject(attachmentId) - .startObject("http") - .startObject("request") - .field("host", "localhost") - .field("port", 443) - .field("path", "/the/evil/test") - .endObject() - .endObject() - .endObject() - .endObject() - .endObject(); + .startObject("attachments") + // http attachment + .startObject(attachmentId) + .startObject("http") + .startObject("request") + .field("host", "localhost") + .field("port", 443) + .field("path", "/the/evil/test") + .endObject() + .endObject() + .endObject() + .endObject() + .endObject(); XContentParser parser = createParser(builder); logger.info("JSON: {}", Strings.toString(builder)); parser.nextToken(); EmailActionFactory emailActionFactory = createEmailActionFactory(); - ExecutableEmailAction executableEmailAction = - emailActionFactory.parseExecutable(randomAlphaOfLength(3), randomAlphaOfLength(7), parser); + ExecutableEmailAction executableEmailAction = emailActionFactory.parseExecutable( + randomAlphaOfLength(3), + randomAlphaOfLength(7), + parser + ); Action.Result result = executableEmailAction.execute("test", createWatchExecutionContext(), new Payload.Simple()); assertThat(result, instanceOf(EmailAction.Result.Success.class)); @@ -476,21 +501,24 @@ public void testThatDataAttachmentGetsAttachedWithId() throws Exception { String attachmentId = randomAlphaOfLength(10) + ".yml"; XContentBuilder builder = jsonBuilder().startObject() - .startObject("attachments") - .startObject(attachmentId) - .startObject("data") - .endObject() - .endObject() - .endObject() - .endObject(); + .startObject("attachments") + .startObject(attachmentId) + .startObject("data") + .endObject() + .endObject() + .endObject() + .endObject(); XContentParser parser = createParser(builder); logger.info("JSON: {}", Strings.toString(builder)); parser.nextToken(); EmailActionFactory emailActionFactory = createEmailActionFactory(); - ExecutableEmailAction executableEmailAction = - emailActionFactory.parseExecutable(randomAlphaOfLength(3), randomAlphaOfLength(7), parser); + ExecutableEmailAction executableEmailAction = emailActionFactory.parseExecutable( + randomAlphaOfLength(3), + randomAlphaOfLength(7), + parser + ); Action.Result result = executableEmailAction.execute("test", createWatchExecutionContext(), new Payload.Simple()); assertThat(result, instanceOf(EmailAction.Result.Success.class)); @@ -512,10 +540,9 @@ public void testThatOneFailedEmailAttachmentResultsInActionFailure() throws Exce // setup mock response, second one is an error Map headers = new HashMap<>(1); - headers.put(HttpHeaders.Names.CONTENT_TYPE, new String[]{"plain/text"}); - when(httpClient.execute(any(HttpRequest.class))) - .thenReturn(new HttpResponse(200, "body", headers)) - .thenReturn(new HttpResponse(403)); + headers.put(HttpHeaders.Names.CONTENT_TYPE, new String[] { "plain/text" }); + when(httpClient.execute(any(HttpRequest.class))).thenReturn(new HttpResponse(200, "body", headers)) + .thenReturn(new HttpResponse(403)); // setup email attachment parsers Map> attachmentParsers = new HashMap<>(); @@ -523,42 +550,46 @@ public void testThatOneFailedEmailAttachmentResultsInActionFailure() throws Exce EmailAttachmentsParser emailAttachmentsParser = new EmailAttachmentsParser(attachmentParsers); XContentBuilder builder = jsonBuilder().startObject() - .startObject("attachments") - .startObject("first") - .startObject("http") - .startObject("request").field("url", "http://localhost/first").endObject() - .endObject() - .endObject() - .startObject("second") - .startObject("http") - .startObject("request").field("url", "http://localhost/second").endObject() - .endObject() - .endObject() - .endObject() - .endObject(); + .startObject("attachments") + .startObject("first") + .startObject("http") + .startObject("request") + .field("url", "http://localhost/first") + .endObject() + .endObject() + .endObject() + .startObject("second") + .startObject("http") + .startObject("request") + .field("url", "http://localhost/second") + .endObject() + .endObject() + .endObject() + .endObject() + .endObject(); XContentParser parser = createParser(builder); parser.nextToken(); ExecutableEmailAction executableEmailAction = new EmailActionFactory(Settings.EMPTY, emailService, engine, emailAttachmentsParser) - .parseExecutable(randomAlphaOfLength(3), randomAlphaOfLength(7), parser); + .parseExecutable(randomAlphaOfLength(3), randomAlphaOfLength(7), parser); ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC); Wid wid = new Wid(randomAlphaOfLength(5), now); Map metadata = MapBuilder.newMapBuilder().put("_key", "_val").map(); - WatchExecutionContext ctx = mockExecutionContextBuilder("watch1") - .wid(wid) - .payload(new Payload.Simple()) - .time("watch1", now) - .metadata(metadata) - .buildMock(); + WatchExecutionContext ctx = mockExecutionContextBuilder("watch1").wid(wid) + .payload(new Payload.Simple()) + .time("watch1", now) + .metadata(metadata) + .buildMock(); Action.Result result = executableEmailAction.execute("test", ctx, new Payload.Simple()); assertThat(result, instanceOf(EmailAction.Result.FailureWithException.class)); EmailAction.Result.FailureWithException failure = (EmailAction.Result.FailureWithException) result; - assertThat(failure.getException().getMessage(), - is("Watch[watch1] attachment[second] HTTP error status host[localhost], port[80], method[GET], path[/second], " + - "status[403]")); + assertThat( + failure.getException().getMessage(), + is("Watch[watch1] attachment[second] HTTP error status host[localhost], port[80], method[GET], path[/second], " + "status[403]") + ); } private EmailActionFactory createEmailActionFactory() { @@ -572,17 +603,19 @@ private WatchExecutionContext createWatchExecutionContext() { ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC); Wid wid = new Wid(randomAlphaOfLength(5), now); Map metadata = MapBuilder.newMapBuilder().put("_key", "_val").map(); - return mockExecutionContextBuilder("watch1") - .wid(wid) - .payload(new Payload.Simple()) - .time("watch1", now) - .metadata(metadata) - .buildMock(); + return mockExecutionContextBuilder("watch1").wid(wid) + .payload(new Payload.Simple()) + .time("watch1", now) + .metadata(metadata) + .buildMock(); } static org.elasticsearch.xpack.watcher.notification.email.DataAttachment randomDataAttachment() { - return randomFrom(org.elasticsearch.xpack.watcher.notification.email.DataAttachment.JSON, - org.elasticsearch.xpack.watcher.notification.email.DataAttachment.YAML, null); + return randomFrom( + org.elasticsearch.xpack.watcher.notification.email.DataAttachment.JSON, + org.elasticsearch.xpack.watcher.notification.email.DataAttachment.YAML, + null + ); } private EmailAttachments randomEmailAttachments() throws IOException { @@ -591,18 +624,25 @@ private EmailAttachments randomEmailAttachments() throws IOException { String attachmentType = randomFrom("http", "data", null); if ("http".equals(attachmentType)) { Map headers = new HashMap<>(1); - headers.put(HttpHeaders.Names.CONTENT_TYPE, new String[]{"plain/text"}); + headers.put(HttpHeaders.Names.CONTENT_TYPE, new String[] { "plain/text" }); String content = "My wonderful text"; HttpResponse mockResponse = new HttpResponse(200, content, headers); when(httpClient.execute(any(HttpRequest.class))).thenReturn(mockResponse); HttpRequestTemplate template = HttpRequestTemplate.builder("localhost", 1234).build(); - attachments.add(new HttpRequestAttachment(randomAlphaOfLength(10), template, - randomBoolean(), randomFrom("my/custom-type", null))); + attachments.add( + new HttpRequestAttachment(randomAlphaOfLength(10), template, randomBoolean(), randomFrom("my/custom-type", null)) + ); } else if ("data".equals(attachmentType)) { - attachments.add(new org.elasticsearch.xpack.watcher.notification.email.attachment.DataAttachment(randomAlphaOfLength(10), - randomFrom(org.elasticsearch.xpack.watcher.notification.email.DataAttachment.JSON, org.elasticsearch.xpack.watcher - .notification.email.DataAttachment.YAML))); + attachments.add( + new org.elasticsearch.xpack.watcher.notification.email.attachment.DataAttachment( + randomAlphaOfLength(10), + randomFrom( + org.elasticsearch.xpack.watcher.notification.email.DataAttachment.JSON, + org.elasticsearch.xpack.watcher.notification.email.DataAttachment.YAML + ) + ) + ); } return new EmailAttachments(attachments); @@ -611,8 +651,12 @@ private EmailAttachments randomEmailAttachments() throws IOException { public static class NoopEmailService extends EmailService { public NoopEmailService() { - super(Settings.EMPTY, null, mock(SSLService.class), - new ClusterSettings(Settings.EMPTY, new HashSet<>(EmailService.getSettings()))); + super( + Settings.EMPTY, + null, + mock(SSLService.class), + new ClusterSettings(Settings.EMPTY, new HashSet<>(EmailService.getSettings())) + ); } @Override diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/email/EmailMessageIdTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/email/EmailMessageIdTests.java index a8b3f8a95f9e3..7915ae4cfff55 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/email/EmailMessageIdTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/email/EmailMessageIdTests.java @@ -24,13 +24,14 @@ import org.junit.After; import org.junit.Before; -import javax.mail.internet.MimeMessage; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; +import javax.mail.internet.MimeMessage; + import static org.hamcrest.Matchers.hasSize; import static org.mockito.Mockito.mock; @@ -49,19 +50,23 @@ public void startSmtpServer() { final MockSecureSettings secureSettings = new MockSecureSettings(); secureSettings.setString("xpack.notification.email.account.test.smtp.secure_password", EmailServer.PASSWORD); Settings settings = Settings.builder() - .put("xpack.notification.email.account.test.smtp.auth", true) - .put("xpack.notification.email.account.test.smtp.user", EmailServer.USERNAME) - .put("xpack.notification.email.account.test.smtp.port", server.port()) - .put("xpack.notification.email.account.test.smtp.host", "localhost") - .setSecureSettings(secureSettings) - .build(); + .put("xpack.notification.email.account.test.smtp.auth", true) + .put("xpack.notification.email.account.test.smtp.user", EmailServer.USERNAME) + .put("xpack.notification.email.account.test.smtp.port", server.port()) + .put("xpack.notification.email.account.test.smtp.host", "localhost") + .setSecureSettings(secureSettings) + .build(); Set> registeredSettings = new HashSet<>(ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); registeredSettings.addAll(EmailService.getSettings()); ClusterSettings clusterSettings = new ClusterSettings(settings, registeredSettings); emailService = new EmailService(settings, null, mock(SSLService.class), clusterSettings); - EmailTemplate emailTemplate = EmailTemplate.builder().from("from@example.org").to("to@example.org") - .subject("subject").textBody("body").build(); + EmailTemplate emailTemplate = EmailTemplate.builder() + .from("from@example.org") + .to("to@example.org") + .subject("subject") + .textBody("body") + .build(); emailAction = new EmailAction(emailTemplate, null, null, null, null, null); } @@ -73,10 +78,22 @@ public void stopSmtpServer() { public void testThatMessageIdIsUnique() throws Exception { List messages = new ArrayList<>(); server.addListener(messages::add); - ExecutableEmailAction firstEmailAction = new ExecutableEmailAction(emailAction, logger, emailService, textTemplateEngine, - htmlSanitizer, Collections.emptyMap()); - ExecutableEmailAction secondEmailAction = new ExecutableEmailAction(emailAction, logger, emailService, textTemplateEngine, - htmlSanitizer, Collections.emptyMap()); + ExecutableEmailAction firstEmailAction = new ExecutableEmailAction( + emailAction, + logger, + emailService, + textTemplateEngine, + htmlSanitizer, + Collections.emptyMap() + ); + ExecutableEmailAction secondEmailAction = new ExecutableEmailAction( + emailAction, + logger, + emailService, + textTemplateEngine, + htmlSanitizer, + Collections.emptyMap() + ); WatchExecutionContext ctx = WatcherTestUtils.createWatchExecutionContext(); firstEmailAction.execute("my_first_action_id", ctx, Payload.EMPTY); @@ -91,4 +108,3 @@ public void testThatMessageIdIsUnique() throws Exception { assertThat(messageIds, hasSize(2)); } } - diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/email/EmailSslTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/email/EmailSslTests.java index 2668a0f2874f9..b00c54bf500fd 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/email/EmailSslTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/email/EmailSslTests.java @@ -38,6 +38,7 @@ import java.util.HashSet; import java.util.List; import java.util.Set; + import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import javax.net.ssl.SSLContext; @@ -78,8 +79,10 @@ public void testFailureSendingMessageToSmtpServerWithUntrustedCertificateAuthori final MockSecureSettings secureSettings = new MockSecureSettings(); final ExecutableEmailAction emailAction = buildEmailAction(settings, secureSettings); final WatchExecutionContext ctx = WatcherTestUtils.createWatchExecutionContext(); - final MessagingException exception = expectThrows(MessagingException.class, - () -> emailAction.execute("my_action_id", ctx, Payload.EMPTY)); + final MessagingException exception = expectThrows( + MessagingException.class, + () -> emailAction.execute("my_action_id", ctx, Payload.EMPTY) + ); final List allCauses = getAllCauses(exception); assertThat(allCauses, Matchers.hasItem(Matchers.instanceOf(SSLException.class))); } @@ -128,8 +131,7 @@ public void testCanSendMessageToSmtpServerUsingSmtpSslTrust() throws Exception { List messages = new ArrayList<>(); server.addListener(messages::add); try { - final Settings.Builder settings = Settings.builder() - .put("xpack.notification.email.account.test.smtp.ssl.trust", "localhost"); + final Settings.Builder settings = Settings.builder().put("xpack.notification.email.account.test.smtp.ssl.trust", "localhost"); final MockSecureSettings secureSettings = new MockSecureSettings(); ExecutableEmailAction emailAction = buildEmailAction(settings, secureSettings); @@ -159,8 +161,10 @@ public void testNotificationSslSettingsOverrideSmtpSslTrust() throws Exception { ExecutableEmailAction emailAction = buildEmailAction(settings, secureSettings); WatchExecutionContext ctx = WatcherTestUtils.createWatchExecutionContext(); - final MessagingException exception = expectThrows(MessagingException.class, - () -> emailAction.execute("my_action_id", ctx, Payload.EMPTY)); + final MessagingException exception = expectThrows( + MessagingException.class, + () -> emailAction.execute("my_action_id", ctx, Payload.EMPTY) + ); final List allCauses = getAllCauses(exception); assertThat(allCauses, Matchers.hasItem(Matchers.instanceOf(SSLException.class))); @@ -171,8 +175,7 @@ public void testNotificationSslSettingsOverrideSmtpSslTrust() throws Exception { private ExecutableEmailAction buildEmailAction(Settings.Builder baseSettings, MockSecureSettings secureSettings) { secureSettings.setString("xpack.notification.email.account.test.smtp.secure_password", EmailServer.PASSWORD); - Settings settings = baseSettings - .put("path.home", createTempDir()) + Settings settings = baseSettings.put("path.home", createTempDir()) .put("xpack.notification.email.account.test.smtp.auth", true) .put("xpack.notification.email.account.test.smtp.user", EmailServer.USERNAME) .put("xpack.notification.email.account.test.smtp.port", server.port()) @@ -185,8 +188,12 @@ private ExecutableEmailAction buildEmailAction(Settings.Builder baseSettings, Mo ClusterSettings clusterSettings = new ClusterSettings(settings, registeredSettings); SSLService sslService = new SSLService(TestEnvironment.newEnvironment(settings)); final EmailService emailService = new EmailService(settings, null, sslService, clusterSettings); - EmailTemplate emailTemplate = EmailTemplate.builder().from("from@example.org").to("to@example.org") - .subject("subject").textBody("body").build(); + EmailTemplate emailTemplate = EmailTemplate.builder() + .from("from@example.org") + .to("to@example.org") + .subject("subject") + .textBody("body") + .build(); final EmailAction emailAction = new EmailAction(emailTemplate, null, null, null, null, null); return new ExecutableEmailAction(emailAction, logger, emailService, textTemplateEngine, htmlSanitizer, Collections.emptyMap()); } @@ -202,4 +209,3 @@ private List getAllCauses(Exception exception) { } } - diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/index/IndexActionTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/index/IndexActionTests.java index 4966fb0a61713..35ebcca692003 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/index/IndexActionTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/index/IndexActionTests.java @@ -19,15 +19,15 @@ import org.elasticsearch.common.collect.MapBuilder; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.time.DateUtils; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.Maps; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.Index; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.watcher.actions.Action; import org.elasticsearch.xpack.core.watcher.actions.Action.Result.Status; import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext; @@ -117,44 +117,37 @@ public void testParser() throws Exception { public void testParserFailure() throws Exception { // wrong type for field - expectParseFailure(jsonBuilder() - .startObject() - .field(IndexAction.Field.TIMEOUT.getPreferredName(), "1234") - .endObject()); + expectParseFailure(jsonBuilder().startObject().field(IndexAction.Field.TIMEOUT.getPreferredName(), "1234").endObject()); // unknown field - expectParseFailure(jsonBuilder() - .startObject() - .field("unknown", "whatever") - .endObject()); + expectParseFailure(jsonBuilder().startObject().field("unknown", "whatever").endObject()); - expectParseFailure(jsonBuilder() - .startObject() - .field("unknown", 1234) - .endObject()); + expectParseFailure(jsonBuilder().startObject().field("unknown", 1234).endObject()); // unknown refresh policy - expectFailure(IllegalArgumentException.class, jsonBuilder() - .startObject() - .field(IndexAction.Field.REFRESH.getPreferredName(), "unknown") - .endObject()); + expectFailure( + IllegalArgumentException.class, + jsonBuilder().startObject().field(IndexAction.Field.REFRESH.getPreferredName(), "unknown").endObject() + ); } public void testOpTypeThatCannotBeParsed() throws Exception { - expectParseFailure(jsonBuilder() - .startObject() - .field(IndexAction.Field.OP_TYPE.getPreferredName(), randomAlphaOfLength(10)) - .endObject(), - "failed to parse op_type value for field [op_type]"); + expectParseFailure( + jsonBuilder().startObject().field(IndexAction.Field.OP_TYPE.getPreferredName(), randomAlphaOfLength(10)).endObject(), + "failed to parse op_type value for field [op_type]" + ); } public void testUnsupportedOpType() throws Exception { - expectParseFailure(jsonBuilder() - .startObject() - .field(IndexAction.Field.OP_TYPE.getPreferredName(), - randomFrom(DocWriteRequest.OpType.UPDATE.name(), DocWriteRequest.OpType.DELETE.name())) - .endObject(), - "op_type value for field [op_type] must be [index] or [create]"); + expectParseFailure( + jsonBuilder().startObject() + .field( + IndexAction.Field.OP_TYPE.getPreferredName(), + randomFrom(DocWriteRequest.OpType.UPDATE.name(), DocWriteRequest.OpType.DELETE.name()) + ) + .endObject(), + "op_type value for field [op_type] must be [index] or [create]" + ); } private void expectParseFailure(XContentBuilder builder, String expectedMessage) throws Exception { @@ -181,11 +174,14 @@ private void expectFailure(Class clazz, XContentBuilder bui public void testUsingParameterIdWithBulkOrIdFieldThrowsIllegalState() { final IndexAction action = new IndexAction("test-index", "123", null, null, null, null, refreshPolicy); - final ExecutableIndexAction executable = new ExecutableIndexAction(action, logger, client, - TimeValue.timeValueSeconds(30), TimeValue.timeValueSeconds(30)); - final Map docWithId = Map.of( - "foo", "bar", - "_id", "0"); + final ExecutableIndexAction executable = new ExecutableIndexAction( + action, + logger, + client, + TimeValue.timeValueSeconds(30), + TimeValue.timeValueSeconds(30) + ); + final Map docWithId = Map.of("foo", "bar", "_id", "0"); final ZonedDateTime executionTime = ZonedDateTime.now(ZoneOffset.UTC); // using doc_id with bulk fails regardless of using ID @@ -193,10 +189,10 @@ public void testUsingParameterIdWithBulkOrIdFieldThrowsIllegalState() { final List> idList = Arrays.asList(docWithId, MapBuilder.newMapBuilder().put("foo", "bar1").put("_id", "1").map()); final Object list = randomFrom( - new Map[] { singletonMap("foo", "bar"), singletonMap("foo", "bar1") }, - Arrays.asList(singletonMap("foo", "bar"), singletonMap("foo", "bar1")), - unmodifiableSet(newHashSet(singletonMap("foo", "bar"), singletonMap("foo", "bar1"))), - idList + new Map[] { singletonMap("foo", "bar"), singletonMap("foo", "bar1") }, + Arrays.asList(singletonMap("foo", "bar"), singletonMap("foo", "bar1")), + unmodifiableSet(newHashSet(singletonMap("foo", "bar"), singletonMap("foo", "bar1"))), + idList ); final WatchExecutionContext ctx = WatcherTestUtils.mockExecutionContext("_id", executionTime, new Payload.Simple("_doc", list)); @@ -226,11 +222,22 @@ public void testThatIndexTypeIdDynamically() throws Exception { entries.add(entry("_index", "my_dynamic_index")); } - final IndexAction action = new IndexAction(configureIndexDynamically ? null : "my_index", - configureIdDynamically ? null : "my_id", - null, null, null, null, refreshPolicy); - final ExecutableIndexAction executable = new ExecutableIndexAction(action, logger, client, - TimeValue.timeValueSeconds(30), TimeValue.timeValueSeconds(30)); + final IndexAction action = new IndexAction( + configureIndexDynamically ? null : "my_index", + configureIdDynamically ? null : "my_id", + null, + null, + null, + null, + refreshPolicy + ); + final ExecutableIndexAction executable = new ExecutableIndexAction( + action, + logger, + client, + TimeValue.timeValueSeconds(30), + TimeValue.timeValueSeconds(30) + ); final WatchExecutionContext ctx = WatcherTestUtils.mockExecutionContext("_id", new Payload.Simple(Maps.ofEntries(entries))); @@ -249,19 +256,26 @@ public void testThatIndexTypeIdDynamically() throws Exception { public void testThatIndexActionCanBeConfiguredWithDynamicIndexNameAndBulk() throws Exception { final IndexAction action = new IndexAction(null, null, null, null, null, null, refreshPolicy); - final ExecutableIndexAction executable = new ExecutableIndexAction(action, logger, client, - TimeValue.timeValueSeconds(30), TimeValue.timeValueSeconds(30)); + final ExecutableIndexAction executable = new ExecutableIndexAction( + action, + logger, + client, + TimeValue.timeValueSeconds(30), + TimeValue.timeValueSeconds(30) + ); final Map docWithIndex = Map.of("foo", "bar", "_index", "my-index"); final Map docWithOtherIndex = Map.of("foo", "bar", "_index", "my-other-index"); - final WatchExecutionContext ctx = WatcherTestUtils.mockExecutionContext("_id", - new Payload.Simple("_doc", Arrays.asList(docWithIndex, docWithOtherIndex))); + final WatchExecutionContext ctx = WatcherTestUtils.mockExecutionContext( + "_id", + new Payload.Simple("_doc", Arrays.asList(docWithIndex, docWithOtherIndex)) + ); ArgumentCaptor captor = ArgumentCaptor.forClass(BulkRequest.class); PlainActionFuture listener = PlainActionFuture.newFuture(); IndexResponse indexResponse = new IndexResponse(new ShardId(new Index("foo", "bar"), 0), "whatever", 1, 1, 1, true); BulkItemResponse response = BulkItemResponse.success(0, DocWriteRequest.OpType.INDEX, indexResponse); - BulkResponse bulkResponse = new BulkResponse(new BulkItemResponse[]{response}, 1); + BulkResponse bulkResponse = new BulkResponse(new BulkItemResponse[] { response }, 1); listener.onResponse(bulkResponse); when(client.bulk(captor.capture())).thenReturn(listener); Action.Result result = executable.execute("_id", ctx, ctx.payload()); @@ -275,18 +289,28 @@ public void testThatIndexActionCanBeConfiguredWithDynamicIndexNameAndBulk() thro public void testConfigureIndexInMapAndAction() { String fieldName = "_index"; - final IndexAction action = new IndexAction("my_index", - null, null,null, null, null, refreshPolicy); - final ExecutableIndexAction executable = new ExecutableIndexAction(action, logger, client, - TimeValue.timeValueSeconds(30), TimeValue.timeValueSeconds(30)); + final IndexAction action = new IndexAction("my_index", null, null, null, null, null, refreshPolicy); + final ExecutableIndexAction executable = new ExecutableIndexAction( + action, + logger, + client, + TimeValue.timeValueSeconds(30), + TimeValue.timeValueSeconds(30) + ); final Map docWithIndex = Map.of("foo", "bar", fieldName, "my-value"); - final WatchExecutionContext ctx = WatcherTestUtils.mockExecutionContext("_id", - new Payload.Simple("_doc", Collections.singletonList(docWithIndex))); + final WatchExecutionContext ctx = WatcherTestUtils.mockExecutionContext( + "_id", + new Payload.Simple("_doc", Collections.singletonList(docWithIndex)) + ); IllegalStateException e = expectThrows(IllegalStateException.class, () -> executable.execute("_id", ctx, ctx.payload())); - assertThat(e.getMessage(), startsWith("could not execute action [_id] of watch [_id]. [ctx.payload." + - fieldName + "] or [ctx.payload._doc." + fieldName + "]")); + assertThat( + e.getMessage(), + startsWith( + "could not execute action [_id] of watch [_id]. [ctx.payload." + fieldName + "] or [ctx.payload._doc." + fieldName + "]" + ) + ); } public void testIndexActionExecuteSingleDoc() throws Exception { @@ -296,8 +320,13 @@ public void testIndexActionExecuteSingleDoc() throws Exception { String timestampField = randomFrom("@timestamp", null); IndexAction action = new IndexAction("test-index", docIdAsParam ? docId : null, null, timestampField, null, null, refreshPolicy); - ExecutableIndexAction executable = new ExecutableIndexAction(action, logger, client, TimeValue.timeValueSeconds(30), - TimeValue.timeValueSeconds(30)); + ExecutableIndexAction executable = new ExecutableIndexAction( + action, + logger, + client, + TimeValue.timeValueSeconds(30), + TimeValue.timeValueSeconds(30) + ); ZonedDateTime executionTime = DateUtils.nowWithMillisResolution(); Payload payload; @@ -321,7 +350,7 @@ public void testIndexActionExecuteSingleDoc() throws Exception { assertThat(result, instanceOf(IndexAction.Result.class)); IndexAction.Result successResult = (IndexAction.Result) result; XContentSource response = successResult.response(); - assertThat(response.getValue("created"), equalTo((Object)Boolean.TRUE)); + assertThat(response.getValue("created"), equalTo((Object) Boolean.TRUE)); assertThat(response.getValue("version"), equalTo((Object) 1)); assertThat(response.getValue("index").toString(), equalTo("test-index")); @@ -332,7 +361,7 @@ public void testIndexActionExecuteSingleDoc() throws Exception { assertThat(indexRequest.id(), is(docId)); } - RefreshPolicy expectedRefreshPolicy = refreshPolicy == null ? RefreshPolicy.NONE: refreshPolicy; + RefreshPolicy expectedRefreshPolicy = refreshPolicy == null ? RefreshPolicy.NONE : refreshPolicy; assertThat(indexRequest.getRefreshPolicy(), is(expectedRefreshPolicy)); if (timestampField != null) { @@ -345,8 +374,13 @@ public void testIndexActionExecuteSingleDoc() throws Exception { public void testFailureResult() throws Exception { IndexAction action = new IndexAction("test-index", null, null, "@timestamp", null, null, refreshPolicy); - ExecutableIndexAction executable = new ExecutableIndexAction(action, logger, client, - TimeValue.timeValueSeconds(30), TimeValue.timeValueSeconds(30)); + ExecutableIndexAction executable = new ExecutableIndexAction( + action, + logger, + client, + TimeValue.timeValueSeconds(30), + TimeValue.timeValueSeconds(30) + ); // should the result resemble a failure or a partial failure boolean isPartialFailure = randomBoolean(); @@ -360,8 +394,7 @@ public void testFailureResult() throws Exception { ArgumentCaptor captor = ArgumentCaptor.forClass(BulkRequest.class); PlainActionFuture listener = PlainActionFuture.newFuture(); - BulkItemResponse.Failure failure = new BulkItemResponse.Failure("test-index", "anything", - new ElasticsearchException("anything")); + BulkItemResponse.Failure failure = new BulkItemResponse.Failure("test-index", "anything", new ElasticsearchException("anything")); BulkItemResponse firstResponse = BulkItemResponse.failure(0, DocWriteRequest.OpType.INDEX, failure); BulkItemResponse secondResponse; if (isPartialFailure) { @@ -371,11 +404,11 @@ public void testFailureResult() throws Exception { } else { secondResponse = BulkItemResponse.failure(1, DocWriteRequest.OpType.INDEX, failure); } - BulkResponse bulkResponse = new BulkResponse(new BulkItemResponse[]{firstResponse, secondResponse}, 1); + BulkResponse bulkResponse = new BulkResponse(new BulkItemResponse[] { firstResponse, secondResponse }, 1); listener.onResponse(bulkResponse); when(client.bulk(captor.capture())).thenReturn(listener); Action.Result result = executable.execute("_id", ctx, payload); - RefreshPolicy expectedRefreshPolicy = refreshPolicy == null ? RefreshPolicy.NONE: refreshPolicy; + RefreshPolicy expectedRefreshPolicy = refreshPolicy == null ? RefreshPolicy.NONE : refreshPolicy; assertThat(captor.getValue().getRefreshPolicy(), is(expectedRefreshPolicy)); if (isPartialFailure) { @@ -387,8 +420,13 @@ public void testFailureResult() throws Exception { public void testIndexSeveralDocumentsIsSimulated() throws Exception { IndexAction action = new IndexAction("test-index", null, null, "@timestamp", null, null, refreshPolicy); - ExecutableIndexAction executable = new ExecutableIndexAction(action, logger, client, - TimeValue.timeValueSeconds(30), TimeValue.timeValueSeconds(30)); + ExecutableIndexAction executable = new ExecutableIndexAction( + action, + logger, + client, + TimeValue.timeValueSeconds(30), + TimeValue.timeValueSeconds(30) + ); String docId = randomAlphaOfLength(5); final List> docs = List.of(Map.of("foo", "bar", "_id", docId)); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/jira/ExecutableJiraActionTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/jira/ExecutableJiraActionTests.java index 1d198a2672207..68a267cb0cec4 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/jira/ExecutableJiraActionTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/jira/ExecutableJiraActionTests.java @@ -81,11 +81,10 @@ public void testProxy() throws Exception { ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC); Wid wid = new Wid(randomAlphaOfLength(5), now); - WatchExecutionContext ctx = mockExecutionContextBuilder(wid.watchId()) - .wid(wid) - .payload(new Payload.Simple()) - .time(wid.watchId(), now) - .buildMock(); + WatchExecutionContext ctx = mockExecutionContextBuilder(wid.watchId()).wid(wid) + .payload(new Payload.Simple()) + .time(wid.watchId(), now) + .buildMock(); ExecutableJiraAction executable = new ExecutableJiraAction(action, logger, service, new UpperCaseTextTemplateEngine()); executable.execute("foo", ctx, new Payload.Simple()); @@ -226,39 +225,34 @@ public void testExecutionFieldsListsNotOverridden() throws Exception { } public void testExecutionFieldsStringArrays() throws Exception { - Settings build = Settings.builder() - .putList("k0", "a", "b", "c") - .put("k1", "v1") - .build(); + Settings build = Settings.builder().putList("k0", "a", "b", "c").put("k1", "v1").build(); Map defaults = build.keySet().stream().collect(Collectors.toMap(Function.identity(), k -> build.get(k))); Map fields = new HashMap<>(); fields.put("k2", "v2"); - fields.put("k3", new String[]{"d", "e", "f"}); + fields.put("k3", new String[] { "d", "e", "f" }); JiraAction.Simulated result = simulateExecution(fields, defaults); assertThat(result.getFields().get("K1"), equalTo("V1")); assertThat(result.getFields().get("K2"), equalTo("V2")); - assertArrayEquals((Object[]) result.getFields().get("K3"), new Object[]{"D", "E", "F"}); + assertArrayEquals((Object[]) result.getFields().get("K3"), new Object[] { "D", "E", "F" }); } public void testExecutionFieldsStringArraysNotOverridden() throws Exception { - Settings build = Settings.builder() - .putList("k0", "a", "b", "c") - .build(); + Settings build = Settings.builder().putList("k0", "a", "b", "c").build(); Map defaults = build.keySet().stream().collect(Collectors.toMap(Function.identity(), k -> build.get(k))); Map fields = new HashMap<>(); fields.put("k1", "v1"); - fields.put("k0", new String[]{"d", "e", "f"}); // should not be overridden byt the defaults + fields.put("k0", new String[] { "d", "e", "f" }); // should not be overridden byt the defaults JiraAction.Simulated result = simulateExecution(fields, defaults); final Map expected = new HashMap<>(); - expected.put("K0", new String[]{"D", "E", "F"}); + expected.put("K0", new String[] { "D", "E", "F" }); expected.put("K1", "V1"); - assertArrayEquals((Object[]) result.getFields().get("K0"), new Object[]{"D", "E", "F"}); + assertArrayEquals((Object[]) result.getFields().get("K0"), new Object[] { "D", "E", "F" }); assertThat(result.getFields().get("K1"), equalTo("V1")); } @@ -268,8 +262,8 @@ private JiraAction.Simulated simulateExecution(Map actionFields, secureSettings.setString(JiraAccount.SECURE_USER_SETTING.getKey(), "elastic"); secureSettings.setString(JiraAccount.SECURE_PASSWORD_SETTING.getKey(), "secret"); Settings.Builder settings = Settings.builder() - .setSecureSettings(secureSettings) - .putProperties(accountFields, s -> "issue_defaults." + s); + .setSecureSettings(secureSettings) + .putProperties(accountFields, s -> "issue_defaults." + s); JiraAccount account = new JiraAccount("account", settings.build(), mock(HttpClient.class)); @@ -292,12 +286,11 @@ private WatchExecutionContext createWatchExecutionContext() { ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC); Wid wid = new Wid(randomAlphaOfLength(5), now); Map metadata = MapBuilder.newMapBuilder().put("_key", "_val").map(); - return mockExecutionContextBuilder("watch1") - .wid(wid) - .payload(new Payload.Simple()) - .time("watch1", now) - .metadata(metadata) - .buildMock(); + return mockExecutionContextBuilder("watch1").wid(wid) + .payload(new Payload.Simple()) + .time("watch1", now) + .metadata(metadata) + .buildMock(); } /** diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/jira/JiraActionFactoryTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/jira/JiraActionFactoryTests.java index 6d0139420db1a..e28269e24e567 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/jira/JiraActionFactoryTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/jira/JiraActionFactoryTests.java @@ -6,16 +6,16 @@ */ package org.elasticsearch.xpack.watcher.actions.jira; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.watcher.notification.jira.JiraAccount; import org.elasticsearch.xpack.watcher.notification.jira.JiraService; import org.junit.Before; import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; -import static org.elasticsearch.xpack.watcher.notification.jira.JiraAccountTests.randomIssueDefaults; import static org.elasticsearch.xpack.watcher.actions.ActionBuilders.jiraAction; +import static org.elasticsearch.xpack.watcher.notification.jira.JiraAccountTests.randomIssueDefaults; import static org.hamcrest.Matchers.equalTo; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/jira/JiraActionTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/jira/JiraActionTests.java index 96c1598041538..c30123ad8720e 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/jira/JiraActionTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/jira/JiraActionTests.java @@ -12,12 +12,12 @@ import org.elasticsearch.common.settings.MockSecureSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.Maps; +import org.elasticsearch.script.ScriptService; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.script.ScriptService; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.watcher.actions.Action; import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext; import org.elasticsearch.xpack.core.watcher.execution.Wid; @@ -62,10 +62,7 @@ public void testParser() throws Exception { final String accountName = randomAlphaOfLength(10); final Map issueDefaults = JiraAccountTests.randomIssueDefaults(); - XContentBuilder builder = jsonBuilder().startObject() - .field("account", accountName) - .field("fields", issueDefaults) - .endObject(); + XContentBuilder builder = jsonBuilder().startObject().field("account", accountName).field("fields", issueDefaults).endObject(); BytesReference bytes = BytesReference.bytes(builder); logger.info("jira action json [{}]", bytes.utf8ToString()); @@ -226,9 +223,9 @@ public void testExecute() throws Exception { secureSettings.setString("secure_password", "secret"); Settings.Builder settings = Settings.builder() - .setSecureSettings(secureSettings) - .put("issue_defaults.customfield_000", "foo") - .put("issue_defaults.customfield_001", "bar"); + .setSecureSettings(secureSettings) + .put("issue_defaults.customfield_000", "foo") + .put("issue_defaults.customfield_001", "bar"); JiraAccount account = new JiraAccount("account", settings.build(), httpClient); @@ -243,12 +240,11 @@ public void testExecute() throws Exception { ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC); - Wid wid = new Wid(randomAlphaOfLength(5), now); - WatchExecutionContext context = mockExecutionContextBuilder(wid.watchId()) - .wid(wid) - .payload(payload) - .time(wid.watchId(), now) - .buildMock(); + Wid wid = new Wid(randomAlphaOfLength(5), now); + WatchExecutionContext context = mockExecutionContextBuilder(wid.watchId()).wid(wid) + .payload(payload) + .time(wid.watchId(), now) + .buildMock(); when(context.simulateAction("test")).thenReturn(false); Action.Result result = executable.execute("test", context, new Payload.Simple()); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/logging/LoggingActionTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/logging/LoggingActionTests.java index 8815cb81debf2..dfd7acf0c1f95 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/logging/LoggingActionTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/logging/LoggingActionTests.java @@ -9,9 +9,9 @@ import org.apache.logging.log4j.Logger; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.SuppressLoggerChecks; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.watcher.actions.Action; import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext; import org.elasticsearch.xpack.core.watcher.watch.Payload; @@ -57,9 +57,7 @@ public void init() throws IOException { public void testExecute() throws Exception { final ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC); - WatchExecutionContext ctx = WatcherTestUtils.mockExecutionContextBuilder("_watch_id") - .time("_watch_id", now) - .buildMock(); + WatchExecutionContext ctx = WatcherTestUtils.mockExecutionContextBuilder("_watch_id").time("_watch_id", now).buildMock(); Map triggerModel = new HashMap<>(); triggerModel.put("scheduled_time", now); @@ -80,8 +78,6 @@ public void testExecute() throws Exception { ExecutableLoggingAction executable = new ExecutableLoggingAction(action, logger, actionLogger, engine); when(engine.render(template, expectedModel)).thenReturn(text); - - Action.Result result = executable.execute("_id", ctx, new Payload.Simple()); verifyLogger(actionLogger, level, text); @@ -171,8 +167,7 @@ public void testParserBuilder() throws Exception { public void testParserFailure() throws Exception { LoggingActionFactory parser = new LoggingActionFactory(engine); - XContentBuilder builder = jsonBuilder() - .startObject().endObject(); + XContentBuilder builder = jsonBuilder().startObject().endObject(); XContentParser xContentParser = createParser(builder); xContentParser.nextToken(); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/pagerduty/PagerDutyActionFactoryTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/pagerduty/PagerDutyActionFactoryTests.java index cec665a41a1d4..000cc0503a69e 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/pagerduty/PagerDutyActionFactoryTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/pagerduty/PagerDutyActionFactoryTests.java @@ -8,9 +8,9 @@ import org.elasticsearch.common.settings.ClusterSettings; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.watcher.common.text.TextTemplateEngine; import org.elasticsearch.xpack.watcher.notification.pagerduty.PagerDutyAccount; import org.elasticsearch.xpack.watcher.notification.pagerduty.PagerDutyService; @@ -50,13 +50,14 @@ public void testParseAction() throws Exception { } public void testParseActionUnknownAccount() throws Exception { - factory = new PagerDutyActionFactory(mock(TextTemplateEngine.class), new PagerDutyService(Settings.EMPTY, null, - new ClusterSettings(Settings.EMPTY, new HashSet<>(PagerDutyService.getSettings())))); + factory = new PagerDutyActionFactory( + mock(TextTemplateEngine.class), + new PagerDutyService(Settings.EMPTY, null, new ClusterSettings(Settings.EMPTY, new HashSet<>(PagerDutyService.getSettings()))) + ); PagerDutyAction action = triggerPagerDutyAction("_unknown", "_body").build(); XContentBuilder jsonBuilder = jsonBuilder().value(action); XContentParser parser = createParser(jsonBuilder); parser.nextToken(); - expectThrows(IllegalArgumentException.class, () -> - factory.parseExecutable("_w1", "_a1", parser)); + expectThrows(IllegalArgumentException.class, () -> factory.parseExecutable("_w1", "_a1", parser)); } } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/pagerduty/PagerDutyActionTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/pagerduty/PagerDutyActionTests.java index b842b514d8f90..ee7526bec831e 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/pagerduty/PagerDutyActionTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/pagerduty/PagerDutyActionTests.java @@ -10,11 +10,11 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.collect.MapBuilder; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.watcher.actions.Action; import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext; import org.elasticsearch.xpack.core.watcher.execution.Wid; @@ -81,12 +81,11 @@ public void testExecute() throws Exception { ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC); Wid wid = new Wid(randomAlphaOfLength(5), now); - WatchExecutionContext ctx = mockExecutionContextBuilder(wid.watchId()) - .wid(wid) - .payload(payload) - .time(wid.watchId(), now) - .metadata(metadata) - .buildMock(); + WatchExecutionContext ctx = mockExecutionContextBuilder(wid.watchId()).wid(wid) + .payload(payload) + .time(wid.watchId(), now) + .metadata(metadata) + .buildMock(); Map ctxModel = new HashMap<>(); ctxModel.put("id", ctx.id().value()); @@ -104,8 +103,17 @@ public void testExecute() throws Exception { when(templateEngine.render(description, expectedModel)).thenReturn(description.getTemplate()); - IncidentEvent event = new IncidentEvent(description.getTemplate(), null, wid.watchId(), null, null, accountName, attachPayload, - null, null); + IncidentEvent event = new IncidentEvent( + description.getTemplate(), + null, + wid.watchId(), + null, + null, + accountName, + attachPayload, + null, + null + ); PagerDutyAccount account = mock(PagerDutyAccount.class); when(account.getDefaults()).thenReturn(new IncidentEventDefaults(Settings.EMPTY)); HttpResponse response = mock(HttpResponse.class); @@ -174,9 +182,8 @@ public void testParser() throws Exception { IncidentEventContext.Template[] contexts = null; if (randomBoolean()) { contexts = new IncidentEventContext.Template[] { - IncidentEventContext.Template.link(new TextTemplate("_href"), new TextTemplate("_text")), - IncidentEventContext.Template.image(new TextTemplate("_src"), new TextTemplate("_href"), new TextTemplate("_alt")) - }; + IncidentEventContext.Template.link(new TextTemplate("_href"), new TextTemplate("_text")), + IncidentEventContext.Template.image(new TextTemplate("_src"), new TextTemplate("_href"), new TextTemplate("_alt")) }; String fieldName = randomBoolean() ? "contexts" : "context"; builder.array(fieldName, (Object) contexts); } @@ -194,8 +201,22 @@ public void testParser() throws Exception { assertThat(action.event.account, is(accountName)); assertThat(action.event, notNullValue()); assertThat(action.event, instanceOf(IncidentEvent.Template.class)); - assertThat(action.event, is(new IncidentEvent.Template(description, eventType, incidentKey, client, clientUrl, accountName, - attachPayload, contexts, proxy))); + assertThat( + action.event, + is( + new IncidentEvent.Template( + description, + eventType, + incidentKey, + client, + clientUrl, + accountName, + attachPayload, + contexts, + proxy + ) + ) + ); } public void testParserSelfGenerated() throws Exception { @@ -217,8 +238,9 @@ public void testParserSelfGenerated() throws Exception { event.addContext(IncidentEventContext.Template.link(new TextTemplate("_href"), new TextTemplate("_text"))); } if (randomBoolean()) { - event.addContext(IncidentEventContext.Template.image(new TextTemplate("_src"), new TextTemplate("_href"), - new TextTemplate("_alt"))); + event.addContext( + IncidentEventContext.Template.image(new TextTemplate("_src"), new TextTemplate("_href"), new TextTemplate("_alt")) + ); } if (randomBoolean()) { event.setEventType(new TextTemplate(randomAlphaOfLength(50))); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/slack/ExecutableSlackActionTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/slack/ExecutableSlackActionTests.java index 34810656a45fc..edb83147938ed 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/slack/ExecutableSlackActionTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/slack/ExecutableSlackActionTests.java @@ -54,11 +54,10 @@ public void testProxy() throws Exception { ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC); Wid wid = new Wid(randomAlphaOfLength(5), now); - WatchExecutionContext ctx = mockExecutionContextBuilder(wid.watchId()) - .wid(wid) - .payload(new Payload.Simple()) - .time(wid.watchId(), now) - .buildMock(); + WatchExecutionContext ctx = mockExecutionContextBuilder(wid.watchId()).wid(wid) + .payload(new Payload.Simple()) + .time(wid.watchId(), now) + .buildMock(); ExecutableSlackAction executable = new ExecutableSlackAction(action, logger, service, new MockTextTemplateEngine()); executable.execute("foo", ctx, new Payload.Simple()); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/slack/SlackActionFactoryTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/slack/SlackActionFactoryTests.java index 76c16c7a4ef07..99c1dd049e3f3 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/slack/SlackActionFactoryTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/slack/SlackActionFactoryTests.java @@ -8,9 +8,9 @@ import org.elasticsearch.common.settings.ClusterSettings; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.watcher.common.text.TextTemplateEngine; import org.elasticsearch.xpack.watcher.notification.slack.SlackAccount; import org.elasticsearch.xpack.watcher.notification.slack.SlackService; @@ -19,8 +19,8 @@ import java.util.HashSet; import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; -import static org.elasticsearch.xpack.watcher.notification.slack.message.SlackMessageTests.createRandomTemplate; import static org.elasticsearch.xpack.watcher.actions.ActionBuilders.slackAction; +import static org.elasticsearch.xpack.watcher.notification.slack.message.SlackMessageTests.createRandomTemplate; import static org.hamcrest.Matchers.is; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -49,8 +49,11 @@ public void testParseAction() throws Exception { } public void testParseActionUnknownAccount() throws Exception { - SlackService service = new SlackService(Settings.EMPTY, null, new ClusterSettings(Settings.EMPTY, - new HashSet<>(SlackService.getSettings()))); + SlackService service = new SlackService( + Settings.EMPTY, + null, + new ClusterSettings(Settings.EMPTY, new HashSet<>(SlackService.getSettings())) + ); factory = new SlackActionFactory(mock(TextTemplateEngine.class), service); SlackAction action = slackAction("_unknown", createRandomTemplate()).build(); XContentBuilder jsonBuilder = jsonBuilder().value(action); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/slack/SlackActionTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/slack/SlackActionTests.java index eb22954a5e3ac..771e206f8b938 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/slack/SlackActionTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/slack/SlackActionTests.java @@ -9,11 +9,11 @@ import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.collect.MapBuilder; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.watcher.actions.Action; import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext; import org.elasticsearch.xpack.core.watcher.execution.Wid; @@ -78,12 +78,11 @@ public void testExecute() throws Exception { ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC); Wid wid = new Wid(randomAlphaOfLength(5), now); - WatchExecutionContext ctx = mockExecutionContextBuilder(wid.watchId()) - .wid(wid) - .payload(payload) - .time(wid.watchId(), now) - .metadata(metadata) - .buildMock(); + WatchExecutionContext ctx = mockExecutionContextBuilder(wid.watchId()).wid(wid) + .payload(payload) + .time(wid.watchId(), now) + .metadata(metadata) + .buildMock(); Map triggerModel = new HashMap<>(); triggerModel.put("triggered_time", now); @@ -98,12 +97,12 @@ public void testExecute() throws Exception { ctxModel.put("vars", emptyMap()); Map expectedModel = singletonMap("ctx", ctxModel); - when(messageTemplate.render(eq(wid.watchId()), eq("_action"), eq(templateEngine), eq(expectedModel), - any(SlackMessageDefaults.class))).thenReturn(message); + when( + messageTemplate.render(eq(wid.watchId()), eq("_action"), eq(templateEngine), eq(expectedModel), any(SlackMessageDefaults.class)) + ).thenReturn(message); SlackAccount account = mock(SlackAccount.class); when(service.getAccount(accountName)).thenReturn(account); - List messages = new ArrayList<>(); boolean hasError = false; boolean hasSuccess = false; @@ -131,10 +130,9 @@ public void testExecute() throws Exception { SentMessages sentMessages = new SentMessages(accountName, messages); when(account.send(message, eq(any()))).thenReturn(sentMessages); - Action.Result.Status expectedStatus = hasError == false ? Action.Result.Status.SUCCESS : - hasSuccess == false ? Action.Result.Status.FAILURE : - Action.Result.Status.PARTIAL_FAILURE; - + Action.Result.Status expectedStatus = hasError == false ? Action.Result.Status.SUCCESS + : hasSuccess == false ? Action.Result.Status.FAILURE + : Action.Result.Status.PARTIAL_FAILURE; Action.Result result = executable.execute("_action", ctx, payload); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/throttler/AckThrottlerTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/throttler/AckThrottlerTests.java index 683ac8e97a205..dfc964e6d308f 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/throttler/AckThrottlerTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/throttler/AckThrottlerTests.java @@ -47,8 +47,9 @@ public void testThrottleWhenAwaitsSuccessfulExecution() throws Exception { WatchExecutionContext ctx = mockExecutionContext("_watch", Payload.EMPTY); Watch watch = ctx.watch(); ActionStatus actionStatus = mock(ActionStatus.class); - when(actionStatus.ackStatus()).thenReturn(new ActionStatus.AckStatus(timestamp, - ActionStatus.AckStatus.State.AWAITS_SUCCESSFUL_EXECUTION)); + when(actionStatus.ackStatus()).thenReturn( + new ActionStatus.AckStatus(timestamp, ActionStatus.AckStatus.State.AWAITS_SUCCESSFUL_EXECUTION) + ); WatchStatus watchStatus = mock(WatchStatus.class); when(watchStatus.actionStatus("_action")).thenReturn(actionStatus); when(watch.status()).thenReturn(watchStatus); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/throttler/PeriodThrottlerTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/throttler/PeriodThrottlerTests.java index 5308e3f25a6f1..5a0106fb48737 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/throttler/PeriodThrottlerTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/throttler/PeriodThrottlerTests.java @@ -35,8 +35,9 @@ public void testBelowPeriodSuccessful() throws Exception { WatchExecutionContext ctx = mockExecutionContext("_name", Payload.EMPTY); ActionStatus actionStatus = mock(ActionStatus.class); ZonedDateTime now = Clock.systemUTC().instant().atZone(ZoneOffset.UTC); - when(actionStatus.lastSuccessfulExecution()) - .thenReturn(ActionStatus.Execution.successful(now.minusSeconds((int) period.seconds() - 1))); + when(actionStatus.lastSuccessfulExecution()).thenReturn( + ActionStatus.Execution.successful(now.minusSeconds((int) period.seconds() - 1)) + ); WatchStatus status = mock(WatchStatus.class); when(status.actionStatus("_action")).thenReturn(actionStatus); when(ctx.watch().status()).thenReturn(status); @@ -56,8 +57,9 @@ public void testAbovePeriod() throws Exception { WatchExecutionContext ctx = mockExecutionContext("_name", Payload.EMPTY); ActionStatus actionStatus = mock(ActionStatus.class); ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC); - when(actionStatus.lastSuccessfulExecution()) - .thenReturn(ActionStatus.Execution.successful(now.minusSeconds((int) period.seconds() + 1))); + when(actionStatus.lastSuccessfulExecution()).thenReturn( + ActionStatus.Execution.successful(now.minusSeconds((int) period.seconds() + 1)) + ); WatchStatus status = mock(WatchStatus.class); when(status.actionStatus("_action")).thenReturn(actionStatus); when(ctx.watch().status()).thenReturn(status); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/webhook/WebhookActionTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/webhook/WebhookActionTests.java index 8b65890d1a847..53031dfce8af2 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/webhook/WebhookActionTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/webhook/WebhookActionTests.java @@ -9,14 +9,14 @@ import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.client.Client; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.env.Environment; import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.http.MockResponse; import org.elasticsearch.test.http.MockWebServer; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ssl.SSLService; import org.elasticsearch.xpack.core.watcher.actions.Action; import org.elasticsearch.xpack.core.watcher.actions.Action.Result.Status; @@ -41,12 +41,13 @@ import org.hamcrest.Matchers; import org.junit.Before; -import javax.mail.internet.AddressException; import java.io.IOException; import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.util.Map; +import javax.mail.internet.AddressException; + import static org.elasticsearch.core.TimeValue.timeValueSeconds; import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.xpack.watcher.common.http.HttpClientTests.mockClusterService; @@ -99,8 +100,14 @@ public void testExecute() throws Exception { scenario.assertResult(httpClient, actionResult); } - private HttpRequestTemplate getHttpRequestTemplate(HttpMethod method, String host, int port, TextTemplate path, TextTemplate body, - Map params) { + private HttpRequestTemplate getHttpRequestTemplate( + HttpMethod method, + String host, + int port, + TextTemplate path, + TextTemplate body, + Map params + ) { HttpRequestTemplate.Builder builder = HttpRequestTemplate.builder(host, port); if (path != null) { builder.path(path); @@ -111,7 +118,7 @@ private HttpRequestTemplate getHttpRequestTemplate(HttpMethod method, String hos if (method != null) { builder.method(method); } - if (params != null){ + if (params != null) { builder.putParams(params); } return builder.build(); @@ -171,7 +178,7 @@ public void testParserBuilder() throws Exception { String watchId = "_watch"; String actionId = randomAlphaOfLength(5); - HttpMethod method = randomFrom(HttpMethod.GET, HttpMethod.POST, HttpMethod.PUT, HttpMethod.DELETE, HttpMethod.HEAD, null); + HttpMethod method = randomFrom(HttpMethod.GET, HttpMethod.POST, HttpMethod.PUT, HttpMethod.DELETE, HttpMethod.HEAD, null); HttpRequestTemplate request = getHttpRequestTemplate(method, host, TEST_PORT, path, body, null); WebhookAction action = WebhookAction.builder(request).build(); @@ -200,7 +207,7 @@ public void testParserFailure() throws Exception { parser.nextToken(); WebhookActionFactory actionParser = webhookFactory(ExecuteScenario.Success.client()); - //This should fail since we are not supplying a url + // This should fail since we are not supplying a url try { actionParser.parseExecutable("_watch", randomAlphaOfLength(5), parser); fail("expected a WebhookActionException since we only provided either a host or a port but not both"); @@ -216,22 +223,31 @@ private WebhookActionFactory webhookFactory(HttpClient client) { public void testThatSelectingProxyWorks() throws Exception { Environment environment = TestEnvironment.newEnvironment(Settings.builder().put("path.home", createTempDir()).build()); - try (HttpClient httpClient = new HttpClient(Settings.EMPTY, new SSLService(environment), null, - mockClusterService()); - MockWebServer proxyServer = new MockWebServer()) { + try ( + HttpClient httpClient = new HttpClient(Settings.EMPTY, new SSLService(environment), null, mockClusterService()); + MockWebServer proxyServer = new MockWebServer() + ) { proxyServer.start(); proxyServer.enqueue(new MockResponse().setResponseCode(200).setBody("fullProxiedContent")); HttpRequestTemplate.Builder builder = HttpRequestTemplate.builder("localhost", 65535) - .path("/").proxy(new HttpProxy("localhost", proxyServer.getPort())); + .path("/") + .proxy(new HttpProxy("localhost", proxyServer.getPort())); WebhookAction action = new WebhookAction(builder.build()); ExecutableWebhookAction executable = new ExecutableWebhookAction(action, logger, httpClient, templateEngine); String watchId = "test_url_encode" + randomAlphaOfLength(10); - ScheduleTriggerEvent triggerEvent = new ScheduleTriggerEvent(watchId, ZonedDateTime.now(ZoneOffset.UTC), - ZonedDateTime.now(ZoneOffset.UTC)); - TriggeredExecutionContext ctx = new TriggeredExecutionContext(watchId, ZonedDateTime.now(ZoneOffset.UTC), - triggerEvent, timeValueSeconds(5)); + ScheduleTriggerEvent triggerEvent = new ScheduleTriggerEvent( + watchId, + ZonedDateTime.now(ZoneOffset.UTC), + ZonedDateTime.now(ZoneOffset.UTC) + ); + TriggeredExecutionContext ctx = new TriggeredExecutionContext( + watchId, + ZonedDateTime.now(ZoneOffset.UTC), + triggerEvent, + timeValueSeconds(5) + ); Watch watch = createWatch(watchId); ctx.ensureWatchExists(() -> watch); executable.execute("_id", ctx, new Payload.Simple()); @@ -242,8 +258,7 @@ public void testThatSelectingProxyWorks() throws Exception { public void testValidUrls() throws Exception { HttpClient client = mock(HttpClient.class); - when(client.execute(any(HttpRequest.class))) - .thenReturn(new HttpResponse(randomIntBetween(200, 399))); + when(client.execute(any(HttpRequest.class))).thenReturn(new HttpResponse(randomIntBetween(200, 399))); String watchId = "test_url_encode" + randomAlphaOfLength(10); HttpMethod method = HttpMethod.POST; @@ -255,10 +270,17 @@ public void testValidUrls() throws Exception { ExecutableWebhookAction executable = new ExecutableWebhookAction(action, logger, client, templateEngine); - ScheduleTriggerEvent triggerEvent = new ScheduleTriggerEvent(watchId, ZonedDateTime.now(ZoneOffset.UTC), - ZonedDateTime.now(ZoneOffset.UTC)); - TriggeredExecutionContext ctx = new TriggeredExecutionContext(watchId, ZonedDateTime.now(ZoneOffset.UTC), - triggerEvent, timeValueSeconds(5)); + ScheduleTriggerEvent triggerEvent = new ScheduleTriggerEvent( + watchId, + ZonedDateTime.now(ZoneOffset.UTC), + ZonedDateTime.now(ZoneOffset.UTC) + ); + TriggeredExecutionContext ctx = new TriggeredExecutionContext( + watchId, + ZonedDateTime.now(ZoneOffset.UTC), + triggerEvent, + timeValueSeconds(5) + ); Watch watch = createWatch(watchId); ctx.ensureWatchExists(() -> watch); Action.Result result = executable.execute("_id", ctx, new Payload.Simple()); @@ -266,12 +288,14 @@ public void testValidUrls() throws Exception { } private Watch createWatch(String watchId) throws AddressException, IOException { - return WatcherTestUtils.createTestWatch(watchId, - mock(Client.class), - ExecuteScenario.Success.client(), - new EmailActionTests.NoopEmailService(), - mock(WatcherSearchTemplateService.class), - logger); + return WatcherTestUtils.createTestWatch( + watchId, + mock(Client.class), + ExecuteScenario.Success.client(), + new EmailActionTests.NoopEmailService(), + mock(WatcherSearchTemplateService.class), + logger + ); } private enum ExecuteScenario { @@ -299,8 +323,7 @@ public void assertResult(HttpClient client, Action.Result actionResult) throws E @Override public HttpClient client() throws IOException { HttpClient client = mock(HttpClient.class); - when(client.execute(any(HttpRequest.class))) - .thenThrow(new IOException("Unable to connect")); + when(client.execute(any(HttpRequest.class))).thenThrow(new IOException("Unable to connect")); return client; } @@ -314,10 +337,9 @@ public void assertResult(HttpClient client, Action.Result actionResult) throws E Success() { @Override - public HttpClient client() throws IOException{ + public HttpClient client() throws IOException { HttpClient client = mock(HttpClient.class); - when(client.execute(any(HttpRequest.class))) - .thenReturn(new HttpResponse(randomIntBetween(200, 399))); + when(client.execute(any(HttpRequest.class))).thenReturn(new HttpResponse(randomIntBetween(200, 399))); return client; } @@ -335,7 +357,7 @@ public void assertResult(HttpClient client, Action.Result actionResult) throws E NoExecute() { @Override - public HttpClient client() throws IOException{ + public HttpClient client() throws IOException { return mock(HttpClient.class); } @@ -347,6 +369,6 @@ public void assertResult(HttpClient client, Action.Result actionResult) throws E public abstract HttpClient client() throws IOException; - public abstract void assertResult(HttpClient client, Action.Result result) throws Exception ; + public abstract void assertResult(HttpClient client, Action.Result result) throws Exception; } } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/http/HttpClientTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/http/HttpClientTests.java index 529e8abce1b5b..b77e63e79464f 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/http/HttpClientTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/http/HttpClientTests.java @@ -7,6 +7,7 @@ package org.elasticsearch.xpack.watcher.common.http; import com.sun.net.httpserver.HttpsServer; + import org.apache.http.HttpHeaders; import org.apache.http.HttpHost; import org.apache.http.client.ClientProtocolException; @@ -15,31 +16,30 @@ import org.apache.logging.log4j.util.Supplier; import org.apache.lucene.util.automaton.CharacterRunAutomaton; import org.elasticsearch.ElasticsearchException; -import org.elasticsearch.common.ssl.SslVerificationMode; -import org.elasticsearch.jdk.JavaVersion; import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.settings.ClusterSettings; import org.elasticsearch.common.settings.MockSecureSettings; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.ssl.SslVerificationMode; import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.core.Tuple; import org.elasticsearch.env.Environment; import org.elasticsearch.env.TestEnvironment; +import org.elasticsearch.jdk.JavaVersion; import org.elasticsearch.mocksocket.MockServerSocket; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.http.MockResponse; import org.elasticsearch.test.http.MockWebServer; import org.elasticsearch.test.junit.annotations.Network; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.XPackSettings; import org.elasticsearch.xpack.core.ssl.SSLService; import org.elasticsearch.xpack.core.ssl.TestsSSLService; import org.junit.After; import org.junit.Before; -import javax.net.ssl.SSLContext; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; @@ -61,6 +61,8 @@ import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicReference; +import javax.net.ssl.SSLContext; + import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; @@ -104,8 +106,8 @@ public void testBasics() throws Exception { webServer.enqueue(new MockResponse().setResponseCode(responseCode).setBody(body)); HttpRequest.Builder requestBuilder = HttpRequest.builder("localhost", webServer.getPort()) - .method(HttpMethod.POST) - .path("/" + randomAlphaOfLength(5)); + .method(HttpMethod.POST) + .path("/" + randomAlphaOfLength(5)); String paramKey = randomAlphaOfLength(3); String paramValue = randomAlphaOfLength(3); @@ -131,9 +133,7 @@ public void testBasics() throws Exception { public void testNoQueryString() throws Exception { webServer.enqueue(new MockResponse().setResponseCode(200).setBody("body")); - HttpRequest.Builder requestBuilder = HttpRequest.builder("localhost", webServer.getPort()) - .method(HttpMethod.GET) - .path("/test"); + HttpRequest.Builder requestBuilder = HttpRequest.builder("localhost", webServer.getPort()).method(HttpMethod.GET).path("/test"); HttpResponse response = httpClient.execute(requestBuilder.build()); assertThat(response.status(), equalTo(200)); @@ -147,9 +147,9 @@ public void testNoQueryString() throws Exception { public void testUrlEncodingWithQueryStrings() throws Exception { webServer.enqueue(new MockResponse().setResponseCode(200).setBody("body")); HttpRequest.Builder requestBuilder = HttpRequest.builder("localhost", webServer.getPort()) - .method(HttpMethod.GET) - .path("/test") - .setParam("key", "value 123:123"); + .method(HttpMethod.GET) + .path("/test") + .setParam("key", "value 123:123"); HttpResponse response = httpClient.execute(requestBuilder.build()); assertThat(response.status(), equalTo(200)); @@ -164,10 +164,10 @@ public void testUrlEncodingWithQueryStrings() throws Exception { public void testBasicAuth() throws Exception { webServer.enqueue(new MockResponse().setResponseCode(200).setBody("body")); HttpRequest.Builder request = HttpRequest.builder("localhost", webServer.getPort()) - .method(HttpMethod.POST) - .path("/test") - .auth(new BasicAuth("user", "pass".toCharArray())) - .body("body"); + .method(HttpMethod.POST) + .path("/test") + .auth(new BasicAuth("user", "pass".toCharArray())) + .body("body"); HttpResponse response = httpClient.execute(request.build()); assertThat(response.status(), equalTo(200)); assertThat(response.body().utf8ToString(), equalTo("body")); @@ -218,11 +218,9 @@ public void testHttps() throws Exception { public void testHttpsDisableHostnameVerification() throws Exception { Path certPath = getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode-no-subjaltname.crt"); Path keyPath = getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode-no-subjaltname.pem"); - Settings.Builder builder = Settings.builder() - .put(environment.settings()) - .put("xpack.http.ssl.certificate_authorities", certPath); + Settings.Builder builder = Settings.builder().put(environment.settings()).put("xpack.http.ssl.certificate_authorities", certPath); if (inFipsJvm()) { - //Can't use TrustAllConfig in FIPS mode + // Can't use TrustAllConfig in FIPS mode builder.put("xpack.http.ssl.verification_mode", SslVerificationMode.CERTIFICATE); } else { builder.put("xpack.http.ssl.verification_mode", randomFrom(SslVerificationMode.NONE, SslVerificationMode.CERTIFICATE)); @@ -271,9 +269,7 @@ private void testSslMockWebserver(HttpClient client, SSLContext sslContext, bool mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody("body")); mockWebServer.start(); - HttpRequest.Builder request = HttpRequest.builder("localhost", mockWebServer.getPort()) - .scheme(Scheme.HTTPS) - .path("/test"); + HttpRequest.Builder request = HttpRequest.builder("localhost", mockWebServer.getPort()).scheme(Scheme.HTTPS).path("/test"); HttpResponse response = client.execute(request.build()); assertThat(response.status(), equalTo(200)); assertThat(response.body().utf8ToString(), equalTo("body")); @@ -293,12 +289,12 @@ public void testHttpResponseWithAnyStatusCodeCanReturnBody() throws Exception { } webServer.enqueue(mockResponse); HttpRequest.Builder request = HttpRequest.builder("localhost", webServer.getPort()) - .method(HttpMethod.POST) - .path("/test") - .auth(new BasicAuth("user", "pass".toCharArray())) - .body("body") - .connectionTimeout(TimeValue.timeValueMillis(500)) - .readTimeout(TimeValue.timeValueMillis(500)); + .method(HttpMethod.POST) + .path("/test") + .auth(new BasicAuth("user", "pass".toCharArray())) + .body("body") + .connectionTimeout(TimeValue.timeValueMillis(500)) + .readTimeout(TimeValue.timeValueMillis(500)); HttpResponse response = httpClient.execute(request.build()); assertThat(response.status(), equalTo(statusCode)); assertThat(response.hasContent(), is(hasBody)); @@ -325,14 +321,12 @@ public void testThatProxyCanBeConfigured() throws Exception { proxyServer.enqueue(new MockResponse().setResponseCode(200).setBody("fullProxiedContent")); proxyServer.start(); Settings settings = Settings.builder() - .put(environment.settings()) - .put(HttpSettings.PROXY_HOST.getKey(), "localhost") - .put(HttpSettings.PROXY_PORT.getKey(), proxyServer.getPort()) - .build(); + .put(environment.settings()) + .put(HttpSettings.PROXY_HOST.getKey(), "localhost") + .put(HttpSettings.PROXY_PORT.getKey(), proxyServer.getPort()) + .build(); - HttpRequest.Builder requestBuilder = HttpRequest.builder("localhost", webServer.getPort()) - .method(HttpMethod.GET) - .path("/"); + HttpRequest.Builder requestBuilder = HttpRequest.builder("localhost", webServer.getPort()).method(HttpMethod.GET).path("/"); final SSLService sslService = new SSLService(TestEnvironment.newEnvironment(settings)); try (HttpClient client = new HttpClient(settings, sslService, null, mockClusterService())) { @@ -357,29 +351,34 @@ public void testSetProxy() throws Exception { // no system wide proxy configured, proxy in request config = RequestConfig.custom(); - HttpClient.setProxy(config, - HttpRequest.builder().fromUrl("https://elastic.co").proxy(new HttpProxy("localhost", 23456)).build(), - HttpProxy.NO_PROXY); + HttpClient.setProxy( + config, + HttpRequest.builder().fromUrl("https://elastic.co").proxy(new HttpProxy("localhost", 23456)).build(), + HttpProxy.NO_PROXY + ); assertThat(config.build().getProxy().toString(), is("http://localhost:23456")); // system wide proxy configured, no proxy in request config = RequestConfig.custom(); - HttpClient.setProxy(config, HttpRequest.builder().fromUrl("https://elastic.co").build(), - localhostHttpProxy); + HttpClient.setProxy(config, HttpRequest.builder().fromUrl("https://elastic.co").build(), localhostHttpProxy); assertThat(config.build().getProxy().toString(), is("http://localhost:1234")); // proxy in request, no system wide proxy configured. request config = RequestConfig.custom(); - HttpClient.setProxy(config, - HttpRequest.builder().fromUrl("https://elastic.co").proxy(new HttpProxy("localhost", 23456, Scheme.HTTP)).build(), - HttpProxy.NO_PROXY); + HttpClient.setProxy( + config, + HttpRequest.builder().fromUrl("https://elastic.co").proxy(new HttpProxy("localhost", 23456, Scheme.HTTP)).build(), + HttpProxy.NO_PROXY + ); assertThat(config.build().getProxy().toString(), is("http://localhost:23456")); // proxy in request, system wide proxy configured. request wins config = RequestConfig.custom(); - HttpClient.setProxy(config, - HttpRequest.builder().fromUrl("http://elastic.co").proxy(new HttpProxy("localhost", 23456, Scheme.HTTPS)).build(), - localhostHttpProxy); + HttpClient.setProxy( + config, + HttpRequest.builder().fromUrl("http://elastic.co").proxy(new HttpProxy("localhost", 23456, Scheme.HTTPS)).build(), + localhostHttpProxy + ); assertThat(config.build().getProxy().toString(), is("https://localhost:23456")); } @@ -417,9 +416,9 @@ public void testProxyCanHaveDifferentSchemeThanRequest() throws Exception { .build(); HttpRequest.Builder requestBuilder = HttpRequest.builder("localhost", webServer.getPort()) - .method(HttpMethod.GET) - .scheme(Scheme.HTTP) - .path("/"); + .method(HttpMethod.GET) + .scheme(Scheme.HTTP) + .path("/"); final SSLService ssl = new SSLService(TestEnvironment.newEnvironment(settings)); try (HttpClient client = new HttpClient(settings, ssl, null, mockClusterService())) { @@ -440,16 +439,16 @@ public void testThatProxyCanBeOverriddenByRequest() throws Exception { proxyServer.enqueue(new MockResponse().setResponseCode(200).setBody("fullProxiedContent")); proxyServer.start(); Settings settings = Settings.builder() - .put(environment.settings()) - .put(HttpSettings.PROXY_HOST.getKey(), "localhost") - .put(HttpSettings.PROXY_PORT.getKey(), proxyServer.getPort() + 1) - .put(HttpSettings.PROXY_HOST.getKey(), "https") - .build(); + .put(environment.settings()) + .put(HttpSettings.PROXY_HOST.getKey(), "localhost") + .put(HttpSettings.PROXY_PORT.getKey(), proxyServer.getPort() + 1) + .put(HttpSettings.PROXY_HOST.getKey(), "https") + .build(); HttpRequest.Builder requestBuilder = HttpRequest.builder("localhost", webServer.getPort()) - .method(HttpMethod.GET) - .proxy(new HttpProxy("localhost", proxyServer.getPort(), Scheme.HTTP)) - .path("/"); + .method(HttpMethod.GET) + .proxy(new HttpProxy("localhost", proxyServer.getPort(), Scheme.HTTP)) + .path("/"); final SSLService sslService = new SSLService(TestEnvironment.newEnvironment(settings)); try (HttpClient client = new HttpClient(settings, sslService, null, mockClusterService())) { @@ -473,11 +472,14 @@ public void testThatProxyConfigurationRequiresHostAndPort() { } final SSLService sslService = new SSLService(TestEnvironment.newEnvironment(settings.build())); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> new HttpClient(settings.build(), sslService, null, - mockClusterService())); - assertThat(e.getMessage(), - containsString("HTTP proxy requires both settings: [xpack.http.proxy.host] and [xpack.http.proxy.port]")); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> new HttpClient(settings.build(), sslService, null, mockClusterService()) + ); + assertThat( + e.getMessage(), + containsString("HTTP proxy requires both settings: [xpack.http.proxy.host] and [xpack.http.proxy.port]") + ); } public void testThatUrlPathIsNotEncoded() throws Exception { @@ -502,10 +504,11 @@ public void testThatUrlPathIsNotEncoded() throws Exception { } public void testThatDuplicateHeaderKeysAreReturned() throws Exception { - MockResponse mockResponse = new MockResponse().setResponseCode(200).setBody("foo") - .addHeader("foo", "bar") - .addHeader("foo", "baz") - .addHeader("Content-Length", "3"); + MockResponse mockResponse = new MockResponse().setResponseCode(200) + .setBody("foo") + .addHeader("foo", "bar") + .addHeader("foo", "baz") + .addHeader("Content-Length", "3"); webServer.enqueue(mockResponse); HttpRequest request = HttpRequest.builder("localhost", webServer.getPort()).path("/").build(); @@ -521,12 +524,13 @@ public void testThatDuplicateHeaderKeysAreReturned() throws Exception { public void testThatClientTakesTimeoutsIntoAccountAfterHeadersAreSent() throws Exception { webServer.enqueue(new MockResponse().setResponseCode(200).setBody("foo").setBodyDelay(TimeValue.timeValueSeconds(2))); - HttpRequest request = HttpRequest.builder("localhost", webServer.getPort()).path("/foo") - .method(HttpMethod.POST) - .body("foo") - .connectionTimeout(TimeValue.timeValueMillis(500)) - .readTimeout(TimeValue.timeValueMillis(500)) - .build(); + HttpRequest request = HttpRequest.builder("localhost", webServer.getPort()) + .path("/foo") + .method(HttpMethod.POST) + .body("foo") + .connectionTimeout(TimeValue.timeValueMillis(500)) + .readTimeout(TimeValue.timeValueMillis(500)) + .build(); SocketTimeoutException e = expectThrows(SocketTimeoutException.class, () -> httpClient.execute(request)); assertThat(e.getMessage(), is("Read timed out")); } @@ -569,13 +573,12 @@ public void testMaxHttpResponseSize() throws Exception { webServer.enqueue(new MockResponse().setResponseCode(200).setBody(data)); Settings settings = Settings.builder() - .put(HttpSettings.MAX_HTTP_RESPONSE_SIZE.getKey(), new ByteSizeValue(randomBytesLength - 1, ByteSizeUnit.BYTES)) - .build(); + .put(HttpSettings.MAX_HTTP_RESPONSE_SIZE.getKey(), new ByteSizeValue(randomBytesLength - 1, ByteSizeUnit.BYTES)) + .build(); HttpRequest.Builder requestBuilder = HttpRequest.builder("localhost", webServer.getPort()).method(HttpMethod.GET).path("/"); - try (HttpClient client = new HttpClient(settings, new SSLService(environment), null, - mockClusterService())) { + try (HttpClient client = new HttpClient(settings, new SSLService(environment), null, mockClusterService())) { IOException e = expectThrows(IOException.class, () -> client.execute(requestBuilder.build())); assertThat(e.getMessage(), startsWith("Maximum limit of")); } @@ -592,9 +595,7 @@ public void testThatGetRedirectIsFollowed() throws Exception { webServer.enqueue(new MockResponse().setResponseCode(200)); } - HttpRequest request = HttpRequest.builder("localhost", webServer.getPort()).path("/") - .method(method) - .build(); + HttpRequest request = HttpRequest.builder("localhost", webServer.getPort()).path("/").method(method).build(); HttpResponse response = httpClient.execute(request); assertThat(webServer.requests(), hasSize(2)); @@ -622,10 +623,10 @@ public void testThatBodyWithUTF8Content() throws Exception { webServer.enqueue(new MockResponse().setResponseCode(200).setBody(body)); HttpRequest request = HttpRequest.builder("localhost", webServer.getPort()) - .path("/") - .setHeader(HttpHeaders.CONTENT_TYPE, XContentType.JSON.mediaType()) - .body(body) - .build(); + .path("/") + .setHeader(HttpHeaders.CONTENT_TYPE, XContentType.JSON.mediaType()) + .body(body) + .build(); HttpResponse response = httpClient.execute(request); assertThat(response.body().utf8ToString(), is(body)); @@ -637,9 +638,7 @@ public void testThatBodyWithUTF8Content() throws Exception { public void testThatUrlDoesNotContainQuestionMarkAtTheEnd() throws Exception { webServer.enqueue(new MockResponse().setResponseCode(200).setBody("whatever")); - HttpRequest request = HttpRequest.builder("localhost", webServer.getPort()) - .path("foo") - .build(); + HttpRequest request = HttpRequest.builder("localhost", webServer.getPort()).path("foo").build(); httpClient.execute(request); assertThat(webServer.requests(), hasSize(1)); assertThat(webServer.requests().get(0).getUri().getRawPath(), is("/foo")); @@ -649,26 +648,26 @@ public void testThatWhiteListingWorks() throws Exception { webServer.enqueue(new MockResponse().setResponseCode(200).setBody("whatever")); Settings settings = Settings.builder().put(HttpSettings.HOSTS_WHITELIST.getKey(), getWebserverUri()).build(); - try (HttpClient client = new HttpClient(settings, new SSLService(environment), null, - mockClusterService())) { + try (HttpClient client = new HttpClient(settings, new SSLService(environment), null, mockClusterService())) { HttpRequest request = HttpRequest.builder(webServer.getHostName(), webServer.getPort()).path("foo").build(); client.execute(request); } } public void testThatWhiteListBlocksRequests() throws Exception { - Settings settings = Settings.builder() - .put(HttpSettings.HOSTS_WHITELIST.getKey(), getWebserverUri()) - .build(); + Settings settings = Settings.builder().put(HttpSettings.HOSTS_WHITELIST.getKey(), getWebserverUri()).build(); - try (HttpClient client = new HttpClient(settings, new SSLService(environment), null, - mockClusterService())) { - HttpRequest request = HttpRequest.builder("blocked.domain.org", webServer.getPort()) - .path("foo") - .build(); + try (HttpClient client = new HttpClient(settings, new SSLService(environment), null, mockClusterService())) { + HttpRequest request = HttpRequest.builder("blocked.domain.org", webServer.getPort()).path("foo").build(); ElasticsearchException e = expectThrows(ElasticsearchException.class, () -> client.execute(request)); - assertThat(e.getMessage(), is("host [http://blocked.domain.org:" + webServer.getPort() + - "] is not whitelisted in setting [xpack.http.whitelist], will not connect")); + assertThat( + e.getMessage(), + is( + "host [http://blocked.domain.org:" + + webServer.getPort() + + "] is not whitelisted in setting [xpack.http.whitelist], will not connect" + ) + ); } } @@ -685,14 +684,13 @@ public void testThatWhiteListBlocksRedirects() throws Exception { Settings settings = Settings.builder().put(HttpSettings.HOSTS_WHITELIST.getKey(), getWebserverUri()).build(); - try (HttpClient client = new HttpClient(settings, new SSLService(environment), null, - mockClusterService())) { - HttpRequest request = HttpRequest.builder(webServer.getHostName(), webServer.getPort()).path("/") - .method(method) - .build(); + try (HttpClient client = new HttpClient(settings, new SSLService(environment), null, mockClusterService())) { + HttpRequest request = HttpRequest.builder(webServer.getHostName(), webServer.getPort()).path("/").method(method).build(); ElasticsearchException e = expectThrows(ElasticsearchException.class, () -> client.execute(request)); - assertThat(e.getMessage(), is("host [" + redirectUrl + "] is not whitelisted in setting [xpack.http.whitelist], " + - "will not redirect")); + assertThat( + e.getMessage(), + is("host [" + redirectUrl + "] is not whitelisted in setting [xpack.http.whitelist], " + "will not redirect") + ); } } @@ -706,9 +704,9 @@ public void testThatWhiteListingWorksForRedirects() throws Exception { Settings settings = Settings.builder().put(HttpSettings.HOSTS_WHITELIST.getKey(), getWebserverUri() + "*").build(); - try (HttpClient client = new HttpClient(settings, new SSLService(environment), null, - mockClusterService())) { - HttpRequest request = HttpRequest.builder(webServer.getHostName(), webServer.getPort()).path("/") + try (HttpClient client = new HttpClient(settings, new SSLService(environment), null, mockClusterService())) { + HttpRequest request = HttpRequest.builder(webServer.getHostName(), webServer.getPort()) + .path("/") .method(HttpMethod.GET) .build(); HttpResponse response = client.execute(request); @@ -725,11 +723,11 @@ public void testThatWhiteListReloadingWorks() throws Exception { ClusterSettings clusterSettings = new ClusterSettings(settings, new HashSet<>(HttpSettings.getSettings())); when(clusterService.getClusterSettings()).thenReturn(clusterSettings); - try (HttpClient client = - new HttpClient(settings, new SSLService(environment), null, clusterService)) { + try (HttpClient client = new HttpClient(settings, new SSLService(environment), null, clusterService)) { // blacklisted - HttpRequest request = HttpRequest.builder(webServer.getHostName(), webServer.getPort()).path("/") + HttpRequest request = HttpRequest.builder(webServer.getHostName(), webServer.getPort()) + .path("/") .method(HttpMethod.GET) .build(); ElasticsearchException e = expectThrows(ElasticsearchException.class, () -> client.execute(request)); @@ -744,8 +742,9 @@ public void testThatWhiteListReloadingWorks() throws Exception { } public void testAutomatonWhitelisting() { - CharacterRunAutomaton automaton = HttpClient.createAutomaton(Arrays.asList("https://example*", "https://bar.com/foo", - "htt*://www.test.org")); + CharacterRunAutomaton automaton = HttpClient.createAutomaton( + Arrays.asList("https://example*", "https://bar.com/foo", "htt*://www.test.org") + ); assertThat(automaton.run("https://example.org"), is(true)); assertThat(automaton.run("https://example.com"), is(true)); assertThat(automaton.run("https://examples.com"), is(true)); @@ -772,9 +771,9 @@ public void testCreateUri() throws Exception { public void testConnectionReuse() throws Exception { final HttpRequest request = HttpRequest.builder("localhost", webServer.getPort()) - .method(HttpMethod.POST) - .path("/" + randomAlphaOfLength(5)) - .build(); + .method(HttpMethod.POST) + .path("/" + randomAlphaOfLength(5)) + .build(); webServer.enqueue(new MockResponse().setResponseCode(200).setBody("whatever")); webServer.enqueue(new MockResponse().setResponseCode(200).setBody("whatever")); @@ -787,11 +786,14 @@ public void testConnectionReuse() throws Exception { assertThat(webServer.requests().get(0).getRemoteAddress(), equalTo(webServer.requests().get(1).getRemoteAddress())); webServer.clearRequests(); - try (HttpClient unpooledHttpClient = new HttpClient( + try ( + HttpClient unpooledHttpClient = new HttpClient( Settings.builder().put(HttpSettings.CONNECTION_POOL_TTL.getKey(), "99ms").build(), new SSLService(environment), null, - mockClusterService())) { + mockClusterService() + ) + ) { webServer.enqueue(new MockResponse().setResponseCode(200).setBody("whatever")); webServer.enqueue(new MockResponse().setResponseCode(200).setBody("whatever")); @@ -802,7 +804,7 @@ public void testConnectionReuse() throws Exception { // we just used to expire final long waitStartTime = System.currentTimeMillis(); while (System.currentTimeMillis() <= waitStartTime + 100) { - //noinspection BusyWait + // noinspection BusyWait Thread.sleep(100); } @@ -839,9 +841,9 @@ private static List getProtocols() { if (JavaVersion.current().compareTo(JavaVersion.parse("12")) < 0) { return List.of("TLSv1.2"); } else { - JavaVersion full = - AccessController.doPrivileged( - (PrivilegedAction) () -> JavaVersion.parse(System.getProperty("java.version"))); + JavaVersion full = AccessController.doPrivileged( + (PrivilegedAction) () -> JavaVersion.parse(System.getProperty("java.version")) + ); if (full.compareTo(JavaVersion.parse("12.0.1")) < 0) { return List.of("TLSv1.2"); } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/http/HttpConnectionTimeoutTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/http/HttpConnectionTimeoutTests.java index a0899b1c34758..041e8baa079b1 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/http/HttpConnectionTimeoutTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/http/HttpConnectionTimeoutTests.java @@ -26,13 +26,9 @@ public class HttpConnectionTimeoutTests extends ESTestCase { @Network public void testDefaultTimeout() throws Exception { Environment environment = TestEnvironment.newEnvironment(Settings.builder().put("path.home", createTempDir()).build()); - HttpClient httpClient = new HttpClient(Settings.EMPTY, new SSLService(environment), null, - mockClusterService()); + HttpClient httpClient = new HttpClient(Settings.EMPTY, new SSLService(environment), null, mockClusterService()); - HttpRequest request = HttpRequest.builder(UNROUTABLE_IP, 12345) - .method(HttpMethod.POST) - .path("/" + randomAlphaOfLength(5)) - .build(); + HttpRequest request = HttpRequest.builder(UNROUTABLE_IP, 12345).method(HttpMethod.POST).path("/" + randomAlphaOfLength(5)).build(); long start = System.nanoTime(); try { @@ -51,14 +47,14 @@ public void testDefaultTimeout() throws Exception { @Network public void testDefaultTimeoutCustom() throws Exception { Environment environment = TestEnvironment.newEnvironment(Settings.builder().put("path.home", createTempDir()).build()); - HttpClient httpClient = new HttpClient(Settings.builder() - .put("xpack.http.default_connection_timeout", "5s").build(), new SSLService(environment), null, - mockClusterService()); + HttpClient httpClient = new HttpClient( + Settings.builder().put("xpack.http.default_connection_timeout", "5s").build(), + new SSLService(environment), + null, + mockClusterService() + ); - HttpRequest request = HttpRequest.builder(UNROUTABLE_IP, 12345) - .method(HttpMethod.POST) - .path("/" + randomAlphaOfLength(5)) - .build(); + HttpRequest request = HttpRequest.builder(UNROUTABLE_IP, 12345).method(HttpMethod.POST).path("/" + randomAlphaOfLength(5)).build(); long start = System.nanoTime(); try { @@ -77,15 +73,18 @@ public void testDefaultTimeoutCustom() throws Exception { @Network public void testTimeoutCustomPerRequest() throws Exception { Environment environment = TestEnvironment.newEnvironment(Settings.builder().put("path.home", createTempDir()).build()); - HttpClient httpClient = new HttpClient(Settings.builder() - .put("xpack.http.default_connection_timeout", "10s").build(), new SSLService(environment), null, - mockClusterService()); + HttpClient httpClient = new HttpClient( + Settings.builder().put("xpack.http.default_connection_timeout", "10s").build(), + new SSLService(environment), + null, + mockClusterService() + ); HttpRequest request = HttpRequest.builder(UNROUTABLE_IP, 12345) - .connectionTimeout(TimeValue.timeValueSeconds(5)) - .method(HttpMethod.POST) - .path("/" + randomAlphaOfLength(5)) - .build(); + .connectionTimeout(TimeValue.timeValueSeconds(5)) + .method(HttpMethod.POST) + .path("/" + randomAlphaOfLength(5)) + .build(); long start = System.nanoTime(); try { diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/http/HttpProxyTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/http/HttpProxyTests.java index 928001e0ce514..a94e69e32729a 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/http/HttpProxyTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/http/HttpProxyTests.java @@ -9,6 +9,7 @@ import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ToXContent; @@ -16,7 +17,6 @@ import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.Matchers.is; @@ -35,9 +35,14 @@ public void testParser() throws Exception { builder.field("scheme", scheme); } builder.endObject(); - try (XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, - BytesReference.bytes(builder).streamInput())) { + try ( + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + BytesReference.bytes(builder).streamInput() + ) + ) { parser.nextToken(); HttpProxy proxy = HttpProxy.parse(parser); assertThat(proxy.getHost(), is(host)); @@ -52,47 +57,63 @@ public void testParser() throws Exception { public void testParserValidScheme() throws Exception { XContentBuilder builder = jsonBuilder().startObject() - .field("host", "localhost").field("port", 12345).field("scheme", "invalid") - .endObject(); - try (XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, - BytesReference.bytes(builder).streamInput())) { + .field("host", "localhost") + .field("port", 12345) + .field("scheme", "invalid") + .endObject(); + try ( + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + BytesReference.bytes(builder).streamInput() + ) + ) { parser.nextToken(); expectThrows(IllegalArgumentException.class, () -> HttpProxy.parse(parser)); } } public void testParserValidPortRange() throws Exception { - XContentBuilder builder = jsonBuilder().startObject() - .field("host", "localhost").field("port", -1) - .endObject(); - try (XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, - BytesReference.bytes(builder).streamInput())) { + XContentBuilder builder = jsonBuilder().startObject().field("host", "localhost").field("port", -1).endObject(); + try ( + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + BytesReference.bytes(builder).streamInput() + ) + ) { parser.nextToken(); expectThrows(ElasticsearchParseException.class, () -> HttpProxy.parse(parser)); } } public void testParserNoHost() throws Exception { - XContentBuilder builder = jsonBuilder().startObject() - .field("port", -1) - .endObject(); - try (XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, - BytesReference.bytes(builder).streamInput())) { + XContentBuilder builder = jsonBuilder().startObject().field("port", -1).endObject(); + try ( + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + BytesReference.bytes(builder).streamInput() + ) + ) { parser.nextToken(); expectThrows(ElasticsearchParseException.class, () -> HttpProxy.parse(parser)); } } public void testParserNoPort() throws Exception { - XContentBuilder builder = jsonBuilder().startObject() - .field("host", "localhost") - .endObject(); - try (XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, - BytesReference.bytes(builder).streamInput())) { + XContentBuilder builder = jsonBuilder().startObject().field("host", "localhost").endObject(); + try ( + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + BytesReference.bytes(builder).streamInput() + ) + ) { parser.nextToken(); expectThrows(ElasticsearchParseException.class, () -> HttpProxy.parse(parser)); } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/http/HttpReadTimeoutTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/http/HttpReadTimeoutTests.java index 06828061356a9..c141b17106ced 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/http/HttpReadTimeoutTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/http/HttpReadTimeoutTests.java @@ -40,16 +40,12 @@ public void cleanup() throws Exception { public void testDefaultTimeout() throws Exception { Environment environment = TestEnvironment.newEnvironment(Settings.builder().put("path.home", createTempDir()).build()); - HttpRequest request = HttpRequest.builder("localhost", webServer.getPort()) - .method(HttpMethod.POST) - .path("/") - .build(); + HttpRequest request = HttpRequest.builder("localhost", webServer.getPort()).method(HttpMethod.POST).path("/").build(); - try (HttpClient httpClient = new HttpClient(Settings.EMPTY, new SSLService(environment), - null, mockClusterService())) { + try (HttpClient httpClient = new HttpClient(Settings.EMPTY, new SSLService(environment), null, mockClusterService())) { long start = System.nanoTime(); - expectThrows(SocketTimeoutException.class, () -> httpClient.execute(request)); + expectThrows(SocketTimeoutException.class, () -> httpClient.execute(request)); TimeValue timeout = TimeValue.timeValueNanos(System.nanoTime() - start); logger.info("http connection timed out after {}", timeout); @@ -62,17 +58,19 @@ null, mockClusterService())) { public void testDefaultTimeoutCustom() throws Exception { Environment environment = TestEnvironment.newEnvironment(Settings.builder().put("path.home", createTempDir()).build()); - HttpRequest request = HttpRequest.builder("localhost", webServer.getPort()) - .method(HttpMethod.POST) - .path("/") - .build(); + HttpRequest request = HttpRequest.builder("localhost", webServer.getPort()).method(HttpMethod.POST).path("/").build(); - try (HttpClient httpClient = new HttpClient(Settings.builder() - .put("xpack.http.default_read_timeout", "3s").build(), new SSLService(environment), - null, mockClusterService())) { + try ( + HttpClient httpClient = new HttpClient( + Settings.builder().put("xpack.http.default_read_timeout", "3s").build(), + new SSLService(environment), + null, + mockClusterService() + ) + ) { long start = System.nanoTime(); - expectThrows(SocketTimeoutException.class, () -> httpClient.execute(request)); + expectThrows(SocketTimeoutException.class, () -> httpClient.execute(request)); TimeValue timeout = TimeValue.timeValueNanos(System.nanoTime() - start); logger.info("http connection timed out after {}", timeout); @@ -86,17 +84,22 @@ public void testTimeoutCustomPerRequest() throws Exception { Environment environment = TestEnvironment.newEnvironment(Settings.builder().put("path.home", createTempDir()).build()); HttpRequest request = HttpRequest.builder("localhost", webServer.getPort()) - .readTimeout(TimeValue.timeValueSeconds(3)) - .method(HttpMethod.POST) - .path("/") - .build(); - - try (HttpClient httpClient = new HttpClient(Settings.builder() - .put("xpack.http.default_read_timeout", "10s").build(), new SSLService(environment), - null, mockClusterService())) { + .readTimeout(TimeValue.timeValueSeconds(3)) + .method(HttpMethod.POST) + .path("/") + .build(); + + try ( + HttpClient httpClient = new HttpClient( + Settings.builder().put("xpack.http.default_read_timeout", "10s").build(), + new SSLService(environment), + null, + mockClusterService() + ) + ) { long start = System.nanoTime(); - expectThrows(SocketTimeoutException.class, () -> httpClient.execute(request)); + expectThrows(SocketTimeoutException.class, () -> httpClient.execute(request)); TimeValue timeout = TimeValue.timeValueNanos(System.nanoTime() - start); logger.info("http connection timed out after {}", timeout); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/http/HttpRequestTemplateTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/http/HttpRequestTemplateTests.java index 02f58c004fe01..143c17c3f44cd 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/http/HttpRequestTemplateTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/http/HttpRequestTemplateTests.java @@ -7,13 +7,14 @@ package org.elasticsearch.xpack.watcher.common.http; import io.netty.handler.codec.http.HttpHeaders; + import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.watcher.support.xcontent.WatcherParams; import org.elasticsearch.xpack.watcher.common.text.TextTemplate; import org.elasticsearch.xpack.watcher.test.MockTextTemplateEngine; @@ -32,24 +33,20 @@ public class HttpRequestTemplateTests extends ESTestCase { public void testBodyWithXContent() throws Exception { XContentType type = randomFrom(XContentType.JSON, XContentType.YAML); HttpRequestTemplate template = HttpRequestTemplate.builder("_host", 1234) - .body(XContentBuilder.builder(type.xContent()).startObject().endObject()) - .build(); + .body(XContentBuilder.builder(type.xContent()).startObject().endObject()) + .build(); HttpRequest request = template.render(new MockTextTemplateEngine(), emptyMap()); assertThat(request.headers, hasEntry(HttpHeaders.Names.CONTENT_TYPE, type.mediaType())); } public void testBody() throws Exception { - HttpRequestTemplate template = HttpRequestTemplate.builder("_host", 1234) - .body("_body") - .build(); + HttpRequestTemplate template = HttpRequestTemplate.builder("_host", 1234).body("_body").build(); HttpRequest request = template.render(new MockTextTemplateEngine(), emptyMap()); assertThat(request.headers.size(), is(0)); } public void testProxy() throws Exception { - HttpRequestTemplate template = HttpRequestTemplate.builder("_host", 1234) - .proxy(new HttpProxy("localhost", 8080)) - .build(); + HttpRequestTemplate template = HttpRequestTemplate.builder("_host", 1234).proxy(new HttpProxy("localhost", 8080)).build(); HttpRequest request = template.render(new MockTextTemplateEngine(), Collections.emptyMap()); assertThat(request.proxy().getHost(), is("localhost")); assertThat(request.proxy().getPort(), is(8080)); @@ -57,11 +54,11 @@ public void testProxy() throws Exception { public void testRender() { HttpRequestTemplate template = HttpRequestTemplate.builder("_host", 1234) - .body(new TextTemplate("_body")) - .path(new TextTemplate("_path")) - .putParam("_key1", new TextTemplate("_value1")) - .putHeader("_key2", new TextTemplate("_value2")) - .build(); + .body(new TextTemplate("_body")) + .path(new TextTemplate("_path")) + .putParam("_key1", new TextTemplate("_value1")) + .putHeader("_key2", new TextTemplate("_value2")) + .build(); HttpRequest result = template.render(new MockTextTemplateEngine(), Collections.emptyMap()); assertThat(result.body(), equalTo("_body")); @@ -163,14 +160,18 @@ public void testParsingEmptyUrl() throws Exception { } public void testInvalidUrlsWithMissingScheme() throws Exception { - ElasticsearchParseException e = expectThrows(ElasticsearchParseException.class, - () -> HttpRequestTemplate.builder().fromUrl("www.test.de")); + ElasticsearchParseException e = expectThrows( + ElasticsearchParseException.class, + () -> HttpRequestTemplate.builder().fromUrl("www.test.de") + ); assertThat(e.getMessage(), containsString("URL [www.test.de] does not contain a scheme")); } public void testInvalidUrlsWithHost() throws Exception { - ElasticsearchParseException e = expectThrows(ElasticsearchParseException.class, - () -> HttpRequestTemplate.builder().fromUrl("https://")); + ElasticsearchParseException e = expectThrows( + ElasticsearchParseException.class, + () -> HttpRequestTemplate.builder().fromUrl("https://") + ); assertThat(e.getMessage(), containsString("Malformed URL [https://]")); } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/http/HttpRequestTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/http/HttpRequestTests.java index 3bb571b281b82..ce22ab8e022db 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/http/HttpRequestTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/http/HttpRequestTests.java @@ -9,9 +9,9 @@ import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.Strings; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.watcher.support.xcontent.WatcherParams; import org.elasticsearch.xpack.core.watcher.support.xcontent.WatcherXContentParser; @@ -105,14 +105,15 @@ public void testXContentSerialization() throws Exception { } if (randomBoolean()) { // micros and nanos don't round trip will full precision so exclude them from the test - String safeConnectionTimeout = randomValueOtherThanMany(s -> (s.endsWith("micros") || s.endsWith("nanos")), - () -> randomTimeValue()); + String safeConnectionTimeout = randomValueOtherThanMany( + s -> (s.endsWith("micros") || s.endsWith("nanos")), + () -> randomTimeValue() + ); builder.connectionTimeout(TimeValue.parseTimeValue(safeConnectionTimeout, "my.setting")); } if (randomBoolean()) { // micros and nanos don't round trip will full precision so exclude them from the test - String safeReadTimeout = randomValueOtherThanMany(s -> (s.endsWith("micros") || s.endsWith("nanos")), - () -> randomTimeValue()); + String safeReadTimeout = randomValueOtherThanMany(s -> (s.endsWith("micros") || s.endsWith("nanos")), () -> randomTimeValue()); builder.readTimeout(TimeValue.parseTimeValue(safeReadTimeout, "my.setting")); } if (randomBoolean()) { @@ -125,7 +126,6 @@ public void testXContentSerialization() throws Exception { try (XContentBuilder xContentBuilder = randomFrom(jsonBuilder(), smileBuilder(), yamlBuilder(), cborBuilder())) { httpRequest.toXContent(xContentBuilder, WatcherParams.builder().hideSecrets(false).build()); - try (XContentParser parser = createParser(xContentBuilder)) { assertNull(parser.currentToken()); parser.nextToken(); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/http/HttpResponseTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/http/HttpResponseTests.java index 9f858a4aee53d..fcb6a75b9da49 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/http/HttpResponseTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/http/HttpResponseTests.java @@ -8,10 +8,10 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.ESTestCase; import java.nio.charset.StandardCharsets; import java.util.Collections; @@ -42,7 +42,7 @@ public void testParseSelfGenerated() throws Exception { final HttpResponse response; if (randomBoolean() && headers.isEmpty() && body == null) { response = new HttpResponse(status); - } else if (body != null ){ + } else if (body != null) { switch (randomIntBetween(0, 2)) { case 0: response = new HttpResponse(status, body, headers); @@ -65,7 +65,7 @@ public void testParseSelfGenerated() throws Exception { case 2: response = new HttpResponse(status, (BytesReference) null, headers); break; - default: //3 + default: // 3 response = new HttpResponse(status, headers); break; } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/http/SizeLimitInputStreamTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/http/SizeLimitInputStreamTests.java index 408bc04afbac3..b804ca97a5467 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/http/SizeLimitInputStreamTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/http/SizeLimitInputStreamTests.java @@ -25,14 +25,13 @@ public void testGoodCase() throws IOException { public void testLimitReached() { int length = scaledRandomIntBetween(1, 100); - IOException e = expectThrows(IOException.class, () -> test(length+1, length)); + IOException e = expectThrows(IOException.class, () -> test(length + 1, length)); assertThat(e.getMessage(), is("Maximum limit of [" + length + "] bytes reached")); } public void testMarking() { ByteSizeValue byteSizeValue = new ByteSizeValue(1, ByteSizeUnit.BYTES); - SizeLimitInputStream is = new SizeLimitInputStream(byteSizeValue, - new ByteArrayInputStream("empty".getBytes(UTF_8))); + SizeLimitInputStream is = new SizeLimitInputStream(byteSizeValue, new ByteArrayInputStream("empty".getBytes(UTF_8))); assertThat(is.markSupported(), is(false)); expectThrows(UnsupportedOperationException.class, () -> is.mark(10)); IOException e = expectThrows(IOException.class, () -> is.reset()); @@ -42,8 +41,7 @@ public void testMarking() { private void test(int inputStreamLength, int maxAllowedSize) throws IOException { String data = randomAlphaOfLength(inputStreamLength); ByteSizeValue byteSizeValue = new ByteSizeValue(maxAllowedSize, ByteSizeUnit.BYTES); - SizeLimitInputStream is = new SizeLimitInputStream(byteSizeValue, - new ByteArrayInputStream(data.getBytes(UTF_8))); + SizeLimitInputStream is = new SizeLimitInputStream(byteSizeValue, new ByteArrayInputStream(data.getBytes(UTF_8))); if (randomBoolean()) { is.read(new byte[inputStreamLength]); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/text/TextTemplateTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/text/TextTemplateTests.java index bfda8137b9a22..6e9bee5922c73 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/text/TextTemplateTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/common/text/TextTemplateTests.java @@ -7,15 +7,15 @@ package org.elasticsearch.xpack.watcher.common.text; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParseException; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptService; import org.elasticsearch.script.ScriptType; import org.elasticsearch.script.TemplateScript; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParseException; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.watcher.Watcher; import org.junit.Before; @@ -59,17 +59,25 @@ public void testRender() throws Exception { merged = unmodifiableMap(merged); ScriptType type = randomFrom(ScriptType.values()); - TemplateScript.Factory compiledTemplate = templateParams -> - new TemplateScript(templateParams) { - @Override - public String execute() { - return "rendered_text"; - } - }; - - when(service.compile(new Script(type, type == ScriptType.STORED ? null : lang, templateText, - type == ScriptType.INLINE ? Collections.singletonMap("content_type", "text/plain") : null, - merged), Watcher.SCRIPT_TEMPLATE_CONTEXT)).thenReturn(compiledTemplate); + TemplateScript.Factory compiledTemplate = templateParams -> new TemplateScript(templateParams) { + @Override + public String execute() { + return "rendered_text"; + } + }; + + when( + service.compile( + new Script( + type, + type == ScriptType.STORED ? null : lang, + templateText, + type == ScriptType.INLINE ? Collections.singletonMap("content_type", "text/plain") : null, + merged + ), + Watcher.SCRIPT_TEMPLATE_CONTEXT + ) + ).thenReturn(compiledTemplate); TextTemplate template = templateBuilder(type, templateText, params); assertThat(engine.render(template, model), is("rendered_text")); @@ -81,17 +89,25 @@ public void testRenderOverridingModel() throws Exception { Map model = singletonMap("key", "model_val"); ScriptType type = randomFrom(ScriptType.values()); - TemplateScript.Factory compiledTemplate = templateParams -> - new TemplateScript(templateParams) { - @Override - public String execute() { - return "rendered_text"; - } - }; - - when(service.compile(new Script(type, type == ScriptType.STORED ? null : lang, templateText, - type == ScriptType.INLINE ? Collections.singletonMap("content_type", "text/plain") : null, - model), Watcher.SCRIPT_TEMPLATE_CONTEXT)).thenReturn(compiledTemplate); + TemplateScript.Factory compiledTemplate = templateParams -> new TemplateScript(templateParams) { + @Override + public String execute() { + return "rendered_text"; + } + }; + + when( + service.compile( + new Script( + type, + type == ScriptType.STORED ? null : lang, + templateText, + type == ScriptType.INLINE ? Collections.singletonMap("content_type", "text/plain") : null, + model + ), + Watcher.SCRIPT_TEMPLATE_CONTEXT + ) + ).thenReturn(compiledTemplate); TextTemplate template = templateBuilder(type, templateText, params); assertThat(engine.render(template, model), is("rendered_text")); @@ -101,17 +117,19 @@ public void testRenderDefaults() throws Exception { String templateText = "{{_template}}"; Map model = singletonMap("key", "model_val"); - TemplateScript.Factory compiledTemplate = templateParams -> - new TemplateScript(templateParams) { - @Override - public String execute() { - return "rendered_text"; - } - }; + TemplateScript.Factory compiledTemplate = templateParams -> new TemplateScript(templateParams) { + @Override + public String execute() { + return "rendered_text"; + } + }; - when(service.compile(new Script(ScriptType.INLINE, lang, templateText, - Collections.singletonMap("content_type", "text/plain"), model), Watcher.SCRIPT_TEMPLATE_CONTEXT)) - .thenReturn(compiledTemplate); + when( + service.compile( + new Script(ScriptType.INLINE, lang, templateText, Collections.singletonMap("content_type", "text/plain"), model), + Watcher.SCRIPT_TEMPLATE_CONTEXT + ) + ).thenReturn(compiledTemplate); TextTemplate template = new TextTemplate(templateText); assertThat(engine.render(template, model), is("rendered_text")); @@ -133,17 +151,19 @@ private void assertScriptServiceInvoked(final String input) { ScriptService scriptService = mock(ScriptService.class); TextTemplateEngine e = new TextTemplateEngine(scriptService); - TemplateScript.Factory compiledTemplate = templateParams -> - new TemplateScript(templateParams) { - @Override - public String execute() { - return input.toUpperCase(Locale.ROOT); - } - }; + TemplateScript.Factory compiledTemplate = templateParams -> new TemplateScript(templateParams) { + @Override + public String execute() { + return input.toUpperCase(Locale.ROOT); + } + }; - when(scriptService.compile(new Script(ScriptType.INLINE, lang, input, - Collections.singletonMap("content_type", "text/plain"), Collections.emptyMap()), Watcher.SCRIPT_TEMPLATE_CONTEXT)) - .thenReturn(compiledTemplate); + when( + scriptService.compile( + new Script(ScriptType.INLINE, lang, input, Collections.singletonMap("content_type", "text/plain"), Collections.emptyMap()), + Watcher.SCRIPT_TEMPLATE_CONTEXT + ) + ).thenReturn(compiledTemplate); String output = e.render(new TextTemplate(input), Collections.emptyMap()); verify(scriptService).compile(any(), any()); @@ -152,8 +172,7 @@ public String execute() { public void testParser() throws Exception { ScriptType type = randomScriptType(); - TextTemplate template = - templateBuilder(type, "_template", singletonMap("param_key", "param_val")); + TextTemplate template = templateBuilder(type, "_template", singletonMap("param_key", "param_val")); XContentBuilder builder = jsonBuilder().startObject(); switch (type) { case INLINE: @@ -174,8 +193,7 @@ public void testParser() throws Exception { public void testParserParserSelfGenerated() throws Exception { ScriptType type = randomScriptType(); - TextTemplate template = - templateBuilder(type, "_template", singletonMap("param_key", "param_val")); + TextTemplate template = templateBuilder(type, "_template", singletonMap("param_key", "param_val")); XContentBuilder builder = jsonBuilder().value(template); BytesReference bytes = BytesReference.bytes(builder); @@ -187,9 +205,7 @@ public void testParserParserSelfGenerated() throws Exception { } public void testParserInvalidUnexpectedField() throws Exception { - XContentBuilder builder = jsonBuilder().startObject() - .field("unknown_field", "value") - .endObject(); + XContentBuilder builder = jsonBuilder().startObject().field("unknown_field", "value").endObject(); BytesReference bytes = BytesReference.bytes(builder); XContentParser parser = createParser(JsonXContent.jsonXContent, bytes); parser.nextToken(); @@ -203,10 +219,11 @@ public void testParserInvalidUnexpectedField() throws Exception { public void testParserInvalidUnknownScriptType() throws Exception { XContentBuilder builder = jsonBuilder().startObject() - .field("template", "_template") - .field("type", "unknown_type") - .startObject("params").endObject() - .endObject(); + .field("template", "_template") + .field("type", "unknown_type") + .startObject("params") + .endObject() + .endObject(); BytesReference bytes = BytesReference.bytes(builder); XContentParser parser = createParser(JsonXContent.jsonXContent, bytes); parser.nextToken(); @@ -216,9 +233,10 @@ public void testParserInvalidUnknownScriptType() throws Exception { public void testParserInvalidMissingText() throws Exception { XContentBuilder builder = jsonBuilder().startObject() - .field("type", ScriptType.STORED) - .startObject("params").endObject() - .endObject(); + .field("type", ScriptType.STORED) + .startObject("params") + .endObject() + .endObject(); BytesReference bytes = BytesReference.bytes(builder); XContentParser parser = createParser(JsonXContent.jsonXContent, bytes); parser.nextToken(); @@ -246,14 +264,14 @@ private TextTemplate createTextTemplate(ScriptType type, String idOrCode) { template = new TextTemplate(new Script(type, lang, idOrCode, Collections.emptyMap(), Collections.emptyMap())); } } else { - template = new TextTemplate(idOrCode, null, type, null); + template = new TextTemplate(idOrCode, null, type, null); } return template; } public void testNullObject() throws Exception { - assertThat(engine.render(null ,new HashMap<>()), is(nullValue())); + assertThat(engine.render(null, new HashMap<>()), is(nullValue())); } private TextTemplate templateBuilder(ScriptType type, String text, Map params) { diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/condition/AlwaysConditionTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/condition/AlwaysConditionTests.java index 484ad45ddff27..bcb45cc75216e 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/condition/AlwaysConditionTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/condition/AlwaysConditionTests.java @@ -7,11 +7,11 @@ package org.elasticsearch.xpack.watcher.condition; import org.elasticsearch.ElasticsearchParseException; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptService; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.watcher.condition.ExecutableCondition; import java.time.Clock; @@ -36,16 +36,16 @@ public void testParserValid() throws Exception { } public void testParserInvalid() throws Exception { - XContentBuilder builder = jsonBuilder() - .startObject() - .field("foo", "bar") - .endObject(); + XContentBuilder builder = jsonBuilder().startObject().field("foo", "bar").endObject(); XContentParser parser = createParser(builder); parser.nextToken(); try { - InternalAlwaysCondition.parse( "_id", parser); - fail("expected a condition exception trying to parse an invalid condition XContent, [" - + InternalAlwaysCondition.TYPE + "] condition should not parse with a body"); + InternalAlwaysCondition.parse("_id", parser); + fail( + "expected a condition exception trying to parse an invalid condition XContent, [" + + InternalAlwaysCondition.TYPE + + "] condition should not parse with a body" + ); } catch (ElasticsearchParseException e) { assertThat(e.getMessage(), containsString("expected an empty object but found [foo]")); } @@ -55,15 +55,19 @@ public static ExecutableCondition randomCondition(ScriptService scriptService) { String type = randomFrom(ScriptCondition.TYPE, InternalAlwaysCondition.TYPE, CompareCondition.TYPE, ArrayCompareCondition.TYPE); switch (type) { case ScriptCondition.TYPE: - Script mockScript = mockScript("_script"); - return new ScriptCondition(mockScript, scriptService); + Script mockScript = mockScript("_script"); + return new ScriptCondition(mockScript, scriptService); case CompareCondition.TYPE: - return new CompareCondition("_path", randomFrom(CompareCondition.Op.values()), randomFrom(5, "3"), - Clock.systemUTC()); + return new CompareCondition("_path", randomFrom(CompareCondition.Op.values()), randomFrom(5, "3"), Clock.systemUTC()); case ArrayCompareCondition.TYPE: - return new ArrayCompareCondition("_array_path", "_path", - randomFrom(ArrayCompareCondition.Op.values()), randomFrom(5, "3"), ArrayCompareCondition.Quantifier.SOME, - Clock.systemUTC()); + return new ArrayCompareCondition( + "_array_path", + "_path", + randomFrom(ArrayCompareCondition.Op.values()), + randomFrom(5, "3"), + ArrayCompareCondition.Quantifier.SOME, + Clock.systemUTC() + ); default: return InternalAlwaysCondition.INSTANCE; } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/condition/ArrayCompareConditionTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/condition/ArrayCompareConditionTests.java index e96db300eba19..7dd6537e3736b 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/condition/ArrayCompareConditionTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/condition/ArrayCompareConditionTests.java @@ -8,10 +8,10 @@ import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext; import org.elasticsearch.xpack.core.watcher.watch.ClockMock; import org.elasticsearch.xpack.core.watcher.watch.Payload; @@ -109,8 +109,7 @@ public void testExecute() { logger.debug("quantifier [{}]", quantifier); logger.debug("met [{}]", met); - ArrayCompareCondition condition = new ArrayCompareCondition("ctx.payload.value", "", op, value, quantifier, - Clock.systemUTC()); + ArrayCompareCondition condition = new ArrayCompareCondition("ctx.payload.value", "", op, value, quantifier, Clock.systemUTC()); WatchExecutionContext ctx = mockExecutionContext("_name", new Payload.Simple("value", values)); assertThat(condition.execute(ctx).met(), is(met)); } @@ -139,8 +138,14 @@ public void testExecutePath() { logger.debug("quantifier [{}]", quantifier); logger.debug("met [{}]", met); - ArrayCompareCondition condition = new ArrayCompareCondition("ctx.payload.value", "doc_count", op, value, quantifier, - Clock.systemUTC()); + ArrayCompareCondition condition = new ArrayCompareCondition( + "ctx.payload.value", + "doc_count", + op, + value, + quantifier, + Clock.systemUTC() + ); WatchExecutionContext ctx = mockExecutionContext("_name", new Payload.Simple("value", values)); assertThat(condition.execute(ctx).met(), is(met)); } @@ -148,12 +153,14 @@ public void testExecutePath() { public void testExecuteDateMath() { ClockMock clock = ClockMock.frozen(); boolean met = randomBoolean(); - ArrayCompareCondition.Op op = met ? - randomFrom(ArrayCompareCondition.Op.GT, ArrayCompareCondition.Op.GTE, ArrayCompareCondition.Op.NOT_EQ) : - randomFrom(ArrayCompareCondition.Op.LT, ArrayCompareCondition.Op.LTE, ArrayCompareCondition.Op.EQ); - - ArrayCompareCondition.Quantifier quantifier = randomFrom(ArrayCompareCondition.Quantifier.ALL, - ArrayCompareCondition.Quantifier.SOME); + ArrayCompareCondition.Op op = met + ? randomFrom(ArrayCompareCondition.Op.GT, ArrayCompareCondition.Op.GTE, ArrayCompareCondition.Op.NOT_EQ) + : randomFrom(ArrayCompareCondition.Op.LT, ArrayCompareCondition.Op.LTE, ArrayCompareCondition.Op.EQ); + + ArrayCompareCondition.Quantifier quantifier = randomFrom( + ArrayCompareCondition.Quantifier.ALL, + ArrayCompareCondition.Quantifier.SOME + ); String value = "<{now-1d}>"; int numberOfValues = randomIntBetween(1, 10); List values = new ArrayList<>(numberOfValues); @@ -171,16 +178,15 @@ public void testParse() throws IOException { ArrayCompareCondition.Op op = randomFrom(ArrayCompareCondition.Op.values()); ArrayCompareCondition.Quantifier quantifier = randomFrom(ArrayCompareCondition.Quantifier.values()); Object value = randomFrom("value", 1, null); - XContentBuilder builder = - jsonBuilder().startObject() - .startObject("key1.key2") - .field("path", "key3.key4") - .startObject(op.id()) - .field("value", value) - .field("quantifier", quantifier.id()) - .endObject() - .endObject() - .endObject(); + XContentBuilder builder = jsonBuilder().startObject() + .startObject("key1.key2") + .field("path", "key3.key4") + .startObject(op.id()) + .field("value", value) + .field("quantifier", quantifier.id()) + .endObject() + .endObject() + .endObject(); XContentParser parser = createParser(JsonXContent.jsonXContent, BytesReference.bytes(builder)); parser.nextToken(); @@ -198,16 +204,15 @@ public void testParse() throws IOException { public void testParseContainsUnknownOperator() throws IOException { ArrayCompareCondition.Quantifier quantifier = randomFrom(ArrayCompareCondition.Quantifier.values()); Object value = randomFrom("value", 1, null); - XContentBuilder builder = - jsonBuilder().startObject() - .startObject("key1.key2") - .field("path", "key3.key4") - .startObject("unknown") - .field("value", value) - .field("quantifier", quantifier.id()) - .endObject() - .endObject() - .endObject(); + XContentBuilder builder = jsonBuilder().startObject() + .startObject("key1.key2") + .field("path", "key3.key4") + .startObject("unknown") + .field("value", value) + .field("quantifier", quantifier.id()) + .endObject() + .endObject() + .endObject(); XContentParser parser = createParser(JsonXContent.jsonXContent, BytesReference.bytes(builder)); parser.nextToken(); @@ -221,16 +226,15 @@ public void testParseContainsUnknownOperator() throws IOException { public void testParseContainsUnknownQuantifier() throws IOException { ArrayCompareCondition.Op op = randomFrom(ArrayCompareCondition.Op.values()); Object value = randomFrom("value", 1, null); - XContentBuilder builder = - jsonBuilder().startObject() - .startObject("key1.key2") - .field("path", "key3.key4") - .startObject(op.id()) - .field("value", value) - .field("quantifier", "unknown") - .endObject() - .endObject() - .endObject(); + XContentBuilder builder = jsonBuilder().startObject() + .startObject("key1.key2") + .field("path", "key3.key4") + .startObject(op.id()) + .field("value", value) + .field("quantifier", "unknown") + .endObject() + .endObject() + .endObject(); XContentParser parser = createParser(JsonXContent.jsonXContent, BytesReference.bytes(builder)); parser.nextToken(); @@ -245,17 +249,16 @@ public void testParseContainsUnexpectedFieldInComparisonOperator() throws IOExce ArrayCompareCondition.Op op = randomFrom(ArrayCompareCondition.Op.values()); ArrayCompareCondition.Quantifier quantifier = randomFrom(ArrayCompareCondition.Quantifier.values()); Object value = randomFrom("value", 1, null); - XContentBuilder builder = - jsonBuilder().startObject() - .startObject("key1.key2") - .field("path", "key3.key4") - .startObject(op.id()) - .field("value", value) - .field("quantifier", quantifier.id()) - .field("unexpected", "unexpected") - .endObject() - .endObject() - .endObject(); + XContentBuilder builder = jsonBuilder().startObject() + .startObject("key1.key2") + .field("path", "key3.key4") + .startObject(op.id()) + .field("value", value) + .field("quantifier", quantifier.id()) + .field("unexpected", "unexpected") + .endObject() + .endObject() + .endObject(); XContentParser parser = createParser(JsonXContent.jsonXContent, BytesReference.bytes(builder)); parser.nextToken(); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/condition/CompareConditionTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/condition/CompareConditionTests.java index 4c58291c03c68..6fca7f6242558 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/condition/CompareConditionTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/condition/CompareConditionTests.java @@ -8,10 +8,10 @@ import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext; import org.elasticsearch.xpack.core.watcher.watch.ClockMock; import org.elasticsearch.xpack.core.watcher.watch.Payload; @@ -35,7 +35,7 @@ public void testOpEvalEQ() throws Exception { assertThat(CompareCondition.Op.EQ.eval(null, null), is(true)); assertThat(CompareCondition.Op.EQ.eval(4, 3.0), is(false)); assertThat(CompareCondition.Op.EQ.eval(3, 3.0), is(true)); - assertThat(CompareCondition.Op.EQ.eval(2, Float.valueOf((float)3.0)), is(false)); + assertThat(CompareCondition.Op.EQ.eval(2, Float.valueOf((float) 3.0)), is(false)); assertThat(CompareCondition.Op.EQ.eval(3, null), is(false)); assertThat(CompareCondition.Op.EQ.eval(2, "2"), is(true)); // comparing as strings assertThat(CompareCondition.Op.EQ.eval(3, "4"), is(false)); // comparing as strings @@ -61,7 +61,7 @@ public void testOpEvalNotEQ() throws Exception { assertThat(CompareCondition.Op.NOT_EQ.eval(null, null), is(false)); assertThat(CompareCondition.Op.NOT_EQ.eval(4, 3.0), is(true)); assertThat(CompareCondition.Op.NOT_EQ.eval(3, 3.0), is(false)); - assertThat(CompareCondition.Op.NOT_EQ.eval(2, Float.valueOf((float)3.0)), is(true)); + assertThat(CompareCondition.Op.NOT_EQ.eval(2, Float.valueOf((float) 3.0)), is(true)); assertThat(CompareCondition.Op.NOT_EQ.eval(3, null), is(true)); assertThat(CompareCondition.Op.NOT_EQ.eval(2, "2"), is(false)); // comparing as strings assertThat(CompareCondition.Op.NOT_EQ.eval(3, "4"), is(true)); // comparing as strings @@ -85,7 +85,7 @@ public void testOpEvalNotEQ() throws Exception { public void testOpEvalGTE() throws Exception { assertThat(CompareCondition.Op.GTE.eval(4, 3.0), is(true)); assertThat(CompareCondition.Op.GTE.eval(3, 3.0), is(true)); - assertThat(CompareCondition.Op.GTE.eval(2, Float.valueOf((float)3.0)), is(false)); + assertThat(CompareCondition.Op.GTE.eval(2, Float.valueOf((float) 3.0)), is(false)); assertThat(CompareCondition.Op.GTE.eval(3, null), is(false)); assertThat(CompareCondition.Op.GTE.eval(3, "2"), is(true)); // comparing as strings assertThat(CompareCondition.Op.GTE.eval(3, "4"), is(false)); // comparing as strings @@ -105,7 +105,7 @@ public void testOpEvalGTE() throws Exception { public void testOpEvalGT() throws Exception { assertThat(CompareCondition.Op.GT.eval(4, 3.0), is(true)); assertThat(CompareCondition.Op.GT.eval(3, 3.0), is(false)); - assertThat(CompareCondition.Op.GT.eval(2, Float.valueOf((float)3.0)), is(false)); + assertThat(CompareCondition.Op.GT.eval(2, Float.valueOf((float) 3.0)), is(false)); assertThat(CompareCondition.Op.GT.eval(3, null), is(false)); assertThat(CompareCondition.Op.GT.eval(3, "2"), is(true)); // comparing as strings assertThat(CompareCondition.Op.GT.eval(3, "4"), is(false)); // comparing as strings @@ -177,8 +177,9 @@ public void testExecute() throws Exception { public void testExecuteDateMath() throws Exception { ClockMock clock = ClockMock.frozen(); boolean met = randomBoolean(); - Op op = met ? randomFrom(CompareCondition.Op.GT, CompareCondition.Op.GTE, CompareCondition.Op.NOT_EQ) : - randomFrom(CompareCondition.Op.LT, CompareCondition.Op.LTE, CompareCondition.Op.EQ); + Op op = met + ? randomFrom(CompareCondition.Op.GT, CompareCondition.Op.GTE, CompareCondition.Op.NOT_EQ) + : randomFrom(CompareCondition.Op.LT, CompareCondition.Op.LTE, CompareCondition.Op.EQ); String value = "<{now-1d}>"; ZonedDateTime payloadValue = clock.instant().atZone(ZoneId.systemDefault()); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/condition/NeverConditionTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/condition/NeverConditionTests.java index 84cb250779421..1bb843f174fca 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/condition/NeverConditionTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/condition/NeverConditionTests.java @@ -7,9 +7,9 @@ package org.elasticsearch.xpack.watcher.condition; import org.elasticsearch.ElasticsearchParseException; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.watcher.condition.ExecutableCondition; import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; @@ -41,8 +41,11 @@ public void testParserInvalid() throws Exception { parser.nextToken(); try { NeverCondition.parse("_id", parser); - fail("expected a condition exception trying to parse an invalid condition XContent, [" - + InternalAlwaysCondition.TYPE + "] condition should not parse with a body"); + fail( + "expected a condition exception trying to parse an invalid condition XContent, [" + + InternalAlwaysCondition.TYPE + + "] condition should not parse with a body" + ); } catch (ElasticsearchParseException e) { assertThat(e.getMessage(), containsString("expected an empty object but found [foo]")); } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/condition/ScriptConditionTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/condition/ScriptConditionTests.java index bceac4c6c40df..059465f1a7584 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/condition/ScriptConditionTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/condition/ScriptConditionTests.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.xpack.watcher.condition; - import org.elasticsearch.ResourceNotFoundException; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.ShardSearchFailure; @@ -15,10 +14,6 @@ import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.common.xcontent.support.XContentMapValues; import org.elasticsearch.script.GeneralScriptException; import org.elasticsearch.script.Script; @@ -28,6 +23,10 @@ import org.elasticsearch.script.ScriptType; import org.elasticsearch.search.internal.InternalSearchResponse; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.watcher.condition.ExecutableCondition; import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext; import org.elasticsearch.xpack.core.watcher.watch.Payload; @@ -66,8 +65,13 @@ public void init() throws IOException { }); scripts.put("null.foo", s -> { - throw new ScriptException("Error evaluating null.foo", new IllegalArgumentException(), emptyList(), - "null.foo", Script.DEFAULT_SCRIPT_LANG); + throw new ScriptException( + "Error evaluating null.foo", + new IllegalArgumentException(), + emptyList(), + "null.foo", + Script.DEFAULT_SCRIPT_LANG + ); }); scripts.put("ctx.payload.hits.total.value > 1", vars -> { @@ -91,18 +95,38 @@ public void init() throws IOException { public void testExecute() throws Exception { ScriptCondition condition = new ScriptCondition(mockScript("ctx.payload.hits.total.value > 1"), scriptService); - SearchResponse response = new SearchResponse(InternalSearchResponse.empty(), "", 3, 3, 0, 500L, ShardSearchFailure.EMPTY_ARRAY, - SearchResponse.Clusters.EMPTY); + SearchResponse response = new SearchResponse( + InternalSearchResponse.empty(), + "", + 3, + 3, + 0, + 500L, + ShardSearchFailure.EMPTY_ARRAY, + SearchResponse.Clusters.EMPTY + ); WatchExecutionContext ctx = mockExecutionContext("_name", new Payload.XContent(response, Settings.EMPTY_PARAMS)); assertFalse(condition.execute(ctx).met()); } public void testExecuteMergedParams() throws Exception { - Script script = new Script(ScriptType.INLINE, "mockscript", - "ctx.payload.hits.total.value > params.threshold", singletonMap("threshold", 1)); + Script script = new Script( + ScriptType.INLINE, + "mockscript", + "ctx.payload.hits.total.value > params.threshold", + singletonMap("threshold", 1) + ); ScriptCondition executable = new ScriptCondition(script, scriptService); - SearchResponse response = new SearchResponse(InternalSearchResponse.empty(), "", 3, 3, 0, 500L, ShardSearchFailure.EMPTY_ARRAY, - SearchResponse.Clusters.EMPTY); + SearchResponse response = new SearchResponse( + InternalSearchResponse.empty(), + "", + 3, + 3, + 0, + 500L, + ShardSearchFailure.EMPTY_ARRAY, + SearchResponse.Clusters.EMPTY + ); WatchExecutionContext ctx = mockExecutionContext("_name", new Payload.XContent(response, Settings.EMPTY_PARAMS)); assertFalse(executable.execute(ctx).met()); } @@ -115,13 +139,20 @@ public void testParserValid() throws Exception { parser.nextToken(); ExecutableCondition executable = ScriptCondition.parse(scriptService, "_watch", parser); - SearchResponse response = new SearchResponse(InternalSearchResponse.empty(), "", 3, 3, 0, 500L, ShardSearchFailure.EMPTY_ARRAY, - SearchResponse.Clusters.EMPTY); + SearchResponse response = new SearchResponse( + InternalSearchResponse.empty(), + "", + 3, + 3, + 0, + 500L, + ShardSearchFailure.EMPTY_ARRAY, + SearchResponse.Clusters.EMPTY + ); WatchExecutionContext ctx = mockExecutionContext("_name", new Payload.XContent(response, Settings.EMPTY_PARAMS)); assertFalse(executable.execute(ctx).met()); - builder = createConditionContent("return true", "mockscript", ScriptType.INLINE); parser = createParser(builder); parser.nextToken(); @@ -141,8 +172,7 @@ public void testParserInvalid() throws Exception { ScriptCondition.parse(scriptService, "_id", parser); fail("expected a condition exception trying to parse an invalid condition XContent"); } catch (IllegalArgumentException e) { - assertThat(e.getMessage(), - containsString("must specify either [source] for an inline script or [id] for a stored script")); + assertThat(e.getMessage(), containsString("must specify either [source] for an inline script or [id] for a stored script")); } } @@ -163,8 +193,7 @@ public void testScriptConditionParserBadScript() throws Exception { XContentParser parser = createParser(builder); parser.nextToken(); - expectThrows(expectedException, - () -> ScriptCondition.parse(scriptService, "_watch", parser)); + expectThrows(expectedException, () -> ScriptCondition.parse(scriptService, "_watch", parser)); } public void testScriptConditionParser_badLang() throws Exception { @@ -172,16 +201,25 @@ public void testScriptConditionParser_badLang() throws Exception { XContentBuilder builder = createConditionContent(script, "not_a_valid_lang", ScriptType.INLINE); XContentParser parser = createParser(builder); parser.nextToken(); - IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, - () -> ScriptCondition.parse(scriptService, "_watch", parser)); + IllegalArgumentException exception = expectThrows( + IllegalArgumentException.class, + () -> ScriptCondition.parse(scriptService, "_watch", parser) + ); assertThat(exception.getMessage(), containsString("script_lang not supported [not_a_valid_lang]")); } public void testScriptConditionThrowException() throws Exception { - ScriptCondition condition = new ScriptCondition( - mockScript("null.foo"), scriptService); - SearchResponse response = new SearchResponse(InternalSearchResponse.empty(), "", 3, 3, 0, 500L, ShardSearchFailure.EMPTY_ARRAY, - SearchResponse.Clusters.EMPTY); + ScriptCondition condition = new ScriptCondition(mockScript("null.foo"), scriptService); + SearchResponse response = new SearchResponse( + InternalSearchResponse.empty(), + "", + 3, + 3, + 0, + 500L, + ShardSearchFailure.EMPTY_ARRAY, + SearchResponse.Clusters.EMPTY + ); WatchExecutionContext ctx = mockExecutionContext("_name", new Payload.XContent(response, ToXContent.EMPTY_PARAMS)); ScriptException exception = expectThrows(ScriptException.class, () -> condition.execute(ctx)); assertThat(exception.getMessage(), containsString("Error evaluating null.foo")); @@ -189,11 +227,24 @@ public void testScriptConditionThrowException() throws Exception { public void testScriptConditionAccessCtx() throws Exception { ScriptCondition condition = new ScriptCondition( - mockScript("ctx.trigger.scheduled_time.toInstant().toEpochMill() < new Date().time"), scriptService); - SearchResponse response = new SearchResponse(InternalSearchResponse.empty(), "", 3, 3, 0, 500L, ShardSearchFailure.EMPTY_ARRAY, - SearchResponse.Clusters.EMPTY); - WatchExecutionContext ctx = mockExecutionContext("_name", ZonedDateTime.now(ZoneOffset.UTC), - new Payload.XContent(response, ToXContent.EMPTY_PARAMS)); + mockScript("ctx.trigger.scheduled_time.toInstant().toEpochMill() < new Date().time"), + scriptService + ); + SearchResponse response = new SearchResponse( + InternalSearchResponse.empty(), + "", + 3, + 3, + 0, + 500L, + ShardSearchFailure.EMPTY_ARRAY, + SearchResponse.Clusters.EMPTY + ); + WatchExecutionContext ctx = mockExecutionContext( + "_name", + ZonedDateTime.now(ZoneOffset.UTC), + new Payload.XContent(response, ToXContent.EMPTY_PARAMS) + ); Thread.sleep(10); assertThat(condition.execute(ctx).met(), is(true)); } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/execution/ExecutionServiceTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/execution/ExecutionServiceTests.java index 93479d047f15f..0dab6aeaa2d3c 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/execution/ExecutionServiceTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/execution/ExecutionServiceTests.java @@ -108,10 +108,10 @@ import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.Matchers.sameInstance; -import static org.mockito.ArgumentMatchers.nullable; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.anyObject; +import static org.mockito.ArgumentMatchers.nullable; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.eq; @@ -121,7 +121,7 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -@SuppressWarnings({"rawtypes", "unchecked"}) +@SuppressWarnings({ "rawtypes", "unchecked" }) public class ExecutionServiceTests extends ESTestCase { private Payload payload; @@ -158,13 +158,27 @@ public void init() throws Exception { when(threadPool.getThreadContext()).thenReturn(new ThreadContext(Settings.EMPTY)); parser = mock(WatchParser.class); - DiscoveryNode discoveryNode = new DiscoveryNode("node_1", ESTestCase.buildNewFakeTransportAddress(), Collections.emptyMap(), - DiscoveryNodeRole.roles(), Version.CURRENT); + DiscoveryNode discoveryNode = new DiscoveryNode( + "node_1", + ESTestCase.buildNewFakeTransportAddress(), + Collections.emptyMap(), + DiscoveryNodeRole.roles(), + Version.CURRENT + ); ClusterService clusterService = mock(ClusterService.class); when(clusterService.localNode()).thenReturn(discoveryNode); - executionService = new ExecutionService(Settings.EMPTY, historyStore, triggeredWatchStore, executor, clock, parser, - clusterService, client, EsExecutors.DIRECT_EXECUTOR_SERVICE); + executionService = new ExecutionService( + Settings.EMPTY, + historyStore, + triggeredWatchStore, + executor, + clock, + parser, + clusterService, + client, + EsExecutors.DIRECT_EXECUTOR_SERVICE + ); } public void testExecute() throws Exception { @@ -866,8 +880,10 @@ public void testThatTriggeredWatchDeletionWorksOnExecutionRejection() throws Exc Wid wid = new Wid(watch.id(), ZonedDateTime.now(ZoneOffset.UTC)); - TriggeredWatch triggeredWatch = new TriggeredWatch(wid, - new ScheduleTriggerEvent(ZonedDateTime.now(ZoneOffset.UTC) ,ZonedDateTime.now(ZoneOffset.UTC))); + TriggeredWatch triggeredWatch = new TriggeredWatch( + wid, + new ScheduleTriggerEvent(ZonedDateTime.now(ZoneOffset.UTC), ZonedDateTime.now(ZoneOffset.UTC)) + ); executionService.executeTriggeredWatches(Collections.singleton(triggeredWatch)); ArgumentCaptor deleteCaptor = ArgumentCaptor.forClass(DeleteRequest.class); @@ -893,10 +909,9 @@ public void testForcePutHistoryOnExecutionRejection() throws Exception { mockGetWatchResponse(client, "foo", getResponse); ActionFuture actionFuture = mock(ActionFuture.class); when(actionFuture.get()).thenReturn(""); - when(client.index(any())) - .thenThrow(new VersionConflictEngineException( - new ShardId(new Index("mockindex", "mockuuid"), 0), "id", "explaination")) - .thenReturn(actionFuture); + when(client.index(any())).thenThrow( + new VersionConflictEngineException(new ShardId(new Index("mockindex", "mockuuid"), 0), "id", "explaination") + ).thenReturn(actionFuture); when(client.delete(any())).thenReturn(actionFuture); when(parser.parseWithSecrets(eq("foo"), eq(true), any(), any(), any(), anyLong(), anyLong())).thenReturn(watch); @@ -906,8 +921,10 @@ public void testForcePutHistoryOnExecutionRejection() throws Exception { Wid wid = new Wid(watch.id(), ZonedDateTime.now(ZoneOffset.UTC)); - TriggeredWatch triggeredWatch = new TriggeredWatch(wid, - new ScheduleTriggerEvent(ZonedDateTime.now(ZoneOffset.UTC), ZonedDateTime.now(ZoneOffset.UTC))); + TriggeredWatch triggeredWatch = new TriggeredWatch( + wid, + new ScheduleTriggerEvent(ZonedDateTime.now(ZoneOffset.UTC), ZonedDateTime.now(ZoneOffset.UTC)) + ); executionService.executeTriggeredWatches(Collections.singleton(triggeredWatch)); ArgumentCaptor deleteCaptor = ArgumentCaptor.forClass(DeleteRequest.class); @@ -933,8 +950,12 @@ public void testThatTriggeredWatchDeletionHappensOnlyIfWatchExists() throws Exce ZonedDateTime now = clock.instant().atZone(ZoneOffset.UTC); ScheduleTriggerEvent event = new ScheduleTriggerEvent("_id", now, now); - WatchExecutionContext context = ManualExecutionContext.builder(watch, false, new ManualTriggerEvent("foo", event), - timeValueSeconds(5)).build(); + WatchExecutionContext context = ManualExecutionContext.builder( + watch, + false, + new ManualTriggerEvent("foo", event), + timeValueSeconds(5) + ).build(); // action throttler, no throttling Throttler.Result throttleResult = mock(Throttler.Result.class); @@ -983,8 +1004,12 @@ public void testExecuteWatchNotFound() throws Exception { when(watch.id()).thenReturn("_id"); ZonedDateTime epochZeroTime = Instant.EPOCH.atZone(ZoneOffset.UTC); ScheduleTriggerEvent triggerEvent = new ScheduleTriggerEvent(watch.id(), epochZeroTime, epochZeroTime); - TriggeredExecutionContext context = new TriggeredExecutionContext(watch.id(), epochZeroTime, triggerEvent, - TimeValue.timeValueSeconds(5)); + TriggeredExecutionContext context = new TriggeredExecutionContext( + watch.id(), + epochZeroTime, + triggerEvent, + TimeValue.timeValueSeconds(5) + ); GetResponse notFoundResponse = mock(GetResponse.class); when(notFoundResponse.isExists()).thenReturn(false); @@ -1000,8 +1025,12 @@ public void testExecuteWatchIndexNotFoundException() { when(watch.id()).thenReturn("_id"); ZonedDateTime epochZeroTime = Instant.EPOCH.atZone(ZoneOffset.UTC); ScheduleTriggerEvent triggerEvent = new ScheduleTriggerEvent(watch.id(), epochZeroTime, epochZeroTime); - TriggeredExecutionContext context = new TriggeredExecutionContext(watch.id(), epochZeroTime, triggerEvent, - TimeValue.timeValueSeconds(5)); + TriggeredExecutionContext context = new TriggeredExecutionContext( + watch.id(), + epochZeroTime, + triggerEvent, + TimeValue.timeValueSeconds(5) + ); mockGetWatchException(client, "_id", new IndexNotFoundException(".watch")); WatchRecord watchRecord = executionService.execute(context); @@ -1013,10 +1042,12 @@ public void testExecuteWatchParseWatchException() { Watch watch = mock(Watch.class); when(watch.id()).thenReturn("_id"); ZonedDateTime epochZeroTime = Instant.EPOCH.atZone(ZoneOffset.UTC); - TriggeredExecutionContext context = new TriggeredExecutionContext(watch.id(), + TriggeredExecutionContext context = new TriggeredExecutionContext( + watch.id(), epochZeroTime, - new ScheduleTriggerEvent(watch.id(), epochZeroTime, epochZeroTime), - TimeValue.timeValueSeconds(5)); + new ScheduleTriggerEvent(watch.id(), epochZeroTime, epochZeroTime), + TimeValue.timeValueSeconds(5) + ); IOException e = new IOException("something went wrong, i.e. index not found"); mockGetWatchException(client, "_id", e); @@ -1062,7 +1093,7 @@ public void testCurrentExecutionSnapshots() throws Exception { List snapshots = executionService.currentExecutions(); assertThat(snapshots, hasSize(snapshotCount)); - assertThat(snapshots.get(0).watchId(), is("_id" + (snapshotCount-1))); + assertThat(snapshots.get(0).watchId(), is("_id" + (snapshotCount - 1))); assertThat(snapshots.get(snapshots.size() - 1).watchId(), is("_id0")); } @@ -1080,21 +1111,37 @@ public void testQueuedWatches() throws Exception { List queuedWatches = executionService.queuedWatches(); assertThat(queuedWatches, hasSize(queuedWatchCount)); - assertThat(queuedWatches.get(0).watchId(), is("_id" + (queuedWatchCount-1))); + assertThat(queuedWatches.get(0).watchId(), is("_id" + (queuedWatchCount - 1))); assertThat(queuedWatches.get(queuedWatches.size() - 1).watchId(), is("_id0")); } public void testUpdateWatchStatusDoesNotUpdateState() throws Exception { WatchStatus status = new WatchStatus(ZonedDateTime.now(ZoneOffset.UTC), Collections.emptyMap()); - Watch watch = new Watch("_id", new ManualTrigger(), new ExecutableNoneInput(), InternalAlwaysCondition.INSTANCE, null, null, - Collections.emptyList(), null, status, 1L, 1L); + Watch watch = new Watch( + "_id", + new ManualTrigger(), + new ExecutableNoneInput(), + InternalAlwaysCondition.INSTANCE, + null, + null, + Collections.emptyList(), + null, + status, + 1L, + 1L + ); final AtomicBoolean assertionsTriggered = new AtomicBoolean(false); doAnswer(invocation -> { UpdateRequest request = (UpdateRequest) invocation.getArguments()[0]; - try (XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(NamedXContentRegistry.EMPTY, - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, request.doc().source().streamInput())) { + try ( + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + request.doc().source().streamInput() + ) + ) { Map map = parser.map(); Map state = ObjectPath.eval("status.state", map); assertThat(state, is(nullValue())); @@ -1102,8 +1149,7 @@ public void testUpdateWatchStatusDoesNotUpdateState() throws Exception { } PlainActionFuture future = PlainActionFuture.newFuture(); - future.onResponse(new UpdateResponse(null, new ShardId("test", "test", 0), "test", 0, 0, 0, - DocWriteResponse.Result.CREATED)); + future.onResponse(new UpdateResponse(null, new ShardId("test", "test", 0), "test", 0, 0, 0, DocWriteResponse.Result.CREATED)); return future; }).when(client).update(any()); @@ -1118,8 +1164,8 @@ public void testManualWatchExecutionContextGetsAlwaysExecuted() throws Exception ZonedDateTime now = clock.instant().atZone(ZoneOffset.UTC); ScheduleTriggerEvent event = new ScheduleTriggerEvent("_id", now, now); - ManualExecutionContext ctx = ManualExecutionContext.builder(watch, true, - new ManualTriggerEvent("foo", event), timeValueSeconds(5)).build(); + ManualExecutionContext ctx = ManualExecutionContext.builder(watch, true, new ManualTriggerEvent("foo", event), timeValueSeconds(5)) + .build(); when(watch.input()).thenReturn(input); Condition.Result conditionResult = InternalAlwaysCondition.RESULT_INSTANCE; @@ -1165,8 +1211,11 @@ public void testLoadingWatchExecutionUser() throws Exception { context.ensureWatchExists(() -> watch); assertNull(context.getUser()); - Authentication authentication = new Authentication(new User("joe", "admin"), - new Authentication.RealmRef("native_realm", "native", "node1"), null); + Authentication authentication = new Authentication( + new User("joe", "admin"), + new Authentication.RealmRef("native_realm", "native", "node1"), + null + ); // Should no longer be null now that the proper header is set when(status.getHeaders()).thenReturn(Collections.singletonMap(AuthenticationField.AUTHENTICATION_KEY, authentication.encode())); @@ -1189,7 +1238,7 @@ private WatchExecutionContext createMockWatchExecutionContext(String watchId, Zo when(watch.id()).thenReturn(watchId); when(ctx.watch()).thenReturn(watch); - WatchExecutionSnapshot snapshot = new WatchExecutionSnapshot(ctx, new StackTraceElement[]{}); + WatchExecutionSnapshot snapshot = new WatchExecutionSnapshot(ctx, new StackTraceElement[] {}); when(ctx.createSnapshot(anyObject())).thenReturn(snapshot); return ctx; @@ -1220,9 +1269,7 @@ private void mockGetWatchResponse(Client client, String id, GetResponse response if (request.id().equals(id)) { listener.onResponse(response); } else { - GetResult notFoundResult = - new GetResult(request.index(), request.id(), UNASSIGNED_SEQ_NO, 0, - -1, false, null, null, null); + GetResult notFoundResult = new GetResult(request.index(), request.id(), UNASSIGNED_SEQ_NO, 0, -1, false, null, null, null); listener.onResponse(new GetResponse(notFoundResult)); } return null; @@ -1236,9 +1283,7 @@ private void mockGetWatchException(Client client, String id, Exception e) { if (request.id().equals(id)) { listener.onFailure(e); } else { - GetResult notFoundResult = - new GetResult(request.index(), request.id(), UNASSIGNED_SEQ_NO, 0, -1, - false, null, null, null); + GetResult notFoundResult = new GetResult(request.index(), request.id(), UNASSIGNED_SEQ_NO, 0, -1, false, null, null, null); listener.onResponse(new GetResponse(notFoundResult)); } return null; diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/execution/TriggeredWatchStoreTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/execution/TriggeredWatchStoreTests.java index ecc59e2af3e08..a0a0f876f5675 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/execution/TriggeredWatchStoreTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/execution/TriggeredWatchStoreTests.java @@ -41,9 +41,6 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexNotFoundException; import org.elasticsearch.index.shard.ShardId; @@ -53,6 +50,9 @@ import org.elasticsearch.search.internal.InternalSearchResponse; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xpack.core.watcher.execution.TriggeredWatchStoreField; import org.elasticsearch.xpack.core.watcher.execution.Wid; import org.elasticsearch.xpack.core.watcher.watch.ClockMock; @@ -94,8 +94,7 @@ public class TriggeredWatchStoreTests extends ESTestCase { - private Settings indexSettings = settings(Version.CURRENT) - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + private Settings indexSettings = settings(Version.CURRENT).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) .build(); @@ -105,8 +104,7 @@ public class TriggeredWatchStoreTests extends ESTestCase { private final Map bulks = new LinkedHashMap<>(); private BulkProcessor.Listener listener = new BulkProcessor.Listener() { @Override - public void beforeBulk(long executionId, BulkRequest request) { - } + public void beforeBulk(long executionId, BulkRequest request) {} @Override public void afterBulk(long executionId, BulkRequest request, BulkResponse response) { @@ -128,8 +126,10 @@ public void init() { when(client.settings()).thenReturn(settings); when(threadPool.getThreadContext()).thenReturn(new ThreadContext(Settings.EMPTY)); parser = mock(TriggeredWatch.Parser.class); - BulkProcessor bulkProcessor = BulkProcessor. - builder(client::bulk, listener, "TriggeredWatchStoreTests").setConcurrentRequests(0).setBulkActions(1).build(); + BulkProcessor bulkProcessor = BulkProcessor.builder(client::bulk, listener, "TriggeredWatchStoreTests") + .setConcurrentRequests(0) + .setBulkActions(1) + .build(); triggeredWatchStore = new TriggeredWatchStore(settings, client, parser, bulkProcessor); } @@ -152,12 +152,12 @@ public void testValidateNoActivePrimaryShards() { int numShards = 2 + randomInt(2); int numStartedShards = 1; - Settings settings = settings(Version.CURRENT) - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, numShards) + Settings settings = settings(Version.CURRENT).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, numShards) .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) .build(); - metadataBuilder.put(IndexMetadata.builder(TriggeredWatchStoreField.INDEX_NAME).settings(settings) - .numberOfShards(numShards).numberOfReplicas(1)); + metadataBuilder.put( + IndexMetadata.builder(TriggeredWatchStoreField.INDEX_NAME).settings(settings).numberOfShards(numShards).numberOfReplicas(1) + ); final Index index = metadataBuilder.get(TriggeredWatchStoreField.INDEX_NAME).getIndex(); IndexRoutingTable.Builder indexRoutingTableBuilder = IndexRoutingTable.builder(index); for (int i = 0; i < numShards; i++) { @@ -171,10 +171,18 @@ public void testValidateNoActivePrimaryShards() { currentNodeId = null; } ShardId shardId = new ShardId(index, 0); - indexRoutingTableBuilder.addIndexShard(new IndexShardRoutingTable.Builder(shardId) - .addShard(TestShardRouting.newShardRouting(shardId, currentNodeId, null, true, state, - new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, ""))) - .build()); + indexRoutingTableBuilder.addIndexShard( + new IndexShardRoutingTable.Builder(shardId).addShard( + TestShardRouting.newShardRouting( + shardId, + currentNodeId, + null, + true, + state, + new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "") + ) + ).build() + ); indexRoutingTableBuilder.addReplica(); } routingTableBuilder.add(indexRoutingTableBuilder.build()); @@ -195,9 +203,11 @@ public void testFindTriggeredWatchesGoodCase() { final Index index = metadataBuilder.get(TriggeredWatchStoreField.INDEX_NAME).getIndex(); IndexRoutingTable.Builder indexRoutingTableBuilder = IndexRoutingTable.builder(index); ShardId shardId = new ShardId(index, 0); - indexRoutingTableBuilder.addIndexShard(new IndexShardRoutingTable.Builder(shardId) - .addShard(TestShardRouting.newShardRouting(shardId, "_node_id", null, true, ShardRoutingState.STARTED)) - .build()); + indexRoutingTableBuilder.addIndexShard( + new IndexShardRoutingTable.Builder(shardId).addShard( + TestShardRouting.newShardRouting(shardId, "_node_id", null, true, ShardRoutingState.STARTED) + ).build() + ); indexRoutingTableBuilder.addReplica(); routingTableBuilder.add(indexRoutingTableBuilder.build()); csBuilder.metadata(metadataBuilder); @@ -219,7 +229,7 @@ public void testFindTriggeredWatchesGoodCase() { hit.version(1L); hit.shard(new SearchShardTarget("_node_id", new ShardId(index, 0), null)); hit.sourceRef(source); - SearchHits hits = new SearchHits(new SearchHit[]{hit}, new TotalHits(1, TotalHits.Relation.EQUAL_TO), 1.0f); + SearchHits hits = new SearchHits(new SearchHit[] { hit }, new TotalHits(1, TotalHits.Relation.EQUAL_TO), 1.0f); when(searchResponse1.getHits()).thenReturn(hits); when(searchResponse1.getScrollId()).thenReturn("_scrollId"); doAnswer(invocation -> { @@ -234,9 +244,17 @@ public void testFindTriggeredWatchesGoodCase() { hit.version(1L); hit.shard(new SearchShardTarget("_node_id", new ShardId(index, 0), null)); hit.sourceRef(source); - hits = new SearchHits(new SearchHit[]{hit}, new TotalHits(1, TotalHits.Relation.EQUAL_TO), 1.0f); + hits = new SearchHits(new SearchHit[] { hit }, new TotalHits(1, TotalHits.Relation.EQUAL_TO), 1.0f); SearchResponse searchResponse2 = new SearchResponse( - new InternalSearchResponse(hits, null, null, null, false, null, 1), "_scrollId1", 1, 1, 0, 1, null, null); + new InternalSearchResponse(hits, null, null, null, false, null, 1), + "_scrollId1", + 1, + 1, + 0, + 1, + null, + null + ); SearchResponse searchResponse3 = new SearchResponse(InternalSearchResponse.empty(), "_scrollId2", 1, 1, 0, 1, null, null); doAnswer(invocation -> { @@ -300,14 +318,19 @@ public void testLoadStoreAsAlias() { RoutingTable.Builder routingTableBuilder = RoutingTable.builder(); Metadata.Builder metadataBuilder = Metadata.builder(); - metadataBuilder.put(IndexMetadata.builder("triggered-watches-alias").settings(indexSettings) - .putAlias(new AliasMetadata.Builder(TriggeredWatchStoreField.INDEX_NAME).build())); + metadataBuilder.put( + IndexMetadata.builder("triggered-watches-alias") + .settings(indexSettings) + .putAlias(new AliasMetadata.Builder(TriggeredWatchStoreField.INDEX_NAME).build()) + ); final Index index = metadataBuilder.get("triggered-watches-alias").getIndex(); IndexRoutingTable.Builder indexRoutingTableBuilder = IndexRoutingTable.builder(index); ShardId shardId = new ShardId(index, 0); - indexRoutingTableBuilder.addIndexShard(new IndexShardRoutingTable.Builder(shardId) - .addShard(TestShardRouting.newShardRouting(shardId, "_node_id", null, true, ShardRoutingState.STARTED)) - .build()); + indexRoutingTableBuilder.addIndexShard( + new IndexShardRoutingTable.Builder(shardId).addShard( + TestShardRouting.newShardRouting(shardId, "_node_id", null, true, ShardRoutingState.STARTED) + ).build() + ); indexRoutingTableBuilder.addReplica(); routingTableBuilder.add(indexRoutingTableBuilder.build()); csBuilder.metadata(metadataBuilder); @@ -324,22 +347,32 @@ public void testLoadingFailsWithTwoAliases() { Metadata.Builder metadataBuilder = Metadata.builder(); RoutingTable.Builder routingTableBuilder = RoutingTable.builder(); - metadataBuilder.put(IndexMetadata.builder("triggered-watches-alias").settings(indexSettings) - .putAlias(new AliasMetadata.Builder(TriggeredWatchStoreField.INDEX_NAME).build())); - metadataBuilder.put(IndexMetadata.builder("whatever").settings(indexSettings) - .putAlias(new AliasMetadata.Builder(TriggeredWatchStoreField.INDEX_NAME).build())); + metadataBuilder.put( + IndexMetadata.builder("triggered-watches-alias") + .settings(indexSettings) + .putAlias(new AliasMetadata.Builder(TriggeredWatchStoreField.INDEX_NAME).build()) + ); + metadataBuilder.put( + IndexMetadata.builder("whatever") + .settings(indexSettings) + .putAlias(new AliasMetadata.Builder(TriggeredWatchStoreField.INDEX_NAME).build()) + ); final Index index = metadataBuilder.get("triggered-watches-alias").getIndex(); IndexRoutingTable.Builder indexRoutingTableBuilder = IndexRoutingTable.builder(index); - indexRoutingTableBuilder.addIndexShard(new IndexShardRoutingTable.Builder(new ShardId(index, 0)) - .addShard(TestShardRouting.newShardRouting("triggered-watches-alias", 0, "_node_id", null, true, ShardRoutingState.STARTED)) - .build()); + indexRoutingTableBuilder.addIndexShard( + new IndexShardRoutingTable.Builder(new ShardId(index, 0)).addShard( + TestShardRouting.newShardRouting("triggered-watches-alias", 0, "_node_id", null, true, ShardRoutingState.STARTED) + ).build() + ); indexRoutingTableBuilder.addReplica(); final Index otherIndex = metadataBuilder.get("whatever").getIndex(); IndexRoutingTable.Builder otherIndexRoutingTableBuilder = IndexRoutingTable.builder(otherIndex); - otherIndexRoutingTableBuilder.addIndexShard(new IndexShardRoutingTable.Builder(new ShardId(index, 0)) - .addShard(TestShardRouting.newShardRouting("whatever", 0, "_node_id", null, true, ShardRoutingState.STARTED)) - .build()); + otherIndexRoutingTableBuilder.addIndexShard( + new IndexShardRoutingTable.Builder(new ShardId(index, 0)).addShard( + TestShardRouting.newShardRouting("whatever", 0, "_node_id", null, true, ShardRoutingState.STARTED) + ).build() + ); csBuilder.metadata(metadataBuilder); csBuilder.routingTable(routingTableBuilder.build()); @@ -354,9 +387,9 @@ public void testTriggeredWatchesIndexIsClosed() { ClusterState.Builder csBuilder = new ClusterState.Builder(new ClusterName("_name")); Metadata.Builder metadataBuilder = Metadata.builder(); - metadataBuilder.put(IndexMetadata.builder(TriggeredWatchStoreField.INDEX_NAME) - .settings(indexSettings) - .state(IndexMetadata.State.CLOSE)); + metadataBuilder.put( + IndexMetadata.builder(TriggeredWatchStoreField.INDEX_NAME).settings(indexSettings).state(IndexMetadata.State.CLOSE) + ); csBuilder.metadata(metadataBuilder); assertThat(TriggeredWatchStore.validate(csBuilder.build()), is(false)); @@ -397,8 +430,11 @@ public void testTriggeredWatchParser() throws Exception { WatcherSearchTemplateService searchTemplateService = mock(WatcherSearchTemplateService.class); Watch watch = WatcherTestUtils.createTestWatch("fired_test", client, httpClient, emailService, searchTemplateService, logger); - ScheduleTriggerEvent event = new ScheduleTriggerEvent(watch.id(), ZonedDateTime.now(ZoneOffset.UTC), - ZonedDateTime.now(ZoneOffset.UTC)); + ScheduleTriggerEvent event = new ScheduleTriggerEvent( + watch.id(), + ZonedDateTime.now(ZoneOffset.UTC), + ZonedDateTime.now(ZoneOffset.UTC) + ); Wid wid = new Wid("_record", ZonedDateTime.now(ZoneOffset.UTC)); TriggeredWatch triggeredWatch = new TriggeredWatch(wid, event); XContentBuilder jsonBuilder = XContentFactory.jsonBuilder(); @@ -444,7 +480,6 @@ public void testPutTriggeredWatches() throws Exception { return null; }).when(client).execute(eq(BulkAction.INSTANCE), any(), any()); - BulkResponse response = triggeredWatchStore.putAll(triggeredWatches); assertThat(response.hasFailures(), is(false)); assertThat(response.getItems().length, is(numberOfTriggeredWatches)); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/history/HistoryStoreTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/history/HistoryStoreTests.java index 3021f9f069b43..28a1eb32f7e9c 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/history/HistoryStoreTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/history/HistoryStoreTests.java @@ -72,8 +72,10 @@ public void init() { when(client.settings()).thenReturn(settings); when(threadPool.getThreadContext()).thenReturn(new ThreadContext(settings)); BulkProcessor.Listener listener = mock(BulkProcessor.Listener.class); - BulkProcessor bulkProcessor - = BulkProcessor.builder(client::bulk, listener, "HistoryStoreTests").setConcurrentRequests(0).setBulkActions(1).build(); + BulkProcessor bulkProcessor = BulkProcessor.builder(client::bulk, listener, "HistoryStoreTests") + .setConcurrentRequests(0) + .setBulkActions(1) + .build(); historyStore = new HistoryStore(bulkProcessor); } @@ -91,8 +93,9 @@ public void testPut() throws Exception { ActionListener listener = (ActionListener) invocation.getArguments()[2]; IndexRequest indexRequest = (IndexRequest) request.requests().get(0); - if (indexRequest.id().equals(wid.value()) && - indexRequest.opType() == OpType.CREATE && indexRequest.index().equals(HistoryStoreField.DATA_STREAM)) { + if (indexRequest.id().equals(wid.value()) + && indexRequest.opType() == OpType.CREATE + && indexRequest.index().equals(HistoryStoreField.DATA_STREAM)) { listener.onResponse( new BulkResponse(new BulkItemResponse[] { BulkItemResponse.success(1, OpType.CREATE, indexResponse) }, 1) ); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/input/InputRegistryTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/input/InputRegistryTests.java index 1d9930f149906..a4f7162c8468a 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/input/InputRegistryTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/input/InputRegistryTests.java @@ -7,8 +7,8 @@ package org.elasticsearch.xpack.watcher.input; import org.elasticsearch.ElasticsearchParseException; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentParser; import static java.util.Collections.emptyMap; import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/input/chain/ChainInputTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/input/chain/ChainInputTests.java index d7a6db8260343..cbaaa65f063bf 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/input/chain/ChainInputTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/input/chain/ChainInputTests.java @@ -11,18 +11,18 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.core.Tuple; +import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.SecuritySettingsSourceField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.test.SecuritySettingsSourceField; import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext; import org.elasticsearch.xpack.core.watcher.input.Input; import org.elasticsearch.xpack.core.watcher.watch.Payload; -import org.elasticsearch.xpack.watcher.common.http.HttpRequestTemplate; import org.elasticsearch.xpack.watcher.common.http.BasicAuth; +import org.elasticsearch.xpack.watcher.common.http.HttpRequestTemplate; import org.elasticsearch.xpack.watcher.condition.ScriptCondition; import org.elasticsearch.xpack.watcher.input.InputFactory; import org.elasticsearch.xpack.watcher.input.InputRegistry; @@ -70,10 +70,24 @@ public void testThatExecutionWorks() throws Exception { ChainInputFactory chainInputFactory = new ChainInputFactory(inputRegistry); factories.put("chain", chainInputFactory); - XContentBuilder builder = jsonBuilder().startObject().startArray("inputs") - .startObject().startObject("first").startObject("simple").field("foo", "bar").endObject().endObject().endObject() - .startObject().startObject("second").startObject("simple").field("spam", "eggs").endObject().endObject().endObject() - .endArray().endObject(); + XContentBuilder builder = jsonBuilder().startObject() + .startArray("inputs") + .startObject() + .startObject("first") + .startObject("simple") + .field("foo", "bar") + .endObject() + .endObject() + .endObject() + .startObject() + .startObject("second") + .startObject("simple") + .field("spam", "eggs") + .endObject() + .endObject() + .endObject() + .endArray() + .endObject(); // first pass JSON and check for correct inputs XContentParser parser = createParser(builder); @@ -106,16 +120,15 @@ public void testThatExecutionWorks() throws Exception { } public void testToXContent() throws Exception { - ChainInput chainedInput = chainInput() - .add("first", simpleInput("foo", "bar")) - .add("second", simpleInput("spam", "eggs")) - .build(); + ChainInput chainedInput = chainInput().add("first", simpleInput("foo", "bar")).add("second", simpleInput("spam", "eggs")).build(); XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); chainedInput.toXContent(builder, ToXContent.EMPTY_PARAMS); - assertThat(BytesReference.bytes(builder).utf8ToString(), - is("{\"inputs\":[{\"first\":{\"simple\":{\"foo\":\"bar\"}}},{\"second\":{\"simple\":{\"spam\":\"eggs\"}}}]}")); + assertThat( + BytesReference.bytes(builder).utf8ToString(), + is("{\"inputs\":[{\"first\":{\"simple\":{\"foo\":\"bar\"}}},{\"second\":{\"simple\":{\"spam\":\"eggs\"}}}]}") + ); // parsing it back as well! Map> factories = new HashMap<>(); @@ -138,21 +151,20 @@ public void testToXContent() throws Exception { public void testThatWatchSourceBuilderWorksWithChainInput() throws Exception { XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); - HttpInput.Builder httpInputBuilder = httpInput(HttpRequestTemplate.builder("theHost", 1234) + HttpInput.Builder httpInputBuilder = httpInput( + HttpRequestTemplate.builder("theHost", 1234) .path("/index/_search") .body(Strings.toString(jsonBuilder().startObject().field("size", 1).endObject())) - .auth(new BasicAuth("test", SecuritySettingsSourceField.TEST_PASSWORD.toCharArray()))); + .auth(new BasicAuth("test", SecuritySettingsSourceField.TEST_PASSWORD.toCharArray())) + ); - ChainInput.Builder chainedInputBuilder = chainInput() - .add("foo", httpInputBuilder) - .add("bar", simpleInput("spam", "eggs")); + ChainInput.Builder chainedInputBuilder = chainInput().add("foo", httpInputBuilder).add("bar", simpleInput("spam", "eggs")); - watchBuilder() - .trigger(schedule(interval("5s"))) - .input(chainedInputBuilder) - .condition(new ScriptCondition(mockScript("ctx.payload.hits.total.value == 1"))) - .addAction("_id", loggingAction("watch [{{ctx.watch_id}}] matched")) - .toXContent(builder, ToXContent.EMPTY_PARAMS); + watchBuilder().trigger(schedule(interval("5s"))) + .input(chainedInputBuilder) + .condition(new ScriptCondition(mockScript("ctx.payload.hits.total.value == 1"))) + .addAction("_id", loggingAction("watch [{{ctx.watch_id}}] matched")) + .toXContent(builder, ToXContent.EMPTY_PARAMS); // no exception means all good } @@ -185,17 +197,30 @@ public void testParsingShouldBeStrictWhenClosingInputs() throws Exception { ChainInputFactory chainInputFactory = new ChainInputFactory(inputRegistry); factories.put("chain", chainInputFactory); - XContentBuilder builder = jsonBuilder().startObject().startArray("inputs").startObject() - .startObject("first").startObject("simple").field("foo", "bar").endObject().endObject() - .startObject("second").startObject("simple").field("spam", "eggs").endObject().endObject() - .endObject().endArray().endObject(); + XContentBuilder builder = jsonBuilder().startObject() + .startArray("inputs") + .startObject() + .startObject("first") + .startObject("simple") + .field("foo", "bar") + .endObject() + .endObject() + .startObject("second") + .startObject("simple") + .field("spam", "eggs") + .endObject() + .endObject() + .endObject() + .endArray() + .endObject(); XContentParser parser = createParser(builder); parser.nextToken(); - ElasticsearchParseException e = - expectThrows(ElasticsearchParseException.class, () -> chainInputFactory.parseInput("test", parser)); - assertThat(e.getMessage(), - containsString("Expected closing JSON object after parsing input [simple] named [first] in watch [test]")); + ElasticsearchParseException e = expectThrows(ElasticsearchParseException.class, () -> chainInputFactory.parseInput("test", parser)); + assertThat( + e.getMessage(), + containsString("Expected closing JSON object after parsing input [simple] named [first] in watch [test]") + ); } /* https://github.com/elastic/x-plugins/issues/3736 @@ -214,16 +239,23 @@ public void testParsingShouldBeStrictWhenStartingInputs() throws Exception { ChainInputFactory chainInputFactory = new ChainInputFactory(inputRegistry); factories.put("chain", chainInputFactory); - XContentBuilder builder = jsonBuilder().startObject().startArray("inputs") - .startObject().startArray("first").startObject() - .startObject("simple").field("foo", "bar").endObject() - .endObject().endArray().endObject() - .endArray().endObject(); + XContentBuilder builder = jsonBuilder().startObject() + .startArray("inputs") + .startObject() + .startArray("first") + .startObject() + .startObject("simple") + .field("foo", "bar") + .endObject() + .endObject() + .endArray() + .endObject() + .endArray() + .endObject(); XContentParser parser = createParser(builder); parser.nextToken(); - ElasticsearchParseException e = - expectThrows(ElasticsearchParseException.class, () -> chainInputFactory.parseInput("test", parser)); + ElasticsearchParseException e = expectThrows(ElasticsearchParseException.class, () -> chainInputFactory.parseInput("test", parser)); assertThat(e.getMessage(), containsString("Expected starting JSON object after [first] in watch [test]")); } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/input/chain/ExecutableChainInputTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/input/chain/ExecutableChainInputTests.java index 5907a93cee918..757d40e2e85f4 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/input/chain/ExecutableChainInputTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/input/chain/ExecutableChainInputTests.java @@ -8,8 +8,8 @@ import org.elasticsearch.core.Nullable; import org.elasticsearch.core.Tuple; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext; import org.elasticsearch.xpack.core.watcher.execution.Wid; import org.elasticsearch.xpack.core.watcher.input.ExecutableInput; @@ -65,11 +65,7 @@ protected XContentBuilder typeXContent(XContentBuilder builder, Params params) t private WatchExecutionContext createWatchExecutionContext() { ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC); Wid wid = new Wid(randomAlphaOfLength(5), now); - return mockExecutionContextBuilder(wid.watchId()) - .wid(wid) - .payload(new Payload.Simple()) - .time(wid.watchId(), now) - .buildMock(); + return mockExecutionContextBuilder(wid.watchId()).wid(wid).payload(new Payload.Simple()).time(wid.watchId(), now).buildMock(); } } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/input/http/HttpInputTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/input/http/HttpInputTests.java index c0bf9813d8c45..5911399dc2bfb 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/input/http/HttpInputTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/input/http/HttpInputTests.java @@ -11,6 +11,7 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.collect.MapBuilder; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ObjectPath; @@ -19,7 +20,6 @@ import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext; import org.elasticsearch.xpack.core.watcher.support.xcontent.WatcherParams; import org.elasticsearch.xpack.core.watcher.watch.Payload; @@ -79,9 +79,7 @@ public void init() throws Exception { public void testExecute() throws Exception { String host = "_host"; int port = 123; - HttpRequestTemplate.Builder request = HttpRequestTemplate.builder(host, port) - .method(HttpMethod.POST) - .body("_body"); + HttpRequestTemplate.Builder request = HttpRequestTemplate.builder(host, port).method(HttpMethod.POST).body("_body"); HttpInput httpInput; HttpResponse response; @@ -95,23 +93,35 @@ public void testExecute() throws Exception { httpInput = InputBuilders.httpInput(request.build()).expectedResponseXContentType(HttpContentType.YAML).build(); break; case 3: - response = new HttpResponse(123, "{\"key\" : \"value\"}".getBytes(StandardCharsets.UTF_8), - singletonMap(HttpHeaders.Names.CONTENT_TYPE, new String[] { XContentType.JSON.mediaType() })); + response = new HttpResponse( + 123, + "{\"key\" : \"value\"}".getBytes(StandardCharsets.UTF_8), + singletonMap(HttpHeaders.Names.CONTENT_TYPE, new String[] { XContentType.JSON.mediaType() }) + ); httpInput = InputBuilders.httpInput(request.build()).build(); break; case 4: - response = new HttpResponse(123, "key: value".getBytes(StandardCharsets.UTF_8), - singletonMap(HttpHeaders.Names.CONTENT_TYPE, new String[] { XContentType.YAML.mediaType() })); + response = new HttpResponse( + 123, + "key: value".getBytes(StandardCharsets.UTF_8), + singletonMap(HttpHeaders.Names.CONTENT_TYPE, new String[] { XContentType.YAML.mediaType() }) + ); httpInput = InputBuilders.httpInput(request.build()).build(); break; case 5: - response = new HttpResponse(123, "---\nkey: value".getBytes(StandardCharsets.UTF_8), - singletonMap(HttpHeaders.Names.CONTENT_TYPE, new String[] { "unrecognized_content_type" })); + response = new HttpResponse( + 123, + "---\nkey: value".getBytes(StandardCharsets.UTF_8), + singletonMap(HttpHeaders.Names.CONTENT_TYPE, new String[] { "unrecognized_content_type" }) + ); httpInput = InputBuilders.httpInput(request.build()).expectedResponseXContentType(HttpContentType.YAML).build(); break; default: - response = new HttpResponse(123, "{\"key\" : \"value\"}".getBytes(StandardCharsets.UTF_8), - singletonMap(HttpHeaders.Names.CONTENT_TYPE, new String[] { "unrecognized_content_type" })); + response = new HttpResponse( + 123, + "{\"key\" : \"value\"}".getBytes(StandardCharsets.UTF_8), + singletonMap(HttpHeaders.Names.CONTENT_TYPE, new String[] { "unrecognized_content_type" }) + ); httpInput = InputBuilders.httpInput(request.build()).build(); break; } @@ -129,9 +139,7 @@ public void testExecute() throws Exception { public void testExecuteNonJson() throws Exception { String host = "_host"; int port = 123; - HttpRequestTemplate.Builder request = HttpRequestTemplate.builder(host, port) - .method(HttpMethod.POST) - .body("_body"); + HttpRequestTemplate.Builder request = HttpRequestTemplate.builder(host, port).method(HttpMethod.POST).body("_body"); HttpInput httpInput = InputBuilders.httpInput(request.build()).expectedResponseXContentType(HttpContentType.TEXT).build(); ExecutableHttpInput input = new ExecutableHttpInput(httpInput, httpClient, templateEngine); String notJson = "This is not json"; @@ -153,17 +161,19 @@ public void testParser() throws Exception { String path = randomAlphaOfLength(3); TextTemplate pathTemplate = new TextTemplate(path); String body = randomBoolean() ? randomAlphaOfLength(3) : null; - Map params = - randomBoolean() ? new MapBuilder().put("a", new TextTemplate("b")).map() : null; - Map headers = - randomBoolean() ? new MapBuilder().put("c", new TextTemplate("d")).map() : null; + Map params = randomBoolean() + ? new MapBuilder().put("a", new TextTemplate("b")).map() + : null; + Map headers = randomBoolean() + ? new MapBuilder().put("c", new TextTemplate("d")).map() + : null; BasicAuth auth = randomBoolean() ? new BasicAuth("username", "password".toCharArray()) : null; HttpRequestTemplate.Builder requestBuilder = HttpRequestTemplate.builder(host, port) - .scheme(scheme) - .method(httpMethod) - .path(pathTemplate) - .body(body != null ? new TextTemplate(body) : null) - .auth(auth); + .scheme(scheme) + .method(httpMethod) + .path(pathTemplate) + .body(body != null ? new TextTemplate(body) : null) + .auth(auth); if (params != null) { requestBuilder.putParams(params); @@ -174,7 +184,7 @@ public void testParser() throws Exception { HttpInput.Builder inputBuilder = InputBuilders.httpInput(requestBuilder); HttpContentType expectedResponseXContentType = randomFrom(HttpContentType.values()); - String[] extractKeys = randomFrom(new String[]{"foo", "bar"}, new String[]{"baz"}, null); + String[] extractKeys = randomFrom(new String[] { "foo", "bar" }, new String[] { "baz" }, null); if (expectedResponseXContentType != HttpContentType.TEXT) { if (extractKeys != null) { inputBuilder.extractKeys(extractKeys); @@ -216,11 +226,11 @@ public void testParser() throws Exception { public void testParserInvalidHttpMethod() throws Exception { XContentBuilder builder = jsonBuilder().startObject() - .startObject("request") - .field("method", "_method") - .field("body", "_body") - .endObject() - .endObject(); + .startObject("request") + .field("method", "_method") + .field("body", "_body") + .endObject() + .endObject(); XContentParser parser = createParser(builder); parser.nextToken(); @@ -258,8 +268,8 @@ public void testThatHeadersAreIncludedInPayload() throws Exception { List expectedHeaderValues = new ArrayList<>(); expectedHeaderValues.add(headerValue); Map expectedHeaderMap = MapBuilder.newMapBuilder() - .put(headerName.toLowerCase(Locale.ROOT), expectedHeaderValues) - .map(); + .put(headerName.toLowerCase(Locale.ROOT), expectedHeaderValues) + .map(); assertThat(result.payload().data(), hasKey("_headers")); assertThat(result.payload().data().get("_headers"), equalTo(expectedHeaderMap)); } @@ -270,8 +280,14 @@ public void testThatExpectedContentTypeOverridesReturnedContentType() throws Exc ExecutableHttpInput input = new ExecutableHttpInput(httpInput, httpClient, templateEngine); Map headers = new HashMap<>(1); - String contentType = randomFrom("application/json", "application/json;charset=utf-8", "text/html", "application/yaml", - "application/smile", "application/cbor"); + String contentType = randomFrom( + "application/json", + "application/json;charset=utf-8", + "text/html", + "application/yaml", + "application/smile", + "application/cbor" + ); headers.put("Content-Type", new String[] { contentType }); String body = "{\"foo\":\"bar\"}"; HttpResponse httpResponse = new HttpResponse(200, body, headers); @@ -300,7 +316,7 @@ public void testThatStatusCodeIsSetInResultAndPayload() throws Exception { @SuppressWarnings("unchecked") public void testThatArrayJsonResponseIsHandled() throws Exception { - Map headers = Collections.singletonMap("Content-Type", new String[]{"application/json"}); + Map headers = Collections.singletonMap("Content-Type", new String[] { "application/json" }); HttpResponse response = new HttpResponse(200, "[ { \"foo\": \"first\" }, { \"foo\": \"second\"}]", headers); when(httpClient.execute(any(HttpRequest.class))).thenReturn(response); @@ -337,8 +353,10 @@ public void testExceptionCase() throws Exception { try (XContentBuilder builder = jsonBuilder()) { result.toXContent(builder, ToXContent.EMPTY_PARAMS); BytesReference bytes = BytesReference.bytes(builder); - try (XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, bytes.streamInput())) { + try ( + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, bytes.streamInput()) + ) { Map data = parser.map(); String reason = ObjectPath.eval("error.reason", data); assertThat(reason, is("could not connect")); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/input/simple/SimpleInputTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/input/simple/SimpleInputTests.java index b19df1f6e5cd1..245a0c3c38dee 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/input/simple/SimpleInputTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/input/simple/SimpleInputTests.java @@ -7,9 +7,9 @@ package org.elasticsearch.xpack.watcher.input.simple; import org.elasticsearch.ElasticsearchParseException; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.watcher.input.ExecutableInput; import org.elasticsearch.xpack.core.watcher.input.Input; import org.elasticsearch.xpack.core.watcher.watch.Payload; @@ -28,7 +28,7 @@ public class SimpleInputTests extends ESTestCase { public void testExecute() throws Exception { Map data = new HashMap<>(); data.put("foo", "bar"); - data.put("baz", new ArrayList() ); + data.put("baz", new ArrayList()); ExecutableInput staticInput = new ExecutableSimpleInput(new SimpleInput(new Payload.Simple(data))); Input.Result staticResult = staticInput.execute(null, new Payload.Simple()); @@ -49,7 +49,6 @@ public void testParserValid() throws Exception { ExecutableInput input = parser.parseExecutable("_id", xContentParser); assertEquals(input.type(), SimpleInput.TYPE); - Input.Result staticResult = input.execute(null, new Payload.Simple()); assertEquals(staticResult.payload().data().get("foo"), "bar"); List baz = (List) staticResult.payload().data().get("baz"); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/input/transform/TransformInputTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/input/transform/TransformInputTests.java index a23219c7685d4..248f5d0ef4783 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/input/transform/TransformInputTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/input/transform/TransformInputTests.java @@ -8,14 +8,14 @@ import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.Strings; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.script.MockScriptEngine; import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptService; import org.elasticsearch.script.ScriptType; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext; import org.elasticsearch.xpack.core.watcher.input.ExecutableInput; import org.elasticsearch.xpack.core.watcher.input.Input; @@ -62,17 +62,20 @@ public void testExecute() { } public void testParserValid() throws Exception { - Map>> transformFactories = - Collections.singletonMap("script", new ScriptTransformFactory(scriptService)); + Collections.singletonMap("script", new ScriptTransformFactory(scriptService)); TransformRegistry registry = new TransformRegistry(transformFactories); TransformInputFactory factory = new TransformInputFactory(registry); // { "script" : { "lang" : "mockscript", "source" : "1" } } - XContentBuilder builder = jsonBuilder().startObject().startObject("script") - .field("lang", MockScriptEngine.NAME) - .field("source", "1") - .endObject().endObject(); + XContentBuilder builder = jsonBuilder().startObject() + .startObject("script") + .field("lang", MockScriptEngine.NAME) + .field("source", "1") + .endObject() + .endObject(); XContentParser parser = createParser(builder); parser.nextToken(); @@ -87,9 +90,10 @@ public void testParserValid() throws Exception { public void testParserInvalid() throws Exception { XContentBuilder jsonBuilder = jsonBuilder().value("just a string"); - Map>> transformFactories = - Collections.singletonMap("script", new ScriptTransformFactory(scriptService)); + Collections.singletonMap("script", new ScriptTransformFactory(scriptService)); TransformRegistry registry = new TransformRegistry(transformFactories); TransformInputFactory factory = new TransformInputFactory(registry); XContentParser parser = createParser(jsonBuilder); @@ -107,16 +111,19 @@ public void testTransformResultToXContent() throws Exception { } public void testTransformInputToXContentIsSameAsParsing() throws Exception { - Map>> transformFactories = - Collections.singletonMap("script", new ScriptTransformFactory(scriptService)); + Collections.singletonMap("script", new ScriptTransformFactory(scriptService)); TransformRegistry registry = new TransformRegistry(transformFactories); TransformInputFactory factory = new TransformInputFactory(registry); - XContentBuilder jsonBuilder = jsonBuilder().startObject().startObject("script") - .field("source", "1") - .field("lang", "mockscript") - .endObject().endObject(); + XContentBuilder jsonBuilder = jsonBuilder().startObject() + .startObject("script") + .field("source", "1") + .field("lang", "mockscript") + .endObject() + .endObject(); XContentParser parser = createParser(jsonBuilder); parser.nextToken(); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/NotificationServiceTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/NotificationServiceTests.java index c1e43db235e17..b8bfdb7e17385 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/NotificationServiceTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/NotificationServiceTests.java @@ -48,10 +48,10 @@ public void testSingleAccount() { public void testMultipleAccountsWithExistingDefault() { String accountName = randomAlphaOfLength(10); Settings settings = Settings.builder() - .put("xpack.notification.test.account." + accountName, "bar") - .put("xpack.notification.test.account.second", "bar") - .put("xpack.notification.test.default_account", accountName) - .build(); + .put("xpack.notification.test.account." + accountName, "bar") + .put("xpack.notification.test.account.second", "bar") + .put("xpack.notification.test.default_account", accountName) + .build(); TestNotificationService service = new TestNotificationService(settings); assertThat(service.getAccount(accountName), is(accountName)); @@ -62,10 +62,10 @@ public void testMultipleAccountsWithExistingDefault() { public void testMultipleAccountsWithNoDefault() { String accountName = randomAlphaOfLength(10); Settings settings = Settings.builder() - .put("xpack.notification.test.account." + accountName, "bar") - .put("xpack.notification.test.account.second", "bar") - .put("xpack.notification.test.account.third", "bar") - .build(); + .put("xpack.notification.test.account." + accountName, "bar") + .put("xpack.notification.test.account.second", "bar") + .put("xpack.notification.test.account.third", "bar") + .build(); TestNotificationService service = new TestNotificationService(settings); assertThat(service.getAccount(null), anyOf(is(accountName), is("second"), is("third"))); @@ -74,10 +74,10 @@ public void testMultipleAccountsWithNoDefault() { public void testMultipleAccountsUnknownDefault() { String accountName = randomAlphaOfLength(10); Settings settings = Settings.builder() - .put("xpack.notification.test.account." + accountName, "bar") - .put("xpack.notification.test.account.second", "bar") - .put("xpack.notification.test.default_account", "non-existing") - .build(); + .put("xpack.notification.test.account." + accountName, "bar") + .put("xpack.notification.test.account.second", "bar") + .put("xpack.notification.test.default_account", "non-existing") + .build(); SettingsException e = expectThrows(SettingsException.class, () -> new TestNotificationService(settings)); assertThat(e.getMessage(), is("could not find default account [non-existing]")); @@ -91,11 +91,13 @@ public void testNoSpecifiedDefaultAccount() { assertThat(service.getAccount(null), is(accountName)); } - public void testAccountDoesNotExist() throws Exception{ + public void testAccountDoesNotExist() throws Exception { TestNotificationService service = new TestNotificationService(Settings.EMPTY); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> service.getAccount(null)); - assertThat(e.getMessage(), - is("no accounts of type [test] configured. Please set up an account using the [xpack.notification.test] settings")); + assertThat( + e.getMessage(), + is("no accounts of type [test] configured. Please set up an account using the [xpack.notification.test] settings") + ); } public void testAccountWithSecureSettings() throws Exception { @@ -105,10 +107,10 @@ public void testAccountWithSecureSettings() throws Exception { secureSettingsMap.put(secureSetting1.getKey(), "secure_only".toCharArray()); secureSettingsMap.put(secureSetting2.getKey(), "mixed_secure".toCharArray()); Settings settings = Settings.builder() - .put("xpack.notification.test.account.unsecure_only", "bar") - .put("xpack.notification.test.account.mixed.unsecure", "mixed_unsecure") - .setSecureSettings(secureSettingsFromMap(secureSettingsMap)) - .build(); + .put("xpack.notification.test.account.unsecure_only", "bar") + .put("xpack.notification.test.account.mixed.unsecure", "mixed_unsecure") + .setSecureSettings(secureSettingsFromMap(secureSettingsMap)) + .build(); TestNotificationService service = new TestNotificationService(settings, Arrays.asList(secureSetting1, secureSetting2)); assertThat(service.getAccount("secure_only"), is("secure_only")); assertThat(service.getAccount("unsecure_only"), is("unsecure_only")); @@ -121,9 +123,10 @@ public void testAccountCreationCached() { Settings settings = Settings.builder().put("xpack.notification.test.account." + accountName, "bar").build(); final AtomicInteger validationInvocationCount = new AtomicInteger(0); - TestNotificationService service = new TestNotificationService(settings, (String name, Settings accountSettings) -> { - validationInvocationCount.incrementAndGet(); - }); + TestNotificationService service = new TestNotificationService( + settings, + (String name, Settings accountSettings) -> { validationInvocationCount.incrementAndGet(); } + ); assertThat(validationInvocationCount.get(), is(0)); assertThat(service.getAccount(accountName), is(accountName)); assertThat(validationInvocationCount.get(), is(1)); @@ -138,22 +141,28 @@ public void testAccountCreationCached() { public void testAccountUpdateSettings() throws Exception { final Setting secureSetting = SecureSetting.secureString("xpack.notification.test.account.x.secure", null); - final Setting setting = Setting.simpleString("xpack.notification.test.account.x.dynamic", Setting.Property.Dynamic, - Setting.Property.NodeScope); + final Setting setting = Setting.simpleString( + "xpack.notification.test.account.x.dynamic", + Setting.Property.Dynamic, + Setting.Property.NodeScope + ); final AtomicReference secureSettingValue = new AtomicReference(randomAlphaOfLength(4)); final AtomicReference settingValue = new AtomicReference(randomAlphaOfLength(4)); final Map secureSettingsMap = new HashMap<>(); final AtomicInteger validationInvocationCount = new AtomicInteger(0); secureSettingsMap.put(secureSetting.getKey(), secureSettingValue.get().toCharArray()); final Settings.Builder settingsBuilder = Settings.builder() - .put(setting.getKey(), settingValue.get()) - .setSecureSettings(secureSettingsFromMap(secureSettingsMap)); - final TestNotificationService service = new TestNotificationService(settingsBuilder.build(), Arrays.asList(secureSetting), - (String name, Settings accountSettings) -> { - assertThat(accountSettings.get("dynamic"), is(settingValue.get())); - assertThat(SecureSetting.secureString("secure", null).get(accountSettings), is(secureSettingValue.get())); - validationInvocationCount.incrementAndGet(); - }); + .put(setting.getKey(), settingValue.get()) + .setSecureSettings(secureSettingsFromMap(secureSettingsMap)); + final TestNotificationService service = new TestNotificationService( + settingsBuilder.build(), + Arrays.asList(secureSetting), + (String name, Settings accountSettings) -> { + assertThat(accountSettings.get("dynamic"), is(settingValue.get())); + assertThat(SecureSetting.secureString("secure", null).get(accountSettings), is(secureSettingValue.get())); + validationInvocationCount.incrementAndGet(); + } + ); assertThat(validationInvocationCount.get(), is(0)); service.getAccount(null); assertThat(validationInvocationCount.get(), is(1)); @@ -183,15 +192,24 @@ public void testAccountUpdateSettings() throws Exception { assertThat(validationInvocationCount.get(), is(4)); } - private static void updateDynamicClusterSetting(AtomicReference settingValue, Setting setting, - Settings.Builder settingsBuilder, TestNotificationService service) { + private static void updateDynamicClusterSetting( + AtomicReference settingValue, + Setting setting, + Settings.Builder settingsBuilder, + TestNotificationService service + ) { settingValue.set(randomAlphaOfLength(4)); settingsBuilder.put(setting.getKey(), settingValue.get()); service.clusterSettingsConsumer(settingsBuilder.build()); } - private static void updateSecureSetting(AtomicReference secureSettingValue, Setting secureSetting, - Map secureSettingsMap, Settings.Builder settingsBuilder, TestNotificationService service) { + private static void updateSecureSetting( + AtomicReference secureSettingValue, + Setting secureSetting, + Map secureSettingsMap, + Settings.Builder settingsBuilder, + TestNotificationService service + ) { secureSettingValue.set(randomAlphaOfLength(4)); secureSettingsMap.put(secureSetting.getKey(), secureSettingValue.get().toCharArray()); service.reload(settingsBuilder.build()); @@ -255,8 +273,7 @@ public byte[] getSHA256Digest(String setting) throws GeneralSecurityException { } @Override - public void close() throws IOException { - } + public void close() throws IOException {} }; } } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/AccountTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/AccountTests.java index 786712acdbc64..c72dec97e7764 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/AccountTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/AccountTests.java @@ -15,13 +15,14 @@ import org.junit.After; import org.junit.Before; -import javax.mail.Address; -import javax.mail.Message; -import javax.mail.internet.InternetAddress; import java.util.Properties; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; +import javax.mail.Address; +import javax.mail.Message; +import javax.mail.internet.InternetAddress; + import static org.hamcrest.Matchers.arrayWithSize; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasItemInArray; @@ -129,10 +130,12 @@ public void testConfig() throws Exception { } // default properties - for (String name : new String[]{ "connection_timeout", "write_timeout", "timeout"}) { + for (String name : new String[] { "connection_timeout", "write_timeout", "timeout" }) { String propertyName = name.replaceAll("_", ""); - smtpProps.put("mail.smtp." + propertyName, - String.valueOf(TimeValue.parseTimeValue(Account.DEFAULT_SMTP_TIMEOUT_SETTINGS.get(name), name).millis())); + smtpProps.put( + "mail.smtp." + propertyName, + String.valueOf(TimeValue.parseTimeValue(Account.DEFAULT_SMTP_TIMEOUT_SETTINGS.get(name), name).millis()) + ); } Settings smtpSettings = smtpBuilder.build(); @@ -161,28 +164,36 @@ public void testConfig() throws Exception { public void testSend() throws Exception { final MockSecureSettings secureSettings = new MockSecureSettings(); secureSettings.setString("smtp." + Account.SECURE_PASSWORD_SETTING.getKey(), EmailServer.PASSWORD); - Account account = new Account(new Account.Config("default", Settings.builder() - .put("smtp.host", "localhost") - .put("smtp.port", server.port()) - .put("smtp.user", EmailServer.USERNAME) - .setSecureSettings(secureSettings) - .build(), null, logger), null, logger); + Account account = new Account( + new Account.Config( + "default", + Settings.builder() + .put("smtp.host", "localhost") + .put("smtp.port", server.port()) + .put("smtp.user", EmailServer.USERNAME) + .setSecureSettings(secureSettings) + .build(), + null, + logger + ), + null, + logger + ); Email email = Email.builder() - .id("_id") - .from(new Email.Address("from@domain.com")) - .to(Email.AddressList.parse("To")) - .subject("_subject") - .textBody("_text_body") - .build(); + .id("_id") + .from(new Email.Address("from@domain.com")) + .to(Email.AddressList.parse("To")) + .subject("_subject") + .textBody("_text_body") + .build(); final CountDownLatch latch = new CountDownLatch(1); server.addListener(message -> { assertThat(message.getFrom().length, is(1)); assertThat(message.getFrom()[0], equalTo(new InternetAddress("from@domain.com"))); assertThat(message.getRecipients(Message.RecipientType.TO).length, is(1)); - assertThat(message.getRecipients(Message.RecipientType.TO)[0], - equalTo(new InternetAddress("to@domain.com", "To"))); + assertThat(message.getRecipients(Message.RecipientType.TO)[0], equalTo(new InternetAddress("to@domain.com", "To"))); assertThat(message.getSubject(), equalTo("_subject")); assertThat(Profile.STANDARD.textBody(message), equalTo("_text_body")); latch.countDown(); @@ -198,21 +209,30 @@ public void testSend() throws Exception { public void testSendCCAndBCC() throws Exception { final MockSecureSettings secureSettings = new MockSecureSettings(); secureSettings.setString("smtp." + Account.SECURE_PASSWORD_SETTING.getKey(), EmailServer.PASSWORD); - Account account = new Account(new Account.Config("default", Settings.builder() - .put("smtp.host", "localhost") - .put("smtp.port", server.port()) - .put("smtp.user", EmailServer.USERNAME) - .setSecureSettings(secureSettings) - .build(), null, logger), null, logger); + Account account = new Account( + new Account.Config( + "default", + Settings.builder() + .put("smtp.host", "localhost") + .put("smtp.port", server.port()) + .put("smtp.user", EmailServer.USERNAME) + .setSecureSettings(secureSettings) + .build(), + null, + logger + ), + null, + logger + ); Email email = Email.builder() - .id("_id") - .from(new Email.Address("from@domain.com")) - .to(Email.AddressList.parse("TO")) - .cc(Email.AddressList.parse("CC1,cc2@domain.com")) - .bcc(Email.AddressList.parse("BCC1,bcc2@domain.com")) - .replyTo(Email.AddressList.parse("noreply@domain.com")) - .build(); + .id("_id") + .from(new Email.Address("from@domain.com")) + .to(Email.AddressList.parse("TO")) + .cc(Email.AddressList.parse("CC1,cc2@domain.com")) + .bcc(Email.AddressList.parse("BCC1,bcc2@domain.com")) + .replyTo(Email.AddressList.parse("noreply@domain.com")) + .build(); final CountDownLatch latch = new CountDownLatch(5); server.addListener(message -> { @@ -221,8 +241,10 @@ public void testSendCCAndBCC() throws Exception { assertThat(message.getRecipients(Message.RecipientType.TO).length, is(1)); assertThat(message.getRecipients(Message.RecipientType.TO)[0], equalTo(new InternetAddress("to@domain.com", "TO"))); assertThat(message.getRecipients(Message.RecipientType.CC).length, is(2)); - assertThat(message.getRecipients(Message.RecipientType.CC), - hasItemInArray((Address) new InternetAddress("cc1@domain.com", "CC1"))); + assertThat( + message.getRecipients(Message.RecipientType.CC), + hasItemInArray((Address) new InternetAddress("cc1@domain.com", "CC1")) + ); assertThat(message.getRecipients(Message.RecipientType.CC), hasItemInArray((Address) new InternetAddress("cc2@domain.com"))); assertThat(message.getReplyTo(), arrayWithSize(1)); assertThat(message.getReplyTo(), hasItemInArray((Address) new InternetAddress("noreply@domain.com"))); @@ -238,18 +260,24 @@ public void testSendCCAndBCC() throws Exception { } public void testSendAuthentication() throws Exception { - Account account = new Account(new Account.Config("default", Settings.builder() - .put("smtp.host", "localhost") - .put("smtp.port", server.port()) - .build(), null, logger), null, logger); + Account account = new Account( + new Account.Config( + "default", + Settings.builder().put("smtp.host", "localhost").put("smtp.port", server.port()).build(), + null, + logger + ), + null, + logger + ); Email email = Email.builder() - .id("_id") - .from(new Email.Address("from@domain.com")) - .to(Email.AddressList.parse("To")) - .subject("_subject") - .textBody("_text_body") - .build(); + .id("_id") + .from(new Email.Address("from@domain.com")) + .to(Email.AddressList.parse("To")) + .subject("_subject") + .textBody("_text_body") + .build(); final CountDownLatch latch = new CountDownLatch(1); server.addListener(message -> latch.countDown()); @@ -262,10 +290,16 @@ public void testSendAuthentication() throws Exception { } public void testDefaultAccountTimeout() { - Account account = new Account(new Account.Config("default", Settings.builder() - .put("smtp.host", "localhost") - .put("smtp.port", server.port()) - .build(), null, logger), null, logger); + Account account = new Account( + new Account.Config( + "default", + Settings.builder().put("smtp.host", "localhost").put("smtp.port", server.port()).build(), + null, + logger + ), + null, + logger + ); Properties mailProperties = account.getConfig().smtp.properties; assertThat(mailProperties.get("mail.smtp.connectiontimeout"), is(String.valueOf(TimeValue.timeValueMinutes(2).millis()))); @@ -274,13 +308,22 @@ public void testDefaultAccountTimeout() { } public void testAccountTimeoutsCanBeConfigureAsTimeValue() { - Account account = new Account(new Account.Config("default", Settings.builder() - .put("smtp.host", "localhost") - .put("smtp.port", server.port()) - .put("smtp.connection_timeout", TimeValue.timeValueMinutes(4)) - .put("smtp.write_timeout", TimeValue.timeValueMinutes(6)) - .put("smtp.timeout", TimeValue.timeValueMinutes(8)) - .build(), null, logger), null, logger); + Account account = new Account( + new Account.Config( + "default", + Settings.builder() + .put("smtp.host", "localhost") + .put("smtp.port", server.port()) + .put("smtp.connection_timeout", TimeValue.timeValueMinutes(4)) + .put("smtp.write_timeout", TimeValue.timeValueMinutes(6)) + .put("smtp.timeout", TimeValue.timeValueMinutes(8)) + .build(), + null, + logger + ), + null, + logger + ); Properties mailProperties = account.getConfig().smtp.properties; @@ -291,11 +334,20 @@ public void testAccountTimeoutsCanBeConfigureAsTimeValue() { public void testAccountTimeoutsConfiguredAsNumberAreRejected() { expectThrows(IllegalArgumentException.class, () -> { - new Account(new Account.Config("default", Settings.builder() - .put("smtp.host", "localhost") - .put("smtp.port", server.port()) - .put("smtp.connection_timeout", 4000) - .build(), null, logger), null, logger); + new Account( + new Account.Config( + "default", + Settings.builder() + .put("smtp.host", "localhost") + .put("smtp.port", server.port()) + .put("smtp.connection_timeout", 4000) + .build(), + null, + logger + ), + null, + logger + ); }); } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/AccountsTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/AccountsTests.java index 21440af6fd2b7..9f77f42cc04e0 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/AccountsTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/AccountsTests.java @@ -22,11 +22,14 @@ public class AccountsTests extends ESTestCase { public void testSingleAccount() throws Exception { - Settings.Builder builder = Settings.builder() - .put("default_account", "account1"); + Settings.Builder builder = Settings.builder().put("default_account", "account1"); addAccountSettings("account1", builder); - EmailService service = new EmailService(builder.build(), null, mock(SSLService.class), - new ClusterSettings(Settings.EMPTY, new HashSet<>(EmailService.getSettings()))); + EmailService service = new EmailService( + builder.build(), + null, + mock(SSLService.class), + new ClusterSettings(Settings.EMPTY, new HashSet<>(EmailService.getSettings())) + ); Account account = service.getAccount("account1"); assertThat(account, notNullValue()); assertThat(account.name(), equalTo("account1")); @@ -38,8 +41,12 @@ public void testSingleAccount() throws Exception { public void testSingleAccountNoExplicitDefault() throws Exception { Settings.Builder builder = Settings.builder(); addAccountSettings("account1", builder); - EmailService service = new EmailService(builder.build(), null, mock(SSLService.class), - new ClusterSettings(Settings.EMPTY, new HashSet<>(EmailService.getSettings()))); + EmailService service = new EmailService( + builder.build(), + null, + mock(SSLService.class), + new ClusterSettings(Settings.EMPTY, new HashSet<>(EmailService.getSettings())) + ); Account account = service.getAccount("account1"); assertThat(account, notNullValue()); assertThat(account.name(), equalTo("account1")); @@ -49,13 +56,16 @@ public void testSingleAccountNoExplicitDefault() throws Exception { } public void testMultipleAccounts() throws Exception { - Settings.Builder builder = Settings.builder() - .put("xpack.notification.email.default_account", "account1"); + Settings.Builder builder = Settings.builder().put("xpack.notification.email.default_account", "account1"); addAccountSettings("account1", builder); addAccountSettings("account2", builder); - EmailService service = new EmailService(builder.build(), null, mock(SSLService.class), - new ClusterSettings(Settings.EMPTY, new HashSet<>(EmailService.getSettings()))); + EmailService service = new EmailService( + builder.build(), + null, + mock(SSLService.class), + new ClusterSettings(Settings.EMPTY, new HashSet<>(EmailService.getSettings())) + ); Account account = service.getAccount("account1"); assertThat(account, notNullValue()); assertThat(account.name(), equalTo("account1")); @@ -68,13 +78,16 @@ public void testMultipleAccounts() throws Exception { } public void testMultipleAccountsNoExplicitDefault() throws Exception { - Settings.Builder builder = Settings.builder() - .put("default_account", "account1"); + Settings.Builder builder = Settings.builder().put("default_account", "account1"); addAccountSettings("account1", builder); addAccountSettings("account2", builder); - EmailService service = new EmailService(builder.build(), null, mock(SSLService.class), - new ClusterSettings(Settings.EMPTY, new HashSet<>(EmailService.getSettings()))); + EmailService service = new EmailService( + builder.build(), + null, + mock(SSLService.class), + new ClusterSettings(Settings.EMPTY, new HashSet<>(EmailService.getSettings())) + ); Account account = service.getAccount("account1"); assertThat(account, notNullValue()); assertThat(account.name(), equalTo("account1")); @@ -91,23 +104,31 @@ public void testMultipleAccountsUnknownDefault() throws Exception { addAccountSettings("account1", builder); addAccountSettings("account2", builder); ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, new HashSet<>(EmailService.getSettings())); - SettingsException e = expectThrows(SettingsException.class, - () -> new EmailService(builder.build(), null, mock(SSLService.class), clusterSettings)); + SettingsException e = expectThrows( + SettingsException.class, + () -> new EmailService(builder.build(), null, mock(SSLService.class), clusterSettings) + ); assertThat(e.getMessage(), is("could not find default account [unknown]")); } public void testNoAccount() throws Exception { Settings.Builder builder = Settings.builder(); - EmailService service = new EmailService(builder.build(), null, mock(SSLService.class), - new ClusterSettings(Settings.EMPTY, new HashSet<>(EmailService.getSettings()))); + EmailService service = new EmailService( + builder.build(), + null, + mock(SSLService.class), + new ClusterSettings(Settings.EMPTY, new HashSet<>(EmailService.getSettings())) + ); expectThrows(IllegalArgumentException.class, () -> service.getAccount(null)); } public void testNoAccountWithDefaultAccount() throws Exception { Settings settings = Settings.builder().put("xpack.notification.email.default_account", "unknown").build(); ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, new HashSet<>(EmailService.getSettings())); - SettingsException e = expectThrows(SettingsException.class, - () -> new EmailService(settings, null, mock(SSLService.class), clusterSettings)); + SettingsException e = expectThrows( + SettingsException.class, + () -> new EmailService(settings, null, mock(SSLService.class), clusterSettings) + ); assertThat(e.getMessage(), is("could not find default account [unknown]")); } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/EmailServiceTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/EmailServiceTests.java index 9b36c6efc6f54..9ad978ba7e5a2 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/EmailServiceTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/EmailServiceTests.java @@ -33,8 +33,12 @@ public class EmailServiceTests extends ESTestCase { @Before public void init() throws Exception { account = mock(Account.class); - service = new EmailService(Settings.builder().put("xpack.notification.email.account.account1.foo", "bar").build(), null, - mock(SSLService.class), new ClusterSettings(Settings.EMPTY, new HashSet<>(EmailService.getSettings()))) { + service = new EmailService( + Settings.builder().put("xpack.notification.email.account.account1.foo", "bar").build(), + null, + mock(SSLService.class), + new ClusterSettings(Settings.EMPTY, new HashSet<>(EmailService.getSettings())) + ) { @Override protected Account createAccount(String name, Settings accountSettings) { return account; @@ -57,23 +61,27 @@ public void testSend() throws Exception { public void testAccountSmtpPropertyConfiguration() { Settings settings = Settings.builder() - .put("xpack.notification.email.account.account1.smtp.host", "localhost") - .put("xpack.notification.email.account.account1.smtp.starttls.required", "true") - .put("xpack.notification.email.account.account2.smtp.host", "localhost") - .put("xpack.notification.email.account.account2.smtp.connection_timeout", "1m") - .put("xpack.notification.email.account.account2.smtp.timeout", "1m") - .put("xpack.notification.email.account.account2.smtp.write_timeout", "1m") - .put("xpack.notification.email.account.account3.smtp.host", "localhost") - .put("xpack.notification.email.account.account3.smtp.send_partial", true) - .put("xpack.notification.email.account.account4.smtp.host", "localhost") - .put("xpack.notification.email.account.account4.smtp.local_address", "localhost") - .put("xpack.notification.email.account.account4.smtp.local_port", "1025") - .put("xpack.notification.email.account.account5.smtp.host", "localhost") - .put("xpack.notification.email.account.account5.smtp.wait_on_quit", true) - .put("xpack.notification.email.account.account5.smtp.ssl.trust", "host1,host2,host3") - .build(); - EmailService emailService = new EmailService(settings, null, mock(SSLService.class), - new ClusterSettings(Settings.EMPTY, new HashSet<>(EmailService.getSettings()))); + .put("xpack.notification.email.account.account1.smtp.host", "localhost") + .put("xpack.notification.email.account.account1.smtp.starttls.required", "true") + .put("xpack.notification.email.account.account2.smtp.host", "localhost") + .put("xpack.notification.email.account.account2.smtp.connection_timeout", "1m") + .put("xpack.notification.email.account.account2.smtp.timeout", "1m") + .put("xpack.notification.email.account.account2.smtp.write_timeout", "1m") + .put("xpack.notification.email.account.account3.smtp.host", "localhost") + .put("xpack.notification.email.account.account3.smtp.send_partial", true) + .put("xpack.notification.email.account.account4.smtp.host", "localhost") + .put("xpack.notification.email.account.account4.smtp.local_address", "localhost") + .put("xpack.notification.email.account.account4.smtp.local_port", "1025") + .put("xpack.notification.email.account.account5.smtp.host", "localhost") + .put("xpack.notification.email.account.account5.smtp.wait_on_quit", true) + .put("xpack.notification.email.account.account5.smtp.ssl.trust", "host1,host2,host3") + .build(); + EmailService emailService = new EmailService( + settings, + null, + mock(SSLService.class), + new ClusterSettings(Settings.EMPTY, new HashSet<>(EmailService.getSettings())) + ); Account account1 = emailService.getAccount("account1"); Properties properties1 = account1.getConfig().smtp.properties; diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/EmailTemplateTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/EmailTemplateTests.java index 8e5b55eae76c9..780b15c233e22 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/EmailTemplateTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/EmailTemplateTests.java @@ -7,11 +7,11 @@ package org.elasticsearch.xpack.watcher.notification.email; import org.elasticsearch.ElasticsearchParseException; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.watcher.common.text.TextTemplate; import org.elasticsearch.xpack.watcher.test.MockTextTemplateEngine; import org.mockito.ArgumentCaptor; @@ -38,7 +38,7 @@ public class EmailTemplateTests extends ESTestCase { public void testEmailTemplateParserSelfGenerated() throws Exception { TextTemplate from = randomFrom(new TextTemplate("from@from.com"), null); List addresses = new ArrayList<>(); - for( int i = 0; i < randomIntBetween(1, 5); ++i){ + for (int i = 0; i < randomIntBetween(1, 5); ++i) { addresses.add(new TextTemplate("address" + i + "@test.com")); } TextTemplate[] possibleList = addresses.toArray(new TextTemplate[addresses.size()]); @@ -55,8 +55,17 @@ public void testEmailTemplateParserSelfGenerated() throws Exception { String htmlBody = "Templated Html Body "; String sanitizedHtmlBody = "Templated Html Body"; - EmailTemplate emailTemplate = new EmailTemplate(from, replyTo, priority, to, cc, bcc, subjectTemplate, textBodyTemplate, - htmlBodyTemplate); + EmailTemplate emailTemplate = new EmailTemplate( + from, + replyTo, + priority, + to, + cc, + bcc, + subjectTemplate, + textBodyTemplate, + htmlBodyTemplate + ); XContentBuilder builder = XContentFactory.jsonBuilder(); emailTemplate.toXContent(builder, ToXContent.EMPTY_PARAMS); @@ -71,7 +80,7 @@ public void testEmailTemplateParserSelfGenerated() throws Exception { while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); - } else { + } else { assertThat(emailTemplateParser.handle(currentFieldName, parser), is(true)); } } @@ -103,24 +112,21 @@ public void testEmailTemplateParserSelfGenerated() throws Exception { public void testParsingMultipleEmailAddresses() throws Exception { EmailTemplate template = EmailTemplate.builder() - .from("sender@example.org") - .to("to1@example.org, to2@example.org") - .cc("cc1@example.org, cc2@example.org") - .bcc("bcc1@example.org, bcc2@example.org") - .textBody("blah") - .build(); + .from("sender@example.org") + .to("to1@example.org, to2@example.org") + .cc("cc1@example.org, cc2@example.org") + .bcc("bcc1@example.org, bcc2@example.org") + .textBody("blah") + .build(); Email email = template.render(new MockTextTemplateEngine(), emptyMap(), null, emptyMap()).id("foo").build(); assertThat(email.to.size(), is(2)); - assertThat(email.to, containsInAnyOrder(new Email.Address("to1@example.org"), - new Email.Address("to2@example.org"))); + assertThat(email.to, containsInAnyOrder(new Email.Address("to1@example.org"), new Email.Address("to2@example.org"))); assertThat(email.cc.size(), is(2)); - assertThat(email.cc, containsInAnyOrder(new Email.Address("cc1@example.org"), - new Email.Address("cc2@example.org"))); + assertThat(email.cc, containsInAnyOrder(new Email.Address("cc1@example.org"), new Email.Address("cc2@example.org"))); assertThat(email.bcc.size(), is(2)); - assertThat(email.bcc, containsInAnyOrder(new Email.Address("bcc1@example.org"), - new Email.Address("bcc2@example.org"))); + assertThat(email.bcc, containsInAnyOrder(new Email.Address("bcc1@example.org"), new Email.Address("bcc2@example.org"))); } public void testEmailValidation() { @@ -156,8 +162,17 @@ public void testEmailWarning() throws Exception { String htmlBody = "Templated Html Body "; String sanitizedHtmlBody = "Templated Html Body"; - EmailTemplate emailTemplate = new EmailTemplate(from, replyTo, priority, to, cc, bcc, subjectTemplate, textBodyTemplate, - htmlBodyTemplate); + EmailTemplate emailTemplate = new EmailTemplate( + from, + replyTo, + priority, + to, + cc, + bcc, + subjectTemplate, + textBodyTemplate, + htmlBodyTemplate + ); XContentBuilder builder = XContentFactory.jsonBuilder(); emailTemplate.toXContent(builder, ToXContent.EMPTY_PARAMS); @@ -184,21 +199,31 @@ public void testEmailWarning() throws Exception { when(htmlSanitizer.sanitize(htmlBody)).thenReturn(sanitizedHtmlBody); ArgumentCaptor htmlSanitizeArguments = ArgumentCaptor.forClass(String.class); - //4 attachments, zero warning, one warning, two warnings, and one with html that should be stripped + // 4 attachments, zero warning, one warning, two warnings, and one with html that should be stripped Map attachments = Map.of( - "one", new Attachment.Bytes("one", "one", randomByteArrayOfLength(100), randomAlphaOfLength(5), false, Collections.emptySet()), - "two", new Attachment.Bytes("two", "two", randomByteArrayOfLength(100), randomAlphaOfLength(5), false, Set.of("warning0")), - "thr", new Attachment.Bytes("thr", "thr", randomByteArrayOfLength(100), randomAlphaOfLength(5), false, - Set.of("warning1", "warning2")), - "for", new Attachment.Bytes("for", "for", randomByteArrayOfLength(100), randomAlphaOfLength(5), false, - Set.of(""))); + "one", + new Attachment.Bytes("one", "one", randomByteArrayOfLength(100), randomAlphaOfLength(5), false, Collections.emptySet()), + "two", + new Attachment.Bytes("two", "two", randomByteArrayOfLength(100), randomAlphaOfLength(5), false, Set.of("warning0")), + "thr", + new Attachment.Bytes("thr", "thr", randomByteArrayOfLength(100), randomAlphaOfLength(5), false, Set.of("warning1", "warning2")), + "for", + new Attachment.Bytes( + "for", + "for", + randomByteArrayOfLength(100), + randomAlphaOfLength(5), + false, + Set.of("") + ) + ); Email.Builder emailBuilder = parsedEmailTemplate.render(new MockTextTemplateEngine(), model, htmlSanitizer, attachments); emailBuilder.id("_id"); Email email = emailBuilder.build(); assertThat(email.subject, equalTo(subjectTemplate.getTemplate())); - //text + // text int bodyStart = email.textBody.indexOf(textBodyTemplate.getTemplate()); String warnings = email.textBody.substring(0, bodyStart); String[] warningLines = warnings.split("\n"); @@ -207,7 +232,7 @@ public void testEmailWarning() throws Exception { assertThat(warnings, containsString("warning" + i)); } - //html - pull the arguments as it is run through the sanitizer + // html - pull the arguments as it is run through the sanitizer verify(htmlSanitizer).sanitize(htmlSanitizeArguments.capture()); String fullHtmlBody = htmlSanitizeArguments.getValue(); bodyStart = fullHtmlBody.indexOf(htmlBodyTemplate.getTemplate()); @@ -224,8 +249,10 @@ private void assertValidEmail(String email) { } private void assertInvalidEmail(String email) { - ElasticsearchParseException e = expectThrows(ElasticsearchParseException.class, - () -> EmailTemplate.Parser.validateEmailAddresses(new TextTemplate(email))); + ElasticsearchParseException e = expectThrows( + ElasticsearchParseException.class, + () -> EmailTemplate.Parser.validateEmailAddresses(new TextTemplate(email)) + ); assertThat(e.getMessage(), startsWith("invalid email address")); } } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/EmailTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/EmailTests.java index 144131bedcf25..39d35e543b4e4 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/EmailTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/EmailTests.java @@ -6,11 +6,11 @@ */ package org.elasticsearch.xpack.watcher.notification.email; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.ESTestCase; import java.time.Instant; import java.time.ZoneOffset; @@ -26,7 +26,7 @@ public void testEmailParserSelfGenerated() throws Exception { String id = "test-id"; Email.Address from = randomFrom(new Email.Address("from@from.com"), null); List addresses = new ArrayList<>(); - for( int i = 0; i < randomIntBetween(1, 5); ++i){ + for (int i = 0; i < randomIntBetween(1, 5); ++i) { addresses.add(new Email.Address("address" + i + "@test.com")); } Email.AddressList possibleList = new Email.AddressList(addresses); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/HtmlSanitizerTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/HtmlSanitizerTests.java index 09e9628d02c77..a3f6f28ad43d8 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/HtmlSanitizerTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/HtmlSanitizerTests.java @@ -14,13 +14,12 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasItem; - public class HtmlSanitizerTests extends ESTestCase { public void testDefaultOnClickDisallowed() { - String badHtml = ""; + String badHtml = ""; HtmlSanitizer sanitizer = new HtmlSanitizer(Settings.EMPTY); String sanitizedHtml = sanitizer.sanitize(badHtml); assertThat(sanitizedHtml, equalTo("Click me to display Date and Time.")); @@ -41,31 +40,31 @@ public void testDefault_EmbeddedImageAllowed() { } public void testDefaultTablesAllowed() { - String html = "

    " - + n.nodeName() - + "" + n.nodeName() + "
    " + - "" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + - "
    caption
    header1header2
    Sum$180
    cost180
    "; + String html = "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "
    caption
    header1header2
    Sum$180
    cost180
    "; HtmlSanitizer sanitizer = new HtmlSanitizer(Settings.EMPTY); String sanitizedHtml = sanitizer.sanitize(html); assertThat(sanitizedHtml, equalTo(html)); @@ -80,8 +79,8 @@ public void testAllowStyles() { } public void testDefaultFormattingAllowed() { - String html = "" + - "


    "; + String html = "" + + "

    "; HtmlSanitizer sanitizer = new HtmlSanitizer(Settings.EMPTY); String sanitizedHtml = sanitizer.sanitize(html); assertThat(sanitizedHtml, equalTo(html)); @@ -96,27 +95,27 @@ public void testDefaultSciptsDisallowed() { public void testCustomDisabled() { String html = "This is a bad image"; - HtmlSanitizer sanitizer = new HtmlSanitizer(Settings.builder() - .put("xpack.notification.email.html.sanitization.enabled", false) - .build()); + HtmlSanitizer sanitizer = new HtmlSanitizer( + Settings.builder().put("xpack.notification.email.html.sanitization.enabled", false).build() + ); String sanitizedHtml = sanitizer.sanitize(html); assertThat(sanitizedHtml, equalTo(html)); } public void testCustomAllImageAllowed() { String html = "This is a bad image"; - HtmlSanitizer sanitizer = new HtmlSanitizer(Settings.builder() - .put("xpack.notification.email.html.sanitization.allow", "img:all") - .build()); + HtmlSanitizer sanitizer = new HtmlSanitizer( + Settings.builder().put("xpack.notification.email.html.sanitization.allow", "img:all").build() + ); String sanitizedHtml = sanitizer.sanitize(html); assertThat(sanitizedHtml, equalTo(html)); } public void testCustomTablesDisallowed() { String html = "
    cell1cell2
    "; - HtmlSanitizer sanitizer = new HtmlSanitizer(Settings.builder() - .put("xpack.notification.email.html.sanitization.disallow", "_tables") - .build()); + HtmlSanitizer sanitizer = new HtmlSanitizer( + Settings.builder().put("xpack.notification.email.html.sanitization.disallow", "_tables").build() + ); String sanitizedHtml = sanitizer.sanitize(html); assertThat(sanitizedHtml, equalTo("cell1cell2")); } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/ProfileTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/ProfileTests.java index bbaf0923efe8a..11c8dff836f01 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/ProfileTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/ProfileTests.java @@ -11,14 +11,14 @@ import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.ssl.SSLService; +import java.util.HashSet; + import javax.mail.BodyPart; import javax.mail.Part; import javax.mail.Session; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; -import java.util.HashSet; - import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.is; import static org.mockito.Mockito.mock; @@ -27,24 +27,32 @@ public class ProfileTests extends ESTestCase { public void testThatInlineAttachmentsAreCreated() throws Exception { String path = "/org/elasticsearch/xpack/watcher/actions/email/service/logo.png"; - Attachment attachment = new Attachment.Stream("inline.png", "inline.png", true, - () -> EmailServiceTests.class.getResourceAsStream(path)); + Attachment attachment = new Attachment.Stream( + "inline.png", + "inline.png", + true, + () -> EmailServiceTests.class.getResourceAsStream(path) + ); Email email = Email.builder() - .id("foo") - .from("foo@example.org") - .to("bar@example.org") - .subject(randomAlphaOfLength(10)) - .attach(attachment) - .build(); + .id("foo") + .from("foo@example.org") + .to("bar@example.org") + .subject(randomAlphaOfLength(10)) + .attach(attachment) + .build(); Settings settings = Settings.builder() - .put("xpack.notification.email.default_account", "foo") - .put("xpack.notification.email.account.foo.smtp.host", "_host") - .build(); + .put("xpack.notification.email.default_account", "foo") + .put("xpack.notification.email.account.foo.smtp.host", "_host") + .build(); - EmailService service = new EmailService(settings, null, mock(SSLService.class), - new ClusterSettings(Settings.EMPTY, new HashSet<>(EmailService.getSettings()))); + EmailService service = new EmailService( + settings, + null, + mock(SSLService.class), + new ClusterSettings(Settings.EMPTY, new HashSet<>(EmailService.getSettings())) + ); Session session = service.getAccount("foo").getConfig().createSession(); MimeMessage mimeMessage = Profile.STANDARD.toMimeMessage(email, session); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/attachment/DataAttachmentParserTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/attachment/DataAttachmentParserTests.java index 66e766f6db12e..8f748dda96cae 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/attachment/DataAttachmentParserTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/attachment/DataAttachmentParserTests.java @@ -8,11 +8,11 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.ESTestCase; import java.util.ArrayList; import java.util.HashMap; @@ -31,9 +31,13 @@ public void testSerializationWorks() throws Exception { EmailAttachmentsParser emailAttachmentsParser = new EmailAttachmentsParser(attachmentParsers); String id = "some-id"; - XContentBuilder builder = jsonBuilder().startObject().startObject(id) - .startObject(DataAttachmentParser.TYPE).field("format", randomFrom("yaml", "json")).endObject() - .endObject().endObject(); + XContentBuilder builder = jsonBuilder().startObject() + .startObject(id) + .startObject(DataAttachmentParser.TYPE) + .field("format", randomFrom("yaml", "json")) + .endObject() + .endObject() + .endObject(); XContentParser parser = createParser(JsonXContent.jsonXContent, BytesReference.bytes(builder)); logger.info("JSON: {}", Strings.toString(builder)); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/attachment/EmailAttachmentParsersTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/attachment/EmailAttachmentParsersTests.java index 04ddd89d7f32a..dc4b764ad3870 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/attachment/EmailAttachmentParsersTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/attachment/EmailAttachmentParsersTests.java @@ -7,12 +7,12 @@ package org.elasticsearch.xpack.watcher.notification.email.attachment; import org.elasticsearch.ElasticsearchParseException; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.Strings; +import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext; import org.elasticsearch.xpack.core.watcher.watch.Payload; import org.elasticsearch.xpack.watcher.common.http.HttpRequestTemplate; @@ -46,17 +46,17 @@ public void testThatCustomParsersCanBeRegistered() throws Exception { XContentBuilder builder = jsonBuilder(); builder.startObject() - .startObject("my-id") - .startObject("test") - .field("foo", "bar") - .endObject() - .endObject() - .startObject("my-other-id") - .startObject("test") - .field("foo", "baz") - .endObject() - .endObject() - .endObject(); + .startObject("my-id") + .startObject("test") + .field("foo", "bar") + .endObject() + .endObject() + .startObject("my-other-id") + .startObject("test") + .field("foo", "baz") + .endObject() + .endObject() + .endObject(); logger.info("JSON: {}", Strings.toString(builder)); XContentParser xContentParser = createParser(builder); @@ -148,8 +148,12 @@ public TestEmailAttachment parse(String id, XContentParser parser) throws IOExce @Override public Attachment toAttachment(WatchExecutionContext ctx, Payload payload, TestEmailAttachment attachment) { - return new Attachment.Bytes(attachment.id(), attachment.getValue().getBytes(StandardCharsets.UTF_8), - "personalContentType", false); + return new Attachment.Bytes( + attachment.id(), + attachment.getValue().getBytes(StandardCharsets.UTF_8), + "personalContentType", + false + ); } } @@ -188,11 +192,7 @@ public boolean inline() { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder.startObject(id) - .startObject(type()) - .field(Fields.FOO.getPreferredName(), value) - .endObject() - .endObject(); + return builder.startObject(id).startObject(type()).field(Fields.FOO.getPreferredName(), value).endObject().endObject(); } } } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/attachment/HttpEmailAttachementParserTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/attachment/HttpEmailAttachementParserTests.java index 3cb6e57d08ab5..0a79ea101f7aa 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/attachment/HttpEmailAttachementParserTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/attachment/HttpEmailAttachementParserTests.java @@ -9,10 +9,10 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.collect.MapBuilder; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext; import org.elasticsearch.xpack.core.watcher.execution.Wid; import org.elasticsearch.xpack.core.watcher.watch.Payload; @@ -52,28 +52,29 @@ public void init() throws Exception { httpClient = mock(HttpClient.class); attachmentParsers = new HashMap<>(); - attachmentParsers.put(HttpEmailAttachementParser.TYPE, - new HttpEmailAttachementParser(httpClient, new MockTextTemplateEngine())); + attachmentParsers.put(HttpEmailAttachementParser.TYPE, new HttpEmailAttachementParser(httpClient, new MockTextTemplateEngine())); emailAttachmentsParser = new EmailAttachmentsParser(attachmentParsers); } - public void testSerializationWorks() throws Exception { HttpResponse response = new HttpResponse(200, "This is my response".getBytes(UTF_8)); when(httpClient.execute(any(HttpRequest.class))).thenReturn(response); String id = "some-id"; - XContentBuilder builder = jsonBuilder().startObject().startObject(id) - .startObject(HttpEmailAttachementParser.TYPE) - .startObject("request") - .field("scheme", "http") - .field("host", "test.de") - .field("port", 80) - .field("method", "get") - .field("path", "/foo") - .startObject("params").endObject() - .startObject("headers").endObject() - .endObject(); + XContentBuilder builder = jsonBuilder().startObject() + .startObject(id) + .startObject(HttpEmailAttachementParser.TYPE) + .startObject("request") + .field("scheme", "http") + .field("host", "test.de") + .field("port", 80) + .field("method", "get") + .field("path", "/foo") + .startObject("params") + .endObject() + .startObject("headers") + .endObject() + .endObject(); boolean configureContentType = randomBoolean(); if (configureContentType) { @@ -106,15 +107,17 @@ public void testNonOkHttpCodeThrowsException() throws Exception { HttpRequestAttachment attachment = new HttpRequestAttachment("someid", requestTemplate, false, null); WatchExecutionContext ctx = createWatchExecutionContext(); - ElasticsearchException exception = expectThrows(ElasticsearchException.class, - () -> { - @SuppressWarnings("unchecked") - EmailAttachmentParser parser = - (EmailAttachmentParser) attachmentParsers.get(HttpEmailAttachementParser.TYPE); - parser.toAttachment(ctx, new Payload.Simple(), attachment); - }); - assertThat(exception.getMessage(), is("Watch[watch1] attachment[someid] HTTP error status host[localhost], port[80], " + - "method[GET], path[foo], status[403]")); + ElasticsearchException exception = expectThrows(ElasticsearchException.class, () -> { + @SuppressWarnings("unchecked") + EmailAttachmentParser parser = (EmailAttachmentParser) attachmentParsers.get( + HttpEmailAttachementParser.TYPE + ); + parser.toAttachment(ctx, new Payload.Simple(), attachment); + }); + assertThat( + exception.getMessage(), + is("Watch[watch1] attachment[someid] HTTP error status host[localhost], port[80], " + "method[GET], path[foo], status[403]") + ); } public void testEmptyResponseThrowsException() throws Exception { @@ -125,15 +128,20 @@ public void testEmptyResponseThrowsException() throws Exception { HttpRequestAttachment attachment = new HttpRequestAttachment("someid", requestTemplate, false, null); WatchExecutionContext ctx = createWatchExecutionContext(); - ElasticsearchException exception = expectThrows(ElasticsearchException.class, - () -> { - @SuppressWarnings("unchecked") - EmailAttachmentParser parser = - (EmailAttachmentParser) attachmentParsers.get(HttpEmailAttachementParser.TYPE); - parser.toAttachment(ctx, new Payload.Simple(), attachment); - }); - assertThat(exception.getMessage(), is("Watch[watch1] attachment[someid] HTTP empty response body host[localhost], port[80], " + - "method[GET], path[foo], status[200]")); + ElasticsearchException exception = expectThrows(ElasticsearchException.class, () -> { + @SuppressWarnings("unchecked") + EmailAttachmentParser parser = (EmailAttachmentParser) attachmentParsers.get( + HttpEmailAttachementParser.TYPE + ); + parser.toAttachment(ctx, new Payload.Simple(), attachment); + }); + assertThat( + exception.getMessage(), + is( + "Watch[watch1] attachment[someid] HTTP empty response body host[localhost], port[80], " + + "method[GET], path[foo], status[200]" + ) + ); } public void testHttpClientThrowsException() throws Exception { @@ -143,13 +151,13 @@ public void testHttpClientThrowsException() throws Exception { HttpRequestAttachment attachment = new HttpRequestAttachment("someid", requestTemplate, false, null); WatchExecutionContext ctx = createWatchExecutionContext(); - IOException exception = expectThrows(IOException.class, - () -> { - @SuppressWarnings("unchecked") - EmailAttachmentParser parser = - (EmailAttachmentParser) attachmentParsers.get(HttpEmailAttachementParser.TYPE); - parser.toAttachment(ctx, new Payload.Simple(), attachment); - }); + IOException exception = expectThrows(IOException.class, () -> { + @SuppressWarnings("unchecked") + EmailAttachmentParser parser = (EmailAttachmentParser) attachmentParsers.get( + HttpEmailAttachementParser.TYPE + ); + parser.toAttachment(ctx, new Payload.Simple(), attachment); + }); assertThat(exception.getMessage(), is("whatever")); } @@ -157,13 +165,11 @@ private WatchExecutionContext createWatchExecutionContext() { ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC); Wid wid = new Wid(randomAlphaOfLength(5), now); Map metadata = MapBuilder.newMapBuilder().put("_key", "_val").map(); - return mockExecutionContextBuilder("watch1") - .wid(wid) - .payload(new Payload.Simple()) - .time("watch1", now) - .metadata(metadata) - .buildMock(); + return mockExecutionContextBuilder("watch1").wid(wid) + .payload(new Payload.Simple()) + .time("watch1", now) + .metadata(metadata) + .buildMock(); } - } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/attachment/ReportingAttachmentParserTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/attachment/ReportingAttachmentParserTests.java index a136913a8e122..4d402cdcaec48 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/attachment/ReportingAttachmentParserTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/attachment/ReportingAttachmentParserTests.java @@ -7,16 +7,17 @@ package org.elasticsearch.xpack.watcher.notification.email.attachment; import com.fasterxml.jackson.core.io.JsonEOFException; + import org.elasticsearch.ElasticsearchException; import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.ClusterSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParseException; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext; import org.elasticsearch.xpack.core.watcher.execution.Wid; import org.elasticsearch.xpack.core.watcher.support.xcontent.WatcherParams; @@ -92,9 +93,10 @@ public void init() throws Exception { public void testSerializationWorks() throws Exception { String id = "some-id"; - XContentBuilder builder = jsonBuilder().startObject().startObject(id) - .startObject(ReportingAttachmentParser.TYPE) - .field("url", dashboardUrl); + XContentBuilder builder = jsonBuilder().startObject() + .startObject(id) + .startObject(ReportingAttachmentParser.TYPE) + .field("url", dashboardUrl); Integer retries = null; boolean withRetries = randomBoolean(); @@ -119,10 +121,12 @@ public void testSerializationWorks() throws Exception { boolean withAuth = randomBoolean(); boolean isPasswordEncrypted = randomBoolean(); if (withAuth) { - builder.startObject("auth").startObject("basic") - .field("username", "foo") - .field("password", isPasswordEncrypted ? "::es_redacted::" :"secret") - .endObject().endObject(); + builder.startObject("auth") + .startObject("basic") + .field("username", "foo") + .field("password", isPasswordEncrypted ? "::es_redacted::" : "secret") + .endObject() + .endObject(); auth = new BasicAuth("foo", "secret".toCharArray()); } @@ -130,10 +134,7 @@ public void testSerializationWorks() throws Exception { boolean withProxy = randomBoolean(); if (withProxy) { proxy = new HttpProxy("example.org", 8080); - builder.startObject("proxy") - .field("host", proxy.getHost()) - .field("port", proxy.getPort()) - .endObject(); + builder.startObject("proxy").field("host", proxy.getHost()).field("port", proxy.getPort()).endObject(); } builder.endObject().endObject().endObject(); @@ -165,17 +166,25 @@ public void testGoodCase() throws Exception { String randomContentType = randomAlphaOfLength(20); Map headers = new HashMap<>(); headers.put("Content-Type", new String[] { randomContentType }); - when(httpClient.execute(any(HttpRequest.class))) - .thenReturn(new HttpResponse(200, "{\"path\":\""+ path +"\", \"other\":\"content\"}")) - .thenReturn(new HttpResponse(503)) - .thenReturn(new HttpResponse(503)) - .thenReturn(new HttpResponse(503)) - .thenReturn(new HttpResponse(503)) - .thenReturn(new HttpResponse(503)) - .thenReturn(new HttpResponse(200, content, headers)); - - ReportingAttachment reportingAttachment = - new ReportingAttachment("foo", dashboardUrl, randomBoolean(), TimeValue.timeValueMillis(1), 10, null, null); + when(httpClient.execute(any(HttpRequest.class))).thenReturn( + new HttpResponse(200, "{\"path\":\"" + path + "\", \"other\":\"content\"}") + ) + .thenReturn(new HttpResponse(503)) + .thenReturn(new HttpResponse(503)) + .thenReturn(new HttpResponse(503)) + .thenReturn(new HttpResponse(503)) + .thenReturn(new HttpResponse(503)) + .thenReturn(new HttpResponse(200, content, headers)); + + ReportingAttachment reportingAttachment = new ReportingAttachment( + "foo", + dashboardUrl, + randomBoolean(), + TimeValue.timeValueMillis(1), + 10, + null, + null + ); Attachment attachment = reportingAttachmentParser.toAttachment(createWatchExecutionContext(), Payload.EMPTY, reportingAttachment); assertThat(attachment, instanceOf(Attachment.Bytes.class)); assertThat(attachment.getWarnings(), hasSize(0)); @@ -200,63 +209,82 @@ public void testGoodCase() throws Exception { } public void testInitialRequestFailsWithError() throws Exception { - when(httpClient.execute(any(HttpRequest.class))) - .thenReturn(new HttpResponse(403)); + when(httpClient.execute(any(HttpRequest.class))).thenReturn(new HttpResponse(403)); ReportingAttachment attachment = new ReportingAttachment("foo", dashboardUrl, randomBoolean(), null, null, null, null); - ElasticsearchException e = expectThrows(ElasticsearchException.class, - () -> reportingAttachmentParser.toAttachment(createWatchExecutionContext(), Payload.EMPTY, attachment)); + ElasticsearchException e = expectThrows( + ElasticsearchException.class, + () -> reportingAttachmentParser.toAttachment(createWatchExecutionContext(), Payload.EMPTY, attachment) + ); assertThat(e.getMessage(), containsString("Error response when trying to trigger reporting generation")); } public void testInitialRequestThrowsIOException() throws Exception { when(httpClient.execute(any(HttpRequest.class))).thenThrow(new IOException("Connection timed out")); ReportingAttachment attachment = new ReportingAttachment("foo", "http://www.example.org/", randomBoolean(), null, null, null, null); - IOException e = expectThrows(IOException.class, - () -> reportingAttachmentParser.toAttachment(createWatchExecutionContext(), Payload.EMPTY, attachment)); + IOException e = expectThrows( + IOException.class, + () -> reportingAttachmentParser.toAttachment(createWatchExecutionContext(), Payload.EMPTY, attachment) + ); assertThat(e.getMessage(), containsString("Connection timed out")); } public void testInitialRequestContainsInvalidPayload() throws Exception { when(httpClient.execute(any(HttpRequest.class))) - // closing json bracket is missing - .thenReturn(new HttpResponse(200, "{\"path\":\"anything\"")); + // closing json bracket is missing + .thenReturn(new HttpResponse(200, "{\"path\":\"anything\"")); ReportingAttachment attachment = new ReportingAttachment("foo", dashboardUrl, randomBoolean(), null, null, null, null); - JsonEOFException e = expectThrows(JsonEOFException.class, - () -> reportingAttachmentParser.toAttachment(createWatchExecutionContext(), Payload.EMPTY, attachment)); + JsonEOFException e = expectThrows( + JsonEOFException.class, + () -> reportingAttachmentParser.toAttachment(createWatchExecutionContext(), Payload.EMPTY, attachment) + ); assertThat(e.getMessage(), containsString("Unexpected end-of-input")); } public void testInitialRequestContainsPathAsObject() throws Exception { when(httpClient.execute(any(HttpRequest.class))) - // path must be a field, but is an object here - .thenReturn(new HttpResponse(200, "{\"path\": { \"foo\" : \"anything\"}}")); + // path must be a field, but is an object here + .thenReturn(new HttpResponse(200, "{\"path\": { \"foo\" : \"anything\"}}")); ReportingAttachment attachment = new ReportingAttachment("foo", "http://www.example.org/", randomBoolean(), null, null, null, null); - XContentParseException e = expectThrows(XContentParseException.class, - () -> reportingAttachmentParser.toAttachment(createWatchExecutionContext(), Payload.EMPTY, attachment)); - assertThat(e.getMessage(), - containsString("[reporting_attachment_kibana_payload] path doesn't support values of type: START_OBJECT")); + XContentParseException e = expectThrows( + XContentParseException.class, + () -> reportingAttachmentParser.toAttachment(createWatchExecutionContext(), Payload.EMPTY, attachment) + ); + assertThat( + e.getMessage(), + containsString("[reporting_attachment_kibana_payload] path doesn't support values of type: START_OBJECT") + ); } public void testInitialRequestDoesNotContainPathInJson() throws Exception { when(httpClient.execute(any(HttpRequest.class))).thenReturn(new HttpResponse(200, "{\"foo\":\"bar\"}")); ReportingAttachment attachment = new ReportingAttachment("foo", dashboardUrl, randomBoolean(), null, null, null, null); - ElasticsearchException e = expectThrows(ElasticsearchException.class, - () -> reportingAttachmentParser.toAttachment(createWatchExecutionContext(), Payload.EMPTY, attachment)); + ElasticsearchException e = expectThrows( + ElasticsearchException.class, + () -> reportingAttachmentParser.toAttachment(createWatchExecutionContext(), Payload.EMPTY, attachment) + ); assertThat(e.getMessage(), containsString("Watch[watch1] reporting[foo] field path found in JSON payload")); } public void testPollingRequestIsError() throws Exception { boolean hasBody = randomBoolean(); - when(httpClient.execute(any(HttpRequest.class))) - .thenReturn(new HttpResponse(200, "{\"path\":\"whatever\"}")) - .thenReturn(new HttpResponse(403, hasBody ? "no permissions" : null)); - - ReportingAttachment attachment = - new ReportingAttachment("foo", "http://www.example.org/", randomBoolean(), TimeValue.timeValueMillis(1), 10, null, null); - - ElasticsearchException e = expectThrows(ElasticsearchException.class, - () -> reportingAttachmentParser.toAttachment(createWatchExecutionContext(), Payload.EMPTY, attachment)); + when(httpClient.execute(any(HttpRequest.class))).thenReturn(new HttpResponse(200, "{\"path\":\"whatever\"}")) + .thenReturn(new HttpResponse(403, hasBody ? "no permissions" : null)); + + ReportingAttachment attachment = new ReportingAttachment( + "foo", + "http://www.example.org/", + randomBoolean(), + TimeValue.timeValueMillis(1), + 10, + null, + null + ); + + ElasticsearchException e = expectThrows( + ElasticsearchException.class, + () -> reportingAttachmentParser.toAttachment(createWatchExecutionContext(), Payload.EMPTY, attachment) + ); assertThat(e.getMessage(), containsString("Error when polling pdf")); if (hasBody) { assertThat(e.getMessage(), containsString("body[no permissions]")); @@ -264,54 +292,84 @@ public void testPollingRequestIsError() throws Exception { } public void testPollingRequestRetryIsExceeded() throws Exception { - when(httpClient.execute(any(HttpRequest.class))) - .thenReturn(new HttpResponse(200, "{\"path\":\"whatever\"}")) - .thenReturn(new HttpResponse(503)) - .thenReturn(new HttpResponse(503)); - - ReportingAttachment attachment = - new ReportingAttachment("foo", "http://www.example.org/", randomBoolean(), TimeValue.timeValueMillis(1), 1, null, null); - - ElasticsearchException e = expectThrows(ElasticsearchException.class, - () -> reportingAttachmentParser.toAttachment(createWatchExecutionContext(), Payload.EMPTY, attachment)); + when(httpClient.execute(any(HttpRequest.class))).thenReturn(new HttpResponse(200, "{\"path\":\"whatever\"}")) + .thenReturn(new HttpResponse(503)) + .thenReturn(new HttpResponse(503)); + + ReportingAttachment attachment = new ReportingAttachment( + "foo", + "http://www.example.org/", + randomBoolean(), + TimeValue.timeValueMillis(1), + 1, + null, + null + ); + + ElasticsearchException e = expectThrows( + ElasticsearchException.class, + () -> reportingAttachmentParser.toAttachment(createWatchExecutionContext(), Payload.EMPTY, attachment) + ); assertThat(e.getMessage(), containsString("Aborting due to maximum number of retries hit [1]")); } public void testPollingRequestUnknownHTTPError() throws Exception { - when(httpClient.execute(any(HttpRequest.class))) - .thenReturn(new HttpResponse(200, "{\"path\":\"whatever\"}")) - .thenReturn(new HttpResponse(1)); - - ReportingAttachment attachment = - new ReportingAttachment("foo", "http://www.example.org/", randomBoolean(), TimeValue.timeValueMillis(1), null, null, null); - - IllegalStateException e = expectThrows(IllegalStateException.class, - () -> reportingAttachmentParser.toAttachment(createWatchExecutionContext(), Payload.EMPTY, attachment)); + when(httpClient.execute(any(HttpRequest.class))).thenReturn(new HttpResponse(200, "{\"path\":\"whatever\"}")) + .thenReturn(new HttpResponse(1)); + + ReportingAttachment attachment = new ReportingAttachment( + "foo", + "http://www.example.org/", + randomBoolean(), + TimeValue.timeValueMillis(1), + null, + null, + null + ); + + IllegalStateException e = expectThrows( + IllegalStateException.class, + () -> reportingAttachmentParser.toAttachment(createWatchExecutionContext(), Payload.EMPTY, attachment) + ); assertThat(e.getMessage(), containsString("Unexpected status code")); } public void testPollingRequestIOException() throws Exception { - when(httpClient.execute(any(HttpRequest.class))) - .thenReturn(new HttpResponse(200, "{\"path\":\"whatever\"}")) - .thenThrow(new IOException("whatever")); - - ReportingAttachment attachment = - new ReportingAttachment("foo", "http://www.example.org/", randomBoolean(), TimeValue.timeValueMillis(1), null, null, null); - - IOException e = expectThrows(IOException.class, - () -> reportingAttachmentParser.toAttachment(createWatchExecutionContext(), Payload.EMPTY, attachment)); + when(httpClient.execute(any(HttpRequest.class))).thenReturn(new HttpResponse(200, "{\"path\":\"whatever\"}")) + .thenThrow(new IOException("whatever")); + + ReportingAttachment attachment = new ReportingAttachment( + "foo", + "http://www.example.org/", + randomBoolean(), + TimeValue.timeValueMillis(1), + null, + null, + null + ); + + IOException e = expectThrows( + IOException.class, + () -> reportingAttachmentParser.toAttachment(createWatchExecutionContext(), Payload.EMPTY, attachment) + ); assertThat(e.getMessage(), containsString("whatever")); } public void testWithBasicAuth() throws Exception { String content = randomAlphaOfLength(200); - when(httpClient.execute(any(HttpRequest.class))) - .thenReturn(new HttpResponse(200, "{\"path\":\"whatever\"}")) - .thenReturn(new HttpResponse(503)) - .thenReturn(new HttpResponse(200, content)); - - ReportingAttachment attachment = new ReportingAttachment("foo", dashboardUrl, randomBoolean(), - TimeValue.timeValueMillis(1), 10, new BasicAuth("foo", "bar".toCharArray()), null); + when(httpClient.execute(any(HttpRequest.class))).thenReturn(new HttpResponse(200, "{\"path\":\"whatever\"}")) + .thenReturn(new HttpResponse(503)) + .thenReturn(new HttpResponse(200, content)); + + ReportingAttachment attachment = new ReportingAttachment( + "foo", + dashboardUrl, + randomBoolean(), + TimeValue.timeValueMillis(1), + 10, + new BasicAuth("foo", "bar".toCharArray()), + null + ); reportingAttachmentParser.toAttachment(createWatchExecutionContext(), Payload.EMPTY, attachment); @@ -328,43 +386,48 @@ public void testWithBasicAuth() throws Exception { } public void testPollingDefaultsRetries() throws Exception { - when(httpClient.execute(any(HttpRequest.class))) - .thenReturn(new HttpResponse(200, "{\"path\":\"whatever\"}")) - .thenReturn(new HttpResponse(503)); - - ReportingAttachment attachment = new ReportingAttachment("foo", dashboardUrl, randomBoolean(), TimeValue.timeValueMillis(1), - RETRIES_SETTING.getDefault(Settings.EMPTY), new BasicAuth("foo", "bar".toCharArray()), null); - expectThrows(ElasticsearchException.class, () -> - reportingAttachmentParser.toAttachment(createWatchExecutionContext(), Payload.EMPTY, attachment)); + when(httpClient.execute(any(HttpRequest.class))).thenReturn(new HttpResponse(200, "{\"path\":\"whatever\"}")) + .thenReturn(new HttpResponse(503)); + + ReportingAttachment attachment = new ReportingAttachment( + "foo", + dashboardUrl, + randomBoolean(), + TimeValue.timeValueMillis(1), + RETRIES_SETTING.getDefault(Settings.EMPTY), + new BasicAuth("foo", "bar".toCharArray()), + null + ); + expectThrows( + ElasticsearchException.class, + () -> reportingAttachmentParser.toAttachment(createWatchExecutionContext(), Payload.EMPTY, attachment) + ); verify(httpClient, times(RETRIES_SETTING.getDefault(Settings.EMPTY) + 1)).execute(any()); } public void testPollingDefaultCanBeOverriddenBySettings() throws Exception { int retries = 10; - when(httpClient.execute(any(HttpRequest.class))) - .thenReturn(new HttpResponse(200, "{\"path\":\"whatever\"}")) - .thenReturn(new HttpResponse(503)); + when(httpClient.execute(any(HttpRequest.class))).thenReturn(new HttpResponse(200, "{\"path\":\"whatever\"}")) + .thenReturn(new HttpResponse(503)); ReportingAttachment attachment = new ReportingAttachment("foo", dashboardUrl, randomBoolean(), null, null, null, null); - Settings settings = Settings.builder() - .put(INTERVAL_SETTING.getKey(), "1ms") - .put(RETRIES_SETTING.getKey(), retries) - .build(); + Settings settings = Settings.builder().put(INTERVAL_SETTING.getKey(), "1ms").put(RETRIES_SETTING.getKey(), retries).build(); reportingAttachmentParser = new ReportingAttachmentParser(settings, httpClient, templateEngine, clusterSettings); - expectThrows(ElasticsearchException.class, () -> - reportingAttachmentParser.toAttachment(createWatchExecutionContext(), Payload.EMPTY, attachment)); + expectThrows( + ElasticsearchException.class, + () -> reportingAttachmentParser.toAttachment(createWatchExecutionContext(), Payload.EMPTY, attachment) + ); verify(httpClient, times(retries + 1)).execute(any()); } public void testThatUrlIsTemplatable() throws Exception { - when(httpClient.execute(any(HttpRequest.class))) - .thenReturn(new HttpResponse(200, "{\"path\":\"whatever\"}")) - .thenReturn(new HttpResponse(503)) - .thenReturn(new HttpResponse(200, randomAlphaOfLength(10))); + when(httpClient.execute(any(HttpRequest.class))).thenReturn(new HttpResponse(200, "{\"path\":\"whatever\"}")) + .thenReturn(new HttpResponse(503)) + .thenReturn(new HttpResponse(200, randomAlphaOfLength(10))); TextTemplateEngine replaceHttpWithHttpsTemplateEngine = new TextTemplateEngine(null) { @Override @@ -373,10 +436,21 @@ public String render(TextTemplate textTemplate, Map model) { } }; - ReportingAttachment attachment = new ReportingAttachment("foo", "http://www.example.org/REPLACEME", randomBoolean(), - TimeValue.timeValueMillis(1), 10, new BasicAuth("foo", "bar".toCharArray()), null); - reportingAttachmentParser = new ReportingAttachmentParser(Settings.EMPTY, httpClient, - replaceHttpWithHttpsTemplateEngine, clusterSettings); + ReportingAttachment attachment = new ReportingAttachment( + "foo", + "http://www.example.org/REPLACEME", + randomBoolean(), + TimeValue.timeValueMillis(1), + 10, + new BasicAuth("foo", "bar".toCharArray()), + null + ); + reportingAttachmentParser = new ReportingAttachmentParser( + Settings.EMPTY, + httpClient, + replaceHttpWithHttpsTemplateEngine, + clusterSettings + ); reportingAttachmentParser.toAttachment(createWatchExecutionContext(), Payload.EMPTY, attachment); ArgumentCaptor requestArgumentCaptor = ArgumentCaptor.forClass(HttpRequest.class); @@ -387,13 +461,17 @@ public String render(TextTemplate textTemplate, Map model) { } public void testRetrySettingCannotBeNegative() throws Exception { - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> - new ReportingAttachment("foo", "http://www.example.org/REPLACEME", randomBoolean(), null, -10, null, null)); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> new ReportingAttachment("foo", "http://www.example.org/REPLACEME", randomBoolean(), null, -10, null, null) + ); assertThat(e.getMessage(), is("Retries for attachment must be >= 0")); Settings invalidSettings = Settings.builder().put("xpack.notification.reporting.retries", -10).build(); - e = expectThrows(IllegalArgumentException.class, - () -> new ReportingAttachmentParser(invalidSettings, httpClient, templateEngine, clusterSettings)); + e = expectThrows( + IllegalArgumentException.class, + () -> new ReportingAttachmentParser(invalidSettings, httpClient, templateEngine, clusterSettings) + ); assertThat(e.getMessage(), is("Failed to parse value [-10] for setting [xpack.notification.reporting.retries] must be >= 0")); } @@ -404,14 +482,20 @@ public void testHttpProxy() throws Exception { Map headers = new HashMap<>(); headers.put("Content-Type", new String[] { randomContentType }); ArgumentCaptor requestCaptor = ArgumentCaptor.forClass(HttpRequest.class); - when(httpClient.execute(requestCaptor.capture())) - .thenReturn(new HttpResponse(200, "{\"path\":\""+ path +"\", \"other\":\"content\"}")) - .thenReturn(new HttpResponse(503)) - .thenReturn(new HttpResponse(200, content, headers)); + when(httpClient.execute(requestCaptor.capture())).thenReturn( + new HttpResponse(200, "{\"path\":\"" + path + "\", \"other\":\"content\"}") + ).thenReturn(new HttpResponse(503)).thenReturn(new HttpResponse(200, content, headers)); HttpProxy proxy = new HttpProxy("localhost", 8080); - ReportingAttachment reportingAttachment = - new ReportingAttachment("foo", "http://www.example.org/", randomBoolean(), TimeValue.timeValueMillis(1), null, null, proxy); + ReportingAttachment reportingAttachment = new ReportingAttachment( + "foo", + "http://www.example.org/", + randomBoolean(), + TimeValue.timeValueMillis(1), + null, + null, + proxy + ); reportingAttachmentParser.toAttachment(createWatchExecutionContext(), Payload.EMPTY, reportingAttachment); @@ -426,19 +510,28 @@ public void testDefaultWarnings() throws Exception { String reportId = randomAlphaOfLength(5); Map headers = new HashMap<>(); headers.put("Content-Type", new String[] { randomContentType }); - WARNINGS.keySet().forEach((k) -> headers.put(k, new String[]{"true"})); - when(httpClient.execute(any(HttpRequest.class))) - .thenReturn(new HttpResponse(200, "{\"path\":\""+ path +"\", \"other\":\"content\"}")) - .thenReturn(new HttpResponse(200, content, headers)); - - ReportingAttachment reportingAttachment = - new ReportingAttachment(reportId, dashboardUrl, randomBoolean(), TimeValue.timeValueMillis(1), 10, null, null); + WARNINGS.keySet().forEach((k) -> headers.put(k, new String[] { "true" })); + when(httpClient.execute(any(HttpRequest.class))).thenReturn( + new HttpResponse(200, "{\"path\":\"" + path + "\", \"other\":\"content\"}") + ).thenReturn(new HttpResponse(200, content, headers)); + + ReportingAttachment reportingAttachment = new ReportingAttachment( + reportId, + dashboardUrl, + randomBoolean(), + TimeValue.timeValueMillis(1), + 10, + null, + null + ); Attachment attachment = reportingAttachmentParser.toAttachment(createWatchExecutionContext(), Payload.EMPTY, reportingAttachment); assertThat(attachment, instanceOf(Attachment.Bytes.class)); assertThat(attachment.getWarnings(), hasSize(WARNINGS.keySet().size())); - //parameterize the messages - assertEquals(attachment.getWarnings(), WARNINGS.values().stream(). - map(s -> String.format(Locale.ROOT, s, reportId)).collect(Collectors.toSet())); + // parameterize the messages + assertEquals( + attachment.getWarnings(), + WARNINGS.values().stream().map(s -> String.format(Locale.ROOT, s, reportId)).collect(Collectors.toSet()) + ); Attachment.Bytes bytesAttachment = (Attachment.Bytes) attachment; assertThat(new String(bytesAttachment.bytes(), StandardCharsets.UTF_8), is(content)); @@ -453,20 +546,26 @@ public void testCustomWarningsNoParams() throws Exception { Map headers = new HashMap<>(); headers.put("Content-Type", new String[] { randomContentType }); Map customWarnings = new HashMap<>(WARNINGS.size()); - WARNINGS.keySet().forEach((k) -> - { + WARNINGS.keySet().forEach((k) -> { final String warning = randomAlphaOfLength(20); customWarnings.put(k, warning); reportingAttachmentParser.addWarningText(k, warning); - headers.put(k, new String[]{"true"}); + headers.put(k, new String[] { "true" }); }); - when(httpClient.execute(any(HttpRequest.class))) - .thenReturn(new HttpResponse(200, "{\"path\":\""+ path +"\", \"other\":\"content\"}")) - .thenReturn(new HttpResponse(200, content, headers)); - - ReportingAttachment reportingAttachment = - new ReportingAttachment(reportId, dashboardUrl, randomBoolean(), TimeValue.timeValueMillis(1), 10, null, null); + when(httpClient.execute(any(HttpRequest.class))).thenReturn( + new HttpResponse(200, "{\"path\":\"" + path + "\", \"other\":\"content\"}") + ).thenReturn(new HttpResponse(200, content, headers)); + + ReportingAttachment reportingAttachment = new ReportingAttachment( + reportId, + dashboardUrl, + randomBoolean(), + TimeValue.timeValueMillis(1), + 10, + null, + null + ); Attachment attachment = reportingAttachmentParser.toAttachment(createWatchExecutionContext(), Payload.EMPTY, reportingAttachment); assertThat(attachment, instanceOf(Attachment.Bytes.class)); assertThat(attachment.getWarnings(), hasSize(WARNINGS.keySet().size())); @@ -483,33 +582,39 @@ public void testCustomWarningsWithParams() throws Exception { String randomContentType = randomAlphaOfLength(20); String reportId = randomAlphaOfLength(5); Map headers = new HashMap<>(); - headers.put("Content-Type", new String[]{randomContentType}); + headers.put("Content-Type", new String[] { randomContentType }); Map customWarnings = new HashMap<>(WARNINGS.size()); - WARNINGS.keySet().forEach((k) -> - { - //add a parameter + WARNINGS.keySet().forEach((k) -> { + // add a parameter final String warning = randomAlphaOfLength(20) + " %s"; customWarnings.put(k, warning); reportingAttachmentParser.addWarningText(k, warning); - headers.put(k, new String[]{"true"}); + headers.put(k, new String[] { "true" }); }); - when(httpClient.execute(any(HttpRequest.class))) - .thenReturn(new HttpResponse(200, "{\"path\":\"" + path + "\", \"other\":\"content\"}")) - .thenReturn(new HttpResponse(200, content, headers)); - - ReportingAttachment reportingAttachment = - new ReportingAttachment(reportId, dashboardUrl, randomBoolean(), TimeValue.timeValueMillis(1), 10, null, null); + when(httpClient.execute(any(HttpRequest.class))).thenReturn( + new HttpResponse(200, "{\"path\":\"" + path + "\", \"other\":\"content\"}") + ).thenReturn(new HttpResponse(200, content, headers)); + + ReportingAttachment reportingAttachment = new ReportingAttachment( + reportId, + dashboardUrl, + randomBoolean(), + TimeValue.timeValueMillis(1), + 10, + null, + null + ); Attachment attachment = reportingAttachmentParser.toAttachment(createWatchExecutionContext(), Payload.EMPTY, reportingAttachment); assertThat(attachment, instanceOf(Attachment.Bytes.class)); assertThat(attachment.getWarnings(), hasSize(WARNINGS.keySet().size())); - //parameterize the messages - assertEquals(attachment.getWarnings(), customWarnings.values().stream(). - map(s -> String.format(Locale.ROOT, s, reportId)).collect(Collectors.toSet())); - //ensure the reportId is parameterized in - attachment.getWarnings().forEach(s -> { - assertThat(s, containsString(reportId)); - }); + // parameterize the messages + assertEquals( + attachment.getWarnings(), + customWarnings.values().stream().map(s -> String.format(Locale.ROOT, s, reportId)).collect(Collectors.toSet()) + ); + // ensure the reportId is parameterized in + attachment.getWarnings().forEach(s -> { assertThat(s, containsString(reportId)); }); Attachment.Bytes bytesAttachment = (Attachment.Bytes) attachment; assertThat(new String(bytesAttachment.bytes(), StandardCharsets.UTF_8), is(content)); assertThat(bytesAttachment.contentType(), is(randomContentType)); @@ -521,23 +626,29 @@ public void testWarningsSuppress() throws Exception { String randomContentType = randomAlphaOfLength(20); String reportId = randomAlphaOfLength(5); Map headers = new HashMap<>(); - headers.put("Content-Type", new String[]{randomContentType}); + headers.put("Content-Type", new String[] { randomContentType }); Map customWarnings = new HashMap<>(WARNINGS.size()); - WARNINGS.keySet().forEach((k) -> - { + WARNINGS.keySet().forEach((k) -> { final String warning = randomAlphaOfLength(20); customWarnings.put(k, warning); reportingAttachmentParser.addWarningText(k, warning); reportingAttachmentParser.setWarningEnabled(false); - headers.put(k, new String[]{"true"}); + headers.put(k, new String[] { "true" }); }); - when(httpClient.execute(any(HttpRequest.class))) - .thenReturn(new HttpResponse(200, "{\"path\":\"" + path + "\", \"other\":\"content\"}")) - .thenReturn(new HttpResponse(200, content, headers)); - - ReportingAttachment reportingAttachment = - new ReportingAttachment(reportId, dashboardUrl, randomBoolean(), TimeValue.timeValueMillis(1), 10, null, null); + when(httpClient.execute(any(HttpRequest.class))).thenReturn( + new HttpResponse(200, "{\"path\":\"" + path + "\", \"other\":\"content\"}") + ).thenReturn(new HttpResponse(200, content, headers)); + + ReportingAttachment reportingAttachment = new ReportingAttachment( + reportId, + dashboardUrl, + randomBoolean(), + TimeValue.timeValueMillis(1), + 10, + null, + null + ); Attachment attachment = reportingAttachmentParser.toAttachment(createWatchExecutionContext(), Payload.EMPTY, reportingAttachment); assertThat(attachment, instanceOf(Attachment.Bytes.class)); assertThat(attachment.getWarnings(), hasSize(0)); @@ -550,8 +661,10 @@ public void testWarningsSuppress() throws Exception { public void testWarningValidation() { WARNINGS.forEach((k, v) -> { String keyName = randomAlphaOfLength(5) + "notavalidsettingname"; - IllegalArgumentException expectedException = expectThrows(IllegalArgumentException.class, - () -> reportingAttachmentParser.warningValidator(keyName, randomAlphaOfLength(10))); + IllegalArgumentException expectedException = expectThrows( + IllegalArgumentException.class, + () -> reportingAttachmentParser.warningValidator(keyName, randomAlphaOfLength(10)) + ); assertThat(expectedException.getMessage(), containsString(keyName)); assertThat(expectedException.getMessage(), containsString("is not supported")); }); @@ -559,20 +672,19 @@ public void testWarningValidation() { private WatchExecutionContext createWatchExecutionContext() { ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC); - return mockExecutionContextBuilder("watch1") - .wid(new Wid(randomAlphaOfLength(5), now)) + return mockExecutionContextBuilder("watch1").wid(new Wid(randomAlphaOfLength(5), now)) .payload(new Payload.Simple()) .time("watch1", now) .metadata(Collections.emptyMap()) .buildMock(); } - private ClusterService mockClusterService() { ClusterService clusterService = mock(ClusterService.class); - ClusterSettings clusterSettings = - new ClusterSettings(Settings.EMPTY, - Set.of(INTERVAL_SETTING, RETRIES_SETTING, REPORT_WARNING_ENABLED_SETTING, REPORT_WARNING_TEXT)); + ClusterSettings clusterSettings = new ClusterSettings( + Settings.EMPTY, + Set.of(INTERVAL_SETTING, RETRIES_SETTING, REPORT_WARNING_ENABLED_SETTING, REPORT_WARNING_TEXT) + ); when(clusterService.getClusterSettings()).thenReturn(clusterSettings); return clusterService; } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/support/EmailServer.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/support/EmailServer.java index c12100c166572..041b6b7686301 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/support/EmailServer.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/support/EmailServer.java @@ -13,12 +13,6 @@ import org.subethamail.smtp.helper.SimpleMessageListenerAdapter; import org.subethamail.smtp.server.SMTPServer; -import javax.mail.MessagingException; -import javax.mail.Session; -import javax.mail.internet.MimeMessage; -import javax.net.ssl.SSLContext; -import javax.net.ssl.SSLSocket; -import javax.net.ssl.SSLSocketFactory; import java.io.IOException; import java.io.InputStream; import java.net.InetSocketAddress; @@ -29,6 +23,13 @@ import java.util.Properties; import java.util.concurrent.CopyOnWriteArrayList; +import javax.mail.MessagingException; +import javax.mail.Session; +import javax.mail.internet.MimeMessage; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSocket; +import javax.net.ssl.SSLSocketFactory; + import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; import static org.junit.Assert.fail; diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/jira/JiraAccountTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/jira/JiraAccountTests.java index 9a81e1c6d77f6..325f0fae54c25 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/jira/JiraAccountTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/jira/JiraAccountTests.java @@ -8,11 +8,11 @@ import org.apache.http.HttpStatus; import org.elasticsearch.common.collect.MapBuilder; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.settings.ClusterSettings; import org.elasticsearch.common.settings.MockSecureSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsException; +import org.elasticsearch.core.Tuple; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.watcher.common.http.HttpClient; import org.elasticsearch.xpack.watcher.common.http.HttpProxy; @@ -81,9 +81,9 @@ public void testJiraAccountSettings() { assertThat(e.getMessage(), containsString("invalid jira [test] account settings. missing required [secure_password] setting")); } - public void testInvalidSchemeUrl() throws Exception{ + public void testInvalidSchemeUrl() throws Exception { MockSecureSettings secureSettings = new MockSecureSettings(); - secureSettings.setString(JiraAccount.SECURE_URL_SETTING.getKey(),"test"); //Setting test as invalid scheme url + secureSettings.setString(JiraAccount.SECURE_URL_SETTING.getKey(), "test"); // Setting test as invalid scheme url secureSettings.setString(JiraAccount.SECURE_USER_SETTING.getKey(), "foo"); secureSettings.setString(JiraAccount.SECURE_PASSWORD_SETTING.getKey(), "password"); Settings settings = Settings.builder().setSecureSettings(secureSettings).build(); @@ -180,12 +180,18 @@ private void assertCustomUrl(String urlSettings, String expectedPath) throws IOE @SuppressWarnings("unchecked") private void addAccountSettings(String name, Settings.Builder builder) { final MockSecureSettings secureSettings = new MockSecureSettings(); - secureSettings.setString("xpack.notification.jira.account." + name + "." + JiraAccount.SECURE_URL_SETTING.getKey(), - "https://internal-jira.elastic.co:443"); - secureSettings.setString("xpack.notification.jira.account." + name + "." + JiraAccount.SECURE_USER_SETTING.getKey(), - randomAlphaOfLength(10)); - secureSettings.setString("xpack.notification.jira.account." + name + "." + JiraAccount.SECURE_PASSWORD_SETTING.getKey(), - randomAlphaOfLength(10)); + secureSettings.setString( + "xpack.notification.jira.account." + name + "." + JiraAccount.SECURE_URL_SETTING.getKey(), + "https://internal-jira.elastic.co:443" + ); + secureSettings.setString( + "xpack.notification.jira.account." + name + "." + JiraAccount.SECURE_USER_SETTING.getKey(), + randomAlphaOfLength(10) + ); + secureSettings.setString( + "xpack.notification.jira.account." + name + "." + JiraAccount.SECURE_PASSWORD_SETTING.getKey(), + randomAlphaOfLength(10) + ); builder.setSecureSettings(secureSettings); Map defaults = randomIssueDefaults(); @@ -228,13 +234,13 @@ public static Map randomIssueDefaults() { static Tuple randomHttpError() { Tuple error = randomFrom( - tuple(400, "Bad Request"), - tuple(401, "Unauthorized (authentication credentials are invalid)"), - tuple(403, "Forbidden (account doesn't have permission to create this issue)"), - tuple(404, "Not Found (account uses invalid JIRA REST APIs)"), - tuple(408, "Request Timeout (request took too long to process)"), - tuple(500, "JIRA Server Error (internal error occurred while processing request)"), - tuple(666, "Unknown Error") + tuple(400, "Bad Request"), + tuple(401, "Unauthorized (authentication credentials are invalid)"), + tuple(403, "Forbidden (account doesn't have permission to create this issue)"), + tuple(404, "Not Found (account uses invalid JIRA REST APIs)"), + tuple(408, "Request Timeout (request took too long to process)"), + tuple(500, "JIRA Server Error (internal error occurred while processing request)"), + tuple(666, "Unknown Error") ); return error; } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/jira/JiraIssueTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/jira/JiraIssueTests.java index e794cba6805ad..73f9d5052979d 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/jira/JiraIssueTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/jira/JiraIssueTests.java @@ -8,14 +8,14 @@ import org.apache.http.HttpStatus; import org.elasticsearch.core.Tuple; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.watcher.support.xcontent.WatcherParams; +import org.elasticsearch.xpack.watcher.common.http.BasicAuth; import org.elasticsearch.xpack.watcher.common.http.HttpMethod; import org.elasticsearch.xpack.watcher.common.http.HttpRequest; import org.elasticsearch.xpack.watcher.common.http.HttpResponse; -import org.elasticsearch.xpack.watcher.common.http.BasicAuth; import java.util.HashMap; import java.util.Map; @@ -111,10 +111,10 @@ private static JiraIssue randomJiraIssue() { String account = "account_" + randomIntBetween(0, 100); Map fields = randomIssueDefaults(); HttpRequest request = HttpRequest.builder(randomFrom("localhost", "internal-jira.elastic.co"), randomFrom(80, 443)) - .method(HttpMethod.POST) - .path(JiraAccount.DEFAULT_PATH) - .auth(new BasicAuth(randomAlphaOfLength(5), randomAlphaOfLength(5).toCharArray())) - .build(); + .method(HttpMethod.POST) + .path(JiraAccount.DEFAULT_PATH) + .auth(new BasicAuth(randomAlphaOfLength(5), randomAlphaOfLength(5).toCharArray())) + .build(); if (rarely()) { Tuple error = randomHttpError(); return JiraIssue.responded(account, fields, request, new HttpResponse(error.v1(), "{\"error\": \"" + error.v2() + "\"}")); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/pagerduty/IncidentEventTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/pagerduty/IncidentEventTests.java index 97d9b44ee6327..a3df442249d49 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/pagerduty/IncidentEventTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/pagerduty/IncidentEventTests.java @@ -7,11 +7,11 @@ package org.elasticsearch.xpack.watcher.notification.pagerduty; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.rest.yaml.ObjectPath; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.test.rest.yaml.ObjectPath; import org.elasticsearch.xpack.core.watcher.watch.Payload; import org.elasticsearch.xpack.watcher.common.http.HttpProxy; @@ -62,8 +62,17 @@ public void testPagerDutyXContent() throws IOException { HttpProxy proxy = rarely() ? null : HttpProxy.NO_PROXY; - IncidentEvent event = new IncidentEvent(description, eventType, incidentKey, client, clientUrl, account, - attachPayload, contexts, proxy); + IncidentEvent event = new IncidentEvent( + description, + eventType, + incidentKey, + client, + clientUrl, + account, + attachPayload, + contexts, + proxy + ); XContentBuilder jsonBuilder = jsonBuilder(); jsonBuilder.startObject(); // since its a snippet @@ -75,19 +84,22 @@ public void testPagerDutyXContent() throws IOException { ObjectPath objectPath = ObjectPath.createFromXContent(jsonBuilder.contentType().xContent(), BytesReference.bytes(jsonBuilder)); String actualServiceKey = objectPath.evaluate(IncidentEvent.Fields.ROUTING_KEY.getPreferredName()); - String actualWatchId = objectPath.evaluate(IncidentEvent.Fields.PAYLOAD.getPreferredName() - + "." + IncidentEvent.Fields.SOURCE.getPreferredName()); + String actualWatchId = objectPath.evaluate( + IncidentEvent.Fields.PAYLOAD.getPreferredName() + "." + IncidentEvent.Fields.SOURCE.getPreferredName() + ); if (actualWatchId == null) { actualWatchId = "watcher"; // hardcoded if the SOURCE is null } - String actualDescription = objectPath.evaluate(IncidentEvent.Fields.PAYLOAD.getPreferredName() - + "." + IncidentEvent.Fields.SUMMARY.getPreferredName()); + String actualDescription = objectPath.evaluate( + IncidentEvent.Fields.PAYLOAD.getPreferredName() + "." + IncidentEvent.Fields.SUMMARY.getPreferredName() + ); String actualEventType = objectPath.evaluate(IncidentEvent.Fields.EVENT_ACTION.getPreferredName()); String actualIncidentKey = objectPath.evaluate(IncidentEvent.Fields.DEDUP_KEY.getPreferredName()); String actualClient = objectPath.evaluate(IncidentEvent.Fields.CLIENT.getPreferredName()); String actualClientUrl = objectPath.evaluate(IncidentEvent.Fields.CLIENT_URL.getPreferredName()); - String actualSeverity = objectPath.evaluate(IncidentEvent.Fields.PAYLOAD.getPreferredName() - + "." + IncidentEvent.Fields.SEVERITY.getPreferredName()); + String actualSeverity = objectPath.evaluate( + IncidentEvent.Fields.PAYLOAD.getPreferredName() + "." + IncidentEvent.Fields.SEVERITY.getPreferredName() + ); Map payloadDetails = objectPath.evaluate("payload.custom_details.payload"); Payload actualPayload = null; diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/pagerduty/PagerDutyAccountsTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/pagerduty/PagerDutyAccountsTests.java index 8d697c66d6fe9..467ea6c6f2499 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/pagerduty/PagerDutyAccountsTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/pagerduty/PagerDutyAccountsTests.java @@ -10,9 +10,9 @@ import org.elasticsearch.common.settings.ClusterSettings; import org.elasticsearch.common.settings.MockSecureSettings; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.rest.yaml.ObjectPath; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.watcher.watch.Payload; import org.elasticsearch.xpack.watcher.common.http.HttpClient; import org.elasticsearch.xpack.watcher.common.http.HttpProxy; @@ -42,8 +42,11 @@ public void init() throws Exception { public void testProxy() throws Exception { Settings.Builder builder = Settings.builder().put("xpack.notification.pagerduty.default_account", "account1"); addAccountSettings("account1", builder); - PagerDutyService service = new PagerDutyService(builder.build(), httpClient, new ClusterSettings(Settings.EMPTY, - new HashSet<>(PagerDutyService.getSettings()))); + PagerDutyService service = new PagerDutyService( + builder.build(), + httpClient, + new ClusterSettings(Settings.EMPTY, new HashSet<>(PagerDutyService.getSettings())) + ); PagerDutyAccount account = service.getAccount("account1"); ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass(HttpRequest.class); @@ -62,18 +65,23 @@ public void testProxy() throws Exception { public void testContextIsSentCorrect() throws Exception { Settings.Builder builder = Settings.builder().put("xpack.notification.pagerduty.default_account", "account1"); addAccountSettings("account1", builder); - PagerDutyService service = new PagerDutyService(builder.build(), httpClient, new ClusterSettings(Settings.EMPTY, - new HashSet<>(PagerDutyService.getSettings()))); + PagerDutyService service = new PagerDutyService( + builder.build(), + httpClient, + new ClusterSettings(Settings.EMPTY, new HashSet<>(PagerDutyService.getSettings())) + ); PagerDutyAccount account = service.getAccount("account1"); ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass(HttpRequest.class); when(httpClient.execute(argumentCaptor.capture())).thenReturn(new HttpResponse(200)); IncidentEventContext[] contexts = { - IncidentEventContext.link("https://www.elastic.co/products/x-pack/alerting", "Go to the Elastic.co Alerting website"), - IncidentEventContext.image("https://www.elastic.co/assets/blte5d899fd0b0e6808/icon-alerting-bb.svg", - "https://www.elastic.co/products/x-pack/alerting", "X-Pack-Alerting website link with log") - }; + IncidentEventContext.link("https://www.elastic.co/products/x-pack/alerting", "Go to the Elastic.co Alerting website"), + IncidentEventContext.image( + "https://www.elastic.co/assets/blte5d899fd0b0e6808/icon-alerting-bb.svg", + "https://www.elastic.co/products/x-pack/alerting", + "X-Pack-Alerting website link with log" + ) }; IncidentEvent event = new IncidentEvent("foo", null, null, null, null, account.getName(), true, contexts, HttpProxy.NO_PROXY); account.send(event, Payload.EMPTY, null); @@ -87,8 +95,9 @@ public void testContextIsSentCorrect() throws Exception { private void addAccountSettings(String name, Settings.Builder builder) { final MockSecureSettings secureSettings = new MockSecureSettings(); secureSettings.setString( - "xpack.notification.pagerduty.account." + name + "." + PagerDutyAccount.SECURE_SERVICE_API_KEY_SETTING.getKey(), - randomAlphaOfLength(50)); + "xpack.notification.pagerduty.account." + name + "." + PagerDutyAccount.SECURE_SERVICE_API_KEY_SETTING.getKey(), + randomAlphaOfLength(50) + ); builder.setSecureSettings(secureSettings); Settings defaults = SlackMessageDefaultsTests.randomSettings(); for (String setting : defaults.keySet()) { diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/pagerduty/SentEventTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/pagerduty/SentEventTests.java index 765350a1e3e85..dfe128d392e28 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/pagerduty/SentEventTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/pagerduty/SentEventTests.java @@ -7,12 +7,12 @@ package org.elasticsearch.xpack.watcher.notification.pagerduty; import org.elasticsearch.common.Strings; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.watcher.support.xcontent.WatcherParams; import org.elasticsearch.xpack.watcher.common.http.HttpRequest; import org.elasticsearch.xpack.watcher.common.http.HttpResponse; @@ -35,9 +35,11 @@ public void testToXContentBodyFiltering() throws Exception { sentEvent.toXContent(builder, params); assertThat(Strings.toString(builder), containsString(body)); - try (XContentParser parser = builder.contentType().xContent() - .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, - Strings.toString(builder))) { + try ( + XContentParser parser = builder.contentType() + .xContent() + .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, Strings.toString(builder)) + ) { parser.map(); } } @@ -45,9 +47,11 @@ public void testToXContentBodyFiltering() throws Exception { sentEvent.toXContent(builder, ToXContent.EMPTY_PARAMS); assertThat(Strings.toString(builder), not(containsString(body))); - try (XContentParser parser = builder.contentType().xContent() - .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, - Strings.toString(builder))) { + try ( + XContentParser parser = builder.contentType() + .xContent() + .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, Strings.toString(builder)) + ) { parser.map(); } } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/slack/message/SlackMessageTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/slack/message/SlackMessageTests.java index 3622d4285664b..6b1cf0b055743 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/slack/message/SlackMessageTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/slack/message/SlackMessageTests.java @@ -8,12 +8,12 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.watcher.support.xcontent.WatcherParams; import org.elasticsearch.xpack.watcher.common.http.HttpRequest; import org.elasticsearch.xpack.watcher.common.http.HttpResponse; @@ -60,7 +60,7 @@ public void testToXContent() throws Exception { String authorLink = authorName == null || randomBoolean() ? null : randomAlphaOfLength(10); String authorIcon = authorName == null || randomBoolean() ? null : randomAlphaOfLength(10); String title = randomBoolean() ? null : randomAlphaOfLength(10); - String titleLink = title == null ||randomBoolean() ? null : randomAlphaOfLength(10); + String titleLink = title == null || randomBoolean() ? null : randomAlphaOfLength(10); String attachmentText = randomBoolean() ? null : randomAlphaOfLength(10); Field[] fields = randomBoolean() ? null : new Field[randomIntBetween(0, 2)]; if (fields != null) { @@ -70,17 +70,31 @@ public void testToXContent() throws Exception { } String imageUrl = randomBoolean() ? null : randomAlphaOfLength(10); String thumbUrl = randomBoolean() ? null : randomAlphaOfLength(10); - String[] markdownFields = randomBoolean() ? null : new String[]{"pretext"}; + String[] markdownFields = randomBoolean() ? null : new String[] { "pretext" }; List actions = new ArrayList<>(); if (randomBoolean()) { actions.add(new Action("primary", "action_name", "button", "action_text", "https://elastic.co")); } - attachments[i] = new Attachment(fallback, color, pretext, authorName, authorLink, authorIcon, title, titleLink, - attachmentText, fields, imageUrl, thumbUrl, markdownFields, actions); + attachments[i] = new Attachment( + fallback, + color, + pretext, + authorName, + authorLink, + authorIcon, + title, + titleLink, + attachmentText, + fields, + imageUrl, + thumbUrl, + markdownFields, + actions + ); } } - SlackMessage expected = new SlackMessage(from, to, icon, text, attachments); + SlackMessage expected = new SlackMessage(from, to, icon, text, attachments); boolean includeTarget = randomBoolean(); @@ -262,11 +276,27 @@ public void testToXContent() throws Exception { while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { data.add(parser.text()); } - markdownSupportedFields = data.toArray(new String[]{}); + markdownSupportedFields = data.toArray(new String[] {}); } } - list.add(new Attachment(fallback, color, pretext, authorName, authorLink, authorIcon, title, titleLink, - attachmentText, fields, imageUrl, thumbUrl, markdownSupportedFields, actions)); + list.add( + new Attachment( + fallback, + color, + pretext, + authorName, + authorLink, + authorIcon, + title, + titleLink, + attachmentText, + fields, + imageUrl, + thumbUrl, + markdownSupportedFields, + actions + ) + ); } attachments = list.toArray(new Attachment[list.size()]); } @@ -376,7 +406,7 @@ public void testTemplateParse() throws Exception { Field.Template[] fields = null; if (randomBoolean()) { jsonBuilder.startArray("fields"); - fields = new Field.Template[randomIntBetween(1,3)]; + fields = new Field.Template[randomIntBetween(1, 3)]; for (int j = 0; j < fields.length; j++) { jsonBuilder.startObject(); TextTemplate fieldTitle = new TextTemplate(randomAlphaOfLength(50)); @@ -418,8 +448,22 @@ public void testTemplateParse() throws Exception { actions.add(action); } jsonBuilder.endObject(); - attachments[i] = new Attachment.Template(fallback, color, pretext, authorName, authorLink, authorIcon, title, - titleLink, attachmentText, fields, imageUrl, thumbUrl, markdownSupportedFields, actions); + attachments[i] = new Attachment.Template( + fallback, + color, + pretext, + authorName, + authorLink, + authorIcon, + title, + titleLink, + attachmentText, + fields, + imageUrl, + thumbUrl, + markdownSupportedFields, + actions + ); } jsonBuilder.endArray(); } @@ -521,40 +565,68 @@ public void testTemplateRender() throws Exception { for (int i = 0; i < template.attachments.length; i++) { Attachment.Template attachmentTemplate = template.attachments[i]; Attachment attachment = message.attachments[i]; - assertThat(attachment.authorName, is(attachmentTemplate.authorName != null ? attachmentTemplate.authorName.getTemplate() - : defaults.attachment.authorName)); - assertThat(attachment.authorLink, is(attachmentTemplate.authorLink != null ? attachmentTemplate.authorLink.getTemplate() - : defaults.attachment.authorLink)); - assertThat(attachment.authorIcon, is(attachmentTemplate.authorIcon != null ? attachmentTemplate.authorIcon.getTemplate() - : defaults.attachment.authorIcon)); - assertThat(attachment.color, is(attachmentTemplate.color != null ? attachmentTemplate.color.getTemplate() - : defaults.attachment.color)); - assertThat(attachment.fallback, is(attachmentTemplate.fallback != null ? attachmentTemplate.fallback.getTemplate() - : defaults.attachment.fallback)); - assertThat(attachment.imageUrl, is(attachmentTemplate.imageUrl != null ? attachmentTemplate.imageUrl.getTemplate() - : defaults.attachment.imageUrl)); - assertThat(attachment.pretext, is(attachmentTemplate.pretext != null ? attachmentTemplate.pretext.getTemplate() - : defaults.attachment.pretext)); - assertThat(attachment.thumbUrl, is(attachmentTemplate.thumbUrl != null ? attachmentTemplate.thumbUrl.getTemplate() - : defaults.attachment.thumbUrl)); - assertThat(attachment.title, is(attachmentTemplate.title != null ? attachmentTemplate.title.getTemplate() - : defaults.attachment.title)); - assertThat(attachment.titleLink, is(attachmentTemplate.titleLink != null ? attachmentTemplate.titleLink.getTemplate() - : defaults.attachment.titleLink)); - assertThat(attachment.text, is(attachmentTemplate.text != null ? attachmentTemplate.text.getTemplate() - : defaults.attachment.text)); + assertThat( + attachment.authorName, + is(attachmentTemplate.authorName != null ? attachmentTemplate.authorName.getTemplate() : defaults.attachment.authorName) + ); + assertThat( + attachment.authorLink, + is(attachmentTemplate.authorLink != null ? attachmentTemplate.authorLink.getTemplate() : defaults.attachment.authorLink) + ); + assertThat( + attachment.authorIcon, + is(attachmentTemplate.authorIcon != null ? attachmentTemplate.authorIcon.getTemplate() : defaults.attachment.authorIcon) + ); + assertThat( + attachment.color, + is(attachmentTemplate.color != null ? attachmentTemplate.color.getTemplate() : defaults.attachment.color) + ); + assertThat( + attachment.fallback, + is(attachmentTemplate.fallback != null ? attachmentTemplate.fallback.getTemplate() : defaults.attachment.fallback) + ); + assertThat( + attachment.imageUrl, + is(attachmentTemplate.imageUrl != null ? attachmentTemplate.imageUrl.getTemplate() : defaults.attachment.imageUrl) + ); + assertThat( + attachment.pretext, + is(attachmentTemplate.pretext != null ? attachmentTemplate.pretext.getTemplate() : defaults.attachment.pretext) + ); + assertThat( + attachment.thumbUrl, + is(attachmentTemplate.thumbUrl != null ? attachmentTemplate.thumbUrl.getTemplate() : defaults.attachment.thumbUrl) + ); + assertThat( + attachment.title, + is(attachmentTemplate.title != null ? attachmentTemplate.title.getTemplate() : defaults.attachment.title) + ); + assertThat( + attachment.titleLink, + is(attachmentTemplate.titleLink != null ? attachmentTemplate.titleLink.getTemplate() : defaults.attachment.titleLink) + ); + assertThat( + attachment.text, + is(attachmentTemplate.text != null ? attachmentTemplate.text.getTemplate() : defaults.attachment.text) + ); if (attachmentTemplate.fields == null) { assertThat(attachment.fields, nullValue()); } else { for (int j = 0; j < attachmentTemplate.fields.length; j++) { Field.Template fieldTemplate = attachmentTemplate.fields[j]; Field field = attachment.fields[j]; - assertThat(field.title, - is(fieldTemplate.title != null ? fieldTemplate.title.getTemplate(): defaults.attachment.field.title)); - assertThat(field.value, - is(fieldTemplate.value != null ? fieldTemplate.value.getTemplate() : defaults.attachment.field.value)); - assertThat(field.isShort, - is(fieldTemplate.isShort != null ? fieldTemplate.isShort : defaults.attachment.field.isShort)); + assertThat( + field.title, + is(fieldTemplate.title != null ? fieldTemplate.title.getTemplate() : defaults.attachment.field.title) + ); + assertThat( + field.value, + is(fieldTemplate.value != null ? fieldTemplate.value.getTemplate() : defaults.attachment.field.value) + ); + assertThat( + field.isShort, + is(fieldTemplate.isShort != null ? fieldTemplate.isShort : defaults.attachment.field.isShort) + ); } } if (attachmentTemplate.markdownSupportedFields == null) { @@ -562,7 +634,8 @@ public void testTemplateRender() throws Exception { } else { for (int j = 0; j < attachmentTemplate.markdownSupportedFields.length; j++) { String[] templateMarkdownSupportFields = Arrays.stream(attachmentTemplate.markdownSupportedFields) - .map(TextTemplate::getTemplate).toArray(String[]::new); + .map(TextTemplate::getTemplate) + .toArray(String[]::new); assertThat(attachment.markdownSupportedFields, arrayContainingInAnyOrder(templateMarkdownSupportFields)); } @@ -576,18 +649,22 @@ public void testUrlPathIsFiltered() throws Exception { HttpResponse response = new HttpResponse(500); String path = randomAlphaOfLength(20); HttpRequest request = HttpRequest.builder("localhost", 1234).path(path).build(); - SlackMessage slackMessage = new SlackMessage("from", new String[] {"to"}, "icon", "text", null); - SentMessages sentMessages = new SentMessages("foo", - Arrays.asList(SentMessages.SentMessage.responded("recipient", slackMessage, request, response))); + SlackMessage slackMessage = new SlackMessage("from", new String[] { "to" }, "icon", "text", null); + SentMessages sentMessages = new SentMessages( + "foo", + Arrays.asList(SentMessages.SentMessage.responded("recipient", slackMessage, request, response)) + ); try (XContentBuilder builder = jsonBuilder()) { WatcherParams params = WatcherParams.builder().hideSecrets(false).build(); sentMessages.toXContent(builder, params); assertThat(Strings.toString(builder), containsString(path)); - try (XContentParser parser = builder.contentType().xContent() - .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, - Strings.toString(builder))) { + try ( + XContentParser parser = builder.contentType() + .xContent() + .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, Strings.toString(builder)) + ) { parser.map(); } } @@ -595,28 +672,30 @@ public void testUrlPathIsFiltered() throws Exception { sentMessages.toXContent(builder, ToXContent.EMPTY_PARAMS); assertThat(Strings.toString(builder), not(containsString(path))); - try (XContentParser parser = builder.contentType().xContent() - .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, - Strings.toString(builder))) { + try ( + XContentParser parser = builder.contentType() + .xContent() + .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, Strings.toString(builder)) + ) { parser.map(); } } } - public void testCanHaveNullText() throws Exception { - SlackMessage slackMessage = new SlackMessage("from", new String[] {"to"}, "icon", null, new Attachment[1]); + public void testCanHaveNullText() throws Exception { + SlackMessage slackMessage = new SlackMessage("from", new String[] { "to" }, "icon", null, new Attachment[1]); assertNull(slackMessage.getText()); assertNotNull(slackMessage.getAttachments()); } - public void testCanHaveNullAttachments() throws Exception { - SlackMessage slackMessage = new SlackMessage("from", new String[] {"to"}, "icon", "text", null); + public void testCanHaveNullAttachments() throws Exception { + SlackMessage slackMessage = new SlackMessage("from", new String[] { "to" }, "icon", "text", null); assertNotNull(slackMessage.getText()); assertNull(slackMessage.getAttachments()); } public void testCannotHaveNullAttachmentsAndNullText() throws Exception { - expectThrows(IllegalArgumentException.class, () -> new SlackMessage("from", new String[]{"to"}, "icon", null, null)); + expectThrows(IllegalArgumentException.class, () -> new SlackMessage("from", new String[] { "to" }, "icon", null, null)); } private static void writeFieldIfNotNull(XContentBuilder builder, String field, Object value) throws IOException { diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/rest/action/RestExecuteWatchActionTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/rest/action/RestExecuteWatchActionTests.java index 44f5f9bd10921..3b4513fd45a90 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/rest/action/RestExecuteWatchActionTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/rest/action/RestExecuteWatchActionTests.java @@ -8,11 +8,11 @@ import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.common.bytes.BytesArray; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.rest.FakeRestRequest; import org.elasticsearch.test.rest.FakeRestRequest.Builder; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.watcher.transport.actions.execute.ExecuteWatchRequest; import java.util.Arrays; @@ -33,7 +33,9 @@ public void testThatFlagsCanBeSpecifiedViaParameters() throws Exception { for (String debugCondition : Arrays.asList("true", "false", null)) { ExecuteWatchRequest request = RestExecuteWatchAction.parseRequest( - createFakeRestRequest(randomId, recordExecution, ignoreCondition, debugCondition), client); + createFakeRestRequest(randomId, recordExecution, ignoreCondition, debugCondition), + client + ); assertThat(request.getId(), is(randomId)); assertThat(request.isRecordExecution(), is(Boolean.parseBoolean(recordExecution))); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/FilterXContentTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/FilterXContentTests.java index e1b2ecf332425..1870f3470b41d 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/FilterXContentTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/FilterXContentTests.java @@ -7,10 +7,10 @@ package org.elasticsearch.xpack.watcher.support; import org.elasticsearch.common.collect.MapBuilder; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ObjectPath; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.hamcrest.Matchers; import java.io.IOException; @@ -120,10 +120,22 @@ public void testNestedPayloadFiltering() throws Exception { // issue #852 public void testArraysAreNotCutOff() throws Exception { - XContentBuilder builder = jsonBuilder().startObject().startArray("buckets") - .startObject().startObject("foo").startObject("values").endObject().endObject().endObject() - .startObject().startObject("foo").startObject("values").endObject().endObject().endObject() - .endArray().endObject(); + XContentBuilder builder = jsonBuilder().startObject() + .startArray("buckets") + .startObject() + .startObject("foo") + .startObject("values") + .endObject() + .endObject() + .endObject() + .startObject() + .startObject("foo") + .startObject("values") + .endObject() + .endObject() + .endObject() + .endArray() + .endObject(); XContentParser parser = createParser(builder); @@ -143,18 +155,32 @@ public void testArraysAreNotCutOff() throws Exception { // issue #4614 public void testNestedArraysWork() throws Exception { - XContentBuilder builder = jsonBuilder().startObject().startArray("buckets") - .startObject().startObject("foo").field("spam", "eggs").endObject().endObject() - .startObject().startObject("foo").field("spam", "eggs2").endObject().endObject() - .startObject().startObject("foo").field("spam", "eggs3").endObject().endObject() - .endArray().endObject(); + XContentBuilder builder = jsonBuilder().startObject() + .startArray("buckets") + .startObject() + .startObject("foo") + .field("spam", "eggs") + .endObject() + .endObject() + .startObject() + .startObject("foo") + .field("spam", "eggs2") + .endObject() + .endObject() + .startObject() + .startObject("foo") + .field("spam", "eggs3") + .endObject() + .endObject() + .endArray() + .endObject(); XContentParser parser = createParser(builder); assertArrayValues(parser, "buckets.foo.spam", "eggs", "eggs2", "eggs3"); } - private void assertArrayValues(XContentParser parser, String key, Object ... expectedValues) throws IOException { + private void assertArrayValues(XContentParser parser, String key, Object... expectedValues) throws IOException { Set keys = new HashSet<>(); keys.add(key); Map filteredData = XContentFilterKeysUtils.filterMapOrdered(keys, parser); @@ -173,11 +199,25 @@ private void assertArrayValues(XContentParser parser, String key, Object ... exp } public void testNestedArraysWorkWithNumbers() throws Exception { - XContentBuilder builder = jsonBuilder().startObject().startArray("buckets") - .startObject().startObject("foo").field("spam", 0).endObject().endObject() - .startObject().startObject("foo").field("spam", 1).endObject().endObject() - .startObject().startObject("foo").field("spam", 2).endObject().endObject() - .endArray().endObject(); + XContentBuilder builder = jsonBuilder().startObject() + .startArray("buckets") + .startObject() + .startObject("foo") + .field("spam", 0) + .endObject() + .endObject() + .startObject() + .startObject("foo") + .field("spam", 1) + .endObject() + .endObject() + .startObject() + .startObject("foo") + .field("spam", 2) + .endObject() + .endObject() + .endArray() + .endObject(); XContentParser parser = createParser(builder); @@ -187,11 +227,25 @@ public void testNestedArraysWorkWithNumbers() throws Exception { public void testNestedArraysWorkWithBooleans() throws Exception { boolean[] bools = new boolean[] { randomBoolean(), randomBoolean(), randomBoolean() }; - XContentBuilder builder = jsonBuilder().startObject().startArray("buckets") - .startObject().startObject("foo").field("spam", bools[0]).endObject().endObject() - .startObject().startObject("foo").field("spam", bools[1]).endObject().endObject() - .startObject().startObject("foo").field("spam", bools[2]).endObject().endObject() - .endArray().endObject(); + XContentBuilder builder = jsonBuilder().startObject() + .startArray("buckets") + .startObject() + .startObject("foo") + .field("spam", bools[0]) + .endObject() + .endObject() + .startObject() + .startObject("foo") + .field("spam", bools[1]) + .endObject() + .endObject() + .startObject() + .startObject("foo") + .field("spam", bools[2]) + .endObject() + .endObject() + .endArray() + .endObject(); XContentParser parser = createParser(builder); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/VariablesTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/VariablesTests.java index 23644a75da89e..8b0276b94e254 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/VariablesTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/VariablesTests.java @@ -6,8 +6,8 @@ */ package org.elasticsearch.xpack.watcher.support; -import org.elasticsearch.xcontent.ObjectPath; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.ObjectPath; import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext; import org.elasticsearch.xpack.core.watcher.execution.Wid; import org.elasticsearch.xpack.core.watcher.trigger.TriggerEvent; @@ -35,12 +35,12 @@ public void testCreateCtxModel() throws Exception { TriggerEvent event = new ScheduleTriggerEvent("_watch_id", triggeredTime, scheduledTime); Wid wid = new Wid("_watch_id", executionTime); WatchExecutionContext ctx = WatcherTestUtils.mockExecutionContextBuilder("_watch_id") - .wid(wid) - .executionTime(executionTime) - .triggerEvent(event) - .payload(payload) - .metadata(metatdata) - .buildMock(); + .wid(wid) + .executionTime(executionTime) + .triggerEvent(event) + .payload(payload) + .metadata(metatdata) + .buildMock(); Map model = Variables.createCtxParamsMap(ctx, payload); assertThat(model, notNullValue()); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherDateTimeUtilsTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherDateTimeUtilsTests.java index 3fe0966fea62f..8402201ffab12 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherDateTimeUtilsTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherDateTimeUtilsTests.java @@ -6,12 +6,11 @@ */ package org.elasticsearch.xpack.watcher.support; - import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.watcher.support.WatcherDateTimeUtils; import java.util.HashMap; @@ -49,8 +48,12 @@ public void testParseTimeValueNumeric() throws Exception { WatcherDateTimeUtils.parseTimeValue(parser, "test"); fail("Expected ElasticsearchParseException"); } catch (ElasticsearchParseException e) { - assertThat(e.getMessage(), either(is("failed to parse time unit")) - .or(is("could not parse time value. expected either a string or a null value but found [VALUE_NUMBER] instead"))); + assertThat( + e.getMessage(), + either(is("failed to parse time unit")).or( + is("could not parse time value. expected either a string or a null value but found [VALUE_NUMBER] instead") + ) + ); } } @@ -66,8 +69,10 @@ public void testParseTimeValueNumericNegative() throws Exception { WatcherDateTimeUtils.parseTimeValue(parser, "test"); fail("Expected ElasticsearchParseException"); } catch (ElasticsearchParseException e) { - assertThat(e.getMessage(), - is("could not parse time value. expected either a string or a null value but found [VALUE_NUMBER] instead")); + assertThat( + e.getMessage(), + is("could not parse time value. expected either a string or a null value but found [VALUE_NUMBER] instead") + ); } } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherIndexTemplateRegistryTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherIndexTemplateRegistryTests.java index c6618d3d66e21..11f40968bd6ab 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherIndexTemplateRegistryTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherIndexTemplateRegistryTests.java @@ -25,17 +25,17 @@ import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.node.DiscoveryNodes; import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.collect.ImmutableOpenMap; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.EsExecutors; import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.xpack.core.ilm.DeleteAction; import org.elasticsearch.xpack.core.ilm.IndexLifecycleMetadata; import org.elasticsearch.xpack.core.ilm.LifecycleAction; @@ -59,8 +59,6 @@ import java.util.Map; import java.util.stream.Collectors; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; import static org.elasticsearch.xpack.core.watcher.support.WatcherIndexTemplateRegistryField.INDEX_TEMPLATE_VERSION; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasSize; @@ -73,7 +71,9 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyZeroInteractions; +import static org.mockito.Mockito.when; public class WatcherIndexTemplateRegistryTests extends ESTestCase { @@ -97,18 +97,23 @@ public void createRegistryAndClient() { when(adminClient.indices()).thenReturn(indicesAdminClient); when(client.admin()).thenReturn(adminClient); doAnswer(invocationOnMock -> { - ActionListener listener = - (ActionListener) invocationOnMock.getArguments()[1]; + ActionListener listener = (ActionListener) invocationOnMock.getArguments()[1]; listener.onResponse(new TestPutIndexTemplateResponse(true)); return null; }).when(indicesAdminClient).putTemplate(any(PutIndexTemplateRequest.class), any(ActionListener.class)); clusterService = mock(ClusterService.class); List entries = new ArrayList<>(ClusterModule.getNamedXWriteables()); - entries.addAll(Arrays.asList( - new NamedXContentRegistry.Entry(LifecycleType.class, new ParseField(TimeseriesLifecycleType.TYPE), - (p) -> TimeseriesLifecycleType.INSTANCE), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse))); + entries.addAll( + Arrays.asList( + new NamedXContentRegistry.Entry( + LifecycleType.class, + new ParseField(TimeseriesLifecycleType.TYPE), + (p) -> TimeseriesLifecycleType.INSTANCE + ), + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse) + ) + ); xContentRegistry = new NamedXContentRegistry(entries); registry = new WatcherIndexTemplateRegistry(Settings.EMPTY, clusterService, threadPool, client, xContentRegistry); } @@ -119,8 +124,9 @@ public void testThatNonExistingTemplatesAreAddedImmediately() { ClusterChangedEvent event = createClusterChangedEvent(Collections.emptyMap(), nodes); registry.clusterChanged(event); - ArgumentCaptor argumentCaptor = - ArgumentCaptor.forClass(PutComposableIndexTemplateAction.Request.class); + ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass( + PutComposableIndexTemplateAction.Request.class + ); verify(client, times(1)).execute(same(PutComposableIndexTemplateAction.INSTANCE), argumentCaptor.capture(), anyObject()); // now delete one template from the cluster state and lets retry @@ -130,7 +136,8 @@ public void testThatNonExistingTemplatesAreAddedImmediately() { registry.clusterChanged(newEvent); argumentCaptor = ArgumentCaptor.forClass(PutComposableIndexTemplateAction.Request.class); verify(client, times(1)).execute(same(PutComposableIndexTemplateAction.INSTANCE), argumentCaptor.capture(), anyObject()); - PutComposableIndexTemplateAction.Request req = argumentCaptor.getAllValues().stream() + PutComposableIndexTemplateAction.Request req = argumentCaptor.getAllValues() + .stream() .filter(r -> r.name().equals(WatcherIndexTemplateRegistryField.HISTORY_TEMPLATE_NAME)) .findFirst() .orElseThrow(() -> new AssertionError("expected the watch history template to be put")); @@ -141,13 +148,18 @@ public void testThatNonExistingTemplatesAreAddedEvenWithILMUsageDisabled() { DiscoveryNode node = new DiscoveryNode("node", ESTestCase.buildNewFakeTransportAddress(), Version.CURRENT); DiscoveryNodes nodes = DiscoveryNodes.builder().localNodeId("node").masterNodeId("node").add(node).build(); - registry = new WatcherIndexTemplateRegistry(Settings.builder() - .put(Watcher.USE_ILM_INDEX_MANAGEMENT.getKey(), false).build(), - clusterService, threadPool, client, xContentRegistry); + registry = new WatcherIndexTemplateRegistry( + Settings.builder().put(Watcher.USE_ILM_INDEX_MANAGEMENT.getKey(), false).build(), + clusterService, + threadPool, + client, + xContentRegistry + ); ClusterChangedEvent event = createClusterChangedEvent(Settings.EMPTY, Collections.emptyMap(), Collections.emptyMap(), nodes); registry.clusterChanged(event); - ArgumentCaptor argumentCaptor = - ArgumentCaptor.forClass(PutComposableIndexTemplateAction.Request.class); + ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass( + PutComposableIndexTemplateAction.Request.class + ); verify(client, times(1)).execute(same(PutComposableIndexTemplateAction.INSTANCE), argumentCaptor.capture(), anyObject()); // now delete one template from the cluster state and lets retry @@ -175,7 +187,8 @@ public void testPolicyAlreadyExists() { DiscoveryNodes nodes = DiscoveryNodes.builder().localNodeId("node").masterNodeId("node").add(node).build(); Map policyMap = new HashMap<>(); - List policies = registry.getPolicyConfigs().stream() + List policies = registry.getPolicyConfigs() + .stream() .map(policyConfig -> policyConfig.load(xContentRegistry)) .collect(Collectors.toList()); assertThat(policies, hasSize(1)); @@ -190,9 +203,13 @@ public void testNoPolicyButILMDisabled() { DiscoveryNode node = new DiscoveryNode("node", ESTestCase.buildNewFakeTransportAddress(), Version.CURRENT); DiscoveryNodes nodes = DiscoveryNodes.builder().localNodeId("node").masterNodeId("node").add(node).build(); - registry = new WatcherIndexTemplateRegistry(Settings.builder() - .put(Watcher.USE_ILM_INDEX_MANAGEMENT.getKey(), false).build(), - clusterService, threadPool, client, xContentRegistry); + registry = new WatcherIndexTemplateRegistry( + Settings.builder().put(Watcher.USE_ILM_INDEX_MANAGEMENT.getKey(), false).build(), + clusterService, + threadPool, + client, + xContentRegistry + ); ClusterChangedEvent event = createClusterChangedEvent(Settings.EMPTY, Collections.emptyMap(), Collections.emptyMap(), nodes); registry.clusterChanged(event); verify(client, times(0)).execute(eq(PutLifecycleAction.INSTANCE), anyObject(), anyObject()); @@ -204,13 +221,16 @@ public void testPolicyAlreadyExistsButDiffers() throws IOException { Map policyMap = new HashMap<>(); String policyStr = "{\"phases\":{\"delete\":{\"min_age\":\"1m\",\"actions\":{\"delete\":{}}}}}"; - List policies = registry.getPolicyConfigs().stream() + List policies = registry.getPolicyConfigs() + .stream() .map(policyConfig -> policyConfig.load(xContentRegistry)) .collect(Collectors.toList()); assertThat(policies, hasSize(1)); LifecyclePolicy policy = policies.get(0); - try (XContentParser parser = XContentType.JSON.xContent() - .createParser(xContentRegistry, LoggingDeprecationHandler.THROW_UNSUPPORTED_OPERATION, policyStr)) { + try ( + XContentParser parser = XContentType.JSON.xContent() + .createParser(xContentRegistry, LoggingDeprecationHandler.THROW_UNSUPPORTED_OPERATION, policyStr) + ) { LifecyclePolicy different = LifecyclePolicy.parse(parser, policy.getName()); policyMap.put(policy.getName(), different); ClusterChangedEvent event = createClusterChangedEvent(Collections.emptyMap(), policyMap, nodes); @@ -282,10 +302,12 @@ private ClusterChangedEvent createClusterChangedEvent(Map exist return createClusterChangedEvent(existingTemplateNames, Collections.emptyMap(), nodes); } - private ClusterState createClusterState(Settings nodeSettings, - Map existingTemplates, - Map existingPolicies, - DiscoveryNodes nodes) { + private ClusterState createClusterState( + Settings nodeSettings, + Map existingTemplates, + Map existingPolicies, + DiscoveryNodes nodes + ) { ImmutableOpenMap.Builder indexTemplates = ImmutableOpenMap.builder(); for (Map.Entry template : existingTemplates.entrySet()) { final IndexTemplateMetadata mockTemplate = mock(IndexTemplateMetadata.class); @@ -294,34 +316,44 @@ private ClusterState createClusterState(Settings nodeSettings, indexTemplates.put(template.getKey(), mockTemplate); } - Map existingILMMeta = existingPolicies.entrySet().stream() + Map existingILMMeta = existingPolicies.entrySet() + .stream() .collect(Collectors.toMap(Map.Entry::getKey, e -> new LifecyclePolicyMetadata(e.getValue(), Collections.emptyMap(), 1, 1))); IndexLifecycleMetadata ilmMeta = new IndexLifecycleMetadata(existingILMMeta, OperationMode.RUNNING); return ClusterState.builder(new ClusterName("test")) - .metadata(Metadata.builder() - .templates(indexTemplates.build()) - .transientSettings(nodeSettings) - .putCustom(IndexLifecycleMetadata.TYPE, ilmMeta) - .build()) + .metadata( + Metadata.builder() + .templates(indexTemplates.build()) + .transientSettings(nodeSettings) + .putCustom(IndexLifecycleMetadata.TYPE, ilmMeta) + .build() + ) .blocks(new ClusterBlocks.Builder().build()) .nodes(nodes) .build(); } - private ClusterChangedEvent createClusterChangedEvent(Map existingTemplateNames, - Map existingPolicies, - DiscoveryNodes nodes) { + private ClusterChangedEvent createClusterChangedEvent( + Map existingTemplateNames, + Map existingPolicies, + DiscoveryNodes nodes + ) { return createClusterChangedEvent(Settings.EMPTY, existingTemplateNames, existingPolicies, nodes); } - private ClusterChangedEvent createClusterChangedEvent(Settings nodeSettings, - Map existingTemplates, - Map existingPolicies, - DiscoveryNodes nodes) { + private ClusterChangedEvent createClusterChangedEvent( + Settings nodeSettings, + Map existingTemplates, + Map existingPolicies, + DiscoveryNodes nodes + ) { ClusterState cs = createClusterState(nodeSettings, existingTemplates, existingPolicies, nodes); - ClusterChangedEvent realEvent = new ClusterChangedEvent("created-from-test", cs, - ClusterState.builder(new ClusterName("test")).build()); + ClusterChangedEvent realEvent = new ClusterChangedEvent( + "created-from-test", + cs, + ClusterState.builder(new ClusterName("test")).build() + ); ClusterChangedEvent event = spy(realEvent); when(event.localNodeMaster()).thenReturn(nodes.isLocalNodeElectedMaster()); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherTemplateTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherTemplateTests.java index 586157fff6d88..0287b5af5d636 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherTemplateTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherTemplateTests.java @@ -7,14 +7,15 @@ package org.elasticsearch.xpack.watcher.support; import com.fasterxml.jackson.core.io.JsonStringEncoder; -import org.elasticsearch.core.Nullable; + import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.core.Nullable; import org.elasticsearch.script.ScriptContext; import org.elasticsearch.script.ScriptEngine; import org.elasticsearch.script.ScriptService; import org.elasticsearch.script.mustache.MustacheScriptEngine; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.watcher.Watcher; import org.elasticsearch.xpack.watcher.common.text.TextTemplate; import org.elasticsearch.xpack.watcher.common.text.TextTemplateEngine; @@ -39,8 +40,10 @@ public class WatcherTemplateTests extends ESTestCase { public void init() throws Exception { MustacheScriptEngine engine = new MustacheScriptEngine(); Map engines = Collections.singletonMap(engine.getType(), engine); - Map> contexts = - Collections.singletonMap(Watcher.SCRIPT_TEMPLATE_CONTEXT.name, Watcher.SCRIPT_TEMPLATE_CONTEXT); + Map> contexts = Collections.singletonMap( + Watcher.SCRIPT_TEMPLATE_CONTEXT.name, + Watcher.SCRIPT_TEMPLATE_CONTEXT + ); ScriptService scriptService = new ScriptService(Settings.EMPTY, engines, contexts); textTemplateEngine = new TextTemplateEngine(scriptService); } @@ -50,12 +53,12 @@ public void testEscaping() throws Exception { if (rarely()) { contentType = null; } - Character[] specialChars = new Character[]{'\f', '\n', '\r', '"', '\\', (char) 11, '\t', '\b' }; + Character[] specialChars = new Character[] { '\f', '\n', '\r', '"', '\\', (char) 11, '\t', '\b' }; int iters = scaledRandomIntBetween(100, 1000); for (int i = 0; i < iters; i++) { int rounds = scaledRandomIntBetween(1, 20); - StringWriter escaped = new StringWriter(); //This will be escaped as it is constructed - StringWriter unescaped = new StringWriter(); //This will be escaped at the end + StringWriter escaped = new StringWriter(); // This will be escaped as it is constructed + StringWriter unescaped = new StringWriter(); // This will be escaped at the end for (int j = 0; j < rounds; j++) { String s = getChars(); @@ -78,8 +81,7 @@ public void testEscaping() throws Exception { if (contentType == XContentType.JSON) { assertThat(escaped.toString(), equalTo(new String(JsonStringEncoder.getInstance().quoteAsString(unescaped.toString())))); - } - else { + } else { assertThat(escaped.toString(), equalTo(unescaped.toString())); } @@ -108,37 +110,47 @@ public void testEscaping() throws Exception { public void testSimpleParameterReplace() { { - String template = "__json__::GET _search {\"query\": " + "{\"boosting\": {" + "\"positive\": {\"match\": {\"body\": \"gift\"}}," - + "\"negative\": {\"term\": {\"body\": {\"value\": \"solr\"}" + "}}, \"negative_boost\": {{boost_val}} } }}"; + String template = "__json__::GET _search {\"query\": " + + "{\"boosting\": {" + + "\"positive\": {\"match\": {\"body\": \"gift\"}}," + + "\"negative\": {\"term\": {\"body\": {\"value\": \"solr\"}" + + "}}, \"negative_boost\": {{boost_val}} } }}"; Map vars = new HashMap<>(); vars.put("boost_val", "0.3"); String result = textTemplateEngine.render(new TextTemplate(template), vars); - assertEquals("GET _search {\"query\": {\"boosting\": {\"positive\": {\"match\": {\"body\": \"gift\"}}," - + "\"negative\": {\"term\": {\"body\": {\"value\": \"solr\"}}}, \"negative_boost\": 0.3 } }}", - result); + assertEquals( + "GET _search {\"query\": {\"boosting\": {\"positive\": {\"match\": {\"body\": \"gift\"}}," + + "\"negative\": {\"term\": {\"body\": {\"value\": \"solr\"}}}, \"negative_boost\": 0.3 } }}", + result + ); } { - String template = "__json__::GET _search {\"query\": " + "{\"boosting\": {" + "\"positive\": {\"match\": {\"body\": \"gift\"}}," - + "\"negative\": {\"term\": {\"body\": {\"value\": \"{{body_val}}\"}" + "}}, \"negative_boost\": {{boost_val}} } }}"; + String template = "__json__::GET _search {\"query\": " + + "{\"boosting\": {" + + "\"positive\": {\"match\": {\"body\": \"gift\"}}," + + "\"negative\": {\"term\": {\"body\": {\"value\": \"{{body_val}}\"}" + + "}}, \"negative_boost\": {{boost_val}} } }}"; Map vars = new HashMap<>(); vars.put("boost_val", "0.3"); vars.put("body_val", "\"quick brown\""); String result = textTemplateEngine.render(new TextTemplate(template), vars); - assertEquals("GET _search {\"query\": {\"boosting\": {\"positive\": {\"match\": {\"body\": \"gift\"}}," - + "\"negative\": {\"term\": {\"body\": {\"value\": \"\\\"quick brown\\\"\"}}}, \"negative_boost\": 0.3 } }}", - result); + assertEquals( + "GET _search {\"query\": {\"boosting\": {\"positive\": {\"match\": {\"body\": \"gift\"}}," + + "\"negative\": {\"term\": {\"body\": {\"value\": \"\\\"quick brown\\\"\"}}}, \"negative_boost\": 0.3 } }}", + result + ); } } public void testInvalidPrefixes() throws Exception { - String[] specialStrings = new String[]{"\f", "\n", "\r", "\"", "\\", "\t", "\b", "__::", "__" }; + String[] specialStrings = new String[] { "\f", "\n", "\r", "\"", "\\", "\t", "\b", "__::", "__" }; String prefix = randomFrom("", "__", "____::", "___::", "____", "::", "++json__::", "__json__", "+_json__::", "__json__:"); String template = prefix + " {{test_var1}} {{test_var2}}"; Map vars = new HashMap<>(); Writer var1Writer = new StringWriter(); Writer var2Writer = new StringWriter(); - for(int i = 0; i < scaledRandomIntBetween(10,1000); ++i) { + for (int i = 0; i < scaledRandomIntBetween(10, 1000); ++i) { var1Writer.write(randomRealisticUnicodeOfCodepointLengthBetween(0, 10)); var2Writer.write(randomRealisticUnicodeOfCodepointLengthBetween(0, 10)); var1Writer.append(randomFrom(specialStrings)); @@ -148,7 +160,7 @@ public void testInvalidPrefixes() throws Exception { vars.put("test_var1", var1Writer.toString()); vars.put("test_var2", var2Writer.toString()); String s1 = textTemplateEngine.render(new TextTemplate(template), vars); - String s2 = prefix + " " + var1Writer.toString() + " " + var2Writer.toString(); + String s2 = prefix + " " + var1Writer.toString() + " " + var2Writer.toString(); assertThat(s1, equalTo(s2)); } @@ -160,11 +172,10 @@ static String prepareTemplate(String template, @Nullable XContentType contentTyp if (contentType == null) { return template; } - return new StringBuilder("__") - .append(contentType.queryParameter().toLowerCase(Locale.ROOT)) - .append("__::") - .append(template) - .toString(); + return new StringBuilder("__").append(contentType.queryParameter().toLowerCase(Locale.ROOT)) + .append("__::") + .append(template) + .toString(); } } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherUtilsTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherUtilsTests.java index 54f907162959f..eb820a39cbc34 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherUtilsTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherUtilsTests.java @@ -11,18 +11,18 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.ToXContentObject; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptType; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.ToXContentObject; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.watcher.support.WatcherUtils; import org.elasticsearch.xpack.watcher.support.search.WatcherSearchTemplateRequest; @@ -48,8 +48,8 @@ public class WatcherUtilsTests extends ESTestCase { - private static final String IGNORE_THROTTLED_FIELD_WARNING = "Deprecated field [ignore_throttled] used, this field is unused and " + - "will be removed entirely"; + private static final String IGNORE_THROTTLED_FIELD_WARNING = "Deprecated field [ignore_throttled] used, this field is unused and " + + "will be removed entirely"; public void testFlattenModel() throws Exception { ZonedDateTime now = ZonedDateTime.now(Clock.systemUTC()); @@ -96,10 +96,17 @@ public void testResponseToData() throws Exception { public void testSerializeSearchRequest() throws Exception { String[] expectedIndices = generateRandomStringArray(5, 5, true); - IndicesOptions expectedIndicesOptions = IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), - randomBoolean(), randomBoolean(), DEFAULT_INDICES_OPTIONS.allowAliasesToMultipleIndices(), - DEFAULT_INDICES_OPTIONS.forbidClosedIndices(), DEFAULT_INDICES_OPTIONS.ignoreAliases(), - DEFAULT_INDICES_OPTIONS.ignoreThrottled()); + IndicesOptions expectedIndicesOptions = IndicesOptions.fromOptions( + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + DEFAULT_INDICES_OPTIONS.allowAliasesToMultipleIndices(), + DEFAULT_INDICES_OPTIONS.forbidClosedIndices(), + DEFAULT_INDICES_OPTIONS.ignoreAliases(), + DEFAULT_INDICES_OPTIONS.ignoreThrottled() + ); SearchType expectedSearchType = getRandomSupportedSearchType(); BytesReference expectedSource = null; @@ -118,15 +125,13 @@ public void testSerializeSearchRequest() throws Exception { ScriptType scriptType = randomFrom(ScriptType.values()); stored = scriptType == ScriptType.STORED; expectedTemplate = new Script(scriptType, stored ? null : "mustache", text, params); - request = new WatcherSearchTemplateRequest(expectedIndices, expectedSearchType, - expectedIndicesOptions, expectedTemplate); + request = new WatcherSearchTemplateRequest(expectedIndices, expectedSearchType, expectedIndicesOptions, expectedTemplate); } else { SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource().query(QueryBuilders.matchAllQuery()).size(11); XContentBuilder builder = jsonBuilder(); builder.value(sourceBuilder); expectedSource = BytesReference.bytes(builder); - request = new WatcherSearchTemplateRequest(expectedIndices, expectedSearchType, - expectedIndicesOptions, expectedSource); + request = new WatcherSearchTemplateRequest(expectedIndices, expectedSearchType, expectedIndicesOptions, expectedSource); } XContentBuilder builder = jsonBuilder(); @@ -170,9 +175,17 @@ public void testDeserializeSearchRequest() throws Exception { IndicesOptions indicesOptions = DEFAULT_INDICES_OPTIONS; if (randomBoolean()) { - indicesOptions = IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), - randomBoolean(), randomBoolean(), indicesOptions.allowAliasesToMultipleIndices(), - indicesOptions.forbidClosedIndices(), indicesOptions.ignoreAliases(), indicesOptions.ignoreThrottled()); + indicesOptions = IndicesOptions.fromOptions( + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + randomBoolean(), + indicesOptions.allowAliasesToMultipleIndices(), + indicesOptions.forbidClosedIndices(), + indicesOptions.ignoreAliases(), + indicesOptions.ignoreThrottled() + ); if (indicesOptions.equals(DEFAULT_INDICES_OPTIONS) == false) { builder.startObject("indices_options"); indicesOptions.toXContent(builder, ToXContent.EMPTY_PARAMS); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/search/WatcherSearchTemplateRequestTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/search/WatcherSearchTemplateRequestTests.java index e331a99caa9d3..d68e6e71333bf 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/search/WatcherSearchTemplateRequestTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/search/WatcherSearchTemplateRequestTests.java @@ -7,9 +7,9 @@ package org.elasticsearch.xpack.watcher.support.search; import org.elasticsearch.action.search.SearchType; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.ESTestCase; import java.io.IOException; import java.util.Map; @@ -36,7 +36,7 @@ public void testDefaultHitCountsDefaults() throws IOException { public void testDefaultHitCountsConfigured() throws IOException { boolean hitCountsAsInt = randomBoolean(); - String source = "{ \"rest_total_hits_as_int\" : " + hitCountsAsInt + " }"; + String source = "{ \"rest_total_hits_as_int\" : " + hitCountsAsInt + " }"; assertHitCount(source, hitCountsAsInt); } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/xcontent/XContentSourceTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/xcontent/XContentSourceTests.java index 765d5226e99b0..1f14a7aff3dd7 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/xcontent/XContentSourceTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/xcontent/XContentSourceTests.java @@ -6,12 +6,11 @@ */ package org.elasticsearch.xpack.watcher.support.xcontent; - import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.watcher.support.xcontent.XContentSource; import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; @@ -21,14 +20,17 @@ public class XContentSourceTests extends ESTestCase { public void testToXContent() throws Exception { XContentBuilder builder = randomBoolean() ? jsonBuilder() : randomBoolean() ? yamlBuilder() : smileBuilder(); - BytesReference bytes = randomBoolean() ? - BytesReference.bytes(builder.startObject().field("key", "value").endObject()) : - BytesReference - .bytes(builder.startObject() - .field("key_str", "value") - .startArray("array_int").value(randomInt(10)).endArray() - .nullField("key_null") - .endObject()); + BytesReference bytes = randomBoolean() + ? BytesReference.bytes(builder.startObject().field("key", "value").endObject()) + : BytesReference.bytes( + builder.startObject() + .field("key_str", "value") + .startArray("array_int") + .value(randomInt(10)) + .endArray() + .nullField("key_null") + .endObject() + ); XContentSource source = new XContentSource(bytes, builder.contentType()); XContentBuilder builder2 = XContentFactory.contentBuilder(builder.contentType()); BytesReference bytes2 = BytesReference.bytes(source.toXContent(builder2, ToXContent.EMPTY_PARAMS)); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/MockTextTemplateEngine.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/MockTextTemplateEngine.java index b7393c9fe0253..f33f8774c8f4b 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/MockTextTemplateEngine.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/MockTextTemplateEngine.java @@ -18,7 +18,7 @@ public MockTextTemplateEngine() { @Override public String render(TextTemplate textTemplate, Map model) { - if (textTemplate == null ) { + if (textTemplate == null) { return null; } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/TimeWarpedWatcher.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/TimeWarpedWatcher.java index 546ecb6d6783f..434ddfb3e5eb7 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/TimeWarpedWatcher.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/TimeWarpedWatcher.java @@ -6,8 +6,8 @@ */ package org.elasticsearch.xpack.watcher.test; -import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.threadpool.ThreadPool; @@ -59,7 +59,7 @@ protected Clock getClock() { } @Override - protected TriggerEngine getTriggerEngine(Clock clock, ScheduleRegistry scheduleRegistry){ + protected TriggerEngine getTriggerEngine(Clock clock, ScheduleRegistry scheduleRegistry) { return new ScheduleTriggerEngineMock(scheduleRegistry, clock); } @@ -69,7 +69,7 @@ protected WatchExecutor getWatchExecutor(ThreadPool threadPool) { } @Override - protected Consumer> getTriggerEngineListener(ExecutionService executionService){ + protected Consumer> getTriggerEngineListener(ExecutionService executionService) { return new SyncTriggerEventConsumer(executionService); } }); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/WatcherMockScriptPlugin.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/WatcherMockScriptPlugin.java index b2f5751c34e31..01ea342b20b5e 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/WatcherMockScriptPlugin.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/WatcherMockScriptPlugin.java @@ -28,31 +28,42 @@ public abstract class WatcherMockScriptPlugin extends MockScriptPlugin { public static final Map, MockScriptEngine.ContextCompiler> CONTEXT_COMPILERS; static { - CONTEXT_COMPILERS = Map.of(WatcherConditionScript.CONTEXT, (script, options) -> - (WatcherConditionScript.Factory) (params, watcherContext) -> - new WatcherConditionScript(params, watcherContext) { - @Override - public boolean execute() { - Map vars = new HashMap<>(); - vars.put("params", getParams()); - vars.put("ctx", getCtx()); - return (boolean) script.apply(vars); - } - }, WatcherTransformScript.CONTEXT, (script, options) -> - (WatcherTransformScript.Factory) (params, watcherContext, payload) -> - new WatcherTransformScript(params, watcherContext, payload) { - @Override - public Object execute() { - Map vars = new HashMap<>(); - vars.put("params", getParams()); - vars.put("ctx", getCtx()); - return script.apply(vars); - } - }); + CONTEXT_COMPILERS = Map.of( + WatcherConditionScript.CONTEXT, + (script, options) -> (WatcherConditionScript.Factory) (params, watcherContext) -> new WatcherConditionScript( + params, + watcherContext + ) { + @Override + public boolean execute() { + Map vars = new HashMap<>(); + vars.put("params", getParams()); + vars.put("ctx", getCtx()); + return (boolean) script.apply(vars); + } + }, + WatcherTransformScript.CONTEXT, + (script, options) -> (WatcherTransformScript.Factory) (params, watcherContext, payload) -> new WatcherTransformScript( + params, + watcherContext, + payload + ) { + @Override + public Object execute() { + Map vars = new HashMap<>(); + vars.put("params", getParams()); + vars.put("ctx", getCtx()); + return script.apply(vars); + } + } + ); } - public static final List> CONTEXTS = - List.of(WatcherConditionScript.CONTEXT, WatcherTransformScript.CONTEXT, Watcher.SCRIPT_TEMPLATE_CONTEXT); + public static final List> CONTEXTS = List.of( + WatcherConditionScript.CONTEXT, + WatcherTransformScript.CONTEXT, + Watcher.SCRIPT_TEMPLATE_CONTEXT + ); @Override protected Map, MockScriptEngine.ContextCompiler> pluginContextCompilers() { @@ -61,8 +72,7 @@ protected Map, MockScriptEngine.ContextCompiler> pluginContextC public static ScriptService newMockScriptService(Map, Object>> scripts) { Map engines = new HashMap<>(); - engines.put(MockScriptEngine.NAME, - new MockScriptEngine(MockScriptEngine.NAME, scripts, CONTEXT_COMPILERS)); + engines.put(MockScriptEngine.NAME, new MockScriptEngine(MockScriptEngine.NAME, scripts, CONTEXT_COMPILERS)); Map> contexts = CONTEXTS.stream().collect(Collectors.toMap(o -> o.name, Function.identity())); return new ScriptService(Settings.EMPTY, engines, contexts); } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/WatcherTestUtils.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/WatcherTestUtils.java index 735031cf65346..f6f7ec7c1aeea 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/WatcherTestUtils.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/WatcherTestUtils.java @@ -11,13 +11,13 @@ import org.elasticsearch.client.Client; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.license.XPackLicenseState; +import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.xcontent.XContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.license.XPackLicenseState; -import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.xpack.core.watcher.actions.ActionStatus; import org.elasticsearch.xpack.core.watcher.actions.ActionWrapper; import org.elasticsearch.xpack.core.watcher.actions.throttler.ActionThrottler; @@ -70,17 +70,16 @@ import java.util.Map; import static java.util.Collections.emptyMap; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery; import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource; import static org.elasticsearch.test.ESTestCase.randomFrom; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.Matchers.is; import static org.mockito.Mockito.mock; public final class WatcherTestUtils { - private WatcherTestUtils() { - } + private WatcherTestUtils() {} public static XContentSource xContentSource(BytesReference bytes) { XContent xContent = XContentFactory.xContent(XContentHelper.xContentType(bytes)); @@ -91,69 +90,87 @@ public static WatcherSearchTemplateRequest templateRequest(SearchSourceBuilder s return templateRequest(sourceBuilder, SearchType.DEFAULT, indices); } - public static WatcherSearchTemplateRequest templateRequest(SearchSourceBuilder sourceBuilder, SearchType searchType, - String... indices) { + public static WatcherSearchTemplateRequest templateRequest( + SearchSourceBuilder sourceBuilder, + SearchType searchType, + String... indices + ) { try { XContentBuilder xContentBuilder = jsonBuilder(); xContentBuilder.value(sourceBuilder); - return new WatcherSearchTemplateRequest(indices, searchType, - WatcherSearchTemplateRequest.DEFAULT_INDICES_OPTIONS, BytesReference.bytes(xContentBuilder)); + return new WatcherSearchTemplateRequest( + indices, + searchType, + WatcherSearchTemplateRequest.DEFAULT_INDICES_OPTIONS, + BytesReference.bytes(xContentBuilder) + ); } catch (IOException e) { throw new RuntimeException(e); } } public static WatchExecutionContextMockBuilder mockExecutionContextBuilder(String watchId) { - return new WatchExecutionContextMockBuilder(watchId) - .wid(new Wid(watchId, ZonedDateTime.now(ZoneOffset.UTC))); + return new WatchExecutionContextMockBuilder(watchId).wid(new Wid(watchId, ZonedDateTime.now(ZoneOffset.UTC))); } public static WatchExecutionContext mockExecutionContext(String watchId, Payload payload) { - return mockExecutionContextBuilder(watchId) - .wid(new Wid(watchId, ZonedDateTime.now(ZoneOffset.UTC))) - .payload(payload) - .buildMock(); + return mockExecutionContextBuilder(watchId).wid(new Wid(watchId, ZonedDateTime.now(ZoneOffset.UTC))).payload(payload).buildMock(); } public static WatchExecutionContext mockExecutionContext(String watchId, ZonedDateTime time, Payload payload) { - return mockExecutionContextBuilder(watchId) - .wid(new Wid(watchId, ZonedDateTime.now(ZoneOffset.UTC))) - .payload(payload) - .time(watchId, time) - .buildMock(); + return mockExecutionContextBuilder(watchId).wid(new Wid(watchId, ZonedDateTime.now(ZoneOffset.UTC))) + .payload(payload) + .time(watchId, time) + .buildMock(); } - public static WatchExecutionContext mockExecutionContext(String watchId, ZonedDateTime executionTime, TriggerEvent event, - Payload payload) { - return mockExecutionContextBuilder(watchId) - .wid(new Wid(watchId, ZonedDateTime.now(ZoneOffset.UTC))) - .payload(payload) - .executionTime(executionTime) - .triggerEvent(event) - .buildMock(); + public static WatchExecutionContext mockExecutionContext( + String watchId, + ZonedDateTime executionTime, + TriggerEvent event, + Payload payload + ) { + return mockExecutionContextBuilder(watchId).wid(new Wid(watchId, ZonedDateTime.now(ZoneOffset.UTC))) + .payload(payload) + .executionTime(executionTime) + .triggerEvent(event) + .buildMock(); } public static WatchExecutionContext createWatchExecutionContext() throws Exception { ZonedDateTime EPOCH_UTC = Instant.EPOCH.atZone(ZoneOffset.UTC); - Watch watch = new Watch("test-watch", - new ScheduleTrigger(new IntervalSchedule(new IntervalSchedule.Interval(1, IntervalSchedule.Interval.Unit.MINUTES))), - new ExecutableSimpleInput(new SimpleInput(new Payload.Simple())), - InternalAlwaysCondition.INSTANCE, - null, - null, - new ArrayList<>(), - null, - - new WatchStatus(EPOCH_UTC, emptyMap()), 1L, 1L); - TriggeredExecutionContext context = new TriggeredExecutionContext(watch.id(), EPOCH_UTC, - new ScheduleTriggerEvent(watch.id(), EPOCH_UTC, EPOCH_UTC), TimeValue.timeValueSeconds(5)); + Watch watch = new Watch( + "test-watch", + new ScheduleTrigger(new IntervalSchedule(new IntervalSchedule.Interval(1, IntervalSchedule.Interval.Unit.MINUTES))), + new ExecutableSimpleInput(new SimpleInput(new Payload.Simple())), + InternalAlwaysCondition.INSTANCE, + null, + null, + new ArrayList<>(), + null, + + new WatchStatus(EPOCH_UTC, emptyMap()), + 1L, + 1L + ); + TriggeredExecutionContext context = new TriggeredExecutionContext( + watch.id(), + EPOCH_UTC, + new ScheduleTriggerEvent(watch.id(), EPOCH_UTC, EPOCH_UTC), + TimeValue.timeValueSeconds(5) + ); context.ensureWatchExists(() -> watch); return context; } - - public static Watch createTestWatch(String watchName, Client client, HttpClient httpClient, EmailService emailService, - WatcherSearchTemplateService searchTemplateService, Logger logger) { + public static Watch createTestWatch( + String watchName, + Client client, + HttpClient httpClient, + EmailService emailService, + WatcherSearchTemplateService searchTemplateService, + Logger logger + ) { ActionThrottler actionThrottler = new ActionThrottler(Clock.systemUTC(), null, mock(XPackLicenseState.class)); List actions = new ArrayList<>(); TextTemplateEngine engine = new MockTextTemplateEngine(); @@ -162,15 +179,29 @@ public static Watch createTestWatch(String watchName, Client client, HttpClient httpRequest.method(HttpMethod.POST); httpRequest.path(new TextTemplate("/foobarbaz/{{ctx.watch_id}}")); httpRequest.body(new TextTemplate("{{ctx.watch_id}} executed with {{ctx.payload.response.hits.total_hits}} hits")); - actions.add(new ActionWrapper("_webhook", actionThrottler, null, null, - new ExecutableWebhookAction(new WebhookAction(httpRequest.build()), logger, httpClient, engine), null, null)); - + actions.add( + new ActionWrapper( + "_webhook", + actionThrottler, + null, + null, + new ExecutableWebhookAction(new WebhookAction(httpRequest.build()), logger, httpClient, engine), + null, + null + ) + ); EmailTemplate email = EmailTemplate.builder().from("from@test.com").to("to@test.com").build(); Authentication auth = new Authentication("testname", new Secret("testpassword".toCharArray())); EmailAction action = new EmailAction(email, "testaccount", auth, Profile.STANDARD, null, null); - ExecutableEmailAction executale = new ExecutableEmailAction(action, logger, emailService, engine, - new HtmlSanitizer(Settings.EMPTY), Collections.emptyMap()); + ExecutableEmailAction executale = new ExecutableEmailAction( + action, + logger, + emailService, + engine, + new HtmlSanitizer(Settings.EMPTY), + Collections.emptyMap() + ); actions.add(new ActionWrapper("_email", actionThrottler, null, null, executale, null, null)); ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC); @@ -182,15 +213,18 @@ public static Watch createTestWatch(String watchName, Client client, HttpClient SearchTransform searchTransform = new SearchTransform(transformRequest, null, null); return new Watch( - watchName, - new ScheduleTrigger(new CronSchedule("0/5 * * * * ? *")), - new ExecutableSimpleInput(new SimpleInput(new Payload.Simple(Collections.singletonMap("bar", "foo")))), - InternalAlwaysCondition.INSTANCE, - new ExecutableSearchTransform(searchTransform, logger, client, searchTemplateService, TimeValue.timeValueMinutes(1)), - new TimeValue(0), - actions, - Collections.singletonMap("foo", "bar"), - new WatchStatus(now, statuses), 1L, 1L); + watchName, + new ScheduleTrigger(new CronSchedule("0/5 * * * * ? *")), + new ExecutableSimpleInput(new SimpleInput(new Payload.Simple(Collections.singletonMap("bar", "foo")))), + InternalAlwaysCondition.INSTANCE, + new ExecutableSearchTransform(searchTransform, logger, client, searchTemplateService, TimeValue.timeValueMinutes(1)), + new TimeValue(0), + actions, + Collections.singletonMap("foo", "bar"), + new WatchStatus(now, statuses), + 1L, + 1L + ); } public static SearchType getRandomSupportedSearchType() { diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/bench/ScheduleEngineTriggerBenchmark.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/bench/ScheduleEngineTriggerBenchmark.java index ee5bab40541e9..de5ee1b7bbe70 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/bench/ScheduleEngineTriggerBenchmark.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/bench/ScheduleEngineTriggerBenchmark.java @@ -6,9 +6,9 @@ */ package org.elasticsearch.xpack.watcher.test.bench; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.common.metrics.MeanMetric; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.xpack.core.watcher.trigger.TriggerEvent; import org.elasticsearch.xpack.core.watcher.watch.Watch; import org.elasticsearch.xpack.watcher.condition.InternalAlwaysCondition; @@ -52,13 +52,24 @@ public static void main(String[] args) throws Exception { } System.out.println("Running benchmark with numWatches=" + numWatches + " benchTime=" + benchTime + " interval=" + interval); - Settings settings = Settings.builder() - .put("name", "test") - .build(); + Settings settings = Settings.builder().put("name", "test").build(); List watches = new ArrayList<>(numWatches); for (int i = 0; i < numWatches; i++) { - watches.add(new Watch("job_" + i, new ScheduleTrigger(interval(interval + "s")), new ExecutableNoneInput(), - InternalAlwaysCondition.INSTANCE, null, null, Collections.emptyList(), null, null, 1L, 1L)); + watches.add( + new Watch( + "job_" + i, + new ScheduleTrigger(interval(interval + "s")), + new ExecutableNoneInput(), + InternalAlwaysCondition.INSTANCE, + null, + null, + Collections.emptyList(), + null, + null, + 1L, + 1L + ) + ); } ScheduleRegistry scheduleRegistry = new ScheduleRegistry(emptySet()); @@ -78,8 +89,13 @@ protected void notifyListeners(List events) { if (running.get()) { for (TriggerEvent event : events) { ScheduleTriggerEvent scheduleTriggerEvent = (ScheduleTriggerEvent) event; - measure(total, triggerMetric, tooEarlyMetric, event.triggeredTime().toInstant().toEpochMilli(), - scheduleTriggerEvent.scheduledTime().toInstant().toEpochMilli()); + measure( + total, + triggerMetric, + tooEarlyMetric, + event.triggeredTime().toInstant().toEpochMilli(), + scheduleTriggerEvent.scheduledTime().toInstant().toEpochMilli() + ); } } } @@ -90,24 +106,39 @@ protected void notifyListeners(List events) { Thread.sleep(benchTime); running.set(false); scheduler.stop(); - System.out.println("done, triggered [" + total.get() + "] times, delayed triggered [" + triggerMetric.count() + - "] times, avg [" + triggerMetric.mean() + "] ms"); + System.out.println( + "done, triggered [" + + total.get() + + "] times, delayed triggered [" + + triggerMetric.count() + + "] times, avg [" + + triggerMetric.mean() + + "] ms" + ); results.add(new Stats(total.get(), triggerMetric.count(), triggerMetric.mean(), tooEarlyMetric.count(), tooEarlyMetric.mean())); System.out.println(" Name | # triggered | # delayed | avg delay | # too early triggered | avg too early delay"); System.out.println("--------------- | ----------- | --------- | --------- | --------------------- | ------------------ "); for (Stats stats : results) { System.out.printf( - Locale.ENGLISH, - "%11d | %9d | %9d | %21d | %18d\n", - stats.numberOfTimesTriggered, stats.numberOfTimesDelayed, stats.avgDelayTime, - stats.numberOfEarlyTriggered, stats.avgEarlyDelayTime + Locale.ENGLISH, + "%11d | %9d | %9d | %21d | %18d\n", + stats.numberOfTimesTriggered, + stats.numberOfTimesDelayed, + stats.avgDelayTime, + stats.numberOfEarlyTriggered, + stats.avgEarlyDelayTime ); } } - private static void measure(AtomicInteger total, MeanMetric triggerMetric, MeanMetric tooEarlyMetric, long triggeredTime, - long scheduledTime) { + private static void measure( + AtomicInteger total, + MeanMetric triggerMetric, + MeanMetric tooEarlyMetric, + long triggeredTime, + long scheduledTime + ) { total.incrementAndGet(); if (Long.compare(triggeredTime, scheduledTime) != 0) { long delta = triggeredTime - scheduledTime; @@ -126,8 +157,13 @@ static class Stats { final long numberOfEarlyTriggered; final long avgEarlyDelayTime; - Stats(int numberOfTimesTriggered, long numberOfTimesDelayed, double avgDelayTime, - long numberOfEarlyTriggered, double avgEarlyDelayTime) { + Stats( + int numberOfTimesTriggered, + long numberOfTimesDelayed, + double avgDelayTime, + long numberOfEarlyTriggered, + double avgEarlyDelayTime + ) { this.numberOfTimesTriggered = numberOfTimesTriggered; this.numberOfTimesDelayed = numberOfTimesDelayed; this.avgDelayTime = Math.round(avgDelayTime); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/bench/WatcherExecutorServiceBenchmark.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/bench/WatcherExecutorServiceBenchmark.java index 384735b9874ef..af6171c587b53 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/bench/WatcherExecutorServiceBenchmark.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/bench/WatcherExecutorServiceBenchmark.java @@ -9,13 +9,13 @@ import org.apache.logging.log4j.LogManager; import org.elasticsearch.client.Client; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.node.MockNode; import org.elasticsearch.node.Node; import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest; import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptType; import org.elasticsearch.search.builder.SearchSourceBuilder; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.watcher.client.WatchSourceBuilder; import org.elasticsearch.xpack.core.watcher.transport.actions.put.PutWatchAction; import org.elasticsearch.xpack.watcher.Watcher; @@ -46,22 +46,21 @@ public class WatcherExecutorServiceBenchmark { private static final Settings SETTINGS = Settings.builder() - .put("xpack.security.enabled", false) - .put("cluster.name", "bench") - .put("network.host", "localhost") - .put("script.disable_dynamic", false) - .put(DISCOVERY_SEED_HOSTS_SETTING.getKey(), "localhost") - .put("http.cors.enabled", true) - .put("cluster.routing.allocation.disk.threshold_enabled", false) -// .put("recycler.page.limit.heap", "60%") - .build(); + .put("xpack.security.enabled", false) + .put("cluster.name", "bench") + .put("network.host", "localhost") + .put("script.disable_dynamic", false) + .put(DISCOVERY_SEED_HOSTS_SETTING.getKey(), "localhost") + .put("http.cors.enabled", true) + .put("cluster.routing.allocation.disk.threshold_enabled", false) + // .put("recycler.page.limit.heap", "60%") + .build(); private static Client client; private static ScheduleTriggerEngineMock scheduler; protected static void start() throws Exception { - Node node = new MockNode(Settings.builder().put(SETTINGS).put("node.data", false).build(), - Arrays.asList(BenchmarkWatcher.class)); + Node node = new MockNode(Settings.builder().put(SETTINGS).put("node.data", false).build(), Arrays.asList(BenchmarkWatcher.class)); client = node.client(); client.admin().cluster().prepareHealth("*").setWaitForGreenStatus().get(); Thread.sleep(5000); @@ -78,14 +77,18 @@ public static void main(String[] args) throws Exception { int numAlerts = 1000; for (int i = 0; i < numAlerts; i++) { final String name = "_name" + i; - PutWatchRequest putAlertRequest = new PutWatchRequest(name, new WatchSourceBuilder() - .trigger(schedule(interval("5s"))) + PutWatchRequest putAlertRequest = new PutWatchRequest( + name, + new WatchSourceBuilder().trigger(schedule(interval("5s"))) .input(searchInput(templateRequest(new SearchSourceBuilder(), "test"))) - .condition(new ScriptCondition(new Script( - ScriptType.INLINE, - Script.DEFAULT_SCRIPT_LANG, - "ctx.payload.hits.total.value > 0", - emptyMap()))).buildAsBytes(XContentType.JSON), XContentType.JSON); + .condition( + new ScriptCondition( + new Script(ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG, "ctx.payload.hits.total.value > 0", emptyMap()) + ) + ) + .buildAsBytes(XContentType.JSON), + XContentType.JSON + ); putAlertRequest.setId(name); client.execute(PutWatchAction.INSTANCE, putAlertRequest).actionGet(); } @@ -123,12 +126,15 @@ public static void main(String[] args) throws Exception { int numAlerts = 1000; for (int i = 0; i < numAlerts; i++) { final String name = "_name" + i; - PutWatchRequest putAlertRequest = new PutWatchRequest(name, new WatchSourceBuilder() - .trigger(schedule(interval("5s"))) - .input(searchInput(templateRequest(new SearchSourceBuilder(), "test")) - .extractKeys("hits.total.value")) + PutWatchRequest putAlertRequest = new PutWatchRequest( + name, + new WatchSourceBuilder().trigger(schedule(interval("5s"))) + .input(searchInput(templateRequest(new SearchSourceBuilder(), "test")).extractKeys("hits.total.value")) .condition(new ScriptCondition(new Script(ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG, "1 == 1", emptyMap()))) - .addAction("_id", indexAction("index")).buildAsBytes(XContentType.JSON), XContentType.JSON); + .addAction("_id", indexAction("index")) + .buildAsBytes(XContentType.JSON), + XContentType.JSON + ); putAlertRequest.setId(name); client.execute(PutWatchAction.INSTANCE, putAlertRequest).actionGet(); } @@ -153,7 +159,6 @@ public void run() { threads[i].start(); } - for (Thread thread : threads) { thread.join(); } @@ -168,14 +173,23 @@ public static void main(String[] args) throws Exception { int numAlerts = 1000; for (int i = 0; i < numAlerts; i++) { final String name = "_name" + i; - PutWatchRequest putAlertRequest = new PutWatchRequest(name, new WatchSourceBuilder() - .trigger(schedule(interval("5s"))) + PutWatchRequest putAlertRequest = new PutWatchRequest( + name, + new WatchSourceBuilder().trigger(schedule(interval("5s"))) .input(httpInput(HttpRequestTemplate.builder("localhost", 9200))) - .condition(new ScriptCondition(new Script( - ScriptType.INLINE, - Script.DEFAULT_SCRIPT_LANG, - "ctx.payload.tagline == \"You Know, for Search\"", - emptyMap()))).buildAsBytes(XContentType.JSON), XContentType.JSON); + .condition( + new ScriptCondition( + new Script( + ScriptType.INLINE, + Script.DEFAULT_SCRIPT_LANG, + "ctx.payload.tagline == \"You Know, for Search\"", + emptyMap() + ) + ) + ) + .buildAsBytes(XContentType.JSON), + XContentType.JSON + ); putAlertRequest.setId(name); client.execute(PutWatchAction.INSTANCE, putAlertRequest).actionGet(); } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/bench/WatcherScheduleEngineBenchmark.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/bench/WatcherScheduleEngineBenchmark.java index 64902a520f6e0..96258c7d9b7cf 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/bench/WatcherScheduleEngineBenchmark.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/bench/WatcherScheduleEngineBenchmark.java @@ -12,11 +12,10 @@ import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.Client; import org.elasticsearch.common.Strings; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.common.metrics.MeanMetric; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeValue; -import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.monitor.jvm.JvmInfo; import org.elasticsearch.node.InternalSettingsPreparer; @@ -29,6 +28,7 @@ import org.elasticsearch.search.aggregations.metrics.Percentiles; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.threadpool.ThreadPoolStats; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.watcher.WatcherState; import org.elasticsearch.xpack.core.watcher.client.WatchSourceBuilder; import org.elasticsearch.xpack.core.watcher.history.HistoryStoreField; @@ -61,16 +61,16 @@ public class WatcherScheduleEngineBenchmark { private static final Settings SETTINGS = Settings.builder() - .put("xpack.security.enabled", false) - .put("cluster.name", "bench") - .put("script.disable_dynamic", false) - .put("http.cors.enabled", true) - .build(); + .put("xpack.security.enabled", false) + .put("cluster.name", "bench") + .put("script.disable_dynamic", false) + .put("http.cors.enabled", true) + .build(); public static void main(String[] args) throws Exception { System.setProperty("es.logger.prefix", ""); - String[] engines = new String[]{"ticker", "scheduler"}; + String[] engines = new String[] { "ticker", "scheduler" }; int numWatches = 2000; int benchTime = 60000; int interval = 1; @@ -91,19 +91,22 @@ public static void main(String[] args) throws Exception { } } System.out.println("Running schedule benchmark with:"); - System.out.println("numWatches=" + numWatches + " benchTime=" + benchTime + " interval=" + interval + - " engines=" + Arrays.toString(engines)); + System.out.println( + "numWatches=" + numWatches + " benchTime=" + benchTime + " interval=" + interval + " engines=" + Arrays.toString(engines) + ); System.out.println("and heap_max=" + JvmInfo.jvmInfo().getMem().getHeapMax()); - // First clean everything and index the watcher (but not via put alert api!) - try (Node node = new Node(InternalSettingsPreparer.prepareEnvironment( + try ( + Node node = new Node( + InternalSettingsPreparer.prepareEnvironment( Settings.builder().put(SETTINGS).put("node.data", false).build(), emptyMap(), null, - () -> { - throw new IllegalArgumentException("settings must have [node.name]"); - })).start()) { + () -> { throw new IllegalArgumentException("settings must have [node.name]"); } + ) + ).start() + ) { try (Client client = node.client()) { ClusterHealthResponse response = client.admin().cluster().prepareHealth().setWaitForNodes("2").get(); if (response.getNumberOfNodes() != 2 && response.getNumberOfDataNodes() != 1) { @@ -117,25 +120,33 @@ public static void main(String[] args) throws Exception { System.out.println("===============> indexing [" + numWatches + "] watches"); for (int i = 0; i < numWatches; i++) { final String id = "_id_" + i; - client.prepareIndex().setIndex(Watch.INDEX).setId(id) - .setSource(new WatchSourceBuilder() - .trigger(schedule(interval(interval + "s"))) - .input(searchInput(templateRequest(new SearchSourceBuilder(), "test"))) - .condition(new ScriptCondition(new Script( - ScriptType.INLINE, - Script.DEFAULT_SCRIPT_LANG, - "ctx.payload.hits.total.value > 0", - emptyMap()))) - .addAction("logging", ActionBuilders.loggingAction("test").setLevel(LoggingLevel.TRACE)) - .buildAsBytes(XContentType.JSON), XContentType.JSON - ).get(); + client.prepareIndex() + .setIndex(Watch.INDEX) + .setId(id) + .setSource( + new WatchSourceBuilder().trigger(schedule(interval(interval + "s"))) + .input(searchInput(templateRequest(new SearchSourceBuilder(), "test"))) + .condition( + new ScriptCondition( + new Script( + ScriptType.INLINE, + Script.DEFAULT_SCRIPT_LANG, + "ctx.payload.hits.total.value > 0", + emptyMap() + ) + ) + ) + .addAction("logging", ActionBuilders.loggingAction("test").setLevel(LoggingLevel.TRACE)) + .buildAsBytes(XContentType.JSON), + XContentType.JSON + ) + .get(); } client.admin().indices().prepareFlush(Watch.INDEX, "test").get(); System.out.println("===============> indexed [" + numWatches + "] watches"); } } - // Now for each scheduler impl run the benchmark Map results = new HashMap<>(); for (String engine : engines) { @@ -144,10 +155,10 @@ public static void main(String[] args) throws Exception { System.out.println("===============> testing engine [" + engine + "]"); System.gc(); Settings settings = Settings.builder() - .put(SETTINGS) - .put("xpack.watcher.trigger.schedule.engine", engine) - .put("node.data", false) - .build(); + .put(SETTINGS) + .put("xpack.watcher.trigger.schedule.engine", engine) + .put("node.data", false) + .build(); try (Node node = new MockNode(settings, Arrays.asList(LocalStateWatcher.class))) { try (Client client = node.client()) { client.admin().cluster().prepareHealth().setWaitForNodes("2").get(); @@ -155,14 +166,21 @@ public static void main(String[] args) throws Exception { client.admin().cluster().prepareHealth(Watch.INDEX, "test").setWaitForYellowStatus().get(); Clock clock = node.injector().getInstance(Clock.class); - while (new WatcherStatsRequestBuilder(client).get().getNodes().stream() - .allMatch(r -> r.getWatcherState() == WatcherState.STARTED) == false) { + while (new WatcherStatsRequestBuilder(client).get() + .getNodes() + .stream() + .allMatch(r -> r.getWatcherState() == WatcherState.STARTED) == false) { Thread.sleep(100); } long actualLoadedWatches = new WatcherStatsRequestBuilder(client).get().getWatchesCount(); if (actualLoadedWatches != numWatches) { - throw new IllegalStateException("Expected [" + numWatches + "] watched to be loaded, but only [" + - actualLoadedWatches + "] watches were actually loaded"); + throw new IllegalStateException( + "Expected [" + + numWatches + + "] watched to be loaded, but only [" + + actualLoadedWatches + + "] watches were actually loaded" + ); } long startTime = clock.millis(); System.out.println("==> watcher started, waiting [" + benchTime + "] seconds now..."); @@ -184,7 +202,7 @@ public void run() { }); sampleThread.start(); Thread.sleep(benchTime); - long endTime = clock.millis(); + long endTime = clock.millis(); start.set(false); sampleThread.join(); @@ -198,22 +216,17 @@ public void run() { } client.admin().indices().prepareRefresh(HistoryStoreField.DATA_STREAM + "*").get(); Script script = new Script( - ScriptType.INLINE, - Script.DEFAULT_SCRIPT_LANG, - "doc['trigger_event.schedule.triggered_time'].value - doc['trigger_event.schedule.scheduled_time'].value", - emptyMap()); + ScriptType.INLINE, + Script.DEFAULT_SCRIPT_LANG, + "doc['trigger_event.schedule.triggered_time'].value - doc['trigger_event.schedule.scheduled_time'].value", + emptyMap() + ); SearchResponse searchResponse = client.prepareSearch(HistoryStoreField.DATA_STREAM + "*") - .setQuery(QueryBuilders.rangeQuery("trigger_event.schedule.scheduled_time").gte(startTime).lte(endTime)) - .addAggregation(terms("state").field("state")) - .addAggregation(histogram("delay") - .script(script) - .interval(10) - ) - .addAggregation(percentiles("percentile_delay") - .script(script) - .percentiles(1.0, 20.0, 50.0, 80.0, 99.0) - ) - .get(); + .setQuery(QueryBuilders.rangeQuery("trigger_event.schedule.scheduled_time").gte(startTime).lte(endTime)) + .addAggregation(terms("state").field("state")) + .addAggregation(histogram("delay").script(script).interval(10)) + .addAggregation(percentiles("percentile_delay").script(script).percentiles(1.0, 20.0, 50.0, 80.0, 99.0)) + .get(); Terms terms = searchResponse.getAggregations().get("state"); stats.setStateStats(terms); Histogram histogram = searchResponse.getAggregations().get("delay"); @@ -328,10 +341,12 @@ public void setAvgJvmUsed(MeanMetric jvmMemUsed) { public void printThreadStats() throws IOException { System.out.printf( - Locale.ENGLISH, - "%10s | %13s | %12d | %13d \n", - name, new ByteSizeValue(avgHeapUsed), - watcherThreadPoolStats.getRejected(), watcherThreadPoolStats.getCompleted() + Locale.ENGLISH, + "%10s | %13s | %12d | %13d \n", + name, + new ByteSizeValue(avgHeapUsed), + watcherThreadPoolStats.getRejected(), + watcherThreadPoolStats.getCompleted() ); } @@ -341,12 +356,13 @@ public void printWatchRecordState() throws IOException { Terms.Bucket throttled = stateStats.getBucketByKey("throttled"); Terms.Bucket awaitsExecution = stateStats.getBucketByKey("awaits_execution"); System.out.printf( - Locale.ENGLISH, - "%10s | %16d | %14d | %17d | %24d \n", - name, executed != null ? executed.getDocCount() : 0, - failed != null ? failed.getDocCount() : 0, - throttled != null ? throttled.getDocCount() : 0, - awaitsExecution != null ? awaitsExecution.getDocCount() : 0 + Locale.ENGLISH, + "%10s | %16d | %14d | %17d | %24d \n", + name, + executed != null ? executed.getDocCount() : 0, + failed != null ? failed.getDocCount() : 0, + throttled != null ? throttled.getDocCount() : 0, + awaitsExecution != null ? awaitsExecution.getDocCount() : 0 ); } @@ -357,9 +373,14 @@ public void printTriggerDelay() throws Exception { String _80thPercentile = String.valueOf(Math.round(delayPercentiles.percentile(80.0))); String _99thPercentile = String.valueOf(Math.round(delayPercentiles.percentile(99.0))); System.out.printf( - Locale.ENGLISH, - "%10s | %10s | %11s | %11s | %11s | %11s \n", - name, _1thPercentile, _20thPercentile, _50thPercentile, _80thPercentile, _99thPercentile + Locale.ENGLISH, + "%10s | %10s | %11s | %11s | %11s | %11s \n", + name, + _1thPercentile, + _20thPercentile, + _50thPercentile, + _80thPercentile, + _99thPercentile ); } } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/integration/SearchInputTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/integration/SearchInputTests.java index f7424dbf2cd6e..c450ff7ed325c 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/integration/SearchInputTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/integration/SearchInputTests.java @@ -15,15 +15,8 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.DeprecationHandler; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentFactory; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.script.MockMustacheScriptEngine; import org.elasticsearch.script.ScriptContext; import org.elasticsearch.script.ScriptEngine; @@ -33,6 +26,13 @@ import org.elasticsearch.search.internal.InternalSearchResponse; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xcontent.DeprecationHandler; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext; import org.elasticsearch.xpack.core.watcher.input.Input; import org.elasticsearch.xpack.core.watcher.watch.Payload; @@ -51,12 +51,11 @@ import java.util.HashMap; import java.util.Map; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.index.query.QueryBuilders.boolQuery; import static org.elasticsearch.index.query.QueryBuilders.matchQuery; import static org.elasticsearch.index.query.QueryBuilders.rangeQuery; -import static org.mockito.Mockito.when; import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.xpack.watcher.test.WatcherTestUtils.getRandomSupportedSearchType; import static org.hamcrest.Matchers.arrayContainingInAnyOrder; import static org.hamcrest.Matchers.equalTo; @@ -65,6 +64,7 @@ import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.nullValue; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class SearchInputTests extends ESTestCase { @@ -91,8 +91,16 @@ public void setup() { public void testExecute() throws Exception { ArgumentCaptor requestCaptor = ArgumentCaptor.forClass(SearchRequest.class); PlainActionFuture searchFuture = PlainActionFuture.newFuture(); - SearchResponse searchResponse = new SearchResponse(InternalSearchResponse.empty(), "", 1, 1, 0, 1234, - ShardSearchFailure.EMPTY_ARRAY, SearchResponse.Clusters.EMPTY); + SearchResponse searchResponse = new SearchResponse( + InternalSearchResponse.empty(), + "", + 1, + 1, + 0, + 1234, + ShardSearchFailure.EMPTY_ARRAY, + SearchResponse.Clusters.EMPTY + ); searchFuture.onResponse(searchResponse); when(client.search(requestCaptor.capture())).thenReturn(searchFuture); @@ -103,8 +111,12 @@ public void testExecute() throws Exception { SearchSourceBuilder searchSourceBuilder = searchSource().query(boolQuery().must(matchQuery("event_type", "a"))); WatcherSearchTemplateRequest request = WatcherTestUtils.templateRequest(searchSourceBuilder); - ExecutableSearchInput searchInput = new ExecutableSearchInput(new SearchInput(request, null, null, null), - client, watcherSearchTemplateService(), TimeValue.timeValueMinutes(1)); + ExecutableSearchInput searchInput = new ExecutableSearchInput( + new SearchInput(request, null, null, null), + client, + watcherSearchTemplateService(), + TimeValue.timeValueMinutes(1) + ); WatchExecutionContext ctx = WatcherTestUtils.createWatchExecutionContext(); SearchInput.Result result = searchInput.execute(ctx, new Payload.Simple()); @@ -120,8 +132,16 @@ public void testExecute() throws Exception { public void testDifferentSearchType() throws Exception { ArgumentCaptor requestCaptor = ArgumentCaptor.forClass(SearchRequest.class); PlainActionFuture searchFuture = PlainActionFuture.newFuture(); - SearchResponse searchResponse = new SearchResponse(InternalSearchResponse.empty(), "", 1, 1, 0, 1234, - ShardSearchFailure.EMPTY_ARRAY, SearchResponse.Clusters.EMPTY); + SearchResponse searchResponse = new SearchResponse( + InternalSearchResponse.empty(), + "", + 1, + 1, + 0, + 1234, + ShardSearchFailure.EMPTY_ARRAY, + SearchResponse.Clusters.EMPTY + ); searchFuture.onResponse(searchResponse); when(client.search(requestCaptor.capture())).thenReturn(searchFuture); @@ -129,8 +149,12 @@ public void testDifferentSearchType() throws Exception { SearchType searchType = getRandomSupportedSearchType(); WatcherSearchTemplateRequest request = WatcherTestUtils.templateRequest(searchSourceBuilder, searchType); - ExecutableSearchInput searchInput = new ExecutableSearchInput(new SearchInput(request, null, null, null), - client, watcherSearchTemplateService(), TimeValue.timeValueMinutes(1)); + ExecutableSearchInput searchInput = new ExecutableSearchInput( + new SearchInput(request, null, null, null), + client, + watcherSearchTemplateService(), + TimeValue.timeValueMinutes(1) + ); WatchExecutionContext ctx = WatcherTestUtils.createWatchExecutionContext(); SearchInput.Result result = searchInput.execute(ctx, new Payload.Simple()); @@ -142,9 +166,10 @@ public void testDifferentSearchType() throws Exception { } public void testParserValid() throws Exception { - SearchSourceBuilder source = searchSource() - .query(boolQuery().must(matchQuery("event_type", "a")).must(rangeQuery("_timestamp") - .from("{{ctx.trigger.scheduled_time}}||-30s").to("{{ctx.trigger.triggered_time}}"))); + SearchSourceBuilder source = searchSource().query( + boolQuery().must(matchQuery("event_type", "a")) + .must(rangeQuery("_timestamp").from("{{ctx.trigger.scheduled_time}}||-30s").to("{{ctx.trigger.triggered_time}}")) + ); TimeValue timeout = randomBoolean() ? TimeValue.timeValueSeconds(randomInt(10)) : null; XContentBuilder builder = jsonBuilder().value(new SearchInput(WatcherTestUtils.templateRequest(source), null, timeout, null)); @@ -162,16 +187,34 @@ public void testParserValid() throws Exception { public void testThatEmptyRequestBodyWorks() throws Exception { ArgumentCaptor requestCaptor = ArgumentCaptor.forClass(SearchRequest.class); PlainActionFuture searchFuture = PlainActionFuture.newFuture(); - SearchResponse searchResponse = new SearchResponse(InternalSearchResponse.empty(), "", 1, 1, 0, 1234, - ShardSearchFailure.EMPTY_ARRAY, SearchResponse.Clusters.EMPTY); + SearchResponse searchResponse = new SearchResponse( + InternalSearchResponse.empty(), + "", + 1, + 1, + 0, + 1234, + ShardSearchFailure.EMPTY_ARRAY, + SearchResponse.Clusters.EMPTY + ); searchFuture.onResponse(searchResponse); when(client.search(requestCaptor.capture())).thenReturn(searchFuture); - try (XContentBuilder builder = jsonBuilder().startObject().startObject("request") - .startArray("indices").value("foo").endArray().endObject().endObject(); - XContentParser parser = XContentFactory.xContent(XContentType.JSON) - .createParser(NamedXContentRegistry.EMPTY, - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, BytesReference.bytes(builder).streamInput())) { + try ( + XContentBuilder builder = jsonBuilder().startObject() + .startObject("request") + .startArray("indices") + .value("foo") + .endArray() + .endObject() + .endObject(); + XContentParser parser = XContentFactory.xContent(XContentType.JSON) + .createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + BytesReference.bytes(builder).streamInput() + ) + ) { parser.nextToken(); // advance past the first starting object diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/integration/SearchTransformTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/integration/SearchTransformTests.java index b6a501902be36..859732e61a0d4 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/integration/SearchTransformTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/integration/SearchTransformTests.java @@ -10,8 +10,6 @@ import org.elasticsearch.client.Client; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.script.MockScriptEngine; import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptEngine; @@ -19,6 +17,8 @@ import org.elasticsearch.script.ScriptService; import org.elasticsearch.script.ScriptType; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.watcher.common.text.TextTemplate; import org.elasticsearch.xpack.watcher.transform.search.ExecutableSearchTransform; import org.elasticsearch.xpack.watcher.transform.search.SearchTransform; @@ -54,12 +54,7 @@ public void testParser() throws Exception { builder.field("template", template); } - builder.startObject("body") - .startObject("query") - .startObject("match_all") - .endObject() - .endObject() - .endObject(); + builder.startObject("body").startObject("query").startObject("match_all").endObject().endObject().endObject(); builder.endObject(); TimeValue readTimeout = randomBoolean() ? TimeValue.timeValueSeconds(randomInt(10)) : null; @@ -76,7 +71,7 @@ public void testParser() throws Exception { ScriptService scriptService = new ScriptService(Settings.EMPTY, engines, ScriptModule.CORE_CONTEXTS); Client client = mock(Client.class); - SearchTransformFactory transformFactory = new SearchTransformFactory(Settings.EMPTY, client, xContentRegistry(), scriptService); + SearchTransformFactory transformFactory = new SearchTransformFactory(Settings.EMPTY, client, xContentRegistry(), scriptService); ExecutableSearchTransform executable = transformFactory.parseExecutable("_id", parser); assertThat(executable, notNullValue()); @@ -89,8 +84,10 @@ public void testParser() throws Exception { assertThat(executable.transform().getRequest().getSearchType(), is(searchType)); } if (templateName != null) { - assertThat(executable.transform().getRequest().getTemplate(), - equalTo(new Script(ScriptType.INLINE, "mustache", "template1", Collections.emptyMap()))); + assertThat( + executable.transform().getRequest().getTemplate(), + equalTo(new Script(ScriptType.INLINE, "mustache", "template1", Collections.emptyMap())) + ); } assertThat(executable.transform().getRequest().getSearchSource().utf8ToString(), equalTo("{\"query\":{\"match_all\":{}}}")); assertThat(executable.transform().getTimeout(), equalTo(readTimeout)); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transform/chain/ChainTransformTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transform/chain/ChainTransformTests.java index 764d4641df61f..fbd5aed43447b 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transform/chain/ChainTransformTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transform/chain/ChainTransformTests.java @@ -9,10 +9,10 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext; import org.elasticsearch.xpack.core.watcher.transform.ExecutableTransform; import org.elasticsearch.xpack.core.watcher.transform.Transform; @@ -43,14 +43,17 @@ public class ChainTransformTests extends ESTestCase { public void testExecute() throws Exception { ChainTransform transform = new ChainTransform( - new NamedExecutableTransform.Transform("name1"), - new NamedExecutableTransform.Transform("name2"), - new NamedExecutableTransform.Transform("name3") + new NamedExecutableTransform.Transform("name1"), + new NamedExecutableTransform.Transform("name2"), + new NamedExecutableTransform.Transform("name3") + ); + ExecutableChainTransform executable = new ExecutableChainTransform( + transform, + logger, + new NamedExecutableTransform("name1"), + new NamedExecutableTransform("name2"), + new NamedExecutableTransform("name3") ); - ExecutableChainTransform executable = new ExecutableChainTransform(transform, logger, - new NamedExecutableTransform("name1"), - new NamedExecutableTransform("name2"), - new NamedExecutableTransform("name3")); WatchExecutionContext ctx = mock(WatchExecutionContext.class); Payload payload = new Payload.Simple(new HashMap<>()); @@ -82,14 +85,17 @@ public void testExecute() throws Exception { public void testExecuteFailure() throws Exception { ChainTransform transform = new ChainTransform( - new NamedExecutableTransform.Transform("name1"), - new NamedExecutableTransform.Transform("name2"), - new FailingExecutableTransform.Transform() + new NamedExecutableTransform.Transform("name1"), + new NamedExecutableTransform.Transform("name2"), + new FailingExecutableTransform.Transform() + ); + ExecutableChainTransform executable = new ExecutableChainTransform( + transform, + logger, + new NamedExecutableTransform("name1"), + new NamedExecutableTransform("name2"), + new FailingExecutableTransform(logger) ); - ExecutableChainTransform executable = new ExecutableChainTransform(transform, logger, - new NamedExecutableTransform("name1"), - new NamedExecutableTransform("name2"), - new FailingExecutableTransform(logger)); WatchExecutionContext ctx = mock(WatchExecutionContext.class); Payload payload = new Payload.Simple(new HashMap<>()); @@ -118,11 +124,25 @@ public void testParser() throws Exception { ChainTransformFactory transformParser = new ChainTransformFactory(registry); XContentBuilder builder = jsonBuilder().startArray() - .startObject().startObject("named").field("name", "name1").endObject().endObject() - .startObject().startObject("named").field("name", "name2").endObject().endObject() - .startObject().startObject("named").field("name", "name3").endObject().endObject() - .startObject().field("named", "name4").endObject() - .endArray(); + .startObject() + .startObject("named") + .field("name", "name1") + .endObject() + .endObject() + .startObject() + .startObject("named") + .field("name", "name2") + .endObject() + .endObject() + .startObject() + .startObject("named") + .field("name", "name3") + .endObject() + .endObject() + .startObject() + .field("named", "name4") + .endObject() + .endArray(); XContentParser parser = createParser(JsonXContent.jsonXContent, BytesReference.bytes(builder)); parser.nextToken(); @@ -141,8 +161,9 @@ private static List getNames(Payload payload) { return (List) payload.data().get("names"); } - private static class NamedExecutableTransform extends ExecutableTransform { + private static class NamedExecutableTransform extends ExecutableTransform< + NamedExecutableTransform.Transform, + NamedExecutableTransform.Result> { private static final String TYPE = "named"; NamedExecutableTransform(String name) { @@ -231,8 +252,9 @@ public NamedExecutableTransform createExecutable(Transform transform) { } } - private static class FailingExecutableTransform extends ExecutableTransform { + private static class FailingExecutableTransform extends ExecutableTransform< + FailingExecutableTransform.Transform, + FailingExecutableTransform.Result> { private static final String TYPE = "throwing"; FailingExecutableTransform(Logger logger) { diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transform/script/ScriptTransformTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transform/script/ScriptTransformTests.java index 260179ed3e873..f1f0871f1851d 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transform/script/ScriptTransformTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transform/script/ScriptTransformTests.java @@ -7,8 +7,6 @@ package org.elasticsearch.xpack.watcher.transform.script; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptContext; import org.elasticsearch.script.ScriptException; @@ -16,6 +14,8 @@ import org.elasticsearch.script.ScriptService; import org.elasticsearch.script.ScriptType; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext; import org.elasticsearch.xpack.core.watcher.transform.Transform; import org.elasticsearch.xpack.core.watcher.watch.Payload; @@ -149,16 +149,23 @@ public void testParserString() throws Exception { public void testScriptConditionParserBadScript() throws Exception { ScriptService scriptService = mock(ScriptService.class); String errorMessage = "expected error message"; - ScriptException scriptException = new ScriptException(errorMessage, new RuntimeException("foo"), - Collections.emptyList(), "whatever", "whatever"); + ScriptException scriptException = new ScriptException( + errorMessage, + new RuntimeException("foo"), + Collections.emptyList(), + "whatever", + "whatever" + ); when(scriptService.compile(anyObject(), eq(WatcherTransformScript.CONTEXT))).thenThrow(scriptException); ScriptTransformFactory transformFactory = new ScriptTransformFactory(scriptService); XContentBuilder builder = jsonBuilder().startObject() - .field(scriptTypeField(randomFrom(ScriptType.values())), "whatever") - .startObject("params").field("key", "value").endObject() - .endObject(); + .field(scriptTypeField(randomFrom(ScriptType.values())), "whatever") + .startObject("params") + .field("key", "value") + .endObject() + .endObject(); XContentParser parser = createParser(builder); parser.nextToken(); @@ -171,11 +178,12 @@ public void testScriptConditionParserBadLang() throws Exception { ScriptTransformFactory transformFactory = new ScriptTransformFactory(createScriptService()); String script = "return true"; XContentBuilder builder = jsonBuilder().startObject() - .field(scriptTypeField(ScriptType.INLINE), script) - .field("lang", "not_a_valid_lang") - .startObject("params").field("key", "value").endObject() - .endObject(); - + .field(scriptTypeField(ScriptType.INLINE), script) + .field("lang", "not_a_valid_lang") + .startObject("params") + .field("key", "value") + .endObject() + .endObject(); XContentParser parser = createParser(builder); parser.nextToken(); @@ -186,17 +194,17 @@ public void testScriptConditionParserBadLang() throws Exception { static String scriptTypeField(ScriptType type) { switch (type) { - case INLINE: return "source"; - case STORED: return "id"; + case INLINE: + return "source"; + case STORED: + return "id"; default: throw illegalArgument("unsupported script type [{}]", type); } } public static ScriptService createScriptService() throws Exception { - Settings settings = Settings.builder() - .put("path.home", createTempDir()) - .build(); + Settings settings = Settings.builder().put("path.home", createTempDir()).build(); Map> contexts = new HashMap<>(ScriptModule.CORE_CONTEXTS); contexts.put(WatcherTransformScript.CONTEXT.name, WatcherTransformScript.CONTEXT); contexts.put(Watcher.SCRIPT_TEMPLATE_CONTEXT.name, Watcher.SCRIPT_TEMPLATE_CONTEXT); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transport/action/QueryWatchesRequestTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transport/action/QueryWatchesRequestTests.java index 68ab7a102e031..fae597a85b371 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transport/action/QueryWatchesRequestTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transport/action/QueryWatchesRequestTests.java @@ -10,8 +10,6 @@ import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchModule; @@ -20,6 +18,8 @@ import org.elasticsearch.search.sort.SortBuilders; import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.test.AbstractSerializingTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.watcher.transport.actions.QueryWatchesAction; import java.io.IOException; @@ -56,7 +56,7 @@ protected QueryWatchesAction.Request createTestInstance() { SearchAfterBuilder searchAfter = null; if (randomBoolean()) { searchAfter = new SearchAfterBuilder(); - searchAfter.setSortValues(new Object[]{randomInt()}); + searchAfter.setSortValues(new Object[] { randomInt() }); } return new QueryWatchesAction.Request( randomBoolean() ? randomIntBetween(0, 10000) : null, diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transport/action/QueryWatchesResponseTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transport/action/QueryWatchesResponseTests.java index d0d973c102277..ec494c5e364c9 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transport/action/QueryWatchesResponseTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transport/action/QueryWatchesResponseTests.java @@ -8,13 +8,13 @@ package org.elasticsearch.xpack.watcher.transport.action; import org.elasticsearch.client.Client; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ContextParser; +import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.xpack.core.watcher.support.xcontent.WatcherParams; import org.elasticsearch.xpack.core.watcher.support.xcontent.WatcherXContentParser; import org.elasticsearch.xpack.core.watcher.support.xcontent.XContentSource; @@ -43,8 +43,13 @@ public class QueryWatchesResponseTests extends AbstractSerializingTestCase TEST_ITEM_PARSER = new ConstructingObjectParser<>( "query_watches_response_item", false, - (args, c) -> new QueryWatchesAction.Response.Item((String) args[0], (XContentSource) args[1], - (WatchStatus) args[2], (long) args[3], (long) args[4]) + (args, c) -> new QueryWatchesAction.Response.Item( + (String) args[0], + (XContentSource) args[1], + (WatchStatus) args[2], + (long) args[3], + (long) args[4] + ) ); static { @@ -92,12 +97,8 @@ protected QueryWatchesAction.Response createTestInstance() { for (int i = 0; i < numWatches; i++) { Watch watch = createWatch("_id + " + i); try (XContentBuilder builder = jsonBuilder()) { - watch.toXContent(builder, WatcherParams.builder() - .hideSecrets(true) - .includeStatus(false) - .build()); - items.add(new QueryWatchesAction.Response.Item(randomAlphaOfLength(4), - new XContentSource(builder), watch.status(), 1, 0)); + watch.toXContent(builder, WatcherParams.builder().hideSecrets(true).includeStatus(false).build()); + items.add(new QueryWatchesAction.Response.Item(randomAlphaOfLength(4), new XContentSource(builder), watch.status(), 1, 0)); } catch (IOException e) { throw new UncheckedIOException(e); } @@ -105,13 +106,15 @@ protected QueryWatchesAction.Response createTestInstance() { return new QueryWatchesAction.Response(numWatches + randomIntBetween(0, 100), items); } - private Watch createWatch(String watchId) { - return WatcherTestUtils.createTestWatch(watchId, + private Watch createWatch(String watchId) { + return WatcherTestUtils.createTestWatch( + watchId, mock(Client.class), mock(HttpClient.class), new EmailActionTests.NoopEmailService(), mock(WatcherSearchTemplateService.class), - logger); + logger + ); } @Override diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transport/action/WatchRequestValidationTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transport/action/WatchRequestValidationTests.java index 77e1b62cb538d..6553a36423b24 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transport/action/WatchRequestValidationTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transport/action/WatchRequestValidationTests.java @@ -9,13 +9,13 @@ import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.protocol.xpack.watcher.DeleteWatchRequest; import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.watcher.execution.ActionExecutionMode; import org.elasticsearch.xpack.core.watcher.transport.actions.ack.AckWatchRequest; import org.elasticsearch.xpack.core.watcher.transport.actions.activate.ActivateWatchRequest; -import org.elasticsearch.protocol.xpack.watcher.DeleteWatchRequest; import org.elasticsearch.xpack.core.watcher.transport.actions.execute.ExecuteWatchRequest; import org.elasticsearch.xpack.core.watcher.transport.actions.get.GetWatchRequest; @@ -26,7 +26,7 @@ public class WatchRequestValidationTests extends ESTestCase { - public void testAcknowledgeWatchInvalidWatchId() { + public void testAcknowledgeWatchInvalidWatchId() { ActionRequestValidationException e = new AckWatchRequest("id with whitespaces").validate(); assertThat(e, is(notNullValue())); assertThat(e.validationErrors(), hasItem("watch id contains whitespace")); @@ -40,13 +40,13 @@ public void testAcknowledgeWatchInvalidActionId() { public void testAcknowledgeWatchNullActionArray() { // need this to prevent some compilation errors, i.e. in 1.8.0_91 - String [] nullArray = null; + String[] nullArray = null; ActionRequestValidationException e = new AckWatchRequest("_id", nullArray).validate(); assertThat(e, is(nullValue())); } public void testAcknowledgeWatchNullActionId() { - ActionRequestValidationException e = new AckWatchRequest("_id", new String[] {null}).validate(); + ActionRequestValidationException e = new AckWatchRequest("_id", new String[] { null }).validate(); assertThat(e, is(notNullValue())); assertThat(e.validationErrors(), hasItem("action id may not be null")); } @@ -114,8 +114,10 @@ public void testExecuteWatchInvalidWatchId() { public void testExecuteWatchMissingWatchIdNoSource() { ActionRequestValidationException e = new ExecuteWatchRequest((String) null).validate(); assertThat(e, is(notNullValue())); - assertThat(e.validationErrors(), - hasItem("a watch execution request must either have a watch id or an inline watch source, but both are missing")); + assertThat( + e.validationErrors(), + hasItem("a watch execution request must either have a watch id or an inline watch source, but both are missing") + ); } public void testExecuteWatchInvalidActionId() { @@ -131,8 +133,10 @@ public void testExecuteWatchWatchIdAndSource() { request.setWatchSource(BytesArray.EMPTY, XContentType.JSON); ActionRequestValidationException e = request.validate(); assertThat(e, is(notNullValue())); - assertThat(e.validationErrors(), - hasItem("a watch execution request must either have a watch id or an inline watch source but not both")); + assertThat( + e.validationErrors(), + hasItem("a watch execution request must either have a watch id or an inline watch source but not both") + ); } public void testExecuteWatchSourceAndRecordExecution() { diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transport/action/execute/ExecuteWatchRequestTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transport/action/execute/ExecuteWatchRequestTests.java index 9031fc5d3d620..6019382120229 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transport/action/execute/ExecuteWatchRequestTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transport/action/execute/ExecuteWatchRequestTests.java @@ -9,8 +9,8 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.StreamInput; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.watcher.transport.actions.execute.ExecuteWatchRequest; import java.io.IOException; diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transport/action/put/PutWatchSerializationTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transport/action/put/PutWatchSerializationTests.java index 7d9f4d36980d2..b6fac1a9e2710 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transport/action/put/PutWatchSerializationTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transport/action/put/PutWatchSerializationTests.java @@ -10,10 +10,10 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.lucene.uid.Versions; -import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.xcontent.json.JsonXContent; import static org.hamcrest.Matchers.is; @@ -25,9 +25,9 @@ public void testPutWatchSerialization() throws Exception { request.setId(randomAlphaOfLength(10)); request.setActive(randomBoolean()); request.setSource( - new BytesArray(Strings.toString(JsonXContent.contentBuilder().startObject().field("foo", - randomAlphaOfLength(20)).endObject())), - XContentType.JSON); + new BytesArray(Strings.toString(JsonXContent.contentBuilder().startObject().field("foo", randomAlphaOfLength(20)).endObject())), + XContentType.JSON + ); BytesStreamOutput streamOutput = new BytesStreamOutput(); request.writeTo(streamOutput); @@ -45,9 +45,9 @@ public void testPutWatchSerializationXContent() throws Exception { request.setId(randomAlphaOfLength(10)); request.setActive(randomBoolean()); request.setSource( - new BytesArray(Strings.toString(JsonXContent.contentBuilder().startObject().field("foo", - randomAlphaOfLength(20)).endObject())), - XContentType.JSON); + new BytesArray(Strings.toString(JsonXContent.contentBuilder().startObject().field("foo", randomAlphaOfLength(20)).endObject())), + XContentType.JSON + ); assertEquals(XContentType.JSON, request.xContentType()); BytesStreamOutput streamOutput = new BytesStreamOutput(); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transport/actions/TransportAckWatchActionTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transport/actions/TransportAckWatchActionTests.java index 23d3386f48181..2b0d5e781e8e5 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transport/actions/TransportAckWatchActionTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transport/actions/TransportAckWatchActionTests.java @@ -48,7 +48,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -@SuppressWarnings({"unchecked", "rawtypes"}) +@SuppressWarnings({ "unchecked", "rawtypes" }) public class TransportAckWatchActionTests extends ESTestCase { private TransportAckWatchAction action; @@ -63,23 +63,48 @@ public void setupAction() { WatchParser watchParser = mock(WatchParser.class); client = mock(Client.class); when(client.threadPool()).thenReturn(threadPool); - action = new TransportAckWatchAction(transportService, new ActionFilters(Collections.emptySet()), - new ClockHolder(Clock.systemUTC()), TestUtils.newTestLicenseState(), watchParser, client); + action = new TransportAckWatchAction( + transportService, + new ActionFilters(Collections.emptySet()), + new ClockHolder(Clock.systemUTC()), + TestUtils.newTestLicenseState(), + watchParser, + client + ); } public void testWatchNotFound() { String watchId = "my_watch_id"; doAnswer(invocation -> { ActionListener listener = (ActionListener) invocation.getArguments()[1]; - listener.onResponse(new GetResponse(new GetResult(Watch.INDEX, watchId, UNASSIGNED_SEQ_NO, - 0, -1, false, BytesArray.EMPTY, Collections.emptyMap(), Collections.emptyMap()))); + listener.onResponse( + new GetResponse( + new GetResult( + Watch.INDEX, + watchId, + UNASSIGNED_SEQ_NO, + 0, + -1, + false, + BytesArray.EMPTY, + Collections.emptyMap(), + Collections.emptyMap() + ) + ) + ); return null; }).when(client).get(anyObject(), anyObject()); doAnswer(invocation -> { ContextPreservingActionListener listener = (ContextPreservingActionListener) invocation.getArguments()[2]; - listener.onResponse(new WatcherStatsResponse(new ClusterName("clusterName"), new WatcherMetadata(false), - Collections.emptyList(), Collections.emptyList())); + listener.onResponse( + new WatcherStatsResponse( + new ClusterName("clusterName"), + new WatcherMetadata(false), + Collections.emptyList(), + Collections.emptyList() + ) + ); return null; }).when(client).execute(eq(WatcherStatsAction.INSTANCE), anyObject(), anyObject()); @@ -102,8 +127,14 @@ public void testThatWatchCannotBeAckedWhileRunning() { WatchExecutionSnapshot snapshot = mock(WatchExecutionSnapshot.class); when(snapshot.watchId()).thenReturn(watchId); node.setSnapshots(Collections.singletonList(snapshot)); - listener.onResponse(new WatcherStatsResponse(new ClusterName("clusterName"), - new WatcherMetadata(false), Collections.singletonList(node), Collections.emptyList())); + listener.onResponse( + new WatcherStatsResponse( + new ClusterName("clusterName"), + new WatcherMetadata(false), + Collections.singletonList(node), + Collections.emptyList() + ) + ); return null; }).when(client).execute(eq(WatcherStatsAction.INSTANCE), anyObject(), anyObject()); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transport/actions/TransportPutWatchActionTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transport/actions/TransportPutWatchActionTests.java index b8a5662a483be..1cbac0da86be8 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transport/actions/TransportPutWatchActionTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transport/actions/TransportPutWatchActionTests.java @@ -77,11 +77,18 @@ public void setupAction() throws Exception { return null; }).when(client).execute(any(), any(), any()); - action = new TransportPutWatchAction(transportService, threadPool, new ActionFilters(Collections.emptySet()), - new ClockHolder(new ClockMock()), TestUtils.newTestLicenseState(), parser, client); + action = new TransportPutWatchAction( + transportService, + threadPool, + new ActionFilters(Collections.emptySet()), + new ClockHolder(new ClockMock()), + TestUtils.newTestLicenseState(), + parser, + client + ); } - @SuppressWarnings({"unchecked", "rawtypes"}) + @SuppressWarnings({ "unchecked", "rawtypes" }) public void testHeadersAreFilteredWhenPuttingWatches() throws Exception { // set up threadcontext with some arbitrary info String headerName = randomFrom(ClientHelper.SECURITY_HEADER_FILTERS); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transport/actions/TransportWatcherStatsActionTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transport/actions/TransportWatcherStatsActionTests.java index cd83a0674f489..9abbd69fcf961 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transport/actions/TransportWatcherStatsActionTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transport/actions/TransportWatcherStatsActionTests.java @@ -14,13 +14,13 @@ import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.xcontent.ToXContent; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.rest.yaml.ObjectPath; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.xpack.core.watcher.WatcherState; import org.elasticsearch.xpack.core.watcher.common.stats.Counters; import org.elasticsearch.xpack.core.watcher.transport.actions.stats.WatcherStatsRequest; @@ -79,8 +79,15 @@ public void setupTransportAction() { secondTriggerServiceStats.inc("foo.bar.baz", 1024); when(triggerService.stats()).thenReturn(firstTriggerServiceStats, secondTriggerServiceStats); - action = new TransportWatcherStatsAction(transportService, clusterService, threadPool, new - ActionFilters(Collections.emptySet()), watcherLifeCycleService, executionService, triggerService); + action = new TransportWatcherStatsAction( + transportService, + clusterService, + threadPool, + new ActionFilters(Collections.emptySet()), + watcherLifeCycleService, + executionService, + triggerService + ); } public void testWatcherStats() throws Exception { @@ -89,8 +96,7 @@ public void testWatcherStats() throws Exception { WatcherStatsResponse.Node nodeResponse1 = action.nodeOperation(new WatcherStatsRequest.Node(request), null); WatcherStatsResponse.Node nodeResponse2 = action.nodeOperation(new WatcherStatsRequest.Node(request), null); - WatcherStatsResponse response = action.newResponse(request, - Arrays.asList(nodeResponse1, nodeResponse2), Collections.emptyList()); + WatcherStatsResponse response = action.newResponse(request, Arrays.asList(nodeResponse1, nodeResponse2), Collections.emptyList()); assertThat(response.getWatchesCount(), is(40L)); try (XContentBuilder builder = jsonBuilder()) { diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/ScheduleTriggerEngineMock.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/ScheduleTriggerEngineMock.java index 49f161efeced6..2f25a858b634d 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/ScheduleTriggerEngineMock.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/ScheduleTriggerEngineMock.java @@ -47,8 +47,8 @@ public ScheduleTrigger parseTrigger(String context, XContentParser parser) throw } @Override - public ScheduleTriggerEvent parseTriggerEvent(TriggerService service, String watchId, String context, - XContentParser parser) throws IOException { + public ScheduleTriggerEvent parseTriggerEvent(TriggerService service, String watchId, String context, XContentParser parser) + throws IOException { return ScheduleTriggerEvent.parse(parser, watchId, context, clock); } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/TriggerServiceTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/TriggerServiceTests.java index 7884fb158a03c..f52170191ac50 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/TriggerServiceTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/TriggerServiceTests.java @@ -144,7 +144,7 @@ private Watch createWatch(String id) { return watch; } - @SuppressWarnings({"rawtypes", "unchecked"}) + @SuppressWarnings({ "rawtypes", "unchecked" }) private void setInput(Watch watch) { ExecutableInput noneInput = new ExecutableNoneInput(); when(watch.input()).thenReturn(noneInput); @@ -161,7 +161,7 @@ private void setCondition(Watch watch, String type) { when(watch.condition()).thenReturn(condition); } - @SuppressWarnings({"rawtypes", "unchecked"}) + @SuppressWarnings({ "rawtypes", "unchecked" }) private void addAction(Watch watch, String type, String condition, String transform) { List actions = watch.actions(); ArrayList newActions = new ArrayList<>(actions); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/CronScheduleTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/CronScheduleTests.java index 33c281ad54515..b4d3ca67f92d4 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/CronScheduleTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/CronScheduleTests.java @@ -43,11 +43,7 @@ public void testParseSingle() throws Exception { } public void testParseMultiple() throws Exception { - XContentBuilder builder = jsonBuilder().value(new String[] { - "0 0/1 * * * ?", - "0 0/2 * * * ?", - "0 0/3 * * * ?" - }); + XContentBuilder builder = jsonBuilder().value(new String[] { "0 0/1 * * * ?", "0 0/2 * * * ?", "0 0/3 * * * ?" }); BytesReference bytes = BytesReference.bytes(builder); XContentParser parser = createParser(JsonXContent.jsonXContent, bytes); parser.nextToken(); @@ -89,8 +85,13 @@ public void testParseInvalidEmpty() throws Exception { new CronSchedule.Parser().parse(parser); fail("Expected ElasticsearchParseException"); } catch (ElasticsearchParseException e) { - assertThat(e.getMessage(), is("could not parse [cron] schedule. expected either a cron string value or an array of cron " + - "string values, but found [null]")); + assertThat( + e.getMessage(), + is( + "could not parse [cron] schedule. expected either a cron string value or an array of cron " + + "string values, but found [null]" + ) + ); } } @@ -103,8 +104,13 @@ public void testParseInvalidObject() throws Exception { new CronSchedule.Parser().parse(parser); fail("Expected ElasticsearchParseException"); } catch (ElasticsearchParseException e) { - assertThat(e.getMessage(), is("could not parse [cron] schedule. expected either a cron string value or an array of cron " + - "string values, but found [START_OBJECT]")); + assertThat( + e.getMessage(), + is( + "could not parse [cron] schedule. expected either a cron string value or an array of cron " + + "string values, but found [START_OBJECT]" + ) + ); } } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/DailyScheduleTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/DailyScheduleTests.java index e1efc655d0c6e..97539c5ed4683 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/DailyScheduleTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/DailyScheduleTests.java @@ -73,13 +73,12 @@ public void testParserEmpty() throws Exception { public void testParserSingleTimeObject() throws Exception { DayTimes time = validDayTime(); - XContentBuilder builder = jsonBuilder() - .startObject() - .startObject("at") - .array("hour", time.hour()) - .array("minute", time.minute()) - .endObject() - .endObject(); + XContentBuilder builder = jsonBuilder().startObject() + .startObject("at") + .array("hour", time.hour()) + .array("minute", time.minute()) + .endObject() + .endObject(); BytesReference bytes = BytesReference.bytes(builder); XContentParser parser = createParser(JsonXContent.jsonXContent, bytes); parser.nextToken(); // advancing to the start object @@ -91,13 +90,12 @@ public void testParserSingleTimeObject() throws Exception { public void testParserSingleTimeObjectInvalid() throws Exception { HourAndMinute time = invalidDayTime(); - XContentBuilder builder = jsonBuilder() - .startObject() - .startObject("at") - .field("hour", time.hour) - .field("minute", time.minute) - .endObject() - .endObject(); + XContentBuilder builder = jsonBuilder().startObject() + .startObject("at") + .field("hour", time.hour) + .field("minute", time.minute) + .endObject() + .endObject(); BytesReference bytes = BytesReference.bytes(builder); XContentParser parser = createParser(JsonXContent.jsonXContent, bytes); parser.nextToken(); // advancing to the start object @@ -111,10 +109,7 @@ public void testParserSingleTimeObjectInvalid() throws Exception { public void testParserSingleTimeString() throws Exception { String timeStr = validDayTimeStr(); - XContentBuilder builder = jsonBuilder() - .startObject() - .field("at", timeStr) - .endObject(); + XContentBuilder builder = jsonBuilder().startObject().field("at", timeStr).endObject(); BytesReference bytes = BytesReference.bytes(builder); XContentParser parser = createParser(JsonXContent.jsonXContent, bytes); parser.nextToken(); // advancing to the start object @@ -125,10 +120,7 @@ public void testParserSingleTimeString() throws Exception { } public void testParserSingleTimeStringInvalid() throws Exception { - XContentBuilder builder = jsonBuilder() - .startObject() - .field("at", invalidDayTimeStr()) - .endObject(); + XContentBuilder builder = jsonBuilder().startObject().field("at", invalidDayTimeStr()).endObject(); BytesReference bytes = BytesReference.bytes(builder); XContentParser parser = createParser(JsonXContent.jsonXContent, bytes); parser.nextToken(); // advancing to the start object @@ -142,10 +134,7 @@ public void testParserSingleTimeStringInvalid() throws Exception { public void testParserMultipleTimesObjects() throws Exception { DayTimes[] times = validDayTimesFromNumbers(); - XContentBuilder builder = jsonBuilder() - .startObject() - .array("at", (Object[]) times) - .endObject(); + XContentBuilder builder = jsonBuilder().startObject().array("at", (Object[]) times).endObject(); BytesReference bytes = BytesReference.bytes(builder); XContentParser parser = createParser(JsonXContent.jsonXContent, bytes); parser.nextToken(); // advancing to the start object @@ -159,10 +148,7 @@ public void testParserMultipleTimesObjects() throws Exception { public void testParserMultipleTimesObjectsInvalid() throws Exception { HourAndMinute[] times = invalidDayTimes(); - XContentBuilder builder = jsonBuilder() - .startObject() - .array("at", (Object[]) times) - .endObject(); + XContentBuilder builder = jsonBuilder().startObject().array("at", (Object[]) times).endObject(); BytesReference bytes = BytesReference.bytes(builder); XContentParser parser = createParser(JsonXContent.jsonXContent, bytes); parser.nextToken(); // advancing to the start object @@ -176,10 +162,7 @@ public void testParserMultipleTimesObjectsInvalid() throws Exception { public void testParserMultipleTimesStrings() throws Exception { DayTimes[] times = validDayTimesFromStrings(); - XContentBuilder builder = jsonBuilder() - .startObject() - .array("at", (Object[]) times) - .endObject(); + XContentBuilder builder = jsonBuilder().startObject().array("at", (Object[]) times).endObject(); BytesReference bytes = BytesReference.bytes(builder); XContentParser parser = createParser(JsonXContent.jsonXContent, bytes); parser.nextToken(); // advancing to the start object @@ -193,10 +176,7 @@ public void testParserMultipleTimesStrings() throws Exception { public void testParserMultipleTimesStringsInvalid() throws Exception { String[] times = invalidDayTimesAsStrings(); - XContentBuilder builder = jsonBuilder() - .startObject() - .array("at", times) - .endObject(); + XContentBuilder builder = jsonBuilder().startObject().array("at", times).endObject(); BytesReference bytes = BytesReference.bytes(builder); XContentParser parser = createParser(JsonXContent.jsonXContent, bytes); parser.nextToken(); // advancing to the start object diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/HourlyScheduleTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/HourlyScheduleTests.java index cc2b4c246d484..1d997bf7116c9 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/HourlyScheduleTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/HourlyScheduleTests.java @@ -84,10 +84,7 @@ public void testParserEmpty() throws Exception { public void testParserSingleMinuteNumber() throws Exception { int minute = validMinute(); - XContentBuilder builder = jsonBuilder() - .startObject() - .field("minute", minute) - .endObject(); + XContentBuilder builder = jsonBuilder().startObject().field("minute", minute).endObject(); BytesReference bytes = BytesReference.bytes(builder); XContentParser parser = createParser(JsonXContent.jsonXContent, bytes); parser.nextToken(); // advancing to the start object @@ -98,10 +95,7 @@ public void testParserSingleMinuteNumber() throws Exception { } public void testParserSingleMinuteNumberInvalid() throws Exception { - XContentBuilder builder = jsonBuilder() - .startObject() - .field("minute", invalidMinute()) - .endObject(); + XContentBuilder builder = jsonBuilder().startObject().field("minute", invalidMinute()).endObject(); BytesReference bytes = BytesReference.bytes(builder); XContentParser parser = createParser(JsonXContent.jsonXContent, bytes); parser.nextToken(); // advancing to the start object @@ -115,10 +109,7 @@ public void testParserSingleMinuteNumberInvalid() throws Exception { public void testParserSingleMinuteString() throws Exception { int minute = validMinute(); - XContentBuilder builder = jsonBuilder() - .startObject() - .field("minute", String.valueOf(minute)) - .endObject(); + XContentBuilder builder = jsonBuilder().startObject().field("minute", String.valueOf(minute)).endObject(); BytesReference bytes = BytesReference.bytes(builder); XContentParser parser = createParser(JsonXContent.jsonXContent, bytes); parser.nextToken(); // advancing to the start object @@ -129,10 +120,7 @@ public void testParserSingleMinuteString() throws Exception { } public void testParserSingleMinuteStringInvalid() throws Exception { - XContentBuilder builder = jsonBuilder() - .startObject() - .field("minute", String.valueOf(invalidMinute())) - .endObject(); + XContentBuilder builder = jsonBuilder().startObject().field("minute", String.valueOf(invalidMinute())).endObject(); BytesReference bytes = BytesReference.bytes(builder); XContentParser parser = createParser(JsonXContent.jsonXContent, bytes); parser.nextToken(); // advancing to the start object @@ -146,10 +134,7 @@ public void testParserSingleMinuteStringInvalid() throws Exception { public void testParserMultipleMinutesNumbers() throws Exception { int[] minutes = validMinutes(); - XContentBuilder builder = jsonBuilder() - .startObject() - .field("minute", minutes) - .endObject(); + XContentBuilder builder = jsonBuilder().startObject().field("minute", minutes).endObject(); BytesReference bytes = BytesReference.bytes(builder); XContentParser parser = createParser(JsonXContent.jsonXContent, bytes); parser.nextToken(); // advancing to the start object @@ -164,10 +149,7 @@ public void testParserMultipleMinutesNumbers() throws Exception { public void testParserMultipleMinutesNumbersInvalid() throws Exception { int[] minutes = invalidMinutes(); - XContentBuilder builder = jsonBuilder() - .startObject() - .field("minute", minutes) - .endObject(); + XContentBuilder builder = jsonBuilder().startObject().field("minute", minutes).endObject(); BytesReference bytes = BytesReference.bytes(builder); XContentParser parser = createParser(JsonXContent.jsonXContent, bytes); parser.nextToken(); // advancing to the start object @@ -181,10 +163,9 @@ public void testParserMultipleMinutesNumbersInvalid() throws Exception { public void testParserMultipleMinutesStrings() throws Exception { int[] minutes = validMinutes(); - XContentBuilder builder = jsonBuilder() - .startObject() - .field("minute", Arrays.stream(minutes).mapToObj(Integer::toString).collect(Collectors.toList())) - .endObject(); + XContentBuilder builder = jsonBuilder().startObject() + .field("minute", Arrays.stream(minutes).mapToObj(Integer::toString).collect(Collectors.toList())) + .endObject(); BytesReference bytes = BytesReference.bytes(builder); XContentParser parser = createParser(JsonXContent.jsonXContent, bytes); parser.nextToken(); // advancing to the start object @@ -200,10 +181,9 @@ public void testParserMultipleMinutesStrings() throws Exception { public void testParserMultipleMinutesStringsInvalid() throws Exception { int[] minutes = invalidMinutes(); - XContentBuilder builder = jsonBuilder() - .startObject() - .field("minute", Arrays.stream(minutes).mapToObj(Integer::toString).collect(Collectors.toList())) - .endObject(); + XContentBuilder builder = jsonBuilder().startObject() + .field("minute", Arrays.stream(minutes).mapToObj(Integer::toString).collect(Collectors.toList())) + .endObject(); BytesReference bytes = BytesReference.bytes(builder); XContentParser parser = createParser(JsonXContent.jsonXContent, bytes); parser.nextToken(); // advancing to the start object diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/IntervalScheduleTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/IntervalScheduleTests.java index 88644e77ae1d2..4a483a40e918d 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/IntervalScheduleTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/IntervalScheduleTests.java @@ -8,10 +8,10 @@ import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.json.JsonXContent; -import org.elasticsearch.test.ESTestCase; import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.Matchers.containsString; @@ -78,8 +78,10 @@ public void testParseInvalidObject() throws Exception { try { new IntervalSchedule.Parser().parse(parser); } catch (ElasticsearchParseException e) { - assertThat(e.getMessage(), - containsString("expected either a numeric value (millis) or a string value representing time value")); + assertThat( + e.getMessage(), + containsString("expected either a numeric value (millis) or a string value representing time value") + ); assertThat(e.getMessage(), containsString("found [START_OBJECT]")); } } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/MonthlyScheduleTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/MonthlyScheduleTests.java index dcf25ac8ea712..d27b6c843fc1e 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/MonthlyScheduleTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/MonthlyScheduleTests.java @@ -78,14 +78,13 @@ public void testParserEmpty() throws Exception { public void testParserSingleTime() throws Exception { DayTimes time = validDayTime(); Object day = randomDayOfMonth(); - XContentBuilder builder = jsonBuilder() - .startObject() - .field("on", day) - .startObject("at") - .array("hour", time.hour()) - .array("minute", time.minute()) - .endObject() - .endObject(); + XContentBuilder builder = jsonBuilder().startObject() + .field("on", day) + .startObject("at") + .array("hour", time.hour()) + .array("minute", time.minute()) + .endObject() + .endObject(); BytesReference bytes = BytesReference.bytes(builder); XContentParser parser = createParser(JsonXContent.jsonXContent, bytes); parser.nextToken(); // advancing to the start object @@ -100,14 +99,13 @@ public void testParserSingleTime() throws Exception { public void testParserSingleTimeInvalid() throws Exception { HourAndMinute time = invalidDayTime(); - XContentBuilder builder = jsonBuilder() - .startObject() - .field("on", randomBoolean() ? invalidDayOfMonth() : randomDayOfMonth()) - .startObject("at") - .field("hour", time.hour) - .field("minute", time.minute) - .endObject() - .endObject(); + XContentBuilder builder = jsonBuilder().startObject() + .field("on", randomBoolean() ? invalidDayOfMonth() : randomDayOfMonth()) + .startObject("at") + .field("hour", time.hour) + .field("minute", time.minute) + .endObject() + .endObject(); BytesReference bytes = BytesReference.bytes(builder); XContentParser parser = createParser(JsonXContent.jsonXContent, bytes); parser.nextToken(); // advancing to the start object @@ -135,11 +133,7 @@ public void testParserMultipleTimes() throws Exception { public void testParserMultipleTimesInvalid() throws Exception { HourAndMinute[] times = invalidDayTimes(); - XContentBuilder builder = jsonBuilder() - .startObject() - .field("on", randomDayOfMonth()) - .array("at", (Object[]) times) - .endObject(); + XContentBuilder builder = jsonBuilder().startObject().field("on", randomDayOfMonth()).array("at", (Object[]) times).endObject(); BytesReference bytes = BytesReference.bytes(builder); XContentParser parser = createParser(JsonXContent.jsonXContent, bytes); parser.nextToken(); // advancing to the start object diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/ScheduleRegistryTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/ScheduleRegistryTests.java index 49f5898bd22c3..7fc4739c342f1 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/ScheduleRegistryTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/ScheduleRegistryTests.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.xpack.watcher.trigger.schedule; - import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -39,10 +38,7 @@ public void init() throws Exception { public void testParserInterval() throws Exception { IntervalSchedule interval = randomIntervalSchedule(); - XContentBuilder builder = jsonBuilder() - .startObject() - .field(IntervalSchedule.TYPE, interval) - .endObject(); + XContentBuilder builder = jsonBuilder().startObject().field(IntervalSchedule.TYPE, interval).endObject(); BytesReference bytes = BytesReference.bytes(builder); XContentParser parser = createParser(JsonXContent.jsonXContent, bytes); parser.nextToken(); @@ -53,13 +49,8 @@ public void testParserInterval() throws Exception { } public void testParseCron() throws Exception { - Object cron = randomBoolean() ? - Schedules.cron("* 0/5 * * * ?") : - Schedules.cron("* 0/2 * * * ?", "* 0/3 * * * ?", "* 0/5 * * * ?"); - XContentBuilder builder = jsonBuilder() - .startObject() - .field(CronSchedule.TYPE, cron) - .endObject(); + Object cron = randomBoolean() ? Schedules.cron("* 0/5 * * * ?") : Schedules.cron("* 0/2 * * * ?", "* 0/3 * * * ?", "* 0/5 * * * ?"); + XContentBuilder builder = jsonBuilder().startObject().field(CronSchedule.TYPE, cron).endObject(); BytesReference bytes = BytesReference.bytes(builder); XContentParser parser = createParser(JsonXContent.jsonXContent, bytes); parser.nextToken(); @@ -71,10 +62,7 @@ public void testParseCron() throws Exception { public void testParseHourly() throws Exception { HourlySchedule hourly = randomHourlySchedule(); - XContentBuilder builder = jsonBuilder() - .startObject() - .field(HourlySchedule.TYPE, hourly) - .endObject(); + XContentBuilder builder = jsonBuilder().startObject().field(HourlySchedule.TYPE, hourly).endObject(); BytesReference bytes = BytesReference.bytes(builder); XContentParser parser = createParser(JsonXContent.jsonXContent, bytes); parser.nextToken(); @@ -86,10 +74,7 @@ public void testParseHourly() throws Exception { public void testParseDaily() throws Exception { DailySchedule daily = randomDailySchedule(); - XContentBuilder builder = jsonBuilder() - .startObject() - .field(DailySchedule.TYPE, daily) - .endObject(); + XContentBuilder builder = jsonBuilder().startObject().field(DailySchedule.TYPE, daily).endObject(); BytesReference bytes = BytesReference.bytes(builder); XContentParser parser = createParser(JsonXContent.jsonXContent, bytes); parser.nextToken(); @@ -101,10 +86,7 @@ public void testParseDaily() throws Exception { public void testParseWeekly() throws Exception { WeeklySchedule weekly = randomWeeklySchedule(); - XContentBuilder builder = jsonBuilder() - .startObject() - .field(WeeklySchedule.TYPE, weekly) - .endObject(); + XContentBuilder builder = jsonBuilder().startObject().field(WeeklySchedule.TYPE, weekly).endObject(); BytesReference bytes = BytesReference.bytes(builder); XContentParser parser = createParser(JsonXContent.jsonXContent, bytes); parser.nextToken(); @@ -116,10 +98,7 @@ public void testParseWeekly() throws Exception { public void testParseMonthly() throws Exception { MonthlySchedule monthly = randomMonthlySchedule(); - XContentBuilder builder = jsonBuilder() - .startObject() - .field(MonthlySchedule.TYPE, monthly) - .endObject(); + XContentBuilder builder = jsonBuilder().startObject().field(MonthlySchedule.TYPE, monthly).endObject(); BytesReference bytes = BytesReference.bytes(builder); XContentParser parser = createParser(JsonXContent.jsonXContent, bytes); parser.nextToken(); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/ScheduleTestCase.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/ScheduleTestCase.java index f1dff3fa0d7a4..6406838c2421b 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/ScheduleTestCase.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/ScheduleTestCase.java @@ -7,9 +7,9 @@ package org.elasticsearch.xpack.watcher.trigger.schedule; import org.elasticsearch.common.util.CollectionUtils; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.scheduler.Cron; import org.elasticsearch.xpack.watcher.trigger.schedule.support.DayOfWeek; import org.elasticsearch.xpack.watcher.trigger.schedule.support.DayTimes; @@ -45,45 +45,64 @@ protected static String[] expressions(Cron[] crons) { protected static MonthlySchedule randomMonthlySchedule() { switch (randomIntBetween(1, 4)) { - case 1: return monthly().build(); - case 2: return monthly().time(MonthTimes.builder().atMidnight()).build(); - case 3: return monthly().time(MonthTimes.builder().on(randomIntBetween(1, 31)).atMidnight()).build(); - default: return new MonthlySchedule(validMonthTimes()); + case 1: + return monthly().build(); + case 2: + return monthly().time(MonthTimes.builder().atMidnight()).build(); + case 3: + return monthly().time(MonthTimes.builder().on(randomIntBetween(1, 31)).atMidnight()).build(); + default: + return new MonthlySchedule(validMonthTimes()); } } protected static WeeklySchedule randomWeeklySchedule() { switch (randomIntBetween(1, 4)) { - case 1: return weekly().build(); - case 2: return weekly().time(WeekTimes.builder().atMidnight()).build(); - case 3: return weekly().time(WeekTimes.builder().on(DayOfWeek.THURSDAY).atMidnight()).build(); - default: return new WeeklySchedule(validWeekTimes()); + case 1: + return weekly().build(); + case 2: + return weekly().time(WeekTimes.builder().atMidnight()).build(); + case 3: + return weekly().time(WeekTimes.builder().on(DayOfWeek.THURSDAY).atMidnight()).build(); + default: + return new WeeklySchedule(validWeekTimes()); } } protected static DailySchedule randomDailySchedule() { switch (randomIntBetween(1, 4)) { - case 1: return daily().build(); - case 2: return daily().atMidnight().build(); - case 3: return daily().atNoon().build(); - default: return new DailySchedule(validDayTimes()); + case 1: + return daily().build(); + case 2: + return daily().atMidnight().build(); + case 3: + return daily().atNoon().build(); + default: + return new DailySchedule(validDayTimes()); } } protected static HourlySchedule randomHourlySchedule() { switch (randomIntBetween(1, 4)) { - case 1: return hourly().build(); - case 2: return hourly().minutes(randomIntBetween(0, 59)).build(); - case 3: return hourly(randomIntBetween(0, 59)); - default: return hourly().minutes(validMinutes()).build(); + case 1: + return hourly().build(); + case 2: + return hourly().minutes(randomIntBetween(0, 59)).build(); + case 3: + return hourly(randomIntBetween(0, 59)); + default: + return hourly().minutes(validMinutes()).build(); } } protected static IntervalSchedule randomIntervalSchedule() { switch (randomIntBetween(1, 3)) { - case 1: return interval(randomInterval().toString()); - case 2: return interval(randomIntBetween(1, 100), randomIntervalUnit()); - default: return new IntervalSchedule(randomInterval()); + case 1: + return interval(randomInterval().toString()); + case 2: + return interval(randomIntBetween(1, 100), randomIntervalUnit()); + default: + return new IntervalSchedule(randomInterval()); } } @@ -144,7 +163,7 @@ protected static WeekTimes[] validWeekTimes() { } protected static EnumSet randomDaysOfWeek() { - int count = randomIntBetween(1, DayOfWeek.values().length-1); + int count = randomIntBetween(1, DayOfWeek.values().length - 1); Set days = new HashSet<>(); for (int i = 0; i < count; i++) { days.add(DayOfWeek.values()[randomIntBetween(0, count)]); @@ -210,9 +229,7 @@ protected static int dayOfMonthToInt(Object dom) { } protected static Object invalidDayOfMonth() { - return randomBoolean() ? - randomAlphaOfLength(5) : - randomBoolean() ? randomIntBetween(-30, -1) : randomIntBetween(33, 45); + return randomBoolean() ? randomAlphaOfLength(5) : randomBoolean() ? randomIntBetween(-30, -1) : randomIntBetween(33, 45); } protected static DayTimes validDayTime() { @@ -234,11 +251,9 @@ protected static String validDayTimeStr() { } protected static HourAndMinute invalidDayTime() { - return randomBoolean() ? - new HourAndMinute(invalidHour(), invalidMinute()) : - randomBoolean() ? - new HourAndMinute(validHour(), invalidMinute()) : - new HourAndMinute(invalidHour(), validMinute()); + return randomBoolean() ? new HourAndMinute(invalidHour(), invalidMinute()) + : randomBoolean() ? new HourAndMinute(validHour(), invalidMinute()) + : new HourAndMinute(invalidHour(), validMinute()); } protected static String invalidDayTimeStr() { @@ -372,9 +387,9 @@ static class HourAndMinute implements ToXContentObject { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { return builder.startObject() - .field(DayTimes.HOUR_FIELD.getPreferredName(), hour) - .field(DayTimes.MINUTE_FIELD.getPreferredName(), minute) - .endObject(); + .field(DayTimes.HOUR_FIELD.getPreferredName(), hour) + .field(DayTimes.MINUTE_FIELD.getPreferredName(), minute) + .endObject(); } @Override diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/ScheduleTriggerEventTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/ScheduleTriggerEventTests.java index f343ffb7f60f7..ec85248dfba58 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/ScheduleTriggerEventTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/ScheduleTriggerEventTests.java @@ -6,11 +6,10 @@ */ package org.elasticsearch.xpack.watcher.trigger.schedule; - +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.ESTestCase; import java.time.Clock; import java.time.Instant; diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/WeeklyScheduleTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/WeeklyScheduleTests.java index cdbe002ef61b7..d326aa222c862 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/WeeklyScheduleTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/WeeklyScheduleTests.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.xpack.watcher.trigger.schedule; - import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; @@ -41,8 +40,17 @@ public void testSingleTime() throws Exception { String[] crons = expressions(schedule); assertThat(crons, arrayWithSize(time.times().length)); for (DayTimes dayTimes : time.times()) { - assertThat(crons, hasItemInArray("0 " + join(",", dayTimes.minute()) + " " + join(",", dayTimes.hour()) + " ? * " + - Strings.collectionToCommaDelimitedString(time.days()))); + assertThat( + crons, + hasItemInArray( + "0 " + + join(",", dayTimes.minute()) + + " " + + join(",", dayTimes.hour()) + + " ? * " + + Strings.collectionToCommaDelimitedString(time.days()) + ) + ); } } @@ -57,8 +65,17 @@ public void testMultipleTimes() throws Exception { assertThat(crons, arrayWithSize(count)); for (WeekTimes weekTimes : times) { for (DayTimes dayTimes : weekTimes.times()) { - assertThat(crons, hasItemInArray("0 " + join(",", dayTimes.minute()) + " " + join(",", dayTimes.hour()) + " ? * " + - Strings.collectionToCommaDelimitedString(weekTimes.days()))); + assertThat( + crons, + hasItemInArray( + "0 " + + join(",", dayTimes.minute()) + + " " + + join(",", dayTimes.hour()) + + " ? * " + + Strings.collectionToCommaDelimitedString(weekTimes.days()) + ) + ); } } } @@ -76,14 +93,13 @@ public void testParserEmpty() throws Exception { public void testParserSingleTime() throws Exception { DayTimes time = validDayTime(); - XContentBuilder builder = jsonBuilder() - .startObject() - .field("on", "mon") - .startObject("at") - .array("hour", time.hour()) - .array("minute", time.minute()) - .endObject() - .endObject(); + XContentBuilder builder = jsonBuilder().startObject() + .field("on", "mon") + .startObject("at") + .array("hour", time.hour()) + .array("minute", time.minute()) + .endObject() + .endObject(); BytesReference bytes = BytesReference.bytes(builder); XContentParser parser = createParser(JsonXContent.jsonXContent, bytes); parser.nextToken(); // advancing to the start object @@ -98,14 +114,13 @@ public void testParserSingleTime() throws Exception { public void testParserSingleTimeInvalid() throws Exception { HourAndMinute time = invalidDayTime(); - XContentBuilder builder = jsonBuilder() - .startObject() - .field("on", "mon") - .startObject("at") - .field("hour", time.hour) - .field("minute", time.minute) - .endObject() - .endObject(); + XContentBuilder builder = jsonBuilder().startObject() + .field("on", "mon") + .startObject("at") + .field("hour", time.hour) + .field("minute", time.minute) + .endObject() + .endObject(); BytesReference bytes = BytesReference.bytes(builder); XContentParser parser = createParser(JsonXContent.jsonXContent, bytes); parser.nextToken(); // advancing to the start object @@ -133,11 +148,7 @@ public void testParserMultipleTimes() throws Exception { public void testParserMultipleTimesObjectsInvalid() throws Exception { HourAndMinute[] times = invalidDayTimes(); - XContentBuilder builder = jsonBuilder() - .startObject() - .field("on", randomDaysOfWeek()) - .array("at", (Object[]) times) - .endObject(); + XContentBuilder builder = jsonBuilder().startObject().field("on", randomDaysOfWeek()).array("at", (Object[]) times).endObject(); BytesReference bytes = BytesReference.bytes(builder); XContentParser parser = createParser(JsonXContent.jsonXContent, bytes); parser.nextToken(); // advancing to the start object diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/YearlyScheduleTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/YearlyScheduleTests.java index 48a356d6a8d06..cf0500e81e655 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/YearlyScheduleTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/YearlyScheduleTests.java @@ -6,7 +6,6 @@ */ package org.elasticsearch.xpack.watcher.trigger.schedule; - import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; @@ -85,15 +84,14 @@ public void testParserSingleTime() throws Exception { DayTimes time = validDayTime(); Object day = randomDayOfMonth(); Object month = randomMonth(); - XContentBuilder builder = jsonBuilder() - .startObject() - .field("in", month) - .field("on", day) - .startObject("at") - .array("hour", time.hour()) - .array("minute", time.minute()) - .endObject() - .endObject(); + XContentBuilder builder = jsonBuilder().startObject() + .field("in", month) + .field("on", day) + .startObject("at") + .array("hour", time.hour()) + .array("minute", time.minute()) + .endObject() + .endObject(); BytesReference bytes = BytesReference.bytes(builder); XContentParser parser = createParser(JsonXContent.jsonXContent, bytes); parser.nextToken(); // advancing to the start object @@ -108,15 +106,14 @@ public void testParserSingleTime() throws Exception { public void testParserSingleTimeInvalid() throws Exception { HourAndMinute time = invalidDayTime(); - XContentBuilder builder = jsonBuilder() - .startObject() - .field("in", randomMonth()) - .field("on", randomBoolean() ? invalidDayOfMonth() : randomDayOfMonth()) - .startObject("at") - .field("hour", time.hour) - .field("minute", time.minute) - .endObject() - .endObject(); + XContentBuilder builder = jsonBuilder().startObject() + .field("in", randomMonth()) + .field("on", randomBoolean() ? invalidDayOfMonth() : randomDayOfMonth()) + .startObject("at") + .field("hour", time.hour) + .field("minute", time.minute) + .endObject() + .endObject(); BytesReference bytes = BytesReference.bytes(builder); XContentParser parser = createParser(JsonXContent.jsonXContent, bytes); parser.nextToken(); // advancing to the start object @@ -144,12 +141,11 @@ public void testParserMultipleTimes() throws Exception { public void testParserMultipleTimesInvalid() throws Exception { HourAndMinute[] times = invalidDayTimes(); - XContentBuilder builder = jsonBuilder() - .startObject() - .field("in", randomMonth()) - .field("on", randomDayOfMonth()) - .array("at", (Object[]) times) - .endObject(); + XContentBuilder builder = jsonBuilder().startObject() + .field("in", randomMonth()) + .field("on", randomDayOfMonth()) + .array("at", (Object[]) times) + .endObject(); BytesReference bytes = BytesReference.bytes(builder); XContentParser parser = createParser(JsonXContent.jsonXContent, bytes); parser.nextToken(); // advancing to the start object diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/engine/TickerScheduleEngineTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/engine/TickerScheduleEngineTests.java index a745164a7b8e1..8b7cfa75f9229 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/engine/TickerScheduleEngineTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/engine/TickerScheduleEngineTests.java @@ -126,17 +126,16 @@ public void accept(Iterable events) { }); int randomMinute = randomIntBetween(0, 59); - ZonedDateTime testNowTime = clock.instant().atZone(ZoneOffset.UTC) + ZonedDateTime testNowTime = clock.instant() + .atZone(ZoneOffset.UTC) .with(ChronoField.MINUTE_OF_HOUR, randomMinute) .with(ChronoField.SECOND_OF_MINUTE, 59); ZonedDateTime scheduledTime = testNowTime.plusSeconds(2); - logger.info("Setting current time to [{}], job execution time [{}]", testNowTime, - scheduledTime); + logger.info("Setting current time to [{}], job execution time [{}]", testNowTime, scheduledTime); clock.setTime(testNowTime); - engine.add(createWatch(name, daily().at(scheduledTime.getHour(), - scheduledTime.getMinute()).build())); + engine.add(createWatch(name, daily().at(scheduledTime.getHour(), scheduledTime.getMinute()).build())); advanceClockIfNeeded(scheduledTime); if (latch.await(5, TimeUnit.SECONDS) == false) { @@ -163,17 +162,17 @@ public void accept(Iterable events) { int randomHour = randomIntBetween(0, 23); int randomMinute = randomIntBetween(0, 59); - ZonedDateTime testNowTime = clock.instant().atZone(ZoneOffset.UTC) - .with(ChronoField.HOUR_OF_DAY, randomHour).with(ChronoField.MINUTE_OF_HOUR, randomMinute) + ZonedDateTime testNowTime = clock.instant() + .atZone(ZoneOffset.UTC) + .with(ChronoField.HOUR_OF_DAY, randomHour) + .with(ChronoField.MINUTE_OF_HOUR, randomMinute) .with(ChronoField.SECOND_OF_MINUTE, 59); ZonedDateTime scheduledTime = testNowTime.plusSeconds(2); - logger.info("Setting current time to [{}], job execution time [{}]", testNowTime, - scheduledTime); + logger.info("Setting current time to [{}], job execution time [{}]", testNowTime, scheduledTime); clock.setTime(testNowTime); - engine.add(createWatch(name, daily().at(scheduledTime.getHour(), - scheduledTime.getMinute()).build())); + engine.add(createWatch(name, daily().at(scheduledTime.getHour(), scheduledTime.getMinute()).build())); advanceClockIfNeeded(scheduledTime); if (latch.await(5, TimeUnit.SECONDS) == false) { @@ -200,25 +199,29 @@ public void accept(Iterable events) { int randomMinute = randomIntBetween(0, 59); int randomDay = randomIntBetween(1, 7); - ZonedDateTime testNowTime = clock.instant().atZone(ZoneOffset.UTC) - .with(ChronoField.DAY_OF_WEEK, randomDay) - .with(ChronoField.HOUR_OF_DAY, randomHour) - .with(ChronoField.MINUTE_OF_HOUR, randomMinute) - .with(ChronoField.SECOND_OF_MINUTE, 59); + ZonedDateTime testNowTime = clock.instant() + .atZone(ZoneOffset.UTC) + .with(ChronoField.DAY_OF_WEEK, randomDay) + .with(ChronoField.HOUR_OF_DAY, randomHour) + .with(ChronoField.MINUTE_OF_HOUR, randomMinute) + .with(ChronoField.SECOND_OF_MINUTE, 59); ZonedDateTime scheduledTime = testNowTime.plusSeconds(2); - logger.info("Setting current time to [{}], job execution time [{}]", testNowTime, - scheduledTime); + logger.info("Setting current time to [{}], job execution time [{}]", testNowTime, scheduledTime); clock.setTime(testNowTime); // fun part here (aka WTF): DayOfWeek with Joda is MON-SUN, starting at 1 - // DayOfWeek with Watcher is SUN-SAT, starting at 1 + // DayOfWeek with Watcher is SUN-SAT, starting at 1 int watcherDay = (scheduledTime.getDayOfWeek().getValue() % 7) + 1; - engine.add(createWatch(name, weekly().time(WeekTimes.builder() - .on(DayOfWeek.resolve(watcherDay)) - .at(scheduledTime.getHour(), scheduledTime.getMinute()).build()) - .build())); + engine.add( + createWatch( + name, + weekly().time( + WeekTimes.builder().on(DayOfWeek.resolve(watcherDay)).at(scheduledTime.getHour(), scheduledTime.getMinute()).build() + ).build() + ) + ); advanceClockIfNeeded(scheduledTime); if (latch.await(5, TimeUnit.SECONDS) == false) { @@ -281,8 +284,18 @@ public void testAddOnlyWithNewSchedule() { } private Watch createWatch(String name, Schedule schedule) { - return new Watch(name, new ScheduleTrigger(schedule), new ExecutableNoneInput(), - InternalAlwaysCondition.INSTANCE, null, null, - Collections.emptyList(), null, null, SequenceNumbers.UNASSIGNED_SEQ_NO, SequenceNumbers.UNASSIGNED_PRIMARY_TERM); + return new Watch( + name, + new ScheduleTrigger(schedule), + new ExecutableNoneInput(), + InternalAlwaysCondition.INSTANCE, + null, + null, + Collections.emptyList(), + null, + null, + SequenceNumbers.UNASSIGNED_SEQ_NO, + SequenceNumbers.UNASSIGNED_PRIMARY_TERM + ); } } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/tool/CronEvalToolTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/tool/CronEvalToolTests.java index a5ae6fc92b820..f3d1ff363cc08 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/tool/CronEvalToolTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/schedule/tool/CronEvalToolTests.java @@ -58,7 +58,7 @@ public void testGetNextValidTimes() throws Exception { // randomized testing sets arbitrary locales and timezones, and we do not care // we always have to output in standard locale and independent from timezone public void testEnsureDateIsShownInRootLocale() throws Exception { - String output = execute("-c","1", "0 0 11 ? * MON-SAT 2040"); + String output = execute("-c", "1", "0 0 11 ? * MON-SAT 2040"); if (ZoneId.systemDefault().equals(ZoneOffset.UTC)) { assertThat(output, not(containsString("local time is"))); long linesStartingWithOne = Arrays.stream(output.split("\n")).filter(s -> s.startsWith("\t")).count(); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/watch/WatchStatusTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/watch/WatchStatusTests.java index 42395d6fe4f39..02b68ef07cdb3 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/watch/WatchStatusTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/watch/WatchStatusTests.java @@ -8,10 +8,10 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.BytesStreamOutput; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.watcher.actions.ActionStatus; import org.elasticsearch.xpack.core.watcher.actions.ActionStatus.AckStatus.State; import org.elasticsearch.xpack.core.watcher.support.xcontent.WatcherParams; diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/watch/WatchTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/watch/WatchTests.java index 25eca54f91c4c..182aeed4eab17 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/watch/WatchTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/watch/WatchTests.java @@ -13,14 +13,9 @@ import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.support.WriteRequest; import org.elasticsearch.client.Client; -import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.xcontent.NamedXContentRegistry; -import org.elasticsearch.xcontent.XContentBuilder; -import org.elasticsearch.xcontent.XContentParser; -import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.index.query.MatchAllQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.ScriptQueryBuilder; @@ -28,6 +23,11 @@ import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptService; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.NamedXContentRegistry; +import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.XContentParser; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.watcher.actions.ActionFactory; import org.elasticsearch.xpack.core.watcher.actions.ActionRegistry; import org.elasticsearch.xpack.core.watcher.actions.ActionStatus; @@ -136,8 +136,8 @@ import static java.util.Collections.singletonMap; import static java.util.Collections.unmodifiableMap; import static org.elasticsearch.core.TimeValue.timeValueSeconds; -import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource; +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.xpack.watcher.input.InputBuilders.searchInput; import static org.elasticsearch.xpack.watcher.test.WatcherTestUtils.templateRequest; import static org.elasticsearch.xpack.watcher.trigger.TriggerBuilders.schedule; @@ -209,8 +209,19 @@ public void testParserSelfGenerated() throws Exception { final long sourceSeqNo = randomNonNegativeLong(); final long sourcePrimaryTerm = randomLongBetween(1, 200); - Watch watch = new Watch("_name", trigger, input, condition, transform, throttlePeriod, actions, metadata, watchStatus, - sourceSeqNo, sourcePrimaryTerm); + Watch watch = new Watch( + "_name", + trigger, + input, + condition, + transform, + throttlePeriod, + actions, + metadata, + watchStatus, + sourceSeqNo, + sourcePrimaryTerm + ); BytesReference bytes = BytesReference.bytes(jsonBuilder().value(watch)); logger.info("{}", bytes.utf8ToString()); @@ -281,13 +292,9 @@ public void testParserBadActions() throws Exception { TransformRegistry transformRegistry = transformRegistry(); List actions = randomActions(); - ActionRegistry actionRegistry = registry(actions,conditionRegistry, transformRegistry); - + ActionRegistry actionRegistry = registry(actions, conditionRegistry, transformRegistry); - XContentBuilder jsonBuilder = jsonBuilder() - .startObject() - .startArray("actions").endArray() - .endObject(); + XContentBuilder jsonBuilder = jsonBuilder().startObject().startArray("actions").endArray().endObject(); WatchParser watchParser = new WatchParser(triggerService, actionRegistry, inputRegistry, null, clock); try { watchParser.parse("failure", false, BytesReference.bytes(jsonBuilder), XContentType.JSON, 1L, 1L); @@ -310,9 +317,7 @@ public void testParserDefaults() throws Exception { XContentBuilder builder = jsonBuilder(); builder.startObject(); - builder.startObject(WatchField.TRIGGER.getPreferredName()) - .field(ScheduleTrigger.TYPE, schedule(schedule).build()) - .endObject(); + builder.startObject(WatchField.TRIGGER.getPreferredName()).field(ScheduleTrigger.TYPE, schedule(schedule).build()).endObject(); builder.endObject(); WatchParser watchParser = new WatchParser(triggerService, actionRegistry, inputRegistry, null, Clock.systemUTC()); Watch watch = watchParser.parse("failure", false, BytesReference.bytes(builder), XContentType.JSON, 1L, 1L); @@ -326,8 +331,9 @@ public void testParserDefaults() throws Exception { } public void testParseWatch_verifyScriptLangDefault() throws Exception { - ScheduleRegistry scheduleRegistry = registry(new IntervalSchedule(new IntervalSchedule.Interval(1, - IntervalSchedule.Interval.Unit.SECONDS))); + ScheduleRegistry scheduleRegistry = registry( + new IntervalSchedule(new IntervalSchedule.Interval(1, IntervalSchedule.Interval.Unit.SECONDS)) + ); TriggerEngine triggerEngine = new ParseOnlyScheduleTriggerEngine(scheduleRegistry, Clock.systemUTC()); TriggerService triggerService = new TriggerService(singleton(triggerEngine)); @@ -394,9 +400,13 @@ public void testParseWatchWithoutInput() throws Exception { builder.startObject("trigger").startObject("schedule").field("interval", "99w").endObject().endObject(); builder.startObject("condition").startObject("always").endObject().endObject(); - builder.startObject("actions").startObject("logme") - .startObject("logging").field("text", "foo").endObject() - .endObject().endObject(); + builder.startObject("actions") + .startObject("logme") + .startObject("logging") + .field("text", "foo") + .endObject() + .endObject() + .endObject(); builder.endObject(); WatchParser parser = createWatchparser(); @@ -428,25 +438,41 @@ public void testParseWatchWithoutTriggerDoesNotWork() throws Exception { builder.startObject("input").startObject("simple").endObject().endObject(); builder.startObject("condition").startObject("always").endObject().endObject(); - builder.startObject("actions").startObject("logme") - .startObject("logging").field("text", "foo").endObject() - .endObject().endObject(); + builder.startObject("actions") + .startObject("logme") + .startObject("logging") + .field("text", "foo") + .endObject() + .endObject() + .endObject(); builder.endObject(); WatchParser parser = createWatchparser(); - ElasticsearchParseException e = expectThrows(ElasticsearchParseException.class, - () -> parser.parse("_id", false, BytesReference.bytes(builder), XContentType.JSON, 1L, 1L)); + ElasticsearchParseException e = expectThrows( + ElasticsearchParseException.class, + () -> parser.parse("_id", false, BytesReference.bytes(builder), XContentType.JSON, 1L, 1L) + ); assertThat(e.getMessage(), is("could not parse watch [_id]. missing required field [trigger]")); } } private WatchParser createWatchparser() throws Exception { LoggingAction loggingAction = new LoggingAction(new TextTemplate("foo"), null, null); - List actions = Collections.singletonList(new ActionWrapper("_logging_", randomThrottler(), null, null, - new ExecutableLoggingAction(loggingAction, logger, new MockTextTemplateEngine()), null, null)); - - ScheduleRegistry scheduleRegistry = registry(new IntervalSchedule(new IntervalSchedule.Interval(1, - IntervalSchedule.Interval.Unit.SECONDS))); + List actions = Collections.singletonList( + new ActionWrapper( + "_logging_", + randomThrottler(), + null, + null, + new ExecutableLoggingAction(loggingAction, logger, new MockTextTemplateEngine()), + null, + null + ) + ); + + ScheduleRegistry scheduleRegistry = registry( + new IntervalSchedule(new IntervalSchedule.Interval(1, IntervalSchedule.Interval.Unit.SECONDS)) + ); TriggerEngine triggerEngine = new ParseOnlyScheduleTriggerEngine(scheduleRegistry, Clock.systemUTC()); TriggerService triggerService = new TriggerService(singleton(triggerEngine)); @@ -459,8 +485,15 @@ private WatchParser createWatchparser() throws Exception { } private static Schedule randomSchedule() { - String type = randomFrom(CronSchedule.TYPE, HourlySchedule.TYPE, DailySchedule.TYPE, WeeklySchedule.TYPE, MonthlySchedule.TYPE, - YearlySchedule.TYPE, IntervalSchedule.TYPE); + String type = randomFrom( + CronSchedule.TYPE, + HourlySchedule.TYPE, + DailySchedule.TYPE, + WeeklySchedule.TYPE, + MonthlySchedule.TYPE, + YearlySchedule.TYPE, + IntervalSchedule.TYPE + ); switch (type) { case CronSchedule.TYPE: return new CronSchedule("0/5 * * * * ? *"); @@ -512,9 +545,9 @@ private static ScheduleRegistry registry(Schedule schedule) { String type = randomFrom(SearchInput.TYPE, SimpleInput.TYPE); switch (type) { case SearchInput.TYPE: - SearchInput searchInput = searchInput(WatcherTestUtils.templateRequest(searchSource(), "idx")) - .timeout(randomBoolean() ? null : timeValueSeconds(between(1, 10000))) - .build(); + SearchInput searchInput = searchInput(WatcherTestUtils.templateRequest(searchSource(), "idx")).timeout( + randomBoolean() ? null : timeValueSeconds(between(1, 10000)) + ).build(); return new ExecutableSearchInput(searchInput, client, searchTemplateService, null); default: SimpleInput simpleInput = InputBuilders.simpleInput(singletonMap("_key", "_val")).build(); @@ -534,8 +567,6 @@ private InputRegistry registry(String inputType) { } } - - private ConditionRegistry conditionRegistry() { Map parsers = new HashMap<>(); parsers.put(InternalAlwaysCondition.TYPE, (c, id, p) -> InternalAlwaysCondition.parse(id, p)); @@ -554,63 +585,106 @@ private ConditionRegistry conditionRegistry() { case ScriptTransform.TYPE: return new ExecutableScriptTransform(new ScriptTransform(mockScript("_script")), logger, scriptService); case SearchTransform.TYPE: - SearchTransform transform = new SearchTransform( - templateRequest(searchSource()), timeout, timeZone); + SearchTransform transform = new SearchTransform(templateRequest(searchSource()), timeout, timeZone); return new ExecutableSearchTransform(transform, logger, client, searchTemplateService, TimeValue.timeValueMinutes(1)); default: // chain - SearchTransform searchTransform = new SearchTransform( - templateRequest(searchSource()), timeout, timeZone); + SearchTransform searchTransform = new SearchTransform(templateRequest(searchSource()), timeout, timeZone); ScriptTransform scriptTransform = new ScriptTransform(mockScript("_script")); ChainTransform chainTransform = new ChainTransform(Arrays.asList(searchTransform, scriptTransform)); - return new ExecutableChainTransform(chainTransform, logger, Arrays.asList( - new ExecutableSearchTransform(new SearchTransform( - templateRequest(searchSource()), timeout, timeZone), - logger, client, searchTemplateService, TimeValue.timeValueMinutes(1)), - new ExecutableScriptTransform(new ScriptTransform(mockScript("_script")), - logger, scriptService))); + return new ExecutableChainTransform( + chainTransform, + logger, + Arrays.asList( + new ExecutableSearchTransform( + new SearchTransform(templateRequest(searchSource()), timeout, timeZone), + logger, + client, + searchTemplateService, + TimeValue.timeValueMinutes(1) + ), + new ExecutableScriptTransform(new ScriptTransform(mockScript("_script")), logger, scriptService) + ) + ); } } private TransformRegistry transformRegistry() { - return new TransformRegistry(Map.of( + return new TransformRegistry( + Map.of( ScriptTransform.TYPE, new ScriptTransformFactory(scriptService), SearchTransform.TYPE, - new SearchTransformFactory(settings, client, xContentRegistry(), scriptService))); + new SearchTransformFactory(settings, client, xContentRegistry(), scriptService) + ) + ); } private List randomActions() { List list = new ArrayList<>(); if (randomBoolean()) { - EmailAction action = new EmailAction(EmailTemplate.builder().build(), null, null, Profile.STANDARD, - randomFrom(DataAttachment.JSON, DataAttachment.YAML), EmailAttachments.EMPTY_ATTACHMENTS); - list.add(new ActionWrapper("_email_" + randomAlphaOfLength(8), randomThrottler(), - AlwaysConditionTests.randomCondition(scriptService), randomTransform(), - new ExecutableEmailAction(action, logger, emailService, templateEngine, htmlSanitizer, - Collections.emptyMap()), null, null)); + EmailAction action = new EmailAction( + EmailTemplate.builder().build(), + null, + null, + Profile.STANDARD, + randomFrom(DataAttachment.JSON, DataAttachment.YAML), + EmailAttachments.EMPTY_ATTACHMENTS + ); + list.add( + new ActionWrapper( + "_email_" + randomAlphaOfLength(8), + randomThrottler(), + AlwaysConditionTests.randomCondition(scriptService), + randomTransform(), + new ExecutableEmailAction(action, logger, emailService, templateEngine, htmlSanitizer, Collections.emptyMap()), + null, + null + ) + ); } if (randomBoolean()) { ZoneOffset timeZone = randomBoolean() ? ZoneOffset.UTC : null; TimeValue timeout = randomBoolean() ? timeValueSeconds(between(1, 10000)) : null; WriteRequest.RefreshPolicy refreshPolicy = randomBoolean() ? null : randomFrom(WriteRequest.RefreshPolicy.values()); - IndexAction action = new IndexAction("_index", randomBoolean() ? "123" : null, - randomBoolean() ? DocWriteRequest.OpType.fromId(randomFrom(new Byte[] { 0, 1 })) : null, null, timeout, timeZone, - refreshPolicy); - list.add(new ActionWrapper("_index_" + randomAlphaOfLength(8), randomThrottler(), - AlwaysConditionTests.randomCondition(scriptService), randomTransform(), - new ExecutableIndexAction(action, logger, client, TimeValue.timeValueSeconds(30), - TimeValue.timeValueSeconds(30)), null, null)); + IndexAction action = new IndexAction( + "_index", + randomBoolean() ? "123" : null, + randomBoolean() ? DocWriteRequest.OpType.fromId(randomFrom(new Byte[] { 0, 1 })) : null, + null, + timeout, + timeZone, + refreshPolicy + ); + list.add( + new ActionWrapper( + "_index_" + randomAlphaOfLength(8), + randomThrottler(), + AlwaysConditionTests.randomCondition(scriptService), + randomTransform(), + new ExecutableIndexAction(action, logger, client, TimeValue.timeValueSeconds(30), TimeValue.timeValueSeconds(30)), + null, + null + ) + ); } if (randomBoolean()) { HttpRequestTemplate httpRequest = HttpRequestTemplate.builder("test.host", randomIntBetween(8000, 9000)) - .method(randomFrom(HttpMethod.GET, HttpMethod.POST, HttpMethod.PUT)) - .path(new TextTemplate("_url")) - .build(); + .method(randomFrom(HttpMethod.GET, HttpMethod.POST, HttpMethod.PUT)) + .path(new TextTemplate("_url")) + .build(); WebhookAction action = new WebhookAction(httpRequest); - list.add(new ActionWrapper("_webhook_" + randomAlphaOfLength(8), randomThrottler(), - AlwaysConditionTests.randomCondition(scriptService), randomTransform(), - new ExecutableWebhookAction(action, logger, httpClient, templateEngine), null, null)); + list.add( + new ActionWrapper( + "_webhook_" + randomAlphaOfLength(8), + randomThrottler(), + AlwaysConditionTests.randomCondition(scriptService), + randomTransform(), + new ExecutableWebhookAction(action, logger, httpClient, templateEngine), + null, + null + ) + ); } return list; } @@ -620,8 +694,10 @@ private ActionRegistry registry(List actions, ConditionRegistry c for (ActionWrapper action : actions) { switch (action.action().type()) { case EmailAction.TYPE: - parsers.put(EmailAction.TYPE, new EmailActionFactory(settings, emailService, templateEngine, - new EmailAttachmentsParser(Collections.emptyMap()))); + parsers.put( + EmailAction.TYPE, + new EmailActionFactory(settings, emailService, templateEngine, new EmailAttachmentsParser(Collections.emptyMap())) + ); break; case IndexAction.TYPE: parsers.put(IndexAction.TYPE, new IndexActionFactory(settings, client)); @@ -638,18 +714,25 @@ private ActionRegistry registry(List actions, ConditionRegistry c } private ActionThrottler randomThrottler() { - return new ActionThrottler(Clock.systemUTC(), randomBoolean() ? null : timeValueSeconds(randomIntBetween(1, 10000)), - licenseState); + return new ActionThrottler(Clock.systemUTC(), randomBoolean() ? null : timeValueSeconds(randomIntBetween(1, 10000)), licenseState); } @Override protected NamedXContentRegistry xContentRegistry() { - return new NamedXContentRegistry(Arrays.asList( - new NamedXContentRegistry.Entry(QueryBuilder.class, new ParseField(MatchAllQueryBuilder.NAME), (p, c) -> - MatchAllQueryBuilder.fromXContent(p)), - new NamedXContentRegistry.Entry(QueryBuilder.class, new ParseField(ScriptQueryBuilder.NAME), (p, c) -> - ScriptQueryBuilder.fromXContent(p)) - )); + return new NamedXContentRegistry( + Arrays.asList( + new NamedXContentRegistry.Entry( + QueryBuilder.class, + new ParseField(MatchAllQueryBuilder.NAME), + (p, c) -> MatchAllQueryBuilder.fromXContent(p) + ), + new NamedXContentRegistry.Entry( + QueryBuilder.class, + new ParseField(ScriptQueryBuilder.NAME), + (p, c) -> ScriptQueryBuilder.fromXContent(p) + ) + ) + ); } public static class ParseOnlyScheduleTriggerEngine extends ScheduleTriggerEngine { @@ -659,20 +742,16 @@ public ParseOnlyScheduleTriggerEngine(ScheduleRegistry registry, Clock clock) { } @Override - public void start(Collection jobs) { - } + public void start(Collection jobs) {} @Override - public void stop() { - } + public void stop() {} @Override - public void add(Watch watch) { - } + public void add(Watch watch) {} @Override - public void pauseExecution() { - } + public void pauseExecution() {} @Override public boolean remove(String jobId) { diff --git a/x-pack/plugin/wildcard/src/main/java/org/elasticsearch/xpack/wildcard/mapper/BinaryDvConfirmedAutomatonQuery.java b/x-pack/plugin/wildcard/src/main/java/org/elasticsearch/xpack/wildcard/mapper/BinaryDvConfirmedAutomatonQuery.java index d65be88b22149..f2836e10e4ed8 100644 --- a/x-pack/plugin/wildcard/src/main/java/org/elasticsearch/xpack/wildcard/mapper/BinaryDvConfirmedAutomatonQuery.java +++ b/x-pack/plugin/wildcard/src/main/java/org/elasticsearch/xpack/wildcard/mapper/BinaryDvConfirmedAutomatonQuery.java @@ -57,7 +57,7 @@ private BinaryDvConfirmedAutomatonQuery(Query approximation, String field, Strin @Override public Query rewrite(IndexReader reader) throws IOException { Query approxRewrite = approxQuery.rewrite(reader); - if (approxQuery != approxRewrite ) { + if (approxQuery != approxRewrite) { return new BinaryDvConfirmedAutomatonQuery(approxRewrite, field, matchPattern, bytesMatcher); } return this; @@ -82,8 +82,7 @@ public Scorer scorer(LeafReaderContext context) throws IOException { TwoPhaseIterator twoPhase = new TwoPhaseIterator(approxDisi) { @Override public boolean matches() throws IOException { - if (values.advanceExact(approxDisi.docID()) == false) - { + if (values.advanceExact(approxDisi.docID()) == false) { // Can happen when approxQuery resolves to some form of MatchAllDocs expression return false; } @@ -92,7 +91,7 @@ public boolean matches() throws IOException { bytes.setPosition(arrayOfValues.offset); int size = bytes.readVInt(); - for (int i=0; i< size; i++) { + for (int i = 0; i < size; i++) { int valLength = bytes.readVInt(); if (bytesMatcher.run(arrayOfValues.bytes, bytes.getPosition(), valLength)) { return true; @@ -117,9 +116,10 @@ public boolean isCacheable(LeafReaderContext ctx) { } }; } + @Override public String toString(String field) { - return field+":"+matchPattern; + return field + ":" + matchPattern; } @Override @@ -128,7 +128,8 @@ public boolean equals(Object obj) { return false; } BinaryDvConfirmedAutomatonQuery other = (BinaryDvConfirmedAutomatonQuery) obj; - return Objects.equals(field, other.field) && Objects.equals(matchPattern, other.matchPattern) + return Objects.equals(field, other.field) + && Objects.equals(matchPattern, other.matchPattern) && Objects.equals(bytesMatcher, other.bytesMatcher) && Objects.equals(approxQuery, other.approxQuery); } diff --git a/x-pack/plugin/wildcard/src/main/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldMapper.java b/x-pack/plugin/wildcard/src/main/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldMapper.java index 8f9be13101abe..c7f155782ed19 100644 --- a/x-pack/plugin/wildcard/src/main/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldMapper.java +++ b/x-pack/plugin/wildcard/src/main/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldMapper.java @@ -5,7 +5,6 @@ * 2.0. */ - package org.elasticsearch.xpack.wildcard.mapper; import org.apache.lucene.analysis.Analyzer; @@ -49,7 +48,6 @@ import org.elasticsearch.common.lucene.search.AutomatonQueries; import org.elasticsearch.common.time.DateMathParser; import org.elasticsearch.common.unit.Fuzziness; -import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.index.analysis.AnalyzerScope; import org.elasticsearch.index.analysis.LowercaseNormalizer; import org.elasticsearch.index.analysis.NamedAnalyzer; @@ -68,6 +66,7 @@ import org.elasticsearch.index.query.SearchExecutionContext; import org.elasticsearch.search.aggregations.support.CoreValuesSourceType; import org.elasticsearch.search.lookup.SearchLookup; +import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.nio.charset.StandardCharsets; @@ -122,16 +121,15 @@ public static class PunctuationFoldingFilter extends TokenFilter { * @param in TokenStream to filter */ public PunctuationFoldingFilter(TokenStream in) { - super(in); + super(in); } @Override public final boolean incrementToken() throws IOException { - if (input.incrementToken()) { - normalize(termAtt.buffer(), 0, termAtt.length()); - return true; - } else - return false; + if (input.incrementToken()) { + normalize(termAtt.buffer(), 0, termAtt.length()); + return true; + } else return false; } public static String normalize(String s) { @@ -144,13 +142,12 @@ public static String normalize(String s) { * Normalizes a token */ public static void normalize(final char[] buffer, final int offset, final int limit) { - assert buffer.length >= limit; - assert 0 <= offset && offset <= buffer.length; - for (int i = offset; i < limit;) { - int codepoint = Character.codePointAt(buffer, i, limit); - i += Character.toChars( - normalize(codepoint), buffer, i); - } + assert buffer.length >= limit; + assert 0 <= offset && offset <= buffer.length; + for (int i = offset; i < limit;) { + int codepoint = Character.codePointAt(buffer, i, limit); + i += Character.toChars(normalize(codepoint), buffer, i); + } } private static int normalize(int codepoint) { @@ -171,7 +168,7 @@ private static int normalize(int codepoint) { } } - } + } public static class Defaults { public static final FieldType FIELD_TYPE = new FieldType(); @@ -182,8 +179,12 @@ public static class Defaults { FIELD_TYPE.setOmitNorms(true); FIELD_TYPE.freeze(); } - public static final TextSearchInfo TEXT_SEARCH_INFO - = new TextSearchInfo(FIELD_TYPE, null, Lucene.KEYWORD_ANALYZER, Lucene.KEYWORD_ANALYZER); + public static final TextSearchInfo TEXT_SEARCH_INFO = new TextSearchInfo( + FIELD_TYPE, + null, + Lucene.KEYWORD_ANALYZER, + Lucene.KEYWORD_ANALYZER + ); public static final int IGNORE_ABOVE = Integer.MAX_VALUE; } @@ -193,15 +194,13 @@ private static WildcardFieldMapper toType(FieldMapper in) { public static class Builder extends FieldMapper.Builder { - final Parameter ignoreAbove - = Parameter.intParam("ignore_above", true, m -> toType(m).ignoreAbove, Defaults.IGNORE_ABOVE) + final Parameter ignoreAbove = Parameter.intParam("ignore_above", true, m -> toType(m).ignoreAbove, Defaults.IGNORE_ABOVE) .addValidator(v -> { if (v < 0) { throw new IllegalArgumentException("[ignore_above] must be positive, got [" + v + "]"); } }); - final Parameter nullValue - = Parameter.stringParam("null_value", false, m -> toType(m).nullValue, null).acceptsNull(); + final Parameter nullValue = Parameter.stringParam("null_value", false, m -> toType(m).nullValue, null).acceptsNull(); final Parameter> meta = Parameter.metaParam(); @@ -243,11 +242,11 @@ public WildcardFieldMapper build(MapperBuilderContext context) { public static TypeParser PARSER = new TypeParser((n, c) -> new Builder(n, c.indexVersionCreated())); - public static final char TOKEN_START_OR_END_CHAR = 0; - public static final String TOKEN_START_STRING = Character.toString(TOKEN_START_OR_END_CHAR); - public static final String TOKEN_END_STRING = TOKEN_START_STRING + TOKEN_START_STRING; + public static final char TOKEN_START_OR_END_CHAR = 0; + public static final String TOKEN_START_STRING = Character.toString(TOKEN_START_OR_END_CHAR); + public static final String TOKEN_END_STRING = TOKEN_START_STRING + TOKEN_START_STRING; - public static final class WildcardFieldType extends MappedFieldType { + public static final class WildcardFieldType extends MappedFieldType { static Analyzer lowercaseNormalizer = new LowercaseNormalizer(); @@ -255,8 +254,7 @@ public static final class WildcardFieldType extends MappedFieldType { private final int ignoreAbove; private final NamedAnalyzer analyzer; - private WildcardFieldType(String name, String nullValue, int ignoreAbove, - Version version, Map meta) { + private WildcardFieldType(String name, String nullValue, int ignoreAbove, Version version, Map meta) { super(name, true, false, true, Defaults.TEXT_SEARCH_INFO, meta); if (version.onOrAfter(Version.V_7_10_0)) { this.analyzer = WILDCARD_ANALYZER_7_10; @@ -345,13 +343,19 @@ public Query wildcardQuery(String wildcardPattern, RewriteMethod method, boolean } @Override - public Query regexpQuery(String value, int syntaxFlags, int matchFlags, int maxDeterminizedStates, - RewriteMethod method, SearchExecutionContext context) { + public Query regexpQuery( + String value, + int syntaxFlags, + int matchFlags, + int maxDeterminizedStates, + RewriteMethod method, + SearchExecutionContext context + ) { if (value.length() == 0) { return new MatchNoDocsQuery(); } - - //Check for simple "match all expressions e.g. .* + + // Check for simple "match all expressions e.g. .* RegExp regExp = new RegExp(value, syntaxFlags, matchFlags); Automaton a = regExp.toAutomaton(); a = Operations.determinize(a, maxDeterminizedStates); @@ -361,7 +365,6 @@ public Query regexpQuery(String value, int syntaxFlags, int matchFlags, int maxD } RegExp ngramRegex = new RegExp(addLineEndChars(value), syntaxFlags, matchFlags); - Query approxBooleanQuery = toApproximationQuery(ngramRegex); Query approxNgramQuery = rewriteBoolToNgramQuery(approxBooleanQuery); @@ -378,10 +381,10 @@ public Query regexpQuery(String value, int syntaxFlags, int matchFlags, int maxD // produce any false negatives. // In addition to Term and BooleanQuery clauses there are MatchAllDocsQuery objects (e.g for .*) and // a RegExpQuery if we can't resolve to any of the above. - // * If an expression resolves to a single MatchAllDocsQuery eg .* then a match all shortcut is possible with - // no verification needed. + // * If an expression resolves to a single MatchAllDocsQuery eg .* then a match all shortcut is possible with + // no verification needed. // * If an expression resolves to a RegExpQuery eg ?? then only the verification - // query is run. + // query is run. // * Anything else is a concrete query that should be run on the ngram index. public static Query toApproximationQuery(RegExp r) throws IllegalArgumentException { Query result = null; @@ -410,7 +413,7 @@ public static Query toApproximationQuery(RegExp r) throws IllegalArgumentExcepti case REGEXP_REPEAT_MINMAX: if (r.min > 0) { result = toApproximationQuery(r.exp1); - if(result instanceof TermQuery) { + if (result instanceof TermQuery) { // Wrap the repeating expression so that it is not concatenated by a parent which concatenates // plain TermQuery objects together. Boolean queries are interpreted as a black box and not // concatenated. @@ -505,7 +508,7 @@ private static Query createUnionQuery(RegExp r) { private static void findLeaves(RegExp exp, Kind kind, List queries) { if (exp.kind == kind) { findLeaves(exp.exp1, kind, queries); - findLeaves( exp.exp2, kind, queries); + findLeaves(exp.exp2, kind, queries); } else { queries.add(toApproximationQuery(exp)); } @@ -517,7 +520,7 @@ private static String toLowerCase(String string) { // Takes a BooleanQuery + TermQuery tree representing query logic and rewrites using ngrams of appropriate size. private Query rewriteBoolToNgramQuery(Query approxQuery) { - //TODO optimise more intelligently so we: + // TODO optimise more intelligently so we: // 1) favour full-length term queries eg abc over short eg a* when pruning too many clauses. // 2) make MAX_CLAUSES_IN_APPROXIMATION_QUERY a global cap rather than per-boolean clause. if (approxQuery == null) { @@ -547,7 +550,7 @@ private Query rewriteBoolToNgramQuery(Query approxQuery) { if (approxQuery instanceof TermQuery) { TermQuery tq = (TermQuery) approxQuery; - //Remove simple terms that are only string beginnings or ends. + // Remove simple terms that are only string beginnings or ends. String s = tq.getTerm().text(); if (s.equals(WildcardFieldMapper.TOKEN_START_STRING) || s.equals(WildcardFieldMapper.TOKEN_END_STRING)) { return new MatchAllDocsQuery(); @@ -567,6 +570,7 @@ private Query rewriteBoolToNgramQuery(Query approxQuery) { } throw new IllegalStateException("Invalid query type found parsing regex query:" + approxQuery); } + protected void getNgramTokens(Set tokens, String fragment) { if (fragment.equals(TOKEN_START_STRING) || fragment.equals(TOKEN_END_STRING)) { // If a regex is a form of match-all e.g. ".*" we only produce the token start/end markers as search @@ -600,7 +604,6 @@ protected void getNgramTokens(Set tokens, String fragment) { } } - private void addClause(String token, BooleanQuery.Builder bqBuilder, Occur occur) { assert token.codePointCount(0, token.length()) <= NGRAM_SIZE; int tokenSize = token.codePointCount(0, token.length()); @@ -677,11 +680,10 @@ public Query rangeQuery( } } } - Automaton automaton = TermRangeQuery.toAutomaton(lower, upper, includeLower, includeUpper); + Automaton automaton = TermRangeQuery.toAutomaton(lower, upper, includeLower, includeUpper); if (accelerationQuery == null) { - return new BinaryDvConfirmedAutomatonQuery(new MatchAllDocsQuery(), - name(), lower + "-" + upper, automaton); + return new BinaryDvConfirmedAutomatonQuery(new MatchAllDocsQuery(), name(), lower + "-" + upper, automaton); } return new BinaryDvConfirmedAutomatonQuery(accelerationQuery, name(), lower + "-" + upper, automaton); } @@ -697,7 +699,7 @@ public Query fuzzyQuery( ) { String searchTerm = BytesRefs.toString(value); try { - //The approximation query can have a prefix and any number of ngrams. + // The approximation query can have a prefix and any number of ngrams. BooleanQuery.Builder approxBuilder = new BooleanQuery.Builder(); String postPrefixString = searchTerm; @@ -706,7 +708,7 @@ public Query fuzzyQuery( if (prefixLength > 0) { Set prefixTokens = new LinkedHashSet<>(); postPrefixString = searchTerm.substring(prefixLength); - String prefixCandidate = TOKEN_START_OR_END_CHAR + searchTerm.substring(0, prefixLength); + String prefixCandidate = TOKEN_START_OR_END_CHAR + searchTerm.substring(0, prefixLength); getNgramTokens(prefixTokens, prefixCandidate); for (String prefixToken : prefixTokens) { addClause(prefixToken, approxBuilder, Occur.MUST); @@ -762,8 +764,7 @@ public Query fuzzyQuery( transpositions ); if (ngramQ.clauses().size() == 0) { - return new BinaryDvConfirmedAutomatonQuery(new MatchAllDocsQuery(), - name(), searchTerm, fq.getAutomata().automaton); + return new BinaryDvConfirmedAutomatonQuery(new MatchAllDocsQuery(), name(), searchTerm, fq.getAutomata().automaton); } return new BinaryDvConfirmedAutomatonQuery(ngramQ, name(), searchTerm, fq.getAutomata().automaton); @@ -785,7 +786,7 @@ public String familyTypeName() { @Override public Query termQuery(Object value, SearchExecutionContext context) { String searchTerm = BytesRefs.toString(value); - return wildcardQuery(escapeWildcardSyntax(searchTerm), MultiTermQuery.CONSTANT_SCORE_REWRITE, false, context); + return wildcardQuery(escapeWildcardSyntax(searchTerm), MultiTermQuery.CONSTANT_SCORE_REWRITE, false, context); } private String escapeWildcardSyntax(String term) { @@ -810,8 +811,12 @@ public Query termQueryCaseInsensitive(Object value, SearchExecutionContext conte } @Override - public Query prefixQuery(String value, MultiTermQuery.RewriteMethod method, boolean caseInsensitive, - SearchExecutionContext context) { + public Query prefixQuery( + String value, + MultiTermQuery.RewriteMethod method, + boolean caseInsensitive, + SearchExecutionContext context + ) { return wildcardQuery(escapeWildcardSyntax(value) + "*", method, caseInsensitive, context); } @@ -830,34 +835,40 @@ public IndexFieldData.Builder fielddataBuilder(String fullyQualifiedIndexName, S return (cache, breakerService) -> new StringBinaryIndexFieldData(name(), CoreValuesSourceType.KEYWORD); } - @Override - public ValueFetcher valueFetcher(SearchExecutionContext context, String format) { - if (format != null) { - throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats."); - } + @Override + public ValueFetcher valueFetcher(SearchExecutionContext context, String format) { + if (format != null) { + throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats."); + } - return new SourceValueFetcher(name(), context, nullValue) { - @Override - protected String parseSourceValue(Object value) { - String keywordValue = value.toString(); - if (keywordValue.length() > ignoreAbove) { - return null; - } - return keywordValue; - } - }; - } + return new SourceValueFetcher(name(), context, nullValue) { + @Override + protected String parseSourceValue(Object value) { + String keywordValue = value.toString(); + if (keywordValue.length() > ignoreAbove) { + return null; + } + return keywordValue; + } + }; + } - } + } private final int ignoreAbove; private final String nullValue; private final FieldType ngramFieldType; private final Version indexVersionCreated; - private WildcardFieldMapper(String simpleName, WildcardFieldType mappedFieldType, - int ignoreAbove, MultiFields multiFields, CopyTo copyTo, - String nullValue, Version indexVersionCreated) { + private WildcardFieldMapper( + String simpleName, + WildcardFieldType mappedFieldType, + int ignoreAbove, + MultiFields multiFields, + CopyTo copyTo, + String nullValue, + Version indexVersionCreated + ) { super(simpleName, mappedFieldType, mappedFieldType.analyzer, multiFields, copyTo); this.nullValue = nullValue; this.ignoreAbove = ignoreAbove; @@ -887,7 +898,7 @@ protected void parseCreateField(DocumentParserContext context) throws IOExceptio if (parser.currentToken() == XContentParser.Token.VALUE_NULL) { value = nullValue; } else { - value = parser.textOrNull(); + value = parser.textOrNull(); } LuceneDocument parseDoc = context.doc(); @@ -902,7 +913,7 @@ protected void parseCreateField(DocumentParserContext context) throws IOExceptio parseDoc.addAll(fields); } - void createFields(String value, LuceneDocument parseDoc, Listfields) { + void createFields(String value, LuceneDocument parseDoc, List fields) { String ngramValue = addLineEndChars(value); Field ngramField = new Field(fieldType().name(), ngramValue, ngramFieldType); fields.add(ngramField); diff --git a/x-pack/plugin/wildcard/src/test/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldAggregationTests.java b/x-pack/plugin/wildcard/src/test/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldAggregationTests.java index 3c1cbb56452a7..c51d3ef449fdb 100644 --- a/x-pack/plugin/wildcard/src/test/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldAggregationTests.java +++ b/x-pack/plugin/wildcard/src/test/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldAggregationTests.java @@ -36,7 +36,6 @@ public class WildcardFieldAggregationTests extends AggregatorTestCase { private WildcardFieldMapper wildcardFieldMapper; private WildcardFieldMapper.WildcardFieldType wildcardFieldType; - @Before public void setup() { WildcardFieldMapper.Builder builder = new WildcardFieldMapper.Builder(WILDCARD_FIELD_NAME, Version.CURRENT); @@ -73,64 +72,53 @@ private void indexStrings(RandomIndexWriter iw, String... values) throws IOExcep } public void testTermsAggregation() throws IOException { - TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder("_name") - .field(WILDCARD_FIELD_NAME) + TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder("_name").field(WILDCARD_FIELD_NAME) .order(BucketOrder.key(true)); - testCase(aggregationBuilder, - new MatchAllDocsQuery(), - iw -> { - indexStrings(iw, "a"); - indexStrings(iw, "a"); - indexStrings(iw, "b"); - indexStrings(iw, "b"); - indexStrings(iw, "b"); - indexStrings(iw, "c"); - }, - (StringTerms result) -> { - assertTrue(AggregationInspectionHelper.hasValue(result)); - - assertEquals(3, result.getBuckets().size()); - assertEquals("a", result.getBuckets().get(0).getKeyAsString()); - assertEquals(2L, result.getBuckets().get(0).getDocCount()); - assertEquals("b", result.getBuckets().get(1).getKeyAsString()); - assertEquals(3L, result.getBuckets().get(1).getDocCount()); - assertEquals("c", result.getBuckets().get(2).getKeyAsString()); - assertEquals(1L, result.getBuckets().get(2).getDocCount()); - }, - wildcardFieldType); + testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { + indexStrings(iw, "a"); + indexStrings(iw, "a"); + indexStrings(iw, "b"); + indexStrings(iw, "b"); + indexStrings(iw, "b"); + indexStrings(iw, "c"); + }, (StringTerms result) -> { + assertTrue(AggregationInspectionHelper.hasValue(result)); + + assertEquals(3, result.getBuckets().size()); + assertEquals("a", result.getBuckets().get(0).getKeyAsString()); + assertEquals(2L, result.getBuckets().get(0).getDocCount()); + assertEquals("b", result.getBuckets().get(1).getKeyAsString()); + assertEquals(3L, result.getBuckets().get(1).getDocCount()); + assertEquals("c", result.getBuckets().get(2).getKeyAsString()); + assertEquals(1L, result.getBuckets().get(2).getDocCount()); + }, wildcardFieldType); } public void testCompositeTermsAggregation() throws IOException { CompositeAggregationBuilder aggregationBuilder = new CompositeAggregationBuilder( "name", - List.of( - new TermsValuesSourceBuilder("terms_key").field(WILDCARD_FIELD_NAME) - ) + List.of(new TermsValuesSourceBuilder("terms_key").field(WILDCARD_FIELD_NAME)) ); - testCase(aggregationBuilder, - new MatchAllDocsQuery(), - iw -> { - indexStrings(iw, "a"); - indexStrings(iw, "c"); - indexStrings(iw, "a"); - indexStrings(iw, "d"); - indexStrings(iw, "c"); - }, - (InternalComposite result) -> { - assertTrue(AggregationInspectionHelper.hasValue(result)); - - assertEquals(3, result.getBuckets().size()); - assertEquals("{terms_key=d}", result.afterKey().toString()); - assertEquals("{terms_key=a}", result.getBuckets().get(0).getKeyAsString()); - assertEquals(2L, result.getBuckets().get(0).getDocCount()); - assertEquals("{terms_key=c}", result.getBuckets().get(1).getKeyAsString()); - assertEquals(2L, result.getBuckets().get(1).getDocCount()); - assertEquals("{terms_key=d}", result.getBuckets().get(2).getKeyAsString()); - assertEquals(1L, result.getBuckets().get(2).getDocCount()); - }, - wildcardFieldType); + testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { + indexStrings(iw, "a"); + indexStrings(iw, "c"); + indexStrings(iw, "a"); + indexStrings(iw, "d"); + indexStrings(iw, "c"); + }, (InternalComposite result) -> { + assertTrue(AggregationInspectionHelper.hasValue(result)); + + assertEquals(3, result.getBuckets().size()); + assertEquals("{terms_key=d}", result.afterKey().toString()); + assertEquals("{terms_key=a}", result.getBuckets().get(0).getKeyAsString()); + assertEquals(2L, result.getBuckets().get(0).getDocCount()); + assertEquals("{terms_key=c}", result.getBuckets().get(1).getKeyAsString()); + assertEquals(2L, result.getBuckets().get(1).getDocCount()); + assertEquals("{terms_key=d}", result.getBuckets().get(2).getKeyAsString()); + assertEquals(1L, result.getBuckets().get(2).getDocCount()); + }, wildcardFieldType); } public void testCompositeTermsSearchAfter() throws IOException { diff --git a/x-pack/plugin/wildcard/src/test/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldMapperTests.java b/x-pack/plugin/wildcard/src/test/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldMapperTests.java index 65cfcdd376b00..e6031acea8fe8 100644 --- a/x-pack/plugin/wildcard/src/test/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldMapperTests.java +++ b/x-pack/plugin/wildcard/src/test/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldMapperTests.java @@ -48,7 +48,6 @@ import org.elasticsearch.common.lucene.search.AutomatonQueries; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.Fuzziness; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.cache.bitset.BitsetFilterCache; @@ -66,6 +65,7 @@ import org.elasticsearch.search.lookup.SearchLookup; import org.elasticsearch.search.sort.FieldSortBuilder; import org.elasticsearch.test.IndexSettingsModule; +import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.wildcard.Wildcard; import org.elasticsearch.xpack.wildcard.mapper.WildcardFieldMapper.Builder; import org.junit.Before; @@ -125,7 +125,7 @@ public void setUp() throws Exception { org.elasticsearch.index.mapper.KeywordFieldMapper.Builder kwBuilder = new KeywordFieldMapper.Builder(KEYWORD_FIELD_NAME); keywordFieldType = kwBuilder.build(MapperBuilderContext.ROOT); - + rewriteDir = newDirectory(); IndexWriterConfig iwc = newIndexWriterConfig(WildcardFieldMapper.WILDCARD_ANALYZER_7_10); RandomIndexWriter iw = new RandomIndexWriter(random(), rewriteDir, iwc); @@ -143,8 +143,6 @@ public void setUp() throws Exception { super.setUp(); } - - @Override public void tearDown() throws Exception { @@ -219,13 +217,11 @@ public void testBWCIndexVersion() throws IOException { IndexSearcher searcher = newSearcher(reader); iw.close(); - // Unnatural circumstance - testing we fail if we were to use the new analyzer on old index Query oldWildcardFieldQuery = wildcardFieldType.fieldType().wildcardQuery("a b", null, null); TopDocs oldWildcardFieldTopDocs = searcher.search(oldWildcardFieldQuery, 10, Sort.INDEXORDER); assertThat(oldWildcardFieldTopDocs.totalHits.value, equalTo(0L)); - // Natural circumstance test we revert to the old analyzer for old indices Query wildcardFieldQuery = wildcardFieldType79.fieldType().wildcardQuery("a b", null, null); TopDocs wildcardFieldTopDocs = searcher.search(wildcardFieldQuery, 10, Sort.INDEXORDER); @@ -235,7 +231,7 @@ public void testBWCIndexVersion() throws IOException { dir.close(); } - //Test long query strings don't cause exceptions + // Test long query strings don't cause exceptions public void testTooBigQueryField() throws IOException { Directory dir = newDirectory(); IndexWriterConfig iwc = newIndexWriterConfig(WildcardFieldMapper.WILDCARD_ANALYZER_7_10); @@ -265,12 +261,10 @@ public void testTooBigQueryField() throws IOException { wildcardFieldTopDocs = searcher.search(wildcardFieldQuery, 10, Sort.INDEXORDER); assertThat(wildcardFieldTopDocs.totalHits.value, equalTo(0L)); - reader.close(); dir.close(); } - public void testTermAndPrefixQueryIgnoreWildcardSyntax() throws IOException { Directory dir = newDirectory(); IndexWriterConfig iwc = newIndexWriterConfig(WildcardFieldMapper.WILDCARD_ANALYZER_7_10); @@ -299,13 +293,13 @@ public void testTermAndPrefixQueryIgnoreWildcardSyntax() throws IOException { dir.close(); } - private void expectTermMatch(IndexSearcher searcher, String term,long count) throws IOException { + private void expectTermMatch(IndexSearcher searcher, String term, long count) throws IOException { Query q = wildcardFieldType.fieldType().termQuery(term, MOCK_CONTEXT); TopDocs td = searcher.search(q, 10, Sort.RELEVANCE); assertThat(td.totalHits.value, equalTo(count)); } - private void expectPrefixMatch(IndexSearcher searcher, String term,long count) throws IOException { + private void expectPrefixMatch(IndexSearcher searcher, String term, long count) throws IOException { Query q = wildcardFieldType.fieldType().prefixQuery(term, null, MOCK_CONTEXT); TopDocs td = searcher.search(q, 10, Sort.RELEVANCE); assertThat(td.totalHits.value, equalTo(count)); @@ -351,66 +345,89 @@ public void testSearchResultsVersusKeywordField() throws IOException { Query keywordFieldQuery = null; String pattern = null; switch (randomInt(4)) { - case 0: - pattern = getRandomWildcardPattern(); - boolean caseInsensitive = randomBoolean(); - wildcardFieldQuery = wildcardFieldType.fieldType().wildcardQuery(pattern, null, caseInsensitive, MOCK_CONTEXT); - keywordFieldQuery = keywordFieldType.fieldType().wildcardQuery(pattern, null, caseInsensitive, MOCK_CONTEXT); - break; - case 1: - pattern = getRandomRegexPattern(values); - int matchFlags = randomBoolean()? 0 : RegExp.ASCII_CASE_INSENSITIVE; - wildcardFieldQuery = wildcardFieldType.fieldType().regexpQuery(pattern, RegExp.ALL, matchFlags, 20000, null, MOCK_CONTEXT); - keywordFieldQuery = keywordFieldType.fieldType().regexpQuery(pattern, RegExp.ALL, matchFlags,20000, null, MOCK_CONTEXT); - break; - case 2: - pattern = randomABString(5); - boolean caseInsensitivePrefix = randomBoolean(); - wildcardFieldQuery = wildcardFieldType.fieldType().prefixQuery(pattern, null, caseInsensitivePrefix, MOCK_CONTEXT); - keywordFieldQuery = keywordFieldType.fieldType().prefixQuery(pattern, null, caseInsensitivePrefix, MOCK_CONTEXT); - break; - case 3: - int edits = randomInt(2); - int prefixLength = randomInt(4); - pattern = getRandomFuzzyPattern(values, edits, prefixLength); - Fuzziness fuzziness = Fuzziness.AUTO; - switch (edits) { - case 0: - fuzziness = Fuzziness.ZERO; - break; - case 1: - fuzziness = Fuzziness.ONE; - break; - case 2: - fuzziness = Fuzziness.TWO; - break; - default: - break; - } - // Prefix length shouldn't be longer than selected search string - // BUT keyword field has a bug with prefix length when equal - see https://github.com/elastic/elasticsearch/issues/55790 - // so we opt for one less - prefixLength = Math.min(pattern.length() - 1 , prefixLength); - boolean transpositions = randomBoolean(); - - wildcardFieldQuery = wildcardFieldType.fieldType().fuzzyQuery(pattern, fuzziness, prefixLength, 50, - transpositions, MOCK_CONTEXT); - keywordFieldQuery = keywordFieldType.fieldType().fuzzyQuery(pattern, fuzziness, prefixLength, 50, - transpositions, MOCK_CONTEXT); - break; - case 4: - TermRangeQuery trq = getRandomRange(values); - wildcardFieldQuery = wildcardFieldType.fieldType().rangeQuery(trq.getLowerTerm(),trq.getUpperTerm(), trq.includesLower(), - trq.includesUpper(), null, null, null, MOCK_CONTEXT); - keywordFieldQuery = keywordFieldType.fieldType().rangeQuery(trq.getLowerTerm(),trq.getUpperTerm(), trq.includesLower(), - trq.includesUpper(), null, null, null, MOCK_CONTEXT); - break; + case 0: + pattern = getRandomWildcardPattern(); + boolean caseInsensitive = randomBoolean(); + wildcardFieldQuery = wildcardFieldType.fieldType().wildcardQuery(pattern, null, caseInsensitive, MOCK_CONTEXT); + keywordFieldQuery = keywordFieldType.fieldType().wildcardQuery(pattern, null, caseInsensitive, MOCK_CONTEXT); + break; + case 1: + pattern = getRandomRegexPattern(values); + int matchFlags = randomBoolean() ? 0 : RegExp.ASCII_CASE_INSENSITIVE; + wildcardFieldQuery = wildcardFieldType.fieldType() + .regexpQuery(pattern, RegExp.ALL, matchFlags, 20000, null, MOCK_CONTEXT); + keywordFieldQuery = keywordFieldType.fieldType() + .regexpQuery(pattern, RegExp.ALL, matchFlags, 20000, null, MOCK_CONTEXT); + break; + case 2: + pattern = randomABString(5); + boolean caseInsensitivePrefix = randomBoolean(); + wildcardFieldQuery = wildcardFieldType.fieldType().prefixQuery(pattern, null, caseInsensitivePrefix, MOCK_CONTEXT); + keywordFieldQuery = keywordFieldType.fieldType().prefixQuery(pattern, null, caseInsensitivePrefix, MOCK_CONTEXT); + break; + case 3: + int edits = randomInt(2); + int prefixLength = randomInt(4); + pattern = getRandomFuzzyPattern(values, edits, prefixLength); + Fuzziness fuzziness = Fuzziness.AUTO; + switch (edits) { + case 0: + fuzziness = Fuzziness.ZERO; + break; + case 1: + fuzziness = Fuzziness.ONE; + break; + case 2: + fuzziness = Fuzziness.TWO; + break; + default: + break; + } + // Prefix length shouldn't be longer than selected search string + // BUT keyword field has a bug with prefix length when equal - see https://github.com/elastic/elasticsearch/issues/55790 + // so we opt for one less + prefixLength = Math.min(pattern.length() - 1, prefixLength); + boolean transpositions = randomBoolean(); + + wildcardFieldQuery = wildcardFieldType.fieldType() + .fuzzyQuery(pattern, fuzziness, prefixLength, 50, transpositions, MOCK_CONTEXT); + keywordFieldQuery = keywordFieldType.fieldType() + .fuzzyQuery(pattern, fuzziness, prefixLength, 50, transpositions, MOCK_CONTEXT); + break; + case 4: + TermRangeQuery trq = getRandomRange(values); + wildcardFieldQuery = wildcardFieldType.fieldType() + .rangeQuery( + trq.getLowerTerm(), + trq.getUpperTerm(), + trq.includesLower(), + trq.includesUpper(), + null, + null, + null, + MOCK_CONTEXT + ); + keywordFieldQuery = keywordFieldType.fieldType() + .rangeQuery( + trq.getLowerTerm(), + trq.getUpperTerm(), + trq.includesLower(), + trq.includesUpper(), + null, + null, + null, + MOCK_CONTEXT + ); + break; } TopDocs kwTopDocs = searcher.search(keywordFieldQuery, values.size() + 1, Sort.RELEVANCE); TopDocs wildcardFieldTopDocs = searcher.search(wildcardFieldQuery, values.size() + 1, Sort.RELEVANCE); - assertThat(keywordFieldQuery + "\n" + wildcardFieldQuery, - wildcardFieldTopDocs.totalHits.value, equalTo(kwTopDocs.totalHits.value)); + assertThat( + keywordFieldQuery + "\n" + wildcardFieldQuery, + wildcardFieldTopDocs.totalHits.value, + equalTo(kwTopDocs.totalHits.value) + ); HashSet expectedDocs = new HashSet<>(); for (ScoreDoc topDoc : kwTopDocs.scoreDocs) { @@ -422,7 +439,7 @@ public void testSearchResultsVersusKeywordField() throws IOException { assertThat(expectedDocs.size(), equalTo(0)); } - //Test keyword and wildcard sort operations are also equivalent + // Test keyword and wildcard sort operations are also equivalent SearchExecutionContext searchExecutionContext = createMockContext(); FieldSortBuilder wildcardSortBuilder = new FieldSortBuilder(WILDCARD_FIELD_NAME); @@ -474,29 +491,44 @@ public void testRangeQueryVersusKeywordField() throws IOException { IndexSearcher searcher = newSearcher(reader); iw.close(); + String[][] rangeTests = { + { "C:\\Program Files\\a", "C:\\Program Files\\z" }, + { "C:\\Program Files\\a", "C:\\Program Files\\n" }, + { null, "C:\\Program Files\\z" }, + { "C:\\Program Files\\a", null }, - String [][] rangeTests = { - {"C:\\Program Files\\a", "C:\\Program Files\\z"}, - {"C:\\Program Files\\a", "C:\\Program Files\\n"}, - {null, "C:\\Program Files\\z"}, - {"C:\\Program Files\\a", null}, - - {"a.txt", "z.txt"}, - {"a.txt", "n.txt"}, - {null, "z.txt"}, - {"a.txt", null}, - {"A.txt", "z.txt"} - }; + { "a.txt", "z.txt" }, + { "a.txt", "n.txt" }, + { null, "z.txt" }, + { "a.txt", null }, + { "A.txt", "z.txt" } }; for (String[] bounds : rangeTests) { - BytesRef lower = bounds[0] == null ? null :new BytesRef(bounds[0]); - BytesRef upper = bounds[1] == null ? null :new BytesRef(bounds[1]); + BytesRef lower = bounds[0] == null ? null : new BytesRef(bounds[0]); + BytesRef upper = bounds[1] == null ? null : new BytesRef(bounds[1]); TermRangeQuery trq = new TermRangeQuery(WILDCARD_FIELD_NAME, lower, upper, randomBoolean(), randomBoolean()); - Query wildcardFieldQuery = wildcardFieldType.fieldType().rangeQuery(trq.getLowerTerm(),trq.getUpperTerm(), trq.includesLower(), - trq.includesUpper(), null, null, null, MOCK_CONTEXT); - Query keywordFieldQuery = keywordFieldType.fieldType().rangeQuery(trq.getLowerTerm(),trq.getUpperTerm(), trq.includesLower(), - trq.includesUpper(), null, null, null, MOCK_CONTEXT); - + Query wildcardFieldQuery = wildcardFieldType.fieldType() + .rangeQuery( + trq.getLowerTerm(), + trq.getUpperTerm(), + trq.includesLower(), + trq.includesUpper(), + null, + null, + null, + MOCK_CONTEXT + ); + Query keywordFieldQuery = keywordFieldType.fieldType() + .rangeQuery( + trq.getLowerTerm(), + trq.getUpperTerm(), + trq.includesLower(), + trq.includesUpper(), + null, + null, + null, + MOCK_CONTEXT + ); TopDocs kwTopDocs = searcher.search(keywordFieldQuery, 10, Sort.RELEVANCE); TopDocs wildcardFieldTopDocs = searcher.search(wildcardFieldQuery, 10, Sort.RELEVANCE); @@ -518,12 +550,12 @@ public void testRangeQueryVersusKeywordField() throws IOException { public void testRegexAcceleration() throws IOException, ParseException { // All these expressions should rewrite to a match all with no verification step required at all - String superfastRegexes[]= { ".*", "(foo|bar|.*)", "@"}; + String superfastRegexes[] = { ".*", "(foo|bar|.*)", "@" }; for (String regex : superfastRegexes) { Query wildcardFieldQuery = wildcardFieldType.fieldType().regexpQuery(regex, RegExp.ALL, 0, 20000, null, MOCK_CONTEXT); assertTrue(regex + "should have been accelerated", wildcardFieldQuery instanceof DocValuesFieldExistsQuery); } - String matchNoDocsRegexes[]= { ""}; + String matchNoDocsRegexes[] = { "" }; for (String regex : matchNoDocsRegexes) { Query wildcardFieldQuery = wildcardFieldType.fieldType().regexpQuery(regex, RegExp.ALL, 0, 20000, null, MOCK_CONTEXT); assertTrue(wildcardFieldQuery instanceof MatchNoDocsQuery); @@ -531,57 +563,65 @@ public void testRegexAcceleration() throws IOException, ParseException { // All of these regexes should be accelerated as the equivalent of the given QueryString query String acceleratedTests[][] = { - {".*foo.*", "eoo"}, - {"..foobar","+eoo +ooa +oaa +aaq +aq_ +q__"}, - {"(maynotexist)?foobar","+eoo +ooa +oaa +aaq +aq_ +q__"}, - {".*/etc/passw.*", "+\\/es +esc +sc\\/ +c\\/o +\\/oa +oas +ass +ssw"}, - {".*etc/passwd", " +esc +sc\\/ +c\\/o +\\/oa +oas +ass +ssw +swc +wc_ +c__"}, - {"(http|ftp)://foo.*", "+((+gss +sso) eso) +(+\\/\\/\\/ +\\/\\/e +\\/eo +eoo)"}, - {"[Pp][Oo][Ww][Ee][Rr][Ss][Hh][Ee][Ll][Ll]\\.[Ee][Xx][Ee]", - "+_oo +oow +owe +weq +eqs +qsg +sge +gek +ekk +kk\\/ +k\\/e +\\/ew +ewe +we_ +e__"}, - {"foo<1-100>bar", "+(+_eo +eoo) +(+aaq +aq_ +q__)"}, - {"(aaa.+&.+bbb)cat", "+cas +as_ +s__"}, - {".a", "a__"} - }; + { ".*foo.*", "eoo" }, + { "..foobar", "+eoo +ooa +oaa +aaq +aq_ +q__" }, + { "(maynotexist)?foobar", "+eoo +ooa +oaa +aaq +aq_ +q__" }, + { ".*/etc/passw.*", "+\\/es +esc +sc\\/ +c\\/o +\\/oa +oas +ass +ssw" }, + { ".*etc/passwd", " +esc +sc\\/ +c\\/o +\\/oa +oas +ass +ssw +swc +wc_ +c__" }, + { "(http|ftp)://foo.*", "+((+gss +sso) eso) +(+\\/\\/\\/ +\\/\\/e +\\/eo +eoo)" }, + { + "[Pp][Oo][Ww][Ee][Rr][Ss][Hh][Ee][Ll][Ll]\\.[Ee][Xx][Ee]", + "+_oo +oow +owe +weq +eqs +qsg +sge +gek +ekk +kk\\/ +k\\/e +\\/ew +ewe +we_ +e__" }, + { "foo<1-100>bar", "+(+_eo +eoo) +(+aaq +aq_ +q__)" }, + { "(aaa.+&.+bbb)cat", "+cas +as_ +s__" }, + { ".a", "a__" } }; for (String[] test : acceleratedTests) { String regex = test[0]; - String expectedAccelerationQueryString = test[1].replaceAll("_", ""+WildcardFieldMapper.TOKEN_START_OR_END_CHAR); + String expectedAccelerationQueryString = test[1].replaceAll("_", "" + WildcardFieldMapper.TOKEN_START_OR_END_CHAR); Query wildcardFieldQuery = wildcardFieldType.fieldType().regexpQuery(regex, RegExp.ALL, 0, 20000, null, MOCK_CONTEXT); testExpectedAccelerationQuery(regex, wildcardFieldQuery, expectedAccelerationQueryString); } // All these expressions should rewrite to just the verification query (there's no ngram acceleration) // TODO we can possibly improve on some of these - String matchAllButVerifyTests[]= { "..", "(a)?","(a|b){0,3}", "((foo)?|(foo|bar)?)", "@&~(abc.+)", "aaa.+&.+bbb", "a*", "...*.."}; + String matchAllButVerifyTests[] = { + "..", + "(a)?", + "(a|b){0,3}", + "((foo)?|(foo|bar)?)", + "@&~(abc.+)", + "aaa.+&.+bbb", + "a*", + "...*.." }; for (String regex : matchAllButVerifyTests) { Query wildcardFieldQuery = wildcardFieldType.fieldType().regexpQuery(regex, RegExp.ALL, 0, 20000, null, MOCK_CONTEXT); - BinaryDvConfirmedAutomatonQuery q = (BinaryDvConfirmedAutomatonQuery)wildcardFieldQuery; + BinaryDvConfirmedAutomatonQuery q = (BinaryDvConfirmedAutomatonQuery) wildcardFieldQuery; Query approximationQuery = unwrapAnyBoost(q.getApproximationQuery()); approximationQuery = getSimplifiedApproximationQuery(q.getApproximationQuery()); - assertTrue(regex +" was not a pure verify query " +formatQuery(wildcardFieldQuery), - approximationQuery instanceof MatchAllDocsQuery); + assertTrue( + regex + " was not a pure verify query " + formatQuery(wildcardFieldQuery), + approximationQuery instanceof MatchAllDocsQuery + ); } - - // Documentation - regexes that do try accelerate but we would like to improve in future versions. String suboptimalTests[][] = { // TODO short wildcards like a* OR b* aren't great so we just drop them. // Ideally we would attach to successors to create (acd OR bcd) - { "[ab]cd", "+(+cc_ +c__) +*:*"} - }; + { "[ab]cd", "+(+cc_ +c__) +*:*" } }; for (String[] test : suboptimalTests) { String regex = test[0]; - String expectedAccelerationQueryString = test[1].replaceAll("_", ""+WildcardFieldMapper.TOKEN_START_OR_END_CHAR); + String expectedAccelerationQueryString = test[1].replaceAll("_", "" + WildcardFieldMapper.TOKEN_START_OR_END_CHAR); Query wildcardFieldQuery = wildcardFieldType.fieldType().regexpQuery(regex, RegExp.ALL, 0, 20000, null, MOCK_CONTEXT); testExpectedAccelerationQuery(regex, wildcardFieldQuery, expectedAccelerationQueryString); } } + // Make error messages more readable String formatQuery(Query q) { - return q.toString().replaceAll(WILDCARD_FIELD_NAME+":", "").replaceAll(WildcardFieldMapper.TOKEN_START_STRING, "_"); + return q.toString().replaceAll(WILDCARD_FIELD_NAME + ":", "").replaceAll(WildcardFieldMapper.TOKEN_START_STRING, "_"); } public void testWildcardAcceleration() throws IOException, ParseException { @@ -636,17 +676,29 @@ public void testQueryCachingEquality() throws IOException, ParseException { new Term("field", pattern), Integer.MAX_VALUE ); - BinaryDvConfirmedAutomatonQuery csQ = new BinaryDvConfirmedAutomatonQuery(new MatchAllDocsQuery(), - "field", pattern, caseSensitiveAutomaton); - BinaryDvConfirmedAutomatonQuery ciQ = new BinaryDvConfirmedAutomatonQuery(new MatchAllDocsQuery(), - "field", pattern, caseInSensitiveAutomaton); + BinaryDvConfirmedAutomatonQuery csQ = new BinaryDvConfirmedAutomatonQuery( + new MatchAllDocsQuery(), + "field", + pattern, + caseSensitiveAutomaton + ); + BinaryDvConfirmedAutomatonQuery ciQ = new BinaryDvConfirmedAutomatonQuery( + new MatchAllDocsQuery(), + "field", + pattern, + caseInSensitiveAutomaton + ); assertNotEquals(csQ, ciQ); assertNotEquals(csQ.hashCode(), ciQ.hashCode()); // Same query should be equal Automaton caseSensitiveAutomaton2 = WildcardQuery.toAutomaton(new Term("field", pattern)); - BinaryDvConfirmedAutomatonQuery csQ2 = new BinaryDvConfirmedAutomatonQuery(new MatchAllDocsQuery(), - "field", pattern, caseSensitiveAutomaton2); + BinaryDvConfirmedAutomatonQuery csQ2 = new BinaryDvConfirmedAutomatonQuery( + new MatchAllDocsQuery(), + "field", + pattern, + caseSensitiveAutomaton2 + ); assertEquals(csQ, csQ2); assertEquals(csQ.hashCode(), csQ2.hashCode()); } @@ -664,8 +716,7 @@ protected Object getSampleValueForDocument() { @Override protected void registerParameters(ParameterChecker checker) throws IOException { checker.registerConflictCheck("null_value", b -> b.field("null_value", "foo")); - checker.registerUpdateCheck(b -> b.field("ignore_above", 256), - m -> assertEquals(256, ((WildcardFieldMapper)m).ignoreAbove())); + checker.registerUpdateCheck(b -> b.field("ignore_above", 256), m -> assertEquals(256, ((WildcardFieldMapper) m).ignoreAbove())); } @@ -746,8 +797,7 @@ public void testFuzzyAcceleration() throws IOException, ParseException { new FuzzyTest("123456", 0, Fuzziness.ONE, null, 1, "113 355"), new FuzzyTest("1234567890", 2, Fuzziness.ONE, "_11", 1, "335 577"), new FuzzyTest("12345678901", 2, Fuzziness.ONE, "_11", 2, "335 577 901"), - new FuzzyTest("12345678", 4, Fuzziness.ONE, "_11 113 133", 0, null) - }; + new FuzzyTest("12345678", 4, Fuzziness.ONE, "_11 113 133", 0, null) }; for (FuzzyTest test : tests) { Query wildcardFieldQuery = test.getFuzzyQuery(); testExpectedAccelerationQuery(test.pattern, wildcardFieldQuery, getSimplifiedApproximationQuery(test.getExpectedApproxQuery())); @@ -759,11 +809,7 @@ static class RangeTest { String upper; String ngrams; - RangeTest( - String lower, - String upper, - String ngrams - ) { + RangeTest(String lower, String upper, String ngrams) { super(); this.lower = lower; this.upper = upper; @@ -793,8 +839,11 @@ public void testRangeAcceleration() throws IOException, ParseException { RangeTest[] tests = { new RangeTest("c:/a.txt", "c:/z.txt", "_c/ c//"), - new RangeTest("C:/ProgramFiles/a.txt", "C:/ProgramFiles/z/txt", "_c/ c// //o /oq oqo qog ogq gqa qam ame mei eik ike kes es/"), - }; + new RangeTest( + "C:/ProgramFiles/a.txt", + "C:/ProgramFiles/z/txt", + "_c/ c// //o /oq oqo qog ogq gqa qam ame mei eik ike kes es/" + ), }; for (RangeTest test : tests) { Query wildcardFieldQuery = test.getRangeQuery(); testExpectedAccelerationQuery(test.lower + "-" + test.upper, wildcardFieldQuery, test.getExpectedApproxQuery()); @@ -817,6 +866,7 @@ private Query unwrapAnyConstantScore(Query q) { return q; } } + private Query unwrapAnyBoost(Query q) { if (q instanceof BoostQuery) { BoostQuery csq = (BoostQuery) q; @@ -824,16 +874,20 @@ private Query unwrapAnyBoost(Query q) { } else { return q; } - } - + } void testExpectedAccelerationQuery(String regex, Query combinedQuery, Query expectedAccelerationQuery) throws ParseException, IOException { BinaryDvConfirmedAutomatonQuery cq = (BinaryDvConfirmedAutomatonQuery) unwrapAnyConstantScore(combinedQuery); Query approximationQuery = cq.getApproximationQuery(); approximationQuery = getSimplifiedApproximationQuery(approximationQuery); - String message = "regex: "+ regex +"\nactual query: " + formatQuery(approximationQuery) + - "\nexpected query: " + formatQuery(expectedAccelerationQuery) + "\n"; + String message = "regex: " + + regex + + "\nactual query: " + + formatQuery(approximationQuery) + + "\nexpected query: " + + formatQuery(expectedAccelerationQuery) + + "\n"; assertEquals(message, expectedAccelerationQuery, approximationQuery); } @@ -847,7 +901,7 @@ protected Query getSimplifiedApproximationQuery(Query approximationQuery) throws break; } approximationQuery = newApprox; - + } assertTrue(numRewrites < maxNumRewrites); approximationQuery = rewriteFiltersToMustsForComparisonPurposes(approximationQuery); @@ -859,10 +913,10 @@ private Query rewriteFiltersToMustsForComparisonPurposes(Query q) { q = unwrapAnyConstantScore(q); if (q instanceof BooleanQuery) { BooleanQuery.Builder result = new BooleanQuery.Builder(); - BooleanQuery bq = (BooleanQuery)q; - for (BooleanClause cq : bq.clauses()){ + BooleanQuery bq = (BooleanQuery) q; + for (BooleanClause cq : bq.clauses()) { Query rewritten = rewriteFiltersToMustsForComparisonPurposes(cq.getQuery()); - if(cq.getOccur() == Occur.FILTER) { + if (cq.getOccur() == Occur.FILTER) { result.add(rewritten, Occur.MUST); } else { result.add(rewritten, cq.getOccur()); @@ -875,20 +929,20 @@ private Query rewriteFiltersToMustsForComparisonPurposes(Query q) { } private String getRandomFuzzyPattern(HashSet values, int edits, int prefixLength) { - assert edits >=0 && edits <=2; + assert edits >= 0 && edits <= 2; // Pick one of the indexed document values to focus our queries on. - String randomValue = values.toArray(new String[0])[randomIntBetween(0, values.size()-1)]; + String randomValue = values.toArray(new String[0])[randomIntBetween(0, values.size() - 1)]; if (edits == 0) { return randomValue; } if (randomValue.length() > prefixLength) { - randomValue = randomValue.substring(0,prefixLength) + "C" + randomValue.substring(prefixLength); + randomValue = randomValue.substring(0, prefixLength) + "C" + randomValue.substring(prefixLength); edits--; } - if(edits > 0) { + if (edits > 0) { randomValue = randomValue + "a"; } return randomValue; @@ -896,118 +950,134 @@ private String getRandomFuzzyPattern(HashSet values, int edits, int pref private TermRangeQuery getRandomRange(HashSet values) { // Pick one of the indexed document values to focus our queries on. - String randomValue = values.toArray(new String[0])[randomIntBetween(0, values.size()-1)]; + String randomValue = values.toArray(new String[0])[randomIntBetween(0, values.size() - 1)]; StringBuilder upper = new StringBuilder(); - //Pick a part of the string to change - int substitutionPoint = randomIntBetween(0, randomValue.length()-1); + // Pick a part of the string to change + int substitutionPoint = randomIntBetween(0, randomValue.length() - 1); int substitutionLength = randomIntBetween(1, Math.min(10, randomValue.length() - substitutionPoint)); - //Add any head to the result, unchanged - if(substitutionPoint >0) { - upper.append(randomValue.substring(0,substitutionPoint)); + // Add any head to the result, unchanged + if (substitutionPoint > 0) { + upper.append(randomValue.substring(0, substitutionPoint)); } // Modify the middle... - String replacementPart = randomValue.substring(substitutionPoint, substitutionPoint+substitutionLength); + String replacementPart = randomValue.substring(substitutionPoint, substitutionPoint + substitutionLength); // .-replace all a chars with z upper.append(replacementPart.replaceAll("a", "z")); - //add any remaining tail, unchanged - if(substitutionPoint + substitutionLength <= randomValue.length()-1) { + // add any remaining tail, unchanged + if (substitutionPoint + substitutionLength <= randomValue.length() - 1) { upper.append(randomValue.substring(substitutionPoint + substitutionLength)); } - return new TermRangeQuery(WILDCARD_FIELD_NAME, new BytesRef(randomValue), new BytesRef(upper.toString()), - randomBoolean(), randomBoolean()); + return new TermRangeQuery( + WILDCARD_FIELD_NAME, + new BytesRef(randomValue), + new BytesRef(upper.toString()), + randomBoolean(), + randomBoolean() + ); } private String getRandomRegexPattern(HashSet values) { // Pick one of the indexed document values to focus our queries on. - String randomValue = values.toArray(new String[0])[randomIntBetween(0, values.size()-1)]; + String randomValue = values.toArray(new String[0])[randomIntBetween(0, values.size() - 1)]; return convertToRandomRegex(randomValue); } // Produces a random regex string guaranteed to match the provided value protected String convertToRandomRegex(String randomValue) { StringBuilder result = new StringBuilder(); - //Pick a part of the string to change - int substitutionPoint = randomIntBetween(0, randomValue.length()-1); + // Pick a part of the string to change + int substitutionPoint = randomIntBetween(0, randomValue.length() - 1); int substitutionLength = randomIntBetween(1, Math.min(10, randomValue.length() - substitutionPoint)); - //Add any head to the result, unchanged - if(substitutionPoint >0) { - result.append(randomValue.substring(0,substitutionPoint)); + // Add any head to the result, unchanged + if (substitutionPoint > 0) { + result.append(randomValue.substring(0, substitutionPoint)); } // Modify the middle... - String replacementPart = randomValue.substring(substitutionPoint, substitutionPoint+substitutionLength); + String replacementPart = randomValue.substring(substitutionPoint, substitutionPoint + substitutionLength); int mutation = randomIntBetween(0, 11); switch (mutation) { - case 0: - // OR with random alpha of same length - result.append("("+replacementPart+"|c"+ randomABString(replacementPart.length())+")"); - break; - case 1: - // OR with non-existant value - result.append("("+replacementPart+"|doesnotexist)"); - break; - case 2: - // OR with another randomised regex (used to create nested levels of expression). - result.append("(" + convertToRandomRegex(replacementPart) +"|doesnotexist)"); - break; - case 3: - // Star-replace all ab sequences. - result.append(replacementPart.replaceAll("ab", ".*")); - break; - case 4: - // .-replace all b chars - result.append(replacementPart.replaceAll("b", ".")); - break; - case 5: - // length-limited stars {1,2} - result.append(".{1,"+replacementPart.length()+"}"); - break; - case 6: - // replace all chars with . - result.append(replacementPart.replaceAll(".", ".")); - break; - case 7: - // OR with uppercase chars eg [aA] (many of these sorts of expression in the wild.. - char [] chars = replacementPart.toCharArray(); - for (char c : chars) { - result.append("[" + c + Character.toUpperCase(c) +"]"); - } - break; - case 8: - // NOT a character - replace all b's with "not a" - result.append(replacementPart.replaceAll("b", "[^a]")); - break; - case 9: - // Make whole part repeatable 1 or more times - result.append("(" + replacementPart +")+"); - break; - case 10: - // Make whole part repeatable 0 or more times - result.append("(" + replacementPart +")?"); - break; - case 11: - // all but ... syntax - result.append("@&~(doesnotexist.+)"); - break; - default: - break; + case 0: + // OR with random alpha of same length + result.append("(" + replacementPart + "|c" + randomABString(replacementPart.length()) + ")"); + break; + case 1: + // OR with non-existant value + result.append("(" + replacementPart + "|doesnotexist)"); + break; + case 2: + // OR with another randomised regex (used to create nested levels of expression). + result.append("(" + convertToRandomRegex(replacementPart) + "|doesnotexist)"); + break; + case 3: + // Star-replace all ab sequences. + result.append(replacementPart.replaceAll("ab", ".*")); + break; + case 4: + // .-replace all b chars + result.append(replacementPart.replaceAll("b", ".")); + break; + case 5: + // length-limited stars {1,2} + result.append(".{1," + replacementPart.length() + "}"); + break; + case 6: + // replace all chars with . + result.append(replacementPart.replaceAll(".", ".")); + break; + case 7: + // OR with uppercase chars eg [aA] (many of these sorts of expression in the wild.. + char[] chars = replacementPart.toCharArray(); + for (char c : chars) { + result.append("[" + c + Character.toUpperCase(c) + "]"); + } + break; + case 8: + // NOT a character - replace all b's with "not a" + result.append(replacementPart.replaceAll("b", "[^a]")); + break; + case 9: + // Make whole part repeatable 1 or more times + result.append("(" + replacementPart + ")+"); + break; + case 10: + // Make whole part repeatable 0 or more times + result.append("(" + replacementPart + ")?"); + break; + case 11: + // all but ... syntax + result.append("@&~(doesnotexist.+)"); + break; + default: + break; } - //add any remaining tail, unchanged - if(substitutionPoint + substitutionLength <= randomValue.length()-1) { + // add any remaining tail, unchanged + if (substitutionPoint + substitutionLength <= randomValue.length() - 1) { result.append(randomValue.substring(substitutionPoint + substitutionLength)); } - //Assert our randomly generated regex actually matches the provided raw input. + // Assert our randomly generated regex actually matches the provided raw input. RegExp regex = new RegExp(result.toString()); Automaton automaton = regex.toAutomaton(); ByteRunAutomaton bytesMatcher = new ByteRunAutomaton(automaton); BytesRef br = new BytesRef(randomValue); - assertTrue("[" + result.toString() + "]should match [" + randomValue + "]" + substitutionPoint + "-" + substitutionLength + "/" - + randomValue.length(), bytesMatcher.run(br.bytes, br.offset, br.length)); + assertTrue( + "[" + + result.toString() + + "]should match [" + + randomValue + + "]" + + substitutionPoint + + "-" + + substitutionLength + + "/" + + randomValue.length(), + bytesMatcher.run(br.bytes, br.offset, br.length) + ); return result.toString(); } @@ -1021,17 +1091,39 @@ protected MappedFieldType provideMappedFieldType(String name) { protected final SearchExecutionContext createMockContext() { Index index = new Index(randomAlphaOfLengthBetween(1, 10), "_na_"); - IndexSettings idxSettings = IndexSettingsModule.newIndexSettings(index, - Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).build()); + IndexSettings idxSettings = IndexSettingsModule.newIndexSettings( + index, + Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).build() + ); BitsetFilterCache bitsetFilterCache = new BitsetFilterCache(idxSettings, Mockito.mock(BitsetFilterCache.Listener.class)); - TriFunction, IndexFieldData> indexFieldDataLookup = - (fieldType, fieldIndexName, searchLookup) -> { + TriFunction, IndexFieldData> indexFieldDataLookup = ( + fieldType, + fieldIndexName, + searchLookup) -> { IndexFieldData.Builder builder = fieldType.fielddataBuilder(fieldIndexName, searchLookup); return builder.build(new IndexFieldDataCache.None(), null); }; - return new SearchExecutionContext(0, 0, idxSettings, bitsetFilterCache, indexFieldDataLookup, - null, null, null, null, xContentRegistry(), null, null, null, - () -> randomNonNegativeLong(), null, null, () -> true, null, emptyMap()) { + return new SearchExecutionContext( + 0, + 0, + idxSettings, + bitsetFilterCache, + indexFieldDataLookup, + null, + null, + null, + null, + xContentRegistry(), + null, + null, + null, + () -> randomNonNegativeLong(), + null, + null, + () -> true, + null, + emptyMap() + ) { @Override public MappedFieldType getFieldType(String name) { return provideMappedFieldType(name); @@ -1061,12 +1153,16 @@ private void indexDoc(LuceneDocument parseDoc, Document doc, RandomIndexWriter i protected IndexSettings createIndexSettings(Version version) { return new IndexSettings( - IndexMetadata.builder("_index").settings(Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, version)) - .numberOfShards(1).numberOfReplicas(0).creationDate(System.currentTimeMillis()).build(), - Settings.EMPTY); + IndexMetadata.builder("_index") + .settings(Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, version)) + .numberOfShards(1) + .numberOfReplicas(0) + .creationDate(System.currentTimeMillis()) + .build(), + Settings.EMPTY + ); } - static String randomABString(int minLength) { StringBuilder sb = new StringBuilder(); while (sb.length() < minLength) { @@ -1085,20 +1181,20 @@ static String randomABString(int minLength) { private void randomSyntaxChar(StringBuilder sb) { switch (randomInt(3)) { - case 0: - sb.append(WildcardQuery.WILDCARD_CHAR); - break; - case 1: - sb.append(WildcardQuery.WILDCARD_STRING); - break; - case 2: - sb.append(WildcardQuery.WILDCARD_ESCAPE); - sb.append(WildcardQuery.WILDCARD_STRING); - break; - case 3: - sb.append(WildcardQuery.WILDCARD_ESCAPE); - sb.append(WildcardQuery.WILDCARD_CHAR); - break; + case 0: + sb.append(WildcardQuery.WILDCARD_CHAR); + break; + case 1: + sb.append(WildcardQuery.WILDCARD_STRING); + break; + case 2: + sb.append(WildcardQuery.WILDCARD_ESCAPE); + sb.append(WildcardQuery.WILDCARD_STRING); + break; + case 3: + sb.append(WildcardQuery.WILDCARD_ESCAPE); + sb.append(WildcardQuery.WILDCARD_CHAR); + break; } } diff --git a/x-pack/plugin/wildcard/src/test/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldTypeTests.java b/x-pack/plugin/wildcard/src/test/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldTypeTests.java index 1b7a8453186ef..c7724e58ee181 100644 --- a/x-pack/plugin/wildcard/src/test/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldTypeTests.java +++ b/x-pack/plugin/wildcard/src/test/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldTypeTests.java @@ -18,23 +18,19 @@ public class WildcardFieldTypeTests extends FieldTypeTestCase { public void testFetchSourceValue() throws IOException { - MappedFieldType mapper = new WildcardFieldMapper.Builder("field", Version.CURRENT) - .build(MapperBuilderContext.ROOT) - .fieldType(); + MappedFieldType mapper = new WildcardFieldMapper.Builder("field", Version.CURRENT).build(MapperBuilderContext.ROOT).fieldType(); assertEquals(List.of("value"), fetchSourceValue(mapper, "value")); assertEquals(List.of("42"), fetchSourceValue(mapper, 42L)); assertEquals(List.of("true"), fetchSourceValue(mapper, true)); - MappedFieldType ignoreAboveMapper = new WildcardFieldMapper.Builder("field", Version.CURRENT) - .ignoreAbove(4) + MappedFieldType ignoreAboveMapper = new WildcardFieldMapper.Builder("field", Version.CURRENT).ignoreAbove(4) .build(MapperBuilderContext.ROOT) .fieldType(); assertEquals(List.of(), fetchSourceValue(ignoreAboveMapper, "value")); assertEquals(List.of("42"), fetchSourceValue(ignoreAboveMapper, 42L)); assertEquals(List.of("true"), fetchSourceValue(ignoreAboveMapper, true)); - MappedFieldType nullValueMapper = new WildcardFieldMapper.Builder("field", Version.CURRENT) - .nullValue("NULL") + MappedFieldType nullValueMapper = new WildcardFieldMapper.Builder("field", Version.CURRENT).nullValue("NULL") .build(MapperBuilderContext.ROOT) .fieldType(); assertEquals(List.of("NULL"), fetchSourceValue(nullValueMapper, null)); diff --git a/x-pack/qa/core-rest-tests-with-security/src/test/java/org/elasticsearch/xpack/security/CoreWithSecurityClientYamlTestSuiteIT.java b/x-pack/qa/core-rest-tests-with-security/src/test/java/org/elasticsearch/xpack/security/CoreWithSecurityClientYamlTestSuiteIT.java index ea509a3c1fb04..e19f1fba91b30 100644 --- a/x-pack/qa/core-rest-tests-with-security/src/test/java/org/elasticsearch/xpack/security/CoreWithSecurityClientYamlTestSuiteIT.java +++ b/x-pack/qa/core-rest-tests-with-security/src/test/java/org/elasticsearch/xpack/security/CoreWithSecurityClientYamlTestSuiteIT.java @@ -10,6 +10,7 @@ import com.carrotsearch.randomizedtesting.annotations.Name; import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; import com.carrotsearch.randomizedtesting.annotations.TimeoutSuite; + import org.apache.lucene.util.TimeUnits; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; @@ -37,10 +38,6 @@ public static Iterable parameters() throws Exception { @Override protected Settings restClientSettings() { String token = basicAuthHeaderValue(USER, new SecureString(PASS.toCharArray())); - return Settings.builder() - .put(super.restClientSettings()) - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(super.restClientSettings()).put(ThreadContext.PREFIX + ".Authorization", token).build(); } } - diff --git a/x-pack/qa/evil-tests/src/test/java/org/elasticsearch/xpack/core/scheduler/EvilSchedulerEngineTests.java b/x-pack/qa/evil-tests/src/test/java/org/elasticsearch/xpack/core/scheduler/EvilSchedulerEngineTests.java index 45065b7c1f61e..92f5ad449d7b8 100644 --- a/x-pack/qa/evil-tests/src/test/java/org/elasticsearch/xpack/core/scheduler/EvilSchedulerEngineTests.java +++ b/x-pack/qa/evil-tests/src/test/java/org/elasticsearch/xpack/core/scheduler/EvilSchedulerEngineTests.java @@ -53,16 +53,14 @@ public void testOutOfMemoryErrorWhileTriggeredIsRethrownAndIsUncaught() throws I } }); final CountDownLatch schedulerLatch = new CountDownLatch(1); - engine.add(new SchedulerEngine.Job( - getTestName(), - (startTime, now) -> { - if (schedulerLatch.getCount() == 1) { - schedulerLatch.countDown(); - return 0; - } else { - throw new AssertionError("nextScheduledTimeAfter invoked more than the expected number of times"); - } - })); + engine.add(new SchedulerEngine.Job(getTestName(), (startTime, now) -> { + if (schedulerLatch.getCount() == 1) { + schedulerLatch.countDown(); + return 0; + } else { + throw new AssertionError("nextScheduledTimeAfter invoked more than the expected number of times"); + } + })); uncaughtLatuch.await(); assertTrue(trigger.get()); diff --git a/x-pack/qa/evil-tests/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosTestCase.java b/x-pack/qa/evil-tests/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosTestCase.java index 6fc86b3786716..44b1a6ce51b50 100644 --- a/x-pack/qa/evil-tests/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosTestCase.java +++ b/x-pack/qa/evil-tests/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosTestCase.java @@ -21,8 +21,6 @@ import org.junit.Before; import org.junit.BeforeClass; -import javax.security.auth.Subject; - import java.io.IOException; import java.nio.file.Path; import java.security.AccessController; @@ -33,6 +31,8 @@ import java.util.Locale; import java.util.Set; +import javax.security.auth.Subject; + /** * Base Test class for Kerberos. *

    @@ -81,14 +81,17 @@ public abstract class KerberosTestCase extends ESTestCase { "ur", "pa", "ig", - "sd"); + "sd" + ); @BeforeClass public static void setupKerberos() throws Exception { if (isLocaleUnsupported()) { Logger logger = LogManager.getLogger(KerberosTestCase.class); - logger.warn("Attempting to run Kerberos test on {} locale, but that breaks SimpleKdcServer. Switching to English.", - Locale.getDefault()); + logger.warn( + "Attempting to run Kerberos test on {} locale, but that breaks SimpleKdcServer. Switching to English.", + Locale.getDefault() + ); restoreLocale = Locale.getDefault(); Locale.setDefault(Locale.ENGLISH); } @@ -116,9 +119,7 @@ public void startSimpleKdcLdapServer() throws Exception { // Create SPNs and UPNs serviceUserNames = new ArrayList<>(); - Randomness.get().ints(randomIntBetween(1, 6)).forEach((i) -> { - serviceUserNames.add("HTTP/" + randomAlphaOfLength(8)); - }); + Randomness.get().ints(randomIntBetween(1, 6)).forEach((i) -> { serviceUserNames.add("HTTP/" + randomAlphaOfLength(8)); }); final Path ktabPathForService = createPrincipalKeyTab(workDir, serviceUserNames.toArray(new String[0])); clientUserNames = new ArrayList<>(); Randomness.get().ints(randomIntBetween(1, 6)).forEach((i) -> { @@ -130,7 +131,7 @@ public void startSimpleKdcLdapServer() throws Exception { throw ExceptionsHelper.convertToRuntime(e); } }); - settings = KerberosRealmTestCase.buildKerberosRealmSettings(REALM_NAME, ktabPathForService.toString()); + settings = KerberosRealmTestCase.buildKerberosRealmSettings(REALM_NAME, ktabPathForService.toString()); } @After diff --git a/x-pack/qa/evil-tests/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosTicketValidatorTests.java b/x-pack/qa/evil-tests/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosTicketValidatorTests.java index 66484d0dd26ab..0c3e0f8ed626e 100644 --- a/x-pack/qa/evil-tests/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosTicketValidatorTests.java +++ b/x-pack/qa/evil-tests/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosTicketValidatorTests.java @@ -9,20 +9,21 @@ import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.support.PlainActionFuture; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.util.concurrent.UncategorizedExecutionException; +import org.elasticsearch.core.Tuple; import org.elasticsearch.env.Environment; import org.elasticsearch.env.TestEnvironment; import org.ietf.jgss.GSSException; -import javax.security.auth.login.LoginException; import java.io.IOException; import java.nio.file.Path; import java.security.PrivilegedActionException; import java.util.Base64; import java.util.concurrent.ExecutionException; +import javax.security.auth.login.LoginException; + import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.is; @@ -38,9 +39,14 @@ public void testKerbTicketGeneratedForDifferentServerFailsValidation() throws Ex // Client login and init token preparation final String clientUserName = randomFrom(clientUserNames); - try (SpnegoClient spnegoClient = new SpnegoClient(principalName(clientUserName), - new SecureString("spnego-test-password".toCharArray()), principalName("differentServer"), - randomFrom(KerberosTicketValidator.SUPPORTED_OIDS))) { + try ( + SpnegoClient spnegoClient = new SpnegoClient( + principalName(clientUserName), + new SecureString("spnego-test-password".toCharArray()), + principalName("differentServer"), + randomFrom(KerberosTicketValidator.SUPPORTED_OIDS) + ) + ) { final String base64KerbToken = spnegoClient.getBase64EncodedTokenForSpnegoHeader(); assertThat(base64KerbToken, is(notNullValue())); @@ -58,32 +64,41 @@ public void testInvalidKerbTicketFailsValidation() throws Exception { final Environment env = TestEnvironment.newEnvironment(globalSettings); final Path keytabPath = getKeytabPath(env); - kerberosTicketValidator.validateTicket(Base64.getDecoder().decode(base64KerbToken), keytabPath, true, - new ActionListener>() { - boolean exceptionHandled = false; - - @Override - public void onResponse(Tuple response) { - fail("expected exception to be thrown of type GSSException"); - } - - @Override - public void onFailure(Exception e) { - assertThat(exceptionHandled, is(false)); - assertThat(e, instanceOf(GSSException.class)); - assertThat(((GSSException) e).getMajor(), equalTo(GSSException.DEFECTIVE_TOKEN)); - exceptionHandled = true; - } - }); + kerberosTicketValidator.validateTicket( + Base64.getDecoder().decode(base64KerbToken), + keytabPath, + true, + new ActionListener>() { + boolean exceptionHandled = false; + + @Override + public void onResponse(Tuple response) { + fail("expected exception to be thrown of type GSSException"); + } + + @Override + public void onFailure(Exception e) { + assertThat(exceptionHandled, is(false)); + assertThat(e, instanceOf(GSSException.class)); + assertThat(((GSSException) e).getMajor(), equalTo(GSSException.DEFECTIVE_TOKEN)); + exceptionHandled = true; + } + } + ); } - public void testWhenKeyTabWithInvalidContentFailsValidation() - throws LoginException, GSSException, IOException, PrivilegedActionException { + public void testWhenKeyTabWithInvalidContentFailsValidation() throws LoginException, GSSException, IOException, + PrivilegedActionException { // Client login and init token preparation final String clientUserName = randomFrom(clientUserNames); - try (SpnegoClient spnegoClient = new SpnegoClient(principalName(clientUserName), - new SecureString("spnego-test-password".toCharArray()), principalName(randomFrom(serviceUserNames)), - randomFrom(KerberosTicketValidator.SUPPORTED_OIDS));) { + try ( + SpnegoClient spnegoClient = new SpnegoClient( + principalName(clientUserName), + new SecureString("spnego-test-password".toCharArray()), + principalName(randomFrom(serviceUserNames)), + randomFrom(KerberosTicketValidator.SUPPORTED_OIDS) + ); + ) { final String base64KerbToken = spnegoClient.getBase64EncodedTokenForSpnegoHeader(); assertThat(base64KerbToken, is(notNullValue())); @@ -103,8 +118,14 @@ public void testValidKebrerosTicket() throws PrivilegedActionException, GSSExcep final String clientUserName = randomFrom(clientUserNames); final SecureString password = new SecureString("spnego-test-password".toCharArray()); final String servicePrincipalName = principalName(randomFrom(serviceUserNames)); - try (SpnegoClient spnegoClient = new SpnegoClient(principalName(clientUserName), password, servicePrincipalName, - randomFrom(KerberosTicketValidator.SUPPORTED_OIDS))) { + try ( + SpnegoClient spnegoClient = new SpnegoClient( + principalName(clientUserName), + password, + servicePrincipalName, + randomFrom(KerberosTicketValidator.SUPPORTED_OIDS) + ) + ) { final String base64KerbToken = spnegoClient.getBase64EncodedTokenForSpnegoHeader(); assertThat(base64KerbToken, is(notNullValue())); diff --git a/x-pack/qa/evil-tests/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/SimpleKdcLdapServer.java b/x-pack/qa/evil-tests/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/SimpleKdcLdapServer.java index 7363c161a60da..eabb551d1fcd0 100644 --- a/x-pack/qa/evil-tests/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/SimpleKdcLdapServer.java +++ b/x-pack/qa/evil-tests/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/SimpleKdcLdapServer.java @@ -142,8 +142,17 @@ private void createLdapServiceAndStart() throws Exception { private void createLdapBackendConf() throws IOException { String backendConf = KdcConfigKey.KDC_IDENTITY_BACKEND.getPropertyKey() - + " = org.apache.kerby.kerberos.kdc.identitybackend.LdapIdentityBackend\n" + "host=127.0.0.1\n" + "port=" + ldapPort + "\n" - + "admin_dn=uid=admin,ou=system," + baseDn + "\n" + "admin_pw=secret\n" + "base_dn=" + baseDn; + + " = org.apache.kerby.kerberos.kdc.identitybackend.LdapIdentityBackend\n" + + "host=127.0.0.1\n" + + "port=" + + ldapPort + + "\n" + + "admin_dn=uid=admin,ou=system," + + baseDn + + "\n" + + "admin_pw=secret\n" + + "base_dn=" + + baseDn; Files.write(this.workDir.resolve("backend.conf"), backendConf.getBytes(StandardCharsets.UTF_8)); assert Files.exists(this.workDir.resolve("backend.conf")); } @@ -250,8 +259,9 @@ public Void run() throws Exception { private static int getServerPort(String transport) { if (transport != null && transport.trim().equalsIgnoreCase("TCP")) { - try (ServerSocket serverSocket = ServerSocketFactory.getDefault().createServerSocket(0, 1, - InetAddress.getByName("127.0.0.1"))) { + try ( + ServerSocket serverSocket = ServerSocketFactory.getDefault().createServerSocket(0, 1, InetAddress.getByName("127.0.0.1")) + ) { serverSocket.setReuseAddress(true); return serverSocket.getLocalPort(); } catch (Exception ex) { diff --git a/x-pack/qa/evil-tests/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/SimpleKdcLdapServerTests.java b/x-pack/qa/evil-tests/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/SimpleKdcLdapServerTests.java index c4bfb0e561e29..c80c4fb7bb5d1 100644 --- a/x-pack/qa/evil-tests/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/SimpleKdcLdapServerTests.java +++ b/x-pack/qa/evil-tests/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/SimpleKdcLdapServerTests.java @@ -12,8 +12,8 @@ import com.unboundid.ldap.sdk.SearchScope; import org.elasticsearch.action.support.PlainActionFuture; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.settings.SecureString; +import org.elasticsearch.core.Tuple; import org.elasticsearch.env.Environment; import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.xpack.security.authc.ldap.support.LdapUtils; @@ -30,16 +30,19 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.nullValue; public class SimpleKdcLdapServerTests extends KerberosTestCase { public void testPrincipalCreationAndSearchOnLdap() throws Exception { simpleKdcLdapServer.createPrincipal(workDir.resolve("p1p2.keytab"), "p1", "p2"); assertTrue(Files.exists(workDir.resolve("p1p2.keytab"))); - try (LDAPConnection ldapConn = - LdapUtils.privilegedConnect(() -> new LDAPConnection("localhost", simpleKdcLdapServer.getLdapListenPort()));) { + try ( + LDAPConnection ldapConn = LdapUtils.privilegedConnect( + () -> new LDAPConnection("localhost", simpleKdcLdapServer.getLdapListenPort()) + ); + ) { assertThat(ldapConn.isConnected(), is(true)); SearchResult sr = ldapConn.search("dc=example,dc=com", SearchScope.SUB, "(krb5PrincipalName=p1@EXAMPLE.COM)"); assertThat(sr.getSearchEntries(), hasSize(1)); @@ -51,9 +54,14 @@ public void testClientServiceMutualAuthentication() throws PrivilegedActionExcep final String serviceUserName = randomFrom(serviceUserNames); // Client login and init token preparation final String clientUserName = randomFrom(clientUserNames); - try (SpnegoClient spnegoClient = new SpnegoClient(principalName(clientUserName), - new SecureString("spnego-test-password".toCharArray()), principalName(serviceUserName), - randomFrom(KerberosTicketValidator.SUPPORTED_OIDS));) { + try ( + SpnegoClient spnegoClient = new SpnegoClient( + principalName(clientUserName), + new SecureString("spnego-test-password".toCharArray()), + principalName(serviceUserName), + randomFrom(KerberosTicketValidator.SUPPORTED_OIDS) + ); + ) { final String base64KerbToken = spnegoClient.getBase64EncodedTokenForSpnegoHeader(); assertThat(base64KerbToken, is(notNullValue())); final KerberosAuthenticationToken kerbAuthnToken = new KerberosAuthenticationToken(Base64.getDecoder().decode(base64KerbToken)); diff --git a/x-pack/qa/evil-tests/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/SpnegoClient.java b/x-pack/qa/evil-tests/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/SpnegoClient.java index 41fae8a9d9ae0..0443742f11c0a 100644 --- a/x-pack/qa/evil-tests/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/SpnegoClient.java +++ b/x-pack/qa/evil-tests/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/SpnegoClient.java @@ -10,8 +10,8 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.ExceptionsHelper; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.common.settings.SecureString; +import org.elasticsearch.core.SuppressForbidden; import org.ietf.jgss.GSSContext; import org.ietf.jgss.GSSCredential; import org.ietf.jgss.GSSException; @@ -19,6 +19,16 @@ import org.ietf.jgss.GSSName; import org.ietf.jgss.Oid; +import java.io.IOException; +import java.security.AccessController; +import java.security.Principal; +import java.security.PrivilegedActionException; +import java.security.PrivilegedExceptionAction; +import java.util.Base64; +import java.util.Collections; +import java.util.Map; +import java.util.Set; + import javax.security.auth.Subject; import javax.security.auth.callback.Callback; import javax.security.auth.callback.CallbackHandler; @@ -30,16 +40,6 @@ import javax.security.auth.login.LoginContext; import javax.security.auth.login.LoginException; -import java.io.IOException; -import java.security.AccessController; -import java.security.Principal; -import java.security.PrivilegedActionException; -import java.security.PrivilegedExceptionAction; -import java.util.Base64; -import java.util.Collections; -import java.util.Map; -import java.util.Set; - /** * This class is used as a Spnego client during testing and handles SPNEGO * interactions using GSS context negotiation.
    @@ -74,20 +74,31 @@ class SpnegoClient implements AutoCloseable { * @throws GSSException thrown when GSS API error occurs */ SpnegoClient(final String userPrincipalName, final SecureString password, final String servicePrincipalName, final Oid mechanism) - throws PrivilegedActionException, GSSException { + throws PrivilegedActionException, GSSException { String oldUseSubjectCredsOnlyFlag = null; try { oldUseSubjectCredsOnlyFlag = getAndSetUseSubjectCredsOnlySystemProperty("true"); LOGGER.info("SpnegoClient with userPrincipalName : {}", userPrincipalName); final GSSName gssUserPrincipalName = gssManager.createName(userPrincipalName, GSSName.NT_USER_NAME); final GSSName gssServicePrincipalName = gssManager.createName(servicePrincipalName, GSSName.NT_USER_NAME); - loginContext = AccessController - .doPrivileged((PrivilegedExceptionAction) () -> loginUsingPassword(userPrincipalName, password)); - final GSSCredential userCreds = KerberosTestCase.doAsWrapper(loginContext.getSubject(), - (PrivilegedExceptionAction) () -> gssManager.createCredential(gssUserPrincipalName, - GSSCredential.DEFAULT_LIFETIME, mechanism, GSSCredential.INITIATE_ONLY)); - gssContext = gssManager.createContext(gssServicePrincipalName.canonicalize(mechanism), - mechanism, userCreds, GSSCredential.DEFAULT_LIFETIME); + loginContext = AccessController.doPrivileged( + (PrivilegedExceptionAction) () -> loginUsingPassword(userPrincipalName, password) + ); + final GSSCredential userCreds = KerberosTestCase.doAsWrapper( + loginContext.getSubject(), + (PrivilegedExceptionAction) () -> gssManager.createCredential( + gssUserPrincipalName, + GSSCredential.DEFAULT_LIFETIME, + mechanism, + GSSCredential.INITIATE_ONLY + ) + ); + gssContext = gssManager.createContext( + gssServicePrincipalName.canonicalize(mechanism), + mechanism, + userCreds, + GSSCredential.DEFAULT_LIFETIME + ); gssContext.requestMutualAuth(true); } catch (PrivilegedActionException pve) { LOGGER.error("privileged action exception, with root cause", pve.getException()); @@ -105,8 +116,10 @@ class SpnegoClient implements AutoCloseable { * @throws PrivilegedActionException when privileged action threw exception */ String getBase64EncodedTokenForSpnegoHeader() throws PrivilegedActionException { - final byte[] outToken = KerberosTestCase.doAsWrapper(loginContext.getSubject(), - (PrivilegedExceptionAction) () -> gssContext.initSecContext(new byte[0], 0, 0)); + final byte[] outToken = KerberosTestCase.doAsWrapper( + loginContext.getSubject(), + (PrivilegedExceptionAction) () -> gssContext.initSecContext(new byte[0], 0, 0) + ); return Base64.getEncoder().encodeToString(outToken); } @@ -124,8 +137,10 @@ String handleResponse(final String base64Token) throws PrivilegedActionException throw new IllegalStateException("GSS Context has already been established"); } final byte[] token = Base64.getDecoder().decode(base64Token); - final byte[] outToken = KerberosTestCase.doAsWrapper(loginContext.getSubject(), - (PrivilegedExceptionAction) () -> gssContext.initSecContext(token, 0, token.length)); + final byte[] outToken = KerberosTestCase.doAsWrapper( + loginContext.getSubject(), + (PrivilegedExceptionAction) () -> gssContext.initSecContext(token, 0, token.length) + ); if (outToken == null || outToken.length == 0) { return null; } @@ -197,16 +212,24 @@ static class PasswordJaasConf extends Configuration { @Override public AppConfigurationEntry[] getAppConfigurationEntry(final String name) { - return new AppConfigurationEntry[]{new AppConfigurationEntry( + return new AppConfigurationEntry[] { + new AppConfigurationEntry( SUN_KRB5_LOGIN_MODULE, AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, Map.of( - "principal", principal, - "storeKey", Boolean.TRUE.toString(), - "isInitiator", Boolean.TRUE.toString(), - "debug", Boolean.TRUE.toString(), - // refresh Krb5 config during tests as the port keeps changing for kdc server - "refreshKrb5Config", Boolean.TRUE.toString()))}; + "principal", + principal, + "storeKey", + Boolean.TRUE.toString(), + "isInitiator", + Boolean.TRUE.toString(), + "debug", + Boolean.TRUE.toString(), + // refresh Krb5 config during tests as the port keeps changing for kdc server + "refreshKrb5Config", + Boolean.TRUE.toString() + ) + ) }; } } @@ -242,7 +265,8 @@ private static String getAndSetUseSubjectCredsOnlySystemProperty(final String va @Override @SuppressForbidden( - reason = "For tests where we provide credentials, need to set and reset javax.security.auth.useSubjectCredsOnly") + reason = "For tests where we provide credentials, need to set and reset javax.security.auth.useSubjectCredsOnly" + ) public String run() throws Exception { String oldValue = System.getProperty("javax.security.auth.useSubjectCredsOnly"); if (value != null) { diff --git a/x-pack/qa/full-cluster-restart/src/test/java/org/elasticsearch/xpack/restart/CoreFullClusterRestartIT.java b/x-pack/qa/full-cluster-restart/src/test/java/org/elasticsearch/xpack/restart/CoreFullClusterRestartIT.java index 60b5cba1fde64..e06cb12f747a7 100644 --- a/x-pack/qa/full-cluster-restart/src/test/java/org/elasticsearch/xpack/restart/CoreFullClusterRestartIT.java +++ b/x-pack/qa/full-cluster-restart/src/test/java/org/elasticsearch/xpack/restart/CoreFullClusterRestartIT.java @@ -18,9 +18,7 @@ public class CoreFullClusterRestartIT extends FullClusterRestartIT { @Override protected Settings restClientSettings() { String token = "Basic " + Base64.getEncoder().encodeToString("test_user:x-pack-test-password".getBytes(StandardCharsets.UTF_8)); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } } diff --git a/x-pack/qa/full-cluster-restart/src/test/java/org/elasticsearch/xpack/restart/FullClusterRestartIT.java b/x-pack/qa/full-cluster-restart/src/test/java/org/elasticsearch/xpack/restart/FullClusterRestartIT.java index fc161c2b4c8c7..d64da74fe9080 100644 --- a/x-pack/qa/full-cluster-restart/src/test/java/org/elasticsearch/xpack/restart/FullClusterRestartIT.java +++ b/x-pack/qa/full-cluster-restart/src/test/java/org/elasticsearch/xpack/restart/FullClusterRestartIT.java @@ -69,12 +69,12 @@ public class FullClusterRestartIT extends AbstractFullClusterRestartTestCase { protected Settings restClientSettings() { String token = "Basic " + Base64.getEncoder().encodeToString("test_user:x-pack-test-password".getBytes(StandardCharsets.UTF_8)); return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - // we increase the timeout here to 90 seconds to handle long waits for a green - // cluster health. the waits for green need to be longer than a minute to - // account for delayed shards - .put(ESRestTestCase.CLIENT_SOCKET_TIMEOUT, "90s") - .build(); + .put(ThreadContext.PREFIX + ".Authorization", token) + // we increase the timeout here to 90 seconds to handle long waits for a green + // cluster health. the waits for green need to be longer than a minute to + // account for delayed shards + .put(ESRestTestCase.CLIENT_SOCKET_TIMEOUT, "90s") + .build(); } /** @@ -102,8 +102,12 @@ public void testSecurityNativeRealm() throws Exception { } else { waitForYellow(".security"); final Request getSettingsRequest = new Request("GET", "/.security/_settings/index.format"); - getSettingsRequest.setOptions(expectWarnings("this request accesses system indices: [.security-7], but in a future major " + - "version, direct access to system indices will be prevented by default")); + getSettingsRequest.setOptions( + expectWarnings( + "this request accesses system indices: [.security-7], but in a future major " + + "version, direct access to system indices will be prevented by default" + ) + ); Response settingsResponse = client().performRequest(getSettingsRequest); Map settingsResponseMap = entityAsMap(settingsResponse); logger.info("settings response map {}", settingsResponseMap); @@ -116,7 +120,7 @@ public void testSecurityNativeRealm() throws Exception { Map settingsMap = (Map) indexSettingsMap.get("settings"); logger.info("settings map {}", settingsMap); if (settingsMap.containsKey("index")) { - int format = Integer.parseInt(String.valueOf(((Map)settingsMap.get("index")).get("format"))); + int format = Integer.parseInt(String.valueOf(((Map) settingsMap.get("index")).get("format"))); assertEquals("The security index needs to be upgraded", SECURITY_EXPECTED_INDEX_FORMAT_VERSION, format); } } @@ -207,15 +211,18 @@ public void testWatcherWithApiKey() throws Exception { if (isRunningAgainstOldCluster()) { final Request createApiKeyRequest = new Request("PUT", "/_security/api_key"); - createApiKeyRequest.setJsonEntity("{\"name\":\"key-1\",\"role_descriptors\":" + - "{\"r\":{\"cluster\":[\"all\"],\"indices\":[{\"names\":[\"*\"],\"privileges\":[\"all\"]}]}}}"); + createApiKeyRequest.setJsonEntity( + "{\"name\":\"key-1\",\"role_descriptors\":" + + "{\"r\":{\"cluster\":[\"all\"],\"indices\":[{\"names\":[\"*\"],\"privileges\":[\"all\"]}]}}}" + ); final Response response = client().performRequest(createApiKeyRequest); final Map createApiKeyResponse = entityAsMap(response); Request createWatchWithApiKeyRequest = new Request("PUT", "/_watcher/watch/watch_with_api_key"); createWatchWithApiKeyRequest.setJsonEntity(loadWatch("logging-watch.json")); - final byte[] keyBytes = - (createApiKeyResponse.get("id") + ":" + createApiKeyResponse.get("api_key")).getBytes(StandardCharsets.UTF_8); + final byte[] keyBytes = (createApiKeyResponse.get("id") + ":" + createApiKeyResponse.get("api_key")).getBytes( + StandardCharsets.UTF_8 + ); final String authHeader = "ApiKey " + Base64.getEncoder().encodeToString(keyBytes); createWatchWithApiKeyRequest.setOptions(RequestOptions.DEFAULT.toBuilder().addHeader("Authorization", authHeader)); client().performRequest(createWatchWithApiKeyRequest); @@ -255,8 +262,11 @@ public void testWatcherWithApiKey() throws Exception { if (false == executed.get() && "executed".equals(newStatus.get("execution_state"))) { executed.set(true); } - assertThat("version increased: [" + versionIncreased.get() + "], executed: [" + executed.get() + "]", - versionIncreased.get() && executed.get(), is(true)); + assertThat( + "version increased: [" + versionIncreased.get() + "], executed: [" + executed.get() + "]", + versionIncreased.get() && executed.get(), + is(true) + ); }); } finally { stopWatcher(); @@ -277,8 +287,11 @@ public void testServiceAccountApiKey() throws IOException { createApiKeyRequest.setJsonEntity("{\"name\":\"key-1\"}"); final Response createApiKeyResponse = client().performRequest(createApiKeyRequest); final Map createApiKeyResponseMap = entityAsMap(createApiKeyResponse); - final String authHeader = "ApiKey " + Base64.getEncoder().encodeToString( - (createApiKeyResponseMap.get("id") + ":" + createApiKeyResponseMap.get("api_key")).getBytes(StandardCharsets.UTF_8)); + final String authHeader = "ApiKey " + + Base64.getEncoder() + .encodeToString( + (createApiKeyResponseMap.get("id") + ":" + createApiKeyResponseMap.get("api_key")).getBytes(StandardCharsets.UTF_8) + ); final Request indexRequest = new Request("PUT", "/api_keys/_doc/key-1"); indexRequest.setJsonEntity("{\"auth_header\":\"" + authHeader + "\"}"); @@ -297,8 +310,7 @@ public void testServiceAccountApiKey() throws IOException { final Request getUserRequest = new Request("GET", "/_security/user"); getUserRequest.setOptions(RequestOptions.DEFAULT.toBuilder().addHeader("Authorization", authHeader)); - final ResponseException e = - expectThrows(ResponseException.class, () -> client().performRequest(getUserRequest)); + final ResponseException e = expectThrows(ResponseException.class, () -> client().performRequest(getUserRequest)); assertThat(e.getResponse().getStatusLine().getStatusCode(), equalTo(403)); assertThat(e.getMessage(), containsString("is unauthorized")); } @@ -335,7 +347,8 @@ public void testRollupAfterRestart() throws Exception { intervalType = "interval"; } - createRollupJobRequest.setJsonEntity("{" + createRollupJobRequest.setJsonEntity( + "{" + "\"index_pattern\":\"rollup-*\"," + "\"rollup_index\":\"results-rollup\"," + "\"cron\":\"*/30 * * * * ?\"," @@ -343,13 +356,16 @@ public void testRollupAfterRestart() throws Exception { + "\"groups\":{" + " \"date_histogram\":{" + " \"field\":\"timestamp\"," - + " \"" + intervalType + "\":\"5m\"" + + " \"" + + intervalType + + "\":\"5m\"" + " }" + "}," + "\"metrics\":[" + " {\"field\":\"value\",\"metrics\":[\"min\",\"max\",\"sum\"]}" + "]" - + "}"); + + "}" + ); Map createRollupJobResponse = entityAsMap(client().performRequest(createRollupJobRequest)); assertThat(createRollupJobResponse.get("acknowledged"), equalTo(Boolean.TRUE)); @@ -375,16 +391,17 @@ public void testRollupAfterRestart() throws Exception { } public void testSlmPolicyAndStats() throws IOException { - SnapshotLifecyclePolicy slmPolicy = new SnapshotLifecyclePolicy("test-policy", "test-policy", "* * * 31 FEB ? *", "test-repo", - Collections.singletonMap("indices", Collections.singletonList("*")), null); + SnapshotLifecyclePolicy slmPolicy = new SnapshotLifecyclePolicy( + "test-policy", + "test-policy", + "* * * 31 FEB ? *", + "test-repo", + Collections.singletonMap("indices", Collections.singletonList("*")), + null + ); if (isRunningAgainstOldCluster() && getOldClusterVersion().onOrAfter(Version.V_7_4_0)) { Request createRepoRequest = new Request("PUT", "_snapshot/test-repo"); - String repoCreateJson = "{" + - " \"type\": \"fs\"," + - " \"settings\": {" + - " \"location\": \"test-repo\"" + - " }" + - "}"; + String repoCreateJson = "{" + " \"type\": \"fs\"," + " \"settings\": {" + " \"location\": \"test-repo\"" + " }" + "}"; createRepoRequest.setJsonEntity(repoCreateJson); Request createSlmPolicyRequest = new Request("PUT", "_slm/policy/test-policy"); try (XContentBuilder builder = JsonXContent.contentBuilder()) { @@ -396,7 +413,7 @@ public void testSlmPolicyAndStats() throws IOException { client().performRequest(createSlmPolicyRequest); } - if(isRunningAgainstOldCluster() == false && getOldClusterVersion().onOrAfter(Version.V_7_4_0)){ + if (isRunningAgainstOldCluster() == false && getOldClusterVersion().onOrAfter(Version.V_7_4_0)) { Request getSlmPolicyRequest = new Request("GET", "_slm/policy/test-policy"); Response response = client().performRequest(getSlmPolicyRequest); Map responseMap = entityAsMap(response); @@ -410,8 +427,14 @@ public void testSlmPolicyAndStats() throws IOException { if (isRunningAgainstOldCluster() == false) { Response response = client().performRequest(new Request("GET", "_slm/stats")); XContentType xContentType = XContentType.fromMediaType(response.getEntity().getContentType().getValue()); - try (XContentParser parser = xContentType.xContent().createParser(NamedXContentRegistry.EMPTY, - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, response.getEntity().getContent())) { + try ( + XContentParser parser = xContentType.xContent() + .createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + response.getEntity().getContent() + ) + ) { assertEquals(new SnapshotLifecycleStats(), SnapshotLifecycleStats.parse(parser)); } } @@ -460,10 +483,9 @@ private void assertWatchIndexContentsWork() throws Exception { assertThat(bwcWatch.get("found"), equalTo(true)); source = (Map) bwcWatch.get("watch"); - Map attachments = ObjectPath.eval("actions.work.email.attachments", source); Map attachment = (Map) attachments.get("test_report.pdf"); - Map request = ObjectPath.eval("http.request", attachment); + Map request = ObjectPath.eval("http.request", attachment); assertEquals(timeout, request.get("read_timeout_millis")); assertEquals("https", request.get("scheme")); assertEquals("example.com", request.get("host")); @@ -483,9 +505,9 @@ private void assertWatchIndexContentsWork() throws Exception { } private void assertBasicWatchInteractions() throws Exception { - String watch = "{\"trigger\":{\"schedule\":{\"interval\":\"1s\"}},\"input\":{\"none\":{}}," + - "\"condition\":{\"always\":{}}," + - "\"actions\":{\"awesome\":{\"logging\":{\"level\":\"info\",\"text\":\"test\"}}}}"; + String watch = "{\"trigger\":{\"schedule\":{\"interval\":\"1s\"}},\"input\":{\"none\":{}}," + + "\"condition\":{\"always\":{}}," + + "\"actions\":{\"awesome\":{\"logging\":{\"level\":\"info\",\"text\":\"test\"}}}}"; Request createWatchRequest = new Request("PUT", "_watcher/watch/new_watch"); createWatchRequest.setJsonEntity(watch); Map createWatch = entityAsMap(client().performRequest(createWatchRequest)); @@ -502,7 +524,7 @@ private void assertBasicWatchInteractions() throws Exception { Map get = entityAsMap(client().performRequest(new Request("GET", "_watcher/watch/new_watch"))); assertThat(get.get("found"), equalTo(true)); Map source = (Map) get.get("watch"); - Map logging = ObjectPath.eval("actions.awesome.logging", source); + Map logging = ObjectPath.eval("actions.awesome.logging", source); assertEquals("info", logging.get("level")); assertEquals("test", logging.get("text")); } @@ -550,8 +572,9 @@ private void startWatcher() throws Exception { assertThat(startWatchResponse.get("acknowledged"), equalTo(Boolean.TRUE)); assertBusy(() -> { Map statsWatchResponse = entityAsMap(client().performRequest(new Request("GET", "_watcher/stats"))); - List states = ((List) statsWatchResponse.get("stats")) - .stream().map(o -> ((Map) o).get("watcher_state")).collect(Collectors.toList()); + List states = ((List) statsWatchResponse.get("stats")).stream() + .map(o -> ((Map) o).get("watcher_state")) + .collect(Collectors.toList()); assertThat(states, everyItem(is("started"))); }); } @@ -560,10 +583,10 @@ private void stopWatcher() throws Exception { Map stopWatchResponse = entityAsMap(client().performRequest(new Request("POST", "_watcher/_stop"))); assertThat(stopWatchResponse.get("acknowledged"), equalTo(Boolean.TRUE)); assertBusy(() -> { - Map statsStoppedWatchResponse = entityAsMap(client().performRequest( - new Request("GET", "_watcher/stats"))); - List states = ((List) statsStoppedWatchResponse.get("stats")) - .stream().map(o -> ((Map) o).get("watcher_state")).collect(Collectors.toList()); + Map statsStoppedWatchResponse = entityAsMap(client().performRequest(new Request("GET", "_watcher/stats"))); + List states = ((List) statsStoppedWatchResponse.get("stats")).stream() + .map(o -> ((Map) o).get("watcher_state")) + .collect(Collectors.toList()); assertThat(states, everyItem(is("stopped"))); }); } @@ -576,13 +599,18 @@ private void createUser(final boolean oldCluster) throws Exception { final String id = oldCluster ? "preupgrade_user" : "postupgrade_user"; Request request = new Request("PUT", "/_security/user/" + id); request.setJsonEntity( - "{\n" + - " \"password\" : \"l0ng-r4nd0m-p@ssw0rd\",\n" + - " \"roles\" : [ \"admin\", \"other_role1\" ],\n" + - " \"full_name\" : \"" + randomAlphaOfLength(5) + "\",\n" + - " \"email\" : \"" + id + "@example.com\",\n" + - " \"enabled\": true\n" + - "}"); + "{\n" + + " \"password\" : \"l0ng-r4nd0m-p@ssw0rd\",\n" + + " \"roles\" : [ \"admin\", \"other_role1\" ],\n" + + " \"full_name\" : \"" + + randomAlphaOfLength(5) + + "\",\n" + + " \"email\" : \"" + + id + + "@example.com\",\n" + + " \"enabled\": true\n" + + "}" + ); client().performRequest(request); } @@ -590,26 +618,28 @@ private void createRole(final boolean oldCluster) throws Exception { final String id = oldCluster ? "preupgrade_role" : "postupgrade_role"; Request request = new Request("PUT", "/_security/role/" + id); request.setJsonEntity( - "{\n" + - " \"run_as\": [ \"abc\" ],\n" + - " \"cluster\": [ \"monitor\" ],\n" + - " \"indices\": [\n" + - " {\n" + - " \"names\": [ \"events-*\" ],\n" + - " \"privileges\": [ \"read\" ],\n" + - " \"field_security\" : {\n" + - " \"grant\" : [ \"category\", \"@timestamp\", \"message\" ]\n" + - " },\n" + - " \"query\": \"{\\\"match\\\": {\\\"category\\\": \\\"click\\\"}}\"\n" + - " }\n" + - " ]\n" + - "}"); + "{\n" + + " \"run_as\": [ \"abc\" ],\n" + + " \"cluster\": [ \"monitor\" ],\n" + + " \"indices\": [\n" + + " {\n" + + " \"names\": [ \"events-*\" ],\n" + + " \"privileges\": [ \"read\" ],\n" + + " \"field_security\" : {\n" + + " \"grant\" : [ \"category\", \"@timestamp\", \"message\" ]\n" + + " },\n" + + " \"query\": \"{\\\"match\\\": {\\\"category\\\": \\\"click\\\"}}\"\n" + + " }\n" + + " ]\n" + + "}" + ); client().performRequest(request); } private void assertUserInfo(final boolean oldCluster) throws Exception { final String user = oldCluster ? "preupgrade_user" : "postupgrade_user"; - Request request = new Request("GET", "/_security/user/" + user);; + Request request = new Request("GET", "/_security/user/" + user); + ; Map response = entityAsMap(client().performRequest(request)); Map userInfo = (Map) response.get(user); assertEquals(user + "@example.com", userInfo.get("email")); @@ -619,9 +649,7 @@ private void assertUserInfo(final boolean oldCluster) throws Exception { private void assertRoleInfo(final boolean oldCluster) throws Exception { final String role = oldCluster ? "preupgrade_role" : "postupgrade_role"; - Map response = (Map) entityAsMap( - client().performRequest(new Request("GET", "/_security/role/" + role)) - ).get(role); + Map response = (Map) entityAsMap(client().performRequest(new Request("GET", "/_security/role/" + role))).get(role); assertNotNull(response.get("run_as")); assertNotNull(response.get("cluster")); assertNotNull(response.get("indices")); @@ -659,8 +687,11 @@ private void assertRollUpJob(final String rollupJob) throws Exception { if (ObjectPath.eval("id", task).equals(rollupJob)) { hasRollupTask = true; final String jobStateField = "task.xpack/rollup/job.state.job_state"; - assertThat("Expected field [" + jobStateField + "] to be started or indexing in " + task.get("id"), - ObjectPath.eval(jobStateField, task), expectedStates); + assertThat( + "Expected field [" + jobStateField + "] to be started or indexing in " + task.get("id"), + ObjectPath.eval(jobStateField, task), + expectedStates + ); break; } } @@ -709,9 +740,11 @@ public void testDataStreams() throws Exception { createComposableTemplate(client(), "dst", "ds"); Request indexRequest = new Request("POST", "/ds/_doc/1?op_type=create&refresh"); - XContentBuilder - builder = - JsonXContent.contentBuilder().startObject().field("f", "v").field("@timestamp", System.currentTimeMillis()).endObject(); + XContentBuilder builder = JsonXContent.contentBuilder() + .startObject() + .field("f", "v") + .field("@timestamp", System.currentTimeMillis()) + .endObject(); indexRequest.setJsonEntity(Strings.toString(builder)); assertOK(client().performRequest(indexRequest)); } @@ -738,19 +771,18 @@ public void testDataStreams() throws Exception { List> indices = (List>) ds.get("indices"); assertEquals("ds", ds.get("name")); assertEquals(1, indices.size()); - assertEquals(DataStreamTestHelper.getLegacyDefaultBackingIndexName("ds", 1, timestamp, getOldClusterVersion()), - indices.get(0).get("index_name")); + assertEquals( + DataStreamTestHelper.getLegacyDefaultBackingIndexName("ds", 1, timestamp, getOldClusterVersion()), + indices.get(0).get("index_name") + ); assertNumHits("ds", 1, 1); } - private static void createComposableTemplate(RestClient client, String templateName, String indexPattern) - throws IOException { + private static void createComposableTemplate(RestClient client, String templateName, String indexPattern) throws IOException { StringEntity templateJSON = new StringEntity( - String.format(Locale.ROOT, "{\n" + - " \"index_patterns\": \"%s\",\n" + - " \"data_stream\": {}\n" + - "}", indexPattern), - ContentType.APPLICATION_JSON); + String.format(Locale.ROOT, "{\n" + " \"index_patterns\": \"%s\",\n" + " \"data_stream\": {}\n" + "}", indexPattern), + ContentType.APPLICATION_JSON + ); Request createIndexTemplateRequest = new Request("PUT", "_index_template/" + templateName); createIndexTemplateRequest.setEntity(templateJSON); client.performRequest(createIndexTemplateRequest); diff --git a/x-pack/qa/full-cluster-restart/src/test/java/org/elasticsearch/xpack/restart/MlConfigIndexMappingsFullClusterRestartIT.java b/x-pack/qa/full-cluster-restart/src/test/java/org/elasticsearch/xpack/restart/MlConfigIndexMappingsFullClusterRestartIT.java index e784abdd37b6b..1eeaeb7fd2a57 100644 --- a/x-pack/qa/full-cluster-restart/src/test/java/org/elasticsearch/xpack/restart/MlConfigIndexMappingsFullClusterRestartIT.java +++ b/x-pack/qa/full-cluster-restart/src/test/java/org/elasticsearch/xpack/restart/MlConfigIndexMappingsFullClusterRestartIT.java @@ -37,9 +37,7 @@ public class MlConfigIndexMappingsFullClusterRestartIT extends AbstractFullClust @Override protected Settings restClientSettings() { String token = "Basic " + Base64.getEncoder().encodeToString("test_user:x-pack-test-password".getBytes(StandardCharsets.UTF_8)); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } @Before @@ -47,8 +45,8 @@ public void waitForMlTemplates() throws Exception { List templatesToWaitFor = (isRunningAgainstOldCluster() && getOldClusterVersion().before(Version.V_7_12_0)) ? XPackRestTestConstants.ML_POST_V660_TEMPLATES : XPackRestTestConstants.ML_POST_V7120_TEMPLATES; - boolean clusterUnderstandsComposableTemplates = - isRunningAgainstOldCluster() == false || getOldClusterVersion().onOrAfter(Version.V_7_8_0); + boolean clusterUnderstandsComposableTemplates = isRunningAgainstOldCluster() == false + || getOldClusterVersion().onOrAfter(Version.V_7_8_0); XPackRestTestHelper.waitForTemplates(client(), templatesToWaitFor, clusterUnderstandsComposableTemplates); } @@ -73,18 +71,19 @@ public void testMlConfigIndexMappingsAfterMigration() throws Exception { } private void createAnomalyDetectorJob(String jobId) throws IOException { - String jobConfig = - "{\n" + - " \"job_id\": \"" + jobId + "\",\n" + - " \"analysis_config\": {\n" + - " \"bucket_span\": \"10m\",\n" + - " \"detectors\": [{\n" + - " \"function\": \"metric\",\n" + - " \"field_name\": \"responsetime\"\n" + - " }]\n" + - " },\n" + - " \"data_description\": {}\n" + - "}"; + String jobConfig = "{\n" + + " \"job_id\": \"" + + jobId + + "\",\n" + + " \"analysis_config\": {\n" + + " \"bucket_span\": \"10m\",\n" + + " \"detectors\": [{\n" + + " \"function\": \"metric\",\n" + + " \"field_name\": \"responsetime\"\n" + + " }]\n" + + " },\n" + + " \"data_description\": {}\n" + + "}"; Request putJobRequest = new Request("PUT", "/_ml/anomaly_detectors/" + jobId); putJobRequest.setJsonEntity(jobConfig); @@ -96,8 +95,8 @@ private void createAnomalyDetectorJob(String jobId) throws IOException { private Map getConfigIndexMappings() throws Exception { Request getIndexMappingsRequest = new Request("GET", ".ml-config/_mappings"); getIndexMappingsRequest.setOptions(expectVersionSpecificWarnings(v -> { - final String systemIndexWarning = "this request accesses system indices: [.ml-config], but in a future major version, direct " + - "access to system indices will be prevented by default"; + final String systemIndexWarning = "this request accesses system indices: [.ml-config], but in a future major version, direct " + + "access to system indices will be prevented by default"; v.current(systemIndexWarning); v.compatible(systemIndexWarning); })); diff --git a/x-pack/qa/full-cluster-restart/src/test/java/org/elasticsearch/xpack/restart/MlHiddenIndicesFullClusterRestartIT.java b/x-pack/qa/full-cluster-restart/src/test/java/org/elasticsearch/xpack/restart/MlHiddenIndicesFullClusterRestartIT.java index 28475fd76410d..6b06decd4da17 100644 --- a/x-pack/qa/full-cluster-restart/src/test/java/org/elasticsearch/xpack/restart/MlHiddenIndicesFullClusterRestartIT.java +++ b/x-pack/qa/full-cluster-restart/src/test/java/org/elasticsearch/xpack/restart/MlHiddenIndicesFullClusterRestartIT.java @@ -40,21 +40,18 @@ public class MlHiddenIndicesFullClusterRestartIT extends AbstractFullClusterRestartTestCase { private static final String JOB_ID = "ml-hidden-indices-old-cluster-job"; - private static final List, String>> EXPECTED_INDEX_ALIAS_PAIRS = - List.of( - Tuple.tuple(List.of(".ml-annotations-000001"), ".ml-annotations-read"), - Tuple.tuple(List.of(".ml-annotations-000001"), ".ml-annotations-write"), - Tuple.tuple(List.of(".ml-state", ".ml-state-000001"), ".ml-state-write"), - Tuple.tuple(List.of(".ml-anomalies-shared"), ".ml-anomalies-" + JOB_ID), - Tuple.tuple(List.of(".ml-anomalies-shared"), ".ml-anomalies-.write-" + JOB_ID) - ); + private static final List, String>> EXPECTED_INDEX_ALIAS_PAIRS = List.of( + Tuple.tuple(List.of(".ml-annotations-000001"), ".ml-annotations-read"), + Tuple.tuple(List.of(".ml-annotations-000001"), ".ml-annotations-write"), + Tuple.tuple(List.of(".ml-state", ".ml-state-000001"), ".ml-state-write"), + Tuple.tuple(List.of(".ml-anomalies-shared"), ".ml-anomalies-" + JOB_ID), + Tuple.tuple(List.of(".ml-anomalies-shared"), ".ml-anomalies-.write-" + JOB_ID) + ); @Override protected Settings restClientSettings() { String token = "Basic " + Base64.getEncoder().encodeToString("test_user:x-pack-test-password".getBytes(StandardCharsets.UTF_8)); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } @Before @@ -62,8 +59,8 @@ public void waitForMlTemplates() throws Exception { List templatesToWaitFor = (isRunningAgainstOldCluster() && getOldClusterVersion().before(Version.V_7_12_0)) ? XPackRestTestConstants.ML_POST_V660_TEMPLATES : XPackRestTestConstants.ML_POST_V7120_TEMPLATES; - boolean clusterUnderstandsComposableTemplates = - isRunningAgainstOldCluster() == false || getOldClusterVersion().onOrAfter(Version.V_7_8_0); + boolean clusterUnderstandsComposableTemplates = isRunningAgainstOldCluster() == false + || getOldClusterVersion().onOrAfter(Version.V_7_8_0); XPackRestTestHelper.waitForTemplates(client(), templatesToWaitFor, clusterUnderstandsComposableTemplates); } @@ -83,9 +80,11 @@ public void testMlIndicesBecomeHidden() throws Exception { @SuppressWarnings("unchecked") Map settings = (Map) e.getValue(); assertThat(settings, is(notNullValue())); - assertThat("Index " + indexName + " expected not to be hidden but was, settings = " + settings, + assertThat( + "Index " + indexName + " expected not to be hidden but was, settings = " + settings, XContentMapValues.extractValue(settings, "settings", "index", "hidden"), - is(nullValue())); + is(nullValue()) + ); } for (Tuple, String> indexAndAlias : EXPECTED_INDEX_ALIAS_PAIRS) { @@ -95,7 +94,8 @@ public void testMlIndicesBecomeHidden() throws Exception { assertThat( indexAndAlias + " expected not be hidden but was, aliasesMap = " + aliasesMap, XContentMapValues.extractValue(aliasesMap, index, "aliases", alias, "is_hidden"), - is(nullValue())); + is(nullValue()) + ); } } } @@ -109,9 +109,11 @@ public void testMlIndicesBecomeHidden() throws Exception { @SuppressWarnings("unchecked") Map settings = (Map) e.getValue(); assertThat(settings, is(notNullValue())); - assertThat("Index " + indexName + " expected to be hidden but wasn't, settings = " + settings, + assertThat( + "Index " + indexName + " expected to be hidden but wasn't, settings = " + settings, XContentMapValues.extractValue(settings, "settings", "index", "hidden"), - is(equalTo("true"))); + is(equalTo("true")) + ); } for (Tuple, String> indexAndAlias : EXPECTED_INDEX_ALIAS_PAIRS) { @@ -120,32 +122,29 @@ public void testMlIndicesBecomeHidden() throws Exception { assertThat( indexAndAlias + " expected to be hidden but wasn't, aliasesMap = " + aliasesMap, indices.stream() - .anyMatch(index -> - Boolean.TRUE.equals(XContentMapValues.extractValue(aliasesMap, index, "aliases", alias, "is_hidden"))), - is(true)); + .anyMatch( + index -> Boolean.TRUE.equals(XContentMapValues.extractValue(aliasesMap, index, "aliases", alias, "is_hidden")) + ), + is(true) + ); } } } private Response getMlIndicesSettings() throws IOException { - Request getSettingsRequest = - new Request("GET", ".ml-anomalies-*,.ml-state*,.ml-stats-*,.ml-notifications*,.ml-annotations*/_settings"); - getSettingsRequest - .setOptions(RequestOptions.DEFAULT.toBuilder() - .setWarningsHandler(WarningsHandler.PERMISSIVE) - .build()); + Request getSettingsRequest = new Request( + "GET", + ".ml-anomalies-*,.ml-state*,.ml-stats-*,.ml-notifications*,.ml-annotations*/_settings" + ); + getSettingsRequest.setOptions(RequestOptions.DEFAULT.toBuilder().setWarningsHandler(WarningsHandler.PERMISSIVE).build()); Response getSettingsResponse = client().performRequest(getSettingsRequest); assertThat(getSettingsResponse, is(notNullValue())); return getSettingsResponse; } private Response getMlAliases() throws IOException { - Request getAliasesRequest = - new Request("GET", ".ml-anomalies-*,.ml-state*,.ml-stats-*,.ml-notifications*,.ml-annotations*/_alias"); - getAliasesRequest - .setOptions(RequestOptions.DEFAULT.toBuilder() - .setWarningsHandler(WarningsHandler.PERMISSIVE) - .build()); + Request getAliasesRequest = new Request("GET", ".ml-anomalies-*,.ml-state*,.ml-stats-*,.ml-notifications*,.ml-annotations*/_alias"); + getAliasesRequest.setOptions(RequestOptions.DEFAULT.toBuilder().setWarningsHandler(WarningsHandler.PERMISSIVE).build()); Response getAliasesResponse = client().performRequest(getAliasesRequest); assertThat(getAliasesResponse, is(notNullValue())); return getAliasesResponse; @@ -154,22 +153,25 @@ private Response getMlAliases() throws IOException { @SuppressWarnings("unchecked") private static Map contentAsMap(Response response) throws IOException { return new ObjectMapper().readValue( - new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8), HashMap.class); + new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8), + HashMap.class + ); } private void createAnomalyDetectorJob(String jobId) throws IOException { - String jobConfig = - "{\n" + - " \"job_id\": \"" + jobId + "\",\n" + - " \"analysis_config\": {\n" + - " \"bucket_span\": \"10m\",\n" + - " \"detectors\": [{\n" + - " \"function\": \"metric\",\n" + - " \"field_name\": \"responsetime\"\n" + - " }]\n" + - " },\n" + - " \"data_description\": {}\n" + - "}"; + String jobConfig = "{\n" + + " \"job_id\": \"" + + jobId + + "\",\n" + + " \"analysis_config\": {\n" + + " \"bucket_span\": \"10m\",\n" + + " \"detectors\": [{\n" + + " \"function\": \"metric\",\n" + + " \"field_name\": \"responsetime\"\n" + + " }]\n" + + " },\n" + + " \"data_description\": {}\n" + + "}"; Request putJobRequest = new Request("PUT", "/_ml/anomaly_detectors/" + jobId); putJobRequest.setJsonEntity(jobConfig); diff --git a/x-pack/qa/full-cluster-restart/src/test/java/org/elasticsearch/xpack/restart/MlMigrationFullClusterRestartIT.java b/x-pack/qa/full-cluster-restart/src/test/java/org/elasticsearch/xpack/restart/MlMigrationFullClusterRestartIT.java index fd97e4809155d..efb463903dc81 100644 --- a/x-pack/qa/full-cluster-restart/src/test/java/org/elasticsearch/xpack/restart/MlMigrationFullClusterRestartIT.java +++ b/x-pack/qa/full-cluster-restart/src/test/java/org/elasticsearch/xpack/restart/MlMigrationFullClusterRestartIT.java @@ -32,8 +32,8 @@ import java.util.Map; import java.util.concurrent.TimeUnit; -import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.emptyOrNullString; +import static org.hamcrest.Matchers.is; public class MlMigrationFullClusterRestartIT extends AbstractFullClusterRestartTestCase { @@ -45,9 +45,7 @@ public class MlMigrationFullClusterRestartIT extends AbstractFullClusterRestartT @Override protected Settings restClientSettings() { String token = "Basic " + Base64.getEncoder().encodeToString("test_user:x-pack-test-password".getBytes(StandardCharsets.UTF_8)); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } @Before @@ -55,22 +53,24 @@ public void waitForMlTemplates() throws Exception { List templatesToWaitFor = (isRunningAgainstOldCluster() && getOldClusterVersion().before(Version.V_7_12_0)) ? XPackRestTestConstants.ML_POST_V660_TEMPLATES : XPackRestTestConstants.ML_POST_V7120_TEMPLATES; - boolean clusterUnderstandsComposableTemplates = - isRunningAgainstOldCluster() == false || getOldClusterVersion().onOrAfter(Version.V_7_8_0); + boolean clusterUnderstandsComposableTemplates = isRunningAgainstOldCluster() == false + || getOldClusterVersion().onOrAfter(Version.V_7_8_0); XPackRestTestHelper.waitForTemplates(client(), templatesToWaitFor, clusterUnderstandsComposableTemplates); } private void createTestIndex() throws IOException { Request createTestIndex = new Request("PUT", "/airline-data"); - createTestIndex.setJsonEntity("{\"mappings\": { \"doc\": {\"properties\": {" + - "\"time\": {\"type\": \"date\"}," + - "\"airline\": {\"type\": \"keyword\"}," + - "\"responsetime\": {\"type\": \"float\"}" + - "}}}}"); + createTestIndex.setJsonEntity( + "{\"mappings\": { \"doc\": {\"properties\": {" + + "\"time\": {\"type\": \"date\"}," + + "\"airline\": {\"type\": \"keyword\"}," + + "\"responsetime\": {\"type\": \"float\"}" + + "}}}}" + ); client().performRequest(createTestIndex); } - @AwaitsFix(bugUrl="https://github.com/elastic/elasticsearch/issues/36816") + @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/36816") public void testMigration() throws Exception { if (isRunningAgainstOldCluster()) { createTestIndex(); @@ -132,8 +132,7 @@ private void waitForJobToBeAssigned(String jobId) throws Exception { Response response = client().performRequest(getJobStats); Map stats = entityAsMap(response); - List> jobStats = - (List>) XContentMapValues.extractValue("jobs", stats); + List> jobStats = (List>) XContentMapValues.extractValue("jobs", stats); assertEquals(jobId, XContentMapValues.extractValue("job_id", jobStats.get(0))); assertEquals("opened", XContentMapValues.extractValue("state", jobStats.get(0))); @@ -148,8 +147,7 @@ private void waitForDatafeedToBeAssigned(String datafeedId) throws Exception { Request getDatafeedStats = new Request("GET", "_ml/datafeeds/" + datafeedId + "/_stats"); Response response = client().performRequest(getDatafeedStats); Map stats = entityAsMap(response); - List> datafeedStats = - (List>) XContentMapValues.extractValue("datafeeds", stats); + List> datafeedStats = (List>) XContentMapValues.extractValue("datafeeds", stats); assertEquals(datafeedId, XContentMapValues.extractValue("datafeed_id", datafeedStats.get(0))); assertEquals("started", XContentMapValues.extractValue("state", datafeedStats.get(0))); @@ -164,8 +162,10 @@ private void checkTaskParamsAreUpdated(String jobId, String datafeedId) throws E Response response = client().performRequest(getClusterState); Map responseMap = entityAsMap(response); - List> tasks = - (List>) XContentMapValues.extractValue("metadata.persistent_tasks.tasks", responseMap); + List> tasks = (List>) XContentMapValues.extractValue( + "metadata.persistent_tasks.tasks", + responseMap + ); assertNotNull(tasks); for (Map task : tasks) { String id = (String) task.get("id"); @@ -173,8 +173,7 @@ private void checkTaskParamsAreUpdated(String jobId, String datafeedId) throws E if (id.equals(MlTasks.jobTaskId(jobId))) { Object jobParam = XContentMapValues.extractValue("task.xpack/ml/job.params.job", task); assertNotNull(jobParam); - } - else if (id.equals(MlTasks.datafeedTaskId(datafeedId))) { + } else if (id.equals(MlTasks.datafeedTaskId(datafeedId))) { Object jobIdParam = XContentMapValues.extractValue("task.xpack/ml/datafeed.params.job_id", task); assertNotNull(jobIdParam); Object indices = XContentMapValues.extractValue("task.xpack/ml/datafeed.params.indices", task); @@ -186,24 +185,27 @@ else if (id.equals(MlTasks.datafeedTaskId(datafeedId))) { private void addAggregations(DatafeedConfig.Builder dfBuilder) { TermsAggregationBuilder airline = AggregationBuilders.terms("airline"); MaxAggregationBuilder maxTime = AggregationBuilders.max("time").field("time").subAggregation(airline); - dfBuilder.setParsedAggregations(AggregatorFactories.builder().addAggregator( - AggregationBuilders.histogram("time").interval(300000).subAggregation(maxTime).field("time"))); + dfBuilder.setParsedAggregations( + AggregatorFactories.builder() + .addAggregator(AggregationBuilders.histogram("time").interval(300000).subAggregation(maxTime).field("time")) + ); } private void putJob(String jobId) throws IOException { - String jobConfig = - "{\n" + - " \"job_id\": \"" + jobId + "\",\n" + - " \"analysis_config\": {\n" + - " \"bucket_span\": \"10m\",\n" + - " \"detectors\": [{\n" + - " \"function\": \"metric\",\n" + - " \"by_field_name\": \"airline\",\n" + - " \"field_name\": \"responsetime\"\n" + - " }]\n" + - " },\n" + - " \"data_description\": {}\n" + - "}"; + String jobConfig = "{\n" + + " \"job_id\": \"" + + jobId + + "\",\n" + + " \"analysis_config\": {\n" + + " \"bucket_span\": \"10m\",\n" + + " \"detectors\": [{\n" + + " \"function\": \"metric\",\n" + + " \"by_field_name\": \"airline\",\n" + + " \"field_name\": \"responsetime\"\n" + + " }]\n" + + " },\n" + + " \"data_description\": {}\n" + + "}"; Request putClosedJob = new Request("PUT", "/_xpack/ml/anomaly_detectors/" + jobId); putClosedJob.setJsonEntity(jobConfig); client().performRequest(putClosedJob); diff --git a/x-pack/qa/full-cluster-restart/src/test/java/org/elasticsearch/xpack/restart/QueryBuilderBWCIT.java b/x-pack/qa/full-cluster-restart/src/test/java/org/elasticsearch/xpack/restart/QueryBuilderBWCIT.java index 60f1dacf8197a..cffc6881df645 100644 --- a/x-pack/qa/full-cluster-restart/src/test/java/org/elasticsearch/xpack/restart/QueryBuilderBWCIT.java +++ b/x-pack/qa/full-cluster-restart/src/test/java/org/elasticsearch/xpack/restart/QueryBuilderBWCIT.java @@ -17,8 +17,6 @@ public class QueryBuilderBWCIT extends org.elasticsearch.upgrades.QueryBuilderBW @Override protected Settings restClientSettings() { String token = "Basic " + Base64.getEncoder().encodeToString("test_user:x-pack-test-password".getBytes(StandardCharsets.UTF_8)); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } } diff --git a/x-pack/qa/kerberos-tests/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosAuthenticationIT.java b/x-pack/qa/kerberos-tests/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosAuthenticationIT.java index 88ca019982348..45edccd26dfd8 100644 --- a/x-pack/qa/kerberos-tests/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosAuthenticationIT.java +++ b/x-pack/qa/kerberos-tests/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosAuthenticationIT.java @@ -14,14 +14,14 @@ import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClientBuilder; import org.elasticsearch.common.Strings; -import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.SuppressForbidden; +import org.elasticsearch.core.TimeValue; +import org.elasticsearch.test.rest.ESRestTestCase; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.rest.ESRestTestCase; import org.ietf.jgss.GSSException; import org.junit.Before; @@ -73,15 +73,22 @@ protected Settings restAdminSettings() { @Before public void setupRoleMapping() throws IOException { final String json = Strings // top-level - .toString(XContentBuilder.builder(XContentType.JSON.xContent()).startObject() - .array("roles", new String[] { "kerb_test" }) - .field("enabled", true) - .startObject("rules") - .startArray("all") - .startObject().startObject("field").field("realm.name", TEST_KERBEROS_REALM_NAME).endObject().endObject() - .endArray() // "all" - .endObject() // "rules" - .endObject()); + .toString( + XContentBuilder.builder(XContentType.JSON.xContent()) + .startObject() + .array("roles", new String[] { "kerb_test" }) + .field("enabled", true) + .startObject("rules") + .startArray("all") + .startObject() + .startObject("field") + .field("realm.name", TEST_KERBEROS_REALM_NAME) + .endObject() + .endObject() + .endArray() // "all" + .endObject() // "rules" + .endObject() + ); final Request request = new Request("POST", "/_security/role_mapping/kerberosrolemapping"); request.setJsonEntity(json); @@ -90,43 +97,55 @@ public void setupRoleMapping() throws IOException { } public void testLoginByKeytab() throws IOException, PrivilegedActionException { - assumeFalse("This test fails often on Java 17 early access. See: https://github.com/elastic/elasticsearch/issues/72120", - "17".equals(System.getProperty("java.version"))); + assumeFalse( + "This test fails often on Java 17 early access. See: https://github.com/elastic/elasticsearch/issues/72120", + "17".equals(System.getProperty("java.version")) + ); final String userPrincipalName = System.getProperty(TEST_USER_WITH_KEYTAB_KEY); final String keytabPath = System.getProperty(TEST_USER_WITH_KEYTAB_PATH_KEY); final boolean enabledDebugLogs = Boolean.parseBoolean(System.getProperty(ENABLE_KERBEROS_DEBUG_LOGS_KEY)); - final SpnegoHttpClientConfigCallbackHandler callbackHandler = new SpnegoHttpClientConfigCallbackHandler(userPrincipalName, - keytabPath, enabledDebugLogs); + final SpnegoHttpClientConfigCallbackHandler callbackHandler = new SpnegoHttpClientConfigCallbackHandler( + userPrincipalName, + keytabPath, + enabledDebugLogs + ); executeRequestAndVerifyResponse(userPrincipalName, callbackHandler); } public void testLoginByUsernamePassword() throws IOException, PrivilegedActionException { - assumeFalse("This test fails often on Java 17 early access. See: https://github.com/elastic/elasticsearch/issues/72120", - "17".equals(System.getProperty("java.version"))); + assumeFalse( + "This test fails often on Java 17 early access. See: https://github.com/elastic/elasticsearch/issues/72120", + "17".equals(System.getProperty("java.version")) + ); final String userPrincipalName = System.getProperty(TEST_USER_WITH_PWD_KEY); final String password = System.getProperty(TEST_USER_WITH_PWD_PASSWD_KEY); final boolean enabledDebugLogs = Boolean.parseBoolean(System.getProperty(ENABLE_KERBEROS_DEBUG_LOGS_KEY)); - final SpnegoHttpClientConfigCallbackHandler callbackHandler = new SpnegoHttpClientConfigCallbackHandler(userPrincipalName, - new SecureString(password.toCharArray()), enabledDebugLogs); + final SpnegoHttpClientConfigCallbackHandler callbackHandler = new SpnegoHttpClientConfigCallbackHandler( + userPrincipalName, + new SecureString(password.toCharArray()), + enabledDebugLogs + ); executeRequestAndVerifyResponse(userPrincipalName, callbackHandler); } public void testGetOauth2TokenInExchangeForKerberosTickets() throws PrivilegedActionException, GSSException, IOException { - assumeFalse("This test fails often on Java 17. See: https://github.com/elastic/elasticsearch/issues/72120", - "17".equals(System.getProperty("java.version"))); + assumeFalse( + "This test fails often on Java 17. See: https://github.com/elastic/elasticsearch/issues/72120", + "17".equals(System.getProperty("java.version")) + ); final String userPrincipalName = System.getProperty(TEST_USER_WITH_PWD_KEY); final String password = System.getProperty(TEST_USER_WITH_PWD_PASSWD_KEY); final boolean enabledDebugLogs = Boolean.parseBoolean(System.getProperty(ENABLE_KERBEROS_DEBUG_LOGS_KEY)); - final SpnegoHttpClientConfigCallbackHandler callbackHandler = new SpnegoHttpClientConfigCallbackHandler(userPrincipalName, - new SecureString(password.toCharArray()), enabledDebugLogs); + final SpnegoHttpClientConfigCallbackHandler callbackHandler = new SpnegoHttpClientConfigCallbackHandler( + userPrincipalName, + new SecureString(password.toCharArray()), + enabledDebugLogs + ); final String host = getClusterHosts().get(0).getHostName(); final String kerberosTicket = callbackHandler.getBase64EncodedTokenForSpnegoHeader(host); final Request request = new Request("POST", "/_security/oauth2/token"); - String json = "{" + - " \"grant_type\" : \"_kerberos\", " + - " \"kerberos_ticket\" : \"" + kerberosTicket + "\"" + - "}"; + String json = "{" + " \"grant_type\" : \"_kerberos\", " + " \"kerberos_ticket\" : \"" + kerberosTicket + "\"" + "}"; request.setJsonEntity(json); try (RestClient client = buildClientForUser("test_kibana_user")) { @@ -156,16 +175,19 @@ protected HttpHost buildHttpHost(String host, int port) { throw new IllegalStateException("DNS not resolved and assume did not trip"); } - private void executeRequestAndVerifyResponse(final String userPrincipalName, - final SpnegoHttpClientConfigCallbackHandler callbackHandler) throws PrivilegedActionException, IOException { + private void executeRequestAndVerifyResponse( + final String userPrincipalName, + final SpnegoHttpClientConfigCallbackHandler callbackHandler + ) throws PrivilegedActionException, IOException { final Request request = new Request("GET", "/_security/_authenticate"); try (RestClient restClient = buildRestClientForKerberos(callbackHandler)) { final AccessControlContext accessControlContext = AccessController.getContext(); final LoginContext lc = callbackHandler.login(); - Response response = SpnegoHttpClientConfigCallbackHandler.doAsPrivilegedWrapper(lc.getSubject(), - (PrivilegedExceptionAction) () -> { - return restClient.performRequest(request); - }, accessControlContext); + Response response = SpnegoHttpClientConfigCallbackHandler.doAsPrivilegedWrapper( + lc.getSubject(), + (PrivilegedExceptionAction) () -> { return restClient.performRequest(request); }, + accessControlContext + ); assertOK(response); final Map map = parseResponseAsMap(response.getEntity()); diff --git a/x-pack/qa/kerberos-tests/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/SpnegoHttpClientConfigCallbackHandler.java b/x-pack/qa/kerberos-tests/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/SpnegoHttpClientConfigCallbackHandler.java index 4770ce275ea37..c5f1c3e0afed2 100644 --- a/x-pack/qa/kerberos-tests/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/SpnegoHttpClientConfigCallbackHandler.java +++ b/x-pack/qa/kerberos-tests/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/SpnegoHttpClientConfigCallbackHandler.java @@ -88,8 +88,11 @@ private static Oid getSpnegoOid() { * @param password password for user * @param enableDebugLogs if {@code true} enables kerberos debug logs */ - public SpnegoHttpClientConfigCallbackHandler(final String userPrincipalName, final SecureString password, - final boolean enableDebugLogs) { + public SpnegoHttpClientConfigCallbackHandler( + final String userPrincipalName, + final SecureString password, + final boolean enableDebugLogs + ) { this.userPrincipalName = userPrincipalName; this.password = password; this.keytabPath = null; @@ -119,22 +122,30 @@ public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpCli private void setupSpnegoAuthSchemeSupport(HttpAsyncClientBuilder httpClientBuilder) { final Lookup authSchemeRegistry = RegistryBuilder.create() - .register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory()).build(); + .register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory()) + .build(); final GSSManager gssManager = GSSManager.getInstance(); try { final GSSName gssUserPrincipalName = gssManager.createName(userPrincipalName, GSSName.NT_USER_NAME); login(); final AccessControlContext acc = AccessController.getContext(); - final GSSCredential credential = doAsPrivilegedWrapper(loginContext.getSubject(), - (PrivilegedExceptionAction) () -> gssManager.createCredential(gssUserPrincipalName, - GSSCredential.DEFAULT_LIFETIME, SPNEGO_OID, GSSCredential.INITIATE_ONLY), - acc); + final GSSCredential credential = doAsPrivilegedWrapper( + loginContext.getSubject(), + (PrivilegedExceptionAction) () -> gssManager.createCredential( + gssUserPrincipalName, + GSSCredential.DEFAULT_LIFETIME, + SPNEGO_OID, + GSSCredential.INITIATE_ONLY + ), + acc + ); final KerberosCredentialsProvider credentialsProvider = new KerberosCredentialsProvider(); credentialsProvider.setCredentials( - new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, AuthSchemes.SPNEGO), - new KerberosCredentials(credential)); + new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, AuthSchemes.SPNEGO), + new KerberosCredentials(credential) + ); httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); } catch (GSSException e) { throw new RuntimeException(e); @@ -154,8 +165,12 @@ private void setupSpnegoAuthSchemeSupport(HttpAsyncClientBuilder httpClientBuild public synchronized LoginContext login() throws PrivilegedActionException { if (this.loginContext == null) { AccessController.doPrivileged((PrivilegedExceptionAction) () -> { - final Subject subject = new Subject(false, Collections.singleton(new KerberosPrincipal(userPrincipalName)), - Collections.emptySet(), Collections.emptySet()); + final Subject subject = new Subject( + false, + Collections.singleton(new KerberosPrincipal(userPrincipalName)), + Collections.emptySet(), + Collections.emptySet() + ); Configuration conf = null; final CallbackHandler callback; if (password != null) { @@ -187,7 +202,7 @@ public synchronized LoginContext login() throws PrivilegedActionException { * @throws PrivilegedActionException if the specified action's run method threw a checked exception */ static T doAsPrivilegedWrapper(final Subject subject, final PrivilegedExceptionAction action, final AccessControlContext acc) - throws PrivilegedActionException { + throws PrivilegedActionException { try { return AccessController.doPrivileged((PrivilegedExceptionAction) () -> Subject.doAsPrivileged(subject, action, acc)); } catch (PrivilegedActionException pae) { @@ -315,8 +330,12 @@ public AppConfigurationEntry[] getAppConfigurationEntry(final String name) { options.put("storeKey", Boolean.TRUE.toString()); options.put("debug", Boolean.toString(enableDebugLogs)); addOptions(options); - return new AppConfigurationEntry[] { new AppConfigurationEntry(SUN_KRB5_LOGIN_MODULE, - AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, Collections.unmodifiableMap(options)) }; + return new AppConfigurationEntry[] { + new AppConfigurationEntry( + SUN_KRB5_LOGIN_MODULE, + AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, + Collections.unmodifiableMap(options) + ) }; } abstract void addOptions(Map options); @@ -332,18 +351,29 @@ public AppConfigurationEntry[] getAppConfigurationEntry(final String name) { */ String getBase64EncodedTokenForSpnegoHeader(final String serviceHost) throws PrivilegedActionException, GSSException { final GSSManager gssManager = GSSManager.getInstance(); - final GSSName gssServicePrincipalName = AccessController - .doPrivileged((PrivilegedExceptionAction) () -> gssManager.createName("HTTP/" + serviceHost, null)); + final GSSName gssServicePrincipalName = AccessController.doPrivileged( + (PrivilegedExceptionAction) () -> gssManager.createName("HTTP/" + serviceHost, null) + ); final GSSName gssUserPrincipalName = gssManager.createName(userPrincipalName, GSSName.NT_USER_NAME); - loginContext = AccessController - .doPrivileged((PrivilegedExceptionAction) () -> loginUsingPassword(userPrincipalName, password)); - final GSSCredential userCreds = doAsWrapper(loginContext.getSubject(), (PrivilegedExceptionAction) () -> gssManager - .createCredential(gssUserPrincipalName, GSSCredential.DEFAULT_LIFETIME, SPNEGO_OID, GSSCredential.INITIATE_ONLY)); + loginContext = AccessController.doPrivileged( + (PrivilegedExceptionAction) () -> loginUsingPassword(userPrincipalName, password) + ); + final GSSCredential userCreds = doAsWrapper( + loginContext.getSubject(), + (PrivilegedExceptionAction) () -> gssManager.createCredential( + gssUserPrincipalName, + GSSCredential.DEFAULT_LIFETIME, + SPNEGO_OID, + GSSCredential.INITIATE_ONLY + ) + ); gssContext = gssManager.createContext(gssServicePrincipalName, SPNEGO_OID, userCreds, GSSCredential.DEFAULT_LIFETIME); gssContext.requestMutualAuth(true); - final byte[] outToken = doAsWrapper(loginContext.getSubject(), - (PrivilegedExceptionAction) () -> gssContext.initSecContext(new byte[0], 0, 0)); + final byte[] outToken = doAsWrapper( + loginContext.getSubject(), + (PrivilegedExceptionAction) () -> gssContext.initSecContext(new byte[0], 0, 0) + ); return Base64.getEncoder().encodeToString(outToken); } @@ -373,8 +403,10 @@ String handleResponse(final String base64Token) throws PrivilegedActionException throw new IllegalStateException("GSS Context has already been established"); } final byte[] token = Base64.getDecoder().decode(base64Token); - final byte[] outToken = doAsWrapper(loginContext.getSubject(), - (PrivilegedExceptionAction) () -> gssContext.initSecContext(token, 0, token.length)); + final byte[] outToken = doAsWrapper( + loginContext.getSubject(), + (PrivilegedExceptionAction) () -> gssContext.initSecContext(token, 0, token.length) + ); if (outToken == null || outToken.length == 0) { return null; } diff --git a/x-pack/qa/mixed-tier-cluster/src/test/java/org/elasticsearch/mixed/DataTierMixedIT.java b/x-pack/qa/mixed-tier-cluster/src/test/java/org/elasticsearch/mixed/DataTierMixedIT.java index 5476b89224fc5..129838b767c08 100644 --- a/x-pack/qa/mixed-tier-cluster/src/test/java/org/elasticsearch/mixed/DataTierMixedIT.java +++ b/x-pack/qa/mixed-tier-cluster/src/test/java/org/elasticsearch/mixed/DataTierMixedIT.java @@ -14,10 +14,10 @@ public class DataTierMixedIT extends ESRestTestCase { public void testMixedTierCompatibility() throws Exception { - createIndex("test-index", Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .build()); + createIndex( + "test-index", + Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).build() + ); ensureGreen("test-index"); } } diff --git a/x-pack/qa/multi-cluster-search-security/src/test/java/org/elasticsearch/xpack/security/MultiClusterSearchWithSecurityYamlTestSuiteIT.java b/x-pack/qa/multi-cluster-search-security/src/test/java/org/elasticsearch/xpack/security/MultiClusterSearchWithSecurityYamlTestSuiteIT.java index e4f08aecb60bc..089bdf461f81b 100644 --- a/x-pack/qa/multi-cluster-search-security/src/test/java/org/elasticsearch/xpack/security/MultiClusterSearchWithSecurityYamlTestSuiteIT.java +++ b/x-pack/qa/multi-cluster-search-security/src/test/java/org/elasticsearch/xpack/security/MultiClusterSearchWithSecurityYamlTestSuiteIT.java @@ -30,8 +30,7 @@ protected boolean preserveDataStreamsUponCompletion() { return true; } - public MultiClusterSearchWithSecurityYamlTestSuiteIT( - @Name("yaml") ClientYamlTestCandidate testCandidate) { + public MultiClusterSearchWithSecurityYamlTestSuiteIT(@Name("yaml") ClientYamlTestCandidate testCandidate) { super(testCandidate); } @@ -43,9 +42,6 @@ public static Iterable parameters() throws Exception { @Override protected Settings restClientSettings() { String token = basicAuthHeaderValue(USER, new SecureString(PASS.toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } } - diff --git a/x-pack/qa/multi-node/src/test/java/org/elasticsearch/multi_node/GlobalCheckpointSyncActionIT.java b/x-pack/qa/multi-node/src/test/java/org/elasticsearch/multi_node/GlobalCheckpointSyncActionIT.java index d43d01947920f..c17446c05d84d 100644 --- a/x-pack/qa/multi-node/src/test/java/org/elasticsearch/multi_node/GlobalCheckpointSyncActionIT.java +++ b/x-pack/qa/multi-node/src/test/java/org/elasticsearch/multi_node/GlobalCheckpointSyncActionIT.java @@ -12,9 +12,9 @@ import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.test.rest.ESRestTestCase; import org.elasticsearch.test.rest.yaml.ObjectPath; +import org.elasticsearch.xcontent.XContentBuilder; import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.Matchers.equalTo; diff --git a/x-pack/qa/multi-node/src/test/java/org/elasticsearch/multi_node/RollupIT.java b/x-pack/qa/multi-node/src/test/java/org/elasticsearch/multi_node/RollupIT.java index 65b7e5e2b63bc..d6883b7d59297 100644 --- a/x-pack/qa/multi-node/src/test/java/org/elasticsearch/multi_node/RollupIT.java +++ b/x-pack/qa/multi-node/src/test/java/org/elasticsearch/multi_node/RollupIT.java @@ -15,13 +15,13 @@ import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.ObjectPath; -import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.common.xcontent.support.XContentMapValues; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.test.rest.ESRestTestCase; +import org.elasticsearch.xcontent.ObjectPath; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xcontent.json.JsonXContent; import java.io.IOException; import java.time.Instant; @@ -79,7 +79,8 @@ public void testBigRollup() throws Exception { .startObject("value") .field("type", "integer") .endObject() - .endObject().endObject(); + .endObject() + .endObject(); } builder.endObject(); final StringEntity entity = new StringEntity(Strings.toString(builder), ContentType.APPLICATION_JSON); @@ -88,12 +89,11 @@ public void testBigRollup() throws Exception { client().performRequest(req); } - // index documents for the rollup job final StringBuilder bulk = new StringBuilder(); for (int i = 0; i < numDocs; i++) { bulk.append("{\"index\":{\"_index\":\"rollup-docs\"}}\n"); - ZonedDateTime zdt = ZonedDateTime.ofInstant(Instant.ofEpochSecond(1531221196 + (60*i)), ZoneId.of("UTC")); + ZonedDateTime zdt = ZonedDateTime.ofInstant(Instant.ofEpochSecond(1531221196 + (60 * i)), ZoneId.of("UTC")); String date = zdt.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME); bulk.append("{\"timestamp\":\"").append(date).append("\",\"value\":").append(i).append("}\n"); } @@ -107,21 +107,25 @@ public void testBigRollup() throws Exception { // create the rollup job final Request createRollupJobRequest = new Request("PUT", "/_rollup/job/rollup-job-test"); int pageSize = randomIntBetween(2, 50); - createRollupJobRequest.setJsonEntity("{" - + "\"index_pattern\":\"rollup-*\"," - + "\"rollup_index\":\"results-rollup\"," - + "\"cron\":\"*/1 * * * * ?\"," // fast cron so test runs quickly - + "\"page_size\":" + pageSize + "," - + "\"groups\":{" - + " \"date_histogram\":{" - + " \"field\":\"timestamp\"," - + " \"fixed_interval\":\"5m\"" - + " }" - + "}," - + "\"metrics\":[" - + " {\"field\":\"value\",\"metrics\":[\"min\",\"max\",\"sum\"]}" - + "]" - + "}"); + createRollupJobRequest.setJsonEntity( + "{" + + "\"index_pattern\":\"rollup-*\"," + + "\"rollup_index\":\"results-rollup\"," + + "\"cron\":\"*/1 * * * * ?\"," // fast cron so test runs quickly + + "\"page_size\":" + + pageSize + + "," + + "\"groups\":{" + + " \"date_histogram\":{" + + " \"field\":\"timestamp\"," + + " \"fixed_interval\":\"5m\"" + + " }" + + "}," + + "\"metrics\":[" + + " {\"field\":\"value\",\"metrics\":[\"min\",\"max\",\"sum\"]}" + + "]" + + "}" + ); Map createRollupJobResponse = toMap(client().performRequest(createRollupJobRequest)); assertThat(createRollupJobResponse.get("acknowledged"), equalTo(Boolean.TRUE)); @@ -150,28 +154,28 @@ public void testBigRollup() throws Exception { final Request refreshRollupIndex = new Request("POST", "results-rollup/_refresh"); toMap(client().performRequest(refreshRollupIndex)); - String jsonRequestBody = "{\n" + - " \"size\": 0,\n" + - " \"query\": {\n" + - " \"match_all\": {}\n" + - " },\n" + - " \"aggs\": {\n" + - " \"date_histo\": {\n" + - " \"date_histogram\": {\n" + - " \"field\": \"timestamp\",\n" + - " \"fixed_interval\": \"60m\",\n" + - " \"format\": \"date_time\"\n" + - " },\n" + - " \"aggs\": {\n" + - " \"the_max\": {\n" + - " \"max\": {\n" + - " \"field\": \"value\"\n" + - " }\n" + - " }\n" + - " }\n" + - " }\n" + - " }\n" + - "}"; + String jsonRequestBody = "{\n" + + " \"size\": 0,\n" + + " \"query\": {\n" + + " \"match_all\": {}\n" + + " },\n" + + " \"aggs\": {\n" + + " \"date_histo\": {\n" + + " \"date_histogram\": {\n" + + " \"field\": \"timestamp\",\n" + + " \"fixed_interval\": \"60m\",\n" + + " \"format\": \"date_time\"\n" + + " },\n" + + " \"aggs\": {\n" + + " \"the_max\": {\n" + + " \"max\": {\n" + + " \"field\": \"value\"\n" + + " }\n" + + " }\n" + + " }\n" + + " }\n" + + " }\n" + + "}"; Request request = new Request("GET", "rollup-docs/_search"); request.setJsonEntity(jsonRequestBody); @@ -184,8 +188,10 @@ public void testBigRollup() throws Exception { Map rollupBody = toMap(rollupResponse); // Do the live agg results match the rollup agg results? - assertThat(ObjectPath.eval("aggregations.date_histo.buckets", liveBody), - equalTo(ObjectPath.eval("aggregations.date_histo.buckets", rollupBody))); + assertThat( + ObjectPath.eval("aggregations.date_histo.buckets", liveBody), + equalTo(ObjectPath.eval("aggregations.date_histo.buckets", rollupBody)) + ); request = new Request("GET", "rollup-docs/_rollup_search"); request.setJsonEntity(jsonRequestBody); @@ -193,14 +199,16 @@ public void testBigRollup() throws Exception { Map liveRollupBody = toMap(liveRollupResponse); // Does searching the live index via rollup_search work match the live search? - assertThat(ObjectPath.eval("aggregations.date_histo.buckets", liveBody), - equalTo(ObjectPath.eval("aggregations.date_histo.buckets", liveRollupBody))); + assertThat( + ObjectPath.eval("aggregations.date_histo.buckets", liveBody), + equalTo(ObjectPath.eval("aggregations.date_histo.buckets", liveRollupBody)) + ); } @SuppressWarnings("unchecked") private void assertRollUpJob(final String rollupJob) throws Exception { - String[] states = new String[]{"indexing", "started"}; + String[] states = new String[] { "indexing", "started" }; waitForRollUpJob(rollupJob, states); // check that the rollup job is started using the RollUp API @@ -233,8 +241,11 @@ private void assertRollUpJob(final String rollupJob) throws Exception { hasRollupTask = true; final String jobStateField = "task.xpack/rollup/job.state.job_state"; - assertThat("Expected field [" + jobStateField + "] to be started or indexing in " + task.get("id"), - ObjectPath.eval(jobStateField, task), is(oneOf(states))); + assertThat( + "Expected field [" + jobStateField + "] to be started or indexing in " + task.get("id"), + ObjectPath.eval(jobStateField, task), + is(oneOf(states)) + ); break; } } @@ -264,8 +275,7 @@ private Map getJob(Response response, String targetJobId) throws @SuppressWarnings("unchecked") private Map getJob(Map jobsMap, String targetJobId) throws IOException { - List> jobs = - (List>) XContentMapValues.extractValue("jobs", jobsMap); + List> jobs = (List>) XContentMapValues.extractValue("jobs", jobsMap); if (jobs == null) { return null; diff --git a/x-pack/qa/oidc-op-tests/src/test/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectAuthIT.java b/x-pack/qa/oidc-op-tests/src/test/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectAuthIT.java index b998ce8c1400f..bef3687d8056b 100644 --- a/x-pack/qa/oidc-op-tests/src/test/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectAuthIT.java +++ b/x-pack/qa/oidc-op-tests/src/test/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectAuthIT.java @@ -8,6 +8,7 @@ import net.minidev.json.JSONObject; import net.minidev.json.parser.JSONParser; + import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpHost; @@ -32,19 +33,19 @@ import org.elasticsearch.client.Response; import org.elasticsearch.client.ResponseException; import org.elasticsearch.client.RestClient; -import org.elasticsearch.core.CheckedFunction; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.core.Tuple; -import org.elasticsearch.core.PathUtils; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.CheckedFunction; +import org.elasticsearch.core.Nullable; +import org.elasticsearch.core.PathUtils; +import org.elasticsearch.core.TimeValue; +import org.elasticsearch.core.Tuple; +import org.elasticsearch.test.rest.ESRestTestCase; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.rest.ESRestTestCase; import org.elasticsearch.xpack.core.common.socket.SocketAccess; import org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken; import org.hamcrest.Matchers; @@ -74,12 +75,14 @@ public class OpenIdConnectAuthIT extends ESRestTestCase { private static final String REALM_NAME_CLIENT_POST_AUTH = "c2id-post"; private static final String REALM_NAME_CLIENT_JWT_AUTH = "c2id-jwt"; private static final String FACILITATOR_PASSWORD = "f@cilit@t0rPassword"; // longer than 14 chars - private static final String REGISTRATION_URL = "http://127.0.0.1:" + getEphemeralTcpPortFromProperty("oidc-provider", "8080") + private static final String REGISTRATION_URL = "http://127.0.0.1:" + + getEphemeralTcpPortFromProperty("oidc-provider", "8080") + "/c2id/clients"; - private static final String LOGIN_API = "http://127.0.0.1:" + getEphemeralTcpPortFromProperty("oidc-provider", "8080") + private static final String LOGIN_API = "http://127.0.0.1:" + + getEphemeralTcpPortFromProperty("oidc-provider", "8080") + "/c2id-login/api/"; private static final String CLIENT_SECRET = "b07efb7a1cf6ec9462afe7b6d3ab55c6c7880262aa61ac28dded292aca47c9a2"; - // SHA256 of this is defined in x-pack/test/idp-fixture/oidc/override.properties + // SHA256 of this is defined in x-pack/test/idp-fixture/oidc/override.properties private static final String OP_API_BEARER_TOKEN = "811fa888f3e0fdc9e01d4201bfeee46a"; private static final String ES_PORT = getEphemeralTcpPortFromProperty("elasticsearch-node", "9200"); private static Path HTTP_TRUSTED_CERT; @@ -105,37 +108,45 @@ public static void readTrustedCert() throws Exception { @BeforeClass public static void registerClients() throws Exception { try (CloseableHttpClient httpClient = HttpClients.createDefault()) { - String codeClient = "{" + - "\"grant_types\": [\"authorization_code\"]," + - "\"response_types\": [\"code\"]," + - "\"preferred_client_id\":\"https://my.elasticsearch.org/rp\"," + - "\"preferred_client_secret\":\"" + CLIENT_SECRET + "\"," + - "\"redirect_uris\": [\"https://my.fantastic.rp/cb\"]," + - "\"token_endpoint_auth_method\":\"client_secret_basic\"" + - "}"; - String implicitClient = "{" + - "\"grant_types\": [\"implicit\"]," + - "\"response_types\": [\"token id_token\"]," + - "\"preferred_client_id\":\"elasticsearch-rp\"," + - "\"preferred_client_secret\":\"" + CLIENT_SECRET + "\"," + - "\"redirect_uris\": [\"https://my.fantastic.rp/cb\"]" + - "}"; - String postClient = "{" + - "\"grant_types\": [\"authorization_code\"]," + - "\"response_types\": [\"code\"]," + - "\"preferred_client_id\":\"elasticsearch-post\"," + - "\"preferred_client_secret\":\"" + CLIENT_SECRET + "\"," + - "\"redirect_uris\": [\"https://my.fantastic.rp/cb\"]," + - "\"token_endpoint_auth_method\":\"client_secret_post\"" + - "}"; - String jwtClient = "{" + - "\"grant_types\": [\"authorization_code\"]," + - "\"response_types\": [\"code\"]," + - "\"preferred_client_id\":\"elasticsearch-post-jwt\"," + - "\"preferred_client_secret\":\"" + CLIENT_SECRET + "\"," + - "\"redirect_uris\": [\"https://my.fantastic.rp/cb\"]," + - "\"token_endpoint_auth_method\":\"client_secret_jwt\"" + - "}"; + String codeClient = "{" + + "\"grant_types\": [\"authorization_code\"]," + + "\"response_types\": [\"code\"]," + + "\"preferred_client_id\":\"https://my.elasticsearch.org/rp\"," + + "\"preferred_client_secret\":\"" + + CLIENT_SECRET + + "\"," + + "\"redirect_uris\": [\"https://my.fantastic.rp/cb\"]," + + "\"token_endpoint_auth_method\":\"client_secret_basic\"" + + "}"; + String implicitClient = "{" + + "\"grant_types\": [\"implicit\"]," + + "\"response_types\": [\"token id_token\"]," + + "\"preferred_client_id\":\"elasticsearch-rp\"," + + "\"preferred_client_secret\":\"" + + CLIENT_SECRET + + "\"," + + "\"redirect_uris\": [\"https://my.fantastic.rp/cb\"]" + + "}"; + String postClient = "{" + + "\"grant_types\": [\"authorization_code\"]," + + "\"response_types\": [\"code\"]," + + "\"preferred_client_id\":\"elasticsearch-post\"," + + "\"preferred_client_secret\":\"" + + CLIENT_SECRET + + "\"," + + "\"redirect_uris\": [\"https://my.fantastic.rp/cb\"]," + + "\"token_endpoint_auth_method\":\"client_secret_post\"" + + "}"; + String jwtClient = "{" + + "\"grant_types\": [\"authorization_code\"]," + + "\"response_types\": [\"code\"]," + + "\"preferred_client_id\":\"elasticsearch-post-jwt\"," + + "\"preferred_client_secret\":\"" + + CLIENT_SECRET + + "\"," + + "\"redirect_uris\": [\"https://my.fantastic.rp/cb\"]," + + "\"token_endpoint_auth_method\":\"client_secret_jwt\"" + + "}"; HttpPost httpPost = new HttpPost(REGISTRATION_URL); final BasicHttpContext context = new BasicHttpContext(); httpPost.setEntity(new StringEntity(codeClient, ContentType.APPLICATION_JSON)); @@ -182,9 +193,9 @@ public static void registerClients() throws Exception { protected Settings restAdminSettings() { String token = basicAuthHeaderValue("x_pack_rest_user", new SecureString("x-pack-test-password".toCharArray())); return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .put(CERTIFICATE_AUTHORITIES, HTTP_TRUSTED_CERT) - .build(); + .put(ThreadContext.PREFIX + ".Authorization", token) + .put(CERTIFICATE_AUTHORITIES, HTTP_TRUSTED_CERT) + .build(); } private String authenticateAtOP(URI opAuthUri) throws Exception { @@ -194,9 +205,7 @@ private String authenticateAtOP(URI opAuthUri) throws Exception { final BasicHttpContext context = new BasicHttpContext(); // Initiate the authentication process HttpPost httpPost = new HttpPost(LOGIN_API + "initAuthRequest"); - String initJson = "{" + - " \"qs\":\"" + opAuthUri.getRawQuery() + "\"" + - "}"; + String initJson = "{" + " \"qs\":\"" + opAuthUri.getRawQuery() + "\"" + "}"; configureJsonRequest(httpPost, initJson); JSONObject initResponse = execute(httpClient, httpPost, context, response -> { assertHttpOk(response.getStatusLine()); @@ -206,27 +215,31 @@ private String authenticateAtOP(URI opAuthUri) throws Exception { final String sid = initResponse.getAsString("sid"); // Actually authenticate the user with ldapAuth HttpPost loginHttpPost = new HttpPost(LOGIN_API + "authenticateSubject?cacheBuster=" + randomAlphaOfLength(8)); - String loginJson = "{" + - "\"username\":\"alice\"," + - "\"password\":\"secret\"" + - "}"; + String loginJson = "{" + "\"username\":\"alice\"," + "\"password\":\"secret\"" + "}"; configureJsonRequest(loginHttpPost, loginJson); JSONObject loginJsonResponse = execute(httpClient, loginHttpPost, context, response -> { assertHttpOk(response.getStatusLine()); return parseJsonResponse(response); }); // Get the consent screen - HttpPut consentFetchHttpPut = - new HttpPut(LOGIN_API + "updateAuthRequest" + "/" + sid + "?cacheBuster=" + randomAlphaOfLength(8)); - String consentFetchJson = "{" + - "\"sub\": \"" + loginJsonResponse.getAsString("id") + "\"," + - "\"acr\": \"http://loa.c2id.com/basic\"," + - "\"amr\": [\"pwd\"]," + - "\"data\": {" + - "\"email\": \"" + loginJsonResponse.getAsString("email") + "\"," + - "\"name\": \"" + loginJsonResponse.getAsString("name") + "\"" + - "}" + - "}"; + HttpPut consentFetchHttpPut = new HttpPut( + LOGIN_API + "updateAuthRequest" + "/" + sid + "?cacheBuster=" + randomAlphaOfLength(8) + ); + String consentFetchJson = "{" + + "\"sub\": \"" + + loginJsonResponse.getAsString("id") + + "\"," + + "\"acr\": \"http://loa.c2id.com/basic\"," + + "\"amr\": [\"pwd\"]," + + "\"data\": {" + + "\"email\": \"" + + loginJsonResponse.getAsString("email") + + "\"," + + "\"name\": \"" + + loginJsonResponse.getAsString("name") + + "\"" + + "}" + + "}"; configureJsonRequest(consentFetchHttpPut, consentFetchJson); JSONObject consentFetchResponse = execute(httpClient, consentFetchHttpPut, context, response -> { assertHttpOk(response.getStatusLine()); @@ -234,12 +247,10 @@ private String authenticateAtOP(URI opAuthUri) throws Exception { }); if (consentFetchResponse.getAsString("type").equals("consent")) { // If needed, submit the consent - HttpPut consentHttpPut = - new HttpPut(LOGIN_API + "updateAuthRequest" + "/" + sid + "?cacheBuster=" + randomAlphaOfLength(8)); - String consentJson = "{" + - "\"claims\":[\"name\", \"email\"]," + - "\"scope\":[\"openid\"]" + - "}"; + HttpPut consentHttpPut = new HttpPut( + LOGIN_API + "updateAuthRequest" + "/" + sid + "?cacheBuster=" + randomAlphaOfLength(8) + ); + String consentJson = "{" + "\"claims\":[\"name\", \"email\"]," + "\"scope\":[\"openid\"]" + "}"; configureJsonRequest(consentHttpPut, consentJson); JSONObject jsonConsentResponse = execute(httpClient, consentHttpPut, context, response -> { assertHttpOk(response.getStatusLine()); @@ -275,9 +286,12 @@ private Map callAuthenticateApiUsingAccessToken(String accessTok } } - private T execute(CloseableHttpClient client, HttpEntityEnclosingRequestBase request, - HttpContext context, CheckedFunction body) - throws Exception { + private T execute( + CloseableHttpClient client, + HttpEntityEnclosingRequestBase request, + HttpContext context, + CheckedFunction body + ) throws Exception { final int timeout = (int) TimeValue.timeValueSeconds(90).millis(); RequestConfig requestConfig = RequestConfig.custom() .setConnectionRequestTimeout(timeout) @@ -285,8 +299,9 @@ private T execute(CloseableHttpClient client, HttpEntityEnclosingRequestBase .setSocketTimeout(timeout) .build(); request.setConfig(requestConfig); - logger.info("Execute HTTP " + request.getMethod() + " " + request.getURI() + - " with payload " + EntityUtils.toString(request.getEntity())); + logger.info( + "Execute HTTP " + request.getMethod() + " " + request.getURI() + " with payload " + EntityUtils.toString(request.getEntity()) + ); try (CloseableHttpResponse response = SocketAccess.doPrivileged(() -> client.execute(request, context))) { return body.apply(response); } catch (Exception e) { @@ -312,32 +327,48 @@ private void configureJsonRequest(HttpEntityEnclosingRequestBase request, String public void testAuthenticateWithCodeFlow() throws Exception { final PrepareAuthResponse prepareAuthResponse = getRedirectedFromFacilitator(REALM_NAME); final String redirectUri = authenticateAtOP(prepareAuthResponse.getAuthUri()); - Tuple tokens = completeAuthentication(redirectUri, prepareAuthResponse.getState(), - prepareAuthResponse.getNonce(), REALM_NAME); + Tuple tokens = completeAuthentication( + redirectUri, + prepareAuthResponse.getState(), + prepareAuthResponse.getNonce(), + REALM_NAME + ); verifyElasticsearchAccessTokenForCodeFlow(tokens.v1()); } public void testAuthenticateWithCodeFlowAndClientPost() throws Exception { final PrepareAuthResponse prepareAuthResponse = getRedirectedFromFacilitator(REALM_NAME_CLIENT_POST_AUTH); final String redirectUri = authenticateAtOP(prepareAuthResponse.getAuthUri()); - Tuple tokens = completeAuthentication(redirectUri, prepareAuthResponse.getState(), - prepareAuthResponse.getNonce(), REALM_NAME_CLIENT_POST_AUTH); + Tuple tokens = completeAuthentication( + redirectUri, + prepareAuthResponse.getState(), + prepareAuthResponse.getNonce(), + REALM_NAME_CLIENT_POST_AUTH + ); verifyElasticsearchAccessTokenForCodeFlow(tokens.v1()); } public void testAuthenticateWithCodeFlowAndClientJwtPost() throws Exception { final PrepareAuthResponse prepareAuthResponse = getRedirectedFromFacilitator(REALM_NAME_CLIENT_JWT_AUTH); final String redirectUri = authenticateAtOP(prepareAuthResponse.getAuthUri()); - Tuple tokens = completeAuthentication(redirectUri, prepareAuthResponse.getState(), - prepareAuthResponse.getNonce(), REALM_NAME_CLIENT_JWT_AUTH); + Tuple tokens = completeAuthentication( + redirectUri, + prepareAuthResponse.getState(), + prepareAuthResponse.getNonce(), + REALM_NAME_CLIENT_JWT_AUTH + ); verifyElasticsearchAccessTokenForCodeFlow(tokens.v1()); } public void testAuthenticateWithImplicitFlow() throws Exception { final PrepareAuthResponse prepareAuthResponse = getRedirectedFromFacilitator(REALM_NAME_IMPLICIT); final String redirectUri = authenticateAtOP(prepareAuthResponse.getAuthUri()); - Tuple tokens = completeAuthentication(redirectUri, prepareAuthResponse.getState(), - prepareAuthResponse.getNonce(), REALM_NAME_IMPLICIT); + Tuple tokens = completeAuthentication( + redirectUri, + prepareAuthResponse.getState(), + prepareAuthResponse.getNonce(), + REALM_NAME_IMPLICIT + ); verifyElasticsearchAccessTokenForImplicitFlow(tokens.v1()); } @@ -345,8 +376,12 @@ public void testAuthenticateWithCodeFlowUsingHttpProxy() throws Exception { final PrepareAuthResponse prepareAuthResponse = getRedirectedFromFacilitator(REALM_NAME_PROXY); final String redirectUri = authenticateAtOP(prepareAuthResponse.getAuthUri()); - Tuple tokens = completeAuthentication(redirectUri, prepareAuthResponse.getState(), - prepareAuthResponse.getNonce(), REALM_NAME_PROXY); + Tuple tokens = completeAuthentication( + redirectUri, + prepareAuthResponse.getState(), + prepareAuthResponse.getNonce(), + REALM_NAME_PROXY + ); verifyElasticsearchAccessTokenForCodeFlow(tokens.v1()); } @@ -355,9 +390,12 @@ public void testAuthenticateWithCodeFlowFailsForWrongRealm() throws Exception { final String redirectUri = authenticateAtOP(prepareAuthResponse.getAuthUri()); // Use existing realm that can't authenticate the response, or a non-existent realm ResponseException e = expectThrows(ResponseException.class, () -> { - completeAuthentication(redirectUri, + completeAuthentication( + redirectUri, prepareAuthResponse.getState(), - prepareAuthResponse.getNonce(), randomFrom(REALM_NAME_IMPLICIT, REALM_NAME + randomAlphaOfLength(8))); + prepareAuthResponse.getNonce(), + randomFrom(REALM_NAME_IMPLICIT, REALM_NAME + randomAlphaOfLength(8)) + ); }); assertThat(401, equalTo(e.getResponse().getStatusLine().getStatusCode())); } @@ -420,7 +458,7 @@ private Tuple completeAuthentication(String redirectUri, String assertNotNull(responseBody.get("access_token")); assertNotNull(responseBody.get("refresh_token")); assertNotNull(responseBody.get("authentication")); - assertEquals("alice", ((Map)responseBody.get("authentication")).get("username")); + assertEquals("alice", ((Map) responseBody.get("authentication")).get("username")); return Tuple.tuple(responseBody.get("access_token").toString(), responseBody.get("refresh_token").toString()); } } @@ -440,8 +478,7 @@ private Request buildRequest(String method, String endpoint, Map body } private static BasicHeader facilitatorAuth() { - final String auth = - UsernamePasswordToken.basicAuthHeaderValue("facilitator", new SecureString(FACILITATOR_PASSWORD.toCharArray())); + final String auth = UsernamePasswordToken.basicAuthHeaderValue("facilitator", new SecureString(FACILITATOR_PASSWORD.toCharArray())); return new BasicHeader(UsernamePasswordToken.BASIC_AUTH_HEADER, auth); } @@ -449,7 +486,6 @@ private Map parseResponseAsMap(HttpEntity entity) throws IOExcep return convertToMap(XContentType.JSON.xContent(), entity.getContent(), false); } - private void assertHttpOk(StatusLine status) { assertThat("Unexpected HTTP Response status: " + status, status.getStatusCode(), Matchers.equalTo(200)); } @@ -472,41 +508,52 @@ private void setFacilitatorUser() throws Exception { private void setRoleMappings() throws Exception { try (RestClient restClient = getClient()) { Request createRoleMappingRequest = new Request("PUT", "/_security/role_mapping/oidc_kibana"); - createRoleMappingRequest.setJsonEntity("{ \"roles\" : [\"kibana_admin\"]," + - "\"enabled\": true," + - "\"rules\": {" + - " \"any\" : [" + - " {\"field\": { \"realm.name\": \"" + REALM_NAME + "\"} }," + - " {\"field\": { \"realm.name\": \"" + REALM_NAME_PROXY + "\"} }," + - " {\"field\": { \"realm.name\": \"" + REALM_NAME_CLIENT_POST_AUTH + "\"} }," + - " {\"field\": { \"realm.name\": \"" + REALM_NAME_CLIENT_JWT_AUTH + "\"} }" + - " ]" + - "}" + - "}"); + createRoleMappingRequest.setJsonEntity( + "{ \"roles\" : [\"kibana_admin\"]," + + "\"enabled\": true," + + "\"rules\": {" + + " \"any\" : [" + + " {\"field\": { \"realm.name\": \"" + + REALM_NAME + + "\"} }," + + " {\"field\": { \"realm.name\": \"" + + REALM_NAME_PROXY + + "\"} }," + + " {\"field\": { \"realm.name\": \"" + + REALM_NAME_CLIENT_POST_AUTH + + "\"} }," + + " {\"field\": { \"realm.name\": \"" + + REALM_NAME_CLIENT_JWT_AUTH + + "\"} }" + + " ]" + + "}" + + "}" + ); restClient.performRequest(createRoleMappingRequest); createRoleMappingRequest = new Request("PUT", "/_security/role_mapping/oidc_limited"); - createRoleMappingRequest.setJsonEntity("{ \"roles\" : [\"limited_user\"]," + - "\"enabled\": true," + - "\"rules\": {" + - "\"field\": { \"realm.name\": \"" + REALM_NAME_IMPLICIT + "\"}" + - "}" + - "}"); + createRoleMappingRequest.setJsonEntity( + "{ \"roles\" : [\"limited_user\"]," + + "\"enabled\": true," + + "\"rules\": {" + + "\"field\": { \"realm.name\": \"" + + REALM_NAME_IMPLICIT + + "\"}" + + "}" + + "}" + ); restClient.performRequest(createRoleMappingRequest); createRoleMappingRequest = new Request("PUT", "/_security/role_mapping/oidc_auditor"); - createRoleMappingRequest.setJsonEntity("{ \"roles\" : [\"auditor\"]," + - "\"enabled\": true," + - "\"rules\": {" + - "\"field\": { \"groups\": \"audit\"}" + - "}" + - "}"); + createRoleMappingRequest.setJsonEntity( + "{ \"roles\" : [\"auditor\"]," + "\"enabled\": true," + "\"rules\": {" + "\"field\": { \"groups\": \"audit\"}" + "}" + "}" + ); restClient.performRequest(createRoleMappingRequest); } } private RestClient getClient() throws Exception { - return buildClient(restAdminSettings(), new HttpHost[]{new HttpHost("localhost", Integer.parseInt(ES_PORT), "https")}); + return buildClient(restAdminSettings(), new HttpHost[] { new HttpHost("localhost", Integer.parseInt(ES_PORT), "https") }); } /** diff --git a/x-pack/qa/openldap-tests/src/test/java/org/elasticsearch/test/OpenLdapTests.java b/x-pack/qa/openldap-tests/src/test/java/org/elasticsearch/test/OpenLdapTests.java index da7734d110706..b172092cbcab4 100644 --- a/x-pack/qa/openldap-tests/src/test/java/org/elasticsearch/test/OpenLdapTests.java +++ b/x-pack/qa/openldap-tests/src/test/java/org/elasticsearch/test/OpenLdapTests.java @@ -8,14 +8,15 @@ import com.unboundid.ldap.sdk.LDAPConnection; import com.unboundid.ldap.sdk.LDAPException; + import org.elasticsearch.action.support.PlainActionFuture; import org.elasticsearch.common.settings.MockSecureSettings; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.ssl.SslVerificationMode; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.common.util.concurrent.UncategorizedExecutionException; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.env.Environment; import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.test.junit.annotations.Network; @@ -120,16 +121,19 @@ public void initializeSslSocketFactory() throws Exception { } public void testConnect() throws Exception { - //openldap does not use cn as naming attributes by default + // openldap does not use cn as naming attributes by default String groupSearchBase = "ou=people,dc=oldap,dc=test,dc=elasticsearch,dc=com"; String userTemplate = "uid={0},ou=people,dc=oldap,dc=test,dc=elasticsearch,dc=com"; final RealmConfig.RealmIdentifier realmId = new RealmConfig.RealmIdentifier("ldap", "oldap-test"); - RealmConfig config = new RealmConfig(realmId, + RealmConfig config = new RealmConfig( + realmId, buildLdapSettings(realmId, OPEN_LDAP_DNS_URL, userTemplate, groupSearchBase, LdapSearchScope.ONE_LEVEL), - TestEnvironment.newEnvironment(globalSettings), new ThreadContext(Settings.EMPTY)); + TestEnvironment.newEnvironment(globalSettings), + new ThreadContext(Settings.EMPTY) + ); LdapSessionFactory sessionFactory = new LdapSessionFactory(config, sslService, threadPool); - String[] users = new String[]{"blackwidow", "cap", "hawkeye", "hulk", "ironman", "thor"}; + String[] users = new String[] { "blackwidow", "cap", "hawkeye", "hulk", "ironman", "thor" }; for (String user : users) { logger.info("testing connect as user [{}]", user); try (LdapSession ldap = session(sessionFactory, user, PASSWORD_SECURE_STRING)) { @@ -139,17 +143,20 @@ public void testConnect() throws Exception { } public void testGroupSearchScopeBase() throws Exception { - //base search on a groups means that the user can be in just one group + // base search on a groups means that the user can be in just one group String groupSearchBase = "cn=Avengers,ou=people,dc=oldap,dc=test,dc=elasticsearch,dc=com"; String userTemplate = "uid={0},ou=people,dc=oldap,dc=test,dc=elasticsearch,dc=com"; final RealmConfig.RealmIdentifier realmId = new RealmConfig.RealmIdentifier("ldap", REALM_NAME); - RealmConfig config = new RealmConfig(realmId, - buildLdapSettings(realmId, OPEN_LDAP_DNS_URL, userTemplate, groupSearchBase, LdapSearchScope.BASE), - TestEnvironment.newEnvironment(globalSettings), new ThreadContext(Settings.EMPTY)); + RealmConfig config = new RealmConfig( + realmId, + buildLdapSettings(realmId, OPEN_LDAP_DNS_URL, userTemplate, groupSearchBase, LdapSearchScope.BASE), + TestEnvironment.newEnvironment(globalSettings), + new ThreadContext(Settings.EMPTY) + ); LdapSessionFactory sessionFactory = new LdapSessionFactory(config, sslService, threadPool); - String[] users = new String[]{"blackwidow", "cap", "hawkeye", "hulk", "ironman", "thor"}; + String[] users = new String[] { "blackwidow", "cap", "hawkeye", "hulk", "ironman", "thor" }; for (String user : users) { try (LdapSession ldap = session(sessionFactory, user, PASSWORD_SECURE_STRING)) { assertThat(groups(ldap), hasItem(containsString("Avengers"))); @@ -166,8 +173,12 @@ public void testCustomFilter() throws Exception { .put(getFullSettingKey(realmId.getName(), SearchGroupsResolverSettings.FILTER), "(&(objectclass=posixGroup)(memberUid={0}))") .put(getFullSettingKey(realmId.getName(), SearchGroupsResolverSettings.USER_ATTRIBUTE), "uid") .build(); - RealmConfig config = new RealmConfig(realmId, settings, - TestEnvironment.newEnvironment(globalSettings), new ThreadContext(Settings.EMPTY)); + RealmConfig config = new RealmConfig( + realmId, + settings, + TestEnvironment.newEnvironment(globalSettings), + new ThreadContext(Settings.EMPTY) + ); LdapSessionFactory sessionFactory = new LdapSessionFactory(config, sslService, threadPool); try (LdapSession ldap = session(sessionFactory, "selvig", PASSWORD_SECURE_STRING)) { @@ -177,7 +188,7 @@ public void testCustomFilter() throws Exception { @Network public void testStandardLdapConnectionHostnameVerificationFailure() throws Exception { - //openldap does not use cn as naming attributes by default + // openldap does not use cn as naming attributes by default String groupSearchBase = "ou=people,dc=oldap,dc=test,dc=elasticsearch,dc=com"; String userTemplate = "uid={0},ou=people,dc=oldap,dc=test,dc=elasticsearch,dc=com"; final RealmConfig.RealmIdentifier realmId = new RealmConfig.RealmIdentifier("ldap", "vmode_full"); @@ -190,16 +201,20 @@ public void testStandardLdapConnectionHostnameVerificationFailure() throws Excep LdapSessionFactory sessionFactory = new LdapSessionFactory(config, sslService, threadPool); String user = "blackwidow"; - UncategorizedExecutionException e = expectThrows(UncategorizedExecutionException.class, - () -> session(sessionFactory, user, PASSWORD_SECURE_STRING)); + UncategorizedExecutionException e = expectThrows( + UncategorizedExecutionException.class, + () -> session(sessionFactory, user, PASSWORD_SECURE_STRING) + ); assertThat(e.getCause(), instanceOf(ExecutionException.class)); assertThat(e.getCause().getCause(), instanceOf(LDAPException.class)); - assertThat(e.getCause().getCause().getMessage(), - anyOf(containsString("Hostname verification failed"), containsString("peer not authenticated"))); + assertThat( + e.getCause().getCause().getMessage(), + anyOf(containsString("Hostname verification failed"), containsString("peer not authenticated")) + ); } public void testStandardLdapConnectionHostnameVerificationSuccess() throws Exception { - //openldap does not use cn as naming attributes by default + // openldap does not use cn as naming attributes by default String groupSearchBase = "ou=people,dc=oldap,dc=test,dc=elasticsearch,dc=com"; String userTemplate = "uid={0},ou=people,dc=oldap,dc=test,dc=elasticsearch,dc=com"; final RealmConfig.RealmIdentifier realmId = new RealmConfig.RealmIdentifier("ldap", "vmode_full"); @@ -208,8 +223,12 @@ public void testStandardLdapConnectionHostnameVerificationSuccess() throws Excep .put(buildLdapSettings(realmId, OPEN_LDAP_DNS_URL, userTemplate, groupSearchBase, LdapSearchScope.ONE_LEVEL)) .build(); - RealmConfig config = new RealmConfig(realmId, settings, - TestEnvironment.newEnvironment(globalSettings), new ThreadContext(Settings.EMPTY)); + RealmConfig config = new RealmConfig( + realmId, + settings, + TestEnvironment.newEnvironment(globalSettings), + new ThreadContext(Settings.EMPTY) + ); LdapSessionFactory sessionFactory = new LdapSessionFactory(config, sslService, threadPool); final String user = "blackwidow"; @@ -222,12 +241,19 @@ public void testStandardLdapConnectionHostnameVerificationSuccess() throws Excep public void testResolveSingleValuedAttributeFromConnection() throws Exception { final RealmConfig.RealmIdentifier realmId = new RealmConfig.RealmIdentifier("ldap", "oldap-test"); final Settings settings = Settings.builder() - .putList(getFullSettingKey(realmId.getName(), LdapMetadataResolverSettings.ADDITIONAL_METADATA_SETTING.apply("ldap")), - "cn", "sn") - .put(getFullSettingKey(realmId, RealmSettings.ORDER_SETTING), 0) - .build(); - final RealmConfig config = new RealmConfig(realmId, settings, - TestEnvironment.newEnvironment(globalSettings), new ThreadContext(Settings.EMPTY)); + .putList( + getFullSettingKey(realmId.getName(), LdapMetadataResolverSettings.ADDITIONAL_METADATA_SETTING.apply("ldap")), + "cn", + "sn" + ) + .put(getFullSettingKey(realmId, RealmSettings.ORDER_SETTING), 0) + .build(); + final RealmConfig config = new RealmConfig( + realmId, + settings, + TestEnvironment.newEnvironment(globalSettings), + new ThreadContext(Settings.EMPTY) + ); LdapMetadataResolver resolver = new LdapMetadataResolver(config, true); try (LDAPConnection ldapConnection = setupOpenLdapConnection()) { final Map map = resolve(ldapConnection, resolver); @@ -240,12 +266,18 @@ public void testResolveSingleValuedAttributeFromConnection() throws Exception { public void testResolveMultiValuedAttributeFromConnection() throws Exception { final RealmConfig.RealmIdentifier realmId = new RealmConfig.RealmIdentifier("ldap", "oldap-test"); final Settings settings = Settings.builder() - .putList(getFullSettingKey(realmId.getName(), LdapMetadataResolverSettings.ADDITIONAL_METADATA_SETTING.apply("ldap")), - "objectClass") - .put(getFullSettingKey(realmId, RealmSettings.ORDER_SETTING), 0) - .build(); - final RealmConfig config = new RealmConfig(realmId, settings, - TestEnvironment.newEnvironment(globalSettings), new ThreadContext(Settings.EMPTY)); + .putList( + getFullSettingKey(realmId.getName(), LdapMetadataResolverSettings.ADDITIONAL_METADATA_SETTING.apply("ldap")), + "objectClass" + ) + .put(getFullSettingKey(realmId, RealmSettings.ORDER_SETTING), 0) + .build(); + final RealmConfig config = new RealmConfig( + realmId, + settings, + TestEnvironment.newEnvironment(globalSettings), + new ThreadContext(Settings.EMPTY) + ); LdapMetadataResolver resolver = new LdapMetadataResolver(config, true); try (LDAPConnection ldapConnection = setupOpenLdapConnection()) { final Map map = resolve(ldapConnection, resolver); @@ -258,12 +290,15 @@ public void testResolveMultiValuedAttributeFromConnection() throws Exception { public void testResolveMissingAttributeFromConnection() throws Exception { final RealmConfig.RealmIdentifier realmId = new RealmConfig.RealmIdentifier("ldap", "oldap-test"); final Settings settings = Settings.builder() - .putList(getFullSettingKey(realmId.getName(), LdapMetadataResolverSettings.ADDITIONAL_METADATA_SETTING.apply("ldap")), - "alias") - .put(getFullSettingKey(realmId, RealmSettings.ORDER_SETTING), 0) - .build(); - final RealmConfig config = new RealmConfig(realmId, settings, - TestEnvironment.newEnvironment(globalSettings), new ThreadContext(Settings.EMPTY)); + .putList(getFullSettingKey(realmId.getName(), LdapMetadataResolverSettings.ADDITIONAL_METADATA_SETTING.apply("ldap")), "alias") + .put(getFullSettingKey(realmId, RealmSettings.ORDER_SETTING), 0) + .build(); + final RealmConfig config = new RealmConfig( + realmId, + settings, + TestEnvironment.newEnvironment(globalSettings), + new ThreadContext(Settings.EMPTY) + ); LdapMetadataResolver resolver = new LdapMetadataResolver(config, true); try (LDAPConnection ldapConnection = setupOpenLdapConnection()) { final Map map = resolve(ldapConnection, resolver); @@ -271,15 +306,19 @@ public void testResolveMissingAttributeFromConnection() throws Exception { } } - private Settings buildLdapSettings(RealmConfig.RealmIdentifier realmId, String ldapUrl, String userTemplate, - String groupSearchBase, LdapSearchScope scope) { - final String[] urls = {ldapUrl}; - final String[] templates = {userTemplate}; + private Settings buildLdapSettings( + RealmConfig.RealmIdentifier realmId, + String ldapUrl, + String userTemplate, + String groupSearchBase, + LdapSearchScope scope + ) { + final String[] urls = { ldapUrl }; + final String[] templates = { userTemplate }; Settings.Builder builder = Settings.builder() .put(LdapTestCase.buildLdapSettings(realmId, urls, templates, groupSearchBase, scope, null, false)); builder.put(getFullSettingKey(realmId.getName(), SearchGroupsResolverSettings.USER_ATTRIBUTE), "uid"); - return builder - .put(SSLConfigurationSettings.TRUSTSTORE_PATH.realm(realmId).getKey(), getDataPath(LDAPTRUST_PATH)) + return builder.put(SSLConfigurationSettings.TRUSTSTORE_PATH.realm(realmId).getKey(), getDataPath(LDAPTRUST_PATH)) .put(SSLConfigurationSettings.LEGACY_TRUSTSTORE_PASSWORD.realm(realmId).getKey(), "changeit") .put(globalSettings) .put(getFullSettingKey(realmId, RealmSettings.ORDER_SETTING), 0) diff --git a/x-pack/qa/openldap-tests/src/test/java/org/elasticsearch/xpack/security/authc/ldap/OpenLdapUserSearchSessionFactoryTests.java b/x-pack/qa/openldap-tests/src/test/java/org/elasticsearch/xpack/security/authc/ldap/OpenLdapUserSearchSessionFactoryTests.java index c6bf52be0cf79..eb3365010b550 100644 --- a/x-pack/qa/openldap-tests/src/test/java/org/elasticsearch/xpack/security/authc/ldap/OpenLdapUserSearchSessionFactoryTests.java +++ b/x-pack/qa/openldap-tests/src/test/java/org/elasticsearch/xpack/security/authc/ldap/OpenLdapUserSearchSessionFactoryTests.java @@ -76,52 +76,88 @@ public void testUserSearchWithBindUserOpenLDAP() throws Exception { String userSearchBase = "ou=people,dc=oldap,dc=test,dc=elasticsearch,dc=com"; final RealmConfig.RealmIdentifier realmId = new RealmConfig.RealmIdentifier("ldap", "oldap-test"); final Settings.Builder realmSettings = Settings.builder() - .put(LdapTestCase.buildLdapSettings(realmId, new String[]{OpenLdapTests.OPEN_LDAP_DNS_URL}, Strings.EMPTY_ARRAY, - groupSearchBase, LdapSearchScope.ONE_LEVEL, null, false)) - .put(getFullSettingKey(realmId.getName(), LdapUserSearchSessionFactorySettings.SEARCH_BASE_DN), userSearchBase) - .put(getFullSettingKey(realmId.getName(), SearchGroupsResolverSettings.USER_ATTRIBUTE), "uid") - .put(getFullSettingKey(realmId, PoolingSessionFactorySettings.BIND_DN), - "uid=blackwidow,ou=people,dc=oldap,dc=test,dc=elasticsearch,dc=com") - .put(getFullSettingKey(realmId.getName(), LdapUserSearchSessionFactorySettings.POOL_ENABLED), randomBoolean()) - .put(getFullSettingKey(realmId, SSLConfigurationSettings.VERIFICATION_MODE_SETTING_REALM), "full"); + .put( + LdapTestCase.buildLdapSettings( + realmId, + new String[] { OpenLdapTests.OPEN_LDAP_DNS_URL }, + Strings.EMPTY_ARRAY, + groupSearchBase, + LdapSearchScope.ONE_LEVEL, + null, + false + ) + ) + .put(getFullSettingKey(realmId.getName(), LdapUserSearchSessionFactorySettings.SEARCH_BASE_DN), userSearchBase) + .put(getFullSettingKey(realmId.getName(), SearchGroupsResolverSettings.USER_ATTRIBUTE), "uid") + .put( + getFullSettingKey(realmId, PoolingSessionFactorySettings.BIND_DN), + "uid=blackwidow,ou=people,dc=oldap,dc=test,dc=elasticsearch,dc=com" + ) + .put(getFullSettingKey(realmId.getName(), LdapUserSearchSessionFactorySettings.POOL_ENABLED), randomBoolean()) + .put(getFullSettingKey(realmId, SSLConfigurationSettings.VERIFICATION_MODE_SETTING_REALM), "full"); if (useSecureBindPassword) { final MockSecureSettings secureSettings = new MockSecureSettings(); - secureSettings.setString(getFullSettingKey(realmId, PoolingSessionFactorySettings.SECURE_BIND_PASSWORD), - OpenLdapTests.PASSWORD); + secureSettings.setString( + getFullSettingKey(realmId, PoolingSessionFactorySettings.SECURE_BIND_PASSWORD), + OpenLdapTests.PASSWORD + ); realmSettings.setSecureSettings(secureSettings); } else { realmSettings.put(getFullSettingKey(realmId, PoolingSessionFactorySettings.LEGACY_BIND_PASSWORD), OpenLdapTests.PASSWORD); } - final Settings settings = realmSettings.put(globalSettings) - .put(getFullSettingKey(realmId, RealmSettings.ORDER_SETTING), 0).build(); - RealmConfig config = new RealmConfig(realmId, settings, - TestEnvironment.newEnvironment(globalSettings), new ThreadContext(globalSettings)); + final Settings settings = realmSettings.put(globalSettings).put(getFullSettingKey(realmId, RealmSettings.ORDER_SETTING), 0).build(); + RealmConfig config = new RealmConfig( + realmId, + settings, + TestEnvironment.newEnvironment(globalSettings), + new ThreadContext(globalSettings) + ); SSLService sslService = new SSLService(TestEnvironment.newEnvironment(settings)); - String[] users = new String[]{"cap", "hawkeye", "hulk", "ironman", "thor"}; + String[] users = new String[] { "cap", "hawkeye", "hulk", "ironman", "thor" }; try (LdapUserSearchSessionFactory sessionFactory = new LdapUserSearchSessionFactory(config, sslService, threadPool)) { for (String user : users) { - //auth + // auth try (LdapSession ldap = session(sessionFactory, user, new SecureString(OpenLdapTests.PASSWORD))) { - assertThat(ldap.userDn(), is(equalTo(new MessageFormat("uid={0},ou=people,dc=oldap,dc=test,dc=elasticsearch,dc=com", - Locale.ROOT).format(new Object[]{user}, new StringBuffer(), null).toString()))); + assertThat( + ldap.userDn(), + is( + equalTo( + new MessageFormat("uid={0},ou=people,dc=oldap,dc=test,dc=elasticsearch,dc=com", Locale.ROOT).format( + new Object[] { user }, + new StringBuffer(), + null + ).toString() + ) + ) + ); assertThat(groups(ldap), hasItem(containsString("Avengers"))); } - //lookup + // lookup try (LdapSession ldap = unauthenticatedSession(sessionFactory, user)) { - assertThat(ldap.userDn(), is(equalTo(new MessageFormat("uid={0},ou=people,dc=oldap,dc=test,dc=elasticsearch,dc=com", - Locale.ROOT).format(new Object[]{user}, new StringBuffer(), null).toString()))); + assertThat( + ldap.userDn(), + is( + equalTo( + new MessageFormat("uid={0},ou=people,dc=oldap,dc=test,dc=elasticsearch,dc=com", Locale.ROOT).format( + new Object[] { user }, + new StringBuffer(), + null + ).toString() + ) + ) + ); assertThat(groups(ldap), hasItem(containsString("Avengers"))); } } } if (useSecureBindPassword == false) { - assertSettingDeprecationsAndWarnings(new Setting[]{ - config.getConcreteSetting(PoolingSessionFactorySettings.LEGACY_BIND_PASSWORD) - }); + assertSettingDeprecationsAndWarnings( + new Setting[] { config.getConcreteSetting(PoolingSessionFactorySettings.LEGACY_BIND_PASSWORD) } + ); } } diff --git a/x-pack/qa/openldap-tests/src/test/java/org/elasticsearch/xpack/security/authc/ldap/SearchGroupsResolverTests.java b/x-pack/qa/openldap-tests/src/test/java/org/elasticsearch/xpack/security/authc/ldap/SearchGroupsResolverTests.java index 436d626e4c695..feec06f4b3b6d 100644 --- a/x-pack/qa/openldap-tests/src/test/java/org/elasticsearch/xpack/security/authc/ldap/SearchGroupsResolverTests.java +++ b/x-pack/qa/openldap-tests/src/test/java/org/elasticsearch/xpack/security/authc/ldap/SearchGroupsResolverTests.java @@ -31,83 +31,118 @@ public class SearchGroupsResolverTests extends GroupsResolverTestCase { public void testResolveSubTree() throws Exception { Settings settings = Settings.builder() - .put(getFullSettingKey(REALM_ID, SearchGroupsResolverSettings.BASE_DN), "dc=oldap,dc=test,dc=elasticsearch,dc=com") - .put(getFullSettingKey(REALM_ID.getName(), SearchGroupsResolverSettings.USER_ATTRIBUTE), "uid") - .build(); + .put(getFullSettingKey(REALM_ID, SearchGroupsResolverSettings.BASE_DN), "dc=oldap,dc=test,dc=elasticsearch,dc=com") + .put(getFullSettingKey(REALM_ID.getName(), SearchGroupsResolverSettings.USER_ATTRIBUTE), "uid") + .build(); SearchGroupsResolver resolver = new SearchGroupsResolver(config(REALM_ID, settings)); - List groups = - resolveBlocking(resolver, ldapConnection, BRUCE_BANNER_DN, TimeValue.timeValueSeconds(10), NoOpLogger.INSTANCE, null); - assertThat(groups, containsInAnyOrder( + List groups = resolveBlocking( + resolver, + ldapConnection, + BRUCE_BANNER_DN, + TimeValue.timeValueSeconds(10), + NoOpLogger.INSTANCE, + null + ); + assertThat( + groups, + containsInAnyOrder( containsString("Avengers"), containsString("SHIELD"), containsString("Geniuses"), - containsString("Philanthropists"))); + containsString("Philanthropists") + ) + ); } public void testResolveOneLevel() throws Exception { Settings settings = Settings.builder() - .put(getFullSettingKey(REALM_ID, SearchGroupsResolverSettings.BASE_DN), - "ou=people,dc=oldap,dc=test,dc=elasticsearch,dc=com") - .put("group_search.scope", LdapSearchScope.ONE_LEVEL) - .put(getFullSettingKey(REALM_ID.getName(), SearchGroupsResolverSettings.USER_ATTRIBUTE), "uid") - .build(); + .put(getFullSettingKey(REALM_ID, SearchGroupsResolverSettings.BASE_DN), "ou=people,dc=oldap,dc=test,dc=elasticsearch,dc=com") + .put("group_search.scope", LdapSearchScope.ONE_LEVEL) + .put(getFullSettingKey(REALM_ID.getName(), SearchGroupsResolverSettings.USER_ATTRIBUTE), "uid") + .build(); SearchGroupsResolver resolver = new SearchGroupsResolver(config(REALM_ID, settings)); - List groups = - resolveBlocking(resolver, ldapConnection, BRUCE_BANNER_DN, TimeValue.timeValueSeconds(10), NoOpLogger.INSTANCE, null); - assertThat(groups, containsInAnyOrder( + List groups = resolveBlocking( + resolver, + ldapConnection, + BRUCE_BANNER_DN, + TimeValue.timeValueSeconds(10), + NoOpLogger.INSTANCE, + null + ); + assertThat( + groups, + containsInAnyOrder( containsString("Avengers"), containsString("SHIELD"), containsString("Geniuses"), - containsString("Philanthropists"))); + containsString("Philanthropists") + ) + ); } public void testResolveBase() throws Exception { Settings settings = Settings.builder() - .put(getFullSettingKey(REALM_ID, SearchGroupsResolverSettings.BASE_DN), - "cn=Avengers,ou=People,dc=oldap,dc=test,dc=elasticsearch,dc=com") - .put("group_search.scope", LdapSearchScope.BASE) - .put(getFullSettingKey(REALM_ID.getName(), SearchGroupsResolverSettings.USER_ATTRIBUTE), "uid") - .build(); + .put( + getFullSettingKey(REALM_ID, SearchGroupsResolverSettings.BASE_DN), + "cn=Avengers,ou=People,dc=oldap,dc=test,dc=elasticsearch,dc=com" + ) + .put("group_search.scope", LdapSearchScope.BASE) + .put(getFullSettingKey(REALM_ID.getName(), SearchGroupsResolverSettings.USER_ATTRIBUTE), "uid") + .build(); SearchGroupsResolver resolver = new SearchGroupsResolver(config(REALM_ID, settings)); - List groups = - resolveBlocking(resolver, ldapConnection, BRUCE_BANNER_DN, TimeValue.timeValueSeconds(10), NoOpLogger.INSTANCE, null); + List groups = resolveBlocking( + resolver, + ldapConnection, + BRUCE_BANNER_DN, + TimeValue.timeValueSeconds(10), + NoOpLogger.INSTANCE, + null + ); assertThat(groups, hasItem(containsString("Avengers"))); } public void testResolveCustomFilter() throws Exception { Settings settings = Settings.builder() - .put(getFullSettingKey(REALM_ID, SearchGroupsResolverSettings.BASE_DN), "dc=oldap,dc=test,dc=elasticsearch,dc=com") - .put("group_search.filter", "(&(objectclass=posixGroup)(memberUID={0}))") - .put(getFullSettingKey(REALM_ID.getName(), SearchGroupsResolverSettings.USER_ATTRIBUTE), "uid") - .build(); + .put(getFullSettingKey(REALM_ID, SearchGroupsResolverSettings.BASE_DN), "dc=oldap,dc=test,dc=elasticsearch,dc=com") + .put("group_search.filter", "(&(objectclass=posixGroup)(memberUID={0}))") + .put(getFullSettingKey(REALM_ID.getName(), SearchGroupsResolverSettings.USER_ATTRIBUTE), "uid") + .build(); SearchGroupsResolver resolver = new SearchGroupsResolver(config(REALM_ID, settings)); - List groups = - resolveBlocking(resolver, ldapConnection, "uid=selvig,ou=people,dc=oldap,dc=test,dc=elasticsearch,dc=com", - TimeValue.timeValueSeconds(10), NoOpLogger.INSTANCE, null); + List groups = resolveBlocking( + resolver, + ldapConnection, + "uid=selvig,ou=people,dc=oldap,dc=test,dc=elasticsearch,dc=com", + TimeValue.timeValueSeconds(10), + NoOpLogger.INSTANCE, + null + ); assertThat(groups, hasItem(containsString("Geniuses"))); } public void testFilterIncludesPosixGroups() throws Exception { Settings settings = Settings.builder() - .put(getFullSettingKey(REALM_ID, SearchGroupsResolverSettings.BASE_DN), "dc=oldap,dc=test,dc=elasticsearch,dc=com") - .put(getFullSettingKey(REALM_ID.getName(), SearchGroupsResolverSettings.USER_ATTRIBUTE), "uid") - .build(); + .put(getFullSettingKey(REALM_ID, SearchGroupsResolverSettings.BASE_DN), "dc=oldap,dc=test,dc=elasticsearch,dc=com") + .put(getFullSettingKey(REALM_ID.getName(), SearchGroupsResolverSettings.USER_ATTRIBUTE), "uid") + .build(); SearchGroupsResolver resolver = new SearchGroupsResolver(config(REALM_ID, settings)); - List groups = - resolveBlocking(resolver, ldapConnection, "uid=selvig,ou=people,dc=oldap,dc=test,dc=elasticsearch,dc=com", - TimeValue.timeValueSeconds(10), NoOpLogger.INSTANCE, null); + List groups = resolveBlocking( + resolver, + ldapConnection, + "uid=selvig,ou=people,dc=oldap,dc=test,dc=elasticsearch,dc=com", + TimeValue.timeValueSeconds(10), + NoOpLogger.INSTANCE, + null + ); assertThat(groups, hasItem(containsString("Geniuses"))); } public void testCreateWithoutSpecifyingBaseDN() throws Exception { - Settings settings = Settings.builder() - .put("group_search.scope", LdapSearchScope.SUB_TREE) - .build(); + Settings settings = Settings.builder().put("group_search.scope", LdapSearchScope.SUB_TREE).build(); try { new SearchGroupsResolver(config(REALM_ID, settings)); @@ -119,8 +154,9 @@ public void testCreateWithoutSpecifyingBaseDN() throws Exception { public void testReadUserAttributeUid() throws Exception { Settings settings = Settings.builder() - .put(getFullSettingKey(REALM_ID, SearchGroupsResolverSettings.BASE_DN), "dc=oldap,dc=test,dc=elasticsearch,dc=com") - .put(getFullSettingKey(REALM_ID.getName(), SearchGroupsResolverSettings.USER_ATTRIBUTE), "uid").build(); + .put(getFullSettingKey(REALM_ID, SearchGroupsResolverSettings.BASE_DN), "dc=oldap,dc=test,dc=elasticsearch,dc=com") + .put(getFullSettingKey(REALM_ID.getName(), SearchGroupsResolverSettings.USER_ATTRIBUTE), "uid") + .build(); SearchGroupsResolver resolver = new SearchGroupsResolver(config(REALM_ID, settings)); PlainActionFuture future = new PlainActionFuture<>(); resolver.readUserAttribute(ldapConnection, BRUCE_BANNER_DN, TimeValue.timeValueSeconds(5), future); @@ -129,9 +165,9 @@ public void testReadUserAttributeUid() throws Exception { public void testReadUserAttributeCn() throws Exception { Settings settings = Settings.builder() - .put(getFullSettingKey(REALM_ID, SearchGroupsResolverSettings.BASE_DN), "dc=oldap,dc=test,dc=elasticsearch,dc=com") - .put(getFullSettingKey(REALM_ID.getName(), SearchGroupsResolverSettings.USER_ATTRIBUTE), "cn") - .build(); + .put(getFullSettingKey(REALM_ID, SearchGroupsResolverSettings.BASE_DN), "dc=oldap,dc=test,dc=elasticsearch,dc=com") + .put(getFullSettingKey(REALM_ID.getName(), SearchGroupsResolverSettings.USER_ATTRIBUTE), "cn") + .build(); SearchGroupsResolver resolver = new SearchGroupsResolver(config(REALM_ID, settings)); PlainActionFuture future = new PlainActionFuture<>(); @@ -141,9 +177,9 @@ public void testReadUserAttributeCn() throws Exception { public void testReadNonExistentUserAttribute() throws Exception { Settings settings = Settings.builder() - .put(getFullSettingKey(REALM_ID, SearchGroupsResolverSettings.BASE_DN), "dc=oldap,dc=test,dc=elasticsearch,dc=com") - .put(getFullSettingKey(REALM_ID.getName(), SearchGroupsResolverSettings.USER_ATTRIBUTE), "doesntExists") - .build(); + .put(getFullSettingKey(REALM_ID, SearchGroupsResolverSettings.BASE_DN), "dc=oldap,dc=test,dc=elasticsearch,dc=com") + .put(getFullSettingKey(REALM_ID.getName(), SearchGroupsResolverSettings.USER_ATTRIBUTE), "doesntExists") + .build(); SearchGroupsResolver resolver = new SearchGroupsResolver(config(REALM_ID, settings)); PlainActionFuture future = new PlainActionFuture<>(); @@ -153,9 +189,9 @@ public void testReadNonExistentUserAttribute() throws Exception { public void testReadBinaryUserAttribute() throws Exception { Settings settings = Settings.builder() - .put(getFullSettingKey(REALM_ID, SearchGroupsResolverSettings.BASE_DN), "dc=oldap,dc=test,dc=elasticsearch,dc=com") - .put(getFullSettingKey(REALM_ID.getName(), SearchGroupsResolverSettings.USER_ATTRIBUTE), "userPassword") - .build(); + .put(getFullSettingKey(REALM_ID, SearchGroupsResolverSettings.BASE_DN), "dc=oldap,dc=test,dc=elasticsearch,dc=com") + .put(getFullSettingKey(REALM_ID.getName(), SearchGroupsResolverSettings.USER_ATTRIBUTE), "userPassword") + .build(); SearchGroupsResolver resolver = new SearchGroupsResolver(config(REALM_ID, settings)); PlainActionFuture future = new PlainActionFuture<>(); diff --git a/x-pack/qa/password-protected-keystore/src/test/java/org/elasticsearch/password_protected_keystore/ReloadSecureSettingsWithPasswordProtectedKeystoreRestIT.java b/x-pack/qa/password-protected-keystore/src/test/java/org/elasticsearch/password_protected_keystore/ReloadSecureSettingsWithPasswordProtectedKeystoreRestIT.java index 5ecb2d074f5a2..247f676ac58bf 100644 --- a/x-pack/qa/password-protected-keystore/src/test/java/org/elasticsearch/password_protected_keystore/ReloadSecureSettingsWithPasswordProtectedKeystoreRestIT.java +++ b/x-pack/qa/password-protected-keystore/src/test/java/org/elasticsearch/password_protected_keystore/ReloadSecureSettingsWithPasswordProtectedKeystoreRestIT.java @@ -11,8 +11,10 @@ import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; -import org.elasticsearch.xcontent.ObjectPath; import org.elasticsearch.test.rest.ESRestTestCase; +import org.elasticsearch.xcontent.ObjectPath; + +import java.util.Map; import static org.hamcrest.Matchers.anyOf; import static org.hamcrest.Matchers.containsString; @@ -20,8 +22,6 @@ import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.nullValue; -import java.util.Map; - public class ReloadSecureSettingsWithPasswordProtectedKeystoreRestIT extends ESRestTestCase { // From build.gradle private final String KEYSTORE_PASSWORD = "keystore-password"; @@ -58,9 +58,10 @@ public void testReloadSecureSettingsWithIncorrectPassword() throws Exception { assertThat(entry.getValue(), instanceOf(Map.class)); final Map node = (Map) entry.getValue(); assertThat(node.get("reload_exception"), instanceOf(Map.class)); - assertThat(ObjectPath.eval("reload_exception.reason", node), anyOf( - equalTo("Provided keystore password was incorrect"), - equalTo("Keystore has been corrupted or tampered with"))); + assertThat( + ObjectPath.eval("reload_exception.reason", node), + anyOf(equalTo("Provided keystore password was incorrect"), equalTo("Keystore has been corrupted or tampered with")) + ); assertThat(ObjectPath.eval("reload_exception.type", node), equalTo("security_exception")); } } @@ -78,12 +79,16 @@ public void testReloadSecureSettingsWithEmptyPassword() throws Exception { assertThat(entry.getValue(), instanceOf(Map.class)); final Map node = (Map) entry.getValue(); assertThat(node.get("reload_exception"), instanceOf(Map.class)); - assertThat(ObjectPath.eval("reload_exception.reason", node), anyOf( - equalTo("Provided keystore password was incorrect"), - equalTo("Keystore has been corrupted or tampered with"), - containsString("Error generating an encryption key from the provided password") // FIPS - )); - assertThat(ObjectPath.eval("reload_exception.type", node), + assertThat( + ObjectPath.eval("reload_exception.reason", node), + anyOf( + equalTo("Provided keystore password was incorrect"), + equalTo("Keystore has been corrupted or tampered with"), + containsString("Error generating an encryption key from the provided password") // FIPS + ) + ); + assertThat( + ObjectPath.eval("reload_exception.type", node), // Depends on exact security provider (eg Sun vs BCFIPS) anyOf(equalTo("security_exception"), equalTo("general_security_exception")) ); @@ -93,16 +98,12 @@ public void testReloadSecureSettingsWithEmptyPassword() throws Exception { @Override protected Settings restClientSettings() { String token = basicAuthHeaderValue("test-user", new SecureString("test-user-password".toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } @Override protected Settings restAdminSettings() { String token = basicAuthHeaderValue("admin_user", new SecureString("admin-password".toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } } diff --git a/x-pack/qa/reindex-tests-with-security/src/test/java/org/elasticsearch/xpack/security/ReindexWithSecurityClientYamlTestSuiteIT.java b/x-pack/qa/reindex-tests-with-security/src/test/java/org/elasticsearch/xpack/security/ReindexWithSecurityClientYamlTestSuiteIT.java index 5a5d852267f00..2d59be775453f 100644 --- a/x-pack/qa/reindex-tests-with-security/src/test/java/org/elasticsearch/xpack/security/ReindexWithSecurityClientYamlTestSuiteIT.java +++ b/x-pack/qa/reindex-tests-with-security/src/test/java/org/elasticsearch/xpack/security/ReindexWithSecurityClientYamlTestSuiteIT.java @@ -8,10 +8,11 @@ import com.carrotsearch.randomizedtesting.annotations.Name; import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; -import org.elasticsearch.core.PathUtils; + import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.PathUtils; import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate; import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase; import org.junit.AfterClass; @@ -37,7 +38,7 @@ public static Iterable parameters() throws Exception { } @BeforeClass - public static void findTrustedCaCertificate( ) throws Exception { + public static void findTrustedCaCertificate() throws Exception { final URL resource = ReindexWithSecurityClientYamlTestSuiteIT.class.getResource("/ssl/ca.crt"); if (resource == null) { throw new FileNotFoundException("Cannot find classpath resource /ssl/ca.crt"); @@ -62,9 +63,8 @@ protected String getProtocol() { protected Settings restClientSettings() { String token = basicAuthHeaderValue(USER, new SecureString(PASS.toCharArray())); return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .put(CERTIFICATE_AUTHORITIES , httpCertificateAuthority) - .build(); + .put(ThreadContext.PREFIX + ".Authorization", token) + .put(CERTIFICATE_AUTHORITIES, httpCertificateAuthority) + .build(); } } - diff --git a/x-pack/qa/reindex-tests-with-security/src/test/java/org/elasticsearch/xpack/security/ReindexWithSecurityIT.java b/x-pack/qa/reindex-tests-with-security/src/test/java/org/elasticsearch/xpack/security/ReindexWithSecurityIT.java index 7784210e48147..722f39949a416 100644 --- a/x-pack/qa/reindex-tests-with-security/src/test/java/org/elasticsearch/xpack/security/ReindexWithSecurityIT.java +++ b/x-pack/qa/reindex-tests-with-security/src/test/java/org/elasticsearch/xpack/security/ReindexWithSecurityIT.java @@ -14,10 +14,10 @@ import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; -import org.elasticsearch.core.PathUtils; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.PathUtils; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.reindex.BulkByScrollResponse; import org.elasticsearch.index.reindex.DeleteByQueryRequest; @@ -36,7 +36,6 @@ import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.is; - @SuppressWarnings("removal") public class ReindexWithSecurityIT extends ESRestTestCase { @@ -46,7 +45,7 @@ public class ReindexWithSecurityIT extends ESRestTestCase { private static Path httpCertificateAuthority; @BeforeClass - public static void findTrustStore( ) throws Exception { + public static void findTrustStore() throws Exception { final URL resource = ReindexWithSecurityClientYamlTestSuiteIT.class.getResource("/ssl/ca.crt"); if (resource == null) { throw new FileNotFoundException("Cannot find classpath resource /ssl/ca.crt"); @@ -72,7 +71,7 @@ protected Settings restClientSettings() { String token = basicAuthHeaderValue(USER, new SecureString(PASS.toCharArray())); return Settings.builder() .put(ThreadContext.PREFIX + ".Authorization", token) - .put(CERTIFICATE_AUTHORITIES , httpCertificateAuthority) + .put(CERTIFICATE_AUTHORITIES, httpCertificateAuthority) .build(); } @@ -80,20 +79,25 @@ public void testDeleteByQuery() throws IOException { createIndicesWithRandomAliases("test1", "test2", "test3"); RestHighLevelClient restClient = new TestRestHighLevelClient(); - BulkByScrollResponse response = restClient.deleteByQuery(new DeleteByQueryRequest() - .setQuery(QueryBuilders.matchAllQuery()) - .indices("test1", "test2"), RequestOptions.DEFAULT); + BulkByScrollResponse response = restClient.deleteByQuery( + new DeleteByQueryRequest().setQuery(QueryBuilders.matchAllQuery()).indices("test1", "test2"), + RequestOptions.DEFAULT + ); assertNotNull(response); - response = restClient.deleteByQuery(new DeleteByQueryRequest() - .setQuery(QueryBuilders.matchAllQuery()) - .indices("test*"), RequestOptions.DEFAULT); + response = restClient.deleteByQuery( + new DeleteByQueryRequest().setQuery(QueryBuilders.matchAllQuery()).indices("test*"), + RequestOptions.DEFAULT + ); assertNotNull(response); - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> restClient.deleteByQuery(new DeleteByQueryRequest() - .setQuery(QueryBuilders.matchAllQuery()) - .indices("test1", "index1"), RequestOptions.DEFAULT)); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> restClient.deleteByQuery( + new DeleteByQueryRequest().setQuery(QueryBuilders.matchAllQuery()).indices("test1", "index1"), + RequestOptions.DEFAULT + ) + ); assertThat(e.getMessage(), containsString("no such index [index1]")); } @@ -101,16 +105,22 @@ public void testUpdateByQuery() throws IOException { createIndicesWithRandomAliases("test1", "test2", "test3"); RestHighLevelClient restClient = new TestRestHighLevelClient(); - BulkByScrollResponse response = - restClient.updateByQuery((UpdateByQueryRequest) new UpdateByQueryRequest().indices("test1", "test2"), RequestOptions.DEFAULT); + BulkByScrollResponse response = restClient.updateByQuery( + (UpdateByQueryRequest) new UpdateByQueryRequest().indices("test1", "test2"), + RequestOptions.DEFAULT + ); assertNotNull(response); response = restClient.updateByQuery((UpdateByQueryRequest) new UpdateByQueryRequest().indices("test*"), RequestOptions.DEFAULT); assertNotNull(response); - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> restClient.updateByQuery((UpdateByQueryRequest) new UpdateByQueryRequest().indices("test1", "index1"), - RequestOptions.DEFAULT)); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> restClient.updateByQuery( + (UpdateByQueryRequest) new UpdateByQueryRequest().indices("test1", "index1"), + RequestOptions.DEFAULT + ) + ); assertThat(e.getMessage(), containsString("no such index [index1]")); } @@ -118,17 +128,19 @@ public void testReindex() throws IOException { createIndicesWithRandomAliases("test1", "test2", "test3", "dest"); RestHighLevelClient restClient = new TestRestHighLevelClient(); - BulkByScrollResponse response = restClient.reindex(new ReindexRequest().setSourceIndices("test1", "test2").setDestIndex("dest"), - RequestOptions.DEFAULT); + BulkByScrollResponse response = restClient.reindex( + new ReindexRequest().setSourceIndices("test1", "test2").setDestIndex("dest"), + RequestOptions.DEFAULT + ); assertNotNull(response); - response = restClient.reindex(new ReindexRequest().setSourceIndices("test*").setDestIndex("dest"), - RequestOptions.DEFAULT); + response = restClient.reindex(new ReindexRequest().setSourceIndices("test*").setDestIndex("dest"), RequestOptions.DEFAULT); assertNotNull(response); - ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, - () -> restClient.reindex(new ReindexRequest().setSourceIndices("test1", "index1").setDestIndex("dest"), - RequestOptions.DEFAULT)); + ElasticsearchStatusException e = expectThrows( + ElasticsearchStatusException.class, + () -> restClient.reindex(new ReindexRequest().setSourceIndices("test1", "index1").setDestIndex("dest"), RequestOptions.DEFAULT) + ); assertThat(e.getMessage(), containsString("no such index [index1]")); } @@ -148,7 +160,7 @@ private void createIndicesWithRandomAliases(String... indices) throws IOExceptio IndicesAliasesRequest request = new IndicesAliasesRequest(); for (String index : indices) { if (frequently()) { - //one alias per index with prefix "alias-" + // one alias per index with prefix "alias-" request.addAliasAction(AliasActions.add().index(index).alias("alias-" + index)); aliasAdded = true; } @@ -156,7 +168,7 @@ private void createIndicesWithRandomAliases(String... indices) throws IOExceptio // If we get to this point and we haven't added an alias to the request we need to add one // or the request will fail so use noAliasAdded to force adding the alias in this case if (aliasAdded == false || randomBoolean()) { - //one alias pointing to all indices + // one alias pointing to all indices for (String index : indices) { request.addAliasAction(AliasActions.add().index(index).alias("alias")); } diff --git a/x-pack/qa/rolling-upgrade-multi-cluster/src/test/java/org/elasticsearch/upgrades/AbstractMultiClusterUpgradeTestCase.java b/x-pack/qa/rolling-upgrade-multi-cluster/src/test/java/org/elasticsearch/upgrades/AbstractMultiClusterUpgradeTestCase.java index 63a27afd64743..a0cb9ab354892 100644 --- a/x-pack/qa/rolling-upgrade-multi-cluster/src/test/java/org/elasticsearch/upgrades/AbstractMultiClusterUpgradeTestCase.java +++ b/x-pack/qa/rolling-upgrade-multi-cluster/src/test/java/org/elasticsearch/upgrades/AbstractMultiClusterUpgradeTestCase.java @@ -13,9 +13,9 @@ import org.elasticsearch.client.Response; import org.elasticsearch.client.RestClient; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.xcontent.json.JsonXContent; import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.test.rest.ESRestTestCase; +import org.elasticsearch.xcontent.json.JsonXContent; import org.junit.AfterClass; import org.junit.Before; @@ -73,8 +73,7 @@ public static ClusterName parse(String value) { protected final ClusterName clusterName = ClusterName.parse(System.getProperty("tests.rest.cluster_name")); - protected static final Version UPGRADE_FROM_VERSION = - Version.fromString(System.getProperty("tests.upgrade_from_version")); + protected static final Version UPGRADE_FROM_VERSION = Version.fromString(System.getProperty("tests.upgrade_from_version")); private static RestClient leaderClient; private static RestClient followerClient; @@ -162,9 +161,12 @@ protected static RestClient followerClient() { private RestClient buildClient(final String url) throws IOException { int portSeparator = url.lastIndexOf(':'); - HttpHost httpHost = new HttpHost(url.substring(0, portSeparator), - Integer.parseInt(url.substring(portSeparator + 1)), getProtocol()); - return buildClient(restAdminSettings(), new HttpHost[]{httpHost}); + HttpHost httpHost = new HttpHost( + url.substring(0, portSeparator), + Integer.parseInt(url.substring(portSeparator + 1)), + getProtocol() + ); + return buildClient(restAdminSettings(), new HttpHost[] { httpHost }); } protected static Map toMap(Response response) throws IOException { diff --git a/x-pack/qa/rolling-upgrade-multi-cluster/src/test/java/org/elasticsearch/upgrades/CcrRollingUpgradeIT.java b/x-pack/qa/rolling-upgrade-multi-cluster/src/test/java/org/elasticsearch/upgrades/CcrRollingUpgradeIT.java index 97b0c5bb339b2..df7d4b9d70323 100644 --- a/x-pack/qa/rolling-upgrade-multi-cluster/src/test/java/org/elasticsearch/upgrades/CcrRollingUpgradeIT.java +++ b/x-pack/qa/rolling-upgrade-multi-cluster/src/test/java/org/elasticsearch/upgrades/CcrRollingUpgradeIT.java @@ -12,8 +12,8 @@ import org.elasticsearch.client.RestClient; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.xcontent.ObjectPath; import org.elasticsearch.common.xcontent.support.XContentMapValues; +import org.elasticsearch.xcontent.ObjectPath; import java.io.IOException; import java.util.Map; @@ -146,8 +146,8 @@ public void testAutoFollowing() throws Exception { String followerIndex = "copy-" + leaderIndex1; assertTotalHitCount(followerIndex, 128, followerClient()); }); - // Auto follow stats are kept in-memory on master elected node - // and if this node get updated then auto follow stats are reset + // Auto follow stats are kept in-memory on master elected node + // and if this node get updated then auto follow stats are reset { int previousNumberOfSuccessfulFollowedIndices = getNumberOfSuccessfulFollowedIndices(); createLeaderIndex(leaderClient(), leaderIndex2); @@ -158,7 +158,7 @@ public void testAutoFollowing() throws Exception { assertTotalHitCount(followerIndex, 64, followerClient()); }); } - break; + break; case TWO_THIRD: index(leaderClient(), leaderIndex1, 64); assertBusy(() -> { @@ -171,8 +171,8 @@ public void testAutoFollowing() throws Exception { assertTotalHitCount(followerIndex, 128, followerClient()); }); - // Auto follow stats are kept in-memory on master elected node - // and if this node get updated then auto follow stats are reset + // Auto follow stats are kept in-memory on master elected node + // and if this node get updated then auto follow stats are reset { int previousNumberOfSuccessfulFollowedIndices = getNumberOfSuccessfulFollowedIndices(); createLeaderIndex(leaderClient(), leaderIndex3); @@ -183,7 +183,7 @@ public void testAutoFollowing() throws Exception { assertTotalHitCount(followerIndex, 64, followerClient()); }); } - break; + break; case ALL: index(leaderClient(), leaderIndex1, 64); assertBusy(() -> { @@ -225,8 +225,10 @@ public void testCannotFollowLeaderInUpgradedCluster() throws Exception { createLeaderIndex(followerClient(), "not_supported"); index(followerClient(), "not_supported", 64); - ResponseException e = expectThrows(ResponseException.class, - () -> followIndex(leaderClient(), "follower", "not_supported", "not_supported")); + ResponseException e = expectThrows( + ResponseException.class, + () -> followIndex(leaderClient(), "follower", "not_supported", "not_supported") + ); assertThat(e.getMessage(), containsString("the snapshot was created with Elasticsearch version [")); assertThat(e.getMessage(), containsString("] which is higher than the version of this node [")); } else if (clusterName == ClusterName.LEADER) { @@ -292,15 +294,13 @@ public void testBiDirectionalIndexFollowing() throws Exception { default: throw new AssertionError("unexpected upgrade_state [" + upgradeState + "]"); } - } else { + } else { throw new AssertionError("unexpected cluster_name [" + clusterName + "]"); } } private static void createLeaderIndex(RestClient client, String indexName) throws IOException { - Settings.Builder indexSettings = Settings.builder() - .put("index.number_of_shards", 1) - .put("index.number_of_replicas", 0); + Settings.Builder indexSettings = Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0); if (randomBoolean()) { indexSettings.put("index.soft_deletes.enabled", true); } @@ -315,16 +315,22 @@ private static void createIndex(RestClient client, String name, Settings setting private static void followIndex(RestClient client, String leaderCluster, String leaderIndex, String followIndex) throws IOException { final Request request = new Request("PUT", "/" + followIndex + "/_ccr/follow?wait_for_active_shards=1"); - request.setJsonEntity("{\"remote_cluster\": \"" + leaderCluster + "\", \"leader_index\": \"" + leaderIndex + - "\", \"read_poll_timeout\": \"10ms\"}"); + request.setJsonEntity( + "{\"remote_cluster\": \"" + leaderCluster + "\", \"leader_index\": \"" + leaderIndex + "\", \"read_poll_timeout\": \"10ms\"}" + ); assertOK(client.performRequest(request)); } private static void putAutoFollowPattern(RestClient client, String name, String remoteCluster, String pattern) throws IOException { Request request = new Request("PUT", "/_ccr/auto_follow/" + name); - request.setJsonEntity("{\"leader_index_patterns\": [\"" + pattern + "\"], \"remote_cluster\": \"" + - remoteCluster + "\"," + - "\"follow_index_pattern\": \"copy-{{leader_index}}\", \"read_poll_timeout\": \"10ms\"}"); + request.setJsonEntity( + "{\"leader_index_patterns\": [\"" + + pattern + + "\"], \"remote_cluster\": \"" + + remoteCluster + + "\"," + + "\"follow_index_pattern\": \"copy-{{leader_index}}\", \"read_poll_timeout\": \"10ms\"}" + ); assertOK(client.performRequest(request)); } @@ -355,16 +361,12 @@ private static void index(RestClient client, String index, int numDocs) throws I } } - private static void assertTotalHitCount(final String index, - final int expectedTotalHits, - final RestClient client) throws Exception { + private static void assertTotalHitCount(final String index, final int expectedTotalHits, final RestClient client) throws Exception { assertOK(client.performRequest(new Request("POST", "/" + index + "/_refresh"))); assertBusy(() -> verifyTotalHitCount(index, expectedTotalHits, client)); } - private static void verifyTotalHitCount(final String index, - final int expectedTotalHits, - final RestClient client) throws IOException { + private static void verifyTotalHitCount(final String index, final int expectedTotalHits, final RestClient client) throws IOException { final Request request = new Request("GET", "/" + index + "/_search"); request.addParameter(TOTAL_HITS_AS_INT_PARAM, "true"); Map response = toMap(client.performRequest(request)); diff --git a/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/AbstractUpgradeTestCase.java b/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/AbstractUpgradeTestCase.java index 568a03a3d091b..5d513b489394e 100644 --- a/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/AbstractUpgradeTestCase.java +++ b/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/AbstractUpgradeTestCase.java @@ -24,11 +24,12 @@ public abstract class AbstractUpgradeTestCase extends ESRestTestCase { - private static final String BASIC_AUTH_VALUE = - basicAuthHeaderValue("test_user", new SecureString(SecuritySettingsSourceField.TEST_PASSWORD)); + private static final String BASIC_AUTH_VALUE = basicAuthHeaderValue( + "test_user", + new SecureString(SecuritySettingsSourceField.TEST_PASSWORD) + ); - protected static final Version UPGRADE_FROM_VERSION = - Version.fromString(System.getProperty("tests.upgrade_from_version")); + protected static final Version UPGRADE_FROM_VERSION = Version.fromString(System.getProperty("tests.upgrade_from_version")); @Override protected boolean preserveIndicesUponCompletion() { @@ -94,14 +95,14 @@ public static ClusterType parse(String value) { @Override protected Settings restClientSettings() { return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", BASIC_AUTH_VALUE) + .put(ThreadContext.PREFIX + ".Authorization", BASIC_AUTH_VALUE) - // increase the timeout here to 90 seconds to handle long waits for a green - // cluster health. the waits for green need to be longer than a minute to - // account for delayed shards - .put(ESRestTestCase.CLIENT_SOCKET_TIMEOUT, "90s") + // increase the timeout here to 90 seconds to handle long waits for a green + // cluster health. the waits for green need to be longer than a minute to + // account for delayed shards + .put(ESRestTestCase.CLIENT_SOCKET_TIMEOUT, "90s") - .build(); + .build(); } protected Collection templatesToWaitFor() { diff --git a/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/DataStreamsUpgradeIT.java b/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/DataStreamsUpgradeIT.java index f1682bcf021cf..857acd334d9a1 100644 --- a/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/DataStreamsUpgradeIT.java +++ b/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/DataStreamsUpgradeIT.java @@ -10,8 +10,8 @@ import org.elasticsearch.Version; import org.elasticsearch.client.Request; import org.elasticsearch.client.Response; -import org.elasticsearch.cluster.metadata.DataStreamTestHelper; import org.elasticsearch.cluster.metadata.DataStream; +import org.elasticsearch.cluster.metadata.DataStreamTestHelper; import org.elasticsearch.core.Booleans; import org.hamcrest.Matchers; @@ -26,20 +26,20 @@ public class DataStreamsUpgradeIT extends AbstractUpgradeTestCase { public void testDataStreams() throws IOException { assumeTrue("no data streams in versions before " + Version.V_7_9_0, UPGRADE_FROM_VERSION.onOrAfter(Version.V_7_9_0)); if (CLUSTER_TYPE == ClusterType.OLD) { - String requestBody = "{\n" + - " \"index_patterns\":[\"logs-*\"],\n" + - " \"template\": {\n" + - " \"mappings\": {\n" + - " \"properties\": {\n" + - " \"@timestamp\": {\n" + - " \"type\": \"date\"\n" + - " }\n" + - " }\n" + - " }\n" + - " },\n" + - " \"data_stream\":{\n" + - " }\n" + - " }"; + String requestBody = "{\n" + + " \"index_patterns\":[\"logs-*\"],\n" + + " \"template\": {\n" + + " \"mappings\": {\n" + + " \"properties\": {\n" + + " \"@timestamp\": {\n" + + " \"type\": \"date\"\n" + + " }\n" + + " }\n" + + " }\n" + + " },\n" + + " \"data_stream\":{\n" + + " }\n" + + " }"; Request request = new Request("PUT", "/_index_template/1"); request.setJsonEntity(requestBody); useIgnoreMultipleMatchingTemplatesWarningsHandler(request); @@ -109,20 +109,20 @@ public void testDataStreams() throws IOException { public void testDataStreamValidationDoesNotBreakUpgrade() throws Exception { assumeTrue("Bug started to occur from version: " + Version.V_7_10_2, UPGRADE_FROM_VERSION.onOrAfter(Version.V_7_10_2)); if (CLUSTER_TYPE == ClusterType.OLD) { - String requestBody = "{\n" + - " \"index_patterns\":[\"logs-*\"],\n" + - " \"template\": {\n" + - " \"mappings\": {\n" + - " \"properties\": {\n" + - " \"@timestamp\": {\n" + - " \"type\": \"date\"\n" + - " }\n" + - " }\n" + - " }\n" + - " },\n" + - " \"data_stream\":{\n" + - " }\n" + - " }"; + String requestBody = "{\n" + + " \"index_patterns\":[\"logs-*\"],\n" + + " \"template\": {\n" + + " \"mappings\": {\n" + + " \"properties\": {\n" + + " \"@timestamp\": {\n" + + " \"type\": \"date\"\n" + + " }\n" + + " }\n" + + " }\n" + + " },\n" + + " \"data_stream\":{\n" + + " }\n" + + " }"; Request request = new Request("PUT", "/_index_template/1"); request.setJsonEntity(requestBody); useIgnoreMultipleMatchingTemplatesWarningsHandler(request); diff --git a/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/IndexingIT.java b/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/IndexingIT.java index 995176286f700..a0caf168aab42 100644 --- a/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/IndexingIT.java +++ b/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/IndexingIT.java @@ -26,25 +26,25 @@ public class IndexingIT extends AbstractUpgradeTestCase { public void testIndexing() throws IOException { switch (CLUSTER_TYPE) { - case OLD: - break; - case MIXED: - ensureHealth((request -> { - request.addParameter("timeout", "70s"); - request.addParameter("wait_for_nodes", "3"); - request.addParameter("wait_for_status", "yellow"); - })); - break; - case UPGRADED: - ensureHealth("test_index,index_with_replicas,empty_index", (request -> { - request.addParameter("wait_for_nodes", "3"); - request.addParameter("wait_for_status", "green"); - request.addParameter("timeout", "70s"); - request.addParameter("level", "shards"); - })); - break; - default: - throw new UnsupportedOperationException("Unknown cluster type [" + CLUSTER_TYPE + "]"); + case OLD: + break; + case MIXED: + ensureHealth((request -> { + request.addParameter("timeout", "70s"); + request.addParameter("wait_for_nodes", "3"); + request.addParameter("wait_for_status", "yellow"); + })); + break; + case UPGRADED: + ensureHealth("test_index,index_with_replicas,empty_index", (request -> { + request.addParameter("wait_for_nodes", "3"); + request.addParameter("wait_for_status", "green"); + request.addParameter("timeout", "70s"); + request.addParameter("level", "shards"); + })); + break; + default: + throw new UnsupportedOperationException("Unknown cluster type [" + CLUSTER_TYPE + "]"); } if (CLUSTER_TYPE == ClusterType.OLD) { @@ -69,21 +69,21 @@ public void testIndexing() throws IOException { int expectedCount; switch (CLUSTER_TYPE) { - case OLD: - expectedCount = 5; - break; - case MIXED: - if (Booleans.parseBoolean(System.getProperty("tests.first_round"))) { + case OLD: expectedCount = 5; - } else { - expectedCount = 10; - } - break; - case UPGRADED: - expectedCount = 15; - break; - default: - throw new UnsupportedOperationException("Unknown cluster type [" + CLUSTER_TYPE + "]"); + break; + case MIXED: + if (Booleans.parseBoolean(System.getProperty("tests.first_round"))) { + expectedCount = 5; + } else { + expectedCount = 10; + } + break; + case UPGRADED: + expectedCount = 15; + break; + default: + throw new UnsupportedOperationException("Unknown cluster type [" + CLUSTER_TYPE + "]"); } assertCount("test_index", expectedCount); @@ -123,7 +123,9 @@ static void assertCount(String index, int count) throws IOException { searchTestIndexRequest.addParameter(TOTAL_HITS_AS_INT_PARAM, "true"); searchTestIndexRequest.addParameter("filter_path", "hits.total"); Response searchTestIndexResponse = client().performRequest(searchTestIndexRequest); - assertEquals("{\"hits\":{\"total\":" + count + "}}", - EntityUtils.toString(searchTestIndexResponse.getEntity(), StandardCharsets.UTF_8)); + assertEquals( + "{\"hits\":{\"total\":" + count + "}}", + EntityUtils.toString(searchTestIndexResponse.getEntity(), StandardCharsets.UTF_8) + ); } } diff --git a/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/MlJobSnapshotUpgradeIT.java b/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/MlJobSnapshotUpgradeIT.java index f6f3610b38fe9..5aea15589eecb 100644 --- a/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/MlJobSnapshotUpgradeIT.java +++ b/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/MlJobSnapshotUpgradeIT.java @@ -79,8 +79,7 @@ protected Collection templatesToWaitFor() { List templatesToWaitFor = UPGRADE_FROM_VERSION.onOrAfter(Version.V_7_12_0) ? XPackRestTestConstants.ML_POST_V7120_TEMPLATES : XPackRestTestConstants.ML_POST_V660_TEMPLATES; - return Stream.concat(templatesToWaitFor.stream(), - super.templatesToWaitFor().stream()).collect(Collectors.toSet()); + return Stream.concat(templatesToWaitFor.stream(), super.templatesToWaitFor().stream()).collect(Collectors.toSet()); } protected static void waitForPendingUpgraderTasks() throws Exception { @@ -94,10 +93,7 @@ protected static void waitForPendingUpgraderTasks() throws Exception { public void testSnapshotUpgrader() throws Exception { hlrc = new HLRC(client()).machineLearning(); Request adjustLoggingLevels = new Request("PUT", "/_cluster/settings"); - adjustLoggingLevels.setJsonEntity( - "{\"persistent\": {" + - "\"logger.org.elasticsearch.xpack.ml\": \"trace\"" + - "}}"); + adjustLoggingLevels.setJsonEntity("{\"persistent\": {" + "\"logger.org.elasticsearch.xpack.ml\": \"trace\"" + "}}"); client().performRequest(adjustLoggingLevels); switch (CLUSTER_TYPE) { case OLD: @@ -139,10 +135,14 @@ private void testSnapshotUpgradeFailsOnMixedCluster() throws Exception { .findFirst() .orElseThrow(() -> new ElasticsearchException("Not found snapshot other than " + currentSnapshot)); - Exception ex = expectThrows(Exception.class, () -> hlrc.upgradeJobSnapshot( - new UpgradeJobModelSnapshotRequest(JOB_ID, snapshot.getSnapshotId(), null, true), - RequestOptions.DEFAULT)); - assertThat(ex.getMessage(), containsString("All nodes must be the same version")); + Exception ex = expectThrows( + Exception.class, + () -> hlrc.upgradeJobSnapshot( + new UpgradeJobModelSnapshotRequest(JOB_ID, snapshot.getSnapshotId(), null, true), + RequestOptions.DEFAULT + ) + ); + assertThat(ex.getMessage(), containsString("All nodes must be the same version")); } private void testSnapshotUpgrade() throws Exception { @@ -151,8 +151,8 @@ private void testSnapshotUpgrade() throws Exception { GetModelSnapshotsResponse modelSnapshots = getModelSnapshots(job.getId()); assertThat(modelSnapshots.snapshots(), hasSize(2)); - assertThat(modelSnapshots.snapshots().get(0).getMinVersion().major, equalTo((byte)7)); - assertThat(modelSnapshots.snapshots().get(1).getMinVersion().major, equalTo((byte)7)); + assertThat(modelSnapshots.snapshots().get(0).getMinVersion().major, equalTo((byte) 7)); + assertThat(modelSnapshots.snapshots().get(1).getMinVersion().major, equalTo((byte) 7)); ModelSnapshot snapshot = modelSnapshots.snapshots() .stream() @@ -160,9 +160,13 @@ private void testSnapshotUpgrade() throws Exception { .findFirst() .orElseThrow(() -> new ElasticsearchException("Not found snapshot other than " + currentSnapshot)); - assertThat(hlrc.upgradeJobSnapshot( - new UpgradeJobModelSnapshotRequest(JOB_ID, snapshot.getSnapshotId(), null, true), - RequestOptions.DEFAULT).isCompleted(), is(true)); + assertThat( + hlrc.upgradeJobSnapshot( + new UpgradeJobModelSnapshotRequest(JOB_ID, snapshot.getSnapshotId(), null, true), + RequestOptions.DEFAULT + ).isCompleted(), + is(true) + ); List snapshots = getModelSnapshots(job.getId(), snapshot.getSnapshotId()).snapshots(); assertThat(snapshots, hasSize(1)); @@ -170,21 +174,29 @@ private void testSnapshotUpgrade() throws Exception { assertThat(snapshot.getLatestRecordTimeStamp(), equalTo(snapshots.get(0).getLatestRecordTimeStamp())); // Does the snapshot still work? - assertThat(hlrc.getJobStats(new GetJobStatsRequest(JOB_ID), RequestOptions.DEFAULT) + assertThat( + hlrc.getJobStats(new GetJobStatsRequest(JOB_ID), RequestOptions.DEFAULT) .jobStats() .get(0) - .getDataCounts().getLatestRecordTimeStamp(), - greaterThan(snapshot.getLatestRecordTimeStamp())); + .getDataCounts() + .getLatestRecordTimeStamp(), + greaterThan(snapshot.getLatestRecordTimeStamp()) + ); RevertModelSnapshotRequest revertModelSnapshotRequest = new RevertModelSnapshotRequest(JOB_ID, snapshot.getSnapshotId()); revertModelSnapshotRequest.setDeleteInterveningResults(true); - assertThat(hlrc.revertModelSnapshot(revertModelSnapshotRequest, RequestOptions.DEFAULT).getModel().getSnapshotId(), - equalTo(snapshot.getSnapshotId())); + assertThat( + hlrc.revertModelSnapshot(revertModelSnapshotRequest, RequestOptions.DEFAULT).getModel().getSnapshotId(), + equalTo(snapshot.getSnapshotId()) + ); assertThat(openJob(JOB_ID).isOpened(), is(true)); - assertThat(hlrc.getJobStats(new GetJobStatsRequest(JOB_ID), RequestOptions.DEFAULT) + assertThat( + hlrc.getJobStats(new GetJobStatsRequest(JOB_ID), RequestOptions.DEFAULT) .jobStats() .get(0) - .getDataCounts().getLatestRecordTimeStamp(), - equalTo(snapshot.getLatestRecordTimeStamp())); + .getDataCounts() + .getLatestRecordTimeStamp(), + equalTo(snapshot.getLatestRecordTimeStamp()) + ); closeJob(JOB_ID); } @@ -195,13 +207,11 @@ private void createJobAndSnapshots() throws Exception { PutJobResponse jobResponse = buildAndPutJob(JOB_ID, bucketSpan); Job job = jobResponse.getResponse(); openJob(job.getId()); - DataCounts dataCounts = postData(job.getId(), - generateData(startTime, - bucketSpan, - 10, - Arrays.asList("foo"), - (bucketIndex, series) -> bucketIndex == 5 ? 100.0 : 10.0).stream().collect(Collectors.joining())) - .getDataCounts(); + DataCounts dataCounts = postData( + job.getId(), + generateData(startTime, bucketSpan, 10, Arrays.asList("foo"), (bucketIndex, series) -> bucketIndex == 5 ? 100.0 : 10.0).stream() + .collect(Collectors.joining()) + ).getDataCounts(); assertThat(dataCounts.getInvalidDateCount(), equalTo(0L)); assertThat(dataCounts.getBucketCount(), greaterThan(0L)); final long lastCount = dataCounts.getBucketCount(); @@ -212,14 +222,12 @@ private void createJobAndSnapshots() throws Exception { waitUntil(() -> false, 2, TimeUnit.SECONDS); openJob(job.getId()); - dataCounts = postData(job.getId(), - generateData( - startTime + 10 * bucketSpan.getMillis(), - bucketSpan, - 10, - Arrays.asList("foo"), - (bucketIndex, series) -> 10.0).stream().collect(Collectors.joining())) - .getDataCounts(); + dataCounts = postData( + job.getId(), + generateData(startTime + 10 * bucketSpan.getMillis(), bucketSpan, 10, Arrays.asList("foo"), (bucketIndex, series) -> 10.0) + .stream() + .collect(Collectors.joining()) + ).getDataCounts(); assertThat(dataCounts.getInvalidDateCount(), equalTo(0L)); assertThat(dataCounts.getBucketCount(), greaterThan(lastCount)); flushJob(job.getId()); @@ -227,8 +235,8 @@ private void createJobAndSnapshots() throws Exception { GetModelSnapshotsResponse modelSnapshots = getModelSnapshots(job.getId()); assertThat(modelSnapshots.snapshots(), hasSize(2)); - assertThat(modelSnapshots.snapshots().get(0).getMinVersion().major, equalTo((byte)7)); - assertThat(modelSnapshots.snapshots().get(1).getMinVersion().major, equalTo((byte)7)); + assertThat(modelSnapshots.snapshots().get(0).getMinVersion().major, equalTo((byte) 7)); + assertThat(modelSnapshots.snapshots().get(1).getMinVersion().major, equalTo((byte) 7)); } private PutJobResponse buildAndPutJob(String jobId, TimeValue bucketSpan) throws Exception { @@ -252,8 +260,13 @@ private PutJobResponse buildAndPutJob(String jobId, TimeValue bucketSpan) throws return putJob(job.build()); } - private static List generateData(long timestamp, TimeValue bucketSpan, int bucketCount, List series, - BiFunction timeAndSeriesToValueFunction) throws IOException { + private static List generateData( + long timestamp, + TimeValue bucketSpan, + int bucketCount, + List series, + BiFunction timeAndSeriesToValueFunction + ) throws IOException { List data = new ArrayList<>(); long now = timestamp; for (int i = 0; i < bucketCount; i++) { @@ -290,17 +303,19 @@ protected OpenJobResponse openJob(String jobId) throws IOException { protected PostDataResponse postData(String jobId, String data) throws IOException { // Post data is deprecated, so a deprecation warning is possible (depending on the old version) - RequestOptions postDataOptions = RequestOptions.DEFAULT.toBuilder() - .setWarningsHandler(warnings -> { - if (warnings.isEmpty()) { - // No warning is OK - it means we hit an old node where post data is not deprecated - return false; - } else if (warnings.size() > 1) { - return true; - } - return warnings.get(0).equals("Posting data directly to anomaly detection jobs is deprecated, " + - "in a future major version it will be compulsory to use a datafeed") == false; - }).build(); + RequestOptions postDataOptions = RequestOptions.DEFAULT.toBuilder().setWarningsHandler(warnings -> { + if (warnings.isEmpty()) { + // No warning is OK - it means we hit an old node where post data is not deprecated + return false; + } else if (warnings.size() > 1) { + return true; + } + return warnings.get(0) + .equals( + "Posting data directly to anomaly detection jobs is deprecated, " + + "in a future major version it will be compulsory to use a datafeed" + ) == false; + }).build(); return hlrc.postData(new PostDataRequest(jobId, XContentType.JSON, new BytesArray(data)), postDataOptions); } diff --git a/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/MlMappingsUpgradeIT.java b/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/MlMappingsUpgradeIT.java index c342395654036..d84dc80569439 100644 --- a/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/MlMappingsUpgradeIT.java +++ b/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/MlMappingsUpgradeIT.java @@ -38,8 +38,7 @@ protected Collection templatesToWaitFor() { List templatesToWaitFor = UPGRADE_FROM_VERSION.onOrAfter(Version.V_7_12_0) ? XPackRestTestConstants.ML_POST_V7120_TEMPLATES : XPackRestTestConstants.ML_POST_V660_TEMPLATES; - return Stream.concat(templatesToWaitFor.stream(), - super.templatesToWaitFor().stream()).collect(Collectors.toSet()); + return Stream.concat(templatesToWaitFor.stream(), super.templatesToWaitFor().stream()).collect(Collectors.toSet()); } /** @@ -127,8 +126,11 @@ private void assertUpgradedResultsMappings() throws Exception { // TODO: as the years go by, the field we assert on here should be changed // to the most recent field we've added that is NOT of type "keyword" - assertEquals("Incorrect type for peak_model_bytes in " + responseLevel, "long", - extractValue("mappings.properties.model_size_stats.properties.peak_model_bytes.type", indexLevel)); + assertEquals( + "Incorrect type for peak_model_bytes in " + responseLevel, + "long", + extractValue("mappings.properties.model_size_stats.properties.peak_model_bytes.type", indexLevel) + ); }); } @@ -158,8 +160,11 @@ private void assertUpgradedAnnotationsMappings() throws Exception { // TODO: as the years go by, the field we assert on here should be changed // to the most recent field we've added that would be incorrectly mapped by dynamic // mappings, for example a field we want to be "keyword" incorrectly mapped as "text" - assertEquals("Incorrect type for event in " + responseLevel, "keyword", - extractValue("mappings.properties.event.type", indexLevel)); + assertEquals( + "Incorrect type for event in " + responseLevel, + "keyword", + extractValue("mappings.properties.event.type", indexLevel) + ); }); } @@ -168,8 +173,12 @@ private void assertUpgradedConfigMappings() throws Exception { assertBusy(() -> { Request getMappings = new Request("GET", ".ml-config/_mappings"); - getMappings.setOptions(expectWarnings("this request accesses system indices: [.ml-config], but in a future major " + - "version, direct access to system indices will be prevented by default")); + getMappings.setOptions( + expectWarnings( + "this request accesses system indices: [.ml-config], but in a future major " + + "version, direct access to system indices will be prevented by default" + ) + ); Response response = client().performRequest(getMappings); Map responseLevel = entityAsMap(response); @@ -181,8 +190,11 @@ private void assertUpgradedConfigMappings() throws Exception { // TODO: as the years go by, the field we assert on here should be changed // to the most recent field we've added that is NOT of type "keyword" - assertEquals("Incorrect type for annotations_enabled in " + responseLevel, "boolean", - extractValue("mappings.properties.model_plot_config.properties.annotations_enabled.type", indexLevel)); + assertEquals( + "Incorrect type for annotations_enabled in " + responseLevel, + "boolean", + extractValue("mappings.properties.model_plot_config.properties.annotations_enabled.type", indexLevel) + ); }); } } diff --git a/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/RollupDateHistoUpgradeIT.java b/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/RollupDateHistoUpgradeIT.java index e957c3ea84d7a..d9d1395233416 100644 --- a/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/RollupDateHistoUpgradeIT.java +++ b/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/RollupDateHistoUpgradeIT.java @@ -9,11 +9,11 @@ import org.elasticsearch.Version; import org.elasticsearch.client.Request; import org.elasticsearch.client.Response; -import org.elasticsearch.core.Booleans; -import org.elasticsearch.xcontent.ObjectPath; import org.elasticsearch.common.xcontent.support.XContentMapValues; +import org.elasticsearch.core.Booleans; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.test.rest.ESRestTestCase; +import org.elasticsearch.xcontent.ObjectPath; import org.hamcrest.Matcher; import java.io.IOException; @@ -29,10 +29,8 @@ import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.equalTo; - public class RollupDateHistoUpgradeIT extends AbstractUpgradeTestCase { - private static final Version UPGRADE_FROM_VERSION = - Version.fromString(System.getProperty("tests.upgrade_from_version")); + private static final Version UPGRADE_FROM_VERSION = Version.fromString(System.getProperty("tests.upgrade_from_version")); public void testDateHistoIntervalUpgrade() throws Exception { assumeTrue("DateHisto interval changed in 7.2", UPGRADE_FROM_VERSION.before(Version.V_7_2_0)); @@ -73,28 +71,30 @@ public void testDateHistoIntervalUpgrade() throws Exception { // create the rollup job with an old interval style final Request createRollupJobRequest = new Request("PUT", "_rollup/job/rollup-id-test"); - createRollupJobRequest.setJsonEntity("{" - + "\"index_pattern\":\"target\"," - + "\"rollup_index\":\"rollup\"," - + "\"cron\":\"*/1 * * * * ?\"," - + "\"page_size\":100," - + "\"groups\":{" - + " \"date_histogram\":{" - + " \"field\":\"timestamp\"," - + " \"interval\":\"5m\"" - + " }," - + "\"histogram\":{" - + " \"fields\": [\"value\"]," - + " \"interval\":1" - + " }," - + "\"terms\":{" - + " \"fields\": [\"value\"]" - + " }" - + "}," - + "\"metrics\":[" - + " {\"field\":\"value\",\"metrics\":[\"min\",\"max\",\"sum\"]}" - + "]" - + "}"); + createRollupJobRequest.setJsonEntity( + "{" + + "\"index_pattern\":\"target\"," + + "\"rollup_index\":\"rollup\"," + + "\"cron\":\"*/1 * * * * ?\"," + + "\"page_size\":100," + + "\"groups\":{" + + " \"date_histogram\":{" + + " \"field\":\"timestamp\"," + + " \"interval\":\"5m\"" + + " }," + + "\"histogram\":{" + + " \"fields\": [\"value\"]," + + " \"interval\":1" + + " }," + + "\"terms\":{" + + " \"fields\": [\"value\"]" + + " }" + + "}," + + "\"metrics\":[" + + " {\"field\":\"value\",\"metrics\":[\"min\",\"max\",\"sum\"]}" + + "]" + + "}" + ); Map createRollupJobResponse = entityAsMap(client().performRequest(createRollupJobRequest)); assertThat(createRollupJobResponse.get("acknowledged"), equalTo(Boolean.TRUE)); @@ -122,8 +122,11 @@ public void testDateHistoIntervalUpgrade() throws Exception { client().performRequest(new Request("POST", "rollup/_refresh")); List ids = getSearchResults(2); - assertThat(ids.toString(), ids, containsInAnyOrder("rollup-id-test$AuaduUZW8tgWmFP87DgzSA", - "rollup-id-test$ehY4NAyVSy8xxUDZrNXXIA")); + assertThat( + ids.toString(), + ids, + containsInAnyOrder("rollup-id-test$AuaduUZW8tgWmFP87DgzSA", "rollup-id-test$ehY4NAyVSy8xxUDZrNXXIA") + ); } if (CLUSTER_TYPE == ClusterType.MIXED && Booleans.parseBoolean(System.getProperty("tests.first_round")) == false) { @@ -135,8 +138,15 @@ public void testDateHistoIntervalUpgrade() throws Exception { client().performRequest(new Request("POST", "rollup/_refresh")); List ids = getSearchResults(3); - assertThat(ids.toString(), ids, containsInAnyOrder("rollup-id-test$AuaduUZW8tgWmFP87DgzSA", - "rollup-id-test$ehY4NAyVSy8xxUDZrNXXIA", "rollup-id-test$60RGDSb92YI5LH4_Fnq_1g")); + assertThat( + ids.toString(), + ids, + containsInAnyOrder( + "rollup-id-test$AuaduUZW8tgWmFP87DgzSA", + "rollup-id-test$ehY4NAyVSy8xxUDZrNXXIA", + "rollup-id-test$60RGDSb92YI5LH4_Fnq_1g" + ) + ); } @@ -149,8 +159,16 @@ public void testDateHistoIntervalUpgrade() throws Exception { client().performRequest(new Request("POST", "rollup/_refresh")); List ids = getSearchResults(4); - assertThat(ids.toString(), ids, containsInAnyOrder("rollup-id-test$AuaduUZW8tgWmFP87DgzSA", - "rollup-id-test$ehY4NAyVSy8xxUDZrNXXIA", "rollup-id-test$60RGDSb92YI5LH4_Fnq_1g", "rollup-id-test$LAKZftDeQwsUtdPixrkkzQ")); + assertThat( + ids.toString(), + ids, + containsInAnyOrder( + "rollup-id-test$AuaduUZW8tgWmFP87DgzSA", + "rollup-id-test$ehY4NAyVSy8xxUDZrNXXIA", + "rollup-id-test$60RGDSb92YI5LH4_Fnq_1g", + "rollup-id-test$LAKZftDeQwsUtdPixrkkzQ" + ) + ); } } @@ -241,8 +259,7 @@ private static Map getJob(Response response, String targetJobId) @SuppressWarnings("unchecked") private static Map getJob(Map jobsMap, String targetJobId) throws IOException { - List> jobs = - (List>) XContentMapValues.extractValue("jobs", jobsMap); + List> jobs = (List>) XContentMapValues.extractValue("jobs", jobsMap); if (jobs == null) { return null; diff --git a/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/SearchableSnapshotsRollingUpgradeIT.java b/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/SearchableSnapshotsRollingUpgradeIT.java index d9c49f6232de2..00acf076c7e46 100644 --- a/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/SearchableSnapshotsRollingUpgradeIT.java +++ b/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/SearchableSnapshotsRollingUpgradeIT.java @@ -51,8 +51,10 @@ public void testMountPartialCopyAndRecoversCorrectly() throws Exception { if (CLUSTER_TYPE.equals(ClusterType.UPGRADED)) { assertBusy(() -> { Map settings = getIndexSettingsAsMap("mounted_index_shared_cache"); - assertThat(settings, - hasEntry(ShardLimitValidator.INDEX_SETTING_SHARD_LIMIT_GROUP.getKey(), ShardLimitValidator.FROZEN_GROUP)); + assertThat( + settings, + hasEntry(ShardLimitValidator.INDEX_SETTING_SHARD_LIMIT_GROUP.getKey(), ShardLimitValidator.FROZEN_GROUP) + ); }); } @@ -72,16 +74,26 @@ private void executeMountAndRecoversCorrectlyTestCase(Storage storage, long numb registerRepository(repository, FsRepository.TYPE, true, repositorySettings(repository)); final String originalIndex = "logs_" + suffix; - createIndex(originalIndex, Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 3)) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .build()); + createIndex( + originalIndex, + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 3)) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .build() + ); indexDocs(originalIndex, numberOfDocs); createSnapshot(repository, snapshot, originalIndex); deleteIndex(originalIndex); - logger.info("mounting snapshot [repository={}, snapshot={}, index={}] as index [{}] with storage [{}] on version [{}]", - repository, snapshot, originalIndex, index, storage, UPGRADE_FROM_VERSION); + logger.info( + "mounting snapshot [repository={}, snapshot={}, index={}] as index [{}] with storage [{}] on version [{}]", + repository, + snapshot, + originalIndex, + index, + storage, + UPGRADE_FROM_VERSION + ); mountSnapshot(repository, snapshot, originalIndex, index, storage, Settings.EMPTY); } @@ -132,10 +144,13 @@ private void executeBlobCacheCreationTestCase(Storage storage, long numberOfDocs // snapshots must be created from indices on the lowest version, otherwise we won't be able // to mount them again in the mixed version cluster (and we'll have IndexFormatTooNewException) for (int i = 0; i < numberOfSnapshots; i++) { - createIndex(indices[i], Settings.builder() - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 3)) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) - .build()); + createIndex( + indices[i], + Settings.builder() + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 3)) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) + .build() + ); indexDocs(indices[i], numberOfDocs * (i + 1L)); createSnapshot(repository, snapshots[i], indices[i]); @@ -152,44 +167,76 @@ private void executeBlobCacheCreationTestCase(Storage storage, long numberOfDocs final Version minVersion = nodesIdsAndVersions.values().stream().min(Version::compareTo).get(); final Version maxVersion = nodesIdsAndVersions.values().stream().max(Version::compareTo).get(); - final String nodeIdWithMinVersion = randomFrom(nodesIdsAndVersions.entrySet().stream() - .filter(node -> minVersion.equals(node.getValue())).map(Map.Entry::getKey) - .collect(Collectors.toSet())); - - final String nodeIdWithMaxVersion = randomValueOtherThan(nodeIdWithMinVersion, - () -> randomFrom(nodesIdsAndVersions.entrySet().stream() - .filter(node -> maxVersion.equals(node.getValue())).map(Map.Entry::getKey) - .collect(Collectors.toSet()))); + final String nodeIdWithMinVersion = randomFrom( + nodesIdsAndVersions.entrySet() + .stream() + .filter(node -> minVersion.equals(node.getValue())) + .map(Map.Entry::getKey) + .collect(Collectors.toSet()) + ); + + final String nodeIdWithMaxVersion = randomValueOtherThan( + nodeIdWithMinVersion, + () -> randomFrom( + nodesIdsAndVersions.entrySet() + .stream() + .filter(node -> maxVersion.equals(node.getValue())) + .map(Map.Entry::getKey) + .collect(Collectors.toSet()) + ) + ); // The snapshot is mounted on the node with the min. version in order to force the node to populate the blob store cache index. // Then the snapshot is mounted again on a different node with a higher version in order to verify that the docs in the cache // index can be used. String index = "first_mount_" + indices[0]; - logger.info("mounting snapshot as index [{}] with storage [{}] on node [{}] with min. version [{}]", - index, storage, nodeIdWithMinVersion, minVersion); - mountSnapshot(repository, snapshots[0], indices[0], index, storage, + logger.info( + "mounting snapshot as index [{}] with storage [{}] on node [{}] with min. version [{}]", + index, + storage, + nodeIdWithMinVersion, + minVersion + ); + mountSnapshot( + repository, + snapshots[0], + indices[0], + index, + storage, Settings.builder() // we want a specific node version to create docs in the blob cache index .put("index.routing.allocation.include._id", nodeIdWithMinVersion) // prevent interferences with blob cache when full_copy is used .put("index.store.snapshot.cache.prewarm.enabled", false) - .build()); + .build() + ); ensureGreen(index); assertHitCount(index, equalTo(numberOfDocs)); deleteIndex(index); index = "second_mount_" + indices[0]; - logger.info("mounting the same snapshot of index [{}] with storage [{}], this time on node [{}] with higher version [{}]", - index, storage, nodeIdWithMaxVersion, maxVersion); - mountSnapshot(repository, snapshots[0], indices[0], index, storage, + logger.info( + "mounting the same snapshot of index [{}] with storage [{}], this time on node [{}] with higher version [{}]", + index, + storage, + nodeIdWithMaxVersion, + maxVersion + ); + mountSnapshot( + repository, + snapshots[0], + indices[0], + index, + storage, Settings.builder() // we want a specific node version to use the cached blobs created by the nodeIdWithMinVersion .put("index.routing.allocation.include._id", nodeIdWithMaxVersion) .put("index.routing.allocation.exclude._id", nodeIdWithMinVersion) // prevent interferences with blob cache when full_copy is used .put("index.store.snapshot.cache.prewarm.enabled", false) - .build()); + .build() + ); ensureGreen(index); assertHitCount(index, equalTo(numberOfDocs)); deleteIndex(index); @@ -198,39 +245,67 @@ private void executeBlobCacheCreationTestCase(Storage storage, long numberOfDocs // time on the node with the minimum version. index = "first_mount_" + indices[1]; - logger.info("mounting snapshot as index [{}] with storage [{}] on node [{}] with max. version [{}]", - index, storage, nodeIdWithMaxVersion, maxVersion); - mountSnapshot(repository, snapshots[1], indices[1], index, storage, + logger.info( + "mounting snapshot as index [{}] with storage [{}] on node [{}] with max. version [{}]", + index, + storage, + nodeIdWithMaxVersion, + maxVersion + ); + mountSnapshot( + repository, + snapshots[1], + indices[1], + index, + storage, Settings.builder() // we want a specific node version to create docs in the blob cache index .put("index.routing.allocation.include._id", nodeIdWithMaxVersion) // prevent interferences with blob cache when full_copy is used .put("index.store.snapshot.cache.prewarm.enabled", false) - .build()); + .build() + ); ensureGreen(index); assertHitCount(index, equalTo(numberOfDocs * 2L)); deleteIndex(index); index = "second_mount_" + indices[1]; - logger.info("mounting the same snapshot of index [{}] with storage [{}], this time on node [{}] with lower version [{}]", - index, storage, nodeIdWithMinVersion, minVersion); - mountSnapshot(repository, snapshots[1], indices[1], index, storage, + logger.info( + "mounting the same snapshot of index [{}] with storage [{}], this time on node [{}] with lower version [{}]", + index, + storage, + nodeIdWithMinVersion, + minVersion + ); + mountSnapshot( + repository, + snapshots[1], + indices[1], + index, + storage, Settings.builder() // we want a specific node version to use the cached blobs created by the nodeIdWithMinVersion .put("index.routing.allocation.include._id", nodeIdWithMinVersion) .put("index.routing.allocation.exclude._id", nodeIdWithMaxVersion) // prevent interferences with blob cache when full_copy is used .put("index.store.snapshot.cache.prewarm.enabled", false) - .build()); + .build() + ); ensureGreen(index); assertHitCount(index, equalTo(numberOfDocs * 2L)); deleteIndex(index); if (UPGRADE_FROM_VERSION.onOrAfter(Version.V_7_13_0)) { - final Request request = new Request("GET", - "/.snapshot-blob-cache/_settings/index.routing.allocation.include._tier_preference"); - request.setOptions(expectWarnings("this request accesses system indices: [.snapshot-blob-cache], but in a future major " + - "version, direct access to system indices will be prevented by default")); + final Request request = new Request( + "GET", + "/.snapshot-blob-cache/_settings/index.routing.allocation.include._tier_preference" + ); + request.setOptions( + expectWarnings( + "this request accesses system indices: [.snapshot-blob-cache], but in a future major " + + "version, direct access to system indices will be prevented by default" + ) + ); request.addParameter("flat_settings", "true"); final Map snapshotBlobCacheSettings = entityAsMap(adminClient().performRequest(request)); @@ -251,8 +326,10 @@ private void executeBlobCacheCreationTestCase(Storage storage, long numberOfDocs } private static void assumeVersion(Version minSupportedVersion, Storage storageType) { - assumeTrue("Searchable snapshots with storage type [" + storageType + "] is supported since version [" + minSupportedVersion + ']', - UPGRADE_FROM_VERSION.onOrAfter(minSupportedVersion)); + assumeTrue( + "Searchable snapshots with storage type [" + storageType + "] is supported since version [" + minSupportedVersion + ']', + UPGRADE_FROM_VERSION.onOrAfter(minSupportedVersion) + ); } private static void indexDocs(String indexName, long numberOfDocs) throws IOException { @@ -317,11 +394,18 @@ private static void mountSnapshot( } else { assertThat("Parameter 'storage' was introduced in 7.12.0 with " + Storage.SHARED_CACHE, storage, equalTo(Storage.FULL_COPY)); } - request.setJsonEntity("{" + - " \"index\": \"" + indexName + "\"," + - " \"renamed_index\": \"" + renamedIndex + "\"," + - " \"index_settings\": " + Strings.toString(indexSettings) - + "}"); + request.setJsonEntity( + "{" + + " \"index\": \"" + + indexName + + "\"," + + " \"renamed_index\": \"" + + renamedIndex + + "\"," + + " \"index_settings\": " + + Strings.toString(indexSettings) + + "}" + ); final Response response = client().performRequest(request); assertThat( "Failed to mount snapshot [" + snapshotName + "] from repository [" + repositoryName + "]: " + response, @@ -343,8 +427,6 @@ private static void assertHitCount(String indexName, Matcher countMatcher) private static Settings repositorySettings(String repository) { final String pathRepo = System.getProperty("tests.path.searchable.snapshots.repo"); assertThat("Searchable snapshots repository path is null", pathRepo, notNullValue()); - return Settings.builder() - .put("location", pathRepo + '/' + repository) - .build(); + return Settings.builder().put("location", pathRepo + '/' + repository).build(); } } diff --git a/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/TokenBackwardsCompatibilityIT.java b/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/TokenBackwardsCompatibilityIT.java index 3b968f1588f56..7c2c1ac0f7dbe 100644 --- a/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/TokenBackwardsCompatibilityIT.java +++ b/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/TokenBackwardsCompatibilityIT.java @@ -37,8 +37,8 @@ private void collectClientsByVersion() throws IOException { // usual case, clients have different versions twoClients = clientsByVersion.values(); } else { - assert clientsByVersion.size() == 1 : "A rolling upgrade has a maximum of two distinct node versions, found: " - + clientsByVersion.keySet(); + assert clientsByVersion.size() == 1 + : "A rolling upgrade has a maximum of two distinct node versions, found: " + clientsByVersion.keySet(); // tests assumes exactly two clients to simplify some logic twoClients = new ArrayList<>(); twoClients.add(clientsByVersion.values().iterator().next()); @@ -278,8 +278,10 @@ private void assertAccessTokenDoesNotWork(String token) throws IOException { ResponseException e = expectThrows(ResponseException.class, () -> client.performRequest(request)); assertEquals(401, e.getResponse().getStatusLine().getStatusCode()); Response response = e.getResponse(); - assertEquals("Bearer realm=\"security\", error=\"invalid_token\", error_description=\"The access token expired\"", - response.getHeader("WWW-Authenticate")); + assertEquals( + "Bearer realm=\"security\", error=\"invalid_token\", error_description=\"The access token expired\"", + response.getHeader("WWW-Authenticate") + ); } } @@ -287,10 +289,8 @@ private void assertRefreshTokenInvalidated(String refreshToken) throws IOExcepti for (RestClient client : twoClients) { Request refreshTokenRequest = new Request("POST", "/_security/oauth2/token"); refreshTokenRequest.setJsonEntity( - "{\n" + - " \"refresh_token\": \"" + refreshToken + "\",\n" + - " \"grant_type\": \"refresh_token\"\n" + - "}"); + "{\n" + " \"refresh_token\": \"" + refreshToken + "\",\n" + " \"grant_type\": \"refresh_token\"\n" + "}" + ); ResponseException e = expectThrows(ResponseException.class, () -> client.performRequest(refreshTokenRequest)); assertEquals(400, e.getResponse().getStatusLine().getStatusCode()); Response response = e.getResponse(); @@ -323,11 +323,16 @@ private Map getRestClientByVersion() throws IOException { private Map createTokens(RestClient client, String username, String password) throws IOException { final Request createTokenRequest = new Request("POST", "/_security/oauth2/token"); createTokenRequest.setJsonEntity( - "{\n" + - " \"username\": \"" + username + "\",\n" + - " \"password\": \"" + password + "\",\n" + - " \"grant_type\": \"password\"\n" + - "}"); + "{\n" + + " \"username\": \"" + + username + + "\",\n" + + " \"password\": \"" + + password + + "\",\n" + + " \"grant_type\": \"password\"\n" + + "}" + ); Response response = client().performRequest(createTokenRequest); assertOK(response); return entityAsMap(response); @@ -336,10 +341,8 @@ private Map createTokens(RestClient client, String username, Str private void storeTokens(RestClient client, int idx, String accessToken, String refreshToken) throws IOException { final Request indexRequest = new Request("PUT", "token_backwards_compatibility_it/_doc/old_cluster_token" + idx); indexRequest.setJsonEntity( - "{\n" + - " \"token\": \"" + accessToken + "\",\n" + - " \"refresh_token\": \"" + refreshToken + "\"\n" + - "}"); + "{\n" + " \"token\": \"" + accessToken + "\",\n" + " \"refresh_token\": \"" + refreshToken + "\"\n" + "}" + ); Response indexResponse1 = client.performRequest(indexRequest); assertOK(indexResponse1); } @@ -355,10 +358,8 @@ private Map retrieveStoredTokens(RestClient client, int tokenIdx private Map refreshToken(RestClient client, String refreshToken) throws IOException { final Request refreshTokenRequest = new Request("POST", "/_security/oauth2/token"); refreshTokenRequest.setJsonEntity( - "{\n" + - " \"refresh_token\": \"" + refreshToken + "\",\n" + - " \"grant_type\": \"refresh_token\"\n" + - "}"); + "{\n" + " \"refresh_token\": \"" + refreshToken + "\",\n" + " \"grant_type\": \"refresh_token\"\n" + "}" + ); Response refreshResponse = client.performRequest(refreshTokenRequest); assertOK(refreshResponse); return entityAsMap(refreshResponse); diff --git a/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/TransformSurvivesUpgradeIT.java b/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/TransformSurvivesUpgradeIT.java index 6f3c73716da4d..046bd98757113 100644 --- a/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/TransformSurvivesUpgradeIT.java +++ b/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/TransformSurvivesUpgradeIT.java @@ -61,10 +61,7 @@ public class TransformSurvivesUpgradeIT extends AbstractUpgradeTestCase { private static final String TRANSFORM_ENDPOINT = "/_transform/"; private static final String CONTINUOUS_TRANSFORM_ID = "continuous-transform-upgrade-job"; private static final String CONTINUOUS_TRANSFORM_SOURCE = "transform-upgrade-continuous-source"; - private static final List ENTITIES = Stream.iterate(1, n -> n + 1) - .limit(5) - .map(v -> "user_" + v) - .collect(Collectors.toList()); + private static final List ENTITIES = Stream.iterate(1, n -> n + 1).limit(5).map(v -> "user_" + v).collect(Collectors.toList()); private static final List BUCKETS = Stream.iterate(1, n -> n + 1) .limit(5) .map(TimeValue::timeValueMinutes) @@ -89,11 +86,12 @@ protected RestClient buildClient(Settings settings, HttpHost[] hosts) throws IOE public void testTransformRollingUpgrade() throws Exception { Request adjustLoggingLevels = new Request("PUT", "/_cluster/settings"); adjustLoggingLevels.setJsonEntity( - "{\"persistent\": {" + - "\"logger.org.elasticsearch.xpack.core.indexing.AsyncTwoPhaseIndexer\": \"trace\"," + - "\"logger.org.elasticsearch.xpack.dataframe\": \"trace\"," + - "\"logger.org.elasticsearch.xpack.transform\": \"trace\"" + - "}}"); + "{\"persistent\": {" + + "\"logger.org.elasticsearch.xpack.core.indexing.AsyncTwoPhaseIndexer\": \"trace\"," + + "\"logger.org.elasticsearch.xpack.dataframe\": \"trace\"," + + "\"logger.org.elasticsearch.xpack.transform\": \"trace\"" + + "}}" + ); client().performRequest(adjustLoggingLevels); Request waitForYellow = new Request("GET", "/_cluster/health"); waitForYellow.addParameter("wait_for_nodes", "3"); @@ -138,10 +136,12 @@ private void createAndStartContinuousTransform() throws Exception { long totalDocsWritten = totalDocsWrittenSum; TransformConfig config = TransformConfig.builder() .setSyncConfig(TimeSyncConfig.builder().setField("timestamp").setDelay(TimeValue.timeValueSeconds(1)).build()) - .setPivotConfig(PivotConfig.builder() - .setAggregations(new AggregatorFactories.Builder().addAggregator(AggregationBuilders.avg("stars").field("stars"))) - .setGroups(GroupConfig.builder().groupBy("user_id", TermsGroupSource.builder().setField("user_id").build()).build()) - .build()) + .setPivotConfig( + PivotConfig.builder() + .setAggregations(new AggregatorFactories.Builder().addAggregator(AggregationBuilders.avg("stars").field("stars"))) + .setGroups(GroupConfig.builder().groupBy("user_id", TermsGroupSource.builder().setField("user_id").build()).build()) + .build() + ) .setDest(DestConfig.builder().setIndex(CONTINUOUS_TRANSFORM_ID + "_idx").build()) .setSource(SourceConfig.builder().setIndex(CONTINUOUS_TRANSFORM_SOURCE).build()) .setId(CONTINUOUS_TRANSFORM_ID) @@ -161,7 +161,6 @@ private void createAndStartContinuousTransform() throws Exception { assertThat(stateAndStats.getState(), oneOf(TransformStats.State.STARTED, TransformStats.State.INDEXING)); }, 120, TimeUnit.SECONDS); - // We want to make sure our latest state is written before we turn the node off, this makes the testing more reliable awaitWrittenIndexerState(CONTINUOUS_TRANSFORM_ID, IndexerState.STARTED.value()); } @@ -172,11 +171,9 @@ private void verifyContinuousTransformHandlesData(long expectedLastCheckpoint) t // A continuous transform should automatically become started when it gets assigned to a node // if it was assigned to the node that was removed from the cluster assertBusy(() -> { - TransformStats stateAndStats = getTransformStats(CONTINUOUS_TRANSFORM_ID); - assertThat(stateAndStats.getState(), oneOf(TransformStats.State.STARTED, TransformStats.State.INDEXING)); - }, - 120, - TimeUnit.SECONDS); + TransformStats stateAndStats = getTransformStats(CONTINUOUS_TRANSFORM_ID); + assertThat(stateAndStats.getState(), oneOf(TransformStats.State.STARTED, TransformStats.State.INDEXING)); + }, 120, TimeUnit.SECONDS); TransformStats previousStateAndStats = getTransformStats(CONTINUOUS_TRANSFORM_ID); @@ -192,50 +189,61 @@ private void verifyContinuousTransformHandlesData(long expectedLastCheckpoint) t waitUntilAfterCheckpoint(CONTINUOUS_TRANSFORM_ID, expectedLastCheckpoint); - assertBusy(() -> assertThat( - getTransformStats(CONTINUOUS_TRANSFORM_ID).getIndexerStats().getDocumentsProcessed(), - greaterThanOrEqualTo(docs + previousStateAndStats.getIndexerStats().getDocumentsProcessed())), + assertBusy( + () -> assertThat( + getTransformStats(CONTINUOUS_TRANSFORM_ID).getIndexerStats().getDocumentsProcessed(), + greaterThanOrEqualTo(docs + previousStateAndStats.getIndexerStats().getDocumentsProcessed()) + ), 120, - TimeUnit.SECONDS); + TimeUnit.SECONDS + ); TransformStats stateAndStats = getTransformStats(CONTINUOUS_TRANSFORM_ID); - assertThat(stateAndStats.getState(), - oneOf(TransformStats.State.STARTED, TransformStats.State.INDEXING)); + assertThat(stateAndStats.getState(), oneOf(TransformStats.State.STARTED, TransformStats.State.INDEXING)); awaitWrittenIndexerState(CONTINUOUS_TRANSFORM_ID, (responseBody) -> { - Map indexerStats = (Map) ((List) XContentMapValues.extractValue("hits.hits._source.stats", - responseBody)) - .get(0); - assertThat((Integer) indexerStats.get("documents_indexed"), - greaterThan(Long.valueOf(previousStateAndStats.getIndexerStats().getDocumentsIndexed()).intValue())); - assertThat((Integer) indexerStats.get("documents_processed"), - greaterThan(Long.valueOf(previousStateAndStats.getIndexerStats().getDocumentsProcessed()).intValue())); + Map indexerStats = (Map) ((List) XContentMapValues.extractValue( + "hits.hits._source.stats", + responseBody + )).get(0); + assertThat( + (Integer) indexerStats.get("documents_indexed"), + greaterThan(Long.valueOf(previousStateAndStats.getIndexerStats().getDocumentsIndexed()).intValue()) + ); + assertThat( + (Integer) indexerStats.get("documents_processed"), + greaterThan(Long.valueOf(previousStateAndStats.getIndexerStats().getDocumentsProcessed()).intValue()) + ); }); } private void awaitWrittenIndexerState(String id, Consumer> responseAssertion) throws Exception { - Request getStatsDocsRequest = new Request("GET", - TRANSFORM_INTERNAL_INDEX_PREFIX + "*," + - TRANSFORM_INTERNAL_INDEX_PREFIX_DEPRECATED + "*" + - "/_search"); - - getStatsDocsRequest.setJsonEntity("{\n" + - " \"query\": {\n" + - " \"bool\": {\n" + - " \"filter\": \n" + - " {\"term\": {\n" + - " \"_id\": \"data_frame_transform_state_and_stats-" + id + "\"\n" + - " }}\n" + - " }\n" + - " },\n" + - " \"sort\": [\n" + - " {\n" + - " \"_index\": {\n" + - " \"order\": \"desc\"\n" + - " }\n" + - " }\n" + - " ],\n" + - " \"size\": 1\n" + - "}"); + Request getStatsDocsRequest = new Request( + "GET", + TRANSFORM_INTERNAL_INDEX_PREFIX + "*," + TRANSFORM_INTERNAL_INDEX_PREFIX_DEPRECATED + "*" + "/_search" + ); + + getStatsDocsRequest.setJsonEntity( + "{\n" + + " \"query\": {\n" + + " \"bool\": {\n" + + " \"filter\": \n" + + " {\"term\": {\n" + + " \"_id\": \"data_frame_transform_state_and_stats-" + + id + + "\"\n" + + " }}\n" + + " }\n" + + " },\n" + + " \"sort\": [\n" + + " {\n" + + " \"_index\": {\n" + + " \"order\": \"desc\"\n" + + " }\n" + + " }\n" + + " ],\n" + + " \"size\": 1\n" + + "}" + ); assertBusy(() -> { // Want to make sure we get the latest docs client().performRequest(new Request("POST", TRANSFORM_INTERNAL_INDEX_PREFIX + "*/_refresh")); @@ -250,8 +258,7 @@ private void awaitWrittenIndexerState(String id, Consumer> responseAss private void awaitWrittenIndexerState(String id, String indexerState) throws Exception { awaitWrittenIndexerState(id, (responseBody) -> { - String storedState = ((List) XContentMapValues.extractValue("hits.hits._source.state.indexer_state", responseBody)) - .get(0) + String storedState = ((List) XContentMapValues.extractValue("hits.hits._source.state.indexer_state", responseBody)).get(0) .toString(); assertThat(storedState, equalTo(indexerState)); }); @@ -280,8 +287,7 @@ private void startTransform(String id) throws IOException { } private void stopTransform(String id) throws IOException { - final Request stopDataframeTransformRequest = new Request("POST", - getTransformEndpoint() + id + "/_stop?wait_for_completion=true"); + final Request stopDataframeTransformRequest = new Request("POST", getTransformEndpoint() + id + "/_stop?wait_for_completion=true"); Response response = client().performRequest(stopDataframeTransformRequest); assertEquals(200, response.getStatusLine().getStatusCode()); } @@ -291,9 +297,14 @@ private TransformStats getTransformStats(String id) throws IOException { Response response = client().performRequest(getStats); assertEquals(200, response.getStatusLine().getStatusCode()); XContentType xContentType = XContentType.fromMediaType(response.getEntity().getContentType().getValue()); - try (XContentParser parser = xContentType.xContent().createParser( - NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, - response.getEntity().getContent())) { + try ( + XContentParser parser = xContentType.xContent() + .createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + response.getEntity().getContent() + ) + ) { GetTransformStatsResponse resp = GetTransformStatsResponse.fromXContent(parser); assertThat(resp.getTransformsStats(), hasSize(1)); return resp.getTransformsStats().get(0); @@ -301,8 +312,11 @@ private TransformStats getTransformStats(String id) throws IOException { } private void waitUntilAfterCheckpoint(String id, long currentCheckpoint) throws Exception { - assertBusy(() -> assertThat(getTransformStats(id).getCheckpointingInfo().getLast().getCheckpoint(), greaterThan(currentCheckpoint)), - 60, TimeUnit.SECONDS); + assertBusy( + () -> assertThat(getTransformStats(id).getCheckpointingInfo().getLast().getCheckpoint(), greaterThan(currentCheckpoint)), + 60, + TimeUnit.SECONDS + ); } private void createIndex(String indexName) throws IOException { diff --git a/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/UpgradeClusterClientYamlTestSuiteIT.java b/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/UpgradeClusterClientYamlTestSuiteIT.java index 49c24ade6e65c..49d8c1a2f1ffe 100644 --- a/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/UpgradeClusterClientYamlTestSuiteIT.java +++ b/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/UpgradeClusterClientYamlTestSuiteIT.java @@ -8,6 +8,7 @@ import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; import com.carrotsearch.randomizedtesting.annotations.TimeoutSuite; + import org.apache.lucene.util.TimeUnits; import org.elasticsearch.Version; import org.elasticsearch.client.Request; @@ -43,8 +44,11 @@ public void waitForTemplates() throws Exception { if (AbstractUpgradeTestCase.CLUSTER_TYPE == AbstractUpgradeTestCase.ClusterType.OLD) { try { boolean clusterUnderstandsComposableTemplates = AbstractUpgradeTestCase.UPGRADE_FROM_VERSION.onOrAfter(Version.V_7_8_0); - XPackRestTestHelper.waitForTemplates(client(), XPackRestTestConstants.ML_POST_V7120_TEMPLATES, - clusterUnderstandsComposableTemplates); + XPackRestTestHelper.waitForTemplates( + client(), + XPackRestTestConstants.ML_POST_V7120_TEMPLATES, + clusterUnderstandsComposableTemplates + ); } catch (AssertionError e) { throw new AssertionError("Failure in test setup: Failed to initialize ML index templates", e); } @@ -124,11 +128,11 @@ public static Iterable parameters() throws Exception { protected Settings restClientSettings() { String token = "Basic " + Base64.getEncoder().encodeToString(("test_user:x-pack-test-password").getBytes(StandardCharsets.UTF_8)); return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - // we increase the timeout here to 90 seconds to handle long waits for a green - // cluster health. the waits for green need to be longer than a minute to - // account for delayed shards - .put(ESRestTestCase.CLIENT_SOCKET_TIMEOUT, "90s") - .build(); + .put(ThreadContext.PREFIX + ".Authorization", token) + // we increase the timeout here to 90 seconds to handle long waits for a green + // cluster health. the waits for green need to be longer than a minute to + // account for delayed shards + .put(ESRestTestCase.CLIENT_SOCKET_TIMEOUT, "90s") + .build(); } } diff --git a/x-pack/qa/runtime-fields/core-with-mapped/src/yamlRestTest/java/org/elasticsearch/xpack/runtimefields/test/mapped/CoreWithMappedRuntimeFieldsIT.java b/x-pack/qa/runtime-fields/core-with-mapped/src/yamlRestTest/java/org/elasticsearch/xpack/runtimefields/test/mapped/CoreWithMappedRuntimeFieldsIT.java index df90b885c7da8..e760953735561 100644 --- a/x-pack/qa/runtime-fields/core-with-mapped/src/yamlRestTest/java/org/elasticsearch/xpack/runtimefields/test/mapped/CoreWithMappedRuntimeFieldsIT.java +++ b/x-pack/qa/runtime-fields/core-with-mapped/src/yamlRestTest/java/org/elasticsearch/xpack/runtimefields/test/mapped/CoreWithMappedRuntimeFieldsIT.java @@ -9,6 +9,7 @@ import com.carrotsearch.randomizedtesting.annotations.Name; import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; + import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate; import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase; diff --git a/x-pack/qa/runtime-fields/core-with-search/src/yamlRestTest/java/org/elasticsearch/xpack/runtimefields/test/search/CoreTestsWithSearchRuntimeFieldsIT.java b/x-pack/qa/runtime-fields/core-with-search/src/yamlRestTest/java/org/elasticsearch/xpack/runtimefields/test/search/CoreTestsWithSearchRuntimeFieldsIT.java index 71dc9430a4dac..6743e64f36913 100644 --- a/x-pack/qa/runtime-fields/core-with-search/src/yamlRestTest/java/org/elasticsearch/xpack/runtimefields/test/search/CoreTestsWithSearchRuntimeFieldsIT.java +++ b/x-pack/qa/runtime-fields/core-with-search/src/yamlRestTest/java/org/elasticsearch/xpack/runtimefields/test/search/CoreTestsWithSearchRuntimeFieldsIT.java @@ -9,6 +9,7 @@ import com.carrotsearch.randomizedtesting.annotations.Name; import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; + import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.common.regex.Regex; import org.elasticsearch.common.xcontent.XContentHelper; diff --git a/x-pack/qa/saml-idp-tests/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlAuthenticationIT.java b/x-pack/qa/saml-idp-tests/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlAuthenticationIT.java index 66b06d443edbd..32710f42caf5e 100644 --- a/x-pack/qa/saml-idp-tests/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlAuthenticationIT.java +++ b/x-pack/qa/saml-idp-tests/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlAuthenticationIT.java @@ -31,30 +31,26 @@ import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.Response; import org.elasticsearch.client.ResponseException; -import org.elasticsearch.core.CheckedFunction; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.collect.MapBuilder; -import org.elasticsearch.core.Tuple; import org.elasticsearch.common.io.Streams; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.CheckedFunction; +import org.elasticsearch.core.TimeValue; +import org.elasticsearch.core.Tuple; +import org.elasticsearch.test.rest.ESRestTestCase; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentType; -import org.elasticsearch.test.rest.ESRestTestCase; import org.elasticsearch.xpack.core.common.socket.SocketAccess; import org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken; import org.elasticsearch.xpack.core.ssl.CertParsingUtils; import org.hamcrest.Matchers; import org.junit.Before; -import javax.net.ssl.KeyManager; -import javax.net.ssl.SSLContext; -import javax.net.ssl.TrustManager; -import javax.net.ssl.X509ExtendedTrustManager; import java.io.IOException; import java.io.InputStream; import java.net.URI; @@ -67,6 +63,11 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; +import javax.net.ssl.KeyManager; +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509ExtendedTrustManager; + import static org.elasticsearch.common.xcontent.XContentHelper.convertToMap; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.equalTo; @@ -86,9 +87,7 @@ public class SamlAuthenticationIT extends ESRestTestCase { @Override protected Settings restAdminSettings() { String token = basicAuthHeaderValue("test_admin", new SecureString("x-pack-test-password".toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } /** @@ -111,17 +110,29 @@ public void setKibanaPassword() throws IOException { @Before public void setupRoleMapping() throws IOException { Request request = new Request("PUT", "/_security/role_mapping/thor-kibana"); - request.setJsonEntity(Strings.toString(XContentBuilder.builder(XContentType.JSON.xContent()) - .startObject() - .array("roles", new String[]{"kibana_admin"}) + request.setJsonEntity( + Strings.toString( + XContentBuilder.builder(XContentType.JSON.xContent()) + .startObject() + .array("roles", new String[] { "kibana_admin" }) .field("enabled", true) .startObject("rules") - .startArray("all") - .startObject().startObject("field").field("username", "thor").endObject().endObject() - .startObject().startObject("field").field("realm.name", "shibboleth").endObject().endObject() - .endArray() // "all" + .startArray("all") + .startObject() + .startObject("field") + .field("username", "thor") + .endObject() + .endObject() + .startObject() + .startObject("field") + .field("realm.name", "shibboleth") + .endObject() + .endObject() + .endArray() // "all" .endObject() // "rules" - .endObject())); + .endObject() + ) + ); adminClient().performRequest(request); } @@ -206,7 +217,7 @@ private Tuple loginViaSaml(String realmName) throws Exception { final Object authentication = result.get("authentication"); assertThat(authentication, notNullValue()); assertThat(authentication, instanceOf(Map.class)); - assertEquals("thor", ((Map)authentication).get("username")); + assertEquals("thor", ((Map) authentication).get("username")); return new Tuple<>((String) accessToken, (String) refreshToken); } @@ -340,8 +351,7 @@ private String submitConsentForm(BasicHttpContext context, CloseableHttpClient c params.add(new BasicNameValuePair("_eventId_proceed", "Accept")); form.setEntity(new UrlEncodedFormEntity(params)); - return execute(client, form, context, - response -> parseSamlSubmissionForm(response.getEntity().getContent())); + return execute(client, form, context, response -> parseSamlSubmissionForm(response.getEntity().getContent())); } /** @@ -354,13 +364,13 @@ private String submitConsentForm(BasicHttpContext context, CloseableHttpClient c private Map submitSamlResponse(String saml, String id, String realmName, boolean shouldSucceed) throws IOException { // By POSTing to the ES API directly, we miss the check that the IDP would post this to the ACS that we would expect them to, but // we implicitly check this while checking the `Destination` element of the SAML response in the SAML realm. - final MapBuilder bodyBuilder = new MapBuilder() - .put("content", saml) + final MapBuilder bodyBuilder = new MapBuilder().put("content", saml) .put("realm", realmName) .put("ids", Collections.singletonList(id)); try { - final Response response = - client().performRequest(buildRequest("POST", "/_security/saml/authenticate", bodyBuilder.map(), kibanaAuth())); + final Response response = client().performRequest( + buildRequest("POST", "/_security/saml/authenticate", bodyBuilder.map(), kibanaAuth()) + ); if (shouldSucceed) { assertHttpOk(response.getStatusLine()); } @@ -404,15 +414,18 @@ private Map parseResponseAsMap(HttpEntity entity) throws IOExcep return convertToMap(XContentType.JSON.xContent(), entity.getContent(), false); } - private T execute(CloseableHttpClient client, HttpRequestBase request, - HttpContext context, CheckedFunction body) - throws IOException { + private T execute( + CloseableHttpClient client, + HttpRequestBase request, + HttpContext context, + CheckedFunction body + ) throws IOException { final int timeout = (int) TimeValue.timeValueSeconds(90).millis(); RequestConfig requestConfig = RequestConfig.custom() - .setConnectionRequestTimeout(timeout) - .setConnectTimeout(timeout) - .setSocketTimeout(timeout) - .build(); + .setConnectionRequestTimeout(timeout) + .setConnectTimeout(timeout) + .setSocketTimeout(timeout) + .build(); request.setConfig(requestConfig); logger.info("Execute HTTP " + request.getMethod() + ' ' + request.getURI()); try (CloseableHttpResponse response = SocketAccess.doPrivileged(() -> client.execute(request, context))) { diff --git a/x-pack/qa/security-example-spi-extension/src/javaRestTest/java/org/elasticsearch/example/realm/CustomRealmIT.java b/x-pack/qa/security-example-spi-extension/src/javaRestTest/java/org/elasticsearch/example/realm/CustomRealmIT.java index f8ef52e70d0f2..3c7ade3282192 100644 --- a/x-pack/qa/security-example-spi-extension/src/javaRestTest/java/org/elasticsearch/example/realm/CustomRealmIT.java +++ b/x-pack/qa/security-example-spi-extension/src/javaRestTest/java/org/elasticsearch/example/realm/CustomRealmIT.java @@ -25,7 +25,7 @@ public class CustomRealmIT extends ESRestTestCase { // These are configured in build.gradle - public static final String USERNAME= "test_user"; + public static final String USERNAME = "test_user"; public static final String PASSWORD = "secret_password"; @Override diff --git a/x-pack/qa/security-example-spi-extension/src/javaRestTest/java/org/elasticsearch/example/realm/CustomRoleMappingRealmIT.java b/x-pack/qa/security-example-spi-extension/src/javaRestTest/java/org/elasticsearch/example/realm/CustomRoleMappingRealmIT.java index d655d6a9ee14e..0d9da62b4ff2d 100644 --- a/x-pack/qa/security-example-spi-extension/src/javaRestTest/java/org/elasticsearch/example/realm/CustomRoleMappingRealmIT.java +++ b/x-pack/qa/security-example-spi-extension/src/javaRestTest/java/org/elasticsearch/example/realm/CustomRoleMappingRealmIT.java @@ -40,13 +40,17 @@ protected Settings restClientSettings() { public void setupRoleMapping() throws Exception { expectedRole = randomAlphaOfLengthBetween(4, 16); Request request = new Request("PUT", "/_security/role_mapping/test"); - request.setJsonEntity("{" + - "\"enabled\": true," + - "\"roles\":[\"" + - expectedRole + - "\"]," + - "\"rules\":{\"field\":{\"groups\":\"" + CustomRoleMappingRealm.USER_GROUP + "\"} }" + - "}"); + request.setJsonEntity( + "{" + + "\"enabled\": true," + + "\"roles\":[\"" + + expectedRole + + "\"]," + + "\"rules\":{\"field\":{\"groups\":\"" + + CustomRoleMappingRealm.USER_GROUP + + "\"} }" + + "}" + ); adminClient().performRequest(request); } diff --git a/x-pack/qa/security-example-spi-extension/src/javaRestTest/java/org/elasticsearch/example/role/CustomRolesProviderIT.java b/x-pack/qa/security-example-spi-extension/src/javaRestTest/java/org/elasticsearch/example/role/CustomRolesProviderIT.java index 4f83c57106a5b..8b997decdf5cf 100644 --- a/x-pack/qa/security-example-spi-extension/src/javaRestTest/java/org/elasticsearch/example/role/CustomRolesProviderIT.java +++ b/x-pack/qa/security-example-spi-extension/src/javaRestTest/java/org/elasticsearch/example/role/CustomRolesProviderIT.java @@ -42,8 +42,10 @@ public class CustomRolesProviderIT extends ESRestTestCase { private static final RequestOptions AUTH_OPTIONS; static { RequestOptions.Builder options = RequestOptions.DEFAULT.toBuilder(); - options.addHeader(UsernamePasswordToken.BASIC_AUTH_HEADER, - UsernamePasswordToken.basicAuthHeaderValue(TEST_USER, new SecureString(TEST_PWD.toCharArray()))); + options.addHeader( + UsernamePasswordToken.BASIC_AUTH_HEADER, + UsernamePasswordToken.basicAuthHeaderValue(TEST_USER, new SecureString(TEST_PWD.toCharArray())) + ); AUTH_OPTIONS = options.build(); } @@ -56,9 +58,11 @@ protected Settings restClientSettings() { } public void setupTestUser(String role) throws IOException { - new TestRestHighLevelClient().security().putUser( - PutUserRequest.withPassword(new User(TEST_USER, List.of(role)), TEST_PWD.toCharArray(), true, RefreshPolicy.IMMEDIATE), - RequestOptions.DEFAULT); + new TestRestHighLevelClient().security() + .putUser( + PutUserRequest.withPassword(new User(TEST_USER, List.of(role)), TEST_PWD.toCharArray(), true, RefreshPolicy.IMMEDIATE), + RequestOptions.DEFAULT + ); } public void testAuthorizedCustomRoleSucceeds() throws Exception { diff --git a/x-pack/qa/security-example-spi-extension/src/main/java/org/elasticsearch/example/ExampleSecurityExtension.java b/x-pack/qa/security-example-spi-extension/src/main/java/org/elasticsearch/example/ExampleSecurityExtension.java index d09dc73c7b119..eeec67a0580fa 100644 --- a/x-pack/qa/security-example-spi-extension/src/main/java/org/elasticsearch/example/ExampleSecurityExtension.java +++ b/x-pack/qa/security-example-spi-extension/src/main/java/org/elasticsearch/example/ExampleSecurityExtension.java @@ -51,8 +51,7 @@ public String extensionName() { public Map getRealms(SecurityComponents components) { return Map.ofEntries( Map.entry(CustomRealm.TYPE, CustomRealm::new), - Map.entry(CustomRoleMappingRealm.TYPE, - config -> new CustomRoleMappingRealm(config, components.roleMapper())) + Map.entry(CustomRoleMappingRealm.TYPE, config -> new CustomRoleMappingRealm(config, components.roleMapper())) ); } @@ -62,8 +61,7 @@ public AuthenticationFailureHandler getAuthenticationFailureHandler(SecurityComp } @Override - public List, ActionListener>> - getRolesProviders(SecurityComponents components) { + public List, ActionListener>> getRolesProviders(SecurityComponents components) { CustomInMemoryRolesProvider rp1 = new CustomInMemoryRolesProvider(Collections.singletonMap(ROLE_A, "read")); Map roles = new HashMap<>(); roles.put(ROLE_A, "all"); diff --git a/x-pack/qa/security-example-spi-extension/src/main/java/org/elasticsearch/example/SpiExtensionPlugin.java b/x-pack/qa/security-example-spi-extension/src/main/java/org/elasticsearch/example/SpiExtensionPlugin.java index 506056d6ef29a..fb3dc8ee6bc15 100644 --- a/x-pack/qa/security-example-spi-extension/src/main/java/org/elasticsearch/example/SpiExtensionPlugin.java +++ b/x-pack/qa/security-example-spi-extension/src/main/java/org/elasticsearch/example/SpiExtensionPlugin.java @@ -28,7 +28,8 @@ public class SpiExtensionPlugin extends Plugin implements ActionPlugin { public Collection getRestHeaders() { return Arrays.asList( new RestHeaderDefinition(CustomRealm.USER_HEADER, false), - new RestHeaderDefinition(CustomRealm.PW_HEADER, false)); + new RestHeaderDefinition(CustomRealm.PW_HEADER, false) + ); } @Override diff --git a/x-pack/qa/security-example-spi-extension/src/main/java/org/elasticsearch/example/realm/CustomAuthenticationFailureHandler.java b/x-pack/qa/security-example-spi-extension/src/main/java/org/elasticsearch/example/realm/CustomAuthenticationFailureHandler.java index 03f0e45d776cf..85a36ccf71b36 100644 --- a/x-pack/qa/security-example-spi-extension/src/main/java/org/elasticsearch/example/realm/CustomAuthenticationFailureHandler.java +++ b/x-pack/qa/security-example-spi-extension/src/main/java/org/elasticsearch/example/realm/CustomAuthenticationFailureHandler.java @@ -22,8 +22,7 @@ public CustomAuthenticationFailureHandler() { } @Override - public ElasticsearchSecurityException failedAuthentication(RestRequest request, AuthenticationToken token, - ThreadContext context) { + public ElasticsearchSecurityException failedAuthentication(RestRequest request, AuthenticationToken token, ThreadContext context) { ElasticsearchSecurityException e = super.failedAuthentication(request, token, context); // set a custom header e.addHeader("WWW-Authenticate", "custom-challenge"); @@ -31,8 +30,12 @@ public ElasticsearchSecurityException failedAuthentication(RestRequest request, } @Override - public ElasticsearchSecurityException failedAuthentication(TransportMessage message, AuthenticationToken token, String action, - ThreadContext context) { + public ElasticsearchSecurityException failedAuthentication( + TransportMessage message, + AuthenticationToken token, + String action, + ThreadContext context + ) { ElasticsearchSecurityException e = super.failedAuthentication(message, token, action, context); // set a custom header e.addHeader("WWW-Authenticate", "custom-challenge"); diff --git a/x-pack/qa/security-example-spi-extension/src/main/java/org/elasticsearch/example/realm/CustomRealm.java b/x-pack/qa/security-example-spi-extension/src/main/java/org/elasticsearch/example/realm/CustomRealm.java index 20ec6ac02b05b..1da7e66af5ed3 100644 --- a/x-pack/qa/security-example-spi-extension/src/main/java/org/elasticsearch/example/realm/CustomRealm.java +++ b/x-pack/qa/security-example-spi-extension/src/main/java/org/elasticsearch/example/realm/CustomRealm.java @@ -7,10 +7,10 @@ package org.elasticsearch.example.realm; import org.elasticsearch.action.ActionListener; -import org.elasticsearch.core.CharArrays; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.CharArrays; import org.elasticsearch.example.SpiExtensionPlugin; import org.elasticsearch.xpack.core.security.authc.AuthenticationResult; import org.elasticsearch.xpack.core.security.authc.AuthenticationToken; @@ -36,8 +36,12 @@ public class CustomRealm extends Realm { // Because simple string settings in realms are common, this is a shorthand method, but it does the same thing as the ROLES_SETTING // that is declared below (with the minor difference that "username" is a single string, and "roles" is a list) - public static final Setting.AffixSetting USERNAME_SETTING = RealmSettings.simpleString(TYPE, "username", - Setting.Property.NodeScope, Setting.Property.Filtered); + public static final Setting.AffixSetting USERNAME_SETTING = RealmSettings.simpleString( + TYPE, + "username", + Setting.Property.NodeScope, + Setting.Property.Filtered + ); public static final Setting.AffixSetting PASSWORD_SETTING = RealmSettings.secureString(TYPE, "password"); @@ -50,7 +54,9 @@ public class CustomRealm extends Realm { */ public static final Setting.AffixSetting> ROLES_SETTING = Setting.affixKeySetting( RealmSettings.realmSettingPrefix(TYPE), - "roles", key -> Setting.listSetting(key, DEFAULT_ROLES, Function.identity(), Setting.Property.NodeScope)); + "roles", + key -> Setting.listSetting(key, DEFAULT_ROLES, Function.identity(), Setting.Property.NodeScope) + ); private final String username; private final SecureString password; @@ -82,7 +88,7 @@ public UsernamePasswordToken token(ThreadContext threadContext) { @Override public void authenticate(AuthenticationToken authToken, ActionListener> listener) { - UsernamePasswordToken token = (UsernamePasswordToken)authToken; + UsernamePasswordToken token = (UsernamePasswordToken) authToken; final String actualUser = token.principal(); if (username.equals(actualUser)) { if (CharArrays.constantTimeEquals(token.credentials().getChars(), password.getChars())) { diff --git a/x-pack/qa/security-example-spi-extension/src/main/java/org/elasticsearch/example/realm/CustomRoleMappingRealm.java b/x-pack/qa/security-example-spi-extension/src/main/java/org/elasticsearch/example/realm/CustomRoleMappingRealm.java index 4aba3ddf9655a..1c46fb3a88ecd 100644 --- a/x-pack/qa/security-example-spi-extension/src/main/java/org/elasticsearch/example/realm/CustomRoleMappingRealm.java +++ b/x-pack/qa/security-example-spi-extension/src/main/java/org/elasticsearch/example/realm/CustomRoleMappingRealm.java @@ -68,10 +68,10 @@ public void lookupUser(String username, ActionListener listener) { return; } if (USERNAME.equals(username)) { - buildUser(username, ActionListener.wrap( - u -> listener.onResponse(cache.computeIfAbsent(username, k -> u)), - listener::onFailure - )); + buildUser( + username, + ActionListener.wrap(u -> listener.onResponse(cache.computeIfAbsent(username, k -> u)), listener::onFailure) + ); } else { listener.onResponse(null); } @@ -79,10 +79,10 @@ public void lookupUser(String username, ActionListener listener) { private void buildUser(String username, ActionListener listener) { final UserRoleMapper.UserData data = new UserRoleMapper.UserData(username, null, List.of(USER_GROUP), Map.of(), super.config); - roleMapper.resolveRoles(data, ActionListener.wrap( - roles -> listener.onResponse(new User(username, roles.toArray(String[]::new))), - listener::onFailure - )); + roleMapper.resolveRoles( + data, + ActionListener.wrap(roles -> listener.onResponse(new User(username, roles.toArray(String[]::new))), listener::onFailure) + ); } @Override diff --git a/x-pack/qa/security-example-spi-extension/src/main/java/org/elasticsearch/example/role/CustomInMemoryRolesProvider.java b/x-pack/qa/security-example-spi-extension/src/main/java/org/elasticsearch/example/role/CustomInMemoryRolesProvider.java index 8890ace7917b9..c60c6a3ff2972 100644 --- a/x-pack/qa/security-example-spi-extension/src/main/java/org/elasticsearch/example/role/CustomInMemoryRolesProvider.java +++ b/x-pack/qa/security-example-spi-extension/src/main/java/org/elasticsearch/example/role/CustomInMemoryRolesProvider.java @@ -37,14 +37,17 @@ public void accept(Set roles, ActionListener listen for (String role : roles) { if (rolePermissionSettings.containsKey(role)) { roleDescriptors.add( - new RoleDescriptor(role, new String[] { "all" }, - new RoleDescriptor.IndicesPrivileges[] { - RoleDescriptor.IndicesPrivileges.builder() - .privileges(rolePermissionSettings.get(role)) - .indices(INDEX) - .grantedFields("*") - .build() - }, null) + new RoleDescriptor( + role, + new String[] { "all" }, + new RoleDescriptor.IndicesPrivileges[] { + RoleDescriptor.IndicesPrivileges.builder() + .privileges(rolePermissionSettings.get(role)) + .indices(INDEX) + .grantedFields("*") + .build() }, + null + ) ); } } diff --git a/x-pack/qa/security-example-spi-extension/src/test/java/org/elasticsearch/example/realm/CustomRealmTests.java b/x-pack/qa/security-example-spi-extension/src/test/java/org/elasticsearch/example/realm/CustomRealmTests.java index 9bd7dc3e78224..c7459175febdc 100644 --- a/x-pack/qa/security-example-spi-extension/src/test/java/org/elasticsearch/example/realm/CustomRealmTests.java +++ b/x-pack/qa/security-example-spi-extension/src/test/java/org/elasticsearch/example/realm/CustomRealmTests.java @@ -31,10 +31,14 @@ public class CustomRealmTests extends ESTestCase { public void testAuthenticateDefaultConfig() { Settings globalSettings = Settings.builder().put("path.home", createTempDir()).build(); final RealmConfig.RealmIdentifier realmIdentifier = new RealmConfig.RealmIdentifier(CustomRealm.TYPE, "test"); - CustomRealm realm = new CustomRealm(new RealmConfig( - realmIdentifier, - Settings.builder().put(globalSettings).put(getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0).build(), - TestEnvironment.newEnvironment(globalSettings), new ThreadContext(globalSettings))); + CustomRealm realm = new CustomRealm( + new RealmConfig( + realmIdentifier, + Settings.builder().put(globalSettings).put(getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0).build(), + TestEnvironment.newEnvironment(globalSettings), + new ThreadContext(globalSettings) + ) + ); SecureString password = CustomRealm.DEFAULT_KNOWN_PW.clone(); UsernamePasswordToken token = new UsernamePasswordToken(CustomRealm.DEFAULT_KNOWN_USER, password); PlainActionFuture> plainActionFuture = new PlainActionFuture<>(); @@ -57,12 +61,9 @@ public void testAuthenticateCustomConfig() { .setSecureSettings(secureSettings) .putList(getFullSettingKey(realmIdentifier.getName(), CustomRealm.ROLES_SETTING), "president", "villain") .build(); - CustomRealm realm = new CustomRealm(new RealmConfig( - realmIdentifier, - settings, - TestEnvironment.newEnvironment(settings), - new ThreadContext(settings) - )); + CustomRealm realm = new CustomRealm( + new RealmConfig(realmIdentifier, settings, TestEnvironment.newEnvironment(settings), new ThreadContext(settings)) + ); UsernamePasswordToken token = new UsernamePasswordToken("skroob", new SecureString(password.toCharArray())); PlainActionFuture> plainActionFuture = new PlainActionFuture<>(); realm.authenticate(token, plainActionFuture); @@ -75,10 +76,14 @@ public void testAuthenticateCustomConfig() { public void testAuthenticateBadUser() { Settings globalSettings = Settings.builder().put("path.home", createTempDir()).build(); final RealmConfig.RealmIdentifier realmIdentifier = new RealmConfig.RealmIdentifier(CustomRealm.TYPE, "test"); - CustomRealm realm = new CustomRealm(new RealmConfig( - realmIdentifier, - Settings.builder().put(globalSettings).put(getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0).build(), - TestEnvironment.newEnvironment(globalSettings), new ThreadContext(globalSettings))); + CustomRealm realm = new CustomRealm( + new RealmConfig( + realmIdentifier, + Settings.builder().put(globalSettings).put(getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0).build(), + TestEnvironment.newEnvironment(globalSettings), + new ThreadContext(globalSettings) + ) + ); SecureString password = CustomRealm.DEFAULT_KNOWN_PW.clone(); UsernamePasswordToken token = new UsernamePasswordToken(CustomRealm.DEFAULT_KNOWN_USER + "1", password); PlainActionFuture> plainActionFuture = new PlainActionFuture<>(); diff --git a/x-pack/qa/security-example-spi-extension/src/test/java/org/elasticsearch/example/realm/CustomRoleMappingRealmTests.java b/x-pack/qa/security-example-spi-extension/src/test/java/org/elasticsearch/example/realm/CustomRoleMappingRealmTests.java index 2282252ffdfc0..95392bf198a79 100644 --- a/x-pack/qa/security-example-spi-extension/src/test/java/org/elasticsearch/example/realm/CustomRoleMappingRealmTests.java +++ b/x-pack/qa/security-example-spi-extension/src/test/java/org/elasticsearch/example/realm/CustomRoleMappingRealmTests.java @@ -38,7 +38,8 @@ public void testCachingOfUserLookup() throws Exception { final RealmConfig realmConfig = new RealmConfig( realmIdentifier, Settings.builder().put(env.settings()).put(getFullSettingKey(realmIdentifier, RealmSettings.ORDER_SETTING), 0).build(), - env, new ThreadContext(env.settings()) + env, + new ThreadContext(env.settings()) ); CustomRoleMappingRealm realm = new CustomRoleMappingRealm(realmConfig, roleMapper); diff --git a/x-pack/qa/security-setup-password-tests/src/test/java/org/elasticsearch/xpack/security/authc/esnative/tool/ResetPasswordToolIT.java b/x-pack/qa/security-setup-password-tests/src/test/java/org/elasticsearch/xpack/security/authc/esnative/tool/ResetPasswordToolIT.java index 1e61eeb85f58e..d03ca72eeca6e 100644 --- a/x-pack/qa/security-setup-password-tests/src/test/java/org/elasticsearch/xpack/security/authc/esnative/tool/ResetPasswordToolIT.java +++ b/x-pack/qa/security-setup-password-tests/src/test/java/org/elasticsearch/xpack/security/authc/esnative/tool/ResetPasswordToolIT.java @@ -71,13 +71,18 @@ public void testResetNativeUser() throws Exception { try { Request putUserRequest = new Request("PUT", "/_security/user/" + nativeUser); putUserRequest.setJsonEntity( - "{\n" + - " \"password\" : \"l0ng-r4nd0m-p@ssw0rd\",\n" + - " \"roles\" : [ \"admin\", \"other_role1\" ],\n" + - " \"full_name\" : \"" + randomAlphaOfLength(5) + "\",\n" + - " \"email\" : \"" + nativeUser + "@example.com\",\n" + - " \"enabled\": true\n" + - "}"); + "{\n" + + " \"password\" : \"l0ng-r4nd0m-p@ssw0rd\",\n" + + " \"roles\" : [ \"admin\", \"other_role1\" ],\n" + + " \"full_name\" : \"" + + randomAlphaOfLength(5) + + "\",\n" + + " \"email\" : \"" + + nativeUser + + "@example.com\",\n" + + " \"enabled\": true\n" + + "}" + ); RequestOptions.Builder options = putUserRequest.getOptions().toBuilder(); options.addHeader("Authorization", adminBasicHeader); putUserRequest.setOptions(options); diff --git a/x-pack/qa/security-setup-password-tests/src/test/java/org/elasticsearch/xpack/security/authc/esnative/tool/SetupPasswordToolIT.java b/x-pack/qa/security-setup-password-tests/src/test/java/org/elasticsearch/xpack/security/authc/esnative/tool/SetupPasswordToolIT.java index 4f1682d6183e1..1f27b6a73a35b 100644 --- a/x-pack/qa/security-setup-password-tests/src/test/java/org/elasticsearch/xpack/security/authc/esnative/tool/SetupPasswordToolIT.java +++ b/x-pack/qa/security-setup-password-tests/src/test/java/org/elasticsearch/xpack/security/authc/esnative/tool/SetupPasswordToolIT.java @@ -51,8 +51,8 @@ public void testSetupPasswordToolAutoSetup() throws Exception { assertEquals(7, userPasswordMap.size()); userPasswordMap.entrySet().forEach(entry -> { - final String basicHeader = "Basic " + - Base64.getEncoder().encodeToString((entry.getKey() + ":" + entry.getValue()).getBytes(StandardCharsets.UTF_8)); + final String basicHeader = "Basic " + + Base64.getEncoder().encodeToString((entry.getKey() + ":" + entry.getValue()).getBytes(StandardCharsets.UTF_8)); try { Request request = new Request("GET", "/_security/_authenticate"); RequestOptions.Builder options = request.getOptions().toBuilder(); diff --git a/x-pack/qa/security-tools-tests/src/test/java/org/elasticsearch/xpack/security/authc/esnative/tool/ResetPasswordToolTests.java b/x-pack/qa/security-tools-tests/src/test/java/org/elasticsearch/xpack/security/authc/esnative/tool/ResetPasswordToolTests.java index a01d77f2dfcf0..330df329b4ded 100644 --- a/x-pack/qa/security-tools-tests/src/test/java/org/elasticsearch/xpack/security/authc/esnative/tool/ResetPasswordToolTests.java +++ b/x-pack/qa/security-tools-tests/src/test/java/org/elasticsearch/xpack/security/authc/esnative/tool/ResetPasswordToolTests.java @@ -264,12 +264,7 @@ public void testAutoBatchSilent() throws Exception { public void testInvalidInvocation() throws Exception { Exception e = expectThrows(Exception.class, () -> execute(randomFrom("-i", "--interactive"))); - assertThat( - e.getMessage(), - containsString( - "Missing required option(s) [u/username]" - ) - ); + assertThat(e.getMessage(), containsString("Missing required option(s) [u/username]")); assertThat(terminal.getOutput(), is(emptyString())); } diff --git a/x-pack/qa/security-tools-tests/src/test/java/org/elasticsearch/xpack/security/authc/file/tool/UsersToolTests.java b/x-pack/qa/security-tools-tests/src/test/java/org/elasticsearch/xpack/security/authc/file/tool/UsersToolTests.java index bf3f70d07198a..ccce1f97292cb 100644 --- a/x-pack/qa/security-tools-tests/src/test/java/org/elasticsearch/xpack/security/authc/file/tool/UsersToolTests.java +++ b/x-pack/qa/security-tools-tests/src/test/java/org/elasticsearch/xpack/security/authc/file/tool/UsersToolTests.java @@ -8,15 +8,16 @@ import com.google.common.jimfs.Configuration; import com.google.common.jimfs.Jimfs; -import org.elasticsearch.core.internal.io.IOUtils; + import org.elasticsearch.cli.Command; import org.elasticsearch.cli.CommandTestCase; import org.elasticsearch.cli.ExitCodes; import org.elasticsearch.cli.UserException; import org.elasticsearch.common.Strings; -import org.elasticsearch.core.PathUtilsForTesting; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.PathUtilsForTesting; +import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.env.Environment; import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.test.SecuritySettingsSourceField; @@ -74,29 +75,30 @@ public void setupHome() throws IOException { Files.createDirectories(confDir); hasher = getFastStoredHashAlgoForTests(); String defaultPassword = SecuritySettingsSourceField.TEST_PASSWORD; - Files.write(confDir.resolve("users"), Arrays.asList( - "existing_user:" + new String(hasher.hash(SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)), - "existing_user2:" + new String(hasher.hash(new SecureString((defaultPassword + "2").toCharArray()))), - "existing_user3:" + new String(hasher.hash(new SecureString((defaultPassword + "3").toCharArray()))) - ), StandardCharsets.UTF_8); - Files.write(confDir.resolve("users_roles"), Arrays.asList( - "test_admin:existing_user,existing_user2", - "test_r1:existing_user2" - ), StandardCharsets.UTF_8); - Files.write(confDir.resolve("roles.yml"), Arrays.asList( - "test_admin:", - " cluster: all", - "test_r1:", - " cluster: all", - "test_r2:", - " cluster: all" - ), StandardCharsets.UTF_8); - settings = - Settings.builder() - .put("path.home", homeDir) - .put("xpack.security.authc.realms.file.file.order", 0) - .put("xpack.security.authc.password_hashing.algorithm", hasher.name()) - .build(); + Files.write( + confDir.resolve("users"), + Arrays.asList( + "existing_user:" + new String(hasher.hash(SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)), + "existing_user2:" + new String(hasher.hash(new SecureString((defaultPassword + "2").toCharArray()))), + "existing_user3:" + new String(hasher.hash(new SecureString((defaultPassword + "3").toCharArray()))) + ), + StandardCharsets.UTF_8 + ); + Files.write( + confDir.resolve("users_roles"), + Arrays.asList("test_admin:existing_user,existing_user2", "test_r1:existing_user2"), + StandardCharsets.UTF_8 + ); + Files.write( + confDir.resolve("roles.yml"), + Arrays.asList("test_admin:", " cluster: all", "test_r1:", " cluster: all", "test_r2:", " cluster: all"), + StandardCharsets.UTF_8 + ); + settings = Settings.builder() + .put("path.home", homeDir) + .put("xpack.security.authc.realms.file.file.order", 0) + .put("xpack.security.authc.password_hashing.algorithm", hasher.name()) + .build(); pathHomeParameter = "-Epath.home=" + homeDir; fileOrderParameter = "-Expack.security.authc.realms.file.file.order=0"; } @@ -224,8 +226,7 @@ void assertRole(String role, String... users) throws IOException { List gotUsers = Arrays.asList(roleUsers[1].split(",")); for (String user : users) { if (gotUsers.contains(user) == false) { - fail("Expected users [" + Arrays.toString(users) + "] for role " + role + - " but found [" + gotUsers.toString() + "]"); + fail("Expected users [" + Arrays.toString(users) + "] for role " + role + " but found [" + gotUsers.toString() + "]"); } } return; @@ -236,18 +237,20 @@ void assertRole(String role, String... users) throws IOException { } public void testParseInvalidUsername() throws Exception { - UserException e = expectThrows(UserException.class, () -> { - UsersTool.parseUsername(Collections.singletonList("áccented"), Settings.EMPTY); - }); + UserException e = expectThrows( + UserException.class, + () -> { UsersTool.parseUsername(Collections.singletonList("áccented"), Settings.EMPTY); } + ); assertEquals(ExitCodes.DATA_ERROR, e.exitCode); assertTrue(e.getMessage(), e.getMessage().contains("Invalid username")); } public void testParseReservedUsername() throws Exception { final String name = randomFrom(ElasticUser.NAME, KibanaUser.NAME); - UserException e = expectThrows(UserException.class, () -> { - UsersTool.parseUsername(Collections.singletonList(name), Settings.EMPTY); - }); + UserException e = expectThrows( + UserException.class, + () -> { UsersTool.parseUsername(Collections.singletonList(name), Settings.EMPTY); } + ); assertEquals(ExitCodes.DATA_ERROR, e.exitCode); assertTrue(e.getMessage(), e.getMessage().contains("Invalid username")); @@ -256,34 +259,29 @@ public void testParseReservedUsername() throws Exception { } public void testParseUsernameMissing() throws Exception { - UserException e = expectThrows(UserException.class, () -> { - UsersTool.parseUsername(Collections.emptyList(), Settings.EMPTY); - }); + UserException e = expectThrows(UserException.class, () -> { UsersTool.parseUsername(Collections.emptyList(), Settings.EMPTY); }); assertEquals(ExitCodes.USAGE, e.exitCode); assertTrue(e.getMessage(), e.getMessage().contains("Missing username argument")); } public void testParseUsernameExtraArgs() throws Exception { - UserException e = expectThrows(UserException.class, () -> { - UsersTool.parseUsername(Arrays.asList("username", "extra"), Settings.EMPTY); - }); + UserException e = expectThrows( + UserException.class, + () -> { UsersTool.parseUsername(Arrays.asList("username", "extra"), Settings.EMPTY); } + ); assertEquals(ExitCodes.USAGE, e.exitCode); assertTrue(e.getMessage(), e.getMessage().contains("Expected a single username argument")); } public void testParseInvalidPasswordOption() throws Exception { - UserException e = expectThrows(UserException.class, () -> { - UsersTool.parsePassword(terminal, "123"); - }); + UserException e = expectThrows(UserException.class, () -> { UsersTool.parsePassword(terminal, "123"); }); assertEquals(ExitCodes.DATA_ERROR, e.exitCode); assertTrue(e.getMessage(), e.getMessage().contains("Invalid password")); } public void testParseInvalidPasswordInput() throws Exception { terminal.addSecretInput("123"); - UserException e = expectThrows(UserException.class, () -> { - UsersTool.parsePassword(terminal, null); - }); + UserException e = expectThrows(UserException.class, () -> { UsersTool.parsePassword(terminal, null); }); assertEquals(ExitCodes.DATA_ERROR, e.exitCode); assertTrue(e.getMessage(), e.getMessage().contains("Invalid password")); } @@ -291,9 +289,7 @@ public void testParseInvalidPasswordInput() throws Exception { public void testParseMismatchPasswordInput() throws Exception { terminal.addSecretInput("password1"); terminal.addSecretInput("password2"); - UserException e = expectThrows(UserException.class, () -> { - UsersTool.parsePassword(terminal, null); - }); + UserException e = expectThrows(UserException.class, () -> { UsersTool.parsePassword(terminal, null); }); assertEquals(ExitCodes.DATA_ERROR, e.exitCode); assertTrue(e.getMessage(), e.getMessage().contains("Password mismatch")); } @@ -313,9 +309,10 @@ public void testParseReservedRole() throws Exception { } public void testParseInvalidRole() throws Exception { - UserException e = expectThrows(UserException.class, () -> { - UsersTool.parseRoles(terminal, TestEnvironment.newEnvironment(settings), "fóóbár"); - }); + UserException e = expectThrows( + UserException.class, + () -> { UsersTool.parseRoles(terminal, TestEnvironment.newEnvironment(settings), "fóóbár"); } + ); assertEquals(ExitCodes.DATA_ERROR, e.exitCode); assertTrue(e.getMessage(), e.getMessage().contains("Invalid role [fóóbár]")); } @@ -340,18 +337,22 @@ public void testUseraddPasswordOption() throws Exception { } public void testUseraddUserExists() throws Exception { - UserException e = expectThrows(UserException.class, () -> { - execute("useradd", pathHomeParameter, fileOrderParameter, "existing_user", "-p", SecuritySettingsSourceField.TEST_PASSWORD); - }); + UserException e = expectThrows( + UserException.class, + () -> { + execute("useradd", pathHomeParameter, fileOrderParameter, "existing_user", "-p", SecuritySettingsSourceField.TEST_PASSWORD); + } + ); assertEquals(ExitCodes.CODE_ERROR, e.exitCode); assertEquals("User [existing_user] already exists", e.getMessage()); } public void testUseraddReservedUser() throws Exception { final String name = randomFrom(ElasticUser.NAME, KibanaUser.NAME); - UserException e = expectThrows(UserException.class, () -> { - execute("useradd", pathHomeParameter, fileOrderParameter, name, "-p", SecuritySettingsSourceField.TEST_PASSWORD); - }); + UserException e = expectThrows( + UserException.class, + () -> { execute("useradd", pathHomeParameter, fileOrderParameter, name, "-p", SecuritySettingsSourceField.TEST_PASSWORD); } + ); assertEquals(ExitCodes.DATA_ERROR, e.exitCode); assertEquals("Invalid username [" + name + "]... Username [" + name + "] is reserved and may not be used.", e.getMessage()); } @@ -365,26 +366,38 @@ public void testUseraddNoRoles() throws Exception { } public void testAddUserWithInvalidHashingAlgorithmInFips() throws Exception { - settings = - Settings.builder() - .put(settings) - .put("xpack.security.authc.password_hashing.algorithm", "bcrypt") - .put("xpack.security.fips_mode.enabled", true) - .build(); - - UserException e = expectThrows(UserException.class, () -> { - execute("useradd", pathHomeParameter, fileOrderParameter, randomAlphaOfLength(12), "-p", - SecuritySettingsSourceField.TEST_PASSWORD); - }); + settings = Settings.builder() + .put(settings) + .put("xpack.security.authc.password_hashing.algorithm", "bcrypt") + .put("xpack.security.fips_mode.enabled", true) + .build(); + + UserException e = expectThrows( + UserException.class, + () -> { + execute( + "useradd", + pathHomeParameter, + fileOrderParameter, + randomAlphaOfLength(12), + "-p", + SecuritySettingsSourceField.TEST_PASSWORD + ); + } + ); assertEquals(ExitCodes.CONFIG, e.exitCode); - assertEquals("Only PBKDF2 is allowed for password hashing in a FIPS 140 JVM. " + - "Please set the appropriate value for [ xpack.security.authc.password_hashing.algorithm ] setting.", e.getMessage()); + assertEquals( + "Only PBKDF2 is allowed for password hashing in a FIPS 140 JVM. " + + "Please set the appropriate value for [ xpack.security.authc.password_hashing.algorithm ] setting.", + e.getMessage() + ); } public void testUserdelUnknownUser() throws Exception { - UserException e = expectThrows(UserException.class, () -> { - execute("userdel", pathHomeParameter, fileOrderParameter, "unknown"); - }); + UserException e = expectThrows( + UserException.class, + () -> { execute("userdel", pathHomeParameter, fileOrderParameter, "unknown"); } + ); assertEquals(ExitCodes.NO_USER, e.exitCode); assertTrue(e.getMessage(), e.getMessage().contains("User [unknown] doesn't exist")); } @@ -395,9 +408,10 @@ public void testUserdel() throws Exception { } public void testPasswdUnknownUser() throws Exception { - UserException e = expectThrows(UserException.class, () -> { - execute("passwd", pathHomeParameter, fileOrderParameter, "unknown", "-p", SecuritySettingsSourceField.TEST_PASSWORD); - }); + UserException e = expectThrows( + UserException.class, + () -> { execute("passwd", pathHomeParameter, fileOrderParameter, "unknown", "-p", SecuritySettingsSourceField.TEST_PASSWORD); } + ); assertEquals(ExitCodes.NO_USER, e.exitCode); assertTrue(e.getMessage(), e.getMessage().contains("User [unknown] doesn't exist")); } @@ -417,24 +431,25 @@ public void testPasswd() throws Exception { } public void testPasswdWithInvalidHashingAlgorithmInFips() throws Exception { - settings = - Settings.builder() - .put(settings) - .put("xpack.security.authc.password_hashing.algorithm", "bcrypt") - .put("xpack.security.fips_mode.enabled", true) - .build(); - UserException e = expectThrows(UserException.class, () -> { - execute("passwd", pathHomeParameter, fileOrderParameter, "existing_user", "-p", "new-test-user-password"); - }); + settings = Settings.builder() + .put(settings) + .put("xpack.security.authc.password_hashing.algorithm", "bcrypt") + .put("xpack.security.fips_mode.enabled", true) + .build(); + UserException e = expectThrows( + UserException.class, + () -> { execute("passwd", pathHomeParameter, fileOrderParameter, "existing_user", "-p", "new-test-user-password"); } + ); assertEquals(ExitCodes.CONFIG, e.exitCode); - assertEquals("Only PBKDF2 is allowed for password hashing in a FIPS 140 JVM. " + - "Please set the appropriate value for [ xpack.security.authc.password_hashing.algorithm ] setting.", e.getMessage()); + assertEquals( + "Only PBKDF2 is allowed for password hashing in a FIPS 140 JVM. " + + "Please set the appropriate value for [ xpack.security.authc.password_hashing.algorithm ] setting.", + e.getMessage() + ); } public void testRolesUnknownUser() throws Exception { - UserException e = expectThrows(UserException.class, () -> { - execute("roles", pathHomeParameter, fileOrderParameter, "unknown"); - }); + UserException e = expectThrows(UserException.class, () -> { execute("roles", pathHomeParameter, fileOrderParameter, "unknown"); }); assertEquals(ExitCodes.NO_USER, e.exitCode); assertTrue(e.getMessage(), e.getMessage().contains("User [unknown] doesn't exist")); } @@ -457,8 +472,16 @@ public void testRolesAddAndRemove() throws Exception { } public void testRolesRemoveLeavesExisting() throws Exception { - execute("useradd", pathHomeParameter, fileOrderParameter, "username", "-p", SecuritySettingsSourceField.TEST_PASSWORD, - "-r", "test_admin"); + execute( + "useradd", + pathHomeParameter, + fileOrderParameter, + "username", + "-p", + SecuritySettingsSourceField.TEST_PASSWORD, + "-r", + "test_admin" + ); execute("roles", pathHomeParameter, fileOrderParameter, "existing_user", "-r", "test_admin"); assertRole("test_admin", "username"); } @@ -470,9 +493,7 @@ public void testRolesNoAddOrRemove() throws Exception { } public void testListUnknownUser() throws Exception { - UserException e = expectThrows(UserException.class, () -> { - execute("list", pathHomeParameter, fileOrderParameter, "unknown"); - }); + UserException e = expectThrows(UserException.class, () -> { execute("list", pathHomeParameter, fileOrderParameter, "unknown"); }); assertEquals(ExitCodes.NO_USER, e.exitCode); assertTrue(e.getMessage(), e.getMessage().contains("User [unknown] doesn't exist")); } @@ -500,8 +521,16 @@ public void testListSingleUser() throws Exception { } public void testListUnknownRoles() throws Exception { - execute("useradd", pathHomeParameter, fileOrderParameter, "username", "-p", SecuritySettingsSourceField.TEST_PASSWORD, - "-r", "test_r1,r2,r3"); + execute( + "useradd", + pathHomeParameter, + fileOrderParameter, + "username", + "-p", + SecuritySettingsSourceField.TEST_PASSWORD, + "-r", + "test_r1,r2,r3" + ); String output = execute("list", pathHomeParameter, fileOrderParameter, "username"); assertTrue(output, output.contains("username")); assertTrue(output, output.contains("r2*,r3*,test_r1")); @@ -531,9 +560,12 @@ public void testUserAddNoConfig() throws Exception { IOUtils.rm(confDir.resolve("users")); pathHomeParameter = "-Epath.home=" + homeDir; fileOrderParameter = "-Expack.security.authc.realms.file.file.order=0"; - UserException e = expectThrows(UserException.class, () -> { - execute("useradd", pathHomeParameter, fileOrderParameter, "username", "-p", SecuritySettingsSourceField.TEST_PASSWORD); - }); + UserException e = expectThrows( + UserException.class, + () -> { + execute("useradd", pathHomeParameter, fileOrderParameter, "username", "-p", SecuritySettingsSourceField.TEST_PASSWORD); + } + ); assertEquals(ExitCodes.CONFIG, e.exitCode); assertThat(e.getMessage(), containsString("Configuration file [/work/eshome/config/users] is missing")); } @@ -543,9 +575,7 @@ public void testUserListNoConfig() throws Exception { IOUtils.rm(confDir.resolve("users")); pathHomeParameter = "-Epath.home=" + homeDir; fileOrderParameter = "-Expack.security.authc.realms.file.file.order=0"; - UserException e = expectThrows(UserException.class, () -> { - execute("list", pathHomeParameter, fileOrderParameter); - }); + UserException e = expectThrows(UserException.class, () -> { execute("list", pathHomeParameter, fileOrderParameter); }); assertEquals(ExitCodes.CONFIG, e.exitCode); assertThat(e.getMessage(), containsString("Configuration file [/work/eshome/config/users] is missing")); } @@ -555,9 +585,10 @@ public void testUserDelNoConfig() throws Exception { IOUtils.rm(confDir.resolve("users")); pathHomeParameter = "-Epath.home=" + homeDir; fileOrderParameter = "-Expack.security.authc.realms.file.file.order=0"; - UserException e = expectThrows(UserException.class, () -> { - execute("userdel", pathHomeParameter, fileOrderParameter, "username"); - }); + UserException e = expectThrows( + UserException.class, + () -> { execute("userdel", pathHomeParameter, fileOrderParameter, "username"); } + ); assertEquals(ExitCodes.CONFIG, e.exitCode); assertThat(e.getMessage(), containsString("Configuration file [/work/eshome/config/users] is missing")); } @@ -567,9 +598,7 @@ public void testListUserRolesNoConfig() throws Exception { IOUtils.rm(confDir.resolve("users_roles")); pathHomeParameter = "-Epath.home=" + homeDir; fileOrderParameter = "-Expack.security.authc.realms.file.file.order=0"; - UserException e = expectThrows(UserException.class, () -> { - execute("roles", pathHomeParameter, fileOrderParameter, "username"); - }); + UserException e = expectThrows(UserException.class, () -> { execute("roles", pathHomeParameter, fileOrderParameter, "username"); }); assertEquals(ExitCodes.CONFIG, e.exitCode); assertThat(e.getMessage(), containsString("Configuration file [/work/eshome/config/users_roles] is missing")); } diff --git a/x-pack/qa/security-tools-tests/src/test/java/org/elasticsearch/xpack/security/authc/service/FileTokensToolTests.java b/x-pack/qa/security-tools-tests/src/test/java/org/elasticsearch/xpack/security/authc/service/FileTokensToolTests.java index 26c1a1e9244f9..823791ee243e6 100644 --- a/x-pack/qa/security-tools-tests/src/test/java/org/elasticsearch/xpack/security/authc/service/FileTokensToolTests.java +++ b/x-pack/qa/security-tools-tests/src/test/java/org/elasticsearch/xpack/security/authc/service/FileTokensToolTests.java @@ -9,13 +9,14 @@ import com.google.common.jimfs.Configuration; import com.google.common.jimfs.Jimfs; + import org.elasticsearch.cli.Command; import org.elasticsearch.cli.CommandTestCase; import org.elasticsearch.cli.UserException; import org.elasticsearch.common.UUIDs; -import org.elasticsearch.core.PathUtilsForTesting; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.PathUtilsForTesting; import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.env.Environment; import org.elasticsearch.xpack.core.security.authc.support.Hasher; @@ -72,11 +73,14 @@ public void setupHome() throws IOException { Files.createDirectories(confDir); hasher = getFastStoredHashAlgoForTests(); - Files.write(confDir.resolve("service_tokens"), List.of( - "elastic/fleet-server/server_1:" + new String(hasher.hash(token1)), - "elastic/fleet-server/server_2:" + new String(hasher.hash(token2)), - "elastic/fleet-server/server_3:" + new String(hasher.hash(token3)) - )); + Files.write( + confDir.resolve("service_tokens"), + List.of( + "elastic/fleet-server/server_1:" + new String(hasher.hash(token1)), + "elastic/fleet-server/server_2:" + new String(hasher.hash(token2)), + "elastic/fleet-server/server_3:" + new String(hasher.hash(token3)) + ) + ); settings = Settings.builder() .put("path.home", homeDir) .put("xpack.security.authc.service_token_hashing.algorithm", hasher.name()) @@ -130,33 +134,43 @@ protected Environment createEnv(Map settings) throws UserExcepti public void testParsePrincipalAndTokenName() throws UserException { final String tokenName1 = randomAlphaOfLengthBetween(3, 8); - final ServiceAccountTokenId accountTokenId = - FileTokensTool.parsePrincipalAndTokenName(List.of("elastic/fleet-server", tokenName1), Settings.EMPTY); + final ServiceAccountTokenId accountTokenId = FileTokensTool.parsePrincipalAndTokenName( + List.of("elastic/fleet-server", tokenName1), + Settings.EMPTY + ); assertEquals("elastic/fleet-server", accountTokenId.getAccountId().asPrincipal()); assertEquals(tokenName1, accountTokenId.getTokenName()); - final UserException e2 = expectThrows(UserException.class, - () -> FileTokensTool.parsePrincipalAndTokenName(List.of(randomAlphaOfLengthBetween(6, 16)), Settings.EMPTY)); + final UserException e2 = expectThrows( + UserException.class, + () -> FileTokensTool.parsePrincipalAndTokenName(List.of(randomAlphaOfLengthBetween(6, 16)), Settings.EMPTY) + ); assertThat(e2.getMessage(), containsString("Missing token-name argument")); - final UserException e3 = expectThrows(UserException.class, - () -> FileTokensTool.parsePrincipalAndTokenName(List.of(), Settings.EMPTY)); + final UserException e3 = expectThrows( + UserException.class, + () -> FileTokensTool.parsePrincipalAndTokenName(List.of(), Settings.EMPTY) + ); assertThat(e3.getMessage(), containsString("Missing service-account-principal and token-name arguments")); - final UserException e4 = expectThrows(UserException.class, + final UserException e4 = expectThrows( + UserException.class, () -> FileTokensTool.parsePrincipalAndTokenName( List.of(randomAlphaOfLengthBetween(6, 16), randomAlphaOfLengthBetween(3, 8), randomAlphaOfLengthBetween(3, 8)), - Settings.EMPTY)); - assertThat(e4.getMessage(), containsString( - "Expected two arguments, service-account-principal and token-name, found extra:")); + Settings.EMPTY + ) + ); + assertThat(e4.getMessage(), containsString("Expected two arguments, service-account-principal and token-name, found extra:")); } public void testCreateToken() throws Exception { final String tokenName1 = randomValueOtherThanMany(n -> n.startsWith("-"), ValidationTests::randomTokenName); execute("create", pathHomeParameter, "elastic/fleet-server", tokenName1); assertServiceTokenExists("elastic/fleet-server/" + tokenName1); - final String tokenName2 = randomValueOtherThanMany(n -> n.startsWith("-") || n.equals(tokenName1), - ValidationTests::randomTokenName); + final String tokenName2 = randomValueOtherThanMany( + n -> n.startsWith("-") || n.equals(tokenName1), + ValidationTests::randomTokenName + ); execute("create", pathHomeParameter, "elastic/fleet-server", tokenName2); assertServiceTokenExists("elastic/fleet-server/" + tokenName2); // token name with a leading hyphen requires an option terminator @@ -171,9 +185,9 @@ public void testCreateToken() throws Exception { public void testCreateTokenWithInvalidTokenName() throws Exception { final String tokenName = ValidationTests.randomInvalidTokenName(); - final String[] args = tokenName.startsWith("-") ? - new String[] { "create", pathHomeParameter, "elastic/fleet-server", "--", tokenName } : - new String[] { "create", pathHomeParameter, "elastic/fleet-server", tokenName }; + final String[] args = tokenName.startsWith("-") + ? new String[] { "create", pathHomeParameter, "elastic/fleet-server", "--", tokenName } + : new String[] { "create", pathHomeParameter, "elastic/fleet-server", tokenName }; final UserException e = expectThrows(UserException.class, () -> execute(args)); assertServiceTokenNotExists("elastic/fleet-server/" + tokenName); assertThat(e.getMessage(), containsString(Validation.INVALID_SERVICE_ACCOUNT_TOKEN_NAME_MESSAGE)); @@ -181,10 +195,15 @@ public void testCreateTokenWithInvalidTokenName() throws Exception { } public void testCreateTokenWithInvalidServiceAccount() throws Exception { - final UserException e = expectThrows(UserException.class, - () -> execute("create", pathHomeParameter, + final UserException e = expectThrows( + UserException.class, + () -> execute( + "create", + pathHomeParameter, randomFrom("elastic/foo", "foo/fleet-server", randomAlphaOfLengthBetween(6, 16)), - randomAlphaOfLengthBetween(3, 8))); + randomAlphaOfLengthBetween(3, 8) + ) + ); assertThat(e.getMessage(), containsString("Unknown service account principal: ")); assertThat(e.getMessage(), containsString("Must be one of ")); } @@ -200,18 +219,23 @@ public void testDeleteToken() throws Exception { public void testDeleteTokenIncorrect() throws IOException { // Invalid principal - final UserException e1 = expectThrows(UserException.class, - () -> execute("delete", pathHomeParameter, + final UserException e1 = expectThrows( + UserException.class, + () -> execute( + "delete", + pathHomeParameter, randomFrom("elastic/foo", "foo/fleet-server", randomAlphaOfLengthBetween(6, 16)), - randomAlphaOfLengthBetween(3, 8))); + randomAlphaOfLengthBetween(3, 8) + ) + ); assertThat(e1.getMessage(), containsString("Unknown service account principal: ")); assertThat(e1.getMessage(), containsString("Must be one of ")); // Invalid token name final String tokenName2 = ValidationTests.randomInvalidTokenName(); - final String[] args = tokenName2.startsWith("-") ? - new String[] { "delete", pathHomeParameter, "elastic/fleet-server", "--", tokenName2 } : - new String[] { "delete", pathHomeParameter, "elastic/fleet-server", tokenName2 }; + final String[] args = tokenName2.startsWith("-") + ? new String[] { "delete", pathHomeParameter, "elastic/fleet-server", "--", tokenName2 } + : new String[] { "delete", pathHomeParameter, "elastic/fleet-server", tokenName2 }; final UserException e2 = expectThrows(UserException.class, () -> execute(args)); assertThat(e2.getMessage(), containsString(Validation.INVALID_SERVICE_ACCOUNT_TOKEN_NAME_MESSAGE)); assertThat(e2.getMessage(), containsString("invalid service token name [" + tokenName2 + "]")); @@ -223,8 +247,10 @@ public void testDeleteTokenIncorrect() throws IOException { Files.delete(serviceTokensFile); } final String tokenName3 = randomAlphaOfLengthBetween(3, 8); - final UserException e3 = expectThrows(UserException.class, - () -> execute("delete", pathHomeParameter, "elastic/fleet-server", tokenName3)); + final UserException e3 = expectThrows( + UserException.class, + () -> execute("delete", pathHomeParameter, "elastic/fleet-server", tokenName3) + ); assertThat(e3.getMessage(), containsString("Service token [elastic/fleet-server/" + tokenName3 + "] does not exist")); if (fileDeleted) { // The file should not be created if not exists in the first place @@ -235,24 +261,27 @@ public void testDeleteTokenIncorrect() throws IOException { public void testListTokens() throws Exception { execute("list", pathHomeParameter); final String output = terminal.getOutput(); - assertThat(output, containsString("elastic/fleet-server/server_1\n" + - "elastic/fleet-server/server_2\n" + - "elastic/fleet-server/server_3")); + assertThat( + output, + containsString("elastic/fleet-server/server_1\n" + "elastic/fleet-server/server_2\n" + "elastic/fleet-server/server_3") + ); } public void testListTokensByPrincipal() throws Exception { execute("list", pathHomeParameter, "elastic/fleet-server"); final String output = terminal.getOutput(); - assertThat(output, containsString("elastic/fleet-server/server_1\n" + - "elastic/fleet-server/server_2\n" + - "elastic/fleet-server/server_3")); + assertThat( + output, + containsString("elastic/fleet-server/server_1\n" + "elastic/fleet-server/server_2\n" + "elastic/fleet-server/server_3") + ); } public void testListTokensNonExist() throws Exception { // Invalid principal - final UserException e1 = expectThrows(UserException.class, - () -> execute("list", pathHomeParameter, - randomFrom("elastic/foo", "foo/fleet-server", randomAlphaOfLengthBetween(6, 16)))); + final UserException e1 = expectThrows( + UserException.class, + () -> execute("list", pathHomeParameter, randomFrom("elastic/foo", "foo/fleet-server", randomAlphaOfLengthBetween(6, 16))) + ); assertThat(e1.getMessage(), containsString("Unknown service account principal: ")); assertThat(e1.getMessage(), containsString("Must be one of ")); diff --git a/x-pack/qa/security-tools-tests/src/test/java/org/elasticsearch/xpack/security/crypto/tool/SystemKeyToolTests.java b/x-pack/qa/security-tools-tests/src/test/java/org/elasticsearch/xpack/security/crypto/tool/SystemKeyToolTests.java index 73262e00ec23a..889748acd5bd5 100644 --- a/x-pack/qa/security-tools-tests/src/test/java/org/elasticsearch/xpack/security/crypto/tool/SystemKeyToolTests.java +++ b/x-pack/qa/security-tools-tests/src/test/java/org/elasticsearch/xpack/security/crypto/tool/SystemKeyToolTests.java @@ -8,12 +8,13 @@ import com.google.common.jimfs.Configuration; import com.google.common.jimfs.Jimfs; -import org.elasticsearch.core.internal.io.IOUtils; + import org.elasticsearch.cli.Command; import org.elasticsearch.cli.CommandTestCase; import org.elasticsearch.cli.UserException; -import org.elasticsearch.core.PathUtilsForTesting; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.PathUtilsForTesting; +import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.env.Environment; import org.elasticsearch.env.TestEnvironment; import org.junit.After; @@ -50,7 +51,7 @@ protected Command newCommand() { @Override protected Environment createEnv(Map settings) throws UserException { Settings.Builder builder = Settings.builder(); - settings.forEach((k,v) -> builder.put(k, v)); + settings.forEach((k, v) -> builder.put(k, v)); return TestEnvironment.newEnvironment(builder.build()); } diff --git a/x-pack/qa/security-tools-tests/src/test/java/org/elasticsearch/xpack/security/enrollment/tool/AutoConfigGenerateElasticPasswordHashTests.java b/x-pack/qa/security-tools-tests/src/test/java/org/elasticsearch/xpack/security/enrollment/tool/AutoConfigGenerateElasticPasswordHashTests.java index b9b56f0ec8ea5..fc84bfde31ac9 100644 --- a/x-pack/qa/security-tools-tests/src/test/java/org/elasticsearch/xpack/security/enrollment/tool/AutoConfigGenerateElasticPasswordHashTests.java +++ b/x-pack/qa/security-tools-tests/src/test/java/org/elasticsearch/xpack/security/enrollment/tool/AutoConfigGenerateElasticPasswordHashTests.java @@ -82,7 +82,8 @@ public static void closeJimfs() throws IOException { } } - @Override protected Command newCommand() { + @Override + protected Command newCommand() { return new AutoConfigGenerateElasticPasswordHash() { @Override protected Environment createEnv(Map settings) throws UserException { @@ -97,8 +98,7 @@ public void testSuccessfullyGenerateAndStoreHash() throws Exception { KeyStoreWrapper keyStoreWrapper = KeyStoreWrapper.load(env.configFile()); assertNotNull(keyStoreWrapper); keyStoreWrapper.decrypt(new char[0]); - assertThat(keyStoreWrapper.getSettingNames(), - containsInAnyOrder(AUTOCONFIG_ELASTIC_PASSWORD_HASH.getKey(), "keystore.seed")); + assertThat(keyStoreWrapper.getSettingNames(), containsInAnyOrder(AUTOCONFIG_ELASTIC_PASSWORD_HASH.getKey(), "keystore.seed")); } public void testExistingKeystoreWithWrongPassword() throws Exception { diff --git a/x-pack/qa/security-tools-tests/src/test/java/org/elasticsearch/xpack/security/enrollment/tool/BaseRunAsSuperuserCommandTests.java b/x-pack/qa/security-tools-tests/src/test/java/org/elasticsearch/xpack/security/enrollment/tool/BaseRunAsSuperuserCommandTests.java index 70b668ffe0552..66986cf160dce 100644 --- a/x-pack/qa/security-tools-tests/src/test/java/org/elasticsearch/xpack/security/enrollment/tool/BaseRunAsSuperuserCommandTests.java +++ b/x-pack/qa/security-tools-tests/src/test/java/org/elasticsearch/xpack/security/enrollment/tool/BaseRunAsSuperuserCommandTests.java @@ -7,11 +7,11 @@ package org.elasticsearch.xpack.security.enrollment.tool; +import joptsimple.OptionSet; + import com.google.common.jimfs.Configuration; import com.google.common.jimfs.Jimfs; -import joptsimple.OptionSet; - import org.elasticsearch.Version; import org.elasticsearch.cli.Command; import org.elasticsearch.cli.CommandTestCase; @@ -27,10 +27,10 @@ import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.env.Environment; import org.elasticsearch.xpack.core.XPackSettings; -import org.elasticsearch.xpack.core.security.authc.support.Hasher; -import org.elasticsearch.xpack.security.tool.BaseRunAsSuperuserCommand; import org.elasticsearch.xpack.core.security.CommandLineHttpClient; import org.elasticsearch.xpack.core.security.HttpResponse; +import org.elasticsearch.xpack.core.security.authc.support.Hasher; +import org.elasticsearch.xpack.security.tool.BaseRunAsSuperuserCommand; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; @@ -111,10 +111,7 @@ public void setup() throws Exception { when(client.getDefaultURL()).thenReturn("https://localhost:9200"); URL url = new URL(client.getDefaultURL()); - HttpResponse healthResponse = new HttpResponse( - HttpURLConnection.HTTP_OK, - Map.of("status", randomFrom("yellow", "green")) - ); + HttpResponse healthResponse = new HttpResponse(HttpURLConnection.HTTP_OK, Map.of("status", randomFrom("yellow", "green"))); when( client.execute( anyString(), @@ -167,37 +164,58 @@ public void testUsersRolesFileIsMissing() throws Exception { final Path rolesPath = confDir.resolve("users_roles"); Files.delete(rolesPath); UserException e = expectThrows(UserException.class, this::execute); - assertThat(e.getMessage(), - equalTo("File realm configuration file [/work/" + rolesPath + "] is missing")); + assertThat(e.getMessage(), equalTo("File realm configuration file [/work/" + rolesPath + "] is missing")); assertThat(terminal.getOutput(), is(emptyString())); } public void testUnhealthyCluster() throws Exception { URL url = new URL(client.getDefaultURL()); - HttpResponse healthResponse = - new HttpResponse(HttpURLConnection.HTTP_OK, Map.of("status", randomFrom("red"))); - when(client.execute(anyString(), eq(clusterHealthUrl(url)), anyString(), any(SecureString.class), any(CheckedSupplier.class), - any(CheckedFunction.class))).thenReturn(healthResponse); + HttpResponse healthResponse = new HttpResponse(HttpURLConnection.HTTP_OK, Map.of("status", randomFrom("red"))); + when( + client.execute( + anyString(), + eq(clusterHealthUrl(url)), + anyString(), + any(SecureString.class), + any(CheckedSupplier.class), + any(CheckedFunction.class) + ) + ).thenReturn(healthResponse); UserException e = expectThrows(UserException.class, this::execute); assertThat(e.exitCode, equalTo(ExitCodes.UNAVAILABLE)); assertThat(e.getMessage(), containsString("RED")); assertThat(terminal.getOutput(), is(emptyString())); String error = terminal.getErrorOutput(); - assertThat(error, stringContainsInOrder("Failed to determine the health of the cluster. Cluster health is currently RED.", - "This means that some cluster data is unavailable and your cluster is not fully functional.", - "The cluster logs (https://www.elastic.co/guide/en/elasticsearch/reference/" - + Version.CURRENT.major + "." + Version.CURRENT.minor + "/logging.html)" + - " might contain information/indications for the underlying cause")); + assertThat( + error, + stringContainsInOrder( + "Failed to determine the health of the cluster. Cluster health is currently RED.", + "This means that some cluster data is unavailable and your cluster is not fully functional.", + "The cluster logs (https://www.elastic.co/guide/en/elasticsearch/reference/" + + Version.CURRENT.major + + "." + + Version.CURRENT.minor + + "/logging.html)" + + " might contain information/indications for the underlying cause" + ) + ); assertNoUsers(); assertNoUsersRoles(); } public void testUnhealthyClusterWithForce() throws Exception { URL url = new URL(client.getDefaultURL()); - HttpResponse healthResponse = - new HttpResponse(HttpURLConnection.HTTP_OK, Map.of("status", randomFrom("red"))); - when(client.execute(anyString(), eq(clusterHealthUrl(url)), anyString(), any(SecureString.class), any(CheckedSupplier.class), - any(CheckedFunction.class))).thenReturn(healthResponse); + HttpResponse healthResponse = new HttpResponse(HttpURLConnection.HTTP_OK, Map.of("status", randomFrom("red"))); + when( + client.execute( + anyString(), + eq(clusterHealthUrl(url)), + anyString(), + any(SecureString.class), + any(CheckedSupplier.class), + any(CheckedFunction.class) + ) + ).thenReturn(healthResponse); execute("-f"); assertThat(terminal.getOutput(), is(emptyString())); assertThat(terminal.getErrorOutput(), is(emptyString())); @@ -207,15 +225,24 @@ public void testUnhealthyClusterWithForce() throws Exception { public void testWillRetryOnUnauthorized() throws Exception { URL url = new URL(client.getDefaultURL()); - HttpResponse unauthorizedResponse = - new HttpResponse(HttpURLConnection.HTTP_UNAUTHORIZED, Map.of()); - when(client.execute(anyString(), eq(clusterHealthUrl(url)), anyString(), any(SecureString.class), any(CheckedSupplier.class), - any(CheckedFunction.class))).thenReturn(unauthorizedResponse); + HttpResponse unauthorizedResponse = new HttpResponse(HttpURLConnection.HTTP_UNAUTHORIZED, Map.of()); + when( + client.execute( + anyString(), + eq(clusterHealthUrl(url)), + anyString(), + any(SecureString.class), + any(CheckedSupplier.class), + any(CheckedFunction.class) + ) + ).thenReturn(unauthorizedResponse); UserException e = expectThrows(UserException.class, () -> execute("--verbose")); String verboseOutput = terminal.getOutput(); assertThat(verboseOutput.split("\\n").length, equalTo(5)); - assertThat(verboseOutput, - containsString("Unexpected http status [401] while attempting to determine cluster health. Will retry at most")); + assertThat( + verboseOutput, + containsString("Unexpected http status [401] while attempting to determine cluster health. Will retry at most") + ); assertThat(e.exitCode, equalTo(ExitCodes.DATA_ERROR)); assertNoUsers(); assertNoUsersRoles(); diff --git a/x-pack/qa/security-tools-tests/src/test/java/org/elasticsearch/xpack/security/enrollment/tool/CreateEnrollmentTokenToolTests.java b/x-pack/qa/security-tools-tests/src/test/java/org/elasticsearch/xpack/security/enrollment/tool/CreateEnrollmentTokenToolTests.java index ce7b815ece5cf..16c7120ab3a76 100644 --- a/x-pack/qa/security-tools-tests/src/test/java/org/elasticsearch/xpack/security/enrollment/tool/CreateEnrollmentTokenToolTests.java +++ b/x-pack/qa/security-tools-tests/src/test/java/org/elasticsearch/xpack/security/enrollment/tool/CreateEnrollmentTokenToolTests.java @@ -23,10 +23,10 @@ import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.env.Environment; import org.elasticsearch.xpack.core.XPackSettings; -import org.elasticsearch.xpack.core.security.EnrollmentToken; -import org.elasticsearch.xpack.security.enrollment.ExternalEnrollmentTokenGenerator; import org.elasticsearch.xpack.core.security.CommandLineHttpClient; +import org.elasticsearch.xpack.core.security.EnrollmentToken; import org.elasticsearch.xpack.core.security.HttpResponse; +import org.elasticsearch.xpack.security.enrollment.ExternalEnrollmentTokenGenerator; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; @@ -66,8 +66,11 @@ public class CreateEnrollmentTokenToolTests extends CommandTestCase { @Override protected Command newCommand() { - return new CreateEnrollmentTokenTool(environment -> client, environment -> keyStoreWrapper, - environment -> externalEnrollmentTokenGenerator) { + return new CreateEnrollmentTokenTool( + environment -> client, + environment -> keyStoreWrapper, + environment -> externalEnrollmentTokenGenerator + ) { @Override protected Environment createEnv(Map settings) { return new Environment(CreateEnrollmentTokenToolTests.this.settings, confDir); @@ -76,7 +79,7 @@ protected Environment createEnv(Map settings) { } @BeforeClass - public static void muteInFips(){ + public static void muteInFips() { assumeFalse("Enrollment mode is not supported in FIPS mode.", inFipsJvm()); } @@ -95,11 +98,8 @@ public void setup() throws Exception { confDir = homeDir.resolve("config"); Files.createDirectories(confDir); Files.write(confDir.resolve("users"), List.of(), StandardCharsets.UTF_8); - Files.write(confDir.resolve("users_roles"), List.of(), StandardCharsets.UTF_8); - settings = Settings.builder() - .put("path.home", homeDir) - .put("xpack.security.enrollment.enabled", true) - .build(); + Files.write(confDir.resolve("users_roles"), List.of(), StandardCharsets.UTF_8); + settings = Settings.builder().put("path.home", homeDir).put("xpack.security.enrollment.enabled", true).build(); pathHomeParameter = "-Epath.home=" + homeDir; this.keyStoreWrapper = mock(KeyStoreWrapper.class); @@ -109,22 +109,33 @@ public void setup() throws Exception { when(client.getDefaultURL()).thenReturn("https://localhost:9200"); URL url = new URL(client.getDefaultURL()); - HttpResponse healthResponse = - new HttpResponse(HttpURLConnection.HTTP_OK, Map.of("status", randomFrom("yellow", "green"))); - when(client.execute(anyString(), eq(clusterHealthUrl(url)), anyString(), any(SecureString.class), any(CheckedSupplier.class), - any(CheckedFunction.class))).thenReturn(healthResponse); + HttpResponse healthResponse = new HttpResponse(HttpURLConnection.HTTP_OK, Map.of("status", randomFrom("yellow", "green"))); + when( + client.execute( + anyString(), + eq(clusterHealthUrl(url)), + anyString(), + any(SecureString.class), + any(CheckedSupplier.class), + any(CheckedFunction.class) + ) + ).thenReturn(healthResponse); this.externalEnrollmentTokenGenerator = mock(ExternalEnrollmentTokenGenerator.class); - EnrollmentToken kibanaToken = new EnrollmentToken("DR6CzXkBDf8amV_48yYX:x3YqU_rqQwm-ESrkExcnOg", - "ce480d53728605674fcfd8ffb51000d8a33bf32de7c7f1e26b4d428f8a91362d", "8.0.0", - Arrays.asList("[192.168.0.1:9201, 172.16.254.1:9202")); - EnrollmentToken nodeToken = new EnrollmentToken("DR6CzXkBDf8amV_48yYX:4BhUk-mkFm-AwvRFg90KJ", - "ce480d53728605674fcfd8ffb51000d8a33bf32de7c7f1e26b4d428f8a91362d", "8.0.0", - Arrays.asList("[192.168.0.1:9201, 172.16.254.1:9202")); - when(externalEnrollmentTokenGenerator.createKibanaEnrollmentToken(anyString(), any(SecureString.class))) - .thenReturn(kibanaToken); - when(externalEnrollmentTokenGenerator.createNodeEnrollmentToken(anyString(), any(SecureString.class))) - .thenReturn(nodeToken); + EnrollmentToken kibanaToken = new EnrollmentToken( + "DR6CzXkBDf8amV_48yYX:x3YqU_rqQwm-ESrkExcnOg", + "ce480d53728605674fcfd8ffb51000d8a33bf32de7c7f1e26b4d428f8a91362d", + "8.0.0", + Arrays.asList("[192.168.0.1:9201, 172.16.254.1:9202") + ); + EnrollmentToken nodeToken = new EnrollmentToken( + "DR6CzXkBDf8amV_48yYX:4BhUk-mkFm-AwvRFg90KJ", + "ce480d53728605674fcfd8ffb51000d8a33bf32de7c7f1e26b4d428f8a91362d", + "8.0.0", + Arrays.asList("[192.168.0.1:9201, 172.16.254.1:9202") + ); + when(externalEnrollmentTokenGenerator.createKibanaEnrollmentToken(anyString(), any(SecureString.class))).thenReturn(kibanaToken); + when(externalEnrollmentTokenGenerator.createNodeEnrollmentToken(anyString(), any(SecureString.class))).thenReturn(nodeToken); } @AfterClass @@ -147,25 +158,30 @@ public void testCreateToken() throws Exception { public void testInvalidScope() throws Exception { String scope = randomAlphaOfLength(14); - UserException e = expectThrows(UserException.class, () -> { - execute(randomFrom("-s", "--s"), scope); - }); + UserException e = expectThrows(UserException.class, () -> { execute(randomFrom("-s", "--s"), scope); }); assertThat(e.exitCode, equalTo(ExitCodes.USAGE)); assertThat(e.getMessage(), equalTo("Invalid scope")); - assertThat(terminal.getErrorOutput(), - containsString("The scope of this enrollment token, can only be one of "+ CreateEnrollmentTokenTool.ALLOWED_SCOPES)); + assertThat( + terminal.getErrorOutput(), + containsString("The scope of this enrollment token, can only be one of " + CreateEnrollmentTokenTool.ALLOWED_SCOPES) + ); } public void testUnhealthyCluster() throws Exception { String scope = randomBoolean() ? "node" : "kibana"; URL url = new URL(client.getDefaultURL()); - HttpResponse healthResponse = - new HttpResponse(HttpURLConnection.HTTP_OK, Map.of("status", randomFrom("red"))); - when(client.execute(anyString(), eq(clusterHealthUrl(url)), anyString(), any(SecureString.class), any(CheckedSupplier.class), - any(CheckedFunction.class))).thenReturn(healthResponse); - UserException e = expectThrows(UserException.class, () -> { - execute(randomFrom("-s", "--s"), scope); - }); + HttpResponse healthResponse = new HttpResponse(HttpURLConnection.HTTP_OK, Map.of("status", randomFrom("red"))); + when( + client.execute( + anyString(), + eq(clusterHealthUrl(url)), + anyString(), + any(SecureString.class), + any(CheckedSupplier.class), + any(CheckedFunction.class) + ) + ).thenReturn(healthResponse); + UserException e = expectThrows(UserException.class, () -> { execute(randomFrom("-s", "--s"), scope); }); assertThat(e.exitCode, equalTo(ExitCodes.UNAVAILABLE)); assertThat(e.getMessage(), containsString("RED")); } @@ -181,33 +197,26 @@ public void testUnhealthyClusterWithForce() throws Exception { } public void testEnrollmentDisabled() { - settings = Settings.builder() - .put(settings) - .put(XPackSettings.ENROLLMENT_ENABLED.getKey(), false) - .build(); + settings = Settings.builder().put(settings).put(XPackSettings.ENROLLMENT_ENABLED.getKey(), false).build(); String scope = randomBoolean() ? "node" : "kibana"; - UserException e = expectThrows(UserException.class, () -> { - execute(randomFrom("-s", "--s"), scope); - }); + UserException e = expectThrows(UserException.class, () -> { execute(randomFrom("-s", "--s"), scope); }); assertThat(e.exitCode, equalTo(ExitCodes.CONFIG)); - assertThat(e.getMessage(), - equalTo("[xpack.security.enrollment.enabled] must be set to `true` to create an enrollment token")); + assertThat(e.getMessage(), equalTo("[xpack.security.enrollment.enabled] must be set to `true` to create an enrollment token")); } public void testUnableToCreateToken() throws Exception { this.externalEnrollmentTokenGenerator = mock(ExternalEnrollmentTokenGenerator.class); - when(externalEnrollmentTokenGenerator.createKibanaEnrollmentToken(anyString(), any(SecureString.class))) - .thenThrow(new IllegalStateException("example exception message")); - when(externalEnrollmentTokenGenerator.createNodeEnrollmentToken(anyString(), any(SecureString.class))) - .thenThrow(new IllegalStateException("example exception message")); + when(externalEnrollmentTokenGenerator.createKibanaEnrollmentToken(anyString(), any(SecureString.class))).thenThrow( + new IllegalStateException("example exception message") + ); + when(externalEnrollmentTokenGenerator.createNodeEnrollmentToken(anyString(), any(SecureString.class))).thenThrow( + new IllegalStateException("example exception message") + ); String scope = randomBoolean() ? "node" : "kibana"; - UserException e = expectThrows(UserException.class, () -> { - execute(randomFrom("-s", "--s"), scope); - }); + UserException e = expectThrows(UserException.class, () -> { execute(randomFrom("-s", "--s"), scope); }); assertThat(e.exitCode, equalTo(ExitCodes.CANT_CREATE)); - assertThat(e.getMessage(), - equalTo("example exception message")); + assertThat(e.getMessage(), equalTo("example exception message")); } private URL clusterHealthUrl(URL url) throws MalformedURLException, URISyntaxException { diff --git a/x-pack/qa/security-tools-tests/src/test/java/org/elasticsearch/xpack/security/support/FileAttributesCheckerTests.java b/x-pack/qa/security-tools-tests/src/test/java/org/elasticsearch/xpack/security/support/FileAttributesCheckerTests.java index 149793731ee51..97ea145c60cd2 100644 --- a/x-pack/qa/security-tools-tests/src/test/java/org/elasticsearch/xpack/security/support/FileAttributesCheckerTests.java +++ b/x-pack/qa/security-tools-tests/src/test/java/org/elasticsearch/xpack/security/support/FileAttributesCheckerTests.java @@ -6,6 +6,12 @@ */ package org.elasticsearch.xpack.security.support; +import com.google.common.jimfs.Configuration; +import com.google.common.jimfs.Jimfs; + +import org.elasticsearch.cli.MockTerminal; +import org.elasticsearch.test.ESTestCase; + import java.nio.file.FileSystem; import java.nio.file.Files; import java.nio.file.Path; @@ -16,11 +22,6 @@ import java.util.HashSet; import java.util.Set; -import com.google.common.jimfs.Configuration; -import com.google.common.jimfs.Jimfs; -import org.elasticsearch.cli.MockTerminal; -import org.elasticsearch.test.ESTestCase; - public class FileAttributesCheckerTests extends ESTestCase { public void testNonExistentFile() throws Exception { diff --git a/x-pack/qa/security-tools-tests/src/test/java/org/elasticsearch/xpack/security/support/SecurityFilesTests.java b/x-pack/qa/security-tools-tests/src/test/java/org/elasticsearch/xpack/security/support/SecurityFilesTests.java index fb13c1fb5d788..dc98d37d26bf0 100644 --- a/x-pack/qa/security-tools-tests/src/test/java/org/elasticsearch/xpack/security/support/SecurityFilesTests.java +++ b/x-pack/qa/security-tools-tests/src/test/java/org/elasticsearch/xpack/security/support/SecurityFilesTests.java @@ -8,6 +8,7 @@ import com.google.common.jimfs.Configuration; import com.google.common.jimfs.Jimfs; + import org.elasticsearch.common.util.set.Sets; import org.elasticsearch.env.Environment; import org.elasticsearch.test.ESTestCase; @@ -80,18 +81,17 @@ public void testFailure() throws IOException { Files.write(path, "foo".getBytes(StandardCharsets.UTF_8)); final Visitor innerVisitor = new Visitor(path); - final RuntimeException re = expectThrows(RuntimeException.class, () -> SecurityFiles.writeFileAtomically( - path, - Collections.singletonMap("foo", "bar"), - e -> { - try { - Files.walkFileTree(path.getParent(), innerVisitor); - } catch (final IOException inner) { - throw new UncheckedIOException(inner); - } - throw new RuntimeException(e.getKey() + " " + e.getValue()); + final RuntimeException re = expectThrows( + RuntimeException.class, + () -> SecurityFiles.writeFileAtomically(path, Collections.singletonMap("foo", "bar"), e -> { + try { + Files.walkFileTree(path.getParent(), innerVisitor); + } catch (final IOException inner) { + throw new UncheckedIOException(inner); } - )); + throw new RuntimeException(e.getKey() + " " + e.getValue()); + }) + ); assertThat(re, hasToString(containsString("foo bar"))); diff --git a/x-pack/qa/smoke-test-plugins-ssl/src/test/java/org/elasticsearch/smoketest/SmokeTestMonitoringWithSecurityIT.java b/x-pack/qa/smoke-test-plugins-ssl/src/test/java/org/elasticsearch/smoketest/SmokeTestMonitoringWithSecurityIT.java index 7036714b4ba59..df6189b5ca681 100644 --- a/x-pack/qa/smoke-test-plugins-ssl/src/test/java/org/elasticsearch/smoketest/SmokeTestMonitoringWithSecurityIT.java +++ b/x-pack/qa/smoke-test-plugins-ssl/src/test/java/org/elasticsearch/smoketest/SmokeTestMonitoringWithSecurityIT.java @@ -8,6 +8,7 @@ import io.netty.util.ThreadDeathWatcher; import io.netty.util.concurrent.GlobalEventExecutor; + import org.elasticsearch.ElasticsearchException; import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest; import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse; @@ -26,11 +27,11 @@ import org.elasticsearch.client.xpack.XPackUsageResponse; import org.elasticsearch.cluster.health.ClusterHealthStatus; import org.elasticsearch.common.Priority; -import org.elasticsearch.core.PathUtils; import org.elasticsearch.common.settings.MockSecureSettings; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.PathUtils; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.test.ESIntegTestCase; import org.elasticsearch.test.rest.ESRestTestCase; @@ -146,7 +147,8 @@ protected Settings restClientSettings() { return Settings.builder() .put(ThreadContext.PREFIX + ".Authorization", token) .put(ESRestTestCase.TRUSTSTORE_PATH, keyStore) - .put(ESRestTestCase.TRUSTSTORE_PASSWORD, KEYSTORE_PASS).build(); + .put(ESRestTestCase.TRUSTSTORE_PASSWORD, KEYSTORE_PASS) + .build(); } @Before @@ -163,8 +165,8 @@ public void enableExporter() throws Exception { .put("xpack.monitoring.exporters._http.ssl.certificate_authorities", "testnode.crt") .setSecureSettings(secureSettings) .build(); - ClusterUpdateSettingsResponse response = newHighLevelClient().cluster().putSettings( - new ClusterUpdateSettingsRequest().transientSettings(exporterSettings), RequestOptions.DEFAULT); + ClusterUpdateSettingsResponse response = newHighLevelClient().cluster() + .putSettings(new ClusterUpdateSettingsRequest().transientSettings(exporterSettings), RequestOptions.DEFAULT); assertTrue(response.isAcknowledged()); } @@ -179,8 +181,8 @@ public void disableExporter() throws IOException { .putNull("xpack.monitoring.exporters._http.ssl.verification_mode") .putNull("xpack.monitoring.exporters._http.ssl.certificate_authorities") .build(); - ClusterUpdateSettingsResponse response = newHighLevelClient().cluster().putSettings( - new ClusterUpdateSettingsRequest().transientSettings(exporterSettings), RequestOptions.DEFAULT); + ClusterUpdateSettingsResponse response = newHighLevelClient().cluster() + .putSettings(new ClusterUpdateSettingsRequest().transientSettings(exporterSettings), RequestOptions.DEFAULT); assertTrue(response.isAcknowledged()); } diff --git a/x-pack/qa/smoke-test-plugins-ssl/src/test/java/org/elasticsearch/smoketest/SmokeTestPluginsSslClientYamlTestSuiteIT.java b/x-pack/qa/smoke-test-plugins-ssl/src/test/java/org/elasticsearch/smoketest/SmokeTestPluginsSslClientYamlTestSuiteIT.java index 5f88eda8b1c90..df1ef788ccc96 100644 --- a/x-pack/qa/smoke-test-plugins-ssl/src/test/java/org/elasticsearch/smoketest/SmokeTestPluginsSslClientYamlTestSuiteIT.java +++ b/x-pack/qa/smoke-test-plugins-ssl/src/test/java/org/elasticsearch/smoketest/SmokeTestPluginsSslClientYamlTestSuiteIT.java @@ -10,10 +10,10 @@ import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; import org.elasticsearch.ElasticsearchException; -import org.elasticsearch.core.PathUtils; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.core.PathUtils; import org.elasticsearch.test.rest.ESRestTestCase; import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate; import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase; @@ -42,28 +42,28 @@ public static Iterable parameters() throws Exception { @BeforeClass public static void getKeyStore() { - try { - certificateAuthorities = PathUtils.get(SmokeTestPluginsSslClientYamlTestSuiteIT.class.getResource("/testnode.crt").toURI()); - } catch (URISyntaxException e) { - throw new ElasticsearchException("exception while reading the store", e); - } - if (Files.exists(certificateAuthorities) == false) { - throw new IllegalStateException("Keystore file [" + certificateAuthorities + "] does not exist."); - } + try { + certificateAuthorities = PathUtils.get(SmokeTestPluginsSslClientYamlTestSuiteIT.class.getResource("/testnode.crt").toURI()); + } catch (URISyntaxException e) { + throw new ElasticsearchException("exception while reading the store", e); + } + if (Files.exists(certificateAuthorities) == false) { + throw new IllegalStateException("Keystore file [" + certificateAuthorities + "] does not exist."); + } } @AfterClass public static void clearKeyStore() { - certificateAuthorities = null; + certificateAuthorities = null; } @Override protected Settings restClientSettings() { String token = basicAuthHeaderValue(USER, new SecureString(PASS.toCharArray())); return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .put(ESRestTestCase.CERTIFICATE_AUTHORITIES, certificateAuthorities) - .build(); + .put(ThreadContext.PREFIX + ".Authorization", token) + .put(ESRestTestCase.CERTIFICATE_AUTHORITIES, certificateAuthorities) + .build(); } @Override diff --git a/x-pack/qa/smoke-test-plugins/src/test/java/org/elasticsearch/smoketest/XSmokeTestPluginsClientYamlTestSuiteIT.java b/x-pack/qa/smoke-test-plugins/src/test/java/org/elasticsearch/smoketest/XSmokeTestPluginsClientYamlTestSuiteIT.java index 5f43aec0f24a4..019e3d64eacfe 100644 --- a/x-pack/qa/smoke-test-plugins/src/test/java/org/elasticsearch/smoketest/XSmokeTestPluginsClientYamlTestSuiteIT.java +++ b/x-pack/qa/smoke-test-plugins/src/test/java/org/elasticsearch/smoketest/XSmokeTestPluginsClientYamlTestSuiteIT.java @@ -33,9 +33,6 @@ public static Iterable parameters() throws Exception { protected Settings restClientSettings() { String token = basicAuthHeaderValue(USER, new SecureString(PASS.toCharArray())); - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", token) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); } } - diff --git a/x-pack/qa/smoke-test-security-with-mustache/src/test/java/org/elasticsearch/smoketest/SmokeTestSecurityWithMustacheClientYamlTestSuiteIT.java b/x-pack/qa/smoke-test-security-with-mustache/src/test/java/org/elasticsearch/smoketest/SmokeTestSecurityWithMustacheClientYamlTestSuiteIT.java index 343cdb931542a..7cba0a8b87fe7 100644 --- a/x-pack/qa/smoke-test-security-with-mustache/src/test/java/org/elasticsearch/smoketest/SmokeTestSecurityWithMustacheClientYamlTestSuiteIT.java +++ b/x-pack/qa/smoke-test-security-with-mustache/src/test/java/org/elasticsearch/smoketest/SmokeTestSecurityWithMustacheClientYamlTestSuiteIT.java @@ -17,8 +17,10 @@ public class SmokeTestSecurityWithMustacheClientYamlTestSuiteIT extends ESClientYamlSuiteTestCase { - private static final String BASIC_AUTH_VALUE = basicAuthHeaderValue("test_admin", - new SecureString("x-pack-test-password".toCharArray())); + private static final String BASIC_AUTH_VALUE = basicAuthHeaderValue( + "test_admin", + new SecureString("x-pack-test-password".toCharArray()) + ); public SmokeTestSecurityWithMustacheClientYamlTestSuiteIT(@Name("yaml") ClientYamlTestCandidate testCandidate) { super(testCandidate); @@ -31,8 +33,6 @@ public static Iterable parameters() throws Exception { @Override protected Settings restClientSettings() { - return Settings.builder() - .put(ThreadContext.PREFIX + ".Authorization", BASIC_AUTH_VALUE) - .build(); + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", BASIC_AUTH_VALUE).build(); } } diff --git a/x-pack/qa/src/main/java/org/elasticsearch/xpack/test/SecuritySettingsSourceField.java b/x-pack/qa/src/main/java/org/elasticsearch/xpack/test/SecuritySettingsSourceField.java index 448eef49134dc..073b76ad67f81 100644 --- a/x-pack/qa/src/main/java/org/elasticsearch/xpack/test/SecuritySettingsSourceField.java +++ b/x-pack/qa/src/main/java/org/elasticsearch/xpack/test/SecuritySettingsSourceField.java @@ -6,8 +6,8 @@ */ package org.elasticsearch.xpack.test; -import org.elasticsearch.core.CharArrays; import org.elasticsearch.common.settings.SecureString; +import org.elasticsearch.core.CharArrays; import java.nio.CharBuffer; import java.util.Arrays; @@ -30,7 +30,7 @@ public static String basicAuthHeaderValue(String username, SecureString passwd) chars.put(username).put(':').put(passwd.getChars()); charBytes = CharArrays.toUtf8Bytes(chars.array()); - //TODO we still have passwords in Strings in headers. Maybe we can look into using a CharSequence? + // TODO we still have passwords in Strings in headers. Maybe we can look into using a CharSequence? String basicToken = Base64.getEncoder().encodeToString(charBytes); return "Basic " + basicToken; } finally { diff --git a/x-pack/qa/src/main/java/org/elasticsearch/xpack/test/rest/IndexMappingTemplateAsserter.java b/x-pack/qa/src/main/java/org/elasticsearch/xpack/test/rest/IndexMappingTemplateAsserter.java index 0045c393caa6f..6d8a50c14cf95 100644 --- a/x-pack/qa/src/main/java/org/elasticsearch/xpack/test/rest/IndexMappingTemplateAsserter.java +++ b/x-pack/qa/src/main/java/org/elasticsearch/xpack/test/rest/IndexMappingTemplateAsserter.java @@ -74,17 +74,23 @@ public static void assertMlMappingsMatchTemplates(RestClient client) throws Exce assertComposableTemplateMatchesIndexMappings(client, ".ml-stats", ".ml-stats-000001", true, statsIndexException, false); assertComposableTemplateMatchesIndexMappings(client, ".ml-state", ".ml-state-000001", true, Collections.emptySet(), false); // Depending on the order Full Cluster restart tests are run there may not be an notifications index yet - assertComposableTemplateMatchesIndexMappings(client, - ".ml-notifications-000002", ".ml-notifications-000002", true, notificationsIndexExceptions, false); + assertComposableTemplateMatchesIndexMappings( + client, + ".ml-notifications-000002", + ".ml-notifications-000002", + true, + notificationsIndexExceptions, + false + ); // .ml-annotations-6 does not use a template // .ml-anomalies-shared uses a template but will have dynamically updated mappings as new jobs are opened // Dynamic mappings updates are banned for system indices. // The .ml-config and .ml-meta indices have mappings that allow dynamic updates. // The effect is instant error if a document containing an unknown field is added - // to one of these indices. Assuming we have some sort of test coverage somewhere + // to one of these indices. Assuming we have some sort of test coverage somewhere // for new fields, we will very quickly catch any failures to add new fields to - // the mappings for the .ml-config and .ml-meta indices. So there is no need to + // the mappings for the .ml-config and .ml-meta indices. So there is no need to // test again here. } @@ -117,12 +123,14 @@ public static void assertMlMappingsMatchTemplates(RestClient client) throws Exce * @param allowSystemIndexWarnings Whether deprecation warnings for system index access should be allowed/expected. */ @SuppressWarnings("unchecked") - public static void assertLegacyTemplateMatchesIndexMappings(RestClient client, - String templateName, - String indexName, - boolean notAnErrorIfIndexDoesNotExist, - Set exceptions, - boolean allowSystemIndexWarnings) throws Exception { + public static void assertLegacyTemplateMatchesIndexMappings( + RestClient client, + String templateName, + String indexName, + boolean notAnErrorIfIndexDoesNotExist, + Set exceptions, + boolean allowSystemIndexWarnings + ) throws Exception { AtomicReference templateResponse = new AtomicReference<>(); @@ -134,11 +142,20 @@ public static void assertLegacyTemplateMatchesIndexMappings(RestClient client, Map templateMappings = (Map) XContentMapValues.extractValue( ESRestTestCase.entityAsMap(templateResponse.get()), - templateName, "mappings"); + templateName, + "mappings" + ); assertNotNull(templateMappings); - assertTemplateMatchesIndexMappingsCommon(client, templateName, templateMappings, - indexName, notAnErrorIfIndexDoesNotExist, exceptions, allowSystemIndexWarnings); + assertTemplateMatchesIndexMappingsCommon( + client, + templateName, + templateMappings, + indexName, + notAnErrorIfIndexDoesNotExist, + exceptions, + allowSystemIndexWarnings + ); } /** @@ -170,12 +187,14 @@ public static void assertLegacyTemplateMatchesIndexMappings(RestClient client, * @param allowSystemIndexWarnings Whether deprecation warnings for system index access should be allowed/expected. */ @SuppressWarnings("unchecked") - public static void assertComposableTemplateMatchesIndexMappings(RestClient client, - String templateName, - String indexName, - boolean notAnErrorIfIndexDoesNotExist, - Set exceptions, - boolean allowSystemIndexWarnings) throws Exception { + public static void assertComposableTemplateMatchesIndexMappings( + RestClient client, + String templateName, + String indexName, + boolean notAnErrorIfIndexDoesNotExist, + Set exceptions, + boolean allowSystemIndexWarnings + ) throws Exception { AtomicReference templateResponse = new AtomicReference<>(); @@ -187,26 +206,41 @@ public static void assertComposableTemplateMatchesIndexMappings(RestClient clien Map templateMappings = ((List>) XContentMapValues.extractValue( ESRestTestCase.entityAsMap(templateResponse.get()), - "index_templates", "index_template", "template", "mappings")).get(0); + "index_templates", + "index_template", + "template", + "mappings" + )).get(0); assertNotNull(templateMappings); - assertTemplateMatchesIndexMappingsCommon(client, templateName, templateMappings, indexName, notAnErrorIfIndexDoesNotExist, - exceptions, allowSystemIndexWarnings); + assertTemplateMatchesIndexMappingsCommon( + client, + templateName, + templateMappings, + indexName, + notAnErrorIfIndexDoesNotExist, + exceptions, + allowSystemIndexWarnings + ); } @SuppressWarnings("unchecked") - private static void assertTemplateMatchesIndexMappingsCommon(RestClient client, - String templateName, - Map templateMappings, - String indexName, - boolean notAnErrorIfIndexDoesNotExist, - Set exceptions, - boolean allowSystemIndexWarnings) throws IOException { + private static void assertTemplateMatchesIndexMappingsCommon( + RestClient client, + String templateName, + Map templateMappings, + String indexName, + boolean notAnErrorIfIndexDoesNotExist, + Set exceptions, + boolean allowSystemIndexWarnings + ) throws IOException { Request getIndexMapping = new Request("GET", indexName + "/_mapping"); if (allowSystemIndexWarnings) { - final String systemIndexWarning = "this request accesses system indices: [" + indexName + "], but in a future major version, " + - "direct access to system indices will be prevented by default"; + final String systemIndexWarning = "this request accesses system indices: [" + + indexName + + "], but in a future major version, " + + "direct access to system indices will be prevented by default"; getIndexMapping.setOptions(ESRestTestCase.expectVersionSpecificWarnings(v -> { v.current(systemIndexWarning); v.compatible(systemIndexWarning); @@ -222,12 +256,13 @@ private static void assertTemplateMatchesIndexMappingsCommon(RestClient client, throw e; } } - assertEquals("error getting mappings for index [" + indexName + "]", - 200, indexMappingResponse.getStatusLine().getStatusCode()); + assertEquals("error getting mappings for index [" + indexName + "]", 200, indexMappingResponse.getStatusLine().getStatusCode()); Map indexMappings = (Map) XContentMapValues.extractValue( ESRestTestCase.entityAsMap(indexMappingResponse), - indexName, "mappings"); + indexName, + "mappings" + ); assertNotNull(indexMappings); // ignore the _meta field @@ -260,8 +295,7 @@ private static void assertTemplateMatchesIndexMappingsCommon(RestClient client, // Remove the exceptions keysInIndexMissingFromTemplate.removeAll(exceptions); - StringBuilder errorMesssage = new StringBuilder("Error the template mappings [") - .append(templateName) + StringBuilder errorMesssage = new StringBuilder("Error the template mappings [").append(templateName) .append("] and index mappings [") .append(indexName) .append("] are not the same") @@ -287,7 +321,7 @@ private static void assertTemplateMatchesIndexMappingsCommon(RestClient client, for (String key : commonKeys) { Object template = flatTemplateMap.get(key); Object index = flatIndexMap.get(key); - if (Objects.equals(template, index) == false) { + if (Objects.equals(template, index) == false) { // Both maybe be booleans but different representations if (areBooleanObjectsAndEqual(index, template)) { continue; @@ -296,9 +330,15 @@ private static void assertTemplateMatchesIndexMappingsCommon(RestClient client, mappingsAreTheSame = false; errorMesssage.append("Values for key [").append(key).append("] are different").append(System.lineSeparator()); - errorMesssage.append(" template value [").append(template).append("] ").append(template.getClass().getSimpleName()) + errorMesssage.append(" template value [") + .append(template) + .append("] ") + .append(template.getClass().getSimpleName()) .append(System.lineSeparator()); - errorMesssage.append(" index value [").append(index).append("] ").append(index.getClass().getSimpleName()) + errorMesssage.append(" index value [") + .append(index) + .append("] ") + .append(index.getClass().getSimpleName()) .append(System.lineSeparator()); } } @@ -313,17 +353,17 @@ private static boolean areBooleanObjectsAndEqual(Object a, Object b) { Boolean right; if (a instanceof Boolean) { - left = (Boolean)a; - } else if (a instanceof String && isBooleanValueString((String)a)) { - left = Boolean.parseBoolean((String)a); + left = (Boolean) a; + } else if (a instanceof String && isBooleanValueString((String) a)) { + left = Boolean.parseBoolean((String) a); } else { return false; } if (b instanceof Boolean) { - right = (Boolean)b; - } else if (b instanceof String && isBooleanValueString((String)b)) { - right = Boolean.parseBoolean((String)b); + right = (Boolean) b; + } else if (b instanceof String && isBooleanValueString((String) b)) { + right = Boolean.parseBoolean((String) b); } else { return false; } @@ -344,9 +384,7 @@ private static Map flattenMap(Map map) { } private static Stream> flatten(String path, Map map) { - return map.entrySet() - .stream() - .flatMap((e) -> extractValue(path, e)); + return map.entrySet().stream().flatMap((e) -> extractValue(path, e)); } @SuppressWarnings("unchecked") diff --git a/x-pack/qa/src/main/java/org/elasticsearch/xpack/test/rest/XPackRestTestConstants.java b/x-pack/qa/src/main/java/org/elasticsearch/xpack/test/rest/XPackRestTestConstants.java index 7c7e1651b41e9..426b920b932ff 100644 --- a/x-pack/qa/src/main/java/org/elasticsearch/xpack/test/rest/XPackRestTestConstants.java +++ b/x-pack/qa/src/main/java/org/elasticsearch/xpack/test/rest/XPackRestTestConstants.java @@ -14,9 +14,7 @@ public final class XPackRestTestConstants { public static final String INDEX_TEMPLATE_VERSION = "10"; public static final String HISTORY_TEMPLATE_NAME_NO_ILM = ".watch-history-no-ilm-" + INDEX_TEMPLATE_VERSION; - public static final String[] TEMPLATE_NAMES_NO_ILM = new String[] { - HISTORY_TEMPLATE_NAME_NO_ILM - }; + public static final String[] TEMPLATE_NAMES_NO_ILM = new String[] { HISTORY_TEMPLATE_NAME_NO_ILM }; // ML constants: public static final String ML_META_INDEX_NAME = ".ml-meta"; @@ -25,17 +23,14 @@ public final class XPackRestTestConstants { public static final String STATE_INDEX_PREFIX = ".ml-state"; public static final String RESULTS_INDEX_DEFAULT = "shared"; - public static final List ML_POST_V660_TEMPLATES = - List.of( - ML_META_INDEX_NAME, - STATE_INDEX_PREFIX, - RESULTS_INDEX_PREFIX, - CONFIG_INDEX); + public static final List ML_POST_V660_TEMPLATES = List.of( + ML_META_INDEX_NAME, + STATE_INDEX_PREFIX, + RESULTS_INDEX_PREFIX, + CONFIG_INDEX + ); - public static final List ML_POST_V7120_TEMPLATES = - List.of( - STATE_INDEX_PREFIX, - RESULTS_INDEX_PREFIX); + public static final List ML_POST_V7120_TEMPLATES = List.of(STATE_INDEX_PREFIX, RESULTS_INDEX_PREFIX); // Transform constants: public static final String TRANSFORM_TASK_NAME = "data_frame/transforms"; @@ -44,6 +39,5 @@ public final class XPackRestTestConstants { public static final String TRANSFORM_INTERNAL_INDEX_PREFIX_DEPRECATED = ".data-frame-internal-"; public static final String TRANSFORM_NOTIFICATIONS_INDEX_PREFIX_DEPRECATED = ".data-frame-notifications-"; - private XPackRestTestConstants() { - } + private XPackRestTestConstants() {} } diff --git a/x-pack/qa/src/main/java/org/elasticsearch/xpack/test/rest/XPackRestTestHelper.java b/x-pack/qa/src/main/java/org/elasticsearch/xpack/test/rest/XPackRestTestHelper.java index 9a82ee006f772..ee743bf0e40ad 100644 --- a/x-pack/qa/src/main/java/org/elasticsearch/xpack/test/rest/XPackRestTestHelper.java +++ b/x-pack/qa/src/main/java/org/elasticsearch/xpack/test/rest/XPackRestTestHelper.java @@ -28,8 +28,7 @@ public final class XPackRestTestHelper { - private XPackRestTestHelper() { - } + private XPackRestTestHelper() {} /** * For each template name wait for the template to be created and @@ -60,7 +59,7 @@ public static void waitForTemplates(RestClient client, List expectedTemp }); // TODO: legacy support can be removed once all X-Pack plugins use only composable - // templates in the oldest version we test upgrades from + // templates in the oldest version we test upgrades from assertBusy(() -> { Map response; if (clusterUnderstandsComposableTemplates) { @@ -68,8 +67,11 @@ public static void waitForTemplates(RestClient client, List expectedTemp request.addParameter("error_trace", "true"); String string = EntityUtils.toString(client.performRequest(request).getEntity()); - List> templateList = (List>) XContentHelper.convertToMap(JsonXContent.jsonXContent, - string, false).get("index_templates"); + List> templateList = (List>) XContentHelper.convertToMap( + JsonXContent.jsonXContent, + string, + false + ).get("index_templates"); response = templateList.stream().collect(Collectors.toMap(m -> (String) m.get("name"), m -> m.get("index_template"))); } else { response = Collections.emptyMap(); @@ -91,9 +93,14 @@ public static void waitForTemplates(RestClient client, List expectedTemp // While it's possible to use a Hamcrest matcher for this, the failure is much less legible. if (missingTemplates.isEmpty() == false) { - fail("Some expected templates are missing: " + missingTemplates - + ". The composable templates that exist are: " + templates - + ". The legacy templates that exist are: " + legacyTemplates); + fail( + "Some expected templates are missing: " + + missingTemplates + + ". The composable templates that exist are: " + + templates + + ". The legacy templates that exist are: " + + legacyTemplates + ); } expectedTemplates.forEach(template -> { @@ -104,7 +111,8 @@ public static void waitForTemplates(RestClient client, List expectedTemp assertThat( "Template [" + template + "] has unexpected version", Version.fromId((Integer) templateDefinition.get("version")), - equalTo(masterNodeVersion.get())); + equalTo(masterNodeVersion.get()) + ); }); }); } diff --git a/x-pack/qa/third-party/active-directory/src/test/java/org/elasticsearch/xpack/security/authc/ldap/ADLdapUserSearchSessionFactoryTests.java b/x-pack/qa/third-party/active-directory/src/test/java/org/elasticsearch/xpack/security/authc/ldap/ADLdapUserSearchSessionFactoryTests.java index dc30d779d7326..9bba75210a9b8 100644 --- a/x-pack/qa/third-party/active-directory/src/test/java/org/elasticsearch/xpack/security/authc/ldap/ADLdapUserSearchSessionFactoryTests.java +++ b/x-pack/qa/third-party/active-directory/src/test/java/org/elasticsearch/xpack/security/authc/ldap/ADLdapUserSearchSessionFactoryTests.java @@ -63,51 +63,60 @@ public void testUserSearchWithActiveDirectory() throws Exception { String groupSearchBase = "DC=ad,DC=test,DC=elasticsearch,DC=com"; String userSearchBase = "CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com"; Settings settings = Settings.builder() - .put("url", ActiveDirectorySessionFactoryTests.AD_LDAP_URL) - .put("group_search.base_dn", groupSearchBase) - .put("user_search.base_dn", userSearchBase) - .put("bind_dn", "ironman@ad.test.elasticsearch.com") - .put("bind_password", ActiveDirectorySessionFactoryTests.PASSWORD) - .put("user_search.filter", "(cn={0})") - .put("user_search.pool.enabled", randomBoolean()) - .put("follow_referrals", ActiveDirectorySessionFactoryTests.FOLLOW_REFERRALS) - .put("order", 0) - .build(); - Settings.Builder builder = Settings.builder() - .put(globalSettings); - settings.keySet().forEach(k -> { - builder.copy("xpack.security.authc.realms.ldap.ad-as-ldap-test." + k, k, settings); - }); + .put("url", ActiveDirectorySessionFactoryTests.AD_LDAP_URL) + .put("group_search.base_dn", groupSearchBase) + .put("user_search.base_dn", userSearchBase) + .put("bind_dn", "ironman@ad.test.elasticsearch.com") + .put("bind_password", ActiveDirectorySessionFactoryTests.PASSWORD) + .put("user_search.filter", "(cn={0})") + .put("user_search.pool.enabled", randomBoolean()) + .put("follow_referrals", ActiveDirectorySessionFactoryTests.FOLLOW_REFERRALS) + .put("order", 0) + .build(); + Settings.Builder builder = Settings.builder().put(globalSettings); + settings.keySet().forEach(k -> { builder.copy("xpack.security.authc.realms.ldap.ad-as-ldap-test." + k, k, settings); }); Settings fullSettings = builder.build(); sslService = new SSLService(TestEnvironment.newEnvironment(fullSettings)); - RealmConfig config = new RealmConfig(realmIdentifier, fullSettings, - TestEnvironment.newEnvironment(fullSettings), new ThreadContext(fullSettings)); + RealmConfig config = new RealmConfig( + realmIdentifier, + fullSettings, + TestEnvironment.newEnvironment(fullSettings), + new ThreadContext(fullSettings) + ); LdapUserSearchSessionFactory sessionFactory = getLdapUserSearchSessionFactory(config, sslService, threadPool); String user = "Bruce Banner"; try { - //auth + // auth try (LdapSession ldap = session(sessionFactory, user, new SecureString(ActiveDirectorySessionFactoryTests.PASSWORD))) { assertConnectionCanReconnect(ldap.getConnection()); List groups = groups(ldap); - assertThat(groups, containsInAnyOrder( + assertThat( + groups, + containsInAnyOrder( containsString("Avengers"), containsString("SHIELD"), containsString("Geniuses"), - containsString("Philanthropists"))); + containsString("Philanthropists") + ) + ); } - //lookup + // lookup try (LdapSession ldap = unauthenticatedSession(sessionFactory, user)) { assertConnectionCanReconnect(ldap.getConnection()); List groups = groups(ldap); - assertThat(groups, containsInAnyOrder( + assertThat( + groups, + containsInAnyOrder( containsString("Avengers"), containsString("SHIELD"), containsString("Geniuses"), - containsString("Philanthropists"))); + containsString("Philanthropists") + ) + ); } } finally { sessionFactory.close(); diff --git a/x-pack/qa/third-party/active-directory/src/test/java/org/elasticsearch/xpack/security/authc/ldap/AbstractActiveDirectoryTestCase.java b/x-pack/qa/third-party/active-directory/src/test/java/org/elasticsearch/xpack/security/authc/ldap/AbstractActiveDirectoryTestCase.java index b4a654260f10c..9ab6b5a309393 100644 --- a/x-pack/qa/third-party/active-directory/src/test/java/org/elasticsearch/xpack/security/authc/ldap/AbstractActiveDirectoryTestCase.java +++ b/x-pack/qa/third-party/active-directory/src/test/java/org/elasticsearch/xpack/security/authc/ldap/AbstractActiveDirectoryTestCase.java @@ -10,10 +10,11 @@ import com.unboundid.ldap.sdk.LDAPConnectionPool; import com.unboundid.ldap.sdk.LDAPException; import com.unboundid.ldap.sdk.LDAPInterface; + import org.elasticsearch.ExceptionsHelper; -import org.elasticsearch.core.Booleans; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.ssl.SslVerificationMode; +import org.elasticsearch.core.Booleans; import org.elasticsearch.env.Environment; import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.test.ESTestCase; @@ -52,7 +53,7 @@ public abstract class AbstractActiveDirectoryTestCase extends ESTestCase { public static final String PASSWORD = getFromEnv("TESTS_AD_USER_PASSWORD", "Passw0rd"); public static final String AD_LDAP_PORT = getFromEnv("TESTS_AD_LDAP_PORT", getFromProperty("389")); - public static final String AD_LDAPS_PORT = getFromEnv("TESTS_AD_LDAPS_PORT", getFromProperty("636")); + public static final String AD_LDAPS_PORT = getFromEnv("TESTS_AD_LDAPS_PORT", getFromProperty("636")); public static final String AD_GC_LDAP_PORT = getFromEnv("TESTS_AD_GC_LDAP_PORT", getFromProperty("3268")); public static final String AD_GC_LDAPS_PORT = getFromEnv("TESTS_AD_GC_LDAPS_PORT", getFromProperty("3269")); public static final String AD_DOMAIN = "ad.test.elasticsearch.com"; @@ -66,8 +67,7 @@ public void initializeSslSocketFactory() throws Exception { // We use certificates in PEM format and `ssl.certificate_authorities` instead of ssl.trustore // so that these tests can also run in a FIPS JVM where JKS keystores can't be used. certificatePaths = new ArrayList<>(); - Files.walkFileTree(getDataPath - ("../ldap/support"), new SimpleFileVisitor() { + Files.walkFileTree(getDataPath("../ldap/support"), new SimpleFileVisitor() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { String fileName = file.getFileName().toString(); @@ -94,8 +94,14 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO sslService = new SSLService(environment); } - Settings buildAdSettings(RealmConfig.RealmIdentifier realmId, String ldapUrl, String adDomainName, String userSearchDN, - LdapSearchScope scope, boolean hostnameVerification) { + Settings buildAdSettings( + RealmConfig.RealmIdentifier realmId, + String ldapUrl, + String adDomainName, + String userSearchDN, + LdapSearchScope scope, + boolean hostnameVerification + ) { final String realmName = realmId.getName(); Settings.Builder builder = Settings.builder() .putList(getFullSettingKey(realmId, SessionFactorySettings.URLS_SETTING), ldapUrl) @@ -109,8 +115,10 @@ Settings buildAdSettings(RealmConfig.RealmIdentifier realmId, String ldapUrl, St .put(getFullSettingKey(realmId, SessionFactorySettings.FOLLOW_REFERRALS_SETTING), FOLLOW_REFERRALS) .putList(getFullSettingKey(realmId, SSLConfigurationSettings.CAPATH_SETTING_REALM), certificatePaths); if (randomBoolean()) { - builder.put(getFullSettingKey(realmId, SSLConfigurationSettings.VERIFICATION_MODE_SETTING_REALM), - hostnameVerification ? SslVerificationMode.FULL : SslVerificationMode.CERTIFICATE); + builder.put( + getFullSettingKey(realmId, SSLConfigurationSettings.VERIFICATION_MODE_SETTING_REALM), + hostnameVerification ? SslVerificationMode.FULL : SslVerificationMode.CERTIFICATE + ); } else { builder.put(getFullSettingKey(realmId, SessionFactorySettings.HOSTNAME_VERIFICATION_SETTING), hostnameVerification); } @@ -130,8 +138,11 @@ public Void run() { } } } catch (LDAPException e) { - fail("Connection is not valid. It will not work on follow referral flow." + - System.lineSeparator() + ExceptionsHelper.stackTrace(e)); + fail( + "Connection is not valid. It will not work on follow referral flow." + + System.lineSeparator() + + ExceptionsHelper.stackTrace(e) + ); } return null; } diff --git a/x-pack/qa/third-party/active-directory/src/test/java/org/elasticsearch/xpack/security/authc/ldap/AbstractAdLdapRealmTestCase.java b/x-pack/qa/third-party/active-directory/src/test/java/org/elasticsearch/xpack/security/authc/ldap/AbstractAdLdapRealmTestCase.java index 23e389a3a7692..fee207b9dc86e 100644 --- a/x-pack/qa/third-party/active-directory/src/test/java/org/elasticsearch/xpack/security/authc/ldap/AbstractAdLdapRealmTestCase.java +++ b/x-pack/qa/third-party/active-directory/src/test/java/org/elasticsearch/xpack/security/authc/ldap/AbstractAdLdapRealmTestCase.java @@ -13,14 +13,14 @@ import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.client.Client; -import org.elasticsearch.core.Nullable; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.ssl.SslVerificationMode; -import org.elasticsearch.xcontent.XContentType; +import org.elasticsearch.core.Nullable; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.SecurityIntegTestCase; +import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.security.action.rolemapping.PutRoleMappingRequestBuilder; import org.elasticsearch.xpack.core.security.action.rolemapping.PutRoleMappingResponse; import org.elasticsearch.xpack.core.security.authc.ldap.ActiveDirectorySessionFactorySettings; @@ -68,31 +68,29 @@ public abstract class AbstractAdLdapRealmTestCase extends SecurityIntegTestCase public static final String SECURITY_INDEX = "security"; private static final RoleMappingEntry[] AD_ROLE_MAPPING = new RoleMappingEntry[] { - new RoleMappingEntry( - "SHIELD: [ \"CN=SHIELD,CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com\" ]", - "{ \"roles\":[\"SHIELD\"], \"enabled\":true, \"rules\":" + - "{\"field\": {\"groups\": \"CN=SHIELD,CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com\"} } }" - ), - new RoleMappingEntry( - "Avengers: [ \"CN=Avengers,CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com\" ]", - "{ \"roles\":[\"Avengers\"], \"enabled\":true, \"rules\":" + - "{ \"field\": { \"groups\" : \"CN=Avengers,CN=Users,*\" } } }" - ), - new RoleMappingEntry( - "Gods: [ \"CN=Gods,CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com\" ]", - "{ \"roles\":[\"Gods\"], \"enabled\":true, \"rules\":{\"any\": [" + - " { \"field\":{ \"groups\": \"CN=Gods,CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com\" } }," + - " { \"field\":{ \"groups\": \"CN=Deities,CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com\" } } " + - "] } }" - ), - new RoleMappingEntry( - "Philanthropists: [ \"CN=Philanthropists,CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com\" ]", - "{ \"roles\":[\"Philanthropists\"], \"enabled\":true, \"rules\": { \"all\": [" + - " { \"field\": { \"groups\" : \"CN=Philanthropists,CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com\" } }," + - " { \"field\": { \"realm.name\" : \"external\" } } " + - "] } }" - ) - }; + new RoleMappingEntry( + "SHIELD: [ \"CN=SHIELD,CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com\" ]", + "{ \"roles\":[\"SHIELD\"], \"enabled\":true, \"rules\":" + + "{\"field\": {\"groups\": \"CN=SHIELD,CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com\"} } }" + ), + new RoleMappingEntry( + "Avengers: [ \"CN=Avengers,CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com\" ]", + "{ \"roles\":[\"Avengers\"], \"enabled\":true, \"rules\":" + "{ \"field\": { \"groups\" : \"CN=Avengers,CN=Users,*\" } } }" + ), + new RoleMappingEntry( + "Gods: [ \"CN=Gods,CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com\" ]", + "{ \"roles\":[\"Gods\"], \"enabled\":true, \"rules\":{\"any\": [" + + " { \"field\":{ \"groups\": \"CN=Gods,CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com\" } }," + + " { \"field\":{ \"groups\": \"CN=Deities,CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com\" } } " + + "] } }" + ), + new RoleMappingEntry( + "Philanthropists: [ \"CN=Philanthropists,CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com\" ]", + "{ \"roles\":[\"Philanthropists\"], \"enabled\":true, \"rules\": { \"all\": [" + + " { \"field\": { \"groups\" : \"CN=Philanthropists,CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com\" } }," + + " { \"field\": { \"realm.name\" : \"external\" } } " + + "] } }" + ) }; protected static RealmConfig realmConfig; protected static List roleMappings; @@ -101,9 +99,13 @@ public abstract class AbstractAdLdapRealmTestCase extends SecurityIntegTestCase public static void setupRealm() { realmConfig = randomFrom(RealmConfig.values()); roleMappings = realmConfig.selectRoleMappings(ESTestCase::randomBoolean); - LogManager.getLogger(AbstractAdLdapRealmTestCase.class).info( + LogManager.getLogger(AbstractAdLdapRealmTestCase.class) + .info( "running test with realm configuration [{}], with direct group to role mapping [{}]. Settings [{}]", - realmConfig, realmConfig.mapGroupsAsRoles, realmConfig.settings); + realmConfig, + realmConfig.mapGroupsAsRoles, + realmConfig.settings + ); } @AfterClass @@ -120,8 +122,11 @@ protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { return builder.build(); } - protected Settings buildRealmSettings(RealmConfig realm, List roleMappingEntries, List - certificateAuthorities) { + protected Settings buildRealmSettings( + RealmConfig realm, + List roleMappingEntries, + List certificateAuthorities + ) { Settings.Builder builder = Settings.builder(); builder.put(realm.buildSettings(certificateAuthorities)); configureFileRoleMappings(builder, realm.type, roleMappingEntries); @@ -139,8 +144,11 @@ public void setupRoleMappings() throws Exception { Map> futures = new LinkedHashMap<>(content.size()); for (int i = 0; i < content.size(); i++) { final String name = "external_" + i; - final PutRoleMappingRequestBuilder builder = new PutRoleMappingRequestBuilder(client()) - .source(name, new BytesArray(content.get(i)), XContentType.JSON); + final PutRoleMappingRequestBuilder builder = new PutRoleMappingRequestBuilder(client()).source( + name, + new BytesArray(content.get(i)), + XContentType.JSON + ); futures.put(name, builder.execute()); } for (String mappingName : futures.keySet()) { @@ -159,10 +167,7 @@ private List getRoleMappingContent(Function co } private List getRoleMappingContent(Function contentFunction, List mappings) { - return mappings.stream() - .map(contentFunction) - .filter(Objects::nonNull) - .collect(Collectors.toList()); + return mappings.stream().map(contentFunction).filter(Objects::nonNull).collect(Collectors.toList()); } protected final void configureFileRoleMappings(Settings.Builder builder, String realmType, List mappings) { @@ -174,46 +179,52 @@ protected final void configureFileRoleMappings(Settings.Builder builder, String @Override protected String configRoles() { - return super.configRoles() + - "\n" + - "Avengers:\n" + - " cluster: [ NONE ]\n" + - " indices:\n" + - " - names: 'avengers'\n" + - " privileges: [ all ]\n" + - "SHIELD:\n" + - " cluster: [ NONE ]\n" + - " indices:\n" + - " - names: '" + SECURITY_INDEX + "'\n" + - " privileges: [ all ]\n" + - "Gods:\n" + - " cluster: [ NONE ]\n" + - " indices:\n" + - " - names: '" + ASGARDIAN_INDEX + "'\n" + - " privileges: [ all ]\n" + - "Philanthropists:\n" + - " cluster: [ NONE ]\n" + - " indices:\n" + - " - names: '" + PHILANTHROPISTS_INDEX + "'\n" + - " privileges: [ all ]\n"; + return super.configRoles() + + "\n" + + "Avengers:\n" + + " cluster: [ NONE ]\n" + + " indices:\n" + + " - names: 'avengers'\n" + + " privileges: [ all ]\n" + + "SHIELD:\n" + + " cluster: [ NONE ]\n" + + " indices:\n" + + " - names: '" + + SECURITY_INDEX + + "'\n" + + " privileges: [ all ]\n" + + "Gods:\n" + + " cluster: [ NONE ]\n" + + " indices:\n" + + " - names: '" + + ASGARDIAN_INDEX + + "'\n" + + " privileges: [ all ]\n" + + "Philanthropists:\n" + + " cluster: [ NONE ]\n" + + " indices:\n" + + " - names: '" + + PHILANTHROPISTS_INDEX + + "'\n" + + " privileges: [ all ]\n"; } protected void assertAccessAllowed(String user, String index) throws IOException { Client client = client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, userHeader(user, PASSWORD))); - IndexResponse indexResponse = client.prepareIndex(index). - setSource(jsonBuilder() - .startObject() - .field("name", "value") - .endObject()) - .execute().actionGet(); + IndexResponse indexResponse = client.prepareIndex(index) + .setSource(jsonBuilder().startObject().field("name", "value").endObject()) + .execute() + .actionGet(); - assertEquals("user " + user + " should have write access to index " + index, - DocWriteResponse.Result.CREATED, indexResponse.getResult()); + assertEquals( + "user " + user + " should have write access to index " + index, + DocWriteResponse.Result.CREATED, + indexResponse.getResult() + ); refresh(); - GetResponse getResponse = client.prepareGet(index, indexResponse.getId()) - .get(); + GetResponse getResponse = client.prepareGet(index, indexResponse.getId()).get(); assertThat("user " + user + " should have read access to index " + index, getResponse.getId(), equalTo(indexResponse.getId())); } @@ -221,12 +232,10 @@ protected void assertAccessAllowed(String user, String index) throws IOException protected void assertAccessDenied(String user, String index) throws IOException { try { client().filterWithHeader(Collections.singletonMap(BASIC_AUTH_HEADER, userHeader(user, PASSWORD))) - .prepareIndex(index). - setSource(jsonBuilder() - .startObject() - .field("name", "value") - .endObject()) - .execute().actionGet(); + .prepareIndex(index) + .setSource(jsonBuilder().startObject().field("name", "value").endObject()) + .execute() + .actionGet(); fail("Write access to index " + index + " should not be allowed for user " + user); } catch (ElasticsearchSecurityException e) { // expected @@ -242,20 +251,22 @@ protected static String userHeader(String username, String password) { * Collects all the certificates that are normally trusted by the node ( contained in testnode.jks ) */ List getNodeTrustedCertificates() { - Path testnodeCert = - getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt"); - Path testnodeClientProfileCert = - getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode-client-profile.crt"); - Path activedirCert = - getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/active-directory-ca.crt"); - Path testclientCert = - getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testclient.crt"); - Path openldapCert = - getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/openldap.crt"); - Path samba4Cert = - getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/samba4.crt"); - return Arrays.asList(testnodeCert.toString(), testnodeClientProfileCert.toString(), activedirCert.toString(), testclientCert - .toString(), openldapCert.toString(), samba4Cert.toString()); + Path testnodeCert = getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt"); + Path testnodeClientProfileCert = getDataPath( + "/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode-client-profile.crt" + ); + Path activedirCert = getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/active-directory-ca.crt"); + Path testclientCert = getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testclient.crt"); + Path openldapCert = getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/openldap.crt"); + Path samba4Cert = getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/samba4.crt"); + return Arrays.asList( + testnodeCert.toString(), + testnodeClientProfileCert.toString(), + activedirCert.toString(), + testclientCert.toString(), + openldapCert.toString(), + samba4Cert.toString() + ); } static class RoleMappingEntry { @@ -301,8 +312,7 @@ public boolean equals(Object o) { } final RoleMappingEntry that = (RoleMappingEntry) o; - return Objects.equals(this.fileContent, that.fileContent) - && Objects.equals(this.nativeContent, that.nativeContent); + return Objects.equals(this.fileContent, that.fileContent) && Objects.equals(this.nativeContent, that.nativeContent); } @Override @@ -318,44 +328,52 @@ public int hashCode() { */ enum RealmConfig { - AD(false, AD_ROLE_MAPPING, - Settings.builder() - .put(XPACK_SECURITY_AUTHC_REALMS_AD_EXTERNAL + ".domain_name", ActiveDirectorySessionFactoryTests.AD_DOMAIN) - .put(XPACK_SECURITY_AUTHC_REALMS_AD_EXTERNAL - + ".group_search.base_dn", "CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com") - .put(XPACK_SECURITY_AUTHC_REALMS_AD_EXTERNAL + ".group_search.scope", randomBoolean() ? SUB_TREE : ONE_LEVEL) - .put(XPACK_SECURITY_AUTHC_REALMS_AD_EXTERNAL + ".url", ActiveDirectorySessionFactoryTests.AD_LDAP_URL) - .put(XPACK_SECURITY_AUTHC_REALMS_AD_EXTERNAL + ".follow_referrals", - ActiveDirectorySessionFactoryTests.FOLLOW_REFERRALS) - .put(getFullSettingKey("external",ActiveDirectorySessionFactorySettings.AD_LDAP_PORT_SETTING), AD_LDAP_PORT) - .put(getFullSettingKey("external",ActiveDirectorySessionFactorySettings.AD_LDAPS_PORT_SETTING), AD_LDAPS_PORT) - .put(getFullSettingKey("external",ActiveDirectorySessionFactorySettings.AD_GC_LDAP_PORT_SETTING), AD_GC_LDAP_PORT) - .put(getFullSettingKey("external",ActiveDirectorySessionFactorySettings.AD_GC_LDAPS_PORT_SETTING), AD_GC_LDAPS_PORT) - .build(), - "active_directory"), - - AD_LDAP_GROUPS_FROM_SEARCH(true, AD_ROLE_MAPPING, - Settings.builder() - .put(XPACK_SECURITY_AUTHC_REALMS_LDAP_EXTERNAL + ".url", ActiveDirectorySessionFactoryTests.AD_LDAP_URL) - .put(XPACK_SECURITY_AUTHC_REALMS_LDAP_EXTERNAL - + ".group_search.base_dn", "CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com") - .put(XPACK_SECURITY_AUTHC_REALMS_LDAP_EXTERNAL + ".group_search.scope", randomBoolean() ? SUB_TREE : ONE_LEVEL) - .putList(XPACK_SECURITY_AUTHC_REALMS_LDAP_EXTERNAL + ".user_dn_templates", - "cn={0},CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com") - .put(XPACK_SECURITY_AUTHC_REALMS_LDAP_EXTERNAL + ".follow_referrals", - ActiveDirectorySessionFactoryTests.FOLLOW_REFERRALS) - .build(), - "ldap"), - - AD_LDAP_GROUPS_FROM_ATTRIBUTE(true, AD_ROLE_MAPPING, - Settings.builder() - .put(XPACK_SECURITY_AUTHC_REALMS_LDAP_EXTERNAL + ".url", ActiveDirectorySessionFactoryTests.AD_LDAP_URL) - .putList(XPACK_SECURITY_AUTHC_REALMS_LDAP_EXTERNAL + ".user_dn_templates", - "cn={0},CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com") - .put(XPACK_SECURITY_AUTHC_REALMS_LDAP_EXTERNAL + ".follow_referrals", - ActiveDirectorySessionFactoryTests.FOLLOW_REFERRALS) - .build(), - "ldap"); + AD( + false, + AD_ROLE_MAPPING, + Settings.builder() + .put(XPACK_SECURITY_AUTHC_REALMS_AD_EXTERNAL + ".domain_name", ActiveDirectorySessionFactoryTests.AD_DOMAIN) + .put(XPACK_SECURITY_AUTHC_REALMS_AD_EXTERNAL + ".group_search.base_dn", "CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com") + .put(XPACK_SECURITY_AUTHC_REALMS_AD_EXTERNAL + ".group_search.scope", randomBoolean() ? SUB_TREE : ONE_LEVEL) + .put(XPACK_SECURITY_AUTHC_REALMS_AD_EXTERNAL + ".url", ActiveDirectorySessionFactoryTests.AD_LDAP_URL) + .put(XPACK_SECURITY_AUTHC_REALMS_AD_EXTERNAL + ".follow_referrals", ActiveDirectorySessionFactoryTests.FOLLOW_REFERRALS) + .put(getFullSettingKey("external", ActiveDirectorySessionFactorySettings.AD_LDAP_PORT_SETTING), AD_LDAP_PORT) + .put(getFullSettingKey("external", ActiveDirectorySessionFactorySettings.AD_LDAPS_PORT_SETTING), AD_LDAPS_PORT) + .put(getFullSettingKey("external", ActiveDirectorySessionFactorySettings.AD_GC_LDAP_PORT_SETTING), AD_GC_LDAP_PORT) + .put(getFullSettingKey("external", ActiveDirectorySessionFactorySettings.AD_GC_LDAPS_PORT_SETTING), AD_GC_LDAPS_PORT) + .build(), + "active_directory" + ), + + AD_LDAP_GROUPS_FROM_SEARCH( + true, + AD_ROLE_MAPPING, + Settings.builder() + .put(XPACK_SECURITY_AUTHC_REALMS_LDAP_EXTERNAL + ".url", ActiveDirectorySessionFactoryTests.AD_LDAP_URL) + .put(XPACK_SECURITY_AUTHC_REALMS_LDAP_EXTERNAL + ".group_search.base_dn", "CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com") + .put(XPACK_SECURITY_AUTHC_REALMS_LDAP_EXTERNAL + ".group_search.scope", randomBoolean() ? SUB_TREE : ONE_LEVEL) + .putList( + XPACK_SECURITY_AUTHC_REALMS_LDAP_EXTERNAL + ".user_dn_templates", + "cn={0},CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com" + ) + .put(XPACK_SECURITY_AUTHC_REALMS_LDAP_EXTERNAL + ".follow_referrals", ActiveDirectorySessionFactoryTests.FOLLOW_REFERRALS) + .build(), + "ldap" + ), + + AD_LDAP_GROUPS_FROM_ATTRIBUTE( + true, + AD_ROLE_MAPPING, + Settings.builder() + .put(XPACK_SECURITY_AUTHC_REALMS_LDAP_EXTERNAL + ".url", ActiveDirectorySessionFactoryTests.AD_LDAP_URL) + .putList( + XPACK_SECURITY_AUTHC_REALMS_LDAP_EXTERNAL + ".user_dn_templates", + "cn={0},CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com" + ) + .put(XPACK_SECURITY_AUTHC_REALMS_LDAP_EXTERNAL + ".follow_referrals", ActiveDirectorySessionFactoryTests.FOLLOW_REFERRALS) + .build(), + "ldap" + ); final String type; final boolean mapGroupsAsRoles; @@ -375,7 +393,6 @@ public Settings buildSettings(List certificateAuthorities) { return buildSettings(certificateAuthorities, randomInt()); } - protected Settings buildSettings(List certificateAuthorities, int order) { Settings.Builder builder = Settings.builder() .put("xpack.security.authc.realms." + type + ".external.order", order) @@ -391,9 +408,7 @@ public List selectRoleMappings(Supplier shouldPickFil if (mapGroupsAsRoles) { return Collections.emptyList(); } else { - return Arrays.stream(this.roleMappings) - .map(e -> e.pickEntry(shouldPickFileContent)) - .collect(Collectors.toList()); + return Arrays.stream(this.roleMappings).map(e -> e.pickEntry(shouldPickFileContent)).collect(Collectors.toList()); } } } diff --git a/x-pack/qa/third-party/active-directory/src/test/java/org/elasticsearch/xpack/security/authc/ldap/ActiveDirectoryGroupsResolverTests.java b/x-pack/qa/third-party/active-directory/src/test/java/org/elasticsearch/xpack/security/authc/ldap/ActiveDirectoryGroupsResolverTests.java index 06ae015ddc902..d8f82c6419501 100644 --- a/x-pack/qa/third-party/active-directory/src/test/java/org/elasticsearch/xpack/security/authc/ldap/ActiveDirectoryGroupsResolverTests.java +++ b/x-pack/qa/third-party/active-directory/src/test/java/org/elasticsearch/xpack/security/authc/ldap/ActiveDirectoryGroupsResolverTests.java @@ -7,6 +7,7 @@ package org.elasticsearch.xpack.security.authc.ldap; import com.unboundid.ldap.sdk.Filter; + import org.elasticsearch.action.support.PlainActionFuture; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.TimeValue; @@ -18,7 +19,6 @@ import java.util.List; import java.util.regex.Pattern; - import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.hasItem; @@ -26,8 +26,7 @@ public class ActiveDirectoryGroupsResolverTests extends GroupsResolverTestCase { - private static final String BRUCE_BANNER_DN = - "cn=Bruce Banner,CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com"; + private static final String BRUCE_BANNER_DN = "cn=Bruce Banner,CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com"; private static final RealmConfig.RealmIdentifier REALM_ID = new RealmConfig.RealmIdentifier("active_directory", "ad"); @@ -38,72 +37,94 @@ public void setReferralFollowing() { public void testResolveSubTree() throws Exception { Settings settings = Settings.builder() - .put("xpack.security.authc.realms.active_directory.ad.group_search.scope", LdapSearchScope.SUB_TREE) - .put("xpack.security.authc.realms.active_directory.ad.group_search.base_dn", "DC=ad,DC=test,DC=elasticsearch,DC=com") - .put("xpack.security.authc.realms.active_directory.ad.domain_name", "ad.test.elasticsearch.com") - .build(); + .put("xpack.security.authc.realms.active_directory.ad.group_search.scope", LdapSearchScope.SUB_TREE) + .put("xpack.security.authc.realms.active_directory.ad.group_search.base_dn", "DC=ad,DC=test,DC=elasticsearch,DC=com") + .put("xpack.security.authc.realms.active_directory.ad.domain_name", "ad.test.elasticsearch.com") + .build(); ActiveDirectoryGroupsResolver resolver = new ActiveDirectoryGroupsResolver(config(REALM_ID, settings)); - List groups = resolveBlocking(resolver, ldapConnection, BRUCE_BANNER_DN, - TimeValue.timeValueSeconds(10), NoOpLogger.INSTANCE, null); - assertThat(groups, containsInAnyOrder( + List groups = resolveBlocking( + resolver, + ldapConnection, + BRUCE_BANNER_DN, + TimeValue.timeValueSeconds(10), + NoOpLogger.INSTANCE, + null + ); + assertThat( + groups, + containsInAnyOrder( containsString("Avengers"), containsString("SHIELD"), containsString("Geniuses"), containsString("Philanthropists"), containsString("CN=Users,CN=Builtin"), containsString("Domain Users"), - containsString("Supers"))); + containsString("Supers") + ) + ); } public void testResolveOneLevel() throws Exception { Settings settings = Settings.builder() - .put("xpack.security.authc.realms.active_directory.ad.scope", LdapSearchScope.ONE_LEVEL) - .put("xpack.security.authc.realms.active_directory.ad.group_search.base_dn", - "CN=Builtin, DC=ad, DC=test, DC=elasticsearch,DC=com") - .put("xpack.security.authc.realms.active_directory.ad.domain_name", "ad.test.elasticsearch.com") - .build(); + .put("xpack.security.authc.realms.active_directory.ad.scope", LdapSearchScope.ONE_LEVEL) + .put( + "xpack.security.authc.realms.active_directory.ad.group_search.base_dn", + "CN=Builtin, DC=ad, DC=test, DC=elasticsearch,DC=com" + ) + .put("xpack.security.authc.realms.active_directory.ad.domain_name", "ad.test.elasticsearch.com") + .build(); ActiveDirectoryGroupsResolver resolver = new ActiveDirectoryGroupsResolver(config(REALM_ID, settings)); - List groups = resolveBlocking(resolver, ldapConnection, BRUCE_BANNER_DN, - TimeValue.timeValueSeconds(10), NoOpLogger.INSTANCE, null); + List groups = resolveBlocking( + resolver, + ldapConnection, + BRUCE_BANNER_DN, + TimeValue.timeValueSeconds(10), + NoOpLogger.INSTANCE, + null + ); assertThat(groups, hasItem(containsString("Users"))); } public void testResolveBaseLevel() throws Exception { Settings settings = Settings.builder() - .put("xpack.security.authc.realms.active_directory.ad.group_search.scope", LdapSearchScope.BASE) - .put("xpack.security.authc.realms.active_directory.ad.group_search.base_dn", - "CN=Users, CN=Builtin, DC=ad, DC=test, DC=elasticsearch, DC=com") - .put("xpack.security.authc.realms.active_directory.ad.domain_name", "ad.test.elasticsearch.com") - .build(); + .put("xpack.security.authc.realms.active_directory.ad.group_search.scope", LdapSearchScope.BASE) + .put( + "xpack.security.authc.realms.active_directory.ad.group_search.base_dn", + "CN=Users, CN=Builtin, DC=ad, DC=test, DC=elasticsearch, DC=com" + ) + .put("xpack.security.authc.realms.active_directory.ad.domain_name", "ad.test.elasticsearch.com") + .build(); ActiveDirectoryGroupsResolver resolver = new ActiveDirectoryGroupsResolver(config(REALM_ID, settings)); - List groups = resolveBlocking(resolver, ldapConnection, BRUCE_BANNER_DN, - TimeValue.timeValueSeconds(10), NoOpLogger.INSTANCE, null); + List groups = resolveBlocking( + resolver, + ldapConnection, + BRUCE_BANNER_DN, + TimeValue.timeValueSeconds(10), + NoOpLogger.INSTANCE, + null + ); assertThat(groups, hasItem(containsString("CN=Users,CN=Builtin"))); } public void testBuildGroupQuery() throws Exception { - //test a user with no assigned groups, other than the default groups + // test a user with no assigned groups, other than the default groups { - String[] expectedSids = new String[]{ - "S-1-5-32-545", //Default Users group + String[] expectedSids = new String[] { "S-1-5-32-545", // Default Users group }; final String dn = "CN=Jarvis, CN=Users, DC=ad, DC=test, DC=elasticsearch, DC=com"; PlainActionFuture future = new PlainActionFuture<>(); - ActiveDirectoryGroupsResolver.buildGroupQuery(ldapConnection, dn, - TimeValue.timeValueSeconds(10), false, future); + ActiveDirectoryGroupsResolver.buildGroupQuery(ldapConnection, dn, TimeValue.timeValueSeconds(10), false, future); Filter query = future.actionGet(); assertValidSidQuery(query, expectedSids); } - //test a user of one groups + // test a user of one groups { - String[] expectedSids = new String[]{ - "S-1-5-32-545" //Default Users group + String[] expectedSids = new String[] { "S-1-5-32-545" // Default Users group }; final String dn = "CN=Odin, CN=Users, DC=ad, DC=test, DC=elasticsearch, DC=com"; PlainActionFuture future = new PlainActionFuture<>(); - ActiveDirectoryGroupsResolver.buildGroupQuery(ldapConnection, dn, - TimeValue.timeValueSeconds(10), false, future); + ActiveDirectoryGroupsResolver.buildGroupQuery(ldapConnection, dn, TimeValue.timeValueSeconds(10), false, future); Filter query = future.actionGet(); assertValidSidQuery(query, expectedSids); } @@ -112,8 +133,11 @@ public void testBuildGroupQuery() throws Exception { private void assertValidSidQuery(Filter query, String[] expectedSids) { String queryString = query.toString(); Pattern sidQueryPattern = Pattern.compile("\\(\\|(\\(objectSid=S(-\\d+)+\\))+\\)"); - assertThat("[" + queryString + "] didn't match the search filter pattern", - sidQueryPattern.matcher(queryString).matches(), is(true)); + assertThat( + "[" + queryString + "] didn't match the search filter pattern", + sidQueryPattern.matcher(queryString).matches(), + is(true) + ); for (String sid : expectedSids) { assertThat(queryString, containsString(sid)); } diff --git a/x-pack/qa/third-party/active-directory/src/test/java/org/elasticsearch/xpack/security/authc/ldap/ActiveDirectoryRunAsIT.java b/x-pack/qa/third-party/active-directory/src/test/java/org/elasticsearch/xpack/security/authc/ldap/ActiveDirectoryRunAsIT.java index 20b8e0928c619..31e4ce4d516db 100644 --- a/x-pack/qa/third-party/active-directory/src/test/java/org/elasticsearch/xpack/security/authc/ldap/ActiveDirectoryRunAsIT.java +++ b/x-pack/qa/third-party/active-directory/src/test/java/org/elasticsearch/xpack/security/authc/ldap/ActiveDirectoryRunAsIT.java @@ -45,13 +45,15 @@ protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { switch (realmConfig) { case AD: builder.put(XPACK_SECURITY_AUTHC_REALMS_AD_EXTERNAL + ".bind_dn", "ironman@ad.test.elasticsearch.com") - .put(XPACK_SECURITY_AUTHC_REALMS_AD_EXTERNAL + ".user_search.pool.enabled", false); + .put(XPACK_SECURITY_AUTHC_REALMS_AD_EXTERNAL + ".user_search.pool.enabled", false); if (useLegacyBindPassword) { builder.put(XPACK_SECURITY_AUTHC_REALMS_AD_EXTERNAL + ".bind_password", ActiveDirectorySessionFactoryTests.PASSWORD); } else { SecuritySettingsSource.addSecureSettings(builder, secureSettings -> { - secureSettings.setString(XPACK_SECURITY_AUTHC_REALMS_AD_EXTERNAL + ".secure_bind_password", - ActiveDirectorySessionFactoryTests.PASSWORD); + secureSettings.setString( + XPACK_SECURITY_AUTHC_REALMS_AD_EXTERNAL + ".secure_bind_password", + ActiveDirectorySessionFactoryTests.PASSWORD + ); }); } break; @@ -71,9 +73,9 @@ public void testRunAs() throws Exception { protected Client runAsClient(String user) { final Map headers = MapBuilder.newMapBuilder() - .put(BASIC_AUTH_HEADER, UsernamePasswordToken.basicAuthHeaderValue(ElasticUser.NAME, BOOTSTRAP_PASSWORD)) - .put(AuthenticationServiceField.RUN_AS_USER_HEADER, user) - .map(); + .put(BASIC_AUTH_HEADER, UsernamePasswordToken.basicAuthHeaderValue(ElasticUser.NAME, BOOTSTRAP_PASSWORD)) + .put(AuthenticationServiceField.RUN_AS_USER_HEADER, user) + .map(); return client().filterWithHeader(headers); } diff --git a/x-pack/qa/third-party/active-directory/src/test/java/org/elasticsearch/xpack/security/authc/ldap/ActiveDirectorySessionFactoryTests.java b/x-pack/qa/third-party/active-directory/src/test/java/org/elasticsearch/xpack/security/authc/ldap/ActiveDirectorySessionFactoryTests.java index 974e1b05936e3..43eeb69ed3fdf 100644 --- a/x-pack/qa/third-party/active-directory/src/test/java/org/elasticsearch/xpack/security/authc/ldap/ActiveDirectorySessionFactoryTests.java +++ b/x-pack/qa/third-party/active-directory/src/test/java/org/elasticsearch/xpack/security/authc/ldap/ActiveDirectorySessionFactoryTests.java @@ -8,13 +8,14 @@ import com.unboundid.ldap.sdk.LDAPException; import com.unboundid.ldap.sdk.ResultCode; + import org.elasticsearch.action.support.PlainActionFuture; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.ssl.SslVerificationMode; -import org.elasticsearch.core.TimeValue; import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.common.util.concurrent.UncategorizedExecutionException; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.env.Environment; import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.threadpool.TestThreadPool; @@ -81,7 +82,9 @@ public void testAdAuth() throws Exception { try (LdapSession ldap = session(sessionFactory, userName, SECURED_PASSWORD)) { assertConnectionCanReconnect(ldap.getConnection()); List groups = groups(ldap); - assertThat(groups, containsInAnyOrder( + assertThat( + groups, + containsInAnyOrder( containsString("Geniuses"), containsString("Billionaire"), containsString("Playboy"), @@ -90,7 +93,9 @@ public void testAdAuth() throws Exception { containsString("SHIELD"), containsString("CN=Users,CN=Builtin"), containsString("Domain Users"), - containsString("Supers"))); + containsString("Supers") + ) + ); } } } @@ -117,7 +122,9 @@ public void testNetbiosAuth() throws Exception { try (LdapSession ldap = session(sessionFactory, userName, SECURED_PASSWORD)) { assertConnectionCanReconnect(ldap.getConnection()); List groups = groups(ldap); - assertThat(groups, containsInAnyOrder( + assertThat( + groups, + containsInAnyOrder( containsString("Geniuses"), containsString("Billionaire"), containsString("Playboy"), @@ -126,7 +133,9 @@ public void testNetbiosAuth() throws Exception { containsString("SHIELD"), containsString("CN=Users,CN=Builtin"), containsString("Domain Users"), - containsString("Supers"))); + containsString("Supers") + ) + ); } } } @@ -135,7 +144,7 @@ public void testAdAuthAvengers() throws Exception { RealmConfig config = configureRealm("ad-test", LdapRealmSettings.AD_TYPE, buildAdSettings(AD_LDAP_URL, AD_DOMAIN, false)); try (ActiveDirectorySessionFactory sessionFactory = getActiveDirectorySessionFactory(config, sslService, threadPool)) { - String[] users = new String[]{"cap", "hawkeye", "hulk", "ironman", "thor", "blackwidow"}; + String[] users = new String[] { "cap", "hawkeye", "hulk", "ironman", "thor", "blackwidow" }; for (String user : users) { try (LdapSession ldap = session(sessionFactory, user, SECURED_PASSWORD)) { assertConnectionCanReconnect(ldap.getConnection()); @@ -146,8 +155,14 @@ public void testAdAuthAvengers() throws Exception { } public void testAuthenticate() throws Exception { - Settings settings = buildAdSettings(REALM_ID, AD_LDAP_URL, AD_DOMAIN, "CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com", - LdapSearchScope.ONE_LEVEL, false); + Settings settings = buildAdSettings( + REALM_ID, + AD_LDAP_URL, + AD_DOMAIN, + "CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com", + LdapSearchScope.ONE_LEVEL, + false + ); RealmConfig config = configureRealm("ad-test", LdapRealmSettings.AD_TYPE, settings); try (ActiveDirectorySessionFactory sessionFactory = getActiveDirectorySessionFactory(config, sslService, threadPool)) { @@ -156,21 +171,31 @@ public void testAuthenticate() throws Exception { assertConnectionCanReconnect(ldap.getConnection()); List groups = groups(ldap); - assertThat(groups, containsInAnyOrder( + assertThat( + groups, + containsInAnyOrder( containsString("Avengers"), containsString("SHIELD"), containsString("Geniuses"), containsString("Philanthropists"), containsString("CN=Users,CN=Builtin"), containsString("Domain Users"), - containsString("Supers"))); + containsString("Supers") + ) + ); } } } public void testAuthenticateBaseUserSearch() throws Exception { - Settings settings = buildAdSettings(REALM_ID, AD_LDAP_URL, AD_DOMAIN, - "CN=Bruce Banner, CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com", LdapSearchScope.BASE, false); + Settings settings = buildAdSettings( + REALM_ID, + AD_LDAP_URL, + AD_DOMAIN, + "CN=Bruce Banner, CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com", + LdapSearchScope.BASE, + false + ); RealmConfig config = configureRealm("ad-test", LdapRealmSettings.AD_TYPE, settings); try (ActiveDirectorySessionFactory sessionFactory = getActiveDirectorySessionFactory(config, sslService, threadPool)) { @@ -179,26 +204,40 @@ public void testAuthenticateBaseUserSearch() throws Exception { assertConnectionCanReconnect(ldap.getConnection()); List groups = groups(ldap); - assertThat(groups, containsInAnyOrder( + assertThat( + groups, + containsInAnyOrder( containsString("Avengers"), containsString("SHIELD"), containsString("Geniuses"), containsString("Philanthropists"), containsString("CN=Users,CN=Builtin"), containsString("Domain Users"), - containsString("Supers"))); + containsString("Supers") + ) + ); } } } public void testAuthenticateBaseGroupSearch() throws Exception { Settings settings = Settings.builder() - .put(buildAdSettings(REALM_ID, AD_LDAP_URL, AD_DOMAIN, "CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com", - LdapSearchScope.ONE_LEVEL, false)) - .put(ActiveDirectorySessionFactorySettings.AD_GROUP_SEARCH_BASEDN_SETTING, - "CN=Avengers,CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com") - .put(ActiveDirectorySessionFactorySettings.AD_GROUP_SEARCH_SCOPE_SETTING, LdapSearchScope.BASE) - .build(); + .put( + buildAdSettings( + REALM_ID, + AD_LDAP_URL, + AD_DOMAIN, + "CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com", + LdapSearchScope.ONE_LEVEL, + false + ) + ) + .put( + ActiveDirectorySessionFactorySettings.AD_GROUP_SEARCH_BASEDN_SETTING, + "CN=Avengers,CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com" + ) + .put(ActiveDirectorySessionFactorySettings.AD_GROUP_SEARCH_SCOPE_SETTING, LdapSearchScope.BASE) + .build(); RealmConfig config = configureRealm("ad-test", LdapRealmSettings.AD_TYPE, settings); try (ActiveDirectorySessionFactory sessionFactory = getActiveDirectorySessionFactory(config, sslService, threadPool)) { @@ -213,76 +252,110 @@ public void testAuthenticateBaseGroupSearch() throws Exception { } public void testAuthenticateWithUserPrincipalName() throws Exception { - Settings settings = buildAdSettings(REALM_ID, AD_LDAP_URL, AD_DOMAIN, "CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com", - LdapSearchScope.ONE_LEVEL, false); + Settings settings = buildAdSettings( + REALM_ID, + AD_LDAP_URL, + AD_DOMAIN, + "CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com", + LdapSearchScope.ONE_LEVEL, + false + ); RealmConfig config = configureRealm("ad-test", LdapRealmSettings.AD_TYPE, settings); try (ActiveDirectorySessionFactory sessionFactory = getActiveDirectorySessionFactory(config, sslService, threadPool)) { - //Login with the UserPrincipalName + // Login with the UserPrincipalName String userDN = "CN=Erik Selvig,CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com"; try (LdapSession ldap = session(sessionFactory, "erik.selvig", SECURED_PASSWORD)) { assertConnectionCanReconnect(ldap.getConnection()); List groups = groups(ldap); assertThat(ldap.userDn(), is(userDN)); - assertThat(groups, containsInAnyOrder( - containsString("Geniuses"), - containsString("CN=Users,CN=Builtin"), - containsString("Domain Users"))); + assertThat( + groups, + containsInAnyOrder(containsString("Geniuses"), containsString("CN=Users,CN=Builtin"), containsString("Domain Users")) + ); } } } public void testAuthenticateWithSAMAccountName() throws Exception { - Settings settings = buildAdSettings(REALM_ID, AD_LDAP_URL, AD_DOMAIN, "CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com", - LdapSearchScope.ONE_LEVEL, false); + Settings settings = buildAdSettings( + REALM_ID, + AD_LDAP_URL, + AD_DOMAIN, + "CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com", + LdapSearchScope.ONE_LEVEL, + false + ); RealmConfig config = configureRealm("ad-test", LdapRealmSettings.AD_TYPE, settings); try (ActiveDirectorySessionFactory sessionFactory = getActiveDirectorySessionFactory(config, sslService, threadPool)) { - //login with sAMAccountName + // login with sAMAccountName String userDN = "CN=Erik Selvig,CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com"; try (LdapSession ldap = session(sessionFactory, "selvig", SECURED_PASSWORD)) { assertConnectionCanReconnect(ldap.getConnection()); assertThat(ldap.userDn(), is(userDN)); List groups = groups(ldap); - assertThat(groups, containsInAnyOrder( - containsString("Geniuses"), - containsString("CN=Users,CN=Builtin"), - containsString("Domain Users"))); + assertThat( + groups, + containsInAnyOrder(containsString("Geniuses"), containsString("CN=Users,CN=Builtin"), containsString("Domain Users")) + ); } } } public void testCustomUserFilter() throws Exception { Settings settings = Settings.builder() - .put(buildAdSettings(REALM_ID, AD_LDAP_URL, AD_DOMAIN, "CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com", - LdapSearchScope.SUB_TREE, false)) - .put(getFullSettingKey(REALM_ID.getName(), ActiveDirectorySessionFactorySettings.AD_USER_SEARCH_FILTER_SETTING), - "(&(objectclass=user)(userPrincipalName={0}@ad.test.elasticsearch.com))") - .build(); + .put( + buildAdSettings( + REALM_ID, + AD_LDAP_URL, + AD_DOMAIN, + "CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com", + LdapSearchScope.SUB_TREE, + false + ) + ) + .put( + getFullSettingKey(REALM_ID.getName(), ActiveDirectorySessionFactorySettings.AD_USER_SEARCH_FILTER_SETTING), + "(&(objectclass=user)(userPrincipalName={0}@ad.test.elasticsearch.com))" + ) + .build(); RealmConfig config = configureRealm("ad-test", LdapRealmSettings.AD_TYPE, settings); try (ActiveDirectorySessionFactory sessionFactory = getActiveDirectorySessionFactory(config, sslService, threadPool)) { - //Login with the UserPrincipalName + // Login with the UserPrincipalName try (LdapSession ldap = session(sessionFactory, "erik.selvig", SECURED_PASSWORD)) { assertConnectionCanReconnect(ldap.getConnection()); List groups = groups(ldap); - assertThat(groups, containsInAnyOrder( + assertThat( + groups, + containsInAnyOrder( containsString("CN=Geniuses"), containsString("CN=Domain Users"), - containsString("CN=Users,CN=Builtin"))); + containsString("CN=Users,CN=Builtin") + ) + ); } } } - public void testStandardLdapConnection() throws Exception { String groupSearchBase = "DC=ad,DC=test,DC=elasticsearch,DC=com"; String userTemplate = "CN={0},CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com"; final RealmConfig.RealmIdentifier realmId = new RealmConfig.RealmIdentifier(LdapRealmSettings.LDAP_TYPE, "ad-as-ldap-test"); final Settings settings = Settings.builder() - .put(LdapTestCase.buildLdapSettings(realmId, new String[]{AD_LDAP_URL}, new String[]{userTemplate}, groupSearchBase, - LdapSearchScope.SUB_TREE, null, false)) + .put( + LdapTestCase.buildLdapSettings( + realmId, + new String[] { AD_LDAP_URL }, + new String[] { userTemplate }, + groupSearchBase, + LdapSearchScope.SUB_TREE, + null, + false + ) + ) .putList(RealmSettings.realmSslPrefix(realmId) + "certificate_authorities", certificatePaths) .put(getFullSettingKey(realmId, SessionFactorySettings.FOLLOW_REFERRALS_SETTING), FOLLOW_REFERRALS) .build(); @@ -294,11 +367,15 @@ public void testStandardLdapConnection() throws Exception { assertConnectionCanReconnect(ldap.getConnection()); List groups = groups(ldap); - assertThat(groups, containsInAnyOrder( + assertThat( + groups, + containsInAnyOrder( containsString("Avengers"), containsString("SHIELD"), containsString("Geniuses"), - containsString("Philanthropists"))); + containsString("Philanthropists") + ) + ); } } @@ -308,8 +385,17 @@ public void testHandlingLdapReferralErrors() throws Exception { String userTemplate = "CN={0},CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com"; final RealmConfig.RealmIdentifier realmId = new RealmConfig.RealmIdentifier(LdapRealmSettings.LDAP_TYPE, "ad-as-ldap-test"); Settings settings = Settings.builder() - .put(LdapTestCase.buildLdapSettings(realmId, new String[]{AD_LDAP_URL}, new String[]{userTemplate}, groupSearchBase, - LdapSearchScope.SUB_TREE, null, false)) + .put( + LdapTestCase.buildLdapSettings( + realmId, + new String[] { AD_LDAP_URL }, + new String[] { userTemplate }, + groupSearchBase, + LdapSearchScope.SUB_TREE, + null, + false + ) + ) .putList(RealmSettings.realmSslPrefix(realmId) + "certificate_authorities", certificatePaths) .put(getFullSettingKey(realmId, SessionFactorySettings.FOLLOW_REFERRALS_SETTING), FOLLOW_REFERRALS) .build(); @@ -319,10 +405,7 @@ public void testHandlingLdapReferralErrors() throws Exception { String user = "Bruce Banner"; try (LdapSession ldap = session(sessionFactory, user, SECURED_PASSWORD)) { - final UncategorizedExecutionException exception = expectThrows( - UncategorizedExecutionException.class, - () -> groups(ldap) - ); + final UncategorizedExecutionException exception = expectThrows(UncategorizedExecutionException.class, () -> groups(ldap)); final Throwable cause = exception.getCause(); assertThat(cause, instanceOf(ExecutionException.class)); assertThat(cause.getCause(), instanceOf(LDAPException.class)); @@ -336,8 +419,17 @@ public void testStandardLdapWithAttributeGroups() throws Exception { String groupSearchBase = "DC=ad,DC=test,DC=elasticsearch,DC=com"; final RealmConfig.RealmIdentifier realmId = new RealmConfig.RealmIdentifier(LdapRealmSettings.LDAP_TYPE, "ad-as-ldap-test"); Settings settings = Settings.builder() - .put(LdapTestCase.buildLdapSettings(realmId, new String[]{AD_LDAP_URL}, new String[]{userTemplate}, groupSearchBase, - LdapSearchScope.SUB_TREE, null, false)) + .put( + LdapTestCase.buildLdapSettings( + realmId, + new String[] { AD_LDAP_URL }, + new String[] { userTemplate }, + groupSearchBase, + LdapSearchScope.SUB_TREE, + null, + false + ) + ) .putList("ssl.certificate_authorities", certificatePaths) .putList(RealmSettings.realmSslPrefix(realmId) + "certificate_authorities", certificatePaths) .put(getFullSettingKey(realmId, SessionFactorySettings.FOLLOW_REFERRALS_SETTING), FOLLOW_REFERRALS) @@ -350,11 +442,15 @@ public void testStandardLdapWithAttributeGroups() throws Exception { assertConnectionCanReconnect(ldap.getConnection()); List groups = groups(ldap); - assertThat(groups, containsInAnyOrder( + assertThat( + groups, + containsInAnyOrder( containsString("Avengers"), containsString("SHIELD"), containsString("Geniuses"), - containsString("Philanthropists"))); + containsString("Philanthropists") + ) + ); } } @@ -362,10 +458,28 @@ public void testADLookup() throws Exception { RealmConfig config = configureRealm("ad-test", LdapRealmSettings.AD_TYPE, buildAdSettings(AD_LDAP_URL, AD_DOMAIN, false, true)); try (ActiveDirectorySessionFactory sessionFactory = getActiveDirectorySessionFactory(config, sslService, threadPool)) { - List users = randomSubsetOf(Arrays.asList("cap", "hawkeye", "hulk", "ironman", "thor", "blackwidow", - "cap@ad.test.elasticsearch.com", "hawkeye@ad.test.elasticsearch.com", "hulk@ad.test.elasticsearch.com", - "ironman@ad.test.elasticsearch.com", "thor@ad.test.elasticsearch.com", "blackwidow@ad.test.elasticsearch.com", - "ADES\\cap", "ADES\\hawkeye", "ADES\\hulk", "ADES\\ironman", "ADES\\thor", "ADES\\blackwidow")); + List users = randomSubsetOf( + Arrays.asList( + "cap", + "hawkeye", + "hulk", + "ironman", + "thor", + "blackwidow", + "cap@ad.test.elasticsearch.com", + "hawkeye@ad.test.elasticsearch.com", + "hulk@ad.test.elasticsearch.com", + "ironman@ad.test.elasticsearch.com", + "thor@ad.test.elasticsearch.com", + "blackwidow@ad.test.elasticsearch.com", + "ADES\\cap", + "ADES\\hawkeye", + "ADES\\hulk", + "ADES\\ironman", + "ADES\\thor", + "ADES\\blackwidow" + ) + ); for (String user : users) { try (LdapSession ldap = unauthenticatedSession(sessionFactory, user)) { assertConnectionCanReconnect(ldap.getConnection()); @@ -381,8 +495,16 @@ public void testResolveTokenGroupsSID() throws Exception { Settings settings = Settings.builder() .put("path.home", createTempDir()) .put(RealmSettings.getFullSettingKey(REALM_ID, RealmSettings.ORDER_SETTING), 0) - .put(buildAdSettings(REALM_ID, AD_LDAP_URL, AD_DOMAIN, "CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com", - LdapSearchScope.SUB_TREE, false)) + .put( + buildAdSettings( + REALM_ID, + AD_LDAP_URL, + AD_DOMAIN, + "CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com", + LdapSearchScope.SUB_TREE, + false + ) + ) .put(ActiveDirectorySessionFactorySettings.AD_GROUP_SEARCH_BASEDN_SETTING, "DC=ad,DC=test,DC=elasticsearch,DC=com") .put(ActiveDirectorySessionFactorySettings.AD_GROUP_SEARCH_SCOPE_SETTING, LdapSearchScope.SUB_TREE) .put(getFullSettingKey(REALM_ID, LdapMetadataResolverSettings.ADDITIONAL_METADATA_SETTING), "tokenGroups") @@ -411,16 +533,18 @@ private Settings buildAdSettings(String ldapUrl, String adDomainName, boolean ho private Settings buildAdSettings(String ldapUrl, String adDomainName, boolean hostnameVerification, boolean useBindUser) { Settings.Builder builder = Settings.builder() - .put(getFullSettingKey(REALM_ID, SessionFactorySettings.URLS_SETTING), ldapUrl) - .put(getFullSettingKey(REALM_ID, ActiveDirectorySessionFactorySettings.AD_DOMAIN_NAME_SETTING), adDomainName) - .put(getFullSettingKey(REALM_NAME, ActiveDirectorySessionFactorySettings.AD_LDAP_PORT_SETTING), AD_LDAP_PORT) - .put(getFullSettingKey(REALM_NAME, ActiveDirectorySessionFactorySettings.AD_LDAPS_PORT_SETTING), AD_LDAPS_PORT) - .put(getFullSettingKey(REALM_NAME, ActiveDirectorySessionFactorySettings.AD_GC_LDAP_PORT_SETTING), AD_GC_LDAP_PORT) - .put(getFullSettingKey(REALM_NAME, ActiveDirectorySessionFactorySettings.AD_GC_LDAPS_PORT_SETTING), AD_GC_LDAPS_PORT) - .put(getFullSettingKey(REALM_ID, SessionFactorySettings.FOLLOW_REFERRALS_SETTING), FOLLOW_REFERRALS); + .put(getFullSettingKey(REALM_ID, SessionFactorySettings.URLS_SETTING), ldapUrl) + .put(getFullSettingKey(REALM_ID, ActiveDirectorySessionFactorySettings.AD_DOMAIN_NAME_SETTING), adDomainName) + .put(getFullSettingKey(REALM_NAME, ActiveDirectorySessionFactorySettings.AD_LDAP_PORT_SETTING), AD_LDAP_PORT) + .put(getFullSettingKey(REALM_NAME, ActiveDirectorySessionFactorySettings.AD_LDAPS_PORT_SETTING), AD_LDAPS_PORT) + .put(getFullSettingKey(REALM_NAME, ActiveDirectorySessionFactorySettings.AD_GC_LDAP_PORT_SETTING), AD_GC_LDAP_PORT) + .put(getFullSettingKey(REALM_NAME, ActiveDirectorySessionFactorySettings.AD_GC_LDAPS_PORT_SETTING), AD_GC_LDAPS_PORT) + .put(getFullSettingKey(REALM_ID, SessionFactorySettings.FOLLOW_REFERRALS_SETTING), FOLLOW_REFERRALS); if (randomBoolean()) { - builder.put(getFullSettingKey(REALM_ID, SSLConfigurationSettings.VERIFICATION_MODE_SETTING_REALM), - hostnameVerification ? SslVerificationMode.FULL : SslVerificationMode.CERTIFICATE); + builder.put( + getFullSettingKey(REALM_ID, SSLConfigurationSettings.VERIFICATION_MODE_SETTING_REALM), + hostnameVerification ? SslVerificationMode.FULL : SslVerificationMode.CERTIFICATE + ); } else { builder.put(getFullSettingKey(REALM_ID, SessionFactorySettings.HOSTNAME_VERIFICATION_SETTING), hostnameVerification); } @@ -428,14 +552,29 @@ private Settings buildAdSettings(String ldapUrl, String adDomainName, boolean ho builder.putList(getFullSettingKey(REALM_ID, SSLConfigurationSettings.CAPATH_SETTING_REALM), certificatePaths); if (useBindUser) { - final String user = randomFrom("cap", "hawkeye", "hulk", "ironman", "thor", "blackwidow", "cap@ad.test.elasticsearch.com", - "hawkeye@ad.test.elasticsearch.com", "hulk@ad.test.elasticsearch.com", "ironman@ad.test.elasticsearch.com", - "thor@ad.test.elasticsearch.com", "blackwidow@ad.test.elasticsearch.com", "ADES\\cap", "ADES\\hawkeye", "ADES\\hulk", - "ADES\\ironman", "ADES\\thor", "ADES\\blackwidow", "CN=Bruce Banner,CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com"); + final String user = randomFrom( + "cap", + "hawkeye", + "hulk", + "ironman", + "thor", + "blackwidow", + "cap@ad.test.elasticsearch.com", + "hawkeye@ad.test.elasticsearch.com", + "hulk@ad.test.elasticsearch.com", + "ironman@ad.test.elasticsearch.com", + "thor@ad.test.elasticsearch.com", + "blackwidow@ad.test.elasticsearch.com", + "ADES\\cap", + "ADES\\hawkeye", + "ADES\\hulk", + "ADES\\ironman", + "ADES\\thor", + "ADES\\blackwidow", + "CN=Bruce Banner,CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com" + ); final boolean poolingEnabled = randomBoolean(); - builder.put("bind_dn", user) - .put("bind_password", PASSWORD) - .put("user_search.pool.enabled", poolingEnabled); + builder.put("bind_dn", user).put("bind_password", PASSWORD).put("user_search.pool.enabled", poolingEnabled); logger.info("using bind user [{}] with pooling enabled [{}]", user, poolingEnabled); } return builder.build(); @@ -460,7 +599,7 @@ private List groups(LdapSession ldapSession) { } static ActiveDirectorySessionFactory getActiveDirectorySessionFactory(RealmConfig config, SSLService sslService, ThreadPool threadPool) - throws LDAPException { + throws LDAPException { ActiveDirectorySessionFactory sessionFactory = new ActiveDirectorySessionFactory(config, sslService, threadPool); if (sessionFactory.getConnectionPool() != null) { // don't use this in production diff --git a/x-pack/qa/third-party/active-directory/src/test/java/org/elasticsearch/xpack/security/authc/ldap/MultiGroupMappingIT.java b/x-pack/qa/third-party/active-directory/src/test/java/org/elasticsearch/xpack/security/authc/ldap/MultiGroupMappingIT.java index d36d9d7359f9d..2d3a5656ea50a 100644 --- a/x-pack/qa/third-party/active-directory/src/test/java/org/elasticsearch/xpack/security/authc/ldap/MultiGroupMappingIT.java +++ b/x-pack/qa/third-party/active-directory/src/test/java/org/elasticsearch/xpack/security/authc/ldap/MultiGroupMappingIT.java @@ -18,27 +18,27 @@ public class MultiGroupMappingIT extends AbstractAdLdapRealmTestCase { @BeforeClass public static void setRoleMappingType() { - final String extraContent = "MarvelCharacters:\n" + - " - \"CN=SHIELD,CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com\"\n" + - " - \"CN=Avengers,CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com\"\n" + - " - \"CN=Gods,CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com\"\n" + - " - \"CN=Philanthropists,CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com\"\n" + - " - \"cn=SHIELD,ou=people,dc=oldap,dc=test,dc=elasticsearch,dc=com\"\n" + - " - \"cn=Avengers,ou=people,dc=oldap,dc=test,dc=elasticsearch,dc=com\"\n" + - " - \"cn=Gods,ou=people,dc=oldap,dc=test,dc=elasticsearch,dc=com\"\n" + - " - \"cn=Philanthropists,ou=people,dc=oldap,dc=test,dc=elasticsearch,dc=com\"\n"; + final String extraContent = "MarvelCharacters:\n" + + " - \"CN=SHIELD,CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com\"\n" + + " - \"CN=Avengers,CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com\"\n" + + " - \"CN=Gods,CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com\"\n" + + " - \"CN=Philanthropists,CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com\"\n" + + " - \"cn=SHIELD,ou=people,dc=oldap,dc=test,dc=elasticsearch,dc=com\"\n" + + " - \"cn=Avengers,ou=people,dc=oldap,dc=test,dc=elasticsearch,dc=com\"\n" + + " - \"cn=Gods,ou=people,dc=oldap,dc=test,dc=elasticsearch,dc=com\"\n" + + " - \"cn=Philanthropists,ou=people,dc=oldap,dc=test,dc=elasticsearch,dc=com\"\n"; roleMappings = CollectionUtils.appendToCopy(roleMappings, new RoleMappingEntry(extraContent, null)); } @Override protected String configRoles() { - return super.configRoles() + - "\n" + - "MarvelCharacters:\n" + - " cluster: [ NONE ]\n" + - " indices:\n" + - " - names: 'marvel_comics'\n" + - " privileges: [ all ]\n"; + return super.configRoles() + + "\n" + + "MarvelCharacters:\n" + + " cluster: [ NONE ]\n" + + " indices:\n" + + " - names: 'marvel_comics'\n" + + " privileges: [ all ]\n"; } public void testGroupMapping() throws IOException { diff --git a/x-pack/qa/third-party/active-directory/src/test/java/org/elasticsearch/xpack/security/authc/ldap/MultipleAdRealmIT.java b/x-pack/qa/third-party/active-directory/src/test/java/org/elasticsearch/xpack/security/authc/ldap/MultipleAdRealmIT.java index b2c40436dde54..1abc0da2a2b2b 100644 --- a/x-pack/qa/third-party/active-directory/src/test/java/org/elasticsearch/xpack/security/authc/ldap/MultipleAdRealmIT.java +++ b/x-pack/qa/third-party/active-directory/src/test/java/org/elasticsearch/xpack/security/authc/ldap/MultipleAdRealmIT.java @@ -29,13 +29,17 @@ public class MultipleAdRealmIT extends AbstractAdLdapRealmTestCase { public static void setupSecondaryRealm() { // Pick a secondary realm that has the inverse value for 'loginWithCommonName' compare with the primary realm final List configs = Arrays.stream(RealmConfig.values()) - .filter(config -> config.loginWithCommonName != AbstractAdLdapRealmTestCase.realmConfig.loginWithCommonName) - .filter(config -> config.name().startsWith("AD")) - .collect(Collectors.toList()); + .filter(config -> config.loginWithCommonName != AbstractAdLdapRealmTestCase.realmConfig.loginWithCommonName) + .filter(config -> config.name().startsWith("AD")) + .collect(Collectors.toList()); secondaryRealmConfig = randomFrom(configs); - LogManager.getLogger(MultipleAdRealmIT.class).info( + LogManager.getLogger(MultipleAdRealmIT.class) + .info( "running test with secondary realm configuration [{}], with direct group to role mapping [{}]. Settings [{}]", - secondaryRealmConfig, secondaryRealmConfig.mapGroupsAsRoles, secondaryRealmConfig.settings); + secondaryRealmConfig, + secondaryRealmConfig.mapGroupsAsRoles, + secondaryRealmConfig.settings + ); // It's easier to test 2 realms when using file based role mapping, and for the purposes of // this test, there's no need to test native mappings. @@ -48,8 +52,11 @@ protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { builder.put(super.nodeSettings(nodeOrdinal, otherSettings)); final List secondaryRoleMappings = secondaryRealmConfig.selectRoleMappings(() -> true); - final Settings secondarySettings = super.buildRealmSettings(secondaryRealmConfig, secondaryRoleMappings, - getNodeTrustedCertificates()); + final Settings secondarySettings = super.buildRealmSettings( + secondaryRealmConfig, + secondaryRoleMappings, + getNodeTrustedCertificates() + ); secondarySettings.keySet().forEach(name -> { final String newname; if (name.contains(LdapRealmSettings.AD_TYPE)) { diff --git a/x-pack/qa/third-party/active-directory/src/test/java/org/elasticsearch/xpack/security/authc/ldap/UserAttributeGroupsResolverTests.java b/x-pack/qa/third-party/active-directory/src/test/java/org/elasticsearch/xpack/security/authc/ldap/UserAttributeGroupsResolverTests.java index 4bab17068a0f5..5a8350739ef6b 100644 --- a/x-pack/qa/third-party/active-directory/src/test/java/org/elasticsearch/xpack/security/authc/ldap/UserAttributeGroupsResolverTests.java +++ b/x-pack/qa/third-party/active-directory/src/test/java/org/elasticsearch/xpack/security/authc/ldap/UserAttributeGroupsResolverTests.java @@ -9,6 +9,7 @@ import com.unboundid.ldap.sdk.Attribute; import com.unboundid.ldap.sdk.SearchRequest; import com.unboundid.ldap.sdk.SearchScope; + import org.elasticsearch.common.settings.Settings; import org.elasticsearch.core.TimeValue; import org.elasticsearch.xpack.core.security.authc.RealmConfig; @@ -31,15 +32,25 @@ public class UserAttributeGroupsResolverTests extends GroupsResolverTestCase { private static final RealmConfig.RealmIdentifier REALM_ID = new RealmConfig.RealmIdentifier("ldap", "realm1"); public void testResolve() throws Exception { - //falling back on the 'memberOf' attribute + // falling back on the 'memberOf' attribute UserAttributeGroupsResolver resolver = new UserAttributeGroupsResolver(config(REALM_ID, Settings.EMPTY)); - List groups = - resolveBlocking(resolver, ldapConnection, BRUCE_BANNER_DN, TimeValue.timeValueSeconds(20), NoOpLogger.INSTANCE, null); - assertThat(groups, containsInAnyOrder( + List groups = resolveBlocking( + resolver, + ldapConnection, + BRUCE_BANNER_DN, + TimeValue.timeValueSeconds(20), + NoOpLogger.INSTANCE, + null + ); + assertThat( + groups, + containsInAnyOrder( containsString("Avengers"), containsString("SHIELD"), containsString("Geniuses"), - containsString("Philanthropists"))); + containsString("Philanthropists") + ) + ); } public void testResolveFromPreloadedAttributes() throws Exception { @@ -47,13 +58,23 @@ public void testResolveFromPreloadedAttributes() throws Exception { final Collection attributes = ldapConnection.searchForEntry(preSearch).getAttributes(); UserAttributeGroupsResolver resolver = new UserAttributeGroupsResolver(config(REALM_ID, Settings.EMPTY)); - List groups = - resolveBlocking(resolver, ldapConnection, BRUCE_BANNER_DN, TimeValue.timeValueSeconds(20), NoOpLogger.INSTANCE, attributes); - assertThat(groups, containsInAnyOrder( + List groups = resolveBlocking( + resolver, + ldapConnection, + BRUCE_BANNER_DN, + TimeValue.timeValueSeconds(20), + NoOpLogger.INSTANCE, + attributes + ); + assertThat( + groups, + containsInAnyOrder( containsString("Avengers"), containsString("SHIELD"), containsString("Geniuses"), - containsString("Philanthropists"))); + containsString("Philanthropists") + ) + ); } public void testResolveCustomGroupAttribute() throws Exception { @@ -61,10 +82,16 @@ public void testResolveCustomGroupAttribute() throws Exception { .put(getFullSettingKey("realm1", UserAttributeGroupsResolverSettings.ATTRIBUTE), "seeAlso") .build(); UserAttributeGroupsResolver resolver = new UserAttributeGroupsResolver(config(REALM_ID, settings)); - List groups = - resolveBlocking(resolver, ldapConnection, BRUCE_BANNER_DN, TimeValue.timeValueSeconds(20), NoOpLogger.INSTANCE, null); + List groups = resolveBlocking( + resolver, + ldapConnection, + BRUCE_BANNER_DN, + TimeValue.timeValueSeconds(20), + NoOpLogger.INSTANCE, + null + ); assertThat(groups, hasSize(1)); - assertThat(groups.get(0), containsString("Avengers")); //seeAlso only has Avengers + assertThat(groups.get(0), containsString("Avengers")); // seeAlso only has Avengers } public void testResolveInvalidGroupAttribute() throws Exception { @@ -72,8 +99,14 @@ public void testResolveInvalidGroupAttribute() throws Exception { .put(getFullSettingKey("realm1", UserAttributeGroupsResolverSettings.ATTRIBUTE), "doesntExist") .build(); UserAttributeGroupsResolver resolver = new UserAttributeGroupsResolver(config(REALM_ID, settings)); - List groups = - resolveBlocking(resolver, ldapConnection, BRUCE_BANNER_DN, TimeValue.timeValueSeconds(20), NoOpLogger.INSTANCE, null); + List groups = resolveBlocking( + resolver, + ldapConnection, + BRUCE_BANNER_DN, + TimeValue.timeValueSeconds(20), + NoOpLogger.INSTANCE, + null + ); assertThat(groups, empty()); } diff --git a/x-pack/qa/third-party/jira/src/test/java/org/elasticsearch/smoketest/WatcherJiraYamlTestSuiteIT.java b/x-pack/qa/third-party/jira/src/test/java/org/elasticsearch/smoketest/WatcherJiraYamlTestSuiteIT.java index 236a7535f3d1a..f8977f8d3cf8d 100644 --- a/x-pack/qa/third-party/jira/src/test/java/org/elasticsearch/smoketest/WatcherJiraYamlTestSuiteIT.java +++ b/x-pack/qa/third-party/jira/src/test/java/org/elasticsearch/smoketest/WatcherJiraYamlTestSuiteIT.java @@ -8,6 +8,7 @@ import com.carrotsearch.randomizedtesting.annotations.Name; import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; + import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate; import org.elasticsearch.test.rest.yaml.ClientYamlTestResponse; import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase; @@ -45,13 +46,16 @@ public void startWatcher() throws Exception { getAdminExecutionContext().callApi("watcher.start", emptyMap(), emptyList(), emptyMap()); for (String template : watcherTemplates) { - ClientYamlTestResponse templateExistsResponse = getAdminExecutionContext().callApi("indices.exists_template", - singletonMap("name", template), emptyList(), emptyMap()); + ClientYamlTestResponse templateExistsResponse = getAdminExecutionContext().callApi( + "indices.exists_template", + singletonMap("name", template), + emptyList(), + emptyMap() + ); assertThat(templateExistsResponse.getStatusCode(), is(200)); } - ClientYamlTestResponse response = - getAdminExecutionContext().callApi("watcher.stats", emptyMap(), emptyList(), emptyMap()); + ClientYamlTestResponse response = getAdminExecutionContext().callApi("watcher.stats", emptyMap(), emptyList(), emptyMap()); String state = (String) response.evaluate("stats.0.watcher_state"); assertThat(state, is("started")); } catch (IOException e) { @@ -65,8 +69,7 @@ public void stopWatcher() throws Exception { assertBusy(() -> { try { getAdminExecutionContext().callApi("watcher.stop", emptyMap(), emptyList(), emptyMap()); - ClientYamlTestResponse response = - getAdminExecutionContext().callApi("watcher.stats", emptyMap(), emptyList(), emptyMap()); + ClientYamlTestResponse response = getAdminExecutionContext().callApi("watcher.stats", emptyMap(), emptyList(), emptyMap()); String state = (String) response.evaluate("stats.0.watcher_state"); assertThat(state, is("stopped")); } catch (IOException e) { diff --git a/x-pack/qa/third-party/pagerduty/src/test/java/org/elasticsearch/smoketest/WatcherPagerDutyYamlTestSuiteIT.java b/x-pack/qa/third-party/pagerduty/src/test/java/org/elasticsearch/smoketest/WatcherPagerDutyYamlTestSuiteIT.java index 85fd6fc4a3de0..10352b54912e5 100644 --- a/x-pack/qa/third-party/pagerduty/src/test/java/org/elasticsearch/smoketest/WatcherPagerDutyYamlTestSuiteIT.java +++ b/x-pack/qa/third-party/pagerduty/src/test/java/org/elasticsearch/smoketest/WatcherPagerDutyYamlTestSuiteIT.java @@ -8,6 +8,7 @@ import com.carrotsearch.randomizedtesting.annotations.Name; import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; + import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate; import org.elasticsearch.test.rest.yaml.ClientYamlTestResponse; import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase; @@ -45,13 +46,16 @@ public void startWatcher() throws Exception { getAdminExecutionContext().callApi("watcher.start", emptyMap(), emptyList(), emptyMap()); for (String template : watcherTemplates) { - ClientYamlTestResponse templateExistsResponse = getAdminExecutionContext().callApi("indices.exists_template", - singletonMap("name", template), emptyList(), emptyMap()); + ClientYamlTestResponse templateExistsResponse = getAdminExecutionContext().callApi( + "indices.exists_template", + singletonMap("name", template), + emptyList(), + emptyMap() + ); assertThat(templateExistsResponse.getStatusCode(), is(200)); } - ClientYamlTestResponse response = - getAdminExecutionContext().callApi("watcher.stats", emptyMap(), emptyList(), emptyMap()); + ClientYamlTestResponse response = getAdminExecutionContext().callApi("watcher.stats", emptyMap(), emptyList(), emptyMap()); String state = (String) response.evaluate("stats.0.watcher_state"); assertThat(state, is("started")); } catch (IOException e) { @@ -65,8 +69,7 @@ public void stopWatcher() throws Exception { assertBusy(() -> { try { getAdminExecutionContext().callApi("watcher.stop", emptyMap(), emptyList(), emptyMap()); - ClientYamlTestResponse response = - getAdminExecutionContext().callApi("watcher.stats", emptyMap(), emptyList(), emptyMap()); + ClientYamlTestResponse response = getAdminExecutionContext().callApi("watcher.stats", emptyMap(), emptyList(), emptyMap()); String state = (String) response.evaluate("stats.0.watcher_state"); assertThat(state, is("stopped")); } catch (IOException e) { diff --git a/x-pack/qa/third-party/slack/src/test/java/org/elasticsearch/smoketest/WatcherSlackYamlTestSuiteIT.java b/x-pack/qa/third-party/slack/src/test/java/org/elasticsearch/smoketest/WatcherSlackYamlTestSuiteIT.java index 6a1d746bb7563..5c083cb90cd69 100644 --- a/x-pack/qa/third-party/slack/src/test/java/org/elasticsearch/smoketest/WatcherSlackYamlTestSuiteIT.java +++ b/x-pack/qa/third-party/slack/src/test/java/org/elasticsearch/smoketest/WatcherSlackYamlTestSuiteIT.java @@ -8,6 +8,7 @@ import com.carrotsearch.randomizedtesting.annotations.Name; import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; + import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate; import org.elasticsearch.test.rest.yaml.ClientYamlTestResponse; import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase; @@ -45,13 +46,16 @@ public void startWatcher() throws Exception { getAdminExecutionContext().callApi("watcher.start", emptyMap(), emptyList(), emptyMap()); for (String template : watcherTemplates) { - ClientYamlTestResponse templateExistsResponse = getAdminExecutionContext().callApi("indices.exists_template", - singletonMap("name", template), emptyList(), emptyMap()); + ClientYamlTestResponse templateExistsResponse = getAdminExecutionContext().callApi( + "indices.exists_template", + singletonMap("name", template), + emptyList(), + emptyMap() + ); assertThat(templateExistsResponse.getStatusCode(), is(200)); } - ClientYamlTestResponse response = - getAdminExecutionContext().callApi("watcher.stats", emptyMap(), emptyList(), emptyMap()); + ClientYamlTestResponse response = getAdminExecutionContext().callApi("watcher.stats", emptyMap(), emptyList(), emptyMap()); String state = (String) response.evaluate("stats.0.watcher_state"); assertThat(state, is("started")); } catch (IOException e) { @@ -65,8 +69,7 @@ public void stopWatcher() throws Exception { assertBusy(() -> { try { getAdminExecutionContext().callApi("watcher.stop", emptyMap(), emptyList(), emptyMap()); - ClientYamlTestResponse response = - getAdminExecutionContext().callApi("watcher.stats", emptyMap(), emptyList(), emptyMap()); + ClientYamlTestResponse response = getAdminExecutionContext().callApi("watcher.stats", emptyMap(), emptyList(), emptyMap()); String state = (String) response.evaluate("stats.0.watcher_state"); assertThat(state, is("stopped")); } catch (IOException e) {